Blame SOURCES/0005-Unify-reason-message-format.patch

f9a98e
From 9eaeea7851ff8682b7b6289dd9bded87d29c6fdd Mon Sep 17 00:00:00 2001
f9a98e
From: Jakub Filak <jfilak@redhat.com>
f9a98e
Date: Fri, 25 Oct 2013 15:46:43 +0200
f9a98e
Subject: [PATCH 05/39] Unify reason message format
f9a98e
f9a98e
Use the same message for abrt and log:
f9a98e
f9a98e
(Caught|Uncaght) exception $FQDN_TYPE in method $FQDN_METHOD
f9a98e
f9a98e
The new format misses thread name and method signature. Thread's name is
f9a98e
available in stack trace and method's signature is useful only for
f9a98e
debugging.
f9a98e
f9a98e
Length of the reason message is limited to 255 characters. If the
f9a98e
message exceeds that limit, algorithm tries to generate the message
f9a98e
according to the following formats:
f9a98e
f9a98e
(Caught|Uncaght) exception $FQDN_TYPE in method $CLASS_NAME_METHOD
f9a98e
(Caught|Uncaght) exception $EXCEPTION_CLASS in method $CLASS_NAME_METHOD
f9a98e
(Caught|Uncaght) exception $EXCEPTION_CLASS in method $METHOD
f9a98e
f9a98e
The first suitable message is used. If none of these formats generate an
f9a98e
acceptable message, the last format is used and the message is truncated.
f9a98e
f9a98e
Related to rhbz#1023081
f9a98e
Related to rhbz#1055581
f9a98e
f9a98e
Signed-off-by: Jakub Filak <jfilak@redhat.com>
f9a98e
---
f9a98e
 src/abrt-checker.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++--------
f9a98e
 1 file changed, 85 insertions(+), 14 deletions(-)
f9a98e
f9a98e
diff --git a/src/abrt-checker.c b/src/abrt-checker.c
f9a98e
index 22f772e..23e76b0 100644
f9a98e
--- a/src/abrt-checker.c
f9a98e
+++ b/src/abrt-checker.c
f9a98e
@@ -87,6 +87,9 @@
f9a98e
 /* Don't need to be changed */
f9a98e
 #define MAX_THREAD_NAME_LENGTH 40
f9a98e
 
f9a98e
+/* Max. length of reason message */
f9a98e
+#define MAX_REASON_MESSAGE_STRING_LENGTH 255
f9a98e
+
f9a98e
 /* Max. length of stack trace */
f9a98e
 #define MAX_STACK_TRACE_STRING_LENGTH 10000
f9a98e
 
f9a98e
@@ -402,7 +405,7 @@ static void add_process_properties_data(problem_data_t *pd)
f9a98e
  * Register new ABRT event using given message and a method name.
f9a98e
  * If reportErrosTo global flags doesn't contain ED_ABRT, this function does nothing.
f9a98e
  */
