Using GDB

a brief introduction


Executive Summary: This document is designed to help you get started using the Gnu debugger. It contains a trace of a debugging session using gdb, as well as a list of some of the more basic commands you will need to use to debug your own programs.

What is GDB?

GDB is a portable debugger for Unix executables. GDB is a debugger that will help you to debug programs from various languages, including the C++ source that you are creating in this course. Don't let the term "debugger" fool you: GDB will not debug your programs for you. However it can be a great tool to help find bugs. As the man page says:
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:
% myprogram
Segmentation Fault
(core dumped)
In this situation, a Unix debugger is worth a thousand printf's.

Invoking GDB

GDB is invoked from the shell prompt and executes within the terminal. There are X-windows versions of GDB (xgdb) publicly available but I don't think they are supported by the CS lab. Suppose you have compiled an executable myprogram and want to debug it. You would invoke GDB with the command:
% gdb myprogram
This 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)

Invoking GDB with a core file

When a program crashes in Unix, it dumps a core file. The core file essentially contains a snapshot of a program's execution state when the crash occurred. GDB can be invoked in such a way that that program state is restored in the debugger. This allows you to, say, examine the call stack or the state of your variables so hopefully you can find out what went wrong. To invoke GDB with the core image, type:
% gdb myprogram core

Commonly used GDB commands

Still under construction. Type help in GDB for a list of features.

Transcript of a debugging session

Also still under construction. I've written a sample program that attempts to convert words to Pig Latin, but has a few bugs in it.
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