Debian Codegraph @

test/unit/tracing/from_current.cpp

raw
1 #include <algorithm>
2 #include <string>
3
4 #include <gtest/gtest.h>
5 #include <gtest/gtest-matchers.h>
6 #include <gmock/gmock.h>
7 #include <gmock/gmock-matchers.h>
8
9 #include "common.hpp"
10 #include "utils/utils.hpp"
11
12 #ifdef TEST_MODULE
13 import cpptrace;
14
15 #include <cpptrace/from_current_macros.hpp>
16 #else
17 #include <cpptrace/cpptrace.hpp>
18 #include <cpptrace/from_current.hpp>
19 #endif
20
21
22 static volatile int truthy = 2;
23
24 // NOTE: returning something and then return stacktrace_multi_3(line_numbers) * rand(); is done to prevent TCO even
25 // under LTO https://github.com/jeremy-rifkin/cpptrace/issues/179#issuecomment-2467302052
26 CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_3(std::vector<int>& line_numbers) {
27 static volatile int lto_guard; lto_guard = lto_guard + 1;
28 if(truthy) { // due to a MSVC warning about unreachable code
29 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
30 throw std::runtime_error("foobar");
31 }
32 return 2;
33 }
34
35 CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_2(std::vector<int>& line_numbers) {
36 static volatile int lto_guard; lto_guard = lto_guard + 1;
37 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
38 return stacktrace_from_current_3(line_numbers) * rand();
39 }
40
41 CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_1(std::vector<int>& line_numbers) {
42 static volatile int lto_guard; lto_guard = lto_guard + 1;
43 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
44 return stacktrace_from_current_2(line_numbers) * rand();
45 }
46
47 TEST(FromCurrent, Basic) {
48 std::vector<int> line_numbers;
49 bool does_enter_catch = false;
50 auto guard = cpptrace::detail::scope_exit([&] {
51 EXPECT_TRUE(does_enter_catch);
52 });
53 CPPTRACE_TRY {
54 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
55 static volatile int tco_guard = stacktrace_from_current_1(line_numbers);
56 (void)tco_guard;
57 } CPPTRACE_CATCH(const std::runtime_error& e) {
58 does_enter_catch = true;
59 EXPECT_FALSE(cpptrace::current_exception_was_rethrown());
60 EXPECT_EQ(e.what(), std::string("foobar"));
61 const auto& trace = cpptrace::from_current_exception();
62 ASSERT_GE(trace.frames.size(), 4);
63 auto it = std::find_if(
64 trace.frames.begin(),
65 trace.frames.end(),
66 [](const cpptrace::stacktrace_frame& frame) {
67 return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
68 }
69 );
70 ASSERT_NE(it, trace.frames.end()) << trace;
71 size_t i = static_cast<size_t>(it - trace.frames.begin());
72 int j = 0;
73 ASSERT_LT(i, trace.frames.size());
74 ASSERT_LT(j, line_numbers.size());
75 EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
76 EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
77 EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_3"));
78 i++;
79 j++;
80 ASSERT_LT(i, trace.frames.size());
81 ASSERT_LT(j, line_numbers.size());
82 EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
83 EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
84 EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_2"));
85 i++;
86 j++;
87 ASSERT_LT(i, trace.frames.size());
88 ASSERT_LT(j, line_numbers.size());
89 EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
90 EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
91 EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_1"));
92 i++;
93 j++;
94 ASSERT_LT(i, trace.frames.size());
95 ASSERT_LT(j, line_numbers.size());
96 EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
97 EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]);
98 EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("FromCurrent_Basic_Test::TestBody"));
99 }
100 }
101
102 TEST(FromCurrent, CorrectHandler) {
103 std::vector<int> line_numbers;
104 bool wrong_handler = false;
105 CPPTRACE_TRY {
106 CPPTRACE_TRY {
107 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
108 stacktrace_from_current_1(line_numbers);
109 } CPPTRACE_CATCH(const std::logic_error&) {
110 wrong_handler = true;
111 }
112 } CPPTRACE_CATCH(const std::exception& e) {
113 EXPECT_EQ(e.what(), std::string("foobar"));
114 const auto& trace = cpptrace::from_current_exception();
115 auto it = std::find_if(
116 trace.frames.begin(),
117 trace.frames.end(),
118 [](const cpptrace::stacktrace_frame& frame) {
119 return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
120 }
121 );
122 EXPECT_NE(it, trace.frames.end());
123 it = std::find_if(
124 trace.frames.begin(),
125 trace.frames.end(),
126 [](const cpptrace::stacktrace_frame& frame) {
127 return frame.symbol.find("FromCurrent_CorrectHandler_Test::TestBody") != std::string::npos;
128 }
129 );
130 EXPECT_NE(it, trace.frames.end());
131 }
132 if(wrong_handler) {
133 FAIL();
134 }
135 }
136
137 TEST(FromCurrent, RawTrace) {
138 std::vector<int> line_numbers;
139 CPPTRACE_TRY {
140 line_numbers.insert(line_numbers.begin(), __LINE__ + 1);
141 static volatile int tco_guard = stacktrace_from_current_1(line_numbers);
142 (void)tco_guard;
143 } CPPTRACE_CATCH(const std::exception& e) {
144 EXPECT_EQ(e.what(), std::string("foobar"));
145 const auto& raw_trace = cpptrace::raw_trace_from_current_exception();
146 auto trace = raw_trace.resolve();
147 auto it = std::find_if(
148 trace.frames.begin(),
149 trace.frames.end(),
150 [](const cpptrace::stacktrace_frame& frame) {
151 return frame.symbol.find("stacktrace_from_current_3") != std::string::npos;
152 }
153 );
154 EXPECT_NE(it, trace.frames.end());
155 it = std::find_if(
156 trace.frames.begin(),
157 trace.frames.end(),
158 [](const cpptrace::stacktrace_frame& frame) {
159 return frame.symbol.find("FromCurrent_RawTrace_Test::TestBody") != std::string::npos;
160 }
161 );
162 EXPECT_NE(it, trace.frames.end());
163 }
164 }
165
166 TEST(FromCurrent, NonThrowingPath) {
167 bool does_enter_catch = false;
168 bool does_reach_end = false;
169 auto guard = cpptrace::detail::scope_exit([&] {
170 EXPECT_FALSE(does_enter_catch);
171 EXPECT_TRUE(does_reach_end);
172 });
173 CPPTRACE_TRY {
174 // pass
175 } CPPTRACE_CATCH(const std::runtime_error&) {
176 does_enter_catch = true;
177 }
178 does_reach_end = true;
179 }
180
181 #ifdef _MSC_VER
182
183 CPPTRACE_FORCE_NO_INLINE
184 int my_div_function(int x, int y) {
185 return x / y;
186 }
187
188 int divide_zero_filter(int code) {
189 if(code == STATUS_INTEGER_DIVIDE_BY_ZERO || code == EXCEPTION_FLT_DIVIDE_BY_ZERO) {
190 return EXCEPTION_EXECUTE_HANDLER;
191 }
192 return EXCEPTION_CONTINUE_SEARCH;
193 }
194
195 TEST(FromCurrent, SEHBasic) {
196 bool does_enter_catch = false;
197 auto guard = cpptrace::detail::scope_exit([&] {
198 EXPECT_TRUE(does_enter_catch);
199 });
200 [&] () {
201 CPPTRACE_SEH_TRY {
202 [&] () {
203 volatile auto res = my_div_function(10, 0);
204 (void)res;
205 } ();
206 } CPPTRACE_SEH_EXCEPT(divide_zero_filter(GetExceptionCode())) {
207 [&] () {
208 does_enter_catch = true;
209 EXPECT_FALSE(cpptrace::current_exception_was_rethrown());
210 const auto& trace = cpptrace::from_current_exception();
211 auto it = std::find_if(
212 trace.frames.begin(),
213 trace.frames.end(),
214 [](const cpptrace::stacktrace_frame& frame) {
215 return frame.symbol.find("my_div_function") != std::string::npos;
216 }
217 );
218 ASSERT_NE(it, trace.frames.end()) << trace;
219 size_t i = static_cast<size_t>(it - trace.frames.begin());
220 EXPECT_FILE(trace.frames[i].filename, "from_current.cpp");
221 } ();
222 }
223 } ();
224 EXPECT_TRUE(does_enter_catch);
225 }
226
227 TEST(FromCurrent, SEHCorrectHandler) {
228 bool does_enter_catch = false;
229 auto guard = cpptrace::detail::scope_exit([&] {
230 EXPECT_TRUE(does_enter_catch);
231 });
232 [&] () {
233 CPPTRACE_SEH_TRY {
234 [&] () {
235 CPPTRACE_SEH_TRY {
236 [&] () {
237 volatile auto res = my_div_function(10, 0);
238 (void)res;
239 } ();
240 } CPPTRACE_SEH_EXCEPT(EXCEPTION_CONTINUE_SEARCH) {
241 [&] () {
242 FAIL();
243 } ();
244 }
245 } ();
246 } CPPTRACE_SEH_EXCEPT(divide_zero_filter(GetExceptionCode())) {
247 does_enter_catch = true;
248 }
249 } ();
250 EXPECT_TRUE(does_enter_catch);
251 }
252
253 #endif