2 * Copyright 2013 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 // Must be first to ensure that UNW_LOCAL_ONLY is defined
18 #define UNW_LOCAL_ONLY 1
19 #include <libunwind.h>
21 #include "folly/experimental/symbolizer/Symbolizer.h"
25 #include "folly/Conv.h"
26 #include "folly/FileUtil.h"
27 #include "folly/String.h"
29 #include "folly/experimental/symbolizer/Elf.h"
30 #include "folly/experimental/symbolizer/Dwarf.h"
31 #include "folly/experimental/symbolizer/LineReader.h"
34 namespace symbolizer {
41 uintptr_t readHex(StringPiece& sp) {
43 const char* p = sp.begin();
44 for (; p != sp.end(); ++p) {
46 if (*p >= '0' && *p <= '9') {
48 } else if (*p >= 'a' && *p <= 'f') {
50 } else if (*p >= 'A' && *p <= 'F') {
57 sp.assign(p, sp.end());
62 * Skip over non-space characters.
64 void skipNS(StringPiece& sp) {
65 const char* p = sp.begin();
66 for (; p != sp.end() && (*p != ' ' && *p != '\t'); ++p) { }
67 sp.assign(p, sp.end());
71 * Skip over space and tab characters.
73 void skipWS(StringPiece& sp) {
74 const char* p = sp.begin();
75 for (; p != sp.end() && (*p == ' ' || *p == '\t'); ++p) { }
76 sp.assign(p, sp.end());
80 * Parse a line from /proc/self/maps
82 bool parseProcMapsLine(StringPiece line,
83 uintptr_t& from, uintptr_t& to,
84 StringPiece& fileName) {
85 // from to perm offset dev inode path
86 // 00400000-00405000 r-xp 00000000 08:03 35291182 /bin/cat
91 // Remove trailing newline, if any
92 if (line.back() == '\n') {
98 if (line.empty() || line.front() != '-') {
105 if (line.empty() || line.front() != ' ') {
112 if (line.empty() || line.front() != ' ') {
117 uintptr_t fileOffset = readHex(line);
118 if (line.empty() || line.front() != ' ') {
122 if (fileOffset != 0) {
123 return false; // main mapping starts at 0
128 if (line.empty() || line.front() != ' ') {
135 if (line.empty() || line.front() != ' ') {
151 ssize_t getStackTrace(AddressInfo* addresses,
155 int r = unw_getcontext(&uctx);
163 while (idx < maxAddresses) {
166 r = unw_init_local(&cursor, &uctx);
168 r = unw_step(&cursor);
182 int rr = unw_get_reg(&cursor, UNW_REG_IP, &ip);
187 // If error, assume not a signal frame
188 rr = unw_is_signal_frame(&cursor);
190 addresses[idx++] = AddressInfo(ip, (rr > 0));
200 void Symbolizer::symbolize(AddressInfo* addresses, size_t addressCount) {
201 size_t remaining = 0;
202 for (size_t i = 0; i < addressCount; ++i) {
203 auto& ainfo = addresses[i];
207 ainfo.location = Dwarf::LocationInfo();
211 if (remaining == 0) { // we're done
215 int fd = openNoInt("/proc/self/maps", O_RDONLY);
220 char buf[PATH_MAX + 100]; // Long enough for any line
221 LineReader reader(fd, buf, sizeof(buf));
223 char fileNameBuf[PATH_MAX];
225 while (remaining != 0) {
227 if (reader.readLine(line) != LineReader::kReading) {
234 StringPiece fileName;
235 if (!parseProcMapsLine(line, from, to, fileName)) {
240 ElfFile* elfFile = nullptr;
242 // See if any addresses are here
243 for (size_t i = 0; i < addressCount; ++i) {
244 auto& ainfo = addresses[i];
249 uintptr_t address = ainfo.address;
251 // If the next address (closer to the top of the stack) was a signal
252 // frame, then this is the *resume* address, which is the address
253 // after the location where the signal was caught. This might be in
254 // the next function, so subtract 1 before symbolizing.
255 if (i != 0 && addresses[i-1].isSignalFrame) {
259 if (from > address || address >= to) {
267 // Open the file on first use
270 if (fileCount_ < kMaxFiles &&
272 fileName.size() < sizeof(fileNameBuf)) {
273 memcpy(fileNameBuf, fileName.data(), fileName.size());
274 fileNameBuf[fileName.size()] = '\0';
275 auto& f = files_[fileCount_++];
276 if (f.openNoThrow(fileNameBuf) != -1) {
287 uintptr_t fileAddress = address - from + elfFile->getBaseAddress();
288 auto sym = elfFile->getDefinitionByAddress(fileAddress);
292 auto name = elfFile->getSymbolName(sym);
297 Dwarf(elfFile).findAddress(fileAddress, ainfo.location);
305 const char kHexChars[] = "0123456789abcdef";
308 void SymbolizePrinter::print(const AddressInfo& ainfo) {
309 uintptr_t address = ainfo.address;
310 // Can't use sprintf, not async-signal-safe
311 static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
312 char buf[] = " @ 0000000000000000";
313 char* end = buf + sizeof(buf) - 1 - (16 - 2 * sizeof(uintptr_t));
314 const char padBuf[] = " ";
315 folly::StringPiece pad(padBuf,
316 sizeof(padBuf) - 1 - (16 - 2 * sizeof(uintptr_t)));
319 while (address != 0) {
320 *p-- = kHexChars[address & 0xf];
323 doPrint(folly::StringPiece(buf, end));
325 char mangledBuf[1024];
327 doPrint(" (not found)\n");
331 if (ainfo.name.empty()) {
332 doPrint(" (unknown)\n");
333 } else if (ainfo.name.size() >= sizeof(mangledBuf)) {
338 memcpy(mangledBuf, ainfo.name.data(), ainfo.name.size());
339 mangledBuf[ainfo.name.size()] = '\0';
341 char demangledBuf[1024];
342 demangle(mangledBuf, demangledBuf, sizeof(demangledBuf));
344 doPrint(demangledBuf);
348 char fileBuf[PATH_MAX];
350 if (ainfo.location.hasFileAndLine) {
351 ainfo.location.file.toBuffer(fileBuf, sizeof(fileBuf));
356 uint32_t n = uint64ToBufferUnsafe(ainfo.location.line, buf);
358 doPrint(StringPiece(buf, n));
362 if (ainfo.location.hasMainFile) {
363 char mainFileBuf[PATH_MAX];
364 mainFileBuf[0] = '\0';
365 ainfo.location.mainFile.toBuffer(mainFileBuf, sizeof(mainFileBuf));
366 if (!ainfo.location.hasFileAndLine || strcmp(fileBuf, mainFileBuf)) {
369 doPrint(mainFileBuf);
375 void SymbolizePrinter::print(const AddressInfo* addresses,
376 size_t addressCount) {
377 for (size_t i = 0; i < addressCount; ++i) {
378 auto& ainfo = addresses[i];
383 void OStreamSymbolizePrinter::doPrint(StringPiece sp) {
387 void FDSymbolizePrinter::doPrint(StringPiece sp) {
388 writeFull(fd_, sp.data(), sp.size());
391 std::ostream& operator<<(std::ostream& out, const AddressInfo& ainfo) {
392 OStreamSymbolizePrinter osp(out);
397 } // namespace symbolizer