Start Debugging a C Program With LLDB
Compile a C program with clang -g, load it in LLDB, break on main, run, read the stop report, step with next and step, and quit. A first session with output.

LLDB is the debugger of the LLVM project. It is the default on macOS and iOS, the debugger Xcode drives, and a full citizen on Linux and FreeBSD. If you have used GDB, most of what you do transfers, but the commands are spelled differently and the output is arranged differently, so a first session is worth taking slowly. This post builds a tiny C program with debug information, starts LLDB on it, and goes through the whole loop once: load, break, run, read the stop report, step, print and quit. It uses LLDB 21 from LLVM 21 with Clang 21 on x86-64 Linux; the commands are identical on macOS with the LLDB that ships with Xcode, and the differences are noted where they appear. LLDB's command reference is the "LLDB Tutorial" and the command help built into the debugger.
The program
The same summing program used throughout this series, so line numbers match across the debuggers.
#include <stdio.h>
int sum(const int *v, int n)
{
int total = 0;
for (int i = 0; i < n; i++)
total += v[i];
return total;
}
int main(void)
{
int data[] = {4, 8, 15, 16, 23, 42};
int n = sizeof data / sizeof data[0];
int result = sum(data, n);
printf("sum = %d\n", result);
return 0;
}The numbers add to 108. Compile with Clang and -g, which tells the compiler to write DWARF debug information, the line tables and variable descriptions LLDB reads. -O0 turns optimisation off so every line has code and every variable lives on the stack.
clang -g -O0 -Wall -Wextra -o sum_g sum.c
./sum_gsum = 108Load the program
Start LLDB with the executable as its argument. --no-lldbinit (or -x) skips your personal startup file, so the session is the stock one.
lldb --no-lldbinit ./sum_g(lldb) target create "./sum_g"
Current executable set to '/home/dev/sum_g' (x86_64).
(lldb)LLDB calls the executable a target. It has parsed the file and its debug information but has not started a process. Everything you do from here is noun verb, which is LLDB's command grammar: breakpoint set, thread step-over, frame variable. The nouns are target, process, thread, frame, breakpoint, watchpoint, memory, register and a few more, and help lists them.
Set a breakpoint and run
(lldb) breakpoint set --name main
Breakpoint 1: where = sum_g`main + 11 at sum.c:13:9, address = 0x0000000000001155
(lldb) run
Process 41873 launched: '/home/dev/sum_g' (x86_64)
Process 41873 stopped
* thread #1, name = 'sum_g', stop reason = breakpoint 1.1
frame #0: 0x0000555555555155 sum_g`main at sum.c:13:9
10
11 int main(void)
12 {
-> 13 int data[] = {4, 8, 15, 16, 23, 42};
14 int n = sizeof data / sizeof data[0];
15 int result = sum(data, n);
16 printf("sum = %d\n", result);The response to breakpoint set says where the breakpoint is, as the function name plus an offset into it (main + 11, the first line after the prologue), the file, line and column, and the address. The address is an offset until the program loads, which is why it differs from the one after run; the column number and the process id will differ on your machine.
After run, LLDB reports the stop in three parts. The first line names the process. The second is the thread line: its number, its name, and the stop reason, breakpoint 1.1, which means breakpoint 1, location 1. The third is the frame line, the program counter, the module and function, and the file and line. Below that, LLDB prints the source around the current line, with -> marking the line about to execute. The surrounding lines are three before and three after by default, controlled by the settings stop-line-count-before and stop-line-count-after.
LLDB stops on line 13, the first line with code after the function's opening brace, because it skips the prologue like GDB does.
The short forms work as in GDB: b main for breakpoint set --name main, and r for run.
Step and look
thread step-over, short form next or n, runs one source line without entering calls. thread step-in, step or s, enters a call. thread step-out, finish, runs until the current function returns.
(lldb) next
Process 41873 stopped
* thread #1, name = 'sum_g', stop reason = step over
frame #0: 0x0000555555555170 sum_g`main at sum.c:14:9
11 int main(void)
12 {
13 int data[] = {4, 8, 15, 16, 23, 42};
-> 14 int n = sizeof data / sizeof data[0];
15 int result = sum(data, n);
16 printf("sum = %d\n", result);
17 return 0;
(lldb) next
(lldb) step
Process 41873 stopped
* thread #1, name = 'sum_g', stop reason = step in
frame #0: 0x0000555555555134 sum_g`sum(v=0x00007fffffffe0e0, n=6) at sum.c:5:9
2
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;The second next moved to line 15 (its stop report is omitted here for brevity). The step from line 15 entered sum, and the frame line now shows the function's arguments, v and n, with their values: n=6 matches the array. A stop caused by a step reports stop reason = step in or step over. Press Enter to repeat the last command, as in GDB.
Print a local with frame variable, abbreviated fr v, or p for an expression. Both get their own post later in this series.
(lldb) frame variable n
(int) n = 6
(lldb) p n * 2
(int) $0 = 12LLDB writes the type in parentheses before the value, and results of p go into numbered variables $0, $1 that you can reuse.
Finish and exit
(lldb) finish
Process 41873 stopped
* thread #1, name = 'sum_g', stop reason = step out
Return value: (int) $1 = 108
frame #0: 0x000055555555519b sum_g`main at sum.c:15:18
12 {
13 int data[] = {4, 8, 15, 16, 23, 42};
14 int n = sizeof data / sizeof data[0];
-> 15 int result = sum(data, n);
16 printf("sum = %d\n", result);
17 return 0;
18 }finish runs sum to its end and prints Return value: (int) $1 = 108, a nice touch that GDB's output also has in a different form. We are now back in main, in the middle of line 15 where the result still has to be stored. continue, short c, lets the program run to the end.
(lldb) continue
Process 41873 resuming
sum = 108
Process 41873 exited with status = 0 (0x00000000)
(lldb) quitThe program's output appears between LLDB's lines. The final line reports the exit status in decimal and hexadecimal. quit, q or Ctrl-D leaves LLDB. If the process is still alive, LLDB asks Do you really want to proceed: [Y/n]? and kills it on yes; for a process you attached to, it detaches instead, as covered in a later post on remote debugging.
Arguments, environment and batch use
Pass arguments after -- on the command line, or set them in the session.
lldb --no-lldbinit -- ./sum_g first second(lldb) settings set target.run-args first second
(lldb) settings set target.env-vars LOG_LEVEL=debug
(lldb) settings show target.run-args
target.run-args (arguments) =
[0]: "first"
[1]: "second"For scripts and automation, -o runs a command at startup and -b (batch) exits afterwards.
lldb --no-lldbinit -b -o 'breakpoint set --name sum' -o run -o 'bt' -o 'continue' ./sum_g(lldb) target create "./sum_g"
Current executable set to '/home/dev/sum_g' (x86_64).
(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
2
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;
(lldb) bt
* 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
frame #1: 0x000055555555519b sum_g`main at sum.c:15:18
(lldb) continue
Process 41990 resuming
sum = 108
Process 41990 exited with status = 0 (0x00000000)Batch mode echoes each command with its prompt, which makes a log readable. The * marks the selected frame in the backtrace.
Reading the stop report
It is worth learning to read the stop report quickly, since you will see it hundreds of times. The * thread #1 line says which thread stopped, and the * at the start marks it as the selected thread. The name in quotes is the thread's name, which defaults to the program's name for the main thread. The stop reason is the most important field: breakpoint 1.1 for a breakpoint, step over, step in or step out after a step command, watchpoint 2 for a watchpoint, signal SIGSEGV for a crash, and exec or fork for those events. A frame line starts with the frame number, then the program counter, then the module and function, with the arguments and the source file, line and column when debug information is present. When a frame has no debug information, the line shows only the module, the function name and an offset, and no source follows it.
Two habits help here. First, check the stop reason before anything else: a program that stopped for signal SIGSEGV needs bt and a look at the arguments, and one that stopped for a breakpoint needs a look at the variables. Second, notice the -> marker, which points at the line that has not run yet. Everything above it has run, and the values you print are the values after the lines above it, which is the opposite of the intuition that the marked line is the one that just executed.
When the symbols are missing
If you forget -g, LLDB loads the program and breakpoint set --name main still works from the symbol table, but the stop report has no source: frame #0: 0x... sum_gmain + 11. Variables cannot be printed by name. Rebuild with -g. On macOS, Clang puts debug information in object files and a separate .dSYM bundle produced by dsymutil, which Xcode builds make for you; a command-line build that skips it leaves LLDB reading the information from the .o files, which works only while they stay in place. Run dsymutil sum_g to produce sum_g.dSYM` next to the executable.


