xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_ident.c (revision 265:a968e4ece133)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 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/sysmacros.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <alloca.h>
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <sys/procfs_isa.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <dt_ident.h>
400Sstevel@tonic-gate #include <dt_parser.h>
410Sstevel@tonic-gate #include <dt_provider.h>
420Sstevel@tonic-gate #include <dt_strtab.h>
430Sstevel@tonic-gate #include <dt_impl.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Common code for cooking an identifier that uses a typed signature list (we
470Sstevel@tonic-gate  * use this for associative arrays and functions).  If the argument list is
480Sstevel@tonic-gate  * of the same length and types, then return the return type.  Otherwise
490Sstevel@tonic-gate  * print an appropriate compiler error message and abort the compile.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static void
520Sstevel@tonic-gate dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,
530Sstevel@tonic-gate     int argc, dt_node_t *args, const char *prefix, const char *suffix)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	dt_idsig_t *isp = idp->di_data;
560Sstevel@tonic-gate 	int i, compat, mismatch, arglimit;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
590Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (isp->dis_varargs >= 0) {
620Sstevel@tonic-gate 		mismatch = argc < isp->dis_varargs;
630Sstevel@tonic-gate 		arglimit = isp->dis_varargs;
640Sstevel@tonic-gate 	} else if (isp->dis_optargs >= 0) {
650Sstevel@tonic-gate 		mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);
660Sstevel@tonic-gate 		arglimit = argc;
670Sstevel@tonic-gate 	} else {
680Sstevel@tonic-gate 		mismatch = argc != isp->dis_argc;
690Sstevel@tonic-gate 		arglimit = isp->dis_argc;
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (mismatch) {
730Sstevel@tonic-gate 		xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d arg%s"
740Sstevel@tonic-gate 		    "passed, %s%d expected\n", prefix, idp->di_name, suffix,
750Sstevel@tonic-gate 		    argc, argc == 1 ? " " : "s ",
760Sstevel@tonic-gate 		    isp->dis_optargs >= 0 ? "at least " : "",
770Sstevel@tonic-gate 		    isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	for (i = 0; i < arglimit; i++, args = args->dn_list) {
810Sstevel@tonic-gate 		if (isp->dis_args[i].dn_ctfp != NULL)
820Sstevel@tonic-gate 			compat = dt_node_is_argcompat(&isp->dis_args[i], args);
830Sstevel@tonic-gate 		else
840Sstevel@tonic-gate 			compat = 1; /* "@" matches any type */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 		if (!compat) {
870Sstevel@tonic-gate 			xyerror(D_PROTO_ARG,
880Sstevel@tonic-gate 			    "%s%s%s argument #%d is incompatible with "
890Sstevel@tonic-gate 			    "prototype:\n\tprototype: %s\n\t argument: %s\n",
900Sstevel@tonic-gate 			    prefix, idp->di_name, suffix, i + 1,
910Sstevel@tonic-gate 			    dt_node_type_name(&isp->dis_args[i], n1,
920Sstevel@tonic-gate 			    sizeof (n1)),
930Sstevel@tonic-gate 			    dt_node_type_name(args, n2, sizeof (n2)));
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Cook an associative array identifier.  If this is the first time we are
1020Sstevel@tonic-gate  * cooking this array, create its signature based on the argument list.
1030Sstevel@tonic-gate  * Otherwise validate the argument list against the existing signature.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static void
1060Sstevel@tonic-gate dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	if (idp->di_data == NULL) {
1090Sstevel@tonic-gate 		dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));
1100Sstevel@tonic-gate 		char n[DT_TYPE_NAMELEN];
1110Sstevel@tonic-gate 		int i;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 		if (isp == NULL)
1140Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		isp->dis_varargs = -1;
1170Sstevel@tonic-gate 		isp->dis_optargs = -1;
1180Sstevel@tonic-gate 		isp->dis_argc = argc;
1190Sstevel@tonic-gate 		isp->dis_args = NULL;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		if (argc != 0 && (isp->dis_args = calloc(argc,
1220Sstevel@tonic-gate 		    sizeof (dt_node_t))) == NULL) {
1230Sstevel@tonic-gate 			idp->di_data = NULL;
1240Sstevel@tonic-gate 			free(isp);
1250Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		/*
1290Sstevel@tonic-gate 		 * If this identifier has not been explicitly declared earlier,
1300Sstevel@tonic-gate 		 * set the identifier's base type to be our special type <DYN>.
1310Sstevel@tonic-gate 		 * If this ident is an aggregation, it will remain as is.  If
1320Sstevel@tonic-gate 		 * this ident is an associative array, it will be reassigned
1330Sstevel@tonic-gate 		 * based on the result type of the first assignment statement.
1340Sstevel@tonic-gate 		 */
1350Sstevel@tonic-gate 		if (!(idp->di_flags & DT_IDFLG_DECL)) {
1360Sstevel@tonic-gate 			idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);
1370Sstevel@tonic-gate 			idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		for (i = 0; i < argc; i++, args = args->dn_list) {
1410Sstevel@tonic-gate 			if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {
1420Sstevel@tonic-gate 				xyerror(D_KEY_TYPE, "%s expression may not be "
1430Sstevel@tonic-gate 				    "used as %s index: key #%d\n",
1440Sstevel@tonic-gate 				    dt_node_type_name(args, n, sizeof (n)),
1450Sstevel@tonic-gate 				    dt_idkind_name(idp->di_kind), i + 1);
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 			dt_node_type_propagate(args, &isp->dis_args[i]);
1490Sstevel@tonic-gate 			isp->dis_args[i].dn_list = &isp->dis_args[i + 1];
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		if (argc != 0)
1530Sstevel@tonic-gate 			isp->dis_args[argc - 1].dn_list = NULL;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	} else {
1580Sstevel@tonic-gate 		dt_idcook_sign(dnp, idp, argc, args,
1590Sstevel@tonic-gate 		    idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Cook a function call.  If this is the first time we are cooking this
1650Sstevel@tonic-gate  * identifier, create its type signature based on predefined prototype stored
1660Sstevel@tonic-gate  * in di_iarg.  We then validate the argument list against this signature.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate static void
1690Sstevel@tonic-gate dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	if (idp->di_data == NULL) {
1720Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1730Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
1740Sstevel@tonic-gate 		dt_idsig_t *isp;
1750Sstevel@tonic-gate 		char *s, *p1, *p2;
1760Sstevel@tonic-gate 		int i = 0;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		assert(idp->di_iarg != NULL);
1790Sstevel@tonic-gate 		s = alloca(strlen(idp->di_iarg) + 1);
1800Sstevel@tonic-gate 		(void) strcpy(s, idp->di_iarg);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		if ((p2 = strrchr(s, ')')) != NULL)
1830Sstevel@tonic-gate 			*p2 = '\0'; /* mark end of parameter list string */
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		if ((p1 = strchr(s, '(')) != NULL)
1860Sstevel@tonic-gate 			*p1++ = '\0'; /* mark end of return type string */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		if (p1 == NULL || p2 == NULL) {
1890Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "internal error: malformed entry "
1900Sstevel@tonic-gate 			    "for built-in function %s\n", idp->di_name);
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		for (p2 = p1; *p2 != '\0'; p2++) {
1940Sstevel@tonic-gate 			if (!isspace(*p2)) {
1950Sstevel@tonic-gate 				i++;
1960Sstevel@tonic-gate 				break;
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		for (p2 = strchr(p2, ','); p2++ != NULL; i++)
2010Sstevel@tonic-gate 			p2 = strchr(p2, ',');
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * We first allocate a new ident signature structure with the
2050Sstevel@tonic-gate 		 * appropriate number of argument entries, and then look up
2060Sstevel@tonic-gate 		 * the return type and store its CTF data in di_ctfp/type.
2070Sstevel@tonic-gate 		 */
2080Sstevel@tonic-gate 		if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)
2090Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		isp->dis_varargs = -1;
2120Sstevel@tonic-gate 		isp->dis_optargs = -1;
2130Sstevel@tonic-gate 		isp->dis_argc = i;
2140Sstevel@tonic-gate 		isp->dis_args = NULL;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		if (i != 0 && (isp->dis_args = calloc(i,
2170Sstevel@tonic-gate 		    sizeof (dt_node_t))) == NULL) {
2180Sstevel@tonic-gate 			idp->di_data = NULL;
2190Sstevel@tonic-gate 			free(isp);
2200Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		if (dt_type_lookup(s, &dtt) == -1) {
2240Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"
2250Sstevel@tonic-gate 			    " %s\n", idp->di_name, s,
2260Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		if (idp->di_kind == DT_IDENT_AGGFUNC) {
2300Sstevel@tonic-gate 			idp->di_ctfp = DT_DYN_CTFP(dtp);
2310Sstevel@tonic-gate 			idp->di_type = DT_DYN_TYPE(dtp);
2320Sstevel@tonic-gate 		} else {
2330Sstevel@tonic-gate 			idp->di_ctfp = dtt.dtt_ctfp;
2340Sstevel@tonic-gate 			idp->di_type = dtt.dtt_type;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		/*
2380Sstevel@tonic-gate 		 * For each comma-delimited parameter in the prototype string,
2390Sstevel@tonic-gate 		 * we look up the corresponding type and store its CTF data in
2400Sstevel@tonic-gate 		 * the corresponding location in dis_args[].  We also recognize
2410Sstevel@tonic-gate 		 * the special type string "@" to indicate that the specified
2420Sstevel@tonic-gate 		 * parameter may be a D expression of *any* type (represented
2430Sstevel@tonic-gate 		 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR).
2440Sstevel@tonic-gate 		 * If a varargs "..." is present, we record the argument index
2450Sstevel@tonic-gate 		 * in dis_varargs for the benefit of dt_idcook_sign(), above.
2460Sstevel@tonic-gate 		 * If the type of an argument is enclosed in square brackets
2470Sstevel@tonic-gate 		 * (e.g. "[int]"), the argument is considered optional:  the
2480Sstevel@tonic-gate 		 * argument may be absent, but if it is present, it must be of
2490Sstevel@tonic-gate 		 * the specified type.  Note that varargs may not optional,
2500Sstevel@tonic-gate 		 * optional arguments may not follow varargs, and non-optional
2510Sstevel@tonic-gate 		 * arguments may not follow optional arguments.
2520Sstevel@tonic-gate 		 */
2530Sstevel@tonic-gate 		for (i = 0; i < isp->dis_argc; i++, p1 = p2) {
2540Sstevel@tonic-gate 			while (isspace(*p1))
2550Sstevel@tonic-gate 				p1++; /* skip leading whitespace */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 			if ((p2 = strchr(p1, ',')) == NULL)
2580Sstevel@tonic-gate 				p2 = p1 + strlen(p1);
2590Sstevel@tonic-gate 			else
2600Sstevel@tonic-gate 				*p2++ = '\0';
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {
2630Sstevel@tonic-gate 				isp->dis_args[i].dn_ctfp = NULL;
2640Sstevel@tonic-gate 				isp->dis_args[i].dn_type = CTF_ERR;
2650Sstevel@tonic-gate 				if (*p1 == '.')
2660Sstevel@tonic-gate 					isp->dis_varargs = i;
2670Sstevel@tonic-gate 				continue;
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 			if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {
2710Sstevel@tonic-gate 				if (isp->dis_varargs != -1) {
2720Sstevel@tonic-gate 					xyerror(D_UNKNOWN, "optional arg#%d "
2730Sstevel@tonic-gate 					    "may not follow variable arg#%d\n",
2740Sstevel@tonic-gate 					    i + 1, isp->dis_varargs + 1);
2750Sstevel@tonic-gate 				}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 				if (isp->dis_optargs == -1)
2780Sstevel@tonic-gate 					isp->dis_optargs = i;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 				p1[strlen(p1) - 1] = '\0';
2810Sstevel@tonic-gate 				p1++;
2820Sstevel@tonic-gate 			} else if (isp->dis_optargs != -1) {
2830Sstevel@tonic-gate 				xyerror(D_UNKNOWN, "required arg#%d may not "
2840Sstevel@tonic-gate 				    "follow optional arg#%d\n", i + 1,
2850Sstevel@tonic-gate 				    isp->dis_optargs + 1);
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 			if (dt_type_lookup(p1, &dtt) == -1) {
2890Sstevel@tonic-gate 				xyerror(D_UNKNOWN, "failed to resolve type of "
2900Sstevel@tonic-gate 				    "%s arg#%d (%s): %s\n", idp->di_name, i + 1,
2910Sstevel@tonic-gate 				    p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2920Sstevel@tonic-gate 			}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 			dt_node_type_assign(&isp->dis_args[i],
2950Sstevel@tonic-gate 			    dtt.dtt_ctfp, dtt.dtt_type);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	dt_idcook_sign(dnp, idp, argc, args, "", "( )");
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * Cook a reference to the dynamically typed args[] array.  We verify that the
3040Sstevel@tonic-gate  * reference is using a single integer constant, and then construct a new ident
3050Sstevel@tonic-gate  * representing the appropriate type or translation specifically for this node.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate static void
3080Sstevel@tonic-gate dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3110Sstevel@tonic-gate 	dt_probe_t *prp = yypcb->pcb_probe;
3120Sstevel@tonic-gate 
313*265Smws 	dt_node_t tag, *nnp, *xnp;
3140Sstevel@tonic-gate 	dt_xlator_t *dxp;
3150Sstevel@tonic-gate 	dt_ident_t *xidp;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	char n1[DT_TYPE_NAMELEN];
3180Sstevel@tonic-gate 	char n2[DT_TYPE_NAMELEN];
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (argc != 1) {
3210Sstevel@tonic-gate 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
3220Sstevel@tonic-gate 		    "passed, 1 expected\n", idp->di_name, argc,
3230Sstevel@tonic-gate 		    argc == 1 ? " " : "s ");
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (ap->dn_kind != DT_NODE_INT) {
3270Sstevel@tonic-gate 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
3280Sstevel@tonic-gate 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
3290Sstevel@tonic-gate 		    idp->di_name, "integer constant",
3300Sstevel@tonic-gate 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (yypcb->pcb_pdesc == NULL) {
3340Sstevel@tonic-gate 		xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "
3350Sstevel@tonic-gate 		    "of a probe clause\n", idp->di_name);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (prp == NULL) {
3390Sstevel@tonic-gate 		xyerror(D_ARGS_MULTI,
3400Sstevel@tonic-gate 		    "%s[ ] may not be referenced because probe description %s "
3410Sstevel@tonic-gate 		    "matches an unstable set of probes\n", idp->di_name,
3420Sstevel@tonic-gate 		    dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (ap->dn_value >= prp->pr_argc) {
3460Sstevel@tonic-gate 		xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",
3470Sstevel@tonic-gate 		    (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,
3480Sstevel@tonic-gate 		    n1, sizeof (n1)), idp->di_name);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	/*
352*265Smws 	 * Look up the native and translated argument types for the probe.
3530Sstevel@tonic-gate 	 * If no translation is needed, these will be the same underlying node.
3540Sstevel@tonic-gate 	 * If translation is needed, look up the appropriate translator.  Once
3550Sstevel@tonic-gate 	 * we have the appropriate node, create a new dt_ident_t for this node,
3560Sstevel@tonic-gate 	 * assign it the appropriate attributes, and set the type of 'dnp'.
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	xnp = prp->pr_xargv[ap->dn_value];
3590Sstevel@tonic-gate 	nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (xnp->dn_type == CTF_ERR) {
3620Sstevel@tonic-gate 		xyerror(D_ARGS_TYPE, "failed to resolve translated type for "
3630Sstevel@tonic-gate 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (nnp->dn_type == CTF_ERR) {
3670Sstevel@tonic-gate 		xyerror(D_ARGS_TYPE, "failed to resolve native type for "
3680Sstevel@tonic-gate 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
371*265Smws 	if (dtp->dt_xlatemode == DT_XL_STATIC && (
372*265Smws 	    nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {
3730Sstevel@tonic-gate 		dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,
3740Sstevel@tonic-gate 		    idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
375*265Smws 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		if (dnp->dn_ident == NULL)
3780Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		dt_node_type_assign(dnp,
3810Sstevel@tonic-gate 		    prp->pr_argv[ap->dn_value].dtt_ctfp,
3820Sstevel@tonic-gate 		    prp->pr_argv[ap->dn_value].dtt_type);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	} else if ((dxp = dt_xlator_lookup(dtp,
385*265Smws 	    nnp, xnp, DT_XLATE_FUZZY)) != NULL || (
386*265Smws 	    dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),
387*265Smws 	    xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,
3920Sstevel@tonic-gate 		    xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
393*265Smws 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		if (dnp->dn_ident == NULL)
3960Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3970Sstevel@tonic-gate 
398*265Smws 		if (dt_xlator_dynamic(dxp))
399*265Smws 			dxp->dx_arg = (int)ap->dn_value;
400*265Smws 
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * Propagate relevant members from the translator's internal
4030Sstevel@tonic-gate 		 * dt_ident_t.  This code must be kept in sync with the state
4040Sstevel@tonic-gate 		 * that is initialized for idents in dt_xlator_create().
4050Sstevel@tonic-gate 		 */
4060Sstevel@tonic-gate 		dnp->dn_ident->di_data = xidp->di_data;
4070Sstevel@tonic-gate 		dnp->dn_ident->di_ctfp = xidp->di_ctfp;
4080Sstevel@tonic-gate 		dnp->dn_ident->di_type = xidp->di_type;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	} else {
4130Sstevel@tonic-gate 		xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "
4140Sstevel@tonic-gate 		    "is not defined\n", idp->di_name, (longlong_t)ap->dn_value,
4150Sstevel@tonic-gate 		    dt_node_type_name(nnp, n1, sizeof (n1)),
4160Sstevel@tonic-gate 		    dt_node_type_name(xnp, n2, sizeof (n2)));
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);
4200Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == idp->di_id);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate static void
4240Sstevel@tonic-gate dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
4270Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4280Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (argc != 1) {
4310Sstevel@tonic-gate 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
4320Sstevel@tonic-gate 		    "passed, 1 expected\n", idp->di_name,
4330Sstevel@tonic-gate 		    argc, argc == 1 ? " " : "s ");
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (ap->dn_kind != DT_NODE_INT) {
4370Sstevel@tonic-gate 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
4380Sstevel@tonic-gate 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
4390Sstevel@tonic-gate 		    idp->di_name, "integer constant",
4400Sstevel@tonic-gate 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {
4440Sstevel@tonic-gate 		xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",
4450Sstevel@tonic-gate 		    (longlong_t)ap->dn_value, idp->di_name);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (dt_type_lookup("uint64_t", &dtt) == -1) {
4490Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",
4500Sstevel@tonic-gate 		    idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	idp->di_ctfp = dtt.dtt_ctfp;
4540Sstevel@tonic-gate 	idp->di_type = dtt.dtt_type;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*ARGSUSED*/
4600Sstevel@tonic-gate static void
4610Sstevel@tonic-gate dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	if (idp->di_type == CTF_ERR) {
4640Sstevel@tonic-gate 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4650Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {
4680Sstevel@tonic-gate 			xyerror(D_UNKNOWN,
4690Sstevel@tonic-gate 			    "failed to resolve type %s for identifier %s: %s\n",
4700Sstevel@tonic-gate 			    (const char *)idp->di_iarg, idp->di_name,
4710Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		idp->di_ctfp = dtt.dtt_ctfp;
4750Sstevel@tonic-gate 		idp->di_type = dtt.dtt_type;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*ARGSUSED*/
4820Sstevel@tonic-gate static void
4830Sstevel@tonic-gate dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)
4860Sstevel@tonic-gate 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate static void
4900Sstevel@tonic-gate dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	if (idp->di_kind == DT_IDENT_ARRAY)
4930Sstevel@tonic-gate 		dt_idcook_assc(dnp, idp, argc, args);
4940Sstevel@tonic-gate 	else
4950Sstevel@tonic-gate 		dt_idcook_thaw(dnp, idp, argc, args);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate static void
4990Sstevel@tonic-gate dt_iddtor_sign(dt_ident_t *idp)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	if (idp->di_data != NULL)
5020Sstevel@tonic-gate 		free(((dt_idsig_t *)idp->di_data)->dis_args);
5030Sstevel@tonic-gate 	free(idp->di_data);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate static void
5070Sstevel@tonic-gate dt_iddtor_free(dt_ident_t *idp)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	free(idp->di_data);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate static void
5130Sstevel@tonic-gate dt_iddtor_inline(dt_ident_t *idp)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	dt_idnode_t *inp = idp->di_iarg;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (inp != NULL) {
5180Sstevel@tonic-gate 		dt_node_link_free(&inp->din_list);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		if (inp->din_hash != NULL)
5210Sstevel@tonic-gate 			dt_idhash_destroy(inp->din_hash);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		free(inp->din_argv);
5240Sstevel@tonic-gate 		free(inp);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (idp->di_kind == DT_IDENT_ARRAY)
5280Sstevel@tonic-gate 		dt_iddtor_sign(idp);
5290Sstevel@tonic-gate 	else
5300Sstevel@tonic-gate 		dt_iddtor_free(idp);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*ARGSUSED*/
5340Sstevel@tonic-gate static void
5350Sstevel@tonic-gate dt_iddtor_none(dt_ident_t *idp)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	/* do nothing */
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate static void
5410Sstevel@tonic-gate dt_iddtor_probe(dt_ident_t *idp)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	if (idp->di_data != NULL)
5440Sstevel@tonic-gate 		dt_probe_destroy(idp->di_data);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static size_t
5480Sstevel@tonic-gate dt_idsize_type(dt_ident_t *idp)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	return (ctf_type_size(idp->di_ctfp, idp->di_type));
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*ARGSUSED*/
5540Sstevel@tonic-gate static size_t
5550Sstevel@tonic-gate dt_idsize_none(dt_ident_t *idp)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	return (0);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate const dt_idops_t dt_idops_assc = {
5610Sstevel@tonic-gate 	dt_idcook_assc,
5620Sstevel@tonic-gate 	dt_iddtor_sign,
5630Sstevel@tonic-gate 	dt_idsize_none,
5640Sstevel@tonic-gate };
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate const dt_idops_t dt_idops_func = {
5670Sstevel@tonic-gate 	dt_idcook_func,
5680Sstevel@tonic-gate 	dt_iddtor_sign,
5690Sstevel@tonic-gate 	dt_idsize_none,
5700Sstevel@tonic-gate };
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate const dt_idops_t dt_idops_args = {
5730Sstevel@tonic-gate 	dt_idcook_args,
5740Sstevel@tonic-gate 	dt_iddtor_none,
5750Sstevel@tonic-gate 	dt_idsize_none,
5760Sstevel@tonic-gate };
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate const dt_idops_t dt_idops_regs = {
5790Sstevel@tonic-gate 	dt_idcook_regs,
5800Sstevel@tonic-gate 	dt_iddtor_free,
5810Sstevel@tonic-gate 	dt_idsize_none,
5820Sstevel@tonic-gate };
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate const dt_idops_t dt_idops_type = {
5850Sstevel@tonic-gate 	dt_idcook_type,
5860Sstevel@tonic-gate 	dt_iddtor_free,
5870Sstevel@tonic-gate 	dt_idsize_type,
5880Sstevel@tonic-gate };
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate const dt_idops_t dt_idops_thaw = {
5910Sstevel@tonic-gate 	dt_idcook_thaw,
5920Sstevel@tonic-gate 	dt_iddtor_free,
5930Sstevel@tonic-gate 	dt_idsize_type,
5940Sstevel@tonic-gate };
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate const dt_idops_t dt_idops_inline = {
5970Sstevel@tonic-gate 	dt_idcook_inline,
5980Sstevel@tonic-gate 	dt_iddtor_inline,
5990Sstevel@tonic-gate 	dt_idsize_type,
6000Sstevel@tonic-gate };
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate const dt_idops_t dt_idops_probe = {
6030Sstevel@tonic-gate 	dt_idcook_thaw,
6040Sstevel@tonic-gate 	dt_iddtor_probe,
6050Sstevel@tonic-gate 	dt_idsize_none,
6060Sstevel@tonic-gate };
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate static void
6090Sstevel@tonic-gate dt_idhash_populate(dt_idhash_t *dhp)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	const dt_ident_t *idp = dhp->dh_tmpl;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */
6140Sstevel@tonic-gate 	dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	for (; idp->di_name != NULL; idp++) {
6170Sstevel@tonic-gate 		if (dt_idhash_insert(dhp, idp->di_name,
6180Sstevel@tonic-gate 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
6190Sstevel@tonic-gate 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
6200Sstevel@tonic-gate 		    idp->di_iarg, 0) == NULL)
6210Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate dt_idhash_t *
6260Sstevel@tonic-gate dt_idhash_create(const char *name, const dt_ident_t *tmpl,
6270Sstevel@tonic-gate     uint_t min, uint_t max)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	dt_idhash_t *dhp;
6300Sstevel@tonic-gate 	size_t size;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	assert(min <= max);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	size = sizeof (dt_idhash_t) +
6350Sstevel@tonic-gate 	    sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if ((dhp = malloc(size)) == NULL)
6380Sstevel@tonic-gate 		return (NULL);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	bzero(dhp, size);
6410Sstevel@tonic-gate 	dhp->dh_name = name;
6420Sstevel@tonic-gate 	dhp->dh_tmpl = tmpl;
6430Sstevel@tonic-gate 	dhp->dh_nextid = min;
6440Sstevel@tonic-gate 	dhp->dh_minid = min;
6450Sstevel@tonic-gate 	dhp->dh_maxid = max;
6460Sstevel@tonic-gate 	dhp->dh_hashsz = _dtrace_strbuckets;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	return (dhp);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Destroy an entire identifier hash.  This must be done using two passes with
6530Sstevel@tonic-gate  * an inlined version of dt_ident_destroy() to avoid referencing freed memory.
6540Sstevel@tonic-gate  * In the first pass di_dtor() is called for all identifiers; then the second
6550Sstevel@tonic-gate  * pass frees the actual dt_ident_t's.  These must be done separately because
6560Sstevel@tonic-gate  * a di_dtor() may operate on data structures which contain references to other
6570Sstevel@tonic-gate  * identifiers inside of this hash itself (e.g. a global inline definition
6580Sstevel@tonic-gate  * which contains a parse tree that refers to another global variable).
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate void
6610Sstevel@tonic-gate dt_idhash_destroy(dt_idhash_t *dhp)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate 	dt_ident_t *idp, *next;
6640Sstevel@tonic-gate 	ulong_t i;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
6670Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
6680Sstevel@tonic-gate 			next = idp->di_next;
6690Sstevel@tonic-gate 			idp->di_ops->di_dtor(idp);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
6740Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
6750Sstevel@tonic-gate 			next = idp->di_next;
6760Sstevel@tonic-gate 			free(idp->di_name);
6770Sstevel@tonic-gate 			free(idp);
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	free(dhp);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate void
6850Sstevel@tonic-gate dt_idhash_update(dt_idhash_t *dhp)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	uint_t nextid = dhp->dh_minid;
6880Sstevel@tonic-gate 	dt_ident_t *idp;
6890Sstevel@tonic-gate 	ulong_t i;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_hashsz; i++) {
6920Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {
6930Sstevel@tonic-gate 			if (idp->di_kind == DT_IDENT_ARRAY ||
6940Sstevel@tonic-gate 			    idp->di_kind == DT_IDENT_SCALAR)
6950Sstevel@tonic-gate 				nextid = MAX(nextid, idp->di_id + 1);
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	dhp->dh_nextid = nextid;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate dt_ident_t *
7030Sstevel@tonic-gate dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	size_t len;
7060Sstevel@tonic-gate 	ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
7070Sstevel@tonic-gate 	dt_ident_t *idp;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7100Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
7130Sstevel@tonic-gate 		if (strcmp(idp->di_name, name) == 0)
7140Sstevel@tonic-gate 			return (idp);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	return (NULL);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate int
7210Sstevel@tonic-gate dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	if (dhp->dh_nextid >= dhp->dh_maxid)
7240Sstevel@tonic-gate 		return (-1); /* no more id's are free to allocate */
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	*p = dhp->dh_nextid++;
7270Sstevel@tonic-gate 	return (0);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate ulong_t
7310Sstevel@tonic-gate dt_idhash_size(const dt_idhash_t *dhp)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	return (dhp->dh_nelems);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate const char *
7370Sstevel@tonic-gate dt_idhash_name(const dt_idhash_t *dhp)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	return (dhp->dh_name);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate dt_ident_t *
7430Sstevel@tonic-gate dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
7440Sstevel@tonic-gate     ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
7450Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg, ulong_t gen)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate 	dt_ident_t *idp;
7480Sstevel@tonic-gate 	ulong_t h;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7510Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	idp = dt_ident_create(name, kind, flags, id,
7540Sstevel@tonic-gate 	    attr, vers, ops, iarg, gen);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (idp == NULL)
7570Sstevel@tonic-gate 		return (NULL);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
7600Sstevel@tonic-gate 	idp->di_next = dhp->dh_hash[h];
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	dhp->dh_hash[h] = idp;
7630Sstevel@tonic-gate 	dhp->dh_nelems++;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (dhp->dh_defer != NULL)
7660Sstevel@tonic-gate 		dhp->dh_defer(dhp, idp);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	return (idp);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate void
7720Sstevel@tonic-gate dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	ulong_t h;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
7770Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
7800Sstevel@tonic-gate 	idp->di_next = dhp->dh_hash[h];
7810Sstevel@tonic-gate 	idp->di_flags &= ~DT_IDFLG_ORPHAN;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	dhp->dh_hash[h] = idp;
7840Sstevel@tonic-gate 	dhp->dh_nelems++;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (dhp->dh_defer != NULL)
7870Sstevel@tonic-gate 		dhp->dh_defer(dhp, idp);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate void
7910Sstevel@tonic-gate dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	size_t len;
7940Sstevel@tonic-gate 	ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
7950Sstevel@tonic-gate 	dt_ident_t **pp = &dhp->dh_hash[h];
7960Sstevel@tonic-gate 	dt_ident_t *idp;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
7990Sstevel@tonic-gate 		if (idp == key)
8000Sstevel@tonic-gate 			break;
8010Sstevel@tonic-gate 		else
8020Sstevel@tonic-gate 			pp = &idp->di_next;
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	assert(idp == key);
8060Sstevel@tonic-gate 	*pp = idp->di_next;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	assert(dhp->dh_nelems != 0);
8090Sstevel@tonic-gate 	dhp->dh_nelems--;
8100Sstevel@tonic-gate 
811*265Smws 	if (!(idp->di_flags & DT_IDFLG_ORPHAN))
812*265Smws 		dt_ident_destroy(idp);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate static int
8160Sstevel@tonic-gate dt_idhash_comp(const void *lp, const void *rp)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	const dt_ident_t *lhs = *((const dt_ident_t **)lp);
8190Sstevel@tonic-gate 	const dt_ident_t *rhs = *((const dt_ident_t **)rp);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (lhs->di_id != rhs->di_id)
8220Sstevel@tonic-gate 		return ((int)(lhs->di_id - rhs->di_id));
8230Sstevel@tonic-gate 	else
8240Sstevel@tonic-gate 		return (strcmp(lhs->di_name, rhs->di_name));
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate int
8280Sstevel@tonic-gate dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	dt_ident_t **ids;
8310Sstevel@tonic-gate 	dt_ident_t *idp;
8320Sstevel@tonic-gate 	ulong_t i, j;
8330Sstevel@tonic-gate 	int rv;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	if (dhp->dh_tmpl != NULL)
8360Sstevel@tonic-gate 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	ids = alloca(sizeof (dt_ident_t *) * dhp->dh_nelems);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
8410Sstevel@tonic-gate 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
8420Sstevel@tonic-gate 			ids[j++] = idp;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	for (i = 0; i < dhp->dh_nelems; i++) {
8480Sstevel@tonic-gate 		if ((rv = func(dhp, ids[i], data)) != 0)
8490Sstevel@tonic-gate 			return (rv);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	return (0);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate dt_ident_t *
8560Sstevel@tonic-gate dt_idstack_lookup(dt_idstack_t *sp, const char *name)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	dt_idhash_t *dhp;
8590Sstevel@tonic-gate 	dt_ident_t *idp;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	for (dhp = dt_list_prev(&sp->dids_list);
8620Sstevel@tonic-gate 	    dhp != NULL; dhp = dt_list_prev(dhp)) {
8630Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
8640Sstevel@tonic-gate 			return (idp);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	return (NULL);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate void
8710Sstevel@tonic-gate dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	dt_list_append(&sp->dids_list, dhp);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate void
8770Sstevel@tonic-gate dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	assert(dt_list_prev(&sp->dids_list) == dhp);
8800Sstevel@tonic-gate 	dt_list_delete(&sp->dids_list, dhp);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate dt_ident_t *
8840Sstevel@tonic-gate dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
8850Sstevel@tonic-gate     dtrace_attribute_t attr, uint_t vers,
8860Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg, ulong_t gen)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	dt_ident_t *idp;
8890Sstevel@tonic-gate 	char *s = NULL;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if ((name != NULL && (s = strdup(name)) == NULL) ||
8920Sstevel@tonic-gate 	    (idp = malloc(sizeof (dt_ident_t))) == NULL) {
8930Sstevel@tonic-gate 		free(s);
8940Sstevel@tonic-gate 		return (NULL);
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	idp->di_name = s;
8980Sstevel@tonic-gate 	idp->di_kind = kind;
8990Sstevel@tonic-gate 	idp->di_flags = flags;
9000Sstevel@tonic-gate 	idp->di_id = id;
9010Sstevel@tonic-gate 	idp->di_attr = attr;
9020Sstevel@tonic-gate 	idp->di_vers = vers;
9030Sstevel@tonic-gate 	idp->di_ops = ops;
9040Sstevel@tonic-gate 	idp->di_iarg = iarg;
9050Sstevel@tonic-gate 	idp->di_data = NULL;
9060Sstevel@tonic-gate 	idp->di_ctfp = NULL;
9070Sstevel@tonic-gate 	idp->di_type = CTF_ERR;
9080Sstevel@tonic-gate 	idp->di_next = NULL;
9090Sstevel@tonic-gate 	idp->di_gen = gen;
9100Sstevel@tonic-gate 	idp->di_lineno = yylineno;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	return (idp);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate  * Destroy an individual identifier.  This code must be kept in sync with the
9170Sstevel@tonic-gate  * dt_idhash_destroy() function below, which separates out the call to di_dtor.
9180Sstevel@tonic-gate  */
9190Sstevel@tonic-gate void
9200Sstevel@tonic-gate dt_ident_destroy(dt_ident_t *idp)
9210Sstevel@tonic-gate {
9220Sstevel@tonic-gate 	idp->di_ops->di_dtor(idp);
9230Sstevel@tonic-gate 	free(idp->di_name);
9240Sstevel@tonic-gate 	free(idp);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate void
9280Sstevel@tonic-gate dt_ident_morph(dt_ident_t *idp, ushort_t kind,
9290Sstevel@tonic-gate     const dt_idops_t *ops, void *iarg)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	idp->di_ops->di_dtor(idp);
9320Sstevel@tonic-gate 	idp->di_kind = kind;
9330Sstevel@tonic-gate 	idp->di_ops = ops;
9340Sstevel@tonic-gate 	idp->di_iarg = iarg;
9350Sstevel@tonic-gate 	idp->di_data = NULL;
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate dtrace_attribute_t
9390Sstevel@tonic-gate dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	dtrace_attribute_t attr;
9420Sstevel@tonic-gate 	dt_node_t *args, *argp;
9430Sstevel@tonic-gate 	int argc = 0;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
9460Sstevel@tonic-gate 	args = pargp ? *pargp : NULL;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	for (argp = args; argp != NULL; argp = argp->dn_list)
9490Sstevel@tonic-gate 		argc++;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	idp->di_ops->di_cook(dnp, idp, argc, args);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	if (idp->di_flags & DT_IDFLG_USER)
9540Sstevel@tonic-gate 		dnp->dn_flags |= DT_NF_USERLAND;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	return (dt_attr_min(attr, idp->di_attr));
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate void
9600Sstevel@tonic-gate dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	idp->di_ctfp = fp;
9630Sstevel@tonic-gate 	idp->di_type = type;
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate dt_ident_t *
9670Sstevel@tonic-gate dt_ident_resolve(dt_ident_t *idp)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	while (idp->di_flags & DT_IDFLG_INLINE) {
9700Sstevel@tonic-gate 		const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
9710Sstevel@tonic-gate 
972*265Smws 		if (dnp == NULL)
973*265Smws 			break; /* can't resolve any further yet */
974*265Smws 
9750Sstevel@tonic-gate 		switch (dnp->dn_kind) {
9760Sstevel@tonic-gate 		case DT_NODE_VAR:
9770Sstevel@tonic-gate 		case DT_NODE_SYM:
9780Sstevel@tonic-gate 		case DT_NODE_FUNC:
9790Sstevel@tonic-gate 		case DT_NODE_AGG:
9800Sstevel@tonic-gate 		case DT_NODE_INLINE:
9810Sstevel@tonic-gate 		case DT_NODE_PROBE:
9820Sstevel@tonic-gate 			idp = dnp->dn_ident;
9830Sstevel@tonic-gate 			continue;
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		if (dt_node_is_dynamic(dnp))
9870Sstevel@tonic-gate 			idp = dnp->dn_ident;
9880Sstevel@tonic-gate 		else
9890Sstevel@tonic-gate 			break;
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	return (idp);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate size_t
9960Sstevel@tonic-gate dt_ident_size(dt_ident_t *idp)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	idp = dt_ident_resolve(idp);
9990Sstevel@tonic-gate 	return (idp->di_ops->di_size(idp));
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate int
10030Sstevel@tonic-gate dt_ident_unref(const dt_ident_t *idp)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
10060Sstevel@tonic-gate 	    (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate const char *
10100Sstevel@tonic-gate dt_idkind_name(uint_t kind)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	switch (kind) {
10130Sstevel@tonic-gate 	case DT_IDENT_ARRAY:	return ("associative array");
10140Sstevel@tonic-gate 	case DT_IDENT_SCALAR:	return ("scalar");
10150Sstevel@tonic-gate 	case DT_IDENT_PTR:	return ("pointer");
10160Sstevel@tonic-gate 	case DT_IDENT_FUNC:	return ("function");
10170Sstevel@tonic-gate 	case DT_IDENT_AGG:	return ("aggregation");
10180Sstevel@tonic-gate 	case DT_IDENT_AGGFUNC:	return ("aggregating function");
10190Sstevel@tonic-gate 	case DT_IDENT_ACTFUNC:	return ("tracing function");
10200Sstevel@tonic-gate 	case DT_IDENT_XLSOU:	return ("translated data");
10210Sstevel@tonic-gate 	case DT_IDENT_XLPTR:	return ("pointer to translated data");
10220Sstevel@tonic-gate 	case DT_IDENT_SYMBOL:	return ("external symbol reference");
10230Sstevel@tonic-gate 	case DT_IDENT_ENUM:	return ("enumerator");
10240Sstevel@tonic-gate 	case DT_IDENT_PRAGAT:	return ("#pragma attributes");
10250Sstevel@tonic-gate 	case DT_IDENT_PRAGBN:	return ("#pragma binding");
10260Sstevel@tonic-gate 	case DT_IDENT_PROBE:	return ("probe definition");
10270Sstevel@tonic-gate 	default:		return ("<?>");
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate }
1030