1 | //===-- llvm/unittest/Support/SignalsTest.cpp - Signals unit tests --------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
10 | /// This file contains unit tests for Signals.cpp and Signals.inc. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
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" |
19 | |
20 | using namespace llvm; |
21 | using namespace llvm::sys; |
22 | using testing::MatchesRegex; |
23 | using testing::Not; |
24 | |
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]+)" |
31 | |
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) { |
36 | auto Exit = |
37 | make_scope_exit(F: []() { unsetenv(name: "LLVM_ENABLE_SYMBOLIZER_MARKUP" ); }); |
38 | setenv(name: "LLVM_ENABLE_SYMBOLIZER_MARKUP" , value: "1" , replace: 1); |
39 | std::string Res; |
40 | raw_string_ostream RawStream(Res); |
41 | PrintStackTrace(OS&: RawStream); |
42 | EXPECT_THAT(Res, MatchesRegex(TAG_BEGIN "reset" TAG_END ".*" )); |
43 | // Module line for main binary |
44 | EXPECT_THAT(Res, |
45 | MatchesRegex(".*" TAG_BEGIN |
46 | "module:0:[^:]*SupportTests:elf:[0-9a-f]+" TAG_END |
47 | ".*" )); |
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 ".*" )); |
51 | // Backtrace line |
52 | EXPECT_THAT(Res, MatchesRegex(".*" TAG_BEGIN "bt:0:" P_REGEX ".*" )); |
53 | } |
54 | |
55 | TEST(SignalsTest, SymbolizerMarkupDisabled) { |
56 | auto Exit = make_scope_exit(F: []() { unsetenv(name: "LLVM_DISABLE_SYMBOLIZATION" ); }); |
57 | setenv(name: "LLVM_DISABLE_SYMBOLIZATION" , value: "1" , replace: 1); |
58 | std::string Res; |
59 | raw_string_ostream RawStream(Res); |
60 | PrintStackTrace(OS&: RawStream); |
61 | EXPECT_THAT(Res, Not(MatchesRegex(TAG_BEGIN "reset" TAG_END ".*" ))); |
62 | } |
63 | |
64 | #endif // defined(HAVE_BACKTRACE) && ... |
65 | |