Linux Bible

Home



Time and effort invested in learning GDB is well spent if you can track down and fix a serious bug in just a few minutes. GDB can make this happen. Most of what you will need to accomplish with GDB can be done with a surprisingly small set of commands. The rest of this chapter explores GDB features and shows you enough GDB commands to get you going.


Effective debugging requires that your source code be compiled with the -g option to create a binary with an extended symbol table. For example, the following command $ gcc -g file1 file2 -o prog causes prog to be created with debugging symbols in its symbol table. If you want, you can use GCC's -ggdb option to generate still more (GDB-specific) debugging information.


However, to work most effectively, this option requires that you have access to the source code for every library against which you link. While this can be very useful in certain situations, it can also be expensive in terms of disk space. In most cases, you can get by with the plain -g option.


Starting GDB To start a debugging session, simply type gdb progname, replacing progname with the name of the program you want to debug. Using a core file is optional but will enhance GDB's debugging capabilities. Of course, you'll need a program on which to try out GDB debugging, so Listing 29-5 provides one:
debugme.c.


A Buggy Program
/*
* debugme.c - poorly written program to debug
*/
#include <stdio.h>
#define BIGNUM 5000
#define SZ 100
void index_to_the_moon(int ary[]);
int main(int argc, char *argv[])
{
int intary[100];
index_to_the_moon(intary);
return 0;
}
void index_to_the_moon(int ary[])
{
int i;
for (i = 0; i < BIGNUM; ++i)
ary[i] = i;
}


 
© 2009