sanitizer_symbolizer.h revision 276851
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_common.h" 23#include "sanitizer_mutex.h" 24 25namespace __sanitizer { 26 27struct AddressInfo { 28 // Owns all the string members. Storage for them is 29 // (de)allocated using sanitizer internal allocator. 30 uptr address; 31 32 char *module; 33 uptr module_offset; 34 35 static const uptr kUnknown = ~(uptr)0; 36 char *function; 37 uptr function_offset; 38 39 char *file; 40 int line; 41 int column; 42 43 AddressInfo(); 44 // Deletes all strings and resets all fields. 45 void Clear(); 46 void FillAddressAndModuleInfo(uptr addr, const char *mod_name, 47 uptr mod_offset); 48}; 49 50// Linked list of symbolized frames (each frame is described by AddressInfo). 51struct SymbolizedStack { 52 SymbolizedStack *next; 53 AddressInfo info; 54 static SymbolizedStack *New(uptr addr); 55 // Deletes current, and all subsequent frames in the linked list. 56 // The object cannot be accessed after the call to this function. 57 void ClearAll(); 58 59 private: 60 SymbolizedStack(); 61}; 62 63// For now, DataInfo is used to describe global variable. 64struct DataInfo { 65 // Owns all the string members. Storage for them is 66 // (de)allocated using sanitizer internal allocator. 67 char *module; 68 uptr module_offset; 69 char *name; 70 uptr start; 71 uptr size; 72 73 DataInfo(); 74 void Clear(); 75}; 76 77class Symbolizer { 78 public: 79 /// Initialize and return platform-specific implementation of symbolizer 80 /// (if it wasn't already initialized). 81 static Symbolizer *GetOrInit(); 82 // Returns a list of symbolized frames for a given address (containing 83 // all inlined functions, if necessary). 84 virtual SymbolizedStack *SymbolizePC(uptr address) { 85 return SymbolizedStack::New(address); 86 } 87 virtual bool SymbolizeData(uptr address, DataInfo *info) { 88 return false; 89 } 90 virtual bool GetModuleNameAndOffsetForPC(uptr pc, const char **module_name, 91 uptr *module_address) { 92 return false; 93 } 94 virtual bool CanReturnFileLineInfo() { 95 return false; 96 } 97 // Release internal caches (if any). 98 virtual void Flush() {} 99 // Attempts to demangle the provided C++ mangled name. 100 virtual const char *Demangle(const char *name) { 101 return name; 102 } 103 virtual void PrepareForSandboxing() {} 104 105 // Allow user to install hooks that would be called before/after Symbolizer 106 // does the actual file/line info fetching. Specific sanitizers may need this 107 // to distinguish system library calls made in user code from calls made 108 // during in-process symbolization. 109 typedef void (*StartSymbolizationHook)(); 110 typedef void (*EndSymbolizationHook)(); 111 // May be called at most once. 112 void AddHooks(StartSymbolizationHook start_hook, 113 EndSymbolizationHook end_hook); 114 115 private: 116 /// Platform-specific function for creating a Symbolizer object. 117 static Symbolizer *PlatformInit(); 118 /// Initialize the symbolizer in a disabled state. Not thread safe. 119 static Symbolizer *Disable(); 120 121 static Symbolizer *symbolizer_; 122 static StaticSpinMutex init_mu_; 123 124 protected: 125 Symbolizer(); 126 127 static LowLevelAllocator symbolizer_allocator_; 128 129 StartSymbolizationHook start_hook_; 130 EndSymbolizationHook end_hook_; 131 class SymbolizerScope { 132 public: 133 explicit SymbolizerScope(const Symbolizer *sym); 134 ~SymbolizerScope(); 135 private: 136 const Symbolizer *sym_; 137 }; 138}; 139 140} // namespace __sanitizer 141 142#endif // SANITIZER_SYMBOLIZER_H 143