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_stacktrace.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
|
2013-12-10 03:25:51 +08:00
|
|
|
#if defined(__sparc__)
|
2012-12-04 02:39:21 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-12-10 03:25:51 +08:00
|
|
|
uptr *frame = (uptr *)bp;
|
|
|
|
uptr *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 &&
|
2013-12-10 03:25:51 +08:00
|
|
|
frame < (uptr *)stack_top - 2 &&
|
|
|
|
frame > (uptr *)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) {
|
2013-12-10 03:25:51 +08:00
|
|
|
uptr pc1 = frame[1];
|
2012-08-28 22:27:06 +08:00
|
|
|
if (pc1 != pc) {
|
2013-12-10 03:25:51 +08:00
|
|
|
trace[size++] = pc1;
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
prev_frame = frame;
|
2013-12-10 03:25:51 +08:00
|
|
|
frame = (uptr*)frame[0];
|
2012-11-20 15:00:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-10 03:52:39 +08:00
|
|
|
void StackTrace::PopStackFrames(uptr count) {
|
2013-12-10 16:30:39 +08:00
|
|
|
CHECK_LT(count, size);
|
2013-12-10 03:52:39 +08:00
|
|
|
size -= count;
|
|
|
|
for (uptr i = 0; i < size; ++i) {
|
|
|
|
trace[i] = trace[i + count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:57:56 +08:00
|
|
|
uptr StackTrace::LocatePcInTrace(uptr pc) {
|
2013-11-13 23:20:10 +08:00
|
|
|
// Use threshold to find PC in stack trace, as PC we want to unwind from may
|
|
|
|
// slightly differ from return address in the actual unwinded stack trace.
|
2013-11-29 21:15:25 +08:00
|
|
|
const int kPcThreshold = 192;
|
2013-11-15 18:57:56 +08:00
|
|
|
for (uptr i = 0; i < size; ++i) {
|
2013-11-13 23:20:10 +08:00
|
|
|
if (MatchPc(pc, trace[i], kPcThreshold))
|
2013-11-09 21:59:12 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-28 22:27:06 +08:00
|
|
|
} // namespace __sanitizer
|