Debugging Android NDK native apps - android

I'm trying to debug and step through an Android application that segfaults. I've tried ndk-gdb, but with little luck. I've also referred to Android NDK Debugging without being able to debug my app.
When I try ndk-gdb --start, and I get:
$ ndk-gdb --start --verbose
Android NDK installation path: /opt/android-ndk-r7
Using default adb command: /opt/android-sdk-linux/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.29
Using final ADB command: '/opt/android-sdk-linux/platform-tools/adb'
Using auto-detected project path: .
Found package name: com.example.native_plasma
ABIs targetted by application: armeabi armeabi-v7a
Device API Level: 10
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi-v7a
Found debuggable flag: true
Found device gdbserver: /data/data/com.example.native_plasma/lib/gdbserver
Using gdb setup init: ./libs/armeabi-v7a/gdb.setup
Using toolchain prefix: /opt/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-
Using app out directory: ./obj/local/armeabi-v7a
Found data directory: '/data/data/com.example.native_plasma'
Found first launchable activity: android.app.NativeActivity
Launching activity: com.example.native_plasma/android.app.NativeActivity
## COMMAND: /opt/android-sdk-linux/platform-tools/adb shell am start -n com.example.native_plasma/android.app.NativeActivity
Starting: Intent { cmp=com.example.native_plasma/android.app.NativeActivity }
## COMMAND: /opt/android-sdk-linux/platform-tools/adb shell sleep 2
Found running PID: 0
ERROR: Could not extract PID of application on device/emulator.
Weird, this probably means one of these:
- The installed package does not match your current manifest.
- The application process was terminated.
Try using the --verbose option and look at its output for details.
This indicates that the application segfaulted more less, but I don't know how to set a breakpoint here since gdb never actually gives a prompt.
I also tried this command:
$ ../../toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-addr2line -f -e libs/armeabi/libnative-plasma.so
bedb2330
??
??:0
I have debug symbols I believe.
ndk-build -B V=1 APP_OPTIM=debug
Android.mk in jni/ has LOCAL_CFLAGS := -g
ant debug
I've also ndk-build NDK_DEBUG=1 but I still get where it looks like I don't have debug symbols.
Here's an image of the stack trace. It doesn't get any more informative:

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.

In latest versions of NDK and Eclipse plug-in you can right click on package and choose Debug as -> Android Native Application

Make sure that you load your native library in either a launchable activity or in your Application class. Otherwise it wouldn't work and you'll get the following error No symbol table is loaded. Use the "file" command..
For example in Application class:
import android.app.Application;
public class MyApp extends Application {
static {
System.loadLibrary("Name");
}
public static native int doSomething();
}
Name is the name of your library (.so file) without the lib part.

I resolved putting --nowait option to the shell command:
ndk-gdb --start --verbose --nowait

Related

ndk-gdb cannot find gdb.setup but it is there under x86

