summaryrefslogtreecommitdiffstats
path: root/test/base-stacktrace.cpp
blob: f0b87e2cf1c8107311b0faf9adf230000de64e3f (plain)
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
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */

#include "base/stacktrace.hpp"
#include <BoostTestTargetConfig.h>

using namespace icinga;


/* If you are reading this, you are probably doing so because this test case just failed. This might happen as it
 * heavily depends on platform and compiler behavior. There are two likely causes why this could break:
 *
 *  - Your compiler found new ways to optimize the functions that are called to create a stack, even though we tried
 *    to disable optimizations using #pragmas for some compilers. If you know a way to disable (more) optimizations for
 *    your compiler, you can try if this helps.
 *
 *  - Boost fails to resolve symbol names as we've already seen on some platforms. In this case, you can try again
 *    passing the additional flag `-DICINGA2_STACKTRACE_USE_BACKTRACE_SYMBOLS=ON` to CMake and see if this helps.
 *
 *  In any case, please report a bug. If you run `make CTEST_OUTPUT_ON_FAILURE=1 test`, the stack trace in question
 *  should be printed. If it looks somewhat meaningful, you can probably ignore a failure of this test case.
 */

#pragma GCC push_options
#pragma GCC optimize ("O0")
#pragma clang optimize off
#ifdef _MSVC_VER
#pragma optimize("", off)
#endif /* _MSVC_VER */

BOOST_AUTO_TEST_SUITE(base_stacktrace)

[[gnu::noinline]]
void stack_test_func_b()
{
	boost::stacktrace::stacktrace stack;
	std::ostringstream obuf;
	obuf << StackTraceFormatter(stack);
	std::string result = obuf.str();
	BOOST_CHECK_MESSAGE(!result.empty(), "stack trace must not be empty");
	size_t pos_a = result.find("stack_test_func_a");
	size_t pos_b = result.find("stack_test_func_b");
	BOOST_CHECK_MESSAGE(pos_a != std::string::npos, "'stack_test_func_a' not found\n\n" << result);
	BOOST_CHECK_MESSAGE(pos_b != std::string::npos, "'stack_test_func_b' not found\n\n" << result);
	BOOST_CHECK_MESSAGE(pos_a > pos_b, "'stack_test_func_a' must appear after 'stack_test_func_b'\n\n" << result);
}

[[gnu::noinline]]
void stack_test_func_a()
{
	boost::stacktrace::stacktrace stack;
	std::ostringstream obuf;
	obuf << StackTraceFormatter(stack);
	std::string result = obuf.str();
	BOOST_CHECK_MESSAGE(!result.empty(), "stack trace must not be empty");
	size_t pos_a = result.find("stack_test_func_a");
	BOOST_CHECK_MESSAGE(pos_a != std::string::npos, "'stack_test_func_a' not found\n\n" << result);

	stack_test_func_b();
}

BOOST_AUTO_TEST_CASE(stacktrace)
{
	stack_test_func_a();
}

BOOST_AUTO_TEST_SUITE_END()

#pragma GCC pop_options
#pragma clang optimize on
#ifdef _MSVC_VER
#pragma optimize("", on)
#endif /* _MSVC_VER */