Debugging with GDB

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.

Programming for Easy Debugging

There are steps you can take to minimize the amount of debugging you have to do or to assist in the process when it does occur. The best practices include

  • Use lots of comments to explain the purpose of variables and specific code.

  • Avoid overly complex macros, functions, and statements.

  • Make explicit use of parentheses or otherwise be careful with operator precedence.

  • Make sure you're using = (the assignment operator) and == (the equals comparison operator) properly, particularly within conditionals and loops.

  • Watch for array indexes and off-by-one errors.

  • Provide a simple, consistent user interface.

  • In switch conditionals, make sure you use break statements and a default case.

  • Be careful about parentheses with function-like macros.

  • Manage dynamic memory and pointers correctly.

  • Pay attention to compiler warnings.

Obviously not making mistakes is the best way to simplify debugging, but we've found that it's tough to count on that policy.


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).

    Figure A.12. Xcode's debugging interface.

  • 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.

    Figure A.13. The GDB help page.

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


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset