如何使用 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. 确保您的 android phone 已 root 在 android phone 上使用 /data/local/tmp 目录。不需要根权限。

  2. lldb-server 提供的 NDK 复制到您的 android phone,然后通过以下方式启动它:

./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