Skip to content

developmeh/debug-tracing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Debug Tracing Demo

This project demonstrates how to use tracepoints with GDB and Delve for debugging Go applications. Tracepoints are like breakpoints that don't actually stop execution - they just log information when hit, making them perfect for understanding program flow without interrupting it.

Prerequisites

You'll need the following tools installed:

  • Go (1.16 or later)
  • GDB (GNU Debugger)
  • Delve (Go debugger)
  • Make

Installing Dependencies

Using Nix

If you have Nix installed, you can use the provided flake to set up the development environment:

nix develop

This will provide all the necessary tools.

On macOS

make install-deps-mac

This will install GDB and Delve using Homebrew.

On Linux

make install-deps-linux

This will install GDB using apt-get and Delve using Go.

Building the Application

make build

This will build the sample application with debug information.

Running the Demos

Running Both Demos

To run both the GDB and Delve demos in sequence:

make demo

This will run the GDB demo first, followed by the Delve demo, with a prompt in between.

GDB Tracepoint Demo

To run just the GDB demo:

make demo-gdb

This will:

  1. Build the application
  2. Set up a tracepoint at the processItem function
  3. Configure the tracepoint to collect the id parameter and register values
  4. Run the program
  5. Display the collected trace data

Delve Tracepoint Demo

To run just the Delve demo:

make demo-delve

This will:

  1. Build the application
  2. Set up a tracepoint at the processItem function
  3. Configure the tracepoint to print the id parameter
  4. Run the program
  5. Display the trace output as the program executes

How It Works

The sample application is a simple Go program that processes items in a loop:

func processItem(id int) string {
    fmt.Printf("Processing item %d\n", id)
    time.Sleep(100 * time.Millisecond)
    result := fmt.Sprintf("result-%d", id)
    return result
}

func main() {
    for i := 0; i < 5; i++ {
        result := processItem(i)
        fmt.Printf("Got result: %s\n", result)
    }
}

When you run the GDB or Delve demo, tracepoints are set at the processItem function, allowing you to see when the function is called and what parameters are passed to it, without interrupting the program's execution.

Manual Usage

GDB

# Compile with debug info
go build -gcflags="-N -l" -o bin/sample-app ./cmd/sample-app

# Start GDB
gdb ./bin/sample-app

# Set a tracepoint at processItem
(gdb) trace processItem

# Define what to collect when the tracepoint is hit
(gdb) actions
Enter actions for tracepoint 1, one per line:
> collect id
> collect $regs
> end

# Run the program
(gdb) run

# After execution, view the trace data
(gdb) tfind start
(gdb) tfind  # Move to next trace frame

Delve

# Start Delve
dlv debug ./cmd/sample-app/main.go

# Set a tracepoint
(dlv) trace processItem

# Configure trace output
(dlv) on 1 print id, "Processing item"

# Continue execution
(dlv) continue

Further Reading