17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ac448965Sahl  * Common Development and Distribution License (the "License").
6ac448965Sahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ac448965Sahl 
227c478bd9Sstevel@tonic-gate /*
23c9a6ea2eSBryan Cantrill  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247994dfddSDan Kimmel  * Copyright (c) 2013 by Delphix. All rights reserved.
25c3377ee9SJohn Levon  * Copyright 2019 Joyent, Inc.
26*9f7719e7SJason King  * Copyright 2022 Racktop Systems, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * explicitly define DTRACE_ERRDEBUG to pull in definition of dtrace_errhash_t
317c478bd9Sstevel@tonic-gate  * explicitly define _STDARG_H to avoid stdarg.h/varargs.h u/k defn conflict
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate #define	DTRACE_ERRDEBUG
347c478bd9Sstevel@tonic-gate #define	_STDARG_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <mdb/mdb_param.h>
377c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_ks.h>
397c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/vmem_impl.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
447c478bd9Sstevel@tonic-gate #include <dtrace.h>
457c478bd9Sstevel@tonic-gate #include <alloca.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <math.h>
497994dfddSDan Kimmel #include <stdio.h>
507994dfddSDan Kimmel #include <unistd.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
537c478bd9Sstevel@tonic-gate int
id2probe(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)547c478bd9Sstevel@tonic-gate id2probe(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
557c478bd9Sstevel@tonic-gate {
56892ad162SToomas Soome 	uintptr_t probe = 0;
577c478bd9Sstevel@tonic-gate 	uintptr_t probes;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
607c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (addr == DTRACE_IDNONE || addr > UINT32_MAX)
637c478bd9Sstevel@tonic-gate 		goto out;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&probes, "dtrace_probes") == -1) {
667c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_probes'");
677c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	probes += (addr - 1) * sizeof (dtrace_probe_t *);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (uintptr_t), probes) == -1) {
737c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_probes[%d]", addr - 1);
747c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate out:
787c478bd9Sstevel@tonic-gate 	mdb_printf("%p\n", probe);
797c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
dtrace_help(void)837c478bd9Sstevel@tonic-gate dtrace_help(void)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	mdb_printf("Given a dtrace_state_t structure that represents a "
877c478bd9Sstevel@tonic-gate 	    "DTrace consumer, prints\n"
88bbf21555SRichard Lowe 	    "dtrace(8)-like output for in-kernel DTrace data.  (The "
897c478bd9Sstevel@tonic-gate 	    "dtrace_state_t\n"
907c478bd9Sstevel@tonic-gate 	    "structures for all DTrace consumers may be obtained by running "
917c478bd9Sstevel@tonic-gate 	    "the \n"
927c478bd9Sstevel@tonic-gate 	    "::dtrace_state dcmd.)   When data is present on multiple CPUs, "
937c478bd9Sstevel@tonic-gate 	    "data are\n"
947c478bd9Sstevel@tonic-gate 	    "presented in CPU order, with records within each CPU ordered "
957c478bd9Sstevel@tonic-gate 	    "oldest to \n"
967c478bd9Sstevel@tonic-gate 	    "youngest.  Options:\n\n"
977c478bd9Sstevel@tonic-gate 	    "-c cpu     Only provide output for specified CPU.\n");
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int
dtracemdb_eprobe(dtrace_state_t * state,dtrace_eprobedesc_t * epd)1017c478bd9Sstevel@tonic-gate dtracemdb_eprobe(dtrace_state_t *state, dtrace_eprobedesc_t *epd)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	dtrace_epid_t epid = epd->dtepd_epid;
1047c478bd9Sstevel@tonic-gate 	dtrace_probe_t probe;
1057c478bd9Sstevel@tonic-gate 	dtrace_ecb_t ecb;
1067c478bd9Sstevel@tonic-gate 	uintptr_t addr, paddr, ap;
1077c478bd9Sstevel@tonic-gate 	dtrace_action_t act;
1087c478bd9Sstevel@tonic-gate 	int nactions, nrecs;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)state->dts_ecbs +
1117c478bd9Sstevel@tonic-gate 	    (epid - 1) * sizeof (dtrace_ecb_t *);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
1147c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read ecb for epid %d", epid);
1157c478bd9Sstevel@tonic-gate 		return (-1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
118892ad162SToomas Soome 	if (addr == 0) {
1197c478bd9Sstevel@tonic-gate 		mdb_warn("epid %d doesn't match an ecb\n", epid);
1207c478bd9Sstevel@tonic-gate 		return (-1);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ecb, sizeof (ecb), addr) == -1) {
1247c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read ecb at %p", addr);
1257c478bd9Sstevel@tonic-gate 		return (-1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	paddr = (uintptr_t)ecb.dte_probe;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
1317c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read probe for ecb %p", addr);
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * This is a little painful:  in order to find the number of actions,
1377c478bd9Sstevel@tonic-gate 	 * we need to first walk through them.
1387c478bd9Sstevel@tonic-gate 	 */
139892ad162SToomas Soome 	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != 0; ) {
1407c478bd9Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1417c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read action %p on ecb %p",
1427c478bd9Sstevel@tonic-gate 			    ap, addr);
1437c478bd9Sstevel@tonic-gate 			return (-1);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple)
1477c478bd9Sstevel@tonic-gate 			nactions++;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	nrecs = epd->dtepd_nrecs;
1537c478bd9Sstevel@tonic-gate 	epd->dtepd_nrecs = nactions;
1547c478bd9Sstevel@tonic-gate 	epd->dtepd_probeid = probe.dtpr_id;
1557c478bd9Sstevel@tonic-gate 	epd->dtepd_uarg = ecb.dte_uarg;
1567c478bd9Sstevel@tonic-gate 	epd->dtepd_size = ecb.dte_size;
1577c478bd9Sstevel@tonic-gate 
158892ad162SToomas Soome 	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != 0; ) {
1597c478bd9Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1607c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read action %p on ecb %p",
1617c478bd9Sstevel@tonic-gate 			    ap, addr);
1627c478bd9Sstevel@tonic-gate 			return (-1);
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) {
1667c478bd9Sstevel@tonic-gate 			if (nrecs-- == 0)
1677c478bd9Sstevel@tonic-gate 				break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 			epd->dtepd_rec[nactions++] = act.dta_rec;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (0);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1797c478bd9Sstevel@tonic-gate static int
dtracemdb_probe(dtrace_state_t * state,dtrace_probedesc_t * pd)1807c478bd9Sstevel@tonic-gate dtracemdb_probe(dtrace_state_t *state, dtrace_probedesc_t *pd)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	uintptr_t base, addr, paddr, praddr;
1837c478bd9Sstevel@tonic-gate 	int nprobes, i;
1847c478bd9Sstevel@tonic-gate 	dtrace_probe_t probe;
1857c478bd9Sstevel@tonic-gate 	dtrace_provider_t prov;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (pd->dtpd_id == DTRACE_IDNONE)
1887c478bd9Sstevel@tonic-gate 		pd->dtpd_id++;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&base, "dtrace_probes") == -1) {
1917c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_probes'");
1927c478bd9Sstevel@tonic-gate 		return (-1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) {
1967c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_nprobes'");
1977c478bd9Sstevel@tonic-gate 		return (-1);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for (i = pd->dtpd_id; i <= nprobes; i++) {
2017c478bd9Sstevel@tonic-gate 		addr = base + (i - 1) * sizeof (dtrace_probe_t *);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		if (mdb_vread(&paddr, sizeof (paddr), addr) == -1) {
2047c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read probe pointer at %p", addr);
2057c478bd9Sstevel@tonic-gate 			return (-1);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
208892ad162SToomas Soome 		if (paddr != 0)
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
212892ad162SToomas Soome 	if (paddr == 0) {
2137c478bd9Sstevel@tonic-gate 		errno = ESRCH;
2147c478bd9Sstevel@tonic-gate 		return (-1);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
2187c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read probe at %p", paddr);
2197c478bd9Sstevel@tonic-gate 		return (-1);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	pd->dtpd_id = probe.dtpr_id;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_name, DTRACE_NAMELEN,
2257c478bd9Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_name) == -1) {
2267c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read probe name for probe %p", paddr);
2277c478bd9Sstevel@tonic-gate 		return (-1);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_func, DTRACE_FUNCNAMELEN,
2317c478bd9Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_func) == -1) {
2327c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read function name for probe %p", paddr);
2337c478bd9Sstevel@tonic-gate 		return (-1);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_mod, DTRACE_MODNAMELEN,
2377c478bd9Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_mod) == -1) {
2387c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read module name for probe %p", paddr);
2397c478bd9Sstevel@tonic-gate 		return (-1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	praddr = (uintptr_t)probe.dtpr_provider;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (mdb_vread(&prov, sizeof (prov), praddr) == -1) {
2457c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read provider for probe %p", paddr);
2467c478bd9Sstevel@tonic-gate 		return (-1);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_provider, DTRACE_PROVNAMELEN,
2507c478bd9Sstevel@tonic-gate 	    (uintptr_t)prov.dtpv_name) == -1) {
2517c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read provider name for probe %p", paddr);
2527c478bd9Sstevel@tonic-gate 		return (-1);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2597c478bd9Sstevel@tonic-gate static int
dtracemdb_aggdesc(dtrace_state_t * state,dtrace_aggdesc_t * agd)2607c478bd9Sstevel@tonic-gate dtracemdb_aggdesc(dtrace_state_t *state, dtrace_aggdesc_t *agd)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	dtrace_aggid_t aggid = agd->dtagd_id;
2637c478bd9Sstevel@tonic-gate 	dtrace_aggregation_t agg;
2647c478bd9Sstevel@tonic-gate 	dtrace_ecb_t ecb;
2657c478bd9Sstevel@tonic-gate 	uintptr_t addr, eaddr, ap, last;
2667c478bd9Sstevel@tonic-gate 	dtrace_action_t act;
2677c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t *lrec;
2687c478bd9Sstevel@tonic-gate 	int nactions, nrecs;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)state->dts_aggregations +
2717c478bd9Sstevel@tonic-gate 	    (aggid - 1) * sizeof (dtrace_aggregation_t *);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
2747c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read aggregation for aggid %d", aggid);
2757c478bd9Sstevel@tonic-gate 		return (-1);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
278892ad162SToomas Soome 	if (addr == 0) {
2797c478bd9Sstevel@tonic-gate 		mdb_warn("aggid %d doesn't match an aggregation\n", aggid);
2807c478bd9Sstevel@tonic-gate 		return (-1);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (mdb_vread(&agg, sizeof (agg), addr) == -1) {
2847c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read aggregation at %p", addr);
2857c478bd9Sstevel@tonic-gate 		return (-1);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	eaddr = (uintptr_t)agg.dtag_ecb;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (mdb_vread(&ecb, sizeof (ecb), eaddr) == -1) {
2917c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read ecb for aggregation %p", addr);
2927c478bd9Sstevel@tonic-gate 		return (-1);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	last = (uintptr_t)addr + offsetof(dtrace_aggregation_t, dtag_action);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
2987c478bd9Sstevel@tonic-gate 	 * This is a little painful:  in order to find the number of actions,
2997c478bd9Sstevel@tonic-gate 	 * we need to first walk through them.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	ap = (uintptr_t)agg.dtag_first;
3027c478bd9Sstevel@tonic-gate 	nactions = 0;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	for (;;) {
3057c478bd9Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
3067c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read action %p on aggregation %p",
3077c478bd9Sstevel@tonic-gate 			    ap, addr);
3087c478bd9Sstevel@tonic-gate 			return (-1);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		nactions++;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		if (ap == last)
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	lrec = &act.dta_rec;
3207c478bd9Sstevel@tonic-gate 	agd->dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - agg.dtag_base;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	nrecs = agd->dtagd_nrecs;
3237c478bd9Sstevel@tonic-gate 	agd->dtagd_nrecs = nactions;
3247c478bd9Sstevel@tonic-gate 	agd->dtagd_epid = ecb.dte_epid;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	ap = (uintptr_t)agg.dtag_first;
3277c478bd9Sstevel@tonic-gate 	nactions = 0;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	for (;;) {
3307c478bd9Sstevel@tonic-gate 		dtrace_recdesc_t rec;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
3337c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read action %p on aggregation %p",
3347c478bd9Sstevel@tonic-gate 			    ap, addr);
3357c478bd9Sstevel@tonic-gate 			return (-1);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (nrecs-- == 0)
3397c478bd9Sstevel@tonic-gate 			break;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		rec = act.dta_rec;
3427c478bd9Sstevel@tonic-gate 		rec.dtrd_offset -= agg.dtag_base;
3437c478bd9Sstevel@tonic-gate 		rec.dtrd_uarg = 0;
3447c478bd9Sstevel@tonic-gate 		agd->dtagd_rec[nactions++] = rec;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		if (ap == last)
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	return (0);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate static int
dtracemdb_bufsnap(dtrace_buffer_t * which,dtrace_bufdesc_t * desc)3567c478bd9Sstevel@tonic-gate dtracemdb_bufsnap(dtrace_buffer_t *which, dtrace_bufdesc_t *desc)
3577c478bd9Sstevel@tonic-gate {
358*9f7719e7SJason King 	static hrtime_t hr_offset = 0;
359*9f7719e7SJason King 	static boolean_t offset_set = B_FALSE;
3607c478bd9Sstevel@tonic-gate 	uintptr_t addr;
3617c478bd9Sstevel@tonic-gate 	size_t bufsize;
3627c478bd9Sstevel@tonic-gate 	dtrace_buffer_t buf;
3637c478bd9Sstevel@tonic-gate 	caddr_t data = desc->dtbd_data;
3647c478bd9Sstevel@tonic-gate 	processorid_t max_cpuid, cpu = desc->dtbd_cpu;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
3677c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'max_cpuid'");
3687c478bd9Sstevel@tonic-gate 		errno = EIO;
3697c478bd9Sstevel@tonic-gate 		return (-1);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (cpu < 0 || cpu > max_cpuid) {
3737c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3747c478bd9Sstevel@tonic-gate 		return (-1);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)which + cpu * sizeof (dtrace_buffer_t);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
3807c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read buffer description at %p", addr);
3817c478bd9Sstevel@tonic-gate 		errno = EIO;
3827c478bd9Sstevel@tonic-gate 		return (-1);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (buf.dtb_tomax == NULL) {
3867c478bd9Sstevel@tonic-gate 		errno = ENOENT;
3877c478bd9Sstevel@tonic-gate 		return (-1);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
3917c478bd9Sstevel@tonic-gate 		bufsize = buf.dtb_size;
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 		bufsize = buf.dtb_offset;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (mdb_vread(data, bufsize, (uintptr_t)buf.dtb_tomax) == -1) {
3977c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read buffer for CPU %d", cpu);
3987c478bd9Sstevel@tonic-gate 		errno = EIO;
3997c478bd9Sstevel@tonic-gate 		return (-1);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (buf.dtb_offset > buf.dtb_size) {
4037c478bd9Sstevel@tonic-gate 		mdb_warn("buffer for CPU %d has corrupt offset\n", cpu);
4047c478bd9Sstevel@tonic-gate 		errno = EIO;
4057c478bd9Sstevel@tonic-gate 		return (-1);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
4097c478bd9Sstevel@tonic-gate 		if (buf.dtb_xamot_offset > buf.dtb_size) {
4107c478bd9Sstevel@tonic-gate 			mdb_warn("ringbuffer for CPU %d has corrupt "
4117c478bd9Sstevel@tonic-gate 			    "wrapped offset\n", cpu);
4127c478bd9Sstevel@tonic-gate 			errno = EIO;
4137c478bd9Sstevel@tonic-gate 			return (-1);
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * If the ring buffer has wrapped, it needs to be polished.
4187c478bd9Sstevel@tonic-gate 		 * See the comment in dtrace_buffer_polish() for details.
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		if (buf.dtb_offset < buf.dtb_xamot_offset) {
4217c478bd9Sstevel@tonic-gate 			bzero(data + buf.dtb_offset,
4227c478bd9Sstevel@tonic-gate 			    buf.dtb_xamot_offset - buf.dtb_offset);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		if (buf.dtb_offset > buf.dtb_xamot_offset) {
4267c478bd9Sstevel@tonic-gate 			bzero(data + buf.dtb_offset,
4277c478bd9Sstevel@tonic-gate 			    buf.dtb_size - buf.dtb_offset);
4287c478bd9Sstevel@tonic-gate 			bzero(data, buf.dtb_xamot_offset);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		desc->dtbd_oldest = buf.dtb_xamot_offset;
4327c478bd9Sstevel@tonic-gate 	} else {
4337c478bd9Sstevel@tonic-gate 		desc->dtbd_oldest = 0;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
436*9f7719e7SJason King 	/*
437*9f7719e7SJason King 	 * On a live system, dtbd_timestamp is set to gethrtime() when the
438*9f7719e7SJason King 	 * DTRACEIOC_BUFSNAP ioctl is called. The effect of this is that the
439*9f7719e7SJason King 	 * timestamps of all the enabled probe records in the buf will always
440*9f7719e7SJason King 	 * be less than dtbd_timestamp. dtrace_consume() relies on this
441*9f7719e7SJason King 	 * invariant to determine when it needs to retrieve more dtrace bufs
442*9f7719e7SJason King 	 * from the kernel.
443*9f7719e7SJason King 	 *
444*9f7719e7SJason King 	 * However when mdb is reading a crash dump, the value of
445*9f7719e7SJason King 	 * gethrtime() on the system running mdb may smaller than the
446*9f7719e7SJason King 	 * enabled probe records in the crash dump, violating the invariant
447*9f7719e7SJason King 	 * dtrace_consume() is relying on. This can cause dtrace_consume()
448*9f7719e7SJason King 	 * to prematurely stop processing records.
449*9f7719e7SJason King 	 *
450*9f7719e7SJason King 	 * To preserve the invariant dtrace_consume() requires, we simply
451*9f7719e7SJason King 	 * add the value of panic_hrtime to gethrtime() when setting
452*9f7719e7SJason King 	 * dtdb_timestamp. On a live system, panic_hrtime will be 0, and
453*9f7719e7SJason King 	 * the invariant will be preserved by virtue of being running on
454*9f7719e7SJason King 	 * a live system. On a crash dump, no valid probe record can have a
455*9f7719e7SJason King 	 * timestamp greater than panic_hrtime, so adding this to the value
456*9f7719e7SJason King 	 * of gethrtime() will guarantee the invariant expected by
457*9f7719e7SJason King 	 * dtrace_consume() is preserved.
458*9f7719e7SJason King 	 */
459*9f7719e7SJason King 	if (!offset_set) {
460*9f7719e7SJason King 		hrtime_t panic_hrtime;
461*9f7719e7SJason King 
462*9f7719e7SJason King 		/*
463*9f7719e7SJason King 		 * We could be slightly more clever and only set hr_offset
464*9f7719e7SJason King 		 * if gethrtime() in mdb is < panic_hrtime, but it doesn't
465*9f7719e7SJason King 		 * seem necessary. If for some reason, we cannot read
466*9f7719e7SJason King 		 * panic_hrtime, we'll try to continue -- ::dtrace may
467*9f7719e7SJason King 		 * still succeed, so we just warn and continue.
468*9f7719e7SJason King 		 */
469*9f7719e7SJason King 		if (mdb_readvar(&panic_hrtime, "panic_hrtime") == -1) {
470*9f7719e7SJason King 			mdb_warn("failed to read 'panic_hrtime' -- "
471*9f7719e7SJason King 			    "some dtrace data may not be displayed");
472*9f7719e7SJason King 		} else {
473*9f7719e7SJason King 			hr_offset = panic_hrtime;
474*9f7719e7SJason King 		}
475*9f7719e7SJason King 		offset_set = B_TRUE;
476*9f7719e7SJason King 	}
477*9f7719e7SJason King 
4787c478bd9Sstevel@tonic-gate 	desc->dtbd_size = bufsize;
4797c478bd9Sstevel@tonic-gate 	desc->dtbd_drops = buf.dtb_drops;
4807c478bd9Sstevel@tonic-gate 	desc->dtbd_errors = buf.dtb_errors;
481*9f7719e7SJason King 	desc->dtbd_timestamp = gethrtime() + hr_offset;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (0);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
487c9a6ea2eSBryan Cantrill  * This is essentially identical to its cousin in the kernel -- with the
488c9a6ea2eSBryan Cantrill  * notable exception that we automatically set DTRACEOPT_GRABANON if this
489c9a6ea2eSBryan Cantrill  * state is an anonymous enabling.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate static dof_hdr_t *
dtracemdb_dof_create(dtrace_state_t * state,int isanon)492c9a6ea2eSBryan Cantrill dtracemdb_dof_create(dtrace_state_t *state, int isanon)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	dof_hdr_t *dof;
4957c478bd9Sstevel@tonic-gate 	dof_sec_t *sec;
4967c478bd9Sstevel@tonic-gate 	dof_optdesc_t *opt;
4977c478bd9Sstevel@tonic-gate 	int i, len = sizeof (dof_hdr_t) +
4987c478bd9Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
4997c478bd9Sstevel@tonic-gate 	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	dof = mdb_zalloc(len, UM_SLEEP);
5027c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
5037c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
5047c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
5057c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
5087c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
509ac448965Sahl 	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
5107c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
5117c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
5127c478bd9Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	dof->dofh_flags = 0;
5157c478bd9Sstevel@tonic-gate 	dof->dofh_hdrsize = sizeof (dof_hdr_t);
5167c478bd9Sstevel@tonic-gate 	dof->dofh_secsize = sizeof (dof_sec_t);
5177c478bd9Sstevel@tonic-gate 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
5187c478bd9Sstevel@tonic-gate 	dof->dofh_secoff = sizeof (dof_hdr_t);
5197c478bd9Sstevel@tonic-gate 	dof->dofh_loadsz = len;
5207c478bd9Sstevel@tonic-gate 	dof->dofh_filesz = len;
5217c478bd9Sstevel@tonic-gate 	dof->dofh_pad = 0;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Fill in the option section header...
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
5277c478bd9Sstevel@tonic-gate 	sec->dofs_type = DOF_SECT_OPTDESC;
5287c478bd9Sstevel@tonic-gate 	sec->dofs_align = sizeof (uint64_t);
5297c478bd9Sstevel@tonic-gate 	sec->dofs_flags = DOF_SECF_LOAD;
5307c478bd9Sstevel@tonic-gate 	sec->dofs_entsize = sizeof (dof_optdesc_t);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	opt = (dof_optdesc_t *)((uintptr_t)sec +
5337c478bd9Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
5367c478bd9Sstevel@tonic-gate 	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++) {
5397c478bd9Sstevel@tonic-gate 		opt[i].dofo_option = i;
5407c478bd9Sstevel@tonic-gate 		opt[i].dofo_strtab = DOF_SECIDX_NONE;
5417c478bd9Sstevel@tonic-gate 		opt[i].dofo_value = state->dts_options[i];
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
544c9a6ea2eSBryan Cantrill 	if (isanon)
545c9a6ea2eSBryan Cantrill 		opt[DTRACEOPT_GRABANON].dofo_value = 1;
546c9a6ea2eSBryan Cantrill 
5477c478bd9Sstevel@tonic-gate 	return (dof);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static int
dtracemdb_format(dtrace_state_t * state,dtrace_fmtdesc_t * desc)5517c478bd9Sstevel@tonic-gate dtracemdb_format(dtrace_state_t *state, dtrace_fmtdesc_t *desc)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	uintptr_t addr, faddr;
5547c478bd9Sstevel@tonic-gate 	char c;
5557c478bd9Sstevel@tonic-gate 	int len = 0;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (desc->dtfd_format == 0 || desc->dtfd_format > state->dts_nformats) {
5587c478bd9Sstevel@tonic-gate 		errno = EINVAL;
5597c478bd9Sstevel@tonic-gate 		return (-1);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	faddr = (uintptr_t)state->dts_formats +
5637c478bd9Sstevel@tonic-gate 	    (desc->dtfd_format - 1) * sizeof (char *);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), faddr) == -1) {
5667c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read format string pointer at %p", faddr);
5677c478bd9Sstevel@tonic-gate 		return (-1);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	do {
5717c478bd9Sstevel@tonic-gate 		if (mdb_vread(&c, sizeof (c), addr + len++) == -1) {
5727c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read format string at %p", addr);
5737c478bd9Sstevel@tonic-gate 			return (-1);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 	} while (c != '\0');
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (len > desc->dtfd_length) {
5787c478bd9Sstevel@tonic-gate 		desc->dtfd_length = len;
5797c478bd9Sstevel@tonic-gate 		return (0);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (mdb_vread(desc->dtfd_string, len, addr) == -1) {
5837c478bd9Sstevel@tonic-gate 		mdb_warn("failed to reread format string at %p", addr);
5847c478bd9Sstevel@tonic-gate 		return (-1);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	return (0);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate static int
dtracemdb_status(dtrace_state_t * state,dtrace_status_t * status)5917c478bd9Sstevel@tonic-gate dtracemdb_status(dtrace_state_t *state, dtrace_status_t *status)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	dtrace_dstate_t *dstate;
5947c478bd9Sstevel@tonic-gate 	int i, j;
5957c478bd9Sstevel@tonic-gate 	uint64_t nerrs;
5967c478bd9Sstevel@tonic-gate 	uintptr_t addr;
5977c478bd9Sstevel@tonic-gate 	int ncpu;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
6007c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read '_ncpu'");
6017c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	bzero(status, sizeof (dtrace_status_t));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
6077c478bd9Sstevel@tonic-gate 		errno = ENOENT;
6087c478bd9Sstevel@tonic-gate 		return (-1);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * For the MDB backend, we never set dtst_exiting or dtst_filled.  This
6137c478bd9Sstevel@tonic-gate 	 * is by design:  we don't want the library to try to stop tracing,
6147c478bd9Sstevel@tonic-gate 	 * because it doesn't particularly mean anything.
6157c478bd9Sstevel@tonic-gate 	 */
6167c478bd9Sstevel@tonic-gate 	nerrs = state->dts_errors;
6177c478bd9Sstevel@tonic-gate 	dstate = &state->dts_vstate.dtvs_dynvars;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncpu; i++) {
6207c478bd9Sstevel@tonic-gate 		dtrace_dstate_percpu_t dcpu;
6217c478bd9Sstevel@tonic-gate 		dtrace_buffer_t buf;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		addr = (uintptr_t)&dstate->dtds_percpu[i];
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		if (mdb_vread(&dcpu, sizeof (dcpu), addr) == -1) {
6267c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read per-CPU dstate at %p", addr);
6277c478bd9Sstevel@tonic-gate 			return (-1);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		status->dtst_dyndrops += dcpu.dtdsc_drops;
6317c478bd9Sstevel@tonic-gate 		status->dtst_dyndrops_dirty += dcpu.dtdsc_dirty_drops;
6327c478bd9Sstevel@tonic-gate 		status->dtst_dyndrops_rinsing += dcpu.dtdsc_rinsing_drops;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		addr = (uintptr_t)&state->dts_buffer[i];
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
6377c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read per-CPU buffer at %p", addr);
6387c478bd9Sstevel@tonic-gate 			return (-1);
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		nerrs += buf.dtb_errors;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		for (j = 0; j < state->dts_nspeculations; j++) {
6447c478bd9Sstevel@tonic-gate 			dtrace_speculation_t spec;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 			addr = (uintptr_t)&state->dts_speculations[j];
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 			if (mdb_vread(&spec, sizeof (spec), addr) == -1) {
6497c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read "
6507c478bd9Sstevel@tonic-gate 				    "speculation at %p", addr);
6517c478bd9Sstevel@tonic-gate 				return (-1);
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 			addr = (uintptr_t)&spec.dtsp_buffer[i];
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 			if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
6577c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read "
6587c478bd9Sstevel@tonic-gate 				    "speculative buffer at %p", addr);
6597c478bd9Sstevel@tonic-gate 				return (-1);
6607c478bd9Sstevel@tonic-gate 			}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 			status->dtst_specdrops += buf.dtb_xamot_drops;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	status->dtst_specdrops_busy = state->dts_speculations_busy;
6677c478bd9Sstevel@tonic-gate 	status->dtst_specdrops_unavail = state->dts_speculations_unavail;
6687c478bd9Sstevel@tonic-gate 	status->dtst_errors = nerrs;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	return (0);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate typedef struct dtracemdb_data {
6747c478bd9Sstevel@tonic-gate 	dtrace_state_t *dtmd_state;
6757c478bd9Sstevel@tonic-gate 	char *dtmd_symstr;
6767c478bd9Sstevel@tonic-gate 	char *dtmd_modstr;
6777c478bd9Sstevel@tonic-gate 	uintptr_t dtmd_addr;
678c9a6ea2eSBryan Cantrill 	int dtmd_isanon;
6797c478bd9Sstevel@tonic-gate } dtracemdb_data_t;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static int
dtracemdb_ioctl(void * varg,int cmd,void * arg)6827c478bd9Sstevel@tonic-gate dtracemdb_ioctl(void *varg, int cmd, void *arg)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	dtracemdb_data_t *data = varg;
6857c478bd9Sstevel@tonic-gate 	dtrace_state_t *state = data->dtmd_state;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	switch (cmd) {
6887c478bd9Sstevel@tonic-gate 	case DTRACEIOC_CONF: {
6897c478bd9Sstevel@tonic-gate 		dtrace_conf_t *conf = arg;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		bzero(conf, sizeof (conf));
6927c478bd9Sstevel@tonic-gate 		conf->dtc_difversion = DIF_VERSION;
6937c478bd9Sstevel@tonic-gate 		conf->dtc_difintregs = DIF_DIR_NREGS;
6947c478bd9Sstevel@tonic-gate 		conf->dtc_diftupregs = DIF_DTR_NREGS;
6957c478bd9Sstevel@tonic-gate 		conf->dtc_ctfmodel = CTF_MODEL_NATIVE;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		return (0);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	case DTRACEIOC_DOFGET: {
7017c478bd9Sstevel@tonic-gate 		dof_hdr_t *hdr = arg, *dof;
7027c478bd9Sstevel@tonic-gate 
703c9a6ea2eSBryan Cantrill 		dof = dtracemdb_dof_create(state, data->dtmd_isanon);
7047c478bd9Sstevel@tonic-gate 		bcopy(dof, hdr, MIN(hdr->dofh_loadsz, dof->dofh_loadsz));
7057c478bd9Sstevel@tonic-gate 		mdb_free(dof, dof->dofh_loadsz);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		return (0);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	case DTRACEIOC_BUFSNAP:
7117c478bd9Sstevel@tonic-gate 		return (dtracemdb_bufsnap(state->dts_buffer, arg));
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	case DTRACEIOC_AGGSNAP:
7147c478bd9Sstevel@tonic-gate 		return (dtracemdb_bufsnap(state->dts_aggbuffer, arg));
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	case DTRACEIOC_AGGDESC:
7177c478bd9Sstevel@tonic-gate 		return (dtracemdb_aggdesc(state, arg));
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	case DTRACEIOC_EPROBE:
7207c478bd9Sstevel@tonic-gate 		return (dtracemdb_eprobe(state, arg));
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	case DTRACEIOC_PROBES:
7237c478bd9Sstevel@tonic-gate 		return (dtracemdb_probe(state, arg));
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	case DTRACEIOC_FORMAT:
7267c478bd9Sstevel@tonic-gate 		return (dtracemdb_format(state, arg));
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	case DTRACEIOC_STATUS:
7297c478bd9Sstevel@tonic-gate 		return (dtracemdb_status(state, arg));
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	case DTRACEIOC_GO:
7327c478bd9Sstevel@tonic-gate 		*(processorid_t *)arg = -1;
7337c478bd9Sstevel@tonic-gate 		return (0);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	case DTRACEIOC_ENABLE:
7367c478bd9Sstevel@tonic-gate 		errno = ENOTTY; /* see dt_open.c:dtrace_go() */
7377c478bd9Sstevel@tonic-gate 		return (-1);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	case DTRACEIOC_PROVIDER:
7407c478bd9Sstevel@tonic-gate 	case DTRACEIOC_PROBEMATCH:
7417c478bd9Sstevel@tonic-gate 		errno = ESRCH;
7427c478bd9Sstevel@tonic-gate 		return (-1);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	default:
7457c478bd9Sstevel@tonic-gate 		mdb_warn("unexpected ioctl 0x%x (%s)\n", cmd,
7467c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROVIDER	? "DTRACEIOC_PROVIDER" :
7477c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBES	? "DTRACEIOC_PROBES" :
7487c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_BUFSNAP	? "DTRACEIOC_BUFSNAP" :
7497c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBEMATCH	? "DTRACEIOC_PROBEMATCH" :
7507c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_ENABLE	? "DTRACEIOC_ENABLE" :
7517c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_AGGSNAP	? "DTRACEIOC_AGGSNAP" :
7527c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_EPROBE	? "DTRACEIOC_EPROBE" :
7537c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBEARG	? "DTRACEIOC_PROBEARG" :
7547c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_CONF	? "DTRACEIOC_CONF" :
7557c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_STATUS	? "DTRACEIOC_STATUS" :
7567c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_GO		? "DTRACEIOC_GO" :
7577c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_STOP	? "DTRACEIOC_STOP" :
7587c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_AGGDESC	? "DTRACEIOC_AGGDESC" :
7597c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_FORMAT	? "DTRACEIOC_FORMAT" :
7607c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_DOFGET	? "DTRACEIOC_DOFGET" :
7617c478bd9Sstevel@tonic-gate 		    cmd == DTRACEIOC_REPLICATE	? "DTRACEIOC_REPLICATE" :
7627c478bd9Sstevel@tonic-gate 		    "???");
7637c478bd9Sstevel@tonic-gate 		errno = ENXIO;
7647c478bd9Sstevel@tonic-gate 		return (-1);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate static int
dtracemdb_modctl(uintptr_t addr,const struct modctl * m,dtracemdb_data_t * data)7697c478bd9Sstevel@tonic-gate dtracemdb_modctl(uintptr_t addr, const struct modctl *m, dtracemdb_data_t *data)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	struct module mod;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (m->mod_mp == NULL)
7747c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
7777c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read modctl %p's module", addr);
7787c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if ((uintptr_t)mod.text > data->dtmd_addr)
7827c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	if ((uintptr_t)mod.text + mod.text_size <= data->dtmd_addr)
7857c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (mdb_readstr(data->dtmd_modstr, MDB_SYM_NAMLEN,
7887c478bd9Sstevel@tonic-gate 	    (uintptr_t)m->mod_modname) == -1)
7897c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	return (WALK_DONE);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate static int
dtracemdb_lookup_by_addr(void * varg,GElf_Addr addr,GElf_Sym * symp,dtrace_syminfo_t * sip)7957c478bd9Sstevel@tonic-gate dtracemdb_lookup_by_addr(void *varg, GElf_Addr addr, GElf_Sym *symp,
7967c478bd9Sstevel@tonic-gate     dtrace_syminfo_t *sip)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	dtracemdb_data_t *data = varg;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (data->dtmd_symstr == NULL) {
8017c478bd9Sstevel@tonic-gate 		data->dtmd_symstr = mdb_zalloc(MDB_SYM_NAMLEN,
8027c478bd9Sstevel@tonic-gate 		    UM_SLEEP | UM_GC);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (data->dtmd_modstr == NULL) {
8067c478bd9Sstevel@tonic-gate 		data->dtmd_modstr = mdb_zalloc(MDB_SYM_NAMLEN,
8077c478bd9Sstevel@tonic-gate 		    UM_SLEEP | UM_GC);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if (symp != NULL) {
8117c478bd9Sstevel@tonic-gate 		if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, data->dtmd_symstr,
8127c478bd9Sstevel@tonic-gate 		    MDB_SYM_NAMLEN, symp) == -1)
8137c478bd9Sstevel@tonic-gate 			return (-1);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
8177c478bd9Sstevel@tonic-gate 		data->dtmd_addr = addr;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		(void) strcpy(data->dtmd_modstr, "???");
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		if (mdb_walk("modctl",
8227c478bd9Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtracemdb_modctl, varg) == -1) {
8237c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't walk 'modctl'");
8247c478bd9Sstevel@tonic-gate 			return (-1);
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		sip->dts_object = data->dtmd_modstr;
8287c478bd9Sstevel@tonic-gate 		sip->dts_id = 0;
8297c478bd9Sstevel@tonic-gate 		sip->dts_name = symp != NULL ? data->dtmd_symstr : NULL;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	return (0);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8367c478bd9Sstevel@tonic-gate static int
dtracemdb_stat(void * varg,processorid_t cpu)8377c478bd9Sstevel@tonic-gate dtracemdb_stat(void *varg, processorid_t cpu)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
8407c478bd9Sstevel@tonic-gate 	cpu_t c;
8417c478bd9Sstevel@tonic-gate 	uintptr_t caddr, addr;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("cpu", &sym) == -1) {
8447c478bd9Sstevel@tonic-gate 		mdb_warn("failed to find symbol for 'cpu'");
8457c478bd9Sstevel@tonic-gate 		return (-1);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	if (cpu * sizeof (uintptr_t) > sym.st_size)
8497c478bd9Sstevel@tonic-gate 		return (-1);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)sym.st_value + cpu * sizeof (uintptr_t);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (mdb_vread(&caddr, sizeof (caddr), addr) == -1) {
8547c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read cpu[%d]", cpu);
8557c478bd9Sstevel@tonic-gate 		return (-1);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
858892ad162SToomas Soome 	if (caddr == 0)
8597c478bd9Sstevel@tonic-gate 		return (-1);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), caddr) == -1) {
8627c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read cpu at %p", caddr);
8637c478bd9Sstevel@tonic-gate 		return (-1);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if (c.cpu_flags & CPU_POWEROFF) {
8677c478bd9Sstevel@tonic-gate 		return (P_POWEROFF);
8687c478bd9Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_SPARE) {
8697c478bd9Sstevel@tonic-gate 		return (P_SPARE);
8707c478bd9Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_FAULTED) {
8717c478bd9Sstevel@tonic-gate 		return (P_FAULTED);
872c3377ee9SJohn Levon 	} else if (c.cpu_flags & CPU_DISABLED) {
873c3377ee9SJohn Levon 		return (P_DISABLED);
8747c478bd9Sstevel@tonic-gate 	} else if ((c.cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) {
8757c478bd9Sstevel@tonic-gate 		return (P_OFFLINE);
8767c478bd9Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_ENABLE) {
8777c478bd9Sstevel@tonic-gate 		return (P_ONLINE);
8787c478bd9Sstevel@tonic-gate 	} else {
8797c478bd9Sstevel@tonic-gate 		return (P_NOINTR);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8847c478bd9Sstevel@tonic-gate static long
dtracemdb_sysconf(void * varg,int name)8857c478bd9Sstevel@tonic-gate dtracemdb_sysconf(void *varg, int name)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	int max_ncpus;
8887c478bd9Sstevel@tonic-gate 	processorid_t max_cpuid;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	switch (name) {
8917c478bd9Sstevel@tonic-gate 	case _SC_CPUID_MAX:
8927c478bd9Sstevel@tonic-gate 		if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
8937c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read 'max_cpuid'");
8947c478bd9Sstevel@tonic-gate 			return (-1);
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		return (max_cpuid);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	case _SC_NPROCESSORS_MAX:
9007c478bd9Sstevel@tonic-gate 		if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
9017c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read 'max_ncpus'");
9027c478bd9Sstevel@tonic-gate 			return (-1);
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		return (max_ncpus);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	default:
9087c478bd9Sstevel@tonic-gate 		mdb_warn("unexpected sysconf code %d\n", name);
9097c478bd9Sstevel@tonic-gate 		return (-1);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate const dtrace_vector_t dtrace_mdbops = {
9147c478bd9Sstevel@tonic-gate 	dtracemdb_ioctl,
9157c478bd9Sstevel@tonic-gate 	dtracemdb_lookup_by_addr,
9167c478bd9Sstevel@tonic-gate 	dtracemdb_stat,
9177c478bd9Sstevel@tonic-gate 	dtracemdb_sysconf
9187c478bd9Sstevel@tonic-gate };
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate typedef struct dtrace_dcmddata {
9217c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtdd_dtp;
9227c478bd9Sstevel@tonic-gate 	int dtdd_cpu;
9237c478bd9Sstevel@tonic-gate 	int dtdd_quiet;
9247c478bd9Sstevel@tonic-gate 	int dtdd_flowindent;
9257c478bd9Sstevel@tonic-gate 	int dtdd_heading;
9267994dfddSDan Kimmel 	FILE *dtdd_output;
9277c478bd9Sstevel@tonic-gate } dtrace_dcmddata_t;
9287c478bd9Sstevel@tonic-gate 
9297994dfddSDan Kimmel /*
9307994dfddSDan Kimmel  * Helper to grab all the content from a file, spit it into a string, and erase
9317994dfddSDan Kimmel  * and reset the file.
9327994dfddSDan Kimmel  */
9337994dfddSDan Kimmel static void
print_and_truncate_file(FILE * fp)9347994dfddSDan Kimmel print_and_truncate_file(FILE *fp)
9357994dfddSDan Kimmel {
9367994dfddSDan Kimmel 	long len;
9377994dfddSDan Kimmel 	char *out;
9387994dfddSDan Kimmel 
9397994dfddSDan Kimmel 	/* flush, find length of file, seek to beginning, initialize buffer */
9407994dfddSDan Kimmel 	if (fflush(fp) || (len = ftell(fp)) < 0 ||
9417994dfddSDan Kimmel 	    fseek(fp, 0, SEEK_SET) < 0) {
9427994dfddSDan Kimmel 		mdb_warn("couldn't prepare DTrace output file: %d\n", errno);
9437994dfddSDan Kimmel 		return;
9447994dfddSDan Kimmel 	}
9457994dfddSDan Kimmel 
9467994dfddSDan Kimmel 	out = mdb_alloc(len + 1, UM_SLEEP);
9477994dfddSDan Kimmel 	out[len] = '\0';
9487994dfddSDan Kimmel 
9497994dfddSDan Kimmel 	/* read file into buffer, truncate file, and seek to beginning */
9507994dfddSDan Kimmel 	if ((fread(out, len + 1, sizeof (char), fp) == 0 && ferror(fp)) ||
9517994dfddSDan Kimmel 	    ftruncate(fileno(fp), 0) < 0 || fseek(fp, 0, SEEK_SET) < 0) {
9527994dfddSDan Kimmel 		mdb_warn("couldn't read DTrace output file: %d\n", errno);
9537994dfddSDan Kimmel 		mdb_free(out, len + 1);
9547994dfddSDan Kimmel 		return;
9557994dfddSDan Kimmel 	}
9567994dfddSDan Kimmel 
9577994dfddSDan Kimmel 	mdb_printf("%s", out);
9587994dfddSDan Kimmel 	mdb_free(out, len + 1);
9597994dfddSDan Kimmel }
9607994dfddSDan Kimmel 
9617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9627c478bd9Sstevel@tonic-gate static int
dtrace_dcmdrec(const dtrace_probedata_t * data,const dtrace_recdesc_t * rec,void * arg)9637c478bd9Sstevel@tonic-gate dtrace_dcmdrec(const dtrace_probedata_t *data,
9647c478bd9Sstevel@tonic-gate     const dtrace_recdesc_t *rec, void *arg)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	dtrace_dcmddata_t *dd = arg;
9677c478bd9Sstevel@tonic-gate 
9687994dfddSDan Kimmel 	print_and_truncate_file(dd->dtdd_output);
9697994dfddSDan Kimmel 
9707c478bd9Sstevel@tonic-gate 	if (rec == NULL) {
9717c478bd9Sstevel@tonic-gate 		/*
9727c478bd9Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
9737c478bd9Sstevel@tonic-gate 		 * we're not in quiet mode.
9747c478bd9Sstevel@tonic-gate 		 */
9757c478bd9Sstevel@tonic-gate 		if (!dd->dtdd_quiet)
9767c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9857c478bd9Sstevel@tonic-gate static int
dtrace_dcmdprobe(const dtrace_probedata_t * data,void * arg)9867c478bd9Sstevel@tonic-gate dtrace_dcmdprobe(const dtrace_probedata_t *data, void *arg)
9877c478bd9Sstevel@tonic-gate {
9887c478bd9Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
9897c478bd9Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
9907c478bd9Sstevel@tonic-gate 	dtrace_dcmddata_t *dd = arg;
9917c478bd9Sstevel@tonic-gate 	char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (dd->dtdd_cpu != -1UL && dd->dtdd_cpu != cpu)
9947c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if (dd->dtdd_heading == 0) {
9977c478bd9Sstevel@tonic-gate 		if (!dd->dtdd_flowindent) {
9987c478bd9Sstevel@tonic-gate 			if (!dd->dtdd_quiet) {
9997c478bd9Sstevel@tonic-gate 				mdb_printf("%3s %6s %32s\n",
10007c478bd9Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
10017c478bd9Sstevel@tonic-gate 			}
10027c478bd9Sstevel@tonic-gate 		} else {
10037c478bd9Sstevel@tonic-gate 			mdb_printf("%3s %-41s\n", "CPU", "FUNCTION");
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		dd->dtdd_heading = 1;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (!dd->dtdd_flowindent) {
10097c478bd9Sstevel@tonic-gate 		if (!dd->dtdd_quiet) {
10107c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%s:%s",
10117c478bd9Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 			mdb_printf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 	} else {
10167c478bd9Sstevel@tonic-gate 		int indent = data->dtpda_indent;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
10197c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s:%s",
10207c478bd9Sstevel@tonic-gate 			    indent, "", data->dtpda_prefix, pd->dtpd_func,
10217c478bd9Sstevel@tonic-gate 			    pd->dtpd_name);
10227c478bd9Sstevel@tonic-gate 		} else {
10237c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s",
10247c478bd9Sstevel@tonic-gate 			    indent, "", data->dtpda_prefix, pd->dtpd_func);
10257c478bd9Sstevel@tonic-gate 		}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		mdb_printf("%3d %-41s ", cpu, name);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10347c478bd9Sstevel@tonic-gate static int
dtrace_dcmderr(const dtrace_errdata_t * data,void * arg)1035a1b5e537Sbmc dtrace_dcmderr(const dtrace_errdata_t *data, void *arg)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	mdb_warn(data->dteda_msg);
10387c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10427c478bd9Sstevel@tonic-gate static int
dtrace_dcmddrop(const dtrace_dropdata_t * data,void * arg)1043a1b5e537Sbmc dtrace_dcmddrop(const dtrace_dropdata_t *data, void *arg)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate 	mdb_warn(data->dtdda_msg);
10467c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10507c478bd9Sstevel@tonic-gate static int
dtrace_dcmdbuffered(const dtrace_bufdata_t * bufdata,void * arg)1051a1b5e537Sbmc dtrace_dcmdbuffered(const dtrace_bufdata_t *bufdata, void *arg)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	mdb_printf("%s", bufdata->dtbda_buffered);
10547c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10587c478bd9Sstevel@tonic-gate int
dtrace(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)10597c478bd9Sstevel@tonic-gate dtrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	dtrace_state_t state;
10627c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp;
10637c478bd9Sstevel@tonic-gate 	int ncpu, err;
10647c478bd9Sstevel@tonic-gate 	uintptr_t c = -1UL;
10657c478bd9Sstevel@tonic-gate 	dtrace_dcmddata_t dd;
10667c478bd9Sstevel@tonic-gate 	dtrace_optval_t val;
10677c478bd9Sstevel@tonic-gate 	dtracemdb_data_t md;
10687c478bd9Sstevel@tonic-gate 	int rval = DCMD_ERR;
1069c9a6ea2eSBryan Cantrill 	dtrace_anon_t anon;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
10727c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv, 'c', MDB_OPT_UINTPTR, &c, NULL) != argc)
10757c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
10787c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read '_ncpu'");
10797c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if (mdb_vread(&state, sizeof (state), addr) == -1) {
10837c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read dtrace_state_t at %p", addr);
10847c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
1087c9a6ea2eSBryan Cantrill 	if (state.dts_anon != NULL) {
1088c9a6ea2eSBryan Cantrill 		addr = (uintptr_t)state.dts_anon;
1089c9a6ea2eSBryan Cantrill 
1090c9a6ea2eSBryan Cantrill 		if (mdb_vread(&state, sizeof (state), addr) == -1) {
1091c9a6ea2eSBryan Cantrill 			mdb_warn("couldn't read anonymous state at %p", addr);
1092c9a6ea2eSBryan Cantrill 			return (DCMD_ERR);
1093