blob: 3f6c3670f64770af73329d0f875b753c0d295dc2 [file] [log] [blame]
Brian Silverman20350ac2021-11-17 18:19:55 -08001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
Austin Schuh745610d2015-09-06 18:19:50 -07002// Copyright (c) 2007, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Produce stack trace. ABI documentation reference can be found at:
33// * PowerPC32 ABI: https://www.power.org/documentation/
34// power-architecture-32-bit-abi-supplement-1-0-embeddedlinuxunified/
35// * PowerPC64 ABI:
36// http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
37
38#ifndef BASE_STACKTRACE_POWERPC_INL_H_
39#define BASE_STACKTRACE_POWERPC_INL_H_
40// Note: this file is included into stacktrace.cc more than once.
41// Anything that should only be defined once should be here:
42
43#include <stdint.h> // for uintptr_t
44#include <stdlib.h> // for NULL
45#include <gperftools/stacktrace.h>
46
47// Given a pointer to a stack frame, locate and return the calling
48// stackframe, or return NULL if no stackframe can be found. Perform sanity
49// checks (the strictness of which is controlled by the boolean parameter
50// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
51template<bool STRICT_UNWINDING>
52static void **NextStackFrame(void **old_sp) {
53 void **new_sp = (void **) *old_sp;
54
55 // Check that the transition from frame pointer old_sp to frame
56 // pointer new_sp isn't clearly bogus
57 if (STRICT_UNWINDING) {
58 // With the stack growing downwards, older stack frame must be
59 // at a greater address that the current one.
60 if (new_sp <= old_sp) return NULL;
61 // Assume stack frames larger than 100,000 bytes are bogus.
62 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
63 } else {
64 // In the non-strict mode, allow discontiguous stack frames.
65 // (alternate-signal-stacks for example).
66 if (new_sp == old_sp) return NULL;
67 // And allow frames upto about 1MB.
68 if ((new_sp > old_sp)
69 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
70 }
71 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
72 return new_sp;
73}
74
75// This ensures that GetStackTrace stes up the Link Register properly.
76void StacktracePowerPCDummyFunction() __attribute__((noinline));
77void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
78#endif // BASE_STACKTRACE_POWERPC_INL_H_
79
80// Note: this part of the file is included several times.
81// Do not put globals below.
82
83// The following 4 functions are generated from the code below:
84// GetStack{Trace,Frames}()
85// GetStack{Trace,Frames}WithContext()
86//
87// These functions take the following args:
88// void** result: the stack-trace, as an array
89// int* sizes: the size of each stack frame, as an array
90// (GetStackFrames* only)
91// int max_depth: the size of the result (and sizes) array(s)
92// int skip_count: how many stack pointers to skip before storing in result
93// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
94int GET_STACK_TRACE_OR_FRAMES {
95 void **sp;
96 // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
97 // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
98 // different asm syntax. I don't know quite the best way to discriminate
99 // systems using the old as from the new one; I've gone with __APPLE__.
100 // TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2
Brian Silverman20350ac2021-11-17 18:19:55 -0800101#ifdef __FreeBSD__
102 __asm__ volatile ("mr %0,1" : "=r" (sp));
103#else
Austin Schuh745610d2015-09-06 18:19:50 -0700104 __asm__ volatile ("mr %0,r1" : "=r" (sp));
Brian Silverman20350ac2021-11-17 18:19:55 -0800105#endif
Austin Schuh745610d2015-09-06 18:19:50 -0700106
107 // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
108 // entry that holds the return address of the subroutine call (what
109 // instruction we run after our function finishes). This is the
110 // same as the stack-pointer of our parent routine, which is what we
111 // want here. While the compiler will always(?) set up LR for
112 // subroutine calls, it may not for leaf functions (such as this one).
113 // This routine forces the compiler (at least gcc) to push it anyway.
114 StacktracePowerPCDummyFunction();
115
116#if IS_STACK_FRAMES
117 // Note we do *not* increment skip_count here for the SYSV ABI. If
118 // we did, the list of stack frames wouldn't properly match up with
119 // the list of return addresses. Note this means the top pc entry
120 // is probably bogus for linux/ppc (and other SYSV-ABI systems).
121#else
122 // The LR save area is used by the callee, so the top entry is bogus.
123 skip_count++;
124#endif
125
126 int n = 0;
127 while (sp && n < max_depth) {
128 // The GetStackFrames routine is called when we are in some
129 // informational context (the failure signal handler for example).
130 // Use the non-strict unwinding rules to produce a stack trace
131 // that is as complete as possible (even if it contains a few
132 // bogus entries in some rare cases).
133 void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
134
135 if (skip_count > 0) {
136 skip_count--;
137 } else {
138 // PowerPC has 3 main ABIs, which say where in the stack the
139 // Link Register is. For DARWIN and AIX (used by apple and
140 // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc),
141 // it's in sp[1].
142#if defined(__PPC64__)
143 // This check is in case the compiler doesn't define _CALL_AIX/etc.
144 result[n] = *(sp+2);
145#elif defined(__linux)
146 // This check is in case the compiler doesn't define _CALL_SYSV.
147 result[n] = *(sp+1);
148#endif
149
150#if IS_STACK_FRAMES
151 if (next_sp > sp) {
152 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
153 } else {
154 // A frame-size of 0 is used to indicate unknown frame size.
155 sizes[n] = 0;
156 }
157#endif
158 n++;
159 }
160 sp = next_sp;
161 }
162 return n;
163}