fall back to .debug_info scan in fatal signal handler
[folly.git] / folly / experimental / exception_tracer / ExceptionTracer.cpp
index 533eb009a8567d69c4a7296b089207e608fab825..d2ec8f64a6b2d0e22d93def0c413b569c134866f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-
-#include "folly/experimental/exception_tracer/ExceptionTracer.h"
+#include <folly/experimental/exception_tracer/ExceptionTracer.h>
 
 #include <dlfcn.h>
 #include <exception>
 #include <iostream>
 #include <glog/logging.h>
 
-#include "folly/experimental/exception_tracer/ExceptionAbi.h"
-#include "folly/experimental/exception_tracer/StackTrace.h"
-#include "folly/experimental/symbolizer/Symbolizer.h"
-#include "folly/String.h"
+#include <folly/experimental/exception_tracer/ExceptionAbi.h>
+#include <folly/experimental/exception_tracer/StackTrace.h>
+#include <folly/experimental/symbolizer/Symbolizer.h>
+#include <folly/String.h>
 
 namespace {
 
@@ -34,7 +33,7 @@ using namespace ::folly::symbolizer;
 using namespace __cxxabiv1;
 
 extern "C" {
-StackTraceStack* getExceptionStackTraceStack(void) __attribute__((weak));
+StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
 typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void);
 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
 }
@@ -45,6 +44,14 @@ namespace folly {
 namespace exception_tracer {
 
 std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
+  printExceptionInfo(out, info, SymbolizePrinter::COLOR_IF_TTY);
+  return out;
+}
+
+void printExceptionInfo(
+    std::ostream& out,
+    const ExceptionInfo& info,
+    int options) {
   out << "Exception type: ";
   if (info.type) {
     out << folly::demangle(*info.type);
@@ -55,29 +62,31 @@ std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
       << (info.frames.size() == 1 ? " frame" : " frames")
       << ")\n";
   try {
-    ssize_t frameCount = info.frames.size();
-    // Skip our own internal frames
-    static constexpr size_t skip = 3;
+    size_t frameCount = info.frames.size();
 
-    if (frameCount > skip) {
-      auto addresses = info.frames.data() + skip;
-      frameCount -= skip;
+    // Skip our own internal frames
+    static constexpr size_t kInternalFramesNumber = 3;
+    if (frameCount > kInternalFramesNumber) {
+      auto addresses = info.frames.data() + kInternalFramesNumber;
+      frameCount -= kInternalFramesNumber;
 
       std::vector<SymbolizedFrame> frames;
       frames.resize(frameCount);
 
-      Symbolizer symbolizer;
+      Symbolizer symbolizer(
+          (options & SymbolizePrinter::NO_FILE_AND_LINE)
+              ? Dwarf::LocationInfoMode::DISABLED
+              : Symbolizer::kDefaultLocationInfoMode);
       symbolizer.symbolize(addresses, frames.data(), frameCount);
 
-      OStreamSymbolizePrinter osp(out);
-      osp.print(addresses, frames.data(), frameCount);
+      OStreamSymbolizePrinter osp(out, options);
+      osp.println(addresses, frames.data(), frameCount);
     }
   } catch (const std::exception& e) {
     out << "\n !! caught " << folly::exceptionStr(e) << "\n";
   } catch (...) {
     out << "\n !!! caught unexpected exception\n";
   }
-  return out;
 }
 
 namespace {
@@ -206,4 +215,3 @@ void installHandlers() {
 
 }  // namespace exception_tracer
 }  // namespace folly
-