1 //===-- llvm/unittest/Support/SignalsTest.cpp - Signals unit tests --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains unit tests for Signals.cpp and Signals.inc.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Signals.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/Config/config.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
21 using namespace llvm::sys
;
22 using testing::MatchesRegex
;
25 #define TAG_BEGIN "\\{\\{\\{"
26 #define TAG_END "\\}\\}\\}"
27 // %p in the Symbolizer Markup Format spec
28 #define P_REGEX "(0+|0x[0-9a-fA-F]+)"
29 // %i in the Symbolizer Markup Format spec
30 #define I_REGEX "(0x[0-9a-fA-F]+|0[0-7]+|[0-9]+)"
32 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H && \
33 (defined(__linux__) || defined(__FreeBSD__) || \
34 defined(__FreeBSD_kernel__) || defined(__NetBSD__))
35 TEST(SignalsTest
, PrintsSymbolizerMarkup
) {
37 make_scope_exit([]() { unsetenv("LLVM_ENABLE_SYMBOLIZER_MARKUP"); });
38 setenv("LLVM_ENABLE_SYMBOLIZER_MARKUP", "1", 1);
40 raw_string_ostream
RawStream(Res
);
41 PrintStackTrace(RawStream
);
42 EXPECT_THAT(Res
, MatchesRegex(TAG_BEGIN
"reset" TAG_END
".*"));
43 // Module line for main binary
45 MatchesRegex(".*" TAG_BEGIN
46 "module:0:[^:]*SupportTests:elf:[0-9a-f]+" TAG_END
48 // Text segment for main binary
49 EXPECT_THAT(Res
, MatchesRegex(".*" TAG_BEGIN
"mmap:" P_REGEX
":" I_REGEX
50 ":load:0:rx:" P_REGEX TAG_END
".*"));
52 EXPECT_THAT(Res
, MatchesRegex(".*" TAG_BEGIN
"bt:0:" P_REGEX
".*"));
55 TEST(SignalsTest
, SymbolizerMarkupDisabled
) {
56 auto Exit
= make_scope_exit([]() { unsetenv("LLVM_DISABLE_SYMBOLIZATION"); });
57 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 1);
59 raw_string_ostream
RawStream(Res
);
60 PrintStackTrace(RawStream
);
61 EXPECT_THAT(Res
, Not(MatchesRegex(TAG_BEGIN
"reset" TAG_END
".*")));
64 #endif // defined(HAVE_BACKTRACE) && ...