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 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
28  */
29 
30 #include <sys/types.h>
31 #ifdef illumos
32 #include <sys/sysmacros.h>
33 #endif
34 
35 #include <assert.h>
36 #include <limits.h>
37 #include <strings.h>
38 #include <stdlib.h>
39 #ifdef illumos
40 #include <alloca.h>
41 #endif
42 #include <unistd.h>
43 #include <errno.h>
44 
45 #include <dt_provider.h>
46 #include <dt_module.h>
47 #include <dt_string.h>
48 #include <dt_list.h>
49 #include <dt_pid.h>
50 #include <dtrace.h>
51 
52 static dt_provider_t *
dt_provider_insert(dtrace_hdl_t * dtp,dt_provider_t * pvp,uint_t h)53 dt_provider_insert(dtrace_hdl_t *dtp, dt_provider_t *pvp, uint_t h)
54 {
55           dt_list_append(&dtp->dt_provlist, pvp);
56 
57           pvp->pv_next = dtp->dt_provs[h];
58           dtp->dt_provs[h] = pvp;
59           dtp->dt_nprovs++;
60 
61           return (pvp);
62 }
63 
64 dt_provider_t *
dt_provider_lookup(dtrace_hdl_t * dtp,const char * name)65 dt_provider_lookup(dtrace_hdl_t *dtp, const char *name)
66 {
67           uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_provbuckets;
68           dtrace_providerdesc_t desc;
69           dt_provider_t *pvp;
70 
71           for (pvp = dtp->dt_provs[h]; pvp != NULL; pvp = pvp->pv_next) {
72                     if (strcmp(pvp->pv_desc.dtvd_name, name) == 0)
73                               return (pvp);
74           }
75 
76           if (strisglob(name) || name[0] == '\0') {
77                     (void) dt_set_errno(dtp, EDT_NOPROV);
78                     return (NULL);
79           }
80 
81           bzero(&desc, sizeof (desc));
82           (void) strlcpy(desc.dtvd_name, name, DTRACE_PROVNAMELEN);
83 
84           if (dt_ioctl(dtp, DTRACEIOC_PROVIDER, &desc) == -1) {
85                     (void) dt_set_errno(dtp, errno == ESRCH ? EDT_NOPROV : errno);
86                     return (NULL);
87           }
88 
89           if ((pvp = dt_provider_create(dtp, name)) == NULL)
90                     return (NULL); /* dt_errno is set for us */
91 
92           bcopy(&desc, &pvp->pv_desc, sizeof (desc));
93           pvp->pv_flags |= DT_PROVIDER_IMPL;
94           return (pvp);
95 }
96 
97 dt_provider_t *
dt_provider_create(dtrace_hdl_t * dtp,const char * name)98 dt_provider_create(dtrace_hdl_t *dtp, const char *name)
99 {
100           dt_provider_t *pvp;
101 
102           if ((pvp = dt_zalloc(dtp, sizeof (dt_provider_t))) == NULL)
103                     return (NULL);
104 
105           (void) strlcpy(pvp->pv_desc.dtvd_name, name, DTRACE_PROVNAMELEN);
106           pvp->pv_probes = dt_idhash_create(pvp->pv_desc.dtvd_name, NULL, 0, 0);
107           pvp->pv_gen = dtp->dt_gen;
108           pvp->pv_hdl = dtp;
109 
110           if (pvp->pv_probes == NULL) {
111                     dt_free(dtp, pvp);
112                     (void) dt_set_errno(dtp, EDT_NOMEM);
113                     return (NULL);
114           }
115 
116           pvp->pv_desc.dtvd_attr.dtpa_provider = _dtrace_prvattr;
117           pvp->pv_desc.dtvd_attr.dtpa_mod = _dtrace_prvattr;
118           pvp->pv_desc.dtvd_attr.dtpa_func = _dtrace_prvattr;
119           pvp->pv_desc.dtvd_attr.dtpa_name = _dtrace_prvattr;
120           pvp->pv_desc.dtvd_attr.dtpa_args = _dtrace_prvattr;
121 
122           return (dt_provider_insert(dtp, pvp,
123               dt_strtab_hash(name, NULL) % dtp->dt_provbuckets));
124 }
125 
126 void
dt_provider_destroy(dtrace_hdl_t * dtp,dt_provider_t * pvp)127 dt_provider_destroy(dtrace_hdl_t *dtp, dt_provider_t *pvp)
128 {
129           dt_provider_t **pp;
130           uint_t h;
131 
132           assert(pvp->pv_hdl == dtp);
133 
134           h = dt_strtab_hash(pvp->pv_desc.dtvd_name, NULL) % dtp->dt_provbuckets;
135           pp = &dtp->dt_provs[h];
136 
137           while (*pp != NULL && *pp != pvp)
138                     pp = &(*pp)->pv_next;
139 
140           assert(*pp != NULL && *pp == pvp);
141           *pp = pvp->pv_next;
142 
143           dt_list_delete(&dtp->dt_provlist, pvp);
144           dtp->dt_nprovs--;
145 
146           if (pvp->pv_probes != NULL)
147                     dt_idhash_destroy(pvp->pv_probes);
148 
149           dt_node_link_free(&pvp->pv_nodes);
150           dt_free(dtp, pvp->pv_xrefs);
151           dt_free(dtp, pvp);
152 }
153 
154 int
dt_provider_xref(dtrace_hdl_t * dtp,dt_provider_t * pvp,id_t id)155 dt_provider_xref(dtrace_hdl_t *dtp, dt_provider_t *pvp, id_t id)
156 {
157           size_t oldsize = BT_SIZEOFMAP(pvp->pv_xrmax);
158           size_t newsize = BT_SIZEOFMAP(dtp->dt_xlatorid);
159 
160           assert(id >= 0 && id < dtp->dt_xlatorid);
161 
162           if (newsize > oldsize) {
163                     ulong_t *xrefs = dt_zalloc(dtp, newsize);
164 
165                     if (xrefs == NULL)
166                               return (-1);
167 
168                     bcopy(pvp->pv_xrefs, xrefs, oldsize);
169                     dt_free(dtp, pvp->pv_xrefs);
170 
171                     pvp->pv_xrefs = xrefs;
172                     pvp->pv_xrmax = dtp->dt_xlatorid;
173           }
174 
175           BT_SET(pvp->pv_xrefs, id);
176           return (0);
177 }
178 
179 static uint8_t
dt_probe_argmap(dt_node_t * xnp,dt_node_t * nnp)180 dt_probe_argmap(dt_node_t *xnp, dt_node_t *nnp)
181 {
182           uint8_t i;
183 
184           for (i = 0; nnp != NULL; i++) {
185                     if (nnp->dn_string != NULL &&
186                         strcmp(nnp->dn_string, xnp->dn_string) == 0)
187                               break;
188                     else
189                               nnp = nnp->dn_list;
190           }
191 
192           return (i);
193 }
194 
195 static dt_node_t *
dt_probe_alloc_args(dt_provider_t * pvp,int argc)196 dt_probe_alloc_args(dt_provider_t *pvp, int argc)
197 {
198           dt_node_t *args = NULL, *pnp = NULL, *dnp;
199           int i;
200 
201           for (i = 0; i < argc; i++, pnp = dnp) {
202                     if ((dnp = dt_node_xalloc(pvp->pv_hdl, DT_NODE_TYPE)) == NULL)
203                               return (NULL);
204 
205                     dnp->dn_link = pvp->pv_nodes;
206                     pvp->pv_nodes = dnp;
207 
208                     if (args == NULL)
209                               args = dnp;
210                     else
211                               pnp->dn_list = dnp;
212           }
213 
214           return (args);
215 }
216 
217 static size_t
dt_probe_keylen(const dtrace_probedesc_t * pdp)218 dt_probe_keylen(const dtrace_probedesc_t *pdp)
219 {
220           return (strlen(pdp->dtpd_mod) + 1 +
221               strlen(pdp->dtpd_func) + 1 + strlen(pdp->dtpd_name) + 1);
222 }
223 
224 static char *
dt_probe_key(const dtrace_probedesc_t * pdp,char * s)225 dt_probe_key(const dtrace_probedesc_t *pdp, char *s)
226 {
227           (void) snprintf(s, INT_MAX, "%s:%s:%s",
228               pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
229           return (s);
230 }
231 
232 /*
233  * If a probe was discovered from the kernel, ask dtrace(7D) for a description
234  * of each of its arguments, including native and translated types.
235  */
236 static dt_probe_t *
dt_probe_discover(dt_provider_t * pvp,const dtrace_probedesc_t * pdp)237 dt_probe_discover(dt_provider_t *pvp, const dtrace_probedesc_t *pdp)
238 {
239           dtrace_hdl_t *dtp = pvp->pv_hdl;
240           char *name = dt_probe_key(pdp, alloca(dt_probe_keylen(pdp)));
241 
242           dt_node_t *xargs, *nargs;
243           dt_ident_t *idp;
244           dt_probe_t *prp;
245 
246           dtrace_typeinfo_t dtt;
247           int i, nc, xc;
248 
249           int adc = _dtrace_argmax;
250           dtrace_argdesc_t *adv = alloca(sizeof (dtrace_argdesc_t) * adc);
251           dtrace_argdesc_t *adp = adv;
252 
253           assert(strcmp(pvp->pv_desc.dtvd_name, pdp->dtpd_provider) == 0);
254           assert(pdp->dtpd_id != DTRACE_IDNONE);
255 
256           dt_dprintf("discovering probe %s:%s id=%d\n",
257               pvp->pv_desc.dtvd_name, name, pdp->dtpd_id);
258 
259           for (nc = -1, i = 0; i < adc; i++, adp++) {
260                     bzero(adp, sizeof (dtrace_argdesc_t));
261                     adp->dtargd_ndx = i;
262                     adp->dtargd_id = pdp->dtpd_id;
263 
264                     if (dt_ioctl(dtp, DTRACEIOC_PROBEARG, adp) != 0) {
265                               (void) dt_set_errno(dtp, errno);
266                               return (NULL);
267                     }
268 
269                     if (adp->dtargd_ndx == DTRACE_ARGNONE)
270                               break; /* all argument descs have been retrieved */
271 
272                     nc = MAX(nc, adp->dtargd_mapping);
273           }
274 
275           xc = i;
276           nc++;
277 
278           /*
279            * The pid provider believes in giving the kernel a break. No reason to
280            * give the kernel all the ctf containers that we're keeping ourselves
281            * just to get it back from it. So if we're coming from a pid provider
282            * probe and the kernel gave us no argument information we'll get some
283            * here. If for some crazy reason the kernel knows about our userland
284            * types then we just ignore this.
285            */
286           if (xc == 0 && nc == 0 &&
287               strncmp(pvp->pv_desc.dtvd_name, "pid", 3) == 0) {
288                     nc = adc;
289                     dt_pid_get_types(dtp, pdp, adv, &nc);
290                     xc = nc;
291           }
292 
293           /*
294            * Now that we have discovered the number of native and translated
295            * arguments from the argument descriptions, allocate a new probe ident
296            * and corresponding dt_probe_t and hash it into the provider.
297            */
298           xargs = dt_probe_alloc_args(pvp, xc);
299           nargs = dt_probe_alloc_args(pvp, nc);
300 
301           if ((xc != 0 && xargs == NULL) || (nc != 0 && nargs == NULL))
302                     return (NULL); /* dt_errno is set for us */
303 
304           idp = dt_ident_create(name, DT_IDENT_PROBE,
305               DT_IDFLG_ORPHAN, pdp->dtpd_id, _dtrace_defattr, 0,
306               &dt_idops_probe, NULL, dtp->dt_gen);
307 
308           if (idp == NULL) {
309                     (void) dt_set_errno(dtp, EDT_NOMEM);
310                     return (NULL);
311           }
312 
313           if ((prp = dt_probe_create(dtp, idp, 2,
314               nargs, nc, xargs, xc)) == NULL) {
315                     dt_ident_destroy(idp);
316                     return (NULL);
317           }
318 
319           dt_probe_declare(pvp, prp);
320 
321           /*
322            * Once our new dt_probe_t is fully constructed, iterate over the
323            * cached argument descriptions and assign types to prp->pr_nargv[]
324            * and prp->pr_xargv[] and assign mappings to prp->pr_mapping[].
325            */
326           for (adp = adv, i = 0; i < xc; i++, adp++) {
327                     if (dtrace_type_strcompile(dtp,
328                         adp->dtargd_native, &dtt) != 0) {
329                               dt_dprintf("failed to resolve input type %s "
330                                   "for %s:%s arg #%d: %s\n", adp->dtargd_native,
331                                   pvp->pv_desc.dtvd_name, name, i + 1,
332                                   dtrace_errmsg(dtp, dtrace_errno(dtp)));
333 
334                               dtt.dtt_object = NULL;
335                               dtt.dtt_ctfp = NULL;
336                               dtt.dtt_type = CTF_ERR;
337                     } else {
338                               dt_node_type_assign(prp->pr_nargv[adp->dtargd_mapping],
339                                   dtt.dtt_ctfp, dtt.dtt_type,
340                                   dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
341                     }
342 
343                     if (dtt.dtt_type != CTF_ERR && (adp->dtargd_xlate[0] == '\0' ||
344                         strcmp(adp->dtargd_native, adp->dtargd_xlate) == 0)) {
345                               dt_node_type_propagate(prp->pr_nargv[
346                                   adp->dtargd_mapping], prp->pr_xargv[i]);
347                     } else if (dtrace_type_strcompile(dtp,
348                         adp->dtargd_xlate, &dtt) != 0) {
349                               dt_dprintf("failed to resolve output type %s "
350                                   "for %s:%s arg #%d: %s\n", adp->dtargd_xlate,
351                                   pvp->pv_desc.dtvd_name, name, i + 1,
352                                   dtrace_errmsg(dtp, dtrace_errno(dtp)));
353 
354                               dtt.dtt_object = NULL;
355                               dtt.dtt_ctfp = NULL;
356                               dtt.dtt_type = CTF_ERR;
357                     } else {
358                               dt_node_type_assign(prp->pr_xargv[i],
359                                   dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
360                     }
361 
362                     prp->pr_mapping[i] = adp->dtargd_mapping;
363                     prp->pr_argv[i] = dtt;
364           }
365 
366           return (prp);
367 }
368 
369 /*
370  * Lookup a probe declaration based on a known provider and full or partially
371  * specified module, function, and name.  If the probe is not known to us yet,
372  * ask dtrace(7D) to match the description and then cache any useful results.
373  */
374 dt_probe_t *
dt_probe_lookup(dt_provider_t * pvp,const char * s)375 dt_probe_lookup(dt_provider_t *pvp, const char *s)
376 {
377           dtrace_hdl_t *dtp = pvp->pv_hdl;
378           dtrace_probedesc_t pd;
379           dt_ident_t *idp;
380           size_t keylen;
381           char *key;
382 
383           if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, s, &pd) != 0)
384                     return (NULL); /* dt_errno is set for us */
385 
386           keylen = dt_probe_keylen(&pd);
387           key = dt_probe_key(&pd, alloca(keylen));
388 
389           /*
390            * If the probe is already declared, then return the dt_probe_t from
391            * the existing identifier.  This could come from a static declaration
392            * or it could have been cached from an earlier call to this function.
393            */
394           if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
395                     return (idp->di_data);
396 
397           /*
398            * If the probe isn't known, use the probe description computed above
399            * to ask dtrace(7D) to find the first matching probe.
400            */
401           if (dt_ioctl(dtp, DTRACEIOC_PROBEMATCH, &pd) == 0)
402                     return (dt_probe_discover(pvp, &pd));
403 
404           if (errno == ESRCH || errno == EBADF)
405                     (void) dt_set_errno(dtp, EDT_NOPROBE);
406           else
407                     (void) dt_set_errno(dtp, errno);
408 
409           return (NULL);
410 }
411 
412 dt_probe_t *
dt_probe_create(dtrace_hdl_t * dtp,dt_ident_t * idp,int protoc,dt_node_t * nargs,uint_t nargc,dt_node_t * xargs,uint_t xargc)413 dt_probe_create(dtrace_hdl_t *dtp, dt_ident_t *idp, int protoc,
414     dt_node_t *nargs, uint_t nargc, dt_node_t *xargs, uint_t xargc)
415 {
416           dt_module_t *dmp;
417           dt_probe_t *prp;
418           const char *p;
419           uint_t i;
420 
421           assert(idp->di_kind == DT_IDENT_PROBE);
422           assert(idp->di_data == NULL);
423 
424           /*
425            * If only a single prototype is given, set xargc/s to nargc/s to
426            * simplify subsequent use.  Note that we can have one or both of nargs
427            * and xargs be specified but set to NULL, indicating a void prototype.
428            */
429           if (protoc < 2) {
430                     assert(xargs == NULL);
431                     assert(xargc == 0);
432                     xargs = nargs;
433                     xargc = nargc;
434           }
435 
436           if ((prp = dt_alloc(dtp, sizeof (dt_probe_t))) == NULL)
437                     return (NULL);
438 
439           prp->pr_pvp = NULL;
440           prp->pr_ident = idp;
441 
442           p = strrchr(idp->di_name, ':');
443           assert(p != NULL);
444           prp->pr_name = p + 1;
445 
446           prp->pr_nargs = nargs;
447           prp->pr_nargv = dt_alloc(dtp, sizeof (dt_node_t *) * nargc);
448           prp->pr_nargc = nargc;
449           prp->pr_xargs = xargs;
450           prp->pr_xargv = dt_alloc(dtp, sizeof (dt_node_t *) * xargc);
451           prp->pr_xargc = xargc;
452           prp->pr_mapping = dt_alloc(dtp, sizeof (uint8_t) * xargc);
453           prp->pr_inst = NULL;
454           prp->pr_argv = dt_alloc(dtp, sizeof (dtrace_typeinfo_t) * xargc);
455           prp->pr_argc = xargc;
456 
457           if ((prp->pr_nargc != 0 && prp->pr_nargv == NULL) ||
458               (prp->pr_xargc != 0 && prp->pr_xargv == NULL) ||
459               (prp->pr_xargc != 0 && prp->pr_mapping == NULL) ||
460               (prp->pr_argc != 0 && prp->pr_argv == NULL)) {
461                     dt_probe_destroy(prp);
462                     return (NULL);
463           }
464 
465           for (i = 0; i < xargc; i++, xargs = xargs->dn_list) {
466                     if (xargs->dn_string != NULL)
467                               prp->pr_mapping[i] = dt_probe_argmap(xargs, nargs);
468                     else
469                               prp->pr_mapping[i] = i;
470 
471                     prp->pr_xargv[i] = xargs;
472 
473                     if ((dmp = dt_module_lookup_by_ctf(dtp,
474                         xargs->dn_ctfp)) != NULL)
475                               prp->pr_argv[i].dtt_object = dmp->dm_name;
476                     else
477                               prp->pr_argv[i].dtt_object = NULL;
478 
479                     prp->pr_argv[i].dtt_ctfp = xargs->dn_ctfp;
480                     prp->pr_argv[i].dtt_type = xargs->dn_type;
481           }
482 
483           for (i = 0; i < nargc; i++, nargs = nargs->dn_list)
484                     prp->pr_nargv[i] = nargs;
485 
486           idp->di_data = prp;
487           return (prp);
488 }
489 
490 void
dt_probe_declare(dt_provider_t * pvp,dt_probe_t * prp)491 dt_probe_declare(dt_provider_t *pvp, dt_probe_t *prp)
492 {
493           assert(prp->pr_ident->di_kind == DT_IDENT_PROBE);
494           assert(prp->pr_ident->di_data == prp);
495           assert(prp->pr_pvp == NULL);
496 
497           if (prp->pr_xargs != prp->pr_nargs)
498                     pvp->pv_flags &= ~DT_PROVIDER_INTF;
499 
500           prp->pr_pvp = pvp;
501           dt_idhash_xinsert(pvp->pv_probes, prp->pr_ident);
502 }
503 
504 void
dt_probe_destroy(dt_probe_t * prp)505 dt_probe_destroy(dt_probe_t *prp)
506 {
507           dt_probe_instance_t *pip, *pip_next;
508           dtrace_hdl_t *dtp;
509 
510           if (prp->pr_pvp != NULL)
511                     dtp = prp->pr_pvp->pv_hdl;
512           else
513                     dtp = yypcb->pcb_hdl;
514 
515           dt_node_list_free(&prp->pr_nargs);
516           dt_node_list_free(&prp->pr_xargs);
517 
518           dt_free(dtp, prp->pr_nargv);
519           dt_free(dtp, prp->pr_xargv);
520 
521           for (pip = prp->pr_inst; pip != NULL; pip = pip_next) {
522                     pip_next = pip->pi_next;
523                     dt_free(dtp, pip->pi_rname);
524                     dt_free(dtp, pip->pi_fname);
525                     dt_free(dtp, pip->pi_offs);
526                     dt_free(dtp, pip->pi_enoffs);
527                     dt_free(dtp, pip);
528           }
529 
530           dt_free(dtp, prp->pr_mapping);
531           dt_free(dtp, prp->pr_argv);
532           dt_free(dtp, prp);
533 }
534 
535 int
dt_probe_define(dt_provider_t * pvp,dt_probe_t * prp,const char * fname,const char * rname,uint32_t offset,int isenabled)536 dt_probe_define(dt_provider_t *pvp, dt_probe_t *prp,
537     const char *fname, const char *rname, uint32_t offset, int isenabled)
538 {
539           dtrace_hdl_t *dtp = pvp->pv_hdl;
540           dt_probe_instance_t *pip;
541           uint32_t **offs;
542           uint_t *noffs, *maxoffs;
543 
544           assert(fname != NULL);
545 
546           for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
547                     if (strcmp(pip->pi_fname, fname) == 0 &&
548                         strcmp(pip->pi_rname, rname) == 0)
549                               break;
550           }
551 
552           if (pip == NULL) {
553                     if ((pip = dt_zalloc(dtp, sizeof (*pip))) == NULL)
554                               return (-1);
555 
556                     if ((pip->pi_offs = dt_zalloc(dtp, sizeof (uint32_t))) == NULL)
557                               goto nomem;
558 
559                     if ((pip->pi_enoffs = dt_zalloc(dtp,
560                         sizeof (uint32_t))) == NULL)
561                               goto nomem;
562 
563                     if ((pip->pi_fname = strdup(fname)) == NULL)
564                               goto nomem;
565 
566                     if ((pip->pi_rname = strdup(rname)) == NULL)
567                               goto nomem;
568 
569                     pip->pi_noffs = 0;
570                     pip->pi_maxoffs = 1;
571                     pip->pi_nenoffs = 0;
572                     pip->pi_maxenoffs = 1;
573 
574                     pip->pi_next = prp->pr_inst;
575 
576                     prp->pr_inst = pip;
577           }
578 
579           if (isenabled) {
580                     offs = &pip->pi_enoffs;
581                     noffs = &pip->pi_nenoffs;
582                     maxoffs = &pip->pi_maxenoffs;
583           } else {
584                     offs = &pip->pi_offs;
585                     noffs = &pip->pi_noffs;
586                     maxoffs = &pip->pi_maxoffs;
587           }
588 
589           if (*noffs == *maxoffs) {
590                     uint_t new_max = *maxoffs * 2;
591                     uint32_t *new_offs = dt_alloc(dtp, sizeof (uint32_t) * new_max);
592 
593                     if (new_offs == NULL)
594                               return (-1);
595 
596                     bcopy(*offs, new_offs, sizeof (uint32_t) * *maxoffs);
597 
598                     dt_free(dtp, *offs);
599                     *maxoffs = new_max;
600                     *offs = new_offs;
601           }
602 
603           dt_dprintf("defined probe %s %s:%s %s() +0x%x (%s)\n",
604               isenabled ? "(is-enabled)" : "",
605               pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, fname, offset,
606               rname);
607 
608           assert(*noffs < *maxoffs);
609           (*offs)[(*noffs)++] = offset;
610 
611           return (0);
612 
613 nomem:
614           dt_free(dtp, pip->pi_fname);
615           dt_free(dtp, pip->pi_enoffs);
616           dt_free(dtp, pip->pi_offs);
617           dt_free(dtp, pip);
618           return (dt_set_errno(dtp, EDT_NOMEM));
619 }
620 
621 /*
622  * Lookup the dynamic translator type tag for the specified probe argument and
623  * assign the type to the specified node.  If the type is not yet defined, add
624  * it to the "D" module's type container as a typedef for an unknown type.
625  */
626 dt_node_t *
dt_probe_tag(dt_probe_t * prp,uint_t argn,dt_node_t * dnp)627 dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp)
628 {
629           dtrace_hdl_t *dtp = prp->pr_pvp->pv_hdl;
630           dtrace_typeinfo_t dtt;
631           size_t len;
632           char *tag;
633 
634           len = snprintf(NULL, 0, "__dtrace_%s___%s_arg%u",
635               prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
636 
637           tag = alloca(len + 1);
638 
639           (void) snprintf(tag, len + 1, "__dtrace_%s___%s_arg%u",
640               prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
641 
642           if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS, tag, &dtt) != 0) {
643                     dtt.dtt_object = DTRACE_OBJ_DDEFS;
644                     dtt.dtt_ctfp = DT_DYN_CTFP(dtp);
645                     dtt.dtt_type = ctf_add_typedef(DT_DYN_CTFP(dtp),
646                         CTF_ADD_ROOT, tag, DT_DYN_TYPE(dtp));
647 
648                     if (dtt.dtt_type == CTF_ERR ||
649                         ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
650                               xyerror(D_UNKNOWN, "cannot define type %s: %s\n",
651                                   tag, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
652                     }
653           }
654 
655           bzero(dnp, sizeof (dt_node_t));
656           dnp->dn_kind = DT_NODE_TYPE;
657 
658           dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
659           dt_node_attr_assign(dnp, _dtrace_defattr);
660 
661           return (dnp);
662 }
663 
664 /*ARGSUSED*/
665 static int
dt_probe_desc(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * arg)666 dt_probe_desc(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
667 {
668           if (((dtrace_probedesc_t *)arg)->dtpd_id == DTRACE_IDNONE) {
669                     bcopy(pdp, arg, sizeof (dtrace_probedesc_t));
670                     return (0);
671           }
672 
673           return (1);
674 }
675 
676 dt_probe_t *
dt_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)677 dt_probe_info(dtrace_hdl_t *dtp,
678     const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
679 {
680           int m_is_glob = pdp->dtpd_mod[0] == '\0' || strisglob(pdp->dtpd_mod);
681           int f_is_glob = pdp->dtpd_func[0] == '\0' || strisglob(pdp->dtpd_func);
682           int n_is_glob = pdp->dtpd_name[0] == '\0' || strisglob(pdp->dtpd_name);
683 
684           dt_probe_t *prp = NULL;
685           const dtrace_pattr_t *pap;
686           dt_provider_t *pvp;
687           dt_ident_t *idp;
688 
689           /*
690            * Attempt to lookup the probe in our existing cache for this provider.
691            * If none is found and an explicit probe ID was specified, discover
692            * that specific probe and cache its description and arguments.
693            */
694           if ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) != NULL) {
695                     size_t keylen = dt_probe_keylen(pdp);
696                     char *key = dt_probe_key(pdp, alloca(keylen));
697 
698                     if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
699                               prp = idp->di_data;
700                     else if (pdp->dtpd_id != DTRACE_IDNONE)
701                               prp = dt_probe_discover(pvp, pdp);
702           }
703 
704           /*
705            * If no probe was found in our cache, convert the caller's partial
706            * probe description into a fully-formed matching probe description by
707            * iterating over up to at most two probes that match 'pdp'.  We then
708            * call dt_probe_discover() on the resulting probe identifier.
709            */
710           if (prp == NULL) {
711                     dtrace_probedesc_t pd;
712                     int m;
713 
714                     bzero(&pd, sizeof (pd));
715                     pd.dtpd_id = DTRACE_IDNONE;
716 
717                     /*
718                      * Call dtrace_probe_iter() to find matching probes.  Our
719                      * dt_probe_desc() callback will produce the following results:
720                      *
721                      * m < 0 dtrace_probe_iter() found zero matches (or failed).
722                      * m > 0 dtrace_probe_iter() found more than one match.
723                      * m = 0 dtrace_probe_iter() found exactly one match.
724                      */
725                     if ((m = dtrace_probe_iter(dtp, pdp, dt_probe_desc, &pd)) < 0)
726                               return (NULL); /* dt_errno is set for us */
727 
728                     if ((pvp = dt_provider_lookup(dtp, pd.dtpd_provider)) == NULL)
729                               return (NULL); /* dt_errno is set for us */
730 
731                     /*
732                      * If more than one probe was matched, then do not report probe
733                      * information if either of the following conditions is true:
734                      *
735                      * (a) The Arguments Data stability of the matched provider is
736                      *        less than Evolving.
737                      *
738                      * (b) Any description component that is at least Evolving is
739                      *        empty or is specified using a globbing expression.
740                      *
741                      * These conditions imply that providers that provide Evolving
742                      * or better Arguments Data stability must guarantee that all
743                      * probes with identical field names in a field of Evolving or
744                      * better Name stability have identical argument signatures.
745                      */
746                     if (m > 0) {
747                               if (pvp->pv_desc.dtvd_attr.dtpa_args.dtat_data <
748                                   DTRACE_STABILITY_EVOLVING) {
749                                         (void) dt_set_errno(dtp, EDT_UNSTABLE);
750                                         return (NULL);
751                               }
752 
753 
754                               if (pvp->pv_desc.dtvd_attr.dtpa_mod.dtat_name >=
755                                   DTRACE_STABILITY_EVOLVING && m_is_glob) {
756                                         (void) dt_set_errno(dtp, EDT_UNSTABLE);
757                                         return (NULL);
758                               }
759 
760                               if (pvp->pv_desc.dtvd_attr.dtpa_func.dtat_name >=
761                                   DTRACE_STABILITY_EVOLVING && f_is_glob) {
762                                         (void) dt_set_errno(dtp, EDT_UNSTABLE);
763                                         return (NULL);
764                               }
765 
766                               if (pvp->pv_desc.dtvd_attr.dtpa_name.dtat_name >=
767                                   DTRACE_STABILITY_EVOLVING && n_is_glob) {
768                                         (void) dt_set_errno(dtp, EDT_UNSTABLE);
769                                         return (NULL);
770                               }
771                     }
772 
773                     /*
774                      * If we matched a probe exported by dtrace(7D), then discover
775                      * the real attributes.  Otherwise grab the static declaration.
776                      */
777                     if (pd.dtpd_id != DTRACE_IDNONE)
778                               prp = dt_probe_discover(pvp, &pd);
779                     else
780                               prp = dt_probe_lookup(pvp, pd.dtpd_name);
781 
782                     if (prp == NULL)
783                               return (NULL); /* dt_errno is set for us */
784           }
785 
786           assert(pvp != NULL && prp != NULL);
787 
788           /*
789            * Compute the probe description attributes by taking the minimum of
790            * the attributes of the specified fields.  If no provider is specified
791            * or a glob pattern is used for the provider, use Unstable attributes.
792            */
793           if (pdp->dtpd_provider[0] == '\0' || strisglob(pdp->dtpd_provider))
794                     pap = &_dtrace_prvdesc;
795           else
796                     pap = &pvp->pv_desc.dtvd_attr;
797 
798           pip->dtp_attr = pap->dtpa_provider;
799 
800           if (!m_is_glob)
801                     pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_mod);
802           if (!f_is_glob)
803                     pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_func);
804           if (!n_is_glob)
805                     pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_name);
806 
807           pip->dtp_arga = pap->dtpa_args;
808           pip->dtp_argv = prp->pr_argv;
809           pip->dtp_argc = prp->pr_argc;
810 
811           return (prp);
812 }
813 
814 int
dtrace_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)815 dtrace_probe_info(dtrace_hdl_t *dtp,
816     const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
817 {
818           return (dt_probe_info(dtp, pdp, pip) != NULL ? 0 : -1);
819 }
820 
821 /*ARGSUSED*/
822 static int
dt_probe_iter(dt_idhash_t * ihp,dt_ident_t * idp,dt_probe_iter_t * pit)823 dt_probe_iter(dt_idhash_t *ihp, dt_ident_t *idp, dt_probe_iter_t *pit)
824 {
825           const dt_probe_t *prp = idp->di_data;
826 
827           if (!dt_gmatch(prp->pr_name, pit->pit_pat))
828                     return (0); /* continue on and examine next probe in hash */
829 
830           (void) strlcpy(pit->pit_desc.dtpd_name, prp->pr_name, DTRACE_NAMELEN);
831           pit->pit_desc.dtpd_id = idp->di_id;
832           pit->pit_matches++;
833 
834           return (pit->pit_func(pit->pit_hdl, &pit->pit_desc, pit->pit_arg));
835 }
836 
837 int
dtrace_probe_iter(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probe_f * func,void * arg)838 dtrace_probe_iter(dtrace_hdl_t *dtp,
839     const dtrace_probedesc_t *pdp, dtrace_probe_f *func, void *arg)
840 {
841           const char *provider = pdp ? pdp->dtpd_provider : NULL;
842           dtrace_id_t id = DTRACE_IDNONE;
843 
844           dtrace_probedesc_t pd;
845           dt_probe_iter_t pit;
846           int cmd, rv;
847 
848           bzero(&pit, sizeof (pit));
849           pit.pit_hdl = dtp;
850           pit.pit_func = func;
851           pit.pit_arg = arg;
852           pit.pit_pat = pdp ? pdp->dtpd_name : NULL;
853 
854           for (pit.pit_pvp = dt_list_next(&dtp->dt_provlist);
855               pit.pit_pvp != NULL; pit.pit_pvp = dt_list_next(pit.pit_pvp)) {
856 
857                     if (pit.pit_pvp->pv_flags & DT_PROVIDER_IMPL)
858                               continue; /* we'll get these later using dt_ioctl() */
859 
860                     if (!dt_gmatch(pit.pit_pvp->pv_desc.dtvd_name, provider))
861                               continue;
862 
863                     (void) strlcpy(pit.pit_desc.dtpd_provider,
864                         pit.pit_pvp->pv_desc.dtvd_name, DTRACE_PROVNAMELEN);
865 
866                     if ((rv = dt_idhash_iter(pit.pit_pvp->pv_probes,
867                         (dt_idhash_f *)dt_probe_iter, &pit)) != 0)
868                               return (rv);
869           }
870 
871           if (pdp != NULL)
872                     cmd = DTRACEIOC_PROBEMATCH;
873           else
874                     cmd = DTRACEIOC_PROBES;
875 
876           for (;;) {
877                     if (pdp != NULL)
878                               bcopy(pdp, &pd, sizeof (pd));
879 
880                     pd.dtpd_id = id;
881 
882                     if (dt_ioctl(dtp, cmd, &pd) != 0)
883                               break;
884                     else if ((rv = func(dtp, &pd, arg)) != 0)
885                               return (rv);
886 
887                     pit.pit_matches++;
888                     id = pd.dtpd_id + 1;
889           }
890 
891           switch (errno) {
892           case ESRCH:
893           case EBADF:
894                     return (pit.pit_matches ? 0 : dt_set_errno(dtp, EDT_NOPROBE));
895           case EINVAL:
896                     return (dt_set_errno(dtp, EDT_BADPGLOB));
897           default:
898                     return (dt_set_errno(dtp, errno));
899           }
900 }
901