]> git.lyx.org Git - lyx.git/blob - src/support/lassert.cpp
Work around qt bug that prevents the glyph LATIN CAPITAL LETTER SHARP S from being...
[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/debug.h"
15
16 #include <boost/assert.hpp>
17
18
19 //#define LYX_CALLSTACK_PRINTING
20 // must be linked with -rdynamic
21 #ifdef LYX_CALLSTACK_PRINTING
22 #include <cstdio>
23 #include <cstdlib>
24 #include <execinfo.h>
25 #include <cxxabi.h>
26 #endif
27
28
29 namespace lyx {
30
31 void doAssert(char const * expr,  char const * file, long line)
32 {
33     // TODO Should we try to print the call stack before exiting?
34
35         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
36         // comment this out if not needed
37         BOOST_ASSERT(false);
38 }
39
40
41 //TODO Return as string, so call stack could be used in dialogs.
42 void printCallStack()
43 {
44 #ifdef LYX_CALLSTACK_PRINTING
45         const int depth = 50;
46         
47         // get void*'s for all entries on the stack
48         void* array[depth];
49         size_t size = backtrace(array, depth);
50         
51         char** messages = backtrace_symbols(array, size);
52         
53         for (size_t i = 0; i < size && messages != NULL; i++) {
54                 std::string orig(messages[i]);
55                 // extract mangled: bin/lyx2.0(_ZN3lyx7support7packageEv+0x32) [0x8a2e02b]
56                 char* mangled = 0;
57                 for (char *p = messages[i]; *p; ++p) {
58                         if (*p == '(') {
59                                 *p = 0;
60                                 mangled = p + 1;
61                         } else if (*p == '+') {
62                                 *p = 0;
63                                 break;
64                         }
65                 }
66                 int err = 0;
67                 char* demangled = abi::__cxa_demangle(mangled, 0, 0, &err);
68                 if (err == 0) {
69                         fprintf(stderr, "[bt]: (%d) %s %s\n", i, messages[i], demangled);
70                         free((void*)demangled);
71                 } else {
72                         fprintf(stderr, "[bt]: (%d) %s\n", i, orig.c_str());
73                 }               
74         }
75 #endif
76 }
77
78 } // namespace lyx