Blame SOURCES/0022-Exception-callback-code-optimizations.patch

f9a98e
From ba1f3f10129765dec30caa238679331db6848938 Mon Sep 17 00:00:00 2001
f9a98e
From: Jakub Filak <jfilak@redhat.com>
f9a98e
Date: Wed, 15 Jan 2014 09:14:44 +0100
f9a98e
Subject: [PATCH 22/39] Exception callback code optimizations
f9a98e
f9a98e
The old code was doing a lot of stuff which is not necessary when the
f9a98e
processes exception won't be reported.
f9a98e
f9a98e
Related to rhbz#1051198
f9a98e
---
f9a98e
 src/abrt-checker.c | 180 ++++++++++++++++++++++++++++++++---------------------
f9a98e
 1 file changed, 108 insertions(+), 72 deletions(-)
f9a98e
f9a98e
diff --git a/src/abrt-checker.c b/src/abrt-checker.c
f9a98e
index 613a0ec..a5c2bdd 100644
f9a98e
--- a/src/abrt-checker.c
f9a98e
+++ b/src/abrt-checker.c
f9a98e
@@ -219,6 +219,7 @@ T_jthreadMap *threadMap;
f9a98e
 static char* get_path_to_class(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jclass class, char *class_name, const char *stringize_method_name);
f9a98e
 static void print_jvm_environment_variables_to_file(FILE *out);
f9a98e
 static char* format_class_name(char *class_signature, char replace_to);
f9a98e
+static int check_jvmti_error(jvmtiEnv *jvmti_env, jvmtiError error_code, const char *str);
f9a98e
 
f9a98e
 
f9a98e
 
f9a98e
@@ -404,26 +405,70 @@ static const char * null2empty(const char *str)
f9a98e
 }
f9a98e
 
f9a98e
 
f9a98e
+static char *get_exception_type_name(
f9a98e
+        jvmtiEnv *jvmti_env,
f9a98e
+        JNIEnv *jni_env,
f9a98e
+        jobject exception_object)
f9a98e
+{
f9a98e
+    jclass exception_class = (*jni_env)->GetObjectClass(jni_env, exception_object);
f9a98e
+
f9a98e
+    char *exception_name_ptr = NULL;
f9a98e
+
f9a98e
+    /* retrieve all required informations */
f9a98e
+    jvmtiError error_code = (*jvmti_env)->GetClassSignature(jvmti_env, exception_class, &exception_name_ptr, NULL);
f9a98e
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
+        return NULL;
f9a98e
+
f9a98e
+    char *formated_exception_name_ptr = format_class_name(exception_name_ptr, '\0');
f9a98e
+    if (formated_exception_name_ptr != exception_name_ptr)
f9a98e
+    {
f9a98e
+        char *dest = exception_name_ptr;
f9a98e
+        char *src = formated_exception_name_ptr;
f9a98e
+
f9a98e
+        while(src[0] != '\0')
f9a98e
+        {
f9a98e
+            *dest = *src;
f9a98e
+            ++dest;
f9a98e
+            ++src;
f9a98e
+        }
f9a98e
+        dest[0] = '\0';
f9a98e
+    }
f9a98e
+
f9a98e
+    return exception_name_ptr;
f9a98e
+}
f9a98e
+
f9a98e
+
f9a98e
 
f9a98e
 /*
f9a98e
  * Returns non zero value if exception's type is intended to be reported even
f9a98e
  * if the exception was caught.
f9a98e
  */
f9a98e
-static int exception_is_intended_to_be_reported(const char *type_name)
f9a98e
+static int exception_is_intended_to_be_reported(
f9a98e
+        jvmtiEnv *jvmti_env,
f9a98e
+        JNIEnv *jni_env,
f9a98e
+        jobject exception_object,
f9a98e
+        char **exception_type)
f9a98e
 {
f9a98e
+    int retval = 0;
f9a98e
+
f9a98e
     if (reportedCaughExceptionTypes != NULL)
f9a98e
     {
f9a98e
+        *exception_type = get_exception_type_name(jvmti_env, jni_env, exception_object);
f9a98e
+        if (NULL == *exception_type)
f9a98e
+            return 0;
f9a98e
+
f9a98e
         /* special cases for selected exceptions */
f9a98e
         for (char **cursor = reportedCaughExceptionTypes; *cursor; ++cursor)
f9a98e
         {
f9a98e
-            if (strcmp(*cursor, type_name) == 0)
f9a98e
+            if (strcmp(*cursor, *exception_type) == 0)
f9a98e
             {
f9a98e
-                return 1;
f9a98e
+                retval = 1;
f9a98e
+                break;
f9a98e
             }
f9a98e
         }
f9a98e
     }
f9a98e
 
f9a98e
-    return 0;
f9a98e
+    return retval;
f9a98e
 }
