changeset 4162427127e9 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=4162427127e9
description:
sim: Add support for generating back traces on errors
Add functionality to generate a back trace if gem5 crashes (SIGABRT or
SIGSEGV). The current implementation uses glibc's stack traversal
support if available and stubs out the call to print_backtrace()
otherwise.
diffstat:
SConstruct | 17 +++++++
src/base/atomicio.hh | 24 ++++++++++
src/sim/SConscript | 1 +
src/sim/backtrace.hh | 50 ++++++++++++++++++++++
src/sim/backtrace_glibc.cc | 66 +++++++++++++++++++++++++++++
src/sim/backtrace_none.cc | 45 +++++++++++++++++++
src/sim/init_signals.cc | 102 ++++++++++++++++++++++++++++++++++++--------
7 files changed, 286 insertions(+), 19 deletions(-)
diffs (truncated from 399 to 300 lines):
diff -r c273990ed9bf -r 4162427127e9 SConstruct
--- a/SConstruct Thu Dec 03 23:53:37 2015 +0000
+++ b/SConstruct Fri Dec 04 00:12:58 2015 +0000
@@ -983,6 +983,21 @@
"installing tcmalloc (libgoogle-perftools-dev package "\
"on Ubuntu or RedHat)." + termcap.Normal
+
+# Detect back trace implementations. The last implementation in the
+# list will be used by default.
+backtrace_impls = [ "none" ]
+
+if conf.CheckLibWithHeader(None, 'execinfo.h', 'C',
+ 'backtrace_symbols_fd((void*)0, 0, 0);'):
+ backtrace_impls.append("glibc")
+
+if backtrace_impls[-1] == "none":
+ default_backtrace_impl = "none"
+ print termcap.Yellow + termcap.Bold + \
+ "No suitable back trace implementation found." + \
+ termcap.Normal
+
if not have_posix_clock:
print "Can't find library for POSIX clocks."
@@ -1131,6 +1146,8 @@
BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
have_kvm),
EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
all_protocols),
+ EnumVariable('BACKTRACE_IMPL', 'Post-mortem dump implementation',
+ backtrace_impls[-1], backtrace_impls)
)
# These variables get exported to #defines in config/*.hh (see src/SConscript).
diff -r c273990ed9bf -r 4162427127e9 src/base/atomicio.hh
--- a/src/base/atomicio.hh Thu Dec 03 23:53:37 2015 +0000
+++ b/src/base/atomicio.hh Fri Dec 04 00:12:58 2015 +0000
@@ -41,4 +41,28 @@
ssize_t atomic_read(int fd, void *s, size_t n);
ssize_t atomic_write(int fd, const void *s, size_t n);
+/**
+ * Statically allocate a string and write it to a file descriptor.
+ *
+ * @warning The return value will from atomic_write will be ignored
+ * which means that errors will be ignored. This is normally fine as
+ * this macro is intended to be used in fatal signal handlers where
+ * error handling might not be feasible.
+ */
+#define STATIC_MSG(fd, m) \
+ do { \
+ static const char msg[] = m; \
+ atomic_write(fd, msg, sizeof(msg) - 1); \
+ } while(0)
+
+/**
+ * Statically allocate a string and write it to STDERR.
+ *
+ * @warning The return value will from atomic_write will be ignored
+ * which means that errors will be ignored. This is normally fine as
+ * this macro is intended to be used in fatal signal handlers where
+ * error handling might not be feasible.
+ */
+#define STATIC_ERR(m) STATIC_MSG(STDERR_FILENO, m)
+
#endif // __BASE_ATOMICIO_HH__
diff -r c273990ed9bf -r 4162427127e9 src/sim/SConscript
--- a/src/sim/SConscript Thu Dec 03 23:53:37 2015 +0000
+++ b/src/sim/SConscript Fri Dec 04 00:12:58 2015 +0000
@@ -41,6 +41,7 @@
Source('arguments.cc')
Source('async.cc')
+Source('backtrace_%s.cc' % env['BACKTRACE_IMPL'])
Source('core.cc')
Source('tags.cc')
Source('cxx_config.cc')
diff -r c273990ed9bf -r 4162427127e9 src/sim/backtrace.hh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sim/backtrace.hh Fri Dec 04 00:12:58 2015 +0000
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andreas Sandberg
+ */
+#ifndef __SIM_BACKTRACE_HH__
+#define __SIM_BACKTRACE_HH__
+
+/**
+ * Print a gem5 post-mortem report
+ *
+ * @note This is usually called from a signal handler. Implementations
+ * must support this use case.
+ */
+void print_backtrace();
+
+#endif // __SIM_BACKTRACE_HH__
diff -r c273990ed9bf -r 4162427127e9 src/sim/backtrace_glibc.cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sim/backtrace_glibc.cc Fri Dec 04 00:12:58 2015 +0000
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2015 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andreas Sandberg
+ */
+
+#include "sim/backtrace.hh"
+
+#include <execinfo.h>
+#include <unistd.h>
+
+#include "base/atomicio.hh"
+
+#define SAFE_MSG(m) \
+ do { \
+ static const char msg[] = m; \
+ atomic_write(STDERR_FILENO, msg, sizeof(msg) - 1); \
+ } while(0)
+
+void
+print_backtrace()
+{
+ void *buffer[32];
+ int size;
+
+ size = backtrace(buffer, sizeof(buffer) / sizeof(*buffer));
+
+ STATIC_ERR("--- BEGIN LIBC BACKTRACE ---\n");
+ backtrace_symbols_fd(buffer, size, STDERR_FILENO);
+ if (size == sizeof(buffer))
+ STATIC_ERR("Warning: Backtrace may have been truncated.\n");
+ STATIC_ERR("--- END LIBC BACKTRACE ---\n");
+}
diff -r c273990ed9bf -r 4162427127e9 src/sim/backtrace_none.cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sim/backtrace_none.cc Fri Dec 04 00:12:58 2015 +0000
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2015 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder. You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Andreas Sandberg
+ */
+
+#include "sim/backtrace.hh"
+
+void
+print_backtrace()
+{
+}
diff -r c273990ed9bf -r 4162427127e9 src/sim/init_signals.cc
--- a/src/sim/init_signals.cc Thu Dec 03 23:53:37 2015 +0000
+++ b/src/sim/init_signals.cc Fri Dec 04 00:12:58 2015 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012 ARM Limited
+ * Copyright (c) 2012, 2015 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
@@ -41,18 +41,67 @@
* Authors: Nathan Binkert
*/
+#include "sim/init_signals.hh"
+
+#include <sys/types.h>
+#include <unistd.h>
+
#include <csignal>
#include <iostream>
#include <string>
+#include "base/atomicio.hh"
#include "base/cprintf.hh"
#include "sim/async.hh"
+#include "sim/backtrace.hh"
#include "sim/core.hh"
#include "sim/eventq.hh"
-#include "sim/init_signals.hh"
using namespace std;
+// Use an separate stack for fatal signal handlers
+static uint8_t fatalSigStack[2 * SIGSTKSZ];
+
+static bool
+setupAltStack()
+{
+ stack_t stack;
+ stack.ss_sp = fatalSigStack;
+ stack.ss_size = sizeof(fatalSigStack);
+ stack.ss_flags = 0;
+
+ return sigaltstack(&stack, NULL) == 0;
+}
+
+static void
+installSignalHandler(int signal, void (*handler)(int sigtype),
+ int flags = SA_RESTART)
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev