首页 > 其他分享 >dlv调试实践

dlv调试实践

时间:2023-06-10 17:15:33浏览次数:41  
标签:http nums int 实践 dlv go main 调试

dlv调试实践

案例一:通过dlv debug进行源码调试
(1.)新建main.go,并执行go mod init

package main

import (
    "fmt"
)

func main() {
    nums := make([]int, 5)
    for i := 0; i <len(nums); i++ {
        nums[i] = i * i
    }
    fmt.Println(nums)
}

(2.)执行dlv debug进入调试模式

[root@localhost demo-1]# dlv debug
Type 'help' for list of commands.

# 设置断点:main.main
(dlv) b main.main
Breakpoint 1 set at 0x49aeea for main.main() ./main.go:7

# 查看已设置的所有断点,我们发现除了我们自己设置的 main.main 函数断点外,Delve 内部已经为 panic 异常函数设置了一个断点。
(dlv) bp
Breakpoint runtime-fatal-throw (enabled) at 0x437a40,0x437940 for (multiple functions)() /usr/local/go/src/runtime/panic.go:1059 (0)
Breakpoint unrecovered-panic (enabled) at 0x437e00 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1143 (0)
        print runtime.curg._panic.arg
Breakpoint 1 (enabled) at 0x49aeea for main.main() ./main.go:7 (0)

# 查看全局变量(可通过正则参数过滤)
(dlv) vars main
runtime.main_init_done = chan bool nil
runtime.mainStarted = false

# 运行到下个断点处
(dlv) c
> main.main() ./main.go:7 (hits goroutine(1):1 total:1) (PC: 0x49aeea)
     2:
     3: import (
     4:     "fmt"
     5: )
     6:
=>   7: func main() {
     8:     nums := make([]int, 5)
     9:     for i := 0; i <len(nums); i++ {
    10:         nums[i] = i * i
    11:     }
    12:     fmt.Println(nums)

# 单步执行
(dlv) n
> main.main() ./main.go:8 (PC: 0x49aef8)
     3: import (
     4:     "fmt"
     5: )
     6:
     7: func main() {
=>   8:     nums := make([]int, 5)
     9:     for i := 0; i <len(nums); i++ {
    10:         nums[i] = i * i
    11:     }
    12:     fmt.Println(nums)
    13: }

# 查看传入函数的参数
(dlv) args
(no args)

# 查看局部变量
(dlv) locals
(no locals)

# 再下一步,初始化nums了,可以看到有局部变量了
(dlv) n
> main.main() ./main.go:9 (PC: 0x49af23)
     4:     "fmt"
     5: )
     6:
     7: func main() {
     8:     nums := make([]int, 5)
=>   9:     for i := 0; i <len(nums); i++ {
    10:         nums[i] = i * i
    11:     }
    12:     fmt.Println(nums)
    13: }
(dlv) locals
nums = []int len: 5, cap: 5, [...]
(dlv) args
(no args)

# 组和使用 break 和 condition 命令设置条件断点
(dlv) b main.go:10
Breakpoint 2 set at 0x49af42 for main.main() ./main.go:10
(dlv) bp
Breakpoint runtime-fatal-throw (enabled) at 0x437940,0x437a40 for (multiple functions)() /usr/local/go/src/runtime/panic.go:1059 (0)
Breakpoint unrecovered-panic (enabled) at 0x437e00 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1143 (0)
        print runtime.curg._panic.arg
Breakpoint 1 (enabled) at 0x49aeea for main.main() ./main.go:7 (1)
Breakpoint 2 (enabled) at 0x49af42 for main.main() ./main.go:10 (0)

# 这里设置条件断点 i 等于 3
(dlv) cond 2 i==3

# 继续 continue 执行到刚设置的条件断点,输出局部变量
(dlv) c
> main.main() ./main.go:10 (hits goroutine(1):1 total:1) (PC: 0x49af42)
     5: )
     6:
     7: func main() {
     8:     nums := make([]int, 5)
     9:     for i := 0; i <len(nums); i++ {
=>  10:         nums[i] = i * i
    11:     }
    12:     fmt.Println(nums)
    13: }
(dlv) locals
nums = []int len: 5, cap: 5, [...]
i = 3
(dlv) print nums
[]int len: 5, cap: 5, [0,1,4,0,0]

# 查看栈帧信息
(dlv) stack
0  0x000000000049af42 in main.main
   at ./main.go:10
