go筆記-delve除錯工具
阿新 • • 發佈:2021-07-17
讓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/[email protected]/xxx.go:281 XXXXXX/XXX/XXX/XXX (0xc9c9e7) Start: /data/home/XXX/XXX/XXXXXXX/XXX/XXX/[email protected]/xxx.go:122 XXXXXX/XXX/XXX/XXX (0xc9be20)
可以看到core發生的位置