xref: /titanic_41/usr/src/lib/libumem/common/getpcstack.c (revision 1d4b38e0077763e7c9b20768eacb841957e787bc)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "misc.h"
30 
31 #include <ucontext.h>
32 
33 #include <sys/frame.h>
34 #include <sys/stack.h>
35 
36 #include <stdio.h>
37 
38 #if defined(__sparc) || defined(__sparcv9)
39 extern void flush_windows(void);
40 #define	UMEM_FRAMESIZE	MINFRAME
41 
42 #elif defined(__i386) || defined(__amd64)
43 /*
44  * On x86, MINFRAME is defined to be 0, but we want to be sure we can
45  * dereference the entire frame structure.
46  */
47 #define	UMEM_FRAMESIZE	(sizeof (struct frame))
48 
49 #else
50 #error needs update for new architecture
51 #endif
52 
53 /*
54  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
55  * Returns MIN(current stack depth, pcstack_limit).
56  */
57 /*ARGSUSED*/
58 int
59 getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
60 {
61 	struct frame *fp;
62 	struct frame *nextfp, *minfp;
63 	int depth = 0;
64 	uintptr_t base = 0;
65 	size_t size = 0;
66 #ifndef UMEM_STANDALONE
67 	int on_altstack = 0;
68 	uintptr_t sigbase = 0;
69 	size_t sigsize = 0;
70 
71 	stack_t st;
72 
73 	if (stack_getbounds(&st) != 0) {
74 		if (thr_stksegment(&st) != 0 ||
75 		    (uintptr_t)st.ss_sp < st.ss_size) {
76 			return (0);		/* unable to get stack bounds */
77 		}
78 		/*
79 		 * thr_stksegment(3C) has a slightly different interface than
80 		 * stack_getbounds(3C) -- correct it
81 		 */
82 		st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
83 		st.ss_flags = 0;		/* can't be on-stack */
84 	}
85 	on_altstack = (st.ss_flags & SS_ONSTACK);
86 
87 	if (st.ss_size != 0) {
88 		base = (uintptr_t)st.ss_sp;
89 		size = st.ss_size;
90 	} else {
91 		/*
92 		 * If size == 0, then ss_sp is the *top* of the stack.
93 		 *
94 		 * Since we only allow increasing frame pointers, and we
95 		 * know our caller set his up correctly, we can treat ss_sp
96 		 * as an upper bound safely.
97 		 */
98 		base = 0;
99 		size = (uintptr_t)st.ss_sp;
100 	}
101 
102 	if (check_signal != 0) {
103 		void (*sigfunc)() = NULL;
104 		int sigfuncsize = 0;
105 		extern void thr_sighndlrinfo(void (**)(), int *);
106 
107 		thr_sighndlrinfo(&sigfunc, &sigfuncsize);
108 		sigbase = (uintptr_t)sigfunc;
109 		sigsize = sigfuncsize;
110 	}
111 #else /* UMEM_STANDALONE */
112 	base = (uintptr_t)umem_min_stack;
113 	size = umem_max_stack - umem_min_stack;
114 #endif
115 
116 	/*
117 	 * shorten size so that fr_savfp and fr_savpc will be within the stack
118 	 * bounds.
119 	 */
120 	if (size >= UMEM_FRAMESIZE - 1)
121 		size -= (UMEM_FRAMESIZE - 1);
122 	else
123 		size = 0;
124 
125 #if defined(__sparc) || defined(__sparcv9)
126 	flush_windows();
127 #endif
128 
129 	/* LINTED alignment */
130 	fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
131 
132 	minfp = fp;
133 
134 	if (((uintptr_t)fp - base) >= size)
135 		return (0);	/* the frame pointer isn't in our stack */
136 
137 	while (depth < pcstack_limit) {
138 		uintptr_t tmp;
139 
140 		/* LINTED alignment */
141 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
142 		tmp = (uintptr_t)nextfp;
143 
144 		/*
145 		 * Check nextfp for validity.  It must be properly aligned,
146 		 * increasing compared to the last %fp (or the top of the
147 		 * stack we just switched to), and it must be inside
148 		 * [base, base + size).
149 		 */
150 		if (tmp != SA(tmp))
151 			break;
152 		else if (nextfp <= minfp || (tmp - base) >= size) {
153 #ifndef UMEM_STANDALONE
154 			if (tmp == NULL || !on_altstack)
155 				break;
156 			/*
157 			 * If we're on an alternate signal stack, try jumping
158 			 * to the main thread stack.
159 			 *
160 			 * If the main thread stack has an unlimited size, we
161 			 * punt, since we don't know where the frame pointer's
162 			 * been.
163 			 *
164 			 * (thr_stksegment() returns the *top of stack*
165 			 * in ss_sp, not the bottom)
166 			 */
167 			if (thr_stksegment(&st) == 0) {
168 				if (st.ss_size >= (uintptr_t)st.ss_sp ||
169 				    st.ss_size < UMEM_FRAMESIZE - 1)
170 					break;
171 
172 				on_altstack = 0;
173 				base = (uintptr_t)st.ss_sp - st.ss_size;
174 				size = st.ss_size - (UMEM_FRAMESIZE - 1);
175 				minfp = (struct frame *)base;
176 				continue;		/* try again */
177 			}
178 #endif
179 			break;
180 		}
181 
182 #ifndef UMEM_STANDALONE
183 		if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
184 			umem_panic("called from signal handler");
185 #endif
186 		pcstack[depth++] = fp->fr_savpc;
187 		fp = nextfp;
188 		minfp = fp;
189 	}
190 	return (depth);
191 }
192