dlv 调试

一、安装

       https://github.com/go-delve/delve

直接去github上查看各种系统的安装教程,应该基本上都是 go get

二、命令

 1 Available Commands:
 2   attach      Attach to running process and begin debugging.
 3   connect     Connect to a headless debug server.
 4   core        Examine a core dump.
 5   dap         [EXPERIMENTAL] Starts a TCP server communicating via Debug Adaptor Protocol (DAP).
 6   debug       Compile and begin debugging main package in current directory, or the package specified.
 7   exec        Execute a precompiled binary, and begin a debug session.
 8   help        Help about any command
 9   run         Deprecated command. Use 'debug' instead.
10   test        Compile test binary and begin debugging program.
11   trace       Compile and begin tracing program.
12   version     Prints version.
13 
14 Flags:
15       --accept-multiclient   Allows a headless server to accept multiple client connections.
16       --api-version int      Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md. (default 1)
17       --backend string       Backend selection (see 'dlv help backend'). (default "default")
18       --build-flags string   Build flags, to be passed to the compiler.
19       --check-go-version     Checks that the version of Go in use is compatible with Delve. (default true)
20       --headless             Run debug server only, in headless mode.
21       --init string          Init file, executed by the terminal client.
22   -l, --listen string        Debugging server listen address. (default "127.0.0.1:0")
23       --log                  Enable debugging server logging.
24       --log-dest string      Writes logs to the specified file or file descriptor (see 'dlv help log').
25       --log-output string    Comma separated list of components that should produce debug output (see 'dlv help log')
26       --only-same-user       Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
27       --wd string            Working directory for running the program. (default ".")
28 
29 Additional help topics:
30   dlv backend Help about the --backend flag.
31   dlv log     Help about logging flags.

dlv 内部调试命令:

 1 Running the program:
 2     call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)
 3     continue (alias: c) --------- Run until breakpoint or program termination.
 4     next (alias: n) ------------- Step over to next source line.
 5     rebuild --------------------- Rebuild the target executable and restarts it. It does not work if the executable was not built by delve.
 6     restart (alias: r) ---------- Restart process.
 7     step (alias: s) ------------- Single step through program.
 8     step-instruction (alias: si)  Single step a single cpu instruction.
 9     stepout (alias: so) --------- Step out of the current function.
10 
11 Manipulating breakpoints:
12     break (alias: b) ------- Sets a breakpoint.
13     breakpoints (alias: bp)  Print out info for active breakpoints.
14     clear ------------------ Deletes breakpoint.
15     clearall --------------- Deletes multiple breakpoints.
16     condition (alias: cond)  Set breakpoint condition.
17     on --------------------- Executes a command when a breakpoint is hit.
18     trace (alias: t) ------- Set tracepoint.
19 
20 Viewing program variables and memory:
21     args ----------------- Print function arguments.
22     display -------------- Print value of an expression every time the program stops.
23     examinemem (alias: x)  Examine memory:
24     locals --------------- Print local variables.
25     print (alias: p) ----- Evaluate an expression.
26     regs ----------------- Print contents of CPU registers.
27     set ------------------ Changes the value of a variable.
28     vars ----------------- Print package variables.
29     whatis --------------- Prints type of an expression.
30 
31 Listing and switching between threads and goroutines:
32     goroutine (alias: gr) -- Shows or changes current goroutine
33     goroutines (alias: grs)  List program goroutines.
34     thread (alias: tr) ----- Switch to the specified thread.
35     threads ---------------- Print out info for every traced thread.
36 
37 Viewing the call stack and selecting frames:
38     deferred --------- Executes command in the context of a deferred call.
39     down ------------- Move the current frame down.
40     frame ------------ Set the current frame, or execute command on a different frame.
41     stack (alias: bt)  Print stack trace.
42     up --------------- Move the current frame up.
43 
44 Other commands:
45     config --------------------- Changes configuration parameters.
46     disassemble (alias: disass)  Disassembler.
47     edit (alias: ed) ----------- Open where you are in $DELVE_EDITOR or $EDITOR
48     exit (alias: quit | q) ----- Exit the debugger.
49     funcs ---------------------- Print list of functions.
50     help (alias: h) ------------ Prints the help message.
51     libraries ------------------ List loaded dynamic libraries
52     list (alias: ls | l) ------- Show source code.
53     source --------------------- Executes a file containing a list of delve commands
54     sources -------------------- Print list of source files.
55     types ---------------------- Print list of types

三、使用

dlv debug

  • 如果位于工程目录下,可以dlv debug开始调试;
  • 也可以指定目录,dlv debug GoWorks/GoDbg;
  • 如果要传入参数,添加–后指定, 如dlv debug GoWorks/GoDbg – -arg1 value

dlv exec

1 lin@MacBook-Pro test % ls
2 activitycenter    go.mod        go.sum        main.go        test
3 lin@MacBook-Pro test % dlv exec ./test 
4 Type 'help' for list of commands.
5 (dlv) 

dlv attach

1 lin@MacBook-Pro / % ps -ef | grep test   
2   501 32158  7146   0  8:12下午 ttys000    0:00.00 grep test
3   501 32156 31667   0  8:12下午 ttys001    0:00.24 ./test
4 lin@MacBook-Pro h/ % dlv attach 32156  
5 Type 'help' for list of commands.
6 (dlv) -h

注意:在退出时,有可选是否要kill该进程。

dlv core

注意:dlv不支持生成core,可以通过gdb attach上去后,执行gcore来生成core;然后再dlv core来调试。

posted @ 2020-09-15 20:28  林锅  阅读(1312)  评论(0编辑  收藏  举报