forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrom_current.cpp
More file actions
257 lines (237 loc) · 8.87 KB
/
from_current.cpp
File metadata and controls
257 lines (237 loc) · 8.87 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <algorithm>
#include <string>
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <gtest/gtest.h>
#include <gtest/gtest-matchers.h>
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include "common.hpp"
#include "utils/utils.hpp"
#ifdef TEST_MODULE
import cpptrace;
#include <cpptrace/from_current_macros.hpp>
#else
#include <cpptrace/cpptrace.hpp>
#include <cpptrace/from_current.hpp>
#endif
static volatile int truthy = 2;
// NOTE: returning something and then return stacktrace_multi_3(line_numbers) * rand(); is done to prevent TCO even
// under LTO https://github.com/jeremy-rifkin/cpptrace/issues/179#issuecomment-2467302052
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_3(std::vector<int>& line_numbers) {
static volatile int lto_guard; lto_guard = lto_guard + 1;
if(truthy) { // due to a MSVC warning about unreachable code
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
throw std::runtime_error("foobar");
}
return 2;
}
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_2(std::vector<int>& line_numbers) {
static volatile int lto_guard; lto_guard = lto_guard + 1;
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
return stacktrace_from_current_3(line_numbers) * rand();
}
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_1(std::vector<int>& line_numbers) {
static volatile int lto_guard; lto_guard = lto_guard + 1;
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
return stacktrace_from_current_2(line_numbers) * rand();
}
TEST(FromCurrent, Basic) {
std::vector<int> line_numbers;
bool does_enter_catch = false;
auto guard = cpptrace::detail::scope_exit([&] {
EXPECT_TRUE(does_enter_catch);
});
CPPTRACE_TRY {
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
static volatile int tco_guard = stacktrace_from_current_1(line_numbers);
(void)tco_guard;
} CPPTRACE_CATCH(const std::runtime_error& e) {
does_enter_catch = true;
EXPECT_FALSE(cpptrace::current_exception_was_rethrown());
EXPECT_EQ(e.what(), std::string("foobar"));
const auto& trace = cpptrace::from_current_exception();
ASSERT_GE(trace.frames.size(), 4);
auto it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
}
);
ASSERT_NE(it, trace.frames.end()) << trace;
size_t i = static_cast<size_t>(it - trace.frames.begin());
int j = 0;
ASSERT_LT(i, trace.frames.size());
ASSERT_LT(j, line_numbers.size());
EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_3"));
i++;
j++;
ASSERT_LT(i, trace.frames.size());
ASSERT_LT(j, line_numbers.size());
EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_2"));
i++;
j++;
ASSERT_LT(i, trace.frames.size());
ASSERT_LT(j, line_numbers.size());
EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_1"));
i++;
j++;
ASSERT_LT(i, trace.frames.size());
ASSERT_LT(j, line_numbers.size());
EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("FromCurrent_Basic_Test::TestBody"));
}
}
TEST(FromCurrent, CorrectHandler) {
std::vector<int> line_numbers;
bool wrong_handler = false;
CPPTRACE_TRY {
CPPTRACE_TRY {
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
stacktrace_from_current_1(line_numbers);
} CPPTRACE_CATCH(const std::logic_error&) {
wrong_handler = true;
}
} CPPTRACE_CATCH(const std::exception& e) {
EXPECT_EQ(e.what(), std::string("foobar"));
const auto& trace = cpptrace::from_current_exception();
auto it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
}
);
EXPECT_NE(it, trace.frames.end());
it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("FromCurrent_CorrectHandler_Test::TestBody") != std::string::npos;
}
);
EXPECT_NE(it, trace.frames.end());
}
if(wrong_handler) {
FAIL();
}
}
TEST(FromCurrent, RawTrace) {
std::vector<int> line_numbers;
CPPTRACE_TRY {
line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
static volatile int tco_guard = stacktrace_from_current_1(line_numbers);
(void)tco_guard;
} CPPTRACE_CATCH(const std::exception& e) {
EXPECT_EQ(e.what(), std::string("foobar"));
const auto& raw_trace = cpptrace::raw_trace_from_current_exception();
auto trace = raw_trace.resolve();
auto it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
}
);
EXPECT_NE(it, trace.frames.end());
it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("FromCurrent_RawTrace_Test::TestBody") != std::string::npos;
}
);
EXPECT_NE(it, trace.frames.end());
}
}
TEST(FromCurrent, NonThrowingPath) {
bool does_enter_catch = false;
bool does_reach_end = false;
auto guard = cpptrace::detail::scope_exit([&] {
EXPECT_FALSE(does_enter_catch);
EXPECT_TRUE(does_reach_end);
});
CPPTRACE_TRY {
// pass
} CPPTRACE_CATCH(const std::runtime_error&) {
does_enter_catch = true;
}
does_reach_end = true;
}
#ifdef _MSC_VER
CPPTRACE_FORCE_NO_INLINE
int my_div_function(int x, int y) {
return x / y;
}
int divide_zero_filter(int code) {
if(code == STATUS_INTEGER_DIVIDE_BY_ZERO || code == EXCEPTION_FLT_DIVIDE_BY_ZERO) {
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
TEST(FromCurrent, SEHBasic) {
bool does_enter_catch = false;
auto guard = cpptrace::detail::scope_exit([&] {
EXPECT_TRUE(does_enter_catch);
});
[&] () {
CPPTRACE_SEH_TRY {
[&] () {
volatile auto res = my_div_function(10, 0);
(void)res;
} ();
} CPPTRACE_SEH_EXCEPT(divide_zero_filter(GetExceptionCode())) {
[&] () {
does_enter_catch = true;
EXPECT_FALSE(cpptrace::current_exception_was_rethrown());
const auto& trace = cpptrace::from_current_exception();
auto it = std::find_if(
trace.frames.begin(),
trace.frames.end(),
[](const cpptrace::stacktrace_frame& frame) {
return frame.symbol.find("my_div_function") != std::string::npos;
}
);
ASSERT_NE(it, trace.frames.end()) << trace;
size_t i = static_cast<size_t>(it - trace.frames.begin());
EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
} ();
}
} ();
EXPECT_TRUE(does_enter_catch);
}
TEST(FromCurrent, SEHCorrectHandler) {
bool does_enter_catch = false;
auto guard = cpptrace::detail::scope_exit([&] {
EXPECT_TRUE(does_enter_catch);
});
[&] () {
CPPTRACE_SEH_TRY {
[&] () {
CPPTRACE_SEH_TRY {
[&] () {
volatile auto res = my_div_function(10, 0);
(void)res;
} ();
} CPPTRACE_SEH_EXCEPT(EXCEPTION_CONTINUE_SEARCH) {
[&] () {
FAIL();
} ();
}
} ();
} CPPTRACE_SEH_EXCEPT(divide_zero_filter(GetExceptionCode())) {
does_enter_catch = true;
}
} ();
EXPECT_TRUE(does_enter_catch);
}
#endif