Skip to content

Last updated:2026-10-12

Debugging C++ Programs with LLDB

This article introduces how to use the LLDB debugger to debug a simple C++ program. Our example code is named test.cpp.

#include <iostream>
#include <string>

struct MyStruct {
    int myInt;
    std::string myString;
};

int main() {
    MyStruct myStruct;
    myStruct.myInt = 42;
    myStruct.myString = "Hello, world!";

    int sum = 0;
    for (int i = 1; i <= 5; ++i) {
        sum += i;
    }

    return 0;
}

Compile

First, we need to compile the code with the -g flag to enable extra debugging information.

clang++ -g test.cpp -o test

Start Debugging

Next, we attach the debugger.

lldb test

Breakpoints

We set a breakpoint at the main() function.

(lldb) b main

Output:

Breakpoint 1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3

Run

Then, we start the process.

(lldb) run

Output:

Process 11333 launched: '/Users/light/test' (x86_64)
Process 11333 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003be3 test`main at test.cpp:10:14
   7    };
   8    
   9    int main() {
-> 10       MyStruct myStruct;
   11       myStruct.myInt = 42;
   12       myStruct.myString = "Hello, world!";
   13  

Inspecting Breakpoints

We have now stopped at the beginning of the main() function. We can set a breakpoint on a specific line.

(lldb) b 16

Output: Breakpoint 2: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c

To list the currently set breakpoints, use the following command.

(lldb) breakpoint list

Output:

(lldb) breakpoint list
Current breakpoints:
1: name = 'main', locations = 1, resolved = 1, hit count = 1
  1.1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3, resolved, hit count = 1 

2: file = '/Users/zhangxing/test.cpp', line = 16, exact_match = 0, locations = 1, resolved = 1, hit count = 0
  2.1: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c, resolved, hit count = 0 

Continuing Execution

We can continue executing the program until the next breakpoint.

(lldb) c

Output:

Process 11823 resuming
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100003c1c test`main at test.cpp:16:16
   13   
   14       int sum = 0;
   15       for (int i = 1; i <= 5; ++i) {
-> 16           sum += i;
   17       }
   18   
   19       return 0;

Printing Variables

To get the value of a current local variable, you can use the following command:

(lldb) p myStruct

Output:

(MyStruct) $0 = (myInt = 42, myString = "Hello, world!")

Watchpoints

To monitor changes in the value of a specific variable, we can use a watchpoint.

(lldb) watch set variable sum

Output:

Watchpoint created: Watchpoint 1: addr = 0x7ffeefbff1e8 size = 4 state = enabled type = w
    declare @ '/Users/zhangxing/test.cpp:14'
    watchpoint spec = 'sum'
    new value: 0

After stepping with next, you will see the watchpoint change.

(lldb) n

Watchpoint 1 hit:
old value: 3
new value: 6
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
    frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
   12       myStruct.myString = "Hello, world!";
   13   
   14       int sum = 0;
-> 15       for (int i = 1; i <= 5; ++i) {
   16           sum += i;
   17       }
   18   

If you want execution to stop based on a condition, you can set a stop condition.

(lldb) watch modify -c '(sum > 5)'

Output:

Watchpoint 1 hit:
new value: 6
Process 12848 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
    frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
   12       myStruct.myString = "Hello, world!";
   13   
   14       int sum = 0;
-> 15       for (int i = 1; i <= 5; ++i) {
   16           sum += i;
   17       }
   18   

When you are done debugging, you can continue executing the program.

(lldb) c

For more information, please refer to the LLDB documentation:


With these steps, you can easily use LLDB to debug your C++ programs, find bugs, and fix problems.

Comments