2 * Copyright 2017 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/Memory.h>
34 #include <folly/ScopeGuard.h>
35 #include <folly/String.h>
37 #include <folly/experimental/symbolizer/Dwarf.h>
38 #include <folly/experimental/symbolizer/Elf.h>
39 #include <folly/experimental/symbolizer/LineReader.h>
40 #include <folly/portability/Unistd.h>
43 * This is declared in `link.h' on Linux platforms, but apparently not on the
44 * Mac version of the file. It's harmless to declare again, in any case.
46 * Note that declaring it with `extern "C"` results in linkage conflicts.
48 extern struct r_debug _r_debug;
51 namespace symbolizer {
55 ElfCache* defaultElfCache() {
56 static constexpr size_t defaultCapacity = 500;
57 static auto cache = new ElfCache(defaultCapacity);
63 void SymbolizedFrame::set(
64 const std::shared_ptr<ElfFile>& file,
66 Dwarf::LocationInfoMode mode) {
70 address += file->getBaseAddress();
71 auto sym = file->getDefinitionByAddress(address);
77 name = file->getSymbolName(sym);
79 Dwarf(file.get()).findAddress(address, location, mode);
82 Symbolizer::Symbolizer(ElfCacheBase* cache, Dwarf::LocationInfoMode mode)
83 : cache_(cache ? cache : defaultElfCache()), mode_(mode) {}
85 void Symbolizer::symbolize(
86 const uintptr_t* addresses,
87 SymbolizedFrame* frames,
90 for (size_t i = 0; i < addrCount; ++i) {
91 auto& frame = frames[i];
98 if (remaining == 0) { // we're done
102 if (_r_debug.r_version != 1) {
106 char selfPath[PATH_MAX + 8];
108 if ((selfSize = readlink("/proc/self/exe", selfPath, PATH_MAX + 1)) == -1) {
109 // Something has gone terribly wrong.
112 selfPath[selfSize] = '\0';
114 for (auto lmap = _r_debug.r_map; lmap != nullptr && remaining != 0;
115 lmap = lmap->l_next) {
116 // The empty string is used in place of the filename for the link_map
117 // corresponding to the running executable. Additionally, the `l_addr' is
118 // 0 and the link_map appears to be first in the list---but none of this
119 // behavior appears to be documented, so checking for the empty string is
120 // as good as anything.
121 auto const objPath = lmap->l_name[0] != '\0' ? lmap->l_name : selfPath;
123 auto const elfFile = cache_->getFile(objPath);
128 // Get the address at which the object is loaded. We have to use the ELF
129 // header for the running executable, since its `l_addr' is zero, but we
130 // should use `l_addr' for everything else---in particular, if the object
131 // is position-independent, getBaseAddress() (which is p_vaddr) will be 0.
133 lmap->l_addr != 0 ? lmap->l_addr : 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);
189 color(Color::DEFAULT);
192 if (!(options_ & NO_FRAME_ADDRESS)) {
193 color(kAddressColor);
195 AddressFormatter formatter;
196 doPrint(formatter.format(address));
199 const char padBuf[] = " ";
200 folly::StringPiece pad(
201 padBuf, sizeof(padBuf) - 1 - (16 - 2 * sizeof(uintptr_t)));
203 color(kFunctionColor);
205 doPrint(" (not found)");
209 if (!frame.name || frame.name[0] == '\0') {
210 doPrint(" (unknown)");
212 char demangledBuf[2048];
213 demangle(frame.name, demangledBuf, sizeof(demangledBuf));
215 doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
218 if (!(options_ & NO_FILE_AND_LINE)) {
220 char fileBuf[PATH_MAX];
222 if (frame.location.hasFileAndLine) {
223 frame.location.file.toBuffer(fileBuf, sizeof(fileBuf));
229 uint32_t n = uint64ToBufferUnsafe(frame.location.line, buf);
231 doPrint(StringPiece(buf, n));
234 if (frame.location.hasMainFile) {
235 char mainFileBuf[PATH_MAX];
236 mainFileBuf[0] = '\0';
237 frame.location.mainFile.toBuffer(mainFileBuf, sizeof(mainFileBuf));
238 if (!frame.location.hasFileAndLine || strcmp(fileBuf, mainFileBuf)) {
242 doPrint(mainFileBuf);
248 void SymbolizePrinter::color(SymbolizePrinter::Color color) {
249 if ((options_ & COLOR) == 0 && ((options_ & COLOR_IF_TTY) == 0 || !isTty_)) {
252 if (color < 0 || color >= kColorMap.size()) {
255 doPrint(kColorMap[color]);
258 void SymbolizePrinter::println(
260 const SymbolizedFrame& frame) {
261 print(address, frame);
265 void SymbolizePrinter::printTerse(
267 const SymbolizedFrame& frame) {
268 if (frame.found && frame.name && frame.name[0] != '\0') {
269 char demangledBuf[2048] = {0};
270 demangle(frame.name, demangledBuf, sizeof(demangledBuf));
271 doPrint(demangledBuf[0] == '\0' ? frame.name : demangledBuf);
273 // Can't use sprintf, not async-signal-safe
274 static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
275 char buf[] = "0x0000000000000000";
276 char* end = buf + sizeof(buf) - 1 - (16 - 2 * sizeof(uintptr_t));
279 while (address != 0) {
280 *p-- = kHexChars[address & 0xf];
283 doPrint(StringPiece(buf, end));
287 void SymbolizePrinter::println(
288 const uintptr_t* addresses,
289 const SymbolizedFrame* frames,
291 for (size_t i = 0; i < frameCount; ++i) {
292 println(addresses[i], frames[i]);
298 int getFD(const std::ios& stream) {
300 std::streambuf* buf = stream.rdbuf();
301 using namespace __gnu_cxx;
304 auto sbuf = dynamic_cast<stdio_sync_filebuf<char>*>(buf);
306 return fileno(sbuf->file());
310 auto sbuf = dynamic_cast<stdio_filebuf<char>*>(buf);
319 bool isColorfulTty(int options, int fd) {
320 if ((options & SymbolizePrinter::TERSE) != 0 ||
321 (options & SymbolizePrinter::COLOR_IF_TTY) == 0 || fd < 0 ||
325 auto term = ::getenv("TERM");
326 return !(term == nullptr || term[0] == '\0' || strcmp(term, "dumb") == 0);
331 OStreamSymbolizePrinter::OStreamSymbolizePrinter(std::ostream& out, int options)
332 : SymbolizePrinter(options, isColorfulTty(options, getFD(out))),
335 void OStreamSymbolizePrinter::doPrint(StringPiece sp) {
339 FDSymbolizePrinter::FDSymbolizePrinter(int fd, int options, size_t bufferSize)
340 : SymbolizePrinter(options, isColorfulTty(options, fd)),
342 buffer_(bufferSize ? IOBuf::create(bufferSize) : nullptr) {}
344 FDSymbolizePrinter::~FDSymbolizePrinter() {
348 void FDSymbolizePrinter::doPrint(StringPiece sp) {
350 if (sp.size() > buffer_->tailroom()) {
352 writeFull(fd_, sp.data(), sp.size());
354 memcpy(buffer_->writableTail(), sp.data(), sp.size());
355 buffer_->append(sp.size());
358 writeFull(fd_, sp.data(), sp.size());
362 void FDSymbolizePrinter::flush() {
363 if (buffer_ && !buffer_->empty()) {
364 writeFull(fd_, buffer_->data(), buffer_->length());
369 FILESymbolizePrinter::FILESymbolizePrinter(FILE* file, int options)
370 : SymbolizePrinter(options, isColorfulTty(options, fileno(file))),
373 void FILESymbolizePrinter::doPrint(StringPiece sp) {
374 fwrite(sp.data(), 1, sp.size(), file_);
377 void StringSymbolizePrinter::doPrint(StringPiece sp) {
378 buf_.append(sp.data(), sp.size());
381 StackTracePrinter::StackTracePrinter(size_t minSignalSafeElfCacheSize, int fd)
383 elfCache_(std::max(countLoadedElfFiles(), minSignalSafeElfCacheSize)),
386 SymbolizePrinter::COLOR_IF_TTY,
387 size_t(64) << 10), // 64KiB
388 addresses_(std::make_unique<FrameArray<kMaxStackTraceDepth>>()) {}
390 void StackTracePrinter::flush() {
395 void StackTracePrinter::printStackTrace(bool symbolize) {
400 // Skip the getStackTrace frame
401 if (!getStackTraceSafe(*addresses_)) {
402 print("(error retrieving stack trace)\n");
403 } else if (symbolize) {
404 // Do our best to populate location info, process is going to terminate,
405 // so performance isn't critical.
406 Symbolizer symbolizer(&elfCache_, Dwarf::LocationInfoMode::FULL);
407 symbolizer.symbolize(*addresses_);
409 // Skip the top 2 frames:
411 // StackTracePrinter::printStackTrace (here)
413 // Leaving signalHandler on the stack for clarity, I think.
414 printer_.println(*addresses_, 2);
416 print("(safe mode, symbolizer not available)\n");
417 AddressFormatter formatter;
418 for (size_t i = 0; i < addresses_->frameCount; ++i) {
419 print(formatter.format(addresses_->addresses[i]));
425 } // namespace symbolizer