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) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2013 by Delphix. All rights reserved. 25 * Copyright (c) 2018, Joyent, Inc. All rights reserved. 26 */ 27 28 /* 29 * explicitly define DTRACE_ERRDEBUG to pull in definition of dtrace_errhash_t 30 * explicitly define _STDARG_H to avoid stdarg.h/varargs.h u/k defn conflict 31 */ 32 #define DTRACE_ERRDEBUG 33 #define _STDARG_H 34 35 #include <mdb/mdb_param.h> 36 #include <mdb/mdb_modapi.h> 37 #include <mdb/mdb_ks.h> 38 #include <sys/dtrace_impl.h> 39 #include <sys/vmem_impl.h> 40 #include <sys/ddi_impldefs.h> 41 #include <sys/sysmacros.h> 42 #include <sys/kobj.h> 43 #include <dtrace.h> 44 #include <alloca.h> 45 #include <ctype.h> 46 #include <errno.h> 47 #include <math.h> 48 #include <stdio.h> 49 #include <unistd.h> 50 51 /*ARGSUSED*/ 52 int 53 id2probe(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 54 { 55 uintptr_t probe = NULL; 56 uintptr_t probes; 57 58 if (!(flags & DCMD_ADDRSPEC)) 59 return (DCMD_USAGE); 60 61 if (addr == DTRACE_IDNONE || addr > UINT32_MAX) 62 goto out; 63 64 if (mdb_readvar(&probes, "dtrace_probes") == -1) { 65 mdb_warn("failed to read 'dtrace_probes'"); 66 return (DCMD_ERR); 67 } 68 69 probes += (addr - 1) * sizeof (dtrace_probe_t *); 70 71 if (mdb_vread(&probe, sizeof (uintptr_t), probes) == -1) { 72 mdb_warn("failed to read dtrace_probes[%d]", addr - 1); 73 return (DCMD_ERR); 74 } 75 76 out: 77 mdb_printf("%p\n", probe); 78 return (DCMD_OK); 79 } 80 81 void 82 dtrace_help(void) 83 { 84 85 mdb_printf("Given a dtrace_state_t structure that represents a " 86 "DTrace consumer, prints\n" 87 "dtrace(1M)-like output for in-kernel DTrace data. (The " 88 "dtrace_state_t\n" 89 "structures for all DTrace consumers may be obtained by running " 90 "the \n" 91 "::dtrace_state dcmd.) When data is present on multiple CPUs, " 92 "data are\n" 93 "presented in CPU order, with records within each CPU ordered " 94 "oldest to \n" 95 "youngest. Options:\n\n" 96 "-c cpu Only provide output for specified CPU.\n"); 97 } 98 99 static int 100 dtracemdb_eprobe(dtrace_state_t *state, dtrace_eprobedesc_t *epd) 101 { 102 dtrace_epid_t epid = epd->dtepd_epid; 103 dtrace_probe_t probe; 104 dtrace_ecb_t ecb; 105 uintptr_t addr, paddr, ap; 106 dtrace_action_t act; 107 int nactions, nrecs; 108 109 addr = (uintptr_t)state->dts_ecbs + 110 (epid - 1) * sizeof (dtrace_ecb_t *); 111 112 if (mdb_vread(&addr, sizeof (addr), addr) == -1) { 113 mdb_warn("failed to read ecb for epid %d", epid); 114 return (-1); 115 } 116 117 if (addr == NULL) { 118 mdb_warn("epid %d doesn't match an ecb\n", epid); 119 return (-1); 120 } 121 122 if (mdb_vread(&ecb, sizeof (ecb), addr) == -1) { 123 mdb_warn("failed to read ecb at %p", addr); 124 return (-1); 125 } 126 127 paddr = (uintptr_t)ecb.dte_probe; 128 129 if (mdb_vread(&probe, sizeof (probe), paddr) == -1) { 130 mdb_warn("failed to read probe for ecb %p", addr); 131 return (-1); 132 } 133 134 /* 135 * This is a little painful: in order to find the number of actions, 136 * we need to first walk through them. 137 */ 138 for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) { 139 if (mdb_vread(&act, sizeof (act), ap) == -1) { 140 mdb_warn("failed to read action %p on ecb %p", 141 ap, addr); 142 return (-1); 143 } 144 145 if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) 146 nactions++; 147 148 ap = (uintptr_t)act.dta_next; 149 } 150 151 nrecs = epd->dtepd_nrecs; 152 epd->dtepd_nrecs = nactions; 153 epd->dtepd_probeid = probe.dtpr_id; 154 epd->dtepd_uarg = ecb.dte_uarg; 155 epd->dtepd_size = ecb.dte_size; 156 157 for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) { 158 if (mdb_vread(&act, sizeof (act), ap) == -1) { 159 mdb_warn("failed to read action %p on ecb %p", 160 ap, addr); 161 return (-1); 162 } 163 164 if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) { 165 if (nrecs-- == 0) 166 break; 167 168 epd->dtepd_rec[nactions++] = act.dta_rec; 169 } 170 171 ap = (uintptr_t)act.dta_next; 172 } 173 174 return (0); 175 } 176 177 /*ARGSUSED*/ 178 static int 179 dtracemdb_probe(dtrace_state_t *state, dtrace_probedesc_t *pd) 180 { 181 uintptr_t base, addr, paddr, praddr; 182 int nprobes, i; 183 dtrace_probe_t probe; 184 dtrace_provider_t prov; 185 186 if (pd->dtpd_id == DTRACE_IDNONE) 187 pd->dtpd_id++; 188 189 if (mdb_readvar(&base, "dtrace_probes") == -1) { 190 mdb_warn("failed to read 'dtrace_probes'"); 191 return (-1); 192 } 193 194 if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) { 195 mdb_warn("failed to read 'dtrace_nprobes'"); 196 return (-1); 197 } 198 199 for (i = pd->dtpd_id; i <= nprobes; i++) { 200 addr = base + (i - 1) * sizeof (dtrace_probe_t *); 201 202 if (mdb_vread(&paddr, sizeof (paddr), addr) == -1) { 203 mdb_warn("couldn't read probe pointer at %p", addr); 204 return (-1); 205 } 206 207 if (paddr != NULL) 208 break; 209 } 210 211 if (paddr == NULL) { 212 errno = ESRCH; 213 return (-1); 214 } 215 216 if (mdb_vread(&probe, sizeof (probe), paddr) == -1) { 217 mdb_warn("couldn't read probe at %p", paddr); 218 return (-1); 219 } 220 221 pd->dtpd_id = probe.dtpr_id; 222 223 if (mdb_vread(pd->dtpd_name, DTRACE_NAMELEN, 224 (uintptr_t)probe.dtpr_name) == -1) { 225 mdb_warn("failed to read probe name for probe %p", paddr); 226 return (-1); 227 } 228 229 if (mdb_vread(pd->dtpd_func, DTRACE_FUNCNAMELEN, 230 (uintptr_t)probe.dtpr_func) == -1) { 231 mdb_warn("failed to read function name for probe %p", paddr); 232 return (-1); 233 } 234 235 if (mdb_vread(pd->dtpd_mod, DTRACE_MODNAMELEN, 236 (uintptr_t)probe.dtpr_mod) == -1) { 237 mdb_warn("failed to read module name for probe %p", paddr); 238 return (-1); 239 } 240 241 praddr = (uintptr_t)probe.dtpr_provider; 242 243 if (mdb_vread(&prov, sizeof (prov), praddr) == -1) { 244 mdb_warn("failed to read provider for probe %p", paddr); 245 return (-1); 246 } 247 248 if (mdb_vread(pd->dtpd_provider, DTRACE_PROVNAMELEN, 249 (uintptr_t)prov.dtpv_name) == -1) { 250 mdb_warn("failed to read provider name for probe %p", paddr); 251 return (-1); 252 } 253 254 return (0); 255 } 256 257 /*ARGSUSED*/ 258 static int 259 dtracemdb_aggdesc(dtrace_state_t *state, dtrace_aggdesc_t *agd) 260 { 261 dtrace_aggid_t aggid = agd->dtagd_id; 262 dtrace_aggregation_t agg; 263 dtrace_ecb_t ecb; 264 uintptr_t addr, eaddr, ap, last; 265 dtrace_action_t act; 266 dtrace_recdesc_t *lrec; 267 int nactions, nrecs; 268 269 addr = (uintptr_t)state->dts_aggregations + 270 (aggid - 1) * sizeof (dtrace_aggregation_t *); 271 272 if (mdb_vread(&addr, sizeof (addr), addr) == -1) { 273 mdb_warn("failed to read aggregation for aggid %d", aggid); 274 return (-1); 275 } 276 277 if (addr == NULL) { 278 mdb_warn("aggid %d doesn't match an aggregation\n", aggid); 279 return (-1); 280 } 281 282 if (mdb_vread(&agg, sizeof (agg), addr) == -1) { 283 mdb_warn("failed to read aggregation at %p", addr); 284 return (-1); 285 } 286 287 eaddr = (uintptr_t)agg.dtag_ecb; 288 289 if (mdb_vread(&ecb, sizeof (ecb), eaddr) == -1) { 290 mdb_warn("failed to read ecb for aggregation %p", addr); 291 return (-1); 292 } 293 294 last = (uintptr_t)addr + offsetof(dtrace_aggregation_t, dtag_action); 295 296 /* 297 * This is a little painful: in order to find the number of actions, 298 * we need to first walk through them. 299 */ 300 ap = (uintptr_t)agg.dtag_first; 301 nactions = 0; 302 303 for (;;) { 304 if (mdb_vread(&act, sizeof (act), ap) == -1) { 305 mdb_warn("failed to read action %p on aggregation %p", 306 ap, addr); 307 return (-1); 308 } 309 310 nactions++; 311 312 if (ap == last) 313 break; 314 315 ap = (uintptr_t)act.dta_next; 316 } 317 318 lrec = &act.dta_rec; 319 agd->dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - agg.dtag_base; 320 321 nrecs = agd->dtagd_nrecs; 322 agd->dtagd_nrecs = nactions; 323 agd->dtagd_epid = ecb.dte_epid; 324 325 ap = (uintptr_t)agg.dtag_first; 326 nactions = 0; 327 328 for (;;) { 329 dtrace_recdesc_t rec; 330 331 if (mdb_vread(&act, sizeof (act), ap) == -1) { 332 mdb_warn("failed to read action %p on aggregation %p", 333 ap, addr); 334 return (-1); 335 } 336 337 if (nrecs-- == 0) 338 break; 339 340 rec = act.dta_rec; 341 rec.dtrd_offset -= agg.dtag_base; 342 rec.dtrd_uarg = 0; 343 agd->dtagd_rec[nactions++] = rec; 344 345 if (ap == last) 346 break; 347 348 ap = (uintptr_t)act.dta_next; 349 } 350 351 return (0); 352 } 353 354 static int 355 dtracemdb_bufsnap(dtrace_buffer_t *which, dtrace_bufdesc_t *desc) 356 { 357 uintptr_t addr; 358 size_t bufsize; 359 dtrace_buffer_t buf; 360 caddr_t data = desc->dtbd_data; 361 processorid_t max_cpuid, cpu = desc->dtbd_cpu; 362 363 if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) { 364 mdb_warn("failed to read 'max_cpuid'"); 365 errno = EIO; 366 return (-1); 367 } 368 369 if (cpu < 0 || cpu > max_cpuid) { 370 errno = EINVAL; 371 return (-1); 372 } 373 374 addr = (uintptr_t)which + cpu * sizeof (dtrace_buffer_t); 375 376 if (mdb_vread(&buf, sizeof (buf), addr) == -1) { 377 mdb_warn("failed to read buffer description at %p", addr); 378 errno = EIO; 379 return (-1); 380 } 381 382 if (buf.dtb_tomax == NULL) { 383 errno = ENOENT; 384 return (-1); 385 } 386 387 if (buf.dtb_flags & DTRACEBUF_WRAPPED) { 388 bufsize = buf.dtb_size; 389 } else { 390 bufsize = buf.dtb_offset; 391 } 392 393 if (mdb_vread(data, bufsize, (uintptr_t)buf.dtb_tomax) == -1) { 394 mdb_warn("couldn't read buffer for CPU %d", cpu); 395 errno = EIO; 396 return (-1); 397 } 398 399 if (buf.dtb_offset > buf.dtb_size) { 400 mdb_warn("buffer for CPU %d has corrupt offset\n", cpu); 401 errno = EIO; 402 return (-1); 403 } 404 405 if (buf.dtb_flags & DTRACEBUF_WRAPPED) { 406 if (buf.dtb_xamot_offset > buf.dtb_size) { 407 mdb_warn("ringbuffer for CPU %d has corrupt " 408 "wrapped offset\n", cpu); 409 errno = EIO; 410 return (-1); 411 } 412 413 /* 414 * If the ring buffer has wrapped, it needs to be polished. 415 * See the comment in dtrace_buffer_polish() for details. 416 */ 417 if (buf.dtb_offset < buf.dtb_xamot_offset) { 418 bzero(data + buf.dtb_offset, 419 buf.dtb_xamot_offset - buf.dtb_offset); 420 } 421 422 if (buf.dtb_offset > buf.dtb_xamot_offset) { 423 bzero(data + buf.dtb_offset, 424 buf.dtb_size - buf.dtb_offset); 425 bzero(data, buf.dtb_xamot_offset); 426 } 427 428 desc->dtbd_oldest = buf.dtb_xamot_offset; 429 } else { 430 desc->dtbd_oldest = 0; 431 } 432 433 desc->dtbd_size = bufsize; 434 desc->dtbd_drops = buf.dtb_drops; 435 desc->dtbd_errors = buf.dtb_errors; 436 desc->dtbd_timestamp = gethrtime(); 437 438 return (0); 439 } 440 441 /* 442 * This is essentially identical to its cousin in the kernel -- with the 443 * notable exception that we automatically set DTRACEOPT_GRABANON if this 444 * state is an anonymous enabling. 445 */ 446 static dof_hdr_t * 447 dtracemdb_dof_create(dtrace_state_t *state, int isanon) 448 { 449 dof_hdr_t *dof; 450 dof_sec_t *sec; 451 dof_optdesc_t *opt; 452 int i, len = sizeof (dof_hdr_t) + 453 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 454 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 455 456 dof = mdb_zalloc(len, UM_SLEEP); 457 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 458 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 459 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 460 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 461 462 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 463 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 464 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 465 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 466 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 467 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 468 469 dof->dofh_flags = 0; 470 dof->dofh_hdrsize = sizeof (dof_hdr_t); 471 dof->dofh_secsize = sizeof (dof_sec_t); 472 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 473 dof->dofh_secoff = sizeof (dof_hdr_t); 474 dof->dofh_loadsz = len; 475 dof->dofh_filesz = len; 476 dof->dofh_pad = 0; 477 478 /* 479 * Fill in the option section header... 480 */ 481 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 482 sec->dofs_type = DOF_SECT_OPTDESC; 483 sec->dofs_align = sizeof (uint64_t); 484 sec->dofs_flags = DOF_SECF_LOAD; 485 sec->dofs_entsize = sizeof (dof_optdesc_t); 486 487 opt = (dof_optdesc_t *)((uintptr_t)sec + 488 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 489 490 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 491 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 492 493 for (i = 0; i < DTRACEOPT_MAX; i++) { 494 opt[i].dofo_option = i; 495 opt[i].dofo_strtab = DOF_SECIDX_NONE; 496 opt[i].dofo_value = state->dts_options[i]; 497 } 498 499 if (isanon) 500 opt[DTRACEOPT_GRABANON].dofo_value = 1; 501 502 return (dof); 503 } 504 505 static int 506 dtracemdb_format(dtrace_state_t *state, dtrace_fmtdesc_t *desc) 507 { 508 uintptr_t addr, faddr; 509 char c; 510 int len = 0; 511 512 if (desc->dtfd_format == 0 || desc->dtfd_format > state->dts_nformats) { 513 errno = EINVAL; 514 return (-1); 515 } 516 517 faddr = (uintptr_t)state->dts_formats + 518 (desc->dtfd_format - 1) * sizeof (char *); 519 520 if (mdb_vread(&addr, sizeof (addr), faddr) == -1) { 521 mdb_warn("failed to read format string pointer at %p", faddr); 522 return (-1); 523 } 524 525 do { 526 if (mdb_vread(&c, sizeof (c), addr + len++) == -1) { 527 mdb_warn("failed to read format string at %p", addr); 528 return (-1); 529 } 530 } while (c != '\0'); 531 532 if (len > desc->dtfd_length) { 533 desc->dtfd_length = len; 534 return (0); 535 } 536 537 if (mdb_vread(desc->dtfd_string, len, addr) == -1) { 538 mdb_warn("failed to reread format string at %p", addr); 539 return (-1); 540 } 541 542 return (0); 543 } 544 545 static int 546 dtracemdb_status(dtrace_state_t *state, dtrace_status_t *status) 547 { 548 dtrace_dstate_t *dstate; 549 int i, j; 550 uint64_t nerrs; 551 uintptr_t addr; 552 int ncpu; 553 554 if (mdb_readvar(&ncpu, "_ncpu") == -1) { 555 mdb_warn("failed to read '_ncpu'"); 556 return (DCMD_ERR); 557 } 558 559 bzero(status, sizeof (dtrace_status_t)); 560 561 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 562 errno = ENOENT; 563 return (-1); 564 } 565 566 /* 567 * For the MDB backend, we never set dtst_exiting or dtst_filled. This 568 * is by design: we don't want the library to try to stop tracing, 569 * because it doesn't particularly mean anything. 570 */ 571 nerrs = state->dts_errors; 572 dstate = &state->dts_vstate.dtvs_dynvars; 573 574 for (i = 0; i < ncpu; i++) { 575 dtrace_dstate_percpu_t dcpu; 576 dtrace_buffer_t buf; 577 578 addr = (uintptr_t)&dstate->dtds_percpu[i]; 579 580 if (mdb_vread(&dcpu, sizeof (dcpu), addr) == -1) { 581 mdb_warn("failed to read per-CPU dstate at %p", addr); 582 return (-1); 583 } 584 585 status->dtst_dyndrops += dcpu.dtdsc_drops; 586 status->dtst_dyndrops_dirty += dcpu.dtdsc_dirty_drops; 587 status->dtst_dyndrops_rinsing += dcpu.dtdsc_rinsing_drops; 588 589 addr = (uintptr_t)&state->dts_buffer[i]; 590 591 if (mdb_vread(&buf, sizeof (buf), addr) == -1) { 592 mdb_warn("failed to read per-CPU buffer at %p", addr); 593 return (-1); 594 } 595 596 nerrs += buf.dtb_errors; 597 598 for (j = 0; j < state->dts_nspeculations; j++) { 599 dtrace_speculation_t spec; 600 601 addr = (uintptr_t)&state->dts_speculations[j]; 602 603 if (mdb_vread(&spec, sizeof (spec), addr) == -1) { 604 mdb_warn("failed to read " 605 "speculation at %p", addr); 606 return (-1); 607 } 608 609 addr = (uintptr_t)&spec.dtsp_buffer[i]; 610 611 if (mdb_vread(&buf, sizeof (buf), addr) == -1) { 612 mdb_warn("failed to read " 613 "speculative buffer at %p", addr); 614 return (-1); 615 } 616 617 status->dtst_specdrops += buf.dtb_xamot_drops; 618 } 619 } 620 621 status->dtst_specdrops_busy = state->dts_speculations_busy; 622 status->dtst_specdrops_unavail = state->dts_speculations_unavail; 623 status->dtst_errors = nerrs; 624 625 return (0); 626 } 627 628 typedef struct dtracemdb_data { 629 dtrace_state_t *dtmd_state; 630 char *dtmd_symstr; 631 char *dtmd_modstr; 632 uintptr_t dtmd_addr; 633 int dtmd_isanon; 634 } dtracemdb_data_t; 635 636 static int 637 dtracemdb_ioctl(void *varg, int cmd, void *arg) 638 { 639 dtracemdb_data_t *data = varg; 640 dtrace_state_t *state = data->dtmd_state; 641 642 switch (cmd) { 643 case DTRACEIOC_CONF: { 644 dtrace_conf_t *conf = arg; 645 646 bzero(conf, sizeof (conf)); 647 conf->dtc_difversion = DIF_VERSION; 648 conf->dtc_difintregs = DIF_DIR_NREGS; 649 conf->dtc_diftupregs = DIF_DTR_NREGS; 650 conf->dtc_ctfmodel = CTF_MODEL_NATIVE; 651 652 return (0); 653 } 654 655 case DTRACEIOC_DOFGET: { 656 dof_hdr_t *hdr = arg, *dof; 657 658 dof = dtracemdb_dof_create(state, data->dtmd_isanon); 659 bcopy(dof, hdr, MIN(hdr->dofh_loadsz, dof->dofh_loadsz)); 660 mdb_free(dof, dof->dofh_loadsz); 661 662 return (0); 663 } 664 665 case DTRACEIOC_BUFSNAP: 666 return (dtracemdb_bufsnap(state->dts_buffer, arg)); 667 668 case DTRACEIOC_AGGSNAP: 669 return (dtracemdb_bufsnap(state->dts_aggbuffer, arg)); 670 671 case DTRACEIOC_AGGDESC: 672 return (dtracemdb_aggdesc(state, arg)); 673 674 case DTRACEIOC_EPROBE: 675 return (dtracemdb_eprobe(state, arg)); 676 677 case DTRACEIOC_PROBES: 678 return (dtracemdb_probe(state, arg)); 679 680 case DTRACEIOC_FORMAT: 681 return (dtracemdb_format(state, arg)); 682 683 case DTRACEIOC_STATUS: 684 return (dtracemdb_status(state, arg)); 685 686 case DTRACEIOC_GO: 687 *(processorid_t *)arg = -1; 688 return (0); 689 690 case DTRACEIOC_ENABLE: 691 errno = ENOTTY; /* see dt_open.c:dtrace_go() */ 692 return (-1); 693 694 case DTRACEIOC_PROVIDER: 695 case DTRACEIOC_PROBEMATCH: 696 errno = ESRCH; 697 return (-1); 698 699 default: 700 mdb_warn("unexpected ioctl 0x%x (%s)\n", cmd, 701 cmd == DTRACEIOC_PROVIDER ? "DTRACEIOC_PROVIDER" : 702 cmd == DTRACEIOC_PROBES ? "DTRACEIOC_PROBES" : 703 cmd == DTRACEIOC_BUFSNAP ? "DTRACEIOC_BUFSNAP" : 704 cmd == DTRACEIOC_PROBEMATCH ? "DTRACEIOC_PROBEMATCH" : 705 cmd == DTRACEIOC_ENABLE ? "DTRACEIOC_ENABLE" : 706 cmd == DTRACEIOC_AGGSNAP ? "DTRACEIOC_AGGSNAP" : 707 cmd == DTRACEIOC_EPROBE ? "DTRACEIOC_EPROBE" : 708 cmd == DTRACEIOC_PROBEARG ? "DTRACEIOC_PROBEARG" : 709 cmd == DTRACEIOC_CONF ? "DTRACEIOC_CONF" : 710 cmd == DTRACEIOC_STATUS ? "DTRACEIOC_STATUS" : 711 cmd == DTRACEIOC_GO ? "DTRACEIOC_GO" : 712 cmd == DTRACEIOC_STOP ? "DTRACEIOC_STOP" : 713 cmd == DTRACEIOC_AGGDESC ? "DTRACEIOC_AGGDESC" : 714 cmd == DTRACEIOC_FORMAT ? "DTRACEIOC_FORMAT" : 715 cmd == DTRACEIOC_DOFGET ? "DTRACEIOC_DOFGET" : 716 cmd == DTRACEIOC_REPLICATE ? "DTRACEIOC_REPLICATE" : 717 "???"); 718 errno = ENXIO; 719 return (-1); 720 } 721 } 722 723 static int 724 dtracemdb_modctl(uintptr_t addr, const struct modctl *m, dtracemdb_data_t *data) 725 { 726 struct module mod; 727 728 if (m->mod_mp == NULL) 729 return (WALK_NEXT); 730 731 if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) { 732 mdb_warn("couldn't read modctl %p's module", addr); 733 return (WALK_NEXT); 734 } 735 736 if ((uintptr_t)mod.text > data->dtmd_addr) 737 return (WALK_NEXT); 738 739 if ((uintptr_t)mod.text + mod.text_size <= data->dtmd_addr) 740 return (WALK_NEXT); 741 742 if (mdb_readstr(data->dtmd_modstr, MDB_SYM_NAMLEN, 743 (uintptr_t)m->mod_modname) == -1) 744 return (WALK_ERR); 745 746 return (WALK_DONE); 747 } 748 749 static int 750 dtracemdb_lookup_by_addr(void *varg, GElf_Addr addr, GElf_Sym *symp, 751 dtrace_syminfo_t *sip) 752 { 753 dtracemdb_data_t *data = varg; 754 755 if (data->dtmd_symstr == NULL) { 756 data->dtmd_symstr = mdb_zalloc(MDB_SYM_NAMLEN, 757 UM_SLEEP | UM_GC); 758 } 759 760 if (data->dtmd_modstr == NULL) { 761 data->dtmd_modstr = mdb_zalloc(MDB_SYM_NAMLEN, 762 UM_SLEEP | UM_GC); 763 } 764 765 if (symp != NULL) { 766 if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, data->dtmd_symstr, 767 MDB_SYM_NAMLEN, symp) == -1) 768 return (-1); 769 } 770 771 if (sip != NULL) { 772 data->dtmd_addr = addr; 773 774 (void) strcpy(data->dtmd_modstr, "???"); 775 776 if (mdb_walk("modctl", 777 (mdb_walk_cb_t)dtracemdb_modctl, varg) == -1) { 778 mdb_warn("couldn't walk 'modctl'"); 779 return (-1); 780 } 781 782 sip->dts_object = data->dtmd_modstr; 783 sip->dts_id = 0; 784 sip->dts_name = symp != NULL ? data->dtmd_symstr : NULL; 785 } 786 787 return (0); 788 } 789 790 /*ARGSUSED*/ 791 static int 792 dtracemdb_stat(void *varg, processorid_t cpu) 793 { 794 GElf_Sym sym; 795 cpu_t c; 796 uintptr_t caddr, addr; 797 798 if (mdb_lookup_by_name("cpu", &sym) == -1) { 799 mdb_warn("failed to find symbol for 'cpu'"); 800 return (-1); 801 } 802 803 if (cpu * sizeof (uintptr_t) > sym.st_size) 804 return (-1); 805 806 addr = (uintptr_t)sym.st_value + cpu * sizeof (uintptr_t); 807 808 if (mdb_vread(&caddr, sizeof (caddr), addr) == -1) { 809 mdb_warn("failed to read cpu[%d]", cpu); 810 return (-1); 811 } 812 813 if (caddr == NULL) 814 return (-1); 815 816 if (mdb_vread(&c, sizeof (c), caddr) == -1) { 817 mdb_warn("failed to read cpu at %p", caddr); 818 return (-1); 819 } 820 821 if (c.cpu_flags & CPU_POWEROFF) { 822 return (P_POWEROFF); 823 } else if (c.cpu_flags & CPU_SPARE) { 824 return (P_SPARE); 825 } else if (c.cpu_flags & CPU_FAULTED) { 826 return (P_FAULTED); 827 } else if ((c.cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) { 828 return (P_OFFLINE); 829 } else if (c.cpu_flags & CPU_ENABLE) { 830 return (P_ONLINE); 831 } else { 832 return (P_NOINTR); 833 } 834 } 835 836 /*ARGSUSED*/ 837 static long 838 dtracemdb_sysconf(void *varg, int name) 839 { 840 int max_ncpus; 841 processorid_t max_cpuid; 842 843 switch (name) { 844 case _SC_CPUID_MAX: 845 if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) { 846 mdb_warn("failed to read 'max_cpuid'"); 847 return (-1); 848 } 849 850 return (max_cpuid); 851 852 case _SC_NPROCESSORS_MAX: 853 if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) { 854 mdb_warn("failed to read 'max_ncpus'"); 855 return (-1); 856 } 857 858 return (max_ncpus); 859 860 default: 861 mdb_warn("unexpected sysconf code %d\n", name); 862 return (-1); 863 } 864 } 865 866 const dtrace_vector_t dtrace_mdbops = { 867 dtracemdb_ioctl, 868 dtracemdb_lookup_by_addr, 869 dtracemdb_stat, 870 dtracemdb_sysconf 871 }; 872 873 typedef struct dtrace_dcmddata { 874 dtrace_hdl_t *dtdd_dtp; 875 int dtdd_cpu; 876 int dtdd_quiet; 877 int dtdd_flowindent; 878 int dtdd_heading; 879 FILE *dtdd_output; 880 } dtrace_dcmddata_t; 881 882 /* 883 * Helper to grab all the content from a file, spit it into a string, and erase 884 * and reset the file. 885 */ 886 static void 887 print_and_truncate_file(FILE *fp) 888 { 889 long len; 890 char *out; 891 892 /* flush, find length of file, seek to beginning, initialize buffer */ 893 if (fflush(fp) || (len = ftell(fp)) < 0 || 894 fseek(fp, 0, SEEK_SET) < 0) { 895 mdb_warn("couldn't prepare DTrace output file: %d\n", errno); 896 return; 897 } 898 899 out = mdb_alloc(len + 1, UM_SLEEP); 900 out[len] = '\0'; 901 902 /* read file into buffer, truncate file, and seek to beginning */ 903 if ((fread(out, len + 1, sizeof (char), fp) == 0 && ferror(fp)) || 904 ftruncate(fileno(fp), 0) < 0 || fseek(fp, 0, SEEK_SET) < 0) { 905 mdb_warn("couldn't read DTrace output file: %d\n", errno); 906 mdb_free(out, len + 1); 907 return; 908 } 909 910 mdb_printf("%s", out); 911 mdb_free(out, len + 1); 912 } 913 914 /*ARGSUSED*/ 915 static int 916 dtrace_dcmdrec(const dtrace_probedata_t *data, 917 const dtrace_recdesc_t *rec, void *arg) 918 { 919 dtrace_dcmddata_t *dd = arg; 920 921 print_and_truncate_file(dd->dtdd_output); 922 923 if (rec == NULL) { 924 /* 925 * We have processed the final record; output the newline if 926 * we're not in quiet mode. 927 */ 928 if (!dd->dtdd_quiet) 929 mdb_printf("\n"); 930 931 return (DTRACE_CONSUME_NEXT); 932 } 933 934 return (DTRACE_CONSUME_THIS); 935 } 936 937 /*ARGSUSED*/ 938 static int 939 dtrace_dcmdprobe(const dtrace_probedata_t *data, void *arg) 940 { 941 dtrace_probedesc_t *pd = data->dtpda_pdesc; 942 processorid_t cpu = data->dtpda_cpu; 943 dtrace_dcmddata_t *dd = arg; 944 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2]; 945 946 if (dd->dtdd_cpu != -1UL && dd->dtdd_cpu != cpu) 947 return (DTRACE_CONSUME_NEXT); 948 949 if (dd->dtdd_heading == 0) { 950 if (!dd->dtdd_flowindent) { 951 if (!dd->dtdd_quiet) { 952 mdb_printf("%3s %6s %32s\n", 953 "CPU", "ID", "FUNCTION:NAME"); 954 } 955 } else { 956 mdb_printf("%3s %-41s\n", "CPU", "FUNCTION"); 957 } 958 dd->dtdd_heading = 1; 959 } 960 961 if (!dd->dtdd_flowindent) { 962 if (!dd->dtdd_quiet) { 963 (void) mdb_snprintf(name, sizeof (name), "%s:%s", 964 pd->dtpd_func, pd->dtpd_name); 965 966 mdb_printf("%3d %6d %32s ", cpu, pd->dtpd_id, name); 967 } 968 } else { 969 int indent = data->dtpda_indent; 970 971 if (data->dtpda_flow == DTRACEFLOW_NONE) { 972 (void) mdb_snprintf(name, sizeof (name), "%*s%s%s:%s", 973 indent, "", data->dtpda_prefix, pd->dtpd_func, 974 pd->dtpd_name); 975 } else { 976 (void) mdb_snprintf(name, sizeof (name), "%*s%s%s", 977 indent, "", data->dtpda_prefix, pd->dtpd_func); 978 } 979 980 mdb_printf("%3d %-41s ", cpu, name); 981 } 982 983 return (DTRACE_CONSUME_THIS); 984 } 985 986 /*ARGSUSED*/ 987 static int 988 dtrace_dcmderr(const dtrace_errdata_t *data, void *arg) 989 { 990 mdb_warn(data->dteda_msg); 991 return (DTRACE_HANDLE_OK); 992 } 993 994 /*ARGSUSED*/ 995 static int 996 dtrace_dcmddrop(const dtrace_dropdata_t *data, void *arg) 997 { 998 mdb_warn(data->dtdda_msg); 999 return (DTRACE_HANDLE_OK); 1000 } 1001 1002 /*ARGSUSED*/ 1003 static int 1004 dtrace_dcmdbuffered(const dtrace_bufdata_t *bufdata, void *arg) 1005 { 1006 mdb_printf("%s", bufdata->dtbda_buffered); 1007 return (DTRACE_HANDLE_OK); 1008 } 1009 1010 /*ARGSUSED*/ 1011 int 1012 dtrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1013 { 1014 dtrace_state_t state; 1015 dtrace_hdl_t *dtp; 1016 int ncpu, err; 1017 uintptr_t c = -1UL; 1018 dtrace_dcmddata_t dd; 1019 dtrace_optval_t val; 1020 dtracemdb_data_t md; 1021 int rval = DCMD_ERR; 1022 dtrace_anon_t anon; 1023 1024 if (!(flags & DCMD_ADDRSPEC)) 1025 return (DCMD_USAGE); 1026 1027 if (mdb_getopts(argc, argv, 'c', MDB_OPT_UINTPTR, &c, NULL) != argc) 1028 return (DCMD_USAGE); 1029 1030 if (mdb_readvar(&ncpu, "_ncpu") == -1) { 1031 mdb_warn("failed to read '_ncpu'"); 1032 return (DCMD_ERR); 1033 } 1034 1035 if (mdb_vread(&state, sizeof (state), addr) == -1) { 1036 mdb_warn("couldn't read dtrace_state_t at %p", addr); 1037 return (DCMD_ERR); 1038 } 1039 1040 if (state.dts_anon != NULL) { 1041 addr = (uintptr_t)state.dts_anon; 1042 1043 if (mdb_vread(&state, sizeof (state), addr) == -1) { 1044 mdb_warn("couldn't read anonymous state at %p", addr); 1045 return (DCMD_ERR); 1046 } 1047 } 1048 1049 bzero(&md, sizeof (md)); 1050 md.dtmd_state = &state; 1051 1052 if ((dtp = dtrace_vopen(DTRACE_VERSION, DTRACE_O_NOSYS, &err, 1053 &dtrace_mdbops, &md)) == NULL) { 1054 mdb_warn("failed to initialize dtrace: %s\n", 1055 dtrace_errmsg(NULL, err)); 1056 return (DCMD_ERR); 1057 } 1058 1059 /* 1060 * If this is the anonymous enabling, we need to set a bit indicating 1061 * that DTRACEOPT_GRABANON should be set. 1062 */ 1063 if (mdb_readvar(&anon, "dtrace_anon") == -1) { 1064 mdb_warn("failed to read 'dtrace_anon'"); 1065 return (DCMD_ERR); 1066 } 1067 1068 md.dtmd_isanon = ((uintptr_t)anon.dta_state == addr); 1069 1070 if (dtrace_go(dtp) != 0) { 1071 mdb_warn("failed to initialize dtrace: %s\n", 1072 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1073 goto err; 1074 } 1075 1076 bzero(&dd, sizeof (dd)); 1077 dd.dtdd_dtp = dtp; 1078 dd.dtdd_cpu = c; 1079 1080 if (dtrace_getopt(dtp, "flowindent", &val) == -1) { 1081 mdb_warn("couldn't get 'flowindent' option: %s\n", 1082 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1083 goto err; 1084 } 1085 1086 dd.dtdd_flowindent = (val != DTRACEOPT_UNSET); 1087 1088 if (dtrace_getopt(dtp, "quiet", &val) == -1) { 1089 mdb_warn("couldn't get 'quiet' option: %s\n", 1090 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1091 goto err; 1092 } 1093 1094 dd.dtdd_quiet = (val != DTRACEOPT_UNSET); 1095 1096 if (dtrace_handle_err(dtp, dtrace_dcmderr, NULL) == -1) { 1097 mdb_warn("couldn't add err handler: %s\n", 1098 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1099 goto err; 1100 } 1101 1102 if (dtrace_handle_drop(dtp, dtrace_dcmddrop, NULL) == -1) { 1103 mdb_warn("couldn't add drop handler: %s\n", 1104 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1105 goto err; 1106 } 1107 1108 if (dtrace_handle_buffered(dtp, dtrace_dcmdbuffered, NULL) == -1) { 1109 mdb_warn("couldn't add buffered handler: %s\n", 1110 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1111 goto err; 1112 } 1113 1114 if (dtrace_status(dtp) == -1) { 1115 mdb_warn("couldn't get status: %s\n", 1116 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1117 goto err; 1118 } 1119 1120 if (dtrace_aggregate_snap(dtp) == -1) { 1121 mdb_warn("couldn't snapshot aggregation: %s\n", 1122 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1123 goto err; 1124 } 1125 1126 if ((dd.dtdd_output = tmpfile()) == NULL) { 1127 mdb_warn("couldn't open DTrace output file: %d\n", errno); 1128 goto err; 1129 } 1130 1131 if (dtrace_consume(dtp, dd.dtdd_output, 1132 dtrace_dcmdprobe, dtrace_dcmdrec, &dd) == -1) { 1133 mdb_warn("couldn't consume DTrace buffers: %s\n", 1134 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1135 } 1136 1137 if (dtrace_aggregate_print(dtp, NULL, NULL) == -1) { 1138 mdb_warn("couldn't print aggregation: %s\n", 1139 dtrace_errmsg(dtp, dtrace_errno(dtp))); 1140 goto err; 1141 } 1142 1143 rval = DCMD_OK; 1144 err: 1145 dtrace_close(dtp); 1146 fclose(dd.dtdd_output); 1147 return (rval); 1148 } 1149 1150 static int 1151 dtrace_errhash_cmp(const void *l, const void *r) 1152 { 1153 uintptr_t lhs = *((uintptr_t *)l); 1154 uintptr_t rhs = *((uintptr_t *)r); 1155 dtrace_errhash_t lerr, rerr; 1156 char lmsg[256], rmsg[256]; 1157 1158 (void) mdb_vread(&lerr, sizeof (lerr), lhs); 1159 (void) mdb_vread(&rerr, sizeof (rerr), rhs); 1160 1161 if (lerr.dter_msg == NULL) 1162 return (-1); 1163 1164 if (rerr.dter_msg == NULL) 1165 return (1); 1166 1167 (void) mdb_readstr(lmsg, sizeof (lmsg), (uintptr_t)lerr.dter_msg); 1168 (void) mdb_readstr(rmsg, sizeof (rmsg), (uintptr_t)rerr.dter_msg); 1169 1170 return (strcmp(lmsg, rmsg)); 1171 } 1172 1173 int 1174 dtrace_errhash_init(mdb_walk_state_t *wsp) 1175 { 1176 GElf_Sym sym; 1177 uintptr_t *hash, addr; 1178 int i; 1179 1180 if (wsp->walk_addr != NULL) { 1181 mdb_warn("dtrace_errhash walk only supports global walks\n"); 1182 return (WALK_ERR); 1183 } 1184 1185 if (mdb_lookup_by_name("dtrace_errhash", &sym) == -1) { 1186 mdb_warn("couldn't find 'dtrace_errhash' (non-DEBUG kernel?)"); 1187 return (WALK_ERR); 1188 } 1189 1190 addr = (uintptr_t)sym.st_value; 1191 hash = mdb_alloc(DTRACE_ERRHASHSZ * sizeof (uintptr_t), 1192 UM_SLEEP | UM_GC); 1193 1194 for (i = 0; i < DTRACE_ERRHASHSZ; i++) 1195 hash[i] = addr + i * sizeof (dtrace_errhash_t); 1196 1197 qsort(hash, DTRACE_ERRHASHSZ, sizeof (uintptr_t), dtrace_errhash_cmp); 1198 1199 wsp->walk_addr = 0; 1200 wsp->walk_data = hash; 1201 1202 return (WALK_NEXT); 1203 } 1204 1205 int 1206 dtrace_errhash_step(mdb_walk_state_t *wsp) 1207 { 1208 int ndx = (int)wsp->walk_addr; 1209 uintptr_t *hash = wsp->walk_data; 1210 dtrace_errhash_t err; 1211 uintptr_t addr; 1212 1213 if (ndx >= DTRACE_ERRHASHSZ) 1214 return (WALK_DONE); 1215 1216 wsp->walk_addr = ndx + 1; 1217 addr = hash[ndx]; 1218 1219 if (mdb_vread(&err, sizeof (err), addr) == -1) { 1220 mdb_warn("failed to read dtrace_errhash_t at %p", addr); 1221 return (WALK_DONE); 1222 } 1223 1224 if (err.dter_msg == NULL) 1225 return (WALK_NEXT); 1226 1227 return (wsp->walk_callback(addr, &err, wsp->walk_cbdata)); 1228 } 1229 1230 /*ARGSUSED*/ 1231 int 1232 dtrace_errhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1233 { 1234 dtrace_errhash_t err; 1235 char msg[256]; 1236 1237 if (!(flags & DCMD_ADDRSPEC)) { 1238 if (mdb_walk_dcmd("dtrace_errhash", "dtrace_errhash", 1239 argc, argv) == -1) { 1240 mdb_warn("can't walk 'dtrace_errhash'"); 1241 return (DCMD_ERR); 1242 } 1243 1244 return (DCMD_OK); 1245 } 1246 1247 if (DCMD_HDRSPEC(flags)) 1248 mdb_printf("%8s %s\n", "COUNT", "ERROR"); 1249 1250 if (mdb_vread(&err, sizeof (err), addr) == -1) { 1251 mdb_warn("failed to read dtrace_errhash_t at %p", addr); 1252 return (DCMD_ERR); 1253 } 1254 1255 addr = (uintptr_t)err.dter_msg; 1256 1257 if (mdb_readstr(msg, sizeof (msg), addr) == -1) { 1258 mdb_warn("failed to read error msg at %p", addr); 1259 return (DCMD_ERR); 1260 } 1261 1262 mdb_printf("%8d %s", err.dter_count, msg); 1263 1264 /* 1265 * Some error messages include a newline -- only print the newline 1266 * if the message doesn't have one. 1267 */ 1268 if (msg[strlen(msg) - 1] != '\n') 1269 mdb_printf("\n"); 1270 1271 return (DCMD_OK); 1272 } 1273 1274 int 1275 dtrace_helptrace_init(mdb_walk_state_t *wsp) 1276 { 1277 uint32_t next; 1278 uintptr_t buffer; 1279 1280 if (wsp->walk_addr != NULL) { 1281 mdb_warn("dtrace_helptrace only supports global walks\n"); 1282 return (WALK_ERR); 1283 } 1284 1285 if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) { 1286 mdb_warn("couldn't read 'dtrace_helptrace_buffer'"); 1287 return (WALK_ERR); 1288 } 1289 1290 if (buffer == NULL) { 1291 mdb_warn("helper tracing is not enabled\n"); 1292 return (WALK_ERR); 1293 } 1294 1295 if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) { 1296 mdb_warn("couldn't read 'dtrace_helptrace_next'"); 1297 return (WALK_ERR); 1298 } 1299 1300 wsp->walk_addr = next; 1301 1302 return (WALK_NEXT); 1303 } 1304 1305 int 1306 dtrace_helptrace_step(mdb_walk_state_t *wsp) 1307 { 1308 uint32_t next, size, nlocals, bufsize; 1309 uintptr_t buffer, addr; 1310 dtrace_helptrace_t *ht; 1311 int rval; 1312 1313 if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) { 1314 mdb_warn("couldn't read 'dtrace_helptrace_next'"); 1315 return (WALK_ERR); 1316 } 1317 1318 if (mdb_readvar(&bufsize, "dtrace_helptrace_bufsize") == -1) { 1319 mdb_warn("couldn't read 'dtrace_helptrace_bufsize'"); 1320 return (WALK_ERR); 1321 } 1322 1323 if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) { 1324 mdb_warn("couldn't read 'dtrace_helptrace_buffer'"); 1325 return (WALK_ERR); 1326 } 1327 1328 if (mdb_readvar(&nlocals, "dtrace_helptrace_nlocals") == -1) { 1329 mdb_warn("couldn't read 'dtrace_helptrace_nlocals'"); 1330 return (WALK_ERR); 1331 } 1332 1333 size = sizeof (dtrace_helptrace_t) + 1334 nlocals * sizeof (uint64_t) - sizeof (uint64_t); 1335 1336 if (wsp->walk_addr + size > bufsize) { 1337 if (next == 0) 1338 return (WALK_DONE); 1339 1340 wsp->walk_addr = 0; 1341 } 1342 1343 addr = buffer + wsp->walk_addr; 1344 ht = alloca(size); 1345 1346 if (mdb_vread(ht, size, addr) == -1) { 1347 mdb_warn("couldn't read entry at %p", addr); 1348 return (WALK_ERR); 1349 } 1350 1351 if (ht->dtht_helper != NULL) { 1352 rval = wsp->walk_callback(addr, ht, wsp->walk_cbdata); 1353 1354 if (rval != WALK_NEXT) 1355 return (rval); 1356 } 1357 1358 if (wsp->walk_addr < next && wsp->walk_addr + size >= next) 1359 return (WALK_DONE); 1360 1361 wsp->walk_addr += size; 1362 return (WALK_NEXT); 1363 } 1364 1365 int 1366 dtrace_helptrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1367 { 1368 dtrace_helptrace_t help; 1369 dtrace_helper_action_t helper; 1370 char where[30]; 1371 uint_t opt_v = FALSE; 1372 uintptr_t haddr; 1373 1374 if (!(flags & DCMD_ADDRSPEC)) { 1375 if (mdb_walk_dcmd("dtrace_helptrace", "dtrace_helptrace", 1376 argc, argv) == -1) { 1377 mdb_warn("can't walk 'dtrace_helptrace'"); 1378 return (DCMD_ERR); 1379 } 1380 1381 return (DCMD_OK); 1382 } 1383 1384 if (mdb_getopts(argc, argv, 'v', 1385 MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc) 1386 return (DCMD_USAGE); 1387 1388 if (DCMD_HDRSPEC(flags)) { 1389 mdb_printf(" %?s %?s %12s %s\n", 1390 "ADDR", "HELPER", "WHERE", "DIFO"); 1391 } 1392 1393 if (mdb_vread(&help, sizeof (help), addr) == -1) { 1394 mdb_warn("failed to read dtrace_helptrace_t at %p", addr); 1395 return (DCMD_ERR); 1396 } 1397 1398 switch (help.dtht_where) { 1399 case 0: 1400 (void) mdb_snprintf(where, sizeof (where), "predicate"); 1401 break; 1402 1403 case DTRACE_HELPTRACE_NEXT: 1404 (void) mdb_snprintf(where, sizeof (where), "next"); 1405 break; 1406 1407 case DTRACE_HELPTRACE_DONE: 1408 (void) mdb_snprintf(where, sizeof (where), "done"); 1409 break; 1410 1411 case DTRACE_HELPTRACE_ERR: 1412 (void) mdb_snprintf(where, sizeof (where), "err"); 1413 break; 1414 1415 default: 1416 (void) mdb_snprintf(where, sizeof (where), 1417 "action #%d", help.dtht_where); 1418 break; 1419 } 1420 1421 mdb_printf(" %?p %?p %12s ", addr, help.dtht_helper, where); 1422 1423 haddr = (uintptr_t)help.dtht_helper; 1424 1425 if (mdb_vread(&helper, sizeof (helper), haddr) == -1) { 1426 /* 1427 * We're not going to warn in this case -- we're just not going 1428 * to print anything exciting. 1429 */ 1430 mdb_printf("???\n"); 1431 } else { 1432 switch (help.dtht_where) { 1433 case 0: 1434 mdb_printf("%p\n", helper.dtha_predicate); 1435 break; 1436 1437 case DTRACE_HELPTRACE_NEXT: 1438 case DTRACE_HELPTRACE_DONE: 1439 case DTRACE_HELPTRACE_ERR: 1440 mdb_printf("-\n"); 1441 break; 1442 1443 default: 1444 haddr = (uintptr_t)helper.dtha_actions + 1445 (help.dtht_where - 1) * sizeof (uintptr_t); 1446 1447 if (mdb_vread(&haddr, sizeof (haddr), haddr) == -1) { 1448 mdb_printf("???\n"); 1449 } else { 1450 mdb_printf("%p\n", haddr); 1451 } 1452 } 1453 } 1454 1455 if (opt_v) { 1456 int i; 1457 1458 if (help.dtht_where == DTRACE_HELPTRACE_ERR) { 1459 int f = help.dtht_fault; 1460 1461 mdb_printf("%?s| %?s %10s |\n", "", "", ""); 1462 mdb_printf("%?s| %?s %10s +-> fault: %s\n", "", "", "", 1463 f == DTRACEFLT_BADADDR ? "BADADDR" : 1464 f == DTRACEFLT_BADALIGN ? "BADALIGN" : 1465 f == DTRACEFLT_ILLOP ? "ILLOP" : 1466 f == DTRACEFLT_DIVZERO ? "DIVZERO" : 1467 f == DTRACEFLT_NOSCRATCH ? "NOSCRATCH" : 1468 f == DTRACEFLT_KPRIV ? "KPRIV" : 1469 f == DTRACEFLT_UPRIV ? "UPRIV" : 1470 f == DTRACEFLT_TUPOFLOW ? "TUPOFLOW" : 1471 f == DTRACEFLT_BADSTACK ? "BADSTACK" : 1472 "DTRACEFLT_UNKNOWN"); 1473 mdb_printf("%?s| %?s %12s addr: 0x%x\n", "", "", "", 1474 help.dtht_illval); 1475 mdb_printf("%?s| %?s %12s offset: %d\n", "", "", "", 1476 help.dtht_fltoffs); 1477 } 1478 1479 mdb_printf("%?s|\n%?s+--> %?s %4s %s\n", "", "", 1480 "ADDR", "NDX", "VALUE"); 1481 addr += sizeof (help) - sizeof (uint64_t); 1482 1483 for (i = 0; i < help.dtht_nlocals; i++) { 1484 uint64_t val; 1485 1486 if (mdb_vread(&val, sizeof (val), addr) == -1) { 1487 mdb_warn("couldn't read local at %p", addr); 1488 continue; 1489 } 1490 1491 mdb_printf("%?s %?p %4d %p\n", "", addr, i, val); 1492 addr += sizeof (uint64_t); 1493 } 1494 1495 mdb_printf("\n"); 1496 } 1497 1498 return (DCMD_OK); 1499 } 1500 1501 /*ARGSUSED*/ 1502 static int 1503 dtrace_state_walk(uintptr_t addr, const vmem_seg_t *seg, minor_t *highest) 1504 { 1505 if (seg->vs_end > *highest) 1506 *highest = seg->vs_end; 1507 1508 return (WALK_NEXT); 1509 } 1510 1511 typedef struct dtrace_state_walk { 1512 uintptr_t dtsw_softstate; 1513 minor_t dtsw_max; 1514 minor_t dtsw_current; 1515 } dtrace_state_walk_t; 1516 1517 int 1518 dtrace_state_init(mdb_walk_state_t *wsp) 1519 { 1520 uintptr_t dtrace_minor; 1521 minor_t max = 0; 1522 dtrace_state_walk_t *dw; 1523 1524 if (wsp->walk_addr != NULL) { 1525 mdb_warn("dtrace_state only supports global walks\n"); 1526 return (WALK_ERR); 1527 } 1528 1529 /* 1530 * Find the dtrace_minor vmem arena and walk it to get the maximum 1531 * minor number. 1532 */ 1533 if (mdb_readvar(&dtrace_minor, "dtrace_minor") == -1) { 1534 mdb_warn("failed to read 'dtrace_minor'"); 1535 return (WALK_ERR); 1536 } 1537 1538 if (mdb_pwalk("vmem_alloc", (mdb_walk_cb_t)dtrace_state_walk, 1539 &max, dtrace_minor) == -1) { 1540 mdb_warn("couldn't walk 'vmem_alloc'"); 1541 return (WALK_ERR); 1542 } 1543 1544 dw = mdb_zalloc(sizeof (dtrace_state_walk_t), UM_SLEEP | UM_GC); 1545 dw->dtsw_current = 0; 1546 dw->dtsw_max = max; 1547 1548 if (mdb_readvar(&dw->dtsw_softstate, "dtrace_softstate") == -1) { 1549 mdb_warn("failed to read 'dtrace_softstate'"); 1550 return (DCMD_ERR); 1551 } 1552 1553 wsp->walk_data = dw; 1554 1555 return (WALK_NEXT); 1556 } 1557 1558 int 1559 dtrace_state_step(mdb_walk_state_t *wsp) 1560 { 1561 dtrace_state_walk_t *dw = wsp->walk_data; 1562 uintptr_t statep; 1563 dtrace_state_t state; 1564 int rval; 1565 1566 while (mdb_get_soft_state_byaddr(dw->dtsw_softstate, dw->dtsw_current, 1567 &statep, NULL, 0) == -1) { 1568 if (dw->dtsw_current >= dw->dtsw_max) 1569 return (WALK_DONE); 1570 1571 dw->dtsw_current++; 1572 } 1573 1574 if (mdb_vread(&state, sizeof (state), statep) == -1) { 1575 mdb_warn("couldn't read dtrace_state_t at %p", statep); 1576 return (WALK_NEXT); 1577 } 1578 1579 rval = wsp->walk_callback(statep, &state, wsp->walk_cbdata); 1580 dw->dtsw_current++; 1581 1582 return (rval); 1583 } 1584 1585 typedef struct dtrace_state_data { 1586 int dtsd_major; 1587 uintptr_t dtsd_proc; 1588 uintptr_t dtsd_softstate; 1589 uintptr_t dtsd_state; 1590 } dtrace_state_data_t; 1591 1592 static int 1593 dtrace_state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data) 1594 { 1595 vnode_t vnode; 1596 proc_t proc; 1597 minor_t minor; 1598 uintptr_t statep; 1599 1600 if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) { 1601 mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode); 1602 return (WALK_NEXT); 1603 } 1604 1605 if (getmajor(vnode.v_rdev) != data->dtsd_major) 1606 return (WALK_NEXT); 1607 1608 minor = getminor(vnode.v_rdev); 1609 1610 if (mdb_vread(&proc, sizeof (proc), data->dtsd_proc) == -1) { 1611 mdb_warn("failed to read proc at %p", data->dtsd_proc); 1612 return (WALK_NEXT); 1613 } 1614 1615 if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor, 1616 &statep, NULL, 0) == -1) { 1617 mdb_warn("failed to read softstate for minor %d", minor); 1618 return (WALK_NEXT); 1619 } 1620 1621 if (statep != data->dtsd_state) 1622 return (WALK_NEXT); 1623 1624 mdb_printf("%?p %5d %?p %-*s %?p\n", statep, minor, 1625 data->dtsd_proc, MAXCOMLEN, proc.p_user.u_comm, addr); 1626 1627 return (WALK_NEXT); 1628 } 1629 1630 /*ARGSUSED*/ 1631 static int 1632 dtrace_state_proc(uintptr_t addr, void *ignored, dtrace_state_data_t *data) 1633 { 1634 data->dtsd_proc = addr; 1635 1636 if (mdb_pwalk("file", 1637 (mdb_walk_cb_t)dtrace_state_file, data, addr) == -1) { 1638 mdb_warn("couldn't walk 'file' for proc %p", addr); 1639 return (WALK_ERR); 1640 } 1641 1642 return (WALK_NEXT); 1643 } 1644 1645 void 1646 dtrace_state_help(void) 1647 { 1648 mdb_printf("Given a dtrace_state_t structure, displays all " 1649 /*CSTYLED*/ 1650 "consumers, or \"<anonymous>\"\nif the consumer is anonymous. If " 1651 "no state structure is provided, iterates\nover all state " 1652 "structures.\n\n" 1653 "Addresses in ADDR column may be provided to ::dtrace to obtain\n" 1654 "dtrace(1M)-like output for in-kernel DTrace data.\n"); 1655 } 1656 1657 int 1658 dtrace_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1659 { 1660 uintptr_t devi; 1661 struct dev_info info; 1662 dtrace_state_data_t data; 1663 dtrace_anon_t anon; 1664 dtrace_state_t state; 1665 1666 if (!(flags & DCMD_ADDRSPEC)) { 1667 if (mdb_walk_dcmd("dtrace_state", 1668 "dtrace_state", argc, argv) == -1) { 1669 mdb_warn("can't walk dtrace_state"); 1670 return (DCMD_ERR); 1671 } 1672 return (DCMD_OK); 1673 } 1674 1675 if (DCMD_HDRSPEC(flags)) { 1676 mdb_printf("%?s %5s %?s %-*s %?s\n", "ADDR", "MINOR", "PROC", 1677 MAXCOMLEN, "NAME", "FILE"); 1678 } 1679 1680 /* 1681 * First determine if this is anonymous state. 1682 */ 1683 if (mdb_readvar(&anon, "dtrace_anon") == -1) { 1684 mdb_warn("failed to read 'dtrace_anon'"); 1685 return (DCMD_ERR); 1686 } 1687 1688 if ((uintptr_t)anon.dta_state == addr) { 1689 if (mdb_vread(&state, sizeof (state), addr) == -1) { 1690 mdb_warn("failed to read anon at %p", addr); 1691 return (DCMD_ERR); 1692 } 1693 1694 mdb_printf("%?p %5d %?s %-*s %?s\n", addr, 1695 getminor(state.dts_dev), "-", MAXCOMLEN, 1696 "<anonymous>", "-"); 1697 1698 return (DCMD_OK); 1699 } 1700 1701 if (mdb_readvar(&devi, "dtrace_devi") == -1) { 1702 mdb_warn("failed to read 'dtrace_devi'"); 1703 return (DCMD_ERR); 1704 } 1705 1706 if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) { 1707 mdb_warn("failed to read 'dev_info'"); 1708 return (DCMD_ERR); 1709 } 1710 1711 data.dtsd_major = info.devi_major; 1712 1713 if (mdb_readvar(&data.dtsd_softstate, "dtrace_softstate") == -1) { 1714 mdb_warn("failed to read 'dtrace_softstate'"); 1715 return (DCMD_ERR); 1716 } 1717 1718 data.dtsd_state = addr; 1719 1720 /* 1721 * Walk through all processes and all open files looking for this 1722 * state. It must be open somewhere... 1723 */ 1724 if (mdb_walk("proc", (mdb_walk_cb_t)dtrace_state_proc, &data) == -1) { 1725 mdb_warn("couldn't walk 'proc'"); 1726 return (DCMD_ERR); 1727 } 1728 1729 return (DCMD_OK); 1730 } 1731 1732 typedef struct dtrace_aggkey_data { 1733 uintptr_t *dtakd_hash; 1734 uintptr_t dtakd_hashsize; 1735 uintptr_t dtakd_next; 1736 uintptr_t dtakd_ndx; 1737 } dtrace_aggkey_data_t; 1738 1739 int 1740 dtrace_aggkey_init(mdb_walk_state_t *wsp) 1741 { 1742 dtrace_buffer_t buf; 1743 uintptr_t addr; 1744 dtrace_aggbuffer_t agb; 1745 dtrace_aggkey_data_t *data; 1746 size_t hsize; 1747 1748 if ((addr = wsp->walk_addr) == NULL) { 1749 mdb_warn("dtrace_aggkey walk needs aggregation buffer\n"); 1750 return (WALK_ERR); 1751 } 1752 1753 if (mdb_vread(&buf, sizeof (buf), addr) == -1) { 1754 mdb_warn("failed to read aggregation buffer at %p", addr); 1755 return (WALK_ERR); 1756 } 1757 1758 addr = (uintptr_t)buf.dtb_tomax + 1759 buf.dtb_size - sizeof (dtrace_aggbuffer_t); 1760 1761 if (mdb_vread(&agb, sizeof (agb), addr) == -1) { 1762 mdb_warn("failed to read dtrace_aggbuffer_t at %p", addr); 1763 return (WALK_ERR); 1764 } 1765 1766 data = mdb_zalloc(sizeof (dtrace_aggkey_data_t), UM_SLEEP); 1767 1768 data->dtakd_hashsize = agb.dtagb_hashsize; 1769 hsize = agb.dtagb_hashsize * sizeof (dtrace_aggkey_t *); 1770 data->dtakd_hash = mdb_alloc(hsize, UM_SLEEP); 1771 1772 if (mdb_vread(data->dtakd_hash, hsize, 1773 (uintptr_t)agb.dtagb_hash) == -1) { 1774 mdb_warn("failed to read hash at %p", 1775 (uintptr_t)agb.dtagb_hash); 1776 mdb_free(data->dtakd_hash, hsize); 1777 mdb_free(data, sizeof (dtrace_aggkey_data_t)); 1778 return (WALK_ERR); 1779 } 1780 1781 wsp->walk_data = data; 1782 return (WALK_NEXT); 1783 } 1784 1785 int 1786 dtrace_aggkey_step(mdb_walk_state_t *wsp) 1787 { 1788 dtrace_aggkey_data_t *data = wsp->walk_data; 1789 dtrace_aggkey_t key; 1790 uintptr_t addr; 1791 1792 while ((addr = data->dtakd_next) == NULL) { 1793 if (data->dtakd_ndx == data->dtakd_hashsize) 1794 return (WALK_DONE); 1795 1796 data->dtakd_next = data->dtakd_hash[data->dtakd_ndx++]; 1797 } 1798 1799 if (mdb_vread(&key, sizeof (key), addr) == -1) { 1800 mdb_warn("failed to read dtrace_aggkey_t at %p", addr); 1801 return (WALK_ERR); 1802 } 1803 1804 data->dtakd_next = (uintptr_t)key.dtak_next; 1805 1806 return (wsp->walk_callback(addr, &key, wsp->walk_cbdata)); 1807 } 1808 1809 void 1810 dtrace_aggkey_fini(mdb_walk_state_t *wsp) 1811 { 1812 dtrace_aggkey_data_t *data = wsp->walk_data; 1813 size_t hsize; 1814 1815 hsize = data->dtakd_hashsize * sizeof (dtrace_aggkey_t *); 1816 mdb_free(data->dtakd_hash, hsize); 1817 mdb_free(data, sizeof (dtrace_aggkey_data_t)); 1818 } 1819 1820 typedef struct dtrace_dynvar_data { 1821 dtrace_dynhash_t *dtdvd_hash; 1822 uintptr_t dtdvd_hashsize; 1823 uintptr_t dtdvd_next; 1824 uintptr_t dtdvd_ndx; 1825 uintptr_t dtdvd_sink; 1826 } dtrace_dynvar_data_t; 1827 1828 int 1829 dtrace_dynvar_init(mdb_walk_state_t *wsp) 1830 { 1831 uintptr_t addr; 1832 dtrace_dstate_t dstate; 1833 dtrace_dynvar_data_t *data; 1834 size_t hsize; 1835 GElf_Sym sym; 1836 1837 if ((addr = wsp->walk_addr) == NULL) { 1838 mdb_warn("dtrace_dynvar walk needs dtrace_dstate_t\n"); 1839 return (WALK_ERR); 1840 } 1841 1842 if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) { 1843 mdb_warn("failed to read dynamic state at %p", addr); 1844 return (WALK_ERR); 1845 } 1846 1847 if (mdb_lookup_by_name("dtrace_dynhash_sink", &sym) == -1) { 1848 mdb_warn("couldn't find 'dtrace_dynhash_sink'"); 1849 return (WALK_ERR); 1850 } 1851 1852 data = mdb_zalloc(sizeof (dtrace_dynvar_data_t), UM_SLEEP); 1853 1854 data->dtdvd_hashsize = dstate.dtds_hashsize; 1855 hsize = dstate.dtds_hashsize * sizeof (dtrace_dynhash_t); 1856 data->dtdvd_hash = mdb_alloc(hsize, UM_SLEEP); 1857 data->dtdvd_sink = (uintptr_t)sym.st_value; 1858 1859 if (mdb_vread(data->dtdvd_hash, hsize, 1860 (uintptr_t)dstate.dtds_hash) == -1) { 1861 mdb_warn("failed to read hash at %p", 1862 (uintptr_t)dstate.dtds_hash); 1863 mdb_free(data->dtdvd_hash, hsize); 1864 mdb_free(data, sizeof (dtrace_dynvar_data_t)); 1865 return (WALK_ERR); 1866 } 1867 1868 data->dtdvd_next = (uintptr_t)data->dtdvd_hash[0].dtdh_chain; 1869 1870 wsp->walk_data = data; 1871 return (WALK_NEXT); 1872 } 1873 1874 int 1875 dtrace_dynvar_step(mdb_walk_state_t *wsp) 1876 { 1877 dtrace_dynvar_data_t *data = wsp->walk_data; 1878 dtrace_dynvar_t dynvar, *dvar; 1879 size_t dvarsize; 1880 uintptr_t addr; 1881 int nkeys; 1882 1883 while ((addr = data->dtdvd_next) == data->dtdvd_sink) { 1884 if (data->dtdvd_ndx == data->dtdvd_hashsize) 1885 return (WALK_DONE); 1886 1887 data->dtdvd_next = 1888 (uintptr_t)data->dtdvd_hash[data->dtdvd_ndx++].dtdh_chain; 1889 } 1890 1891 if (mdb_vread(&dynvar, sizeof (dynvar), addr) == -1) { 1892 mdb_warn("failed to read dtrace_dynvar_t at %p", addr); 1893 return (WALK_ERR); 1894 } 1895 1896 /* 1897 * Now we need to allocate the correct size. 1898 */ 1899 nkeys = dynvar.dtdv_tuple.dtt_nkeys; 1900 dvarsize = (uintptr_t)&dynvar.dtdv_tuple.dtt_key[nkeys] - 1901 (uintptr_t)&dynvar; 1902 1903 dvar = alloca(dvarsize); 1904 1905 if (mdb_vread(dvar, dvarsize, addr) == -1) { 1906 mdb_warn("failed to read dtrace_dynvar_t at %p", addr); 1907 return (WALK_ERR); 1908 } 1909 1910 data->dtdvd_next = (uintptr_t)dynvar.dtdv_next; 1911 1912 return (wsp->walk_callback(addr, dvar, wsp->walk_cbdata)); 1913 } 1914 1915 void 1916 dtrace_dynvar_fini(mdb_walk_state_t *wsp) 1917 { 1918 dtrace_dynvar_data_t *data = wsp->walk_data; 1919 size_t hsize; 1920 1921 hsize = data->dtdvd_hashsize * sizeof (dtrace_dynvar_t *); 1922 mdb_free(data->dtdvd_hash, hsize); 1923 mdb_free(data, sizeof (dtrace_dynvar_data_t)); 1924 } 1925 1926 typedef struct dtrace_hashstat_data { 1927 size_t *dthsd_counts; 1928 size_t dthsd_hashsize; 1929 char *dthsd_data; 1930 size_t dthsd_size; 1931 int dthsd_header; 1932 } dtrace_hashstat_data_t; 1933 1934 typedef void (*dtrace_hashstat_func_t)(dtrace_hashstat_data_t *); 1935 1936 static void 1937 dtrace_hashstat_additive(dtrace_hashstat_data_t *data) 1938 { 1939 int i; 1940 int hval = 0; 1941 1942 for (i = 0; i < data->dthsd_size; i++) 1943 hval += data->dthsd_data[i]; 1944 1945 data->dthsd_counts[hval % data->dthsd_hashsize]++; 1946 } 1947 1948 static void 1949 dtrace_hashstat_shifty(dtrace_hashstat_data_t *data) 1950 { 1951 uint64_t hval = 0; 1952 int i; 1953 1954 if (data->dthsd_size < sizeof (uint64_t)) { 1955 dtrace_hashstat_additive(data); 1956 return; 1957 } 1958 1959 for (i = 0; i < data->dthsd_size; i += sizeof (uint64_t)) { 1960 /* LINTED - alignment */ 1961 uint64_t val = *((uint64_t *)&data->dthsd_data[i]); 1962 1963 hval += (val & ((1 << NBBY) - 1)) + 1964 ((val >> NBBY) & ((1 << NBBY) - 1)) + 1965 ((val >> (NBBY << 1)) & ((1 << NBBY) - 1)) + 1966 ((val >> (NBBY << 2)) & ((1 << NBBY) - 1)) + 1967 (val & USHRT_MAX) + (val >> (NBBY << 1) & USHRT_MAX); 1968 } 1969 1970 data->dthsd_counts[hval % data->dthsd_hashsize]++; 1971 } 1972 1973 static void 1974 dtrace_hashstat_knuth(dtrace_hashstat_data_t *data) 1975 { 1976 int i; 1977 int hval = data->dthsd_size; 1978 1979 for (i = 0; i < data->dthsd_size; i++) 1980 hval = (hval << 4) ^ (hval >> 28) ^ data->dthsd_data[i]; 1981 1982 data->dthsd_counts[hval % data->dthsd_hashsize]++; 1983 } 1984 1985 static void 1986 dtrace_hashstat_oneatatime(dtrace_hashstat_data_t *data) 1987 { 1988 int i; 1989 uint32_t hval = 0; 1990 1991 for (i = 0; i < data->dthsd_size; i++) { 1992 hval += data->dthsd_data[i]; 1993 hval += (hval << 10); 1994 hval ^= (hval >> 6); 1995 } 1996 1997 hval += (hval << 3); 1998 hval ^= (hval >> 11); 1999 hval += (hval << 15); 2000 2001 data->dthsd_counts[hval % data->dthsd_hashsize]++; 2002 } 2003 2004 static void 2005 dtrace_hashstat_fnv(dtrace_hashstat_data_t *data) 2006 { 2007 static const uint32_t prime = 0x01000193; 2008 uint32_t hval = 0; 2009 int i; 2010 2011 for (i = 0; i < data->dthsd_size; i++) { 2012 hval *= prime; 2013 hval ^= data->dthsd_data[i]; 2014 } 2015 2016 data->dthsd_counts[hval % data->dthsd_hashsize]++; 2017 } 2018 2019 static void 2020 dtrace_hashstat_stats(char *name, dtrace_hashstat_data_t *data) 2021 { 2022 size_t nz = 0, i; 2023 int longest = 0; 2024 size_t ttl = 0; 2025 double sum = 0.0; 2026 double avg; 2027 uint_t util, stddev; 2028 2029 if (!data->dthsd_header) { 2030 mdb_printf("%15s %11s %11s %11s %11s %11s\n", "NAME", 2031 "HASHSIZE", "%UTIL", "LONGEST", "AVERAGE", "STDDEV"); 2032 data->dthsd_header = 1; 2033 } 2034 2035 for (i = 0; i < data->dthsd_hashsize; i++) { 2036 if (data->dthsd_counts[i] != 0) { 2037 nz++; 2038 2039 if (data->dthsd_counts[i] > longest) 2040 longest = data->dthsd_counts[i]; 2041 2042 ttl += data->dthsd_counts[i]; 2043 } 2044 } 2045 2046 if (nz == 0) { 2047 mdb_printf("%15s %11d %11s %11s %11s %11s\n", name, 2048 data->dthsd_hashsize, "-", "-", "-", "-"); 2049 return; 2050 } 2051 2052 avg = (double)ttl / (double)nz; 2053 2054 for (i = 0; i < data->dthsd_hashsize; i++) { 2055 double delta = (double)data->dthsd_counts[i] - avg; 2056 2057 if (data->dthsd_counts[i] == 0) 2058 continue; 2059 2060 sum += delta * delta; 2061 } 2062 2063 util = (nz * 1000) / data->dthsd_hashsize; 2064 stddev = (uint_t)sqrt(sum / (double)nz) * 10; 2065 2066 mdb_printf("%15s %11d %9u.%1u %11d %11d %9u.%1u\n", name, 2067 data->dthsd_hashsize, util / 10, util % 10, longest, ttl / nz, 2068 stddev / 10, stddev % 10); 2069 } 2070 2071 static struct dtrace_hashstat { 2072 char *dths_name; 2073 dtrace_hashstat_func_t dths_func; 2074 } _dtrace_hashstat[] = { 2075 { "<actual>", NULL }, 2076 { "additive", dtrace_hashstat_additive }, 2077 { "shifty", dtrace_hashstat_shifty }, 2078 { "knuth", dtrace_hashstat_knuth }, 2079 { "one-at-a-time", dtrace_hashstat_oneatatime }, 2080 { "fnv", dtrace_hashstat_fnv }, 2081 { NULL, 0 } 2082 }; 2083 2084 typedef struct dtrace_aggstat_data { 2085 dtrace_hashstat_data_t dtagsd_hash; 2086 dtrace_hashstat_func_t dtagsd_func; 2087 } dtrace_aggstat_data_t; 2088 2089 static int 2090 dtrace_aggstat_walk(uintptr_t addr, dtrace_aggkey_t *key, 2091 dtrace_aggstat_data_t *data) 2092 { 2093 dtrace_hashstat_data_t *hdata = &data->dtagsd_hash; 2094 size_t size; 2095 2096 if (data->dtagsd_func == NULL) { 2097 size_t bucket = key->dtak_hashval % hdata->dthsd_hashsize; 2098 2099 hdata->dthsd_counts[bucket]++; 2100 return (WALK_NEXT); 2101 } 2102 2103 /* 2104 * We need to read the data. 2105 */ 2106 size = key->dtak_size - sizeof (dtrace_aggid_t); 2107 addr = (uintptr_t)key->dtak_data + sizeof (dtrace_aggid_t); 2108 hdata->dthsd_data = alloca(size); 2109 hdata->dthsd_size = size; 2110 2111 if (mdb_vread(hdata->dthsd_data, size, addr) == -1) { 2112 mdb_warn("couldn't read data at %p", addr); 2113 return (WALK_ERR); 2114 } 2115 2116 data->dtagsd_func(hdata); 2117 2118 return (WALK_NEXT); 2119 } 2120 2121 /*ARGSUSED*/ 2122 int 2123 dtrace_aggstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2124 { 2125 dtrace_buffer_t buf; 2126 uintptr_t aaddr; 2127 dtrace_aggbuffer_t agb; 2128 size_t hsize, i, actual, prime, evenpow; 2129 dtrace_aggstat_data_t data; 2130 dtrace_hashstat_data_t *hdata = &data.dtagsd_hash; 2131 2132 bzero(&data, sizeof (data)); 2133 2134 if (!(flags & DCMD_ADDRSPEC)) 2135 return (DCMD_USAGE); 2136 2137 if (mdb_vread(&buf, sizeof (buf), addr) == -1) { 2138 mdb_warn("failed to read aggregation buffer at %p", addr); 2139 return (DCMD_ERR); 2140 } 2141 2142 aaddr = (uintptr_t)buf.dtb_tomax + 2143 buf.dtb_size - sizeof (dtrace_aggbuffer_t); 2144 2145 if (mdb_vread(&agb, sizeof (agb), aaddr) == -1) { 2146 mdb_warn("failed to read dtrace_aggbuffer_t at %p", aaddr); 2147 return (DCMD_ERR); 2148 } 2149 2150 hsize = (actual = agb.dtagb_hashsize) * sizeof (size_t); 2151 hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC); 2152 2153 /* 2154 * Now pick the largest prime smaller than the hash size. (If the 2155 * existing size is prime, we'll pick a smaller prime just for the 2156 * hell of it.) 2157 */ 2158 for (prime = agb.dtagb_hashsize - 1; prime > 7; prime--) { 2159 size_t limit = prime / 7; 2160 2161 for (i = 2; i < limit; i++) { 2162 if ((prime % i) == 0) 2163 break; 2164 } 2165 2166 if (i == limit) 2167 break; 2168 } 2169 2170 /* 2171 * And now we want to pick the largest power of two smaller than the 2172 * hashsize. 2173 */ 2174 for (i = 0; (1 << i) < agb.dtagb_hashsize; i++) 2175 continue; 2176 2177 evenpow = (1 << (i - 1)); 2178 2179 for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) { 2180 data.dtagsd_func = _dtrace_hashstat[i].dths_func; 2181 2182 hdata->dthsd_hashsize = actual; 2183 hsize = hdata->dthsd_hashsize * sizeof (size_t); 2184 bzero(hdata->dthsd_counts, hsize); 2185 2186 if (mdb_pwalk("dtrace_aggkey", 2187 (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) { 2188 mdb_warn("failed to walk dtrace_aggkey at %p", addr); 2189 return (DCMD_ERR); 2190 } 2191 2192 dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata); 2193 2194 /* 2195 * If we were just printing the actual value, we won't try 2196 * any of the sizing experiments. 2197 */ 2198 if (data.dtagsd_func == NULL) 2199 continue; 2200 2201 hdata->dthsd_hashsize = prime; 2202 hsize = hdata->dthsd_hashsize * sizeof (size_t); 2203 bzero(hdata->dthsd_counts, hsize); 2204 2205 if (mdb_pwalk("dtrace_aggkey", 2206 (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) { 2207 mdb_warn("failed to walk dtrace_aggkey at %p", addr); 2208 return (DCMD_ERR); 2209 } 2210 2211 dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata); 2212 2213 hdata->dthsd_hashsize = evenpow; 2214 hsize = hdata->dthsd_hashsize * sizeof (size_t); 2215 bzero(hdata->dthsd_counts, hsize); 2216 2217 if (mdb_pwalk("dtrace_aggkey", 2218 (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) { 2219 mdb_warn("failed to walk dtrace_aggkey at %p", addr); 2220 return (DCMD_ERR); 2221 } 2222 2223 dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata); 2224 } 2225 2226 return (DCMD_OK); 2227 } 2228 2229 /*ARGSUSED*/ 2230 static int 2231 dtrace_dynstat_walk(uintptr_t addr, dtrace_dynvar_t *dynvar, 2232 dtrace_aggstat_data_t *data) 2233 { 2234 dtrace_hashstat_data_t *hdata = &data->dtagsd_hash; 2235 dtrace_tuple_t *tuple = &dynvar->dtdv_tuple; 2236 dtrace_key_t *key = tuple->dtt_key; 2237 size_t size = 0, offs = 0; 2238 int i, nkeys = tuple->dtt_nkeys; 2239 char *buf; 2240 2241 if (data->dtagsd_func == NULL) { 2242 size_t bucket = dynvar->dtdv_hashval % hdata->dthsd_hashsize; 2243 2244 hdata->dthsd_counts[bucket]++; 2245 return (WALK_NEXT); 2246 } 2247 2248 /* 2249 * We want to hand the hashing algorithm a contiguous buffer. First 2250 * run through the tuple and determine the size. 2251 */ 2252 for (i = 0; i < nkeys; i++) { 2253 if (key[i].dttk_size == 0) { 2254 size += sizeof (uint64_t); 2255 } else { 2256 size += key[i].dttk_size; 2257 } 2258 } 2259 2260 buf = alloca(size); 2261 2262 /* 2263 * Now go back through the tuple and copy the data into the buffer. 2264 */ 2265 for (i = 0; i < nkeys; i++) { 2266 if (key[i].dttk_size == 0) { 2267 bcopy(&key[i].dttk_value, &buf[offs], 2268 sizeof (uint64_t)); 2269 offs += sizeof (uint64_t); 2270 } else { 2271 if (mdb_vread(&buf[offs], key[i].dttk_size, 2272 key[i].dttk_value) == -1) { 2273 mdb_warn("couldn't read tuple data at %p", 2274 key[i].dttk_value); 2275 return (WALK_ERR); 2276 } 2277 2278 offs += key[i].dttk_size; 2279 } 2280 } 2281 2282 hdata->dthsd_data = buf; 2283 hdata->dthsd_size = size; 2284 2285 data->dtagsd_func(hdata); 2286 2287 return (WALK_NEXT); 2288 } 2289 2290 /*ARGSUSED*/ 2291 int 2292 dtrace_dynstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2293 { 2294 dtrace_dstate_t dstate; 2295 size_t hsize, i, actual, prime; 2296 dtrace_aggstat_data_t data; 2297 dtrace_hashstat_data_t *hdata = &data.dtagsd_hash; 2298 2299 bzero(&data, sizeof (data)); 2300 2301 if (!(flags & DCMD_ADDRSPEC)) 2302 return (DCMD_USAGE); 2303 2304 if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) { 2305 mdb_warn("failed to read dynamic variable state at %p", addr); 2306 return (DCMD_ERR); 2307 } 2308 2309 hsize = (actual = dstate.dtds_hashsize) * sizeof (size_t); 2310 hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC); 2311 2312 /* 2313 * Now pick the largest prime smaller than the hash size. (If the 2314 * existing size is prime, we'll pick a smaller prime just for the 2315 * hell of it.) 2316 */ 2317 for (prime = dstate.dtds_hashsize - 1; prime > 7; prime--) { 2318 size_t limit = prime / 7; 2319 2320 for (i = 2; i < limit; i++) { 2321 if ((prime % i) == 0) 2322 break; 2323 } 2324 2325 if (i == limit) 2326 break; 2327 } 2328 2329 for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) { 2330 data.dtagsd_func = _dtrace_hashstat[i].dths_func; 2331 2332 hdata->dthsd_hashsize = actual; 2333 hsize = hdata->dthsd_hashsize * sizeof (size_t); 2334 bzero(hdata->dthsd_counts, hsize); 2335 2336 if (mdb_pwalk("dtrace_dynvar", 2337 (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) { 2338 mdb_warn("failed to walk dtrace_dynvar at %p", addr); 2339 return (DCMD_ERR); 2340 } 2341 2342 dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata); 2343 2344 /* 2345 * If we were just printing the actual value, we won't try 2346 * any of the sizing experiments. 2347 */ 2348 if (data.dtagsd_func == NULL) 2349 continue; 2350 2351 hdata->dthsd_hashsize = prime; 2352 hsize = hdata->dthsd_hashsize * sizeof (size_t); 2353 bzero(hdata->dthsd_counts, hsize); 2354 2355 if (mdb_pwalk("dtrace_dynvar", 2356 (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) { 2357 mdb_warn("failed to walk dtrace_aggkey at %p", addr); 2358 return (DCMD_ERR); 2359 } 2360 2361 dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata); 2362 } 2363 2364 return (DCMD_OK); 2365 } 2366 2367 typedef struct dtrace_ecb_walk { 2368 dtrace_ecb_t **dtew_ecbs; 2369 int dtew_necbs; 2370 int dtew_curecb; 2371 } dtrace_ecb_walk_t; 2372 2373 static int 2374 dtrace_ecb_init(mdb_walk_state_t *wsp) 2375 { 2376 uintptr_t addr; 2377 dtrace_state_t state; 2378 dtrace_ecb_walk_t *ecbwp; 2379 2380 if ((addr = wsp->walk_addr) == NULL) { 2381 mdb_warn("dtrace_ecb walk needs dtrace_state_t\n"); 2382 return (WALK_ERR); 2383 } 2384 2385 if (mdb_vread(&state, sizeof (state), addr) == -1) { 2386 mdb_warn("failed to read dtrace state pointer at %p", addr); 2387 return (WALK_ERR); 2388 } 2389 2390 ecbwp = mdb_zalloc(sizeof (dtrace_ecb_walk_t), UM_SLEEP | UM_GC); 2391 2392 ecbwp->dtew_ecbs = state.dts_ecbs; 2393 ecbwp->dtew_necbs = state.dts_necbs; 2394 ecbwp->dtew_curecb = 0; 2395 2396 wsp->walk_data = ecbwp; 2397 2398 return (WALK_NEXT); 2399 } 2400 2401 static int 2402 dtrace_ecb_step(mdb_walk_state_t *wsp) 2403 { 2404 uintptr_t ecbp, addr; 2405 dtrace_ecb_walk_t *ecbwp = wsp->walk_data; 2406 2407 addr = (uintptr_t)ecbwp->dtew_ecbs + 2408 ecbwp->dtew_curecb * sizeof (dtrace_ecb_t *); 2409 2410 if (ecbwp->dtew_curecb++ == ecbwp->dtew_necbs) 2411 return (WALK_DONE); 2412 2413 if (mdb_vread(&ecbp, sizeof (addr), addr) == -1) { 2414 mdb_warn("failed to read ecb at entry %d\n", 2415 ecbwp->dtew_curecb); 2416 return (WALK_ERR); 2417 } 2418 2419 if (ecbp == NULL) 2420 return (WALK_NEXT); 2421 2422 return (wsp->walk_callback(ecbp, NULL, wsp->walk_cbdata)); 2423 } 2424 2425 static void 2426 dtrace_options_numtostr(uint64_t num, char *buf, size_t len) 2427 { 2428 uint64_t n = num; 2429 int index = 0; 2430 char u; 2431 2432 while (n >= 1024) { 2433 n = (n + (1024 / 2)) / 1024; /* Round up or down */ 2434 index++; 2435 } 2436 2437 u = " KMGTPE"[index]; 2438 2439 if (index == 0) { 2440 (void) mdb_snprintf(buf, len, "%llu", (u_longlong_t)n); 2441 } else if (n < 10 && (num & (num - 1)) != 0) { 2442 (void) mdb_snprintf(buf, len, "%.2f%c", 2443 (double)num / (1ULL << 10 * index), u); 2444 } else if (n < 100 && (num & (num - 1)) != 0) { 2445 (void) mdb_snprintf(buf, len, "%.1f%c", 2446 (double)num / (1ULL << 10 * index), u); 2447 } else { 2448 (void) mdb_snprintf(buf, len, "%llu%c", (u_longlong_t)n, u); 2449 } 2450 } 2451 2452 static void 2453 dtrace_options_numtohz(uint64_t num, char *buf, size_t len) 2454 { 2455 (void) mdb_snprintf(buf, len, "%dhz", NANOSEC/num); 2456 } 2457 2458 static void 2459 dtrace_options_numtobufpolicy(uint64_t num, char *buf, size_t len) 2460 { 2461 char *policy = "unknown"; 2462 2463 switch (num) { 2464 case DTRACEOPT_BUFPOLICY_RING: 2465 policy = "ring"; 2466 break; 2467 2468 case DTRACEOPT_BUFPOLICY_FILL: 2469 policy = "fill"; 2470 break; 2471 2472 case DTRACEOPT_BUFPOLICY_SWITCH: 2473 policy = "switch"; 2474 break; 2475 } 2476 2477 (void) mdb_snprintf(buf, len, "%s", policy); 2478 } 2479 2480 static void 2481 dtrace_options_numtocpu(uint64_t cpu, char *buf, size_t len) 2482 { 2483 if (cpu == DTRACE_CPUALL) 2484 (void) mdb_snprintf(buf, len, "%7s", "unbound"); 2485 else 2486 (void) mdb_snprintf(buf, len, "%d", cpu); 2487 } 2488 2489 typedef void (*dtrace_options_func_t)(uint64_t, char *, size_t); 2490 2491 static struct dtrace_options { 2492 char *dtop_optstr; 2493 dtrace_options_func_t dtop_func; 2494 } _dtrace_options[] = { 2495 { "bufsize", dtrace_options_numtostr }, 2496 { "bufpolicy", dtrace_options_numtobufpolicy }, 2497 { "dynvarsize", dtrace_options_numtostr }, 2498 { "aggsize", dtrace_options_numtostr }, 2499 { "specsize", dtrace_options_numtostr }, 2500 { "nspec", dtrace_options_numtostr }, 2501 { "strsize", dtrace_options_numtostr }, 2502 { "cleanrate", dtrace_options_numtohz }, 2503 { "cpu", dtrace_options_numtocpu }, 2504 { "bufresize", dtrace_options_numtostr }, 2505 { "grabanon", dtrace_options_numtostr }, 2506 { "flowindent", dtrace_options_numtostr }, 2507 { "quiet", dtrace_options_numtostr }, 2508 { "stackframes", dtrace_options_numtostr }, 2509 { "ustackframes", dtrace_options_numtostr }, 2510 { "aggrate", dtrace_options_numtohz }, 2511 { "switchrate", dtrace_options_numtohz }, 2512 { "statusrate", dtrace_options_numtohz }, 2513 { "destructive", dtrace_options_numtostr }, 2514 { "stackindent", dtrace_options_numtostr }, 2515 { "rawbytes", dtrace_options_numtostr }, 2516 { "jstackframes", dtrace_options_numtostr }, 2517 { "jstackstrsize", dtrace_options_numtostr }, 2518 { "aggsortkey", dtrace_options_numtostr }, 2519 { "aggsortrev", dtrace_options_numtostr }, 2520 { "aggsortpos", dtrace_options_numtostr }, 2521 { "aggsortkeypos", dtrace_options_numtostr }, 2522 { "temporal", dtrace_options_numtostr }, 2523 { "agghist", dtrace_options_numtostr }, 2524 { "aggpack", dtrace_options_numtostr }, 2525 { "aggzoom", dtrace_options_numtostr }, 2526 { "zone", dtrace_options_numtostr } 2527 }; 2528 2529 CTASSERT(ARRAY_SIZE(_dtrace_options) == DTRACEOPT_MAX); 2530 2531 static void 2532 dtrace_options_help(void) 2533 { 2534 mdb_printf("Given a dtrace_state_t structure, displays the " 2535 "current tunable option\nsettings.\n"); 2536 } 2537 2538 /*ARGSUSED*/ 2539 static int 2540 dtrace_options(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2541 { 2542 dtrace_state_t state; 2543 int i = 0; 2544 dtrace_optval_t *options; 2545 char val[32]; 2546 2547 if (!(flags & DCMD_ADDRSPEC)) 2548 return (DCMD_USAGE); 2549 2550 if (mdb_vread(&state, sizeof (dtrace_state_t), (uintptr_t)addr) == -1) { 2551 mdb_warn("failed to read state pointer at %p\n", addr); 2552 return (DCMD_ERR); 2553 } 2554 2555 options = &state.dts_options[0]; 2556 2557 mdb_printf("%<u>%-25s %s%</u>\n", "OPTION", "VALUE"); 2558 for (i = 0; i < DTRACEOPT_MAX; i++) { 2559 if (options[i] == DTRACEOPT_UNSET) { 2560 mdb_printf("%-25s %s\n", 2561 _dtrace_options[i].dtop_optstr, "UNSET"); 2562 } else { 2563 (void) _dtrace_options[i].dtop_func(options[i], 2564 val, 32); 2565 mdb_printf("%-25s %s\n", 2566 _dtrace_options[i].dtop_optstr, val); 2567 } 2568 } 2569 2570 return (DCMD_OK); 2571 } 2572 2573 static int 2574 pid2state_init(mdb_walk_state_t *wsp) 2575 { 2576 dtrace_state_data_t *data; 2577 uintptr_t devi; 2578 uintptr_t proc; 2579 struct dev_info info; 2580 pid_t pid = (pid_t)wsp->walk_addr; 2581 2582 if (wsp->walk_addr == NULL) { 2583 mdb_warn("pid2state walk requires PID\n"); 2584 return (WALK_ERR); 2585 } 2586 2587 data = mdb_zalloc(sizeof (dtrace_state_data_t), UM_SLEEP | UM_GC); 2588 2589 if (mdb_readvar(&data->dtsd_softstate, "dtrace_softstate") == -1) { 2590 mdb_warn("failed to read 'dtrace_softstate'"); 2591 return (DCMD_ERR); 2592 } 2593 2594 if ((proc = mdb_pid2proc(pid, NULL)) == 0) { 2595 mdb_warn("PID 0t%d not found\n", pid); 2596 return (DCMD_ERR); 2597 } 2598 2599 if (mdb_readvar(&devi, "dtrace_devi") == -1) { 2600 mdb_warn("failed to read 'dtrace_devi'"); 2601 return (DCMD_ERR); 2602 } 2603 2604 if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) { 2605 mdb_warn("failed to read 'dev_info'"); 2606 return (DCMD_ERR); 2607 } 2608 2609 data->dtsd_major = info.devi_major; 2610 data->dtsd_proc = proc; 2611 2612 wsp->walk_data = data; 2613 2614 return (WALK_NEXT); 2615 } 2616 2617 /*ARGSUSED*/ 2618 static int 2619 pid2state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data) 2620 { 2621 vnode_t vnode; 2622 minor_t minor; 2623 uintptr_t statep; 2624 2625 /* Get the vnode for this file */ 2626 if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) { 2627 mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode); 2628 return (WALK_NEXT); 2629 } 2630 2631 2632 /* Is this the dtrace device? */ 2633 if (getmajor(vnode.v_rdev) != data->dtsd_major) 2634 return (WALK_NEXT); 2635 2636 /* Get the minor number for this device entry */ 2637 minor = getminor(vnode.v_rdev); 2638 2639 if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor, 2640 &statep, NULL, 0) == -1) { 2641 mdb_warn("failed to read softstate for minor %d", minor); 2642 return (WALK_NEXT); 2643 } 2644 2645 mdb_printf("%p\n", statep); 2646 2647 return (WALK_NEXT); 2648 } 2649 2650 static int 2651 pid2state_step(mdb_walk_state_t *wsp) 2652 { 2653 dtrace_state_data_t *ds = wsp->walk_data; 2654 2655 if (mdb_pwalk("file", 2656 (mdb_walk_cb_t)pid2state_file, ds, ds->dtsd_proc) == -1) { 2657 mdb_warn("couldn't walk 'file' for proc %p", ds->dtsd_proc); 2658 return (WALK_ERR); 2659 } 2660 2661 return (WALK_DONE); 2662 } 2663 2664 /*ARGSUSED*/ 2665 static int 2666 dtrace_probes_walk(uintptr_t addr, void *ignored, uintptr_t *target) 2667 { 2668 dtrace_ecb_t ecb; 2669 dtrace_probe_t probe; 2670 dtrace_probedesc_t pd; 2671 2672 if (addr == NULL) 2673 return (WALK_ERR); 2674 2675 if (mdb_vread(&ecb, sizeof (dtrace_ecb_t), addr) == -1) { 2676 mdb_warn("failed to read ecb %p\n", addr); 2677 return (WALK_ERR); 2678 } 2679 2680 if (ecb.dte_probe == NULL) 2681 return (WALK_ERR); 2682 2683 if (mdb_vread(&probe, sizeof (dtrace_probe_t), 2684 (uintptr_t)ecb.dte_probe) == -1) { 2685 mdb_warn("failed to read probe %p\n", ecb.dte_probe); 2686 return (WALK_ERR); 2687 } 2688 2689 pd.dtpd_id = probe.dtpr_id; 2690 dtracemdb_probe(NULL, &pd); 2691 2692 mdb_printf("%5d %10s %17s %33s %s\n", pd.dtpd_id, pd.dtpd_provider, 2693 pd.dtpd_mod, pd.dtpd_func, pd.dtpd_name); 2694 2695 return (WALK_NEXT); 2696 } 2697 2698 static void 2699 dtrace_probes_help(void) 2700 { 2701 mdb_printf("Given a dtrace_state_t structure, displays all " 2702 "its active enablings. If no\nstate structure is provided, " 2703 "all available probes are listed.\n"); 2704 } 2705 2706 /*ARGSUSED*/ 2707 static int 2708 dtrace_probes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2709 { 2710 dtrace_probedesc_t pd; 2711 uintptr_t caddr, base, paddr; 2712 int nprobes, i; 2713 2714 mdb_printf("%5s %10s %17s %33s %s\n", 2715 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME"); 2716 2717 if (!(flags & DCMD_ADDRSPEC)) { 2718 /* 2719 * If no argument is provided just display all available 2720 * probes. 2721 */ 2722 if (mdb_readvar(&base, "dtrace_probes") == -1) { 2723 mdb_warn("failed to read 'dtrace_probes'"); 2724 return (-1); 2725 } 2726 2727 if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) { 2728 mdb_warn("failed to read 'dtrace_nprobes'"); 2729 return (-1); 2730 } 2731 2732 for (i = 0; i < nprobes; i++) { 2733 caddr = base + i * sizeof (dtrace_probe_t *); 2734 2735 if (mdb_vread(&paddr, sizeof (paddr), caddr) == -1) { 2736 mdb_warn("couldn't read probe pointer at %p", 2737 caddr); 2738 continue; 2739 } 2740 2741 if (paddr == NULL) 2742 continue; 2743 2744 pd.dtpd_id = i + 1; 2745 if (dtracemdb_probe(NULL, &pd) == 0) { 2746 mdb_printf("%5d %10s %17s %33s %s\n", 2747 pd.dtpd_id, pd.dtpd_provider, 2748 pd.dtpd_mod, pd.dtpd_func, pd.dtpd_name); 2749 } 2750 } 2751 } else { 2752 if (mdb_pwalk("dtrace_ecb", (mdb_walk_cb_t)dtrace_probes_walk, 2753 NULL, addr) == -1) { 2754 mdb_warn("couldn't walk 'dtrace_ecb'"); 2755 return (DCMD_ERR); 2756 } 2757 } 2758 2759 return (DCMD_OK); 2760 } 2761 2762 const mdb_dcmd_t kernel_dcmds[] = { 2763 { "id2probe", ":", "translate a dtrace_id_t to a dtrace_probe_t", 2764 id2probe }, 2765 { "dtrace", ":[-c cpu]", "print dtrace(1M)-like output", 2766 dtrace, dtrace_help }, 2767 { "dtrace_errhash", ":", "print DTrace error hash", dtrace_errhash }, 2768 { "dtrace_helptrace", ":", "print DTrace helper trace", 2769 dtrace_helptrace }, 2770 { "dtrace_state", ":", "print active DTrace consumers", dtrace_state, 2771 dtrace_state_help }, 2772 { "dtrace_aggstat", ":", 2773 "print DTrace aggregation hash statistics", dtrace_aggstat }, 2774 { "dtrace_dynstat", ":", 2775 "print DTrace dynamic variable hash statistics", dtrace_dynstat }, 2776 { "dtrace_options", ":", 2777 "print a DTrace consumer's current tuneable options", 2778 dtrace_options, dtrace_options_help }, 2779 { "dtrace_probes", "?", "print a DTrace consumer's enabled probes", 2780 dtrace_probes, dtrace_probes_help }, 2781 { NULL } 2782 }; 2783 2784 const mdb_walker_t kernel_walkers[] = { 2785 { "dtrace_errhash", "walk hash of DTrace error messasges", 2786 dtrace_errhash_init, dtrace_errhash_step }, 2787 { "dtrace_helptrace", "walk DTrace helper trace entries", 2788 dtrace_helptrace_init, dtrace_helptrace_step }, 2789 { "dtrace_state", "walk DTrace per-consumer softstate", 2790 dtrace_state_init, dtrace_state_step }, 2791 { "dtrace_aggkey", "walk DTrace aggregation keys", 2792 dtrace_aggkey_init, dtrace_aggkey_step, dtrace_aggkey_fini }, 2793 { "dtrace_dynvar", "walk DTrace dynamic variables", 2794 dtrace_dynvar_init, dtrace_dynvar_step, dtrace_dynvar_fini }, 2795 { "dtrace_ecb", "walk a DTrace consumer's enabling control blocks", 2796 dtrace_ecb_init, dtrace_ecb_step }, 2797 { "pid2state", "walk a processes dtrace_state structures", 2798 pid2state_init, pid2state_step }, 2799 { NULL } 2800 }; 2801