Logo  

CS351 - Computer Organization

GDB - The GNU Debugger

GDB TUI mode (Text User Interface)

The TUI mode represents a nice curses interface to gdb.

Compile your programs with:

Language Options
GNU C gcc -ggdb ...
clang clang -gdwarf ...
nasm nasm -g -F dwarf -felf64 prog.s ...
ld -o prog prog.o

Then issue the command: gdb prog

Then inside of GDB issue the following commands:

tui enable
tui reg general
break _start
run [<params>]

Then use 'step', 'next', 'si', 'ni', etc as normal, but enjoy the curses window. The source window should be the selected one, which will respond to arrow keys and scroll wheel events to move around. The enter key will repeat the last command run over and over.

To direct the output of the program to a different terminal window (because you will not see the output because of the curses nature of the tui window,) use "tty" in the terminal you want to direct the output to, then in the gdb window use "tty ", such as "tty /dev/pts/4" for example, then "run" the program (re-run it if using debug.)

gdb program

-p pid - Debug the already running program with PID 'pid'
-c core - Use the core file as the processes memory.

Use:

unlimit coredump (tcsh)
ulimit -c unlimited (bash)

to enable core-dumps.

Common commands:

Command What it does
run args... Runs the program
c Continue the program
next Step the program (skips function calls)
step Step one source line (descends into function calls)
list [file:]func List the source where the program is stopped at
break location Set a break-point at location
catch event Catch an event such as "fork", "signal", etc.
watch expr Break whenever a data location changes.
awatch expr Break whenever a data location is read/written.
print expr Print the value of expr
where/bt Print a stack-trace of where the program is currently
up Go up the previous stack frame
down Go down a stack frame
help GDB help
quit Exit gdb

Assembly debugging:

Command What it does
info registers
i r
Dumps all the registers and their values
info frame
i f
Dump information about the current stack frame
print $reg
p $reg
Print a specific register and its value
print *(char **)($rsp+8) Can apply C type to a dereferenced address.
display /3i $pc Display 3 instructions relative to the program counter when stepping. You probably always want to do this before stepping
ni Go to next instruction (stepping over calls)
si Step to next instruction (stepping into calls)
x /8g $rsp Examine 8 (giant (8-byte) words) starting at $rsp

TUI keybindings

Key What it does
Ctrl-X 1 Use TUI mode w/ only one window
Ctrl-X 2 Use TUI mode w/ at least two windows
Ctrl-X o Change the active output window
Ctrl-X a Exit TUI mode
Ctrl-X s Switch in/out of TUI mode
PgUp / PgDown Scroll active window one page up/down
Up/Down/Left/Right Scroll active window one line/column
Ctrl-L Refresh the display