mfabik / rpms / satyr

Forked from rpms/satyr 3 years ago
Clone

Blame SOURCES/satyr-0.13-elfutils-unwinder.patch

f41043
From 17853e2316aed32d56343b2c4a73fa82b43a078d Mon Sep 17 00:00:00 2001
f41043
From: Martin Milata <mmilata@redhat.com>
f41043
Date: Mon, 21 Oct 2013 14:53:02 +0200
f41043
Subject: [SATYR PATCH 2/3] unwind: Port to current API of elfutils unwinder
f41043
f41043
Related to rhbz#1051569.
f41043
f41043
Signed-off-by: Martin Milata <mmilata@redhat.com>
f41043
---
f41043
 configure.ac               |   2 +-
f41043
 lib/core_unwind_elfutils.c | 164 +++++++++++++++++++++++++++++----------------
f41043
 lib/internal_unwind.h      |   2 +-
f41043
 3 files changed, 110 insertions(+), 58 deletions(-)
f41043
f41043
diff --git a/configure.ac b/configure.ac
f41043
index af6cb3f..cf3a193 100644
f41043
--- a/configure.ac
f41043
+++ b/configure.ac
f41043
@@ -104,7 +104,7 @@ AC_CHECK_LIB([elf], [main])
f41043
 AC_CHECK_LIB([dw], [main])
f41043
 AC_CHECK_LIB([dwfl], [main])
f41043
 AC_CHECK_LIB([dl], [main])
f41043
-AC_CHECK_FUNC(dwfl_frame_state_core, AC_DEFINE(HAVE_DWFL_FRAME_STATE_CORE, [], [Have function dwfl_frame_state_core for coredump unwinding]))
f41043
+AC_CHECK_FUNC(dwfl_getthreads, AC_DEFINE(HAVE_DWFL_NEXT_THREAD, [], [Have function dwfl_getthreads for coredump unwinding]))
f41043
 
f41043
 # libunwind
f41043
 AC_CHECK_HEADERS([libunwind-coredump.h])
f41043
diff --git a/lib/core_unwind_elfutils.c b/lib/core_unwind_elfutils.c
f41043
index 9a489ac..a8d8b3f 100644
f41043
--- a/lib/core_unwind_elfutils.c
f41043
+++ b/lib/core_unwind_elfutils.c
f41043
@@ -29,59 +29,108 @@
f41043
 #include <stdio.h>
f41043
 #include <string.h>
f41043
 
f41043
-static struct sr_core_thread *
f41043
-unwind_thread(Dwfl *dwfl, Dwfl_Frame_State *state, char **error_msg)
f41043
+struct frame_callback_arg
f41043
 {
f41043
-    struct sr_core_frame *head = NULL, *tail = NULL;
f41043
-    pid_t tid = 0;
f41043
+    struct sr_core_thread *thread;
f41043
+    char *error_msg;
f41043
+};
f41043
 
f41043
-    if (state)
f41043
+struct thread_callback_arg
f41043
+{
f41043
+    struct sr_core_stacktrace *stacktrace;
f41043
+    char *error_msg;
f41043
+};
f41043
+
f41043
+static int CB_STOP_UNWIND = DWARF_CB_ABORT+1;
f41043
+
f41043
+static int
f41043
+frame_callback(Dwfl_Frame *frame, void *data)
f41043
+{
f41043
+    struct frame_callback_arg *frame_arg = data;
f41043
+    char **error_msg = &frame_arg->error_msg;
f41043
+
f41043
+    Dwarf_Addr pc;
f41043
+    bool minus_one;
f41043
+    if (!dwfl_frame_pc(frame, &pc, &minus_one))
f41043
     {
f41043
-        tid = dwfl_frame_tid_get(state);
f41043
+        set_error_dwfl("dwfl_frame_pc");
f41043
+        return DWARF_CB_ABORT;
f41043
     }
f41043
 
f41043
-    while (state)
f41043
+    Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(frame));
f41043
+    struct sr_core_frame *result = resolve_frame(dwfl, pc, minus_one);
f41043
+
f41043
+    /* Do not unwind below __libc_start_main. */
f41043
+    if (0 == sr_strcmp0(result->function_name, "__libc_start_main"))
f41043
     {
f41043
-        Dwarf_Addr pc;
f41043
-        bool minus_one;
f41043
-        if (!dwfl_frame_state_pc(state, &pc, &minus_one))
f41043
-        {
f41043
-            warn("Failed to obtain PC: %s", dwfl_errmsg(-1));
f41043
-            break;
f41043
-        }
f41043
-
f41043
-        struct sr_core_frame *frame = resolve_frame(dwfl, pc, minus_one);
f41043
-        list_append(head, tail, frame);
f41043
-
f41043
-        /* Do not unwind below __libc_start_main. */
f41043
-        if (0 == sr_strcmp0(frame->function_name, "__libc_start_main"))
f41043
-            break;
f41043
-
f41043
-        if (!dwfl_frame_unwind(&state))
f41043
-        {
f41043
-            warn("Cannot unwind frame: %s", dwfl_errmsg(-1));
f41043
-            break;
f41043
-        }
f41043
+        sr_core_frame_free(result);
f41043
+        return CB_STOP_UNWIND;
f41043
     }
