当前位置:网站首页> 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第一个元素的左节点 
边栏推荐
- 用两个栈模拟队列
- 设置工作模式与环境(下):探查和收集信息
- 跨域的学习
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- P1449 后缀表达式
- websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
- (PC+WAP)织梦模板不锈钢类网站
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- 创建函数报错,提示DECLARE定义语法问题
- 【并发编程】ReentrantLock的lockInterruptibly()方法源码分析
猜你喜欢

Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"

The salary of soft testers at each stage, come to Kangkang, how much can you get?

(PC+WAP)织梦模板不锈钢类网站

3D 语义分割——2DPASS

FinClip,助长智能电视更多想象空间

A simple understanding of TCP, learn how to shake hands, wave hands and various states

走迷宫 BFS

【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)

数据分析知识点搜集(纯粹的搜集)

3D Semantic Segmentation - 2DPASS
随机推荐
跨域的学习
HCIP BGP lab report
utils timer
完全二叉树问题
【OpenCV图像处理】 图像拼接技术
Zilliz 2023 Fall Campus Recruitment Officially Launched!
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time
Pytest学习-setup/teardown
详谈RDMA技术原理和三种实现方式
Take an example of a web worker
log4j-slf4j-impl cannot be present with log4j-to-slf4j
JS获得URL超链接的参数值
Shell编程之循环语句与函数
响应式织梦模板塑身瑜伽类网站
The principle and use of AOSP CameraLatencyHistogram
超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
The salary of soft testers at each stage, come to Kangkang, how much can you get?
AOSP CameraLatencyHistogram的原理与使用