【Go调试】Delve调试学习

参考:

1.Linux下安装

注意:go需要更新到1.10以上了,否则报错。

方法1,采用go安装:

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

方法2,git普通安装,需设置好$GOPATH环境变量:

    $ git clone https://github.com/go-delve/delve.git $GOPATH/src/github.com/go-delve/delve
    $ cd $GOPATH/src/github.com/go-delve/delve
    $ make install

2.简单用法

(1)调试main packages

只要和main packages在同一目录,不需要参数就能运行。

例如以下目录结构:

    .
    ├── github.com/me/foo
    ├── cmd
    │   └── foo
    │       └── main.go
    ├── pkg
    │   └── baz
    │       ├── bar.go
    │       └── bar_test.go

若就在github.com/me/foo/cmd/foo目录,则直接运行dlv debug;否则运行dlv debug github.com/me/foo/cmd/foo;若提供其他参数,则加"—"隔开,dlv debug github.com/me/foo/cmd/foo – -arg1 value。

    $ dlv debug github.com/me/foo/cmd/foo
    Type 'help' for list of commands.
    (dlv) break main.main
    Breakpoint 1 set at 0x49ecf3 for main.main() ./test.go:5
    (dlv) continue
    > main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ecf3)
         1:	package main
         2:	
         3:	import "fmt"
         4:	
    =>   5:	func main() {
         6:		fmt.Println("delve test")
         7:	}
    (dlv) 
(2)调试tests

也可以测试自己的test suite,命令是dlv test。若没有给定参数则构建当前package。

    $ dlv test github.com/me/foo/pkg/baz
    Type 'help' for list of commands.
    (dlv) funcs test.Test*
    /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi
    (dlv) break TestHi
    Breakpoint 1 set at 0x536513 for /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./test_test.go:5
    (dlv) continue
    > /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./bar_test.go:5 (hits goroutine(5):1 total:1) (PC: 0x536513)
         1:	package baz
         2:	
         3:	import "testing"
         4:	
    =>   5:	func TestHi(t *testing.T) {
         6:		t.Fatal("implement me!")
         7:	}
    (dlv) 

通过funcs命令加正则参数来筛查函数,调试test二进制文件。其他命令见delve help。

3.详细用法

(1)调试选项和模式

调试选项,例如dlv exec ./hello – server --config conf/config.toml。

          --accept-multiclient   Allows a headless server to accept multiple client connections. Note that the server API is not reentrant and clients will have to coordinate.
          --api-version int      Selects API version when headless. (default 1)
          --backend string       Backend selection:
    	default		Uses lldb on macOS, native everywhere else.
    	native		Native backend.
    	lldb		Uses lldb-server or debugserver.
    	rr		Uses mozilla rr (https://github.com/mozilla/rr).
     (default "default")
          --build-flags string   Build flags, to be passed to the compiler.
          --check-go-version     Checks that the version of Go in use is compatible with Delve. (default true)
          --headless             Run debug server only, in headless mode.
          --init string          Init file, executed by the terminal client.
      -l, --listen string        Debugging server listen address. (default "localhost:0")
          --log                  Enable debugging server logging.
          --log-dest string      Writes logs to the specified file or file descriptor. If the argument is a number it will be interpreted as a file descriptor, otherwise as a file path. This option will also redirect the "API listening" message in headless mode.
          --log-output string    Comma separated list of components that should produce debug output, possible values:
    	debugger	Log debugger commands
    	gdbwire		Log connection to gdbserial backend
    	lldbout		Copy output from debugserver/lldb to standard output
    	debuglineerr	Log recoverable errors reading .debug_line
    	rpc		Log all RPC messages
    	fncall		Log function call protocol
    	minidump	Log minidump loading
    Defaults to "debugger" when logging is enabled with --log.
          --wd string            Working directory for running the program. (default ".")

调试模式:

  • dlv attach - 附加到运行的进程进行调试— dlv attach pid [executable]
  • dlv connect - 连接到调试服务器— dlv connect 127.0.0.1:8181
  • dlv core - 检查一个core dump— dlv core
  • dlv debug - 编译并开始调试当前目录下的main package或指定的package—dlv debug [package]
  • dlv exec - 执行并调试一个已编译的binary—dlv exec <path/to/binary>
  • dlv replay - 重放mozilla rr记录的trace—dlv replay [trace directory]
  • dlv run - 弃用的命令,用’debug’代替
  • dlv test - 编译test并调试—dlv test [package]
  • dlv version - 打印版本
  • dlv trace - 编译并开始追踪程序—dlv trace [package] regexp。当执行到匹配正则表达式的函数时输出提示信息,如果不想调试整个程序,只想知道你的进程执行了哪些函数,就很有用。选项如下
      -e, --exec string     需执行和追踪的二进制文件
          --output string   二进制文件的输出路径(default "debug")
      -p, --pid int         待附加的进程号
      -s, --stack int       显示给定深度的stack trace
      -t, --test            追踪一个test binary.
(2)详细命令
  • Command Description
  • args 打印函数参数
  • break—b 设置一个断点
  • breakpoints—bp 打印活动断点信息
  • call Resumes process, injecting a function call (EXPERIMENTAL!!!)
  • check Creates a checkpoint at the current position.
  • checkpoints Print out info for existing checkpoints.
  • clear 删除断点
  • clear-checkpoint Deletes checkpoint.
  • clearall 删除所有的断点
  • condition—cond 设置断点条件
  • config 改变配置参数
  • continue—c 运行到断点或程序终止
  • deferred Executes command in the context of a deferred call.
  • disassemble Disassembler.
  • down Move the current frame down.
  • edit Open where you are in DELVE_EDITOR or EDITOR
  • exit—quit 退出调试
  • frame Set the current frame, or execute command on a different -frame.
  • funcs 打印函数列表
  • goroutine 显示或更改当前goroutine
  • goroutines 列出程序的全部goroutines
  • help—h 打印出帮助信息
  • libraries List loaded dynamic libraries
  • list—ls Show source code.
  • locals 打印局部变量
  • next—n 跳到下一行
  • on 在遇到断点时执行一个命令
  • print—p Evaluate an 表达式.
  • regs 打印CPU寄存器的内容
  • restart—r 从a checkpoint or event重启进程
  • rev Reverses the execution of the target program for the command specified.
  • rewind Run backwards until breakpoint or program termination.
  • set 更改变量的值
  • source 执行包含delve命令列表的文件
  • sources 打印源文件列表
  • stack—bt 打印堆栈跟踪信息
  • step—s 单步执行程序
  • step-instruction—si 单步单个执行cpu指令
  • stepout 退出当前函数
  • thread—tr 切换到指定的线程
  • threads 打印每一个跟踪线程的信息
  • trace—t 设置跟踪点
  • types 打印类型列表
  • up Move the current frame up.
  • vars 打印某个包内的(全局)变量
  • whatis 打印表达式的类型

常用命令:

    #直接调试
    $ dlv debug ./main.go
    $ b main.main
    $ c
    $ b main.hi      #在func li  里打一个断点
    $ b /home/goworkspace/src/github.com/mytest/main.go:20  #使用   "文件:行号"来打断点
    $ n  #回车,单步执行
    $ print #(别名p)输出变量信息      p  hostName
    $ args #打印出所有的方法参数信息
    $ locals  #打印所有的本地变量
    
    #附加调试
    $ go build main.go
    $ ./main
    $ ps aux|grep main   #查pid
    $ dlv attach 29260
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值