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