GDB tutorial

This is a short tutorial to get you acquainted with "gdb" and its command-line interface. You are encouraged to use the gdb reference card for more details.
Commands reviewed: run, continue, next, break, delete, print, display, step, quit

  • Copy the files author.c, author.h, gdbexample.c from General Resources/GDB/gdbtutorialexamples.rar and save in a folder on your Ubuntu desktop.
     
  • Examine the contents of the files quickly to get an idea of what the program is doing.
    - In author.h, you will find the definition of a structure and the function prototype of a related function.
    - In author.c, you will find the definition of the function declared in author.h
    - In gdbexample.c, you will find a main function that uses the definitions and declarations in the author.{c,h} files.
    - In makefile, you have the contents of a makefile (which will be described later).
     
  • To compile the programs:

     gcc gdbexample.c -c -g
     gcc author.c -c -g
     gcc author.o gdbexample.o -o test -g


    Notes:
    1. The header file is not listed as it has been included in the C source files.
    2. The executable will be called "test".
    3. The "g" flags instructs gcc to provide information for debugging.
     
  • To compile the programs, using the makefile:

    make
     
  • To invoke gdb, 

    gdb test           //gdb <executable-name>

     
  • To begin debugging, set a breakpoint. breakpoints can be set at line numbers of function names. In the following steps we will set three breakpoints - at main, at a line in the function display_author_details in source file author.c and at the function display_author_details itself.

    break main                            //break <function-name>

    b author.c:6                           //b <line-number>. Note the default is the program that contains main(). Use the <file-name>:<line-number> for other files.
                                              
    break display_author_details //Since the function is defined only in one file, you don't have to specify the file name.
     
  • To display all current breakpoints
  • info break
     

  • To delete a breakpoint:

    delete 2            //delete <breakpoint-number-obtained-from-list>
     
  • To begin running the program

    run 2                        
    //run <list-of-command-line-arguments>

    The program will run until the first breakpoint (in our case "main").
     
  • The line of code displayed will be the line that will be executed next. To go to the next line use "next" or "n".

    next                     /
    /Step to the next statement; also skip over a user-defined function.

    n
     
  •  Keep moving until you get to the line 30 in gdbexample.c. This is the statement: int count = atoi(argv[1]);
    To display the value of count after the statement has executed, use "display".

    display count                //display <variable-name>

    When you type next again, you should see the value of count change. Note that using "display" allows you to continuously watch the variable, while it is in scope.
     

  • To undisplay a varaible, use "undisplay".

    undisplay 1               //undisplay <display-value-number>
     
  • To look at some pointers and their values, let's use the "print" command. This will also show us the values of the variables, however, only for the current state of the program.

     print &one                     //display the address of "one".
     
    display ptr                     //display value of "ptr" which will be some random address

    print *ptr                 //display the value pointed to by "ptr" which will be the random values currently at the random address.

  • Execute the following sequence of statements:

     display one              //display the value of "one".

     next                        //Do this until you have executed the strcpy(one.book_name[2],"Harry Potter and the Prisoner of Azkaban");

    You should notice the value of "one" change as its fields got assigned value.
     
  • Now, use "display" to examine the address of "one". Also, use "display" to examine the value pointed to by "ptr". What do you observe? How would display the value of "one.name"? How would use display the same value using "ptr"?
     
  • To skip the next few lines of code and get to the next breakpoint use "continue" or "c".

    continue           //Go to the next breakpoint or end of program if no more breakpoints.

    This will probably take you to the function display_author_details. Feel free to test some of the commands you have just learned.
     
  • To enter a function when moving from one statement to next, use "step" instead of "next".

    step             //Step into the user-defined function in the next statement.
     
  • Whenever you are done, type "quit".

    quit                  //quit the execution of the program and gdb
     
  •