Skip to content
systemcoding.dev
Debugging

Inspect Variables With frame variable and expression in LLDB

Read variables in LLDB with frame variable, which reads memory, and expression, which compiles and runs code. Learn which to use, plus formats and structs.

7 min read
Summarize in AI
Flat illustration of a magnifier over lines of a log, with the LLVM, C++ and GNU logos.

GDB has one command for looking at values, print, and it evaluates everything the same way. LLDB has two, and the split between them is the thing to learn first. frame variable reads a local, argument or global straight from the program's memory using the debug information, and never runs any code. expression hands your text to the compiler, builds a small piece of code and executes it in the program, so it can call functions, compute with operators and assign to variables. The two answer the same question most of the time, and the differences matter when something goes wrong. This post shows both on a struct-heavy program, then formats, arrays and pointers, and where each one fails. LLDB 21 with Clang 21 on x86-64 Linux is the reference.

The program

A small inventory with a nested struct, an array and a pointer.

items.cc
#include <stdio.h>

struct dims {
    int width;
    int height;
};

struct item {
    int id;
    char name[16];
    double price;
    struct dims box;
};

static double total_price(const struct item *items, int count)
{
    double sum = 0.0;
    for (int i = 0; i < count; i++)
        sum += items[i].price;
    return sum;
}

int main(void)
{
    struct item stock[3] = {
        {1, "bolt", 0.25, {2, 1}},
        {2, "washer", 0.10, {3, 3}},
        {3, "bracket", 2.50, {10, 4}},
    };
    int counts[4] = {10, 20, 30, 40};
    struct item *cursor = &stock[1];
    double total = total_price(stock, 3);
    printf("%.2f %d %s\n", total, counts[0], cursor->name);
    return 0;
}

The total is 2.85. Build, stop on the call at line 33 and look around.

Shell
clang -g -O0 -Wall -Wextra -o items items.c
lldb --no-lldbinit ./items
Text
(lldb) breakpoint set --file items.c --line 33
Breakpoint 1: where = items`main + 213 at items.c:33:20, address = 0x00000000000011e5
(lldb) run
Process 60211 launched: '/home/dev/items' (x86_64)
Process 60211 stopped
* thread #1, name = 'items', stop reason = breakpoint 1.1
    frame #0: 0x00005555555551e5 items`main at items.c:33:20

The source listing LLDB prints after the frame line is omitted here.

frame variable

frame variable, short fr v or v, with no arguments prints every variable in the selected frame: arguments first, then locals.

Text
(lldb) frame variable
(item[3]) stock = {
  [0] = (id = 1, name = "bolt", price = 0.25, box = (width = 2, height = 1))
  [1] = (id = 2, name = "washer", price = 0.10000000000000001, box = (width = 3, height = 3))
  [2] = (id = 3, name = "bracket", price = 2.5, box = (width = 10, height = 4))
}
(int[4]) counts = ([0] = 10, [1] = 20, [2] = 30, [3] = 40)
(item *) cursor = 0x00007fffffffe0f0
(double) total = 0

Each line carries the type in parentheses and then the value. LLDB prints a struct with named fields in parentheses and an array with its indexes, and it shows the name field as a string without the trailing NULs, which GDB prints as repeat counts. total is 0 or stack garbage here since line 33 has not yet run. The price 0.1 shows as 0.10000000000000001, the 17-digit form of the nearest double, as in GDB.

Name variables to see just those, and use C syntax for parts.

Text
(lldb) frame variable counts
(int[4]) counts = ([0] = 10, [1] = 20, [2] = 30, [3] = 40)
(lldb) frame variable stock[2].name
(char[16]) stock[2].name = "bracket"
(lldb) frame variable cursor->name
(char[16]) cursor->name = "washer"
(lldb) frame variable -L cursor
0x00007fffffffe0e8: (item *) cursor = 0x00007fffffffe0f0

frame variable understands member access with . and ->, array indexing and * to dereference. The -L flag prints the variable's address in front, which tells you where on the stack it lives. Useful flags: -T shows types only, -D 3 limits the depth of nested structs, -A shows arguments only, -a shows locals only, -r REGEX matches variable names, and -f x formats the values in hex.

Text
(lldb) frame variable -f x counts
(int[4]) counts = ([0] = 0x0000000a, [1] = 0x00000014, [2] = 0x0000001e, [3] = 0x00000028)

Because frame variable reads memory directly, it works even when the program cannot run code: in a core file, in a process stopped at an odd place, and with no JIT or expression machinery working. It is also fast. Its limits are the flip side: it cannot add, call functions or cast arbitrary expressions. frame variable counts[1] + 1 is an error, since this command takes variable paths, not expressions.

