记录-hellojni

package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}
#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
void func_a(char *p);
void func_b(char *p);
void func_a(char *p)
{
    const char* A = "AAAAAAAAA"; // len = 9
    char* a = "dead";
    memcpy(p, A, strlen(A));
    memcpy(p, a, strlen(a));
    p[strlen(a)] = 0;
    func_b(p);
}
void func_b(char *p)
{
    char* b = "0xddeeaadd";
    memcpy(b, p, strlen(p));
}

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    char buf[10];
    func_a(buf);
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}
#Copyright (C) 2010 by Garmin Ltd. or its subsidiaries.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := libhello-jni
LOCAL_SRC_FILES := jni/hello-jni.c

LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_PACKAGE_NAME := mytest

LOCAL_SRC_FILES := \
    $(call all-subdir-java-files) \
    $(empty)

LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_JNI_SHARED_LIBRARIES := libhello-jni

include $(BUILD_PACKAGE)

 

将工程加入到package/apps目录下

mma编译,在out/target/product/***/system/app下会生成mytest  push到system/app下面     还需要把编译出的lib-hellojni.so push到system/lib64目录下

这个程序跑起来会发生native crash,会在data/anr目录下生成tombstones文件   debuggerd工具也可以触发生成log,debuggerd -b tid会生成backtrace,debuggerd tid会生成完整tombstone

 

tombstone异常如下:

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Xiaomi/perseus/perseus:9/PKQ1.180729.001/V10.3.0.1.PEECNXM:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 5544, tid: 5544, name: xample.hellojni >>> com.example.hellojni <<<
signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x725939b817
x0 000000725939b817 x1 0000007fe24d199c x2 0000000000000004 x3 000000725939b81b
x4 0000007fe24d19a0 x5 0000000080808000 x6 0000000064616564 x7 0000000064616564
x8 000000725939b817 x9 0630db868f9f175e x10 0000000000430000 x11 0000007270ce5688
x12 00000072f6689150 x13 429940db552003dd x14 00000072f66db000 x15 ffffffffffffffff
x16 00000072593bafe8 x17 00000072f59cb8b8 x18 0000007271291b00 x19 00000072712e8460
x20 00000072f6d395e0 x21 0000007271214c00 x22 0000007fe24d1cc0 x23 00000072594692ac
x24 0000000000000004 x25 00000072f6d395e0 x26 0000007271214ca0 x27 0000000000000001
x28 0000007fe24d19f0 x29 0000007fe24d19c0
sp 0000007fe24d1990 lr 000000725939b7cc pc 00000072f59a30b8

backtrace:
#00 pc 000000000001f0b8 /system/lib64/libc.so (memcpy+312)
#01 pc 00000000000007c8 /system/lib64/libhello-jni.so (Java_com_example_hellojni_HelloJni_stringFromJNI+36)
#02 pc 00000000005659e0 /system/lib64/libart.so (art_quick_generic_jni_trampoline+144)
#03 pc 000000000055c988 /system/lib64/libart.so (art_quick_invoke_stub+584)
#04 pc 00000000000d0520 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+200)
#05 pc 0000000000280b90 /system/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+344)
#06 pc 000000000027aba4 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+968)
#07 pc 000000000052c2dc /system/lib64/libart.so (MterpInvokeVirtual+588)
#08 pc 000000000054f014 /system/lib64/libart.so (ExecuteMterpImpl+14228)
#09 pc 0000000000000238 /data/dalvik-cache/arm64/system@app@mytest.apk@classes.vdex (com.example.hellojni.HelloJni.onCreate+16)
#10 pc 00000000002548a8 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.223931584+488)
#11 pc 000000000025a39c /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+216)
#12 pc 000000000027ab88 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+940)
#13 pc 000000000052c2dc /system/lib64/libart.so (MterpInvokeVirtual+588)
#14 pc 000000000054f014 /system/lib64/libart.so (ExecuteMterpImpl+14228)
#15 pc 0000000000429004 /system/framework/boot-framework.vdex (android.app.Activity.performCreate+32)
#16 pc 00000000002548a8 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.223931584+488)
#17 pc 000000000025a39c /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+216)
#18 pc 000000000027ab88 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+940)
#19 pc 000000000052c2dc /system/lib64/libart.so (MterpInvokeVirtual+588)
#20 pc 000000000054f014 /system/lib64/libart.so (ExecuteMterpImpl+14228)
#21 pc 0000000000428fd8 /system/framework/boot-framework.vdex (android.app.Activity.performCreate+2)
#22 pc 00000000002548a8 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.223931584+488)
#23 pc 000000000025a39c /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+216)
#24 pc 000000000027ab88 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+940)

用addr2line看一下00000000000007c8对应代码哪里  可定位出出错代码行

这里面

char* b = "0xddeeaadd"; "0xddeeaadd"是一个常量字符串,不能修改

 

posted on 2019-04-09 11:18  EMH1899  阅读(398)  评论(0编辑  收藏  举报

导航