Skip to content
systemcoding.dev
Debugging

Watch Memory for Changes With LLDB Watchpoints

Find what writes a variable with LLDB watchpoint set variable and expression, choose read, write or both, set a size, add conditions and know the limits.

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

When a value changes and you do not know which line changed it, the question is about data, not code, and a breakpoint cannot answer it. A watchpoint can: you name a memory location, and the debugger stops the program at the instruction that touches it. LLDB implements watchpoints with the CPU's debug registers where it can, so the program runs at full speed until the moment of access. This post walks through setting one on a variable and on a raw address, choosing whether to stop on reads or writes, using conditions and commands, and the limits that hardware imposes. It reuses the buffer overflow from the GDB hardware-watchpoints post so the two debuggers can be compared. LLDB 21 with Clang 21 on x86-64 Linux is the reference; on Apple silicon the same commands work with different register limits.

The bug

A session record has an 8-byte name and a privilege level. A setter copies in a name that is too long and overwrites the level.

corrupt.cc
#include <stdio.h>
#include <string.h>

struct session {
    char name[8];
    int  level;
};

static struct session current = {"guest", 1};

static void set_name(const char *src)
{
    strcpy(current.name, src);
}

int main(void)
{
    set_name("administrator");
    printf("%s level=%d\n", current.name, current.level);
    return 0;
}

The program prints administrator level=1869898098. No line in the source assigns to level, which is why a breakpoint will not find the cause. Build it and stop in main.

Shell
clang -g -O0 -Wall -Wextra -o corrupt corrupt.c
lldb --no-lldbinit ./corrupt
Text
(lldb) breakpoint set --name main
Breakpoint 1: where = corrupt`main + 4 at corrupt.c:18:5, address = 0x0000000000001171
(lldb) run
Process 71102 launched: '/home/dev/corrupt' (x86_64)
Process 71102 stopped
* thread #1, name = 'corrupt', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555171 corrupt`main at corrupt.c:18:5

Watch a variable

watchpoint set variable, short w s v, takes a variable path, which can include members.

Text
(lldb) watchpoint set variable current.level
Watchpoint created: Watchpoint 1: addr = 0x555555558018 size = 4 state = enabled type = w
    declare @ '/home/dev/corrupt.c:9'
    watchpoint spec = 'current.level'
    new value: 1
(lldb) continue
Process 71102 resuming

Watchpoint 1 hit:
old value: 1
new value: 1869898098
Process 71102 stopped
* thread #1, name = 'corrupt', stop reason = watchpoint 1
    frame #0: 0x00007ffff7f2a8b4 libc.so.6`__strcpy_avx2 + 164
libc.so.6`__strcpy_avx2:
->  0x7ffff7f2a8b4 <+164>: mov    %rdx, 0x8(%rdi)

The creation message gives the address, the size (4 bytes, the size of the int), the state and the type, w for write. After continue, LLDB stops with Watchpoint 1 hit and prints the old and new values. The value 1869898098 is the four letters r, a, t, o of "administrator" read as a little-endian integer. The stop is inside the C library's strcpy, so look at the callers.

Text
(lldb) bt
* thread #1, name = 'corrupt', stop reason = watchpoint 1
  * frame #0: 0x00007ffff7f2a8b4 libc.so.6`__strcpy_avx2 + 164
    frame #1: 0x0000555555555168 corrupt`set_name(src="administrator") at corrupt.c:13:5
    frame #2: 0x0000555555555190 corrupt`main at corrupt.c:18:5

Frame 1 is the call to fix, with the argument printed in the frame line. The function name __strcpy_avx2 and the offsets depend on your CPU, since glibc picks an implementation at startup.

Watch an address or expression

watchpoint set expression, short w s e, evaluates an expression to an address and watches it. The -- separates options from the expression, and a size is required unless the expression has a type that fixes it.

Text
(lldb) watchpoint set expression -s 4 -- &current.level
Watchpoint created: Watchpoint 2: addr = 0x555555558018 size = 4 state = enabled type = w
    new value: 1869898098

Use this form for heap memory, where there is no variable name: watchpoint set expression -s 8 -- ptr watches the 8 bytes ptr points to at the time you type it. For a variable that lives in a frame, watchpoint set variable follows the variable by name and is deleted when the frame is popped, as a GDB local watchpoint is; the expression form watches the address and survives the frame, with the risk that the stack slot is reused by something else.

Read, write or both

The -w option chooses the access type.

