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

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