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

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