LoginSignup
4
3

More than 5 years have passed since last update.

rust-gdb を真似たgo-gdbという簡単gdb起動スクリプト

Posted at

gdbでgolangをデバッグする方法

gdbでgolangをデバッグする方法は公式ドキュメントに書かれています。
Debugging Go Code with GDB

しかし、オプションをつけたり、.gdbinit を作ったりしなくてはならないのでちょっと面倒くさい。
gdbでRustをデバッグするときもそのあたりの事情は同じはずなのですが、rust-gdbというコマンドがそのややこしさを隠蔽してくれています。

go-gdbスクリプト

rust-gdb のshellスクリプト実装を見つけました。
https://github.com/rust-lang/rust/blob/master/src/etc/rust-gdb
これを真似てgolang用のものを作りました。

go-gdb
#!/bin/sh
set -e
GOROOT=`go env GOROOT`

GO_GDB="${GO_GDB:-gdb}"
exec ${GO_GDB} \
  --directory="$GOROOT" \
  -iex "add-auto-load-safe-path $GOROOT/src/runtime/runtime-gdb.py" \
  "$@"

gistにも置きました。
https://gist.github.com/tetsu-koba/648166471d7c3db6f02aa1ffec1f0259

これをPATHの通ったディレクトリに置く。(例えば ~/bin )

使用例

$ go-gdb hello
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
Loading Go Runtime support.
(gdb) 

このように起動した時にLoading Go Runtime support. が出ていればOK。
golangのmain関数の先頭まで実行してブレークさせるにはstartコマンドが便利。

(gdb) start
Temporary breakpoint 1 at 0x483880: file /home/koba/go/src/hello/hello.go, line 5.
Starting program: /home/koba/go/src/hello/hello 
[New LWP 2361]
[New LWP 2362]
[New LWP 2363]

Thread 1 "hello" hit Temporary breakpoint 1, main.main ()
    at /home/koba/go/src/hello/hello.go:5
5   func main() {
(gdb) 

goroutineの一覧を見るにはinfo goroutine。省略してinfo go でもOK。

(gdb) info go
* 1 running  runtime.systemstack_switch
  2 waiting  runtime.gopark
  17 waiting  runtime.gopark
  18 waiting  runtime.gopark
(gdb) 

残念ながらgoroutineの切り替えはエラーになってしまって現状は使えない。

(gdb) goroutine 2
Python Exception <class 'ValueError'> not enough values to unpack (expected 2, got 1): 
Error occurred in Python command: not enough values to unpack (expected 2, got 1)
(gdb) 

emacsの中からgo-gdb を起動する

M-x gdb

を実行して、

Run gdb (like this): gdb -i=mi hello

これを以下のように書き換えるだけ。

Run gdb (like this): go-gdb -i=mi hello

参考。
emacsの中からrust-gdbを起動する

補足情報

gdbはpython拡張機能が有効になったものが必要です。ubuntuのaptでインストールできるものはそうなっているので大丈夫。
自分でgdbをビルドする時には、sudo apt install libpython-dev をしておくこと。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3