golang 调试工具 dlv

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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,922评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,591评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,546评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,467评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,553评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,580评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,588评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,334评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,780评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,092评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,270评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,925评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,573评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,194评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,437评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,154评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352

推荐阅读更多精彩内容