Debugging Android Native C++: activate breakpoint - android

I am trying to debug a C++ native code on Android device using NDK.
The Android.mk already has the debug flags:
APP_OPTIM:= debug
LOCAL_CFLAGS := -DRAPIDXML_NO_EXCEPTIONS
LOCAL_CFLAGS += -g
LOCAL_CFLAGS += -ggdb
LOCAL_CFLAGS += -O1
I also tried with the flag -O0 instead of -O1.
And the AndroidManifest.xml has also the debug mode:
android:debuggable="true"
So, here we go... I put some breakpoints in the code to watch it step by step. Went to the terminal and typed:
$ adb shell ps |grep packtpub
app_57 24084 117 467368 49136 ffffffff 00000000 S com.packtpub.app3D
Then run the debug:
$ ndk-gdb --verbose --force
...
Launched gdbserver succesfully.
Setup network redirection
## COMMAND: /home/android-sdks/platform-tools/adb forward tcp:5039 localfilesystem:/data/data/com.packtpub.app3D/debug-socket
## COMMAND: /home/android-sdks/platform-tools/adb shell run-as com.packtpub.app3D lib/gdbserver +debug-socket --attach 1822
## COMMAND: /home/android-sdks/platform-tools/adb pull /system/bin/app_process ./obj/local/armeabi/app_process
Attached; pid = 1822
Listening on sockaddr socket debug-socket
Pulled app_process from device/emulator.
## COMMAND: /home/android-sdks/platform-tools/adb pull /system/lib/libc.so ./obj/local/armeabi/libc.so
Pulled libc.so from device/emulator.
The application starts and run. But no break points show up. Neither the (gdb) prompt appears to allow me to set commands.
I tried both ways: from Eclipse IDE, and from console using the command line. And the same happens.
Any suggestion how to spot what is missing to put the debug on and have the breakpoints working?
All comments and suggestions are highly appreciated.
So... I keep fighting to solve why the ndk-debug is not running. Maybe I came across something that can shed a little light on it:
I have also added to Android.mk:
APP_MODULES := callbacks
APP_PLATFORM := android-14
On command line in the terminal:
$ android update project -p . --target android-14
It gives the message:
Error: Target id 'android-14' is not valid. Use 'android list targets' to get the target ids.
Then we obtain the output when running the list targets:
$ android list targets
id: 1 or "android-15"
Name: Android 4.0.3
Type: Platform
API level: 15
...
id: 2 or "Google Inc.:Google APIs:15"
On the other hand it compiles and builds on command line only if I put 'android-15', but it will not run in the device (a Samsung Nexus), which has the 'android-14'. In the AndroidManifest.xml it was setup as 14, to allow the App to compile, load and run in the device from the Eclipse IDE:
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="14">
In Eclipse IDE it compiles and runs with no complaints as Android-14. When asked to run ndk-debug on Eclipse IDE, it also doesn't complaint as shown mentioned in my previous post, but neither the (gdb) appears.
However, when I try to run the ndk-debug on command line it complains and shows that it is not attaching the debugger:
$ ndk-gdb --verbose --force
...
Launched gdbserver successfully.
## COMMAND: /home/android-sdks/platform-tools/adb shell run-as com.packtpub.app3D lib/gdbserver +debug-socket --attach 7722
## COMMAND: /home/android-sdks/platform-tools/adb pull /system/bin/app_process ./obj/local/armeabi/app_process
Cannot attach to process 7722: Operation not permitted (1)
Pulled app_process from device/emulator.
The ndk-gdb not working has something to do with the target version?
Any suggestions or workaround that might fix it and make possible to have active breakpoint to debug NDK?
Thanks in advance.

Solved the problem installing the API 14 in the debugging environment,and creating a new project entirely as API 14. Now it works.

Related

Error with compiling and source code(AOSPA)

