GDB – 附加到并中断正在运行的Go应用程序

huangapple go评论93阅读模式
英文:

GDB - Attach to and break a running Go application

问题

我使用调试标志编译了一个简单的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并执行了breakbreak 11

gdb --pid=<pid>

Gdb报告断点已成功设置,但它们从未被触发。有没有办法让它工作?

英文:

I compiled a simple go application with debug flags:

go build -gcflags &quot;-N -l&quot; -o main main.go

main.go

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

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

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

gdb --pid=&lt;pid&gt;

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

答案1

得分: 1

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.

英文:

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.

答案2

得分: 1

请注意,即使在将您的runtime-gdb.py添加到.gdbrc中时,也可能无法在Ubuntu 13.10上工作,您将收到一个“SyntaxError”消息,如下所示:

> 问题在于Ubuntu 13.10将GDB链接到Python 3.3,而golang提供的脚本是为Python 2编写的。有人已经提出了一个问题,并且似乎已经在上游修复了(所以预计Go 1.3会正常工作)。
>
> 幸运的是,修复问题很容易。只需将现有的runtime-gdb.py移出路径,并下载上游版本替换它。
>
> 如果您的$GOROOT/usr/local/go,则以下操作应该正常工作:

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
英文:

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

huangapple
  • 本文由 发表于 2013年7月3日 12:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/17439471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定