我无法使用NDK工具链调试本机程序。以下是我的详细步骤和输出。
环境设定:
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
资料来源:hello.c
1 #include <stdio.h>
2
3 int main() {
4 printf("Hello World!\n");
5 return 0;
6 }
由NDK提供的独立工具链构建。
#arm-linux-androideabi-gcc -g hello.c -o hello --sysroot $SYSROOT
推送到模拟器并启动gdbserver(我已将端口转发)
#adb push hello /data/hello
#adb shell gdbserver 10.0.2.15:10000 /data/hello
在另一个终端远程调试:
#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 ?? ()
我用NDK Android.mk风格测试它,它很好。这是输出
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)
构建,推送到模拟器,启动调试服务器
#ndk-build
#push obj/local/armeabi/hello /data/hello
#adb shell gdbserver 10.0.2.15:10000 /data/hello
调试远程:
#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.
仍然使用Android.mk通过ndk-build构建,当我在gdb remote中执行其他操作时,仍然失败。
(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
=============================================== ================
我对android很新鲜,有人能告诉我发生了什么吗?
答案 0 :(得分:2)
我不知道为什么gdb通过ndk-build使用二进制文件,但是使用file命令而不是symbol-file命令,它可能会起作用。 gdb需要知道远程执行程序的图像。
(gdb) file hello