Of the available tools for developing C applications, GDB (Gnu Debugger) is one of the more common. GDB is a command-line tool for C and C++, intended to help you find bugs in an application. It takes a while to learn how to make the most out of GDB, but if you do much C programming, doing so will pay off.
GDB will do the following:
Show the source code of a file.
Show the values of variables.
Stop infinite loops.
Demonstrate the logical progression through an application.
Stop a program's execution at set points.
Walk through a program, instruction by instruction.
Allow you to inspect a program's state immediately after it crashes (to see what caused the crash).
GDB comes with Dev-C++ and is readily available on most variants of Unix, including Mac OS X. You can also download it and find out more information at www.gnu.org/software/gdb/gdb.html.
Showing you how to use GDB is beyond the scope of this book, but we can offer you a few starter tips:
You cannot debug a file until you have tried to compile it. The compilation process generates the information GDB needs.
On Dev-C++, be certain to click the Generate debugging information check box in your preferences panel (Figure A.3).
When using the command line gcc complier, always use the -g option to generate debugging data.
In Dev-C++, select Execute > Debug or press F8 to bring up the debugger from the command line.
In Xcode, you can select Debug > Show Debugger to bring up the graphical version of the debugger (Figure A.12).
To use the debugger from the command line, type gdb application_name, where application_name is the name of the compiled executable.
Get help by typing help in the console window (Figure A.13). Type help commandname to get help on a specific command.
Those are specific tips for working with GDB using the tools recommended by this book. Once you get to the point where you want to learn all about GDB, search the Web for “gdb tutorial” and peruse any of the thousands of results.
✓ Tips
The Insight Debugger (http://sources.redhat.com/insight/) is a graphical interface to GDB for Unix.
DDD (Data Display Debugger, www.gnu.org/software/ddd/) is another popular graphical interface to GDB.