-->
Previous Table of Contents Next


gdb Basic Commands

The gdb supports many commands that enable you to perform different debugging operations. These commands range in complexity from very simple file-loading commands to complicated commands that allow you to examine the contents of the call stack. Table 26.1 describes the commands that you need to get up and debugging with gdb. To get a description of all of the gdb commands, refer to the gdb manual page.

Table 26.1. Basic gdb commands.

Command Description

file Loads the executable file that is to be debugged.
kill Terminates the program that you are currently debugging.
list Lists sections of the source code used to generate the executable file.
next Advances one line of source code in the current function, without stepping into other functions.
step Advances one line of source code in the current function and does step into other functions.
run Executes the program that is currently being debugged.
quit Terminates gdb.
watch Enables you to examine the value of a program variable whenever the value changes.
break Sets a breakpoint in the code; this causes the execution of the program to be suspended whenever this point is reached.
make Enables you to remake the executable program without quitting gdb or using another window.
shell Enables you to execute UNIX shell commands without leaving gdb.

The gdb environment supports many of the same command-editing features as do the UNIX shell programs. You can tell gdb to complete unique commands by pressing the Tab key just as you do when you are using bash or tcsh. If what you type is not unique, you can make gdb print a list of all the commands that match what you have entered so far by pressing the Tab key again. You can also scroll up and down through the commands that you have entered previously by pressing the up- and down-arrow keys.

Sample gdb Session

This section goes step by step through a sample gdb session. The sample program that is being debugged is quite simple, but it is sufficient to illustrate how gdb is typically used.

We will start by showing a listing of the program that is to be debugged. The program is called greeting and is supposed to display a simple greeting followed by the greeting printed in reverse order.


#include <stdio.h>



main ()

{

  char my_string[] = “hello there”;



  my_print (my_string);

  my_print2 (my_string);

}



void my_print (char *string)

{

  printf (“The string is %s\n”, string);

}



void my_print2 (char *string)

{

  char *string2;

  int size, i;



  size = strlen (string);

  string2 = (char *) malloc (size + 1);

  for (i = 0; i < size; i++)

    string2[size - i] = string[i];

  string2[size+1] = ’\0’;

  printf (“The string printed backward is %s\n”, string2);

}

To compile the preceding program, use the gcc command followed by the filename. To rename the generated binary (instead of using the default a.out filename), use the -o option followed by the binary name, such as


gcc -o test test.c

The program, when executed, displays the following output:


The string is hello there

The string printed backward is

The first line of output displays correctly, but the second line prints something that is unexpected. The second line of output was supposed to be


The string printed backward is ereht olleh

For some reason the my_print2 function is not working properly. Let’s take a look at the problem using gdb. First we need to start gdb, specifying the greeting program as the one to debug. Do this by entering the following command:


gdb greeting


Tip:  
Remember that you must compile the greeting program with the compiler debug options turned on.

If you forget to pass the program to debug as a parameter to gdb, load it in after gdb is started by using the file command at the gdb prompt:


(gdb) file greeting

This command loads the greeting executable just as if you had told gdb to load it on the command line.

Next, let’s run greeting by entering the gdb run command. When the program is executed from within gdb, the result should resemble the following:


(gdb) run

Starting program: /root/greeting

The string is hello there

The string printed backward is

Program exited with code 041

The output of the greeting program is the same as when we executed the program outside of gdb. The question is, why is the backward print not working? To find the problem, let’s set a breakpoint at the line after the for statement in the my_print2 function. To do this, list the source file by entering the list command three times at the gdb prompt:


(gdb) list

(gdb) list

(gdb) list


Tip:  
Pressing Enter by itself at the gdb prompt will repeat the last command that was entered.

Enter the list command for the first time and the output resembles the following:


1    #include <stdio.h>

2

3    main ()

4    {

5     char my_string[] = “hello there”;

6

7     my_print (my_string);

8     my_print2 (my_string);

9    }

10

Press Enter and gdb executes the list command again, displaying the following output:


11   my_print (char *string)

12   {

13    printf (“The string is %s\n”, string);

14   }

15

16   my_print2 (char *string)

17   {

18    char *string2;

19    int size, i;

20

Pressing Enter one more time lists the rest of the greeting program:


21    size = strlen (string);

22    string2 = (char *) malloc (size + 1);

23    for (i = 0; i < size; i++)

24      string2[size - i] = string[i];

25    string2[size+1] = ’\0’;

26    printf (“The string printed backward is %s\n”, string2);

26   }


Previous Table of Contents Next