Click here to Skip to main content
15,867,488 members
Articles / All Topics

Debugging with GDB

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Nov 2015CPOL2 min read 6.3K   1
This article attempts to summarize the basics involved in debugging with GDB, the GNU project debugger.

"Printf debugging" is the most straight forward way of figuring out what is happening under the hood, during run time of an C / C++ program. But this approach is a bit time consuming as the code has to be modified and recompiled according to the debugging scenario. A more flexible way is to use a separate debugger program. GDB: The GNU project debugger stands at the top of this list.

The general practice is to use GDB with an Integrated Development Environment as eclipse, which greatly simplifies the debugging process. However, to debug a program executing within a device which does not have a graphical user interface (for instance an embedded Linux system with a serial console), GDB on a console is a pretty useful option.

Debugging a simple C program

C++
#include <stdio.h>

 int main(int argc, char * argv[])
 {
    int i, j, sum;
    printf("Testing gdb...\n");
    
    i = 5;
    j = 4;
    sum = i + j;
    printf("%i + %i = %i\n", i, j, sum);
    return 0;
 }
  1. Compile the C program source (in this case gdb_test.c) with the "-g" GNU C compiler option to enable debugging.
    BAT
    user@user-virtual-machine:/tmp/gdb_test$ gcc -g gdb_test.c -o gdb_test
    
  2. Launch the executable file (gdb_test) on the target environment using gdb.

    The GDB will launch the selected program (gdb_test) and pause execution at the beginning. The "gdb" prompt will be displayed after the initial banner to allow user to enter the next debug instruction.

    BAT
    user@user-virtual-machine:/tmp/gdb_test$ gdb gdb_test
    GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
    Copyright (C) 2012 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i686-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /tmp/gdb_test/gdb_test...done.
    (gdb)
    
  3. Set a break point at a selected code line using the break command.
    BAT
    (gdb) break 6
    Breakpoint 1 at 0x804841d: file gdb_test.c, line 6.
    (gdb)
    
  4. Execute the program with the run command. GDB will pause execution when it reaches a break point.
    BAT
    (gdb) run
    Starting program: /tmp/gdb_test/gdb_test
    
    Breakpoint 1, main (argc=1, argv=0xbffff3f4) at gdb_test.c:6
    6    printf("Testing gdb...\n");
    (gdb)
    
  5. Use display command to select the variables to monitor.
    BAT
    (gdb) display i
    1: i = 0
    (gdb) display j
    2: j = 134513801
    (gdb) display sum
    3: sum = -1208209420
    (gdb)
    
  6. Step through the code using step command. The value of each selected variable will be displayed after each step.
    BAT
      (gdb) step
    Testing gdb...
    8    i = 5;
    3: sum = -1208209420
    2: j = 134513801
    1: i = 0
    (gdb) step
    9    j = 4;
    3: sum = -1208209420
    2: j = 134513801
    1: i = 5
    (gdb) step
    10    sum = i + j;
    3: sum = -1208209420
    2: j = 4
    1: i = 5
    (gdb) step
    11    printf("%i + %i = %i\n", i, j, sum);
    3: sum = 9
    2: j = 4
    1: i = 5
    (gdb) step
    5 + 4 = 9
    12    return 0;
    3: sum = 9
    2: j = 4
    1: i = 5
    
  7. To resume execution of the program, use continue command. Since there are no other break points, this program will execute until it reaches the end.
    BAT
    (gdb) continue
    Continuing.
    [Inferior 1 (process 3707) exited normally]
    (gdb)
    
  8. Use quit command to exit from the gdb console.
    BAT
    (gdb) quit
    user@user-virtual-machine:/tmp/gdb_test$
    

This article presents sufficient details to get started with command line debugging with GDB. However, GDB supports a number of additional commands to enable more sophisticated debugging scenarios, which are listed in the GDB documentation page, available here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer MTT Network (Pvt.) Ltd.
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDebugging with GDB Pin
SergeiV5-Nov-15 0:38
SergeiV5-Nov-15 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.