Golang 现在可以很好地与 GDB 配合使用
这是一个示例 golang 应用程序gdbtest
- gdbtest/
- main.go
以下面的例子main.go
package main
import "fmt"
type MyStruct struct {
x string
i int
f float64
}
func main() {
x := "abc"
i := 3
fmt.Println(i)
fmt.Println(x)
ms := &MyStruct{
x: "cba",
i: 10,
f: 11.10335,
}
fmt.Println(ms)
}
将其保存到 main.go。然后使用以下gcflag 标志进行编译。
go build -gcflags "-N"
用你新建的 golang 应用打开 gdb
gdb gdbtest
# or
gdb <PROJECT_NAME>
您现在可以完全控制 gdb。例如,用br <linenumber>命令添加断点,然后用run执行应用程序
(gdb) br 22
Breakpoint 1 at 0x2311: file /go/src/github.com/cevaris/gdbtest/main.go, line 22.
(gdb) run
Starting program: /go/src/github.com/cevaris/gdbtest/gdbtest
3
abc
Breakpoint 1, main.main () at /go/src/github.com/cevaris/gdbtest/main.go:22
22 fmt.Println(ms)
(gdb)
现在你可以打印所有的局部变量了
(gdb) info locals
i = 3
ms = 0x20819e020
x = 0xdb1d0 "abc"
甚至可以访问指针
(gdb) p ms
$1 = (struct main.MyStruct *) 0x20819e020
(gdb) p *ms
$2 = {x = 0xdb870 "cba", i = 10, f = 11.103350000000001}