Blame SOURCES/0148-Create-core-backtrace-in-unwind-hook.patch

47a88a
From 8eefbac3b67756f0dfe9d68741d70015023b5216 Mon Sep 17 00:00:00 2001
47a88a
From: Martin Milata <mmilata@redhat.com>
47a88a
Date: Fri, 17 Jul 2015 12:52:49 +0200
47a88a
Subject: [PATCH] Create core backtrace in unwind hook
47a88a
47a88a
Related to #829.
47a88a
47a88a
We need to implement #882 in order for this to work. This change
47a88a
requires (yet unreleased) satyr-0.16.
47a88a
47a88a
The feature is turned off by default, you need to pass
47a88a
--enable-dump-time-unwind to configure in order to enable it.
47a88a
47a88a
Signed-off-by: Martin Milata <mmilata@redhat.com>
47a88a
47a88a
Conflicts:
47a88a
	src/hooks/abrt-hook-ccpp.c
47a88a
---
47a88a
 configure.ac                        |  12 ++++
47a88a
 doc/abrt-CCpp.conf.txt              |  18 ++++++
47a88a
 src/hooks/CCpp.conf                 |  15 +++++
47a88a
 src/hooks/abrt-hook-ccpp.c          | 108 ++++++++++++++++++++++++------------
47a88a
 src/hooks/abrt-install-ccpp-hook.in |   4 +-
47a88a
 5 files changed, 121 insertions(+), 36 deletions(-)
47a88a
47a88a
diff --git a/configure.ac b/configure.ac
47a88a
index 56b8ad8..330dd9c 100644
47a88a
--- a/configure.ac
47a88a
+++ b/configure.ac
47a88a
@@ -232,6 +232,18 @@ AC_ARG_ENABLE([native-unwinder],
47a88a
 [fi]
47a88a
 
47a88a
 
47a88a
+# Perform stack unwind on live/dying process in the core handler?
47a88a
+
47a88a
+AC_ARG_ENABLE([dump-time-unwind],
47a88a
+    AS_HELP_STRING([--enable-dump-time-unwind],
47a88a
+        [create core stack trace while the crashed process is still in memory (default is no)]),
47a88a
+    [], [enable_dump_time_unwind=no])
47a88a
+
47a88a
+[if test "$enable_native_unwinder" = "yes" -a "$enable_dump_time_unwind" = "yes"]
47a88a
+[then]
47a88a
+    AC_DEFINE([ENABLE_DUMP_TIME_UNWIND], [1], [Create core stacktrace while the process is still in memory.])
47a88a
+[fi]
47a88a
+
47a88a
 AC_SUBST(CONF_DIR)
47a88a
 AC_SUBST(DEFAULT_CONF_DIR)
47a88a
 AC_SUBST(VAR_RUN)
47a88a
diff --git a/doc/abrt-CCpp.conf.txt b/doc/abrt-CCpp.conf.txt
47a88a
index ad3830b..498d53d 100644
47a88a
--- a/doc/abrt-CCpp.conf.txt
47a88a
+++ b/doc/abrt-CCpp.conf.txt
47a88a
@@ -19,12 +19,30 @@ SaveBinaryImage = 'yes' / 'no' ...::
47a88a
    Useful, for example, when _deleted binary_ segfaults.
47a88a
    Default is 'no'.
47a88a
 
47a88a
+CreateCoreBacktrace = 'yes' / 'no' ...::
47a88a
+   When this option is set to 'yes', core backtrace is generated
47a88a
+   from the memory image of the crashing process. Only the crash
47a88a
+   thread is present in the backtrace. This feature requires
47a88a
+   kernel 3.18 or newer, otherwise the core backtrace is not
47a88a
+   created.
47a88a
+   Default is 'yes'.
47a88a
+
47a88a
+SaveFullCore = 'yes' / 'no' ...::
47a88a
+   Save full coredump? If set to 'no', coredump won't be saved
47a88a
+   and you won't be able to report the crash to Bugzilla. Only
47a88a
+   useful with 'CreateCoreBacktrace' set to 'yes'. Please
47a88a
+   note that if this option is set to 'no' and MakeCompatCore
47a88a
+   is set to 'yes', the core is still written to the current
47a88a
+   directory.
47a88a
+   Default is 'yes'.
47a88a
+
47a88a
 VerboseLog = NUM::
47a88a
    Used to make the hook more verbose
47a88a
 
47a88a
 SEE ALSO
47a88a
 --------
47a88a
 abrt.conf(5)
47a88a
+abrt-action-generate-core-backtrace(1)
47a88a
 
47a88a
 AUTHORS
47a88a
 -------
47a88a
diff --git a/src/hooks/CCpp.conf b/src/hooks/CCpp.conf
47a88a
index d199116..b1a0a22 100644
47a88a
--- a/src/hooks/CCpp.conf
47a88a
+++ b/src/hooks/CCpp.conf
47a88a
@@ -8,6 +8,21 @@ MakeCompatCore = yes
47a88a
 # (useful, for example, when _deleted binary_ segfaults)
47a88a
 SaveBinaryImage = no
47a88a
 
47a88a
+# When this option is set to 'yes', core backtrace is generated
47a88a
+# from the memory image of the crashing process. Only the crash
47a88a
+# thread is present in the backtrace. This feature requires
47a88a
+# kernel 3.18 or newer, otherwise the core backtrace is not
47a88a
+# created.
47a88a
+CreateCoreBacktrace = yes
47a88a
+
47a88a
+# Save full coredump? If set to 'no', coredump won't be saved
47a88a
+# and you won't be able to report the crash to Bugzilla. Only
47a88a
+# useful with CreateCoreBacktrace set to 'yes'. Please
47a88a
+# note that if this option is set to 'no' and MakeCompatCore
47a88a
+# is set to 'yes', the core is still written to the current
47a88a
+# directory.
47a88a
+SaveFullCore = yes
47a88a
+
47a88a
 # Used for debugging the hook
47a88a
 #VerboseLog = 2
47a88a
 
47a88a
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
47a88a
index 2dd9ac6..b5f00f6 100644
47a88a
--- a/src/hooks/abrt-hook-ccpp.c
47a88a
+++ b/src/hooks/abrt-hook-ccpp.c
47a88a
@@ -22,6 +22,11 @@
47a88a
 #include "libabrt.h"
47a88a
 #include <selinux/selinux.h>
47a88a
 
47a88a
+#ifdef ENABLE_DUMP_TIME_UNWIND
47a88a
+#include <satyr/abrt.h>
47a88a
+#include <satyr/utils.h>
47a88a
+#endif /* ENABLE_DUMP_TIME_UNWIND */
47a88a
+
47a88a
 #define  DUMP_SUID_UNSAFE 1
47a88a
 #define  DUMP_SUID_SAFE 2
47a88a
 
47a88a
@@ -155,13 +160,13 @@ static struct dump_dir *dd;
47a88a
  * %g - gid
47a88a
  * %t - UNIX time of dump
47a88a
  * %e - executable filename
47a88a
- * %h - hostname
47a88a
+ * %i - crash thread tid
47a88a
  * %% - output one "%"
47a88a
  */
47a88a
 /* Hook must be installed with exactly the same sequence of %c specifiers.
47a88a
  * Last one, %h, may be omitted (we can find it out).
47a88a
  */
47a88a
-static const char percent_specifiers[] = "%scpugteh";
47a88a
+static const char percent_specifiers[] = "%scpugtei";
47a88a
 static char *core_basename = (char*) "core";
47a88a
 
47a88a
 static char* get_executable(pid_t pid, int *fd_p)
47a88a
@@ -580,6 +585,24 @@ static int create_or_die(const char *filename, int user_core_fd)
47a88a
     perror_msg_and_die("Can't open '%s'", filename);
47a88a
 }
