ohos-kotlin-native-kexe-lldb-demo:基于 OpenHarmony 生态的 Kotlin Native 调试示例项目

用户可借助此项目学习 OpenHarmony 环境下 Kotlin Native 应用的 lldb 调试流程,包含 kexe 文件生成、lldb-server 部署、端口转发及断点调试等核心操作,解决远程调试中表达式调用失败问题。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago

生成kn-debug.kexe

./gradlew :app:linkDebugExecutableNative

推送lldb-server到(设备端)

hdc file send /Users/issuser/Library/OpenHarmony/Sdk/20/native/llvm/lib/clang/15.0.4/bin/aarch64-linux-ohos/lldb-server /data/local/tmp/lldb-server

//加权限
hdc shell chmod 755 /data/local/tmp/lldb-server

推送kexe文件到(设备端)

hdc file send /Users/issuser/Documents/testDemo/ohosApplication/app/build/bin/native/debugExecutable/kn-debug.kexe /data/local/tmp/kn-debug.kexe

//加权限
hdc shell chmod 755 /data/local/tmp/kn-debug.kexe

终端A:启动gdbserver

hdc shell /data/local/tmp/lldb-server gdbserver :45678 -- /data/local/tmp/kn-debug.kexe

终端B:

端口转发

与启动的gdbserver一致

hdc fport tcp:45678 tcp:45678

进入lldb

/Users/issuser/Library/OpenHarmony/Sdk/20/native/llvm/bin/lldb 

创建目标

target create /Users/issuser/Documents/testDemo/ohosApplication/app/build/bin/native/debugExecutable/kn-debug.kexe

加载脚本

command script import "/Users/issuser/Documents/testDemo/ohosApplication/scripts/konan_lldb.py"

映射源码

settings set target.source-map /Users/issuser/Documents/testDemo/ohosApplication /Users/issuser/Documents/testDemo/ohosApplication

打断点

br set -f DebugFunc.kt -l 185

让本机 lldb 作为“调试器客户端”,连接到设备上

gdb-remote 127.0.0.1:45678

问题点

查 pid:

hdc shell ps -A | grep kn-debug

杀死进程

hdc shell pkill -9 kn-debug.kexe \ hdc shell kill -9 59582

只能看到 ObjHeader* 和 []

(lldb) expr -l c -- (void)Konan_DebugPrint(complexPerson) 
error: expression failed to parse: error: Can't evaluate the expression without a running target due to: Interpreter doesn't handle one of the expression's opcodes 
(lldb) expr -l c -- (int)Konan_DebugGetFieldCount(complexPerson) 
error: expression failed to parse: error: Can't evaluate the expression without a running target due to: Interpreter doesn't handle one of the expression's opcodes (lldb)

Konan_DebugPrint 和 Konan_DebugGetFieldCount 在你的 kexe 里存在 所以问题不在“缺符号”,而是 脚本调用这些 helper 的方式在你当前的远程调试环境里失败(很常见于 remote gdb-remote / ohos / lldb 组合)

当前这种 remote gdb-remote(lldb-server)+ HarmonyOS 的链路里,LLDB 的表达式解释器(用于“在目标进程里调用函数/执行表达式”)不支持某些指令/操作码,所以 无法做“函数调用型表达式”。

而 konan_lldb.py 的核心能力正是:通过在目标里调用 Konan_DebugGetFieldCount / Konan_DebugPrint / Konan_DebugGetField* 这些 runtime helper 来解包 ObjHeader。现在这些调用全部被 LLDB expression engine 禁掉了 → 脚本必然 DebuggerException → 你只能看到 ObjHeader* 和 []。

Introduction

用户可借助此项目学习 OpenHarmony 环境下 Kotlin Native 应用的 lldb 调试流程,包含 kexe 文件生成、lldb-server 部署、端口转发及断点调试等核心操作,解决远程调试中表达式调用失败问题。【此简介由AI生成】

Customize your domain