首页 > 技术文章 > [go] 像gdb一样使用dlv debug golang程序

hugetong 2020-01-15 14:29 原文

gdb也可以debug golang程序,但是没有办法很好的处理协程。(或者我不太擅长)

golang本身,提供了更好的工具。 delve:https://github.com/go-delve/delve/tree/master/Documentation/installation

 

现在以gobench为例,https://github.com/tony-caotong/gobench

记录一下简单的用法。整体来说,和gdb没什么差别

 [classic_tong @  https://www.cnblogs.com/hugetong/p/12196521.html]

1. 启动

到工程目录下,直接运行dlv debug

┬─[tong@T7:~/Src/go/src/github.com/cmpxchg16/gobench]─[11:28:55 AM]
╰─>$ dlv debug
Type 'help' for list of commands.
(dlv) 

 

2 使用包名加函数名可以查看代码

(dlv) l main.main
Showing /home/tong/Src/go/src/github.com/cmpxchg16/gobench/gobench.go:322 (PC: 0x77d8bb)
 317:           }
 318:
 319:           done.Done()
 320:   }
 321:
 322:   func main() {
 323:
 324:           startTime := time.Now()
 325:           var done sync.WaitGroup
 326:           results := make(map[int]*Result)
 327:
(dlv) l fasthttp.AcquireRequest
Showing /home/tong/Src/go/src/github.com/valyala/fasthttp/client.go:909 (PC: 0x751693)
 904:   // AcquireRequest returns an empty Request instance from request pool.
 905:   //
 906:   // The returned Request instance may be passed to ReleaseRequest when it is
 907:   // no longer needed. This allows Request recycling, reduces GC pressure
 908:   // and usually improves performance.
 909:   func AcquireRequest() *Request {
 910:           v := requestPool.Get()
 911:           if v == nil {
 912:                   return &Request{}
 913:           }
 914:           return v.(*Request)
(dlv) 

 

3  在函数上设置断点

(dlv) b fasthttp.AcquireRequest
Breakpoint 1 set at 0x751693 for github.com/valyala/fasthttp.AcquireRequest() /home/tong/Src/go/src/github.com/valyala/fasthttp/client.go:909

   查看断点

(dlv) bp
Breakpoint runtime-fatal-throw at 0x434300 for runtime.fatalthrow() /usr/lib/go/src/runtime/panic.go:820 (0)
Breakpoint unrecovered-panic at 0x434370 for runtime.fatalpanic() /usr/lib/go/src/runtime/panic.go:847 (0)
        print runtime.curg._panic.arg
Breakpoint 1 at 0x77d3eb for main.client() ./gobench.go:278 (0)
Breakpoint 2 at 0x751693 for github.com/valyala/fasthttp.AcquireRequest() /home/tong/Src/go/src/github.com/valyala/fasthttp/client.go:909 (0)
(dlv) 

 

4 运行程序

(dlv) r -c 1 -r 1 -u http://www.baidu.com
Process restarted with PID 7345

运行之后程序会阻塞在入口,这个是和gdb的区别,这个时候要continue一下程序才会继续执行。

(dlv) c
Dispatching 1 clients
Waiting for results...
> main.client() ./gobench.go:278 (hits goroutine(9):1 total:1) (PC: 0x77d3eb)
   273:
   274:                 return myConn, nil
   275:         }
   276: }
   277:
=> 278: func client(configuration *Configuration, result *Result, done *sync.WaitGroup) {
   279:         for result.requests < configuration.requests {
   280:                 for _, tmpUrl := range configuration.urls {
   281:
   282:                         req := fasthttp.AcquireRequest()
   283:
(dlv) 

 

5 其他的命令,查看help,可以自助

(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.
。。。 。。。

 

6  q可以退出

 

------

推荐阅读