Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 1 | //===- Signals.cpp - Signal Handling support --------------------*- C++ -*-===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines some helpful functions for dealing with the possibility of |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 11 | // Unix signals occurring while your program is running. |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Signals.h" |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 18 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorOr.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | #include "llvm/Support/FileUtilities.h" |
| 22 | #include "llvm/Support/Format.h" |
Yaron Keren | 240bd9c | 2015-07-22 19:01:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/Mutex.h" |
| 26 | #include "llvm/Support/Program.h" |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/StringSaver.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 4a60d37 | 2017-06-09 07:29:03 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Options.h" |
Yaron Keren | 240bd9c | 2015-07-22 19:01:14 +0000 | [diff] [blame] | 30 | #include <vector> |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 34 | //=== independent code. |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | |
David Blaikie | 4a60d37 | 2017-06-09 07:29:03 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 39 | // Use explicit storage to avoid accessing cl::opt in a signal handler. |
| 40 | static bool DisableSymbolicationFlag = false; |
| 41 | static cl::opt<bool, true> |
David Blaikie | 4a60d37 | 2017-06-09 07:29:03 +0000 | [diff] [blame] | 42 | DisableSymbolication("disable-symbolication", |
| 43 | cl::desc("Disable symbolizing crash backtraces."), |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 44 | cl::location(DisableSymbolicationFlag), cl::Hidden); |
David Blaikie | 4a60d37 | 2017-06-09 07:29:03 +0000 | [diff] [blame] | 45 | |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 46 | // Callbacks to run in signal handler must be lock-free because a signal handler |
| 47 | // could be running as we add new callbacks. We don't add unbounded numbers of |
| 48 | // callbacks, an array is therefore sufficient. |
| 49 | struct CallbackAndCookie { |
| 50 | sys::SignalHandlerCallback Callback; |
| 51 | void *Cookie; |
| 52 | enum class Status { Empty, Initializing, Initialized, Executing }; |
| 53 | std::atomic<Status> Flag; |
| 54 | }; |
| 55 | static constexpr size_t MaxSignalHandlerCallbacks = 8; |
| 56 | static CallbackAndCookie CallBacksToRun[MaxSignalHandlerCallbacks]; |
| 57 | |
| 58 | // Signal-safe. |
JF Bastien | b8931c1 | 2018-05-16 04:36:37 +0000 | [diff] [blame] | 59 | void sys::RunSignalHandlers() { |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 60 | for (size_t I = 0; I < MaxSignalHandlerCallbacks; ++I) { |
| 61 | auto &RunMe = CallBacksToRun[I]; |
| 62 | auto Expected = CallbackAndCookie::Status::Initialized; |
| 63 | auto Desired = CallbackAndCookie::Status::Executing; |
| 64 | if (!RunMe.Flag.compare_exchange_strong(Expected, Desired)) |
| 65 | continue; |
| 66 | (*RunMe.Callback)(RunMe.Cookie); |
| 67 | RunMe.Callback = nullptr; |
| 68 | RunMe.Cookie = nullptr; |
| 69 | RunMe.Flag.store(CallbackAndCookie::Status::Empty); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Signal-safe. |
| 74 | static void insertSignalHandler(sys::SignalHandlerCallback FnPtr, |
| 75 | void *Cookie) { |
| 76 | for (size_t I = 0; I < MaxSignalHandlerCallbacks; ++I) { |
| 77 | auto &SetMe = CallBacksToRun[I]; |
| 78 | auto Expected = CallbackAndCookie::Status::Empty; |
| 79 | auto Desired = CallbackAndCookie::Status::Initializing; |
| 80 | if (!SetMe.Flag.compare_exchange_strong(Expected, Desired)) |
| 81 | continue; |
| 82 | SetMe.Callback = FnPtr; |
| 83 | SetMe.Cookie = Cookie; |
| 84 | SetMe.Flag.store(CallbackAndCookie::Status::Initialized); |
JF Bastien | b8931c1 | 2018-05-16 04:36:37 +0000 | [diff] [blame] | 85 | return; |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 86 | } |
| 87 | report_fatal_error("too many signal callbacks already registered"); |
Yaron Keren | 240bd9c | 2015-07-22 19:01:14 +0000 | [diff] [blame] | 88 | } |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 89 | |
| 90 | static bool findModulesAndOffsets(void **StackTrace, int Depth, |
| 91 | const char **Modules, intptr_t *Offsets, |
| 92 | const char *MainExecutableName, |
| 93 | StringSaver &StrPool); |
| 94 | |
| 95 | /// Format a pointer value as hexadecimal. Zero pad it out so its always the |
| 96 | /// same width. |
| 97 | static FormattedNumber format_ptr(void *PC) { |
| 98 | // Each byte is two hex digits plus 2 for the 0x prefix. |
| 99 | unsigned PtrWidth = 2 + 2 * sizeof(void *); |
| 100 | return format_hex((uint64_t)PC, PtrWidth); |
| 101 | } |
| 102 | |
| 103 | /// Helper that launches llvm-symbolizer and symbolizes a backtrace. |
Fangrui Song | 862eebb | 2018-05-05 20:14:38 +0000 | [diff] [blame] | 104 | LLVM_ATTRIBUTE_USED |
| 105 | static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace, |
| 106 | int Depth, llvm::raw_ostream &OS) { |
JF Bastien | aa1333a | 2018-05-16 17:25:35 +0000 | [diff] [blame] | 107 | if (DisableSymbolicationFlag) |
David Blaikie | 4a60d37 | 2017-06-09 07:29:03 +0000 | [diff] [blame] | 108 | return false; |
| 109 | |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 110 | // Don't recursively invoke the llvm-symbolizer binary. |
| 111 | if (Argv0.find("llvm-symbolizer") != std::string::npos) |
| 112 | return false; |
| 113 | |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 114 | // FIXME: Subtract necessary number from StackTrace entries to turn return addresses |
| 115 | // into actual instruction addresses. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 116 | // Use llvm-symbolizer tool to symbolize the stack traces. First look for it |
| 117 | // alongside our binary, then in $PATH. |
| 118 | ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code(); |
| 119 | if (!Argv0.empty()) { |
| 120 | StringRef Parent = llvm::sys::path::parent_path(Argv0); |
| 121 | if (!Parent.empty()) |
| 122 | LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent); |
| 123 | } |
| 124 | if (!LLVMSymbolizerPathOrErr) |
| 125 | LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer"); |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 126 | if (!LLVMSymbolizerPathOrErr) |
| 127 | return false; |
| 128 | const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr; |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 129 | |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 130 | // If we don't know argv0 or the address of main() at this point, try |
| 131 | // to guess it anyway (it's possible on some platforms). |
| 132 | std::string MainExecutableName = |
| 133 | Argv0.empty() ? sys::fs::getMainExecutable(nullptr, nullptr) |
| 134 | : (std::string)Argv0; |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 135 | BumpPtrAllocator Allocator; |
| 136 | StringSaver StrPool(Allocator); |
| 137 | std::vector<const char *> Modules(Depth, nullptr); |
| 138 | std::vector<intptr_t> Offsets(Depth, 0); |
| 139 | if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(), |
| 140 | MainExecutableName.c_str(), StrPool)) |
| 141 | return false; |
| 142 | int InputFD; |
| 143 | SmallString<32> InputFile, OutputFile; |
| 144 | sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile); |
| 145 | sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile); |
| 146 | FileRemover InputRemover(InputFile.c_str()); |
| 147 | FileRemover OutputRemover(OutputFile.c_str()); |
| 148 | |
| 149 | { |
| 150 | raw_fd_ostream Input(InputFD, true); |
| 151 | for (int i = 0; i < Depth; i++) { |
| 152 | if (Modules[i]) |
| 153 | Input << Modules[i] << " " << (void*)Offsets[i] << "\n"; |
| 154 | } |
| 155 | } |
| 156 | |
Zachary Turner | 08426e1 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 157 | Optional<StringRef> Redirects[] = {StringRef(InputFile), |
| 158 | StringRef(OutputFile), llvm::None}; |
| 159 | StringRef Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining", |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 160 | #ifdef _WIN32 |
Zachary Turner | 08426e1 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 161 | // Pass --relative-address on Windows so that we don't |
| 162 | // have to add ImageBase from PE file. |
| 163 | // FIXME: Make this the default for llvm-symbolizer. |
| 164 | "--relative-address", |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 165 | #endif |
Zachary Turner | 08426e1 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 166 | "--demangle"}; |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 167 | int RunResult = |
Zachary Turner | 08426e1 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 168 | sys::ExecuteAndWait(LLVMSymbolizerPath, Args, None, Redirects); |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 169 | if (RunResult != 0) |
| 170 | return false; |
| 171 | |
| 172 | // This report format is based on the sanitizer stack trace printer. See |
| 173 | // sanitizer_stacktrace_printer.cc in compiler-rt. |
| 174 | auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str()); |
| 175 | if (!OutputBuf) |
| 176 | return false; |
| 177 | StringRef Output = OutputBuf.get()->getBuffer(); |
| 178 | SmallVector<StringRef, 32> Lines; |
| 179 | Output.split(Lines, "\n"); |
| 180 | auto CurLine = Lines.begin(); |
| 181 | int frame_no = 0; |
| 182 | for (int i = 0; i < Depth; i++) { |
| 183 | if (!Modules[i]) { |
| 184 | OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << '\n'; |
| 185 | continue; |
| 186 | } |
| 187 | // Read pairs of lines (function name and file/line info) until we |
| 188 | // encounter empty line. |
| 189 | for (;;) { |
| 190 | if (CurLine == Lines.end()) |
| 191 | return false; |
| 192 | StringRef FunctionName = *CurLine++; |
| 193 | if (FunctionName.empty()) |
| 194 | break; |
| 195 | OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << ' '; |
| 196 | if (!FunctionName.startswith("??")) |
| 197 | OS << FunctionName << ' '; |
| 198 | if (CurLine == Lines.end()) |
| 199 | return false; |
| 200 | StringRef FileLineInfo = *CurLine++; |
| 201 | if (!FileLineInfo.startswith("??")) |
| 202 | OS << FileLineInfo; |
| 203 | else |
| 204 | OS << "(" << Modules[i] << '+' << format_hex(Offsets[i], 0) << ")"; |
| 205 | OS << "\n"; |
| 206 | } |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 211 | // Include the platform-specific parts of this class. |
Reid Spencer | 844f3fe | 2004-12-27 06:16:11 +0000 | [diff] [blame] | 212 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 213 | #include "Unix/Signals.inc" |
Reid Spencer | 844f3fe | 2004-12-27 06:16:11 +0000 | [diff] [blame] | 214 | #endif |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 215 | #ifdef _WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 216 | #include "Windows/Signals.inc" |
Reid Spencer | 844f3fe | 2004-12-27 06:16:11 +0000 | [diff] [blame] | 217 | #endif |