在本机代码中捕获信号(SIGSEGV)之后从JNI回调到Java Android应用程序代码

我有一个使用新项目向导创建的最小
Android应用程序,并启用了c支持.该应用程序的目的是允许c在捕获信号(SIGSEGV)后回调到java.该程序的序列简短而甜蜜,伪代码如下:

>输入本机方法handleSegv()

>本机代码作为测试回调到java
>本机代码设置SIGSEGV处理程序

>输入Native方法sendSegv()

>本机代码引发/发送SIGSEGV

>输入Native Method signal_handler

>本机代码捕获信号并记录它
>本机代码回调到java
>再次显示本机代码以显示其超过回调

上面唯一不起作用的步骤是步骤3.2.似乎在捕获SIGSEGV后,当本机代码尝试回调到java时没有任何反应.我已经在模拟器和设备上尝试了相同的结果.我不确定在这一点上我是否做错了什么或者是否有一些关于处理信号的基本信息,这些信号在捕获之后不允许我回调到java.

我有代码演示这可以从repo from on github克隆,但实际上只有两个源文件:

CrashActivity.java

package com.kevinkreiser.crashtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class CrashActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash);

        //setup segv handler
        handleSegv();
        //cause a segv
        sendSegv();
    }

    /**
     * Sets up signal handler for SIGSEGV which will call the callback function below
     * @return true if the handler was set
     */
    public native boolean handleSegv();

    /**
     * Raises the SIGSEGV signal which will cause the handler to be called
     */
    public native void sendSegv();

    /**
     * A function that the native code will call back when it receives SIGSEGV
     * as an illustration it just logs
     *
     * @param message  The message coming back from c++
     */
    public void callback(String message) {
        Log.e("CrashActivity.callback", message);
    }
}

native-lib.cpp

#include <android/log.h>
#include <jni.h>
#include <string.h>
#include <signal.h>
#include <string>

//globals persisting between calls from javaland
static JavaVM* vm = NULL;
static jobject activity = NULL;
static jmethodID callback = NULL;

//gets called first when a signal is sent to the running pid
static void signal_handler(int signal, siginfo_t*, void*) {
    //get an env so we can call back to java
    JNIEnv* env;
    if(vm->AttachCurrentThread(&env, NULL) != JNI_OK)
        return;

    //call back to java with a message
    __android_log_print(ANDROID_LOG_ERROR, "native-lib.signal_handler", "Calling with signal %d", signal);
    std::string message = "Got signal " + std::to_string(signal);
    jstring msg = env->NewStringUTF(message.c_str());
    env->CallVoidMethod(activity, callback, msg);
    __android_log_print(ANDROID_LOG_ERROR, "native-lib.signal_handler", "Called with signal %d", signal);
}

extern "C" JNIEXPORT void JNICALL
Java_com_kevinkreiser_crashtest_CrashActivity_sendSegv(JNIEnv*, jobject) {
    raise(SIGSEGV);
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_kevinkreiser_crashtest_CrashActivity_handleSegv(JNIEnv* env, jobject obj) {
    //get java hooks we need to make the callback
    env->GetJavaVM(&vm);
    activity = env->NewGlobalRef(obj);
    if (!activity)
        return false;
    jclass activity_class = env->GetObjectClass(activity);
    if (!activity_class)
        return false;
    callback = env->GetMethodID(activity_class, "callback", "(Ljava/lang/String;)V");
    if (!callback)
        return false;

    //try calling back to java with a message
    jstring message = env->NewStringUTF("No signal yet");
    env->CallVoidMethod(activity, callback, message);

    //register for SIGSEGV
    struct sigaction action;
    memset(&action, 0, sizeof(struct sigaction));
    action.sa_sigaction = signal_handler;
    action.sa_flags = SA_SIGINFO;
    sigaction(SIGSEGV, &action, NULL);

    return true;
}

当我运行程序并查看logcats输出时,我看到以下内容:

2019-01-15 11:59:50.795 11183-11183/com.kevinkreiser.crashtest E/CrashActivity.callback: No signal yet
2019-01-15 11:59:50.795 11183-11183/com.kevinkreiser.crashtest E/native-lib.signal_handler: Calling with signal 11
2019-01-15 11:59:50.795 11183-11183/com.kevinkreiser.crashtest E/native-lib.signal_handler: Called with signal 11

如果我使用调试器逐步执行程序并在本机signal_handler中设置断点,我可以逐步调到第一次使用信号调用时记录的行….如果我跨过包含调用的任何行使用JNIEnv(在这种情况下为env)调试器将重新启动,程序将完成.你会注意到,从logcat输出来看,我确实得到了最后的本机日志行,用调用信号…在使用env的调用之后,最重要的是调用回java.

我已经在stackoverflow上看到了其他实现基本上这样做但我无法让它们中的任何一个工作.我也尝试从本机代码中抛出一个java异常,但最终也没有回到javaland,并显示有关挂起异常的消息.有谁能看到这里有什么问题?提前致谢!

最佳答案 @Andrew Henle的评论是正确答案:

Yes, you’re doing something wrong: you’re invoking undefined behavior by calling non-async-signal-safe functions from within a signal handler. Absent specific documentation supporting calling a function, such as the POSIX list of async-safe functions, you really can’t make any calls from a signal handler. Footnote 188 of the C standard even states: “Thus, a signal handler cannot, in general, call standard library functions.” POSIX provides a list of functions that are safe to call – under POSIX. Anything else is undefined behavior.

他此前在这里给出了一个更详细的答案:https://stackoverflow.com/a/34553070/5251867

编辑:

查看可用的功能,似乎有两种途径可供使用.

>利用open,write,close来删除一个文件,其中包含有关被捕获信号的相关信息并稍后处理此文件(在应用程序重启时或从监视此文件的其他服务进行更改)
>利用connect,bind,send将套接字上的详细信息发送给其他进程

我猜这两个都是技术上的IPC,因为它们都是让另一个进程能够访问信号处理程序放在那里的信息的手段.将此信息发送到另一个可以对信息执行操作的过程似乎是唯一合适的方法.

点赞