sanitizer_symbolizer.h revision e00495aa0f3b114c7b764769d9450e5d2efb8e64
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_allocator_internal.h" 28#include "sanitizer_internal_defs.h" 29#include "sanitizer_libc.h" 30// WARNING: Do not include system headers here. See details above. 31 32namespace __sanitizer { 33 34struct AddressInfo { 35 uptr address; 36 char *module; 37 uptr module_offset; 38 char *function; 39 char *file; 40 int line; 41 int column; 42 43 AddressInfo() { 44 internal_memset(this, 0, sizeof(AddressInfo)); 45 } 46 47 // Deletes all strings and sets all fields to zero. 48 void Clear() { 49 InternalFree(module); 50 InternalFree(function); 51 InternalFree(file); 52 internal_memset(this, 0, sizeof(AddressInfo)); 53 } 54 55 void FillAddressAndModuleInfo(uptr addr, const char *mod_name, 56 uptr mod_offset) { 57 address = addr; 58 module = internal_strdup(mod_name); 59 module_offset = mod_offset; 60 } 61}; 62 63struct DataInfo { 64 uptr address; 65 char *module; 66 uptr module_offset; 67 char *name; 68 uptr start; 69 uptr size; 70}; 71 72class Symbolizer { 73 public: 74 /// Returns platform-specific implementation of Symbolizer. The symbolizer 75 /// must be initialized (with init or disable) before calling this function. 76 static Symbolizer *Get(); 77 /// Returns platform-specific implementation of Symbolizer, or null if not 78 /// initialized. 79 static Symbolizer *GetOrNull(); 80 /// Returns platform-specific implementation of Symbolizer. Will 81 /// automatically initialize symbolizer as if by calling Init(0) if needed. 82 static Symbolizer *GetOrInit(); 83 /// Initialize and return the symbolizer, given an optional path to an 84 /// external symbolizer. The path argument is only required for legacy 85 /// reasons as this function will check $PATH for an external symbolizer. Not 86 /// thread safe. 87 static Symbolizer *Init(const char* path_to_external = 0); 88 /// Initialize the symbolizer in a disabled state. Not thread safe. 89 static Symbolizer *Disable(); 90 // Fills at most "max_frames" elements of "frames" with descriptions 91 // for a given address (in all inlined functions). Returns the number 92 // of descriptions actually filled. 93 virtual uptr SymbolizeCode(uptr address, AddressInfo *frames, 94 uptr max_frames) { 95 return 0; 96 } 97 virtual bool SymbolizeData(uptr address, DataInfo *info) { 98 return false; 99 } 100 virtual bool IsAvailable() { 101 return false; 102 } 103 virtual bool IsExternalAvailable() { 104 return false; 105 } 106 // Release internal caches (if any). 107 virtual void Flush() {} 108 // Attempts to demangle the provided C++ mangled name. 109 virtual const char *Demangle(const char *name) { 110 return name; 111 } 112 virtual void PrepareForSandboxing() {} 113 114 private: 115 /// Platform-specific function for creating a Symbolizer object. 116 static Symbolizer *PlatformInit(const char *path_to_external); 117 /// Create a symbolizer and store it to symbolizer_ without checking if one 118 /// already exists. Not thread safe. 119 static Symbolizer *CreateAndStore(const char *path_to_external); 120 121 static Symbolizer *symbolizer_; 122 static StaticSpinMutex init_mu_; 123 124 protected: 125 static LowLevelAllocator symbolizer_allocator_; 126}; 127 128} // namespace __sanitizer 129 130#endif // SANITIZER_SYMBOLIZER_H 131