When I compiling the AOSPA ROM, it always stopped with following massage:
make: *** No rule to make target 'out/target/product/nx529j/obj/SHARED_LIBRARIES/libqdutils_intermediates/export_includes', needed by 'out/target/product/nx529j/obj/SHARED_LIBRARIES/libsurfaceflinger_intermediates/import_includes'. Stop.
I have tried to find solution on Google, github and XDA-Developers forum, but I can't found any solutions .
Wha shoi I do?
Solution is Simple but no perfect:
$ pushd out/target/product/nx529j/obj/SHARED_LIBRARIES/
$ mkdir libqdutils_intermediates
$ touch libqdutils_intermediates/export_includes
$ popd
Do not clear cache or delete the out/ folder, just try to build (bruch/make/launch) again .
The reason for this error is missing libqdutils.so , the one you can pull from device. The solution above just for showing up the missing libqdutils.so error for you in next build you try.
Once you encounter missing libqdutils.so error, just (be sure android device connected to pc for adb):
$ pushd ./vendor/{manufacturer}/{device}/proprietary/
$ adb pull /system/lib/libqdutils.so ./
$ popd
Reference:
https://forum.xda-developers.com/showthread.php?t=2357126
https://forum.xda-developers.com/showthread.php?t=2754997&page=147

IntelliJ SQLite permission denied on Android Lollipop, worked before

I updated Nexus 7 with Android Lollipop and now i can't access SQLite db from IntelliJ IDEA 14. Everything was working fine before Lollipop update, even though Android wasn't rooted i could access db from IntelliJ (but not pull it via ADB).
The error i am getting when i try to connect is:
Data Source Synchronization Error
Cannot synchronize 'SQLite': run-as: exec failed for /data/local/tmp/intellij_native_tools/get_modification_time Error:Permission denied
I am aware that SQLite was updated in lollipop from 3.7 to 3.8. What could cause this kind of error? Wrong DB creation, old DB drivers, something else?
adb root doesn't resolve anything, there's numerous issues depending on environment. Here's how I got it working on emulator where I spend a lot of my development time.
First we need to resolve the error only position independent executables (PIE) are supported with the binary.
get the source of android ultimate plugin tool:
git clone https://android.googlesource.com/platform/tools/adt/idea
cd idea/android/ultimate/get_modification_time/jni
Apply the following patch:
diff --git a/android/ultimate/get_modification_time/jni/Application.mk b/android/ultimate/get_modification_time/jni/Application.mk
index a252a72..bdf815d 100644
--- a/android/ultimate/get_modification_time/jni/Application.mk
+++ b/android/ultimate/get_modification_time/jni/Application.mk
## -1 +1,2 ##
APP_ABI := all
+APP_PLATFORM := android-16
You'll need to setup a recent version of NDK to build, I had ndk-r10d installed already.
# ndk-r10d
ndk-build
cd ..
cp -R libs native_tools
# update the intellij plugin with whatever arch you plan to use
# adjust the path here to where your copy of IntelliJ is located
zip -u ~/local/apps/idea/plugins/android/lib/android-ultimate.jar native_tools/*/get_modification_time
Note that you need to update the path in that last line to point to your local IntelliJ installation. For example, on MacOS, it'll be:
zip -u /Applications/IntelliJ\ IDEA\ 14.app/Contents/plugins/android/lib/android-ultimate.jar native_tools/*/get_modification_time
Restart IntelliJ.
Next, we'll be replacing /system/bin/run-as on the emulator. Create the following script locally and name it run-as.
#! /system/bin/sh
INTELLIJ_NATIVE="/data/local/tmp/intellij_native_tools/get_modification_time"
if [ "$2" = "$INTELLIJ_NATIVE" ]; then
cd /data/local/tmp/intellij_native_tools
./get_modification_time "$3"
else
/system/bin/run-as.org "$#"
fi
Start the emulator. After it has booted, update it.
# update run-as after start
adb shell mount -o remount,rw /system
adb shell mv /system/bin/run-as /system/bin/run-as.org
adb push ./run-as /system/bin/run-as
Now you can synchronize to your heart's content.
Note, you may also want to follow progress on the bug upstream which just had priority bumped after posting this: https://youtrack.jetbrains.com/issue/IDEA-137606
If you have a rooted device try command
adb root
After it IDEA synhronization is successfull.

