I have an android application that consists of a Java based APK, native executable, and native library. The apk talks to the native (root NDK c/c++) executable and library over a socket.
I'm not sure if it matters but the executable and library are compiled via cmake, and copied to be executable and then run as root. I need to get some type of debugging going with breakpoints and such, regardless of if it's directly in android studio or via command line.
You would need to run gdbserver on the device and let it attach to your executable
gdbserver comes prebuilt with ndk, usually under <ndk>/prebuilt/android-arm/gdbserver/
Copy gdbserver binary to your device, for instance to /data/local/tmp and give it executable permissions with chmod
If your executable is already running, find its PID with ps command and attach gdb to it:
gdbserver :5039 --attach <PID>
Note that 5039 is port number that is usually used for debugging with gdb, you can use your own if you like
set up a port forwarding from device to pc with
adb forward tcp:5039 tcp:5039
Run gdb locally, note that you need arm targeted gdb that comes with ndk too, usually at
<ndk>toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gdb
Attach gdb to your process
target remote :5039
And from here you need to use gdb commands that match your debugging expectations (set breakpoints, load symbols, step through etc), for examples use cheatsheet or ask in comments
Thanks to cmake-android-chain, I can build a C++ program successfully. By using adb commands, I can also make the program run in Android devices:
adb push c_plus_plus_program /data/local/tmp/c_plus_plus_program
adb shell chmod -777 /data/local/tmp/c_plus_plus_program
adb shell ./data/local/tmp/c_plus_plus_program
Now I want to debug this program in Eclipse. With cmake, I can create a project for Eclipse, and I have also succeeded in building the project. However, the problem is I do not know how to run the program insider Eclipse, hence debugging the program. Any ideas? Thanks.
I used ant to build my Eclipse project from the command line in a debug configuration. (Eclipse Luna and Android NDK-R10d is broken, so I can't use Eclipse any longer. Confer, Eclipse/ADT plugin cannot locate symbols for r10d NDK).
I then performed an install using adb:
<Project Directory>$ adb install bin/AndroidPrng-debug.apk
When I rummage for the program on the device in Apps, the program is not offered. When I attempt to search for it by name on the device (AndroidPrng and com.example.prng), I'm provided with useless web search results. When I go to Settings → Apps, the app is shown under the Downloaded tab (it shows the name as com.example.prng). It has the familiar Force Stop and Uninstall.
I have DDMS running and waiting to capture LogCat output from the program. But even though the app is on the device, I cannot figure out how to run it.
How do I run and debug the app when it does not show up under Apps?
Assuming that your app do have an Activity from where you can navigate into other parts of your app.
Try using below command:
$ adb shell am start -n com.example.yourpackagename/.YourMainActivity
or $ adb shell am start -n com.package.yourpackagename/com.example.yourpackagename.YourMainActivity
This am start command, is a command-line interface to the ActivityManager.
I would like to perform debug operations on the Android open source platform.
I am trying to run "gdbserver :5039 --attach" in my terminal but I keep receiving "command not found".
I have built the Android OS using the "full_crespo-userdebug" configuration, which according to the android docs, should provide me with root access on my Nexus S phone?
How can I set things up so that I can debug?
You can copy it from "$NDK_HOME/prebuild/$PLATFORM/gdbserver/gdbserver"
then use adb to push it to device and make it executable
To debug an android device you first need to run gdbserver on the device.
gdbserver :5039 --attach pid
then in your gingerbread source folder you need to run
source build/envsetup.sh
this will allow you to now run
gdbclient
which should connect to the gdbserver on device
I have a C++ Android application that I'm trying to debug with ndk-gdb. The application does use multiple threads, but supposedly r5 of the ndk supports multiple threads. Also, I'm not even getting to the point where gdb starts up. I run the command:
ndk-gdb --start --force --verbose
It then finds the proper path for the ndk and sdk (or at least adb), and the needed ABIs and whatnot.
$ ndk-gdb --start --force --verbose
Android NDK installation path: /home/leif/eclipse/android-ndk-r5b
Using default adb command: /home/leif/eclipse/android-sdk-linux_86/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.26
Using final ADB command: '/home/leif/eclipse/android-sdk-linux_86/platform-tools/adb'
Using auto-detected project path: .
Found package name: net.leifandersen.mobile.android.marblemachine
ABIs targetted by application: armeabi
Device API Level: 10
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi
It then looks for gdb server, and finds it, including the proper PID, followed by starting the activity.
But then, it tells me that the the package cannot be found:
Setup network redirection
## COMMAND: /home/leif/eclipse/android-sdk-linux_86/platform-tools/adb shell run-as <package name> lib/gdbserver +debug-socket --attach 16040
## COMMAND: /home/leif/eclipse/android-sdk-linux_86/platform-tools/adb forward tcp:5039 localfilesystem:run-as: Package '<package name>' is unknown/debug-socket
It then spits out what you would get if you improperly use adb (the help file), followed by:
ERROR: Could not setup network redirection to gdbserver?
Maybe using --port=<port> to use a different TCP port might help?
run-as: Package '<package name>' is unknown
I looked into /data/system/packages.list, and yes, my apk is most certainly in there, and the location it's pointing to is correct on the file system. So that's not the problem.
This tutorial: http://vilimpoc.org/blog/2010/09/23/hello-gdbserver-a-debuggable-jni-example-for-android/ recommends deleting and reinstalling, as well as cleaning your eclipse build.
I didn't use eclipse to build the package, but I did clean everything out and compile from scratch, deleted, and reinstalled to no luck.
Has anyone had similar problems, and how did you resolve them? Thank you.
Edit: Oh, and I have tried a different port to no avail, there does not appear to be anything on 5039 (the default port) anyway. And afaik, I don't have any firewalls blocking that connection. I'm developing on Ubuntu 11.04 as well.
Edit2: Hmm...it looks like with the new ndk (r5c), the error message has now changed too:
ERROR: Could not extract package's data directory. Are you sure that
your installed application is debuggable?
And yes, debuggable is set to true in the manifest, and all of the native code is built with:
LOCAL_CFLAGS := -Wall -g
LOCAL_LDFLAGS := -Wl,-Map,xxx.map
run-as: Package 'net.leifandersen.mobile.android.marblemachine' is unknown
Thus, unfortunately, your device is not able to be used with ndk-gdb, because run-as doesn't work. If you want to use that device, you must have root privilege.
EDITED:
Modify ndk-gdb script to get rid of the dependency of run-as. It works only on root privilege ('adb shell whoami' should be 'root').
--- ndk-gdb 2011-02-24 16:55:07.000000000 +0900
+++ ndk-gdb-root 2011-06-09 08:35:04.000000000 +0900
## -465,7 +465,7 ##
log "Using app out directory: $APP_OUT"
# Find the <dataDir> of the package on the device
-DATA_DIR=`adb_shell run-as $PACKAGE_NAME /system/bin/sh -c pwd`
+DATA_DIR="/data/data/$PACKAGE_NAME"
log "Found data directory: '$DATA_DIR'"
if [ $? != 0 -o -z "$DATA_DIR" ] ; then
echo "ERROR: Could not extract package's data directory. Are you sure that"
## -543,7 +543,7 ##
# Launch gdbserver now
DEBUG_SOCKET=debug-socket
-run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
+run $ADB_CMD shell "(cd $DATA_DIR; lib/gdbserver +$DEBUG_SOCKET --attach $PID)" &
if [ $? != 0 ] ; then
echo "ERROR: Could not launch gdbserver on the device?"
exit 1
There is a bug with run-as, it will fail if you have too many apps installed. I was able to work around this problem by removing some apps from my Evo 4G. I found this in the NDK discussion groups - http://groups.google.com/group/android-ndk/browse_thread/thread/ae9e8d5fe2716ae6?pli=1
I had the same issue today with Samsung Galaxy S running MIUI rom. ndk-gdb always reported "Could not extract package's data directory. Are you sure that your installed application is debuggable?"
It turned out the reason is that run-as not working due to /data/data symlink. Cyanogen is used in the customized ROM. Removing the symbolic link and move all files from /datadata to /data/data solved the problem.
Cyanogen 2.3 fix:
ndk-gdb relies on the 'run-as' command, which itself makes a number of checks on the /data/data directory. In Cyanogen 2.3, it's a symlink, and run-as fails with a cryptic message, and ndk-gdb fails in return with [2]:
ERROR: Could not extract package's data directory. Are you sure that
your installed application is debuggable?
A work-around is to recreate /data/data with symlink :
cd /data/data /datadata.break-run-as
mkdir -m 771 /data/data/
chown system: v
mv /datadata/* /data/data/
http://en.wikibooks.org/wiki/OpenGL_Programming/Installation/Android_NDK
http://forum.cyanogenmod.com/topic/27657-run-as-not-working-due-to-datadata-symlink/
Hope it helps others with similar issue. Check to see if run-as works as expected or not. It's not because your binary is not debuggable. ndk-gdb's error message is very misleading.
Had a similar problem and running:
adb shell run-as com.mypackagename /system/bin/sh -c pwd
would output:
run-as: Package 'com.mypackagename' has corrupt installation
fix was to uninstall on device then reinstall from command line via:
adb install MyApkFile.apk
I also experienced this problem and discovered that it can be caused by short package names!
When testing on an Android 2.2 system with an application that had a package with 3 levels (e.g: a.b.c) ndk-gdb would not work. Changing the package to have 4 or more levels (e.g. a.b.c.d) or running on Android 2.3 or later resolved the issue.
see http://code.google.com/p/android/issues/detail?id=13965 for more information.
There is still another posibility for this problem to occur: if you have previously installed your application as a system app (in /system/app), uninstalled it and then installed it once again as a normal application. In that case it is posible that there is still some files remaining that your application cannot access because it has no permissions.
I solved it by uninstalling my application and manually removing every piece of information related to it (with adb shell and root privileges). As far as I know, that comprises:
Everything under /data/data/<your app package>
The file named /data/dalvik-cache/* <your app package>*
After installing it again, I was able to debug the application again.
In case anyone is using Samsung Galaxy S4/... and got 4.4.2 (latest stock rom - by now at all countries) - you got screwed! Samsung bug. So root as explained in one of the answers above, or get another device...
Another solution is to revert to Android 4.2.2 (not 4.4.2) - pre 4.3 version which started this issue.