Repository navigation
Expand file tree
/
Copy pathDebug.cpp
More file actions
130 lines (88 loc) · 2.42 KB
/
Copy pathDebug.cpp
File metadata and controls
130 lines (88 loc) · 2.42 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
/**
* \file Debug.cpp
* \author Jan Milík <milikjan@fit.cvut.cz>
* \date 2013-06-26
*
* \brief Implementation file for the Debug class.
*/
#include "Debug.h"
namespace cppapp {
////////////////////////////////////////////////////////////////////////////////
// BACKTRACE
////////////////////////////////////////////////////////////////////////////////
void Backtrace::get(int maxSize)
{
AddressType *buffer = new AddressType[maxSize];
size_t actualSize = backtrace(buffer, maxSize);
vector<AddressType> result;
result.reserve(actualSize);
for (size_t i = 0; i < actualSize; i++)
result.push_back(buffer[i]);
addresses_.swap(result);
delete [] buffer;
getSymbols();
}
void Backtrace::getSymbols()
{
char **strings = backtrace_symbols(&addresses_[0], addresses_.size());
std::vector<std::string> symbols;
symbols.reserve(addresses_.size());
std::vector<char> buffer;
for (unsigned int i = 0; i < addresses_.size(); i++) {
size_t length = 0;
int status = 0;
char *output = abi::__cxa_demangle(strings[i], NULL, &length, &status);
if (status == 0)
symbols.push_back(std::string(output));
else
symbols.push_back(std::string(strings[i]));
free(output);
}
symbols_.swap(symbols);
free(strings);
}
void Backtrace::print(ostream &out) const
{
for (unsigned int i = 0; i < symbols_.size(); i++) {
out << symbols_[i] << std::endl;
}
}
std::ostream& operator<< (std::ostream &output, const Backtrace &bt)
{
bt.print(output);
return output;
}
void Backtrace::print()
{
Backtrace bt;
bt.get();
std::cerr << bt << endl;
}
////////////////////////////////////////////////////////////////////////////////
// ASSERT
////////////////////////////////////////////////////////////////////////////////
bool Assert::throwExceptions_ = false;
void Assert::setThrow(bool value)
{
throwExceptions_ = value;
}
void Assert::abortAssert(bool condition, const char *expr, const char *file, int line)
{
if (condition) return;
std::cerr << file << ":" << line << ": " <<
"Assertion failed: " << expr << std::endl;
abort();
}
void Assert::throwAssert(bool condition, const char *expr, const char *file, int line)
{
if (condition) return;
throw std::exception();
}
void Assert::assert_(bool condition, const char *expr, const char *file, int line)
{
if (throwExceptions_)
throwAssert(condition, expr, file, line);
else
abortAssert(condition, expr, file, line);
}
} /* namespace cppapp */