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