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