When something goes badly wrong in a C program, things generally go south in one of two ways:
- If the programmer has been careful, the program will detect that something is wrong and abort itself
- If the programmer was lazy or the program has strayed into a bad place that was unanticipated, the operating system may terminate the program with a fault.
In either case, if you want to debug the problem, you need a core file. If your program aborts or faults and no core is produced, see Where’s my Core Dump? for help.
While it is possible to debug a core using gdb (see Debugging with GDB), I prefer to use a graphical debugger, the nearly as venerable ddd. In either case, you are in for a world of hurt if you have not built your program with debugger support. Please refer to the above post for details. Once you have a debuggable program and a core file, run ddd as follows:
ddd program-file core
DDD should open with one of the source files of the program showing up in the center pane, with an arrow pointing to the line where the fault (or abort) occurred. Sometimes, it won’t. For example, in the shot below, the main program file is displayed with no arrow. If you look in the bottom pane, you can see that the program died in raise, which is part of the C library.
Clicking Up in the DDD toolbar window will get you to the point in the program that triggered the call to raise. The first click told me that the program was in abort when it called raise. The next click showed me the error:
My program was intentionally aborting, because I had inverted the condition that was checking whether to abort if a value was not found. Removing the ~ from the line above the call to abort fixed it right up.