#include <limits.h>
#include <cstdio>
#include <iostream>
-#include <map>
#ifdef __GNUC__
#include <ext/stdio_filebuf.h>
} // namespace
constexpr char AddressFormatter::bufTemplate[];
+constexpr std::array<const char*, SymbolizePrinter::Color::NUM>
+ SymbolizePrinter::kColorMap;
AddressFormatter::AddressFormatter() {
memcpy(buf_, bufTemplate, sizeof(buf_));
}
void SymbolizePrinter::color(SymbolizePrinter::Color color) {
- static const std::map<SymbolizePrinter::Color, std::string> kColorMap = {
- { SymbolizePrinter::Color::DEFAULT, "\x1B[0m" },
- { SymbolizePrinter::Color::RED, "\x1B[31m" },
- { SymbolizePrinter::Color::GREEN, "\x1B[32m" },
- { SymbolizePrinter::Color::YELLOW, "\x1B[33m" },
- { SymbolizePrinter::Color::BLUE, "\x1B[34m" },
- { SymbolizePrinter::Color::CYAN, "\x1B[36m" },
- { SymbolizePrinter::Color::WHITE, "\x1B[37m" },
- { SymbolizePrinter::Color::PURPLE, "\x1B[35m" },
- };
-
if ((options_ & COLOR) == 0 &&
((options_ & COLOR_IF_TTY) == 0 || !isTty_)) {
return;
}
- auto it = kColorMap.find(color);
- if (it == kColorMap.end()) {
+ if (color < 0 || color >= kColorMap.size()) {
return;
}
- doPrint(it->second);
+ doPrint(kColorMap[color]);
}
void SymbolizePrinter::println(uintptr_t address,
#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>
* Frame information: symbol name and location.
*/
struct SymbolizedFrame {
- SymbolizedFrame() : found(false), name(nullptr) { }
+ SymbolizedFrame() { }
void set(const std::shared_ptr<ElfFile>& file, uintptr_t address);
void clear() { *this = SymbolizedFrame(); }
- bool isSignalFrame;
- bool found;
- const char* name;
+ bool found = false;
+ const char* name = nullptr;
Dwarf::LocationInfo location;
/**
template <size_t N>
struct FrameArray {
- FrameArray() : frameCount(0) { }
+ FrameArray() { }
- size_t frameCount;
+ size_t frameCount = 0;
uintptr_t addresses[N];
SymbolizedFrame frames[N];
};
}
private:
- ElfCacheBase* cache_;
+ ElfCacheBase* const cache_ = nullptr;
};
/**
COLOR_IF_TTY = 1 << 3,
};
- 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:
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",
+ }};
};
/**
private:
void doPrint(StringPiece sp) override;
- int fd_;
+ const int fd_;
std::unique_ptr<IOBuf> buffer_;
};
explicit FILESymbolizePrinter(FILE* file, int options=0);
private:
void doPrint(StringPiece sp) override;
- FILE* file_;
+ FILE* const file_ = nullptr;
};
/**