sanitizer_symbolizer.h revision 68c016aea0b61f649b9d9ba65c7d7217e0c0f6cb
1//===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Symbolizer is intended to be used by both 11// AddressSanitizer and ThreadSanitizer to symbolize a given 12// address. It is an analogue of addr2line utility and allows to map 13// instruction address to a location in source code at run-time. 14// 15// Symbolizer is planned to use debug information (in DWARF format) 16// in a binary via interface defined in "llvm/DebugInfo/DIContext.h" 17// 18// Symbolizer code should be called from the run-time library of 19// dynamic tools, and generally should not call memory allocation 20// routines or other system library functions intercepted by those tools. 21// Instead, Symbolizer code should use their replacements, defined in 22// "compiler-rt/lib/sanitizer_common/sanitizer_libc.h". 23//===----------------------------------------------------------------------===// 24#ifndef SANITIZER_SYMBOLIZER_H 25#define SANITIZER_SYMBOLIZER_H 26 27#include "sanitizer_internal_defs.h" 28#include "sanitizer_libc.h" 29// WARNING: Do not include system headers here. See details above. 30 31namespace __sanitizer { 32 33struct AddressInfo { 34 uptr address; 35 char *module; 36 uptr module_offset; 37 char *function; 38 char *file; 39 int line; 40 int column; 41 42 AddressInfo() { 43 internal_memset(this, 0, sizeof(AddressInfo)); 44 } 45 // Deletes all strings and sets all fields to zero. 46 SANITIZER_WEAK_ATTRIBUTE void Clear(); 47 48 void FillAddressAndModuleInfo(uptr addr, const char *mod_name, 49 uptr mod_offset) { 50 address = addr; 51 module = internal_strdup(mod_name); 52 module_offset = mod_offset; 53 } 54}; 55 56struct DataInfo { 57 uptr address; 58 char *module; 59 uptr module_offset; 60 char *name; 61 uptr start; 62 uptr size; 63}; 64 65// Fills at most "max_frames" elements of "frames" with descriptions 66// for a given address (in all inlined functions). Returns the number 67// of descriptions actually filled. 68// This function should NOT be called from two threads simultaneously. 69SANITIZER_WEAK_ATTRIBUTE 70uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames); 71bool SymbolizeData(uptr address, DataInfo *info); 72 73bool IsSymbolizerAvailable(); 74void FlushSymbolizer(); // releases internal caches (if any) 75 76// Attempts to demangle the provided C++ mangled name. 77const char *Demangle(const char *name); 78// Attempts to demangle the name via __cxa_demangle from __cxxabiv1. 79const char *DemangleCXXABI(const char *name); 80 81// Starts external symbolizer program in a subprocess. Sanitizer communicates 82// with external symbolizer via pipes. If path_to_symbolizer is NULL or empty, 83// tries to look for llvm-symbolizer in PATH. 84bool InitializeExternalSymbolizer(const char *path_to_symbolizer); 85 86const int kSymbolizerStartupTimeMillis = 10; 87 88class LoadedModule { 89 public: 90 LoadedModule(const char *module_name, uptr base_address); 91 void addAddressRange(uptr beg, uptr end); 92 bool containsAddress(uptr address) const; 93 94 const char *full_name() const { return full_name_; } 95 uptr base_address() const { return base_address_; } 96 97 private: 98 struct AddressRange { 99 uptr beg; 100 uptr end; 101 }; 102 char *full_name_; 103 uptr base_address_; 104 static const uptr kMaxNumberOfAddressRanges = 6; 105 AddressRange ranges_[kMaxNumberOfAddressRanges]; 106 uptr n_ranges_; 107}; 108 109// Creates external symbolizer connected via pipe, user should write 110// to output_fd and read from input_fd. 111bool StartSymbolizerSubprocess(const char *path_to_symbolizer, 112 int *input_fd, int *output_fd); 113 114// OS-dependent function that fills array with descriptions of at most 115// "max_modules" currently loaded modules. Returns the number of 116// initialized modules. If filter is nonzero, ignores modules for which 117// filter(full_name) is false. 118typedef bool (*string_predicate_t)(const char *); 119uptr GetListOfModules(LoadedModule *modules, uptr max_modules, 120 string_predicate_t filter); 121 122void SymbolizerPrepareForSandboxing(); 123 124} // namespace __sanitizer 125 126#endif // SANITIZER_SYMBOLIZER_H 127