proxygen
folly::symbolizer::SymbolizePrinter Class Referenceabstract

#include <Symbolizer.h>

Inheritance diagram for folly::symbolizer::SymbolizePrinter:
folly::symbolizer::FDSymbolizePrinter folly::symbolizer::FILESymbolizePrinter folly::symbolizer::OStreamSymbolizePrinter folly::symbolizer::StringSymbolizePrinter

Public Types

enum  Options {
  NO_FILE_AND_LINE = 1 << 0, TERSE = 1 << 1, COLOR = 1 << 2, COLOR_IF_TTY = 1 << 3,
  NO_FRAME_ADDRESS = 1 << 4
}
 
enum  Color {
  DEFAULT, RED, GREEN, YELLOW,
  BLUE, CYAN, WHITE, PURPLE,
  NUM
}
 

Public Member Functions

void print (uintptr_t address, const SymbolizedFrame &frame)
 
void println (uintptr_t address, const SymbolizedFrame &frame)
 
void println (const uintptr_t *addresses, const SymbolizedFrame *frames, size_t frameCount)
 
void print (StringPiece sp)
 
template<size_t N>
void println (const FrameArray< N > &fa, size_t skip=0)
 
virtual void flush ()
 
virtual ~SymbolizePrinter ()
 
void color (Color c)
 

Protected Member Functions

 SymbolizePrinter (int options, bool isTty=false)
 

Protected Attributes

const int options_
 
const bool isTty_
 

Private Member Functions

void printTerse (uintptr_t address, const SymbolizedFrame &frame)
 
virtual void doPrint (StringPiece sp)=0
 

Static Private Attributes

static constexpr std::array< const char *, Color::NUM > kColorMap
 

Detailed Description

Print a list of symbolized addresses. Base class.

Definition at line 180 of file Symbolizer.h.

Member Enumeration Documentation

Enumerator
NO_FILE_AND_LINE 
TERSE 
COLOR 
COLOR_IF_TTY 
NO_FRAME_ADDRESS 

Definition at line 226 of file Symbolizer.h.

226  {
227  // Skip file and line information
228  NO_FILE_AND_LINE = 1 << 0,
229 
230  // As terse as it gets: function name if found, address otherwise
231  TERSE = 1 << 1,
232 
233  // Always colorize output (ANSI escape code)
234  COLOR = 1 << 2,
235 
236  // Colorize output only if output is printed to a TTY (ANSI escape code)
237  COLOR_IF_TTY = 1 << 3,
238 
239  // Skip frame address information
240  NO_FRAME_ADDRESS = 1 << 4,
241  };

Constructor & Destructor Documentation

virtual folly::symbolizer::SymbolizePrinter::~SymbolizePrinter ( )
inlinevirtual

Definition at line 224 of file Symbolizer.h.

224 {}
folly::symbolizer::SymbolizePrinter::SymbolizePrinter ( int  options,
bool  isTty = false 
)
inlineexplicitprotected

Definition at line 248 of file Symbolizer.h.

249  : options_(options), isTty_(isTty) {}

Member Function Documentation

void folly::symbolizer::SymbolizePrinter::color ( SymbolizePrinter::Color  color)

Definition at line 274 of file Symbolizer.cpp.

274  {
275  if ((options_ & COLOR) == 0 && ((options_ & COLOR_IF_TTY) == 0 || !isTty_)) {
276  return;
277  }
278  if (static_cast<size_t>(color) >= kColorMap.size()) { // catches underflow too
279  return;
280  }
282 }
static constexpr std::array< const char *, Color::NUM > kColorMap
Definition: Symbolizer.h:258
virtual void doPrint(StringPiece sp)=0
virtual void folly::symbolizer::SymbolizePrinter::doPrint ( StringPiece  sp)
privatepure virtual
virtual void folly::symbolizer::SymbolizePrinter::flush ( )
inlinevirtual

If output buffered inside this class, send it to the output stream, so that any output done in other ways appears after this.

Reimplemented in folly::symbolizer::FDSymbolizePrinter.

Definition at line 222 of file Symbolizer.h.

222 {}
void folly::symbolizer::SymbolizePrinter::print ( uintptr_t  address,
const SymbolizedFrame frame 
)

Print one address, no ending newline.

Definition at line 208 of file Symbolizer.cpp.

References folly::demangle(), folly::symbolizer::Dwarf::LocationInfo::file, folly::symbolizer::AddressFormatter::format(), folly::symbolizer::SymbolizedFrame::found, folly::symbolizer::Dwarf::LocationInfo::hasFileAndLine, folly::symbolizer::Dwarf::LocationInfo::hasMainFile, folly::symbolizer::Dwarf::LocationInfo::line, folly::symbolizer::SymbolizedFrame::location, folly::symbolizer::Dwarf::LocationInfo::mainFile, folly::symbolizer::SymbolizedFrame::name, SCOPE_EXIT, folly::symbolizer::Dwarf::Path::toBuffer(), uint32_t, and folly::uint64ToBufferUnsafe().

208  {
209  if (options_ & TERSE) {
210  printTerse(address, frame);
211  return;
212  }
213 
214  SCOPE_EXIT {
215  color(Color::DEFAULT);
216  };
217 
218  if (!(options_ & NO_FRAME_ADDRESS)) {
219  color(kAddressColor);
220 
221  AddressFormatter formatter;
222  doPrint(formatter.format(address));
223  }
224 
225  const char padBuf[] = " ";
226  folly::StringPiece pad(
227  padBuf, sizeof(padBuf) - 1 - (16 - 2 * sizeof(uintptr_t)));
228 
229  color(kFunctionColor);
230  if (!frame.found) {
231  doPrint(" (not found)");
232  return;
233  }
234 
235  if (!frame.name || frame.name[0] == '\0') {
236  doPrint(" (unknown)");
237  } else {
238  char demangledBuf[2048];
239  demangle(frame.name, demangledBuf, sizeof(demangledBuf));
240  doPrint(" ");
241  doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
242  }
243 
244  if (!(options_ & NO_FILE_AND_LINE)) {
245  color(kFileColor);
246  char fileBuf[PATH_MAX];
247  fileBuf[0] = '\0';
248  if (frame.location.hasFileAndLine) {
249  frame.location.file.toBuffer(fileBuf, sizeof(fileBuf));
250  doPrint("\n");
251  doPrint(pad);
252  doPrint(fileBuf);
253 
254  char buf[22];
255  uint32_t n = uint64ToBufferUnsafe(frame.location.line, buf);
256  doPrint(":");
257  doPrint(StringPiece(buf, n));
258  }
259 
260  if (frame.location.hasMainFile) {
261  char mainFileBuf[PATH_MAX];
262  mainFileBuf[0] = '\0';
263  frame.location.mainFile.toBuffer(mainFileBuf, sizeof(mainFileBuf));
264  if (!frame.location.hasFileAndLine || strcmp(fileBuf, mainFileBuf)) {
265  doPrint("\n");
266  doPrint(pad);
267  doPrint("-> ");
268  doPrint(mainFileBuf);
269  }
270  }
271  }
272 }
uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer)
Definition: Conv.h:383
void printTerse(uintptr_t address, const SymbolizedFrame &frame)
Definition: Symbolizer.cpp:291
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
Range< const char * > StringPiece
virtual void doPrint(StringPiece sp)=0
fbstring demangle(const char *name)
Definition: Demangle.cpp:111
void folly::symbolizer::SymbolizePrinter::print ( StringPiece  sp)
inline

Print a string, no endling newline.

Definition at line 203 of file Symbolizer.h.

203  {
204  doPrint(sp);
205  }
virtual void doPrint(StringPiece sp)=0
void folly::symbolizer::SymbolizePrinter::println ( uintptr_t  address,
const SymbolizedFrame frame 
)

