目录

LLDB高性能调试器

概述

LLDB(Low Level Debugger)是新一代高性能调试器。详细说明参考LLDB官方文档。

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

工具获取

可通过HUAWEI DevEco Studio下载SDK获取LLDB调试工具。

以Windows平台为例,lldb.exe的存放路径为sdk\[HarmonyOS版本]\openharmony\native\llvm\bin。例如“sdk\HarmonyOS-NEXT-DP1\openharmony\native\llvm\bin”。

表1 lldb-server工具目录

路径

说明

sdk\[HarmonyOS版本]\hms\native\lldb\aarch64-linux-ohos\lldb-server

适用于aarch64-linux-ohos架构的lldb-server

sdk\[HarmonyOS版本]\hms\native\lldb\arm-linux-ohos\lldb-server

适用于arm-linux-ohos架构的lldb-server

sdk\[HarmonyOS版本]\hms\native\lldb\x86_64-linux-ohos\lldb-server

适用于x86_64-linux-ohos架构的lldb-server

功能列表

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

应用场景

使用指导-本地调试

Linux x86_64或Mac本地调试步骤一致。

使用LLDB工具启动应用并进行调试

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

源文件:hello.cpp

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

编译:

<clang distribution>/bin/clang++ -g hello.cpp -o a.out
  1. 获取到与LLDB同一版本的clang编译器生成的带有调试信息的可执行文件a.out。
  2. 运行LLDB工具,并指定要调试的文件为a.out。
    ./lldb a.out
  3. 在代码中main函数处设置断点。
    (lldb) b main
  4. 运行应用,使其停在断点处。
    (lldb) run
  5. 继续运行应用。
    (lldb) continue
  6. 列出所有断点。
    (lldb) breakpoint list
  7. 显示当前帧的参数和局部变量。
    (lldb) frame variable
  8. 按需执行调试命令进行后续调试操作。
  9. 退出调试。
    (lldb) quit

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

此处以在Mac环境调试一个使用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++ -g hello.cpp -o a.out
  1. 在终端窗口1启动应用。(窗口会返回一条信息“Please input a number of type int”)
    ./a.out
  2. 在终端窗口2运行LLDB工具。
    ./lldb
  3. attach应用。
    (lldb) process attach --name a.out
  4. 在hello.cpp的第10行设置断点。
    (lldb) breakpoint set --file hello.cpp --line 10
  5. 在终端窗口1,输入一个int类型的数。
    88
  6. 在终端行窗口2继续运行应用,使应用停在断点处。
    (lldb) continue
  7. 按需执行调试命令进行后续调试操作。
  8. detach应用。
    (lldb) detach
  9. 退出调试。
    (lldb) quit

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

使用指导-远程调试

  • 基于HUAWEI DevEco Studio的远程调试参考官方调试指导
  • 远程调试时需要lldb-server和lldb配合使用。
  • Windows,Linux x86_64和Mac远程调试步骤一致。

Windows平台连接aarch64-linux-ohos架构root镜像SELinux关闭的Harmony设备进行远程调试

源文件: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,将lldb-server和可执行文件a.out推送到设备。
    hdc file send lldb-server /data/local/tmp
    hdc file send a.out /data/local/tmp
    hdc shell chmod 755 /data/local/tmp/lldb-server /data/local/tmp/a.out
  2. 运行lldb-server。(8080为有效且当前未被占用的端口号,用户可自定义)
    hdc shell ./data/local/tmp/lldb-server p --server --listen "*:8080"
  3. 打开命令行窗口2,运行二进制文件lldb。
    lldb
  4. 在LLDB命令行窗口进行远端选择与连接。
    (lldb) platform select remote-ohos
    (lldb) platform connect connect://localhost:8080 
  5. 指定要调试的设备上的二进制文件a.out。
    (lldb) target create /data/local/tmp/a.out
  6. 在代码中main函数处设置断点。
    (lldb) b main
  7. 启动应用。
    (lldb) run
  8. 查看当前目标进程的源码。
    (lldb) source list
  9. 按需执行调试命令进行后续调试操作。
  10. 退出调试。
    (lldb) quit
Windows平台连接aarch64-linux-ohos架构user镜像SELinux开启的HarmonyOS设备进行远程调试
  • 建议基于HUAWEI 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包为基于HUAWEI 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