Skip to content
systemcoding.dev
Debugging

Use LLDB's Built-In Curses Interface With gui

Type gui in LLDB to open a full-screen terminal interface with source, variables and threads windows, and learn the keys that step, resume and quit it.

7 min read
Summarize in AI
Flat illustration of a debugger stopped at a breakpoint beside the register values, with the LLVM, Arm and GNU logos.

LLDB's command line prints a stop report and waits. That is fine for short sessions, and tiring for a long one where you want to see the source, the locals and the call stack all at once. GDB has its TUI for that. LLDB has a curses interface of its own, opened with a single command, gui. It draws a full-screen terminal application with a source window, a variables window, a threads window and a status line, and it stays in step with the process as you step. It works over SSH, needs no extra packages, and is entirely driven by the keyboard. This post covers how to open it, the windows and what they show, the keys, how to give the program its own terminal and where the interface is thin compared with an IDE. LLDB 21 on x86-64 Linux, with Clang 21, is the reference; the interface is the same on macOS in the Terminal app.

Opening it

Start a session as usual, set a breakpoint, run until it hits, and type gui at the prompt.

Shell
clang -g -O0 -Wall -Wextra -o sum_g sum.c
lldb --no-lldbinit ./sum_g
Text
(lldb) breakpoint set --name sum
Breakpoint 1: where = sum_g`sum + 11 at sum.c:5:9, address = 0x0000000000001134
(lldb) run
Process 41990 launched: '/home/dev/sum_g' (x86_64)
Process 41990 stopped
* thread #1, name = 'sum_g', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555134 sum_g`sum(v=0x00007fffffffe0e0, n=6) at sum.c:5:9
(lldb) gui

The program is the familiar sum.c from earlier posts, summing six numbers to 108. The terminal switches to full-screen. gui works on a live process or a target that has not started; with no process the source window shows the file around main. If the terminal is too small, LLDB declines with an error; resize it to at least about 80 columns by 24 rows, and larger is better.

The windows

The screen is divided into panes. The text below is an approximation of the layout, since a curses screen is a picture, not a transcript.

Text
+- Sources ---------------------------------------------------------+
|  3   int sum(const int *v, int n)                                  |
|  4   {                                                             |
|> 5       int total = 0;                                            |
|  6       for (int i = 0; i < n; i++)                               |
|  7           total += v[i];                                        |
|  8       return total;                                             |
+- Variables --------------------+- Threads ------------------------+
| (const int *) v = 0x7fffffffe0e0| thread #1: tid = 41990           |
| (int) n = 6                    |   frame #0: sum(v=..., n=6)       |
| (int) total = 0                |   frame #1: main                  |
| (int) i = 21845                |                                   |
+--------------------------------+-----------------------------------+
| Process 41990 stopped                                              |
+--------------------------------------------------------------------+

The Sources window shows the file around the current line, with an arrow at the line that will run next and a marker on lines with breakpoints. The Variables window shows the arguments and locals of the selected frame, with values updated at every stop, and a value that changed since the previous stop is highlighted so you can see what a line did. The Threads window lists threads and the selected thread's stack, where you can pick a frame. At the bottom is a status line, and a Help window can be called up over the others. i here is stack garbage until its line runs, as in any -O0 session.

Which window has focus decides what the arrow keys do. The focused window has a highlighted title.

Keys

The keys are single letters or special keys and are the way to learn the interface fastest, since pressing h shows its own list.

KeyAction
hshow or hide the help window
Tabmove focus to the next window
Arrow keys, Page Up, Page Downscroll or move the cursor in the focused window
Enterin Variables, expand or collapse a struct; in Threads, select the frame
ccontinue
nstep over (next)
sstep into
ostep out (finish)
ddetach from the process
kkill the process
btoggle a breakpoint on the cursor line (in Sources)
Ddisable or enable the breakpoint on the cursor line
Escleave the GUI and return to the command line
Ctrl-Cinterrupt the running process

If Esc does nothing in your build, the menu bar opened with F1 has an exit entry under the LLDB menu. The exact set of keys is printed by the help window and is the authority; the table lists the ones that matter and is current for LLDB 21. A key that is not bound does nothing, so a mistake is harmless. In the Variables window, Enter on a struct expands its fields, which gives you a navigable tree of the data without typing any frame variable command.

A short walk-through: with the focus on Sources, press n four times and watch the arrow move down while total changes in the Variables window as lines execute. Press s on a line with a call to enter it, o to come back out. Press Tab until the Variables window is focused, scroll to a struct and press Enter to open it. Press b on a source line to set a breakpoint, c to run to it. Press Esc and the normal prompt returns, with the process still alive and stopped where you left it.

The command line is still there

Esc takes you back to (lldb), and you can type any command and then enter gui again; the state is shared. That mixed use is common: use the GUI to move and look, drop to the command line for an expression, a watchpoint set or a memory read, and return. The program is not restarted by entering or leaving.

The program's output

The program's standard output goes to the same terminal LLDB is drawing on, so a printf can corrupt the screen. Two ways around it. Run the program with its output redirected, in the session before you start the GUI.

Text
(lldb) settings set target.output-path /tmp/sum.out
(lldb) settings set target.error-path /tmp/sum.err

Or give the program a separate terminal. Find the device with tty in another terminal window, and tell LLDB to use it.

Text
(lldb) settings set target.input-path /dev/pts/4
(lldb) settings set target.output-path /dev/pts/4
(lldb) settings set target.error-path /dev/pts/4

Then output and input for the program go to that terminal, and the GUI stays clean. Set these before run, because the process takes its file descriptors when it starts. If the screen is damaged anyway, Ctrl-L redraws the terminal in many setups.

Settings that help

The windows follow LLDB's usual settings. settings set stop-line-count-before does not apply here since the Sources window decides its own extent. settings set frame-format and thread-format change what the threads window prints. settings set target.x86-disassembly-flavor intel affects any disassembly view. Colours follow your terminal's palette and cannot be changed in the GUI itself.

What it lacks

The curses interface is a viewer with stepping and breakpoints, not an IDE. Its windows are the ones described above, with no register, disassembly or memory window, so for those you leave with Esc and use register read and disassemble. There is no watch list of arbitrary expressions, and mouse interaction depends on the terminal. Expression results do not appear in a window; you type them at the command line. It is also less used than GDB's TUI and less documented, with the built-in help window as the main reference, so behaviour can change between LLVM releases. For a richer view, use an IDE with LLDB's Debug Adapter Protocol server, lldb-dap, or Xcode on macOS, which are designed for it.

It works best for exactly the case it was built for: you are on a server over SSH with no graphical tools and want to step through a function while watching two or three variables.

Troubleshooting

If gui prints error: the gui command requires an interactive terminal, you are running LLDB with its input redirected or in a script. It needs a real terminal. If it prints a message about the terminal being too small, enlarge the window. If the colours are missing or boxes are drawn as letters, check TERM: xterm-256color is a safe value, and a TERM that the system's terminfo database does not know makes curses fall back. If keys act strangely inside tmux or screen, check that your multiplexer passes the keys through; Esc has a delay in tmux unless escape-time is set low, which makes leaving the GUI feel slow.

If a process dies while you are in the GUI, the windows empty and the status line says so; press Esc and run again.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.