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