~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Support/PrettyStackTrace.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "llvm/System/ThreadLocal.h"
 
19
#include "llvm/ADT/SmallString.h"
 
20
using namespace llvm;
 
21
 
 
22
namespace llvm {
 
23
  bool DisablePrettyStackTrace = false;
 
24
}
 
25
 
 
26
// FIXME: This should be thread local when llvm supports threads.
 
27
static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
 
28
 
 
29
static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
 
30
  unsigned NextID = 0;
 
31
  if (Entry->getNextEntry())
 
32
    NextID = PrintStack(Entry->getNextEntry(), OS);
 
33
  OS << NextID << ".\t";
 
34
  Entry->print(OS);
 
35
  
 
36
  return NextID+1;
 
37
}
 
38
 
 
39
/// PrintCurStackTrace - Print the current stack trace to the specified stream.
 
40
static void PrintCurStackTrace(raw_ostream &OS) {
 
41
  // Don't print an empty trace.
 
42
  if (PrettyStackTraceHead.get() == 0) return;
 
43
  
 
44
  // If there are pretty stack frames registered, walk and emit them.
 
45
  OS << "Stack dump:\n";
 
46
  
 
47
  PrintStack(PrettyStackTraceHead.get(), OS);
 
48
  OS.flush();
 
49
}
 
50
 
 
51
// Integrate with crash reporter.
 
52
#ifdef __APPLE__
 
53
extern "C" const char *__crashreporter_info__;
 
54
const char *__crashreporter_info__ = 0;
 
55
#endif
 
56
 
 
57
 
 
58
/// CrashHandler - This callback is run if a fatal signal is delivered to the
 
59
/// process, it prints the pretty stack trace.
 
60
static void CrashHandler(void *Cookie) {
 
61
#ifndef __APPLE__
 
62
  // On non-apple systems, just emit the crash stack trace to stderr.
 
63
  PrintCurStackTrace(errs());
 
64
#else
 
65
  // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
 
66
  // put it into __crashreporter_info__.
 
67
  SmallString<2048> TmpStr;
 
68
  {
 
69
    raw_svector_ostream Stream(TmpStr);
 
70
    PrintCurStackTrace(Stream);
 
71
  }
 
72
  
 
73
  if (!TmpStr.empty()) {
 
74
    __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
 
75
    errs() << TmpStr.str();
 
76
  }
 
77
  
 
78
#endif
 
79
}
 
80
 
 
81
static bool RegisterCrashPrinter() {
 
82
  if (!DisablePrettyStackTrace)
 
83
    sys::AddSignalHandler(CrashHandler, 0);
 
84
  return false;
 
85
}
 
86
 
 
87
PrettyStackTraceEntry::PrettyStackTraceEntry() {
 
88
  // The first time this is called, we register the crash printer.
 
89
  static bool HandlerRegistered = RegisterCrashPrinter();
 
90
  HandlerRegistered = HandlerRegistered;
 
91
    
 
92
  // Link ourselves.
 
93
  NextEntry = PrettyStackTraceHead.get();
 
94
  PrettyStackTraceHead.set(this);
 
95
}
 
96
 
 
97
PrettyStackTraceEntry::~PrettyStackTraceEntry() {
 
98
  assert(PrettyStackTraceHead.get() == this &&
 
99
         "Pretty stack trace entry destruction is out of order");
 
100
  PrettyStackTraceHead.set(getNextEntry());
 
101
}
 
102
 
 
103
void PrettyStackTraceString::print(raw_ostream &OS) const {
 
104
  OS << Str << "\n";
 
105
}
 
106
 
 
107
void PrettyStackTraceProgram::print(raw_ostream &OS) const {
 
108
  OS << "Program arguments: ";
 
109
  // Print the argument list.
 
110
  for (unsigned i = 0, e = ArgC; i != e; ++i)
 
111
    OS << ArgV[i] << ' ';
 
112
  OS << '\n';
 
113
}
 
114