调试 android jni 程序


http://groups.google.com/group/android-ndk/browse_thread/thread/50362904ae0574cf/fe9703f5cc08714e

调试jni apk

http://vilimpoc.org/blog/2010/09/23/hello-gdbserver-a-debuggable-jni-example-for-android/

android 上ndk-build 编译可执行程序方法

http://vilimpoc.org/blog/2010/09/26/straightforward-android-native-executables/

1.

After doing much, much research and headaches, I believe I got

debugging to work FINALLY. I want to thank everyone here for there
responses. For those that want to try this, this is what I did:

note: The way I'm debugging here is on a simple mixed java/native c++
app (app that uses jni) using gdb. Most guides regarding this topic
of debugging native code involved a independent standalone c/c++
executable (runs from android shell), this is not the case here. This
is debugging on a java app that makes calls to code in c/c++.

setup: To do this you need an os that can get and build the android os
source, so linux or mac. For windows you can use a virtual machine of
linux (thats what I did vm linux mint)

the app: SimpleJNI (this sample app is part of android os source
located in development/sample/SimpleJNI), it demonstrates the concept

1. First get the Android OS source following the instructions here
http://source.android.com/downloadyou need to get it and make it.
2. Next follow the instructions herehttp://source.android.com/using-eclipse
to setup the eclipse and add the SimpleJNI app to the build path (most
important part to note here is adding the app to build path)
3. Now you need edit the app's Android.mk to add debugging symbols for
gdb. Go to SimpleJNI/jni folder in Android.mk and look for this line
"LOCAL_CFLAGS +=" change it to read "LOCAL_CFLAGS += -g"
4. cd /path/to/android/root and do "make SimpleJNI" to make the app.
5. Back in the Using Eclipse link follow these instructions from the
debugging with eclipse:

cd /path/to/android/root
. build/envsetup.sh
lunch 1 # to build the emulator
make # if you didn't already do this
emulator # you should see a GUI picture of a phone

Note: if your terminal can't seem to recognize the commands make sure
you are in the android root, and rerun ". build/ensetup.sh" and "lunch
1" commands

6. Next you need to install the app, from another terminal use this:
adb install -r ./out/target/product/generic/system/app/SimpleJNI.apk

7. You also need to push gdbserver in the emulator too, this will go
in /data/local directory
adb push ./out/target/product/generic/obj/EXECUTABLES/
gdbserver_intermediates/gdbserver /data/local

8. run the app SimpleJNI in the emulator, so it's running.

9. Follow the instructions herehttp://pdk.android.com/online-pdk/guide/debugging_gdb.html,
use the method of attaching to pid instead of executable

10. To see it work set a breakpoint i.e. b native.cpp:26, now since
there is no main() you can't just use <gdb> run, so what I found that
works is click "back" on the emulator and go back to android
homescreen. Then click open SimpleJNI again and gdb should break at
the breakpoint in this case line 26 in gdb.

11. In the emulator there should be no result displayed except for the
title SimpleJNI if you breaked at line 26

12. In the gdb window type "continue" or "c" and the app should resume
displaying some operation.

Thats it. I hope this was helpful because I felt there wasn't any
clear steps on how to debug w/ gdb/gdbserver on a java/jni app
scenario. I do think this method is very cumbersome and hopefully the
ndk later on will provide a better solution for debuging native code.





2. 一步一步

It is basically that

Then I am doing basically what you said and it is working. But I'll
write here the steps I am following:

cd AOS_ROOT
source build/envsetup.sh
lunch 1
emulator <--- it is important to run this emu instead of the one
included with the SDK

Recompile .so with NDK
cd to NDK_ROOT
make APP=<app_name>
Generate pkg (there are a lot of ways to do this, I am doing it with
eclipse (clean and build))
cp NDK_ROOT/out/apps/<app_name>/<app_name>.so AOS_ROOT/out/target/
product/generic/symbols/system/lib/<app_name>.so

cd to AOS_ROOT
adb install -r NDK_ROOT/apps/<app_name>/bin/<app_name>.apk
execute the installed application in the emulator
adb forward tcp:1234: tcp:1234
adb shell ps
adb shell gdbserver :1234 --attach pid <--- the pid displayed with ps

Open another terminal and cd to AOS_ROOT
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gdb out/
target/product/generic/symbols/system/bin/app_process
----INSIDE GDB ----
set solib-absolute-prefix out/target/product/generic/symbols
set solib-search-path out/target/product/generic/symbols/system/lib/
target remote :1234

Then a list of lines saying the symbols have been loaded appear and
you can insert breakpoints and debug as usual. I have changed the
behaviour of my app so that it stays doing nothing until I press down
in the keyboard, then the lib is loaded and executed, that way I have
time to attach the debugger.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值