CodeProject
"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
#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;
}
- Compile the C program source (in this case gdb_test.c) with the "
-g
" GNU C compiler option to enable debugging.
user@user-virtual-machine:/tmp/gdb_test$ gcc -g gdb_test.c -o gdb_test
- 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.
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)
- Set a break point at a selected code line using the
break
command.
(gdb) break 6
Breakpoint 1 at 0x804841d: file gdb_test.c, line 6.
(gdb)
- Execute the program with the
run
command. GDB will pause execution when it reaches a break point.
(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)
- Use
display
command to select the variables to monitor.
(gdb) display i
1: i = 0
(gdb) display j
2: j = 134513801
(gdb) display sum
3: sum = -1208209420
(gdb)
- Step through the code using
step
command. The value of each selected variable will be displayed after each step.
(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
- 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.
(gdb) continue
Continuing.
[Inferior 1 (process 3707) exited normally]
(gdb)
- Use
quit
command to exit from the gdb console.
(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.