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