17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5ac44896ahl * Common Development and Distribution License (the "License").
6ac44896ahl * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21ac44896ahl
227c478bdstevel@tonic-gate/*
23c9a6ea2Bryan Cantrill * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247994dfdDan Kimmel * Copyright (c) 2013 by Delphix. All rights reserved.
25c3377eeJohn Levon * Copyright 2019 Joyent, Inc.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * explicitly define DTRACE_ERRDEBUG to pull in definition of dtrace_errhash_t
307c478bdstevel@tonic-gate * explicitly define _STDARG_H to avoid stdarg.h/varargs.h u/k defn conflict
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate#define	DTRACE_ERRDEBUG
337c478bdstevel@tonic-gate#define	_STDARG_H
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate#include <mdb/mdb_param.h>
367c478bdstevel@tonic-gate#include <mdb/mdb_modapi.h>
377c478bdstevel@tonic-gate#include <mdb/mdb_ks.h>
387c478bdstevel@tonic-gate#include <sys/dtrace_impl.h>
397c478bdstevel@tonic-gate#include <sys/vmem_impl.h>
407c478bdstevel@tonic-gate#include <sys/ddi_impldefs.h>
417c478bdstevel@tonic-gate#include <sys/sysmacros.h>
427c478bdstevel@tonic-gate#include <sys/kobj.h>
437c478bdstevel@tonic-gate#include <dtrace.h>
447c478bdstevel@tonic-gate#include <alloca.h>
457c478bdstevel@tonic-gate#include <ctype.h>
467c478bdstevel@tonic-gate#include <errno.h>
477c478bdstevel@tonic-gate#include <math.h>
487994dfdDan Kimmel#include <stdio.h>
497994dfdDan Kimmel#include <unistd.h>
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate/*ARGSUSED*/
527c478bdstevel@tonic-gateint
537c478bdstevel@tonic-gateid2probe(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
547c478bdstevel@tonic-gate{
55892ad16Toomas Soome	uintptr_t probe = 0;
567c478bdstevel@tonic-gate	uintptr_t probes;
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC))
597c478bdstevel@tonic-gate		return (DCMD_USAGE);
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	if (addr == DTRACE_IDNONE || addr > UINT32_MAX)
627c478bdstevel@tonic-gate		goto out;
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	if (mdb_readvar(&probes, "dtrace_probes") == -1) {
657c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_probes'");
667c478bdstevel@tonic-gate		return (DCMD_ERR);
677c478bdstevel@tonic-gate	}
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate	probes += (addr - 1) * sizeof (dtrace_probe_t *);
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate	if (mdb_vread(&probe, sizeof (uintptr_t), probes) == -1) {
727c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_probes[%d]", addr - 1);
737c478bdstevel@tonic-gate		return (DCMD_ERR);
747c478bdstevel@tonic-gate	}
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gateout:
777c478bdstevel@tonic-gate	mdb_printf("%p\n", probe);
787c478bdstevel@tonic-gate	return (DCMD_OK);
797c478bdstevel@tonic-gate}
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gatevoid
827c478bdstevel@tonic-gatedtrace_help(void)
837c478bdstevel@tonic-gate{
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	mdb_printf("Given a dtrace_state_t structure that represents a "
867c478bdstevel@tonic-gate	    "DTrace consumer, prints\n"
877c478bdstevel@tonic-gate	    "dtrace(1M)-like output for in-kernel DTrace data.  (The "
887c478bdstevel@tonic-gate	    "dtrace_state_t\n"
897c478bdstevel@tonic-gate	    "structures for all DTrace consumers may be obtained by running "
907c478bdstevel@tonic-gate	    "the \n"
917c478bdstevel@tonic-gate	    "::dtrace_state dcmd.)   When data is present on multiple CPUs, "
927c478bdstevel@tonic-gate	    "data are\n"
937c478bdstevel@tonic-gate	    "presented in CPU order, with records within each CPU ordered "
947c478bdstevel@tonic-gate	    "oldest to \n"
957c478bdstevel@tonic-gate	    "youngest.  Options:\n\n"
967c478bdstevel@tonic-gate	    "-c cpu     Only provide output for specified CPU.\n");
977c478bdstevel@tonic-gate}
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gatestatic int
1007c478bdstevel@tonic-gatedtracemdb_eprobe(dtrace_state_t *state, dtrace_eprobedesc_t *epd)
1017c478bdstevel@tonic-gate{
1027c478bdstevel@tonic-gate	dtrace_epid_t epid = epd->dtepd_epid;
1037c478bdstevel@tonic-gate	dtrace_probe_t probe;
1047c478bdstevel@tonic-gate	dtrace_ecb_t ecb;
1057c478bdstevel@tonic-gate	uintptr_t addr, paddr, ap;
1067c478bdstevel@tonic-gate	dtrace_action_t act;
1077c478bdstevel@tonic-gate	int nactions, nrecs;
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate	addr = (uintptr_t)state->dts_ecbs +
1107c478bdstevel@tonic-gate	    (epid - 1) * sizeof (dtrace_ecb_t *);
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gate	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
1137c478bdstevel@tonic-gate		mdb_warn("failed to read ecb for epid %d", epid);
1147c478bdstevel@tonic-gate		return (-1);
1157c478bdstevel@tonic-gate	}
1167c478bdstevel@tonic-gate
117892ad16Toomas Soome	if (addr == 0) {
1187c478bdstevel@tonic-gate		mdb_warn("epid %d doesn't match an ecb\n", epid);
1197c478bdstevel@tonic-gate		return (-1);
1207c478bdstevel@tonic-gate	}
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate	if (mdb_vread(&ecb, sizeof (ecb), addr) == -1) {
1237c478bdstevel@tonic-gate		mdb_warn("failed to read ecb at %p", addr);
1247c478bdstevel@tonic-gate		return (-1);
1257c478bdstevel@tonic-gate	}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	paddr = (uintptr_t)ecb.dte_probe;
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
1307c478bdstevel@tonic-gate		mdb_warn("failed to read probe for ecb %p", addr);
1317c478bdstevel@tonic-gate		return (-1);
1327c478bdstevel@tonic-gate	}
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	/*
1357c478bdstevel@tonic-gate	 * This is a little painful:  in order to find the number of actions,
1367c478bdstevel@tonic-gate	 * we need to first walk through them.
1377c478bdstevel@tonic-gate	 */
138892ad16Toomas Soome	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != 0; ) {
1397c478bdstevel@tonic-gate		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1407c478bdstevel@tonic-gate			mdb_warn("failed to read action %p on ecb %p",
1417c478bdstevel@tonic-gate			    ap, addr);
1427c478bdstevel@tonic-gate			return (-1);
1437c478bdstevel@tonic-gate		}
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple)
1467c478bdstevel@tonic-gate			nactions++;
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate		ap = (uintptr_t)act.dta_next;
1497c478bdstevel@tonic-gate	}
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate	nrecs = epd->dtepd_nrecs;
1527c478bdstevel@tonic-gate	epd->dtepd_nrecs = nactions;
1537c478bdstevel@tonic-gate	epd->dtepd_probeid = probe.dtpr_id;
1547c478bdstevel@tonic-gate	epd->dtepd_uarg = ecb.dte_uarg;
1557c478bdstevel@tonic-gate	epd->dtepd_size = ecb.dte_size;
1567c478bdstevel@tonic-gate
157892ad16Toomas Soome	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != 0; ) {
1587c478bdstevel@tonic-gate		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1597c478bdstevel@tonic-gate			mdb_warn("failed to read action %p on ecb %p",
1607c478bdstevel@tonic-gate			    ap, addr);
1617c478bdstevel@tonic-gate			return (-1);
1627c478bdstevel@tonic-gate		}
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) {
1657c478bdstevel@tonic-gate			if (nrecs-- == 0)
1667c478bdstevel@tonic-gate				break;
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate			epd->dtepd_rec[nactions++] = act.dta_rec;
1697c478bdstevel@tonic-gate		}
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate		ap = (uintptr_t)act.dta_next;
1727c478bdstevel@tonic-gate	}
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate	return (0);
1757c478bdstevel@tonic-gate}
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate/*ARGSUSED*/
1787c478bdstevel@tonic-gatestatic int
1797c478bdstevel@tonic-gatedtracemdb_probe(dtrace_state_t *state, dtrace_probedesc_t *pd)
1807c478bdstevel@tonic-gate{
1817c478bdstevel@tonic-gate	uintptr_t base, addr, paddr, praddr;
1827c478bdstevel@tonic-gate	int nprobes, i;
1837c478bdstevel@tonic-gate	dtrace_probe_t probe;
1847c478bdstevel@tonic-gate	dtrace_provider_t prov;
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate	if (pd->dtpd_id == DTRACE_IDNONE)
1877c478bdstevel@tonic-gate		pd->dtpd_id++;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	if (mdb_readvar(&base, "dtrace_probes") == -1) {
1907c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_probes'");
1917c478bdstevel@tonic-gate		return (-1);
1927c478bdstevel@tonic-gate	}
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) {
1957c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_nprobes'");
1967c478bdstevel@tonic-gate		return (-1);
1977c478bdstevel@tonic-gate	}
1987c478bdstevel@tonic-gate
1997c478bdstevel@tonic-gate	for (i = pd->dtpd_id; i <= nprobes; i++) {
2007c478bdstevel@tonic-gate		addr = base + (i - 1) * sizeof (dtrace_probe_t *);
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate		if (mdb_vread(&paddr, sizeof (paddr), addr) == -1) {
2037c478bdstevel@tonic-gate			mdb_warn("couldn't read probe pointer at %p", addr);
2047c478bdstevel@tonic-gate			return (-1);
2057c478bdstevel@tonic-gate		}
2067c478bdstevel@tonic-gate
207892ad16Toomas Soome		if (paddr != 0)
2087c478bdstevel@tonic-gate			break;
2097c478bdstevel@tonic-gate	}
2107c478bdstevel@tonic-gate
211892ad16Toomas Soome	if (paddr == 0) {
2127c478bdstevel@tonic-gate		errno = ESRCH;
2137c478bdstevel@tonic-gate		return (-1);
2147c478bdstevel@tonic-gate	}
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
2177c478bdstevel@tonic-gate		mdb_warn("couldn't read probe at %p", paddr);
2187c478bdstevel@tonic-gate		return (-1);
2197c478bdstevel@tonic-gate	}
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate	pd->dtpd_id = probe.dtpr_id;
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate	if (mdb_vread(pd->dtpd_name, DTRACE_NAMELEN,
2247c478bdstevel@tonic-gate	    (uintptr_t)probe.dtpr_name) == -1) {
2257c478bdstevel@tonic-gate		mdb_warn("failed to read probe name for probe %p", paddr);
2267c478bdstevel@tonic-gate		return (-1);
2277c478bdstevel@tonic-gate	}
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate	if (mdb_vread(pd->dtpd_func, DTRACE_FUNCNAMELEN,
2307c478bdstevel@tonic-gate	    (uintptr_t)probe.dtpr_func) == -1) {
2317c478bdstevel@tonic-gate		mdb_warn("failed to read function name for probe %p", paddr);
2327c478bdstevel@tonic-gate		return (-1);
2337c478bdstevel@tonic-gate	}
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	if (mdb_vread(pd->dtpd_mod, DTRACE_MODNAMELEN,
2367c478bdstevel@tonic-gate	    (uintptr_t)probe.dtpr_mod) == -1) {
2377c478bdstevel@tonic-gate		mdb_warn("failed to read module name for probe %p", paddr);
2387c478bdstevel@tonic-gate		return (-1);
2397c478bdstevel@tonic-gate	}
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate	praddr = (uintptr_t)probe.dtpr_provider;
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate	if (mdb_vread(&prov, sizeof (prov), praddr) == -1) {
2447c478bdstevel@tonic-gate		mdb_warn("failed to read provider for probe %p", paddr);
2457c478bdstevel@tonic-gate		return (-1);
2467c478bdstevel@tonic-gate	}
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate	if (mdb_vread(pd->dtpd_provider, DTRACE_PROVNAMELEN,
2497c478bdstevel@tonic-gate	    (uintptr_t)prov.dtpv_name) == -1) {
2507c478bdstevel@tonic-gate		mdb_warn("failed to read provider name for probe %p", paddr);
2517c478bdstevel@tonic-gate		return (-1);
2527c478bdstevel@tonic-gate	}
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate	return (0);
2557c478bdstevel@tonic-gate}
2567c478bdstevel@tonic-gate
2577c478bdstevel@tonic-gate/*ARGSUSED*/
2587c478bdstevel@tonic-gatestatic int
2597c478bdstevel@tonic-gatedtracemdb_aggdesc(dtrace_state_t *state, dtrace_aggdesc_t *agd)
2607c478bdstevel@tonic-gate{
2617c478bdstevel@tonic-gate	dtrace_aggid_t aggid = agd->dtagd_id;
2627c478bdstevel@tonic-gate	dtrace_aggregation_t agg;
2637c478bdstevel@tonic-gate	dtrace_ecb_t ecb;
2647c478bdstevel@tonic-gate	uintptr_t addr, eaddr, ap, last;
2657c478bdstevel@tonic-gate	dtrace_action_t act;
2667c478bdstevel@tonic-gate	dtrace_recdesc_t *lrec;
2677c478bdstevel@tonic-gate	int nactions, nrecs;
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate	addr = (uintptr_t)state->dts_aggregations +
2707c478bdstevel@tonic-gate	    (aggid - 1) * sizeof (dtrace_aggregation_t *);
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
2737c478bdstevel@tonic-gate		mdb_warn("failed to read aggregation for aggid %d", aggid);
2747c478bdstevel@tonic-gate		return (-1);
2757c478bdstevel@tonic-gate	}
2767c478bdstevel@tonic-gate
277892ad16Toomas Soome	if (addr == 0) {
2787c478bdstevel@tonic-gate		mdb_warn("aggid %d doesn't match an aggregation\n", aggid);
2797c478bdstevel@tonic-gate		return (-1);
2807c478bdstevel@tonic-gate	}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	if (mdb_vread(&agg, sizeof (agg), addr) == -1) {
2837c478bdstevel@tonic-gate		mdb_warn("failed to read aggregation at %p", addr);
2847c478bdstevel@tonic-gate		return (-1);
2857c478bdstevel@tonic-gate	}
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate	eaddr = (uintptr_t)agg.dtag_ecb;
2887c478bdstevel@tonic-gate
2897c478bdstevel@tonic-gate	if (mdb_vread(&ecb, sizeof (ecb), eaddr) == -1) {
2907c478bdstevel@tonic-gate		mdb_warn("failed to read ecb for aggregation %p", addr);
2917c478bdstevel@tonic-gate		return (-1);
2927c478bdstevel@tonic-gate	}
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate	last = (uintptr_t)addr + offsetof(dtrace_aggregation_t, dtag_action);
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate	/*
2977c478bdstevel@tonic-gate	 * This is a little painful:  in order to find the number of actions,
2987c478bdstevel@tonic-gate	 * we need to first walk through them.
2997c478bdstevel@tonic-gate	 */
3007c478bdstevel@tonic-gate	ap = (uintptr_t)agg.dtag_first;
3017c478bdstevel@tonic-gate	nactions = 0;
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate	for (;;) {
3047c478bdstevel@tonic-gate		if (mdb_vread(&act, sizeof (act), ap) == -1) {
3057c478bdstevel@tonic-gate			mdb_warn("failed to read action %p on aggregation %p",
3067c478bdstevel@tonic-gate			    ap, addr);
3077c478bdstevel@tonic-gate			return (-1);
3087c478bdstevel@tonic-gate		}
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate		nactions++;
3117c478bdstevel@tonic-gate
3127c478bdstevel@tonic-gate		if (ap == last)
3137c478bdstevel@tonic-gate			break;
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate		ap = (uintptr_t)act.dta_next;
3167c478bdstevel@tonic-gate	}
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate	lrec = &act.dta_rec;
3197c478bdstevel@tonic-gate	agd->dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - agg.dtag_base;
3207c478bdstevel@tonic-gate
3217c478bdstevel@tonic-gate	nrecs = agd->dtagd_nrecs;
3227c478bdstevel@tonic-gate	agd->dtagd_nrecs = nactions;
3237c478bdstevel@tonic-gate	agd->dtagd_epid = ecb.dte_epid;
3247c478bdstevel@tonic-gate
3257c478bdstevel@tonic-gate	ap = (uintptr_t)agg.dtag_first;
3267c478bdstevel@tonic-gate	nactions = 0;
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate	for (;;) {
3297c478bdstevel@tonic-gate		dtrace_recdesc_t rec;
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate		if (mdb_vread(&act, sizeof (act), ap) == -1) {
3327c478bdstevel@tonic-gate			mdb_warn("failed to read action %p on aggregation %p",
3337c478bdstevel@tonic-gate			    ap, addr);
3347c478bdstevel@tonic-gate			return (-1);
3357c478bdstevel@tonic-gate		}
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate		if (nrecs-- == 0)
3387c478bdstevel@tonic-gate			break;
3397c478bdstevel@tonic-gate
3407c478bdstevel@tonic-gate		rec = act.dta_rec;
3417c478bdstevel@tonic-gate		rec.dtrd_offset -= agg.dtag_base;
3427c478bdstevel@tonic-gate		rec.dtrd_uarg = 0;
3437c478bdstevel@tonic-gate		agd->dtagd_rec[nactions++] = rec;
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate		if (ap == last)
3467c478bdstevel@tonic-gate			break;
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate		ap = (uintptr_t)act.dta_next;
3497c478bdstevel@tonic-gate	}
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	return (0);
3527c478bdstevel@tonic-gate}
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gatestatic int
3557c478bdstevel@tonic-gatedtracemdb_bufsnap(dtrace_buffer_t *which, dtrace_bufdesc_t *desc)
3567c478bdstevel@tonic-gate{
3577c478bdstevel@tonic-gate	uintptr_t addr;
3587c478bdstevel@tonic-gate	size_t bufsize;
3597c478bdstevel@tonic-gate	dtrace_buffer_t buf;
3607c478bdstevel@tonic-gate	caddr_t data = desc->dtbd_data;
3617c478bdstevel@tonic-gate	processorid_t max_cpuid, cpu = desc->dtbd_cpu;
3627c478bdstevel@tonic-gate
3637c478bdstevel@tonic-gate	if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
3647c478bdstevel@tonic-gate		mdb_warn("failed to read 'max_cpuid'");
3657c478bdstevel@tonic-gate		errno = EIO;
3667c478bdstevel@tonic-gate		return (-1);
3677c478bdstevel@tonic-gate	}
3687c478bdstevel@tonic-gate
3697c478bdstevel@tonic-gate	if (cpu < 0 || cpu > max_cpuid) {
3707c478bdstevel@tonic-gate		errno = EINVAL;
3717c478bdstevel@tonic-gate		return (-1);
3727c478bdstevel@tonic-gate	}
3737c478bdstevel@tonic-gate
3747c478bdstevel@tonic-gate	addr = (uintptr_t)which + cpu * sizeof (dtrace_buffer_t);
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
3777c478bdstevel@tonic-gate		mdb_warn("failed to read buffer description at %p", addr);
3787c478bdstevel@tonic-gate		errno = EIO;
3797c478bdstevel@tonic-gate		return (-1);
3807c478bdstevel@tonic-gate	}
3817c478bdstevel@tonic-gate
3827c478bdstevel@tonic-gate	if (buf.dtb_tomax == NULL) {
3837c478bdstevel@tonic-gate		errno = ENOENT;
3847c478bdstevel@tonic-gate		return (-1);
3857c478bdstevel@tonic-gate	}
3867c478bdstevel@tonic-gate
3877c478bdstevel@tonic-gate	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
3887c478bdstevel@tonic-gate		bufsize = buf.dtb_size;
3897c478bdstevel@tonic-gate	} else {
3907c478bdstevel@tonic-gate		bufsize = buf.dtb_offset;
3917c478bdstevel@tonic-gate	}
3927c478bdstevel@tonic-gate
3937c478bdstevel@tonic-gate	if (mdb_vread(data, bufsize, (uintptr_t)buf.dtb_tomax) == -1) {
3947c478bdstevel@tonic-gate		mdb_warn("couldn't read buffer for CPU %d", cpu);
3957c478bdstevel@tonic-gate		errno = EIO;
3967c478bdstevel@tonic-gate		return (-1);
3977c478bdstevel@tonic-gate	}
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gate	if (buf.dtb_offset > buf.dtb_size) {
4007c478bdstevel@tonic-gate		mdb_warn("buffer for CPU %d has corrupt offset\n", cpu);
4017c478bdstevel@tonic-gate		errno = EIO;
4027c478bdstevel@tonic-gate		return (-1);
4037c478bdstevel@tonic-gate	}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gate	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
4067c478bdstevel@tonic-gate		if (buf.dtb_xamot_offset > buf.dtb_size) {
4077c478bdstevel@tonic-gate			mdb_warn("ringbuffer for CPU %d has corrupt "
4087c478bdstevel@tonic-gate			    "wrapped offset\n", cpu);
4097c478bdstevel@tonic-gate			errno = EIO;
4107c478bdstevel@tonic-gate			return (-1);
4117c478bdstevel@tonic-gate		}
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate		/*
4147c478bdstevel@tonic-gate		 * If the ring buffer has wrapped, it needs to be polished.
4157c478bdstevel@tonic-gate		 * See the comment in dtrace_buffer_polish() for details.
4167c478bdstevel@tonic-gate		 */
4177c478bdstevel@tonic-gate		if (buf.dtb_offset < buf.dtb_xamot_offset) {
4187c478bdstevel@tonic-gate			bzero(data + buf.dtb_offset,
4197c478bdstevel@tonic-gate			    buf.dtb_xamot_offset - buf.dtb_offset);
4207c478bdstevel@tonic-gate		}
4217c478bdstevel@tonic-gate
4227c478bdstevel@tonic-gate		if (buf.dtb_offset > buf.dtb_xamot_offset) {
4237c478bdstevel@tonic-gate			bzero(data + buf.dtb_offset,
4247c478bdstevel@tonic-gate			    buf.dtb_size - buf.dtb_offset);
4257c478bdstevel@tonic-gate			bzero(data, buf.dtb_xamot_offset);
4267c478bdstevel@tonic-gate		}
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gate		desc->dtbd_oldest = buf.dtb_xamot_offset;
4297c478bdstevel@tonic-gate	} else {
4307c478bdstevel@tonic-gate		desc->dtbd_oldest = 0;
4317c478bdstevel@tonic-gate	}
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate	desc->dtbd_size = bufsize;
4347c478bdstevel@tonic-gate	desc->dtbd_drops = buf.dtb_drops;
4357c478bdstevel@tonic-gate	desc->dtbd_errors = buf.dtb_errors;
436a62da11Bryan Cantrill	desc->dtbd_timestamp = gethrtime();
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate	return (0);
4397c478bdstevel@tonic-gate}
4407c478bdstevel@tonic-gate
4417c478bdstevel@tonic-gate/*
442c9a6ea2Bryan Cantrill * This is essentially identical to its cousin in the kernel -- with the
443c9a6ea2Bryan Cantrill * notable exception that we automatically set DTRACEOPT_GRABANON if this
444c9a6ea2Bryan Cantrill * state is an anonymous enabling.
4457c478bdstevel@tonic-gate */
4467c478bdstevel@tonic-gatestatic dof_hdr_t *
447c9a6ea2Bryan Cantrilldtracemdb_dof_create(dtrace_state_t *state, int isanon)
4487c478bdstevel@tonic-gate{
4497c478bdstevel@tonic-gate	dof_hdr_t *dof;
4507c478bdstevel@tonic-gate	dof_sec_t *sec;
4517c478bdstevel@tonic-gate	dof_optdesc_t *opt;
4527c478bdstevel@tonic-gate	int i, len = sizeof (dof_hdr_t) +
4537c478bdstevel@tonic-gate	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
4547c478bdstevel@tonic-gate	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate	dof = mdb_zalloc(len, UM_SLEEP);
4577c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
4587c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
4597c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
4607c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
4617c478bdstevel@tonic-gate
4627c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
4637c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
464ac44896ahl	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
4657c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
4667c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
4677c478bdstevel@tonic-gate	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate	dof->dofh_flags = 0;
4707c478bdstevel@tonic-gate	dof->dofh_hdrsize = sizeof (dof_hdr_t);
4717c478bdstevel@tonic-gate	dof->dofh_secsize = sizeof (dof_sec_t);
4727c478bdstevel@tonic-gate	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
4737c478bdstevel@tonic-gate	dof->dofh_secoff = sizeof (dof_hdr_t);
4747c478bdstevel@tonic-gate	dof->dofh_loadsz = len;
4757c478bdstevel@tonic-gate	dof->dofh_filesz = len;
4767c478bdstevel@tonic-gate	dof->dofh_pad = 0;
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate	/*
4797c478bdstevel@tonic-gate	 * Fill in the option section header...
4807c478bdstevel@tonic-gate	 */
4817c478bdstevel@tonic-gate	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
4827c478bdstevel@tonic-gate	sec->dofs_type = DOF_SECT_OPTDESC;
4837c478bdstevel@tonic-gate	sec->dofs_align = sizeof (uint64_t);
4847c478bdstevel@tonic-gate	sec->dofs_flags = DOF_SECF_LOAD;
4857c478bdstevel@tonic-gate	sec->dofs_entsize = sizeof (dof_optdesc_t);
4867c478bdstevel@tonic-gate
4877c478bdstevel@tonic-gate	opt = (dof_optdesc_t *)((uintptr_t)sec +
4887c478bdstevel@tonic-gate	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
4897c478bdstevel@tonic-gate
4907c478bdstevel@tonic-gate	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
4917c478bdstevel@tonic-gate	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate	for (i = 0; i < DTRACEOPT_MAX; i++) {
4947c478bdstevel@tonic-gate		opt[i].dofo_option = i;
4957c478bdstevel@tonic-gate		opt[i].dofo_strtab = DOF_SECIDX_NONE;
4967c478bdstevel@tonic-gate		opt[i].dofo_value = state->dts_options[i];
4977c478bdstevel@tonic-gate	}
4987c478bdstevel@tonic-gate
499c9a6ea2Bryan Cantrill	if (isanon)
500c9a6ea2Bryan Cantrill		opt[DTRACEOPT_GRABANON].dofo_value = 1;
501c9a6ea2Bryan Cantrill
5027c478bdstevel@tonic-gate	return (dof);
5037c478bdstevel@tonic-gate}
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gatestatic int
5067c478bdstevel@tonic-gatedtracemdb_format(dtrace_state_t *state, dtrace_fmtdesc_t *desc)
5077c478bdstevel@tonic-gate{
5087c478bdstevel@tonic-gate	uintptr_t addr, faddr;
5097c478bdstevel@tonic-gate	char c;
5107c478bdstevel@tonic-gate	int len = 0;
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate	if (desc->dtfd_format == 0 || desc->dtfd_format > state->dts_nformats) {
5137c478bdstevel@tonic-gate		errno = EINVAL;
5147c478bdstevel@tonic-gate		return (-1);
5157c478bdstevel@tonic-gate	}
5167c478bdstevel@tonic-gate
5177c478bdstevel@tonic-gate	faddr = (uintptr_t)state->dts_formats +
5187c478bdstevel@tonic-gate	    (desc->dtfd_format - 1) * sizeof (char *);
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate	if (mdb_vread(&addr, sizeof (addr), faddr) == -1) {
5217c478bdstevel@tonic-gate		mdb_warn("failed to read format string pointer at %p", faddr);
5227c478bdstevel@tonic-gate		return (-1);
5237c478bdstevel@tonic-gate	}
5247c478bdstevel@tonic-gate
5257c478bdstevel@tonic-gate	do {
5267c478bdstevel@tonic-gate		if (mdb_vread(&c, sizeof (c), addr + len++) == -1) {
5277c478bdstevel@tonic-gate			mdb_warn("failed to read format string at %p", addr);
5287c478bdstevel@tonic-gate			return (-1);
5297c478bdstevel@tonic-gate		}
5307c478bdstevel@tonic-gate	} while (c != '\0');
5317c478bdstevel@tonic-gate
5327c478bdstevel@tonic-gate	if (len > desc->dtfd_length) {
5337c478bdstevel@tonic-gate		desc->dtfd_length = len;
5347c478bdstevel@tonic-gate		return (0);
5357c478bdstevel@tonic-gate	}
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate	if (mdb_vread(desc->dtfd_string, len, addr) == -1) {
5387c478bdstevel@tonic-gate		mdb_warn("failed to reread format string at %p", addr);
5397c478bdstevel@tonic-gate		return (-1);
5407c478bdstevel@tonic-gate	}
5417c478bdstevel@tonic-gate
5427c478bdstevel@tonic-gate	return (0);
5437c478bdstevel@tonic-gate}
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gatestatic int
5467c478bdstevel@tonic-gatedtracemdb_status(dtrace_state_t *state, dtrace_status_t *status)
5477c478bdstevel@tonic-gate{
5487c478bdstevel@tonic-gate	dtrace_dstate_t *dstate;
5497c478bdstevel@tonic-gate	int i, j;
5507c478bdstevel@tonic-gate	uint64_t nerrs;
5517c478bdstevel@tonic-gate	uintptr_t addr;
5527c478bdstevel@tonic-gate	int ncpu;
5537c478bdstevel@tonic-gate
5547c478bdstevel@tonic-gate	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
5557c478bdstevel@tonic-gate		mdb_warn("failed to read '_ncpu'");
5567c478bdstevel@tonic-gate		return (DCMD_ERR);
5577c478bdstevel@tonic-gate	}
5587c478bdstevel@tonic-gate
5597c478bdstevel@tonic-gate	bzero(status, sizeof (dtrace_status_t));
5607c478bdstevel@tonic-gate
5617c478bdstevel@tonic-gate	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
5627c478bdstevel@tonic-gate		errno = ENOENT;
5637c478bdstevel@tonic-gate		return (-1);
5647c478bdstevel@tonic-gate	}
5657c478bdstevel@tonic-gate
5667c478bdstevel@tonic-gate	/*
5677c478bdstevel@tonic-gate	 * For the MDB backend, we never set dtst_exiting or dtst_filled.  This
5687c478bdstevel@tonic-gate	 * is by design:  we don't want the library to try to stop tracing,
5697c478bdstevel@tonic-gate	 * because it doesn't particularly mean anything.
5707c478bdstevel@tonic-gate	 */
5717c478bdstevel@tonic-gate	nerrs = state->dts_errors;
5727c478bdstevel@tonic-gate	dstate = &state->dts_vstate.dtvs_dynvars;
5737c478bdstevel@tonic-gate
5747c478bdstevel@tonic-gate	for (i = 0; i < ncpu; i++) {
5757c478bdstevel@tonic-gate		dtrace_dstate_percpu_t dcpu;
5767c478bdstevel@tonic-gate		dtrace_buffer_t buf;
5777c478bdstevel@tonic-gate
5787c478bdstevel@tonic-gate		addr = (uintptr_t)&dstate->dtds_percpu[i];
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate		if (mdb_vread(&dcpu, sizeof (dcpu), addr) == -1) {
5817c478bdstevel@tonic-gate			mdb_warn("failed to read per-CPU dstate at %p", addr);
5827c478bdstevel@tonic-gate			return (-1);
5837c478bdstevel@tonic-gate		}
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate		status->dtst_dyndrops += dcpu.dtdsc_drops;
5867c478bdstevel@tonic-gate		status->dtst_dyndrops_dirty += dcpu.dtdsc_dirty_drops;
5877c478bdstevel@tonic-gate		status->dtst_dyndrops_rinsing += dcpu.dtdsc_rinsing_drops;
5887c478bdstevel@tonic-gate
5897c478bdstevel@tonic-gate		addr = (uintptr_t)&state->dts_buffer[i];
5907c478bdstevel@tonic-gate
5917c478bdstevel@tonic-gate		if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
5927c478bdstevel@tonic-gate			mdb_warn("failed to read per-CPU buffer at %p", addr);
5937c478bdstevel@tonic-gate			return (-1);
5947c478bdstevel@tonic-gate		}
5957c478bdstevel@tonic-gate
5967c478bdstevel@tonic-gate		nerrs += buf.dtb_errors;
5977c478bdstevel@tonic-gate
5987c478bdstevel@tonic-gate		for (j = 0; j < state->dts_nspeculations; j++) {
5997c478bdstevel@tonic-gate			dtrace_speculation_t spec;
6007c478bdstevel@tonic-gate
6017c478bdstevel@tonic-gate			addr = (uintptr_t)&state->dts_speculations[j];
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gate			if (mdb_vread(&spec, sizeof (spec), addr) == -1) {
6047c478bdstevel@tonic-gate				mdb_warn("failed to read "
6057c478bdstevel@tonic-gate				    "speculation at %p", addr);
6067c478bdstevel@tonic-gate				return (-1);
6077c478bdstevel@tonic-gate			}
6087c478bdstevel@tonic-gate
6097c478bdstevel@tonic-gate			addr = (uintptr_t)&spec.dtsp_buffer[i];
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate			if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
6127c478bdstevel@tonic-gate				mdb_warn("failed to read "
6137c478bdstevel@tonic-gate				    "speculative buffer at %p", addr);
6147c478bdstevel@tonic-gate				return (-1);
6157c478bdstevel@tonic-gate			}
6167c478bdstevel@tonic-gate
6177c478bdstevel@tonic-gate			status->dtst_specdrops += buf.dtb_xamot_drops;
6187c478bdstevel@tonic-gate		}
6197c478bdstevel@tonic-gate	}
6207c478bdstevel@tonic-gate
6217c478bdstevel@tonic-gate	status->dtst_specdrops_busy = state->dts_speculations_busy;
6227c478bdstevel@tonic-gate	status->dtst_specdrops_unavail = state->dts_speculations_unavail;
6237c478bdstevel@tonic-gate	status->dtst_errors = nerrs;
6247c478bdstevel@tonic-gate
6257c478bdstevel@tonic-gate	return (0);
6267c478bdstevel@tonic-gate}
6277c478bdstevel@tonic-gate
6287c478bdstevel@tonic-gatetypedef struct dtracemdb_data {
6297c478bdstevel@tonic-gate	dtrace_state_t *dtmd_state;
6307c478bdstevel@tonic-gate	char *dtmd_symstr;
6317c478bdstevel@tonic-gate	char *dtmd_modstr;
6327c478bdstevel@tonic-gate	uintptr_t dtmd_addr;
633c9a6ea2Bryan Cantrill	int dtmd_isanon;
6347c478bdstevel@tonic-gate} dtracemdb_data_t;
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gatestatic int
6377c478bdstevel@tonic-gatedtracemdb_ioctl(void *varg, int cmd, void *arg)
6387c478bdstevel@tonic-gate{
6397c478bdstevel@tonic-gate	dtracemdb_data_t *data = varg;
6407c478bdstevel@tonic-gate	dtrace_state_t *state = data->dtmd_state;
6417c478bdstevel@tonic-gate
6427c478bdstevel@tonic-gate	switch (cmd) {
6437c478bdstevel@tonic-gate	case DTRACEIOC_CONF: {
6447c478bdstevel@tonic-gate		dtrace_conf_t *conf = arg;
6457c478bdstevel@tonic-gate
6467c478bdstevel@tonic-gate		bzero(conf, sizeof (conf));
6477c478bdstevel@tonic-gate		conf->dtc_difversion = DIF_VERSION;
6487c478bdstevel@tonic-gate		conf->dtc_difintregs = DIF_DIR_NREGS;
6497c478bdstevel@tonic-gate		conf->dtc_diftupregs = DIF_DTR_NREGS;
6507c478bdstevel@tonic-gate		conf->dtc_ctfmodel = CTF_MODEL_NATIVE;
6517c478bdstevel@tonic-gate
6527c478bdstevel@tonic-gate		return (0);
6537c478bdstevel@tonic-gate	}
6547c478bdstevel@tonic-gate
6557c478bdstevel@tonic-gate	case DTRACEIOC_DOFGET: {
6567c478bdstevel@tonic-gate		dof_hdr_t *hdr = arg, *dof;
6577c478bdstevel@tonic-gate
658c9a6ea2Bryan Cantrill		dof = dtracemdb_dof_create(state, data->dtmd_isanon);
6597c478bdstevel@tonic-gate		bcopy(dof, hdr, MIN(hdr->dofh_loadsz, dof->dofh_loadsz));
6607c478bdstevel@tonic-gate		mdb_free(dof, dof->dofh_loadsz);
6617c478bdstevel@tonic-gate
6627c478bdstevel@tonic-gate		return (0);
6637c478bdstevel@tonic-gate	}
6647c478bdstevel@tonic-gate
6657c478bdstevel@tonic-gate	case DTRACEIOC_BUFSNAP:
6667c478bdstevel@tonic-gate		return (dtracemdb_bufsnap(state->dts_buffer, arg));
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate	case DTRACEIOC_AGGSNAP:
6697c478bdstevel@tonic-gate		return (dtracemdb_bufsnap(state->dts_aggbuffer, arg));
6707c478bdstevel@tonic-gate
6717c478bdstevel@tonic-gate	case DTRACEIOC_AGGDESC:
6727c478bdstevel@tonic-gate		return (dtracemdb_aggdesc(state, arg));
6737c478bdstevel@tonic-gate
6747c478bdstevel@tonic-gate	case DTRACEIOC_EPROBE:
6757c478bdstevel@tonic-gate		return (dtracemdb_eprobe(state, arg));
6767c478bdstevel@tonic-gate
6777c478bdstevel@tonic-gate	case DTRACEIOC_PROBES:
6787c478bdstevel@tonic-gate		return (dtracemdb_probe(state, arg));
6797c478bdstevel@tonic-gate
6807c478bdstevel@tonic-gate	case DTRACEIOC_FORMAT:
6817c478bdstevel@tonic-gate		return (dtracemdb_format(state, arg));
6827c478bdstevel@tonic-gate
6837c478bdstevel@tonic-gate	case DTRACEIOC_STATUS:
6847c478bdstevel@tonic-gate		return (dtracemdb_status(state, arg));
6857c478bdstevel@tonic-gate
6867c478bdstevel@tonic-gate	case DTRACEIOC_GO:
6877c478bdstevel@tonic-gate		*(processorid_t *)arg = -1;
6887c478bdstevel@tonic-gate		return (0);
6897c478bdstevel@tonic-gate
6907c478bdstevel@tonic-gate	case DTRACEIOC_ENABLE:
6917c478bdstevel@tonic-gate		errno = ENOTTY; /* see dt_open.c:dtrace_go() */
6927c478bdstevel@tonic-gate		return (-1);
6937c478bdstevel@tonic-gate
6947c478bdstevel@tonic-gate	case DTRACEIOC_PROVIDER:
6957c478bdstevel@tonic-gate	case DTRACEIOC_PROBEMATCH:
6967c478bdstevel@tonic-gate		errno = ESRCH;
6977c478bdstevel@tonic-gate		return (-1);
6987c478bdstevel@tonic-gate
6997c478bdstevel@tonic-gate	default:
7007c478bdstevel@tonic-gate		mdb_warn("unexpected ioctl 0x%x (%s)\n", cmd,
7017c478bdstevel@tonic-gate		    cmd == DTRACEIOC_PROVIDER	? "DTRACEIOC_PROVIDER" :
7027c478bdstevel@tonic-gate		    cmd == DTRACEIOC_PROBES	? "DTRACEIOC_PROBES" :
7037c478bdstevel@tonic-gate		    cmd == DTRACEIOC_BUFSNAP	? "DTRACEIOC_BUFSNAP" :
7047c478bdstevel@tonic-gate		    cmd == DTRACEIOC_PROBEMATCH	? "DTRACEIOC_PROBEMATCH" :
7057c478bdstevel@tonic-gate		    cmd == DTRACEIOC_ENABLE	? "DTRACEIOC_ENABLE" :
7067c478bdstevel@tonic-gate		    cmd == DTRACEIOC_AGGSNAP	? "DTRACEIOC_AGGSNAP" :
7077c478bdstevel@tonic-gate		    cmd == DTRACEIOC_EPROBE	? "DTRACEIOC_EPROBE" :
7087c478bdstevel@tonic-gate		    cmd == DTRACEIOC_PROBEARG	? "DTRACEIOC_PROBEARG" :
7097c478bdstevel@tonic-gate		    cmd == DTRACEIOC_CONF	? "DTRACEIOC_CONF" :
7107c478bdstevel@tonic-gate		    cmd == DTRACEIOC_STATUS	? "DTRACEIOC_STATUS" :
7117c478bdstevel@tonic-gate		    cmd == DTRACEIOC_GO		? "DTRACEIOC_GO" :
7127c478bdstevel@tonic-gate		    cmd == DTRACEIOC_STOP	? "DTRACEIOC_STOP" :
7137c478bdstevel@tonic-gate		    cmd == DTRACEIOC_AGGDESC	? "DTRACEIOC_AGGDESC" :
7147c478bdstevel@tonic-gate		    cmd == DTRACEIOC_FORMAT	? "DTRACEIOC_FORMAT" :
7157c478bdstevel@tonic-gate		    cmd == DTRACEIOC_DOFGET	? "DTRACEIOC_DOFGET" :
7167c478bdstevel@tonic-gate		    cmd == DTRACEIOC_REPLICATE	? "DTRACEIOC_REPLICATE" :
7177c478bdstevel@tonic-gate		    "???");
7187c478bdstevel@tonic-gate		errno = ENXIO;
7197c478bdstevel@tonic-gate		return (-1);
7207c478bdstevel@tonic-gate	}
7217c478bdstevel@tonic-gate}
7227c478bdstevel@tonic-gate
7237c478bdstevel@tonic-gatestatic int
7247c478bdstevel@tonic-gatedtracemdb_modctl(uintptr_t addr, const struct modctl *m, dtracemdb_data_t *data)
7257c478bdstevel@tonic-gate{
7267c478bdstevel@tonic-gate	struct module mod;
7277c478bdstevel@tonic-gate
7287c478bdstevel@tonic-gate	if (m->mod_mp == NULL)
7297c478bdstevel@tonic-gate		return (WALK_NEXT);
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
7327c478bdstevel@tonic-gate		mdb_warn("couldn't read modctl %p's module", addr);
7337c478bdstevel@tonic-gate		return (WALK_NEXT);
7347c478bdstevel@tonic-gate	}
7357c478bdstevel@tonic-gate
7367c478bdstevel@tonic-gate	if ((uintptr_t)mod.text > data->dtmd_addr)
7377c478bdstevel@tonic-gate		return (WALK_NEXT);
7387c478bdstevel@tonic-gate
7397c478bdstevel@tonic-gate	if ((uintptr_t)mod.text + mod.text_size <= data->dtmd_addr)
7407c478bdstevel@tonic-gate		return (WALK_NEXT);
7417c478bdstevel@tonic-gate
7427c478bdstevel@tonic-gate	if (mdb_readstr(data->dtmd_modstr, MDB_SYM_NAMLEN,
7437c478bdstevel@tonic-gate	    (uintptr_t)m->mod_modname) == -1)
7447c478bdstevel@tonic-gate		return (WALK_ERR);
7457c478bdstevel@tonic-gate
7467c478bdstevel@tonic-gate	return (WALK_DONE);
7477c478bdstevel@tonic-gate}
7487c478bdstevel@tonic-gate
7497c478bdstevel@tonic-gatestatic int
7507c478bdstevel@tonic-gatedtracemdb_lookup_by_addr(void *varg, GElf_Addr addr, GElf_Sym *symp,
7517c478bdstevel@tonic-gate    dtrace_syminfo_t *sip)
7527c478bdstevel@tonic-gate{
7537c478bdstevel@tonic-gate	dtracemdb_data_t *data = varg;
7547c478bdstevel@tonic-gate
7557c478bdstevel@tonic-gate	if (data->dtmd_symstr == NULL) {
7567c478bdstevel@tonic-gate		data->dtmd_symstr = mdb_zalloc(MDB_SYM_NAMLEN,
7577c478bdstevel@tonic-gate		    UM_SLEEP | UM_GC);
7587c478bdstevel@tonic-gate	}
7597c478bdstevel@tonic-gate
7607c478bdstevel@tonic-gate	if (data->dtmd_modstr == NULL) {
7617c478bdstevel@tonic-gate		data->dtmd_modstr = mdb_zalloc(MDB_SYM_NAMLEN,
7627c478bdstevel@tonic-gate		    UM_SLEEP | UM_GC);
7637c478bdstevel@tonic-gate	}
7647c478bdstevel@tonic-gate
7657c478bdstevel@tonic-gate	if (symp != NULL) {
7667c478bdstevel@tonic-gate		if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, data->dtmd_symstr,
7677c478bdstevel@tonic-gate		    MDB_SYM_NAMLEN, symp) == -1)
7687c478bdstevel@tonic-gate			return (-1);
7697c478bdstevel@tonic-gate	}
7707c478bdstevel@tonic-gate
7717c478bdstevel@tonic-gate	if (sip != NULL) {
7727c478bdstevel@tonic-gate		data->dtmd_addr = addr;
7737c478bdstevel@tonic-gate
7747c478bdstevel@tonic-gate		(void) strcpy(data->dtmd_modstr, "???");
7757c478bdstevel@tonic-gate
7767c478bdstevel@tonic-gate		if (mdb_walk("modctl",
7777c478bdstevel@tonic-gate		    (mdb_walk_cb_t)dtracemdb_modctl, varg) == -1) {
7787c478bdstevel@tonic-gate			mdb_warn("couldn't walk 'modctl'");
7797c478bdstevel@tonic-gate			return (-1);
7807c478bdstevel@tonic-gate		}
7817c478bdstevel@tonic-gate
7827c478bdstevel@tonic-gate		sip->dts_object = data->dtmd_modstr;
7837c478bdstevel@tonic-gate		sip->dts_id = 0;
7847c478bdstevel@tonic-gate		sip->dts_name = symp != NULL ? data->dtmd_symstr : NULL;
7857c478bdstevel@tonic-gate	}
7867c478bdstevel@tonic-gate
7877c478bdstevel@tonic-gate	return (0);
7887c478bdstevel@tonic-gate}
7897c478bdstevel@tonic-gate
7907c478bdstevel@tonic-gate/*ARGSUSED*/
7917c478bdstevel@tonic-gatestatic int
7927c478bdstevel@tonic-gatedtracemdb_stat(void *varg, processorid_t cpu)
7937c478bdstevel@tonic-gate{
7947c478bdstevel@tonic-gate	GElf_Sym sym;
7957c478bdstevel@tonic-gate	cpu_t c;
7967c478bdstevel@tonic-gate	uintptr_t caddr, addr;
7977c478bdstevel@tonic-gate
7987c478bdstevel@tonic-gate	if (mdb_lookup_by_name("cpu", &sym) == -1) {
7997c478bdstevel@tonic-gate		mdb_warn("failed to find symbol for 'cpu'");
8007c478bdstevel@tonic-gate		return (-1);
8017c478bdstevel@tonic-gate	}
8027c478bdstevel@tonic-gate
8037c478bdstevel@tonic-gate	if (cpu * sizeof (uintptr_t) > sym.st_size)
8047c478bdstevel@tonic-gate		return (-1);
8057c478bdstevel@tonic-gate
8067c478bdstevel@tonic-gate	addr = (uintptr_t)sym.st_value + cpu * sizeof (uintptr_t);
8077c478bdstevel@tonic-gate
8087c478bdstevel@tonic-gate	if (mdb_vread(&caddr, sizeof (caddr), addr) == -1) {
8097c478bdstevel@tonic-gate		mdb_warn("failed to read cpu[%d]", cpu);
8107c478bdstevel@tonic-gate		return (-1);
8117c478bdstevel@tonic-gate	}
8127c478bdstevel@tonic-gate
813892ad16Toomas Soome	if (caddr == 0)
8147c478bdstevel@tonic-gate		return (-1);
8157c478bdstevel@tonic-gate
8167c478bdstevel@tonic-gate	if (mdb_vread(&c, sizeof (c), caddr) == -1) {
8177c478bdstevel@tonic-gate		mdb_warn("failed to read cpu at %p", caddr);
8187c478bdstevel@tonic-gate		return (-1);
8197c478bdstevel@tonic-gate	}
8207c478bdstevel@tonic-gate
8217c478bdstevel@tonic-gate	if (c.cpu_flags & CPU_POWEROFF) {
8227c478bdstevel@tonic-gate		return (P_POWEROFF);
8237c478bdstevel@tonic-gate	} else if (c.cpu_flags & CPU_SPARE) {
8247c478bdstevel@tonic-gate		return (P_SPARE);
8257c478bdstevel@tonic-gate	} else if (c.cpu_flags & CPU_FAULTED) {
8267c478bdstevel@tonic-gate		return (P_FAULTED);
827c3377eeJohn Levon	} else if (c.cpu_flags & CPU_DISABLED) {
828c3377eeJohn Levon		return (P_DISABLED);
8297c478bdstevel@tonic-gate	} else if ((c.cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) {
8307c478bdstevel@tonic-gate		return (P_OFFLINE);
8317c478bdstevel@tonic-gate	} else if (c.cpu_flags & CPU_ENABLE) {
8327c478bdstevel@tonic-gate		return (P_ONLINE);
8337c478bdstevel@tonic-gate	} else {
8347c478bdstevel@tonic-gate		return (P_NOINTR);
8357c478bdstevel@tonic-gate	}
8367c478bdstevel@tonic-gate}
8377c478bdstevel@tonic-gate
8387c478bdstevel@tonic-gate/*ARGSUSED*/
8397c478bdstevel@tonic-gatestatic long
8407c478bdstevel@tonic-gatedtracemdb_sysconf(void *varg, int name)
8417c478bdstevel@tonic-gate{
8427c478bdstevel@tonic-gate	int max_ncpus;
8437c478bdstevel@tonic-gate	processorid_t max_cpuid;
8447c478bdstevel@tonic-gate
8457c478bdstevel@tonic-gate	switch (name) {
8467c478bdstevel@tonic-gate	case _SC_CPUID_MAX:
8477c478bdstevel@tonic-gate		if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
8487c478bdstevel@tonic-gate			mdb_warn("failed to read 'max_cpuid'");
8497c478bdstevel@tonic-gate			return (-1);
8507c478bdstevel@tonic-gate		}
8517c478bdstevel@tonic-gate
8527c478bdstevel@tonic-gate		return (max_cpuid);
8537c478bdstevel@tonic-gate
8547c478bdstevel@tonic-gate	case _SC_NPROCESSORS_MAX:
8557c478bdstevel@tonic-gate		if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
8567c478bdstevel@tonic-gate			mdb_warn("failed to read 'max_ncpus'");
8577c478bdstevel@tonic-gate			return (-1);
8587c478bdstevel@tonic-gate		}
8597c478bdstevel@tonic-gate
8607c478bdstevel@tonic-gate		return (max_ncpus);
8617c478bdstevel@tonic-gate
8627c478bdstevel@tonic-gate	default:
8637c478bdstevel@tonic-gate		mdb_warn("unexpected sysconf code %d\n", name);
8647c478bdstevel@tonic-gate		return (-1);
8657c478bdstevel@tonic-gate	}
8667c478bdstevel@tonic-gate}
8677c478bdstevel@tonic-gate
8687c478bdstevel@tonic-gateconst dtrace_vector_t dtrace_mdbops = {
8697c478bdstevel@tonic-gate	dtracemdb_ioctl,
8707c478bdstevel@tonic-gate	dtracemdb_lookup_by_addr,
8717c478bdstevel@tonic-gate	dtracemdb_stat,
8727c478bdstevel@tonic-gate	dtracemdb_sysconf
8737c478bdstevel@tonic-gate};
8747c478bdstevel@tonic-gate
8757c478bdstevel@tonic-gatetypedef struct dtrace_dcmddata {
8767c478bdstevel@tonic-gate	dtrace_hdl_t *dtdd_dtp;
8777c478bdstevel@tonic-gate	int dtdd_cpu;
8787c478bdstevel@tonic-gate	int dtdd_quiet;
8797c478bdstevel@tonic-gate	int dtdd_flowindent;
8807c478bdstevel@tonic-gate	int dtdd_heading;
8817994dfdDan Kimmel	FILE *dtdd_output;
8827c478bdstevel@tonic-gate} dtrace_dcmddata_t;
8837c478bdstevel@tonic-gate
8847994dfdDan Kimmel/*
8857994dfdDan Kimmel * Helper to grab all the content from a file, spit it into a string, and erase
8867994dfdDan Kimmel * and reset the file.
8877994dfdDan Kimmel */
8887994dfdDan Kimmelstatic void
8897994dfdDan Kimmelprint_and_truncate_file(FILE *fp)
8907994dfdDan Kimmel{
8917994dfdDan Kimmel	long len;
8927994dfdDan Kimmel	char *out;
8937994dfdDan Kimmel
8947994dfdDan Kimmel	/* flush, find length of file, seek to beginning, initialize buffer */
8957994dfdDan Kimmel	if (fflush(fp) || (len = ftell(fp)) < 0 ||
8967994dfdDan Kimmel	    fseek(fp, 0, SEEK_SET) < 0) {
8977994dfdDan Kimmel		mdb_warn("couldn't prepare DTrace output file: %d\n", errno);
8987994dfdDan Kimmel		return;
8997994dfdDan Kimmel	}
9007994dfdDan Kimmel
9017994dfdDan Kimmel	out = mdb_alloc(len + 1, UM_SLEEP);
9027994dfdDan Kimmel	out[len] = '\0';
9037994dfdDan Kimmel
9047994dfdDan Kimmel	/* read file into buffer, truncate file, and seek to beginning */
9057994dfdDan Kimmel	if ((fread(out, len + 1, sizeof (char), fp) == 0 && ferror(fp)) ||
9067994dfdDan Kimmel	    ftruncate(fileno(fp), 0) < 0 || fseek(fp, 0, SEEK_SET) < 0) {
9077994dfdDan Kimmel		mdb_warn("couldn't read DTrace output file: %d\n", errno);
9087994dfdDan Kimmel		mdb_free(out, len + 1);
9097994dfdDan Kimmel		return;
9107994dfdDan Kimmel	}
9117994dfdDan Kimmel
9127994dfdDan Kimmel	mdb_printf("%s", out);
9137994dfdDan Kimmel	mdb_free(out, len + 1);
9147994dfdDan Kimmel}
9157994dfdDan Kimmel
9167c478bdstevel@tonic-gate/*ARGSUSED*/
9177c478bdstevel@tonic-gatestatic int
9187c478bdstevel@tonic-gatedtrace_dcmdrec(const dtrace_probedata_t *data,
9197c478bdstevel@tonic-gate    const dtrace_recdesc_t *rec, void *arg)
9207c478bdstevel@tonic-gate{
9217c478bdstevel@tonic-gate	dtrace_dcmddata_t *dd = arg;
9227c478bdstevel@tonic-gate
9237994dfdDan Kimmel	print_and_truncate_file(dd->dtdd_output);
9247994dfdDan Kimmel
9257c478bdstevel@tonic-gate	if (rec == NULL) {
9267c478bdstevel@tonic-gate		/*
9277c478bdstevel@tonic-gate		 * We have processed the final record; output the newline if
9287c478bdstevel@tonic-gate		 * we're not in quiet mode.
9297c478bdstevel@tonic-gate		 */
9307c478bdstevel@tonic-gate		if (!dd->dtdd_quiet)
9317c478bdstevel@tonic-gate			mdb_printf("\n");
9327c478bdstevel@tonic-gate
9337c478bdstevel@tonic-gate		return (DTRACE_CONSUME_NEXT);
9347c478bdstevel@tonic-gate	}
9357c478bdstevel@tonic-gate
9367c478bdstevel@tonic-gate	return (DTRACE_CONSUME_THIS);
9377c478bdstevel@tonic-gate}
9387c478bdstevel@tonic-gate
9397c478bdstevel@tonic-gate/*ARGSUSED*/
9407c478bdstevel@tonic-gatestatic int
9417c478bdstevel@tonic-gatedtrace_dcmdprobe(const dtrace_probedata_t *data, void *arg)
9427c478bdstevel@tonic-gate{
9437c478bdstevel@tonic-gate	dtrace_probedesc_t *pd = data->dtpda_pdesc;
9447c478bdstevel@tonic-gate	processorid_t cpu = data->dtpda_cpu;
9457c478bdstevel@tonic-gate	dtrace_dcmddata_t *dd = arg;
9467c478bdstevel@tonic-gate	char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
9477c478bdstevel@tonic-gate
9487c478bdstevel@tonic-gate	if (dd->dtdd_cpu != -1UL && dd->dtdd_cpu != cpu)
9497c478bdstevel@tonic-gate		return (DTRACE_CONSUME_NEXT);
9507c478bdstevel@tonic-gate
9517c478bdstevel@tonic-gate	if (dd->dtdd_heading == 0) {
9527c478bdstevel@tonic-gate		if (!dd->dtdd_flowindent) {
9537c478bdstevel@tonic-gate			if (!dd->dtdd_quiet) {
9547c478bdstevel@tonic-gate				mdb_printf("%3s %6s %32s\n",
9557c478bdstevel@tonic-gate				    "CPU", "ID", "FUNCTION:NAME");
9567c478bdstevel@tonic-gate			}
9577c478bdstevel@tonic-gate		} else {
9587c478bdstevel@tonic-gate			mdb_printf("%3s %-41s\n", "CPU", "FUNCTION");
9597c478bdstevel@tonic-gate		}
9607c478bdstevel@tonic-gate		dd->dtdd_heading = 1;
9617c478bdstevel@tonic-gate	}
9627c478bdstevel@tonic-gate
9637c478bdstevel@tonic-gate	if (!dd->dtdd_flowindent) {
9647c478bdstevel@tonic-gate		if (!dd->dtdd_quiet) {
9657c478bdstevel@tonic-gate			(void) mdb_snprintf(name, sizeof (name), "%s:%s",
9667c478bdstevel@tonic-gate			    pd->dtpd_func, pd->dtpd_name);
9677c478bdstevel@tonic-gate
9687c478bdstevel@tonic-gate			mdb_printf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
9697c478bdstevel@tonic-gate		}
9707c478bdstevel@tonic-gate	} else {
9717c478bdstevel@tonic-gate		int indent = data->dtpda_indent;
9727c478bdstevel@tonic-gate
9737c478bdstevel@tonic-gate		if (data->dtpda_flow == DTRACEFLOW_NONE) {
9747c478bdstevel@tonic-gate			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s:%s",
9757c478bdstevel@tonic-gate			    indent, "", data->dtpda_prefix, pd->dtpd_func,
9767c478bdstevel@tonic-gate			    pd->dtpd_name);
9777c478bdstevel@tonic-gate		} else {
9787c478bdstevel@tonic-gate			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s",
9797c478bdstevel@tonic-gate			    indent, "", data->dtpda_prefix, pd->dtpd_func);
9807c478bdstevel@tonic-gate		}
9817c478bdstevel@tonic-gate
9827c478bdstevel@tonic-gate		mdb_printf("%3d %-41s ", cpu, name);
9837c478bdstevel@tonic-gate	}
9847c478bdstevel@tonic-gate
9857c478bdstevel@tonic-gate	return (DTRACE_CONSUME_THIS);
9867c478bdstevel@tonic-gate}
9877c478bdstevel@tonic-gate
9887c478bdstevel@tonic-gate/*ARGSUSED*/
9897c478bdstevel@tonic-gatestatic int
990a1b5e53bmcdtrace_dcmderr(const dtrace_errdata_t *data, void *arg)
9917c478bdstevel@tonic-gate{
9927c478bdstevel@tonic-gate	mdb_warn(data->dteda_msg);
9937c478bdstevel@tonic-gate	return (DTRACE_HANDLE_OK);
9947c478bdstevel@tonic-gate}
9957c478bdstevel@tonic-gate
9967c478bdstevel@tonic-gate/*ARGSUSED*/
9977c478bdstevel@tonic-gatestatic int
998a1b5e53bmcdtrace_dcmddrop(const dtrace_dropdata_t *data, void *arg)
9997c478bdstevel@tonic-gate{
10007c478bdstevel@tonic-gate	mdb_warn(data->dtdda_msg);
10017c478bdstevel@tonic-gate	return (DTRACE_HANDLE_OK);
10027c478bdstevel@tonic-gate}
10037c478bdstevel@tonic-gate
10047c478bdstevel@tonic-gate/*ARGSUSED*/
10057c478bdstevel@tonic-gatestatic int
1006a1b5e53bmcdtrace_dcmdbuffered(const dtrace_bufdata_t *bufdata, void *arg)
10077c478bdstevel@tonic-gate{
10087c478bdstevel@tonic-gate	mdb_printf("%s", bufdata->dtbda_buffered);
10097c478bdstevel@tonic-gate	return (DTRACE_HANDLE_OK);
10107c478bdstevel@tonic-gate}
10117c478bdstevel@tonic-gate
10127c478bdstevel@tonic-gate/*ARGSUSED*/
10137c478bdstevel@tonic-gateint
10147c478bdstevel@tonic-gatedtrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10157c478bdstevel@tonic-gate{
10167c478bdstevel@tonic-gate	dtrace_state_t state;
10177c478bdstevel@tonic-gate	dtrace_hdl_t *dtp;
10187c478bdstevel@tonic-gate	int ncpu, err;
10197c478bdstevel@tonic-gate	uintptr_t c = -1UL;
10207c478bdstevel@tonic-gate	dtrace_dcmddata_t dd;
10217c478bdstevel@tonic-gate	dtrace_optval_t val;
10227c478bdstevel@tonic-gate	dtracemdb_data_t md;
10237c478bdstevel@tonic-gate	int rval = DCMD_ERR;
1024c9a6ea2Bryan Cantrill	dtrace_anon_t anon;
10257c478bdstevel@tonic-gate
10267c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC))
10277c478bdstevel@tonic-gate		return (DCMD_USAGE);
10287c478bdstevel@tonic-gate
10297c478bdstevel@tonic-gate	if (mdb_getopts(argc, argv, 'c', MDB_OPT_UINTPTR, &c, NULL) != argc)
10307c478bdstevel@tonic-gate		return (DCMD_USAGE);
10317c478bdstevel@tonic-gate
10327c478bdstevel@tonic-gate	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
10337c478bdstevel@tonic-gate		mdb_warn("failed to read '_ncpu'");
10347c478bdstevel@tonic-gate		return (DCMD_ERR);
10357c478bdstevel@tonic-gate	}
10367c478bdstevel@tonic-gate
10377c478bdstevel@tonic-gate	if (mdb_vread(&state, sizeof (state), addr) == -1) {
10387c478bdstevel@tonic-gate		mdb_warn("couldn't read dtrace_state_t at %p", addr);
10397c478bdstevel@tonic-gate		return (DCMD_ERR);
10407c478bdstevel@tonic-gate	}
10417c478bdstevel@tonic-gate
1042c9a6ea2Bryan Cantrill	if (state.dts_anon != NULL) {
1043c9a6ea2Bryan Cantrill		addr = (uintptr_t)state.dts_anon;
1044c9a6ea2Bryan Cantrill
1045c9a6ea2Bryan Cantrill		if (mdb_vread(&state, sizeof (state), addr) == -1) {
1046c9a6ea2Bryan Cantrill			mdb_warn("couldn't read anonymous state at %p", addr);
1047c9a6ea2Bryan Cantrill			return (DCMD_ERR);
1048c9a6ea2Bryan Cantrill		}
1049c9a6ea2Bryan Cantrill	}
1050c9a6ea2Bryan Cantrill
10517c478bdstevel@tonic-gate	bzero(&md, sizeof (md));
10527c478bdstevel@tonic-gate	md.dtmd_state = &state;
10537c478bdstevel@tonic-gate
10547c478bdstevel@tonic-gate	if ((dtp = dtrace_vopen(DTRACE_VERSION, DTRACE_O_NOSYS, &err,
10557c478bdstevel@tonic-gate	    &dtrace_mdbops, &md)) == NULL) {
10567c478bdstevel@tonic-gate		mdb_warn("failed to initialize dtrace: %s\n",
10577c478bdstevel@tonic-gate		    dtrace_errmsg(NULL, err));
10587c478bdstevel@tonic-gate		return (DCMD_ERR);
10597c478bdstevel@tonic-gate	}
10607c478bdstevel@tonic-gate
1061c9a6ea2Bryan Cantrill	/*
1062c9a6ea2Bryan Cantrill	 * If this is the anonymous enabling, we need to set a bit indicating
1063c9a6ea2Bryan Cantrill	 * that DTRACEOPT_GRABANON should be set.
1064c9a6ea2Bryan Cantrill	 */
1065c9a6ea2Bryan Cantrill	if (mdb_readvar(&anon, "dtrace_anon") == -1) {
1066c9a6ea2Bryan Cantrill		mdb_warn("failed to read 'dtrace_anon'");
1067c9a6ea2Bryan Cantrill		return (DCMD_ERR);
1068c9a6ea2Bryan Cantrill	}
1069c9a6ea2Bryan Cantrill
1070c9a6ea2Bryan Cantrill	md.dtmd_isanon = ((uintptr_t)anon.dta_state == addr);
1071c9a6ea2Bryan Cantrill
10727c478bdstevel@tonic-gate	if (dtrace_go(dtp) != 0) {
10737c478bdstevel@tonic-gate		mdb_warn("failed to initialize dtrace: %s\n",
10747c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
10757c478bdstevel@tonic-gate		goto err;
10767c478bdstevel@tonic-gate	}
10777c478bdstevel@tonic-gate
10787c478bdstevel@tonic-gate	bzero(&dd, sizeof (dd));
10797c478bdstevel@tonic-gate	dd.dtdd_dtp = dtp;
10807c478bdstevel@tonic-gate	dd.dtdd_cpu = c;
10817c478bdstevel@tonic-gate
10827c478bdstevel@tonic-gate	if (dtrace_getopt(dtp, "flowindent", &val) == -1) {
10837c478bdstevel@tonic-gate		mdb_warn("couldn't get 'flowindent' option: %s\n",
10847c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
10857c478bdstevel@tonic-gate		goto err;
10867c478bdstevel@tonic-gate	}
10877c478bdstevel@tonic-gate
10887c478bdstevel@tonic-gate	dd.dtdd_flowindent = (val != DTRACEOPT_UNSET);
10897c478bdstevel@tonic-gate
10907c478bdstevel@tonic-gate	if (dtrace_getopt(dtp, "quiet", &val) == -1) {
10917c478bdstevel@tonic-gate		mdb_warn("couldn't get 'quiet' option: %s\n",
10927c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
10937c478bdstevel@tonic-gate		goto err;
10947c478bdstevel@tonic-gate	}
10957c478bdstevel@tonic-gate
10967c478bdstevel@tonic-gate	dd.dtdd_quiet = (val != DTRACEOPT_UNSET);
10977c478bdstevel@tonic-gate
10987c478bdstevel@tonic-gate	if (dtrace_handle_err(dtp, dtrace_dcmderr, NULL) == -1) {
10997c478bdstevel@tonic-gate		mdb_warn("couldn't add err handler: %s\n",
11007c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11017c478bdstevel@tonic-gate		goto err;
11027c478bdstevel@tonic-gate	}
11037c478bdstevel@tonic-gate
11047c478bdstevel@tonic-gate	if (dtrace_handle_drop(dtp, dtrace_dcmddrop, NULL) == -1) {
11057c478bdstevel@tonic-gate		mdb_warn("couldn't add drop handler: %s\n",
11067c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11077c478bdstevel@tonic-gate		goto err;
11087c478bdstevel@tonic-gate	}
11097c478bdstevel@tonic-gate
11107c478bdstevel@tonic-gate	if (dtrace_handle_buffered(dtp, dtrace_dcmdbuffered, NULL) == -1) {
11117c478bdstevel@tonic-gate		mdb_warn("couldn't add buffered handler: %s\n",
11127c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11137c478bdstevel@tonic-gate		goto err;
11147c478bdstevel@tonic-gate	}
11157c478bdstevel@tonic-gate
11167c478bdstevel@tonic-gate	if (dtrace_status(dtp) == -1) {
11177c478bdstevel@tonic-gate		mdb_warn("couldn't get status: %s\n",
11187c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11197c478bdstevel@tonic-gate		goto err;
11207c478bdstevel@tonic-gate	}
11217c478bdstevel@tonic-gate
11227c478bdstevel@tonic-gate	if (dtrace_aggregate_snap(dtp) == -1) {
11237c478bdstevel@tonic-gate		mdb_warn("couldn't snapshot aggregation: %s\n",
11247c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11257c478bdstevel@tonic-gate		goto err;
11267c478bdstevel@tonic-gate	}
11277c478bdstevel@tonic-gate
11287994dfdDan Kimmel	if ((dd.dtdd_output = tmpfile()) == NULL) {
11297994dfdDan Kimmel		mdb_warn("couldn't open DTrace output file: %d\n", errno);
11307994dfdDan Kimmel		goto err;
11317994dfdDan Kimmel	}
11327994dfdDan Kimmel
11337994dfdDan Kimmel	if (dtrace_consume(dtp, dd.dtdd_output,
11347c478bdstevel@tonic-gate	    dtrace_dcmdprobe, dtrace_dcmdrec, &dd) == -1) {
11357c478bdstevel@tonic-gate		mdb_warn("couldn't consume DTrace buffers: %s\n",
11367c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11377c478bdstevel@tonic-gate	}
11387c478bdstevel@tonic-gate
11397c478bdstevel@tonic-gate	if (dtrace_aggregate_print(dtp, NULL, NULL) == -1) {
11407c478bdstevel@tonic-gate		mdb_warn("couldn't print aggregation: %s\n",
11417c478bdstevel@tonic-gate		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11427c478bdstevel@tonic-gate		goto err;
11437c478bdstevel@tonic-gate	}
11447c478bdstevel@tonic-gate
11457c478bdstevel@tonic-gate	rval = DCMD_OK;
11467c478bdstevel@tonic-gateerr:
11477c478bdstevel@tonic-gate	dtrace_close(dtp);
11487994dfdDan Kimmel	fclose(dd.dtdd_output);
11497c478bdstevel@tonic-gate	return (rval);
11507c478bdstevel@tonic-gate}
11517c478bdstevel@tonic-gate
11527c478bdstevel@tonic-gatestatic int
11537c478bdstevel@tonic-gatedtrace_errhash_cmp(const void *l, const void *r)
11547c478bdstevel@tonic-gate{
11557c478bdstevel@tonic-gate	uintptr_t lhs = *((uintptr_t *)l);
11567c478bdstevel@tonic-gate	uintptr_t rhs = *((uintptr_t *)r);
11577c478bdstevel@tonic-gate	dtrace_errhash_t lerr, rerr;
11587c478bdstevel@tonic-gate	char lmsg[256], rmsg[256];
11597c478bdstevel@tonic-gate
11607c478bdstevel@tonic-gate	(void) mdb_vread(&lerr, sizeof (lerr), lhs);
11617c478bdstevel@tonic-gate	(void) mdb_vread(&rerr, sizeof (rerr), rhs);
11627c478bdstevel@tonic-gate
11637c478bdstevel@tonic-gate	if (lerr.dter_msg == NULL)
11647c478bdstevel@tonic-gate		return (-1);
11657c478bdstevel@tonic-gate
11667c478bdstevel@tonic-gate	if (rerr.dter_msg == NULL)
11677c478bdstevel@tonic-gate		return (1);
11687c478bdstevel@tonic-gate
11697c478bdstevel@tonic-gate	(void) mdb_readstr(lmsg, sizeof (lmsg), (uintptr_t)lerr.dter_msg);
11707c478bdstevel@tonic-gate	(void) mdb_readstr(rmsg, sizeof (rmsg), (uintptr_t)rerr.dter_msg);
11717c478bdstevel@tonic-gate
11727c478bdstevel@tonic-gate	return (strcmp(lmsg, rmsg));
11737c478bdstevel@tonic-gate}
11747c478bdstevel@tonic-gate
11757c478bdstevel@tonic-gateint
11767c478bdstevel@tonic-gatedtrace_errhash_init(mdb_walk_state_t *wsp)
11777c478bdstevel@tonic-gate{
11787c478bdstevel@tonic-gate	GElf_Sym sym;
11797c478bdstevel@tonic-gate	uintptr_t *hash, addr;
11807c478bdstevel@tonic-gate	int i;
11817c478bdstevel@tonic-gate
1182892ad16Toomas Soome	if (wsp->walk_addr != 0) {
11837c478bdstevel@tonic-gate		mdb_warn("dtrace_errhash walk only supports global walks\n");
11847c478bdstevel@tonic-gate		return (WALK_ERR);
11857c478bdstevel@tonic-gate	}
11867c478bdstevel@tonic-gate
11877c478bdstevel@tonic-gate	if (mdb_lookup_by_name("dtrace_errhash", &sym) == -1) {
11887c478bdstevel@tonic-gate		mdb_warn("couldn't find 'dtrace_errhash' (non-DEBUG kernel?)");
11897c478bdstevel@tonic-gate		return (WALK_ERR);
11907c478bdstevel@tonic-gate	}
11917c478bdstevel@tonic-gate
11927c478bdstevel@tonic-gate	addr = (uintptr_t)sym.st_value;
11937c478bdstevel@tonic-gate	hash = mdb_alloc(DTRACE_ERRHASHSZ * sizeof (uintptr_t),
11947c478bdstevel@tonic-gate	    UM_SLEEP | UM_GC);
11957c478bdstevel@tonic-gate
11967c478bdstevel@tonic-gate	for (i = 0; i < DTRACE_ERRHASHSZ; i++)
11977c478bdstevel@tonic-gate		hash[i] = addr + i * sizeof (dtrace_errhash_t);
11987c478bdstevel@tonic-gate
11997c478bdstevel@tonic-gate	qsort(hash, DTRACE_ERRHASHSZ, sizeof (uintptr_t), dtrace_errhash_cmp);
12007c478bdstevel@tonic-gate
12017c478bdstevel@tonic-gate	wsp->walk_addr = 0;
12027c478bdstevel@tonic-gate	wsp->walk_data = hash;
12037c478bdstevel@tonic-gate
12047c478bdstevel@tonic-gate	return (WALK_NEXT);
12057c478bdstevel@tonic-gate}
12067c478bdstevel@tonic-gate
12077c478bdstevel@tonic-gateint
12087c478bdstevel@tonic-gatedtrace_errhash_step(mdb_walk_state_t *wsp)
12097c478bdstevel@tonic-gate{
12107c478bdstevel@tonic-gate	int ndx = (int)wsp->walk_addr;
12117c478bdstevel@tonic-gate	uintptr_t *hash = wsp->walk_data;
12127c478bdstevel@tonic-gate	dtrace_errhash_t err;
12137c478bdstevel@tonic-gate	uintptr_t addr;
12147c478bdstevel@tonic-gate
12157c478bdstevel@tonic-gate	if (ndx >= DTRACE_ERRHASHSZ)
12167c478bdstevel@tonic-gate		return (WALK_DONE);
12177c478bdstevel@tonic-gate
12187c478bdstevel@tonic-gate	wsp->walk_addr = ndx + 1;
12197c478bdstevel@tonic-gate	addr = hash[ndx];
12207c478bdstevel@tonic-gate
12217c478bdstevel@tonic-gate	if (mdb_vread(&err, sizeof (err), addr) == -1) {
12227c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_errhash_t at %p", addr);
12237c478bdstevel@tonic-gate		return (WALK_DONE);
12247c478bdstevel@tonic-gate	}
12257c478bdstevel@tonic-gate
12267c478bdstevel@tonic-gate	if (err.dter_msg == NULL)
12277c478bdstevel@tonic-gate		return (WALK_NEXT);
12287c478bdstevel@tonic-gate
12297c478bdstevel@tonic-gate	return (wsp->walk_callback(addr, &err, wsp->walk_cbdata));
12307c478bdstevel@tonic-gate}
12317c478bdstevel@tonic-gate
12327c478bdstevel@tonic-gate/*ARGSUSED*/
12337c478bdstevel@tonic-gateint
12347c478bdstevel@tonic-gatedtrace_errhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12357c478bdstevel@tonic-gate{
12367c478bdstevel@tonic-gate	dtrace_errhash_t err;
12377c478bdstevel@tonic-gate	char msg[256];
12387c478bdstevel@tonic-gate
12397c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC)) {
12407c478bdstevel@tonic-gate		if (mdb_walk_dcmd("dtrace_errhash", "dtrace_errhash",
12417c478bdstevel@tonic-gate		    argc, argv) == -1) {
12427c478bdstevel@tonic-gate			mdb_warn("can't walk 'dtrace_errhash'");
12437c478bdstevel@tonic-gate			return (DCMD_ERR);
12447c478bdstevel@tonic-gate		}
12457c478bdstevel@tonic-gate
12467c478bdstevel@tonic-gate		return (DCMD_OK);
12477c478bdstevel@tonic-gate	}
12487c478bdstevel@tonic-gate
12497c478bdstevel@tonic-gate	if (DCMD_HDRSPEC(flags))
12507c478bdstevel@tonic-gate		mdb_printf("%8s %s\n", "COUNT", "ERROR");
12517c478bdstevel@tonic-gate
12527c478bdstevel@tonic-gate	if (mdb_vread(&err, sizeof (err), addr) == -1) {
12537c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_errhash_t at %p", addr);
12547c478bdstevel@tonic-gate		return (DCMD_ERR);
12557c478bdstevel@tonic-gate	}
12567c478bdstevel@tonic-gate
12577c478bdstevel@tonic-gate	addr = (uintptr_t)err.dter_msg;
12587c478bdstevel@tonic-gate
12597c478bdstevel@tonic-gate	if (mdb_readstr(msg, sizeof (msg), addr) == -1) {
12607c478bdstevel@tonic-gate		mdb_warn("failed to read error msg at %p", addr);
12617c478bdstevel@tonic-gate		return (DCMD_ERR);
12627c478bdstevel@tonic-gate	}
12637c478bdstevel@tonic-gate
12647c478bdstevel@tonic-gate	mdb_printf("%8d %s", err.dter_count, msg);
12657c478bdstevel@tonic-gate
12667c478bdstevel@tonic-gate	/*
12677c478bdstevel@tonic-gate	 * Some error messages include a newline -- only print the newline
12687c478bdstevel@tonic-gate	 * if the message doesn't have one.
12697c478bdstevel@tonic-gate	 */
12707c478bdstevel@tonic-gate	if (msg[strlen(msg) - 1] != '\n')
12717c478bdstevel@tonic-gate		mdb_printf("\n");
12727c478bdstevel@tonic-gate
12737c478bdstevel@tonic-gate	return (DCMD_OK);
12747c478bdstevel@tonic-gate}
12757c478bdstevel@tonic-gate
12767c478bdstevel@tonic-gateint
12777c478bdstevel@tonic-gatedtrace_helptrace_init(mdb_walk_state_t *wsp)
12787c478bdstevel@tonic-gate{
12797c478bdstevel@tonic-gate	uint32_t next;
1280902686dBryan Cantrill	uintptr_t buffer;
12817c478bdstevel@tonic-gate
1282892ad16Toomas Soome	if (wsp->walk_addr != 0) {
12837c478bdstevel@tonic-gate		mdb_warn("dtrace_helptrace only supports global walks\n");
12847c478bdstevel@tonic-gate		return (WALK_ERR);
12857c478bdstevel@tonic-gate	}
12867c478bdstevel@tonic-gate
1287902686dBryan Cantrill	if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) {
1288902686dBryan Cantrill		mdb_warn("couldn't read 'dtrace_helptrace_buffer'");
12897c478bdstevel@tonic-gate		return (WALK_ERR);
12907c478bdstevel@tonic-gate	}
12917c478bdstevel@tonic-gate
1292892ad16Toomas Soome	if (buffer == 0) {
12937c478bdstevel@tonic-gate		mdb_warn("helper tracing is not enabled\n");
12947c478bdstevel@tonic-gate		return (WALK_ERR);
12957c478bdstevel@tonic-gate	}
12967c478bdstevel@tonic-gate
12977c478bdstevel@tonic-gate	if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
12987c478bdstevel@tonic-gate		mdb_warn("couldn't read 'dtrace_helptrace_next'");
12997c478bdstevel@tonic-gate		return (WALK_ERR);
13007c478bdstevel@tonic-gate	}
13017c478bdstevel@tonic-gate
13027c478bdstevel@tonic-gate	wsp->walk_addr = next;
13037c478bdstevel@tonic-gate
13047c478bdstevel@tonic-gate	return (WALK_NEXT);
13057c478bdstevel@tonic-gate}
13067c478bdstevel@tonic-gate
13077c478bdstevel@tonic-gateint
13087c478bdstevel@tonic-gatedtrace_helptrace_step(mdb_walk_state_t *wsp)
13097c478bdstevel@tonic-gate{
13107c478bdstevel@tonic-gate	uint32_t next, size, nlocals, bufsize;
13117c478bdstevel@tonic-gate	uintptr_t buffer, addr;
13127c478bdstevel@tonic-gate	dtrace_helptrace_t *ht;
13137c478bdstevel@tonic-gate	int rval;
13147c478bdstevel@tonic-gate
13157c478bdstevel@tonic-gate	if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
13167c478bdstevel@tonic-gate		mdb_warn("couldn't read 'dtrace_helptrace_next'");
13177c478bdstevel@tonic-gate		return (WALK_ERR);
13187c478bdstevel@tonic-gate	}
13197c478bdstevel@tonic-gate
13207c478bdstevel@tonic-gate	if (mdb_readvar(&bufsize, "dtrace_helptrace_bufsize") == -1) {
13217c478bdstevel@tonic-gate		mdb_warn("couldn't read 'dtrace_helptrace_bufsize'");
13227c478bdstevel@tonic-gate		return (WALK_ERR);
13237c478bdstevel@tonic-gate	}
13247c478bdstevel@tonic-gate
13257c478bdstevel@tonic-gate	if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) {
13267c478bdstevel@tonic-gate		mdb_warn("couldn't read 'dtrace_helptrace_buffer'");
13277c478bdstevel@tonic-gate		return (WALK_ERR);
13287c478bdstevel@tonic-gate	}
13297c478bdstevel@tonic-gate
13307c478bdstevel@tonic-gate	if (mdb_readvar(&nlocals, "dtrace_helptrace_nlocals") == -1) {
13317c478bdstevel@tonic-gate		mdb_warn("couldn't read 'dtrace_helptrace_nlocals'");
13327c478bdstevel@tonic-gate		return (WALK_ERR);
13337c478bdstevel@tonic-gate	}
13347c478bdstevel@tonic-gate
13357c478bdstevel@tonic-gate	size = sizeof (dtrace_helptrace_t) +
13367c478bdstevel@tonic-gate	    nlocals * sizeof (uint64_t) - sizeof (uint64_t);
13377c478bdstevel@tonic-gate
13387c478bdstevel@tonic-gate	if (wsp->walk_addr + size > bufsize) {
13397c478bdstevel@tonic-gate		if (next == 0)
13407c478bdstevel@tonic-gate			return (WALK_DONE);
13417c478bdstevel@tonic-gate
13427c478bdstevel@tonic-gate		wsp->walk_addr = 0;
13437c478bdstevel@tonic-gate	}
13447c478bdstevel@tonic-gate
13457c478bdstevel@tonic-gate	addr = buffer + wsp->walk_addr;
13467c478bdstevel@tonic-gate	ht = alloca(size);
13477c478bdstevel@tonic-gate
13487c478bdstevel@tonic-gate	if (mdb_vread(ht, size, addr) == -1) {
13497c478bdstevel@tonic-gate		mdb_warn("couldn't read entry at %p", addr);
13507c478bdstevel@tonic-gate		return (WALK_ERR);
13517c478bdstevel@tonic-gate	}
13527c478bdstevel@tonic-gate
13537c478bdstevel@tonic-gate	if (ht->dtht_helper != NULL) {
13547c478bdstevel@tonic-gate		rval = wsp->walk_callback(addr, ht, wsp->walk_cbdata);
13557c478bdstevel@tonic-gate
13567c478bdstevel@tonic-gate		if (rval != WALK_NEXT)
13577c478bdstevel@tonic-gate			return (rval);
13587c478bdstevel@tonic-gate	}
13597c478bdstevel@tonic-gate
13607c478bdstevel@tonic-gate	if (wsp->walk_addr < next && wsp->walk_addr + size >= next)
13617c478bdstevel@tonic-gate		return (WALK_DONE);
13627c478bdstevel@tonic-gate
13637c478bdstevel@tonic-gate	wsp->walk_addr += size;
13647c478bdstevel@tonic-gate	return (WALK_NEXT);
13657c478bdstevel@tonic-gate}
13667c478bdstevel@tonic-gate
13677c478bdstevel@tonic-gateint
13687c478bdstevel@tonic-gatedtrace_helptrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
13697c478bdstevel@tonic-gate{
13707c478bdstevel@tonic-gate	dtrace_helptrace_t help;
13717c478bdstevel@tonic-gate	dtrace_helper_action_t helper;
13727c478bdstevel@tonic-gate	char where[30];
13737c478bdstevel@tonic-gate	uint_t opt_v = FALSE;
13747c478bdstevel@tonic-gate	uintptr_t haddr;
13757c478bdstevel@tonic-gate
13767c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC)) {
13777c478bdstevel@tonic-gate		if (mdb_walk_dcmd("dtrace_helptrace", "dtrace_helptrace",
13787c478bdstevel@tonic-gate		    argc, argv) == -1) {
13797c478bdstevel@tonic-gate			mdb_warn("can't walk 'dtrace_helptrace'");
13807c478bdstevel@tonic-gate			return (DCMD_ERR);
13817c478bdstevel@tonic-gate		}
13827c478bdstevel@tonic-gate
13837c478bdstevel@tonic-gate		return (DCMD_OK);
13847c478bdstevel@tonic-gate	}
13857c478bdstevel@tonic-gate
13867c478bdstevel@tonic-gate	if (mdb_getopts(argc, argv, 'v',
13877c478bdstevel@tonic-gate	    MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc)
13887c478bdstevel@tonic-gate		return (DCMD_USAGE);
13897c478bdstevel@tonic-gate
13907c478bdstevel@tonic-gate	if (DCMD_HDRSPEC(flags)) {
13917c478bdstevel@tonic-gate		mdb_printf(" %?s %?s %12s %s\n",
13927c478bdstevel@tonic-gate		    "ADDR", "HELPER", "WHERE", "DIFO");
13937c478bdstevel@tonic-gate	}
13947c478bdstevel@tonic-gate
13957c478bdstevel@tonic-gate	if (mdb_vread(&help, sizeof (help), addr) == -1) {
13967c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_helptrace_t at %p", addr);
13977c478bdstevel@tonic-gate		return (DCMD_ERR);
13987c478bdstevel@tonic-gate	}
13997c478bdstevel@tonic-gate
14007c478bdstevel@tonic-gate	switch (help.dtht_where) {
14017c478bdstevel@tonic-gate	case 0:
14027c478bdstevel@tonic-gate		(void) mdb_snprintf(where, sizeof (where), "predicate");
14037c478bdstevel@tonic-gate		break;
14047c478bdstevel@tonic-gate
14057c478bdstevel@tonic-gate	case DTRACE_HELPTRACE_NEXT:
14067c478bdstevel@tonic-gate		(void) mdb_snprintf(where, sizeof (where), "next");
14077c478bdstevel@tonic-gate		break;
14087c478bdstevel@tonic-gate
14097c478bdstevel@tonic-gate	case DTRACE_HELPTRACE_DONE:
14107c478bdstevel@tonic-gate		(void) mdb_snprintf(where, sizeof (where), "done");
14117c478bdstevel@tonic-gate		break;
14127c478bdstevel@tonic-gate
14137c478bdstevel@tonic-gate	case DTRACE_HELPTRACE_ERR:
14147c478bdstevel@tonic-gate		(void) mdb_snprintf(where, sizeof (where), "err");
14157c478bdstevel@tonic-gate		break;
14167c478bdstevel@tonic-gate
14177c478bdstevel@tonic-gate	default:
14187c478bdstevel@tonic-gate		(void) mdb_snprintf(where, sizeof (where),
14197c478bdstevel@tonic-gate		    "action #%d", help.dtht_where);
14207c478bdstevel@tonic-gate		break;
14217c478bdstevel@tonic-gate	}
14227c478bdstevel@tonic-gate
14237c478bdstevel@tonic-gate	mdb_printf(" %?p %?p %12s ", addr, help.dtht_helper, where);
14247c478bdstevel@tonic-gate
14257c478bdstevel@tonic-gate	haddr = (uintptr_t)help.dtht_helper;
14267c478bdstevel@tonic-gate
14277c478bdstevel@tonic-gate	if (mdb_vread(&helper, sizeof (helper), haddr) == -1) {
14287c478bdstevel@tonic-gate		/*
14297c478bdstevel@tonic-gate		 * We're not going to warn in this case -- we're just not going
14307c478bdstevel@tonic-gate		 * to print anything exciting.
14317c478bdstevel@tonic-gate		 */
14327c478bdstevel@tonic-gate		mdb_printf("???\n");
14337c478bdstevel@tonic-gate	} else {
14347c478bdstevel@tonic-gate		switch (help.dtht_where) {
14357c478bdstevel@tonic-gate		case 0:
143699fd1a4ahl			mdb_printf("%p\n", helper.dtha_predicate);
14377c478bdstevel@tonic-gate			break;
14387c478bdstevel@tonic-gate
14397c478bdstevel@tonic-gate		case DTRACE_HELPTRACE_NEXT:
14407c478bdstevel@tonic-gate		case DTRACE_HELPTRACE_DONE:
14417c478bdstevel@tonic-gate		case DTRACE_HELPTRACE_ERR:
14427c478bdstevel@tonic-gate			mdb_printf("-\n");
14437c478bdstevel@tonic-gate			break;
14447c478bdstevel@tonic-gate
14457c478bdstevel@tonic-gate		default:
144699fd1a4ahl			haddr = (uintptr_t)helper.dtha_actions +
14477c478bdstevel@tonic-gate			    (help.dtht_where - 1) * sizeof (uintptr_t);
14487c478bdstevel@tonic-gate
14497c478bdstevel@tonic-gate			if (mdb_vread(&haddr, sizeof (haddr), haddr) == -1) {
14507c478bdstevel@tonic-gate				mdb_printf("???\n");
14517c478bdstevel@tonic-gate			} else {
14527c478bdstevel@tonic-gate				mdb_printf("%p\n", haddr);
14537c478bdstevel@tonic-gate			}
14547c478bdstevel@tonic-gate		}
14557c478bdstevel@tonic-gate	}
14567c478bdstevel@tonic-gate
14577c478bdstevel@tonic-gate	if (opt_v) {
14587c478bdstevel@tonic-gate		int i;
14597c478bdstevel@tonic-gate
1460187eccfbmc		if (help.dtht_where == DTRACE_HELPTRACE_ERR) {
1461187eccfbmc			int f = help.dtht_fault;
1462187eccfbmc
1463187eccfbmc			mdb_printf("%?s| %?s %10s |\n", "", "", "");
1464187eccfbmc			mdb_printf("%?s| %?s %10s +->  fault: %s\n", "", "", "",
1465187eccfbmc			    f == DTRACEFLT_BADADDR ? "BADADDR" :
1466187eccfbmc			    f == DTRACEFLT_BADALIGN ? "BADALIGN" :
1467187eccfbmc			    f == DTRACEFLT_ILLOP ? "ILLOP" :
1468187eccfbmc			    f == DTRACEFLT_DIVZERO ? "DIVZERO" :
1469187eccfbmc			    f == DTRACEFLT_NOSCRATCH ? "NOSCRATCH" :
1470187eccfbmc			    f == DTRACEFLT_KPRIV ? "KPRIV" :
1471187eccfbmc			    f == DTRACEFLT_UPRIV ? "UPRIV" :
1472187eccfbmc			    f == DTRACEFLT_TUPOFLOW ? "TUPOFLOW" :
1473b8fac8ejhaslam			    f == DTRACEFLT_BADSTACK ? "BADSTACK" :
1474187eccfbmc			    "DTRACEFLT_UNKNOWN");
1475187eccfbmc			mdb_printf("%?s| %?s %12s     addr: 0x%x\n", "", "", "",
1476187eccfbmc			    help.dtht_illval);
1477187eccfbmc			mdb_printf("%?s| %?s %12s   offset: %d\n", "", "", "",
1478187eccfbmc			    help.dtht_fltoffs);
1479187eccfbmc		}
1480187eccfbmc
14817c478bdstevel@tonic-gate		mdb_printf("%?s|\n%?s+--> %?s %4s %s\n", "", "",
14827c478bdstevel@tonic-gate		    "ADDR", "NDX", "VALUE");
14837c478bdstevel@tonic-gate		addr += sizeof (help) - sizeof (uint64_t);
14847c478bdstevel@tonic-gate
14857c478bdstevel@tonic-gate		for (i = 0; i < help.dtht_nlocals; i++) {
14867c478bdstevel@tonic-gate			uint64_t val;
14877c478bdstevel@tonic-gate
14887c478bdstevel@tonic-gate			if (mdb_vread(&val, sizeof (val), addr) == -1) {
14897c478bdstevel@tonic-gate				mdb_warn("couldn't read local at %p", addr);
14907c478bdstevel@tonic-gate				continue;
14917c478bdstevel@tonic-gate			}
14927c478bdstevel@tonic-gate
14937c478bdstevel@tonic-gate			mdb_printf("%?s     %?p %4d %p\n", "", addr, i, val);
14947c478bdstevel@tonic-gate			addr += sizeof (uint64_t);
14957c478bdstevel@tonic-gate		}
14967c478bdstevel@tonic-gate
14977c478bdstevel@tonic-gate		mdb_printf("\n");
14987c478bdstevel@tonic-gate	}
14997c478bdstevel@tonic-gate
15007c478bdstevel@tonic-gate	return (DCMD_OK);
15017c478bdstevel@tonic-gate}
15027c478bdstevel@tonic-gate
15037c478bdstevel@tonic-gate/*ARGSUSED*/
15047c478bdstevel@tonic-gatestatic int
15057c478bdstevel@tonic-gatedtrace_state_walk(uintptr_t addr, const vmem_seg_t *seg, minor_t *highest)
15067c478bdstevel@tonic-gate{
15077c478bdstevel@tonic-gate	if (seg->vs_end > *highest)
15087c478bdstevel@tonic-gate		*highest = seg->vs_end;
15097c478bdstevel@tonic-gate
15107c478bdstevel@tonic-gate	return (WALK_NEXT);
15117c478bdstevel@tonic-gate}
15127c478bdstevel@tonic-gate
15137c478bdstevel@tonic-gatetypedef struct dtrace_state_walk {
15147c478bdstevel@tonic-gate	uintptr_t dtsw_softstate;
15157c478bdstevel@tonic-gate	minor_t dtsw_max;
15167c478bdstevel@tonic-gate	minor_t dtsw_current;
15177c478bdstevel@tonic-gate} dtrace_state_walk_t;
15187c478bdstevel@tonic-gate
15197c478bdstevel@tonic-gateint
15207c478bdstevel@tonic-gatedtrace_state_init(mdb_walk_state_t *wsp)
15217c478bdstevel@tonic-gate{
15227c478bdstevel@tonic-gate	uintptr_t dtrace_minor;
15237c478bdstevel@tonic-gate	minor_t max = 0;
15247c478bdstevel@tonic-gate	dtrace_state_walk_t *dw;
15257c478bdstevel@tonic-gate
1526892ad16Toomas Soome	if (wsp->walk_addr != 0) {
15277c478bdstevel@tonic-gate		mdb_warn("dtrace_state only supports global walks\n");
15287c478bdstevel@tonic-gate		return (WALK_ERR);
15297c478bdstevel@tonic-gate	}
15307c478bdstevel@tonic-gate
15317c478bdstevel@tonic-gate	/*
15327c478bdstevel@tonic-gate	 * Find the dtrace_minor vmem arena and walk it to get the maximum
15337c478bdstevel@tonic-gate	 * minor number.
15347c478bdstevel@tonic-gate	 */
15357c478bdstevel@tonic-gate	if (mdb_readvar(&dtrace_minor, "dtrace_minor") == -1) {
15367c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_minor'");
15377c478bdstevel@tonic-gate		return (WALK_ERR);
15387c478bdstevel@tonic-gate	}
15397c478bdstevel@tonic-gate
15407c478bdstevel@tonic-gate	if (mdb_pwalk("vmem_alloc", (mdb_walk_cb_t)dtrace_state_walk,
15417c478bdstevel@tonic-gate	    &max, dtrace_minor) == -1) {
15427c478bdstevel@tonic-gate		mdb_warn("couldn't walk 'vmem_alloc'");
15437c478bdstevel@tonic-gate		return (WALK_ERR);
15447c478bdstevel@tonic-gate	}
15457c478bdstevel@tonic-gate
15467c478bdstevel@tonic-gate	dw = mdb_zalloc(sizeof (dtrace_state_walk_t), UM_SLEEP | UM_GC);
15477c478bdstevel@tonic-gate	dw->dtsw_current = 0;
15487c478bdstevel@tonic-gate	dw->dtsw_max = max;
15497c478bdstevel@tonic-gate
15507c478bdstevel@tonic-gate	if (mdb_readvar(&dw->dtsw_softstate, "dtrace_softstate") == -1) {
15517c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_softstate'");
15527c478bdstevel@tonic-gate		return (DCMD_ERR);
15537c478bdstevel@tonic-gate	}
15547c478bdstevel@tonic-gate
15557c478bdstevel@tonic-gate	wsp->walk_data = dw;
15567c478bdstevel@tonic-gate
15577c478bdstevel@tonic-gate	return (WALK_NEXT);
15587c478bdstevel@tonic-gate}
15597c478bdstevel@tonic-gate
15607c478bdstevel@tonic-gateint
15617c478bdstevel@tonic-gatedtrace_state_step(mdb_walk_state_t *wsp)
15627c478bdstevel@tonic-gate{
15637c478bdstevel@tonic-gate	dtrace_state_walk_t *dw = wsp->walk_data;
15647c478bdstevel@tonic-gate	uintptr_t statep;
15657c478bdstevel@tonic-gate	dtrace_state_t state;
15667c478bdstevel@tonic-gate	int rval;
15677c478bdstevel@tonic-gate
15687c478bdstevel@tonic-gate	while (mdb_get_soft_state_byaddr(dw->dtsw_softstate, dw->dtsw_current,
15697c478bdstevel@tonic-gate	    &statep, NULL, 0) == -1) {
15707c478bdstevel@tonic-gate		if (dw->dtsw_current >= dw->dtsw_max)
15717c478bdstevel@tonic-gate			return (WALK_DONE);
15727c478bdstevel@tonic-gate
15737c478bdstevel@tonic-gate		dw->dtsw_current++;
15747c478bdstevel@tonic-gate	}
15757c478bdstevel@tonic-gate
15767c478bdstevel@tonic-gate	if (mdb_vread(&state, sizeof (state), statep) == -1) {
15777c478bdstevel@tonic-gate		mdb_warn("couldn't read dtrace_state_t at %p", statep);
15787c478bdstevel@tonic-gate		return (WALK_NEXT);
15797c478bdstevel@tonic-gate	}
15807c478bdstevel@tonic-gate
15817c478bdstevel@tonic-gate	rval = wsp->walk_callback(statep, &state, wsp->walk_cbdata);
15827c478bdstevel@tonic-gate	dw->dtsw_current++;
15837c478bdstevel@tonic-gate
15847c478bdstevel@tonic-gate	return (rval);
15857c478bdstevel@tonic-gate}
15867c478bdstevel@tonic-gate
15877c478bdstevel@tonic-gatetypedef struct dtrace_state_data {
15887c478bdstevel@tonic-gate	int dtsd_major;
15897c478bdstevel@tonic-gate	uintptr_t dtsd_proc;
15907c478bdstevel@tonic-gate	uintptr_t dtsd_softstate;
15917c478bdstevel@tonic-gate	uintptr_t dtsd_state;
15927c478bdstevel@tonic-gate} dtrace_state_data_t;
15937c478bdstevel@tonic-gate
15947c478bdstevel@tonic-gatestatic int
15957c478bdstevel@tonic-gatedtrace_state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data)
15967c478bdstevel@tonic-gate{
15977c478bdstevel@tonic-gate	vnode_t vnode;
15987c478bdstevel@tonic-gate	proc_t proc;
15997c478bdstevel@tonic-gate	minor_t minor;
16007c478bdstevel@tonic-gate	uintptr_t statep;
16017c478bdstevel@tonic-gate
16027c478bdstevel@tonic-gate	if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) {
16037c478bdstevel@tonic-gate		mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode);
16047c478bdstevel@tonic-gate		return (WALK_NEXT);
16057c478bdstevel@tonic-gate	}
16067c478bdstevel@tonic-gate
16077c478bdstevel@tonic-gate	if (getmajor(vnode.v_rdev) != data->dtsd_major)
16087c478bdstevel@tonic-gate		return (WALK_NEXT);
16097c478bdstevel@tonic-gate
16107c478bdstevel@tonic-gate	minor = getminor(vnode.v_rdev);
16117c478bdstevel@tonic-gate
16127c478bdstevel@tonic-gate	if (mdb_vread(&proc, sizeof (proc), data->dtsd_proc) == -1) {
16137c478bdstevel@tonic-gate		mdb_warn("failed to read proc at %p", data->dtsd_proc);
16147c478bdstevel@tonic-gate		return (WALK_NEXT);
16157c478bdstevel@tonic-gate	}
16167c478bdstevel@tonic-gate
16177c478bdstevel@tonic-gate	if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor,
16187c478bdstevel@tonic-gate	    &statep, NULL, 0) == -1) {
16197c478bdstevel@tonic-gate		mdb_warn("failed to read softstate for minor %d", minor);
16207c478bdstevel@tonic-gate		return (WALK_NEXT);
16217c478bdstevel@tonic-gate	}
16227c478bdstevel@tonic-gate
16237c478bdstevel@tonic-gate	if (statep != data->dtsd_state)
16247c478bdstevel@tonic-gate		return (WALK_NEXT);
16257c478bdstevel@tonic-gate
16267c478bdstevel@tonic-gate	mdb_printf("%?p %5d %?p %-*s %?p\n", statep, minor,
16277c478bdstevel@tonic-gate	    data->dtsd_proc, MAXCOMLEN, proc.p_user.u_comm, addr);
16287c478bdstevel@tonic-gate
16297c478bdstevel@tonic-gate	return (WALK_NEXT);
16307c478bdstevel@tonic-gate}
16317c478bdstevel@tonic-gate
16327c478bdstevel@tonic-gate/*ARGSUSED*/
16337c478bdstevel@tonic-gatestatic int
16347c478bdstevel@tonic-gatedtrace_state_proc(uintptr_t addr, void *ignored, dtrace_state_data_t *data)
16357c478bdstevel@tonic-gate{
16367c478bdstevel@tonic-gate	data->dtsd_proc = addr;
16377c478bdstevel@tonic-gate
16387c478bdstevel@tonic-gate	if (mdb_pwalk("file",
16397c478bdstevel@tonic-gate	    (mdb_walk_cb_t)dtrace_state_file, data, addr) == -1) {
16407c478bdstevel@tonic-gate		mdb_warn("couldn't walk 'file' for proc %p", addr);
16417c478bdstevel@tonic-gate		return (WALK_ERR);
16427c478bdstevel@tonic-gate	}
16437c478bdstevel@tonic-gate
16447c478bdstevel@tonic-gate	return (WALK_NEXT);
16457c478bdstevel@tonic-gate}
16467c478bdstevel@tonic-gate
16477c478bdstevel@tonic-gatevoid
16487c478bdstevel@tonic-gatedtrace_state_help(void)
16497c478bdstevel@tonic-gate{
16507c478bdstevel@tonic-gate	mdb_printf("Given a dtrace_state_t structure, displays all "
16517c478bdstevel@tonic-gate	    /*CSTYLED*/
16527c478bdstevel@tonic-gate	    "consumers, or \"<anonymous>\"\nif the consumer is anonymous.  If "
16537c478bdstevel@tonic-gate	    "no state structure is provided, iterates\nover all state "
16547c478bdstevel@tonic-gate	    "structures.\n\n"
16557c478bdstevel@tonic-gate	    "Addresses in ADDR column may be provided to ::dtrace to obtain\n"
16567c478bdstevel@tonic-gate	    "dtrace(1M)-like output for in-kernel DTrace data.\n");
16577c478bdstevel@tonic-gate}
16587c478bdstevel@tonic-gate
16597c478bdstevel@tonic-gateint
16607c478bdstevel@tonic-gatedtrace_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
16617c478bdstevel@tonic-gate{
16627c478bdstevel@tonic-gate	uintptr_t devi;
16637c478bdstevel@tonic-gate	struct dev_info info;
16647c478bdstevel@tonic-gate	dtrace_state_data_t data;
16657c478bdstevel@tonic-gate	dtrace_anon_t anon;
16667c478bdstevel@tonic-gate	dtrace_state_t state;
16677c478bdstevel@tonic-gate
16687c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC)) {
16697c478bdstevel@tonic-gate		if (mdb_walk_dcmd("dtrace_state",
16707c478bdstevel@tonic-gate		    "dtrace_state", argc, argv) == -1) {
16717c478bdstevel@tonic-gate			mdb_warn("can't walk dtrace_state");
16727c478bdstevel@tonic-gate			return (DCMD_ERR);
16737c478bdstevel@tonic-gate		}
16747c478bdstevel@tonic-gate		return (DCMD_OK);
16757c478bdstevel@tonic-gate	}
16767c478bdstevel@tonic-gate
16777c478bdstevel@tonic-gate	if (DCMD_HDRSPEC(flags)) {
16787c478bdstevel@tonic-gate		mdb_printf("%?s %5s %?s %-*s %?s\n", "ADDR", "MINOR", "PROC",
16797c478bdstevel@tonic-gate		    MAXCOMLEN, "NAME", "FILE");
16807c478bdstevel@tonic-gate	}
16817c478bdstevel@tonic-gate
16827c478bdstevel@tonic-gate	/*
16837c478bdstevel@tonic-gate	 * First determine if this is anonymous state.
16847c478bdstevel@tonic-gate	 */
16857c478bdstevel@tonic-gate	if (mdb_readvar(&anon, "dtrace_anon") == -1) {
16867c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_anon'");
16877c478bdstevel@tonic-gate		return (DCMD_ERR);
16887c478bdstevel@tonic-gate	}
16897c478bdstevel@tonic-gate
16907c478bdstevel@tonic-gate	if ((uintptr_t)anon.dta_state == addr) {
16917c478bdstevel@tonic-gate		if (mdb_vread(&state, sizeof (state), addr) == -1) {
16927c478bdstevel@tonic-gate			mdb_warn("failed to read anon at %p", addr);
16937c478bdstevel@tonic-gate			return (DCMD_ERR);
16947c478bdstevel@tonic-gate		}
16957c478bdstevel@tonic-gate
16967c478bdstevel@tonic-gate		mdb_printf("%?p %5d %?s %-*s %?s\n", addr,
16977c478bdstevel@tonic-gate		    getminor(state.dts_dev), "-", MAXCOMLEN,
16987c478bdstevel@tonic-gate		    "<anonymous>", "-");
16997c478bdstevel@tonic-gate
17007c478bdstevel@tonic-gate		return (DCMD_OK);
17017c478bdstevel@tonic-gate	}
17027c478bdstevel@tonic-gate
17037c478bdstevel@tonic-gate	if (mdb_readvar(&devi, "dtrace_devi") == -1) {
17047c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_devi'");
17057c478bdstevel@tonic-gate		return (DCMD_ERR);
17067c478bdstevel@tonic-gate	}
17077c478bdstevel@tonic-gate
17087c478bdstevel@tonic-gate	if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) {
17097c478bdstevel@tonic-gate		mdb_warn("failed to read 'dev_info'");
17107c478bdstevel@tonic-gate		return (DCMD_ERR);
17117c478bdstevel@tonic-gate	}
17127c478bdstevel@tonic-gate
17137c478bdstevel@tonic-gate	data.dtsd_major = info.devi_major;
17147c478bdstevel@tonic-gate
17157c478bdstevel@tonic-gate	if (mdb_readvar(&data.dtsd_softstate, "dtrace_softstate") == -1) {
17167c478bdstevel@tonic-gate		mdb_warn("failed to read 'dtrace_softstate'");
17177c478bdstevel@tonic-gate		return (DCMD_ERR);
17187c478bdstevel@tonic-gate	}
17197c478bdstevel@tonic-gate
17207c478bdstevel@tonic-gate	data.dtsd_state = addr;
17217c478bdstevel@tonic-gate
17227c478bdstevel@tonic-gate	/*
17237c478bdstevel@tonic-gate	 * Walk through all processes and all open files looking for this
17247c478bdstevel@tonic-gate	 * state.  It must be open somewhere...
17257c478bdstevel@tonic-gate	 */
17267c478bdstevel@tonic-gate	if (mdb_walk("proc", (mdb_walk_cb_t)dtrace_state_proc, &data) == -1) {
17277c478bdstevel@tonic-gate		mdb_warn("couldn't walk 'proc'");
17287c478bdstevel@tonic-gate		return (DCMD_ERR);
17297c478bdstevel@tonic-gate	}
17307c478bdstevel@tonic-gate
17317c478bdstevel@tonic-gate	return (DCMD_OK);
17327c478bdstevel@tonic-gate}
17337c478bdstevel@tonic-gate
17347c478bdstevel@tonic-gatetypedef struct dtrace_aggkey_data {
17357c478bdstevel@tonic-gate	uintptr_t *dtakd_hash;
17367c478bdstevel@tonic-gate	uintptr_t dtakd_hashsize;
17377c478bdstevel@tonic-gate	uintptr_t dtakd_next;
17387c478bdstevel@tonic-gate	uintptr_t dtakd_ndx;
17397c478bdstevel@tonic-gate} dtrace_aggkey_data_t;
17407c478bdstevel@tonic-gate
17417c478bdstevel@tonic-gateint
17427c478bdstevel@tonic-gatedtrace_aggkey_init(mdb_walk_state_t *wsp)
17437c478bdstevel@tonic-gate{
17447c478bdstevel@tonic-gate	dtrace_buffer_t buf;
17457c478bdstevel@tonic-gate	uintptr_t addr;
17467c478bdstevel@tonic-gate	dtrace_aggbuffer_t agb;
17477c478bdstevel@tonic-gate	dtrace_aggkey_data_t *data;
17487c478bdstevel@tonic-gate	size_t hsize;
17497c478bdstevel@tonic-gate
1750892ad16Toomas Soome	if ((addr = wsp->walk_addr) == 0) {
17517c478bdstevel@tonic-gate		mdb_warn("dtrace_aggkey walk needs aggregation buffer\n");
17527c478bdstevel@tonic-gate		return (WALK_ERR);
17537c478bdstevel@tonic-gate	}
17547c478bdstevel@tonic-gate
17557c478bdstevel@tonic-gate	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
17567c478bdstevel@tonic-gate		mdb_warn("failed to read aggregation buffer at %p", addr);
17577c478bdstevel@tonic-gate		return (WALK_ERR);
17587c478bdstevel@tonic-gate	}
17597c478bdstevel@tonic-gate
17607c478bdstevel@tonic-gate	addr = (uintptr_t)buf.dtb_tomax +
17617c478bdstevel@tonic-gate	    buf.dtb_size - sizeof (dtrace_aggbuffer_t);
17627c478bdstevel@tonic-gate
17637c478bdstevel@tonic-gate	if (mdb_vread(&agb, sizeof (agb), addr) == -1) {
17647c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_aggbuffer_t at %p", addr);
17657c478bdstevel@tonic-gate		return (WALK_ERR);
17667c478bdstevel@tonic-gate	}
17677c478bdstevel@tonic-gate
17687c478bdstevel@tonic-gate	data = mdb_zalloc(sizeof (dtrace_aggkey_data_t), UM_SLEEP);
17697c478bdstevel@tonic-gate
17707c478bdstevel@tonic-gate	data->dtakd_hashsize = agb.dtagb_hashsize;
17717c478bdstevel@tonic-gate	hsize = agb.dtagb_hashsize * sizeof (dtrace_aggkey_t *);
17727c478bdstevel@tonic-gate	data->dtakd_hash = mdb_alloc(hsize, UM_SLEEP);
17737c478bdstevel@tonic-gate
17747c478bdstevel@tonic-gate	if (mdb_vread(data->dtakd_hash, hsize,
17757c478bdstevel@tonic-gate	    (uintptr_t)agb.dtagb_hash) == -1) {
17767c478bdstevel@tonic-gate		mdb_warn("failed to read hash at %p",
17777c478bdstevel@tonic-gate		    (uintptr_t)agb.dtagb_hash);
17787c478bdstevel@tonic-gate		mdb_free(data->dtakd_hash, hsize);
17797c478bdstevel@tonic-gate		mdb_free(data, sizeof (dtrace_aggkey_data_t));
17807c478bdstevel@tonic-gate		return (WALK_ERR);
17817c478bdstevel@tonic-gate	}
17827c478bdstevel@tonic-gate
17837c478bdstevel@tonic-gate	wsp->walk_data = data;
17847c478bdstevel@tonic-gate	return (WALK_NEXT);
17857c478bdstevel@tonic-gate}
17867c478bdstevel@tonic-gate
17877c478bdstevel@tonic-gateint
17887c478bdstevel@tonic-gatedtrace_aggkey_step(mdb_walk_state_t *wsp)
17897c478bdstevel@tonic-gate{
17907c478bdstevel@tonic-gate	dtrace_aggkey_data_t *data = wsp->walk_data;
17917c478bdstevel@tonic-gate	dtrace_aggkey_t key;
17927c478bdstevel@tonic-gate	uintptr_t addr;
17937c478bdstevel@tonic-gate
1794892ad16Toomas Soome	while ((addr = data->dtakd_next) == 0) {
17957c478bdstevel@tonic-gate		if (data->dtakd_ndx == data->dtakd_hashsize)
17967c478bdstevel@tonic-gate			return (WALK_DONE);
17977c478bdstevel@tonic-gate
17987c478bdstevel@tonic-gate		data->dtakd_next = data->dtakd_hash[data->dtakd_ndx++];
17997c478bdstevel@tonic-gate	}
18007c478bdstevel@tonic-gate
18017c478bdstevel@tonic-gate	if (mdb_vread(&key, sizeof (key), addr) == -1) {
18027c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_aggkey_t at %p", addr);
18037c478bdstevel@tonic-gate		return (WALK_ERR);
18047c478bdstevel@tonic-gate	}
18057c478bdstevel@tonic-gate
18067c478bdstevel@tonic-gate	data->dtakd_next = (uintptr_t)key.dtak_next;
18077c478bdstevel@tonic-gate
18087c478bdstevel@tonic-gate	return (wsp->walk_callback(addr, &key, wsp->walk_cbdata));
18097c478bdstevel@tonic-gate}
18107c478bdstevel@tonic-gate
18117c478bdstevel@tonic-gatevoid
18127c478bdstevel@tonic-gatedtrace_aggkey_fini(mdb_walk_state_t *wsp)
18137c478bdstevel@tonic-gate{
18147c478bdstevel@tonic-gate	dtrace_aggkey_data_t *data = wsp->walk_data;
18157c478bdstevel@tonic-gate	size_t hsize;
18167c478bdstevel@tonic-gate
18177c478bdstevel@tonic-gate	hsize = data->dtakd_hashsize * sizeof (dtrace_aggkey_t *);
18187c478bdstevel@tonic-gate	mdb_free(data->dtakd_hash, hsize);
18197c478bdstevel@tonic-gate	mdb_free(data, sizeof (dtrace_aggkey_data_t));
18207c478bdstevel@tonic-gate}
18217c478bdstevel@tonic-gate
18227c478bdstevel@tonic-gatetypedef struct dtrace_dynvar_data {
18237c478bdstevel@tonic-gate	dtrace_dynhash_t *dtdvd_hash;
18247c478bdstevel@tonic-gate	uintptr_t dtdvd_hashsize;
18257c478bdstevel@tonic-gate	uintptr_t dtdvd_next;
18267c478bdstevel@tonic-gate	uintptr_t dtdvd_ndx;
1827c9a6ea2Bryan Cantrill	uintptr_t dtdvd_sink;
18287c478bdstevel@tonic-gate} dtrace_dynvar_data_t;
18297c478bdstevel@tonic-gate
18307c478bdstevel@tonic-gateint
18317c478bdstevel@tonic-gatedtrace_dynvar_init(mdb_walk_state_t *wsp)
18327c478bdstevel@tonic-gate{
18337c478bdstevel@tonic-gate	uintptr_t addr;
18347c478bdstevel@tonic-gate	dtrace_dstate_t dstate;
18357c478bdstevel@tonic-gate	dtrace_dynvar_data_t *data;
18367c478bdstevel@tonic-gate	size_t hsize;
1837c9a6ea2Bryan Cantrill	GElf_Sym sym;
18387c478bdstevel@tonic-gate
1839892ad16Toomas Soome	if ((addr = wsp->walk_addr) == 0) {
18407c478bdstevel@tonic-gate		mdb_warn("dtrace_dynvar walk needs dtrace_dstate_t\n");
18417c478bdstevel@tonic-gate		return (WALK_ERR);
18427c478bdstevel@tonic-gate	}
18437c478bdstevel@tonic-gate
18447c478bdstevel@tonic-gate	if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
18457c478bdstevel@tonic-gate		mdb_warn("failed to read dynamic state at %p", addr);
18467c478bdstevel@tonic-gate		return (WALK_ERR);
18477c478bdstevel@tonic-gate	}
18487c478bdstevel@tonic-gate
1849c9a6ea2Bryan Cantrill	if (mdb_lookup_by_name("dtrace_dynhash_sink", &sym) == -1) {
1850c9a6ea2Bryan Cantrill		mdb_warn("couldn't find 'dtrace_dynhash_sink'");
1851c9a6ea2Bryan Cantrill		return (WALK_ERR);
1852c9a6ea2Bryan Cantrill	}
1853c9a6ea2Bryan Cantrill
18547c478bdstevel@tonic-gate	data = mdb_zalloc(sizeof (dtrace_dynvar_data_t), UM_SLEEP);
18557c478bdstevel@tonic-gate
18567c478bdstevel@tonic-gate	data->dtdvd_hashsize = dstate.dtds_hashsize;
18577c478bdstevel@tonic-gate	hsize = dstate.dtds_hashsize * sizeof (dtrace_dynhash_t);
18587c478bdstevel@tonic-gate	data->dtdvd_hash = mdb_alloc(hsize, UM_SLEEP);
1859c9a6ea2Bryan Cantrill	data->dtdvd_sink = (uintptr_t)sym.st_value;
18607c478bdstevel@tonic-gate
18617c478bdstevel@tonic-gate	if (mdb_vread(data->dtdvd_hash, hsize,
18627c478bdstevel@tonic-gate	    (uintptr_t)dstate.dtds_hash) == -1) {
18637c478bdstevel@tonic-gate		mdb_warn("failed to read hash at %p",
18647c478bdstevel@tonic-gate		    (uintptr_t)dstate.dtds_hash);
18657c478bdstevel@tonic-gate		mdb_free(data->dtdvd_hash, hsize);
18667c478bdstevel@tonic-gate		mdb_free(data, sizeof (dtrace_dynvar_data_t));
18677c478bdstevel@tonic-gate		return (WALK_ERR);
18687c478bdstevel@tonic-gate	}
18697c478bdstevel@tonic-gate
1870c9a6ea2Bryan Cantrill	data->dtdvd_next = (uintptr_t)data->dtdvd_hash[0].dtdh_chain;
1871c9a6ea2Bryan Cantrill
18727c478bdstevel@tonic-gate	wsp->walk_data = data;
18737c478bdstevel@tonic-gate	return (WALK_NEXT);
18747c478bdstevel@tonic-gate}
18757c478bdstevel@tonic-gate
18767c478bdstevel@tonic-gateint
18777c478bdstevel@tonic-gatedtrace_dynvar_step(mdb_walk_state_t *wsp)
18787c478bdstevel@tonic-gate{
18797c478bdstevel@tonic-gate	dtrace_dynvar_data_t *data = wsp->walk_data;
18807c478bdstevel@tonic-gate	dtrace_dynvar_t dynvar, *dvar;
18817c478bdstevel@tonic-gate	size_t dvarsize;
18827c478bdstevel@tonic-gate	uintptr_t addr;
18837c478bdstevel@tonic-gate	int nkeys;
18847c478bdstevel@tonic-gate
1885c9a6ea2Bryan Cantrill	while ((addr = data->dtdvd_next) == data->dtdvd_sink) {
18867c478bdstevel@tonic-gate		if (data->dtdvd_ndx == data->dtdvd_hashsize)
18877c478bdstevel@tonic-gate			return (WALK_DONE);
18887c478bdstevel@tonic-gate
18897c478bdstevel@tonic-gate		data->dtdvd_next =
18907c478bdstevel@tonic-gate		    (uintptr_t)data->dtdvd_hash[data->dtdvd_ndx++].dtdh_chain;
18917c478bdstevel@tonic-gate	}
18927c478bdstevel@tonic-gate
18937c478bdstevel@tonic-gate	if (mdb_vread(&dynvar, sizeof (dynvar), addr) == -1) {
18947c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
18957c478bdstevel@tonic-gate		return (WALK_ERR);
18967c478bdstevel@tonic-gate	}
18977c478bdstevel@tonic-gate
18987c478bdstevel@tonic-gate	/*
18997c478bdstevel@tonic-gate	 * Now we need to allocate the correct size.
19007c478bdstevel@tonic-gate	 */
19017c478bdstevel@tonic-gate	nkeys = dynvar.dtdv_tuple.dtt_nkeys;
19027c478bdstevel@tonic-gate	dvarsize = (uintptr_t)&dynvar.dtdv_tuple.dtt_key[nkeys] -
19037c478bdstevel@tonic-gate	    (uintptr_t)&dynvar;
19047c478bdstevel@tonic-gate
19057c478bdstevel@tonic-gate	dvar = alloca(dvarsize);
19067c478bdstevel@tonic-gate
19077c478bdstevel@tonic-gate	if (mdb_vread(dvar, dvarsize, addr) == -1) {
19087c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
19097c478bdstevel@tonic-gate		return (WALK_ERR);
19107c478bdstevel@tonic-gate	}
19117c478bdstevel@tonic-gate
19127c478bdstevel@tonic-gate	data->dtdvd_next = (uintptr_t)dynvar.dtdv_next;
19137c478bdstevel@tonic-gate
19147c478bdstevel@tonic-gate	return (wsp->walk_callback(addr, dvar, wsp->walk_cbdata));
19157c478bdstevel@tonic-gate}
19167c478bdstevel@tonic-gate
19177c478bdstevel@tonic-gatevoid
19187c478bdstevel@tonic-gatedtrace_dynvar_fini(mdb_walk_state_t *wsp)
19197c478bdstevel@tonic-gate{
19207c478bdstevel@tonic-gate	dtrace_dynvar_data_t *data = wsp->walk_data;
19217c478bdstevel@tonic-gate	size_t hsize;
19227c478bdstevel@tonic-gate
19237c478bdstevel@tonic-gate	hsize = data->dtdvd_hashsize * sizeof (dtrace_dynvar_t *);
19247c478bdstevel@tonic-gate	mdb_free(data->dtdvd_hash, hsize);
19257c478bdstevel@tonic-gate	mdb_free(data, sizeof (dtrace_dynvar_data_t));
19267c478bdstevel@tonic-gate}
19277c478bdstevel@tonic-gate
19287c478bdstevel@tonic-gatetypedef struct dtrace_hashstat_data {
19297c478bdstevel@tonic-gate	size_t *dthsd_counts;
19307c478bdstevel@tonic-gate	size_t dthsd_hashsize;
19317c478bdstevel@tonic-gate	char *dthsd_data;
19327c478bdstevel@tonic-gate	size_t dthsd_size;
19337c478bdstevel@tonic-gate	int dthsd_header;
19347c478bdstevel@tonic-gate} dtrace_hashstat_data_t;
19357c478bdstevel@tonic-gate
19367c478bdstevel@tonic-gatetypedef void (*dtrace_hashstat_func_t)(dtrace_hashstat_data_t *);
19377c478bdstevel@tonic-gate
19387c478bdstevel@tonic-gatestatic void
19397c478bdstevel@tonic-gatedtrace_hashstat_additive(dtrace_hashstat_data_t *data)
19407c478bdstevel@tonic-gate{
19417c478bdstevel@tonic-gate	int i;
19427c478bdstevel@tonic-gate	int hval = 0;
19437c478bdstevel@tonic-gate
19447c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_size; i++)
19457c478bdstevel@tonic-gate		hval += data->dthsd_data[i];
19467c478bdstevel@tonic-gate
19477c478bdstevel@tonic-gate	data->dthsd_counts[hval % data->dthsd_hashsize]++;
19487c478bdstevel@tonic-gate}
19497c478bdstevel@tonic-gate
19507c478bdstevel@tonic-gatestatic void
19517c478bdstevel@tonic-gatedtrace_hashstat_shifty(dtrace_hashstat_data_t *data)
19527c478bdstevel@tonic-gate{
19537c478bdstevel@tonic-gate	uint64_t hval = 0;
19547c478bdstevel@tonic-gate	int i;
19557c478bdstevel@tonic-gate
19567c478bdstevel@tonic-gate	if (data->dthsd_size < sizeof (uint64_t)) {
19577c478bdstevel@tonic-gate		dtrace_hashstat_additive(data);
19587c478bdstevel@tonic-gate		return;
19597c478bdstevel@tonic-gate	}
19607c478bdstevel@tonic-gate
19617c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_size; i += sizeof (uint64_t)) {
19627c478bdstevel@tonic-gate		/* LINTED - alignment */
19637c478bdstevel@tonic-gate		uint64_t val = *((uint64_t *)&data->dthsd_data[i]);
19647c478bdstevel@tonic-gate
19657c478bdstevel@tonic-gate		hval += (val & ((1 << NBBY) - 1)) +
19667c478bdstevel@tonic-gate		    ((val >> NBBY) & ((1 << NBBY) - 1)) +
19677c478bdstevel@tonic-gate		    ((val >> (NBBY << 1)) & ((1 << NBBY) - 1)) +
19687c478bdstevel@tonic-gate		    ((val >> (NBBY << 2)) & ((1 << NBBY) - 1)) +
19697c478bdstevel@tonic-gate		    (val & USHRT_MAX) + (val >> (NBBY << 1) & USHRT_MAX);
19707c478bdstevel@tonic-gate	}
19717c478bdstevel@tonic-gate
19727c478bdstevel@tonic-gate	data->dthsd_counts[hval % data->dthsd_hashsize]++;
19737c478bdstevel@tonic-gate}
19747c478bdstevel@tonic-gate
19757c478bdstevel@tonic-gatestatic void
19767c478bdstevel@tonic-gatedtrace_hashstat_knuth(dtrace_hashstat_data_t *data)
19777c478bdstevel@tonic-gate{
19787c478bdstevel@tonic-gate	int i;
19797c478bdstevel@tonic-gate	int hval = data->dthsd_size;
19807c478bdstevel@tonic-gate
19817c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_size; i++)
19827c478bdstevel@tonic-gate		hval = (hval << 4) ^ (hval >> 28) ^ data->dthsd_data[i];
19837c478bdstevel@tonic-gate
19847c478bdstevel@tonic-gate	data->dthsd_counts[hval % data->dthsd_hashsize]++;
19857c478bdstevel@tonic-gate}
19867c478bdstevel@tonic-gate
19877c478bdstevel@tonic-gatestatic void
19887c478bdstevel@tonic-gatedtrace_hashstat_oneatatime(dtrace_hashstat_data_t *data)
19897c478bdstevel@tonic-gate{
19907c478bdstevel@tonic-gate	int i;
19917c478bdstevel@tonic-gate	uint32_t hval = 0;
19927c478bdstevel@tonic-gate
19937c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_size; i++) {
19947c478bdstevel@tonic-gate		hval += data->dthsd_data[i];
19957c478bdstevel@tonic-gate		hval += (hval << 10);
19967c478bdstevel@tonic-gate		hval ^= (hval >> 6);
19977c478bdstevel@tonic-gate	}
19987c478bdstevel@tonic-gate
19997c478bdstevel@tonic-gate	hval += (hval << 3);
20007c478bdstevel@tonic-gate	hval ^= (hval >> 11);
20017c478bdstevel@tonic-gate	hval += (hval << 15);
20027c478bdstevel@tonic-gate
20037c478bdstevel@tonic-gate	data->dthsd_counts[hval % data->dthsd_hashsize]++;
20047c478bdstevel@tonic-gate}
20057c478bdstevel@tonic-gate
20067c478bdstevel@tonic-gatestatic void
20077c478bdstevel@tonic-gatedtrace_hashstat_fnv(dtrace_hashstat_data_t *data)
20087c478bdstevel@tonic-gate{
20097c478bdstevel@tonic-gate	static const uint32_t prime = 0x01000193;
20107c478bdstevel@tonic-gate	uint32_t hval = 0;
20117c478bdstevel@tonic-gate	int i;
20127c478bdstevel@tonic-gate
20137c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_size; i++) {
20147c478bdstevel@tonic-gate		hval *= prime;
20157c478bdstevel@tonic-gate		hval ^= data->dthsd_data[i];
20167c478bdstevel@tonic-gate	}
20177c478bdstevel@tonic-gate
20187c478bdstevel@tonic-gate	data->dthsd_counts[hval % data->dthsd_hashsize]++;
20197c478bdstevel@tonic-gate}
20207c478bdstevel@tonic-gate
20217c478bdstevel@tonic-gatestatic void
20227c478bdstevel@tonic-gatedtrace_hashstat_stats(char *name, dtrace_hashstat_data_t *data)
20237c478bdstevel@tonic-gate{
20247c478bdstevel@tonic-gate	size_t nz = 0, i;
20257c478bdstevel@tonic-gate	int longest = 0;
20267c478bdstevel@tonic-gate	size_t ttl = 0;
20277c478bdstevel@tonic-gate	double sum = 0.0;
20287c478bdstevel@tonic-gate	double avg;
20297c478bdstevel@tonic-gate	uint_t util, stddev;
20307c478bdstevel@tonic-gate
20317c478bdstevel@tonic-gate	if (!data->dthsd_header) {
20327c478bdstevel@tonic-gate		mdb_printf("%15s %11s %11s %11s %11s %11s\n", "NAME",
20337c478bdstevel@tonic-gate		    "HASHSIZE", "%UTIL", "LONGEST", "AVERAGE", "STDDEV");
20347c478bdstevel@tonic-gate		data->dthsd_header = 1;
20357c478bdstevel@tonic-gate	}
20367c478bdstevel@tonic-gate
20377c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_hashsize; i++) {
20387c478bdstevel@tonic-gate		if (data->dthsd_counts[i] != 0) {
20397c478bdstevel@tonic-gate			nz++;
20407c478bdstevel@tonic-gate
20417c478bdstevel@tonic-gate			if (data->dthsd_counts[i] > longest)
20427c478bdstevel@tonic-gate				longest = data->dthsd_counts[i];
20437c478bdstevel@tonic-gate
20447c478bdstevel@tonic-gate			ttl += data->dthsd_counts[i];
20457c478bdstevel@tonic-gate		}
20467c478bdstevel@tonic-gate	}
20477c478bdstevel@tonic-gate
20487c478bdstevel@tonic-gate	if (nz == 0) {
20497c478bdstevel@tonic-gate		mdb_printf("%15s %11d %11s %11s %11s %11s\n", name,
20507c478bdstevel@tonic-gate		    data->dthsd_hashsize, "-", "-", "-", "-");
20517c478bdstevel@tonic-gate		return;
20527c478bdstevel@tonic-gate	}
20537c478bdstevel@tonic-gate
20547c478bdstevel@tonic-gate	avg = (double)ttl / (double)nz;
20557c478bdstevel@tonic-gate
20567c478bdstevel@tonic-gate	for (i = 0; i < data->dthsd_hashsize; i++) {
20577c478bdstevel@tonic-gate		double delta = (double)data->dthsd_counts[i] - avg;
20587c478bdstevel@tonic-gate
20597c478bdstevel@tonic-gate		if (data->dthsd_counts[i] == 0)
20607c478bdstevel@tonic-gate			continue;
20617c478bdstevel@tonic-gate
20627c478bdstevel@tonic-gate		sum += delta * delta;
20637c478bdstevel@tonic-gate	}
20647c478bdstevel@tonic-gate
20657c478bdstevel@tonic-gate	util = (nz * 1000) / data->dthsd_hashsize;
20667c478bdstevel@tonic-gate	stddev = (uint_t)sqrt(sum / (double)nz) * 10;
20677c478bdstevel@tonic-gate
20687c478bdstevel@tonic-gate	mdb_printf("%15s %11d %9u.%1u %11d %11d %9u.%1u\n", name,
20697c478bdstevel@tonic-gate	    data->dthsd_hashsize, util / 10, util % 10, longest, ttl / nz,
20707c478bdstevel@tonic-gate	    stddev / 10, stddev % 10);
20717c478bdstevel@tonic-gate}
20727c478bdstevel@tonic-gate
20737c478bdstevel@tonic-gatestatic struct dtrace_hashstat {
20747c478bdstevel@tonic-gate	char *dths_name;
20757c478bdstevel@tonic-gate	dtrace_hashstat_func_t dths_func;
20767c478bdstevel@tonic-gate} _dtrace_hashstat[] = {
20777c478bdstevel@tonic-gate	{ "<actual>", NULL },
20787c478bdstevel@tonic-gate	{ "additive", dtrace_hashstat_additive },
20797c478bdstevel@tonic-gate	{ "shifty", dtrace_hashstat_shifty },
20807c478bdstevel@tonic-gate	{ "knuth", dtrace_hashstat_knuth },
20817c478bdstevel@tonic-gate	{ "one-at-a-time", dtrace_hashstat_oneatatime },
20827c478bdstevel@tonic-gate	{ "fnv", dtrace_hashstat_fnv },
20837c478bdstevel@tonic-gate	{ NULL, 0 }
20847c478bdstevel@tonic-gate};
20857c478bdstevel@tonic-gate
20867c478bdstevel@tonic-gatetypedef struct dtrace_aggstat_data {
20877c478bdstevel@tonic-gate	dtrace_hashstat_data_t dtagsd_hash;
20887c478bdstevel@tonic-gate	dtrace_hashstat_func_t dtagsd_func;
20897c478bdstevel@tonic-gate} dtrace_aggstat_data_t;
20907c478bdstevel@tonic-gate
20917c478bdstevel@tonic-gatestatic int
20927c478bdstevel@tonic-gatedtrace_aggstat_walk(uintptr_t addr, dtrace_aggkey_t *key,
20937c478bdstevel@tonic-gate    dtrace_aggstat_data_t *data)
20947c478bdstevel@tonic-gate{
20957c478bdstevel@tonic-gate	dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
20967c478bdstevel@tonic-gate	size_t size;
20977c478bdstevel@tonic-gate
20987c478bdstevel@tonic-gate	if (data->dtagsd_func == NULL) {
20997c478bdstevel@tonic-gate		size_t bucket = key->dtak_hashval % hdata->dthsd_hashsize;
21007c478bdstevel@tonic-gate
21017c478bdstevel@tonic-gate		hdata->dthsd_counts[bucket]++;
21027c478bdstevel@tonic-gate		return (WALK_NEXT);
21037c478bdstevel@tonic-gate	}
21047c478bdstevel@tonic-gate
21057c478bdstevel@tonic-gate	/*
21067c478bdstevel@tonic-gate	 * We need to read the data.
21077c478bdstevel@tonic-gate	 */
21087c478bdstevel@tonic-gate	size = key->dtak_size - sizeof (dtrace_aggid_t);
21097c478bdstevel@tonic-gate	addr = (uintptr_t)key->dtak_data + sizeof (dtrace_aggid_t);
21107c478bdstevel@tonic-gate	hdata->dthsd_data = alloca(size);
21117c478bdstevel@tonic-gate	hdata->dthsd_size = size;
21127c478bdstevel@tonic-gate
21137c478bdstevel@tonic-gate	if (mdb_vread(hdata->dthsd_data, size, addr) == -1) {
21147c478bdstevel@tonic-gate		mdb_warn("couldn't read data at %p", addr);
21157c478bdstevel@tonic-gate		return (WALK_ERR);
21167c478bdstevel@tonic-gate	}
21177c478bdstevel@tonic-gate
21187c478bdstevel@tonic-gate	data->dtagsd_func(hdata);
21197c478bdstevel@tonic-gate
21207c478bdstevel@tonic-gate	return (WALK_NEXT);
21217c478bdstevel@tonic-gate}
21227c478bdstevel@tonic-gate
21237c478bdstevel@tonic-gate/*ARGSUSED*/
21247c478bdstevel@tonic-gateint
21257c478bdstevel@tonic-gatedtrace_aggstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
21267c478bdstevel@tonic-gate{
21277c478bdstevel@tonic-gate	dtrace_buffer_t buf;
21287c478bdstevel@tonic-gate	uintptr_t aaddr;
21297c478bdstevel@tonic-gate	dtrace_aggbuffer_t agb;
21307c478bdstevel@tonic-gate	size_t hsize, i, actual, prime, evenpow;
21317c478bdstevel@tonic-gate	dtrace_aggstat_data_t data;
21327c478bdstevel@tonic-gate	dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
21337c478bdstevel@tonic-gate
21347c478bdstevel@tonic-gate	bzero(&data, sizeof (data));
21357c478bdstevel@tonic-gate
21367c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC))
21377c478bdstevel@tonic-gate		return (DCMD_USAGE);
21387c478bdstevel@tonic-gate
21397c478bdstevel@tonic-gate	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
21407c478bdstevel@tonic-gate		mdb_warn("failed to read aggregation buffer at %p", addr);
21417c478bdstevel@tonic-gate		return (DCMD_ERR);
21427c478bdstevel@tonic-gate	}
21437c478bdstevel@tonic-gate
21447c478bdstevel@tonic-gate	aaddr = (uintptr_t)buf.dtb_tomax +
21457c478bdstevel@tonic-gate	    buf.dtb_size - sizeof (dtrace_aggbuffer_t);
21467c478bdstevel@tonic-gate
21477c478bdstevel@tonic-gate	if (mdb_vread(&agb, sizeof (agb), aaddr) == -1) {
21487c478bdstevel@tonic-gate		mdb_warn("failed to read dtrace_aggbuffer_t at %p", aaddr);
21497c478bdstevel@tonic-gate		return (DCMD_ERR);
21507c478bdstevel@tonic-gate	}
21517c478bdstevel@tonic-gate
21527c478bdstevel@tonic-gate	hsize = (actual = agb.dtagb_hashsize) * sizeof (size_t);
21537c478bdstevel@tonic-gate	hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
21547c478bdstevel@tonic-gate
21557c478bdstevel@tonic-gate	/*
21567c478bdstevel@tonic-gate	 * Now pick the largest prime smaller than the hash size.  (If the
21577c478bdstevel@tonic-gate	 * existing size is prime, we'll pick a smaller prime just for the
21587c478bdstevel@tonic-gate	 * hell of it.)
21597c478bdstevel@tonic-gate	 */
21607c478bdstevel@tonic-gate	for (prime = agb.dtagb_hashsize - 1; prime > 7; prime--) {
21617c478bdstevel@tonic-gate		size_t limit = prime / 7;
21627c478bdstevel@tonic-gate
21637c478bdstevel@tonic-gate		for (i = 2; i < limit; i++) {
21647c478bdstevel@tonic-gate			if ((prime % i) == 0)
21657c478bdstevel@tonic-gate				break;
21667c478bdstevel@tonic-gate		}
21677c478bdstevel@tonic-gate
21687c478bdstevel@tonic-gate		if (i == limit)
21697c478bdstevel@tonic-gate			break;
21707c478bdstevel@tonic-gate	}
21717c478bdstevel@tonic-gate
21727c478bdstevel@tonic-gate	/*
21737c478bdstevel@tonic-gate	 * And now we want to pick the largest power of two smaller than the
21747c478bdstevel@tonic-gate	 * hashsize.
21757c478bdstevel@tonic-gate	 */
21767c478bdstevel@tonic-gate	for (i = 0; (1 << i) < agb.dtagb_hashsize; i++)
21777c478bdstevel@tonic-gate		continue;
21787c478bdstevel@tonic-gate
21797c478bdstevel@tonic-gate	evenpow = (1 << (i - 1));
21807c478bdstevel@tonic-gate
21817c478bdstevel@tonic-gate	for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) {
21827c478bdstevel@tonic-gate		data.dtagsd_func = _dtrace_hashstat[i].dths_func;
21837c478bdstevel@tonic-gate
21847c478bdstevel@tonic-gate		hdata->dthsd_hashsize = actual;
21857c478bdstevel@tonic-gate		hsize = hdata->dthsd_hashsize * sizeof (size_t);
21867c478bdstevel@tonic-gate		bzero(hdata->dthsd_counts, hsize);
21877c478bdstevel@tonic-gate
21887c478bdstevel@tonic-gate		if (mdb_pwalk("dtrace_aggkey",
21897c478bdstevel@tonic-gate		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
21907c478bdstevel@tonic-gate			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
21917c478bdstevel@tonic-gate			return (DCMD_ERR);
21927c478bdstevel@tonic-gate		}
21937c478bdstevel@tonic-gate
21947c478bdstevel@tonic-gate		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
21957c478bdstevel@tonic-gate
21967c478bdstevel@tonic-gate		/*
21977c478bdstevel@tonic-gate		 * If we were just printing the actual value, we won't try
21987c478bdstevel@tonic-gate		 * any of the sizing experiments.
21997c478bdstevel@tonic-gate		 */
22007c478bdstevel@tonic-gate		if (data.dtagsd_func == NULL)
22017c478bdstevel@tonic-gate			continue;
22027c478bdstevel@tonic-gate
22037c478bdstevel@tonic-gate		hdata->dthsd_hashsize = prime;
22047c478bdstevel@tonic-gate		hsize = hdata->dthsd_hashsize * sizeof (size_t);
22057c478bdstevel@tonic-gate		bzero(hdata->dthsd_counts, hsize);
22067c478bdstevel@tonic-gate
22077c478bdstevel@tonic-gate		if (mdb_pwalk("dtrace_aggkey",
22087c478bdstevel@tonic-gate		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
22097c478bdstevel@tonic-gate			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
22107c478bdstevel@tonic-gate			return (DCMD_ERR);
22117c478bdstevel@tonic-gate		}
22127c478bdstevel@tonic-gate
22137c478bdstevel@tonic-gate		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
22147c478bdstevel@tonic-gate
22157c478bdstevel@tonic-gate		hdata->dthsd_hashsize = evenpow;
22167c478bdstevel@tonic-gate		hsize = hdata->dthsd_hashsize * sizeof (size_t);
22177c478bdstevel@tonic-gate		bzero(hdata->dthsd_counts, hsize);
22187c478bdstevel@tonic-gate
22197c478bdstevel@tonic-gate		if (mdb_pwalk("dtrace_aggkey",
22207c478bdstevel@tonic-gate		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
22217c478bdstevel@tonic-gate			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
22227c478bdstevel@tonic-gate			return (DCMD_ERR);
22237c478bdstevel@tonic-gate		}
22247c478bdstevel@tonic-gate
22257c478bdstevel@tonic-gate		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
22267c478bdstevel@tonic-gate	}
22277c478bdstevel@tonic-gate
22287c478bdstevel@tonic-gate	return (DCMD_OK);
22297c478bdstevel@tonic-gate}
22307c478bdstevel@tonic-gate
22317c478bdstevel@tonic-gate/*ARGSUSED*/
22327c478bdstevel@tonic-gatestatic int
22337c478bdstevel@tonic-gatedtrace_dynstat_walk(uintptr_t addr, dtrace_dynvar_t *dynvar,
22347c478bdstevel@tonic-gate    dtrace_aggstat_data_t *data)
22357c478bdstevel@tonic-gate{
22367c478bdstevel@tonic-gate	dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
22377c478bdstevel@tonic-gate	dtrace_tuple_t *tuple = &dynvar->dtdv_tuple;
22387c478bdstevel@tonic-gate	dtrace_key_t *key = tuple->dtt_key;
22397c478bdstevel@tonic-gate	size_t size = 0, offs = 0;
22407c478bdstevel@tonic-gate	int i, nkeys = tuple->dtt_nkeys;
22417c478bdstevel@tonic-gate	char *buf;
22427c478bdstevel@tonic-gate
22437c478bdstevel@tonic-gate	if (data->dtagsd_func == NULL) {
22447c478bdstevel@tonic-gate		size_t bucket = dynvar->dtdv_hashval % hdata->dthsd_hashsize;
22457c478bdstevel@tonic-gate
22467c478bdstevel@tonic-gate		hdata->dthsd_counts[bucket]++;
22477c478bdstevel@tonic-gate		return (WALK_NEXT);
22487c478bdstevel@tonic-gate	}
22497c478bdstevel@tonic-gate
22507c478bdstevel@tonic-gate	/*
22517c478bdstevel@tonic-gate	 * We want to hand the hashing algorithm a contiguous buffer.  First
22527c478bdstevel@tonic-gate	 * run through the tuple and determine the size.
22537c478bdstevel@tonic-gate	 */
22547c478bdstevel@tonic-gate	for (i = 0; i < nkeys; i++) {
22557c478bdstevel@tonic-gate		if (key[i].dttk_size == 0) {
22567c478bdstevel@tonic-gate			size += sizeof (uint64_t);
22577c478bdstevel@tonic-gate		} else {
22587c478bdstevel@tonic-gate			size += key[i].dttk_size;
22597c478bdstevel@tonic-gate		}
22607c478bdstevel@tonic-gate	}
22617c478bdstevel@tonic-gate
22627c478bdstevel@tonic-gate	buf = alloca(size);
22637c478bdstevel@tonic-gate
22647c478bdstevel@tonic-gate	/*
22657c478bdstevel@tonic-gate	 * Now go back through the tuple and copy the data into the buffer.
22667c478bdstevel@tonic-gate	 */
22677c478bdstevel@tonic-gate	for (i = 0; i < nkeys; i++) {
22687c478bdstevel@tonic-gate		if (key[i].dttk_size == 0) {
22697c478bdstevel@tonic-gate			bcopy(&key[i].dttk_value, &buf[offs],
22707c478bdstevel@tonic-gate			    sizeof (uint64_t));
22717c478bdstevel@tonic-gate			offs += sizeof (uint64_t);
22727c478bdstevel@tonic-gate		} else {
22737c478bdstevel@tonic-gate			if (mdb_vread(&buf[offs], key[i].dttk_size,
22747c478bdstevel@tonic-gate			    key[i].dttk_value) == -1) {
22757c478bdstevel@tonic-gate				mdb_warn("couldn't read tuple data at %p",
22767c478bdstevel@tonic-gate				    key[i].dttk_value);
22777c478bdstevel@tonic-gate				return (WALK_ERR);
22787c478bdstevel@tonic-gate			}
22797c478bdstevel@tonic-gate
22807c478bdstevel@tonic-gate			offs += key[i].dttk_size;
22817c478bdstevel@tonic-gate		}
22827c478bdstevel@tonic-gate	}
22837c478bdstevel@tonic-gate
22847c478bdstevel@tonic-gate	hdata->dthsd_data = buf;
22857c478bdstevel@tonic-gate	hdata->dthsd_size = size;
22867c478bdstevel@tonic-gate
22877c478bdstevel@tonic-gate	data->dtagsd_func(hdata);
22887c478bdstevel@tonic-gate
22897c478bdstevel@tonic-gate	return (WALK_NEXT);
22907c478bdstevel@tonic-gate}
22917c478bdstevel@tonic-gate
22927c478bdstevel@tonic-gate/*ARGSUSED*/
22937c478bdstevel@tonic-gateint
22947c478bdstevel@tonic-gatedtrace_dynstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
22957c478bdstevel@tonic-gate{
22967c478bdstevel@tonic-gate	dtrace_dstate_t dstate;
22977c478bdstevel@tonic-gate	size_t hsize, i, actual, prime;
22987c478bdstevel@tonic-gate	dtrace_aggstat_data_t data;
22997c478bdstevel@tonic-gate	dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
23007c478bdstevel@tonic-gate
23017c478bdstevel@tonic-gate	bzero(&data, sizeof (data));
23027c478bdstevel@tonic-gate
23037c478bdstevel@tonic-gate	if (!(flags & DCMD_ADDRSPEC))
23047c478bdstevel@tonic-gate		return (DCMD_USAGE);
23057c478bdstevel@tonic-gate
23067c478bdstevel@tonic-gate	if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
23077c478bdstevel@tonic-gate		mdb_warn("failed to read dynamic variable state at %p", addr);
23087c478bdstevel@tonic-gate		return (DCMD_ERR);
23097c478bdstevel@tonic-gate	}
23107c478bdstevel@tonic-gate
23117c478bdstevel@tonic-gate	hsize = (actual = dstate.dtds_hashsize) * sizeof (size_t);
23127c478bdstevel@tonic-gate	hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
23137c478bdstevel@tonic-gate
23147c478bdstevel@tonic-gate	/*
23157c478bdstevel@tonic-gate	 * Now pick the largest prime smaller than the hash size.  (If the
23167c478bdstevel@tonic-gate	 * existing size is prime, we'll pick a smaller prime just for the
23177c478bdstevel@tonic-gate	 * hell of it.)
2318