1  0x000000000043a138 in runtime.main
   at /usr/local/go/src/runtime/proc.go:250
2  0x0000000000464cc1 in runtime.goexit
   at /usr/local/go/src/runtime/asm_amd64.s:1594
(dlv) n
> main.main() ./main.go:9 (PC: 0x49af73)
     4:     "fmt"
     5: )
     6:
     7: func main() {
     8:     nums := make([]int, 5)
=>   9:     for i := 0; i <len(nums); i++ {
    10:         nums[i] = i * i
    11:     }
    12:     fmt.Println(nums)
    13: }
(dlv) b main.go:12
Breakpoint 3 set at 0x49af82 for main.main() ./main.go:12
(dlv) c
> main.main() ./main.go:12 (hits goroutine(1):1 total:1) (PC: 0x49af82)
     7: func main() {
     8:     nums := make([]int, 5)
     9:     for i := 0; i <len(nums); i++ {
    10:         nums[i] = i * i
    11:     }
=>  12:     fmt.Println(nums)
    13: }
(dlv) print nums
[]int len: 5, cap: 5, [0,1,4,9,16]
(dlv) q

案例二:使用dlv attach 进行调试
(1.)新建main.go,并进行go mod init

package main

import (
        "log"
        "net/http"
)

func main() {
        http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
                nums := make([]int, 5)
                for i := 0; i < len(nums); i++ {
                        nums[i] = i * i
                }
                writer.Write([]byte("OK"))
        })

        err := http.ListenAndServe(":4781", nil)
        if err != nil {
                log.Fatalln(err)
        }
}

(2.) 编译,禁用编译器优化

go build -gcflags="all=-N -l" main.go
// 运行
./main

(3.)获取进程ID

ps -ef |grep main

(4.)进入调试

dlv attach $pid // pid为当前进程的pid

(5.)示例如下:

[root@localhost ~]# dlv attach 18175
Type 'help' for list of commands.

# 在main函数12行进行断点
(dlv) b main.go:12
Breakpoint 1 set at 0x74078e for main.main.func1() /home/nova000918/workspace/gocode/src/demo-1/main.go:12

# 查看断点
(dlv) bp
Breakpoint runtime-fatal-throw (enabled) at 0x43d380,0x43d480 for (multiple functions)() /usr/local/go/src/runtime/panic.go:1059 (0)
Breakpoint unrecovered-panic (enabled) at 0x43d840 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1143 (0)
        print runtime.curg._panic.arg
Breakpoint 1 (enabled) at 0x74078e for main.main.func1() /home/nova000918/workspace/gocode/src/demo-1/main.go:12 (0)

# 继续直到断点处卡住,这里发送http请求,curl http://127.0.0.1:4781/hello 会进入断点
(dlv) c
> main.main.func1() /home/nova000918/workspace/gocode/src/demo-1/main.go:12 (hits goroutine(6):1 total:1) (PC: 0x74078e)
     7:
     8: func main() {
     9:         http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
    10:                 nums := make([]int, 5)
    11:                 for i := 0; i < len(nums); i++ {
=>  12:                         nums[i] = i * i
    13:                 }
    14:                 writer.Write([]byte("OK"))
    15:         })
    16:
    17:         err := http.ListenAndServe(":4781", nil)

# 打印变量
(dlv) print nums
[]int len: 5, cap: 5, [0,0,0,0,0]

# 查看本地变量
(dlv) locals
nums = []int len: 5, cap: 5, [...]
i = 0

# 下一步
(dlv) n
> main.main.func1() /home/nova000918/workspace/gocode/src/demo-1/main.go:11 (PC: 0x7407be)
     6: )
     7:
     8: func main() {
     9:         http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
    10:                 nums := make([]int, 5)
=>  11:                 for i := 0; i < len(nums); i++ {
    12:                         nums[i] = i * i
    13:                 }
    14:                 writer.Write([]byte("OK"))
    15:         })
    16:
(dlv) n
> main.main.func1() /home/nova000918/workspace/gocode/src/demo-1/main.go:12 (hits goroutine(6):2 total:2) (PC: 0x74078e)
     7:
     8: func main() {
     9:         http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
    10:                 nums := make([]int, 5)
    11:                 for i := 0; i < len(nums); i++ {
=>  12:                         nums[i] = i * i
    13:                 }
    14:                 writer.Write([]byte("OK"))
    15:         })
    16:
    17:         err := http.ListenAndServe(":4781", nil)
(dlv) print nums
[]int len: 5, cap: 5, [0,0,0,0,0]
(dlv) locals
nums = []int len: 5, cap: 5, [...]
(dlv) print nums
[]int len: 5, cap: 5, [0,1,4,9,16]
(dlv) q

标签:http,nums,int,实践,dlv,go,main,调试
From: https://www.cnblogs.com/tomtellyou/p/17439821.html

相关文章

  • GDB调试
    在编译的时候要加上“-g”选项才可以调试,例如:gcc-g-oprogmain.cmstore.c 参考链接:GDB官方手册:Top(DebuggingwithGDB)(sourceware.org)......
  • 你以为搞个流水线每天跑,团队就在使用CI/CD实践了?
    在实践中,很多团队对于DevOps流水线没有很透彻的理解,要不就创建一大堆流水线,要不就一个流水线通吃。实际上,流水线的设计和写代码一样,需要基于“业务场景”进行一定的设计编排,特别是很多通过“开源工具”搭建的流水线,更需要如此(商业的一体化平台大部分已经把设计思想融入自己产品里......
  • 带你体验AI系列之云原生最佳实践--免费体验GPT-4教程
    前言​【GPT-4】是OpenAI最新推出的大型语言模型,它支持图像和文本输入,以文本形式输出。它比GPT-3.5更大、更强、更猛。最重要的是据与研究表明,他在某些场景下,可以通过图灵测试。但是,却缺点是收费,不像GPT-3.5那样容易白嫖。不过今天我就带你嫖一手,真香警告!本教程可称为云原生......
  • Python代码调试之异常回溯
    当发生异常时,Python会回溯异常,给出大量的提示,可能会给程序员的定位和纠错带来一定的困难,这时可以使用sys模块的exc_info()函数来回溯最近一次异常。sys.exc_info()的返回值tuple是一个三元组(type, value, traceback),其中:type——异常的类型value——异常的信息或者参数tr......
  • 虚拟ECU实践:汽车发动机控制器仿真
    ​虚拟化技术使得在WindowsPC上对汽车ECU(ElectronicControlUnit,电子控制器单元)进行闭环仿真成为可能,能有效改善ECU开发过程。一些开发任务得以从道路、测试平台和HIL(HardwareintheLoop,硬件在环)转移到PC上,缩短开发时间和成本。▲汽车系统模型测试现状由在本文中,我们将主要......
  • vc6,windows 7 x64 调试 (IS2120@BG57IV3)
    //z2012-12-2416:31:[email protected]在windows7x64上调试vc6console程序时,按了shift+f5,程序并不结束。解决方法:替换这个文件TLLOC.dllwww.dr-hoiby.com/TLLOC.dll//z2012-12-2416:31:[email protected]......
  • 调试小技巧
    “Debuggingistwiceashardaswritingthecodeinthefirstplace.Therefore,ifyouwritethecodeascleverlyaspossible,youare,bydefinition,notsmartenoughtodebugit.”—BrianW.Kernighan1.中断进入调试器1.1c++_asm{int3}一个宏(ma......
  • EC20调试过程备份
    1、模块通电 如图,将单片机引脚(VBAT_EN)拉低,即可给EC20供3.8V电源。2、模块开机如图,单片机引脚拉高500ms,即可让模块开机。TCP步骤:1、给模块上电并开机2、AT+CPIN?查看SIM卡的状态3、AT+CREG?检查是否注册到网络4、AT+CGREG?/AT+CEREG?查询GPRS网络注册状态(2G网络)/......
  • 在使用vs2010调试时大量出现iso_whid,HR PROPAGATED 信息
    //z9/5/20112:[email protected]_whid错误在用visualstudio2010调试时会向dbgview输出大量的如下信息:***HRpropagated:-2147024774[8092]*** SourceFile:d:\iso_whid\x86fre\base\isolation\com找了下有以下这些解决方案1.在dbgview中设置过滤器在d......
  • Vue 调试访问本地后端接口配置
    记录一下本地测试前端的时候怎么访问本地后端接口。时间太长容易忘。。。。1、首先要知道前端在访问后端的时候是怎么加上后端接口地址的。如下图,找到封装http请求的文件,src/utils/rerquest.js该图中的baseURL就是访问时我们加上的后端请求地址。baseURL:process.env.VUE_APP......