0

I'm trying to call java static method from c++ on sigaction handler,but the java method dose not work. CallStaticVoidMethod return success but the java code has not been execute.

bool Init( JNIEnv* env,jobject obj,const std::string &functionName)
        {
            LOGE("Init 1");
            //jobject is the java static method object
            _j_class_ = (jclass)env->NewGlobalRef((jobject)env->GetObjectClass(obj));
            if(_j_class_ == 0)
            {
                LOGE("className not found! _j_class_ == 0");

                return false;
            }

            _j_method_ = env->GetStaticMethodID(_j_class_, functionName.c_str(),"(Ljava/lang/String;)V");
            if(_j_method_ == 0)
            {
                LOGE("functionName:%s,_j_method_ == 0",functionName.c_str());

                return false;
            }
            LOGE("functionName:%s,_j_method_ success",functionName.c_str());
            return m_is_inited = true;
        }

//when native crash occurs,will call this method    
void InvokeToJava(const char *pErrorString)
        {
            LOGE("NativeCrashHelper::InvokeToJava Start.");

            m_is_inited = false;

            JNIEnv *pEnv = NULL;
            const int nRet = JNI_Object::beforeCallToJava(&pEnv); //init the pEnv
            if(nRet == -1)
            {
                LOGE("nRet == -1");

                return;
            }

            if(_j_class_ == 0)
            {
                LOGE("_j_class_ == 0");

                return ;
            }

            if(_j_method_ == 0)
            {
                LOGE("_j_method_ == 0");

                return;
            }

            jstring err = 0;
            LOGE("InvokeToJava Convert Error String.");

            StringFunction::C2J(pEnv,pErrorString,err);

            LOGE("InvokeToJava CallStaticVoidMethod");

            pEnv->CallStaticVoidMethod(_j_class_,_j_method_,err);

            LOGE("InvokeToJava CallStaticVoidMethod Finished!");

            pEnv->DeleteLocalRef(err);

            JNI_Object::afterCallToJava(nRet);

            LOGE("NativeCrashHelper::InvokeToJava Finished.");
        }

Here is the java method:

public static void crashHelperNotify(String nativeCrashInfo){
        Log.e("NativeCrashHelper","crashHelperNotify: ");
        //if (s_CallBackMethod != null) {
        //    s_CallBackMethod.invoke(null, nativeCrashInfo);
        //}
    }

And the logcat is show below:

// those log seems that call java method success, I can not figure out why the java method dose't work

_jni_log: Native_SigAction      //zz
_jni_log: Native_SigAction: Reset Sigaction.    //zz
_jni_log: Native_SigAction InvokeToJava :signal : 11,                   
_jni_log: NativeCrashHelper::InvokeToJava Start.  //zz
_jni_log: InvokeToJava Convert Error String.      //zz
_jni_log: InvokeToJava CallStaticVoidMethod       //zz
_jni_log: InvokeToJava CallStaticVoidMethod Finished!  //should print java log here
_jni_log: NativeCrashHelper::InvokeToJava Finished.    //zz
_jni_log: Native_SigAction Finished!   //zz

I search a lot but can not find any help

Michael
  • 57,169
  • 9
  • 80
  • 125
retry
  • 1
  • You've wrapped a bunch of stuff in classes that you haven't shown us, so we don't know e.g. if you're properly obtaining a `JNIEnv*` or whether you're properly handling any Java exceptions that might be thrown. – Michael Oct 17 '17 at 09:14
  • Show how pEnv->CallStaticVoidMethod(_j_class_,_j_method_,err); calls public static void crashHelperNotify(String nativeCrashInfo) – Jon Goodwin Oct 17 '17 at 09:21
  • Please use `logcat -v thread` to understand which threads are involved, and paste the unredacted log. Make sure that `beforeCallToJava()` attaches the thread if needed. And what is your test platform? – Alex Cohn Oct 17 '17 at 09:59
  • What makes you believe that you can make Java calls from within a signal handler? You're lucky the logging calls work. Only async-signal-safe functions can be safely called from within a signal handler. On top of that, installing your own signal handlers in a JVM isn't trivial. – Andrew Henle Oct 17 '17 at 11:07

1 Answers1

0

Calling the java method on the signal handler thread will fail, so I create a new thread and wait for the signal. It works. end.

retry
  • 1