diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2015-05-27 18:44:32 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2015-05-27 18:44:32 +0000 | 
| commit | 5a5ac124e1efaf208671f01c46edb15f29ed2a0b (patch) | |
| tree | a6140557876943cdd800ee997c9317283394b22c /lib/Support/Unix | |
| parent | f03b5bed27d0d2eafd68562ce14f8b5e3f1f0801 (diff) | |
Notes
Diffstat (limited to 'lib/Support/Unix')
| -rw-r--r-- | lib/Support/Unix/COM.inc | 27 | ||||
| -rw-r--r-- | lib/Support/Unix/Host.inc | 24 | ||||
| -rw-r--r-- | lib/Support/Unix/Process.inc | 16 | ||||
| -rw-r--r-- | lib/Support/Unix/Program.inc | 18 | ||||
| -rw-r--r-- | lib/Support/Unix/Signals.inc | 70 | 
5 files changed, 102 insertions, 53 deletions
| diff --git a/lib/Support/Unix/COM.inc b/lib/Support/Unix/COM.inc new file mode 100644 index 0000000000000..5b71de74ebf38 --- /dev/null +++ b/lib/Support/Unix/COM.inc @@ -0,0 +1,27 @@ +//===- llvm/Support/Unix/COM.inc - Unix COM Implementation -----*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the Unix portion of COM support. +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +//=== WARNING: Implementation here must contain only generic UNIX code that +//===          is guaranteed to work on *all* UNIX variants. +//===----------------------------------------------------------------------===// + +namespace llvm { +namespace sys { + +InitializeCOMRAII::InitializeCOMRAII(COMThreadingMode Threading, +                                     bool SpeedOverMemory) {} + +InitializeCOMRAII::~InitializeCOMRAII() {} +} +} diff --git a/lib/Support/Unix/Host.inc b/lib/Support/Unix/Host.inc index 519f2a1fe6877..457217125a222 100644 --- a/lib/Support/Unix/Host.inc +++ b/lib/Support/Unix/Host.inc @@ -35,29 +35,15 @@ static std::string getOSVersion() {  }  std::string sys::getDefaultTargetTriple() { -  StringRef TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE); -  std::pair<StringRef, StringRef> ArchSplit = TargetTripleString.split('-'); - -  // Normalize the arch, since the target triple may not actually match the -  // target. -  std::string Arch = ArchSplit.first; - -  std::string Triple(Arch); -  Triple += '-'; -  Triple += ArchSplit.second; - -  // Force i<N>86 to i386. -  if (Triple[0] == 'i' && isdigit(Triple[1]) && -      Triple[2] == '8' && Triple[3] == '6') -    Triple[1] = '3'; +  std::string TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE);    // On darwin, we want to update the version to match that of the    // target. -  std::string::size_type DarwinDashIdx = Triple.find("-darwin"); +  std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");    if (DarwinDashIdx != std::string::npos) { -    Triple.resize(DarwinDashIdx + strlen("-darwin")); -    Triple += getOSVersion(); +    TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); +    TargetTripleString += getOSVersion();    } -  return Triple::normalize(Triple); +  return Triple::normalize(TargetTripleString);  } diff --git a/lib/Support/Unix/Process.inc b/lib/Support/Unix/Process.inc index 530f86cf90702..df13bd2217390 100644 --- a/lib/Support/Unix/Process.inc +++ b/lib/Support/Unix/Process.inc @@ -39,6 +39,9 @@      !defined(__OpenBSD__) && !defined(__Bitrig__)  #include <malloc.h>  #endif +#if defined(HAVE_MALLCTL) +#include <malloc_np.h> +#endif  #ifdef HAVE_MALLOC_MALLOC_H  #include <malloc/malloc.h>  #endif @@ -98,6 +101,12 @@ size_t Process::GetMallocUsage() {    malloc_statistics_t Stats;    malloc_zone_statistics(malloc_default_zone(), &Stats);    return Stats.size_in_use;   // darwin +#elif defined(HAVE_MALLCTL) +  size_t alloc, sz; +  sz = sizeof(size_t); +  if (mallctl("stats.allocated", &alloc, &sz, NULL, 0) == 0) +    return alloc; +  return 0;  #elif defined(HAVE_SBRK)    // Note this is only an approximation and more closely resembles    // the value returned by mallinfo in the arena field. @@ -105,8 +114,7 @@ size_t Process::GetMallocUsage() {    char *EndOfMemory = (char*)sbrk(0);    if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1))      return EndOfMemory - StartOfMemory; -  else -    return 0; +  return 0;  #else  #warning Cannot get malloc info on this platform    return 0; @@ -191,8 +199,8 @@ public:    }  private: -  FDCloser(const FDCloser &) LLVM_DELETED_FUNCTION; -  void operator=(const FDCloser &) LLVM_DELETED_FUNCTION; +  FDCloser(const FDCloser &) = delete; +  void operator=(const FDCloser &) = delete;    int &FD;    bool KeepOpen; diff --git a/lib/Support/Unix/Program.inc b/lib/Support/Unix/Program.inc index 0f45df1a0da0e..5816fb812e9f0 100644 --- a/lib/Support/Unix/Program.inc +++ b/lib/Support/Unix/Program.inc @@ -18,10 +18,11 @@  #include "Unix.h"  #include "llvm/ADT/StringExtras.h" +#include "llvm/Config/config.h"  #include "llvm/Support/Compiler.h"  #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h"  #include "llvm/Support/raw_ostream.h" -#include <llvm/Config/config.h>  #if HAVE_SYS_STAT_H  #include <sys/stat.h>  #endif @@ -42,7 +43,18 @@  #define  _RESTRICT_KYWD  #endif  #include <spawn.h> -#if !defined(__APPLE__) + +#if defined(__APPLE__) +#include <TargetConditionals.h> +#endif + +#if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) +#define USE_NSGETENVIRON 1 +#else +#define USE_NSGETENVIRON 0 +#endif + +#if !USE_NSGETENVIRON    extern char **environ;  #else  #include <crt_externs.h> // _NSGetEnviron @@ -217,7 +229,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,      }      if (!envp) -#if !defined(__APPLE__) +#if !USE_NSGETENVIRON        envp = const_cast<const char **>(environ);  #else        // environ is missing in dylibs. diff --git a/lib/Support/Unix/Signals.inc b/lib/Support/Unix/Signals.inc index e8f4643dc8a76..bfe2a3a380edc 100644 --- a/lib/Support/Unix/Signals.inc +++ b/lib/Support/Unix/Signals.inc @@ -14,6 +14,7 @@  #include "Unix.h"  #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Format.h"  #include "llvm/Support/FileSystem.h"  #include "llvm/Support/FileUtilities.h"  #include "llvm/Support/ManagedStatic.h" @@ -111,6 +112,12 @@ static void RegisterHandler(int Signal) {  }  static void RegisterHandlers() { +  // We need to dereference the signals mutex during handler registration so +  // that we force its construction. This is to prevent the first use being +  // during handling an actual signal because you can't safely call new in a +  // signal handler. +  *SignalsMutex; +      // If the handlers are already registered, we're done.    if (NumRegisteredSignals != 0) return; @@ -132,6 +139,11 @@ static void UnregisterHandlers() {  /// NB: This must be an async signal safe function. It cannot allocate or free  /// memory, even in debug builds.  static void RemoveFilesToRemove() { +  // Avoid constructing ManagedStatic in the signal handler. +  // If FilesToRemove is not constructed, there are no files to remove. +  if (!FilesToRemove.isConstructed()) +    return; +    // We avoid iterators in case of debug iterators that allocate or release    // memory.    std::vector<std::string>& FilesToRemoveRef = *FilesToRemove; @@ -193,10 +205,11 @@ static RETSIGTYPE SignalHandler(int Sig) {    }    // Otherwise if it is a fault (like SEGV) run any handler. -  std::vector<std::pair<void (*)(void *), void *>>& CallBacksToRunRef = -      *CallBacksToRun; -  for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) -    CallBacksToRunRef[i].first(CallBacksToRunRef[i].second); +  if (CallBacksToRun.isConstructed()) { +    auto &CallBacksToRunRef = *CallBacksToRun; +    for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) +      CallBacksToRunRef[i].first(CallBacksToRunRef[i].second); +  }  #ifdef __s390__    // On S/390, certain signals are delivered with PSW Address pointing to @@ -324,7 +337,8 @@ static bool findModulesAndOffsets(void **StackTrace, int Depth,  }  #endif -static bool printSymbolizedStackTrace(void **StackTrace, int Depth, FILE *FD) { +static bool printSymbolizedStackTrace(void **StackTrace, int Depth, +                                      llvm::raw_ostream &OS) {    // FIXME: Subtract necessary number from StackTrace entries to turn return addresses    // into actual instruction addresses.    // Use llvm-symbolizer tool to symbolize the stack traces. @@ -382,7 +396,7 @@ static bool printSymbolizedStackTrace(void **StackTrace, int Depth, FILE *FD) {    int frame_no = 0;    for (int i = 0; i < Depth; i++) {      if (!Modules[i]) { -      fprintf(FD, "#%d %p\n", frame_no++, StackTrace[i]); +      OS << format("#%d %p\n", frame_no++, StackTrace[i]);        continue;      }      // Read pairs of lines (function name and file/line info) until we @@ -393,17 +407,17 @@ static bool printSymbolizedStackTrace(void **StackTrace, int Depth, FILE *FD) {        StringRef FunctionName = *CurLine++;        if (FunctionName.empty())          break; -      fprintf(FD, "#%d %p ", frame_no++, StackTrace[i]); +      OS << format("#%d %p ", frame_no++, StackTrace[i]);        if (!FunctionName.startswith("??")) -        fprintf(FD, "%s ", FunctionName.str().c_str()); +        OS << format("%s ", FunctionName.str().c_str());        if (CurLine == Lines.end())          return false;        StringRef FileLineInfo = *CurLine++;        if (!FileLineInfo.startswith("??")) -        fprintf(FD, "%s", FileLineInfo.str().c_str()); +        OS << format("%s", FileLineInfo.str().c_str());        else -        fprintf(FD, "(%s+%p)", Modules[i], (void *)Offsets[i]); -      fprintf(FD, "\n"); +        OS << format("(%s+%p)", Modules[i], (void *)Offsets[i]); +      OS << "\n";      }    }    return true; @@ -415,13 +429,13 @@ static bool printSymbolizedStackTrace(void **StackTrace, int Depth, FILE *FD) {  //  // On glibc systems we have the 'backtrace' function, which works nicely, but  // doesn't demangle symbols. -void llvm::sys::PrintStackTrace(FILE *FD) { +void llvm::sys::PrintStackTrace(raw_ostream &OS) {  #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)    static void* StackTrace[256];    // Use backtrace() to output a backtrace on Linux systems with glibc.    int depth = backtrace(StackTrace,                          static_cast<int>(array_lengthof(StackTrace))); -  if (printSymbolizedStackTrace(StackTrace, depth, FD)) +  if (printSymbolizedStackTrace(StackTrace, depth, OS))      return;  #if HAVE_DLFCN_H && __GNUG__    int width = 0; @@ -441,34 +455,34 @@ void llvm::sys::PrintStackTrace(FILE *FD) {      Dl_info dlinfo;      dladdr(StackTrace[i], &dlinfo); -    fprintf(FD, "%-2d", i); +    OS << format("%-2d", i);      const char* name = strrchr(dlinfo.dli_fname, '/'); -    if (!name) fprintf(FD, " %-*s", width, dlinfo.dli_fname); -    else       fprintf(FD, " %-*s", width, name+1); +    if (!name) OS << format(" %-*s", width, dlinfo.dli_fname); +    else       OS << format(" %-*s", width, name+1); -    fprintf(FD, " %#0*lx", -            (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]); +    OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2, +                 (unsigned long)StackTrace[i]);      if (dlinfo.dli_sname != nullptr) { -      fputc(' ', FD); +      OS << ' ';  #  if HAVE_CXXABI_H        int res;        char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res);  #  else        char* d = NULL;  #  endif -      if (!d) fputs(dlinfo.dli_sname, FD); -      else    fputs(d, FD); +      if (!d) OS << dlinfo.dli_sname; +      else    OS << d;        free(d);        // FIXME: When we move to C++11, use %t length modifier. It's not in        // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of        // the stack offset for a stack dump isn't likely to cause any problems. -      fprintf(FD, " + %u",(unsigned)((char*)StackTrace[i]- -                                     (char*)dlinfo.dli_saddr)); +      OS << format(" + %u",(unsigned)((char*)StackTrace[i]- +                                      (char*)dlinfo.dli_saddr));      } -    fputc('\n', FD); +    OS << '\n';    }  #else    backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); @@ -477,17 +491,19 @@ void llvm::sys::PrintStackTrace(FILE *FD) {  }  static void PrintStackTraceSignalHandler(void *) { -  PrintStackTrace(stderr); +  PrintStackTrace(llvm::errs());  } +void llvm::sys::DisableSystemDialogsOnCrash() {} +  /// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or  /// SIGSEGV) is delivered to the process, print a stack trace and then exit. -void llvm::sys::PrintStackTraceOnErrorSignal() { +void llvm::sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) {    AddSignalHandler(PrintStackTraceSignalHandler, nullptr);  #if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)    // Environment variable to disable any kind of crash dialog. -  if (getenv("LLVM_DISABLE_CRASH_REPORT")) { +  if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) {      mach_port_t self = mach_task_self();      exception_mask_t mask = EXC_MASK_CRASH; | 
