xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_provider.c (revision 2179a159ea93e37b7fb2126ae0b1627b875f808b)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
268e648814SRui Paulo /*
278e648814SRui Paulo  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
288e648814SRui Paulo  */
296ff6d951SJohn Birrell 
306ff6d951SJohn Birrell #include <sys/types.h>
31bc96366cSSteven Hartland #ifdef illumos
326ff6d951SJohn Birrell #include <sys/sysmacros.h>
3340de299fSJohn Birrell #endif
346ff6d951SJohn Birrell 
356ff6d951SJohn Birrell #include <assert.h>
366ff6d951SJohn Birrell #include <limits.h>
376ff6d951SJohn Birrell #include <strings.h>
386ff6d951SJohn Birrell #include <stdlib.h>
39bc96366cSSteven Hartland #ifdef illumos
406ff6d951SJohn Birrell #include <alloca.h>
4140de299fSJohn Birrell #endif
426ff6d951SJohn Birrell #include <unistd.h>
436ff6d951SJohn Birrell #include <errno.h>
446ff6d951SJohn Birrell 
456ff6d951SJohn Birrell #include <dt_provider.h>
466ff6d951SJohn Birrell #include <dt_module.h>
476ff6d951SJohn Birrell #include <dt_string.h>
486ff6d951SJohn Birrell #include <dt_list.h>
498e648814SRui Paulo #include <dt_pid.h>
508e648814SRui Paulo #include <dtrace.h>
51*2179a159SChristos Margiolis #include <kinst.h>
526ff6d951SJohn Birrell 
536ff6d951SJohn Birrell static dt_provider_t *
dt_provider_insert(dtrace_hdl_t * dtp,dt_provider_t * pvp,uint_t h)546ff6d951SJohn Birrell dt_provider_insert(dtrace_hdl_t *dtp, dt_provider_t *pvp, uint_t h)
556ff6d951SJohn Birrell {
566ff6d951SJohn Birrell 	dt_list_append(&dtp->dt_provlist, pvp);
576ff6d951SJohn Birrell 
586ff6d951SJohn Birrell 	pvp->pv_next = dtp->dt_provs[h];
596ff6d951SJohn Birrell 	dtp->dt_provs[h] = pvp;
606ff6d951SJohn Birrell 	dtp->dt_nprovs++;
616ff6d951SJohn Birrell 
626ff6d951SJohn Birrell 	return (pvp);
636ff6d951SJohn Birrell }
646ff6d951SJohn Birrell 
656ff6d951SJohn Birrell dt_provider_t *
dt_provider_lookup(dtrace_hdl_t * dtp,const char * name)666ff6d951SJohn Birrell dt_provider_lookup(dtrace_hdl_t *dtp, const char *name)
676ff6d951SJohn Birrell {
686ff6d951SJohn Birrell 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_provbuckets;
696ff6d951SJohn Birrell 	dtrace_providerdesc_t desc;
706ff6d951SJohn Birrell 	dt_provider_t *pvp;
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell 	for (pvp = dtp->dt_provs[h]; pvp != NULL; pvp = pvp->pv_next) {
736ff6d951SJohn Birrell 		if (strcmp(pvp->pv_desc.dtvd_name, name) == 0)
746ff6d951SJohn Birrell 			return (pvp);
756ff6d951SJohn Birrell 	}
766ff6d951SJohn Birrell 
776ff6d951SJohn Birrell 	if (strisglob(name) || name[0] == '\0') {
786ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EDT_NOPROV);
796ff6d951SJohn Birrell 		return (NULL);
806ff6d951SJohn Birrell 	}
816ff6d951SJohn Birrell 
826ff6d951SJohn Birrell 	bzero(&desc, sizeof (desc));
836ff6d951SJohn Birrell 	(void) strlcpy(desc.dtvd_name, name, DTRACE_PROVNAMELEN);
846ff6d951SJohn Birrell 
856ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_PROVIDER, &desc) == -1) {
866ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, errno == ESRCH ? EDT_NOPROV : errno);
876ff6d951SJohn Birrell 		return (NULL);
886ff6d951SJohn Birrell 	}
896ff6d951SJohn Birrell 
906ff6d951SJohn Birrell 	if ((pvp = dt_provider_create(dtp, name)) == NULL)
916ff6d951SJohn Birrell 		return (NULL); /* dt_errno is set for us */
926ff6d951SJohn Birrell 
936ff6d951SJohn Birrell 	bcopy(&desc, &pvp->pv_desc, sizeof (desc));
946ff6d951SJohn Birrell 	pvp->pv_flags |= DT_PROVIDER_IMPL;
956ff6d951SJohn Birrell 	return (pvp);
966ff6d951SJohn Birrell }
976ff6d951SJohn Birrell 
986ff6d951SJohn Birrell dt_provider_t *
dt_provider_create(dtrace_hdl_t * dtp,const char * name)996ff6d951SJohn Birrell dt_provider_create(dtrace_hdl_t *dtp, const char *name)
1006ff6d951SJohn Birrell {
1016ff6d951SJohn Birrell 	dt_provider_t *pvp;
1026ff6d951SJohn Birrell 
1036ff6d951SJohn Birrell 	if ((pvp = dt_zalloc(dtp, sizeof (dt_provider_t))) == NULL)
1046ff6d951SJohn Birrell 		return (NULL);
1056ff6d951SJohn Birrell 
1066ff6d951SJohn Birrell 	(void) strlcpy(pvp->pv_desc.dtvd_name, name, DTRACE_PROVNAMELEN);
1076ff6d951SJohn Birrell 	pvp->pv_probes = dt_idhash_create(pvp->pv_desc.dtvd_name, NULL, 0, 0);
1086ff6d951SJohn Birrell 	pvp->pv_gen = dtp->dt_gen;
1096ff6d951SJohn Birrell 	pvp->pv_hdl = dtp;
1106ff6d951SJohn Birrell 
1116ff6d951SJohn Birrell 	if (pvp->pv_probes == NULL) {
1126ff6d951SJohn Birrell 		dt_free(dtp, pvp);
1136ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EDT_NOMEM);
1146ff6d951SJohn Birrell 		return (NULL);
1156ff6d951SJohn Birrell 	}
1166ff6d951SJohn Birrell 
1176ff6d951SJohn Birrell 	pvp->pv_desc.dtvd_attr.dtpa_provider = _dtrace_prvattr;
1186ff6d951SJohn Birrell 	pvp->pv_desc.dtvd_attr.dtpa_mod = _dtrace_prvattr;
1196ff6d951SJohn Birrell 	pvp->pv_desc.dtvd_attr.dtpa_func = _dtrace_prvattr;
1206ff6d951SJohn Birrell 	pvp->pv_desc.dtvd_attr.dtpa_name = _dtrace_prvattr;
1216ff6d951SJohn Birrell 	pvp->pv_desc.dtvd_attr.dtpa_args = _dtrace_prvattr;
1226ff6d951SJohn Birrell 
1236ff6d951SJohn Birrell 	return (dt_provider_insert(dtp, pvp,
1246ff6d951SJohn Birrell 	    dt_strtab_hash(name, NULL) % dtp->dt_provbuckets));
1256ff6d951SJohn Birrell }
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell void
dt_provider_destroy(dtrace_hdl_t * dtp,dt_provider_t * pvp)1286ff6d951SJohn Birrell dt_provider_destroy(dtrace_hdl_t *dtp, dt_provider_t *pvp)
1296ff6d951SJohn Birrell {
1306ff6d951SJohn Birrell 	dt_provider_t **pp;
1316ff6d951SJohn Birrell 	uint_t h;
1326ff6d951SJohn Birrell 
1336ff6d951SJohn Birrell 	assert(pvp->pv_hdl == dtp);
1346ff6d951SJohn Birrell 
1356ff6d951SJohn Birrell 	h = dt_strtab_hash(pvp->pv_desc.dtvd_name, NULL) % dtp->dt_provbuckets;
1366ff6d951SJohn Birrell 	pp = &dtp->dt_provs[h];
1376ff6d951SJohn Birrell 
1386ff6d951SJohn Birrell 	while (*pp != NULL && *pp != pvp)
1396ff6d951SJohn Birrell 		pp = &(*pp)->pv_next;
1406ff6d951SJohn Birrell 
1416ff6d951SJohn Birrell 	assert(*pp != NULL && *pp == pvp);
1426ff6d951SJohn Birrell 	*pp = pvp->pv_next;
1436ff6d951SJohn Birrell 
1446ff6d951SJohn Birrell 	dt_list_delete(&dtp->dt_provlist, pvp);
1456ff6d951SJohn Birrell 	dtp->dt_nprovs--;
1466ff6d951SJohn Birrell 
1476ff6d951SJohn Birrell 	if (pvp->pv_probes != NULL)
1486ff6d951SJohn Birrell 		dt_idhash_destroy(pvp->pv_probes);
1496ff6d951SJohn Birrell 
1506ff6d951SJohn Birrell 	dt_node_link_free(&pvp->pv_nodes);
1516ff6d951SJohn Birrell 	dt_free(dtp, pvp->pv_xrefs);
1526ff6d951SJohn Birrell 	dt_free(dtp, pvp);
1536ff6d951SJohn Birrell }
1546ff6d951SJohn Birrell 
1556ff6d951SJohn Birrell int
dt_provider_xref(dtrace_hdl_t * dtp,dt_provider_t * pvp,id_t id)1566ff6d951SJohn Birrell dt_provider_xref(dtrace_hdl_t *dtp, dt_provider_t *pvp, id_t id)
1576ff6d951SJohn Birrell {
1586ff6d951SJohn Birrell 	size_t oldsize = BT_SIZEOFMAP(pvp->pv_xrmax);
1596ff6d951SJohn Birrell 	size_t newsize = BT_SIZEOFMAP(dtp->dt_xlatorid);
1606ff6d951SJohn Birrell 
1616ff6d951SJohn Birrell 	assert(id >= 0 && id < dtp->dt_xlatorid);
1626ff6d951SJohn Birrell 
1636ff6d951SJohn Birrell 	if (newsize > oldsize) {
1646ff6d951SJohn Birrell 		ulong_t *xrefs = dt_zalloc(dtp, newsize);
1656ff6d951SJohn Birrell 
1666ff6d951SJohn Birrell 		if (xrefs == NULL)
1676ff6d951SJohn Birrell 			return (-1);
1686ff6d951SJohn Birrell 
1696ff6d951SJohn Birrell 		bcopy(pvp->pv_xrefs, xrefs, oldsize);
1706ff6d951SJohn Birrell 		dt_free(dtp, pvp->pv_xrefs);
1716ff6d951SJohn Birrell 
1726ff6d951SJohn Birrell 		pvp->pv_xrefs = xrefs;
1736ff6d951SJohn Birrell 		pvp->pv_xrmax = dtp->dt_xlatorid;
1746ff6d951SJohn Birrell 	}
1756ff6d951SJohn Birrell 
1766ff6d951SJohn Birrell 	BT_SET(pvp->pv_xrefs, id);
1776ff6d951SJohn Birrell 	return (0);
1786ff6d951SJohn Birrell }
1796ff6d951SJohn Birrell 
1806ff6d951SJohn Birrell static uint8_t
dt_probe_argmap(dt_node_t * xnp,dt_node_t * nnp)1816ff6d951SJohn Birrell dt_probe_argmap(dt_node_t *xnp, dt_node_t *nnp)
1826ff6d951SJohn Birrell {
1836ff6d951SJohn Birrell 	uint8_t i;
1846ff6d951SJohn Birrell 
1856ff6d951SJohn Birrell 	for (i = 0; nnp != NULL; i++) {
1866ff6d951SJohn Birrell 		if (nnp->dn_string != NULL &&
1876ff6d951SJohn Birrell 		    strcmp(nnp->dn_string, xnp->dn_string) == 0)
1886ff6d951SJohn Birrell 			break;
1896ff6d951SJohn Birrell 		else
1906ff6d951SJohn Birrell 			nnp = nnp->dn_list;
1916ff6d951SJohn Birrell 	}
1926ff6d951SJohn Birrell 
1936ff6d951SJohn Birrell 	return (i);
1946ff6d951SJohn Birrell }
1956ff6d951SJohn Birrell 
1966ff6d951SJohn Birrell static dt_node_t *
dt_probe_alloc_args(dt_provider_t * pvp,int argc)1976ff6d951SJohn Birrell dt_probe_alloc_args(dt_provider_t *pvp, int argc)
1986ff6d951SJohn Birrell {
1996ff6d951SJohn Birrell 	dt_node_t *args = NULL, *pnp = NULL, *dnp;
2006ff6d951SJohn Birrell 	int i;
2016ff6d951SJohn Birrell 
2026ff6d951SJohn Birrell 	for (i = 0; i < argc; i++, pnp = dnp) {
2036ff6d951SJohn Birrell 		if ((dnp = dt_node_xalloc(pvp->pv_hdl, DT_NODE_TYPE)) == NULL)
2046ff6d951SJohn Birrell 			return (NULL);
2056ff6d951SJohn Birrell 
2066ff6d951SJohn Birrell 		dnp->dn_link = pvp->pv_nodes;
2076ff6d951SJohn Birrell 		pvp->pv_nodes = dnp;
2086ff6d951SJohn Birrell 
2096ff6d951SJohn Birrell 		if (args == NULL)
2106ff6d951SJohn Birrell 			args = dnp;
2116ff6d951SJohn Birrell 		else
2126ff6d951SJohn Birrell 			pnp->dn_list = dnp;
2136ff6d951SJohn Birrell 	}
2146ff6d951SJohn Birrell 
2156ff6d951SJohn Birrell 	return (args);
2166ff6d951SJohn Birrell }
2176ff6d951SJohn Birrell 
2186ff6d951SJohn Birrell static size_t
dt_probe_keylen(const dtrace_probedesc_t * pdp)2196ff6d951SJohn Birrell dt_probe_keylen(const dtrace_probedesc_t *pdp)
2206ff6d951SJohn Birrell {
2216ff6d951SJohn Birrell 	return (strlen(pdp->dtpd_mod) + 1 +
2226ff6d951SJohn Birrell 	    strlen(pdp->dtpd_func) + 1 + strlen(pdp->dtpd_name) + 1);
2236ff6d951SJohn Birrell }
2246ff6d951SJohn Birrell 
2256ff6d951SJohn Birrell static char *
dt_probe_key(const dtrace_probedesc_t * pdp,char * s)2266ff6d951SJohn Birrell dt_probe_key(const dtrace_probedesc_t *pdp, char *s)
2276ff6d951SJohn Birrell {
2286ff6d951SJohn Birrell 	(void) snprintf(s, INT_MAX, "%s:%s:%s",
2296ff6d951SJohn Birrell 	    pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
2306ff6d951SJohn Birrell 	return (s);
2316ff6d951SJohn Birrell }
2326ff6d951SJohn Birrell 
2336ff6d951SJohn Birrell /*
2346ff6d951SJohn Birrell  * If a probe was discovered from the kernel, ask dtrace(7D) for a description
2356ff6d951SJohn Birrell  * of each of its arguments, including native and translated types.
2366ff6d951SJohn Birrell  */
2376ff6d951SJohn Birrell static dt_probe_t *
dt_probe_discover(dt_provider_t * pvp,const dtrace_probedesc_t * pdp)2386ff6d951SJohn Birrell dt_probe_discover(dt_provider_t *pvp, const dtrace_probedesc_t *pdp)
2396ff6d951SJohn Birrell {
2406ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = pvp->pv_hdl;
2416ff6d951SJohn Birrell 	char *name = dt_probe_key(pdp, alloca(dt_probe_keylen(pdp)));
2426ff6d951SJohn Birrell 
2436ff6d951SJohn Birrell 	dt_node_t *xargs, *nargs;
2446ff6d951SJohn Birrell 	dt_ident_t *idp;
2456ff6d951SJohn Birrell 	dt_probe_t *prp;
2466ff6d951SJohn Birrell 
2476ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
2486ff6d951SJohn Birrell 	int i, nc, xc;
2496ff6d951SJohn Birrell 
2506ff6d951SJohn Birrell 	int adc = _dtrace_argmax;
2516ff6d951SJohn Birrell 	dtrace_argdesc_t *adv = alloca(sizeof (dtrace_argdesc_t) * adc);
2526ff6d951SJohn Birrell 	dtrace_argdesc_t *adp = adv;
2536ff6d951SJohn Birrell 
2546ff6d951SJohn Birrell 	assert(strcmp(pvp->pv_desc.dtvd_name, pdp->dtpd_provider) == 0);
2556ff6d951SJohn Birrell 	assert(pdp->dtpd_id != DTRACE_IDNONE);
2566ff6d951SJohn Birrell 
2576ff6d951SJohn Birrell 	dt_dprintf("discovering probe %s:%s id=%d\n",
2586ff6d951SJohn Birrell 	    pvp->pv_desc.dtvd_name, name, pdp->dtpd_id);
2596ff6d951SJohn Birrell 
2606ff6d951SJohn Birrell 	for (nc = -1, i = 0; i < adc; i++, adp++) {
2616ff6d951SJohn Birrell 		bzero(adp, sizeof (dtrace_argdesc_t));
2626ff6d951SJohn Birrell 		adp->dtargd_ndx = i;
2636ff6d951SJohn Birrell 		adp->dtargd_id = pdp->dtpd_id;
2646ff6d951SJohn Birrell 
2656ff6d951SJohn Birrell 		if (dt_ioctl(dtp, DTRACEIOC_PROBEARG, adp) != 0) {
2666ff6d951SJohn Birrell 			(void) dt_set_errno(dtp, errno);
2676ff6d951SJohn Birrell 			return (NULL);
2686ff6d951SJohn Birrell 		}
2696ff6d951SJohn Birrell 
2706ff6d951SJohn Birrell 		if (adp->dtargd_ndx == DTRACE_ARGNONE)
2716ff6d951SJohn Birrell 			break; /* all argument descs have been retrieved */
2726ff6d951SJohn Birrell 
2736ff6d951SJohn Birrell 		nc = MAX(nc, adp->dtargd_mapping);
2746ff6d951SJohn Birrell 	}
2756ff6d951SJohn Birrell 
2766ff6d951SJohn Birrell 	xc = i;
2776ff6d951SJohn Birrell 	nc++;
2786ff6d951SJohn Birrell 
2796ff6d951SJohn Birrell 	/*
2808e648814SRui Paulo 	 * The pid provider believes in giving the kernel a break. No reason to
2818e648814SRui Paulo 	 * give the kernel all the ctf containers that we're keeping ourselves
2828e648814SRui Paulo 	 * just to get it back from it. So if we're coming from a pid provider
2838e648814SRui Paulo 	 * probe and the kernel gave us no argument information we'll get some
2848e648814SRui Paulo 	 * here. If for some crazy reason the kernel knows about our userland
2858e648814SRui Paulo 	 * types then we just ignore this.
2868e648814SRui Paulo 	 */
2878e648814SRui Paulo 	if (xc == 0 && nc == 0 &&
2888e648814SRui Paulo 	    strncmp(pvp->pv_desc.dtvd_name, "pid", 3) == 0) {
2898e648814SRui Paulo 		nc = adc;
2908e648814SRui Paulo 		dt_pid_get_types(dtp, pdp, adv, &nc);
2918e648814SRui Paulo 		xc = nc;
2928e648814SRui Paulo 	}
2938e648814SRui Paulo 
2948e648814SRui Paulo 	/*
2956ff6d951SJohn Birrell 	 * Now that we have discovered the number of native and translated
2966ff6d951SJohn Birrell 	 * arguments from the argument descriptions, allocate a new probe ident
2976ff6d951SJohn Birrell 	 * and corresponding dt_probe_t and hash it into the provider.
2986ff6d951SJohn Birrell 	 */
2996ff6d951SJohn Birrell 	xargs = dt_probe_alloc_args(pvp, xc);
3006ff6d951SJohn Birrell 	nargs = dt_probe_alloc_args(pvp, nc);
3016ff6d951SJohn Birrell 
3026ff6d951SJohn Birrell 	if ((xc != 0 && xargs == NULL) || (nc != 0 && nargs == NULL))
3036ff6d951SJohn Birrell 		return (NULL); /* dt_errno is set for us */
3046ff6d951SJohn Birrell 
3056ff6d951SJohn Birrell 	idp = dt_ident_create(name, DT_IDENT_PROBE,
3066ff6d951SJohn Birrell 	    DT_IDFLG_ORPHAN, pdp->dtpd_id, _dtrace_defattr, 0,
3076ff6d951SJohn Birrell 	    &dt_idops_probe, NULL, dtp->dt_gen);
3086ff6d951SJohn Birrell 
3096ff6d951SJohn Birrell 	if (idp == NULL) {
3106ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EDT_NOMEM);
3116ff6d951SJohn Birrell 		return (NULL);
3126ff6d951SJohn Birrell 	}
3136ff6d951SJohn Birrell 
3146ff6d951SJohn Birrell 	if ((prp = dt_probe_create(dtp, idp, 2,
3156ff6d951SJohn Birrell 	    nargs, nc, xargs, xc)) == NULL) {
3166ff6d951SJohn Birrell 		dt_ident_destroy(idp);
3176ff6d951SJohn Birrell 		return (NULL);
3186ff6d951SJohn Birrell 	}
3196ff6d951SJohn Birrell 
3206ff6d951SJohn Birrell 	dt_probe_declare(pvp, prp);
3216ff6d951SJohn Birrell 
3226ff6d951SJohn Birrell 	/*
3236ff6d951SJohn Birrell 	 * Once our new dt_probe_t is fully constructed, iterate over the
3246ff6d951SJohn Birrell 	 * cached argument descriptions and assign types to prp->pr_nargv[]
3256ff6d951SJohn Birrell 	 * and prp->pr_xargv[] and assign mappings to prp->pr_mapping[].
3266ff6d951SJohn Birrell 	 */
3276ff6d951SJohn Birrell 	for (adp = adv, i = 0; i < xc; i++, adp++) {
3286ff6d951SJohn Birrell 		if (dtrace_type_strcompile(dtp,
3296ff6d951SJohn Birrell 		    adp->dtargd_native, &dtt) != 0) {
3306ff6d951SJohn Birrell 			dt_dprintf("failed to resolve input type %s "
3316ff6d951SJohn Birrell 			    "for %s:%s arg #%d: %s\n", adp->dtargd_native,
3326ff6d951SJohn Birrell 			    pvp->pv_desc.dtvd_name, name, i + 1,
3336ff6d951SJohn Birrell 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
3346ff6d951SJohn Birrell 
3356ff6d951SJohn Birrell 			dtt.dtt_object = NULL;
3366ff6d951SJohn Birrell 			dtt.dtt_ctfp = NULL;
3376ff6d951SJohn Birrell 			dtt.dtt_type = CTF_ERR;
3386ff6d951SJohn Birrell 		} else {
3396ff6d951SJohn Birrell 			dt_node_type_assign(prp->pr_nargv[adp->dtargd_mapping],
3408e648814SRui Paulo 			    dtt.dtt_ctfp, dtt.dtt_type,
3418e648814SRui Paulo 			    dtt.dtt_flags & DTT_FL_USER ? B_TRUE : B_FALSE);
3426ff6d951SJohn Birrell 		}
3436ff6d951SJohn Birrell 
3446ff6d951SJohn Birrell 		if (dtt.dtt_type != CTF_ERR && (adp->dtargd_xlate[0] == '\0' ||
3456ff6d951SJohn Birrell 		    strcmp(adp->dtargd_native, adp->dtargd_xlate) == 0)) {
3466ff6d951SJohn Birrell 			dt_node_type_propagate(prp->pr_nargv[
3476ff6d951SJohn Birrell 			    adp->dtargd_mapping], prp->pr_xargv[i]);
3486ff6d951SJohn Birrell 		} else if (dtrace_type_strcompile(dtp,
3496ff6d951SJohn Birrell 		    adp->dtargd_xlate, &dtt) != 0) {
3506ff6d951SJohn Birrell 			dt_dprintf("failed to resolve output type %s "
3516ff6d951SJohn Birrell 			    "for %s:%s arg #%d: %s\n", adp->dtargd_xlate,
3526ff6d951SJohn Birrell 			    pvp->pv_desc.dtvd_name, name, i + 1,
3536ff6d951SJohn Birrell 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
3546ff6d951SJohn Birrell 
3556ff6d951SJohn Birrell 			dtt.dtt_object = NULL;
3566ff6d951SJohn Birrell 			dtt.dtt_ctfp = NULL;
3576ff6d951SJohn Birrell 			dtt.dtt_type = CTF_ERR;
3586ff6d951SJohn Birrell 		} else {
3596ff6d951SJohn Birrell 			dt_node_type_assign(prp->pr_xargv[i],
3608e648814SRui Paulo 			    dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
3616ff6d951SJohn Birrell 		}
3626ff6d951SJohn Birrell 
3636ff6d951SJohn Birrell 		prp->pr_mapping[i] = adp->dtargd_mapping;
3646ff6d951SJohn Birrell 		prp->pr_argv[i] = dtt;
3656ff6d951SJohn Birrell 	}
3666ff6d951SJohn Birrell 
3676ff6d951SJohn Birrell 	return (prp);
3686ff6d951SJohn Birrell }
3696ff6d951SJohn Birrell 
3706ff6d951SJohn Birrell /*
3716ff6d951SJohn Birrell  * Lookup a probe declaration based on a known provider and full or partially
3726ff6d951SJohn Birrell  * specified module, function, and name.  If the probe is not known to us yet,
3736ff6d951SJohn Birrell  * ask dtrace(7D) to match the description and then cache any useful results.
3746ff6d951SJohn Birrell  */
3756ff6d951SJohn Birrell dt_probe_t *
dt_probe_lookup(dt_provider_t * pvp,const char * s)3766ff6d951SJohn Birrell dt_probe_lookup(dt_provider_t *pvp, const char *s)
3776ff6d951SJohn Birrell {
3786ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = pvp->pv_hdl;
3796ff6d951SJohn Birrell 	dtrace_probedesc_t pd;
3806ff6d951SJohn Birrell 	dt_ident_t *idp;
3816ff6d951SJohn Birrell 	size_t keylen;
3826ff6d951SJohn Birrell 	char *key;
3836ff6d951SJohn Birrell 
3846ff6d951SJohn Birrell 	if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, s, &pd) != 0)
3856ff6d951SJohn Birrell 		return (NULL); /* dt_errno is set for us */
3866ff6d951SJohn Birrell 
3876ff6d951SJohn Birrell 	keylen = dt_probe_keylen(&pd);
3886ff6d951SJohn Birrell 	key = dt_probe_key(&pd, alloca(keylen));
3896ff6d951SJohn Birrell 
3906ff6d951SJohn Birrell 	/*
3916ff6d951SJohn Birrell 	 * If the probe is already declared, then return the dt_probe_t from
3926ff6d951SJohn Birrell 	 * the existing identifier.  This could come from a static declaration
3936ff6d951SJohn Birrell 	 * or it could have been cached from an earlier call to this function.
3946ff6d951SJohn Birrell 	 */
3956ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
3966ff6d951SJohn Birrell 		return (idp->di_data);
3976ff6d951SJohn Birrell 
3986ff6d951SJohn Birrell 	/*
3996ff6d951SJohn Birrell 	 * If the probe isn't known, use the probe description computed above
4006ff6d951SJohn Birrell 	 * to ask dtrace(7D) to find the first matching probe.
4016ff6d951SJohn Birrell 	 */
4026ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_PROBEMATCH, &pd) == 0)
4036ff6d951SJohn Birrell 		return (dt_probe_discover(pvp, &pd));
4046ff6d951SJohn Birrell 
4056ff6d951SJohn Birrell 	if (errno == ESRCH || errno == EBADF)
4066ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EDT_NOPROBE);
4076ff6d951SJohn Birrell 	else
4086ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, errno);
4096ff6d951SJohn Birrell 
4106ff6d951SJohn Birrell 	return (NULL);
4116ff6d951SJohn Birrell }
4126ff6d951SJohn Birrell 
4136ff6d951SJohn Birrell 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)4146ff6d951SJohn Birrell dt_probe_create(dtrace_hdl_t *dtp, dt_ident_t *idp, int protoc,
4156ff6d951SJohn Birrell     dt_node_t *nargs, uint_t nargc, dt_node_t *xargs, uint_t xargc)
4166ff6d951SJohn Birrell {
4176ff6d951SJohn Birrell 	dt_module_t *dmp;
4186ff6d951SJohn Birrell 	dt_probe_t *prp;
4196ff6d951SJohn Birrell 	const char *p;
4206ff6d951SJohn Birrell 	uint_t i;
4216ff6d951SJohn Birrell 
4226ff6d951SJohn Birrell 	assert(idp->di_kind == DT_IDENT_PROBE);
4236ff6d951SJohn Birrell 	assert(idp->di_data == NULL);
4246ff6d951SJohn Birrell 
4256ff6d951SJohn Birrell 	/*
4266ff6d951SJohn Birrell 	 * If only a single prototype is given, set xargc/s to nargc/s to
4276ff6d951SJohn Birrell 	 * simplify subsequent use.  Note that we can have one or both of nargs
4286ff6d951SJohn Birrell 	 * and xargs be specified but set to NULL, indicating a void prototype.
4296ff6d951SJohn Birrell 	 */
4306ff6d951SJohn Birrell 	if (protoc < 2) {
4316ff6d951SJohn Birrell 		assert(xargs == NULL);
4326ff6d951SJohn Birrell 		assert(xargc == 0);
4336ff6d951SJohn Birrell 		xargs = nargs;
4346ff6d951SJohn Birrell 		xargc = nargc;
4356ff6d951SJohn Birrell 	}
4366ff6d951SJohn Birrell 
4376ff6d951SJohn Birrell 	if ((prp = dt_alloc(dtp, sizeof (dt_probe_t))) == NULL)
4386ff6d951SJohn Birrell 		return (NULL);
4396ff6d951SJohn Birrell 
4406ff6d951SJohn Birrell 	prp->pr_pvp = NULL;
4416ff6d951SJohn Birrell 	prp->pr_ident = idp;
4426ff6d951SJohn Birrell 
4436ff6d951SJohn Birrell 	p = strrchr(idp->di_name, ':');
4446ff6d951SJohn Birrell 	assert(p != NULL);
4456ff6d951SJohn Birrell 	prp->pr_name = p + 1;
4466ff6d951SJohn Birrell 
4476ff6d951SJohn Birrell 	prp->pr_nargs = nargs;
4486ff6d951SJohn Birrell 	prp->pr_nargv = dt_alloc(dtp, sizeof (dt_node_t *) * nargc);
4496ff6d951SJohn Birrell 	prp->pr_nargc = nargc;
4506ff6d951SJohn Birrell 	prp->pr_xargs = xargs;
4516ff6d951SJohn Birrell 	prp->pr_xargv = dt_alloc(dtp, sizeof (dt_node_t *) * xargc);
4526ff6d951SJohn Birrell 	prp->pr_xargc = xargc;
4536ff6d951SJohn Birrell 	prp->pr_mapping = dt_alloc(dtp, sizeof (uint8_t) * xargc);
4546ff6d951SJohn Birrell 	prp->pr_inst = NULL;
4556ff6d951SJohn Birrell 	prp->pr_argv = dt_alloc(dtp, sizeof (dtrace_typeinfo_t) * xargc);
4566ff6d951SJohn Birrell 	prp->pr_argc = xargc;
4576ff6d951SJohn Birrell 
4586ff6d951SJohn Birrell 	if ((prp->pr_nargc != 0 && prp->pr_nargv == NULL) ||
4596ff6d951SJohn Birrell 	    (prp->pr_xargc != 0 && prp->pr_xargv == NULL) ||
4606ff6d951SJohn Birrell 	    (prp->pr_xargc != 0 && prp->pr_mapping == NULL) ||
4616ff6d951SJohn Birrell 	    (prp->pr_argc != 0 && prp->pr_argv == NULL)) {
4626ff6d951SJohn Birrell 		dt_probe_destroy(prp);
4636ff6d951SJohn Birrell 		return (NULL);
4646ff6d951SJohn Birrell 	}
4656ff6d951SJohn Birrell 
4666ff6d951SJohn Birrell 	for (i = 0; i < xargc; i++, xargs = xargs->dn_list) {
4676ff6d951SJohn Birrell 		if (xargs->dn_string != NULL)
4686ff6d951SJohn Birrell 			prp->pr_mapping[i] = dt_probe_argmap(xargs, nargs);
4696ff6d951SJohn Birrell 		else
4706ff6d951SJohn Birrell 			prp->pr_mapping[i] = i;
4716ff6d951SJohn Birrell 
4726ff6d951SJohn Birrell 		prp->pr_xargv[i] = xargs;
4736ff6d951SJohn Birrell 
4746ff6d951SJohn Birrell 		if ((dmp = dt_module_lookup_by_ctf(dtp,
4756ff6d951SJohn Birrell 		    xargs->dn_ctfp)) != NULL)
4766ff6d951SJohn Birrell 			prp->pr_argv[i].dtt_object = dmp->dm_name;
4776ff6d951SJohn Birrell 		else
4786ff6d951SJohn Birrell 			prp->pr_argv[i].dtt_object = NULL;
4796ff6d951SJohn Birrell 
4806ff6d951SJohn Birrell 		prp->pr_argv[i].dtt_ctfp = xargs->dn_ctfp;
4816ff6d951SJohn Birrell 		prp->pr_argv[i].dtt_type = xargs->dn_type;
4826ff6d951SJohn Birrell 	}
4836ff6d951SJohn Birrell 
4846ff6d951SJohn Birrell 	for (i = 0; i < nargc; i++, nargs = nargs->dn_list)
4856ff6d951SJohn Birrell 		prp->pr_nargv[i] = nargs;
4866ff6d951SJohn Birrell 
4876ff6d951SJohn Birrell 	idp->di_data = prp;
4886ff6d951SJohn Birrell 	return (prp);
4896ff6d951SJohn Birrell }
4906ff6d951SJohn Birrell 
4916ff6d951SJohn Birrell void
dt_probe_declare(dt_provider_t * pvp,dt_probe_t * prp)4926ff6d951SJohn Birrell dt_probe_declare(dt_provider_t *pvp, dt_probe_t *prp)
4936ff6d951SJohn Birrell {
4946ff6d951SJohn Birrell 	assert(prp->pr_ident->di_kind == DT_IDENT_PROBE);
4956ff6d951SJohn Birrell 	assert(prp->pr_ident->di_data == prp);
4966ff6d951SJohn Birrell 	assert(prp->pr_pvp == NULL);
4976ff6d951SJohn Birrell 
4986ff6d951SJohn Birrell 	if (prp->pr_xargs != prp->pr_nargs)
4996ff6d951SJohn Birrell 		pvp->pv_flags &= ~DT_PROVIDER_INTF;
5006ff6d951SJohn Birrell 
5016ff6d951SJohn Birrell 	prp->pr_pvp = pvp;
5026ff6d951SJohn Birrell 	dt_idhash_xinsert(pvp->pv_probes, prp->pr_ident);
5036ff6d951SJohn Birrell }
5046ff6d951SJohn Birrell 
5056ff6d951SJohn Birrell void
dt_probe_destroy(dt_probe_t * prp)5066ff6d951SJohn Birrell dt_probe_destroy(dt_probe_t *prp)
5076ff6d951SJohn Birrell {
5086ff6d951SJohn Birrell 	dt_probe_instance_t *pip, *pip_next;
5096ff6d951SJohn Birrell 	dtrace_hdl_t *dtp;
5106ff6d951SJohn Birrell 
5116ff6d951SJohn Birrell 	if (prp->pr_pvp != NULL)
5126ff6d951SJohn Birrell 		dtp = prp->pr_pvp->pv_hdl;
5136ff6d951SJohn Birrell 	else
5146ff6d951SJohn Birrell 		dtp = yypcb->pcb_hdl;
5156ff6d951SJohn Birrell 
5166ff6d951SJohn Birrell 	dt_node_list_free(&prp->pr_nargs);
5176ff6d951SJohn Birrell 	dt_node_list_free(&prp->pr_xargs);
5186ff6d951SJohn Birrell 
5196ff6d951SJohn Birrell 	dt_free(dtp, prp->pr_nargv);
5206ff6d951SJohn Birrell 	dt_free(dtp, prp->pr_xargv);
5216ff6d951SJohn Birrell 
5226ff6d951SJohn Birrell 	for (pip = prp->pr_inst; pip != NULL; pip = pip_next) {
5236ff6d951SJohn Birrell 		pip_next = pip->pi_next;
524af62c4ddSMark Johnston 		dt_free(dtp, pip->pi_rname);
525af62c4ddSMark Johnston 		dt_free(dtp, pip->pi_fname);
5266ff6d951SJohn Birrell 		dt_free(dtp, pip->pi_offs);
5276ff6d951SJohn Birrell 		dt_free(dtp, pip->pi_enoffs);
5286ff6d951SJohn Birrell 		dt_free(dtp, pip);
5296ff6d951SJohn Birrell 	}
5306ff6d951SJohn Birrell 
5316ff6d951SJohn Birrell 	dt_free(dtp, prp->pr_mapping);
5326ff6d951SJohn Birrell 	dt_free(dtp, prp->pr_argv);
5336ff6d951SJohn Birrell 	dt_free(dtp, prp);
5346ff6d951SJohn Birrell }
5356ff6d951SJohn Birrell 
5366ff6d951SJohn Birrell int
dt_probe_define(dt_provider_t * pvp,dt_probe_t * prp,const char * fname,const char * rname,uint32_t offset,int isenabled)5376ff6d951SJohn Birrell dt_probe_define(dt_provider_t *pvp, dt_probe_t *prp,
5386ff6d951SJohn Birrell     const char *fname, const char *rname, uint32_t offset, int isenabled)
5396ff6d951SJohn Birrell {
5406ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = pvp->pv_hdl;
5416ff6d951SJohn Birrell 	dt_probe_instance_t *pip;
5426ff6d951SJohn Birrell 	uint32_t **offs;
5436ff6d951SJohn Birrell 	uint_t *noffs, *maxoffs;
5446ff6d951SJohn Birrell 
5456ff6d951SJohn Birrell 	assert(fname != NULL);
5466ff6d951SJohn Birrell 
5476ff6d951SJohn Birrell 	for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
5486ff6d951SJohn Birrell 		if (strcmp(pip->pi_fname, fname) == 0 &&
549e801af6fSMark Johnston 		    strcmp(pip->pi_rname, rname) == 0)
5506ff6d951SJohn Birrell 			break;
5516ff6d951SJohn Birrell 	}
5526ff6d951SJohn Birrell 
5536ff6d951SJohn Birrell 	if (pip == NULL) {
5546ff6d951SJohn Birrell 		if ((pip = dt_zalloc(dtp, sizeof (*pip))) == NULL)
5556ff6d951SJohn Birrell 			return (-1);
5566ff6d951SJohn Birrell 
557af62c4ddSMark Johnston 		if ((pip->pi_offs = dt_zalloc(dtp, sizeof (uint32_t))) == NULL)
558af62c4ddSMark Johnston 			goto nomem;
5596ff6d951SJohn Birrell 
5606ff6d951SJohn Birrell 		if ((pip->pi_enoffs = dt_zalloc(dtp,
561af62c4ddSMark Johnston 		    sizeof (uint32_t))) == NULL)
562af62c4ddSMark Johnston 			goto nomem;
5636ff6d951SJohn Birrell 
564af62c4ddSMark Johnston 		if ((pip->pi_fname = strdup(fname)) == NULL)
565af62c4ddSMark Johnston 			goto nomem;
566af62c4ddSMark Johnston 
567e801af6fSMark Johnston 		if ((pip->pi_rname = strdup(rname)) == NULL)
568af62c4ddSMark Johnston 			goto nomem;
5696ff6d951SJohn Birrell 
5706ff6d951SJohn Birrell 		pip->pi_noffs = 0;
5716ff6d951SJohn Birrell 		pip->pi_maxoffs = 1;
5726ff6d951SJohn Birrell 		pip->pi_nenoffs = 0;
5736ff6d951SJohn Birrell 		pip->pi_maxenoffs = 1;
5746ff6d951SJohn Birrell 
5756ff6d951SJohn Birrell 		pip->pi_next = prp->pr_inst;
5766ff6d951SJohn Birrell 
5776ff6d951SJohn Birrell 		prp->pr_inst = pip;
5786ff6d951SJohn Birrell 	}
5796ff6d951SJohn Birrell 
5806ff6d951SJohn Birrell 	if (isenabled) {
5816ff6d951SJohn Birrell 		offs = &pip->pi_enoffs;
5826ff6d951SJohn Birrell 		noffs = &pip->pi_nenoffs;
5836ff6d951SJohn Birrell 		maxoffs = &pip->pi_maxenoffs;
5846ff6d951SJohn Birrell 	} else {
5856ff6d951SJohn Birrell 		offs = &pip->pi_offs;
5866ff6d951SJohn Birrell 		noffs = &pip->pi_noffs;
5876ff6d951SJohn Birrell 		maxoffs = &pip->pi_maxoffs;
5886ff6d951SJohn Birrell 	}
5896ff6d951SJohn Birrell 
5906ff6d951SJohn Birrell 	if (*noffs == *maxoffs) {
5916ff6d951SJohn Birrell 		uint_t new_max = *maxoffs * 2;
5926ff6d951SJohn Birrell 		uint32_t *new_offs = dt_alloc(dtp, sizeof (uint32_t) * new_max);
5936ff6d951SJohn Birrell 
5946ff6d951SJohn Birrell 		if (new_offs == NULL)
5956ff6d951SJohn Birrell 			return (-1);
5966ff6d951SJohn Birrell 
5976ff6d951SJohn Birrell 		bcopy(*offs, new_offs, sizeof (uint32_t) * *maxoffs);
5986ff6d951SJohn Birrell 
5996ff6d951SJohn Birrell 		dt_free(dtp, *offs);
6006ff6d951SJohn Birrell 		*maxoffs = new_max;
6016ff6d951SJohn Birrell 		*offs = new_offs;
6026ff6d951SJohn Birrell 	}
6036ff6d951SJohn Birrell 
6046ff6d951SJohn Birrell 	dt_dprintf("defined probe %s %s:%s %s() +0x%x (%s)\n",
6056ff6d951SJohn Birrell 	    isenabled ? "(is-enabled)" : "",
6066ff6d951SJohn Birrell 	    pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, fname, offset,
607e801af6fSMark Johnston 	    rname);
6086ff6d951SJohn Birrell 
6096ff6d951SJohn Birrell 	assert(*noffs < *maxoffs);
6106ff6d951SJohn Birrell 	(*offs)[(*noffs)++] = offset;
6116ff6d951SJohn Birrell 
6126ff6d951SJohn Birrell 	return (0);
613af62c4ddSMark Johnston 
614af62c4ddSMark Johnston nomem:
615af62c4ddSMark Johnston 	dt_free(dtp, pip->pi_fname);
616af62c4ddSMark Johnston 	dt_free(dtp, pip->pi_enoffs);
617af62c4ddSMark Johnston 	dt_free(dtp, pip->pi_offs);
618af62c4ddSMark Johnston 	dt_free(dtp, pip);
619af62c4ddSMark Johnston 	return (dt_set_errno(dtp, EDT_NOMEM));
6206ff6d951SJohn Birrell }
6216ff6d951SJohn Birrell 
6226ff6d951SJohn Birrell /*
6236ff6d951SJohn Birrell  * Lookup the dynamic translator type tag for the specified probe argument and
6246ff6d951SJohn Birrell  * assign the type to the specified node.  If the type is not yet defined, add
6256ff6d951SJohn Birrell  * it to the "D" module's type container as a typedef for an unknown type.
6266ff6d951SJohn Birrell  */
6276ff6d951SJohn Birrell dt_node_t *
dt_probe_tag(dt_probe_t * prp,uint_t argn,dt_node_t * dnp)6286ff6d951SJohn Birrell dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp)
6296ff6d951SJohn Birrell {
6306ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = prp->pr_pvp->pv_hdl;
6316ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
6326ff6d951SJohn Birrell 	size_t len;
6336ff6d951SJohn Birrell 	char *tag;
6346ff6d951SJohn Birrell 
6356ff6d951SJohn Birrell 	len = snprintf(NULL, 0, "__dtrace_%s___%s_arg%u",
6366ff6d951SJohn Birrell 	    prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
6376ff6d951SJohn Birrell 
6386ff6d951SJohn Birrell 	tag = alloca(len + 1);
6396ff6d951SJohn Birrell 
6406ff6d951SJohn Birrell 	(void) snprintf(tag, len + 1, "__dtrace_%s___%s_arg%u",
6416ff6d951SJohn Birrell 	    prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
6426ff6d951SJohn Birrell 
6436ff6d951SJohn Birrell 	if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS, tag, &dtt) != 0) {
6446ff6d951SJohn Birrell 		dtt.dtt_object = DTRACE_OBJ_DDEFS;
6456ff6d951SJohn Birrell 		dtt.dtt_ctfp = DT_DYN_CTFP(dtp);
6466ff6d951SJohn Birrell 		dtt.dtt_type = ctf_add_typedef(DT_DYN_CTFP(dtp),
6476ff6d951SJohn Birrell 		    CTF_ADD_ROOT, tag, DT_DYN_TYPE(dtp));
6486ff6d951SJohn Birrell 
6496ff6d951SJohn Birrell 		if (dtt.dtt_type == CTF_ERR ||
6506ff6d951SJohn Birrell 		    ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
6516ff6d951SJohn Birrell 			xyerror(D_UNKNOWN, "cannot define type %s: %s\n",
6526ff6d951SJohn Birrell 			    tag, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
6536ff6d951SJohn Birrell 		}
6546ff6d951SJohn Birrell 	}
6556ff6d951SJohn Birrell 
6566ff6d951SJohn Birrell 	bzero(dnp, sizeof (dt_node_t));
6576ff6d951SJohn Birrell 	dnp->dn_kind = DT_NODE_TYPE;
6586ff6d951SJohn Birrell 
6598e648814SRui Paulo 	dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
6606ff6d951SJohn Birrell 	dt_node_attr_assign(dnp, _dtrace_defattr);
6616ff6d951SJohn Birrell 
6626ff6d951SJohn Birrell 	return (dnp);
6636ff6d951SJohn Birrell }
6646ff6d951SJohn Birrell 
6656ff6d951SJohn Birrell /*ARGSUSED*/
6666ff6d951SJohn Birrell static int
dt_probe_desc(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * arg)6676ff6d951SJohn Birrell dt_probe_desc(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
6686ff6d951SJohn Birrell {
6696ff6d951SJohn Birrell 	if (((dtrace_probedesc_t *)arg)->dtpd_id == DTRACE_IDNONE) {
6706ff6d951SJohn Birrell 		bcopy(pdp, arg, sizeof (dtrace_probedesc_t));
6716ff6d951SJohn Birrell 		return (0);
6726ff6d951SJohn Birrell 	}
6736ff6d951SJohn Birrell 
6746ff6d951SJohn Birrell 	return (1);
6756ff6d951SJohn Birrell }
6766ff6d951SJohn Birrell 
6776ff6d951SJohn Birrell dt_probe_t *
dt_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)6786ff6d951SJohn Birrell dt_probe_info(dtrace_hdl_t *dtp,
6796ff6d951SJohn Birrell     const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
6806ff6d951SJohn Birrell {
6816ff6d951SJohn Birrell 	int m_is_glob = pdp->dtpd_mod[0] == '\0' || strisglob(pdp->dtpd_mod);
6826ff6d951SJohn Birrell 	int f_is_glob = pdp->dtpd_func[0] == '\0' || strisglob(pdp->dtpd_func);
6836ff6d951SJohn Birrell 	int n_is_glob = pdp->dtpd_name[0] == '\0' || strisglob(pdp->dtpd_name);
6846ff6d951SJohn Birrell 
6856ff6d951SJohn Birrell 	dt_probe_t *prp = NULL;
6866ff6d951SJohn Birrell 	const dtrace_pattr_t *pap;
6876ff6d951SJohn Birrell 	dt_provider_t *pvp;
6886ff6d951SJohn Birrell 	dt_ident_t *idp;
6896ff6d951SJohn Birrell 
6906ff6d951SJohn Birrell 	/*
6916ff6d951SJohn Birrell 	 * Attempt to lookup the probe in our existing cache for this provider.
6926ff6d951SJohn Birrell 	 * If none is found and an explicit probe ID was specified, discover
6936ff6d951SJohn Birrell 	 * that specific probe and cache its description and arguments.
6946ff6d951SJohn Birrell 	 */
6956ff6d951SJohn Birrell 	if ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) != NULL) {
6966ff6d951SJohn Birrell 		size_t keylen = dt_probe_keylen(pdp);
6976ff6d951SJohn Birrell 		char *key = dt_probe_key(pdp, alloca(keylen));
6986ff6d951SJohn Birrell 
6996ff6d951SJohn Birrell 		if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
7006ff6d951SJohn Birrell 			prp = idp->di_data;
7016ff6d951SJohn Birrell 		else if (pdp->dtpd_id != DTRACE_IDNONE)
7026ff6d951SJohn Birrell 			prp = dt_probe_discover(pvp, pdp);
703*2179a159SChristos Margiolis 
704*2179a159SChristos Margiolis 		if (strcmp(pvp->pv_desc.dtvd_name, "kinst") == 0) {
705*2179a159SChristos Margiolis 			dtrace_kinst_probedesc_t pd;
706*2179a159SChristos Margiolis 
707*2179a159SChristos Margiolis 			if (dtp->dt_kinstfd == -1) {
708*2179a159SChristos Margiolis 				int fd;
709*2179a159SChristos Margiolis 
710*2179a159SChristos Margiolis 				fd = open("/dev/dtrace/kinst", O_WRONLY);
711*2179a159SChristos Margiolis 				if (fd < 0) {
712*2179a159SChristos Margiolis 					(void) dt_set_errno(dtp, errno);
713*2179a159SChristos Margiolis 					return (NULL);
714*2179a159SChristos Margiolis 				}
715*2179a159SChristos Margiolis 				dtp->dt_kinstfd = fd;
716*2179a159SChristos Margiolis 			}
717*2179a159SChristos Margiolis 			memset(&pd, 0, sizeof(pd));
718*2179a159SChristos Margiolis 			strlcpy(pd.kpd_func, pdp->dtpd_func,
719*2179a159SChristos Margiolis 			    sizeof (pd.kpd_func));
720*2179a159SChristos Margiolis 
721*2179a159SChristos Margiolis 			if (n_is_glob)
722*2179a159SChristos Margiolis 				pd.kpd_off = -1;
723*2179a159SChristos Margiolis 			else
724*2179a159SChristos Margiolis 				pd.kpd_off = strtol(pdp->dtpd_name, NULL, 10);
725*2179a159SChristos Margiolis 			if (ioctl(dtp->dt_kinstfd, KINSTIOC_MAKEPROBE, &pd) !=
726*2179a159SChristos Margiolis 			    0) {
727*2179a159SChristos Margiolis 				(void) dt_set_errno(dtp, errno);
728*2179a159SChristos Margiolis 				return (NULL);
729*2179a159SChristos Margiolis 			}
730*2179a159SChristos Margiolis 		}
7316ff6d951SJohn Birrell 	}
7326ff6d951SJohn Birrell 
7336ff6d951SJohn Birrell 	/*
7346ff6d951SJohn Birrell 	 * If no probe was found in our cache, convert the caller's partial
7356ff6d951SJohn Birrell 	 * probe description into a fully-formed matching probe description by
7366ff6d951SJohn Birrell 	 * iterating over up to at most two probes that match 'pdp'.  We then
7376ff6d951SJohn Birrell 	 * call dt_probe_discover() on the resulting probe identifier.
7386ff6d951SJohn Birrell 	 */
7396ff6d951SJohn Birrell 	if (prp == NULL) {
7406ff6d951SJohn Birrell 		dtrace_probedesc_t pd;
7416ff6d951SJohn Birrell 		int m;
7426ff6d951SJohn Birrell 
7436ff6d951SJohn Birrell 		bzero(&pd, sizeof (pd));
7446ff6d951SJohn Birrell 		pd.dtpd_id = DTRACE_IDNONE;
7456ff6d951SJohn Birrell 
7466ff6d951SJohn Birrell 		/*
7476ff6d951SJohn Birrell 		 * Call dtrace_probe_iter() to find matching probes.  Our
7486ff6d951SJohn Birrell 		 * dt_probe_desc() callback will produce the following results:
7496ff6d951SJohn Birrell 		 *
7506ff6d951SJohn Birrell 		 * m < 0 dtrace_probe_iter() found zero matches (or failed).
7516ff6d951SJohn Birrell 		 * m > 0 dtrace_probe_iter() found more than one match.
7526ff6d951SJohn Birrell 		 * m = 0 dtrace_probe_iter() found exactly one match.
7536ff6d951SJohn Birrell 		 */
7546ff6d951SJohn Birrell 		if ((m = dtrace_probe_iter(dtp, pdp, dt_probe_desc, &pd)) < 0)
7556ff6d951SJohn Birrell 			return (NULL); /* dt_errno is set for us */
7566ff6d951SJohn Birrell 
7576ff6d951SJohn Birrell 		if ((pvp = dt_provider_lookup(dtp, pd.dtpd_provider)) == NULL)
7586ff6d951SJohn Birrell 			return (NULL); /* dt_errno is set for us */
7596ff6d951SJohn Birrell 
7606ff6d951SJohn Birrell 		/*
7616ff6d951SJohn Birrell 		 * If more than one probe was matched, then do not report probe
7626ff6d951SJohn Birrell 		 * information if either of the following conditions is true:
7636ff6d951SJohn Birrell 		 *
7646ff6d951SJohn Birrell 		 * (a) The Arguments Data stability of the matched provider is
7656ff6d951SJohn Birrell 		 *	less than Evolving.
7666ff6d951SJohn Birrell 		 *
7676ff6d951SJohn Birrell 		 * (b) Any description component that is at least Evolving is
7686ff6d951SJohn Birrell 		 *	empty or is specified using a globbing expression.
7696ff6d951SJohn Birrell 		 *
7706ff6d951SJohn Birrell 		 * These conditions imply that providers that provide Evolving
7716ff6d951SJohn Birrell 		 * or better Arguments Data stability must guarantee that all
7726ff6d951SJohn Birrell 		 * probes with identical field names in a field of Evolving or
7736ff6d951SJohn Birrell 		 * better Name stability have identical argument signatures.
7746ff6d951SJohn Birrell 		 */
7756ff6d951SJohn Birrell 		if (m > 0) {
7766ff6d951SJohn Birrell 			if (pvp->pv_desc.dtvd_attr.dtpa_args.dtat_data <
7776ff6d951SJohn Birrell 			    DTRACE_STABILITY_EVOLVING) {
7786ff6d951SJohn Birrell 				(void) dt_set_errno(dtp, EDT_UNSTABLE);
7796ff6d951SJohn Birrell 				return (NULL);
7806ff6d951SJohn Birrell 			}
7816ff6d951SJohn Birrell 
7826ff6d951SJohn Birrell 
7836ff6d951SJohn Birrell 			if (pvp->pv_desc.dtvd_attr.dtpa_mod.dtat_name >=
7846ff6d951SJohn Birrell 			    DTRACE_STABILITY_EVOLVING && m_is_glob) {
7856ff6d951SJohn Birrell 				(void) dt_set_errno(dtp, EDT_UNSTABLE);
7866ff6d951SJohn Birrell 				return (NULL);
7876ff6d951SJohn Birrell 			}
7886ff6d951SJohn Birrell 
7896ff6d951SJohn Birrell 			if (pvp->pv_desc.dtvd_attr.dtpa_func.dtat_name >=
7906ff6d951SJohn Birrell 			    DTRACE_STABILITY_EVOLVING && f_is_glob) {
7916ff6d951SJohn Birrell 				(void) dt_set_errno(dtp, EDT_UNSTABLE);
7926ff6d951SJohn Birrell 				return (NULL);
7936ff6d951SJohn Birrell 			}
7946ff6d951SJohn Birrell 
7956ff6d951SJohn Birrell 			if (pvp->pv_desc.dtvd_attr.dtpa_name.dtat_name >=
7966ff6d951SJohn Birrell 			    DTRACE_STABILITY_EVOLVING && n_is_glob) {
7976ff6d951SJohn Birrell 				(void) dt_set_errno(dtp, EDT_UNSTABLE);
7986ff6d951SJohn Birrell 				return (NULL);
7996ff6d951SJohn Birrell 			}
8006ff6d951SJohn Birrell 		}
8016ff6d951SJohn Birrell 
8026ff6d951SJohn Birrell 		/*
8036ff6d951SJohn Birrell 		 * If we matched a probe exported by dtrace(7D), then discover
8046ff6d951SJohn Birrell 		 * the real attributes.  Otherwise grab the static declaration.
8056ff6d951SJohn Birrell 		 */
8066ff6d951SJohn Birrell 		if (pd.dtpd_id != DTRACE_IDNONE)
8076ff6d951SJohn Birrell 			prp = dt_probe_discover(pvp, &pd);
8086ff6d951SJohn Birrell 		else
8096ff6d951SJohn Birrell 			prp = dt_probe_lookup(pvp, pd.dtpd_name);
8106ff6d951SJohn Birrell 
8116ff6d951SJohn Birrell 		if (prp == NULL)
8126ff6d951SJohn Birrell 			return (NULL); /* dt_errno is set for us */
8136ff6d951SJohn Birrell 	}
8146ff6d951SJohn Birrell 
8156ff6d951SJohn Birrell 	assert(pvp != NULL && prp != NULL);
8166ff6d951SJohn Birrell 
8176ff6d951SJohn Birrell 	/*
8186ff6d951SJohn Birrell 	 * Compute the probe description attributes by taking the minimum of
8196ff6d951SJohn Birrell 	 * the attributes of the specified fields.  If no provider is specified
8206ff6d951SJohn Birrell 	 * or a glob pattern is used for the provider, use Unstable attributes.
8216ff6d951SJohn Birrell 	 */
8226ff6d951SJohn Birrell 	if (pdp->dtpd_provider[0] == '\0' || strisglob(pdp->dtpd_provider))
8236ff6d951SJohn Birrell 		pap = &_dtrace_prvdesc;
8246ff6d951SJohn Birrell 	else
8256ff6d951SJohn Birrell 		pap = &pvp->pv_desc.dtvd_attr;
8266ff6d951SJohn Birrell 
8276ff6d951SJohn Birrell 	pip->dtp_attr = pap->dtpa_provider;
8286ff6d951SJohn Birrell 
8296ff6d951SJohn Birrell 	if (!m_is_glob)
8306ff6d951SJohn Birrell 		pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_mod);
8316ff6d951SJohn Birrell 	if (!f_is_glob)
8326ff6d951SJohn Birrell 		pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_func);
8336ff6d951SJohn Birrell 	if (!n_is_glob)
8346ff6d951SJohn Birrell 		pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_name);
8356ff6d951SJohn Birrell 
8366ff6d951SJohn Birrell 	pip->dtp_arga = pap->dtpa_args;
8376ff6d951SJohn Birrell 	pip->dtp_argv = prp->pr_argv;
8386ff6d951SJohn Birrell 	pip->dtp_argc = prp->pr_argc;
8396ff6d951SJohn Birrell 
8406ff6d951SJohn Birrell 	return (prp);
8416ff6d951SJohn Birrell }
8426ff6d951SJohn Birrell 
8436ff6d951SJohn Birrell int
dtrace_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)8446ff6d951SJohn Birrell dtrace_probe_info(dtrace_hdl_t *dtp,
8456ff6d951SJohn Birrell     const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
8466ff6d951SJohn Birrell {
8476ff6d951SJohn Birrell 	return (dt_probe_info(dtp, pdp, pip) != NULL ? 0 : -1);
8486ff6d951SJohn Birrell }
8496ff6d951SJohn Birrell 
8506ff6d951SJohn Birrell /*ARGSUSED*/
8516ff6d951SJohn Birrell static int
dt_probe_iter(dt_idhash_t * ihp,dt_ident_t * idp,dt_probe_iter_t * pit)8526ff6d951SJohn Birrell dt_probe_iter(dt_idhash_t *ihp, dt_ident_t *idp, dt_probe_iter_t *pit)
8536ff6d951SJohn Birrell {
8546ff6d951SJohn Birrell 	const dt_probe_t *prp = idp->di_data;
8556ff6d951SJohn Birrell 
8566ff6d951SJohn Birrell 	if (!dt_gmatch(prp->pr_name, pit->pit_pat))
8576ff6d951SJohn Birrell 		return (0); /* continue on and examine next probe in hash */
8586ff6d951SJohn Birrell 
8596ff6d951SJohn Birrell 	(void) strlcpy(pit->pit_desc.dtpd_name, prp->pr_name, DTRACE_NAMELEN);
8606ff6d951SJohn Birrell 	pit->pit_desc.dtpd_id = idp->di_id;
8616ff6d951SJohn Birrell 	pit->pit_matches++;
8626ff6d951SJohn Birrell 
8636ff6d951SJohn Birrell 	return (pit->pit_func(pit->pit_hdl, &pit->pit_desc, pit->pit_arg));
8646ff6d951SJohn Birrell }
8656ff6d951SJohn Birrell 
8666ff6d951SJohn Birrell int
dtrace_probe_iter(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probe_f * func,void * arg)8676ff6d951SJohn Birrell dtrace_probe_iter(dtrace_hdl_t *dtp,
8686ff6d951SJohn Birrell     const dtrace_probedesc_t *pdp, dtrace_probe_f *func, void *arg)
8696ff6d951SJohn Birrell {
8706ff6d951SJohn Birrell 	const char *provider = pdp ? pdp->dtpd_provider : NULL;
8716ff6d951SJohn Birrell 	dtrace_id_t id = DTRACE_IDNONE;
8726ff6d951SJohn Birrell 
8736ff6d951SJohn Birrell 	dtrace_probedesc_t pd;
8746ff6d951SJohn Birrell 	dt_probe_iter_t pit;
8756ff6d951SJohn Birrell 	int cmd, rv;
8766ff6d951SJohn Birrell 
8776ff6d951SJohn Birrell 	bzero(&pit, sizeof (pit));
8786ff6d951SJohn Birrell 	pit.pit_hdl = dtp;
8796ff6d951SJohn Birrell 	pit.pit_func = func;
8806ff6d951SJohn Birrell 	pit.pit_arg = arg;
8816ff6d951SJohn Birrell 	pit.pit_pat = pdp ? pdp->dtpd_name : NULL;
8826ff6d951SJohn Birrell 
8836ff6d951SJohn Birrell 	for (pit.pit_pvp = dt_list_next(&dtp->dt_provlist);
8846ff6d951SJohn Birrell 	    pit.pit_pvp != NULL; pit.pit_pvp = dt_list_next(pit.pit_pvp)) {
8856ff6d951SJohn Birrell 
8866ff6d951SJohn Birrell 		if (pit.pit_pvp->pv_flags & DT_PROVIDER_IMPL)
8876ff6d951SJohn Birrell 			continue; /* we'll get these later using dt_ioctl() */
8886ff6d951SJohn Birrell 
8896ff6d951SJohn Birrell 		if (!dt_gmatch(pit.pit_pvp->pv_desc.dtvd_name, provider))
8906ff6d951SJohn Birrell 			continue;
8916ff6d951SJohn Birrell 
8926ff6d951SJohn Birrell 		(void) strlcpy(pit.pit_desc.dtpd_provider,
8936ff6d951SJohn Birrell 		    pit.pit_pvp->pv_desc.dtvd_name, DTRACE_PROVNAMELEN);
8946ff6d951SJohn Birrell 
8956ff6d951SJohn Birrell 		if ((rv = dt_idhash_iter(pit.pit_pvp->pv_probes,
8966ff6d951SJohn Birrell 		    (dt_idhash_f *)dt_probe_iter, &pit)) != 0)
8976ff6d951SJohn Birrell 			return (rv);
8986ff6d951SJohn Birrell 	}
8996ff6d951SJohn Birrell 
9006ff6d951SJohn Birrell 	if (pdp != NULL)
9016ff6d951SJohn Birrell 		cmd = DTRACEIOC_PROBEMATCH;
9026ff6d951SJohn Birrell 	else
9036ff6d951SJohn Birrell 		cmd = DTRACEIOC_PROBES;
9046ff6d951SJohn Birrell 
9056ff6d951SJohn Birrell 	for (;;) {
9066ff6d951SJohn Birrell 		if (pdp != NULL)
9076ff6d951SJohn Birrell 			bcopy(pdp, &pd, sizeof (pd));
9086ff6d951SJohn Birrell 
9096ff6d951SJohn Birrell 		pd.dtpd_id = id;
9106ff6d951SJohn Birrell 
9116ff6d951SJohn Birrell 		if (dt_ioctl(dtp, cmd, &pd) != 0)
9126ff6d951SJohn Birrell 			break;
9136ff6d951SJohn Birrell 		else if ((rv = func(dtp, &pd, arg)) != 0)
9146ff6d951SJohn Birrell 			return (rv);
9156ff6d951SJohn Birrell 
9166ff6d951SJohn Birrell 		pit.pit_matches++;
9176ff6d951SJohn Birrell 		id = pd.dtpd_id + 1;
9186ff6d951SJohn Birrell 	}
9196ff6d951SJohn Birrell 
9206ff6d951SJohn Birrell 	switch (errno) {
9216ff6d951SJohn Birrell 	case ESRCH:
9226ff6d951SJohn Birrell 	case EBADF:
9236ff6d951SJohn Birrell 		return (pit.pit_matches ? 0 : dt_set_errno(dtp, EDT_NOPROBE));
9246ff6d951SJohn Birrell 	case EINVAL:
9256ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADPGLOB));
9266ff6d951SJohn Birrell 	default:
9276ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
9286ff6d951SJohn Birrell 	}
9296ff6d951SJohn Birrell }
930