xref: /illumos-gate/usr/src/cmd/mdb/common/modules/genunix/findstack_subr.c (revision 7014882c6a3672fd0e5d60200af8643ae53c5928)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <mdb/mdb_modapi.h>
27 #include <mdb/mdb_ctf.h>
28 
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>
34 
35 #include "findstack.h"
36 #include "thread.h"
37 #include "sobj.h"
38 
39 #define	TOO_BIG_FOR_A_STACK (1024 * 1024)
40 
41 #define	KTOU(p) ((p) - kbase + ubase)
42 #define	UTOK(p) ((p) - ubase + kbase)
43 
44 #define	CRAWL_FOUNDALL	(-1)
45 
46 #if defined(__i386) || defined(__amd64)
47 struct rwindow {
48 	uintptr_t rw_fp;
49 	uintptr_t rw_rtn;
50 };
51 #endif
52 
53 #ifndef STACK_BIAS
54 #define	STACK_BIAS	0
55 #endif
56 
57 /*
58  * Given a stack pointer, try to crawl down it to the bottom.
59  * "frame" is a VA in MDB's address space.
60  *
61  * Returns the number of frames successfully crawled down, or
62  * CRAWL_FOUNDALL if it got to the bottom of the stack.
63  */
64 static int
65 crawl(uintptr_t frame, uintptr_t kbase, uintptr_t ktop, uintptr_t ubase,
66     int kill_fp, findstack_info_t *fsip)
67 {
68 	int levels = 0;
69 
70 	fsip->fsi_depth = 0;
71 	fsip->fsi_overflow = 0;
72 
73 	fs_dprintf(("<0> frame = %p, kbase = %p, ktop = %p, ubase = %p\n",
74 	    frame, kbase, ktop, ubase));
75 	for (;;) {
76 		uintptr_t fp;
77 		long *fpp = (long *)&((struct rwindow *)frame)->rw_fp;
78 
79 		fs_dprintf(("<1> fpp = %p, frame = %p\n", fpp, frame));
80 
81 		if ((frame & (STACK_ALIGN - 1)) != 0)
82 			break;
83 
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;
88 		else
89 			fsip->fsi_overflow = 1;
90 
91 		fs_dprintf(("<2> fp = %p\n", fp));
92 
93 		if (fp == ktop)
94 			return (CRAWL_FOUNDALL);
95 		fs_dprintf(("<3> not at base\n"));
96 
97 #if defined(__i386) || defined(__amd64)
98 		if (ktop - fp == sizeof (struct rwindow)) {
99 			fs_dprintf(("<4> found base\n"));
100 			return (CRAWL_FOUNDALL);
101 		}
102 #endif
103 
104 		fs_dprintf(("<5> fp = %p, kbase = %p, ktop - size = %p\n",
105 		    fp, kbase, ktop - sizeof (struct rwindow)));
106 
107 		if (fp < kbase || fp >= (ktop - sizeof (struct rwindow)))
108 			break;
109 
110 		frame = KTOU(fp);
111 		fs_dprintf(("<6> frame = %p\n", frame));
112 
113 		/*
114 		 * NULL out the old %fp so we don't go down this stack
115 		 * more than once.
116 		 */
117 		if (kill_fp) {
118 			fs_dprintf(("<7> fpp = %p\n", fpp));
119 			*fpp = NULL;
120 		}
121 
122 		fs_dprintf(("<8> levels = %d\n", levels));
123 		levels++;
124 	}
125 
126 	return (levels);
127 }
128 
129 /*ARGSUSED*/
130 int
131 stacks_findstack(uintptr_t addr, findstack_info_t *fsip, uint_t print_warnings)
132 {
133 	kthread_t thr;
134 	size_t stksz;
135 	uintptr_t ubase, utop;
136 	uintptr_t kbase, ktop;
137 	uintptr_t win, sp;
138 
139 	fsip->fsi_failed = 0;
140 	fsip->fsi_pc = 0;
141 	fsip->fsi_sp = 0;
142 	fsip->fsi_depth = 0;
143 	fsip->fsi_overflow = 0;
144 
145 	bzero(&thr, sizeof (thr));
146 	if (mdb_ctf_vread(&thr, "kthread_t", addr,
147 	    MDB_CTF_VREAD_IGNORE_ALL) == -1) {
148 		if (print_warnings)
149 			mdb_warn("couldn't read thread at %p\n", addr);
150 		fsip->fsi_failed = FSI_FAIL_BADTHREAD;
151 		return (DCMD_ERR);
152 	}
153 
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);
157 
158 	if ((thr.t_schedflag & TS_LOAD) == 0) {
159 		if (print_warnings)
160 			mdb_warn("thread %p isn't in memory\n", addr);
161 		fsip->fsi_failed = FSI_FAIL_NOTINMEMORY;
162 		return (DCMD_ERR);
163 	}
164 
165 	if (thr.t_stk < thr.t_stkbase) {
166 		if (print_warnings)
167 			mdb_warn(
168 			    "stack base or stack top corrupt for thread %p\n",
169 			    addr);
170 		fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
171 		return (DCMD_ERR);
172 	}
173 
174 	kbase = (uintptr_t)thr.t_stkbase;
175 	ktop = (uintptr_t)thr.t_stk;
176 	stksz = ktop - kbase;
177 
178 #ifdef __amd64
179 	/*
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
183 	 * needed.
184 	 */
185 	if ((ktop & (STACK_ALIGN - 1)) != 0)
186 		ktop -= STACK_ENTRY_ALIGN;
187 #endif
188 
189 	/*
190 	 * If the stack size is larger than a meg, assume that it's bogus.
191 	 */
192 	if (stksz > TOO_BIG_FOR_A_STACK) {
193 		if (print_warnings)
194 			mdb_warn("stack size for thread %p is too big to be "
195 			    "reasonable\n", addr);
196 		fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
197 		return (DCMD_ERR);
198 	}
199 
200 	/*
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.
209 	 */
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);
214 		if (print_warnings)
215 			mdb_warn("couldn't read entire stack for thread %p\n",
216 			    addr);
217 		fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
218 		return (DCMD_ERR);
219 	}
220 
221 	/*
222 	 * Try the saved %sp first, if it looks reasonable.
223 	 */
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;
228 #if !defined(__i386)
229 			fsip->fsi_pc = (uintptr_t)thr.t_pc;
230 #endif
231 			goto found;
232 		}
233 	}
234 
235 	/*
236 	 * Now walk through the whole stack, starting at the base,
237 	 * trying every possible "window".
238 	 */
239 	for (win = ubase;
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;
244 			goto found;
245 		}
246 	}
247 
248 	/*
249 	 * We didn't conclusively find the stack.  So we'll take another lap,
250 	 * and print out anything that looks possible.
251 	 */
252 	if (print_warnings)
253 		mdb_printf("Possible stack pointers for thread %p:\n", addr);
254 	(void) mdb_vread((caddr_t)ubase, stksz, kbase);
255 
256 	for (win = ubase;
257 	    win + sizeof (struct rwindow) <= utop;
258 	    win += sizeof (struct rwindow *)) {
259 		uintptr_t fp = ((struct rwindow *)win)->rw_fp;
260 		int levels;
261 
262 		if ((levels = crawl(win, kbase, ktop, ubase, 1, fsip)) > 1) {
263 			if (print_warnings)
264 				mdb_printf("  %p (%d)\n", fp, levels);
265 		} else if (levels == CRAWL_FOUNDALL) {
266 			/*
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.
270 			 */
271 			fsip->fsi_sp = UTOK(win) - STACK_BIAS;
272 			goto found;
273 		}
274 	}
275 
276 	fsip->fsi_depth = 0;
277 	fsip->fsi_overflow = 0;
278 	fsip->fsi_failed = FSI_FAIL_STACKNOTFOUND;
279 
280 	mdb_free((void *)ubase, stksz);
281 	return (DCMD_ERR);
282 found:
283 	mdb_free((void *)ubase, stksz);
284 	return (DCMD_OK);
285 }
286 
287 void
288 stacks_findstack_cleanup()
289 {}
290 
291 /*ARGSUSED*/
292 int
293 stacks_module_cb(uintptr_t addr, const modctl_t *mp, stacks_module_t *smp)
294 {
295 	char mod_modname[MODMAXNAMELEN + 1];
296 
297 	if (!mp->mod_modname)
298 		return (WALK_NEXT);
299 
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");
303 		return (WALK_ERR);
304 	}
305 
306 	if (strcmp(smp->sm_name, mod_modname))
307 		return (WALK_NEXT);
308 
309 	smp->sm_text = (uintptr_t)mp->mod_text;
310 	smp->sm_size = mp->mod_text_size;
311 
312 	return (WALK_DONE);
313 }
314 
315 int
316 stacks_module(stacks_module_t *smp)
317 {
318 	if (mdb_walk("modctl", (mdb_walk_cb_t)stacks_module_cb, smp) != 0) {
319 		mdb_warn("cannot walk \"modctl\"");
320 		return (-1);
321 	}
322 
323 	return (0);
324 }
325 
326 /*ARGSUSED*/
327 static void
328 print_sobj_help(int type, const char *name, const char *ops_name, void *ign)
329 {
330 	mdb_printf(" %s", name);
331 }
332 
333 /*ARGSUSED*/
334 static void
335 print_tstate_help(uint_t state, const char *name, void *ignored)
336 {
337 	mdb_printf(" %s", name);
338 }
339 
340 void
341 stacks_help(void)
342 {
343 	mdb_printf(
344 "::stacks processes all of the thread stacks on the system, grouping\n"
345 "together threads which have the same:\n"
346 "\n"
347 "  * Thread state,\n"
348 "  * Sync object type, and\n"
349 "  * PCs in their stack trace.\n"
350 "\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"
357 "\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"
360 "you can do:\n"
361 "\n"
362 "  %<b>procp%</b>::walk thread | ::stacks\n"
363 "\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"
369 "\n"
370 "  ::stacks -c fop_read | ::stacks -c cv_wait_sig_swap_core\n"
371 "\n"
372 "To get the full list of threads in each group, use the '-a' flag:\n"
373 "\n"
374 "  ::stacks -a\n"
375 "\n");
376 	mdb_dec_indent(2);
377 	mdb_printf("%<b>OPTIONS%</b>\n");
378 	mdb_inc_indent(2);
379 	mdb_printf("%s",
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"
383 "\n");
384 	mdb_dec_indent(2);
385 	mdb_printf("%<b>FILTERS%</b>\n");
386 	mdb_inc_indent(2);
387 	mdb_printf("%s",
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"
393 "  -m module\n"
394 "        Only print threads whose stacks contain functions from module.\n"
395 "  -M module\n"
396 "        Only print threads whose stacks do not contain functions from\n"
397 "        module.\n"
398 "  -s {type | ALL}\n"
399 "        Only print threads which are on a 'type' synchronization object\n"
400 "        (SOBJ).\n"
401 "  -S {type | ALL}\n"
402 "        Only print threads which are not on a 'type' SOBJ.\n"
403 "  -t tstate\n"
404 "        Only print threads which are in thread state 'tstate'.\n"
405 "  -T tstate\n"
406 "        Only print threads which are not in thread state 'tstate'.\n"
407 "\n");
408 	mdb_printf("   SOBJ types:");
409 	sobj_type_walk(print_sobj_help, NULL);
410 	mdb_printf("\n");
411 	mdb_printf("Thread states:");
412 	thread_walk_states(print_tstate_help, NULL);
413 	mdb_printf(" panic\n");
414 }
415