How can I debug my Android NDK project in C++, using the lldb
debugger from the command line?
Probably you can try below: (This example steps are based on macOS)
run gdb server and attach process
//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.
attach gdb debugger
//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
target remote :5045
is gdb-remote localhost:5045
. lldb can talk to a generic gdbserver, so shizen's instructions should work fine, just replacing the gdb with the lldb command to connect. –
Skellum APP_ABI=all
. –
Necker 1. Preparation
- Prepare an android device(Root privilege is not required, we will use its
/data/local/tmp
directory). - Install NDK, CMake, Ninja, adb, lldb, and put them in
PATH
env var.
2. Compile program with debugging info (i.e. keep the -g
flag)
After compilation, copy them to your device's /data/local/tmp
directory.
3. Copy NDK provided lldb-server
to device
Copy NDK provided lldb-server
to your android phone(prefer the 64bit one),
adb push ./toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/9.0.9/lib/linux/aarch64/lldb-server /data/local/tmp
and start it by:
./lldb-server platform --listen "*:10086" --server
10086
is port number, you may change it.
4. Forward port
Forward port by running:
adb forward tcp:10086 tcp:10086
5. Get device name
adb devices #For me, it's 39688bd9
6. Install LLDB
Install LLDB, adding its binary to PATH
, typing these commands:
platform select remote-android
platform connect connect://39688bd9:10086
Among whith, 39688bd9
is my device id, 10086
is the port that I choose in previous steps.
7. Use LLDB
Now, you're connected with lldb-server, thus just use lldb like locally:
file some_executable_file_with_debug_info
b main
r
device id
but not localhost
? –
Dogoodism localhost
also works, but 127.0.0.1
doesn't. –
Dogoodism Probably you can try below: (This example steps are based on macOS)
run gdb server and attach process
//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.
attach gdb debugger
//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
target remote :5045
is gdb-remote localhost:5045
. lldb can talk to a generic gdbserver, so shizen's instructions should work fine, just replacing the gdb with the lldb command to connect. –
Skellum APP_ABI=all
. –
Necker With android-ndk-r25b, I had some luck with the below:
In 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'"
In 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>
In 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
Then go back to lldb running in window 2, and continue your process
I found this script to be useful: https://github.com/iivke/flutter_android_lldb/blob/main/flutter_lldb.py
© 2022 - 2024 — McMap. All rights reserved.