xref: /illumos-gate/usr/src/lib/libproc/common/Pstack.c (revision c75682cd)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Pstack.c
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Common helper functions for stack walking.  The ISA-specific code is found in
317c478bd9Sstevel@tonic-gate  * Pstack_iter() in Pisadep.c.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "libproc.h"
407c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
417c478bd9Sstevel@tonic-gate #include "P32ton.h"
427c478bd9Sstevel@tonic-gate #include "Pstack.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Utility function to prevent stack loops from running on forever by
467c478bd9Sstevel@tonic-gate  * detecting when there is a stack loop (the %fp has been seen before).
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate int
stack_loop(prgreg_t fp,prgreg_t ** prevfpp,int * nfpp,uint_t * pfpsizep)497c478bd9Sstevel@tonic-gate stack_loop(prgreg_t fp, prgreg_t **prevfpp, int *nfpp, uint_t *pfpsizep)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = *prevfpp;
527c478bd9Sstevel@tonic-gate 	uint_t pfpsize = *pfpsizep;
537c478bd9Sstevel@tonic-gate 	int nfp = *nfpp;
547c478bd9Sstevel@tonic-gate 	int i;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfp; i++) {
577c478bd9Sstevel@tonic-gate 		if (fp == prevfp[i])
587c478bd9Sstevel@tonic-gate 			return (1); /* stack loop detected */
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (nfp == pfpsize) {
627c478bd9Sstevel@tonic-gate 		pfpsize = pfpsize ? pfpsize * 2 : 16;
637c478bd9Sstevel@tonic-gate 		prevfp = realloc(prevfp, pfpsize * sizeof (prgreg_t));
647c478bd9Sstevel@tonic-gate 		/*
657c478bd9Sstevel@tonic-gate 		 * Just assume there is no loop in the face of allocation
667c478bd9Sstevel@tonic-gate 		 * failure; the caller still has the original prevfp pointer.
677c478bd9Sstevel@tonic-gate 		 */
687c478bd9Sstevel@tonic-gate 		if (prevfp == NULL)
697c478bd9Sstevel@tonic-gate 			return (0);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	prevfp[nfp++] = fp;
737c478bd9Sstevel@tonic-gate 	*prevfpp = prevfp;
747c478bd9Sstevel@tonic-gate 	*pfpsizep = pfpsize;
757c478bd9Sstevel@tonic-gate 	*nfpp = nfp;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Signal Frame Detection
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * In order to facilitate detection and processing of signal handler frames
847c478bd9Sstevel@tonic-gate  * during a stack backtrace, we define a set of utility routines to operate on
857c478bd9Sstevel@tonic-gate  * a uclist (ucontext address list), and then use these routines in the various
867c478bd9Sstevel@tonic-gate  * implementations of Pstack_iter below.  Certain source-level debuggers and
877c478bd9Sstevel@tonic-gate  * virtual machines that shall remain nameless believe that in order to detect
887c478bd9Sstevel@tonic-gate  * signal handler frames, one must hard-code checks for symbol names defined
897c478bd9Sstevel@tonic-gate  * in libc and libthread and knowledge of their implementation.  We make no
907c478bd9Sstevel@tonic-gate  * such assumptions, allowing us to operate on programs that manipulate their
917c478bd9Sstevel@tonic-gate  * underlying kernel signal handlers (i.e. use __sigaction) and to not require
927c478bd9Sstevel@tonic-gate  * changes in the face of future library modifications.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * A signal handler frame is essentially a set of data pushed on to the user
957c478bd9Sstevel@tonic-gate  * stack by the kernel prior to returning to the user program in one of the
967c478bd9Sstevel@tonic-gate  * pre-defined signal handlers.  The signal handler itself receives the signal
977c478bd9Sstevel@tonic-gate  * number, an optional pointer to a siginfo_t, and a pointer to the interrupted
987c478bd9Sstevel@tonic-gate  * ucontext as arguments.  When performing a stack backtrace, we would like to
997c478bd9Sstevel@tonic-gate  * detect these frames so that we can correctly return the interrupted program
1007c478bd9Sstevel@tonic-gate  * counter and frame pointer as a separate frame.  When a signal handler frame
1017c478bd9Sstevel@tonic-gate  * is constructed on the stack by the kernel, the signalled LWP has its
1027c478bd9Sstevel@tonic-gate  * lwp_oldcontext member (exported through /proc as lwpstatus.pr_oldcontext)
1037c478bd9Sstevel@tonic-gate  * set to the user address at which the ucontext_t was placed on the LWP's
1047c478bd9Sstevel@tonic-gate  * stack.  The ucontext_t's uc_link member is set to the previous value of
1057c478bd9Sstevel@tonic-gate  * lwp_oldcontext.  Thus when signal handlers are active, pr_oldcontext will
1067c478bd9Sstevel@tonic-gate  * point to the first element of a linked list of ucontext_t addresses.
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * The stack layout for a signal handler frame is as follows:
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * SPARC v7/v9:                           Intel ia32:
1117c478bd9Sstevel@tonic-gate  * +--------------+ -        high         +--------------+ -
1127c478bd9Sstevel@tonic-gate  * |  struct fq   | ^        addrs        |  siginfo_t   | optional
1137c478bd9Sstevel@tonic-gate  * +--------------+ |          ^          +--------------+ -
1147c478bd9Sstevel@tonic-gate  * |  gwindows_t  |            |          |  ucontext_t  | ^
1157c478bd9Sstevel@tonic-gate  * +--------------+ optional              +--------------+ |
1167c478bd9Sstevel@tonic-gate  * |  siginfo_t   |                       | ucontext_t * | |
1177c478bd9Sstevel@tonic-gate  * +--------------+ |          |          +--------------+
1187c478bd9Sstevel@tonic-gate  * |  xregs data  | v          v          |  siginfo_t * | mandatory
1197c478bd9Sstevel@tonic-gate  * +--------------+ -         low         +--------------+
1207c478bd9Sstevel@tonic-gate  * |  ucontext_t  | ^        addrs        |  int (signo) | |
1217c478bd9Sstevel@tonic-gate  * +--------------+ mandatory             +--------------+ |
1227c478bd9Sstevel@tonic-gate  * | struct frame | v                     | struct frame | v
1237c478bd9Sstevel@tonic-gate  * +--------------+ - <- %sp on resume    +--------------+ - <- %esp on resume
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  * amd64 (64-bit):
1267c478bd9Sstevel@tonic-gate  * +--------------+ -
1277c478bd9Sstevel@tonic-gate  * |  siginfo_t   | optional
1287c478bd9Sstevel@tonic-gate  * +--------------+ -
1297c478bd9Sstevel@tonic-gate  * |  ucontext_t  | ^
1307c478bd9Sstevel@tonic-gate  * +--------------+ |
1317c478bd9Sstevel@tonic-gate  * |  siginfo_t * |
1327c478bd9Sstevel@tonic-gate  * +--------------+ mandatory
1337c478bd9Sstevel@tonic-gate  * |  int (signo) |
1347c478bd9Sstevel@tonic-gate  * +--------------+ |
1357c478bd9Sstevel@tonic-gate  * | struct frame | v
1367c478bd9Sstevel@tonic-gate  * +--------------+ - <- %rsp on resume
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * The bottom-most struct frame is actually constructed by the kernel by
1397c478bd9Sstevel@tonic-gate  * copying the previous stack frame, allowing naive backtrace code to simply
1407c478bd9Sstevel@tonic-gate  * skip over the interrupted frame.  The copied frame is never really used,
1417c478bd9Sstevel@tonic-gate  * since it is presumed the libc or libthread signal handler wrapper function
1427c478bd9Sstevel@tonic-gate  * will explicitly setcontext(2) to the interrupted context if the user
1437c478bd9Sstevel@tonic-gate  * program's handler returns.  If we detect a signal handler frame, we simply
1447c478bd9Sstevel@tonic-gate  * read the interrupted context structure from the stack, use its embedded
1457c478bd9Sstevel@tonic-gate  * gregs to construct the register set for the interrupted frame, and then
1467c478bd9Sstevel@tonic-gate  * continue our backtrace.  Detecting the frame itself is easy according to
1477c478bd9Sstevel@tonic-gate  * the diagram ("oldcontext" represents any element in the uc_link chain):
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * On SPARC v7 or v9:
1507c478bd9Sstevel@tonic-gate  * %fp + sizeof (struct frame) == oldcontext
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * On Intel ia32:
1537c478bd9Sstevel@tonic-gate  * %ebp + sizeof (struct frame) + (3 * regsize) == oldcontext
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * On amd64:
1567c478bd9Sstevel@tonic-gate  * %rbp + sizeof (struct frame) + (2 * regsize) == oldcontext
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * A final complication is that we want libproc to support backtraces from
1597c478bd9Sstevel@tonic-gate  * arbitrary addresses without the caller passing in an LWP id.  To do this,
1607c478bd9Sstevel@tonic-gate  * we must first determine all the known oldcontexts by iterating over all
1617c478bd9Sstevel@tonic-gate  * LWPs and following their pr_oldcontext pointers.  We optimize our search
1627c478bd9Sstevel@tonic-gate  * by discarding NULL pointers and pointers whose value is less than that
1637c478bd9Sstevel@tonic-gate  * of the initial stack pointer (since stacks grow down from high memory),
1647c478bd9Sstevel@tonic-gate  * and then sort the resulting list by virtual address so we can binary search.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate int
load_uclist(uclist_t * ucl,const lwpstatus_t * psp)1687c478bd9Sstevel@tonic-gate load_uclist(uclist_t *ucl, const lwpstatus_t *psp)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = ucl->uc_proc;
1717c478bd9Sstevel@tonic-gate 	uintptr_t addr = psp->pr_oldcontext;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	uintptr_t *new_addrs;
1747c478bd9Sstevel@tonic-gate 	uint_t new_size, i;
1757c478bd9Sstevel@tonic-gate 	ucontext_t uc;
1767c478bd9Sstevel@tonic-gate 
177*c75682cdSToomas Soome 	if (addr == (uintptr_t)NULL)
1787c478bd9Sstevel@tonic-gate 		return (0);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (;;) {
1817c478bd9Sstevel@tonic-gate 		if (ucl->uc_nelems == ucl->uc_size) {
1827c478bd9Sstevel@tonic-gate 			new_size = ucl->uc_size ? ucl->uc_size * 2 : 16;
1837c478bd9Sstevel@tonic-gate 			new_addrs = realloc(ucl->uc_addrs,
1847c478bd9Sstevel@tonic-gate 			    new_size * sizeof (uintptr_t));
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			if (new_addrs != NULL) {
1877c478bd9Sstevel@tonic-gate 				ucl->uc_addrs = new_addrs;
1887c478bd9Sstevel@tonic-gate 				ucl->uc_size = new_size;
1897c478bd9Sstevel@tonic-gate 			} else
1907c478bd9Sstevel@tonic-gate 				break; /* abort if allocation failure */
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate #ifdef _LP64
1937c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1947c478bd9Sstevel@tonic-gate 			ucontext32_t u32;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 			if (Pread(P, &u32, sizeof (u32), addr) != sizeof (u32))
1977c478bd9Sstevel@tonic-gate 				break; /* abort if we fail to read ucontext */
1987c478bd9Sstevel@tonic-gate 			uc.uc_link = (ucontext_t *)(uintptr_t)u32.uc_link;
1997c478bd9Sstevel@tonic-gate 		} else
2007c478bd9Sstevel@tonic-gate #endif
2017c478bd9Sstevel@tonic-gate 		if (Pread(P, &uc, sizeof (uc), addr) != sizeof (uc))
2027c478bd9Sstevel@tonic-gate 			break; /* abort if we fail to read ucontext */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		dprintf("detected lwp %d signal context at %p\n",
2057c478bd9Sstevel@tonic-gate 		    (int)psp->pr_lwpid, (void *)addr);
2067c478bd9Sstevel@tonic-gate 		ucl->uc_addrs[ucl->uc_nelems++] = addr;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		addr = (uintptr_t)uc.uc_link;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		/*
2117c478bd9Sstevel@tonic-gate 		 * Abort if we find a NULL uc_link pointer or a duplicate
2127c478bd9Sstevel@tonic-gate 		 * entry which could indicate a cycle or a very peculiar
2137c478bd9Sstevel@tonic-gate 		 * interference pattern between threads.
2147c478bd9Sstevel@tonic-gate 		 */
215*c75682cdSToomas Soome 		if (addr == (uintptr_t)NULL)
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		for (i = 0; i < ucl->uc_nelems - 1; i++) {
2197c478bd9Sstevel@tonic-gate 			if (ucl->uc_addrs[i] == addr)
2207c478bd9Sstevel@tonic-gate 				return (0);
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (0);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate int
sort_uclist(const void * lhp,const void * rhp)2287c478bd9Sstevel@tonic-gate sort_uclist(const void *lhp, const void *rhp)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	uintptr_t lhs = *((const uintptr_t *)lhp);
2317c478bd9Sstevel@tonic-gate 	uintptr_t rhs = *((const uintptr_t *)rhp);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (lhs < rhs)
2347c478bd9Sstevel@tonic-gate 		return (-1);
2357c478bd9Sstevel@tonic-gate 	if (lhs > rhs)
2367c478bd9Sstevel@tonic-gate 		return (+1);
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate void
init_uclist(uclist_t * ucl,struct ps_prochandle * P)2417c478bd9Sstevel@tonic-gate init_uclist(uclist_t *ucl, struct ps_prochandle *P)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	if ((P->state == PS_STOP || P->state == PS_DEAD) &&
2447c478bd9Sstevel@tonic-gate 	    P->ucaddrs != NULL) {
2457c478bd9Sstevel@tonic-gate 		ucl->uc_proc = P;
2467c478bd9Sstevel@tonic-gate 		ucl->uc_addrs = P->ucaddrs;
2477c478bd9Sstevel@tonic-gate 		ucl->uc_nelems = P->ucnelems;
2487c478bd9Sstevel@tonic-gate 		ucl->uc_size = P->ucnelems;
2497c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 1;
2507c478bd9Sstevel@tonic-gate 		return;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	ucl->uc_proc = P;
2547c478bd9Sstevel@tonic-gate 	ucl->uc_addrs = NULL;
2557c478bd9Sstevel@tonic-gate 	ucl->uc_nelems = 0;
2567c478bd9Sstevel@tonic-gate 	ucl->uc_size = 0;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(P, (proc_lwp_f *)load_uclist, ucl);
2597c478bd9Sstevel@tonic-gate 	qsort(ucl->uc_addrs, ucl->uc_nelems, sizeof (uintptr_t), sort_uclist);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (P->state == PS_STOP || P->state == PS_DEAD) {
2627c478bd9Sstevel@tonic-gate 		P->ucaddrs = ucl->uc_addrs;
2637c478bd9Sstevel@tonic-gate 		P->ucnelems = ucl->uc_nelems;
2647c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 1;
2657c478bd9Sstevel@tonic-gate 	} else {
2667c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 0;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate void
free_uclist(uclist_t * ucl)2717c478bd9Sstevel@tonic-gate free_uclist(uclist_t *ucl)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	if (!ucl->uc_cached && ucl->uc_addrs != NULL)
2747c478bd9Sstevel@tonic-gate 		free(ucl->uc_addrs);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate int
find_uclink(uclist_t * ucl,uintptr_t addr)2787c478bd9Sstevel@tonic-gate find_uclink(uclist_t *ucl, uintptr_t addr)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	if (ucl->uc_nelems != 0) {
2817c478bd9Sstevel@tonic-gate 		return (bsearch(&addr, ucl->uc_addrs, ucl->uc_nelems,
2827c478bd9Sstevel@tonic-gate 		    sizeof (uintptr_t), sort_uclist) != NULL);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	return (0);
2867c478bd9Sstevel@tonic-gate }
287