当前位置:网站首页> Go编译原理系列7(Go源码调试)
Go编译原理系列7(Go源码调试)
2022-08-03 23:31:00 【书旅】
前言
在前边几篇文章中分享了Go编译过程中的源码实现,本文主要是想分享一下我是怎么调试Go的源代码的(如果你很熟悉的话,可以跳过本文)。本文主要是分享两种Go源码的调试方法
Goland的debug dlv工具
本文我还会以抽象语法树为例,来通过dlv对它的构建过程进行调试
Goland的debug调试Go源码
下边以调试Go编译的入口文件为例
编辑debug配置
填写配置信息
打断点,并开始执行
调试
这些调试按钮的功能其实跟其他的IDEA是一样的,之前整理过,这里不重复整理了,不清楚的小伙伴可以看这里
dlv工具调试Go源码
安装
这里以mac为例
brew install dlv
启动
$ dlv debug 待调试文件
常用命令
可以通过下边的方式查看一些常用的命令
$ 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调试抽象语法树构建
下边利用dlv来调试Go编译过程中的抽象语法树构建。我这里没有粘代码,你可以打开源代码对着下边看
启动dlv,并调试Go编译的入口文件

设置断点、continue的使用、n的使用(r 设置编译器编译目标文件)

在指定文件的指定位置设置断点

打印抽象语法树构建出来的结果(xtop)
你也可以打印xtop下边元素的值,比如查看xtop第一个元素的左节点
边栏推荐
- Redis persistence method
- Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- 689. 三个无重叠子数组的最大和
- 跨域的学习
- First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
- 响应式织梦模板餐饮酒店类网站
- What is memoization and what is it good for?
- CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
- Storage engine written by golang, based on b+ tree, mmap
猜你喜欢
随机推荐
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
HNUCM 您好中国
软测人每个阶段的薪资待遇,快来康康你能拿多少?
Zilliz 2023 Fall Campus Recruitment Officially Launched!
简单了解下 TCP,学习握手和挥手以及各种状态到底是怎么样的
P1996 约瑟夫问题
leetcode/子串中不能有重复字符的最长子串
FinClip, help smart TV more imagination
The principle and use of AOSP CameraLatencyHistogram
ros mavros stereo读取rosbag并记录IMU和图片到文件夹
Three.js入门详解
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
智能座舱的「交互设计」大战
Testng listener
V8中的快慢数组(附源码、图文更易理解)
End-to-End Lane Marker Detection via Row-wise Classification
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
Internship: Upload method for writing excel sheet (import)
禾匠编译错误记录
【并发编程】ReentrantLock的lockInterruptibly()方法源码分析