2015-03-03 05:15:09 +08:00
|
|
|
//===-- sanitizer_symbolizer_internal.h -------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Header for internal classes and functions to be used by implementations of
|
|
|
|
// symbolizers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_SYMBOLIZER_INTERNAL_H
|
|
|
|
#define SANITIZER_SYMBOLIZER_INTERNAL_H
|
|
|
|
|
|
|
|
#include "sanitizer_symbolizer.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2015-03-03 06:15:14 +08:00
|
|
|
// Parsing helpers, 'str' is searched for delimiter(s) and a string or uptr
|
|
|
|
// is extracted. When extracting a string, a newly allocated (using
|
|
|
|
// InternalAlloc) and null-terminataed buffer is returned. They return a pointer
|
|
|
|
// to the next characted after the found delimiter.
|
|
|
|
const char *ExtractToken(const char *str, const char *delims, char **result);
|
|
|
|
const char *ExtractInt(const char *str, const char *delims, int *result);
|
|
|
|
const char *ExtractUptr(const char *str, const char *delims, uptr *result);
|
2015-03-12 18:53:18 +08:00
|
|
|
const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter,
|
|
|
|
char **result);
|
2015-03-03 06:15:14 +08:00
|
|
|
|
2016-04-16 07:51:00 +08:00
|
|
|
const char *DemangleSwiftAndCXX(const char *name);
|
2015-03-22 19:38:55 +08:00
|
|
|
|
2015-03-06 22:33:56 +08:00
|
|
|
// SymbolizerTool is an interface that is implemented by individual "tools"
|
|
|
|
// that can perform symbolication (external llvm-symbolizer, libbacktrace,
|
|
|
|
// Windows DbgHelp symbolizer, etc.).
|
2015-03-03 05:15:09 +08:00
|
|
|
class SymbolizerTool {
|
|
|
|
public:
|
2015-03-10 02:36:28 +08:00
|
|
|
// The main |Symbolizer| class implements a "fallback chain" of symbolizer
|
|
|
|
// tools. In a request to symbolize an address, if one tool returns false,
|
|
|
|
// the next tool in the chain will be tried.
|
2015-03-05 17:47:13 +08:00
|
|
|
SymbolizerTool *next;
|
|
|
|
|
|
|
|
SymbolizerTool() : next(nullptr) { }
|
|
|
|
|
2015-03-03 05:15:09 +08:00
|
|
|
// Can't declare pure virtual functions in sanitizer runtimes:
|
|
|
|
// __cxa_pure_virtual might be unavailable.
|
|
|
|
|
|
|
|
// The |stack| parameter is inout. It is pre-filled with the address,
|
|
|
|
// module base and module offset values and is to be used to construct
|
|
|
|
// other stack frames.
|
|
|
|
virtual bool SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The |info| parameter is inout. It is pre-filled with the module base
|
|
|
|
// and module offset values.
|
|
|
|
virtual bool SymbolizeData(uptr addr, DataInfo *info) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Flush() {}
|
|
|
|
|
2015-03-10 02:36:28 +08:00
|
|
|
// Return nullptr to fallback to the default platform-specific demangler.
|
2015-03-03 05:15:09 +08:00
|
|
|
virtual const char *Demangle(const char *name) {
|
2015-03-04 07:13:02 +08:00
|
|
|
return nullptr;
|
2015-03-03 05:15:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// SymbolizerProcess encapsulates communication between the tool and
|
|
|
|
// external symbolizer program, running in a different subprocess.
|
|
|
|
// SymbolizerProcess may not be used from two threads simultaneously.
|
|
|
|
class SymbolizerProcess {
|
|
|
|
public:
|
2015-03-12 18:53:18 +08:00
|
|
|
explicit SymbolizerProcess(const char *path, bool use_forkpty = false);
|
2015-03-03 05:15:09 +08:00
|
|
|
const char *SendCommand(const char *command);
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
protected:
|
2015-03-03 05:15:09 +08:00
|
|
|
virtual bool ReachedEndOfOutput(const char *buffer, uptr length) const {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
/// The maximum number of arguments required to invoke a tool process.
|
|
|
|
enum { kArgVMax = 6 };
|
|
|
|
|
|
|
|
/// Fill in an argv array to invoke the child process.
|
|
|
|
virtual void GetArgV(const char *path_to_binary,
|
|
|
|
const char *(&argv)[kArgVMax]) const {
|
2015-03-03 05:15:09 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2015-09-15 07:56:42 +08:00
|
|
|
virtual bool ReadFromSymbolizer(char *buffer, uptr max_length);
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
private:
|
|
|
|
bool Restart();
|
|
|
|
const char *SendCommandImpl(const char *command);
|
|
|
|
bool WriteToSymbolizer(const char *buffer, uptr length);
|
|
|
|
bool StartSymbolizerSubprocess();
|
|
|
|
|
2015-03-03 05:15:09 +08:00
|
|
|
const char *path_;
|
2015-08-11 08:30:22 +08:00
|
|
|
fd_t input_fd_;
|
|
|
|
fd_t output_fd_;
|
2015-03-03 05:15:09 +08:00
|
|
|
|
|
|
|
static const uptr kBufferSize = 16 * 1024;
|
|
|
|
char buffer_[kBufferSize];
|
|
|
|
|
|
|
|
static const uptr kMaxTimesRestarted = 5;
|
|
|
|
static const int kSymbolizerStartupTimeMillis = 10;
|
|
|
|
uptr times_restarted_;
|
|
|
|
bool failed_to_start_;
|
|
|
|
bool reported_invalid_path_;
|
2015-03-12 18:53:18 +08:00
|
|
|
bool use_forkpty_;
|
2015-03-03 05:15:09 +08:00
|
|
|
};
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
class LLVMSymbolizerProcess;
|
|
|
|
|
|
|
|
// This tool invokes llvm-symbolizer in a subprocess. It should be as portable
|
|
|
|
// as the llvm-symbolizer tool is.
|
|
|
|
class LLVMSymbolizer : public SymbolizerTool {
|
|
|
|
public:
|
|
|
|
explicit LLVMSymbolizer(const char *path, LowLevelAllocator *allocator);
|
|
|
|
|
|
|
|
bool SymbolizePC(uptr addr, SymbolizedStack *stack) override;
|
|
|
|
|
|
|
|
bool SymbolizeData(uptr addr, DataInfo *info) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *SendCommand(bool is_data, const char *module_name,
|
|
|
|
uptr module_offset);
|
|
|
|
|
|
|
|
LLVMSymbolizerProcess *symbolizer_process_;
|
|
|
|
static const uptr kBufferSize = 16 * 1024;
|
|
|
|
char buffer_[kBufferSize];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parses one or more two-line strings in the following format:
|
|
|
|
// <function_name>
|
|
|
|
// <file_name>:<line_number>[:<column_number>]
|
|
|
|
// Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of
|
|
|
|
// them use the same output format. Returns true if any useful debug
|
|
|
|
// information was found.
|
|
|
|
void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res);
|
|
|
|
|
|
|
|
// Parses a two-line string in the following format:
|
|
|
|
// <symbol_name>
|
|
|
|
// <start_address> <size>
|
|
|
|
// Used by LLVMSymbolizer and InternalSymbolizer.
|
|
|
|
void ParseSymbolizeDataOutput(const char *str, DataInfo *info);
|
|
|
|
|
2015-03-03 05:15:09 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_SYMBOLIZER_INTERNAL_H
|