proxygen
folly::exception_tracer Namespace Reference

Namespaces

 detail
 

Classes

struct  ExceptionInfo
 
struct  ExceptionStats
 
struct  StackTrace
 
class  StackTraceStack
 

Typedefs

using CxaThrowType = decltype(&detail::dummyCxaThrow)
 
using CxaBeginCatchType = decltype(&detail::dummyCxaBeginCatch)
 
using CxaRethrowType = decltype(&detail::dummyCxaRethrow)
 
using CxaEndCatchType = decltype(&detail::dummyCxaEndCatch)
 
using RethrowExceptionType = decltype(&detail::dummyRethrowException)
 

Functions

std::vector< ExceptionStatsgetExceptionStatistics ()
 
std::ostream & operator<< (std::ostream &out, const ExceptionStats &stats)
 
std::ostream & operator<< (std::ostream &out, const ExceptionInfo &info)
 
void printExceptionInfo (std::ostream &out, const ExceptionInfo &info, int options)
 
std::vector< ExceptionInfogetCurrentExceptions ()
 
void installHandlers ()
 
CallbackHolder< CxaThrowType > & getCxaThrowCallbacks ()
 
void registerCxaThrowCallback (CxaThrowType callback)
 
CallbackHolder< CxaBeginCatchType > & getCxaBeginCatchCallbacks ()
 
void registerCxaBeginCatchCallback (CxaBeginCatchType callback)
 
CallbackHolder< CxaRethrowType > & getCxaRethrowCallbacks ()
 
void registerCxaRethrowCallback (CxaRethrowType callback)
 
CallbackHolder< CxaEndCatchType > & getCxaEndCatchCallbacks ()
 
void registerCxaEndCatchCallback (CxaEndCatchType callback)
 
CallbackHolder< RethrowExceptionType > & getRethrowExceptionCallbacks ()
 
void registerRethrowExceptionCallback (RethrowExceptionType callback)
 

Variables

constexpr size_t kMaxFrames = 500
 

Typedef Documentation

Definition at line 40 of file ExceptionTracerLib.h.

Definition at line 42 of file ExceptionTracerLib.h.

Definition at line 41 of file ExceptionTracerLib.h.

Definition at line 39 of file ExceptionTracerLib.h.

Function Documentation

std::vector< ExceptionInfo > folly::exception_tracer::getCurrentExceptions ( )

Get current exceptions being handled. front() is the most recent exception. There should be at most one unless rethrowing.

Definition at line 112 of file ExceptionTracer.cpp.

References __cxxabiv1::__cxa_get_globals(), folly::exception_tracer::StackTrace::addresses, __cxxabiv1::__cxa_eh_globals::caughtExceptions, folly::demangle(), folly::DFATAL, folly::exception_tracer::StackTrace::frameCount, folly::exception_tracer::ExceptionInfo::frames, getExceptionStackTraceStack(), deadlock::info(), folly::gen::move, folly::exception_tracer::StackTraceStack::next(), prefix(), folly::exception_tracer::StackTraceStack::top(), folly::exception_tracer::ExceptionInfo::type, and folly::WARNING.

Referenced by dumpExceptions(), and loop().

112  {
113  struct Once {
114  Once() {
115  // See if linked in with us (getExceptionStackTraceStack is weak)
116  getExceptionStackTraceStackFn = getExceptionStackTraceStack;
117 
118  if (!getExceptionStackTraceStackFn) {
119  // Nope, see if it's in a shared library
120  getExceptionStackTraceStackFn = (GetExceptionStackTraceStackType)dlsym(
121  RTLD_NEXT, "getExceptionStackTraceStack");
122  }
123  }
124  };
125  static Once once;
126 
127  std::vector<ExceptionInfo> exceptions;
128  auto currentException = __cxa_get_globals()->caughtExceptions;
129  if (!currentException) {
130  return exceptions;
131  }
132 
133  StackTraceStack* traceStack = nullptr;
134  if (!getExceptionStackTraceStackFn) {
135  static bool logged = false;
136  if (!logged) {
137  LOG(WARNING)
138  << "Exception tracer library not linked, stack traces not available";
139  logged = true;
140  }
141  } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
142  static bool logged = false;
143  if (!logged) {
144  LOG(WARNING)
145  << "Exception stack trace invalid, stack traces not available";
146  logged = true;
147  }
148  }
149 
150  StackTrace* trace = traceStack ? traceStack->top() : nullptr;
151  while (currentException) {
152  ExceptionInfo info;
153  // Dependent exceptions (thrown via std::rethrow_exception) aren't
154  // standard ABI __cxa_exception objects, and are correctly labeled as
155  // such in the exception_class field. We could try to extract the
156  // primary exception type in horribly hacky ways, but, for now, nullptr.
157  info.type = isAbiCppException(currentException)
158  ? currentException->exceptionType
159  : nullptr;
160 
161  if (traceStack) {
162  LOG_IF(DFATAL, !trace)
163  << "Invalid trace stack for exception of type: "
164  << (info.type ? folly::demangle(*info.type) : "null");
165 
166  if (!trace) {
167  return {};
168  }
169 
170  info.frames.assign(
171  trace->addresses, trace->addresses + trace->frameCount);
172  trace = traceStack->next(trace);
173  }
174  currentException = currentException->nextException;
175  exceptions.push_back(std::move(info));
176  }
177 
178  LOG_IF(DFATAL, trace) << "Invalid trace stack!";
179 
180  return exceptions;
181 }
def info()
Definition: deadlock.py:447
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
StackTraceStack * getExceptionStackTraceStack()
__cxa_eh_globals * __cxa_get_globals(void) noexcept
__cxa_exception * caughtExceptions
Definition: ExceptionAbi.h:47
fbstring demangle(const char *name)
Definition: Demangle.cpp:111
CallbackHolder< CxaBeginCatchType>& folly::exception_tracer::getCxaBeginCatchCallbacks ( )

Definition at line 81 of file ExceptionTracerLib.cpp.

Referenced by __cxxabiv1::__cxa_begin_catch().

