proxygen
folly::symbolizer::Symbolizer Class Reference

#include <Symbolizer.h>

Public Member Functions

 Symbolizer (Dwarf::LocationInfoMode mode=kDefaultLocationInfoMode)
 
 Symbolizer (ElfCacheBase *cache, Dwarf::LocationInfoMode mode=kDefaultLocationInfoMode, size_t symbolCacheSize=0)
 
void symbolize (const uintptr_t *addresses, SymbolizedFrame *frames, size_t frameCount)
 
template<size_t N>
void symbolize (FrameArray< N > &fa)
 
bool symbolize (uintptr_t address, SymbolizedFrame &frame)
 

Static Public Attributes

static constexpr Dwarf::LocationInfoMode kDefaultLocationInfoMode
 

Private Types

using SymbolCache = EvictingCacheMap< uintptr_t, SymbolizedFrame >
 

Private Attributes

ElfCacheBase *const cache_
 
const Dwarf::LocationInfoMode mode_
 
folly::Optional< Synchronized< SymbolCache > > symbolCache_
 

Detailed Description

Definition at line 118 of file Symbolizer.h.

Member Typedef Documentation

Definition at line 155 of file Symbolizer.h.

Constructor & Destructor Documentation

folly::symbolizer::Symbolizer::Symbolizer ( Dwarf::LocationInfoMode  mode = kDefaultLocationInfoMode)
inlineexplicit

Definition at line 123 of file Symbolizer.h.

References mode.

124  : Symbolizer(nullptr, mode) {}
Symbolizer(Dwarf::LocationInfoMode mode=kDefaultLocationInfoMode)
Definition: Symbolizer.h:123
folly::Optional< PskKeyExchangeMode > mode
folly::symbolizer::Symbolizer::Symbolizer ( ElfCacheBase cache,
Dwarf::LocationInfoMode  mode = kDefaultLocationInfoMode,
size_t  symbolCacheSize = 0 
)
explicit

Definition at line 84 of file Symbolizer.cpp.

References folly::in_place(), and symbolCache_.

88  : cache_(cache ? cache : defaultElfCache()), mode_(mode) {
89  if (symbolCacheSize > 0) {
90  symbolCache_.emplace(folly::in_place, symbolCacheSize);
91  }
92 }
const Dwarf::LocationInfoMode mode_
Definition: Symbolizer.h:153
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
folly::Optional< PskKeyExchangeMode > mode
ElfCacheBase *const cache_
Definition: Symbolizer.h:152
folly::Optional< Synchronized< SymbolCache > > symbolCache_
Definition: Symbolizer.h:156

Member Function Documentation

void folly::symbolizer::Symbolizer::symbolize ( const uintptr_t *  addresses,
SymbolizedFrame frames,
size_t  frameCount 
)

Symbolize given addresses.

Definition at line 94 of file Symbolizer.cpp.

References _r_debug, addr, folly::symbolizer::AddressFormatter::bufTemplate, cache_, folly::symbolizer::SymbolizedFrame::clear(), folly::symbolizer::ElfCacheBase::getFile(), i, folly::symbolizer::SymbolizePrinter::kColorMap, mode_, and symbolCache_.

Referenced by folly::symbolizer::FastStackTracePrinter::printStackTrace(), folly::symbolizer::SafeStackTracePrinter::printSymbolizedStackTrace(), folly::symbolizer::test::runElfCacheTest(), folly::SingletonVault::scheduleDestroyInstances(), folly::symbolizer::test::ElfCacheTest::SetUp(), folly::symbolizer::test::TEST(), and verifyStackTraces().

