xref: /titanic_51/usr/src/lib/libumem/common/getpcstack.c (revision a574db851cdc636fc3939b68e80d79fe7fbd57f2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a574db85Sraf  * Common Development and Distribution License (the "License").
6*a574db85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
23*a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29*a574db85Sraf #include "c_synonyms.h"
307c478bd9Sstevel@tonic-gate #include "misc.h"
317c478bd9Sstevel@tonic-gate #include <ucontext.h>
327c478bd9Sstevel@tonic-gate #include <sys/frame.h>
337c478bd9Sstevel@tonic-gate #include <sys/stack.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
377c478bd9Sstevel@tonic-gate extern void flush_windows(void);
387c478bd9Sstevel@tonic-gate #define	UMEM_FRAMESIZE	MINFRAME
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * On x86, MINFRAME is defined to be 0, but we want to be sure we can
437c478bd9Sstevel@tonic-gate  * dereference the entire frame structure.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #define	UMEM_FRAMESIZE	(sizeof (struct frame))
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate #error needs update for new architecture
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
537c478bd9Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
567c478bd9Sstevel@tonic-gate int
577c478bd9Sstevel@tonic-gate getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	struct frame *fp;
607c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp;
617c478bd9Sstevel@tonic-gate 	int depth = 0;
627c478bd9Sstevel@tonic-gate 	uintptr_t base = 0;
637c478bd9Sstevel@tonic-gate 	size_t size = 0;
647c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
657c478bd9Sstevel@tonic-gate 	int on_altstack = 0;
667c478bd9Sstevel@tonic-gate 	uintptr_t sigbase = 0;
677c478bd9Sstevel@tonic-gate 	size_t sigsize = 0;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	stack_t st;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (stack_getbounds(&st) != 0) {
727c478bd9Sstevel@tonic-gate 		if (thr_stksegment(&st) != 0 ||
737c478bd9Sstevel@tonic-gate 		    (uintptr_t)st.ss_sp < st.ss_size) {
747c478bd9Sstevel@tonic-gate 			return (0);		/* unable to get stack bounds */
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 		/*
777c478bd9Sstevel@tonic-gate 		 * thr_stksegment(3C) has a slightly different interface than
787c478bd9Sstevel@tonic-gate 		 * stack_getbounds(3C) -- correct it
797c478bd9Sstevel@tonic-gate 		 */
807c478bd9Sstevel@tonic-gate 		st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
817c478bd9Sstevel@tonic-gate 		st.ss_flags = 0;		/* can't be on-stack */
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	on_altstack = (st.ss_flags & SS_ONSTACK);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (st.ss_size != 0) {
867c478bd9Sstevel@tonic-gate 		base = (uintptr_t)st.ss_sp;
877c478bd9Sstevel@tonic-gate 		size = st.ss_size;
887c478bd9Sstevel@tonic-gate 	} else {
897c478bd9Sstevel@tonic-gate 		/*
907c478bd9Sstevel@tonic-gate 		 * If size == 0, then ss_sp is the *top* of the stack.
917c478bd9Sstevel@tonic-gate 		 *
927c478bd9Sstevel@tonic-gate 		 * Since we only allow increasing frame pointers, and we
937c478bd9Sstevel@tonic-gate 		 * know our caller set his up correctly, we can treat ss_sp
947c478bd9Sstevel@tonic-gate 		 * as an upper bound safely.
957c478bd9Sstevel@tonic-gate 		 */
967c478bd9Sstevel@tonic-gate 		base = 0;
977c478bd9Sstevel@tonic-gate 		size = (uintptr_t)st.ss_sp;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (check_signal != 0) {
1017c478bd9Sstevel@tonic-gate 		void (*sigfunc)() = NULL;
1027c478bd9Sstevel@tonic-gate 		int sigfuncsize = 0;
1037c478bd9Sstevel@tonic-gate 		extern void thr_sighndlrinfo(void (**)(), int *);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		thr_sighndlrinfo(&sigfunc, &sigfuncsize);
1067c478bd9Sstevel@tonic-gate 		sigbase = (uintptr_t)sigfunc;
1077c478bd9Sstevel@tonic-gate 		sigsize = sigfuncsize;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate #else /* UMEM_STANDALONE */
1107c478bd9Sstevel@tonic-gate 	base = (uintptr_t)umem_min_stack;
1117c478bd9Sstevel@tonic-gate 	size = umem_max_stack - umem_min_stack;
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * shorten size so that fr_savfp and fr_savpc will be within the stack
1167c478bd9Sstevel@tonic-gate 	 * bounds.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	if (size >= UMEM_FRAMESIZE - 1)
1197c478bd9Sstevel@tonic-gate 		size -= (UMEM_FRAMESIZE - 1);
1207c478bd9Sstevel@tonic-gate 	else
1217c478bd9Sstevel@tonic-gate 		size = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
1247c478bd9Sstevel@tonic-gate 	flush_windows();
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
1287c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	minfp = fp;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (((uintptr_t)fp - base) >= size)
1337c478bd9Sstevel@tonic-gate 		return (0);	/* the frame pointer isn't in our stack */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
1367c478bd9Sstevel@tonic-gate 		uintptr_t tmp;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		/* LINTED alignment */
1397c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
1407c478bd9Sstevel@tonic-gate 		tmp = (uintptr_t)nextfp;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * Check nextfp for validity.  It must be properly aligned,
1447c478bd9Sstevel@tonic-gate 		 * increasing compared to the last %fp (or the top of the
1457c478bd9Sstevel@tonic-gate 		 * stack we just switched to), and it must be inside
1467c478bd9Sstevel@tonic-gate 		 * [base, base + size).
1477c478bd9Sstevel@tonic-gate 		 */
1487c478bd9Sstevel@tonic-gate 		if (tmp != SA(tmp))
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		else if (nextfp <= minfp || (tmp - base) >= size) {
1517c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1527c478bd9Sstevel@tonic-gate 			if (tmp == NULL || !on_altstack)
1537c478bd9Sstevel@tonic-gate 				break;
1547c478bd9Sstevel@tonic-gate 			/*
1557c478bd9Sstevel@tonic-gate 			 * If we're on an alternate signal stack, try jumping
1567c478bd9Sstevel@tonic-gate 			 * to the main thread stack.
1577c478bd9Sstevel@tonic-gate 			 *
1587c478bd9Sstevel@tonic-gate 			 * If the main thread stack has an unlimited size, we
1597c478bd9Sstevel@tonic-gate 			 * punt, since we don't know where the frame pointer's
1607c478bd9Sstevel@tonic-gate 			 * been.
1617c478bd9Sstevel@tonic-gate 			 *
1627c478bd9Sstevel@tonic-gate 			 * (thr_stksegment() returns the *top of stack*
1637c478bd9Sstevel@tonic-gate 			 * in ss_sp, not the bottom)
1647c478bd9Sstevel@tonic-gate 			 */
1657c478bd9Sstevel@tonic-gate 			if (thr_stksegment(&st) == 0) {
1667c478bd9Sstevel@tonic-gate 				if (st.ss_size >= (uintptr_t)st.ss_sp ||
1677c478bd9Sstevel@tonic-gate 				    st.ss_size < UMEM_FRAMESIZE - 1)
1687c478bd9Sstevel@tonic-gate 					break;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 				on_altstack = 0;
1717c478bd9Sstevel@tonic-gate 				base = (uintptr_t)st.ss_sp - st.ss_size;
1727c478bd9Sstevel@tonic-gate 				size = st.ss_size - (UMEM_FRAMESIZE - 1);
1737c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)base;
1747c478bd9Sstevel@tonic-gate 				continue;		/* try again */
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
1817c478bd9Sstevel@tonic-gate 		if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
1827c478bd9Sstevel@tonic-gate 			umem_panic("called from signal handler");
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate 		pcstack[depth++] = fp->fr_savpc;
1857c478bd9Sstevel@tonic-gate 		fp = nextfp;
1867c478bd9Sstevel@tonic-gate 		minfp = fp;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	return (depth);
1897c478bd9Sstevel@tonic-gate }
190