golang 调试篇 一、go delve 使用

一、安装  参考文档 https://github.com/go-delve/delve/blob/master/Documentation/installation/linux/install.md

二、使用

  1. go build hello.go 生成二进制文件 hello
dlv exec ./hello -- server --config conf/config.toml
break main.go:52

(dlv) break main.go:52
Breakpoint 1 set at 0x109b901 for main.main() ./main.go:52

(dlv) bp
Breakpoint runtime-fatal-throw at 0x1028450 for runtime.fatalthrow() /usr/local/go/src/runtime/panic.go:663 (0)
Breakpoint unrecovered-panic at 0x10284c0 for runtime.fatalpanic() /usr/local/go/src/runtime/panic.go:690 (0)
	print runtime.curg._panic.arg
Breakpoint 1 at 0x109b901 for main.main() ./main.go:52 (0)


(dlv) c
> main.main() ./main.go:52 (hits goroutine(1):1 total:1) (PC: 0x109b901)
Warning: debugging optimized function
    47:		return n
    48:	}
    49:	
    50:	func main() {
    51:		// Create the network
=>  52:		net := NewGreetingApp()
    53:		// We need a channel to talk to it
    54:		in := make(chan string)
    55:		net.SetInPort("In", in)
    56:		// Run the net
    57:		wait := goflow.Run(net)

(dlv) b +2     //偏移量2加断点
Breakpoint 2 set at 0x109b90f for main.main() ./main.go:54

(dlv) next    //下一个断点
> main.main() ./main.go:54 (hits goroutine(1):1 total:1) (PC: 0x109b90f)
Warning: debugging optimized function
    49:	
    50:	func main() {
    51:		// Create the network
    52:		net := NewGreetingApp()
    53:		// We need a channel to talk to it
=>  54:		in := make(chan string)
    55:		net.SetInPort("In", in)
    56:		// Run the net
    57:		wait := goflow.Run(net)
    58:		// Now we can send some names and see what happens
    59:		in <- "John"

(dlv) n
> main.main() ./main.go:55 (PC: 0x10c1d82)
    50:	func main() {
    51:		// Create the network
    52:		net := NewGreetingApp()
    53:		// We need a channel to talk to it
    54:		in := make(chan string)
=>  55:		net.SetInPort("In", in)
    56:		// Run the net
    57:		wait := goflow.Run(net)
    58:		// Now we can send some names and see what happens
    59:		in <- "John"
    60:		in <- "Boris"

(dlv) p in  //打印变量
chan string {
	qcount: 0,
	dataqsiz: 0,
	buf: *[0]string [],
	elemsize: 16,
	closed: 0,
	elemtype: *runtime._type {
		size: 16,
		ptrdata: 8,
		hash: 3774831796,
		tflag: tflagUncommon|tflagExtraStar|tflagNamed (7),
		align: 8,
		fieldalign: 8,
		kind: 24,
		alg: *(*runtime.typeAlg)(0x11ae170),
		gcdata: *1,
		str: 7020,
		ptrToThis: 60992,},
	sendx: 0,
	recvx: 0,
	recvq: waitq<string> {
		first: *sudog<string> nil,
		last: *sudog<string> nil,},
	sendq: waitq<string> {
		first: *sudog<string> nil,
		last: *sudog<string> nil,},
	lock: runtime.mutex {key: 0},}

Commands

Command Description
args Print function arguments.
break Sets a breakpoint.
breakpoints Print out info for active breakpoints.
call Resumes process, injecting a function call (EXPERIMENTAL!!!)
check Creates a checkpoint at the current position.
checkpoints Print out info for existing checkpoints.
clear Deletes breakpoint.
clear-checkpoint Deletes checkpoint.
clearall Deletes multiple breakpoints.
condition Set breakpoint condition.
config Changes configuration parameters.
continue Run until breakpoint or program termination.
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 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 Prints the help message.
libraries List loaded dynamic libraries
list Show source code.
locals Print local variables.
next Step over to next source line.
on Executes a command when a breakpoint is hit.
print Evaluate an expression.
regs Print contents of CPU registers.
restart Restart process from 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 Changes the value of a variable.
source Executes a file containing a list of delve commands
sources Print list of source files.
stack Print stack trace.
step Single step through program.
step-instruction Single step a single cpu instruction.
stepout Step out of the current function.
thread Switch to the specified thread.
threads Print out info for every traced thread.
trace Set tracepoint.
types Print list of types
up Move the current frame up.
vars Print package variables.
whatis Prints type of an expression.
  • dlv attach - Attach to running process and begin debugging.

  • dlv connect - Connect to a headless debug server.

  • dlv core - Examine a core dump.

  • dlv debug - Compile and begin debugging main package in current directory, or the package specified.

  • dlv exec - Execute a precompiled binary, and begin a debug session.

  • dlv replay - Replays a rr trace.

  • dlv run - Deprecated command. Use 'debug' instead.

  • dlv test - Compile test binary and begin debugging program.

  • dlv trace - Compile and begin tracing program.

  • dlv version - Prints version.

  • dlv log - Help about logging flags

  • dlv backend - Help about the --backend flag

发布了37 篇原创文章 · 获赞 9 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qingxili/article/details/102558909
今日推荐