关于后端:Go编译原理系列7Go源码调试

2次阅读

共计 2485 个字符,预计需要花费 7 分钟才能阅读完成。

前言

在前边几篇文章中分享了 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 第一个元素的左节点

正文完
 0