dlv
Debug Go programs
TLDR
Compile and begin debugging the main package in the current directory (by default, with no arguments)
Compile and begin debugging a specific package
Compile a test binary and begin debugging the compiled program
Connect to a headless debug server
Attach to a running process and begin debugging
Compile and begin tracing a program
SYNOPSIS
dlv command [arguments]
PARAMETERS
attach pid
Attach to an already running process specified by process id pid.
core corefile
Examine a core dump file.
debug [package]
Compile and begin debugging a main package.
exec program
Execute a precompiled binary.
test [package]
Compile and begin debugging a test package.
help [command]
Show help for the given command.
version
Prints the version of dlv.
DESCRIPTION
dlv is a powerful command-line debugger for Go programs. It allows developers to step through their code, set breakpoints, inspect variables, and evaluate expressions. dlv supports debugging local and remote processes, making it suitable for debugging various Go applications, including web servers and distributed systems.
It provides a rich set of commands for controlling the execution flow and examining the program's state, aiding in identifying and resolving bugs. The debugger leverages Go's runtime and compiler information to provide accurate and comprehensive debugging capabilities.
CAVEATS
dlv requires appropriate permissions to debug processes, especially when attaching to processes owned by other users. Also, debugging optimized binaries can sometimes lead to unexpected behavior due to compiler optimizations.
<B>DEBUGGING REMOTE PROCESSES</B>
dlv can debug Go programs running on remote machines. This typically involves running dlv in server mode on the remote machine and connecting to it from a local client. This is useful for debugging applications deployed in cloud environments or containers.
<B>BREAKPOINTS</B>
Breakpoints are essential for stopping the program execution at specific locations. dlv supports various types of breakpoints, including line breakpoints, function breakpoints, and conditional breakpoints. The 'break' command is used to set breakpoints, and the 'clear' command removes them.
<B>INSPECTING VARIABLES</B>
The 'print' command in dlv allows inspecting the values of variables. It can also display the contents of complex data structures like structs, arrays, and maps. Additionally, expressions can be evaluated using the 'eval' command.
HISTORY
dlv was created to provide a more robust and Go-centric debugging experience than existing tools like GDB. It has evolved significantly over time, with improvements in performance, stability, and feature set. Initially created by Derek Parker, dlv quickly became a popular choice for Go developers due to its deep integration with the Go runtime and its ability to understand Go-specific constructs like goroutines and channels.
SEE ALSO
gdb(1)