xref: /titanic_52/usr/src/lib/libproc/common/Pstack.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Pstack.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Common helper functions for stack walking.  The ISA-specific code is found in
33*7c478bd9Sstevel@tonic-gate  * Pstack_iter() in Pisadep.c.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "libproc.h"
42*7c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
43*7c478bd9Sstevel@tonic-gate #include "P32ton.h"
44*7c478bd9Sstevel@tonic-gate #include "Pstack.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Utility function to prevent stack loops from running on forever by
48*7c478bd9Sstevel@tonic-gate  * detecting when there is a stack loop (the %fp has been seen before).
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate int
51*7c478bd9Sstevel@tonic-gate stack_loop(prgreg_t fp, prgreg_t **prevfpp, int *nfpp, uint_t *pfpsizep)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = *prevfpp;
54*7c478bd9Sstevel@tonic-gate 	uint_t pfpsize = *pfpsizep;
55*7c478bd9Sstevel@tonic-gate 	int nfp = *nfpp;
56*7c478bd9Sstevel@tonic-gate 	int i;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfp; i++) {
59*7c478bd9Sstevel@tonic-gate 		if (fp == prevfp[i])
60*7c478bd9Sstevel@tonic-gate 			return (1); /* stack loop detected */
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if (nfp == pfpsize) {
64*7c478bd9Sstevel@tonic-gate 		pfpsize = pfpsize ? pfpsize * 2 : 16;
65*7c478bd9Sstevel@tonic-gate 		prevfp = realloc(prevfp, pfpsize * sizeof (prgreg_t));
66*7c478bd9Sstevel@tonic-gate 		/*
67*7c478bd9Sstevel@tonic-gate 		 * Just assume there is no loop in the face of allocation
68*7c478bd9Sstevel@tonic-gate 		 * failure; the caller still has the original prevfp pointer.
69*7c478bd9Sstevel@tonic-gate 		 */
70*7c478bd9Sstevel@tonic-gate 		if (prevfp == NULL)
71*7c478bd9Sstevel@tonic-gate 			return (0);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	prevfp[nfp++] = fp;
75*7c478bd9Sstevel@tonic-gate 	*prevfpp = prevfp;
76*7c478bd9Sstevel@tonic-gate 	*pfpsizep = pfpsize;
77*7c478bd9Sstevel@tonic-gate 	*nfpp = nfp;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	return (0);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * Signal Frame Detection
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  * In order to facilitate detection and processing of signal handler frames
86*7c478bd9Sstevel@tonic-gate  * during a stack backtrace, we define a set of utility routines to operate on
87*7c478bd9Sstevel@tonic-gate  * a uclist (ucontext address list), and then use these routines in the various
88*7c478bd9Sstevel@tonic-gate  * implementations of Pstack_iter below.  Certain source-level debuggers and
89*7c478bd9Sstevel@tonic-gate  * virtual machines that shall remain nameless believe that in order to detect
90*7c478bd9Sstevel@tonic-gate  * signal handler frames, one must hard-code checks for symbol names defined
91*7c478bd9Sstevel@tonic-gate  * in libc and libthread and knowledge of their implementation.  We make no
92*7c478bd9Sstevel@tonic-gate  * such assumptions, allowing us to operate on programs that manipulate their
93*7c478bd9Sstevel@tonic-gate  * underlying kernel signal handlers (i.e. use __sigaction) and to not require
94*7c478bd9Sstevel@tonic-gate  * changes in the face of future library modifications.
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * A signal handler frame is essentially a set of data pushed on to the user
97*7c478bd9Sstevel@tonic-gate  * stack by the kernel prior to returning to the user program in one of the
98*7c478bd9Sstevel@tonic-gate  * pre-defined signal handlers.  The signal handler itself receives the signal
99*7c478bd9Sstevel@tonic-gate  * number, an optional pointer to a siginfo_t, and a pointer to the interrupted
100*7c478bd9Sstevel@tonic-gate  * ucontext as arguments.  When performing a stack backtrace, we would like to
101*7c478bd9Sstevel@tonic-gate  * detect these frames so that we can correctly return the interrupted program
102*7c478bd9Sstevel@tonic-gate  * counter and frame pointer as a separate frame.  When a signal handler frame
103*7c478bd9Sstevel@tonic-gate  * is constructed on the stack by the kernel, the signalled LWP has its
104*7c478bd9Sstevel@tonic-gate  * lwp_oldcontext member (exported through /proc as lwpstatus.pr_oldcontext)
105*7c478bd9Sstevel@tonic-gate  * set to the user address at which the ucontext_t was placed on the LWP's
106*7c478bd9Sstevel@tonic-gate  * stack.  The ucontext_t's uc_link member is set to the previous value of
107*7c478bd9Sstevel@tonic-gate  * lwp_oldcontext.  Thus when signal handlers are active, pr_oldcontext will
108*7c478bd9Sstevel@tonic-gate  * point to the first element of a linked list of ucontext_t addresses.
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * The stack layout for a signal handler frame is as follows:
111*7c478bd9Sstevel@tonic-gate  *
112*7c478bd9Sstevel@tonic-gate  * SPARC v7/v9:                           Intel ia32:
113*7c478bd9Sstevel@tonic-gate  * +--------------+ -        high         +--------------+ -
114*7c478bd9Sstevel@tonic-gate  * |  struct fq   | ^        addrs        |  siginfo_t   | optional
115*7c478bd9Sstevel@tonic-gate  * +--------------+ |          ^          +--------------+ -
116*7c478bd9Sstevel@tonic-gate  * |  gwindows_t  |            |          |  ucontext_t  | ^
117*7c478bd9Sstevel@tonic-gate  * +--------------+ optional              +--------------+ |
118*7c478bd9Sstevel@tonic-gate  * |  siginfo_t   |                       | ucontext_t * | |
119*7c478bd9Sstevel@tonic-gate  * +--------------+ |          |          +--------------+
120*7c478bd9Sstevel@tonic-gate  * |  xregs data  | v          v          |  siginfo_t * | mandatory
121*7c478bd9Sstevel@tonic-gate  * +--------------+ -         low         +--------------+
122*7c478bd9Sstevel@tonic-gate  * |  ucontext_t  | ^        addrs        |  int (signo) | |
123*7c478bd9Sstevel@tonic-gate  * +--------------+ mandatory             +--------------+ |
124*7c478bd9Sstevel@tonic-gate  * | struct frame | v                     | struct frame | v
125*7c478bd9Sstevel@tonic-gate  * +--------------+ - <- %sp on resume    +--------------+ - <- %esp on resume
126*7c478bd9Sstevel@tonic-gate  *
127*7c478bd9Sstevel@tonic-gate  * amd64 (64-bit):
128*7c478bd9Sstevel@tonic-gate  * +--------------+ -
129*7c478bd9Sstevel@tonic-gate  * |  siginfo_t   | optional
130*7c478bd9Sstevel@tonic-gate  * +--------------+ -
131*7c478bd9Sstevel@tonic-gate  * |  ucontext_t  | ^
132*7c478bd9Sstevel@tonic-gate  * +--------------+ |
133*7c478bd9Sstevel@tonic-gate  * |  siginfo_t * |
134*7c478bd9Sstevel@tonic-gate  * +--------------+ mandatory
135*7c478bd9Sstevel@tonic-gate  * |  int (signo) |
136*7c478bd9Sstevel@tonic-gate  * +--------------+ |
137*7c478bd9Sstevel@tonic-gate  * | struct frame | v
138*7c478bd9Sstevel@tonic-gate  * +--------------+ - <- %rsp on resume
139*7c478bd9Sstevel@tonic-gate  *
140*7c478bd9Sstevel@tonic-gate  * The bottom-most struct frame is actually constructed by the kernel by
141*7c478bd9Sstevel@tonic-gate  * copying the previous stack frame, allowing naive backtrace code to simply
142*7c478bd9Sstevel@tonic-gate  * skip over the interrupted frame.  The copied frame is never really used,
143*7c478bd9Sstevel@tonic-gate  * since it is presumed the libc or libthread signal handler wrapper function
144*7c478bd9Sstevel@tonic-gate  * will explicitly setcontext(2) to the interrupted context if the user
145*7c478bd9Sstevel@tonic-gate  * program's handler returns.  If we detect a signal handler frame, we simply
146*7c478bd9Sstevel@tonic-gate  * read the interrupted context structure from the stack, use its embedded
147*7c478bd9Sstevel@tonic-gate  * gregs to construct the register set for the interrupted frame, and then
148*7c478bd9Sstevel@tonic-gate  * continue our backtrace.  Detecting the frame itself is easy according to
149*7c478bd9Sstevel@tonic-gate  * the diagram ("oldcontext" represents any element in the uc_link chain):
150*7c478bd9Sstevel@tonic-gate  *
151*7c478bd9Sstevel@tonic-gate  * On SPARC v7 or v9:
152*7c478bd9Sstevel@tonic-gate  * %fp + sizeof (struct frame) == oldcontext
153*7c478bd9Sstevel@tonic-gate  *
154*7c478bd9Sstevel@tonic-gate  * On Intel ia32:
155*7c478bd9Sstevel@tonic-gate  * %ebp + sizeof (struct frame) + (3 * regsize) == oldcontext
156*7c478bd9Sstevel@tonic-gate  *
157*7c478bd9Sstevel@tonic-gate  * On amd64:
158*7c478bd9Sstevel@tonic-gate  * %rbp + sizeof (struct frame) + (2 * regsize) == oldcontext
159*7c478bd9Sstevel@tonic-gate  *
160*7c478bd9Sstevel@tonic-gate  * A final complication is that we want libproc to support backtraces from
161*7c478bd9Sstevel@tonic-gate  * arbitrary addresses without the caller passing in an LWP id.  To do this,
162*7c478bd9Sstevel@tonic-gate  * we must first determine all the known oldcontexts by iterating over all
163*7c478bd9Sstevel@tonic-gate  * LWPs and following their pr_oldcontext pointers.  We optimize our search
164*7c478bd9Sstevel@tonic-gate  * by discarding NULL pointers and pointers whose value is less than that
165*7c478bd9Sstevel@tonic-gate  * of the initial stack pointer (since stacks grow down from high memory),
166*7c478bd9Sstevel@tonic-gate  * and then sort the resulting list by virtual address so we can binary search.
167*7c478bd9Sstevel@tonic-gate  */
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate int
170*7c478bd9Sstevel@tonic-gate load_uclist(uclist_t *ucl, const lwpstatus_t *psp)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = ucl->uc_proc;
173*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = psp->pr_oldcontext;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	uintptr_t *new_addrs;
176*7c478bd9Sstevel@tonic-gate 	uint_t new_size, i;
177*7c478bd9Sstevel@tonic-gate 	ucontext_t uc;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (addr == NULL)
180*7c478bd9Sstevel@tonic-gate 		return (0);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	for (;;) {
183*7c478bd9Sstevel@tonic-gate 		if (ucl->uc_nelems == ucl->uc_size) {
184*7c478bd9Sstevel@tonic-gate 			new_size = ucl->uc_size ? ucl->uc_size * 2 : 16;
185*7c478bd9Sstevel@tonic-gate 			new_addrs = realloc(ucl->uc_addrs,
186*7c478bd9Sstevel@tonic-gate 			    new_size * sizeof (uintptr_t));
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 			if (new_addrs != NULL) {
189*7c478bd9Sstevel@tonic-gate 				ucl->uc_addrs = new_addrs;
190*7c478bd9Sstevel@tonic-gate 				ucl->uc_size = new_size;
191*7c478bd9Sstevel@tonic-gate 			} else
192*7c478bd9Sstevel@tonic-gate 				break; /* abort if allocation failure */
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate #ifdef _LP64
195*7c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_ILP32) {
196*7c478bd9Sstevel@tonic-gate 			ucontext32_t u32;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 			if (Pread(P, &u32, sizeof (u32), addr) != sizeof (u32))
199*7c478bd9Sstevel@tonic-gate 				break; /* abort if we fail to read ucontext */
200*7c478bd9Sstevel@tonic-gate 			uc.uc_link = (ucontext_t *)(uintptr_t)u32.uc_link;
201*7c478bd9Sstevel@tonic-gate 		} else
202*7c478bd9Sstevel@tonic-gate #endif
203*7c478bd9Sstevel@tonic-gate 		if (Pread(P, &uc, sizeof (uc), addr) != sizeof (uc))
204*7c478bd9Sstevel@tonic-gate 			break; /* abort if we fail to read ucontext */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 		dprintf("detected lwp %d signal context at %p\n",
207*7c478bd9Sstevel@tonic-gate 		    (int)psp->pr_lwpid, (void *)addr);
208*7c478bd9Sstevel@tonic-gate 		ucl->uc_addrs[ucl->uc_nelems++] = addr;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		addr = (uintptr_t)uc.uc_link;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		/*
213*7c478bd9Sstevel@tonic-gate 		 * Abort if we find a NULL uc_link pointer or a duplicate
214*7c478bd9Sstevel@tonic-gate 		 * entry which could indicate a cycle or a very peculiar
215*7c478bd9Sstevel@tonic-gate 		 * interference pattern between threads.
216*7c478bd9Sstevel@tonic-gate 		 */
217*7c478bd9Sstevel@tonic-gate 		if (addr == NULL)
218*7c478bd9Sstevel@tonic-gate 			break;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ucl->uc_nelems - 1; i++) {
221*7c478bd9Sstevel@tonic-gate 			if (ucl->uc_addrs[i] == addr)
222*7c478bd9Sstevel@tonic-gate 				return (0);
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	return (0);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate int
230*7c478bd9Sstevel@tonic-gate sort_uclist(const void *lhp, const void *rhp)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	uintptr_t lhs = *((const uintptr_t *)lhp);
233*7c478bd9Sstevel@tonic-gate 	uintptr_t rhs = *((const uintptr_t *)rhp);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	if (lhs < rhs)
236*7c478bd9Sstevel@tonic-gate 		return (-1);
237*7c478bd9Sstevel@tonic-gate 	if (lhs > rhs)
238*7c478bd9Sstevel@tonic-gate 		return (+1);
239*7c478bd9Sstevel@tonic-gate 	return (0);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate void
243*7c478bd9Sstevel@tonic-gate init_uclist(uclist_t *ucl, struct ps_prochandle *P)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	if ((P->state == PS_STOP || P->state == PS_DEAD) &&
246*7c478bd9Sstevel@tonic-gate 	    P->ucaddrs != NULL) {
247*7c478bd9Sstevel@tonic-gate 		ucl->uc_proc = P;
248*7c478bd9Sstevel@tonic-gate 		ucl->uc_addrs = P->ucaddrs;
249*7c478bd9Sstevel@tonic-gate 		ucl->uc_nelems = P->ucnelems;
250*7c478bd9Sstevel@tonic-gate 		ucl->uc_size = P->ucnelems;
251*7c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 1;
252*7c478bd9Sstevel@tonic-gate 		return;
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	ucl->uc_proc = P;
256*7c478bd9Sstevel@tonic-gate 	ucl->uc_addrs = NULL;
257*7c478bd9Sstevel@tonic-gate 	ucl->uc_nelems = 0;
258*7c478bd9Sstevel@tonic-gate 	ucl->uc_size = 0;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(P, (proc_lwp_f *)load_uclist, ucl);
261*7c478bd9Sstevel@tonic-gate 	qsort(ucl->uc_addrs, ucl->uc_nelems, sizeof (uintptr_t), sort_uclist);
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (P->state == PS_STOP || P->state == PS_DEAD) {
264*7c478bd9Sstevel@tonic-gate 		P->ucaddrs = ucl->uc_addrs;
265*7c478bd9Sstevel@tonic-gate 		P->ucnelems = ucl->uc_nelems;
266*7c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 1;
267*7c478bd9Sstevel@tonic-gate 	} else {
268*7c478bd9Sstevel@tonic-gate 		ucl->uc_cached = 0;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate void
273*7c478bd9Sstevel@tonic-gate free_uclist(uclist_t *ucl)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	if (!ucl->uc_cached && ucl->uc_addrs != NULL)
276*7c478bd9Sstevel@tonic-gate 		free(ucl->uc_addrs);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate int
280*7c478bd9Sstevel@tonic-gate find_uclink(uclist_t *ucl, uintptr_t addr)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	if (ucl->uc_nelems != 0) {
283*7c478bd9Sstevel@tonic-gate 		return (bsearch(&addr, ucl->uc_addrs, ucl->uc_nelems,
284*7c478bd9Sstevel@tonic-gate 		    sizeof (uintptr_t), sort_uclist) != NULL);
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	return (0);
288*7c478bd9Sstevel@tonic-gate }
289