47a88a
 
47a88a
+static void create_core_backtrace(pid_t tid, const char *executable, int signal_no, const char *dd_path)
47a88a
+{
47a88a
+#ifdef ENABLE_DUMP_TIME_UNWIND
47a88a
+    if (g_verbose > 1)
47a88a
+        sr_debug_parser = true;
47a88a
+
47a88a
+    char *error_message = NULL;
47a88a
+    bool success = sr_abrt_create_core_stacktrace_from_core_hook(dd_path, tid, executable,
47a88a
+                                                                 signal_no, &error_message);
47a88a
+
47a88a
+    if (!success)
47a88a
+    {
47a88a
+        log("Failed to create core_backtrace: %s", error_message);
47a88a
+        free(error_message);
47a88a
+    }
47a88a
+#endif /* ENABLE_DUMP_TIME_UNWIND */
47a88a
+}
47a88a
+
47a88a
 static int create_user_core(int user_core_fd, pid_t pid, off_t ulimit_c)
47a88a
 {
47a88a
     int err = 1;
47a88a
@@ -619,9 +642,9 @@ int main(int argc, char** argv)
47a88a
 
47a88a
     if (argc < 8)
47a88a
     {
47a88a
-        /* percent specifier:         %s   %c              %p  %u  %g  %t   %e          %h */
47a88a
+        /* percent specifier:         %s   %c              %p  %u  %g  %t   %e          %i */
47a88a
         /* argv:                  [0] [1]  [2]             [3] [4] [5] [6]  [7]         [8]*/
47a88a
-        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME BINARY_NAME [HOSTNAME]", argv[0]);
47a88a
+        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME BINARY_NAME [TID]", argv[0]);
47a88a
     }
47a88a
 
47a88a
     /* Not needed on 2.6.30.
47a88a
@@ -646,6 +669,8 @@ int main(int argc, char** argv)
47a88a
     /* ... and plugins/CCpp.conf */
47a88a
     bool setting_MakeCompatCore;
47a88a
     bool setting_SaveBinaryImage;
47a88a
+    bool setting_SaveFullCore;
47a88a
+    bool setting_CreateCoreBacktrace;
47a88a
     {
47a88a
         map_string_t *settings = new_map_string();
47a88a
         load_abrt_plugin_conf_file("CCpp.conf", settings);
47a88a
@@ -654,6 +679,10 @@ int main(int argc, char** argv)
47a88a
         setting_MakeCompatCore = value && string_to_bool(value);
47a88a
         value = get_map_string_item_or_NULL(settings, "SaveBinaryImage");
47a88a
         setting_SaveBinaryImage = value && string_to_bool(value);
47a88a
+        value = get_map_string_item_or_NULL(settings, "SaveFullCore");
47a88a
+        setting_SaveFullCore = value ? string_to_bool(value) : true;
47a88a
+        value = get_map_string_item_or_NULL(settings, "CreateCoreBacktrace");
47a88a
+        setting_CreateCoreBacktrace = value ? string_to_bool(value) : true;
47a88a
         value = get_map_string_item_or_NULL(settings, "VerboseLog");
47a88a
         if (value)
47a88a
             g_verbose = xatoi_positive(value);
47a88a
@@ -686,11 +715,10 @@ int main(int argc, char** argv)
47a88a
             free(s);
47a88a
     }
47a88a
 
47a88a
-    struct utsname uts;
47a88a
-    if (!argv[8]) /* no HOSTNAME? */
47a88a
+    pid_t tid = 0;
47a88a
+    if (argv[8])
47a88a
     {
47a88a
-        uname(&uts;;
47a88a
-        argv[8] = uts.nodename;
47a88a
+        tid = xatoi_positive(argv[8]);
47a88a
     }
47a88a
 
47a88a
     char path[PATH_MAX];
47a88a
@@ -906,36 +934,42 @@ int main(int argc, char** argv)
47a88a
             off_t sz = copyfd_eof(src_fd_binary, dst_fd, COPYFD_SPARSE);
47a88a
             if (fsync(dst_fd) != 0 || close(dst_fd) != 0 || sz < 0)
47a88a
             {
47a88a
-                dd_delete(dd);
47a88a
-                error_msg_and_die("Error saving '%s'", path);
47a88a
+                dd_delete(dd); error_msg_and_die("Error saving '%s'", path);
47a88a
             }
47a88a
             close(src_fd_binary);
47a88a
         }
47a88a
 
47a88a
-        strcpy(path + path_len, "/"FILENAME_COREDUMP);
47a88a
-        int abrt_core_fd = create_or_die(path, user_core_fd);
47a88a
-
47a88a
-        /* We write both coredumps at once.
47a88a
-         * We can't write user coredump first, since it might be truncated
47a88a
-         * and thus can't be copied and used as abrt coredump;
47a88a
-         * and if we write abrt coredump first and then copy it as user one,
47a88a
-         * then we have a race when process exits but coredump does not exist yet:
47a88a
-         * $ echo -e '#include<signal.h>\nmain(){raise(SIGSEGV);}' | gcc -o test -x c -
47a88a
-         * $ rm -f core*; ulimit -c unlimited; ./test; ls -l core*
47a88a
-         * 21631 Segmentation fault (core dumped) ./test
47a88a
-         * ls: cannot access core*: No such file or directory <=== BAD
47a88a
-         */
47a88a
-        off_t core_size = copyfd_sparse(STDIN_FILENO, abrt_core_fd, user_core_fd, ulimit_c);
47a88a
-
47a88a
-        close_user_core(user_core_fd, core_size);
47a88a
-
47a88a
-        if (fsync(abrt_core_fd) != 0 || close(abrt_core_fd) != 0 || core_size < 0)
47a88a
+        off_t core_size = 0;
47a88a
+        if (setting_SaveFullCore)
47a88a
         {
47a88a
-            unlink(path);
47a88a
-            dd_delete(dd);
47a88a
-            /* copyfd_sparse logs the error including errno string,
47a88a
-             * but it does not log file name */
47a88a
-            error_msg_and_die("Error writing '%s'", path);
47a88a
+            strcpy(path + path_len, "/"FILENAME_COREDUMP);
47a88a
+            int abrt_core_fd = create_or_die(path, user_core_fd);
47a88a
+
47a88a
+            /* We write both coredumps at once.
47a88a
+             * We can't write user coredump first, since it might be truncated
47a88a
+             * and thus can't be copied and used as abrt coredump;
47a88a
+             * and if we write abrt coredump first and then copy it as user one,
47a88a
+             * then we have a race when process exits but coredump does not exist yet:
47a88a
+             * $ echo -e '#include<signal.h>\nmain(){raise(SIGSEGV);}' | gcc -o test -x c -
47a88a
+             * $ rm -f core*; ulimit -c unlimited; ./test; ls -l core*
47a88a
+             * 21631 Segmentation fault (core dumped) ./test
47a88a
+             * ls: cannot access core*: No such file or directory <=== BAD
47a88a
+             */
47a88a
+            core_size = copyfd_sparse(STDIN_FILENO, abrt_core_fd, user_core_fd, ulimit_c);
47a88a
+            close_user_core(user_core_fd, core_size);
47a88a
+            if (fsync(abrt_core_fd) != 0 || close(abrt_core_fd) != 0 || core_size < 0)
47a88a
+            {
47a88a
+                unlink(path);
47a88a
+                dd_delete(dd);
47a88a
+                /* copyfd_sparse logs the error including errno string,
47a88a
+                 * but it does not log file name */
47a88a
+                error_msg_and_die("Error writing '%s'", path);
47a88a
+            }
47a88a
+        }
47a88a
+        else
47a88a
+        {
47a88a
+            /* User core is created even if WriteFullCore is off. */
47a88a
+            create_user_core(user_core_fd, pid, ulimit_c);
47a88a
         }
47a88a
 
47a88a
 /* Because of #1211835 and #1126850 */
47a88a
@@ -977,6 +1011,10 @@ int main(int argc, char** argv)
47a88a
         /* And finally set the right uid and gid */
47a88a
         dd_reset_ownership(dd);
47a88a
 
47a88a
+        /* Perform crash-time unwind of the guilty thread. */
47a88a
+        if (tid > 0 && setting_CreateCoreBacktrace)
47a88a
+            create_core_backtrace(tid, executable, signal_no, dd->dd_dirname);
47a88a
+
47a88a
         /* We close dumpdir before we start catering for crash storm case.
47a88a
          * Otherwise, delete_dump_dir's from other concurrent
47a88a
          * CCpp's won't be able to delete our dump (their delete_dump_dir
47a88a
@@ -990,7 +1028,9 @@ int main(int argc, char** argv)
47a88a
             strcpy(path, newpath);
47a88a
         free(newpath);
47a88a
 
47a88a
-        log("Saved core dump of pid %lu (%s) to %s (%llu bytes)", (long)pid, executable, path, (long long)core_size);
47a88a
+        if (core_size > 0)
47a88a
+            log_notice("Saved core dump of pid %lu (%s) to %s (%llu bytes)",
47a88a
+                       (long)pid, executable, path, (long long)core_size);
47a88a
 
47a88a
         notify_new_path(path);
47a88a
 
47a88a
diff --git a/src/hooks/abrt-install-ccpp-hook.in b/src/hooks/abrt-install-ccpp-hook.in
47a88a
index aa01231..d4ed4a5 100755
47a88a
--- a/src/hooks/abrt-install-ccpp-hook.in
47a88a
+++ b/src/hooks/abrt-install-ccpp-hook.in
47a88a
@@ -11,9 +11,9 @@ SAVED_PATTERN_DIR="@VAR_RUN@/abrt"
47a88a
 SAVED_PATTERN_FILE="@VAR_RUN@/abrt/saved_core_pattern"
47a88a
 HOOK_BIN="@libexecdir@/abrt-hook-ccpp"
47a88a
 # Must match percent_specifiers[] order in abrt-hook-ccpp.c:
47a88a
-PATTERN="|$HOOK_BIN %s %c %p %u %g %t %e"
47a88a
+PATTERN="|$HOOK_BIN %s %c %p %u %g %t %e %i"
47a88a
 # Same, but with bogus "executable name" parameter
47a88a
-PATTERN1="|$HOOK_BIN %s %c %p %u %g %t e"
47a88a
+PATTERN1="|$HOOK_BIN %s %c %p %u %g %t e %i"
47a88a
 
47a88a
 # core_pipe_limit specifies how many dump_helpers can run at the same time
47a88a
 # 0 - means unlimited, but it's not guaranteed that /proc/<pid> of crashing
47a88a
-- 
47a88a
2.4.3
47a88a