golang调试工具delve(dlv)

golang调试工具delve
之前一直在烦心不知道怎么打印所有goroutine的stack,最近终于发现一个该工具。

  1. 什么是delve

delve是golang推荐的专门go语言调试工具,用来替代gdb,因为:golang组织说delve能更好的理解go语言。

  • golang说delve更能理解go语言:https://golang.org/doc/gdb
    Note that Delve is a better alternative to GDB when debugging Go programs built with the standard toolchain. It understands the Go runtime, data structures, and expressions better than GDB. Delve currently supports Linux, OSX, and Windows on amd64. For the most up-to-date list of supported platforms, please see the Delve documentation.

    GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger and cause incorrect results even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations (e.g., debugging Cgo code, or debugging the runtime itself), it is not a reliable debugger for Go programs, particularly heavily concurrent ones. Moreover, it is not a priority for the Go project to address these issues, which are difficult.

  • delve的github地址:https://github.com/derekparker/delve

  1. delve的安装

delve的项目里面说的很详细,不列出了。
本人使用的是Linux,偷懒直接下载binary包就可以了。

$ go get -u github.com/derekparker/delve/cmd/dlv

在$GOPATH/bin目录下会包含dlv可执行程序。可以直接运行dlv启动delve。

$ dlv 
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`

Usage:
  dlv [command]
...

参数很多,很复杂,自己根据需要按需采用就行。

  1. delve常用命令行
  1. attach到进程PID
$ dlv attach <pid>
Type 'help' for list of commands.
(dlv) help
  1. 打印所有的goroutines列表
(dlv) goroutines
[10 goroutines]
  Goroutine 1 - User: /usr/local/go/src/syscall/asm_linux_amd64.s:27 syscall.Syscall (0x466e70) (thread 14023)
  Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
  Goroutine 3 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
  Goroutine 17 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
  Goroutine 18 - User: /usr/local/go/src/runtime/sigqueue.go:139 os/signal.signal_recv (0x43b5b6)
  Goroutine 19 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
  Goroutine 20 - User: ./main.go:43 main.setupSignalHandler.func1 (0x49318c)
  Goroutine 21 - User: /usr/local/go/src/runtime/time.go:102 time.Sleep (0x442ac6)
  Goroutine 22 - User: /usr/local/go/src/runtime/time.go:102 time.Sleep (0x442ac6)
  Goroutine 23 - User: /usr/local/go/src/runtime/lock_futex.go:227 runtime.notetsleepg (0x40c9a2)
  1. 打印单个goroutine的stack
(dlv) goroutine <goroutine_id> stack
 0  0x0000000000466e70 in syscall.Syscall
    at /usr/local/go/src/syscall/asm_linux_amd64.s:27
 1  0x000000000046692f in syscall.read
    at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749
 2  0x0000000000466449 in syscall.Read
    at /usr/local/go/src/syscall/syscall_unix.go:162
 3  0x0000000000468798 in internal/poll.(*FD).Read
    at /usr/local/go/src/internal/poll/fd_unix.go:153
 4  0x0000000000469b8e in os.(*File).read
    at /usr/local/go/src/os/file_unix.go:226
 5  0x0000000000468f7a in os.(*File).Read
    at /usr/local/go/src/os/file.go:107
 6  0x00000000004658a6 in io.ReadAtLeast
    at /usr/local/go/src/io/io.go:309
 7  0x0000000000465a18 in io.ReadFull
    at /usr/local/go/src/io/io.go:327
...
  1. 打印所有goroutine的stack
(dlv) goroutines -t
[10 goroutines]
  Goroutine 1 - User: /usr/local/go/src/syscall/asm_linux_amd64.s:27 syscall.Syscall (0x466e70) (thread 14023)
         0  0x0000000000466e70 in syscall.Syscall
             at /usr/local/go/src/syscall/asm_linux_amd64.s:27
         1  0x000000000046692f in syscall.read
             at /usr/local/go/src/syscall/zsyscall_linux_amd64.go:749
         2  0x0000000000466449 in syscall.Read
             at /usr/local/go/src/syscall/syscall_unix.go:162
          ...
        (truncated)
  Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:292 runtime.gopark (0x42800a)
        0  0x000000000042800a in runtime.gopark
            at /usr/local/go/src/runtime/proc.go:292
        1  0x00000000004280be in runtime.goparkunlock
            at /usr/local/go/src/runtime/proc.go:297
        2  0x0000000000427e4c in runtime.forcegchelper
            at /usr/local/go/src/runtime/proc.go:248
        3  0x0000000000451671 in runtime.goexit
            at /usr/local/go/src/runtime/asm_amd64.s:2361
...
  1. 等等等。。。,不列了,用到的时候再去查文档就行

最后,如果想让命令直接输出到文件,那么可以使用shell工具,例如:

$ cat mydlv.sh 
#!/bin/bash

dlv attach <PID> <<EOF
goroutines -t
quit
n
EOF
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,408评论 4 371
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,690评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,036评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,726评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,123评论 3 296
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,037评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,178评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,964评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,703评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,863评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,333评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,658评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,374评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,195评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,988评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,167评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,970评论 2 279

推荐阅读更多精彩内容