2015年6月25日星期四
2015年6月18日星期四
Android native debug: 手动使用gdbserver远程调试C代码
有时候就要调试一个Android里的C程序,和java做成的APK一点关系都没有,这种情况下,查找到的帮助信息大多数都被APK里内置的ndk-gdb那一套给遮掩了。不服,就是要更简单的方法。
目标程序的命令行参数...
一般用java做一个Android app,里面夹杂着jni/*.c 所编译出来的lib,是可以通过gdb来调试的,也许大多数人都在按照网上各种帮助在使用,原理是比较古怪的:app里被塞进去一个gdbserver程序!安装后,会在该app的data目录下出现gdbserver,然后eclipse活着什么别的工具偷偷的启动gdbserver,让他调试改app进程,哦,是通过侦听一个socket,以便让PC端远程发送命令。关于这个网上有不少原理叙述。
但是有时候,就是开发一个和java无关的C程序放在android里运行,要想调试,这么简单的需求,就不想被那些乱七八糟的束缚了,方法基本参照gdbserver标准方法,例如这个。 只是稍微结合一下Android和NDK。
大致如下:
在ndk里找到gdbserver, 例如
1
| find / Applications / android - ndk - type f - name '*gdbserver*' |grep arm |
结果
1
2
| / Applications / android - ndk / prebuilt / android - arm / gdbserver / gdbserver / Applications / android - ndk / prebuilt / android - arm64 / gdbserver / gdbserver |
Android 5.0之前是第1行,之后是第2行。
把找到的gdbserver传到Android上, 例如
1
| adb push / Applications / android - ndk / prebuilt / android - arm / gdbserver / gdbserver / data / local / tmp / |
启动Android里的gdbserver来打开目标C程序,例如a.out
1
| adb shell / data / local / tmp / gdbserver localhost: 9999 / data / local / tmp / a.out |
结果
1
2
| Process / data / local / tmp / a.out created; pid = 16236 Listening on port 9999 |
把本地的9999端口映射到Android上以便以后的操作
1
| adb forward tcp: 9999 tcp: 9999 |
至于gdb client,也是在NDK里找出来
1
| find / Applications / android - ndk - type f - name '*gdb' |grep arm |
结果
1
2
3
| / Applications / android - ndk / toolchains / arm - linux - androideabi - 4.6 / prebuilt / darwin - x86_64 / bin / arm - linux - androideabi - gdb / Applications / android - ndk / toolchains / arm - linux - androideabi - 4.8 / prebuilt / darwin - x86_64 / bin / arm - linux - androideabi - gdb / Applications / android - ndk / toolchains / arm - linux - androideabi - 4.9 / prebuilt / darwin - x86_64 / bin / arm - linux - androideabi - gdb |
就选最后一行好了。
启动gdb client(当然在PC上)
1
| / Applications / android - ndk / / toolchains / arm - linux - androideabi - 4.9 / prebuilt / darwin - x86_64 / bin / arm - linux - androideabi - gdb . / a.out target remote :9999 |
结果就不啰嗦了,就是个(gdb)提示符,等你输入断点命令(b)和继续命令(cont)还有什么显示代码命令(list)等等,直接在网上找找还快些。
拒绝ndk-build (Android Native Development Kit)
订阅:
博文 (Atom)