Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/debugging/internal/stacktrace_arm-inl.inc is written in an unsupported language. File is not indexed.

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // This is inspired by Craig Silverstein's PowerPC stacktrace code.
0016 
0017 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
0018 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
0019 
0020 #include <cstdint>
0021 
0022 #include "absl/debugging/stacktrace.h"
0023 
0024 // WARNING:
0025 // This only works if all your code is in either ARM or THUMB mode.  With
0026 // interworking, the frame pointer of the caller can either be in r11 (ARM
0027 // mode) or r7 (THUMB mode).  A callee only saves the frame pointer of its
0028 // mode in a fixed location on its stack frame.  If the caller is a different
0029 // mode, there is no easy way to find the frame pointer.  It can either be
0030 // still in the designated register or saved on stack along with other callee
0031 // saved registers.
0032 
0033 // Given a pointer to a stack frame, locate and return the calling
0034 // stackframe, or return nullptr if no stackframe can be found. Perform sanity
0035 // checks (the strictness of which is controlled by the boolean parameter
0036 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
0037 template<bool STRICT_UNWINDING>
0038 static void **NextStackFrame(void **old_sp) {
0039   void **new_sp = (void**) old_sp[-1];
0040 
0041   // Check that the transition from frame pointer old_sp to frame
0042   // pointer new_sp isn't clearly bogus
0043   if (STRICT_UNWINDING) {
0044     // With the stack growing downwards, older stack frame must be
0045     // at a greater address that the current one.
0046     if (new_sp <= old_sp) return nullptr;
0047     // Assume stack frames larger than 100,000 bytes are bogus.
0048     if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
0049   } else {
0050     // In the non-strict mode, allow discontiguous stack frames.
0051     // (alternate-signal-stacks for example).
0052     if (new_sp == old_sp) return nullptr;
0053     // And allow frames upto about 1MB.
0054     if ((new_sp > old_sp)
0055         && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
0056   }
0057   if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return nullptr;
0058   return new_sp;
0059 }
0060 
0061 // This ensures that absl::GetStackTrace sets up the Link Register properly.
0062 #ifdef __GNUC__
0063 void StacktraceArmDummyFunction() __attribute__((noinline));
0064 void StacktraceArmDummyFunction() { __asm__ volatile(""); }
0065 #else
0066 # error StacktraceArmDummyFunction() needs to be ported to this platform.
0067 #endif
0068 
0069 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
0070 static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
0071                       const void * /* ucp */, int *min_dropped_frames) {
0072 #ifdef __GNUC__
0073   void **sp = reinterpret_cast<void**>(__builtin_frame_address(0));
0074 #else
0075 # error reading stack point not yet supported on this platform.
0076 #endif
0077 
0078   // On ARM, the return address is stored in the link register (r14).
0079   // This is not saved on the stack frame of a leaf function.  To
0080   // simplify code that reads return addresses, we call a dummy
0081   // function so that the return address of this function is also
0082   // stored in the stack frame.  This works at least for gcc.
0083   StacktraceArmDummyFunction();
0084 
0085   int n = 0;
0086   while (sp && n < max_depth) {
0087     // The absl::GetStackFrames routine is called when we are in some
0088     // informational context (the failure signal handler for example).
0089     // Use the non-strict unwinding rules to produce a stack trace
0090     // that is as complete as possible (even if it contains a few bogus
0091     // entries in some rare cases).
0092     void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
0093 
0094     if (skip_count > 0) {
0095       skip_count--;
0096     } else {
0097       result[n] = *sp;
0098 
0099       if (IS_STACK_FRAMES) {
0100         if (next_sp > sp) {
0101           sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
0102         } else {
0103           // A frame-size of 0 is used to indicate unknown frame size.
0104           sizes[n] = 0;
0105         }
0106       }
0107       n++;
0108     }
0109     sp = next_sp;
0110   }
0111   if (min_dropped_frames != nullptr) {
0112     // Implementation detail: we clamp the max of frames we are willing to
0113     // count, so as not to spend too much time in the loop below.
0114     const int kMaxUnwind = 200;
0115     int num_dropped_frames = 0;
0116     for (int j = 0; sp != nullptr && j < kMaxUnwind; j++) {
0117       if (skip_count > 0) {
0118         skip_count--;
0119       } else {
0120         num_dropped_frames++;
0121       }
0122       sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
0123     }
0124     *min_dropped_frames = num_dropped_frames;
0125   }
0126   return n;
0127 }
0128 
0129 namespace absl {
0130 ABSL_NAMESPACE_BEGIN
0131 namespace debugging_internal {
0132 bool StackTraceWorksForTest() {
0133   return false;
0134 }
0135 }  // namespace debugging_internal
0136 ABSL_NAMESPACE_END
0137 }  // namespace absl
0138 
0139 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_