xref: /titanic_41/usr/src/lib/libdtrace/common/dt_cc.c (revision 5aefb6555731130ca4fd295960123d71f2d21fe8)
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  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * DTrace D Language Compiler
32  *
33  * The code in this source file implements the main engine for the D language
34  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
35  * compiler operates on either stdio FILEs or in-memory strings as its input
36  * and can produce either dtrace_prog_t structures from a D program or a single
37  * dtrace_difo_t structure from a D expression.  Multiple entry points are
38  * provided as wrappers around dt_compile() for the various input/output pairs.
39  * The compiler itself is implemented across the following source files:
40  *
41  * dt_lex.l - lex scanner
42  * dt_grammar.y - yacc grammar
43  * dt_parser.c - parse tree creation and semantic checking
44  * dt_decl.c - declaration stack processing
45  * dt_xlator.c - D translator lookup and creation
46  * dt_ident.c - identifier and symbol table routines
47  * dt_pragma.c - #pragma processing and D pragmas
48  * dt_printf.c - D printf() and printa() argument checking and processing
49  * dt_cc.c - compiler driver and dtrace_prog_t construction
50  * dt_cg.c - DIF code generator
51  * dt_as.c - DIF assembler
52  * dt_dof.c - dtrace_prog_t -> DOF conversion
53  *
54  * Several other source files provide collections of utility routines used by
55  * these major files.  The compiler itself is implemented in multiple passes:
56  *
57  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
58  *     and parse tree nodes are constructed using the routines in dt_parser.c.
59  *     This node construction pass is described further in dt_parser.c.
60  *
61  * (2) The parse tree is "cooked" by assigning each clause a context (see the
62  *     routine dt_setcontext(), below) based on its probe description and then
63  *     recursively descending the tree performing semantic checking.  The cook
64  *     routines are also implemented in dt_parser.c and described there.
65  *
66  * (3) For actions that are DIF expression statements, the DIF code generator
67  *     and assembler are invoked to create a finished DIFO for the statement.
68  *
69  * (4) The dtrace_prog_t data structures for the program clauses and actions
70  *     are built, containing pointers to any DIFOs created in step (3).
71  *
72  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
73  *     into DOF format for use in anonymous tracing or enabling in the kernel.
74  *
75  * In the implementation, steps 2-4 are intertwined in that they are performed
76  * in order for each clause as part of a loop that executes over the clauses.
77  *
78  * The D compiler currently implements nearly no optimization.  The compiler
79  * implements integer constant folding as part of pass (1), and a set of very
80  * simple peephole optimizations as part of pass (3).  As with any C compiler,
81  * a large number of optimizations are possible on both the intermediate data
82  * structures and the generated DIF code.  These possibilities should be
83  * investigated in the context of whether they will have any substantive effect
84  * on the overall DTrace probe effect before they are undertaken.
85  */
86 
87 #include <sys/types.h>
88 #include <sys/wait.h>
89 
90 #include <assert.h>
91 #include <strings.h>
92 #include <signal.h>
93 #include <unistd.h>
94 #include <stdlib.h>
95 #include <stdio.h>
96 #include <errno.h>
97 #include <ucontext.h>
98 #include <limits.h>
99 #include <ctype.h>
100 #include <dirent.h>
101 #include <dt_module.h>
102 #include <dt_program.h>
103 #include <dt_provider.h>
104 #include <dt_printf.h>
105 #include <dt_pid.h>
106 #include <dt_grammar.h>
107 #include <dt_ident.h>
108 #include <dt_string.h>
109 #include <dt_impl.h>
110 
111 static const dtrace_diftype_t dt_void_rtype = {
112 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
113 };
114 
115 static const dtrace_diftype_t dt_int_rtype = {
116 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
117 };
118 
119 /*ARGSUSED*/
120 static int
121 dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
122 {
123 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
124 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
125 	return (0);
126 }
127 
128 /*ARGSUSED*/
129 static int
130 dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
131 {
132 	yylineno = idp->di_lineno;
133 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
134 	return (0);
135 }
136 
137 static dtrace_stmtdesc_t *
138 dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
139     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
140 {
141 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
142 
143 	if (sdp == NULL)
144 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
145 
146 	assert(yypcb->pcb_stmt == NULL);
147 	yypcb->pcb_stmt = sdp;
148 
149 	sdp->dtsd_descattr = descattr;
150 	sdp->dtsd_stmtattr = stmtattr;
151 
152 	return (sdp);
153 }
154 
155 static dtrace_actdesc_t *
156 dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
157 {
158 	dtrace_actdesc_t *new;
159 
160 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
161 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
162 
163 	return (new);
164 }
165 
166 /*
167  * Utility function to determine if a given action description is destructive.
168  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
169  */
170 static int
171 dt_action_destructive(const dtrace_actdesc_t *ap)
172 {
173 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
174 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
175 }
176 
177 static void
178 dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
179 {
180 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
181 	dtrace_actdesc_t *ap, *tap;
182 	int commit = 0;
183 	int speculate = 0;
184 	int datarec = 0;
185 
186 	/*
187 	 * Make sure that the new statement jibes with the rest of the ECB.
188 	 */
189 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
190 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
191 			if (commit) {
192 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
193 				    "not follow commit( )\n");
194 			}
195 
196 			if (datarec) {
197 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
198 				    "not follow data-recording action(s)\n");
199 			}
200 
201 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
202 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
203 					continue;
204 
205 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
206 				    "may not follow commit( )\n");
207 			}
208 
209 			commit = 1;
210 			continue;
211 		}
212 
213 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
214 			if (speculate) {
215 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
216 				    "not follow speculate( )\n");
217 			}
218 
219 			if (commit) {
220 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
221 				    "not follow commit( )\n");
222 			}
223 
224 			if (datarec) {
225 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
226 				    "not follow data-recording action(s)\n");
227 			}
228 
229 			speculate = 1;
230 			continue;
231 		}
232 
233 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
234 			if (speculate) {
235 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
236 				    "may not follow speculate( )\n");
237 			}
238 
239 			datarec = 1;
240 			continue;
241 		}
242 
243 		if (speculate) {
244 			if (dt_action_destructive(ap)) {
245 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
246 				    "may not follow speculate( )\n");
247 			}
248 
249 			if (ap->dtad_kind == DTRACEACT_EXIT) {
250 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
251 				    "follow speculate( )\n");
252 			}
253 		}
254 
255 		/*
256 		 * Exclude all non data-recording actions.
257 		 */
258 		if (dt_action_destructive(ap) ||
259 		    ap->dtad_kind == DTRACEACT_DISCARD)
260 			continue;
261 
262 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
263 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
264 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
265 			continue;
266 
267 		if (commit) {
268 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
269 			    "may not follow commit( )\n");
270 		}
271 
272 		if (!speculate)
273 			datarec = 1;
274 	}
275 
276 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
277 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
278 
279 	if (yypcb->pcb_stmt == sdp)
280 		yypcb->pcb_stmt = NULL;
281 }
282 
283 /*
284  * For the first element of an aggregation tuple or for printa(), we create a
285  * simple DIF program that simply returns the immediate value that is the ID
286  * of the aggregation itself.  This could be optimized in the future by
287  * creating a new in-kernel dtad_kind that just returns an integer.
288  */
289 static void
290 dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
291 {
292 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
293 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
294 
295 	if (dp == NULL)
296 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
297 
298 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
299 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
300 
301 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
302 		dt_difo_free(dtp, dp);
303 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
304 	}
305 
306 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
307 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
308 	dp->dtdo_len = 2;
309 	dp->dtdo_inttab[0] = id;
310 	dp->dtdo_intlen = 1;
311 	dp->dtdo_rtype = dt_int_rtype;
312 
313 	ap->dtad_difo = dp;
314 	ap->dtad_kind = kind;
315 }
316 
317 static void
318 dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
319 {
320 	dt_ident_t *aid;
321 	dtrace_actdesc_t *ap;
322 	dt_node_t *anp;
323 
324 	char n[DT_TYPE_NAMELEN];
325 	int argc = 0;
326 
327 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
328 		argc++; /* count up arguments for error messages below */
329 
330 	if (argc != 1) {
331 		dnerror(dnp, D_CLEAR_PROTO,
332 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
333 		    dnp->dn_ident->di_name, argc);
334 	}
335 
336 	anp = dnp->dn_args;
337 	assert(anp != NULL);
338 
339 	if (anp->dn_kind != DT_NODE_AGG) {
340 		dnerror(dnp, D_CLEAR_AGGARG,
341 		    "%s( ) argument #1 is incompatible with prototype:\n"
342 		    "\tprototype: aggregation\n\t argument: %s\n",
343 		    dnp->dn_ident->di_name,
344 		    dt_node_type_name(anp, n, sizeof (n)));
345 	}
346 
347 	aid = anp->dn_ident;
348 
349 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
350 		dnerror(dnp, D_CLEAR_AGGBAD,
351 		    "undefined aggregation: @%s\n", aid->di_name);
352 	}
353 
354 	ap = dt_stmt_action(dtp, sdp);
355 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
356 	ap->dtad_arg = DT_ACT_CLEAR;
357 }
358 
359 static void
360 dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
361 {
362 	dt_ident_t *aid;
363 	dtrace_actdesc_t *ap;
364 	dt_node_t *anp, *normal;
365 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
366 
367 	char n[DT_TYPE_NAMELEN];
368 	int argc = 0;
369 
370 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
371 		argc++; /* count up arguments for error messages below */
372 
373 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
374 		dnerror(dnp, D_NORMALIZE_PROTO,
375 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
376 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
377 	}
378 
379 	anp = dnp->dn_args;
380 	assert(anp != NULL);
381 
382 	if (anp->dn_kind != DT_NODE_AGG) {
383 		dnerror(dnp, D_NORMALIZE_AGGARG,
384 		    "%s( ) argument #1 is incompatible with prototype:\n"
385 		    "\tprototype: aggregation\n\t argument: %s\n",
386 		    dnp->dn_ident->di_name,
387 		    dt_node_type_name(anp, n, sizeof (n)));
388 	}
389 
390 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
391 		dnerror(dnp, D_NORMALIZE_SCALAR,
392 		    "%s( ) argument #2 must be of scalar type\n",
393 		    dnp->dn_ident->di_name);
394 	}
395 
396 	aid = anp->dn_ident;
397 
398 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
399 		dnerror(dnp, D_NORMALIZE_AGGBAD,
400 		    "undefined aggregation: @%s\n", aid->di_name);
401 	}
402 
403 	ap = dt_stmt_action(dtp, sdp);
404 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
405 
406 	if (denormal) {
407 		ap->dtad_arg = DT_ACT_DENORMALIZE;
408 		return;
409 	}
410 
411 	ap->dtad_arg = DT_ACT_NORMALIZE;
412 
413 	assert(normal != NULL);
414 	ap = dt_stmt_action(dtp, sdp);
415 	dt_cg(yypcb, normal);
416 
417 	ap->dtad_difo = dt_as(yypcb);
418 	ap->dtad_kind = DTRACEACT_LIBACT;
419 	ap->dtad_arg = DT_ACT_NORMALIZE;
420 }
421 
422 static void
423 dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
424 {
425 	dt_ident_t *aid;
426 	dtrace_actdesc_t *ap;
427 	dt_node_t *anp, *trunc;
428 
429 	char n[DT_TYPE_NAMELEN];
430 	int argc = 0;
431 
432 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
433 		argc++; /* count up arguments for error messages below */
434 
435 	if (argc > 2 || argc < 1) {
436 		dnerror(dnp, D_TRUNC_PROTO,
437 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
438 		    dnp->dn_ident->di_name, argc,
439 		    argc < 1 ? "at least 1" : "no more than 2");
440 	}
441 
442 	anp = dnp->dn_args;
443 	assert(anp != NULL);
444 	trunc = anp->dn_list;
445 
446 	if (anp->dn_kind != DT_NODE_AGG) {
447 		dnerror(dnp, D_TRUNC_AGGARG,
448 		    "%s( ) argument #1 is incompatible with prototype:\n"
449 		    "\tprototype: aggregation\n\t argument: %s\n",
450 		    dnp->dn_ident->di_name,
451 		    dt_node_type_name(anp, n, sizeof (n)));
452 	}
453 
454 	if (argc == 2) {
455 		assert(trunc != NULL);
456 		if (!dt_node_is_scalar(trunc)) {
457 			dnerror(dnp, D_TRUNC_SCALAR,
458 			    "%s( ) argument #2 must be of scalar type\n",
459 			    dnp->dn_ident->di_name);
460 		}
461 	}
462 
463 	aid = anp->dn_ident;
464 
465 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
466 		dnerror(dnp, D_TRUNC_AGGBAD,
467 		    "undefined aggregation: @%s\n", aid->di_name);
468 	}
469 
470 	ap = dt_stmt_action(dtp, sdp);
471 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
472 	ap->dtad_arg = DT_ACT_TRUNC;
473 
474 	ap = dt_stmt_action(dtp, sdp);
475 
476 	if (argc == 1) {
477 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
478 	} else {
479 		assert(trunc != NULL);
480 		dt_cg(yypcb, trunc);
481 		ap->dtad_difo = dt_as(yypcb);
482 		ap->dtad_kind = DTRACEACT_LIBACT;
483 	}
484 
485 	ap->dtad_arg = DT_ACT_TRUNC;
486 }
487 
488 static void
489 dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
490 {
491 	dt_ident_t *aid, *fid;
492 	dtrace_actdesc_t *ap;
493 	const char *format;
494 	dt_node_t *anp, *proto = NULL;
495 
496 	char n[DT_TYPE_NAMELEN];
497 	int argc = 0, argr = 0;
498 
499 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
500 		argc++; /* count up arguments for error messages below */
501 
502 	switch (dnp->dn_args->dn_kind) {
503 	case DT_NODE_STRING:
504 		format = dnp->dn_args->dn_string;
505 		anp = dnp->dn_args->dn_list;
506 		argr = 2;
507 		break;
508 	case DT_NODE_AGG:
509 		format = NULL;
510 		anp = dnp->dn_args;
511 		argr = 1;
512 		break;
513 	default:
514 		format = NULL;
515 		anp = dnp->dn_args;
516 		argr = 1;
517 	}
518 
519 	if (argc < argr) {
520 		dnerror(dnp, D_PRINTA_PROTO,
521 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
522 		    dnp->dn_ident->di_name, argc, argr);
523 	}
524 
525 	assert(anp != NULL);
526 
527 	while (anp != NULL) {
528 		if (anp->dn_kind != DT_NODE_AGG) {
529 			dnerror(dnp, D_PRINTA_AGGARG,
530 			    "%s( ) argument #%d is incompatible with "
531 			    "prototype:\n\tprototype: aggregation\n"
532 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
533 			    dt_node_type_name(anp, n, sizeof (n)));
534 		}
535 
536 		aid = anp->dn_ident;
537 		fid = aid->di_iarg;
538 
539 		if (aid->di_gen == dtp->dt_gen &&
540 		    !(aid->di_flags & DT_IDFLG_MOD)) {
541 			dnerror(dnp, D_PRINTA_AGGBAD,
542 			    "undefined aggregation: @%s\n", aid->di_name);
543 		}
544 
545 		/*
546 		 * If we have multiple aggregations, we must be sure that
547 		 * their key signatures match.
548 		 */
549 		if (proto != NULL) {
550 			dt_printa_validate(proto, anp);
551 		} else {
552 			proto = anp;
553 		}
554 
555 		if (format != NULL) {
556 			yylineno = dnp->dn_line;
557 
558 			sdp->dtsd_fmtdata =
559 			    dt_printf_create(yypcb->pcb_hdl, format);
560 			dt_printf_validate(sdp->dtsd_fmtdata,
561 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
562 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
563 			format = NULL;
564 		}
565 
566 		ap = dt_stmt_action(dtp, sdp);
567 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
568 
569 		anp = anp->dn_list;
570 		argr++;
571 	}
572 }
573 
574 static void
575 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
576     dtrace_actkind_t kind)
577 {
578 	dt_node_t *anp, *arg1;
579 	dtrace_actdesc_t *ap = NULL;
580 	char n[DT_TYPE_NAMELEN], *str;
581 
582 	assert(DTRACEACT_ISPRINTFLIKE(kind));
583 
584 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
585 		dnerror(dnp, D_PRINTF_ARG_FMT,
586 		    "%s( ) argument #1 is incompatible with prototype:\n"
587 		    "\tprototype: string constant\n\t argument: %s\n",
588 		    dnp->dn_ident->di_name,
589 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
590 	}
591 
592 	arg1 = dnp->dn_args->dn_list;
593 	yylineno = dnp->dn_line;
594 	str = dnp->dn_args->dn_string;
595 
596 
597 	/*
598 	 * If this is an freopen(), we use an empty string to denote that
599 	 * stdout should be restored.  For other printf()-like actions, an
600 	 * empty format string is illegal:  an empty format string would
601 	 * result in malformed DOF, and the compiler thus flags an empty
602 	 * format string as a compile-time error.  To avoid propagating the
603 	 * freopen() special case throughout the system, we simply transpose
604 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
605 	 * denotes that stdout should be restored.
606 	 */
607 	if (kind == DTRACEACT_FREOPEN) {
608 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
609 			/*
610 			 * Our sentinel is always an invalid argument to
611 			 * freopen(), but if it's been manually specified, we
612 			 * must fail now instead of when the freopen() is
613 			 * actually evaluated.
614 			 */
615 			dnerror(dnp, D_FREOPEN_INVALID,
616 			    "%s( ) argument #1 cannot be \"%s\"\n",
617 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
618 		}
619 
620 		if (str[0] == '\0')
621 			str = DT_FREOPEN_RESTORE;
622 	}
623 
624 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
625 
626 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
627 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
628 
629 	if (arg1 == NULL) {
630 		dif_instr_t *dbuf;
631 		dtrace_difo_t *dp;
632 
633 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
634 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
635 			dt_free(dtp, dbuf);
636 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
637 		}
638 
639 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
640 
641 		dp->dtdo_buf = dbuf;
642 		dp->dtdo_len = 1;
643 		dp->dtdo_rtype = dt_int_rtype;
644 
645 		ap = dt_stmt_action(dtp, sdp);
646 		ap->dtad_difo = dp;
647 		ap->dtad_kind = kind;
648 		return;
649 	}
650 
651 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
652 		ap = dt_stmt_action(dtp, sdp);
653 		dt_cg(yypcb, anp);
654 		ap->dtad_difo = dt_as(yypcb);
655 		ap->dtad_kind = kind;
656 	}
657 }
658 
659 static void
660 dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
661 {
662 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
663 
664 	if (dt_node_is_void(dnp->dn_args)) {
665 		dnerror(dnp->dn_args, D_TRACE_VOID,
666 		    "trace( ) may not be applied to a void expression\n");
667 	}
668 
669 	if (dt_node_is_dynamic(dnp->dn_args)) {
670 		dnerror(dnp->dn_args, D_TRACE_DYN,
671 		    "trace( ) may not be applied to a dynamic expression\n");
672 	}
673 
674 	dt_cg(yypcb, dnp->dn_args);
675 	ap->dtad_difo = dt_as(yypcb);
676 	ap->dtad_kind = DTRACEACT_DIFEXPR;
677 }
678 
679 static void
680 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
681 {
682 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
683 
684 	dt_node_t *addr = dnp->dn_args;
685 	dt_node_t *size = dnp->dn_args->dn_list;
686 
687 	char n[DT_TYPE_NAMELEN];
688 
689 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
690 		dnerror(addr, D_TRACEMEM_ADDR,
691 		    "tracemem( ) argument #1 is incompatible with "
692 		    "prototype:\n\tprototype: pointer or integer\n"
693 		    "\t argument: %s\n",
694 		    dt_node_type_name(addr, n, sizeof (n)));
695 	}
696 
697 	if (dt_node_is_posconst(size) == 0) {
698 		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
699 		    "be a non-zero positive integral constant expression\n");
700 	}
701 
702 	dt_cg(yypcb, addr);
703 	ap->dtad_difo = dt_as(yypcb);
704 	ap->dtad_kind = DTRACEACT_DIFEXPR;
705 
706 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
707 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
708 }
709 
710 static void
711 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
712 {
713 	ap->dtad_kind = DTRACEACT_STACK;
714 
715 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
716 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
717 	} else {
718 		ap->dtad_arg = 0;
719 	}
720 
721 	if (arg0 != NULL) {
722 		if (arg0->dn_list != NULL) {
723 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
724 			    "mismatch: too many arguments\n");
725 		}
726 
727 		if (dt_node_is_posconst(arg0) == 0) {
728 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
729 			    "non-zero positive integral constant expression\n");
730 		}
731 
732 		ap->dtad_arg = arg0->dn_value;
733 	}
734 }
735 
736 static void
737 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
738 {
739 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
740 	dt_action_stack_args(dtp, ap, dnp->dn_args);
741 }
742 
743 static void
744 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
745 {
746 	uint32_t nframes = 0;
747 	uint32_t strsize = 0;	/* default string table size */
748 	dt_node_t *arg0 = dnp->dn_args;
749 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
750 
751 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
752 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
753 
754 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
755 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
756 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
757 
758 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
759 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
760 
761 		ap->dtad_kind = DTRACEACT_JSTACK;
762 	} else {
763 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
764 
765 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
766 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
767 
768 		ap->dtad_kind = DTRACEACT_USTACK;
769 	}
770 
771 	if (arg0 != NULL) {
772 		if (!dt_node_is_posconst(arg0)) {
773 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
774 			    "must be a non-zero positive integer constant\n");
775 		}
776 		nframes = (uint32_t)arg0->dn_value;
777 	}
778 
779 	if (arg1 != NULL) {
780 		if (arg1->dn_kind != DT_NODE_INT ||
781 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
782 		    (int64_t)arg1->dn_value < 0)) {
783 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
784 			    "must be a positive integer constant\n");
785 		}
786 
787 		if (arg1->dn_list != NULL) {
788 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
789 			    "mismatch: too many arguments\n");
790 		}
791 
792 		strsize = (uint32_t)arg1->dn_value;
793 	}
794 
795 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
796 }
797 
798 static void
799 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
800 {
801 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
802 	dt_action_ustack_args(dtp, ap, dnp);
803 }
804 
805 static void
806 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
807 {
808 	dtrace_actdesc_t *ap;
809 	dt_node_t *arg0, *arg1;
810 
811 	/*
812 	 * The prototype guarantees that we are called with either one or
813 	 * two arguments, and that any arguments that are present are strings.
814 	 */
815 	arg0 = dnp->dn_args;
816 	arg1 = arg0->dn_list;
817 
818 	ap = dt_stmt_action(dtp, sdp);
819 	dt_cg(yypcb, arg0);
820 	ap->dtad_difo = dt_as(yypcb);
821 	ap->dtad_kind = DTRACEACT_LIBACT;
822 	ap->dtad_arg = DT_ACT_SETOPT;
823 
824 	ap = dt_stmt_action(dtp, sdp);
825 
826 	if (arg1 == NULL) {
827 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
828 	} else {
829 		dt_cg(yypcb, arg1);
830 		ap->dtad_difo = dt_as(yypcb);
831 		ap->dtad_kind = DTRACEACT_LIBACT;
832 	}
833 
834 	ap->dtad_arg = DT_ACT_SETOPT;
835 }
836 
837 /*ARGSUSED*/
838 static void
839 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
840     dt_node_t *dnp, dtrace_actkind_t kind)
841 {
842 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
843 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
844 	    kind == DTRACEACT_UADDR);
845 
846 	dt_cg(yypcb, dnp);
847 	ap->dtad_difo = dt_as(yypcb);
848 	ap->dtad_kind = kind;
849 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
850 }
851 
852 static void
853 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
854     dtrace_actkind_t kind)
855 {
856 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
857 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
858 }
859 
860 /*ARGSUSED*/
861 static void
862 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
863 {
864 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
865 
866 	/*
867 	 * Library actions need a DIFO that serves as an argument.  As
868 	 * ftruncate() doesn't take an argument, we generate the constant 0
869 	 * in a DIFO; this constant will be ignored when the ftruncate() is
870 	 * processed.
871 	 */
872 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
873 	ap->dtad_arg = DT_ACT_FTRUNCATE;
874 }
875 
876 /*ARGSUSED*/
877 static void
878 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
879 {
880 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
881 
882 	ap->dtad_kind = DTRACEACT_STOP;
883 	ap->dtad_arg = 0;
884 }
885 
886 /*ARGSUSED*/
887 static void
888 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
889 {
890 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
891 
892 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
893 	ap->dtad_arg = 0;
894 }
895 
896 /*ARGSUSED*/
897 static void
898 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
899 {
900 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
901 
902 	ap->dtad_kind = DTRACEACT_PANIC;
903 	ap->dtad_arg = 0;
904 }
905 
906 static void
907 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
908 {
909 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
910 
911 	dt_cg(yypcb, dnp->dn_args);
912 	ap->dtad_difo = dt_as(yypcb);
913 	ap->dtad_kind = DTRACEACT_CHILL;
914 }
915 
916 static void
917 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
918 {
919 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
920 
921 	dt_cg(yypcb, dnp->dn_args);
922 	ap->dtad_difo = dt_as(yypcb);
923 	ap->dtad_kind = DTRACEACT_RAISE;
924 }
925 
926 static void
927 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
928 {
929 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
930 
931 	dt_cg(yypcb, dnp->dn_args);
932 	ap->dtad_difo = dt_as(yypcb);
933 	ap->dtad_kind = DTRACEACT_EXIT;
934 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
935 }
936 
937 static void
938 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
939 {
940 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
941 
942 	dt_cg(yypcb, dnp->dn_args);
943 	ap->dtad_difo = dt_as(yypcb);
944 	ap->dtad_kind = DTRACEACT_SPECULATE;
945 }
946 
947 static void
948 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
949 {
950 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
951 
952 	dt_cg(yypcb, dnp->dn_args);
953 	ap->dtad_difo = dt_as(yypcb);
954 	ap->dtad_kind = DTRACEACT_COMMIT;
955 }
956 
957 static void
958 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
959 {
960 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
961 
962 	dt_cg(yypcb, dnp->dn_args);
963 	ap->dtad_difo = dt_as(yypcb);
964 	ap->dtad_kind = DTRACEACT_DISCARD;
965 }
966 
967 static void
968 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
969 {
970 	switch (dnp->dn_expr->dn_ident->di_id) {
971 	case DT_ACT_BREAKPOINT:
972 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
973 		break;
974 	case DT_ACT_CHILL:
975 		dt_action_chill(dtp, dnp->dn_expr, sdp);
976 		break;
977 	case DT_ACT_CLEAR:
978 		dt_action_clear(dtp, dnp->dn_expr, sdp);
979 		break;
980 	case DT_ACT_COMMIT:
981 		dt_action_commit(dtp, dnp->dn_expr, sdp);
982 		break;
983 	case DT_ACT_DENORMALIZE:
984 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
985 		break;
986 	case DT_ACT_DISCARD:
987 		dt_action_discard(dtp, dnp->dn_expr, sdp);
988 		break;
989 	case DT_ACT_EXIT:
990 		dt_action_exit(dtp, dnp->dn_expr, sdp);
991 		break;
992 	case DT_ACT_FREOPEN:
993 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
994 		break;
995 	case DT_ACT_FTRUNCATE:
996 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
997 		break;
998 	case DT_ACT_MOD:
999 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1000 		break;
1001 	case DT_ACT_NORMALIZE:
1002 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
1003 		break;
1004 	case DT_ACT_PANIC:
1005 		dt_action_panic(dtp, dnp->dn_expr, sdp);
1006 		break;
1007 	case DT_ACT_PRINTA:
1008 		dt_action_printa(dtp, dnp->dn_expr, sdp);
1009 		break;
1010 	case DT_ACT_PRINTF:
1011 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1012 		break;
1013 	case DT_ACT_RAISE:
1014 		dt_action_raise(dtp, dnp->dn_expr, sdp);
1015 		break;
1016 	case DT_ACT_SETOPT:
1017 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1018 		break;
1019 	case DT_ACT_SPECULATE:
1020 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
1021 		break;
1022 	case DT_ACT_STACK:
1023 		dt_action_stack(dtp, dnp->dn_expr, sdp);
1024 		break;
1025 	case DT_ACT_STOP:
1026 		dt_action_stop(dtp, dnp->dn_expr, sdp);
1027 		break;
1028 	case DT_ACT_SYM:
1029 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1030 		break;
1031 	case DT_ACT_SYSTEM:
1032 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1033 		break;
1034 	case DT_ACT_TRACE:
1035 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1036 		break;
1037 	case DT_ACT_TRACEMEM:
1038 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1039 		break;
1040 	case DT_ACT_TRUNC:
1041 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
1042 		break;
1043 	case DT_ACT_UADDR:
1044 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1045 		break;
1046 	case DT_ACT_UMOD:
1047 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1048 		break;
1049 	case DT_ACT_USYM:
1050 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1051 		break;
1052 	case DT_ACT_USTACK:
1053 	case DT_ACT_JSTACK:
1054 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
1055 		break;
1056 	default:
1057 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1058 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1059 	}
1060 }
1061 
1062 static void
1063 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1064 {
1065 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1066 
1067 	dt_cg(yypcb, dnp->dn_expr);
1068 	ap->dtad_difo = dt_as(yypcb);
1069 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1070 	ap->dtad_kind = DTRACEACT_DIFEXPR;
1071 }
1072 
1073 static void
1074 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1075 {
1076 	dt_ident_t *aid, *fid;
1077 	dt_node_t *anp, *incr = NULL;
1078 	dtrace_actdesc_t *ap;
1079 	uint_t n = 1, argmax;
1080 	uint64_t arg = 0;
1081 
1082 	/*
1083 	 * If the aggregation has no aggregating function applied to it, then
1084 	 * this statement has no effect.  Flag this as a programming error.
1085 	 */
1086 	if (dnp->dn_aggfun == NULL) {
1087 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1088 		    dnp->dn_ident->di_name);
1089 	}
1090 
1091 	aid = dnp->dn_ident;
1092 	fid = dnp->dn_aggfun->dn_ident;
1093 
1094 	if (dnp->dn_aggfun->dn_args != NULL &&
1095 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1096 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1097 		    "be of scalar type\n", fid->di_name);
1098 	}
1099 
1100 	/*
1101 	 * The ID of the aggregation itself is implicitly recorded as the first
1102 	 * member of each aggregation tuple so we can distinguish them later.
1103 	 */
1104 	ap = dt_stmt_action(dtp, sdp);
1105 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1106 
1107 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1108 		ap = dt_stmt_action(dtp, sdp);
1109 		n++;
1110 
1111 		if (anp->dn_kind == DT_NODE_FUNC) {
1112 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
1113 				dt_action_stack_args(dtp, ap, anp->dn_args);
1114 				continue;
1115 			}
1116 
1117 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1118 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
1119 				dt_action_ustack_args(dtp, ap, anp);
1120 				continue;
1121 			}
1122 
1123 			switch (anp->dn_ident->di_id) {
1124 			case DT_ACT_UADDR:
1125 				dt_action_symmod_args(dtp, ap,
1126 				    anp->dn_args, DTRACEACT_UADDR);
1127 				continue;
1128 
1129 			case DT_ACT_USYM:
1130 				dt_action_symmod_args(dtp, ap,
1131 				    anp->dn_args, DTRACEACT_USYM);
1132 				continue;
1133 
1134 			case DT_ACT_UMOD:
1135 				dt_action_symmod_args(dtp, ap,
1136 				    anp->dn_args, DTRACEACT_UMOD);
1137 				continue;
1138 
1139 			case DT_ACT_SYM:
1140 				dt_action_symmod_args(dtp, ap,
1141 				    anp->dn_args, DTRACEACT_SYM);
1142 				continue;
1143 
1144 			case DT_ACT_MOD:
1145 				dt_action_symmod_args(dtp, ap,
1146 				    anp->dn_args, DTRACEACT_MOD);
1147 				continue;
1148 
1149 			default:
1150 				break;
1151 			}
1152 		}
1153 
1154 		dt_cg(yypcb, anp);
1155 		ap->dtad_difo = dt_as(yypcb);
1156 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1157 	}
1158 
1159 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1160 		/*
1161 		 * For linear quantization, we have between two and four
1162 		 * arguments in addition to the expression:
1163 		 *
1164 		 *    arg1 => Base value
1165 		 *    arg2 => Limit value
1166 		 *    arg3 => Quantization level step size (defaults to 1)
1167 		 *    arg4 => Quantization increment value (defaults to 1)
1168 		 */
1169 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1170 		dt_node_t *arg2 = arg1->dn_list;
1171 		dt_node_t *arg3 = arg2->dn_list;
1172 		dt_idsig_t *isp;
1173 		uint64_t nlevels, step = 1, oarg;
1174 		int64_t baseval, limitval;
1175 
1176 		if (arg1->dn_kind != DT_NODE_INT) {
1177 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1178 			    "argument #1 must be an integer constant\n");
1179 		}
1180 
1181 		baseval = (int64_t)arg1->dn_value;
1182 
1183 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
1184 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1185 			    "argument #1 must be a 32-bit quantity\n");
1186 		}
1187 
1188 		if (arg2->dn_kind != DT_NODE_INT) {
1189 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1190 			    "argument #2 must be an integer constant\n");
1191 		}
1192 
1193 		limitval = (int64_t)arg2->dn_value;
1194 
1195 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
1196 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1197 			    "argument #2 must be a 32-bit quantity\n");
1198 		}
1199 
1200 		if (limitval < baseval) {
1201 			dnerror(dnp, D_LQUANT_MISMATCH,
1202 			    "lquantize( ) base (argument #1) must be less "
1203 			    "than limit (argument #2)\n");
1204 		}
1205 
1206 		if (arg3 != NULL) {
1207 			if (!dt_node_is_posconst(arg3)) {
1208 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1209 				    "argument #3 must be a non-zero positive "
1210 				    "integer constant\n");
1211 			}
1212 
1213 			if ((step = arg3->dn_value) > UINT16_MAX) {
1214 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1215 				    "argument #3 must be a 16-bit quantity\n");
1216 			}
1217 		}
1218 
1219 		nlevels = (limitval - baseval) / step;
1220 
1221 		if (nlevels == 0) {
1222 			dnerror(dnp, D_LQUANT_STEPLARGE,
1223 			    "lquantize( ) step (argument #3) too large: must "
1224 			    "have at least one quantization level\n");
1225 		}
1226 
1227 		if (nlevels > UINT16_MAX) {
1228 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1229 			    "(argument #3) too small: number of quantization "
1230 			    "levels must be a 16-bit quantity\n");
1231 		}
1232 
1233 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1234 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1235 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1236 		    DTRACE_LQUANTIZE_BASEMASK);
1237 
1238 		assert(arg != 0);
1239 
1240 		isp = (dt_idsig_t *)aid->di_data;
1241 
1242 		if (isp->dis_auxinfo == 0) {
1243 			/*
1244 			 * This is the first time we've seen an lquantize()
1245 			 * for this aggregation; we'll store our argument
1246 			 * as the auxiliary signature information.
1247 			 */
1248 			isp->dis_auxinfo = arg;
1249 		} else if ((oarg = isp->dis_auxinfo) != arg) {
1250 			/*
1251 			 * If we have seen this lquantize() before and the
1252 			 * argument doesn't match the original argument, pick
1253 			 * the original argument apart to concisely report the
1254 			 * mismatch.
1255 			 */
1256 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1257 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1258 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1259 
1260 			if (obaseval != baseval) {
1261 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1262 				    "base (argument #1) doesn't match previous "
1263 				    "declaration: expected %d, found %d\n",
1264 				    obaseval, (int)baseval);
1265 			}
1266 
1267 			if (onlevels * ostep != nlevels * step) {
1268 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1269 				    "limit (argument #2) doesn't match previous"
1270 				    " declaration: expected %d, found %d\n",
1271 				    obaseval + onlevels * ostep,
1272 				    (int)baseval + (int)nlevels * (int)step);
1273 			}
1274 
1275 			if (ostep != step) {
1276 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1277 				    "step (argument #3) doesn't match previous "
1278 				    "declaration: expected %d, found %d\n",
1279 				    ostep, (int)step);
1280 			}
1281 
1282 			/*
1283 			 * We shouldn't be able to get here -- one of the
1284 			 * parameters must be mismatched if the arguments
1285 			 * didn't match.
1286 			 */
1287 			assert(0);
1288 		}
1289 
1290 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1291 		argmax = 5;
1292 	}
1293 
1294 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1295 		incr = dnp->dn_aggfun->dn_args->dn_list;
1296 		argmax = 2;
1297 	}
1298 
1299 	if (incr != NULL) {
1300 		if (!dt_node_is_scalar(incr)) {
1301 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1302 			    "(argument #%d) must be of scalar type\n",
1303 			    fid->di_name, argmax);
1304 		}
1305 
1306 		if ((anp = incr->dn_list) != NULL) {
1307 			int argc = argmax;
1308 
1309 			for (; anp != NULL; anp = anp->dn_list)
1310 				argc++;
1311 
1312 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1313 			    "mismatch: %d args passed, at most %d expected",
1314 			    fid->di_name, argc, argmax);
1315 		}
1316 
1317 		ap = dt_stmt_action(dtp, sdp);
1318 		n++;
1319 
1320 		dt_cg(yypcb, incr);
1321 		ap->dtad_difo = dt_as(yypcb);
1322 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1323 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1324 	}
1325 
1326 	assert(sdp->dtsd_aggdata == NULL);
1327 	sdp->dtsd_aggdata = aid;
1328 
1329 	ap = dt_stmt_action(dtp, sdp);
1330 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1331 	assert(DTRACEACT_ISAGG(fid->di_id));
1332 	ap->dtad_kind = fid->di_id;
1333 	ap->dtad_ntuple = n;
1334 	ap->dtad_arg = arg;
1335 
1336 	if (dnp->dn_aggfun->dn_args != NULL) {
1337 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1338 		ap->dtad_difo = dt_as(yypcb);
1339 	}
1340 }
1341 
1342 static void
1343 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1344 {
1345 	dtrace_ecbdesc_t *edp;
1346 	dtrace_stmtdesc_t *sdp;
1347 	dt_node_t *dnp;
1348 
1349 	yylineno = pnp->dn_line;
1350 	dt_setcontext(dtp, pnp->dn_desc);
1351 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
1352 
1353 	if (DT_TREEDUMP_PASS(dtp, 2))
1354 		dt_node_printr(cnp, stderr, 0);
1355 
1356 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1357 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1358 
1359 	assert(yypcb->pcb_ecbdesc == NULL);
1360 	yypcb->pcb_ecbdesc = edp;
1361 
1362 	if (cnp->dn_pred != NULL) {
1363 		dt_cg(yypcb, cnp->dn_pred);
1364 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1365 	}
1366 
1367 	if (cnp->dn_acts == NULL) {
1368 		dt_stmt_append(dt_stmt_create(dtp, edp,
1369 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
1370 	}
1371 
1372 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1373 		assert(yypcb->pcb_stmt == NULL);
1374 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1375 
1376 		switch (dnp->dn_kind) {
1377 		case DT_NODE_DEXPR:
1378 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1379 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
1380 			else
1381 				dt_compile_exp(dtp, dnp, sdp);
1382 			break;
1383 		case DT_NODE_DFUNC:
1384 			dt_compile_fun(dtp, dnp, sdp);
1385 			break;
1386 		case DT_NODE_AGG:
1387 			dt_compile_agg(dtp, dnp, sdp);
1388 			break;
1389 		default:
1390 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1391 			    "%u is not a valid statement\n", dnp->dn_kind);
1392 		}
1393 
1394 		assert(yypcb->pcb_stmt == sdp);
1395 		dt_stmt_append(sdp, dnp);
1396 	}
1397 
1398 	assert(yypcb->pcb_ecbdesc == edp);
1399 	dt_ecbdesc_release(dtp, edp);
1400 	dt_endcontext(dtp);
1401 	yypcb->pcb_ecbdesc = NULL;
1402 }
1403 
1404 static void
1405 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1406 {
1407 	dt_node_t *pnp;
1408 
1409 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1410 		dt_compile_one_clause(dtp, cnp, pnp);
1411 }
1412 
1413 static void
1414 dt_compile_xlator(dt_node_t *dnp)
1415 {
1416 	dt_xlator_t *dxp = dnp->dn_xlator;
1417 	dt_node_t *mnp;
1418 
1419 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1420 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1421 		dt_cg(yypcb, mnp);
1422 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1423 	}
1424 }
1425 
1426 void
1427 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1428 {
1429 	const dtrace_pattr_t *pap;
1430 	dt_probe_t *prp;
1431 	dt_ident_t *idp;
1432 	char attrstr[8];
1433 	int err;
1434 
1435 	/*
1436 	 * If the provider name ends with what could be interpreted as a
1437 	 * number, we assume that it's a pid and that we may need to
1438 	 * dynamically create those probes for that process. On an error,
1439 	 * dt_pid_create_probes() will set the error message and tag --
1440 	 * we just have to longjmp() out of here.
1441 	 */
1442 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1443 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1444 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1445 	}
1446 
1447 	/*
1448 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
1449 	 * a representative probe is found, set 'pap' to the probe provider's
1450 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
1451 	 */
1452 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1453 		pap = &_dtrace_prvdesc;
1454 		err = dtrace_errno(dtp);
1455 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1456 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1457 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1458 	} else {
1459 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1460 		err = 0;
1461 	}
1462 
1463 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1464 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1465 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1466 		    pdp->dtpd_func, pdp->dtpd_name);
1467 	}
1468 
1469 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1470 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1471 
1472 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1473 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1474 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1475 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1476 
1477 	/*
1478 	 * Reset the stability attributes of D global variables that vary
1479 	 * based on the attributes of the provider and context itself.
1480 	 */
1481 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1482 		idp->di_attr = pap->dtpa_provider;
1483 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1484 		idp->di_attr = pap->dtpa_mod;
1485 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1486 		idp->di_attr = pap->dtpa_func;
1487 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1488 		idp->di_attr = pap->dtpa_name;
1489 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1490 		idp->di_attr = pap->dtpa_args;
1491 
1492 	yypcb->pcb_pdesc = pdp;
1493 	yypcb->pcb_probe = prp;
1494 }
1495 
1496 /*
1497  * Reset context-dependent variables and state at the end of cooking a D probe
1498  * definition clause.  This ensures that external declarations between clauses
1499  * do not reference any stale context-dependent data from the previous clause.
1500  */
1501 void
1502 dt_endcontext(dtrace_hdl_t *dtp)
1503 {
1504 	static const char *const cvars[] = {
1505 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
1506 	};
1507 
1508 	dt_ident_t *idp;
1509 	int i;
1510 
1511 	for (i = 0; cvars[i] != NULL; i++) {
1512 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1513 			idp->di_attr = _dtrace_defattr;
1514 	}
1515 
1516 	yypcb->pcb_pdesc = NULL;
1517 	yypcb->pcb_probe = NULL;
1518 }
1519 
1520 static int
1521 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1522 {
1523 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1524 		dt_idhash_delete(dhp, idp);
1525 
1526 	return (0);
1527 }
1528 
1529 /*
1530  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1531  * any identifiers or translators that have been previously defined as bound to
1532  * a version greater than the specified version.  Therefore, in our current
1533  * version implementation, establishing a binding is a one-way transformation.
1534  * In addition, no versioning is currently provided for types as our .d library
1535  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1536  * for our exclusive use.  If required, type versioning will require more work.
1537  */
1538 int
1539 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1540 {
1541 	char s[DT_VERSION_STRMAX];
1542 	dt_xlator_t *dxp, *nxp;
1543 
1544 	if (v > dtp->dt_vmax)
1545 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
1546 	else if (v == dtp->dt_vmax)
1547 		return (0); /* no reduction necessary */
1548 
1549 	dt_dprintf("reducing api version to %s\n",
1550 	    dt_version_num2str(v, s, sizeof (s)));
1551 
1552 	dtp->dt_vmax = v;
1553 
1554 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1555 		nxp = dt_list_next(dxp);
1556 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1557 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1558 			dt_list_delete(&dtp->dt_xlators, dxp);
1559 	}
1560 
1561 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1562 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1563 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1564 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1565 
1566 	return (0);
1567 }
1568 
1569 /*
1570  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1571  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
1572  * here to simplify the code by leveraging file descriptor inheritance.
1573  */
1574 static FILE *
1575 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1576 {
1577 	int argc = dtp->dt_cpp_argc;
1578 	char **argv = malloc(sizeof (char *) * (argc + 5));
1579 	FILE *ofp = tmpfile();
1580 
1581 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1582 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1583 
1584 	struct sigaction act, oact;
1585 	sigset_t mask, omask;
1586 
1587 	int wstat, estat;
1588 	pid_t pid;
1589 	off64_t off;
1590 	int c;
1591 
1592 	if (argv == NULL || ofp == NULL) {
1593 		(void) dt_set_errno(dtp, errno);
1594 		goto err;
1595 	}
1596 
1597 	/*
1598 	 * If the input is a seekable file, see if it is an interpreter file.
1599 	 * If we see #!, seek past the first line because cpp will choke on it.
1600 	 * We start cpp just prior to the \n at the end of this line so that
1601 	 * it still sees the newline, ensuring that #line values are correct.
1602 	 */
1603 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1604 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1605 			for (off += 2; c != '\n'; off++) {
1606 				if ((c = fgetc(ifp)) == EOF)
1607 					break;
1608 			}
1609 			if (c == '\n')
1610 				off--; /* start cpp just prior to \n */
1611 		}
1612 		(void) fflush(ifp);
1613 		(void) fseeko64(ifp, off, SEEK_SET);
1614 	}
1615 
1616 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1617 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1618 
1619 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1620 
1621 	(void) snprintf(verdef, sizeof (verdef),
1622 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1623 	argv[argc++] = verdef;
1624 
1625 	switch (dtp->dt_stdcmode) {
1626 	case DT_STDC_XA:
1627 	case DT_STDC_XT:
1628 		argv[argc++] = "-D__STDC__=0";
1629 		break;
1630 	case DT_STDC_XC:
1631 		argv[argc++] = "-D__STDC__=1";
1632 		break;
1633 	}
1634 
1635 	argv[argc++] = ipath;
1636 	argv[argc++] = opath;
1637 	argv[argc] = NULL;
1638 
1639 	/*
1640 	 * libdtrace must be able to be embedded in other programs that may
1641 	 * include application-specific signal handlers.  Therefore, if we
1642 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1643 	 * that could confuse the containing application.  To do this,
1644 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
1645 	 * We restore our signal state once we are done.
1646 	 */
1647 	(void) sigemptyset(&mask);
1648 	(void) sigaddset(&mask, SIGCHLD);
1649 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
1650 
1651 	bzero(&act, sizeof (act));
1652 	act.sa_handler = SIG_DFL;
1653 	(void) sigaction(SIGCHLD, &act, &oact);
1654 
1655 	if ((pid = fork1()) == -1) {
1656 		(void) sigaction(SIGCHLD, &oact, NULL);
1657 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1658 		(void) dt_set_errno(dtp, EDT_CPPFORK);
1659 		goto err;
1660 	}
1661 
1662 	if (pid == 0) {
1663 		(void) execvp(dtp->dt_cpp_path, argv);
1664 		_exit(errno == ENOENT ? 127 : 126);
1665 	}
1666 
1667 	do {
1668 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1669 		    (int)pid);
1670 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1671 
1672 	(void) sigaction(SIGCHLD, &oact, NULL);
1673 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1674 
1675 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1676 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1677 
1678 	if (estat != 0) {
1679 		switch (estat) {
1680 		case 126:
1681 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
1682 			break;
1683 		case 127:
1684 			(void) dt_set_errno(dtp, EDT_CPPENT);
1685 			break;
1686 		default:
1687 			(void) dt_set_errno(dtp, EDT_CPPERR);
1688 		}
1689 		goto err;
1690 	}
1691 
1692 	free(argv);
1693 	(void) fflush(ofp);
1694 	(void) fseek(ofp, 0, SEEK_SET);
1695 	return (ofp);
1696 
1697 err:
1698 	free(argv);
1699 	(void) fclose(ofp);
1700 	return (NULL);
1701 }
1702 
1703 /*
1704  * Open all of the .d library files found in the specified directory and try to
1705  * compile each one in order to cache its inlines and translators, etc.  We
1706  * silently ignore any missing directories and other files found therein.
1707  * We only fail (and thereby fail dt_load_libs()) if we fail to compile a
1708  * library and the error is something other than #pragma D depends_on.
1709  * Dependency errors are silently ignored to permit a library directory to
1710  * contain libraries which may not be accessible depending on our privileges.
1711  *
1712  * Note that at present, no ordering is defined between library files found in
1713  * the same directory: if cross-library dependencies are eventually required,
1714  * we will need to extend the #pragma D depends_on directive with an additional
1715  * class for libraries, and this function will need to create a graph of the
1716  * various library pathnames and then perform a topological ordering using the
1717  * dependency information before we attempt to compile any of them.
1718  */
1719 static int
1720 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
1721 {
1722 	struct dirent *dp;
1723 	const char *p;
1724 	DIR *dirp;
1725 
1726 	char fname[PATH_MAX];
1727 	dtrace_prog_t *pgp;
1728 	FILE *fp;
1729 
1730 	if ((dirp = opendir(path)) == NULL) {
1731 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
1732 		return (0);
1733 	}
1734 
1735 	while ((dp = readdir(dirp)) != NULL) {
1736 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
1737 			continue; /* skip any filename not ending in .d */
1738 
1739 		(void) snprintf(fname, sizeof (fname),
1740 		    "%s/%s", path, dp->d_name);
1741 
1742 		if ((fp = fopen(fname, "r")) == NULL) {
1743 			dt_dprintf("skipping library %s: %s\n",
1744 			    fname, strerror(errno));
1745 			continue;
1746 		}
1747 
1748 		dtp->dt_filetag = fname;
1749 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
1750 		(void) fclose(fp);
1751 		dtp->dt_filetag = NULL;
1752 
1753 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
1754 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
1755 			(void) closedir(dirp);
1756 			return (-1); /* preserve dt_errno */
1757 		}
1758 
1759 		if (pgp == NULL) {
1760 			dt_dprintf("skipping library: %s\n",
1761 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1762 		} else
1763 			dt_program_destroy(dtp, pgp);
1764 	}
1765 
1766 	(void) closedir(dirp);
1767 	return (0);
1768 }
1769 
1770 /*
1771  * Load the contents of any appropriate DTrace .d library files.  These files
1772  * contain inlines and translators that will be cached by the compiler.  We
1773  * defer this activity until the first compile to permit libdtrace clients to
1774  * add their own library directories and so that we can properly report errors.
1775  */
1776 static int
1777 dt_load_libs(dtrace_hdl_t *dtp)
1778 {
1779 	dt_dirpath_t *dirp;
1780 
1781 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
1782 		return (0); /* libraries already processed */
1783 
1784 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
1785 
1786 	for (dirp = dt_list_next(&dtp->dt_lib_path);
1787 	    dirp != NULL; dirp = dt_list_next(dirp)) {
1788 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
1789 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
1790 			return (-1); /* errno is set for us */
1791 		}
1792 	}
1793 
1794 	return (0);
1795 }
1796 
1797 static void *
1798 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
1799     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
1800 {
1801 	dt_node_t *dnp;
1802 	dt_decl_t *ddp;
1803 	dt_pcb_t pcb;
1804 	void *rv;
1805 	int err;
1806 
1807 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
1808 		(void) dt_set_errno(dtp, EINVAL);
1809 		return (NULL);
1810 	}
1811 
1812 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
1813 		return (NULL); /* errno is set for us */
1814 
1815 	(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
1816 	(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
1817 
1818 	(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
1819 	(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
1820 
1821 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
1822 		return (NULL); /* errno is set for us */
1823 
1824 	dt_pcb_push(dtp, &pcb);
1825 
1826 	pcb.pcb_fileptr = fp;
1827 	pcb.pcb_string = s;
1828 	pcb.pcb_strptr = s;
1829 	pcb.pcb_strlen = s ? strlen(s) : 0;
1830 	pcb.pcb_sargc = argc;
1831 	pcb.pcb_sargv = argv;
1832 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
1833 	pcb.pcb_pspec = pspec;
1834 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
1835 	pcb.pcb_amin = dtp->dt_amin;
1836 	pcb.pcb_yystate = -1;
1837 	pcb.pcb_context = context;
1838 	pcb.pcb_token = context;
1839 
1840 	if (context == DT_CTX_DPROG)
1841 		yybegin(YYS_CLAUSE);
1842 	else
1843 		yybegin(YYS_EXPR);
1844 
1845 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
1846 		goto out;
1847 
1848 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
1849 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1850 
1851 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
1852 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
1853 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1854 
1855 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
1856 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1857 
1858 	/*
1859 	 * Invoke the parser to evaluate the D source code.  If any errors
1860 	 * occur during parsing, an error function will be called and we
1861 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
1862 	 * we optionally display the parse tree if debugging is enabled.
1863 	 */
1864 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
1865 		xyerror(D_EMPTY, "empty D program translation unit\n");
1866 
1867 	yybegin(YYS_DONE);
1868 
1869 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
1870 		dt_node_printr(yypcb->pcb_root, stderr, 0);
1871 
1872 	if (yypcb->pcb_pragmas != NULL)
1873 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
1874 
1875 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
1876 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
1877 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
1878 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
1879 	}
1880 
1881 	/*
1882 	 * If we have successfully created a parse tree for a D program, loop
1883 	 * over the clauses and actions and instantiate the corresponding
1884 	 * libdtrace program.  If we are parsing a D expression, then we
1885 	 * simply run the code generator and assembler on the resulting tree.
1886 	 */
1887 	switch (context) {
1888 	case DT_CTX_DPROG:
1889 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
1890 
1891 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
1892 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
1893 			xyerror(D_EMPTY, "empty D program translation unit\n");
1894 
1895 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
1896 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
1897 
1898 		for (; dnp != NULL; dnp = dnp->dn_list) {
1899 			switch (dnp->dn_kind) {
1900 			case DT_NODE_CLAUSE:
1901 				dt_compile_clause(dtp, dnp);
1902 				break;
1903 			case DT_NODE_XLATOR:
1904 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
1905 					dt_compile_xlator(dnp);
1906 				break;
1907 			case DT_NODE_PROVIDER:
1908 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
1909 				break;
1910 			}
1911 		}
1912 
1913 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
1914 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
1915 		yypcb->pcb_asxrefs = NULL;
1916 		yypcb->pcb_asxreflen = 0;
1917 
1918 		rv = yypcb->pcb_prog;
1919 		break;
1920 
1921 	case DT_CTX_DEXPR:
1922 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
1923 		dt_cg(yypcb, yypcb->pcb_root);
1924 		rv = dt_as(yypcb);
1925 		break;
1926 
1927 	case DT_CTX_DTYPE:
1928 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
1929 		err = dt_decl_type(ddp, arg);
1930 		dt_decl_free(ddp);
1931 
1932 		if (err != 0)
1933 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1934 
1935 		rv = NULL;
1936 		break;
1937 	}
1938 
1939 out:
1940 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
1941 		dt_node_printr(yypcb->pcb_root, stderr, 0);
1942 
1943 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
1944 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
1945 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
1946 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
1947 
1948 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
1949 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
1950 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
1951 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
1952 
1953 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
1954 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
1955 
1956 	dt_pcb_pop(dtp, err);
1957 	(void) dt_set_errno(dtp, err);
1958 	return (err ? NULL : rv);
1959 }
1960 
1961 dtrace_prog_t *
1962 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
1963     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
1964 {
1965 	return (dt_compile(dtp, DT_CTX_DPROG,
1966 	    spec, NULL, cflags, argc, argv, NULL, s));
1967 }
1968 
1969 dtrace_prog_t *
1970 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
1971     uint_t cflags, int argc, char *const argv[])
1972 {
1973 	return (dt_compile(dtp, DT_CTX_DPROG,
1974 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
1975 }
1976 
1977 int
1978 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
1979 {
1980 	(void) dt_compile(dtp, DT_CTX_DTYPE,
1981 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
1982 	return (dtp->dt_errno ? -1 : 0);
1983 }
1984 
1985 int
1986 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
1987 {
1988 	(void) dt_compile(dtp, DT_CTX_DTYPE,
1989 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
1990 	return (dtp->dt_errno ? -1 : 0);
1991 }
1992