xref: /aosp_15_r20/external/llvm/lib/Support/Unix/Signals.inc (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker//
3*9880d681SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker//
5*9880d681SAndroid Build Coastguard Worker// This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker// License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker//
8*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker//
10*9880d681SAndroid Build Coastguard Worker// This file defines some helpful functions for dealing with the possibility of
11*9880d681SAndroid Build Coastguard Worker// Unix signals occurring while your program is running.
12*9880d681SAndroid Build Coastguard Worker//
13*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker#include "Unix.h"
16*9880d681SAndroid Build Coastguard Worker#include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/Format.h"
18*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/FileSystem.h"
19*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/FileUtilities.h"
20*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/MemoryBuffer.h"
21*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/Mutex.h"
22*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/Program.h"
23*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/UniqueLock.h"
24*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/raw_ostream.h"
25*9880d681SAndroid Build Coastguard Worker#include <algorithm>
26*9880d681SAndroid Build Coastguard Worker#include <string>
27*9880d681SAndroid Build Coastguard Worker#if HAVE_EXECINFO_H
28*9880d681SAndroid Build Coastguard Worker# include <execinfo.h>         // For backtrace().
29*9880d681SAndroid Build Coastguard Worker#endif
30*9880d681SAndroid Build Coastguard Worker#if HAVE_SIGNAL_H
31*9880d681SAndroid Build Coastguard Worker#include <signal.h>
32*9880d681SAndroid Build Coastguard Worker#endif
33*9880d681SAndroid Build Coastguard Worker#if HAVE_SYS_STAT_H
34*9880d681SAndroid Build Coastguard Worker#include <sys/stat.h>
35*9880d681SAndroid Build Coastguard Worker#endif
36*9880d681SAndroid Build Coastguard Worker#if HAVE_CXXABI_H
37*9880d681SAndroid Build Coastguard Worker#include <cxxabi.h>
38*9880d681SAndroid Build Coastguard Worker#endif
39*9880d681SAndroid Build Coastguard Worker#if HAVE_DLFCN_H
40*9880d681SAndroid Build Coastguard Worker#include <dlfcn.h>
41*9880d681SAndroid Build Coastguard Worker#endif
42*9880d681SAndroid Build Coastguard Worker#if HAVE_MACH_MACH_H
43*9880d681SAndroid Build Coastguard Worker#include <mach/mach.h>
44*9880d681SAndroid Build Coastguard Worker#endif
45*9880d681SAndroid Build Coastguard Worker#if HAVE_LINK_H
46*9880d681SAndroid Build Coastguard Worker#include <link.h>
47*9880d681SAndroid Build Coastguard Worker#endif
48*9880d681SAndroid Build Coastguard Worker#if HAVE_UNWIND_BACKTRACE
49*9880d681SAndroid Build Coastguard Worker// FIXME: We should be able to use <unwind.h> for any target that has an
50*9880d681SAndroid Build Coastguard Worker// _Unwind_Backtrace function, but on FreeBSD the configure test passes
51*9880d681SAndroid Build Coastguard Worker// despite the function not existing, and on Android, <unwind.h> conflicts
52*9880d681SAndroid Build Coastguard Worker// with <link.h>.
53*9880d681SAndroid Build Coastguard Worker#ifdef __GLIBC__
54*9880d681SAndroid Build Coastguard Worker#include <unwind.h>
55*9880d681SAndroid Build Coastguard Worker#else
56*9880d681SAndroid Build Coastguard Worker#undef HAVE_UNWIND_BACKTRACE
57*9880d681SAndroid Build Coastguard Worker#endif
58*9880d681SAndroid Build Coastguard Worker#endif
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Workerusing namespace llvm;
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Workerstatic RETSIGTYPE SignalHandler(int Sig);  // defined below.
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Workerstatic ManagedStatic<SmartMutex<true> > SignalsMutex;
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker/// InterruptFunction - The function to call if ctrl-c is pressed.
67*9880d681SAndroid Build Coastguard Workerstatic void (*InterruptFunction)() = nullptr;
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Workerstatic ManagedStatic<std::vector<std::string>> FilesToRemove;
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Workerstatic StringRef Argv0;
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker// IntSigs - Signals that represent requested termination. There's no bug
74*9880d681SAndroid Build Coastguard Worker// or failure, or if there is, it's not our direct responsibility. For whatever
75*9880d681SAndroid Build Coastguard Worker// reason, our continued execution is no longer desirable.
76*9880d681SAndroid Build Coastguard Workerstatic const int IntSigs[] = {
77*9880d681SAndroid Build Coastguard Worker  SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
78*9880d681SAndroid Build Coastguard Worker};
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker// KillSigs - Signals that represent that we have a bug, and our prompt
81*9880d681SAndroid Build Coastguard Worker// termination has been ordered.
82*9880d681SAndroid Build Coastguard Workerstatic const int KillSigs[] = {
83*9880d681SAndroid Build Coastguard Worker  SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
84*9880d681SAndroid Build Coastguard Worker#ifdef SIGSYS
85*9880d681SAndroid Build Coastguard Worker  , SIGSYS
86*9880d681SAndroid Build Coastguard Worker#endif
87*9880d681SAndroid Build Coastguard Worker#ifdef SIGXCPU
88*9880d681SAndroid Build Coastguard Worker  , SIGXCPU
89*9880d681SAndroid Build Coastguard Worker#endif
90*9880d681SAndroid Build Coastguard Worker#ifdef SIGXFSZ
91*9880d681SAndroid Build Coastguard Worker  , SIGXFSZ
92*9880d681SAndroid Build Coastguard Worker#endif
93*9880d681SAndroid Build Coastguard Worker#ifdef SIGEMT
94*9880d681SAndroid Build Coastguard Worker  , SIGEMT
95*9880d681SAndroid Build Coastguard Worker#endif
96*9880d681SAndroid Build Coastguard Worker};
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Workerstatic unsigned NumRegisteredSignals = 0;
99*9880d681SAndroid Build Coastguard Workerstatic struct {
100*9880d681SAndroid Build Coastguard Worker  struct sigaction SA;
101*9880d681SAndroid Build Coastguard Worker  int SigNo;
102*9880d681SAndroid Build Coastguard Worker} RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)];
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Workerstatic void RegisterHandler(int Signal) {
106*9880d681SAndroid Build Coastguard Worker  assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) &&
107*9880d681SAndroid Build Coastguard Worker         "Out of space for signal handlers!");
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker  struct sigaction NewHandler;
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker  NewHandler.sa_handler = SignalHandler;
112*9880d681SAndroid Build Coastguard Worker  NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
113*9880d681SAndroid Build Coastguard Worker  sigemptyset(&NewHandler.sa_mask);
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker  // Install the new handler, save the old one in RegisteredSignalInfo.
116*9880d681SAndroid Build Coastguard Worker  sigaction(Signal, &NewHandler,
117*9880d681SAndroid Build Coastguard Worker            &RegisteredSignalInfo[NumRegisteredSignals].SA);
118*9880d681SAndroid Build Coastguard Worker  RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
119*9880d681SAndroid Build Coastguard Worker  ++NumRegisteredSignals;
120*9880d681SAndroid Build Coastguard Worker}
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker#if defined(HAVE_SIGALTSTACK)
123*9880d681SAndroid Build Coastguard Worker// Hold onto the old alternate signal stack so that it's not reported as a leak.
124*9880d681SAndroid Build Coastguard Worker// We don't make any attempt to remove our alt signal stack if we remove our
125*9880d681SAndroid Build Coastguard Worker// signal handlers; that can't be done reliably if someone else is also trying
126*9880d681SAndroid Build Coastguard Worker// to do the same thing.
127*9880d681SAndroid Build Coastguard Workerstatic stack_t OldAltStack;
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Workerstatic void CreateSigAltStack() {
130*9880d681SAndroid Build Coastguard Worker  const size_t AltStackSize = MINSIGSTKSZ + 8192;
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker  // If we're executing on the alternate stack, or we already have an alternate
133*9880d681SAndroid Build Coastguard Worker  // signal stack that we're happy with, there's nothing for us to do. Don't
134*9880d681SAndroid Build Coastguard Worker  // reduce the size, some other part of the process might need a larger stack
135*9880d681SAndroid Build Coastguard Worker  // than we do.
136*9880d681SAndroid Build Coastguard Worker  if (sigaltstack(nullptr, &OldAltStack) != 0 ||
137*9880d681SAndroid Build Coastguard Worker      OldAltStack.ss_flags & SS_ONSTACK ||
138*9880d681SAndroid Build Coastguard Worker      (OldAltStack.ss_sp && OldAltStack.ss_size >= AltStackSize))
139*9880d681SAndroid Build Coastguard Worker    return;
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker  stack_t AltStack = {};
142*9880d681SAndroid Build Coastguard Worker  AltStack.ss_sp = reinterpret_cast<char *>(malloc(AltStackSize));
143*9880d681SAndroid Build Coastguard Worker  AltStack.ss_size = AltStackSize;
144*9880d681SAndroid Build Coastguard Worker  if (sigaltstack(&AltStack, &OldAltStack) != 0)
145*9880d681SAndroid Build Coastguard Worker    free(AltStack.ss_sp);
146*9880d681SAndroid Build Coastguard Worker}
147*9880d681SAndroid Build Coastguard Worker#else
148*9880d681SAndroid Build Coastguard Workerstatic void CreateSigAltStack() {}
149*9880d681SAndroid Build Coastguard Worker#endif
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Workerstatic void RegisterHandlers() {
152*9880d681SAndroid Build Coastguard Worker  // We need to dereference the signals mutex during handler registration so
153*9880d681SAndroid Build Coastguard Worker  // that we force its construction. This is to prevent the first use being
154*9880d681SAndroid Build Coastguard Worker  // during handling an actual signal because you can't safely call new in a
155*9880d681SAndroid Build Coastguard Worker  // signal handler.
156*9880d681SAndroid Build Coastguard Worker  *SignalsMutex;
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker  // If the handlers are already registered, we're done.
159*9880d681SAndroid Build Coastguard Worker  if (NumRegisteredSignals != 0) return;
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker  // Create an alternate stack for signal handling. This is necessary for us to
162*9880d681SAndroid Build Coastguard Worker  // be able to reliably handle signals due to stack overflow.
163*9880d681SAndroid Build Coastguard Worker  CreateSigAltStack();
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker  for (auto S : IntSigs) RegisterHandler(S);
166*9880d681SAndroid Build Coastguard Worker  for (auto S : KillSigs) RegisterHandler(S);
167*9880d681SAndroid Build Coastguard Worker}
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Workerstatic void UnregisterHandlers() {
170*9880d681SAndroid Build Coastguard Worker  // Restore all of the signal handlers to how they were before we showed up.
171*9880d681SAndroid Build Coastguard Worker  for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
172*9880d681SAndroid Build Coastguard Worker    sigaction(RegisteredSignalInfo[i].SigNo,
173*9880d681SAndroid Build Coastguard Worker              &RegisteredSignalInfo[i].SA, nullptr);
174*9880d681SAndroid Build Coastguard Worker  NumRegisteredSignals = 0;
175*9880d681SAndroid Build Coastguard Worker}
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker/// RemoveFilesToRemove - Process the FilesToRemove list. This function
179*9880d681SAndroid Build Coastguard Worker/// should be called with the SignalsMutex lock held.
180*9880d681SAndroid Build Coastguard Worker/// NB: This must be an async signal safe function. It cannot allocate or free
181*9880d681SAndroid Build Coastguard Worker/// memory, even in debug builds.
182*9880d681SAndroid Build Coastguard Workerstatic void RemoveFilesToRemove() {
183*9880d681SAndroid Build Coastguard Worker  // Avoid constructing ManagedStatic in the signal handler.
184*9880d681SAndroid Build Coastguard Worker  // If FilesToRemove is not constructed, there are no files to remove.
185*9880d681SAndroid Build Coastguard Worker  if (!FilesToRemove.isConstructed())
186*9880d681SAndroid Build Coastguard Worker    return;
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker  // We avoid iterators in case of debug iterators that allocate or release
189*9880d681SAndroid Build Coastguard Worker  // memory.
190*9880d681SAndroid Build Coastguard Worker  std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
191*9880d681SAndroid Build Coastguard Worker  for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
192*9880d681SAndroid Build Coastguard Worker    const char *path = FilesToRemoveRef[i].c_str();
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker    // Get the status so we can determine if it's a file or directory. If we
195*9880d681SAndroid Build Coastguard Worker    // can't stat the file, ignore it.
196*9880d681SAndroid Build Coastguard Worker    struct stat buf;
197*9880d681SAndroid Build Coastguard Worker    if (stat(path, &buf) != 0)
198*9880d681SAndroid Build Coastguard Worker      continue;
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker    // If this is not a regular file, ignore it. We want to prevent removal of
201*9880d681SAndroid Build Coastguard Worker    // special files like /dev/null, even if the compiler is being run with the
202*9880d681SAndroid Build Coastguard Worker    // super-user permissions.
203*9880d681SAndroid Build Coastguard Worker    if (!S_ISREG(buf.st_mode))
204*9880d681SAndroid Build Coastguard Worker      continue;
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker    // Otherwise, remove the file. We ignore any errors here as there is nothing
207*9880d681SAndroid Build Coastguard Worker    // else we can do.
208*9880d681SAndroid Build Coastguard Worker    unlink(path);
209*9880d681SAndroid Build Coastguard Worker  }
210*9880d681SAndroid Build Coastguard Worker}
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker// SignalHandler - The signal handler that runs.
213*9880d681SAndroid Build Coastguard Workerstatic RETSIGTYPE SignalHandler(int Sig) {
214*9880d681SAndroid Build Coastguard Worker  // Restore the signal behavior to default, so that the program actually
215*9880d681SAndroid Build Coastguard Worker  // crashes when we return and the signal reissues.  This also ensures that if
216*9880d681SAndroid Build Coastguard Worker  // we crash in our signal handler that the program will terminate immediately
217*9880d681SAndroid Build Coastguard Worker  // instead of recursing in the signal handler.
218*9880d681SAndroid Build Coastguard Worker  UnregisterHandlers();
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard Worker  // Unmask all potentially blocked kill signals.
221*9880d681SAndroid Build Coastguard Worker  sigset_t SigMask;
222*9880d681SAndroid Build Coastguard Worker  sigfillset(&SigMask);
223*9880d681SAndroid Build Coastguard Worker  sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker  {
226*9880d681SAndroid Build Coastguard Worker    unique_lock<SmartMutex<true>> Guard(*SignalsMutex);
227*9880d681SAndroid Build Coastguard Worker    RemoveFilesToRemove();
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker    if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
230*9880d681SAndroid Build Coastguard Worker        != std::end(IntSigs)) {
231*9880d681SAndroid Build Coastguard Worker      if (InterruptFunction) {
232*9880d681SAndroid Build Coastguard Worker        void (*IF)() = InterruptFunction;
233*9880d681SAndroid Build Coastguard Worker        Guard.unlock();
234*9880d681SAndroid Build Coastguard Worker        InterruptFunction = nullptr;
235*9880d681SAndroid Build Coastguard Worker        IF();        // run the interrupt function.
236*9880d681SAndroid Build Coastguard Worker        return;
237*9880d681SAndroid Build Coastguard Worker      }
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker      Guard.unlock();
240*9880d681SAndroid Build Coastguard Worker      raise(Sig);   // Execute the default handler.
241*9880d681SAndroid Build Coastguard Worker      return;
242*9880d681SAndroid Build Coastguard Worker   }
243*9880d681SAndroid Build Coastguard Worker  }
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker  // Otherwise if it is a fault (like SEGV) run any handler.
246*9880d681SAndroid Build Coastguard Worker  llvm::sys::RunSignalHandlers();
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker#ifdef __s390__
249*9880d681SAndroid Build Coastguard Worker  // On S/390, certain signals are delivered with PSW Address pointing to
250*9880d681SAndroid Build Coastguard Worker  // *after* the faulting instruction.  Simply returning from the signal
251*9880d681SAndroid Build Coastguard Worker  // handler would continue execution after that point, instead of
252*9880d681SAndroid Build Coastguard Worker  // re-raising the signal.  Raise the signal manually in those cases.
253*9880d681SAndroid Build Coastguard Worker  if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
254*9880d681SAndroid Build Coastguard Worker    raise(Sig);
255*9880d681SAndroid Build Coastguard Worker#endif
256*9880d681SAndroid Build Coastguard Worker}
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Workervoid llvm::sys::RunInterruptHandlers() {
259*9880d681SAndroid Build Coastguard Worker  sys::SmartScopedLock<true> Guard(*SignalsMutex);
260*9880d681SAndroid Build Coastguard Worker  RemoveFilesToRemove();
261*9880d681SAndroid Build Coastguard Worker}
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Workervoid llvm::sys::SetInterruptFunction(void (*IF)()) {
264*9880d681SAndroid Build Coastguard Worker  {
265*9880d681SAndroid Build Coastguard Worker    sys::SmartScopedLock<true> Guard(*SignalsMutex);
266*9880d681SAndroid Build Coastguard Worker    InterruptFunction = IF;
267*9880d681SAndroid Build Coastguard Worker  }
268*9880d681SAndroid Build Coastguard Worker  RegisterHandlers();
269*9880d681SAndroid Build Coastguard Worker}
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker// RemoveFileOnSignal - The public API
272*9880d681SAndroid Build Coastguard Workerbool llvm::sys::RemoveFileOnSignal(StringRef Filename,
273*9880d681SAndroid Build Coastguard Worker                                   std::string* ErrMsg) {
274*9880d681SAndroid Build Coastguard Worker  {
275*9880d681SAndroid Build Coastguard Worker    sys::SmartScopedLock<true> Guard(*SignalsMutex);
276*9880d681SAndroid Build Coastguard Worker    FilesToRemove->push_back(Filename);
277*9880d681SAndroid Build Coastguard Worker  }
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard Worker  RegisterHandlers();
280*9880d681SAndroid Build Coastguard Worker  return false;
281*9880d681SAndroid Build Coastguard Worker}
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker// DontRemoveFileOnSignal - The public API
284*9880d681SAndroid Build Coastguard Workervoid llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
285*9880d681SAndroid Build Coastguard Worker  sys::SmartScopedLock<true> Guard(*SignalsMutex);
286*9880d681SAndroid Build Coastguard Worker  std::vector<std::string>::reverse_iterator RI =
287*9880d681SAndroid Build Coastguard Worker    std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
288*9880d681SAndroid Build Coastguard Worker  std::vector<std::string>::iterator I = FilesToRemove->end();
289*9880d681SAndroid Build Coastguard Worker  if (RI != FilesToRemove->rend())
290*9880d681SAndroid Build Coastguard Worker    I = FilesToRemove->erase(RI.base()-1);
291*9880d681SAndroid Build Coastguard Worker}
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker/// AddSignalHandler - Add a function to be called when a signal is delivered
294*9880d681SAndroid Build Coastguard Worker/// to the process.  The handler can have a cookie passed to it to identify
295*9880d681SAndroid Build Coastguard Worker/// what instance of the handler it is.
296*9880d681SAndroid Build Coastguard Workervoid llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
297*9880d681SAndroid Build Coastguard Worker  CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
298*9880d681SAndroid Build Coastguard Worker  RegisterHandlers();
299*9880d681SAndroid Build Coastguard Worker}
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) && HAVE_LINK_H &&    \
302*9880d681SAndroid Build Coastguard Worker    (defined(__linux__) || defined(__FreeBSD__) ||                             \
303*9880d681SAndroid Build Coastguard Worker     defined(__FreeBSD_kernel__) || defined(__NetBSD__))
304*9880d681SAndroid Build Coastguard Workerstruct DlIteratePhdrData {
305*9880d681SAndroid Build Coastguard Worker  void **StackTrace;
306*9880d681SAndroid Build Coastguard Worker  int depth;
307*9880d681SAndroid Build Coastguard Worker  bool first;
308*9880d681SAndroid Build Coastguard Worker  const char **modules;
309*9880d681SAndroid Build Coastguard Worker  intptr_t *offsets;
310*9880d681SAndroid Build Coastguard Worker  const char *main_exec_name;
311*9880d681SAndroid Build Coastguard Worker};
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Workerstatic int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
314*9880d681SAndroid Build Coastguard Worker  DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
315*9880d681SAndroid Build Coastguard Worker  const char *name = data->first ? data->main_exec_name : info->dlpi_name;
316*9880d681SAndroid Build Coastguard Worker  data->first = false;
317*9880d681SAndroid Build Coastguard Worker  for (int i = 0; i < info->dlpi_phnum; i++) {
318*9880d681SAndroid Build Coastguard Worker    const auto *phdr = &info->dlpi_phdr[i];
319*9880d681SAndroid Build Coastguard Worker    if (phdr->p_type != PT_LOAD)
320*9880d681SAndroid Build Coastguard Worker      continue;
321*9880d681SAndroid Build Coastguard Worker    intptr_t beg = info->dlpi_addr + phdr->p_vaddr;
322*9880d681SAndroid Build Coastguard Worker    intptr_t end = beg + phdr->p_memsz;
323*9880d681SAndroid Build Coastguard Worker    for (int j = 0; j < data->depth; j++) {
324*9880d681SAndroid Build Coastguard Worker      if (data->modules[j])
325*9880d681SAndroid Build Coastguard Worker        continue;
326*9880d681SAndroid Build Coastguard Worker      intptr_t addr = (intptr_t)data->StackTrace[j];
327*9880d681SAndroid Build Coastguard Worker      if (beg <= addr && addr < end) {
328*9880d681SAndroid Build Coastguard Worker        data->modules[j] = name;
329*9880d681SAndroid Build Coastguard Worker        data->offsets[j] = addr - info->dlpi_addr;
330*9880d681SAndroid Build Coastguard Worker      }
331*9880d681SAndroid Build Coastguard Worker    }
332*9880d681SAndroid Build Coastguard Worker  }
333*9880d681SAndroid Build Coastguard Worker  return 0;
334*9880d681SAndroid Build Coastguard Worker}
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker/// If this is an ELF platform, we can find all loaded modules and their virtual
337*9880d681SAndroid Build Coastguard Worker/// addresses with dl_iterate_phdr.
338*9880d681SAndroid Build Coastguard Workerstatic bool findModulesAndOffsets(void **StackTrace, int Depth,
339*9880d681SAndroid Build Coastguard Worker                                  const char **Modules, intptr_t *Offsets,
340*9880d681SAndroid Build Coastguard Worker                                  const char *MainExecutableName,
341*9880d681SAndroid Build Coastguard Worker                                  StringSaver &StrPool) {
342*9880d681SAndroid Build Coastguard Worker  DlIteratePhdrData data = {StackTrace, Depth,   true,
343*9880d681SAndroid Build Coastguard Worker                            Modules,    Offsets, MainExecutableName};
344*9880d681SAndroid Build Coastguard Worker  dl_iterate_phdr(dl_iterate_phdr_cb, &data);
345*9880d681SAndroid Build Coastguard Worker  return true;
346*9880d681SAndroid Build Coastguard Worker}
347*9880d681SAndroid Build Coastguard Worker#else
348*9880d681SAndroid Build Coastguard Worker/// This platform does not have dl_iterate_phdr, so we do not yet know how to
349*9880d681SAndroid Build Coastguard Worker/// find all loaded DSOs.
350*9880d681SAndroid Build Coastguard Workerstatic bool findModulesAndOffsets(void **StackTrace, int Depth,
351*9880d681SAndroid Build Coastguard Worker                                  const char **Modules, intptr_t *Offsets,
352*9880d681SAndroid Build Coastguard Worker                                  const char *MainExecutableName,
353*9880d681SAndroid Build Coastguard Worker                                  StringSaver &StrPool) {
354*9880d681SAndroid Build Coastguard Worker  return false;
355*9880d681SAndroid Build Coastguard Worker}
356*9880d681SAndroid Build Coastguard Worker#endif // defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) && ...
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard Worker#if defined(ENABLE_BACKTRACES) && defined(HAVE_UNWIND_BACKTRACE)
359*9880d681SAndroid Build Coastguard Workerstatic int unwindBacktrace(void **StackTrace, int MaxEntries) {
360*9880d681SAndroid Build Coastguard Worker  if (MaxEntries < 0)
361*9880d681SAndroid Build Coastguard Worker    return 0;
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker  // Skip the first frame ('unwindBacktrace' itself).
364*9880d681SAndroid Build Coastguard Worker  int Entries = -1;
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Worker  auto HandleFrame = [&](_Unwind_Context *Context) -> _Unwind_Reason_Code {
367*9880d681SAndroid Build Coastguard Worker    // Apparently we need to detect reaching the end of the stack ourselves.
368*9880d681SAndroid Build Coastguard Worker    void *IP = (void *)_Unwind_GetIP(Context);
369*9880d681SAndroid Build Coastguard Worker    if (!IP)
370*9880d681SAndroid Build Coastguard Worker      return _URC_END_OF_STACK;
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker    assert(Entries < MaxEntries && "recursively called after END_OF_STACK?");
373*9880d681SAndroid Build Coastguard Worker    if (Entries >= 0)
374*9880d681SAndroid Build Coastguard Worker      StackTrace[Entries] = IP;
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker    if (++Entries == MaxEntries)
377*9880d681SAndroid Build Coastguard Worker      return _URC_END_OF_STACK;
378*9880d681SAndroid Build Coastguard Worker    return _URC_NO_REASON;
379*9880d681SAndroid Build Coastguard Worker  };
380*9880d681SAndroid Build Coastguard Worker
381*9880d681SAndroid Build Coastguard Worker  _Unwind_Backtrace(
382*9880d681SAndroid Build Coastguard Worker      [](_Unwind_Context *Context, void *Handler) {
383*9880d681SAndroid Build Coastguard Worker        return (*static_cast<decltype(HandleFrame) *>(Handler))(Context);
384*9880d681SAndroid Build Coastguard Worker      },
385*9880d681SAndroid Build Coastguard Worker      static_cast<void *>(&HandleFrame));
386*9880d681SAndroid Build Coastguard Worker  return std::max(Entries, 0);
387*9880d681SAndroid Build Coastguard Worker}
388*9880d681SAndroid Build Coastguard Worker#endif
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker// PrintStackTrace - In the case of a program crash or fault, print out a stack
391*9880d681SAndroid Build Coastguard Worker// trace so that the user has an indication of why and where we died.
392*9880d681SAndroid Build Coastguard Worker//
393*9880d681SAndroid Build Coastguard Worker// On glibc systems we have the 'backtrace' function, which works nicely, but
394*9880d681SAndroid Build Coastguard Worker// doesn't demangle symbols.
395*9880d681SAndroid Build Coastguard Workervoid llvm::sys::PrintStackTrace(raw_ostream &OS) {
396*9880d681SAndroid Build Coastguard Worker#if defined(ENABLE_BACKTRACES)
397*9880d681SAndroid Build Coastguard Worker  static void *StackTrace[256];
398*9880d681SAndroid Build Coastguard Worker  int depth = 0;
399*9880d681SAndroid Build Coastguard Worker#if defined(HAVE_BACKTRACE)
400*9880d681SAndroid Build Coastguard Worker  // Use backtrace() to output a backtrace on Linux systems with glibc.
401*9880d681SAndroid Build Coastguard Worker  if (!depth)
402*9880d681SAndroid Build Coastguard Worker    depth = backtrace(StackTrace, static_cast<int>(array_lengthof(StackTrace)));
403*9880d681SAndroid Build Coastguard Worker#endif
404*9880d681SAndroid Build Coastguard Worker#if defined(HAVE_UNWIND_BACKTRACE)
405*9880d681SAndroid Build Coastguard Worker  // Try _Unwind_Backtrace() if backtrace() failed.
406*9880d681SAndroid Build Coastguard Worker  if (!depth)
407*9880d681SAndroid Build Coastguard Worker    depth = unwindBacktrace(StackTrace,
408*9880d681SAndroid Build Coastguard Worker                        static_cast<int>(array_lengthof(StackTrace)));
409*9880d681SAndroid Build Coastguard Worker#endif
410*9880d681SAndroid Build Coastguard Worker  if (!depth)
411*9880d681SAndroid Build Coastguard Worker    return;
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker  if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS))
414*9880d681SAndroid Build Coastguard Worker    return;
415*9880d681SAndroid Build Coastguard Worker#if HAVE_DLFCN_H && __GNUG__
416*9880d681SAndroid Build Coastguard Worker  int width = 0;
417*9880d681SAndroid Build Coastguard Worker  for (int i = 0; i < depth; ++i) {
418*9880d681SAndroid Build Coastguard Worker    Dl_info dlinfo;
419*9880d681SAndroid Build Coastguard Worker    dladdr(StackTrace[i], &dlinfo);
420*9880d681SAndroid Build Coastguard Worker    const char* name = strrchr(dlinfo.dli_fname, '/');
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker    int nwidth;
423*9880d681SAndroid Build Coastguard Worker    if (!name) nwidth = strlen(dlinfo.dli_fname);
424*9880d681SAndroid Build Coastguard Worker    else       nwidth = strlen(name) - 1;
425*9880d681SAndroid Build Coastguard Worker
426*9880d681SAndroid Build Coastguard Worker    if (nwidth > width) width = nwidth;
427*9880d681SAndroid Build Coastguard Worker  }
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker  for (int i = 0; i < depth; ++i) {
430*9880d681SAndroid Build Coastguard Worker    Dl_info dlinfo;
431*9880d681SAndroid Build Coastguard Worker    dladdr(StackTrace[i], &dlinfo);
432*9880d681SAndroid Build Coastguard Worker
433*9880d681SAndroid Build Coastguard Worker    OS << format("%-2d", i);
434*9880d681SAndroid Build Coastguard Worker
435*9880d681SAndroid Build Coastguard Worker    const char* name = strrchr(dlinfo.dli_fname, '/');
436*9880d681SAndroid Build Coastguard Worker    if (!name) OS << format(" %-*s", width, dlinfo.dli_fname);
437*9880d681SAndroid Build Coastguard Worker    else       OS << format(" %-*s", width, name+1);
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard Worker    OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2,
440*9880d681SAndroid Build Coastguard Worker                 (unsigned long)StackTrace[i]);
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker    if (dlinfo.dli_sname != nullptr) {
443*9880d681SAndroid Build Coastguard Worker      OS << ' ';
444*9880d681SAndroid Build Coastguard Worker#  if HAVE_CXXABI_H
445*9880d681SAndroid Build Coastguard Worker      int res;
446*9880d681SAndroid Build Coastguard Worker      char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res);
447*9880d681SAndroid Build Coastguard Worker#  else
448*9880d681SAndroid Build Coastguard Worker      char* d = NULL;
449*9880d681SAndroid Build Coastguard Worker#  endif
450*9880d681SAndroid Build Coastguard Worker      if (!d) OS << dlinfo.dli_sname;
451*9880d681SAndroid Build Coastguard Worker      else    OS << d;
452*9880d681SAndroid Build Coastguard Worker      free(d);
453*9880d681SAndroid Build Coastguard Worker
454*9880d681SAndroid Build Coastguard Worker      // FIXME: When we move to C++11, use %t length modifier. It's not in
455*9880d681SAndroid Build Coastguard Worker      // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of
456*9880d681SAndroid Build Coastguard Worker      // the stack offset for a stack dump isn't likely to cause any problems.
457*9880d681SAndroid Build Coastguard Worker      OS << format(" + %u",(unsigned)((char*)StackTrace[i]-
458*9880d681SAndroid Build Coastguard Worker                                      (char*)dlinfo.dli_saddr));
459*9880d681SAndroid Build Coastguard Worker    }
460*9880d681SAndroid Build Coastguard Worker    OS << '\n';
461*9880d681SAndroid Build Coastguard Worker  }
462*9880d681SAndroid Build Coastguard Worker#elif defined(HAVE_BACKTRACE)
463*9880d681SAndroid Build Coastguard Worker  backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
464*9880d681SAndroid Build Coastguard Worker#endif
465*9880d681SAndroid Build Coastguard Worker#endif
466*9880d681SAndroid Build Coastguard Worker}
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Workerstatic void PrintStackTraceSignalHandler(void *) {
469*9880d681SAndroid Build Coastguard Worker  PrintStackTrace(llvm::errs());
470*9880d681SAndroid Build Coastguard Worker}
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard Workervoid llvm::sys::DisableSystemDialogsOnCrash() {}
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
475*9880d681SAndroid Build Coastguard Worker/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
476*9880d681SAndroid Build Coastguard Workervoid llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
477*9880d681SAndroid Build Coastguard Worker                                             bool DisableCrashReporting) {
478*9880d681SAndroid Build Coastguard Worker  ::Argv0 = Argv0;
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker  AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
481*9880d681SAndroid Build Coastguard Worker
482*9880d681SAndroid Build Coastguard Worker#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
483*9880d681SAndroid Build Coastguard Worker  // Environment variable to disable any kind of crash dialog.
484*9880d681SAndroid Build Coastguard Worker  if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) {
485*9880d681SAndroid Build Coastguard Worker    mach_port_t self = mach_task_self();
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker    exception_mask_t mask = EXC_MASK_CRASH;
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard Worker    kern_return_t ret = task_set_exception_ports(self,
490*9880d681SAndroid Build Coastguard Worker                             mask,
491*9880d681SAndroid Build Coastguard Worker                             MACH_PORT_NULL,
492*9880d681SAndroid Build Coastguard Worker                             EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
493*9880d681SAndroid Build Coastguard Worker                             THREAD_STATE_NONE);
494*9880d681SAndroid Build Coastguard Worker    (void)ret;
495*9880d681SAndroid Build Coastguard Worker  }
496*9880d681SAndroid Build Coastguard Worker#endif
497*9880d681SAndroid Build Coastguard Worker}
498