Sample Debugging

athena% cat stupid.c

#include <stdio.h>

int main()
{
  int x;

  printf( "Enter x: " );
  scanf( "%d", x );
}
athena% gcc -g stupid.c -o stupid
athena% gdb stupid
GDB is free software and you are welcome to 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.
GDB 4.14 (mips-sgi-irix5.2), Copyright 1995 Free Software Foundation, Inc...
(gdb) run
Starting program: /afs/athena.mit.edu/user/t/h/thomasc/Public/prog/stupid
Enter x: 7

Program received signal SIGSEGV, Segmentation fault.
0xfac84f4 in number () at doscan.c:422
doscan.c:422: No such file or directory.
(gdb) where
#0  0xfac84f4 in number () at doscan.c:422
#1  0xfac5d18 in _doscan () at doscan.c:226
#2  0xfae6524 in scanf () at scanf.c:63
#3  0x400934 in main () at stupid.c:9
(gdb) up
#1  0xfac5d18 in _doscan () at doscan.c:226
doscan.c:226: No such file or directory.
(gdb) up
#2  0xfae6524 in scanf () at scanf.c:63
scanf.c:63: No such file or directory.
(gdb) up
up
#3  0x400934 in main () at stupid.c:9
9         scanf( "%d", x );
(gdb) list
4       int main()
5       {
6         int x;
7
8         printf( "Enter x: " );
9         scanf( "%d", x );
10      }
(gdb) print x
$1 = 263561776
(gdb) print &x
$2 = (int *) 0x7fffaef0
(gdb) quit

Next Slide