Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gdb debug the native (not jni) program on android

I failed to debug the native program with NDK toolchain. Follows are my detailed steps and output.

Env Setting:

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    }

Build by the standalone toolchain provied by NDK.

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

Push to the emulator and start the gdbserver (I forward the port already)

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

Debug 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 test it with NDK Android.mk style, it woks fine. Here is the output

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, push to emulator, start debug server

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

Debug remote:

#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 build with Android.mk by ndk-build, when i do something else in gdb remote, 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 am fresh on android, anyone can tell me what is happening ?

like image 208
Peng Liang Avatar asked Nov 05 '22 17:11

Peng Liang


1 Answers

I don't know why gdb works with the binary by ndk-build, but use file command instead of symbol-file command, it might work. gdb needs to know the image of the remote executed program.

(gdb) file hello
like image 58
Kazuki Sakamoto Avatar answered Nov 14 '22 22:11

Kazuki Sakamoto