• Go编译原理系列7(Go源码调试)


    前言

    在前边几篇文章中分享了Go编译过程中的源码实现,本文主要是想分享一下我是怎么调试Go的源代码的(如果你很熟悉的话,可以跳过本文)。本文主要是分享两种Go源码的调试方法

    • Goland的debug
    • dlv工具

    本文我还会以抽象语法树为例,来通过dlv对它的构建过程进行调试

    Goland的debug调试Go源码

    下边以调试Go编译的入口文件为例

    • 编辑debug配置

    • 填写配置信息

    • 打断点,并开始执行

    • 调试

      这些调试按钮的功能其实跟其他的IDEA是一样的,之前整理过,这里不重复整理了,不清楚的小伙伴可以看这里

    dlv工具调试Go源码

    安装

    这里以mac为例

    brew install dlv
    
    • 1

    启动

    $ dlv debug 待调试文件
    
    • 1

    常用命令

    可以通过下边的方式查看一些常用的命令

    $ 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    dlv调试抽象语法树构建

    下边利用dlv来调试Go编译过程中的抽象语法树构建。我这里没有粘代码,你可以打开源代码对着下边看

    • 启动dlv,并调试Go编译的入口文件

    • 设置断点、continue的使用、n的使用(r 设置编译器编译目标文件)

    • 在指定文件的指定位置设置断点

    • 打印抽象语法树构建出来的结果(xtop)


    你也可以打印xtop下边元素的值,比如查看xtop第一个元素的左节点

  • 相关阅读:
    阿里云云平台的物理安全防御措施
    <二>派生类的构造过程
    小程序源码:网课查题微信小程序源码下载,题库资源丰富自动采集,支持语音拍照识别-多玩法安装简单
    如何在mac下使用homebrew安装 mysql?
    jq实现多页展示并且进度条轮播
    Swin-Transformer详解
    NET工控,上位机,Modbus485网口/串口通讯(鸣志步进电机,鸣志伺服电机,松下伺服电机,华庆军继电器模块)
    Java学习笔记 --- Java绘图技术
    Coordinatorlayout:协调员布局
    Couchdb-权限绕过--命令执行--(CVE-2017-12635)&&(CVE-2017-12636)--H2database命令执行--(CVE-2022-23221)
  • 原文地址:https://blog.csdn.net/self_realian/article/details/126149154