Fix some copyright lines in folly/experimental/symbolizer/
[folly.git] / folly / experimental / symbolizer / Symbolizer.h
index 2cb8a9b73ab92b4e8b05b9462e4e4638eadfbf18..2c906b6e39bdbec036a19c00705e5f8a1530ea22 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2012-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <folly/FBString.h>
 #include <folly/Range.h>
 #include <folly/String.h>
-#include <folly/io/IOBuf.h>
+#include <folly/experimental/symbolizer/Dwarf.h>
 #include <folly/experimental/symbolizer/Elf.h>
 #include <folly/experimental/symbolizer/ElfCache.h>
-#include <folly/experimental/symbolizer/Dwarf.h>
 #include <folly/experimental/symbolizer/StackTrace.h>
+#include <folly/io/IOBuf.h>
 
 namespace folly {
 namespace symbolizer {
@@ -39,13 +39,16 @@ class Symbolizer;
  * Frame information: symbol name and location.
  */
 struct SymbolizedFrame {
-  SymbolizedFrame() { }
+  SymbolizedFrame() {}
 
-  void set(const std::shared_ptr<ElfFile>& file,
-           uintptr_t address,
-           Dwarf::LocationInfoMode mode);
+  void set(
+      const std::shared_ptr<ElfFile>& file,
+      uintptr_t address,
+      Dwarf::LocationInfoMode mode);
 
-  void clear() { *this = SymbolizedFrame(); }
+  void clear() {
+    *this = SymbolizedFrame();
+  }
 
   bool found = false;
   const char* name = nullptr;
@@ -64,7 +67,7 @@ struct SymbolizedFrame {
 
 template <size_t N>
 struct FrameArray {
-  FrameArray() { }
+  FrameArray() {}
 
   size_t frameCount = 0;
   uintptr_t addresses[N];
@@ -90,7 +93,7 @@ bool fixFrameArray(FrameArray<N>& fa, ssize_t n) {
     return false;
   }
 }
-}  // namespace detail
+} // namespace detail
 
 // Always inline these functions; they don't do much, and unittests rely
 // on them never showing up in a stack trace.
@@ -115,17 +118,19 @@ class Symbolizer {
       Dwarf::LocationInfoMode::FAST;
 
   explicit Symbolizer(Dwarf::LocationInfoMode mode = kDefaultLocationInfoMode)
-    : Symbolizer(nullptr, mode) {}
+      : Symbolizer(nullptr, mode) {}
 
-  explicit Symbolizer(ElfCacheBase* cache,
-                      Dwarf::LocationInfoMode mode = kDefaultLocationInfoMode);
+  explicit Symbolizer(
+      ElfCacheBase* cache,
+      Dwarf::LocationInfoMode mode = kDefaultLocationInfoMode);
 
   /**
    * Symbolize given addresses.
    */
-  void symbolize(const uintptr_t* addresses,
-                 SymbolizedFrame* frames,
-                 size_t frameCount);
+  void symbolize(
+      const uintptr_t* addresses,
+      SymbolizedFrame* frames,
+      size_t frameCount);
 
   template <size_t N>
   void symbolize(FrameArray<N>& fa) {
@@ -181,27 +186,30 @@ class SymbolizePrinter {
   /**
    * Print multiple addresses on separate lines.
    */
-  void println(const uintptr_t* addresses,
-               const SymbolizedFrame* frames,
-               size_t frameCount);
+  void println(
+      const uintptr_t* addresses,
+      const SymbolizedFrame* frames,
+      size_t frameCount);
 
   /**
    * Print a string, no endling newline.
    */
-  void print(StringPiece sp) { doPrint(sp); }
+  void print(StringPiece sp) {
+    doPrint(sp);
+  }
 
   /**
    * Print multiple addresses on separate lines, skipping the first
    * skip addresses.
    */
   template <size_t N>
-  void println(const FrameArray<N>& fa, size_t skip=0) {
+  void println(const FrameArray<N>& fa, size_t skip = 0) {
     if (skip < fa.frameCount) {
       println(fa.addresses + skip, fa.frames + skip, fa.frameCount - skip);
     }
   }
 
-  virtual ~SymbolizePrinter() { }
+  virtual ~SymbolizePrinter() {}
 
   enum Options {
     // Skip file and line information
@@ -226,9 +234,7 @@ class SymbolizePrinter {
 
  protected:
   explicit SymbolizePrinter(int options, bool isTty = false)
-    : options_(options),
-      isTty_(isTty) {
-  }
+      : options_(options), isTty_(isTty) {}
 
   const int options_;
   const bool isTty_;
@@ -255,7 +261,8 @@ class SymbolizePrinter {
  */
 class OStreamSymbolizePrinter : public SymbolizePrinter {
  public:
-  explicit OStreamSymbolizePrinter(std::ostream& out, int options=0);
+  explicit OStreamSymbolizePrinter(std::ostream& out, int options = 0);
+
  private:
   void doPrint(StringPiece sp) override;
   std::ostream& out_;
@@ -267,10 +274,10 @@ class OStreamSymbolizePrinter : public SymbolizePrinter {
  */
 class FDSymbolizePrinter : public SymbolizePrinter {
  public:
-  explicit FDSymbolizePrinter(int fd, int options=0,
-                              size_t bufferSize=0);
-  ~FDSymbolizePrinter();
+  explicit FDSymbolizePrinter(int fd, int options = 0, size_t bufferSize = 0);
+  ~FDSymbolizePrinter() override;
   void flush();
+
  private:
   void doPrint(StringPiece sp) override;
 
@@ -284,7 +291,8 @@ class FDSymbolizePrinter : public SymbolizePrinter {
  */
 class FILESymbolizePrinter : public SymbolizePrinter {
  public:
-  explicit FILESymbolizePrinter(FILE* file, int options=0);
+  explicit FILESymbolizePrinter(FILE* file, int options = 0);
+
  private:
   void doPrint(StringPiece sp) override;
   FILE* const file_ = nullptr;
@@ -296,11 +304,18 @@ class FILESymbolizePrinter : public SymbolizePrinter {
  */
 class StringSymbolizePrinter : public SymbolizePrinter {
  public:
-  explicit StringSymbolizePrinter(int options=0) : SymbolizePrinter(options) { }
+  explicit StringSymbolizePrinter(int options = 0)
+      : SymbolizePrinter(options) {}
 
-  std::string str() const { return buf_.toStdString(); }
-  const fbstring& fbstr() const { return buf_; }
-  fbstring moveFbString() { return std::move(buf_); }
+  std::string str() const {
+    return buf_.toStdString();
+  }
+  const fbstring& fbstr() const {
+    return buf_;
+  }
+  fbstring moveFbString() {
+    return std::move(buf_);
+  }
 
  private:
   void doPrint(StringPiece sp) override;
@@ -324,6 +339,7 @@ class StringSymbolizePrinter : public SymbolizePrinter {
 class StackTracePrinter {
  public:
   static constexpr size_t kDefaultMinSignalSafeElfCacheSize = 500;
+
   explicit StackTracePrinter(
       size_t minSignalSafeElfCacheSize = kDefaultMinSignalSafeElfCacheSize,
       int fd = STDERR_FILENO);
@@ -343,10 +359,13 @@ class StackTracePrinter {
   void flush();
 
  private:
+  static constexpr size_t kMaxStackTraceDepth = 100;
+
   int fd_;
   SignalSafeElfCache elfCache_;
   FDSymbolizePrinter printer_;
+  std::unique_ptr<FrameArray<kMaxStackTraceDepth>> addresses_;
 };
 
-}  // namespace symbolizer
-}  // namespace folly
+} // namespace symbolizer
+} // namespace folly