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