sanitizer_symbolizer.h revision 274201
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 used by sanitizers to map instruction address to a location in 11// source code at run-time. Symbolizer either uses __sanitizer_symbolize_* 12// defined in the program, or (if they are missing) tries to find and 13// launch "llvm-symbolizer" commandline tool in a separate process and 14// communicate with it. 15// 16// Generally we should try to avoid calling system library functions during 17// symbolization (and use their replacements from sanitizer_libc.h instead). 18//===----------------------------------------------------------------------===// 19#ifndef SANITIZER_SYMBOLIZER_H 20#define SANITIZER_SYMBOLIZER_H 21 22#include "sanitizer_allocator_internal.h" 23#include "sanitizer_internal_defs.h" 24#include "sanitizer_libc.h" 25 26namespace __sanitizer { 27 28struct AddressInfo { 29 uptr address; 30 char *module; 31 uptr module_offset; 32 char *function; 33 char *file; 34 int line; 35 int column; 36 37 AddressInfo() { 38 internal_memset(this, 0, sizeof(AddressInfo)); 39 } 40 41 // Deletes all strings and sets all fields to zero. 42 void Clear() { 43 InternalFree(module); 44 InternalFree(function); 45 InternalFree(file); 46 internal_memset(this, 0, sizeof(AddressInfo)); 47 } 48 49 void FillAddressAndModuleInfo(uptr addr, const char *mod_name, 50 uptr mod_offset) { 51 address = addr; 52 module = internal_strdup(mod_name); 53 module_offset = mod_offset; 54 } 55}; 56 57struct DataInfo { 58 uptr address; 59 char *module; 60 uptr module_offset; 61 char *name; 62 uptr start; 63 uptr size; 64}; 65 66class Symbolizer { 67 public: 68 /// Returns platform-specific implementation of Symbolizer. The symbolizer 69 /// must be initialized (with init or disable) before calling this function. 70 static Symbolizer *Get(); 71 /// Returns platform-specific implementation of Symbolizer, or null if not 72 /// initialized. 73 static Symbolizer *GetOrNull(); 74 /// Returns platform-specific implementation of Symbolizer. Will 75 /// automatically initialize symbolizer as if by calling Init(0) if needed. 76 static Symbolizer *GetOrInit(); 77 /// Initialize and return the symbolizer, given an optional path to an 78 /// external symbolizer. The path argument is only required for legacy 79 /// reasons as this function will check $PATH for an external symbolizer. Not 80 /// thread safe. 81 static Symbolizer *Init(const char* path_to_external = 0); 82 /// Initialize the symbolizer in a disabled state. Not thread safe. 83 static Symbolizer *Disable(); 84 // Fills at most "max_frames" elements of "frames" with descriptions 85 // for a given address (in all inlined functions). Returns the number 86 // of descriptions actually filled. 87 virtual uptr SymbolizeCode(uptr address, AddressInfo *frames, 88 uptr max_frames) { 89 return 0; 90 } 91 virtual bool SymbolizeData(uptr address, DataInfo *info) { 92 return false; 93 } 94 virtual bool IsAvailable() { 95 return false; 96 } 97 virtual bool IsExternalAvailable() { 98 return false; 99 } 100 // Release internal caches (if any). 101 virtual void Flush() {} 102 // Attempts to demangle the provided C++ mangled name. 103 virtual const char *Demangle(const char *name) { 104 return name; 105 } 106 virtual void PrepareForSandboxing() {} 107 108 // Allow user to install hooks that would be called before/after Symbolizer 109 // does the actual file/line info fetching. Specific sanitizers may need this 110 // to distinguish system library calls made in user code from calls made 111 // during in-process symbolization. 112 typedef void (*StartSymbolizationHook)(); 113 typedef void (*EndSymbolizationHook)(); 114 // May be called at most once. 115 void AddHooks(StartSymbolizationHook start_hook, 116 EndSymbolizationHook end_hook); 117 118 private: 119 /// Platform-specific function for creating a Symbolizer object. 120 static Symbolizer *PlatformInit(const char *path_to_external); 121 /// Create a symbolizer and store it to symbolizer_ without checking if one 122 /// already exists. Not thread safe. 123 static Symbolizer *CreateAndStore(const char *path_to_external); 124 125 static Symbolizer *symbolizer_; 126 static StaticSpinMutex init_mu_; 127 128 protected: 129 Symbolizer(); 130 131 static LowLevelAllocator symbolizer_allocator_; 132 133 StartSymbolizationHook start_hook_; 134 EndSymbolizationHook end_hook_; 135 class SymbolizerScope { 136 public: 137 explicit SymbolizerScope(const Symbolizer *sym); 138 ~SymbolizerScope(); 139 private: 140 const Symbolizer *sym_; 141 }; 142}; 143 144} // namespace __sanitizer 145 146#endif // SANITIZER_SYMBOLIZER_H 147