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