]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cddl/dev/dtrace/dtrace_ioctl.c
MFC r250574, r250812, r253725;
[FreeBSD/stable/9.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                         desc.dtbd_timestamp = dtrace_gethrtime();
282
283                         mutex_exit(&dtrace_lock);
284
285                         if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
286                                 return (EFAULT);
287
288                         buf->dtb_flags |= DTRACEBUF_CONSUMED;
289
290                         return (0);
291                 }
292
293                 if (buf->dtb_tomax == NULL) {
294                         ASSERT(buf->dtb_xamot == NULL);
295                         mutex_exit(&dtrace_lock);
296                         return (ENOENT);
297                 }
298
299                 cached = buf->dtb_tomax;
300                 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
301
302                 dtrace_xcall(desc.dtbd_cpu,
303                     (dtrace_xcall_t)dtrace_buffer_switch, buf);
304
305                 state->dts_errors += buf->dtb_xamot_errors;
306
307                 /*
308                  * If the buffers did not actually switch, then the cross call
309                  * did not take place -- presumably because the given CPU is
310                  * not in the ready set.  If this is the case, we'll return
311                  * ENOENT.
312                  */
313                 if (buf->dtb_tomax == cached) {
314                         ASSERT(buf->dtb_xamot != cached);
315                         mutex_exit(&dtrace_lock);
316                         return (ENOENT);
317                 }
318
319                 ASSERT(cached == buf->dtb_xamot);
320
321                 DTRACE_IOCTL_PRINTF("%s(%d): copyout the buffer snapshot\n",__func__,__LINE__);
322
323                 /*
324                  * We have our snapshot; now copy it out.
325                  */
326                 if (copyout(buf->dtb_xamot, desc.dtbd_data,
327                     buf->dtb_xamot_offset) != 0) {
328                         mutex_exit(&dtrace_lock);
329                         return (EFAULT);
330                 }
331
332                 desc.dtbd_size = buf->dtb_xamot_offset;
333                 desc.dtbd_drops = buf->dtb_xamot_drops;
334                 desc.dtbd_errors = buf->dtb_xamot_errors;
335                 desc.dtbd_oldest = 0;
336                 desc.dtbd_timestamp = buf->dtb_switched;
337
338                 mutex_exit(&dtrace_lock);
339
340                 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);
341
342                 /*
343                  * Finally, copy out the buffer description.
344                  */
345                 if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
346                         return (EFAULT);
347
348                 return (0);
349         }
350         case DTRACEIOC_CONF: {
351                 dtrace_conf_t conf;
352
353                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_CONF\n",__func__,__LINE__);
354
355                 bzero(&conf, sizeof (conf));
356                 conf.dtc_difversion = DIF_VERSION;
357                 conf.dtc_difintregs = DIF_DIR_NREGS;
358                 conf.dtc_diftupregs = DIF_DTR_NREGS;
359                 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
360
361                 *((dtrace_conf_t *) addr) = conf;
362
363                 return (0);
364         }
365         case DTRACEIOC_DOFGET: {
366                 dof_hdr_t **pdof = (dof_hdr_t **) addr;
367                 dof_hdr_t hdr, *dof = *pdof;
368                 int rval;
369                 uint64_t len;
370
371                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_DOFGET\n",__func__,__LINE__);
372
373                 if (copyin((void *)dof, &hdr, sizeof (hdr)) != 0)
374                         return (EFAULT);
375
376                 mutex_enter(&dtrace_lock);
377                 dof = dtrace_dof_create(state);
378                 mutex_exit(&dtrace_lock);
379
380                 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
381                 rval = copyout(dof, (void *) *pdof, len);
382                 dtrace_dof_destroy(dof);
383
384                 return (rval == 0 ? 0 : EFAULT);
385         }
386         case DTRACEIOC_ENABLE: {
387                 dof_hdr_t *dof = NULL;
388                 dtrace_enabling_t *enab = NULL;
389                 dtrace_vstate_t *vstate;
390                 int err = 0;
391                 int rval;
392                 dtrace_enable_io_t *p = (dtrace_enable_io_t *) addr;
393
394                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_ENABLE\n",__func__,__LINE__);
395
396                 /*
397                  * If a NULL argument has been passed, we take this as our
398                  * cue to reevaluate our enablings.
399                  */
400                 if (p->dof == NULL) {
401                         dtrace_enabling_matchall();
402
403                         return (0);
404                 }
405
406                 if ((dof = dtrace_dof_copyin((uintptr_t) p->dof, &rval)) == NULL)
407                         return (EINVAL);
408
409                 mutex_enter(&cpu_lock);
410                 mutex_enter(&dtrace_lock);
411                 vstate = &state->dts_vstate;
412
413                 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
414                         mutex_exit(&dtrace_lock);
415                         mutex_exit(&cpu_lock);
416                         dtrace_dof_destroy(dof);
417                         return (EBUSY);
418                 }
419
420                 if (dtrace_dof_slurp(dof, vstate, td->td_ucred, &enab, 0, B_TRUE) != 0) {
421                         mutex_exit(&dtrace_lock);
422                         mutex_exit(&cpu_lock);
423                         dtrace_dof_destroy(dof);
424                         return (EINVAL);
425                 }
426
427                 if ((rval = dtrace_dof_options(dof, state)) != 0) {
428                         dtrace_enabling_destroy(enab);
429                         mutex_exit(&dtrace_lock);
430                         mutex_exit(&cpu_lock);
431                         dtrace_dof_destroy(dof);
432                         return (rval);
433                 }
434
435                 if ((err = dtrace_enabling_match(enab, &p->n_matched)) == 0) {
436                         err = dtrace_enabling_retain(enab);
437                 } else {
438                         dtrace_enabling_destroy(enab);
439                 }
440
441                 mutex_exit(&cpu_lock);
442                 mutex_exit(&dtrace_lock);
443                 dtrace_dof_destroy(dof);
444
445                 return (err);
446         }
447         case DTRACEIOC_EPROBE: {
448                 dtrace_eprobedesc_t **pepdesc = (dtrace_eprobedesc_t **) addr;
449                 dtrace_eprobedesc_t epdesc;
450                 dtrace_ecb_t *ecb;
451                 dtrace_action_t *act;
452                 void *buf;
453                 size_t size;
454                 uintptr_t dest;
455                 int nrecs;
456
457                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_EPROBE\n",__func__,__LINE__);
458
459                 if (copyin((void *)*pepdesc, &epdesc, sizeof (epdesc)) != 0)
460                         return (EFAULT);
461
462                 mutex_enter(&dtrace_lock);
463
464                 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
465                         mutex_exit(&dtrace_lock);
466                         return (EINVAL);
467                 }
468
469                 if (ecb->dte_probe == NULL) {
470                         mutex_exit(&dtrace_lock);
471                         return (EINVAL);
472                 }
473
474                 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
475                 epdesc.dtepd_uarg = ecb->dte_uarg;
476                 epdesc.dtepd_size = ecb->dte_size;
477
478                 nrecs = epdesc.dtepd_nrecs;
479                 epdesc.dtepd_nrecs = 0;
480                 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
481                         if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
482                                 continue;
483
484                         epdesc.dtepd_nrecs++;
485                 }
486
487                 /*
488                  * Now that we have the size, we need to allocate a temporary
489                  * buffer in which to store the complete description.  We need
490                  * the temporary buffer to be able to drop dtrace_lock()
491                  * across the copyout(), below.
492                  */
493                 size = sizeof (dtrace_eprobedesc_t) +
494                     (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
495
496                 buf = kmem_alloc(size, KM_SLEEP);
497                 dest = (uintptr_t)buf;
498
499                 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
500                 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
501
502                 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
503                         if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
504                                 continue;
505
506                         if (nrecs-- == 0)
507                                 break;
508
509                         bcopy(&act->dta_rec, (void *)dest,
510                             sizeof (dtrace_recdesc_t));
511                         dest += sizeof (dtrace_recdesc_t);
512                 }
513
514                 mutex_exit(&dtrace_lock);
515
516                 if (copyout(buf, (void *) *pepdesc, dest - (uintptr_t)buf) != 0) {
517                         kmem_free(buf, size);
518                         return (EFAULT);
519                 }
520
521                 kmem_free(buf, size);
522                 return (0);
523         }
524         case DTRACEIOC_FORMAT: {
525                 dtrace_fmtdesc_t *fmt = (dtrace_fmtdesc_t *) addr;
526                 char *str;
527                 int len;
528
529                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_FORMAT\n",__func__,__LINE__);
530
531                 mutex_enter(&dtrace_lock);
532
533                 if (fmt->dtfd_format == 0 ||
534                     fmt->dtfd_format > state->dts_nformats) {
535                         mutex_exit(&dtrace_lock);
536                         return (EINVAL);
537                 }
538
539                 /*
540                  * Format strings are allocated contiguously and they are
541                  * never freed; if a format index is less than the number
542                  * of formats, we can assert that the format map is non-NULL
543                  * and that the format for the specified index is non-NULL.
544                  */
545                 ASSERT(state->dts_formats != NULL);
546                 str = state->dts_formats[fmt->dtfd_format - 1];
547                 ASSERT(str != NULL);
548
549                 len = strlen(str) + 1;
550
551                 if (len > fmt->dtfd_length) {
552                         fmt->dtfd_length = len;
553                 } else {
554                         if (copyout(str, fmt->dtfd_string, len) != 0) {
555                                 mutex_exit(&dtrace_lock);
556                                 return (EINVAL);
557                         }
558                 }
559
560                 mutex_exit(&dtrace_lock);
561                 return (0);
562         }
563         case DTRACEIOC_GO: {
564                 int rval;
565                 processorid_t *cpuid = (processorid_t *) addr;
566
567                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_GO\n",__func__,__LINE__);
568
569                 rval = dtrace_state_go(state, cpuid);
570
571                 return (rval);
572         }
573         case DTRACEIOC_PROBEARG: {
574                 dtrace_argdesc_t *desc = (dtrace_argdesc_t *) addr;
575                 dtrace_probe_t *probe;
576                 dtrace_provider_t *prov;
577
578                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROBEARG\n",__func__,__LINE__);
579
580                 if (desc->dtargd_id == DTRACE_IDNONE)
581                         return (EINVAL);
582
583                 if (desc->dtargd_ndx == DTRACE_ARGNONE)
584                         return (EINVAL);
585
586                 mutex_enter(&dtrace_provider_lock);
587                 mutex_enter(&mod_lock);
588                 mutex_enter(&dtrace_lock);
589
590                 if (desc->dtargd_id > dtrace_nprobes) {
591                         mutex_exit(&dtrace_lock);
592                         mutex_exit(&mod_lock);
593                         mutex_exit(&dtrace_provider_lock);
594                         return (EINVAL);
595                 }
596
597                 if ((probe = dtrace_probes[desc->dtargd_id - 1]) == NULL) {
598                         mutex_exit(&dtrace_lock);
599                         mutex_exit(&mod_lock);
600                         mutex_exit(&dtrace_provider_lock);
601                         return (EINVAL);
602                 }
603
604                 mutex_exit(&dtrace_lock);
605
606                 prov = probe->dtpr_provider;
607
608                 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
609                         /*
610                          * There isn't any typed information for this probe.
611                          * Set the argument number to DTRACE_ARGNONE.
612                          */
613                         desc->dtargd_ndx = DTRACE_ARGNONE;
614                 } else {
615                         desc->dtargd_native[0] = '\0';
616                         desc->dtargd_xlate[0] = '\0';
617                         desc->dtargd_mapping = desc->dtargd_ndx;
618
619                         prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
620                             probe->dtpr_id, probe->dtpr_arg, desc);
621                 }
622
623                 mutex_exit(&mod_lock);
624                 mutex_exit(&dtrace_provider_lock);
625
626                 return (0);
627         }
628         case DTRACEIOC_PROBEMATCH:
629         case DTRACEIOC_PROBES: {
630                 dtrace_probedesc_t *p_desc = (dtrace_probedesc_t *) addr;
631                 dtrace_probe_t *probe = NULL;
632                 dtrace_probekey_t pkey;
633                 dtrace_id_t i;
634                 int m = 0;
635                 uint32_t priv = 0;
636                 uid_t uid = 0;
637                 zoneid_t zoneid = 0;
638
639                 DTRACE_IOCTL_PRINTF("%s(%d): %s\n",__func__,__LINE__,
640                     cmd == DTRACEIOC_PROBEMATCH ?
641                     "DTRACEIOC_PROBEMATCH":"DTRACEIOC_PROBES");
642
643                 p_desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
644                 p_desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
645                 p_desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
646                 p_desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
647
648                 /*
649                  * Before we attempt to match this probe, we want to give
650                  * all providers the opportunity to provide it.
651                  */
652                 if (p_desc->dtpd_id == DTRACE_IDNONE) {
653                         mutex_enter(&dtrace_provider_lock);
654                         dtrace_probe_provide(p_desc, NULL);
655                         mutex_exit(&dtrace_provider_lock);
656                         p_desc->dtpd_id++;
657                 }
658
659                 if (cmd == DTRACEIOC_PROBEMATCH)  {
660                         dtrace_probekey(p_desc, &pkey);
661                         pkey.dtpk_id = DTRACE_IDNONE;
662                 }
663
664                 dtrace_cred2priv(td->td_ucred, &priv, &uid, &zoneid);
665
666                 mutex_enter(&dtrace_lock);
667
668                 if (cmd == DTRACEIOC_PROBEMATCH) {
669                         for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
670                                 if ((probe = dtrace_probes[i - 1]) != NULL &&
671                                     (m = dtrace_match_probe(probe, &pkey,
672                                     priv, uid, zoneid)) != 0)
673                                         break;
674                         }
675
676                         if (m < 0) {
677                                 mutex_exit(&dtrace_lock);
678                                 return (EINVAL);
679                         }
680
681                 } else {
682                         for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
683                                 if ((probe = dtrace_probes[i - 1]) != NULL &&
684                                     dtrace_match_priv(probe, priv, uid, zoneid))
685                                         break;
686                         }
687                 }
688
689                 if (probe == NULL) {
690                         mutex_exit(&dtrace_lock);
691                         return (ESRCH);
692                 }
693
694                 dtrace_probe_description(probe, p_desc);
695                 mutex_exit(&dtrace_lock);
696
697                 return (0);
698         }
699         case DTRACEIOC_PROVIDER: {
700                 dtrace_providerdesc_t *pvd = (dtrace_providerdesc_t *) addr;
701                 dtrace_provider_t *pvp;
702
703                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROVIDER\n",__func__,__LINE__);
704
705                 pvd->dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
706                 mutex_enter(&dtrace_provider_lock);
707
708                 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
709                         if (strcmp(pvp->dtpv_name, pvd->dtvd_name) == 0)
710                                 break;
711                 }
712
713                 mutex_exit(&dtrace_provider_lock);
714
715                 if (pvp == NULL)
716                         return (ESRCH);
717
718                 bcopy(&pvp->dtpv_priv, &pvd->dtvd_priv, sizeof (dtrace_ppriv_t));
719                 bcopy(&pvp->dtpv_attr, &pvd->dtvd_attr, sizeof (dtrace_pattr_t));
720
721                 return (0);
722         }
723         case DTRACEIOC_REPLICATE: {
724                 dtrace_repldesc_t *desc = (dtrace_repldesc_t *) addr;
725                 dtrace_probedesc_t *match = &desc->dtrpd_match;
726                 dtrace_probedesc_t *create = &desc->dtrpd_create;
727                 int err;
728
729                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_REPLICATE\n",__func__,__LINE__);
730
731                 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
732                 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
733                 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
734                 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
735
736                 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
737                 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
738                 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
739                 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
740
741                 mutex_enter(&dtrace_lock);
742                 err = dtrace_enabling_replicate(state, match, create);
743                 mutex_exit(&dtrace_lock);
744
745                 return (err);
746         }
747         case DTRACEIOC_STATUS: {
748                 dtrace_status_t *stat = (dtrace_status_t *) addr;
749                 dtrace_dstate_t *dstate;
750                 int i, j;
751                 uint64_t nerrs;
752
753                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STATUS\n",__func__,__LINE__);
754
755                 /*
756                  * See the comment in dtrace_state_deadman() for the reason
757                  * for setting dts_laststatus to INT64_MAX before setting
758                  * it to the correct value.
759                  */
760                 state->dts_laststatus = INT64_MAX;
761                 dtrace_membar_producer();
762                 state->dts_laststatus = dtrace_gethrtime();
763
764                 bzero(stat, sizeof (*stat));
765
766                 mutex_enter(&dtrace_lock);
767
768                 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
769                         mutex_exit(&dtrace_lock);
770                         return (ENOENT);
771                 }
772
773                 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
774                         stat->dtst_exiting = 1;
775
776                 nerrs = state->dts_errors;
777                 dstate = &state->dts_vstate.dtvs_dynvars;
778
779                 for (i = 0; i < NCPU; i++) {
780 #if !defined(sun)
781                         if (pcpu_find(i) == NULL)
782                                 continue;
783 #endif
784                         dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
785
786                         stat->dtst_dyndrops += dcpu->dtdsc_drops;
787                         stat->dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
788                         stat->dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
789
790                         if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
791                                 stat->dtst_filled++;
792
793                         nerrs += state->dts_buffer[i].dtb_errors;
794
795                         for (j = 0; j < state->dts_nspeculations; j++) {
796                                 dtrace_speculation_t *spec;
797                                 dtrace_buffer_t *buf;
798
799                                 spec = &state->dts_speculations[j];
800                                 buf = &spec->dtsp_buffer[i];
801                                 stat->dtst_specdrops += buf->dtb_xamot_drops;
802                         }
803                 }
804
805                 stat->dtst_specdrops_busy = state->dts_speculations_busy;
806                 stat->dtst_specdrops_unavail = state->dts_speculations_unavail;
807                 stat->dtst_stkstroverflows = state->dts_stkstroverflows;
808                 stat->dtst_dblerrors = state->dts_dblerrors;
809                 stat->dtst_killed =
810                     (state->dts_activity == DTRACE_ACTIVITY_KILLED);
811                 stat->dtst_errors = nerrs;
812
813                 mutex_exit(&dtrace_lock);
814
815                 return (0);
816         }
817         case DTRACEIOC_STOP: {
818                 int rval;
819                 processorid_t *cpuid = (processorid_t *) addr;
820
821                 DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STOP\n",__func__,__LINE__);
822
823                 mutex_enter(&dtrace_lock);
824                 rval = dtrace_state_stop(state, cpuid);
825                 mutex_exit(&dtrace_lock);
826
827                 return (rval);
828         }
829         default:
830                 error = ENOTTY;
831         }
832         return (error);
833 }