Debug a Vala or C App

How to effectively debug Vala or C code.

elementary OS: 6.0 OdinStatus: Verified

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:

G_MESSAGES_DEBUG=all com.github.marbetschar.my-app

Or if the app in question is a Flatpak app, you'll need to start the app with flatpak run:

G_MESSAGES_DEBUG=all flatpak run com.github.marbetschar.my-app

Using GNU Debugger

Installing GNU Debugger

sudo apt install gdb

Retrieve a backtrace (e.g. to debug a Segmentation fault)

Run the program in question with GNU Debugger:

$ gdb com.github.marbetschar.my-app
...
Reading symbols from com.github.marbetschar.my-app...
...
# Now type `run` and hit enter to start the app:
(gdb) run

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

Was this helpful?