2015-03-12 18:53:18 +08:00
|
|
|
//===-- sanitizer_symbolizer_mac.cc ---------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between various sanitizers' runtime libraries.
|
|
|
|
//
|
|
|
|
// Implementation of Mac-specific "atos" symbolizer.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
#if SANITIZER_MAC
|
|
|
|
|
|
|
|
#include "sanitizer_allocator_internal.h"
|
2015-03-25 19:01:33 +08:00
|
|
|
#include "sanitizer_mac.h"
|
2015-03-12 18:53:18 +08:00
|
|
|
#include "sanitizer_symbolizer_mac.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
|
|
|
Dl_info info;
|
|
|
|
int result = dladdr((const void *)addr, &info);
|
|
|
|
if (!result) return false;
|
2015-03-22 19:38:55 +08:00
|
|
|
const char *demangled = DemangleCXXABI(info.dli_sname);
|
2015-11-25 21:11:38 +08:00
|
|
|
stack->info.function = demangled ? internal_strdup(demangled) : nullptr;
|
2015-03-12 18:53:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-12 23:19:40 +08:00
|
|
|
bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
|
|
|
|
Dl_info info;
|
|
|
|
int result = dladdr((const void *)addr, &info);
|
|
|
|
if (!result) return false;
|
|
|
|
const char *demangled = DemangleCXXABI(info.dli_sname);
|
|
|
|
datainfo->name = internal_strdup(demangled);
|
|
|
|
datainfo->start = (uptr)info.dli_saddr;
|
|
|
|
return true;
|
2015-03-12 18:53:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AtosSymbolizerProcess : public SymbolizerProcess {
|
|
|
|
public:
|
|
|
|
explicit AtosSymbolizerProcess(const char *path, pid_t parent_pid)
|
2015-08-11 23:51:40 +08:00
|
|
|
: SymbolizerProcess(path, /*use_forkpty*/ true) {
|
|
|
|
// Put the string command line argument in the object so that it outlives
|
|
|
|
// the call to GetArgV.
|
2015-08-12 00:30:27 +08:00
|
|
|
internal_snprintf(pid_str_, sizeof(pid_str_), "%d", parent_pid);
|
2015-08-11 23:51:40 +08:00
|
|
|
}
|
2015-03-12 18:53:18 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
|
|
|
|
return (length >= 1 && buffer[length - 1] == '\n');
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
void GetArgV(const char *path_to_binary,
|
|
|
|
const char *(&argv)[kArgVMax]) const override {
|
|
|
|
int i = 0;
|
|
|
|
argv[i++] = path_to_binary;
|
|
|
|
argv[i++] = "-p";
|
|
|
|
argv[i++] = &pid_str_[0];
|
2015-03-25 19:01:33 +08:00
|
|
|
if (GetMacosVersion() == MACOS_VERSION_MAVERICKS) {
|
|
|
|
// On Mavericks atos prints a deprecation warning which we suppress by
|
|
|
|
// passing -d. The warning isn't present on other OSX versions, even the
|
|
|
|
// newer ones.
|
2015-08-11 23:51:40 +08:00
|
|
|
argv[i++] = "-d";
|
2015-03-25 19:01:33 +08:00
|
|
|
}
|
2015-08-11 23:51:40 +08:00
|
|
|
argv[i++] = nullptr;
|
2015-03-12 18:53:18 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 23:51:40 +08:00
|
|
|
char pid_str_[16];
|
2015-03-12 18:53:18 +08:00
|
|
|
};
|
|
|
|
|
2015-11-13 14:45:49 +08:00
|
|
|
static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
|
|
|
|
char **out_module, char **out_file, uptr *line,
|
|
|
|
uptr *start_address) {
|
2015-03-12 18:53:18 +08:00
|
|
|
// Trim ending newlines.
|
|
|
|
char *trim;
|
|
|
|
ExtractTokenUpToDelimiter(str, "\n", &trim);
|
|
|
|
|
|
|
|
// The line from `atos` is in one of these formats:
|
|
|
|
// myfunction (in library.dylib) (sourcefile.c:17)
|
|
|
|
// myfunction (in library.dylib) + 0x1fe
|
2015-11-13 14:45:49 +08:00
|
|
|
// myfunction (in library.dylib) + 15
|
2015-03-12 18:53:18 +08:00
|
|
|
// 0xdeadbeef (in library.dylib) + 0x1fe
|
2015-11-13 14:45:49 +08:00
|
|
|
// 0xdeadbeef (in library.dylib) + 15
|
2015-03-12 18:53:18 +08:00
|
|
|
// 0xdeadbeef (in library.dylib)
|
|
|
|
// 0xdeadbeef
|
|
|
|
|
|
|
|
const char *rest = trim;
|
2015-11-13 14:45:49 +08:00
|
|
|
char *symbol_name;
|
|
|
|
rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
|
2015-11-30 22:12:47 +08:00
|
|
|
if (rest[0] == '\0') {
|
|
|
|
InternalFree(symbol_name);
|
|
|
|
InternalFree(trim);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:45:49 +08:00
|
|
|
if (internal_strncmp(symbol_name, "0x", 2) != 0)
|
|
|
|
*out_name = symbol_name;
|
2015-03-12 18:53:18 +08:00
|
|
|
else
|
2015-11-13 14:45:49 +08:00
|
|
|
InternalFree(symbol_name);
|
|
|
|
rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
|
2015-03-12 18:53:18 +08:00
|
|
|
|
|
|
|
if (rest[0] == '(') {
|
2015-11-13 14:45:49 +08:00
|
|
|
if (out_file) {
|
|
|
|
rest++;
|
|
|
|
rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
|
|
|
|
char *extracted_line_number;
|
|
|
|
rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
|
|
|
|
if (line) *line = (uptr)internal_atoll(extracted_line_number);
|
|
|
|
InternalFree(extracted_line_number);
|
|
|
|
}
|
|
|
|
} else if (rest[0] == '+') {
|
|
|
|
rest += 2;
|
|
|
|
uptr offset = internal_atoll(rest);
|
|
|
|
if (start_address) *start_address = addr - offset;
|
2015-03-12 18:53:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InternalFree(trim);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
|
|
|
|
: process_(new(*allocator) AtosSymbolizerProcess(path, getpid())) {}
|
|
|
|
|
|
|
|
bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
|
|
|
if (!process_) return false;
|
2016-04-04 03:13:03 +08:00
|
|
|
if (addr == 0) return false;
|
2015-03-12 18:53:18 +08:00
|
|
|
char command[32];
|
|
|
|
internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
|
|
|
|
const char *buf = process_->SendCommand(command);
|
|
|
|
if (!buf) return false;
|
2015-11-13 14:45:49 +08:00
|
|
|
uptr line;
|
|
|
|
if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
|
|
|
|
&stack->info.file, &line, nullptr)) {
|
2015-03-12 18:53:18 +08:00
|
|
|
process_ = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-13 14:45:49 +08:00
|
|
|
stack->info.line = (int)line;
|
2015-03-12 18:53:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:45:49 +08:00
|
|
|
bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
|
|
|
|
if (!process_) return false;
|
|
|
|
char command[32];
|
|
|
|
internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
|
|
|
|
const char *buf = process_->SendCommand(command);
|
|
|
|
if (!buf) return false;
|
|
|
|
if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
|
|
|
|
nullptr, &info->start)) {
|
|
|
|
process_ = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-12 18:53:18 +08:00
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_MAC
|