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

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