* config/i386/i386.md (fmodxf3): Enable for flag_finite_math_only only.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_symbolizer_posix_libcdep.cc
blob4f30225a4c897ce3250b9ad3f89af3b3882364aa
1 //===-- sanitizer_symbolizer_posix_libcdep.cc -----------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 // POSIX-specific implementation of symbolizer parts.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
14 #if SANITIZER_POSIX
15 #include "sanitizer_allocator_internal.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_linux.h"
20 #include "sanitizer_placement_new.h"
21 #include "sanitizer_procmaps.h"
22 #include "sanitizer_symbolizer.h"
23 #include "sanitizer_symbolizer_libbacktrace.h"
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
30 // C++ demangling function, as required by Itanium C++ ABI. This is weak,
31 // because we do not require a C++ ABI library to be linked to a program
32 // using sanitizers; if it's not present, we'll just use the mangled name.
33 namespace __cxxabiv1 {
34 extern "C" SANITIZER_WEAK_ATTRIBUTE
35 char *__cxa_demangle(const char *mangled, char *buffer,
36 size_t *length, int *status);
39 namespace __sanitizer {
41 // Attempts to demangle the name via __cxa_demangle from __cxxabiv1.
42 static const char *DemangleCXXABI(const char *name) {
43 // FIXME: __cxa_demangle aggressively insists on allocating memory.
44 // There's not much we can do about that, short of providing our
45 // own demangler (libc++abi's implementation could be adapted so that
46 // it does not allocate). For now, we just call it anyway, and we leak
47 // the returned value.
48 if (__cxxabiv1::__cxa_demangle)
49 if (const char *demangled_name =
50 __cxxabiv1::__cxa_demangle(name, 0, 0, 0))
51 return demangled_name;
53 return name;
56 // Extracts the prefix of "str" that consists of any characters not
57 // present in "delims" string, and copies this prefix to "result", allocating
58 // space for it.
59 // Returns a pointer to "str" after skipping extracted prefix and first
60 // delimiter char.
61 static const char *ExtractToken(const char *str, const char *delims,
62 char **result) {
63 uptr prefix_len = internal_strcspn(str, delims);
64 *result = (char*)InternalAlloc(prefix_len + 1);
65 internal_memcpy(*result, str, prefix_len);
66 (*result)[prefix_len] = '\0';
67 const char *prefix_end = str + prefix_len;
68 if (*prefix_end != '\0') prefix_end++;
69 return prefix_end;
72 // Same as ExtractToken, but converts extracted token to integer.
73 static const char *ExtractInt(const char *str, const char *delims,
74 int *result) {
75 char *buff;
76 const char *ret = ExtractToken(str, delims, &buff);
77 if (buff != 0) {
78 *result = (int)internal_atoll(buff);
80 InternalFree(buff);
81 return ret;
84 static const char *ExtractUptr(const char *str, const char *delims,
85 uptr *result) {
86 char *buff;
87 const char *ret = ExtractToken(str, delims, &buff);
88 if (buff != 0) {
89 *result = (uptr)internal_atoll(buff);
91 InternalFree(buff);
92 return ret;
95 class ExternalSymbolizerInterface {
96 public:
97 // Can't declare pure virtual functions in sanitizer runtimes:
98 // __cxa_pure_virtual might be unavailable.
99 virtual char *SendCommand(bool is_data, const char *module_name,
100 uptr module_offset) {
101 UNIMPLEMENTED();
105 // SymbolizerProcess encapsulates communication between the tool and
106 // external symbolizer program, running in a different subprocess.
107 // SymbolizerProcess may not be used from two threads simultaneously.
108 class SymbolizerProcess : public ExternalSymbolizerInterface {
109 public:
110 explicit SymbolizerProcess(const char *path)
111 : path_(path),
112 input_fd_(kInvalidFd),
113 output_fd_(kInvalidFd),
114 times_restarted_(0),
115 failed_to_start_(false),
116 reported_invalid_path_(false) {
117 CHECK(path_);
118 CHECK_NE(path_[0], '\0');
121 char *SendCommand(bool is_data, const char *module_name, uptr module_offset) {
122 for (; times_restarted_ < kMaxTimesRestarted; times_restarted_++) {
123 // Start or restart symbolizer if we failed to send command to it.
124 if (char *res = SendCommandImpl(is_data, module_name, module_offset))
125 return res;
126 Restart();
128 if (!failed_to_start_) {
129 Report("WARNING: Failed to use and restart external symbolizer!\n");
130 failed_to_start_ = true;
132 return 0;
135 private:
136 bool Restart() {
137 if (input_fd_ != kInvalidFd)
138 internal_close(input_fd_);
139 if (output_fd_ != kInvalidFd)
140 internal_close(output_fd_);
141 return StartSymbolizerSubprocess();
144 char *SendCommandImpl(bool is_data, const char *module_name,
145 uptr module_offset) {
146 if (input_fd_ == kInvalidFd || output_fd_ == kInvalidFd)
147 return 0;
148 CHECK(module_name);
149 if (!RenderInputCommand(buffer_, kBufferSize, is_data, module_name,
150 module_offset))
151 return 0;
152 if (!writeToSymbolizer(buffer_, internal_strlen(buffer_)))
153 return 0;
154 if (!readFromSymbolizer(buffer_, kBufferSize))
155 return 0;
156 return buffer_;
159 bool readFromSymbolizer(char *buffer, uptr max_length) {
160 if (max_length == 0)
161 return true;
162 uptr read_len = 0;
163 while (true) {
164 uptr just_read = internal_read(input_fd_, buffer + read_len,
165 max_length - read_len - 1);
166 // We can't read 0 bytes, as we don't expect external symbolizer to close
167 // its stdout.
168 if (just_read == 0 || just_read == (uptr)-1) {
169 Report("WARNING: Can't read from symbolizer at fd %d\n", input_fd_);
170 return false;
172 read_len += just_read;
173 if (ReachedEndOfOutput(buffer, read_len))
174 break;
176 buffer[read_len] = '\0';
177 return true;
180 bool writeToSymbolizer(const char *buffer, uptr length) {
181 if (length == 0)
182 return true;
183 uptr write_len = internal_write(output_fd_, buffer, length);
184 if (write_len == 0 || write_len == (uptr)-1) {
185 Report("WARNING: Can't write to symbolizer at fd %d\n", output_fd_);
186 return false;
188 return true;
191 bool StartSymbolizerSubprocess() {
192 if (!FileExists(path_)) {
193 if (!reported_invalid_path_) {
194 Report("WARNING: invalid path to external symbolizer!\n");
195 reported_invalid_path_ = true;
197 return false;
200 int *infd = NULL;
201 int *outfd = NULL;
202 // The client program may close its stdin and/or stdout and/or stderr
203 // thus allowing socketpair to reuse file descriptors 0, 1 or 2.
204 // In this case the communication between the forked processes may be
205 // broken if either the parent or the child tries to close or duplicate
206 // these descriptors. The loop below produces two pairs of file
207 // descriptors, each greater than 2 (stderr).
208 int sock_pair[5][2];
209 for (int i = 0; i < 5; i++) {
210 if (pipe(sock_pair[i]) == -1) {
211 for (int j = 0; j < i; j++) {
212 internal_close(sock_pair[j][0]);
213 internal_close(sock_pair[j][1]);
215 Report("WARNING: Can't create a socket pair to start "
216 "external symbolizer (errno: %d)\n", errno);
217 return false;
218 } else if (sock_pair[i][0] > 2 && sock_pair[i][1] > 2) {
219 if (infd == NULL) {
220 infd = sock_pair[i];
221 } else {
222 outfd = sock_pair[i];
223 for (int j = 0; j < i; j++) {
224 if (sock_pair[j] == infd) continue;
225 internal_close(sock_pair[j][0]);
226 internal_close(sock_pair[j][1]);
228 break;
232 CHECK(infd);
233 CHECK(outfd);
235 // Real fork() may call user callbacks registered with pthread_atfork().
236 int pid = internal_fork();
237 if (pid == -1) {
238 // Fork() failed.
239 internal_close(infd[0]);
240 internal_close(infd[1]);
241 internal_close(outfd[0]);
242 internal_close(outfd[1]);
243 Report("WARNING: failed to fork external symbolizer "
244 " (errno: %d)\n", errno);
245 return false;
246 } else if (pid == 0) {
247 // Child subprocess.
248 internal_close(STDOUT_FILENO);
249 internal_close(STDIN_FILENO);
250 internal_dup2(outfd[0], STDIN_FILENO);
251 internal_dup2(infd[1], STDOUT_FILENO);
252 internal_close(outfd[0]);
253 internal_close(outfd[1]);
254 internal_close(infd[0]);
255 internal_close(infd[1]);
256 for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--)
257 internal_close(fd);
258 ExecuteWithDefaultArgs(path_);
259 internal__exit(1);
262 // Continue execution in parent process.
263 internal_close(outfd[0]);
264 internal_close(infd[1]);
265 input_fd_ = infd[0];
266 output_fd_ = outfd[1];
268 // Check that symbolizer subprocess started successfully.
269 int pid_status;
270 SleepForMillis(kSymbolizerStartupTimeMillis);
271 int exited_pid = waitpid(pid, &pid_status, WNOHANG);
272 if (exited_pid != 0) {
273 // Either waitpid failed, or child has already exited.
274 Report("WARNING: external symbolizer didn't start up correctly!\n");
275 return false;
278 return true;
281 virtual bool RenderInputCommand(char *buffer, uptr max_length, bool is_data,
282 const char *module_name,
283 uptr module_offset) const {
284 UNIMPLEMENTED();
287 virtual bool ReachedEndOfOutput(const char *buffer, uptr length) const {
288 UNIMPLEMENTED();
291 virtual void ExecuteWithDefaultArgs(const char *path_to_binary) const {
292 UNIMPLEMENTED();
295 const char *path_;
296 int input_fd_;
297 int output_fd_;
299 static const uptr kBufferSize = 16 * 1024;
300 char buffer_[kBufferSize];
302 static const uptr kMaxTimesRestarted = 5;
303 static const int kSymbolizerStartupTimeMillis = 10;
304 uptr times_restarted_;
305 bool failed_to_start_;
306 bool reported_invalid_path_;
309 // For now we assume the following protocol:
310 // For each request of the form
311 // <module_name> <module_offset>
312 // passed to STDIN, external symbolizer prints to STDOUT response:
313 // <function_name>
314 // <file_name>:<line_number>:<column_number>
315 // <function_name>
316 // <file_name>:<line_number>:<column_number>
317 // ...
318 // <empty line>
319 class LLVMSymbolizerProcess : public SymbolizerProcess {
320 public:
321 explicit LLVMSymbolizerProcess(const char *path) : SymbolizerProcess(path) {}
323 private:
324 bool RenderInputCommand(char *buffer, uptr max_length, bool is_data,
325 const char *module_name, uptr module_offset) const {
326 internal_snprintf(buffer, max_length, "%s\"%s\" 0x%zx\n",
327 is_data ? "DATA " : "", module_name, module_offset);
328 return true;
331 bool ReachedEndOfOutput(const char *buffer, uptr length) const {
332 // Empty line marks the end of llvm-symbolizer output.
333 return length >= 2 && buffer[length - 1] == '\n' &&
334 buffer[length - 2] == '\n';
337 void ExecuteWithDefaultArgs(const char *path_to_binary) const {
338 #if defined(__x86_64__)
339 const char* const kSymbolizerArch = "--default-arch=x86_64";
340 #elif defined(__i386__)
341 const char* const kSymbolizerArch = "--default-arch=i386";
342 #elif defined(__powerpc64__)
343 const char* const kSymbolizerArch = "--default-arch=powerpc64";
344 #else
345 const char* const kSymbolizerArch = "--default-arch=unknown";
346 #endif
347 execl(path_to_binary, path_to_binary, kSymbolizerArch, (char *)0);
351 class Addr2LineProcess : public SymbolizerProcess {
352 public:
353 Addr2LineProcess(const char *path, const char *module_name)
354 : SymbolizerProcess(path), module_name_(internal_strdup(module_name)) {}
356 const char *module_name() const { return module_name_; }
358 private:
359 bool RenderInputCommand(char *buffer, uptr max_length, bool is_data,
360 const char *module_name, uptr module_offset) const {
361 if (is_data)
362 return false;
363 CHECK_EQ(0, internal_strcmp(module_name, module_name_));
364 internal_snprintf(buffer, max_length, "0x%zx\n", module_offset);
365 return true;
368 bool ReachedEndOfOutput(const char *buffer, uptr length) const {
369 // Output should consist of two lines.
370 int num_lines = 0;
371 for (uptr i = 0; i < length; ++i) {
372 if (buffer[i] == '\n')
373 num_lines++;
374 if (num_lines >= 2)
375 return true;
377 return false;
380 void ExecuteWithDefaultArgs(const char *path_to_binary) const {
381 execl(path_to_binary, path_to_binary, "-Cfe", module_name_, (char *)0);
384 const char *module_name_; // Owned, leaked.
387 class Addr2LinePool : public ExternalSymbolizerInterface {
388 public:
389 explicit Addr2LinePool(const char *addr2line_path,
390 LowLevelAllocator *allocator)
391 : addr2line_path_(addr2line_path), allocator_(allocator),
392 addr2line_pool_(16) {}
394 char *SendCommand(bool is_data, const char *module_name, uptr module_offset) {
395 if (is_data)
396 return 0;
397 Addr2LineProcess *addr2line = 0;
398 for (uptr i = 0; i < addr2line_pool_.size(); ++i) {
399 if (0 ==
400 internal_strcmp(module_name, addr2line_pool_[i]->module_name())) {
401 addr2line = addr2line_pool_[i];
402 break;
405 if (!addr2line) {
406 addr2line =
407 new(*allocator_) Addr2LineProcess(addr2line_path_, module_name);
408 addr2line_pool_.push_back(addr2line);
410 return addr2line->SendCommand(is_data, module_name, module_offset);
413 private:
414 const char *addr2line_path_;
415 LowLevelAllocator *allocator_;
416 InternalMmapVector<Addr2LineProcess*> addr2line_pool_;
419 #if SANITIZER_SUPPORTS_WEAK_HOOKS
420 extern "C" {
421 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
422 bool __sanitizer_symbolize_code(const char *ModuleName, u64 ModuleOffset,
423 char *Buffer, int MaxLength);
424 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
425 bool __sanitizer_symbolize_data(const char *ModuleName, u64 ModuleOffset,
426 char *Buffer, int MaxLength);
427 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
428 void __sanitizer_symbolize_flush();
429 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
430 int __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
431 int MaxLength);
432 } // extern "C"
434 class InternalSymbolizer {
435 public:
436 typedef bool (*SanitizerSymbolizeFn)(const char*, u64, char*, int);
438 static InternalSymbolizer *get(LowLevelAllocator *alloc) {
439 if (__sanitizer_symbolize_code != 0 &&
440 __sanitizer_symbolize_data != 0) {
441 return new(*alloc) InternalSymbolizer();
443 return 0;
446 char *SendCommand(bool is_data, const char *module_name, uptr module_offset) {
447 SanitizerSymbolizeFn symbolize_fn = is_data ? __sanitizer_symbolize_data
448 : __sanitizer_symbolize_code;
449 if (symbolize_fn(module_name, module_offset, buffer_, kBufferSize))
450 return buffer_;
451 return 0;
454 void Flush() {
455 if (__sanitizer_symbolize_flush)
456 __sanitizer_symbolize_flush();
459 const char *Demangle(const char *name) {
460 if (__sanitizer_symbolize_demangle) {
461 for (uptr res_length = 1024;
462 res_length <= InternalSizeClassMap::kMaxSize;) {
463 char *res_buff = static_cast<char*>(InternalAlloc(res_length));
464 uptr req_length =
465 __sanitizer_symbolize_demangle(name, res_buff, res_length);
466 if (req_length > res_length) {
467 res_length = req_length + 1;
468 InternalFree(res_buff);
469 continue;
471 return res_buff;
474 return name;
477 private:
478 InternalSymbolizer() { }
480 static const int kBufferSize = 16 * 1024;
481 static const int kMaxDemangledNameSize = 1024;
482 char buffer_[kBufferSize];
484 #else // SANITIZER_SUPPORTS_WEAK_HOOKS
486 class InternalSymbolizer {
487 public:
488 static InternalSymbolizer *get(LowLevelAllocator *alloc) { return 0; }
489 char *SendCommand(bool is_data, const char *module_name, uptr module_offset) {
490 return 0;
492 void Flush() { }
493 const char *Demangle(const char *name) { return name; }
496 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
498 class POSIXSymbolizer : public Symbolizer {
499 public:
500 POSIXSymbolizer(ExternalSymbolizerInterface *external_symbolizer,
501 InternalSymbolizer *internal_symbolizer,
502 LibbacktraceSymbolizer *libbacktrace_symbolizer)
503 : Symbolizer(),
504 external_symbolizer_(external_symbolizer),
505 internal_symbolizer_(internal_symbolizer),
506 libbacktrace_symbolizer_(libbacktrace_symbolizer) {}
508 uptr SymbolizePC(uptr addr, AddressInfo *frames, uptr max_frames) {
509 BlockingMutexLock l(&mu_);
510 if (max_frames == 0)
511 return 0;
512 const char *module_name;
513 uptr module_offset;
514 if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset))
515 return 0;
516 // First, try to use libbacktrace symbolizer (if it's available).
517 if (libbacktrace_symbolizer_ != 0) {
518 mu_.CheckLocked();
519 uptr res = libbacktrace_symbolizer_->SymbolizeCode(
520 addr, frames, max_frames, module_name, module_offset);
521 if (res > 0)
522 return res;
524 const char *str = SendCommand(false, module_name, module_offset);
525 if (str == 0) {
526 // Symbolizer was not initialized or failed. Fill only data
527 // about module name and offset.
528 AddressInfo *info = &frames[0];
529 info->Clear();
530 info->FillAddressAndModuleInfo(addr, module_name, module_offset);
531 return 1;
533 uptr frame_id = 0;
534 for (frame_id = 0; frame_id < max_frames; frame_id++) {
535 AddressInfo *info = &frames[frame_id];
536 char *function_name = 0;
537 str = ExtractToken(str, "\n", &function_name);
538 CHECK(function_name);
539 if (function_name[0] == '\0') {
540 // There are no more frames.
541 break;
543 info->Clear();
544 info->FillAddressAndModuleInfo(addr, module_name, module_offset);
545 info->function = function_name;
546 // Parse <file>:<line>:<column> buffer.
547 char *file_line_info = 0;
548 str = ExtractToken(str, "\n", &file_line_info);
549 CHECK(file_line_info);
550 const char *line_info = ExtractToken(file_line_info, ":", &info->file);
551 line_info = ExtractInt(line_info, ":", &info->line);
552 line_info = ExtractInt(line_info, "", &info->column);
553 InternalFree(file_line_info);
555 // Functions and filenames can be "??", in which case we write 0
556 // to address info to mark that names are unknown.
557 if (0 == internal_strcmp(info->function, "??")) {
558 InternalFree(info->function);
559 info->function = 0;
561 if (0 == internal_strcmp(info->file, "??")) {
562 InternalFree(info->file);
563 info->file = 0;
566 if (frame_id == 0) {
567 // Make sure we return at least one frame.
568 AddressInfo *info = &frames[0];
569 info->Clear();
570 info->FillAddressAndModuleInfo(addr, module_name, module_offset);
571 frame_id = 1;
573 return frame_id;
576 bool SymbolizeData(uptr addr, DataInfo *info) {
577 BlockingMutexLock l(&mu_);
578 LoadedModule *module = FindModuleForAddress(addr);
579 if (module == 0)
580 return false;
581 const char *module_name = module->full_name();
582 uptr module_offset = addr - module->base_address();
583 internal_memset(info, 0, sizeof(*info));
584 info->address = addr;
585 info->module = internal_strdup(module_name);
586 info->module_offset = module_offset;
587 // First, try to use libbacktrace symbolizer (if it's available).
588 if (libbacktrace_symbolizer_ != 0) {
589 mu_.CheckLocked();
590 if (libbacktrace_symbolizer_->SymbolizeData(info))
591 return true;
593 const char *str = SendCommand(true, module_name, module_offset);
594 if (str == 0)
595 return true;
596 str = ExtractToken(str, "\n", &info->name);
597 str = ExtractUptr(str, " ", &info->start);
598 str = ExtractUptr(str, "\n", &info->size);
599 info->start += module->base_address();
600 return true;
603 bool GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
604 uptr *module_address) {
605 BlockingMutexLock l(&mu_);
606 return FindModuleNameAndOffsetForAddress(pc, module_name, module_address);
609 bool CanReturnFileLineInfo() {
610 return internal_symbolizer_ != 0 || external_symbolizer_ != 0 ||
611 libbacktrace_symbolizer_ != 0;
614 void Flush() {
615 BlockingMutexLock l(&mu_);
616 if (internal_symbolizer_ != 0) {
617 SymbolizerScope sym_scope(this);
618 internal_symbolizer_->Flush();
622 const char *Demangle(const char *name) {
623 BlockingMutexLock l(&mu_);
624 // Run hooks even if we don't use internal symbolizer, as cxxabi
625 // demangle may call system functions.
626 SymbolizerScope sym_scope(this);
627 // Try to use libbacktrace demangler (if available).
628 if (libbacktrace_symbolizer_ != 0) {
629 if (const char *demangled = libbacktrace_symbolizer_->Demangle(name))
630 return demangled;
632 if (internal_symbolizer_ != 0)
633 return internal_symbolizer_->Demangle(name);
634 return DemangleCXXABI(name);
637 void PrepareForSandboxing() {
638 #if SANITIZER_LINUX && !SANITIZER_ANDROID
639 BlockingMutexLock l(&mu_);
640 // Cache /proc/self/exe on Linux.
641 CacheBinaryName();
642 #endif
645 private:
646 char *SendCommand(bool is_data, const char *module_name, uptr module_offset) {
647 mu_.CheckLocked();
648 // First, try to use internal symbolizer.
649 if (internal_symbolizer_) {
650 SymbolizerScope sym_scope(this);
651 return internal_symbolizer_->SendCommand(is_data, module_name,
652 module_offset);
654 // Otherwise, fall back to external symbolizer.
655 if (external_symbolizer_) {
656 SymbolizerScope sym_scope(this);
657 return external_symbolizer_->SendCommand(is_data, module_name,
658 module_offset);
660 return 0;
663 LoadedModule *FindModuleForAddress(uptr address) {
664 mu_.CheckLocked();
665 bool modules_were_reloaded = false;
666 if (modules_ == 0 || !modules_fresh_) {
667 modules_ = (LoadedModule*)(symbolizer_allocator_.Allocate(
668 kMaxNumberOfModuleContexts * sizeof(LoadedModule)));
669 CHECK(modules_);
670 n_modules_ = GetListOfModules(modules_, kMaxNumberOfModuleContexts,
671 /* filter */ 0);
672 CHECK_GT(n_modules_, 0);
673 CHECK_LT(n_modules_, kMaxNumberOfModuleContexts);
674 modules_fresh_ = true;
675 modules_were_reloaded = true;
677 for (uptr i = 0; i < n_modules_; i++) {
678 if (modules_[i].containsAddress(address)) {
679 return &modules_[i];
682 // Reload the modules and look up again, if we haven't tried it yet.
683 if (!modules_were_reloaded) {
684 // FIXME: set modules_fresh_ from dlopen()/dlclose() interceptors.
685 // It's too aggressive to reload the list of modules each time we fail
686 // to find a module for a given address.
687 modules_fresh_ = false;
688 return FindModuleForAddress(address);
690 return 0;
693 bool FindModuleNameAndOffsetForAddress(uptr address, const char **module_name,
694 uptr *module_offset) {
695 mu_.CheckLocked();
696 LoadedModule *module = FindModuleForAddress(address);
697 if (module == 0)
698 return false;
699 *module_name = module->full_name();
700 *module_offset = address - module->base_address();
701 return true;
704 // 16K loaded modules should be enough for everyone.
705 static const uptr kMaxNumberOfModuleContexts = 1 << 14;
706 LoadedModule *modules_; // Array of module descriptions is leaked.
707 uptr n_modules_;
708 // If stale, need to reload the modules before looking up addresses.
709 bool modules_fresh_;
710 BlockingMutex mu_;
712 ExternalSymbolizerInterface *external_symbolizer_; // Leaked.
713 InternalSymbolizer *const internal_symbolizer_; // Leaked.
714 LibbacktraceSymbolizer *libbacktrace_symbolizer_; // Leaked.
717 Symbolizer *Symbolizer::PlatformInit() {
718 if (!common_flags()->symbolize) {
719 return new(symbolizer_allocator_) POSIXSymbolizer(0, 0, 0);
721 InternalSymbolizer* internal_symbolizer =
722 InternalSymbolizer::get(&symbolizer_allocator_);
723 ExternalSymbolizerInterface *external_symbolizer = 0;
724 LibbacktraceSymbolizer *libbacktrace_symbolizer = 0;
726 if (!internal_symbolizer) {
727 libbacktrace_symbolizer =
728 LibbacktraceSymbolizer::get(&symbolizer_allocator_);
729 if (!libbacktrace_symbolizer) {
730 const char *path_to_external = common_flags()->external_symbolizer_path;
731 if (path_to_external && path_to_external[0] == '\0') {
732 // External symbolizer is explicitly disabled. Do nothing.
733 } else {
734 // Find path to llvm-symbolizer if it's not provided.
735 if (!path_to_external)
736 path_to_external = FindPathToBinary("llvm-symbolizer");
737 if (path_to_external) {
738 external_symbolizer = new(symbolizer_allocator_)
739 LLVMSymbolizerProcess(path_to_external);
740 } else if (common_flags()->allow_addr2line) {
741 // If llvm-symbolizer is not found, try to use addr2line.
742 if (const char *addr2line_path = FindPathToBinary("addr2line")) {
743 external_symbolizer = new(symbolizer_allocator_)
744 Addr2LinePool(addr2line_path, &symbolizer_allocator_);
751 return new(symbolizer_allocator_) POSIXSymbolizer(
752 external_symbolizer, internal_symbolizer, libbacktrace_symbolizer);
755 } // namespace __sanitizer
757 #endif // SANITIZER_POSIX