Android tracedmdump post_trace: command not found malformed trace

I am attempting to use tracedmdump to parse trace files off the Android emulator. I have gone backwards and forwards with the Android OS repo, building the kernel, etc, and I can only assume I missed a step since when I run tracedmdump I get the output:
-bash: post_trace: command not found
***
*** Error: malformed trace. Did you remember to exit the emulator?
***
I'm getting my trace files through:
$ android create avd -n <emu name> -t <target id> -p <my dir> -s <skin> -b armabi-v7a -c 256M -f
$ emulator -trace <folder name> -avd <avd name> -memory 2048 -gpu on -debug-init
then once the emulator is going I use F9 to start then stop tracing.
I've looked, and there's no sign of post_trace anywhere on my machine. I have found 0 resources for why this might be, so I'm lead to believe that something quietly failed in my build process, but I have no idea what it could possibly be. I'm on Mac OSX, and attempting to use the manta kernel, but I don't know what other information could be useful. Please help!
Try cd into /sdk/emulator/qtools folder, and type mm command option to compile those tools.

Unable to attach ndk-gdb to android process

I have two processes in the Android application- main application process and a remote service process.
The native libraries are loaded from the remote service process.
I want to debug the native library code.
The application is debuggable. The native library was built from Windows command-line using ndk-build. In Android.mk, -g -ggdb flag was used for this library.
I am running ndk-gdb from cygwin.
Test device: Galaxy Nexus running Android 4.2.2
Error line: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
In the verbose information, I see that gdbserver is trying to attach to process id of the main application process. Should it matter that the library is loaded from a remote process within this application.
Detailed ndk-gdb run information-
$ ndk-gdb --verbose --force
Android NDK installation path: /cygdrive/c/work/android/ndk/android-ndk-r9-windows-x86/android-ndk-r9
Using default adb command: /cygdrive/c/work/android/sdk/android-sdk_r10-windows/android-sdk-windows/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.31
Using ADB flags:
Using JDB command: /cygdrive/c/program files (x86)/java/jdk1.6.0_25/bin/jdb
Using auto-detected project path: .
Found package name: [application-pkg-name]
ABIs targetted by application: armeabi
Device API Level: 17
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi
Using gdb setup init: ./libs/armeabi/gdb.setup
Using toolchain prefix: /cygdrive/c/work/android/ndk/android-ndk-r9-windows-x86/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-
Using app out directory: ./obj/local/armeabi
Found debuggable flag: true
Found device gdbserver: /data/data/[applcation-pkg-name]/lib/gdbserver
Found data directory: '/data/data/[applcation-pkg-name]'
Found running PID: 17890
Launched gdbserver succesfully.
Setup network redirection
## COMMAND: adb_cmd shell run-as [applcation-pkg-name] lib/gdbserver +debug-socket --attach 17890
## COMMAND: adb_cmd forward tcp:5039 localfilesystem:/data/data/[applcation-pkg-name]/debug-socket
Attached; pid = 17890 [Comment: This pid is for the main application process]
Listening on Unix socket debug-socket
## COMMAND: adb_cmd pull /system/bin/app_process obj/local/armeabi/app_process
493 KB/s (9592 bytes in 0.019s)
Pulled app_process from device/emulator.
## COMMAND: adb_cmd pull /system/bin/linker obj/local/armeabi/linker
1165 KB/s (63244 bytes in 0.053s)
Pulled linker from device/emulator.
## COMMAND: adb_cmd pull /system/lib/libc.so obj/local/armeabi/libc.so
2442 KB/s (297608 bytes in 0.119s)
Pulled libc.so from device/emulator.
/cygdrive/c/work/android/ndk/android-ndk-r9-windows-x86/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
Couple of side notes that could be helpful-
1) If I run the ndk-gdb command without --force, it gives me error that Another debug session is running. Although the gdb prompt came out during the last run.
2) On Android 4.3, with the same application and setup, I get error that "Could not extract package's data directory"

