Finding out where your Program Crashes with the Emacs GUD

This post describes a very, very elementary debugging skill. Yet, I could not find any concise tutorial about it on the web. So, here we go!

Assume you’re developing a small software under Linux, maybe using C or C++ and the GCC compiler. Testing your program, you find that it crashes with an error (segfault, assertion, …). How do find the cause for this crash rapidly? How can you back-trace the error?

First, compile your program again adding the option “-g” (or “-gstabs“). The compiler (e.g. g++) will now include information necessary for debugging into your binary. Start Emacs and invoke “M-x gdb“. As parameter, enter the full path of your executable. You end up with something like “gdb --annotate=3 ~/myProject/myProgram“. In the newly opened buffer, set the commandline parameters as for example in “start --verbose-mode inputfile.dat“. Simply append the program options you normally use after “start“.

Run the command “c” (=continue). Your program will start – and crash. Now, invoke “M-x gdb-many-windows“. You will see the steps leading to the function that caused the crash in the window “stack frames”. By clicking on each of the steps, Emacs will directly navigate to the respective source code block, enabling you to trace the cause of the error. Find a more extensive tutorial here.

Comments are closed.