go语言之路

使用dlv调试Go程序

delve调试go代码 (http服务)

> dlv help
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`

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.
  dap         [EXPERIMENTAL] Starts a TCP server communicating via Debug Adaptor Protocol (DAP).
  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.

Flags:
      --accept-multiclient               Allows a headless server to accept multiple client connections.
      --allow-non-terminal-interactive   Allows interactive sessions of Delve that don't have a terminal as stdin, stdout and stderr
      --api-version int                  Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md. (default 1)
      --backend string                   Backend selection (see 'dlv help backend'). (default "default")
      --build-flags string               Build flags, to be passed to the compiler. For example: --build-flags="-tags=integration -mod=vendor -cover -v"
      --check-go-version                 Checks that the version of Go in use is compatible with Delve. (default true)
      --disable-aslr                     Disables address space randomization
      --headless                         Run debug server only, in headless mode.
  -h, --help                             help for dlv
      --init string                      Init file, executed by the terminal client.
  -l, --listen string                    Debugging server listen address. (default "127.0.0.1:0")
      --log                              Enable debugging server logging.
      --log-dest string                  Writes logs to the specified file or file descriptor (see 'dlv help log').
      --log-output string                Comma separated list of components that should produce debug output (see 'dlv help log')
      --only-same-user                   Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
  -r, --redirect stringArray             Specifies redirect rules for target process (see 'dlv help redirect')
      --wd string                        Working directory for running the program.

Additional help topics:
  dlv backend  Help about the --backend flag.
  dlv log      Help about logging flags.
  dlv redirect Help about file redirection.

Use "dlv [command] --help" for more information about a command.
  • attach(dlv attach $pid进入调试)
  • connect(远程调试)
  • debug(dlv debug main.go进入调试)
  • exec(dlv exec go二进制程序i进入调试)

b 打断点

c运行到断点

n单步执行

p 打印变量

locals 打印所有的本利变量

args打印所有方法的参数信息

测试代码

//conn.go
package main
import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.POST("/ping", ping)
    _ = r.Run(":8080")
}

func ping(c *gin.Context) {
    key := c.PostForm("key")
    c.IndentedJSON(200, gin.H{
        "data": key,
    })
}
> dlv debug test.go
> b main.main
shows: 
Breakpoint 1 set at 0x13e86ff for main.main() d:/git/home/jay/github/gotest/conn.go:7
> c
shows:
 main.main() d:/git/home/jay/github/gotest/conn.go:7 (hits goroutine(1):1 total:1) (PC: 0x13e86ff)
     2:
     3: import (
     4:         "github.com/gin-gonic/gin"
     5: )
     6:
=>   7: func main() {
     8:
     9:         r := gin.Default()
    10:         r.POST("/ping", ping)
    11:         _ = r.Run(":8080")
    12:
> n
shows:
> b main.ping
shows:
Breakpoint 2 set at 0x13e889f for main.ping() d:/git/home/jay/github/gotest/conn.go:15
> c
shows:
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /ping                     --> main.ping (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080

接下来我们就用curl模拟一次请求

curl -d "key=1243" localhost:8080/ping

image-20210319154345377

可以看到该方法执行了

接下来我们单步执行

image-20210319154454498

获取key的值

image-20210319154545981

成功!

其他的尝试后面更新!

alias cat = 'bat' 丢掉你的cat尽情拥抱bat吧

上一篇

一个比grep更快高效的文本搜索命令silversearcher-ag

下一篇

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

个人微信公众号

we-tuiguang

qq交流群

群号:1046260719

微信扫一扫

微信扫一扫