debug: go build -gcflags=all="-N -l" -ldflags '$(LDFLAGS)' $(BUILD_FLAGS) main.go
执行 make debug 完成代码的编译。
1 2
» make debug jackson@bogon go build -gcflags=all="-N -l" -ldflags '-X "github.com/dreamerjackson/crawler/version.BuildTS=2022-12-25 03:33:21" -X "github.com/dreamerjackson/crawler/version.GitHash=6a4e939d8e68f5f29ee9f46bb3dc898157a8ca8e" -X "github.com/dreamerjackson/crawler/version.GitBranch=master" -X "github.com/dreamerjackson/crawler/version.Version=v1.0.0"' main.go
执行 dlv exec 指令启动程序并开始调试执行,执行完毕后会出现如下的(dlv)提示符。
1 2 3 4
» sudo dlv exec ./main worker jackson@bogon Password: Type 'help' for list of commands. (dlv)
下面我们来看看在Delve调试中一些常见的命令。
**查看帮助信息:help。**当我们记不清楚具体指令的含义的时候,可以执行该指令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(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!!!) clear ----------------------- Deletes breakpoint. clearall -------------------- Deletes multiple breakpoints. condition (alias: cond) ----- Set breakpoint condition. config ---------------------- Changes configuration parameters. continue (alias: c) --------- Run until breakpoint or program termination. deferred -------------------- Executes command in the context of a deferred call. disassemble (alias: disass) - Disassembler. ....
**打断点:break 或者b。**执行该指令会在main函数处打印一个断点。
1 2
(dlv) b main.main Breakpoint 1 set at 0x2089e86 for main.main() ./main.go:8
(dlv) stack 0 0x0000000002071659 in github.com/dreamerjackson/crawler/cmd/worker.Run at ./cmd/worker/worker.go:135 1 0x00000000020702cb in github.com/dreamerjackson/crawler/cmd/worker.glob..func1 at ./cmd/worker/worker.go:44 2 0x0000000002058734 in github.com/spf13/cobra.(*Command).execute at /Users/jackson/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 3 0x00000000020596c6 in github.com/spf13/cobra.(*Command).ExecuteC at /Users/jackson/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 4 0x0000000002058c8f in github.com/spf13/cobra.(*Command).Execute at /Users/jackson/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968 5 0x0000000002089e5d in github.com/dreamerjackson/crawler/cmd.Execute at ./cmd/cmd.go:23 6 0x0000000002089e97 in main.main at ./main.go:9 7 0x000000000103e478 in runtime.main at /usr/local/opt/go/libexec/src/runtime/proc.go:250 8 0x000000000106fee1 in runtime.goexit at /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1571
(dlv) breakpoints Breakpoint 1 at 0x2089e86 for main.main() ./main.go:8 (1) Breakpoint 2 at 0x2071659 for github.com/dreamerjackson/crawler/cmd/worker.Run() ./cmd/worker/worker.go:135 (1)
**clear 命令,**清除断点。下面这个例子就可以清除序号为1的断点。
1 2
(dlv) clear 1 Breakpoint 1 cleared at 0x2089e86 for main.main() ./main.go:8
(dlv) goroutine 2 Switched from 1 to 2 (thread 8118196) (dlv) stack 0 0x000000000103e892 in runtime.gopark at /usr/local/opt/go/libexec/src/runtime/proc.go:362 1 0x000000000103e92a in runtime.goparkunlock at /usr/local/opt/go/libexec/src/runtime/proc.go:367 2 0x000000000103e6c5 in runtime.forcegchelper at /usr/local/opt/go/libexec/src/runtime/proc.go:301 3 0x000000000106fee1 in runtime.goexit at /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1571
(dlv) goroutine 1 Switched from 0 to 1 (thread 9333412) (dlv) stack 0 0x00000000010677e0 in runtime.systemstack_switch at /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:436 1 0x00000000010563e6 in runtime.libcCall at /usr/local/opt/go/libexec/src/runtime/sys_libc.go:48 2 0x000000000106629f in syscall.syscall at /usr/local/opt/go/libexec/src/runtime/sys_darwin.go:23 3 0x000000000107ce09 in syscall.write at /usr/local/opt/go/libexec/src/syscall/zsyscall_darwin_amd64.go:1653 4 0x00000000010d188e in internal/poll.ignoringEINTRIO at /usr/local/opt/go/libexec/src/syscall/syscall_unix.go:216 5 0x00000000010d188e in syscall.Write at /usr/local/opt/go/libexec/src/internal/poll/fd_unix.go:383 6 0x00000000010d188e in internal/poll.(*FD).Write at /usr/local/opt/go/libexec/src/internal/poll/fd_unix.go:794 7 0x00000000010d93c5 in os.(*File).write at /usr/local/opt/go/libexec/src/os/file_posix.go:48 8 0x00000000010d93c5 in os.(*File).Write at /usr/local/opt/go/libexec/src/os/file.go:176 9 0x00000000010e2775 in fmt.Fprintln at /usr/local/opt/go/libexec/src/fmt/print.go:265 10 0x0000000001a4e329 in fmt.Println at /usr/local/opt/go/libexec/src/fmt/print.go:274 11 0x0000000001a4e329 in github.com/dreamerjackson/crawler/cmd/worker.Run at ./cmd/worker/worker.go:84 12 0x0000000001a4e097 in github.com/dreamerjackson/crawler/cmd/worker.glob..func1 at ./cmd/worker/worker.go:45