天天看點

【Go調試】Delve調試學習

參考:

  • https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md
  • https://juejin.im/entry/5aa1f98d6fb9a028c522c84b

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
           

繼續閱讀