Chapter 21. Debugging C Programs with GDB

An important part of software development is testing and troubleshooting. In a large program, programming errors—or bugs—are practically inevitable. Programs can deliver wrong results, get hung up in infinite loops, or crash due to illegal memory operations. The task of finding and eliminating such errors is called debugging a program.

Many bugs are not apparent by simply studying the source code. Extra output provided by a testing version of the program is one helpful diagnostic technique. You can add statements to display the contents of variables and other information during runtime. However, you can generally perform runtime diagnostics much more efficiently by using a debugger.

A debugger is a program that runs another program in a finely controlled environment. For example, a debugger allows you to run the program step by step, observing the contents of variables, memory locations, and CPU registers after each statement. You can also analyze the sequence of function calls that lead to a given point in the program.

This chapter is an introduction to one powerful and widely used debugger, the GNU debugger (GDB). The sections that follow describe GDB’s basic options and commands. Most of the features and working principles described here are similar to those of other debugging tools. For a complete description of GDB’s capabilities, see the program manual “Debugging with GDB” by the Free Software Foundation, which is available in PDF ...

Get C in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.