Error handling 在StackOverflowerr上自动重新启动JVM的最简单方法

Error handling 在StackOverflowerr上自动重新启动JVM的最简单方法,error-handling,jvm,stack-overflow,Error Handling,Jvm,Stack Overflow,似乎没有-XX选项可以在StackOverflowerError上重新启动JVM。当JVM获得StackOverflowerError时,自动重启JVM的最简单方法是什么?热点JVM具有内置的-XX:AbortVMOnException=java.lang.StackOverflowerError选项,但不幸的是,此标志仅在JVM的调试版本中可用 有效的解决方案是使用一个将截获所有异常并在异常属于指定类时中止进程的方法。这是一个这样的代理的例子 #include <jvmti.h>

似乎没有-XX选项可以在StackOverflowerError上重新启动JVM。当JVM获得StackOverflowerError时,自动重启JVM的最简单方法是什么?

热点JVM具有内置的
-XX:AbortVMOnException=java.lang.StackOverflowerError
选项,但不幸的是,此标志仅在JVM的调试版本中可用

有效的解决方案是使用一个将截获所有异常并在异常属于指定类时中止进程的方法。这是一个这样的代理的例子

#include <jvmti.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static const char* fatal_error_class;

void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
                               jmethodID method, jlocation location, jobject exception,
                               jmethodID catch_method, jlocation catch_location) {
    char* class_name;
    jclass exception_class = env->GetObjectClass(exception);
    jvmti->GetClassSignature(exception_class, &class_name, NULL);
    class_name[strlen(class_name) - 1] = 0;

    if (strcmp(class_name + 1, fatal_error_class) == 0) {
        printf("Abort on fatal error\n");
        exit(1);
    }

    jvmti->Deallocate((unsigned char*)class_name);
}

extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* unused) {
    if (options == NULL || options[0] == 0) {
        printf("Usage: -agentpath:/path/to/libabort.so=java/lang/StackOverflowError\n");
        return 1;
    }

    fatal_error_class = strdup(options);

    jvmtiEnv* jvmti;
    vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);

    jvmtiCapabilities capabilities = {0};
    capabilities.can_generate_exception_events = 1;
    jvmti->AddCapabilities(&capabilities);

    jvmtiEventCallbacks callbacks = {0};
    callbacks.Exception = ExceptionCallback;
    jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
    jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);

    return 0;
}
如何运行:

java -agentpath:/path/to/libabort.so=java/lang/StackOverflowError ...

HotSpot JVM具有内置的
-XX:AbortVMOnException=java.lang.StackOverflowerError
选项,但不幸的是,此标志仅在JVM的调试版本中可用

有效的解决方案是使用一个将截获所有异常并在异常属于指定类时中止进程的方法。这是一个这样的代理的例子

#include <jvmti.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static const char* fatal_error_class;

void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
                               jmethodID method, jlocation location, jobject exception,
                               jmethodID catch_method, jlocation catch_location) {
    char* class_name;
    jclass exception_class = env->GetObjectClass(exception);
    jvmti->GetClassSignature(exception_class, &class_name, NULL);
    class_name[strlen(class_name) - 1] = 0;

    if (strcmp(class_name + 1, fatal_error_class) == 0) {
        printf("Abort on fatal error\n");
        exit(1);
    }

    jvmti->Deallocate((unsigned char*)class_name);
}

extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* unused) {
    if (options == NULL || options[0] == 0) {
        printf("Usage: -agentpath:/path/to/libabort.so=java/lang/StackOverflowError\n");
        return 1;
    }

    fatal_error_class = strdup(options);

    jvmtiEnv* jvmti;
    vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);

    jvmtiCapabilities capabilities = {0};
    capabilities.can_generate_exception_events = 1;
    jvmti->AddCapabilities(&capabilities);

    jvmtiEventCallbacks callbacks = {0};
    callbacks.Exception = ExceptionCallback;
    jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
    jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);

    return 0;
}
如何运行:

java -agentpath:/path/to/libabort.so=java/lang/StackOverflowError ...