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