Blame SOURCES/satyr-0.13-support-unwinding-from-core-hook.patch

f41043
From 91cdbeb45d11cfd1dd62afe50f80a7c5d4342f5b Mon Sep 17 00:00:00 2001
f41043
From: Martin Milata <mmilata@redhat.com>
f41043
Date: Mon, 24 Nov 2014 14:36:14 +0100
f41043
Subject: [SATYR PATCH] unwind: support unwinding from core hook
f41043
f41043
In-memory unwinding of dying processes, related to abrt/abrt#gh829. The
f41043
implementation mostly mirrors code in src/stack.c of elfutils repo
f41043
(jankratochvil/corepattern branch).
f41043
f41043
Related to abrt/abrt#829.
f41043
f41043
Signed-off-by: Martin Milata <mmilata@redhat.com>
f41043
---
f41043
 include/abrt.h             |   8 +++
f41043
 include/core/unwind.h      |  18 ++++++
f41043
 lib/abrt.c                 |  35 ++++++++++++
f41043
 lib/core_unwind.c          |  14 +++++
f41043
 lib/core_unwind_elfutils.c | 136 +++++++++++++++++++++++++++++++++++++++++++++
f41043
 5 files changed, 211 insertions(+)
f41043
f41043
diff --git a/include/abrt.h b/include/abrt.h
f41043
index 0a8f5ac..5116446 100644
f41043
--- a/include/abrt.h
f41043
+++ b/include/abrt.h
f41043
@@ -26,6 +26,7 @@ extern "C" {
f41043
 
f41043
 #include "report_type.h"
f41043
 #include <stdbool.h>
f41043
+#include <sys/types.h>
f41043
 
f41043
 bool
f41043
 sr_abrt_print_report_from_dir(const char *directory,
f41043
@@ -43,6 +44,13 @@ sr_abrt_create_core_stacktrace_from_gdb(const char *directory,
f41043
                                         bool hash_fingerprints,
f41043
                                         char **error_message);
f41043
 
f41043
+bool
f41043
+sr_abrt_create_core_stacktrace_from_core_hook(const char *directory,
f41043
+                                              pid_t thread_id,
f41043
+                                              const char *executable,
f41043
+                                              int signum,
f41043
+                                              char **error_message);
f41043
+
f41043
 struct sr_rpm_package *
f41043
 sr_abrt_parse_dso_list(const char *text);
f41043
 
f41043
diff --git a/include/core/unwind.h b/include/core/unwind.h
f41043
index 8ab9d52..b128c76 100644
f41043
--- a/include/core/unwind.h
f41043
+++ b/include/core/unwind.h
f41043
@@ -24,6 +24,8 @@
f41043
 extern "C" {
f41043
 #endif
f41043
 
f41043
+#include <sys/types.h>
f41043
+
f41043
 struct sr_core_stacktrace;
f41043
 struct sr_gdb_stacktrace;
f41043
 
f41043
@@ -38,6 +40,22 @@ sr_core_stacktrace_from_gdb(const char *gdb_output,
f41043
                             const char *executable_filename,
f41043
                             char **error_message);
f41043
 
f41043
+/* This function can be used to unwind stack of live ("dying") process, invoked
f41043
+ * from the core dump hook (/proc/sys/kernel/core_pattern).
f41043
+ *
f41043
+ * Beware:
f41043
+ *
f41043
+ * - It can only unwind one thread of the process, the thread that caused the
f41043
+ *   terminating signal to be sent. You must supply that thread's tid.
f41043
+ * - The function calls close() on stdin, meaning that in the core handler you
f41043
+ *   cannot access the core image after calling this function.
f41043
+ */
f41043
+struct sr_core_stacktrace *
f41043
+sr_core_stacktrace_from_core_hook(pid_t thread_id,
f41043
+                                  const char *executable_filename,
f41043
+                                  int signum,
f41043
+                                  char **error_message);
f41043
+
f41043
 #ifdef __cplusplus
f41043
 }
f41043
 #endif
f41043
diff --git a/lib/abrt.c b/lib/abrt.c
f41043
index 39bc45d..585fdfb 100644
f41043
--- a/lib/abrt.c
f41043
+++ b/lib/abrt.c
f41043
@@ -138,6 +138,41 @@ sr_abrt_create_core_stacktrace(const char *directory,
f41043
                                   error_message);
f41043
 }
f41043
 
f41043
+bool
f41043
+sr_abrt_create_core_stacktrace_from_core_hook(const char *directory,
f41043
+                                              pid_t thread_id,
f41043
+                                              const char *executable,
f41043
+                                              int signum,
f41043
+                                              char **error_message)
f41043
+{
f41043
+
f41043
+    struct sr_core_stacktrace *core_stacktrace;
f41043
+    core_stacktrace = sr_core_stacktrace_from_core_hook(thread_id, executable,
f41043
+                                                        signum, error_message);
f41043
+
f41043
+    if (!core_stacktrace)
f41043
+        return false;
f41043
+
f41043
+    fulfill_missing_values(core_stacktrace);
f41043
+
f41043
+    char *json = sr_core_stacktrace_to_json(core_stacktrace);
f41043
+
f41043
+    // Add newline to the end of core stacktrace file to make text
f41043
+    // editors happy.
f41043
+    json = sr_realloc(json, strlen(json) + 2);
f41043
+    strcat(json, "\n");
f41043
+
f41043
+    char *core_backtrace_filename = sr_build_path(directory, "core_backtrace", NULL);
f41043
+    bool success = sr_string_to_file(core_backtrace_filename,
f41043
+                                    json,
f41043
+                                    error_message);
f41043
+
f41043
+    free(core_backtrace_filename);
f41043
+    free(json);
f41043
+    sr_core_stacktrace_free(core_stacktrace);
f41043
+    return success;
f41043
+}
f41043
+
f41043
 struct sr_rpm_package *
f41043
 sr_abrt_parse_dso_list(const char *text)
f41043
 {
f41043
diff --git a/lib/core_unwind.c b/lib/core_unwind.c
f41043
index 8b7cc22..7ea66da 100644
f41043
--- a/lib/core_unwind.c
f41043
+++ b/lib/core_unwind.c
f41043
@@ -52,6 +52,20 @@ sr_parse_coredump(const char *coredump_filename,
f41043
 
f41043
 #endif /* !defined WITH_LIBDWFL && !defined WITH_LIBUNWIND */
f41043
 
f41043
+#if !defined WITH_LIBDWFL
f41043
+
f41043
+struct sr_core_stacktrace *
f41043
+sr_core_stacktrace_from_core_hook(pid_t thread_id,
f41043
+                                  const char *executable_filename,
f41043
+                                  int signum,
f41043
+                                  char **error_message);
f41043
+{
f41043
+    *error_message = sr_asprintf("satyr is built without live process unwind support");
f41043
+    return NULL;
f41043
+}
f41043
+
f41043
+#endif /* !defined WITH_LIBDWFL */
f41043
+
f41043
 /* FIXME: is there another way to pass the executable name to the find_elf
f41043
  * callback? */
f41043
 const char *executable_file = NULL;
f41043
diff --git a/lib/core_unwind_elfutils.c b/lib/core_unwind_elfutils.c
f41043
index bbd4813..bea9b5f 100644
f41043
--- a/lib/core_unwind_elfutils.c
f41043
+++ b/lib/core_unwind_elfutils.c
f41043
@@ -28,6 +28,10 @@
f41043
 
f41043
 #include <stdio.h>
f41043
 #include <string.h>
f41043
+#include <unistd.h>
f41043
+#include <errno.h>
f41043
+#include <sys/ptrace.h>
f41043
+#include <sys/wait.h>
f41043
 
f41043
 #define FRAME_LIMIT 256
f41043
 
f41043
@@ -202,4 +206,136 @@ fail:
f41043
     return stacktrace;
f41043
 }
f41043
 
f41043
+struct sr_core_stacktrace *
f41043
+sr_core_stacktrace_from_core_hook(pid_t tid,
f41043
+                                  const char *executable,
f41043
+                                  int signum,
f41043
+                                  char **error_msg)
f41043
+{
f41043
+    struct sr_core_stacktrace *stacktrace = NULL;
f41043
+
f41043
+    /* Initialize error_msg to 'no error'. */
f41043
+    if (error_msg)
f41043
+        *error_msg = NULL;
f41043
+
f41043
+    const Dwfl_Callbacks proc_cb =
f41043
+    {
f41043
+        .find_elf = dwfl_linux_proc_find_elf,
f41043
+        .find_debuginfo = find_debuginfo_none,
f41043
+    };
f41043
+
f41043
+    Dwfl *dwfl = dwfl_begin(&proc_cb);
f41043
+
f41043
+    if (dwfl_linux_proc_report(dwfl, tid) != 0)
f41043
+    {
f41043
+        set_error_dwfl("dwfl_linux_proc_report");
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (dwfl_report_end(dwfl, NULL, NULL) != 0)
f41043
+    {
f41043
+        set_error_dwfl("dwfl_report_end");
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (ptrace(PTRACE_SEIZE, tid, NULL, (void *)(uintptr_t)PTRACE_O_TRACEEXIT) != 0)
f41043
+    {
f41043
+        set_error("PTRACE_SEIZE (tid %u) failed: %s", (unsigned)tid, strerror(errno));
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (close(STDIN_FILENO) != 0)
f41043
+    {
f41043
+        set_error("Failed to close stdin: %s", strerror(errno));
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    int status;
f41043
+    pid_t got = waitpid(tid, &status, 0);
f41043
+    if (got == -1)
f41043
+    {
f41043
+        set_error("waitpid failed: %s", strerror(errno));
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (got != tid)
f41043
+    {
f41043
+        set_error("waitpid returned %u but %u was expected", (unsigned)got, (unsigned)tid);
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (!WIFSTOPPED(status))
f41043
+    {
f41043
+        set_error("waitpid returned 0x%x but WIFSTOPPED was expected", status);
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if ((status >> 8) != (SIGTRAP | (PTRACE_EVENT_EXIT << 8)))
f41043
+    {
f41043
+        set_error("waitpid returned 0x%x but (status >> 8) == "
f41043
+                  "(SIGTRAP | (PTRACE_EVENT_EXIT << 8)) was expected", status);
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    if (dwfl_linux_proc_attach(dwfl, tid, true) != 0)
f41043
+    {
f41043
+        set_error_dwfl("dwfl_linux_proc_attach");
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    stacktrace = sr_core_stacktrace_new();
f41043
+    if (!stacktrace)
f41043
+    {
f41043
+        set_error("Failed to initialize stacktrace memory");
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    stacktrace->threads = sr_core_thread_new();
f41043
+    if (!stacktrace->threads)
f41043
+    {
f41043
+        set_error("Failed to initialize thread memory");
f41043
+        sr_core_stacktrace_free(stacktrace);
f41043
+        stacktrace = 0;
f41043
+        goto fail;
f41043
+    }
f41043
+    stacktrace->threads->id = tid;
f41043
+
f41043
+    struct frame_callback_arg frame_arg =
f41043
+    {
f41043
+        .frames_tail = &(stacktrace->threads->frames),
f41043
+        .error_msg = NULL,
f41043
+        .nframes = 0
f41043
+    };
f41043
+
f41043
+    int ret = dwfl_getthread_frames(dwfl, tid, frame_callback, &frame_arg);
f41043
+    if (ret != 0 && ret != CB_STOP_UNWIND)
f41043
+    {
f41043
+        if (ret == -1)
f41043
+            set_error_dwfl("dwfl_getthread_frames");
f41043
+        else if (ret == DWARF_CB_ABORT)
f41043
+        {
f41043
+            set_error("%s", frame_arg.error_msg);
f41043
+            free(frame_arg.error_msg);
f41043
+        }
f41043
+        else
f41043
+            set_error("Unknown error in dwfl_getthreads");
f41043
+
f41043
+        sr_core_stacktrace_free(stacktrace);
f41043
+        stacktrace = NULL;
f41043
+        goto fail;
f41043
+    }
f41043
+
f41043
+    truncate_long_thread(stacktrace->threads, &frame_arg);
f41043
+
f41043
+    if (executable)
f41043
+        stacktrace->executable = sr_strdup(executable);
f41043
+    if (signum > 0)
f41043
+        stacktrace->signal = (uint16_t)signum;
f41043
+    stacktrace->crash_thread = stacktrace->threads;
f41043
+
f41043
+fail:
f41043
+    dwfl_end(dwfl);
f41043
+    return stacktrace;
f41043
+}
f41043
+
f41043
 #endif /* WITH_LIBDWFL */
f41043
-- 
f41043
2.4.3
f41043