Fix a typo
[folly.git] / folly / experimental / symbolizer / Symbolizer.h
index 0000614114431f85c8ad87d53a03ee473efa2b95..bbbb1e7588e43a3dddf902b7905c779fb853f476 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 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.
  */
 
-#ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
-#define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
+#pragma once
 
+#include <array>
 #include <cstdint>
+#include <memory>
 #include <string>
-#include <unordered_map>
 
-#include "folly/FBString.h"
-#include "folly/Range.h"
-#include "folly/String.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/FBString.h>
+#include <folly/Range.h>
+#include <folly/String.h>
+#include <folly/io/IOBuf.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>
 
 namespace folly {
 namespace symbolizer {
@@ -38,21 +39,20 @@ class Symbolizer;
  * Frame information: symbol name and location.
  */
 struct SymbolizedFrame {
-  SymbolizedFrame() : found(false) { }
+  SymbolizedFrame() { }
 
   void set(const std::shared_ptr<ElfFile>& file, uintptr_t address);
   void clear() { *this = SymbolizedFrame(); }
 
-  bool isSignalFrame;
-  bool found;
-  StringPiece name;
+  bool found = false;
+  const char* name = nullptr;
   Dwarf::LocationInfo location;
 
   /**
    * Demangle the name and return it. Not async-signal-safe; allocates memory.
    */
   fbstring demangledName() const {
-    return demangle(name.fbstr().c_str());
+    return name ? demangle(name) : fbstring();
   }
  private:
   std::shared_ptr<ElfFile> file_;
@@ -60,9 +60,9 @@ struct SymbolizedFrame {
 
 template <size_t N>
 struct FrameArray {
-  FrameArray() : frameCount(0) { }
+  FrameArray() { }
 
-  size_t frameCount;
+  size_t frameCount = 0;
   uintptr_t addresses[N];
   SymbolizedFrame frames[N];
 };
@@ -130,11 +130,11 @@ class Symbolizer {
   }
 
  private:
-  ElfCacheBase* cache_;
+  ElfCacheBase* const cache_ = nullptr;
 };
 
 /**
- * Format one address in the way it's usually printer by SymbolizePrinter.
+ * Format one address in the way it's usually printed by SymbolizePrinter.
  * Async-signal-safe.
  */
 class AddressFormatter {
@@ -173,6 +173,11 @@ class SymbolizePrinter {
                const SymbolizedFrame* frames,
                size_t frameCount);
 
+  /**
+   * Print a string, no endling newline.
+   */
+  void print(StringPiece sp) { doPrint(sp); }
+
   /**
    * Print multiple addresses on separate lines, skipping the first
    * skip addresses.
@@ -198,9 +203,13 @@ class SymbolizePrinter {
 
     // Colorize output only if output is printed to a TTY (ANSI escape code)
     COLOR_IF_TTY = 1 << 3,
+
+    // Skip frame address information
+    NO_FRAME_ADDRESS = 1 << 4,
   };
 
-  enum Color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, WHITE, PURPLE };
+  // NOTE: enum values used as indexes in kColorMap.
+  enum Color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, WHITE, PURPLE, NUM };
   void color(Color c);
 
  protected:
@@ -215,6 +224,17 @@ class SymbolizePrinter {
  private:
   void printTerse(uintptr_t address, const SymbolizedFrame& frame);
   virtual void doPrint(StringPiece sp) = 0;
+
+  static constexpr std::array<const char*, Color::NUM> kColorMap = {{
+      "\x1B[0m",
+      "\x1B[31m",
+      "\x1B[32m",
+      "\x1B[33m",
+      "\x1B[34m",
+      "\x1B[36m",
+      "\x1B[37m",
+      "\x1B[35m",
+  }};
 };
 
 /**
@@ -235,10 +255,15 @@ class OStreamSymbolizePrinter : public SymbolizePrinter {
  */
 class FDSymbolizePrinter : public SymbolizePrinter {
  public:
-  explicit FDSymbolizePrinter(int fd, int options=0);
+  explicit FDSymbolizePrinter(int fd, int options=0,
+                              size_t bufferSize=0);
+  ~FDSymbolizePrinter();
+  void flush();
  private:
   void doPrint(StringPiece sp) override;
-  int fd_;
+
+  const int fd_;
+  std::unique_ptr<IOBuf> buffer_;
 };
 
 /**
@@ -250,7 +275,7 @@ class FILESymbolizePrinter : public SymbolizePrinter {
   explicit FILESymbolizePrinter(FILE* file, int options=0);
  private:
   void doPrint(StringPiece sp) override;
-  FILE* file_;
+  FILE* const file_ = nullptr;
 };
 
 /**
@@ -272,5 +297,3 @@ class StringSymbolizePrinter : public SymbolizePrinter {
 
 }  // namespace symbolizer
 }  // namespace folly
-
-#endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */