6.3. Debugging programs

Compiler Complaint 

Compiler Complaint

[Note]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 -g or the -ggdb compiler flag, as mentioned in Table 6.1, “Commonly used GCC flags”.

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

CategoryCommandAction
Basic GDB Commands---
~

help (h)

GDB's built-in help
~

quit (q)

Quit GDB
Controlling Program Flow---
~

continue (c)

Continue running the program (can use when a breakpoint is reached)
~

run (r) [args]

Run the program being debugged [with the supplied command line arguments (args)]
~

next (n) [#]

Step through program, over subroutines [repeat for specified number of times]
~

nexti (ni) [#]

Step through by one instruction [repeat for specified number of times]
~

step (s) [#]

Step through program, into subroutines [repeat for specified number of times]
~

stepi (si) [#]

Step by one instruction exactly [repeat for specified number of times]
~

kill (k)

Halt a program that's currently running in GDB

Table 6.3. Commonly used GDB commands, Part 2

CategoryCommandAction
Breakpoints and Watchpoints---
~

break (b) section

Set a breakpoint at section
~

delete [num_list]

Delete all breakpoints [or just those specified, listed by breakpoint number]
~

watch [expression]

Set a watchpoint for expression
Displaying Information---
~

disas [function]

Disassemble function in current frame [the given function instead]
~

info (i) frame

Display the state of the current stack frame
~

info (i) registers

Display the contents of the CPU registers
~

list (l) [function]

Show 10 lines of source code centered around the currently executing line [centered around the start of function]
~

print (p) [&]variable

Display the value of variable [the memory address of variable]
~

x/16 address

Display 16 words of data from memory starting at the provided address


Back to Guide main page