golang调试工具delve

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

CSS实战手册(第2版)

CSS实战手册(第2版)

[美] David Sawyer McFarland / 俞黎敏 / 电子工业出版社 / 2010-6 / 69.80元

本书从介绍最基本的CSS知识开始,到建立用于打印网页的CSS和改进你的CSS习惯的最佳实践。将关于CSS的选择器、继承、层叠、格式化、边距、填充、边框、图片、网站导航、表格、表单、浮动布局、定位网页上的元素,以及用于打印网页的CSS等技术通过逐步地讲解与教程串联了起来。每章内容从简单到复杂,一步一步地建立起一个完整的教程示例,并在每章都会详细讨论一些技巧、最佳实践和各浏览器之间一致性的兼容问题及如......一起来看看 《CSS实战手册(第2版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具