f9a98e
-static void register_abrt_event(char * executable, char * message, unsigned char * method, char * backtrace)
f9a98e
+static void register_abrt_event(char * executable, char * message, char * backtrace)
f9a98e
 {
f9a98e
     if ((reportErrosTo & ED_ABRT) == 0)
f9a98e
     {
f9a98e
@@ -410,7 +413,6 @@ static void register_abrt_event(char * executable, char * message, unsigned char
f9a98e
         return;
f9a98e
     }
f9a98e
 
f9a98e
-    char abrt_message[1000];
f9a98e
     char s[11];
f9a98e
     problem_data_t *pd = problem_data_new();
f9a98e
 
f9a98e
@@ -421,14 +423,12 @@ static void register_abrt_event(char * executable, char * message, unsigned char
f9a98e
     get_uid_as_string(s);
f9a98e
     problem_data_add_text_editable(pd, FILENAME_UID, s);
f9a98e
 
f9a98e
-    sprintf(abrt_message, "%s in method %s", message, method);
f9a98e
-
f9a98e
     /* executable must belong to some package otherwise ABRT refuse it */
f9a98e
     problem_data_add_text_editable(pd, FILENAME_EXECUTABLE, executable);
f9a98e
     problem_data_add_text_editable(pd, FILENAME_BACKTRACE, backtrace);
f9a98e
 
f9a98e
     /* type and analyzer are the same for abrt, we keep both just for sake of comaptibility */
f9a98e
-    problem_data_add_text_editable(pd, FILENAME_REASON, abrt_message);
f9a98e
+    problem_data_add_text_editable(pd, FILENAME_REASON, message);
f9a98e
     /* end of required fields */
f9a98e
 
f9a98e
     /* add optional fields */
f9a98e
@@ -489,6 +489,66 @@ static int get_tid(
f9a98e
 
f9a98e
 
f9a98e
 
f9a98e
+static char *format_exception_reason_message(
f9a98e
+        int caught,
f9a98e
+        const char *exception_fqdn,
f9a98e
+        const char *class_fqdn,
f9a98e
+        const char *method)
f9a98e
+{
f9a98e
+    const char *exception_name = exception_fqdn;
f9a98e
+    const char *class_name = class_fqdn;
f9a98e
+    const char *prefix = caught ? "Caught" : "Uncaught";
f9a98e
+
f9a98e
+    char *message = (char*)calloc(MAX_REASON_MESSAGE_STRING_LENGTH + 1, sizeof(char));
f9a98e
+    if (message == NULL)
f9a98e
+    {
f9a98e
+        fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": calloc(): out of memory");
f9a98e
+        return NULL;
f9a98e
+    }
f9a98e
+
f9a98e
+    while (1)
f9a98e
+    {
f9a98e
+        const int message_len = snprintf(message, MAX_REASON_MESSAGE_STRING_LENGTH,
f9a98e
+                "%s exception %s in method %s%s%s()", prefix,
f9a98e
+                exception_name, class_name, ('\0' != class_name[0] ? "." : ""),
f9a98e
+                method);
f9a98e
+
f9a98e
+        if (message_len <= 0)
f9a98e
+        {
f9a98e
+            fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": snprintf(): can't print reason message to memory on stack\n");
f9a98e
+            free(message);
f9a98e
+            return NULL;
f9a98e
+        }
f9a98e
+        else if (message_len >= MAX_REASON_MESSAGE_STRING_LENGTH)
f9a98e
+        {
f9a98e
+            const char *ptr = NULL;
f9a98e
+            if (NULL != (ptr = strrchr(class_name, '.')))
f9a98e
+            {
f9a98e
+                /* Drop name space from method signature */
f9a98e
+                class_name = ptr + 1;
f9a98e
+                continue;
f9a98e
+            }
f9a98e
+            else if(NULL != (ptr = strrchr(exception_name, '.')))
f9a98e
+            {
f9a98e
+                /* Drop name space from exception class signature */
f9a98e
+                exception_name = ptr + 1;
f9a98e
+                continue;
f9a98e
+            }
f9a98e
+            else if (class_name[0] != '\0')
f9a98e
+            {
f9a98e
+                /* Drop class name from method signature */
f9a98e
+                class_name += strlen(class_name);
f9a98e
+                continue;
f9a98e
+            }
f9a98e
+            /* No more place for shortening. The message will remain truncated. */
f9a98e
+        }
f9a98e
+
f9a98e
+        return message;
f9a98e
+    }
f9a98e
+}
f9a98e
+
f9a98e
+
f9a98e
+
f9a98e
 /*
f9a98e
  * Format class signature into a printable form.
f9a98e
  * Class names have form "Ljava/lang/String;"
f9a98e
@@ -1892,19 +1952,30 @@ static void JNICALL callback_on_exception(
f9a98e
                 jthrowable_circular_buf_push(threads_exc_buf, exception_object);
f9a98e
             }
f9a98e
 
f9a98e
-            log_print("%s %s exception in thread \"%s\" ", (catch_method == NULL ? "Uncaught" : "Caught"), updated_exception_name_ptr, tname);
f9a98e
-            log_print("in a method %s%s() with signature %s\n", class_name_ptr, method_name_ptr, method_signature_ptr);
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
+            char *message = format_exception_reason_message(/*caught?*/NULL != catch_method,
f9a98e
+                    updated_exception_name_ptr, class_name_ptr, method_name_ptr);
f9a98e
 
f9a98e
-            //char *stack_trace_str = generate_stack_trace(jvmti_env, jni_env, thr, tname, updated_exception_name_ptr);
f9a98e
-            char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
f9a98e
-            if (NULL != stack_trace_str)
f9a98e
+            if (NULL != message)
f9a98e
             {
f9a98e
-                log_print("%s", stack_trace_str);
f9a98e
-                if (NULL != threads_exc_buf)
f9a98e
+                log_print("%s\n", message);
f9a98e
+
f9a98e
+                //char *stack_trace_str = generate_stack_trace(jvmti_env, jni_env, thr, tname, updated_exception_name_ptr);
f9a98e
+                char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
f9a98e
+                if (NULL != stack_trace_str)
f9a98e
                 {
f9a98e
-                    register_abrt_event(processProperties.main_class, (catch_method == NULL ? "Uncaught exception" : "Caught exception"), (unsigned char *)method_name_ptr, stack_trace_str);
f9a98e
+                    log_print("%s", stack_trace_str);
f9a98e
+                    if (NULL != threads_exc_buf)
f9a98e
+                    {
f9a98e
+                        register_abrt_event(processProperties.main_class, message, stack_trace_str);
f9a98e
+                    }
f9a98e
+                    free(stack_trace_str);
f9a98e
                 }
f9a98e
-                free(stack_trace_str);
f9a98e
+                free(message);
f9a98e
             }
f9a98e
         }
f9a98e
         else
f9a98e
-- 
f9a98e
1.8.3.1
f9a98e