* gcc.dg/20001117-1.c: Needs return_address.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.cc
bloba751da2f740400dc70d064075eb7dd15994790da
1 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common.h"
13 #include "sanitizer_flags.h"
14 #include "sanitizer_stacktrace.h"
16 namespace __sanitizer {
18 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
19 #if defined(__arm__)
20 // Cancel Thumb bit.
21 pc = pc & (~1);
22 #endif
23 #if defined(__powerpc__) || defined(__powerpc64__)
24 // PCs are always 4 byte aligned.
25 return pc - 4;
26 #elif defined(__sparc__) || defined(__mips__)
27 return pc - 8;
28 #else
29 return pc - 1;
30 #endif
33 uptr StackTrace::GetCurrentPc() {
34 return GET_CALLER_PC();
37 void BufferedStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
38 size = cnt + !!extra_top_pc;
39 CHECK_LE(size, kStackTraceMax);
40 internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
41 if (extra_top_pc)
42 trace_buffer[cnt] = extra_top_pc;
43 top_frame_bp = 0;
46 // Check if given pointer points into allocated stack area.
47 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
48 return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
51 // In GCC on ARM bp points to saved lr, not fp, so we should check the next
52 // cell in stack to be a saved frame pointer. GetCanonicFrame returns the
53 // pointer to saved frame pointer in any case.
54 static inline uhwptr *GetCanonicFrame(uptr bp,
55 uptr stack_top,
56 uptr stack_bottom) {
57 #ifdef __arm__
58 if (!IsValidFrame(bp, stack_top, stack_bottom)) return 0;
59 uhwptr *bp_prev = (uhwptr *)bp;
60 if (IsValidFrame((uptr)bp_prev[0], stack_top, stack_bottom)) return bp_prev;
61 // The next frame pointer does not look right. This could be a GCC frame, step
62 // back by 1 word and try again.
63 if (IsValidFrame((uptr)bp_prev[-1], stack_top, stack_bottom))
64 return bp_prev - 1;
65 // Nope, this does not look right either. This means the frame after next does
66 // not have a valid frame pointer, but we can still extract the caller PC.
67 // Unfortunately, there is no way to decide between GCC and LLVM frame
68 // layouts. Assume LLVM.
69 return bp_prev;
70 #else
71 return (uhwptr*)bp;
72 #endif
75 void BufferedStackTrace::FastUnwindStack(uptr pc, uptr bp, uptr stack_top,
76 uptr stack_bottom, uptr max_depth) {
77 CHECK_GE(max_depth, 2);
78 trace_buffer[0] = pc;
79 size = 1;
80 if (stack_top < 4096) return; // Sanity check for stack top.
81 uhwptr *frame = GetCanonicFrame(bp, stack_top, stack_bottom);
82 // Lowest possible address that makes sense as the next frame pointer.
83 // Goes up as we walk the stack.
84 uptr bottom = stack_bottom;
85 // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
86 while (IsValidFrame((uptr)frame, stack_top, bottom) &&
87 IsAligned((uptr)frame, sizeof(*frame)) &&
88 size < max_depth) {
89 #ifdef __powerpc__
90 // PowerPC ABIs specify that the return address is saved at offset
91 // 16 of the *caller's* stack frame. Thus we must dereference the
92 // back chain to find the caller frame before extracting it.
93 uhwptr *caller_frame = (uhwptr*)frame[0];
94 if (!IsValidFrame((uptr)caller_frame, stack_top, bottom) ||
95 !IsAligned((uptr)caller_frame, sizeof(uhwptr)))
96 break;
97 uhwptr pc1 = caller_frame[2];
98 #else
99 uhwptr pc1 = frame[1];
100 #endif
101 if (pc1 != pc) {
102 trace_buffer[size++] = (uptr) pc1;
104 bottom = (uptr)frame;
105 frame = GetCanonicFrame((uptr)frame[0], stack_top, bottom);
109 static bool MatchPc(uptr cur_pc, uptr trace_pc, uptr threshold) {
110 return cur_pc - trace_pc <= threshold || trace_pc - cur_pc <= threshold;
113 void BufferedStackTrace::PopStackFrames(uptr count) {
114 CHECK_LT(count, size);
115 size -= count;
116 for (uptr i = 0; i < size; ++i) {
117 trace_buffer[i] = trace_buffer[i + count];
121 uptr BufferedStackTrace::LocatePcInTrace(uptr pc) {
122 // Use threshold to find PC in stack trace, as PC we want to unwind from may
123 // slightly differ from return address in the actual unwinded stack trace.
124 const int kPcThreshold = 288;
125 for (uptr i = 0; i < size; ++i) {
126 if (MatchPc(pc, trace[i], kPcThreshold))
127 return i;
129 return 0;
132 } // namespace __sanitizer