目录

LLDB高性能调试器

概述

LLDB(Low Level Debugger)是新一代高性能调试器。具备断点设置、变量查看与修改、内存操作、线程控制、表达式计算、堆栈回溯等功能,并支持跨平台和插件扩展。

当前HarmonyOS中的LLDB工具是在llvm15.0.4基础上适配演进出来的,是DevEco Studio工具链中默认的调试器,支持调试C和C++应用程序。

详细说明参考LLDB官方文档。

功能特点

LLDB调试器具备以下功能特点:

工具获取路径

签名校验机制

lldb-server在运行时会对自身进行数字签名验证,只有通过华为官方签名的lldb-server才能正常调试应用。

中的lldb-server是经过闭源签名处理的特殊版本。

这种设计主要用于保护鸿蒙(HarmonyOS)调试权限,防止未授权的调试行为。

功能列表

此处列举LLDB调试器支持的部分功能,更多命令参考:LLDB工具使用指导和LLDB官网手册。Windows、Linux x86_64和Mac平台的LLDB工具有些许差异,以实际应用为准。

环境准备

root镜像:使用hdc shell id命令查询到“uid=0(root)”,或执行hdc shell进入交互命令环境,提示符为“#”。

user镜像:使用hdc shell id命令查询到“uid=2000(shell)”,或执行hdc shell进入交互命令环境,提示符为“$”。

SELinux开启模式:使用hdc shell getenforce命令查询到“Enforcing”。

SELinux关闭模式:使用hdc shell getenforce命令查询到“Permissive”。

注意:

lldb-server建议通过DevEco Studio创建Native项目并点击Debug按钮自动推送,避免手动推送的版本兼容性和权限问题。

使用指导-本地调试

[h2]使用LLDB工具启动并调试应用

此处以在HarmonyOS环境调试一个使用clang编译器生成的带有调试信息的可执行文件a.out为例。

源文件:hello.cpp

#include <iostream>
using namespace std;
int main() {
    cout << "hello world!" <<endl;
    return 0;
}

编译:

<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out
  1. 获取到与LLDB同一版本的clang编译器生成的带有调试信息的可执行文件a.out。

  2. 使用hdc shell进入手机交互命令环境,进入lldb路径。

  3. 运行LLDB工具,并指定要调试的文件为a.out。

    ./lldb a.out
  4. 在代码中main函数处设置断点。

    (lldb) b main
  5. 运行应用,使其停在断点处。

    (lldb) run
  6. 继续运行应用。

    (lldb) continue
  7. 列出所有断点。

    (lldb) breakpoint list
  8. 显示当前帧的参数和局部变量。

    (lldb) frame variable
  9. 按需执行调试命令进行后续调试操作。

  10. 退出调试。

    (lldb) quit

[h2]使用LLDB工具调试已经启动的应用

此处以在手机环境调试一个使用clang编译器生成的带有调试信息和用户输入的可执行文件a.out为例。

源文件:hello.cpp

#include <iostream>
using namespace std;

int main() {
    int i = 0, j = 5, sum = 0;
    cout << "Please input a number of type int" <<endl;
    cin >> i;
    cout << i;
    sum = i + j;
    cout << sum <<endl;
    return 0;
}

编译:

<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out
  1. 使用hdc shell进入手机交互命令环境,进入lldb路径。

  2. 在终端窗口1启动应用。(窗口会返回一条信息“Please input a number of type int”)

    ./a.out
  3. 在终端窗口2运行LLDB工具。

    ./lldb
  4. attach应用。

    (lldb) process attach --name a.out
  5. 在hello.cpp的第10行设置断点。

    (lldb) breakpoint set --file hello.cpp --line 10
  6. 在终端窗口1,输入一个int类型的数。

    88
  7. 在终端行窗口2继续运行应用,使应用停在断点处。

    (lldb) continue
  8. 按需执行调试命令进行后续调试操作。

  9. detach应用。

    (lldb) detach
  10. 退出调试。

    (lldb) quit

步骤attach应用和设置断点可以调换顺序执行。

使用指导-远程调试

  • 远程调试是指使用lldb进行跨端调试。本章节主要针对开发者跨平台调试HarmonyOS设备的应用进行说明。

  • 基于DevEco Studio的远程调试参考官方调试指导。

  • 远程调试时需要lldb-server和lldb配合使用。

  • Windows,Linux x86_64和Mac远程调试步骤一致。

[h2]root镜像远程调试

  • 支持调试的应用或二进制是aarch64-linux-ohos架构的native C++工程。

  • 为了方便调试建议调试时关闭SELinux。

