| 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 |
|
| 11 |
#ifdef TEST_MODULE |
| 12 |
import cpptrace; |
| 13 |
#else |
| 14 |
#include <cpptrace/cpptrace.hpp> |
| 15 |
#include <cpptrace/from_current.hpp> |
| 16 |
#endif |
| 17 |
|
| 18 |
|
| 19 |
static volatile int truthy = 2; |
| 20 |
|
| 21 |
// NOTE: returning something and then return stacktrace_multi_3(line_numbers) * rand(); is done to prevent TCO even |
| 22 |
// under LTO https://github.com/jeremy-rifkin/cpptrace/issues/179#issuecomment-2467302052 |
| 23 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_try_catch_3(std::vector<int>& line_numbers) { |
| 24 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 25 |
if(truthy) { // due to a MSVC warning about unreachable code |
| 26 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 27 |
throw std::runtime_error("foobar"); |
| 28 |
} |
| 29 |
return 2; |
| 30 |
} |
| 31 |
|
| 32 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_try_catch_2(std::vector<int>& line_numbers) { |
| 33 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 34 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 35 |
return stacktrace_from_current_try_catch_3(line_numbers) * rand(); |
| 36 |
} |
| 37 |
|
| 38 |
CPPTRACE_FORCE_NO_INLINE int stacktrace_from_current_try_catch_1(std::vector<int>& line_numbers) { |
| 39 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 40 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 41 |
return stacktrace_from_current_try_catch_2(line_numbers) * rand(); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(FromCurrentTryCatch, Basic) { |
| 45 |
std::vector<int> line_numbers; |
| 46 |
cpptrace::try_catch( |
| 47 |
[&] { |
| 48 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 49 |
static volatile int tco_guard = stacktrace_from_current_try_catch_1(line_numbers); |
| 50 |
(void)tco_guard; |
| 51 |
}, |
| 52 |
[&] (const std::runtime_error& e) { |
| 53 |
EXPECT_FALSE(cpptrace::current_exception_was_rethrown()); |
| 54 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 55 |
const auto& trace = cpptrace::from_current_exception(); |
| 56 |
ASSERT_GE(trace.frames.size(), 4); |
| 57 |
auto it = std::find_if( |
| 58 |
trace.frames.begin(), |
| 59 |
trace.frames.end(), |
| 60 |
[](const cpptrace::stacktrace_frame& frame) { |
| 61 |
return frame.symbol.find("stacktrace_from_current_try_catch_3") != std::string::npos; |
| 62 |
} |
| 63 |
); |
| 64 |
ASSERT_NE(it, trace.frames.end()); |
| 65 |
size_t i = static_cast<size_t>(it - trace.frames.begin()); |
| 66 |
int j = 0; |
| 67 |
ASSERT_LT(i, trace.frames.size()); |
| 68 |
ASSERT_LT(j, line_numbers.size()); |
| 69 |
EXPECT_FILE(trace.frames[i].filename, "from_current_try_catch.cpp"); |
| 70 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 71 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_try_catch_3")); |
| 72 |
i++; |
| 73 |
j++; |
| 74 |
ASSERT_LT(i, trace.frames.size()); |
| 75 |
ASSERT_LT(j, line_numbers.size()); |
| 76 |
EXPECT_FILE(trace.frames[i].filename, "from_current_try_catch.cpp"); |
| 77 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 78 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_try_catch_2")); |
| 79 |
i++; |
| 80 |
j++; |
| 81 |
ASSERT_LT(i, trace.frames.size()); |
| 82 |
ASSERT_LT(j, line_numbers.size()); |
| 83 |
EXPECT_FILE(trace.frames[i].filename, "from_current_try_catch.cpp"); |
| 84 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 85 |
EXPECT_THAT(trace.frames[i].symbol, testing::HasSubstr("stacktrace_from_current_try_catch_1")); |
| 86 |
i++; |
| 87 |
j++; |
| 88 |
ASSERT_LT(i, trace.frames.size()); |
| 89 |
ASSERT_LT(j, line_numbers.size()); |
| 90 |
EXPECT_FILE(trace.frames[i].filename, "from_current_try_catch.cpp"); |
| 91 |
EXPECT_LINE(trace.frames[i].line.value(), line_numbers[j]); |
| 92 |
} |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(FromCurrentTryCatch, CorrectHandler) { |
| 97 |
std::vector<int> line_numbers; |
| 98 |
cpptrace::try_catch( |
| 99 |
[&] { |
| 100 |
cpptrace::try_catch( |
| 101 |
[&] { |
| 102 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 103 |
stacktrace_from_current_try_catch_1(line_numbers); |
| 104 |
}, |
| 105 |
[&] (const std::logic_error&) { |
| 106 |
FAIL(); |
| 107 |
} |
| 108 |
); |
| 109 |
}, |
| 110 |
[&] (const std::logic_error&) { |
| 111 |
FAIL(); |
| 112 |
}, |
| 113 |
[&] (const std::runtime_error& e) { |
| 114 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 115 |
const auto& trace = cpptrace::from_current_exception(); |
| 116 |
auto it = std::find_if( |
| 117 |
trace.frames.begin(), |
| 118 |
trace.frames.end(), |
| 119 |
[](const cpptrace::stacktrace_frame& frame) { |
| 120 |
return frame.symbol.find("stacktrace_from_current_try_catch_3") != std::string::npos; |
| 121 |
} |
| 122 |
); |
| 123 |
EXPECT_NE(it, trace.frames.end()); |
| 124 |
it = std::find_if( |
| 125 |
trace.frames.begin(), |
| 126 |
trace.frames.end(), |
| 127 |
[](const cpptrace::stacktrace_frame& frame) { |
| 128 |
return frame.symbol.find("FromCurrentTryCatch_CorrectHandler_Test::TestBody") != std::string::npos; |
| 129 |
} |
| 130 |
); |
| 131 |
EXPECT_NE(it, trace.frames.end()); |
| 132 |
} |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
TEST(FromCurrentTryCatch, RawTrace) { |
| 137 |
std::vector<int> line_numbers; |
| 138 |
cpptrace::try_catch( |
| 139 |
[&] { |
| 140 |
line_numbers.insert(line_numbers.begin(), __LINE__ + 1); |
| 141 |
static volatile int tco_guard = stacktrace_from_current_try_catch_1(line_numbers); |
| 142 |
(void)tco_guard; |
| 143 |
}, |
| 144 |
[&] (const std::runtime_error& e) { |
| 145 |
EXPECT_EQ(e.what(), std::string("foobar")); |
| 146 |
const auto& raw_trace = cpptrace::raw_trace_from_current_exception(); |
| 147 |
auto trace = raw_trace.resolve(); |
| 148 |
auto it = std::find_if( |
| 149 |
trace.frames.begin(), |
| 150 |
trace.frames.end(), |
| 151 |
[](const cpptrace::stacktrace_frame& frame) { |
| 152 |
return frame.symbol.find("stacktrace_from_current_try_catch_3") != std::string::npos; |
| 153 |
} |
| 154 |
); |
| 155 |
EXPECT_NE(it, trace.frames.end()); |
| 156 |
it = std::find_if( |
| 157 |
trace.frames.begin(), |
| 158 |
trace.frames.end(), |
| 159 |
[](const cpptrace::stacktrace_frame& frame) { |
| 160 |
return frame.symbol.find("FromCurrentTryCatch_RawTrace_Test::TestBody") != std::string::npos; |
| 161 |
} |
| 162 |
); |
| 163 |
EXPECT_NE(it, trace.frames.end()); |
| 164 |
} |
| 165 |
); |
| 166 |
} |