xref: /llvm-project/llvm/unittests/Support/SignalsTest.cpp (revision 7c06786479311fbf19f54c7980c6e96936fa05a8)
122b9404fSDaniel Thornburgh //===-- llvm/unittest/Support/SignalsTest.cpp - Signals unit tests --------===//
222b9404fSDaniel Thornburgh //
322b9404fSDaniel Thornburgh // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422b9404fSDaniel Thornburgh // See https://llvm.org/LICENSE.txt for license information.
522b9404fSDaniel Thornburgh // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
622b9404fSDaniel Thornburgh //
722b9404fSDaniel Thornburgh //===----------------------------------------------------------------------===//
822b9404fSDaniel Thornburgh ///
922b9404fSDaniel Thornburgh /// \file
1022b9404fSDaniel Thornburgh /// This file contains unit tests for Signals.cpp and Signals.inc.
1122b9404fSDaniel Thornburgh ///
1222b9404fSDaniel Thornburgh //===----------------------------------------------------------------------===//
1322b9404fSDaniel Thornburgh 
1422b9404fSDaniel Thornburgh #include "llvm/Support/Signals.h"
1522b9404fSDaniel Thornburgh #include "llvm/ADT/ScopeExit.h"
1622b9404fSDaniel Thornburgh #include "llvm/Config/config.h"
1722b9404fSDaniel Thornburgh #include "gmock/gmock.h"
1822b9404fSDaniel Thornburgh #include "gtest/gtest.h"
1922b9404fSDaniel Thornburgh 
2022b9404fSDaniel Thornburgh using namespace llvm;
2122b9404fSDaniel Thornburgh using namespace llvm::sys;
2222b9404fSDaniel Thornburgh using testing::MatchesRegex;
2322b9404fSDaniel Thornburgh using testing::Not;
2422b9404fSDaniel Thornburgh 
2522b9404fSDaniel Thornburgh #define TAG_BEGIN "\\{\\{\\{"
2622b9404fSDaniel Thornburgh #define TAG_END "\\}\\}\\}"
278d3ff601SDaniel Thornburgh // %p in the Symbolizer Markup Format spec
288d3ff601SDaniel Thornburgh #define P_REGEX "(0+|0x[0-9a-fA-F]+)"
298d3ff601SDaniel Thornburgh // %i in the Symbolizer Markup Format spec
308d3ff601SDaniel Thornburgh #define I_REGEX "(0x[0-9a-fA-F]+|0[0-7]+|[0-9]+)"
3122b9404fSDaniel Thornburgh 
32*7c067864SFangrui Song #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES &&                            \
3322b9404fSDaniel Thornburgh     (defined(__linux__) || defined(__FreeBSD__) ||                             \
3422b9404fSDaniel Thornburgh      defined(__FreeBSD_kernel__) || defined(__NetBSD__))
3522b9404fSDaniel Thornburgh TEST(SignalsTest, PrintsSymbolizerMarkup) {
3622b9404fSDaniel Thornburgh   auto Exit =
3722b9404fSDaniel Thornburgh       make_scope_exit([]() { unsetenv("LLVM_ENABLE_SYMBOLIZER_MARKUP"); });
3822b9404fSDaniel Thornburgh   setenv("LLVM_ENABLE_SYMBOLIZER_MARKUP", "1", 1);
3922b9404fSDaniel Thornburgh   std::string Res;
4022b9404fSDaniel Thornburgh   raw_string_ostream RawStream(Res);
4122b9404fSDaniel Thornburgh   PrintStackTrace(RawStream);
4222b9404fSDaniel Thornburgh   EXPECT_THAT(Res, MatchesRegex(TAG_BEGIN "reset" TAG_END ".*"));
4322b9404fSDaniel Thornburgh   // Module line for main binary
4422b9404fSDaniel Thornburgh   EXPECT_THAT(Res,
4522b9404fSDaniel Thornburgh               MatchesRegex(".*" TAG_BEGIN
4622b9404fSDaniel Thornburgh                            "module:0:[^:]*SupportTests:elf:[0-9a-f]+" TAG_END
4722b9404fSDaniel Thornburgh                            ".*"));
4822b9404fSDaniel Thornburgh   // Text segment for main binary
498d3ff601SDaniel Thornburgh   EXPECT_THAT(Res, MatchesRegex(".*" TAG_BEGIN "mmap:" P_REGEX ":" I_REGEX
508d3ff601SDaniel Thornburgh                                 ":load:0:rx:" P_REGEX TAG_END ".*"));
5122b9404fSDaniel Thornburgh   // Backtrace line
528d3ff601SDaniel Thornburgh   EXPECT_THAT(Res, MatchesRegex(".*" TAG_BEGIN "bt:0:" P_REGEX ".*"));
5322b9404fSDaniel Thornburgh }
5422b9404fSDaniel Thornburgh 
5522b9404fSDaniel Thornburgh TEST(SignalsTest, SymbolizerMarkupDisabled) {
5622b9404fSDaniel Thornburgh   auto Exit = make_scope_exit([]() { unsetenv("LLVM_DISABLE_SYMBOLIZATION"); });
5722b9404fSDaniel Thornburgh   setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 1);
5822b9404fSDaniel Thornburgh   std::string Res;
5922b9404fSDaniel Thornburgh   raw_string_ostream RawStream(Res);
6022b9404fSDaniel Thornburgh   PrintStackTrace(RawStream);
6122b9404fSDaniel Thornburgh   EXPECT_THAT(Res, Not(MatchesRegex(TAG_BEGIN "reset" TAG_END ".*")));
6222b9404fSDaniel Thornburgh }
6322b9404fSDaniel Thornburgh 
6422b9404fSDaniel Thornburgh #endif // defined(HAVE_BACKTRACE) && ...
65