Debugging Go applications on FreeBSD

Go uses a standardised debugging standard called DWARF. Yes, the pun is intended because DWARF was developed alongside ELF (Executable and Linking Format)

The be able to debug you need a debugger wich understands this standard.  The version Go build emits is DWARF3, GDB version 7.x supports this version. The standard debugger (used to compile the system) is version 6 9DWARF2 only), so you need to install a newer one. (/usr/ports/devel/gdb is version 7.x).

Instead of the usual `go run MyBuggyProgram.go`, you need to first build the program:
go build MyBuggyProgram.go

and start it with the debugger:
/usr/local/bin/gdb74 EchoSCTPServer -d $GOROOT

The environment variable GOROOT is optional but is useful if you want to debug the Go code itself, it has to point to a directory where Go is build from source.

Once within the debugger the usual commands are valid:

  • Ctrl+l: clear screen.
  • run: run programme
  • break: set break point
  • next: execute current line and move on the next
  • print (variable): print the value of (variable)
  • step: step into a function while executing

An example debug session:

(gdb) b 33
Note: breakpoint 1 also set at pc 0x400c8b.
Breakpoint 2 at 0x400c8b: file /home/olivier/projects/msc-report-src/echo-go-sctp/server/EchoSCTPServer.go, line 33.
(gdb) run
(gdb) s
net.ListenPacket (net="sctp", addr="localhost:4242", noname=void) at /home/olivier/projects/go-sctp/src/pkg/net/dial.go:213
213 func ListenPacket(net, addr string) (PacketConn, error) {
(gdb) s
214 afnet, a, err := resolveNetAddr("listen", net, addr)
(gdb) n
215 if err != nil {
(gdb) p err
$1 = 0
(gdb) p a
$2 = {IP = {array = "\177", len = 4, cap = 4}, Port = 4242}

More info can be found on the Go blog

Happy debugging 🙂

Olivier

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s