Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 1 | //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 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. |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "llvm-c/Core.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/Config/config.h" // Get autoconf configuration settings |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Signals.h" |
Nick Lewycky | 4e06def | 2013-03-26 01:27:52 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Watchdog.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | e9c1bb6 | 2010-06-22 21:01:04 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef HAVE_CRASHREPORTERCLIENT_H |
| 24 | #include <CrashReporterClient.h> |
| 25 | #endif |
| 26 | |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 29 | // We need a thread local pointer to manage the stack of our stack trace |
| 30 | // objects, but we *really* cannot tolerate destructors running and do not want |
| 31 | // to pay any overhead of synchronizing. As a consequence, we use a raw |
| 32 | // thread-local variable. Some day, we should be able to use a limited subset |
| 33 | // of C++11's thread_local, but compilers aren't up for it today. |
| 34 | // FIXME: This should be moved to a Compiler.h abstraction. |
| 35 | #ifdef _MSC_VER // MSVC supports this with a __declspec. |
| 36 | static __declspec(thread) const PrettyStackTraceEntry |
| 37 | *PrettyStackTraceHead = nullptr; |
| 38 | #else // Clang, GCC, and all compatible compilers tend to use __thread. |
| 39 | static __thread const PrettyStackTraceEntry *PrettyStackTraceHead = nullptr; |
| 40 | #endif |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8d0fe8c | 2009-03-05 07:03:49 +0000 | [diff] [blame] | 42 | static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){ |
| 43 | unsigned NextID = 0; |
| 44 | if (Entry->getNextEntry()) |
| 45 | NextID = PrintStack(Entry->getNextEntry(), OS); |
| 46 | OS << NextID << ".\t"; |
Nick Lewycky | 4e06def | 2013-03-26 01:27:52 +0000 | [diff] [blame] | 47 | { |
| 48 | sys::Watchdog W(5); |
| 49 | Entry->print(OS); |
| 50 | } |
Chris Lattner | 8d0fe8c | 2009-03-05 07:03:49 +0000 | [diff] [blame] | 51 | |
| 52 | return NextID+1; |
| 53 | } |
| 54 | |
Chris Lattner | e41a434 | 2009-03-06 07:19:54 +0000 | [diff] [blame] | 55 | /// PrintCurStackTrace - Print the current stack trace to the specified stream. |
| 56 | static void PrintCurStackTrace(raw_ostream &OS) { |
Chris Lattner | 8d0fe8c | 2009-03-05 07:03:49 +0000 | [diff] [blame] | 57 | // Don't print an empty trace. |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 58 | if (!PrettyStackTraceHead) return; |
Chris Lattner | 8d0fe8c | 2009-03-05 07:03:49 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 60 | // If there are pretty stack frames registered, walk and emit them. |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 61 | OS << "Stack dump:\n"; |
| 62 | |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 63 | PrintStack(PrettyStackTraceHead, OS); |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 64 | OS.flush(); |
| 65 | } |
| 66 | |
Eric Christopher | 51f29083 | 2010-06-28 18:25:51 +0000 | [diff] [blame] | 67 | // Integrate with crash reporter libraries. |
Eric Christopher | ca46673 | 2010-12-03 07:45:22 +0000 | [diff] [blame] | 68 | #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H |
Eric Christopher | 51f29083 | 2010-06-28 18:25:51 +0000 | [diff] [blame] | 69 | // If any clients of llvm try to link to libCrashReporterClient.a themselves, |
| 70 | // only one crash info struct will be used. |
Eric Christopher | 7f103a2 | 2010-06-28 18:33:48 +0000 | [diff] [blame] | 71 | extern "C" { |
Eric Christopher | 51f29083 | 2010-06-28 18:25:51 +0000 | [diff] [blame] | 72 | CRASH_REPORTER_CLIENT_HIDDEN |
| 73 | struct crashreporter_annotations_t gCRAnnotations |
| 74 | __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) |
Eric Christopher | c4b9b52 | 2011-10-05 05:00:26 +0000 | [diff] [blame] | 75 | = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 }; |
Eric Christopher | 7f103a2 | 2010-06-28 18:33:48 +0000 | [diff] [blame] | 76 | } |
Eric Christopher | ca46673 | 2010-12-03 07:45:22 +0000 | [diff] [blame] | 77 | #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO |
Daniel Dunbar | c120ffe | 2010-05-20 23:50:19 +0000 | [diff] [blame] | 78 | static const char *__crashreporter_info__ = 0; |
| 79 | asm(".desc ___crashreporter_info__, 0x10"); |
Chris Lattner | e41a434 | 2009-03-06 07:19:54 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | |
| 83 | /// CrashHandler - This callback is run if a fatal signal is delivered to the |
| 84 | /// process, it prints the pretty stack trace. |
Eric Christopher | 87947f7 | 2010-08-08 00:00:34 +0000 | [diff] [blame] | 85 | static void CrashHandler(void *) { |
Chris Lattner | e41a434 | 2009-03-06 07:19:54 +0000 | [diff] [blame] | 86 | #ifndef __APPLE__ |
| 87 | // On non-apple systems, just emit the crash stack trace to stderr. |
| 88 | PrintCurStackTrace(errs()); |
| 89 | #else |
| 90 | // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also |
| 91 | // put it into __crashreporter_info__. |
| 92 | SmallString<2048> TmpStr; |
| 93 | { |
| 94 | raw_svector_ostream Stream(TmpStr); |
| 95 | PrintCurStackTrace(Stream); |
| 96 | } |
| 97 | |
| 98 | if (!TmpStr.empty()) { |
Eric Christopher | ca46673 | 2010-12-03 07:45:22 +0000 | [diff] [blame] | 99 | #ifdef HAVE_CRASHREPORTERCLIENT_H |
Eric Christopher | 87947f7 | 2010-08-08 00:00:34 +0000 | [diff] [blame] | 100 | // Cast to void to avoid warning. |
| 101 | (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str()); |
Eric Christopher | ca46673 | 2010-12-03 07:45:22 +0000 | [diff] [blame] | 102 | #elif HAVE_CRASHREPORTER_INFO |
| 103 | __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str()); |
Eric Christopher | e9c1bb6 | 2010-06-22 21:01:04 +0000 | [diff] [blame] | 104 | #endif |
Daniel Dunbar | 8b0b115 | 2009-08-19 20:07:03 +0000 | [diff] [blame] | 105 | errs() << TmpStr.str(); |
Chris Lattner | e41a434 | 2009-03-06 07:19:54 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #endif |
| 109 | } |
| 110 | |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 111 | PrettyStackTraceEntry::PrettyStackTraceEntry() { |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 112 | // Link ourselves. |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 113 | NextEntry = PrettyStackTraceHead; |
| 114 | PrettyStackTraceHead = this; |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | PrettyStackTraceEntry::~PrettyStackTraceEntry() { |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 118 | assert(PrettyStackTraceHead == this && |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 119 | "Pretty stack trace entry destruction is out of order"); |
Chandler Carruth | 16b670e | 2015-01-28 09:52:14 +0000 | [diff] [blame] | 120 | PrettyStackTraceHead = getNextEntry(); |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void PrettyStackTraceString::print(raw_ostream &OS) const { |
| 124 | OS << Str << "\n"; |
| 125 | } |
| 126 | |
| 127 | void PrettyStackTraceProgram::print(raw_ostream &OS) const { |
Chris Lattner | 2674eb4 | 2009-03-05 06:51:42 +0000 | [diff] [blame] | 128 | OS << "Program arguments: "; |
Chris Lattner | 68061d5 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 129 | // Print the argument list. |
| 130 | for (unsigned i = 0, e = ArgC; i != e; ++i) |
| 131 | OS << ArgV[i] << ' '; |
| 132 | OS << '\n'; |
Chris Lattner | 2674eb4 | 2009-03-05 06:51:42 +0000 | [diff] [blame] | 133 | } |
Filip Pizlo | 9f50ccd | 2013-11-03 00:29:47 +0000 | [diff] [blame] | 134 | |
Filip Pizlo | c10ca90 | 2013-11-04 02:22:25 +0000 | [diff] [blame] | 135 | static bool RegisterCrashPrinter() { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 136 | sys::AddSignalHandler(CrashHandler, nullptr); |
Filip Pizlo | c10ca90 | 2013-11-04 02:22:25 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | void llvm::EnablePrettyStackTrace() { |
| 141 | // The first time this is called, we register the crash printer. |
| 142 | static bool HandlerRegistered = RegisterCrashPrinter(); |
| 143 | (void)HandlerRegistered; |
| 144 | } |
| 145 | |
| 146 | void LLVMEnablePrettyStackTrace() { |
| 147 | EnablePrettyStackTrace(); |
Filip Pizlo | 9f50ccd | 2013-11-03 00:29:47 +0000 | [diff] [blame] | 148 | } |