1 //===-- sanitizer_symbolizer_internal.h -------------------------*- C++ -*-===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // Header for internal classes and functions to be used by implementations of 9 // symbolizers. 10 // 11 //===----------------------------------------------------------------------===// 12 #ifndef SANITIZER_SYMBOLIZER_INTERNAL_H 13 #define SANITIZER_SYMBOLIZER_INTERNAL_H 14 15 #include "sanitizer_symbolizer.h" 16 #include "sanitizer_file.h" 17 18 namespace __sanitizer { 19 20 // Parsing helpers, 'str' is searched for delimiter(s) and a string or uptr 21 // is extracted. When extracting a string, a newly allocated (using 22 // InternalAlloc) and null-terminataed buffer is returned. They return a pointer 23 // to the next characted after the found delimiter. 24 const char *ExtractToken(const char *str, const char *delims, char **result); 25 const char *ExtractInt(const char *str, const char *delims, int *result); 26 const char *ExtractUptr(const char *str, const char *delims, uptr *result); 27 const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter, 28 char **result); 29 30 const char *DemangleSwiftAndCXX(const char *name); 31 32 // SymbolizerTool is an interface that is implemented by individual "tools" 33 // that can perform symbolication (external llvm-symbolizer, libbacktrace, 34 // Windows DbgHelp symbolizer, etc.). 35 class SymbolizerTool { 36 public: 37 // The main |Symbolizer| class implements a "fallback chain" of symbolizer 38 // tools. In a request to symbolize an address, if one tool returns false, 39 // the next tool in the chain will be tried. 40 SymbolizerTool *next; 41 SymbolizerTool()42 SymbolizerTool() : next(nullptr) { } 43 44 // Can't declare pure virtual functions in sanitizer runtimes: 45 // __cxa_pure_virtual might be unavailable. 46 47 // The |stack| parameter is inout. It is pre-filled with the address, 48 // module base and module offset values and is to be used to construct 49 // other stack frames. SymbolizePC(uptr addr,SymbolizedStack * stack)50 virtual bool SymbolizePC(uptr addr, SymbolizedStack *stack) { 51 UNIMPLEMENTED(); 52 } 53 54 // The |info| parameter is inout. It is pre-filled with the module base 55 // and module offset values. SymbolizeData(uptr addr,DataInfo * info)56 virtual bool SymbolizeData(uptr addr, DataInfo *info) { 57 UNIMPLEMENTED(); 58 } 59 Flush()60 virtual void Flush() {} 61 62 // Return nullptr to fallback to the default platform-specific demangler. Demangle(const char * name)63 virtual const char *Demangle(const char *name) { 64 return nullptr; 65 } 66 }; 67 68 // SymbolizerProcess encapsulates communication between the tool and 69 // external symbolizer program, running in a different subprocess. 70 // SymbolizerProcess may not be used from two threads simultaneously. 71 class SymbolizerProcess { 72 public: 73 explicit SymbolizerProcess(const char *path, bool use_forkpty = false); 74 const char *SendCommand(const char *command); 75 76 protected: ReachedEndOfOutput(const char * buffer,uptr length)77 virtual bool ReachedEndOfOutput(const char *buffer, uptr length) const { 78 UNIMPLEMENTED(); 79 } 80 81 /// The maximum number of arguments required to invoke a tool process. 82 enum { kArgVMax = 6 }; 83 84 /// Fill in an argv array to invoke the child process. GetArgV(const char * path_to_binary,const char * (& argv)[kArgVMax])85 virtual void GetArgV(const char *path_to_binary, 86 const char *(&argv)[kArgVMax]) const { 87 UNIMPLEMENTED(); 88 } 89 90 virtual bool ReadFromSymbolizer(char *buffer, uptr max_length); 91 92 private: 93 bool Restart(); 94 const char *SendCommandImpl(const char *command); 95 bool WriteToSymbolizer(const char *buffer, uptr length); 96 bool StartSymbolizerSubprocess(); 97 98 const char *path_; 99 fd_t input_fd_; 100 fd_t output_fd_; 101 102 static const uptr kBufferSize = 16 * 1024; 103 char buffer_[kBufferSize]; 104 105 static const uptr kMaxTimesRestarted = 5; 106 static const int kSymbolizerStartupTimeMillis = 10; 107 uptr times_restarted_; 108 bool failed_to_start_; 109 bool reported_invalid_path_; 110 bool use_forkpty_; 111 }; 112 113 class LLVMSymbolizerProcess; 114 115 // This tool invokes llvm-symbolizer in a subprocess. It should be as portable 116 // as the llvm-symbolizer tool is. 117 class LLVMSymbolizer : public SymbolizerTool { 118 public: 119 explicit LLVMSymbolizer(const char *path, LowLevelAllocator *allocator); 120 121 bool SymbolizePC(uptr addr, SymbolizedStack *stack) override; 122 123 bool SymbolizeData(uptr addr, DataInfo *info) override; 124 125 private: 126 const char *FormatAndSendCommand(bool is_data, const char *module_name, 127 uptr module_offset, ModuleArch arch); 128 129 LLVMSymbolizerProcess *symbolizer_process_; 130 static const uptr kBufferSize = 16 * 1024; 131 char buffer_[kBufferSize]; 132 }; 133 134 // Parses one or more two-line strings in the following format: 135 // <function_name> 136 // <file_name>:<line_number>[:<column_number>] 137 // Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of 138 // them use the same output format. Returns true if any useful debug 139 // information was found. 140 void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res); 141 142 // Parses a two-line string in the following format: 143 // <symbol_name> 144 // <start_address> <size> 145 // Used by LLVMSymbolizer and InternalSymbolizer. 146 void ParseSymbolizeDataOutput(const char *str, DataInfo *info); 147 148 } // namespace __sanitizer 149 150 #endif // SANITIZER_SYMBOLIZER_INTERNAL_H 151