[llvm-commits] [llvm] r66076 - in /llvm/trunk: include/llvm/Support/PrettyStackTrace.h lib/Support/PrettyStackTrace.cpp

Chris Lattner sabre at nondot.org
Wed Mar 4 13:40:24 PST 2009


Author: lattner
Date: Wed Mar  4 15:40:23 2009
New Revision: 66076

URL: http://llvm.org/viewvc/llvm-project?rev=66076&view=rev
Log:
add some helper classes for building light-weight symbolic stack traces
that get printed when a program crashes.  This is the first step of many.

Added:
    llvm/trunk/include/llvm/Support/PrettyStackTrace.h
    llvm/trunk/lib/Support/PrettyStackTrace.cpp

Added: llvm/trunk/include/llvm/Support/PrettyStackTrace.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/PrettyStackTrace.h?rev=66076&view=auto

==============================================================================
--- llvm/trunk/include/llvm/Support/PrettyStackTrace.h (added)
+++ llvm/trunk/include/llvm/Support/PrettyStackTrace.h Wed Mar  4 15:40:23 2009
@@ -0,0 +1,65 @@
+//===- llvm/Support/PrettyStackTrace.h - Pretty Crash Handling --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the PrettyStackTraceEntry class, which is used to make
+// crashes give more contextual information about what the program was doing
+// when it crashed.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_PRETTYSTACKTRACE_H
+#define LLVM_SUPPORT_PRETTYSTACKTRACE_H
+
+namespace llvm {
+  class raw_ostream;
+  
+  /// PrettyStackTraceEntry - This class is used to represent a frame of the
+  /// "pretty" stack trace that is dumped when a program crashes. You can define
+  /// subclasses of this and declare them on the program stack: when they are 
+  /// constructed and destructed, they will add their symbolic frames to a
+  /// virtual stack trace.  This gets dumped out if the program crashes.
+  class PrettyStackTraceEntry {
+    const PrettyStackTraceEntry *NextEntry;
+    PrettyStackTraceEntry(const PrettyStackTraceEntry &); // DO NOT IMPLEMENT
+    void operator=(const PrettyStackTraceEntry&);         // DO NOT IMPLEMENT
+  public:
+    PrettyStackTraceEntry();
+    virtual ~PrettyStackTraceEntry();
+    
+    /// print - Emit information about this stack frame to OS.
+    virtual void print(raw_ostream &OS) const = 0;
+    
+    /// getNextEntry - Return the next entry in the list of frames.
+    const PrettyStackTraceEntry *getNextEntry() const { return NextEntry; }
+  };
+  
+  /// PrettyStackTraceString - This object prints a specified string (which
+  /// should not contain newlines) to the stream as the stack trace when a crash
+  /// occurs.
+  class PrettyStackTraceString : public PrettyStackTraceEntry {
+    const char *Str;
+  public:
+    PrettyStackTraceString(const char *str) : Str(str) {}
+    virtual void print(raw_ostream &OS) const;
+  };
+  
+  /// PrettyStackTraceProgram - This object prints a specified program arguments
+  /// to the stream as the stack trace when a crash occurs.
+  class PrettyStackTraceProgram : public PrettyStackTraceEntry {
+    int ArgC;
+    const char *const *ArgV;
+  public:
+    PrettyStackTraceProgram(int argc, const char * const*argv)
+      : ArgC(argc), ArgV(argv) {}
+    virtual void print(raw_ostream &OS) const;
+  };
+  
+} // end namespace llvm
+
+#endif

Added: llvm/trunk/lib/Support/PrettyStackTrace.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/PrettyStackTrace.cpp?rev=66076&view=auto

==============================================================================
--- llvm/trunk/lib/Support/PrettyStackTrace.cpp (added)
+++ llvm/trunk/lib/Support/PrettyStackTrace.cpp Wed Mar  4 15:40:23 2009
@@ -0,0 +1,69 @@
+//===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This file defines some helpful functions for dealing with the possibility of
+// Unix signals occuring while your program is running.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Signals.h"
+using namespace llvm;
+
+// FIXME: This should be thread local when llvm supports threads.
+static const PrettyStackTraceEntry *PrettyStackTraceHead = 0;
+
+/// CrashHandler - This callback is run if a fatal signal is delivered to the
+/// process, it prints the pretty stack trace.
+static void CrashHandler(void *Cookie) {
+  // If there are pretty stack frames registered, walk and emit them.
+  raw_ostream &OS = errs();
+  OS << "Stack dump:\n";
+  
+  unsigned i = 0;
+  for (const PrettyStackTraceEntry *Entry = PrettyStackTraceHead; Entry;
+       Entry = Entry->getNextEntry(), ++i) {
+    OS << i << ".\t";
+    Entry->print(OS);
+  }
+  OS.flush();
+}
+
+static bool RegisterCrashPrinter() {
+  sys::AddSignalHandler(CrashHandler, 0);
+  return false;
+}
+
+PrettyStackTraceEntry::PrettyStackTraceEntry() {
+  // The first time this is called, we register the crash printer.
+  static bool HandlerRegistered = RegisterCrashPrinter();
+  HandlerRegistered = HandlerRegistered;
+    
+  // Link ourselves.
+  NextEntry = PrettyStackTraceHead;
+  PrettyStackTraceHead = this;
+}
+
+PrettyStackTraceEntry::~PrettyStackTraceEntry() {
+  assert(PrettyStackTraceHead == this &&
+         "Pretty stack trace entry destruction is out of order");
+  PrettyStackTraceHead = getNextEntry();
+}
+
+void PrettyStackTraceString::print(raw_ostream &OS) const {
+  OS << Str << "\n";
+}
+
+void PrettyStackTraceProgram::print(raw_ostream &OS) const {
+  // Print the argument list.
+  for (unsigned i = 0, e = ArgC; i != e; ++i)
+    OS << ArgV[i] << ' ';
+  OS << '\n';
+}
\ No newline at end of file





More information about the llvm-commits mailing list