Debug app segmentation fault? Compiler's fault?

I am currently compiling Android Linaro build 11.11 (staging-panda) for pandaboard.
In the build process, Android compiles some tools with the host gcc compiler. On my Linux Mint 12 (Ubuntu-11.10 based), I have gcc-4.6 installed by default.
I built Android, everything runs fine, pandaboard booted, but then starting any application will lead to segmentation fault (signal 11 in logcat).
I then learned that Linaro built this release with gcc-4.5, not 4.6 version. I installed it using apt-get. I removed out/ directory and rebuild Android entirely.
The compilation runs fine, but the linker insults me:
g++-4.5 -Wl,-rpath-link=out/target/product/pandaboard/obj/lib -Wl,-rpath,\$ORIGIN/../lib -Lout/host/linux-x86/obj/lib -Wl,--no-undefined -m32 out/host/linux-x86/obj/EXECUTABLES/acp_intermediates/acp.o -Wl,--whole-archive -Wl,--no-whole-archive out/host/linux-x86/obj/STATIC_LIBRARIES/libhost_intermediates/libhost.a -o out/host/linux-x86/obj/EXECUTABLES/acp_intermediates/acp
g++-4.5 -Wl,-rpath-link=out/target/product/pandaboard/obj/lib -Wl,-rpath,\$ORIGIN/../lib -Lout/host/linux-x86/obj/lib -Wl,--no-undefined -m32 out/host/linux-x86/obj/EXECUTABLES/mkbootfs_intermediates/mkbootfs.o -Wl,--whole-archive -Wl,--no-whole-archive -o out/host/linux-x86/obj/EXECUTABLES/mkbootfs_intermediates/mkbootfs
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.5.4/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.5.4/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.5.4/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.5.4/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: ld returned 1 exit status
The linker grabs the 64bit libraries of gcc-4.5, although it probably for 32bit version. So far, here are the things I tried, without success:
re-do the package installation following these instructions
sudo ldconfig
reboot my machine
I looked into removing gcc-4.6 to get only gcc-4.5 on my machine.
But synaptic showed so many dependencies needed to be uninstalled,
that I didn't make that step :)
Finally I've been told to install gcc-4.5-multilib and g++-4.5-multilib. It worked, and the build got further. I start the panda, still the apps (eg com.android.launcher) fails to launch. I know this is an assumption, but I think that this segfault is somehow linked to the compiler.
My questions are simple:
Is com.android.launcher cross-compiled or compiled with host 32bit
compiler? What keyword should I look for to find the associated command in the build log?
How can I debug this segmentation fault? In particular, I am looking
for starting com.android.launcher with the "am" command.
Will strace provide valuable information for this issue?
Thanks heaps.
Use gdb. Requirements:
# is the target shell, although being root is not mandatory
$ is the host shell
The app must be compiled in debug mode
LOCAL_CFLAGS += -g
start gdbserver on the target:
# gdbserver :5039 </system/bin/executable>
(or)
# gdbserver :5039 --attach <pid>
forward tcp port to adb connection:
$ adb forward tcp:5039 tcp:5039
start gdbclient on the host:
$ gdbclient :5039 <executable>
If you struggle with gdbclient, check build/envsetup.sh where the function is defined: $ type gdbclient Adding the verbose -v option might be of some help. Also if your executable is not in system/bin, you'll definitely need to modify build/envsetup.sh as it is hard-coded.
Some more information can be found here.

Categories

Resources