gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:09:04 26 4
gpt4 key购买 nike

一个简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com