網(wǎng)站首頁 編程語言 正文
前言
在前邊幾篇文章中分享了Go編譯過程中的源碼實(shí)現(xiàn),本文主要是想分享一下我是怎么調(diào)試Go的源代碼的(如果你很熟悉的話,可以跳過本文)。本文主要是分享兩種Go源碼的調(diào)試方法
- Goland的debug
- dlv工具
本文我還會(huì)以抽象語法樹為例,來通過dlv對(duì)它的構(gòu)建過程進(jìn)行調(diào)試
Goland的debug調(diào)試Go源碼
下邊以調(diào)試Go編譯的入口文件為例
編輯debug配置
填寫配置信息
打斷點(diǎn),并開始執(zhí)行
調(diào)試
這些調(diào)試按鈕的功能其實(shí)跟其他的IDEA是一樣的,之前整理過,這里不重復(fù)整理了,不清楚的小伙伴可以看這里
dlv工具調(diào)試Go源碼
安裝
這里以mac為例
brew install dlv
啟動(dòng)
$ dlv debug 待調(diào)試文件
常用命令
可以通過下邊的方式查看一些常用的命令
$ 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!!!)恢復(fù)進(jìn)程,注入函數(shù)調(diào)用(實(shí)驗(yàn)的)
continue (alias: c) --------- 運(yùn)行到斷點(diǎn)或程序終止
next (alias: n) ------------- 執(zhí)行下一行.
rebuild --------------------- 重新生成目標(biāo)可執(zhí)行文件并重新啟動(dòng)它. 如果可執(zhí)行文件不是由dlv構(gòu)建,它就不能工作.
restart (alias: r) ---------- 重新啟動(dòng)一個(gè)進(jìn)程.
step (alias: s) ------------- 單步調(diào)試.
step-instruction (alias: si) Single step a single cpu instruction.
stepout (alias: so) --------- Step out of the current function.
Manipulating breakpoints:
break (alias: b) ------- 設(shè)置一個(gè)端點(diǎn).
breakpoints (alias: bp) 打印所有的端點(diǎn)信息.
clear ------------------ 清除端點(diǎn).
clearall --------------- 刪除多個(gè)端點(diǎn).
condition (alias: cond) 設(shè)置斷點(diǎn)條件.
on --------------------- 在命中斷點(diǎn)時(shí)執(zhí)行命令.
toggle ----------------- 打開或關(guān)閉斷點(diǎn).
trace (alias: t) ------- Set tracepoint.
watch ------------------ Set watchpoint.
Viewing program variables and memory:
args ----------------- 打印函數(shù)參數(shù).
display -------------- 每次程序停止時(shí)打印表達(dá)式的值.
examinemem (alias: x) 檢查給定地址的原始內(nèi)存.
locals --------------- 打印局部變量.
print (alias: p) ----- 打印變量值.
regs ----------------- 打印CPU寄存器的內(nèi)容.
set ------------------ 更改變量的值.
vars ----------------- 打印包變量.
whatis --------------- 打印表達(dá)式的類型.
Listing and switching between threads and goroutines:
goroutine (alias: gr) -- 顯示或更改當(dāng)前goroutine
goroutines (alias: grs) 列出程序goroutines.
thread (alias: tr) ----- 切換到指定的線程.
threads ---------------- 打印每個(gè)跟蹤線程的信息.
Viewing the call stack and selecting frames:
deferred --------- 在延遲調(diào)用的上下文中執(zhí)行命令.
down ------------- 向下移動(dòng)當(dāng)前幀.
frame ------------ 設(shè)置當(dāng)前幀,或在其他幀上執(zhí)行命令.
stack (alias: bt) 打印堆棧信息.
up --------------- 向上移動(dòng)當(dāng)前幀
Other commands:
config --------------------- 更改配置參數(shù).
disassemble (alias: disass) Disassembler.
dump ----------------------- 從當(dāng)前進(jìn)程狀態(tài)創(chuàng)建核心轉(zhuǎn)儲(chǔ)
edit (alias: ed) ----------- Open where you are in $DELVE_EDITOR or $EDITOR
exit (alias: quit | q) ----- 退出調(diào)試.
funcs ---------------------- 打印函數(shù)列表.
help (alias: h) ------------ 打印幫助信息.
libraries ------------------ 列出加載的動(dòng)態(tài)庫
list (alias: ls | l) ------- 展示源代碼.
source --------------------- 執(zhí)行包含delve命令列表的文件
sources -------------------- 打印源文件列表
types ---------------------- 打印類型列表
Type help followed by a command for full documentation.
(dlv)
dlv調(diào)試抽象語法樹構(gòu)建
下邊利用dlv來調(diào)試Go編譯過程中的抽象語法樹構(gòu)建。我這里沒有粘代碼,你可以打開源代碼對(duì)著下邊看
- 啟動(dòng)dlv,并調(diào)試Go編譯的入口文件
- 設(shè)置斷點(diǎn)、continue的使用、n的使用(r 設(shè)置編譯器編譯目標(biāo)文件)
- 在指定文件的指定位置設(shè)置斷點(diǎn)
- 打印抽象語法樹構(gòu)建出來的結(jié)果(xtop)
你也可以打印xtop下邊元素的值,比如查看xtop第一個(gè)元素的左節(jié)點(diǎn)
原文鏈接:https://juejin.cn/post/7127638222330396686
相關(guān)推薦
- 2023-04-21 C語言哈希表概念超詳細(xì)講解_C 語言
- 2022-04-02 IDEA集成Docker實(shí)現(xiàn)打包的方法_docker
- 2022-01-27 laravel JWTAuth對(duì)api接口權(quán)限進(jìn)行鑒權(quán)
- 2022-09-20 C#單線程和多線程端口掃描器詳解_C#教程
- 2022-11-27 Python線性網(wǎng)絡(luò)實(shí)現(xiàn)分類糖尿病病例_python
- 2022-08-05 python內(nèi)置模塊之上下文管理contextlib_python
- 2022-03-19 基于React?Hooks的小型狀態(tài)管理詳解_React
- 2022-05-23 .NET中堆棧和堆的特點(diǎn)與差異介紹_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支