Five minute intro to debuggers for Go

What should be written on the Golang / Go debugger box



The out of the box experience of debugging Go on a Mac was a tad disappointing. I was expecting to be able to type my usual list command and throw in a few break points and get started.

Firstly, gdb is no longer installed by default with the developer tools (apparently it works but …), and secondly lldb (which is installed by default) could not even list the code.

Delve (dlv)



Next up was dlv (
https://github.com/go-delve/delve) which is designed for Go.

This short article will cover the few things I found out the hard way that would have speeded up the experience of getting started with this debugger.

Installation



Just use brew (
https://brew.sh). If you have not already setup debugging on your mac you will want to read macOS considerations on the installation page.

Use



From the start its a little different to other debuggers - in go fashion you have an action after the program name

To start debugging your binary you use the command:
dlv debug binary

And the usual goto of a simple list command will get you

maurice@visitor216 testprog % dlv debug testprog
Type 'help' for list of commands.
(dlv) list
Stopped at: 0xa147ad0
=> 1: no source available


Naively you would assume you need some directive to find the source code on your system … but that is not what is happening …

What you should have typed is something like
list main.main and then you would be in familiar teritory.

(dlv) list main.main
Showing /Users/maurice/go/src/testprog/testprog.go:39 (PC: 0x1374032)
34: return "", err
35: }
36: return reply.val, nil
37: }
38:
39: func main() {
40: host := flag.String("host", "http://127.0.0.1:80/", "URL")
41: user := flag.String("user", "user", "Username")
42: pw := flag.String("pass", "pass", "Password")


The clue finding your starting points is to lookup your functions using the
funcs command (it takes a regexp as an argument)

How do I get the program I am debugging to start with command line arguments?



Another case where your past experience will lead you astray. There is no run command in the debugger … instead you pass the arguments explicitly after a -- when you invoke the debugger.

dlv debug testprog -- --host https://localhost:8080/


After putting in your break points use the
continue / c command to start the program.

Another little difference is that the shortcuts are exact not just prefixes of the full commands.

Conclusion



This debugger has made some different UI choices gdb and lldb that make the first toe in the water for a new user coming from those environments a bit frustrating. The choices are actually reasonable in the context of how the tool works and the target language, but they do require the breaking of familiar muscle memory typing habits.

Key take aways:
  • dlv debug program_name -- program_arguments
  • list main.main
  • continue to start the program
To get started in 5 minutes.