2014-01-16 21:48:15 +08:00
|
|
|
//===-- sanitizer_symbolizer_libbacktrace.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 AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
// Libbacktrace implementation of symbolizer parts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
|
|
|
|
#include "sanitizer_internal_defs.h"
|
|
|
|
#include "sanitizer_symbolizer.h"
|
|
|
|
#include "sanitizer_symbolizer_libbacktrace.h"
|
|
|
|
|
|
|
|
#if SANITIZER_LIBBACKTRACE
|
|
|
|
# include "backtrace-supported.h"
|
|
|
|
# if SANITIZER_POSIX && BACKTRACE_SUPPORTED && !BACKTRACE_USES_MALLOC
|
|
|
|
# include "backtrace.h"
|
|
|
|
# if SANITIZER_CP_DEMANGLE
|
|
|
|
# undef ARRAY_SIZE
|
|
|
|
# include "demangle.h"
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define SANITIZER_LIBBACKTRACE 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2015-03-03 06:56:25 +08:00
|
|
|
static char *DemangleAlloc(const char *name, bool always_alloc);
|
|
|
|
|
2014-01-16 21:48:15 +08:00
|
|
|
#if SANITIZER_LIBBACKTRACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
# if SANITIZER_CP_DEMANGLE
|
|
|
|
struct CplusV3DemangleData {
|
|
|
|
char *buf;
|
|
|
|
uptr size, allocated;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
static void CplusV3DemangleCallback(const char *s, size_t l, void *vdata) {
|
|
|
|
CplusV3DemangleData *data = (CplusV3DemangleData *)vdata;
|
|
|
|
uptr needed = data->size + l + 1;
|
|
|
|
if (needed > data->allocated) {
|
|
|
|
data->allocated *= 2;
|
|
|
|
if (needed > data->allocated)
|
|
|
|
data->allocated = needed;
|
|
|
|
char *buf = (char *)InternalAlloc(data->allocated);
|
|
|
|
if (data->buf) {
|
|
|
|
internal_memcpy(buf, data->buf, data->size);
|
|
|
|
InternalFree(data->buf);
|
|
|
|
}
|
|
|
|
data->buf = buf;
|
|
|
|
}
|
|
|
|
internal_memcpy(data->buf + data->size, s, l);
|
|
|
|
data->buf[data->size + l] = '\0';
|
|
|
|
data->size += l;
|
|
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
char *CplusV3Demangle(const char *name) {
|
|
|
|
CplusV3DemangleData data;
|
|
|
|
data.buf = 0;
|
|
|
|
data.size = 0;
|
|
|
|
data.allocated = 0;
|
|
|
|
if (cplus_demangle_v3_callback(name, DMGL_PARAMS | DMGL_ANSI,
|
|
|
|
CplusV3DemangleCallback, &data)) {
|
|
|
|
if (data.size + 64 > data.allocated)
|
|
|
|
return data.buf;
|
|
|
|
char *buf = internal_strdup(data.buf);
|
|
|
|
InternalFree(data.buf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
if (data.buf)
|
|
|
|
InternalFree(data.buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
# endif // SANITIZER_CP_DEMANGLE
|
|
|
|
|
2014-12-03 03:48:40 +08:00
|
|
|
struct SymbolizeCodeCallbackArg {
|
|
|
|
SymbolizedStack *first;
|
|
|
|
SymbolizedStack *last;
|
2015-03-03 06:56:25 +08:00
|
|
|
uptr frames_symbolized;
|
|
|
|
|
|
|
|
AddressInfo *get_new_frame(uintptr_t addr) {
|
|
|
|
CHECK(last);
|
|
|
|
if (frames_symbolized > 0) {
|
|
|
|
SymbolizedStack *cur = SymbolizedStack::New(addr);
|
|
|
|
AddressInfo *info = &cur->info;
|
2017-11-08 04:10:17 +08:00
|
|
|
info->FillModuleInfo(first->info.module, first->info.module_offset,
|
|
|
|
first->info.module_arch);
|
2015-03-03 06:56:25 +08:00
|
|
|
last->next = cur;
|
|
|
|
last = cur;
|
2014-12-03 03:48:40 +08:00
|
|
|
}
|
2015-03-03 06:56:25 +08:00
|
|
|
CHECK_EQ(addr, first->info.address);
|
|
|
|
CHECK_EQ(addr, last->info.address);
|
|
|
|
return &last->info;
|
2014-12-03 03:48:40 +08:00
|
|
|
}
|
2014-01-16 21:48:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
static int SymbolizeCodePCInfoCallback(void *vdata, uintptr_t addr,
|
|
|
|
const char *filename, int lineno,
|
|
|
|
const char *function) {
|
2014-12-03 03:48:40 +08:00
|
|
|
SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
|
2014-01-16 21:48:15 +08:00
|
|
|
if (function) {
|
2015-03-03 06:56:25 +08:00
|
|
|
AddressInfo *info = cdata->get_new_frame(addr);
|
|
|
|
info->function = DemangleAlloc(function, /*always_alloc*/ true);
|
2014-01-16 21:48:15 +08:00
|
|
|
if (filename)
|
|
|
|
info->file = internal_strdup(filename);
|
|
|
|
info->line = lineno;
|
2015-03-03 06:56:25 +08:00
|
|
|
cdata->frames_symbolized++;
|
2014-01-16 21:48:15 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SymbolizeCodeCallback(void *vdata, uintptr_t addr,
|
|
|
|
const char *symname, uintptr_t, uintptr_t) {
|
2014-12-03 03:48:40 +08:00
|
|
|
SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
|
2014-01-16 21:48:15 +08:00
|
|
|
if (symname) {
|
2015-03-03 06:56:25 +08:00
|
|
|
AddressInfo *info = cdata->get_new_frame(addr);
|
|
|
|
info->function = DemangleAlloc(symname, /*always_alloc*/ true);
|
|
|
|
cdata->frames_symbolized++;
|
2014-01-16 21:48:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SymbolizeDataCallback(void *vdata, uintptr_t, const char *symname,
|
|
|
|
uintptr_t symval, uintptr_t symsize) {
|
|
|
|
DataInfo *info = (DataInfo *)vdata;
|
|
|
|
if (symname && symval) {
|
2015-03-03 06:56:25 +08:00
|
|
|
info->name = DemangleAlloc(symname, /*always_alloc*/ true);
|
2014-01-16 21:48:15 +08:00
|
|
|
info->start = symval;
|
|
|
|
info->size = symsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ErrorCallback(void *, const char *, int) {}
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
LibbacktraceSymbolizer *LibbacktraceSymbolizer::get(LowLevelAllocator *alloc) {
|
|
|
|
// State created in backtrace_create_state is leaked.
|
|
|
|
void *state = (void *)(backtrace_create_state("/proc/self/exe", 0,
|
|
|
|
ErrorCallback, NULL));
|
|
|
|
if (!state)
|
|
|
|
return 0;
|
|
|
|
return new(*alloc) LibbacktraceSymbolizer(state);
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:56:25 +08:00
|
|
|
bool LibbacktraceSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
2014-12-03 03:48:40 +08:00
|
|
|
SymbolizeCodeCallbackArg data;
|
2015-03-03 06:56:25 +08:00
|
|
|
data.first = stack;
|
|
|
|
data.last = stack;
|
|
|
|
data.frames_symbolized = 0;
|
2014-01-16 21:48:15 +08:00
|
|
|
backtrace_pcinfo((backtrace_state *)state_, addr, SymbolizeCodePCInfoCallback,
|
|
|
|
ErrorCallback, &data);
|
2015-03-03 06:56:25 +08:00
|
|
|
if (data.frames_symbolized > 0)
|
|
|
|
return true;
|
2014-01-16 21:48:15 +08:00
|
|
|
backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeCodeCallback,
|
|
|
|
ErrorCallback, &data);
|
2015-03-03 06:56:25 +08:00
|
|
|
return (data.frames_symbolized > 0);
|
2014-01-16 21:48:15 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 09:52:55 +08:00
|
|
|
bool LibbacktraceSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
|
|
|
|
backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeDataCallback,
|
|
|
|
ErrorCallback, info);
|
2014-01-16 21:48:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // SANITIZER_LIBBACKTRACE
|
|
|
|
|
|
|
|
LibbacktraceSymbolizer *LibbacktraceSymbolizer::get(LowLevelAllocator *alloc) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:56:25 +08:00
|
|
|
bool LibbacktraceSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
|
2014-01-16 21:48:15 +08:00
|
|
|
(void)state_;
|
2015-03-03 06:56:25 +08:00
|
|
|
return false;
|
2014-01-16 21:48:15 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 09:52:55 +08:00
|
|
|
bool LibbacktraceSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
|
2014-01-16 21:48:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SANITIZER_LIBBACKTRACE
|
|
|
|
|
2015-03-03 06:56:25 +08:00
|
|
|
static char *DemangleAlloc(const char *name, bool always_alloc) {
|
2014-01-16 21:48:15 +08:00
|
|
|
#if SANITIZER_LIBBACKTRACE && SANITIZER_CP_DEMANGLE
|
|
|
|
if (char *demangled = CplusV3Demangle(name))
|
|
|
|
return demangled;
|
|
|
|
#endif
|
|
|
|
if (always_alloc)
|
|
|
|
return internal_strdup(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:56:25 +08:00
|
|
|
const char *LibbacktraceSymbolizer::Demangle(const char *name) {
|
|
|
|
return DemangleAlloc(name, /*always_alloc*/ false);
|
|
|
|
}
|
|
|
|
|
2014-01-16 21:48:15 +08:00
|
|
|
} // namespace __sanitizer
|