2012-08-28 22:27:06 +08:00
|
|
|
//===-- sanitizer_stacktrace.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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-28 22:48:28 +08:00
|
|
|
#include "sanitizer_common.h"
|
2013-10-04 16:55:03 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2012-08-28 22:48:28 +08:00
|
|
|
#include "sanitizer_procmaps.h"
|
|
|
|
#include "sanitizer_stacktrace.h"
|
|
|
|
#include "sanitizer_symbolizer.h"
|
2012-08-28 22:27:06 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-12-18 17:57:34 +08:00
|
|
|
uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
|
2012-08-28 22:27:06 +08:00
|
|
|
#ifdef __arm__
|
|
|
|
// Cancel Thumb bit.
|
|
|
|
pc = pc & (~1);
|
|
|
|
#endif
|
2012-11-20 15:00:42 +08:00
|
|
|
#if defined(__powerpc__) || defined(__powerpc64__)
|
|
|
|
// PCs are always 4 byte aligned.
|
|
|
|
return pc - 4;
|
2012-12-04 02:39:21 +08:00
|
|
|
#elif defined(__sparc__)
|
|
|
|
return pc - 8;
|
2012-11-20 15:00:42 +08:00
|
|
|
#else
|
2012-08-28 22:27:06 +08:00
|
|
|
return pc - 1;
|
2012-11-20 15:00:42 +08:00
|
|
|
#endif
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
|
2012-10-02 20:11:17 +08:00
|
|
|
static void PrintStackFramePrefix(uptr frame_num, uptr pc) {
|
|
|
|
Printf(" #%zu 0x%zx", frame_num, pc);
|
|
|
|
}
|
|
|
|
|
2013-10-29 13:31:25 +08:00
|
|
|
void StackTrace::PrintStack(const uptr *addr, uptr size,
|
2013-10-04 16:55:03 +08:00
|
|
|
SymbolizeCallback symbolize_callback) {
|
2013-11-01 08:19:46 +08:00
|
|
|
if (addr == 0) {
|
|
|
|
Printf("<empty stack>\n\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-03-26 18:34:37 +08:00
|
|
|
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
|
2012-11-24 13:03:11 +08:00
|
|
|
InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
|
2012-08-29 16:40:36 +08:00
|
|
|
InternalScopedBuffer<AddressInfo> addr_frames(64);
|
2012-08-28 22:27:06 +08:00
|
|
|
uptr frame_num = 0;
|
|
|
|
for (uptr i = 0; i < size && addr[i]; i++) {
|
2012-12-18 17:57:34 +08:00
|
|
|
// PCs in stack traces are actually the return addresses, that is,
|
|
|
|
// addresses of the next instructions after the call.
|
|
|
|
uptr pc = GetPreviousInstructionPc(addr[i]);
|
2012-10-02 20:11:17 +08:00
|
|
|
uptr addr_frames_num = 0; // The number of stack frames for current
|
|
|
|
// instruction address.
|
2012-08-28 22:27:06 +08:00
|
|
|
if (symbolize_callback) {
|
2012-10-02 20:11:17 +08:00
|
|
|
if (symbolize_callback((void*)pc, buff.data(), buff.size())) {
|
|
|
|
addr_frames_num = 1;
|
|
|
|
PrintStackFramePrefix(frame_num, pc);
|
|
|
|
// We can't know anything about the string returned by external
|
|
|
|
// symbolizer, but if it starts with filename, try to strip path prefix
|
|
|
|
// from it.
|
2013-10-04 16:55:03 +08:00
|
|
|
Printf(" %s\n",
|
|
|
|
StripPathPrefix(buff.data(), common_flags()->strip_path_prefix));
|
2012-10-02 20:11:17 +08:00
|
|
|
frame_num++;
|
|
|
|
}
|
2012-10-18 19:46:22 +08:00
|
|
|
}
|
2013-10-29 13:31:25 +08:00
|
|
|
if (common_flags()->symbolize && addr_frames_num == 0) {
|
2012-10-02 20:11:17 +08:00
|
|
|
// Use our own (online) symbolizer, if necessary.
|
2013-10-26 07:03:29 +08:00
|
|
|
if (Symbolizer *sym = Symbolizer::GetOrNull())
|
|
|
|
addr_frames_num =
|
|
|
|
sym->SymbolizeCode(pc, addr_frames.data(), addr_frames.size());
|
2012-08-28 22:27:06 +08:00
|
|
|
for (uptr j = 0; j < addr_frames_num; j++) {
|
|
|
|
AddressInfo &info = addr_frames[j];
|
2012-10-02 20:11:17 +08:00
|
|
|
PrintStackFramePrefix(frame_num, pc);
|
2012-08-28 22:27:06 +08:00
|
|
|
if (info.function) {
|
|
|
|
Printf(" in %s", info.function);
|
|
|
|
}
|
|
|
|
if (info.file) {
|
2013-10-04 16:55:03 +08:00
|
|
|
Printf(" ");
|
|
|
|
PrintSourceLocation(info.file, info.line, info.column);
|
2012-08-28 22:27:06 +08:00
|
|
|
} else if (info.module) {
|
2013-10-04 16:55:03 +08:00
|
|
|
Printf(" ");
|
|
|
|
PrintModuleAndOffset(info.module, info.module_offset);
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
Printf("\n");
|
|
|
|
info.Clear();
|
|
|
|
frame_num++;
|
|
|
|
}
|
2012-10-02 20:11:17 +08:00
|
|
|
}
|
|
|
|
if (addr_frames_num == 0) {
|
|
|
|
// If online symbolization failed, try to output at least module and
|
|
|
|
// offset for instruction.
|
|
|
|
PrintStackFramePrefix(frame_num, pc);
|
2012-08-28 22:27:06 +08:00
|
|
|
uptr offset;
|
|
|
|
if (proc_maps.GetObjectNameAndOffset(pc, &offset,
|
2013-03-13 14:51:02 +08:00
|
|
|
buff.data(), buff.size(),
|
|
|
|
/* protection */0)) {
|
2013-10-04 16:55:03 +08:00
|
|
|
Printf(" ");
|
|
|
|
PrintModuleAndOffset(buff.data(), offset);
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
2012-10-02 20:11:17 +08:00
|
|
|
Printf("\n");
|
2012-08-28 22:27:06 +08:00
|
|
|
frame_num++;
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 08:19:46 +08:00
|
|
|
// Always print a trailing empty line after stack trace.
|
|
|
|
Printf("\n");
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr StackTrace::GetCurrentPc() {
|
|
|
|
return GET_CALLER_PC();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackTrace::FastUnwindStack(uptr pc, uptr bp,
|
2013-10-11 17:58:30 +08:00
|
|
|
uptr stack_top, uptr stack_bottom,
|
|
|
|
uptr max_depth) {
|
2013-10-12 20:40:47 +08:00
|
|
|
if (max_depth == 0) {
|
2013-10-11 17:58:30 +08:00
|
|
|
size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trace[0] = pc;
|
2012-08-28 22:27:06 +08:00
|
|
|
size = 1;
|
2012-11-19 18:31:00 +08:00
|
|
|
uhwptr *frame = (uhwptr *)bp;
|
2013-02-21 04:29:48 +08:00
|
|
|
uhwptr *prev_frame = frame - 1;
|
2013-06-18 22:47:40 +08:00
|
|
|
if (stack_top < 4096) return; // Sanity check for stack top.
|
2013-02-21 04:29:48 +08:00
|
|
|
// Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
|
|
|
|
while (frame > prev_frame &&
|
2012-11-19 18:31:00 +08:00
|
|
|
frame < (uhwptr *)stack_top - 2 &&
|
|
|
|
frame > (uhwptr *)stack_bottom &&
|
2013-04-04 14:52:40 +08:00
|
|
|
IsAligned((uptr)frame, sizeof(*frame)) &&
|
2013-10-12 20:40:47 +08:00
|
|
|
size < max_depth) {
|
2012-11-19 18:31:00 +08:00
|
|
|
uhwptr pc1 = frame[1];
|
2012-08-28 22:27:06 +08:00
|
|
|
if (pc1 != pc) {
|
2012-11-19 18:31:00 +08:00
|
|
|
trace[size++] = (uptr) pc1;
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
prev_frame = frame;
|
2012-11-19 18:31:00 +08:00
|
|
|
frame = (uhwptr *)frame[0];
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 15:00:42 +08:00
|
|
|
void StackTrace::PopStackFrames(uptr count) {
|
2012-11-21 21:00:04 +08:00
|
|
|
CHECK(size >= count);
|
2012-11-20 15:00:42 +08:00
|
|
|
size -= count;
|
|
|
|
for (uptr i = 0; i < size; i++) {
|
|
|
|
trace[i] = trace[i + count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 21:59:12 +08:00
|
|
|
static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
|
|
|
|
return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr
|
|
|
|
StackTrace::LocatePcInTrace(uptr pc, uptr pc_threshold, uptr max_pc_depth) {
|
|
|
|
for (uptr i = 0; i < max_pc_depth && i < size; ++i) {
|
|
|
|
if (MatchPc(pc, trace[i], pc_threshold))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-28 22:27:06 +08:00
|
|
|
} // namespace __sanitizer
|