/*
- * 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.
*/
+#pragma once
-#ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
-#define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
-
+#include <array>
#include <cstdint>
+#include <memory>
#include <string>
-#include <unordered_map>
-#include "folly/FBString.h"
-#include "folly/Range.h"
-#include "folly/experimental/symbolizer/Elf.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 {
+class Symbolizer;
+
/**
* Frame information: symbol name and location.
- *
- * Note that both name and location are references in the Symbolizer object,
- * which must outlive this SymbolizedFrame object.
*/
struct SymbolizedFrame {
- SymbolizedFrame() : found(false) { }
- bool isSignalFrame;
- bool found;
- StringPiece name;
+ SymbolizedFrame() { }
+
+ void set(const std::shared_ptr<ElfFile>& file,
+ uintptr_t address,
+ Dwarf::LocationInfoMode mode);
+
+ void clear() { *this = SymbolizedFrame(); }
+
+ 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 name ? demangle(name) : fbstring();
+ }
+
+ private:
+ std::shared_ptr<ElfFile> file_;
};
template <size_t N>
struct FrameArray {
- FrameArray() : frameCount(0) { }
+ FrameArray() { }
- size_t frameCount;
+ size_t frameCount = 0;
uintptr_t addresses[N];
SymbolizedFrame frames[N];
};
* set frameCount to the actual frame count, which may be > N) and false
* on failure.
*/
+namespace detail {
template <size_t N>
-bool getStackTrace(FrameArray<N>& fa) {
- ssize_t n = getStackTrace(fa.addresses, N);
+bool fixFrameArray(FrameArray<N>& fa, ssize_t n) {
if (n != -1) {
fa.frameCount = n;
for (size_t i = 0; i < fa.frameCount; ++i) {
return false;
}
}
+} // namespace detail
+
+// Always inline these functions; they don't do much, and unittests rely
+// on them never showing up in a stack trace.
+template <size_t N>
+FOLLY_ALWAYS_INLINE bool getStackTrace(FrameArray<N>& fa);
+
+template <size_t N>
+inline bool getStackTrace(FrameArray<N>& fa) {
+ return detail::fixFrameArray(fa, getStackTrace(fa.addresses, N));
+}
+template <size_t N>
+FOLLY_ALWAYS_INLINE bool getStackTraceSafe(FrameArray<N>& fa);
+
+template <size_t N>
+inline bool getStackTraceSafe(FrameArray<N>& fa) {
+ return detail::fixFrameArray(fa, getStackTraceSafe(fa.addresses, N));
+}
class Symbolizer {
public:
- Symbolizer() : fileCount_(0) { }
+ static constexpr Dwarf::LocationInfoMode kDefaultLocationInfoMode =
+ Dwarf::LocationInfoMode::FAST;
+
+ explicit Symbolizer(Dwarf::LocationInfoMode mode = kDefaultLocationInfoMode)
+ : Symbolizer(nullptr, mode) {}
+
+ explicit Symbolizer(ElfCacheBase* cache,
+ Dwarf::LocationInfoMode mode = kDefaultLocationInfoMode);
/**
* Symbolize given addresses.
}
private:
- // We can't allocate memory, so we'll preallocate room.
- // "1023 shared libraries should be enough for everyone"
- static constexpr size_t kMaxFiles = 1024;
- size_t fileCount_;
- ElfFile files_[kMaxFiles];
+ ElfCacheBase* const cache_;
+ const Dwarf::LocationInfoMode mode_;
+};
+
+/**
+ * Format one address in the way it's usually printed by SymbolizePrinter.
+ * Async-signal-safe.
+ */
+class AddressFormatter {
+ public:
+ AddressFormatter();
+
+ /**
+ * Format the address. Returns an internal buffer.
+ */
+ StringPiece format(uintptr_t address);
+
+ private:
+ static constexpr char bufTemplate[] = " @ 0000000000000000";
+ char buf_[sizeof(bufTemplate)];
};
/**
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.
// As terse as it gets: function name if found, address otherwise
TERSE = 1 << 1,
+
+ // Always colorize output (ANSI escape code)
+ COLOR = 1 << 2,
+
+ // 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,
};
+ // NOTE: enum values used as indexes in kColorMap.
+ enum Color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, WHITE, PURPLE, NUM };
+ void color(Color c);
+
protected:
- explicit SymbolizePrinter(int options) : options_(options) { }
+ explicit SymbolizePrinter(int options, bool isTty = false)
+ : options_(options),
+ isTty_(isTty) {
+ }
+
const int options_;
+ const bool isTty_;
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",
+ }};
};
/**
*/
class OStreamSymbolizePrinter : public SymbolizePrinter {
public:
- explicit OStreamSymbolizePrinter(std::ostream& out, int options=0)
- : SymbolizePrinter(options),
- out_(out) { }
+ explicit OStreamSymbolizePrinter(std::ostream& out, int options=0);
private:
void doPrint(StringPiece sp) override;
std::ostream& out_;
*/
class FDSymbolizePrinter : public SymbolizePrinter {
public:
- explicit FDSymbolizePrinter(int fd, int options=0)
- : SymbolizePrinter(options),
- fd_(fd) { }
+ 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_;
};
/**
*/
class FILESymbolizePrinter : public SymbolizePrinter {
public:
- explicit FILESymbolizePrinter(FILE* file, int options=0)
- : SymbolizePrinter(options),
- file_(file) { }
+ explicit FILESymbolizePrinter(FILE* file, int options=0);
private:
void doPrint(StringPiece sp) override;
- FILE* file_;
+ FILE* const file_ = nullptr;
};
/**
} // namespace symbolizer
} // namespace folly
-
-#endif /* FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ */
-