Delve is a debugger for the Go language. The goal is to provide an easy-to-use, fully functional debugging tool for the Go language.
Installation is available here.
Run the DLV version command to check whether the installation is successful:
$ dlv versionDelve the Debugger Version: 1.7.1 Build: $Id: bde2354aafb5a4043fd59838842c4cd4a8b6f0b $3Copy the code
Building an
Delve makes it easy to debug remotely deployed Go applications locally on a real project, but for Delve’s debugger to work properly, some information must be kept from being optimized by the compiler or linker during the build process. This is done by adding parameters to the Go Build.
Disable compiler optimization when building applications
To ensure that the actual code running when debugging is the same as the source code, you need to use the following command to build the application to prevent the compiler from optimizing the code at compile time.
go build -gcflags="all=-N -l" -o bin/app
Go build can pass parameters to the go compiler with -gcflags, which is the same parameter as the go tool compile –help, so you can see all available parameters with go tool compile –help. All = -n -l” indicates that -n and -l are applied to compilation of all packages.
-n: compile –help go tool compile –help -n: compile –help
-n disable optimizations # disabling optimizations. -l disable inlining # Disabling inliningCopy the code
Use -m to view compiler optimization decisions.
Delve does not force compiler optimizations to be disabled, but it may cause the actual running code to not match the source code, so that the mismatched code (such as code optimized inline) cannot be debugged. Production environments are performance-sensitive, and optimization strategies should not be abandoned lightly.
Make sure the linker generates the DWARF information
Also, when building the Release package we usually add the ldFlags argument: -ldFlags ‘-w -s’. Ldflags passes arguments to the linker, and -w reduces the size of the executable file by nearly 20%.
Use go Tool link –help to check whether -w and -s work.
-s disable symbol table # Do not use the symbol table. -w disable DWARF generation # Do not generate DWARF informationCopy the code
Note that Delve relies on DWARF information, so you cannot use the -w parameter if you want to debug with Delve.
Debug your application with Delve
Delve provides rich local application debugging capabilities. In addition, Delve allows us to connect to its debugger remotely, providing the ability to debug remote applications locally.
Run the application directly
Delve has two ways to run applications directly. One is to directly start the application in debug mode in the application root directory (similar to main.go), and then enter the interactive mode of Delve.
$ dlv debug
Type 'help' for list of commands.
(dlv) threads
* Thread 457777 at :0
(dlv)
Copy the code
Type help to view all available commands, and type quit to exit and terminate the program.
The other is to run an executable:
$ go build -o demo
$ dlv exec demo
Type 'help' for list of commands.
(dlv) threads
* Thread 459012 at :0
(dlv)
Copy the code
Attach to a running Go process
Build and start the Web service.
$ go build
$ ./demo
2021/09/06 15:26:23 read config from: config.yaml
2021/09/06 15:26:23 start server and listen: 127.0.0.1:8081
Copy the code
The DLV is attached to the Demo application whose PID is 17062.
$ps PID TTY TIME CMD 17062 ttys000 05:00.02./thewaytowire $DLV attach 17062 Type 'help' for list of commands. (DLV)Copy the code
Expose the Delve debugger to allow remote connection debugging
All of these methods debug locally. The basic logic is to start the application, then start Delve’s debugger, and then attach the debugger to the application process.
In order to be able to control Delve’s debugger remotely, you need to expose the debugger to the network, specify the port to connect to remotely, the VERSION of the API to be used by the debugger remotely, and other parameters. The above three methods: debug, exec, attach all support remote control, just add the following parameters to the DLV command:
--listen=:2345 --headless=true --api-version=2 --accept-multiclient
Finally, the local connection runs in the remote Delve debugger
Delve supports multiple editors, which can be found here.
Here’s how GoLand is used.
Create the configuration.
Connect the debugger.