Go使用gdb调试

其实我一般调试都是直接打log的,不过gdb调试还是很有用处,尤其是当碰到一些底层错误的需要单步跟踪的时候,比如,想研究一下 Go的runtime是如何实现的的时候。

首先在编译Go程序的时候,要让Go带上编译信息:

$ go build -gcflags=all="-N -l" .
$ gdb test
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 test...done.
Loading Go Runtime support.
(gdb) 

注意这里,它可能会提醒你说,要加一个 .py~/.gdbinit 里,加!比如我加的就是:

$ cat ~/.gdbinit 
set auto-load safe-path /snap/go

来看下常见命令:

  • list。列出代码,输入第一次后如果输入回车,那么就会重复以上命令
  • break。加断点,一般是list之后,break 行号来加断点
  • bt。打印调用链
  • info files。打印调试文件信息
  • run。运行所要调试的代码
  • up 和 down。在frame里跳来跳去
  • info args 和 info locals 打印参数和本地变量
  • whatis 和 p。打印变量和想要看的值,例如数组啊,函数啊,都可以
  • info goroutines。查看所有的goroutine及其ID
  • goroutine 命令。对对应的goroutine执行命令。
  • q。退出
  • help。打印帮助文档

接下来,我们来看看实战例子:

$ go build -gcflags=all="-N -l" .
$ gdb ./test
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 ./test...done.
Loading Go Runtime support.
(gdb) info files
Symbols from "/home/jiajun/Code/test/test".
Local exec file:
	`/home/jiajun/Code/test/test', file type elf64-x86-64.
	Entry point: 0x45cc50
	0x0000000000401000 - 0x000000000045fcdb is .text
	0x0000000000460000 - 0x0000000000486f49 is .rodata
	0x0000000000487120 - 0x0000000000487778 is .typelink
	0x0000000000487778 - 0x0000000000487780 is .itablink
	0x0000000000487780 - 0x0000000000487780 is .gosymtab
	0x0000000000487780 - 0x00000000004ceaef is .gopclntab
	0x00000000004cf000 - 0x00000000004cf020 is .go.buildinfo
	0x00000000004cf020 - 0x00000000004d0620 is .noptrdata
	0x00000000004d0620 - 0x00000000004d2450 is .data
	0x00000000004d2460 - 0x00000000004fb9d0 is .bss
	0x00000000004fb9e0 - 0x00000000004fe1e8 is .noptrbss
	0x0000000000400f9c - 0x0000000000401000 is .note.go.buildid
(gdb) list
1	package main
2	
3	func add(a, b int64) int64 {
4		var i int64 = 1
5		return a + b + i
6	}
7	
8	func main() {
9		println(add(1, 2))
10	}
(gdb) b 9
Breakpoint 1 at 0x45fc8d: file /home/jiajun/Code/test/main.go, line 9.
(gdb) run
Starting program: /home/jiajun/Code/test/test 
[New LWP 25172]
[New LWP 25173]
[New LWP 25174]
[New LWP 25175]
[New LWP 25176]

Thread 1 "test" hit Breakpoint 1, main.main () at /home/jiajun/Code/test/main.go:9
9		println(add(1, 2))
(gdb) s
main.add (a=1, b=2, ~r2=824634032128) at /home/jiajun/Code/test/main.go:3
3	func add(a, b int64) int64 {
(gdb) list
1	package main
2	
3	func add(a, b int64) int64 {
4		var i int64 = 1
5		return a + b + i
6	}
7	
8	func main() {
9		println(add(1, 2))
10	}
(gdb) n
4		var i int64 = 1
(gdb) info args
a = 1
b = 2
~r2 = 0
(gdb) info locals
i = 530
(gdb) p a
$1 = 1
(gdb) whatis add
No symbol "add" in current context.
(gdb) whatis main.add
type = void (int64, int64, int64)
(gdb) bt
#0  main.add (a=1, b=2, ~r2=0) at /home/jiajun/Code/test/main.go:4
#1  0x000000000045fca3 in main.main () at /home/jiajun/Code/test/main.go:9
(gdb) up
#1  0x000000000045fca3 in main.main () at /home/jiajun/Code/test/main.go:9
9		println(add(1, 2))
(gdb) down
#0  main.add (a=1, b=2, ~r2=0) at /home/jiajun/Code/test/main.go:4
4		var i int64 = 1
(gdb) info goroutines 
* 1 running  runtime.asyncPreempt2
  2 waiting  runtime.gopark
  3 waiting  runtime.gopark
  4 waiting  runtime.gopark
(gdb) goroutine 2 bt
#0  runtime.gopark (unlockf={void (runtime.g *, void *, bool *)} 0xc00002ef88, lock=0x4d2650 <runtime.forcegc>, reason=17 '\021', traceEv=20 '\024', traceskip=1)
    at /snap/go/5646/src/runtime/proc.go:305
#1  0x0000000000430c23 in runtime.goparkunlock (lock=0x4d2650 <runtime.forcegc>, reason=17 '\021', traceEv=20 '\024', traceskip=1) at /snap/go/5646/src/runtime/proc.go:310
#2  0x0000000000430a0a in runtime.forcegchelper () at /snap/go/5646/src/runtime/proc.go:253
#3  0x000000000045b261 in runtime.goexit () at /snap/go/5646/src/runtime/asm_amd64.s:1373
#4  0x0000000000000000 in ?? ()
(gdb) q
A debugging session is active.

	Inferior 1 [process 25168] will be killed.

Quit anyway? (y or n) y

所以如果你想看runtime代码的话,那么就应该先找到Go代码到底从哪里开始运行:

$ gdb test
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
Copyright (C) 2018 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 test...done.
Loading Go Runtime support.
(gdb) info files
Symbols from "/home/jiajun/Code/test/test".
Local exec file:
	`/home/jiajun/Code/test/test', file type elf64-x86-64.
	Entry point: 0x45cc50
	0x0000000000401000 - 0x000000000045fcdb is .text
	0x0000000000460000 - 0x0000000000486f49 is .rodata
	0x0000000000487120 - 0x0000000000487778 is .typelink
	0x0000000000487778 - 0x0000000000487780 is .itablink
	0x0000000000487780 - 0x0000000000487780 is .gosymtab
	0x0000000000487780 - 0x00000000004ceaef is .gopclntab
	0x00000000004cf000 - 0x00000000004cf020 is .go.buildinfo
	0x00000000004cf020 - 0x00000000004d0620 is .noptrdata
	0x00000000004d0620 - 0x00000000004d2450 is .data
	0x00000000004d2460 - 0x00000000004fb9d0 is .bss
	0x00000000004fb9e0 - 0x00000000004fe1e8 is .noptrbss
	0x0000000000400f9c - 0x0000000000401000 is .note.go.buildid