OptionTriggers on
-w writewrites (the default)
-w readreads
-w read_writeany access
Text
(lldb) watchpoint set variable -w read_write current.level
Watchpoint created: Watchpoint 3: addr = 0x555555558018 size = 4 state = enabled type = rw
    declare @ '/home/dev/corrupt.c:9'
    watchpoint spec = 'current.level'
    new value: 1869898098
(lldb) continue
Process 71102 resuming

Watchpoint 3 hit:
old value: 1869898098
new value: 1869898098
Process 71102 stopped
* thread #1, name = 'corrupt', stop reason = watchpoint 3
    frame #0: 0x0000555555555185 corrupt`main at corrupt.c:19:5

On x86 the debug registers cannot trap on reads alone, so a read watch is implemented as read-or-write, and LLDB reports a hit with equal old and new values for a read. The stop in main at line 19 is the printf reading level. Note that Watchpoint 1 and 2 were still active in this session and would hit on a later write; delete the ones you do not need.

Manage watchpoints

Text
(lldb) watchpoint list
Number of supported hardware watchpoints: 4
Current watchpoints:
Watchpoint 1: addr = 0x555555558018 size = 4 state = enabled type = w
    declare @ '/home/dev/corrupt.c:9'
    watchpoint spec = 'current.level'
    hit_count = 1     ignore_count = 0
Watchpoint 3: addr = 0x555555558018 size = 4 state = enabled type = rw
    hit_count = 1     ignore_count = 0
(lldb) watchpoint disable 3
(lldb) watchpoint delete 1 2

The header line Number of supported hardware watchpoints: 4 is the limit on this machine: x86-64 has four debug address registers. Each watchpoint uses one register per 1, 2, 4 or 8 aligned bytes it covers. A watch on a 16-byte object needs two, and anything above the available registers fails when you try to create it with error: Watchpoint creation failed. Unlike GDB, LLDB does not fall back to single-stepping, so there are no software watchpoints: you get hardware or an error. That is slower to discover but never silently slow.

Conditions, ignore counts and commands

Watchpoints take the same modifiers as breakpoints, through watchpoint modify.

Text
(lldb) watchpoint modify -c 'current.level > 100' 1
(lldb) watchpoint command add 1
Enter your debugger command(s).  Type 'DONE' to end.
> bt 3
> continue
> DONE

The condition is evaluated each time the hardware trap fires, so a variable written a million times costs a million stops and evaluations. Use a condition to narrow, and reach for a different tool if the hit rate is high. The commands list works as for breakpoints, and ends at the first command that resumes the process.

Scope and lifetime

A watchpoint made with watchpoint set variable on a local is tied to that frame. When the function returns, LLDB deletes it with a message, because the stack space will hold other data. If you want to keep the address, use the expression form on &var. For a global like current, which lives for the whole run, the variable form is safe. A process that exits loses its watchpoints; they are not saved across runs, but watchpoint commands in a ~/.lldbinit file or a -o script recreate them.

A worked reading of the stop

Look again at the stop in __strcpy_avx2. The program counter is on the instruction after the store that changed the value, because the CPU reports the debug exception after the instruction completes. So the instruction shown, mov %rdx, 0x8(%rdi), is not the writing one: the writer is the instruction before it in the listing. The register rdi holds the destination, current.name, and the offset 8 reaches current.level, which fits a copy that stores eight bytes at offset 8 of the destination. You can confirm the destination with register read rdi and compare it with &current.name, and you can confirm that the store was longer than the field with memory read --size 1 --count 16 --format x &current.name, which shows the letters of the name running into the integer. None of that needs the source of strcpy.

Frame 1 is the real finding: it shows the call site and the argument, src="administrator", which is longer than the eight-byte field. The fix is in your code, a bounded copy such as snprintf(current.name, sizeof current.name, "%s", src) or a check of the length first, and the watchpoint's job was to point at the call, wherever in a large program it was made.

When not to use a watchpoint

A watchpoint stops at the instruction that wrote, which for corruption by another thread is a different thread than the one you are looking at; thread list shows which one hit. Memory written by the kernel, such as the buffer a read fills, is changed in kernel mode and may not trigger the hardware trap at the moment of the write. In a virtual machine whose hypervisor does not expose debug registers, creation fails or the watchpoint never fires. For a region larger than four registers can cover, use AddressSanitizer to catch the invalid write at its source, or a recorded run with rr under GDB to go backwards from the corruption.

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.