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