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

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