go build -gcflags=all="-N -l"## 必须这样编译,才能用gdb打印出变量,第二个是小写的L,不是大写的i
需要加编译选项,类似gcc中的 -g选项,加入调试信息。关于如何安装dlv,请自行百度或者谷歌。
2、使用dlv调试
dlv的功能介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Usage: dlv [command]
Available Commands: attach Attach to running process and begin debugging. connect Connect to a headless debug server. core Examine a core dump. debug Compile and begin debugging main package in current directory, or the package specified. exec Execute a precompiled binary, and begin a debug session. help Help about any command run Deprecated command. Use 'debug' instead. test Compile test binary and begin debugging program. trace Compile and begin tracing program. version Prints version.
(dlv) goroutine 8 ## 切换到 8 号 goroutine Switched from 0 to 8 (thread 21239)
(dlv) bt ## 查看栈帧 0 0x0000000000450774 in runtime.raise at /usr/local/go/src/runtime/sys_linux_amd64.s:159 1 0x000000000044cea0 in runtime.systemstack_switch at /usr/local/go/src/runtime/asm_amd64.s:363 2 0x00000000004265ba in runtime.dopanic at /usr/local/go/src/runtime/panic.go:597 3 0x00000000004261f1 in runtime.gopanic at /usr/local/go/src/runtime/panic.go:551 4 0x00000000004250ce in runtime.panicmem at /usr/local/go/src/runtime/panic.go:63 5 0x0000000000438baa in runtime.sigpanic at /usr/local/go/src/runtime/signal_unix.go:388 6 0x00000000004a33cb in main.main.func2 at ./time.go:114 ## 显然6号栈是自己的业务代码 7 0x000000000044f6d1 in runtime.goexit at /usr/local/go/src/runtime/asm_amd64.s:2361
(dlv) frame 6 ## 进入6号栈帧 > runtime.raise() /usr/local/go/src/runtime/sys_linux_amd64.s:159 (PC: 0x450774) Warning: debugging optimized function Frame 6: ./time.go:114 (PC: 4a33cb) 109: // if r := recover(); r != nil { 110: 111: // } 112: // }() 113: var p *int = nil => 114: fmt.Printf("p=%d\n", *p) ## 这里发生了异常 115: }() 116: 117: time.Sleep(10000000) 118: }
5、dlv test
dlv test 也很有特色,是用来调试测试代码的。因为测试代码都是某一个包里面,是以包为单位的。
1
dlv test$packname## 包名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[KentZhang@LOCAL-192-168-97-2 src]$ dlv test db ## 调试db包内部的测试用例 Type 'help' for list of commands. (dlv) b TestMoneyDbGet ## 打断点,不用加包名了 Breakpoint 1 set at 0x73c15b for db.TestMoneyDbGet() ./db/moneydb_test.go:9 (dlv) c > db.TestMoneyDbGet() ./db/moneydb_test.go:9 (hits goroutine(5):1 total:1) (PC: 0x73c15b) 4: "logger" 5: "testing" 6: ) 7: 8: //日志不分离 => 9: func TestMoneyDbGet(t *testing.T) { 10: logger.Init("testlog", ".", 1000, 3, logger.DEBUG_LEVEL, false, logger.PUT_CONSOLE) 11: c := MoneydbConnect("192.168.202.92:12515") 12: if nil == c { 13: t.Error("Init() failed.") 14: return (dlv)
(dlv) help The following commands are available: args -------------------------------- Print function arguments. break (alias: b) -------------------- Sets a breakpoint. breakpoints (alias: bp) ------------- Print out info for active breakpoints. call -------------------------------- Resumes process, injecting a function call (EXPERIMENTAL!!!) check (alias: checkpoint) ----------- Creates a checkpoint at the current position. checkpoints ------------------------- Print out info for existing checkpoints. clear ------------------------------- Deletes breakpoint. clear-checkpoint (alias: clearcheck) Deletes checkpoint. clearall ---------------------------- Deletes multiple breakpoints. condition (alias: cond) ------------- Set breakpoint condition. config ------------------------------ Changes configuration parameters. continue (alias: c) ----------------- Run until breakpoint or program termination. disassemble (alias: disass) --------- Disassembler. down -------------------------------- Move the current frame down. edit (alias: ed) -------------------- Open where you are in $DELVE_EDITOR or $EDITOR exit (alias: quit | q) -------------- Exit the debugger. frame ------------------------------- Set the current frame, or execute command on a different frame. funcs ------------------------------- Print list of functions. goroutine --------------------------- Shows or changes current goroutine goroutines -------------------------- List program goroutines. help (alias: h) --------------------- Prints the help message. list (alias: ls | l) ---------------- Show source code. locals ------------------------------ Print local variables. next (alias: n) --------------------- Step over to next source line. on ---------------------------------- Executes a command when a breakpoint is hit. print (alias: p) -------------------- Evaluate an expression. regs -------------------------------- Print contents of CPU registers. restart (alias: r) ------------------ Restart process from a checkpoint or event. rewind (alias: rw) ------------------ Run backwards until breakpoint or program termination. set --------------------------------- Changes the value of a variable. source ------------------------------ Executes a file containing a list of delve commands sources ----------------------------- Print list of source files. stack (alias: bt) ------------------- Print stack trace. step (alias: s) --------------------- Single step through program. step-instruction (alias: si) -------- Single step a single cpu instruction. stepout ----------------------------- Step out of the current function. thread (alias: tr) ------------------ Switch to the specified thread. threads ----------------------------- Print out info for every traced thread. trace (alias: t) -------------------- Set tracepoint. types ------------------------------- Print list of types up ---------------------------------- Move the current frame up. vars -------------------------------- Print package variables. whatis ------------------------------ Prints type of an expression.
help [command]
使用 help command 打印出具体命令的用法,例如:
1 2 3 4 5 6 7 8
(dlv) help set Changes the value of a variable.
[goroutine <n>] [frame <m>] set <variable> = <value> (dlv) help on Executes a command when a breakpoint is hit.