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

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