To use gdb to debug your program, you can start the debugger, and give it as an option the full name of your program:
Or, under dos:
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)’:
The actual prompt will vary depending on your operating system and installed version of gdb, of course.
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:
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 such), 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:
Remark: My copy of gdb needs ’.’ to be added explicitly to the search path, otherwise it doesn’t find the sources.
In appendix E a sample init file for gdb is presented. It produces good results when debugging Free Pascal programs.
For more information, refer to the gdb User Manual, or use the ’help’ function in gdb.
The text mode IDE and Lazarus both use GDB as a debugging backend. It may be preferable to use that, as they hide much of the details of the debugger in an easy-to-use user interface.