]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/cddl/dev/dtrace/dtrace_ioctl.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / cddl / dev / dtrace / dtrace_ioctl.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * $FreeBSD$
22  *
23  */
24
25 static int dtrace_verbose_ioctl;
26 SYSCTL_INT(_debug_dtrace, OID_AUTO, verbose_ioctl, CTLFLAG_RW, &dtrace_verbose_ioctl, 0, "");
27
28 #define DTRACE_IOCTL_PRINTF(fmt, ...)   if (dtrace_verbose_ioctl) printf(fmt, ## __VA_ARGS__ )
29
30 static int
31 dtrace_ioctl_helper(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
32     struct thread *td)
33 {
34         int rval;
35         dof_helper_t *dhp = NULL;
36         dof_hdr_t *dof = NULL;
37
38         switch (cmd) {
39         case DTRACEHIOC_ADDDOF:
40                 dhp = (dof_helper_t *)addr;
41                 /* XXX all because dofhp_dof is 64 bit */
42 #ifdef __i386
43                 addr = (caddr_t)(uint32_t)dhp->dofhp_dof;
44 #else
45                 addr = (caddr_t)dhp->dofhp_dof;
46 #endif
47                 /* FALLTHROUGH */
48         case DTRACEHIOC_ADD:
49                 dof = dtrace_dof_copyin((intptr_t)addr, &rval);
50
51                 if (dof == NULL)
52                         return (rval);
53
54                 mutex_enter(&dtrace_lock);
55                 if ((rval = dtrace_helper_slurp((dof_hdr_t *)dof, dhp)) != -1) {
56                         if (dhp) {
57                                 dhp->gen = rval;
58                                 copyout(dhp, addr, sizeof(*dhp));
59                         }
60                         rval = 0;
61                 } else {
62                         rval = EINVAL;
63                 }
64                 mutex_exit(&dtrace_lock);
65                 return (rval);
66         case DTRACEHIOC_REMOVE:
67                 mutex_enter(&dtrace_lock);
68                 rval = dtrace_helper_destroygen((int)*addr);
69                 mutex_exit(&dtrace_lock);
70
71                 return (rval);
72         default:
73                 break;
74         }
75
76         return (ENOTTY);
77 }
78
79 /* ARGSUSED */
80 static int
81 dtrace_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
82     int flags __unused, struct thread *td)
83 {
84 #if __FreeBSD_version < 800039
85         dtrace_state_t *state = dev->si_drv1;
86 #else
87         dtrace_state_t *state;
88         devfs_get_cdevpriv((void **) &state);
89 #endif
90         int error = 0;
91         if (state == NULL)
92                 return (EINVAL);
93
94         if (state->dts_anon) {
95                 ASSERT(dtrace_anon.dta_state == NULL);
96                 state = state->dts_anon;
97         }
98
99         switch (cmd) {
100         case DTRACEIOC_AGGDESC: {
101                 dtrace_aggdesc_t **paggdesc = (dtrace_aggdesc_t **) addr;
102                 dtrace_aggdesc_t aggdesc;
103                 dtrace_action_t *act;
104                 dtrace_aggregation_t *agg;
105                 int nrecs;
106                 uint32_t offs;
107                 dtrace_recdesc_t *lrec;
108                 void *buf;
109                 size_t size;
110                 uintptr_t dest;
111
112                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_AGGDESC\n",__func__,__LINE__);
113
114                 if (copyin((void *) *paggdesc, &aggdesc, sizeof (aggdesc)) != 0)
115                         return (EFAULT);
116
117                 mutex_enter(&dtrace_lock);
118
119                 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
120                         mutex_exit(&dtrace_lock);
121                         return (EINVAL);
122                 }
123
124                 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
125
126                 nrecs = aggdesc.dtagd_nrecs;
127                 aggdesc.dtagd_nrecs = 0;
128
129                 offs = agg->dtag_base;
130                 lrec = &agg->dtag_action.dta_rec;
131                 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
132
133                 for (act = agg->dtag_first; ; act = act->dta_next) {
134                         ASSERT(act->dta_intuple ||
135                             DTRACEACT_ISAGG(act->dta_kind));
136
137                         /*
138                          * If this action has a record size of zero, it
139                          * denotes an argument to the aggregating action.
140                          * Because the presence of this record doesn't (or
141                          * shouldn't) affect the way the data is interpreted,
142                          * we don't copy it out to save user-level the
143                          * confusion of dealing with a zero-length record.
144                          */
145                         if (act->dta_rec.dtrd_size == 0) {
146                                 ASSERT(agg->dtag_hasarg);
147                                 continue;
148                         }
149
150                         aggdesc.dtagd_nrecs++;
151
152                         if (act == &agg->dtag_action)
153                                 break;
154                 }
155
156                 /*
157                  * Now that we have the size, we need to allocate a temporary
158                  * buffer in which to store the complete description.  We need
159                  * the temporary buffer to be able to drop dtrace_lock()
160                  * across the copyout(), below.
161                  */
162                 size = sizeof (dtrace_aggdesc_t) +
163                     (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
164
165                 buf = kmem_alloc(size, KM_SLEEP);
166                 dest = (uintptr_t)buf;
167
168                 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
169                 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
170
171                 for (act = agg->dtag_first; ; act = act->dta_next) {
172                         dtrace_recdesc_t rec = act->dta_rec;
173
174                         /*
175                          * See the comment in the above loop for why we pass
176                          * over zero-length records.
177                          */
178                         if (rec.dtrd_size == 0) {
179                                 ASSERT(agg->dtag_hasarg);
180                                 continue;
181                         }
182
183                         if (nrecs-- == 0)
184                                 break;
185
186                         rec.dtrd_offset -= offs;
187                         bcopy(&rec, (void *)dest, sizeof (rec));
188                         dest += sizeof (dtrace_recdesc_t);
189
190                         if (act == &agg->dtag_action)
191                                 break;
192                 }
193
194                 mutex_exit(&dtrace_lock);
195
196                 if (copyout(buf, (void *) *paggdesc, dest - (uintptr_t)buf) != 0) {
197                         kmem_free(buf, size);
198                         return (EFAULT);
199                 }
200
201                 kmem_free(buf, size);
202                 return (0);
203         }
204         case DTRACEIOC_AGGSNAP:
205         case DTRACEIOC_BUFSNAP: {
206                 dtrace_bufdesc_t **pdesc = (dtrace_bufdesc_t **) addr;
207                 dtrace_bufdesc_t desc;
208                 caddr_t cached;
209                 dtrace_buffer_t *buf;
210
211                 dtrace_debug_output();
212
213                 if (copyin((void *) *pdesc, &desc, sizeof (desc)) != 0)
214                         return (EFAULT);
215
216                 DTRACE_IOCTL_PRINTF("%s(%d): %s curcpu %d cpu %d\n",
217                     __func__,__LINE__,
218                     cmd == DTRACEIOC_AGGSNAP ?
219                     "DTRACEIOC_AGGSNAP":"DTRACEIOC_BUFSNAP",
220                     curcpu, desc.dtbd_cpu);
221
222                 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
223                         return (ENOENT);
224                 if (pcpu_find(desc.dtbd_cpu) == NULL)
225                         return (ENOENT);
226
227                 mutex_enter(&dtrace_lock);
228
229                 if (cmd == DTRACEIOC_BUFSNAP) {
230                         buf = &state->dts_buffer[desc.dtbd_cpu];
231                 } else {
232                         buf = &state->dts_aggbuffer[desc.dtbd_cpu];
233                 }
234
235                 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
236                         size_t sz = buf->dtb_offset;
237
238                         if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
239                                 mutex_exit(&dtrace_lock);
240                                 return (EBUSY);
241                         }
242
243                         /*
244                          * If this buffer has already been consumed, we're
245                          * going to indicate that there's nothing left here
246                          * to consume.
247                          */
248                         if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
249                                 mutex_exit(&dtrace_lock);
250
251                                 desc.dtbd_size = 0;
252                                 desc.dtbd_drops = 0;
253                                 desc.dtbd_errors = 0;
254                                 desc.dtbd_oldest = 0;
255                                 sz = sizeof (desc);
256
257                                 if (copyout(&desc, (void *) *pdesc, sz) != 0)
258                                         return (EFAULT);
259
260                                 return (0);
261                         }
262
263                         /*
264                          * If this is a ring buffer that has wrapped, we want
265                          * to copy the whole thing out.
266                          */
267                         if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
268                                 dtrace_buffer_polish(buf);
269                                 sz = buf->dtb_size;
270                         }
271
272                         if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
273                                 mutex_exit(&dtrace_lock);
274                                 return (EFAULT);
275                         }
276
277                         desc.dtbd_size = sz;
278                         desc.dtbd_drops = buf->dtb_drops;
279                         desc.dtbd_errors = buf->dtb_errors;
280                         desc.dtbd_oldest = buf->dtb_xamot_offset;
281
282                         mutex_exit(&dtrace_lock);
283
284                         if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
285                                 return (EFAULT);
286
287                         buf->dtb_flags |= DTRACEBUF_CONSUMED;
288
289                         return (0);
290                 }
291
292                 if (buf->dtb_tomax == NULL) {
293                         ASSERT(buf->dtb_xamot == NULL);
294                         mutex_exit(&dtrace_lock);
295                         return (ENOENT);
296                 }
297
298                 cached = buf->dtb_tomax;
299                 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
300
301                 dtrace_xcall(desc.dtbd_cpu,
302                     (dtrace_xcall_t)dtrace_buffer_switch, buf);
303
304                 state->dts_errors += buf->dtb_xamot_errors;
305
306                 /*
307                  * If the buffers did not actually switch, then the cross call
308                  * did not take place -- presumably because the given CPU is
309                  * not in the ready set.  If this is the case, we'll return
310                  * ENOENT.
311                  */
312                 if (buf->dtb_tomax == cached) {
313                         ASSERT(buf->dtb_xamot != cached);
314                         mutex_exit(&dtrace_lock);
315                         return (ENOENT);
316                 }
317
318                 ASSERT(cached == buf->dtb_xamot);
319
320                 DTRACE_IOCTL_PRINTF("%s(%d): copyout the buffer snapshot\n",__func__,__LINE__);
321
322                 /*
323                  * We have our snapshot; now copy it out.
324                  */
325                 if (copyout(buf->dtb_xamot, desc.dtbd_data,
326                     buf->dtb_xamot_offset) != 0) {
327                         mutex_exit(&dtrace_lock);
328                         return (EFAULT);
329                 }
330
331                 desc.dtbd_size = buf->dtb_xamot_offset;
332                 desc.dtbd_drops = buf->dtb_xamot_drops;
333                 desc.dtbd_errors = buf->dtb_xamot_errors;
334                 desc.dtbd_oldest = 0;
335
336                 mutex_exit(&dtrace_lock);
337
338                 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);
339
340                 /*
341                  * Finally, copy out the buffer description.
342                  */
343                 if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
344                         return (EFAULT);
345
346                 return (0);
347         }
348         case DTRACEIOC_CONF: {
349                 dtrace_conf_t conf;
350
351                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_CONF\n",__func__,__LINE__);
352
353                 bzero(&conf, sizeof (conf));
354                 conf.dtc_difversion = DIF_VERSION;
355                 conf.dtc_difintregs = DIF_DIR_NREGS;
356                 conf.dtc_diftupregs = DIF_DTR_NREGS;
357                 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
358
359                 *((dtrace_conf_t *) addr) = conf;
360
361                 return (0);
362         }
363         case DTRACEIOC_DOFGET: {
364                 dof_hdr_t **pdof = (dof_hdr_t **) addr;
365                 dof_hdr_t hdr, *dof = *pdof;
366                 int rval;
367                 uint64_t len;
368
369                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_DOFGET\n",__func__,__LINE__);
370
371                 if (copyin((void *)dof, &hdr, sizeof (hdr)) != 0)
372                         return (EFAULT);
373
374                 mutex_enter(&dtrace_lock);
375                 dof = dtrace_dof_create(state);
376                 mutex_exit(&dtrace_lock);
377
378                 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
379                 rval = copyout(dof, (void *) *pdof, len);
380                 dtrace_dof_destroy(dof);
381
382                 return (rval == 0 ? 0 : EFAULT);
383         }
384         case DTRACEIOC_ENABLE: {
385                 dof_hdr_t *dof = NULL;
386                 dtrace_enabling_t *enab = NULL;
387                 dtrace_vstate_t *vstate;
388                 int err = 0;
389                 int rval;
390                 dtrace_enable_io_t *p = (dtrace_enable_io_t *) addr;
391
392                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_ENABLE\n",__func__,__LINE__);
393
394                 /*
395                  * If a NULL argument has been passed, we take this as our
396                  * cue to reevaluate our enablings.
397                  */
398                 if (p->dof == NULL) {
399                         dtrace_enabling_matchall();
400
401                         return (0);
402                 }
403
404                 if ((dof = dtrace_dof_copyin((uintptr_t) p->dof, &rval)) == NULL)
405                         return (EINVAL);
406
407                 mutex_enter(&cpu_lock);
408                 mutex_enter(&dtrace_lock);
409                 vstate = &state->dts_vstate;
410
411                 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
412                         mutex_exit(&dtrace_lock);
413                         mutex_exit(&cpu_lock);
414                         dtrace_dof_destroy(dof);
415                         return (EBUSY);
416                 }
417
418                 if (dtrace_dof_slurp(dof, vstate, td->td_ucred, &enab, 0, B_TRUE) != 0) {
419                         mutex_exit(&dtrace_lock);
420                         mutex_exit(&cpu_lock);
421                         dtrace_dof_destroy(dof);
422                         return (EINVAL);
423                 }
424
425                 if ((rval = dtrace_dof_options(dof, state)) != 0) {
426                         dtrace_enabling_destroy(enab);
427                         mutex_exit(&dtrace_lock);
428                         mutex_exit(&cpu_lock);
429                         dtrace_dof_destroy(dof);
430                         return (rval);
431                 }
432
433                 if ((err = dtrace_enabling_match(enab, &p->n_matched)) == 0) {
434                         err = dtrace_enabling_retain(enab);
435                 } else {
436                         dtrace_enabling_destroy(enab);
437                 }
438
439                 mutex_exit(&cpu_lock);
440                 mutex_exit(&dtrace_lock);
441                 dtrace_dof_destroy(dof);
442
443                 return (err);
444         }
445         case DTRACEIOC_EPROBE: {
446                 dtrace_eprobedesc_t **pepdesc = (dtrace_eprobedesc_t **) addr;
447                 dtrace_eprobedesc_t epdesc;
448                 dtrace_ecb_t *ecb;
449                 dtrace_action_t *act;
450                 void *buf;
451                 size_t size;
452                 uintptr_t dest;
453                 int nrecs;
454
455                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_EPROBE\n",__func__,__LINE__);
456
457                 if (copyin((void *)*pepdesc, &epdesc, sizeof (epdesc)) != 0)
458                         return (EFAULT);
459
460                 mutex_enter(&dtrace_lock);
461
462                 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
463                         mutex_exit(&dtrace_lock);
464                         return (EINVAL);
465                 }
466
467                 if (ecb->dte_probe == NULL) {
468                         mutex_exit(&dtrace_lock);
469                         return (EINVAL);
470                 }
471
472                 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
473                 epdesc.dtepd_uarg = ecb->dte_uarg;
474                 epdesc.dtepd_size = ecb->dte_size;
475
476                 nrecs = epdesc.dtepd_nrecs;
477                 epdesc.dtepd_nrecs = 0;
478                 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
479                         if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
480                                 continue;
481
482                         epdesc.dtepd_nrecs++;
483                 }
484
485                 /*
486                  * Now that we have the size, we need to allocate a temporary
487                  * buffer in which to store the complete description.  We need
488                  * the temporary buffer to be able to drop dtrace_lock()
489                  * across the copyout(), below.
490                  */
491                 size = sizeof (dtrace_eprobedesc_t) +
492                     (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
493
494                 buf = kmem_alloc(size, KM_SLEEP);
495                 dest = (uintptr_t)buf;
496
497                 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
498                 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
499
500                 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
501                         if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
502                                 continue;
503
504                         if (nrecs-- == 0)
505                                 break;
506
507                         bcopy(&act->dta_rec, (void *)dest,
508                             sizeof (dtrace_recdesc_t));
509                         dest += sizeof (dtrace_recdesc_t);
510                 }
511
512                 mutex_exit(&dtrace_lock);
513
514                 if (copyout(buf, (void *) *pepdesc, dest - (uintptr_t)buf) != 0) {
515                         kmem_free(buf, size);
516                         return (EFAULT);
517                 }
518
519                 kmem_free(buf, size);
520                 return (0);
521         }
522         case DTRACEIOC_FORMAT: {
523                 dtrace_fmtdesc_t *fmt = (dtrace_fmtdesc_t *) addr;
524                 char *str;
525                 int len;
526
527                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_FORMAT\n",__func__,__LINE__);
528
529                 mutex_enter(&dtrace_lock);
530
531                 if (fmt->dtfd_format == 0 ||
532                     fmt->dtfd_format > state->dts_nformats) {
533                         mutex_exit(&dtrace_lock);
534                         return (EINVAL);
535                 }
536
537                 /*
538                  * Format strings are allocated contiguously and they are
539                  * never freed; if a format index is less than the number
540                  * of formats, we can assert that the format map is non-NULL
541                  * and that the format for the specified index is non-NULL.
542                  */
543                 ASSERT(state->dts_formats != NULL);
544                 str = state->dts_formats[fmt->dtfd_format - 1];
545                 ASSERT(str != NULL);
546
547                 len = strlen(str) + 1;
548
549                 if (len > fmt->dtfd_length) {
550                         fmt->dtfd_length = len;
551                 } else {
552                         if (copyout(str, fmt->dtfd_string, len) != 0) {
553                                 mutex_exit(&dtrace_lock);
554                                 return (EINVAL);
555                         }
556                 }
557
558                 mutex_exit(&dtrace_lock);
559                 return (0);
560         }
561         case DTRACEIOC_GO: {
562                 int rval;
563                 processorid_t *cpuid = (processorid_t *) addr;
564
565                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_GO\n",__func__,__LINE__);
566
567                 rval = dtrace_state_go(state, cpuid);
568
569                 return (rval);
570         }
571         case DTRACEIOC_PROBEARG: {
572                 dtrace_argdesc_t *desc = (dtrace_argdesc_t *) addr;
573                 dtrace_probe_t *probe;
574                 dtrace_provider_t *prov;
575
576                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROBEARG\n",__func__,__LINE__);
577
578                 if (desc->dtargd_id == DTRACE_IDNONE)
579                         return (EINVAL);
580
581                 if (desc->dtargd_ndx == DTRACE_ARGNONE)
582                         return (EINVAL);
583
584                 mutex_enter(&dtrace_provider_lock);
585                 mutex_enter(&mod_lock);
586                 mutex_enter(&dtrace_lock);
587
588                 if (desc->dtargd_id > dtrace_nprobes) {
589                         mutex_exit(&dtrace_lock);
590                         mutex_exit(&mod_lock);
591                         mutex_exit(&dtrace_provider_lock);
592                         return (EINVAL);
593                 }
594
595                 if ((probe = dtrace_probes[desc->dtargd_id - 1]) == NULL) {
596                         mutex_exit(&dtrace_lock);
597                         mutex_exit(&mod_lock);
598                         mutex_exit(&dtrace_provider_lock);
599                         return (EINVAL);
600                 }
601
602                 mutex_exit(&dtrace_lock);
603
604                 prov = probe->dtpr_provider;
605
606                 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
607                         /*
608                          * There isn't any typed information for this probe.
609                          * Set the argument number to DTRACE_ARGNONE.
610                          */
611                         desc->dtargd_ndx = DTRACE_ARGNONE;
612                 } else {
613                         desc->dtargd_native[0] = '\0';
614                         desc->dtargd_xlate[0] = '\0';
615                         desc->dtargd_mapping = desc->dtargd_ndx;
616
617                         prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
618                             probe->dtpr_id, probe->dtpr_arg, desc);
619                 }
620
621                 mutex_exit(&mod_lock);
622                 mutex_exit(&dtrace_provider_lock);
623
624                 return (0);
625         }
626         case DTRACEIOC_PROBEMATCH:
627         case DTRACEIOC_PROBES: {
628                 dtrace_probedesc_t *p_desc = (dtrace_probedesc_t *) addr;
629                 dtrace_probe_t *probe = NULL;
630                 dtrace_probekey_t pkey;
631                 dtrace_id_t i;
632                 int m = 0;
633                 uint32_t priv = 0;
634                 uid_t uid = 0;
635                 zoneid_t zoneid = 0;
636
637                 DTRACE_IOCTL_PRINTF("%s(%d): %s\n",__func__,__LINE__,
638                     cmd == DTRACEIOC_PROBEMATCH ?
639                     "DTRACEIOC_PROBEMATCH":"DTRACEIOC_PROBES");
640
641                 p_desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
642                 p_desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
643                 p_desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
644                 p_desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
645
646                 /*
647                  * Before we attempt to match this probe, we want to give
648                  * all providers the opportunity to provide it.
649                  */
650                 if (p_desc->dtpd_id == DTRACE_IDNONE) {
651                         mutex_enter(&dtrace_provider_lock);
652                         dtrace_probe_provide(p_desc, NULL);
653                         mutex_exit(&dtrace_provider_lock);
654                         p_desc->dtpd_id++;
655                 }
656
657                 if (cmd == DTRACEIOC_PROBEMATCH)  {
658                         dtrace_probekey(p_desc, &pkey);
659                         pkey.dtpk_id = DTRACE_IDNONE;
660                 }
661
662                 dtrace_cred2priv(td->td_ucred, &priv, &uid, &zoneid);
663
664                 mutex_enter(&dtrace_lock);
665
666                 if (cmd == DTRACEIOC_PROBEMATCH) {
667                         for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
668                                 if ((probe = dtrace_probes[i - 1]) != NULL &&
669                                     (m = dtrace_match_probe(probe, &pkey,
670                                     priv, uid, zoneid)) != 0)
671                                         break;
672                         }
673
674                         if (m < 0) {
675                                 mutex_exit(&dtrace_lock);
676                                 return (EINVAL);
677                         }
678
679                 } else {
680                         for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
681                                 if ((probe = dtrace_probes[i - 1]) != NULL &&
682                                     dtrace_match_priv(probe, priv, uid, zoneid))
683                                         break;
684                         }
685                 }
686
687                 if (probe == NULL) {
688                         mutex_exit(&dtrace_lock);
689                         return (ESRCH);
690                 }
691
692                 dtrace_probe_description(probe, p_desc);
693                 mutex_exit(&dtrace_lock);
694
695                 return (0);
696         }
697         case DTRACEIOC_PROVIDER: {
698                 dtrace_providerdesc_t *pvd = (dtrace_providerdesc_t *) addr;
699                 dtrace_provider_t *pvp;
700
701                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROVIDER\n",__func__,__LINE__);
702
703                 pvd->dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
704                 mutex_enter(&dtrace_provider_lock);
705
706                 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
707                         if (strcmp(pvp->dtpv_name, pvd->dtvd_name) == 0)
708                                 break;
709                 }
710
711                 mutex_exit(&dtrace_provider_lock);
712
713                 if (pvp == NULL)
714                         return (ESRCH);
715
716                 bcopy(&pvp->dtpv_priv, &pvd->dtvd_priv, sizeof (dtrace_ppriv_t));
717                 bcopy(&pvp->dtpv_attr, &pvd->dtvd_attr, sizeof (dtrace_pattr_t));
718
719                 return (0);
720         }
721         case DTRACEIOC_REPLICATE: {
722                 dtrace_repldesc_t *desc = (dtrace_repldesc_t *) addr;
723                 dtrace_probedesc_t *match = &desc->dtrpd_match;
724                 dtrace_probedesc_t *create = &desc->dtrpd_create;
725                 int err;
726
727                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_REPLICATE\n",__func__,__LINE__);
728
729                 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
730                 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
731                 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
732                 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
733
734                 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
735                 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
736                 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
737                 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
738
739                 mutex_enter(&dtrace_lock);
740                 err = dtrace_enabling_replicate(state, match, create);
741                 mutex_exit(&dtrace_lock);
742
743                 return (err);
744         }
745         case DTRACEIOC_STATUS: {
746                 dtrace_status_t *stat = (dtrace_status_t *) addr;
747                 dtrace_dstate_t *dstate;
748                 int i, j;
749                 uint64_t nerrs;
750
751                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STATUS\n",__func__,__LINE__);
752
753                 /*
754                  * See the comment in dtrace_state_deadman() for the reason
755                  * for setting dts_laststatus to INT64_MAX before setting
756                  * it to the correct value.
757                  */
758                 state->dts_laststatus = INT64_MAX;
759                 dtrace_membar_producer();
760                 state->dts_laststatus = dtrace_gethrtime();
761
762                 bzero(stat, sizeof (*stat));
763
764                 mutex_enter(&dtrace_lock);
765
766                 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
767                         mutex_exit(&dtrace_lock);
768                         return (ENOENT);
769                 }
770
771                 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
772                         stat->dtst_exiting = 1;
773
774                 nerrs = state->dts_errors;
775                 dstate = &state->dts_vstate.dtvs_dynvars;
776
777                 for (i = 0; i < NCPU; i++) {
778 #if !defined(sun)
779                         if (pcpu_find(i) == NULL)
780                                 continue;
781 #endif
782                         dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
783
784                         stat->dtst_dyndrops += dcpu->dtdsc_drops;
785                         stat->dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
786                         stat->dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
787
788                         if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
789                                 stat->dtst_filled++;
790
791                         nerrs += state->dts_buffer[i].dtb_errors;
792
793                         for (j = 0; j < state->dts_nspeculations; j++) {
794                                 dtrace_speculation_t *spec;
795                                 dtrace_buffer_t *buf;
796
797                                 spec = &state->dts_speculations[j];
798                                 buf = &spec->dtsp_buffer[i];
799                                 stat->dtst_specdrops += buf->dtb_xamot_drops;
800                         }
801                 }
802
803                 stat->dtst_specdrops_busy = state->dts_speculations_busy;
804                 stat->dtst_specdrops_unavail = state->dts_speculations_unavail;
805                 stat->dtst_stkstroverflows = state->dts_stkstroverflows;
806                 stat->dtst_dblerrors = state->dts_dblerrors;
807                 stat->dtst_killed =
808                     (state->dts_activity == DTRACE_ACTIVITY_KILLED);
809                 stat->dtst_errors = nerrs;
810
811                 mutex_exit(&dtrace_lock);
812
813                 return (0);
814         }
815         case DTRACEIOC_STOP: {
816                 int rval;
817                 processorid_t *cpuid = (processorid_t *) addr;
818
819                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STOP\n",__func__,__LINE__);
820
821                 mutex_enter(&dtrace_lock);
822                 rval = dtrace_state_stop(state, cpuid);
823                 mutex_exit(&dtrace_lock);
824
825                 return (rval);
826         }
827         default:
828                 error = ENOTTY;
829         }
830         return (error);
831 }