go - 在 macOS 上使用 GDB 调试 Go 程序

标签 go gdb

一个简单的 Go 程序,比如 main.go:

package main

func main() {
    println("hello, world!")
}

然后构建

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

使用 GDB:

$ gdb main
GNU gdb (GDB) 8.2
(...)
Reading symbols from main...(no debugging symbols found)...done.
Loading Go Runtime support.
(gdb) source /usr/local/Cellar/go/1.11/libexec/src/runtime/runtime-gdb.py
Loading Go Runtime support.
(gdb) info files
Symbols from "/Users/changkun/Desktop/demo/main".
Local exec file:
        `/Users/changkun/Desktop/demo/main', file type mach-o-x86-64.
        Entry point: 0x1049e20
        0x0000000001001000 - 0x000000000104dfcf is .text
        0x000000000104dfe0 - 0x0000000001077344 is __TEXT.__rodata
        (...)
(gdb) b *0x1049e20
Breakpoint 1 at 0x1049e20
(gdb)

GDB输出中没有at,Go的版本是go version go1.11 darwin/amd64 并且:

$ ls -al /usr/local/bin | grep go
lrwxr-xr-x    1 changkun  admin        24 Aug 25 16:37 go -> ../Cellar/go/1.11/bin/go

======

linux环境下相同进程:

docker run -itd --name golang golang:1.11
docker exec -it golang bash

然后进入container install gdb

root@1326d3f1a957:/# gdb main
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
(...)
(gdb) info files
Symbols from "/main".
Local exec file:
        `/main', file type elf64-x86-64.
        Entry point: 0x44a2e0
        0x0000000000401000 - 0x000000000044ea8f is .text
        (...)
(gdb) b *0x44a2e0
Breakpoint 1 at 0x44a2e0: file /usr/local/go/src/runtime/rt0_linux_amd64.s, line 8.
(gdb)

Linux 能够显示 (gdb) b *0x44a2e0 0x44a2e0 处的断点 1:文件/usr/local/go/src/runtime/rt0_linux_amd64.s,第 8 行。

我在 macOS 中错过了什么?如何在 macOS 上调试和跟踪程序?

最佳答案

在 Go 1.11 中,调试信息被压缩以减少二进制大小,Mac 上的 gdb 不理解压缩的 DWARF。

解决方法是同时指定 -ldflags=-compressdwarf=false,这完全符合它的要求。

通常要这样做:

export GOFLAGS="-ldflags=-compressdwarf=false"

参见讨论:https://groups.google.com/forum/#!topic/golang-nuts/LlgN1qpbRE8

关于go - 在 macOS 上使用 GDB 调试 Go 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52534287/

相关文章:

c - Valgrind 条件跳转或移动取决于未初始化的值

c++ - 条件断点失败的 GDB Eigen 调试

c - 我如何将命令 "commands"放入 gdbinit 中?

go - 为什么 CGO_ENABLED=0 的编译速度较慢?

vim 去 : Can't get autocompletion

amazon-web-services - 使用go在dynamodb中创建项目

c++ - gdb 找不到由 clang++ 编译的源文件

go - 如何找出golang channel 的目的地

Git2go : Simulate git checkout and an immediate git push

c++ - 如何在核心转储中检查堆大小和创建的对象