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