Print this page
6583 remove whole-process swapping
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mdb/common/modules/genunix/findstack_subr.c
+++ new/usr/src/cmd/mdb/common/modules/genunix/findstack_subr.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 25 */
26 26
27 27 #include <mdb/mdb_modapi.h>
28 28 #include <mdb/mdb_ctf.h>
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/regset.h>
32 32 #include <sys/stack.h>
33 33 #include <sys/thread.h>
34 34 #include <sys/modctl.h>
35 35
36 36 #include "findstack.h"
37 37 #include "thread.h"
38 38 #include "sobj.h"
39 39
40 40 #define TOO_BIG_FOR_A_STACK (1024 * 1024)
41 41
42 42 #define KTOU(p) ((p) - kbase + ubase)
43 43 #define UTOK(p) ((p) - ubase + kbase)
44 44
45 45 #define CRAWL_FOUNDALL (-1)
46 46
47 47 #if defined(__i386) || defined(__amd64)
48 48 struct rwindow {
49 49 uintptr_t rw_fp;
50 50 uintptr_t rw_rtn;
51 51 };
52 52 #endif
53 53
54 54 #ifndef STACK_BIAS
55 55 #define STACK_BIAS 0
56 56 #endif
57 57
58 58 /*
59 59 * Given a stack pointer, try to crawl down it to the bottom.
60 60 * "frame" is a VA in MDB's address space.
61 61 *
62 62 * Returns the number of frames successfully crawled down, or
63 63 * CRAWL_FOUNDALL if it got to the bottom of the stack.
64 64 */
65 65 static int
66 66 crawl(uintptr_t frame, uintptr_t kbase, uintptr_t ktop, uintptr_t ubase,
67 67 int kill_fp, findstack_info_t *fsip)
68 68 {
69 69 int levels = 0;
70 70
71 71 fsip->fsi_depth = 0;
72 72 fsip->fsi_overflow = 0;
73 73
74 74 fs_dprintf(("<0> frame = %p, kbase = %p, ktop = %p, ubase = %p\n",
75 75 frame, kbase, ktop, ubase));
76 76 for (;;) {
77 77 uintptr_t fp;
78 78 long *fpp = (long *)&((struct rwindow *)frame)->rw_fp;
79 79
80 80 fs_dprintf(("<1> fpp = %p, frame = %p\n", fpp, frame));
81 81
82 82 if ((frame & (STACK_ALIGN - 1)) != 0)
83 83 break;
84 84
85 85 fp = ((struct rwindow *)frame)->rw_fp + STACK_BIAS;
86 86 if (fsip->fsi_depth < fsip->fsi_max_depth)
87 87 fsip->fsi_stack[fsip->fsi_depth++] =
88 88 ((struct rwindow *)frame)->rw_rtn;
89 89 else
90 90 fsip->fsi_overflow = 1;
91 91
92 92 fs_dprintf(("<2> fp = %p\n", fp));
93 93
94 94 if (fp == ktop)
95 95 return (CRAWL_FOUNDALL);
96 96 fs_dprintf(("<3> not at base\n"));
97 97
98 98 #if defined(__i386) || defined(__amd64)
99 99 if (ktop - fp == sizeof (struct rwindow)) {
100 100 fs_dprintf(("<4> found base\n"));
101 101 return (CRAWL_FOUNDALL);
102 102 }
103 103 #endif
104 104
105 105 fs_dprintf(("<5> fp = %p, kbase = %p, ktop - size = %p\n",
106 106 fp, kbase, ktop - sizeof (struct rwindow)));
107 107
108 108 if (fp < kbase || fp >= (ktop - sizeof (struct rwindow)))
109 109 break;
110 110
111 111 frame = KTOU(fp);
112 112 fs_dprintf(("<6> frame = %p\n", frame));
113 113
114 114 /*
115 115 * NULL out the old %fp so we don't go down this stack
116 116 * more than once.
117 117 */
118 118 if (kill_fp) {
119 119 fs_dprintf(("<7> fpp = %p\n", fpp));
120 120 *fpp = NULL;
121 121 }
122 122
123 123 fs_dprintf(("<8> levels = %d\n", levels));
124 124 levels++;
125 125 }
126 126
127 127 return (levels);
128 128 }
129 129
130 130 typedef struct mdb_findstack_kthread {
131 131 struct _sobj_ops *t_sobj_ops;
132 132 uint_t t_state;
133 133 ushort_t t_flag;
134 134 ushort_t t_schedflag;
135 135 caddr_t t_stk;
136 136 caddr_t t_stkbase;
137 137 label_t t_pcb;
138 138 } mdb_findstack_kthread_t;
139 139
140 140 /*ARGSUSED*/
141 141 int
142 142 stacks_findstack(uintptr_t addr, findstack_info_t *fsip, uint_t print_warnings)
143 143 {
144 144 mdb_findstack_kthread_t thr;
145 145 size_t stksz;
146 146 uintptr_t ubase, utop;
147 147 uintptr_t kbase, ktop;
148 148 uintptr_t win, sp;
149 149
150 150 fsip->fsi_failed = 0;
151 151 fsip->fsi_pc = 0;
152 152 fsip->fsi_sp = 0;
153 153 fsip->fsi_depth = 0;
154 154 fsip->fsi_overflow = 0;
155 155
↓ open down ↓ |
155 lines elided |
↑ open up ↑ |
156 156 if (mdb_ctf_vread(&thr, "kthread_t", "mdb_findstack_kthread_t",
157 157 addr, print_warnings ? 0 : MDB_CTF_VREAD_QUIET) == -1) {
158 158 fsip->fsi_failed = FSI_FAIL_BADTHREAD;
159 159 return (DCMD_ERR);
160 160 }
161 161
162 162 fsip->fsi_sobj_ops = (uintptr_t)thr.t_sobj_ops;
163 163 fsip->fsi_tstate = thr.t_state;
164 164 fsip->fsi_panic = !!(thr.t_flag & T_PANIC);
165 165
166 - if ((thr.t_schedflag & TS_LOAD) == 0) {
167 - if (print_warnings)
168 - mdb_warn("thread %p isn't in memory\n", addr);
169 - fsip->fsi_failed = FSI_FAIL_NOTINMEMORY;
170 - return (DCMD_ERR);
171 - }
172 -
173 166 if (thr.t_stk < thr.t_stkbase) {
174 167 if (print_warnings)
175 168 mdb_warn(
176 169 "stack base or stack top corrupt for thread %p\n",
177 170 addr);
178 171 fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
179 172 return (DCMD_ERR);
180 173 }
181 174
182 175 kbase = (uintptr_t)thr.t_stkbase;
183 176 ktop = (uintptr_t)thr.t_stk;
184 177 stksz = ktop - kbase;
185 178
186 179 #ifdef __amd64
187 180 /*
188 181 * The stack on amd64 is intentionally misaligned, so ignore the top
189 182 * half-frame. See thread_stk_init(). When handling traps, the frame
190 183 * is automatically aligned by the hardware, so we only alter ktop if
191 184 * needed.
192 185 */
193 186 if ((ktop & (STACK_ALIGN - 1)) != 0)
194 187 ktop -= STACK_ENTRY_ALIGN;
195 188 #endif
196 189
197 190 /*
198 191 * If the stack size is larger than a meg, assume that it's bogus.
199 192 */
200 193 if (stksz > TOO_BIG_FOR_A_STACK) {
201 194 if (print_warnings)
202 195 mdb_warn("stack size for thread %p is too big to be "
203 196 "reasonable\n", addr);
204 197 fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
205 198 return (DCMD_ERR);
206 199 }
207 200
208 201 /*
209 202 * This could be (and was) a UM_GC allocation. Unfortunately,
210 203 * stksz tends to be very large. As currently implemented, dcmds
211 204 * invoked as part of pipelines don't have their UM_GC-allocated
212 205 * memory freed until the pipeline completes. With stksz in the
213 206 * neighborhood of 20k, the popular ::walk thread |::findstack
214 207 * pipeline can easily run memory-constrained debuggers (kmdb) out
215 208 * of memory. This can be changed back to a gc-able allocation when
216 209 * the debugger is changed to free UM_GC memory more promptly.
217 210 */
218 211 ubase = (uintptr_t)mdb_alloc(stksz, UM_SLEEP);
219 212 utop = ubase + stksz;
220 213 if (mdb_vread((caddr_t)ubase, stksz, kbase) != stksz) {
221 214 mdb_free((void *)ubase, stksz);
222 215 if (print_warnings)
223 216 mdb_warn("couldn't read entire stack for thread %p\n",
224 217 addr);
225 218 fsip->fsi_failed = FSI_FAIL_THREADCORRUPT;
226 219 return (DCMD_ERR);
227 220 }
228 221
229 222 /*
230 223 * Try the saved %sp first, if it looks reasonable.
231 224 */
232 225 sp = KTOU((uintptr_t)thr.t_sp + STACK_BIAS);
233 226 if (sp >= ubase && sp <= utop) {
234 227 if (crawl(sp, kbase, ktop, ubase, 0, fsip) == CRAWL_FOUNDALL) {
235 228 fsip->fsi_sp = (uintptr_t)thr.t_sp;
236 229 #if !defined(__i386)
237 230 fsip->fsi_pc = (uintptr_t)thr.t_pc;
238 231 #endif
239 232 goto found;
240 233 }
241 234 }
242 235
243 236 /*
244 237 * Now walk through the whole stack, starting at the base,
245 238 * trying every possible "window".
246 239 */
247 240 for (win = ubase;
248 241 win + sizeof (struct rwindow) <= utop;
249 242 win += sizeof (struct rwindow *)) {
250 243 if (crawl(win, kbase, ktop, ubase, 1, fsip) == CRAWL_FOUNDALL) {
251 244 fsip->fsi_sp = UTOK(win) - STACK_BIAS;
252 245 goto found;
253 246 }
254 247 }
255 248
256 249 /*
257 250 * We didn't conclusively find the stack. So we'll take another lap,
258 251 * and print out anything that looks possible.
259 252 */
260 253 if (print_warnings)
261 254 mdb_printf("Possible stack pointers for thread %p:\n", addr);
262 255 (void) mdb_vread((caddr_t)ubase, stksz, kbase);
263 256
264 257 for (win = ubase;
265 258 win + sizeof (struct rwindow) <= utop;
266 259 win += sizeof (struct rwindow *)) {
267 260 uintptr_t fp = ((struct rwindow *)win)->rw_fp;
268 261 int levels;
269 262
270 263 if ((levels = crawl(win, kbase, ktop, ubase, 1, fsip)) > 1) {
271 264 if (print_warnings)
272 265 mdb_printf(" %p (%d)\n", fp, levels);
273 266 } else if (levels == CRAWL_FOUNDALL) {
274 267 /*
275 268 * If this is a live system, the stack could change
276 269 * between the two mdb_vread(ubase, utop, kbase)'s,
277 270 * and we could have a fully valid stack here.
278 271 */
279 272 fsip->fsi_sp = UTOK(win) - STACK_BIAS;
280 273 goto found;
281 274 }
282 275 }
283 276
284 277 fsip->fsi_depth = 0;
285 278 fsip->fsi_overflow = 0;
286 279 fsip->fsi_failed = FSI_FAIL_STACKNOTFOUND;
287 280
288 281 mdb_free((void *)ubase, stksz);
289 282 return (DCMD_ERR);
290 283 found:
291 284 mdb_free((void *)ubase, stksz);
292 285 return (DCMD_OK);
293 286 }
294 287
295 288 void
296 289 stacks_findstack_cleanup()
297 290 {}
298 291
299 292 /*ARGSUSED*/
300 293 int
301 294 stacks_module_cb(uintptr_t addr, const modctl_t *mp, stacks_module_t *smp)
302 295 {
303 296 char mod_modname[MODMAXNAMELEN + 1];
304 297
305 298 if (!mp->mod_modname)
306 299 return (WALK_NEXT);
307 300
308 301 if (mdb_readstr(mod_modname, sizeof (mod_modname),
309 302 (uintptr_t)mp->mod_modname) == -1) {
310 303 mdb_warn("failed to read mod_modname in \"modctl\" walk");
311 304 return (WALK_ERR);
312 305 }
313 306
314 307 if (strcmp(smp->sm_name, mod_modname))
315 308 return (WALK_NEXT);
316 309
317 310 smp->sm_text = (uintptr_t)mp->mod_text;
318 311 smp->sm_size = mp->mod_text_size;
319 312
320 313 return (WALK_DONE);
321 314 }
322 315
323 316 int
324 317 stacks_module(stacks_module_t *smp)
325 318 {
326 319 if (mdb_walk("modctl", (mdb_walk_cb_t)stacks_module_cb, smp) != 0) {
327 320 mdb_warn("cannot walk \"modctl\"");
328 321 return (-1);
329 322 }
330 323
331 324 return (0);
332 325 }
333 326
334 327 /*ARGSUSED*/
335 328 static void
336 329 print_sobj_help(int type, const char *name, const char *ops_name, void *ign)
337 330 {
338 331 mdb_printf(" %s", name);
339 332 }
340 333
341 334 /*ARGSUSED*/
342 335 static void
343 336 print_tstate_help(uint_t state, const char *name, void *ignored)
344 337 {
345 338 mdb_printf(" %s", name);
346 339 }
347 340
348 341 void
349 342 stacks_help(void)
350 343 {
351 344 mdb_printf(
352 345 "::stacks processes all of the thread stacks on the system, grouping\n"
353 346 "together threads which have the same:\n"
354 347 "\n"
355 348 " * Thread state,\n"
356 349 " * Sync object type, and\n"
357 350 " * PCs in their stack trace.\n"
358 351 "\n"
359 352 "The default output (no address or options) is just a dump of the thread\n"
360 353 "groups in the system. For a view of active threads, use \"::stacks -i\",\n"
361 354 "which filters out FREE threads (interrupt threads which are currently\n"
362 355 "inactive) and threads sleeping on a CV. (Note that those threads may still\n"
363 356 "be noteworthy; this is just for a first glance.) More general filtering\n"
364 357 "options are described below, in the \"FILTERS\" section.\n"
365 358 "\n"
366 359 "::stacks can be used in a pipeline. The input to ::stacks is one or more\n"
367 360 "thread pointers. For example, to get a summary of threads in a process,\n"
368 361 "you can do:\n"
369 362 "\n"
370 363 " %<b>procp%</b>::walk thread | ::stacks\n"
371 364 "\n"
372 365 "When output into a pipe, ::stacks prints all of the threads input,\n"
373 366 "filtered by the given filtering options. This means that multiple\n"
374 367 "::stacks invocations can be piped together to achieve more complicated\n"
375 368 "filters. For example, to get threads which have both 'fop_read' and\n"
376 369 "'cv_wait_sig_swap' in their stack trace, you could do:\n"
377 370 "\n"
378 371 " ::stacks -c fop_read | ::stacks -c cv_wait_sig_swap_core\n"
379 372 "\n"
380 373 "To get the full list of threads in each group, use the '-a' flag:\n"
381 374 "\n"
382 375 " ::stacks -a\n"
383 376 "\n");
384 377 mdb_dec_indent(2);
385 378 mdb_printf("%<b>OPTIONS%</b>\n");
386 379 mdb_inc_indent(2);
387 380 mdb_printf("%s",
388 381 " -a Print all of the grouped threads, instead of just a count.\n"
389 382 " -f Force a re-run of the thread stack gathering.\n"
390 383 " -v Be verbose about thread stack gathering.\n"
391 384 "\n");
392 385 mdb_dec_indent(2);
393 386 mdb_printf("%<b>FILTERS%</b>\n");
394 387 mdb_inc_indent(2);
395 388 mdb_printf("%s",
396 389 " -i Show active threads; equivalent to '-S CV -T FREE'.\n"
397 390 " -c func[+offset]\n"
398 391 " Only print threads whose stacks contain func/func+offset.\n"
399 392 " -C func[+offset]\n"
400 393 " Only print threads whose stacks do not contain func/func+offset.\n"
401 394 " -m module\n"
402 395 " Only print threads whose stacks contain functions from module.\n"
403 396 " -M module\n"
404 397 " Only print threads whose stacks do not contain functions from\n"
405 398 " module.\n"
406 399 " -s {type | ALL}\n"
407 400 " Only print threads which are on a 'type' synchronization object\n"
408 401 " (SOBJ).\n"
409 402 " -S {type | ALL}\n"
410 403 " Only print threads which are not on a 'type' SOBJ.\n"
411 404 " -t tstate\n"
412 405 " Only print threads which are in thread state 'tstate'.\n"
413 406 " -T tstate\n"
414 407 " Only print threads which are not in thread state 'tstate'.\n"
415 408 "\n");
416 409 mdb_printf(" SOBJ types:");
417 410 sobj_type_walk(print_sobj_help, NULL);
418 411 mdb_printf("\n");
419 412 mdb_printf("Thread states:");
420 413 thread_walk_states(print_tstate_help, NULL);
421 414 mdb_printf(" panic\n");
422 415 }
↓ open down ↓ |
240 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX