Commit 2f57c3a9 authored by Lucian Grijincu's avatar Lucian Grijincu Committed by facebook-github-bot-4

folly: Symbolizer: don't allocate from function which is supposed to be signal safe

Reviewed By: @philippv, @dcolascione

Differential Revision: D2548023

fb-gh-sync-id: d2b0571d5db03e38ed17a5de6a13d2194a05b2b9
parent 4463e230
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <limits.h> #include <limits.h>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <map>
#ifdef __GNUC__ #ifdef __GNUC__
#include <ext/stdio_filebuf.h> #include <ext/stdio_filebuf.h>
...@@ -266,6 +265,8 @@ constexpr auto kFileColor = SymbolizePrinter::Color::DEFAULT; ...@@ -266,6 +265,8 @@ constexpr auto kFileColor = SymbolizePrinter::Color::DEFAULT;
} // namespace } // namespace
constexpr char AddressFormatter::bufTemplate[]; constexpr char AddressFormatter::bufTemplate[];
constexpr std::array<const char*, SymbolizePrinter::Color::NUM>
SymbolizePrinter::kColorMap;
AddressFormatter::AddressFormatter() { AddressFormatter::AddressFormatter() {
memcpy(buf_, bufTemplate, sizeof(buf_)); memcpy(buf_, bufTemplate, sizeof(buf_));
...@@ -348,26 +349,14 @@ void SymbolizePrinter::print(uintptr_t address, const SymbolizedFrame& frame) { ...@@ -348,26 +349,14 @@ void SymbolizePrinter::print(uintptr_t address, const SymbolizedFrame& frame) {
} }
void SymbolizePrinter::color(SymbolizePrinter::Color color) { 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 && if ((options_ & COLOR) == 0 &&
((options_ & COLOR_IF_TTY) == 0 || !isTty_)) { ((options_ & COLOR_IF_TTY) == 0 || !isTty_)) {
return; return;
} }
auto it = kColorMap.find(color); if (color < 0 || color >= kColorMap.size()) {
if (it == kColorMap.end()) {
return; return;
} }
doPrint(it->second); doPrint(kColorMap[color]);
} }
void SymbolizePrinter::println(uintptr_t address, void SymbolizePrinter::println(uintptr_t address,
......
...@@ -17,9 +17,10 @@ ...@@ -17,9 +17,10 @@
#ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
#define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_ #define FOLLY_EXPERIMENTAL_SYMBOLIZER_SYMBOLIZER_H_
#include <array>
#include <cstdint> #include <cstdint>
#include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <folly/FBString.h> #include <folly/FBString.h>
#include <folly/Range.h> #include <folly/Range.h>
...@@ -39,14 +40,13 @@ class Symbolizer; ...@@ -39,14 +40,13 @@ class Symbolizer;
* Frame information: symbol name and location. * Frame information: symbol name and location.
*/ */
struct SymbolizedFrame { struct SymbolizedFrame {
SymbolizedFrame() : found(false), name(nullptr) { } SymbolizedFrame() { }
void set(const std::shared_ptr<ElfFile>& file, uintptr_t address); void set(const std::shared_ptr<ElfFile>& file, uintptr_t address);
void clear() { *this = SymbolizedFrame(); } void clear() { *this = SymbolizedFrame(); }
bool isSignalFrame; bool found = false;
bool found; const char* name = nullptr;
const char* name;
Dwarf::LocationInfo location; Dwarf::LocationInfo location;
/** /**
...@@ -61,9 +61,9 @@ struct SymbolizedFrame { ...@@ -61,9 +61,9 @@ struct SymbolizedFrame {
template <size_t N> template <size_t N>
struct FrameArray { struct FrameArray {
FrameArray() : frameCount(0) { } FrameArray() { }
size_t frameCount; size_t frameCount = 0;
uintptr_t addresses[N]; uintptr_t addresses[N];
SymbolizedFrame frames[N]; SymbolizedFrame frames[N];
}; };
...@@ -131,7 +131,7 @@ class Symbolizer { ...@@ -131,7 +131,7 @@ class Symbolizer {
} }
private: private:
ElfCacheBase* cache_; ElfCacheBase* const cache_ = nullptr;
}; };
/** /**
...@@ -206,7 +206,8 @@ class SymbolizePrinter { ...@@ -206,7 +206,8 @@ class SymbolizePrinter {
COLOR_IF_TTY = 1 << 3, 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); void color(Color c);
protected: protected:
...@@ -221,6 +222,17 @@ class SymbolizePrinter { ...@@ -221,6 +222,17 @@ class SymbolizePrinter {
private: private:
void printTerse(uintptr_t address, const SymbolizedFrame& frame); void printTerse(uintptr_t address, const SymbolizedFrame& frame);
virtual void doPrint(StringPiece sp) = 0; 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",
}};
}; };
/** /**
...@@ -248,7 +260,7 @@ class FDSymbolizePrinter : public SymbolizePrinter { ...@@ -248,7 +260,7 @@ class FDSymbolizePrinter : public SymbolizePrinter {
private: private:
void doPrint(StringPiece sp) override; void doPrint(StringPiece sp) override;
int fd_; const int fd_;
std::unique_ptr<IOBuf> buffer_; std::unique_ptr<IOBuf> buffer_;
}; };
...@@ -261,7 +273,7 @@ class FILESymbolizePrinter : public SymbolizePrinter { ...@@ -261,7 +273,7 @@ class FILESymbolizePrinter : public SymbolizePrinter {
explicit FILESymbolizePrinter(FILE* file, int options=0); explicit FILESymbolizePrinter(FILE* file, int options=0);
private: private:
void doPrint(StringPiece sp) override; void doPrint(StringPiece sp) override;
FILE* file_; FILE* const file_ = nullptr;
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment