http://hi.baidu.com/subo4110/item/d00395b3bf63e4432bebe36d
Step1:Android应用主文件:CPPTestActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
com.subo4110.cpptest;
import
android.os.Bundle;
import
android.app.Activity;
import
android.util.Log;
import
android.view.Menu;
public
class
CPPTestActivity
extends
Activity {
private
static
final
String TAG =
"CPPTestActivity"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpptest);
Log.w(TAG,
"load native so file, add return: "
+ UtilsJNILib.add(
193.1245
,
577.90
));
Log.w(TAG,
"load native so file, pow return: "
+ UtilsJNILib.pow(
2
,
13
));
Log.w(TAG,
"load native so file, pow return: "
+ UtilsJNILib.div(
21.456
,
0
));
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cpptest, menu);
return
true
;
}
}
|
Step2:定义native接口的java类:UtilsJNILib.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.subo4110.cpptest;
public
class
UtilsJNILib {
static
{
System.loadLibrary(
"UtilsJNILib"
);
}
public
static
native
double
add(
double
a,
double
b);
public
static
native
double
pow(
double
a,
double
b);
public
static
native
double
div(
double
a,
double
b);
}
|
Step3:调用JNI命令并实现JNI接口的C++类:UtilsJNILib.cpp,具体的JNI命令为:javah -jni com.subo4110.cpptest.UtilsJNILib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <jni.h>
#include <android/log.h>
#include <math.h>
#define LOG_TAG "UtilsJNILib"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
extern
"C"
{
jint JNI_OnLoad(JavaVM *vm,
void
*reserved)
{
LOGI(
"JNI_OnLoad"
);
return
JNI_VERSION_1_4;
}
/*
* Class: com_subo4110_cpptest_UtilsJNILib
* Method: nativeAdd
* Signature: (DD)D
*/
JNIEXPORT jdouble JNICALL Java_com_subo4110_cpptest_UtilsJNILib_add
(JNIEnv *env, jclass obj, jdouble a, jdouble b)
{
return
(a+b);
}
/*
* Class: com_subo4110_cpptest_UtilsJNILib
* Method: pow
* Signature: (DD)D
*/
JNIEXPORT jdouble JNICALL Java_com_subo4110_cpptest_UtilsJNILib_pow
(JNIEnv *env, jclass obj, jdouble a, jdouble b)
{
return
pow
(a, b);
}
/*
* Class: com_subo4110_cpptest_UtilsJNILib
* Method: div
* Signature: (DD)D
*/
JNIEXPORT jdouble JNICALL Java_com_subo4110_cpptest_UtilsJNILib_div
(JNIEnv *env, jclass obj, jdouble a, jdouble b)
{
char
* buf = 0;
buf[1] =
'a'
;
//此处将报错,Fatal Signal 11。。。
return
a/b;
}
};
|
Step4:用来ndk-build的make文件:Android.mk
1
2
3
4
5
6
7
8
9
10
11
|
LOCAL_PATH := $(call my-
dir
)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := UtilsJNILib
LOCAL_SRC_FILES := UtilsJNILib.cpp
include $(BUILD_SHARED_LIBRARY)
|
Step5:运行后,将在Eclipse的logcat窗口看到如下log信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
05-08 16:45:56.800: E/Trace(12735): error opening trace file: No such file or directory (2)
05-08 16:45:56.800: D/ActivityThread(12735): setTargetHeapUtilization:0.25
05-08 16:45:56.800: D/ActivityThread(12735): setTargetHeapIdealFree:8388608
05-08 16:45:56.800: D/ActivityThread(12735): setTargetHeapConcurrentStart:2097152
05-08 16:45:57.000: W/ResourceType(12735): No package identifier when getting value for resource number 0x00000000
05-08 16:45:57.000: W/PackageManager(12735): Failure retrieving resources forcom.subo4110.cpptest: Resource ID #0x0
05-08 16:45:57.030: D/IconCustomizer(12735): Generate customized icon for com.subo4110.cpptest.png
05-08 16:45:57.030: W/IconCustomizer(12735): can't load transform_config.xml
05-08 16:45:57.100: I/themeservice(12735): add pending job /data/data/com.subo4110.cpptest/cache/com.subo4110.cpptest.png
05-08 16:45:57.120: I/themeservice(12735): binding service
05-08 16:45:57.130: I/UtilsJNILib(12735): JNI_OnLoad
05-08 16:45:57.130: W/CPPTestActivity(12735): load native so file, add return: 771.0245
05-08 16:45:57.130: W/CPPTestActivity(12735): load native so file, pow return: 8192.0
05-08 16:45:57.130: A/libc(12735): Fatal signal 11 (SIGSEGV) at 0x00000001 (code=1), thread 12735 (ubo4110.cpptest)
|
Step6:在cmd中输入命令:adb logcat | ndk-stack -sym D:\workspace\Android\MyOwnSamples\JNICPPTest\obj\local\armeabi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
C:\Users\yongle.liu>adb logcat | ndk-stack -sym D:\workspace\Android\MyOwnSamples\JNICPPTest\obj\
local
\armeabi
********** Crash dump: **********
Build fingerprint:
'unknown'
pid: 4079, tid: 4095 >>> com.mobinex.service:TeliDHService <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 03f9b848
Stack frame
#00 pc 00019012 /system/lib/libutils.so (_ZNK7android7RefBase9decStrongEPKv)
Stack frame
#01 pc 00010930 /system/lib/libcamera_client.so (_ZN7android2spINS_7ICameraEED1Ev)
Stack frame
#02 pc 00010d82 /system/lib/libcamera_client.so (_ZN7android6CameraD1Ev)
Stack frame
#03 pc 00010de8 /system/lib/libcamera_client.so (_ZN7android6CameraD0Ev)
Stack frame
#04 pc 000118f2 /system/lib/libcamera_client.so (_ZTv0_n12_N7android6CameraD0Ev)
Stack frame
#05 pc 00019038 /system/lib/libutils.so (_ZNK7android7RefBase9decStrongEPKv)
Stack frame
#06 pc 00017398 /system/lib/libbinder.so (_ZN7android14IPCThreadState14joinThreadPoolEb)
Stack frame
#07 pc 0001c668 /system/lib/libbinder.so
Stack frame
#08 pc 00020156 /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv)
Stack frame
#09 pc 00096260 /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv)
Stack frame
#10 pc 0002079c /system/lib/libutils.so
Stack frame
#11 pc 00012134 /system/lib/libc.so (__thread_entry)
Stack frame
#12 pc 00011c88 /system/lib/libc.so (pthread_create)
Crash dump is completed
********** Crash dump: **********
Build fingerprint:
'unknown'
pid: 4201, tid: 4201 >>> com.subo4110.cpptest <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000001
Stack frame
#00 pc 00000fee /data/data/com.subo4110.cpptest/lib/libUtilsJNILib.so (Java_com_subo4110_cpptest_UtilsJNILib_div): Routine Java_com_subo4110_cpptest_UtilsJNILib_div in jni/UtilsJNILib.cp
p:54
Stack frame
#01 pc 00018c30 /system/lib/libdvm.so (dvmPlatformInvoke)
Stack frame
#02 pc 00053146 /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#03 pc 00046c44 /system/lib/libdvm.so (_Z21dvmCheckCallJNIMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#04 pc 00054e74 /system/lib/libdvm.so (_Z22dvmResolveNativeMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#05 pc 0002aa4c /system/lib/libdvm.so
Stack frame
#06 pc 0002e1fc /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
Stack frame
#07 pc 00066832 /system/lib/libdvm.so (_Z15dvmInvokeMethodP6ObjectPK6MethodP11ArrayObjectS5_P11ClassObjectb)
Stack frame
#08 pc 0006dc62 /system/lib/libdvm.so
Stack frame
#09 pc 00054e74 /system/lib/libdvm.so (_Z22dvmResolveNativeMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#10 pc 0002aa4c /system/lib/libdvm.so
Stack frame
#11 pc 0002e1fc /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
Stack frame
#12 pc 00066b02 /system/lib/libdvm.so (_Z14dvmCallMethodVP6ThreadPK6MethodP6ObjectbP6JValueSt9__va_list)
Stack frame
#13 pc 0004f19a /system/lib/libdvm.so
Stack frame
#14 pc 00043b20 /system/lib/libdvm.so
Stack frame
#15 pc 000962fe /system/lib/libandroid_runtime.so
Stack frame
#16 pc 00096e66 /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime5startEPKcS2_)
Stack frame
#17 pc 00008f0e /system/bin/app_process
Stack frame
#18 pc 00015a34 /system/lib/libc.so (__libc_init)
Crash dump is completed
********** Crash dump: **********
Build fingerprint:
'unknown'
pid: 4309, tid: 4309 >>> com.subo4110.cpptest <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000001
Stack frame
#00 pc 00000fee /data/data/com.subo4110.cpptest/lib/libUtilsJNILib.so (Java_com_subo4110_cpptest_UtilsJNILib_div): Routine Java_com_subo4110_cpptest_UtilsJNILib_div in jni/UtilsJNILib.cp
p:54
Stack frame
#01 pc 00018c30 /system/lib/libdvm.so (dvmPlatformInvoke)
Stack frame
#02 pc 00053146 /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#03 pc 00046c44 /system/lib/libdvm.so (_Z21dvmCheckCallJNIMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#04 pc 00054e74 /system/lib/libdvm.so (_Z22dvmResolveNativeMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#05 pc 0002aa4c /system/lib/libdvm.so
Stack frame
#06 pc 0002e1fc /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
Stack frame
#07 pc 00066832 /system/lib/libdvm.so (_Z15dvmInvokeMethodP6ObjectPK6MethodP11ArrayObjectS5_P11ClassObjectb)
Stack frame
#08 pc 0006dc62 /system/lib/libdvm.so
Stack frame
#09 pc 00054e74 /system/lib/libdvm.so (_Z22dvmResolveNativeMethodPKjP6JValuePK6MethodP6Thread)
Stack frame
#10 pc 0002aa4c /system/lib/libdvm.so
Stack frame
#11 pc 0002e1fc /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
Stack frame
#12 pc 00066b02 /system/lib/libdvm.so (_Z14dvmCallMethodVP6ThreadPK6MethodP6ObjectbP6JValueSt9__va_list)
Stack frame
#13 pc 0004f19a /system/lib/libdvm.so
Stack frame
#14 pc 00043b20 /system/lib/libdvm.so
Stack frame
#15 pc 000962fe /system/lib/libandroid_runtime.so
Stack frame
#16 pc 00096e66 /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime5startEPKcS2_)
Stack frame
#17 pc 00008f0e /system/bin/app_process
Stack frame
#18 pc 00015a34 /system/lib/libc.so (__libc_init)
C:\Users\yongle.liu>
|
Step7:堆栈中最后2个里面,可以找到signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000001,而这个地址与Eclipse的logcat的log信息中的错误地址一样。那么你就可以开始找错误啦!
参考资料:http://www.cocos2d-x.org/boards/6/topics/13392?r=13472
但是,我还没有找到如何查看多个so库有链接关系的情况,如何利用ndk-stack查看堆栈?望高手指点!