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

Sco*_*ttF 6 debugging android adb android-ndk lldb

如何使用lldb命令行调试器在 C++ 中调试我的 Android NDK 项目?

Hax*_*raZ 11

1. 准备工作

  • 准备一个android设备(不需要Root权限,我们将使用它的/data/local/tmp目录)。
  • 安装NDK、CMake、Ninja、adb、lldb,并将它们放入PATH环境变量中。

2. 编译带有调试信息的程序(即保留-g标志)

编译完成后,将它们复制到您设备的/data/local/tmp目录中。

3. 将提供的NDK复制lldb-server到设备

将提供的 NDK 复制lldb-server到您的 Android 手机(最好是 64 位手机),

adb push ./toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/9.0.9/lib/linux/aarch64/lldb-server /data/local/tmp
Run Code Online (Sandbox Code Playgroud)

并通过以下方式启动:

./lldb-server platform --listen "*:10086" --server
Run Code Online (Sandbox Code Playgroud)

10086是端口号,你可以更改它。

4. 转发端口

通过运行转发端口:

./lldb-server platform --listen "*:10086" --server
Run Code Online (Sandbox Code Playgroud)

5. 获取设备名称

adb forward tcp:10086 tcp:10086
Run Code Online (Sandbox Code Playgroud)

6.安装LLDB

安装 LLDB,将其二进制文件添加到PATH,输入以下命令:

adb devices   #For me, it's 39688bd9
Run Code Online (Sandbox Code Playgroud)

其中,39688bd9是我的设备ID,10086是我在前面步骤中选择的端口。

7.使用LLDB

现在,您已连接到 lldb-server,因此只需像在本地一样使用 lldb 即可:

platform select remote-android
platform connect connect://39688bd9:10086
Run Code Online (Sandbox Code Playgroud)


shi*_*hen 8

也许你可以尝试以下:(此示例步骤基于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.
Run Code Online (Sandbox Code Playgroud)

附加 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
Run Code Online (Sandbox Code Playgroud)

  • 相当于 `target remote :5045` 的 lldb 命令是 `gdb-remote localhost:5045`。lldb 可以与通用 gdbserver 通信,因此 shizen 的指令应该可以正常工作,只需用 lldb 命令替换 gdb 即可进行连接。 (3认同)