mfabik / rpms / satyr

Forked from rpms/satyr 3 years ago
Clone

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

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