Debug a Vala or C App
How to effectively debug Vala or C code.
This page mainly focuses on debugging in the Terminal. If you want to setup a graphical environment for Vala development and debugging, please have a look at Vala Development in VSCode.
If you like my work, ❤️ Sponsor Me. It would mean the world to me!
Enable Debug Output
Set the environment variable G_MESSAGES_DEBUG
to all
to get all log messages printed to stdout:
Or if the app in question is a Flatpak app, you'll need to start the app with flatpak run
:
Using GNU Debugger
Installing GNU Debugger
Retrieve a backtrace (e.g. to debug a Segmentation fault)
Run the program in question with GNU Debugger:
Once the application crashes you should see a message similar to the following:
To retrieve a backtrace at this state, simply execute the backtrace
command:
Retrieve a backtrace for a warning or critical message
In this case you want to abort the program as soon as a warning or critical message is logged. To do so, the G_DEBUG
environment variable comes in handy:
Once the environment variable is set, it causes the application to crash at the desired place. You are then able to retrieve a backtrace as described above.
Using core dumps
You can let Linux automatically collect the required information for a backtrace whenever an application crashes unexpectedly. This information is stored in a so called "core dump" file which contains the recorded state of the complete memory from the crashed application. Those core dumps are especially useful for errors which occur seemingly at random, because you don't need to explicitly start the application using GNU Debugger and you are still able to retrieve a backtrace after a crash.
Please be aware that core dumps may contain sensitive information due to their very nature: They are really just a copy of the app's memory at a given point in time. This may or may not include sensitive information such as passwords etc.
Installing systemd-coredump
List existing core dumps
Debug existing core dump
Further Reading
Last updated