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

gblwokeq  于 2022-12-04  发布在  Android
关注(0)|答案(3)|浏览(154)

我试图弄清楚如何使用lldb调试器在C++中调试我的Android ndk项目。
我尝试仅使用命令行来实现这一点。
我似乎找不到任何关于如何使用lldb沿着adb从命令行调试应用程序的文章或文档。

rqcrx0a6

rqcrx0a61#

大概你可以试试下面的:(本示例步骤基于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
ryhaxcpt

ryhaxcpt2#

1.请确保您的android手机是root使用您的android手机上的/data/local/tmp目录。不需要Root权限。
1.将NDK提供的lldb-server复制到您的Android手机,并通过以下方式启动它:

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

10086是端口号,可以更改
1.通过运行以下命令转发端口:

adb forward tcp:10086 tcp:10086

1.通过adb devices获取设备名称。对我来说,它是39688bd9
1.使用适当的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
yrefmtwq

yrefmtwq3#

使用android-ndk-r25 b,我有一些运气与以下:

在shell窗口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窗口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窗口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

然后返回到在窗口2中运行的lldb,并继续您的过程
我发现这个脚本很有用:https://github.com/iivke/flutter_android_lldb/blob/main/flutter_lldb.py

相关问题