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

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