xref: /freebsd/sys/cddl/dev/dtrace/dtrace_ioctl.c (revision 9610c897509b09d40c61c0bf7c054d4474fa6cdf)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell  * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell  *
891eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell  * and limitations under the License.
1291eaf3e1SJohn Birrell  *
1391eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell  *
1991eaf3e1SJohn Birrell  * CDDL HEADER END
2091eaf3e1SJohn Birrell  *
2191eaf3e1SJohn Birrell  * $FreeBSD$
2291eaf3e1SJohn Birrell  *
2391eaf3e1SJohn Birrell  */
2491eaf3e1SJohn Birrell 
2591eaf3e1SJohn Birrell static int dtrace_verbose_ioctl;
26b53bfbbaSMark Johnston SYSCTL_INT(_debug_dtrace, OID_AUTO, verbose_ioctl, CTLFLAG_RW,
27b53bfbbaSMark Johnston     &dtrace_verbose_ioctl, 0, "log DTrace ioctls");
2891eaf3e1SJohn Birrell 
2991eaf3e1SJohn Birrell #define DTRACE_IOCTL_PRINTF(fmt, ...)	if (dtrace_verbose_ioctl) printf(fmt, ## __VA_ARGS__ )
3091eaf3e1SJohn Birrell 
31c6f5742fSRui Paulo static int
32c6f5742fSRui Paulo dtrace_ioctl_helper(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
33c6f5742fSRui Paulo     struct thread *td)
34c6f5742fSRui Paulo {
356e0f204cSMark Johnston 	struct proc *p;
366e0f204cSMark Johnston 	dof_helper_t *dhp;
376e0f204cSMark Johnston 	dof_hdr_t *dof;
3867cf27b7SMark Johnston 	int rval;
39c6f5742fSRui Paulo 
406e0f204cSMark Johnston 	dhp = NULL;
416e0f204cSMark Johnston 	dof = NULL;
426e0f204cSMark Johnston 	rval = 0;
43c6f5742fSRui Paulo 	switch (cmd) {
44c6f5742fSRui Paulo 	case DTRACEHIOC_ADDDOF:
45c6f5742fSRui Paulo 		dhp = (dof_helper_t *)addr;
466e0f204cSMark Johnston 		addr = (caddr_t)(uintptr_t)dhp->dofhp_dof;
47c6f5742fSRui Paulo 		/* FALLTHROUGH */
48c6f5742fSRui Paulo 	case DTRACEHIOC_ADD:
496e0f204cSMark Johnston 		p = curproc;
50*9610c897SMark Johnston 		if (dhp == NULL || p->p_pid == dhp->dofhp_pid) {
516e0f204cSMark Johnston 			dof = dtrace_dof_copyin((uintptr_t)addr, &rval);
526e0f204cSMark Johnston 		} else {
536e0f204cSMark Johnston 			p = pfind(dhp->dofhp_pid);
546e0f204cSMark Johnston 			if (p == NULL)
556e0f204cSMark Johnston 				return (EINVAL);
566e0f204cSMark Johnston 			if (!P_SHOULDSTOP(p) ||
57*9610c897SMark Johnston 			    (p->p_flag & (P_TRACED | P_WEXIT)) != P_TRACED ||
586e0f204cSMark Johnston 			    p->p_pptr != curproc) {
596e0f204cSMark Johnston 				PROC_UNLOCK(p);
606e0f204cSMark Johnston 				return (EINVAL);
616e0f204cSMark Johnston 			}
626e0f204cSMark Johnston 			_PHOLD(p);
636e0f204cSMark Johnston 			PROC_UNLOCK(p);
646e0f204cSMark Johnston 			dof = dtrace_dof_copyin_proc(p, (uintptr_t)addr, &rval);
656e0f204cSMark Johnston 		}
66c6f5742fSRui Paulo 
676e0f204cSMark Johnston 		if (dof == NULL) {
686e0f204cSMark Johnston 			if (p != curproc)
696e0f204cSMark Johnston 				PRELE(p);
706e0f204cSMark Johnston 			break;
716e0f204cSMark Johnston 		}
72c6f5742fSRui Paulo 
73c6f5742fSRui Paulo 		mutex_enter(&dtrace_lock);
746e0f204cSMark Johnston 		if ((rval = dtrace_helper_slurp(dof, dhp, p)) != -1) {
756e0f204cSMark Johnston 			if (dhp != NULL) {
7667cf27b7SMark Johnston 				dhp->dofhp_gen = rval;
77c6f5742fSRui Paulo 				copyout(dhp, addr, sizeof(*dhp));
78c6f5742fSRui Paulo 			}
79c6f5742fSRui Paulo 			rval = 0;
80c6f5742fSRui Paulo 		} else {
81c6f5742fSRui Paulo 			rval = EINVAL;
82c6f5742fSRui Paulo 		}
83c6f5742fSRui Paulo 		mutex_exit(&dtrace_lock);
846e0f204cSMark Johnston 		if (p != curproc)
856e0f204cSMark Johnston 			PRELE(p);
866e0f204cSMark Johnston 		break;
87c6f5742fSRui Paulo 	case DTRACEHIOC_REMOVE:
88c6f5742fSRui Paulo 		mutex_enter(&dtrace_lock);
896e0f204cSMark Johnston 		rval = dtrace_helper_destroygen(NULL, *(int *)(uintptr_t)addr);
90c6f5742fSRui Paulo 		mutex_exit(&dtrace_lock);
916e0f204cSMark Johnston 		break;
92c6f5742fSRui Paulo 	default:
936e0f204cSMark Johnston 		rval = ENOTTY;
94c6f5742fSRui Paulo 		break;
95c6f5742fSRui Paulo 	}
966e0f204cSMark Johnston 	return (rval);
97c6f5742fSRui Paulo }
98c6f5742fSRui Paulo 
9991eaf3e1SJohn Birrell /* ARGSUSED */
10091eaf3e1SJohn Birrell static int
10191eaf3e1SJohn Birrell dtrace_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
10291eaf3e1SJohn Birrell     int flags __unused, struct thread *td)
10391eaf3e1SJohn Birrell {
104f5a97d1bSCraig Rodrigues 	dtrace_state_t *state;
105f5a97d1bSCraig Rodrigues 	devfs_get_cdevpriv((void **) &state);
106a99098e2SDavide Italiano 
10791eaf3e1SJohn Birrell 	int error = 0;
10891eaf3e1SJohn Birrell 	if (state == NULL)
10991eaf3e1SJohn Birrell 		return (EINVAL);
11091eaf3e1SJohn Birrell 
11191eaf3e1SJohn Birrell 	if (state->dts_anon) {
11291eaf3e1SJohn Birrell 		ASSERT(dtrace_anon.dta_state == NULL);
11391eaf3e1SJohn Birrell 		state = state->dts_anon;
11491eaf3e1SJohn Birrell 	}
11591eaf3e1SJohn Birrell 
11691eaf3e1SJohn Birrell 	switch (cmd) {
11791eaf3e1SJohn Birrell 	case DTRACEIOC_AGGDESC: {
11891eaf3e1SJohn Birrell 		dtrace_aggdesc_t **paggdesc = (dtrace_aggdesc_t **) addr;
11991eaf3e1SJohn Birrell 		dtrace_aggdesc_t aggdesc;
12091eaf3e1SJohn Birrell 		dtrace_action_t *act;
12191eaf3e1SJohn Birrell 		dtrace_aggregation_t *agg;
12291eaf3e1SJohn Birrell 		int nrecs;
12391eaf3e1SJohn Birrell 		uint32_t offs;
12491eaf3e1SJohn Birrell 		dtrace_recdesc_t *lrec;
12591eaf3e1SJohn Birrell 		void *buf;
12691eaf3e1SJohn Birrell 		size_t size;
12791eaf3e1SJohn Birrell 		uintptr_t dest;
12891eaf3e1SJohn Birrell 
12991eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_AGGDESC\n",__func__,__LINE__);
13091eaf3e1SJohn Birrell 
13191eaf3e1SJohn Birrell 		if (copyin((void *) *paggdesc, &aggdesc, sizeof (aggdesc)) != 0)
13291eaf3e1SJohn Birrell 			return (EFAULT);
13391eaf3e1SJohn Birrell 
13491eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
13591eaf3e1SJohn Birrell 
13691eaf3e1SJohn Birrell 		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
13791eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
13891eaf3e1SJohn Birrell 			return (EINVAL);
13991eaf3e1SJohn Birrell 		}
14091eaf3e1SJohn Birrell 
14191eaf3e1SJohn Birrell 		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
14291eaf3e1SJohn Birrell 
14391eaf3e1SJohn Birrell 		nrecs = aggdesc.dtagd_nrecs;
14491eaf3e1SJohn Birrell 		aggdesc.dtagd_nrecs = 0;
14591eaf3e1SJohn Birrell 
14691eaf3e1SJohn Birrell 		offs = agg->dtag_base;
14791eaf3e1SJohn Birrell 		lrec = &agg->dtag_action.dta_rec;
14891eaf3e1SJohn Birrell 		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
14991eaf3e1SJohn Birrell 
15091eaf3e1SJohn Birrell 		for (act = agg->dtag_first; ; act = act->dta_next) {
15191eaf3e1SJohn Birrell 			ASSERT(act->dta_intuple ||
15291eaf3e1SJohn Birrell 			    DTRACEACT_ISAGG(act->dta_kind));
15391eaf3e1SJohn Birrell 
15491eaf3e1SJohn Birrell 			/*
15591eaf3e1SJohn Birrell 			 * If this action has a record size of zero, it
15691eaf3e1SJohn Birrell 			 * denotes an argument to the aggregating action.
15791eaf3e1SJohn Birrell 			 * Because the presence of this record doesn't (or
15891eaf3e1SJohn Birrell 			 * shouldn't) affect the way the data is interpreted,
15991eaf3e1SJohn Birrell 			 * we don't copy it out to save user-level the
16091eaf3e1SJohn Birrell 			 * confusion of dealing with a zero-length record.
16191eaf3e1SJohn Birrell 			 */
16291eaf3e1SJohn Birrell 			if (act->dta_rec.dtrd_size == 0) {
16391eaf3e1SJohn Birrell 				ASSERT(agg->dtag_hasarg);
16491eaf3e1SJohn Birrell 				continue;
16591eaf3e1SJohn Birrell 			}
16691eaf3e1SJohn Birrell 
16791eaf3e1SJohn Birrell 			aggdesc.dtagd_nrecs++;
16891eaf3e1SJohn Birrell 
16991eaf3e1SJohn Birrell 			if (act == &agg->dtag_action)
17091eaf3e1SJohn Birrell 				break;
17191eaf3e1SJohn Birrell 		}
17291eaf3e1SJohn Birrell 
17391eaf3e1SJohn Birrell 		/*
17491eaf3e1SJohn Birrell 		 * Now that we have the size, we need to allocate a temporary
17591eaf3e1SJohn Birrell 		 * buffer in which to store the complete description.  We need
17691eaf3e1SJohn Birrell 		 * the temporary buffer to be able to drop dtrace_lock()
17791eaf3e1SJohn Birrell 		 * across the copyout(), below.
17891eaf3e1SJohn Birrell 		 */
17991eaf3e1SJohn Birrell 		size = sizeof (dtrace_aggdesc_t) +
18091eaf3e1SJohn Birrell 		    (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
18191eaf3e1SJohn Birrell 
18291eaf3e1SJohn Birrell 		buf = kmem_alloc(size, KM_SLEEP);
18391eaf3e1SJohn Birrell 		dest = (uintptr_t)buf;
18491eaf3e1SJohn Birrell 
18591eaf3e1SJohn Birrell 		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
18691eaf3e1SJohn Birrell 		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
18791eaf3e1SJohn Birrell 
18891eaf3e1SJohn Birrell 		for (act = agg->dtag_first; ; act = act->dta_next) {
18991eaf3e1SJohn Birrell 			dtrace_recdesc_t rec = act->dta_rec;
19091eaf3e1SJohn Birrell 
19191eaf3e1SJohn Birrell 			/*
19291eaf3e1SJohn Birrell 			 * See the comment in the above loop for why we pass
19391eaf3e1SJohn Birrell 			 * over zero-length records.
19491eaf3e1SJohn Birrell 			 */
19591eaf3e1SJohn Birrell 			if (rec.dtrd_size == 0) {
19691eaf3e1SJohn Birrell 				ASSERT(agg->dtag_hasarg);
19791eaf3e1SJohn Birrell 				continue;
19891eaf3e1SJohn Birrell 			}
19991eaf3e1SJohn Birrell 
20091eaf3e1SJohn Birrell 			if (nrecs-- == 0)
20191eaf3e1SJohn Birrell 				break;
20291eaf3e1SJohn Birrell 
20391eaf3e1SJohn Birrell 			rec.dtrd_offset -= offs;
20491eaf3e1SJohn Birrell 			bcopy(&rec, (void *)dest, sizeof (rec));
20591eaf3e1SJohn Birrell 			dest += sizeof (dtrace_recdesc_t);
20691eaf3e1SJohn Birrell 
20791eaf3e1SJohn Birrell 			if (act == &agg->dtag_action)
20891eaf3e1SJohn Birrell 				break;
20991eaf3e1SJohn Birrell 		}
21091eaf3e1SJohn Birrell 
21191eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
21291eaf3e1SJohn Birrell 
21391eaf3e1SJohn Birrell 		if (copyout(buf, (void *) *paggdesc, dest - (uintptr_t)buf) != 0) {
21491eaf3e1SJohn Birrell 			kmem_free(buf, size);
21591eaf3e1SJohn Birrell 			return (EFAULT);
21691eaf3e1SJohn Birrell 		}
21791eaf3e1SJohn Birrell 
21891eaf3e1SJohn Birrell 		kmem_free(buf, size);
21991eaf3e1SJohn Birrell 		return (0);
22091eaf3e1SJohn Birrell 	}
22191eaf3e1SJohn Birrell 	case DTRACEIOC_AGGSNAP:
22291eaf3e1SJohn Birrell 	case DTRACEIOC_BUFSNAP: {
22391eaf3e1SJohn Birrell 		dtrace_bufdesc_t **pdesc = (dtrace_bufdesc_t **) addr;
22491eaf3e1SJohn Birrell 		dtrace_bufdesc_t desc;
22591eaf3e1SJohn Birrell 		caddr_t cached;
22691eaf3e1SJohn Birrell 		dtrace_buffer_t *buf;
22791eaf3e1SJohn Birrell 
22891eaf3e1SJohn Birrell 		dtrace_debug_output();
22991eaf3e1SJohn Birrell 
23091eaf3e1SJohn Birrell 		if (copyin((void *) *pdesc, &desc, sizeof (desc)) != 0)
23191eaf3e1SJohn Birrell 			return (EFAULT);
23291eaf3e1SJohn Birrell 
23391eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): %s curcpu %d cpu %d\n",
23491eaf3e1SJohn Birrell 		    __func__,__LINE__,
23591eaf3e1SJohn Birrell 		    cmd == DTRACEIOC_AGGSNAP ?
23691eaf3e1SJohn Birrell 		    "DTRACEIOC_AGGSNAP":"DTRACEIOC_BUFSNAP",
23791eaf3e1SJohn Birrell 		    curcpu, desc.dtbd_cpu);
23891eaf3e1SJohn Birrell 
2393c56b4f1SSergey Kandaurov 		if (desc.dtbd_cpu >= NCPU)
24091eaf3e1SJohn Birrell 			return (ENOENT);
24191eaf3e1SJohn Birrell 		if (pcpu_find(desc.dtbd_cpu) == NULL)
24291eaf3e1SJohn Birrell 			return (ENOENT);
24391eaf3e1SJohn Birrell 
24491eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
24591eaf3e1SJohn Birrell 
24691eaf3e1SJohn Birrell 		if (cmd == DTRACEIOC_BUFSNAP) {
24791eaf3e1SJohn Birrell 			buf = &state->dts_buffer[desc.dtbd_cpu];
24891eaf3e1SJohn Birrell 		} else {
24991eaf3e1SJohn Birrell 			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
25091eaf3e1SJohn Birrell 		}
25191eaf3e1SJohn Birrell 
25291eaf3e1SJohn Birrell 		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
25391eaf3e1SJohn Birrell 			size_t sz = buf->dtb_offset;
25491eaf3e1SJohn Birrell 
25591eaf3e1SJohn Birrell 			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
25691eaf3e1SJohn Birrell 				mutex_exit(&dtrace_lock);
25791eaf3e1SJohn Birrell 				return (EBUSY);
25891eaf3e1SJohn Birrell 			}
25991eaf3e1SJohn Birrell 
26091eaf3e1SJohn Birrell 			/*
26191eaf3e1SJohn Birrell 			 * If this buffer has already been consumed, we're
26291eaf3e1SJohn Birrell 			 * going to indicate that there's nothing left here
26391eaf3e1SJohn Birrell 			 * to consume.
26491eaf3e1SJohn Birrell 			 */
26591eaf3e1SJohn Birrell 			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
26691eaf3e1SJohn Birrell 				mutex_exit(&dtrace_lock);
26791eaf3e1SJohn Birrell 
26891eaf3e1SJohn Birrell 				desc.dtbd_size = 0;
26991eaf3e1SJohn Birrell 				desc.dtbd_drops = 0;
27091eaf3e1SJohn Birrell 				desc.dtbd_errors = 0;
27191eaf3e1SJohn Birrell 				desc.dtbd_oldest = 0;
27291eaf3e1SJohn Birrell 				sz = sizeof (desc);
27391eaf3e1SJohn Birrell 
27491eaf3e1SJohn Birrell 				if (copyout(&desc, (void *) *pdesc, sz) != 0)
27591eaf3e1SJohn Birrell 					return (EFAULT);
27691eaf3e1SJohn Birrell 
27791eaf3e1SJohn Birrell 				return (0);
27891eaf3e1SJohn Birrell 			}
27991eaf3e1SJohn Birrell 
28091eaf3e1SJohn Birrell 			/*
28191eaf3e1SJohn Birrell 			 * If this is a ring buffer that has wrapped, we want
28291eaf3e1SJohn Birrell 			 * to copy the whole thing out.
28391eaf3e1SJohn Birrell 			 */
28491eaf3e1SJohn Birrell 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
28591eaf3e1SJohn Birrell 				dtrace_buffer_polish(buf);
28691eaf3e1SJohn Birrell 				sz = buf->dtb_size;
28791eaf3e1SJohn Birrell 			}
28891eaf3e1SJohn Birrell 
28991eaf3e1SJohn Birrell 			if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
29091eaf3e1SJohn Birrell 				mutex_exit(&dtrace_lock);
29191eaf3e1SJohn Birrell 				return (EFAULT);
29291eaf3e1SJohn Birrell 			}
29391eaf3e1SJohn Birrell 
29491eaf3e1SJohn Birrell 			desc.dtbd_size = sz;
29591eaf3e1SJohn Birrell 			desc.dtbd_drops = buf->dtb_drops;
29691eaf3e1SJohn Birrell 			desc.dtbd_errors = buf->dtb_errors;
29791eaf3e1SJohn Birrell 			desc.dtbd_oldest = buf->dtb_xamot_offset;
29809e6105fSMark Johnston 			desc.dtbd_timestamp = dtrace_gethrtime();
29991eaf3e1SJohn Birrell 
30091eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
30191eaf3e1SJohn Birrell 
30291eaf3e1SJohn Birrell 			if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
30391eaf3e1SJohn Birrell 				return (EFAULT);
30491eaf3e1SJohn Birrell 
30591eaf3e1SJohn Birrell 			buf->dtb_flags |= DTRACEBUF_CONSUMED;
30691eaf3e1SJohn Birrell 
30791eaf3e1SJohn Birrell 			return (0);
30891eaf3e1SJohn Birrell 		}
30991eaf3e1SJohn Birrell 
31091eaf3e1SJohn Birrell 		if (buf->dtb_tomax == NULL) {
31191eaf3e1SJohn Birrell 			ASSERT(buf->dtb_xamot == NULL);
31291eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
31391eaf3e1SJohn Birrell 			return (ENOENT);
31491eaf3e1SJohn Birrell 		}
31591eaf3e1SJohn Birrell 
31691eaf3e1SJohn Birrell 		cached = buf->dtb_tomax;
31791eaf3e1SJohn Birrell 		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
31891eaf3e1SJohn Birrell 
31991eaf3e1SJohn Birrell 		dtrace_xcall(desc.dtbd_cpu,
32091eaf3e1SJohn Birrell 		    (dtrace_xcall_t)dtrace_buffer_switch, buf);
32191eaf3e1SJohn Birrell 
32291eaf3e1SJohn Birrell 		state->dts_errors += buf->dtb_xamot_errors;
32391eaf3e1SJohn Birrell 
32491eaf3e1SJohn Birrell 		/*
32591eaf3e1SJohn Birrell 		 * If the buffers did not actually switch, then the cross call
32691eaf3e1SJohn Birrell 		 * did not take place -- presumably because the given CPU is
32791eaf3e1SJohn Birrell 		 * not in the ready set.  If this is the case, we'll return
32891eaf3e1SJohn Birrell 		 * ENOENT.
32991eaf3e1SJohn Birrell 		 */
33091eaf3e1SJohn Birrell 		if (buf->dtb_tomax == cached) {
33191eaf3e1SJohn Birrell 			ASSERT(buf->dtb_xamot != cached);
33291eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
33391eaf3e1SJohn Birrell 			return (ENOENT);
33491eaf3e1SJohn Birrell 		}
33591eaf3e1SJohn Birrell 
33691eaf3e1SJohn Birrell 		ASSERT(cached == buf->dtb_xamot);
33791eaf3e1SJohn Birrell 
33891eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): copyout the buffer snapshot\n",__func__,__LINE__);
33991eaf3e1SJohn Birrell 
34091eaf3e1SJohn Birrell 		/*
34191eaf3e1SJohn Birrell 		 * We have our snapshot; now copy it out.
34291eaf3e1SJohn Birrell 		 */
34391eaf3e1SJohn Birrell 		if (copyout(buf->dtb_xamot, desc.dtbd_data,
34491eaf3e1SJohn Birrell 		    buf->dtb_xamot_offset) != 0) {
34591eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
34691eaf3e1SJohn Birrell 			return (EFAULT);
34791eaf3e1SJohn Birrell 		}
34891eaf3e1SJohn Birrell 
34991eaf3e1SJohn Birrell 		desc.dtbd_size = buf->dtb_xamot_offset;
35091eaf3e1SJohn Birrell 		desc.dtbd_drops = buf->dtb_xamot_drops;
35191eaf3e1SJohn Birrell 		desc.dtbd_errors = buf->dtb_xamot_errors;
35291eaf3e1SJohn Birrell 		desc.dtbd_oldest = 0;
35309e6105fSMark Johnston 		desc.dtbd_timestamp = buf->dtb_switched;
35491eaf3e1SJohn Birrell 
35591eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
35691eaf3e1SJohn Birrell 
35791eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): copyout buffer desc: size %zd drops %lu errors %lu\n",__func__,__LINE__,(size_t) desc.dtbd_size,(u_long) desc.dtbd_drops,(u_long) desc.dtbd_errors);
35891eaf3e1SJohn Birrell 
35991eaf3e1SJohn Birrell 		/*
36091eaf3e1SJohn Birrell 		 * Finally, copy out the buffer description.
36191eaf3e1SJohn Birrell 		 */
36291eaf3e1SJohn Birrell 		if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
36391eaf3e1SJohn Birrell 			return (EFAULT);
36491eaf3e1SJohn Birrell 
36591eaf3e1SJohn Birrell 		return (0);
36691eaf3e1SJohn Birrell 	}
36791eaf3e1SJohn Birrell 	case DTRACEIOC_CONF: {
36891eaf3e1SJohn Birrell 		dtrace_conf_t conf;
36991eaf3e1SJohn Birrell 
37091eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_CONF\n",__func__,__LINE__);
37191eaf3e1SJohn Birrell 
37291eaf3e1SJohn Birrell 		bzero(&conf, sizeof (conf));
37391eaf3e1SJohn Birrell 		conf.dtc_difversion = DIF_VERSION;
37491eaf3e1SJohn Birrell 		conf.dtc_difintregs = DIF_DIR_NREGS;
37591eaf3e1SJohn Birrell 		conf.dtc_diftupregs = DIF_DTR_NREGS;
37691eaf3e1SJohn Birrell 		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
37791eaf3e1SJohn Birrell 
37891eaf3e1SJohn Birrell 		*((dtrace_conf_t *) addr) = conf;
37991eaf3e1SJohn Birrell 
38091eaf3e1SJohn Birrell 		return (0);
38191eaf3e1SJohn Birrell 	}
38291eaf3e1SJohn Birrell 	case DTRACEIOC_DOFGET: {
38391eaf3e1SJohn Birrell 		dof_hdr_t **pdof = (dof_hdr_t **) addr;
38491eaf3e1SJohn Birrell 		dof_hdr_t hdr, *dof = *pdof;
38591eaf3e1SJohn Birrell 		int rval;
38691eaf3e1SJohn Birrell 		uint64_t len;
38791eaf3e1SJohn Birrell 
38891eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_DOFGET\n",__func__,__LINE__);
38991eaf3e1SJohn Birrell 
39091eaf3e1SJohn Birrell 		if (copyin((void *)dof, &hdr, sizeof (hdr)) != 0)
39191eaf3e1SJohn Birrell 			return (EFAULT);
39291eaf3e1SJohn Birrell 
39391eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
39491eaf3e1SJohn Birrell 		dof = dtrace_dof_create(state);
39591eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
39691eaf3e1SJohn Birrell 
39791eaf3e1SJohn Birrell 		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
39891eaf3e1SJohn Birrell 		rval = copyout(dof, (void *) *pdof, len);
39991eaf3e1SJohn Birrell 		dtrace_dof_destroy(dof);
40091eaf3e1SJohn Birrell 
40191eaf3e1SJohn Birrell 		return (rval == 0 ? 0 : EFAULT);
40291eaf3e1SJohn Birrell 	}
40391eaf3e1SJohn Birrell 	case DTRACEIOC_ENABLE: {
40491eaf3e1SJohn Birrell 		dof_hdr_t *dof = NULL;
40591eaf3e1SJohn Birrell 		dtrace_enabling_t *enab = NULL;
40691eaf3e1SJohn Birrell 		dtrace_vstate_t *vstate;
40791eaf3e1SJohn Birrell 		int err = 0;
40891eaf3e1SJohn Birrell 		int rval;
40991eaf3e1SJohn Birrell 		dtrace_enable_io_t *p = (dtrace_enable_io_t *) addr;
41091eaf3e1SJohn Birrell 
41191eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_ENABLE\n",__func__,__LINE__);
41291eaf3e1SJohn Birrell 
41391eaf3e1SJohn Birrell 		/*
41491eaf3e1SJohn Birrell 		 * If a NULL argument has been passed, we take this as our
41591eaf3e1SJohn Birrell 		 * cue to reevaluate our enablings.
41691eaf3e1SJohn Birrell 		 */
41791eaf3e1SJohn Birrell 		if (p->dof == NULL) {
41891eaf3e1SJohn Birrell 			dtrace_enabling_matchall();
41991eaf3e1SJohn Birrell 
42091eaf3e1SJohn Birrell 			return (0);
42191eaf3e1SJohn Birrell 		}
42291eaf3e1SJohn Birrell 
42391eaf3e1SJohn Birrell 		if ((dof = dtrace_dof_copyin((uintptr_t) p->dof, &rval)) == NULL)
42491eaf3e1SJohn Birrell 			return (EINVAL);
42591eaf3e1SJohn Birrell 
42691eaf3e1SJohn Birrell 		mutex_enter(&cpu_lock);
42791eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
42891eaf3e1SJohn Birrell 		vstate = &state->dts_vstate;
42991eaf3e1SJohn Birrell 
43091eaf3e1SJohn Birrell 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
43191eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
43291eaf3e1SJohn Birrell 			mutex_exit(&cpu_lock);
43391eaf3e1SJohn Birrell 			dtrace_dof_destroy(dof);
43491eaf3e1SJohn Birrell 			return (EBUSY);
43591eaf3e1SJohn Birrell 		}
43691eaf3e1SJohn Birrell 
43791eaf3e1SJohn Birrell 		if (dtrace_dof_slurp(dof, vstate, td->td_ucred, &enab, 0, B_TRUE) != 0) {
43891eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
43991eaf3e1SJohn Birrell 			mutex_exit(&cpu_lock);
44091eaf3e1SJohn Birrell 			dtrace_dof_destroy(dof);
44191eaf3e1SJohn Birrell 			return (EINVAL);
44291eaf3e1SJohn Birrell 		}
44391eaf3e1SJohn Birrell 
44491eaf3e1SJohn Birrell 		if ((rval = dtrace_dof_options(dof, state)) != 0) {
44591eaf3e1SJohn Birrell 			dtrace_enabling_destroy(enab);
44691eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
44791eaf3e1SJohn Birrell 			mutex_exit(&cpu_lock);
44891eaf3e1SJohn Birrell 			dtrace_dof_destroy(dof);
44991eaf3e1SJohn Birrell 			return (rval);
45091eaf3e1SJohn Birrell 		}
45191eaf3e1SJohn Birrell 
45291eaf3e1SJohn Birrell 		if ((err = dtrace_enabling_match(enab, &p->n_matched)) == 0) {
45391eaf3e1SJohn Birrell 			err = dtrace_enabling_retain(enab);
45491eaf3e1SJohn Birrell 		} else {
45591eaf3e1SJohn Birrell 			dtrace_enabling_destroy(enab);
45691eaf3e1SJohn Birrell 		}
45791eaf3e1SJohn Birrell 
45891eaf3e1SJohn Birrell 		mutex_exit(&cpu_lock);
45991eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
46091eaf3e1SJohn Birrell 		dtrace_dof_destroy(dof);
46191eaf3e1SJohn Birrell 
46291eaf3e1SJohn Birrell 		return (err);
46391eaf3e1SJohn Birrell 	}
46491eaf3e1SJohn Birrell 	case DTRACEIOC_EPROBE: {
46591eaf3e1SJohn Birrell 		dtrace_eprobedesc_t **pepdesc = (dtrace_eprobedesc_t **) addr;
46691eaf3e1SJohn Birrell 		dtrace_eprobedesc_t epdesc;
46791eaf3e1SJohn Birrell 		dtrace_ecb_t *ecb;
46891eaf3e1SJohn Birrell 		dtrace_action_t *act;
46991eaf3e1SJohn Birrell 		void *buf;
47091eaf3e1SJohn Birrell 		size_t size;
47191eaf3e1SJohn Birrell 		uintptr_t dest;
47291eaf3e1SJohn Birrell 		int nrecs;
47391eaf3e1SJohn Birrell 
47491eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_EPROBE\n",__func__,__LINE__);
47591eaf3e1SJohn Birrell 
47691eaf3e1SJohn Birrell 		if (copyin((void *)*pepdesc, &epdesc, sizeof (epdesc)) != 0)
47791eaf3e1SJohn Birrell 			return (EFAULT);
47891eaf3e1SJohn Birrell 
47991eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
48091eaf3e1SJohn Birrell 
48191eaf3e1SJohn Birrell 		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
48291eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
48391eaf3e1SJohn Birrell 			return (EINVAL);
48491eaf3e1SJohn Birrell 		}
48591eaf3e1SJohn Birrell 
48691eaf3e1SJohn Birrell 		if (ecb->dte_probe == NULL) {
48791eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
48891eaf3e1SJohn Birrell 			return (EINVAL);
48991eaf3e1SJohn Birrell 		}
49091eaf3e1SJohn Birrell 
49191eaf3e1SJohn Birrell 		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
49291eaf3e1SJohn Birrell 		epdesc.dtepd_uarg = ecb->dte_uarg;
49391eaf3e1SJohn Birrell 		epdesc.dtepd_size = ecb->dte_size;
49491eaf3e1SJohn Birrell 
49591eaf3e1SJohn Birrell 		nrecs = epdesc.dtepd_nrecs;
49691eaf3e1SJohn Birrell 		epdesc.dtepd_nrecs = 0;
49791eaf3e1SJohn Birrell 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
49891eaf3e1SJohn Birrell 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
49991eaf3e1SJohn Birrell 				continue;
50091eaf3e1SJohn Birrell 
50191eaf3e1SJohn Birrell 			epdesc.dtepd_nrecs++;
50291eaf3e1SJohn Birrell 		}
50391eaf3e1SJohn Birrell 
50491eaf3e1SJohn Birrell 		/*
50591eaf3e1SJohn Birrell 		 * Now that we have the size, we need to allocate a temporary
50691eaf3e1SJohn Birrell 		 * buffer in which to store the complete description.  We need
50791eaf3e1SJohn Birrell 		 * the temporary buffer to be able to drop dtrace_lock()
50891eaf3e1SJohn Birrell 		 * across the copyout(), below.
50991eaf3e1SJohn Birrell 		 */
51091eaf3e1SJohn Birrell 		size = sizeof (dtrace_eprobedesc_t) +
51191eaf3e1SJohn Birrell 		    (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
51291eaf3e1SJohn Birrell 
51391eaf3e1SJohn Birrell 		buf = kmem_alloc(size, KM_SLEEP);
51491eaf3e1SJohn Birrell 		dest = (uintptr_t)buf;
51591eaf3e1SJohn Birrell 
51691eaf3e1SJohn Birrell 		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
51791eaf3e1SJohn Birrell 		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
51891eaf3e1SJohn Birrell 
51991eaf3e1SJohn Birrell 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
52091eaf3e1SJohn Birrell 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
52191eaf3e1SJohn Birrell 				continue;
52291eaf3e1SJohn Birrell 
52391eaf3e1SJohn Birrell 			if (nrecs-- == 0)
52491eaf3e1SJohn Birrell 				break;
52591eaf3e1SJohn Birrell 
52691eaf3e1SJohn Birrell 			bcopy(&act->dta_rec, (void *)dest,
52791eaf3e1SJohn Birrell 			    sizeof (dtrace_recdesc_t));
52891eaf3e1SJohn Birrell 			dest += sizeof (dtrace_recdesc_t);
52991eaf3e1SJohn Birrell 		}
53091eaf3e1SJohn Birrell 
53191eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
53291eaf3e1SJohn Birrell 
53391eaf3e1SJohn Birrell 		if (copyout(buf, (void *) *pepdesc, dest - (uintptr_t)buf) != 0) {
53491eaf3e1SJohn Birrell 			kmem_free(buf, size);
53591eaf3e1SJohn Birrell 			return (EFAULT);
53691eaf3e1SJohn Birrell 		}
53791eaf3e1SJohn Birrell 
53891eaf3e1SJohn Birrell 		kmem_free(buf, size);
53991eaf3e1SJohn Birrell 		return (0);
54091eaf3e1SJohn Birrell 	}
54191eaf3e1SJohn Birrell 	case DTRACEIOC_FORMAT: {
54291eaf3e1SJohn Birrell 		dtrace_fmtdesc_t *fmt = (dtrace_fmtdesc_t *) addr;
54391eaf3e1SJohn Birrell 		char *str;
54491eaf3e1SJohn Birrell 		int len;
54591eaf3e1SJohn Birrell 
54691eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_FORMAT\n",__func__,__LINE__);
54791eaf3e1SJohn Birrell 
54891eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
54991eaf3e1SJohn Birrell 
55091eaf3e1SJohn Birrell 		if (fmt->dtfd_format == 0 ||
55191eaf3e1SJohn Birrell 		    fmt->dtfd_format > state->dts_nformats) {
55291eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
55391eaf3e1SJohn Birrell 			return (EINVAL);
55491eaf3e1SJohn Birrell 		}
55591eaf3e1SJohn Birrell 
55691eaf3e1SJohn Birrell 		/*
55791eaf3e1SJohn Birrell 		 * Format strings are allocated contiguously and they are
55891eaf3e1SJohn Birrell 		 * never freed; if a format index is less than the number
55991eaf3e1SJohn Birrell 		 * of formats, we can assert that the format map is non-NULL
56091eaf3e1SJohn Birrell 		 * and that the format for the specified index is non-NULL.
56191eaf3e1SJohn Birrell 		 */
56291eaf3e1SJohn Birrell 		ASSERT(state->dts_formats != NULL);
56391eaf3e1SJohn Birrell 		str = state->dts_formats[fmt->dtfd_format - 1];
56491eaf3e1SJohn Birrell 		ASSERT(str != NULL);
56591eaf3e1SJohn Birrell 
56691eaf3e1SJohn Birrell 		len = strlen(str) + 1;
56791eaf3e1SJohn Birrell 
56891eaf3e1SJohn Birrell 		if (len > fmt->dtfd_length) {
56991eaf3e1SJohn Birrell 			fmt->dtfd_length = len;
57091eaf3e1SJohn Birrell 		} else {
57191eaf3e1SJohn Birrell 			if (copyout(str, fmt->dtfd_string, len) != 0) {
57291eaf3e1SJohn Birrell 				mutex_exit(&dtrace_lock);
57391eaf3e1SJohn Birrell 				return (EINVAL);
57491eaf3e1SJohn Birrell 			}
57591eaf3e1SJohn Birrell 		}
57691eaf3e1SJohn Birrell 
57791eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
57891eaf3e1SJohn Birrell 		return (0);
57991eaf3e1SJohn Birrell 	}
58091eaf3e1SJohn Birrell 	case DTRACEIOC_GO: {
58191eaf3e1SJohn Birrell 		int rval;
58291eaf3e1SJohn Birrell 		processorid_t *cpuid = (processorid_t *) addr;
58391eaf3e1SJohn Birrell 
58491eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_GO\n",__func__,__LINE__);
58591eaf3e1SJohn Birrell 
58691eaf3e1SJohn Birrell 		rval = dtrace_state_go(state, cpuid);
58791eaf3e1SJohn Birrell 
58891eaf3e1SJohn Birrell 		return (rval);
58991eaf3e1SJohn Birrell 	}
59091eaf3e1SJohn Birrell 	case DTRACEIOC_PROBEARG: {
59191eaf3e1SJohn Birrell 		dtrace_argdesc_t *desc = (dtrace_argdesc_t *) addr;
59291eaf3e1SJohn Birrell 		dtrace_probe_t *probe;
59391eaf3e1SJohn Birrell 		dtrace_provider_t *prov;
59491eaf3e1SJohn Birrell 
59591eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROBEARG\n",__func__,__LINE__);
59691eaf3e1SJohn Birrell 
59791eaf3e1SJohn Birrell 		if (desc->dtargd_id == DTRACE_IDNONE)
59891eaf3e1SJohn Birrell 			return (EINVAL);
59991eaf3e1SJohn Birrell 
60091eaf3e1SJohn Birrell 		if (desc->dtargd_ndx == DTRACE_ARGNONE)
60191eaf3e1SJohn Birrell 			return (EINVAL);
60291eaf3e1SJohn Birrell 
60391eaf3e1SJohn Birrell 		mutex_enter(&dtrace_provider_lock);
604bc96366cSSteven Hartland #ifdef illumos
60591eaf3e1SJohn Birrell 		mutex_enter(&mod_lock);
60646d27dbbSMark Johnston #endif
60791eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
60891eaf3e1SJohn Birrell 
60991eaf3e1SJohn Birrell 		if (desc->dtargd_id > dtrace_nprobes) {
61091eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
611bc96366cSSteven Hartland #ifdef illumos
61291eaf3e1SJohn Birrell 			mutex_exit(&mod_lock);
61346d27dbbSMark Johnston #endif
61491eaf3e1SJohn Birrell 			mutex_exit(&dtrace_provider_lock);
61591eaf3e1SJohn Birrell 			return (EINVAL);
61691eaf3e1SJohn Birrell 		}
61791eaf3e1SJohn Birrell 
61891eaf3e1SJohn Birrell 		if ((probe = dtrace_probes[desc->dtargd_id - 1]) == NULL) {
61991eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
620bc96366cSSteven Hartland #ifdef illumos
62191eaf3e1SJohn Birrell 			mutex_exit(&mod_lock);
62246d27dbbSMark Johnston #endif
62391eaf3e1SJohn Birrell 			mutex_exit(&dtrace_provider_lock);
62491eaf3e1SJohn Birrell 			return (EINVAL);
62591eaf3e1SJohn Birrell 		}
62691eaf3e1SJohn Birrell 
62791eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
62891eaf3e1SJohn Birrell 
62991eaf3e1SJohn Birrell 		prov = probe->dtpr_provider;
63091eaf3e1SJohn Birrell 
63191eaf3e1SJohn Birrell 		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
63291eaf3e1SJohn Birrell 			/*
63391eaf3e1SJohn Birrell 			 * There isn't any typed information for this probe.
63491eaf3e1SJohn Birrell 			 * Set the argument number to DTRACE_ARGNONE.
63591eaf3e1SJohn Birrell 			 */
63691eaf3e1SJohn Birrell 			desc->dtargd_ndx = DTRACE_ARGNONE;
63791eaf3e1SJohn Birrell 		} else {
63891eaf3e1SJohn Birrell 			desc->dtargd_native[0] = '\0';
63991eaf3e1SJohn Birrell 			desc->dtargd_xlate[0] = '\0';
64091eaf3e1SJohn Birrell 			desc->dtargd_mapping = desc->dtargd_ndx;
64191eaf3e1SJohn Birrell 
64291eaf3e1SJohn Birrell 			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
64391eaf3e1SJohn Birrell 			    probe->dtpr_id, probe->dtpr_arg, desc);
64491eaf3e1SJohn Birrell 		}
64591eaf3e1SJohn Birrell 
646bc96366cSSteven Hartland #ifdef illumos
64791eaf3e1SJohn Birrell 		mutex_exit(&mod_lock);
64846d27dbbSMark Johnston #endif
64991eaf3e1SJohn Birrell 		mutex_exit(&dtrace_provider_lock);
65091eaf3e1SJohn Birrell 
65191eaf3e1SJohn Birrell 		return (0);
65291eaf3e1SJohn Birrell 	}
65391eaf3e1SJohn Birrell 	case DTRACEIOC_PROBEMATCH:
65491eaf3e1SJohn Birrell 	case DTRACEIOC_PROBES: {
65591eaf3e1SJohn Birrell 		dtrace_probedesc_t *p_desc = (dtrace_probedesc_t *) addr;
65691eaf3e1SJohn Birrell 		dtrace_probe_t *probe = NULL;
65791eaf3e1SJohn Birrell 		dtrace_probekey_t pkey;
65891eaf3e1SJohn Birrell 		dtrace_id_t i;
65991eaf3e1SJohn Birrell 		int m = 0;
66091eaf3e1SJohn Birrell 		uint32_t priv = 0;
66191eaf3e1SJohn Birrell 		uid_t uid = 0;
66291eaf3e1SJohn Birrell 		zoneid_t zoneid = 0;
66391eaf3e1SJohn Birrell 
66491eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): %s\n",__func__,__LINE__,
66591eaf3e1SJohn Birrell 		    cmd == DTRACEIOC_PROBEMATCH ?
66691eaf3e1SJohn Birrell 		    "DTRACEIOC_PROBEMATCH":"DTRACEIOC_PROBES");
66791eaf3e1SJohn Birrell 
66891eaf3e1SJohn Birrell 		p_desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
66991eaf3e1SJohn Birrell 		p_desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
67091eaf3e1SJohn Birrell 		p_desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
67191eaf3e1SJohn Birrell 		p_desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
67291eaf3e1SJohn Birrell 
67391eaf3e1SJohn Birrell 		/*
67491eaf3e1SJohn Birrell 		 * Before we attempt to match this probe, we want to give
67591eaf3e1SJohn Birrell 		 * all providers the opportunity to provide it.
67691eaf3e1SJohn Birrell 		 */
67791eaf3e1SJohn Birrell 		if (p_desc->dtpd_id == DTRACE_IDNONE) {
67891eaf3e1SJohn Birrell 			mutex_enter(&dtrace_provider_lock);
67991eaf3e1SJohn Birrell 			dtrace_probe_provide(p_desc, NULL);
68091eaf3e1SJohn Birrell 			mutex_exit(&dtrace_provider_lock);
68191eaf3e1SJohn Birrell 			p_desc->dtpd_id++;
68291eaf3e1SJohn Birrell 		}
68391eaf3e1SJohn Birrell 
68491eaf3e1SJohn Birrell 		if (cmd == DTRACEIOC_PROBEMATCH)  {
68591eaf3e1SJohn Birrell 			dtrace_probekey(p_desc, &pkey);
68691eaf3e1SJohn Birrell 			pkey.dtpk_id = DTRACE_IDNONE;
68791eaf3e1SJohn Birrell 		}
68891eaf3e1SJohn Birrell 
68991eaf3e1SJohn Birrell 		dtrace_cred2priv(td->td_ucred, &priv, &uid, &zoneid);
69091eaf3e1SJohn Birrell 
69191eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
69291eaf3e1SJohn Birrell 
69391eaf3e1SJohn Birrell 		if (cmd == DTRACEIOC_PROBEMATCH) {
69491eaf3e1SJohn Birrell 			for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
69591eaf3e1SJohn Birrell 				if ((probe = dtrace_probes[i - 1]) != NULL &&
69691eaf3e1SJohn Birrell 				    (m = dtrace_match_probe(probe, &pkey,
69791eaf3e1SJohn Birrell 				    priv, uid, zoneid)) != 0)
69891eaf3e1SJohn Birrell 					break;
69991eaf3e1SJohn Birrell 			}
70091eaf3e1SJohn Birrell 
70191eaf3e1SJohn Birrell 			if (m < 0) {
70291eaf3e1SJohn Birrell 				mutex_exit(&dtrace_lock);
70391eaf3e1SJohn Birrell 				return (EINVAL);
70491eaf3e1SJohn Birrell 			}
70591eaf3e1SJohn Birrell 
70691eaf3e1SJohn Birrell 		} else {
70791eaf3e1SJohn Birrell 			for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
70891eaf3e1SJohn Birrell 				if ((probe = dtrace_probes[i - 1]) != NULL &&
70991eaf3e1SJohn Birrell 				    dtrace_match_priv(probe, priv, uid, zoneid))
71091eaf3e1SJohn Birrell 					break;
71191eaf3e1SJohn Birrell 			}
71291eaf3e1SJohn Birrell 		}
71391eaf3e1SJohn Birrell 
71491eaf3e1SJohn Birrell 		if (probe == NULL) {
71591eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
71691eaf3e1SJohn Birrell 			return (ESRCH);
71791eaf3e1SJohn Birrell 		}
71891eaf3e1SJohn Birrell 
71991eaf3e1SJohn Birrell 		dtrace_probe_description(probe, p_desc);
72091eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
72191eaf3e1SJohn Birrell 
72291eaf3e1SJohn Birrell 		return (0);
72391eaf3e1SJohn Birrell 	}
72491eaf3e1SJohn Birrell 	case DTRACEIOC_PROVIDER: {
72591eaf3e1SJohn Birrell 		dtrace_providerdesc_t *pvd = (dtrace_providerdesc_t *) addr;
72691eaf3e1SJohn Birrell 		dtrace_provider_t *pvp;
72791eaf3e1SJohn Birrell 
72891eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROVIDER\n",__func__,__LINE__);
72991eaf3e1SJohn Birrell 
73091eaf3e1SJohn Birrell 		pvd->dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
73191eaf3e1SJohn Birrell 		mutex_enter(&dtrace_provider_lock);
73291eaf3e1SJohn Birrell 
73391eaf3e1SJohn Birrell 		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
73491eaf3e1SJohn Birrell 			if (strcmp(pvp->dtpv_name, pvd->dtvd_name) == 0)
73591eaf3e1SJohn Birrell 				break;
73691eaf3e1SJohn Birrell 		}
73791eaf3e1SJohn Birrell 
73891eaf3e1SJohn Birrell 		mutex_exit(&dtrace_provider_lock);
73991eaf3e1SJohn Birrell 
74091eaf3e1SJohn Birrell 		if (pvp == NULL)
74191eaf3e1SJohn Birrell 			return (ESRCH);
74291eaf3e1SJohn Birrell 
74391eaf3e1SJohn Birrell 		bcopy(&pvp->dtpv_priv, &pvd->dtvd_priv, sizeof (dtrace_ppriv_t));
74491eaf3e1SJohn Birrell 		bcopy(&pvp->dtpv_attr, &pvd->dtvd_attr, sizeof (dtrace_pattr_t));
74591eaf3e1SJohn Birrell 
74691eaf3e1SJohn Birrell 		return (0);
74791eaf3e1SJohn Birrell 	}
74891eaf3e1SJohn Birrell 	case DTRACEIOC_REPLICATE: {
74991eaf3e1SJohn Birrell 		dtrace_repldesc_t *desc = (dtrace_repldesc_t *) addr;
75091eaf3e1SJohn Birrell 		dtrace_probedesc_t *match = &desc->dtrpd_match;
75191eaf3e1SJohn Birrell 		dtrace_probedesc_t *create = &desc->dtrpd_create;
75291eaf3e1SJohn Birrell 		int err;
75391eaf3e1SJohn Birrell 
75491eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_REPLICATE\n",__func__,__LINE__);
75591eaf3e1SJohn Birrell 
75691eaf3e1SJohn Birrell 		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
75791eaf3e1SJohn Birrell 		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
75891eaf3e1SJohn Birrell 		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
75991eaf3e1SJohn Birrell 		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
76091eaf3e1SJohn Birrell 
76191eaf3e1SJohn Birrell 		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
76291eaf3e1SJohn Birrell 		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
76391eaf3e1SJohn Birrell 		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
76491eaf3e1SJohn Birrell 		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
76591eaf3e1SJohn Birrell 
76691eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
76791eaf3e1SJohn Birrell 		err = dtrace_enabling_replicate(state, match, create);
76891eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
76991eaf3e1SJohn Birrell 
77091eaf3e1SJohn Birrell 		return (err);
77191eaf3e1SJohn Birrell 	}
77291eaf3e1SJohn Birrell 	case DTRACEIOC_STATUS: {
77391eaf3e1SJohn Birrell 		dtrace_status_t *stat = (dtrace_status_t *) addr;
77491eaf3e1SJohn Birrell 		dtrace_dstate_t *dstate;
77591eaf3e1SJohn Birrell 		int i, j;
77691eaf3e1SJohn Birrell 		uint64_t nerrs;
77791eaf3e1SJohn Birrell 
77891eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STATUS\n",__func__,__LINE__);
77991eaf3e1SJohn Birrell 
78091eaf3e1SJohn Birrell 		/*
78191eaf3e1SJohn Birrell 		 * See the comment in dtrace_state_deadman() for the reason
78291eaf3e1SJohn Birrell 		 * for setting dts_laststatus to INT64_MAX before setting
78391eaf3e1SJohn Birrell 		 * it to the correct value.
78491eaf3e1SJohn Birrell 		 */
78591eaf3e1SJohn Birrell 		state->dts_laststatus = INT64_MAX;
78691eaf3e1SJohn Birrell 		dtrace_membar_producer();
78791eaf3e1SJohn Birrell 		state->dts_laststatus = dtrace_gethrtime();
78891eaf3e1SJohn Birrell 
78991eaf3e1SJohn Birrell 		bzero(stat, sizeof (*stat));
79091eaf3e1SJohn Birrell 
79191eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
79291eaf3e1SJohn Birrell 
79391eaf3e1SJohn Birrell 		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
79491eaf3e1SJohn Birrell 			mutex_exit(&dtrace_lock);
79591eaf3e1SJohn Birrell 			return (ENOENT);
79691eaf3e1SJohn Birrell 		}
79791eaf3e1SJohn Birrell 
79891eaf3e1SJohn Birrell 		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
79991eaf3e1SJohn Birrell 			stat->dtst_exiting = 1;
80091eaf3e1SJohn Birrell 
80191eaf3e1SJohn Birrell 		nerrs = state->dts_errors;
80291eaf3e1SJohn Birrell 		dstate = &state->dts_vstate.dtvs_dynvars;
80391eaf3e1SJohn Birrell 
80491eaf3e1SJohn Birrell 		for (i = 0; i < NCPU; i++) {
805bc96366cSSteven Hartland #ifndef illumos
80691eaf3e1SJohn Birrell 			if (pcpu_find(i) == NULL)
80791eaf3e1SJohn Birrell 				continue;
80891eaf3e1SJohn Birrell #endif
80991eaf3e1SJohn Birrell 			dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
81091eaf3e1SJohn Birrell 
81191eaf3e1SJohn Birrell 			stat->dtst_dyndrops += dcpu->dtdsc_drops;
81291eaf3e1SJohn Birrell 			stat->dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
81391eaf3e1SJohn Birrell 			stat->dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
81491eaf3e1SJohn Birrell 
81591eaf3e1SJohn Birrell 			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
81691eaf3e1SJohn Birrell 				stat->dtst_filled++;
81791eaf3e1SJohn Birrell 
81891eaf3e1SJohn Birrell 			nerrs += state->dts_buffer[i].dtb_errors;
81991eaf3e1SJohn Birrell 
82091eaf3e1SJohn Birrell 			for (j = 0; j < state->dts_nspeculations; j++) {
82191eaf3e1SJohn Birrell 				dtrace_speculation_t *spec;
82291eaf3e1SJohn Birrell 				dtrace_buffer_t *buf;
82391eaf3e1SJohn Birrell 
82491eaf3e1SJohn Birrell 				spec = &state->dts_speculations[j];
82591eaf3e1SJohn Birrell 				buf = &spec->dtsp_buffer[i];
82691eaf3e1SJohn Birrell 				stat->dtst_specdrops += buf->dtb_xamot_drops;
82791eaf3e1SJohn Birrell 			}
82891eaf3e1SJohn Birrell 		}
82991eaf3e1SJohn Birrell 
83091eaf3e1SJohn Birrell 		stat->dtst_specdrops_busy = state->dts_speculations_busy;
83191eaf3e1SJohn Birrell 		stat->dtst_specdrops_unavail = state->dts_speculations_unavail;
83291eaf3e1SJohn Birrell 		stat->dtst_stkstroverflows = state->dts_stkstroverflows;
83391eaf3e1SJohn Birrell 		stat->dtst_dblerrors = state->dts_dblerrors;
83491eaf3e1SJohn Birrell 		stat->dtst_killed =
83591eaf3e1SJohn Birrell 		    (state->dts_activity == DTRACE_ACTIVITY_KILLED);
83691eaf3e1SJohn Birrell 		stat->dtst_errors = nerrs;
83791eaf3e1SJohn Birrell 
83891eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
83991eaf3e1SJohn Birrell 
84091eaf3e1SJohn Birrell 		return (0);
84191eaf3e1SJohn Birrell 	}
84291eaf3e1SJohn Birrell 	case DTRACEIOC_STOP: {
84391eaf3e1SJohn Birrell 		int rval;
84491eaf3e1SJohn Birrell 		processorid_t *cpuid = (processorid_t *) addr;
84591eaf3e1SJohn Birrell 
84691eaf3e1SJohn Birrell 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STOP\n",__func__,__LINE__);
84791eaf3e1SJohn Birrell 
84891eaf3e1SJohn Birrell 		mutex_enter(&dtrace_lock);
84991eaf3e1SJohn Birrell 		rval = dtrace_state_stop(state, cpuid);
85091eaf3e1SJohn Birrell 		mutex_exit(&dtrace_lock);
85191eaf3e1SJohn Birrell 
85291eaf3e1SJohn Birrell 		return (rval);
85391eaf3e1SJohn Birrell 	}
85491eaf3e1SJohn Birrell 	default:
85591eaf3e1SJohn Birrell 		error = ENOTTY;
85691eaf3e1SJohn Birrell 	}
85791eaf3e1SJohn Birrell 	return (error);
85891eaf3e1SJohn Birrell }
859