f9a98e
 
f9a98e
 
f9a98e
@@ -695,19 +740,21 @@ static char* format_class_name(char *class_signature, char replace_to)
f9a98e
         {
f9a98e
             output++; /* goto to the next character */
f9a98e
         }
f9a98e
-        /* replace the last character in the class name */
f9a98e
-        /* but inly if this character is ';' */
f9a98e
-        char *last_char = output + strlen(output) - 1;
f9a98e
-        if (*last_char == ';')
f9a98e
-        {
f9a98e
-            *last_char = replace_to;
f9a98e
-        }
f9a98e
+
f9a98e
         /* replace all '/'s to '.'s */
f9a98e
         char *c;
f9a98e
         for (c = class_signature; *c != 0; c++)
f9a98e
         {
f9a98e
             if (*c == '/') *c = '.';
f9a98e
         }
f9a98e
+
f9a98e
+        /* replace the last character in the class name */
f9a98e
+        /* but inly if this character is ';' */
f9a98e
+        /* c[0] == '\0' see the for loop above */
f9a98e
+        if (c != class_signature && c[-1] == ';')
f9a98e
+        {
f9a98e
+            c[-1] = replace_to;
f9a98e
+        }
f9a98e
     }
f9a98e
     else
f9a98e
     {
f9a98e
@@ -2053,52 +2100,17 @@ static void JNICALL callback_on_exception(
f9a98e
             jmethodID catch_method,
f9a98e
             jlocation catch_location __UNUSED_VAR)
f9a98e
 {
f9a98e
-    jvmtiError error_code;
f9a98e
-
f9a98e
-    char *method_name_ptr = NULL;
f9a98e
-    char *method_signature_ptr = NULL;
f9a98e
-    char *class_name_ptr = NULL;
f9a98e
-    char *class_signature_ptr = NULL;
f9a98e
-    char *exception_name_ptr = NULL;
f9a98e
-    char *updated_exception_name_ptr = NULL;
f9a98e
-
f9a98e
-    jclass method_class;
f9a98e
-    jclass exception_class;
f9a98e
+    char *exception_type_name = NULL;
f9a98e
 
f9a98e
     /* all operations should be processed in critical section */
f9a98e
     enter_critical_section(jvmti_env, shared_lock);
f9a98e
 
f9a98e
-    char tname[MAX_THREAD_NAME_LENGTH];
f9a98e
-    get_thread_name(jvmti_env, thr, tname, sizeof(tname));
f9a98e
-
f9a98e
-    exception_class = (*jni_env)->GetObjectClass(jni_env, exception_object);
f9a98e
-
f9a98e
-    /* retrieve all required informations */
f9a98e
-    error_code = (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
f9a98e
-    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
-        goto callback_on_exception_cleanup;
f9a98e
-
f9a98e
-    error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &method_class);
f9a98e
-    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
-        goto callback_on_exception_cleanup;
f9a98e
-
f9a98e
-    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, method_class, &class_signature_ptr, NULL);
f9a98e
-    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
-        goto callback_on_exception_cleanup;
f9a98e
-
f9a98e
-    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, exception_class, &exception_name_ptr, NULL);
f9a98e
-    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
-        goto callback_on_exception_cleanup;
f9a98e
-
f9a98e
     /* readable class names */
