29 # include BACKTRACE_HEADER // For backtrace().
41 #include <mach/mach.h>
46 #ifdef HAVE__UNWIND_BACKTRACE
54 #undef HAVE__UNWIND_BACKTRACE
60 static RETSIGTYPE SignalHandler(
int Sig);
65 static void (*InterruptFunction)() =
nullptr;
74 static const int IntSigs[] = {
75 SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
80 static const int KillSigs[] = {
81 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
96 static unsigned NumRegisteredSignals = 0;
103 static void RegisterHandler(
int Signal) {
105 "Out of space for signal handlers!");
107 struct sigaction NewHandler;
109 NewHandler.sa_handler = SignalHandler;
110 NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
111 sigemptyset(&NewHandler.sa_mask);
114 sigaction(Signal, &NewHandler,
115 &RegisteredSignalInfo[NumRegisteredSignals].SA);
116 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
117 ++NumRegisteredSignals;
120 #if defined(HAVE_SIGALTSTACK)
125 static stack_t OldAltStack;
126 static void* NewAltStackPointer;
128 static void CreateSigAltStack() {
129 const size_t AltStackSize = MINSIGSTKSZ + 64 * 1024;
135 if (sigaltstack(
nullptr, &OldAltStack) != 0 ||
136 OldAltStack.ss_flags & SS_ONSTACK ||
137 (OldAltStack.ss_sp && OldAltStack.ss_size >= AltStackSize))
140 stack_t AltStack = {};
141 AltStack.ss_sp =
reinterpret_cast<char *
>(malloc(AltStackSize));
142 NewAltStackPointer = AltStack.ss_sp;
143 AltStack.ss_size = AltStackSize;
144 if (sigaltstack(&AltStack, &OldAltStack) != 0)
145 free(AltStack.ss_sp);
148 static void CreateSigAltStack() {}
151 static void RegisterHandlers() {
155 if (NumRegisteredSignals != 0)
return;
161 for (
auto S : IntSigs) RegisterHandler(S);
162 for (
auto S : KillSigs) RegisterHandler(S);
165 static void UnregisterHandlers() {
167 for (
unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
168 sigaction(RegisteredSignalInfo[i].SigNo,
169 &RegisteredSignalInfo[i].SA,
nullptr);
170 NumRegisteredSignals = 0;
178 static void RemoveFilesToRemove() {
181 if (!FilesToRemove.isConstructed())
186 std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
187 for (
unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
188 const char *path = FilesToRemoveRef[i].c_str();
193 if (stat(path, &buf) != 0)
199 if (!S_ISREG(buf.st_mode))
209 static RETSIGTYPE SignalHandler(
int Sig) {
214 UnregisterHandlers();
218 sigfillset(&SigMask);
219 sigprocmask(SIG_UNBLOCK, &SigMask,
nullptr);
223 RemoveFilesToRemove();
227 if (InterruptFunction) {
228 void (*
IF)() = InterruptFunction;
230 InterruptFunction =
nullptr;
249 if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
256 RemoveFilesToRemove();
262 InterruptFunction =
IF;
272 FilesToRemove->push_back(Filename);
282 std::vector<std::string>::reverse_iterator RI =
284 std::vector<std::string>::iterator
I = FilesToRemove->end();
285 if (RI != FilesToRemove->rend())
286 I = FilesToRemove->erase(RI.base()-1);
297 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H && \
298 (defined(__linux__) || defined(__FreeBSD__) || \
299 defined(__FreeBSD_kernel__) || defined(__NetBSD__))
300 struct DlIteratePhdrData {
304 const char **modules;
306 const char *main_exec_name;
309 static int dl_iterate_phdr_cb(dl_phdr_info *
info,
size_t size,
void *arg) {
310 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
311 const char *
name = data->first ? data->main_exec_name : info->dlpi_name;
313 for (
int i = 0; i < info->dlpi_phnum; i++) {
314 const auto *phdr = &info->dlpi_phdr[i];
317 intptr_t beg = info->dlpi_addr + phdr->p_vaddr;
319 for (
int j = 0; j < data->depth; j++) {
320 if (data->modules[j])
323 if (beg <= addr && addr < end) {
324 data->modules[j] =
name;
325 data->offsets[j] = addr - info->dlpi_addr;
336 const char *MainExecutableName,
338 DlIteratePhdrData data = {StackTrace,
Depth,
true,
339 Modules,
Offsets, MainExecutableName};
340 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
348 const char *MainExecutableName,
352 #endif // defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && ...
354 #if ENABLE_BACKTRACES && defined(HAVE__UNWIND_BACKTRACE)
355 static int unwindBacktrace(
void **StackTrace,
int MaxEntries) {
362 auto HandleFrame = [&](_Unwind_Context *
Context) -> _Unwind_Reason_Code {
364 void *
IP = (
void *)_Unwind_GetIP(
Context);
366 return _URC_END_OF_STACK;
368 assert(Entries < MaxEntries &&
"recursively called after END_OF_STACK?");
370 StackTrace[Entries] = IP;
372 if (++Entries == MaxEntries)
373 return _URC_END_OF_STACK;
374 return _URC_NO_REASON;
378 [](_Unwind_Context *
Context,
void *Handler) {
379 return (*
static_cast<decltype(HandleFrame) *
>(Handler))(Context);
381 static_cast<void *
>(&HandleFrame));
392 #if ENABLE_BACKTRACES
393 static void *StackTrace[256];
395 #if defined(HAVE_BACKTRACE)
398 depth = backtrace(StackTrace, static_cast<int>(
array_lengthof(StackTrace)));
400 #if defined(HAVE__UNWIND_BACKTRACE)
403 depth = unwindBacktrace(StackTrace,
411 #if HAVE_DLFCN_H && HAVE_DLADDR
413 for (
int i = 0; i < depth; ++i) {
415 dladdr(StackTrace[i], &dlinfo);
416 const char* name = strrchr(dlinfo.dli_fname,
'/');
419 if (!name) nwidth = strlen(dlinfo.dli_fname);
420 else nwidth = strlen(name) - 1;
422 if (nwidth > width) width = nwidth;
425 for (
int i = 0; i < depth; ++i) {
427 dladdr(StackTrace[i], &dlinfo);
431 const char* name = strrchr(dlinfo.dli_fname,
'/');
432 if (!name) OS <<
format(
" %-*s", width, dlinfo.dli_fname);
433 else OS <<
format(
" %-*s", width, name+1);
435 OS <<
format(
" %#0*lx", (
int)(
sizeof(
void*) * 2) + 2,
436 (
unsigned long)StackTrace[i]);
438 if (dlinfo.dli_sname !=
nullptr) {
442 if (!d) OS << dlinfo.dli_sname;
449 OS <<
format(
" + %u",(
unsigned)((
char*)StackTrace[i]-
450 (
char*)dlinfo.dli_saddr));
454 #elif defined(HAVE_BACKTRACE)
455 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
460 static void PrintStackTraceSignalHandler(
void *) {
469 bool DisableCrashReporting) {
474 #if defined(__APPLE__) && ENABLE_CRASH_OVERRIDES
476 if (DisableCrashReporting || getenv(
"LLVM_DISABLE_CRASH_REPORT")) {
477 mach_port_t
self = mach_task_self();
479 exception_mask_t mask = EXC_MASK_CRASH;
481 kern_return_t ret = task_set_exception_ports(
self,
484 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
const_iterator end(StringRef path)
Get end iterator over path.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
char * itaniumDemangle(const char *mangled_name, char *buf, size_t *n, int *status)
This is a llvm local version of __cxa_demangle.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
void DisableSystemDialogsOnCrash()
Disable all system dialog boxes that appear when the process crashes.
static bool findModulesAndOffsets(void **StackTrace, int Depth, const char **Modules, intptr_t *Offsets, const char *MainExecutableName, StringSaver &StrPool)
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
void PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting=false)
When an error signal (such as SIBABRT or SIGSEGV) is delivered to the process, print a stack trace an...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void PrintStackTrace(raw_ostream &OS)
Print the stack trace using the given raw_ostream object.
void RunInterruptHandlers()
This function runs all the registered interrupt handlers, including the removal of files registered b...
A pared-down imitation of std::unique_lock from C++11.
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace, int Depth, llvm::raw_ostream &OS) LLVM_ATTRIBUTE_USED
Helper that launches llvm-symbolizer and symbolizes a backtrace.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
Saves strings in the inheritor's stable storage and returns a StringRef with a stable character point...
static ManagedStatic< std::vector< std::pair< void(*)(void *), void * > > > CallBacksToRun
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
void SetInterruptFunction(void(*IF)())
This function registers a function to be called when the user "interrupts" the program (typically by ...
void AddSignalHandler(void(*FnPtr)(void *), void *Cookie)
AddSignalHandler - Add a function to be called when an abort/kill signal is delivered to the process...