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

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