如何gdb打印var in go程序的地址?

时间:2018-06-04 08:10:31

标签: 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需要main.main上的b然后b在线)

(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的地址?

1 个答案:

答案 0 :(得分:1)

您可以为Go禁用Python漂亮打印机,然后您将获得以下信息:

(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以某种方式解释,但是在这种情况下似乎可以使用。