Blob Blame Raw
From 9eaeea7851ff8682b7b6289dd9bded87d29c6fdd Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Fri, 25 Oct 2013 15:46:43 +0200
Subject: [PATCH 05/39] Unify reason message format

Use the same message for abrt and log:

(Caught|Uncaght) exception $FQDN_TYPE in method $FQDN_METHOD

The new format misses thread name and method signature. Thread's name is
available in stack trace and method's signature is useful only for
debugging.

Length of the reason message is limited to 255 characters. If the
message exceeds that limit, algorithm tries to generate the message
according to the following formats:

(Caught|Uncaght) exception $FQDN_TYPE in method $CLASS_NAME_METHOD
(Caught|Uncaght) exception $EXCEPTION_CLASS in method $CLASS_NAME_METHOD
(Caught|Uncaght) exception $EXCEPTION_CLASS in method $METHOD

The first suitable message is used. If none of these formats generate an
acceptable message, the last format is used and the message is truncated.

Related to rhbz#1023081
Related to rhbz#1055581

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/abrt-checker.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 85 insertions(+), 14 deletions(-)

diff --git a/src/abrt-checker.c b/src/abrt-checker.c
index 22f772e..23e76b0 100644
--- a/src/abrt-checker.c
+++ b/src/abrt-checker.c
@@ -87,6 +87,9 @@
 /* Don't need to be changed */
 #define MAX_THREAD_NAME_LENGTH 40
 
+/* Max. length of reason message */
+#define MAX_REASON_MESSAGE_STRING_LENGTH 255
+
 /* Max. length of stack trace */
 #define MAX_STACK_TRACE_STRING_LENGTH 10000
 
@@ -402,7 +405,7 @@ static void add_process_properties_data(problem_data_t *pd)
  * Register new ABRT event using given message and a method name.
  * If reportErrosTo global flags doesn't contain ED_ABRT, this function does nothing.
  */
-static void register_abrt_event(char * executable, char * message, unsigned char * method, char * backtrace)
+static void register_abrt_event(char * executable, char * message, char * backtrace)
 {
     if ((reportErrosTo & ED_ABRT) == 0)
     {
@@ -410,7 +413,6 @@ static void register_abrt_event(char * executable, char * message, unsigned char
         return;
     }
 
-    char abrt_message[1000];
     char s[11];
     problem_data_t *pd = problem_data_new();
 
@@ -421,14 +423,12 @@ static void register_abrt_event(char * executable, char * message, unsigned char
     get_uid_as_string(s);
     problem_data_add_text_editable(pd, FILENAME_UID, s);
 
-    sprintf(abrt_message, "%s in method %s", message, method);
-
     /* executable must belong to some package otherwise ABRT refuse it */
     problem_data_add_text_editable(pd, FILENAME_EXECUTABLE, executable);
     problem_data_add_text_editable(pd, FILENAME_BACKTRACE, backtrace);
 
     /* type and analyzer are the same for abrt, we keep both just for sake of comaptibility */
-    problem_data_add_text_editable(pd, FILENAME_REASON, abrt_message);
+    problem_data_add_text_editable(pd, FILENAME_REASON, message);
     /* end of required fields */
 
     /* add optional fields */
@@ -489,6 +489,66 @@ static int get_tid(
 
 
 
+static char *format_exception_reason_message(
+        int caught,
+        const char *exception_fqdn,
+        const char *class_fqdn,
+        const char *method)
+{
+    const char *exception_name = exception_fqdn;
+    const char *class_name = class_fqdn;
+    const char *prefix = caught ? "Caught" : "Uncaught";
+
+    char *message = (char*)calloc(MAX_REASON_MESSAGE_STRING_LENGTH + 1, sizeof(char));
+    if (message == NULL)
+    {
+        fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": calloc(): out of memory");
+        return NULL;
+    }
+
+    while (1)
+    {
+        const int message_len = snprintf(message, MAX_REASON_MESSAGE_STRING_LENGTH,
+                "%s exception %s in method %s%s%s()", prefix,
+                exception_name, class_name, ('\0' != class_name[0] ? "." : ""),
+                method);
+
+        if (message_len <= 0)
+        {
+            fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": snprintf(): can't print reason message to memory on stack\n");
+            free(message);
+            return NULL;
+        }
+        else if (message_len >= MAX_REASON_MESSAGE_STRING_LENGTH)
+        {
+            const char *ptr = NULL;
+            if (NULL != (ptr = strrchr(class_name, '.')))
+            {
+                /* Drop name space from method signature */
+                class_name = ptr + 1;
+                continue;
+            }
+            else if(NULL != (ptr = strrchr(exception_name, '.')))
+            {
+                /* Drop name space from exception class signature */
+                exception_name = ptr + 1;
+                continue;
+            }
+            else if (class_name[0] != '\0')
+            {
+                /* Drop class name from method signature */
+                class_name += strlen(class_name);
+                continue;
+            }
+            /* No more place for shortening. The message will remain truncated. */
+        }
+
+        return message;
+    }
+}
+
+
+
 /*
  * Format class signature into a printable form.
  * Class names have form "Ljava/lang/String;"
@@ -1892,19 +1952,30 @@ static void JNICALL callback_on_exception(
                 jthrowable_circular_buf_push(threads_exc_buf, exception_object);
             }
 
-            log_print("%s %s exception in thread \"%s\" ", (catch_method == NULL ? "Uncaught" : "Caught"), updated_exception_name_ptr, tname);
-            log_print("in a method %s%s() with signature %s\n", class_name_ptr, method_name_ptr, method_signature_ptr);
+            /* Remove trailing '.' */
+            const ssize_t class_name_len = strlen(class_name_ptr);
+            if (class_name_len > 0)
+                class_name_ptr[class_name_len - 1] = '\0';
+
+            char *message = format_exception_reason_message(/*caught?*/NULL != catch_method,
+                    updated_exception_name_ptr, class_name_ptr, method_name_ptr);
 
-            //char *stack_trace_str = generate_stack_trace(jvmti_env, jni_env, thr, tname, updated_exception_name_ptr);
-            char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
-            if (NULL != stack_trace_str)
+            if (NULL != message)
             {
-                log_print("%s", stack_trace_str);
-                if (NULL != threads_exc_buf)
+                log_print("%s\n", message);
+
+                //char *stack_trace_str = generate_stack_trace(jvmti_env, jni_env, thr, tname, updated_exception_name_ptr);
+                char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
+                if (NULL != stack_trace_str)
                 {
-                    register_abrt_event(processProperties.main_class, (catch_method == NULL ? "Uncaught exception" : "Caught exception"), (unsigned char *)method_name_ptr, stack_trace_str);
+                    log_print("%s", stack_trace_str);
+                    if (NULL != threads_exc_buf)
+                    {
+                        register_abrt_event(processProperties.main_class, message, stack_trace_str);
+                    }
+                    free(stack_trace_str);
                 }
-                free(stack_trace_str);
+                free(message);
             }
         }
         else
-- 
1.8.3.1