The following is a list of ideas on how to make use of the GNU debugger. Many of the same commands will work for the Solaris supplied debugger which is called "dbx". The dbx debugger is supplied with most UNIX systems. There are also other debuggers supplied with UNIX systems.
Program preparation: All files must be compiled and linked with the "-g" option. This preparation is true for all the debuggers on UNIX. Why don't people always compile with the -g option?
Invoking the debugger: The debugger may be run as a stand alone process by typing "gdb <program name>". The debugger may be run from emacs by typing "esc x" and then at the prompt "gdb". You will then be prompted for the name of your executable file. Emacs will divide into two windows. One from which you enter commands and the other containing the source code you are executing. This is by far the better way to run gdb. Note: you may also use gdb to analyze core files.
The following is a list of gdb commands that you might find useful. There are many other commands.
l - list
Lists the ten lines around the statement to be executed. A second l
will list the next 10 lines, etc.
b - break
Sets a break point. Conditional breakpoints are allowed. You can specify
a line number or a function name at which you would like execution to stop.
r - run
Runs the program. Command-line arguments are permitted.
s - single step
Allows the user to step statement by statement through the program.
(steps into functions).
n - single step
Allows the user to step statement by statement through the program.
(steps over functions).
c - continue
Continues execution from a break point.
j - jump
Allows the user to specify the next statement to be executed by its
line number.
ret - return from function.
Completes a function call. Execution stops at the return point.
p - display.
Displays the values of variables. Works for structures and you may
de-reference variables. There are options to display data in other bases
and to display a series of words. The format of the output and the number
of elements may be specified. e.g. "p /x duck@10", assuming that duck is
an "int", prints 10 words starting at duck in hexadecimal. See the
help option for more information.
where
Tells you where your are in the program.
q quit
Terminates the debugger.
Additional features:
attach
Allows the debugger to attach to a running process using its process
ID.
detach
Allows the debugger to releases a process.
shell
Executes the shell. ^d gets you back to the debugger.