f41043
 
f41043
-    if (!error_msg && !head)
f41043
+    frame_arg->thread->frames =
f41043
+        sr_core_frame_append(frame_arg->thread->frames, result);
f41043
+
f41043
+    return DWARF_CB_OK;
f41043
+}
f41043
+
f41043
+static int
f41043
+unwind_thread(Dwfl_Thread *thread, void *data)
f41043
+{
f41043
+    struct thread_callback_arg *thread_arg = data;
f41043
+    char **error_msg = &thread_arg->error_msg;
f41043
+
f41043
+    struct sr_core_thread *result = sr_core_thread_new();
f41043
+    if (!result)
f41043
     {
f41043
-        set_error("No frames found for thread id %d", (int)tid);
f41043
+        set_error("Failed to initialize stacktrace memory");
f41043
+        return DWARF_CB_ABORT;
f41043
     }
f41043
+    result->id = (int64_t)dwfl_thread_tid(thread);
f41043
+
f41043
+    struct frame_callback_arg frame_arg =
f41043
+    {
f41043
+        .thread = result,
f41043
+        .error_msg = NULL
f41043
+    };
f41043
 
f41043
-    struct sr_core_thread *thread = sr_core_thread_new();
f41043
-    thread->id = (int64_t)tid;
f41043
-    thread->frames = head;
f41043
-    return thread;
f41043
+    int ret = dwfl_thread_getframes(thread, frame_callback, &frame_arg);
f41043
+    if (ret == -1)
f41043
+    {
f41043
+        warn("dwfl_thread_getframes failed for thread id %d: %s",
f41043
+             (int)result->id, dwfl_errmsg(-1));
f41043
+    }
f41043
+    else if (ret == DWARF_CB_ABORT)
f41043
+    {
f41043
+        *error_msg = frame_arg.error_msg;
f41043
+        goto abort;
f41043
+    }
f41043
+    else if (ret != 0 && ret != CB_STOP_UNWIND)
f41043
+    {
f41043
+        *error_msg = sr_strdup("Unknown error in dwfl_thread_getframes");
f41043
+        goto abort;
f41043
+    }
f41043
+
f41043
+    if (!error_msg && !frame_arg.thread->frames)
f41043
+    {
f41043
+        set_error("No frames found for thread id %d", (int)result->id);
f41043
+        goto abort;
f41043
+    }
f41043
+
f41043
+    thread_arg->stacktrace->threads =
f41043
+        sr_core_thread_append(thread_arg->stacktrace->threads, result);
f41043
+    return DWARF_CB_OK;
f41043
+
f41043
+abort:
f41043
+    sr_core_thread_free(result);
f41043
+    return DWARF_CB_ABORT;
f41043
 }
f41043
 
f41043
 struct sr_core_stacktrace *
