Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debugging Go with GDB

koya fukushi
August 31, 2017
120

Debugging Go with GDB

koya fukushi

August 31, 2017
Tweet

Transcript

  1. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 2/28 Who am I?

    福士 倖也と申します Pairs のサーバーサイド・フロントエンド www.pairs.lv/ (https://www.pairs.lv/) 大学時代はのほほんとヨットに乗ってました ヨット画像 (https://www.google.co.jp/search?biw=1315&bih=736&tbm=isch&sa=1&q=%E3%83%A8%E3%83%83%E3%83%88&oq=%E3%83%A8%E3%83%83%E3%83%88&gs_l=psy- ab.3...0.0.0.6966.0.0.0.0.0.0.0.0..0.0....0...1..64.psy-ab..0.0.0.DovgZ26-inI) 青森出身です
  2. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 6/28 Introduction 大量のlog の中から探し出さないといけない

    毎回build しなければいけない デバッグコードをcommit してしまうことがある 時間がかかりそうならデバッガを使うべき
  3. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 8/28 What is GDB

    GDB (GNU Debugger) はFSF( フリーソフトウェア財団) が配布しているUNIX システムのプログ ラムデバッグツール 以下のことができる ブレークポイントを設定 バックトレースなど実行時の状態の表示 実行中のプログラムの変数の書き換え
  4. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 10/28 How to install

    GDB Mac の場合install が少し面倒くさい (gdb) b 7 Breakpoint 1 at 0x206f: file /.../repos/src/github.com/Kooooya/ground/gdb/01.go, line 7. (gdb) run Starting program: /.../src/github.com/Kooooya/ground/gdb/01 Unable to find Mach task port for process-id 12160: (os/kern) failure (0x5). (please check gdb is codesigned - see taskgated(8)) macOS Sierra でgdb を使う (http://qiita.com/kaityo256/items/d2f7ac7acc42cf2098b2) ↑ のリンクを参考にinstall してみて下さい
  5. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 12/28 How to debug

    起動する $ go build -gcflags "-N -l" hoge.go # ← 難 最適化 $ gdb hoge GNU gdb (GDB) 8.0 Copyright (C) 2017 Free Software Foundation, Inc. ..... Loading Go Runtime support. (gdb)
  6. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 13/28 How to debug

    以下のコマンドがよく使われます list break run continue delete backtrace info print
  7. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 14/28 list command 行、ファイル名を指定してソースコードを表示

    (gdb) list (gdb) list line (gdb) l line (gdb) list file.go:line Example: (gdb) l 01.go:8 3 import "fmt" 4 5 func main() { 6 say() 7 } 8 9 func say() { 10 const n = 10000 11 for i := 0; i < n; i++ { 12 fmt.Println(i)}}
  8. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 15/28 break command ブレークポイントを設定

    (gdb) break line (gdb) b line (gdb) break file.go:line Example: (gdb) b 01.go:8 Note: breakpoint 1 also set at pc 0x208c. Breakpoint 2 at 0x208c: file /.../src/github.com/Kooooya/ground/gdb/01.go, line 8.
  9. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 16/28 run command プログラムを実行

    (gdb) run (gdb) r Example: (gdb) b 02.go:6 Breakpoint 1 at 0x20f4: file /.../src/github.com/Kooooya/ground/gdb/02.go, line 6. (gdb) run Starting program: /.../src/github.com/Kooooya/ground/gdb/01 [New Thread 0x1403 of process 15902] warning: unhandled dyld version (15) [New Thread 0x1207 of process 15902] [New Thread 0x1503 of process 15902] [New Thread 0x1603 of process 15902] [New Thread 0x1703 of process 15902] Thread 2 hit Breakpoint 1, main.Hoge (i=0) at /.../src/github.com/Kooooya/ground/gdb/02.go:6 6 fmt.Println(i) (gdb)
  10. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 17/28 continue command 次のブレークポイントまで実行

    (gdb) continue (gdb) c Example: (gdb) c Continuing. Thread 2 hit Breakpoint 1, main.Hoge (i=7) at /.../src/github.com/Kooooya/ground/gdb/02.go:6 6 fmt.Println(i) (gdb)
  11. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 18/28 delete command ブレークポイントを削除

    引数のnum は `info breakpoints` で対応する番号 (gdb) d num (gdb) delete num Example: (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000000020f4 in main.Hoge at /.../src/github.com/Kooooya/ground/gdb/02. breakpoint already hit 9 times (gdb) d 1
  12. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 19/28 backtrace バックトレースの出力 (gdb)

    bt (gdb) backtrace -n (gdb) bt n Example: (gdb) run Starting program: /.../src/github.com/Kooooya/ground/gdb/01 .... Thread 2 hit Breakpoint 2, main.Hoge (i=0) at /.../src/github.com/Kooooya/ground/gdb/02.go:6 6 fmt.Println(i) (gdb) bt #0 main.Hoge (i=0) at /.../src/github.com/Kooooya/ground/gdb/02.go:6 #1 0x00000000000020a5 in main.say () at /.../src/github.com/Kooooya/ground/gdb/01.go:10 #2 0x0000000000002054 in main.main () at /.../src/github.com/Kooooya/ground/gdb/01.go:4
  13. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 20/28 info command global

    変数 (gdb) info variables .... error io.EOF; error io.ErrClosedPipe; error io.ErrNoProgress; .... local 変数 (gdb) info locals i = 987 引数 (gdb) info args i = 987
  14. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 21/28 info command ブレークポイント一覧

    (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000000020f4 in main.Hoge at /.../src/github.com/Kooooya/ground/gdb/02. breakpoint already hit 1 time 2 breakpoint keep y 0x0000000000002097 in main.say at /.../src/github.com/Kooooya/ground/gdb/01.g
  15. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 22/28 info command goroutine

    一覧 (gdb) info goroutines * 1 running syscall.Syscall6 ← * 先頭 現在実行中 2 waiting runtime.gopark 3 waiting runtime.gopark 4 waiting runtime.gopark (gdb) goroutine 3 bt #0 runtime.gopark (unlockf={void (struct runtime.g *, void *, bool *)} 0xc420024f38, lock=0xf7d80 <runtime.s reason="GC sweep wait", traceEv=20 '\024', traceskip=1) at /usr/local/go/src/runtime/proc.go:260 #1 0x0000000000028f0e in runtime.goparkunlock (lock=0xf7d80 <runtime.sweep>, reason="GC sweep wait", traceEv at /usr/local/go/src/runtime/proc.go:265 #2 0x000000000001baa6 in runtime.bgsweep (c=0xc42004c000) at /usr/local/go/src/runtime/mgcsweep.go:63 #3 0x00000000000517e1 in runtime.goexit () at /usr/local/go/src/runtime/asm_amd64.s:2086 #4 0x000000c42004c000 in ?? () #5 0x0000000000000000 in ?? () (gdb) goroutine 3 info locals mp = 0xc42002a800 gp = 0xc420000b60
  16. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 23/28 print command 変数またはその他の情報を表示する

    (gdb) print var (gdb) p var (gdb) p $len(var) Example: (gdb) p i $5 = 31 (gdb) p &i $6 = (int *) 0xc42003bf18 (gdb) p $len(s) $2 = 11 (gdb) p $cap(s) $1 = 20
  17. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 24/28 How to debug

    他にも以下のコマンドなどがあります whatis ← 変数の型を表示 disas ← 逆アセンブル watch ← ウォッチポイント設定 set variable ← 変数書き換え ステップ実行 step next until nish
  18. 2017/8/31 Debugging Go with GDB http://go-talks.appspot.com/github.com/Kooooya/slides/2017/0827_golang_tokyo/gdb.slide#1 26/28 感想 tab 補完が効くのが気持ち良い

    cheatsheet 良い darkdust.net/ les/GDB%20Cheat%20Sheet.pdf (http://darkdust.net/ les/GDB%20Cheat%20Sheet.pdf) HTTP Server のデバッグどうするの? github.com/derekparker/delve (https://github.com/derekparker/delve) が良さそう