1# LLDB工具 2## 概述 3LLDB(Low Level Debugger)是新一代高性能调试器。具备断点设置、变量查看与修改、内存操作、线程控制、表达式计算、堆栈回溯等功能,并支持跨平台和插件扩展。 4 5当前 OpenHarmony 中的 LLDB 工具基于 [llvm15.0.4](https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.4) 适配演进,是 HUAWEI DevEco Studio 工具链的默认调试器,支持调试 C 和 C++ 应用程序。 6 7详细说明参考[LLDB官方文档](https://lldb.llvm.org/)。 8 9## 功能特点 10 11LLDB调试器具备以下功能特点: 12- **强大的调试功能**:支持断点设置、变量查看与修改、内存操作、线程控制、表达式计算、堆栈回溯等。 13- **跨平台支持**:适用于Windows、Linux x86_64、ohos和Mac平台。 14- **插件扩展性**:支持插件扩展,方便开发者根据需求进行定制。 15 16## 工具获取路径 17通过OpenHarmony的SDK获取,获取路径:http://ci.openharmony.cn/workbench/cicd/dailybuild 18 19lldb工具在SDK中的路径为`\ohos-sdk\[system]\native\llvm`,其中system可选windows/linux/darwin。 20 21- lldb客户端 (Windows系统) 执行文件路径: 22 ```bash 23 \ohos-sdk\windows\native\llvm\bin\lldb.exe 24 ``` 25 26## 功能列表 27 28此处列举LLDB调试器支持的部分功能,更多命令参考:[LLDB工具使用指导](https://gitee.com/openharmony/third_party_llvm-project/blob/master/lldb/README_zh.md)和[LLDB官网手册](https://lldb.llvm.org/use/map.html#)。Windows、Linux x86_64和Mac平台的LLDB工具有些许差异,以实际应用为准。 29 30- 记录日志 31 32```bash 33# 记录完整调试会话到文件 34(lldb) log enable -F -T -p -f d:\lldb.log lldb all 35(lldb) log enable -F -T -p -f d:\lldbgdbremote.log gdb-remote all 36# 示例:过滤只记录断点事件 37(lldb) log enable -f /tmp/breakpoints.log lldb break 38# 查看当前日志配置 39(lldb) log list 40``` 41 42- 断点管理 43 44```bash 45# 设置函数断点(支持模糊匹配) 46(lldb) breakpoint -f main.cpp -l 266 47# 设置条件断点(当x>100时触发) 48(lldb) breakpoint set -f main.cpp -l 20 -c '(x > 100)' 49# 列出所有断点 50(lldb) breakpoint list 51# 临时禁用断点 52(lldb) breakpoint disable 1 53``` 54 55- 观察点管理 56 57```bash 58# 监控变量变化 59(lldb) watchpoint set variable global_var 60# 监控内存地址变化 61(lldb) watchpoint set expression -w write -- 0x7ffeefbff5d8 62# 查看观察点列表 63(lldb) watchpoint list 64``` 65- 表达式处理 66 67```bash 68# 创建变量 69(lldb)print int $value1 = 7 70(lldb)expression int $value2 = 7 71# 打印变量值 72(lldb) print $value1 73(lldb) expression $value2 74# 变量运算 75(lldb) expression $value1 * 3 76# 格式化输出(16进制显示) 77(lldb) p/x 12345 78``` 79 80- 查看变量 81 82```bash 83# 查看当前帧的已初始化的局部变量 84(lldb) frame variable 85# 查看全局变量和静态变量 86(lldb) frame variable -g 87# 查看寄存器 88(lldb) register read 89``` 90 91- 进程/线程管理 92 93```bash 94# 显示线程回溯(所有线程) 95(lldb) thread backtrace all 96# 单步跳过(Step over) 97(lldb) thread step-over (或next) 98# 跳出当前选定的帧(步出) 99(lldb) thread step-out (或finish) 100``` 101 102- 汇编处理 103 104```bash 105# 查看寄存器 106(lldb) register read 107# 更改pc寄存器 108(lldb) register write pc `$pc+8` 109# 查看当前堆栈帧的汇编指令 110(lldb) disassemble --frame (或 dis -f ) 111# 查看main函数的汇编指令 112(lldb) disassemble -name main 113# 汇编单步执行程序,步过(不进入函数体) 114(lldb) nexti 115# 汇编单步执行程序,步入(进入函数体) 116(lldb) stepi 117``` 118 119- 源码信息获取 120 121```bash 122(lldb) source list 123``` 124 125- 信号处理 126 127```bash 128# 捕获信号时打印回溯 129(lldb) process handle SIGSEGV -s true 130# 查看当前信号处理配置 131(lldb) process handle 132``` 133 134- attach进程 135 136```bash 137# 附加到PID(需调试权限) 138(lldb) process attach -p 1234 139``` 140 141## 环境准备 142- 本地调试 143 - 无需使用DevEco IDE,可以直接在设备端进行调试。 144 - 选择静态化的lldb路径并使用hdc传输到设备: 145 ```bash 146 hdc file send \ohos-sdk\[system]\native\llvm\lib\clang\[version]\bin\aarch64-linux-ohos\lldb /data/local/tmp/debugserver 147 ``` 148 - 选择lldb server路径(根据设备CPU架构选择)并使用hdc传输到设备: 149 150 ```bash 151 hdc file send \ohos-sdk\[system]\native\llvm\lib\clang\[version]\bin\aarch64-linux-ohos\lldb-server /data/local/tmp/debugserver 152 ``` 153- 远程调试(主要调试方式) 154 - 一键调试: 155 - 下载HUAWEI IDE,根据IDE的调试方法即可进行一键调试:通过DevEco Studio调试。 156 - 支持Windows/Mac连接OpenHarmony设备,支持调试Native C++应用。 157 - 直接使用DevEco Studio的Debug功能即可,无需手动推送lldb或lldb-server。 158 - 手动调试: 159 - 如需要手动进行远程调试(不通过DevEco Studio),如调试二进制等,则需要保证设备上有lldb-server,PC上有lldb 160 - 准备lldb-server,建议使用DevEco IDE推送。如手动推送,选择lldb-server路径并使用hdc传输到设备: 161 ```bash 162 hdc file send \ohos-sdk\[system]\native\llvm\lib\clang\[version]\bin\aarch64-linux-ohos\lldb-server /data/local/tmp/debugserver 163 ``` 164 - PC上准备lldb,如windows系统则使用lldb.exe, 稍后将使用lldb与OH设备上的lldb-server远程连接进行调试。 165 166## 使用指导-本地调试 167 168### 使用LLDB工具启动并调试应用 169 170 此处以在Linux x86_64环境下,调试一个使用Clang编译器生成的、带有调试信息的可执行文件a.out为例。 171 源文件:hello.cpp 172```cpp 173#include <iostream> 174using namespace std; 175int main() { 176 cout << "hello world!" << endl; 177 return 0; 178} 179``` 180编译: 181```cpp 182<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out 183``` 184 1. 获取到与LLDB同一版本的clang编译器生成的带有调试信息的可执行文件a.out。 185 2. 使用hdc shell进入手机交互命令环境,进入lldb路径。 186 3. 运行LLDB工具,并指定要调试的文件为a.out。 187 188 ```shell 189 ./lldb a.out 190 ``` 191 192 4. 在代码中main函数处设置断点。 193 194 ```shell 195 (lldb) b main 196 ``` 197 198 5. 运行应用,使其停在断点处。 199 200 ```shell 201 (lldb) run 202 ``` 203 204 6. 继续运行应用。 205 206 ```shell 207 (lldb) continue 208 ``` 209 210 7. 列出所有断点。 211 212 ```shell 213 (lldb) breakpoint list 214 ``` 215 216 8. 显示当前帧的参数和局部变量。 217 218 ```shell 219 (lldb) frame variable 220 ``` 221 222 9. 按需执行调试命令进行后续调试操作。 223 224 10. 退出调试。 225 226 ```shell 227 (lldb) quit 228 ``` 229 230**使用LLDB工具调试已经启动的应用。** 231 232此处以手机环境调试使用clang编译器生成的带有调试信息的可执行文件a.out为例。 233源文件:hello.cpp 234 235```cpp 236#include <iostream> 237using namespace std; 238int main() { 239 int i = 0, j = 5, sum = 0; 240 cout << "Please input a number of type int" << endl; 241 cin >> i; 242 cout << i; 243 sum = i + j; 244 cout << sum << endl; 245 return 0; 246} 247``` 248 249编译: 250 251```cpp 252<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out 253``` 254 2551. 使用hdc shell进入交互命令环境,进入lldb路径。 2562. 在命令行窗口1启动应用。(窗口会返回一条信息“Please input a number of type int”。) 257 ```shell 258 ./a.out 259 ``` 260 2613. 在命令行窗口2运行LLDB工具。 262 ```shell 263 ./lldb 264 ``` 265 2664. attach应用。 267 268 ```shell 269 (lldb) process attach --name a.out 270 ``` 271 2725. 在`hello.cpp`的第10行设置断点。 273 274 ```shell 275 (lldb) breakpoint set --file hello.cpp --line 10 276 ``` 277 2786. 在命令行窗口1,输入一个int类型的数。 279 280 ```shell 281 88 282 ``` 283 2847. 在命令行窗口2继续运行应用,使应用停在断点处。 285 286 ````shell 287 (lldb) continue 288 ```` 289 2908. 按需执行调试命令进行后续调试操作。 291 2929. detach应用。 293 294 ```shell 295 (lldb) detach 296 ``` 297 29810. 退出调试。 299 300 ```shell 301 (lldb) quit 302 ``` 303 304> **说明:** 305> 306> 步骤attach应用和设置断点可以调换顺序执行。 307 308 309## 使用指导-远程调试 310 311> **说明:** 312> - 远程调试是指使用lldb进行跨端调试。本章节主要针对开发者跨平台调试OpenHarmony设备的应用进行说明。 313> - 远程调试时需要lldb-server和lldb配合使用。 314> - Windows,Linux x86_64和Mac远程调试步骤一致。 315> 316 317### 远程调试 318 319此处以在Windows平台连接arm架构OpenHarmony设备(如:RK3568开发板)进行远程调试为例。 320 321> **说明:** 322> 323> 其中/data/local/tmp为设备上指定的目录。 324> 325> 8080为监听端口,可自定义。 326> 327> 请确保设备上的lldb-server和a.out有可执行权限。 328> 329源文件:hello.cpp 330```cpp 331#include <iostream> 332using namespace std; 333int main() { 334 cout << "hello world!" << endl; 335 return 0; 336} 337``` 338 339编译: 340```bash 341<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out 342``` 343 3441. 打开命令行窗口1,关闭SELinux。 345 ```shell 346 hdc shell setenforce 0 347 ``` 348 3492. 打开命令行窗口1,将lldb-server和可执行文件a.out推送到设备。(a.out是使用clang编译器编译hello.cpp生成的。) 350 351 ```shell 352 hdc file send lldb-server /data/local/tmp 353 hdc file send a.out /data/local/tmp 354 ``` 355 3563. 运行lldb-server。 357 358 ```shell 359 hdc shell ./data/local/tmp/lldb-server p --server --listen "*:8080" 360 ``` 361 3624. 打开命令行窗口2,运行二进制文件lldb。 363 364 ```shell 365 ./lldb 366 ``` 367 3685. 在LLDB命令行窗口进行远端选择与连接。 369 370 ```shell 371 (lldb) platform select remote-ohos 372 (lldb) platform connect connect://localhost:8080 373 ``` 374 3756. 指定要调试的设备上的二进制文件a.out。 376 377 ```shell 378 (lldb) target create /data/local/tmp/a.out 379 ``` 380 3817. 在代码中main函数处设置断点。 382 383 ```shell 384 (lldb) b main 385 ``` 386 3878. 启动应用。 388 389 ```shell 390 (lldb) run 391 ``` 392 3939. 查看当前目标进程的源码。 394 395 ```shell 396 (lldb) source list 397 ``` 398 39910. 按需执行调试命令进行后续调试操作。 400 40111. 退出调试。 402 403 ```shell 404 (lldb) quit 405 ``` 406 407## FAQ 408- 当在LLDB调试环境中执行run命令时,若控制台返回`error: 'A' packet returned an error: 8`或类似错误代码,此问题通常表明调试器无法创建调试进程。该异常现象通常表明调试器无法创建调试进程,主要原因是权限不足,建议通过以下步骤排查: 409 410 1)验证目标设备是否已开启调试授权。 411 412 验证方式为设备上设置中的`开发者选项`,如果没有开启,开启后再尝试调试。 413 414 2)确认当前用户是否具有目标进程的调试权限。 415 416 user用户只能调试应用,不能调试可执行文件。 417 418- 运行lldb-server,报错`Permission denied`。这一般是由于lldb-server无可执行文件导致的,添加权限即可。