f9a98e
-    class_name_ptr = format_class_name(class_signature_ptr, '.');
f9a98e
-    updated_exception_name_ptr = format_class_name(exception_name_ptr, '\0');
f9a98e
-
f9a98e
-    INFO_PRINT("%s %s exception in thread \"%s\" ", (catch_method == NULL ? "Uncaught" : "Caught"), updated_exception_name_ptr, tname);
f9a98e
-    INFO_PRINT("in a method %s%s() with signature %s\n", class_name_ptr, method_name_ptr, method_signature_ptr);
f9a98e
-
f9a98e
-    if (catch_method == NULL || exception_is_intended_to_be_reported(updated_exception_name_ptr))
f9a98e
+    if (catch_method == NULL || exception_is_intended_to_be_reported(jvmti_env, jni_env, exception_object, &exception_type_name))
f9a98e
     {
f9a98e
+        char tname[MAX_THREAD_NAME_LENGTH];
f9a98e
+        get_thread_name(jvmti_env, thr, tname, sizeof(tname));
f9a98e
+
f9a98e
         jlong tid = 0;
f9a98e
         T_jthrowableCircularBuf *threads_exc_buf = NULL;
f9a98e
 
f9a98e
@@ -2114,19 +2126,43 @@ static void JNICALL callback_on_exception(
f9a98e
 
f9a98e
         if (NULL == threads_exc_buf || NULL == jthrowable_circular_buf_find(threads_exc_buf, exception_object))
f9a98e
         {
f9a98e
+            jvmtiError error_code;
f9a98e
+            jclass method_class;
f9a98e
+            char *method_name_ptr = NULL;
f9a98e
+            char *method_signature_ptr = NULL;
f9a98e
+            char *class_name_ptr = NULL;
f9a98e
+            char *class_signature_ptr = NULL;
f9a98e
+
f9a98e
             if (NULL != threads_exc_buf)
f9a98e
             {
f9a98e
                 VERBOSE_PRINT("Pushing to circular buffer\n");
f9a98e
                 jthrowable_circular_buf_push(threads_exc_buf, exception_object);
f9a98e
             }
f9a98e
 
f9a98e
+            error_code = (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
f9a98e
+            if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
+                goto callback_on_exception_cleanup;
f9a98e
+
f9a98e
+            error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &method_class);
f9a98e
+            if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
+                goto callback_on_exception_cleanup;
f9a98e
+
f9a98e
+            error_code = (*jvmti_env)->GetClassSignature(jvmti_env, method_class, &class_signature_ptr, NULL);
f9a98e
+            if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
f9a98e
+                goto callback_on_exception_cleanup;
f9a98e
+
f9a98e
+            class_name_ptr = format_class_name(class_signature_ptr, '.');
f9a98e
+
f9a98e
             /* Remove trailing '.' */
f9a98e
             const ssize_t class_name_len = strlen(class_name_ptr);
f9a98e
             if (class_name_len > 0)
f9a98e
                 class_name_ptr[class_name_len - 1] = '\0';
f9a98e
 
f9a98e
+            if (NULL == exception_type_name)
f9a98e
+                exception_type_name = get_exception_type_name(jvmti_env, jni_env, exception_object);
f9a98e
+
f9a98e
             char *message = format_exception_reason_message(/*caught?*/NULL != catch_method,
f9a98e
-                    updated_exception_name_ptr, class_name_ptr, method_name_ptr);
f9a98e
+                    exception_type_name, class_name_ptr, method_name_ptr);
f9a98e
 
f9a98e
             char *executable = NULL;
f9a98e
             char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object,
f9a98e
@@ -2143,6 +2179,24 @@ static void JNICALL callback_on_exception(
f9a98e
 
f9a98e
             free(message);
f9a98e
             free(stack_trace_str);
f9a98e
+
f9a98e
+callback_on_exception_cleanup:
f9a98e
+        /* cleapup */
f9a98e
+            if (method_name_ptr != NULL)
f9a98e
+            {
f9a98e
+                error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)method_name_ptr);
f9a98e
+                check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
+            }
f9a98e
+            if (method_signature_ptr != NULL)
f9a98e
+            {
f9a98e
+                error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)method_signature_ptr);
f9a98e
+                check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
+            }
f9a98e
+            if (class_signature_ptr != NULL)
f9a98e
+            {
f9a98e
+                error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)class_signature_ptr);
f9a98e
+                check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
+            }
f9a98e
         }
f9a98e
         else
f9a98e
         {
f9a98e
@@ -2150,27 +2204,9 @@ static void JNICALL callback_on_exception(
f9a98e
         }
f9a98e
     }
f9a98e
 
f9a98e
-callback_on_exception_cleanup:
f9a98e
-    /* cleapup */
f9a98e
-    if (method_name_ptr != NULL)
f9a98e
-    {
f9a98e
-        error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)method_name_ptr);
f9a98e
-        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
-    }
f9a98e
-    if (method_signature_ptr != NULL)
f9a98e
+    if (NULL != exception_type_name)
f9a98e
     {
f9a98e
-        error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)method_signature_ptr);
f9a98e
-        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
-    }
f9a98e
-    if (class_signature_ptr != NULL)
f9a98e
-    {
f9a98e
-        error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)class_signature_ptr);
f9a98e
-        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
-    }
f9a98e
-    if (exception_name_ptr != NULL)
f9a98e
-    {
f9a98e
-        error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)exception_name_ptr);
f9a98e
-        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
f9a98e
+        free(exception_type_name);
f9a98e
     }
f9a98e
 
f9a98e
     exit_critical_section(jvmti_env, shared_lock);
f9a98e
-- 
f9a98e
1.8.3.1
f9a98e