LLDB 调试入门教程
LLDB 调试入门教程
- 1. `sample.cpp`
- 1.1. Compile and Run
- 2. LLDB 调试
- 2.1. Specifying the Program to Debug
- 2.2. Using LLDB as a Standalone Debugger
- References
The LLDB Debugger (Low-Level Debugger, LLDB) is the debugger component of the LLVM project.
LLDB is a next generation, high-performance debugger. It is built as a set of reusable components which highly leverage existing libraries in the larger LLVM Project, such as the Clang expression parser and LLVM disassembler.
1. sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -al
total 12
drwxr-xr-x 2 yongqiang yongqiang 4096 Nov 9 22:27 .
drwxr-xr-x 4 yongqiang yongqiang 4096 Feb 16 2025 ..
-rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 2025 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$
(base) yongqiang@yongqiang:~/workspace/yongqiang$ cat sample.cpp
#include <iostream>
#include <vector>double Sum(const std::vector<double> &data) {double total = 0;for (size_t i = 0; i < data.size(); ++i) {total += data[i];}return total;
}int main() {std::vector<double> data;data.push_back(10);data.push_back(20);data.push_back(30);std::cout << "Sum(data) = " << Sum(data) << std::endl;return 0;
}(base) yongqiang@yongqiang:~/workspace/yongqiang$
1.1. Compile and Run
(base) yongqiang@yongqiang:~/workspace/yongqiang$ uname -a
Linux yongqiang 6.6.87.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
(base) yongqiang@yongqiang:~/workspace/yongqiang$
Compile your program with the -g flag.
(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -a -l
total 12
drwxr-xr-x 2 yongqiang yongqiang 4096 Nov 9 22:27 .
drwxr-xr-x 4 yongqiang yongqiang 4096 Feb 16 2025 ..
-rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 2025 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$(base) yongqiang@yongqiang:~/workspace/yongqiang$ g++ -g --std=c++17 sample.cpp -o sample
(base) yongqiang@yongqiang:~/workspace/yongqiang$
(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -al
total 152
drwxr-xr-x 2 yongqiang yongqiang 4096 Nov 9 23:18 .
drwxr-xr-x 4 yongqiang yongqiang 4096 Feb 16 2025 ..
-rwxr-xr-x 1 yongqiang yongqiang 142488 Nov 9 23:18 sample
-rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 2025 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$(base) yongqiang@yongqiang:~/workspace/yongqiang$ ./sample
Sum(data) = 60
(base) yongqiang@yongqiang:~/workspace/yongqiang$
2. LLDB 调试
GDB to LLDB command map
https://lldb.llvm.org/use/map.html
2.1. Specifying the Program to Debug
As with GDB, you can start LLDB and specify the file you want to debug using the command line.
(base) yongqiang@yongqiang:~/workspace/yongqiang$ lldb ./sample
(lldb) target create "./sample"
Current executable set to '/home/yongqiang/workspace/yongqiang/sample' (x86_64).
(lldb) run
Process 942 launched: '/home/yongqiang/workspace/yongqiang/sample' (x86_64)
Sum(data) = 60
Process 942 exited with status = 0 (0x00000000)
(lldb) quit
(base) yongqiang@yongqiang:~/workspace/yongqiang$
Or you can specify the executable file to debug after it is already running using the file command:
(base) yongqiang@yongqiang:~/workspace/yongqiang$ lldb
(lldb) file ./sample
Current executable set to '/home/yongqiang/workspace/yongqiang/sample' (x86_64).
(lldb) run
Process 1045 launched: '/home/yongqiang/workspace/yongqiang/sample' (x86_64)
Sum(data) = 60
Process 1045 exited with status = 0 (0x00000000)
(lldb) quit
(base) yongqiang@yongqiang:~/workspace/yongqiang$
2.2. Using LLDB as a Standalone Debugger
(base) yongqiang@yongqiang:~/workspace/yongqiang$ lldb ./sample
(lldb) target create "./sample"
Current executable set to '/home/yongqiang/workspace/yongqiang/sample' (x86_64).
(lldb) breakpoint set --name main
Breakpoint 1: where = sample`main + 28 at sample.cpp:14:22, address = 0x0000000000001330
(lldb) run
Process 1281 launched: '/home/yongqiang/workspace/yongqiang/sample' (x86_64)
Process 1281 stopped
* thread #1, name = 'sample', stop reason = breakpoint 1.1frame #0: 0x0000555555555330 sample`main at sample.cpp:14:2211 }1213 int main() {
-> 14 std::vector<double> data;15 data.push_back(10);16 data.push_back(20);17 data.push_back(30);
(lldb) next
Process 1281 stopped
* thread #1, name = 'sample', stop reason = step overframe #0: 0x000055555555533c sample`main at sample.cpp:15:161213 int main() {14 std::vector<double> data;
-> 15 data.push_back(10);16 data.push_back(20);17 data.push_back(30);18
(lldb) frame variable
(std::vector<double, std::allocator<double> >) data = size=0 {}
(lldb) next
Process 1281 stopped
* thread #1, name = 'sample', stop reason = step overframe #0: 0x000055555555535c sample`main at sample.cpp:16:1613 int main() {14 std::vector<double> data;15 data.push_back(10);
-> 16 data.push_back(20);17 data.push_back(30);1819 std::cout << "Sum(data) = " << Sum(data) << std::endl;
(lldb) next
Process 1281 stopped
* thread #1, name = 'sample', stop reason = step overframe #0: 0x000055555555537c sample`main at sample.cpp:17:1614 std::vector<double> data;15 data.push_back(10);16 data.push_back(20);
-> 17 data.push_back(30);1819 std::cout << "Sum(data) = " << Sum(data) << std::endl;20
(lldb) frame variable
(std::vector<double, std::allocator<double> >) data = size=2 {[0] = 10[1] = 20
}
(lldb) next
Process 1281 stopped
* thread #1, name = 'sample', stop reason = step overframe #0: 0x000055555555539c sample`main at sample.cpp:19:1516 data.push_back(20);17 data.push_back(30);18
-> 19 std::cout << "Sum(data) = " << Sum(data) << std::endl;2021 return 0;22 }
(lldb) frame variable
(std::vector<double, std::allocator<double> >) data = size=3 {[0] = 10[1] = 20[2] = 30
}
(lldb) run
There is a running process, kill it and restart?: [Y/n] n
(lldb) continue
Process 1281 resuming
Sum(data) = 60
Process 1281 exited with status = 0 (0x00000000)
(lldb) quit
(base) yongqiang@yongqiang:~/workspace/yongqiang$
References
[1] Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/
[2] LLDB Quick Start Guide, https://developer.apple.com/library/archive/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/Introduction.html
[3] LLDB (debugger), https://en.wikipedia.org/wiki/LLDB_(debugger)
