icinga2/test/base-stacktrace.cpp

78 lines
2.7 KiB
C++
Raw Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2020 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#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.
*/
2025-10-17 09:05:22 -04:00
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC push_options
2025-10-17 09:05:22 -04:00
#pragma GCC optimize("O0")
#elif defined(__clang__)
#pragma clang optimize off
2025-10-17 09:05:22 -04:00
#elif defined(_MSC_VER)
#pragma optimize("", off)
2025-10-17 09:05:22 -04:00
#endif
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()
2025-10-17 09:05:22 -04:00
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC pop_options
2025-10-17 09:05:22 -04:00
#elif defined(__clang__)
#pragma clang optimize on
2025-10-17 09:05:22 -04:00
#elif defined(_MSC_VER)
#pragma optimize("", on)
2025-10-17 09:05:22 -04:00
#endif