【问题标题】:How do I use lldb to debug C++ code on Android on command line如何在命令行上使用 lldb 调试 Android 上的 C++ 代码
【发布时间】:2019-05-13 00:18:11
【问题描述】:

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

我正在尝试仅使用命令行来实现这一点。

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

【问题讨论】:

    标签: android debugging android-ndk adb lldb


    【解决方案1】:

    或许您可以尝试以下方法:(此示例步骤基于 ma​​cOS

    运行 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
    

    【讨论】:

    • 感谢您的回复,但这个答案使用的是 gdb,而不是 lldb
    • 相当于target remote :5045的lldb命令是gdb-remote localhost:5045。 lldb 可以与通用 gdbserver 通信,因此 shizen 的说明应该可以正常工作,只需将 gdb 替换为 lldb 命令即可连接。
    • 注意:lldb 也提供了自己的 gdb 远程协议服务器(不出所料地称为 lldb-server)。它不是必需的,但确实有一些扩展 lldb 将用于更有效地调试。遗憾的是,我对 Android 发行版的了解还不够,不知道如何获取和安装适当版本的 lldb-server。
    • shell 命令'gdbserver :5045 --attach ' 返回'/system/bin/sh: gdbserver: inaccessible or not found',大概是因为我没有root。有没有办法在没有root的情况下做到这一点?我注意到我的 Android Studio (3.5.2) 在调试检测测试时似乎不想暂停 LLDB 调试器附加。事实上,它一直在说“启动 LLDB 服务器”,并且不会让我取消操作。考虑到这个错误,我希望直接使用 CLI,但我的测试设备生根并不是一个真正的选择。
    • 我也遇到了同样的问题,不知道Android studio是怎么在设备端启动lldb server的
    【解决方案2】:
    1. 确保你的安卓手机已经root 在你的安卓手机上使用/data/local/tmp 目录。不需要 root 权限。

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2011-05-30
      • 2015-05-17
      • 1970-01-01
      • 2019-04-26
      • 2021-07-19
      相关资源
      最近更新 更多