blob: 1ffc6079a2ae374a09f07ffd239d95f12c832fe1 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// Copyright (c) 2006, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: Satoru Takabayashi
31// Stack-footprint reduction work done by Raksit Ashok
32//
33// Implementation note:
34//
35// We don't use heaps but only use stacks. We want to reduce the
36// stack consumption so that the symbolizer can run on small stacks.
37//
38// Here are some numbers collected with GCC 4.1.0 on x86:
39// - sizeof(Elf32_Sym) = 16
40// - sizeof(Elf32_Shdr) = 40
41// - sizeof(Elf64_Sym) = 24
42// - sizeof(Elf64_Shdr) = 64
43//
44// This implementation is intended to be async-signal-safe but uses
45// some functions which are not guaranteed to be so, such as memchr()
46// and memmove(). We assume they are async-signal-safe.
47//
48// Additional header can be specified by the GLOG_BUILD_CONFIG_INCLUDE
49// macro to add platform specific defines (e.g. OS_OPENBSD).
50
51#ifdef GLOG_BUILD_CONFIG_INCLUDE
52#include GLOG_BUILD_CONFIG_INCLUDE
53#endif // GLOG_BUILD_CONFIG_INCLUDE
54
55#include "utilities.h"
56
57#if defined(HAVE_SYMBOLIZE)
58
59#include <string.h>
60
61#include <algorithm>
62#include <limits>
63
64#include "symbolize.h"
65#include "demangle.h"
66
67_START_GOOGLE_NAMESPACE_
68
69// We don't use assert() since it's not guaranteed to be
70// async-signal-safe. Instead we define a minimal assertion
71// macro. So far, we don't need pretty printing for __FILE__, etc.
72
73// A wrapper for abort() to make it callable in ? :.
74static int AssertFail() {
75 abort();
76 return 0; // Should not reach.
77}
78
79#define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
80
81static SymbolizeCallback g_symbolize_callback = NULL;
82void InstallSymbolizeCallback(SymbolizeCallback callback) {
83 g_symbolize_callback = callback;
84}
85
86static SymbolizeOpenObjectFileCallback g_symbolize_open_object_file_callback =
87 NULL;
88void InstallSymbolizeOpenObjectFileCallback(
89 SymbolizeOpenObjectFileCallback callback) {
90 g_symbolize_open_object_file_callback = callback;
91}
92
93// This function wraps the Demangle function to provide an interface
94// where the input symbol is demangled in-place.
95// To keep stack consumption low, we would like this function to not
96// get inlined.
97static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
98 char demangled[256]; // Big enough for sane demangled symbols.
99 if (Demangle(out, demangled, sizeof(demangled))) {
100 // Demangling succeeded. Copy to out if the space allows.
101 size_t len = strlen(demangled);
102 if (len + 1 <= (size_t)out_size) { // +1 for '\0'.
103 SAFE_ASSERT(len < sizeof(demangled));
104 memmove(out, demangled, len + 1);
105 }
106 }
107}
108
109_END_GOOGLE_NAMESPACE_
110
111#if defined(__ELF__)
112
113#include <dlfcn.h>
114#if defined(OS_OPENBSD)
115#include <sys/exec_elf.h>
116#else
117#include <elf.h>
118#endif
119#include <errno.h>
120#include <fcntl.h>
121#include <limits.h>
122#include <stdint.h>
123#include <stdio.h>
124#include <stdlib.h>
125#include <stddef.h>
126#include <string.h>
127#include <sys/stat.h>
128#include <sys/types.h>
129#include <unistd.h>
130
131#include "symbolize.h"
132#include "config.h"
133#include "glog/raw_logging.h"
134
135// Re-runs fn until it doesn't cause EINTR.
136#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
137
138_START_GOOGLE_NAMESPACE_
139
140// Read up to "count" bytes from "offset" in the file pointed by file
141// descriptor "fd" into the buffer starting at "buf" while handling short reads
142// and EINTR. On success, return the number of bytes read. Otherwise, return
143// -1.
144static ssize_t ReadFromOffset(const int fd, void *buf, const size_t count,
145 const off_t offset) {
146 SAFE_ASSERT(fd >= 0);
147 SAFE_ASSERT(count <= std::numeric_limits<ssize_t>::max());
148 char *buf0 = reinterpret_cast<char *>(buf);
149 ssize_t num_bytes = 0;
150 while (num_bytes < count) {
151 ssize_t len;
152 NO_INTR(len = pread(fd, buf0 + num_bytes, count - num_bytes,
153 offset + num_bytes));
154 if (len < 0) { // There was an error other than EINTR.
155 return -1;
156 }
157 if (len == 0) { // Reached EOF.
158 break;
159 }
160 num_bytes += len;
161 }
162 SAFE_ASSERT(num_bytes <= count);
163 return num_bytes;
164}
165
166// Try reading exactly "count" bytes from "offset" bytes in a file
167// pointed by "fd" into the buffer starting at "buf" while handling
168// short reads and EINTR. On success, return true. Otherwise, return
169// false.
170static bool ReadFromOffsetExact(const int fd, void *buf,
171 const size_t count, const off_t offset) {
172 ssize_t len = ReadFromOffset(fd, buf, count, offset);
173 return len == count;
174}
175
176// Returns elf_header.e_type if the file pointed by fd is an ELF binary.
177static int FileGetElfType(const int fd) {
178 ElfW(Ehdr) elf_header;
179 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
180 return -1;
181 }
182 if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
183 return -1;
184 }
185 return elf_header.e_type;
186}
187
188// Read the section headers in the given ELF binary, and if a section
189// of the specified type is found, set the output to this section header
190// and return true. Otherwise, return false.
191// To keep stack consumption low, we would like this function to not get
192// inlined.
193static ATTRIBUTE_NOINLINE bool
194GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
195 ElfW(Word) type, ElfW(Shdr) *out) {
196 // Read at most 16 section headers at a time to save read calls.
197 ElfW(Shdr) buf[16];
198 for (int i = 0; i < sh_num;) {
199 const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
200 const ssize_t num_bytes_to_read =
201 (sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
202 const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
203 sh_offset + i * sizeof(buf[0]));
204 if (len == -1) {
205 return false;
206 }
207 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
208 const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
209 SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
210 for (int j = 0; j < num_headers_in_buf; ++j) {
211 if (buf[j].sh_type == type) {
212 *out = buf[j];
213 return true;
214 }
215 }
216 i += num_headers_in_buf;
217 }
218 return false;
219}
220
221// There is no particular reason to limit section name to 63 characters,
222// but there has (as yet) been no need for anything longer either.
223const int kMaxSectionNameLen = 64;
224
225// name_len should include terminating '\0'.
226bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
227 ElfW(Shdr) *out) {
228 ElfW(Ehdr) elf_header;
229 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
230 return false;
231 }
232
233 ElfW(Shdr) shstrtab;
234 off_t shstrtab_offset = (elf_header.e_shoff +
235 elf_header.e_shentsize * elf_header.e_shstrndx);
236 if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
237 return false;
238 }
239
240 for (int i = 0; i < elf_header.e_shnum; ++i) {
241 off_t section_header_offset = (elf_header.e_shoff +
242 elf_header.e_shentsize * i);
243 if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
244 return false;
245 }
246 char header_name[kMaxSectionNameLen];
247 if (sizeof(header_name) < name_len) {
248 RAW_LOG(WARNING, "Section name '%s' is too long (%" PRIuS "); "
249 "section will not be found (even if present).", name, name_len);
250 // No point in even trying.
251 return false;
252 }
253 off_t name_offset = shstrtab.sh_offset + out->sh_name;
254 ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
255 if (n_read == -1) {
256 return false;
257 } else if (n_read != name_len) {
258 // Short read -- name could be at end of file.
259 continue;
260 }
261 if (memcmp(header_name, name, name_len) == 0) {
262 return true;
263 }
264 }
265 return false;
266}
267
268// Read a symbol table and look for the symbol containing the
269// pc. Iterate over symbols in a symbol table and look for the symbol
270// containing "pc". On success, return true and write the symbol name
271// to out. Otherwise, return false.
272// To keep stack consumption low, we would like this function to not get
273// inlined.
274static ATTRIBUTE_NOINLINE bool
275FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
276 uint64_t symbol_offset, const ElfW(Shdr) *strtab,
277 const ElfW(Shdr) *symtab) {
278 if (symtab == NULL) {
279 return false;
280 }
281 const int num_symbols = symtab->sh_size / symtab->sh_entsize;
282 for (int i = 0; i < num_symbols;) {
283 off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
284
285 // If we are reading Elf64_Sym's, we want to limit this array to
286 // 32 elements (to keep stack consumption low), otherwise we can
287 // have a 64 element Elf32_Sym array.
288#if __WORDSIZE == 64
289#define NUM_SYMBOLS 32
290#else
291#define NUM_SYMBOLS 64
292#endif
293
294 // Read at most NUM_SYMBOLS symbols at once to save read() calls.
295 ElfW(Sym) buf[NUM_SYMBOLS];
296 int num_symbols_to_read = std::min(NUM_SYMBOLS, num_symbols - i);
297 const ssize_t len =
298 ReadFromOffset(fd, &buf, sizeof(buf[0]) * num_symbols_to_read, offset);
299 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
300 const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
301 SAFE_ASSERT(num_symbols_in_buf <= num_symbols_to_read);
302 for (int j = 0; j < num_symbols_in_buf; ++j) {
303 const ElfW(Sym)& symbol = buf[j];
304 uint64_t start_address = symbol.st_value;
305 start_address += symbol_offset;
306 uint64_t end_address = start_address + symbol.st_size;
307 if (symbol.st_value != 0 && // Skip null value symbols.
308 symbol.st_shndx != 0 && // Skip undefined symbols.
309 start_address <= pc && pc < end_address) {
310 ssize_t len1 = ReadFromOffset(fd, out, out_size,
311 strtab->sh_offset + symbol.st_name);
312 if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
313 return false;
314 }
315 return true; // Obtained the symbol name.
316 }
317 }
318 i += num_symbols_in_buf;
319 }
320 return false;
321}
322
323// Get the symbol name of "pc" from the file pointed by "fd". Process
324// both regular and dynamic symbol tables if necessary. On success,
325// write the symbol name to "out" and return true. Otherwise, return
326// false.
327static bool GetSymbolFromObjectFile(const int fd,
328 uint64_t pc,
329 char* out,
330 int out_size,
331 uint64_t base_address) {
332 // Read the ELF header.
333 ElfW(Ehdr) elf_header;
334 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
335 return false;
336 }
337
338 ElfW(Shdr) symtab, strtab;
339
340 // Consult a regular symbol table first.
341 if (GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
342 SHT_SYMTAB, &symtab)) {
343 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
344 symtab.sh_link * sizeof(symtab))) {
345 return false;
346 }
347 if (FindSymbol(pc, fd, out, out_size, base_address, &strtab, &symtab)) {
348 return true; // Found the symbol in a regular symbol table.
349 }
350 }
351
352 // If the symbol is not found, then consult a dynamic symbol table.
353 if (GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
354 SHT_DYNSYM, &symtab)) {
355 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
356 symtab.sh_link * sizeof(symtab))) {
357 return false;
358 }
359 if (FindSymbol(pc, fd, out, out_size, base_address, &strtab, &symtab)) {
360 return true; // Found the symbol in a dynamic symbol table.
361 }
362 }
363
364 return false;
365}
366
367namespace {
368// Thin wrapper around a file descriptor so that the file descriptor
369// gets closed for sure.
370struct FileDescriptor {
371 const int fd_;
372 explicit FileDescriptor(int fd) : fd_(fd) {}
373 ~FileDescriptor() {
374 if (fd_ >= 0) {
375 NO_INTR(close(fd_));
376 }
377 }
378 int get() { return fd_; }
379
380 private:
381 explicit FileDescriptor(const FileDescriptor&);
382 void operator=(const FileDescriptor&);
383};
384
385// Helper class for reading lines from file.
386//
387// Note: we don't use ProcMapsIterator since the object is big (it has
388// a 5k array member) and uses async-unsafe functions such as sscanf()
389// and snprintf().
390class LineReader {
391 public:
392 explicit LineReader(int fd, char *buf, int buf_len, off_t offset)
393 : fd_(fd),
394 buf_(buf),
395 buf_len_(buf_len),
396 offset_(offset),
397 bol_(buf),
398 eol_(buf),
399 eod_(buf) {}
400
401 // Read '\n'-terminated line from file. On success, modify "bol"
402 // and "eol", then return true. Otherwise, return false.
403 //
404 // Note: if the last line doesn't end with '\n', the line will be
405 // dropped. It's an intentional behavior to make the code simple.
406 bool ReadLine(const char **bol, const char **eol) {
407 if (BufferIsEmpty()) { // First time.
408 const ssize_t num_bytes = ReadFromOffset(fd_, buf_, buf_len_, offset_);
409 if (num_bytes <= 0) { // EOF or error.
410 return false;
411 }
412 offset_ += num_bytes;
413 eod_ = buf_ + num_bytes;
414 bol_ = buf_;
415 } else {
416 bol_ = eol_ + 1; // Advance to the next line in the buffer.
417 SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
418 if (!HasCompleteLine()) {
419 const int incomplete_line_length = eod_ - bol_;
420 // Move the trailing incomplete line to the beginning.
421 memmove(buf_, bol_, incomplete_line_length);
422 // Read text from file and append it.
423 char * const append_pos = buf_ + incomplete_line_length;
424 const int capacity_left = buf_len_ - incomplete_line_length;
425 const ssize_t num_bytes =
426 ReadFromOffset(fd_, append_pos, capacity_left, offset_);
427 if (num_bytes <= 0) { // EOF or error.
428 return false;
429 }
430 offset_ += num_bytes;
431 eod_ = append_pos + num_bytes;
432 bol_ = buf_;
433 }
434 }
435 eol_ = FindLineFeed();
436 if (eol_ == NULL) { // '\n' not found. Malformed line.
437 return false;
438 }
439 *eol_ = '\0'; // Replace '\n' with '\0'.
440
441 *bol = bol_;
442 *eol = eol_;
443 return true;
444 }
445
446 // Beginning of line.
447 const char *bol() {
448 return bol_;
449 }
450
451 // End of line.
452 const char *eol() {
453 return eol_;
454 }
455
456 private:
457 explicit LineReader(const LineReader&);
458 void operator=(const LineReader&);
459
460 char *FindLineFeed() {
461 return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_));
462 }
463
464 bool BufferIsEmpty() {
465 return buf_ == eod_;
466 }
467
468 bool HasCompleteLine() {
469 return !BufferIsEmpty() && FindLineFeed() != NULL;
470 }
471
472 const int fd_;
473 char * const buf_;
474 const int buf_len_;
475 off_t offset_;
476 char *bol_;
477 char *eol_;
478 const char *eod_; // End of data in "buf_".
479};
480} // namespace
481
482// Place the hex number read from "start" into "*hex". The pointer to
483// the first non-hex character or "end" is returned.
484static char *GetHex(const char *start, const char *end, uint64_t *hex) {
485 *hex = 0;
486 const char *p;
487 for (p = start; p < end; ++p) {
488 int ch = *p;
489 if ((ch >= '0' && ch <= '9') ||
490 (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
491 *hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
492 } else { // Encountered the first non-hex character.
493 break;
494 }
495 }
496 SAFE_ASSERT(p <= end);
497 return const_cast<char *>(p);
498}
499
500// Searches for the object file (from /proc/self/maps) that contains
501// the specified pc. If found, sets |start_address| to the start address
502// of where this object file is mapped in memory, sets the module base
503// address into |base_address|, copies the object file name into
504// |out_file_name|, and attempts to open the object file. If the object
505// file is opened successfully, returns the file descriptor. Otherwise,
506// returns -1. |out_file_name_size| is the size of the file name buffer
507// (including the null-terminator).
508static ATTRIBUTE_NOINLINE int
509OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
510 uint64_t &start_address,
511 uint64_t &base_address,
512 char *out_file_name,
513 int out_file_name_size) {
514 int object_fd;
515
516 int maps_fd;
517 NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
518 FileDescriptor wrapped_maps_fd(maps_fd);
519 if (wrapped_maps_fd.get() < 0) {
520 return -1;
521 }
522
523 int mem_fd;
524 NO_INTR(mem_fd = open("/proc/self/mem", O_RDONLY));
525 FileDescriptor wrapped_mem_fd(mem_fd);
526 if (wrapped_mem_fd.get() < 0) {
527 return -1;
528 }
529
530 // Iterate over maps and look for the map containing the pc. Then
531 // look into the symbol tables inside.
532 char buf[1024]; // Big enough for line of sane /proc/self/maps
533 int num_maps = 0;
534 LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf), 0);
535 while (true) {
536 num_maps++;
537 const char *cursor;
538 const char *eol;
539 if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line.
540 return -1;
541 }
542
543 // Start parsing line in /proc/self/maps. Here is an example:
544 //
545 // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat
546 //
547 // We want start address (08048000), end address (0804c000), flags
548 // (r-xp) and file name (/bin/cat).
549
550 // Read start address.
551 cursor = GetHex(cursor, eol, &start_address);
552 if (cursor == eol || *cursor != '-') {
553 return -1; // Malformed line.
554 }
555 ++cursor; // Skip '-'.
556
557 // Read end address.
558 uint64_t end_address;
559 cursor = GetHex(cursor, eol, &end_address);
560 if (cursor == eol || *cursor != ' ') {
561 return -1; // Malformed line.
562 }
563 ++cursor; // Skip ' '.
564
565 // Read flags. Skip flags until we encounter a space or eol.
566 const char * const flags_start = cursor;
567 while (cursor < eol && *cursor != ' ') {
568 ++cursor;
569 }
570 // We expect at least four letters for flags (ex. "r-xp").
571 if (cursor == eol || cursor < flags_start + 4) {
572 return -1; // Malformed line.
573 }
574
575 // Determine the base address by reading ELF headers in process memory.
576 ElfW(Ehdr) ehdr;
577 // Skip non-readable maps.
578 if (flags_start[0] == 'r' &&
579 ReadFromOffsetExact(mem_fd, &ehdr, sizeof(ElfW(Ehdr)), start_address) &&
580 memcmp(ehdr.e_ident, ELFMAG, SELFMAG) == 0) {
581 switch (ehdr.e_type) {
582 case ET_EXEC:
583 base_address = 0;
584 break;
585 case ET_DYN:
586 // Find the segment containing file offset 0. This will correspond
587 // to the ELF header that we just read. Normally this will have
588 // virtual address 0, but this is not guaranteed. We must subtract
589 // the virtual address from the address where the ELF header was
590 // mapped to get the base address.
591 //
592 // If we fail to find a segment for file offset 0, use the address
593 // of the ELF header as the base address.
594 base_address = start_address;
595 for (unsigned i = 0; i != ehdr.e_phnum; ++i) {
596 ElfW(Phdr) phdr;
597 if (ReadFromOffsetExact(
598 mem_fd, &phdr, sizeof(phdr),
599 start_address + ehdr.e_phoff + i * sizeof(phdr)) &&
600 phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
601 base_address = start_address - phdr.p_vaddr;
602 break;
603 }
604 }
605 break;
606 default:
607 // ET_REL or ET_CORE. These aren't directly executable, so they don't
608 // affect the base address.
609 break;
610 }
611 }
612
613 // Check start and end addresses.
614 if (!(start_address <= pc && pc < end_address)) {
615 continue; // We skip this map. PC isn't in this map.
616 }
617
618 // Check flags. We are only interested in "r*x" maps.
619 if (flags_start[0] != 'r' || flags_start[2] != 'x') {
620 continue; // We skip this map.
621 }
622 ++cursor; // Skip ' '.
623
624 // Read file offset.
625 uint64_t file_offset;
626 cursor = GetHex(cursor, eol, &file_offset);
627 if (cursor == eol || *cursor != ' ') {
628 return -1; // Malformed line.
629 }
630 ++cursor; // Skip ' '.
631
632 // Skip to file name. "cursor" now points to dev. We need to
633 // skip at least two spaces for dev and inode.
634 int num_spaces = 0;
635 while (cursor < eol) {
636 if (*cursor == ' ') {
637 ++num_spaces;
638 } else if (num_spaces >= 2) {
639 // The first non-space character after skipping two spaces
640 // is the beginning of the file name.
641 break;
642 }
643 ++cursor;
644 }
645 if (cursor == eol) {
646 return -1; // Malformed line.
647 }
648
649 // Finally, "cursor" now points to file name of our interest.
650 NO_INTR(object_fd = open(cursor, O_RDONLY));
651 if (object_fd < 0) {
652 // Failed to open object file. Copy the object file name to
653 // |out_file_name|.
654 strncpy(out_file_name, cursor, out_file_name_size);
655 // Making sure |out_file_name| is always null-terminated.
656 out_file_name[out_file_name_size - 1] = '\0';
657 return -1;
658 }
659 return object_fd;
660 }
661}
662
663// POSIX doesn't define any async-signal safe function for converting
664// an integer to ASCII. We'll have to define our own version.
665// itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
666// conversion was successful or NULL otherwise. It never writes more than "sz"
667// bytes. Output will be truncated as needed, and a NUL character is always
668// appended.
669// NOTE: code from sandbox/linux/seccomp-bpf/demo.cc.
670static char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) {
671 // Make sure we can write at least one NUL byte.
672 size_t n = 1;
673 if (n > sz)
674 return NULL;
675
676 if (base < 2 || base > 16) {
677 buf[0] = '\000';
678 return NULL;
679 }
680
681 char *start = buf;
682
683 uintptr_t j = i;
684
685 // Handle negative numbers (only for base 10).
686 if (i < 0 && base == 10) {
687 // This does "j = -i" while avoiding integer overflow.
688 j = static_cast<uintptr_t>(-(i + 1)) + 1;
689
690 // Make sure we can write the '-' character.
691 if (++n > sz) {
692 buf[0] = '\000';
693 return NULL;
694 }
695 *start++ = '-';
696 }
697
698 // Loop until we have converted the entire number. Output at least one
699 // character (i.e. '0').
700 char *ptr = start;
701 do {
702 // Make sure there is still enough space left in our output buffer.
703 if (++n > sz) {
704 buf[0] = '\000';
705 return NULL;
706 }
707
708 // Output the next digit.
709 *ptr++ = "0123456789abcdef"[j % base];
710 j /= base;
711
712 if (padding > 0)
713 padding--;
714 } while (j > 0 || padding > 0);
715
716 // Terminate the output with a NUL character.
717 *ptr = '\000';
718
719 // Conversion to ASCII actually resulted in the digits being in reverse
720 // order. We can't easily generate them in forward order, as we can't tell
721 // the number of characters needed until we are done converting.
722 // So, now, we reverse the string (except for the possible "-" sign).
723 while (--ptr > start) {
724 char ch = *ptr;
725 *ptr = *start;
726 *start++ = ch;
727 }
728 return buf;
729}
730
731// Safely appends string |source| to string |dest|. Never writes past the
732// buffer size |dest_size| and guarantees that |dest| is null-terminated.
733static void SafeAppendString(const char* source, char* dest, int dest_size) {
734 int dest_string_length = strlen(dest);
735 SAFE_ASSERT(dest_string_length < dest_size);
736 dest += dest_string_length;
737 dest_size -= dest_string_length;
738 strncpy(dest, source, dest_size);
739 // Making sure |dest| is always null-terminated.
740 dest[dest_size - 1] = '\0';
741}
742
743// Converts a 64-bit value into a hex string, and safely appends it to |dest|.
744// Never writes past the buffer size |dest_size| and guarantees that |dest| is
745// null-terminated.
746static void SafeAppendHexNumber(uint64_t value, char* dest, int dest_size) {
747 // 64-bit numbers in hex can have up to 16 digits.
748 char buf[17] = {'\0'};
749 SafeAppendString(itoa_r(value, buf, sizeof(buf), 16, 0), dest, dest_size);
750}
751
752// The implementation of our symbolization routine. If it
753// successfully finds the symbol containing "pc" and obtains the
754// symbol name, returns true and write the symbol name to "out".
755// Otherwise, returns false. If Callback function is installed via
756// InstallSymbolizeCallback(), the function is also called in this function,
757// and "out" is used as its output.
758// To keep stack consumption low, we would like this function to not
759// get inlined.
760static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
761 int out_size) {
762 uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
763 uint64_t start_address = 0;
764 uint64_t base_address = 0;
765 int object_fd = -1;
766
767 if (out_size < 1) {
768 return false;
769 }
770 out[0] = '\0';
771 SafeAppendString("(", out, out_size);
772
773 if (g_symbolize_open_object_file_callback) {
774 object_fd = g_symbolize_open_object_file_callback(pc0, start_address,
775 base_address, out + 1,
776 out_size - 1);
777 } else {
778 object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0, start_address,
779 base_address,
780 out + 1,
781 out_size - 1);
782 }
783
784#if defined(PRINT_UNSYMBOLIZED_STACK_TRACES)
785 {
786 FileDescriptor wrapped_object_fd(object_fd);
787#else
788 // Check whether a file name was returned.
789 if (object_fd < 0) {
790#endif
791 if (out[1]) {
792 // The object file containing PC was determined successfully however the
793 // object file was not opened successfully. This is still considered
794 // success because the object file name and offset are known and tools
795 // like asan_symbolize.py can be used for the symbolization.
796 out[out_size - 1] = '\0'; // Making sure |out| is always null-terminated.
797 SafeAppendString("+0x", out, out_size);
798 SafeAppendHexNumber(pc0 - base_address, out, out_size);
799 SafeAppendString(")", out, out_size);
800 return true;
801 }
802 // Failed to determine the object file containing PC. Bail out.
803 return false;
804 }
805 FileDescriptor wrapped_object_fd(object_fd);
806 int elf_type = FileGetElfType(wrapped_object_fd.get());
807 if (elf_type == -1) {
808 return false;
809 }
810 if (g_symbolize_callback) {
811 // Run the call back if it's installed.
812 // Note: relocation (and much of the rest of this code) will be
813 // wrong for prelinked shared libraries and PIE executables.
814 uint64_t relocation = (elf_type == ET_DYN) ? start_address : 0;
815 int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
816 pc, out, out_size,
817 relocation);
818 if (num_bytes_written > 0) {
819 out += num_bytes_written;
820 out_size -= num_bytes_written;
821 }
822 }
823 if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
824 out, out_size, base_address)) {
825 return false;
826 }
827
828 // Symbolization succeeded. Now we try to demangle the symbol.
829 DemangleInplace(out, out_size);
830 return true;
831}
832
833_END_GOOGLE_NAMESPACE_
834
835#elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
836
837#include <dlfcn.h>
838#include <string.h>
839
840_START_GOOGLE_NAMESPACE_
841
842static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
843 int out_size) {
844 Dl_info info;
845 if (dladdr(pc, &info)) {
846 if ((int)strlen(info.dli_sname) < out_size) {
847 strcpy(out, info.dli_sname);
848 // Symbolization succeeded. Now we try to demangle the symbol.
849 DemangleInplace(out, out_size);
850 return true;
851 }
852 }
853 return false;
854}
855
856_END_GOOGLE_NAMESPACE_
857
858#elif defined(OS_WINDOWS) || defined(OS_CYGWIN)
859
860#include <windows.h>
861#include <dbghelp.h>
862
863#ifdef _MSC_VER
864#pragma comment(lib, "dbghelp")
865#endif
866
867_START_GOOGLE_NAMESPACE_
868
869class SymInitializer {
870public:
871 HANDLE process;
872 bool ready;
873 SymInitializer() : process(NULL), ready(false) {
874 // Initialize the symbol handler.
875 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680344(v=vs.85).aspx
876 process = GetCurrentProcess();
877 // Defer symbol loading.
878 // We do not request undecorated symbols with SYMOPT_UNDNAME
879 // because the mangling library calls UnDecorateSymbolName.
880 SymSetOptions(SYMOPT_DEFERRED_LOADS);
881 if (SymInitialize(process, NULL, true)) {
882 ready = true;
883 }
884 }
885 ~SymInitializer() {
886 SymCleanup(process);
887 // We do not need to close `HANDLE process` because it's a "pseudo handle."
888 }
889private:
890 SymInitializer(const SymInitializer&);
891 SymInitializer& operator=(const SymInitializer&);
892};
893
894static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
895 int out_size) {
896 const static SymInitializer symInitializer;
897 if (!symInitializer.ready) {
898 return false;
899 }
900 // Resolve symbol information from address.
901 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680578(v=vs.85).aspx
902 char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
903 SYMBOL_INFO *symbol = reinterpret_cast<SYMBOL_INFO *>(buf);
904 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
905 symbol->MaxNameLen = MAX_SYM_NAME;
906 // We use the ANSI version to ensure the string type is always `char *`.
907 // This could break if a symbol has Unicode in it.
908 BOOL ret = SymFromAddr(symInitializer.process,
909 reinterpret_cast<DWORD64>(pc), 0, symbol);
910 if (ret == 1 && static_cast<int>(symbol->NameLen) < out_size) {
911 // `NameLen` does not include the null terminating character.
912 strncpy(out, symbol->Name, static_cast<size_t>(symbol->NameLen) + 1);
913 out[static_cast<size_t>(symbol->NameLen)] = '\0';
914 // Symbolization succeeded. Now we try to demangle the symbol.
915 DemangleInplace(out, out_size);
916 return true;
917 }
918 return false;
919}
920
921_END_GOOGLE_NAMESPACE_
922
923#else
924# error BUG: HAVE_SYMBOLIZE was wrongly set
925#endif
926
927_START_GOOGLE_NAMESPACE_
928
929bool Symbolize(void *pc, char *out, int out_size) {
930 SAFE_ASSERT(out_size >= 0);
931 return SymbolizeAndDemangle(pc, out, out_size);
932}
933
934_END_GOOGLE_NAMESPACE_
935
936#else /* HAVE_SYMBOLIZE */
937
938#include <assert.h>
939
940#include "config.h"
941
942_START_GOOGLE_NAMESPACE_
943
944// TODO: Support other environments.
945bool Symbolize(void *pc, char *out, int out_size) {
946 assert(0);
947 return false;
948}
949
950_END_GOOGLE_NAMESPACE_
951
952#endif