blob: c84083379bb237e11d9a1264c8db7a4479a86f70 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2011 and onwards Google Inc.
2// All rights reserved.
3//
4// Author: Doug Kwan
5// This is inspired by Craig Silverstein's PowerPC stacktrace code.
6//
7
8#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
9#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
10
11#include <cstdint>
12
13#include "absl/debugging/stacktrace.h"
14
15// WARNING:
16// This only works if all your code is in either ARM or THUMB mode. With
17// interworking, the frame pointer of the caller can either be in r11 (ARM
18// mode) or r7 (THUMB mode). A callee only saves the frame pointer of its
19// mode in a fixed location on its stack frame. If the caller is a different
20// mode, there is no easy way to find the frame pointer. It can either be
21// still in the designated register or saved on stack along with other callee
22// saved registers.
23
24// Given a pointer to a stack frame, locate and return the calling
25// stackframe, or return nullptr if no stackframe can be found. Perform sanity
26// checks (the strictness of which is controlled by the boolean parameter
27// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
28template<bool STRICT_UNWINDING>
29static void **NextStackFrame(void **old_sp) {
30 void **new_sp = (void**) old_sp[-1];
31
32 // Check that the transition from frame pointer old_sp to frame
33 // pointer new_sp isn't clearly bogus
34 if (STRICT_UNWINDING) {
35 // With the stack growing downwards, older stack frame must be
36 // at a greater address that the current one.
37 if (new_sp <= old_sp) return nullptr;
38 // Assume stack frames larger than 100,000 bytes are bogus.
39 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
40 } else {
41 // In the non-strict mode, allow discontiguous stack frames.
42 // (alternate-signal-stacks for example).
43 if (new_sp == old_sp) return nullptr;
44 // And allow frames upto about 1MB.
45 if ((new_sp > old_sp)
46 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
47 }
48 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return nullptr;
49 return new_sp;
50}
51
52// This ensures that absl::GetStackTrace sets up the Link Register properly.
53#ifdef __GNUC__
54void StacktraceArmDummyFunction() __attribute__((noinline));
55void StacktraceArmDummyFunction() { __asm__ volatile(""); }
56#else
57# error StacktraceArmDummyFunction() needs to be ported to this platform.
58#endif
59
60template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
61static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
62 const void * /* ucp */, int *min_dropped_frames) {
63#ifdef __GNUC__
64 void **sp = reinterpret_cast<void**>(__builtin_frame_address(0));
65#else
66# error reading stack point not yet supported on this platform.
67#endif
68
69 // On ARM, the return address is stored in the link register (r14).
70 // This is not saved on the stack frame of a leaf function. To
71 // simplify code that reads return addresses, we call a dummy
72 // function so that the return address of this function is also
73 // stored in the stack frame. This works at least for gcc.
74 StacktraceArmDummyFunction();
75
76 int n = 0;
77 while (sp && n < max_depth) {
78 // The absl::GetStackFrames routine is called when we are in some
79 // informational context (the failure signal handler for example).
80 // Use the non-strict unwinding rules to produce a stack trace
81 // that is as complete as possible (even if it contains a few bogus
82 // entries in some rare cases).
83 void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
84
85 if (skip_count > 0) {
86 skip_count--;
87 } else {
88 result[n] = *sp;
89
90 if (IS_STACK_FRAMES) {
91 if (next_sp > sp) {
92 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
93 } else {
94 // A frame-size of 0 is used to indicate unknown frame size.
95 sizes[n] = 0;
96 }
97 }
98 n++;
99 }
100 sp = next_sp;
101 }
102 if (min_dropped_frames != nullptr) {
103 // Implementation detail: we clamp the max of frames we are willing to
104 // count, so as not to spend too much time in the loop below.
105 const int kMaxUnwind = 200;
106 int j = 0;
107 for (; sp != nullptr && j < kMaxUnwind; j++) {
108 sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
109 }
110 *min_dropped_frames = j;
111 }
112 return n;
113}
114
115namespace absl {
116namespace debugging_internal {
117bool StackTraceWorksForTest() {
118 return false;
119}
120} // namespace debugging_internal
121} // namespace absl
122
123#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_