xref: /onnv-gate/usr/src/lib/libumem/common/getpcstack.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5891Sraf  * Common Development and Distribution License (the "License").
6*5891Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*5891Sraf 
220Sstevel@tonic-gate /*
23*5891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "misc.h"
300Sstevel@tonic-gate #include <ucontext.h>
310Sstevel@tonic-gate #include <sys/frame.h>
320Sstevel@tonic-gate #include <sys/stack.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
360Sstevel@tonic-gate extern void flush_windows(void);
370Sstevel@tonic-gate #define	UMEM_FRAMESIZE	MINFRAME
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * On x86, MINFRAME is defined to be 0, but we want to be sure we can
420Sstevel@tonic-gate  * dereference the entire frame structure.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate #define	UMEM_FRAMESIZE	(sizeof (struct frame))
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #else
470Sstevel@tonic-gate #error needs update for new architecture
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
520Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate /*ARGSUSED*/
550Sstevel@tonic-gate int
getpcstack(uintptr_t * pcstack,int pcstack_limit,int check_signal)560Sstevel@tonic-gate getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	struct frame *fp;
590Sstevel@tonic-gate 	struct frame *nextfp, *minfp;
600Sstevel@tonic-gate 	int depth = 0;
610Sstevel@tonic-gate 	uintptr_t base = 0;
620Sstevel@tonic-gate 	size_t size = 0;
630Sstevel@tonic-gate #ifndef UMEM_STANDALONE
640Sstevel@tonic-gate 	int on_altstack = 0;
650Sstevel@tonic-gate 	uintptr_t sigbase = 0;
660Sstevel@tonic-gate 	size_t sigsize = 0;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	stack_t st;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (stack_getbounds(&st) != 0) {
710Sstevel@tonic-gate 		if (thr_stksegment(&st) != 0 ||
720Sstevel@tonic-gate 		    (uintptr_t)st.ss_sp < st.ss_size) {
730Sstevel@tonic-gate 			return (0);		/* unable to get stack bounds */
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 		/*
760Sstevel@tonic-gate 		 * thr_stksegment(3C) has a slightly different interface than
770Sstevel@tonic-gate 		 * stack_getbounds(3C) -- correct it
780Sstevel@tonic-gate 		 */
790Sstevel@tonic-gate 		st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
800Sstevel@tonic-gate 		st.ss_flags = 0;		/* can't be on-stack */
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	on_altstack = (st.ss_flags & SS_ONSTACK);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (st.ss_size != 0) {
850Sstevel@tonic-gate 		base = (uintptr_t)st.ss_sp;
860Sstevel@tonic-gate 		size = st.ss_size;
870Sstevel@tonic-gate 	} else {
880Sstevel@tonic-gate 		/*
890Sstevel@tonic-gate 		 * If size == 0, then ss_sp is the *top* of the stack.
900Sstevel@tonic-gate 		 *
910Sstevel@tonic-gate 		 * Since we only allow increasing frame pointers, and we
920Sstevel@tonic-gate 		 * know our caller set his up correctly, we can treat ss_sp
930Sstevel@tonic-gate 		 * as an upper bound safely.
940Sstevel@tonic-gate 		 */
950Sstevel@tonic-gate 		base = 0;
960Sstevel@tonic-gate 		size = (uintptr_t)st.ss_sp;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (check_signal != 0) {
1000Sstevel@tonic-gate 		void (*sigfunc)() = NULL;
1010Sstevel@tonic-gate 		int sigfuncsize = 0;
1020Sstevel@tonic-gate 		extern void thr_sighndlrinfo(void (**)(), int *);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		thr_sighndlrinfo(&sigfunc, &sigfuncsize);
1050Sstevel@tonic-gate 		sigbase = (uintptr_t)sigfunc;
1060Sstevel@tonic-gate 		sigsize = sigfuncsize;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate #else /* UMEM_STANDALONE */
1090Sstevel@tonic-gate 	base = (uintptr_t)umem_min_stack;
1100Sstevel@tonic-gate 	size = umem_max_stack - umem_min_stack;
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * shorten size so that fr_savfp and fr_savpc will be within the stack
1150Sstevel@tonic-gate 	 * bounds.
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	if (size >= UMEM_FRAMESIZE - 1)
1180Sstevel@tonic-gate 		size -= (UMEM_FRAMESIZE - 1);
1190Sstevel@tonic-gate 	else
1200Sstevel@tonic-gate 		size = 0;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
1230Sstevel@tonic-gate 	flush_windows();
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/* LINTED alignment */
1270Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	minfp = fp;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (((uintptr_t)fp - base) >= size)
1320Sstevel@tonic-gate 		return (0);	/* the frame pointer isn't in our stack */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	while (depth < pcstack_limit) {
1350Sstevel@tonic-gate 		uintptr_t tmp;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		/* LINTED alignment */
1380Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
1390Sstevel@tonic-gate 		tmp = (uintptr_t)nextfp;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		/*
1420Sstevel@tonic-gate 		 * Check nextfp for validity.  It must be properly aligned,
1430Sstevel@tonic-gate 		 * increasing compared to the last %fp (or the top of the
1440Sstevel@tonic-gate 		 * stack we just switched to), and it must be inside
1450Sstevel@tonic-gate 		 * [base, base + size).
1460Sstevel@tonic-gate 		 */
1470Sstevel@tonic-gate 		if (tmp != SA(tmp))
1480Sstevel@tonic-gate 			break;
1490Sstevel@tonic-gate 		else if (nextfp <= minfp || (tmp - base) >= size) {
1500Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1510Sstevel@tonic-gate 			if (tmp == NULL || !on_altstack)
1520Sstevel@tonic-gate 				break;
1530Sstevel@tonic-gate 			/*
1540Sstevel@tonic-gate 			 * If we're on an alternate signal stack, try jumping
1550Sstevel@tonic-gate 			 * to the main thread stack.
1560Sstevel@tonic-gate 			 *
1570Sstevel@tonic-gate 			 * If the main thread stack has an unlimited size, we
1580Sstevel@tonic-gate 			 * punt, since we don't know where the frame pointer's
1590Sstevel@tonic-gate 			 * been.
1600Sstevel@tonic-gate 			 *
1610Sstevel@tonic-gate 			 * (thr_stksegment() returns the *top of stack*
1620Sstevel@tonic-gate 			 * in ss_sp, not the bottom)
1630Sstevel@tonic-gate 			 */
1640Sstevel@tonic-gate 			if (thr_stksegment(&st) == 0) {
1650Sstevel@tonic-gate 				if (st.ss_size >= (uintptr_t)st.ss_sp ||
1660Sstevel@tonic-gate 				    st.ss_size < UMEM_FRAMESIZE - 1)
1670Sstevel@tonic-gate 					break;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 				on_altstack = 0;
1700Sstevel@tonic-gate 				base = (uintptr_t)st.ss_sp - st.ss_size;
1710Sstevel@tonic-gate 				size = st.ss_size - (UMEM_FRAMESIZE - 1);
1720Sstevel@tonic-gate 				minfp = (struct frame *)base;
1730Sstevel@tonic-gate 				continue;		/* try again */
1740Sstevel@tonic-gate 			}
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1800Sstevel@tonic-gate 		if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
1810Sstevel@tonic-gate 			umem_panic("called from signal handler");
1820Sstevel@tonic-gate #endif
1830Sstevel@tonic-gate 		pcstack[depth++] = fp->fr_savpc;
1840Sstevel@tonic-gate 		fp = nextfp;
1850Sstevel@tonic-gate 		minfp = fp;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	return (depth);
1880Sstevel@tonic-gate }
189