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”。
路径 | 说明 |
|---|---|
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工具有些许差异,以实际应用为准。
记录日志
断点管理
观察点管理
表达式处理
查看变量
进程/线程管理
汇编处理
源码信息获取
信号处理
进程启动
attach进程
Linux x86_64本地调试。
LLDB支持在Linux x86_64环境上调试C和C++应用。
Mac桌面本地调试。
LLDB支持在Mac桌面(包括Mac x86_64和M1系统)调试C和C++应用。
基于HUAWEI DevEco Studio的远程调试。
LLDB支持基于HUAWEI DevEco Studio在Windows和Mac桌面连接HarmonyOS设备或模拟器远程调试Native C++应用,即使用HUAWEI DevEco Studio的Debug调试功能。
桌面连接HarmonyOS设备远程调试。
HarmonyOS设备为root镜像,SELinux关闭:LLDB支持在Windows、Mac桌面和Linux x86_64环境直连HarmonyOS设备远程调试C和C++应用以及可执行二进制。
HarmonyOS设备为root镜像,SELinux开启:LLDB支持在Windows、Mac桌面和Linux x86_64环境直连HarmonyOS设备远程调试C和C++应用以及可执行二进制,但是lldb-server和要调试的应用或可执行二进制只能放在/data/local/tmp/debugserver目录内。
HarmonyOS设备是user镜像,SELinux开启:LLDB支持在Windows、Mac桌面和Linux x86_64环境直连HarmonyOS设备远程调试基于HUAWEI DevEco Studio编译的带签名的debug版本的hap包,目前仅支持此类调试。
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”。
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
./lldb a.out
(lldb) b main
(lldb) run
(lldb) continue
(lldb) breakpoint list
(lldb) frame variable
(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
./a.out
./lldb
(lldb) process attach --name a.out
(lldb) breakpoint set --file hello.cpp --line 10
88
(lldb) continue
(lldb) detach
(lldb) quit
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
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
hdc shell ./data/local/tmp/lldb-server p --server --listen "*:8080"
lldb
(lldb) platform select remote-ohos (lldb) platform connect connect://localhost:8080
(lldb) target create /data/local/tmp/a.out
(lldb) b main
(lldb) run
(lldb) source list
(lldb) quit
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
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
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"
lldb
(lldb) platform select remote-ohos (lldb) platform connect unix-abstract-connect:///lldb-server/platform.sock
(lldb) settings append target.exec-search-paths "C:\Users\xxx\DevEcoStudioProjects\MyApplication \entry\build\default\intermediates\cmake\default\obj\arm64-v8a"
(lldb) breakpoint set --file "C:/Users/xxx/DevEcoStudioProjects/MyApplication/entry/src/main/cpp/napi_init.cpp" --line 6
(lldb) attach <pid>
(lldb) continue
(lldb) bt
(lldb) quit