1*9356374aSAndroid Build Coastguard Worker// Copyright 2017 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker// 3*9356374aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker// 7*9356374aSAndroid Build Coastguard Worker// https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker// 9*9356374aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker// limitations under the License. 14*9356374aSAndroid Build Coastguard Worker// 15*9356374aSAndroid Build Coastguard Worker// Produce stack trace. I'm guessing (hoping!) the code is much like 16*9356374aSAndroid Build Coastguard Worker// for x86. For apple machines, at least, it seems to be; see 17*9356374aSAndroid Build Coastguard Worker// https://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html 18*9356374aSAndroid Build Coastguard Worker// https://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK 19*9356374aSAndroid Build Coastguard Worker// Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882 20*9356374aSAndroid Build Coastguard Worker 21*9356374aSAndroid Build Coastguard Worker#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_ 22*9356374aSAndroid Build Coastguard Worker#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_ 23*9356374aSAndroid Build Coastguard Worker 24*9356374aSAndroid Build Coastguard Worker#if defined(__linux__) 25*9356374aSAndroid Build Coastguard Worker#include <asm/ptrace.h> // for PT_NIP. 26*9356374aSAndroid Build Coastguard Worker#include <ucontext.h> // for ucontext_t 27*9356374aSAndroid Build Coastguard Worker#endif 28*9356374aSAndroid Build Coastguard Worker 29*9356374aSAndroid Build Coastguard Worker#include <unistd.h> 30*9356374aSAndroid Build Coastguard Worker#include <cassert> 31*9356374aSAndroid Build Coastguard Worker#include <cstdint> 32*9356374aSAndroid Build Coastguard Worker#include <cstdio> 33*9356374aSAndroid Build Coastguard Worker 34*9356374aSAndroid Build Coastguard Worker#include "absl/base/attributes.h" 35*9356374aSAndroid Build Coastguard Worker#include "absl/base/optimization.h" 36*9356374aSAndroid Build Coastguard Worker#include "absl/base/port.h" 37*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/stacktrace.h" 38*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/internal/address_is_readable.h" 39*9356374aSAndroid Build Coastguard Worker#include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems 40*9356374aSAndroid Build Coastguard Worker 41*9356374aSAndroid Build Coastguard Worker// Given a stack pointer, return the saved link register value. 42*9356374aSAndroid Build Coastguard Worker// Note that this is the link register for a callee. 43*9356374aSAndroid Build Coastguard Workerstatic inline void *StacktracePowerPCGetLR(void **sp) { 44*9356374aSAndroid Build Coastguard Worker // PowerPC has 3 main ABIs, which say where in the stack the 45*9356374aSAndroid Build Coastguard Worker // Link Register is. For DARWIN and AIX (used by apple and 46*9356374aSAndroid Build Coastguard Worker // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc), 47*9356374aSAndroid Build Coastguard Worker // it's in sp[1]. 48*9356374aSAndroid Build Coastguard Worker#if defined(_CALL_AIX) || defined(_CALL_DARWIN) 49*9356374aSAndroid Build Coastguard Worker return *(sp+2); 50*9356374aSAndroid Build Coastguard Worker#elif defined(_CALL_SYSV) 51*9356374aSAndroid Build Coastguard Worker return *(sp+1); 52*9356374aSAndroid Build Coastguard Worker#elif defined(__APPLE__) || defined(__FreeBSD__) || \ 53*9356374aSAndroid Build Coastguard Worker (defined(__linux__) && defined(__PPC64__)) 54*9356374aSAndroid Build Coastguard Worker // This check is in case the compiler doesn't define _CALL_AIX/etc. 55*9356374aSAndroid Build Coastguard Worker return *(sp+2); 56*9356374aSAndroid Build Coastguard Worker#elif defined(__linux) 57*9356374aSAndroid Build Coastguard Worker // This check is in case the compiler doesn't define _CALL_SYSV. 58*9356374aSAndroid Build Coastguard Worker return *(sp+1); 59*9356374aSAndroid Build Coastguard Worker#else 60*9356374aSAndroid Build Coastguard Worker#error Need to specify the PPC ABI for your architecture. 61*9356374aSAndroid Build Coastguard Worker#endif 62*9356374aSAndroid Build Coastguard Worker} 63*9356374aSAndroid Build Coastguard Worker 64*9356374aSAndroid Build Coastguard Worker// Given a pointer to a stack frame, locate and return the calling 65*9356374aSAndroid Build Coastguard Worker// stackframe, or return null if no stackframe can be found. Perform sanity 66*9356374aSAndroid Build Coastguard Worker// checks (the strictness of which is controlled by the boolean parameter 67*9356374aSAndroid Build Coastguard Worker// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. 68*9356374aSAndroid Build Coastguard Workertemplate<bool STRICT_UNWINDING, bool IS_WITH_CONTEXT> 69*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 70*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 71*9356374aSAndroid Build Coastguard Workerstatic void **NextStackFrame(void **old_sp, const void *uc) { 72*9356374aSAndroid Build Coastguard Worker void **new_sp = (void **) *old_sp; 73*9356374aSAndroid Build Coastguard Worker enum { kStackAlignment = 16 }; 74*9356374aSAndroid Build Coastguard Worker 75*9356374aSAndroid Build Coastguard Worker // Check that the transition from frame pointer old_sp to frame 76*9356374aSAndroid Build Coastguard Worker // pointer new_sp isn't clearly bogus 77*9356374aSAndroid Build Coastguard Worker if (STRICT_UNWINDING) { 78*9356374aSAndroid Build Coastguard Worker // With the stack growing downwards, older stack frame must be 79*9356374aSAndroid Build Coastguard Worker // at a greater address that the current one. 80*9356374aSAndroid Build Coastguard Worker if (new_sp <= old_sp) return nullptr; 81*9356374aSAndroid Build Coastguard Worker // Assume stack frames larger than 100,000 bytes are bogus. 82*9356374aSAndroid Build Coastguard Worker if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr; 83*9356374aSAndroid Build Coastguard Worker } else { 84*9356374aSAndroid Build Coastguard Worker // In the non-strict mode, allow discontiguous stack frames. 85*9356374aSAndroid Build Coastguard Worker // (alternate-signal-stacks for example). 86*9356374aSAndroid Build Coastguard Worker if (new_sp == old_sp) return nullptr; 87*9356374aSAndroid Build Coastguard Worker // And allow frames upto about 1MB. 88*9356374aSAndroid Build Coastguard Worker if ((new_sp > old_sp) 89*9356374aSAndroid Build Coastguard Worker && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr; 90*9356374aSAndroid Build Coastguard Worker } 91*9356374aSAndroid Build Coastguard Worker if ((uintptr_t)new_sp % kStackAlignment != 0) return nullptr; 92*9356374aSAndroid Build Coastguard Worker 93*9356374aSAndroid Build Coastguard Worker#if defined(__linux__) 94*9356374aSAndroid Build Coastguard Worker enum StackTraceKernelSymbolStatus { 95*9356374aSAndroid Build Coastguard Worker kNotInitialized = 0, kAddressValid, kAddressInvalid }; 96*9356374aSAndroid Build Coastguard Worker 97*9356374aSAndroid Build Coastguard Worker if (IS_WITH_CONTEXT && uc != nullptr) { 98*9356374aSAndroid Build Coastguard Worker static StackTraceKernelSymbolStatus kernel_symbol_status = 99*9356374aSAndroid Build Coastguard Worker kNotInitialized; // Sentinel: not computed yet. 100*9356374aSAndroid Build Coastguard Worker // Initialize with sentinel value: __kernel_rt_sigtramp_rt64 can not 101*9356374aSAndroid Build Coastguard Worker // possibly be there. 102*9356374aSAndroid Build Coastguard Worker static const unsigned char *kernel_sigtramp_rt64_address = nullptr; 103*9356374aSAndroid Build Coastguard Worker if (kernel_symbol_status == kNotInitialized) { 104*9356374aSAndroid Build Coastguard Worker absl::debugging_internal::VDSOSupport vdso; 105*9356374aSAndroid Build Coastguard Worker if (vdso.IsPresent()) { 106*9356374aSAndroid Build Coastguard Worker absl::debugging_internal::VDSOSupport::SymbolInfo 107*9356374aSAndroid Build Coastguard Worker sigtramp_rt64_symbol_info; 108*9356374aSAndroid Build Coastguard Worker if (!vdso.LookupSymbol( 109*9356374aSAndroid Build Coastguard Worker "__kernel_sigtramp_rt64", "LINUX_2.6.15", 110*9356374aSAndroid Build Coastguard Worker absl::debugging_internal::VDSOSupport::kVDSOSymbolType, 111*9356374aSAndroid Build Coastguard Worker &sigtramp_rt64_symbol_info) || 112*9356374aSAndroid Build Coastguard Worker sigtramp_rt64_symbol_info.address == nullptr) { 113*9356374aSAndroid Build Coastguard Worker // Unexpected: VDSO is present, yet the expected symbol is missing 114*9356374aSAndroid Build Coastguard Worker // or null. 115*9356374aSAndroid Build Coastguard Worker assert(false && "VDSO is present, but doesn't have expected symbol"); 116*9356374aSAndroid Build Coastguard Worker kernel_symbol_status = kAddressInvalid; 117*9356374aSAndroid Build Coastguard Worker } else { 118*9356374aSAndroid Build Coastguard Worker kernel_sigtramp_rt64_address = 119*9356374aSAndroid Build Coastguard Worker reinterpret_cast<const unsigned char *>( 120*9356374aSAndroid Build Coastguard Worker sigtramp_rt64_symbol_info.address); 121*9356374aSAndroid Build Coastguard Worker kernel_symbol_status = kAddressValid; 122*9356374aSAndroid Build Coastguard Worker } 123*9356374aSAndroid Build Coastguard Worker } else { 124*9356374aSAndroid Build Coastguard Worker kernel_symbol_status = kAddressInvalid; 125*9356374aSAndroid Build Coastguard Worker } 126*9356374aSAndroid Build Coastguard Worker } 127*9356374aSAndroid Build Coastguard Worker 128*9356374aSAndroid Build Coastguard Worker if (new_sp != nullptr && 129*9356374aSAndroid Build Coastguard Worker kernel_symbol_status == kAddressValid && 130*9356374aSAndroid Build Coastguard Worker StacktracePowerPCGetLR(new_sp) == kernel_sigtramp_rt64_address) { 131*9356374aSAndroid Build Coastguard Worker const ucontext_t* signal_context = 132*9356374aSAndroid Build Coastguard Worker reinterpret_cast<const ucontext_t*>(uc); 133*9356374aSAndroid Build Coastguard Worker void **const sp_before_signal = 134*9356374aSAndroid Build Coastguard Worker#if defined(__PPC64__) 135*9356374aSAndroid Build Coastguard Worker reinterpret_cast<void **>(signal_context->uc_mcontext.gp_regs[PT_R1]); 136*9356374aSAndroid Build Coastguard Worker#else 137*9356374aSAndroid Build Coastguard Worker reinterpret_cast<void **>( 138*9356374aSAndroid Build Coastguard Worker signal_context->uc_mcontext.uc_regs->gregs[PT_R1]); 139*9356374aSAndroid Build Coastguard Worker#endif 140*9356374aSAndroid Build Coastguard Worker // Check that alleged sp before signal is nonnull and is reasonably 141*9356374aSAndroid Build Coastguard Worker // aligned. 142*9356374aSAndroid Build Coastguard Worker if (sp_before_signal != nullptr && 143*9356374aSAndroid Build Coastguard Worker ((uintptr_t)sp_before_signal % kStackAlignment) == 0) { 144*9356374aSAndroid Build Coastguard Worker // Check that alleged stack pointer is actually readable. This is to 145*9356374aSAndroid Build Coastguard Worker // prevent a "double fault" in case we hit the first fault due to e.g. 146*9356374aSAndroid Build Coastguard Worker // a stack corruption. 147*9356374aSAndroid Build Coastguard Worker if (absl::debugging_internal::AddressIsReadable(sp_before_signal)) { 148*9356374aSAndroid Build Coastguard Worker // Alleged stack pointer is readable, use it for further unwinding. 149*9356374aSAndroid Build Coastguard Worker new_sp = sp_before_signal; 150*9356374aSAndroid Build Coastguard Worker } 151*9356374aSAndroid Build Coastguard Worker } 152*9356374aSAndroid Build Coastguard Worker } 153*9356374aSAndroid Build Coastguard Worker } 154*9356374aSAndroid Build Coastguard Worker#endif 155*9356374aSAndroid Build Coastguard Worker 156*9356374aSAndroid Build Coastguard Worker return new_sp; 157*9356374aSAndroid Build Coastguard Worker} 158*9356374aSAndroid Build Coastguard Worker 159*9356374aSAndroid Build Coastguard Worker// This ensures that absl::GetStackTrace sets up the Link Register properly. 160*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NOINLINE static void AbslStacktracePowerPCDummyFunction() { 161*9356374aSAndroid Build Coastguard Worker ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); 162*9356374aSAndroid Build Coastguard Worker} 163*9356374aSAndroid Build Coastguard Worker 164*9356374aSAndroid Build Coastguard Workertemplate <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT> 165*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS // May read random elements from stack. 166*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_NO_SANITIZE_MEMORY // May read random elements from stack. 167*9356374aSAndroid Build Coastguard Workerstatic int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, 168*9356374aSAndroid Build Coastguard Worker const void *ucp, int *min_dropped_frames) { 169*9356374aSAndroid Build Coastguard Worker void **sp; 170*9356374aSAndroid Build Coastguard Worker // Apple macOS uses an old version of gnu as -- both Darwin 7.9.0 (Panther) 171*9356374aSAndroid Build Coastguard Worker // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a 172*9356374aSAndroid Build Coastguard Worker // different asm syntax. I don't know quite the best way to discriminate 173*9356374aSAndroid Build Coastguard Worker // systems using the old as from the new one; I've gone with __APPLE__. 174*9356374aSAndroid Build Coastguard Worker#ifdef __APPLE__ 175*9356374aSAndroid Build Coastguard Worker __asm__ volatile ("mr %0,r1" : "=r" (sp)); 176*9356374aSAndroid Build Coastguard Worker#else 177*9356374aSAndroid Build Coastguard Worker __asm__ volatile ("mr %0,1" : "=r" (sp)); 178*9356374aSAndroid Build Coastguard Worker#endif 179*9356374aSAndroid Build Coastguard Worker 180*9356374aSAndroid Build Coastguard Worker // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack 181*9356374aSAndroid Build Coastguard Worker // entry that holds the return address of the subroutine call (what 182*9356374aSAndroid Build Coastguard Worker // instruction we run after our function finishes). This is the 183*9356374aSAndroid Build Coastguard Worker // same as the stack-pointer of our parent routine, which is what we 184*9356374aSAndroid Build Coastguard Worker // want here. While the compiler will always(?) set up LR for 185*9356374aSAndroid Build Coastguard Worker // subroutine calls, it may not for leaf functions (such as this one). 186*9356374aSAndroid Build Coastguard Worker // This routine forces the compiler (at least gcc) to push it anyway. 187*9356374aSAndroid Build Coastguard Worker AbslStacktracePowerPCDummyFunction(); 188*9356374aSAndroid Build Coastguard Worker 189*9356374aSAndroid Build Coastguard Worker // The LR save area is used by the callee, so the top entry is bogus. 190*9356374aSAndroid Build Coastguard Worker skip_count++; 191*9356374aSAndroid Build Coastguard Worker 192*9356374aSAndroid Build Coastguard Worker int n = 0; 193*9356374aSAndroid Build Coastguard Worker 194*9356374aSAndroid Build Coastguard Worker // Unlike ABIs of X86 and ARM, PowerPC ABIs say that return address (in 195*9356374aSAndroid Build Coastguard Worker // the link register) of a function call is stored in the caller's stack 196*9356374aSAndroid Build Coastguard Worker // frame instead of the callee's. When we look for the return address 197*9356374aSAndroid Build Coastguard Worker // associated with a stack frame, we need to make sure that there is a 198*9356374aSAndroid Build Coastguard Worker // caller frame before it. So we call NextStackFrame before entering the 199*9356374aSAndroid Build Coastguard Worker // loop below and check next_sp instead of sp for loop termination. 200*9356374aSAndroid Build Coastguard Worker // The outermost frame is set up by runtimes and it does not have a 201*9356374aSAndroid Build Coastguard Worker // caller frame, so it is skipped. 202*9356374aSAndroid Build Coastguard Worker 203*9356374aSAndroid Build Coastguard Worker // The absl::GetStackFrames routine is called when we are in some 204*9356374aSAndroid Build Coastguard Worker // informational context (the failure signal handler for example). 205*9356374aSAndroid Build Coastguard Worker // Use the non-strict unwinding rules to produce a stack trace 206*9356374aSAndroid Build Coastguard Worker // that is as complete as possible (even if it contains a few 207*9356374aSAndroid Build Coastguard Worker // bogus entries in some rare cases). 208*9356374aSAndroid Build Coastguard Worker void **next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp); 209*9356374aSAndroid Build Coastguard Worker 210*9356374aSAndroid Build Coastguard Worker while (next_sp && n < max_depth) { 211*9356374aSAndroid Build Coastguard Worker if (skip_count > 0) { 212*9356374aSAndroid Build Coastguard Worker skip_count--; 213*9356374aSAndroid Build Coastguard Worker } else { 214*9356374aSAndroid Build Coastguard Worker result[n] = StacktracePowerPCGetLR(sp); 215*9356374aSAndroid Build Coastguard Worker if (IS_STACK_FRAMES) { 216*9356374aSAndroid Build Coastguard Worker if (next_sp > sp) { 217*9356374aSAndroid Build Coastguard Worker sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; 218*9356374aSAndroid Build Coastguard Worker } else { 219*9356374aSAndroid Build Coastguard Worker // A frame-size of 0 is used to indicate unknown frame size. 220*9356374aSAndroid Build Coastguard Worker sizes[n] = 0; 221*9356374aSAndroid Build Coastguard Worker } 222*9356374aSAndroid Build Coastguard Worker } 223*9356374aSAndroid Build Coastguard Worker n++; 224*9356374aSAndroid Build Coastguard Worker } 225*9356374aSAndroid Build Coastguard Worker 226*9356374aSAndroid Build Coastguard Worker sp = next_sp; 227*9356374aSAndroid Build Coastguard Worker next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp); 228*9356374aSAndroid Build Coastguard Worker } 229*9356374aSAndroid Build Coastguard Worker 230*9356374aSAndroid Build Coastguard Worker if (min_dropped_frames != nullptr) { 231*9356374aSAndroid Build Coastguard Worker // Implementation detail: we clamp the max of frames we are willing to 232*9356374aSAndroid Build Coastguard Worker // count, so as not to spend too much time in the loop below. 233*9356374aSAndroid Build Coastguard Worker const int kMaxUnwind = 1000; 234*9356374aSAndroid Build Coastguard Worker int num_dropped_frames = 0; 235*9356374aSAndroid Build Coastguard Worker for (int j = 0; next_sp != nullptr && j < kMaxUnwind; j++) { 236*9356374aSAndroid Build Coastguard Worker if (skip_count > 0) { 237*9356374aSAndroid Build Coastguard Worker skip_count--; 238*9356374aSAndroid Build Coastguard Worker } else { 239*9356374aSAndroid Build Coastguard Worker num_dropped_frames++; 240*9356374aSAndroid Build Coastguard Worker } 241*9356374aSAndroid Build Coastguard Worker next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(next_sp, ucp); 242*9356374aSAndroid Build Coastguard Worker } 243*9356374aSAndroid Build Coastguard Worker *min_dropped_frames = num_dropped_frames; 244*9356374aSAndroid Build Coastguard Worker } 245*9356374aSAndroid Build Coastguard Worker return n; 246*9356374aSAndroid Build Coastguard Worker} 247*9356374aSAndroid Build Coastguard Worker 248*9356374aSAndroid Build Coastguard Workernamespace absl { 249*9356374aSAndroid Build Coastguard WorkerABSL_NAMESPACE_BEGIN 250*9356374aSAndroid Build Coastguard Workernamespace debugging_internal { 251*9356374aSAndroid Build Coastguard Workerbool StackTraceWorksForTest() { 252*9356374aSAndroid Build Coastguard Worker return true; 253*9356374aSAndroid Build Coastguard Worker} 254*9356374aSAndroid Build Coastguard Worker} // namespace debugging_internal 255*9356374aSAndroid Build Coastguard WorkerABSL_NAMESPACE_END 256*9356374aSAndroid Build Coastguard Worker} // namespace absl 257*9356374aSAndroid Build Coastguard Worker 258*9356374aSAndroid Build Coastguard Worker#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_POWERPC_INL_H_ 259