Print this page
LINTED a bad cast align warning that seems to be oddly omnios specific.
I think the alignment of any pointer we receive from mdb_zalloc must be
sufficiently aligned for 'long' on both x86 platforms.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mdb/i86pc/modules/unix/unix.c
+++ new/usr/src/cmd/mdb/i86pc/modules/unix/unix.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 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24
25 25 /*
26 26 * Copyright (c) 2012 Joyent, Inc. All rights reserved.
27 27 */
28 28
29 29 #include <mdb/mdb_modapi.h>
30 30 #include <mdb/mdb_ctf.h>
31 31 #include <sys/cpuvar.h>
32 32 #include <sys/systm.h>
33 33 #include <sys/traptrace.h>
34 34 #include <sys/x_call.h>
35 35 #include <sys/xc_levels.h>
36 36 #include <sys/avintr.h>
37 37 #include <sys/systm.h>
38 38 #include <sys/trap.h>
39 39 #include <sys/mutex.h>
40 40 #include <sys/mutex_impl.h>
41 41 #include "i86mmu.h"
42 42 #include <sys/apix.h>
43 43 #include <sys/x86_archext.h>
44 44 #include <sys/bitmap.h>
45 45
46 46 #define TT_HDLR_WIDTH 17
47 47
48 48
49 49 /* apix only */
50 50 static apix_impl_t *d_apixs[NCPU];
51 51 static int use_apix = 0;
52 52
53 53 static int
54 54 ttrace_ttr_size_check(void)
55 55 {
56 56 mdb_ctf_id_t ttrtid;
57 57 ssize_t ttr_size;
58 58
59 59 if (mdb_ctf_lookup_by_name("trap_trace_rec_t", &ttrtid) != 0 ||
60 60 mdb_ctf_type_resolve(ttrtid, &ttrtid) != 0) {
61 61 mdb_warn("failed to determine size of trap_trace_rec_t; "
62 62 "non-TRAPTRACE kernel?\n");
63 63 return (0);
64 64 }
65 65
66 66 if ((ttr_size = mdb_ctf_type_size(ttrtid)) !=
67 67 sizeof (trap_trace_rec_t)) {
68 68 /*
69 69 * On Intel machines, this will happen when TTR_STACK_DEPTH
70 70 * is changed. This code could be smarter, and could
71 71 * dynamically adapt to different depths, but not until a
72 72 * need for such adaptation is demonstrated.
73 73 */
74 74 mdb_warn("size of trap_trace_rec_t (%d bytes) doesn't "
75 75 "match expected %d\n", ttr_size, sizeof (trap_trace_rec_t));
76 76 return (0);
77 77 }
78 78
79 79 return (1);
80 80 }
81 81
82 82 int
83 83 ttrace_walk_init(mdb_walk_state_t *wsp)
84 84 {
85 85 trap_trace_ctl_t *ttcp;
86 86 size_t ttc_size = sizeof (trap_trace_ctl_t) * NCPU;
87 87 int i;
88 88
89 89 if (!ttrace_ttr_size_check())
90 90 return (WALK_ERR);
91 91
92 92 ttcp = mdb_zalloc(ttc_size, UM_SLEEP);
93 93
94 94 if (wsp->walk_addr != NULL) {
95 95 mdb_warn("ttrace only supports global walks\n");
96 96 return (WALK_ERR);
97 97 }
98 98
99 99 if (mdb_readsym(ttcp, ttc_size, "trap_trace_ctl") == -1) {
100 100 mdb_warn("symbol 'trap_trace_ctl' not found; "
101 101 "non-TRAPTRACE kernel?\n");
102 102 mdb_free(ttcp, ttc_size);
103 103 return (WALK_ERR);
104 104 }
105 105
106 106 /*
107 107 * We'll poach the ttc_current pointer (which isn't used for
108 108 * anything) to store a pointer to our current TRAPTRACE record.
109 109 * This allows us to only keep the array of trap_trace_ctl structures
110 110 * as our walker state (ttc_current may be the only kernel data
111 111 * structure member added exclusively to make writing the mdb walker
112 112 * a little easier).
113 113 */
114 114 for (i = 0; i < NCPU; i++) {
115 115 trap_trace_ctl_t *ttc = &ttcp[i];
116 116
117 117 if (ttc->ttc_first == NULL)
118 118 continue;
119 119
120 120 /*
121 121 * Assign ttc_current to be the last completed record.
122 122 * Note that the error checking (i.e. in the ttc_next ==
123 123 * ttc_first case) is performed in the step function.
124 124 */
125 125 ttc->ttc_current = ttc->ttc_next - sizeof (trap_trace_rec_t);
126 126 }
127 127
128 128 wsp->walk_data = ttcp;
129 129 return (WALK_NEXT);
130 130 }
131 131
132 132 int
133 133 ttrace_walk_step(mdb_walk_state_t *wsp)
134 134 {
135 135 trap_trace_ctl_t *ttcp = wsp->walk_data, *ttc, *latest_ttc;
136 136 trap_trace_rec_t rec;
137 137 int rval, i, recsize = sizeof (trap_trace_rec_t);
138 138 hrtime_t latest = 0;
139 139
140 140 /*
141 141 * Loop through the CPUs, looking for the latest trap trace record
142 142 * (we want to walk through the trap trace records in reverse
143 143 * chronological order).
144 144 */
145 145 for (i = 0; i < NCPU; i++) {
146 146 ttc = &ttcp[i];
147 147
148 148 if (ttc->ttc_current == NULL)
149 149 continue;
150 150
151 151 if (ttc->ttc_current < ttc->ttc_first)
152 152 ttc->ttc_current = ttc->ttc_limit - recsize;
153 153
154 154 if (mdb_vread(&rec, sizeof (rec), ttc->ttc_current) == -1) {
155 155 mdb_warn("couldn't read rec at %p", ttc->ttc_current);
156 156 return (WALK_ERR);
157 157 }
158 158
159 159 if (rec.ttr_stamp > latest) {
160 160 latest = rec.ttr_stamp;
161 161 latest_ttc = ttc;
162 162 }
163 163 }
164 164
165 165 if (latest == 0)
166 166 return (WALK_DONE);
167 167
168 168 ttc = latest_ttc;
169 169
170 170 if (mdb_vread(&rec, sizeof (rec), ttc->ttc_current) == -1) {
171 171 mdb_warn("couldn't read rec at %p", ttc->ttc_current);
172 172 return (WALK_ERR);
173 173 }
174 174
175 175 rval = wsp->walk_callback(ttc->ttc_current, &rec, wsp->walk_cbdata);
176 176
177 177 if (ttc->ttc_current == ttc->ttc_next)
178 178 ttc->ttc_current = NULL;
179 179 else
180 180 ttc->ttc_current -= sizeof (trap_trace_rec_t);
181 181
182 182 return (rval);
183 183 }
184 184
185 185 void
186 186 ttrace_walk_fini(mdb_walk_state_t *wsp)
187 187 {
188 188 mdb_free(wsp->walk_data, sizeof (trap_trace_ctl_t) * NCPU);
189 189 }
190 190
191 191 static int
192 192 ttrace_syscall(trap_trace_rec_t *rec)
193 193 {
194 194 GElf_Sym sym;
195 195 int sysnum = rec->ttr_sysnum;
196 196 uintptr_t addr;
197 197 struct sysent sys;
198 198
199 199 mdb_printf("%-3x", sysnum);
200 200
201 201 if (rec->ttr_sysnum > NSYSCALL) {
202 202 mdb_printf(" %-*d", TT_HDLR_WIDTH, rec->ttr_sysnum);
203 203 return (0);
204 204 }
205 205
206 206 if (mdb_lookup_by_name("sysent", &sym) == -1) {
207 207 mdb_warn("\ncouldn't find 'sysent'");
208 208 return (-1);
209 209 }
210 210
211 211 addr = (uintptr_t)sym.st_value + sysnum * sizeof (struct sysent);
212 212
213 213 if (addr >= (uintptr_t)sym.st_value + sym.st_size) {
214 214 mdb_warn("\nsysnum %d out-of-range\n", sysnum);
215 215 return (-1);
216 216 }
217 217
218 218 if (mdb_vread(&sys, sizeof (sys), addr) == -1) {
219 219 mdb_warn("\nfailed to read sysent at %p", addr);
220 220 return (-1);
221 221 }
222 222
223 223 mdb_printf(" %-*a", TT_HDLR_WIDTH, sys.sy_callc);
224 224
225 225 return (0);
226 226 }
227 227
228 228 static int
229 229 ttrace_interrupt(trap_trace_rec_t *rec)
230 230 {
231 231 GElf_Sym sym;
232 232 uintptr_t addr;
233 233 struct av_head hd;
234 234 struct autovec av;
235 235
236 236 switch (rec->ttr_regs.r_trapno) {
237 237 case T_SOFTINT:
238 238 mdb_printf("%-3s %-*s", "-", TT_HDLR_WIDTH, "(fakesoftint)");
239 239 return (0);
240 240 default:
241 241 break;
242 242 }
243 243
244 244 mdb_printf("%-3x ", rec->ttr_vector);
245 245
246 246 if (mdb_lookup_by_name("autovect", &sym) == -1) {
247 247 mdb_warn("\ncouldn't find 'autovect'");
248 248 return (-1);
249 249 }
250 250
251 251 addr = (uintptr_t)sym.st_value +
252 252 rec->ttr_vector * sizeof (struct av_head);
253 253
254 254 if (addr >= (uintptr_t)sym.st_value + sym.st_size) {
255 255 mdb_warn("\nav_head for vec %x is corrupt\n", rec->ttr_vector);
256 256 return (-1);
257 257 }
258 258
259 259 if (mdb_vread(&hd, sizeof (hd), addr) == -1) {
260 260 mdb_warn("\ncouldn't read av_head for vec %x", rec->ttr_vector);
261 261 return (-1);
262 262 }
263 263
264 264 if (hd.avh_link == NULL) {
265 265 if (rec->ttr_ipl == XC_CPUPOKE_PIL)
266 266 mdb_printf("%-*s", TT_HDLR_WIDTH, "(cpupoke)");
267 267 else
268 268 mdb_printf("%-*s", TT_HDLR_WIDTH, "(spurious)");
269 269 } else {
270 270 if (mdb_vread(&av, sizeof (av), (uintptr_t)hd.avh_link) == -1) {
271 271 mdb_warn("couldn't read autovec at %p",
272 272 (uintptr_t)hd.avh_link);
273 273 }
274 274
275 275 mdb_printf("%-*a", TT_HDLR_WIDTH, av.av_vector);
276 276 }
277 277
278 278 return (0);
279 279 }
280 280
281 281 static int
282 282 ttrace_apix_interrupt(trap_trace_rec_t *rec)
283 283 {
284 284 struct autovec av;
285 285 apix_impl_t apix;
286 286 apix_vector_t apix_vector;
287 287
288 288 switch (rec->ttr_regs.r_trapno) {
289 289 case T_SOFTINT:
290 290 mdb_printf("%-3s %-*s", "-", TT_HDLR_WIDTH, "(fakesoftint)");
291 291 return (0);
292 292 default:
293 293 break;
294 294 }
295 295
296 296 mdb_printf("%-3x ", rec->ttr_vector);
297 297
298 298 /* Read the per CPU apix entry */
299 299 if (mdb_vread(&apix, sizeof (apix_impl_t),
300 300 (uintptr_t)d_apixs[rec->ttr_cpuid]) == -1) {
301 301 mdb_warn("\ncouldn't read apix[%d]", rec->ttr_cpuid);
302 302 return (-1);
303 303 }
304 304 if (mdb_vread(&apix_vector, sizeof (apix_vector_t),
305 305 (uintptr_t)apix.x_vectbl[rec->ttr_vector]) == -1) {
306 306 mdb_warn("\ncouldn't read apix_vector_t[%d]", rec->ttr_vector);
307 307 return (-1);
308 308 }
309 309 if (apix_vector.v_share == 0) {
310 310 if (rec->ttr_ipl == XC_CPUPOKE_PIL)
311 311 mdb_printf("%-*s", TT_HDLR_WIDTH, "(cpupoke)");
312 312 else
313 313 mdb_printf("%-*s", TT_HDLR_WIDTH, "(spurious)");
314 314 } else {
315 315 if (mdb_vread(&av, sizeof (struct autovec),
316 316 (uintptr_t)(apix_vector.v_autovect)) == -1) {
317 317 mdb_warn("couldn't read autovec at %p",
318 318 (uintptr_t)apix_vector.v_autovect);
319 319 }
320 320
321 321 mdb_printf("%-*a", TT_HDLR_WIDTH, av.av_vector);
322 322 }
323 323
324 324 return (0);
325 325 }
326 326
327 327
328 328 static struct {
329 329 int tt_trapno;
330 330 char *tt_name;
331 331 } ttrace_traps[] = {
332 332 { T_ZERODIV, "divide-error" },
333 333 { T_SGLSTP, "debug-exception" },
334 334 { T_NMIFLT, "nmi-interrupt" },
335 335 { T_BPTFLT, "breakpoint" },
336 336 { T_OVFLW, "into-overflow" },
337 337 { T_BOUNDFLT, "bound-exceeded" },
338 338 { T_ILLINST, "invalid-opcode" },
339 339 { T_NOEXTFLT, "device-not-avail" },
340 340 { T_DBLFLT, "double-fault" },
341 341 { T_EXTOVRFLT, "segment-overrun" },
342 342 { T_TSSFLT, "invalid-tss" },
343 343 { T_SEGFLT, "segment-not-pres" },
344 344 { T_STKFLT, "stack-fault" },
345 345 { T_GPFLT, "general-protectn" },
346 346 { T_PGFLT, "page-fault" },
347 347 { T_EXTERRFLT, "error-fault" },
348 348 { T_ALIGNMENT, "alignment-check" },
349 349 { T_MCE, "machine-check" },
350 350 { T_SIMDFPE, "sse-exception" },
351 351
352 352 { T_DBGENTR, "debug-enter" },
353 353 { T_FASTTRAP, "fasttrap-0xd2" },
354 354 { T_SYSCALLINT, "syscall-0x91" },
355 355 { T_DTRACE_RET, "dtrace-ret" },
356 356 { T_SOFTINT, "softint" },
357 357 { T_INTERRUPT, "interrupt" },
358 358 { T_FAULT, "fault" },
359 359 { T_AST, "ast" },
360 360 { T_SYSCALL, "syscall" },
361 361
362 362 { 0, NULL }
363 363 };
364 364
365 365 static int
366 366 ttrace_trap(trap_trace_rec_t *rec)
367 367 {
368 368 int i;
369 369
370 370 if (rec->ttr_regs.r_trapno == T_AST)
371 371 mdb_printf("%-3s ", "-");
372 372 else
373 373 mdb_printf("%-3x ", rec->ttr_regs.r_trapno);
374 374
375 375 for (i = 0; ttrace_traps[i].tt_name != NULL; i++) {
376 376 if (rec->ttr_regs.r_trapno == ttrace_traps[i].tt_trapno)
377 377 break;
378 378 }
379 379
380 380 if (ttrace_traps[i].tt_name == NULL)
381 381 mdb_printf("%-*s", TT_HDLR_WIDTH, "(unknown)");
382 382 else
383 383 mdb_printf("%-*s", TT_HDLR_WIDTH, ttrace_traps[i].tt_name);
384 384
385 385 return (0);
386 386 }
387 387
388 388 static void
389 389 ttrace_intr_detail(trap_trace_rec_t *rec)
390 390 {
391 391 mdb_printf("\tirq %x ipl %d oldpri %d basepri %d\n", rec->ttr_vector,
392 392 rec->ttr_ipl, rec->ttr_pri, rec->ttr_spl);
393 393 }
394 394
395 395 static struct {
396 396 uchar_t t_marker;
397 397 char *t_name;
398 398 int (*t_hdlr)(trap_trace_rec_t *);
399 399 } ttrace_hdlr[] = {
400 400 { TT_SYSCALL, "sysc", ttrace_syscall },
401 401 { TT_SYSENTER, "syse", ttrace_syscall },
402 402 { TT_SYSC, "asys", ttrace_syscall },
403 403 { TT_SYSC64, "sc64", ttrace_syscall },
404 404 { TT_INTERRUPT, "intr", ttrace_interrupt },
405 405 { TT_TRAP, "trap", ttrace_trap },
406 406 { TT_EVENT, "evnt", ttrace_trap },
407 407 { 0, NULL, NULL }
408 408 };
409 409
410 410 typedef struct ttrace_dcmd {
411 411 processorid_t ttd_cpu;
412 412 uint_t ttd_extended;
413 413 trap_trace_ctl_t ttd_ttc[NCPU];
414 414 } ttrace_dcmd_t;
415 415
416 416 #if defined(__amd64)
417 417
418 418 #define DUMP(reg) #reg, regs->r_##reg
419 419 #define THREEREGS " %3s: %16lx %3s: %16lx %3s: %16lx\n"
420 420
421 421 static void
422 422 ttrace_dumpregs(trap_trace_rec_t *rec)
423 423 {
424 424 struct regs *regs = &rec->ttr_regs;
425 425
426 426 mdb_printf(THREEREGS, DUMP(rdi), DUMP(rsi), DUMP(rdx));
427 427 mdb_printf(THREEREGS, DUMP(rcx), DUMP(r8), DUMP(r9));
428 428 mdb_printf(THREEREGS, DUMP(rax), DUMP(rbx), DUMP(rbp));
429 429 mdb_printf(THREEREGS, DUMP(r10), DUMP(r11), DUMP(r12));
430 430 mdb_printf(THREEREGS, DUMP(r13), DUMP(r14), DUMP(r15));
431 431 mdb_printf(THREEREGS, DUMP(ds), DUMP(es), DUMP(fs));
432 432 mdb_printf(THREEREGS, DUMP(gs), "trp", regs->r_trapno, DUMP(err));
433 433 mdb_printf(THREEREGS, DUMP(rip), DUMP(cs), DUMP(rfl));
434 434 mdb_printf(THREEREGS, DUMP(rsp), DUMP(ss), "cr2", rec->ttr_cr2);
435 435 mdb_printf("\n");
436 436 }
437 437
438 438 #else
439 439
440 440 #define DUMP(reg) #reg, regs->r_##reg
441 441 #define FOURREGS " %3s: %08x %3s: %08x %3s: %08x %3s: %08x\n"
442 442
443 443 static void
444 444 ttrace_dumpregs(trap_trace_rec_t *rec)
445 445 {
446 446 struct regs *regs = &rec->ttr_regs;
447 447
448 448 mdb_printf(FOURREGS, DUMP(gs), DUMP(fs), DUMP(es), DUMP(ds));
449 449 mdb_printf(FOURREGS, DUMP(edi), DUMP(esi), DUMP(ebp), DUMP(esp));
450 450 mdb_printf(FOURREGS, DUMP(ebx), DUMP(edx), DUMP(ecx), DUMP(eax));
451 451 mdb_printf(FOURREGS, "trp", regs->r_trapno, DUMP(err),
452 452 DUMP(pc), DUMP(cs));
453 453 mdb_printf(FOURREGS, DUMP(efl), "usp", regs->r_uesp, DUMP(ss),
454 454 "cr2", rec->ttr_cr2);
455 455 mdb_printf("\n");
456 456 }
457 457
458 458 #endif /* __amd64 */
459 459
460 460 int
461 461 ttrace_walk(uintptr_t addr, trap_trace_rec_t *rec, ttrace_dcmd_t *dcmd)
462 462 {
463 463 struct regs *regs = &rec->ttr_regs;
464 464 processorid_t cpu = -1, i;
465 465
466 466 for (i = 0; i < NCPU; i++) {
467 467 if (addr >= dcmd->ttd_ttc[i].ttc_first &&
468 468 addr < dcmd->ttd_ttc[i].ttc_limit) {
469 469 cpu = i;
470 470 break;
471 471 }
472 472 }
473 473
474 474 if (cpu == -1) {
475 475 mdb_warn("couldn't find %p in any trap trace ctl\n", addr);
476 476 return (WALK_ERR);
477 477 }
478 478
479 479 if (dcmd->ttd_cpu != -1 && cpu != dcmd->ttd_cpu)
480 480 return (WALK_NEXT);
481 481
482 482 mdb_printf("%3d %15llx ", cpu, rec->ttr_stamp);
483 483
484 484 for (i = 0; ttrace_hdlr[i].t_hdlr != NULL; i++) {
485 485 if (rec->ttr_marker != ttrace_hdlr[i].t_marker)
486 486 continue;
487 487 mdb_printf("%4s ", ttrace_hdlr[i].t_name);
488 488 if (ttrace_hdlr[i].t_hdlr(rec) == -1)
489 489 return (WALK_ERR);
490 490 }
491 491
492 492 mdb_printf(" %a\n", regs->r_pc);
493 493
494 494 if (dcmd->ttd_extended == FALSE)
495 495 return (WALK_NEXT);
496 496
497 497 if (rec->ttr_marker == TT_INTERRUPT)
498 498 ttrace_intr_detail(rec);
499 499 else
500 500 ttrace_dumpregs(rec);
501 501
502 502 if (rec->ttr_sdepth > 0) {
503 503 for (i = 0; i < rec->ttr_sdepth; i++) {
504 504 if (i >= TTR_STACK_DEPTH) {
505 505 mdb_printf("%17s*** invalid ttr_sdepth (is %d, "
506 506 "should be <= %d)\n", " ", rec->ttr_sdepth,
507 507 TTR_STACK_DEPTH);
508 508 break;
509 509 }
510 510
511 511 mdb_printf("%17s %a()\n", " ", rec->ttr_stack[i]);
512 512 }
513 513 mdb_printf("\n");
514 514 }
515 515
516 516 return (WALK_NEXT);
517 517 }
518 518
519 519 int
520 520 ttrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
521 521 {
522 522 ttrace_dcmd_t dcmd;
523 523 trap_trace_ctl_t *ttc = dcmd.ttd_ttc;
524 524 trap_trace_rec_t rec;
525 525 size_t ttc_size = sizeof (trap_trace_ctl_t) * NCPU;
526 526
527 527 if (!ttrace_ttr_size_check())
528 528 return (WALK_ERR);
529 529
530 530 bzero(&dcmd, sizeof (dcmd));
531 531 dcmd.ttd_cpu = -1;
532 532 dcmd.ttd_extended = FALSE;
533 533
534 534 if (mdb_readsym(ttc, ttc_size, "trap_trace_ctl") == -1) {
535 535 mdb_warn("symbol 'trap_trace_ctl' not found; "
536 536 "non-TRAPTRACE kernel?\n");
537 537 return (DCMD_ERR);
538 538 }
539 539
540 540 if (mdb_getopts(argc, argv,
541 541 'x', MDB_OPT_SETBITS, TRUE, &dcmd.ttd_extended, NULL) != argc)
542 542 return (DCMD_USAGE);
543 543
544 544 if (DCMD_HDRSPEC(flags)) {
545 545 mdb_printf("%3s %15s %4s %2s %-*s%s\n", "CPU",
546 546 "TIMESTAMP", "TYPE", "Vec", TT_HDLR_WIDTH, "HANDLER",
547 547 " EIP");
548 548 }
549 549
550 550 if (flags & DCMD_ADDRSPEC) {
551 551 if (addr >= NCPU) {
552 552 if (mdb_vread(&rec, sizeof (rec), addr) == -1) {
553 553 mdb_warn("couldn't read trap trace record "
554 554 "at %p", addr);
555 555 return (DCMD_ERR);
556 556 }
557 557
558 558 if (ttrace_walk(addr, &rec, &dcmd) == WALK_ERR)
559 559 return (DCMD_ERR);
560 560
561 561 return (DCMD_OK);
562 562 }
563 563 dcmd.ttd_cpu = addr;
564 564 }
565 565
566 566 if (mdb_readvar(&use_apix, "apix_enable") == -1) {
567 567 mdb_warn("failed to read apix_enable");
568 568 use_apix = 0;
569 569 }
570 570
571 571 if (use_apix) {
572 572 if (mdb_readvar(&d_apixs, "apixs") == -1) {
573 573 mdb_warn("\nfailed to read apixs.");
574 574 return (DCMD_ERR);
575 575 }
576 576 /* change to apix ttrace interrupt handler */
577 577 ttrace_hdlr[4].t_hdlr = ttrace_apix_interrupt;
578 578 }
579 579
580 580 if (mdb_walk("ttrace", (mdb_walk_cb_t)ttrace_walk, &dcmd) == -1) {
581 581 mdb_warn("couldn't walk 'ttrace'");
582 582 return (DCMD_ERR);
583 583 }
584 584
585 585 return (DCMD_OK);
586 586 }
587 587
588 588 /*ARGSUSED*/
589 589 int
590 590 mutex_owner_init(mdb_walk_state_t *wsp)
591 591 {
592 592 return (WALK_NEXT);
593 593 }
594 594
595 595 int
596 596 mutex_owner_step(mdb_walk_state_t *wsp)
597 597 {
598 598 uintptr_t addr = wsp->walk_addr;
599 599 mutex_impl_t mtx;
600 600 uintptr_t owner;
601 601 kthread_t thr;
602 602
603 603 if (mdb_vread(&mtx, sizeof (mtx), addr) == -1)
604 604 return (WALK_ERR);
605 605
606 606 if (!MUTEX_TYPE_ADAPTIVE(&mtx))
607 607 return (WALK_DONE);
608 608
609 609 if ((owner = (uintptr_t)MUTEX_OWNER(&mtx)) == NULL)
610 610 return (WALK_DONE);
611 611
612 612 if (mdb_vread(&thr, sizeof (thr), owner) != -1)
613 613 (void) wsp->walk_callback(owner, &thr, wsp->walk_cbdata);
614 614
615 615 return (WALK_DONE);
616 616 }
617 617
618 618 static void
619 619 gate_desc_dump(gate_desc_t *gate, const char *label, int header)
620 620 {
621 621 const char *lastnm;
622 622 uint_t lastval;
623 623 char type[4];
624 624
625 625 switch (gate->sgd_type) {
626 626 case SDT_SYSIGT:
627 627 strcpy(type, "int");
628 628 break;
629 629 case SDT_SYSTGT:
630 630 strcpy(type, "trp");
631 631 break;
632 632 case SDT_SYSTASKGT:
633 633 strcpy(type, "tsk");
634 634 break;
635 635 default:
636 636 (void) mdb_snprintf(type, sizeof (type), "%3x", gate->sgd_type);
637 637 }
638 638
639 639 #if defined(__amd64)
640 640 lastnm = "IST";
641 641 lastval = gate->sgd_ist;
642 642 #else
643 643 lastnm = "STK";
644 644 lastval = gate->sgd_stkcpy;
645 645 #endif
646 646
647 647 if (header) {
648 648 mdb_printf("%*s%<u>%-30s%</u> %<u>%-4s%</u> %<u>%3s%</u> "
649 649 "%<u>%1s%</u> %<u>%3s%</u> %<u>%3s%</u>\n", strlen(label),
650 650 "", "HANDLER", "SEL", "DPL", "P", "TYP", lastnm);
651 651 }
652 652
653 653 mdb_printf("%s", label);
654 654
655 655 if (gate->sgd_type == SDT_SYSTASKGT)
656 656 mdb_printf("%-30s ", "-");
657 657 else
658 658 mdb_printf("%-30a ", GATESEG_GETOFFSET(gate));
659 659
660 660 mdb_printf("%4x %d %c %3s %2x\n", gate->sgd_selector,
661 661 gate->sgd_dpl, (gate->sgd_p ? '+' : ' '), type, lastval);
662 662 }
663 663
664 664 /*ARGSUSED*/
665 665 static int
666 666 gate_desc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
667 667 {
668 668 gate_desc_t gate;
669 669
670 670 if (argc != 0 || !(flags & DCMD_ADDRSPEC))
671 671 return (DCMD_USAGE);
672 672
673 673 if (mdb_vread(&gate, sizeof (gate_desc_t), addr) !=
674 674 sizeof (gate_desc_t)) {
675 675 mdb_warn("failed to read gate descriptor at %p\n", addr);
676 676 return (DCMD_ERR);
677 677 }
678 678
679 679 gate_desc_dump(&gate, "", DCMD_HDRSPEC(flags));
680 680
681 681 return (DCMD_OK);
682 682 }
683 683
684 684 /*ARGSUSED*/
685 685 static int
686 686 idt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
687 687 {
688 688 int i;
689 689
690 690 if (!(flags & DCMD_ADDRSPEC)) {
691 691 GElf_Sym idt0_va;
692 692 gate_desc_t *idt0;
693 693
694 694 if (mdb_lookup_by_name("idt0", &idt0_va) < 0) {
695 695 mdb_warn("failed to find VA of idt0");
696 696 return (DCMD_ERR);
697 697 }
698 698
699 699 addr = idt0_va.st_value;
700 700 if (mdb_vread(&idt0, sizeof (idt0), addr) != sizeof (idt0)) {
701 701 mdb_warn("failed to read idt0 at %p\n", addr);
702 702 return (DCMD_ERR);
703 703 }
704 704
705 705 addr = (uintptr_t)idt0;
706 706 }
707 707
708 708 for (i = 0; i < NIDT; i++, addr += sizeof (gate_desc_t)) {
709 709 gate_desc_t gate;
710 710 char label[6];
711 711
712 712 if (mdb_vread(&gate, sizeof (gate_desc_t), addr) !=
713 713 sizeof (gate_desc_t)) {
714 714 mdb_warn("failed to read gate descriptor at %p\n",
715 715 addr);
716 716 return (DCMD_ERR);
717 717 }
718 718
719 719 (void) mdb_snprintf(label, sizeof (label), "%3d: ", i);
720 720 gate_desc_dump(&gate, label, i == 0);
721 721 }
722 722
723 723 return (DCMD_OK);
724 724 }
725 725
726 726 static void
727 727 htables_help(void)
728 728 {
729 729 mdb_printf(
730 730 "Given a (hat_t *), generates the list of all (htable_t *)s\n"
731 731 "that correspond to that address space\n");
732 732 }
733 733
734 734 static void
735 735 report_maps_help(void)
736 736 {
737 737 mdb_printf(
738 738 "Given a PFN, report HAT structures that map the page, or use\n"
739 739 "the page as a pagetable.\n"
740 740 "\n"
741 741 "-m Interpret the PFN as an MFN (machine frame number)\n");
742 742 }
743 743
744 744 static void
745 745 ptable_help(void)
746 746 {
747 747 mdb_printf(
748 748 "Given a PFN holding a page table, print its contents, and\n"
749 749 "the address of the corresponding htable structure.\n"
750 750 "\n"
751 751 "-m Interpret the PFN as an MFN (machine frame number)\n");
752 752 }
753 753
754 754 /*
755 755 * NSEC_SHIFT is replicated here (it is not defined in a header file),
756 756 * but for amusement, the reader is directed to the comment that explains
757 757 * the rationale for this particular value on x86. Spoiler: the value is
758 758 * selected to accommodate 60 MHz Pentiums! (And a confession: if the voice
759 759 * in that comment sounds too familiar, it's because your author also wrote
760 760 * that code -- some fifteen years prior to this writing in 2011...)
761 761 */
762 762 #define NSEC_SHIFT 5
763 763
764 764 /*ARGSUSED*/
765 765 static int
766 766 scalehrtime_cmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
767 767 {
768 768 uint32_t nsec_scale;
769 769 hrtime_t tsc = addr, hrt;
770 770 unsigned int *tscp = (unsigned int *)&tsc;
771 771 uintptr_t scalehrtimef;
772 772 uint64_t scale;
773 773 GElf_Sym sym;
774 774
775 775 if (!(flags & DCMD_ADDRSPEC)) {
776 776 if (argc != 1)
777 777 return (DCMD_USAGE);
778 778
779 779 switch (argv[0].a_type) {
780 780 case MDB_TYPE_STRING:
781 781 tsc = mdb_strtoull(argv[0].a_un.a_str);
782 782 break;
783 783 case MDB_TYPE_IMMEDIATE:
784 784 tsc = argv[0].a_un.a_val;
785 785 break;
786 786 default:
787 787 return (DCMD_USAGE);
788 788 }
789 789 }
790 790
791 791 if (mdb_readsym(&scalehrtimef,
792 792 sizeof (scalehrtimef), "scalehrtimef") == -1) {
793 793 mdb_warn("couldn't read 'scalehrtimef'");
794 794 return (DCMD_ERR);
795 795 }
796 796
797 797 if (mdb_lookup_by_name("tsc_scalehrtime", &sym) == -1) {
798 798 mdb_warn("couldn't find 'tsc_scalehrtime'");
799 799 return (DCMD_ERR);
800 800 }
801 801
802 802 if (sym.st_value != scalehrtimef) {
803 803 mdb_warn("::scalehrtime requires that scalehrtimef "
804 804 "be set to tsc_scalehrtime\n");
805 805 return (DCMD_ERR);
806 806 }
807 807
808 808 if (mdb_readsym(&nsec_scale, sizeof (nsec_scale), "nsec_scale") == -1) {
809 809 mdb_warn("couldn't read 'nsec_scale'");
810 810 return (DCMD_ERR);
811 811 }
812 812
813 813 scale = (uint64_t)nsec_scale;
814 814
815 815 hrt = ((uint64_t)tscp[1] * scale) << NSEC_SHIFT;
816 816 hrt += ((uint64_t)tscp[0] * scale) >> (32 - NSEC_SHIFT);
817 817
818 818 mdb_printf("0x%llx\n", hrt);
819 819
820 820 return (DCMD_OK);
821 821 }
822 822
823 823 /*
824 824 * The x86 feature set is implemented as a bitmap array. That bitmap array is
825 825 * stored across a number of uchars based on the BT_SIZEOFMAP(NUM_X86_FEATURES)
826 826 * macro. We have the names for each of these features in unix's text segment
827 827 * so we do not have to duplicate them and instead just look them up.
828 828 */
829 829 /*ARGSUSED*/
830 830 static int
831 831 x86_featureset_cmd(uintptr_t addr, uint_t flags, int argc,
832 832 const mdb_arg_t *argv)
833 833 {
834 834 uchar_t *fset;
835 835 GElf_Sym sym;
836 836 uintptr_t nptr;
837 837 char name[128];
838 838 int ii;
839 839
840 840 size_t sz = sizeof (uchar_t) * BT_SIZEOFMAP(NUM_X86_FEATURES);
841 841
842 842 if (argc != 0)
843 843 return (DCMD_USAGE);
844 844
845 845 if (mdb_lookup_by_name("x86_feature_names", &sym) == -1) {
846 846 mdb_warn("couldn't find x86_feature_names");
847 847 return (DCMD_ERR);
848 848 }
849 849
850 850 fset = mdb_zalloc(sz, UM_NOSLEEP);
851 851 if (fset == NULL) {
852 852 mdb_warn("failed to allocate memory for x86_featureset");
↓ open down ↓ |
852 lines elided |
↑ open up ↑ |
853 853 return (DCMD_ERR);
854 854 }
855 855
856 856 if (mdb_readvar(fset, "x86_featureset") != sz) {
857 857 mdb_warn("failed to read x86_featureset");
858 858 mdb_free(fset, sz);
859 859 return (DCMD_ERR);
860 860 }
861 861
862 862 for (ii = 0; ii < NUM_X86_FEATURES; ii++) {
863 + /* LINTED */
863 864 if (!BT_TEST((ulong_t *)fset, ii))
864 865 continue;
865 866
866 867 if (mdb_vread(&nptr, sizeof (char *), sym.st_value +
867 868 sizeof (void *) * ii) != sizeof (char *)) {
868 869 mdb_warn("failed to read feature array %d", ii);
869 870 mdb_free(fset, sz);
870 871 return (DCMD_ERR);
871 872 }
872 873
873 874 if (mdb_readstr(name, sizeof (name), nptr) == -1) {
874 875 mdb_warn("failed to read feature %d", ii);
875 876 mdb_free(fset, sz);
876 877 return (DCMD_ERR);
877 878 }
878 879 mdb_printf("%s\n", name);
879 880 }
880 881
881 882 mdb_free(fset, sz);
882 883 return (DCMD_OK);
883 884 }
884 885
885 886 static const mdb_dcmd_t dcmds[] = {
886 887 { "gate_desc", ":", "dump a gate descriptor", gate_desc },
887 888 { "idt", ":[-v]", "dump an IDT", idt },
888 889 { "ttrace", "[-x]", "dump trap trace buffers", ttrace },
889 890 { "vatopfn", ":[-a as]", "translate address to physical page",
890 891 va2pfn_dcmd },
891 892 { "report_maps", ":[-m]",
892 893 "Given PFN, report mappings / page table usage",
893 894 report_maps_dcmd, report_maps_help },
894 895 { "htables", "", "Given hat_t *, lists all its htable_t * values",
895 896 htables_dcmd, htables_help },
896 897 { "ptable", ":[-m]", "Given PFN, dump contents of a page table",
897 898 ptable_dcmd, ptable_help },
898 899 { "pte", ":[-p XXXXX] [-l N]", "print human readable page table entry",
899 900 pte_dcmd },
900 901 { "pfntomfn", ":", "convert physical page to hypervisor machine page",
901 902 pfntomfn_dcmd },
902 903 { "mfntopfn", ":", "convert hypervisor machine page to physical page",
903 904 mfntopfn_dcmd },
904 905 { "memseg_list", ":", "show memseg list", memseg_list },
905 906 { "scalehrtime", ":",
906 907 "scale an unscaled high-res time", scalehrtime_cmd },
907 908 { "x86_featureset", NULL, "dump the x86_featureset vector",
908 909 x86_featureset_cmd },
909 910 { NULL }
910 911 };
911 912
912 913 static const mdb_walker_t walkers[] = {
913 914 { "ttrace", "walks trap trace buffers in reverse chronological order",
914 915 ttrace_walk_init, ttrace_walk_step, ttrace_walk_fini },
915 916 { "mutex_owner", "walks the owner of a mutex",
916 917 mutex_owner_init, mutex_owner_step },
917 918 { "memseg", "walk the memseg structures",
918 919 memseg_walk_init, memseg_walk_step, memseg_walk_fini },
919 920 { NULL }
920 921 };
921 922
922 923 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
923 924
924 925 const mdb_modinfo_t *
925 926 _mdb_init(void)
926 927 {
927 928 return (&modinfo);
928 929 }
929 930
930 931 void
931 932 _mdb_fini(void)
932 933 {
933 934 free_mmu();
934 935 }
↓ open down ↓ |
62 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX