4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Portions Copyright 2006 OmniTI, Inc.
30 /* #pragma ident "@(#)getpcstack.c 1.5 05/06/08 SMI" */
40 #include <sys/frame.h>
43 #include <sys/stack.h>
50 * Darwin doesn't have any exposed frame info, so give it some space.
52 #define UMEM_FRAMESIZE (2 * sizeof(long long))
54 #elif defined(__sparc) || defined(__sparcv9)
55 extern void flush_windows(void);
56 #define UMEM_FRAMESIZE MINFRAME
58 #elif defined(__i386) || defined(__amd64)
60 * On x86, MINFRAME is defined to be 0, but we want to be sure we can
61 * dereference the entire frame structure.
63 #define UMEM_FRAMESIZE (sizeof (struct frame))
65 #elif !defined(EC_UMEM_DUMMY_PCSTACK)
66 #error needs update for new architecture
70 * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking.
71 * Returns MIN(current stack depth, pcstack_limit).
75 getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
77 #ifdef EC_UMEM_DUMMY_PCSTACK
81 struct frame *nextfp, *minfp;
85 #ifndef UMEM_STANDALONE
87 uintptr_t sigbase = 0;
92 if (stack_getbounds(&st) != 0) {
93 if (thr_stksegment(&st) != 0 ||
94 (uintptr_t)st.ss_sp < st.ss_size) {
95 return (0); /* unable to get stack bounds */
98 * thr_stksegment(3C) has a slightly different interface than
99 * stack_getbounds(3C) -- correct it
101 st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
102 st.ss_flags = 0; /* can't be on-stack */
104 on_altstack = (st.ss_flags & SS_ONSTACK);
106 if (st.ss_size != 0) {
107 base = (uintptr_t)st.ss_sp;
111 * If size == 0, then ss_sp is the *top* of the stack.
113 * Since we only allow increasing frame pointers, and we
114 * know our caller set his up correctly, we can treat ss_sp
115 * as an upper bound safely.
118 size = (uintptr_t)st.ss_sp;
121 if (check_signal != 0) {
122 void (*sigfunc)() = NULL;
124 extern void thr_sighndlrinfo(void (**)(), int *);
126 thr_sighndlrinfo(&sigfunc, &sigfuncsize);
127 sigbase = (uintptr_t)sigfunc;
128 sigsize = sigfuncsize;
130 #else /* UMEM_STANDALONE */
131 base = (uintptr_t)umem_min_stack;
132 size = umem_max_stack - umem_min_stack;
136 * shorten size so that fr_savfp and fr_savpc will be within the stack
139 if (size >= UMEM_FRAMESIZE - 1)
140 size -= (UMEM_FRAMESIZE - 1);
144 #if defined(__sparc) || defined(__sparcv9)
148 /* LINTED alignment */
149 fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
153 if (((uintptr_t)fp - base) >= size)
154 return (0); /* the frame pointer isn't in our stack */
156 while (depth < pcstack_limit) {
159 /* LINTED alignment */
160 nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
161 tmp = (uintptr_t)nextfp;
164 * Check nextfp for validity. It must be properly aligned,
165 * increasing compared to the last %fp (or the top of the
166 * stack we just switched to), and it must be inside
167 * [base, base + size).
171 else if (nextfp <= minfp || (tmp - base) >= size) {
172 #ifndef UMEM_STANDALONE
173 if (tmp == NULL || !on_altstack)
176 * If we're on an alternate signal stack, try jumping
177 * to the main thread stack.
179 * If the main thread stack has an unlimited size, we
180 * punt, since we don't know where the frame pointer's
183 * (thr_stksegment() returns the *top of stack*
184 * in ss_sp, not the bottom)
186 if (thr_stksegment(&st) == 0) {
187 if (st.ss_size >= (uintptr_t)st.ss_sp ||
188 st.ss_size < UMEM_FRAMESIZE - 1)
192 base = (uintptr_t)st.ss_sp - st.ss_size;
193 size = st.ss_size - (UMEM_FRAMESIZE - 1);
194 minfp = (struct frame *)base;
195 continue; /* try again */
201 #ifndef UMEM_STANDALONE
202 if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
203 umem_panic("called from signal handler");
205 pcstack[depth++] = fp->fr_savpc;