


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



gdb - An Introduction
Debugging is programming. You will spend more of your time debugging than actually writing code. Even very experienced programmers spend a significant time debugging. Even if you write rock solid code, chances are you are going to be using libraries and system calls written by someone else. So, you can also end up debugging problems surrounding the interfaces between your code and other code. The GNU debugger (gdb) is a very popular command-line tool for stepping through and analyzing C code. It can also be used with other languages such as C++, Java, and assembly. The basic idea is that you first start gdb by giving it the name of the program (executable) that you want to debug. Once inside gdb, you can set breakpoints at arbitrary locations in the source code. Then you can run your program until the code hits a breakpoint or until the program causes an exception (like a segmentation violation, also known as dereferencing a bad pointer). The gdb program is most useful in finding out how a program is executing by watching the program flow and by inspecting the values of variables at run time. Although gdb is very sophisticated, you only need to learn a small subset of the commands in order to make it useful. In linux, there are some GUI debuggers based on gdb, such as ddd. To get more information on gdb type info gdb.
gdb - Summary
gdb command summary
Preparing a program for debugging In order to debug a program with gdb you need to compile your source code (all source modules that you want to debug) with the -g option:
gcc -g -o foo foo.c
Starting gdb Simply start gdb with the name of your executable:
$ gcc -g -o foo foo.c $ gdb foo GNU gdb 5.1. Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-suse-linux"... (gdb)
To start your program within gdb, simple use the “r” command (for run):
(gdb) r
Optionally, you can put command arguments that you want to send to your program after the “r” command:
(gdb) r arg1 arg2 ... argN
Example 1 - setting breakpoints and examining variables Here is a program that is supposed to add 2 to the variable j in every iteration of the for-loop:
#include <stdio.h>
int main(int argc, char argv[]) { int i, j = 0; for (i = 0; i < 100; i++); / <-- unwanted semicolon */ j += 2; printf("The value of j is: %d\n", j); return 0; }
Compiling and running the program reveals that it is not adding 2 to j 100 times:
$ gcc -g -o ex1 ex1.c $ ./ex The value of j is: 2
Because we compiled ex1.c with the -g option we can run gdb on the ex1 executable.
After executing the for-loop, j should be 200. It seems that j is not getting incremented. Close inspection of the for-loop reveals that we have a semi-colon where we do not want one. If we remove the semi-colon, u with get incremented properly. To end the gdb session we can use the quit command:
(gdb) q The program is running. Exit anyway? (y or n) y
Example 2 - finding a segmentation fault You can also use gdb to find the location of a segfault. Consider the following program:
#include<stdio.h>
int main(int argc, char *argv[]) { int a[10]; int *a1;
a1 = a;
a1 = 0;
a1[0] = 1;
return 0; }
Let’s run it under gdb:
[2785]orb: gdb segfault GNU gdb 5.1. Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-suse-linux"... (gdb) r Starting program: /home/benson/tch/usf/cs326/2002F/handouts/03-gdb/src/segfault
Program received signal SIGSEGV, Segmentation fault. 0x08048426 in main (argc=1, argv=0x3ffff174) at segfault.c: 13 a1[0] = 1; (gdb)
Notice how gdb shows exactly where the segfault occurs.