cev*_*ris 13

Golang现在可以很好地与GDB配合使用

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)
}
gcflag
go build -gcflags "-N"

使用新构建的golang应用程序打开gdb

gdb gdbtest
# or 
gdb <PROJECT_NAME>
br 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}