golang 調(diào)試工具 dlv

package main

import "fmt"

// dlv 斷點調(diào)試法
/*

下載安裝:
    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: 執(zhí)行到下一行
    c: 跳過此斷點
    args: 打印所有的方法參數(shù)
    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: 打印所有的方法參數(shù)
    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: 打印所有的方法參數(shù)
    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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窜锯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子挪捕,更是在濱河造成了極大的恐慌萨醒,老刑警劉巖迁沫,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異鸳粉,居然都是意外死亡扔涧,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門届谈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枯夜,“玉大人,你說我怎么就攤上這事艰山『ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵曙搬,是天一觀的道長摔吏。 經(jīng)常有香客問我,道長纵装,這世上最難降的妖魔是什么征讲? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮橡娄,結(jié)果婚禮上诗箍,老公的妹妹穿的比我還像新娘。我一直安慰自己挽唉,他們只是感情好扳还,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布才避。 她就那樣靜靜地躺著,像睡著了一般氨距。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上棘劣,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天俏让,我揣著相機與錄音,去河邊找鬼茬暇。 笑死首昔,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的糙俗。 我是一名探鬼主播勒奇,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼巧骚!你這毒婦竟也來了赊颠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤劈彪,失蹤者是張志新(化名)和其女友劉穎竣蹦,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沧奴,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡痘括,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了滔吠。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纲菌。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖疮绷,靈堂內(nèi)的尸體忽然破棺而出翰舌,到底是詐尸還是另有隱情,我是刑警寧澤矗愧,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布灶芝,位于F島的核電站,受9級特大地震影響唉韭,放射性物質(zhì)發(fā)生泄漏夜涕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一属愤、第九天 我趴在偏房一處隱蔽的房頂上張望女器。 院中可真熱鬧,春花似錦住诸、人聲如沸驾胆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丧诺。三九已至入桂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間驳阎,已是汗流浹背抗愁。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留呵晚,地道東北人蜘腌。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像饵隙,于是被迫代替她去往敵國和親撮珠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容