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