在本机代码中捕获信号 (SIGSEGV) 后从 JNI 回调到 Java Android 应用程序代码
Callback into Java Android Application Code from JNI after Catching Signal (SIGSEGV) in Native Code
我有一个最小的 Android 应用程序,它是使用启用了 C++ 支持的新项目向导创建的。该应用程序的目的是允许 c++ 在捕获信号 (SIGSEGV) 后回调到 java。程序的序列简短而甜美,伪代码如下:
- 进入本机方法
handleSegv()
- 本机代码回调 java 作为测试
- 本机代码设置 SIGSEGV 处理程序
- 进入本机方法
sendSegv()
- 本地代码raises/sends SIGSEGV
- 进入本机方法
signal_handler
- 本机代码捕获信号并记录它
- 本机代码回调 java
- 本机代码再次记录以显示其步过回调
上面唯一不起作用的步骤是步骤 3.2
。似乎在捕获 SIGSEGV
之后,当本机代码尝试回调 java 时没有任何反应。我在模拟器和设备上都试过了,结果相同。我现在不确定我是不是做错了什么,或者是否有一些关于处理信号的基本原理不允许我在捕获它后回调 java。
我有演示代码可以从 repo from on github 克隆,但实际上只有两个源文件:
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);
}
}
#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;
}
当我 运行 程序并查看 logcat
s 输出时,我看到以下内容:
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
我可以下到它第一次记录的行 Calling with signal...
。在此之后,如果我跨过任何包含使用 JNIEnv
(在本例中为 env
)的调用的行,调试器将分离并且程序将完成。不过,您会注意到,从 logcat
输出中,我确实在使用 env
调用之后得到了最后一个本机日志行 Called with signal...
,最重要的是调用回 java.
我在 Whosebug 上看到其他实现基本上是这样做的,但我无法让它们中的任何一个工作。我还尝试从本机代码中抛出 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.
他之前在这里对这个问题给出了更详细的回答:
编辑:
查看可用功能,似乎有两种途径可以说服。
- 利用
open
、write
、close
删除包含有关已捕获信号的相关信息的文件,稍后处理此文件(在应用程序重新启动时或从另一个监视此文件更改的服务)
- 利用
connect
、bind
、send
通过套接字将详细信息发送到其他进程
我想这两者在技术上都是 IPC,因为它们都是让另一个进程访问信号处理程序在那里提供的信息的方法。将此信息发送到另一个进程,您可以在其中对信息执行某些操作似乎是唯一合适的前进方式。
我有一个最小的 Android 应用程序,它是使用启用了 C++ 支持的新项目向导创建的。该应用程序的目的是允许 c++ 在捕获信号 (SIGSEGV) 后回调到 java。程序的序列简短而甜美,伪代码如下:
- 进入本机方法
handleSegv()
- 本机代码回调 java 作为测试
- 本机代码设置 SIGSEGV 处理程序
- 进入本机方法
sendSegv()
- 本地代码raises/sends SIGSEGV
- 进入本机方法
signal_handler
- 本机代码捕获信号并记录它
- 本机代码回调 java
- 本机代码再次记录以显示其步过回调
上面唯一不起作用的步骤是步骤 3.2
。似乎在捕获 SIGSEGV
之后,当本机代码尝试回调 java 时没有任何反应。我在模拟器和设备上都试过了,结果相同。我现在不确定我是不是做错了什么,或者是否有一些关于处理信号的基本原理不允许我在捕获它后回调 java。
我有演示代码可以从 repo from on github 克隆,但实际上只有两个源文件:
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);
}
}
#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;
}
当我 运行 程序并查看 logcat
s 输出时,我看到以下内容:
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
我可以下到它第一次记录的行 Calling with signal...
。在此之后,如果我跨过任何包含使用 JNIEnv
(在本例中为 env
)的调用的行,调试器将分离并且程序将完成。不过,您会注意到,从 logcat
输出中,我确实在使用 env
调用之后得到了最后一个本机日志行 Called with signal...
,最重要的是调用回 java.
我在 Whosebug 上看到其他实现基本上是这样做的,但我无法让它们中的任何一个工作。我还尝试从本机代码中抛出 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.
他之前在这里对这个问题给出了更详细的回答:
编辑:
查看可用功能,似乎有两种途径可以说服。
- 利用
open
、write
、close
删除包含有关已捕获信号的相关信息的文件,稍后处理此文件(在应用程序重新启动时或从另一个监视此文件更改的服务) - 利用
connect
、bind
、send
通过套接字将详细信息发送到其他进程
我想这两者在技术上都是 IPC,因为它们都是让另一个进程访问信号处理程序在那里提供的信息的方法。将此信息发送到另一个进程,您可以在其中对信息执行某些操作似乎是唯一合适的前进方式。