foreword
In the previous articles, I shared the source code implementation in the Go compilation process. This article mainly wants to share how I debug the Go source code (if you are familiar with it, you can skip this article). This article mainly shares two debugging methods of Go source code
- Goland's debug
- dlv tool
In this article, I will also use the abstract syntax tree as an example to debug its construction process through dlv
Goland's debug debugging Go source code
The following is an example of debugging the entry file compiled by Go
- Edit debug configuration
- Fill in the configuration information
- breakpoint and start execution
- debugging
The functions of these debug buttons are actually the same as other IDEAs. They have been sorted out before and are not repeated here. For those who are not clear, you can read them here.
dlv tool to debug Go source code
Install
Here is an example of mac
brew install dlv
start up
$ dlv debug 待调试文件
Common commands
You can view some commonly used commands in the following ways
$ gc dlv debug /usr/local/go/src/cmd/compile/main.go
Type 'help' for list of commands.
(dlv) help
The following commands are available:
Running the program:
call ------------------------ (EXPERIMENTAL!!!)恢复进程,注入函数调用(实验的)
continue (alias: c) --------- 运行到断点或程序终止
next (alias: n) ------------- 执行下一行.
rebuild --------------------- 重新生成目标可执行文件并重新启动它. 如果可执行文件不是由dlv构建,它就不能工作.
restart (alias: r) ---------- 重新启动一个进程.
step (alias: s) ------------- 单步调试.
step-instruction (alias: si) Single step a single cpu instruction.
stepout (alias: so) --------- Step out of the current function.
Manipulating breakpoints:
break (alias: b) ------- 设置一个端点.
breakpoints (alias: bp) 打印所有的端点信息.
clear ------------------ 清除端点.
clearall --------------- 删除多个端点.
condition (alias: cond) 设置断点条件.
on --------------------- 在命中断点时执行命令.
toggle ----------------- 打开或关闭断点.
trace (alias: t) ------- Set tracepoint.
watch ------------------ Set watchpoint.
Viewing program variables and memory:
args ----------------- 打印函数参数.
display -------------- 每次程序停止时打印表达式的值.
examinemem (alias: x) 检查给定地址的原始内存.
locals --------------- 打印局部变量.
print (alias: p) ----- 打印变量值.
regs ----------------- 打印CPU寄存器的内容.
set ------------------ 更改变量的值.
vars ----------------- 打印包变量.
whatis --------------- 打印表达式的类型.
Listing and switching between threads and goroutines:
goroutine (alias: gr) -- 显示或更改当前goroutine
goroutines (alias: grs) 列出程序goroutines.
thread (alias: tr) ----- 切换到指定的线程.
threads ---------------- 打印每个跟踪线程的信息.
Viewing the call stack and selecting frames:
deferred --------- 在延迟调用的上下文中执行命令.
down ------------- 向下移动当前帧.
frame ------------ 设置当前帧,或在其他帧上执行命令.
stack (alias: bt) 打印堆栈信息.
up --------------- 向上移动当前帧
Other commands:
config --------------------- 更改配置参数.
disassemble (alias: disass) Disassembler.
dump ----------------------- 从当前进程状态创建核心转储
edit (alias: ed) ----------- Open where you are in $DELVE_EDITOR or $EDITOR
exit (alias: quit | q) ----- 退出调试.
funcs ---------------------- 打印函数列表.
help (alias: h) ------------ 打印帮助信息.
libraries ------------------ 列出加载的动态库
list (alias: ls | l) ------- 展示源代码.
source --------------------- 执行包含delve命令列表的文件
sources -------------------- 打印源文件列表
types ---------------------- 打印类型列表
Type help followed by a command for full documentation.
(dlv)
dlv debug abstract syntax tree build
The following uses dlv to debug the abstract syntax tree construction during Go compilation. I didn't paste the code here, you can open the source code and look below
- Start dlv and debug the entry file compiled by Go
- Set breakpoints, use of continue, use of n (r sets the compiler to compile object files)
- Set a breakpoint at the specified location in the specified file
- Print the result of the abstract syntax tree construction (xtop)
You can also print the value of the element below xtop, for example to view the left node of the first element of xtop
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。