AutoBacktrace Class Reference

AutoBacktrace provides a handler that intercepts fatal errors, prints a backtrace, and passes on the fatal error to other handlers. More...

#include <Backtrace.h>

List of all members.

Static Public Member Functions

static void install (bool includeSegFault=true)
 Installs backtrace on error; default output is to stderr.
static void setOutputStream (std::ostream &outStream)
 Sets an ostream to which the backtrace is written.
static void setOutputStream ()
 Unsets a target stream for backtrace.
static void setTraceTarget (SharedTraceTarget pTraceTarget=SharedTraceTarget())
 Sets a TraceTarget to which the backtrace is written, independent of setOutputStream.

Private Member Functions

 AutoBacktrace ()

Static Private Member Functions

static void signal_handler (int signum)
static void installSignal (int signum)

Static Private Attributes

static std::ostream * pstream
static SharedTraceTarget ptrace
static struct sigaction nextAction [BACKTRACE_SIG_MAX]


Detailed Description

AutoBacktrace provides a handler that intercepts fatal errors, prints a backtrace, and passes on the fatal error to other handlers.

The backtrace handler has global scope. Fatal errors include abort(), assert(), fennel permAssert(), and runaway C++ exceptions.

Definition at line 121 of file Backtrace.h.


Constructor & Destructor Documentation

AutoBacktrace::AutoBacktrace (  )  [inline, private]

Definition at line 132 of file Backtrace.h.

00133     {
00134     }


Member Function Documentation

void AutoBacktrace::signal_handler ( int  signum  )  [static, private]

Definition at line 198 of file Backtrace.cpp.

References TRACE_SEVERE.

Referenced by installSignal().

00199 {
00200 #ifndef __MSVC__
00201     Backtrace bt;
00202     if (ptrace) {
00203         std::ostringstream oss;
00204         oss <<
00205             "*** CAUGHT SIGNAL " << signum << "; BACKTRACE:" << std::endl;
00206         oss << bt;
00207         std::string msg = oss.str();
00208         if (pstream) {
00209             *pstream << msg;
00210         }
00211         ptrace->notifyTrace("backtrace", TRACE_SEVERE, msg);
00212     } else if (pstream) {
00213         *pstream <<
00214             "*** CAUGHT SIGNAL " << signum << "; BACKTRACE:" << std::endl;
00215         *pstream << bt;
00216     }
00217 
00218     // invoke next handler: never coming back, so reset the signal handler
00219     sigaction(signum, &(nextAction[signum]), NULL);
00220     raise(signum);
00221 #endif
00222 }

void AutoBacktrace::installSignal ( int  signum  )  [static, private]

Definition at line 256 of file Backtrace.cpp.

References nextAction, and signal_handler().

Referenced by install().

00257 {
00258 #ifndef __MSVC__
00259     permAssert(signum < BACKTRACE_SIG_MAX);
00260     struct sigaction act;
00261     struct sigaction old_act;
00262     act.sa_handler = signal_handler;
00263     sigemptyset (&act.sa_mask);
00264     act.sa_flags = 0;
00265     int rc = sigaction(signum, &act, &old_act);
00266     if (rc) {
00267         return;                         // failed
00268     }
00269     if (old_act.sa_handler != signal_handler) {
00270         // installed for the first time
00271         nextAction[signum] = old_act;
00272     }
00273 #endif
00274 }

void AutoBacktrace::install ( bool  includeSegFault = true  )  [static]

Installs backtrace on error; default output is to stderr.

NOTE jvs 25-Dec-2005: for more flexibility, we could allow the caller to specify a sigset_t.

Parameters:
includeSegFault if true, SIGSEGV is included in the set of signals causing a backtrace; if false, it is omitted (required in environments such as a Java VM where spurious segfaults may be signalled but handled as part of normal operation)

Definition at line 239 of file Backtrace.cpp.

References installSignal().

Referenced by TestBase::beforeTestCase(), and JniUtil::init().

00240 {
00241     // Traps SIGABRT: this handles assert(); unless NDEBUG, permAssert() =>
00242     // assert(), so that's covered. std::terminate() also => abort().  TODO:
00243     // trap permAssert() directly.
00244 #ifndef __MSVC__
00245     installSignal(SIGILL);
00246     installSignal(SIGABRT);
00247 
00248     if (includeSegFault) {
00249         installSignal(SIGSEGV);
00250     }
00251 
00252     installSignal(SIGBUS);
00253 #endif
00254 }

static void AutoBacktrace::setOutputStream ( std::ostream &  outStream  )  [static]

Sets an ostream to which the backtrace is written.

Parameters:
outStream receives backtrace if signal is handled

void AutoBacktrace::setOutputStream (  )  [static]

Unsets a target stream for backtrace.

Definition at line 224 of file Backtrace.cpp.

References pstream.

Referenced by TestBase::beforeTestCase().

00225 {
00226     pstream = 0;
00227 }

void AutoBacktrace::setTraceTarget ( SharedTraceTarget  pTraceTarget = SharedTraceTarget()  )  [static]

Sets a TraceTarget to which the backtrace is written, independent of setOutputStream.

Parameters:
pTraceTarget receives backtrace if signal is handled; if singular (the default), resets target to nothing

Definition at line 234 of file Backtrace.cpp.

References ptrace.

Referenced by TestBase::afterTestCase(), TestBase::beforeTestCase(), and CmdInterpreter::visit().

00235 {
00236     ptrace = p;
00237 }


Member Data Documentation

std::ostream * AutoBacktrace::pstream [static, private]

Definition at line 123 of file Backtrace.h.

Referenced by setOutputStream().

SharedTraceTarget AutoBacktrace::ptrace [static, private]

Definition at line 124 of file Backtrace.h.

Referenced by setTraceTarget().

struct sigaction AutoBacktrace::nextAction[BACKTRACE_SIG_MAX] [static, private]

Definition at line 128 of file Backtrace.h.

Referenced by installSignal().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:24 2009 for Fennel by  doxygen 1.5.1