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}