英文:
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
并执行了break
和break 11
。
gdb --pid=<pid>
Gdb报告断点已成功设置,但它们从未被触发。有没有办法让它工作?
英文:
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?
答案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
”消息,如下所示:
- 来自Michael Susens-Schurter(
schmichael
)的博文“在Ubuntu 13.10上使用GDB调试Go 1.2” - 问题6698(gdb:升级为兼容Python 3)
> 问题在于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:
- blog post "Debugging Go 1.2 on Ubuntu 13.10 with GDB" from Michael Susens-Schurter (
schmichael
) - issue 6698 (gdb: upgrade to be python 3 compatible)
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论