xref: /titanic_44/usr/src/lib/libdtrace/common/dt_ident.c (revision f56c1286e5113aa46bd6e723da14d30c123153f2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/sysmacros.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <alloca.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <sys/procfs_isa.h>
37 #include <limits.h>
38 
39 #include <dt_ident.h>
40 #include <dt_parser.h>
41 #include <dt_provider.h>
42 #include <dt_strtab.h>
43 #include <dt_impl.h>
44 
45 /*
46  * Common code for cooking an identifier that uses a typed signature list (we
47  * use this for associative arrays and functions).  If the argument list is
48  * of the same length and types, then return the return type.  Otherwise
49  * print an appropriate compiler error message and abort the compile.
50  */
51 static void
52 dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,
53     int argc, dt_node_t *args, const char *prefix, const char *suffix)
54 {
55 	dt_idsig_t *isp = idp->di_data;
56 	int i, compat, mismatch, arglimit, iskey;
57 
58 	char n1[DT_TYPE_NAMELEN];
59 	char n2[DT_TYPE_NAMELEN];
60 
61 	iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG;
62 
63 	if (isp->dis_varargs >= 0) {
64 		mismatch = argc < isp->dis_varargs;
65 		arglimit = isp->dis_varargs;
66 	} else if (isp->dis_optargs >= 0) {
67 		mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);
68 		arglimit = argc;
69 	} else {
70 		mismatch = argc != isp->dis_argc;
71 		arglimit = isp->dis_argc;
72 	}
73 
74 	if (mismatch) {
75 		xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s"
76 		    "passed, %s%d expected\n", prefix, idp->di_name, suffix,
77 		    argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ",
78 		    isp->dis_optargs >= 0 ? "at least " : "",
79 		    isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);
80 	}
81 
82 	for (i = 0; i < arglimit; i++, args = args->dn_list) {
83 		if (isp->dis_args[i].dn_ctfp != NULL)
84 			compat = dt_node_is_argcompat(&isp->dis_args[i], args);
85 		else
86 			compat = 1; /* "@" matches any type */
87 
88 		if (!compat) {
89 			xyerror(D_PROTO_ARG,
90 			    "%s%s%s %s #%d is incompatible with "
91 			    "prototype:\n\tprototype: %s\n\t%9s: %s\n",
92 			    prefix, idp->di_name, suffix,
93 			    iskey ? "key" : "argument", i + 1,
94 			    dt_node_type_name(&isp->dis_args[i], n1,
95 			    sizeof (n1)),
96 			    iskey ? "key" : "argument",
97 			    dt_node_type_name(args, n2, sizeof (n2)));
98 		}
99 	}
100 
101 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
102 }
103 
104 /*
105  * Cook an associative array identifier.  If this is the first time we are
106  * cooking this array, create its signature based on the argument list.
107  * Otherwise validate the argument list against the existing signature.
108  */
109 static void
110 dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
111 {
112 	if (idp->di_data == NULL) {
113 		dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));
114 		char n[DT_TYPE_NAMELEN];
115 		int i;
116 
117 		if (isp == NULL)
118 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
119 
120 		isp->dis_varargs = -1;
121 		isp->dis_optargs = -1;
122 		isp->dis_argc = argc;
123 		isp->dis_args = NULL;
124 		isp->dis_auxinfo = 0;
125 
126 		if (argc != 0 && (isp->dis_args = calloc(argc,
127 		    sizeof (dt_node_t))) == NULL) {
128 			idp->di_data = NULL;
129 			free(isp);
130 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
131 		}
132 
133 		/*
134 		 * If this identifier has not been explicitly declared earlier,
135 		 * set the identifier's base type to be our special type <DYN>.
136 		 * If this ident is an aggregation, it will remain as is.  If
137 		 * this ident is an associative array, it will be reassigned
138 		 * based on the result type of the first assignment statement.
139 		 */
140 		if (!(idp->di_flags & DT_IDFLG_DECL)) {
141 			idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);
142 			idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);
143 		}
144 
145 		for (i = 0; i < argc; i++, args = args->dn_list) {
146 			if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {
147 				xyerror(D_KEY_TYPE, "%s expression may not be "
148 				    "used as %s index: key #%d\n",
149 				    dt_node_type_name(args, n, sizeof (n)),
150 				    dt_idkind_name(idp->di_kind), i + 1);
151 			}
152 
153 			dt_node_type_propagate(args, &isp->dis_args[i]);
154 			isp->dis_args[i].dn_list = &isp->dis_args[i + 1];
155 		}
156 
157 		if (argc != 0)
158 			isp->dis_args[argc - 1].dn_list = NULL;
159 
160 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
161 
162 	} else {
163 		dt_idcook_sign(dnp, idp, argc, args,
164 		    idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");
165 	}
166 }
167 
168 /*
169  * Cook a function call.  If this is the first time we are cooking this
170  * identifier, create its type signature based on predefined prototype stored
171  * in di_iarg.  We then validate the argument list against this signature.
172  */
173 static void
174 dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
175 {
176 	if (idp->di_data == NULL) {
177 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
178 		dtrace_typeinfo_t dtt;
179 		dt_idsig_t *isp;
180 		char *s, *p1, *p2;
181 		int i = 0;
182 
183 		assert(idp->di_iarg != NULL);
184 		s = alloca(strlen(idp->di_iarg) + 1);
185 		(void) strcpy(s, idp->di_iarg);
186 
187 		if ((p2 = strrchr(s, ')')) != NULL)
188 			*p2 = '\0'; /* mark end of parameter list string */
189 
190 		if ((p1 = strchr(s, '(')) != NULL)
191 			*p1++ = '\0'; /* mark end of return type string */
192 
193 		if (p1 == NULL || p2 == NULL) {
194 			xyerror(D_UNKNOWN, "internal error: malformed entry "
195 			    "for built-in function %s\n", idp->di_name);
196 		}
197 
198 		for (p2 = p1; *p2 != '\0'; p2++) {
199 			if (!isspace(*p2)) {
200 				i++;
201 				break;
202 			}
203 		}
204 
205 		for (p2 = strchr(p2, ','); p2++ != NULL; i++)
206 			p2 = strchr(p2, ',');
207 
208 		/*
209 		 * We first allocate a new ident signature structure with the
210 		 * appropriate number of argument entries, and then look up
211 		 * the return type and store its CTF data in di_ctfp/type.
212 		 */
213 		if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)
214 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
215 
216 		isp->dis_varargs = -1;
217 		isp->dis_optargs = -1;
218 		isp->dis_argc = i;
219 		isp->dis_args = NULL;
220 		isp->dis_auxinfo = 0;
221 
222 		if (i != 0 && (isp->dis_args = calloc(i,
223 		    sizeof (dt_node_t))) == NULL) {
224 			idp->di_data = NULL;
225 			free(isp);
226 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
227 		}
228 
229 		if (dt_type_lookup(s, &dtt) == -1) {
230 			xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"
231 			    " %s\n", idp->di_name, s,
232 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
233 		}
234 
235 		if (idp->di_kind == DT_IDENT_AGGFUNC) {
236 			idp->di_ctfp = DT_DYN_CTFP(dtp);
237 			idp->di_type = DT_DYN_TYPE(dtp);
238 		} else {
239 			idp->di_ctfp = dtt.dtt_ctfp;
240 			idp->di_type = dtt.dtt_type;
241 		}
242 
243 		/*
244 		 * For each comma-delimited parameter in the prototype string,
245 		 * we look up the corresponding type and store its CTF data in
246 		 * the corresponding location in dis_args[].  We also recognize
247 		 * the special type string "@" to indicate that the specified
248 		 * parameter may be a D expression of *any* type (represented
249 		 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR).
250 		 * If a varargs "..." is present, we record the argument index
251 		 * in dis_varargs for the benefit of dt_idcook_sign(), above.
252 		 * If the type of an argument is enclosed in square brackets
253 		 * (e.g. "[int]"), the argument is considered optional:  the
254 		 * argument may be absent, but if it is present, it must be of
255 		 * the specified type.  Note that varargs may not optional,
256 		 * optional arguments may not follow varargs, and non-optional
257 		 * arguments may not follow optional arguments.
258 		 */
259 		for (i = 0; i < isp->dis_argc; i++, p1 = p2) {
260 			while (isspace(*p1))
261 				p1++; /* skip leading whitespace */
262 
263 			if ((p2 = strchr(p1, ',')) == NULL)
264 				p2 = p1 + strlen(p1);
265 			else
266 				*p2++ = '\0';
267 
268 			if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {
269 				isp->dis_args[i].dn_ctfp = NULL;
270 				isp->dis_args[i].dn_type = CTF_ERR;
271 				if (*p1 == '.')
272 					isp->dis_varargs = i;
273 				continue;
274 			}
275 
276 			if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {
277 				if (isp->dis_varargs != -1) {
278 					xyerror(D_UNKNOWN, "optional arg#%d "
279 					    "may not follow variable arg#%d\n",
280 					    i + 1, isp->dis_varargs + 1);
281 				}
282 
283 				if (isp->dis_optargs == -1)
284 					isp->dis_optargs = i;
285 
286 				p1[strlen(p1) - 1] = '\0';
287 				p1++;
288 			} else if (isp->dis_optargs != -1) {
289 				xyerror(D_UNKNOWN, "required arg#%d may not "
290 				    "follow optional arg#%d\n", i + 1,
291 				    isp->dis_optargs + 1);
292 			}
293 
294 			if (dt_type_lookup(p1, &dtt) == -1) {
295 				xyerror(D_UNKNOWN, "failed to resolve type of "
296 				    "%s arg#%d (%s): %s\n", idp->di_name, i + 1,
297 				    p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));
298 			}
299 
300 			dt_node_type_assign(&isp->dis_args[i],
301 			    dtt.dtt_ctfp, dtt.dtt_type);
302 		}
303 	}
304 
305 	dt_idcook_sign(dnp, idp, argc, args, "", "( )");
306 }
307 
308 /*
309  * Cook a reference to the dynamically typed args[] array.  We verify that the
310  * reference is using a single integer constant, and then construct a new ident
311  * representing the appropriate type or translation specifically for this node.
312  */
313 static void
314 dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
315 {
316 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
317 	dt_probe_t *prp = yypcb->pcb_probe;
318 
319 	dt_node_t tag, *nnp, *xnp;
320 	dt_xlator_t *dxp;
321 	dt_ident_t *xidp;
322 
323 	char n1[DT_TYPE_NAMELEN];
324 	char n2[DT_TYPE_NAMELEN];
325 
326 	if (argc != 1) {
327 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
328 		    "passed, 1 expected\n", idp->di_name, argc,
329 		    argc == 1 ? " " : "s ");
330 	}
331 
332 	if (ap->dn_kind != DT_NODE_INT) {
333 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
334 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
335 		    idp->di_name, "integer constant",
336 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));
337 	}
338 
339 	if (yypcb->pcb_pdesc == NULL) {
340 		xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "
341 		    "of a probe clause\n", idp->di_name);
342 	}
343 
344 	if (prp == NULL) {
345 		xyerror(D_ARGS_MULTI,
346 		    "%s[ ] may not be referenced because probe description %s "
347 		    "matches an unstable set of probes\n", idp->di_name,
348 		    dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));
349 	}
350 
351 	if (ap->dn_value >= prp->pr_argc) {
352 		xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",
353 		    (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,
354 		    n1, sizeof (n1)), idp->di_name);
355 	}
356 
357 	/*
358 	 * Look up the native and translated argument types for the probe.
359 	 * If no translation is needed, these will be the same underlying node.
360 	 * If translation is needed, look up the appropriate translator.  Once
361 	 * we have the appropriate node, create a new dt_ident_t for this node,
362 	 * assign it the appropriate attributes, and set the type of 'dnp'.
363 	 */
364 	xnp = prp->pr_xargv[ap->dn_value];
365 	nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];
366 
367 	if (xnp->dn_type == CTF_ERR) {
368 		xyerror(D_ARGS_TYPE, "failed to resolve translated type for "
369 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
370 	}
371 
372 	if (nnp->dn_type == CTF_ERR) {
373 		xyerror(D_ARGS_TYPE, "failed to resolve native type for "
374 		    "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
375 	}
376 
377 	if (dtp->dt_xlatemode == DT_XL_STATIC && (
378 	    nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {
379 		dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,
380 		    idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
381 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
382 
383 		if (dnp->dn_ident == NULL)
384 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
385 
386 		dt_node_type_assign(dnp,
387 		    prp->pr_argv[ap->dn_value].dtt_ctfp,
388 		    prp->pr_argv[ap->dn_value].dtt_type);
389 
390 	} else if ((dxp = dt_xlator_lookup(dtp,
391 	    nnp, xnp, DT_XLATE_FUZZY)) != NULL || (
392 	    dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),
393 	    xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {
394 
395 		xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);
396 
397 		dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,
398 		    xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
399 		    idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
400 
401 		if (dnp->dn_ident == NULL)
402 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
403 
404 		if (dt_xlator_dynamic(dxp))
405 			dxp->dx_arg = (int)ap->dn_value;
406 
407 		/*
408 		 * Propagate relevant members from the translator's internal
409 		 * dt_ident_t.  This code must be kept in sync with the state
410 		 * that is initialized for idents in dt_xlator_create().
411 		 */
412 		dnp->dn_ident->di_data = xidp->di_data;
413 		dnp->dn_ident->di_ctfp = xidp->di_ctfp;
414 		dnp->dn_ident->di_type = xidp->di_type;
415 
416 		dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
417 
418 	} else {
419 		xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "
420 		    "is not defined\n", idp->di_name, (longlong_t)ap->dn_value,
421 		    dt_node_type_name(nnp, n1, sizeof (n1)),
422 		    dt_node_type_name(xnp, n2, sizeof (n2)));
423 	}
424 
425 	assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);
426 	assert(dnp->dn_ident->di_id == idp->di_id);
427 }
428 
429 static void
430 dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
431 {
432 	dtrace_typeinfo_t dtt;
433 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
434 	char n[DT_TYPE_NAMELEN];
435 
436 	if (argc != 1) {
437 		xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
438 		    "passed, 1 expected\n", idp->di_name,
439 		    argc, argc == 1 ? " " : "s ");
440 	}
441 
442 	if (ap->dn_kind != DT_NODE_INT) {
443 		xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
444 		    "prototype:\n\tprototype: %s\n\t argument: %s\n",
445 		    idp->di_name, "integer constant",
446 		    dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));
447 	}
448 
449 	if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {
450 		xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",
451 		    (longlong_t)ap->dn_value, idp->di_name);
452 	}
453 
454 	if (dt_type_lookup("uint64_t", &dtt) == -1) {
455 		xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",
456 		    idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
457 	}
458 
459 	idp->di_ctfp = dtt.dtt_ctfp;
460 	idp->di_type = dtt.dtt_type;
461 
462 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
463 }
464 
465 /*ARGSUSED*/
466 static void
467 dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
468 {
469 	if (idp->di_type == CTF_ERR) {
470 		dtrace_hdl_t *dtp = yypcb->pcb_hdl;
471 		dtrace_typeinfo_t dtt;
472 
473 		if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {
474 			xyerror(D_UNKNOWN,
475 			    "failed to resolve type %s for identifier %s: %s\n",
476 			    (const char *)idp->di_iarg, idp->di_name,
477 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
478 		}
479 
480 		idp->di_ctfp = dtt.dtt_ctfp;
481 		idp->di_type = dtt.dtt_type;
482 	}
483 
484 	dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
485 }
486 
487 /*ARGSUSED*/
488 static void
489 dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
490 {
491 	if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)
492 		dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
493 }
494 
495 static void
496 dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
497 {
498 	if (idp->di_kind == DT_IDENT_ARRAY)
499 		dt_idcook_assc(dnp, idp, argc, args);
500 	else
501 		dt_idcook_thaw(dnp, idp, argc, args);
502 }
503 
504 static void
505 dt_iddtor_sign(dt_ident_t *idp)
506 {
507 	if (idp->di_data != NULL)
508 		free(((dt_idsig_t *)idp->di_data)->dis_args);
509 	free(idp->di_data);
510 }
511 
512 static void
513 dt_iddtor_free(dt_ident_t *idp)
514 {
515 	free(idp->di_data);
516 }
517 
518 static void
519 dt_iddtor_inline(dt_ident_t *idp)
520 {
521 	dt_idnode_t *inp = idp->di_iarg;
522 
523 	if (inp != NULL) {
524 		dt_node_link_free(&inp->din_list);
525 
526 		if (inp->din_hash != NULL)
527 			dt_idhash_destroy(inp->din_hash);
528 
529 		free(inp->din_argv);
530 		free(inp);
531 	}
532 
533 	if (idp->di_kind == DT_IDENT_ARRAY)
534 		dt_iddtor_sign(idp);
535 	else
536 		dt_iddtor_free(idp);
537 }
538 
539 /*ARGSUSED*/
540 static void
541 dt_iddtor_none(dt_ident_t *idp)
542 {
543 	/* do nothing */
544 }
545 
546 static void
547 dt_iddtor_probe(dt_ident_t *idp)
548 {
549 	if (idp->di_data != NULL)
550 		dt_probe_destroy(idp->di_data);
551 }
552 
553 static size_t
554 dt_idsize_type(dt_ident_t *idp)
555 {
556 	return (ctf_type_size(idp->di_ctfp, idp->di_type));
557 }
558 
559 /*ARGSUSED*/
560 static size_t
561 dt_idsize_none(dt_ident_t *idp)
562 {
563 	return (0);
564 }
565 
566 const dt_idops_t dt_idops_assc = {
567 	dt_idcook_assc,
568 	dt_iddtor_sign,
569 	dt_idsize_none,
570 };
571 
572 const dt_idops_t dt_idops_func = {
573 	dt_idcook_func,
574 	dt_iddtor_sign,
575 	dt_idsize_none,
576 };
577 
578 const dt_idops_t dt_idops_args = {
579 	dt_idcook_args,
580 	dt_iddtor_none,
581 	dt_idsize_none,
582 };
583 
584 const dt_idops_t dt_idops_regs = {
585 	dt_idcook_regs,
586 	dt_iddtor_free,
587 	dt_idsize_none,
588 };
589 
590 const dt_idops_t dt_idops_type = {
591 	dt_idcook_type,
592 	dt_iddtor_free,
593 	dt_idsize_type,
594 };
595 
596 const dt_idops_t dt_idops_thaw = {
597 	dt_idcook_thaw,
598 	dt_iddtor_free,
599 	dt_idsize_type,
600 };
601 
602 const dt_idops_t dt_idops_inline = {
603 	dt_idcook_inline,
604 	dt_iddtor_inline,
605 	dt_idsize_type,
606 };
607 
608 const dt_idops_t dt_idops_probe = {
609 	dt_idcook_thaw,
610 	dt_iddtor_probe,
611 	dt_idsize_none,
612 };
613 
614 static void
615 dt_idhash_populate(dt_idhash_t *dhp)
616 {
617 	const dt_ident_t *idp = dhp->dh_tmpl;
618 
619 	dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */
620 	dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);
621 
622 	for (; idp->di_name != NULL; idp++) {
623 		if (dt_idhash_insert(dhp, idp->di_name,
624 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
625 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
626 		    idp->di_iarg, 0) == NULL)
627 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
628 	}
629 }
630 
631 dt_idhash_t *
632 dt_idhash_create(const char *name, const dt_ident_t *tmpl,
633     uint_t min, uint_t max)
634 {
635 	dt_idhash_t *dhp;
636 	size_t size;
637 
638 	assert(min <= max);
639 
640 	size = sizeof (dt_idhash_t) +
641 	    sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);
642 
643 	if ((dhp = malloc(size)) == NULL)
644 		return (NULL);
645 
646 	bzero(dhp, size);
647 	dhp->dh_name = name;
648 	dhp->dh_tmpl = tmpl;
649 	dhp->dh_nextid = min;
650 	dhp->dh_minid = min;
651 	dhp->dh_maxid = max;
652 	dhp->dh_hashsz = _dtrace_strbuckets;
653 
654 	return (dhp);
655 }
656 
657 /*
658  * Destroy an entire identifier hash.  This must be done using two passes with
659  * an inlined version of dt_ident_destroy() to avoid referencing freed memory.
660  * In the first pass di_dtor() is called for all identifiers; then the second
661  * pass frees the actual dt_ident_t's.  These must be done separately because
662  * a di_dtor() may operate on data structures which contain references to other
663  * identifiers inside of this hash itself (e.g. a global inline definition
664  * which contains a parse tree that refers to another global variable).
665  */
666 void
667 dt_idhash_destroy(dt_idhash_t *dhp)
668 {
669 	dt_ident_t *idp, *next;
670 	ulong_t i;
671 
672 	for (i = 0; i < dhp->dh_hashsz; i++) {
673 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
674 			next = idp->di_next;
675 			idp->di_ops->di_dtor(idp);
676 		}
677 	}
678 
679 	for (i = 0; i < dhp->dh_hashsz; i++) {
680 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
681 			next = idp->di_next;
682 			free(idp->di_name);
683 			free(idp);
684 		}
685 	}
686 
687 	free(dhp);
688 }
689 
690 void
691 dt_idhash_update(dt_idhash_t *dhp)
692 {
693 	uint_t nextid = dhp->dh_minid;
694 	dt_ident_t *idp;
695 	ulong_t i;
696 
697 	for (i = 0; i < dhp->dh_hashsz; i++) {
698 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {
699 			if (idp->di_kind == DT_IDENT_ARRAY ||
700 			    idp->di_kind == DT_IDENT_SCALAR)
701 				nextid = MAX(nextid, idp->di_id + 1);
702 		}
703 	}
704 
705 	dhp->dh_nextid = nextid;
706 }
707 
708 dt_ident_t *
709 dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
710 {
711 	size_t len;
712 	ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
713 	dt_ident_t *idp;
714 
715 	if (dhp->dh_tmpl != NULL)
716 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
717 
718 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
719 		if (strcmp(idp->di_name, name) == 0)
720 			return (idp);
721 	}
722 
723 	return (NULL);
724 }
725 
726 int
727 dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
728 {
729 	if (dhp->dh_nextid >= dhp->dh_maxid)
730 		return (-1); /* no more id's are free to allocate */
731 
732 	*p = dhp->dh_nextid++;
733 	return (0);
734 }
735 
736 ulong_t
737 dt_idhash_size(const dt_idhash_t *dhp)
738 {
739 	return (dhp->dh_nelems);
740 }
741 
742 const char *
743 dt_idhash_name(const dt_idhash_t *dhp)
744 {
745 	return (dhp->dh_name);
746 }
747 
748 dt_ident_t *
749 dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
750     ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
751     const dt_idops_t *ops, void *iarg, ulong_t gen)
752 {
753 	dt_ident_t *idp;
754 	ulong_t h;
755 
756 	if (dhp->dh_tmpl != NULL)
757 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
758 
759 	idp = dt_ident_create(name, kind, flags, id,
760 	    attr, vers, ops, iarg, gen);
761 
762 	if (idp == NULL)
763 		return (NULL);
764 
765 	h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
766 	idp->di_next = dhp->dh_hash[h];
767 
768 	dhp->dh_hash[h] = idp;
769 	dhp->dh_nelems++;
770 
771 	if (dhp->dh_defer != NULL)
772 		dhp->dh_defer(dhp, idp);
773 
774 	return (idp);
775 }
776 
777 void
778 dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
779 {
780 	ulong_t h;
781 
782 	if (dhp->dh_tmpl != NULL)
783 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
784 
785 	h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
786 	idp->di_next = dhp->dh_hash[h];
787 	idp->di_flags &= ~DT_IDFLG_ORPHAN;
788 
789 	dhp->dh_hash[h] = idp;
790 	dhp->dh_nelems++;
791 
792 	if (dhp->dh_defer != NULL)
793 		dhp->dh_defer(dhp, idp);
794 }
795 
796 void
797 dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
798 {
799 	size_t len;
800 	ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
801 	dt_ident_t **pp = &dhp->dh_hash[h];
802 	dt_ident_t *idp;
803 
804 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
805 		if (idp == key)
806 			break;
807 		else
808 			pp = &idp->di_next;
809 	}
810 
811 	assert(idp == key);
812 	*pp = idp->di_next;
813 
814 	assert(dhp->dh_nelems != 0);
815 	dhp->dh_nelems--;
816 
817 	if (!(idp->di_flags & DT_IDFLG_ORPHAN))
818 		dt_ident_destroy(idp);
819 }
820 
821 static int
822 dt_idhash_comp(const void *lp, const void *rp)
823 {
824 	const dt_ident_t *lhs = *((const dt_ident_t **)lp);
825 	const dt_ident_t *rhs = *((const dt_ident_t **)rp);
826 
827 	if (lhs->di_id != rhs->di_id)
828 		return ((int)(lhs->di_id - rhs->di_id));
829 	else
830 		return (strcmp(lhs->di_name, rhs->di_name));
831 }
832 
833 int
834 dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
835 {
836 	dt_ident_t **ids;
837 	dt_ident_t *idp;
838 	ulong_t i, j;
839 	int rv;
840 
841 	if (dhp->dh_tmpl != NULL)
842 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
843 
844 	ids = alloca(sizeof (dt_ident_t *) * dhp->dh_nelems);
845 
846 	for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
847 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
848 			ids[j++] = idp;
849 	}
850 
851 	qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
852 
853 	for (i = 0; i < dhp->dh_nelems; i++) {
854 		if ((rv = func(dhp, ids[i], data)) != 0)
855 			return (rv);
856 	}
857 
858 	return (0);
859 }
860 
861 dt_ident_t *
862 dt_idstack_lookup(dt_idstack_t *sp, const char *name)
863 {
864 	dt_idhash_t *dhp;
865 	dt_ident_t *idp;
866 
867 	for (dhp = dt_list_prev(&sp->dids_list);
868 	    dhp != NULL; dhp = dt_list_prev(dhp)) {
869 		if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
870 			return (idp);
871 	}
872 
873 	return (NULL);
874 }
875 
876 void
877 dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
878 {
879 	dt_list_append(&sp->dids_list, dhp);
880 }
881 
882 void
883 dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
884 {
885 	assert(dt_list_prev(&sp->dids_list) == dhp);
886 	dt_list_delete(&sp->dids_list, dhp);
887 }
888 
889 dt_ident_t *
890 dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
891     dtrace_attribute_t attr, uint_t vers,
892     const dt_idops_t *ops, void *iarg, ulong_t gen)
893 {
894 	dt_ident_t *idp;
895 	char *s = NULL;
896 
897 	if ((name != NULL && (s = strdup(name)) == NULL) ||
898 	    (idp = malloc(sizeof (dt_ident_t))) == NULL) {
899 		free(s);
900 		return (NULL);
901 	}
902 
903 	idp->di_name = s;
904 	idp->di_kind = kind;
905 	idp->di_flags = flags;
906 	idp->di_id = id;
907 	idp->di_attr = attr;
908 	idp->di_vers = vers;
909 	idp->di_ops = ops;
910 	idp->di_iarg = iarg;
911 	idp->di_data = NULL;
912 	idp->di_ctfp = NULL;
913 	idp->di_type = CTF_ERR;
914 	idp->di_next = NULL;
915 	idp->di_gen = gen;
916 	idp->di_lineno = yylineno;
917 
918 	return (idp);
919 }
920 
921 /*
922  * Destroy an individual identifier.  This code must be kept in sync with the
923  * dt_idhash_destroy() function below, which separates out the call to di_dtor.
924  */
925 void
926 dt_ident_destroy(dt_ident_t *idp)
927 {
928 	idp->di_ops->di_dtor(idp);
929 	free(idp->di_name);
930 	free(idp);
931 }
932 
933 void
934 dt_ident_morph(dt_ident_t *idp, ushort_t kind,
935     const dt_idops_t *ops, void *iarg)
936 {
937 	idp->di_ops->di_dtor(idp);
938 	idp->di_kind = kind;
939 	idp->di_ops = ops;
940 	idp->di_iarg = iarg;
941 	idp->di_data = NULL;
942 }
943 
944 dtrace_attribute_t
945 dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
946 {
947 	dtrace_attribute_t attr;
948 	dt_node_t *args, *argp;
949 	int argc = 0;
950 
951 	attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
952 	args = pargp ? *pargp : NULL;
953 
954 	for (argp = args; argp != NULL; argp = argp->dn_list)
955 		argc++;
956 
957 	idp->di_ops->di_cook(dnp, idp, argc, args);
958 
959 	if (idp->di_flags & DT_IDFLG_USER)
960 		dnp->dn_flags |= DT_NF_USERLAND;
961 
962 	return (dt_attr_min(attr, idp->di_attr));
963 }
964 
965 void
966 dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
967 {
968 	idp->di_ctfp = fp;
969 	idp->di_type = type;
970 }
971 
972 dt_ident_t *
973 dt_ident_resolve(dt_ident_t *idp)
974 {
975 	while (idp->di_flags & DT_IDFLG_INLINE) {
976 		const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
977 
978 		if (dnp == NULL)
979 			break; /* can't resolve any further yet */
980 
981 		switch (dnp->dn_kind) {
982 		case DT_NODE_VAR:
983 		case DT_NODE_SYM:
984 		case DT_NODE_FUNC:
985 		case DT_NODE_AGG:
986 		case DT_NODE_INLINE:
987 		case DT_NODE_PROBE:
988 			idp = dnp->dn_ident;
989 			continue;
990 		}
991 
992 		if (dt_node_is_dynamic(dnp))
993 			idp = dnp->dn_ident;
994 		else
995 			break;
996 	}
997 
998 	return (idp);
999 }
1000 
1001 size_t
1002 dt_ident_size(dt_ident_t *idp)
1003 {
1004 	idp = dt_ident_resolve(idp);
1005 	return (idp->di_ops->di_size(idp));
1006 }
1007 
1008 int
1009 dt_ident_unref(const dt_ident_t *idp)
1010 {
1011 	return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
1012 	    (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
1013 }
1014 
1015 const char *
1016 dt_idkind_name(uint_t kind)
1017 {
1018 	switch (kind) {
1019 	case DT_IDENT_ARRAY:	return ("associative array");
1020 	case DT_IDENT_SCALAR:	return ("scalar");
1021 	case DT_IDENT_PTR:	return ("pointer");
1022 	case DT_IDENT_FUNC:	return ("function");
1023 	case DT_IDENT_AGG:	return ("aggregation");
1024 	case DT_IDENT_AGGFUNC:	return ("aggregating function");
1025 	case DT_IDENT_ACTFUNC:	return ("tracing function");
1026 	case DT_IDENT_XLSOU:	return ("translated data");
1027 	case DT_IDENT_XLPTR:	return ("pointer to translated data");
1028 	case DT_IDENT_SYMBOL:	return ("external symbol reference");
1029 	case DT_IDENT_ENUM:	return ("enumerator");
1030 	case DT_IDENT_PRAGAT:	return ("#pragma attributes");
1031 	case DT_IDENT_PRAGBN:	return ("#pragma binding");
1032 	case DT_IDENT_PROBE:	return ("probe definition");
1033 	default:		return ("<?>");
1034 	}
1035 }
1036