97  {
98  size_t remaining = 0;
99  for (size_t i = 0; i < addrCount; ++i) {
100  auto& frame = frames[i];
101  if (!frame.found) {
102  ++remaining;
103  frame.clear();
104  }
105  }
106 
107  if (remaining == 0) { // we're done
108  return;
109  }
110 
111  if (_r_debug.r_version != 1) {
112  return;
113  }
114 
115  char selfPath[PATH_MAX + 8];
116  ssize_t selfSize;
117  if ((selfSize = readlink("/proc/self/exe", selfPath, PATH_MAX + 1)) == -1) {
118  // Something has gone terribly wrong.
119  return;
120  }
121  selfPath[selfSize] = '\0';
122 
123  for (auto lmap = _r_debug.r_map; lmap != nullptr && remaining != 0;
124  lmap = lmap->l_next) {
125  // The empty string is used in place of the filename for the link_map
126  // corresponding to the running executable. Additionally, the `l_addr' is
127  // 0 and the link_map appears to be first in the list---but none of this
128  // behavior appears to be documented, so checking for the empty string is
129  // as good as anything.
130  auto const objPath = lmap->l_name[0] != '\0' ? lmap->l_name : selfPath;
131 
132  auto const elfFile = cache_->getFile(objPath);
133  if (!elfFile) {
134  continue;
135  }
136 
137  // Get the address at which the object is loaded. We have to use the ELF
138  // header for the running executable, since its `l_addr' is zero, but we
139  // should use `l_addr' for everything else---in particular, if the object
140  // is position-independent, getBaseAddress() (which is p_vaddr) will be 0.
141  auto const base =
142  lmap->l_addr != 0 ? lmap->l_addr : elfFile->getBaseAddress();
143 
144  for (size_t i = 0; i < addrCount && remaining != 0; ++i) {
145  auto& frame = frames[i];
146  if (frame.found) {
147  continue;
148  }
149 
150  auto const addr = addresses[i];
151  if (symbolCache_) {
152  // Need a write lock, because EvictingCacheMap brings found item to
153  // front of eviction list.
154  auto lockedSymbolCache = symbolCache_->wlock();
155 
156  auto const iter = lockedSymbolCache->find(addr);
157  if (iter != lockedSymbolCache->end()) {
158  frame = iter->second;
159  continue;
160  }
161  }
162 
163  // Get the unrelocated, ELF-relative address.
164  auto const adjusted = addr - base;
165 
166  if (elfFile->getSectionContainingAddress(adjusted)) {
167  frame.set(elfFile, adjusted, mode_);
168  --remaining;
169  if (symbolCache_) {
170  // frame may already have been set here. That's ok, we'll just
171  // overwrite, which doesn't cause a correctness problem.
172  symbolCache_->wlock()->set(addr, frame);
173  }
174  }
175  }
176  }
177 }
virtual std::shared_ptr< ElfFile > getFile(StringPiece path)=0
const Dwarf::LocationInfoMode mode_
Definition: Symbolizer.h:153
struct r_debug _r_debug
ElfCacheBase *const cache_
Definition: Symbolizer.h:152
folly::Optional< Synchronized< SymbolCache > > symbolCache_
Definition: Symbolizer.h:156
ThreadPoolListHook * addr
template<size_t N>
void folly::symbolizer::Symbolizer::symbolize ( FrameArray< N > &  fa)
inline

Definition at line 139 of file Symbolizer.h.

References folly::symbolizer::FrameArray< N >::addresses, folly::symbolizer::FrameArray< N >::frameCount, and folly::symbolizer::FrameArray< N >::frames.

139  {
140  symbolize(fa.addresses, fa.frames, fa.frameCount);
141  }
void symbolize(const uintptr_t *addresses, SymbolizedFrame *frames, size_t frameCount)
Definition: Symbolizer.cpp:94
bool folly::symbolizer::Symbolizer::symbolize ( uintptr_t  address,
SymbolizedFrame frame 
)
inline

Shortcut to symbolize one address.

Definition at line 146 of file Symbolizer.h.

References folly::symbolizer::SymbolizedFrame::found.

146  {
147  symbolize(&address, &frame, 1);
148  return frame.found;
149  }
void symbolize(const uintptr_t *addresses, SymbolizedFrame *frames, size_t frameCount)
Definition: Symbolizer.cpp:94

Member Data Documentation

ElfCacheBase* const folly::symbolizer::Symbolizer::cache_
private

Definition at line 152 of file Symbolizer.h.

Referenced by symbolize().

constexpr Dwarf::LocationInfoMode folly::symbolizer::Symbolizer::kDefaultLocationInfoMode
static
Initial value:

Definition at line 120 of file Symbolizer.h.

const Dwarf::LocationInfoMode folly::symbolizer::Symbolizer::mode_
private

Definition at line 153 of file Symbolizer.h.

Referenced by symbolize().

folly::Optional<Synchronized<SymbolCache> > folly::symbolizer::Symbolizer::symbolCache_
private

Definition at line 156 of file Symbolizer.h.

Referenced by symbolize(), and Symbolizer().


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