日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Go語言編譯原理之源碼調(diào)試_Golang

作者:書旅 ? 更新時(shí)間: 2022-09-30 編程語言

前言

在前邊幾篇文章中分享了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

欄目分類
最近更新