美文网首页工作生活
golang 调试工具 dlv

golang 调试工具 dlv

作者: 每天十分钟玩转测试 | 来源:发表于2019-07-01 16:36 被阅读0次
    package main
    
    import "fmt"
    
    // dlv 断点调试法
    /*
    
    下载安装:
        go get -u github.com/derekparker/delve/cmd/dlv
    
    使用: 
    (base) ➜  dlv dlv debug ./learn2.go
    Type 'help' for list of commands.
    (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.
        down ------------------------ Move the current frame down.
        edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITOR
        exit (alias: quit | q) ------ Exit the debugger.
        frame ----------------------- Set the current frame, or execute command on a different frame.
        funcs ----------------------- Print list of functions.
        goroutine (alias: gr) ------- Shows or changes current goroutine
        goroutines (alias: grs) ----- List program goroutines.
        help (alias: h) ------------- Prints the help message.
        libraries ------------------- List loaded dynamic libraries
        list (alias: ls | l) -------- Show source code.
        locals ---------------------- Print local variables.
        next (alias: n) ------------- Step over to next source line.
        on -------------------------- Executes a command when a breakpoint is hit.
        print (alias: p) ------------ Evaluate an expression.
        regs ------------------------ Print contents of CPU registers.
        restart (alias: r) ---------- Restart process.
        set ------------------------- Changes the value of a variable.
        source ---------------------- Executes a file containing a list of delve commands
        sources --------------------- Print list of source files.
        stack (alias: bt) ----------- Print stack trace.
        step (alias: s) ------------- Single step through program.
        step-instruction (alias: si)  Single step a single cpu instruction.
        stepout --------------------- Step out of the current function.
        thread (alias: tr) ---------- Switch to the specified thread.
        threads --------------------- Print out info for every traced thread.
        trace (alias: t) ------------ Set tracepoint.
        types ----------------------- Print list of types
        up -------------------------- Move the current frame up.
        vars ------------------------ Print package variables.
        whatis ---------------------- Prints type of an expression.
        Type help followed by a command for full documentation.
    
        以上为全部命令, 常用的有一下几个:
        b 打断点
        eg:
            b main.main  或者使用文件:行号来打断点
        p 打印:
        eg:
            p a   打印a变量
        n: 执行到下一行
        c: 跳过此断点
        args: 打印所有的方法参数
        locals  打印出所有的本地变量
        l 列出断点最近几行的代码
        bp: 展示出所有的断点
        q: 退出
    */
    
    func main() {
        a := []string{"a", "b", "c", "D", "ef"}
        for k, v := range a {
            fmt.Println(k, v)
        }
    }
    

    使用示例

    (base) ➜  dlv dlv debug ./learn2.go
    Type 'help' for list of commands.
    (dlv) b main.main
    Breakpoint 1 set at 0x4a243b for main.main() ./learn2.go:67
    (dlv) c
    > main.main() ./learn2.go:67 (hits goroutine(1):1 total:1) (PC: 0x4a243b)
        62:     c: 跳过此断点
        63:     args: 打印所有的方法参数
        64:     locals  打印出所有的本地变量
        65: */
        66: 
    =>  67: func main() {
        68:     a := []string{"a", "b", "c", "D", "ef"}
        69:     for k, v := range a {
        70:         fmt.Println(k, v)
        71:     }
        72: }
    (dlv) l
    > main.main() ./learn2.go:67 (hits goroutine(1):1 total:1) (PC: 0x4a243b)
        62:     c: 跳过此断点
        63:     args: 打印所有的方法参数
        64:     locals  打印出所有的本地变量
        65: */
        66: 
    =>  67: func main() {
        68:     a := []string{"a", "b", "c", "D", "ef"}
        69:     for k, v := range a {
        70:         fmt.Println(k, v)
        71:     }
        72: }
    (dlv) b 70
    Breakpoint 2 set at 0x4a253c for main.main() ./learn2.go:70
    (dlv) bp 
    Breakpoint runtime-fatal-throw at 0x42c4a0 for runtime.fatalthrow() /home/yz/go/src/runtime/panic.go:663 (0)
    Breakpoint unrecovered-panic at 0x42c510 for runtime.fatalpanic() /home/yz/go/src/runtime/panic.go:690 (0)
        print runtime.curg._panic.arg
    Breakpoint 1 at 0x4a243b for main.main() ./learn2.go:67 (1)
    Breakpoint 2 at 0x4a253c for main.main() ./learn2.go:70 (0)
    (dlv) c
    > main.main() ./learn2.go:70 (hits goroutine(1):1 total:1) (PC: 0x4a253c)
    Warning: listing may not match stale executable
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
        69: */
    =>  70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
        75:     }
    (dlv) p k
    0
    (dlv) p v
    "a"
    (dlv) n 
    0 a
    > main.main() ./learn2.go:69 (PC: 0x4a261b)
    Warning: listing may not match stale executable
        64:     locals  打印出所有的本地变量
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
    =>  69: */
        70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
    (dlv) p k
    0
    (dlv) n
    > main.main() ./learn2.go:70 (hits goroutine(1):2 total:2) (PC: 0x4a253c)
    Warning: listing may not match stale executable
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
        69: */
    =>  70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
        75:     }
    (dlv) n
    1 b
    > main.main() ./learn2.go:69 (PC: 0x4a261b)
    Warning: listing may not match stale executable
        64:     locals  打印出所有的本地变量
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
    =>  69: */
        70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
    (dlv) p k
    1
    (dlv) p v
    "b"
    (dlv) c
    > main.main() ./learn2.go:70 (hits goroutine(1):3 total:3) (PC: 0x4a253c)
    Warning: listing may not match stale executable
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
        69: */
    =>  70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
        75:     }
    (dlv) c
    2 c
    > main.main() ./learn2.go:70 (hits goroutine(1):4 total:4) (PC: 0x4a253c)
    Warning: listing may not match stale executable
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
        69: */
    =>  70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
        75:     }
    (dlv) c
    3 D
    > main.main() ./learn2.go:70 (hits goroutine(1):5 total:5) (PC: 0x4a253c)
    Warning: listing may not match stale executable
        65:     l 列出断点最近几行的代码
        66:     bp: 展示出所有的断点
        67: 
        68: 
        69: */
    =>  70: 
        71: func main() {
        72:     a := []string{"a", "b", "c", "D", "ef"}
        73:     for k, v := range a {
        74:         fmt.Println(k, v)
        75:     }
    (dlv) c
    4 ef
    Process 16382 has exited with status 0
    (dlv) q
    Process 16382 has exited with status 0
    

    相关文章

      网友评论

        本文标题:golang 调试工具 dlv

        本文链接:https://www.haomeiwen.com/subject/ppqncctx.html