Chris Lattner | e97c733 | 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 |
| 11 | // Unix signals occuring while your program is running. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Support/PrettyStackTrace.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include "llvm/System/Signals.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | // FIXME: This should be thread local when llvm supports threads. |
| 21 | static const PrettyStackTraceEntry *PrettyStackTraceHead = 0; |
| 22 | |
Chris Lattner | d56786f | 2009-03-05 07:03:49 +0000 | [diff] [blame^] | 23 | static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){ |
| 24 | unsigned NextID = 0; |
| 25 | if (Entry->getNextEntry()) |
| 26 | NextID = PrintStack(Entry->getNextEntry(), OS); |
| 27 | OS << NextID << ".\t"; |
| 28 | Entry->print(OS); |
| 29 | |
| 30 | return NextID+1; |
| 31 | } |
| 32 | |
Chris Lattner | e97c733 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 33 | /// CrashHandler - This callback is run if a fatal signal is delivered to the |
| 34 | /// process, it prints the pretty stack trace. |
| 35 | static void CrashHandler(void *Cookie) { |
Chris Lattner | d56786f | 2009-03-05 07:03:49 +0000 | [diff] [blame^] | 36 | // Don't print an empty trace. |
| 37 | if (PrettyStackTraceHead == 0) return; |
| 38 | |
Chris Lattner | e97c733 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 39 | // If there are pretty stack frames registered, walk and emit them. |
| 40 | raw_ostream &OS = errs(); |
| 41 | OS << "Stack dump:\n"; |
| 42 | |
Chris Lattner | d56786f | 2009-03-05 07:03:49 +0000 | [diff] [blame^] | 43 | PrintStack(PrettyStackTraceHead, OS); |
Chris Lattner | e97c733 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 44 | OS.flush(); |
| 45 | } |
| 46 | |
| 47 | static bool RegisterCrashPrinter() { |
| 48 | sys::AddSignalHandler(CrashHandler, 0); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | PrettyStackTraceEntry::PrettyStackTraceEntry() { |
| 53 | // The first time this is called, we register the crash printer. |
| 54 | static bool HandlerRegistered = RegisterCrashPrinter(); |
| 55 | HandlerRegistered = HandlerRegistered; |
| 56 | |
| 57 | // Link ourselves. |
| 58 | NextEntry = PrettyStackTraceHead; |
| 59 | PrettyStackTraceHead = this; |
| 60 | } |
| 61 | |
| 62 | PrettyStackTraceEntry::~PrettyStackTraceEntry() { |
| 63 | assert(PrettyStackTraceHead == this && |
| 64 | "Pretty stack trace entry destruction is out of order"); |
| 65 | PrettyStackTraceHead = getNextEntry(); |
| 66 | } |
| 67 | |
| 68 | void PrettyStackTraceString::print(raw_ostream &OS) const { |
| 69 | OS << Str << "\n"; |
| 70 | } |
| 71 | |
| 72 | void PrettyStackTraceProgram::print(raw_ostream &OS) const { |
Chris Lattner | fcba7cd | 2009-03-05 06:51:42 +0000 | [diff] [blame] | 73 | OS << "Program arguments: "; |
Chris Lattner | e97c733 | 2009-03-04 21:40:23 +0000 | [diff] [blame] | 74 | // Print the argument list. |
| 75 | for (unsigned i = 0, e = ArgC; i != e; ++i) |
| 76 | OS << ArgV[i] << ' '; |
| 77 | OS << '\n'; |
Chris Lattner | fcba7cd | 2009-03-05 06:51:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |