JVMT异常监视器

Posted

技术标签:

【中文标题】JVMT异常监视器【英文标题】:JVMT exception monitor 【发布时间】:2015-08-23 06:54:49 【问题描述】:

如何使用 JVMTI 获取异常堆栈跟踪。我为处理异常编写了简单的代码,但我想要处理异常行数。如何获得这个号码?

#include <jvmti.h>
#include <string.h>
#include "agent.h"

void printStackTrace(JNIEnv* env, jobject exception) 
    jclass throwable_class = (*env).FindClass("java/lang/Throwable");
    jmethodID print_method = (*env).GetMethodID(throwable_class, "printStackTrace", "()V");
    (*env).CallVoidMethod(exception, print_method);


void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
                               jmethodID method, jlocation location, jobject exception,
                               jmethodID catch_method, jlocation catch_location) 
    char* class_name;
    jvmtiFrameInfo frames[10000];
    jint count;
    jvmtiError err;

    jclass exception_class = (*env).GetObjectClass(exception);
    (*jvmti).GetClassSignature(exception_class, &class_name, NULL);
    printf("Exception: %s\n", class_name);
    printStackTrace(env, exception);
    err = (*jvmti).GetStackTrace(thread, 0, 10000, (jvmtiFrameInfo *)&frames, &count);
    if (err != JVMTI_ERROR_NONE) 
        printf("(GetThreadInfo) Error expected: %d, got: %d\n", JVMTI_ERROR_NONE, err);
        printf("\n");

    
    printf("Number of records filled: %d\n", count);


JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) 
    jvmtiEnv* jvmti;
    jvmtiEventCallbacks callbacks;
    jvmtiCapabilities capabilities;

    (*vm).GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);

    memset(&capabilities, 0, sizeof(capabilities));
    capabilities.can_generate_exception_events = 1;
    (*jvmti).AddCapabilities(&capabilities);

    memset(&callbacks, 0, sizeof(callbacks));
    callbacks.Exception = ExceptionCallback;
    (*jvmti).SetEventCallbacks(&callbacks, sizeof(callbacks));
    (*jvmti).SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);

    return 0;

【问题讨论】:

【参考方案1】:

我通过问题解决了。只需添加 capabilities.can_get_line_numbers = 1; 并调用 (*jvmti)->GetLineNumberTable(jvmti, method, &count, &location_table);

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

void printStackTrace(JNIEnv* env, jobject exception) 
    jclass throwable_class = (*env)->FindClass(env, "java/lang/Throwable");
    jmethodID print_method = (*env)->GetMethodID(env, throwable_class, "printStackTrace", "()V");
    (*env)->CallVoidMethod(env, exception, print_method);


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(env, exception);
    (*jvmti)->GetClassSignature(jvmti, exception_class, &class_name, NULL);


    int count;
    int line_number = 0;
    int i;
    jvmtiLineNumberEntry *location_table;
    (*jvmti)->GetLineNumberTable(jvmti, method, &count, &location_table);
    for (i = 0; i < count - 1; i++)
    
        jvmtiLineNumberEntry entry1 = location_table[i];
        jvmtiLineNumberEntry entry2 = location_table[i+1];
        if (location >= entry1.start_location && location < entry2.start_location)
        
            line_number = entry1.line_number;
            break;
        
    
    if (location >= location_table[count-1].start_location)
    
        line_number = location_table[count-1].line_number;
    

    printf("Exception: %s ", class_name);
    printf("%d \n", line_number);


JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) 
    jvmtiEnv* jvmti;
    jvmtiEventCallbacks callbacks;
    jvmtiCapabilities capabilities;

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

    memset(&capabilities, 0, sizeof(capabilities));
    capabilities.can_generate_exception_events = 1;
    capabilities.can_get_line_numbers = 1;
    (*jvmti)->AddCapabilities(jvmti, &capabilities);

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

    return 0;

【讨论】:

以上是关于JVMT异常监视器的主要内容,如果未能解决你的问题,请参考以下文章

Testcontainer 不适用于 Kotlin 和 Quarkus - 连接到服务器 mongo:27017 时监视器线程中出现异常

ASP.NET-使用事件监视诊断程序异常

将 COM 对象传递给 VB 6 并打开监视窗口时出现 VariantClear 异常

java.lang.IllegalMonitorStateException: (m=null) 无法获得监视器

android设备监视器屏幕显示这个奇怪的空白屏幕,没有任何工作

使用AOP实现一个接口监视器