The purpose of a debugger such as GDB is to allow you to see what is going on ``inside'' another program while it executes-or what another program was doing at the moment it crashed.The second point in the excerpt aboveis a good one, and probably the best reason to learn GDB. Some of you may like to debug using print statements rather than stepping through a debugger-- perhaps you are used to this mode of developing. However you can also use GDB to see what happened when your program crashed and where it was executing when it did crash. You probably have seen the dreaded:
% myprogramIn this situation, a Unix debugger is worth a thousand printf's.
Segmentation Fault
(core dumped)
% gdb myprogramThis will give you a welcome message and get you to a GDB prompt:
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.16 (alpha-dec-osf3.2), Copyright 1996 Free Software Foundation, Inc... Reading symbols from /usr/shlib/libm.so...done. Reading symbols from /usr/shlib/libc.so...done. (gdb)
% gdb myprogram core
orcas% gdb piglatin core 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.16 (alpha-dec-osf3.2), Copyright 1996 Free Software Foundation, Inc... Core was generated by `piglatin'. Program terminated with signal 11, Segmentation fault. Reading symbols from /usr/shlib/libm.so...done. Reading symbols from /usr/shlib/libc.so...done. #0 0x3ff800c7800 in NLstrlen () (gdb) where #0 0x3ff800c7800 in NLstrlen () #1 0x120009088 in Rotate (string=0x11ffff728 "buggy") at piglatin.cpp:17 #2 0x120009188 in IgPay (ingstray=0x11ffff728 "buggy", string=0x11ffff720 "buggy") at piglatin.cpp:26 #3 0x120009498 in main () at piglatin.cpp:43 (gdb) up #1 0x120009088 in Rotate (string=0x11ffff728 "buggy") at piglatin.cpp:17 17 for (i=0; i < strlen(*string)-1; i++) { (gdb) list 12 } 13 14 void Rotate(char *string) 15 { 16 int i; 17 for (i=0; i < strlen(*string)-1; i++) { 18 SwapChars(string, i, i+1); 19 } 20 } 21 (gdb) quit orcas% emacs piglatin.cpp orcas% g++ -g -o piglatin piglatin.cpp . . . orcas% piglatin buggy buggy uggy uggy uggy uggy uggy uggy orcas% gdb piglatin 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.16 (alpha-dec-osf3.2), Copyright 1996 Free Software Foundation, Inc... (gdb) break Rotate Breakpoint 1 at 0x120009060: file piglatin.cpp, line 17. (gdb) run Starting program: /Sucia/u8/fix/326/sample/piglatin buggy buggy Breakpoint 1, Rotate (string=0x11ffff5a8 "buggy") at piglatin.cpp:17 17 for (i=0; i < strlen(string); i++) { (gdb) print string $1 = 0x11ffff5a8 "buggy" (gdb) display i 1: i = 1073741880 (gdb) display string 2: string = 0x11ffff5a8 "buggy" (gdb) step 18 SwapChars(string, i, i+1); 2: string = 0x11ffff5a8 "buggy" 1: i = 0 (gdb) step SwapChars (string=0x11ffff5a8 "buggy", i=0, j=1) at piglatin.cpp:9 9 temp = string[i]; (gdb) finish Run till exit from #0 SwapChars (string=0x11ffff5a8 "buggy", i=0, j=1) at piglatin.cpp:9 Rotate (string=0x11ffff5a8 "ubggy") at piglatin.cpp:19 19 } 2: string = 0x11ffff5a8 "ubggy" 1: i = 0 (gdb) next 18 SwapChars(string, i, i+1); 2: string = 0x11ffff5a8 "ubggy" 1: i = 1 (gdb) next 19 } 2: string = 0x11ffff5a8 "ugbgy" 1: i = 1 (gdb) next 18 SwapChars(string, i, i+1); 2: string = 0x11ffff5a8 "ugbgy" 1: i = 2 (gdb) . . . (gdb) 18 SwapChars(string, i, i+1); 2: string = 0x11ffff5a8 "uggyb" 1: i = 4 (gdb) n 19 } 2: string = 0x11ffff5a8 "uggy" 1: i = 4 (gdb) continue Continuing. uggy uggy uggy uggy uggy uggy Program exited normally. (gdb) quit orcas% g++ -g -o piglatin piglatin.cpp . . . orcas% piglatin buggy buggy uggyb uggyb uggyba uggybay uggybay uggybay orcas% exit