dongtanxi5676756 2018-06-04 08:10
浏览 89
已采纳

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

I success install gdb 8.0.1 and make it run in mac os x. When debug this program I don't see the address of key.

package main

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

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

Here is what I have done with 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'}

I give a look at lldb. (lldb need b on main.main then 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) 

And my question is how can show the address of key in gdb?

  • 写回答

1条回答 默认 最新

  • duanmeng9336 2018-06-30 06:47
    关注

    You can either disable the Python pretty-printers for Go, then you will get this:

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

    Or you can temporarily switch to the C language, like this:

    (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) 
    

    This assumes that Go arrays are interpreted by GDB in a certain way in C mode, but it seems to be to work in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?