1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
3 // Stack-footprint reduction work done by Raksit Ashok
5 // Implementation note:
7 // We don't use heaps but only use stacks. We want to reduce the
8 // stack consumption so that the symbolizer can run on small stacks.
10 // Here are some numbers collected with GCC 4.1.0 on x86:
11 // - sizeof(Elf32_Sym) = 16
12 // - sizeof(Elf32_Shdr) = 40
13 // - sizeof(Elf64_Sym) = 24
14 // - sizeof(Elf64_Shdr) = 64
16 // This implementation is intended to be async-signal-safe but uses
17 // some functions which are not guaranteed to be so, such as memchr()
18 // and memmove(). We assume they are async-signal-safe.
21 #include "utilities.h"
23 #if defined(HAVE_SYMBOLIZE)
25 #include "symbolize.h"
28 _START_GOOGLE_NAMESPACE_
30 // We don't use assert() since it's not guaranteed to be
31 // async-signal-safe. Instead we define a minimal assertion
32 // macro. So far, we don't need pretty printing for __FILE__, etc.
34 // A wrapper for abort() to make it callable in ? :.
35 static int AssertFail() {
37 return 0; // Should not reach.
40 #define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
42 static SymbolizeCallback g_symbolize_callback = NULL;
43 void InstallSymbolizeCallback(SymbolizeCallback callback) {
44 g_symbolize_callback = callback;
47 // This function wraps the Demangle function to provide an interface
48 // where the input symbol is demangled in-place.
49 // To keep stack consumption low, we would like this function to not
51 static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
52 char demangled[256]; // Big enough for sane demangled symbols.
53 if (Demangle(out, demangled, sizeof(demangled))) {
54 // Demangling succeeded. Copy to out if the space allows.
55 int len = strlen(demangled);
56 if (len + 1 <= out_size) { // +1 for '\0'.
57 SAFE_ASSERT(len < sizeof(demangled));
58 memmove(out, demangled, len + 1);
63 _END_GOOGLE_NAMESPACE_
72 #include <link.h> // For ElfW() macro.
79 #include <sys/types.h>
82 #include "symbolize.h"
84 #include "glog/raw_logging.h"
86 // Re-runs fn until it doesn't cause EINTR.
87 #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
89 _START_GOOGLE_NAMESPACE_
91 // Read up to "count" bytes from file descriptor "fd" into the buffer
92 // starting at "buf" while handling short reads and EINTR. On
93 // success, return the number of bytes read. Otherwise, return -1.
94 static ssize_t ReadPersistent(const int fd, void *buf, const size_t count) {
96 SAFE_ASSERT(count >= 0 && count <= SSIZE_MAX);
97 char *buf0 = reinterpret_cast<char *>(buf);
98 ssize_t num_bytes = 0;
99 while (num_bytes < count) {
101 NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
102 if (len < 0) { // There was an error other than EINTR.
105 if (len == 0) { // Reached EOF.
110 SAFE_ASSERT(num_bytes <= count);
114 // Read up to "count" bytes from "offset" in the file pointed by file
115 // descriptor "fd" into the buffer starting at "buf". On success,
116 // return the number of bytes read. Otherwise, return -1.
117 static ssize_t ReadFromOffset(const int fd, void *buf,
118 const size_t count, const off_t offset) {
119 off_t off = lseek(fd, offset, SEEK_SET);
120 if (off == (off_t)-1) {
123 return ReadPersistent(fd, buf, count);
126 // Try reading exactly "count" bytes from "offset" bytes in a file
127 // pointed by "fd" into the buffer starting at "buf" while handling
128 // short reads and EINTR. On success, return true. Otherwise, return
130 static bool ReadFromOffsetExact(const int fd, void *buf,
131 const size_t count, const off_t offset) {
132 ssize_t len = ReadFromOffset(fd, buf, count, offset);
136 // Returns elf_header.e_type if the file pointed by fd is an ELF binary.
137 static int FileGetElfType(const int fd) {
138 ElfW(Ehdr) elf_header;
139 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
142 if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
145 return elf_header.e_type;
148 // Read the section headers in the given ELF binary, and if a section
149 // of the specified type is found, set the output to this section header
150 // and return true. Otherwise, return false.
151 // To keep stack consumption low, we would like this function to not get
153 static ATTRIBUTE_NOINLINE bool
154 GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
155 ElfW(Word) type, ElfW(Shdr) *out) {
156 // Read at most 16 section headers at a time to save read calls.
158 for (int i = 0; i < sh_num;) {
159 const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
160 const ssize_t num_bytes_to_read =
161 (sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
162 const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
163 sh_offset + i * sizeof(buf[0]));
164 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
165 const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
166 SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
167 for (int j = 0; j < num_headers_in_buf; ++j) {
168 if (buf[j].sh_type == type) {
173 i += num_headers_in_buf;
178 // There is no particular reason to limit section name to 63 characters,
179 // but there has (as yet) been no need for anything longer either.
180 const int kMaxSectionNameLen = 64;
182 // name_len should include terminating '\0'.
183 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
185 ElfW(Ehdr) elf_header;
186 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
191 off_t shstrtab_offset = (elf_header.e_shoff +
192 elf_header.e_shentsize * elf_header.e_shstrndx);
193 if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
197 for (int i = 0; i < elf_header.e_shnum; ++i) {
198 off_t section_header_offset = (elf_header.e_shoff +
199 elf_header.e_shentsize * i);
200 if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
203 char header_name[kMaxSectionNameLen];
204 if (sizeof(header_name) < name_len) {
205 RAW_LOG(WARNING, "Section name '%s' is too long (%"PRIuS"); "
206 "section will not be found (even if present).", name, name_len);
207 // No point in even trying.
210 off_t name_offset = shstrtab.sh_offset + out->sh_name;
211 ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
214 } else if (n_read != name_len) {
215 // Short read -- name could be at end of file.
218 if (memcmp(header_name, name, name_len) == 0) {
225 // Read a symbol table and look for the symbol containing the
226 // pc. Iterate over symbols in a symbol table and look for the symbol
227 // containing "pc". On success, return true and write the symbol name
228 // to out. Otherwise, return false.
229 // To keep stack consumption low, we would like this function to not get
231 static ATTRIBUTE_NOINLINE bool
232 FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
233 uint64_t symbol_offset, const ElfW(Shdr) *strtab,
234 const ElfW(Shdr) *symtab) {
235 if (symtab == NULL) {
238 const int num_symbols = symtab->sh_size / symtab->sh_entsize;
239 for (int i = 0; i < num_symbols;) {
240 off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
242 // If we are reading Elf64_Sym's, we want to limit this array to
243 // 32 elements (to keep stack consumption low), otherwise we can
244 // have a 64 element Elf32_Sym array.
246 #define NUM_SYMBOLS 32
248 #define NUM_SYMBOLS 64
251 // Read at most NUM_SYMBOLS symbols at once to save read() calls.
252 ElfW(Sym) buf[NUM_SYMBOLS];
253 const ssize_t len = ReadFromOffset(fd, &buf, sizeof(buf), offset);
254 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
255 const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
256 SAFE_ASSERT(num_symbols_in_buf <= sizeof(buf)/sizeof(buf[0]));
257 for (int j = 0; j < num_symbols_in_buf; ++j) {
258 const ElfW(Sym)& symbol = buf[j];
259 uint64_t start_address = symbol.st_value;
260 start_address += symbol_offset;
261 uint64_t end_address = start_address + symbol.st_size;
262 if (symbol.st_value != 0 && // Skip null value symbols.
263 symbol.st_shndx != 0 && // Skip undefined symbols.
264 start_address <= pc && pc < end_address) {
265 ssize_t len1 = ReadFromOffset(fd, out, out_size,
266 strtab->sh_offset + symbol.st_name);
267 if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
270 return true; // Obtained the symbol name.
273 i += num_symbols_in_buf;
278 // Get the symbol name of "pc" from the file pointed by "fd". Process
279 // both regular and dynamic symbol tables if necessary. On success,
280 // write the symbol name to "out" and return true. Otherwise, return
282 static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
283 char *out, int out_size,
284 uint64_t map_start_address) {
285 // Read the ELF header.
286 ElfW(Ehdr) elf_header;
287 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
291 uint64_t symbol_offset = 0;
292 if (elf_header.e_type == ET_DYN) { // DSO needs offset adjustment.
293 symbol_offset = map_start_address;
296 ElfW(Shdr) symtab, strtab;
298 // Consult a regular symbol table first.
299 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
300 SHT_SYMTAB, &symtab)) {
303 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
304 symtab.sh_link * sizeof(symtab))) {
307 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
309 return true; // Found the symbol in a regular symbol table.
312 // If the symbol is not found, then consult a dynamic symbol table.
313 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
314 SHT_DYNSYM, &symtab)) {
317 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
318 symtab.sh_link * sizeof(symtab))) {
321 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
323 return true; // Found the symbol in a dynamic symbol table.
330 // Thin wrapper around a file descriptor so that the file descriptor
331 // gets closed for sure.
332 struct FileDescriptor {
334 explicit FileDescriptor(int fd) : fd_(fd) {}
340 int get() { return fd_; }
343 explicit FileDescriptor(const FileDescriptor&);
344 void operator=(const FileDescriptor&);
347 // Helper class for reading lines from file.
349 // Note: we don't use ProcMapsIterator since the object is big (it has
350 // a 5k array member) and uses async-unsafe functions such as sscanf()
354 explicit LineReader(int fd, char *buf, int buf_len) : fd_(fd),
355 buf_(buf), buf_len_(buf_len), bol_(buf), eol_(buf), eod_(buf) {
358 // Read '\n'-terminated line from file. On success, modify "bol"
359 // and "eol", then return true. Otherwise, return false.
361 // Note: if the last line doesn't end with '\n', the line will be
362 // dropped. It's an intentional behavior to make the code simple.
363 bool ReadLine(const char **bol, const char **eol) {
364 if (BufferIsEmpty()) { // First time.
365 const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
366 if (num_bytes <= 0) { // EOF or error.
369 eod_ = buf_ + num_bytes;
372 bol_ = eol_ + 1; // Advance to the next line in the buffer.
373 SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
374 if (!HasCompleteLine()) {
375 const int incomplete_line_length = eod_ - bol_;
376 // Move the trailing incomplete line to the beginning.
377 memmove(buf_, bol_, incomplete_line_length);
378 // Read text from file and append it.
379 char * const append_pos = buf_ + incomplete_line_length;
380 const int capacity_left = buf_len_ - incomplete_line_length;
381 const ssize_t num_bytes = ReadPersistent(fd_, append_pos,
383 if (num_bytes <= 0) { // EOF or error.
386 eod_ = append_pos + num_bytes;
390 eol_ = FindLineFeed();
391 if (eol_ == NULL) { // '\n' not found. Malformed line.
394 *eol_ = '\0'; // Replace '\n' with '\0'.
401 // Beginning of line.
412 explicit LineReader(const LineReader&);
413 void operator=(const LineReader&);
415 char *FindLineFeed() {
416 return reinterpret_cast<char *>
417 (memchr(reinterpret_cast<const void *>(bol_), '\n', eod_ - bol_));
420 bool BufferIsEmpty() {
424 bool HasCompleteLine() {
425 return !BufferIsEmpty() && FindLineFeed() != NULL;
433 const char *eod_; // End of data in "buf_".
437 // Place the hex number read from "start" into "*hex". The pointer to
438 // the first non-hex character or "end" is returned.
439 static char *GetHex(const char *start, const char *end, uint64_t *hex) {
442 for (p = start; p < end; ++p) {
444 if ((ch >= '0' && ch <= '9') ||
445 (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
446 *hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
447 } else { // Encountered the first non-hex character.
451 SAFE_ASSERT(p <= end);
452 return const_cast<char *>(p);
455 // Search for the object file (from /proc/self/maps) that contains
456 // the specified pc. If found, open this file and return the file handle,
457 // and also set start_address to the start address of where this object
458 // file is mapped to in memory. Otherwise, return -1.
459 static ATTRIBUTE_NOINLINE int
460 OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
461 uint64_t &start_address) {
464 // Open /proc/self/maps.
466 NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
467 FileDescriptor wrapped_maps_fd(maps_fd);
468 if (wrapped_maps_fd.get() < 0) {
472 // Iterate over maps and look for the map containing the pc. Then
473 // look into the symbol tables inside.
474 char buf[1024]; // Big enough for line of sane /proc/self/maps
475 LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf));
479 if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line.
483 // Start parsing line in /proc/self/maps. Here is an example:
485 // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat
487 // We want start address (08048000), end address (0804c000), flags
488 // (r-xp) and file name (/bin/cat).
490 // Read start address.
491 cursor = GetHex(cursor, eol, &start_address);
492 if (cursor == eol || *cursor != '-') {
493 return -1; // Malformed line.
495 ++cursor; // Skip '-'.
498 uint64_t end_address;
499 cursor = GetHex(cursor, eol, &end_address);
500 if (cursor == eol || *cursor != ' ') {
501 return -1; // Malformed line.
503 ++cursor; // Skip ' '.
505 // Check start and end addresses.
506 if (!(start_address <= pc && pc < end_address)) {
507 continue; // We skip this map. PC isn't in this map.
510 // Read flags. Skip flags until we encounter a space or eol.
511 const char * const flags_start = cursor;
512 while (cursor < eol && *cursor != ' ') {
515 // We expect at least four letters for flags (ex. "r-xp").
516 if (cursor == eol || cursor < flags_start + 4) {
517 return -1; // Malformed line.
520 // Check flags. We are only interested in "r-x" maps.
521 if (memcmp(flags_start, "r-x", 3) != 0) { // Not a "r-x" map.
522 continue; // We skip this map.
524 ++cursor; // Skip ' '.
526 // Skip to file name. "cursor" now points to file offset. We need to
527 // skip at least three spaces for file offset, dev, and inode.
529 while (cursor < eol) {
530 if (*cursor == ' ') {
532 } else if (num_spaces >= 3) {
533 // The first non-space character after skipping three spaces
534 // is the beginning of the file name.
540 return -1; // Malformed line.
543 // Finally, "cursor" now points to file name of our interest.
544 NO_INTR(object_fd = open(cursor, O_RDONLY));
552 // The implementation of our symbolization routine. If it
553 // successfully finds the symbol containing "pc" and obtains the
554 // symbol name, returns true and write the symbol name to "out".
555 // Otherwise, returns false. If Callback function is installed via
556 // InstallSymbolizeCallback(), the function is also called in this function,
557 // and "out" is used as its output.
558 // To keep stack consumption low, we would like this function to not
560 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
562 uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
563 uint64_t start_address = 0;
565 int object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0,
567 if (object_fd == -1) {
570 FileDescriptor wrapped_object_fd(object_fd);
571 int elf_type = FileGetElfType(wrapped_object_fd.get());
572 if (elf_type == -1) {
575 if (g_symbolize_callback) {
576 // Run the call back if it's installed.
577 // Note: relocation (and much of the rest of this code) will be
578 // wrong for prelinked shared libraries and PIE executables.
579 uint64 relocation = (elf_type == ET_DYN) ? start_address : 0;
580 int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
583 if (num_bytes_written > 0) {
584 out += num_bytes_written;
585 out_size -= num_bytes_written;
588 if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
589 out, out_size, start_address)) {
593 // Symbolization succeeded. Now we try to demangle the symbol.
594 DemangleInplace(out, out_size);
598 _END_GOOGLE_NAMESPACE_
600 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
605 _START_GOOGLE_NAMESPACE_
607 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
610 if (dladdr(pc, &info)) {
611 if (strlen(info.dli_sname) < out_size) {
612 strcpy(out, info.dli_sname);
613 // Symbolization succeeded. Now we try to demangle the symbol.
614 DemangleInplace(out, out_size);
621 _END_GOOGLE_NAMESPACE_
624 # error BUG: HAVE_SYMBOLIZE was wrongly set
627 _START_GOOGLE_NAMESPACE_
629 bool Symbolize(void *pc, char *out, int out_size) {
630 SAFE_ASSERT(out_size >= 0);
631 return SymbolizeAndDemangle(pc, out, out_size);
634 _END_GOOGLE_NAMESPACE_
636 #else /* HAVE_SYMBOLIZE */
642 _START_GOOGLE_NAMESPACE_
644 // TODO: Support other environments.
645 bool Symbolize(void *pc, char *out, int out_size) {
650 _END_GOOGLE_NAMESPACE_