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