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

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