gcc -g -O0 main.c
gdb a.out
gcc -g -O3 main.c
gdb --args a.out arg1 arg2
- Getting to the Shell
- Ctrl+C pauses execution and escapes to the shell
continue
resumes program executionrun <args>
reruns the program
- Figuring out commands
help
browses documentation.apropos <search_term>
searches for commands relating to your search term- Hitting tab after typing a partial command or argument expands to a list of possible commands or arguments
- Saving keystrokes
- Hitting enter re-runs the last command
- GDB only requires only as many characters as are needed to uniquely identify a command.
- This reference shows the terse forms of commands
i va <regex>
- list global and static variablesi fu <regex>
- lists functions
p <variable_name>
- get value of variablep array[0]
- show element in array (or array decayed to pointer)p my_ptr
- shows address pointer points top *my_ptr
- shows contents of the address pointed top *my_ptr@len
- show array that has decayed into a pointer
So for this example:
size_t my_array_len = 10;
int *my_array = (int *) malloc(my_array_len * sizeof(int));
You would enter:
p *my_array@10
wha <variable_name>
- get type of variablept <type_name>
- inspect layout of complex types like structs
Note: To view structs cleanly, run set print pretty
b
- List Breakpointsb <function_name>|<filename:line_number>|*<address>
- Set breakpointscle <function_name>|<filename:line_number>|*<address>
- Clear breakpoint
Examples
b *0x7ffff70871c0
b printf
b printf.c:20
bt
- Display a stack tracefr <n>
- Select a frame in the stack trace. Selected frames can have local variables targetted byprint
(p
).up
anddown
- Select a frame up or down the stack.i frame
- Display info on the selected framei args
- Display arguments to the selected framei lo
- Display local variables in the selected framei reg
- Display registers as of the selected frame
next <n>
- step forward n lines in the current functionstep
- step into a function
- Useful if using
next
andstep
to follow control flow: - ctrl-x ctrl-a to show
- ctrl-x ctrl-a to hide
- PGUP and PGDOWN to navigate
watch <variable_name>
- Break on change to a variable
When the variable changes, a breakpoint fires, and the old and new state of the variable is printed
info threads
- show all threadsthread <thread_no>
- switch debugger to a specific thread
- Run
make qemu-nox-gdb
. If you get an error about missing target for.gdbinit.tmpl
, copy this file into your xv6 directory - Then in a new tab run
gdb kernel
. - Once gdb starts, run
source .gdbinit
- Setup any breakpoints you want. When ready to boot xv6, run
continue
- The xv6 shell is locked when the GDB console is active and visa versa.
- To stop xv6 execution and open the GDB console, hit
ctrl+c
- Inspect state and set breakpoints
- To resume xv6 execution, enter
continue
- Repeat steps 4-7 until debugging complete
- Quit GDB by entering
quit
at the GDB console. - To quit the qemu session running xv6 by selecting the xv6 terminal and entering
ctrl+a
followed by x
By default, GDB is configured to debug the xv6 kernel. The process for debugging programs that run on the xv6 shell is a bit more involved. Here is an example with the program cat
. Be sure that the Makefile is configured to add the debugging symbols to these programs.
- Run
make qemu-nox-gdb
. If you get an error about missing target for.gdbinit.tmpl
, copy this file into your xv6 directory - Then in a new tab run
gbd kernel
- Once gdb starts, run
source .gdbinit
- When ready to boot xv6, run
continue
- The xv6 shell is locked when the GDB console is active and visa versa.
- To stop xv6 execution and open the GDB console, hit
ctrl+c
- Load the debugging symbol for the userspace program that you want to debug. Generally these are prefixed with an underscore, so to debug
cat
, you enterfile _cat
- Set a breakpoint on cat,
b cat
- Resume xv6 execution, enter
continue
- Execute
cat
in the xv6 shell - The breakpoint you set should have been caught, throwing you to the GDB shell. Perform debugging as needed.
- Repeat steps 7-10 as needed
- When done with cat, you can resume debugging the kernel by entering
file kernel