1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: Satoru Takabayashi
31 // Stack-footprint reduction work done by Raksit Ashok
33 // Implementation note:
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.
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
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.
49 #include "utilities.h"
51 #if defined(HAVE_SYMBOLIZE)
53 #include "symbolize.h"
56 _START_GOOGLE_NAMESPACE_
58 // We don't use assert() since it's not guaranteed to be
59 // async-signal-safe. Instead we define a minimal assertion
60 // macro. So far, we don't need pretty printing for __FILE__, etc.
62 // A wrapper for abort() to make it callable in ? :.
63 static int AssertFail() {
65 return 0; // Should not reach.
68 #define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
70 static SymbolizeCallback g_symbolize_callback = NULL;
71 void InstallSymbolizeCallback(SymbolizeCallback callback) {
72 g_symbolize_callback = callback;
75 // This function wraps the Demangle function to provide an interface
76 // where the input symbol is demangled in-place.
77 // To keep stack consumption low, we would like this function to not
79 static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) {
80 char demangled[256]; // Big enough for sane demangled symbols.
81 if (Demangle(out, demangled, sizeof(demangled))) {
82 // Demangling succeeded. Copy to out if the space allows.
83 int len = strlen(demangled);
84 if (len + 1 <= out_size) { // +1 for '\0'.
85 SAFE_ASSERT(len < sizeof(demangled));
86 memmove(out, demangled, len + 1);
91 _END_GOOGLE_NAMESPACE_
100 #include <link.h> // For ElfW() macro.
106 #include <sys/stat.h>
107 #include <sys/types.h>
110 #include "symbolize.h"
112 #include "glog/raw_logging.h"
114 // Re-runs fn until it doesn't cause EINTR.
115 #define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
117 _START_GOOGLE_NAMESPACE_
119 // Read up to "count" bytes from file descriptor "fd" into the buffer
120 // starting at "buf" while handling short reads and EINTR. On
121 // success, return the number of bytes read. Otherwise, return -1.
122 static ssize_t ReadPersistent(const int fd, void *buf, const size_t count) {
123 SAFE_ASSERT(fd >= 0);
124 SAFE_ASSERT(count >= 0 && count <= SSIZE_MAX);
125 char *buf0 = reinterpret_cast<char *>(buf);
126 ssize_t num_bytes = 0;
127 while (num_bytes < count) {
129 NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
130 if (len < 0) { // There was an error other than EINTR.
133 if (len == 0) { // Reached EOF.
138 SAFE_ASSERT(num_bytes <= count);
142 // Read up to "count" bytes from "offset" in the file pointed by file
143 // descriptor "fd" into the buffer starting at "buf". On success,
144 // return the number of bytes read. Otherwise, return -1.
145 static ssize_t ReadFromOffset(const int fd, void *buf,
146 const size_t count, const off_t offset) {
147 off_t off = lseek(fd, offset, SEEK_SET);
148 if (off == (off_t)-1) {
151 return ReadPersistent(fd, buf, count);
154 // Try reading exactly "count" bytes from "offset" bytes in a file
155 // pointed by "fd" into the buffer starting at "buf" while handling
156 // short reads and EINTR. On success, return true. Otherwise, return
158 static bool ReadFromOffsetExact(const int fd, void *buf,
159 const size_t count, const off_t offset) {
160 ssize_t len = ReadFromOffset(fd, buf, count, offset);
164 // Returns elf_header.e_type if the file pointed by fd is an ELF binary.
165 static int FileGetElfType(const int fd) {
166 ElfW(Ehdr) elf_header;
167 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
170 if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
173 return elf_header.e_type;
176 // Read the section headers in the given ELF binary, and if a section
177 // of the specified type is found, set the output to this section header
178 // and return true. Otherwise, return false.
179 // To keep stack consumption low, we would like this function to not get
181 static ATTRIBUTE_NOINLINE bool
182 GetSectionHeaderByType(const int fd, ElfW(Half) sh_num, const off_t sh_offset,
183 ElfW(Word) type, ElfW(Shdr) *out) {
184 // Read at most 16 section headers at a time to save read calls.
186 for (int i = 0; i < sh_num;) {
187 const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]);
188 const ssize_t num_bytes_to_read =
189 (sizeof(buf) > num_bytes_left) ? num_bytes_left : sizeof(buf);
190 const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read,
191 sh_offset + i * sizeof(buf[0]));
192 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
193 const ssize_t num_headers_in_buf = len / sizeof(buf[0]);
194 SAFE_ASSERT(num_headers_in_buf <= sizeof(buf) / sizeof(buf[0]));
195 for (int j = 0; j < num_headers_in_buf; ++j) {
196 if (buf[j].sh_type == type) {
201 i += num_headers_in_buf;
206 // There is no particular reason to limit section name to 63 characters,
207 // but there has (as yet) been no need for anything longer either.
208 const int kMaxSectionNameLen = 64;
210 // name_len should include terminating '\0'.
211 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
213 ElfW(Ehdr) elf_header;
214 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
219 off_t shstrtab_offset = (elf_header.e_shoff +
220 elf_header.e_shentsize * elf_header.e_shstrndx);
221 if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
225 for (int i = 0; i < elf_header.e_shnum; ++i) {
226 off_t section_header_offset = (elf_header.e_shoff +
227 elf_header.e_shentsize * i);
228 if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
231 char header_name[kMaxSectionNameLen];
232 if (sizeof(header_name) < name_len) {
233 RAW_LOG(WARNING, "Section name '%s' is too long (%"PRIuS"); "
234 "section will not be found (even if present).", name, name_len);
235 // No point in even trying.
238 off_t name_offset = shstrtab.sh_offset + out->sh_name;
239 ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
242 } else if (n_read != name_len) {
243 // Short read -- name could be at end of file.
246 if (memcmp(header_name, name, name_len) == 0) {
253 // Read a symbol table and look for the symbol containing the
254 // pc. Iterate over symbols in a symbol table and look for the symbol
255 // containing "pc". On success, return true and write the symbol name
256 // to out. Otherwise, return false.
257 // To keep stack consumption low, we would like this function to not get
259 static ATTRIBUTE_NOINLINE bool
260 FindSymbol(uint64_t pc, const int fd, char *out, int out_size,
261 uint64_t symbol_offset, const ElfW(Shdr) *strtab,
262 const ElfW(Shdr) *symtab) {
263 if (symtab == NULL) {
266 const int num_symbols = symtab->sh_size / symtab->sh_entsize;
267 for (int i = 0; i < num_symbols;) {
268 off_t offset = symtab->sh_offset + i * symtab->sh_entsize;
270 // If we are reading Elf64_Sym's, we want to limit this array to
271 // 32 elements (to keep stack consumption low), otherwise we can
272 // have a 64 element Elf32_Sym array.
274 #define NUM_SYMBOLS 32
276 #define NUM_SYMBOLS 64
279 // Read at most NUM_SYMBOLS symbols at once to save read() calls.
280 ElfW(Sym) buf[NUM_SYMBOLS];
281 const ssize_t len = ReadFromOffset(fd, &buf, sizeof(buf), offset);
282 SAFE_ASSERT(len % sizeof(buf[0]) == 0);
283 const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
284 SAFE_ASSERT(num_symbols_in_buf <= sizeof(buf)/sizeof(buf[0]));
285 for (int j = 0; j < num_symbols_in_buf; ++j) {
286 const ElfW(Sym)& symbol = buf[j];
287 uint64_t start_address = symbol.st_value;
288 start_address += symbol_offset;
289 uint64_t end_address = start_address + symbol.st_size;
290 if (symbol.st_value != 0 && // Skip null value symbols.
291 symbol.st_shndx != 0 && // Skip undefined symbols.
292 start_address <= pc && pc < end_address) {
293 ssize_t len1 = ReadFromOffset(fd, out, out_size,
294 strtab->sh_offset + symbol.st_name);
295 if (len1 <= 0 || memchr(out, '\0', out_size) == NULL) {
298 return true; // Obtained the symbol name.
301 i += num_symbols_in_buf;
306 // Get the symbol name of "pc" from the file pointed by "fd". Process
307 // both regular and dynamic symbol tables if necessary. On success,
308 // write the symbol name to "out" and return true. Otherwise, return
310 static bool GetSymbolFromObjectFile(const int fd, uint64_t pc,
311 char *out, int out_size,
312 uint64_t map_start_address) {
313 // Read the ELF header.
314 ElfW(Ehdr) elf_header;
315 if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
319 uint64_t symbol_offset = 0;
320 if (elf_header.e_type == ET_DYN) { // DSO needs offset adjustment.
321 symbol_offset = map_start_address;
324 ElfW(Shdr) symtab, strtab;
326 // Consult a regular symbol table first.
327 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
328 SHT_SYMTAB, &symtab)) {
331 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
332 symtab.sh_link * sizeof(symtab))) {
335 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
337 return true; // Found the symbol in a regular symbol table.
340 // If the symbol is not found, then consult a dynamic symbol table.
341 if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
342 SHT_DYNSYM, &symtab)) {
345 if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
346 symtab.sh_link * sizeof(symtab))) {
349 if (FindSymbol(pc, fd, out, out_size, symbol_offset,
351 return true; // Found the symbol in a dynamic symbol table.
358 // Thin wrapper around a file descriptor so that the file descriptor
359 // gets closed for sure.
360 struct FileDescriptor {
362 explicit FileDescriptor(int fd) : fd_(fd) {}
368 int get() { return fd_; }
371 explicit FileDescriptor(const FileDescriptor&);
372 void operator=(const FileDescriptor&);
375 // Helper class for reading lines from file.
377 // Note: we don't use ProcMapsIterator since the object is big (it has
378 // a 5k array member) and uses async-unsafe functions such as sscanf()
382 explicit LineReader(int fd, char *buf, int buf_len) : fd_(fd),
383 buf_(buf), buf_len_(buf_len), bol_(buf), eol_(buf), eod_(buf) {
386 // Read '\n'-terminated line from file. On success, modify "bol"
387 // and "eol", then return true. Otherwise, return false.
389 // Note: if the last line doesn't end with '\n', the line will be
390 // dropped. It's an intentional behavior to make the code simple.
391 bool ReadLine(const char **bol, const char **eol) {
392 if (BufferIsEmpty()) { // First time.
393 const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_);
394 if (num_bytes <= 0) { // EOF or error.
397 eod_ = buf_ + num_bytes;
400 bol_ = eol_ + 1; // Advance to the next line in the buffer.
401 SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
402 if (!HasCompleteLine()) {
403 const int incomplete_line_length = eod_ - bol_;
404 // Move the trailing incomplete line to the beginning.
405 memmove(buf_, bol_, incomplete_line_length);
406 // Read text from file and append it.
407 char * const append_pos = buf_ + incomplete_line_length;
408 const int capacity_left = buf_len_ - incomplete_line_length;
409 const ssize_t num_bytes = ReadPersistent(fd_, append_pos,
411 if (num_bytes <= 0) { // EOF or error.
414 eod_ = append_pos + num_bytes;
418 eol_ = FindLineFeed();
419 if (eol_ == NULL) { // '\n' not found. Malformed line.
422 *eol_ = '\0'; // Replace '\n' with '\0'.
429 // Beginning of line.
440 explicit LineReader(const LineReader&);
441 void operator=(const LineReader&);
443 char *FindLineFeed() {
444 return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_));
447 bool BufferIsEmpty() {
451 bool HasCompleteLine() {
452 return !BufferIsEmpty() && FindLineFeed() != NULL;
460 const char *eod_; // End of data in "buf_".
464 // Place the hex number read from "start" into "*hex". The pointer to
465 // the first non-hex character or "end" is returned.
466 static char *GetHex(const char *start, const char *end, uint64_t *hex) {
469 for (p = start; p < end; ++p) {
471 if ((ch >= '0' && ch <= '9') ||
472 (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) {
473 *hex = (*hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
474 } else { // Encountered the first non-hex character.
478 SAFE_ASSERT(p <= end);
479 return const_cast<char *>(p);
482 // Search for the object file (from /proc/self/maps) that contains
483 // the specified pc. If found, open this file and return the file handle,
484 // and also set start_address to the start address of where this object
485 // file is mapped to in memory. Otherwise, return -1.
486 static ATTRIBUTE_NOINLINE int
487 OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc,
488 uint64_t &start_address) {
491 // Open /proc/self/maps.
493 NO_INTR(maps_fd = open("/proc/self/maps", O_RDONLY));
494 FileDescriptor wrapped_maps_fd(maps_fd);
495 if (wrapped_maps_fd.get() < 0) {
499 // Iterate over maps and look for the map containing the pc. Then
500 // look into the symbol tables inside.
501 char buf[1024]; // Big enough for line of sane /proc/self/maps
502 LineReader reader(wrapped_maps_fd.get(), buf, sizeof(buf));
506 if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line.
510 // Start parsing line in /proc/self/maps. Here is an example:
512 // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat
514 // We want start address (08048000), end address (0804c000), flags
515 // (r-xp) and file name (/bin/cat).
517 // Read start address.
518 cursor = GetHex(cursor, eol, &start_address);
519 if (cursor == eol || *cursor != '-') {
520 return -1; // Malformed line.
522 ++cursor; // Skip '-'.
525 uint64_t end_address;
526 cursor = GetHex(cursor, eol, &end_address);
527 if (cursor == eol || *cursor != ' ') {
528 return -1; // Malformed line.
530 ++cursor; // Skip ' '.
532 // Check start and end addresses.
533 if (!(start_address <= pc && pc < end_address)) {
534 continue; // We skip this map. PC isn't in this map.
537 // Read flags. Skip flags until we encounter a space or eol.
538 const char * const flags_start = cursor;
539 while (cursor < eol && *cursor != ' ') {
542 // We expect at least four letters for flags (ex. "r-xp").
543 if (cursor == eol || cursor < flags_start + 4) {
544 return -1; // Malformed line.
547 // Check flags. We are only interested in "r-x" maps.
548 if (memcmp(flags_start, "r-x", 3) != 0) { // Not a "r-x" map.
549 continue; // We skip this map.
551 ++cursor; // Skip ' '.
553 // Skip to file name. "cursor" now points to file offset. We need to
554 // skip at least three spaces for file offset, dev, and inode.
556 while (cursor < eol) {
557 if (*cursor == ' ') {
559 } else if (num_spaces >= 3) {
560 // The first non-space character after skipping three spaces
561 // is the beginning of the file name.
567 return -1; // Malformed line.
570 // Finally, "cursor" now points to file name of our interest.
571 NO_INTR(object_fd = open(cursor, O_RDONLY));
579 // The implementation of our symbolization routine. If it
580 // successfully finds the symbol containing "pc" and obtains the
581 // symbol name, returns true and write the symbol name to "out".
582 // Otherwise, returns false. If Callback function is installed via
583 // InstallSymbolizeCallback(), the function is also called in this function,
584 // and "out" is used as its output.
585 // To keep stack consumption low, we would like this function to not
587 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
589 uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
590 uint64_t start_address = 0;
592 int object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0,
594 if (object_fd == -1) {
597 FileDescriptor wrapped_object_fd(object_fd);
598 int elf_type = FileGetElfType(wrapped_object_fd.get());
599 if (elf_type == -1) {
602 if (g_symbolize_callback) {
603 // Run the call back if it's installed.
604 // Note: relocation (and much of the rest of this code) will be
605 // wrong for prelinked shared libraries and PIE executables.
606 uint64 relocation = (elf_type == ET_DYN) ? start_address : 0;
607 int num_bytes_written = g_symbolize_callback(wrapped_object_fd.get(),
610 if (num_bytes_written > 0) {
611 out += num_bytes_written;
612 out_size -= num_bytes_written;
615 if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
616 out, out_size, start_address)) {
620 // Symbolization succeeded. Now we try to demangle the symbol.
621 DemangleInplace(out, out_size);
625 _END_GOOGLE_NAMESPACE_
627 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
632 _START_GOOGLE_NAMESPACE_
634 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
637 if (dladdr(pc, &info)) {
638 if (strlen(info.dli_sname) < out_size) {
639 strcpy(out, info.dli_sname);
640 // Symbolization succeeded. Now we try to demangle the symbol.
641 DemangleInplace(out, out_size);
648 _END_GOOGLE_NAMESPACE_
651 # error BUG: HAVE_SYMBOLIZE was wrongly set
654 _START_GOOGLE_NAMESPACE_
656 bool Symbolize(void *pc, char *out, int out_size) {
657 SAFE_ASSERT(out_size >= 0);
658 return SymbolizeAndDemangle(pc, out, out_size);
661 _END_GOOGLE_NAMESPACE_
663 #else /* HAVE_SYMBOLIZE */
669 _START_GOOGLE_NAMESPACE_
671 // TODO: Support other environments.
672 bool Symbolize(void *pc, char *out, int out_size) {
677 _END_GOOGLE_NAMESPACE_