




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!





In this lab you will learn how to use GDB for debugging C programs. GDB is useful for debugging normal applications as well as kernel code. A little investment in learning the basics of GDB will save you a lot of time later.
Debugging is programming. You will spend more of your time debugging than actually writing code. Even very experienced programmers spend a significant time debugging. Furthermore, 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++ and assembly. For our purposes, we will also use gdb to debug the Pintos operating system. Pintos is setup to allow for remote debugging of the Pintos kernel. Using gdb with Pintos will be invaluable for the remaining projects. The basic idea is that you first start gdb by giving it the name of the program (executable) that you want to debug (debugging a kernel will be a little bit different). 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.
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
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, simply 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
Here is a program that is supposed to add 2 to the variable j in every iteration of the for-loop:
The n command executes the next statement in the program:
(gdb) n 6 for (i = 0; i < 100; i++); /* <-- unwanted semicolon */ (gdb) n 7 j += 2; (gdb)
Note that the line displayed after typing n is the next line of the program to be executed. Up to this point we have executed the for-loop, but have not executed j += 2. Now we can use the print command to view the values of i and j:
(gdb) p i $1 = 100 (gdb) p j $2 = 0
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, j will 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
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.
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 "powerpc-apple-darwin" ...Reading symbols for shared libraries ... done
(gdb) l warning: Source file is more recent than executable.
15 { 16 printf("Name: %s, Age: %d, Weight: %d\n", 17 fp->name, fp->age, fp->weight); 18 } 19 20 int main(int argc, char *argv[]) 21 { 22 struct foo *foop; 23 24 foop = (struct foo *) &memory[0]; (gdb) l 25 26 strcpy(foop->name, "Greg"); 27 foop->age = 29; 28 foop->weight = 150; 29 30 foop = (struct foo *) &memory[(sizeof(struct foo))]; 31 32 strcpy(foop->name, "Tony"); 33 foop->age = 20; 34 foop->weight = 200; (gdb) l 35 36 print_foo((struct foo *) &memory[0]); 37 print_foo((struct foo *) &memory[(sizeof(struct foo))]); 38 39 return 0; 40 } (gdb) b 36 Breakpoint 1 at 0x2ac8: file memory.c, line 36. (gdb) r Starting program: /Users/benson/Share/Teaching/cs326/handouts/01-gdb/src/memory Reading symbols for shared libraries. done
Breakpoint 1, main (argc=1, argv=0xbffff740) at memory.c: 36 print_foo((struct foo *) &memory[0]); (gdb) print *((struct foo *) &memory[0]) $1 = { name = "Greg", ’\0’ <repeats 27 times>, age = 29,
weight = 150 } (gdb) print sizeof(struct foo) $2 = 40 (gdb) print *((struct foo *) &memory[40]) $3 = { name = "Tony", ’\0’ <repeats 27 times>, age = 20, weight = 200 } (gdb) x/100c memory 0x30d0