UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

The Basics

In order to use the debugger with your source code, you must first compile it with the -g flag. linux3[104] % gcc -g -Wall -ansi example1.c linux3[105] % a.out Beginning: ******* * * * * *******

The -g flag tells the compiler to leave information about your code in the object (.o) and executable files. The information allows gdb and other debuggers to show your code the way you're used to seeing it. If the code compiles cleanly, you can then run gdb, using the name of your executable as an argument:

linux3[106] % gdb a.out GNU gdb 19991004 Copyright 1998 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-redhat-linux"... (gdb) The (gdb) prompt tells you that you are in the debugger. First, let's list the source code. With no arguments, the list command will list 10 lines of code, beginning at the prompt. You can specify ranges with a comma. (gdb) list 1,26 1 2 #include <stdio.h> 3 4 int main() 5 { 6 int i; 7 8 for (i = 0; i < 5; ++i) 9 { 10 switch (i % 5) 11 { 12 case 2: 13 case 3: 14 printf("* *\n"); 15 break; 16 case 1: 17 case 4: 18 printf(" *******\n "); 19 break; 20 default: 21 printf(" Beginning:\n "); 22 } 23 } 24 25 return 0; 26 } (gdb) As you can see, the lines are numbered.


CSEE | 201 | 201 S'02 | lectures | news | help