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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright (c) 2013 Joyent, Inc. All rights reserved.
29 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/sysmacros.h>
43 #include <sys/procfs_isa.h>
48 #include <dt_parser.h>
49 #include <dt_provider.h>
50 #include <dt_strtab.h>
54 * Common code for cooking an identifier that uses a typed signature list (we
55 * use this for associative arrays and functions). If the argument list is
56 * of the same length and types, then return the return type. Otherwise
57 * print an appropriate compiler error message and abort the compile.
60 dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,
61 int argc, dt_node_t *args, const char *prefix, const char *suffix)
63 dt_idsig_t *isp = idp->di_data;
64 int i, compat, mismatch, arglimit, iskey;
66 char n1[DT_TYPE_NAMELEN];
67 char n2[DT_TYPE_NAMELEN];
69 iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG;
71 if (isp->dis_varargs >= 0) {
72 mismatch = argc < isp->dis_varargs;
73 arglimit = isp->dis_varargs;
74 } else if (isp->dis_optargs >= 0) {
75 mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);
78 mismatch = argc != isp->dis_argc;
79 arglimit = isp->dis_argc;
83 xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s"
84 "passed, %s%d expected\n", prefix, idp->di_name, suffix,
85 argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ",
86 isp->dis_optargs >= 0 ? "at least " : "",
87 isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);
90 for (i = 0; i < arglimit; i++, args = args->dn_list) {
91 if (isp->dis_args[i].dn_ctfp != NULL)
92 compat = dt_node_is_argcompat(&isp->dis_args[i], args);
94 compat = 1; /* "@" matches any type */
98 "%s%s%s %s #%d is incompatible with "
99 "prototype:\n\tprototype: %s\n\t%9s: %s\n",
100 prefix, idp->di_name, suffix,
101 iskey ? "key" : "argument", i + 1,
102 dt_node_type_name(&isp->dis_args[i], n1,
104 iskey ? "key" : "argument",
105 dt_node_type_name(args, n2, sizeof (n2)));
109 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
113 * Cook an associative array identifier. If this is the first time we are
114 * cooking this array, create its signature based on the argument list.
115 * Otherwise validate the argument list against the existing signature.
118 dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
120 if (idp->di_data == NULL) {
121 dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));
122 char n[DT_TYPE_NAMELEN];
126 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
128 isp->dis_varargs = -1;
129 isp->dis_optargs = -1;
130 isp->dis_argc = argc;
131 isp->dis_args = NULL;
132 isp->dis_auxinfo = 0;
134 if (argc != 0 && (isp->dis_args = calloc(argc,
135 sizeof (dt_node_t))) == NULL) {
138 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
142 * If this identifier has not been explicitly declared earlier,
143 * set the identifier's base type to be our special type <DYN>.
144 * If this ident is an aggregation, it will remain as is. If
145 * this ident is an associative array, it will be reassigned
146 * based on the result type of the first assignment statement.
148 if (!(idp->di_flags & DT_IDFLG_DECL)) {
149 idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);
150 idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);
153 for (i = 0; i < argc; i++, args = args->dn_list) {
154 if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {
155 xyerror(D_KEY_TYPE, "%s expression may not be "
156 "used as %s index: key #%d\n",
157 dt_node_type_name(args, n, sizeof (n)),
158 dt_idkind_name(idp->di_kind), i + 1);
161 dt_node_type_propagate(args, &isp->dis_args[i]);
162 isp->dis_args[i].dn_list = &isp->dis_args[i + 1];
166 isp->dis_args[argc - 1].dn_list = NULL;
168 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
171 dt_idcook_sign(dnp, idp, argc, args,
172 idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");
177 * Cook a function call. If this is the first time we are cooking this
178 * identifier, create its type signature based on predefined prototype stored
179 * in di_iarg. We then validate the argument list against this signature.
182 dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
184 if (idp->di_data == NULL) {
185 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
186 dtrace_typeinfo_t dtt;
191 assert(idp->di_iarg != NULL);
192 s = alloca(strlen(idp->di_iarg) + 1);
193 (void) strcpy(s, idp->di_iarg);
195 if ((p2 = strrchr(s, ')')) != NULL)
196 *p2 = '\0'; /* mark end of parameter list string */
198 if ((p1 = strchr(s, '(')) != NULL)
199 *p1++ = '\0'; /* mark end of return type string */
201 if (p1 == NULL || p2 == NULL) {
202 xyerror(D_UNKNOWN, "internal error: malformed entry "
203 "for built-in function %s\n", idp->di_name);
206 for (p2 = p1; *p2 != '\0'; p2++) {
213 for (p2 = strchr(p2, ','); p2++ != NULL; i++)
214 p2 = strchr(p2, ',');
217 * We first allocate a new ident signature structure with the
218 * appropriate number of argument entries, and then look up
219 * the return type and store its CTF data in di_ctfp/type.
221 if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)
222 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
224 isp->dis_varargs = -1;
225 isp->dis_optargs = -1;
227 isp->dis_args = NULL;
228 isp->dis_auxinfo = 0;
230 if (i != 0 && (isp->dis_args = calloc(i,
231 sizeof (dt_node_t))) == NULL) {
234 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
237 if (dt_type_lookup(s, &dtt) == -1) {
238 xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"
239 " %s\n", idp->di_name, s,
240 dtrace_errmsg(dtp, dtrace_errno(dtp)));
243 if (idp->di_kind == DT_IDENT_AGGFUNC) {
244 idp->di_ctfp = DT_DYN_CTFP(dtp);
245 idp->di_type = DT_DYN_TYPE(dtp);
247 idp->di_ctfp = dtt.dtt_ctfp;
248 idp->di_type = dtt.dtt_type;
252 * For each comma-delimited parameter in the prototype string,
253 * we look up the corresponding type and store its CTF data in
254 * the corresponding location in dis_args[]. We also recognize
255 * the special type string "@" to indicate that the specified
256 * parameter may be a D expression of *any* type (represented
257 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR).
258 * If a varargs "..." is present, we record the argument index
259 * in dis_varargs for the benefit of dt_idcook_sign(), above.
260 * If the type of an argument is enclosed in square brackets
261 * (e.g. "[int]"), the argument is considered optional: the
262 * argument may be absent, but if it is present, it must be of
263 * the specified type. Note that varargs may not optional,
264 * optional arguments may not follow varargs, and non-optional
265 * arguments may not follow optional arguments.
267 for (i = 0; i < isp->dis_argc; i++, p1 = p2) {
269 p1++; /* skip leading whitespace */
271 if ((p2 = strchr(p1, ',')) == NULL)
272 p2 = p1 + strlen(p1);
276 if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {
277 isp->dis_args[i].dn_ctfp = NULL;
278 isp->dis_args[i].dn_type = CTF_ERR;
280 isp->dis_varargs = i;
284 if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {
285 if (isp->dis_varargs != -1) {
286 xyerror(D_UNKNOWN, "optional arg#%d "
287 "may not follow variable arg#%d\n",
288 i + 1, isp->dis_varargs + 1);
291 if (isp->dis_optargs == -1)
292 isp->dis_optargs = i;
294 p1[strlen(p1) - 1] = '\0';
296 } else if (isp->dis_optargs != -1) {
297 xyerror(D_UNKNOWN, "required arg#%d may not "
298 "follow optional arg#%d\n", i + 1,
299 isp->dis_optargs + 1);
302 if (dt_type_lookup(p1, &dtt) == -1) {
303 xyerror(D_UNKNOWN, "failed to resolve type of "
304 "%s arg#%d (%s): %s\n", idp->di_name, i + 1,
305 p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));
308 dt_node_type_assign(&isp->dis_args[i],
309 dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
313 dt_idcook_sign(dnp, idp, argc, args, "", "( )");
317 * Cook a reference to the dynamically typed args[] array. We verify that the
318 * reference is using a single integer constant, and then construct a new ident
319 * representing the appropriate type or translation specifically for this node.
322 dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
324 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
325 dt_probe_t *prp = yypcb->pcb_probe;
327 dt_node_t tag, *nnp, *xnp;
331 char n1[DT_TYPE_NAMELEN];
332 char n2[DT_TYPE_NAMELEN];
335 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
336 "passed, 1 expected\n", idp->di_name, argc,
337 argc == 1 ? " " : "s ");
340 if (ap->dn_kind != DT_NODE_INT) {
341 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
342 "prototype:\n\tprototype: %s\n\t argument: %s\n",
343 idp->di_name, "integer constant",
344 dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));
347 if (yypcb->pcb_pdesc == NULL) {
348 xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "
349 "of a probe clause\n", idp->di_name);
353 xyerror(D_ARGS_MULTI,
354 "%s[ ] may not be referenced because probe description %s "
355 "matches an unstable set of probes\n", idp->di_name,
356 dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));
359 if (ap->dn_value >= prp->pr_argc) {
360 xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",
361 (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,
362 n1, sizeof (n1)), idp->di_name);
366 * Look up the native and translated argument types for the probe.
367 * If no translation is needed, these will be the same underlying node.
368 * If translation is needed, look up the appropriate translator. Once
369 * we have the appropriate node, create a new dt_ident_t for this node,
370 * assign it the appropriate attributes, and set the type of 'dnp'.
372 xnp = prp->pr_xargv[ap->dn_value];
373 nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];
375 if (xnp->dn_type == CTF_ERR) {
376 xyerror(D_ARGS_TYPE, "failed to resolve translated type for "
377 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
380 if (nnp->dn_type == CTF_ERR) {
381 xyerror(D_ARGS_TYPE, "failed to resolve native type for "
382 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
385 if (dtp->dt_xlatemode == DT_XL_STATIC && (
386 nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {
387 dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,
388 idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
389 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
391 if (dnp->dn_ident == NULL)
392 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
394 dt_node_type_assign(dnp,
395 prp->pr_argv[ap->dn_value].dtt_ctfp,
396 prp->pr_argv[ap->dn_value].dtt_type,
397 prp->pr_argv[ap->dn_value].dtt_flags & DTT_FL_USER ?
400 } else if ((dxp = dt_xlator_lookup(dtp,
401 nnp, xnp, DT_XLATE_FUZZY)) != NULL || (
402 dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),
403 xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {
405 xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);
407 dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,
408 xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
409 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
411 if (dnp->dn_ident == NULL)
412 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
414 if (dt_xlator_dynamic(dxp))
415 dxp->dx_arg = (int)ap->dn_value;
418 * Propagate relevant members from the translator's internal
419 * dt_ident_t. This code must be kept in sync with the state
420 * that is initialized for idents in dt_xlator_create().
422 dnp->dn_ident->di_data = xidp->di_data;
423 dnp->dn_ident->di_ctfp = xidp->di_ctfp;
424 dnp->dn_ident->di_type = xidp->di_type;
426 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
430 xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "
431 "is not defined\n", idp->di_name, (longlong_t)ap->dn_value,
432 dt_node_type_name(nnp, n1, sizeof (n1)),
433 dt_node_type_name(xnp, n2, sizeof (n2)));
436 assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);
437 assert(dnp->dn_ident->di_id == idp->di_id);
441 dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
443 dtrace_typeinfo_t dtt;
444 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
445 char n[DT_TYPE_NAMELEN];
448 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
449 "passed, 1 expected\n", idp->di_name,
450 argc, argc == 1 ? " " : "s ");
453 if (ap->dn_kind != DT_NODE_INT) {
454 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
455 "prototype:\n\tprototype: %s\n\t argument: %s\n",
456 idp->di_name, "integer constant",
457 dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));
460 if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {
461 xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",
462 (longlong_t)ap->dn_value, idp->di_name);
465 if (dt_type_lookup("uint64_t", &dtt) == -1) {
466 xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",
467 idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
470 idp->di_ctfp = dtt.dtt_ctfp;
471 idp->di_type = dtt.dtt_type;
473 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
478 dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
480 if (idp->di_type == CTF_ERR) {
481 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
482 dtrace_typeinfo_t dtt;
484 if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {
486 "failed to resolve type %s for identifier %s: %s\n",
487 (const char *)idp->di_iarg, idp->di_name,
488 dtrace_errmsg(dtp, dtrace_errno(dtp)));
491 idp->di_ctfp = dtt.dtt_ctfp;
492 idp->di_type = dtt.dtt_type;
495 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
500 dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
502 if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)
503 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
507 dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
509 if (idp->di_kind == DT_IDENT_ARRAY)
510 dt_idcook_assc(dnp, idp, argc, args);
512 dt_idcook_thaw(dnp, idp, argc, args);
516 dt_iddtor_sign(dt_ident_t *idp)
518 if (idp->di_data != NULL)
519 free(((dt_idsig_t *)idp->di_data)->dis_args);
524 dt_iddtor_free(dt_ident_t *idp)
530 dt_iddtor_inline(dt_ident_t *idp)
532 dt_idnode_t *inp = idp->di_iarg;
535 dt_node_link_free(&inp->din_list);
537 if (inp->din_hash != NULL)
538 dt_idhash_destroy(inp->din_hash);
544 if (idp->di_kind == DT_IDENT_ARRAY)
552 dt_iddtor_none(dt_ident_t *idp)
558 dt_iddtor_probe(dt_ident_t *idp)
560 if (idp->di_data != NULL)
561 dt_probe_destroy(idp->di_data);
565 dt_idsize_type(dt_ident_t *idp)
567 return (ctf_type_size(idp->di_ctfp, idp->di_type));
572 dt_idsize_none(dt_ident_t *idp)
577 const dt_idops_t dt_idops_assc = {
583 const dt_idops_t dt_idops_func = {
589 const dt_idops_t dt_idops_args = {
595 const dt_idops_t dt_idops_regs = {
601 const dt_idops_t dt_idops_type = {
607 const dt_idops_t dt_idops_thaw = {
613 const dt_idops_t dt_idops_inline = {
619 const dt_idops_t dt_idops_probe = {
626 dt_idhash_populate(dt_idhash_t *dhp)
628 const dt_ident_t *idp = dhp->dh_tmpl;
630 dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */
631 dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);
633 for (; idp->di_name != NULL; idp++) {
634 if (dt_idhash_insert(dhp, idp->di_name,
635 idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
636 idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
637 idp->di_iarg, 0) == NULL)
638 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
643 dt_idhash_create(const char *name, const dt_ident_t *tmpl,
644 uint_t min, uint_t max)
651 size = sizeof (dt_idhash_t) +
652 sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);
654 if ((dhp = malloc(size)) == NULL)
660 dhp->dh_nextid = min;
663 dhp->dh_hashsz = _dtrace_strbuckets;
669 * Destroy an entire identifier hash. This must be done using two passes with
670 * an inlined version of dt_ident_destroy() to avoid referencing freed memory.
671 * In the first pass di_dtor() is called for all identifiers; then the second
672 * pass frees the actual dt_ident_t's. These must be done separately because
673 * a di_dtor() may operate on data structures which contain references to other
674 * identifiers inside of this hash itself (e.g. a global inline definition
675 * which contains a parse tree that refers to another global variable).
678 dt_idhash_destroy(dt_idhash_t *dhp)
680 dt_ident_t *idp, *next;
683 for (i = 0; i < dhp->dh_hashsz; i++) {
684 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
686 idp->di_ops->di_dtor(idp);
690 for (i = 0; i < dhp->dh_hashsz; i++) {
691 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
702 dt_idhash_update(dt_idhash_t *dhp)
704 uint_t nextid = dhp->dh_minid;
708 for (i = 0; i < dhp->dh_hashsz; i++) {
709 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {
711 * Right now we're hard coding which types need to be
712 * reset, but ideally this would be done dynamically.
714 if (idp->di_kind == DT_IDENT_ARRAY ||
715 idp->di_kind == DT_IDENT_SCALAR ||
716 idp->di_kind == DT_IDENT_AGG)
717 nextid = MAX(nextid, idp->di_id + 1);
721 dhp->dh_nextid = nextid;
725 dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
728 ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
731 if (dhp->dh_tmpl != NULL)
732 dt_idhash_populate(dhp); /* fill hash w/ initial population */
734 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
735 if (strcmp(idp->di_name, name) == 0)
743 dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
745 if (dhp->dh_nextid >= dhp->dh_maxid)
746 return (-1); /* no more id's are free to allocate */
748 *p = dhp->dh_nextid++;
753 dt_idhash_size(const dt_idhash_t *dhp)
755 return (dhp->dh_nelems);
759 dt_idhash_name(const dt_idhash_t *dhp)
761 return (dhp->dh_name);
765 dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
766 ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
767 const dt_idops_t *ops, void *iarg, ulong_t gen)
772 if (dhp->dh_tmpl != NULL)
773 dt_idhash_populate(dhp); /* fill hash w/ initial population */
775 idp = dt_ident_create(name, kind, flags, id,
776 attr, vers, ops, iarg, gen);
781 h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
782 idp->di_next = dhp->dh_hash[h];
784 dhp->dh_hash[h] = idp;
787 if (dhp->dh_defer != NULL)
788 dhp->dh_defer(dhp, idp);
794 dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
798 if (dhp->dh_tmpl != NULL)
799 dt_idhash_populate(dhp); /* fill hash w/ initial population */
801 h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
802 idp->di_next = dhp->dh_hash[h];
803 idp->di_flags &= ~DT_IDFLG_ORPHAN;
805 dhp->dh_hash[h] = idp;
808 if (dhp->dh_defer != NULL)
809 dhp->dh_defer(dhp, idp);
813 dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
816 ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
817 dt_ident_t **pp = &dhp->dh_hash[h];
820 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
830 assert(dhp->dh_nelems != 0);
833 if (!(idp->di_flags & DT_IDFLG_ORPHAN))
834 dt_ident_destroy(idp);
838 dt_idhash_comp(const void *lp, const void *rp)
840 const dt_ident_t *lhs = *((const dt_ident_t **)lp);
841 const dt_ident_t *rhs = *((const dt_ident_t **)rp);
843 if (lhs->di_id != rhs->di_id)
844 return ((int)(lhs->di_id - rhs->di_id));
846 return (strcmp(lhs->di_name, rhs->di_name));
850 dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
857 if (dhp->dh_tmpl != NULL)
858 dt_idhash_populate(dhp); /* fill hash w/ initial population */
861 ids = alloca(sizeof (dt_ident_t *) * n);
863 for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
864 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
868 qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
870 for (i = 0; i < n; i++) {
871 if ((rv = func(dhp, ids[i], data)) != 0)
879 dt_idstack_lookup(dt_idstack_t *sp, const char *name)
884 for (dhp = dt_list_prev(&sp->dids_list);
885 dhp != NULL; dhp = dt_list_prev(dhp)) {
886 if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
894 dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
896 dt_list_append(&sp->dids_list, dhp);
900 dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
902 assert(dt_list_prev(&sp->dids_list) == dhp);
903 dt_list_delete(&sp->dids_list, dhp);
907 dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
908 dtrace_attribute_t attr, uint_t vers,
909 const dt_idops_t *ops, void *iarg, ulong_t gen)
914 if ((name != NULL && (s = strdup(name)) == NULL) ||
915 (idp = malloc(sizeof (dt_ident_t))) == NULL) {
922 idp->di_flags = flags;
930 idp->di_type = CTF_ERR;
933 idp->di_lineno = yylineno;
939 * Destroy an individual identifier. This code must be kept in sync with the
940 * dt_idhash_destroy() function below, which separates out the call to di_dtor.
943 dt_ident_destroy(dt_ident_t *idp)
945 idp->di_ops->di_dtor(idp);
951 dt_ident_morph(dt_ident_t *idp, ushort_t kind,
952 const dt_idops_t *ops, void *iarg)
954 idp->di_ops->di_dtor(idp);
962 dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
964 dtrace_attribute_t attr;
965 dt_node_t *args, *argp;
968 attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
969 args = pargp ? *pargp : NULL;
971 for (argp = args; argp != NULL; argp = argp->dn_list)
974 idp->di_ops->di_cook(dnp, idp, argc, args);
976 if (idp->di_flags & DT_IDFLG_USER)
977 dnp->dn_flags |= DT_NF_USERLAND;
979 return (dt_attr_min(attr, idp->di_attr));
983 dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
990 dt_ident_resolve(dt_ident_t *idp)
992 while (idp->di_flags & DT_IDFLG_INLINE) {
993 const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
996 break; /* can't resolve any further yet */
998 switch (dnp->dn_kind) {
1003 case DT_NODE_INLINE:
1005 idp = dnp->dn_ident;
1009 if (dt_node_is_dynamic(dnp))
1010 idp = dnp->dn_ident;
1019 dt_ident_size(dt_ident_t *idp)
1021 idp = dt_ident_resolve(idp);
1022 return (idp->di_ops->di_size(idp));
1026 dt_ident_unref(const dt_ident_t *idp)
1028 return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
1029 (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
1033 dt_idkind_name(uint_t kind)
1036 case DT_IDENT_ARRAY: return ("associative array");
1037 case DT_IDENT_SCALAR: return ("scalar");
1038 case DT_IDENT_PTR: return ("pointer");
1039 case DT_IDENT_FUNC: return ("function");
1040 case DT_IDENT_AGG: return ("aggregation");
1041 case DT_IDENT_AGGFUNC: return ("aggregating function");
1042 case DT_IDENT_ACTFUNC: return ("tracing function");
1043 case DT_IDENT_XLSOU: return ("translated data");
1044 case DT_IDENT_XLPTR: return ("pointer to translated data");
1045 case DT_IDENT_SYMBOL: return ("external symbol reference");
1046 case DT_IDENT_ENUM: return ("enumerator");
1047 case DT_IDENT_PRAGAT: return ("#pragma attributes");
1048 case DT_IDENT_PRAGBN: return ("#pragma binding");
1049 case DT_IDENT_PROBE: return ("probe definition");
1050 default: return ("<?>");