golang调试工具delve

栏目: Go · 发布时间: 7年前

内容简介:golang调试工具delve之前一直在烦心不知道怎么打印所有goroutine的stack,最近终于发现一个该工具。delve是golang推荐的专门go语言调试工具,用来替代gdb,因为:golang组织说delve能更好的理解go语言。

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. 等等等。。。,不列了,用到的时候再去查文档就行

以上所述就是小编给大家介绍的《golang调试工具delve》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

思考的技术

思考的技术

[日]大前研一 / 刘锦秀、谢育容 / 中信出版社 / 2010-11 / 32.00元

思路决定出路,没有了思路,也就没有了出路。 在充满危机与冒险的当下,我们缺乏的不是技巧而是揭发事务本质的动力和好奇心,缺少怀疑一切的心态和对固有模式的怠惰。 大前研一凭借他30多年的管理咨询经验,为我们提供了一种全新的可借鉴的思考方式。 企业和个人惟有改变既有的思考模式,放弃对过去成功经验的迷恋,学习有创意的思考方法,方能找到正确的经营思路。一起来看看 《思考的技术》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具