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