ndk-gdb sample?

1,305 views
Skip to first unread message

kcsasquatch

unread,
Jun 10, 2010, 3:40:29 PM6/10/10
to android-ndk
Hello,

I am trying to get ndk-gdb working under ubuntu 10.04, but it does not
work for me out of the box. To do the simplest test possible, I
modified the hello-jni example to add a null pointer reference:

---- code ----
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
int *i = 0;
*i = 2; // << Crash
return (*env)->NewStringUTF(env, "Hello from JNI!");
}
---- code ----

I expected ndk-gdb to trap with a nice stack trace, but I get an error
from gdb instead:

"/home/kevin/android/android-ndk-r4/samples/hello-jni/bin/ndk/local/
armeabi/gdb.setup:4: Error in sourced command file:
Remote communication error: Connection reset by peer."

I believe that the crash happens before gdb has a chance to attach to
the running process. I can fix this, yes, but does anyone have a
"known good" sample that shows ndk-gdb working? I really appreciate
the work that Google has already done to enable debugging in the ndk,
but a simple example would go a long way to making it easier to use.

Thanks,
Kevin

FYI, Here is a transcript of my session:


kevin@ubuntu:~/opt/ndk/samples/hello-jni$ ls -R
.:
AndroidManifest.xml build.xml default.properties jni libs
local.properties res src tests

./jni:
Android.mk hello-jni.c

./libs:
armeabi

./libs/armeabi:

./res:
values

./res/values:
strings.xml

./src:
com

./src/com:
example

./src/com/example:
hellojni

./src/com/example/hellojni:
HelloJni.java

./tests:
AndroidManifest.xml build.xml default.properties local.properties
src

./tests/src:
com

./tests/src/com:
example

./tests/src/com/example:
HelloJni

./tests/src/com/example/HelloJni:
HelloJniTest.java
kevin@ubuntu:~/opt/ndk/samples/hello-jni$ cat default.properties
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-8
kevin@ubuntu:~/opt/ndk/samples/hello-jni$ cat local.properties
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked in Version Control Systems,
# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read
the
# header note.
sdk.dir=/home/kevin/android/android-sdk-linux_86
kevin@ubuntu:~/opt/ndk/samples/hello-jni$ ../../ndk-build
Gdbserver : [arm-eabi-4.4.0] /home/kevin/android/android-ndk-r4/
samples/hello-jni/libs/armeabi/gdbserver
Gdbsetup : /home/kevin/android/android-ndk-r4/samples/hello-jni/
libs/armeabi/gdb.setup
Gdbsetup : + source directory /home/kevin/android/android-ndk-r4/
samples/hello-jni/jni
Compile thumb : hello-jni <= /home/kevin/android/android-ndk-r4/
samples/hello-jni/jni/hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => /home/kevin/android/android-ndk-r4/
samples/hello-jni/libs/armeabi
kevin@ubuntu:~/opt/ndk/samples/hello-jni$ ant install
Buildfile: build.xml
[setup] Android SDK Tools Revision 6
[setup] Project Target: Android 2.2
[setup] API level: 8
[setup] WARNING: Attribute minSdkVersion in AndroidManifest.xml
(3) is lower than the project target API level (8)
[setup] Importing rules file: platforms/android-8/ant/
ant_rules_r2.xml

-compile-tested-if-test:

-dirs:
[echo] Creating output directories if needed...
[mkdir] Created dir: /home/kevin/android/android-ndk-r4/samples/
hello-jni/gen
[mkdir] Created dir: /home/kevin/android/android-ndk-r4/samples/
hello-jni/bin/classes

-resource-src:
[echo] Generating R.java / Manifest.java from the resources...

-aidl:
[echo] Compiling aidl files into Java classes...

compile:
[javac] Compiling 2 source files to /home/kevin/android/android-
ndk-r4/samples/hello-jni/bin/classes

-dex:
[echo] Converting compiled files and external libraries into /
home/kevin/android/android-ndk-r4/samples/hello-jni/bin/classes.dex...

-package-resources:
[echo] Packaging resources
[aaptexec] Creating full resource package...

-package-debug-sign:
[apkbuilder] Creating hello-jni-debug-unaligned.apk and signing it
with a debug key...
[apkbuilder] Using keystore: /home/kevin/.android/debug.keystore

debug:
[echo] Running zip align on final apk...
[echo] Debug Package: /home/kevin/android/android-ndk-r4/samples/
hello-jni/bin/hello-jni-debug.apk

install:
[echo] Installing /home/kevin/android/android-ndk-r4/samples/
hello-jni/bin/hello-jni-debug.apk onto default emulator or device...
[exec] 1111 KB/s (78378 bytes in 0.068s)
[exec] pkg: /data/local/tmp/hello-jni-debug.apk
[exec] Success

BUILD SUCCESSFUL
Total time: 5 seconds
kevin@ubuntu:~/opt/ndk/samples/hello-jni$ ../../ndk-gdb
/home/kevin/android/android-sdk-linux_86/tools/adb shell ls /data/data/
com.example.hellojni/lib/gdbserver
PID=/home/kevin/android/android-sdk-linux_86/tools/adb shell ps | awk -
f /home/kevin/opt/ndk/build/awk/extract-pid.awk -v
PACKAGE=com.example.hellojni
/home/kevin/opt/ndk/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-
eabi-gdb -x /home/kevin/android/android-ndk-r4/samples/hello-jni/bin/
ndk/local/armeabi/gdb.setup -e /home/kevin/android/android-ndk-r4/
samples/hello-jni/bin/ndk/local/armeabi/app_process
GNU gdb 6.6
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "--host=x86_64-unknown-linux-gnu --
target=arm-elf-linux".
/home/kevin/android/android-ndk-r4/samples/hello-jni/bin/ndk/local/
armeabi/gdb.setup:4: Error in sourced command file:
Remote communication error: Connection reset by peer.
(gdb)



-------------------------
Logcat
-------------------------

D/AndroidRuntime( 1258): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
D/AndroidRuntime( 1258): CheckJNI is ON
D/AndroidRuntime( 1258): --- registering native functions ---
I/ActivityManager( 73): Starting activity: Intent { flg=0x10000000
cmp=com.example.hellojni/.HelloJni }
D/AndroidRuntime( 1258): Shutting down VM
D/dalvikvm( 1258): Debugger has detached; object registry had 1
entries
I/ActivityManager( 73): Start proc com.example.hellojni for activity
com.example.hellojni/.HelloJni: pid=1265 uid=10032 gids={1015}
I/AndroidRuntime( 1258): NOTE: attach of thread 'Binder Thread #3'
failed
D/dalvikvm( 1265): Trying to load lib /data/data/com.example.hellojni/
lib/libhello-jni.so 0x43dc1910
D/dalvikvm( 1265): Added shared lib /data/data/com.example.hellojni/
lib/libhello-jni.so 0x43dc1910
D/dalvikvm( 1265): No JNI_OnLoad found in /data/data/
com.example.hellojni/lib/libhello-jni.so 0x43dc1910, skipping init
I/DEBUG ( 31): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG ( 31): Build fingerprint: 'generic/sdk/generic/:2.2/
FRF42/36942:eng/test-keys'
I/DEBUG ( 31): pid: 1265, tid: 1265 >>> com.example.hellojni <<<
I/DEBUG ( 31): signal 11 (SIGSEGV), fault addr 00000000
I/DEBUG ( 31): r0 0000aa50 r1 00000002 r2 00000000 r3 420ff484
I/DEBUG ( 31): r4 bed8e910 r5 00000004 r6 428cf423 r7 4104bce0
I/DEBUG ( 31): r8 bed8e8f0 r9 4104bcd8 10 4104bcc0 fp 00000000
I/DEBUG ( 31): ip 80900b91 sp bed8e8e8 lr 80813978 pc
80900b96 cpsr 60000030
I/DEBUG ( 31): #00 pc 00000b96 /data/data/
com.example.hellojni/lib/libhello-jni.so
I/DEBUG ( 31): #01 pc 00013974 /system/lib/libdvm.so
I/DEBUG ( 31): #02 pc 0003ddf0 /system/lib/libdvm.so
I/DEBUG ( 31): #03 pc 000371b6 /system/lib/libdvm.so
I/DEBUG ( 31): #04 pc 000432ec /system/lib/libdvm.so
I/DEBUG ( 31): #05 pc 00018714 /system/lib/libdvm.so
I/DEBUG ( 31): #06 pc 0001e8c4 /system/lib/libdvm.so
I/DEBUG ( 31): #07 pc 0001d790 /system/lib/libdvm.so
I/DEBUG ( 31): #08 pc 0005402e /system/lib/libdvm.so
I/DEBUG ( 31): #09 pc 0005bd82 /system/lib/libdvm.so
I/DEBUG ( 31): #10 pc 00018714 /system/lib/libdvm.so
I/DEBUG ( 31): #11 pc 0001e8c4 /system/lib/libdvm.so
I/DEBUG ( 31): #12 pc 0001d790 /system/lib/libdvm.so
I/DEBUG ( 31): #13 pc 00053e8c /system/lib/libdvm.so
I/DEBUG ( 31): #14 pc 0004072c /system/lib/libdvm.so
I/DEBUG ( 31): #15 pc 00034454 /system/lib/libdvm.so
I/DEBUG ( 31): #16 pc 0002c8c0 /system/lib/
libandroid_runtime.so
I/DEBUG ( 31): #17 pc 0002d7ec /system/lib/
libandroid_runtime.so
I/DEBUG ( 31): #18 pc 00008c86 /system/bin/app_process
I/DEBUG ( 31): #19 pc 0000d362 /system/lib/libc.so
I/DEBUG ( 31):
I/DEBUG ( 31): code around pc:
I/DEBUG ( 31): 80900b74 e5bcf6a8 e28fc600 e28cca02 e5bcf6a0
I/DEBUG ( 31): 80900b84 e28fc600 e28cca02 e5bcf698 2102b510
I/DEBUG ( 31): 80900b94 60112200 68044b04 22a74904 0092447b
I/DEBUG ( 31): 80900ba4 58a31859 bd104798 00002664 ffffee00
I/DEBUG ( 31): 80900bb4 e5903000 e3530000 10833000 e1a00003
I/DEBUG ( 31):
I/DEBUG ( 31): code around lr:
I/DEBUG ( 31): 80813958 3497c004 3488c004 3afffff9 e2888004
I/DEBUG ( 31): 80813968 eafffff9 e899000c e594c008 e12fff3c
I/DEBUG ( 31): 80813978 e3550000 1594c00c 188c0003 e914a3f0
I/DEBUG ( 31): 80813988 e1a05e22 e5946004 e3a02000 e4d6c001
I/DEBUG ( 31): 80813998 e35c0000 0a000007 e2822001 e35c0044
I/DEBUG ( 31):
I/DEBUG ( 31): stack:
I/DEBUG ( 31): bed8e8a8 000013fc
I/DEBUG ( 31): bed8e8ac 000001b8
I/DEBUG ( 31): bed8e8b0 80813b00 /system/lib/libdvm.so
I/DEBUG ( 31): bed8e8b4 0000ccb0 [heap]
I/DEBUG ( 31): bed8e8b8 4104bcc0
I/DEBUG ( 31): bed8e8bc afd0be39 /system/lib/libc.so
I/DEBUG ( 31): bed8e8c0 00000000
I/DEBUG ( 31): bed8e8c4 b0004998 /system/bin/linker
I/DEBUG ( 31): bed8e8c8 b0014df4
I/DEBUG ( 31): bed8e8cc 80900b91 /data/data/
com.example.hellojni/lib/libhello-jni.so
I/DEBUG ( 31): bed8e8d0 000089ac /system/bin/app_process
I/DEBUG ( 31): bed8e8d4 002009c0 [heap]
I/DEBUG ( 31): bed8e8d8 00000000
I/DEBUG ( 31): bed8e8dc 00000014
I/DEBUG ( 31): bed8e8e0 df002777
I/DEBUG ( 31): bed8e8e4 e3a070ad
I/DEBUG ( 31): #00 bed8e8e8 bed8e910 [stack]
I/DEBUG ( 31): bed8e8ec 80813978 /system/lib/libdvm.so
I/DEBUG ( 31): #01 bed8e8f0 41255ecc /dev/ashmem/dalvik-
LinearAlloc (deleted)
I/DEBUG ( 31): bed8e8f4 0000ccb0 [heap]
I/DEBUG ( 31): bed8e8f8 428cf423 /data/dalvik-cache/
data@app@com.example.h...@classes.dex
I/DEBUG ( 31): bed8e8fc bed8e9d0 [stack]
I/DEBUG ( 31): bed8e900 80813b00 /system/lib/libdvm.so
I/DEBUG ( 31): bed8e904 0000ccb0 [heap]
I/DEBUG ( 31): bed8e908 bed8e910 [stack]
I/DEBUG ( 31): bed8e90c 8083ddf3 /system/lib/libdvm.so
D/Zygote ( 33): Process 1265 terminated by signal (11)
I/ActivityManager( 73): Process com.example.hellojni (pid 1265) has
died.
W/InputManagerService( 73): Window already focused, ignoring focus
gain of: com.android.internal.view.IInputMethodClient$Stub
$Proxy@43f37850
I/UsageStats( 73): Unexpected resume of com.android.launcher while
already resumed in com.example.hellojni

David Turner

unread,
Jun 10, 2010, 5:20:50 PM6/10/10
to andro...@googlegroups.com
Your process is probably crashing before gdbserver can attach to it.

You should try delaying the call to your crashing method. One way to do that is to set a Java breakpoint
in your onCreate() method before the stringFromJNI method is called. Or use waitForDebugger, or a
timed sleep.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


andreih

unread,
Jun 16, 2010, 9:56:29 AM6/16/10
to android-ndk
Hello Kevin and David,

Is there any news on this issue? I think I have the same problem
(running Snow Leopard with a N1 froyo device). I get the same error
but the process is not crashing, it just continues to run fine on N1.
It's not a stock froyo but as far as I can look the problem is not
related to the phone's image but rather with the gdbserver binary
going in the lib folder of my application.

As I can see from "ndk-gdb --verbose" the scripts calls this line at a
moment:

adb shell run-as com.my.app lib/gdbserver +debug-socket --attach

the output for this line is the following:

Usage: gdbserver COMM PROG [ARGS ...]
gdbserver COMM --attach PID

COMM may either be a tty device (for serial debugging), or
HOST:PORT to listen for a TCP connection.

As far as I can figure it out the gdbserver is not starting because
the arguments are not right and this goes to the broken pipe error in
the gdb client.

Any suggestions?

Thanks for any info.

Here is the whole gdb-ndk --verbose output:
Android NDK installation path: /Users/handrei/android/android-ndk-r4
Using default adb command: /Users/handrei/android/android-sdk-mac/
tools/adb
ADB version found: Android Debug Bridge version 1.0.26
Using final ADB command: '/Users/handrei/android/android-sdk-mac/tools/
adb'
Using auto-detected project path: .
Found package name: com.revo.sog
Found debuggable flag: true
ABIs targetted by application: armeabi-v7a
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi
Found device gdbserver: /data/data/com.revo.sog/lib/gdbserver
Using gdb setup init: /Users/handrei/android/android-ndk-r4/apps/sog/
project/libs/armeabi/gdb.setup
Using toolchain prefix: /Users/handrei/android/android-ndk-r4/build/
prebuilt/darwin-x86/arm-eabi-4.4.0/bin/arm-eabi-
Using app out directory: /Users/handrei/android/android-ndk-r4/apps/
sog/project/bin/ndk/local/armeabi
Found data directory: '/data/data/com.revo.sog'
Found first launchable activity: .SOGActivity
Launching activity: com.revo.sog/.SOGActivity
##### NEW COMMAND
/Users/handrei/android/android-sdk-mac/tools/adb shell am start -n
com.revo.sog/.SOGActivity
Starting: Intent { cmp=com.revo.sog/.SOGActivity }
##### NEW COMMAND
/Users/handrei/android/android-sdk-mac/tools/adb shell sleep 1
awk: calling undefined function gensub
source line number 33
Found running PID:
Launched gdbserver succesfully.
Setup network redirection
##### NEW COMMAND
/Users/handrei/android/android-sdk-mac/tools/adb forward tcp:5039
localfilesystem:/data/data/com.revo.sog/debug-socket
##### NEW COMMAND
/Users/handrei/android/android-sdk-mac/tools/adb shell run-as
com.revo.sog lib/gdbserver +debug-socket --attach
##### NEW COMMAND
adb pull /system/bin/app_process /Users/handrei/android/android-ndk-r4/
apps/sog/project/bin/ndk/local/armeabi/app_process
Usage: gdbserver COMM PROG [ARGS ...]
gdbserver COMM --attach PID

COMM may either be a tty device (for serial debugging), or
HOST:PORT to listen for a TCP connection.
534 KB/s (5680 bytes in 0.010s)
Pulled from device/emulator.
GNU gdb 6.6
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "--host=i386-apple-darwin9.8.0 --target=arm-
elf-linux".
/Users/handrei/android/android-ndk-r4/apps/sog/project/bin/ndk/local/
armeabi/gdb.setup:4: Error in sourced command file:
Remote communication error: Connection reset by peer.
(gdb)


> ...
>
> read more »

David Turner

unread,
Jun 16, 2010, 10:01:14 AM6/16/10
to andro...@googlegroups.com
On Wed, Jun 16, 2010 at 6:56 AM, andreih <han...@gmail.com> wrote:
Hello Kevin and David,

Is there any news on this issue?

A bugfix release is on its way. Hopefully it will be available soon, but I can't give you a date yet.
 
I think I have the same problem
(running Snow Leopard with a N1 froyo device). I get the same error
but the process is not crashing, it just continues to run fine on N1.
It's not a stock froyo but as far as I can look the problem is not
related to the phone's image but rather with the gdbserver binary
going in the lib folder of my application.

As I can see from "ndk-gdb --verbose" the scripts calls this line at a
moment:

adb shell run-as com.my.app lib/gdbserver +debug-socket --attach

the output for this line is the following:

Usage:  gdbserver COMM PROG [ARGS ...]
       gdbserver COMM --attach PID

COMM may either be a tty device (for serial debugging), or
HOST:PORT to listen for a TCP connection.


that's because the line is missing a PID number after the --attach. Due to a script bug.
 

--

Romkin

unread,
Jul 5, 2010, 7:38:18 AM7/5/10
to android-ndk
is that possible you can show how to modify the script to try it out
while the fix is coming?

On Jun 16, 3:01 pm, David Turner <di...@android.com> wrote:
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages