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

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