f41043
 sr_parse_coredump(const char *core_file,
f41043
-                   const char *exe_file,
f41043
-                   char **error_msg)
f41043
+                  const char *exe_file,
f41043
+                  char **error_msg)
f41043
 {
f41043
     struct sr_core_stacktrace *stacktrace = NULL;
f41043
-    short signal = 0;
f41043
 
f41043
     /* Initialize error_msg to 'no error'. */
f41043
     if (error_msg)
f41043
@@ -91,11 +140,9 @@ sr_parse_coredump(const char *core_file,
f41043
     if (*error_msg)
f41043
         return NULL;
f41043
 
f41043
-    Dwfl_Frame_State *state = dwfl_frame_state_core(ch->dwfl, core_file);
f41043
-    if (!state)
f41043
+    if (dwfl_core_file_attach(ch->dwfl, ch->eh) < 0)
f41043
     {
f41043
-        set_error("Failed to initialize frame state from core '%s'",
f41043
-                   core_file);
f41043
+        set_error_dwfl("dwfl_core_file_attach");
f41043
         goto fail_destroy_handle;
f41043
     }
f41043
 
f41043
@@ -105,20 +152,30 @@ sr_parse_coredump(const char *core_file,
f41043
         set_error("Failed to initialize stacktrace memory");
f41043
         goto fail_destroy_handle;
f41043
     }
f41043
-    struct sr_core_thread *threads_tail = NULL;
f41043
 
f41043
-    do
f41043
+    struct thread_callback_arg thread_arg =
f41043
     {
f41043
-        struct sr_core_thread *t = unwind_thread(ch->dwfl, state, error_msg);
f41043
-        if (*error_msg)
f41043
-        {
f41043
-            goto fail_destroy_trace;
f41043
-        }
f41043
-        list_append(stacktrace->threads, threads_tail, t);
f41043
-        state = dwfl_frame_thread_next(state);
f41043
-    } while (state);
f41043
+        .stacktrace = stacktrace,
f41043
+        .error_msg = NULL
f41043
+    };
f41043
 
f41043
-    signal = get_signal_number(ch->eh, core_file);
f41043
+    int ret = dwfl_getthreads(ch->dwfl, unwind_thread, &thread_arg);
f41043
+    if (ret != 0)
f41043
+    {
f41043
+        if (ret == -1)
f41043
+            set_error_dwfl("dwfl_getthreads");
f41043
+        else if (ret == DWARF_CB_ABORT)
f41043
+            *error_msg = thread_arg.error_msg;
f41043
+        else
f41043
+            *error_msg = sr_strdup("Unknown error in dwfl_getthreads");
f41043
+
f41043
+        goto fail_destroy_trace;
f41043
+    }
f41043
+
f41043
+    stacktrace->executable = sr_strdup(exe_file);
f41043
+    stacktrace->signal = get_signal_number(ch->eh, core_file);
f41043
+    /* FIXME: is this the best we can do? */
f41043
+    stacktrace->crash_thread = stacktrace->threads;
f41043
 
f41043
 fail_destroy_trace:
f41043
     if (*error_msg)
f41043
@@ -128,11 +185,6 @@ fail_destroy_trace:
f41043
     }
f41043
 fail_destroy_handle:
f41043
     core_handle_free(ch);
f41043
-
f41043
-    stacktrace->executable = sr_strdup(exe_file);
f41043
-    stacktrace->signal = signal;
f41043
-    /* FIXME: is this the best we can do? */
f41043
-    stacktrace->crash_thread = stacktrace->threads;
f41043
     return stacktrace;
f41043
 }
f41043
 
f41043
diff --git a/lib/internal_unwind.h b/lib/internal_unwind.h
f41043
index d537f86..2c9abb7 100644
f41043
--- a/lib/internal_unwind.h
f41043
+++ b/lib/internal_unwind.h
f41043
@@ -27,7 +27,7 @@
f41043
 #include "config.h"
f41043
 
f41043
 /* define macros indicating what unwinder are we using */
f41043
-#if (defined HAVE_LIBELF_H && defined HAVE_GELF_H && defined HAVE_LIBELF && defined HAVE_LIBDW && defined HAVE_ELFUTILS_LIBDWFL_H && defined HAVE_DWFL_FRAME_STATE_CORE)
f41043
+#if (defined HAVE_LIBELF_H && defined HAVE_GELF_H && defined HAVE_LIBELF && defined HAVE_LIBDW && defined HAVE_ELFUTILS_LIBDWFL_H && defined HAVE_DWFL_NEXT_THREAD)
f41043
 #  define WITH_LIBDWFL
f41043
 #endif
f41043
 
f41043
-- 
f41043
1.8.3.1
f41043