From: <sv...@ww...> - 2004-06-21 07:17:00
|
Author: mkrose Date: 2004-06-21 00:16:54 -0700 (Mon, 21 Jun 2004) New Revision: 1060 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Trace.h Log: Minor fixups to Trace.h (needs more work, and really needs a win32 implementation). Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1060 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-06-21 07:11:10 UTC (rev 1059) +++ trunk/CSP/SimData/CHANGES.current 2004-06-21 07:16:54 UTC (rev 1060) @@ -25,6 +25,9 @@ * Updated docs in TaggedRecord.h. Added a global registry for tagged records. + * Minor fixups to Trace.h (needs more work, and really needs a + win32 implementation). + 2004-06-16: onsight * Doxygen cleanups. Modified: trunk/CSP/SimData/Include/SimData/Trace.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Trace.h 2004-06-21 07:11:10 UTC (rev 1059) +++ trunk/CSP/SimData/Include/SimData/Trace.h 2004-06-21 07:16:54 UTC (rev 1060) @@ -1,18 +1,18 @@ /* SimData: Data Infrastructure for Simulations - * Copyright (C) 2003 Mark Rose <tm...@st...> - * + * Copyright 2003, 2004 Mark Rose <mk...@us...> + * * This file is part of SimData. - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -22,6 +22,11 @@ /** * @file Trace.h * @brief Automatic callstack traces reporting on error. + * + * TODO does not show the offending address (need siginfo_t). + * TODO difficult to decode addresses for libraries with lazy loading. + * TODO what's the point of TraceBase? + * TODO need a windows implementation */ @@ -35,27 +40,41 @@ #ifdef __GNUC__ # include <csignal> # include <execinfo.h> -# include <exception> +# include <exception> # include <cstdlib> +# include <dlfcn.h> #endif // __GNUC__ NAMESPACE_SIMDATA + +/** Base class for implementing signal handlers and stack trace dumps. + */ class TraceBase { +private: typedef void (*Callback)(); + Callback _precallback, _postcallback; logstream *_log; bool _traced; - Callback _precallback, _postcallback; + void *_reference; + protected: - TraceBase(): _precallback(0), _postcallback(0), _log(0), _traced(false) { } + TraceBase(): _precallback(0), _postcallback(0), _log(0), _traced(false), _reference(0) { } + void setCallbacks_impl(Callback precallback, Callback postcallback) { _precallback = precallback; _postcallback = postcallback; } + + void setReference(void *ref) { + _reference = ref; + } + void setLog_impl(logstream &log) { _log = &log; } + std::ostream &log() { if (_log) { return (*_log) << loglevel(LOG_ALL, LOG_ERROR); @@ -63,29 +82,34 @@ return simdata::log() << loglevel(LOG_ALL, LOG_ERROR); } } + void _preCallback() { if (_precallback != 0) _precallback(); } + void _postCallback() { if (_postcallback != 0) _postcallback(); } + void error(int skip, bool segv=false) { if (_traced) return; _traced = true; if (segv) { log() << "FATAL ERROR: segmentation fault." << std::endl; + if (_reference) { + log() << "reference = " << _reference << std::endl; + } } _preCallback(); _backtrace(skip, segv); _postCallback(); } + virtual void _backtrace(int skip, bool segv) {} }; - /** Singleton callstack trace error handler. - * */ class Trace: public TraceBase { @@ -128,17 +152,17 @@ } Trace(): TraceBase() {} - ~Trace() {} + virtual ~Trace() {} public: - /** Install a new backtrace error handler. * * @returns true if succesful. */ - static bool install() { + static bool install(void *reference) { #ifdef __GNUC__ + getTrace().setReference(reference); signal(SIGABRT, __sigabort); signal(SIGSEGV, __sigsegv); return true; |