1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Copyright (c) 2013, Joyent Inc. All rights reserved.
26 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
27 */
28
29 #pragma ident "%Z%%M% %I% %E% SMI"
30
31 /*
32 * DTrace D Language Parser
33 *
34 * The D Parser is a lex/yacc parser consisting of the lexer dt_lex.l, the
35 * parsing grammar dt_grammar.y, and this file, dt_parser.c, which handles
36 * the construction of the parse tree nodes and their syntactic validation.
37 * The parse tree is constructed of dt_node_t structures (see <dt_parser.h>)
38 * that are built in two passes: (1) the "create" pass, where the parse tree
39 * nodes are allocated by calls from the grammar to dt_node_*() subroutines,
40 * and (2) the "cook" pass, where nodes are coalesced, assigned D types, and
41 * validated according to the syntactic rules of the language.
42 *
43 * All node allocations are performed using dt_node_alloc(). All node frees
44 * during the parsing phase are performed by dt_node_free(), which frees node-
45 * internal state but does not actually free the nodes. All final node frees
46 * are done as part of the end of dt_compile() or as part of destroying
47 * persistent identifiers or translators which have embedded nodes.
48 *
49 * The dt_node_* routines that implement pass (1) may allocate new nodes. The
50 * dt_cook_* routines that implement pass (2) may *not* allocate new nodes.
51 * They may free existing nodes using dt_node_free(), but they may not actually
52 * deallocate any dt_node_t's. Currently dt_cook_op2() is an exception to this
53 * rule: see the comments therein for how this issue is resolved.
54 *
55 * The dt_cook_* routines are responsible for (at minimum) setting the final
56 * node type (dn_ctfp/dn_type) and attributes (dn_attr). If dn_ctfp/dn_type
57 * are set manually (i.e. not by one of the type assignment functions), then
58 * the DT_NF_COOKED flag must be set manually on the node.
59 *
60 * The cooking pass can be applied to the same parse tree more than once (used
61 * in the case of a comma-separated list of probe descriptions). As such, the
62 * cook routines must not perform any parse tree transformations which would
63 * be invalid if the tree were subsequently cooked using a different context.
64 *
65 * The dn_ctfp and dn_type fields form the type of the node. This tuple can
66 * take on the following set of values, which form our type invariants:
67 *
68 * 1. dn_ctfp = NULL, dn_type = CTF_ERR
69 *
70 * In this state, the node has unknown type and is not yet cooked. The
71 * DT_NF_COOKED flag is not yet set on the node.
72 *
73 * 2. dn_ctfp = DT_DYN_CTFP(dtp), dn_type = DT_DYN_TYPE(dtp)
74 *
75 * In this state, the node is a dynamic D type. This means that generic
76 * operations are not valid on this node and only code that knows how to
77 * examine the inner details of the node can operate on it. A <DYN> node
78 * must have dn_ident set to point to an identifier describing the object
79 * and its type. The DT_NF_REF flag is set for all nodes of type <DYN>.
80 * At present, the D compiler uses the <DYN> type for:
81 *
82 * - associative arrays that do not yet have a value type defined
83 * - translated data (i.e. the result of the xlate operator)
84 * - aggregations
85 *
86 * 3. dn_ctfp = DT_STR_CTFP(dtp), dn_type = DT_STR_TYPE(dtp)
87 *
88 * In this state, the node is of type D string. The string type is really
89 * a char[0] typedef, but requires special handling throughout the compiler.
90 *
91 * 4. dn_ctfp != NULL, dn_type = any other type ID
92 *
93 * In this state, the node is of some known D/CTF type. The normal libctf
94 * APIs can be used to learn more about the type name or structure. When
95 * the type is assigned, the DT_NF_SIGNED, DT_NF_REF, and DT_NF_BITFIELD
96 * flags cache the corresponding attributes of the underlying CTF type.
97 */
98
99 #include <sys/param.h>
100 #include <sys/sysmacros.h>
101 #include <limits.h>
102 #include <setjmp.h>
103 #include <strings.h>
104 #include <assert.h>
105 #ifdef illumos
106 #include <alloca.h>
107 #endif
108 #include <stdlib.h>
109 #include <stdarg.h>
110 #include <stdio.h>
111 #include <errno.h>
112 #include <ctype.h>
113
114 #include <dt_impl.h>
115 #include <dt_grammar.h>
116 #include <dt_module.h>
117 #include <dt_provider.h>
118 #include <dt_string.h>
119 #include <dt_as.h>
120
121 dt_pcb_t *yypcb; /* current control block for parser */
122 dt_node_t *yypragma; /* lex token list for control lines */
123 char yyintprefix; /* int token macro prefix (+/-) */
124 char yyintsuffix[4]; /* int token suffix string [uU][lL] */
125 int yyintdecimal; /* int token format flag (1=decimal, 0=octal/hex) */
126
127 static const char *
opstr(int op)128 opstr(int op)
129 {
130 switch (op) {
131 case DT_TOK_COMMA: return (",");
132 case DT_TOK_ELLIPSIS: return ("...");
133 case DT_TOK_ASGN: return ("=");
134 case DT_TOK_ADD_EQ: return ("+=");
135 case DT_TOK_SUB_EQ: return ("-=");
136 case DT_TOK_MUL_EQ: return ("*=");
137 case DT_TOK_DIV_EQ: return ("/=");
138 case DT_TOK_MOD_EQ: return ("%=");
139 case DT_TOK_AND_EQ: return ("&=");
140 case DT_TOK_XOR_EQ: return ("^=");
141 case DT_TOK_OR_EQ: return ("|=");
142 case DT_TOK_LSH_EQ: return ("<<=");
143 case DT_TOK_RSH_EQ: return (">>=");
144 case DT_TOK_QUESTION: return ("?");
145 case DT_TOK_COLON: return (":");
146 case DT_TOK_LOR: return ("||");
147 case DT_TOK_LXOR: return ("^^");
148 case DT_TOK_LAND: return ("&&");
149 case DT_TOK_BOR: return ("|");
150 case DT_TOK_XOR: return ("^");
151 case DT_TOK_BAND: return ("&");
152 case DT_TOK_EQU: return ("==");
153 case DT_TOK_NEQ: return ("!=");
154 case DT_TOK_LT: return ("<");
155 case DT_TOK_LE: return ("<=");
156 case DT_TOK_GT: return (">");
157 case DT_TOK_GE: return (">=");
158 case DT_TOK_LSH: return ("<<");
159 case DT_TOK_RSH: return (">>");
160 case DT_TOK_ADD: return ("+");
161 case DT_TOK_SUB: return ("-");
162 case DT_TOK_MUL: return ("*");
163 case DT_TOK_DIV: return ("/");
164 case DT_TOK_MOD: return ("%");
165 case DT_TOK_LNEG: return ("!");
166 case DT_TOK_BNEG: return ("~");
167 case DT_TOK_ADDADD: return ("++");
168 case DT_TOK_PREINC: return ("++");
169 case DT_TOK_POSTINC: return ("++");
170 case DT_TOK_SUBSUB: return ("--");
171 case DT_TOK_PREDEC: return ("--");
172 case DT_TOK_POSTDEC: return ("--");
173 case DT_TOK_IPOS: return ("+");
174 case DT_TOK_INEG: return ("-");
175 case DT_TOK_DEREF: return ("*");
176 case DT_TOK_ADDROF: return ("&");
177 case DT_TOK_OFFSETOF: return ("offsetof");
178 case DT_TOK_SIZEOF: return ("sizeof");
179 case DT_TOK_STRINGOF: return ("stringof");
180 case DT_TOK_XLATE: return ("xlate");
181 case DT_TOK_LPAR: return ("(");
182 case DT_TOK_RPAR: return (")");
183 case DT_TOK_LBRAC: return ("[");
184 case DT_TOK_RBRAC: return ("]");
185 case DT_TOK_PTR: return ("->");
186 case DT_TOK_DOT: return (".");
187 case DT_TOK_STRING: return ("<string>");
188 case DT_TOK_IDENT: return ("<ident>");
189 case DT_TOK_TNAME: return ("<type>");
190 case DT_TOK_INT: return ("<int>");
191 default: return ("<?>");
192 }
193 }
194
195 int
dt_type_lookup(const char * s,dtrace_typeinfo_t * tip)196 dt_type_lookup(const char *s, dtrace_typeinfo_t *tip)
197 {
198 static const char delimiters[] = " \t\n\r\v\f*`";
199 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
200 const char *p, *q, *r, *end, *obj;
201
202 for (p = s, end = s + strlen(s); *p != '\0'; p = q) {
203 while (isspace(*p))
204 p++; /* skip leading whitespace prior to token */
205
206 if (p == end || (q = strpbrk(p + 1, delimiters)) == NULL)
207 break; /* empty string or single token remaining */
208
209 if (*q == '`') {
210 char *object = alloca((size_t)(q - p) + 1);
211 char *type = alloca((size_t)(end - s) + 1);
212
213 /*
214 * Copy from the start of the token (p) to the location
215 * backquote (q) to extract the nul-terminated object.
216 */
217 bcopy(p, object, (size_t)(q - p));
218 object[(size_t)(q - p)] = '\0';
219
220 /*
221 * Copy the original string up to the start of this
222 * token (p) into type, and then concatenate everything
223 * after q. This is the type name without the object.
224 */
225 bcopy(s, type, (size_t)(p - s));
226 bcopy(q + 1, type + (size_t)(p - s), strlen(q + 1) + 1);
227
228 /*
229 * There may be at most three delimeters. The second
230 * delimeter is usually used to distinguish the type
231 * within a given module, however, there could be a link
232 * map id on the scene in which case that delimeter
233 * would be the third. We determine presence of the lmid
234 * if it rouglhly meets the from LM[0-9]
235 */
236 if ((r = strchr(q + 1, '`')) != NULL &&
237 ((r = strchr(r + 1, '`')) != NULL)) {
238 if (strchr(r + 1, '`') != NULL)
239 return (dt_set_errno(dtp,
240 EDT_BADSCOPE));
241 if (q[1] != 'L' || q[2] != 'M')
242 return (dt_set_errno(dtp,
243 EDT_BADSCOPE));
244 }
245
246 return (dtrace_lookup_by_type(dtp, object, type, tip));
247 }
248 }
249
250 if (yypcb->pcb_idepth != 0)
251 obj = DTRACE_OBJ_CDEFS;
252 else
253 obj = DTRACE_OBJ_EVERY;
254
255 return (dtrace_lookup_by_type(dtp, obj, s, tip));
256 }
257
258 /*
259 * When we parse type expressions or parse an expression with unary "&", we
260 * need to find a type that is a pointer to a previously known type.
261 * Unfortunately CTF is limited to a per-container view, so ctf_type_pointer()
262 * alone does not suffice for our needs. We provide a more intelligent wrapper
263 * for the compiler that attempts to compute a pointer to either the given type
264 * or its base (that is, we try both "foo_t *" and "struct foo *"), and also
265 * to potentially construct the required type on-the-fly.
266 */
267 int
dt_type_pointer(dtrace_typeinfo_t * tip)268 dt_type_pointer(dtrace_typeinfo_t *tip)
269 {
270 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
271 ctf_file_t *ctfp = tip->dtt_ctfp;
272 ctf_id_t type = tip->dtt_type;
273 ctf_id_t base = ctf_type_resolve(ctfp, type);
274 uint_t bflags = tip->dtt_flags;
275
276 dt_module_t *dmp;
277 ctf_id_t ptr;
278
279 if ((ptr = ctf_type_pointer(ctfp, type)) != CTF_ERR ||
280 (ptr = ctf_type_pointer(ctfp, base)) != CTF_ERR) {
281 tip->dtt_type = ptr;
282 return (0);
283 }
284
285 if (yypcb->pcb_idepth != 0)
286 dmp = dtp->dt_cdefs;
287 else
288 dmp = dtp->dt_ddefs;
289
290 if (ctfp != dmp->dm_ctfp && ctfp != ctf_parent_file(dmp->dm_ctfp) &&
291 (type = ctf_add_type(dmp->dm_ctfp, ctfp, type)) == CTF_ERR) {
292 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
293 return (dt_set_errno(dtp, EDT_CTF));
294 }
295
296 ptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, type);
297
298 if (ptr == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
299 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
300 return (dt_set_errno(dtp, EDT_CTF));
301 }
302
303 tip->dtt_object = dmp->dm_name;
304 tip->dtt_ctfp = dmp->dm_ctfp;
305 tip->dtt_type = ptr;
306 tip->dtt_flags = bflags;
307
308 return (0);
309 }
310
311 const char *
dt_type_name(ctf_file_t * ctfp,ctf_id_t type,char * buf,size_t len)312 dt_type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
313 {
314 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
315
316 if (ctfp == DT_FPTR_CTFP(dtp) && type == DT_FPTR_TYPE(dtp))
317 (void) snprintf(buf, len, "function pointer");
318 else if (ctfp == DT_FUNC_CTFP(dtp) && type == DT_FUNC_TYPE(dtp))
319 (void) snprintf(buf, len, "function");
320 else if (ctfp == DT_DYN_CTFP(dtp) && type == DT_DYN_TYPE(dtp))
321 (void) snprintf(buf, len, "dynamic variable");
322 else if (ctfp == NULL)
323 (void) snprintf(buf, len, "<none>");
324 else if (ctf_type_name(ctfp, type, buf, len) == NULL)
325 (void) snprintf(buf, len, "unknown");
326
327 return (buf);
328 }
329
330 /*
331 * Perform the "usual arithmetic conversions" to determine which of the two
332 * input operand types should be promoted and used as a result type. The
333 * rules for this are described in ISOC[6.3.1.8] and K&R[A6.5].
334 */
335 static void
dt_type_promote(dt_node_t * lp,dt_node_t * rp,ctf_file_t ** ofp,ctf_id_t * otype)336 dt_type_promote(dt_node_t *lp, dt_node_t *rp, ctf_file_t **ofp, ctf_id_t *otype)
337 {
338 ctf_file_t *lfp = lp->dn_ctfp;
339 ctf_id_t ltype = lp->dn_type;
340
341 ctf_file_t *rfp = rp->dn_ctfp;
342 ctf_id_t rtype = rp->dn_type;
343
344 ctf_id_t lbase = ctf_type_resolve(lfp, ltype);
345 uint_t lkind = ctf_type_kind(lfp, lbase);
346
347 ctf_id_t rbase = ctf_type_resolve(rfp, rtype);
348 uint_t rkind = ctf_type_kind(rfp, rbase);
349
350 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
351 ctf_encoding_t le, re;
352 uint_t lrank, rrank;
353
354 assert(lkind == CTF_K_INTEGER || lkind == CTF_K_ENUM);
355 assert(rkind == CTF_K_INTEGER || rkind == CTF_K_ENUM);
356
357 if (lkind == CTF_K_ENUM) {
358 lfp = DT_INT_CTFP(dtp);
359 ltype = lbase = DT_INT_TYPE(dtp);
360 }
361
362 if (rkind == CTF_K_ENUM) {
363 rfp = DT_INT_CTFP(dtp);
364 rtype = rbase = DT_INT_TYPE(dtp);
365 }
366
367 if (ctf_type_encoding(lfp, lbase, &le) == CTF_ERR) {
368 yypcb->pcb_hdl->dt_ctferr = ctf_errno(lfp);
369 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
370 }
371
372 if (ctf_type_encoding(rfp, rbase, &re) == CTF_ERR) {
373 yypcb->pcb_hdl->dt_ctferr = ctf_errno(rfp);
374 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
375 }
376
377 /*
378 * Compute an integer rank based on the size and unsigned status.
379 * If rank is identical, pick the "larger" of the equivalent types
380 * which we define as having a larger base ctf_id_t. If rank is
381 * different, pick the type with the greater rank.
382 */
383 lrank = le.cte_bits + ((le.cte_format & CTF_INT_SIGNED) == 0);
384 rrank = re.cte_bits + ((re.cte_format & CTF_INT_SIGNED) == 0);
385
386 if (lrank == rrank) {
387 if (lbase - rbase < 0)
388 goto return_rtype;
389 else
390 goto return_ltype;
391 } else if (lrank > rrank) {
392 goto return_ltype;
393 } else
394 goto return_rtype;
395
396 return_ltype:
397 *ofp = lfp;
398 *otype = ltype;
399 return;
400
401 return_rtype:
402 *ofp = rfp;
403 *otype = rtype;
404 }
405
406 void
dt_node_promote(dt_node_t * lp,dt_node_t * rp,dt_node_t * dnp)407 dt_node_promote(dt_node_t *lp, dt_node_t *rp, dt_node_t *dnp)
408 {
409 dt_type_promote(lp, rp, &dnp->dn_ctfp, &dnp->dn_type);
410 dt_node_type_assign(dnp, dnp->dn_ctfp, dnp->dn_type, B_FALSE);
411 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
412 }
413
414 const char *
dt_node_name(const dt_node_t * dnp,char * buf,size_t len)415 dt_node_name(const dt_node_t *dnp, char *buf, size_t len)
416 {
417 char n1[DT_TYPE_NAMELEN];
418 char n2[DT_TYPE_NAMELEN];
419
420 const char *prefix = "", *suffix = "";
421 const dtrace_syminfo_t *dts;
422 char *s;
423
424 switch (dnp->dn_kind) {
425 case DT_NODE_INT:
426 (void) snprintf(buf, len, "integer constant 0x%llx",
427 (u_longlong_t)dnp->dn_value);
428 break;
429 case DT_NODE_STRING:
430 s = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
431 (void) snprintf(buf, len, "string constant \"%s\"",
432 s != NULL ? s : dnp->dn_string);
433 free(s);
434 break;
435 case DT_NODE_IDENT:
436 (void) snprintf(buf, len, "identifier %s", dnp->dn_string);
437 break;
438 case DT_NODE_VAR:
439 case DT_NODE_FUNC:
440 case DT_NODE_AGG:
441 case DT_NODE_INLINE:
442 switch (dnp->dn_ident->di_kind) {
443 case DT_IDENT_FUNC:
444 case DT_IDENT_AGGFUNC:
445 case DT_IDENT_ACTFUNC:
446 suffix = "( )";
447 break;
448 case DT_IDENT_AGG:
449 prefix = "@";
450 break;
451 }
452 (void) snprintf(buf, len, "%s %s%s%s",
453 dt_idkind_name(dnp->dn_ident->di_kind),
454 prefix, dnp->dn_ident->di_name, suffix);
455 break;
456 case DT_NODE_SYM:
457 dts = dnp->dn_ident->di_data;
458 (void) snprintf(buf, len, "symbol %s`%s",
459 dts->dts_object, dts->dts_name);
460 break;
461 case DT_NODE_TYPE:
462 (void) snprintf(buf, len, "type %s",
463 dt_node_type_name(dnp, n1, sizeof (n1)));
464 break;
465 case DT_NODE_OP1:
466 case DT_NODE_OP2:
467 case DT_NODE_OP3:
468 (void) snprintf(buf, len, "operator %s", opstr(dnp->dn_op));
469 break;
470 case DT_NODE_DEXPR:
471 case DT_NODE_DFUNC:
472 if (dnp->dn_expr)
473 return (dt_node_name(dnp->dn_expr, buf, len));
474 (void) snprintf(buf, len, "%s", "statement");
475 break;
476 case DT_NODE_PDESC:
477 if (dnp->dn_desc->dtpd_id == 0) {
478 (void) snprintf(buf, len,
479 "probe description %s:%s:%s:%s",
480 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
481 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
482 } else {
483 (void) snprintf(buf, len, "probe description %u",
484 dnp->dn_desc->dtpd_id);
485 }
486 break;
487 case DT_NODE_CLAUSE:
488 (void) snprintf(buf, len, "%s", "clause");
489 break;
490 case DT_NODE_MEMBER:
491 (void) snprintf(buf, len, "member %s", dnp->dn_membname);
492 break;
493 case DT_NODE_XLATOR:
494 (void) snprintf(buf, len, "translator <%s> (%s)",
495 dt_type_name(dnp->dn_xlator->dx_dst_ctfp,
496 dnp->dn_xlator->dx_dst_type, n1, sizeof (n1)),
497 dt_type_name(dnp->dn_xlator->dx_src_ctfp,
498 dnp->dn_xlator->dx_src_type, n2, sizeof (n2)));
499 break;
500 case DT_NODE_PROG:
501 (void) snprintf(buf, len, "%s", "program");
502 break;
503 default:
504 (void) snprintf(buf, len, "node <%u>", dnp->dn_kind);
505 break;
506 }
507
508 return (buf);
509 }
510
511 /*
512 * dt_node_xalloc() can be used to create new parse nodes from any libdtrace
513 * caller. The caller is responsible for assigning dn_link appropriately.
514 */
515 dt_node_t *
dt_node_xalloc(dtrace_hdl_t * dtp,int kind)516 dt_node_xalloc(dtrace_hdl_t *dtp, int kind)
517 {
518 dt_node_t *dnp = dt_alloc(dtp, sizeof (dt_node_t));
519
520 if (dnp == NULL)
521 return (NULL);
522
523 dnp->dn_ctfp = NULL;
524 dnp->dn_type = CTF_ERR;
525 dnp->dn_kind = (uchar_t)kind;
526 dnp->dn_flags = 0;
527 dnp->dn_op = 0;
528 dnp->dn_line = -1;
529 dnp->dn_reg = -1;
530 dnp->dn_attr = _dtrace_defattr;
531 dnp->dn_list = NULL;
532 dnp->dn_link = NULL;
533 bzero(&dnp->dn_u, sizeof (dnp->dn_u));
534
535 return (dnp);
536 }
537
538 /*
539 * dt_node_alloc() is used to create new parse nodes from the parser. It
540 * assigns the node location based on the current lexer line number and places
541 * the new node on the default allocation list. If allocation fails, we
542 * automatically longjmp the caller back to the enclosing compilation call.
543 */
544 static dt_node_t *
dt_node_alloc(int kind)545 dt_node_alloc(int kind)
546 {
547 dt_node_t *dnp = dt_node_xalloc(yypcb->pcb_hdl, kind);
548
549 if (dnp == NULL)
550 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
551
552 dnp->dn_line = yylineno;
553 dnp->dn_link = yypcb->pcb_list;
554 yypcb->pcb_list = dnp;
555
556 return (dnp);
557 }
558
559 void
dt_node_free(dt_node_t * dnp)560 dt_node_free(dt_node_t *dnp)
561 {
562 uchar_t kind = dnp->dn_kind;
563
564 dnp->dn_kind = DT_NODE_FREE;
565
566 switch (kind) {
567 case DT_NODE_STRING:
568 case DT_NODE_IDENT:
569 case DT_NODE_TYPE:
570 free(dnp->dn_string);
571 dnp->dn_string = NULL;
572 break;
573
574 case DT_NODE_VAR:
575 case DT_NODE_FUNC:
576 case DT_NODE_PROBE:
577 if (dnp->dn_ident != NULL) {
578 if (dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN)
579 dt_ident_destroy(dnp->dn_ident);
580 dnp->dn_ident = NULL;
581 }
582 dt_node_list_free(&dnp->dn_args);
583 break;
584
585 case DT_NODE_OP1:
586 if (dnp->dn_child != NULL) {
587 dt_node_free(dnp->dn_child);
588 dnp->dn_child = NULL;
589 }
590 break;
591
592 case DT_NODE_OP3:
593 if (dnp->dn_expr != NULL) {
594 dt_node_free(dnp->dn_expr);
595 dnp->dn_expr = NULL;
596 }
597 /*FALLTHRU*/
598 case DT_NODE_OP2:
599 if (dnp->dn_left != NULL) {
600 dt_node_free(dnp->dn_left);
601 dnp->dn_left = NULL;
602 }
603 if (dnp->dn_right != NULL) {
604 dt_node_free(dnp->dn_right);
605 dnp->dn_right = NULL;
606 }
607 break;
608
609 case DT_NODE_DEXPR:
610 case DT_NODE_DFUNC:
611 if (dnp->dn_expr != NULL) {
612 dt_node_free(dnp->dn_expr);
613 dnp->dn_expr = NULL;
614 }
615 break;
616
617 case DT_NODE_AGG:
618 if (dnp->dn_aggfun != NULL) {
619 dt_node_free(dnp->dn_aggfun);
620 dnp->dn_aggfun = NULL;
621 }
622 dt_node_list_free(&dnp->dn_aggtup);
623 break;
624
625 case DT_NODE_PDESC:
626 free(dnp->dn_spec);
627 dnp->dn_spec = NULL;
628 free(dnp->dn_desc);
629 dnp->dn_desc = NULL;
630 break;
631
632 case DT_NODE_CLAUSE:
633 if (dnp->dn_pred != NULL)
634 dt_node_free(dnp->dn_pred);
635 if (dnp->dn_locals != NULL)
636 dt_idhash_destroy(dnp->dn_locals);
637 dt_node_list_free(&dnp->dn_pdescs);
638 dt_node_list_free(&dnp->dn_acts);
639 break;
640
641 case DT_NODE_MEMBER:
642 free(dnp->dn_membname);
643 dnp->dn_membname = NULL;
644 if (dnp->dn_membexpr != NULL) {
645 dt_node_free(dnp->dn_membexpr);
646 dnp->dn_membexpr = NULL;
647 }
648 break;
649
650 case DT_NODE_PROVIDER:
651 dt_node_list_free(&dnp->dn_probes);
652 free(dnp->dn_provname);
653 dnp->dn_provname = NULL;
654 break;
655
656 case DT_NODE_PROG:
657 dt_node_list_free(&dnp->dn_list);
658 break;
659 }
660 }
661
662 void
dt_node_attr_assign(dt_node_t * dnp,dtrace_attribute_t attr)663 dt_node_attr_assign(dt_node_t *dnp, dtrace_attribute_t attr)
664 {
665 if ((yypcb->pcb_cflags & DTRACE_C_EATTR) &&
666 (dt_attr_cmp(attr, yypcb->pcb_amin) < 0)) {
667 char a[DTRACE_ATTR2STR_MAX];
668 char s[BUFSIZ];
669
670 dnerror(dnp, D_ATTR_MIN, "attributes for %s (%s) are less than "
671 "predefined minimum\n", dt_node_name(dnp, s, sizeof (s)),
672 dtrace_attr2str(attr, a, sizeof (a)));
673 }
674
675 dnp->dn_attr = attr;
676 }
677
678 void
dt_node_type_assign(dt_node_t * dnp,ctf_file_t * fp,ctf_id_t type,boolean_t user)679 dt_node_type_assign(dt_node_t *dnp, ctf_file_t *fp, ctf_id_t type,
680 boolean_t user)
681 {
682 ctf_id_t base = ctf_type_resolve(fp, type);
683 uint_t kind = ctf_type_kind(fp, base);
684 ctf_encoding_t e;
685
686 dnp->dn_flags &=
687 ~(DT_NF_SIGNED | DT_NF_REF | DT_NF_BITFIELD | DT_NF_USERLAND);
688
689 if (kind == CTF_K_INTEGER && ctf_type_encoding(fp, base, &e) == 0) {
690 size_t size = e.cte_bits / NBBY;
691
692 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)))
693 dnp->dn_flags |= DT_NF_BITFIELD;
694
695 if (e.cte_format & CTF_INT_SIGNED)
696 dnp->dn_flags |= DT_NF_SIGNED;
697 }
698
699 if (kind == CTF_K_FLOAT && ctf_type_encoding(fp, base, &e) == 0) {
700 if (e.cte_bits / NBBY > sizeof (uint64_t))
701 dnp->dn_flags |= DT_NF_REF;
702 }
703
704 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION ||
705 kind == CTF_K_FORWARD ||
706 kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION)
707 dnp->dn_flags |= DT_NF_REF;
708 else if (yypcb != NULL && fp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
709 type == DT_DYN_TYPE(yypcb->pcb_hdl))
710 dnp->dn_flags |= DT_NF_REF;
711
712 if (user)
713 dnp->dn_flags |= DT_NF_USERLAND;
714
715 dnp->dn_flags |= DT_NF_COOKED;
716 dnp->dn_ctfp = fp;
717 dnp->dn_type = type;
718 }
719
720 void
dt_node_type_propagate(const dt_node_t * src,dt_node_t * dst)721 dt_node_type_propagate(const dt_node_t *src, dt_node_t *dst)
722 {
723 assert(src->dn_flags & DT_NF_COOKED);
724 dst->dn_flags = src->dn_flags & ~DT_NF_LVALUE;
725 dst->dn_ctfp = src->dn_ctfp;
726 dst->dn_type = src->dn_type;
727 }
728
729 const char *
dt_node_type_name(const dt_node_t * dnp,char * buf,size_t len)730 dt_node_type_name(const dt_node_t *dnp, char *buf, size_t len)
731 {
732 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL) {
733 (void) snprintf(buf, len, "%s",
734 dt_idkind_name(dt_ident_resolve(dnp->dn_ident)->di_kind));
735 return (buf);
736 }
737
738 if (dnp->dn_flags & DT_NF_USERLAND) {
739 size_t n = snprintf(buf, len, "userland ");
740 len = len > n ? len - n : 0;
741 (void) dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf + n, len);
742 return (buf);
743 }
744
745 return (dt_type_name(dnp->dn_ctfp, dnp->dn_type, buf, len));
746 }
747
748 size_t
dt_node_type_size(const dt_node_t * dnp)749 dt_node_type_size(const dt_node_t *dnp)
750 {
751 ctf_id_t base;
752 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
753
754 if (dnp->dn_kind == DT_NODE_STRING)
755 return (strlen(dnp->dn_string) + 1);
756
757 if (dt_node_is_dynamic(dnp) && dnp->dn_ident != NULL)
758 return (dt_ident_size(dnp->dn_ident));
759
760 base = ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type);
761
762 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_FORWARD)
763 return (0);
764
765 /*
766 * Here we have a 32-bit user pointer that is being used with a 64-bit
767 * kernel. When we're using it and its tagged as a userland reference --
768 * then we need to keep it as a 32-bit pointer. However, if we are
769 * referring to it as a kernel address, eg. being used after a copyin()
770 * then we need to make sure that we actually return the kernel's size
771 * of a pointer, 8 bytes.
772 */
773 if (ctf_type_kind(dnp->dn_ctfp, base) == CTF_K_POINTER &&
774 ctf_getmodel(dnp->dn_ctfp) == CTF_MODEL_ILP32 &&
775 !(dnp->dn_flags & DT_NF_USERLAND) &&
776 dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
777 return (8);
778
779 return (ctf_type_size(dnp->dn_ctfp, dnp->dn_type));
780 }
781
782 /*
783 * Determine if the specified parse tree node references an identifier of the
784 * specified kind, and if so return a pointer to it; otherwise return NULL.
785 * This function resolves the identifier itself, following through any inlines.
786 */
787 dt_ident_t *
dt_node_resolve(const dt_node_t * dnp,uint_t idkind)788 dt_node_resolve(const dt_node_t *dnp, uint_t idkind)
789 {
790 dt_ident_t *idp;
791
792 switch (dnp->dn_kind) {
793 case DT_NODE_VAR:
794 case DT_NODE_SYM:
795 case DT_NODE_FUNC:
796 case DT_NODE_AGG:
797 case DT_NODE_INLINE:
798 case DT_NODE_PROBE:
799 idp = dt_ident_resolve(dnp->dn_ident);
800 return (idp->di_kind == idkind ? idp : NULL);
801 }
802
803 if (dt_node_is_dynamic(dnp)) {
804 idp = dt_ident_resolve(dnp->dn_ident);
805 return (idp->di_kind == idkind ? idp : NULL);
806 }
807
808 return (NULL);
809 }
810
811 size_t
dt_node_sizeof(const dt_node_t * dnp)812 dt_node_sizeof(const dt_node_t *dnp)
813 {
814 dtrace_syminfo_t *sip;
815 GElf_Sym sym;
816 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
817
818 /*
819 * The size of the node as used for the sizeof() operator depends on
820 * the kind of the node. If the node is a SYM, the size is obtained
821 * from the symbol table; if it is not a SYM, the size is determined
822 * from the node's type. This is slightly different from C's sizeof()
823 * operator in that (for example) when applied to a function, sizeof()
824 * will evaluate to the length of the function rather than the size of
825 * the function type.
826 */
827 if (dnp->dn_kind != DT_NODE_SYM)
828 return (dt_node_type_size(dnp));
829
830 sip = dnp->dn_ident->di_data;
831
832 if (dtrace_lookup_by_name(dtp, sip->dts_object,
833 sip->dts_name, &sym, NULL) == -1)
834 return (0);
835
836 return (sym.st_size);
837 }
838
839 int
dt_node_is_integer(const dt_node_t * dnp)840 dt_node_is_integer(const dt_node_t *dnp)
841 {
842 ctf_file_t *fp = dnp->dn_ctfp;
843 ctf_encoding_t e;
844 ctf_id_t type;
845 uint_t kind;
846
847 assert(dnp->dn_flags & DT_NF_COOKED);
848
849 type = ctf_type_resolve(fp, dnp->dn_type);
850 kind = ctf_type_kind(fp, type);
851
852 if (kind == CTF_K_INTEGER &&
853 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
854 return (0); /* void integer */
855
856 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
857 }
858
859 int
dt_node_is_float(const dt_node_t * dnp)860 dt_node_is_float(const dt_node_t *dnp)
861 {
862 ctf_file_t *fp = dnp->dn_ctfp;
863 ctf_encoding_t e;
864 ctf_id_t type;
865 uint_t kind;
866
867 assert(dnp->dn_flags & DT_NF_COOKED);
868
869 type = ctf_type_resolve(fp, dnp->dn_type);
870 kind = ctf_type_kind(fp, type);
871
872 return (kind == CTF_K_FLOAT &&
873 ctf_type_encoding(dnp->dn_ctfp, type, &e) == 0 && (
874 e.cte_format == CTF_FP_SINGLE || e.cte_format == CTF_FP_DOUBLE ||
875 e.cte_format == CTF_FP_LDOUBLE));
876 }
877
878 int
dt_node_is_scalar(const dt_node_t * dnp)879 dt_node_is_scalar(const dt_node_t *dnp)
880 {
881 ctf_file_t *fp = dnp->dn_ctfp;
882 ctf_encoding_t e;
883 ctf_id_t type;
884 uint_t kind;
885
886 assert(dnp->dn_flags & DT_NF_COOKED);
887
888 type = ctf_type_resolve(fp, dnp->dn_type);
889 kind = ctf_type_kind(fp, type);
890
891 if (kind == CTF_K_INTEGER &&
892 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e))
893 return (0); /* void cannot be used as a scalar */
894
895 return (kind == CTF_K_INTEGER || kind == CTF_K_ENUM ||
896 kind == CTF_K_POINTER);
897 }
898
899 int
dt_node_is_arith(const dt_node_t * dnp)900 dt_node_is_arith(const dt_node_t *dnp)
901 {
902 ctf_file_t *fp = dnp->dn_ctfp;
903 ctf_encoding_t e;
904 ctf_id_t type;
905 uint_t kind;
906
907 assert(dnp->dn_flags & DT_NF_COOKED);
908
909 type = ctf_type_resolve(fp, dnp->dn_type);
910 kind = ctf_type_kind(fp, type);
911
912 if (kind == CTF_K_INTEGER)
913 return (ctf_type_encoding(fp, type, &e) == 0 && !IS_VOID(e));
914 else
915 return (kind == CTF_K_ENUM);
916 }
917
918 int
dt_node_is_vfptr(const dt_node_t * dnp)919 dt_node_is_vfptr(const dt_node_t *dnp)
920 {
921 ctf_file_t *fp = dnp->dn_ctfp;
922 ctf_encoding_t e;
923 ctf_id_t type;
924 uint_t kind;
925
926 assert(dnp->dn_flags & DT_NF_COOKED);
927
928 type = ctf_type_resolve(fp, dnp->dn_type);
929 if (ctf_type_kind(fp, type) != CTF_K_POINTER)
930 return (0); /* type is not a pointer */
931
932 type = ctf_type_resolve(fp, ctf_type_reference(fp, type));
933 kind = ctf_type_kind(fp, type);
934
935 return (kind == CTF_K_FUNCTION || (kind == CTF_K_INTEGER &&
936 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e)));
937 }
938
939 int
dt_node_is_dynamic(const dt_node_t * dnp)940 dt_node_is_dynamic(const dt_node_t *dnp)
941 {
942 if (dnp->dn_kind == DT_NODE_VAR &&
943 (dnp->dn_ident->di_flags & DT_IDFLG_INLINE)) {
944 const dt_idnode_t *inp = dnp->dn_ident->di_iarg;
945 return (inp->din_root ? dt_node_is_dynamic(inp->din_root) : 0);
946 }
947
948 return (dnp->dn_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
949 dnp->dn_type == DT_DYN_TYPE(yypcb->pcb_hdl));
950 }
951
952 int
dt_node_is_string(const dt_node_t * dnp)953 dt_node_is_string(const dt_node_t *dnp)
954 {
955 return (dnp->dn_ctfp == DT_STR_CTFP(yypcb->pcb_hdl) &&
956 dnp->dn_type == DT_STR_TYPE(yypcb->pcb_hdl));
957 }
958
959 int
dt_node_is_stack(const dt_node_t * dnp)960 dt_node_is_stack(const dt_node_t *dnp)
961 {
962 return (dnp->dn_ctfp == DT_STACK_CTFP(yypcb->pcb_hdl) &&
963 dnp->dn_type == DT_STACK_TYPE(yypcb->pcb_hdl));
964 }
965
966 int
dt_node_is_symaddr(const dt_node_t * dnp)967 dt_node_is_symaddr(const dt_node_t *dnp)
968 {
969 return (dnp->dn_ctfp == DT_SYMADDR_CTFP(yypcb->pcb_hdl) &&
970 dnp->dn_type == DT_SYMADDR_TYPE(yypcb->pcb_hdl));
971 }
972
973 int
dt_node_is_usymaddr(const dt_node_t * dnp)974 dt_node_is_usymaddr(const dt_node_t *dnp)
975 {
976 return (dnp->dn_ctfp == DT_USYMADDR_CTFP(yypcb->pcb_hdl) &&
977 dnp->dn_type == DT_USYMADDR_TYPE(yypcb->pcb_hdl));
978 }
979
980 int
dt_node_is_strcompat(const dt_node_t * dnp)981 dt_node_is_strcompat(const dt_node_t *dnp)
982 {
983 ctf_file_t *fp = dnp->dn_ctfp;
984 ctf_encoding_t e;
985 ctf_arinfo_t r;
986 ctf_id_t base;
987 uint_t kind;
988
989 assert(dnp->dn_flags & DT_NF_COOKED);
990
991 base = ctf_type_resolve(fp, dnp->dn_type);
992 kind = ctf_type_kind(fp, base);
993
994 if (kind == CTF_K_POINTER &&
995 (base = ctf_type_reference(fp, base)) != CTF_ERR &&
996 (base = ctf_type_resolve(fp, base)) != CTF_ERR &&
997 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
998 return (1); /* promote char pointer to string */
999
1000 if (kind == CTF_K_ARRAY && ctf_array_info(fp, base, &r) == 0 &&
1001 (base = ctf_type_resolve(fp, r.ctr_contents)) != CTF_ERR &&
1002 ctf_type_encoding(fp, base, &e) == 0 && IS_CHAR(e))
1003 return (1); /* promote char array to string */
1004
1005 return (0);
1006 }
1007
1008 int
dt_node_is_pointer(const dt_node_t * dnp)1009 dt_node_is_pointer(const dt_node_t *dnp)
1010 {
1011 ctf_file_t *fp = dnp->dn_ctfp;
1012 uint_t kind;
1013
1014 assert(dnp->dn_flags & DT_NF_COOKED);
1015
1016 if (dt_node_is_string(dnp))
1017 return (0); /* string are pass-by-ref but act like structs */
1018
1019 kind = ctf_type_kind(fp, ctf_type_resolve(fp, dnp->dn_type));
1020 return (kind == CTF_K_POINTER || kind == CTF_K_ARRAY);
1021 }
1022
1023 int
dt_node_is_void(const dt_node_t * dnp)1024 dt_node_is_void(const dt_node_t *dnp)
1025 {
1026 ctf_file_t *fp = dnp->dn_ctfp;
1027 ctf_encoding_t e;
1028 ctf_id_t type;
1029
1030 if (dt_node_is_dynamic(dnp))
1031 return (0); /* <DYN> is an alias for void but not the same */
1032
1033 if (dt_node_is_stack(dnp))
1034 return (0);
1035
1036 if (dt_node_is_symaddr(dnp) || dt_node_is_usymaddr(dnp))
1037 return (0);
1038
1039 type = ctf_type_resolve(fp, dnp->dn_type);
1040
1041 return (ctf_type_kind(fp, type) == CTF_K_INTEGER &&
1042 ctf_type_encoding(fp, type, &e) == 0 && IS_VOID(e));
1043 }
1044
1045 int
dt_node_is_ptrcompat(const dt_node_t * lp,const dt_node_t * rp,ctf_file_t ** fpp,ctf_id_t * tp)1046 dt_node_is_ptrcompat(const dt_node_t *lp, const dt_node_t *rp,
1047 ctf_file_t **fpp, ctf_id_t *tp)
1048 {
1049 ctf_file_t *lfp = lp->dn_ctfp;
1050 ctf_file_t *rfp = rp->dn_ctfp;
1051
1052 ctf_id_t lbase = CTF_ERR, rbase = CTF_ERR;
1053 ctf_id_t lref = CTF_ERR, rref = CTF_ERR;
1054
1055 int lp_is_void, rp_is_void, lp_is_int, rp_is_int, compat;
1056 uint_t lkind, rkind;
1057 ctf_encoding_t e;
1058 ctf_arinfo_t r;
1059
1060 assert(lp->dn_flags & DT_NF_COOKED);
1061 assert(rp->dn_flags & DT_NF_COOKED);
1062
1063 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp))
1064 return (0); /* fail if either node is a dynamic variable */
1065
1066 lp_is_int = dt_node_is_integer(lp);
1067 rp_is_int = dt_node_is_integer(rp);
1068
1069 if (lp_is_int && rp_is_int)
1070 return (0); /* fail if both nodes are integers */
1071
1072 if (lp_is_int && (lp->dn_kind != DT_NODE_INT || lp->dn_value != 0))
1073 return (0); /* fail if lp is an integer that isn't 0 constant */
1074
1075 if (rp_is_int && (rp->dn_kind != DT_NODE_INT || rp->dn_value != 0))
1076 return (0); /* fail if rp is an integer that isn't 0 constant */
1077
1078 if ((lp_is_int == 0 && rp_is_int == 0) && (
1079 (lp->dn_flags & DT_NF_USERLAND) ^ (rp->dn_flags & DT_NF_USERLAND)))
1080 return (0); /* fail if only one pointer is a userland address */
1081
1082 /*
1083 * Resolve the left-hand and right-hand types to their base type, and
1084 * then resolve the referenced type as well (assuming the base type
1085 * is CTF_K_POINTER or CTF_K_ARRAY). Otherwise [lr]ref = CTF_ERR.
1086 */
1087 if (!lp_is_int) {
1088 lbase = ctf_type_resolve(lfp, lp->dn_type);
1089 lkind = ctf_type_kind(lfp, lbase);
1090
1091 if (lkind == CTF_K_POINTER) {
1092 lref = ctf_type_resolve(lfp,
1093 ctf_type_reference(lfp, lbase));
1094 } else if (lkind == CTF_K_ARRAY &&
1095 ctf_array_info(lfp, lbase, &r) == 0) {
1096 lref = ctf_type_resolve(lfp, r.ctr_contents);
1097 }
1098 }
1099
1100 if (!rp_is_int) {
1101 rbase = ctf_type_resolve(rfp, rp->dn_type);
1102 rkind = ctf_type_kind(rfp, rbase);
1103
1104 if (rkind == CTF_K_POINTER) {
1105 rref = ctf_type_resolve(rfp,
1106 ctf_type_reference(rfp, rbase));
1107 } else if (rkind == CTF_K_ARRAY &&
1108 ctf_array_info(rfp, rbase, &r) == 0) {
1109 rref = ctf_type_resolve(rfp, r.ctr_contents);
1110 }
1111 }
1112
1113 /*
1114 * We know that one or the other type may still be a zero-valued
1115 * integer constant. To simplify the code below, set the integer
1116 * type variables equal to the non-integer types and proceed.
1117 */
1118 if (lp_is_int) {
1119 lbase = rbase;
1120 lkind = rkind;
1121 lref = rref;
1122 lfp = rfp;
1123 } else if (rp_is_int) {
1124 rbase = lbase;
1125 rkind = lkind;
1126 rref = lref;
1127 rfp = lfp;
1128 }
1129
1130 lp_is_void = ctf_type_encoding(lfp, lref, &e) == 0 && IS_VOID(e);
1131 rp_is_void = ctf_type_encoding(rfp, rref, &e) == 0 && IS_VOID(e);
1132
1133 /*
1134 * Let a pointer to a forward declaration be compatible with a pointer
1135 * to a struct or union of the same name.
1136 */
1137 if (lkind == CTF_K_POINTER && rkind == CTF_K_POINTER) {
1138 int lrkind, rrkind;
1139
1140 lrkind = ctf_type_kind(lfp, lref);
1141 rrkind = ctf_type_kind(rfp, rref);
1142 if (lrkind == CTF_K_FORWARD || rrkind == CTF_K_FORWARD) {
1143 const char *lname, *rname;
1144 char ln[DT_TYPE_NAMELEN], rn[DT_TYPE_NAMELEN];
1145
1146 lname = ctf_type_name(lfp, lref, ln, sizeof (ln));
1147 rname = ctf_type_name(rfp, rref, rn, sizeof (rn));
1148 if (lname != NULL && rname != NULL &&
1149 strcmp(lname, rname) == 0) {
1150 lp_is_void = lrkind == CTF_K_FORWARD;
1151 rp_is_void = rrkind == CTF_K_FORWARD;
1152 }
1153 }
1154 }
1155
1156 /*
1157 * The types are compatible if both are pointers to the same type, or
1158 * if either pointer is a void pointer. If they are compatible, set
1159 * tp to point to the more specific pointer type and return it.
1160 */
1161 compat = (lkind == CTF_K_POINTER || lkind == CTF_K_ARRAY) &&
1162 (rkind == CTF_K_POINTER || rkind == CTF_K_ARRAY) &&
1163 (lp_is_void || rp_is_void || ctf_type_compat(lfp, lref, rfp, rref));
1164
1165 if (compat) {
1166 if (fpp != NULL)
1167 *fpp = rp_is_void ? lfp : rfp;
1168 if (tp != NULL)
1169 *tp = rp_is_void ? lbase : rbase;
1170 }
1171
1172 return (compat);
1173 }
1174
1175 /*
1176 * The rules for checking argument types against parameter types are described
1177 * in the ANSI-C spec (see K&R[A7.3.2] and K&R[A7.17]). We use the same rule
1178 * set to determine whether associative array arguments match the prototype.
1179 */
1180 int
dt_node_is_argcompat(const dt_node_t * lp,const dt_node_t * rp)1181 dt_node_is_argcompat(const dt_node_t *lp, const dt_node_t *rp)
1182 {
1183 ctf_file_t *lfp = lp->dn_ctfp;
1184 ctf_file_t *rfp = rp->dn_ctfp;
1185
1186 assert(lp->dn_flags & DT_NF_COOKED);
1187 assert(rp->dn_flags & DT_NF_COOKED);
1188
1189 if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
1190 return (1); /* integer types are compatible */
1191
1192 if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp))
1193 return (1); /* string types are compatible */
1194
1195 if (dt_node_is_stack(lp) && dt_node_is_stack(rp))
1196 return (1); /* stack types are compatible */
1197
1198 if (dt_node_is_symaddr(lp) && dt_node_is_symaddr(rp))
1199 return (1); /* symaddr types are compatible */
1200
1201 if (dt_node_is_usymaddr(lp) && dt_node_is_usymaddr(rp))
1202 return (1); /* usymaddr types are compatible */
1203
1204 switch (ctf_type_kind(lfp, ctf_type_resolve(lfp, lp->dn_type))) {
1205 case CTF_K_FUNCTION:
1206 case CTF_K_STRUCT:
1207 case CTF_K_UNION:
1208 return (ctf_type_compat(lfp, lp->dn_type, rfp, rp->dn_type));
1209 default:
1210 return (dt_node_is_ptrcompat(lp, rp, NULL, NULL));
1211 }
1212 }
1213
1214 /*
1215 * We provide dt_node_is_posconst() as a convenience routine for callers who
1216 * wish to verify that an argument is a positive non-zero integer constant.
1217 */
1218 int
dt_node_is_posconst(const dt_node_t * dnp)1219 dt_node_is_posconst(const dt_node_t *dnp)
1220 {
1221 return (dnp->dn_kind == DT_NODE_INT && dnp->dn_value != 0 && (
1222 (dnp->dn_flags & DT_NF_SIGNED) == 0 || (int64_t)dnp->dn_value > 0));
1223 }
1224
1225 int
dt_node_is_actfunc(const dt_node_t * dnp)1226 dt_node_is_actfunc(const dt_node_t *dnp)
1227 {
1228 return (dnp->dn_kind == DT_NODE_FUNC &&
1229 dnp->dn_ident->di_kind == DT_IDENT_ACTFUNC);
1230 }
1231
1232 /*
1233 * The original rules for integer constant typing are described in K&R[A2.5.1].
1234 * However, since we support long long, we instead use the rules from ISO C99
1235 * clause 6.4.4.1 since that is where long longs are formally described. The
1236 * rules require us to know whether the constant was specified in decimal or
1237 * in octal or hex, which we do by looking at our lexer's 'yyintdecimal' flag.
1238 * The type of an integer constant is the first of the corresponding list in
1239 * which its value can be represented:
1240 *
1241 * unsuffixed decimal: int, long, long long
1242 * unsuffixed oct/hex: int, unsigned int, long, unsigned long,
1243 * long long, unsigned long long
1244 * suffix [uU]: unsigned int, unsigned long, unsigned long long
1245 * suffix [lL] decimal: long, long long
1246 * suffix [lL] oct/hex: long, unsigned long, long long, unsigned long long
1247 * suffix [uU][Ll]: unsigned long, unsigned long long
1248 * suffix ll/LL decimal: long long
1249 * suffix ll/LL oct/hex: long long, unsigned long long
1250 * suffix [uU][ll/LL]: unsigned long long
1251 *
1252 * Given that our lexer has already validated the suffixes by regexp matching,
1253 * there is an obvious way to concisely encode these rules: construct an array
1254 * of the types in the order int, unsigned int, long, unsigned long, long long,
1255 * unsigned long long. Compute an integer array starting index based on the
1256 * suffix (e.g. none = 0, u = 1, ull = 5), and compute an increment based on
1257 * the specifier (dec/oct/hex) and suffix (u). Then iterate from the starting
1258 * index to the end, advancing using the increment, and searching until we
1259 * find a limit that matches or we run out of choices (overflow). To make it
1260 * even faster, we precompute the table of type information in dtrace_open().
1261 */
1262 dt_node_t *
dt_node_int(uintmax_t value)1263 dt_node_int(uintmax_t value)
1264 {
1265 dt_node_t *dnp = dt_node_alloc(DT_NODE_INT);
1266 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1267
1268 int n = (yyintdecimal | (yyintsuffix[0] == 'u')) + 1;
1269 int i = 0;
1270
1271 const char *p;
1272 char c;
1273
1274 dnp->dn_op = DT_TOK_INT;
1275 dnp->dn_value = value;
1276
1277 for (p = yyintsuffix; (c = *p) != '\0'; p++) {
1278 if (c == 'U' || c == 'u')
1279 i += 1;
1280 else if (c == 'L' || c == 'l')
1281 i += 2;
1282 }
1283
1284 for (; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i += n) {
1285 if (value <= dtp->dt_ints[i].did_limit) {
1286 dt_node_type_assign(dnp,
1287 dtp->dt_ints[i].did_ctfp,
1288 dtp->dt_ints[i].did_type, B_FALSE);
1289
1290 /*
1291 * If a prefix character is present in macro text, add
1292 * in the corresponding operator node (see dt_lex.l).
1293 */
1294 switch (yyintprefix) {
1295 case '+':
1296 return (dt_node_op1(DT_TOK_IPOS, dnp));
1297 case '-':
1298 return (dt_node_op1(DT_TOK_INEG, dnp));
1299 default:
1300 return (dnp);
1301 }
1302 }
1303 }
1304
1305 xyerror(D_INT_OFLOW, "integer constant 0x%llx cannot be represented "
1306 "in any built-in integral type\n", (u_longlong_t)value);
1307 /*NOTREACHED*/
1308 return (NULL); /* keep gcc happy */
1309 }
1310
1311 dt_node_t *
dt_node_string(char * string)1312 dt_node_string(char *string)
1313 {
1314 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1315 dt_node_t *dnp;
1316
1317 if (string == NULL)
1318 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1319
1320 dnp = dt_node_alloc(DT_NODE_STRING);
1321 dnp->dn_op = DT_TOK_STRING;
1322 dnp->dn_string = string;
1323 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp), B_FALSE);
1324
1325 return (dnp);
1326 }
1327
1328 dt_node_t *
dt_node_ident(char * name)1329 dt_node_ident(char *name)
1330 {
1331 dt_ident_t *idp;
1332 dt_node_t *dnp;
1333
1334 if (name == NULL)
1335 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1336
1337 /*
1338 * If the identifier is an inlined integer constant, then create an INT
1339 * node that is a clone of the inline parse tree node and return that
1340 * immediately, allowing this inline to be used in parsing contexts
1341 * that require constant expressions (e.g. scalar array sizes).
1342 */
1343 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL &&
1344 (idp->di_flags & DT_IDFLG_INLINE)) {
1345 dt_idnode_t *inp = idp->di_iarg;
1346
1347 if (inp->din_root != NULL &&
1348 inp->din_root->dn_kind == DT_NODE_INT) {
1349 free(name);
1350
1351 dnp = dt_node_alloc(DT_NODE_INT);
1352 dnp->dn_op = DT_TOK_INT;
1353 dnp->dn_value = inp->din_root->dn_value;
1354 dt_node_type_propagate(inp->din_root, dnp);
1355
1356 return (dnp);
1357 }
1358 }
1359
1360 dnp = dt_node_alloc(DT_NODE_IDENT);
1361 dnp->dn_op = name[0] == '@' ? DT_TOK_AGG : DT_TOK_IDENT;
1362 dnp->dn_string = name;
1363
1364 return (dnp);
1365 }
1366
1367 /*
1368 * Create an empty node of type corresponding to the given declaration.
1369 * Explicit references to user types (C or D) are assigned the default
1370 * stability; references to other types are _dtrace_typattr (Private).
1371 */
1372 dt_node_t *
dt_node_type(dt_decl_t * ddp)1373 dt_node_type(dt_decl_t *ddp)
1374 {
1375 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1376 dtrace_typeinfo_t dtt;
1377 dt_node_t *dnp;
1378 char *name = NULL;
1379 int err;
1380
1381 /*
1382 * If 'ddp' is NULL, we get a decl by popping the decl stack. This
1383 * form of dt_node_type() is used by parameter rules in dt_grammar.y.
1384 */
1385 if (ddp == NULL)
1386 ddp = dt_decl_pop_param(&name);
1387
1388 err = dt_decl_type(ddp, &dtt);
1389 dt_decl_free(ddp);
1390
1391 if (err != 0) {
1392 free(name);
1393 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1394 }
1395
1396 dnp = dt_node_alloc(DT_NODE_TYPE);
1397 dnp->dn_op = DT_TOK_IDENT;
1398 dnp->dn_string = name;
1399
1400 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, dtt.dtt_flags);
1401
1402 if (dtt.dtt_ctfp == dtp->dt_cdefs->dm_ctfp ||
1403 dtt.dtt_ctfp == dtp->dt_ddefs->dm_ctfp)
1404 dt_node_attr_assign(dnp, _dtrace_defattr);
1405 else
1406 dt_node_attr_assign(dnp, _dtrace_typattr);
1407
1408 return (dnp);
1409 }
1410
1411 /*
1412 * Create a type node corresponding to a varargs (...) parameter by just
1413 * assigning it type CTF_ERR. The decl processing code will handle this.
1414 */
1415 dt_node_t *
dt_node_vatype(void)1416 dt_node_vatype(void)
1417 {
1418 dt_node_t *dnp = dt_node_alloc(DT_NODE_TYPE);
1419
1420 dnp->dn_op = DT_TOK_IDENT;
1421 dnp->dn_ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
1422 dnp->dn_type = CTF_ERR;
1423 dnp->dn_attr = _dtrace_defattr;
1424
1425 return (dnp);
1426 }
1427
1428 /*
1429 * Instantiate a decl using the contents of the current declaration stack. As
1430 * we do not currently permit decls to be initialized, this function currently
1431 * returns NULL and no parse node is created. When this function is called,
1432 * the topmost scope's ds_ident pointer will be set to NULL (indicating no
1433 * init_declarator rule was matched) or will point to the identifier to use.
1434 */
1435 dt_node_t *
dt_node_decl(void)1436 dt_node_decl(void)
1437 {
1438 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1439 dt_scope_t *dsp = &yypcb->pcb_dstack;
1440 dt_dclass_t class = dsp->ds_class;
1441 dt_decl_t *ddp = dt_decl_top();
1442
1443 dt_module_t *dmp;
1444 dtrace_typeinfo_t dtt;
1445 ctf_id_t type;
1446
1447 char n1[DT_TYPE_NAMELEN];
1448 char n2[DT_TYPE_NAMELEN];
1449
1450 if (dt_decl_type(ddp, &dtt) != 0)
1451 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1452
1453 /*
1454 * If we have no declaration identifier, then this is either a spurious
1455 * declaration of an intrinsic type (e.g. "extern int;") or declaration
1456 * or redeclaration of a struct, union, or enum type or tag.
1457 */
1458 if (dsp->ds_ident == NULL) {
1459 if (ddp->dd_kind != CTF_K_STRUCT &&
1460 ddp->dd_kind != CTF_K_UNION && ddp->dd_kind != CTF_K_ENUM)
1461 xyerror(D_DECL_USELESS, "useless declaration\n");
1462
1463 dt_dprintf("type %s added as id %ld\n", dt_type_name(
1464 ddp->dd_ctfp, ddp->dd_type, n1, sizeof (n1)), ddp->dd_type);
1465
1466 return (NULL);
1467 }
1468
1469 if (strchr(dsp->ds_ident, '`') != NULL) {
1470 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
1471 "a declaration name (%s)\n", dsp->ds_ident);
1472 }
1473
1474 /*
1475 * If we are nested inside of a C include file, add the declaration to
1476 * the C definition module; otherwise use the D definition module.
1477 */
1478 if (yypcb->pcb_idepth != 0)
1479 dmp = dtp->dt_cdefs;
1480 else
1481 dmp = dtp->dt_ddefs;
1482
1483 /*
1484 * If we see a global or static declaration of a function prototype,
1485 * treat this as equivalent to a D extern declaration.
1486 */
1487 if (ctf_type_kind(dtt.dtt_ctfp, dtt.dtt_type) == CTF_K_FUNCTION &&
1488 (class == DT_DC_DEFAULT || class == DT_DC_STATIC))
1489 class = DT_DC_EXTERN;
1490
1491 switch (class) {
1492 case DT_DC_AUTO:
1493 case DT_DC_REGISTER:
1494 case DT_DC_STATIC:
1495 xyerror(D_DECL_BADCLASS, "specified storage class not "
1496 "appropriate in D\n");
1497 /*NOTREACHED*/
1498
1499 case DT_DC_EXTERN: {
1500 dtrace_typeinfo_t ott;
1501 dtrace_syminfo_t dts;
1502 GElf_Sym sym;
1503
1504 int exists = dtrace_lookup_by_name(dtp,
1505 dmp->dm_name, dsp->ds_ident, &sym, &dts) == 0;
1506
1507 if (exists && (dtrace_symbol_type(dtp, &sym, &dts, &ott) != 0 ||
1508 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1509 ott.dtt_ctfp, ott.dtt_type) != 0)) {
1510 xyerror(D_DECL_IDRED, "identifier redeclared: %s`%s\n"
1511 "\t current: %s\n\tprevious: %s\n",
1512 dmp->dm_name, dsp->ds_ident,
1513 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1514 n1, sizeof (n1)),
1515 dt_type_name(ott.dtt_ctfp, ott.dtt_type,
1516 n2, sizeof (n2)));
1517 } else if (!exists && dt_module_extern(dtp, dmp,
1518 dsp->ds_ident, &dtt) == NULL) {
1519 xyerror(D_UNKNOWN,
1520 "failed to extern %s: %s\n", dsp->ds_ident,
1521 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1522 } else {
1523 dt_dprintf("extern %s`%s type=<%s>\n",
1524 dmp->dm_name, dsp->ds_ident,
1525 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1526 n1, sizeof (n1)));
1527 }
1528 break;
1529 }
1530
1531 case DT_DC_TYPEDEF:
1532 if (dt_idstack_lookup(&yypcb->pcb_globals, dsp->ds_ident)) {
1533 xyerror(D_DECL_IDRED, "global variable identifier "
1534 "redeclared: %s\n", dsp->ds_ident);
1535 }
1536
1537 if (ctf_lookup_by_name(dmp->dm_ctfp,
1538 dsp->ds_ident) != CTF_ERR) {
1539 xyerror(D_DECL_IDRED,
1540 "typedef redeclared: %s\n", dsp->ds_ident);
1541 }
1542
1543 /*
1544 * If the source type for the typedef is not defined in the
1545 * target container or its parent, copy the type to the target
1546 * container and reset dtt_ctfp and dtt_type to the copy.
1547 */
1548 if (dtt.dtt_ctfp != dmp->dm_ctfp &&
1549 dtt.dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
1550
1551 dtt.dtt_type = ctf_add_type(dmp->dm_ctfp,
1552 dtt.dtt_ctfp, dtt.dtt_type);
1553 dtt.dtt_ctfp = dmp->dm_ctfp;
1554
1555 if (dtt.dtt_type == CTF_ERR ||
1556 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
1557 xyerror(D_UNKNOWN, "failed to copy typedef %s "
1558 "source type: %s\n", dsp->ds_ident,
1559 ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1560 }
1561 }
1562
1563 type = ctf_add_typedef(dmp->dm_ctfp,
1564 CTF_ADD_ROOT, dsp->ds_ident, dtt.dtt_type);
1565
1566 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1567 xyerror(D_UNKNOWN, "failed to typedef %s: %s\n",
1568 dsp->ds_ident, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1569 }
1570
1571 dt_dprintf("typedef %s added as id %ld\n", dsp->ds_ident, type);
1572 break;
1573
1574 default: {
1575 ctf_encoding_t cte;
1576 dt_idhash_t *dhp;
1577 dt_ident_t *idp;
1578 dt_node_t idn;
1579 int assc, idkind;
1580 uint_t id, kind;
1581 ushort_t idflags;
1582
1583 switch (class) {
1584 case DT_DC_THIS:
1585 dhp = yypcb->pcb_locals;
1586 idflags = DT_IDFLG_LOCAL;
1587 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1588 break;
1589 case DT_DC_SELF:
1590 dhp = dtp->dt_tls;
1591 idflags = DT_IDFLG_TLS;
1592 idp = dt_idhash_lookup(dhp, dsp->ds_ident);
1593 break;
1594 default:
1595 dhp = dtp->dt_globals;
1596 idflags = 0;
1597 idp = dt_idstack_lookup(
1598 &yypcb->pcb_globals, dsp->ds_ident);
1599 break;
1600 }
1601
1602 if (ddp->dd_kind == CTF_K_ARRAY && ddp->dd_node == NULL) {
1603 xyerror(D_DECL_ARRNULL,
1604 "array declaration requires array dimension or "
1605 "tuple signature: %s\n", dsp->ds_ident);
1606 }
1607
1608 if (idp != NULL && idp->di_gen == 0) {
1609 xyerror(D_DECL_IDRED, "built-in identifier "
1610 "redeclared: %s\n", idp->di_name);
1611 }
1612
1613 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_CDEFS,
1614 dsp->ds_ident, NULL) == 0 ||
1615 dtrace_lookup_by_type(dtp, DTRACE_OBJ_DDEFS,
1616 dsp->ds_ident, NULL) == 0) {
1617 xyerror(D_DECL_IDRED, "typedef identifier "
1618 "redeclared: %s\n", dsp->ds_ident);
1619 }
1620
1621 /*
1622 * Cache some attributes of the decl to make the rest of this
1623 * code simpler: if the decl is an array which is subscripted
1624 * by a type rather than an integer, then it's an associative
1625 * array (assc). We then expect to match either DT_IDENT_ARRAY
1626 * for associative arrays or DT_IDENT_SCALAR for anything else.
1627 */
1628 assc = ddp->dd_kind == CTF_K_ARRAY &&
1629 ddp->dd_node->dn_kind == DT_NODE_TYPE;
1630
1631 idkind = assc ? DT_IDENT_ARRAY : DT_IDENT_SCALAR;
1632
1633 /*
1634 * Create a fake dt_node_t on the stack so we can determine the
1635 * type of any matching identifier by assigning to this node.
1636 * If the pre-existing ident has its di_type set, propagate
1637 * the type by hand so as not to trigger a prototype check for
1638 * arrays (yet); otherwise we use dt_ident_cook() on the ident
1639 * to ensure it is fully initialized before looking at it.
1640 */
1641 bzero(&idn, sizeof (dt_node_t));
1642
1643 if (idp != NULL && idp->di_type != CTF_ERR)
1644 dt_node_type_assign(&idn, idp->di_ctfp, idp->di_type,
1645 B_FALSE);
1646 else if (idp != NULL)
1647 (void) dt_ident_cook(&idn, idp, NULL);
1648
1649 if (assc) {
1650 if (class == DT_DC_THIS) {
1651 xyerror(D_DECL_LOCASSC, "associative arrays "
1652 "may not be declared as local variables:"
1653 " %s\n", dsp->ds_ident);
1654 }
1655
1656 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
1657 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1658 }
1659
1660 if (idp != NULL && (idp->di_kind != idkind ||
1661 ctf_type_cmp(dtt.dtt_ctfp, dtt.dtt_type,
1662 idn.dn_ctfp, idn.dn_type) != 0)) {
1663 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n"
1664 "\t current: %s %s\n\tprevious: %s %s\n",
1665 dsp->ds_ident, dt_idkind_name(idkind),
1666 dt_type_name(dtt.dtt_ctfp,
1667 dtt.dtt_type, n1, sizeof (n1)),
1668 dt_idkind_name(idp->di_kind),
1669 dt_node_type_name(&idn, n2, sizeof (n2)));
1670
1671 } else if (idp != NULL && assc) {
1672 const dt_idsig_t *isp = idp->di_data;
1673 dt_node_t *dnp = ddp->dd_node;
1674 int argc = 0;
1675
1676 for (; dnp != NULL; dnp = dnp->dn_list, argc++) {
1677 const dt_node_t *pnp = &isp->dis_args[argc];
1678
1679 if (argc >= isp->dis_argc)
1680 continue; /* tuple length mismatch */
1681
1682 if (ctf_type_cmp(dnp->dn_ctfp, dnp->dn_type,
1683 pnp->dn_ctfp, pnp->dn_type) == 0)
1684 continue;
1685
1686 xyerror(D_DECL_IDRED,
1687 "identifier redeclared: %s\n"
1688 "\t current: %s, key #%d of type %s\n"
1689 "\tprevious: %s, key #%d of type %s\n",
1690 dsp->ds_ident,
1691 dt_idkind_name(idkind), argc + 1,
1692 dt_node_type_name(dnp, n1, sizeof (n1)),
1693 dt_idkind_name(idp->di_kind), argc + 1,
1694 dt_node_type_name(pnp, n2, sizeof (n2)));
1695 }
1696
1697 if (isp->dis_argc != argc) {
1698 xyerror(D_DECL_IDRED,
1699 "identifier redeclared: %s\n"
1700 "\t current: %s of %s, tuple length %d\n"
1701 "\tprevious: %s of %s, tuple length %d\n",
1702 dsp->ds_ident, dt_idkind_name(idkind),
1703 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1704 n1, sizeof (n1)), argc,
1705 dt_idkind_name(idp->di_kind),
1706 dt_node_type_name(&idn, n2, sizeof (n2)),
1707 isp->dis_argc);
1708 }
1709
1710 } else if (idp == NULL) {
1711 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1712 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1713
1714 switch (kind) {
1715 case CTF_K_INTEGER:
1716 if (ctf_type_encoding(dtt.dtt_ctfp, type,
1717 &cte) == 0 && IS_VOID(cte)) {
1718 xyerror(D_DECL_VOIDOBJ, "cannot have "
1719 "void object: %s\n", dsp->ds_ident);
1720 }
1721 break;
1722 case CTF_K_STRUCT:
1723 case CTF_K_UNION:
1724 if (ctf_type_size(dtt.dtt_ctfp, type) != 0)
1725 break; /* proceed to declaring */
1726 /*FALLTHRU*/
1727 case CTF_K_FORWARD:
1728 xyerror(D_DECL_INCOMPLETE,
1729 "incomplete struct/union/enum %s: %s\n",
1730 dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
1731 n1, sizeof (n1)), dsp->ds_ident);
1732 /*NOTREACHED*/
1733 }
1734
1735 if (dt_idhash_nextid(dhp, &id) == -1) {
1736 xyerror(D_ID_OFLOW, "cannot create %s: limit "
1737 "on number of %s variables exceeded\n",
1738 dsp->ds_ident, dt_idhash_name(dhp));
1739 }
1740
1741 dt_dprintf("declare %s %s variable %s, id=%u\n",
1742 dt_idhash_name(dhp), dt_idkind_name(idkind),
1743 dsp->ds_ident, id);
1744
1745 idp = dt_idhash_insert(dhp, dsp->ds_ident, idkind,
1746 idflags | DT_IDFLG_WRITE | DT_IDFLG_DECL, id,
1747 _dtrace_defattr, 0, assc ? &dt_idops_assc :
1748 &dt_idops_thaw, NULL, dtp->dt_gen);
1749
1750 if (idp == NULL)
1751 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1752
1753 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
1754
1755 /*
1756 * If we are declaring an associative array, use our
1757 * fake parse node to cook the new assoc identifier.
1758 * This will force the ident code to instantiate the
1759 * array type signature corresponding to the list of
1760 * types pointed to by ddp->dd_node. We also reset
1761 * the identifier's attributes based upon the result.
1762 */
1763 if (assc) {
1764 idp->di_attr =
1765 dt_ident_cook(&idn, idp, &ddp->dd_node);
1766 }
1767 }
1768 }
1769
1770 } /* end of switch */
1771
1772 free(dsp->ds_ident);
1773 dsp->ds_ident = NULL;
1774
1775 return (NULL);
1776 }
1777
1778 dt_node_t *
dt_node_func(dt_node_t * dnp,dt_node_t * args)1779 dt_node_func(dt_node_t *dnp, dt_node_t *args)
1780 {
1781 dt_ident_t *idp;
1782
1783 if (dnp->dn_kind != DT_NODE_IDENT) {
1784 xyerror(D_FUNC_IDENT,
1785 "function designator is not of function type\n");
1786 }
1787
1788 idp = dt_idstack_lookup(&yypcb->pcb_globals, dnp->dn_string);
1789
1790 if (idp == NULL) {
1791 xyerror(D_FUNC_UNDEF,
1792 "undefined function name: %s\n", dnp->dn_string);
1793 }
1794
1795 if (idp->di_kind != DT_IDENT_FUNC &&
1796 idp->di_kind != DT_IDENT_AGGFUNC &&
1797 idp->di_kind != DT_IDENT_ACTFUNC) {
1798 xyerror(D_FUNC_IDKIND, "%s '%s' may not be referenced as a "
1799 "function\n", dt_idkind_name(idp->di_kind), idp->di_name);
1800 }
1801
1802 free(dnp->dn_string);
1803 dnp->dn_string = NULL;
1804
1805 dnp->dn_kind = DT_NODE_FUNC;
1806 dnp->dn_flags &= ~DT_NF_COOKED;
1807 dnp->dn_ident = idp;
1808 dnp->dn_args = args;
1809 dnp->dn_list = NULL;
1810
1811 return (dnp);
1812 }
1813
1814 /*
1815 * The offsetof() function is special because it takes a type name as an
1816 * argument. It does not actually construct its own node; after looking up the
1817 * structure or union offset, we just return an integer node with the offset.
1818 */
1819 dt_node_t *
dt_node_offsetof(dt_decl_t * ddp,char * s)1820 dt_node_offsetof(dt_decl_t *ddp, char *s)
1821 {
1822 dtrace_typeinfo_t dtt;
1823 dt_node_t dn;
1824 char *name;
1825 int err;
1826
1827 ctf_membinfo_t ctm;
1828 ctf_id_t type;
1829 uint_t kind;
1830
1831 name = alloca(strlen(s) + 1);
1832 (void) strcpy(name, s);
1833 free(s);
1834
1835 err = dt_decl_type(ddp, &dtt);
1836 dt_decl_free(ddp);
1837
1838 if (err != 0)
1839 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1840
1841 type = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
1842 kind = ctf_type_kind(dtt.dtt_ctfp, type);
1843
1844 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1845 xyerror(D_OFFSETOF_TYPE,
1846 "offsetof operand must be a struct or union type\n");
1847 }
1848
1849 if (ctf_member_info(dtt.dtt_ctfp, type, name, &ctm) == CTF_ERR) {
1850 xyerror(D_UNKNOWN, "failed to determine offset of %s: %s\n",
1851 name, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
1852 }
1853
1854 bzero(&dn, sizeof (dn));
1855 dt_node_type_assign(&dn, dtt.dtt_ctfp, ctm.ctm_type, B_FALSE);
1856
1857 if (dn.dn_flags & DT_NF_BITFIELD) {
1858 xyerror(D_OFFSETOF_BITFIELD,
1859 "cannot take offset of a bit-field: %s\n", name);
1860 }
1861
1862 return (dt_node_int(ctm.ctm_offset / NBBY));
1863 }
1864
1865 dt_node_t *
dt_node_op1(int op,dt_node_t * cp)1866 dt_node_op1(int op, dt_node_t *cp)
1867 {
1868 dt_node_t *dnp;
1869
1870 if (cp->dn_kind == DT_NODE_INT) {
1871 switch (op) {
1872 case DT_TOK_INEG:
1873 /*
1874 * If we're negating an unsigned integer, zero out any
1875 * extra top bits to truncate the value to the size of
1876 * the effective type determined by dt_node_int().
1877 */
1878 cp->dn_value = -cp->dn_value;
1879 if (!(cp->dn_flags & DT_NF_SIGNED)) {
1880 cp->dn_value &= ~0ULL >>
1881 (64 - dt_node_type_size(cp) * NBBY);
1882 }
1883 /*FALLTHRU*/
1884 case DT_TOK_IPOS:
1885 return (cp);
1886 case DT_TOK_BNEG:
1887 cp->dn_value = ~cp->dn_value;
1888 return (cp);
1889 case DT_TOK_LNEG:
1890 cp->dn_value = !cp->dn_value;
1891 return (cp);
1892 }
1893 }
1894
1895 /*
1896 * If sizeof is applied to a type_name or string constant, we can
1897 * transform 'cp' into an integer constant in the node construction
1898 * pass so that it can then be used for arithmetic in this pass.
1899 */
1900 if (op == DT_TOK_SIZEOF &&
1901 (cp->dn_kind == DT_NODE_STRING || cp->dn_kind == DT_NODE_TYPE)) {
1902 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1903 size_t size = dt_node_type_size(cp);
1904
1905 if (size == 0) {
1906 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
1907 "operand of unknown size\n");
1908 }
1909
1910 dt_node_type_assign(cp, dtp->dt_ddefs->dm_ctfp,
1911 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
1912 B_FALSE);
1913
1914 cp->dn_kind = DT_NODE_INT;
1915 cp->dn_op = DT_TOK_INT;
1916 cp->dn_value = size;
1917
1918 return (cp);
1919 }
1920
1921 dnp = dt_node_alloc(DT_NODE_OP1);
1922 assert(op <= USHRT_MAX);
1923 dnp->dn_op = (ushort_t)op;
1924 dnp->dn_child = cp;
1925
1926 return (dnp);
1927 }
1928
1929 /*
1930 * If an integer constant is being cast to another integer type, we can
1931 * perform the cast as part of integer constant folding in this pass. We must
1932 * take action when the integer is being cast to a smaller type or if it is
1933 * changing signed-ness. If so, we first shift rp's bits bits high (losing
1934 * excess bits if narrowing) and then shift them down with either a logical
1935 * shift (unsigned) or arithmetic shift (signed).
1936 */
1937 static void
dt_cast(dt_node_t * lp,dt_node_t * rp)1938 dt_cast(dt_node_t *lp, dt_node_t *rp)
1939 {
1940 size_t srcsize = dt_node_type_size(rp);
1941 size_t dstsize = dt_node_type_size(lp);
1942
1943 if (dstsize < srcsize) {
1944 int n = (sizeof (uint64_t) - dstsize) * NBBY;
1945 rp->dn_value <<= n;
1946 rp->dn_value >>= n;
1947 } else if (dstsize > srcsize) {
1948 int n = (sizeof (uint64_t) - srcsize) * NBBY;
1949 int s = (dstsize - srcsize) * NBBY;
1950
1951 rp->dn_value <<= n;
1952 if (rp->dn_flags & DT_NF_SIGNED) {
1953 rp->dn_value = (intmax_t)rp->dn_value >> s;
1954 rp->dn_value >>= n - s;
1955 } else {
1956 rp->dn_value >>= n;
1957 }
1958 }
1959 }
1960
1961 dt_node_t *
dt_node_op2(int op,dt_node_t * lp,dt_node_t * rp)1962 dt_node_op2(int op, dt_node_t *lp, dt_node_t *rp)
1963 {
1964 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1965 dt_node_t *dnp;
1966
1967 /*
1968 * First we check for operations that are illegal -- namely those that
1969 * might result in integer division by zero, and abort if one is found.
1970 */
1971 if (rp->dn_kind == DT_NODE_INT && rp->dn_value == 0 &&
1972 (op == DT_TOK_MOD || op == DT_TOK_DIV ||
1973 op == DT_TOK_MOD_EQ || op == DT_TOK_DIV_EQ))
1974 xyerror(D_DIV_ZERO, "expression contains division by zero\n");
1975
1976 /*
1977 * If both children are immediate values, we can just perform inline
1978 * calculation and return a new immediate node with the result.
1979 */
1980 if (lp->dn_kind == DT_NODE_INT && rp->dn_kind == DT_NODE_INT) {
1981 uintmax_t l = lp->dn_value;
1982 uintmax_t r = rp->dn_value;
1983
1984 dnp = dt_node_int(0); /* allocate new integer node for result */
1985
1986 switch (op) {
1987 case DT_TOK_LOR:
1988 dnp->dn_value = l || r;
1989 dt_node_type_assign(dnp,
1990 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1991 break;
1992 case DT_TOK_LXOR:
1993 dnp->dn_value = (l != 0) ^ (r != 0);
1994 dt_node_type_assign(dnp,
1995 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
1996 break;
1997 case DT_TOK_LAND:
1998 dnp->dn_value = l && r;
1999 dt_node_type_assign(dnp,
2000 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2001 break;
2002 case DT_TOK_BOR:
2003 dnp->dn_value = l | r;
2004 dt_node_promote(lp, rp, dnp);
2005 break;
2006 case DT_TOK_XOR:
2007 dnp->dn_value = l ^ r;
2008 dt_node_promote(lp, rp, dnp);
2009 break;
2010 case DT_TOK_BAND:
2011 dnp->dn_value = l & r;
2012 dt_node_promote(lp, rp, dnp);
2013 break;
2014 case DT_TOK_EQU:
2015 dnp->dn_value = l == r;
2016 dt_node_type_assign(dnp,
2017 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2018 break;
2019 case DT_TOK_NEQ:
2020 dnp->dn_value = l != r;
2021 dt_node_type_assign(dnp,
2022 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2023 break;
2024 case DT_TOK_LT:
2025 dt_node_promote(lp, rp, dnp);
2026 if (dnp->dn_flags & DT_NF_SIGNED)
2027 dnp->dn_value = (intmax_t)l < (intmax_t)r;
2028 else
2029 dnp->dn_value = l < r;
2030 dt_node_type_assign(dnp,
2031 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2032 break;
2033 case DT_TOK_LE:
2034 dt_node_promote(lp, rp, dnp);
2035 if (dnp->dn_flags & DT_NF_SIGNED)
2036 dnp->dn_value = (intmax_t)l <= (intmax_t)r;
2037 else
2038 dnp->dn_value = l <= r;
2039 dt_node_type_assign(dnp,
2040 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2041 break;
2042 case DT_TOK_GT:
2043 dt_node_promote(lp, rp, dnp);
2044 if (dnp->dn_flags & DT_NF_SIGNED)
2045 dnp->dn_value = (intmax_t)l > (intmax_t)r;
2046 else
2047 dnp->dn_value = l > r;
2048 dt_node_type_assign(dnp,
2049 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2050 break;
2051 case DT_TOK_GE:
2052 dt_node_promote(lp, rp, dnp);
2053 if (dnp->dn_flags & DT_NF_SIGNED)
2054 dnp->dn_value = (intmax_t)l >= (intmax_t)r;
2055 else
2056 dnp->dn_value = l >= r;
2057 dt_node_type_assign(dnp,
2058 DT_INT_CTFP(dtp), DT_INT_TYPE(dtp), B_FALSE);
2059 break;
2060 case DT_TOK_LSH:
2061 dnp->dn_value = l << r;
2062 dt_node_type_propagate(lp, dnp);
2063 dt_node_attr_assign(rp,
2064 dt_attr_min(lp->dn_attr, rp->dn_attr));
2065 break;
2066 case DT_TOK_RSH:
2067 dnp->dn_value = l >> r;
2068 dt_node_type_propagate(lp, dnp);
2069 dt_node_attr_assign(rp,
2070 dt_attr_min(lp->dn_attr, rp->dn_attr));
2071 break;
2072 case DT_TOK_ADD:
2073 dnp->dn_value = l + r;
2074 dt_node_promote(lp, rp, dnp);
2075 break;
2076 case DT_TOK_SUB:
2077 dnp->dn_value = l - r;
2078 dt_node_promote(lp, rp, dnp);
2079 break;
2080 case DT_TOK_MUL:
2081 dnp->dn_value = l * r;
2082 dt_node_promote(lp, rp, dnp);
2083 break;
2084 case DT_TOK_DIV:
2085 dt_node_promote(lp, rp, dnp);
2086 if (dnp->dn_flags & DT_NF_SIGNED)
2087 dnp->dn_value = (intmax_t)l / (intmax_t)r;
2088 else
2089 dnp->dn_value = l / r;
2090 break;
2091 case DT_TOK_MOD:
2092 dt_node_promote(lp, rp, dnp);
2093 if (dnp->dn_flags & DT_NF_SIGNED)
2094 dnp->dn_value = (intmax_t)l % (intmax_t)r;
2095 else
2096 dnp->dn_value = l % r;
2097 break;
2098 default:
2099 dt_node_free(dnp);
2100 dnp = NULL;
2101 }
2102
2103 if (dnp != NULL) {
2104 dt_node_free(lp);
2105 dt_node_free(rp);
2106 return (dnp);
2107 }
2108 }
2109
2110 if (op == DT_TOK_LPAR && rp->dn_kind == DT_NODE_INT &&
2111 dt_node_is_integer(lp)) {
2112 dt_cast(lp, rp);
2113 dt_node_type_propagate(lp, rp);
2114 dt_node_attr_assign(rp, dt_attr_min(lp->dn_attr, rp->dn_attr));
2115 dt_node_free(lp);
2116
2117 return (rp);
2118 }
2119
2120 /*
2121 * If no immediate optimizations are available, create an new OP2 node
2122 * and glue the left and right children into place and return.
2123 */
2124 dnp = dt_node_alloc(DT_NODE_OP2);
2125 assert(op <= USHRT_MAX);
2126 dnp->dn_op = (ushort_t)op;
2127 dnp->dn_left = lp;
2128 dnp->dn_right = rp;
2129
2130 return (dnp);
2131 }
2132
2133 dt_node_t *
dt_node_op3(dt_node_t * expr,dt_node_t * lp,dt_node_t * rp)2134 dt_node_op3(dt_node_t *expr, dt_node_t *lp, dt_node_t *rp)
2135 {
2136 dt_node_t *dnp;
2137
2138 if (expr->dn_kind == DT_NODE_INT)
2139 return (expr->dn_value != 0 ? lp : rp);
2140
2141 dnp = dt_node_alloc(DT_NODE_OP3);
2142 dnp->dn_op = DT_TOK_QUESTION;
2143 dnp->dn_expr = expr;
2144 dnp->dn_left = lp;
2145 dnp->dn_right = rp;
2146
2147 return (dnp);
2148 }
2149
2150 dt_node_t *
dt_node_statement(dt_node_t * expr)2151 dt_node_statement(dt_node_t *expr)
2152 {
2153 dt_node_t *dnp;
2154
2155 if (expr->dn_kind == DT_NODE_AGG)
2156 return (expr);
2157
2158 if (expr->dn_kind == DT_NODE_FUNC &&
2159 expr->dn_ident->di_kind == DT_IDENT_ACTFUNC)
2160 dnp = dt_node_alloc(DT_NODE_DFUNC);
2161 else
2162 dnp = dt_node_alloc(DT_NODE_DEXPR);
2163
2164 dnp->dn_expr = expr;
2165 return (dnp);
2166 }
2167
2168 dt_node_t *
dt_node_if(dt_node_t * pred,dt_node_t * acts,dt_node_t * else_acts)2169 dt_node_if(dt_node_t *pred, dt_node_t *acts, dt_node_t *else_acts)
2170 {
2171 dt_node_t *dnp = dt_node_alloc(DT_NODE_IF);
2172 dnp->dn_conditional = pred;
2173 dnp->dn_body = acts;
2174 dnp->dn_alternate_body = else_acts;
2175
2176 return (dnp);
2177 }
2178
2179 dt_node_t *
dt_node_pdesc_by_name(char * spec)2180 dt_node_pdesc_by_name(char *spec)
2181 {
2182 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2183 dt_node_t *dnp;
2184
2185 if (spec == NULL)
2186 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2187
2188 dnp = dt_node_alloc(DT_NODE_PDESC);
2189 dnp->dn_spec = spec;
2190 dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t));
2191
2192 if (dnp->dn_desc == NULL)
2193 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2194
2195 if (dtrace_xstr2desc(dtp, yypcb->pcb_pspec, dnp->dn_spec,
2196 yypcb->pcb_sargc, yypcb->pcb_sargv, dnp->dn_desc) != 0) {
2197 xyerror(D_PDESC_INVAL, "invalid probe description \"%s\": %s\n",
2198 dnp->dn_spec, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2199 }
2200
2201 free(dnp->dn_spec);
2202 dnp->dn_spec = NULL;
2203
2204 return (dnp);
2205 }
2206
2207 dt_node_t *
dt_node_pdesc_by_id(uintmax_t id)2208 dt_node_pdesc_by_id(uintmax_t id)
2209 {
2210 static const char *const names[] = {
2211 "providers", "modules", "functions"
2212 };
2213
2214 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2215 dt_node_t *dnp = dt_node_alloc(DT_NODE_PDESC);
2216
2217 if ((dnp->dn_desc = malloc(sizeof (dtrace_probedesc_t))) == NULL)
2218 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2219
2220 if (id > UINT_MAX) {
2221 xyerror(D_PDESC_INVAL, "identifier %llu exceeds maximum "
2222 "probe id\n", (u_longlong_t)id);
2223 }
2224
2225 if (yypcb->pcb_pspec != DTRACE_PROBESPEC_NAME) {
2226 xyerror(D_PDESC_INVAL, "probe identifier %llu not permitted "
2227 "when specifying %s\n", (u_longlong_t)id,
2228 names[yypcb->pcb_pspec]);
2229 }
2230
2231 if (dtrace_id2desc(dtp, (dtrace_id_t)id, dnp->dn_desc) != 0) {
2232 xyerror(D_PDESC_INVAL, "invalid probe identifier %llu: %s\n",
2233 (u_longlong_t)id, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2234 }
2235
2236 return (dnp);
2237 }
2238
2239 dt_node_t *
dt_node_clause(dt_node_t * pdescs,dt_node_t * pred,dt_node_t * acts)2240 dt_node_clause(dt_node_t *pdescs, dt_node_t *pred, dt_node_t *acts)
2241 {
2242 dt_node_t *dnp = dt_node_alloc(DT_NODE_CLAUSE);
2243
2244 dnp->dn_pdescs = pdescs;
2245 dnp->dn_pred = pred;
2246 dnp->dn_acts = acts;
2247
2248 return (dnp);
2249 }
2250
2251 dt_node_t *
dt_node_inline(dt_node_t * expr)2252 dt_node_inline(dt_node_t *expr)
2253 {
2254 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2255 dt_scope_t *dsp = &yypcb->pcb_dstack;
2256 dt_decl_t *ddp = dt_decl_top();
2257
2258 char n[DT_TYPE_NAMELEN];
2259 dtrace_typeinfo_t dtt;
2260
2261 dt_ident_t *idp, *rdp;
2262 dt_idnode_t *inp;
2263 dt_node_t *dnp;
2264
2265 if (dt_decl_type(ddp, &dtt) != 0)
2266 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2267
2268 if (dsp->ds_class != DT_DC_DEFAULT) {
2269 xyerror(D_DECL_BADCLASS, "specified storage class not "
2270 "appropriate for inline declaration\n");
2271 }
2272
2273 if (dsp->ds_ident == NULL)
2274 xyerror(D_DECL_USELESS, "inline declaration requires a name\n");
2275
2276 if ((idp = dt_idstack_lookup(
2277 &yypcb->pcb_globals, dsp->ds_ident)) != NULL) {
2278 xyerror(D_DECL_IDRED, "identifier redefined: %s\n\t current: "
2279 "inline definition\n\tprevious: %s %s\n",
2280 idp->di_name, dt_idkind_name(idp->di_kind),
2281 (idp->di_flags & DT_IDFLG_INLINE) ? "inline" : "");
2282 }
2283
2284 /*
2285 * If we are declaring an inlined array, verify that we have a tuple
2286 * signature, and then recompute 'dtt' as the array's value type.
2287 */
2288 if (ddp->dd_kind == CTF_K_ARRAY) {
2289 if (ddp->dd_node == NULL) {
2290 xyerror(D_DECL_ARRNULL, "inline declaration requires "
2291 "array tuple signature: %s\n", dsp->ds_ident);
2292 }
2293
2294 if (ddp->dd_node->dn_kind != DT_NODE_TYPE) {
2295 xyerror(D_DECL_ARRNULL, "inline declaration cannot be "
2296 "of scalar array type: %s\n", dsp->ds_ident);
2297 }
2298
2299 if (dt_decl_type(ddp->dd_next, &dtt) != 0)
2300 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2301 }
2302
2303 /*
2304 * If the inline identifier is not defined, then create it with the
2305 * orphan flag set. We do not insert the identifier into dt_globals
2306 * until we have successfully cooked the right-hand expression, below.
2307 */
2308 dnp = dt_node_alloc(DT_NODE_INLINE);
2309 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
2310 dt_node_attr_assign(dnp, _dtrace_defattr);
2311
2312 if (dt_node_is_void(dnp)) {
2313 xyerror(D_DECL_VOIDOBJ,
2314 "cannot declare void inline: %s\n", dsp->ds_ident);
2315 }
2316
2317 if (ctf_type_kind(dnp->dn_ctfp, ctf_type_resolve(
2318 dnp->dn_ctfp, dnp->dn_type)) == CTF_K_FORWARD) {
2319 xyerror(D_DECL_INCOMPLETE,
2320 "incomplete struct/union/enum %s: %s\n",
2321 dt_node_type_name(dnp, n, sizeof (n)), dsp->ds_ident);
2322 }
2323
2324 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
2325 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2326
2327 bzero(inp, sizeof (dt_idnode_t));
2328
2329 idp = dnp->dn_ident = dt_ident_create(dsp->ds_ident,
2330 ddp->dd_kind == CTF_K_ARRAY ? DT_IDENT_ARRAY : DT_IDENT_SCALAR,
2331 DT_IDFLG_INLINE | DT_IDFLG_REF | DT_IDFLG_DECL | DT_IDFLG_ORPHAN, 0,
2332 _dtrace_defattr, 0, &dt_idops_inline, inp, dtp->dt_gen);
2333
2334 if (idp == NULL) {
2335 free(inp);
2336 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2337 }
2338
2339 /*
2340 * If we're inlining an associative array, create a private identifier
2341 * hash containing the named parameters and store it in inp->din_hash.
2342 * We then push this hash on to the top of the pcb_globals stack.
2343 */
2344 if (ddp->dd_kind == CTF_K_ARRAY) {
2345 dt_idnode_t *pinp;
2346 dt_ident_t *pidp;
2347 dt_node_t *pnp;
2348 uint_t i = 0;
2349
2350 for (pnp = ddp->dd_node; pnp != NULL; pnp = pnp->dn_list)
2351 i++; /* count up parameters for din_argv[] */
2352
2353 inp->din_hash = dt_idhash_create("inline args", NULL, 0, 0);
2354 inp->din_argv = calloc(i, sizeof (dt_ident_t *));
2355
2356 if (inp->din_hash == NULL || inp->din_argv == NULL)
2357 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2358
2359 /*
2360 * Create an identifier for each parameter as a scalar inline,
2361 * and store it in din_hash and in position in din_argv[]. The
2362 * parameter identifiers also use dt_idops_inline, but we leave
2363 * the dt_idnode_t argument 'pinp' zeroed. This will be filled
2364 * in by the code generation pass with references to the args.
2365 */
2366 for (i = 0, pnp = ddp->dd_node;
2367 pnp != NULL; pnp = pnp->dn_list, i++) {
2368
2369 if (pnp->dn_string == NULL)
2370 continue; /* ignore anonymous parameters */
2371
2372 if ((pinp = malloc(sizeof (dt_idnode_t))) == NULL)
2373 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2374
2375 pidp = dt_idhash_insert(inp->din_hash, pnp->dn_string,
2376 DT_IDENT_SCALAR, DT_IDFLG_DECL | DT_IDFLG_INLINE, 0,
2377 _dtrace_defattr, 0, &dt_idops_inline,
2378 pinp, dtp->dt_gen);
2379
2380 if (pidp == NULL) {
2381 free(pinp);
2382 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2383 }
2384
2385 inp->din_argv[i] = pidp;
2386 bzero(pinp, sizeof (dt_idnode_t));
2387 dt_ident_type_assign(pidp, pnp->dn_ctfp, pnp->dn_type);
2388 }
2389
2390 dt_idstack_push(&yypcb->pcb_globals, inp->din_hash);
2391 }
2392
2393 /*
2394 * Unlike most constructors, we need to explicitly cook the right-hand
2395 * side of the inline definition immediately to prevent recursion. If
2396 * the right-hand side uses the inline itself, the cook will fail.
2397 */
2398 expr = dt_node_cook(expr, DT_IDFLG_REF);
2399
2400 if (ddp->dd_kind == CTF_K_ARRAY)
2401 dt_idstack_pop(&yypcb->pcb_globals, inp->din_hash);
2402
2403 /*
2404 * Set the type, attributes, and flags for the inline. If the right-
2405 * hand expression has an identifier, propagate its flags. Then cook
2406 * the identifier to fully initialize it: if we're declaring an inline
2407 * associative array this will construct a type signature from 'ddp'.
2408 */
2409 if (dt_node_is_dynamic(expr))
2410 rdp = dt_ident_resolve(expr->dn_ident);
2411 else if (expr->dn_kind == DT_NODE_VAR || expr->dn_kind == DT_NODE_SYM)
2412 rdp = expr->dn_ident;
2413 else
2414 rdp = NULL;
2415
2416 if (rdp != NULL) {
2417 idp->di_flags |= (rdp->di_flags &
2418 (DT_IDFLG_WRITE | DT_IDFLG_USER | DT_IDFLG_PRIM));
2419 }
2420
2421 idp->di_attr = dt_attr_min(_dtrace_defattr, expr->dn_attr);
2422 dt_ident_type_assign(idp, dtt.dtt_ctfp, dtt.dtt_type);
2423 (void) dt_ident_cook(dnp, idp, &ddp->dd_node);
2424
2425 /*
2426 * Store the parse tree nodes for 'expr' inside of idp->di_data ('inp')
2427 * so that they will be preserved with this identifier. Then pop the
2428 * inline declaration from the declaration stack and restore the lexer.
2429 */
2430 inp->din_list = yypcb->pcb_list;
2431 inp->din_root = expr;
2432
2433 dt_decl_free(dt_decl_pop());
2434 yybegin(YYS_CLAUSE);
2435
2436 /*
2437 * Finally, insert the inline identifier into dt_globals to make it
2438 * visible, and then cook 'dnp' to check its type against 'expr'.
2439 */
2440 dt_idhash_xinsert(dtp->dt_globals, idp);
2441 return (dt_node_cook(dnp, DT_IDFLG_REF));
2442 }
2443
2444 dt_node_t *
dt_node_member(dt_decl_t * ddp,char * name,dt_node_t * expr)2445 dt_node_member(dt_decl_t *ddp, char *name, dt_node_t *expr)
2446 {
2447 dtrace_typeinfo_t dtt;
2448 dt_node_t *dnp;
2449 int err;
2450
2451 if (ddp != NULL) {
2452 err = dt_decl_type(ddp, &dtt);
2453 dt_decl_free(ddp);
2454
2455 if (err != 0)
2456 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2457 }
2458
2459 dnp = dt_node_alloc(DT_NODE_MEMBER);
2460 dnp->dn_membname = name;
2461 dnp->dn_membexpr = expr;
2462
2463 if (ddp != NULL)
2464 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2465 dtt.dtt_flags);
2466
2467 return (dnp);
2468 }
2469
2470 dt_node_t *
dt_node_xlator(dt_decl_t * ddp,dt_decl_t * sdp,char * name,dt_node_t * members)2471 dt_node_xlator(dt_decl_t *ddp, dt_decl_t *sdp, char *name, dt_node_t *members)
2472 {
2473 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2474 dtrace_typeinfo_t src, dst;
2475 dt_node_t sn, dn;
2476 dt_xlator_t *dxp;
2477 dt_node_t *dnp;
2478 int edst, esrc;
2479 uint_t kind;
2480
2481 char n1[DT_TYPE_NAMELEN];
2482 char n2[DT_TYPE_NAMELEN];
2483
2484 edst = dt_decl_type(ddp, &dst);
2485 dt_decl_free(ddp);
2486
2487 esrc = dt_decl_type(sdp, &src);
2488 dt_decl_free(sdp);
2489
2490 if (edst != 0 || esrc != 0) {
2491 free(name);
2492 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2493 }
2494
2495 bzero(&sn, sizeof (sn));
2496 dt_node_type_assign(&sn, src.dtt_ctfp, src.dtt_type, B_FALSE);
2497
2498 bzero(&dn, sizeof (dn));
2499 dt_node_type_assign(&dn, dst.dtt_ctfp, dst.dtt_type, B_FALSE);
2500
2501 if (dt_xlator_lookup(dtp, &sn, &dn, DT_XLATE_EXACT) != NULL) {
2502 xyerror(D_XLATE_REDECL,
2503 "translator from %s to %s has already been declared\n",
2504 dt_node_type_name(&sn, n1, sizeof (n1)),
2505 dt_node_type_name(&dn, n2, sizeof (n2)));
2506 }
2507
2508 kind = ctf_type_kind(dst.dtt_ctfp,
2509 ctf_type_resolve(dst.dtt_ctfp, dst.dtt_type));
2510
2511 if (kind == CTF_K_FORWARD) {
2512 xyerror(D_XLATE_SOU, "incomplete struct/union/enum %s\n",
2513 dt_type_name(dst.dtt_ctfp, dst.dtt_type, n1, sizeof (n1)));
2514 }
2515
2516 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
2517 xyerror(D_XLATE_SOU,
2518 "translator output type must be a struct or union\n");
2519 }
2520
2521 dxp = dt_xlator_create(dtp, &src, &dst, name, members, yypcb->pcb_list);
2522 yybegin(YYS_CLAUSE);
2523 free(name);
2524
2525 if (dxp == NULL)
2526 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2527
2528 dnp = dt_node_alloc(DT_NODE_XLATOR);
2529 dnp->dn_xlator = dxp;
2530 dnp->dn_members = members;
2531
2532 return (dt_node_cook(dnp, DT_IDFLG_REF));
2533 }
2534
2535 dt_node_t *
dt_node_probe(char * s,int protoc,dt_node_t * nargs,dt_node_t * xargs)2536 dt_node_probe(char *s, int protoc, dt_node_t *nargs, dt_node_t *xargs)
2537 {
2538 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2539 int nargc, xargc;
2540 dt_node_t *dnp;
2541
2542 size_t len = strlen(s) + 3; /* +3 for :: and \0 */
2543 char *name = alloca(len);
2544
2545 (void) snprintf(name, len, "::%s", s);
2546 (void) strhyphenate(name);
2547 free(s);
2548
2549 if (strchr(name, '`') != NULL) {
2550 xyerror(D_PROV_BADNAME, "probe name may not "
2551 "contain scoping operator: %s\n", name);
2552 }
2553
2554 if (strlen(name) - 2 >= DTRACE_NAMELEN) {
2555 xyerror(D_PROV_BADNAME, "probe name may not exceed %d "
2556 "characters: %s\n", DTRACE_NAMELEN - 1, name);
2557 }
2558
2559 dnp = dt_node_alloc(DT_NODE_PROBE);
2560
2561 dnp->dn_ident = dt_ident_create(name, DT_IDENT_PROBE,
2562 DT_IDFLG_ORPHAN, DTRACE_IDNONE, _dtrace_defattr, 0,
2563 &dt_idops_probe, NULL, dtp->dt_gen);
2564
2565 nargc = dt_decl_prototype(nargs, nargs,
2566 "probe input", DT_DP_VOID | DT_DP_ANON);
2567
2568 xargc = dt_decl_prototype(xargs, nargs,
2569 "probe output", DT_DP_VOID);
2570
2571 if (nargc > UINT8_MAX) {
2572 xyerror(D_PROV_PRARGLEN, "probe %s input prototype exceeds %u "
2573 "parameters: %d params used\n", name, UINT8_MAX, nargc);
2574 }
2575
2576 if (xargc > UINT8_MAX) {
2577 xyerror(D_PROV_PRARGLEN, "probe %s output prototype exceeds %u "
2578 "parameters: %d params used\n", name, UINT8_MAX, xargc);
2579 }
2580
2581 if (dnp->dn_ident == NULL || dt_probe_create(dtp,
2582 dnp->dn_ident, protoc, nargs, nargc, xargs, xargc) == NULL)
2583 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2584
2585 return (dnp);
2586 }
2587
2588 dt_node_t *
dt_node_provider(char * name,dt_node_t * probes)2589 dt_node_provider(char *name, dt_node_t *probes)
2590 {
2591 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2592 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROVIDER);
2593 dt_node_t *lnp;
2594 size_t len;
2595
2596 dnp->dn_provname = name;
2597 dnp->dn_probes = probes;
2598
2599 if (strchr(name, '`') != NULL) {
2600 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2601 "contain scoping operator: %s\n", name);
2602 }
2603
2604 if ((len = strlen(name)) >= DTRACE_PROVNAMELEN) {
2605 dnerror(dnp, D_PROV_BADNAME, "provider name may not exceed %d "
2606 "characters: %s\n", DTRACE_PROVNAMELEN - 1, name);
2607 }
2608
2609 if (isdigit(name[len - 1])) {
2610 dnerror(dnp, D_PROV_BADNAME, "provider name may not "
2611 "end with a digit: %s\n", name);
2612 }
2613
2614 /*
2615 * Check to see if the provider is already defined or visible through
2616 * dtrace(7D). If so, set dn_provred to treat it as a re-declaration.
2617 * If not, create a new provider and set its interface-only flag. This
2618 * flag may be cleared later by calls made to dt_probe_declare().
2619 */
2620 if ((dnp->dn_provider = dt_provider_lookup(dtp, name)) != NULL)
2621 dnp->dn_provred = B_TRUE;
2622 else if ((dnp->dn_provider = dt_provider_create(dtp, name)) == NULL)
2623 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2624 else
2625 dnp->dn_provider->pv_flags |= DT_PROVIDER_INTF;
2626
2627 /*
2628 * Store all parse nodes created since we consumed the DT_KEY_PROVIDER
2629 * token with the provider and then restore our lexing state to CLAUSE.
2630 * Note that if dnp->dn_provred is true, we may end up storing dups of
2631 * a provider's interface and implementation: we eat this space because
2632 * the implementation will likely need to redeclare probe members, and
2633 * therefore may result in those member nodes becoming persistent.
2634 */
2635 for (lnp = yypcb->pcb_list; lnp->dn_link != NULL; lnp = lnp->dn_link)
2636 continue; /* skip to end of allocation list */
2637
2638 lnp->dn_link = dnp->dn_provider->pv_nodes;
2639 dnp->dn_provider->pv_nodes = yypcb->pcb_list;
2640
2641 yybegin(YYS_CLAUSE);
2642 return (dnp);
2643 }
2644
2645 dt_node_t *
dt_node_program(dt_node_t * lnp)2646 dt_node_program(dt_node_t *lnp)
2647 {
2648 dt_node_t *dnp = dt_node_alloc(DT_NODE_PROG);
2649 dnp->dn_list = lnp;
2650 return (dnp);
2651 }
2652
2653 /*
2654 * This function provides the underlying implementation of cooking an
2655 * identifier given its node, a hash of dynamic identifiers, an identifier
2656 * kind, and a boolean flag indicating whether we are allowed to instantiate
2657 * a new identifier if the string is not found. This function is either
2658 * called from dt_cook_ident(), below, or directly by the various cooking
2659 * routines that are allowed to instantiate identifiers (e.g. op2 TOK_ASGN).
2660 */
2661 static void
dt_xcook_ident(dt_node_t * dnp,dt_idhash_t * dhp,uint_t idkind,int create)2662 dt_xcook_ident(dt_node_t *dnp, dt_idhash_t *dhp, uint_t idkind, int create)
2663 {
2664 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2665 const char *sname = dt_idhash_name(dhp);
2666 int uref = 0;
2667
2668 dtrace_attribute_t attr = _dtrace_defattr;
2669 dt_ident_t *idp;
2670 dtrace_syminfo_t dts;
2671 GElf_Sym sym;
2672
2673 const char *scope, *mark;
2674 uchar_t dnkind;
2675 char *name;
2676
2677 /*
2678 * Look for scoping marks in the identifier. If one is found, set our
2679 * scope to either DTRACE_OBJ_KMODS or UMODS or to the first part of
2680 * the string that specifies the scope using an explicit module name.
2681 * If two marks in a row are found, set 'uref' (user symbol reference).
2682 * Otherwise we set scope to DTRACE_OBJ_EXEC, indicating that normal
2683 * scope is desired and we should search the specified idhash.
2684 */
2685 if ((name = strrchr(dnp->dn_string, '`')) != NULL) {
2686 if (name > dnp->dn_string && name[-1] == '`') {
2687 uref++;
2688 name[-1] = '\0';
2689 }
2690
2691 if (name == dnp->dn_string + uref)
2692 scope = uref ? DTRACE_OBJ_UMODS : DTRACE_OBJ_KMODS;
2693 else
2694 scope = dnp->dn_string;
2695
2696 *name++ = '\0'; /* leave name pointing after scoping mark */
2697 dnkind = DT_NODE_VAR;
2698
2699 } else if (idkind == DT_IDENT_AGG) {
2700 scope = DTRACE_OBJ_EXEC;
2701 name = dnp->dn_string + 1;
2702 dnkind = DT_NODE_AGG;
2703 } else {
2704 scope = DTRACE_OBJ_EXEC;
2705 name = dnp->dn_string;
2706 dnkind = DT_NODE_VAR;
2707 }
2708
2709 /*
2710 * If create is set to false, and we fail our idhash lookup, preset
2711 * the errno code to EDT_NOVAR for our final error message below.
2712 * If we end up calling dtrace_lookup_by_name(), it will reset the
2713 * errno appropriately and that error will be reported instead.
2714 */
2715 (void) dt_set_errno(dtp, EDT_NOVAR);
2716 mark = uref ? "``" : "`";
2717
2718 if (scope == DTRACE_OBJ_EXEC && (
2719 (dhp != dtp->dt_globals &&
2720 (idp = dt_idhash_lookup(dhp, name)) != NULL) ||
2721 (dhp == dtp->dt_globals &&
2722 (idp = dt_idstack_lookup(&yypcb->pcb_globals, name)) != NULL))) {
2723 /*
2724 * Check that we are referencing the ident in the manner that
2725 * matches its type if this is a global lookup. In the TLS or
2726 * local case, we don't know how the ident will be used until
2727 * the time operator -> is seen; more parsing is needed.
2728 */
2729 if (idp->di_kind != idkind && dhp == dtp->dt_globals) {
2730 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
2731 "as %s\n", dt_idkind_name(idp->di_kind),
2732 idp->di_name, dt_idkind_name(idkind));
2733 }
2734
2735 /*
2736 * Arrays and aggregations are not cooked individually. They
2737 * have dynamic types and must be referenced using operator [].
2738 * This is handled explicitly by the code for DT_TOK_LBRAC.
2739 */
2740 if (idp->di_kind != DT_IDENT_ARRAY &&
2741 idp->di_kind != DT_IDENT_AGG)
2742 attr = dt_ident_cook(dnp, idp, NULL);
2743 else {
2744 dt_node_type_assign(dnp,
2745 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2746 attr = idp->di_attr;
2747 }
2748
2749 free(dnp->dn_string);
2750 dnp->dn_string = NULL;
2751 dnp->dn_kind = dnkind;
2752 dnp->dn_ident = idp;
2753 dnp->dn_flags |= DT_NF_LVALUE;
2754
2755 if (idp->di_flags & DT_IDFLG_WRITE)
2756 dnp->dn_flags |= DT_NF_WRITABLE;
2757
2758 dt_node_attr_assign(dnp, attr);
2759
2760 } else if (dhp == dtp->dt_globals && scope != DTRACE_OBJ_EXEC &&
2761 dtrace_lookup_by_name(dtp, scope, name, &sym, &dts) == 0) {
2762
2763 dt_module_t *mp = dt_module_lookup_by_name(dtp, dts.dts_object);
2764 int umod = (mp->dm_flags & DT_DM_KERNEL) == 0;
2765 static const char *const kunames[] = { "kernel", "user" };
2766
2767 dtrace_typeinfo_t dtt;
2768 dtrace_syminfo_t *sip;
2769
2770 if (uref ^ umod) {
2771 xyerror(D_SYM_BADREF, "%s module '%s' symbol '%s' may "
2772 "not be referenced as a %s symbol\n", kunames[umod],
2773 dts.dts_object, dts.dts_name, kunames[uref]);
2774 }
2775
2776 if (dtrace_symbol_type(dtp, &sym, &dts, &dtt) != 0) {
2777 /*
2778 * For now, we special-case EDT_DATAMODEL to clarify
2779 * that mixed data models are not currently supported.
2780 */
2781 if (dtp->dt_errno == EDT_DATAMODEL) {
2782 xyerror(D_SYM_MODEL, "cannot use %s symbol "
2783 "%s%s%s in a %s D program\n",
2784 dt_module_modelname(mp),
2785 dts.dts_object, mark, dts.dts_name,
2786 dt_module_modelname(dtp->dt_ddefs));
2787 }
2788
2789 xyerror(D_SYM_NOTYPES,
2790 "no symbolic type information is available for "
2791 "%s%s%s: %s\n", dts.dts_object, mark, dts.dts_name,
2792 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2793 }
2794
2795 idp = dt_ident_create(name, DT_IDENT_SYMBOL, 0, 0,
2796 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
2797
2798 if (idp == NULL)
2799 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2800
2801 if (mp->dm_flags & DT_DM_PRIMARY)
2802 idp->di_flags |= DT_IDFLG_PRIM;
2803
2804 idp->di_next = dtp->dt_externs;
2805 dtp->dt_externs = idp;
2806
2807 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL)
2808 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2809
2810 bcopy(&dts, sip, sizeof (dtrace_syminfo_t));
2811 idp->di_data = sip;
2812 idp->di_ctfp = dtt.dtt_ctfp;
2813 idp->di_type = dtt.dtt_type;
2814
2815 free(dnp->dn_string);
2816 dnp->dn_string = NULL;
2817 dnp->dn_kind = DT_NODE_SYM;
2818 dnp->dn_ident = idp;
2819 dnp->dn_flags |= DT_NF_LVALUE;
2820
2821 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
2822 dtt.dtt_flags);
2823 dt_node_attr_assign(dnp, _dtrace_symattr);
2824
2825 if (uref) {
2826 idp->di_flags |= DT_IDFLG_USER;
2827 dnp->dn_flags |= DT_NF_USERLAND;
2828 }
2829
2830 } else if (scope == DTRACE_OBJ_EXEC && create == B_TRUE) {
2831 uint_t flags = DT_IDFLG_WRITE;
2832 uint_t id;
2833
2834 if (dt_idhash_nextid(dhp, &id) == -1) {
2835 xyerror(D_ID_OFLOW, "cannot create %s: limit on number "
2836 "of %s variables exceeded\n", name, sname);
2837 }
2838
2839 if (dhp == yypcb->pcb_locals)
2840 flags |= DT_IDFLG_LOCAL;
2841 else if (dhp == dtp->dt_tls)
2842 flags |= DT_IDFLG_TLS;
2843
2844 dt_dprintf("create %s %s variable %s, id=%u\n",
2845 sname, dt_idkind_name(idkind), name, id);
2846
2847 if (idkind == DT_IDENT_ARRAY || idkind == DT_IDENT_AGG) {
2848 idp = dt_idhash_insert(dhp, name,
2849 idkind, flags, id, _dtrace_defattr, 0,
2850 &dt_idops_assc, NULL, dtp->dt_gen);
2851 } else {
2852 idp = dt_idhash_insert(dhp, name,
2853 idkind, flags, id, _dtrace_defattr, 0,
2854 &dt_idops_thaw, NULL, dtp->dt_gen);
2855 }
2856
2857 if (idp == NULL)
2858 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2859
2860 /*
2861 * Arrays and aggregations are not cooked individually. They
2862 * have dynamic types and must be referenced using operator [].
2863 * This is handled explicitly by the code for DT_TOK_LBRAC.
2864 */
2865 if (idp->di_kind != DT_IDENT_ARRAY &&
2866 idp->di_kind != DT_IDENT_AGG)
2867 attr = dt_ident_cook(dnp, idp, NULL);
2868 else {
2869 dt_node_type_assign(dnp,
2870 DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
2871 attr = idp->di_attr;
2872 }
2873
2874 free(dnp->dn_string);
2875 dnp->dn_string = NULL;
2876 dnp->dn_kind = dnkind;
2877 dnp->dn_ident = idp;
2878 dnp->dn_flags |= DT_NF_LVALUE | DT_NF_WRITABLE;
2879
2880 dt_node_attr_assign(dnp, attr);
2881
2882 } else if (scope != DTRACE_OBJ_EXEC) {
2883 xyerror(D_IDENT_UNDEF, "failed to resolve %s%s%s: %s\n",
2884 dnp->dn_string, mark, name,
2885 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2886 } else {
2887 xyerror(D_IDENT_UNDEF, "failed to resolve %s: %s\n",
2888 dnp->dn_string, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2889 }
2890 }
2891
2892 static dt_node_t *
dt_cook_ident(dt_node_t * dnp,uint_t idflags)2893 dt_cook_ident(dt_node_t *dnp, uint_t idflags)
2894 {
2895 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2896
2897 if (dnp->dn_op == DT_TOK_AGG)
2898 dt_xcook_ident(dnp, dtp->dt_aggs, DT_IDENT_AGG, B_FALSE);
2899 else
2900 dt_xcook_ident(dnp, dtp->dt_globals, DT_IDENT_SCALAR, B_FALSE);
2901
2902 return (dt_node_cook(dnp, idflags));
2903 }
2904
2905 /*
2906 * Since operators [ and -> can instantiate new variables before we know
2907 * whether the reference is for a read or a write, we need to check read
2908 * references to determine if the identifier is currently dt_ident_unref().
2909 * If so, we report that this first access was to an undefined variable.
2910 */
2911 static dt_node_t *
dt_cook_var(dt_node_t * dnp,uint_t idflags)2912 dt_cook_var(dt_node_t *dnp, uint_t idflags)
2913 {
2914 dt_ident_t *idp = dnp->dn_ident;
2915
2916 if ((idflags & DT_IDFLG_REF) && dt_ident_unref(idp)) {
2917 dnerror(dnp, D_VAR_UNDEF,
2918 "%s%s has not yet been declared or assigned\n",
2919 (idp->di_flags & DT_IDFLG_LOCAL) ? "this->" :
2920 (idp->di_flags & DT_IDFLG_TLS) ? "self->" : "",
2921 idp->di_name);
2922 }
2923
2924 dt_node_attr_assign(dnp, dt_ident_cook(dnp, idp, &dnp->dn_args));
2925 return (dnp);
2926 }
2927
2928 /*ARGSUSED*/
2929 static dt_node_t *
dt_cook_func(dt_node_t * dnp,uint_t idflags)2930 dt_cook_func(dt_node_t *dnp, uint_t idflags)
2931 {
2932 dt_node_attr_assign(dnp,
2933 dt_ident_cook(dnp, dnp->dn_ident, &dnp->dn_args));
2934
2935 return (dnp);
2936 }
2937
2938 static dt_node_t *
dt_cook_op1(dt_node_t * dnp,uint_t idflags)2939 dt_cook_op1(dt_node_t *dnp, uint_t idflags)
2940 {
2941 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2942 dt_node_t *cp = dnp->dn_child;
2943
2944 char n[DT_TYPE_NAMELEN];
2945 dtrace_typeinfo_t dtt;
2946 dt_ident_t *idp;
2947
2948 ctf_encoding_t e;
2949 ctf_arinfo_t r;
2950 ctf_id_t type, base;
2951 uint_t kind;
2952
2953 if (dnp->dn_op == DT_TOK_PREINC || dnp->dn_op == DT_TOK_POSTINC ||
2954 dnp->dn_op == DT_TOK_PREDEC || dnp->dn_op == DT_TOK_POSTDEC)
2955 idflags = DT_IDFLG_REF | DT_IDFLG_MOD;
2956 else
2957 idflags = DT_IDFLG_REF;
2958
2959 /*
2960 * We allow the unary ++ and -- operators to instantiate new scalar
2961 * variables if applied to an identifier; otherwise just cook as usual.
2962 */
2963 if (cp->dn_kind == DT_NODE_IDENT && (idflags & DT_IDFLG_MOD))
2964 dt_xcook_ident(cp, dtp->dt_globals, DT_IDENT_SCALAR, B_TRUE);
2965
2966 cp = dnp->dn_child = dt_node_cook(cp, 0); /* don't set idflags yet */
2967
2968 if (cp->dn_kind == DT_NODE_VAR && dt_ident_unref(cp->dn_ident)) {
2969 if (dt_type_lookup("int64_t", &dtt) != 0)
2970 xyerror(D_TYPE_ERR, "failed to lookup int64_t\n");
2971
2972 dt_ident_type_assign(cp->dn_ident, dtt.dtt_ctfp, dtt.dtt_type);
2973 dt_node_type_assign(cp, dtt.dtt_ctfp, dtt.dtt_type,
2974 dtt.dtt_flags);
2975 }
2976
2977 if (cp->dn_kind == DT_NODE_VAR)
2978 cp->dn_ident->di_flags |= idflags;
2979
2980 switch (dnp->dn_op) {
2981 case DT_TOK_DEREF:
2982 /*
2983 * If the deref operator is applied to a translated pointer,
2984 * we set our output type to the output of the translation.
2985 */
2986 if ((idp = dt_node_resolve(cp, DT_IDENT_XLPTR)) != NULL) {
2987 dt_xlator_t *dxp = idp->di_data;
2988
2989 dnp->dn_ident = &dxp->dx_souid;
2990 dt_node_type_assign(dnp,
2991 dnp->dn_ident->di_ctfp, dnp->dn_ident->di_type,
2992 cp->dn_flags & DT_NF_USERLAND);
2993 break;
2994 }
2995
2996 type = ctf_type_resolve(cp->dn_ctfp, cp->dn_type);
2997 kind = ctf_type_kind(cp->dn_ctfp, type);
2998
2999 if (kind == CTF_K_ARRAY) {
3000 if (ctf_array_info(cp->dn_ctfp, type, &r) != 0) {
3001 dtp->dt_ctferr = ctf_errno(cp->dn_ctfp);
3002 longjmp(yypcb->pcb_jmpbuf, EDT_CTF);
3003 } else
3004 type = r.ctr_contents;
3005 } else if (kind == CTF_K_POINTER) {
3006 type = ctf_type_reference(cp->dn_ctfp, type);
3007 } else {
3008 xyerror(D_DEREF_NONPTR,
3009 "cannot dereference non-pointer type\n");
3010 }
3011
3012 dt_node_type_assign(dnp, cp->dn_ctfp, type,
3013 cp->dn_flags & DT_NF_USERLAND);
3014 base = ctf_type_resolve(cp->dn_ctfp, type);
3015 kind = ctf_type_kind(cp->dn_ctfp, base);
3016
3017 if (kind == CTF_K_INTEGER && ctf_type_encoding(cp->dn_ctfp,
3018 base, &e) == 0 && IS_VOID(e)) {
3019 xyerror(D_DEREF_VOID,
3020 "cannot dereference pointer to void\n");
3021 }
3022
3023 if (kind == CTF_K_FUNCTION) {
3024 xyerror(D_DEREF_FUNC,
3025 "cannot dereference pointer to function\n");
3026 }
3027
3028 if (kind != CTF_K_ARRAY || dt_node_is_string(dnp))
3029 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.4.3] */
3030
3031 /*
3032 * If we propagated the l-value bit and the child operand was
3033 * a writable D variable or a binary operation of the form
3034 * a + b where a is writable, then propagate the writable bit.
3035 * This is necessary to permit assignments to scalar arrays,
3036 * which are converted to expressions of the form *(a + i).
3037 */
3038 if ((cp->dn_flags & DT_NF_WRITABLE) ||
3039 (cp->dn_kind == DT_NODE_OP2 && cp->dn_op == DT_TOK_ADD &&
3040 (cp->dn_left->dn_flags & DT_NF_WRITABLE)))
3041 dnp->dn_flags |= DT_NF_WRITABLE;
3042
3043 if ((cp->dn_flags & DT_NF_USERLAND) &&
3044 (kind == CTF_K_POINTER || (dnp->dn_flags & DT_NF_REF)))
3045 dnp->dn_flags |= DT_NF_USERLAND;
3046 break;
3047
3048 case DT_TOK_IPOS:
3049 case DT_TOK_INEG:
3050 if (!dt_node_is_arith(cp)) {
3051 xyerror(D_OP_ARITH, "operator %s requires an operand "
3052 "of arithmetic type\n", opstr(dnp->dn_op));
3053 }
3054 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3055 break;
3056
3057 case DT_TOK_BNEG:
3058 if (!dt_node_is_integer(cp)) {
3059 xyerror(D_OP_INT, "operator %s requires an operand of "
3060 "integral type\n", opstr(dnp->dn_op));
3061 }
3062 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.4-6] */
3063 break;
3064
3065 case DT_TOK_LNEG:
3066 if (!dt_node_is_scalar(cp)) {
3067 xyerror(D_OP_SCALAR, "operator %s requires an operand "
3068 "of scalar type\n", opstr(dnp->dn_op));
3069 }
3070 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3071 B_FALSE);
3072 break;
3073
3074 case DT_TOK_ADDROF:
3075 if (cp->dn_kind == DT_NODE_VAR || cp->dn_kind == DT_NODE_AGG) {
3076 xyerror(D_ADDROF_VAR,
3077 "cannot take address of dynamic variable\n");
3078 }
3079
3080 if (dt_node_is_dynamic(cp)) {
3081 xyerror(D_ADDROF_VAR,
3082 "cannot take address of dynamic object\n");
3083 }
3084
3085 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3086 xyerror(D_ADDROF_LVAL, /* see K&R[A7.4.2] */
3087 "unacceptable operand for unary & operator\n");
3088 }
3089
3090 if (cp->dn_flags & DT_NF_BITFIELD) {
3091 xyerror(D_ADDROF_BITFIELD,
3092 "cannot take address of bit-field\n");
3093 }
3094
3095 dtt = (dtrace_typeinfo_t){
3096 .dtt_ctfp = cp->dn_ctfp,
3097 .dtt_type = cp->dn_type,
3098 };
3099
3100 if (dt_type_pointer(&dtt) == -1) {
3101 xyerror(D_TYPE_ERR, "cannot find type for \"&\": %s*\n",
3102 dt_node_type_name(cp, n, sizeof (n)));
3103 }
3104
3105 dt_node_type_assign(dnp, dtt.dtt_ctfp, dtt.dtt_type,
3106 cp->dn_flags & DT_NF_USERLAND);
3107 break;
3108
3109 case DT_TOK_SIZEOF:
3110 if (cp->dn_flags & DT_NF_BITFIELD) {
3111 xyerror(D_SIZEOF_BITFIELD,
3112 "cannot apply sizeof to a bit-field\n");
3113 }
3114
3115 if (dt_node_sizeof(cp) == 0) {
3116 xyerror(D_SIZEOF_TYPE, "cannot apply sizeof to an "
3117 "operand of unknown size\n");
3118 }
3119
3120 dt_node_type_assign(dnp, dtp->dt_ddefs->dm_ctfp,
3121 ctf_lookup_by_name(dtp->dt_ddefs->dm_ctfp, "size_t"),
3122 B_FALSE);
3123 break;
3124
3125 case DT_TOK_STRINGOF:
3126 if (!dt_node_is_scalar(cp) && !dt_node_is_pointer(cp) &&
3127 !dt_node_is_strcompat(cp)) {
3128 xyerror(D_STRINGOF_TYPE,
3129 "cannot apply stringof to a value of type %s\n",
3130 dt_node_type_name(cp, n, sizeof (n)));
3131 }
3132 dt_node_type_assign(dnp, DT_STR_CTFP(dtp), DT_STR_TYPE(dtp),
3133 cp->dn_flags & DT_NF_USERLAND);
3134 break;
3135
3136 case DT_TOK_PREINC:
3137 case DT_TOK_POSTINC:
3138 case DT_TOK_PREDEC:
3139 case DT_TOK_POSTDEC:
3140 if (dt_node_is_scalar(cp) == 0) {
3141 xyerror(D_OP_SCALAR, "operator %s requires operand of "
3142 "scalar type\n", opstr(dnp->dn_op));
3143 }
3144
3145 if (dt_node_is_vfptr(cp)) {
3146 xyerror(D_OP_VFPTR, "operator %s requires an operand "
3147 "of known size\n", opstr(dnp->dn_op));
3148 }
3149
3150 if (!(cp->dn_flags & DT_NF_LVALUE)) {
3151 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3152 "lvalue as an operand\n", opstr(dnp->dn_op));
3153 }
3154
3155 if (!(cp->dn_flags & DT_NF_WRITABLE)) {
3156 xyerror(D_OP_WRITE, "operator %s can only be applied "
3157 "to a writable variable\n", opstr(dnp->dn_op));
3158 }
3159
3160 dt_node_type_propagate(cp, dnp); /* see K&R[A7.4.1] */
3161 break;
3162
3163 default:
3164 xyerror(D_UNKNOWN, "invalid unary op %s\n", opstr(dnp->dn_op));
3165 }
3166
3167 dt_node_attr_assign(dnp, cp->dn_attr);
3168 return (dnp);
3169 }
3170
3171 static void
dt_assign_common(dt_node_t * dnp)3172 dt_assign_common(dt_node_t *dnp)
3173 {
3174 dt_node_t *lp = dnp->dn_left;
3175 dt_node_t *rp = dnp->dn_right;
3176 int op = dnp->dn_op;
3177
3178 if (rp->dn_kind == DT_NODE_INT)
3179 dt_cast(lp, rp);
3180
3181 if (!(lp->dn_flags & DT_NF_LVALUE)) {
3182 xyerror(D_OP_LVAL, "operator %s requires modifiable "
3183 "lvalue as an operand\n", opstr(op));
3184 /* see K&R[A7.17] */
3185 }
3186
3187 if (!(lp->dn_flags & DT_NF_WRITABLE)) {
3188 xyerror(D_OP_WRITE, "operator %s can only be applied "
3189 "to a writable variable\n", opstr(op));
3190 }
3191
3192 dt_node_type_propagate(lp, dnp); /* see K&R[A7.17] */
3193 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3194 }
3195
3196 static dt_node_t *
dt_cook_op2(dt_node_t * dnp,uint_t idflags)3197 dt_cook_op2(dt_node_t *dnp, uint_t idflags)
3198 {
3199 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3200 dt_node_t *lp = dnp->dn_left;
3201 dt_node_t *rp = dnp->dn_right;
3202 int op = dnp->dn_op;
3203
3204 ctf_membinfo_t m;
3205 ctf_file_t *ctfp;
3206 ctf_id_t type;
3207 int kind, val, uref;
3208 dt_ident_t *idp;
3209
3210 char n1[DT_TYPE_NAMELEN];
3211 char n2[DT_TYPE_NAMELEN];
3212
3213 /*
3214 * The expression E1[E2] is identical by definition to *((E1)+(E2)) so
3215 * we convert "[" to "+" and glue on "*" at the end (see K&R[A7.3.1])
3216 * unless the left-hand side is an untyped D scalar, associative array,
3217 * or aggregation. In these cases, we proceed to case DT_TOK_LBRAC and
3218 * handle associative array and aggregation references there.
3219 */
3220 if (op == DT_TOK_LBRAC) {
3221 if (lp->dn_kind == DT_NODE_IDENT) {
3222 dt_idhash_t *dhp;
3223 uint_t idkind;
3224
3225 if (lp->dn_op == DT_TOK_AGG) {
3226 dhp = dtp->dt_aggs;
3227 idp = dt_idhash_lookup(dhp, lp->dn_string + 1);
3228 idkind = DT_IDENT_AGG;
3229 } else {
3230 dhp = dtp->dt_globals;
3231 idp = dt_idstack_lookup(
3232 &yypcb->pcb_globals, lp->dn_string);
3233 idkind = DT_IDENT_ARRAY;
3234 }
3235
3236 if (idp == NULL || dt_ident_unref(idp))
3237 dt_xcook_ident(lp, dhp, idkind, B_TRUE);
3238 else
3239 dt_xcook_ident(lp, dhp, idp->di_kind, B_FALSE);
3240 } else {
3241 lp = dnp->dn_left = dt_node_cook(lp, 0);
3242 }
3243
3244 /*
3245 * Switch op to '+' for *(E1 + E2) array mode in these cases:
3246 * (a) lp is a DT_IDENT_ARRAY variable that has already been
3247 * referenced using [] notation (dn_args != NULL).
3248 * (b) lp is a non-ARRAY variable that has already been given
3249 * a type by assignment or declaration (!dt_ident_unref())
3250 * (c) lp is neither a variable nor an aggregation
3251 */
3252 if (lp->dn_kind == DT_NODE_VAR) {
3253 if (lp->dn_ident->di_kind == DT_IDENT_ARRAY) {
3254 if (lp->dn_args != NULL)
3255 op = DT_TOK_ADD;
3256 } else if (!dt_ident_unref(lp->dn_ident)) {
3257 op = DT_TOK_ADD;
3258 }
3259 } else if (lp->dn_kind != DT_NODE_AGG) {
3260 op = DT_TOK_ADD;
3261 }
3262 }
3263
3264 switch (op) {
3265 case DT_TOK_BAND:
3266 case DT_TOK_XOR:
3267 case DT_TOK_BOR:
3268 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3269 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3270
3271 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3272 xyerror(D_OP_INT, "operator %s requires operands of "
3273 "integral type\n", opstr(op));
3274 }
3275
3276 dt_node_promote(lp, rp, dnp); /* see K&R[A7.11-13] */
3277 break;
3278
3279 case DT_TOK_LSH:
3280 case DT_TOK_RSH:
3281 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3282 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3283
3284 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3285 xyerror(D_OP_INT, "operator %s requires operands of "
3286 "integral type\n", opstr(op));
3287 }
3288
3289 dt_node_type_propagate(lp, dnp); /* see K&R[A7.8] */
3290 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3291 break;
3292
3293 case DT_TOK_MOD:
3294 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3295 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3296
3297 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3298 xyerror(D_OP_INT, "operator %s requires operands of "
3299 "integral type\n", opstr(op));
3300 }
3301
3302 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3303 break;
3304
3305 case DT_TOK_MUL:
3306 case DT_TOK_DIV:
3307 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3308 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3309
3310 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3311 xyerror(D_OP_ARITH, "operator %s requires operands of "
3312 "arithmetic type\n", opstr(op));
3313 }
3314
3315 dt_node_promote(lp, rp, dnp); /* see K&R[A7.6] */
3316 break;
3317
3318 case DT_TOK_LAND:
3319 case DT_TOK_LXOR:
3320 case DT_TOK_LOR:
3321 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3322 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3323
3324 if (!dt_node_is_scalar(lp) || !dt_node_is_scalar(rp)) {
3325 xyerror(D_OP_SCALAR, "operator %s requires operands "
3326 "of scalar type\n", opstr(op));
3327 }
3328
3329 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3330 B_FALSE);
3331 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3332 break;
3333
3334 case DT_TOK_LT:
3335 case DT_TOK_LE:
3336 case DT_TOK_GT:
3337 case DT_TOK_GE:
3338 case DT_TOK_EQU:
3339 case DT_TOK_NEQ:
3340 /*
3341 * The D comparison operators provide the ability to transform
3342 * a right-hand identifier into a corresponding enum tag value
3343 * if the left-hand side is an enum type. To do this, we cook
3344 * the left-hand side, and then see if the right-hand side is
3345 * an unscoped identifier defined in the enum. If so, we
3346 * convert into an integer constant node with the tag's value.
3347 */
3348 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3349
3350 kind = ctf_type_kind(lp->dn_ctfp,
3351 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3352
3353 if (kind == CTF_K_ENUM && rp->dn_kind == DT_NODE_IDENT &&
3354 strchr(rp->dn_string, '`') == NULL && ctf_enum_value(
3355 lp->dn_ctfp, lp->dn_type, rp->dn_string, &val) == 0) {
3356
3357 if ((idp = dt_idstack_lookup(&yypcb->pcb_globals,
3358 rp->dn_string)) != NULL) {
3359 xyerror(D_IDENT_AMBIG,
3360 "ambiguous use of operator %s: %s is "
3361 "both a %s enum tag and a global %s\n",
3362 opstr(op), rp->dn_string,
3363 dt_node_type_name(lp, n1, sizeof (n1)),
3364 dt_idkind_name(idp->di_kind));
3365 }
3366
3367 free(rp->dn_string);
3368 rp->dn_string = NULL;
3369 rp->dn_kind = DT_NODE_INT;
3370 rp->dn_flags |= DT_NF_COOKED;
3371 rp->dn_op = DT_TOK_INT;
3372 rp->dn_value = (intmax_t)val;
3373
3374 dt_node_type_assign(rp, lp->dn_ctfp, lp->dn_type,
3375 B_FALSE);
3376 dt_node_attr_assign(rp, _dtrace_symattr);
3377 }
3378
3379 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3380
3381 /*
3382 * The rules for type checking for the relational operators are
3383 * described in the ANSI-C spec (see K&R[A7.9-10]). We perform
3384 * the various tests in order from least to most expensive. We
3385 * also allow derived strings to be compared as a first-class
3386 * type (resulting in a strcmp(3C)-style comparison), and we
3387 * slightly relax the A7.9 rules to permit void pointer
3388 * comparisons as in A7.10. Our users won't be confused by
3389 * this since they understand pointers are just numbers, and
3390 * relaxing this constraint simplifies the implementation.
3391 */
3392 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3393 rp->dn_ctfp, rp->dn_type))
3394 /*EMPTY*/;
3395 else if (dt_node_is_integer(lp) && dt_node_is_integer(rp))
3396 /*EMPTY*/;
3397 else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
3398 (dt_node_is_string(lp) || dt_node_is_string(rp)))
3399 /*EMPTY*/;
3400 else if (dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3401 xyerror(D_OP_INCOMPAT, "operands have "
3402 "incompatible types: \"%s\" %s \"%s\"\n",
3403 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3404 dt_node_type_name(rp, n2, sizeof (n2)));
3405 }
3406
3407 dt_node_type_assign(dnp, DT_INT_CTFP(dtp), DT_INT_TYPE(dtp),
3408 B_FALSE);
3409 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3410 break;
3411
3412 case DT_TOK_ADD:
3413 case DT_TOK_SUB: {
3414 /*
3415 * The rules for type checking for the additive operators are
3416 * described in the ANSI-C spec (see K&R[A7.7]). Pointers and
3417 * integers may be manipulated according to specific rules. In
3418 * these cases D permits strings to be treated as pointers.
3419 */
3420 int lp_is_ptr, lp_is_int, rp_is_ptr, rp_is_int;
3421
3422 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3423 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3424
3425 lp_is_ptr = dt_node_is_string(lp) ||
3426 (dt_node_is_pointer(lp) && !dt_node_is_vfptr(lp));
3427 lp_is_int = dt_node_is_integer(lp);
3428
3429 rp_is_ptr = dt_node_is_string(rp) ||
3430 (dt_node_is_pointer(rp) && !dt_node_is_vfptr(rp));
3431 rp_is_int = dt_node_is_integer(rp);
3432
3433 if (lp_is_int && rp_is_int) {
3434 dt_type_promote(lp, rp, &ctfp, &type);
3435 uref = 0;
3436 } else if (lp_is_ptr && rp_is_int) {
3437 ctfp = lp->dn_ctfp;
3438 type = lp->dn_type;
3439 uref = lp->dn_flags & DT_NF_USERLAND;
3440 } else if (lp_is_int && rp_is_ptr && op == DT_TOK_ADD) {
3441 ctfp = rp->dn_ctfp;
3442 type = rp->dn_type;
3443 uref = rp->dn_flags & DT_NF_USERLAND;
3444 } else if (lp_is_ptr && rp_is_ptr && op == DT_TOK_SUB &&
3445 dt_node_is_ptrcompat(lp, rp, NULL, NULL)) {
3446 ctfp = dtp->dt_ddefs->dm_ctfp;
3447 type = ctf_lookup_by_name(ctfp, "ptrdiff_t");
3448 uref = 0;
3449 } else {
3450 xyerror(D_OP_INCOMPAT, "operands have incompatible "
3451 "types: \"%s\" %s \"%s\"\n",
3452 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3453 dt_node_type_name(rp, n2, sizeof (n2)));
3454 }
3455
3456 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
3457 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3458
3459 if (uref)
3460 dnp->dn_flags |= DT_NF_USERLAND;
3461 break;
3462 }
3463
3464 case DT_TOK_OR_EQ:
3465 case DT_TOK_XOR_EQ:
3466 case DT_TOK_AND_EQ:
3467 case DT_TOK_LSH_EQ:
3468 case DT_TOK_RSH_EQ:
3469 case DT_TOK_MOD_EQ:
3470 if (lp->dn_kind == DT_NODE_IDENT) {
3471 dt_xcook_ident(lp, dtp->dt_globals,
3472 DT_IDENT_SCALAR, B_TRUE);
3473 }
3474
3475 lp = dnp->dn_left =
3476 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3477
3478 rp = dnp->dn_right =
3479 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3480
3481 if (!dt_node_is_integer(lp) || !dt_node_is_integer(rp)) {
3482 xyerror(D_OP_INT, "operator %s requires operands of "
3483 "integral type\n", opstr(op));
3484 }
3485 goto asgn_common;
3486
3487 case DT_TOK_MUL_EQ:
3488 case DT_TOK_DIV_EQ:
3489 if (lp->dn_kind == DT_NODE_IDENT) {
3490 dt_xcook_ident(lp, dtp->dt_globals,
3491 DT_IDENT_SCALAR, B_TRUE);
3492 }
3493
3494 lp = dnp->dn_left =
3495 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3496
3497 rp = dnp->dn_right =
3498 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3499
3500 if (!dt_node_is_arith(lp) || !dt_node_is_arith(rp)) {
3501 xyerror(D_OP_ARITH, "operator %s requires operands of "
3502 "arithmetic type\n", opstr(op));
3503 }
3504 goto asgn_common;
3505
3506 case DT_TOK_ASGN:
3507 /*
3508 * If the left-hand side is an identifier, attempt to resolve
3509 * it as either an aggregation or scalar variable. We pass
3510 * B_TRUE to dt_xcook_ident to indicate that a new variable can
3511 * be created if no matching variable exists in the namespace.
3512 */
3513 if (lp->dn_kind == DT_NODE_IDENT) {
3514 if (lp->dn_op == DT_TOK_AGG) {
3515 dt_xcook_ident(lp, dtp->dt_aggs,
3516 DT_IDENT_AGG, B_TRUE);
3517 } else {
3518 dt_xcook_ident(lp, dtp->dt_globals,
3519 DT_IDENT_SCALAR, B_TRUE);
3520 }
3521 }
3522
3523 lp = dnp->dn_left = dt_node_cook(lp, 0); /* don't set mod yet */
3524 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3525
3526 /*
3527 * If the left-hand side is an aggregation, verify that we are
3528 * assigning it the result of an aggregating function. Once
3529 * we've done so, hide the func node in the aggregation and
3530 * return the aggregation itself up to the parse tree parent.
3531 * This transformation is legal since the assigned function
3532 * cannot change identity across disjoint cooking passes and
3533 * the argument list subtree is retained for later cooking.
3534 */
3535 if (lp->dn_kind == DT_NODE_AGG) {
3536 const char *aname = lp->dn_ident->di_name;
3537 dt_ident_t *oid = lp->dn_ident->di_iarg;
3538
3539 if (rp->dn_kind != DT_NODE_FUNC ||
3540 rp->dn_ident->di_kind != DT_IDENT_AGGFUNC) {
3541 xyerror(D_AGG_FUNC,
3542 "@%s must be assigned the result of "
3543 "an aggregating function\n", aname);
3544 }
3545
3546 if (oid != NULL && oid != rp->dn_ident) {
3547 xyerror(D_AGG_REDEF,
3548 "aggregation redefined: @%s\n\t "
3549 "current: @%s = %s( )\n\tprevious: @%s = "
3550 "%s( ) : line %d\n", aname, aname,
3551 rp->dn_ident->di_name, aname, oid->di_name,
3552 lp->dn_ident->di_lineno);
3553 } else if (oid == NULL)
3554 lp->dn_ident->di_iarg = rp->dn_ident;
3555
3556 /*
3557 * Do not allow multiple aggregation assignments in a
3558 * single statement, e.g. (@a = count()) = count();
3559 * We produce a message as if the result of aggregating
3560 * function does not propagate DT_NF_LVALUE.
3561 */
3562 if (lp->dn_aggfun != NULL) {
3563 xyerror(D_OP_LVAL, "operator = requires "
3564 "modifiable lvalue as an operand\n");
3565 }
3566
3567 lp->dn_aggfun = rp;
3568 lp = dt_node_cook(lp, DT_IDFLG_MOD);
3569
3570 dnp->dn_left = dnp->dn_right = NULL;
3571 dt_node_free(dnp);
3572
3573 return (lp);
3574 }
3575
3576 /*
3577 * If the right-hand side is a dynamic variable that is the
3578 * output of a translator, our result is the translated type.
3579 */
3580 if ((idp = dt_node_resolve(rp, DT_IDENT_XLSOU)) != NULL) {
3581 ctfp = idp->di_ctfp;
3582 type = idp->di_type;
3583 uref = idp->di_flags & DT_IDFLG_USER;
3584 } else {
3585 ctfp = rp->dn_ctfp;
3586 type = rp->dn_type;
3587 uref = rp->dn_flags & DT_NF_USERLAND;
3588 }
3589
3590 /*
3591 * If the left-hand side of an assignment statement is a virgin
3592 * variable created by this compilation pass, reset the type of
3593 * this variable to the type of the right-hand side.
3594 */
3595 if (lp->dn_kind == DT_NODE_VAR &&
3596 dt_ident_unref(lp->dn_ident)) {
3597 dt_node_type_assign(lp, ctfp, type, B_FALSE);
3598 dt_ident_type_assign(lp->dn_ident, ctfp, type);
3599
3600 if (uref) {
3601 lp->dn_flags |= DT_NF_USERLAND;
3602 lp->dn_ident->di_flags |= DT_IDFLG_USER;
3603 }
3604 }
3605
3606 if (lp->dn_kind == DT_NODE_VAR)
3607 lp->dn_ident->di_flags |= DT_IDFLG_MOD;
3608
3609 /*
3610 * The rules for type checking for the assignment operators are
3611 * described in the ANSI-C spec (see K&R[A7.17]). We share
3612 * most of this code with the argument list checking code.
3613 */
3614 if (!dt_node_is_string(lp)) {
3615 kind = ctf_type_kind(lp->dn_ctfp,
3616 ctf_type_resolve(lp->dn_ctfp, lp->dn_type));
3617
3618 if (kind == CTF_K_ARRAY || kind == CTF_K_FUNCTION) {
3619 xyerror(D_OP_ARRFUN, "operator %s may not be "
3620 "applied to operand of type \"%s\"\n",
3621 opstr(op),
3622 dt_node_type_name(lp, n1, sizeof (n1)));
3623 }
3624 }
3625
3626 if (idp != NULL && idp->di_kind == DT_IDENT_XLSOU &&
3627 ctf_type_compat(lp->dn_ctfp, lp->dn_type, ctfp, type))
3628 goto asgn_common;
3629
3630 if (dt_node_is_argcompat(lp, rp))
3631 goto asgn_common;
3632
3633 xyerror(D_OP_INCOMPAT,
3634 "operands have incompatible types: \"%s\" %s \"%s\"\n",
3635 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3636 dt_node_type_name(rp, n2, sizeof (n2)));
3637 /*NOTREACHED*/
3638
3639 case DT_TOK_ADD_EQ:
3640 case DT_TOK_SUB_EQ:
3641 if (lp->dn_kind == DT_NODE_IDENT) {
3642 dt_xcook_ident(lp, dtp->dt_globals,
3643 DT_IDENT_SCALAR, B_TRUE);
3644 }
3645
3646 lp = dnp->dn_left =
3647 dt_node_cook(lp, DT_IDFLG_REF | DT_IDFLG_MOD);
3648
3649 rp = dnp->dn_right =
3650 dt_node_cook(rp, DT_IDFLG_REF | DT_IDFLG_MOD);
3651
3652 if (dt_node_is_string(lp) || dt_node_is_string(rp)) {
3653 xyerror(D_OP_INCOMPAT, "operands have "
3654 "incompatible types: \"%s\" %s \"%s\"\n",
3655 dt_node_type_name(lp, n1, sizeof (n1)), opstr(op),
3656 dt_node_type_name(rp, n2, sizeof (n2)));
3657 }
3658
3659 /*
3660 * The rules for type checking for the assignment operators are
3661 * described in the ANSI-C spec (see K&R[A7.17]). To these
3662 * rules we add that only writable D nodes can be modified.
3663 */
3664 if (dt_node_is_integer(lp) == 0 ||
3665 dt_node_is_integer(rp) == 0) {
3666 if (!dt_node_is_pointer(lp) || dt_node_is_vfptr(lp)) {
3667 xyerror(D_OP_VFPTR,
3668 "operator %s requires left-hand scalar "
3669 "operand of known size\n", opstr(op));
3670 } else if (dt_node_is_integer(rp) == 0 &&
3671 dt_node_is_ptrcompat(lp, rp, NULL, NULL) == 0) {
3672 xyerror(D_OP_INCOMPAT, "operands have "
3673 "incompatible types: \"%s\" %s \"%s\"\n",
3674 dt_node_type_name(lp, n1, sizeof (n1)),
3675 opstr(op),
3676 dt_node_type_name(rp, n2, sizeof (n2)));
3677 }
3678 }
3679 asgn_common:
3680 dt_assign_common(dnp);
3681 break;
3682
3683 case DT_TOK_PTR:
3684 /*
3685 * If the left-hand side of operator -> is one of the scoping
3686 * keywords, permit a local or thread variable to be created or
3687 * referenced.
3688 */
3689 if (lp->dn_kind == DT_NODE_IDENT) {
3690 dt_idhash_t *dhp = NULL;
3691
3692 if (strcmp(lp->dn_string, "self") == 0) {
3693 dhp = dtp->dt_tls;
3694 } else if (strcmp(lp->dn_string, "this") == 0) {
3695 dhp = yypcb->pcb_locals;
3696 }
3697 if (dhp != NULL) {
3698 if (rp->dn_kind != DT_NODE_VAR) {
3699 dt_xcook_ident(rp, dhp,
3700 DT_IDENT_SCALAR, B_TRUE);
3701 }
3702
3703 if (idflags != 0)
3704 rp = dt_node_cook(rp, idflags);
3705
3706 /* avoid freeing rp */
3707 dnp->dn_right = dnp->dn_left;
3708 dt_node_free(dnp);
3709 return (rp);
3710 }
3711 }
3712 /*FALLTHRU*/
3713 case DT_TOK_DOT:
3714 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3715
3716 if (rp->dn_kind != DT_NODE_IDENT) {
3717 xyerror(D_OP_IDENT, "operator %s must be followed by "
3718 "an identifier\n", opstr(op));
3719 }
3720
3721 if ((idp = dt_node_resolve(lp, DT_IDENT_XLSOU)) != NULL ||
3722 (idp = dt_node_resolve(lp, DT_IDENT_XLPTR)) != NULL) {
3723 /*
3724 * If the left-hand side is a translated struct or ptr,
3725 * the type of the left is the translation output type.
3726 */
3727 dt_xlator_t *dxp = idp->di_data;
3728
3729 if (dt_xlator_member(dxp, rp->dn_string) == NULL) {
3730 xyerror(D_XLATE_NOCONV,
3731 "translator does not define conversion "
3732 "for member: %s\n", rp->dn_string);
3733 }
3734
3735 ctfp = idp->di_ctfp;
3736 type = ctf_type_resolve(ctfp, idp->di_type);
3737 uref = idp->di_flags & DT_IDFLG_USER;
3738 } else {
3739 ctfp = lp->dn_ctfp;
3740 type = ctf_type_resolve(ctfp, lp->dn_type);
3741 uref = lp->dn_flags & DT_NF_USERLAND;
3742 }
3743
3744 kind = ctf_type_kind(ctfp, type);
3745
3746 if (op == DT_TOK_PTR) {
3747 if (kind != CTF_K_POINTER) {
3748 xyerror(D_OP_PTR, "operator %s must be "
3749 "applied to a pointer\n", opstr(op));
3750 }
3751 type = ctf_type_reference(ctfp, type);
3752 type = ctf_type_resolve(ctfp, type);
3753 kind = ctf_type_kind(ctfp, type);
3754 }
3755
3756 /*
3757 * If we follow a reference to a forward declaration tag,
3758 * search the entire type space for the actual definition.
3759 */
3760 while (kind == CTF_K_FORWARD) {
3761 char *tag = ctf_type_name(ctfp, type, n1, sizeof (n1));
3762 dtrace_typeinfo_t dtt;
3763
3764 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
3765 (dtt.dtt_ctfp != ctfp || dtt.dtt_type != type)) {
3766 ctfp = dtt.dtt_ctfp;
3767 type = ctf_type_resolve(ctfp, dtt.dtt_type);
3768 kind = ctf_type_kind(ctfp, type);
3769 } else {
3770 xyerror(D_OP_INCOMPLETE,
3771 "operator %s cannot be applied to a "
3772 "forward declaration: no %s definition "
3773 "is available\n", opstr(op), tag);
3774 }
3775 }
3776
3777 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
3778 if (op == DT_TOK_PTR) {
3779 xyerror(D_OP_SOU, "operator -> cannot be "
3780 "applied to pointer to type \"%s\"; must "
3781 "be applied to a struct or union pointer\n",
3782 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3783 } else {
3784 xyerror(D_OP_SOU, "operator %s cannot be "
3785 "applied to type \"%s\"; must be applied "
3786 "to a struct or union\n", opstr(op),
3787 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3788 }
3789 }
3790
3791 if (ctf_member_info(ctfp, type, rp->dn_string, &m) == CTF_ERR) {
3792 xyerror(D_TYPE_MEMBER,
3793 "%s is not a member of %s\n", rp->dn_string,
3794 ctf_type_name(ctfp, type, n1, sizeof (n1)));
3795 }
3796
3797 type = ctf_type_resolve(ctfp, m.ctm_type);
3798 kind = ctf_type_kind(ctfp, type);
3799
3800 dt_node_type_assign(dnp, ctfp, m.ctm_type, B_FALSE);
3801 dt_node_attr_assign(dnp, lp->dn_attr);
3802
3803 if (op == DT_TOK_PTR && (kind != CTF_K_ARRAY ||
3804 dt_node_is_string(dnp)))
3805 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3806
3807 if (op == DT_TOK_DOT && (lp->dn_flags & DT_NF_LVALUE) &&
3808 (kind != CTF_K_ARRAY || dt_node_is_string(dnp)))
3809 dnp->dn_flags |= DT_NF_LVALUE; /* see K&R[A7.3.3] */
3810
3811 if (lp->dn_flags & DT_NF_WRITABLE)
3812 dnp->dn_flags |= DT_NF_WRITABLE;
3813
3814 if (uref && (kind == CTF_K_POINTER ||
3815 (dnp->dn_flags & DT_NF_REF)))
3816 dnp->dn_flags |= DT_NF_USERLAND;
3817 break;
3818
3819 case DT_TOK_LBRAC: {
3820 /*
3821 * If op is DT_TOK_LBRAC, we know from the special-case code at
3822 * the top that lp is either a D variable or an aggregation.
3823 */
3824 dt_node_t *lnp;
3825
3826 /*
3827 * If the left-hand side is an aggregation, just set dn_aggtup
3828 * to the right-hand side and return the cooked aggregation.
3829 * This transformation is legal since we are just collapsing
3830 * nodes to simplify later processing, and the entire aggtup
3831 * parse subtree is retained for subsequent cooking passes.
3832 */
3833 if (lp->dn_kind == DT_NODE_AGG) {
3834 if (lp->dn_aggtup != NULL) {
3835 xyerror(D_AGG_MDIM, "improper attempt to "
3836 "reference @%s as a multi-dimensional "
3837 "array\n", lp->dn_ident->di_name);
3838 }
3839
3840 lp->dn_aggtup = rp;
3841 lp = dt_node_cook(lp, 0);
3842
3843 dnp->dn_left = dnp->dn_right = NULL;
3844 dt_node_free(dnp);
3845
3846 return (lp);
3847 }
3848
3849 assert(lp->dn_kind == DT_NODE_VAR);
3850 idp = lp->dn_ident;
3851
3852 /*
3853 * If the left-hand side is a non-global scalar that hasn't yet
3854 * been referenced or modified, it was just created by self->
3855 * or this-> and we can convert it from scalar to assoc array.
3856 */
3857 if (idp->di_kind == DT_IDENT_SCALAR && dt_ident_unref(idp) &&
3858 (idp->di_flags & (DT_IDFLG_LOCAL | DT_IDFLG_TLS)) != 0) {
3859
3860 if (idp->di_flags & DT_IDFLG_LOCAL) {
3861 xyerror(D_ARR_LOCAL,
3862 "local variables may not be used as "
3863 "associative arrays: %s\n", idp->di_name);
3864 }
3865
3866 dt_dprintf("morph variable %s (id %u) from scalar to "
3867 "array\n", idp->di_name, idp->di_id);
3868
3869 dt_ident_morph(idp, DT_IDENT_ARRAY,
3870 &dt_idops_assc, NULL);
3871 }
3872
3873 if (idp->di_kind != DT_IDENT_ARRAY) {
3874 xyerror(D_IDENT_BADREF, "%s '%s' may not be referenced "
3875 "as %s\n", dt_idkind_name(idp->di_kind),
3876 idp->di_name, dt_idkind_name(DT_IDENT_ARRAY));
3877 }
3878
3879 /*
3880 * Now that we've confirmed our left-hand side is a DT_NODE_VAR
3881 * of idkind DT_IDENT_ARRAY, we need to splice the [ node from
3882 * the parse tree and leave a cooked DT_NODE_VAR in its place
3883 * where dn_args for the VAR node is the right-hand 'rp' tree,
3884 * as shown in the parse tree diagram below:
3885 *
3886 * / /
3887 * [ OP2 "[" ]=dnp [ VAR ]=dnp
3888 * / \ => |
3889 * / \ +- dn_args -> [ ??? ]=rp
3890 * [ VAR ]=lp [ ??? ]=rp
3891 *
3892 * Since the final dt_node_cook(dnp) can fail using longjmp we
3893 * must perform the transformations as a group first by over-
3894 * writing 'dnp' to become the VAR node, so that the parse tree
3895 * is guaranteed to be in a consistent state if the cook fails.
3896 */
3897 assert(lp->dn_kind == DT_NODE_VAR);
3898 assert(lp->dn_args == NULL);
3899
3900 lnp = dnp->dn_link;
3901 bcopy(lp, dnp, sizeof (dt_node_t));
3902 dnp->dn_link = lnp;
3903
3904 dnp->dn_args = rp;
3905 dnp->dn_list = NULL;
3906
3907 dt_node_free(lp);
3908 return (dt_node_cook(dnp, idflags));
3909 }
3910
3911 case DT_TOK_XLATE: {
3912 dt_xlator_t *dxp;
3913
3914 assert(lp->dn_kind == DT_NODE_TYPE);
3915 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3916 dxp = dt_xlator_lookup(dtp, rp, lp, DT_XLATE_FUZZY);
3917
3918 if (dxp == NULL) {
3919 xyerror(D_XLATE_NONE,
3920 "cannot translate from \"%s\" to \"%s\"\n",
3921 dt_node_type_name(rp, n1, sizeof (n1)),
3922 dt_node_type_name(lp, n2, sizeof (n2)));
3923 }
3924
3925 dnp->dn_ident = dt_xlator_ident(dxp, lp->dn_ctfp, lp->dn_type);
3926 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
3927 B_FALSE);
3928 dt_node_attr_assign(dnp,
3929 dt_attr_min(rp->dn_attr, dnp->dn_ident->di_attr));
3930 break;
3931 }
3932
3933 case DT_TOK_LPAR: {
3934 ctf_id_t ltype, rtype;
3935 uint_t lkind, rkind;
3936
3937 assert(lp->dn_kind == DT_NODE_TYPE);
3938 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3939
3940 ltype = ctf_type_resolve(lp->dn_ctfp, lp->dn_type);
3941 lkind = ctf_type_kind(lp->dn_ctfp, ltype);
3942
3943 rtype = ctf_type_resolve(rp->dn_ctfp, rp->dn_type);
3944 rkind = ctf_type_kind(rp->dn_ctfp, rtype);
3945
3946 /*
3947 * The rules for casting are loosely explained in K&R[A7.5]
3948 * and K&R[A6]. Basically, we can cast to the same type or
3949 * same base type, between any kind of scalar values, from
3950 * arrays to pointers, and we can cast anything to void.
3951 * To these rules D adds casts from scalars to strings.
3952 */
3953 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
3954 rp->dn_ctfp, rp->dn_type))
3955 /*EMPTY*/;
3956 else if (dt_node_is_scalar(lp) &&
3957 (dt_node_is_scalar(rp) || rkind == CTF_K_FUNCTION))
3958 /*EMPTY*/;
3959 else if (dt_node_is_void(lp))
3960 /*EMPTY*/;
3961 else if (lkind == CTF_K_POINTER && dt_node_is_pointer(rp))
3962 /*EMPTY*/;
3963 else if (dt_node_is_string(lp) && (dt_node_is_scalar(rp) ||
3964 dt_node_is_pointer(rp) || dt_node_is_strcompat(rp)))
3965 /*EMPTY*/;
3966 else {
3967 xyerror(D_CAST_INVAL,
3968 "invalid cast expression: \"%s\" to \"%s\"\n",
3969 dt_node_type_name(rp, n1, sizeof (n1)),
3970 dt_node_type_name(lp, n2, sizeof (n2)));
3971 }
3972
3973 dt_node_type_propagate(lp, dnp); /* see K&R[A7.5] */
3974 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
3975
3976 /*
3977 * If it's a pointer then should be able to (attempt to)
3978 * assign to it.
3979 */
3980 if (lkind == CTF_K_POINTER)
3981 dnp->dn_flags |= DT_NF_WRITABLE;
3982
3983 break;
3984 }
3985
3986 case DT_TOK_COMMA:
3987 lp = dnp->dn_left = dt_node_cook(lp, DT_IDFLG_REF);
3988 rp = dnp->dn_right = dt_node_cook(rp, DT_IDFLG_REF);
3989
3990 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
3991 xyerror(D_OP_DYN, "operator %s operands "
3992 "cannot be of dynamic type\n", opstr(op));
3993 }
3994
3995 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
3996 xyerror(D_OP_ACT, "operator %s operands "
3997 "cannot be actions\n", opstr(op));
3998 }
3999
4000 dt_node_type_propagate(rp, dnp); /* see K&R[A7.18] */
4001 dt_node_attr_assign(dnp, dt_attr_min(lp->dn_attr, rp->dn_attr));
4002 break;
4003
4004 default:
4005 xyerror(D_UNKNOWN, "invalid binary op %s\n", opstr(op));
4006 }
4007
4008 /*
4009 * Complete the conversion of E1[E2] to *((E1)+(E2)) that we started
4010 * at the top of our switch() above (see K&R[A7.3.1]). Since E2 is
4011 * parsed as an argument_expression_list by dt_grammar.y, we can
4012 * end up with a comma-separated list inside of a non-associative
4013 * array reference. We check for this and report an appropriate error.
4014 */
4015 if (dnp->dn_op == DT_TOK_LBRAC && op == DT_TOK_ADD) {
4016 dt_node_t *pnp;
4017
4018 if (rp->dn_list != NULL) {
4019 xyerror(D_ARR_BADREF,
4020 "cannot access %s as an associative array\n",
4021 dt_node_name(lp, n1, sizeof (n1)));
4022 }
4023
4024 dnp->dn_op = DT_TOK_ADD;
4025 pnp = dt_node_op1(DT_TOK_DEREF, dnp);
4026
4027 /*
4028 * Cook callbacks are not typically permitted to allocate nodes.
4029 * When we do, we must insert them in the middle of an existing
4030 * allocation list rather than having them appended to the pcb
4031 * list because the sub-expression may be part of a definition.
4032 */
4033 assert(yypcb->pcb_list == pnp);
4034 yypcb->pcb_list = pnp->dn_link;
4035
4036 pnp->dn_link = dnp->dn_link;
4037 dnp->dn_link = pnp;
4038
4039 return (dt_node_cook(pnp, DT_IDFLG_REF));
4040 }
4041
4042 return (dnp);
4043 }
4044
4045 /*ARGSUSED*/
4046 static dt_node_t *
dt_cook_op3(dt_node_t * dnp,uint_t idflags)4047 dt_cook_op3(dt_node_t *dnp, uint_t idflags)
4048 {
4049 dt_node_t *lp, *rp;
4050 ctf_file_t *ctfp;
4051 ctf_id_t type;
4052
4053 dnp->dn_expr = dt_node_cook(dnp->dn_expr, DT_IDFLG_REF);
4054 lp = dnp->dn_left = dt_node_cook(dnp->dn_left, DT_IDFLG_REF);
4055 rp = dnp->dn_right = dt_node_cook(dnp->dn_right, DT_IDFLG_REF);
4056
4057 if (!dt_node_is_scalar(dnp->dn_expr)) {
4058 xyerror(D_OP_SCALAR,
4059 "operator ?: expression must be of scalar type\n");
4060 }
4061
4062 if (dt_node_is_dynamic(lp) || dt_node_is_dynamic(rp)) {
4063 xyerror(D_OP_DYN,
4064 "operator ?: operands cannot be of dynamic type\n");
4065 }
4066
4067 /*
4068 * The rules for type checking for the ternary operator are complex and
4069 * are described in the ANSI-C spec (see K&R[A7.16]). We implement
4070 * the various tests in order from least to most expensive.
4071 */
4072 if (ctf_type_compat(lp->dn_ctfp, lp->dn_type,
4073 rp->dn_ctfp, rp->dn_type)) {
4074 ctfp = lp->dn_ctfp;
4075 type = lp->dn_type;
4076 } else if (dt_node_is_integer(lp) && dt_node_is_integer(rp)) {
4077 dt_type_promote(lp, rp, &ctfp, &type);
4078 } else if (dt_node_is_strcompat(lp) && dt_node_is_strcompat(rp) &&
4079 (dt_node_is_string(lp) || dt_node_is_string(rp))) {
4080 ctfp = DT_STR_CTFP(yypcb->pcb_hdl);
4081 type = DT_STR_TYPE(yypcb->pcb_hdl);
4082 } else if (dt_node_is_ptrcompat(lp, rp, &ctfp, &type) == 0) {
4083 xyerror(D_OP_INCOMPAT,
4084 "operator ?: operands must have compatible types\n");
4085 }
4086
4087 if (dt_node_is_actfunc(lp) || dt_node_is_actfunc(rp)) {
4088 xyerror(D_OP_ACT, "action cannot be "
4089 "used in a conditional context\n");
4090 }
4091
4092 dt_node_type_assign(dnp, ctfp, type, B_FALSE);
4093 dt_node_attr_assign(dnp, dt_attr_min(dnp->dn_expr->dn_attr,
4094 dt_attr_min(lp->dn_attr, rp->dn_attr)));
4095
4096 return (dnp);
4097 }
4098
4099 static dt_node_t *
dt_cook_statement(dt_node_t * dnp,uint_t idflags)4100 dt_cook_statement(dt_node_t *dnp, uint_t idflags)
4101 {
4102 dnp->dn_expr = dt_node_cook(dnp->dn_expr, idflags);
4103 dt_node_attr_assign(dnp, dnp->dn_expr->dn_attr);
4104
4105 return (dnp);
4106 }
4107
4108 /*
4109 * If dn_aggfun is set, this node is a collapsed aggregation assignment (see
4110 * the special case code for DT_TOK_ASGN in dt_cook_op2() above), in which
4111 * case we cook both the tuple and the function call. If dn_aggfun is NULL,
4112 * this node is just a reference to the aggregation's type and attributes.
4113 */
4114 /*ARGSUSED*/
4115 static dt_node_t *
dt_cook_aggregation(dt_node_t * dnp,uint_t idflags)4116 dt_cook_aggregation(dt_node_t *dnp, uint_t idflags)
4117 {
4118 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4119
4120 if (dnp->dn_aggfun != NULL) {
4121 dnp->dn_aggfun = dt_node_cook(dnp->dn_aggfun, DT_IDFLG_REF);
4122 dt_node_attr_assign(dnp, dt_ident_cook(dnp,
4123 dnp->dn_ident, &dnp->dn_aggtup));
4124 } else {
4125 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
4126 B_FALSE);
4127 dt_node_attr_assign(dnp, dnp->dn_ident->di_attr);
4128 }
4129
4130 return (dnp);
4131 }
4132
4133 /*
4134 * Since D permits new variable identifiers to be instantiated in any program
4135 * expression, we may need to cook a clause's predicate either before or after
4136 * the action list depending on the program code in question. Consider:
4137 *
4138 * probe-description-list probe-description-list
4139 * /x++/ /x == 0/
4140 * { {
4141 * trace(x); trace(x++);
4142 * } }
4143 *
4144 * In the left-hand example, the predicate uses operator ++ to instantiate 'x'
4145 * as a variable of type int64_t. The predicate must be cooked first because
4146 * otherwise the statement trace(x) refers to an unknown identifier. In the
4147 * right-hand example, the action list uses ++ to instantiate 'x'; the action
4148 * list must be cooked first because otherwise the predicate x == 0 refers to
4149 * an unknown identifier. In order to simplify programming, we support both.
4150 *
4151 * When cooking a clause, we cook the action statements before the predicate by
4152 * default, since it seems more common to create or modify identifiers in the
4153 * action list. If cooking fails due to an unknown identifier, we attempt to
4154 * cook the predicate (i.e. do it first) and then go back and cook the actions.
4155 * If this, too, fails (or if we get an error other than D_IDENT_UNDEF) we give
4156 * up and report failure back to the user. There are five possible paths:
4157 *
4158 * cook actions = OK, cook predicate = OK -> OK
4159 * cook actions = OK, cook predicate = ERR -> ERR
4160 * cook actions = ERR, cook predicate = ERR -> ERR
4161 * cook actions = ERR, cook predicate = OK, cook actions = OK -> OK
4162 * cook actions = ERR, cook predicate = OK, cook actions = ERR -> ERR
4163 *
4164 * The programmer can still defeat our scheme by creating circular definition
4165 * dependencies between predicates and actions, as in this example clause:
4166 *
4167 * probe-description-list
4168 * /x++ && y == 0/
4169 * {
4170 * trace(x + y++);
4171 * }
4172 *
4173 * but it doesn't seem worth the complexity to handle such rare cases. The
4174 * user can simply use the D variable declaration syntax to work around them.
4175 */
4176 static dt_node_t *
dt_cook_clause(dt_node_t * dnp,uint_t idflags)4177 dt_cook_clause(dt_node_t *dnp, uint_t idflags)
4178 {
4179 volatile int err, tries;
4180 jmp_buf ojb;
4181
4182 /*
4183 * Before assigning dn_ctxattr, temporarily assign the probe attribute
4184 * to 'dnp' itself to force an attribute check and minimum violation.
4185 */
4186 dt_node_attr_assign(dnp, yypcb->pcb_pinfo.dtp_attr);
4187 dnp->dn_ctxattr = yypcb->pcb_pinfo.dtp_attr;
4188
4189 bcopy(yypcb->pcb_jmpbuf, ojb, sizeof (jmp_buf));
4190 tries = 0;
4191
4192 if (dnp->dn_pred != NULL && (err = setjmp(yypcb->pcb_jmpbuf)) != 0) {
4193 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4194 if (tries++ != 0 || err != EDT_COMPILER || (
4195 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_IDENT_UNDEF) &&
4196 yypcb->pcb_hdl->dt_errtag != dt_errtag(D_VAR_UNDEF)))
4197 longjmp(yypcb->pcb_jmpbuf, err);
4198 }
4199
4200 if (tries == 0) {
4201 yylabel("action list");
4202
4203 dt_node_attr_assign(dnp,
4204 dt_node_list_cook(&dnp->dn_acts, idflags));
4205
4206 bcopy(ojb, yypcb->pcb_jmpbuf, sizeof (jmp_buf));
4207 yylabel(NULL);
4208 }
4209
4210 if (dnp->dn_pred != NULL) {
4211 yylabel("predicate");
4212
4213 dnp->dn_pred = dt_node_cook(dnp->dn_pred, idflags);
4214 dt_node_attr_assign(dnp,
4215 dt_attr_min(dnp->dn_attr, dnp->dn_pred->dn_attr));
4216
4217 if (!dt_node_is_scalar(dnp->dn_pred)) {
4218 xyerror(D_PRED_SCALAR,
4219 "predicate result must be of scalar type\n");
4220 }
4221
4222 yylabel(NULL);
4223 }
4224
4225 if (tries != 0) {
4226 yylabel("action list");
4227
4228 dt_node_attr_assign(dnp,
4229 dt_node_list_cook(&dnp->dn_acts, idflags));
4230
4231 yylabel(NULL);
4232 }
4233
4234 return (dnp);
4235 }
4236
4237 /*ARGSUSED*/
4238 static dt_node_t *
dt_cook_inline(dt_node_t * dnp,uint_t idflags)4239 dt_cook_inline(dt_node_t *dnp, uint_t idflags)
4240 {
4241 dt_idnode_t *inp = dnp->dn_ident->di_iarg;
4242 dt_ident_t *rdp;
4243
4244 char n1[DT_TYPE_NAMELEN];
4245 char n2[DT_TYPE_NAMELEN];
4246
4247 assert(dnp->dn_ident->di_flags & DT_IDFLG_INLINE);
4248 assert(inp->din_root->dn_flags & DT_NF_COOKED);
4249
4250 /*
4251 * If we are inlining a translation, verify that the inline declaration
4252 * type exactly matches the type that is returned by the translation.
4253 * Otherwise just use dt_node_is_argcompat() to check the types.
4254 */
4255 if ((rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLSOU)) != NULL ||
4256 (rdp = dt_node_resolve(inp->din_root, DT_IDENT_XLPTR)) != NULL) {
4257
4258 ctf_file_t *lctfp = dnp->dn_ctfp;
4259 ctf_id_t ltype = ctf_type_resolve(lctfp, dnp->dn_type);
4260
4261 dt_xlator_t *dxp = rdp->di_data;
4262 ctf_file_t *rctfp = dxp->dx_dst_ctfp;
4263 ctf_id_t rtype = dxp->dx_dst_base;
4264
4265 if (ctf_type_kind(lctfp, ltype) == CTF_K_POINTER) {
4266 ltype = ctf_type_reference(lctfp, ltype);
4267 ltype = ctf_type_resolve(lctfp, ltype);
4268 }
4269
4270 if (ctf_type_compat(lctfp, ltype, rctfp, rtype) == 0) {
4271 dnerror(dnp, D_OP_INCOMPAT,
4272 "inline %s definition uses incompatible types: "
4273 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4274 dt_type_name(lctfp, ltype, n1, sizeof (n1)),
4275 dt_type_name(rctfp, rtype, n2, sizeof (n2)));
4276 }
4277
4278 } else if (dt_node_is_argcompat(dnp, inp->din_root) == 0) {
4279 dnerror(dnp, D_OP_INCOMPAT,
4280 "inline %s definition uses incompatible types: "
4281 "\"%s\" = \"%s\"\n", dnp->dn_ident->di_name,
4282 dt_node_type_name(dnp, n1, sizeof (n1)),
4283 dt_node_type_name(inp->din_root, n2, sizeof (n2)));
4284 }
4285
4286 return (dnp);
4287 }
4288
4289 static dt_node_t *
dt_cook_member(dt_node_t * dnp,uint_t idflags)4290 dt_cook_member(dt_node_t *dnp, uint_t idflags)
4291 {
4292 dnp->dn_membexpr = dt_node_cook(dnp->dn_membexpr, idflags);
4293 dt_node_attr_assign(dnp, dnp->dn_membexpr->dn_attr);
4294 return (dnp);
4295 }
4296
4297 /*ARGSUSED*/
4298 static dt_node_t *
dt_cook_xlator(dt_node_t * dnp,uint_t idflags)4299 dt_cook_xlator(dt_node_t *dnp, uint_t idflags)
4300 {
4301 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4302 dt_xlator_t *dxp = dnp->dn_xlator;
4303 dt_node_t *mnp;
4304
4305 char n1[DT_TYPE_NAMELEN];
4306 char n2[DT_TYPE_NAMELEN];
4307
4308 dtrace_attribute_t attr = _dtrace_maxattr;
4309 ctf_membinfo_t ctm;
4310
4311 /*
4312 * Before cooking each translator member, we push a reference to the
4313 * hash containing translator-local identifiers on to pcb_globals to
4314 * temporarily interpose these identifiers in front of other globals.
4315 */
4316 dt_idstack_push(&yypcb->pcb_globals, dxp->dx_locals);
4317
4318 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
4319 if (ctf_member_info(dxp->dx_dst_ctfp, dxp->dx_dst_type,
4320 mnp->dn_membname, &ctm) == CTF_ERR) {
4321 xyerror(D_XLATE_MEMB,
4322 "translator member %s is not a member of %s\n",
4323 mnp->dn_membname, ctf_type_name(dxp->dx_dst_ctfp,
4324 dxp->dx_dst_type, n1, sizeof (n1)));
4325 }
4326
4327 (void) dt_node_cook(mnp, DT_IDFLG_REF);
4328 dt_node_type_assign(mnp, dxp->dx_dst_ctfp, ctm.ctm_type,
4329 B_FALSE);
4330 attr = dt_attr_min(attr, mnp->dn_attr);
4331
4332 if (dt_node_is_argcompat(mnp, mnp->dn_membexpr) == 0) {
4333 xyerror(D_XLATE_INCOMPAT,
4334 "translator member %s definition uses "
4335 "incompatible types: \"%s\" = \"%s\"\n",
4336 mnp->dn_membname,
4337 dt_node_type_name(mnp, n1, sizeof (n1)),
4338 dt_node_type_name(mnp->dn_membexpr,
4339 n2, sizeof (n2)));
4340 }
4341 }
4342
4343 dt_idstack_pop(&yypcb->pcb_globals, dxp->dx_locals);
4344
4345 dxp->dx_souid.di_attr = attr;
4346 dxp->dx_ptrid.di_attr = attr;
4347
4348 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), B_FALSE);
4349 dt_node_attr_assign(dnp, _dtrace_defattr);
4350
4351 return (dnp);
4352 }
4353
4354 static void
dt_node_provider_cmp_argv(dt_provider_t * pvp,dt_node_t * pnp,const char * kind,uint_t old_argc,dt_node_t * old_argv,uint_t new_argc,dt_node_t * new_argv)4355 dt_node_provider_cmp_argv(dt_provider_t *pvp, dt_node_t *pnp, const char *kind,
4356 uint_t old_argc, dt_node_t *old_argv, uint_t new_argc, dt_node_t *new_argv)
4357 {
4358 dt_probe_t *prp = pnp->dn_ident->di_data;
4359 uint_t i;
4360
4361 char n1[DT_TYPE_NAMELEN];
4362 char n2[DT_TYPE_NAMELEN];
4363
4364 if (old_argc != new_argc) {
4365 dnerror(pnp, D_PROV_INCOMPAT,
4366 "probe %s:%s %s prototype mismatch:\n"
4367 "\t current: %u arg%s\n\tprevious: %u arg%s\n",
4368 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind,
4369 new_argc, new_argc != 1 ? "s" : "",
4370 old_argc, old_argc != 1 ? "s" : "");
4371 }
4372
4373 for (i = 0; i < old_argc; i++,
4374 old_argv = old_argv->dn_list, new_argv = new_argv->dn_list) {
4375 if (ctf_type_cmp(old_argv->dn_ctfp, old_argv->dn_type,
4376 new_argv->dn_ctfp, new_argv->dn_type) == 0)
4377 continue;
4378
4379 dnerror(pnp, D_PROV_INCOMPAT,
4380 "probe %s:%s %s prototype argument #%u mismatch:\n"
4381 "\t current: %s\n\tprevious: %s\n",
4382 pvp->pv_desc.dtvd_name, prp->pr_ident->di_name, kind, i + 1,
4383 dt_node_type_name(new_argv, n1, sizeof (n1)),
4384 dt_node_type_name(old_argv, n2, sizeof (n2)));
4385 }
4386 }
4387
4388 /*
4389 * Compare a new probe declaration with an existing probe definition (either
4390 * from a previous declaration or cached from the kernel). If the existing
4391 * definition and declaration both have an input and output parameter list,
4392 * compare both lists. Otherwise compare only the output parameter lists.
4393 */
4394 static void
dt_node_provider_cmp(dt_provider_t * pvp,dt_node_t * pnp,dt_probe_t * old,dt_probe_t * new)4395 dt_node_provider_cmp(dt_provider_t *pvp, dt_node_t *pnp,
4396 dt_probe_t *old, dt_probe_t *new)
4397 {
4398 dt_node_provider_cmp_argv(pvp, pnp, "output",
4399 old->pr_xargc, old->pr_xargs, new->pr_xargc, new->pr_xargs);
4400
4401 if (old->pr_nargs != old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4402 dt_node_provider_cmp_argv(pvp, pnp, "input",
4403 old->pr_nargc, old->pr_nargs, new->pr_nargc, new->pr_nargs);
4404 }
4405
4406 if (old->pr_nargs == old->pr_xargs && new->pr_nargs != new->pr_xargs) {
4407 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
4408 dnerror(pnp, D_PROV_INCOMPAT,
4409 "provider interface mismatch: %s\n"
4410 "\t current: probe %s:%s has an output prototype\n"
4411 "\tprevious: probe %s:%s has no output prototype\n",
4412 pvp->pv_desc.dtvd_name, pvp->pv_desc.dtvd_name,
4413 new->pr_ident->di_name, pvp->pv_desc.dtvd_name,
4414 old->pr_ident->di_name);
4415 }
4416
4417 if (old->pr_ident->di_gen == yypcb->pcb_hdl->dt_gen)
4418 old->pr_ident->di_flags |= DT_IDFLG_ORPHAN;
4419
4420 dt_idhash_delete(pvp->pv_probes, old->pr_ident);
4421 dt_probe_declare(pvp, new);
4422 }
4423 }
4424
4425 static void
dt_cook_probe(dt_node_t * dnp,dt_provider_t * pvp)4426 dt_cook_probe(dt_node_t *dnp, dt_provider_t *pvp)
4427 {
4428 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
4429 dt_probe_t *prp = dnp->dn_ident->di_data;
4430
4431 dt_xlator_t *dxp;
4432 uint_t i;
4433
4434 char n1[DT_TYPE_NAMELEN];
4435 char n2[DT_TYPE_NAMELEN];
4436
4437 if (prp->pr_nargs == prp->pr_xargs)
4438 return;
4439
4440 for (i = 0; i < prp->pr_xargc; i++) {
4441 dt_node_t *xnp = prp->pr_xargv[i];
4442 dt_node_t *nnp = prp->pr_nargv[prp->pr_mapping[i]];
4443
4444 if ((dxp = dt_xlator_lookup(dtp,
4445 nnp, xnp, DT_XLATE_FUZZY)) != NULL) {
4446 if (dt_provider_xref(dtp, pvp, dxp->dx_id) != 0)
4447 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
4448 continue;
4449 }
4450
4451 if (dt_node_is_argcompat(nnp, xnp))
4452 continue; /* no translator defined and none required */
4453
4454 dnerror(dnp, D_PROV_PRXLATOR, "translator for %s:%s output "
4455 "argument #%u from %s to %s is not defined\n",
4456 pvp->pv_desc.dtvd_name, dnp->dn_ident->di_name, i + 1,
4457 dt_node_type_name(nnp, n1, sizeof (n1)),
4458 dt_node_type_name(xnp, n2, sizeof (n2)));
4459 }
4460 }
4461
4462 /*ARGSUSED*/
4463 static dt_node_t *
dt_cook_provider(dt_node_t * dnp,uint_t idflags)4464 dt_cook_provider(dt_node_t *dnp, uint_t idflags)
4465 {
4466 dt_provider_t *pvp = dnp->dn_provider;
4467 dt_node_t *pnp;
4468
4469 /*
4470 * If we're declaring a provider for the first time and it is unknown
4471 * to dtrace(7D), insert the probe definitions into the provider's hash.
4472 * If we're redeclaring a known provider, verify the interface matches.
4473 */
4474 for (pnp = dnp->dn_probes; pnp != NULL; pnp = pnp->dn_list) {
4475 const char *probename = pnp->dn_ident->di_name;
4476 dt_probe_t *prp = dt_probe_lookup(pvp, probename);
4477
4478 assert(pnp->dn_kind == DT_NODE_PROBE);
4479
4480 if (prp != NULL && dnp->dn_provred) {
4481 dt_node_provider_cmp(pvp, pnp,
4482 prp, pnp->dn_ident->di_data);
4483 } else if (prp == NULL && dnp->dn_provred) {
4484 dnerror(pnp, D_PROV_INCOMPAT,
4485 "provider interface mismatch: %s\n"
4486 "\t current: probe %s:%s defined\n"
4487 "\tprevious: probe %s:%s not defined\n",
4488 dnp->dn_provname, dnp->dn_provname,
4489 probename, dnp->dn_provname, probename);
4490 } else if (prp != NULL) {
4491 dnerror(pnp, D_PROV_PRDUP, "probe redeclared: %s:%s\n",
4492 dnp->dn_provname, probename);
4493 } else
4494 dt_probe_declare(pvp, pnp->dn_ident->di_data);
4495
4496 dt_cook_probe(pnp, pvp);
4497 }
4498
4499 return (dnp);
4500 }
4501
4502 /*ARGSUSED*/
4503 static dt_node_t *
dt_cook_none(dt_node_t * dnp,uint_t idflags)4504 dt_cook_none(dt_node_t *dnp, uint_t idflags)
4505 {
4506 return (dnp);
4507 }
4508
4509 static dt_node_t *(*dt_cook_funcs[])(dt_node_t *, uint_t) = {
4510 dt_cook_none, /* DT_NODE_FREE */
4511 dt_cook_none, /* DT_NODE_INT */
4512 dt_cook_none, /* DT_NODE_STRING */
4513 dt_cook_ident, /* DT_NODE_IDENT */
4514 dt_cook_var, /* DT_NODE_VAR */
4515 dt_cook_none, /* DT_NODE_SYM */
4516 dt_cook_none, /* DT_NODE_TYPE */
4517 dt_cook_func, /* DT_NODE_FUNC */
4518 dt_cook_op1, /* DT_NODE_OP1 */
4519 dt_cook_op2, /* DT_NODE_OP2 */
4520 dt_cook_op3, /* DT_NODE_OP3 */
4521 dt_cook_statement, /* DT_NODE_DEXPR */
4522 dt_cook_statement, /* DT_NODE_DFUNC */
4523 dt_cook_aggregation, /* DT_NODE_AGG */
4524 dt_cook_none, /* DT_NODE_PDESC */
4525 dt_cook_clause, /* DT_NODE_CLAUSE */
4526 dt_cook_inline, /* DT_NODE_INLINE */
4527 dt_cook_member, /* DT_NODE_MEMBER */
4528 dt_cook_xlator, /* DT_NODE_XLATOR */
4529 dt_cook_none, /* DT_NODE_PROBE */
4530 dt_cook_provider, /* DT_NODE_PROVIDER */
4531 dt_cook_none, /* DT_NODE_PROG */
4532 dt_cook_none, /* DT_NODE_IF */
4533 };
4534
4535 /*
4536 * Recursively cook the parse tree starting at the specified node. The idflags
4537 * parameter is used to indicate the type of reference (r/w) and is applied to
4538 * the resulting identifier if it is a D variable or D aggregation.
4539 */
4540 dt_node_t *
dt_node_cook(dt_node_t * dnp,uint_t idflags)4541 dt_node_cook(dt_node_t *dnp, uint_t idflags)
4542 {
4543 int oldlineno = yylineno;
4544
4545 yylineno = dnp->dn_line;
4546
4547 assert(dnp->dn_kind <
4548 sizeof (dt_cook_funcs) / sizeof (dt_cook_funcs[0]));
4549 dnp = dt_cook_funcs[dnp->dn_kind](dnp, idflags);
4550 dnp->dn_flags |= DT_NF_COOKED;
4551
4552 if (dnp->dn_kind == DT_NODE_VAR || dnp->dn_kind == DT_NODE_AGG)
4553 dnp->dn_ident->di_flags |= idflags;
4554
4555 yylineno = oldlineno;
4556 return (dnp);
4557 }
4558
4559 dtrace_attribute_t
dt_node_list_cook(dt_node_t ** pnp,uint_t idflags)4560 dt_node_list_cook(dt_node_t **pnp, uint_t idflags)
4561 {
4562 dtrace_attribute_t attr = _dtrace_defattr;
4563 dt_node_t *dnp, *nnp;
4564
4565 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4566 nnp = dnp->dn_list;
4567 dnp = *pnp = dt_node_cook(dnp, idflags);
4568 attr = dt_attr_min(attr, dnp->dn_attr);
4569 dnp->dn_list = nnp;
4570 pnp = &dnp->dn_list;
4571 }
4572
4573 return (attr);
4574 }
4575
4576 void
dt_node_list_free(dt_node_t ** pnp)4577 dt_node_list_free(dt_node_t **pnp)
4578 {
4579 dt_node_t *dnp, *nnp;
4580
4581 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4582 nnp = dnp->dn_list;
4583 dt_node_free(dnp);
4584 }
4585
4586 if (pnp != NULL)
4587 *pnp = NULL;
4588 }
4589
4590 void
dt_node_link_free(dt_node_t ** pnp)4591 dt_node_link_free(dt_node_t **pnp)
4592 {
4593 dt_node_t *dnp, *nnp;
4594
4595 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4596 nnp = dnp->dn_link;
4597 dt_node_free(dnp);
4598 }
4599
4600 for (dnp = (pnp != NULL ? *pnp : NULL); dnp != NULL; dnp = nnp) {
4601 nnp = dnp->dn_link;
4602 free(dnp);
4603 }
4604
4605 if (pnp != NULL)
4606 *pnp = NULL;
4607 }
4608
4609 dt_node_t *
dt_node_link(dt_node_t * lp,dt_node_t * rp)4610 dt_node_link(dt_node_t *lp, dt_node_t *rp)
4611 {
4612 dt_node_t *dnp;
4613
4614 if (lp == NULL)
4615 return (rp);
4616 else if (rp == NULL)
4617 return (lp);
4618
4619 for (dnp = lp; dnp->dn_list != NULL; dnp = dnp->dn_list)
4620 continue;
4621
4622 dnp->dn_list = rp;
4623 return (lp);
4624 }
4625
4626 /*
4627 * Compute the DOF dtrace_diftype_t representation of a node's type. This is
4628 * called from a variety of places in the library so it cannot assume yypcb
4629 * is valid: any references to handle-specific data must be made through 'dtp'.
4630 */
4631 void
dt_node_diftype(dtrace_hdl_t * dtp,const dt_node_t * dnp,dtrace_diftype_t * tp)4632 dt_node_diftype(dtrace_hdl_t *dtp, const dt_node_t *dnp, dtrace_diftype_t *tp)
4633 {
4634 if (dnp->dn_ctfp == DT_STR_CTFP(dtp) &&
4635 dnp->dn_type == DT_STR_TYPE(dtp)) {
4636 tp->dtdt_kind = DIF_TYPE_STRING;
4637 tp->dtdt_ckind = CTF_K_UNKNOWN;
4638 } else {
4639 tp->dtdt_kind = DIF_TYPE_CTF;
4640 tp->dtdt_ckind = ctf_type_kind(dnp->dn_ctfp,
4641 ctf_type_resolve(dnp->dn_ctfp, dnp->dn_type));
4642 }
4643
4644 tp->dtdt_flags = (dnp->dn_flags & DT_NF_REF) ?
4645 (dnp->dn_flags & DT_NF_USERLAND) ? DIF_TF_BYUREF :
4646 DIF_TF_BYREF : 0;
4647 tp->dtdt_pad = 0;
4648 tp->dtdt_size = ctf_type_size(dnp->dn_ctfp, dnp->dn_type);
4649 }
4650
4651 /*
4652 * Output the parse tree as D. The "-xtree=8" argument will call this
4653 * function to print out the program after any syntactic sugar
4654 * transformations have been applied (e.g. to implement "if"). The
4655 * resulting output can be used to understand the transformations
4656 * applied by these features, or to run such a script on a system that
4657 * does not support these features
4658 *
4659 * Note that the output does not express precisely the same program as
4660 * the input. In particular:
4661 * - Only the clauses are output. #pragma options, variable
4662 * declarations, etc. are excluded.
4663 * - Command argument substitution has already been done, so the output
4664 * will not contain e.g. $$1, but rather the substituted string.
4665 */
4666 void
dt_printd(dt_node_t * dnp,FILE * fp,int depth)4667 dt_printd(dt_node_t *dnp, FILE *fp, int depth)
4668 {
4669 dt_node_t *arg;
4670
4671 switch (dnp->dn_kind) {
4672 case DT_NODE_INT:
4673 (void) fprintf(fp, "0x%llx", (u_longlong_t)dnp->dn_value);
4674 if (!(dnp->dn_flags & DT_NF_SIGNED))
4675 (void) fprintf(fp, "u");
4676 break;
4677
4678 case DT_NODE_STRING: {
4679 char *escd = strchr2esc(dnp->dn_string, strlen(dnp->dn_string));
4680 (void) fprintf(fp, "\"%s\"", escd);
4681 free(escd);
4682 break;
4683 }
4684
4685 case DT_NODE_IDENT:
4686 (void) fprintf(fp, "%s", dnp->dn_string);
4687 break;
4688
4689 case DT_NODE_VAR:
4690 (void) fprintf(fp, "%s%s",
4691 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4692 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4693 dnp->dn_ident->di_name);
4694
4695 if (dnp->dn_args != NULL) {
4696 (void) fprintf(fp, "[");
4697
4698 for (arg = dnp->dn_args; arg != NULL;
4699 arg = arg->dn_list) {
4700 dt_printd(arg, fp, 0);
4701 if (arg->dn_list != NULL)
4702 (void) fprintf(fp, ", ");
4703 }
4704
4705 (void) fprintf(fp, "]");
4706 }
4707 break;
4708
4709 case DT_NODE_SYM: {
4710 const dtrace_syminfo_t *dts = dnp->dn_ident->di_data;
4711 (void) fprintf(fp, "%s`%s", dts->dts_object, dts->dts_name);
4712 break;
4713 }
4714 case DT_NODE_FUNC:
4715 (void) fprintf(fp, "%s(", dnp->dn_ident->di_name);
4716
4717 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4718 dt_printd(arg, fp, 0);
4719 if (arg->dn_list != NULL)
4720 (void) fprintf(fp, ", ");
4721 }
4722 (void) fprintf(fp, ")");
4723 break;
4724
4725 case DT_NODE_OP1:
4726 (void) fprintf(fp, "%s(", opstr(dnp->dn_op));
4727 dt_printd(dnp->dn_child, fp, 0);
4728 (void) fprintf(fp, ")");
4729 break;
4730
4731 case DT_NODE_OP2:
4732 (void) fprintf(fp, "(");
4733 dt_printd(dnp->dn_left, fp, 0);
4734 if (dnp->dn_op == DT_TOK_LPAR) {
4735 (void) fprintf(fp, ")");
4736 dt_printd(dnp->dn_right, fp, 0);
4737 break;
4738 }
4739 if (dnp->dn_op == DT_TOK_PTR || dnp->dn_op == DT_TOK_DOT ||
4740 dnp->dn_op == DT_TOK_LBRAC)
4741 (void) fprintf(fp, "%s", opstr(dnp->dn_op));
4742 else
4743 (void) fprintf(fp, " %s ", opstr(dnp->dn_op));
4744 dt_printd(dnp->dn_right, fp, 0);
4745 if (dnp->dn_op == DT_TOK_LBRAC) {
4746 dt_node_t *ln = dnp->dn_right;
4747 while (ln->dn_list != NULL) {
4748 (void) fprintf(fp, ", ");
4749 dt_printd(ln->dn_list, fp, depth);
4750 ln = ln->dn_list;
4751 }
4752 (void) fprintf(fp, "]");
4753 }
4754 (void) fprintf(fp, ")");
4755 break;
4756
4757 case DT_NODE_OP3:
4758 (void) fprintf(fp, "(");
4759 dt_printd(dnp->dn_expr, fp, 0);
4760 (void) fprintf(fp, " ? ");
4761 dt_printd(dnp->dn_left, fp, 0);
4762 (void) fprintf(fp, " : ");
4763 dt_printd(dnp->dn_right, fp, 0);
4764 (void) fprintf(fp, ")");
4765 break;
4766
4767 case DT_NODE_DEXPR:
4768 case DT_NODE_DFUNC:
4769 (void) fprintf(fp, "%*s", depth * 8, "");
4770 dt_printd(dnp->dn_expr, fp, depth + 1);
4771 (void) fprintf(fp, ";\n");
4772 break;
4773
4774 case DT_NODE_PDESC:
4775 (void) fprintf(fp, "%s:%s:%s:%s",
4776 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4777 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name);
4778 break;
4779
4780 case DT_NODE_CLAUSE:
4781 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list) {
4782 dt_printd(arg, fp, 0);
4783 if (arg->dn_list != NULL)
4784 (void) fprintf(fp, ",");
4785 (void) fprintf(fp, "\n");
4786 }
4787
4788 if (dnp->dn_pred != NULL) {
4789 (void) fprintf(fp, "/");
4790 dt_printd(dnp->dn_pred, fp, 0);
4791 (void) fprintf(fp, "/\n");
4792 }
4793
4794 (void) fprintf(fp, "{\n");
4795 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
4796 dt_printd(arg, fp, depth + 1);
4797 (void) fprintf(fp, "}\n");
4798 (void) fprintf(fp, "\n");
4799 break;
4800
4801 case DT_NODE_IF:
4802 (void) fprintf(fp, "%*sif (", depth * 8, "");
4803 dt_printd(dnp->dn_conditional, fp, 0);
4804 (void) fprintf(fp, ") {\n");
4805
4806 for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
4807 dt_printd(arg, fp, depth + 1);
4808 if (dnp->dn_alternate_body == NULL) {
4809 (void) fprintf(fp, "%*s}\n", depth * 8, "");
4810 } else {
4811 (void) fprintf(fp, "%*s} else {\n", depth * 8, "");
4812 for (arg = dnp->dn_alternate_body; arg != NULL;
4813 arg = arg->dn_list)
4814 dt_printd(arg, fp, depth + 1);
4815 (void) fprintf(fp, "%*s}\n", depth * 8, "");
4816 }
4817
4818 break;
4819
4820 default:
4821 (void) fprintf(fp, "/* bad node %p, kind %d */\n",
4822 (void *)dnp, dnp->dn_kind);
4823 }
4824 }
4825
4826 void
dt_node_printr(dt_node_t * dnp,FILE * fp,int depth)4827 dt_node_printr(dt_node_t *dnp, FILE *fp, int depth)
4828 {
4829 char n[DT_TYPE_NAMELEN], buf[BUFSIZ], a[8];
4830 const dtrace_syminfo_t *dts;
4831 const dt_idnode_t *inp;
4832 dt_node_t *arg;
4833
4834 (void) fprintf(fp, "%*s", depth * 2, "");
4835 (void) dt_attr_str(dnp->dn_attr, a, sizeof (a));
4836
4837 if (dnp->dn_ctfp != NULL && dnp->dn_type != CTF_ERR &&
4838 ctf_type_name(dnp->dn_ctfp, dnp->dn_type, n, sizeof (n)) != NULL) {
4839 (void) snprintf(buf, BUFSIZ, "type=<%s> attr=%s flags=", n, a);
4840 } else {
4841 (void) snprintf(buf, BUFSIZ, "type=<%ld> attr=%s flags=",
4842 dnp->dn_type, a);
4843 }
4844
4845 if (dnp->dn_flags != 0) {
4846 n[0] = '\0';
4847 if (dnp->dn_flags & DT_NF_SIGNED)
4848 (void) strcat(n, ",SIGN");
4849 if (dnp->dn_flags & DT_NF_COOKED)
4850 (void) strcat(n, ",COOK");
4851 if (dnp->dn_flags & DT_NF_REF)
4852 (void) strcat(n, ",REF");
4853 if (dnp->dn_flags & DT_NF_LVALUE)
4854 (void) strcat(n, ",LVAL");
4855 if (dnp->dn_flags & DT_NF_WRITABLE)
4856 (void) strcat(n, ",WRITE");
4857 if (dnp->dn_flags & DT_NF_BITFIELD)
4858 (void) strcat(n, ",BITF");
4859 if (dnp->dn_flags & DT_NF_USERLAND)
4860 (void) strcat(n, ",USER");
4861 (void) strcat(buf, n + 1);
4862 } else
4863 (void) strcat(buf, "0");
4864
4865 switch (dnp->dn_kind) {
4866 case DT_NODE_FREE:
4867 (void) fprintf(fp, "FREE <node %p>\n", (void *)dnp);
4868 break;
4869
4870 case DT_NODE_INT:
4871 (void) fprintf(fp, "INT 0x%llx (%s)\n",
4872 (u_longlong_t)dnp->dn_value, buf);
4873 break;
4874
4875 case DT_NODE_STRING:
4876 (void) fprintf(fp, "STRING \"%s\" (%s)\n", dnp->dn_string, buf);
4877 break;
4878
4879 case DT_NODE_IDENT:
4880 (void) fprintf(fp, "IDENT %s (%s)\n", dnp->dn_string, buf);
4881 break;
4882
4883 case DT_NODE_VAR:
4884 (void) fprintf(fp, "VARIABLE %s%s (%s)\n",
4885 (dnp->dn_ident->di_flags & DT_IDFLG_LOCAL) ? "this->" :
4886 (dnp->dn_ident->di_flags & DT_IDFLG_TLS) ? "self->" : "",
4887 dnp->dn_ident->di_name, buf);
4888
4889 if (dnp->dn_args != NULL)
4890 (void) fprintf(fp, "%*s[\n", depth * 2, "");
4891
4892 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4893 dt_node_printr(arg, fp, depth + 1);
4894 if (arg->dn_list != NULL)
4895 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4896 }
4897
4898 if (dnp->dn_args != NULL)
4899 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4900 break;
4901
4902 case DT_NODE_SYM:
4903 dts = dnp->dn_ident->di_data;
4904 (void) fprintf(fp, "SYMBOL %s`%s (%s)\n",
4905 dts->dts_object, dts->dts_name, buf);
4906 break;
4907
4908 case DT_NODE_TYPE:
4909 if (dnp->dn_string != NULL) {
4910 (void) fprintf(fp, "TYPE (%s) %s\n",
4911 buf, dnp->dn_string);
4912 } else
4913 (void) fprintf(fp, "TYPE (%s)\n", buf);
4914 break;
4915
4916 case DT_NODE_FUNC:
4917 (void) fprintf(fp, "FUNC %s (%s)\n",
4918 dnp->dn_ident->di_name, buf);
4919
4920 for (arg = dnp->dn_args; arg != NULL; arg = arg->dn_list) {
4921 dt_node_printr(arg, fp, depth + 1);
4922 if (arg->dn_list != NULL)
4923 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4924 }
4925 break;
4926
4927 case DT_NODE_OP1:
4928 (void) fprintf(fp, "OP1 %s (%s)\n", opstr(dnp->dn_op), buf);
4929 dt_node_printr(dnp->dn_child, fp, depth + 1);
4930 break;
4931
4932 case DT_NODE_OP2:
4933 (void) fprintf(fp, "OP2 %s (%s)\n", opstr(dnp->dn_op), buf);
4934 dt_node_printr(dnp->dn_left, fp, depth + 1);
4935 dt_node_printr(dnp->dn_right, fp, depth + 1);
4936 if (dnp->dn_op == DT_TOK_LBRAC) {
4937 dt_node_t *ln = dnp->dn_right;
4938 while (ln->dn_list != NULL) {
4939 dt_node_printr(ln->dn_list, fp, depth + 1);
4940 ln = ln->dn_list;
4941 }
4942 }
4943 break;
4944
4945 case DT_NODE_OP3:
4946 (void) fprintf(fp, "OP3 (%s)\n", buf);
4947 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4948 (void) fprintf(fp, "%*s?\n", depth * 2, "");
4949 dt_node_printr(dnp->dn_left, fp, depth + 1);
4950 (void) fprintf(fp, "%*s:\n", depth * 2, "");
4951 dt_node_printr(dnp->dn_right, fp, depth + 1);
4952 break;
4953
4954 case DT_NODE_DEXPR:
4955 case DT_NODE_DFUNC:
4956 (void) fprintf(fp, "D EXPRESSION attr=%s\n", a);
4957 dt_node_printr(dnp->dn_expr, fp, depth + 1);
4958 break;
4959
4960 case DT_NODE_AGG:
4961 (void) fprintf(fp, "AGGREGATE @%s attr=%s [\n",
4962 dnp->dn_ident->di_name, a);
4963
4964 for (arg = dnp->dn_aggtup; arg != NULL; arg = arg->dn_list) {
4965 dt_node_printr(arg, fp, depth + 1);
4966 if (arg->dn_list != NULL)
4967 (void) fprintf(fp, "%*s,\n", depth * 2, "");
4968 }
4969
4970 if (dnp->dn_aggfun) {
4971 (void) fprintf(fp, "%*s] = ", depth * 2, "");
4972 dt_node_printr(dnp->dn_aggfun, fp, depth + 1);
4973 } else
4974 (void) fprintf(fp, "%*s]\n", depth * 2, "");
4975
4976 if (dnp->dn_aggfun)
4977 (void) fprintf(fp, "%*s)\n", depth * 2, "");
4978 break;
4979
4980 case DT_NODE_PDESC:
4981 (void) fprintf(fp, "PDESC %s:%s:%s:%s [%u]\n",
4982 dnp->dn_desc->dtpd_provider, dnp->dn_desc->dtpd_mod,
4983 dnp->dn_desc->dtpd_func, dnp->dn_desc->dtpd_name,
4984 dnp->dn_desc->dtpd_id);
4985 break;
4986
4987 case DT_NODE_CLAUSE:
4988 (void) fprintf(fp, "CLAUSE attr=%s\n", a);
4989
4990 for (arg = dnp->dn_pdescs; arg != NULL; arg = arg->dn_list)
4991 dt_node_printr(arg, fp, depth + 1);
4992
4993 (void) fprintf(fp, "%*sCTXATTR %s\n", depth * 2, "",
4994 dt_attr_str(dnp->dn_ctxattr, a, sizeof (a)));
4995
4996 if (dnp->dn_pred != NULL) {
4997 (void) fprintf(fp, "%*sPREDICATE /\n", depth * 2, "");
4998 dt_node_printr(dnp->dn_pred, fp, depth + 1);
4999 (void) fprintf(fp, "%*s/\n", depth * 2, "");
5000 }
5001
5002 for (arg = dnp->dn_acts; arg != NULL; arg = arg->dn_list)
5003 dt_node_printr(arg, fp, depth + 1);
5004 (void) fprintf(fp, "\n");
5005 break;
5006
5007 case DT_NODE_INLINE:
5008 inp = dnp->dn_ident->di_iarg;
5009
5010 (void) fprintf(fp, "INLINE %s (%s)\n",
5011 dnp->dn_ident->di_name, buf);
5012 dt_node_printr(inp->din_root, fp, depth + 1);
5013 break;
5014
5015 case DT_NODE_MEMBER:
5016 (void) fprintf(fp, "MEMBER %s (%s)\n", dnp->dn_membname, buf);
5017 if (dnp->dn_membexpr)
5018 dt_node_printr(dnp->dn_membexpr, fp, depth + 1);
5019 break;
5020
5021 case DT_NODE_XLATOR:
5022 (void) fprintf(fp, "XLATOR (%s)", buf);
5023
5024 if (ctf_type_name(dnp->dn_xlator->dx_src_ctfp,
5025 dnp->dn_xlator->dx_src_type, n, sizeof (n)) != NULL)
5026 (void) fprintf(fp, " from <%s>", n);
5027
5028 if (ctf_type_name(dnp->dn_xlator->dx_dst_ctfp,
5029 dnp->dn_xlator->dx_dst_type, n, sizeof (n)) != NULL)
5030 (void) fprintf(fp, " to <%s>", n);
5031
5032 (void) fprintf(fp, "\n");
5033
5034 for (arg = dnp->dn_members; arg != NULL; arg = arg->dn_list)
5035 dt_node_printr(arg, fp, depth + 1);
5036 break;
5037
5038 case DT_NODE_PROBE:
5039 (void) fprintf(fp, "PROBE %s\n", dnp->dn_ident->di_name);
5040 break;
5041
5042 case DT_NODE_PROVIDER:
5043 (void) fprintf(fp, "PROVIDER %s (%s)\n",
5044 dnp->dn_provname, dnp->dn_provred ? "redecl" : "decl");
5045 for (arg = dnp->dn_probes; arg != NULL; arg = arg->dn_list)
5046 dt_node_printr(arg, fp, depth + 1);
5047 break;
5048
5049 case DT_NODE_PROG:
5050 (void) fprintf(fp, "PROGRAM attr=%s\n", a);
5051 for (arg = dnp->dn_list; arg != NULL; arg = arg->dn_list)
5052 dt_node_printr(arg, fp, depth + 1);
5053 break;
5054
5055 case DT_NODE_IF:
5056 (void) fprintf(fp, "IF attr=%s CONDITION:\n", a);
5057
5058 dt_node_printr(dnp->dn_conditional, fp, depth + 1);
5059
5060 (void) fprintf(fp, "%*sIF BODY: \n", depth * 2, "");
5061 for (arg = dnp->dn_body; arg != NULL; arg = arg->dn_list)
5062 dt_node_printr(arg, fp, depth + 1);
5063
5064 if (dnp->dn_alternate_body != NULL) {
5065 (void) fprintf(fp, "%*sIF ELSE: \n", depth * 2, "");
5066 for (arg = dnp->dn_alternate_body; arg != NULL;
5067 arg = arg->dn_list)
5068 dt_node_printr(arg, fp, depth + 1);
5069 }
5070
5071 break;
5072
5073 default:
5074 (void) fprintf(fp, "<bad node %p, kind %d>\n",
5075 (void *)dnp, dnp->dn_kind);
5076 }
5077 }
5078
5079 int
dt_node_root(dt_node_t * dnp)5080 dt_node_root(dt_node_t *dnp)
5081 {
5082 yypcb->pcb_root = dnp;
5083 return (0);
5084 }
5085
5086 /*PRINTFLIKE3*/
5087 void
dnerror(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)5088 dnerror(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
5089 {
5090 int oldlineno = yylineno;
5091 va_list ap;
5092
5093 yylineno = dnp->dn_line;
5094
5095 va_start(ap, format);
5096 xyvwarn(tag, format, ap);
5097 va_end(ap);
5098
5099 yylineno = oldlineno;
5100 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5101 }
5102
5103 /*PRINTFLIKE3*/
5104 void
dnwarn(const dt_node_t * dnp,dt_errtag_t tag,const char * format,...)5105 dnwarn(const dt_node_t *dnp, dt_errtag_t tag, const char *format, ...)
5106 {
5107 int oldlineno = yylineno;
5108 va_list ap;
5109
5110 yylineno = dnp->dn_line;
5111
5112 va_start(ap, format);
5113 xyvwarn(tag, format, ap);
5114 va_end(ap);
5115
5116 yylineno = oldlineno;
5117 }
5118
5119 /*PRINTFLIKE2*/
5120 void
xyerror(dt_errtag_t tag,const char * format,...)5121 xyerror(dt_errtag_t tag, const char *format, ...)
5122 {
5123 va_list ap;
5124
5125 va_start(ap, format);
5126 xyvwarn(tag, format, ap);
5127 va_end(ap);
5128
5129 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5130 }
5131
5132 /*PRINTFLIKE2*/
5133 void
xywarn(dt_errtag_t tag,const char * format,...)5134 xywarn(dt_errtag_t tag, const char *format, ...)
5135 {
5136 va_list ap;
5137
5138 va_start(ap, format);
5139 xyvwarn(tag, format, ap);
5140 va_end(ap);
5141 }
5142
5143 void
xyvwarn(dt_errtag_t tag,const char * format,va_list ap)5144 xyvwarn(dt_errtag_t tag, const char *format, va_list ap)
5145 {
5146 if (yypcb == NULL)
5147 return; /* compiler is not currently active: act as a no-op */
5148
5149 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(tag), yypcb->pcb_region,
5150 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
5151 }
5152
5153 /*PRINTFLIKE1*/
5154 void
yyerror(const char * format,...)5155 yyerror(const char *format, ...)
5156 {
5157 va_list ap;
5158
5159 va_start(ap, format);
5160 yyvwarn(format, ap);
5161 va_end(ap);
5162
5163 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5164 }
5165
5166 /*PRINTFLIKE1*/
5167 void
yywarn(const char * format,...)5168 yywarn(const char *format, ...)
5169 {
5170 va_list ap;
5171
5172 va_start(ap, format);
5173 yyvwarn(format, ap);
5174 va_end(ap);
5175 }
5176
5177 void
yyvwarn(const char * format,va_list ap)5178 yyvwarn(const char *format, va_list ap)
5179 {
5180 if (yypcb == NULL)
5181 return; /* compiler is not currently active: act as a no-op */
5182
5183 dt_set_errmsg(yypcb->pcb_hdl, dt_errtag(D_SYNTAX), yypcb->pcb_region,
5184 yypcb->pcb_filetag, yypcb->pcb_fileptr ? yylineno : 0, format, ap);
5185
5186 if (strchr(format, '\n') == NULL) {
5187 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
5188 size_t len = strlen(dtp->dt_errmsg);
5189 char *p, *s = dtp->dt_errmsg + len;
5190 size_t n = sizeof (dtp->dt_errmsg) - len;
5191
5192 if (yytext[0] == '\0')
5193 (void) snprintf(s, n, " near end of input");
5194 else if (yytext[0] == '\n')
5195 (void) snprintf(s, n, " near end of line");
5196 else {
5197 if ((p = strchr(yytext, '\n')) != NULL)
5198 *p = '\0'; /* crop at newline */
5199 (void) snprintf(s, n, " near \"%s\"", yytext);
5200 }
5201 }
5202 }
5203
5204 void
yylabel(const char * label)5205 yylabel(const char *label)
5206 {
5207 dt_dprintf("set label to <%s>\n", label ? label : "NULL");
5208 yypcb->pcb_region = label;
5209 }
5210
5211 int
yywrap(void)5212 yywrap(void)
5213 {
5214 return (1); /* indicate that lex should return a zero token for EOF */
5215 }
5216