1. 设置android jni工程为debug模式
在Application.mk中添加
APP_PLATFORM
:= android-9
APP_OPTIM := debug
#release
APP_CFLAG := -g -ggdb -O0
在Manifest.xml中添加
android:debuggable="true"
好像 Application.mk中的 debug 可以覆盖 manifest中的debug设置,没有验证过.
2. 在Eclipse 下设置jni debug
2.1
右键选择工程 ----> Android Tools ---> Add Native Support # 这个取消比较麻烦,据说是要 删除 .cproject 和修改.project,去掉有关CDEBUG的内容.
2.2
Window -> Preferences - > Anddroid ---> NDK ,
设置 NDK Location, 比如: D:\Android\android-ndk-r8b
2.3
Project --> Properties ----〉 C/C++ Build
设置 Build command ndk-build NDK_DEBUG=1
2.4
- Go to "Run" menu-> "Debug Configurations"
- Under "Android Native Application" in the left pane, select your application
- Under the "Debugger" tab click "Add..." in the "Shared Libraries" section.
- Browse to your android library project directory and add its subdirectory obj/local/armeabi. #这一项应该用下图中所示的默认值就可以,不必作为Symbol添加.
2.5 Debug as ------> Android Native Application
设置 break point, 开始调试.
注意: 要在 native C 中设置断点, 要让程序先 Run 起来, 加载 so 以后, 才能C 中设置断点. 可以先在 java 中设置一个断点, 程序运行到这里后, so 已经加载,然后再在 C 中设置断点. 或者使用 button 启动native.
经过验证,这种方式是可以work 的.
3. 命令行下DEBUG jni工程
http://www.cnblogs.com/liuyue0802/p/3347461.html
这边文章写了如何用ndk-gdb 调试程序,很详细,没有验证过.
$ ndk-build NDK_DEBUG=1
$ android update project -p <javaprojectname> (在cygwin下,$ android.bat update project -p . -t android-18)
$ cd <javaprojectname>
$ ant clean debug install
$ ant debug install$ ndk-gdb --start
用list或l命令查看C文件的内容. (gdb) list
如果遇到No symbol table is loaded. Use the "file" command.则需使用以下命令加载需要调试的so文件:
用break 或 b命令设定断点
(gdb) break 30 //将断点设置在30行
查看断点信息
(gdb) info breakpoints
使用c或者continue命令继续运行程序。
(gdb) continue
出现错误:Continuing.
Warning:Cannot insert breakpoint 1.Error accessing memory address 0x11a0: Input/output error.错误原因:使用ndk-gdb --start 启动程序第一个activity,但是此时so文件并没有被加载。
解决方法:首先打开程序并使用,在保证so文件已经被使用的情况下,调用ndk-gdb命令调试程序进程。
QA:
经过验证, 这种方式出现在设置断点是出现 Cannot access memory at address 的错误.
4. JNI Crash coredump
在Android设备的data/tombstone/ 会产生coredump文件.
如果没有, 设置 ulimit
ulimit -c
unlimited
如果不能查看文件, 需要root权限,先是用刷机精灵的一键ROOT, 获得SU权限,然后再remount, 获取读取权限.
adb shell mount -o remount rw /
如果 backtrac中没有显示调用方法信息,可以使用
arm-linux-androideabi-addr2line
-C -f -e D:\Project\ffmpeg_project\ffmpeg\obj\local\armeabi\libH264Android.s
0000a2ab
ndk-stack
-sym D:\Project\ffmpeg_project\ffmpeg\obj\local\armeabi\ -dump C:\Users\dreamtale\Desktop\tomtones\tombstone_04
5 参考链接:
http://stackoverflow.com/questions/12638849/debug-native-code-in-android-library
http://www.cnblogs.com/liuyue0802/p/3347461.html
http://room302.cn/2013/01/how-to-get-native-code-crash-call-stack-log-in-android/
http://stackoverflow.com/questions/16285413/android-ndk-gdb