<tfoot id='6Gb3G'></tfoot>
    <bdo id='6Gb3G'></bdo><ul id='6Gb3G'></ul>

<small id='6Gb3G'></small><noframes id='6Gb3G'>

    1. <legend id='6Gb3G'><style id='6Gb3G'><dir id='6Gb3G'><q id='6Gb3G'></q></dir></style></legend>
    2. <i id='6Gb3G'><tr id='6Gb3G'><dt id='6Gb3G'><q id='6Gb3G'><span id='6Gb3G'><b id='6Gb3G'><form id='6Gb3G'><ins id='6Gb3G'></ins><ul id='6Gb3G'></ul><sub id='6Gb3G'></sub></form><legend id='6Gb3G'></legend><bdo id='6Gb3G'><pre id='6Gb3G'><center id='6Gb3G'></center></pre></bdo></b><th id='6Gb3G'></th></span></q></dt></tr></i><div id='6Gb3G'><tfoot id='6Gb3G'></tfoot><dl id='6Gb3G'><fieldset id='6Gb3G'></fieldset></dl></div>

        JVM HotSpot 上的 Java 异常计数器

        时间:2023-08-25
          <tbody id='69u6U'></tbody>
      1. <tfoot id='69u6U'></tfoot>

        <i id='69u6U'><tr id='69u6U'><dt id='69u6U'><q id='69u6U'><span id='69u6U'><b id='69u6U'><form id='69u6U'><ins id='69u6U'></ins><ul id='69u6U'></ul><sub id='69u6U'></sub></form><legend id='69u6U'></legend><bdo id='69u6U'><pre id='69u6U'><center id='69u6U'></center></pre></bdo></b><th id='69u6U'></th></span></q></dt></tr></i><div id='69u6U'><tfoot id='69u6U'></tfoot><dl id='69u6U'><fieldset id='69u6U'></fieldset></dl></div>
        <legend id='69u6U'><style id='69u6U'><dir id='69u6U'><q id='69u6U'></q></dir></style></legend>

              <bdo id='69u6U'></bdo><ul id='69u6U'></ul>

                  <small id='69u6U'></small><noframes id='69u6U'>

                  本文介绍了JVM HotSpot 上的 Java 异常计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道是否可以在不更改应用程序代码的情况下记录在 JVM 级别发生的每个异常?每个异常我的意思是捕获和未捕获的异常......我想稍后分析这些日志并按异常类型(类)对它们进行分组,并按类型简单地计算异常.我正在使用热点 ;)

                  I am wondering is it possible to log every exception which occurs on JVM level without changing application code? By every exception I mean caught and uncaught exception... I would like to analyze those logs later and group them by exception type (class) and simply count exceptions by type. I am using HotSpot ;)

                  也许这样做更聪明?例如通过任何免费的分析器(YourKit 有它但它不是免费的)?我认为 JRockit 在管理控制台中有异常计数器,但在 HotSpot 中没有看到任何类似的东西.

                  Maybe there is smarter why of doing it? For example by any free profiler (YourKit has it but it is not free)? I think that JRockit has exception counter in management console, but don't see anything similar for HotSpot.

                  推荐答案

                  我相信有免费的工具可以做到这一点,但即使制作自己的工具也很容易.JVMTI 会有所帮助.

                  I believe there are free tools to do it, but even making your own tool is easy. JVMTI will help.

                  这是一个简单的 JVMTI 代理,用于跟踪所有异常:

                  Here is a simple JVMTI agent I made to trace all exceptions:

                  #include <jni.h>
                  #include <jvmti.h>
                  #include <string.h>
                  #include <stdio.h>
                  
                  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);
                      printf("Exception: %s
                  ", class_name);
                  }
                  
                  
                  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;
                      (*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;
                  }
                  

                  要使用它,请从给定的源代码创建一个共享库 (.so),然后使用 -agentpath 选项运行 Java:

                  To use it, make a shared library (.so) from the given source code, and run Java with -agentpath option:

                  java -agentpath:libextrace.so MyApplication
                  

                  这将在标准输出上记录所有异常类名称.ExceptionCallback 还接收一个线程、一个方法和一个发生异常的位置,因此您可以扩展回调以打印更多详细信息.

                  This will log all exception class names on stdout. ExceptionCallback also receives a thread, a method and a location where the exception occured, so you can extend the callback to print much more details.

                  这篇关于JVM HotSpot 上的 Java 异常计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:优化期间会使用 Java 内联方法吗? 下一篇:Java“虚拟机"vs. Python“解释器"用语?

                  相关文章

                    <legend id='bM1Nj'><style id='bM1Nj'><dir id='bM1Nj'><q id='bM1Nj'></q></dir></style></legend>
                      <bdo id='bM1Nj'></bdo><ul id='bM1Nj'></ul>
                  1. <i id='bM1Nj'><tr id='bM1Nj'><dt id='bM1Nj'><q id='bM1Nj'><span id='bM1Nj'><b id='bM1Nj'><form id='bM1Nj'><ins id='bM1Nj'></ins><ul id='bM1Nj'></ul><sub id='bM1Nj'></sub></form><legend id='bM1Nj'></legend><bdo id='bM1Nj'><pre id='bM1Nj'><center id='bM1Nj'></center></pre></bdo></b><th id='bM1Nj'></th></span></q></dt></tr></i><div id='bM1Nj'><tfoot id='bM1Nj'></tfoot><dl id='bM1Nj'><fieldset id='bM1Nj'></fieldset></dl></div>

                    <small id='bM1Nj'></small><noframes id='bM1Nj'>

                    <tfoot id='bM1Nj'></tfoot>