前言

在前边几篇文章中分享了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.goType 'help' for list of commands.(dlv) helpThe 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第一个元素的左节点