4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 * Common helper functions for stack walking. The ISA-specific code is found in
33 * Pstack_iter() in Pisadep.c.
47 * Utility function to prevent stack loops from running on forever by
48 * detecting when there is a stack loop (the %fp has been seen before).
51 stack_loop(prgreg_t fp
, prgreg_t
**prevfpp
, int *nfpp
, uint_t
*pfpsizep
)
53 prgreg_t
*prevfp
= *prevfpp
;
54 uint_t pfpsize
= *pfpsizep
;
58 for (i
= 0; i
< nfp
; i
++) {
60 return (1); /* stack loop detected */
64 pfpsize
= pfpsize
? pfpsize
* 2 : 16;
65 prevfp
= realloc(prevfp
, pfpsize
* sizeof (prgreg_t
));
67 * Just assume there is no loop in the face of allocation
68 * failure; the caller still has the original prevfp pointer.
83 * Signal Frame Detection
85 * In order to facilitate detection and processing of signal handler frames
86 * during a stack backtrace, we define a set of utility routines to operate on
87 * a uclist (ucontext address list), and then use these routines in the various
88 * implementations of Pstack_iter below. Certain source-level debuggers and
89 * virtual machines that shall remain nameless believe that in order to detect
90 * signal handler frames, one must hard-code checks for symbol names defined
91 * in libc and libthread and knowledge of their implementation. We make no
92 * such assumptions, allowing us to operate on programs that manipulate their
93 * underlying kernel signal handlers (i.e. use __sigaction) and to not require
94 * changes in the face of future library modifications.
96 * A signal handler frame is essentially a set of data pushed on to the user
97 * stack by the kernel prior to returning to the user program in one of the
98 * pre-defined signal handlers. The signal handler itself receives the signal
99 * number, an optional pointer to a siginfo_t, and a pointer to the interrupted
100 * ucontext as arguments. When performing a stack backtrace, we would like to
101 * detect these frames so that we can correctly return the interrupted program
102 * counter and frame pointer as a separate frame. When a signal handler frame
103 * is constructed on the stack by the kernel, the signalled LWP has its
104 * lwp_oldcontext member (exported through /proc as lwpstatus.pr_oldcontext)
105 * set to the user address at which the ucontext_t was placed on the LWP's
106 * stack. The ucontext_t's uc_link member is set to the previous value of
107 * lwp_oldcontext. Thus when signal handlers are active, pr_oldcontext will
108 * point to the first element of a linked list of ucontext_t addresses.
110 * The stack layout for a signal handler frame is as follows:
112 * SPARC v7/v9: Intel ia32:
113 * +--------------+ - high +--------------+ -
114 * | struct fq | ^ addrs | siginfo_t | optional
115 * +--------------+ | ^ +--------------+ -
116 * | gwindows_t | | | ucontext_t | ^
117 * +--------------+ optional +--------------+ |
118 * | siginfo_t | | ucontext_t * | |
119 * +--------------+ | | +--------------+
120 * | xregs data | v v | siginfo_t * | mandatory
121 * +--------------+ - low +--------------+
122 * | ucontext_t | ^ addrs | int (signo) | |
123 * +--------------+ mandatory +--------------+ |
124 * | struct frame | v | struct frame | v
125 * +--------------+ - <- %sp on resume +--------------+ - <- %esp on resume
129 * | siginfo_t | optional
134 * +--------------+ mandatory
138 * +--------------+ - <- %rsp on resume
140 * The bottom-most struct frame is actually constructed by the kernel by
141 * copying the previous stack frame, allowing naive backtrace code to simply
142 * skip over the interrupted frame. The copied frame is never really used,
143 * since it is presumed the libc or libthread signal handler wrapper function
144 * will explicitly setcontext(2) to the interrupted context if the user
145 * program's handler returns. If we detect a signal handler frame, we simply
146 * read the interrupted context structure from the stack, use its embedded
147 * gregs to construct the register set for the interrupted frame, and then
148 * continue our backtrace. Detecting the frame itself is easy according to
149 * the diagram ("oldcontext" represents any element in the uc_link chain):
152 * %fp + sizeof (struct frame) == oldcontext
155 * %ebp + sizeof (struct frame) + (3 * regsize) == oldcontext
158 * %rbp + sizeof (struct frame) + (2 * regsize) == oldcontext
160 * A final complication is that we want libproc to support backtraces from
161 * arbitrary addresses without the caller passing in an LWP id. To do this,
162 * we must first determine all the known oldcontexts by iterating over all
163 * LWPs and following their pr_oldcontext pointers. We optimize our search
164 * by discarding NULL pointers and pointers whose value is less than that
165 * of the initial stack pointer (since stacks grow down from high memory),
166 * and then sort the resulting list by virtual address so we can binary search.
170 load_uclist(uclist_t
*ucl
, const lwpstatus_t
*psp
)
172 struct ps_prochandle
*P
= ucl
->uc_proc
;
173 uintptr_t addr
= psp
->pr_oldcontext
;
175 uintptr_t *new_addrs
;
183 if (ucl
->uc_nelems
== ucl
->uc_size
) {
184 new_size
= ucl
->uc_size
? ucl
->uc_size
* 2 : 16;
185 new_addrs
= realloc(ucl
->uc_addrs
,
186 new_size
* sizeof (uintptr_t));
188 if (new_addrs
!= NULL
) {
189 ucl
->uc_addrs
= new_addrs
;
190 ucl
->uc_size
= new_size
;
192 break; /* abort if allocation failure */
195 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
198 if (Pread(P
, &u32
, sizeof (u32
), addr
) != sizeof (u32
))
199 break; /* abort if we fail to read ucontext */
200 uc
.uc_link
= (ucontext_t
*)(uintptr_t)u32
.uc_link
;
203 if (Pread(P
, &uc
, sizeof (uc
), addr
) != sizeof (uc
))
204 break; /* abort if we fail to read ucontext */
206 dprintf("detected lwp %d signal context at %p\n",
207 (int)psp
->pr_lwpid
, (void *)addr
);
208 ucl
->uc_addrs
[ucl
->uc_nelems
++] = addr
;
210 addr
= (uintptr_t)uc
.uc_link
;
213 * Abort if we find a NULL uc_link pointer or a duplicate
214 * entry which could indicate a cycle or a very peculiar
215 * interference pattern between threads.
220 for (i
= 0; i
< ucl
->uc_nelems
- 1; i
++) {
221 if (ucl
->uc_addrs
[i
] == addr
)
230 sort_uclist(const void *lhp
, const void *rhp
)
232 uintptr_t lhs
= *((const uintptr_t *)lhp
);
233 uintptr_t rhs
= *((const uintptr_t *)rhp
);
243 init_uclist(uclist_t
*ucl
, struct ps_prochandle
*P
)
245 if ((P
->state
== PS_STOP
|| P
->state
== PS_DEAD
) &&
246 P
->ucaddrs
!= NULL
) {
248 ucl
->uc_addrs
= P
->ucaddrs
;
249 ucl
->uc_nelems
= P
->ucnelems
;
250 ucl
->uc_size
= P
->ucnelems
;
256 ucl
->uc_addrs
= NULL
;
260 (void) Plwp_iter(P
, (proc_lwp_f
*)load_uclist
, ucl
);
261 qsort(ucl
->uc_addrs
, ucl
->uc_nelems
, sizeof (uintptr_t), sort_uclist
);
263 if (P
->state
== PS_STOP
|| P
->state
== PS_DEAD
) {
264 P
->ucaddrs
= ucl
->uc_addrs
;
265 P
->ucnelems
= ucl
->uc_nelems
;
273 free_uclist(uclist_t
*ucl
)
275 if (!ucl
->uc_cached
&& ucl
->uc_addrs
!= NULL
)
280 find_uclink(uclist_t
*ucl
, uintptr_t addr
)
282 if (ucl
->uc_nelems
!= 0) {
283 return (bsearch(&addr
, ucl
->uc_addrs
, ucl
->uc_nelems
,
284 sizeof (uintptr_t), sort_uclist
) != NULL
);