Cocos2d-x 3.7.1 + ndk r10e project on Mac trying to debug in Eclipse. Days pass and the errors keep coming...
I can build and run using cocos compile and cocos run with -p android -m debug --ndk-mode NDK_DEBUG=1 from the terminal in proj.android.
My app runs Ok (eventually) on my x86 emulator no problem.
Typing 'ndk-gdb' in the terminal from ./proj.android and I get the error
ERROR: Could not find gdb.setup under ./libs/
BUT the file gdb.setup (and gdbserver & libcocos2dcpp.so) can be seen in ./proj.android/libs/x86 !?
My AndroidManifest.xml includes android:debuggable="true"
My jni/Application.mk includes APP_ABI := x86 (only)
If anyone can tell me how I can diagnose or fix this problem then I guess I owe them a beer (in Yorkshire btw)
UPDATE: I have tried running up GDB using the --verbose flag. The output shows
Using gdb setup init: ./libs/armeabi/gdb.setup
Using app out directory: ./obj/local/armeabi
Guess I need to change to settings/init file somewhere??
I ran into the same problem. It seems something changed in recent version of Android Build tools.
The trick is:
ERROR: Could not find gdb.setup under ./libs/
It expects gdb.setup is in the directory libs but NOT libs/armeabi
so the simple workground is to copy gdb.setup and gdbserver from "libs/cpu" to "libs" and it works like a magic!
This is a bug caused by adb from android sdk.Issue 191085: ndk-gdb issue - Could not find gdb.setup under ./libs/ (even though it's there)
Currently previewl channel's adb whose version is 1.0.35 still has the same problem.
Without revert adb to 1.0.31.A simple workaround would be modify ndk-gdb a little: change the line if [ $? = 0 ]; then which is below adb_var_shell BCFILES run-as $PACKAGE_NAME /system/bin/sh -c "ls lib/*.bc" to if [ $? = 1 ]; then

How do I build Libgdx.so from source for Android with gdb tracing enabled?

I have setup a debug ROM on an Android device, and enabled the DDMS Native Heap in search for a libgdx memory leak.
I now have a trace, but no source code attached to follow the lead.
I downloaded libgdx source code.
How do I build it enabling gdb tracing so I can follow the code referenced by the trace ?
Update:
I built the debug .so libgdx from source
To do this, I modified the file:
libgdx/gdx/jni/build-android32.xml
Adding to the compile-natives target
I also added APP_OPTIM := debug to the Application.mk in the same folder
And added '-g' to the Android.mk file in the same folder:
LOCAL_CFLAGS := -g $(LOCAL_C_INCLUDES:%=-I%) -O2 -Wall -D__ANDROID__
This, indeed, produces a dbg libgdx.so dynamic library place in
libgdx/gdx/libs/armeabi
Although I am getting closer, I still cannot get the name of the function that is loading memory.
I am using arm-linux-androideabi-addr2line and the Hex address of the function, but it prints
??
Download the Android Source and build it.
Point DDMS to the libs with debug symbols. On the command line:
export ANDROID_SYMBOLS=$ANDROID_SOURCE/out/target/product/flo/symbols/system/lib
Note that $ANDROID_SOURCE refers to the location where you built the Android source.
start DDMS from that shell
$ddms
Now you should see the native traces on ddms.
I also built libgdx from source and added $LIBGDX_SOURCE/libgdx/gdx/obj/local/armeabi/libgdx.so
to $ANDROID_SOURCE/out/target/product/flo/symbols/system/lib to see the method names for libgdx.so.
Preliminaries
You need to set the device to debug memory
adb root
adb shell setprop libc.debug.malloc 1
adb shell stop
adb shell start
The device must be rooted or with a dbg ROM.

Install Calabash-Android causes error on OS X Mavericks

I need some help with trying to install Calabash-Android. After installing rvm, home-brew, when I install Calabash i get a strange error:
sudo gem install calabash-android
Building native extensions. This could take a while...
ERROR: Error installing calabash-android:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for main() in -lc... yes
creating Makefile
make "DESTDIR="
compiling gherkin_lexer_ar.c
ragel/i18n/ar.c.rl:440:1: warning: control may reach end of non-void function [- Wreturn-type]
}
^
ext/gherkin_lexer_ar/gherkin_lexer_ar.c:864:18: warning: unused variable 'lexer_en_main' [-Wunused-const-variable]
static const int lexer_en_main = 1;
^
2 warnings generated.
linking shared-object gherkin_lexer_ar.bundle
clang: error: unknown argument: '-multiply_definedsuppress' [-Wunused-command-line- argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
make: *** [gherkin_lexer_ar.bundle] Error 1
Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/gherkin-2.12.2 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/gherkin- 2.12.2/ext/gherkin_lexer_ar/gem_make.out
doronkatz#Dorons-MacBook-Air ~ $ mkdir ~/android
doronkatz#Dorons-MacBook-Air ~ $ mkdir ~/android
mkdir: /Users/doronkatz/android: File exists
doronkatz#Dorons-MacBook-Air ~ $ sudo gem install calabash-android
Password:
Building native extensions. This could take a while...
ERROR: Error installing calabash-android:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for main() in -lc... yes
creating Makefile
make "DESTDIR="
compiling gherkin_lexer_ar.c
ragel/i18n/ar.c.rl:440:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
ext/gherkin_lexer_ar/gherkin_lexer_ar.c:864:18: warning: unused variable 'lexer_en_main' [-Wunused-const-variable]
static const int lexer_en_main = 1;
^
2 warnings generated.
linking shared-object gherkin_lexer_ar.bundle
clang: error: unknown argument: '-multiply_definedsuppress' [-Wunused-command-line- argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
make: *** [gherkin_lexer_ar.bundle] Error 1
Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/gherkin-2.12.2 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/gherkin- 2.12.2/ext/gherkin_lexer_ar/gem_make.out
Not sure what the error means, I have installed Xcode tools for mavericks as well. Any thoughts out there?
I had the same problem and I just found out a solution for this.
I found one related problem here:
http://techespanto.wordpress.com/2013/03/29/upgrade-ruby-version-on-mac-osx/
and it suggested I had to upgrade the ruby version on my machine. The first two commands in that link are not working, so I leave you here what I did:
Install the command line tools for mac with:
xcode-select --install
Install homebrew in your mac with:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Install rvm with:
\curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enable
List the latest ruby versions with:
rvm list known
Install the latest ruby version with:
rvm install ruby-<version>
(for me it was ruby-2.1-head)
And that's all. Hope it works for you as well!
You can also try the solution from https://stackoverflow.com/a/22787223/1165581
I looked at the calabash-ios google group for you and I think I have the solution for your
Try installing it like this
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install calabash-cucumber
If that fails you can try to downgrade xcode from 5.1 to one of the previous(Preferences->Locations).
And the reference link

No emulator after building Android kernel

I followed the directions from http://source.android.com/source/building.html. However once I get to "Emulate an Android Device" section, there is no "emulator".
During the build, I got Killed messages:
/bin/bash: line 1: 5372 Killed javac -J-Xmx512M -target 1.5 -Xmaxerrs >9999999 -encoding ascii -g -classpath out/host/linux-x86/framework/dx.jar:out
/host/linux->x86>/framework/dasm.jar:out/host/linux-x86/framework/cfassembler.jar:/usr/lib
/jvm/java-1.5.0>-sun/lib/tools.jar -extdirs "" -d out/host/common/obj/JAVA_LIBRARIES
/cts-dalvik-buildutil_intermediates/classes \#out/host/common/obj/JAVA_LIBRARIES/cts-dalvik-
buildutil_intermediates/java-source-list-uniq
make: * [out/host/common/obj/JAVA_LIBRARIES/cts-dalvik-buildutil_intermediates/javalib.jar] Killed
This is the first javac command that follows the C++ compilations.
I've retried it multiple times without success.
The problem resulted from paths not being properly exported.
I followed this link: http://blog.markloiseau.com/2012/07/envsetup-setpaths-and-the-emulator-avd-error/
and set ANDROID_PRODUCT_OUT=/out/target/product/generic/
and also emulator is located at out/host/linux-x86/bin/emulator

Android NDK debugging: armeabi-v7a not working

Eclipse / Cygwin
NDK 8c
Building a shared library
I can't get gdbserver to start anymore after switching to armeabi-v7a. I've searched online for hours but can't find a topic that deals specifically with armeabi-v7a debugging issues.
I have no choice to switch to armeabi-v7a due to using a third party library which depends on it. Without it, I get these kind of errors:
D:\TEMP\ccnnGAqD.s:10427: Error: selected processor does not support Thumb mode `ldrex r6,[r3]'
D:\TEMP\ccnnGAqD.s:10429: Error: selected processor does not support Thumb mode `strex r4,r5,[r3]'
It was all working fine before with 'armeabi', using this setup: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
The only change I've made is to add this to Application.mk:
APP_ABI := armeabi-v7a
At the very bottom of the shared library Android.mk I added this:
$(info TARGET_ARCH = $(TARGET_ARCH))
$(info TARGET_ARCH_ABI = $(TARGET_ARCH_ABI))
$(info TARGET_ABI = $(TARGET_ABI))
which outputs the following:
TARGET_ARCH = arm
TARGET_ARCH_ABI = armeabi-v7a
TARGET_ABI = android-14-armeabi-v7a
I've uninstalled the app using
adb uninstall com.example.game
AndroidManifest.xml does have the android:debuggable="true" property.
Done a "clean all" in Eclipse, and manually deleted the ./libs and ./obj folders. Then, ndk-build outputs to the right folders (obj/local/armeabi-v7a and libs/armeabi-v7a), and obj/local/armeabi and libs/armeabi do not exist.
However, here's what happens when I run ndk-gdb:
user#MACHINENAME /cygdrive/e/projects/game
$ ndk-gdb-eclipse --force --verbose
Android NDK installation path: /cygdrive/e/projects/sdks/android-ndk
Using default adb command: /cygdrive/e/projects/sdks/android-sdk/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.31
Using ADB flags:
Using auto-detected project path: .
Found package name: com.example.game
ABIs targetted by application: armeabi
Device API Level: 15
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi
Using gdb setup init: ./libs/armeabi/gdb.setup
Using toolchain prefix: /cygdrive/e/projects/sdks/android-ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-
Using app out directory: ./obj/local/armeabi
Found debuggable flag: true
ERROR: Could not find gdbserver binary under ./libs/armeabi
This usually means you modified your AndroidManifest.xml to set
the android:debuggable flag to 'true' but did not rebuild the
native binaries. Please call 'ndk-build' to do so,
*then* re-install to the device!
Notice the "ABIs targetted by application" using the wrong 'armeabi'. Here's the relevant part of ndk-gdb:
get_build_var ()
{
if [ -z "$GNUMAKE" ] ; then
GNUMAKE=make
fi
$GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 | tail -1
}
APP_ABIS=`get_build_var APP_ABI`
if [ "$APP_ABIS" != "${APP_ABIS%%all*}" ] ; then
# replace first "all" with all available ABIs
ALL_ABIS=`get_build_var NDK_ALL_ABIS`
APP_ABIS_FRONT="${APP_ABIS%%all*}"
APP_ABIS_BACK="${APP_ABIS#*all}"
APP_ABIS="${APP_ABIS_FRONT}${ALL_ABIS}${APP_ABIS_BACK}"
fi
log "ABIs targetted by application: $APP_ABIS"
I clearly set APP_ABI to armeabi-v7a in Application.mk, so is this a bug in the NDK? Or am I missing something?
I had the same issue. I configured eclipse following this article.
Then I change from armeabi to armeabi-v7a. Then I couldn't debug.
I fixed this issue:
1) You must fix the folders in "Debug configurations"
Main tab change ...obj/local/armeabi/app_process to ...obj/local/armeabi-v7a/app_process
Debugger tab change ...obj/local/armeabi/gdb2.setup to obj/local/armeabi-v7a/gdb2.setup
Debugger tab change .../toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb to toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb
2) May be this is workaround but it works. In "Debug configurations" ->Debugger->Shared Libraries add <project path>/obj/local/armeabi-v7a and check "Load shared library symbols automatically"

Categories

Resources