GDB Cheatsheet

TUI and Layouts

Use this GDB reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Turning It On

The TUI is a curses split screen: source or assembly above, the gdb prompt below. It needs a real terminal, so it fails under a pipe or the machine interface.

gdb -tui ./prog        # start in the TUI
gdb ./prog             # then type tui enable
(gdb) tui enable
(gdb) tui disable
(gdb) info win
        Name       Lines Columns Focus
        src           15      80 (has focus)
        status         1      80
        cmd            8      80
CommandEffect
tui enableactivate the TUI, restoring the last layout used
tui disableback to the plain console
Ctrl-x atoggle the TUI on and off, also Ctrl-x Ctrl-a and Ctrl-x A
info winlist visible windows, their sizes, and which has focus
tui refresh or refreshredraw everything, same as Ctrl-l

tui disable does not lose anything. Toggling out to read a long bt and back with Ctrl-x a is the normal rhythm, since scrollback in the cmd window is small.

Layouts

(gdb) layout src      # source and command
(gdb) layout asm      # disassembly and command
(gdb) layout split    # source, disassembly, and command
(gdb) layout regs     # adds registers to whichever of the above is active
(gdb) layout next
(gdb) layout prev
LayoutWindows
srcsource plus command
asmassembly plus command
splitsource, assembly, and command
regsregisters plus whatever src, asm, or split was showing
next / prevcycle through the layouts, built-in and user-defined
KeyEffect
Ctrl-x 1a one-window layout, source or assembly
Ctrl-x 2a layout with at least two windows, cycling
Ctrl-x omove focus to the next window
Ctrl-x stoggle SingleKey mode
Ctrl-lredraw

layout regs is a modifier, not a standalone layout. From src it shows registers plus source, from asm it shows registers plus disassembly, which is why the same command looks different depending on where you were.

(gdb) tui reg general      # which register group the regs window shows
(gdb) tui reg float
(gdb) tui reg vector
(gdb) tui reg next

Focus and Sizing

Only one window is active for scrolling at a time, and the arrow keys go to it. That is why the up arrow sometimes scrolls source instead of recalling a command.

(gdb) focus cmd        # arrows go back to command history
(gdb) focus src
(gdb) focus asm
(gdb) focus regs
(gdb) focus next
(gdb) winheight src +8
(gdb) winheight cmd -4
(gdb) tui window height src 20
(gdb) tui window width src +10
CommandEffect
focus NAMEmake that window active for scrolling, next and prev also work
winheight NAME +N / -Ngrow or shrink a window by N lines
tui window height NAME +Nthe long form of winheight
tui window width NAME +Nhorizontal resize, for side-by-side layouts
Ctrl-x orotate focus, the fast version

Windows must always fill the terminal, so a resize only works when a neighbor can give up or absorb the space. With focus src the arrow keys scroll source and the readline history is unreachable until you focus cmd or press Ctrl-x o.

The Redraw Gotcha

The TUI does not own the terminal exclusively. Anything the inferior writes to stdout or stderr lands on top of the curses windows and scrambles the display.

(gdb) refresh          # or Ctrl-l, repaint everything

Fixes in rough order of preference:

1. refresh or Ctrl-l after each burst of program output. 2. Redirect the program's output away from the terminal: run > /tmp/out 2>&1. 3. Run the inferior on a second terminal. Start tty in another window, note the device, then tty /dev/pts/5 in gdb before run. 4. Turn the TUI off while the noisy part runs, back on afterward.

(gdb) tty /dev/pts/5
(gdb) run

A resized terminal can also leave the layout stale. refresh fixes that, and if the screen is unrecoverable tui disable then tui enable rebuilds every window.

SingleKey Mode and Looks

Ctrl-x s binds bare letters to stepping commands, so you drive the debugger one keystroke at a time. Any other key drops you back to the prompt with that character already typed.

KeyCommandKeyCommand
ccontinueCreverse-continue
nnextNreverse-next
sstepSreverse-step
onexti, step OverOreverse-nexti
istepi, step IntoIreverse-stepi
ffinishFreverse-finish
uupddown
vinfo localswwhere
rrunqleave SingleKey mode
(gdb) set tui border-kind acs        # space, ascii, or acs
(gdb) set tui border-mode normal
(gdb) set tui active-border-mode bold-standout
(gdb) set tui compact-source on      # narrower line-number column
(gdb) set tui tab-width 4
(gdb) set tui mouse-events off       # let the terminal handle selection again

set tui mouse-events off is the one worth knowing. With mouse events on, gdb eats clicks and you cannot select text to copy out of the source window. Put the settings you like in ~/.gdbinit, along with tui enable if you always want it.