(gdb) b *0x45cc50
Breakpoint 1 at 0x45cc50
(gdb) run
Starting program: /home/jiajun/Code/test/test 

Breakpoint 1, 0x000000000045cc50 in _rt0_amd64_linux ()
(gdb) s
Single stepping until exit from function _rt0_amd64_linux,
which has no line number information.
_rt0_amd64 () at /snap/go/5646/src/runtime/asm_amd64.s:15
15		MOVQ	0(SP), DI	// argc
(gdb) 

就这样,我们就晓得了,原来是在 /snap/go/5646/src/runtime/asm_amd64.s 的第15行开始的,接下来就一路跟下去就可以了。


参考资料:


更多文章
  • 长任务系统如何处理?
  • macOS/Linux 编译 InputLeap
  • 使用开源软KVM - synergy-core
  • 解决 macOS 终端hostname一直变化问题
  • KVM 共享 Intel 集成显卡
  • PromQL 备忘
  • 读《格鲁夫给经理人的第一课》
  • 读《打开心智》
  • 为什么要把复杂的联表操作拆成多个单表查询?
  • 红包系统的设计
  • 2022年终总结
  • MySQL Index Condition Pushdown Optimization
  • Go mod 简明教程
  • OpenWRT 使用 Android/iOS USB 网络
  • 搭建旁路由