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) {
|
2014-05-30 16:52:03 +08:00
|
|
|
#if defined(__arm__)
|
2012-08-28 22:27:06 +08:00
|
|
|
// Cancel Thumb bit.
|
|
|
|
pc = pc & (~1);
|
2014-05-30 18:50:17 +08:00
|
|
|
#endif
|
|
|
|
#if defined(__powerpc__) || defined(__powerpc64__)
|
2014-05-30 16:52:03 +08:00
|
|
|
// PCs are always 4 byte aligned.
|
|
|
|
return pc - 4;
|
|
|
|
#elif 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) {
|
2014-03-04 20:21:28 +08:00
|
|
|
CHECK_GE(max_depth, 2);
|
2013-10-11 17:58:30 +08:00
|
|
|
trace[0] = pc;
|
2012-08-28 22:27:06 +08:00
|
|
|
size = 1;
|
2014-05-21 15:34:03 +08:00
|
|
|
uhwptr *frame = (uhwptr *)bp;
|
|
|
|
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 &&
|
2014-05-21 15:34:03 +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) {
|
2014-05-21 15:34:03 +08:00
|
|
|
uhwptr pc1 = frame[1];
|
2012-08-28 22:27:06 +08:00
|
|
|
if (pc1 != pc) {
|
2014-05-21 15:34:03 +08:00
|
|
|
trace[size++] = (uptr) pc1;
|
2012-08-28 22:27:06 +08:00
|
|
|
}
|
|
|
|
prev_frame = frame;
|
2014-05-21 15:34:03 +08:00
|
|
|
frame = (uhwptr *)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.
|
2014-03-05 16:29:56 +08:00
|
|
|
const int kPcThreshold = 288;
|
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
|