源文件:hello.cpp

#include <iostream>
using namespace std;
int main() {
    cout << "hello world!" <<endl;
    return 0;
}

编译:

<clang distribution>/bin/clang++ --target=aarch64-linux-ohos --sysroot=<sysroot distribution> -g hello.cpp -o a.out
  1. 打开命令行窗口1,关闭SELinux。

    hdc shell setenforce 0
  2. 在命令行窗口1,将lldb-server和可执行文件a.out推送到设备。

    hdc file send lldb-server hdc file send a.out /data/local/tmp/debugserver/debugserver
    hdc shell chmod 755
    /data/local/tmp/debugserver/lldb-server /data/local/tmp/debugserver/a.out
  3. 运行lldb-server。(8080为有效且当前未被占用的端口号,用户可自定义)

    hdc shell /data/local/tmp/debugserver/lldb-server p --server --listen "*:8080"
  4. 打开命令行窗口2,运行二进制文件lldb。

    ./lldb
  5. 在LLDB命令行窗口进行远端选择与连接。

    (lldb) platform select remote-ohos
    (lldb) platform connect connect://localhost:8080
  6. 指定要调试的设备上的二进制文件a.out。

    (lldb) target create /data/local/tmp/debugserver/a.out
  7. 在代码中main函数处设置断点。

    (lldb) b main
  8. 启动应用。

    (lldb) run
  9. 查看当前目标进程的源码。

    (lldb) source list
  10. 按需执行调试命令进行后续调试操作。

  11. 退出调试。

    (lldb) quit

[h2]user镜像远程调试

  • user镜像SELinux默认开启,无法关闭。

  • 建议基于DevEco Studio调试user镜像SELinux开启的HarmonyOS设备的hap包。

  • lldb-server需推送至指定的目录/data/local/tmp/debugserver。如:/data/local/tmp/debugserver/lldb-server或/data/local/tmp/debugserver/com.example.myapplication/lldb-server。

  • lldb-server在aarch64-linux-ohos架构目录获取。详情参考表1 lldb-server工具目录

  • 此案例中的hap包为基于DevEco Studio创建的native C++默认工程编译的带debug信息的hap包。

  • 调试过程中需保持设备在非锁屏状态,锁屏不允许启动调试器调试。

  1. 打开命令行窗口1,将lldb-server和hap包推送到设备。

    hdc shell mkdir data/local/tmp/debugserver/com.example.myapplication
    hdc file send lldb-server data/local/tmp/debugserver/com.example.myapplication
    hdc shell chmod 755 data/local/tmp/debugserver/com.example.myapplication/lldb-server
    hdc shell mkdir data/local/tmp/d333e74fe3ab488aad622a7055fbf396
    hdc file send C:\Users\xxx\DevEcoStudioProjects\MyApplication\entry\build\default\outputs\default\entry-default-signed.hap data/local/tmp/d333e74fe3ab488aad622a7055fbf396
  2. hap包安装运行,关闭appfreeze。

    hdc shell bm install -p data/local/tmp/d333e74fe3ab488aad622a7055fbf396
    hdc shell aa start -a EntryAbility -b com.example.myapplication
    hdc shell aa attach -b com.example.myapplication
  3. 运行lldb-server。

    hdc shell aa process -a EntryAbility -b com.example.myapplication -D "/data/local/tmp/debugserver/com.example.myapplication/lldb-server platform --listen unix-abstract:///lldb-server/platform.sock"
  4. 打开命令行窗口2,运行二进制文件lldb。

    ./lldb
  5. 在LLDB命令行窗口进行远端选择与连接。

    (lldb) platform select remote-ohos
    (lldb) platform connect unix-abstract-connect:///lldb-server/platform.sock
  6. 添加目标可执行文件搜索路径。

    (lldb) settings append target.exec-search-paths "C:\Users\xxx\DevEcoStudioProjects\MyApplication\entry\build\default\intermediates\cmake\default\obj\arm64-v8a"
  7. 在源代码第6行处设置断点。

    (lldb) breakpoint set --file "C:/Users/xxx/DevEcoStudioProjects/MyApplication/entry/src/main/cpp/napi_init.cpp" --line 6
  8. 指定要调试的设备上的hap包对应的应用pid。

    (lldb) attach <pid>
  9. 点击设备应用,使其继续响应,并停止在断点处。

  10. 继续调试。

    (lldb) continue
  11. 显示当前线程的堆栈回溯。

    (lldb) bt
  12. 按需执行调试命令进行后续调试操作。

  13. 退出调试。

    (lldb) quit

FAQ