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.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
27 #include <mdb/mdb_modapi.h>
28 #include <mdb/mdb_ctf.h>
30 #include <sys/types.h>
31 #include <sys/regset.h>
32 #include <sys/stack.h>
33 #include <sys/thread.h>
34 #include <sys/modctl.h>
36 #include "findstack.h"
40 #define TOO_BIG_FOR_A_STACK (1024 * 1024)
42 #define KTOU(p) ((p) - kbase + ubase)
43 #define UTOK(p) ((p) - ubase + kbase)
45 #define CRAWL_FOUNDALL (-1)
47 #if defined(__i386) || defined(__amd64)
59 * Given a stack pointer, try to crawl down it to the bottom.
60 * "frame" is a VA in MDB's address space.
62 * Returns the number of frames successfully crawled down, or
63 * CRAWL_FOUNDALL if it got to the bottom of the stack.
66 crawl(uintptr_t frame
, uintptr_t kbase
, uintptr_t ktop
, uintptr_t ubase
,
67 int kill_fp
, findstack_info_t
*fsip
)
72 fsip
->fsi_overflow
= 0;
74 fs_dprintf(("<0> frame = %p, kbase = %p, ktop = %p, ubase = %p\n",
75 frame
, kbase
, ktop
, ubase
));
78 long *fpp
= (long *)&((struct rwindow
*)frame
)->rw_fp
;
80 fs_dprintf(("<1> fpp = %p, frame = %p\n", fpp
, frame
));
82 if ((frame
& (STACK_ALIGN
- 1)) != 0)
85 fp
= ((struct rwindow
*)frame
)->rw_fp
+ STACK_BIAS
;
86 if (fsip
->fsi_depth
< fsip
->fsi_max_depth
)
87 fsip
->fsi_stack
[fsip
->fsi_depth
++] =
88 ((struct rwindow
*)frame
)->rw_rtn
;
90 fsip
->fsi_overflow
= 1;
92 fs_dprintf(("<2> fp = %p\n", fp
));
95 return (CRAWL_FOUNDALL
);
96 fs_dprintf(("<3> not at base\n"));
98 #if defined(__i386) || defined(__amd64)
99 if (ktop
- fp
== sizeof (struct rwindow
)) {
100 fs_dprintf(("<4> found base\n"));
101 return (CRAWL_FOUNDALL
);
105 fs_dprintf(("<5> fp = %p, kbase = %p, ktop - size = %p\n",
106 fp
, kbase
, ktop
- sizeof (struct rwindow
)));
108 if (fp
< kbase
|| fp
>= (ktop
- sizeof (struct rwindow
)))
112 fs_dprintf(("<6> frame = %p\n", frame
));
115 * NULL out the old %fp so we don't go down this stack
119 fs_dprintf(("<7> fpp = %p\n", fpp
));
123 fs_dprintf(("<8> levels = %d\n", levels
));
130 typedef struct mdb_findstack_kthread
{
131 struct _sobj_ops
*t_sobj_ops
;
134 ushort_t t_schedflag
;
138 } mdb_findstack_kthread_t
;
142 stacks_findstack(uintptr_t addr
, findstack_info_t
*fsip
, uint_t print_warnings
)
144 mdb_findstack_kthread_t thr
;
146 uintptr_t ubase
, utop
;
147 uintptr_t kbase
, ktop
;
150 fsip
->fsi_failed
= 0;
154 fsip
->fsi_overflow
= 0;
156 if (mdb_ctf_vread(&thr
, "kthread_t", "mdb_findstack_kthread_t",
157 addr
, print_warnings
? 0 : MDB_CTF_VREAD_QUIET
) == -1) {
158 fsip
->fsi_failed
= FSI_FAIL_BADTHREAD
;
162 fsip
->fsi_sobj_ops
= (uintptr_t)thr
.t_sobj_ops
;
163 fsip
->fsi_tstate
= thr
.t_state
;
164 fsip
->fsi_panic
= !!(thr
.t_flag
& T_PANIC
);
166 if ((thr
.t_schedflag
& TS_LOAD
) == 0) {
168 mdb_warn("thread %p isn't in memory\n", addr
);
169 fsip
->fsi_failed
= FSI_FAIL_NOTINMEMORY
;
173 if (thr
.t_stk
< thr
.t_stkbase
) {
176 "stack base or stack top corrupt for thread %p\n",
178 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
182 kbase
= (uintptr_t)thr
.t_stkbase
;
183 ktop
= (uintptr_t)thr
.t_stk
;
184 stksz
= ktop
- kbase
;
188 * The stack on amd64 is intentionally misaligned, so ignore the top
189 * half-frame. See thread_stk_init(). When handling traps, the frame
190 * is automatically aligned by the hardware, so we only alter ktop if
193 if ((ktop
& (STACK_ALIGN
- 1)) != 0)
194 ktop
-= STACK_ENTRY_ALIGN
;
198 * If the stack size is larger than a meg, assume that it's bogus.
200 if (stksz
> TOO_BIG_FOR_A_STACK
) {
202 mdb_warn("stack size for thread %p is too big to be "
203 "reasonable\n", addr
);
204 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
209 * This could be (and was) a UM_GC allocation. Unfortunately,
210 * stksz tends to be very large. As currently implemented, dcmds
211 * invoked as part of pipelines don't have their UM_GC-allocated
212 * memory freed until the pipeline completes. With stksz in the
213 * neighborhood of 20k, the popular ::walk thread |::findstack
214 * pipeline can easily run memory-constrained debuggers (kmdb) out
215 * of memory. This can be changed back to a gc-able allocation when
216 * the debugger is changed to free UM_GC memory more promptly.
218 ubase
= (uintptr_t)mdb_alloc(stksz
, UM_SLEEP
);
219 utop
= ubase
+ stksz
;
220 if (mdb_vread((caddr_t
)ubase
, stksz
, kbase
) != stksz
) {
221 mdb_free((void *)ubase
, stksz
);
223 mdb_warn("couldn't read entire stack for thread %p\n",
225 fsip
->fsi_failed
= FSI_FAIL_THREADCORRUPT
;
230 * Try the saved %sp first, if it looks reasonable.
232 sp
= KTOU((uintptr_t)thr
.t_sp
+ STACK_BIAS
);
233 if (sp
>= ubase
&& sp
<= utop
) {
234 if (crawl(sp
, kbase
, ktop
, ubase
, 0, fsip
) == CRAWL_FOUNDALL
) {
235 fsip
->fsi_sp
= (uintptr_t)thr
.t_sp
;
237 fsip
->fsi_pc
= (uintptr_t)thr
.t_pc
;
244 * Now walk through the whole stack, starting at the base,
245 * trying every possible "window".
248 win
+ sizeof (struct rwindow
) <= utop
;
249 win
+= sizeof (struct rwindow
*)) {
250 if (crawl(win
, kbase
, ktop
, ubase
, 1, fsip
) == CRAWL_FOUNDALL
) {
251 fsip
->fsi_sp
= UTOK(win
) - STACK_BIAS
;
257 * We didn't conclusively find the stack. So we'll take another lap,
258 * and print out anything that looks possible.
261 mdb_printf("Possible stack pointers for thread %p:\n", addr
);
262 (void) mdb_vread((caddr_t
)ubase
, stksz
, kbase
);
265 win
+ sizeof (struct rwindow
) <= utop
;
266 win
+= sizeof (struct rwindow
*)) {
267 uintptr_t fp
= ((struct rwindow
*)win
)->rw_fp
;
270 if ((levels
= crawl(win
, kbase
, ktop
, ubase
, 1, fsip
)) > 1) {
272 mdb_printf(" %p (%d)\n", fp
, levels
);
273 } else if (levels
== CRAWL_FOUNDALL
) {
275 * If this is a live system, the stack could change
276 * between the two mdb_vread(ubase, utop, kbase)'s,
277 * and we could have a fully valid stack here.
279 fsip
->fsi_sp
= UTOK(win
) - STACK_BIAS
;
285 fsip
->fsi_overflow
= 0;
286 fsip
->fsi_failed
= FSI_FAIL_STACKNOTFOUND
;
288 mdb_free((void *)ubase
, stksz
);
291 mdb_free((void *)ubase
, stksz
);
296 stacks_findstack_cleanup()
301 stacks_module_cb(uintptr_t addr
, const modctl_t
*mp
, stacks_module_t
*smp
)
303 char mod_modname
[MODMAXNAMELEN
+ 1];
305 if (!mp
->mod_modname
)
308 if (mdb_readstr(mod_modname
, sizeof (mod_modname
),
309 (uintptr_t)mp
->mod_modname
) == -1) {
310 mdb_warn("failed to read mod_modname in \"modctl\" walk");
314 if (strcmp(smp
->sm_name
, mod_modname
))
317 smp
->sm_text
= (uintptr_t)mp
->mod_text
;
318 smp
->sm_size
= mp
->mod_text_size
;
324 stacks_module(stacks_module_t
*smp
)
326 if (mdb_walk("modctl", (mdb_walk_cb_t
)stacks_module_cb
, smp
) != 0) {
327 mdb_warn("cannot walk \"modctl\"");
336 print_sobj_help(int type
, const char *name
, const char *ops_name
, void *ign
)
338 mdb_printf(" %s", name
);
343 print_tstate_help(uint_t state
, const char *name
, void *ignored
)
345 mdb_printf(" %s", name
);
352 "::stacks processes all of the thread stacks on the system, grouping\n"
353 "together threads which have the same:\n"
356 " * Sync object type, and\n"
357 " * PCs in their stack trace.\n"
359 "The default output (no address or options) is just a dump of the thread\n"
360 "groups in the system. For a view of active threads, use \"::stacks -i\",\n"
361 "which filters out FREE threads (interrupt threads which are currently\n"
362 "inactive) and threads sleeping on a CV. (Note that those threads may still\n"
363 "be noteworthy; this is just for a first glance.) More general filtering\n"
364 "options are described below, in the \"FILTERS\" section.\n"
366 "::stacks can be used in a pipeline. The input to ::stacks is one or more\n"
367 "thread pointers. For example, to get a summary of threads in a process,\n"
370 " %<b>procp%</b>::walk thread | ::stacks\n"
372 "When output into a pipe, ::stacks prints all of the threads input,\n"
373 "filtered by the given filtering options. This means that multiple\n"
374 "::stacks invocations can be piped together to achieve more complicated\n"
375 "filters. For example, to get threads which have both 'fop_read' and\n"
376 "'cv_wait_sig_swap' in their stack trace, you could do:\n"
378 " ::stacks -c fop_read | ::stacks -c cv_wait_sig_swap_core\n"
380 "To get the full list of threads in each group, use the '-a' flag:\n"
385 mdb_printf("%<b>OPTIONS%</b>\n");
388 " -a Print all of the grouped threads, instead of just a count.\n"
389 " -f Force a re-run of the thread stack gathering.\n"
390 " -v Be verbose about thread stack gathering.\n"
393 mdb_printf("%<b>FILTERS%</b>\n");
396 " -i Show active threads; equivalent to '-S CV -T FREE'.\n"
397 " -c func[+offset]\n"
398 " Only print threads whose stacks contain func/func+offset.\n"
399 " -C func[+offset]\n"
400 " Only print threads whose stacks do not contain func/func+offset.\n"
402 " Only print threads whose stacks contain functions from module.\n"
404 " Only print threads whose stacks do not contain functions from\n"
407 " Only print threads which are on a 'type' synchronization object\n"
410 " Only print threads which are not on a 'type' SOBJ.\n"
412 " Only print threads which are in thread state 'tstate'.\n"
414 " Only print threads which are not in thread state 'tstate'.\n"
416 mdb_printf(" SOBJ types:");
417 sobj_type_walk(print_sobj_help
, NULL
);
419 mdb_printf("Thread states:");
420 thread_walk_states(print_tstate_help
, NULL
);
421 mdb_printf(" panic\n");