98 {
CallbackHolder< CxaEndCatchType>& folly::exception_tracer::getCxaEndCatchCallbacks ( )

Definition at line 83 of file ExceptionTracerLib.cpp.

Referenced by __cxxabiv1::__cxa_end_catch().

98 {
CallbackHolder< CxaRethrowType>& folly::exception_tracer::getCxaRethrowCallbacks ( )

Definition at line 82 of file ExceptionTracerLib.cpp.

Referenced by __cxxabiv1::__cxa_rethrow().

98 {
CallbackHolder< CxaThrowType>& folly::exception_tracer::getCxaThrowCallbacks ( )

Definition at line 80 of file ExceptionTracerLib.cpp.

Referenced by __cxxabiv1::__cxa_throw().

98 {
std::vector< ExceptionStats > folly::exception_tracer::getExceptionStatistics ( )

This function accumulates exception throwing statistics across all threads. Please note, that during call to this function, other threads might block on exception throws, so it should be called seldomly. All pef-thread statistics is being reset by the call.

Definition at line 67 of file ExceptionCounterLib.cpp.

References folly::exception_tracer::ExceptionStats::count, folly::gen::move, and folly::detail::rhs.

Referenced by TEST().

67  {
68  ExceptionStatsHolderType accumulator;
69  for (auto& threadStats : gExceptionStats.accessAllThreads()) {
70  threadStats.appendTo(accumulator);
71  }
72 
73  std::vector<ExceptionStats> result;
74  result.reserve(accumulator.size());
75  for (auto& item : accumulator) {
76  result.push_back(std::move(item.second));
77  }
78 
79  std::sort(
80  result.begin(),
81  result.end(),
82  [](const ExceptionStats& lhs, const ExceptionStats& rhs) {
83  return lhs.count > rhs.count;
84  });
85 
86  return result;
87 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
CallbackHolder< RethrowExceptionType>& folly::exception_tracer::getRethrowExceptionCallbacks ( )

Definition at line 84 of file ExceptionTracerLib.cpp.

Referenced by std::rethrow_exception().

98 {
void folly::exception_tracer::installHandlers ( )

Install the terminate / unexpected handlers to dump exceptions.

Definition at line 212 of file ExceptionTracer.cpp.

Referenced by getExceptionStackTraceStack().

212  {
213  struct Once {
214  Once() {
215  origTerminate = std::set_terminate(terminateHandler);
216  origUnexpected = std::set_unexpected(unexpectedHandler);
217  }
218  };
219  static Once once;
220 }
std::ostream & folly::exception_tracer::operator<< ( std::ostream &  out,
const ExceptionInfo info 
)

Definition at line 48 of file ExceptionTracer.cpp.

References printExceptionInfo().

48  {
49  printExceptionInfo(out, info, SymbolizePrinter::COLOR_IF_TTY);
50  return out;
51 }
def info()
Definition: deadlock.py:447
void printExceptionInfo(std::ostream &out, const ExceptionInfo &info, int options)
void folly::exception_tracer::printExceptionInfo ( std::ostream &  out,
const ExceptionInfo info,
int  options 
)

Definition at line 53 of file ExceptionTracer.cpp.

References folly::demangle(), folly::exceptionStr(), folly::exception_tracer::ExceptionInfo::frames, folly::exception_tracer::ExceptionInfo::type, uint64_t, and __cxxabiv1::__cxa_exception::unwindHeader.

Referenced by operator<<().

56  {
57  out << "Exception type: ";
58  if (info.type) {
59  out << folly::demangle(*info.type);
60  } else {
61  out << "(unknown type)";
62  }
63  out << " (" << info.frames.size()
64  << (info.frames.size() == 1 ? " frame" : " frames") << ")\n";
65  try {
66  size_t frameCount = info.frames.size();
67 
68  // Skip our own internal frames
69  static constexpr size_t kInternalFramesNumber = 3;
70  if (frameCount > kInternalFramesNumber) {
71  auto addresses = info.frames.data() + kInternalFramesNumber;
72  frameCount -= kInternalFramesNumber;
73 
74  std::vector<SymbolizedFrame> frames;
75  frames.resize(frameCount);
76 
77  Symbolizer symbolizer(
78  (options & SymbolizePrinter::NO_FILE_AND_LINE)
79  ? Dwarf::LocationInfoMode::DISABLED
80  : Symbolizer::kDefaultLocationInfoMode);
81  symbolizer.symbolize(addresses, frames.data(), frameCount);
82 
83  OStreamSymbolizePrinter osp(out, options);
84  osp.println(addresses, frames.data(), frameCount);
85  }
86  } catch (const std::exception& e) {
87  out << "\n !! caught " << folly::exceptionStr(e) << "\n";
88  } catch (...) {
89  out << "\n !!! caught unexpected exception\n";
90  }
91 }
def info()
Definition: deadlock.py:447
fbstring exceptionStr(const std::exception &e)
fbstring demangle(const char *name)
Definition: Demangle.cpp:111
void folly::exception_tracer::registerCxaBeginCatchCallback ( CxaBeginCatchType  callback)

Definition at line 81 of file ExceptionTracerLib.cpp.

Referenced by getExceptionStackTraceStack().

98 {
void folly::exception_tracer::registerCxaEndCatchCallback ( CxaEndCatchType  callback)

Definition at line 83 of file ExceptionTracerLib.cpp.

Referenced by getExceptionStackTraceStack().

98 {
void folly::exception_tracer::registerCxaRethrowCallback ( CxaRethrowType  callback)

Definition at line 82 of file ExceptionTracerLib.cpp.

Referenced by getExceptionStackTraceStack().

98 {
void folly::exception_tracer::registerCxaThrowCallback ( CxaThrowType  callback)

Definition at line 80 of file ExceptionTracerLib.cpp.

Referenced by getExceptionStackTraceStack(), and operator<<().

98 {
void folly::exception_tracer::registerRethrowExceptionCallback ( RethrowExceptionType  callback)

Definition at line 84 of file ExceptionTracerLib.cpp.

Referenced by getExceptionStackTraceStack().

98 {

Variable Documentation

constexpr size_t folly::exception_tracer::kMaxFrames = 500