Blame SOURCES/0166-UUID-from-core-backtrace-if-coredump-is-missing.patch

baab13
From 54ff71a2bec4386c871b54100e02065384aefb63 Mon Sep 17 00:00:00 2001
baab13
From: Martin Milata <mmilata@redhat.com>
baab13
Date: Tue, 2 Dec 2014 12:44:49 +0100
baab13
Subject: [PATCH] UUID from core backtrace if coredump is missing
baab13
baab13
Closes #882.
baab13
baab13
Signed-off-by: Martin Milata <mmilata@redhat.com>
baab13
---
baab13
 src/plugins/abrt-action-analyze-c.c | 91 ++++++++++++++++++++++++++++++++++---
baab13
 1 file changed, 85 insertions(+), 6 deletions(-)
baab13
baab13
diff --git a/src/plugins/abrt-action-analyze-c.c b/src/plugins/abrt-action-analyze-c.c
baab13
index a53f4fb..f98cfe5 100644
baab13
--- a/src/plugins/abrt-action-analyze-c.c
baab13
+++ b/src/plugins/abrt-action-analyze-c.c
baab13
@@ -18,6 +18,13 @@
baab13
 */
baab13
 #include "libabrt.h"
baab13
 
baab13
+#include <glib.h>
baab13
+
baab13
+#include <satyr/thread.h>
baab13
+#include <satyr/core/stacktrace.h>
baab13
+#include <satyr/core/thread.h>
baab13
+#include <satyr/core/frame.h>
baab13
+
baab13
 static void trim_unstrip_output(char *result, const char *unstrip_n_output)
baab13
 {
baab13
     // lines look like this:
baab13
@@ -49,6 +56,62 @@ static void trim_unstrip_output(char *result, const char *unstrip_n_output)
baab13
     *dst = '\0';
baab13
 }
baab13
 
baab13
+static char *build_ids_from_core_backtrace(const char *dump_dir_name)
baab13
+{
baab13
+    char *error = NULL;
baab13
+    char *core_backtrace_path = xasprintf("%s/"FILENAME_CORE_BACKTRACE, dump_dir_name);
baab13
+    char *json = xmalloc_open_read_close(core_backtrace_path, /*maxsize:*/ NULL);
baab13
+    free(core_backtrace_path);
baab13
+
baab13
+    if (!json)
baab13
+        return NULL;
baab13
+
baab13
+    struct sr_core_stacktrace *stacktrace = sr_core_stacktrace_from_json_text(json, &error);
baab13
+    free(json);
baab13
+    if (!stacktrace)
baab13
+    {
baab13
+        if (error)
baab13
+        {
baab13
+            log_info("Failed to parse core backtrace: %s", error);
baab13
+            free(error);
baab13
+        }
baab13
+        return NULL;
baab13
+    }
baab13
+
baab13
+    struct sr_core_thread *thread = sr_core_stacktrace_find_crash_thread(stacktrace);
baab13
+    if (!thread)
baab13
+    {
baab13
+        log_info("Failed to find crash thread");
baab13
+        sr_core_stacktrace_free(stacktrace);
baab13
+        return NULL;
baab13
+    }
baab13
+
baab13
+    void *build_id_list = NULL;
baab13
+
baab13
+    struct strbuf *strbuf = strbuf_new();
baab13
+    for (struct sr_core_frame *frame = thread->frames;
baab13
+         frame;
baab13
+         frame = frame->next)
baab13
+    {
baab13
+        if (frame->build_id)
baab13
+            build_id_list = g_list_prepend(build_id_list, frame->build_id);
baab13
+    }
baab13
+
baab13
+    build_id_list = g_list_sort(build_id_list, (GCompareFunc)strcmp);
baab13
+    for (GList *iter = build_id_list; iter; iter = g_list_next(iter))
baab13
+    {
baab13
+        GList *next = g_list_next(iter);
baab13
+        if (next == NULL || 0 != strcmp(iter->data, next->data))
baab13
+        {
baab13
+            strbuf = strbuf_append_strf(strbuf, "%s\n", (char *)iter->data);
baab13
+        }
baab13
+    }
baab13
+    g_list_free(build_id_list);
baab13
+    sr_core_stacktrace_free(stacktrace);
baab13
+
baab13
+    return strbuf_free_nobuf(strbuf);
baab13
+}
baab13
+
baab13
 int main(int argc, char **argv)
baab13
 {
baab13
     /* I18n */
baab13
@@ -82,13 +145,27 @@ int main(int argc, char **argv)
baab13
 
baab13
     export_abrt_envvars(0);
baab13
 
baab13
-    /* Run unstrip -n and trim its output, leaving only sizes and build ids */
baab13
+    char *unstrip_n_output = NULL;
baab13
+    char *coredump_path = xasprintf("%s/"FILENAME_COREDUMP, dump_dir_name);
baab13
+    if (access(coredump_path, R_OK) == 0)
baab13
+        unstrip_n_output = run_unstrip_n(dump_dir_name, /*timeout_sec:*/ 30);
baab13
 
baab13
-    char *unstrip_n_output = run_unstrip_n(dump_dir_name, /*timeout_sec:*/ 30);
baab13
-    if (!unstrip_n_output)
baab13
-        return 1; /* bad dump_dir_name, can't run unstrip, etc... */
baab13
-    /* modifies unstrip_n_output in-place: */
baab13
-    trim_unstrip_output(unstrip_n_output, unstrip_n_output);
baab13
+    free(coredump_path);
baab13
+
baab13
+    if (unstrip_n_output)
baab13
+    {
baab13
+        /* Run unstrip -n and trim its output, leaving only sizes and build ids */
baab13
+        /* modifies unstrip_n_output in-place: */
baab13
+        trim_unstrip_output(unstrip_n_output, unstrip_n_output);
baab13
+    }
baab13
+    else
baab13
+    {
baab13
+        /* bad dump_dir_name, can't run unstrip, etc...
baab13
+         * or maybe missing coredump - try generating it from core_backtrace
baab13
+         */
baab13
+
baab13
+        unstrip_n_output = build_ids_from_core_backtrace(dump_dir_name);
baab13
+    }
baab13
 
baab13
     /* Hash package + executable + unstrip_n_output and save it as UUID */
baab13
 
baab13
@@ -131,6 +208,8 @@ int main(int argc, char **argv)
baab13
     /*free(executable);*/
baab13
     /*free(unstrip_n_output);*/
baab13
 
baab13
+    log_debug("String to hash: %s", string_to_hash);
baab13
+
baab13
     char hash_str[SHA1_RESULT_LEN*2 + 1];
baab13
     str_to_sha1str(hash_str, string_to_hash);
baab13
 
baab13
-- 
baab13
2.4.3
baab13