- Published on
Debugging a Simple Program with LLDB - Step-by-Step Guide
- Authors
- Name
- light-city
Debugging a Simple Program with LLDB
Table of Contents
This article will demonstrate how to debug a simple C++ program using the LLDB debugger. 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;
}
Compilation
First, we need to compile the code with the -g
flag to enable additional debugging information.
clang++ -g test.cpp -o test
Getting Started with 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
Running
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
Viewing Breakpoints
Now, we've stopped at the beginning of the main()
function. We can set breakpoints on specific lines.
(lldb) b 16
Output: Breakpoint 2: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c
To list 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 Program 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 current local variables, 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 watchpoints.
(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
When we next
, we will see the change in Watchpoint.
(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 to stop execution 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
After finishing debugging, you can continue executing the program.
(lldb) c
For more information, please refer to the LLDB documentation:
With these steps, you can easily debug your C++ programs using LLDB, identify issues, and resolve them.