| 1 |
#include <algorithm> |
| 2 |
#include <exception> |
| 3 |
#include <string> |
| 4 |
|
| 5 |
#include <gtest/gtest.h> |
| 6 |
#include <gtest/gtest-matchers.h> |
| 7 |
#include <gmock/gmock.h> |
| 8 |
#include <gmock/gmock-matchers.h> |
| 9 |
|
| 10 |
#include "common.hpp" |
| 11 |
|
| 12 |
#ifdef TEST_MODULE |
| 13 |
import cpptrace; |
| 14 |
|
| 15 |
#include <cpptrace/from_current.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_rethrow_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 |
| 36 |
int stacktrace_from_current_rethrow_2(std::vector<int>& line_numbers, std::vector<int>& rethrow_line_numbers) { |
| 37 |
int ret; |
| 38 |
CPPTRACE_TRY { |
| 39 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 40 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 41 |
ret = stacktrace_from_current_rethrow_3(line_numbers) * rand(); |
| 42 |
} CPPTRACE_CATCH(const std::exception&) { |
| 43 |
rethrow_line_numbers.insert(rethrow_line_numbers.begin(), __LINE__ + 1); |
| 44 |
cpptrace::rethrow(); |
| 45 |
} |
| 46 |
return ret; |
| 47 |
} |
| 48 |
|
| 49 |
#ifdef _MSC_VER |
| 50 |
#pragma warning(push) |
| 51 |
#pragma warning(disable: 4702) // unreachable code |
| 52 |
#endif |
| 53 |
CPPTRACE_FORCE_NO_INLINE |
| 54 |
int stacktrace_from_current_rethrow_1(std::vector<int>& line_numbers, std::vector<int>& rethrow_line_numbers) { |
| 55 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 56 |
rethrow_line_numbers.insert(rethrow_line_numbers.begin(), __LINE__ + 2); |
| 57 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 58 |
return stacktrace_from_current_rethrow_2(line_numbers, rethrow_line_numbers) * rand(); |
| 59 |
} |
| 60 |
#ifdef _MSC_VER |
| 61 |
#pragma warning(pop) |
| 62 |
#endif |
| 63 |
|
| 64 |
void clean_trace(cpptrace::stacktrace& trace, std::vector<cpptrace::stacktrace_frame>::iterator it) { |
| 65 |
// because stacktrace_from_current_rethrow_2 has a try/catch which uses lambdas under msvc, we need to filter |
| 66 |
// all but the first frame mentioning stacktrace_from_current_rethrow_2. |
| 67 |
auto frame_2_it = std::find_if( |
| 68 |
it, |
| 69 |
trace.frames.end(), |
| 70 |
[](const cpptrace::stacktrace_frame& frame) { |
| 71 |
return frame.symbol.find("stacktrace_from_current_rethrow_2") != std::string::npos; |
| 72 |
} |
| 73 |
); |
| 74 |
ASSERT_NE(frame_2_it, trace.frames.end()); |
| 75 |
frame_2_it++; |
| 76 |
auto remove_it = std::remove_if( |
| 77 |
frame_2_it, |
| 78 |
trace.frames.end(), |
| 79 |
[](const cpptrace::stacktrace_frame& frame) { |
| 80 |
return frame.symbol.find("stacktrace_from_current_rethrow_2") != std::string::npos; |
| 81 |
} |
| 82 |
); |
| 83 |
if(remove_it != trace.frames.end()) { |
| 84 |
trace.frames.erase(remove_it); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
TEST(Rethrow, RethrowPreservesTrace) { |
| 89 |
std::vector<int> line_numbers; |
| 90 |
std::vector<int> rethrow_line_numbers; |
| 91 |
CPPTRACE_TRY { |
| 92 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 93 |
static volatile int tco_guard = stacktrace_from_current_rethrow_1(line_numbers, rethrow_line_numbers); |
| 94 |
(void)tco_guard; |
| 95 |
} CPPTRACE_CATCH(const std::runtime_error& e) { |
| 96 |
EXPECT_TRUE(cpptrace::current_exception_was_rethrown()); |
| 97 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 98 |
auto trace = cpptrace::from_current_exception(); |
| 99 |
ASSERT_GE(trace.frames.size(), 4); |
| 100 |
auto it = std::find_if( |
| 101 |
trace.frames.begin(), |
| 102 |
trace.frames.end(), |
| 103 |
[](const cpptrace::stacktrace_frame& frame) { |
| 104 |
return frame.symbol.find("stacktrace_from_current_rethrow_3") != std::string::npos; |
| 105 |
} |
| 106 |
); |
| 107 |
ASSERT_NE(it, trace.frames.end()) << trace; |
| 108 |
clean_trace(trace, it); |
| 109 |
size_t i = static_cast<size_t>(it - trace.frames.begin()); |
| 110 |
int j = 0; |
| 111 |
ASSERT_LT(i, trace.frames.size()); |
| 112 |
ASSERT_LT(j, line_numbers.size()); |
| 113 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 114 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 115 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_rethrow_3")); |
| 116 |
i++; |
| 117 |
j++; |
| 118 |
ASSERT_LT(i, trace.frames.size()); |
| 119 |
ASSERT_LT(j, line_numbers.size()); |
| 120 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 121 |
#ifndef _MSC_VER |
| 122 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 123 |
#endif |
| 124 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_rethrow_2")); |
| 125 |
i++; |
| 126 |
j++; |
| 127 |
ASSERT_LT(i, trace.frames.size()); |
| 128 |
ASSERT_LT(j, line_numbers.size()); |
| 129 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 130 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 131 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_rethrow_1")); |
| 132 |
i++; |
| 133 |
j++; |
| 134 |
ASSERT_LT(i, trace.frames.size()); |
| 135 |
ASSERT_LT(j, line_numbers.size()); |
| 136 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 137 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 138 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("Rethrow_RethrowPreservesTrace_Test::TestBody")); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
TEST(Rethrow, RethrowTraceCorrect) { |
| 143 |
std::vector<int> line_numbers; |
| 144 |
std::vector<int> rethrow_line_numbers; |
| 145 |
CPPTRACE_TRY { |
| 146 |
rethrow_line_numbers.insert(rethrow_line_numbers.begin(), __LINE__ + 2); |
| 147 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 148 |
static volatile int tco_guard = stacktrace_from_current_rethrow_1(line_numbers, rethrow_line_numbers); |
| 149 |
(void)tco_guard; |
| 150 |
} CPPTRACE_CATCH(const std::runtime_error& e) { |
| 151 |
EXPECT_TRUE(cpptrace::current_exception_was_rethrown()); |
| 152 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 153 |
auto rethrow_trace = cpptrace::from_current_exception_rethrow(); |
| 154 |
ASSERT_GE(rethrow_trace.frames.size(), 4); |
| 155 |
// reverse to get the last one matching instead of "`stacktrace_from_current_rethrow_2'::`1'::catch$4()" on msvc |
| 156 |
auto rit = std::find_if( |
| 157 |
rethrow_trace.frames.rbegin(), |
| 158 |
rethrow_trace.frames.rend(), |
| 159 |
[](const cpptrace::stacktrace_frame& frame) { |
| 160 |
return frame.symbol.find("stacktrace_from_current_rethrow_2") != std::string::npos |
| 161 |
&& frame.symbol.find("::catch") == std::string::npos; |
| 162 |
} |
| 163 |
); |
| 164 |
ASSERT_NE(rit, rethrow_trace.frames.rend()) << rethrow_trace; |
| 165 |
size_t i = static_cast<size_t>(&*rit - &*rethrow_trace.frames.begin()); |
| 166 |
auto it = rethrow_trace.frames.begin() + i; |
| 167 |
clean_trace(rethrow_trace, it); |
| 168 |
int j = 0; |
| 169 |
ASSERT_LT(i, rethrow_trace.frames.size()); |
| 170 |
ASSERT_LT(j, rethrow_line_numbers.size()); |
| 171 |
EXPECT_FILE(rethrow_trace.frames[i].filename, "rethrow.cpp"); |
| 172 |
#ifndef _MSC_VER |
| 173 |
EXPECT_LINE(rethrow_trace.frames[i].line.value(), rethrow_line_numbers[j]); |
| 174 |
#endif |
| 175 |
EXPECT_THAT(rethrow_trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_rethrow_2")); |
| 176 |
i++; |
| 177 |
j++; |
| 178 |
ASSERT_LT(i, rethrow_trace.frames.size()); |
| 179 |
ASSERT_LT(j, rethrow_line_numbers.size()); |
| 180 |
EXPECT_FILE(rethrow_trace.frames[i].filename, "rethrow.cpp"); |
| 181 |
EXPECT_LINE(rethrow_trace.frames[i].line.value(), rethrow_line_numbers[j]); |
| 182 |
EXPECT_THAT(rethrow_trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_rethrow_1")); |
| 183 |
i++; |
| 184 |
j++; |
| 185 |
ASSERT_LT(i, rethrow_trace.frames.size()); |
| 186 |
ASSERT_LT(j, rethrow_line_numbers.size()); |
| 187 |
EXPECT_FILE(rethrow_trace.frames[i].filename, "rethrow.cpp"); |
| 188 |
EXPECT_LINE(rethrow_trace.frames[i].line.value(), rethrow_line_numbers[j]); |
| 189 |
EXPECT_THAT(rethrow_trace.frames[i].symbol, testing::HasSubstr("Rethrow_RethrowTraceCorrect_Test::TestBody")); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// NOTE: returning something and then return stacktrace_multi_3(line_numbers) * rand(); is done to prevent TCO even |
| 194 |
// under LTO https://github.com/jeremy-rifkin/cpptrace/issues/179#issuecomment-2467302052 |
| 195 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_basic_3(std::vector<int>& line_numbers) { |
| 196 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 197 |
if(truthy) { // due to a MSVC warning about unreachable code |
| 198 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 199 |
throw std::runtime_error("foobar"); |
| 200 |
} |
| 201 |
return 2; |
| 202 |
} |
| 203 |
|
| 204 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_basic_2(std::vector<int>& line_numbers) { |
| 205 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 206 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 207 |
return stacktrace_from_current_basic_3(line_numbers) * rand(); |
| 208 |
} |
| 209 |
|
| 210 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_basic_1(std::vector<int>& line_numbers) { |
| 211 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 212 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 213 |
return stacktrace_from_current_basic_2(line_numbers) * rand(); |
| 214 |
} |
| 215 |
|
| 216 |
TEST(Rethrow, RethrowDoesntInterfereWithSubsequentTraces) { |
| 217 |
std::vector<int> line_numbers; |
| 218 |
std::vector<int> rethrow_line_numbers; |
| 219 |
CPPTRACE_TRY { |
| 220 |
// do rethrow path so a rethrow happens |
| 221 |
try { |
| 222 |
static volatile int tco_guard = stacktrace_from_current_rethrow_1(line_numbers, rethrow_line_numbers); |
| 223 |
(void)tco_guard; |
| 224 |
} catch(...) {} |
| 225 |
// now do real test |
| 226 |
line_numbers.clear(); |
| 227 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 228 |
stacktrace_from_current_basic_1(line_numbers); |
| 229 |
} CPPTRACE_CATCH(const std::runtime_error& e) { |
| 230 |
EXPECT_FALSE(cpptrace::current_exception_was_rethrown()); |
| 231 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 232 |
auto trace = cpptrace::from_current_exception(); |
| 233 |
ASSERT_GE(trace.frames.size(), 4); |
| 234 |
auto it = std::find_if( |
| 235 |
trace.frames.begin(), |
| 236 |
trace.frames.end(), |
| 237 |
[](const cpptrace::stacktrace_frame& frame) { |
| 238 |
return frame.symbol.find("stacktrace_from_current_basic_3") != std::string::npos; |
| 239 |
} |
| 240 |
); |
| 241 |
ASSERT_NE(it, trace.frames.end()) << trace; |
| 242 |
size_t i = static_cast<size_t>(it - trace.frames.begin()); |
| 243 |
int j = 0; |
| 244 |
ASSERT_LT(i, trace.frames.size()); |
| 245 |
ASSERT_LT(j, line_numbers.size()); |
| 246 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 247 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 248 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_basic_3")); |
| 249 |
i++; |
| 250 |
j++; |
| 251 |
ASSERT_LT(i, trace.frames.size()); |
| 252 |
ASSERT_LT(j, line_numbers.size()); |
| 253 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 254 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 255 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_basic_2")); |
| 256 |
i++; |
| 257 |
j++; |
| 258 |
ASSERT_LT(i, trace.frames.size()); |
| 259 |
ASSERT_LT(j, line_numbers.size()); |
| 260 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 261 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 262 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_basic_1")); |
| 263 |
i++; |
| 264 |
j++; |
| 265 |
ASSERT_LT(i, trace.frames.size()); |
| 266 |
ASSERT_LT(j, line_numbers.size()); |
| 267 |
EXPECT_FILE(trace.frames[i].filename, "rethrow.cpp"); |
| 268 |
#ifndef _MSC_VER |
| 269 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 270 |
#endif |
| 271 |
EXPECT_THAT( |
| 272 |
trace.frames[i].symbol, |
| 273 |
testing::HasSubstr("Rethrow_RethrowDoesntInterfereWithSubsequentTraces_Test::TestBody") |
| 274 |
); |
| 275 |
} |
| 276 |
} |