Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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) elf_header; |
| 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) elf_header; |
| 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 GetSectionHeaderByType( |
| 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 num_headers_in_buf = 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) elf_header; |
| 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 section_header_offset = |
| 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 header_name[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 GetSectionHeaderByName(int fd, const char *name, size_t name_len, |
| 541 | ElfW(Shdr) * out) { |
| 542 | char header_name[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) elf_header; |
| 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 section_header_offset = |
| 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 | static const char *ComputeOffset(const char *base, ptrdiff_t offset) { |
| 628 | // Note: cast to uintptr_t to avoid undefined behavior when base evaluates to |
| 629 | // zero and offset is non-zero. |
| 630 | return reinterpret_cast<const char *>( |
| 631 | reinterpret_cast<uintptr_t>(base) + offset); |
| 632 | } |
| 633 | |
| 634 | // Read a symbol table and look for the symbol containing the |
| 635 | // pc. Iterate over symbols in a symbol table and look for the symbol |
| 636 | // containing "pc". If the symbol is found, and its name fits in |
| 637 | // out_size, the name is written into out and SYMBOL_FOUND is returned. |
| 638 | // If the name does not fit, truncated name is written into out, |
| 639 | // and SYMBOL_TRUNCATED is returned. Out is NUL-terminated. |
| 640 | // If the symbol is not found, SYMBOL_NOT_FOUND is returned; |
| 641 | // To keep stack consumption low, we would like this function to not get |
| 642 | // inlined. |
| 643 | static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( |
| 644 | const void *const pc, const int fd, char *out, int out_size, |
| 645 | ptrdiff_t relocation, const ElfW(Shdr) * strtab, const ElfW(Shdr) * symtab, |
| 646 | const ElfW(Shdr) * opd, char *tmp_buf, int tmp_buf_size) { |
| 647 | if (symtab == nullptr) { |
| 648 | return SYMBOL_NOT_FOUND; |
| 649 | } |
| 650 | |
| 651 | // Read multiple symbols at once to save read() calls. |
| 652 | ElfW(Sym) *buf = reinterpret_cast<ElfW(Sym) *>(tmp_buf); |
| 653 | const int buf_entries = tmp_buf_size / sizeof(buf[0]); |
| 654 | |
| 655 | const int num_symbols = symtab->sh_size / symtab->sh_entsize; |
| 656 | |
| 657 | // On platforms using an .opd section (PowerPC & IA64), a function symbol |
| 658 | // has the address of a function descriptor, which contains the real |
| 659 | // starting address. However, we do not always want to use the real |
| 660 | // starting address because we sometimes want to symbolize a function |
| 661 | // pointer into the .opd section, e.g. FindSymbol(&foo,...). |
| 662 | const bool pc_in_opd = |
| 663 | kPlatformUsesOPDSections && opd != nullptr && InSection(pc, opd); |
| 664 | const bool deref_function_descriptor_pointer = |
| 665 | kPlatformUsesOPDSections && opd != nullptr && !pc_in_opd; |
| 666 | |
| 667 | ElfW(Sym) best_match; |
| 668 | SafeMemZero(&best_match, sizeof(best_match)); |
| 669 | bool found_match = false; |
| 670 | for (int i = 0; i < num_symbols;) { |
| 671 | off_t offset = symtab->sh_offset + i * symtab->sh_entsize; |
| 672 | const int num_remaining_symbols = num_symbols - i; |
| 673 | const int entries_in_chunk = std::min(num_remaining_symbols, buf_entries); |
| 674 | const int bytes_in_chunk = entries_in_chunk * sizeof(buf[0]); |
| 675 | const ssize_t len = ReadFromOffset(fd, buf, bytes_in_chunk, offset); |
| 676 | SAFE_ASSERT(len % sizeof(buf[0]) == 0); |
| 677 | const ssize_t num_symbols_in_buf = len / sizeof(buf[0]); |
| 678 | SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk); |
| 679 | for (int j = 0; j < num_symbols_in_buf; ++j) { |
| 680 | const ElfW(Sym) &symbol = buf[j]; |
| 681 | |
| 682 | // For a DSO, a symbol address is relocated by the loading address. |
| 683 | // We keep the original address for opd redirection below. |
| 684 | const char *const original_start_address = |
| 685 | reinterpret_cast<const char *>(symbol.st_value); |
| 686 | const char *start_address = |
| 687 | ComputeOffset(original_start_address, relocation); |
| 688 | |
| 689 | if (deref_function_descriptor_pointer && |
| 690 | InSection(original_start_address, opd)) { |
| 691 | // The opd section is mapped into memory. Just dereference |
| 692 | // start_address to get the first double word, which points to the |
| 693 | // function entry. |
| 694 | start_address = *reinterpret_cast<const char *const *>(start_address); |
| 695 | } |
| 696 | |
| 697 | // If pc is inside the .opd section, it points to a function descriptor. |
| 698 | const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size; |
| 699 | const void *const end_address = ComputeOffset(start_address, size); |
| 700 | if (symbol.st_value != 0 && // Skip null value symbols. |
| 701 | symbol.st_shndx != 0 && // Skip undefined symbols. |
| 702 | #ifdef STT_TLS |
| 703 | ELF_ST_TYPE(symbol.st_info) != STT_TLS && // Skip thread-local data. |
| 704 | #endif // STT_TLS |
| 705 | ((start_address <= pc && pc < end_address) || |
| 706 | (start_address == pc && pc == end_address))) { |
| 707 | if (!found_match || ShouldPickFirstSymbol(symbol, best_match)) { |
| 708 | found_match = true; |
| 709 | best_match = symbol; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | i += num_symbols_in_buf; |
| 714 | } |
| 715 | |
| 716 | if (found_match) { |
| 717 | const size_t off = strtab->sh_offset + best_match.st_name; |
| 718 | const ssize_t n_read = ReadFromOffset(fd, out, out_size, off); |
| 719 | if (n_read <= 0) { |
| 720 | // This should never happen. |
| 721 | ABSL_RAW_LOG(WARNING, |
| 722 | "Unable to read from fd %d at offset %zu: n_read = %zd", fd, |
| 723 | off, n_read); |
| 724 | return SYMBOL_NOT_FOUND; |
| 725 | } |
| 726 | ABSL_RAW_CHECK(n_read <= out_size, "ReadFromOffset read too much data."); |
| 727 | |
| 728 | // strtab->sh_offset points into .strtab-like section that contains |
| 729 | // NUL-terminated strings: '\0foo\0barbaz\0...". |
| 730 | // |
| 731 | // sh_offset+st_name points to the start of symbol name, but we don't know |
| 732 | // how long the symbol is, so we try to read as much as we have space for, |
| 733 | // and usually over-read (i.e. there is a NUL somewhere before n_read). |
| 734 | if (memchr(out, '\0', n_read) == nullptr) { |
| 735 | // Either out_size was too small (n_read == out_size and no NUL), or |
| 736 | // we tried to read past the EOF (n_read < out_size) and .strtab is |
| 737 | // corrupt (missing terminating NUL; should never happen for valid ELF). |
| 738 | out[n_read - 1] = '\0'; |
| 739 | return SYMBOL_TRUNCATED; |
| 740 | } |
| 741 | return SYMBOL_FOUND; |
| 742 | } |
| 743 | |
| 744 | return SYMBOL_NOT_FOUND; |
| 745 | } |
| 746 | |
| 747 | // Get the symbol name of "pc" from the file pointed by "fd". Process |
| 748 | // both regular and dynamic symbol tables if necessary. |
| 749 | // See FindSymbol() comment for description of return value. |
| 750 | FindSymbolResult Symbolizer::GetSymbolFromObjectFile( |
| 751 | const ObjFile &obj, const void *const pc, const ptrdiff_t relocation, |
| 752 | char *out, int out_size, char *tmp_buf, int tmp_buf_size) { |
| 753 | ElfW(Shdr) symtab; |
| 754 | ElfW(Shdr) strtab; |
| 755 | ElfW(Shdr) opd; |
| 756 | ElfW(Shdr) *opd_ptr = nullptr; |
| 757 | |
| 758 | // On platforms using an .opd sections for function descriptor, read |
| 759 | // the section header. The .opd section is in data segment and should be |
| 760 | // loaded but we check that it is mapped just to be extra careful. |
| 761 | if (kPlatformUsesOPDSections) { |
| 762 | if (GetSectionHeaderByName(obj.fd, kOpdSectionName, |
| 763 | sizeof(kOpdSectionName) - 1, &opd) && |
| 764 | FindObjFile(reinterpret_cast<const char *>(opd.sh_addr) + relocation, |
| 765 | opd.sh_size) != nullptr) { |
| 766 | opd_ptr = &opd; |
| 767 | } else { |
| 768 | return SYMBOL_NOT_FOUND; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Consult a regular symbol table, then fall back to the dynamic symbol table. |
| 773 | for (const auto symbol_table_type : {SHT_SYMTAB, SHT_DYNSYM}) { |
| 774 | if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, |
| 775 | obj.elf_header.e_shoff, symbol_table_type, |
| 776 | &symtab, tmp_buf, tmp_buf_size)) { |
| 777 | continue; |
| 778 | } |
| 779 | if (!ReadFromOffsetExact( |
| 780 | obj.fd, &strtab, sizeof(strtab), |
| 781 | obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { |
| 782 | continue; |
| 783 | } |
| 784 | const FindSymbolResult rc = |
| 785 | FindSymbol(pc, obj.fd, out, out_size, relocation, &strtab, &symtab, |
| 786 | opd_ptr, tmp_buf, tmp_buf_size); |
| 787 | if (rc != SYMBOL_NOT_FOUND) { |
| 788 | return rc; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | return SYMBOL_NOT_FOUND; |
| 793 | } |
| 794 | |
| 795 | namespace { |
| 796 | // Thin wrapper around a file descriptor so that the file descriptor |
| 797 | // gets closed for sure. |
| 798 | class FileDescriptor { |
| 799 | public: |
| 800 | explicit FileDescriptor(int fd) : fd_(fd) {} |
| 801 | FileDescriptor(const FileDescriptor &) = delete; |
| 802 | FileDescriptor &operator=(const FileDescriptor &) = delete; |
| 803 | |
| 804 | ~FileDescriptor() { |
| 805 | if (fd_ >= 0) { |
| 806 | NO_INTR(close(fd_)); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | int get() const { return fd_; } |
| 811 | |
| 812 | private: |
| 813 | const int fd_; |
| 814 | }; |
| 815 | |
| 816 | // Helper class for reading lines from file. |
| 817 | // |
| 818 | // Note: we don't use ProcMapsIterator since the object is big (it has |
| 819 | // a 5k array member) and uses async-unsafe functions such as sscanf() |
| 820 | // and snprintf(). |
| 821 | class LineReader { |
| 822 | public: |
| 823 | explicit LineReader(int fd, char *buf, int buf_len) |
| 824 | : fd_(fd), |
| 825 | buf_len_(buf_len), |
| 826 | buf_(buf), |
| 827 | bol_(buf), |
| 828 | eol_(buf), |
| 829 | eod_(buf) {} |
| 830 | |
| 831 | LineReader(const LineReader &) = delete; |
| 832 | LineReader &operator=(const LineReader &) = delete; |
| 833 | |
| 834 | // Read '\n'-terminated line from file. On success, modify "bol" |
| 835 | // and "eol", then return true. Otherwise, return false. |
| 836 | // |
| 837 | // Note: if the last line doesn't end with '\n', the line will be |
| 838 | // dropped. It's an intentional behavior to make the code simple. |
| 839 | bool ReadLine(const char **bol, const char **eol) { |
| 840 | if (BufferIsEmpty()) { // First time. |
| 841 | const ssize_t num_bytes = ReadPersistent(fd_, buf_, buf_len_); |
| 842 | if (num_bytes <= 0) { // EOF or error. |
| 843 | return false; |
| 844 | } |
| 845 | eod_ = buf_ + num_bytes; |
| 846 | bol_ = buf_; |
| 847 | } else { |
| 848 | bol_ = eol_ + 1; // Advance to the next line in the buffer. |
| 849 | SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_". |
| 850 | if (!HasCompleteLine()) { |
| 851 | const int incomplete_line_length = eod_ - bol_; |
| 852 | // Move the trailing incomplete line to the beginning. |
| 853 | memmove(buf_, bol_, incomplete_line_length); |
| 854 | // Read text from file and append it. |
| 855 | char *const append_pos = buf_ + incomplete_line_length; |
| 856 | const int capacity_left = buf_len_ - incomplete_line_length; |
| 857 | const ssize_t num_bytes = |
| 858 | ReadPersistent(fd_, append_pos, capacity_left); |
| 859 | if (num_bytes <= 0) { // EOF or error. |
| 860 | return false; |
| 861 | } |
| 862 | eod_ = append_pos + num_bytes; |
| 863 | bol_ = buf_; |
| 864 | } |
| 865 | } |
| 866 | eol_ = FindLineFeed(); |
| 867 | if (eol_ == nullptr) { // '\n' not found. Malformed line. |
| 868 | return false; |
| 869 | } |
| 870 | *eol_ = '\0'; // Replace '\n' with '\0'. |
| 871 | |
| 872 | *bol = bol_; |
| 873 | *eol = eol_; |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | private: |
| 878 | char *FindLineFeed() const { |
| 879 | return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_)); |
| 880 | } |
| 881 | |
| 882 | bool BufferIsEmpty() const { return buf_ == eod_; } |
| 883 | |
| 884 | bool HasCompleteLine() const { |
| 885 | return !BufferIsEmpty() && FindLineFeed() != nullptr; |
| 886 | } |
| 887 | |
| 888 | const int fd_; |
| 889 | const int buf_len_; |
| 890 | char *const buf_; |
| 891 | char *bol_; |
| 892 | char *eol_; |
| 893 | const char *eod_; // End of data in "buf_". |
| 894 | }; |
| 895 | } // namespace |
| 896 | |
| 897 | // Place the hex number read from "start" into "*hex". The pointer to |
| 898 | // the first non-hex character or "end" is returned. |
| 899 | static const char *GetHex(const char *start, const char *end, |
| 900 | uint64_t *const value) { |
| 901 | uint64_t hex = 0; |
| 902 | const char *p; |
| 903 | for (p = start; p < end; ++p) { |
| 904 | int ch = *p; |
| 905 | if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || |
| 906 | (ch >= 'a' && ch <= 'f')) { |
| 907 | hex = (hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9); |
| 908 | } else { // Encountered the first non-hex character. |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | SAFE_ASSERT(p <= end); |
| 913 | *value = hex; |
| 914 | return p; |
| 915 | } |
| 916 | |
| 917 | static const char *GetHex(const char *start, const char *end, |
| 918 | const void **const addr) { |
| 919 | uint64_t hex = 0; |
| 920 | const char *p = GetHex(start, end, &hex); |
| 921 | *addr = reinterpret_cast<void *>(hex); |
| 922 | return p; |
| 923 | } |
| 924 | |
| 925 | // Normally we are only interested in "r?x" maps. |
| 926 | // On the PowerPC, function pointers point to descriptors in the .opd |
| 927 | // section. The descriptors themselves are not executable code, so |
| 928 | // we need to relax the check below to "r??". |
| 929 | static bool ShouldUseMapping(const char *const flags) { |
| 930 | return flags[0] == 'r' && (kPlatformUsesOPDSections || flags[2] == 'x'); |
| 931 | } |
| 932 | |
| 933 | // Read /proc/self/maps and run "callback" for each mmapped file found. If |
| 934 | // "callback" returns false, stop scanning and return true. Else continue |
| 935 | // scanning /proc/self/maps. Return true if no parse error is found. |
| 936 | static ABSL_ATTRIBUTE_NOINLINE bool ReadAddrMap( |
| 937 | bool (*callback)(const char *filename, const void *const start_addr, |
| 938 | const void *const end_addr, uint64_t offset, void *arg), |
| 939 | void *arg, void *tmp_buf, int tmp_buf_size) { |
| 940 | // Use /proc/self/task/<pid>/maps instead of /proc/self/maps. The latter |
| 941 | // requires kernel to stop all threads, and is significantly slower when there |
| 942 | // are 1000s of threads. |
| 943 | char maps_path[80]; |
| 944 | snprintf(maps_path, sizeof(maps_path), "/proc/self/task/%d/maps", getpid()); |
| 945 | |
| 946 | int maps_fd; |
| 947 | NO_INTR(maps_fd = open(maps_path, O_RDONLY)); |
| 948 | FileDescriptor wrapped_maps_fd(maps_fd); |
| 949 | if (wrapped_maps_fd.get() < 0) { |
| 950 | ABSL_RAW_LOG(WARNING, "%s: errno=%d", maps_path, errno); |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | // Iterate over maps and look for the map containing the pc. Then |
| 955 | // look into the symbol tables inside. |
| 956 | LineReader reader(wrapped_maps_fd.get(), static_cast<char *>(tmp_buf), |
| 957 | tmp_buf_size); |
| 958 | while (true) { |
| 959 | const char *cursor; |
| 960 | const char *eol; |
| 961 | if (!reader.ReadLine(&cursor, &eol)) { // EOF or malformed line. |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | const char *line = cursor; |
| 966 | const void *start_address; |
| 967 | // Start parsing line in /proc/self/maps. Here is an example: |
| 968 | // |
| 969 | // 08048000-0804c000 r-xp 00000000 08:01 2142121 /bin/cat |
| 970 | // |
| 971 | // We want start address (08048000), end address (0804c000), flags |
| 972 | // (r-xp) and file name (/bin/cat). |
| 973 | |
| 974 | // Read start address. |
| 975 | cursor = GetHex(cursor, eol, &start_address); |
| 976 | if (cursor == eol || *cursor != '-') { |
| 977 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s", line); |
| 978 | return false; |
| 979 | } |
| 980 | ++cursor; // Skip '-'. |
| 981 | |
| 982 | // Read end address. |
| 983 | const void *end_address; |
| 984 | cursor = GetHex(cursor, eol, &end_address); |
| 985 | if (cursor == eol || *cursor != ' ') { |
| 986 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps line: %s", line); |
| 987 | return false; |
| 988 | } |
| 989 | ++cursor; // Skip ' '. |
| 990 | |
| 991 | // Read flags. Skip flags until we encounter a space or eol. |
| 992 | const char *const flags_start = cursor; |
| 993 | while (cursor < eol && *cursor != ' ') { |
| 994 | ++cursor; |
| 995 | } |
| 996 | // We expect at least four letters for flags (ex. "r-xp"). |
| 997 | if (cursor == eol || cursor < flags_start + 4) { |
| 998 | ABSL_RAW_LOG(WARNING, "Corrupt /proc/self/maps: %s", line); |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | // Check flags. |
| 1003 | if (!ShouldUseMapping(flags_start)) { |
| 1004 | continue; // We skip this map. |
| 1005 | } |
| 1006 | ++cursor; // Skip ' '. |
| 1007 | |
| 1008 | // Read file offset. |
| 1009 | uint64_t offset; |
| 1010 | cursor = GetHex(cursor, eol, &offset); |
| 1011 | ++cursor; // Skip ' '. |
| 1012 | |
| 1013 | // Skip to file name. "cursor" now points to dev. We need to skip at least |
| 1014 | // two spaces for dev and inode. |
| 1015 | int num_spaces = 0; |
| 1016 | while (cursor < eol) { |
| 1017 | if (*cursor == ' ') { |
| 1018 | ++num_spaces; |
| 1019 | } else if (num_spaces >= 2) { |
| 1020 | // The first non-space character after skipping two spaces |
| 1021 | // is the beginning of the file name. |
| 1022 | break; |
| 1023 | } |
| 1024 | ++cursor; |
| 1025 | } |
| 1026 | |
| 1027 | // Check whether this entry corresponds to our hint table for the true |
| 1028 | // filename. |
| 1029 | bool hinted = |
| 1030 | GetFileMappingHint(&start_address, &end_address, &offset, &cursor); |
| 1031 | if (!hinted && (cursor == eol || cursor[0] == '[')) { |
| 1032 | // not an object file, typically [vdso] or [vsyscall] |
| 1033 | continue; |
| 1034 | } |
| 1035 | if (!callback(cursor, start_address, end_address, offset, arg)) break; |
| 1036 | } |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
| 1040 | // Find the objfile mapped in address region containing [addr, addr + len). |
| 1041 | ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) { |
| 1042 | for (int i = 0; i < 2; ++i) { |
| 1043 | if (!ok_) return nullptr; |
| 1044 | |
| 1045 | // Read /proc/self/maps if necessary |
| 1046 | if (!addr_map_read_) { |
| 1047 | addr_map_read_ = true; |
| 1048 | if (!ReadAddrMap(RegisterObjFile, this, tmp_buf_, TMP_BUF_SIZE)) { |
| 1049 | ok_ = false; |
| 1050 | return nullptr; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | int lo = 0; |
| 1055 | int hi = addr_map_.Size(); |
| 1056 | while (lo < hi) { |
| 1057 | int mid = (lo + hi) / 2; |
| 1058 | if (addr < addr_map_.At(mid)->end_addr) { |
| 1059 | hi = mid; |
| 1060 | } else { |
| 1061 | lo = mid + 1; |
| 1062 | } |
| 1063 | } |
| 1064 | if (lo != addr_map_.Size()) { |
| 1065 | ObjFile *obj = addr_map_.At(lo); |
| 1066 | SAFE_ASSERT(obj->end_addr > addr); |
| 1067 | if (addr >= obj->start_addr && |
| 1068 | reinterpret_cast<const char *>(addr) + len <= obj->end_addr) |
| 1069 | return obj; |
| 1070 | } |
| 1071 | |
| 1072 | // The address mapping may have changed since it was last read. Retry. |
| 1073 | ClearAddrMap(); |
| 1074 | } |
| 1075 | return nullptr; |
| 1076 | } |
| 1077 | |
| 1078 | void Symbolizer::ClearAddrMap() { |
| 1079 | for (int i = 0; i != addr_map_.Size(); i++) { |
| 1080 | ObjFile *o = addr_map_.At(i); |
| 1081 | base_internal::LowLevelAlloc::Free(o->filename); |
| 1082 | if (o->fd >= 0) { |
| 1083 | NO_INTR(close(o->fd)); |
| 1084 | } |
| 1085 | } |
| 1086 | addr_map_.Clear(); |
| 1087 | addr_map_read_ = false; |
| 1088 | } |
| 1089 | |
| 1090 | // Callback for ReadAddrMap to register objfiles in an in-memory table. |
| 1091 | bool Symbolizer::RegisterObjFile(const char *filename, |
| 1092 | const void *const start_addr, |
| 1093 | const void *const end_addr, uint64_t offset, |
| 1094 | void *arg) { |
| 1095 | Symbolizer *impl = static_cast<Symbolizer *>(arg); |
| 1096 | |
| 1097 | // Files are supposed to be added in the increasing address order. Make |
| 1098 | // sure that's the case. |
| 1099 | int addr_map_size = impl->addr_map_.Size(); |
| 1100 | if (addr_map_size != 0) { |
| 1101 | ObjFile *old = impl->addr_map_.At(addr_map_size - 1); |
| 1102 | if (old->end_addr > end_addr) { |
| 1103 | ABSL_RAW_LOG(ERROR, |
| 1104 | "Unsorted addr map entry: 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR |
| 1105 | ": %s", |
| 1106 | reinterpret_cast<uintptr_t>(end_addr), filename, |
| 1107 | reinterpret_cast<uintptr_t>(old->end_addr), old->filename); |
| 1108 | return true; |
| 1109 | } else if (old->end_addr == end_addr) { |
| 1110 | // The same entry appears twice. This sometimes happens for [vdso]. |
| 1111 | if (old->start_addr != start_addr || |
| 1112 | strcmp(old->filename, filename) != 0) { |
| 1113 | ABSL_RAW_LOG(ERROR, |
| 1114 | "Duplicate addr 0x%" PRIxPTR ": %s <-> 0x%" PRIxPTR ": %s", |
| 1115 | reinterpret_cast<uintptr_t>(end_addr), filename, |
| 1116 | reinterpret_cast<uintptr_t>(old->end_addr), old->filename); |
| 1117 | } |
| 1118 | return true; |
| 1119 | } |
| 1120 | } |
| 1121 | ObjFile *obj = impl->addr_map_.Add(); |
| 1122 | obj->filename = impl->CopyString(filename); |
| 1123 | obj->start_addr = start_addr; |
| 1124 | obj->end_addr = end_addr; |
| 1125 | obj->offset = offset; |
| 1126 | obj->elf_type = -1; // filled on demand |
| 1127 | obj->fd = -1; // opened on demand |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | // This function wraps the Demangle function to provide an interface |
| 1132 | // where the input symbol is demangled in-place. |
| 1133 | // To keep stack consumption low, we would like this function to not |
| 1134 | // get inlined. |
| 1135 | static ABSL_ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size, |
| 1136 | char *tmp_buf, |
| 1137 | int tmp_buf_size) { |
| 1138 | if (Demangle(out, tmp_buf, tmp_buf_size)) { |
| 1139 | // Demangling succeeded. Copy to out if the space allows. |
| 1140 | int len = strlen(tmp_buf); |
| 1141 | if (len + 1 <= out_size) { // +1 for '\0'. |
| 1142 | SAFE_ASSERT(len < tmp_buf_size); |
| 1143 | memmove(out, tmp_buf, len + 1); |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | SymbolCacheLine *Symbolizer::GetCacheLine(const void *const pc) { |
| 1149 | uintptr_t pc0 = reinterpret_cast<uintptr_t>(pc); |
| 1150 | pc0 >>= 3; // drop the low 3 bits |
| 1151 | |
| 1152 | // Shuffle bits. |
| 1153 | pc0 ^= (pc0 >> 6) ^ (pc0 >> 12) ^ (pc0 >> 18); |
| 1154 | return &symbol_cache_[pc0 % SYMBOL_CACHE_LINES]; |
| 1155 | } |
| 1156 | |
| 1157 | void Symbolizer::AgeSymbols(SymbolCacheLine *line) { |
| 1158 | for (uint32_t &age : line->age) { |
| 1159 | ++age; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | const char *Symbolizer::FindSymbolInCache(const void *const pc) { |
| 1164 | if (pc == nullptr) return nullptr; |
| 1165 | |
| 1166 | SymbolCacheLine *line = GetCacheLine(pc); |
| 1167 | for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) { |
| 1168 | if (line->pc[i] == pc) { |
| 1169 | AgeSymbols(line); |
| 1170 | line->age[i] = 0; |
| 1171 | return line->name[i]; |
| 1172 | } |
| 1173 | } |
| 1174 | return nullptr; |
| 1175 | } |
| 1176 | |
| 1177 | const char *Symbolizer::InsertSymbolInCache(const void *const pc, |
| 1178 | const char *name) { |
| 1179 | SAFE_ASSERT(pc != nullptr); |
| 1180 | |
| 1181 | SymbolCacheLine *line = GetCacheLine(pc); |
| 1182 | uint32_t max_age = 0; |
| 1183 | int oldest_index = -1; |
| 1184 | for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) { |
| 1185 | if (line->pc[i] == nullptr) { |
| 1186 | AgeSymbols(line); |
| 1187 | line->pc[i] = pc; |
| 1188 | line->name[i] = CopyString(name); |
| 1189 | line->age[i] = 0; |
| 1190 | return line->name[i]; |
| 1191 | } |
| 1192 | if (line->age[i] >= max_age) { |
| 1193 | max_age = line->age[i]; |
| 1194 | oldest_index = i; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | AgeSymbols(line); |
| 1199 | ABSL_RAW_CHECK(oldest_index >= 0, "Corrupt cache"); |
| 1200 | base_internal::LowLevelAlloc::Free(line->name[oldest_index]); |
| 1201 | line->pc[oldest_index] = pc; |
| 1202 | line->name[oldest_index] = CopyString(name); |
| 1203 | line->age[oldest_index] = 0; |
| 1204 | return line->name[oldest_index]; |
| 1205 | } |
| 1206 | |
| 1207 | static void MaybeOpenFdFromSelfExe(ObjFile *obj) { |
| 1208 | if (memcmp(obj->start_addr, ELFMAG, SELFMAG) != 0) { |
| 1209 | return; |
| 1210 | } |
| 1211 | int fd = open("/proc/self/exe", O_RDONLY); |
| 1212 | if (fd == -1) { |
| 1213 | return; |
| 1214 | } |
| 1215 | // Verify that contents of /proc/self/exe matches in-memory image of |
| 1216 | // the binary. This can fail if the "deleted" binary is in fact not |
| 1217 | // the main executable, or for binaries that have the first PT_LOAD |
| 1218 | // segment smaller than 4K. We do it in four steps so that the |
| 1219 | // buffer is smaller and we don't consume too much stack space. |
| 1220 | const char *mem = reinterpret_cast<const char *>(obj->start_addr); |
| 1221 | for (int i = 0; i < 4; ++i) { |
| 1222 | char buf[1024]; |
| 1223 | ssize_t n = read(fd, buf, sizeof(buf)); |
| 1224 | if (n != sizeof(buf) || memcmp(buf, mem, sizeof(buf)) != 0) { |
| 1225 | close(fd); |
| 1226 | return; |
| 1227 | } |
| 1228 | mem += sizeof(buf); |
| 1229 | } |
| 1230 | obj->fd = fd; |
| 1231 | } |
| 1232 | |
| 1233 | static bool MaybeInitializeObjFile(ObjFile *obj) { |
| 1234 | if (obj->fd < 0) { |
| 1235 | obj->fd = open(obj->filename, O_RDONLY); |
| 1236 | |
| 1237 | if (obj->fd < 0) { |
| 1238 | // Getting /proc/self/exe here means that we were hinted. |
| 1239 | if (strcmp(obj->filename, "/proc/self/exe") == 0) { |
| 1240 | // /proc/self/exe may be inaccessible (due to setuid, etc.), so try |
| 1241 | // accessing the binary via argv0. |
| 1242 | if (argv0_value != nullptr) { |
| 1243 | obj->fd = open(argv0_value, O_RDONLY); |
| 1244 | } |
| 1245 | } else { |
| 1246 | MaybeOpenFdFromSelfExe(obj); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (obj->fd < 0) { |
| 1251 | ABSL_RAW_LOG(WARNING, "%s: open failed: errno=%d", obj->filename, errno); |
| 1252 | return false; |
| 1253 | } |
| 1254 | obj->elf_type = FileGetElfType(obj->fd); |
| 1255 | if (obj->elf_type < 0) { |
| 1256 | ABSL_RAW_LOG(WARNING, "%s: wrong elf type: %d", obj->filename, |
| 1257 | obj->elf_type); |
| 1258 | return false; |
| 1259 | } |
| 1260 | |
| 1261 | if (!ReadFromOffsetExact(obj->fd, &obj->elf_header, sizeof(obj->elf_header), |
| 1262 | 0)) { |
| 1263 | ABSL_RAW_LOG(WARNING, "%s: failed to read elf header", obj->filename); |
| 1264 | return false; |
| 1265 | } |
| 1266 | } |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | // The implementation of our symbolization routine. If it |
| 1271 | // successfully finds the symbol containing "pc" and obtains the |
| 1272 | // symbol name, returns pointer to that symbol. Otherwise, returns nullptr. |
| 1273 | // If any symbol decorators have been installed via InstallSymbolDecorator(), |
| 1274 | // they are called here as well. |
| 1275 | // To keep stack consumption low, we would like this function to not |
| 1276 | // get inlined. |
| 1277 | const char *Symbolizer::GetSymbol(const void *const pc) { |
| 1278 | const char *entry = FindSymbolInCache(pc); |
| 1279 | if (entry != nullptr) { |
| 1280 | return entry; |
| 1281 | } |
| 1282 | symbol_buf_[0] = '\0'; |
| 1283 | |
| 1284 | ObjFile *const obj = FindObjFile(pc, 1); |
| 1285 | ptrdiff_t relocation = 0; |
| 1286 | int fd = -1; |
| 1287 | if (obj != nullptr) { |
| 1288 | if (MaybeInitializeObjFile(obj)) { |
| 1289 | if (obj->elf_type == ET_DYN && |
| 1290 | reinterpret_cast<uint64_t>(obj->start_addr) >= obj->offset) { |
| 1291 | // This object was relocated. |
| 1292 | // |
| 1293 | // For obj->offset > 0, adjust the relocation since a mapping at offset |
| 1294 | // X in the file will have a start address of [true relocation]+X. |
| 1295 | relocation = reinterpret_cast<ptrdiff_t>(obj->start_addr) - obj->offset; |
| 1296 | } |
| 1297 | |
| 1298 | fd = obj->fd; |
| 1299 | } |
| 1300 | if (GetSymbolFromObjectFile(*obj, pc, relocation, symbol_buf_, |
| 1301 | sizeof(symbol_buf_), tmp_buf_, |
| 1302 | sizeof(tmp_buf_)) == SYMBOL_FOUND) { |
| 1303 | // Only try to demangle the symbol name if it fit into symbol_buf_. |
| 1304 | DemangleInplace(symbol_buf_, sizeof(symbol_buf_), tmp_buf_, |
| 1305 | sizeof(tmp_buf_)); |
| 1306 | } |
| 1307 | } else { |
| 1308 | #if ABSL_HAVE_VDSO_SUPPORT |
| 1309 | VDSOSupport vdso; |
| 1310 | if (vdso.IsPresent()) { |
| 1311 | VDSOSupport::SymbolInfo symbol_info; |
| 1312 | if (vdso.LookupSymbolByAddress(pc, &symbol_info)) { |
| 1313 | // All VDSO symbols are known to be short. |
| 1314 | size_t len = strlen(symbol_info.name); |
| 1315 | ABSL_RAW_CHECK(len + 1 < sizeof(symbol_buf_), |
| 1316 | "VDSO symbol unexpectedly long"); |
| 1317 | memcpy(symbol_buf_, symbol_info.name, len + 1); |
| 1318 | } |
| 1319 | } |
| 1320 | #endif |
| 1321 | } |
| 1322 | |
| 1323 | if (g_decorators_mu.TryLock()) { |
| 1324 | if (g_num_decorators > 0) { |
| 1325 | SymbolDecoratorArgs decorator_args = { |
| 1326 | pc, relocation, fd, symbol_buf_, sizeof(symbol_buf_), |
| 1327 | tmp_buf_, sizeof(tmp_buf_), nullptr}; |
| 1328 | for (int i = 0; i < g_num_decorators; ++i) { |
| 1329 | decorator_args.arg = g_decorators[i].arg; |
| 1330 | g_decorators[i].fn(&decorator_args); |
| 1331 | } |
| 1332 | } |
| 1333 | g_decorators_mu.Unlock(); |
| 1334 | } |
| 1335 | if (symbol_buf_[0] == '\0') { |
| 1336 | return nullptr; |
| 1337 | } |
| 1338 | symbol_buf_[sizeof(symbol_buf_) - 1] = '\0'; // Paranoia. |
| 1339 | return InsertSymbolInCache(pc, symbol_buf_); |
| 1340 | } |
| 1341 | |
| 1342 | bool RemoveAllSymbolDecorators(void) { |
| 1343 | if (!g_decorators_mu.TryLock()) { |
| 1344 | // Someone else is using decorators. Get out. |
| 1345 | return false; |
| 1346 | } |
| 1347 | g_num_decorators = 0; |
| 1348 | g_decorators_mu.Unlock(); |
| 1349 | return true; |
| 1350 | } |
| 1351 | |
| 1352 | bool RemoveSymbolDecorator(int ticket) { |
| 1353 | if (!g_decorators_mu.TryLock()) { |
| 1354 | // Someone else is using decorators. Get out. |
| 1355 | return false; |
| 1356 | } |
| 1357 | for (int i = 0; i < g_num_decorators; ++i) { |
| 1358 | if (g_decorators[i].ticket == ticket) { |
| 1359 | while (i < g_num_decorators - 1) { |
| 1360 | g_decorators[i] = g_decorators[i + 1]; |
| 1361 | ++i; |
| 1362 | } |
| 1363 | g_num_decorators = i; |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | g_decorators_mu.Unlock(); |
| 1368 | return true; // Decorator is known to be removed. |
| 1369 | } |
| 1370 | |
| 1371 | int InstallSymbolDecorator(SymbolDecorator decorator, void *arg) { |
| 1372 | static int ticket = 0; |
| 1373 | |
| 1374 | if (!g_decorators_mu.TryLock()) { |
| 1375 | // Someone else is using decorators. Get out. |
| 1376 | return false; |
| 1377 | } |
| 1378 | int ret = ticket; |
| 1379 | if (g_num_decorators >= kMaxDecorators) { |
| 1380 | ret = -1; |
| 1381 | } else { |
| 1382 | g_decorators[g_num_decorators] = {decorator, arg, ticket++}; |
| 1383 | ++g_num_decorators; |
| 1384 | } |
| 1385 | g_decorators_mu.Unlock(); |
| 1386 | return ret; |
| 1387 | } |
| 1388 | |
| 1389 | bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset, |
| 1390 | const char *filename) { |
| 1391 | SAFE_ASSERT(start <= end); |
| 1392 | SAFE_ASSERT(filename != nullptr); |
| 1393 | |
| 1394 | InitSigSafeArena(); |
| 1395 | |
| 1396 | if (!g_file_mapping_mu.TryLock()) { |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | bool ret = true; |
| 1401 | if (g_num_file_mapping_hints >= kMaxFileMappingHints) { |
| 1402 | ret = false; |
| 1403 | } else { |
| 1404 | // TODO(ckennelly): Move this into a std::string copy routine. |
| 1405 | int len = strlen(filename); |
| 1406 | char *dst = static_cast<char *>( |
| 1407 | base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); |
| 1408 | ABSL_RAW_CHECK(dst != nullptr, "out of memory"); |
| 1409 | memcpy(dst, filename, len + 1); |
| 1410 | |
| 1411 | auto &hint = g_file_mapping_hints[g_num_file_mapping_hints++]; |
| 1412 | hint.start = start; |
| 1413 | hint.end = end; |
| 1414 | hint.offset = offset; |
| 1415 | hint.filename = dst; |
| 1416 | } |
| 1417 | |
| 1418 | g_file_mapping_mu.Unlock(); |
| 1419 | return ret; |
| 1420 | } |
| 1421 | |
| 1422 | bool GetFileMappingHint(const void **start, const void **end, uint64_t *offset, |
| 1423 | const char **filename) { |
| 1424 | if (!g_file_mapping_mu.TryLock()) { |
| 1425 | return false; |
| 1426 | } |
| 1427 | bool found = false; |
| 1428 | for (int i = 0; i < g_num_file_mapping_hints; i++) { |
| 1429 | if (g_file_mapping_hints[i].start <= *start && |
| 1430 | *end <= g_file_mapping_hints[i].end) { |
| 1431 | // We assume that the start_address for the mapping is the base |
| 1432 | // address of the ELF section, but when [start_address,end_address) is |
| 1433 | // not strictly equal to [hint.start, hint.end), that assumption is |
| 1434 | // invalid. |
| 1435 | // |
| 1436 | // This uses the hint's start address (even though hint.start is not |
| 1437 | // necessarily equal to start_address) to ensure the correct |
| 1438 | // relocation is computed later. |
| 1439 | *start = g_file_mapping_hints[i].start; |
| 1440 | *end = g_file_mapping_hints[i].end; |
| 1441 | *offset = g_file_mapping_hints[i].offset; |
| 1442 | *filename = g_file_mapping_hints[i].filename; |
| 1443 | found = true; |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | g_file_mapping_mu.Unlock(); |
| 1448 | return found; |
| 1449 | } |
| 1450 | |
| 1451 | } // namespace debugging_internal |
| 1452 | |
| 1453 | bool Symbolize(const void *pc, char *out, int out_size) { |
| 1454 | // Symbolization is very slow under tsan. |
| 1455 | ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN(); |
| 1456 | SAFE_ASSERT(out_size >= 0); |
| 1457 | debugging_internal::Symbolizer *s = debugging_internal::AllocateSymbolizer(); |
| 1458 | const char *name = s->GetSymbol(pc); |
| 1459 | bool ok = false; |
| 1460 | if (name != nullptr && out_size > 0) { |
| 1461 | strncpy(out, name, out_size); |
| 1462 | ok = true; |
| 1463 | if (out[out_size - 1] != '\0') { |
| 1464 | // strncpy() does not '\0' terminate when it truncates. Do so, with |
| 1465 | // trailing ellipsis. |
| 1466 | static constexpr char kEllipsis[] = "..."; |
| 1467 | int ellipsis_size = |
| 1468 | std::min(implicit_cast<int>(strlen(kEllipsis)), out_size - 1); |
| 1469 | memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); |
| 1470 | out[out_size - 1] = '\0'; |
| 1471 | } |
| 1472 | } |
| 1473 | debugging_internal::FreeSymbolizer(s); |
| 1474 | ANNOTATE_IGNORE_READS_AND_WRITES_END(); |
| 1475 | return ok; |
| 1476 | } |
| 1477 | |
| 1478 | } // namespace absl |