2013-11-07 14:33:06 +08:00
|
|
|
//===-- sanitizer_stacktrace_libcdep.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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-12-25 17:29:54 +08:00
|
|
|
#include "sanitizer_common.h"
|
2014-11-06 06:44:36 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
2013-11-07 14:33:06 +08:00
|
|
|
#include "sanitizer_stacktrace.h"
|
2014-11-06 06:44:36 +08:00
|
|
|
#include "sanitizer_stacktrace_printer.h"
|
2013-12-25 17:29:54 +08:00
|
|
|
#include "sanitizer_symbolizer.h"
|
2013-11-07 14:33:06 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void StackTrace::Print() const {
|
|
|
|
if (trace == nullptr || size == 0) {
|
2013-12-25 17:29:54 +08:00
|
|
|
Printf(" <empty stack>\n\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InternalScopedString frame_desc(GetPageSizeCached() * 2);
|
2016-05-28 09:25:44 +08:00
|
|
|
InternalScopedString dedup_token(GetPageSizeCached());
|
|
|
|
int dedup_frames = common_flags()->dedup_token_length;
|
2013-12-25 17:29:54 +08:00
|
|
|
uptr frame_num = 0;
|
2014-10-26 11:35:14 +08:00
|
|
|
for (uptr i = 0; i < size && trace[i]; i++) {
|
2013-12-25 17:29:54 +08:00
|
|
|
// PCs in stack traces are actually the return addresses, that is,
|
|
|
|
// addresses of the next instructions after the call.
|
2014-10-26 11:35:14 +08:00
|
|
|
uptr pc = GetPreviousInstructionPc(trace[i]);
|
2014-12-03 03:48:40 +08:00
|
|
|
SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(pc);
|
|
|
|
CHECK(frames);
|
|
|
|
for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
|
2013-12-25 17:29:54 +08:00
|
|
|
frame_desc.clear();
|
2014-11-07 02:43:45 +08:00
|
|
|
RenderFrame(&frame_desc, common_flags()->stack_trace_format, frame_num++,
|
2015-06-04 09:20:06 +08:00
|
|
|
cur->info, common_flags()->symbolize_vs_style,
|
|
|
|
common_flags()->strip_path_prefix);
|
2013-12-25 17:29:54 +08:00
|
|
|
Printf("%s\n", frame_desc.data());
|
2016-05-28 09:25:44 +08:00
|
|
|
if (dedup_frames-- > 0) {
|
|
|
|
if (dedup_token.length())
|
|
|
|
dedup_token.append("--");
|
|
|
|
dedup_token.append(cur->info.function);
|
|
|
|
}
|
2013-12-25 17:29:54 +08:00
|
|
|
}
|
2014-12-03 03:48:40 +08:00
|
|
|
frames->ClearAll();
|
2013-12-25 17:29:54 +08:00
|
|
|
}
|
|
|
|
// Always print a trailing empty line after stack trace.
|
|
|
|
Printf("\n");
|
2016-05-28 09:25:44 +08:00
|
|
|
if (dedup_token.length())
|
|
|
|
Printf("DEDUP_TOKEN: %s\n", dedup_token.data());
|
2013-12-25 17:29:54 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 21:33:16 +08:00
|
|
|
void BufferedStackTrace::Unwind(u32 max_depth, uptr pc, uptr bp, void *context,
|
2014-10-26 11:35:14 +08:00
|
|
|
uptr stack_top, uptr stack_bottom,
|
|
|
|
bool request_fast_unwind) {
|
2014-03-04 20:21:28 +08:00
|
|
|
top_frame_bp = (max_depth > 0) ? bp : 0;
|
|
|
|
// Avoid doing any work for small max_depth.
|
|
|
|
if (max_depth == 0) {
|
|
|
|
size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (max_depth == 1) {
|
|
|
|
size = 1;
|
2014-10-26 11:35:14 +08:00
|
|
|
trace_buffer[0] = pc;
|
2014-03-04 20:21:28 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-02-11 21:38:57 +08:00
|
|
|
if (!WillUseFastUnwind(request_fast_unwind)) {
|
2015-07-02 21:56:37 +08:00
|
|
|
#if SANITIZER_CAN_SLOW_UNWIND
|
2014-02-11 21:38:57 +08:00
|
|
|
if (context)
|
|
|
|
SlowUnwindStackWithContext(pc, context, max_depth);
|
|
|
|
else
|
|
|
|
SlowUnwindStack(pc, max_depth);
|
2015-07-02 21:56:37 +08:00
|
|
|
#else
|
|
|
|
UNREACHABLE("slow unwind requested but not available");
|
|
|
|
#endif
|
2014-02-11 21:45:01 +08:00
|
|
|
} else {
|
2013-11-07 14:33:06 +08:00
|
|
|
FastUnwindStack(pc, bp, stack_top, stack_bottom, max_depth);
|
2014-02-11 21:38:57 +08:00
|
|
|
}
|
2013-11-07 14:33:06 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 05:45:14 +08:00
|
|
|
static int GetModuleAndOffsetForPc(uptr pc, char *module_name,
|
|
|
|
uptr module_name_len, uptr *pc_offset) {
|
|
|
|
const char *found_module_name = nullptr;
|
|
|
|
bool ok = Symbolizer::GetOrInit()->GetModuleNameAndOffsetForPC(
|
|
|
|
pc, &found_module_name, pc_offset);
|
|
|
|
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
if (module_name && module_name_len) {
|
|
|
|
internal_strncpy(module_name, found_module_name, module_name_len);
|
|
|
|
module_name[module_name_len - 1] = '\x00';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-07 14:33:06 +08:00
|
|
|
} // namespace __sanitizer
|
2016-09-19 13:10:32 +08:00
|
|
|
using namespace __sanitizer;
|
2016-08-26 05:35:29 +08:00
|
|
|
|
|
|
|
extern "C" {
|
2016-09-16 06:18:36 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2016-09-19 13:10:32 +08:00
|
|
|
void __sanitizer_symbolize_pc(uptr pc, const char *fmt, char *out_buf,
|
|
|
|
uptr out_buf_size) {
|
2016-09-09 10:13:27 +08:00
|
|
|
if (!out_buf_size) return;
|
2016-08-26 05:35:29 +08:00
|
|
|
pc = StackTrace::GetPreviousInstructionPc(pc);
|
|
|
|
SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc);
|
|
|
|
if (!frame) {
|
|
|
|
internal_strncpy(out_buf, "<can't symbolize>", out_buf_size);
|
2016-09-09 10:13:27 +08:00
|
|
|
out_buf[out_buf_size - 1] = 0;
|
2016-08-26 05:35:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
InternalScopedString frame_desc(GetPageSizeCached());
|
|
|
|
RenderFrame(&frame_desc, fmt, 0, frame->info,
|
|
|
|
common_flags()->symbolize_vs_style,
|
|
|
|
common_flags()->strip_path_prefix);
|
|
|
|
internal_strncpy(out_buf, frame_desc.data(), out_buf_size);
|
2016-09-09 10:13:27 +08:00
|
|
|
out_buf[out_buf_size - 1] = 0;
|
2016-08-26 05:35:29 +08:00
|
|
|
}
|
2016-09-19 13:10:32 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2016-09-19 22:18:16 +08:00
|
|
|
void __sanitizer_symbolize_global(uptr data_addr, const char *fmt,
|
|
|
|
char *out_buf, uptr out_buf_size) {
|
2016-09-19 13:10:32 +08:00
|
|
|
if (!out_buf_size) return;
|
|
|
|
out_buf[0] = 0;
|
|
|
|
DataInfo DI;
|
|
|
|
if (!Symbolizer::GetOrInit()->SymbolizeData(data_addr, &DI)) return;
|
|
|
|
InternalScopedString data_desc(GetPageSizeCached());
|
|
|
|
RenderData(&data_desc, fmt, &DI, common_flags()->strip_path_prefix);
|
|
|
|
internal_strncpy(out_buf, data_desc.data(), out_buf_size);
|
|
|
|
out_buf[out_buf_size - 1] = 0;
|
|
|
|
}
|
2016-12-06 05:45:14 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
int __sanitizer_get_module_and_offset_for_pc( // NOLINT
|
|
|
|
uptr pc, char *module_name, uptr module_name_len, uptr *pc_offset) {
|
|
|
|
return __sanitizer::GetModuleAndOffsetForPc(pc, module_name, module_name_len,
|
|
|
|
pc_offset);
|
|
|
|
}
|
2016-08-26 05:35:29 +08:00
|
|
|
} // extern "C"
|