go笔记-查看coredump:delve调试工具
目录
coredump是一个包含程序意外终止时的内存快照的文件。它可以用于事后调试,以了解崩溃发生的原因以及其中涉及的变量。通过GOTRACEBACK,Go提供了一个环境变量来控制程序崩溃时产生的输出。这个变量可以强制生成coredump,便于调试。
让golang程序生成core文件
a. ulimit -c unlimited 修改 core 文件的大小
b. 环境变量export GOTRACEBACK=crash 说明golang程序产生coredump
可以使用gdb对coredump进行查看,delve对golang的兼容更好
1. 编译delve
# git clone https://github.com/derekparker/delve.git
# cd delve/cmd/dlv/
# go build
2. 将 dlv 拷贝到线上有coredump的服务器
分析coredump
./dlv core ./engine core.1871450_engine --check-go-version=false
3. 输入help可以查看Delve支持的命令
Type 'help' for list of commands.
(dlv) help
The following commands are available:
Running the program:
call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)
continue (alias: c) --------- Run until breakpoint or program termination.
next (alias: n) ------------- Step over to next source line.
rebuild --------------------- Rebuild the target executable and restarts it. It does not work if the executable was not built by delve.
restart (alias: r) ---------- Restart process.
rev ------------------------- Reverses the execution of the target program for the command specified.
rewind (alias: rw) ---------- Run backwards until breakpoint or program termination.
step (alias: s) ------------- Single step through program.
step-instruction (alias: si) Single step a single cpu instruction.
stepout (alias: so) --------- Step out of the current function.
Manipulating breakpoints:
break (alias: b) ------- Sets a breakpoint.
breakpoints (alias: bp) Print out info for active breakpoints.
clear ------------------ Deletes breakpoint.
clearall --------------- Deletes multiple breakpoints.
condition (alias: cond) Set breakpoint condition.
on --------------------- Executes a command when a breakpoint is hit.
toggle ----------------- Toggles on or off a breakpoint.
trace (alias: t) ------- Set tracepoint.
watch ------------------ Set watchpoint.
Viewing program variables and memory:
args ----------------- Print function arguments.
display -------------- Print value of an expression every time the program stops.
examinemem (alias: x) Examine memory:
locals --------------- Print local variables.
print (alias: p) ----- Evaluate an expression.
regs ----------------- Print contents of CPU registers.
set ------------------ Changes the value of a variable.
vars ----------------- Print package variables.
whatis --------------- Prints type of an expression.
Listing and switching between threads and goroutines:
goroutine (alias: gr) -- Shows or changes current goroutine
goroutines (alias: grs) List program goroutines.
thread (alias: tr) ----- Switch to the specified thread.
threads ---------------- Print out info for every traced thread.
Viewing the call stack and selecting frames:
deferred --------- Executes command in the context of a deferred call.
down ------------- Move the current frame down.
frame ------------ Set the current frame, or execute command on a different frame.
stack (alias: bt) Print stack trace.
up --------------- Move the current frame up.
Other commands:
check (alias: checkpoint) ----------- Creates a checkpoint at the current position.
checkpoints ------------------------- Print out info for existing checkpoints.
clear-checkpoint (alias: clearcheck) Deletes checkpoint.
config ------------------------------ Changes configuration parameters.
disassemble (alias: disass) --------- Disassembler.
dump -------------------------------- Creates a core dump from the current process state
edit (alias: ed) -------------------- Open where you are in $DELVE_EDITOR or $EDITOR
exit (alias: quit | q) -------------- Exit the debugger.
funcs ------------------------------- Print list of functions.
help (alias: h) --------------------- Prints the help message.
libraries --------------------------- List loaded dynamic libraries
list (alias: ls | l) ---------------- Show source code.
source ------------------------------ Executes a file containing a list of delve commands
sources ----------------------------- Print list of source files.
types ------------------------------- Print list of types
4. goroutine 显示或修改当前goroutine
(dlv) goroutine
Thread 1871459 at /usr/local/go/src/runtime/sys_linux_amd64.s:165
Goroutine 2072788:
Runtime: /usr/local/go/src/runtime/sys_linux_amd64.s:165 runtime.raise (0x474461)
User: /data/home/XXX/XXX/XXX.go:104 XXXXXX/XXX/XXX/XXX (0x14f454d)
Go:/data/home/XXX/XXX/XXXXXXX/XXX/XXX/XXX@v0.0.0-xxx/xxx.go:281 XXXXXX/XXX/XXX/XXX (0xc9c9e7)
Start: /data/home/XXX/XXX/XXXXXXX/XXX/XXX/XXX@v0.0.0-xxx/xxx.go:122 XXXXXX/XXX/XXX/XXX (0xc9be20)
可以看到core发生的位置
5. stack 查看堆栈信息
(dlv) stack
0 0x000000000046ede1 in runtime.raise
at /usr/local/go/src/runtime/sys_linux_amd64.s:168
1 0x00000000004507a5 in runtime.dieFromSignal
at /usr/local/go/src/runtime/signal_unix.go:852
2 0x0000000000451136 in runtime.sigfwdgo
at /usr/local/go/src/runtime/signal_unix.go:1066
3 0x000000000044f4c7 in runtime.sigtrampgo
at /usr/local/go/src/runtime/signal_unix.go:430
4 0x000000000046fc2e in runtime.sigtrampgo
at <autogenerated>:1
5 0x000000000046f0dd in runtime.sigtramp
at /usr/local/go/src/runtime/sys_linux_amd64.s:361
6 0x00007fe457148370 in ???
at ?:-1
7 0x000000000043a709 in runtime.crash
at /usr/local/go/src/runtime/signal_unix.go:944
8 0x000000000043a709 in runtime.fatalpanic
at /usr/local/go/src/runtime/panic.go:1092
9 0x0000000000439ed7 in runtime.gopanic
at /usr/local/go/src/runtime/panic.go:941
10 0x0000000000450676 in runtime.panicmem
at /usr/local/go/src/runtime/panic.go:220
11 0x0000000000450676 in runtime.sigpanic
at /usr/local/go/src/runtime/signal_unix.go:818
12 0x000000000046e441 in runtime.memmove
at /usr/local/go/src/runtime/memmove_amd64.s:182
13 0x00000000004553fc in runtime.concatstrings
at /usr/local/go/src/runtime/string.go:53
6. frame 13 设置栈帧
(dlv) frame 13
> runtime.raise() /usr/local/go/src/runtime/sys_linux_amd64.s:168 (PC: 0x46ede1)
Warning: debugging optimized function
Frame 13: /usr/local/go/src/runtime/string.go:53 (PC: 4553fc)
48: }
49: s, b := rawstringtmp(buf, l)
50: for _, x := range a {
51: copy(b, x)
52: b = b[len(x):]
=> 53: }
54: return s
55: }
56:
57: func concatstring2(buf *tmpBuf, a [2]string) string {
58: return concatstrings(buf, a[:])
(dlv)
7. locals / locals -v 打印本地变量
(dlv) locals -v
idx = (unreadable could not find loclist entry at 0x732a0 for address 0x4553fc)
l = (unreadable could not find loclist entry at 0x7330c for address 0x4553fc)
count = (unreadable could not find loclist entry at 0x7339e for address 0x4553fc)
s = "formal_test_str"
b = []uint8 len: 0, cap: 37, []
x = ""
8. args / args -v 打印函数参数
(dlv) args -v
inputName = ""
buf = (unreadable could not find loclist entry at 0x7316b for address 0x4553fc)
a = []string len: 6, cap: 0, [nil,(unreadable could not read string len error while reading spliced memory at 0x10: EOF),(unreadable could not read string len error while reading spliced memory at 0x20: EOF),(unreadable could not read string len error while reading spliced memory at 0x30: EOF),(unreadable could not read string len error while reading spliced memory at 0x40: EOF),...+1 more]
~r0 = (unreadable empty OP stack)
9. print 打印变量值
(dlv) print buf
附录
Delve 调试器 https://chai2010.cn/advanced-go-programming-book/ch3-asm/ch3-09-debug.html
https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md