10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51710Sahl * Common Development and Distribution License (the "License").
61710Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211710Sahl
220Sstevel@tonic-gate /*
231239Sahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <alloca.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include <dt_provider.h>
410Sstevel@tonic-gate #include <dt_module.h>
420Sstevel@tonic-gate #include <dt_string.h>
430Sstevel@tonic-gate #include <dt_list.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate static dt_provider_t *
dt_provider_insert(dtrace_hdl_t * dtp,dt_provider_t * pvp,uint_t h)460Sstevel@tonic-gate dt_provider_insert(dtrace_hdl_t *dtp, dt_provider_t *pvp, uint_t h)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate dt_list_append(&dtp->dt_provlist, pvp);
490Sstevel@tonic-gate
500Sstevel@tonic-gate pvp->pv_next = dtp->dt_provs[h];
510Sstevel@tonic-gate dtp->dt_provs[h] = pvp;
520Sstevel@tonic-gate dtp->dt_nprovs++;
530Sstevel@tonic-gate
540Sstevel@tonic-gate return (pvp);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate dt_provider_t *
dt_provider_lookup(dtrace_hdl_t * dtp,const char * name)580Sstevel@tonic-gate dt_provider_lookup(dtrace_hdl_t *dtp, const char *name)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_provbuckets;
61265Smws dtrace_providerdesc_t desc;
620Sstevel@tonic-gate dt_provider_t *pvp;
630Sstevel@tonic-gate
640Sstevel@tonic-gate for (pvp = dtp->dt_provs[h]; pvp != NULL; pvp = pvp->pv_next) {
650Sstevel@tonic-gate if (strcmp(pvp->pv_desc.dtvd_name, name) == 0)
660Sstevel@tonic-gate return (pvp);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate if (strisglob(name) || name[0] == '\0') {
700Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOPROV);
710Sstevel@tonic-gate return (NULL);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
74265Smws bzero(&desc, sizeof (desc));
75265Smws (void) strlcpy(desc.dtvd_name, name, DTRACE_PROVNAMELEN);
76265Smws
77265Smws if (dt_ioctl(dtp, DTRACEIOC_PROVIDER, &desc) == -1) {
78265Smws (void) dt_set_errno(dtp, errno == ESRCH ? EDT_NOPROV : errno);
79265Smws return (NULL);
80265Smws }
81265Smws
820Sstevel@tonic-gate if ((pvp = dt_provider_create(dtp, name)) == NULL)
830Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
840Sstevel@tonic-gate
85265Smws bcopy(&desc, &pvp->pv_desc, sizeof (desc));
860Sstevel@tonic-gate pvp->pv_flags |= DT_PROVIDER_IMPL;
870Sstevel@tonic-gate return (pvp);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate dt_provider_t *
dt_provider_create(dtrace_hdl_t * dtp,const char * name)910Sstevel@tonic-gate dt_provider_create(dtrace_hdl_t *dtp, const char *name)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate dt_provider_t *pvp;
940Sstevel@tonic-gate
950Sstevel@tonic-gate if ((pvp = dt_zalloc(dtp, sizeof (dt_provider_t))) == NULL)
960Sstevel@tonic-gate return (NULL);
970Sstevel@tonic-gate
980Sstevel@tonic-gate (void) strlcpy(pvp->pv_desc.dtvd_name, name, DTRACE_PROVNAMELEN);
990Sstevel@tonic-gate pvp->pv_probes = dt_idhash_create(pvp->pv_desc.dtvd_name, NULL, 0, 0);
1000Sstevel@tonic-gate pvp->pv_gen = dtp->dt_gen;
1010Sstevel@tonic-gate pvp->pv_hdl = dtp;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (pvp->pv_probes == NULL) {
1040Sstevel@tonic-gate dt_free(dtp, pvp);
1050Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM);
1060Sstevel@tonic-gate return (NULL);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate pvp->pv_desc.dtvd_attr.dtpa_provider = _dtrace_prvattr;
1100Sstevel@tonic-gate pvp->pv_desc.dtvd_attr.dtpa_mod = _dtrace_prvattr;
1110Sstevel@tonic-gate pvp->pv_desc.dtvd_attr.dtpa_func = _dtrace_prvattr;
1120Sstevel@tonic-gate pvp->pv_desc.dtvd_attr.dtpa_name = _dtrace_prvattr;
1130Sstevel@tonic-gate pvp->pv_desc.dtvd_attr.dtpa_args = _dtrace_prvattr;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate return (dt_provider_insert(dtp, pvp,
1160Sstevel@tonic-gate dt_strtab_hash(name, NULL) % dtp->dt_provbuckets));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate void
dt_provider_destroy(dtrace_hdl_t * dtp,dt_provider_t * pvp)1200Sstevel@tonic-gate dt_provider_destroy(dtrace_hdl_t *dtp, dt_provider_t *pvp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate dt_provider_t **pp;
1230Sstevel@tonic-gate uint_t h;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate assert(pvp->pv_hdl == dtp);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate h = dt_strtab_hash(pvp->pv_desc.dtvd_name, NULL) % dtp->dt_provbuckets;
1280Sstevel@tonic-gate pp = &dtp->dt_provs[h];
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate while (*pp != NULL && *pp != pvp)
1310Sstevel@tonic-gate pp = &(*pp)->pv_next;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate assert(*pp != NULL && *pp == pvp);
1340Sstevel@tonic-gate *pp = pvp->pv_next;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate dt_list_delete(&dtp->dt_provlist, pvp);
1370Sstevel@tonic-gate dtp->dt_nprovs--;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (pvp->pv_probes != NULL)
1400Sstevel@tonic-gate dt_idhash_destroy(pvp->pv_probes);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate dt_node_link_free(&pvp->pv_nodes);
143265Smws dt_free(dtp, pvp->pv_xrefs);
1440Sstevel@tonic-gate dt_free(dtp, pvp);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
147265Smws int
dt_provider_xref(dtrace_hdl_t * dtp,dt_provider_t * pvp,id_t id)148265Smws dt_provider_xref(dtrace_hdl_t *dtp, dt_provider_t *pvp, id_t id)
149265Smws {
150265Smws size_t oldsize = BT_SIZEOFMAP(pvp->pv_xrmax);
151265Smws size_t newsize = BT_SIZEOFMAP(dtp->dt_xlatorid);
152265Smws
153265Smws assert(id >= 0 && id < dtp->dt_xlatorid);
154265Smws
155265Smws if (newsize > oldsize) {
156265Smws ulong_t *xrefs = dt_zalloc(dtp, newsize);
157265Smws
158265Smws if (xrefs == NULL)
159265Smws return (-1);
160265Smws
161265Smws bcopy(pvp->pv_xrefs, xrefs, oldsize);
162265Smws dt_free(dtp, pvp->pv_xrefs);
163265Smws
164265Smws pvp->pv_xrefs = xrefs;
165265Smws pvp->pv_xrmax = dtp->dt_xlatorid;
166265Smws }
167265Smws
168265Smws BT_SET(pvp->pv_xrefs, id);
169265Smws return (0);
170265Smws }
171265Smws
1720Sstevel@tonic-gate static uint8_t
dt_probe_argmap(dt_node_t * xnp,dt_node_t * nnp)1730Sstevel@tonic-gate dt_probe_argmap(dt_node_t *xnp, dt_node_t *nnp)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate uint8_t i;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate for (i = 0; nnp != NULL; i++) {
1780Sstevel@tonic-gate if (nnp->dn_string != NULL &&
1790Sstevel@tonic-gate strcmp(nnp->dn_string, xnp->dn_string) == 0)
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate else
1820Sstevel@tonic-gate nnp = nnp->dn_list;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate return (i);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate static dt_node_t *
dt_probe_alloc_args(dt_provider_t * pvp,int argc)1890Sstevel@tonic-gate dt_probe_alloc_args(dt_provider_t *pvp, int argc)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate dt_node_t *args = NULL, *pnp = NULL, *dnp;
1920Sstevel@tonic-gate int i;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate for (i = 0; i < argc; i++, pnp = dnp) {
1950Sstevel@tonic-gate if ((dnp = dt_node_xalloc(pvp->pv_hdl, DT_NODE_TYPE)) == NULL)
1960Sstevel@tonic-gate return (NULL);
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate dnp->dn_link = pvp->pv_nodes;
1990Sstevel@tonic-gate pvp->pv_nodes = dnp;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (args == NULL)
2020Sstevel@tonic-gate args = dnp;
2030Sstevel@tonic-gate else
2040Sstevel@tonic-gate pnp->dn_list = dnp;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate return (args);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate static size_t
dt_probe_keylen(const dtrace_probedesc_t * pdp)2110Sstevel@tonic-gate dt_probe_keylen(const dtrace_probedesc_t *pdp)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate return (strlen(pdp->dtpd_mod) + 1 +
2140Sstevel@tonic-gate strlen(pdp->dtpd_func) + 1 + strlen(pdp->dtpd_name) + 1);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate static char *
dt_probe_key(const dtrace_probedesc_t * pdp,char * s)2180Sstevel@tonic-gate dt_probe_key(const dtrace_probedesc_t *pdp, char *s)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate (void) snprintf(s, INT_MAX, "%s:%s:%s",
2210Sstevel@tonic-gate pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
2220Sstevel@tonic-gate return (s);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * If a probe was discovered from the kernel, ask dtrace(7D) for a description
2270Sstevel@tonic-gate * of each of its arguments, including native and translated types.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate static dt_probe_t *
dt_probe_discover(dt_provider_t * pvp,const dtrace_probedesc_t * pdp)2300Sstevel@tonic-gate dt_probe_discover(dt_provider_t *pvp, const dtrace_probedesc_t *pdp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate dtrace_hdl_t *dtp = pvp->pv_hdl;
2330Sstevel@tonic-gate char *name = dt_probe_key(pdp, alloca(dt_probe_keylen(pdp)));
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate dt_node_t *xargs, *nargs;
2360Sstevel@tonic-gate dt_ident_t *idp;
2370Sstevel@tonic-gate dt_probe_t *prp;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate dtrace_typeinfo_t dtt;
2400Sstevel@tonic-gate int i, nc, xc;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate int adc = _dtrace_argmax;
2430Sstevel@tonic-gate dtrace_argdesc_t *adv = alloca(sizeof (dtrace_argdesc_t) * adc);
2440Sstevel@tonic-gate dtrace_argdesc_t *adp = adv;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate assert(strcmp(pvp->pv_desc.dtvd_name, pdp->dtpd_provider) == 0);
2470Sstevel@tonic-gate assert(pdp->dtpd_id != DTRACE_IDNONE);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate dt_dprintf("discovering probe %s:%s id=%d\n",
2500Sstevel@tonic-gate pvp->pv_desc.dtvd_name, name, pdp->dtpd_id);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate for (nc = -1, i = 0; i < adc; i++, adp++) {
2530Sstevel@tonic-gate bzero(adp, sizeof (dtrace_argdesc_t));
2540Sstevel@tonic-gate adp->dtargd_ndx = i;
2550Sstevel@tonic-gate adp->dtargd_id = pdp->dtpd_id;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (dt_ioctl(dtp, DTRACEIOC_PROBEARG, adp) != 0) {
2580Sstevel@tonic-gate (void) dt_set_errno(dtp, errno);
2590Sstevel@tonic-gate return (NULL);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (adp->dtargd_ndx == DTRACE_ARGNONE)
2630Sstevel@tonic-gate break; /* all argument descs have been retrieved */
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate nc = MAX(nc, adp->dtargd_mapping);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate xc = i;
2690Sstevel@tonic-gate nc++;
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate * Now that we have discovered the number of native and translated
2730Sstevel@tonic-gate * arguments from the argument descriptions, allocate a new probe ident
2740Sstevel@tonic-gate * and corresponding dt_probe_t and hash it into the provider.
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate xargs = dt_probe_alloc_args(pvp, xc);
2770Sstevel@tonic-gate nargs = dt_probe_alloc_args(pvp, nc);
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if ((xc != 0 && xargs == NULL) || (nc != 0 && nargs == NULL))
2800Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate idp = dt_ident_create(name, DT_IDENT_PROBE,
2830Sstevel@tonic-gate DT_IDFLG_ORPHAN, pdp->dtpd_id, _dtrace_defattr, 0,
2840Sstevel@tonic-gate &dt_idops_probe, NULL, dtp->dt_gen);
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate if (idp == NULL) {
2870Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM);
2880Sstevel@tonic-gate return (NULL);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
291265Smws if ((prp = dt_probe_create(dtp, idp, 2,
292265Smws nargs, nc, xargs, xc)) == NULL) {
2930Sstevel@tonic-gate dt_ident_destroy(idp);
2940Sstevel@tonic-gate return (NULL);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate dt_probe_declare(pvp, prp);
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * Once our new dt_probe_t is fully constructed, iterate over the
3010Sstevel@tonic-gate * cached argument descriptions and assign types to prp->pr_nargv[]
3020Sstevel@tonic-gate * and prp->pr_xargv[] and assign mappings to prp->pr_mapping[].
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate for (adp = adv, i = 0; i < xc; i++, adp++) {
3050Sstevel@tonic-gate if (dtrace_type_strcompile(dtp,
3060Sstevel@tonic-gate adp->dtargd_native, &dtt) != 0) {
3070Sstevel@tonic-gate dt_dprintf("failed to resolve input type %s "
3080Sstevel@tonic-gate "for %s:%s arg #%d: %s\n", adp->dtargd_native,
3090Sstevel@tonic-gate pvp->pv_desc.dtvd_name, name, i + 1,
3100Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate dtt.dtt_object = NULL;
3130Sstevel@tonic-gate dtt.dtt_ctfp = NULL;
3140Sstevel@tonic-gate dtt.dtt_type = CTF_ERR;
3150Sstevel@tonic-gate } else {
3160Sstevel@tonic-gate dt_node_type_assign(prp->pr_nargv[adp->dtargd_mapping],
3170Sstevel@tonic-gate dtt.dtt_ctfp, dtt.dtt_type);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate if (dtt.dtt_type != CTF_ERR && (adp->dtargd_xlate[0] == '\0' ||
3210Sstevel@tonic-gate strcmp(adp->dtargd_native, adp->dtargd_xlate) == 0)) {
3220Sstevel@tonic-gate dt_node_type_propagate(prp->pr_nargv[
3230Sstevel@tonic-gate adp->dtargd_mapping], prp->pr_xargv[i]);
3240Sstevel@tonic-gate } else if (dtrace_type_strcompile(dtp,
3250Sstevel@tonic-gate adp->dtargd_xlate, &dtt) != 0) {
3260Sstevel@tonic-gate dt_dprintf("failed to resolve output type %s "
3270Sstevel@tonic-gate "for %s:%s arg #%d: %s\n", adp->dtargd_xlate,
3280Sstevel@tonic-gate pvp->pv_desc.dtvd_name, name, i + 1,
3290Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp)));
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate dtt.dtt_object = NULL;
3320Sstevel@tonic-gate dtt.dtt_ctfp = NULL;
3330Sstevel@tonic-gate dtt.dtt_type = CTF_ERR;
3340Sstevel@tonic-gate } else {
3350Sstevel@tonic-gate dt_node_type_assign(prp->pr_xargv[i],
3360Sstevel@tonic-gate dtt.dtt_ctfp, dtt.dtt_type);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate prp->pr_mapping[i] = adp->dtargd_mapping;
3400Sstevel@tonic-gate prp->pr_argv[i] = dtt;
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate return (prp);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate * Lookup a probe declaration based on a known provider and full or partially
3480Sstevel@tonic-gate * specified module, function, and name. If the probe is not known to us yet,
3490Sstevel@tonic-gate * ask dtrace(7D) to match the description and then cache any useful results.
3500Sstevel@tonic-gate */
3510Sstevel@tonic-gate dt_probe_t *
dt_probe_lookup(dt_provider_t * pvp,const char * s)3520Sstevel@tonic-gate dt_probe_lookup(dt_provider_t *pvp, const char *s)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate dtrace_hdl_t *dtp = pvp->pv_hdl;
3550Sstevel@tonic-gate dtrace_probedesc_t pd;
3560Sstevel@tonic-gate dt_ident_t *idp;
3570Sstevel@tonic-gate size_t keylen;
3580Sstevel@tonic-gate char *key;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (dtrace_str2desc(dtp, DTRACE_PROBESPEC_NAME, s, &pd) != 0)
3610Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate keylen = dt_probe_keylen(&pd);
3640Sstevel@tonic-gate key = dt_probe_key(&pd, alloca(keylen));
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * If the probe is already declared, then return the dt_probe_t from
3680Sstevel@tonic-gate * the existing identifier. This could come from a static declaration
3690Sstevel@tonic-gate * or it could have been cached from an earlier call to this function.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
3720Sstevel@tonic-gate return (idp->di_data);
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate * If the probe isn't known, use the probe description computed above
3760Sstevel@tonic-gate * to ask dtrace(7D) to find the first matching probe.
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate if (dt_ioctl(dtp, DTRACEIOC_PROBEMATCH, &pd) == 0)
3790Sstevel@tonic-gate return (dt_probe_discover(pvp, &pd));
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate if (errno == ESRCH || errno == EBADF)
3820Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOPROBE);
3830Sstevel@tonic-gate else
3840Sstevel@tonic-gate (void) dt_set_errno(dtp, errno);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate return (NULL);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate 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)390265Smws dt_probe_create(dtrace_hdl_t *dtp, dt_ident_t *idp, int protoc,
3910Sstevel@tonic-gate dt_node_t *nargs, uint_t nargc, dt_node_t *xargs, uint_t xargc)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate dt_module_t *dmp;
3940Sstevel@tonic-gate dt_probe_t *prp;
3950Sstevel@tonic-gate const char *p;
3960Sstevel@tonic-gate uint_t i;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate assert(idp->di_kind == DT_IDENT_PROBE);
3990Sstevel@tonic-gate assert(idp->di_data == NULL);
4000Sstevel@tonic-gate
401265Smws /*
402265Smws * If only a single prototype is given, set xargc/s to nargc/s to
403265Smws * simplify subsequent use. Note that we can have one or both of nargs
404265Smws * and xargs be specified but set to NULL, indicating a void prototype.
405265Smws */
406265Smws if (protoc < 2) {
407265Smws assert(xargs == NULL);
408265Smws assert(xargc == 0);
4090Sstevel@tonic-gate xargs = nargs;
4100Sstevel@tonic-gate xargc = nargc;
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate if ((prp = dt_alloc(dtp, sizeof (dt_probe_t))) == NULL)
4140Sstevel@tonic-gate return (NULL);
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate prp->pr_pvp = NULL;
4170Sstevel@tonic-gate prp->pr_ident = idp;
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate p = strrchr(idp->di_name, ':');
4200Sstevel@tonic-gate assert(p != NULL);
4210Sstevel@tonic-gate prp->pr_name = p + 1;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate prp->pr_nargs = nargs;
4240Sstevel@tonic-gate prp->pr_nargv = dt_alloc(dtp, sizeof (dt_node_t *) * nargc);
4250Sstevel@tonic-gate prp->pr_nargc = nargc;
4260Sstevel@tonic-gate prp->pr_xargs = xargs;
4270Sstevel@tonic-gate prp->pr_xargv = dt_alloc(dtp, sizeof (dt_node_t *) * xargc);
4280Sstevel@tonic-gate prp->pr_xargc = xargc;
4290Sstevel@tonic-gate prp->pr_mapping = dt_alloc(dtp, sizeof (uint8_t) * xargc);
4300Sstevel@tonic-gate prp->pr_inst = NULL;
4310Sstevel@tonic-gate prp->pr_argv = dt_alloc(dtp, sizeof (dtrace_typeinfo_t) * xargc);
4320Sstevel@tonic-gate prp->pr_argc = xargc;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate if ((prp->pr_nargc != 0 && prp->pr_nargv == NULL) ||
4350Sstevel@tonic-gate (prp->pr_xargc != 0 && prp->pr_xargv == NULL) ||
4360Sstevel@tonic-gate (prp->pr_xargc != 0 && prp->pr_mapping == NULL) ||
4370Sstevel@tonic-gate (prp->pr_argc != 0 && prp->pr_argv == NULL)) {
4380Sstevel@tonic-gate dt_probe_destroy(prp);
4390Sstevel@tonic-gate return (NULL);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate for (i = 0; i < xargc; i++, xargs = xargs->dn_list) {
4430Sstevel@tonic-gate if (xargs->dn_string != NULL)
4440Sstevel@tonic-gate prp->pr_mapping[i] = dt_probe_argmap(xargs, nargs);
4450Sstevel@tonic-gate else
4460Sstevel@tonic-gate prp->pr_mapping[i] = i;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate prp->pr_xargv[i] = xargs;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate if ((dmp = dt_module_lookup_by_ctf(dtp,
4510Sstevel@tonic-gate xargs->dn_ctfp)) != NULL)
4520Sstevel@tonic-gate prp->pr_argv[i].dtt_object = dmp->dm_name;
4530Sstevel@tonic-gate else
4540Sstevel@tonic-gate prp->pr_argv[i].dtt_object = NULL;
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate prp->pr_argv[i].dtt_ctfp = xargs->dn_ctfp;
4570Sstevel@tonic-gate prp->pr_argv[i].dtt_type = xargs->dn_type;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate for (i = 0; i < nargc; i++, nargs = nargs->dn_list)
4610Sstevel@tonic-gate prp->pr_nargv[i] = nargs;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate idp->di_data = prp;
4640Sstevel@tonic-gate return (prp);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate void
dt_probe_declare(dt_provider_t * pvp,dt_probe_t * prp)4680Sstevel@tonic-gate dt_probe_declare(dt_provider_t *pvp, dt_probe_t *prp)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate assert(prp->pr_ident->di_kind == DT_IDENT_PROBE);
4710Sstevel@tonic-gate assert(prp->pr_ident->di_data == prp);
4720Sstevel@tonic-gate assert(prp->pr_pvp == NULL);
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate if (prp->pr_xargs != prp->pr_nargs)
4750Sstevel@tonic-gate pvp->pv_flags &= ~DT_PROVIDER_INTF;
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate prp->pr_pvp = pvp;
4780Sstevel@tonic-gate dt_idhash_xinsert(pvp->pv_probes, prp->pr_ident);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate void
dt_probe_destroy(dt_probe_t * prp)4820Sstevel@tonic-gate dt_probe_destroy(dt_probe_t *prp)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate dt_probe_instance_t *pip, *pip_next;
4850Sstevel@tonic-gate dtrace_hdl_t *dtp;
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate if (prp->pr_pvp != NULL)
4880Sstevel@tonic-gate dtp = prp->pr_pvp->pv_hdl;
4890Sstevel@tonic-gate else
4900Sstevel@tonic-gate dtp = yypcb->pcb_hdl;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate dt_node_list_free(&prp->pr_nargs);
4930Sstevel@tonic-gate dt_node_list_free(&prp->pr_xargs);
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate dt_free(dtp, prp->pr_nargv);
4960Sstevel@tonic-gate dt_free(dtp, prp->pr_xargv);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate for (pip = prp->pr_inst; pip != NULL; pip = pip_next) {
4990Sstevel@tonic-gate pip_next = pip->pi_next;
5000Sstevel@tonic-gate dt_free(dtp, pip->pi_offs);
5011710Sahl dt_free(dtp, pip->pi_enoffs);
5020Sstevel@tonic-gate dt_free(dtp, pip);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate dt_free(dtp, prp->pr_mapping);
5060Sstevel@tonic-gate dt_free(dtp, prp->pr_argv);
5070Sstevel@tonic-gate dt_free(dtp, prp);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate int
dt_probe_define(dt_provider_t * pvp,dt_probe_t * prp,const char * fname,const char * rname,uint32_t offset,int isenabled)5110Sstevel@tonic-gate dt_probe_define(dt_provider_t *pvp, dt_probe_t *prp,
5121710Sahl const char *fname, const char *rname, uint32_t offset, int isenabled)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate dtrace_hdl_t *dtp = pvp->pv_hdl;
5150Sstevel@tonic-gate dt_probe_instance_t *pip;
5161710Sahl uint32_t **offs;
5171710Sahl uint_t *noffs, *maxoffs;
5180Sstevel@tonic-gate
5191239Sahl assert(fname != NULL);
5201239Sahl
5210Sstevel@tonic-gate for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
5221239Sahl if (strcmp(pip->pi_fname, fname) == 0 &&
5231239Sahl ((rname == NULL && pip->pi_rname[0] == '\0') ||
5241239Sahl (rname != NULL && strcmp(pip->pi_rname, rname)) == 0))
5250Sstevel@tonic-gate break;
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate if (pip == NULL) {
5291239Sahl if ((pip = dt_zalloc(dtp, sizeof (*pip))) == NULL)
5301239Sahl return (-1);
5311239Sahl
5321710Sahl if ((pip->pi_offs = dt_zalloc(dtp,
5331710Sahl sizeof (uint32_t))) == NULL) {
5341710Sahl dt_free(dtp, pip);
5351710Sahl return (-1);
5361710Sahl }
5371710Sahl
5381710Sahl if ((pip->pi_enoffs = dt_zalloc(dtp,
5391710Sahl sizeof (uint32_t))) == NULL) {
5401710Sahl dt_free(dtp, pip->pi_offs);
5410Sstevel@tonic-gate dt_free(dtp, pip);
5420Sstevel@tonic-gate return (-1);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate (void) strlcpy(pip->pi_fname, fname, sizeof (pip->pi_fname));
5461239Sahl if (rname != NULL) {
5471239Sahl if (strlen(rname) + 1 > sizeof (pip->pi_rname)) {
5481239Sahl dt_free(dtp, pip->pi_offs);
5491239Sahl dt_free(dtp, pip);
5501239Sahl return (dt_set_errno(dtp, EDT_COMPILER));
5511239Sahl }
5521239Sahl (void) strcpy(pip->pi_rname, rname);
5531239Sahl }
5541710Sahl
5550Sstevel@tonic-gate pip->pi_noffs = 0;
5560Sstevel@tonic-gate pip->pi_maxoffs = 1;
5571710Sahl pip->pi_nenoffs = 0;
5581710Sahl pip->pi_maxenoffs = 1;
5591710Sahl
5600Sstevel@tonic-gate pip->pi_next = prp->pr_inst;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate prp->pr_inst = pip;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5651710Sahl if (isenabled) {
5661710Sahl offs = &pip->pi_enoffs;
5671710Sahl noffs = &pip->pi_nenoffs;
5681710Sahl maxoffs = &pip->pi_maxenoffs;
5691710Sahl } else {
5701710Sahl offs = &pip->pi_offs;
5711710Sahl noffs = &pip->pi_noffs;
5721710Sahl maxoffs = &pip->pi_maxoffs;
5731710Sahl }
5741710Sahl
5751710Sahl if (*noffs == *maxoffs) {
5761710Sahl uint_t new_max = *maxoffs * 2;
5770Sstevel@tonic-gate uint32_t *new_offs = dt_alloc(dtp, sizeof (uint32_t) * new_max);
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate if (new_offs == NULL)
5800Sstevel@tonic-gate return (-1);
5810Sstevel@tonic-gate
5821710Sahl bcopy(*offs, new_offs, sizeof (uint32_t) * *maxoffs);
5830Sstevel@tonic-gate
5841710Sahl dt_free(dtp, *offs);
5851710Sahl *maxoffs = new_max;
5861710Sahl *offs = new_offs;
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate
5891710Sahl dt_dprintf("defined probe %s %s:%s %s() +0x%x (%s)\n",
5901710Sahl isenabled ? "(is-enabled)" : "",
5911239Sahl pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, fname, offset,
5921239Sahl rname != NULL ? rname : fname);
5930Sstevel@tonic-gate
5941710Sahl assert(*noffs < *maxoffs);
595*1750Sbmc (*offs)[(*noffs)++] = offset;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate return (0);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
600265Smws /*
601265Smws * Lookup the dynamic translator type tag for the specified probe argument and
602265Smws * assign the type to the specified node. If the type is not yet defined, add
603265Smws * it to the "D" module's type container as a typedef for an unknown type.
604265Smws */
605265Smws dt_node_t *
dt_probe_tag(dt_probe_t * prp,uint_t argn,dt_node_t * dnp)606265Smws dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp)
607265Smws {
608265Smws dtrace_hdl_t *dtp = prp->pr_pvp->pv_hdl;
609265Smws dtrace_typeinfo_t dtt;
610265Smws size_t len;
611265Smws char *tag;
612265Smws
613265Smws len = snprintf(NULL, 0, "__dtrace_%s___%s_arg%u",
614265Smws prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
615265Smws
616265Smws tag = alloca(len + 1);
617265Smws
618265Smws (void) snprintf(tag, len + 1, "__dtrace_%s___%s_arg%u",
619265Smws prp->pr_pvp->pv_desc.dtvd_name, prp->pr_name, argn);
620265Smws
621265Smws if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS, tag, &dtt) != 0) {
622265Smws dtt.dtt_object = DTRACE_OBJ_DDEFS;
623265Smws dtt.dtt_ctfp = DT_DYN_CTFP(dtp);
624265Smws dtt.dtt_type = ctf_add_typedef(DT_DYN_CTFP(dtp),
625265Smws CTF_ADD_ROOT, tag, DT_DYN_TYPE(dtp));
626265Smws
627265Smws if (dtt.dtt_type == CTF_ERR ||
628265Smws ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
629265Smws xyerror(D_UNKNOWN, "cannot define type %s: %s\n",
630265Smws tag, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
631265Smws }
632265Smws }
633265Smws
634265Smws bzero(dnp, sizeof (dt_node_t));
635265Smws dnp->dn_kind = DT_NODE_TYPE;
636265Smws
637265Smws dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type);
638265Smws dt_node_attr_assign(dnp, _dtrace_defattr);
639265Smws
640265Smws return (dnp);
641265Smws }
642265Smws
6430Sstevel@tonic-gate /*ARGSUSED*/
6440Sstevel@tonic-gate static int
dt_probe_desc(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * arg)6450Sstevel@tonic-gate dt_probe_desc(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate if (((dtrace_probedesc_t *)arg)->dtpd_id == DTRACE_IDNONE) {
6480Sstevel@tonic-gate bcopy(pdp, arg, sizeof (dtrace_probedesc_t));
6490Sstevel@tonic-gate return (0);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate return (1);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate dt_probe_t *
dt_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)6560Sstevel@tonic-gate dt_probe_info(dtrace_hdl_t *dtp,
6570Sstevel@tonic-gate const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate int m_is_glob = pdp->dtpd_mod[0] == '\0' || strisglob(pdp->dtpd_mod);
6600Sstevel@tonic-gate int f_is_glob = pdp->dtpd_func[0] == '\0' || strisglob(pdp->dtpd_func);
6610Sstevel@tonic-gate int n_is_glob = pdp->dtpd_name[0] == '\0' || strisglob(pdp->dtpd_name);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate dt_probe_t *prp = NULL;
6640Sstevel@tonic-gate const dtrace_pattr_t *pap;
6650Sstevel@tonic-gate dt_provider_t *pvp;
6660Sstevel@tonic-gate dt_ident_t *idp;
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate * Attempt to lookup the probe in our existing cache for this provider.
6700Sstevel@tonic-gate * If none is found and an explicit probe ID was specified, discover
6710Sstevel@tonic-gate * that specific probe and cache its description and arguments.
6720Sstevel@tonic-gate */
6730Sstevel@tonic-gate if ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) != NULL) {
6740Sstevel@tonic-gate size_t keylen = dt_probe_keylen(pdp);
6750Sstevel@tonic-gate char *key = dt_probe_key(pdp, alloca(keylen));
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate if ((idp = dt_idhash_lookup(pvp->pv_probes, key)) != NULL)
6780Sstevel@tonic-gate prp = idp->di_data;
6790Sstevel@tonic-gate else if (pdp->dtpd_id != DTRACE_IDNONE)
6800Sstevel@tonic-gate prp = dt_probe_discover(pvp, pdp);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate * If no probe was found in our cache, convert the caller's partial
6850Sstevel@tonic-gate * probe description into a fully-formed matching probe description by
6860Sstevel@tonic-gate * iterating over up to at most two probes that match 'pdp'. We then
6870Sstevel@tonic-gate * call dt_probe_discover() on the resulting probe identifier.
6880Sstevel@tonic-gate */
6890Sstevel@tonic-gate if (prp == NULL) {
6900Sstevel@tonic-gate dtrace_probedesc_t pd;
6910Sstevel@tonic-gate int m;
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate bzero(&pd, sizeof (pd));
6940Sstevel@tonic-gate pd.dtpd_id = DTRACE_IDNONE;
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * Call dtrace_probe_iter() to find matching probes. Our
6980Sstevel@tonic-gate * dt_probe_desc() callback will produce the following results:
6990Sstevel@tonic-gate *
7000Sstevel@tonic-gate * m < 0 dtrace_probe_iter() found zero matches (or failed).
7010Sstevel@tonic-gate * m > 0 dtrace_probe_iter() found more than one match.
7020Sstevel@tonic-gate * m = 0 dtrace_probe_iter() found exactly one match.
7030Sstevel@tonic-gate */
7040Sstevel@tonic-gate if ((m = dtrace_probe_iter(dtp, pdp, dt_probe_desc, &pd)) < 0)
7050Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if ((pvp = dt_provider_lookup(dtp, pd.dtpd_provider)) == NULL)
7080Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * If more than one probe was matched, then do not report probe
7120Sstevel@tonic-gate * information if either of the following conditions is true:
7130Sstevel@tonic-gate *
7140Sstevel@tonic-gate * (a) The Arguments Data stability of the matched provider is
7150Sstevel@tonic-gate * less than Evolving.
7160Sstevel@tonic-gate *
7170Sstevel@tonic-gate * (b) Any description component that is at least Evolving is
7180Sstevel@tonic-gate * empty or is specified using a globbing expression.
7190Sstevel@tonic-gate *
7200Sstevel@tonic-gate * These conditions imply that providers that provide Evolving
7210Sstevel@tonic-gate * or better Arguments Data stability must guarantee that all
7220Sstevel@tonic-gate * probes with identical field names in a field of Evolving or
7230Sstevel@tonic-gate * better Name stability have identical argument signatures.
7240Sstevel@tonic-gate */
7250Sstevel@tonic-gate if (m > 0) {
7260Sstevel@tonic-gate if (pvp->pv_desc.dtvd_attr.dtpa_args.dtat_data <
7270Sstevel@tonic-gate DTRACE_STABILITY_EVOLVING) {
7280Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_UNSTABLE);
7290Sstevel@tonic-gate return (NULL);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate if (pvp->pv_desc.dtvd_attr.dtpa_mod.dtat_name >=
7340Sstevel@tonic-gate DTRACE_STABILITY_EVOLVING && m_is_glob) {
7350Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_UNSTABLE);
7360Sstevel@tonic-gate return (NULL);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate if (pvp->pv_desc.dtvd_attr.dtpa_func.dtat_name >=
7400Sstevel@tonic-gate DTRACE_STABILITY_EVOLVING && f_is_glob) {
7410Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_UNSTABLE);
7420Sstevel@tonic-gate return (NULL);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate if (pvp->pv_desc.dtvd_attr.dtpa_name.dtat_name >=
7460Sstevel@tonic-gate DTRACE_STABILITY_EVOLVING && n_is_glob) {
7470Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_UNSTABLE);
7480Sstevel@tonic-gate return (NULL);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate
752265Smws /*
753265Smws * If we matched a probe exported by dtrace(7D), then discover
754265Smws * the real attributes. Otherwise grab the static declaration.
755265Smws */
756265Smws if (pd.dtpd_id != DTRACE_IDNONE)
757265Smws prp = dt_probe_discover(pvp, &pd);
758265Smws else
759265Smws prp = dt_probe_lookup(pvp, pd.dtpd_name);
760265Smws
761265Smws if (prp == NULL)
7620Sstevel@tonic-gate return (NULL); /* dt_errno is set for us */
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate assert(pvp != NULL && prp != NULL);
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate * Compute the probe description attributes by taking the minimum of
7690Sstevel@tonic-gate * the attributes of the specified fields. If no provider is specified
7700Sstevel@tonic-gate * or a glob pattern is used for the provider, use Unstable attributes.
7710Sstevel@tonic-gate */
7720Sstevel@tonic-gate if (pdp->dtpd_provider[0] == '\0' || strisglob(pdp->dtpd_provider))
7730Sstevel@tonic-gate pap = &_dtrace_prvdesc;
7740Sstevel@tonic-gate else
7750Sstevel@tonic-gate pap = &pvp->pv_desc.dtvd_attr;
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate pip->dtp_attr = pap->dtpa_provider;
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate if (!m_is_glob)
7800Sstevel@tonic-gate pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_mod);
7810Sstevel@tonic-gate if (!f_is_glob)
7820Sstevel@tonic-gate pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_func);
7830Sstevel@tonic-gate if (!n_is_glob)
7840Sstevel@tonic-gate pip->dtp_attr = dt_attr_min(pip->dtp_attr, pap->dtpa_name);
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate pip->dtp_arga = pap->dtpa_args;
7870Sstevel@tonic-gate pip->dtp_argv = prp->pr_argv;
7880Sstevel@tonic-gate pip->dtp_argc = prp->pr_argc;
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate return (prp);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate int
dtrace_probe_info(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probeinfo_t * pip)7940Sstevel@tonic-gate dtrace_probe_info(dtrace_hdl_t *dtp,
7950Sstevel@tonic-gate const dtrace_probedesc_t *pdp, dtrace_probeinfo_t *pip)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate return (dt_probe_info(dtp, pdp, pip) != NULL ? 0 : -1);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate /*ARGSUSED*/
8010Sstevel@tonic-gate static int
dt_probe_iter(dt_idhash_t * ihp,dt_ident_t * idp,dt_probe_iter_t * pit)8020Sstevel@tonic-gate dt_probe_iter(dt_idhash_t *ihp, dt_ident_t *idp, dt_probe_iter_t *pit)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate const dt_probe_t *prp = idp->di_data;
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate if (!dt_gmatch(prp->pr_name, pit->pit_pat))
8070Sstevel@tonic-gate return (0); /* continue on and examine next probe in hash */
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate (void) strlcpy(pit->pit_desc.dtpd_name, prp->pr_name, DTRACE_NAMELEN);
8100Sstevel@tonic-gate pit->pit_desc.dtpd_id = idp->di_id;
8110Sstevel@tonic-gate pit->pit_matches++;
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate return (pit->pit_func(pit->pit_hdl, &pit->pit_desc, pit->pit_arg));
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate int
dtrace_probe_iter(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,dtrace_probe_f * func,void * arg)8170Sstevel@tonic-gate dtrace_probe_iter(dtrace_hdl_t *dtp,
8180Sstevel@tonic-gate const dtrace_probedesc_t *pdp, dtrace_probe_f *func, void *arg)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate const char *provider = pdp ? pdp->dtpd_provider : NULL;
8210Sstevel@tonic-gate dtrace_id_t id = DTRACE_IDNONE;
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate dtrace_probedesc_t pd;
8240Sstevel@tonic-gate dt_probe_iter_t pit;
8250Sstevel@tonic-gate int cmd, rv;
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate bzero(&pit, sizeof (pit));
8280Sstevel@tonic-gate pit.pit_hdl = dtp;
8290Sstevel@tonic-gate pit.pit_func = func;
8300Sstevel@tonic-gate pit.pit_arg = arg;
8310Sstevel@tonic-gate pit.pit_pat = pdp ? pdp->dtpd_name : NULL;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate for (pit.pit_pvp = dt_list_next(&dtp->dt_provlist);
8340Sstevel@tonic-gate pit.pit_pvp != NULL; pit.pit_pvp = dt_list_next(pit.pit_pvp)) {
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate if (pit.pit_pvp->pv_flags & DT_PROVIDER_IMPL)
8370Sstevel@tonic-gate continue; /* we'll get these later using dt_ioctl() */
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if (!dt_gmatch(pit.pit_pvp->pv_desc.dtvd_name, provider))
8400Sstevel@tonic-gate continue;
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate (void) strlcpy(pit.pit_desc.dtpd_provider,
8430Sstevel@tonic-gate pit.pit_pvp->pv_desc.dtvd_name, DTRACE_PROVNAMELEN);
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate if ((rv = dt_idhash_iter(pit.pit_pvp->pv_probes,
8460Sstevel@tonic-gate (dt_idhash_f *)dt_probe_iter, &pit)) != 0)
8470Sstevel@tonic-gate return (rv);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate if (pdp != NULL)
8510Sstevel@tonic-gate cmd = DTRACEIOC_PROBEMATCH;
8520Sstevel@tonic-gate else
8530Sstevel@tonic-gate cmd = DTRACEIOC_PROBES;
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate for (;;) {
8560Sstevel@tonic-gate if (pdp != NULL)
8570Sstevel@tonic-gate bcopy(pdp, &pd, sizeof (pd));
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate pd.dtpd_id = id;
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate if (dt_ioctl(dtp, cmd, &pd) != 0)
8620Sstevel@tonic-gate break;
8630Sstevel@tonic-gate else if ((rv = func(dtp, &pd, arg)) != 0)
8640Sstevel@tonic-gate return (rv);
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate pit.pit_matches++;
8670Sstevel@tonic-gate id = pd.dtpd_id + 1;
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate switch (errno) {
8710Sstevel@tonic-gate case ESRCH:
8720Sstevel@tonic-gate case EBADF:
8730Sstevel@tonic-gate return (pit.pit_matches ? 0 : dt_set_errno(dtp, EDT_NOPROBE));
8740Sstevel@tonic-gate case EINVAL:
8750Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADPGLOB));
8760Sstevel@tonic-gate default:
8770Sstevel@tonic-gate return (dt_set_errno(dtp, errno));
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate }
880