Sanitize the way we use abort() and exit(). With this commit, we should never crash in release mode, even when assertions are enabled. In debug mode, we crash with abort in order to get a useful backtrace.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22165 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-12-16 11:34:29 +00:00
parent c66e214724
commit efbd1d30cb
12 changed files with 3104 additions and 1035 deletions

View File

@ -336,7 +336,6 @@ src_support_files = Split('''
SignalSlot.cpp
Systemcall.cpp
Timeout.cpp
abort.cpp
convert.cpp
debug.cpp
docstream.cpp

File diff suppressed because it is too large Load Diff

View File

@ -103,6 +103,7 @@
#include <algorithm>
#include <iomanip>
#include <stack>
#include <stdlib.h>
#include <sstream>
#include <fstream>
@ -1001,8 +1002,7 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
}
catch (...) {
lyxerr << "Caught some really weird exception..." << endl;
LyX::cref().emergencyCleanup();
lyx::support::abort();
LyX::cref().exit(1);
}
ofs.close();

View File

@ -184,6 +184,27 @@ LyX::~LyX()
delete pimpl_;
}
#include <stdlib.h>
void LyX::exit(int exit_code) const
{
if (exit_code)
// Something wrong happened so better save everything, just in
// case.
emergencyCleanup();
#ifndef NDEBUG
// Properly crash in debug mode in order to get a useful backtrace.
abort();
#endif
// In release mode, try to exit gracefully.
if (theApp())
theApp()->exit(exit_code);
else
exit(exit_code);
}
LyX & LyX::ref()
{
@ -729,7 +750,7 @@ static void error_handler(int err_sig)
#else
if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty())
#endif
support::abort();
abort();
exit(0);
}

View File

@ -54,6 +54,11 @@ public:
/// Execute LyX.
int exec(int & argc, char * argv[]);
/// Try to exit LyX properly.
/// \p exit_code is 0 by default, if a non zero value is passed,
/// emergencyCleanup() will be called before exiting.
void exit(int exit_code = 0) const;
static LyX & ref();
static LyX const & cref();

View File

@ -19,6 +19,7 @@
#include <exception>
#include <iomanip>
#include <stdlib.h>
using namespace std;
using lyx::lyxerr;
@ -35,26 +36,17 @@ void throw_exception(exception const & e)
#endif
void emergencyCleanup()
{
static bool didCleanup;
if (didCleanup)
return;
didCleanup = true;
LyX::cref().emergencyCleanup();
}
void assertion_failed(char const * expr, char const * function,
char const * file, long line)
{
lyxerr << "Assertion triggered in " << function
<< " by failing check \"" << expr << "\""
<< " in file " << file << ":" << line << endl;
emergencyCleanup();
lyx::support::abort();
// FIXME: by default we exit here but we could also inform the user
// about the assertion and do the emergency cleanup without exiting.
// FIXME: do we have a list of exit codes defined somewhere?
LyX::cref().exit(1);
}
} // namespace boost

View File

@ -65,6 +65,7 @@
#include <boost/assert.hpp>
#include <stdlib.h>
#include <sstream>
using namespace std;
@ -361,9 +362,9 @@ Inset * createInset(Buffer & buf, FuncRequest const & cmd)
} catch (ExceptionMessage const & message) {
if (message.type_ == ErrorException) {
// This should never happen!
Alert::error(message.title_, message.details_);
LyX::cref().emergencyCleanup();
abort();
LyX::cref().exit(1);
} else if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_);
return 0;

View File

@ -436,8 +436,7 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
catch (ExceptionMessage const & e) {
if (e.type_ == ErrorException) {
Alert::error(e.title_, e.details_);
LyX::cref().emergencyCleanup();
QApplication::exit(1);
LyX::cref().exit(1);
} else if (e.type_ == WarningException) {
Alert::warning(e.title_, e.details_);
return false;
@ -449,15 +448,13 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
"\n\nException: ");
s += from_ascii(e.what());
Alert::error(_("Software exception Detected"), s);
LyX::cref().emergencyCleanup();
QApplication::exit(1);
LyX::cref().exit(1);
}
catch (...) {
docstring s = _("LyX has caught some really weird exception, it will "
"now attempt to save all unsaved documents and exit.");
Alert::error(_("Software exception Detected"), s);
LyX::cref().emergencyCleanup();
QApplication::exit(1);
LyX::cref().exit(1);
}
return false;

View File

@ -110,6 +110,7 @@ namespace lyx {
InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
: InsetCollapsable(bp), name_(from_utf8(type))
{
BOOST_ASSERT(false);
setLabel(_("float: ") + floatName(type, bp));
params_.type = type;
}

View File

@ -41,7 +41,6 @@ liblyxsupport_la_SOURCES = \
FileMonitor.h \
FileMonitor.cpp \
RandomAccessList.h \
abort.cpp \
convert.cpp \
convert.h \
copied_ptr.h \

View File

@ -1,29 +0,0 @@
/**
* \file abort.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "support/lyxlib.h"
namespace lyx {
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
void support::abort()
{
::abort();
}
} // namespace lyx

View File

@ -15,17 +15,11 @@
#ifndef LYX_LIB_H
#define LYX_LIB_H
#include <string>
namespace lyx {
namespace support {
/// FIXME: some point to this hmm ?
int kill(int pid, int sig);
/// FIXME: same here
void abort();
/**
* Returns true if var is approximately equal to number with allowed error