In This Chapter
Source Debugging with GDB
Debugging Multiprocess Applications
Debugging Multithreaded Applications
Debugging Programs Already Running
Post-mortem Debugging
The GNU Debugger (also known as GDB) is a source-level debugger that provides the ability to debug applications at the source and machine levels. Additionally, GDB permits the debugging of already running applications (by attaching to the application s process ID) as well as debugging applications post-mortem. All of the traditional features one would expect from a source-level debugger are available with GDB, including multilanguage and multiarchitecture support.
This chapter will introduce GDB and explore its features and capabilities in a tutorial manner.
Before we jump into GDB, we ll first need to know how to build our application so that it s debuggable by GDB. The -g flag tells the compiler to include debugging information in the image, which can be used by GDB to understand variable types (for data inspection) and machine instruction to source-line mappings. Compiling is illustrated as:
# gcc -g testapp.c -o testapp
The test image can now be successfully debugged via GDB. One very important point to note is that debugging with optimization enabled can yield odd results. The optimizer may move code around or remove code altogether. This can make an optimized debugging session confusing and hard to follow. Therefore, while GDB will still debug optimized code, it s much easier to debug an unoptimized image.