Introduction:
The Debugger GDB is a technique for finding anomalies--program bugs, it allows 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 way the debugger allows you to find bugs is by allowing you to run your program on a line-by-line basis, pausing the program at times or conditions that you specify and allowing you to examine variables, registers, the run time stack and other facets of program state while paused.Function Summary:
GDB can do four main kinds of things to help you catch bugs in the act:Starting GDB:
1. Start your program, specifying anything that might affect its behavior.
2. Make your program stop on specified conditions.
3. Examine what has happened when your program has stopped.
4. Change things in your program to allows you experiment with correcting the effects of one bug and go on to learn about another.
In order to use the GDB, your program must have been compiled with the -g option and the executable must be in current directory.Basic GDB commands:
- From the UNIX shell prompt just type: % gdb < target-program > where the < target- program > is the name of the executable that you want to debug. If you do not specify a target then gdb will start without a target and you will need to specify one later before you can do anything useful. Once started,GDB reads commands from the terminal until you tell it to exit.
- You can also start GDB with both an executable program and a core file specified(a core file is produced when a program crashed for certain reasons, it contains a snapshot of memory at the time of the crash), type: % gdb < program > < core >
- You can also invoke GDB from within emacs, you can use the command"M-x gdb" which will prompt you for the name of the target file. You can not start an inferior gdb session from within emacs without specifying a target. The emacs window will then split between the gdb window and a buffer containing the current source line.
help --ask for help (see below) where --display program stack up/down --navigate through the different program stack frames list --see the contents of the corresponding source file run < arglist > --start your program with arglist as its command line print < expr > --display the value of an expression break < function > --set breakpoint at function cont --continue running your program next --execute next line, including function calls step --execute until another line is reached finish --execute until the end of the current function quit --exit from GDBGetting help:
(gdb) help List of classes of commands: aliases -- Aliases of other commands breakpoints -- Making program stop at certain points data -- Examining data files -- Specifying and examining files internals -- Maintenance commands obscure -- Obscure features running -- Running the program stack -- Examining the stack status -- Status inquiries support -- Support facilities tracepoints -- Tracing of program execution without stopping the program user-defined-- User-defined commands Type "help" followed by a class name for a list of commands in that class. Type "help" followed by command name for full documentation. Command name abbreviations are allowed if unambiguous.