Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/debugging/internal/stacktrace_powerpc-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 // Produce stack trace.  I'm guessing (hoping!) the code is much like
0016 // for x86.  For apple machines, at least, it seems to be; see
0017 //    https://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
0018 //    https://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
0019 // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
0020 
0021 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_
0022 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_
0023 
0024 #if defined(__linux__)
0025 #include <asm/ptrace.h>   // for PT_NIP.
0026 #include <ucontext.h>     // for ucontext_t
0027 #endif
0028 
0029 #include <unistd.h>
0030 #include <cassert>
0031 #include <cstdint>
0032 #include <cstdio>
0033 
0034 #include "absl/base/attributes.h"
0035 #include "absl/base/optimization.h"
0036 #include "absl/base/port.h"
0037 #include "absl/debugging/stacktrace.h"
0038 #include "absl/debugging/internal/address_is_readable.h"
0039 #include "absl/debugging/internal/vdso_support.h"  // a no-op on non-elf or non-glibc systems
0040 
0041 // Given a stack pointer, return the saved link register value.
0042 // Note that this is the link register for a callee.
0043 static inline void *StacktracePowerPCGetLR(void **sp) {
0044   // PowerPC has 3 main ABIs, which say where in the stack the
0045   // Link Register is.  For DARWIN and AIX (used by apple and
0046   // linux ppc64), it's in sp[2].  For SYSV (used by linux ppc),
0047   // it's in sp[1].
0048 #if defined(_CALL_AIX) || defined(_CALL_DARWIN)
0049   return *(sp+2);
0050 #elif defined(_CALL_SYSV)
0051   return *(sp+1);
0052 #elif defined(__APPLE__) || defined(__FreeBSD__) || \
0053     (defined(__linux__) && defined(__PPC64__))
0054   // This check is in case the compiler doesn't define _CALL_AIX/etc.
0055   return *(sp+2);
0056 #elif defined(__linux)
0057   // This check is in case the compiler doesn't define _CALL_SYSV.
0058   return *(sp+1);
0059 #else
0060 #error Need to specify the PPC ABI for your architecture.
0061 #endif
0062 }
0063 
0064 // Given a pointer to a stack frame, locate and return the calling
0065 // stackframe, or return null if no stackframe can be found. Perform sanity
0066 // checks (the strictness of which is controlled by the boolean parameter
0067 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
0068 template<bool STRICT_UNWINDING, bool IS_WITH_CONTEXT>
0069 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0070 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
0071 static void **NextStackFrame(void **old_sp, const void *uc) {
0072   void **new_sp = (void **) *old_sp;
0073   enum { kStackAlignment = 16 };
0074 
0075   // Check that the transition from frame pointer old_sp to frame
0076   // pointer new_sp isn't clearly bogus
0077   if (STRICT_UNWINDING) {
0078     // With the stack growing downwards, older stack frame must be
0079     // at a greater address that the current one.
0080     if (new_sp <= old_sp) return nullptr;
0081     // Assume stack frames larger than 100,000 bytes are bogus.
0082     if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
0083   } else {
0084     // In the non-strict mode, allow discontiguous stack frames.
0085     // (alternate-signal-stacks for example).
0086     if (new_sp == old_sp) return nullptr;
0087     // And allow frames upto about 1MB.
0088     if ((new_sp > old_sp)
0089         && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
0090   }
0091   if ((uintptr_t)new_sp % kStackAlignment != 0) return nullptr;
0092 
0093 #if defined(__linux__)
0094   enum StackTraceKernelSymbolStatus {
0095       kNotInitialized = 0, kAddressValid, kAddressInvalid };
0096 
0097   if (IS_WITH_CONTEXT && uc != nullptr) {
0098     static StackTraceKernelSymbolStatus kernel_symbol_status =
0099         kNotInitialized;  // Sentinel: not computed yet.
0100     // Initialize with sentinel value: __kernel_rt_sigtramp_rt64 can not
0101     // possibly be there.
0102     static const unsigned char *kernel_sigtramp_rt64_address = nullptr;
0103     if (kernel_symbol_status == kNotInitialized) {
0104       absl::debugging_internal::VDSOSupport vdso;
0105       if (vdso.IsPresent()) {
0106         absl::debugging_internal::VDSOSupport::SymbolInfo
0107             sigtramp_rt64_symbol_info;
0108         if (!vdso.LookupSymbol(
0109                 "__kernel_sigtramp_rt64", "LINUX_2.6.15",
0110                 absl::debugging_internal::VDSOSupport::kVDSOSymbolType,
0111                 &sigtramp_rt64_symbol_info) ||
0112             sigtramp_rt64_symbol_info.address == nullptr) {
0113           // Unexpected: VDSO is present, yet the expected symbol is missing
0114           // or null.
0115           assert(false && "VDSO is present, but doesn't have expected symbol");
0116           kernel_symbol_status = kAddressInvalid;
0117         } else {
0118           kernel_sigtramp_rt64_address =
0119               reinterpret_cast<const unsigned char *>(
0120                   sigtramp_rt64_symbol_info.address);
0121           kernel_symbol_status = kAddressValid;
0122         }
0123       } else {
0124         kernel_symbol_status = kAddressInvalid;
0125       }
0126     }
0127 
0128     if (new_sp != nullptr &&
0129         kernel_symbol_status == kAddressValid &&
0130         StacktracePowerPCGetLR(new_sp) == kernel_sigtramp_rt64_address) {
0131       const ucontext_t* signal_context =
0132           reinterpret_cast<const ucontext_t*>(uc);
0133       void **const sp_before_signal =
0134 #if defined(__PPC64__)
0135           reinterpret_cast<void **>(signal_context->uc_mcontext.gp_regs[PT_R1]);
0136 #else
0137           reinterpret_cast<void **>(
0138               signal_context->uc_mcontext.uc_regs->gregs[PT_R1]);
0139 #endif
0140       // Check that alleged sp before signal is nonnull and is reasonably
0141       // aligned.
0142       if (sp_before_signal != nullptr &&
0143           ((uintptr_t)sp_before_signal % kStackAlignment) == 0) {
0144         // Check that alleged stack pointer is actually readable. This is to
0145         // prevent a "double fault" in case we hit the first fault due to e.g.
0146         // a stack corruption.
0147         if (absl::debugging_internal::AddressIsReadable(sp_before_signal)) {
0148           // Alleged stack pointer is readable, use it for further unwinding.
0149           new_sp = sp_before_signal;
0150         }
0151       }
0152     }
0153   }
0154 #endif
0155 
0156   return new_sp;
0157 }
0158 
0159 // This ensures that absl::GetStackTrace sets up the Link Register properly.
0160 ABSL_ATTRIBUTE_NOINLINE static void AbslStacktracePowerPCDummyFunction() {
0161   ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
0162 }
0163 
0164 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
0165 ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS  // May read random elements from stack.
0166 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY   // May read random elements from stack.
0167 static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
0168                       const void *ucp, int *min_dropped_frames) {
0169   void **sp;
0170   // Apple macOS uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
0171   // and Darwin 8.8.1 (Tiger) use as 1.38.  This means we have to use a
0172   // different asm syntax.  I don't know quite the best way to discriminate
0173   // systems using the old as from the new one; I've gone with __APPLE__.
0174 #ifdef __APPLE__
0175   __asm__ volatile ("mr %0,r1" : "=r" (sp));
0176 #else
0177   __asm__ volatile ("mr %0,1" : "=r" (sp));
0178 #endif
0179 
0180   // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
0181   // entry that holds the return address of the subroutine call (what
0182   // instruction we run after our function finishes).  This is the
0183   // same as the stack-pointer of our parent routine, which is what we
0184   // want here.  While the compiler will always(?) set up LR for
0185   // subroutine calls, it may not for leaf functions (such as this one).
0186   // This routine forces the compiler (at least gcc) to push it anyway.
0187   AbslStacktracePowerPCDummyFunction();
0188 
0189   // The LR save area is used by the callee, so the top entry is bogus.
0190   skip_count++;
0191 
0192   int n = 0;
0193 
0194   // Unlike ABIs of X86 and ARM, PowerPC ABIs say that return address (in
0195   // the link register) of a function call is stored in the caller's stack
0196   // frame instead of the callee's.  When we look for the return address
0197   // associated with a stack frame, we need to make sure that there is a
0198   // caller frame before it.  So we call NextStackFrame before entering the
0199   // loop below and check next_sp instead of sp for loop termination.
0200   // The outermost frame is set up by runtimes and it does not have a
0201   // caller frame, so it is skipped.
0202 
0203   // The absl::GetStackFrames routine is called when we are in some
0204   // informational context (the failure signal handler for example).
0205   // Use the non-strict unwinding rules to produce a stack trace
0206   // that is as complete as possible (even if it contains a few
0207   // bogus entries in some rare cases).
0208   void **next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
0209 
0210   while (next_sp && n < max_depth) {
0211     if (skip_count > 0) {
0212       skip_count--;
0213     } else {
0214       result[n] = StacktracePowerPCGetLR(sp);
0215       if (IS_STACK_FRAMES) {
0216         if (next_sp > sp) {
0217           sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
0218         } else {
0219           // A frame-size of 0 is used to indicate unknown frame size.
0220           sizes[n] = 0;
0221         }
0222       }
0223       n++;
0224     }
0225 
0226     sp = next_sp;
0227     next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
0228   }
0229 
0230   if (min_dropped_frames != nullptr) {
0231     // Implementation detail: we clamp the max of frames we are willing to
0232     // count, so as not to spend too much time in the loop below.
0233     const int kMaxUnwind = 1000;
0234     int num_dropped_frames = 0;
0235     for (int j = 0; next_sp != nullptr && j < kMaxUnwind; j++) {
0236       if (skip_count > 0) {
0237         skip_count--;
0238       } else {
0239         num_dropped_frames++;
0240       }
0241       next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(next_sp, ucp);
0242     }
0243     *min_dropped_frames = num_dropped_frames;
0244   }
0245   return n;
0246 }
0247 
0248 namespace absl {
0249 ABSL_NAMESPACE_BEGIN
0250 namespace debugging_internal {
0251 bool StackTraceWorksForTest() {
0252   return true;
0253 }
0254 }  // namespace debugging_internal
0255 ABSL_NAMESPACE_END
0256 }  // namespace absl
0257 
0258 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_