Repository navigation
Expand file tree
/
Copy pathDebug.h
More file actions
198 lines (151 loc) · 4.57 KB
/
Copy pathDebug.h
File metadata and controls
198 lines (151 loc) · 4.57 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* \file Debug.h
* \author Jan Milík <milikjan@fit.cvut.cz>
* \date 2013-06-26
*
* \brief Header file for the Debug class.
*/
#ifndef DEBUG_UDCRJS1F
#define DEBUG_UDCRJS1F
#include <execinfo.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <cxxabi.h>
#include "utils.h"
namespace cppapp {
////////////////////////////////////////////////////////////////////////////////
// BACKTRACE
////////////////////////////////////////////////////////////////////////////////
#define CPPAPP_BACKTRACE_LIMIT 256
class Backtrace {
public:
typedef void* AddressType;
private:
std::vector<AddressType> addresses_;
std::vector<std::string> symbols_;
std::vector<std::string> lines_;
bool getLines()
{
std::stringstream tmpStream;
std::vector<std::string> lines;
FOR_EACH(addresses_, addr) {
tmpStream.str("");
tmpStream << "addr2line -fCp " << *addr << " 2> /dev/null";
FILE *pipe = popen(tmpStream.str().c_str(), "r");
if (!pipe) {
tmpStream.str("");
tmpStream << "[" << *addr << "]";
lines.push_back(tmpStream.str());
continue;
}
char buffer[128];
tmpStream.str("");
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL) {
for (char *p = buffer; (p < buffer + 128) && *p != '\n'; p++)
tmpStream << *p;
}
}
pclose(pipe);
lines.push_back(tmpStream.str());
}
lines_.swap(lines);
return true;
}
public:
typedef std::vector<std::string>::const_iterator Iterator;
int size() const { return (int)addresses_.size(); }
Iterator begin() const { return lines_.begin(); }
Iterator end() const { return lines_.end(); }
std::string operator[](int index) { return lines_[index]; }
void get(int maxSize = CPPAPP_BACKTRACE_LIMIT);
void getSymbols();
void print(ostream &out) const;
static void print();
};
std::ostream& operator<< (std::ostream &output, const Backtrace &bt);
////////////////////////////////////////////////////////////////////////////////
// ASSERT
////////////////////////////////////////////////////////////////////////////////
#ifdef NDEBUG
# define CPPAPP_ASSERT(expr)
#else
/**
* \brief Evaluates the expression \c expr and if it is false, either aborts
* the program or throws an exception.
*
* The action taken can be changed using Assert methods. For more information,
* see cppapp::Assert::assert_() and cppapp::Assert::setThrow().
*
* Like the original \ref assert macro, this macro is disabled if the
* \ref NDEBUG macro is defined.
*
* \see cppapp::Assert::assert_()
* \see cppapp::Assert::setThrow()
*/
# define CPPAPP_ASSERT(expr) \
{ cppapp::Assert::assert_((expr), #expr, __FILE__, __LINE__); }
#endif
/**
* \brief Works like the CPPAPP_ASSERT(expr) macro, except it always throws an
* exception regardless of the Assert class state.
*/
#define CPPAPP_THROW_ASSERT(expr) \
{ cppapp::Assert::throwAssert((expr), #expr, __FILE__, __LINE__); }
/**
* \brief Static class replacing the standard \c assert macro in \c cassert.
*
* Preferred usage of this class is through the CPPAPP_ASSERT(expr) and
* CPPAPP_THROW_ASSERT(expr) macros.
*
* For more information on the \c assert macro, see
* http://www.cplusplus.com/reference/cassert/ .
*
* \see CPPAPP_ASSERT
* \see CPPAPP_THROW_ASSERT
*/
class Assert {
private:
static bool throwExceptions_;
Assert();
public:
/**
* \brief Sets whether CPPAPP_ASSERT should throw an exception (if
* \c true) or abort the program.
*
* \see CPPAPP_ASSERT
* \see Assert::assert_
*/
static void setThrow(bool value);
static void abortAssert(bool condition,
const char *expr,
const char *file,
int line);
static void throwAssert(bool condition,
const char *expr,
const char *file,
int line);
/**
* \brief The main interface of the Assert class.
*
* Based on internal logic, this method simply calls Assert::abortAssert()
* or Assert::throwAssert(). To controll this behavior, call
* Assert::setThrow().
*
* \param condition The condition that is expected to be \c true.
* \param expr The expression whose value is passed to \c condition.
* \param file The name of the file from which the assertion has been called.
* \param line Line in the file where the assertion is located.
*
* \see CPPAPP_ASSERT
* \see CPPAPP_THROW_ASSERT
* \see Assert::setThrow
*/
static void assert_(bool condition, const char *expr, const char *file, int line);
};
} /* namespace cppapp */
#endif /* end of include guard: DEBUG_UDCRJS1F */