LLVM 22.0.0git
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
15
16#include "DebugOptions.h"
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Config/llvm-config.h"
24#include "llvm/Support/Format.h"
29#include "llvm/Support/Path.h"
33#include <array>
34#include <cmath>
35
36//===----------------------------------------------------------------------===//
37//=== WARNING: Implementation here must contain only TRULY operating system
38//=== independent code.
39//===----------------------------------------------------------------------===//
40
41using namespace llvm;
42
43// Use explicit storage to avoid accessing cl::opt in a signal handler.
44static bool DisableSymbolicationFlag = false;
46namespace {
47struct CreateDisableSymbolication {
48 static void *call() {
49 return new cl::opt<bool, true>(
50 "disable-symbolication",
51 cl::desc("Disable symbolizing crash backtraces."),
53 }
54};
55struct CreateCrashDiagnosticsDir {
56 static void *call() {
58 "crash-diagnostics-dir", cl::value_desc("directory"),
59 cl::desc("Directory for crash diagnostic files."),
61 }
62};
63} // namespace
65 static ManagedStatic<cl::opt<bool, true>, CreateDisableSymbolication>
66 DisableSymbolication;
67 static ManagedStatic<cl::opt<std::string, true>, CreateCrashDiagnosticsDir>
68 CrashDiagnosticsDir;
69 *DisableSymbolication;
70 *CrashDiagnosticsDir;
71}
72
73constexpr char DisableSymbolizationEnv[] = "LLVM_DISABLE_SYMBOLIZATION";
74constexpr char LLVMSymbolizerPathEnv[] = "LLVM_SYMBOLIZER_PATH";
75constexpr char EnableSymbolizerMarkupEnv[] = "LLVM_ENABLE_SYMBOLIZER_MARKUP";
76
77// Callbacks to run in signal handler must be lock-free because a signal handler
78// could be running as we add new callbacks. We don't add unbounded numbers of
79// callbacks, an array is therefore sufficient.
86
87static constexpr size_t MaxSignalHandlerCallbacks = 8;
88
89// A global array of CallbackAndCookie may not compile with
90// -Werror=global-constructors in c++20 and above
91static std::array<CallbackAndCookie, MaxSignalHandlerCallbacks> &
93 static std::array<CallbackAndCookie, MaxSignalHandlerCallbacks> callbacks;
94 return callbacks;
95}
96
97// Signal-safe.
99 // Let's not interfere with stack trace symbolication and friends.
100 auto BypassSandbox = sandbox::scopedDisable();
101
102 for (CallbackAndCookie &RunMe : CallBacksToRun()) {
105 if (!RunMe.Flag.compare_exchange_strong(Expected, Desired))
106 continue;
107 (*RunMe.Callback)(RunMe.Cookie);
108 RunMe.Callback = nullptr;
109 RunMe.Cookie = nullptr;
110 RunMe.Flag.store(CallbackAndCookie::Status::Empty);
111 }
112}
113
114// Signal-safe.
116 void *Cookie) {
117 for (CallbackAndCookie &SetMe : CallBacksToRun()) {
120 if (!SetMe.Flag.compare_exchange_strong(Expected, Desired))
121 continue;
122 SetMe.Callback = FnPtr;
123 SetMe.Cookie = Cookie;
125 return;
126 }
127 report_fatal_error("too many signal callbacks already registered");
128}
129
130static bool findModulesAndOffsets(void **StackTrace, int Depth,
131 const char **Modules, intptr_t *Offsets,
132 const char *MainExecutableName,
133 StringSaver &StrPool);
134
135/// Format a pointer value as hexadecimal. Zero pad it out so its always the
136/// same width.
137static FormattedNumber format_ptr(void *PC) {
138 // Each byte is two hex digits plus 2 for the 0x prefix.
139 unsigned PtrWidth = 2 + 2 * sizeof(void *);
140 return format_hex((uint64_t)PC, PtrWidth);
141}
142
143/// Reads a file \p Filename written by llvm-symbolizer containing function
144/// names and source locations for the addresses in \p AddressList and returns
145/// the strings in a vector of pairs, where the first pair element is the index
146/// of the corresponding entry in AddressList and the second is the symbolized
147/// frame, in a format based on the sanitizer stack trace printer, with the
148/// exception that it does not write out frame numbers (i.e. "#2 " for the
149/// third address), as it is not assumed that \p AddressList corresponds to a
150/// single stack trace.
151/// There may be multiple returned entries for a single \p AddressList entry if
152/// that frame address corresponds to one or more inlined frames; in this case,
153/// all frames for an address will appear contiguously and in-order.
154std::optional<SmallVector<std::pair<unsigned, std::string>, 0>>
155collectAddressSymbols(void **AddressList, unsigned AddressCount,
156 const char *MainExecutableName,
157 const std::string &LLVMSymbolizerPath) {
159 StringSaver StrPool(Allocator);
160 SmallVector<const char *, 0> Modules(AddressCount, nullptr);
161 SmallVector<intptr_t, 0> Offsets(AddressCount, 0);
162 if (!findModulesAndOffsets(AddressList, AddressCount, Modules.data(),
163 Offsets.data(), MainExecutableName, StrPool))
164 return {};
165 int InputFD;
166 SmallString<32> InputFile, OutputFile;
167 sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile);
168 sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile);
169 FileRemover InputRemover(InputFile.c_str());
170 FileRemover OutputRemover(OutputFile.c_str());
171
172 {
173 raw_fd_ostream Input(InputFD, true);
174 for (unsigned AddrIdx = 0; AddrIdx < AddressCount; AddrIdx++) {
175 if (Modules[AddrIdx])
176 Input << Modules[AddrIdx] << " " << (void *)Offsets[AddrIdx] << "\n";
177 }
178 }
179
180 std::optional<StringRef> Redirects[] = {InputFile.str(), OutputFile.str(),
181 StringRef("")};
182 StringRef Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining",
183#ifdef _WIN32
184 // Pass --relative-address on Windows so that we don't
185 // have to add ImageBase from PE file.
186 // FIXME: Make this the default for llvm-symbolizer.
187 "--relative-address",
188#endif
189 "--demangle"};
190 int RunResult =
191 sys::ExecuteAndWait(LLVMSymbolizerPath, Args, std::nullopt, Redirects);
192 if (RunResult != 0)
193 return {};
194
196 auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str());
197 if (!OutputBuf)
198 return {};
199 StringRef Output = OutputBuf.get()->getBuffer();
201 Output.split(Lines, "\n");
202 auto *CurLine = Lines.begin();
203 // Lines contains the output from llvm-symbolizer, which should contain for
204 // each address with a module in order of appearance, one or more lines
205 // containing the function name and line associated with that address,
206 // followed by an empty line.
207 // For each address, adds an output entry for every real or inlined frame at
208 // that address. For addresses without known modules, we have a single entry
209 // containing just the formatted address; for all other output entries, we
210 // output the function entry if it is known, and either the line number if it
211 // is known or the module+address offset otherwise.
212 for (unsigned AddrIdx = 0; AddrIdx < AddressCount; AddrIdx++) {
213 if (!Modules[AddrIdx]) {
214 auto &SymbolizedFrame = Result.emplace_back(std::make_pair(AddrIdx, ""));
215 raw_string_ostream OS(SymbolizedFrame.second);
216 OS << format_ptr(AddressList[AddrIdx]);
217 continue;
218 }
219 // Read pairs of lines (function name and file/line info) until we
220 // encounter empty line.
221 for (;;) {
222 if (CurLine == Lines.end())
223 return {};
224 StringRef FunctionName = *CurLine++;
225 if (FunctionName.empty())
226 break;
227 auto &SymbolizedFrame = Result.emplace_back(std::make_pair(AddrIdx, ""));
228 raw_string_ostream OS(SymbolizedFrame.second);
229 OS << format_ptr(AddressList[AddrIdx]) << ' ';
230 if (!FunctionName.starts_with("??"))
231 OS << FunctionName << ' ';
232 if (CurLine == Lines.end())
233 return {};
234 StringRef FileLineInfo = *CurLine++;
235 if (!FileLineInfo.starts_with("??")) {
236 OS << FileLineInfo;
237 } else {
238 OS << "(" << Modules[AddrIdx] << '+' << format_hex(Offsets[AddrIdx], 0)
239 << ")";
240 }
241 }
242 }
243 return Result;
244}
245
247 ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code();
248 if (const char *Path = getenv(LLVMSymbolizerPathEnv)) {
249 LLVMSymbolizerPathOrErr = sys::findProgramByName(Path);
250 } else if (!Argv0.empty()) {
252 if (!Parent.empty())
253 LLVMSymbolizerPathOrErr =
254 sys::findProgramByName("llvm-symbolizer", Parent);
255 }
256 if (!LLVMSymbolizerPathOrErr)
257 LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer");
258 return LLVMSymbolizerPathOrErr;
259}
260
261/// Helper that launches llvm-symbolizer and symbolizes a backtrace.
263static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace,
264 int Depth, llvm::raw_ostream &OS) {
266 return false;
267
268 // Don't recursively invoke the llvm-symbolizer binary.
269 if (Argv0.contains("llvm-symbolizer"))
270 return false;
271
272 // FIXME: Subtract necessary number from StackTrace entries to turn return
273 // addresses into actual instruction addresses.
274 // Use llvm-symbolizer tool to symbolize the stack traces. First look for it
275 // alongside our binary, then in $PATH.
276 ErrorOr<std::string> LLVMSymbolizerPathOrErr = getLLVMSymbolizerPath(Argv0);
277 if (!LLVMSymbolizerPathOrErr)
278 return false;
279 const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
280
281 // If we don't know argv0 or the address of main() at this point, try
282 // to guess it anyway (it's possible on some platforms).
283 std::string MainExecutableName =
284 sys::fs::exists(Argv0) ? std::string(Argv0)
285 : sys::fs::getMainExecutable(nullptr, nullptr);
286
287 auto SymbolizedAddressesOpt = collectAddressSymbols(
288 StackTrace, Depth, MainExecutableName.c_str(), LLVMSymbolizerPath);
289 if (!SymbolizedAddressesOpt)
290 return false;
291 for (unsigned FrameNo = 0; FrameNo < SymbolizedAddressesOpt->size();
292 ++FrameNo) {
293 OS << right_justify(formatv("#{0}", FrameNo).str(), std::log10(Depth) + 2)
294 << ' ' << (*SymbolizedAddressesOpt)[FrameNo].second << '\n';
295 }
296 return true;
297}
298
299#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
300void sys::symbolizeAddresses(AddressSet &Addresses,
301 SymbolizedAddressMap &SymbolizedAddresses) {
303 "Debugify origin stacktraces require symbolization to be enabled.");
304
305 // Convert Set of Addresses to ordered list.
306 SmallVector<void *, 0> AddressList(Addresses.begin(), Addresses.end());
307 if (AddressList.empty())
308 return;
309 llvm::sort(AddressList);
310
311 // Use llvm-symbolizer tool to symbolize the stack traces. First look for it
312 // alongside our binary, then in $PATH.
313 ErrorOr<std::string> LLVMSymbolizerPathOrErr = getLLVMSymbolizerPath();
314 if (!LLVMSymbolizerPathOrErr)
315 report_fatal_error("Debugify origin stacktraces require llvm-symbolizer");
316 const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
317
318 // Try to guess the main executable name, since we don't have argv0 available
319 // here.
320 std::string MainExecutableName = sys::fs::getMainExecutable(nullptr, nullptr);
321
322 auto SymbolizedAddressesOpt =
323 collectAddressSymbols(AddressList.begin(), AddressList.size(),
324 MainExecutableName.c_str(), LLVMSymbolizerPath);
325 if (!SymbolizedAddressesOpt)
326 return;
327 for (auto SymbolizedFrame : *SymbolizedAddressesOpt) {
328 SmallVector<std::string, 0> &SymbolizedAddrs =
329 SymbolizedAddresses[AddressList[SymbolizedFrame.first]];
330 SymbolizedAddrs.push_back(SymbolizedFrame.second);
331 }
332 return;
333}
334#endif
335
336static bool printMarkupContext(raw_ostream &OS, const char *MainExecutableName);
337
339static bool printMarkupStackTrace(StringRef Argv0, void **StackTrace, int Depth,
340 raw_ostream &OS) {
341 const char *Env = getenv(EnableSymbolizerMarkupEnv);
342 if (!Env || !*Env)
343 return false;
344
345 std::string MainExecutableName =
346 sys::fs::exists(Argv0) ? std::string(Argv0)
347 : sys::fs::getMainExecutable(nullptr, nullptr);
348 if (!printMarkupContext(OS, MainExecutableName.c_str()))
349 return false;
350 for (int I = 0; I < Depth; I++)
351 OS << format("{{{bt:%d:%#016x}}}\n", I, StackTrace[I]);
352 return true;
353}
354
355// Include the platform-specific parts of this class.
356#ifdef LLVM_ON_UNIX
357#include "Unix/Signals.inc"
358#endif
359#ifdef _WIN32
360#include "Windows/Signals.inc"
361#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ATTRIBUTE_USED
Definition Compiler.h:236
Provides ErrorOr<T> smart pointer.
#define I(x, y, z)
Definition MD5.cpp:57
Basic Register Allocator
static FormattedNumber format_ptr(void *PC)
Format a pointer value as hexadecimal.
Definition Signals.cpp:137
constexpr char DisableSymbolizationEnv[]
Definition Signals.cpp:73
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:263
static std::array< CallbackAndCookie, MaxSignalHandlerCallbacks > & CallBacksToRun()
Definition Signals.cpp:92
static bool findModulesAndOffsets(void **StackTrace, int Depth, const char **Modules, intptr_t *Offsets, const char *MainExecutableName, StringSaver &StrPool)
static bool DisableSymbolicationFlag
Definition Signals.cpp:44
static ManagedStatic< std::string > CrashDiagnosticsDirectory
Definition Signals.cpp:45
ErrorOr< std::string > getLLVMSymbolizerPath(StringRef Argv0={})
Definition Signals.cpp:246
static constexpr size_t MaxSignalHandlerCallbacks
Definition Signals.cpp:87
constexpr char LLVMSymbolizerPathEnv[]
Definition Signals.cpp:74
static bool printMarkupContext(raw_ostream &OS, const char *MainExecutableName)
std::optional< SmallVector< std::pair< unsigned, std::string >, 0 > > collectAddressSymbols(void **AddressList, unsigned AddressCount, const char *MainExecutableName, const std::string &LLVMSymbolizerPath)
Reads a file Filename written by llvm-symbolizer containing function names and source locations for t...
Definition Signals.cpp:155
static LLVM_ATTRIBUTE_USED bool printMarkupStackTrace(StringRef Argv0, void **StackTrace, int Depth, raw_ostream &OS)
Definition Signals.cpp:339
static void insertSignalHandler(sys::SignalHandlerCallback FnPtr, void *Cookie)
Definition Signals.cpp:115
constexpr char EnableSymbolizerMarkupEnv[]
Definition Signals.cpp:75
The Input class is used to parse a yaml document into in-memory structs and vectors.
Represents either an error or a value T.
Definition ErrorOr.h:56
Tagged union holding either a T or a Error.
Definition Error.h:485
FileRemover - This class is a simple object meant to be stack allocated.
This is a helper class used for format_hex() and format_decimal().
Definition Format.h:169
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
const char * c_str()
StringRef str() const
Explicit conversion to StringRef.
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:702
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:426
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
LocationClass< Ty > location(Ty &L)
LLVM_ABI bool exists(const basic_file_status &status)
Does file exist?
Definition Path.cpp:1086
LLVM_ABI 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...
LLVM_ABI std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None)
Create a file in the system temporary directory.
Definition Path.cpp:915
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:468
ScopedSetting scopedDisable()
Definition IOSandbox.h:36
LLVM_ABI ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
LLVM_ABI void RunSignalHandlers()
Definition Signals.cpp:98
LLVM_ABI int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition Program.cpp:32
void(*)(void *) SignalHandlerCallback
Definition Signals.h:98
This is an optimization pass for GlobalISel generic memory operations.
FormattedString right_justify(StringRef Str, unsigned Width)
right_justify - add spaces before string so total output is Width characters.
Definition Format.h:157
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
void initSignalsOptions()
Definition Signals.cpp:64
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:191
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
sys::SignalHandlerCallback Callback
Definition Signals.cpp:81
std::atomic< Status > Flag
Definition Signals.cpp:84