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.
You'll need the following tools installed:
- Go (1.16 or later)
- GDB (GNU Debugger)
- Delve (Go debugger)
- Make
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.
make install-deps-mac
This will install GDB and Delve using Homebrew.
make install-deps-linux
This will install GDB using apt-get and Delve using Go.
make build
This will build the sample application with debug information.
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.
To run just the GDB demo:
make demo-gdb
This will:
- Build the application
- Set up a tracepoint at the
processItem
function - Configure the tracepoint to collect the
id
parameter and register values - Run the program
- Display the collected trace data
To run just the Delve demo:
make demo-delve
This will:
- Build the application
- Set up a tracepoint at the
processItem
function - Configure the tracepoint to print the
id
parameter - Run the program
- Display the trace output as the program executes
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.
# 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
# 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