10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51710Sahl * Common Development and Distribution License (the "License").
61710Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211710Sahl
220Sstevel@tonic-gate /*
23*12902SBryan.Cantrill@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * explicitly define DTRACE_ERRDEBUG to pull in definition of dtrace_errhash_t
280Sstevel@tonic-gate * explicitly define _STDARG_H to avoid stdarg.h/varargs.h u/k defn conflict
290Sstevel@tonic-gate */
300Sstevel@tonic-gate #define DTRACE_ERRDEBUG
310Sstevel@tonic-gate #define _STDARG_H
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <mdb/mdb_param.h>
340Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
350Sstevel@tonic-gate #include <mdb/mdb_ks.h>
360Sstevel@tonic-gate #include <sys/dtrace_impl.h>
370Sstevel@tonic-gate #include <sys/vmem_impl.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/kobj.h>
410Sstevel@tonic-gate #include <dtrace.h>
420Sstevel@tonic-gate #include <alloca.h>
430Sstevel@tonic-gate #include <ctype.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <math.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*ARGSUSED*/
480Sstevel@tonic-gate int
id2probe(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)490Sstevel@tonic-gate id2probe(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate uintptr_t probe = NULL;
520Sstevel@tonic-gate uintptr_t probes;
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
550Sstevel@tonic-gate return (DCMD_USAGE);
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (addr == DTRACE_IDNONE || addr > UINT32_MAX)
580Sstevel@tonic-gate goto out;
590Sstevel@tonic-gate
600Sstevel@tonic-gate if (mdb_readvar(&probes, "dtrace_probes") == -1) {
610Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_probes'");
620Sstevel@tonic-gate return (DCMD_ERR);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate
650Sstevel@tonic-gate probes += (addr - 1) * sizeof (dtrace_probe_t *);
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (mdb_vread(&probe, sizeof (uintptr_t), probes) == -1) {
680Sstevel@tonic-gate mdb_warn("failed to read dtrace_probes[%d]", addr - 1);
690Sstevel@tonic-gate return (DCMD_ERR);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate
720Sstevel@tonic-gate out:
730Sstevel@tonic-gate mdb_printf("%p\n", probe);
740Sstevel@tonic-gate return (DCMD_OK);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate void
dtrace_help(void)780Sstevel@tonic-gate dtrace_help(void)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate
810Sstevel@tonic-gate mdb_printf("Given a dtrace_state_t structure that represents a "
820Sstevel@tonic-gate "DTrace consumer, prints\n"
830Sstevel@tonic-gate "dtrace(1M)-like output for in-kernel DTrace data. (The "
840Sstevel@tonic-gate "dtrace_state_t\n"
850Sstevel@tonic-gate "structures for all DTrace consumers may be obtained by running "
860Sstevel@tonic-gate "the \n"
870Sstevel@tonic-gate "::dtrace_state dcmd.) When data is present on multiple CPUs, "
880Sstevel@tonic-gate "data are\n"
890Sstevel@tonic-gate "presented in CPU order, with records within each CPU ordered "
900Sstevel@tonic-gate "oldest to \n"
910Sstevel@tonic-gate "youngest. Options:\n\n"
920Sstevel@tonic-gate "-c cpu Only provide output for specified CPU.\n");
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate static int
dtracemdb_eprobe(dtrace_state_t * state,dtrace_eprobedesc_t * epd)960Sstevel@tonic-gate dtracemdb_eprobe(dtrace_state_t *state, dtrace_eprobedesc_t *epd)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate dtrace_epid_t epid = epd->dtepd_epid;
990Sstevel@tonic-gate dtrace_probe_t probe;
1000Sstevel@tonic-gate dtrace_ecb_t ecb;
1010Sstevel@tonic-gate uintptr_t addr, paddr, ap;
1020Sstevel@tonic-gate dtrace_action_t act;
1030Sstevel@tonic-gate int nactions, nrecs;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate addr = (uintptr_t)state->dts_ecbs +
1060Sstevel@tonic-gate (epid - 1) * sizeof (dtrace_ecb_t *);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
1090Sstevel@tonic-gate mdb_warn("failed to read ecb for epid %d", epid);
1100Sstevel@tonic-gate return (-1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (addr == NULL) {
1140Sstevel@tonic-gate mdb_warn("epid %d doesn't match an ecb\n", epid);
1150Sstevel@tonic-gate return (-1);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if (mdb_vread(&ecb, sizeof (ecb), addr) == -1) {
1190Sstevel@tonic-gate mdb_warn("failed to read ecb at %p", addr);
1200Sstevel@tonic-gate return (-1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate paddr = (uintptr_t)ecb.dte_probe;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
1260Sstevel@tonic-gate mdb_warn("failed to read probe for ecb %p", addr);
1270Sstevel@tonic-gate return (-1);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * This is a little painful: in order to find the number of actions,
1320Sstevel@tonic-gate * we need to first walk through them.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) {
1350Sstevel@tonic-gate if (mdb_vread(&act, sizeof (act), ap) == -1) {
1360Sstevel@tonic-gate mdb_warn("failed to read action %p on ecb %p",
1370Sstevel@tonic-gate ap, addr);
1380Sstevel@tonic-gate return (-1);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple)
1420Sstevel@tonic-gate nactions++;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate ap = (uintptr_t)act.dta_next;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate nrecs = epd->dtepd_nrecs;
1480Sstevel@tonic-gate epd->dtepd_nrecs = nactions;
1490Sstevel@tonic-gate epd->dtepd_probeid = probe.dtpr_id;
1500Sstevel@tonic-gate epd->dtepd_uarg = ecb.dte_uarg;
1510Sstevel@tonic-gate epd->dtepd_size = ecb.dte_size;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) {
1540Sstevel@tonic-gate if (mdb_vread(&act, sizeof (act), ap) == -1) {
1550Sstevel@tonic-gate mdb_warn("failed to read action %p on ecb %p",
1560Sstevel@tonic-gate ap, addr);
1570Sstevel@tonic-gate return (-1);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) {
1610Sstevel@tonic-gate if (nrecs-- == 0)
1620Sstevel@tonic-gate break;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate epd->dtepd_rec[nactions++] = act.dta_rec;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate ap = (uintptr_t)act.dta_next;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate return (0);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /*ARGSUSED*/
1740Sstevel@tonic-gate static int
dtracemdb_probe(dtrace_state_t * state,dtrace_probedesc_t * pd)1750Sstevel@tonic-gate dtracemdb_probe(dtrace_state_t *state, dtrace_probedesc_t *pd)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate uintptr_t base, addr, paddr, praddr;
1780Sstevel@tonic-gate int nprobes, i;
1790Sstevel@tonic-gate dtrace_probe_t probe;
1800Sstevel@tonic-gate dtrace_provider_t prov;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (pd->dtpd_id == DTRACE_IDNONE)
1830Sstevel@tonic-gate pd->dtpd_id++;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if (mdb_readvar(&base, "dtrace_probes") == -1) {
1860Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_probes'");
1870Sstevel@tonic-gate return (-1);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) {
1910Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_nprobes'");
1920Sstevel@tonic-gate return (-1);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate for (i = pd->dtpd_id; i <= nprobes; i++) {
1960Sstevel@tonic-gate addr = base + (i - 1) * sizeof (dtrace_probe_t *);
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (mdb_vread(&paddr, sizeof (paddr), addr) == -1) {
1990Sstevel@tonic-gate mdb_warn("couldn't read probe pointer at %p", addr);
2000Sstevel@tonic-gate return (-1);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (paddr != NULL)
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (paddr == NULL) {
2080Sstevel@tonic-gate errno = ESRCH;
2090Sstevel@tonic-gate return (-1);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
2130Sstevel@tonic-gate mdb_warn("couldn't read probe at %p", paddr);
2140Sstevel@tonic-gate return (-1);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate pd->dtpd_id = probe.dtpr_id;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (mdb_vread(pd->dtpd_name, DTRACE_NAMELEN,
2200Sstevel@tonic-gate (uintptr_t)probe.dtpr_name) == -1) {
2210Sstevel@tonic-gate mdb_warn("failed to read probe name for probe %p", paddr);
2220Sstevel@tonic-gate return (-1);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if (mdb_vread(pd->dtpd_func, DTRACE_FUNCNAMELEN,
2260Sstevel@tonic-gate (uintptr_t)probe.dtpr_func) == -1) {
2270Sstevel@tonic-gate mdb_warn("failed to read function name for probe %p", paddr);
2280Sstevel@tonic-gate return (-1);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate if (mdb_vread(pd->dtpd_mod, DTRACE_MODNAMELEN,
2320Sstevel@tonic-gate (uintptr_t)probe.dtpr_mod) == -1) {
2330Sstevel@tonic-gate mdb_warn("failed to read module name for probe %p", paddr);
2340Sstevel@tonic-gate return (-1);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate praddr = (uintptr_t)probe.dtpr_provider;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if (mdb_vread(&prov, sizeof (prov), praddr) == -1) {
2400Sstevel@tonic-gate mdb_warn("failed to read provider for probe %p", paddr);
2410Sstevel@tonic-gate return (-1);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (mdb_vread(pd->dtpd_provider, DTRACE_PROVNAMELEN,
2450Sstevel@tonic-gate (uintptr_t)prov.dtpv_name) == -1) {
2460Sstevel@tonic-gate mdb_warn("failed to read provider name for probe %p", paddr);
2470Sstevel@tonic-gate return (-1);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*ARGSUSED*/
2540Sstevel@tonic-gate static int
dtracemdb_aggdesc(dtrace_state_t * state,dtrace_aggdesc_t * agd)2550Sstevel@tonic-gate dtracemdb_aggdesc(dtrace_state_t *state, dtrace_aggdesc_t *agd)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate dtrace_aggid_t aggid = agd->dtagd_id;
2580Sstevel@tonic-gate dtrace_aggregation_t agg;
2590Sstevel@tonic-gate dtrace_ecb_t ecb;
2600Sstevel@tonic-gate uintptr_t addr, eaddr, ap, last;
2610Sstevel@tonic-gate dtrace_action_t act;
2620Sstevel@tonic-gate dtrace_recdesc_t *lrec;
2630Sstevel@tonic-gate int nactions, nrecs;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate addr = (uintptr_t)state->dts_aggregations +
2660Sstevel@tonic-gate (aggid - 1) * sizeof (dtrace_aggregation_t *);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
2690Sstevel@tonic-gate mdb_warn("failed to read aggregation for aggid %d", aggid);
2700Sstevel@tonic-gate return (-1);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate if (addr == NULL) {
2740Sstevel@tonic-gate mdb_warn("aggid %d doesn't match an aggregation\n", aggid);
2750Sstevel@tonic-gate return (-1);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate if (mdb_vread(&agg, sizeof (agg), addr) == -1) {
2790Sstevel@tonic-gate mdb_warn("failed to read aggregation at %p", addr);
2800Sstevel@tonic-gate return (-1);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate eaddr = (uintptr_t)agg.dtag_ecb;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if (mdb_vread(&ecb, sizeof (ecb), eaddr) == -1) {
2860Sstevel@tonic-gate mdb_warn("failed to read ecb for aggregation %p", addr);
2870Sstevel@tonic-gate return (-1);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate last = (uintptr_t)addr + offsetof(dtrace_aggregation_t, dtag_action);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * This is a little painful: in order to find the number of actions,
2940Sstevel@tonic-gate * we need to first walk through them.
2950Sstevel@tonic-gate */
2960Sstevel@tonic-gate ap = (uintptr_t)agg.dtag_first;
2970Sstevel@tonic-gate nactions = 0;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate for (;;) {
3000Sstevel@tonic-gate if (mdb_vread(&act, sizeof (act), ap) == -1) {
3010Sstevel@tonic-gate mdb_warn("failed to read action %p on aggregation %p",
3020Sstevel@tonic-gate ap, addr);
3030Sstevel@tonic-gate return (-1);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate nactions++;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate if (ap == last)
3090Sstevel@tonic-gate break;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate ap = (uintptr_t)act.dta_next;
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate lrec = &act.dta_rec;
3150Sstevel@tonic-gate agd->dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - agg.dtag_base;
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate nrecs = agd->dtagd_nrecs;
3180Sstevel@tonic-gate agd->dtagd_nrecs = nactions;
3190Sstevel@tonic-gate agd->dtagd_epid = ecb.dte_epid;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate ap = (uintptr_t)agg.dtag_first;
3220Sstevel@tonic-gate nactions = 0;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate for (;;) {
3250Sstevel@tonic-gate dtrace_recdesc_t rec;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate if (mdb_vread(&act, sizeof (act), ap) == -1) {
3280Sstevel@tonic-gate mdb_warn("failed to read action %p on aggregation %p",
3290Sstevel@tonic-gate ap, addr);
3300Sstevel@tonic-gate return (-1);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate if (nrecs-- == 0)
3340Sstevel@tonic-gate break;
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate rec = act.dta_rec;
3370Sstevel@tonic-gate rec.dtrd_offset -= agg.dtag_base;
3380Sstevel@tonic-gate rec.dtrd_uarg = 0;
3390Sstevel@tonic-gate agd->dtagd_rec[nactions++] = rec;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate if (ap == last)
3420Sstevel@tonic-gate break;
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate ap = (uintptr_t)act.dta_next;
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate return (0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate static int
dtracemdb_bufsnap(dtrace_buffer_t * which,dtrace_bufdesc_t * desc)3510Sstevel@tonic-gate dtracemdb_bufsnap(dtrace_buffer_t *which, dtrace_bufdesc_t *desc)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate uintptr_t addr;
3540Sstevel@tonic-gate size_t bufsize;
3550Sstevel@tonic-gate dtrace_buffer_t buf;
3560Sstevel@tonic-gate caddr_t data = desc->dtbd_data;
3570Sstevel@tonic-gate processorid_t max_cpuid, cpu = desc->dtbd_cpu;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
3600Sstevel@tonic-gate mdb_warn("failed to read 'max_cpuid'");
3610Sstevel@tonic-gate errno = EIO;
3620Sstevel@tonic-gate return (-1);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if (cpu < 0 || cpu > max_cpuid) {
3660Sstevel@tonic-gate errno = EINVAL;
3670Sstevel@tonic-gate return (-1);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate addr = (uintptr_t)which + cpu * sizeof (dtrace_buffer_t);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
3730Sstevel@tonic-gate mdb_warn("failed to read buffer description at %p", addr);
3740Sstevel@tonic-gate errno = EIO;
3750Sstevel@tonic-gate return (-1);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (buf.dtb_tomax == NULL) {
3790Sstevel@tonic-gate errno = ENOENT;
3800Sstevel@tonic-gate return (-1);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
3840Sstevel@tonic-gate bufsize = buf.dtb_size;
3850Sstevel@tonic-gate } else {
3860Sstevel@tonic-gate bufsize = buf.dtb_offset;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate if (mdb_vread(data, bufsize, (uintptr_t)buf.dtb_tomax) == -1) {
3900Sstevel@tonic-gate mdb_warn("couldn't read buffer for CPU %d", cpu);
3910Sstevel@tonic-gate errno = EIO;
3920Sstevel@tonic-gate return (-1);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate if (buf.dtb_offset > buf.dtb_size) {
3960Sstevel@tonic-gate mdb_warn("buffer for CPU %d has corrupt offset\n", cpu);
3970Sstevel@tonic-gate errno = EIO;
3980Sstevel@tonic-gate return (-1);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
4020Sstevel@tonic-gate if (buf.dtb_xamot_offset > buf.dtb_size) {
4030Sstevel@tonic-gate mdb_warn("ringbuffer for CPU %d has corrupt "
4040Sstevel@tonic-gate "wrapped offset\n", cpu);
4050Sstevel@tonic-gate errno = EIO;
4060Sstevel@tonic-gate return (-1);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * If the ring buffer has wrapped, it needs to be polished.
4110Sstevel@tonic-gate * See the comment in dtrace_buffer_polish() for details.
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate if (buf.dtb_offset < buf.dtb_xamot_offset) {
4140Sstevel@tonic-gate bzero(data + buf.dtb_offset,
4150Sstevel@tonic-gate buf.dtb_xamot_offset - buf.dtb_offset);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (buf.dtb_offset > buf.dtb_xamot_offset) {
4190Sstevel@tonic-gate bzero(data + buf.dtb_offset,
4200Sstevel@tonic-gate buf.dtb_size - buf.dtb_offset);
4210Sstevel@tonic-gate bzero(data, buf.dtb_xamot_offset);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate desc->dtbd_oldest = buf.dtb_xamot_offset;
4250Sstevel@tonic-gate } else {
4260Sstevel@tonic-gate desc->dtbd_oldest = 0;
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate desc->dtbd_size = bufsize;
4300Sstevel@tonic-gate desc->dtbd_drops = buf.dtb_drops;
4310Sstevel@tonic-gate desc->dtbd_errors = buf.dtb_errors;
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate /*
437*12902SBryan.Cantrill@Sun.COM * This is essentially identical to its cousin in the kernel -- with the
438*12902SBryan.Cantrill@Sun.COM * notable exception that we automatically set DTRACEOPT_GRABANON if this
439*12902SBryan.Cantrill@Sun.COM * state is an anonymous enabling.
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate static dof_hdr_t *
dtracemdb_dof_create(dtrace_state_t * state,int isanon)442*12902SBryan.Cantrill@Sun.COM dtracemdb_dof_create(dtrace_state_t *state, int isanon)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate dof_hdr_t *dof;
4450Sstevel@tonic-gate dof_sec_t *sec;
4460Sstevel@tonic-gate dof_optdesc_t *opt;
4470Sstevel@tonic-gate int i, len = sizeof (dof_hdr_t) +
4480Sstevel@tonic-gate roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
4490Sstevel@tonic-gate sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate dof = mdb_zalloc(len, UM_SLEEP);
4520Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
4530Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
4540Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
4550Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
4580Sstevel@tonic-gate dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
4591710Sahl dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
4600Sstevel@tonic-gate dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
4610Sstevel@tonic-gate dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
4620Sstevel@tonic-gate dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate dof->dofh_flags = 0;
4650Sstevel@tonic-gate dof->dofh_hdrsize = sizeof (dof_hdr_t);
4660Sstevel@tonic-gate dof->dofh_secsize = sizeof (dof_sec_t);
4670Sstevel@tonic-gate dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
4680Sstevel@tonic-gate dof->dofh_secoff = sizeof (dof_hdr_t);
4690Sstevel@tonic-gate dof->dofh_loadsz = len;
4700Sstevel@tonic-gate dof->dofh_filesz = len;
4710Sstevel@tonic-gate dof->dofh_pad = 0;
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * Fill in the option section header...
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
4770Sstevel@tonic-gate sec->dofs_type = DOF_SECT_OPTDESC;
4780Sstevel@tonic-gate sec->dofs_align = sizeof (uint64_t);
4790Sstevel@tonic-gate sec->dofs_flags = DOF_SECF_LOAD;
4800Sstevel@tonic-gate sec->dofs_entsize = sizeof (dof_optdesc_t);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate opt = (dof_optdesc_t *)((uintptr_t)sec +
4830Sstevel@tonic-gate roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
4860Sstevel@tonic-gate sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate for (i = 0; i < DTRACEOPT_MAX; i++) {
4890Sstevel@tonic-gate opt[i].dofo_option = i;
4900Sstevel@tonic-gate opt[i].dofo_strtab = DOF_SECIDX_NONE;
4910Sstevel@tonic-gate opt[i].dofo_value = state->dts_options[i];
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
494*12902SBryan.Cantrill@Sun.COM if (isanon)
495*12902SBryan.Cantrill@Sun.COM opt[DTRACEOPT_GRABANON].dofo_value = 1;
496*12902SBryan.Cantrill@Sun.COM
4970Sstevel@tonic-gate return (dof);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate static int
dtracemdb_format(dtrace_state_t * state,dtrace_fmtdesc_t * desc)5010Sstevel@tonic-gate dtracemdb_format(dtrace_state_t *state, dtrace_fmtdesc_t *desc)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate uintptr_t addr, faddr;
5040Sstevel@tonic-gate char c;
5050Sstevel@tonic-gate int len = 0;
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate if (desc->dtfd_format == 0 || desc->dtfd_format > state->dts_nformats) {
5080Sstevel@tonic-gate errno = EINVAL;
5090Sstevel@tonic-gate return (-1);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate faddr = (uintptr_t)state->dts_formats +
5130Sstevel@tonic-gate (desc->dtfd_format - 1) * sizeof (char *);
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate if (mdb_vread(&addr, sizeof (addr), faddr) == -1) {
5160Sstevel@tonic-gate mdb_warn("failed to read format string pointer at %p", faddr);
5170Sstevel@tonic-gate return (-1);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate do {
5210Sstevel@tonic-gate if (mdb_vread(&c, sizeof (c), addr + len++) == -1) {
5220Sstevel@tonic-gate mdb_warn("failed to read format string at %p", addr);
5230Sstevel@tonic-gate return (-1);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate } while (c != '\0');
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate if (len > desc->dtfd_length) {
5280Sstevel@tonic-gate desc->dtfd_length = len;
5290Sstevel@tonic-gate return (0);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate if (mdb_vread(desc->dtfd_string, len, addr) == -1) {
5330Sstevel@tonic-gate mdb_warn("failed to reread format string at %p", addr);
5340Sstevel@tonic-gate return (-1);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate return (0);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate static int
dtracemdb_status(dtrace_state_t * state,dtrace_status_t * status)5410Sstevel@tonic-gate dtracemdb_status(dtrace_state_t *state, dtrace_status_t *status)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate dtrace_dstate_t *dstate;
5440Sstevel@tonic-gate int i, j;
5450Sstevel@tonic-gate uint64_t nerrs;
5460Sstevel@tonic-gate uintptr_t addr;
5470Sstevel@tonic-gate int ncpu;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate if (mdb_readvar(&ncpu, "_ncpu") == -1) {
5500Sstevel@tonic-gate mdb_warn("failed to read '_ncpu'");
5510Sstevel@tonic-gate return (DCMD_ERR);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate bzero(status, sizeof (dtrace_status_t));
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
5570Sstevel@tonic-gate errno = ENOENT;
5580Sstevel@tonic-gate return (-1);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate * For the MDB backend, we never set dtst_exiting or dtst_filled. This
5630Sstevel@tonic-gate * is by design: we don't want the library to try to stop tracing,
5640Sstevel@tonic-gate * because it doesn't particularly mean anything.
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate nerrs = state->dts_errors;
5670Sstevel@tonic-gate dstate = &state->dts_vstate.dtvs_dynvars;
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate for (i = 0; i < ncpu; i++) {
5700Sstevel@tonic-gate dtrace_dstate_percpu_t dcpu;
5710Sstevel@tonic-gate dtrace_buffer_t buf;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate addr = (uintptr_t)&dstate->dtds_percpu[i];
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate if (mdb_vread(&dcpu, sizeof (dcpu), addr) == -1) {
5760Sstevel@tonic-gate mdb_warn("failed to read per-CPU dstate at %p", addr);
5770Sstevel@tonic-gate return (-1);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate status->dtst_dyndrops += dcpu.dtdsc_drops;
5810Sstevel@tonic-gate status->dtst_dyndrops_dirty += dcpu.dtdsc_dirty_drops;
5820Sstevel@tonic-gate status->dtst_dyndrops_rinsing += dcpu.dtdsc_rinsing_drops;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate addr = (uintptr_t)&state->dts_buffer[i];
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
5870Sstevel@tonic-gate mdb_warn("failed to read per-CPU buffer at %p", addr);
5880Sstevel@tonic-gate return (-1);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate nerrs += buf.dtb_errors;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate for (j = 0; j < state->dts_nspeculations; j++) {
5940Sstevel@tonic-gate dtrace_speculation_t spec;
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate addr = (uintptr_t)&state->dts_speculations[j];
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate if (mdb_vread(&spec, sizeof (spec), addr) == -1) {
5990Sstevel@tonic-gate mdb_warn("failed to read "
6000Sstevel@tonic-gate "speculation at %p", addr);
6010Sstevel@tonic-gate return (-1);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate addr = (uintptr_t)&spec.dtsp_buffer[i];
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
6070Sstevel@tonic-gate mdb_warn("failed to read "
6080Sstevel@tonic-gate "speculative buffer at %p", addr);
6090Sstevel@tonic-gate return (-1);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate status->dtst_specdrops += buf.dtb_xamot_drops;
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate status->dtst_specdrops_busy = state->dts_speculations_busy;
6170Sstevel@tonic-gate status->dtst_specdrops_unavail = state->dts_speculations_unavail;
6180Sstevel@tonic-gate status->dtst_errors = nerrs;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate typedef struct dtracemdb_data {
6240Sstevel@tonic-gate dtrace_state_t *dtmd_state;
6250Sstevel@tonic-gate char *dtmd_symstr;
6260Sstevel@tonic-gate char *dtmd_modstr;
6270Sstevel@tonic-gate uintptr_t dtmd_addr;
628*12902SBryan.Cantrill@Sun.COM int dtmd_isanon;
6290Sstevel@tonic-gate } dtracemdb_data_t;
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate static int
dtracemdb_ioctl(void * varg,int cmd,void * arg)6320Sstevel@tonic-gate dtracemdb_ioctl(void *varg, int cmd, void *arg)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate dtracemdb_data_t *data = varg;
6350Sstevel@tonic-gate dtrace_state_t *state = data->dtmd_state;
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate switch (cmd) {
6380Sstevel@tonic-gate case DTRACEIOC_CONF: {
6390Sstevel@tonic-gate dtrace_conf_t *conf = arg;
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate bzero(conf, sizeof (conf));
6420Sstevel@tonic-gate conf->dtc_difversion = DIF_VERSION;
6430Sstevel@tonic-gate conf->dtc_difintregs = DIF_DIR_NREGS;
6440Sstevel@tonic-gate conf->dtc_diftupregs = DIF_DTR_NREGS;
6450Sstevel@tonic-gate conf->dtc_ctfmodel = CTF_MODEL_NATIVE;
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate return (0);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate case DTRACEIOC_DOFGET: {
6510Sstevel@tonic-gate dof_hdr_t *hdr = arg, *dof;
6520Sstevel@tonic-gate
653*12902SBryan.Cantrill@Sun.COM dof = dtracemdb_dof_create(state, data->dtmd_isanon);
6540Sstevel@tonic-gate bcopy(dof, hdr, MIN(hdr->dofh_loadsz, dof->dofh_loadsz));
6550Sstevel@tonic-gate mdb_free(dof, dof->dofh_loadsz);
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate return (0);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate case DTRACEIOC_BUFSNAP:
6610Sstevel@tonic-gate return (dtracemdb_bufsnap(state->dts_buffer, arg));
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate case DTRACEIOC_AGGSNAP:
6640Sstevel@tonic-gate return (dtracemdb_bufsnap(state->dts_aggbuffer, arg));
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate case DTRACEIOC_AGGDESC:
6670Sstevel@tonic-gate return (dtracemdb_aggdesc(state, arg));
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate case DTRACEIOC_EPROBE:
6700Sstevel@tonic-gate return (dtracemdb_eprobe(state, arg));
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate case DTRACEIOC_PROBES:
6730Sstevel@tonic-gate return (dtracemdb_probe(state, arg));
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate case DTRACEIOC_FORMAT:
6760Sstevel@tonic-gate return (dtracemdb_format(state, arg));
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate case DTRACEIOC_STATUS:
6790Sstevel@tonic-gate return (dtracemdb_status(state, arg));
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate case DTRACEIOC_GO:
6820Sstevel@tonic-gate *(processorid_t *)arg = -1;
6830Sstevel@tonic-gate return (0);
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate case DTRACEIOC_ENABLE:
6860Sstevel@tonic-gate errno = ENOTTY; /* see dt_open.c:dtrace_go() */
6870Sstevel@tonic-gate return (-1);
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate case DTRACEIOC_PROVIDER:
6900Sstevel@tonic-gate case DTRACEIOC_PROBEMATCH:
6910Sstevel@tonic-gate errno = ESRCH;
6920Sstevel@tonic-gate return (-1);
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate default:
6950Sstevel@tonic-gate mdb_warn("unexpected ioctl 0x%x (%s)\n", cmd,
6960Sstevel@tonic-gate cmd == DTRACEIOC_PROVIDER ? "DTRACEIOC_PROVIDER" :
6970Sstevel@tonic-gate cmd == DTRACEIOC_PROBES ? "DTRACEIOC_PROBES" :
6980Sstevel@tonic-gate cmd == DTRACEIOC_BUFSNAP ? "DTRACEIOC_BUFSNAP" :
6990Sstevel@tonic-gate cmd == DTRACEIOC_PROBEMATCH ? "DTRACEIOC_PROBEMATCH" :
7000Sstevel@tonic-gate cmd == DTRACEIOC_ENABLE ? "DTRACEIOC_ENABLE" :
7010Sstevel@tonic-gate cmd == DTRACEIOC_AGGSNAP ? "DTRACEIOC_AGGSNAP" :
7020Sstevel@tonic-gate cmd == DTRACEIOC_EPROBE ? "DTRACEIOC_EPROBE" :
7030Sstevel@tonic-gate cmd == DTRACEIOC_PROBEARG ? "DTRACEIOC_PROBEARG" :
7040Sstevel@tonic-gate cmd == DTRACEIOC_CONF ? "DTRACEIOC_CONF" :
7050Sstevel@tonic-gate cmd == DTRACEIOC_STATUS ? "DTRACEIOC_STATUS" :
7060Sstevel@tonic-gate cmd == DTRACEIOC_GO ? "DTRACEIOC_GO" :
7070Sstevel@tonic-gate cmd == DTRACEIOC_STOP ? "DTRACEIOC_STOP" :
7080Sstevel@tonic-gate cmd == DTRACEIOC_AGGDESC ? "DTRACEIOC_AGGDESC" :
7090Sstevel@tonic-gate cmd == DTRACEIOC_FORMAT ? "DTRACEIOC_FORMAT" :
7100Sstevel@tonic-gate cmd == DTRACEIOC_DOFGET ? "DTRACEIOC_DOFGET" :
7110Sstevel@tonic-gate cmd == DTRACEIOC_REPLICATE ? "DTRACEIOC_REPLICATE" :
7120Sstevel@tonic-gate "???");
7130Sstevel@tonic-gate errno = ENXIO;
7140Sstevel@tonic-gate return (-1);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate static int
dtracemdb_modctl(uintptr_t addr,const struct modctl * m,dtracemdb_data_t * data)7190Sstevel@tonic-gate dtracemdb_modctl(uintptr_t addr, const struct modctl *m, dtracemdb_data_t *data)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate struct module mod;
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate if (m->mod_mp == NULL)
7240Sstevel@tonic-gate return (WALK_NEXT);
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
7270Sstevel@tonic-gate mdb_warn("couldn't read modctl %p's module", addr);
7280Sstevel@tonic-gate return (WALK_NEXT);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate if ((uintptr_t)mod.text > data->dtmd_addr)
7320Sstevel@tonic-gate return (WALK_NEXT);
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate if ((uintptr_t)mod.text + mod.text_size <= data->dtmd_addr)
7350Sstevel@tonic-gate return (WALK_NEXT);
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate if (mdb_readstr(data->dtmd_modstr, MDB_SYM_NAMLEN,
7380Sstevel@tonic-gate (uintptr_t)m->mod_modname) == -1)
7390Sstevel@tonic-gate return (WALK_ERR);
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate return (WALK_DONE);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate static int
dtracemdb_lookup_by_addr(void * varg,GElf_Addr addr,GElf_Sym * symp,dtrace_syminfo_t * sip)7450Sstevel@tonic-gate dtracemdb_lookup_by_addr(void *varg, GElf_Addr addr, GElf_Sym *symp,
7460Sstevel@tonic-gate dtrace_syminfo_t *sip)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate dtracemdb_data_t *data = varg;
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate if (data->dtmd_symstr == NULL) {
7510Sstevel@tonic-gate data->dtmd_symstr = mdb_zalloc(MDB_SYM_NAMLEN,
7520Sstevel@tonic-gate UM_SLEEP | UM_GC);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate if (data->dtmd_modstr == NULL) {
7560Sstevel@tonic-gate data->dtmd_modstr = mdb_zalloc(MDB_SYM_NAMLEN,
7570Sstevel@tonic-gate UM_SLEEP | UM_GC);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate if (symp != NULL) {
7610Sstevel@tonic-gate if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, data->dtmd_symstr,
7620Sstevel@tonic-gate MDB_SYM_NAMLEN, symp) == -1)
7630Sstevel@tonic-gate return (-1);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate if (sip != NULL) {
7670Sstevel@tonic-gate data->dtmd_addr = addr;
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate (void) strcpy(data->dtmd_modstr, "???");
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate if (mdb_walk("modctl",
7720Sstevel@tonic-gate (mdb_walk_cb_t)dtracemdb_modctl, varg) == -1) {
7730Sstevel@tonic-gate mdb_warn("couldn't walk 'modctl'");
7740Sstevel@tonic-gate return (-1);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate sip->dts_object = data->dtmd_modstr;
7780Sstevel@tonic-gate sip->dts_id = 0;
7790Sstevel@tonic-gate sip->dts_name = symp != NULL ? data->dtmd_symstr : NULL;
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate return (0);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /*ARGSUSED*/
7860Sstevel@tonic-gate static int
dtracemdb_stat(void * varg,processorid_t cpu)7870Sstevel@tonic-gate dtracemdb_stat(void *varg, processorid_t cpu)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate GElf_Sym sym;
7900Sstevel@tonic-gate cpu_t c;
7910Sstevel@tonic-gate uintptr_t caddr, addr;
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate if (mdb_lookup_by_name("cpu", &sym) == -1) {
7940Sstevel@tonic-gate mdb_warn("failed to find symbol for 'cpu'");
7950Sstevel@tonic-gate return (-1);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate if (cpu * sizeof (uintptr_t) > sym.st_size)
7990Sstevel@tonic-gate return (-1);
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate addr = (uintptr_t)sym.st_value + cpu * sizeof (uintptr_t);
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate if (mdb_vread(&caddr, sizeof (caddr), addr) == -1) {
8040Sstevel@tonic-gate mdb_warn("failed to read cpu[%d]", cpu);
8050Sstevel@tonic-gate return (-1);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate if (caddr == NULL)
8090Sstevel@tonic-gate return (-1);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate if (mdb_vread(&c, sizeof (c), caddr) == -1) {
8120Sstevel@tonic-gate mdb_warn("failed to read cpu at %p", caddr);
8130Sstevel@tonic-gate return (-1);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate if (c.cpu_flags & CPU_POWEROFF) {
8170Sstevel@tonic-gate return (P_POWEROFF);
8180Sstevel@tonic-gate } else if (c.cpu_flags & CPU_SPARE) {
8190Sstevel@tonic-gate return (P_SPARE);
8200Sstevel@tonic-gate } else if (c.cpu_flags & CPU_FAULTED) {
8210Sstevel@tonic-gate return (P_FAULTED);
8220Sstevel@tonic-gate } else if ((c.cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) {
8230Sstevel@tonic-gate return (P_OFFLINE);
8240Sstevel@tonic-gate } else if (c.cpu_flags & CPU_ENABLE) {
8250Sstevel@tonic-gate return (P_ONLINE);
8260Sstevel@tonic-gate } else {
8270Sstevel@tonic-gate return (P_NOINTR);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate /*ARGSUSED*/
8320Sstevel@tonic-gate static long
dtracemdb_sysconf(void * varg,int name)8330Sstevel@tonic-gate dtracemdb_sysconf(void *varg, int name)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate int max_ncpus;
8360Sstevel@tonic-gate processorid_t max_cpuid;
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate switch (name) {
8390Sstevel@tonic-gate case _SC_CPUID_MAX:
8400Sstevel@tonic-gate if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
8410Sstevel@tonic-gate mdb_warn("failed to read 'max_cpuid'");
8420Sstevel@tonic-gate return (-1);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate return (max_cpuid);
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate case _SC_NPROCESSORS_MAX:
8480Sstevel@tonic-gate if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
8490Sstevel@tonic-gate mdb_warn("failed to read 'max_ncpus'");
8500Sstevel@tonic-gate return (-1);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate return (max_ncpus);
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate default:
8560Sstevel@tonic-gate mdb_warn("unexpected sysconf code %d\n", name);
8570Sstevel@tonic-gate return (-1);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate const dtrace_vector_t dtrace_mdbops = {
8620Sstevel@tonic-gate dtracemdb_ioctl,
8630Sstevel@tonic-gate dtracemdb_lookup_by_addr,
8640Sstevel@tonic-gate dtracemdb_stat,
8650Sstevel@tonic-gate dtracemdb_sysconf
8660Sstevel@tonic-gate };
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate typedef struct dtrace_dcmddata {
8690Sstevel@tonic-gate dtrace_hdl_t *dtdd_dtp;
8700Sstevel@tonic-gate int dtdd_cpu;
8710Sstevel@tonic-gate int dtdd_quiet;
8720Sstevel@tonic-gate int dtdd_flowindent;
8730Sstevel@tonic-gate int dtdd_heading;
8740Sstevel@tonic-gate } dtrace_dcmddata_t;
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate /*ARGSUSED*/
8770Sstevel@tonic-gate static int
dtrace_dcmdrec(const dtrace_probedata_t * data,const dtrace_recdesc_t * rec,void * arg)8780Sstevel@tonic-gate dtrace_dcmdrec(const dtrace_probedata_t *data,
8790Sstevel@tonic-gate const dtrace_recdesc_t *rec, void *arg)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate dtrace_dcmddata_t *dd = arg;
8820Sstevel@tonic-gate
8830Sstevel@tonic-gate if (rec == NULL) {
8840Sstevel@tonic-gate /*
8850Sstevel@tonic-gate * We have processed the final record; output the newline if
8860Sstevel@tonic-gate * we're not in quiet mode.
8870Sstevel@tonic-gate */
8880Sstevel@tonic-gate if (!dd->dtdd_quiet)
8890Sstevel@tonic-gate mdb_printf("\n");
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate return (DTRACE_CONSUME_THIS);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /*ARGSUSED*/
8980Sstevel@tonic-gate static int
dtrace_dcmdprobe(const dtrace_probedata_t * data,void * arg)8990Sstevel@tonic-gate dtrace_dcmdprobe(const dtrace_probedata_t *data, void *arg)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate dtrace_probedesc_t *pd = data->dtpda_pdesc;
9020Sstevel@tonic-gate processorid_t cpu = data->dtpda_cpu;
9030Sstevel@tonic-gate dtrace_dcmddata_t *dd = arg;
9040Sstevel@tonic-gate char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate if (dd->dtdd_cpu != -1UL && dd->dtdd_cpu != cpu)
9070Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT);
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate if (dd->dtdd_heading == 0) {
9100Sstevel@tonic-gate if (!dd->dtdd_flowindent) {
9110Sstevel@tonic-gate if (!dd->dtdd_quiet) {
9120Sstevel@tonic-gate mdb_printf("%3s %6s %32s\n",
9130Sstevel@tonic-gate "CPU", "ID", "FUNCTION:NAME");
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate } else {
9160Sstevel@tonic-gate mdb_printf("%3s %-41s\n", "CPU", "FUNCTION");
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate dd->dtdd_heading = 1;
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate if (!dd->dtdd_flowindent) {
9220Sstevel@tonic-gate if (!dd->dtdd_quiet) {
9230Sstevel@tonic-gate (void) mdb_snprintf(name, sizeof (name), "%s:%s",
9240Sstevel@tonic-gate pd->dtpd_func, pd->dtpd_name);
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate mdb_printf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate } else {
9290Sstevel@tonic-gate int indent = data->dtpda_indent;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate if (data->dtpda_flow == DTRACEFLOW_NONE) {
9320Sstevel@tonic-gate (void) mdb_snprintf(name, sizeof (name), "%*s%s%s:%s",
9330Sstevel@tonic-gate indent, "", data->dtpda_prefix, pd->dtpd_func,
9340Sstevel@tonic-gate pd->dtpd_name);
9350Sstevel@tonic-gate } else {
9360Sstevel@tonic-gate (void) mdb_snprintf(name, sizeof (name), "%*s%s%s",
9370Sstevel@tonic-gate indent, "", data->dtpda_prefix, pd->dtpd_func);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate mdb_printf("%3d %-41s ", cpu, name);
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate return (DTRACE_CONSUME_THIS);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate /*ARGSUSED*/
9470Sstevel@tonic-gate static int
dtrace_dcmderr(const dtrace_errdata_t * data,void * arg)948457Sbmc dtrace_dcmderr(const dtrace_errdata_t *data, void *arg)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate mdb_warn(data->dteda_msg);
9510Sstevel@tonic-gate return (DTRACE_HANDLE_OK);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate /*ARGSUSED*/
9550Sstevel@tonic-gate static int
dtrace_dcmddrop(const dtrace_dropdata_t * data,void * arg)956457Sbmc dtrace_dcmddrop(const dtrace_dropdata_t *data, void *arg)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate mdb_warn(data->dtdda_msg);
9590Sstevel@tonic-gate return (DTRACE_HANDLE_OK);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate /*ARGSUSED*/
9630Sstevel@tonic-gate static int
dtrace_dcmdbuffered(const dtrace_bufdata_t * bufdata,void * arg)964457Sbmc dtrace_dcmdbuffered(const dtrace_bufdata_t *bufdata, void *arg)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate mdb_printf("%s", bufdata->dtbda_buffered);
9670Sstevel@tonic-gate return (DTRACE_HANDLE_OK);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate /*ARGSUSED*/
9710Sstevel@tonic-gate int
dtrace(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)9720Sstevel@tonic-gate dtrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate dtrace_state_t state;
9750Sstevel@tonic-gate dtrace_hdl_t *dtp;
9760Sstevel@tonic-gate int ncpu, err;
9770Sstevel@tonic-gate uintptr_t c = -1UL;
9780Sstevel@tonic-gate dtrace_dcmddata_t dd;
9790Sstevel@tonic-gate dtrace_optval_t val;
9800Sstevel@tonic-gate dtracemdb_data_t md;
9810Sstevel@tonic-gate int rval = DCMD_ERR;
982*12902SBryan.Cantrill@Sun.COM dtrace_anon_t anon;
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
9850Sstevel@tonic-gate return (DCMD_USAGE);
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate if (mdb_getopts(argc, argv, 'c', MDB_OPT_UINTPTR, &c, NULL) != argc)
9880Sstevel@tonic-gate return (DCMD_USAGE);
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate if (mdb_readvar(&ncpu, "_ncpu") == -1) {
9910Sstevel@tonic-gate mdb_warn("failed to read '_ncpu'");
9920Sstevel@tonic-gate return (DCMD_ERR);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate if (mdb_vread(&state, sizeof (state), addr) == -1) {
9960Sstevel@tonic-gate mdb_warn("couldn't read dtrace_state_t at %p", addr);
9970Sstevel@tonic-gate return (DCMD_ERR);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate
1000*12902SBryan.Cantrill@Sun.COM if (state.dts_anon != NULL) {
1001*12902SBryan.Cantrill@Sun.COM addr = (uintptr_t)state.dts_anon;
1002*12902SBryan.Cantrill@Sun.COM
1003*12902SBryan.Cantrill@Sun.COM if (mdb_vread(&state, sizeof (state), addr) == -1) {
1004*12902SBryan.Cantrill@Sun.COM mdb_warn("couldn't read anonymous state at %p", addr);
1005*12902SBryan.Cantrill@Sun.COM return (DCMD_ERR);
1006*12902SBryan.Cantrill@Sun.COM }
1007*12902SBryan.Cantrill@Sun.COM }
1008*12902SBryan.Cantrill@Sun.COM
10090Sstevel@tonic-gate bzero(&md, sizeof (md));
10100Sstevel@tonic-gate md.dtmd_state = &state;
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate if ((dtp = dtrace_vopen(DTRACE_VERSION, DTRACE_O_NOSYS, &err,
10130Sstevel@tonic-gate &dtrace_mdbops, &md)) == NULL) {
10140Sstevel@tonic-gate mdb_warn("failed to initialize dtrace: %s\n",
10150Sstevel@tonic-gate dtrace_errmsg(NULL, err));
10160Sstevel@tonic-gate return (DCMD_ERR);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate
1019*12902SBryan.Cantrill@Sun.COM /*
1020*12902SBryan.Cantrill@Sun.COM * If this is the anonymous enabling, we need to set a bit indicating
1021*12902SBryan.Cantrill@Sun.COM * that DTRACEOPT_GRABANON should be set.
1022*12902SBryan.Cantrill@Sun.COM */
1023*12902SBryan.Cantrill@Sun.COM if (mdb_readvar(&anon, "dtrace_anon") == -1) {
1024*12902SBryan.Cantrill@Sun.COM mdb_warn("failed to read 'dtrace_anon'");
1025*12902SBryan.Cantrill@Sun.COM return (DCMD_ERR);
1026*12902SBryan.Cantrill@Sun.COM }
1027*12902SBryan.Cantrill@Sun.COM
1028*12902SBryan.Cantrill@Sun.COM md.dtmd_isanon = ((uintptr_t)anon.dta_state == addr);
1029*12902SBryan.Cantrill@Sun.COM
10300Sstevel@tonic-gate if (dtrace_go(dtp) != 0) {
10310Sstevel@tonic-gate mdb_warn("failed to initialize dtrace: %s\n",
10320Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10330Sstevel@tonic-gate goto err;
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate bzero(&dd, sizeof (dd));
10370Sstevel@tonic-gate dd.dtdd_dtp = dtp;
10380Sstevel@tonic-gate dd.dtdd_cpu = c;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate if (dtrace_getopt(dtp, "flowindent", &val) == -1) {
10410Sstevel@tonic-gate mdb_warn("couldn't get 'flowindent' option: %s\n",
10420Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10430Sstevel@tonic-gate goto err;
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate dd.dtdd_flowindent = (val != DTRACEOPT_UNSET);
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate if (dtrace_getopt(dtp, "quiet", &val) == -1) {
10490Sstevel@tonic-gate mdb_warn("couldn't get 'quiet' option: %s\n",
10500Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10510Sstevel@tonic-gate goto err;
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate dd.dtdd_quiet = (val != DTRACEOPT_UNSET);
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate if (dtrace_handle_err(dtp, dtrace_dcmderr, NULL) == -1) {
10570Sstevel@tonic-gate mdb_warn("couldn't add err handler: %s\n",
10580Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10590Sstevel@tonic-gate goto err;
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate if (dtrace_handle_drop(dtp, dtrace_dcmddrop, NULL) == -1) {
10630Sstevel@tonic-gate mdb_warn("couldn't add drop handler: %s\n",
10640Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10650Sstevel@tonic-gate goto err;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate if (dtrace_handle_buffered(dtp, dtrace_dcmdbuffered, NULL) == -1) {
10690Sstevel@tonic-gate mdb_warn("couldn't add buffered handler: %s\n",
10700Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10710Sstevel@tonic-gate goto err;
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if (dtrace_status(dtp) == -1) {
10750Sstevel@tonic-gate mdb_warn("couldn't get status: %s\n",
10760Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10770Sstevel@tonic-gate goto err;
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate if (dtrace_aggregate_snap(dtp) == -1) {
10810Sstevel@tonic-gate mdb_warn("couldn't snapshot aggregation: %s\n",
10820Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10830Sstevel@tonic-gate goto err;
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate if (dtrace_consume(dtp, NULL,
10870Sstevel@tonic-gate dtrace_dcmdprobe, dtrace_dcmdrec, &dd) == -1) {
10880Sstevel@tonic-gate mdb_warn("couldn't consume DTrace buffers: %s\n",
10890Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate if (dtrace_aggregate_print(dtp, NULL, NULL) == -1) {
10930Sstevel@tonic-gate mdb_warn("couldn't print aggregation: %s\n",
10940Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
10950Sstevel@tonic-gate goto err;
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate rval = DCMD_OK;
10990Sstevel@tonic-gate err:
11000Sstevel@tonic-gate dtrace_close(dtp);
11010Sstevel@tonic-gate return (rval);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate static int
dtrace_errhash_cmp(const void * l,const void * r)11050Sstevel@tonic-gate dtrace_errhash_cmp(const void *l, const void *r)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate uintptr_t lhs = *((uintptr_t *)l);
11080Sstevel@tonic-gate uintptr_t rhs = *((uintptr_t *)r);
11090Sstevel@tonic-gate dtrace_errhash_t lerr, rerr;
11100Sstevel@tonic-gate char lmsg[256], rmsg[256];
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate (void) mdb_vread(&lerr, sizeof (lerr), lhs);
11130Sstevel@tonic-gate (void) mdb_vread(&rerr, sizeof (rerr), rhs);
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate if (lerr.dter_msg == NULL)
11160Sstevel@tonic-gate return (-1);
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate if (rerr.dter_msg == NULL)
11190Sstevel@tonic-gate return (1);
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate (void) mdb_readstr(lmsg, sizeof (lmsg), (uintptr_t)lerr.dter_msg);
11220Sstevel@tonic-gate (void) mdb_readstr(rmsg, sizeof (rmsg), (uintptr_t)rerr.dter_msg);
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate return (strcmp(lmsg, rmsg));
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate
11270Sstevel@tonic-gate int
dtrace_errhash_init(mdb_walk_state_t * wsp)11280Sstevel@tonic-gate dtrace_errhash_init(mdb_walk_state_t *wsp)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate GElf_Sym sym;
11310Sstevel@tonic-gate uintptr_t *hash, addr;
11320Sstevel@tonic-gate int i;
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if (wsp->walk_addr != NULL) {
11350Sstevel@tonic-gate mdb_warn("dtrace_errhash walk only supports global walks\n");
11360Sstevel@tonic-gate return (WALK_ERR);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate if (mdb_lookup_by_name("dtrace_errhash", &sym) == -1) {
11400Sstevel@tonic-gate mdb_warn("couldn't find 'dtrace_errhash' (non-DEBUG kernel?)");
11410Sstevel@tonic-gate return (WALK_ERR);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate addr = (uintptr_t)sym.st_value;
11450Sstevel@tonic-gate hash = mdb_alloc(DTRACE_ERRHASHSZ * sizeof (uintptr_t),
11460Sstevel@tonic-gate UM_SLEEP | UM_GC);
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate for (i = 0; i < DTRACE_ERRHASHSZ; i++)
11490Sstevel@tonic-gate hash[i] = addr + i * sizeof (dtrace_errhash_t);
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate qsort(hash, DTRACE_ERRHASHSZ, sizeof (uintptr_t), dtrace_errhash_cmp);
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate wsp->walk_addr = 0;
11540Sstevel@tonic-gate wsp->walk_data = hash;
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate return (WALK_NEXT);
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate int
dtrace_errhash_step(mdb_walk_state_t * wsp)11600Sstevel@tonic-gate dtrace_errhash_step(mdb_walk_state_t *wsp)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate int ndx = (int)wsp->walk_addr;
11630Sstevel@tonic-gate uintptr_t *hash = wsp->walk_data;
11640Sstevel@tonic-gate dtrace_errhash_t err;
11650Sstevel@tonic-gate uintptr_t addr;
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate if (ndx >= DTRACE_ERRHASHSZ)
11680Sstevel@tonic-gate return (WALK_DONE);
11690Sstevel@tonic-gate
11700Sstevel@tonic-gate wsp->walk_addr = ndx + 1;
11710Sstevel@tonic-gate addr = hash[ndx];
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate if (mdb_vread(&err, sizeof (err), addr) == -1) {
11740Sstevel@tonic-gate mdb_warn("failed to read dtrace_errhash_t at %p", addr);
11750Sstevel@tonic-gate return (WALK_DONE);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate if (err.dter_msg == NULL)
11790Sstevel@tonic-gate return (WALK_NEXT);
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate return (wsp->walk_callback(addr, &err, wsp->walk_cbdata));
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate /*ARGSUSED*/
11850Sstevel@tonic-gate int
dtrace_errhash(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11860Sstevel@tonic-gate dtrace_errhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate dtrace_errhash_t err;
11890Sstevel@tonic-gate char msg[256];
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
11920Sstevel@tonic-gate if (mdb_walk_dcmd("dtrace_errhash", "dtrace_errhash",
11930Sstevel@tonic-gate argc, argv) == -1) {
11940Sstevel@tonic-gate mdb_warn("can't walk 'dtrace_errhash'");
11950Sstevel@tonic-gate return (DCMD_ERR);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate return (DCMD_OK);
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate if (DCMD_HDRSPEC(flags))
12020Sstevel@tonic-gate mdb_printf("%8s %s\n", "COUNT", "ERROR");
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate if (mdb_vread(&err, sizeof (err), addr) == -1) {
12050Sstevel@tonic-gate mdb_warn("failed to read dtrace_errhash_t at %p", addr);
12060Sstevel@tonic-gate return (DCMD_ERR);
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate addr = (uintptr_t)err.dter_msg;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate if (mdb_readstr(msg, sizeof (msg), addr) == -1) {
12120Sstevel@tonic-gate mdb_warn("failed to read error msg at %p", addr);
12130Sstevel@tonic-gate return (DCMD_ERR);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate mdb_printf("%8d %s", err.dter_count, msg);
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate * Some error messages include a newline -- only print the newline
12200Sstevel@tonic-gate * if the message doesn't have one.
12210Sstevel@tonic-gate */
12220Sstevel@tonic-gate if (msg[strlen(msg) - 1] != '\n')
12230Sstevel@tonic-gate mdb_printf("\n");
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate return (DCMD_OK);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate int
dtrace_helptrace_init(mdb_walk_state_t * wsp)12290Sstevel@tonic-gate dtrace_helptrace_init(mdb_walk_state_t *wsp)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate uint32_t next;
12320Sstevel@tonic-gate int enabled;
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate if (wsp->walk_addr != NULL) {
12350Sstevel@tonic-gate mdb_warn("dtrace_helptrace only supports global walks\n");
12360Sstevel@tonic-gate return (WALK_ERR);
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate
12390Sstevel@tonic-gate if (mdb_readvar(&enabled, "dtrace_helptrace_enabled") == -1) {
12400Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_enabled'");
12410Sstevel@tonic-gate return (WALK_ERR);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate if (!enabled) {
12450Sstevel@tonic-gate mdb_warn("helper tracing is not enabled\n");
12460Sstevel@tonic-gate return (WALK_ERR);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate
12490Sstevel@tonic-gate if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
12500Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_next'");
12510Sstevel@tonic-gate return (WALK_ERR);
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate wsp->walk_addr = next;
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate return (WALK_NEXT);
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate
12590Sstevel@tonic-gate int
dtrace_helptrace_step(mdb_walk_state_t * wsp)12600Sstevel@tonic-gate dtrace_helptrace_step(mdb_walk_state_t *wsp)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate uint32_t next, size, nlocals, bufsize;
12630Sstevel@tonic-gate uintptr_t buffer, addr;
12640Sstevel@tonic-gate dtrace_helptrace_t *ht;
12650Sstevel@tonic-gate int rval;
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
12680Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_next'");
12690Sstevel@tonic-gate return (WALK_ERR);
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate
12720Sstevel@tonic-gate if (mdb_readvar(&bufsize, "dtrace_helptrace_bufsize") == -1) {
12730Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_bufsize'");
12740Sstevel@tonic-gate return (WALK_ERR);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) {
12780Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_buffer'");
12790Sstevel@tonic-gate return (WALK_ERR);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate if (mdb_readvar(&nlocals, "dtrace_helptrace_nlocals") == -1) {
12830Sstevel@tonic-gate mdb_warn("couldn't read 'dtrace_helptrace_nlocals'");
12840Sstevel@tonic-gate return (WALK_ERR);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate size = sizeof (dtrace_helptrace_t) +
12880Sstevel@tonic-gate nlocals * sizeof (uint64_t) - sizeof (uint64_t);
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate if (wsp->walk_addr + size > bufsize) {
12910Sstevel@tonic-gate if (next == 0)
12920Sstevel@tonic-gate return (WALK_DONE);
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate wsp->walk_addr = 0;
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate addr = buffer + wsp->walk_addr;
12980Sstevel@tonic-gate ht = alloca(size);
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate if (mdb_vread(ht, size, addr) == -1) {
13010Sstevel@tonic-gate mdb_warn("couldn't read entry at %p", addr);
13020Sstevel@tonic-gate return (WALK_ERR);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate
13050Sstevel@tonic-gate if (ht->dtht_helper != NULL) {
13060Sstevel@tonic-gate rval = wsp->walk_callback(addr, ht, wsp->walk_cbdata);
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate if (rval != WALK_NEXT)
13090Sstevel@tonic-gate return (rval);
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate if (wsp->walk_addr < next && wsp->walk_addr + size >= next)
13130Sstevel@tonic-gate return (WALK_DONE);
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate wsp->walk_addr += size;
13160Sstevel@tonic-gate return (WALK_NEXT);
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate int
dtrace_helptrace(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)13200Sstevel@tonic-gate dtrace_helptrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate dtrace_helptrace_t help;
13230Sstevel@tonic-gate dtrace_helper_action_t helper;
13240Sstevel@tonic-gate char where[30];
13250Sstevel@tonic-gate uint_t opt_v = FALSE;
13260Sstevel@tonic-gate uintptr_t haddr;
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
13290Sstevel@tonic-gate if (mdb_walk_dcmd("dtrace_helptrace", "dtrace_helptrace",
13300Sstevel@tonic-gate argc, argv) == -1) {
13310Sstevel@tonic-gate mdb_warn("can't walk 'dtrace_helptrace'");
13320Sstevel@tonic-gate return (DCMD_ERR);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate return (DCMD_OK);
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate if (mdb_getopts(argc, argv, 'v',
13390Sstevel@tonic-gate MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc)
13400Sstevel@tonic-gate return (DCMD_USAGE);
13410Sstevel@tonic-gate
13420Sstevel@tonic-gate if (DCMD_HDRSPEC(flags)) {
13430Sstevel@tonic-gate mdb_printf(" %?s %?s %12s %s\n",
13440Sstevel@tonic-gate "ADDR", "HELPER", "WHERE", "DIFO");
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate if (mdb_vread(&help, sizeof (help), addr) == -1) {
13480Sstevel@tonic-gate mdb_warn("failed to read dtrace_helptrace_t at %p", addr);
13490Sstevel@tonic-gate return (DCMD_ERR);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate switch (help.dtht_where) {
13530Sstevel@tonic-gate case 0:
13540Sstevel@tonic-gate (void) mdb_snprintf(where, sizeof (where), "predicate");
13550Sstevel@tonic-gate break;
13560Sstevel@tonic-gate
13570Sstevel@tonic-gate case DTRACE_HELPTRACE_NEXT:
13580Sstevel@tonic-gate (void) mdb_snprintf(where, sizeof (where), "next");
13590Sstevel@tonic-gate break;
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate case DTRACE_HELPTRACE_DONE:
13620Sstevel@tonic-gate (void) mdb_snprintf(where, sizeof (where), "done");
13630Sstevel@tonic-gate break;
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate case DTRACE_HELPTRACE_ERR:
13660Sstevel@tonic-gate (void) mdb_snprintf(where, sizeof (where), "err");
13670Sstevel@tonic-gate break;
13680Sstevel@tonic-gate
13690Sstevel@tonic-gate default:
13700Sstevel@tonic-gate (void) mdb_snprintf(where, sizeof (where),
13710Sstevel@tonic-gate "action #%d", help.dtht_where);
13720Sstevel@tonic-gate break;
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate mdb_printf(" %?p %?p %12s ", addr, help.dtht_helper, where);
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate haddr = (uintptr_t)help.dtht_helper;
13780Sstevel@tonic-gate
13790Sstevel@tonic-gate if (mdb_vread(&helper, sizeof (helper), haddr) == -1) {
13800Sstevel@tonic-gate /*
13810Sstevel@tonic-gate * We're not going to warn in this case -- we're just not going
13820Sstevel@tonic-gate * to print anything exciting.
13830Sstevel@tonic-gate */
13840Sstevel@tonic-gate mdb_printf("???\n");
13850Sstevel@tonic-gate } else {
13860Sstevel@tonic-gate switch (help.dtht_where) {
13870Sstevel@tonic-gate case 0:
13882021Sahl mdb_printf("%p\n", helper.dtha_predicate);
13890Sstevel@tonic-gate break;
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate case DTRACE_HELPTRACE_NEXT:
13920Sstevel@tonic-gate case DTRACE_HELPTRACE_DONE:
13930Sstevel@tonic-gate case DTRACE_HELPTRACE_ERR:
13940Sstevel@tonic-gate mdb_printf("-\n");
13950Sstevel@tonic-gate break;
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate default:
13982021Sahl haddr = (uintptr_t)helper.dtha_actions +
13990Sstevel@tonic-gate (help.dtht_where - 1) * sizeof (uintptr_t);
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate if (mdb_vread(&haddr, sizeof (haddr), haddr) == -1) {
14020Sstevel@tonic-gate mdb_printf("???\n");
14030Sstevel@tonic-gate } else {
14040Sstevel@tonic-gate mdb_printf("%p\n", haddr);
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate if (opt_v) {
14100Sstevel@tonic-gate int i;
14110Sstevel@tonic-gate
1412491Sbmc if (help.dtht_where == DTRACE_HELPTRACE_ERR) {
1413491Sbmc int f = help.dtht_fault;
1414491Sbmc
1415491Sbmc mdb_printf("%?s| %?s %10s |\n", "", "", "");
1416491Sbmc mdb_printf("%?s| %?s %10s +-> fault: %s\n", "", "", "",
1417491Sbmc f == DTRACEFLT_BADADDR ? "BADADDR" :
1418491Sbmc f == DTRACEFLT_BADALIGN ? "BADALIGN" :
1419491Sbmc f == DTRACEFLT_ILLOP ? "ILLOP" :
1420491Sbmc f == DTRACEFLT_DIVZERO ? "DIVZERO" :
1421491Sbmc f == DTRACEFLT_NOSCRATCH ? "NOSCRATCH" :
1422491Sbmc f == DTRACEFLT_KPRIV ? "KPRIV" :
1423491Sbmc f == DTRACEFLT_UPRIV ? "UPRIV" :
1424491Sbmc f == DTRACEFLT_TUPOFLOW ? "TUPOFLOW" :
14253682Sjhaslam f == DTRACEFLT_BADSTACK ? "BADSTACK" :
1426491Sbmc "DTRACEFLT_UNKNOWN");
1427491Sbmc mdb_printf("%?s| %?s %12s addr: 0x%x\n", "", "", "",
1428491Sbmc help.dtht_illval);
1429491Sbmc mdb_printf("%?s| %?s %12s offset: %d\n", "", "", "",
1430491Sbmc help.dtht_fltoffs);
1431491Sbmc }
1432491Sbmc
14330Sstevel@tonic-gate mdb_printf("%?s|\n%?s+--> %?s %4s %s\n", "", "",
14340Sstevel@tonic-gate "ADDR", "NDX", "VALUE");
14350Sstevel@tonic-gate addr += sizeof (help) - sizeof (uint64_t);
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate for (i = 0; i < help.dtht_nlocals; i++) {
14380Sstevel@tonic-gate uint64_t val;
14390Sstevel@tonic-gate
14400Sstevel@tonic-gate if (mdb_vread(&val, sizeof (val), addr) == -1) {
14410Sstevel@tonic-gate mdb_warn("couldn't read local at %p", addr);
14420Sstevel@tonic-gate continue;
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate
14450Sstevel@tonic-gate mdb_printf("%?s %?p %4d %p\n", "", addr, i, val);
14460Sstevel@tonic-gate addr += sizeof (uint64_t);
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate mdb_printf("\n");
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate return (DCMD_OK);
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate
14550Sstevel@tonic-gate /*ARGSUSED*/
14560Sstevel@tonic-gate static int
dtrace_state_walk(uintptr_t addr,const vmem_seg_t * seg,minor_t * highest)14570Sstevel@tonic-gate dtrace_state_walk(uintptr_t addr, const vmem_seg_t *seg, minor_t *highest)
14580Sstevel@tonic-gate {
14590Sstevel@tonic-gate if (seg->vs_end > *highest)
14600Sstevel@tonic-gate *highest = seg->vs_end;
14610Sstevel@tonic-gate
14620Sstevel@tonic-gate return (WALK_NEXT);
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate typedef struct dtrace_state_walk {
14660Sstevel@tonic-gate uintptr_t dtsw_softstate;
14670Sstevel@tonic-gate minor_t dtsw_max;
14680Sstevel@tonic-gate minor_t dtsw_current;
14690Sstevel@tonic-gate } dtrace_state_walk_t;
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate int
dtrace_state_init(mdb_walk_state_t * wsp)14720Sstevel@tonic-gate dtrace_state_init(mdb_walk_state_t *wsp)
14730Sstevel@tonic-gate {
14740Sstevel@tonic-gate uintptr_t dtrace_minor;
14750Sstevel@tonic-gate minor_t max = 0;
14760Sstevel@tonic-gate dtrace_state_walk_t *dw;
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate if (wsp->walk_addr != NULL) {
14790Sstevel@tonic-gate mdb_warn("dtrace_state only supports global walks\n");
14800Sstevel@tonic-gate return (WALK_ERR);
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate
14830Sstevel@tonic-gate /*
14840Sstevel@tonic-gate * Find the dtrace_minor vmem arena and walk it to get the maximum
14850Sstevel@tonic-gate * minor number.
14860Sstevel@tonic-gate */
14870Sstevel@tonic-gate if (mdb_readvar(&dtrace_minor, "dtrace_minor") == -1) {
14880Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_minor'");
14890Sstevel@tonic-gate return (WALK_ERR);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate if (mdb_pwalk("vmem_alloc", (mdb_walk_cb_t)dtrace_state_walk,
14930Sstevel@tonic-gate &max, dtrace_minor) == -1) {
14940Sstevel@tonic-gate mdb_warn("couldn't walk 'vmem_alloc'");
14950Sstevel@tonic-gate return (WALK_ERR);
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate dw = mdb_zalloc(sizeof (dtrace_state_walk_t), UM_SLEEP | UM_GC);
14990Sstevel@tonic-gate dw->dtsw_current = 0;
15000Sstevel@tonic-gate dw->dtsw_max = max;
15010Sstevel@tonic-gate
15020Sstevel@tonic-gate if (mdb_readvar(&dw->dtsw_softstate, "dtrace_softstate") == -1) {
15030Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_softstate'");
15040Sstevel@tonic-gate return (DCMD_ERR);
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate
15070Sstevel@tonic-gate wsp->walk_data = dw;
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate return (WALK_NEXT);
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate
15120Sstevel@tonic-gate int
dtrace_state_step(mdb_walk_state_t * wsp)15130Sstevel@tonic-gate dtrace_state_step(mdb_walk_state_t *wsp)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate dtrace_state_walk_t *dw = wsp->walk_data;
15160Sstevel@tonic-gate uintptr_t statep;
15170Sstevel@tonic-gate dtrace_state_t state;
15180Sstevel@tonic-gate int rval;
15190Sstevel@tonic-gate
15200Sstevel@tonic-gate while (mdb_get_soft_state_byaddr(dw->dtsw_softstate, dw->dtsw_current,
15210Sstevel@tonic-gate &statep, NULL, 0) == -1) {
15220Sstevel@tonic-gate if (dw->dtsw_current >= dw->dtsw_max)
15230Sstevel@tonic-gate return (WALK_DONE);
15240Sstevel@tonic-gate
15250Sstevel@tonic-gate dw->dtsw_current++;
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate if (mdb_vread(&state, sizeof (state), statep) == -1) {
15290Sstevel@tonic-gate mdb_warn("couldn't read dtrace_state_t at %p", statep);
15300Sstevel@tonic-gate return (WALK_NEXT);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate rval = wsp->walk_callback(statep, &state, wsp->walk_cbdata);
15340Sstevel@tonic-gate dw->dtsw_current++;
15350Sstevel@tonic-gate
15360Sstevel@tonic-gate return (rval);
15370Sstevel@tonic-gate }
15380Sstevel@tonic-gate
15390Sstevel@tonic-gate typedef struct dtrace_state_data {
15400Sstevel@tonic-gate int dtsd_major;
15410Sstevel@tonic-gate uintptr_t dtsd_proc;
15420Sstevel@tonic-gate uintptr_t dtsd_softstate;
15430Sstevel@tonic-gate uintptr_t dtsd_state;
15440Sstevel@tonic-gate } dtrace_state_data_t;
15450Sstevel@tonic-gate
15460Sstevel@tonic-gate static int
dtrace_state_file(uintptr_t addr,struct file * f,dtrace_state_data_t * data)15470Sstevel@tonic-gate dtrace_state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate vnode_t vnode;
15500Sstevel@tonic-gate proc_t proc;
15510Sstevel@tonic-gate minor_t minor;
15520Sstevel@tonic-gate uintptr_t statep;
15530Sstevel@tonic-gate
15540Sstevel@tonic-gate if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) {
15550Sstevel@tonic-gate mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode);
15560Sstevel@tonic-gate return (WALK_NEXT);
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate if (getmajor(vnode.v_rdev) != data->dtsd_major)
15600Sstevel@tonic-gate return (WALK_NEXT);
15610Sstevel@tonic-gate
15620Sstevel@tonic-gate minor = getminor(vnode.v_rdev);
15630Sstevel@tonic-gate
15640Sstevel@tonic-gate if (mdb_vread(&proc, sizeof (proc), data->dtsd_proc) == -1) {
15650Sstevel@tonic-gate mdb_warn("failed to read proc at %p", data->dtsd_proc);
15660Sstevel@tonic-gate return (WALK_NEXT);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor,
15700Sstevel@tonic-gate &statep, NULL, 0) == -1) {
15710Sstevel@tonic-gate mdb_warn("failed to read softstate for minor %d", minor);
15720Sstevel@tonic-gate return (WALK_NEXT);
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate
15750Sstevel@tonic-gate if (statep != data->dtsd_state)
15760Sstevel@tonic-gate return (WALK_NEXT);
15770Sstevel@tonic-gate
15780Sstevel@tonic-gate mdb_printf("%?p %5d %?p %-*s %?p\n", statep, minor,
15790Sstevel@tonic-gate data->dtsd_proc, MAXCOMLEN, proc.p_user.u_comm, addr);
15800Sstevel@tonic-gate
15810Sstevel@tonic-gate return (WALK_NEXT);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate /*ARGSUSED*/
15850Sstevel@tonic-gate static int
dtrace_state_proc(uintptr_t addr,void * ignored,dtrace_state_data_t * data)15860Sstevel@tonic-gate dtrace_state_proc(uintptr_t addr, void *ignored, dtrace_state_data_t *data)
15870Sstevel@tonic-gate {
15880Sstevel@tonic-gate data->dtsd_proc = addr;
15890Sstevel@tonic-gate
15900Sstevel@tonic-gate if (mdb_pwalk("file",
15910Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_state_file, data, addr) == -1) {
15920Sstevel@tonic-gate mdb_warn("couldn't walk 'file' for proc %p", addr);
15930Sstevel@tonic-gate return (WALK_ERR);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate return (WALK_NEXT);
15970Sstevel@tonic-gate }
15980Sstevel@tonic-gate
15990Sstevel@tonic-gate void
dtrace_state_help(void)16000Sstevel@tonic-gate dtrace_state_help(void)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate mdb_printf("Given a dtrace_state_t structure, displays all "
16030Sstevel@tonic-gate /*CSTYLED*/
16040Sstevel@tonic-gate "consumers, or \"<anonymous>\"\nif the consumer is anonymous. If "
16050Sstevel@tonic-gate "no state structure is provided, iterates\nover all state "
16060Sstevel@tonic-gate "structures.\n\n"
16070Sstevel@tonic-gate "Addresses in ADDR column may be provided to ::dtrace to obtain\n"
16080Sstevel@tonic-gate "dtrace(1M)-like output for in-kernel DTrace data.\n");
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate
16110Sstevel@tonic-gate int
dtrace_state(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)16120Sstevel@tonic-gate dtrace_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
16130Sstevel@tonic-gate {
16140Sstevel@tonic-gate uintptr_t devi;
16150Sstevel@tonic-gate struct dev_info info;
16160Sstevel@tonic-gate dtrace_state_data_t data;
16170Sstevel@tonic-gate dtrace_anon_t anon;
16180Sstevel@tonic-gate dtrace_state_t state;
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC)) {
16210Sstevel@tonic-gate if (mdb_walk_dcmd("dtrace_state",
16220Sstevel@tonic-gate "dtrace_state", argc, argv) == -1) {
16230Sstevel@tonic-gate mdb_warn("can't walk dtrace_state");
16240Sstevel@tonic-gate return (DCMD_ERR);
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate return (DCMD_OK);
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate
16290Sstevel@tonic-gate if (DCMD_HDRSPEC(flags)) {
16300Sstevel@tonic-gate mdb_printf("%?s %5s %?s %-*s %?s\n", "ADDR", "MINOR", "PROC",
16310Sstevel@tonic-gate MAXCOMLEN, "NAME", "FILE");
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate * First determine if this is anonymous state.
16360Sstevel@tonic-gate */
16370Sstevel@tonic-gate if (mdb_readvar(&anon, "dtrace_anon") == -1) {
16380Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_anon'");
16390Sstevel@tonic-gate return (DCMD_ERR);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate if ((uintptr_t)anon.dta_state == addr) {
16430Sstevel@tonic-gate if (mdb_vread(&state, sizeof (state), addr) == -1) {
16440Sstevel@tonic-gate mdb_warn("failed to read anon at %p", addr);
16450Sstevel@tonic-gate return (DCMD_ERR);
16460Sstevel@tonic-gate }
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate mdb_printf("%?p %5d %?s %-*s %?s\n", addr,
16490Sstevel@tonic-gate getminor(state.dts_dev), "-", MAXCOMLEN,
16500Sstevel@tonic-gate "<anonymous>", "-");
16510Sstevel@tonic-gate
16520Sstevel@tonic-gate return (DCMD_OK);
16530Sstevel@tonic-gate }
16540Sstevel@tonic-gate
16550Sstevel@tonic-gate if (mdb_readvar(&devi, "dtrace_devi") == -1) {
16560Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_devi'");
16570Sstevel@tonic-gate return (DCMD_ERR);
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate
16600Sstevel@tonic-gate if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) {
16610Sstevel@tonic-gate mdb_warn("failed to read 'dev_info'");
16620Sstevel@tonic-gate return (DCMD_ERR);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate data.dtsd_major = info.devi_major;
16660Sstevel@tonic-gate
16670Sstevel@tonic-gate if (mdb_readvar(&data.dtsd_softstate, "dtrace_softstate") == -1) {
16680Sstevel@tonic-gate mdb_warn("failed to read 'dtrace_softstate'");
16690Sstevel@tonic-gate return (DCMD_ERR);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate data.dtsd_state = addr;
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate /*
16750Sstevel@tonic-gate * Walk through all processes and all open files looking for this
16760Sstevel@tonic-gate * state. It must be open somewhere...
16770Sstevel@tonic-gate */
16780Sstevel@tonic-gate if (mdb_walk("proc", (mdb_walk_cb_t)dtrace_state_proc, &data) == -1) {
16790Sstevel@tonic-gate mdb_warn("couldn't walk 'proc'");
16800Sstevel@tonic-gate return (DCMD_ERR);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate return (DCMD_OK);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate
16860Sstevel@tonic-gate typedef struct dtrace_aggkey_data {
16870Sstevel@tonic-gate uintptr_t *dtakd_hash;
16880Sstevel@tonic-gate uintptr_t dtakd_hashsize;
16890Sstevel@tonic-gate uintptr_t dtakd_next;
16900Sstevel@tonic-gate uintptr_t dtakd_ndx;
16910Sstevel@tonic-gate } dtrace_aggkey_data_t;
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate int
dtrace_aggkey_init(mdb_walk_state_t * wsp)16940Sstevel@tonic-gate dtrace_aggkey_init(mdb_walk_state_t *wsp)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate dtrace_buffer_t buf;
16970Sstevel@tonic-gate uintptr_t addr;
16980Sstevel@tonic-gate dtrace_aggbuffer_t agb;
16990Sstevel@tonic-gate dtrace_aggkey_data_t *data;
17000Sstevel@tonic-gate size_t hsize;
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate if ((addr = wsp->walk_addr) == NULL) {
17030Sstevel@tonic-gate mdb_warn("dtrace_aggkey walk needs aggregation buffer\n");
17040Sstevel@tonic-gate return (WALK_ERR);
17050Sstevel@tonic-gate }
17060Sstevel@tonic-gate
17070Sstevel@tonic-gate if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
17080Sstevel@tonic-gate mdb_warn("failed to read aggregation buffer at %p", addr);
17090Sstevel@tonic-gate return (WALK_ERR);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate addr = (uintptr_t)buf.dtb_tomax +
17130Sstevel@tonic-gate buf.dtb_size - sizeof (dtrace_aggbuffer_t);
17140Sstevel@tonic-gate
17150Sstevel@tonic-gate if (mdb_vread(&agb, sizeof (agb), addr) == -1) {
17160Sstevel@tonic-gate mdb_warn("failed to read dtrace_aggbuffer_t at %p", addr);
17170Sstevel@tonic-gate return (WALK_ERR);
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate
17200Sstevel@tonic-gate data = mdb_zalloc(sizeof (dtrace_aggkey_data_t), UM_SLEEP);
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate data->dtakd_hashsize = agb.dtagb_hashsize;
17230Sstevel@tonic-gate hsize = agb.dtagb_hashsize * sizeof (dtrace_aggkey_t *);
17240Sstevel@tonic-gate data->dtakd_hash = mdb_alloc(hsize, UM_SLEEP);
17250Sstevel@tonic-gate
17260Sstevel@tonic-gate if (mdb_vread(data->dtakd_hash, hsize,
17270Sstevel@tonic-gate (uintptr_t)agb.dtagb_hash) == -1) {
17280Sstevel@tonic-gate mdb_warn("failed to read hash at %p",
17290Sstevel@tonic-gate (uintptr_t)agb.dtagb_hash);
17300Sstevel@tonic-gate mdb_free(data->dtakd_hash, hsize);
17310Sstevel@tonic-gate mdb_free(data, sizeof (dtrace_aggkey_data_t));
17320Sstevel@tonic-gate return (WALK_ERR);
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate wsp->walk_data = data;
17360Sstevel@tonic-gate return (WALK_NEXT);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate int
dtrace_aggkey_step(mdb_walk_state_t * wsp)17400Sstevel@tonic-gate dtrace_aggkey_step(mdb_walk_state_t *wsp)
17410Sstevel@tonic-gate {
17420Sstevel@tonic-gate dtrace_aggkey_data_t *data = wsp->walk_data;
17430Sstevel@tonic-gate dtrace_aggkey_t key;
17440Sstevel@tonic-gate uintptr_t addr;
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate while ((addr = data->dtakd_next) == NULL) {
17470Sstevel@tonic-gate if (data->dtakd_ndx == data->dtakd_hashsize)
17480Sstevel@tonic-gate return (WALK_DONE);
17490Sstevel@tonic-gate
17500Sstevel@tonic-gate data->dtakd_next = data->dtakd_hash[data->dtakd_ndx++];
17510Sstevel@tonic-gate }
17520Sstevel@tonic-gate
17530Sstevel@tonic-gate if (mdb_vread(&key, sizeof (key), addr) == -1) {
17540Sstevel@tonic-gate mdb_warn("failed to read dtrace_aggkey_t at %p", addr);
17550Sstevel@tonic-gate return (WALK_ERR);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate
17580Sstevel@tonic-gate data->dtakd_next = (uintptr_t)key.dtak_next;
17590Sstevel@tonic-gate
17600Sstevel@tonic-gate return (wsp->walk_callback(addr, &key, wsp->walk_cbdata));
17610Sstevel@tonic-gate }
17620Sstevel@tonic-gate
17630Sstevel@tonic-gate void
dtrace_aggkey_fini(mdb_walk_state_t * wsp)17640Sstevel@tonic-gate dtrace_aggkey_fini(mdb_walk_state_t *wsp)
17650Sstevel@tonic-gate {
17660Sstevel@tonic-gate dtrace_aggkey_data_t *data = wsp->walk_data;
17670Sstevel@tonic-gate size_t hsize;
17680Sstevel@tonic-gate
17690Sstevel@tonic-gate hsize = data->dtakd_hashsize * sizeof (dtrace_aggkey_t *);
17700Sstevel@tonic-gate mdb_free(data->dtakd_hash, hsize);
17710Sstevel@tonic-gate mdb_free(data, sizeof (dtrace_aggkey_data_t));
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate
17740Sstevel@tonic-gate typedef struct dtrace_dynvar_data {
17750Sstevel@tonic-gate dtrace_dynhash_t *dtdvd_hash;
17760Sstevel@tonic-gate uintptr_t dtdvd_hashsize;
17770Sstevel@tonic-gate uintptr_t dtdvd_next;
17780Sstevel@tonic-gate uintptr_t dtdvd_ndx;
1779*12902SBryan.Cantrill@Sun.COM uintptr_t dtdvd_sink;
17800Sstevel@tonic-gate } dtrace_dynvar_data_t;
17810Sstevel@tonic-gate
17820Sstevel@tonic-gate int
dtrace_dynvar_init(mdb_walk_state_t * wsp)17830Sstevel@tonic-gate dtrace_dynvar_init(mdb_walk_state_t *wsp)
17840Sstevel@tonic-gate {
17850Sstevel@tonic-gate uintptr_t addr;
17860Sstevel@tonic-gate dtrace_dstate_t dstate;
17870Sstevel@tonic-gate dtrace_dynvar_data_t *data;
17880Sstevel@tonic-gate size_t hsize;
1789*12902SBryan.Cantrill@Sun.COM GElf_Sym sym;
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate if ((addr = wsp->walk_addr) == NULL) {
17920Sstevel@tonic-gate mdb_warn("dtrace_dynvar walk needs dtrace_dstate_t\n");
17930Sstevel@tonic-gate return (WALK_ERR);
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate
17960Sstevel@tonic-gate if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
17970Sstevel@tonic-gate mdb_warn("failed to read dynamic state at %p", addr);
17980Sstevel@tonic-gate return (WALK_ERR);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate
1801*12902SBryan.Cantrill@Sun.COM if (mdb_lookup_by_name("dtrace_dynhash_sink", &sym) == -1) {
1802*12902SBryan.Cantrill@Sun.COM mdb_warn("couldn't find 'dtrace_dynhash_sink'");
1803*12902SBryan.Cantrill@Sun.COM return (WALK_ERR);
1804*12902SBryan.Cantrill@Sun.COM }
1805*12902SBryan.Cantrill@Sun.COM
18060Sstevel@tonic-gate data = mdb_zalloc(sizeof (dtrace_dynvar_data_t), UM_SLEEP);
18070Sstevel@tonic-gate
18080Sstevel@tonic-gate data->dtdvd_hashsize = dstate.dtds_hashsize;
18090Sstevel@tonic-gate hsize = dstate.dtds_hashsize * sizeof (dtrace_dynhash_t);
18100Sstevel@tonic-gate data->dtdvd_hash = mdb_alloc(hsize, UM_SLEEP);
1811*12902SBryan.Cantrill@Sun.COM data->dtdvd_sink = (uintptr_t)sym.st_value;
18120Sstevel@tonic-gate
18130Sstevel@tonic-gate if (mdb_vread(data->dtdvd_hash, hsize,
18140Sstevel@tonic-gate (uintptr_t)dstate.dtds_hash) == -1) {
18150Sstevel@tonic-gate mdb_warn("failed to read hash at %p",
18160Sstevel@tonic-gate (uintptr_t)dstate.dtds_hash);
18170Sstevel@tonic-gate mdb_free(data->dtdvd_hash, hsize);
18180Sstevel@tonic-gate mdb_free(data, sizeof (dtrace_dynvar_data_t));
18190Sstevel@tonic-gate return (WALK_ERR);
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate
1822*12902SBryan.Cantrill@Sun.COM data->dtdvd_next = (uintptr_t)data->dtdvd_hash[0].dtdh_chain;
1823*12902SBryan.Cantrill@Sun.COM
18240Sstevel@tonic-gate wsp->walk_data = data;
18250Sstevel@tonic-gate return (WALK_NEXT);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate int
dtrace_dynvar_step(mdb_walk_state_t * wsp)18290Sstevel@tonic-gate dtrace_dynvar_step(mdb_walk_state_t *wsp)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate dtrace_dynvar_data_t *data = wsp->walk_data;
18320Sstevel@tonic-gate dtrace_dynvar_t dynvar, *dvar;
18330Sstevel@tonic-gate size_t dvarsize;
18340Sstevel@tonic-gate uintptr_t addr;
18350Sstevel@tonic-gate int nkeys;
18360Sstevel@tonic-gate
1837*12902SBryan.Cantrill@Sun.COM while ((addr = data->dtdvd_next) == data->dtdvd_sink) {
18380Sstevel@tonic-gate if (data->dtdvd_ndx == data->dtdvd_hashsize)
18390Sstevel@tonic-gate return (WALK_DONE);
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate data->dtdvd_next =
18420Sstevel@tonic-gate (uintptr_t)data->dtdvd_hash[data->dtdvd_ndx++].dtdh_chain;
18430Sstevel@tonic-gate }
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate if (mdb_vread(&dynvar, sizeof (dynvar), addr) == -1) {
18460Sstevel@tonic-gate mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
18470Sstevel@tonic-gate return (WALK_ERR);
18480Sstevel@tonic-gate }
18490Sstevel@tonic-gate
18500Sstevel@tonic-gate /*
18510Sstevel@tonic-gate * Now we need to allocate the correct size.
18520Sstevel@tonic-gate */
18530Sstevel@tonic-gate nkeys = dynvar.dtdv_tuple.dtt_nkeys;
18540Sstevel@tonic-gate dvarsize = (uintptr_t)&dynvar.dtdv_tuple.dtt_key[nkeys] -
18550Sstevel@tonic-gate (uintptr_t)&dynvar;
18560Sstevel@tonic-gate
18570Sstevel@tonic-gate dvar = alloca(dvarsize);
18580Sstevel@tonic-gate
18590Sstevel@tonic-gate if (mdb_vread(dvar, dvarsize, addr) == -1) {
18600Sstevel@tonic-gate mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
18610Sstevel@tonic-gate return (WALK_ERR);
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate
18640Sstevel@tonic-gate data->dtdvd_next = (uintptr_t)dynvar.dtdv_next;
18650Sstevel@tonic-gate
18660Sstevel@tonic-gate return (wsp->walk_callback(addr, dvar, wsp->walk_cbdata));
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate
18690Sstevel@tonic-gate void
dtrace_dynvar_fini(mdb_walk_state_t * wsp)18700Sstevel@tonic-gate dtrace_dynvar_fini(mdb_walk_state_t *wsp)
18710Sstevel@tonic-gate {
18720Sstevel@tonic-gate dtrace_dynvar_data_t *data = wsp->walk_data;
18730Sstevel@tonic-gate size_t hsize;
18740Sstevel@tonic-gate
18750Sstevel@tonic-gate hsize = data->dtdvd_hashsize * sizeof (dtrace_dynvar_t *);
18760Sstevel@tonic-gate mdb_free(data->dtdvd_hash, hsize);
18770Sstevel@tonic-gate mdb_free(data, sizeof (dtrace_dynvar_data_t));
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate
18800Sstevel@tonic-gate typedef struct dtrace_hashstat_data {
18810Sstevel@tonic-gate size_t *dthsd_counts;
18820Sstevel@tonic-gate size_t dthsd_hashsize;
18830Sstevel@tonic-gate char *dthsd_data;
18840Sstevel@tonic-gate size_t dthsd_size;
18850Sstevel@tonic-gate int dthsd_header;
18860Sstevel@tonic-gate } dtrace_hashstat_data_t;
18870Sstevel@tonic-gate
18880Sstevel@tonic-gate typedef void (*dtrace_hashstat_func_t)(dtrace_hashstat_data_t *);
18890Sstevel@tonic-gate
18900Sstevel@tonic-gate static void
dtrace_hashstat_additive(dtrace_hashstat_data_t * data)18910Sstevel@tonic-gate dtrace_hashstat_additive(dtrace_hashstat_data_t *data)
18920Sstevel@tonic-gate {
18930Sstevel@tonic-gate int i;
18940Sstevel@tonic-gate int hval = 0;
18950Sstevel@tonic-gate
18960Sstevel@tonic-gate for (i = 0; i < data->dthsd_size; i++)
18970Sstevel@tonic-gate hval += data->dthsd_data[i];
18980Sstevel@tonic-gate
18990Sstevel@tonic-gate data->dthsd_counts[hval % data->dthsd_hashsize]++;
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate
19020Sstevel@tonic-gate static void
dtrace_hashstat_shifty(dtrace_hashstat_data_t * data)19030Sstevel@tonic-gate dtrace_hashstat_shifty(dtrace_hashstat_data_t *data)
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate uint64_t hval = 0;
19060Sstevel@tonic-gate int i;
19070Sstevel@tonic-gate
19080Sstevel@tonic-gate if (data->dthsd_size < sizeof (uint64_t)) {
19090Sstevel@tonic-gate dtrace_hashstat_additive(data);
19100Sstevel@tonic-gate return;
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate
19130Sstevel@tonic-gate for (i = 0; i < data->dthsd_size; i += sizeof (uint64_t)) {
19140Sstevel@tonic-gate /* LINTED - alignment */
19150Sstevel@tonic-gate uint64_t val = *((uint64_t *)&data->dthsd_data[i]);
19160Sstevel@tonic-gate
19170Sstevel@tonic-gate hval += (val & ((1 << NBBY) - 1)) +
19180Sstevel@tonic-gate ((val >> NBBY) & ((1 << NBBY) - 1)) +
19190Sstevel@tonic-gate ((val >> (NBBY << 1)) & ((1 << NBBY) - 1)) +
19200Sstevel@tonic-gate ((val >> (NBBY << 2)) & ((1 << NBBY) - 1)) +
19210Sstevel@tonic-gate (val & USHRT_MAX) + (val >> (NBBY << 1) & USHRT_MAX);
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate
19240Sstevel@tonic-gate data->dthsd_counts[hval % data->dthsd_hashsize]++;
19250Sstevel@tonic-gate }
19260Sstevel@tonic-gate
19270Sstevel@tonic-gate static void
dtrace_hashstat_knuth(dtrace_hashstat_data_t * data)19280Sstevel@tonic-gate dtrace_hashstat_knuth(dtrace_hashstat_data_t *data)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate int i;
19310Sstevel@tonic-gate int hval = data->dthsd_size;
19320Sstevel@tonic-gate
19330Sstevel@tonic-gate for (i = 0; i < data->dthsd_size; i++)
19340Sstevel@tonic-gate hval = (hval << 4) ^ (hval >> 28) ^ data->dthsd_data[i];
19350Sstevel@tonic-gate
19360Sstevel@tonic-gate data->dthsd_counts[hval % data->dthsd_hashsize]++;
19370Sstevel@tonic-gate }
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate static void
dtrace_hashstat_oneatatime(dtrace_hashstat_data_t * data)19400Sstevel@tonic-gate dtrace_hashstat_oneatatime(dtrace_hashstat_data_t *data)
19410Sstevel@tonic-gate {
19420Sstevel@tonic-gate int i;
19430Sstevel@tonic-gate uint32_t hval = 0;
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate for (i = 0; i < data->dthsd_size; i++) {
19460Sstevel@tonic-gate hval += data->dthsd_data[i];
19470Sstevel@tonic-gate hval += (hval << 10);
19480Sstevel@tonic-gate hval ^= (hval >> 6);
19490Sstevel@tonic-gate }
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate hval += (hval << 3);
19520Sstevel@tonic-gate hval ^= (hval >> 11);
19530Sstevel@tonic-gate hval += (hval << 15);
19540Sstevel@tonic-gate
19550Sstevel@tonic-gate data->dthsd_counts[hval % data->dthsd_hashsize]++;
19560Sstevel@tonic-gate }
19570Sstevel@tonic-gate
19580Sstevel@tonic-gate static void
dtrace_hashstat_fnv(dtrace_hashstat_data_t * data)19590Sstevel@tonic-gate dtrace_hashstat_fnv(dtrace_hashstat_data_t *data)
19600Sstevel@tonic-gate {
19610Sstevel@tonic-gate static const uint32_t prime = 0x01000193;
19620Sstevel@tonic-gate uint32_t hval = 0;
19630Sstevel@tonic-gate int i;
19640Sstevel@tonic-gate
19650Sstevel@tonic-gate for (i = 0; i < data->dthsd_size; i++) {
19660Sstevel@tonic-gate hval *= prime;
19670Sstevel@tonic-gate hval ^= data->dthsd_data[i];
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate
19700Sstevel@tonic-gate data->dthsd_counts[hval % data->dthsd_hashsize]++;
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate
19730Sstevel@tonic-gate static void
dtrace_hashstat_stats(char * name,dtrace_hashstat_data_t * data)19740Sstevel@tonic-gate dtrace_hashstat_stats(char *name, dtrace_hashstat_data_t *data)
19750Sstevel@tonic-gate {
19760Sstevel@tonic-gate size_t nz = 0, i;
19770Sstevel@tonic-gate int longest = 0;
19780Sstevel@tonic-gate size_t ttl = 0;
19790Sstevel@tonic-gate double sum = 0.0;
19800Sstevel@tonic-gate double avg;
19810Sstevel@tonic-gate uint_t util, stddev;
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate if (!data->dthsd_header) {
19840Sstevel@tonic-gate mdb_printf("%15s %11s %11s %11s %11s %11s\n", "NAME",
19850Sstevel@tonic-gate "HASHSIZE", "%UTIL", "LONGEST", "AVERAGE", "STDDEV");
19860Sstevel@tonic-gate data->dthsd_header = 1;
19870Sstevel@tonic-gate }
19880Sstevel@tonic-gate
19890Sstevel@tonic-gate for (i = 0; i < data->dthsd_hashsize; i++) {
19900Sstevel@tonic-gate if (data->dthsd_counts[i] != 0) {
19910Sstevel@tonic-gate nz++;
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate if (data->dthsd_counts[i] > longest)
19940Sstevel@tonic-gate longest = data->dthsd_counts[i];
19950Sstevel@tonic-gate
19960Sstevel@tonic-gate ttl += data->dthsd_counts[i];
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate if (nz == 0) {
20010Sstevel@tonic-gate mdb_printf("%15s %11d %11s %11s %11s %11s\n", name,
20020Sstevel@tonic-gate data->dthsd_hashsize, "-", "-", "-", "-");
20030Sstevel@tonic-gate return;
20040Sstevel@tonic-gate }
20050Sstevel@tonic-gate
20060Sstevel@tonic-gate avg = (double)ttl / (double)nz;
20070Sstevel@tonic-gate
20080Sstevel@tonic-gate for (i = 0; i < data->dthsd_hashsize; i++) {
20090Sstevel@tonic-gate double delta = (double)data->dthsd_counts[i] - avg;
20100Sstevel@tonic-gate
20110Sstevel@tonic-gate if (data->dthsd_counts[i] == 0)
20120Sstevel@tonic-gate continue;
20130Sstevel@tonic-gate
20140Sstevel@tonic-gate sum += delta * delta;
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate
20170Sstevel@tonic-gate util = (nz * 1000) / data->dthsd_hashsize;
20180Sstevel@tonic-gate stddev = (uint_t)sqrt(sum / (double)nz) * 10;
20190Sstevel@tonic-gate
20200Sstevel@tonic-gate mdb_printf("%15s %11d %9u.%1u %11d %11d %9u.%1u\n", name,
20210Sstevel@tonic-gate data->dthsd_hashsize, util / 10, util % 10, longest, ttl / nz,
20220Sstevel@tonic-gate stddev / 10, stddev % 10);
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate
20250Sstevel@tonic-gate static struct dtrace_hashstat {
20260Sstevel@tonic-gate char *dths_name;
20270Sstevel@tonic-gate dtrace_hashstat_func_t dths_func;
20280Sstevel@tonic-gate } _dtrace_hashstat[] = {
20290Sstevel@tonic-gate { "<actual>", NULL },
20300Sstevel@tonic-gate { "additive", dtrace_hashstat_additive },
20310Sstevel@tonic-gate { "shifty", dtrace_hashstat_shifty },
20320Sstevel@tonic-gate { "knuth", dtrace_hashstat_knuth },
20330Sstevel@tonic-gate { "one-at-a-time", dtrace_hashstat_oneatatime },
20340Sstevel@tonic-gate { "fnv", dtrace_hashstat_fnv },
20350Sstevel@tonic-gate { NULL, 0 }
20360Sstevel@tonic-gate };
20370Sstevel@tonic-gate
20380Sstevel@tonic-gate typedef struct dtrace_aggstat_data {
20390Sstevel@tonic-gate dtrace_hashstat_data_t dtagsd_hash;
20400Sstevel@tonic-gate dtrace_hashstat_func_t dtagsd_func;
20410Sstevel@tonic-gate } dtrace_aggstat_data_t;
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate static int
dtrace_aggstat_walk(uintptr_t addr,dtrace_aggkey_t * key,dtrace_aggstat_data_t * data)20440Sstevel@tonic-gate dtrace_aggstat_walk(uintptr_t addr, dtrace_aggkey_t *key,
20450Sstevel@tonic-gate dtrace_aggstat_data_t *data)
20460Sstevel@tonic-gate {
20470Sstevel@tonic-gate dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
20480Sstevel@tonic-gate size_t size;
20490Sstevel@tonic-gate
20500Sstevel@tonic-gate if (data->dtagsd_func == NULL) {
20510Sstevel@tonic-gate size_t bucket = key->dtak_hashval % hdata->dthsd_hashsize;
20520Sstevel@tonic-gate
20530Sstevel@tonic-gate hdata->dthsd_counts[bucket]++;
20540Sstevel@tonic-gate return (WALK_NEXT);
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate
20570Sstevel@tonic-gate /*
20580Sstevel@tonic-gate * We need to read the data.
20590Sstevel@tonic-gate */
20600Sstevel@tonic-gate size = key->dtak_size - sizeof (dtrace_aggid_t);
20610Sstevel@tonic-gate addr = (uintptr_t)key->dtak_data + sizeof (dtrace_aggid_t);
20620Sstevel@tonic-gate hdata->dthsd_data = alloca(size);
20630Sstevel@tonic-gate hdata->dthsd_size = size;
20640Sstevel@tonic-gate
20650Sstevel@tonic-gate if (mdb_vread(hdata->dthsd_data, size, addr) == -1) {
20660Sstevel@tonic-gate mdb_warn("couldn't read data at %p", addr);
20670Sstevel@tonic-gate return (WALK_ERR);
20680Sstevel@tonic-gate }
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate data->dtagsd_func(hdata);
20710Sstevel@tonic-gate
20720Sstevel@tonic-gate return (WALK_NEXT);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate
20750Sstevel@tonic-gate /*ARGSUSED*/
20760Sstevel@tonic-gate int
dtrace_aggstat(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)20770Sstevel@tonic-gate dtrace_aggstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
20780Sstevel@tonic-gate {
20790Sstevel@tonic-gate dtrace_buffer_t buf;
20800Sstevel@tonic-gate uintptr_t aaddr;
20810Sstevel@tonic-gate dtrace_aggbuffer_t agb;
20820Sstevel@tonic-gate size_t hsize, i, actual, prime, evenpow;
20830Sstevel@tonic-gate dtrace_aggstat_data_t data;
20840Sstevel@tonic-gate dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
20850Sstevel@tonic-gate
20860Sstevel@tonic-gate bzero(&data, sizeof (data));
20870Sstevel@tonic-gate
20880Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
20890Sstevel@tonic-gate return (DCMD_USAGE);
20900Sstevel@tonic-gate
20910Sstevel@tonic-gate if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
20920Sstevel@tonic-gate mdb_warn("failed to read aggregation buffer at %p", addr);
20930Sstevel@tonic-gate return (DCMD_ERR);
20940Sstevel@tonic-gate }
20950Sstevel@tonic-gate
20960Sstevel@tonic-gate aaddr = (uintptr_t)buf.dtb_tomax +
20970Sstevel@tonic-gate buf.dtb_size - sizeof (dtrace_aggbuffer_t);
20980Sstevel@tonic-gate
20990Sstevel@tonic-gate if (mdb_vread(&agb, sizeof (agb), aaddr) == -1) {
21000Sstevel@tonic-gate mdb_warn("failed to read dtrace_aggbuffer_t at %p", aaddr);
21010Sstevel@tonic-gate return (DCMD_ERR);
21020Sstevel@tonic-gate }
21030Sstevel@tonic-gate
21040Sstevel@tonic-gate hsize = (actual = agb.dtagb_hashsize) * sizeof (size_t);
21050Sstevel@tonic-gate hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
21060Sstevel@tonic-gate
21070Sstevel@tonic-gate /*
21080Sstevel@tonic-gate * Now pick the largest prime smaller than the hash size. (If the
21090Sstevel@tonic-gate * existing size is prime, we'll pick a smaller prime just for the
21100Sstevel@tonic-gate * hell of it.)
21110Sstevel@tonic-gate */
21120Sstevel@tonic-gate for (prime = agb.dtagb_hashsize - 1; prime > 7; prime--) {
21130Sstevel@tonic-gate size_t limit = prime / 7;
21140Sstevel@tonic-gate
21150Sstevel@tonic-gate for (i = 2; i < limit; i++) {
21160Sstevel@tonic-gate if ((prime % i) == 0)
21170Sstevel@tonic-gate break;
21180Sstevel@tonic-gate }
21190Sstevel@tonic-gate
21200Sstevel@tonic-gate if (i == limit)
21210Sstevel@tonic-gate break;
21220Sstevel@tonic-gate }
21230Sstevel@tonic-gate
21240Sstevel@tonic-gate /*
21250Sstevel@tonic-gate * And now we want to pick the largest power of two smaller than the
21260Sstevel@tonic-gate * hashsize.
21270Sstevel@tonic-gate */
21280Sstevel@tonic-gate for (i = 0; (1 << i) < agb.dtagb_hashsize; i++)
21290Sstevel@tonic-gate continue;
21300Sstevel@tonic-gate
21310Sstevel@tonic-gate evenpow = (1 << (i - 1));
21320Sstevel@tonic-gate
21330Sstevel@tonic-gate for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) {
21340Sstevel@tonic-gate data.dtagsd_func = _dtrace_hashstat[i].dths_func;
21350Sstevel@tonic-gate
21360Sstevel@tonic-gate hdata->dthsd_hashsize = actual;
21370Sstevel@tonic-gate hsize = hdata->dthsd_hashsize * sizeof (size_t);
21380Sstevel@tonic-gate bzero(hdata->dthsd_counts, hsize);
21390Sstevel@tonic-gate
21400Sstevel@tonic-gate if (mdb_pwalk("dtrace_aggkey",
21410Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
21420Sstevel@tonic-gate mdb_warn("failed to walk dtrace_aggkey at %p", addr);
21430Sstevel@tonic-gate return (DCMD_ERR);
21440Sstevel@tonic-gate }
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
21470Sstevel@tonic-gate
21480Sstevel@tonic-gate /*
21490Sstevel@tonic-gate * If we were just printing the actual value, we won't try
21500Sstevel@tonic-gate * any of the sizing experiments.
21510Sstevel@tonic-gate */
21520Sstevel@tonic-gate if (data.dtagsd_func == NULL)
21530Sstevel@tonic-gate continue;
21540Sstevel@tonic-gate
21550Sstevel@tonic-gate hdata->dthsd_hashsize = prime;
21560Sstevel@tonic-gate hsize = hdata->dthsd_hashsize * sizeof (size_t);
21570Sstevel@tonic-gate bzero(hdata->dthsd_counts, hsize);
21580Sstevel@tonic-gate
21590Sstevel@tonic-gate if (mdb_pwalk("dtrace_aggkey",
21600Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
21610Sstevel@tonic-gate mdb_warn("failed to walk dtrace_aggkey at %p", addr);
21620Sstevel@tonic-gate return (DCMD_ERR);
21630Sstevel@tonic-gate }
21640Sstevel@tonic-gate
21650Sstevel@tonic-gate dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
21660Sstevel@tonic-gate
21670Sstevel@tonic-gate hdata->dthsd_hashsize = evenpow;
21680Sstevel@tonic-gate hsize = hdata->dthsd_hashsize * sizeof (size_t);
21690Sstevel@tonic-gate bzero(hdata->dthsd_counts, hsize);
21700Sstevel@tonic-gate
21710Sstevel@tonic-gate if (mdb_pwalk("dtrace_aggkey",
21720Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
21730Sstevel@tonic-gate mdb_warn("failed to walk dtrace_aggkey at %p", addr);
21740Sstevel@tonic-gate return (DCMD_ERR);
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate
21770Sstevel@tonic-gate dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate
21800Sstevel@tonic-gate return (DCMD_OK);
21810Sstevel@tonic-gate }
21820Sstevel@tonic-gate
21830Sstevel@tonic-gate /*ARGSUSED*/
21840Sstevel@tonic-gate static int
dtrace_dynstat_walk(uintptr_t addr,dtrace_dynvar_t * dynvar,dtrace_aggstat_data_t * data)21850Sstevel@tonic-gate dtrace_dynstat_walk(uintptr_t addr, dtrace_dynvar_t *dynvar,
21860Sstevel@tonic-gate dtrace_aggstat_data_t *data)
21870Sstevel@tonic-gate {
21880Sstevel@tonic-gate dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
21890Sstevel@tonic-gate dtrace_tuple_t *tuple = &dynvar->dtdv_tuple;
21900Sstevel@tonic-gate dtrace_key_t *key = tuple->dtt_key;
21910Sstevel@tonic-gate size_t size = 0, offs = 0;
21920Sstevel@tonic-gate int i, nkeys = tuple->dtt_nkeys;
21930Sstevel@tonic-gate char *buf;
21940Sstevel@tonic-gate
21950Sstevel@tonic-gate if (data->dtagsd_func == NULL) {
21960Sstevel@tonic-gate size_t bucket = dynvar->dtdv_hashval % hdata->dthsd_hashsize;
21970Sstevel@tonic-gate
21980Sstevel@tonic-gate hdata->dthsd_counts[bucket]++;
21990Sstevel@tonic-gate return (WALK_NEXT);
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate
22020Sstevel@tonic-gate /*
22030Sstevel@tonic-gate * We want to hand the hashing algorithm a contiguous buffer. First
22040Sstevel@tonic-gate * run through the tuple and determine the size.
22050Sstevel@tonic-gate */
22060Sstevel@tonic-gate for (i = 0; i < nkeys; i++) {
22070Sstevel@tonic-gate if (key[i].dttk_size == 0) {
22080Sstevel@tonic-gate size += sizeof (uint64_t);
22090Sstevel@tonic-gate } else {
22100Sstevel@tonic-gate size += key[i].dttk_size;
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate
22140Sstevel@tonic-gate buf = alloca(size);
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate /*
22170Sstevel@tonic-gate * Now go back through the tuple and copy the data into the buffer.
22180Sstevel@tonic-gate */
22190Sstevel@tonic-gate for (i = 0; i < nkeys; i++) {
22200Sstevel@tonic-gate if (key[i].dttk_size == 0) {
22210Sstevel@tonic-gate bcopy(&key[i].dttk_value, &buf[offs],
22220Sstevel@tonic-gate sizeof (uint64_t));
22230Sstevel@tonic-gate offs += sizeof (uint64_t);
22240Sstevel@tonic-gate } else {
22250Sstevel@tonic-gate if (mdb_vread(&buf[offs], key[i].dttk_size,
22260Sstevel@tonic-gate key[i].dttk_value) == -1) {
22270Sstevel@tonic-gate mdb_warn("couldn't read tuple data at %p",
22280Sstevel@tonic-gate key[i].dttk_value);
22290Sstevel@tonic-gate return (WALK_ERR);
22300Sstevel@tonic-gate }
22310Sstevel@tonic-gate
22320Sstevel@tonic-gate offs += key[i].dttk_size;
22330Sstevel@tonic-gate }
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate
22360Sstevel@tonic-gate hdata->dthsd_data = buf;
22370Sstevel@tonic-gate hdata->dthsd_size = size;
22380Sstevel@tonic-gate
22390Sstevel@tonic-gate data->dtagsd_func(hdata);
22400Sstevel@tonic-gate
22410Sstevel@tonic-gate return (WALK_NEXT);
22420Sstevel@tonic-gate }
22430Sstevel@tonic-gate
22440Sstevel@tonic-gate /*ARGSUSED*/
22450Sstevel@tonic-gate int
dtrace_dynstat(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)22460Sstevel@tonic-gate dtrace_dynstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
22470Sstevel@tonic-gate {
22480Sstevel@tonic-gate dtrace_dstate_t dstate;
22490Sstevel@tonic-gate size_t hsize, i, actual, prime;
22500Sstevel@tonic-gate dtrace_aggstat_data_t data;
22510Sstevel@tonic-gate dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
22520Sstevel@tonic-gate
22530Sstevel@tonic-gate bzero(&data, sizeof (data));
22540Sstevel@tonic-gate
22550Sstevel@tonic-gate if (!(flags & DCMD_ADDRSPEC))
22560Sstevel@tonic-gate return (DCMD_USAGE);
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
22590Sstevel@tonic-gate mdb_warn("failed to read dynamic variable state at %p", addr);
22600Sstevel@tonic-gate return (DCMD_ERR);
22610Sstevel@tonic-gate }
22620Sstevel@tonic-gate
22630Sstevel@tonic-gate hsize = (actual = dstate.dtds_hashsize) * sizeof (size_t);
22640Sstevel@tonic-gate hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
22650Sstevel@tonic-gate
22660Sstevel@tonic-gate /*
22670Sstevel@tonic-gate * Now pick the largest prime smaller than the hash size. (If the
22680Sstevel@tonic-gate * existing size is prime, we'll pick a smaller prime just for the
22690Sstevel@tonic-gate * hell of it.)
22700Sstevel@tonic-gate */
22710Sstevel@tonic-gate for (prime = dstate.dtds_hashsize - 1; prime > 7; prime--) {
22720Sstevel@tonic-gate size_t limit = prime / 7;
22730Sstevel@tonic-gate
22740Sstevel@tonic-gate for (i = 2; i < limit; i++) {
22750Sstevel@tonic-gate if ((prime % i) == 0)
22760Sstevel@tonic-gate break;
22770Sstevel@tonic-gate }
22780Sstevel@tonic-gate
22790Sstevel@tonic-gate if (i == limit)
22800Sstevel@tonic-gate break;
22810Sstevel@tonic-gate }
22820Sstevel@tonic-gate
22830Sstevel@tonic-gate for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) {
22840Sstevel@tonic-gate data.dtagsd_func = _dtrace_hashstat[i].dths_func;
22850Sstevel@tonic-gate
22860Sstevel@tonic-gate hdata->dthsd_hashsize = actual;
22870Sstevel@tonic-gate hsize = hdata->dthsd_hashsize * sizeof (size_t);
22880Sstevel@tonic-gate bzero(hdata->dthsd_counts, hsize);
22890Sstevel@tonic-gate
22900Sstevel@tonic-gate if (mdb_pwalk("dtrace_dynvar",
22910Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) {
22920Sstevel@tonic-gate mdb_warn("failed to walk dtrace_dynvar at %p", addr);
22930Sstevel@tonic-gate return (DCMD_ERR);
22940Sstevel@tonic-gate }
22950Sstevel@tonic-gate
22960Sstevel@tonic-gate dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
22970Sstevel@tonic-gate
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate * If we were just printing the actual value, we won't try
23000Sstevel@tonic-gate * any of the sizing experiments.
23010Sstevel@tonic-gate */
23020Sstevel@tonic-gate if (data.dtagsd_func == NULL)
23030Sstevel@tonic-gate continue;
23040Sstevel@tonic-gate
23050Sstevel@tonic-gate hdata->dthsd_hashsize = prime;
23060Sstevel@tonic-gate hsize = hdata->dthsd_hashsize * sizeof (size_t);
23070Sstevel@tonic-gate bzero(hdata->dthsd_counts, hsize);
23080Sstevel@tonic-gate
23090Sstevel@tonic-gate if (mdb_pwalk("dtrace_dynvar",
23100Sstevel@tonic-gate (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) {
23110Sstevel@tonic-gate mdb_warn("failed to walk dtrace_aggkey at %p", addr);
23120Sstevel@tonic-gate return (DCMD_ERR);
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate
23150Sstevel@tonic-gate dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
23160Sstevel@tonic-gate }
23170Sstevel@tonic-gate
23180Sstevel@tonic-gate return (DCMD_OK);
23190Sstevel@tonic-gate }
23200Sstevel@tonic-gate
23213276Sjhaslam typedef struct dtrace_ecb_walk {
23223276Sjhaslam dtrace_ecb_t **dtew_ecbs;
23233276Sjhaslam int dtew_necbs;
23243276Sjhaslam int dtew_curecb;
23253276Sjhaslam } dtrace_ecb_walk_t;
23263276Sjhaslam
23273276Sjhaslam static int
dtrace_ecb_init(mdb_walk_state_t * wsp)23283276Sjhaslam dtrace_ecb_init(mdb_walk_state_t *wsp)
23293276Sjhaslam {
23303276Sjhaslam uintptr_t addr;
23313276Sjhaslam dtrace_state_t state;
23323276Sjhaslam dtrace_ecb_walk_t *ecbwp;
23333276Sjhaslam
23343276Sjhaslam if ((addr = wsp->walk_addr) == NULL) {
23353276Sjhaslam mdb_warn("dtrace_ecb walk needs dtrace_state_t\n");
23363276Sjhaslam return (WALK_ERR);
23373276Sjhaslam }
23383276Sjhaslam
23393276Sjhaslam if (mdb_vread(&state, sizeof (state), addr) == -1) {
23403276Sjhaslam mdb_warn("failed to read dtrace state pointer at %p", addr);
23413276Sjhaslam return (WALK_ERR);
23423276Sjhaslam }
23433276Sjhaslam
23443276Sjhaslam ecbwp = mdb_zalloc(sizeof (dtrace_ecb_walk_t), UM_SLEEP | UM_GC);
23453276Sjhaslam
23463276Sjhaslam ecbwp->dtew_ecbs = state.dts_ecbs;
23473276Sjhaslam ecbwp->dtew_necbs = state.dts_necbs;
23483276Sjhaslam ecbwp->dtew_curecb = 0;
23493276Sjhaslam
23503276Sjhaslam wsp->walk_data = ecbwp;
23513276Sjhaslam
23523276Sjhaslam return (WALK_NEXT);
23533276Sjhaslam }
23543276Sjhaslam
23553276Sjhaslam static int
dtrace_ecb_step(mdb_walk_state_t * wsp)23563276Sjhaslam dtrace_ecb_step(mdb_walk_state_t *wsp)
23573276Sjhaslam {
23583276Sjhaslam uintptr_t ecbp, addr;
23593276Sjhaslam dtrace_ecb_walk_t *ecbwp = wsp->walk_data;
23603276Sjhaslam
23613276Sjhaslam addr = (uintptr_t)ecbwp->dtew_ecbs +
23623276Sjhaslam ecbwp->dtew_curecb * sizeof (dtrace_ecb_t *);
23633276Sjhaslam
23643276Sjhaslam if (ecbwp->dtew_curecb++ == ecbwp->dtew_necbs)
23653276Sjhaslam return (WALK_DONE);
23663276Sjhaslam
23673276Sjhaslam if (mdb_vread(&ecbp, sizeof (addr), addr) == -1) {
23683276Sjhaslam mdb_warn("failed to read ecb at entry %d\n",
23693276Sjhaslam ecbwp->dtew_curecb);
23703276Sjhaslam return (WALK_ERR);
23713276Sjhaslam }
23723276Sjhaslam
23733276Sjhaslam if (ecbp == NULL)
23743276Sjhaslam return (WALK_NEXT);
23753276Sjhaslam
23763276Sjhaslam return (wsp->walk_callback(ecbp, NULL, wsp->walk_cbdata));
23773276Sjhaslam }
23783276Sjhaslam
237910807SJonathan.Haslam@Sun.COM static void
dtrace_options_numtostr(uint64_t num,char * buf,size_t len)238010807SJonathan.Haslam@Sun.COM dtrace_options_numtostr(uint64_t num, char *buf, size_t len)
238110807SJonathan.Haslam@Sun.COM {
238210807SJonathan.Haslam@Sun.COM uint64_t n = num;
238310807SJonathan.Haslam@Sun.COM int index = 0;
238410807SJonathan.Haslam@Sun.COM char u;
238510807SJonathan.Haslam@Sun.COM
238610807SJonathan.Haslam@Sun.COM while (n >= 1024) {
238710807SJonathan.Haslam@Sun.COM n = (n + (1024 / 2)) / 1024; /* Round up or down */
238810807SJonathan.Haslam@Sun.COM index++;
238910807SJonathan.Haslam@Sun.COM }
239010807SJonathan.Haslam@Sun.COM
239110807SJonathan.Haslam@Sun.COM u = " KMGTPE"[index];
239210807SJonathan.Haslam@Sun.COM
239310807SJonathan.Haslam@Sun.COM if (index == 0) {
239410807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%llu", (u_longlong_t)n);
239510807SJonathan.Haslam@Sun.COM } else if (n < 10 && (num & (num - 1)) != 0) {
239610807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%.2f%c",
239710807SJonathan.Haslam@Sun.COM (double)num / (1ULL << 10 * index), u);
239810807SJonathan.Haslam@Sun.COM } else if (n < 100 && (num & (num - 1)) != 0) {
239910807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%.1f%c",
240010807SJonathan.Haslam@Sun.COM (double)num / (1ULL << 10 * index), u);
240110807SJonathan.Haslam@Sun.COM } else {
240210807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%llu%c", (u_longlong_t)n, u);
240310807SJonathan.Haslam@Sun.COM }
240410807SJonathan.Haslam@Sun.COM }
240510807SJonathan.Haslam@Sun.COM
240610807SJonathan.Haslam@Sun.COM static void
dtrace_options_numtohz(uint64_t num,char * buf,size_t len)240710807SJonathan.Haslam@Sun.COM dtrace_options_numtohz(uint64_t num, char *buf, size_t len)
240810807SJonathan.Haslam@Sun.COM {
240910807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%dhz", NANOSEC/num);
241010807SJonathan.Haslam@Sun.COM }
241110807SJonathan.Haslam@Sun.COM
241210807SJonathan.Haslam@Sun.COM static void
dtrace_options_numtobufpolicy(uint64_t num,char * buf,size_t len)241310807SJonathan.Haslam@Sun.COM dtrace_options_numtobufpolicy(uint64_t num, char *buf, size_t len)
241410807SJonathan.Haslam@Sun.COM {
241510807SJonathan.Haslam@Sun.COM char *policy = "unknown";
241610807SJonathan.Haslam@Sun.COM
241710807SJonathan.Haslam@Sun.COM switch (num) {
241810807SJonathan.Haslam@Sun.COM case DTRACEOPT_BUFPOLICY_RING:
241910807SJonathan.Haslam@Sun.COM policy = "ring";
242010807SJonathan.Haslam@Sun.COM break;
242110807SJonathan.Haslam@Sun.COM
242210807SJonathan.Haslam@Sun.COM case DTRACEOPT_BUFPOLICY_FILL:
242310807SJonathan.Haslam@Sun.COM policy = "fill";
242410807SJonathan.Haslam@Sun.COM break;
242510807SJonathan.Haslam@Sun.COM
242610807SJonathan.Haslam@Sun.COM case DTRACEOPT_BUFPOLICY_SWITCH:
242710807SJonathan.Haslam@Sun.COM policy = "switch";
242810807SJonathan.Haslam@Sun.COM break;
242910807SJonathan.Haslam@Sun.COM }
243010807SJonathan.Haslam@Sun.COM
243110807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%s", policy);
243210807SJonathan.Haslam@Sun.COM }
243310807SJonathan.Haslam@Sun.COM
243410807SJonathan.Haslam@Sun.COM static void
dtrace_options_numtocpu(uint64_t cpu,char * buf,size_t len)243510807SJonathan.Haslam@Sun.COM dtrace_options_numtocpu(uint64_t cpu, char *buf, size_t len)
243610807SJonathan.Haslam@Sun.COM {
243710807SJonathan.Haslam@Sun.COM if (cpu == DTRACE_CPUALL)
243810807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%7s", "unbound");
243910807SJonathan.Haslam@Sun.COM else
244010807SJonathan.Haslam@Sun.COM (void) mdb_snprintf(buf, len, "%d", cpu);
244110807SJonathan.Haslam@Sun.COM }
244210807SJonathan.Haslam@Sun.COM
244310807SJonathan.Haslam@Sun.COM typedef void (*dtrace_options_func_t)(uint64_t, char *, size_t);
244410807SJonathan.Haslam@Sun.COM
244510807SJonathan.Haslam@Sun.COM static struct dtrace_options {
244610807SJonathan.Haslam@Sun.COM char *dtop_optstr;
244710807SJonathan.Haslam@Sun.COM dtrace_options_func_t dtop_func;
244810807SJonathan.Haslam@Sun.COM } _dtrace_options[] = {
244910807SJonathan.Haslam@Sun.COM { "bufsize", dtrace_options_numtostr },
245010807SJonathan.Haslam@Sun.COM { "bufpolicy", dtrace_options_numtobufpolicy },
245110807SJonathan.Haslam@Sun.COM { "dynvarsize", dtrace_options_numtostr },
245210807SJonathan.Haslam@Sun.COM { "aggsize", dtrace_options_numtostr },
245310807SJonathan.Haslam@Sun.COM { "specsize", dtrace_options_numtostr },
245410807SJonathan.Haslam@Sun.COM { "nspec", dtrace_options_numtostr },
245510807SJonathan.Haslam@Sun.COM { "strsize", dtrace_options_numtostr },
245610807SJonathan.Haslam@Sun.COM { "cleanrate", dtrace_options_numtohz },
245710807SJonathan.Haslam@Sun.COM { "cpu", dtrace_options_numtocpu },
245810807SJonathan.Haslam@Sun.COM { "bufresize", dtrace_options_numtostr },
245910807SJonathan.Haslam@Sun.COM { "grabanon", dtrace_options_numtostr },
246010807SJonathan.Haslam@Sun.COM { "flowindent", dtrace_options_numtostr },
246110807SJonathan.Haslam@Sun.COM { "quiet", dtrace_options_numtostr },
246210807SJonathan.Haslam@Sun.COM { "stackframes", dtrace_options_numtostr },
246310807SJonathan.Haslam@Sun.COM { "ustackframes", dtrace_options_numtostr },
246410807SJonathan.Haslam@Sun.COM { "aggrate", dtrace_options_numtohz },
246510807SJonathan.Haslam@Sun.COM { "switchrate", dtrace_options_numtohz },
246610807SJonathan.Haslam@Sun.COM { "statusrate", dtrace_options_numtohz },
246710807SJonathan.Haslam@Sun.COM { "destructive", dtrace_options_numtostr },
246810807SJonathan.Haslam@Sun.COM { "stackindent", dtrace_options_numtostr },
246910807SJonathan.Haslam@Sun.COM { "rawbytes", dtrace_options_numtostr },
247010807SJonathan.Haslam@Sun.COM { "jstackframes", dtrace_options_numtostr },
247110807SJonathan.Haslam@Sun.COM { "jstackstrsize", dtrace_options_numtostr },
247210807SJonathan.Haslam@Sun.COM { "aggsortkey", dtrace_options_numtostr },
247310807SJonathan.Haslam@Sun.COM { "aggsortrev", dtrace_options_numtostr },
247410807SJonathan.Haslam@Sun.COM { "aggsortpos", dtrace_options_numtostr },
247510807SJonathan.Haslam@Sun.COM { "aggsortkeypos", dtrace_options_numtostr }
247610807SJonathan.Haslam@Sun.COM };
247710807SJonathan.Haslam@Sun.COM
247810807SJonathan.Haslam@Sun.COM static void
dtrace_options_help(void)247910807SJonathan.Haslam@Sun.COM dtrace_options_help(void)
248010807SJonathan.Haslam@Sun.COM {
248110807SJonathan.Haslam@Sun.COM mdb_printf("Given a dtrace_state_t structure, displays the "
248210807SJonathan.Haslam@Sun.COM "current tunable option\nsettings.\n");
248310807SJonathan.Haslam@Sun.COM }
248410807SJonathan.Haslam@Sun.COM
248510807SJonathan.Haslam@Sun.COM /*ARGSUSED*/
248610807SJonathan.Haslam@Sun.COM static int
dtrace_options(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)248710807SJonathan.Haslam@Sun.COM dtrace_options(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
248810807SJonathan.Haslam@Sun.COM {
248910807SJonathan.Haslam@Sun.COM dtrace_state_t state;
249010807SJonathan.Haslam@Sun.COM int i = 0;
249110807SJonathan.Haslam@Sun.COM dtrace_optval_t *options;
249210807SJonathan.Haslam@Sun.COM char val[32];
249310807SJonathan.Haslam@Sun.COM
249410807SJonathan.Haslam@Sun.COM if (!(flags & DCMD_ADDRSPEC))
249510807SJonathan.Haslam@Sun.COM return (DCMD_USAGE);
249610807SJonathan.Haslam@Sun.COM
249710807SJonathan.Haslam@Sun.COM if (mdb_vread(&state, sizeof (dtrace_state_t), (uintptr_t)addr) == -1) {
249810807SJonathan.Haslam@Sun.COM mdb_warn("failed to read state pointer at %p\n", addr);
249910807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
250010807SJonathan.Haslam@Sun.COM }
250110807SJonathan.Haslam@Sun.COM
250210807SJonathan.Haslam@Sun.COM options = &state.dts_options[0];
250310807SJonathan.Haslam@Sun.COM
250410807SJonathan.Haslam@Sun.COM mdb_printf("%<u>%-25s %s%</u>\n", "OPTION", "VALUE");
250510807SJonathan.Haslam@Sun.COM for (i = 0; i < DTRACEOPT_MAX; i++) {
250610807SJonathan.Haslam@Sun.COM if (options[i] == DTRACEOPT_UNSET) {
250710807SJonathan.Haslam@Sun.COM mdb_printf("%-25s %s\n",
250810807SJonathan.Haslam@Sun.COM _dtrace_options[i].dtop_optstr, "UNSET");
250910807SJonathan.Haslam@Sun.COM } else {
251010807SJonathan.Haslam@Sun.COM (void) _dtrace_options[i].dtop_func(options[i],
251110807SJonathan.Haslam@Sun.COM val, 32);
251210807SJonathan.Haslam@Sun.COM mdb_printf("%-25s %s\n",
251310807SJonathan.Haslam@Sun.COM _dtrace_options[i].dtop_optstr, val);
251410807SJonathan.Haslam@Sun.COM }
251510807SJonathan.Haslam@Sun.COM }
251610807SJonathan.Haslam@Sun.COM
251710807SJonathan.Haslam@Sun.COM return (DCMD_OK);
251810807SJonathan.Haslam@Sun.COM }
251910807SJonathan.Haslam@Sun.COM
252010807SJonathan.Haslam@Sun.COM static int
pid2state_init(mdb_walk_state_t * wsp)252110807SJonathan.Haslam@Sun.COM pid2state_init(mdb_walk_state_t *wsp)
252210807SJonathan.Haslam@Sun.COM {
252310807SJonathan.Haslam@Sun.COM dtrace_state_data_t *data;
252410807SJonathan.Haslam@Sun.COM uintptr_t devi;
252510807SJonathan.Haslam@Sun.COM uintptr_t proc;
252610807SJonathan.Haslam@Sun.COM struct dev_info info;
252710807SJonathan.Haslam@Sun.COM pid_t pid = (pid_t)wsp->walk_addr;
252810807SJonathan.Haslam@Sun.COM
252910807SJonathan.Haslam@Sun.COM if (wsp->walk_addr == NULL) {
253010807SJonathan.Haslam@Sun.COM mdb_warn("pid2state walk requires PID\n");
253110807SJonathan.Haslam@Sun.COM return (WALK_ERR);
253210807SJonathan.Haslam@Sun.COM }
253310807SJonathan.Haslam@Sun.COM
253410807SJonathan.Haslam@Sun.COM data = mdb_zalloc(sizeof (dtrace_state_data_t), UM_SLEEP | UM_GC);
253510807SJonathan.Haslam@Sun.COM
253610807SJonathan.Haslam@Sun.COM if (mdb_readvar(&data->dtsd_softstate, "dtrace_softstate") == -1) {
253710807SJonathan.Haslam@Sun.COM mdb_warn("failed to read 'dtrace_softstate'");
253810807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
253910807SJonathan.Haslam@Sun.COM }
254010807SJonathan.Haslam@Sun.COM
254110807SJonathan.Haslam@Sun.COM if ((proc = mdb_pid2proc(pid, NULL)) == NULL) {
254210807SJonathan.Haslam@Sun.COM mdb_warn("PID 0t%d not found\n", pid);
254310807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
254410807SJonathan.Haslam@Sun.COM }
254510807SJonathan.Haslam@Sun.COM
254610807SJonathan.Haslam@Sun.COM if (mdb_readvar(&devi, "dtrace_devi") == -1) {
254710807SJonathan.Haslam@Sun.COM mdb_warn("failed to read 'dtrace_devi'");
254810807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
254910807SJonathan.Haslam@Sun.COM }
255010807SJonathan.Haslam@Sun.COM
255110807SJonathan.Haslam@Sun.COM if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) {
255210807SJonathan.Haslam@Sun.COM mdb_warn("failed to read 'dev_info'");
255310807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
255410807SJonathan.Haslam@Sun.COM }
255510807SJonathan.Haslam@Sun.COM
255610807SJonathan.Haslam@Sun.COM data->dtsd_major = info.devi_major;
255710807SJonathan.Haslam@Sun.COM data->dtsd_proc = proc;
255810807SJonathan.Haslam@Sun.COM
255910807SJonathan.Haslam@Sun.COM wsp->walk_data = data;
256010807SJonathan.Haslam@Sun.COM
256110807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
256210807SJonathan.Haslam@Sun.COM }
256310807SJonathan.Haslam@Sun.COM
256410807SJonathan.Haslam@Sun.COM /*ARGSUSED*/
256510807SJonathan.Haslam@Sun.COM static int
pid2state_file(uintptr_t addr,struct file * f,dtrace_state_data_t * data)256610807SJonathan.Haslam@Sun.COM pid2state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data)
256710807SJonathan.Haslam@Sun.COM {
256810807SJonathan.Haslam@Sun.COM vnode_t vnode;
256910807SJonathan.Haslam@Sun.COM minor_t minor;
257010807SJonathan.Haslam@Sun.COM uintptr_t statep;
257110807SJonathan.Haslam@Sun.COM
257210807SJonathan.Haslam@Sun.COM /* Get the vnode for this file */
257310807SJonathan.Haslam@Sun.COM if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) {
257410807SJonathan.Haslam@Sun.COM mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode);
257510807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
257610807SJonathan.Haslam@Sun.COM }
257710807SJonathan.Haslam@Sun.COM
257810807SJonathan.Haslam@Sun.COM
257910807SJonathan.Haslam@Sun.COM /* Is this the dtrace device? */
258010807SJonathan.Haslam@Sun.COM if (getmajor(vnode.v_rdev) != data->dtsd_major)
258110807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
258210807SJonathan.Haslam@Sun.COM
258310807SJonathan.Haslam@Sun.COM /* Get the minor number for this device entry */
258410807SJonathan.Haslam@Sun.COM minor = getminor(vnode.v_rdev);
258510807SJonathan.Haslam@Sun.COM
258610807SJonathan.Haslam@Sun.COM if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor,
258710807SJonathan.Haslam@Sun.COM &statep, NULL, 0) == -1) {
258810807SJonathan.Haslam@Sun.COM mdb_warn("failed to read softstate for minor %d", minor);
258910807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
259010807SJonathan.Haslam@Sun.COM }
259110807SJonathan.Haslam@Sun.COM
259210807SJonathan.Haslam@Sun.COM mdb_printf("%p\n", statep);
259310807SJonathan.Haslam@Sun.COM
259410807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
259510807SJonathan.Haslam@Sun.COM }
259610807SJonathan.Haslam@Sun.COM
259710807SJonathan.Haslam@Sun.COM static int
pid2state_step(mdb_walk_state_t * wsp)259810807SJonathan.Haslam@Sun.COM pid2state_step(mdb_walk_state_t *wsp)
259910807SJonathan.Haslam@Sun.COM {
260010807SJonathan.Haslam@Sun.COM dtrace_state_data_t *ds = wsp->walk_data;
260110807SJonathan.Haslam@Sun.COM
260210807SJonathan.Haslam@Sun.COM if (mdb_pwalk("file",
260310807SJonathan.Haslam@Sun.COM (mdb_walk_cb_t)pid2state_file, ds, ds->dtsd_proc) == -1) {
260410807SJonathan.Haslam@Sun.COM mdb_warn("couldn't walk 'file' for proc %p", ds->dtsd_proc);
260510807SJonathan.Haslam@Sun.COM return (WALK_ERR);
260610807SJonathan.Haslam@Sun.COM }
260710807SJonathan.Haslam@Sun.COM
260810807SJonathan.Haslam@Sun.COM return (WALK_DONE);
260910807SJonathan.Haslam@Sun.COM }
261010807SJonathan.Haslam@Sun.COM
261110807SJonathan.Haslam@Sun.COM /*ARGSUSED*/
261210807SJonathan.Haslam@Sun.COM static int
dtrace_probes_walk(uintptr_t addr,void * ignored,uintptr_t * target)261310807SJonathan.Haslam@Sun.COM dtrace_probes_walk(uintptr_t addr, void *ignored, uintptr_t *target)
261410807SJonathan.Haslam@Sun.COM {
261510807SJonathan.Haslam@Sun.COM dtrace_ecb_t ecb;
261610807SJonathan.Haslam@Sun.COM dtrace_probe_t probe;
261710807SJonathan.Haslam@Sun.COM dtrace_probedesc_t pd;
261810807SJonathan.Haslam@Sun.COM
261910807SJonathan.Haslam@Sun.COM if (addr == NULL)
262010807SJonathan.Haslam@Sun.COM return (WALK_ERR);
262110807SJonathan.Haslam@Sun.COM
262210807SJonathan.Haslam@Sun.COM if (mdb_vread(&ecb, sizeof (dtrace_ecb_t), addr) == -1) {
262310807SJonathan.Haslam@Sun.COM mdb_warn("failed to read ecb %p\n", addr);
262410807SJonathan.Haslam@Sun.COM return (WALK_ERR);
262510807SJonathan.Haslam@Sun.COM }
262610807SJonathan.Haslam@Sun.COM
262710807SJonathan.Haslam@Sun.COM if (ecb.dte_probe == NULL)
262810807SJonathan.Haslam@Sun.COM return (WALK_ERR);
262910807SJonathan.Haslam@Sun.COM
263010807SJonathan.Haslam@Sun.COM if (mdb_vread(&probe, sizeof (dtrace_probe_t),
263110807SJonathan.Haslam@Sun.COM (uintptr_t)ecb.dte_probe) == -1) {
263210807SJonathan.Haslam@Sun.COM mdb_warn("failed to read probe %p\n", ecb.dte_probe);
263310807SJonathan.Haslam@Sun.COM return (WALK_ERR);
263410807SJonathan.Haslam@Sun.COM }
263510807SJonathan.Haslam@Sun.COM
263610807SJonathan.Haslam@Sun.COM pd.dtpd_id = probe.dtpr_id;
263710807SJonathan.Haslam@Sun.COM dtracemdb_probe(NULL, &pd);
263810807SJonathan.Haslam@Sun.COM
263910807SJonathan.Haslam@Sun.COM mdb_printf("%5d %10s %17s %33s %s\n", pd.dtpd_id, pd.dtpd_provider,
264010807SJonathan.Haslam@Sun.COM pd.dtpd_mod, pd.dtpd_func, pd.dtpd_name);
264110807SJonathan.Haslam@Sun.COM
264210807SJonathan.Haslam@Sun.COM return (WALK_NEXT);
264310807SJonathan.Haslam@Sun.COM }
264410807SJonathan.Haslam@Sun.COM
264510807SJonathan.Haslam@Sun.COM static void
dtrace_probes_help(void)264610807SJonathan.Haslam@Sun.COM dtrace_probes_help(void)
264710807SJonathan.Haslam@Sun.COM {
264810807SJonathan.Haslam@Sun.COM mdb_printf("Given a dtrace_state_t structure, displays all "
264910807SJonathan.Haslam@Sun.COM "its active enablings. If no\nstate structure is provided, "
265010807SJonathan.Haslam@Sun.COM "all available probes are listed.\n");
265110807SJonathan.Haslam@Sun.COM }
265210807SJonathan.Haslam@Sun.COM
265310807SJonathan.Haslam@Sun.COM /*ARGSUSED*/
265410807SJonathan.Haslam@Sun.COM static int
dtrace_probes(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)265510807SJonathan.Haslam@Sun.COM dtrace_probes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
265610807SJonathan.Haslam@Sun.COM {
265710807SJonathan.Haslam@Sun.COM dtrace_probedesc_t pd;
265810807SJonathan.Haslam@Sun.COM uintptr_t caddr, base, paddr;
265910807SJonathan.Haslam@Sun.COM int nprobes, i;
266010807SJonathan.Haslam@Sun.COM
266110807SJonathan.Haslam@Sun.COM mdb_printf("%5s %10s %17s %33s %s\n",
266210807SJonathan.Haslam@Sun.COM "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
266310807SJonathan.Haslam@Sun.COM
266410807SJonathan.Haslam@Sun.COM if (!(flags & DCMD_ADDRSPEC)) {
266510807SJonathan.Haslam@Sun.COM /*
266610807SJonathan.Haslam@Sun.COM * If no argument is provided just display all available
266710807SJonathan.Haslam@Sun.COM * probes.
266810807SJonathan.Haslam@Sun.COM */
266910807SJonathan.Haslam@Sun.COM if (mdb_readvar(&base, "dtrace_probes") == -1) {
267010807SJonathan.Haslam@Sun.COM mdb_warn("failed to read 'dtrace_probes'");
267110807SJonathan.Haslam@Sun.COM return (-1);
267210807SJonathan.Haslam@Sun.COM }
267310807SJonathan.Haslam@Sun.COM
267410807SJonathan.Haslam@Sun.COM if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) {
267510807SJonathan.Haslam@Sun.COM mdb_warn("failed to read 'dtrace_nprobes'");
267610807SJonathan.Haslam@Sun.COM return (-1);
267710807SJonathan.Haslam@Sun.COM }
267810807SJonathan.Haslam@Sun.COM
267910807SJonathan.Haslam@Sun.COM for (i = 0; i < nprobes; i++) {
268010807SJonathan.Haslam@Sun.COM caddr = base + i * sizeof (dtrace_probe_t *);
268110807SJonathan.Haslam@Sun.COM
268210807SJonathan.Haslam@Sun.COM if (mdb_vread(&paddr, sizeof (paddr), caddr) == -1) {
268310807SJonathan.Haslam@Sun.COM mdb_warn("couldn't read probe pointer at %p",
268410807SJonathan.Haslam@Sun.COM caddr);
268510807SJonathan.Haslam@Sun.COM continue;
268610807SJonathan.Haslam@Sun.COM }
268710807SJonathan.Haslam@Sun.COM
268810807SJonathan.Haslam@Sun.COM if (paddr == NULL)
268910807SJonathan.Haslam@Sun.COM continue;
269010807SJonathan.Haslam@Sun.COM
269110807SJonathan.Haslam@Sun.COM pd.dtpd_id = i + 1;
269210807SJonathan.Haslam@Sun.COM if (dtracemdb_probe(NULL, &pd) == 0) {
269310807SJonathan.Haslam@Sun.COM mdb_printf("%5d %10s %17s %33s %s\n",
269410807SJonathan.Haslam@Sun.COM pd.dtpd_id, pd.dtpd_provider,
269510807SJonathan.Haslam@Sun.COM pd.dtpd_mod, pd.dtpd_func, pd.dtpd_name);
269610807SJonathan.Haslam@Sun.COM }
269710807SJonathan.Haslam@Sun.COM }
269810807SJonathan.Haslam@Sun.COM } else {
269910807SJonathan.Haslam@Sun.COM if (mdb_pwalk("dtrace_ecb", (mdb_walk_cb_t)dtrace_probes_walk,
270010807SJonathan.Haslam@Sun.COM NULL, addr) == -1) {
270110807SJonathan.Haslam@Sun.COM mdb_warn("couldn't walk 'dtrace_ecb'");
270210807SJonathan.Haslam@Sun.COM return (DCMD_ERR);
270310807SJonathan.Haslam@Sun.COM }
270410807SJonathan.Haslam@Sun.COM }
270510807SJonathan.Haslam@Sun.COM
270610807SJonathan.Haslam@Sun.COM return (DCMD_OK);
270710807SJonathan.Haslam@Sun.COM }
270810807SJonathan.Haslam@Sun.COM
2709265Smws const mdb_dcmd_t kernel_dcmds[] = {
27100Sstevel@tonic-gate { "id2probe", ":", "translate a dtrace_id_t to a dtrace_probe_t",
27110Sstevel@tonic-gate id2probe },
27120Sstevel@tonic-gate { "dtrace", ":[-c cpu]", "print dtrace(1M)-like output",
27130Sstevel@tonic-gate dtrace, dtrace_help },
27140Sstevel@tonic-gate { "dtrace_errhash", ":", "print DTrace error hash", dtrace_errhash },
27150Sstevel@tonic-gate { "dtrace_helptrace", ":", "print DTrace helper trace",
27160Sstevel@tonic-gate dtrace_helptrace },
27170Sstevel@tonic-gate { "dtrace_state", ":", "print active DTrace consumers", dtrace_state,
27180Sstevel@tonic-gate dtrace_state_help },
27190Sstevel@tonic-gate { "dtrace_aggstat", ":",
27200Sstevel@tonic-gate "print DTrace aggregation hash statistics", dtrace_aggstat },
27210Sstevel@tonic-gate { "dtrace_dynstat", ":",
27220Sstevel@tonic-gate "print DTrace dynamic variable hash statistics", dtrace_dynstat },
272310807SJonathan.Haslam@Sun.COM { "dtrace_options", ":",
272410807SJonathan.Haslam@Sun.COM "print a DTrace consumer's current tuneable options",
272510807SJonathan.Haslam@Sun.COM dtrace_options, dtrace_options_help },
272610807SJonathan.Haslam@Sun.COM { "dtrace_probes", "?", "print a DTrace consumer's enabled probes",
272710807SJonathan.Haslam@Sun.COM dtrace_probes, dtrace_probes_help },
27280Sstevel@tonic-gate { NULL }
27290Sstevel@tonic-gate };
27300Sstevel@tonic-gate
2731265Smws const mdb_walker_t kernel_walkers[] = {
27320Sstevel@tonic-gate { "dtrace_errhash", "walk hash of DTrace error messasges",
27330Sstevel@tonic-gate dtrace_errhash_init, dtrace_errhash_step },
27340Sstevel@tonic-gate { "dtrace_helptrace", "walk DTrace helper trace entries",
27350Sstevel@tonic-gate dtrace_helptrace_init, dtrace_helptrace_step },
27360Sstevel@tonic-gate { "dtrace_state", "walk DTrace per-consumer softstate",
27370Sstevel@tonic-gate dtrace_state_init, dtrace_state_step },
27380Sstevel@tonic-gate { "dtrace_aggkey", "walk DTrace aggregation keys",
27390Sstevel@tonic-gate dtrace_aggkey_init, dtrace_aggkey_step, dtrace_aggkey_fini },
27400Sstevel@tonic-gate { "dtrace_dynvar", "walk DTrace dynamic variables",
27410Sstevel@tonic-gate dtrace_dynvar_init, dtrace_dynvar_step, dtrace_dynvar_fini },
27423276Sjhaslam { "dtrace_ecb", "walk a DTrace consumer's enabling control blocks",
27433276Sjhaslam dtrace_ecb_init, dtrace_ecb_step },
274410807SJonathan.Haslam@Sun.COM { "pid2state", "walk a processes dtrace_state structures",
274510807SJonathan.Haslam@Sun.COM pid2state_init, pid2state_step },
27460Sstevel@tonic-gate { NULL }
27470Sstevel@tonic-gate };
2748