Gdb debugs native (not jni) Android program

I was not able to debug my own program using the NDK toolkit. Below are my detailed steps and conclusion.

Env Settings:

NDK_ROOT=/opt/android/ndk
SYSROOT=$NDK_ROOT/platforms/android-8/arch-arm
TOOLCHAIN=$NDK_ROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
PATH=$TOOLCHAIN:$NDK_ROOT:$PATH

Source: hello.c

 1    #include <stdio.h>
 2   
 3    int main() {
 4      printf("Hello World!\n");
 5      return 0;
 6    }

Create a standalone toolchain installed by the NDK.

#arm-linux-androideabi-gcc -g hello.c -o hello --sysroot $SYSROOT 

Click on the emulator and run gdbserver (I am already forwarding the port)

#adb push hello /data/hello
#adb shell gdbserver 10.0.2.15:10000 /data/hello

Debugging remotely in another terminal:

#arm-linux-androideabi-gdb
#(gdb) target remote localhost:10000
Remote debugging using :10000
0xb0001000 in ?? ()       ------------------------------------what is this?
#(gdb) symbol-file hello
Reading symbols from hello...done.
#(gdb) l
1    #include <stdio.h>
2   
3    int main() {
4      printf("Hello World!\n");
5      return 0;
6    }
#(gdb)  b main
Breakpoint 1 at 0x8318: file hello.c, line 4.
#(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.  -------It should be break at main function, but segmentation falut.  

0xafd0f5f0 in ?? ()
#(gdb) bt
#0  0xafd0f5f0 in ?? ()

And I am testing it using the NDK style of Android.mk, it works great. Here is the conclusion

Android.mk

1. LOCAL_PATH := $(call my-dir)
2.
3. include $(CLEAR_VARS)
4.
5. LOCAL_MODULE    := hello
6. LOCAL_SRC_FILES := hello.c
7. LOCAL_MODULE_TAGS := optional
8.
9. include $(BUILD_EXECUTABLE)

Build, click on the emulator, start the debug server

   #ndk-build
   #push obj/local/armeabi/hello /data/hello
   #adb shell gdbserver 10.0.2.15:10000 /data/hello

Debugging a remote device:

#arm-linux-androideabi-gdb
(gdb) target remote :10000
Remote debugging using :10000
0xb0001000 in ?? ()  --------------still here
(gdb) symbol-file hello
Reading symbols from hello...done.
(gdb) l
1    #include <stdio.h>
2   
3    int main() {
4      printf("Hello World!\n");
5      return 0;
6    }
(gdb) b main
Breakpoint 1 at 0x8372: file hello.c, line 4.
(gdb) c
Continuing.

Breakpoint 1, main () at hello.c:4
4      printf("Hello World!\n");
(gdb) c
Continuing.

Program exited normally.      -------Yes, erverything is normal, Hello World is output.

Still create using Android.mk ndk-build, when I do something else in gdb remote mode, it still failed.

(gdb) target remote :10000
Remote debugging using :10000
0xb0001000 in ?? ()
(gdb) symbol-file hello
Reading symbols from hello...done.
(gdb) l
1    #include <stdio.h>
2   
3    int main() {
4      printf("Hello World!\n");
5      return 0;
6    }
(gdb) b main
Breakpoint 1 at 0x8372: file hello.c, line 4.
(gdb) next
Cannot access memory at address 0x0
Cannot find bounds of current function
(gdb) c
Continuing.

Breakpoint 1, main () at hello.c:4
4      printf("Hello World!\n");
(gdb) next
6    }
(gdb) next

Program received signal SIGSEGV, Segmentation fault. ------Again fault. And no "Hello World" output in gdbserver.
0x0000832c in ?? ()
(gdb) next
Cannot find bounds of current function

==================================================== ==============

I'm on android, can anyone tell me what's going on?

+5
1

, gdb ndk-build, symbol-file file, . gdb .

(gdb) file hello
+2

All Articles