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"
17 namespace __sanitizer
{
19 // Parsing helpers, 'str' is searched for delimiter(s) and a string or uptr
20 // is extracted. When extracting a string, a newly allocated (using
21 // InternalAlloc) and null-terminataed buffer is returned. They return a pointer
22 // to the next characted after the found delimiter.
23 const char *ExtractToken(const char *str
, const char *delims
, char **result
);
24 const char *ExtractInt(const char *str
, const char *delims
, int *result
);
25 const char *ExtractUptr(const char *str
, const char *delims
, uptr
*result
);
26 const char *ExtractTokenUpToDelimiter(const char *str
, const char *delimiter
,
29 const char *DemangleSwiftAndCXX(const char *name
);
31 // SymbolizerTool is an interface that is implemented by individual "tools"
32 // that can perform symbolication (external llvm-symbolizer, libbacktrace,
33 // Windows DbgHelp symbolizer, etc.).
34 class SymbolizerTool
{
36 // The main |Symbolizer| class implements a "fallback chain" of symbolizer
37 // tools. In a request to symbolize an address, if one tool returns false,
38 // the next tool in the chain will be tried.
41 SymbolizerTool() : next(nullptr) { }
43 // Can't declare pure virtual functions in sanitizer runtimes:
44 // __cxa_pure_virtual might be unavailable.
46 // The |stack| parameter is inout. It is pre-filled with the address,
47 // module base and module offset values and is to be used to construct
48 // other stack frames.
49 virtual bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) {
53 // The |info| parameter is inout. It is pre-filled with the module base
54 // and module offset values.
55 virtual bool SymbolizeData(uptr addr
, DataInfo
*info
) {
59 virtual void Flush() {}
61 // Return nullptr to fallback to the default platform-specific demangler.
62 virtual const char *Demangle(const char *name
) {
67 // SymbolizerProcess encapsulates communication between the tool and
68 // external symbolizer program, running in a different subprocess.
69 // SymbolizerProcess may not be used from two threads simultaneously.
70 class SymbolizerProcess
{
72 explicit SymbolizerProcess(const char *path
, bool use_forkpty
= false);
73 const char *SendCommand(const char *command
);
76 virtual bool ReachedEndOfOutput(const char *buffer
, uptr length
) const {
80 /// The maximum number of arguments required to invoke a tool process.
81 enum { kArgVMax
= 6 };
83 /// Fill in an argv array to invoke the child process.
84 virtual void GetArgV(const char *path_to_binary
,
85 const char *(&argv
)[kArgVMax
]) const {
89 virtual bool ReadFromSymbolizer(char *buffer
, uptr max_length
);
93 const char *SendCommandImpl(const char *command
);
94 bool WriteToSymbolizer(const char *buffer
, uptr length
);
95 bool StartSymbolizerSubprocess();
101 static const uptr kBufferSize
= 16 * 1024;
102 char buffer_
[kBufferSize
];
104 static const uptr kMaxTimesRestarted
= 5;
105 static const int kSymbolizerStartupTimeMillis
= 10;
106 uptr times_restarted_
;
107 bool failed_to_start_
;
108 bool reported_invalid_path_
;
112 class LLVMSymbolizerProcess
;
114 // This tool invokes llvm-symbolizer in a subprocess. It should be as portable
115 // as the llvm-symbolizer tool is.
116 class LLVMSymbolizer
: public SymbolizerTool
{
118 explicit LLVMSymbolizer(const char *path
, LowLevelAllocator
*allocator
);
120 bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) override
;
122 bool SymbolizeData(uptr addr
, DataInfo
*info
) override
;
125 const char *SendCommand(bool is_data
, const char *module_name
,
128 LLVMSymbolizerProcess
*symbolizer_process_
;
129 static const uptr kBufferSize
= 16 * 1024;
130 char buffer_
[kBufferSize
];
133 // Parses one or more two-line strings in the following format:
135 // <file_name>:<line_number>[:<column_number>]
136 // Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of
137 // them use the same output format. Returns true if any useful debug
138 // information was found.
139 void ParseSymbolizePCOutput(const char *str
, SymbolizedStack
*res
);
141 // Parses a two-line string in the following format:
143 // <start_address> <size>
144 // Used by LLVMSymbolizer and InternalSymbolizer.
145 void ParseSymbolizeDataOutput(const char *str
, DataInfo
*info
);
147 } // namespace __sanitizer
149 #endif // SANITIZER_SYMBOLIZER_INTERNAL_H