Debugging with GoLand – Advanced Debugging features
- Debugging with GoLand – Getting Started
- Debugging with GoLand – Essentials
- Debugging with GoLand – Advanced Debugging features (this post)
- Debugging with GoLand – Windows minidumps
Debugging core dumps
Core dumps are memory snapshots of running applications taken at a certain point in time. They can help us visualize all the goroutines present, with all the values for the variables, as well as the current execution point for each active goroutine.
Right now, GoLand supports only Core Dumps taken on Linux systems, but it can read and analyze them on any operating system.
There are two ways to obtain a core dump. If we want to see the in-memory values as the process terminates because of a crash, we need to set the ulimit to be reasonably high, e.g.,
sudo ulimit -c unlimited, and have the following environment variable configured
GOTRACEBACK=crash. This will enable Go applications to crash with a stacktrace printed and with a core dump file written.
You might also want to check the output of the cat /proc/sys/kernel/core_pattern command. The contents of the core_pattern file determine where the core file will be written and how it will be named.
If the output is not “core”, then your core dumps will not be written in the current working directory. To set the /proc/sys/kernel/core_pattern file to “core”, run this command in your terminal:
sudo sysctl -w kernel.core_pattern=core
To summarize, in order to successfully generate a core dump when a program crashes, you need to have the following output:
Build your program, run it with GOTRACEBACK=crash, and interrupt it with Ctrl + \. The core file should appear in your current working directory.
Get core dumps with gcore
To be able to get core dumps from a running process without having to crash it, we need to have gdb installed on the system and run these commands:
sudo ulimit -c unlimited echo 0 | sudo tee -a /proc/sys/kernel/yama/ptrace_scope
Note that this value will be reset on system restart and you’ll need to configure it again.
To generate and use the core dump, we need to configure the IDE to save the binary in a known location, such as our project root directory.