在未捕获的异常上生成 Java 堆转储

2022-01-16 00:00:00 jvm java heap-dump

当触发未捕获的异常时,我尝试生成堆转储.我尝试使用 jmap,但是因为当异常发生时进程已经完成,所以这是不可能的.

I try to generate a Heap Dump when a uncaught exception is fired. I tried using jmap, but because the process is finished when the exception happens this is not possible.

使用 UncaughtExceptionHandler 也不是选项,因为我只有被执行程序的二进制文件.

Using a UncaughtExceptionHandler is no option either, because I only have the binaries of the programs that is executed.

谁能帮帮我?

通过命令行或类似方法可以使用该技术很重要,因为我需要将其自动化.使用 GUI 是不行的

It is important that the technique is available through a command line or similar, because I need to automated this. Using a GUI is no option

推荐答案

这可以通过 JVMTI 代理将监听 VMDeath 事件,然后使用 JMM 接口 启动堆转储.

This can be achieved with JVMTI agent that will listen to VMDeath event and then use JMM interface to initiate Heap Dump.

以下是此类 JVMTI 代理的示例源代码:

Here is a sample source code of such JVMTI agent:

#include <jvmti.h>
#include <string.h>
#include <stdio.h>
#include "jmm.h"

JNIEXPORT void* JNICALL JVM_GetManagement(jint version);

void JNICALL VMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
    JmmInterface* jmm = (JmmInterface*) JVM_GetManagement(JMM_VERSION_1_0);
    if (jmm == NULL) {
        printf("Sorry, JMM is not supported
");
    } else {
        jstring path = (*jni)->NewStringUTF(jni, "dump.hprof");
        jmm->DumpHeap0(jni, path, JNI_TRUE);
        printf("Heap dumped
");
    }
}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
    jvmtiEnv* jvmti;
    (*vm)->GetEnv(vm, (void**)&jvmti, JVMTI_VERSION_1_0);

    jvmtiEventCallbacks callbacks;
    memset(&callbacks, 0, sizeof(callbacks));
    callbacks.VMDeath = VMDeath;
    (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
    (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL);

    return 0;
}

将其编译到共享库 (libdump.so) 后,使用 -agentpath 选项运行 Java:

After you've compiled it into the shared library (libdump.so) run Java with -agentpath option:

java -agentpath:/path/to/libdump.so MainClass

如果您希望处理未捕获的异常而不是等待 VMDeath,您可以使用类似的技术为异常事件安装回调.查看此处的示例.

If you wish to handle uncaught exceptions instead of waiting for VMDeath, you may use similar technique to install callback for Exception event. Look here for an example.

相关文章