71 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// 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 20using namespace llvm;21using namespace llvm::sys;22using testing::MatchesRegex;23using testing::Not;24 25#define TAG_BEGIN "\\{\\{\\{"26#define TAG_END "\\}\\}\\}"27// %p in the Symbolizer Markup Format spec28#define P_REGEX "(0+|0x[0-9a-fA-F]+)"29// %i in the Symbolizer Markup Format spec30#define I_REGEX "(0x[0-9a-fA-F]+|0[0-7]+|[0-9]+)"31 32#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && \33 (defined(__linux__) || defined(__FreeBSD__) || \34 defined(__FreeBSD_kernel__) || defined(__NetBSD__))35// Test relies on the binary this test is linked into having a GNU build ID36// note, which is not universally enabled by default (even when using Clang).37// Disable until we can reliably detect whether this is the case and skip it if38// not. See https://github.com/llvm/llvm-project/issues/168891.39#if 040TEST(SignalsTest, PrintsSymbolizerMarkup) {41 auto Exit =42 make_scope_exit([]() { unsetenv("LLVM_ENABLE_SYMBOLIZER_MARKUP"); });43 setenv("LLVM_ENABLE_SYMBOLIZER_MARKUP", "1", 1);44 std::string Res;45 raw_string_ostream RawStream(Res);46 PrintStackTrace(RawStream);47 EXPECT_THAT(Res, MatchesRegex(TAG_BEGIN "reset" TAG_END ".*"));48 // Module line for main binary49 EXPECT_THAT(Res,50 MatchesRegex(".*" TAG_BEGIN51 "module:0:[^:]*SupportTests:elf:[0-9a-f]+" TAG_END52 ".*"));53 // Text segment for main binary54 EXPECT_THAT(Res, MatchesRegex(".*" TAG_BEGIN "mmap:" P_REGEX ":" I_REGEX55 ":load:0:rx:" P_REGEX TAG_END ".*"));56 // Backtrace line57 EXPECT_THAT(Res, MatchesRegex(".*" TAG_BEGIN "bt:0:" P_REGEX ".*"));58}59#endif60 61TEST(SignalsTest, SymbolizerMarkupDisabled) {62 auto Exit = make_scope_exit([]() { unsetenv("LLVM_DISABLE_SYMBOLIZATION"); });63 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 1);64 std::string Res;65 raw_string_ostream RawStream(Res);66 PrintStackTrace(RawStream);67 EXPECT_THAT(Res, Not(MatchesRegex(TAG_BEGIN "reset" TAG_END ".*")));68}69 70#endif // defined(HAVE_BACKTRACE) && ...71