Debugging Golang using GDB?
I have 2 questions about GDB + golang?
1) Go build GCC flags
when i run "go build" what gcc flags does the go builder use to build the program? Build value is the same as "GOGCCFLAGS" set in go envionment?
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
because I don't see the -g "or" -g3 "flags to add a code character. If so, how can the character table be compiled?
2) How to print a value in GDB I followed the tutorial here GDB debug go tutorial but it doesn't seem to be what I asked.
BTW, I noticed there is a post about this gdb debug go However, it doesn't work for me either.
source to share
Golang works well with GDB now
Here is a sample golang app gdbtest
- gdbtest/
- main.go
Let's take the following example 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)
}
Save this value to main.go . Then compile the checkbox gcflag
.
go build -gcflags "-N"
Open gdb with the newly created golang app.
gdb gdbtest
# or
gdb <PROJECT_NAME>
You now have complete control over gdb. For example, add a breakpoint using the command br <linenumber>
, then run the application withrun
(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)
Now you can print all local variables
(gdb) info locals
i = 3
ms = 0x20819e020
x = 0xdb1d0 "abc"
Even access pointers
(gdb) p ms
$1 = (struct main.MyStruct *) 0x20819e020
(gdb) p *ms
$2 = {x = 0xdb870 "cba", i = 10, f = 11.103350000000001}
source to share
The accepted answer is outdated. Golang currently works with GDB (including local ones) if you build with flags -gcflags=all="-N -l"
as described in the official documentation
source to share