blob: e5e38f59c0407b99e5f8ea330213c6749d7f64c2 [file] [log] [blame]
Reid Spencer3d7a6142004-08-29 19:22:48 +00001//===- Signals.cpp - Signal Handling support --------------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer3d7a6142004-08-29 19:22:48 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer3d7a6142004-08-29 19:22:48 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines some helpful functions for dealing with the possibility of
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000011// Unix signals occurring while your program is running.
Reid Spencer3d7a6142004-08-29 19:22:48 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Klecknerba5757d2015-11-05 01:07:54 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
Reid Spencer844f3fe2004-12-27 06:16:11 +000017#include "llvm/Config/config.h"
Reid Klecknerba5757d2015-11-05 01:07:54 +000018#include "llvm/Support/ErrorOr.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/FileUtilities.h"
21#include "llvm/Support/Format.h"
Yaron Keren240bd9c2015-07-22 19:01:14 +000022#include "llvm/Support/ManagedStatic.h"
Reid Klecknerba5757d2015-11-05 01:07:54 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/Mutex.h"
25#include "llvm/Support/Program.h"
Yaron Keren240bd9c2015-07-22 19:01:14 +000026#include "llvm/Support/Signals.h"
Reid Klecknerba5757d2015-11-05 01:07:54 +000027#include "llvm/Support/StringSaver.h"
28#include "llvm/Support/raw_ostream.h"
Yaron Keren240bd9c2015-07-22 19:01:14 +000029#include <vector>
Reid Spencer3d7a6142004-08-29 19:22:48 +000030
31namespace llvm {
32using namespace sys;
33
34//===----------------------------------------------------------------------===//
35//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000036//=== independent code.
Reid Spencer3d7a6142004-08-29 19:22:48 +000037//===----------------------------------------------------------------------===//
38
Yaron Keren240bd9c2015-07-22 19:01:14 +000039static ManagedStatic<std::vector<std::pair<void (*)(void *), void *>>>
40 CallBacksToRun;
Yaron Keren28738102015-07-22 21:11:17 +000041void sys::RunSignalHandlers() {
Yaron Keren240bd9c2015-07-22 19:01:14 +000042 if (!CallBacksToRun.isConstructed())
43 return;
44 for (auto &I : *CallBacksToRun)
45 I.first(I.second);
46 CallBacksToRun->clear();
47}
Reid Spencer3d7a6142004-08-29 19:22:48 +000048}
49
Reid Klecknerba5757d2015-11-05 01:07:54 +000050using namespace llvm;
51
52static bool findModulesAndOffsets(void **StackTrace, int Depth,
53 const char **Modules, intptr_t *Offsets,
54 const char *MainExecutableName,
55 StringSaver &StrPool);
56
57/// Format a pointer value as hexadecimal. Zero pad it out so its always the
58/// same width.
59static FormattedNumber format_ptr(void *PC) {
60 // Each byte is two hex digits plus 2 for the 0x prefix.
61 unsigned PtrWidth = 2 + 2 * sizeof(void *);
62 return format_hex((uint64_t)PC, PtrWidth);
63}
64
Richard Smith2ad6d482016-06-09 00:53:21 +000065static bool printSymbolizedStackTrace(StringRef Argv0,
66 void **StackTrace, int Depth,
NAKAMURA Takumi02d97aa2015-11-08 09:45:06 +000067 llvm::raw_ostream &OS)
68 LLVM_ATTRIBUTE_USED;
69
Reid Klecknerba5757d2015-11-05 01:07:54 +000070/// Helper that launches llvm-symbolizer and symbolizes a backtrace.
Richard Smith2ad6d482016-06-09 00:53:21 +000071static bool printSymbolizedStackTrace(StringRef Argv0,
72 void **StackTrace, int Depth,
Reid Klecknerba5757d2015-11-05 01:07:54 +000073 llvm::raw_ostream &OS) {
Richard Smith2ad6d482016-06-09 00:53:21 +000074 // Don't recursively invoke the llvm-symbolizer binary.
75 if (Argv0.find("llvm-symbolizer") != std::string::npos)
76 return false;
77
Reid Klecknerba5757d2015-11-05 01:07:54 +000078 // FIXME: Subtract necessary number from StackTrace entries to turn return addresses
79 // into actual instruction addresses.
Richard Smith2ad6d482016-06-09 00:53:21 +000080 // Use llvm-symbolizer tool to symbolize the stack traces. First look for it
81 // alongside our binary, then in $PATH.
82 ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code();
83 if (!Argv0.empty()) {
84 StringRef Parent = llvm::sys::path::parent_path(Argv0);
85 if (!Parent.empty())
86 LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent);
87 }
88 if (!LLVMSymbolizerPathOrErr)
89 LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer");
Reid Klecknerba5757d2015-11-05 01:07:54 +000090 if (!LLVMSymbolizerPathOrErr)
91 return false;
92 const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
Reid Klecknerba5757d2015-11-05 01:07:54 +000093
Richard Smith2ad6d482016-06-09 00:53:21 +000094 // If we don't know argv0 or the address of main() at this point, try
95 // to guess it anyway (it's possible on some platforms).
96 std::string MainExecutableName =
97 Argv0.empty() ? sys::fs::getMainExecutable(nullptr, nullptr)
98 : (std::string)Argv0;
Reid Klecknerba5757d2015-11-05 01:07:54 +000099 BumpPtrAllocator Allocator;
100 StringSaver StrPool(Allocator);
101 std::vector<const char *> Modules(Depth, nullptr);
102 std::vector<intptr_t> Offsets(Depth, 0);
103 if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(),
104 MainExecutableName.c_str(), StrPool))
105 return false;
106 int InputFD;
107 SmallString<32> InputFile, OutputFile;
108 sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile);
109 sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile);
110 FileRemover InputRemover(InputFile.c_str());
111 FileRemover OutputRemover(OutputFile.c_str());
112
113 {
114 raw_fd_ostream Input(InputFD, true);
115 for (int i = 0; i < Depth; i++) {
116 if (Modules[i])
117 Input << Modules[i] << " " << (void*)Offsets[i] << "\n";
118 }
119 }
120
121 StringRef InputFileStr(InputFile);
122 StringRef OutputFileStr(OutputFile);
123 StringRef StderrFileStr;
124 const StringRef *Redirects[] = {&InputFileStr, &OutputFileStr,
125 &StderrFileStr};
126 const char *Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining",
127#ifdef LLVM_ON_WIN32
128 // Pass --relative-address on Windows so that we don't
129 // have to add ImageBase from PE file.
130 // FIXME: Make this the default for llvm-symbolizer.
131 "--relative-address",
132#endif
133 "--demangle", nullptr};
134 int RunResult =
135 sys::ExecuteAndWait(LLVMSymbolizerPath, Args, nullptr, Redirects);
136 if (RunResult != 0)
137 return false;
138
139 // This report format is based on the sanitizer stack trace printer. See
140 // sanitizer_stacktrace_printer.cc in compiler-rt.
141 auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str());
142 if (!OutputBuf)
143 return false;
144 StringRef Output = OutputBuf.get()->getBuffer();
145 SmallVector<StringRef, 32> Lines;
146 Output.split(Lines, "\n");
147 auto CurLine = Lines.begin();
148 int frame_no = 0;
149 for (int i = 0; i < Depth; i++) {
150 if (!Modules[i]) {
151 OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << '\n';
152 continue;
153 }
154 // Read pairs of lines (function name and file/line info) until we
155 // encounter empty line.
156 for (;;) {
157 if (CurLine == Lines.end())
158 return false;
159 StringRef FunctionName = *CurLine++;
160 if (FunctionName.empty())
161 break;
162 OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << ' ';
163 if (!FunctionName.startswith("??"))
164 OS << FunctionName << ' ';
165 if (CurLine == Lines.end())
166 return false;
167 StringRef FileLineInfo = *CurLine++;
168 if (!FileLineInfo.startswith("??"))
169 OS << FileLineInfo;
170 else
171 OS << "(" << Modules[i] << '+' << format_hex(Offsets[i], 0) << ")";
172 OS << "\n";
173 }
174 }
175 return true;
176}
177
Reid Spencer3d7a6142004-08-29 19:22:48 +0000178// Include the platform-specific parts of this class.
Reid Spencer844f3fe2004-12-27 06:16:11 +0000179#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +0000180#include "Unix/Signals.inc"
Reid Spencer844f3fe2004-12-27 06:16:11 +0000181#endif
182#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +0000183#include "Windows/Signals.inc"
Reid Spencer844f3fe2004-12-27 06:16:11 +0000184#endif