2019-08-01 02:51:27 +08:00
|
|
|
//===-- sanitizer_symbolizer_libcdep.cpp ----------------------------------===//
|
2013-10-26 07:03:29 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-10-26 07:03:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-03 06:15:14 +08:00
|
|
|
#include "sanitizer_allocator_internal.h"
|
2013-10-26 07:03:29 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
2015-03-03 05:15:09 +08:00
|
|
|
#include "sanitizer_symbolizer_internal.h"
|
2013-10-26 07:03:29 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2017-08-02 06:54:51 +08:00
|
|
|
Symbolizer *Symbolizer::GetOrInit() {
|
|
|
|
SpinMutexLock l(&init_mu_);
|
|
|
|
if (symbolizer_)
|
|
|
|
return symbolizer_;
|
|
|
|
symbolizer_ = PlatformInit();
|
|
|
|
CHECK(symbolizer_);
|
|
|
|
return symbolizer_;
|
|
|
|
}
|
|
|
|
|
2019-08-01 02:51:27 +08:00
|
|
|
// See sanitizer_symbolizer_markup.cpp.
|
2018-05-18 08:43:54 +08:00
|
|
|
#if !SANITIZER_SYMBOLIZER_MARKUP
|
2017-08-02 06:54:51 +08:00
|
|
|
|
2015-03-03 06:15:14 +08:00
|
|
|
const char *ExtractToken(const char *str, const char *delims, char **result) {
|
|
|
|
uptr prefix_len = internal_strcspn(str, delims);
|
|
|
|
*result = (char*)InternalAlloc(prefix_len + 1);
|
|
|
|
internal_memcpy(*result, str, prefix_len);
|
|
|
|
(*result)[prefix_len] = '\0';
|
|
|
|
const char *prefix_end = str + prefix_len;
|
|
|
|
if (*prefix_end != '\0') prefix_end++;
|
|
|
|
return prefix_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ExtractInt(const char *str, const char *delims, int *result) {
|
2020-02-07 00:44:42 +08:00
|
|
|
char *buff = nullptr;
|
2015-03-03 06:15:14 +08:00
|
|
|
const char *ret = ExtractToken(str, delims, &buff);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (buff) {
|
2015-03-03 06:15:14 +08:00
|
|
|
*result = (int)internal_atoll(buff);
|
|
|
|
}
|
|
|
|
InternalFree(buff);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
|
2020-02-07 00:44:42 +08:00
|
|
|
char *buff = nullptr;
|
2015-03-03 06:15:14 +08:00
|
|
|
const char *ret = ExtractToken(str, delims, &buff);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (buff) {
|
2015-03-03 06:15:14 +08:00
|
|
|
*result = (uptr)internal_atoll(buff);
|
|
|
|
}
|
|
|
|
InternalFree(buff);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:16:13 +08:00
|
|
|
const char *ExtractSptr(const char *str, const char *delims, sptr *result) {
|
2020-02-07 00:44:42 +08:00
|
|
|
char *buff = nullptr;
|
2019-06-28 07:16:13 +08:00
|
|
|
const char *ret = ExtractToken(str, delims, &buff);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (buff) {
|
2019-06-28 07:16:13 +08:00
|
|
|
*result = (sptr)internal_atoll(buff);
|
|
|
|
}
|
|
|
|
InternalFree(buff);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-12 18:53:18 +08:00
|
|
|
const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter,
|
|
|
|
char **result) {
|
|
|
|
const char *found_delimiter = internal_strstr(str, delimiter);
|
|
|
|
uptr prefix_len =
|
|
|
|
found_delimiter ? found_delimiter - str : internal_strlen(str);
|
|
|
|
*result = (char *)InternalAlloc(prefix_len + 1);
|
|
|
|
internal_memcpy(*result, str, prefix_len);
|
|
|
|
(*result)[prefix_len] = '\0';
|
|
|
|
const char *prefix_end = str + prefix_len;
|
|
|
|
if (*prefix_end != '\0') prefix_end += internal_strlen(delimiter);
|
|
|
|
return prefix_end;
|
|
|
|
}
|
|
|
|
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
|
|
|
|
BlockingMutexLock l(&mu_);
|
2020-02-07 00:44:42 +08:00
|
|
|
const char *module_name = nullptr;
|
2015-04-09 17:37:46 +08:00
|
|
|
uptr module_offset;
|
2017-01-07 05:45:05 +08:00
|
|
|
ModuleArch arch;
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizedStack *res = SymbolizedStack::New(addr);
|
2017-01-07 05:45:05 +08:00
|
|
|
if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
|
|
|
|
&arch))
|
2015-04-09 17:37:46 +08:00
|
|
|
return res;
|
|
|
|
// Always fill data about module name and offset.
|
2017-01-07 05:45:05 +08:00
|
|
|
res->info.FillModuleInfo(module_name, module_offset, arch);
|
2016-01-15 10:19:20 +08:00
|
|
|
for (auto &tool : tools_) {
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizerScope sym_scope(this);
|
2016-01-15 10:19:20 +08:00
|
|
|
if (tool.SymbolizePC(addr, res)) {
|
2015-04-09 17:37:46 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
|
|
|
|
BlockingMutexLock l(&mu_);
|
2020-02-07 00:44:42 +08:00
|
|
|
const char *module_name = nullptr;
|
2015-04-09 17:37:46 +08:00
|
|
|
uptr module_offset;
|
2017-01-07 05:45:05 +08:00
|
|
|
ModuleArch arch;
|
|
|
|
if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
|
|
|
|
&arch))
|
2015-04-09 17:37:46 +08:00
|
|
|
return false;
|
|
|
|
info->Clear();
|
|
|
|
info->module = internal_strdup(module_name);
|
|
|
|
info->module_offset = module_offset;
|
2017-01-07 05:45:05 +08:00
|
|
|
info->module_arch = arch;
|
2016-01-15 10:19:20 +08:00
|
|
|
for (auto &tool : tools_) {
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizerScope sym_scope(this);
|
2016-01-15 10:19:20 +08:00
|
|
|
if (tool.SymbolizeData(addr, info)) {
|
2015-04-09 17:37:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:16:13 +08:00
|
|
|
bool Symbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) {
|
|
|
|
BlockingMutexLock l(&mu_);
|
2020-02-07 00:44:42 +08:00
|
|
|
const char *module_name = nullptr;
|
2019-06-28 07:16:13 +08:00
|
|
|
if (!FindModuleNameAndOffsetForAddress(
|
|
|
|
addr, &module_name, &info->module_offset, &info->module_arch))
|
|
|
|
return false;
|
|
|
|
info->module = internal_strdup(module_name);
|
|
|
|
for (auto &tool : tools_) {
|
|
|
|
SymbolizerScope sym_scope(this);
|
|
|
|
if (tool.SymbolizeFrame(addr, info)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-09 17:37:46 +08:00
|
|
|
bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
|
|
|
|
uptr *module_address) {
|
|
|
|
BlockingMutexLock l(&mu_);
|
|
|
|
const char *internal_module_name = nullptr;
|
2017-01-07 05:45:05 +08:00
|
|
|
ModuleArch arch;
|
2015-04-09 17:37:46 +08:00
|
|
|
if (!FindModuleNameAndOffsetForAddress(pc, &internal_module_name,
|
2017-01-07 05:45:05 +08:00
|
|
|
module_address, &arch))
|
2015-04-09 17:37:46 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (module_name)
|
|
|
|
*module_name = module_names_.GetOwnedCopy(internal_module_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbolizer::Flush() {
|
|
|
|
BlockingMutexLock l(&mu_);
|
2016-01-15 10:19:20 +08:00
|
|
|
for (auto &tool : tools_) {
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizerScope sym_scope(this);
|
2016-01-15 10:19:20 +08:00
|
|
|
tool.Flush();
|
2015-04-09 17:37:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Symbolizer::Demangle(const char *name) {
|
|
|
|
BlockingMutexLock l(&mu_);
|
2016-01-15 10:19:20 +08:00
|
|
|
for (auto &tool : tools_) {
|
2015-04-09 17:37:46 +08:00
|
|
|
SymbolizerScope sym_scope(this);
|
2016-01-15 10:19:20 +08:00
|
|
|
if (const char *demangled = tool.Demangle(name))
|
2015-04-09 17:37:46 +08:00
|
|
|
return demangled;
|
|
|
|
}
|
|
|
|
return PlatformDemangle(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbolizer::FindModuleNameAndOffsetForAddress(uptr address,
|
|
|
|
const char **module_name,
|
2017-01-07 05:45:05 +08:00
|
|
|
uptr *module_offset,
|
|
|
|
ModuleArch *module_arch) {
|
2016-02-23 02:52:51 +08:00
|
|
|
const LoadedModule *module = FindModuleForAddress(address);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (!module)
|
2015-04-09 17:37:46 +08:00
|
|
|
return false;
|
|
|
|
*module_name = module->full_name();
|
|
|
|
*module_offset = address - module->base_address();
|
2017-01-07 05:45:05 +08:00
|
|
|
*module_arch = module->arch();
|
2015-04-09 17:37:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-29 23:14:31 +08:00
|
|
|
void Symbolizer::RefreshModules() {
|
|
|
|
modules_.init();
|
2017-10-03 04:22:16 +08:00
|
|
|
fallback_modules_.fallbackInit();
|
2017-09-29 23:14:31 +08:00
|
|
|
RAW_CHECK(modules_.size() > 0);
|
|
|
|
modules_fresh_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const LoadedModule *SearchForModule(const ListOfModules &modules,
|
|
|
|
uptr address) {
|
|
|
|
for (uptr i = 0; i < modules.size(); i++) {
|
|
|
|
if (modules[i].containsAddress(address)) {
|
|
|
|
return &modules[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:52:51 +08:00
|
|
|
const LoadedModule *Symbolizer::FindModuleForAddress(uptr address) {
|
2015-04-09 17:37:46 +08:00
|
|
|
bool modules_were_reloaded = false;
|
|
|
|
if (!modules_fresh_) {
|
2017-09-29 23:14:31 +08:00
|
|
|
RefreshModules();
|
2015-04-09 17:37:46 +08:00
|
|
|
modules_were_reloaded = true;
|
|
|
|
}
|
2017-09-29 23:14:31 +08:00
|
|
|
const LoadedModule *module = SearchForModule(modules_, address);
|
|
|
|
if (module) return module;
|
|
|
|
|
2017-09-29 03:37:17 +08:00
|
|
|
// dlopen/dlclose interceptors invalidate the module list, but when
|
|
|
|
// interception is disabled, we need to retry if the lookup fails in
|
|
|
|
// case the module list changed.
|
2017-09-27 00:12:56 +08:00
|
|
|
#if !SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
|
2015-04-09 17:37:46 +08:00
|
|
|
if (!modules_were_reloaded) {
|
2017-09-29 23:14:31 +08:00
|
|
|
RefreshModules();
|
|
|
|
module = SearchForModule(modules_, address);
|
|
|
|
if (module) return module;
|
2015-04-09 17:37:46 +08:00
|
|
|
}
|
2017-09-27 00:12:56 +08:00
|
|
|
#endif
|
2017-10-03 04:22:16 +08:00
|
|
|
|
|
|
|
if (fallback_modules_.size()) {
|
|
|
|
module = SearchForModule(fallback_modules_, address);
|
|
|
|
}
|
2017-09-29 23:14:31 +08:00
|
|
|
return module;
|
2015-04-09 17:37:46 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
// For now we assume the following protocol:
|
|
|
|
// For each request of the form
|
|
|
|
// <module_name> <module_offset>
|
|
|
|
// passed to STDIN, external symbolizer prints to STDOUT response:
|
|
|
|
// <function_name>
|
|
|
|
// <file_name>:<line_number>:<column_number>
|
|
|
|
// <function_name>
|
|
|
|
// <file_name>:<line_number>:<column_number>
|
|
|
|
// ...
|
|
|
|
// <empty line>
|
|
|
|
class LLVMSymbolizerProcess : public SymbolizerProcess {
|
|
|
|
public:
|
2019-08-16 01:25:10 +08:00
|
|
|
explicit LLVMSymbolizerProcess(const char *path)
|
|
|
|
: SymbolizerProcess(path, /*use_posix_spawn=*/SANITIZER_MAC) {}
|
2015-08-11 23:51:40 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
|
|
|
|
// Empty line marks the end of llvm-symbolizer output.
|
|
|
|
return length >= 2 && buffer[length - 1] == '\n' &&
|
|
|
|
buffer[length - 2] == '\n';
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:45:05 +08:00
|
|
|
// When adding a new architecture, don't forget to also update
|
|
|
|
// script/asan_symbolize.py and sanitizer_common.h.
|
2015-08-11 23:51:40 +08:00
|
|
|
void GetArgV(const char *path_to_binary,
|
|
|
|
const char *(&argv)[kArgVMax]) const override {
|
|
|
|
#if defined(__x86_64h__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=x86_64h";
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=x86_64";
|
|
|
|
#elif defined(__i386__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=i386";
|
2016-02-02 10:01:17 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=arm64";
|
|
|
|
#elif defined(__arm__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=arm";
|
2016-03-10 07:39:40 +08:00
|
|
|
#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
2015-08-11 23:51:40 +08:00
|
|
|
const char* const kSymbolizerArch = "--default-arch=powerpc64";
|
2016-03-10 07:39:40 +08:00
|
|
|
#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2015-08-11 23:51:40 +08:00
|
|
|
const char* const kSymbolizerArch = "--default-arch=powerpc64le";
|
2016-04-26 18:41:30 +08:00
|
|
|
#elif defined(__s390x__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=s390x";
|
|
|
|
#elif defined(__s390__)
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=s390";
|
2015-08-11 23:51:40 +08:00
|
|
|
#else
|
|
|
|
const char* const kSymbolizerArch = "--default-arch=unknown";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *const inline_flag = common_flags()->symbolize_inline_frames
|
|
|
|
? "--inlining=true"
|
|
|
|
: "--inlining=false";
|
|
|
|
int i = 0;
|
|
|
|
argv[i++] = path_to_binary;
|
|
|
|
argv[i++] = inline_flag;
|
|
|
|
argv[i++] = kSymbolizerArch;
|
|
|
|
argv[i++] = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LLVMSymbolizer::LLVMSymbolizer(const char *path, LowLevelAllocator *allocator)
|
|
|
|
: symbolizer_process_(new(*allocator) LLVMSymbolizerProcess(path)) {}
|
|
|
|
|
|
|
|
// Parse a <file>:<line>[:<column>] buffer. The file path may contain colons on
|
|
|
|
// Windows, so extract tokens from the right hand side first. The column info is
|
|
|
|
// also optional.
|
|
|
|
static const char *ParseFileLineInfo(AddressInfo *info, const char *str) {
|
2020-02-07 00:44:42 +08:00
|
|
|
char *file_line_info = nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
str = ExtractToken(str, "\n", &file_line_info);
|
|
|
|
CHECK(file_line_info);
|
2016-12-09 06:26:03 +08:00
|
|
|
|
|
|
|
if (uptr size = internal_strlen(file_line_info)) {
|
|
|
|
char *back = file_line_info + size - 1;
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
while (back > file_line_info && IsDigit(*back)) --back;
|
|
|
|
if (*back != ':' || !IsDigit(back[1])) break;
|
|
|
|
info->column = info->line;
|
|
|
|
info->line = internal_atoll(back + 1);
|
|
|
|
// Truncate the string at the colon to keep only filename.
|
|
|
|
*back = '\0';
|
|
|
|
--back;
|
|
|
|
}
|
|
|
|
ExtractToken(file_line_info, "", &info->file);
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
2016-12-09 06:26:03 +08:00
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
InternalFree(file_line_info);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res) {
|
|
|
|
bool top_frame = true;
|
|
|
|
SymbolizedStack *last = res;
|
|
|
|
while (true) {
|
2020-02-07 00:44:42 +08:00
|
|
|
char *function_name = nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
str = ExtractToken(str, "\n", &function_name);
|
|
|
|
CHECK(function_name);
|
|
|
|
if (function_name[0] == '\0') {
|
|
|
|
// There are no more frames.
|
|
|
|
InternalFree(function_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SymbolizedStack *cur;
|
|
|
|
if (top_frame) {
|
|
|
|
cur = res;
|
|
|
|
top_frame = false;
|
|
|
|
} else {
|
|
|
|
cur = SymbolizedStack::New(res->info.address);
|
2017-01-07 05:45:05 +08:00
|
|
|
cur->info.FillModuleInfo(res->info.module, res->info.module_offset,
|
|
|
|
res->info.module_arch);
|
2015-08-11 23:51:40 +08:00
|
|
|
last->next = cur;
|
|
|
|
last = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressInfo *info = &cur->info;
|
|
|
|
info->function = function_name;
|
|
|
|
str = ParseFileLineInfo(info, str);
|
|
|
|
|
|
|
|
// Functions and filenames can be "??", in which case we write 0
|
|
|
|
// to address info to mark that names are unknown.
|
|
|
|
if (0 == internal_strcmp(info->function, "??")) {
|
|
|
|
InternalFree(info->function);
|
|
|
|
info->function = 0;
|
|
|
|
}
|
|
|
|
if (0 == internal_strcmp(info->file, "??")) {
|
|
|
|
InternalFree(info->file);
|
|
|
|
info->file = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
str = ExtractToken(str, "\n", &info->name);
|
|
|
|
str = ExtractUptr(str, " ", &info->start);
|
|
|
|
str = ExtractUptr(str, "\n", &info->size);
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:16:13 +08:00
|
|
|
static void ParseSymbolizeFrameOutput(const char *str,
|
|
|
|
InternalMmapVector<LocalInfo> *locals) {
|
|
|
|
if (internal_strncmp(str, "??", 2) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
LocalInfo local;
|
|
|
|
str = ExtractToken(str, "\n", &local.function_name);
|
|
|
|
str = ExtractToken(str, "\n", &local.name);
|
|
|
|
|
|
|
|
AddressInfo addr;
|
|
|
|
str = ParseFileLineInfo(&addr, str);
|
|
|
|
local.decl_file = addr.file;
|
|
|
|
local.decl_line = addr.line;
|
|
|
|
|
|
|
|
local.has_frame_offset = internal_strncmp(str, "??", 2) != 0;
|
|
|
|
str = ExtractSptr(str, " ", &local.frame_offset);
|
|
|
|
|
|
|
|
local.has_size = internal_strncmp(str, "??", 2) != 0;
|
|
|
|
str = ExtractUptr(str, " ", &local.size);
|
|
|
|
|
|
|
|
local.has_tag_offset = internal_strncmp(str, "??", 2) != 0;
|
|
|
|
str = ExtractUptr(str, "\n", &local.tag_offset);
|
|
|
|
|
|
|
|
locals->push_back(local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
bool LLVMSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
2017-01-07 05:45:05 +08:00
|
|
|
AddressInfo *info = &stack->info;
|
|
|
|
const char *buf = FormatAndSendCommand(
|
2019-06-28 07:16:13 +08:00
|
|
|
"CODE", info->module, info->module_offset, info->module_arch);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (!buf)
|
|
|
|
return false;
|
|
|
|
ParseSymbolizePCOutput(buf, stack);
|
|
|
|
return true;
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LLVMSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
|
2017-01-07 05:45:05 +08:00
|
|
|
const char *buf = FormatAndSendCommand(
|
2019-06-28 07:16:13 +08:00
|
|
|
"DATA", info->module, info->module_offset, info->module_arch);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (!buf)
|
|
|
|
return false;
|
|
|
|
ParseSymbolizeDataOutput(buf, info);
|
|
|
|
info->start += (addr - info->module_offset); // Add the base address.
|
|
|
|
return true;
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
|
|
|
|
2019-06-28 07:16:13 +08:00
|
|
|
bool LLVMSymbolizer::SymbolizeFrame(uptr addr, FrameInfo *info) {
|
|
|
|
const char *buf = FormatAndSendCommand(
|
|
|
|
"FRAME", info->module, info->module_offset, info->module_arch);
|
2020-02-07 00:44:42 +08:00
|
|
|
if (!buf)
|
|
|
|
return false;
|
|
|
|
ParseSymbolizeFrameOutput(buf, &info->locals);
|
|
|
|
return true;
|
2019-06-28 07:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *LLVMSymbolizer::FormatAndSendCommand(const char *command_prefix,
|
2017-01-07 05:45:05 +08:00
|
|
|
const char *module_name,
|
|
|
|
uptr module_offset,
|
|
|
|
ModuleArch arch) {
|
2015-08-11 23:51:40 +08:00
|
|
|
CHECK(module_name);
|
2020-02-07 00:44:42 +08:00
|
|
|
int size_needed = 0;
|
|
|
|
if (arch == kModuleArchUnknown)
|
|
|
|
size_needed = internal_snprintf(buffer_, kBufferSize, "%s \"%s\" 0x%zx\n",
|
|
|
|
command_prefix, module_name, module_offset);
|
|
|
|
else
|
|
|
|
size_needed = internal_snprintf(buffer_, kBufferSize,
|
|
|
|
"%s \"%s:%s\" 0x%zx\n", command_prefix,
|
|
|
|
module_name, ModuleArchToString(arch),
|
|
|
|
module_offset);
|
|
|
|
|
|
|
|
if (size_needed >= static_cast<int>(kBufferSize)) {
|
|
|
|
Report("WARNING: Command buffer too small");
|
|
|
|
return nullptr;
|
2017-01-07 05:45:05 +08:00
|
|
|
}
|
2020-02-07 00:44:42 +08:00
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
return symbolizer_process_->SendCommand(buffer_);
|
|
|
|
}
|
|
|
|
|
2019-08-15 08:18:55 +08:00
|
|
|
SymbolizerProcess::SymbolizerProcess(const char *path, bool use_posix_spawn)
|
2015-08-11 23:51:40 +08:00
|
|
|
: path_(path),
|
|
|
|
input_fd_(kInvalidFd),
|
|
|
|
output_fd_(kInvalidFd),
|
|
|
|
times_restarted_(0),
|
|
|
|
failed_to_start_(false),
|
|
|
|
reported_invalid_path_(false),
|
2019-08-15 08:18:55 +08:00
|
|
|
use_posix_spawn_(use_posix_spawn) {
|
2015-08-11 23:51:40 +08:00
|
|
|
CHECK(path_);
|
|
|
|
CHECK_NE(path_[0], '\0');
|
|
|
|
}
|
|
|
|
|
2017-02-22 05:39:24 +08:00
|
|
|
static bool IsSameModule(const char* path) {
|
|
|
|
if (const char* ProcessName = GetProcessName()) {
|
|
|
|
if (const char* SymbolizerName = StripModuleName(path)) {
|
|
|
|
return !internal_strcmp(ProcessName, SymbolizerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
const char *SymbolizerProcess::SendCommand(const char *command) {
|
2017-02-22 05:39:24 +08:00
|
|
|
if (failed_to_start_)
|
|
|
|
return nullptr;
|
|
|
|
if (IsSameModule(path_)) {
|
|
|
|
Report("WARNING: Symbolizer was blocked from starting itself!\n");
|
|
|
|
failed_to_start_ = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-11 23:51:40 +08:00
|
|
|
for (; times_restarted_ < kMaxTimesRestarted; times_restarted_++) {
|
|
|
|
// Start or restart symbolizer if we failed to send command to it.
|
|
|
|
if (const char *res = SendCommandImpl(command))
|
|
|
|
return res;
|
|
|
|
Restart();
|
|
|
|
}
|
|
|
|
if (!failed_to_start_) {
|
|
|
|
Report("WARNING: Failed to use and restart external symbolizer!\n");
|
|
|
|
failed_to_start_ = true;
|
|
|
|
}
|
2020-02-07 00:44:42 +08:00
|
|
|
return nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *SymbolizerProcess::SendCommandImpl(const char *command) {
|
|
|
|
if (input_fd_ == kInvalidFd || output_fd_ == kInvalidFd)
|
2020-02-07 00:44:42 +08:00
|
|
|
return nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
if (!WriteToSymbolizer(command, internal_strlen(command)))
|
2020-02-07 00:44:42 +08:00
|
|
|
return nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
if (!ReadFromSymbolizer(buffer_, kBufferSize))
|
2020-02-07 00:44:42 +08:00
|
|
|
return nullptr;
|
2015-08-11 23:51:40 +08:00
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolizerProcess::Restart() {
|
|
|
|
if (input_fd_ != kInvalidFd)
|
|
|
|
CloseFile(input_fd_);
|
|
|
|
if (output_fd_ != kInvalidFd)
|
|
|
|
CloseFile(output_fd_);
|
|
|
|
return StartSymbolizerSubprocess();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolizerProcess::ReadFromSymbolizer(char *buffer, uptr max_length) {
|
|
|
|
if (max_length == 0)
|
|
|
|
return true;
|
|
|
|
uptr read_len = 0;
|
|
|
|
while (true) {
|
|
|
|
uptr just_read = 0;
|
|
|
|
bool success = ReadFromFile(input_fd_, buffer + read_len,
|
|
|
|
max_length - read_len - 1, &just_read);
|
|
|
|
// We can't read 0 bytes, as we don't expect external symbolizer to close
|
|
|
|
// its stdout.
|
|
|
|
if (!success || just_read == 0) {
|
|
|
|
Report("WARNING: Can't read from symbolizer at fd %d\n", input_fd_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
read_len += just_read;
|
|
|
|
if (ReachedEndOfOutput(buffer, read_len))
|
|
|
|
break;
|
2017-02-03 04:10:07 +08:00
|
|
|
if (read_len + 1 == max_length) {
|
2017-08-05 02:39:36 +08:00
|
|
|
Report("WARNING: Symbolizer buffer too small\n");
|
2020-02-11 02:19:03 +08:00
|
|
|
read_len = 0;
|
|
|
|
break;
|
2017-02-03 04:10:07 +08:00
|
|
|
}
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
|
|
|
buffer[read_len] = '\0';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolizerProcess::WriteToSymbolizer(const char *buffer, uptr length) {
|
|
|
|
if (length == 0)
|
|
|
|
return true;
|
|
|
|
uptr write_len = 0;
|
|
|
|
bool success = WriteToFile(output_fd_, buffer, length, &write_len);
|
|
|
|
if (!success || write_len != length) {
|
|
|
|
Report("WARNING: Can't write to symbolizer at fd %d\n", output_fd_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-18 08:43:54 +08:00
|
|
|
#endif // !SANITIZER_SYMBOLIZER_MARKUP
|
2017-08-02 06:54:51 +08:00
|
|
|
|
2013-10-26 07:03:29 +08:00
|
|
|
} // namespace __sanitizer
|