xref: /titanic_50/usr/src/lib/libdtrace/common/dt_ident.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 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 			/*
700 			 * Right now we're hard coding which types need to be
701 			 * reset, but ideally this would be done dynamically.
702 			 */
703 			if (idp->di_kind == DT_IDENT_ARRAY ||
704 			    idp->di_kind == DT_IDENT_SCALAR ||
705 			    idp->di_kind == DT_IDENT_AGG)
706 				nextid = MAX(nextid, idp->di_id + 1);
707 		}
708 	}
709 
710 	dhp->dh_nextid = nextid;
711 }
712 
713 dt_ident_t *
714 dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
715 {
716 	size_t len;
717 	ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
718 	dt_ident_t *idp;
719 
720 	if (dhp->dh_tmpl != NULL)
721 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
722 
723 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
724 		if (strcmp(idp->di_name, name) == 0)
725 			return (idp);
726 	}
727 
728 	return (NULL);
729 }
730 
731 int
732 dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
733 {
734 	if (dhp->dh_nextid >= dhp->dh_maxid)
735 		return (-1); /* no more id's are free to allocate */
736 
737 	*p = dhp->dh_nextid++;
738 	return (0);
739 }
740 
741 ulong_t
742 dt_idhash_size(const dt_idhash_t *dhp)
743 {
744 	return (dhp->dh_nelems);
745 }
746 
747 const char *
748 dt_idhash_name(const dt_idhash_t *dhp)
749 {
750 	return (dhp->dh_name);
751 }
752 
753 dt_ident_t *
754 dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
755     ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
756     const dt_idops_t *ops, void *iarg, ulong_t gen)
757 {
758 	dt_ident_t *idp;
759 	ulong_t h;
760 
761 	if (dhp->dh_tmpl != NULL)
762 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
763 
764 	idp = dt_ident_create(name, kind, flags, id,
765 	    attr, vers, ops, iarg, gen);
766 
767 	if (idp == NULL)
768 		return (NULL);
769 
770 	h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
771 	idp->di_next = dhp->dh_hash[h];
772 
773 	dhp->dh_hash[h] = idp;
774 	dhp->dh_nelems++;
775 
776 	if (dhp->dh_defer != NULL)
777 		dhp->dh_defer(dhp, idp);
778 
779 	return (idp);
780 }
781 
782 void
783 dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
784 {
785 	ulong_t h;
786 
787 	if (dhp->dh_tmpl != NULL)
788 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
789 
790 	h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
791 	idp->di_next = dhp->dh_hash[h];
792 	idp->di_flags &= ~DT_IDFLG_ORPHAN;
793 
794 	dhp->dh_hash[h] = idp;
795 	dhp->dh_nelems++;
796 
797 	if (dhp->dh_defer != NULL)
798 		dhp->dh_defer(dhp, idp);
799 }
800 
801 void
802 dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
803 {
804 	size_t len;
805 	ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
806 	dt_ident_t **pp = &dhp->dh_hash[h];
807 	dt_ident_t *idp;
808 
809 	for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
810 		if (idp == key)
811 			break;
812 		else
813 			pp = &idp->di_next;
814 	}
815 
816 	assert(idp == key);
817 	*pp = idp->di_next;
818 
819 	assert(dhp->dh_nelems != 0);
820 	dhp->dh_nelems--;
821 
822 	if (!(idp->di_flags & DT_IDFLG_ORPHAN))
823 		dt_ident_destroy(idp);
824 }
825 
826 static int
827 dt_idhash_comp(const void *lp, const void *rp)
828 {
829 	const dt_ident_t *lhs = *((const dt_ident_t **)lp);
830 	const dt_ident_t *rhs = *((const dt_ident_t **)rp);
831 
832 	if (lhs->di_id != rhs->di_id)
833 		return ((int)(lhs->di_id - rhs->di_id));
834 	else
835 		return (strcmp(lhs->di_name, rhs->di_name));
836 }
837 
838 int
839 dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
840 {
841 	dt_ident_t **ids;
842 	dt_ident_t *idp;
843 	ulong_t i, j;
844 	int rv;
845 
846 	if (dhp->dh_tmpl != NULL)
847 		dt_idhash_populate(dhp); /* fill hash w/ initial population */
848 
849 	ids = alloca(sizeof (dt_ident_t *) * dhp->dh_nelems);
850 
851 	for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
852 		for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
853 			ids[j++] = idp;
854 	}
855 
856 	qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
857 
858 	for (i = 0; i < dhp->dh_nelems; i++) {
859 		if ((rv = func(dhp, ids[i], data)) != 0)
860 			return (rv);
861 	}
862 
863 	return (0);
864 }
865 
866 dt_ident_t *
867 dt_idstack_lookup(dt_idstack_t *sp, const char *name)
868 {
869 	dt_idhash_t *dhp;
870 	dt_ident_t *idp;
871 
872 	for (dhp = dt_list_prev(&sp->dids_list);
873 	    dhp != NULL; dhp = dt_list_prev(dhp)) {
874 		if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
875 			return (idp);
876 	}
877 
878 	return (NULL);
879 }
880 
881 void
882 dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
883 {
884 	dt_list_append(&sp->dids_list, dhp);
885 }
886 
887 void
888 dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
889 {
890 	assert(dt_list_prev(&sp->dids_list) == dhp);
891 	dt_list_delete(&sp->dids_list, dhp);
892 }
893 
894 dt_ident_t *
895 dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
896     dtrace_attribute_t attr, uint_t vers,
897     const dt_idops_t *ops, void *iarg, ulong_t gen)
898 {
899 	dt_ident_t *idp;
900 	char *s = NULL;
901 
902 	if ((name != NULL && (s = strdup(name)) == NULL) ||
903 	    (idp = malloc(sizeof (dt_ident_t))) == NULL) {
904 		free(s);
905 		return (NULL);
906 	}
907 
908 	idp->di_name = s;
909 	idp->di_kind = kind;
910 	idp->di_flags = flags;
911 	idp->di_id = id;
912 	idp->di_attr = attr;
913 	idp->di_vers = vers;
914 	idp->di_ops = ops;
915 	idp->di_iarg = iarg;
916 	idp->di_data = NULL;
917 	idp->di_ctfp = NULL;
918 	idp->di_type = CTF_ERR;
919 	idp->di_next = NULL;
920 	idp->di_gen = gen;
921 	idp->di_lineno = yylineno;
922 
923 	return (idp);
924 }
925 
926 /*
927  * Destroy an individual identifier.  This code must be kept in sync with the
928  * dt_idhash_destroy() function below, which separates out the call to di_dtor.
929  */
930 void
931 dt_ident_destroy(dt_ident_t *idp)
932 {
933 	idp->di_ops->di_dtor(idp);
934 	free(idp->di_name);
935 	free(idp);
936 }
937 
938 void
939 dt_ident_morph(dt_ident_t *idp, ushort_t kind,
940     const dt_idops_t *ops, void *iarg)
941 {
942 	idp->di_ops->di_dtor(idp);
943 	idp->di_kind = kind;
944 	idp->di_ops = ops;
945 	idp->di_iarg = iarg;
946 	idp->di_data = NULL;
947 }
948 
949 dtrace_attribute_t
950 dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
951 {
952 	dtrace_attribute_t attr;
953 	dt_node_t *args, *argp;
954 	int argc = 0;
955 
956 	attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
957 	args = pargp ? *pargp : NULL;
958 
959 	for (argp = args; argp != NULL; argp = argp->dn_list)
960 		argc++;
961 
962 	idp->di_ops->di_cook(dnp, idp, argc, args);
963 
964 	if (idp->di_flags & DT_IDFLG_USER)
965 		dnp->dn_flags |= DT_NF_USERLAND;
966 
967 	return (dt_attr_min(attr, idp->di_attr));
968 }
969 
970 void
971 dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
972 {
973 	idp->di_ctfp = fp;
974 	idp->di_type = type;
975 }
976 
977 dt_ident_t *
978 dt_ident_resolve(dt_ident_t *idp)
979 {
980 	while (idp->di_flags & DT_IDFLG_INLINE) {
981 		const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
982 
983 		if (dnp == NULL)
984 			break; /* can't resolve any further yet */
985 
986 		switch (dnp->dn_kind) {
987 		case DT_NODE_VAR:
988 		case DT_NODE_SYM:
989 		case DT_NODE_FUNC:
990 		case DT_NODE_AGG:
991 		case DT_NODE_INLINE:
992 		case DT_NODE_PROBE:
993 			idp = dnp->dn_ident;
994 			continue;
995 		}
996 
997 		if (dt_node_is_dynamic(dnp))
998 			idp = dnp->dn_ident;
999 		else
1000 			break;
1001 	}
1002 
1003 	return (idp);
1004 }
1005 
1006 size_t
1007 dt_ident_size(dt_ident_t *idp)
1008 {
1009 	idp = dt_ident_resolve(idp);
1010 	return (idp->di_ops->di_size(idp));
1011 }
1012 
1013 int
1014 dt_ident_unref(const dt_ident_t *idp)
1015 {
1016 	return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
1017 	    (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
1018 }
1019 
1020 const char *
1021 dt_idkind_name(uint_t kind)
1022 {
1023 	switch (kind) {
1024 	case DT_IDENT_ARRAY:	return ("associative array");
1025 	case DT_IDENT_SCALAR:	return ("scalar");
1026 	case DT_IDENT_PTR:	return ("pointer");
1027 	case DT_IDENT_FUNC:	return ("function");
1028 	case DT_IDENT_AGG:	return ("aggregation");
1029 	case DT_IDENT_AGGFUNC:	return ("aggregating function");
1030 	case DT_IDENT_ACTFUNC:	return ("tracing function");
1031 	case DT_IDENT_XLSOU:	return ("translated data");
1032 	case DT_IDENT_XLPTR:	return ("pointer to translated data");
1033 	case DT_IDENT_SYMBOL:	return ("external symbol reference");
1034 	case DT_IDENT_ENUM:	return ("enumerator");
1035 	case DT_IDENT_PRAGAT:	return ("#pragma attributes");
1036 	case DT_IDENT_PRAGBN:	return ("#pragma binding");
1037 	case DT_IDENT_PROBE:	return ("probe definition");
1038 	default:		return ("<?>");
1039 	}
1040 }
1041