5

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 ?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, no segmentation fault ever, but instead, the breakpoints does not work anymore, continue command will run the program till exited. It ignores all my breakpoints.
Hmm, I'm using android-ndk-r5b-darwin-x86 and Nexus S/Android 2.3.4. It works fine with breakpoints like 'b main', 'b hello.c:5'.
do you also use the standalone tool-chain? It is interesting, i am trying to figure out what is happening.
Yes, I did exactly what you wrote, except 'file hello' and the path (/data/local/tmp/hello).
Ok, it seems the problem is the gdbserver version does not match with my gdb. I use the gdb from NDK r6, but the gdbserver was from the emulator. After push the matched gdbserver from NDK to emulator, problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.