惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例) 读《为什么学生不喜欢上学》 OpenAI Prompt Engineering 摘录和总结 读《打造真正的新产品》 VueJS 总结 Linux 自动挂载 alist 提供的webdav FreeBSD 使用 vm-bhyve 安装Debian虚拟机 FreeBSD 和 Linux 网卡聚合实现提速 GPT 帮我搞定了时区转换问题 长任务系统如何处理? macOS/Linux 编译 InputLeap 使用开源软KVM - synergy-core 解决 macOS 终端hostname一直变化问题 KVM 共享 Intel 集成显卡 PromQL 备忘 读《格鲁夫给经理人的第一课》 读《打开心智》 为什么要把复杂的联表操作拆成多个单表查询? 红包系统的设计 MySQL Index Condition Pushdown Optimization Go mod 简明教程 OpenWRT 使用 Android/iOS USB 网络 搭建旁路由 Golang gRPC 错误处理 编写可维护的单元测试代码 OAuth 2 详解(六):Authorization Code Flow with PKCE OAuth 2 详解(五):Device Authorization Flow OAuth 2 详解(三):Resource Owner Password Credentials Grant OAuth 2 详解(四):Client Credentials Flow OAuth 2 详解(二):Implict Grant Flow OAuth 2 详解(一):简介及 Authorization Code 模式 ElasticSearch 学习笔记 三种git流程以及发版模型 错误处理实践 权限模型(RBAC/ABAC) OIDC(OpenID Connect) 简介 任务队列简介 PostgreSQL 操作笔记 使用Drone CI构建CI/CD系统 Golang migrate 做数据库变更管理 使用PostgreSQL做搜索引擎 Nginx 源码阅读(三): 连接池、内存池 Nginx 源码阅读(二): 请求处理 Nginx 源码阅读(一): 启动流程 Go 泛型简明教程 KVM 显卡穿透给 Windows 使用 HTTP Router 处理 Telegram Bot 按钮回调 使用反射(reflect)对结构体赋值 GIN 是如何绑定参数的 你好 2022(2021 年终总结) 用Go导入大型CSV到PostgreSQL 使用 OpenWRT 搭建软路由 使用软KVM切换器 barrier 共享键鼠 SQL 防注入及原理 使用 gomock 测试 Go 代码 gevent不是黑魔法(二): gevent 实现 gevent不是黑魔法(一): greenlet 实现 用 entgo 替代 gorm 应用内使用crontab不是那么方便 单测时要不要 mock 数据库? Sentry 自建指南 用selenium完成自动化任务 用闲置的安卓手机做垃圾电话短信过滤 推荐三个时间管理工具 一次事故反思 当JS遇到uint64:JS整数溢出问题 SQLite3 存储以及ACID原理 Redis源码阅读:pub/sub实现 Redis源码阅读:zset实现 Redis源码阅读:bitmap 位图的运算 Redis源码阅读:set是怎么做交并集运算的?
Go使用gdb调试
Jiajun Huang · 2020-04-23 · via Jiajun的技术笔记

其实我一般调试都是直接打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行开始的,接下来就一路跟下去就可以了。


参考资料: