使用delve调试golang

简介: 使用delve调试golang

目录

前置要求

dlv调试要求可执行文件不能删掉调试信息,即-ldflags参数中不能包含 -w -s标志。可以使用如下方式查看可执行文件是否有删除调试信息,"not stripped"表示没有删除调试信息

# file alert-sd-engine
alert-sd-engine: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

这样编译即可:go build -gcflags "all=-N -l" -o ./app

使用方式

使用funcs查找支持的函数

使用funcs可以打印可以查看调试的函数。可以在后面加上函数的名字或部分名字可以检索出支持的函数,如:

(dlv) funcs VmSvc
devops/alert-sd-engine/pkg.(*Monitor).createVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).deleteVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape
devops/alert-sd-engine/pkg.(*Monitor).isVmSvcScrapeExist
devops/alert-sd-engine/pkg.(*Monitor).updateVmSvcScrape
devops/alert-sd-engine/pkg.createVmSvcScrape
devops/alert-sd-engine/pkg.deleteVmSvcScrape
devops/alert-sd-engine/pkg.getVmSvcScrape
devops/alert-sd-engine/pkg.updateVmSvcScrape

使用break(b)打断点

根据funcs找到的函数,使用break在需要的函数上打断点

(dlv) break devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape

当然也可以使用如下方式将断点打到文件的某一行

(dlv) b engine.go:196

使用breakpoints查看当前活动的断点。

(dlv) breakpoints
Breakpoint runtime-fatal-throw (enabled) at 0x4345e0 for runtime.throw() /usr/local/go/src/runtime/panic.go:1188 (0)
Breakpoint unrecovered-panic (enabled) at 0x434940 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:1271 (0)
print runtime.curg._panic.arg
Breakpoint 2 (enabled) at 0x1399452 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() .alert-sd-engine/pkg/engine.go:195 (0)
Breakpoint 4 (enabled) at 0x1399479 for devops/alert-sd-engine/pkg.(*Monitor).getVmSvcScrape() ./alert-sd-engine/pkg/engine.go:196 (0)

使用clear清除断点

使用clear 清除某个断电

使用clearall可以清除所有断点

使用goroutines查看所有协程

(dlv) goroutines
Goroutine 1 - User: /usr/local/go/src/net/fd_unix.go:173 net.(*netFD).accept (0x5f4f55) [IO wait]
Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [force gc (idle) 455958h37m56.413188346s]
Goroutine 3 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [GC sweep wait]
Goroutine 4 - User: /usr/local/go/src/runtime/proc.go:367 runtime.gopark (0x4370f6) [GC scavenge wait]

使用goroutine 可以切换goroutine

使用stack(bt)查看goroutine的栈信息

(dlv) goroutine 1 stack
0  0x00000000004370f6 in runtime.gopark
    at /usr/local/go/src/runtime/proc.go:367
1  0x000000000042f7fe in runtime.netpollblock
    at /usr/local/go/src/runtime/netpoll.go:445
2  0x000000000045efa9 in internal/poll.runtime_pollWait
    at /usr/local/go/src/runtime/netpoll.go:229

使用frame可以设置当前栈位置,使用up可以向上移动栈,使用down可以向下移动栈

使用attach连接到正在运行的进程

使用attach 可以连接到正在运行的进程

使用locals打印当前的局部遍历,使用-v可以打印更详细的信息

(dlv) locals -v req
req = devops/alert-sd-engine/pkg.Req {
Base: devops/alert-sd-engine/pkg.commData {
Env: 0,
ClusterName: "",
DualActive: false,
Namespace: "",
Name: "",
Endpoints: []devops/alert-sd-engine/pkg.Endpoint len: 0, cap: 0, nil,},
Selector: struct { Appid string "json:\"appId,omitempty\"" } {Appid: ""},}

更多参见官方文档

Tips

设置打印的字符串长度:

(dlv) config -list
aliases            map[]
substitute-path    []
max-string-len     <not defined>
max-array-values   <not defined>
show-location-expr false
(dlv) config max-string-len 1000

goland远程调试

点击 Run — Edit configurations,点击+,添加Go Remote

远端执行:dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./demo.exe

在golang的代码中打上断点,并启动debug该程序即可,需要确保两端的代码是一致的。

参考

目录
相关文章
|
SQL Oracle 关系型数据库
Navicat Premium 15激活方式
介绍如何安装使用Navicat Premium 15
|
缓存 安全 网络协议
socket是并发安全的吗 2
socket是并发安全的吗
1260 0
|
存储 算法 NoSQL
[Eigen中文文档] 存储顺序
矩阵和二维数组有两种不同的存储顺序:列优先和行优先。本节解释了这些存储顺序以及如何指定应该使用哪一种。
964 0
|
4月前
|
人工智能 Rust JavaScript
让 AI Agent 少读 89% 的文件——我试了 CodeGraph 这个代码知识图谱
CodeGraph是开源代码知识图谱工具,为AI编程Agent预建项目结构地图,避免反复grep/读文件。支持20+语言,Rust内核,本地化存储,显著降低token消耗(-69%)与工具调用(-89%),大幅提升大项目理解效率。
714 0
|
6月前
|
自然语言处理 Java API
【AgentScope Java新手村系列】(7)子Agent编排
子Agent编排 — SubagentDeclaration 描述子 agent,主 agent 通过 agent_spawn 工具同步/异步委派子任务。
1281 0
|
12月前
|
Java Windows
IDEA 插件 SpotBugs Idea 1.2.7.zip 使用详解(一步步教你排查Bug)
SpotBugs Idea 1.2.7.zip 是IntelliJ IDEA的Java静态分析插件,可检测空指针、资源泄漏等潜在问题。下载后通过Settings→Plugins→Install from Disk安装,重启IDEA即可使用。分析项目时右键选择“Analyze with SpotBugs”,结果按严重程度分级显示,支持跳转定位代码。兼容性佳,误报可忽略或注解屏蔽。
1262 144
|
Linux Go 网络安全
goland 远程调试 remote debug
goland 远程调试 remote debug
1104 0
|
测试技术 Docker 容器
使用Docker构建多环境应用:开发、测试、生产环境
Docker已经成为了现代应用程序开发和部署的核心工具之一。通过使用Docker,开发团队可以轻松地在不同的环境中构建、测试和部署应用程序,从而提高开发速度和应用程序的可移植性。本文将介绍如何使用Docker构建多环境应用,包括开发、测试和生产环境,并提供丰富的示例代码,以帮助大家轻松应对不同环境的挑战。
|
网络协议 Go C语言
在golang中调试时的指令和使用技巧
【7月更文挑战第4天】 本文介绍 Go调试工具`dlv`常用命令概览及其使用技巧。
1093 2
在golang中调试时的指令和使用技巧