LLVM  9.0.0svn
Signals.cpp
Go to the documentation of this file.
1 //===- Signals.cpp - Signal Handling support --------------------*- C++ -*-===//
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 // This file defines some helpful functions for dealing with the possibility of
10 // Unix signals occurring while your program is running.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Signals.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/Format.h"
26 #include "llvm/Support/Mutex.h"
27 #include "llvm/Support/Program.h"
30 #include "llvm/Support/Options.h"
31 #include <vector>
32 
33 //===----------------------------------------------------------------------===//
34 //=== WARNING: Implementation here must contain only TRULY operating system
35 //=== independent code.
36 //===----------------------------------------------------------------------===//
37 
38 using namespace llvm;
39 
40 // Use explicit storage to avoid accessing cl::opt in a signal handler.
41 static bool DisableSymbolicationFlag = false;
43  DisableSymbolication("disable-symbolication",
44  cl::desc("Disable symbolizing crash backtraces."),
45  cl::location(DisableSymbolicationFlag), cl::Hidden);
46 
47 // Callbacks to run in signal handler must be lock-free because a signal handler
48 // could be running as we add new callbacks. We don't add unbounded numbers of
49 // callbacks, an array is therefore sufficient.
52  void *Cookie;
53  enum class Status { Empty, Initializing, Initialized, Executing };
54  std::atomic<Status> Flag;
55 };
56 static constexpr size_t MaxSignalHandlerCallbacks = 8;
58 
59 // Signal-safe.
61  for (size_t I = 0; I < MaxSignalHandlerCallbacks; ++I) {
62  auto &RunMe = CallBacksToRun[I];
65  if (!RunMe.Flag.compare_exchange_strong(Expected, Desired))
66  continue;
67  (*RunMe.Callback)(RunMe.Cookie);
68  RunMe.Callback = nullptr;
69  RunMe.Cookie = nullptr;
70  RunMe.Flag.store(CallbackAndCookie::Status::Empty);
71  }
72 }
73 
74 // Signal-safe.
76  void *Cookie) {
77  for (size_t I = 0; I < MaxSignalHandlerCallbacks; ++I) {
78  auto &SetMe = CallBacksToRun[I];
81  if (!SetMe.Flag.compare_exchange_strong(Expected, Desired))
82  continue;
83  SetMe.Callback = FnPtr;
84  SetMe.Cookie = Cookie;
86  return;
87  }
88  report_fatal_error("too many signal callbacks already registered");
89 }
90 
91 static bool findModulesAndOffsets(void **StackTrace, int Depth,
92  const char **Modules, intptr_t *Offsets,
93  const char *MainExecutableName,
94  StringSaver &StrPool);
95 
96 /// Format a pointer value as hexadecimal. Zero pad it out so its always the
97 /// same width.
98 static FormattedNumber format_ptr(void *PC) {
99  // Each byte is two hex digits plus 2 for the 0x prefix.
100  unsigned PtrWidth = 2 + 2 * sizeof(void *);
101  return format_hex((uint64_t)PC, PtrWidth);
102 }
103 
104 /// Helper that launches llvm-symbolizer and symbolizes a backtrace.
106 static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace,
107  int Depth, llvm::raw_ostream &OS) {
108  if (DisableSymbolicationFlag)
109  return false;
110 
111  // Don't recursively invoke the llvm-symbolizer binary.
112  if (Argv0.find("llvm-symbolizer") != std::string::npos)
113  return false;
114 
115  // FIXME: Subtract necessary number from StackTrace entries to turn return addresses
116  // into actual instruction addresses.
117  // Use llvm-symbolizer tool to symbolize the stack traces. First look for it
118  // alongside our binary, then in $PATH.
119  ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code();
120  if (!Argv0.empty()) {
121  StringRef Parent = llvm::sys::path::parent_path(Argv0);
122  if (!Parent.empty())
123  LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent);
124  }
125  if (!LLVMSymbolizerPathOrErr)
126  LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer");
127  if (!LLVMSymbolizerPathOrErr)
128  return false;
129  const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
130 
131  // If we don't know argv0 or the address of main() at this point, try
132  // to guess it anyway (it's possible on some platforms).
133  std::string MainExecutableName =
134  sys::fs::exists(Argv0) ? (std::string)Argv0
135  : sys::fs::getMainExecutable(nullptr, nullptr);
137  StringSaver StrPool(Allocator);
138  std::vector<const char *> Modules(Depth, nullptr);
139  std::vector<intptr_t> Offsets(Depth, 0);
140  if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(),
141  MainExecutableName.c_str(), StrPool))
142  return false;
143  int InputFD;
144  SmallString<32> InputFile, OutputFile;
145  sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile);
146  sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile);
147  FileRemover InputRemover(InputFile.c_str());
148  FileRemover OutputRemover(OutputFile.c_str());
149 
150  {
151  raw_fd_ostream Input(InputFD, true);
152  for (int i = 0; i < Depth; i++) {
153  if (Modules[i])
154  Input << Modules[i] << " " << (void*)Offsets[i] << "\n";
155  }
156  }
157 
158  Optional<StringRef> Redirects[] = {StringRef(InputFile),
159  StringRef(OutputFile), StringRef("")};
160  StringRef Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining",
161 #ifdef _WIN32
162  // Pass --relative-address on Windows so that we don't
163  // have to add ImageBase from PE file.
164  // FIXME: Make this the default for llvm-symbolizer.
165  "--relative-address",
166 #endif
167  "--demangle"};
168  int RunResult =
169  sys::ExecuteAndWait(LLVMSymbolizerPath, Args, None, Redirects);
170  if (RunResult != 0)
171  return false;
172 
173  // This report format is based on the sanitizer stack trace printer. See
174  // sanitizer_stacktrace_printer.cc in compiler-rt.
175  auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str());
176  if (!OutputBuf)
177  return false;
178  StringRef Output = OutputBuf.get()->getBuffer();
180  Output.split(Lines, "\n");
181  auto CurLine = Lines.begin();
182  int frame_no = 0;
183  for (int i = 0; i < Depth; i++) {
184  auto PrintLineHeader = [&]() {
185  OS << right_justify(formatv("#{0}", frame_no++).str(),
186  std::log10(Depth) + 2)
187  << ' ' << format_ptr(StackTrace[i]) << ' ';
188  };
189  if (!Modules[i]) {
190  PrintLineHeader();
191  OS << '\n';
192  continue;
193  }
194  // Read pairs of lines (function name and file/line info) until we
195  // encounter empty line.
196  for (;;) {
197  if (CurLine == Lines.end())
198  return false;
199  StringRef FunctionName = *CurLine++;
200  if (FunctionName.empty())
201  break;
202  PrintLineHeader();
203  if (!FunctionName.startswith("??"))
204  OS << FunctionName << ' ';
205  if (CurLine == Lines.end())
206  return false;
207  StringRef FileLineInfo = *CurLine++;
208  if (!FileLineInfo.startswith("??"))
209  OS << FileLineInfo;
210  else
211  OS << "(" << Modules[i] << '+' << format_hex(Offsets[i], 0) << ")";
212  OS << "\n";
213  }
214  }
215  return true;
216 }
217 
218 // Include the platform-specific parts of this class.
219 #ifdef LLVM_ON_UNIX
220 #include "Unix/Signals.inc"
221 #endif
222 #ifdef _WIN32
223 #include "Windows/Signals.inc"
224 #endif
Represents either an error or a value T.
Definition: ErrorOr.h:56
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:185
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
void(*)(void *) SignalHandlerCallback
Definition: Signals.h:58
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
sys::SignalHandlerCallback Callback
Definition: Signals.cpp:51
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:1094
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
static cl::opt< bool, true > DisableSymbolication("disable-symbolication", cl::desc("Disable symbolizing crash backtraces."), cl::location(DisableSymbolicationFlag), cl::Hidden)
int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env=None, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:30
static bool findModulesAndOffsets(void **StackTrace, int Depth, const char **Modules, intptr_t *Offsets, const char *MainExecutableName, StringSaver &StrPool)
FileRemover - This class is a simple object meant to be stack allocated.
Definition: FileUtilities.h:39
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
static bool DisableSymbolicationFlag
Definition: Signals.cpp:41
FormattedString right_justify(StringRef Str, unsigned Width)
right_justify - add spaces before string so total output is Width characters.
Definition: Format.h:151
std::string getMainExecutable(const char *argv0, void *MainExecAddr)
Return the path to the main executable, given the value of argv[0] from program startup and the addre...
void RunSignalHandlers()
Definition: Signals.cpp:60
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:285
std::atomic< Status > Flag
Definition: Signals.cpp:54
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:466
static LLVM_ATTRIBUTE_USED bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace, int Depth, llvm::raw_ostream &OS)
Helper that launches llvm-symbolizer and symbolizes a backtrace.
Definition: Signals.cpp:106
Basic Register Allocator
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:163
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:696
This file declares helper objects for defining debug options that can be configured via the command l...
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Definition: StringSaver.h:21
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:365
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
Definition: Path.cpp:812
const char * c_str()
Definition: SmallString.h:269
#define I(x, y, z)
Definition: MD5.cpp:58
static CallbackAndCookie CallBacksToRun[MaxSignalHandlerCallbacks]
Definition: Signals.cpp:57
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
static constexpr size_t MaxSignalHandlerCallbacks
Definition: Signals.cpp:56
static void insertSignalHandler(sys::SignalHandlerCallback FnPtr, void *Cookie)
Definition: Signals.cpp:75
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static FormattedNumber format_ptr(void *PC)
Format a pointer value as hexadecimal.
Definition: Signals.cpp:98
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1025
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:448
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:119