4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <mdb/mdb_modapi.h>
27 #include <mdb/mdb_ctf.h>
29 #include <sys/types.h>
30 #include <sys/regset.h>
31 #include <sys/stack.h>
32 #include <sys/thread.h>
33 #include <sys/modctl.h>
35 #include "findstack.h"
39 #define TOO_BIG_FOR_A_STACK (1024 * 1024)
41 #define KTOU(p) ((p) - kbase + ubase)
42 #define UTOK(p) ((p) - ubase + kbase)
44 #define CRAWL_FOUNDALL (-1)
46 #if defined(__i386) || defined(__amd64)
58 * Given a stack pointer, try to crawl down it to the bottom.
59 * "frame" is a VA in MDB's address space.
61 * Returns the number of frames successfully crawled down, or
62 * CRAWL_FOUNDALL if it got to the bottom of the stack.
65 crawl(uintptr_t frame
, uintptr_t kbase
, uintptr_t ktop
, uintptr_t ubase
,
66 int kill_fp
, findstack_info_t
*fsip
)
71 fsip
->fsi_overflow
= 0;
73 fs_dprintf(("<0> frame = %p, kbase = %p, ktop = %p, ubase = %p\n",
74 frame
, kbase
, ktop
, ubase
));
77 long *fpp
= (long *)&((struct rwindow
*)frame
)->rw_fp
;
79 fs_dprintf(("<1> fpp = %p, frame = %p\n", fpp
, frame
));
81 if ((frame
& (STACK_ALIGN
- 1)) != 0)
84 fp
= ((struct rwindow
*)frame
)->rw_fp
+ STACK_BIAS
;
85 if (fsip
->fsi_depth
< fsip
->fsi_max_depth
)
86 fsip
->fsi_stack
[fsip
->fsi_depth
++] =
87 ((struct rwindow
*)frame
)->rw_rtn
;
89 fsip
->fsi_overflow
= 1;
91 fs_dprintf(("<2> fp = %p\n", fp
));
94 return (CRAWL_FOUNDALL
);
95 fs_dprintf(("<3> not at base\n"));
97 #if defined(__i386) || defined(__amd64)
98 if (ktop
- fp
== sizeof (struct rwindow
)) {
99 fs_dprintf(("<4> found base\n"));
100 return (CRAWL_FOUNDALL
);
104 fs_dprintf(("<5> fp = %p, kbase = %p, ktop - size = %p\n",
105 fp
, kbase
, ktop
- sizeof (struct rwindow
)));
107 if (fp
< kbase
|| fp
>= (ktop
- sizeof (struct rwindow
)))
111 fs_dprintf(("<6> frame = %p\n", frame
));
114 * NULL out the old %fp so we don't go down this stack
118 fs_dprintf(("<7> fpp = %p\n", fpp
));
122 fs_dprintf(("<8> levels = %d\n", levels
));
131 stacks_findstack(uintptr_t addr
, findstack_info_t
*fsip
, uint_t print_warnings
)
135 uintptr_t ubase
, utop
;
136 uintptr_t kbase
, ktop
;
139 fsip
->fsi_failed
= 0;
143 fsip
->fsi_overflow
= 0;
145 bzero(&thr
, sizeof (thr
));
146 if (mdb_ctf_vread(&thr
, "kthread_t", addr
,
147 MDB_CTF_VREAD_IGNORE_ALL
) == -1) {
149 mdb_warn("couldn't read thread at %p\n", addr
);
150 fsip
->fsi_failed
= FSI_FAIL_BADTHREAD
;
154 fsip
->fsi_sobj_ops
= (uintptr_t)thr
.t_sobj_ops
;
155 fsip
->fsi_tstate
= thr
.t_state
;
156 fsip
->fsi_panic
= !!(thr
.t_flag
& T_PANIC
);
158 if ((thr
.t_schedflag
& TS_LOAD
) == 0) {
160 mdb_warn("thread %p isn't in memory\n", addr
);
161 fsip
->fsi_failed
= FSI_FAIL_NOTINMEMORY
;
165 if (thr
.t_stk
< thr
.t_stkbase
) {
168 "stack base or stack top corrupt for thread %p\n",
170 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
174 kbase
= (uintptr_t)thr
.t_stkbase
;
175 ktop
= (uintptr_t)thr
.t_stk
;
176 stksz
= ktop
- kbase
;
180 * The stack on amd64 is intentionally misaligned, so ignore the top
181 * half-frame. See thread_stk_init(). When handling traps, the frame
182 * is automatically aligned by the hardware, so we only alter ktop if
185 if ((ktop
& (STACK_ALIGN
- 1)) != 0)
186 ktop
-= STACK_ENTRY_ALIGN
;
190 * If the stack size is larger than a meg, assume that it's bogus.
192 if (stksz
> TOO_BIG_FOR_A_STACK
) {
194 mdb_warn("stack size for thread %p is too big to be "
195 "reasonable\n", addr
);
196 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
201 * This could be (and was) a UM_GC allocation. Unfortunately,
202 * stksz tends to be very large. As currently implemented, dcmds
203 * invoked as part of pipelines don't have their UM_GC-allocated
204 * memory freed until the pipeline completes. With stksz in the
205 * neighborhood of 20k, the popular ::walk thread |::findstack
206 * pipeline can easily run memory-constrained debuggers (kmdb) out
207 * of memory. This can be changed back to a gc-able allocation when
208 * the debugger is changed to free UM_GC memory more promptly.
210 ubase
= (uintptr_t)mdb_alloc(stksz
, UM_SLEEP
);
211 utop
= ubase
+ stksz
;
212 if (mdb_vread((caddr_t
)ubase
, stksz
, kbase
) != stksz
) {
213 mdb_free((void *)ubase
, stksz
);
215 mdb_warn("couldn't read entire stack for thread %p\n",
217 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
222 * Try the saved %sp first, if it looks reasonable.
224 sp
= KTOU((uintptr_t)thr
.t_sp
+ STACK_BIAS
);
225 if (sp
>= ubase
&& sp
<= utop
) {
226 if (crawl(sp
, kbase
, ktop
, ubase
, 0, fsip
) == CRAWL_FOUNDALL
) {
227 fsip
->fsi_sp
= (uintptr_t)thr
.t_sp
;
229 fsip
->fsi_pc
= (uintptr_t)thr
.t_pc
;
236 * Now walk through the whole stack, starting at the base,
237 * trying every possible "window".
240 win
+ sizeof (struct rwindow
) <= utop
;
241 win
+= sizeof (struct rwindow
*)) {
242 if (crawl(win
, kbase
, ktop
, ubase
, 1, fsip
) == CRAWL_FOUNDALL
) {
243 fsip
->fsi_sp
= UTOK(win
) - STACK_BIAS
;
249 * We didn't conclusively find the stack. So we'll take another lap,
250 * and print out anything that looks possible.
253 mdb_printf("Possible stack pointers for thread %p:\n", addr
);
254 (void) mdb_vread((caddr_t
)ubase
, stksz
, kbase
);
257 win
+ sizeof (struct rwindow
) <= utop
;
258 win
+= sizeof (struct rwindow
*)) {
259 uintptr_t fp
= ((struct rwindow
*)win
)->rw_fp
;
262 if ((levels
= crawl(win
, kbase
, ktop
, ubase
, 1, fsip
)) > 1) {
264 mdb_printf(" %p (%d)\n", fp
, levels
);
265 } else if (levels
== CRAWL_FOUNDALL
) {
267 * If this is a live system, the stack could change
268 * between the two mdb_vread(ubase, utop, kbase)'s,
269 * and we could have a fully valid stack here.
271 fsip
->fsi_sp
= UTOK(win
) - STACK_BIAS
;
277 fsip
->fsi_overflow
= 0;
278 fsip
->fsi_failed
= FSI_FAIL_STACKNOTFOUND
;
280 mdb_free((void *)ubase
, stksz
);
283 mdb_free((void *)ubase
, stksz
);
288 stacks_findstack_cleanup()
293 stacks_module_cb(uintptr_t addr
, const modctl_t
*mp
, stacks_module_t
*smp
)
295 char mod_modname
[MODMAXNAMELEN
+ 1];
297 if (!mp
->mod_modname
)
300 if (mdb_readstr(mod_modname
, sizeof (mod_modname
),
301 (uintptr_t)mp
->mod_modname
) == -1) {
302 mdb_warn("failed to read mod_modname in \"modctl\" walk");
306 if (strcmp(smp
->sm_name
, mod_modname
))
309 smp
->sm_text
= (uintptr_t)mp
->mod_text
;
310 smp
->sm_size
= mp
->mod_text_size
;
316 stacks_module(stacks_module_t
*smp
)
318 if (mdb_walk("modctl", (mdb_walk_cb_t
)stacks_module_cb
, smp
) != 0) {
319 mdb_warn("cannot walk \"modctl\"");
328 print_sobj_help(int type
, const char *name
, const char *ops_name
, void *ign
)
330 mdb_printf(" %s", name
);
335 print_tstate_help(uint_t state
, const char *name
, void *ignored
)
337 mdb_printf(" %s", name
);
344 "::stacks processes all of the thread stacks on the system, grouping\n"
345 "together threads which have the same:\n"
348 " * Sync object type, and\n"
349 " * PCs in their stack trace.\n"
351 "The default output (no address or options) is just a dump of the thread\n"
352 "groups in the system. For a view of active threads, use \"::stacks -i\",\n"
353 "which filters out FREE threads (interrupt threads which are currently\n"
354 "inactive) and threads sleeping on a CV. (Note that those threads may still\n"
355 "be noteworthy; this is just for a first glance.) More general filtering\n"
356 "options are described below, in the \"FILTERS\" section.\n"
358 "::stacks can be used in a pipeline. The input to ::stacks is one or more\n"
359 "thread pointers. For example, to get a summary of threads in a process,\n"
362 " %<b>procp%</b>::walk thread | ::stacks\n"
364 "When output into a pipe, ::stacks prints all of the threads input,\n"
365 "filtered by the given filtering options. This means that multiple\n"
366 "::stacks invocations can be piped together to achieve more complicated\n"
367 "filters. For example, to get threads which have both 'fop_read' and\n"
368 "'cv_wait_sig_swap' in their stack trace, you could do:\n"
370 " ::stacks -c fop_read | ::stacks -c cv_wait_sig_swap_core\n"
372 "To get the full list of threads in each group, use the '-a' flag:\n"
377 mdb_printf("%<b>OPTIONS%</b>\n");
380 " -a Print all of the grouped threads, instead of just a count.\n"
381 " -f Force a re-run of the thread stack gathering.\n"
382 " -v Be verbose about thread stack gathering.\n"
385 mdb_printf("%<b>FILTERS%</b>\n");
388 " -i Show active threads; equivalent to '-S CV -T FREE'.\n"
389 " -c func[+offset]\n"
390 " Only print threads whose stacks contain func/func+offset.\n"
391 " -C func[+offset]\n"
392 " Only print threads whose stacks do not contain func/func+offset.\n"
394 " Only print threads whose stacks contain functions from module.\n"
396 " Only print threads whose stacks do not contain functions from\n"
399 " Only print threads which are on a 'type' synchronization object\n"
402 " Only print threads which are not on a 'type' SOBJ.\n"
404 " Only print threads which are in thread state 'tstate'.\n"
406 " Only print threads which are not in thread state 'tstate'.\n"
408 mdb_printf(" SOBJ types:");
409 sobj_type_walk(print_sobj_help
, NULL
);
411 mdb_printf("Thread states:");
412 thread_walk_states(print_tstate_help
, NULL
);
413 mdb_printf(" panic\n");