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