2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/experimental/symbolizer/Symbolizer.h>
27 #include <ext/stdio_filebuf.h>
28 #include <ext/stdio_sync_filebuf.h>
31 #include <folly/Conv.h>
32 #include <folly/FileUtil.h>
33 #include <folly/ScopeGuard.h>
34 #include <folly/String.h>
36 #include <folly/experimental/symbolizer/Elf.h>
37 #include <folly/experimental/symbolizer/Dwarf.h>
38 #include <folly/experimental/symbolizer/LineReader.h>
42 * This is declared in `link.h' on Linux platforms, but apparently not on the
43 * Mac version of the file. It's harmless to declare again, in any case.
45 * Note that declaring it with `extern "C"` results in linkage conflicts.
47 extern struct r_debug _r_debug;
50 namespace symbolizer {
54 ElfCache* defaultElfCache() {
55 static constexpr size_t defaultCapacity = 500;
56 static auto cache = new ElfCache(defaultCapacity);
62 void SymbolizedFrame::set(const std::shared_ptr<ElfFile>& file,
64 Dwarf::LocationInfoMode mode) {
68 address += file->getBaseAddress();
69 auto sym = file->getDefinitionByAddress(address);
75 name = file->getSymbolName(sym);
77 Dwarf(file.get()).findAddress(address, location, mode);
80 Symbolizer::Symbolizer(ElfCacheBase* cache, Dwarf::LocationInfoMode mode)
81 : cache_(cache ?: defaultElfCache()), mode_(mode) {
84 void Symbolizer::symbolize(const uintptr_t* addresses,
85 SymbolizedFrame* frames,
88 for (size_t i = 0; i < addrCount; ++i) {
89 auto& frame = frames[i];
96 if (remaining == 0) { // we're done
100 if (_r_debug.r_version != 1) {
104 char selfPath[PATH_MAX + 8];
106 if ((selfSize = readlink("/proc/self/exe", selfPath, PATH_MAX + 1)) == -1) {
107 // Something has gone terribly wrong.
110 selfPath[selfSize] = '\0';
112 for (auto lmap = _r_debug.r_map;
113 lmap != nullptr && remaining != 0;
114 lmap = lmap->l_next) {
115 // The empty string is used in place of the filename for the link_map
116 // corresponding to the running executable. Additionally, the `l_addr' is
117 // 0 and the link_map appears to be first in the list---but none of this
118 // behavior appears to be documented, so checking for the empty string is
119 // as good as anything.
120 auto const objPath = lmap->l_name[0] != '\0' ? lmap->l_name : selfPath;
122 auto const elfFile = cache_->getFile(objPath);
127 // Get the address at which the object is loaded. We have to use the ELF
128 // header for the running executable, since its `l_addr' is zero, but we
129 // should use `l_addr' for everything else---in particular, if the object
130 // is position-independent, getBaseAddress() (which is p_vaddr) will be 0.
131 auto const base = lmap->l_addr != 0
133 : elfFile->getBaseAddress();
135 for (size_t i = 0; i < addrCount && remaining != 0; ++i) {
136 auto& frame = frames[i];
141 auto const addr = addresses[i];
142 // Get the unrelocated, ELF-relative address.
143 auto const adjusted = addr - base;
145 if (elfFile->getSectionContainingAddress(adjusted)) {
146 frame.set(elfFile, adjusted, mode_);
154 constexpr char kHexChars[] = "0123456789abcdef";
155 constexpr auto kAddressColor = SymbolizePrinter::Color::BLUE;
156 constexpr auto kFunctionColor = SymbolizePrinter::Color::PURPLE;
157 constexpr auto kFileColor = SymbolizePrinter::Color::DEFAULT;
160 constexpr char AddressFormatter::bufTemplate[];
161 constexpr std::array<const char*, SymbolizePrinter::Color::NUM>
162 SymbolizePrinter::kColorMap;
164 AddressFormatter::AddressFormatter() {
165 memcpy(buf_, bufTemplate, sizeof(buf_));
168 folly::StringPiece AddressFormatter::format(uintptr_t address) {
169 // Can't use sprintf, not async-signal-safe
170 static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
171 char* end = buf_ + sizeof(buf_) - 1 - (16 - 2 * sizeof(uintptr_t));
174 while (address != 0) {
175 *p-- = kHexChars[address & 0xf];
179 return folly::StringPiece(buf_, end);
182 void SymbolizePrinter::print(uintptr_t address, const SymbolizedFrame& frame) {
183 if (options_ & TERSE) {
184 printTerse(address, frame);
188 SCOPE_EXIT { color(Color::DEFAULT); };
190 if (!(options_ & NO_FRAME_ADDRESS)) {
191 color(kAddressColor);
193 AddressFormatter formatter;
194 doPrint(formatter.format(address));
197 const char padBuf[] = " ";
198 folly::StringPiece pad(padBuf,
199 sizeof(padBuf) - 1 - (16 - 2 * sizeof(uintptr_t)));
201 color(kFunctionColor);
203 doPrint(" (not found)");
207 if (!frame.name || frame.name[0] == '\0') {
208 doPrint(" (unknown)");
210 char demangledBuf[2048];
211 demangle(frame.name, demangledBuf, sizeof(demangledBuf));
213 doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
216 if (!(options_ & NO_FILE_AND_LINE)) {
218 char fileBuf[PATH_MAX];
220 if (frame.location.hasFileAndLine) {
221 frame.location.file.toBuffer(fileBuf, sizeof(fileBuf));
227 uint32_t n = uint64ToBufferUnsafe(frame.location.line, buf);
229 doPrint(StringPiece(buf, n));
232 if (frame.location.hasMainFile) {
233 char mainFileBuf[PATH_MAX];
234 mainFileBuf[0] = '\0';
235 frame.location.mainFile.toBuffer(mainFileBuf, sizeof(mainFileBuf));
236 if (!frame.location.hasFileAndLine || strcmp(fileBuf, mainFileBuf)) {
240 doPrint(mainFileBuf);
246 void SymbolizePrinter::color(SymbolizePrinter::Color color) {
247 if ((options_ & COLOR) == 0 &&
248 ((options_ & COLOR_IF_TTY) == 0 || !isTty_)) {
251 if (color < 0 || color >= kColorMap.size()) {
254 doPrint(kColorMap[color]);
257 void SymbolizePrinter::println(uintptr_t address,
258 const SymbolizedFrame& frame) {
259 print(address, frame);
263 void SymbolizePrinter::printTerse(uintptr_t address,
264 const SymbolizedFrame& frame) {
265 if (frame.found && frame.name && frame.name[0] != '\0') {
266 char demangledBuf[2048] = {0};
267 demangle(frame.name, demangledBuf, sizeof(demangledBuf));
268 doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
270 // Can't use sprintf, not async-signal-safe
271 static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
272 char buf[] = "0x0000000000000000";
273 char* end = buf + sizeof(buf) - 1 - (16 - 2 * sizeof(uintptr_t));
276 while (address != 0) {
277 *p-- = kHexChars[address & 0xf];
280 doPrint(StringPiece(buf, end));
284 void SymbolizePrinter::println(const uintptr_t* addresses,
285 const SymbolizedFrame* frames,
287 for (size_t i = 0; i < frameCount; ++i) {
288 println(addresses[i], frames[i]);
294 int getFD(const std::ios& stream) {
296 std::streambuf* buf = stream.rdbuf();
297 using namespace __gnu_cxx;
300 auto sbuf = dynamic_cast<stdio_sync_filebuf<char>*>(buf);
302 return fileno(sbuf->file());
306 auto sbuf = dynamic_cast<stdio_filebuf<char>*>(buf);
315 bool isColorfulTty(int options, int fd) {
316 if ((options & SymbolizePrinter::TERSE) != 0 ||
317 (options & SymbolizePrinter::COLOR_IF_TTY) == 0 ||
318 fd < 0 || !::isatty(fd)) {
321 auto term = ::getenv("TERM");
322 return !(term == nullptr || term[0] == '\0' || strcmp(term, "dumb") == 0);
325 } // anonymous namespace
327 OStreamSymbolizePrinter::OStreamSymbolizePrinter(std::ostream& out, int options)
328 : SymbolizePrinter(options, isColorfulTty(options, getFD(out))),
332 void OStreamSymbolizePrinter::doPrint(StringPiece sp) {
336 FDSymbolizePrinter::FDSymbolizePrinter(int fd, int options, size_t bufferSize)
337 : SymbolizePrinter(options, isColorfulTty(options, fd)),
339 buffer_(bufferSize ? IOBuf::create(bufferSize) : nullptr) {
342 FDSymbolizePrinter::~FDSymbolizePrinter() {
346 void FDSymbolizePrinter::doPrint(StringPiece sp) {
348 if (sp.size() > buffer_->tailroom()) {
350 writeFull(fd_, sp.data(), sp.size());
352 memcpy(buffer_->writableTail(), sp.data(), sp.size());
353 buffer_->append(sp.size());
356 writeFull(fd_, sp.data(), sp.size());
360 void FDSymbolizePrinter::flush() {
361 if (buffer_ && !buffer_->empty()) {
362 writeFull(fd_, buffer_->data(), buffer_->length());
367 FILESymbolizePrinter::FILESymbolizePrinter(FILE* file, int options)
368 : SymbolizePrinter(options, isColorfulTty(options, fileno(file))),
372 void FILESymbolizePrinter::doPrint(StringPiece sp) {
373 fwrite(sp.data(), 1, sp.size(), file_);
376 void StringSymbolizePrinter::doPrint(StringPiece sp) {
377 buf_.append(sp.data(), sp.size());
380 } // namespace symbolizer