Use fixed size stack traces; unify getStackTrace
[folly.git] / folly / experimental / symbolizer / Symbolizer.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "folly/experimental/symbolizer/Symbolizer.h"
18
19 #include <limits.h>
20
21 #include "folly/Conv.h"
22 #include "folly/FileUtil.h"
23 #include "folly/String.h"
24
25 #include "folly/experimental/symbolizer/Elf.h"
26 #include "folly/experimental/symbolizer/Dwarf.h"
27 #include "folly/experimental/symbolizer/LineReader.h"
28
29 namespace folly {
30 namespace symbolizer {
31
32 namespace {
33
34 /**
35  * Read a hex value.
36  */
37 uintptr_t readHex(StringPiece& sp) {
38   uintptr_t val = 0;
39   const char* p = sp.begin();
40   for (; p != sp.end(); ++p) {
41     unsigned int v;
42     if (*p >= '0' && *p <= '9') {
43       v = (*p - '0');
44     } else if (*p >= 'a' && *p <= 'f') {
45       v = (*p - 'a') + 10;
46     } else if (*p >= 'A' && *p <= 'F') {
47       v = (*p - 'A') + 10;
48     } else {
49       break;
50     }
51     val = (val << 4) + v;
52   }
53   sp.assign(p, sp.end());
54   return val;
55 }
56
57 /**
58  * Skip over non-space characters.
59  */
60 void skipNS(StringPiece& sp) {
61   const char* p = sp.begin();
62   for (; p != sp.end() && (*p != ' ' && *p != '\t'); ++p) { }
63   sp.assign(p, sp.end());
64 }
65
66 /**
67  * Skip over space and tab characters.
68  */
69 void skipWS(StringPiece& sp) {
70   const char* p = sp.begin();
71   for (; p != sp.end() && (*p == ' ' || *p == '\t'); ++p) { }
72   sp.assign(p, sp.end());
73 }
74
75 /**
76  * Parse a line from /proc/self/maps
77  */
78 bool parseProcMapsLine(StringPiece line,
79                        uintptr_t& from, uintptr_t& to,
80                        StringPiece& fileName) {
81   // from     to       perm offset   dev   inode             path
82   // 00400000-00405000 r-xp 00000000 08:03 35291182          /bin/cat
83   if (line.empty()) {
84     return false;
85   }
86
87   // Remove trailing newline, if any
88   if (line.back() == '\n') {
89     line.pop_back();
90   }
91
92   // from
93   from = readHex(line);
94   if (line.empty() || line.front() != '-') {
95     return false;
96   }
97   line.pop_front();
98
99   // to
100   to = readHex(line);
101   if (line.empty() || line.front() != ' ') {
102     return false;
103   }
104   line.pop_front();
105
106   // perms
107   skipNS(line);
108   if (line.empty() || line.front() != ' ') {
109     return false;
110   }
111   line.pop_front();
112
113   uintptr_t fileOffset = readHex(line);
114   if (line.empty() || line.front() != ' ') {
115     return false;
116   }
117   line.pop_front();
118   if (fileOffset != 0) {
119     return false;  // main mapping starts at 0
120   }
121
122   // dev
123   skipNS(line);
124   if (line.empty() || line.front() != ' ') {
125     return false;
126   }
127   line.pop_front();
128
129   // inode
130   skipNS(line);
131   if (line.empty() || line.front() != ' ') {
132     return false;
133   }
134
135   skipWS(line);
136   if (line.empty()) {
137     fileName.clear();
138     return true;
139   }
140
141   fileName = line;
142   return true;
143 }
144
145 }  // namespace
146
147 void Symbolizer::symbolize(const uintptr_t* addresses,
148                            SymbolizedFrame* frames,
149                            size_t addressCount) {
150   size_t remaining = 0;
151   for (size_t i = 0; i < addressCount; ++i) {
152     auto& frame = frames[i];
153     if (!frame.found) {
154       ++remaining;
155       frame.name.clear();
156       frame.location = Dwarf::LocationInfo();
157     }
158   }
159
160   if (remaining == 0) {  // we're done
161     return;
162   }
163
164   int fd = openNoInt("/proc/self/maps", O_RDONLY);
165   if (fd == -1) {
166     return;
167   }
168
169   char buf[PATH_MAX + 100];  // Long enough for any line
170   LineReader reader(fd, buf, sizeof(buf));
171
172   char fileNameBuf[PATH_MAX];
173
174   while (remaining != 0) {
175     StringPiece line;
176     if (reader.readLine(line) != LineReader::kReading) {
177       break;
178     }
179
180     // Parse line
181     uintptr_t from;
182     uintptr_t to;
183     StringPiece fileName;
184     if (!parseProcMapsLine(line, from, to, fileName)) {
185       continue;
186     }
187
188     bool first = true;
189     ElfFile* elfFile = nullptr;
190
191     // See if any addresses are here
192     for (size_t i = 0; i < addressCount; ++i) {
193       auto& frame = frames[i];
194       if (frame.found) {
195         continue;
196       }
197
198       uintptr_t address = addresses[i];
199
200       if (from > address || address >= to) {
201         continue;
202       }
203
204       // Found
205       frame.found = true;
206       --remaining;
207
208       // Open the file on first use
209       if (first) {
210         first = false;
211         if (fileCount_ < kMaxFiles &&
212             !fileName.empty() &&
213             fileName.size() < sizeof(fileNameBuf)) {
214           memcpy(fileNameBuf, fileName.data(), fileName.size());
215           fileNameBuf[fileName.size()] = '\0';
216           auto& f = files_[fileCount_++];
217           if (f.openNoThrow(fileNameBuf) != -1) {
218             elfFile = &f;
219           }
220         }
221       }
222
223       if (!elfFile) {
224         continue;
225       }
226
227       // Undo relocation
228       uintptr_t fileAddress = address - from + elfFile->getBaseAddress();
229       auto sym = elfFile->getDefinitionByAddress(fileAddress);
230       if (!sym.first) {
231         continue;
232       }
233       auto name = elfFile->getSymbolName(sym);
234       if (name) {
235         frame.name = name;
236       }
237
238       Dwarf(elfFile).findAddress(fileAddress, frame.location);
239     }
240   }
241
242   closeNoInt(fd);
243 }
244
245 namespace {
246 const char kHexChars[] = "0123456789abcdef";
247 }  // namespace
248
249 void SymbolizePrinter::print(uintptr_t address, const SymbolizedFrame& frame) {
250   // Can't use sprintf, not async-signal-safe
251   static_assert(sizeof(uintptr_t) <= 8, "huge uintptr_t?");
252   char buf[] = "    @ 0000000000000000";
253   char* end = buf + sizeof(buf) - 1 - (16 - 2 * sizeof(uintptr_t));
254   const char padBuf[] = "                       ";
255   folly::StringPiece pad(padBuf,
256                          sizeof(padBuf) - 1 - (16 - 2 * sizeof(uintptr_t)));
257   char* p = end;
258   *p-- = '\0';
259   while (address != 0) {
260     *p-- = kHexChars[address & 0xf];
261     address >>= 4;
262   }
263   doPrint(folly::StringPiece(buf, end));
264
265   char mangledBuf[1024];
266   if (!frame.found) {
267     doPrint(" (not found)\n");
268     return;
269   }
270
271   if (frame.name.empty()) {
272     doPrint(" (unknown)\n");
273   } else if (frame.name.size() >= sizeof(mangledBuf)) {
274     doPrint(" ");
275     doPrint(frame.name);
276     doPrint("\n");
277   } else {
278     memcpy(mangledBuf, frame.name.data(), frame.name.size());
279     mangledBuf[frame.name.size()] = '\0';
280
281     char demangledBuf[1024];
282     demangle(mangledBuf, demangledBuf, sizeof(demangledBuf));
283     doPrint(" ");
284     doPrint(demangledBuf);
285     doPrint("\n");
286   }
287
288   char fileBuf[PATH_MAX];
289   fileBuf[0] = '\0';
290   if (frame.location.hasFileAndLine) {
291     frame.location.file.toBuffer(fileBuf, sizeof(fileBuf));
292     doPrint(pad);
293     doPrint(fileBuf);
294
295     char buf[22];
296     uint32_t n = uint64ToBufferUnsafe(frame.location.line, buf);
297     doPrint(":");
298     doPrint(StringPiece(buf, n));
299     doPrint("\n");
300   }
301
302   if (frame.location.hasMainFile) {
303     char mainFileBuf[PATH_MAX];
304     mainFileBuf[0] = '\0';
305     frame.location.mainFile.toBuffer(mainFileBuf, sizeof(mainFileBuf));
306     if (!frame.location.hasFileAndLine || strcmp(fileBuf, mainFileBuf)) {
307       doPrint(pad);
308       doPrint("-> ");
309       doPrint(mainFileBuf);
310       doPrint("\n");
311     }
312   }
313 }
314
315 void SymbolizePrinter::print(const uintptr_t* addresses,
316                              const SymbolizedFrame* frames,
317                              size_t frameCount) {
318   for (size_t i = 0; i < frameCount; ++i) {
319     print(addresses[i], frames[i]);
320   }
321 }
322
323 void OStreamSymbolizePrinter::doPrint(StringPiece sp) {
324   out_ << sp;
325 }
326
327 void FDSymbolizePrinter::doPrint(StringPiece sp) {
328   writeFull(fd_, sp.data(), sp.size());
329 }
330
331 }  // namespace symbolizer
332 }  // namespace folly