当前位置: 首页 > news >正文

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)

http://www.dtcms.com/a/590677.html

相关文章:

  • 中山微网站建设多少钱大连市建设部网站官网
  • MCP驱动的AI角色扮演游戏
  • 用html5做的网站素材做网站的人 优帮云
  • 网站开发方法 优帮云宿迁做网站的公司
  • 说说Java有哪些集合类
  • 网站首页顶部图片尺寸北京logo设计
  • 数学周刊第45期(2025年11月03日-11月09日)
  • 网站建设行业新闻有没有做字的网站
  • 网站的设计思想大美互助app
  • 【开题答辩过程】以《基于Android的学院自助洗衣店预约系统的设计与实现》为例,不会开题答辩的可以进来看看
  • 查询成绩的网站怎么做怎样做网站快手刷粉
  • html做网站标题的代码移动网站建设制作公司
  • 学习RT-thread(项目一:基于RT-thread的multi_button控制灯闪烁)
  • 3607. 电网维护
  • 中控技术(SUPCON)亮相ADIPEC,引领工业人工智能发展,推动智能化转型
  • SAP客户对S/4HANA投资回报持怀疑态度
  • 江苏建设厅网站电话多少六安发布最新通告
  • 做网络写手最好进那个网站在线直播
  • 网站用开源cms邯山网站制作
  • python-类相关
  • wordpress企业站源码wordpress 中文tag
  • 企业网站管理的含义假网站的域名
  • 金融机构信用评估系统中的业务数据审核流程设计
  • transformer中的位置编码
  • 检测网站是否被墙ui设计需要学哪些课程
  • 网站双语版的怎么制作wordpress文章带描述的工具
  • 成品软件网站大全推荐河南省水利建设厅网站
  • 算法:合并石头的最低成本
  • 大型新型网站网页设计公司简介代码
  • 食品营销型网站长沙网站建设0731