mfabik / rpms / satyr

Forked from rpms/satyr 3 years ago
Clone

Blame SOURCES/satyr-0.13-debug-unwinding-from-core-hook-using-satyr-binary.patch

f41043
From 455c47f1f18858c439ff0a0b833ce93c9f79045f Mon Sep 17 00:00:00 2001
f41043
From: Martin Milata <mmilata@redhat.com>
f41043
Date: Mon, 24 Nov 2014 14:38:06 +0100
f41043
Subject: [PATCH 4/5] debug unwinding from core hook using satyr binary
f41043
f41043
Write "|/usr/bin/satyr debug unwind-from-hook %e %i %s" to
f41043
/proc/sys/kernel/core_pattern in order to use this. Before kernel 3.18
f41043
the %i flag is not available - you can use %p for single-thread
f41043
processes.
f41043
f41043
Related to abrt/abrt#829.
f41043
f41043
Signed-off-by: Martin Milata <mmilata@redhat.com>
f41043
---
f41043
 satyr.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
f41043
 1 file changed, 81 insertions(+)
f41043
f41043
diff --git a/satyr.c b/satyr.c
f41043
index 7bdaa45..6e1ed28 100644
f41043
--- a/satyr.c
f41043
+++ b/satyr.c
f41043
@@ -20,6 +20,8 @@
f41043
 #include "gdb/stacktrace.h"
f41043
 #include "gdb/thread.h"
f41043
 #include "gdb/frame.h"
f41043
+#include "core/unwind.h"
f41043
+#include "core/stacktrace.h"
f41043
 #include "utils.h"
f41043
 #include "location.h"
f41043
 #include "strbuf.h"
f41043
@@ -37,6 +39,7 @@
f41043
 #include <sysexits.h>
f41043
 #include <assert.h>
f41043
 #include <libgen.h>
f41043
+#include <time.h>
f41043
 
f41043
 static char *g_program_name;
f41043
 
f41043
@@ -275,6 +278,82 @@ debug_duphash(int argc, char **argv)
f41043
 }
f41043
 
f41043
 static void
f41043
+debug_unwind_from_hook(int argc, char **argv)
f41043
+{
f41043
+    if (argc != 3)
f41043
+    {
f41043
+        fprintf(stderr, "Wrong number of arguments.\n");
f41043
+        fprintf(stderr, "Usage: satyr debug unwind-from-hook "
f41043
+                        "<executable> <tid> <signal>\n");
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    char *executable = argv[0];
f41043
+    char *end;
f41043
+    unsigned long tid = strtoul(argv[1], &end, 10);
f41043
+    if (*end != '\0')
f41043
+    {
f41043
+        fprintf(stderr, "Wrong tid\n");
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    unsigned long signum = strtoul(argv[2], &end, 10);
f41043
+    if (*end != '\0')
f41043
+    {
f41043
+        fprintf(stderr, "Wrong signal number\n");
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    char *error_message = NULL;
f41043
+    struct sr_core_stacktrace *core_stacktrace;
f41043
+
f41043
+    core_stacktrace = sr_core_stacktrace_from_core_hook(tid, executable, signum,
f41043
+                                                        &error_message);
f41043
+    if (!core_stacktrace)
f41043
+    {
f41043
+        fprintf(stderr, "Unwind failed: %s\n", error_message);
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    char *json = sr_core_stacktrace_to_json(core_stacktrace);
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
+    time_t t = time(NULL);
f41043
+    struct tm *tm = localtime(&t);
f41043
+    char core_backtrace_filename[256];
f41043
+
f41043
+    if (tm == NULL)
f41043
+    {
f41043
+        perror("localtime");
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    if (strftime(core_backtrace_filename, sizeof(core_backtrace_filename),
f41043
+                 "/tmp/satyr-core-stacktrace-%F-%T.json", tm) == 0)
f41043
+    {
f41043
+        fprintf(stderr, "stftime failed\n");
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+
f41043
+    bool success = sr_string_to_file(core_backtrace_filename,
f41043
+                                    json,
f41043
+                                    &error_message);
f41043
+
f41043
+    if (!success)
f41043
+    {
f41043
+        fprintf(stderr, "Failed to write stacktrace: %s\n", error_message);
f41043
+        exit(1);
f41043
+    }
f41043
+
f41043
+    free(json);
f41043
+    sr_core_stacktrace_free(core_stacktrace);
f41043
+}
f41043
+
f41043
+static void
f41043
 debug(int argc, char **argv)
f41043
 {
f41043
     /* Debug command requires a subcommand. */
f41043
@@ -288,6 +367,8 @@ debug(int argc, char **argv)
f41043
         debug_normalize(argc - 1, argv + 1);
f41043
     else if (0 == strcmp(argv[0], "duphash"))
f41043
         debug_duphash(argc - 1, argv + 1);
f41043
+    else if (0 == strcmp(argv[0], "unwind-from-hook"))
f41043
+        debug_unwind_from_hook(argc - 1, argv + 1);
f41043
     else
f41043
     {
f41043
         fprintf(stderr, "Unknown debug subcommand.\n");
f41043
-- 
f41043
2.4.3
f41043