-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmythrow.cpp
More file actions
130 lines (117 loc) · 3.49 KB
/
Copy pathmythrow.cpp
File metadata and controls
130 lines (117 loc) · 3.49 KB
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
/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* 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 3, 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <execinfo.h>
#include <stdlib.h>
#include <cxxabi.h>
#include <iostream>
#include <sstream>
#include "mythrow.h"
using namespace std;
ExceptionBase::ExceptionBase(const std::string& msg) throw()
: mMsg(msg),
mFile("<unknown file>"),
mFunc("<unknown func>"),
mLine(-1),
mStackTraceSize(0)
{}
ExceptionBase::~ExceptionBase() throw()
{}
void ExceptionBase::Init(const char* file, const char* func, int line)
{
mFile = file;
mFunc = func;
mLine = line;
mStackTraceSize = backtrace(mStackTrace, MAX_STACK_TRACE_SIZE);
}
std::string ExceptionBase::GetClassName() const
{
return "ExceptionBase";
}
const char* ExceptionBase::what() const throw()
{
return ToString().c_str();
}
const std::string& ExceptionBase::ToString() const
{
if (mWhat.empty())
{
stringstream sstr("");
if (mLine > 0)
{
sstr << mFile << "(" << mLine << ")";
}
sstr << ": " << GetClassName();
if (!GetMessage().empty())
{
sstr << ": " << GetMessage();
}
sstr << "\nStack Trace:\n";
sstr << GetStackTrace();
mWhat = sstr.str();
}
return mWhat;
}
std::string ExceptionBase::GetMessage() const
{
return mMsg;
}
std::string ExceptionBase::GetStackTrace() const
{
if (mStackTraceSize == 0)
return "<No stack trace>\n";
char** strings = backtrace_symbols(mStackTrace, 10);
if (strings == NULL) // Since this is for debug only thus
// non-critical, don't throw an exception.
return "<Unknown error: backtrace_symbols returned NULL>\n";
std::string result;
for (size_t i = 0; i < mStackTraceSize; ++i)
{
std::string mangledName = strings[i];
std::string::size_type begin = mangledName.find('(');
std::string::size_type end = mangledName.find('+', begin);
if (begin == std::string::npos || end == std::string::npos)
{
result += mangledName;
result += '\n';
continue;
}
++begin;
int status;
char* s = abi::__cxa_demangle(mangledName.substr(begin, end-begin).c_str(),
NULL, 0, &status);
if (status != 0)
{
result += mangledName;
result += '\n';
continue;
}
std::string demangledName(s);
free(s);
// Ignore ExceptionBase::Init so the top frame is the
// user's frame where this exception is thrown.
//
// Can't just ignore frame#0 because the compiler might
// inline ExceptionBase::Init.
result += mangledName.substr(0, begin);
result += demangledName;
result += mangledName.substr(end);
result += '\n';
}
free(strings);
return result;
}