Debugging Android Ndk Native Apps

Debugging Android NDK native apps

Well NDK_DEBUG=1 and debuggable flag in manifest set to true are required. When you build the app,in your project/libs/armeabi, there should be a gdb.setup file. There is symbol search path there, check whether it is valid. And did you try this:

ndk-gdb --start --verbose --force

And looks like you are getting a null pointer exception.

Debug native code in Android Studio

Actually, the advertised NDK support isn't available yet, even if you download the ndk-bundle and update Android Studio to the latest version in the canary channel (1.3-preview3 as of now).

The SDK tools team said that the NDK support wasn't part of the first previews of Android Studio 1.3. However it should be out soon - they recently mentioned mid-June as a target.

update: the debugging support is out now. It wasn't the case at the time of the initial question - thanks for all the downvotes since then :) please look at donturner's answer below.

How to get NDK debugging to work in Android Studio?

By the syntax of your build.gradle looks like you don't use the experimental plugin for gradle,
without it you wont be able to debug native c/c++ in android studio.
For more information read this : Android NDK Preview

Remotely debugging android NDK app using VSCode

In then end, I eschewed lldb in favour of gdb, which ndk-debug can use (for now, they're depreciating it, even though it works and lldb clearly either does not work or requires some setting that it doesn't advertise to work) if you include the 'no_lldb' flag or set 'use_lldb' to 'False' in ndk-gdb.py.

However, if you want visual debugging, you have to not let ndk-dbg.py launch the local gdb client. I've done this by creating a minimal batch file that connects to the phone and this may be useful for people reading this in the future in order to have a clear overview of what's happening on the android device and on the local machine. This script assumes only one device is attached for clarity.

adb shell forward tcp:5039 localfilesystem:/data/user/0/yourorg.yourapp/debug_socket #forward port 5039 to a temporary file called 'debug_socket'. I believe this is called is a 'Unix domain socket'
adb shell run-as yourorg.yourapp rm /data/user/0/yourorg.yourapp/debug_socket #remove the old socket file
adb push libs\arm64-v8a\gdbserver /data/local/tmp/arm64-gdbserver #push the arm64-gdbserver to a temp dir on the device
adb shell run-as yourorg.yourapp "cp /data/local/tmp/arm64-gdbserver /data/user/0/yourorg.yourapp/arm64-gdbserver" #copy the gdbserver to a directory from which it can be run with the privilages of the app you wish to debug
adb shell run-as yourorg.yourapp chmod 700 /data/user/0/yourorg.yourapp/arm64-gdbserver #Ensure 'arm64-gdbserver' is executable
adb shell run-as yourorg.yourapp /data/user/0/yourorg.yourapp/arm64-gdbserver --once +/data/user/0/yourorg.yourapp/debug_socket --attach 1234 #Run gdb server on device, connecting to the societ and the process '1234' (replace with actual PID of your running process)

Where 'yourorg.yourapp' is the name of your org and app and 1234 is the PID of the process to be debugged on your android device.

In visual studio code, you will need to have C++ extensions and gdb debugger extensions installed.

This is the 'launch.json' script that launches gdb locally and connects to the android device. Note that symbols are needed on the client side but not on the android device, so they can stripped if needed.

{
"name": "Debug pilkapel on device",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceRoot}",
"program":"${workspaceRoot}\\android-build\\DebugSys\\system\\bin\\app_process64",
"additionalSOLibSearchPath": "${workspaceRoot}\\android-build\\obj\\local\\arm64-v8a",
"miDebuggerServerAddress": "localhost:5039",
"setupCommands": [
{
"text": "set solib-absolute-prefix ${workspaceRoot}/android-build/",
"ignoreFailures": true
}
],
"windows":
{
"miDebuggerPath": "C:\\Users\\luthe\\AppData\\Local\\Android\\Sdk\\ndk\\23.1.7779620\\prebuilt\\windows-x86_64\\bin\\gdb.exe",
"MIMode": "gdb"
}
}

Also, compiler flags are important for this!
These are the compiler flags I used for the debug build of the library I'm working on:

LOCAL_CPPFLAGS := $(LOCAL_CPPFLAGS) -O0 -g -ggdb -gdwarf64 
LOCAL_CPPFLAGS += -gfull -fstandalone-debug -Wl -gno-split-dwarf
LOCAL_CPPFLAGS += -fno-unique-internal-linkage-names -fno-direct-access-external-data
LOCAL_CPPFLAGS += -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes
LOCAL_CPPFLAGS += -Wformat -Werror=format-security -fno-limit-debug-info -fPIC

Some of these may be unnecessary for the task at hand, I did really quite a lot of iteration using lldb but nothing worked.
For gdb, I believe it needs -gdwarf-5 as the debugging format (which I gather is the default). When I specified -gdwarf-4 as part of my experimentations, the gdb server ran but breakpoints did not work.

Visual proof of GDB android debugging within visual studio code

How to debug native code in an Android library project?

Try the answer by Jay in my question: debug native code in Android library.
I had no time to test it by my self, but it looks promising.
Let me know if it works.



Related Topics



Leave a reply



Submit