]> git.lyx.org Git - lyx.git/blob - src/support/lassert.cpp
On Linux show in crash message box the backtrace
[lyx.git] / src / support / lassert.cpp
1 /**
2  * \file lassert.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Peter Kümmel
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/convert.h"
15 #include "support/debug.h"
16 #include "support/docstring.h"
17 #include "support/ExceptionMessage.h"
18 #include "support/gettext.h"
19 #include "support/lstrings.h"
20
21 #include <boost/assert.hpp>
22
23 #include <QString>
24
25 #ifdef LYX_CALLSTACK_PRINTING
26 #include <cstdio>
27 #include <cstdlib>
28 #include <execinfo.h>
29 #include <cxxabi.h>
30 #endif
31
32
33 namespace lyx {
34
35 using namespace std;
36 using namespace support;
37
38 // TODO Should we try to print the call stack in the course of these?
39
40 void doAssert(char const * expr, char const * file, long line)
41 {
42         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
43         // comment this out if not needed
44         BOOST_ASSERT(false);
45 }
46
47
48 docstring formatHelper(docstring const & msg,
49         char const * expr, char const * file, long line)
50 {
51         docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
52         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
53         
54         return bformat(d, from_ascii(expr), from_ascii(file), 
55                 convert<docstring>(line)) + '\n' + msg;
56 }
57
58
59 void doWarnIf(char const * expr, char const * file, long line)
60 {
61         docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
62         // comment this out if not needed
63         BOOST_ASSERT(false);
64         throw ExceptionMessage(WarningException, _("Warning!"), 
65                 formatHelper(d, expr, file, line));
66 }
67
68
69 void doBufErr(char const * expr, char const * file, long line)
70 {
71         docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
72         // comment this out if not needed
73         BOOST_ASSERT(false);
74         throw ExceptionMessage(BufferException, _("Buffer Error!"),
75                 formatHelper(d, expr, file, line));
76 }
77
78
79 void doAppErr(char const * expr, char const * file, long line)
80 {
81         docstring const d = _("LyX has encountered an application error\nand will now shut down.");
82         // comment this out if not needed
83         BOOST_ASSERT(false);
84         throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
85                 formatHelper(d, expr, file, line));
86 }
87
88
89 docstring printCallStack()
90 {
91 #ifndef LYX_CALLSTACK_PRINTING
92         return docstring();
93 #else
94         const int depth = 200;
95         
96         // get void*'s for all entries on the stack
97         void* array[depth];
98         size_t size = backtrace(array, depth);
99         
100         char** messages = backtrace_symbols(array, size);
101         
102         docstring bt;
103         for (size_t i = 1; i < size && messages != NULL; i++) {
104                 const std::string orig(messages[i]);
105                 char* mangled = 0;
106                 for (char *p = messages[i]; *p; ++p) {
107                         if (*p == '(') {
108                                 *p = 0;
109                                 mangled = p + 1;
110                         } else if (*p == '+') {
111                                 *p = 0;
112                                 break;
113                         }
114                 }
115                 int status = 0;
116                 const char* demangled = abi::__cxa_demangle(mangled, 0, 0, &status);
117                 const QByteArray line = QString("(%1) %2: %3\n").arg(i, 3).arg(messages[i])
118                                                                 .arg(demangled ? demangled : orig.c_str()).toLocal8Bit();
119                 free((void*)demangled);
120
121                 fprintf(stderr, "%s", line.constData());
122                 bt += from_local8bit(line.constData());
123         }
124                 return bt;
125 #endif
126 }
127
128 } // namespace lyx