expression

expression, short expr, p or print, takes any expression in the language of the current frame.

Text
(lldb) expression counts[1] + counts[2]
(int) $0 = 50
(lldb) p sizeof(struct item)
(unsigned long) $1 = 40
(lldb) p stock[2].box.width * stock[2].box.height
(int) $2 = 40
(lldb) p $0 * 2
(int) $3 = 100

The results are stored in $0, $1 and so on, and may appear in later expressions. A bare p result from frame variable has no such name. Behind the scenes, LLDB compiled your text with Clang, resolved the names against the program's debug information, and ran the result in the process. That is how it can do the next things.

Text
(lldb) expression total_price(stock, 3)
(double) $4 = 2.8500000000000001
(lldb) expression counts[0] = 99
(int) $5 = 99
(lldb) frame variable counts[0]
(int) counts[0] = 99

A call into the program computes the real total, and an assignment changes the program's memory, as frame variable then shows. Treat both with care: calling a function runs program code on the stopped thread, which can take locks or change state, and assignment changes the very thing you are debugging. The -- form separates options from the expression, which matters when the expression starts with a dash: expression -- -counts[0].

Formats and sizes

expression takes a format with -f or the p/FORMAT shorthand. Common letters: x hex, d decimal, u unsigned, o octal, t binary, c character, f float, s C string.

Text
(lldb) p/x counts[3]
(int) $6 = 0x00000028
(lldb) p/t counts[0]
(int) $7 = 0b00000000000000000000000001100011
(lldb) p/c stock[0].name[0]
(char) $8 = 'b'

LLDB zero-pads hex to the type's width, so 0x00000028 where GDB prints 0x28. The binary form carries a 0b prefix. To see memory without the type system, use memory read, covered in the post that compares the two debuggers.

Arrays and pointers

A pointer does not say how many elements follow it. For frame variable, the -Z option (--element-count) sets how many elements of a pointed-to array to show. For expression, cast the pointer to a pointer to an array of the length you want and dereference it.

Text
(lldb) frame variable -Z 2 cursor
(item *) cursor = 0x00007fffffffe0f0
(lldb) expression -- stock
(item[3]) $9 = ([0] = (id = 1, name = "bolt", price = 0.25, box = (width = 2, height = 1)), [1] = (id = 2, name = "washer", price = 0.10000000000000001, box = (width = 3, height = 3)), [2] = (id = 3, name = "bracket", price = 2.5, box = (width = 10, height = 4)))
(lldb) p *cursor
(item) $10 = (id = 2, name = "washer", price = 0.10000000000000001, box = (width = 3, height = 3))
(lldb) p cursor - stock
(long) $11 = 1

The second command printed the whole stock array on one line, which is what expression does with an aggregate. Dereferencing a pointer with * shows one element, and pointer arithmetic follows C: cursor - stock is the element difference, 1. For an array behind a plain pointer, such as an int *p function argument, cast to an array type: expression -- *(int (*)[8])p shows eight elements starting at p, which is the same idea as GDB's *p@8. There is no @ operator in LLDB.

Which one to use

TaskCommandWhy
Show a local or argumentframe variable xdirect memory read, no code run
Show a field or array elementframe variable a.b[2]same
Compute with variablesexpression x + yneeds the compiler
Call a functionexpression f(x)runs code in the process
Change a valueexpression x = 5assignment needs execution
Look in a core fileframe variablenothing is running
Optimised code with partly lost variablesbotheach reports <variable not available>

When in doubt, start with frame variable. It is the cheaper, safer one, and its output is the same as expression's for a plain variable. Reach for expression when you need to compute.

When they fail

An expression can fail with error: use of undeclared identifier 'x', which means the name is not visible in the selected frame; select the right frame with frame select 1 or up. It can fail with error: Couldn't lookup symbols, when it needs a symbol that is not in the debug information. An expression that includes a template or a macro may need -g3 or -fstandalone-debug for the compiler to see enough. frame variable fails with error: no variable named 'x' found in this frame, with the same cause. In optimised builds both print <variable not available> or <optimized out>, as in GDB.

Printing in a core file and in a stopped, broken process

frame variable earns its keep when the process cannot run code. Load a core file with target create -c core ./items and every command that only reads memory still works: frame variable, bt, memory read, register read. expression fails there with a message that the process is not running, since it needs to execute the compiled snippet. The same holds for a process stopped in a state where running code is unsafe, such as inside a signal handler with a lock held: an expression that calls a function can deadlock, while frame variable cannot. That is the practical reason to build the habit of starting with frame variable and moving to expression only when you need to compute.

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.