Print one address with ending newline.

Definition at line 284 of file Symbolizer.cpp.

Referenced by folly::symbolizer::SafeStackTracePrinter::printSymbolizedStackTrace(), folly::SingletonVault::scheduleDestroyInstances(), and verifyStackTraces().

286  {
287  print(address, frame);
288  doPrint("\n");
289 }
void print(uintptr_t address, const SymbolizedFrame &frame)
Definition: Symbolizer.cpp:208
virtual void doPrint(StringPiece sp)=0
void folly::symbolizer::SymbolizePrinter::println ( const uintptr_t *  addresses,
const SymbolizedFrame frames,
size_t  frameCount 
)

Print multiple addresses on separate lines.

Definition at line 313 of file Symbolizer.cpp.

References COLOR_IF_TTY, i, println, and TERSE.

316  {
317  for (size_t i = 0; i < frameCount; ++i) {
318  println(addresses[i], frames[i]);
319  }
320 }
void println(uintptr_t address, const SymbolizedFrame &frame)
Definition: Symbolizer.cpp:284
template<size_t N>
void folly::symbolizer::SymbolizePrinter::println ( const FrameArray< N > &  fa,
size_t  skip = 0 
)
inline

Print multiple addresses on separate lines, skipping the first skip addresses.

Definition at line 212 of file Symbolizer.h.

References folly::symbolizer::FrameArray< N >::addresses, folly::symbolizer::FrameArray< N >::frameCount, folly::symbolizer::FrameArray< N >::frames, println, and folly::gen::skip().

212  {
213  if (skip < fa.frameCount) {
214  println(fa.addresses + skip, fa.frames + skip, fa.frameCount - skip);
215  }
216  }
detail::Skip skip(size_t count)
Definition: Base-inl.h:2598
void println(uintptr_t address, const SymbolizedFrame &frame)
Definition: Symbolizer.cpp:284
void folly::symbolizer::SymbolizePrinter::printTerse ( uintptr_t  address,
const SymbolizedFrame frame 
)
private

Definition at line 291 of file Symbolizer.cpp.

References folly::demangle(), folly::test::end(), folly::symbolizer::SymbolizedFrame::found, and folly::symbolizer::SymbolizedFrame::name.

293  {
294  if (frame.found && frame.name && frame.name[0] != '\0') {
295  char demangledBuf[2048] = {0};
296  demangle(frame.name, demangledBuf, sizeof(demangledBuf));
297  doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
298  } else {
299  // Can't use sprintf, not async-signal-safe
300  static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
301  char buf[] = "0x0000000000000000";
302  char* end = buf + sizeof(buf) - 1 - (16 - 2 * sizeof(uintptr_t));
303  char* p = end;
304  *p-- = '\0';
305  while (address != 0) {
306  *p-- = kHexChars[address & 0xf];
307  address >>= 4;
308  }
309  doPrint(StringPiece(buf, end));
310  }
311 }
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
Range< const char * > StringPiece
virtual void doPrint(StringPiece sp)=0
fbstring demangle(const char *name)
Definition: Demangle.cpp:111

Member Data Documentation

const bool folly::symbolizer::SymbolizePrinter::isTty_
protected

Definition at line 252 of file Symbolizer.h.

constexpr std::array< const char *, SymbolizePrinter::Color::NUM > folly::symbolizer::SymbolizePrinter::kColorMap
staticprivate
Initial value:
= {{
"\x1B[0m",
"\x1B[31m",
"\x1B[32m",
"\x1B[33m",
"\x1B[34m",
"\x1B[36m",
"\x1B[37m",
"\x1B[35m",
}}

Definition at line 258 of file Symbolizer.h.

Referenced by folly::symbolizer::Symbolizer::symbolize().

const int folly::symbolizer::SymbolizePrinter::options_
protected

Definition at line 251 of file Symbolizer.h.


The documentation for this class was generated from the following files: