Ilia Choly
Ilia Choly

Reputation: 18557

GDB - Attach to and break a running Go application

I compiled a simple go application with debug flags:

go build -gcflags "-N -l" -o main main.go

main.go

package main

import (
    "fmt"
    "time"
)

func main() {
    for i := 0; true; i++ {
        fmt.Println("number:", i)
        time.Sleep(time.Second)
    }
}

In gdb, I attached to its pid and executed break and break 11.

gdb --pid=<pid>

Gdb reports that the breakpoints are successfully set, but they don't ever get hit. Is there a way to get this working?

Upvotes: 5

Views: 1651

Answers (2)

VonC
VonC

Reputation: 1323125

Note: that same setup (even when adding your runtime-gdb.py to your .gdbrc) risks to not work with Ubuntu 13.10, where you would get a "SyntaxError" message, as illustrated in:

The problem is that Ubuntu 13.10 links GDB against Python 3.3 while the script golang ships is for Python 2. Someone has already filed an issue, and it appears to be fixed upstream (so expect Go 1.3 to Just Work).

Luckily backporting the fix is easy. Simply move your exist runtime-gdb.py out of the way and download the upstream version in its place.

If your $GOROOT is /usr/local/go the following should Just Work:

sudo mv /usr/local/go/src/pkg/runtime/runtime-gdb.py /usr/local/go/src/pkg/runtime/runtime-gdb.py.orig
cd /usr/local/go/src/pkg/runtime/
sudo wget https://go.googlecode.com/hg/src/pkg/runtime/runtime-gdb.py

Upvotes: 1

fabmilo
fabmilo

Reputation: 48310

the go/src/pkg/runtime/runtime-gdb.py script needs to be loaded inside gdb to effectively be able to debug go programs.

You can add it to the .gdbrc file.

Upvotes: 1

Related Questions