Gdb 调试 Android 上的本机(不是 jni)程序
我无法使用 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.
仍然通过 ndk-build 使用 Android.mk 构建,当我在 gdb 远程中执行其他操作时,仍然失败。
(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很陌生,任何人都可以告诉我发生了什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么 gdb 通过 ndk-build 处理二进制文件,但是使用 file 命令而不是符号文件命令,它可能会工作。 gdb 需要知道远程执行程序的映像。
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.