GDB – 连接并中断正在运行的Go应用程序

我用调试标志编译了一个简单的go应用程序:

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)
    }
}

在gdb中,我附加了它的pid并执行了break和break 11.

gdb –pid =< pid>

Gdb报告断点已成功设置,但它们不会受到影响.有没有办法让这个工作?

最佳答案 注意:相同的设置(即使将你的runtime-gdb.py添加到你的.gdbrc中)也有可能无法使用Ubuntu 13.10,你会收到一条“SyntaxError”消息,如下所示:

>博客文章“Debugging Go 1.2 on Ubuntu 13.10 with GDB”,自Michael Susens-Schurter (schmichael)
> issue 6698(gdb:升级为兼容python 3)

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
点赞