Note | |
---|---|
To use the GNU Debugger (GDB) effectively, you need to direct the compiler to include debugging information when it compiles your program. You can do this by using either the |
To debug a program using the GNU Debugger (GDB), type gdb program_to_debug
(such as gdb lab2
) at the prompt. You can then use GDB’s commands, some of which are listed in Table 6.2, “Commonly used GDB commands, Part 1” and Table 6.3, “Commonly used GDB commands, Part 2” below, to debug your program. GDB has many more commands than what is listed here; consult GDB’s built-in help for a complete listing. Also note that many commands have shortened versions: for example, you can type r in place of run. As with the shell (discussed in Section 3.1, “Features of the shell”), command history and command completion are available.
Finally, the command x/ (listing the contents of memory) is a particularly powerful command, but it's a fairly complex one, and describing it in detail is beyond the scope of this guide. Therefore, consulting help x while in GDB is recommended.
References to further reading on GDB can be found in Section A.5.4, “More on GDB”.
Table 6.2. Commonly used GDB commands, Part 1
Category | Command | Action |
---|---|---|
Basic GDB Commands | --- | |
~ |
| GDB's built-in help |
~ |
| Quit GDB |
Controlling Program Flow | --- | |
~ |
| Continue running the program (can use when a breakpoint is reached) |
~ |
| Run the program being debugged [with the supplied command line arguments (args )] |
~ |
| Step through program, over subroutines [repeat for specified number of times] |
~ |
| Step through by one instruction [repeat for specified number of times] |
~ |
| Step through program, into subroutines [repeat for specified number of times] |
~ |
| Step by one instruction exactly [repeat for specified number of times] |
~ |
| Halt a program that's currently running in GDB |
Table 6.3. Commonly used GDB commands, Part 2
Category | Command | Action |
---|---|---|
Breakpoints and Watchpoints | --- | |
~ |
| Set a breakpoint at section |
~ |
| Delete all breakpoints [or just those specified, listed by breakpoint number] |
~ |
| Set a watchpoint for expression |
Displaying Information | --- | |
~ |
| Disassemble function in current frame [the given function instead] |
~ |
| Display the state of the current stack frame |
~ |
| Display the contents of the CPU registers |
~ |
| Show 10 lines of source code centered around the currently executing line [centered around the start of function ] |
~ |
| Display the value of variable [the memory address of variable ] |
~ |
| Display 16 words of data from memory starting at the provided address |