debugging - gdb 如何在 go 程序中打印 var 的地址?

标签 debugging go gdb lldb

我成功安装了 gdb 8.0.1 并使其在 mac os x 中运行。调试此程序时,我没有看到 key 的地址。

package main

func main(){
    m := map[string]int{
        "abc":123,
    }

    key := []byte("abc")
    x, ok := m[string(key)]
    println(x, ok)
}

这是我用 gdb 所做的:

go build -gcflags "-N" test_append.go
gdb test_append
(gdb) b 9
Breakpoint 1 at 0x104d4b4: file /Users/jiamo/go/src/test/test_append.go, line 9.
(gdb) c
The program is not being run.
(gdb) run
Starting program: /Users/jiamo/go/src/test/test_append
Thread 3 hit Breakpoint 1, main.main () at /Users/jiamo/go/src/test/test_append.go:9
9       x, ok := m[string(key)]
(gdb) info locals
key =  []uint8 = {97 'a', 98 'b', 99 'c'}
m = map[string]int = {["abc"] = 123}
ok = false
x = 17195648
(gdb) p key
$1 =  []uint8 * = {97 'a', 98 'b', 99 'c'}
(gdb) p &key
$2 =  []uint8 * = {97 'a', 98 'b', 99 'c'}

我看看 lldb。 (lldb 需要 b on main.main 然后 b on line)

(lldb) b main.main
Breakpoint 1: where = test_append`main.main + 50 at test_append.go:4, address = 0x000000000104d372
(lldb) run
(lldb) b 9
(lldb) c
(lldb) fr v
    ([]uint8) key = (len 3, cap 32) {
    [0] = 97
    [1] = 98
    [2] = 99
    }
    # no address too

(lldb) p key
([]uint8) key = (len 3, cap 32) {
[0] = 97
[1] = 98
[2] = 99
}
(lldb) p &key
(*[]uint8)  = 0x000000c420055e10 (len 0, cap 0)   
# now it can show the address, 
# And I am not sure why it becomes (len 0, cap 0) 

我的问题是如何在gdb中显示key的地址?

最佳答案

你可以为 Go 禁用 Python pretty-print ,然后你会得到这个:

(gdb) print key
$1 = {array = 0xc42003de10 "abc", len = 3, cap = 32}

或者你可以暂时切换到C语言,像这样:

(gdb) set language c
Warning: the current language does not match this frame.
(gdb) print key
$1 =  []uint8 = {97 'a', 98 'b', 99 'c'}
(gdb) print (char *)key
$2 = 0xc420043e10 "abc"
(gdb) 

这假设 Go 数组在 C 模式下由 GDB 以某种方式解释,但在这种情况下似乎可行。

关于debugging - gdb 如何在 go 程序中打印 var 的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50676025/

相关文章:

在 DrRacket 中调试多个文件

javascript - 无法找到回调处理程序

windows - 为什么不加载符号进行调试?

go - 如何调用外部程序并处理其输出?

PHP 和 Golang sha512 不同的结果

go - net/http 不适用于 ipv6 地址

python - pyenv环境下如何搭建gdb

c++ - 在 qt creator 中单步执行调试器导致 gdb 在几秒钟后崩溃

shell - gdb可以调试suid root程序吗?

javascript - 我可以在 chrome 开发工具中模拟我的请求响应吗?