forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermination.cc
167 lines (125 loc) · 2.85 KB
/
termination.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "termination.hh"
#include <exception>
#include <typeinfo>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include <string>
#ifndef __WIN32
#include <execinfo.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QtCore>
using std::string;
static void termHandler()
{
if( logFile.isOpen() )
logFile.close();
std::string message( "GoldenDict has crashed with an unexpected exception\n\n" );
int status;
char * function = 0;
size_t functionLength = 0;
#ifdef _MSC_VER
std::type_info * ti = 0;
#else
std::type_info * ti = __cxxabiv1::__cxa_current_exception_type();
#endif
if ( ti )
{
char const * name = ti->name();
#ifdef _MSC_VER
char * ret = 0;
// avoid 'unused' warnings
(void) status;
(void) functionLength;
#else
char * ret = abi::__cxa_demangle( name, function, &functionLength, &status );
#endif
if ( ret )
{
function = ret;
name = function;
}
message += "Exception: ";
message += name;
message += '\n';
try
{
throw;
}
catch( std::exception & e )
{
message += "Message: ";
message += e.what();
message += '\n';
}
catch( ... )
{
}
}
else
{
message += "terminate() called without active exception\n";
}
#ifndef __WIN32
message += "\nBacktrace:\n";
const size_t maxDepth = 200;
size_t stackDepth;
void * stackAddrs[ maxDepth ];
char ** stackStrings;
stackDepth = backtrace( stackAddrs, maxDepth );
stackStrings = backtrace_symbols( stackAddrs, stackDepth );
for (size_t i = 1; i < stackDepth; i++)
{
char * begin = 0, * end = 0;
for (char *j = stackStrings[i]; *j; ++j)
{
if (*j == '(')
begin = j + 1;
else if ( begin && ( *j == '+' || *j == ')' ) )
{
end = j;
break;
}
}
string line;
if ( end )
{
char endSymbol = *end;
*end = 0;
char * ret = abi::__cxa_demangle( begin, function, &functionLength, &status );
*end = endSymbol;
if ( ret )
{
function = ret;
line = string( stackStrings[ i ], begin );
line += function;
line += string( end );
}
}
message += " ";
message += line.size() ? line.c_str() : stackStrings[ i ];
message += '\n';
}
#endif
QTemporaryFile file;
if ( file.open() )
{
file.setAutoRemove( false );
file.write( message.data(), message.size() );
QStringList args;
args << "--show-error-file";
args << file.fileName();
file.close();
QProcess::execute( QCoreApplication::applicationFilePath(), args );
abort();
}
}
void installTerminationHandler()
{
std::set_terminate( termHandler );
}