Initialize gflags in signalhandler_unittest.
[platform/upstream/glog.git] / src / symbolize.cc
1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
3 // Stack-footprint reduction work done by Raksit Ashok
4 //
5 // Implementation note:
6 //
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.
9 //
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
15 //
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.
19 //
20
21 #include "utilities.h"
22
23 #if defined(HAVE_SYMBOLIZE)
24
25 #include "symbolize.h"
26 #include "demangle.h"
27
28 _START_GOOGLE_NAMESPACE_
29
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.
33
34 // A wrapper for abort() to make it callable in ? :.
35 static int AssertFail() {
36   abort();
37   return 0;  // Should not reach.
38 }
39
40 #define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail())
41
42 static SymbolizeCallback g_symbolize_callback = NULL;
43 void InstallSymbolizeCallback(SymbolizeCallback callback) {
44   g_symbolize_callback = callback;
45 }
46
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
50 // get inlined.
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);
59     }
60   }
61 }
62
63 _END_GOOGLE_NAMESPACE_
64
65 #if defined(__ELF__)
66
67 #include <dlfcn.h>
68 #include <elf.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <limits.h>
72 #include <link.h>  // For ElfW() macro.
73 #include <stdint.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <stddef.h>
77 #include <string.h>
78 #include <sys/stat.h>
79 #include <sys/types.h>
80 #include <unistd.h>
81
82 #include "symbolize.h"
83 #include "config.h"
84 #include "glog/raw_logging.h"
85
86 // Re-runs fn until it doesn't cause EINTR.
87 #define NO_INTR(fn)   do {} while ((fn) < 0 && errno == EINTR)
88
89 _START_GOOGLE_NAMESPACE_
90
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) {
95   SAFE_ASSERT(fd >= 0);
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) {
100     ssize_t len;
101     NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes));
102     if (len < 0) {  // There was an error other than EINTR.
103       return -1;
104     }
105     if (len == 0) {  // Reached EOF.
106       break;
107     }
108     num_bytes += len;
109   }
110   SAFE_ASSERT(num_bytes <= count);
111   return num_bytes;
112 }
113
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) {
121     return -1;
122   }
123   return ReadPersistent(fd, buf, count);
124 }
125
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
129 // false.
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);
133   return len == count;
134 }
135
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)) {
140     return -1;
141   }
142   if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) {
143     return -1;
144   }
145   return elf_header.e_type;
146 }
147
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
152 // inlined.
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.
157   ElfW(Shdr) buf[16];
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) {
169         *out = buf[j];
170         return true;
171       }
172     }
173     i += num_headers_in_buf;
174   }
175   return false;
176 }
177
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;
181
182 // name_len should include terminating '\0'.
183 bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
184                             ElfW(Shdr) *out) {
185   ElfW(Ehdr) elf_header;
186   if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) {
187     return false;
188   }
189
190   ElfW(Shdr) shstrtab;
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)) {
194     return false;
195   }
196
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)) {
201       return false;
202     }
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.
208       return false;
209     }
210     off_t name_offset = shstrtab.sh_offset + out->sh_name;
211     ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
212     if (n_read == -1) {
213       return false;
214     } else if (n_read != name_len) {
215       // Short read -- name could be at end of file.
216       continue;
217     }
218     if (memcmp(header_name, name, name_len) == 0) {
219       return true;
220     }
221   }
222   return false;
223 }
224
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
230 // inlined.
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) {
236     return false;
237   }
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;
241
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.
245 #if __WORDSIZE == 64
246 #define NUM_SYMBOLS 32
247 #else
248 #define NUM_SYMBOLS 64
249 #endif
250
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) {
268           return false;
269         }
270         return true;  // Obtained the symbol name.
271       }
272     }
273     i += num_symbols_in_buf;
274   }
275   return false;
276 }
277
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
281 // false.
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)) {
288     return false;
289   }
290
291   uint64_t symbol_offset = 0;
292   if (elf_header.e_type == ET_DYN) {  // DSO needs offset adjustment.
293     symbol_offset = map_start_address;
294   }
295
296   ElfW(Shdr) symtab, strtab;
297
298   // Consult a regular symbol table first.
299   if (!GetSectionHeaderByType(fd, elf_header.e_shnum, elf_header.e_shoff,
300                               SHT_SYMTAB, &symtab)) {
301     return false;
302   }
303   if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
304                            symtab.sh_link * sizeof(symtab))) {
305     return false;
306   }
307   if (FindSymbol(pc, fd, out, out_size, symbol_offset,
308                  &strtab, &symtab)) {
309     return true;  // Found the symbol in a regular symbol table.
310   }
311
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)) {
315     return false;
316   }
317   if (!ReadFromOffsetExact(fd, &strtab, sizeof(strtab), elf_header.e_shoff +
318                            symtab.sh_link * sizeof(symtab))) {
319     return false;
320   }
321   if (FindSymbol(pc, fd, out, out_size, symbol_offset,
322                  &strtab, &symtab)) {
323     return true;  // Found the symbol in a dynamic symbol table.
324   }
325
326   return false;
327 }
328
329 namespace {
330 // Thin wrapper around a file descriptor so that the file descriptor
331 // gets closed for sure.
332 struct FileDescriptor {
333   const int fd_;
334   explicit FileDescriptor(int fd) : fd_(fd) {}
335   ~FileDescriptor() {
336     if (fd_ >= 0) {
337       NO_INTR(close(fd_));
338     }
339   }
340   int get() { return fd_; }
341
342  private:
343   explicit FileDescriptor(const FileDescriptor&);
344   void operator=(const FileDescriptor&);
345 };
346
347 // Helper class for reading lines from file.
348 //
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()
351 // and snprintf().
352 class LineReader {
353  public:
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) {
356   }
357
358   // Read '\n'-terminated line from file.  On success, modify "bol"
359   // and "eol", then return true.  Otherwise, return false.
360   //
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.
367         return false;
368       }
369       eod_ = buf_ + num_bytes;
370       bol_ = buf_;
371     } else {
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,
382                                                  capacity_left);
383         if (num_bytes <= 0) {  // EOF or error.
384           return false;
385         }
386         eod_ = append_pos + num_bytes;
387         bol_ = buf_;
388       }
389     }
390     eol_ = FindLineFeed();
391     if (eol_ == NULL) {  // '\n' not found.  Malformed line.
392       return false;
393     }
394     *eol_ = '\0';  // Replace '\n' with '\0'.
395
396     *bol = bol_;
397     *eol = eol_;
398     return true;
399   }
400
401   // Beginning of line.
402   const char *bol() {
403     return bol_;
404   }
405
406   // End of line.
407   const char *eol() {
408     return eol_;
409   }
410
411  private:
412   explicit LineReader(const LineReader&);
413   void operator=(const LineReader&);
414
415   char *FindLineFeed() {
416     return reinterpret_cast<char *>
417       (memchr(reinterpret_cast<const void *>(bol_), '\n', eod_ - bol_));
418   }
419
420   bool BufferIsEmpty() {
421     return buf_ == eod_;
422   }
423
424   bool HasCompleteLine() {
425     return !BufferIsEmpty() && FindLineFeed() != NULL;
426   }
427
428   const int fd_;
429   char * const buf_;
430   const int buf_len_;
431   const char *bol_;
432   char *eol_;
433   const char *eod_;  // End of data in "buf_".
434 };
435 }  // namespace
436
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) {
440   *hex = 0;
441   const char *p;
442   for (p = start; p < end; ++p) {
443     int ch = *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.
448       break;
449     }
450   }
451   SAFE_ASSERT(p <= end);
452   return const_cast<char *>(p);
453 }
454
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) {
462   int object_fd;
463
464   // Open /proc/self/maps.
465   int maps_fd;
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) {
469     return -1;
470   }
471
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));
476   while (true) {
477     const char *cursor;
478     const char *eol;
479     if (!reader.ReadLine(&cursor, &eol)) {  // EOF or malformed line.
480       return -1;
481     }
482
483     // Start parsing line in /proc/self/maps.  Here is an example:
484     //
485     // 08048000-0804c000 r-xp 00000000 08:01 2142121    /bin/cat
486     //
487     // We want start address (08048000), end address (0804c000), flags
488     // (r-xp) and file name (/bin/cat).
489
490     // Read start address.
491     cursor = GetHex(cursor, eol, &start_address);
492     if (cursor == eol || *cursor != '-') {
493       return -1;  // Malformed line.
494     }
495     ++cursor;  // Skip '-'.
496
497     // Read end address.
498     uint64_t end_address;
499     cursor = GetHex(cursor, eol, &end_address);
500     if (cursor == eol || *cursor != ' ') {
501       return -1;  // Malformed line.
502     }
503     ++cursor;  // Skip ' '.
504
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.
508     }
509
510     // Read flags.  Skip flags until we encounter a space or eol.
511     const char * const flags_start = cursor;
512     while (cursor < eol && *cursor != ' ') {
513       ++cursor;
514     }
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.
518     }
519
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.
523     }
524     ++cursor;  // Skip ' '.
525
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.
528     int num_spaces = 0;
529     while (cursor < eol) {
530       if (*cursor == ' ') {
531         ++num_spaces;
532       } else if (num_spaces >= 3) {
533         // The first non-space character after  skipping three spaces
534         // is the beginning of the file name.
535         break;
536       }
537       ++cursor;
538     }
539     if (cursor == eol) {
540       return -1;  // Malformed line.
541     }
542
543     // Finally, "cursor" now points to file name of our interest.
544     NO_INTR(object_fd = open(cursor, O_RDONLY));
545     if (object_fd < 0) {
546       return -1;
547     }
548     return object_fd;
549   }
550 }
551
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
559 // get inlined.
560 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
561                                                     int out_size) {
562   uint64_t pc0 = reinterpret_cast<uintptr_t>(pc);
563   uint64_t start_address = 0;
564
565   int object_fd = OpenObjectFileContainingPcAndGetStartAddress(pc0,
566                                                                start_address);
567   if (object_fd == -1) {
568     return false;
569   }
570   FileDescriptor wrapped_object_fd(object_fd);
571   int elf_type = FileGetElfType(wrapped_object_fd.get());
572   if (elf_type == -1) {
573     return false;
574   }
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(),
581                                                  pc, out, out_size,
582                                                  relocation);
583     if (num_bytes_written > 0) {
584       out += num_bytes_written;
585       out_size -= num_bytes_written;
586     }
587   }
588   if (!GetSymbolFromObjectFile(wrapped_object_fd.get(), pc0,
589                                out, out_size, start_address)) {
590     return false;
591   }
592
593   // Symbolization succeeded.  Now we try to demangle the symbol.
594   DemangleInplace(out, out_size);
595   return true;
596 }
597
598 _END_GOOGLE_NAMESPACE_
599
600 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
601
602 #include <dlfcn.h>
603 #include <string.h>
604
605 _START_GOOGLE_NAMESPACE_
606
607 static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
608                                                     int out_size) {
609   Dl_info info;
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);
615       return true;
616     }
617   }
618   return false;
619 }
620
621 _END_GOOGLE_NAMESPACE_
622
623 #else
624 # error BUG: HAVE_SYMBOLIZE was wrongly set
625 #endif
626
627 _START_GOOGLE_NAMESPACE_
628
629 bool Symbolize(void *pc, char *out, int out_size) {
630   SAFE_ASSERT(out_size >= 0);
631   return SymbolizeAndDemangle(pc, out, out_size);
632 }
633
634 _END_GOOGLE_NAMESPACE_
635
636 #else  /* HAVE_SYMBOLIZE */
637
638 #include <assert.h>
639
640 #include "config.h"
641
642 _START_GOOGLE_NAMESPACE_
643
644 // TODO: Support other environments.
645 bool Symbolize(void *pc, char *out, int out_size) {
646   assert(0);
647   return false;
648 }
649
650 _END_GOOGLE_NAMESPACE_
651
652 #endif