Delve is now available on Red Hat Enterprise Linux (RHEL). Starting in the RHEL 8.2 and devtools-2020.2
releases, the Go language debugger Delve will be installed with the Go toolchain itself via the go-toolset
package.
Being tailored specifically for Go, Delve has intricate knowledge of the Go runtime and provides features and an environment not available in other debuggers. The tool aims for simplicity of use, staying out of your way as you figure out what's going wrong with your program. Delve also offers powerful features that let you debug your Go programs as quickly as possible.
Installation
Installation requires only one command to install both Go and Delve:
sudo dnf install -y go-toolset
If the command is successful, both the Go toolchain and the Delve debugger will be installed and ready to use.
Advantages of Delve
As I mentioned earlier, the goal of this tool is to stay out of your way as much as possible. This means removing many manual steps to provide a straightforward debugging experience.
As an example, the Go compiler tries to choose sensible defaults wherever feasible, so it turns on optimizations by default. The optimizations are great for running your Go programs in production but they can make debugging more difficult. Delve solves this issue by turning off optimizations automatically when building your binary. Delve can also work alongside other tools such as Mozilla RR, which is currently not available as a package on RHEL, but can be built from source or installed from upstream releases.
Using Delve
Delve aims to be as simple to use as the Go command itself. The basic use case to start debugging a package is:
dlv debug
Issuing this command causes Delve to compile your program with optimizations disabled automatically, and land you at a command prompt ready to start debugging.
The other most common ways to begin debugging your Go program with Delve are to attach to a running process or to execute a pre-built binary:
dlv attach $pid dlv exec ./path/to/binary
The only potential downside to these options turns up if the binary was built with optimizations. In this case, some information might be unavailable to the debugger. So, if you want to invoke Delve on a precompiled binary, we recommend building it with optimizations disabled, using the following flags:
-go build -gcflags="-N -l"
Delve tips and tricks
Let's go over a few additional tips and tricks to help you make the most of Delve.
Delve can call functions in the debugged process
For instance, to jump to a function named main.callme
within Delve, enter:
(dlv) call main.callme
Delve can act like strace
Instead of starting a debugging session, you can connect to and trace a running binary. For example, you might trace a function named callme
as follows:
dlv trace callme > goroutine(1): main.callme(12) => (16)
The output from Delve displays both the argument and the return value when the function is called—all without ever starting an interactive debug session!
Delve allows different backends
For instance, you can switch to the Mozilla RR backend for record-and-replay debugging as follows:
dlv debug --backend=rr
To learn more about this technique, please watch my talk, Deterministic debugging with Delve.
Delve lets you script the debugger and add your own commands
To provide an API for users to automate Delve or add new commands, Delve uses the scripting language Starlark, which is similar to Python and was developed by the project that creates the Bazel build tool. See Delve's API documentation for more about this tool and how to use it.
Conclusion
Now that Delve is available in the go-toolset
package on RHEL, debugging Go programs has never been easier. Try it out and give your feedback in the upstream repo.