10.2 Using gdb to debug your program

To use gdb to debug your program, you can start the debugger, and give it as an option the full name of your program:
gdb hello

Or, under dos:

gdb hello.exe

This starts the debugger, and the debugger immediately loads your program into memory, but it does not run the program yet. Instead, you are presented with the following (more or less) message, followed by the gdb prompt '(gdb)':

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.15.1 (i486-slackware-linux),  
Copyright 1995 Free Software Foundation, Inc...  
(gdb)

To start the program you can use the run command. You can optionally specify command-line parameters, which will then be fed to your program, for example:

(gdb) run -option -anotheroption needed_argument

If your program runs without problems, gdb will inform you of this, and return the exit code of your program. If the exit code was zero, then the message 'Program exited normally' is displayed.

If something went wrong (a segmentation fault or so), gdb will stop the execution of your program, and inform you of this with an appropriate message. You can then use the other gdb commands to see what happened. Alternatively, you can instruct gdb to stop at a certain point in your program, with the break command.

Here is a short list of gdb commands, which you are likely to need when debugging your program:

quit 
Exits the debugger.
kill 
Stops a running program.
help 
Gives help on all gdb commands.
le 
Loads a new program into the debugger.
directory 
Add a new directory to the search path for source les.

Remark: My copy of gdb needs '.' to be added explicitly to the search path, otherwise it doesn't nd the sources.

list 
Lists the program sources per 10 lines. As an option you can specify a line number or function name.
break 
Sets a breakpoint at a specied line or function
awatch 
Sets a watch-point for an expression. A watch-point stops execution of your program whenever the value of an expression is either read or written.

for more information, see the gdb users' guide, or use the 'help' function in gdb.

The appendix 15 contains a sample init le for gdb, which produces good results when debugging Free Pascal programs.

It is also possible to use RHIDE, a text-based IDE that uses gdb. There is a version of RHIDE available that can work together with FPC.