1 | // Copyright 2018 The Abseil Authors. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | // This library provides Symbolize() function that symbolizes program |
16 | // counters to their corresponding symbol names on linux platforms. |
17 | // This library has a minimal implementation of an ELF symbol table |
18 | // reader (i.e. it doesn't depend on libelf, etc.). |
19 | // |
20 | // The algorithm used in Symbolize() is as follows. |
21 | // |
22 | // 1. Go through a list of maps in /proc/self/maps and find the map |
23 | // containing the program counter. |
24 | // |
25 | // 2. Open the mapped file and find a regular symbol table inside. |
26 | // Iterate over symbols in the symbol table and look for the symbol |
27 | // containing the program counter. If such a symbol is found, |
28 | // obtain the symbol name, and demangle the symbol if possible. |
29 | // If the symbol isn't found in the regular symbol table (binary is |
30 | // stripped), try the same thing with a dynamic symbol table. |
31 | // |
32 | // Note that Symbolize() is originally implemented to be used in |
33 | // signal handlers, hence it doesn't use malloc() and other unsafe |
34 | // operations. It should be both thread-safe and async-signal-safe. |
35 | // |
36 | // Implementation note: |
37 | // |
38 | // We don't use heaps but only use stacks. We want to reduce the |
39 | // stack consumption so that the symbolizer can run on small stacks. |
40 | // |
41 | // Here are some numbers collected with GCC 4.1.0 on x86: |
42 | // - sizeof(Elf32_Sym) = 16 |
43 | // - sizeof(Elf32_Shdr) = 40 |
44 | // - sizeof(Elf64_Sym) = 24 |
45 | // - sizeof(Elf64_Shdr) = 64 |
46 | // |
47 | // This implementation is intended to be async-signal-safe but uses some |
48 | // functions which are not guaranteed to be so, such as memchr() and |
49 | // memmove(). We assume they are async-signal-safe. |
50 | |
51 | #include <dlfcn.h> |
52 | #include <elf.h> |
53 | #include <fcntl.h> |
54 | #include <link.h> // For ElfW() macro. |
55 | #include <sys/stat.h> |
56 | #include <sys/types.h> |
57 | #include <unistd.h> |
58 | |
59 | #include <algorithm> |
60 | #include <atomic> |
61 | #include <cerrno> |
62 | #include <cinttypes> |
63 | #include <climits> |
64 | #include <cstdint> |
65 | #include <cstdio> |
66 | #include <cstdlib> |
67 | #include <cstring> |
68 | |
69 | #include "absl/base/casts.h" |
70 | #include "absl/base/dynamic_annotations.h" |
71 | #include "absl/base/internal/low_level_alloc.h" |
72 | #include "absl/base/internal/raw_logging.h" |
73 | #include "absl/base/internal/spinlock.h" |
74 | #include "absl/base/port.h" |
75 | #include "absl/debugging/internal/demangle.h" |
76 | #include "absl/debugging/internal/vdso_support.h" |
77 | |
78 | namespace absl { |
79 | |
80 | // Value of argv[0]. Used by MaybeInitializeObjFile(). |
81 | static char *argv0_value = nullptr; |
82 | |
83 | void InitializeSymbolizer(const char *argv0) { |
84 | if (argv0_value != nullptr) { |
85 | free(argv0_value); |
86 | argv0_value = nullptr; |
87 | } |
88 | if (argv0 != nullptr && argv0[0] != '\0') { |
89 | argv0_value = strdup(argv0); |
90 | } |
91 | } |
92 | |
93 | namespace debugging_internal { |
94 | namespace { |
95 | |
96 | // Re-runs fn until it doesn't cause EINTR. |
97 | #define NO_INTR(fn) \ |
98 | do { \ |
99 | } while ((fn) < 0 && errno == EINTR) |
100 | |
101 | // On Linux, ELF_ST_* are defined in <linux/elf.h>. To make this portable |
102 | // we define our own ELF_ST_BIND and ELF_ST_TYPE if not available. |
103 | #ifndef ELF_ST_BIND |
104 | #define ELF_ST_BIND(info) (((unsigned char)(info)) >> 4) |
105 | #endif |
106 | |
107 | #ifndef ELF_ST_TYPE |
108 | #define ELF_ST_TYPE(info) (((unsigned char)(info)) & 0xF) |
109 | #endif |
110 | |
111 | // Some platforms use a special .opd section to store function pointers. |
112 | const char kOpdSectionName[] = ".opd" ; |
113 | |
114 | #if (defined(__powerpc__) && !(_CALL_ELF > 1)) || defined(__ia64) |
115 | // Use opd section for function descriptors on these platforms, the function |
116 | // address is the first word of the descriptor. |
117 | enum { kPlatformUsesOPDSections = 1 }; |
118 | #else // not PPC or IA64 |
119 | enum { kPlatformUsesOPDSections = 0 }; |
120 | #endif |
121 | |
122 | // This works for PowerPC & IA64 only. A function descriptor consist of two |
123 | // pointers and the first one is the function's entry. |
124 | const size_t kFunctionDescriptorSize = sizeof(void *) * 2; |
125 | |
126 | const int kMaxDecorators = 10; // Seems like a reasonable upper limit. |
127 | |
128 | struct InstalledSymbolDecorator { |
129 | SymbolDecorator fn; |
130 | void *arg; |
131 | int ticket; |
132 | }; |
133 | |
134 | int g_num_decorators; |
135 | InstalledSymbolDecorator g_decorators[kMaxDecorators]; |
136 | |
137 | struct FileMappingHint { |
138 | const void *start; |
139 | const void *end; |
140 | uint64_t offset; |
141 | const char *filename; |
142 | }; |
143 | |
144 | // Protects g_decorators. |
145 | // We are using SpinLock and not a Mutex here, because we may be called |
146 | // from inside Mutex::Lock itself, and it prohibits recursive calls. |
147 | // This happens in e.g. base/stacktrace_syscall_unittest. |
148 | // Moreover, we are using only TryLock(), if the decorator list |
149 | // is being modified (is busy), we skip all decorators, and possibly |
150 | // loose some info. Sorry, that's the best we could do. |
151 | base_internal::SpinLock g_decorators_mu(base_internal::kLinkerInitialized); |
152 | |
153 | const int kMaxFileMappingHints = 8; |
154 | int g_num_file_mapping_hints; |
155 | FileMappingHint g_file_mapping_hints[kMaxFileMappingHints]; |
156 | // Protects g_file_mapping_hints. |
157 | base_internal::SpinLock g_file_mapping_mu(base_internal::kLinkerInitialized); |
158 | |
159 | // Async-signal-safe function to zero a buffer. |
160 | // memset() is not guaranteed to be async-signal-safe. |
161 | static void SafeMemZero(void* p, size_t size) { |
162 | unsigned char *c = static_cast<unsigned char *>(p); |
163 | while (size--) { |
164 | *c++ = 0; |
165 | } |
166 | } |
167 | |
168 | struct ObjFile { |
169 | ObjFile() |
170 | : filename(nullptr), |
171 | start_addr(nullptr), |
172 | end_addr(nullptr), |
173 | offset(0), |
174 | fd(-1), |
175 | elf_type(-1) { |
176 | SafeMemZero(&elf_header, sizeof(elf_header)); |
177 | } |
178 | |
179 | char *filename; |
180 | const void *start_addr; |
181 | const void *end_addr; |
182 | uint64_t offset; |
183 | |
184 | // The following fields are initialized on the first access to the |
185 | // object file. |
186 | int fd; |
187 | int elf_type; |
188 | ElfW(Ehdr) ; |
189 | }; |
190 | |
191 | // Build 4-way associative cache for symbols. Within each cache line, symbols |
192 | // are replaced in LRU order. |
193 | enum { |
194 | ASSOCIATIVITY = 4, |
195 | }; |
196 | struct SymbolCacheLine { |
197 | const void *pc[ASSOCIATIVITY]; |
198 | char *name[ASSOCIATIVITY]; |
199 | |
200 | // age[i] is incremented when a line is accessed. it's reset to zero if the |
201 | // i'th entry is read. |
202 | uint32_t age[ASSOCIATIVITY]; |
203 | }; |
204 | |
205 | // --------------------------------------------------------------- |
206 | // An async-signal-safe arena for LowLevelAlloc |
207 | static std::atomic<base_internal::LowLevelAlloc::Arena *> g_sig_safe_arena; |
208 | |
209 | static base_internal::LowLevelAlloc::Arena *SigSafeArena() { |
210 | return g_sig_safe_arena.load(std::memory_order_acquire); |
211 | } |
212 | |
213 | static void InitSigSafeArena() { |
214 | if (SigSafeArena() == nullptr) { |
215 | base_internal::LowLevelAlloc::Arena *new_arena = |
216 | base_internal::LowLevelAlloc::NewArena( |
217 | base_internal::LowLevelAlloc::kAsyncSignalSafe); |
218 | base_internal::LowLevelAlloc::Arena *old_value = nullptr; |
219 | if (!g_sig_safe_arena.compare_exchange_strong(old_value, new_arena, |
220 | std::memory_order_release, |
221 | std::memory_order_relaxed)) { |
222 | // We lost a race to allocate an arena; deallocate. |
223 | base_internal::LowLevelAlloc::DeleteArena(new_arena); |
224 | } |
225 | } |
226 | } |
227 | |
228 | // --------------------------------------------------------------- |
229 | // An AddrMap is a vector of ObjFile, using SigSafeArena() for allocation. |
230 | |
231 | class AddrMap { |
232 | public: |
233 | AddrMap() : size_(0), allocated_(0), obj_(nullptr) {} |
234 | ~AddrMap() { base_internal::LowLevelAlloc::Free(obj_); } |
235 | int Size() const { return size_; } |
236 | ObjFile *At(int i) { return &obj_[i]; } |
237 | ObjFile *Add(); |
238 | void Clear(); |
239 | |
240 | private: |
241 | int size_; // count of valid elements (<= allocated_) |
242 | int allocated_; // count of allocated elements |
243 | ObjFile *obj_; // array of allocated_ elements |
244 | AddrMap(const AddrMap &) = delete; |
245 | AddrMap &operator=(const AddrMap &) = delete; |
246 | }; |
247 | |
248 | void AddrMap::Clear() { |
249 | for (int i = 0; i != size_; i++) { |
250 | At(i)->~ObjFile(); |
251 | } |
252 | size_ = 0; |
253 | } |
254 | |
255 | ObjFile *AddrMap::Add() { |
256 | if (size_ == allocated_) { |
257 | int new_allocated = allocated_ * 2 + 50; |
258 | ObjFile *new_obj_ = |
259 | static_cast<ObjFile *>(base_internal::LowLevelAlloc::AllocWithArena( |
260 | new_allocated * sizeof(*new_obj_), SigSafeArena())); |
261 | if (obj_) { |
262 | memcpy(new_obj_, obj_, allocated_ * sizeof(*new_obj_)); |
263 | base_internal::LowLevelAlloc::Free(obj_); |
264 | } |
265 | obj_ = new_obj_; |
266 | allocated_ = new_allocated; |
267 | } |
268 | return new (&obj_[size_++]) ObjFile; |
269 | } |
270 | |
271 | // --------------------------------------------------------------- |
272 | |
273 | enum FindSymbolResult { SYMBOL_NOT_FOUND = 1, SYMBOL_TRUNCATED, SYMBOL_FOUND }; |
274 | |
275 | class Symbolizer { |
276 | public: |
277 | Symbolizer(); |
278 | ~Symbolizer(); |
279 | const char *GetSymbol(const void *const pc); |
280 | |
281 | private: |
282 | char *CopyString(const char *s) { |
283 | int len = strlen(s); |
284 | char *dst = static_cast<char *>( |
285 | base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); |
286 | ABSL_RAW_CHECK(dst != nullptr, "out of memory" ); |
287 | memcpy(dst, s, len + 1); |
288 | return dst; |
289 | } |
290 | ObjFile *FindObjFile(const void *const start, |
291 | size_t size) ABSL_ATTRIBUTE_NOINLINE; |
292 | static bool RegisterObjFile(const char *filename, |
293 | const void *const start_addr, |
294 | const void *const end_addr, uint64_t offset, |
295 | void *arg); |
296 | SymbolCacheLine *GetCacheLine(const void *const pc); |
297 | const char *FindSymbolInCache(const void *const pc); |
298 | const char *InsertSymbolInCache(const void *const pc, const char *name); |
299 | void AgeSymbols(SymbolCacheLine *line); |
300 | void ClearAddrMap(); |
301 | FindSymbolResult GetSymbolFromObjectFile(const ObjFile &obj, |
302 | const void *const pc, |
303 | const ptrdiff_t relocation, |
304 | char *out, int out_size, |
305 | char *tmp_buf, int tmp_buf_size); |
306 | |
307 | enum { |
308 | SYMBOL_BUF_SIZE = 3072, |
309 | TMP_BUF_SIZE = 1024, |
310 | SYMBOL_CACHE_LINES = 128, |
311 | }; |
312 | |
313 | AddrMap addr_map_; |
314 | |
315 | bool ok_; |
316 | bool addr_map_read_; |
317 | |
318 | char symbol_buf_[SYMBOL_BUF_SIZE]; |
319 | |
320 | // tmp_buf_ will be used to store arrays of ElfW(Shdr) and ElfW(Sym) |
321 | // so we ensure that tmp_buf_ is properly aligned to store either. |
322 | alignas(16) char tmp_buf_[TMP_BUF_SIZE]; |
323 | static_assert(alignof(ElfW(Shdr)) <= 16, |
324 | "alignment of tmp buf too small for Shdr" ); |
325 | static_assert(alignof(ElfW(Sym)) <= 16, |
326 | "alignment of tmp buf too small for Sym" ); |
327 | |
328 | SymbolCacheLine symbol_cache_[SYMBOL_CACHE_LINES]; |
329 | }; |
330 | |
331 | static std::atomic<Symbolizer *> g_cached_symbolizer; |
332 | |
333 | } // namespace |
334 | |
335 | static int SymbolizerSize() { |
336 | #if defined(__wasm__) || defined(__asmjs__) |
337 | int pagesize = getpagesize(); |
338 | #else |
339 | int pagesize = sysconf(_SC_PAGESIZE); |
340 | #endif |
341 | return ((sizeof(Symbolizer) - 1) / pagesize + 1) * pagesize; |
342 | } |
343 | |
344 | // Return (and set null) g_cached_symbolized_state if it is not null. |
345 | // Otherwise return a new symbolizer. |
346 | static Symbolizer *AllocateSymbolizer() { |
347 | InitSigSafeArena(); |
348 | Symbolizer *symbolizer = |
349 | g_cached_symbolizer.exchange(nullptr, std::memory_order_acquire); |
350 | if (symbolizer != nullptr) { |
351 | return symbolizer; |
352 | } |
353 | return new (base_internal::LowLevelAlloc::AllocWithArena( |
354 | SymbolizerSize(), SigSafeArena())) Symbolizer(); |
355 | } |
356 | |
357 | // Set g_cached_symbolize_state to s if it is null, otherwise |
358 | // delete s. |
359 | static void FreeSymbolizer(Symbolizer *s) { |
360 | Symbolizer *old_cached_symbolizer = nullptr; |
361 | if (!g_cached_symbolizer.compare_exchange_strong(old_cached_symbolizer, s, |
362 | std::memory_order_release, |
363 | std::memory_order_relaxed)) { |
364 | s->~Symbolizer(); |
365 | base_internal::LowLevelAlloc::Free(s); |
366 | } |
367 | } |
368 | |
369 | Symbolizer::Symbolizer() : ok_(true), addr_map_read_(false) { |
370 | for (SymbolCacheLine &symbol_cache_line : symbol_cache_) { |
371 | for (size_t j = 0; j < ABSL_ARRAYSIZE(symbol_cache_line.name); ++j) { |
372 | symbol_cache_line.pc[j] = nullptr; |
373 | symbol_cache_line.name[j] = nullptr; |
374 | symbol_cache_line.age[j] = 0; |
375 | } |
376 | } |
377 | } |
378 | |
379 | Symbolizer::~Symbolizer() { |
380 | for (SymbolCacheLine &symbol_cache_line : symbol_cache_) { |
381 | for (char *s : symbol_cache_line.name) { |
382 | base_internal::LowLevelAlloc::Free(s); |
383 | } |
384 | } |
385 | ClearAddrMap(); |
386 | } |
387 | |
388 | // We don't use assert() since it's not guaranteed to be |
389 | // async-signal-safe. Instead we define a minimal assertion |
390 | // macro. So far, we don't need pretty printing for __FILE__, etc. |
391 | #define SAFE_ASSERT(expr) ((expr) ? static_cast<void>(0) : abort()) |
392 | |
393 | // Read up to "count" bytes from file descriptor "fd" into the buffer |
394 | // starting at "buf" while handling short reads and EINTR. On |
395 | // success, return the number of bytes read. Otherwise, return -1. |
396 | static ssize_t ReadPersistent(int fd, void *buf, size_t count) { |
397 | SAFE_ASSERT(fd >= 0); |
398 | SAFE_ASSERT(count <= SSIZE_MAX); |
399 | char *buf0 = reinterpret_cast<char *>(buf); |
400 | size_t num_bytes = 0; |
401 | while (num_bytes < count) { |
402 | ssize_t len; |
403 | NO_INTR(len = read(fd, buf0 + num_bytes, count - num_bytes)); |
404 | if (len < 0) { // There was an error other than EINTR. |
405 | ABSL_RAW_LOG(WARNING, "read failed: errno=%d" , errno); |
406 | return -1; |
407 | } |
408 | if (len == 0) { // Reached EOF. |
409 | break; |
410 | } |
411 | num_bytes += len; |
412 | } |
413 | SAFE_ASSERT(num_bytes <= count); |
414 | return static_cast<ssize_t>(num_bytes); |
415 | } |
416 | |
417 | // Read up to "count" bytes from "offset" in the file pointed by file |
418 | // descriptor "fd" into the buffer starting at "buf". On success, |
419 | // return the number of bytes read. Otherwise, return -1. |
420 | static ssize_t ReadFromOffset(const int fd, void *buf, const size_t count, |
421 | const off_t offset) { |
422 | off_t off = lseek(fd, offset, SEEK_SET); |
423 | if (off == (off_t)-1) { |
424 | ABSL_RAW_LOG(WARNING, "lseek(%d, %ju, SEEK_SET) failed: errno=%d" , fd, |
425 | static_cast<uintmax_t>(offset), errno); |
426 | return -1; |
427 | } |
428 | return ReadPersistent(fd, buf, count); |
429 | } |
430 | |
431 | // Try reading exactly "count" bytes from "offset" bytes in a file |
432 | // pointed by "fd" into the buffer starting at "buf" while handling |
433 | // short reads and EINTR. On success, return true. Otherwise, return |
434 | // false. |
435 | static bool ReadFromOffsetExact(const int fd, void *buf, const size_t count, |
436 | const off_t offset) { |
437 | ssize_t len = ReadFromOffset(fd, buf, count, offset); |
438 | return len >= 0 && static_cast<size_t>(len) == count; |
439 | } |
440 | |
441 | // Returns elf_header.e_type if the file pointed by fd is an ELF binary. |
442 | static int FileGetElfType(const int fd) { |
443 | ElfW(Ehdr) ; |
444 | if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) { |
445 | return -1; |
446 | } |
447 | if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0) { |
448 | return -1; |
449 | } |
450 | return elf_header.e_type; |
451 | } |
452 | |
453 | // Read the section headers in the given ELF binary, and if a section |
454 | // of the specified type is found, set the output to this section header |
455 | // and return true. Otherwise, return false. |
456 | // To keep stack consumption low, we would like this function to not get |
457 | // inlined. |
458 | static ABSL_ATTRIBUTE_NOINLINE bool ( |
459 | const int fd, ElfW(Half) sh_num, const off_t sh_offset, ElfW(Word) type, |
460 | ElfW(Shdr) * out, char *tmp_buf, int tmp_buf_size) { |
461 | ElfW(Shdr) *buf = reinterpret_cast<ElfW(Shdr) *>(tmp_buf); |
462 | const int buf_entries = tmp_buf_size / sizeof(buf[0]); |
463 | const int buf_bytes = buf_entries * sizeof(buf[0]); |
464 | |
465 | for (int i = 0; i < sh_num;) { |
466 | const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]); |
467 | const ssize_t num_bytes_to_read = |
468 | (buf_bytes > num_bytes_left) ? num_bytes_left : buf_bytes; |
469 | const off_t offset = sh_offset + i * sizeof(buf[0]); |
470 | const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read, offset); |
471 | if (len % sizeof(buf[0]) != 0) { |
472 | ABSL_RAW_LOG( |
473 | WARNING, |
474 | "Reading %zd bytes from offset %ju returned %zd which is not a " |
475 | "multiple of %zu." , |
476 | num_bytes_to_read, static_cast<uintmax_t>(offset), len, |
477 | sizeof(buf[0])); |
478 | return false; |
479 | } |
480 | const ssize_t = len / sizeof(buf[0]); |
481 | SAFE_ASSERT(num_headers_in_buf <= buf_entries); |
482 | for (int j = 0; j < num_headers_in_buf; ++j) { |
483 | if (buf[j].sh_type == type) { |
484 | *out = buf[j]; |
485 | return true; |
486 | } |
487 | } |
488 | i += num_headers_in_buf; |
489 | } |
490 | return false; |
491 | } |
492 | |
493 | // There is no particular reason to limit section name to 63 characters, |
494 | // but there has (as yet) been no need for anything longer either. |
495 | const int kMaxSectionNameLen = 64; |
496 | |
497 | bool ForEachSection(int fd, |
498 | const std::function<bool(const std::string &name, |
499 | const ElfW(Shdr) &)> &callback) { |
500 | ElfW(Ehdr) ; |
501 | if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) { |
502 | return false; |
503 | } |
504 | |
505 | ElfW(Shdr) shstrtab; |
506 | off_t shstrtab_offset = |
507 | (elf_header.e_shoff + elf_header.e_shentsize * elf_header.e_shstrndx); |
508 | if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) { |
509 | return false; |
510 | } |
511 | |
512 | for (int i = 0; i < elf_header.e_shnum; ++i) { |
513 | ElfW(Shdr) out; |
514 | off_t = |
515 | (elf_header.e_shoff + elf_header.e_shentsize * i); |
516 | if (!ReadFromOffsetExact(fd, &out, sizeof(out), section_header_offset)) { |
517 | return false; |
518 | } |
519 | off_t name_offset = shstrtab.sh_offset + out.sh_name; |
520 | char [kMaxSectionNameLen + 1]; |
521 | ssize_t n_read = |
522 | ReadFromOffset(fd, &header_name, kMaxSectionNameLen, name_offset); |
523 | if (n_read == -1) { |
524 | return false; |
525 | } else if (n_read > kMaxSectionNameLen) { |
526 | // Long read? |
527 | return false; |
528 | } |
529 | header_name[n_read] = '\0'; |
530 | |
531 | std::string name(header_name); |
532 | if (!callback(name, out)) { |
533 | break; |
534 | } |
535 | } |
536 | return true; |
537 | } |
538 | |
539 | // name_len should include terminating '\0'. |
540 | bool (int fd, const char *name, size_t name_len, |
541 | ElfW(Shdr) * out) { |
542 | char [kMaxSectionNameLen]; |
543 | if (sizeof(header_name) < name_len) { |
544 | ABSL_RAW_LOG(WARNING, |
545 | "Section name '%s' is too long (%zu); " |
546 | "section will not be found (even if present)." , |
547 | name, name_len); |
548 | // No point in even trying. |
549 | return false; |
550 | } |
551 | |
552 | ElfW(Ehdr) ; |
553 | if (!ReadFromOffsetExact(fd, &elf_header, sizeof(elf_header), 0)) { |
554 | return false; |
555 | } |
556 | |
557 | ElfW(Shdr) shstrtab; |
558 | off_t shstrtab_offset = |
559 | (elf_header.e_shoff + elf_header.e_shentsize * elf_header.e_shstrndx); |
560 | if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) { |
561 | return false; |
562 | } |
563 | |
564 | for (int i = 0; i < elf_header.e_shnum; ++i) { |
565 | off_t = |
566 | (elf_header.e_shoff + elf_header.e_shentsize * i); |
567 | if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) { |
568 | return false; |
569 | } |
570 | off_t name_offset = shstrtab.sh_offset + out->sh_name; |
571 | ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset); |
572 | if (n_read < 0) { |
573 | return false; |
574 | } else if (static_cast<size_t>(n_read) != name_len) { |
575 | // Short read -- name could be at end of file. |
576 | continue; |
577 | } |
578 | if (memcmp(header_name, name, name_len) == 0) { |
579 | return true; |
580 | } |
581 | } |
582 | return false; |
583 | } |
584 | |
585 | // Compare symbols at in the same address. |
586 | // Return true if we should pick symbol1. |
587 | static bool ShouldPickFirstSymbol(const ElfW(Sym) & symbol1, |
588 | const ElfW(Sym) & symbol2) { |
589 | // If one of the symbols is weak and the other is not, pick the one |
590 | // this is not a weak symbol. |
591 | char bind1 = ELF_ST_BIND(symbol1.st_info); |
592 | char bind2 = ELF_ST_BIND(symbol1.st_info); |
593 | if (bind1 == STB_WEAK && bind2 != STB_WEAK) return false; |
594 | if (bind2 == STB_WEAK && bind1 != STB_WEAK) return true; |
595 | |
596 | // If one of the symbols has zero size and the other is not, pick the |
597 | // one that has non-zero size. |
598 | if (symbol1.st_size != 0 && symbol2.st_size == 0) { |
599 | return true; |
600 | } |
601 | if (symbol1.st_size == 0 && symbol2.st_size != 0) { |
602 | return false; |
603 | } |
604 | |
605 | // If one of the symbols has no type and the other is not, pick the |
606 | // one that has a type. |
607 | char type1 = ELF_ST_TYPE(symbol1.st_info); |
608 | char type2 = ELF_ST_TYPE(symbol1.st_info); |
609 | if (type1 != STT_NOTYPE && type2 == STT_NOTYPE) { |
610 | return true; |
611 | } |
612 | if (type1 == STT_NOTYPE && type2 != STT_NOTYPE) { |
613 | return false; |
614 | } |
615 | |
616 | // Pick the first one, if we still cannot decide. |
617 | return true; |
618 | } |
619 | |
620 | // Return true if an address is inside a section. |
621 | static bool InSection(const void *address, const ElfW(Shdr) * section) { |
622 | const char *start = reinterpret_cast<const char *>(section->sh_addr); |
623 | size_t size = static_cast<size_t>(section->sh_size); |
624 | return start <= address && address < (start + size); |
625 | } |
626 | |
627 | // Read a symbol table and look for the symbol containing the |
628 | // pc. Iterate over symbols in a symbol table and look for the symbol |
629 | // containing "pc". If the symbol is found, and its name fits in |
630 | // out_size, the name is written into out and SYMBOL_FOUND is returned. |
631 | // If the name does not fit, truncated name is written into out, |
632 | // and SYMBOL_TRUNCATED is returned. Out is NUL-terminated. |
633 | // If the symbol is not found, SYMBOL_NOT_FOUND is returned; |
634 | // To keep stack consumption low, we would like this function to not get |
635 | // inlined. |
636 | static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( |
637 | const void *const pc, const int fd, char *out, int out_size, |
638 | ptrdiff_t relocation, const ElfW(Shdr) * strtab, const ElfW(Shdr) * symtab, |
639 | const ElfW(Shdr) * opd, char *tmp_buf, int tmp_buf_size) { |
640 | if (symtab == nullptr) { |
641 | return SYMBOL_NOT_FOUND; |
642 | } |
643 | |
644 | // Read multiple symbols at once to save read() calls. |
645 | ElfW(Sym) *buf = reinterpret_cast<ElfW(Sym) *>(tmp_buf); |
646 | const int buf_entries = tmp_buf_size / sizeof(buf[0]); |
647 | |
648 | const int num_symbols = symtab->sh_size / symtab->sh_entsize; |
649 | |
650 | // On platforms using an .opd section (PowerPC & IA64), a function symbol |
651 | // has the address of a function descriptor, which contains the real |
652 | // starting address. However, we do not always want to use the real |
653 | // starting address because we sometimes want to symbolize a function |
654 | // pointer into the .opd section, e.g. FindSymbol(&foo,...). |
655 | const bool pc_in_opd = |
656 | kPlatformUsesOPDSections && opd != nullptr && InSection(pc, opd); |
657 | const bool deref_function_descriptor_pointer = |
658 | kPlatformUsesOPDSections && opd != nullptr && !pc_in_opd; |
659 | |
660 | ElfW(Sym) best_match; |
661 | SafeMemZero(&best_match, sizeof(best_match)); |
662 | bool found_match = false; |
663 | for (int i = 0; i < num_symbols;) { |
664 | off_t offset = symtab->sh_offset + i * symtab->sh_entsize; |
665 | const int num_remaining_symbols = num_symbols - i; |
666 | const int entries_in_chunk = std::min(num_remaining_symbols, buf_entries); |
667 | const int bytes_in_chunk = entries_in_chunk * sizeof(buf[0]); |
668 | const ssize_t len = ReadFromOffset(fd, buf, bytes_in_chunk, offset); |
669 | SAFE_ASSERT(len % sizeof(buf[0]) == 0); |
670 | const ssize_t num_symbols_in_buf = len / sizeof(buf[0]); |
671 | SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk); |
672 | for (int j = 0; j < num_symbols_in_buf; ++j) { |
673 | const ElfW(Sym) &symbol = buf[j]; |
674 | |
675 | // For a DSO, a symbol address is relocated by the loading address. |
676 | // We keep the original address for opd redirection below. |
677 | const char *const original_start_address = |
678 | reinterpret_cast<const char *>(symbol.st_value); |
679 | const char *start_address = original_start_address + relocation; |
680 | |
681 | if (deref_function_descriptor_pointer && |
682 | InSection(original_start_address, opd)) { |
683 | // The opd section is mapped into memory. Just dereference |
684 | // start_address to get the first double word, which points to the |
685 | // function entry. |
686 | start_address = *reinterpret_cast<const char *const *>(start_address); |
687 | } |
688 | |
689 | // If pc is inside the .opd section, it points to a function descriptor. |
690 | const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size; |
691 | const void *const end_address = |
692 | reinterpret_cast<const char *>(start_address) + size; |
693 | if (symbol.st_value != 0 && // Skip null value symbols. |
694 | symbol.st_shndx != 0 && // Skip undefined symbols. |
695 | #ifdef STT_TLS |
696 | ELF_ST_TYPE(symbol.st_info) != STT_TLS && // Skip thread-local data. |
697 | #endif // STT_TLS |
698 | ((start_address <= pc && pc < end_address) || |
699 | (start_address == pc && pc == end_address))) { |
700 | if (!found_match || ShouldPickFirstSymbol(symbol, best_match)) { |
701 | found_match = true; |
702 | best_match = symbol; |
703 | } |
704 | } |
705 | } |
706 | i += num_symbols_in_buf; |
707 | } |
708 | |
709 | if (found_match) { |
710 | const size_t off = strtab->sh_offset + best_match.st_name; |
711 | const ssize_t n_read = ReadFromOffset(fd, out, out_size, off); |
712 | if (n_read <= 0) { |
713 | // This should never happen. |
714 | ABSL_RAW_LOG(WARNING, |
715 | "Unable to read from fd %d at offset %zu: n_read = %zd" , fd, |
716 | off, n_read); |
717 | return SYMBOL_NOT_FOUND; |
718 | } |
719 | ABSL_RAW_CHECK(n_read <= out_size, "ReadFromOffset read too much data." ); |
720 | |
721 | // strtab->sh_offset points into .strtab-like section that contains |
722 | // NUL-terminated strings: '\0foo\0barbaz\0...". |
723 | // |
724 | // sh_offset+st_name points to the start of symbol name, but we don't know |
725 | // how long the symbol is, so we try to read as much as we have space for, |
726 | // and usually over-read (i.e. there is a NUL somewhere before n_read). |
727 | if (memchr(out, '\0', n_read) == nullptr) { |
728 | // Either out_size was too small (n_read == out_size and no NUL), or |
729 | // we tried to read past the EOF (n_read < out_size) and .strtab is |
730 | // corrupt (missing terminating NUL; should never happen for valid ELF). |
731 | out[n_read - 1] = '\0'; |
732 | return SYMBOL_TRUNCATED; |
733 | } |
734 | return SYMBOL_FOUND; |
735 | } |
736 | |
737 | return SYMBOL_NOT_FOUND; |
738 | } |
739 | |
740 | // Get the symbol name of "pc" from the file pointed by "fd". Process |
741 | // both regular and dynamic symbol tables if necessary. |
742 | // See FindSymbol() comment for description of return value. |
743 | FindSymbolResult Symbolizer::GetSymbolFromObjectFile( |
744 | const ObjFile &obj, const void *const pc, const ptrdiff_t relocation, |
745 | char *out, int out_size, char *tmp_buf, int tmp_buf_size) { |
746 | ElfW(Shdr) symtab; |
747 | ElfW(Shdr) strtab; |
748 | ElfW(Shdr) opd; |
749 | ElfW(Shdr) *opd_ptr = nullptr; |
750 | |
751 | // On platforms using an .opd sections for function descriptor, read |
752 | // the section header. The .opd section is in data segment and should be |
753 | // loaded but we check that it is mapped just to be extra careful. |
754 | if (kPlatformUsesOPDSections) { |
755 | if (GetSectionHeaderByName(obj.fd, kOpdSectionName, |
756 | sizeof(kOpdSectionName) - 1, &opd) && |
757 | FindObjFile(reinterpret_cast<const char *>(opd.sh_addr) + relocation, |
758 | opd.sh_size) != nullptr) { |
759 | opd_ptr = &opd; |
760 | } else { |
761 | return SYMBOL_NOT_FOUND; |
762 | } |
763 | } |
764 | |
765 | // Consult a regular symbol table first. |
766 | if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, |
767 | obj.elf_header.e_shoff, SHT_SYMTAB, &symtab, |
768 | tmp_buf, tmp_buf_size)) { |
769 | return SYMBOL_NOT_FOUND; |
770 | } |
771 | if (!ReadFromOffsetExact( |
772 | obj.fd, &strtab, sizeof(strtab), |
773 | obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { |
774 | return SYMBOL_NOT_FOUND; |
775 | } |
776 | const FindSymbolResult rc = |
777 | FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, |
778 | opd_ptr, tmp_buf, tmp_buf_size); |
779 | if (rc != SYMBOL_NOT_FOUND) { |
780 | return rc; // Found the symbol in a regular symbol table. |
781 | } |
782 | |
783 | // If the symbol is not found, then consult a dynamic symbol table. |
784 | if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, |
785 | obj.elf_header.e_shoff, SHT_DYNSYM, &symtab, |
786 | tmp_buf, tmp_buf_size)) { |
787 | return SYMBOL_NOT_FOUND; |
788 | } |
789 | if (!ReadFromOffsetExact( |
790 | obj.fd, &strtab, sizeof(strtab), |
791 | obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { |
792 | return SYMBOL_NOT_FOUND; |
793 | } |
794 | return FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, |
795 | opd_ptr, tmp_buf, tmp_buf_size); |
796 | } |
797 | |
798 | namespace { |
799 | // Thin wrapper around a file descriptor so that the file descriptor |
800 | // gets closed for sure. |
801 | class FileDescriptor { |
802 | public: |
803 | explicit FileDescriptor(int fd) : fd_(fd) {} |
804 | FileDescriptor(const FileDescriptor &) = delete; |
805 | FileDescriptor &operator=(const FileDescriptor &) = delete; |
806 | |
807 | ~FileDescriptor() { |
808 | if (fd_ >= 0) { |
809 | NO_INTR(close(fd_)); |
810 | } |
811 | } |
812 | |
813 | int get() const { return fd_; } |
814 | |
815 | private: |
816 | const int fd_; |
817 | }; |
818 | |
819 | // Helper class for reading lines from file. |
820 | // |
821 | // Note: we don't use ProcMapsIterator since the object is big (it has |
822 | // a 5k array member) and uses async-unsafe functions such as sscanf() |
823 | // and snprintf(). |
824 | class LineReader { |
825 | public: |
826 | explicit LineReader(int fd, char *buf, int buf_len) |
827 | : fd_(fd), |
828 | buf_len_(buf_len), |
829 | buf_(buf), |
830 | bol_(buf), |
831 | eol_(buf), |
832 | eod_(buf) {} |
833 | |
834 | LineReader(const LineReader &) = delete; |
835 | LineReader &operator=(const LineReader &) = delete; |
836 | |
837 | // Read '\n'-terminated line from file. On success, modify "bol" |
838 | // and "eol", then return true. Otherwise, return false. |
839 | // |
840 | // Note: if the last line doesn't end with '\n', the line will be |
841 | // dropped. It's an intentional behavior to make the code simple. |
842 | bool ReadLine(const char **bol, const char **eol) { |
843 | if (BufferIsEmpty()) { // First time. |
844 | const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_); |
845 | if (num_bytes <= 0) { // EOF or error. |
846 | return false; |
847 | } |
848 | eod_ = buf_ + num_bytes; |
849 | bol_ = buf_; |
850 | } else { |
851 | bol_ = eol_ + 1; // Advance to the next line in the buffer. |
852 | SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_". |
853 | if (!HasCompleteLine()) { |
854 | const int incomplete_line_length = eod_ - bol_; |
855 | // Move the trailing incomplete line to the beginning. |
856 | memmove(buf_, bol_, incomplete_line_length); |
857 | // Read text from file and append it. |
858 | char *const append_pos = buf_ + incomplete_line_length; |
859 | const int capacity_left = buf_len_ - incomplete_line_length; |
860 | const ssize_t num_bytes = |
861 | ReadPersistent(fd_, append_pos, capacity_left); |
862 | if (num_bytes <= 0) { // EOF or error. |
863 | return false; |
864 | } |
865 | eod_ = append_pos + num_bytes; |
866 | bol_ = buf_; |
867 | } |
868 | } |
869 | eol_ = FindLineFeed(); |
870 | if (eol_ == nullptr) { // '\n' not found. Malformed line. |
871 | return false; |
872 | } |
873 | *eol_ = '\0'; // Replace '\n' with '\0'. |
874 | |
875 | *bol = bol_; |
876 | *eol = eol_; |
877 | return true; |
878 | } |
879 | |
880 | private: |
881 | char *FindLineFeed() const { |
882 | return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_)); |
883 | } |
884 | |
885 | bool BufferIsEmpty() const { return buf_ == eod_; } |
886 | |
887 | bool HasCompleteLine() const { |
888 | return !BufferIsEmpty() && FindLineFeed() != nullptr; |
889 | } |
890 | |
891 | const int fd_; |
892 | const int buf_len_; |
893 | char *const buf_; |
894 | char *bol_; |
895 | char *eol_; |
896 | const char *eod_; // End of data in "buf_". |
897 | }; |
898 | } // namespace |
899 | |
900 | // Place the hex number read from "start" into "*hex". The pointer to |
901 | // the first non-hex character or "end" is returned. |
902 | static const char *GetHex(const char *start, const char *end, |
903 | uint64_t *const value) { |
904 | uint64_t hex = 0; |
905 | const char *p; |
906 | for (p = start; p < end; ++p) { |
907 | int ch = *p; |
908 | if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || |
909 | (ch >= 'a' && ch <= 'f')) { |
910 | hex = (hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9); |
911 | } else { // Encountered the first non-hex character. |
912 | break; |
913 | } |
914 | } |
915 | SAFE_ASSERT(p <= end); |
916 | *value = hex; |
917 | return p; |
918 | } |
919 | |
920 | static const char *GetHex(const char *start, const char *end, |
921 | const void **const addr) { |
922 | uint64_t hex = 0; |
923 | const char *p = GetHex(start, end, &hex); |
924 | *addr = reinterpret_cast<void *>(hex); |
925 | return p; |
926 | } |
927 | |
928 | // Normally we are only interested in "r?x" maps. |
929 | // On the PowerPC, function pointers point to descriptors in the .opd |
930 | // section. The descriptors themselves are not executable code, so |
931 | // we need to relax the check below to "r??". |
932 | static bool ShouldUseMapping(const char *const flags) { |
933 | return flags[0] == 'r' && (kPlatformUsesOPDSections || flags[2] == 'x'); |
934 | } |
935 | |
936 | // Read /proc/self/maps and run "callback" for each mmapped file found. If |
937 | // "callback" returns false, stop scanning and return true. Else continue |
938 | // scanning /proc/self/maps. Return true if no parse error is found. |
939 | static ABSL_ATTRIBUTE_NOINLINE bool ReadAddrMap( |
940 | bool (*callback)(const char *filename, const void *const start_addr, |
941 | const void *const end_addr, uint64_t offset, void *arg), |
942 | void *arg, void *tmp_buf, int tmp_buf_size) { |
943 | // Use /proc/self/task/<pid>/maps instead of /proc/self/maps. The latter |
944 | // requires kernel to stop all threads, and is significantly slower when there |
945 | // are 1000s of threads. |
946 | char maps_path[80]; |
947 | snprintf(maps_path, sizeof(maps_path), "/proc/self/task/%d/maps" , getpid()); |
948 | |
949 | int maps_fd; |
950 | NO_INTR(maps_fd = open(maps_path, O_RDONLY)); |
951 | FileDescriptor wrapped_maps_fd(maps_fd); |
952 | if (wrapped_maps_fd.get() < 0) { |
953 | ABSL_RAW_LOG(WARNING, "%s: errno=%d" , maps_path, errno); |
954 | return false; |
955 | } |
956 | |
957 | // Iterate over maps and look for the map containing the pc. Then |
958 | // look into the symbol tables inside. |
959 | LineReader reader(wrapped_maps_fd.get(), static_cast<char *>(tmp_buf), |
960 | tmp_buf_size); |
961 | while (true) { |
962 | const char *cursor; |
963 | const char *eol; |
964 | if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line. |
965 | break; |
966 | } |
967 | |
968 | const char *line = cursor; |
969 | const void *start_address; |
970 | // Start parsing line in /proc/self/maps. Here is an example: |
971 | // |
972 | // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat |
973 | // |
974 | // We want start address (08048000), end address (0804c000), flags |
975 | // (r-xp) and file name (/bin/cat). |
976 | |
977 | // Read start address. |
978 | cursor = GetHex(cursor, eol, &start_address); |
979 | if (cursor == eol || *cursor != '-') { |
980 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s" , line); |
981 | return false; |
982 | } |
983 | ++cursor; // Skip '-'. |
984 | |
985 | // Read end address. |
986 | const void *end_address; |
987 | cursor = GetHex(cursor, eol, &end_address); |
988 | if (cursor == eol || *cursor != ' ') { |
989 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s" , line); |
990 | return false; |
991 | } |
992 | ++cursor; // Skip ' '. |
993 | |
994 | // Read flags. Skip flags until we encounter a space or eol. |
995 | const char *const flags_start = cursor; |
996 | while (cursor < eol && *cursor != ' ') { |
997 | ++cursor; |
998 | } |
999 | // We expect at least four letters for flags (ex. "r-xp"). |
1000 | if (cursor == eol || cursor < flags_start + 4) { |
1001 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps: %s" , line); |
1002 | return false; |
1003 | } |
1004 | |
1005 | // Check flags. |
1006 | if (!ShouldUseMapping(flags_start)) { |
1007 | continue; // We skip this map. |
1008 | } |
1009 | ++cursor; // Skip ' '. |
1010 | |
1011 | // Read file offset. |
1012 | uint64_t offset; |
1013 | cursor = GetHex(cursor, eol, &offset); |
1014 | ++cursor; // Skip ' '. |
1015 | |
1016 | // Skip to file name. "cursor" now points to dev. We need to skip at least |
1017 | // two spaces for dev and inode. |
1018 | int num_spaces = 0; |
1019 | while (cursor < eol) { |
1020 | if (*cursor == ' ') { |
1021 | ++num_spaces; |
1022 | } else if (num_spaces >= 2) { |
1023 | // The first non-space character after skipping two spaces |
1024 | // is the beginning of the file name. |
1025 | break; |
1026 | } |
1027 | ++cursor; |
1028 | } |
1029 | |
1030 | // Check whether this entry corresponds to our hint table for the true |
1031 | // filename. |
1032 | bool hinted = |
1033 | GetFileMappingHint(&start_address, &end_address, &offset, &cursor); |
1034 | if (!hinted && (cursor == eol || cursor[0] == '[')) { |
1035 | // not an object file, typically [vdso] or [vsyscall] |
1036 | continue; |
1037 | } |
1038 | if (!callback(cursor, start_address, end_address, offset, arg)) break; |
1039 | } |
1040 | return true; |
1041 | } |
1042 | |
1043 | // Find the objfile mapped in address region containing [addr, addr + len). |
1044 | ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) { |
1045 | for (int i = 0; i < 2; ++i) { |
1046 | if (!ok_) return nullptr; |
1047 | |
1048 | // Read /proc/self/maps if necessary |
1049 | if (!addr_map_read_) { |
1050 | addr_map_read_ = true; |
1051 | if (!ReadAddrMap(RegisterObjFile, this, tmp_buf_, TMP_BUF_SIZE)) { |
1052 | ok_ = false; |
1053 | return nullptr; |
1054 | } |
1055 | } |
1056 | |
1057 | int lo = 0; |
1058 | int hi = addr_map_.Size(); |
1059 | while (lo < hi) { |
1060 | int mid = (lo + hi) / 2; |
1061 | if (addr < addr_map_.At(mid)->end_addr) { |
1062 | hi = mid; |
1063 | } else { |
1064 | lo = mid + 1; |
1065 | } |
1066 | } |
1067 | if (lo != addr_map_.Size()) { |
1068 | ObjFile *obj = addr_map_.At(lo); |
1069 | SAFE_ASSERT(obj->end_addr > addr); |
1070 | if (addr >= obj->start_addr && |
1071 | reinterpret_cast<const char *>(addr) + len <= obj->end_addr) |
1072 | return obj; |
1073 | } |
1074 | |
1075 | // The address mapping may have changed since it was last read. Retry. |
1076 | ClearAddrMap(); |
1077 | } |
1078 | return nullptr; |
1079 | } |
1080 | |
1081 | void Symbolizer::ClearAddrMap() { |
1082 | for (int i = 0; i != addr_map_.Size(); i++) { |
1083 | ObjFile *o = addr_map_.At(i); |
1084 | base_internal::LowLevelAlloc::Free(o->filename); |
1085 | if (o->fd >= 0) { |
1086 | NO_INTR(close(o->fd)); |
1087 | } |
1088 | } |
1089 | addr_map_.Clear(); |
1090 | addr_map_read_ = false; |
1091 | } |
1092 | |
1093 | // Callback for ReadAddrMap to register objfiles in an in-memory table. |
1094 | bool Symbolizer::RegisterObjFile(const char *filename, |
1095 | const void *const start_addr, |
1096 | const void *const end_addr, uint64_t offset, |
1097 | void *arg) { |
1098 | Symbolizer *impl = static_cast<Symbolizer *>(arg); |
1099 | |
1100 | // Files are supposed to be added in the increasing address order. Make |
1101 | // sure that's the case. |
1102 | int addr_map_size = impl->addr_map_.Size(); |
1103 | if (addr_map_size != 0) { |
1104 | ObjFile *old = impl->addr_map_.At(addr_map_size - 1); |
1105 | if (old->end_addr > end_addr) { |
1106 | ABSL_RAW_LOG(ERROR, |
1107 | "Unsorted addr map entry: 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR |
1108 | ": %s" , |
1109 | reinterpret_cast<uintptr_t>(end_addr), filename, |
1110 | reinterpret_cast<uintptr_t>(old->end_addr), old->filename); |
1111 | return true; |
1112 | } else if (old->end_addr == end_addr) { |
1113 | // The same entry appears twice. This sometimes happens for [vdso]. |
1114 | if (old->start_addr != start_addr || |
1115 | strcmp(old->filename, filename) != 0) { |
1116 | ABSL_RAW_LOG(ERROR, |
1117 | "Duplicate addr 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR ": %s" , |
1118 | reinterpret_cast<uintptr_t>(end_addr), filename, |
1119 | reinterpret_cast<uintptr_t>(old->end_addr), old->filename); |
1120 | } |
1121 | return true; |
1122 | } |
1123 | } |
1124 | ObjFile *obj = impl->addr_map_.Add(); |
1125 | obj->filename = impl->CopyString(filename); |
1126 | obj->start_addr = start_addr; |
1127 | obj->end_addr = end_addr; |
1128 | obj->offset = offset; |
1129 | obj->elf_type = -1; // filled on demand |
1130 | obj->fd = -1; // opened on demand |
1131 | return true; |
1132 | } |
1133 | |
1134 | // This function wraps the Demangle function to provide an interface |
1135 | // where the input symbol is demangled in-place. |
1136 | // To keep stack consumption low, we would like this function to not |
1137 | // get inlined. |
1138 | static ABSL_ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size, |
1139 | char *tmp_buf, |
1140 | int tmp_buf_size) { |
1141 | if (Demangle(out, tmp_buf, tmp_buf_size)) { |
1142 | // Demangling succeeded. Copy to out if the space allows. |
1143 | int len = strlen(tmp_buf); |
1144 | if (len + 1 <= out_size) { // +1 for '\0'. |
1145 | SAFE_ASSERT(len < tmp_buf_size); |
1146 | memmove(out, tmp_buf, len + 1); |
1147 | } |
1148 | } |
1149 | } |
1150 | |
1151 | SymbolCacheLine *Symbolizer::GetCacheLine(const void *const pc) { |
1152 | uintptr_t pc0 = reinterpret_cast<uintptr_t>(pc); |
1153 | pc0 >>= 3; // drop the low 3 bits |
1154 | |
1155 | // Shuffle bits. |
1156 | pc0 ^= (pc0 >> 6) ^ (pc0 >> 12) ^ (pc0 >> 18); |
1157 | return &symbol_cache_[pc0 % SYMBOL_CACHE_LINES]; |
1158 | } |
1159 | |
1160 | void Symbolizer::AgeSymbols(SymbolCacheLine *line) { |
1161 | for (uint32_t &age : line->age) { |
1162 | ++age; |
1163 | } |
1164 | } |
1165 | |
1166 | const char *Symbolizer::FindSymbolInCache(const void *const pc) { |
1167 | if (pc == nullptr) return nullptr; |
1168 | |
1169 | SymbolCacheLine *line = GetCacheLine(pc); |
1170 | for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) { |
1171 | if (line->pc[i] == pc) { |
1172 | AgeSymbols(line); |
1173 | line->age[i] = 0; |
1174 | return line->name[i]; |
1175 | } |
1176 | } |
1177 | return nullptr; |
1178 | } |
1179 | |
1180 | const char *Symbolizer::InsertSymbolInCache(const void *const pc, |
1181 | const char *name) { |
1182 | SAFE_ASSERT(pc != nullptr); |
1183 | |
1184 | SymbolCacheLine *line = GetCacheLine(pc); |
1185 | uint32_t max_age = 0; |
1186 | int oldest_index = -1; |
1187 | for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) { |
1188 | if (line->pc[i] == nullptr) { |
1189 | AgeSymbols(line); |
1190 | line->pc[i] = pc; |
1191 | line->name[i] = CopyString(name); |
1192 | line->age[i] = 0; |
1193 | return line->name[i]; |
1194 | } |
1195 | if (line->age[i] >= max_age) { |
1196 | max_age = line->age[i]; |
1197 | oldest_index = i; |
1198 | } |
1199 | } |
1200 | |
1201 | AgeSymbols(line); |
1202 | ABSL_RAW_CHECK(oldest_index >= 0, "Corrupt cache" ); |
1203 | base_internal::LowLevelAlloc::Free(line->name[oldest_index]); |
1204 | line->pc[oldest_index] = pc; |
1205 | line->name[oldest_index] = CopyString(name); |
1206 | line->age[oldest_index] = 0; |
1207 | return line->name[oldest_index]; |
1208 | } |
1209 | |
1210 | static void MaybeOpenFdFromSelfExe(ObjFile *obj) { |
1211 | if (memcmp(obj->start_addr, ELFMAG, SELFMAG) != 0) { |
1212 | return; |
1213 | } |
1214 | int fd = open("/proc/self/exe" , O_RDONLY); |
1215 | if (fd == -1) { |
1216 | return; |
1217 | } |
1218 | // Verify that contents of /proc/self/exe matches in-memory image of |
1219 | // the binary. This can fail if the "deleted" binary is in fact not |
1220 | // the main executable, or for binaries that have the first PT_LOAD |
1221 | // segment smaller than 4K. We do it in four steps so that the |
1222 | // buffer is smaller and we don't consume too much stack space. |
1223 | const char *mem = reinterpret_cast<const char *>(obj->start_addr); |
1224 | for (int i = 0; i < 4; ++i) { |
1225 | char buf[1024]; |
1226 | ssize_t n = read(fd, buf, sizeof(buf)); |
1227 | if (n != sizeof(buf) || memcmp(buf, mem, sizeof(buf)) != 0) { |
1228 | close(fd); |
1229 | return; |
1230 | } |
1231 | mem += sizeof(buf); |
1232 | } |
1233 | obj->fd = fd; |
1234 | } |
1235 | |
1236 | static bool MaybeInitializeObjFile(ObjFile *obj) { |
1237 | if (obj->fd < 0) { |
1238 | obj->fd = open(obj->filename, O_RDONLY); |
1239 | |
1240 | if (obj->fd < 0) { |
1241 | // Getting /proc/self/exe here means that we were hinted. |
1242 | if (strcmp(obj->filename, "/proc/self/exe" ) == 0) { |
1243 | // /proc/self/exe may be inaccessible (due to setuid, etc.), so try |
1244 | // accessing the binary via argv0. |
1245 | if (argv0_value != nullptr) { |
1246 | obj->fd = open(argv0_value, O_RDONLY); |
1247 | } |
1248 | } else { |
1249 | MaybeOpenFdFromSelfExe(obj); |
1250 | } |
1251 | } |
1252 | |
1253 | if (obj->fd < 0) { |
1254 | ABSL_RAW_LOG(WARNING, "%s: open failed: errno=%d" , obj->filename, errno); |
1255 | return false; |
1256 | } |
1257 | obj->elf_type = FileGetElfType(obj->fd); |
1258 | if (obj->elf_type < 0) { |
1259 | ABSL_RAW_LOG(WARNING, "%s: wrong elf type: %d" , obj->filename, |
1260 | obj->elf_type); |
1261 | return false; |
1262 | } |
1263 | |
1264 | if (!ReadFromOffsetExact(obj->fd, &obj->elf_header, sizeof(obj->elf_header), |
1265 | 0)) { |
1266 | ABSL_RAW_LOG(WARNING, "%s: failed to read elf header" , obj->filename); |
1267 | return false; |
1268 | } |
1269 | } |
1270 | return true; |
1271 | } |
1272 | |
1273 | // The implementation of our symbolization routine. If it |
1274 | // successfully finds the symbol containing "pc" and obtains the |
1275 | // symbol name, returns pointer to that symbol. Otherwise, returns nullptr. |
1276 | // If any symbol decorators have been installed via InstallSymbolDecorator(), |
1277 | // they are called here as well. |
1278 | // To keep stack consumption low, we would like this function to not |
1279 | // get inlined. |
1280 | const char *Symbolizer::GetSymbol(const void *const pc) { |
1281 | const char *entry = FindSymbolInCache(pc); |
1282 | if (entry != nullptr) { |
1283 | return entry; |
1284 | } |
1285 | symbol_buf_[0] = '\0'; |
1286 | |
1287 | ObjFile *const obj = FindObjFile(pc, 1); |
1288 | ptrdiff_t relocation = 0; |
1289 | int fd = -1; |
1290 | if (obj != nullptr) { |
1291 | if (MaybeInitializeObjFile(obj)) { |
1292 | if (obj->elf_type == ET_DYN && |
1293 | reinterpret_cast<uint64_t>(obj->start_addr) >= obj->offset) { |
1294 | // This object was relocated. |
1295 | // |
1296 | // For obj->offset > 0, adjust the relocation since a mapping at offset |
1297 | // X in the file will have a start address of [true relocation]+X. |
1298 | relocation = reinterpret_cast<ptrdiff_t>(obj->start_addr) - obj->offset; |
1299 | } |
1300 | |
1301 | fd = obj->fd; |
1302 | } |
1303 | if (GetSymbolFromObjectFile(*obj, pc, relocation, symbol_buf_, |
1304 | sizeof(symbol_buf_), tmp_buf_, |
1305 | sizeof(tmp_buf_)) == SYMBOL_FOUND) { |
1306 | // Only try to demangle the symbol name if it fit into symbol_buf_. |
1307 | DemangleInplace(symbol_buf_, sizeof(symbol_buf_), tmp_buf_, |
1308 | sizeof(tmp_buf_)); |
1309 | } |
1310 | } else { |
1311 | #if ABSL_HAVE_VDSO_SUPPORT |
1312 | VDSOSupport vdso; |
1313 | if (vdso.IsPresent()) { |
1314 | VDSOSupport::SymbolInfo symbol_info; |
1315 | if (vdso.LookupSymbolByAddress(pc, &symbol_info)) { |
1316 | // All VDSO symbols are known to be short. |
1317 | size_t len = strlen(symbol_info.name); |
1318 | ABSL_RAW_CHECK(len + 1 < sizeof(symbol_buf_), |
1319 | "VDSO symbol unexpectedly long" ); |
1320 | memcpy(symbol_buf_, symbol_info.name, len + 1); |
1321 | } |
1322 | } |
1323 | #endif |
1324 | } |
1325 | |
1326 | if (g_decorators_mu.TryLock()) { |
1327 | if (g_num_decorators > 0) { |
1328 | SymbolDecoratorArgs decorator_args = { |
1329 | pc, relocation, fd, symbol_buf_, sizeof(symbol_buf_), |
1330 | tmp_buf_, sizeof(tmp_buf_), nullptr}; |
1331 | for (int i = 0; i < g_num_decorators; ++i) { |
1332 | decorator_args.arg = g_decorators[i].arg; |
1333 | g_decorators[i].fn(&decorator_args); |
1334 | } |
1335 | } |
1336 | g_decorators_mu.Unlock(); |
1337 | } |
1338 | if (symbol_buf_[0] == '\0') { |
1339 | return nullptr; |
1340 | } |
1341 | symbol_buf_[sizeof(symbol_buf_) - 1] = '\0'; // Paranoia. |
1342 | return InsertSymbolInCache(pc, symbol_buf_); |
1343 | } |
1344 | |
1345 | bool RemoveAllSymbolDecorators(void) { |
1346 | if (!g_decorators_mu.TryLock()) { |
1347 | // Someone else is using decorators. Get out. |
1348 | return false; |
1349 | } |
1350 | g_num_decorators = 0; |
1351 | g_decorators_mu.Unlock(); |
1352 | return true; |
1353 | } |
1354 | |
1355 | bool RemoveSymbolDecorator(int ticket) { |
1356 | if (!g_decorators_mu.TryLock()) { |
1357 | // Someone else is using decorators. Get out. |
1358 | return false; |
1359 | } |
1360 | for (int i = 0; i < g_num_decorators; ++i) { |
1361 | if (g_decorators[i].ticket == ticket) { |
1362 | while (i < g_num_decorators - 1) { |
1363 | g_decorators[i] = g_decorators[i + 1]; |
1364 | ++i; |
1365 | } |
1366 | g_num_decorators = i; |
1367 | break; |
1368 | } |
1369 | } |
1370 | g_decorators_mu.Unlock(); |
1371 | return true; // Decorator is known to be removed. |
1372 | } |
1373 | |
1374 | int InstallSymbolDecorator(SymbolDecorator decorator, void *arg) { |
1375 | static int ticket = 0; |
1376 | |
1377 | if (!g_decorators_mu.TryLock()) { |
1378 | // Someone else is using decorators. Get out. |
1379 | return false; |
1380 | } |
1381 | int ret = ticket; |
1382 | if (g_num_decorators >= kMaxDecorators) { |
1383 | ret = -1; |
1384 | } else { |
1385 | g_decorators[g_num_decorators] = {decorator, arg, ticket++}; |
1386 | ++g_num_decorators; |
1387 | } |
1388 | g_decorators_mu.Unlock(); |
1389 | return ret; |
1390 | } |
1391 | |
1392 | bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset, |
1393 | const char *filename) { |
1394 | SAFE_ASSERT(start <= end); |
1395 | SAFE_ASSERT(filename != nullptr); |
1396 | |
1397 | InitSigSafeArena(); |
1398 | |
1399 | if (!g_file_mapping_mu.TryLock()) { |
1400 | return false; |
1401 | } |
1402 | |
1403 | bool ret = true; |
1404 | if (g_num_file_mapping_hints >= kMaxFileMappingHints) { |
1405 | ret = false; |
1406 | } else { |
1407 | // TODO(ckennelly): Move this into a std::string copy routine. |
1408 | int len = strlen(filename); |
1409 | char *dst = static_cast<char *>( |
1410 | base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); |
1411 | ABSL_RAW_CHECK(dst != nullptr, "out of memory" ); |
1412 | memcpy(dst, filename, len + 1); |
1413 | |
1414 | auto &hint = g_file_mapping_hints[g_num_file_mapping_hints++]; |
1415 | hint.start = start; |
1416 | hint.end = end; |
1417 | hint.offset = offset; |
1418 | hint.filename = dst; |
1419 | } |
1420 | |
1421 | g_file_mapping_mu.Unlock(); |
1422 | return ret; |
1423 | } |
1424 | |
1425 | bool GetFileMappingHint(const void **start, const void **end, uint64_t *offset, |
1426 | const char **filename) { |
1427 | if (!g_file_mapping_mu.TryLock()) { |
1428 | return false; |
1429 | } |
1430 | bool found = false; |
1431 | for (int i = 0; i < g_num_file_mapping_hints; i++) { |
1432 | if (g_file_mapping_hints[i].start <= *start && |
1433 | *end <= g_file_mapping_hints[i].end) { |
1434 | // We assume that the start_address for the mapping is the base |
1435 | // address of the ELF section, but when [start_address,end_address) is |
1436 | // not strictly equal to [hint.start, hint.end), that assumption is |
1437 | // invalid. |
1438 | // |
1439 | // This uses the hint's start address (even though hint.start is not |
1440 | // necessarily equal to start_address) to ensure the correct |
1441 | // relocation is computed later. |
1442 | *start = g_file_mapping_hints[i].start; |
1443 | *end = g_file_mapping_hints[i].end; |
1444 | *offset = g_file_mapping_hints[i].offset; |
1445 | *filename = g_file_mapping_hints[i].filename; |
1446 | found = true; |
1447 | break; |
1448 | } |
1449 | } |
1450 | g_file_mapping_mu.Unlock(); |
1451 | return found; |
1452 | } |
1453 | |
1454 | } // namespace debugging_internal |
1455 | |
1456 | bool Symbolize(const void *pc, char *out, int out_size) { |
1457 | // Symbolization is very slow under tsan. |
1458 | ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN(); |
1459 | SAFE_ASSERT(out_size >= 0); |
1460 | debugging_internal::Symbolizer *s = debugging_internal::AllocateSymbolizer(); |
1461 | const char *name = s->GetSymbol(pc); |
1462 | bool ok = false; |
1463 | if (name != nullptr && out_size > 0) { |
1464 | strncpy(out, name, out_size); |
1465 | ok = true; |
1466 | if (out[out_size - 1] != '\0') { |
1467 | // strncpy() does not '\0' terminate when it truncates. Do so, with |
1468 | // trailing ellipsis. |
1469 | static constexpr char kEllipsis[] = "..." ; |
1470 | int ellipsis_size = |
1471 | std::min(implicit_cast<int>(strlen(kEllipsis)), out_size - 1); |
1472 | memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); |
1473 | out[out_size - 1] = '\0'; |
1474 | } |
1475 | } |
1476 | debugging_internal::FreeSymbolizer(s); |
1477 | ANNOTATE_IGNORE_READS_AND_WRITES_END(); |
1478 | return ok; |
1479 | } |
1480 | |
1481 | } // namespace absl |
1482 | |