环境说明

macOs bigSur 11.4
GNU gdb (GDB) 10.2

代码准备

package main

type Ner interface {
    a()
    b(int)
    c(string) string
}

type N int

func (N) a() {
}

func (*N) b(i int) {
    return
}

func (*N) c(s string) string {
    return "c"
}

func main() {
    var n N
    var t Ner = &n
    t.a()
}

命令执行

go build -gcflags "-N -l" -ldflags=-compressdwarf=false main.go
gdb main

正常输出

注意必须要有:Loading Go Runtime support.

deMacBook-Pro:debugGDB de$ gdb main
GNU gdb (GDB) 10.2
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin20.3.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
Loading Go Runtime support.
(gdb) 
  1. 设置断点(在第25行),然后执行
(gdb) b main.go:25

Loading Go Runtime support.
(gdb) b main.go:25
Breakpoint 1 at 0x1054d98: file /Users/didi/GolandProjects/goLarnNote/Chapter7/Iface/debugGDB/main.go, line 25.
(gdb) run
Starting program: /Users/didi/GolandProjects/goLarnNote/Chapter7/Iface/debugGDB/main 
[New Thread 0x2603 of process 72590]
[New Thread 0x1a03 of process 72590]
warning: unhandled dyld version (17)
[New Thread 0x2403 of process 72590]
[New Thread 0x2507 of process 72590]
[New Thread 0x2203 of process 72590]

Thread 2 hit Breakpoint 1, main.main () at /Users/didi/GolandProjects/goLarnNote/Chapter7/Iface/debugGDB/main.go:25
25              t.a()
  1. 查看信息并打印
(gdb) info locals
t = {tab = 0x1076b60 <N,main.Ner>, data = 0xc00003c748}
n = 0
(gdb) set print pretty on
(gdb) p *t.tab.inter
$10 = {
  typ = {
    size = 16,
    ptrdata = 16,
    hash = 1801897693,
    tflag = 7 '\a',
    align = 8 '\b',
    fieldAlign = 8 '\b',
    kind = 20 '\024',
    equal = {void (void *, void *, bool *)} 0x105b818 <type.*+26936>,
    gcdata = 0x1065b18 <func.*+254> "\002",
    str = 2790,
    ptrToThis = 9664
  },
  pkgpath = {
    bytes = 0x105500a <type.*+298> ""
  },
  mhdr = []runtime.imethod = {{
      name = 8,
      ityp = 15552
    }, {
      name = 11,
--Type <RET> for more, q to quit, c to continue without paging--

(gdb) ptype t
type = struct runtime.iface {
    runtime.itab *tab;
    void *data;
}
(gdb) p t
$2 = {tab = 0x1076b60 <N,main.Ner>, data = 0xc00003c748}

(gdb) ptype t.tab._type
type = struct runtime._type {
    uintptr size;
    uintptr ptrdata;
    uint32 hash;
    runtime.tflag tflag;
    uint8 align;
    uint8 fieldAlign;
    uint8 kind;
    func(unsafe.Pointer, unsafe.Pointer) bool equal;
    uint8 *gcdata;
--Type <RET> for more, q to quit, c to continue without paging--

后续todo

结合iface,eface的结构来剖析接口判等 等系列问题

参考资料

gdb-debugger-with-go


牙小木木
1.5k 声望80 粉丝

iamtb.cn