1 //===-- sanitizer_symbolizer_internal.h -------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Header for internal classes and functions to be used by implementations of
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_SYMBOLIZER_INTERNAL_H
13 #define SANITIZER_SYMBOLIZER_INTERNAL_H
15 #include "sanitizer_symbolizer.h"
16 #include "sanitizer_file.h"
18 namespace __sanitizer
{
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
,
30 const char *DemangleSwiftAndCXX(const char *name
);
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
{
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.
42 SymbolizerTool() : next(nullptr) { }
44 // Can't declare pure virtual functions in sanitizer runtimes:
45 // __cxa_pure_virtual might be unavailable.
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.
50 virtual bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) {
54 // The |info| parameter is inout. It is pre-filled with the module base
55 // and module offset values.
56 virtual bool SymbolizeData(uptr addr
, DataInfo
*info
) {
60 virtual void Flush() {}
62 // Return nullptr to fallback to the default platform-specific demangler.
63 virtual const char *Demangle(const char *name
) {
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
{
73 explicit SymbolizerProcess(const char *path
, bool use_forkpty
= false);
74 const char *SendCommand(const char *command
);
77 virtual bool ReachedEndOfOutput(const char *buffer
, uptr length
) const {
81 /// The maximum number of arguments required to invoke a tool process.
82 enum { kArgVMax
= 6 };
84 /// Fill in an argv array to invoke the child process.
85 virtual void GetArgV(const char *path_to_binary
,
86 const char *(&argv
)[kArgVMax
]) const {
90 virtual bool ReadFromSymbolizer(char *buffer
, uptr max_length
);
94 const char *SendCommandImpl(const char *command
);
95 bool WriteToSymbolizer(const char *buffer
, uptr length
);
96 bool StartSymbolizerSubprocess();
102 static const uptr kBufferSize
= 16 * 1024;
103 char buffer_
[kBufferSize
];
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_
;
113 class LLVMSymbolizerProcess
;
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
{
119 explicit LLVMSymbolizer(const char *path
, LowLevelAllocator
*allocator
);
121 bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) override
;
123 bool SymbolizeData(uptr addr
, DataInfo
*info
) override
;
126 const char *FormatAndSendCommand(bool is_data
, const char *module_name
,
127 uptr module_offset
, ModuleArch arch
);
129 LLVMSymbolizerProcess
*symbolizer_process_
;
130 static const uptr kBufferSize
= 16 * 1024;
131 char buffer_
[kBufferSize
];
134 // Parses one or more two-line strings in the following format:
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
);
142 // Parses a two-line string in the following format:
144 // <start_address> <size>
145 // Used by LLVMSymbolizer and InternalSymbolizer.
146 void ParseSymbolizeDataOutput(const char *str
, DataInfo
*info
);
148 } // namespace __sanitizer
150 #endif // SANITIZER_SYMBOLIZER_INTERNAL_H