繁体   English   中英

如何使用 lldb 在命令行上调试 Android 上的 C++ 代码

[英]How do I use lldb to debug C++ code on Android on command line

我正在尝试使用 lldb 调试器调试 c++ 中的 Android ndk 项目。

我试图通过仅使用命令行来实现这一点。

我似乎找不到任何关于如何使用 lldb 和 adb 从命令行调试应用程序的文章或文档。

大概你可以试试下面的:(本示例步骤基于macOS

运行 gdb 服务器并附加进程

//Below commands will suspend the execution on the running app, and waits for a debugger to connect to it on port 5045.
adb shell

// to get pid
root@generic_x86:/ # ps | grep <your-app-name>
u0_a54    6510  1196  800157 47442 ffffffff b662df1b S 

<your-app-name>

root@generic_x86:/ # gdbserver :5045 --attach 6510 (PID)
Attached; pid = 6510
Listening on port 5045
//The process is now suspended, and gdbserver is listening for debugging clients on port 5045.

附加 gdb 调试器

//open a new terminal, e.g. terminal2, send below commands from this new terminal
//forward the above port to a local port on the host with the abd forward command
adb forward tcp:5045 tcp:5045
//launch gdb client from your android ndk folder
<your-ndk-home>/android-ndk-r16b/prebuilt/darwin-x86_64/bin/gdb
//Target the gdb to the remote sever
(gdb) target remote :5045

//now the process is successfully attached with the application for debugging, you can see below info from terminal 1.
Remote debugging from host 127.0.0.1
  1. 确保你的安卓手机已经植根

  2. 将 NDK 提供的lldb-server复制到您的 android 手机,并通过以下方式启动它:

./lldb-server platform --listen "*:10086" --server

10086 是端口号,您可以更改它

  1. 通过运行转发端口:
adb forward tcp:10086 tcp:10086
  1. 通过adb devices获取设备名称。 对我来说,它是39688bd9

  2. 使用适当的 python 安装 LLVM(我使用 LLVM-11.0 和 python3.6),然后打开 lldb,输入以下命令:

platform select remote-android
platform connect connect://39688bd9:10086
  1. 现在,您已连接到 lldb-server,因此只需像本地一样使用 lldb:
file some_exeutable_file_with_debug_info
b main
r

使用 android-ndk-r25b,我在以下方面有些运气:

在 shell window 1

adb push <ndk_dir>/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.6/lib/linux/aarch64/lldb-server /data/local/tmp
adb shell chmod +x /data/local/tmp/lldb-server
adb shell run-as <package_name> killall -9 lldb-server
sleep 1
adb shell run-as <package_name> cp /data/local/tmp/lldb-server /data/data/<package_name>/
adb shell am start -D -n "<package_name>/android.app.NativeActivity"
adb shell run-as <package_name> sh -c '/data/data/<package_name>/lldb-server platform --server --listen unix-abstract:///data/data/<package_name>/debug.socket'"

在 shell window 2

# Get the pid of the process you are trying to debug
adb shell run-as <package_name> ps
lldb
> platform select remote-android
> platform connect unix-abstract-connect:///data/data/<package_name>/debug.socket
> attach <pid>

在 shell window 3

# You will again need the pid of the process you are trying to debug
adb shell run-as <package_name> ps
adb forward tcp:12345 jdwp:<pid>
jdb -attach localhost:12345

然后go回到lldb运行在window 2,继续你的过程

我发现这个脚本很有用: https://github.com/iivke/flutter_android_lldb/blob/main/flutter_lldb.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM