xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_cc.c (revision 7d99ab9fd0cc2c1ce2ecef0ed6d0672c2a50b0cb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, Joyent Inc. All rights reserved.
25  * Copyright (c) 2011 by Delphix. All rights reserved.
26  */
27 
28 /*
29  * DTrace D Language Compiler
30  *
31  * The code in this source file implements the main engine for the D language
32  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
33  * compiler operates on either stdio FILEs or in-memory strings as its input
34  * and can produce either dtrace_prog_t structures from a D program or a single
35  * dtrace_difo_t structure from a D expression.  Multiple entry points are
36  * provided as wrappers around dt_compile() for the various input/output pairs.
37  * The compiler itself is implemented across the following source files:
38  *
39  * dt_lex.l - lex scanner
40  * dt_grammar.y - yacc grammar
41  * dt_parser.c - parse tree creation and semantic checking
42  * dt_decl.c - declaration stack processing
43  * dt_xlator.c - D translator lookup and creation
44  * dt_ident.c - identifier and symbol table routines
45  * dt_pragma.c - #pragma processing and D pragmas
46  * dt_printf.c - D printf() and printa() argument checking and processing
47  * dt_cc.c - compiler driver and dtrace_prog_t construction
48  * dt_cg.c - DIF code generator
49  * dt_as.c - DIF assembler
50  * dt_dof.c - dtrace_prog_t -> DOF conversion
51  *
52  * Several other source files provide collections of utility routines used by
53  * these major files.  The compiler itself is implemented in multiple passes:
54  *
55  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
56  *     and parse tree nodes are constructed using the routines in dt_parser.c.
57  *     This node construction pass is described further in dt_parser.c.
58  *
59  * (2) The parse tree is "cooked" by assigning each clause a context (see the
60  *     routine dt_setcontext(), below) based on its probe description and then
61  *     recursively descending the tree performing semantic checking.  The cook
62  *     routines are also implemented in dt_parser.c and described there.
63  *
64  * (3) For actions that are DIF expression statements, the DIF code generator
65  *     and assembler are invoked to create a finished DIFO for the statement.
66  *
67  * (4) The dtrace_prog_t data structures for the program clauses and actions
68  *     are built, containing pointers to any DIFOs created in step (3).
69  *
70  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
71  *     into DOF format for use in anonymous tracing or enabling in the kernel.
72  *
73  * In the implementation, steps 2-4 are intertwined in that they are performed
74  * in order for each clause as part of a loop that executes over the clauses.
75  *
76  * The D compiler currently implements nearly no optimization.  The compiler
77  * implements integer constant folding as part of pass (1), and a set of very
78  * simple peephole optimizations as part of pass (3).  As with any C compiler,
79  * a large number of optimizations are possible on both the intermediate data
80  * structures and the generated DIF code.  These possibilities should be
81  * investigated in the context of whether they will have any substantive effect
82  * on the overall DTrace probe effect before they are undertaken.
83  */
84 
85 #include <sys/types.h>
86 #include <sys/wait.h>
87 #include <sys/sysmacros.h>
88 
89 #include <assert.h>
90 #include <string.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 static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120     uint_t, int, char *const[], FILE *, const char *);
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, *proto = NULL;
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 	assert(anp != NULL);
530 
531 	while (anp != NULL) {
532 		if (anp->dn_kind != DT_NODE_AGG) {
533 			dnerror(dnp, D_PRINTA_AGGARG,
534 			    "%s( ) argument #%d is incompatible with "
535 			    "prototype:\n\tprototype: aggregation\n"
536 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
537 			    dt_node_type_name(anp, n, sizeof (n)));
538 		}
539 
540 		aid = anp->dn_ident;
541 		fid = aid->di_iarg;
542 
543 		if (aid->di_gen == dtp->dt_gen &&
544 		    !(aid->di_flags & DT_IDFLG_MOD)) {
545 			dnerror(dnp, D_PRINTA_AGGBAD,
546 			    "undefined aggregation: @%s\n", aid->di_name);
547 		}
548 
549 		/*
550 		 * If we have multiple aggregations, we must be sure that
551 		 * their key signatures match.
552 		 */
553 		if (proto != NULL) {
554 			dt_printa_validate(proto, anp);
555 		} else {
556 			proto = anp;
557 		}
558 
559 		if (format != NULL) {
560 			yylineno = dnp->dn_line;
561 
562 			sdp->dtsd_fmtdata =
563 			    dt_printf_create(yypcb->pcb_hdl, format);
564 			dt_printf_validate(sdp->dtsd_fmtdata,
565 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
566 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
567 			format = NULL;
568 		}
569 
570 		ap = dt_stmt_action(dtp, sdp);
571 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
572 
573 		anp = anp->dn_list;
574 		argr++;
575 	}
576 }
577 
578 static void
579 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
580     dtrace_actkind_t kind)
581 {
582 	dt_node_t *anp, *arg1;
583 	dtrace_actdesc_t *ap = NULL;
584 	char n[DT_TYPE_NAMELEN], *str;
585 
586 	assert(DTRACEACT_ISPRINTFLIKE(kind));
587 
588 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
589 		dnerror(dnp, D_PRINTF_ARG_FMT,
590 		    "%s( ) argument #1 is incompatible with prototype:\n"
591 		    "\tprototype: string constant\n\t argument: %s\n",
592 		    dnp->dn_ident->di_name,
593 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
594 	}
595 
596 	arg1 = dnp->dn_args->dn_list;
597 	yylineno = dnp->dn_line;
598 	str = dnp->dn_args->dn_string;
599 
600 
601 	/*
602 	 * If this is an freopen(), we use an empty string to denote that
603 	 * stdout should be restored.  For other printf()-like actions, an
604 	 * empty format string is illegal:  an empty format string would
605 	 * result in malformed DOF, and the compiler thus flags an empty
606 	 * format string as a compile-time error.  To avoid propagating the
607 	 * freopen() special case throughout the system, we simply transpose
608 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
609 	 * denotes that stdout should be restored.
610 	 */
611 	if (kind == DTRACEACT_FREOPEN) {
612 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
613 			/*
614 			 * Our sentinel is always an invalid argument to
615 			 * freopen(), but if it's been manually specified, we
616 			 * must fail now instead of when the freopen() is
617 			 * actually evaluated.
618 			 */
619 			dnerror(dnp, D_FREOPEN_INVALID,
620 			    "%s( ) argument #1 cannot be \"%s\"\n",
621 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
622 		}
623 
624 		if (str[0] == '\0')
625 			str = DT_FREOPEN_RESTORE;
626 	}
627 
628 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
629 
630 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
631 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
632 
633 	if (arg1 == NULL) {
634 		dif_instr_t *dbuf;
635 		dtrace_difo_t *dp;
636 
637 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
638 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
639 			dt_free(dtp, dbuf);
640 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
641 		}
642 
643 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
644 
645 		dp->dtdo_buf = dbuf;
646 		dp->dtdo_len = 1;
647 		dp->dtdo_rtype = dt_int_rtype;
648 
649 		ap = dt_stmt_action(dtp, sdp);
650 		ap->dtad_difo = dp;
651 		ap->dtad_kind = kind;
652 		return;
653 	}
654 
655 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
656 		ap = dt_stmt_action(dtp, sdp);
657 		dt_cg(yypcb, anp);
658 		ap->dtad_difo = dt_as(yypcb);
659 		ap->dtad_kind = kind;
660 	}
661 }
662 
663 static void
664 dt_action_trace(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 	if (dt_node_is_void(dnp->dn_args)) {
669 		dnerror(dnp->dn_args, D_TRACE_VOID,
670 		    "trace( ) may not be applied to a void expression\n");
671 	}
672 
673 	if (dt_node_is_dynamic(dnp->dn_args)) {
674 		dnerror(dnp->dn_args, D_TRACE_DYN,
675 		    "trace( ) may not be applied to a dynamic expression\n");
676 	}
677 
678 	dt_cg(yypcb, dnp->dn_args);
679 	ap->dtad_difo = dt_as(yypcb);
680 	ap->dtad_kind = DTRACEACT_DIFEXPR;
681 }
682 
683 /*
684  * The print() action behaves identically to trace(), except that it stores the
685  * CTF type of the argument (if present) within the DOF for the DIFEXPR action.
686  * To do this, we set the 'dtsd_strdata' to point to the fully-qualified CTF
687  * type ID for the result of the DIF action.  We use the ID instead of the name
688  * to handles complex types like arrays and function pointers that can't be
689  * resolved by ctf_type_lookup().  This is later processed by
690  * dtrace_dof_create() and turned into a reference into the string table so
691  * that we can get the type information when we process the data after the
692  * fact.
693  */
694 static void
695 dt_action_print(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
696 {
697 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
698 	dt_node_t *dret;
699 	size_t len;
700 	dt_module_t *dmp;
701 
702 	if (dt_node_is_void(dnp->dn_args)) {
703 		dnerror(dnp->dn_args, D_PRINT_VOID,
704 		    "print( ) may not be applied to a void expression\n");
705 	}
706 
707 	if (dt_node_is_dynamic(dnp->dn_args)) {
708 		dnerror(dnp->dn_args, D_PRINT_DYN,
709 		    "print( ) may not be applied to a dynamic expression\n");
710 	}
711 
712 	dt_cg(yypcb, dnp->dn_args);
713 
714 	dret = yypcb->pcb_dret;
715 	dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
716 
717 	len = snprintf(NULL, 0, "%s`%ld", dmp->dm_name, dret->dn_type) + 1;
718 	sdp->dtsd_strdata = dt_alloc(dtp, len);
719 	if (sdp->dtsd_strdata == NULL)
720 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
721 	(void) snprintf(sdp->dtsd_strdata, len, "%s`%ld", dmp->dm_name,
722 	    dret->dn_type);
723 
724 	ap->dtad_difo = dt_as(yypcb);
725 	ap->dtad_kind = DTRACEACT_DIFEXPR;
726 }
727 
728 static void
729 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
730 {
731 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
732 
733 	dt_node_t *addr = dnp->dn_args;
734 	dt_node_t *max = dnp->dn_args->dn_list;
735 	dt_node_t *size;
736 
737 	char n[DT_TYPE_NAMELEN];
738 
739 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
740 		dnerror(addr, D_TRACEMEM_ADDR,
741 		    "tracemem( ) argument #1 is incompatible with "
742 		    "prototype:\n\tprototype: pointer or integer\n"
743 		    "\t argument: %s\n",
744 		    dt_node_type_name(addr, n, sizeof (n)));
745 	}
746 
747 	if (dt_node_is_posconst(max) == 0) {
748 		dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
749 		    "be a non-zero positive integral constant expression\n");
750 	}
751 
752 	if ((size = max->dn_list) != NULL) {
753 		if (size->dn_list != NULL) {
754 			dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
755 			    "mismatch: expected at most 3 args\n");
756 		}
757 
758 		if (!dt_node_is_scalar(size)) {
759 			dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
760 			    "dynamic size (argument #3) must be of "
761 			    "scalar type\n");
762 		}
763 
764 		dt_cg(yypcb, size);
765 		ap->dtad_difo = dt_as(yypcb);
766 		ap->dtad_difo->dtdo_rtype = dt_int_rtype;
767 		ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
768 
769 		ap = dt_stmt_action(dtp, sdp);
770 	}
771 
772 	dt_cg(yypcb, addr);
773 	ap->dtad_difo = dt_as(yypcb);
774 	ap->dtad_kind = DTRACEACT_TRACEMEM;
775 
776 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
777 	ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
778 }
779 
780 static void
781 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
782 {
783 	ap->dtad_kind = DTRACEACT_STACK;
784 
785 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
786 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
787 	} else {
788 		ap->dtad_arg = 0;
789 	}
790 
791 	if (arg0 != NULL) {
792 		if (arg0->dn_list != NULL) {
793 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
794 			    "mismatch: too many arguments\n");
795 		}
796 
797 		if (dt_node_is_posconst(arg0) == 0) {
798 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
799 			    "non-zero positive integral constant expression\n");
800 		}
801 
802 		ap->dtad_arg = arg0->dn_value;
803 	}
804 }
805 
806 static void
807 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
808 {
809 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
810 	dt_action_stack_args(dtp, ap, dnp->dn_args);
811 }
812 
813 static void
814 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
815 {
816 	uint32_t nframes = 0;
817 	uint32_t strsize = 0;	/* default string table size */
818 	dt_node_t *arg0 = dnp->dn_args;
819 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
820 
821 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
822 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
823 
824 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
825 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
826 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
827 
828 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
829 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
830 
831 		ap->dtad_kind = DTRACEACT_JSTACK;
832 	} else {
833 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
834 
835 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
836 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
837 
838 		ap->dtad_kind = DTRACEACT_USTACK;
839 	}
840 
841 	if (arg0 != NULL) {
842 		if (!dt_node_is_posconst(arg0)) {
843 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
844 			    "must be a non-zero positive integer constant\n");
845 		}
846 		nframes = (uint32_t)arg0->dn_value;
847 	}
848 
849 	if (arg1 != NULL) {
850 		if (arg1->dn_kind != DT_NODE_INT ||
851 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
852 		    (int64_t)arg1->dn_value < 0)) {
853 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
854 			    "must be a positive integer constant\n");
855 		}
856 
857 		if (arg1->dn_list != NULL) {
858 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
859 			    "mismatch: too many arguments\n");
860 		}
861 
862 		strsize = (uint32_t)arg1->dn_value;
863 	}
864 
865 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
866 }
867 
868 static void
869 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
870 {
871 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
872 	dt_action_ustack_args(dtp, ap, dnp);
873 }
874 
875 static void
876 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
877 {
878 	dtrace_actdesc_t *ap;
879 	dt_node_t *arg0, *arg1;
880 
881 	/*
882 	 * The prototype guarantees that we are called with either one or
883 	 * two arguments, and that any arguments that are present are strings.
884 	 */
885 	arg0 = dnp->dn_args;
886 	arg1 = arg0->dn_list;
887 
888 	ap = dt_stmt_action(dtp, sdp);
889 	dt_cg(yypcb, arg0);
890 	ap->dtad_difo = dt_as(yypcb);
891 	ap->dtad_kind = DTRACEACT_LIBACT;
892 	ap->dtad_arg = DT_ACT_SETOPT;
893 
894 	ap = dt_stmt_action(dtp, sdp);
895 
896 	if (arg1 == NULL) {
897 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
898 	} else {
899 		dt_cg(yypcb, arg1);
900 		ap->dtad_difo = dt_as(yypcb);
901 		ap->dtad_kind = DTRACEACT_LIBACT;
902 	}
903 
904 	ap->dtad_arg = DT_ACT_SETOPT;
905 }
906 
907 /*ARGSUSED*/
908 static void
909 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
910     dt_node_t *dnp, dtrace_actkind_t kind)
911 {
912 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
913 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
914 	    kind == DTRACEACT_UADDR);
915 
916 	dt_cg(yypcb, dnp);
917 	ap->dtad_difo = dt_as(yypcb);
918 	ap->dtad_kind = kind;
919 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
920 }
921 
922 static void
923 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
924     dtrace_actkind_t kind)
925 {
926 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
927 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
928 }
929 
930 /*ARGSUSED*/
931 static void
932 dt_action_ftruncate(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 	/*
937 	 * Library actions need a DIFO that serves as an argument.  As
938 	 * ftruncate() doesn't take an argument, we generate the constant 0
939 	 * in a DIFO; this constant will be ignored when the ftruncate() is
940 	 * processed.
941 	 */
942 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
943 	ap->dtad_arg = DT_ACT_FTRUNCATE;
944 }
945 
946 /*ARGSUSED*/
947 static void
948 dt_action_stop(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 	ap->dtad_kind = DTRACEACT_STOP;
953 	ap->dtad_arg = 0;
954 }
955 
956 /*ARGSUSED*/
957 static void
958 dt_action_breakpoint(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 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
963 	ap->dtad_arg = 0;
964 }
965 
966 /*ARGSUSED*/
967 static void
968 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
969 {
970 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
971 
972 	ap->dtad_kind = DTRACEACT_PANIC;
973 	ap->dtad_arg = 0;
974 }
975 
976 static void
977 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
978 {
979 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
980 
981 	dt_cg(yypcb, dnp->dn_args);
982 	ap->dtad_difo = dt_as(yypcb);
983 	ap->dtad_kind = DTRACEACT_CHILL;
984 }
985 
986 static void
987 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
988 {
989 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
990 
991 	dt_cg(yypcb, dnp->dn_args);
992 	ap->dtad_difo = dt_as(yypcb);
993 	ap->dtad_kind = DTRACEACT_RAISE;
994 }
995 
996 static void
997 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
998 {
999 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1000 
1001 	dt_cg(yypcb, dnp->dn_args);
1002 	ap->dtad_difo = dt_as(yypcb);
1003 	ap->dtad_kind = DTRACEACT_EXIT;
1004 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
1005 }
1006 
1007 static void
1008 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1009 {
1010 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1011 
1012 	dt_cg(yypcb, dnp->dn_args);
1013 	ap->dtad_difo = dt_as(yypcb);
1014 	ap->dtad_kind = DTRACEACT_SPECULATE;
1015 }
1016 
1017 static void
1018 dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1019 {
1020 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1021 
1022 	dt_node_t *size = dnp->dn_args;
1023 	dt_node_t *addr = dnp->dn_args->dn_list;
1024 
1025 	char n[DT_TYPE_NAMELEN];
1026 
1027 	if (dt_node_is_posconst(size) == 0) {
1028 		dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
1029 		    "be a non-zero positive integral constant expression\n");
1030 	}
1031 
1032 	if (dt_node_is_pointer(addr) == 0) {
1033 		dnerror(addr, D_PRINTM_ADDR,
1034 		    "printm( ) argument #2 is incompatible with "
1035 		    "prototype:\n\tprototype: pointer\n"
1036 		    "\t argument: %s\n",
1037 		    dt_node_type_name(addr, n, sizeof (n)));
1038 	}
1039 
1040 	dt_cg(yypcb, addr);
1041 	ap->dtad_difo = dt_as(yypcb);
1042 	ap->dtad_kind = DTRACEACT_PRINTM;
1043 
1044 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1045 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
1046 }
1047 
1048 static void
1049 dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1050 {
1051 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1052 
1053 	dt_node_t *size = dnp->dn_args;
1054 	dt_node_t *addr = dnp->dn_args->dn_list;
1055 
1056 	char n[DT_TYPE_NAMELEN];
1057 
1058 	if (dt_node_is_posconst(size) == 0) {
1059 		dnerror(size, D_PRINTT_SIZE, "printt( ) argument #1 must "
1060 		    "be a non-zero positive integral constant expression\n");
1061 	}
1062 
1063 	if (addr == NULL || addr->dn_kind != DT_NODE_FUNC ||
1064 	    addr->dn_ident != dt_idhash_lookup(dtp->dt_globals, "typeref")) {
1065 		dnerror(addr, D_PRINTT_ADDR,
1066 		    "printt( ) argument #2 is incompatible with "
1067 		    "prototype:\n\tprototype: typeref()\n"
1068 		    "\t argument: %s\n",
1069 		    dt_node_type_name(addr, n, sizeof (n)));
1070 	}
1071 
1072 	dt_cg(yypcb, addr);
1073 	ap->dtad_difo = dt_as(yypcb);
1074 	ap->dtad_kind = DTRACEACT_PRINTT;
1075 
1076 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1077 
1078 	/*
1079 	 * Allow additional buffer space for the data size, type size,
1080 	 * type string length and a stab in the dark (32 bytes) for the
1081 	 * type string. The type string is part of the typeref() that
1082 	 * this action references.
1083 	 */
1084 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1085 
1086 }
1087 
1088 static void
1089 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1090 {
1091 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1092 
1093 	dt_cg(yypcb, dnp->dn_args);
1094 	ap->dtad_difo = dt_as(yypcb);
1095 	ap->dtad_kind = DTRACEACT_COMMIT;
1096 }
1097 
1098 static void
1099 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1100 {
1101 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1102 
1103 	dt_cg(yypcb, dnp->dn_args);
1104 	ap->dtad_difo = dt_as(yypcb);
1105 	ap->dtad_kind = DTRACEACT_DISCARD;
1106 }
1107 
1108 static void
1109 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1110 {
1111 	switch (dnp->dn_expr->dn_ident->di_id) {
1112 	case DT_ACT_BREAKPOINT:
1113 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1114 		break;
1115 	case DT_ACT_CHILL:
1116 		dt_action_chill(dtp, dnp->dn_expr, sdp);
1117 		break;
1118 	case DT_ACT_CLEAR:
1119 		dt_action_clear(dtp, dnp->dn_expr, sdp);
1120 		break;
1121 	case DT_ACT_COMMIT:
1122 		dt_action_commit(dtp, dnp->dn_expr, sdp);
1123 		break;
1124 	case DT_ACT_DENORMALIZE:
1125 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
1126 		break;
1127 	case DT_ACT_DISCARD:
1128 		dt_action_discard(dtp, dnp->dn_expr, sdp);
1129 		break;
1130 	case DT_ACT_EXIT:
1131 		dt_action_exit(dtp, dnp->dn_expr, sdp);
1132 		break;
1133 	case DT_ACT_FREOPEN:
1134 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1135 		break;
1136 	case DT_ACT_FTRUNCATE:
1137 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1138 		break;
1139 	case DT_ACT_MOD:
1140 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1141 		break;
1142 	case DT_ACT_NORMALIZE:
1143 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
1144 		break;
1145 	case DT_ACT_PANIC:
1146 		dt_action_panic(dtp, dnp->dn_expr, sdp);
1147 		break;
1148 	case DT_ACT_PRINTA:
1149 		dt_action_printa(dtp, dnp->dn_expr, sdp);
1150 		break;
1151 	case DT_ACT_PRINTF:
1152 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1153 		break;
1154 	case DT_ACT_PRINTM:
1155 		dt_action_printm(dtp, dnp->dn_expr, sdp);
1156 		break;
1157 	case DT_ACT_PRINTT:
1158 		dt_action_printt(dtp, dnp->dn_expr, sdp);
1159 		break;
1160 	case DT_ACT_RAISE:
1161 		dt_action_raise(dtp, dnp->dn_expr, sdp);
1162 		break;
1163 	case DT_ACT_SETOPT:
1164 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1165 		break;
1166 	case DT_ACT_SPECULATE:
1167 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
1168 		break;
1169 	case DT_ACT_STACK:
1170 		dt_action_stack(dtp, dnp->dn_expr, sdp);
1171 		break;
1172 	case DT_ACT_STOP:
1173 		dt_action_stop(dtp, dnp->dn_expr, sdp);
1174 		break;
1175 	case DT_ACT_SYM:
1176 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1177 		break;
1178 	case DT_ACT_SYSTEM:
1179 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1180 		break;
1181 	case DT_ACT_TRACE:
1182 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1183 		break;
1184 	case DT_ACT_PRINT:
1185 		dt_action_print(dtp, dnp->dn_expr, sdp);
1186 		break;
1187 	case DT_ACT_TRACEMEM:
1188 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1189 		break;
1190 	case DT_ACT_TRUNC:
1191 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
1192 		break;
1193 	case DT_ACT_UADDR:
1194 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1195 		break;
1196 	case DT_ACT_UMOD:
1197 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1198 		break;
1199 	case DT_ACT_USYM:
1200 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1201 		break;
1202 	case DT_ACT_USTACK:
1203 	case DT_ACT_JSTACK:
1204 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
1205 		break;
1206 	default:
1207 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1208 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1209 	}
1210 }
1211 
1212 static void
1213 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1214 {
1215 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1216 
1217 	dt_cg(yypcb, dnp->dn_expr);
1218 	ap->dtad_difo = dt_as(yypcb);
1219 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1220 	ap->dtad_kind = DTRACEACT_DIFEXPR;
1221 }
1222 
1223 static void
1224 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1225 {
1226 	dt_ident_t *aid, *fid;
1227 	dt_node_t *anp, *incr = NULL;
1228 	dtrace_actdesc_t *ap;
1229 	uint_t n = 1, argmax;
1230 	uint64_t arg = 0;
1231 
1232 	/*
1233 	 * If the aggregation has no aggregating function applied to it, then
1234 	 * this statement has no effect.  Flag this as a programming error.
1235 	 */
1236 	if (dnp->dn_aggfun == NULL) {
1237 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1238 		    dnp->dn_ident->di_name);
1239 	}
1240 
1241 	aid = dnp->dn_ident;
1242 	fid = dnp->dn_aggfun->dn_ident;
1243 
1244 	if (dnp->dn_aggfun->dn_args != NULL &&
1245 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1246 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1247 		    "be of scalar type\n", fid->di_name);
1248 	}
1249 
1250 	/*
1251 	 * The ID of the aggregation itself is implicitly recorded as the first
1252 	 * member of each aggregation tuple so we can distinguish them later.
1253 	 */
1254 	ap = dt_stmt_action(dtp, sdp);
1255 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1256 
1257 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1258 		ap = dt_stmt_action(dtp, sdp);
1259 		n++;
1260 
1261 		if (anp->dn_kind == DT_NODE_FUNC) {
1262 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
1263 				dt_action_stack_args(dtp, ap, anp->dn_args);
1264 				continue;
1265 			}
1266 
1267 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1268 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
1269 				dt_action_ustack_args(dtp, ap, anp);
1270 				continue;
1271 			}
1272 
1273 			switch (anp->dn_ident->di_id) {
1274 			case DT_ACT_UADDR:
1275 				dt_action_symmod_args(dtp, ap,
1276 				    anp->dn_args, DTRACEACT_UADDR);
1277 				continue;
1278 
1279 			case DT_ACT_USYM:
1280 				dt_action_symmod_args(dtp, ap,
1281 				    anp->dn_args, DTRACEACT_USYM);
1282 				continue;
1283 
1284 			case DT_ACT_UMOD:
1285 				dt_action_symmod_args(dtp, ap,
1286 				    anp->dn_args, DTRACEACT_UMOD);
1287 				continue;
1288 
1289 			case DT_ACT_SYM:
1290 				dt_action_symmod_args(dtp, ap,
1291 				    anp->dn_args, DTRACEACT_SYM);
1292 				continue;
1293 
1294 			case DT_ACT_MOD:
1295 				dt_action_symmod_args(dtp, ap,
1296 				    anp->dn_args, DTRACEACT_MOD);
1297 				continue;
1298 
1299 			default:
1300 				break;
1301 			}
1302 		}
1303 
1304 		dt_cg(yypcb, anp);
1305 		ap->dtad_difo = dt_as(yypcb);
1306 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1307 	}
1308 
1309 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1310 		/*
1311 		 * For linear quantization, we have between two and four
1312 		 * arguments in addition to the expression:
1313 		 *
1314 		 *    arg1 => Base value
1315 		 *    arg2 => Limit value
1316 		 *    arg3 => Quantization level step size (defaults to 1)
1317 		 *    arg4 => Quantization increment value (defaults to 1)
1318 		 */
1319 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1320 		dt_node_t *arg2 = arg1->dn_list;
1321 		dt_node_t *arg3 = arg2->dn_list;
1322 		dt_idsig_t *isp;
1323 		uint64_t nlevels, step = 1, oarg;
1324 		int64_t baseval, limitval;
1325 
1326 		if (arg1->dn_kind != DT_NODE_INT) {
1327 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1328 			    "argument #1 must be an integer constant\n");
1329 		}
1330 
1331 		baseval = (int64_t)arg1->dn_value;
1332 
1333 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
1334 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1335 			    "argument #1 must be a 32-bit quantity\n");
1336 		}
1337 
1338 		if (arg2->dn_kind != DT_NODE_INT) {
1339 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1340 			    "argument #2 must be an integer constant\n");
1341 		}
1342 
1343 		limitval = (int64_t)arg2->dn_value;
1344 
1345 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
1346 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1347 			    "argument #2 must be a 32-bit quantity\n");
1348 		}
1349 
1350 		if (limitval < baseval) {
1351 			dnerror(dnp, D_LQUANT_MISMATCH,
1352 			    "lquantize( ) base (argument #1) must be less "
1353 			    "than limit (argument #2)\n");
1354 		}
1355 
1356 		if (arg3 != NULL) {
1357 			if (!dt_node_is_posconst(arg3)) {
1358 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1359 				    "argument #3 must be a non-zero positive "
1360 				    "integer constant\n");
1361 			}
1362 
1363 			if ((step = arg3->dn_value) > UINT16_MAX) {
1364 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1365 				    "argument #3 must be a 16-bit quantity\n");
1366 			}
1367 		}
1368 
1369 		nlevels = (limitval - baseval) / step;
1370 
1371 		if (nlevels == 0) {
1372 			dnerror(dnp, D_LQUANT_STEPLARGE,
1373 			    "lquantize( ) step (argument #3) too large: must "
1374 			    "have at least one quantization level\n");
1375 		}
1376 
1377 		if (nlevels > UINT16_MAX) {
1378 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1379 			    "(argument #3) too small: number of quantization "
1380 			    "levels must be a 16-bit quantity\n");
1381 		}
1382 
1383 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1384 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1385 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1386 		    DTRACE_LQUANTIZE_BASEMASK);
1387 
1388 		assert(arg != 0);
1389 
1390 		isp = (dt_idsig_t *)aid->di_data;
1391 
1392 		if (isp->dis_auxinfo == 0) {
1393 			/*
1394 			 * This is the first time we've seen an lquantize()
1395 			 * for this aggregation; we'll store our argument
1396 			 * as the auxiliary signature information.
1397 			 */
1398 			isp->dis_auxinfo = arg;
1399 		} else if ((oarg = isp->dis_auxinfo) != arg) {
1400 			/*
1401 			 * If we have seen this lquantize() before and the
1402 			 * argument doesn't match the original argument, pick
1403 			 * the original argument apart to concisely report the
1404 			 * mismatch.
1405 			 */
1406 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1407 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1408 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1409 
1410 			if (obaseval != baseval) {
1411 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1412 				    "base (argument #1) doesn't match previous "
1413 				    "declaration: expected %d, found %d\n",
1414 				    obaseval, (int)baseval);
1415 			}
1416 
1417 			if (onlevels * ostep != nlevels * step) {
1418 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1419 				    "limit (argument #2) doesn't match previous"
1420 				    " declaration: expected %d, found %d\n",
1421 				    obaseval + onlevels * ostep,
1422 				    (int)baseval + (int)nlevels * (int)step);
1423 			}
1424 
1425 			if (ostep != step) {
1426 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1427 				    "step (argument #3) doesn't match previous "
1428 				    "declaration: expected %d, found %d\n",
1429 				    ostep, (int)step);
1430 			}
1431 
1432 			/*
1433 			 * We shouldn't be able to get here -- one of the
1434 			 * parameters must be mismatched if the arguments
1435 			 * didn't match.
1436 			 */
1437 			assert(0);
1438 		}
1439 
1440 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1441 		argmax = 5;
1442 	}
1443 
1444 	if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1445 		/*
1446 		 * For log/linear quantizations, we have between one and five
1447 		 * arguments in addition to the expression:
1448 		 *
1449 		 *    arg1 => Factor
1450 		 *    arg2 => Low magnitude
1451 		 *    arg3 => High magnitude
1452 		 *    arg4 => Number of steps per magnitude
1453 		 *    arg5 => Quantization increment value (defaults to 1)
1454 		 */
1455 		dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1456 		uint64_t oarg, order, v;
1457 		dt_idsig_t *isp;
1458 		int i;
1459 
1460 		struct {
1461 			char *str;		/* string identifier */
1462 			int badtype;		/* error on bad type */
1463 			int badval;		/* error on bad value */
1464 			int mismatch;		/* error on bad match */
1465 			int shift;		/* shift value */
1466 			uint16_t value;		/* value itself */
1467 		} args[] = {
1468 			{ "factor", D_LLQUANT_FACTORTYPE,
1469 			    D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1470 			    DTRACE_LLQUANTIZE_FACTORSHIFT },
1471 			{ "low magnitude", D_LLQUANT_LOWTYPE,
1472 			    D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1473 			    DTRACE_LLQUANTIZE_LOWSHIFT },
1474 			{ "high magnitude", D_LLQUANT_HIGHTYPE,
1475 			    D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1476 			    DTRACE_LLQUANTIZE_HIGHSHIFT },
1477 			{ "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1478 			    D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1479 			    DTRACE_LLQUANTIZE_NSTEPSHIFT },
1480 			{ NULL }
1481 		};
1482 
1483 		assert(arg == 0);
1484 
1485 		for (i = 0; args[i].str != NULL; i++) {
1486 			if (llarg->dn_kind != DT_NODE_INT) {
1487 				dnerror(llarg, args[i].badtype, "llquantize( ) "
1488 				    "argument #%d (%s) must be an "
1489 				    "integer constant\n", i + 1, args[i].str);
1490 			}
1491 
1492 			if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1493 				dnerror(llarg, args[i].badval, "llquantize( ) "
1494 				    "argument #%d (%s) must be an unsigned "
1495 				    "16-bit quantity\n", i + 1, args[i].str);
1496 			}
1497 
1498 			args[i].value = (uint16_t)llarg->dn_value;
1499 
1500 			assert(!(arg & ((uint64_t)UINT16_MAX <<
1501 			    args[i].shift)));
1502 			arg |= ((uint64_t)args[i].value << args[i].shift);
1503 			llarg = llarg->dn_list;
1504 		}
1505 
1506 		assert(arg != 0);
1507 
1508 		if (args[0].value < 2) {
1509 			dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1510 			    "factor (argument #1) must be two or more\n");
1511 		}
1512 
1513 		if (args[1].value >= args[2].value) {
1514 			dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1515 			    "high magnitude (argument #3) must be greater "
1516 			    "than low magnitude (argument #2)\n");
1517 		}
1518 
1519 		if (args[3].value < args[0].value) {
1520 			dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1521 			    "factor (argument #1) must be less than or "
1522 			    "equal to the number of linear steps per "
1523 			    "magnitude (argument #4)\n");
1524 		}
1525 
1526 		for (v = args[0].value; v < args[3].value; v *= args[0].value)
1527 			continue;
1528 
1529 		if ((args[3].value % args[0].value) || (v % args[3].value)) {
1530 			dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1531 			    "factor (argument #1) must evenly divide the "
1532 			    "number of steps per magnitude (argument #4), "
1533 			    "and the number of steps per magnitude must evenly "
1534 			    "divide a power of the factor\n");
1535 		}
1536 
1537 		for (i = 0, order = 1; i < args[2].value; i++) {
1538 			if (order * args[0].value > order) {
1539 				order *= args[0].value;
1540 				continue;
1541 			}
1542 
1543 			dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1544 			    "factor (%d) raised to power of high magnitude "
1545 			    "(%d) overflows 64-bits\n", args[0].value,
1546 			    args[2].value);
1547 		}
1548 
1549 		isp = (dt_idsig_t *)aid->di_data;
1550 
1551 		if (isp->dis_auxinfo == 0) {
1552 			/*
1553 			 * This is the first time we've seen an llquantize()
1554 			 * for this aggregation; we'll store our argument
1555 			 * as the auxiliary signature information.
1556 			 */
1557 			isp->dis_auxinfo = arg;
1558 		} else if ((oarg = isp->dis_auxinfo) != arg) {
1559 			/*
1560 			 * If we have seen this llquantize() before and the
1561 			 * argument doesn't match the original argument, pick
1562 			 * the original argument apart to concisely report the
1563 			 * mismatch.
1564 			 */
1565 			int expected = 0, found = 0;
1566 
1567 			for (i = 0; expected == found; i++) {
1568 				assert(args[i].str != NULL);
1569 
1570 				expected = (oarg >> args[i].shift) & UINT16_MAX;
1571 				found = (arg >> args[i].shift) & UINT16_MAX;
1572 			}
1573 
1574 			dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1575 			    "%s (argument #%d) doesn't match previous "
1576 			    "declaration: expected %d, found %d\n",
1577 			    args[i - 1].str, i, expected, found);
1578 		}
1579 
1580 		incr = llarg;
1581 		argmax = 6;
1582 	}
1583 
1584 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1585 		incr = dnp->dn_aggfun->dn_args->dn_list;
1586 		argmax = 2;
1587 	}
1588 
1589 	if (incr != NULL) {
1590 		if (!dt_node_is_scalar(incr)) {
1591 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1592 			    "(argument #%d) must be of scalar type\n",
1593 			    fid->di_name, argmax);
1594 		}
1595 
1596 		if ((anp = incr->dn_list) != NULL) {
1597 			int argc = argmax;
1598 
1599 			for (; anp != NULL; anp = anp->dn_list)
1600 				argc++;
1601 
1602 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1603 			    "mismatch: %d args passed, at most %d expected",
1604 			    fid->di_name, argc, argmax);
1605 		}
1606 
1607 		ap = dt_stmt_action(dtp, sdp);
1608 		n++;
1609 
1610 		dt_cg(yypcb, incr);
1611 		ap->dtad_difo = dt_as(yypcb);
1612 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1613 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1614 	}
1615 
1616 	assert(sdp->dtsd_aggdata == NULL);
1617 	sdp->dtsd_aggdata = aid;
1618 
1619 	ap = dt_stmt_action(dtp, sdp);
1620 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1621 	assert(DTRACEACT_ISAGG(fid->di_id));
1622 	ap->dtad_kind = fid->di_id;
1623 	ap->dtad_ntuple = n;
1624 	ap->dtad_arg = arg;
1625 
1626 	if (dnp->dn_aggfun->dn_args != NULL) {
1627 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1628 		ap->dtad_difo = dt_as(yypcb);
1629 	}
1630 }
1631 
1632 static void
1633 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1634 {
1635 	dtrace_ecbdesc_t *edp;
1636 	dtrace_stmtdesc_t *sdp;
1637 	dt_node_t *dnp;
1638 
1639 	yylineno = pnp->dn_line;
1640 	dt_setcontext(dtp, pnp->dn_desc);
1641 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
1642 
1643 	if (DT_TREEDUMP_PASS(dtp, 2))
1644 		dt_node_printr(cnp, stderr, 0);
1645 
1646 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1647 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1648 
1649 	assert(yypcb->pcb_ecbdesc == NULL);
1650 	yypcb->pcb_ecbdesc = edp;
1651 
1652 	if (cnp->dn_pred != NULL) {
1653 		dt_cg(yypcb, cnp->dn_pred);
1654 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1655 	}
1656 
1657 	if (cnp->dn_acts == NULL) {
1658 		dt_stmt_append(dt_stmt_create(dtp, edp,
1659 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
1660 	}
1661 
1662 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1663 		assert(yypcb->pcb_stmt == NULL);
1664 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1665 
1666 		switch (dnp->dn_kind) {
1667 		case DT_NODE_DEXPR:
1668 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1669 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
1670 			else
1671 				dt_compile_exp(dtp, dnp, sdp);
1672 			break;
1673 		case DT_NODE_DFUNC:
1674 			dt_compile_fun(dtp, dnp, sdp);
1675 			break;
1676 		case DT_NODE_AGG:
1677 			dt_compile_agg(dtp, dnp, sdp);
1678 			break;
1679 		default:
1680 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1681 			    "%u is not a valid statement\n", dnp->dn_kind);
1682 		}
1683 
1684 		assert(yypcb->pcb_stmt == sdp);
1685 		dt_stmt_append(sdp, dnp);
1686 	}
1687 
1688 	assert(yypcb->pcb_ecbdesc == edp);
1689 	dt_ecbdesc_release(dtp, edp);
1690 	dt_endcontext(dtp);
1691 	yypcb->pcb_ecbdesc = NULL;
1692 }
1693 
1694 static void
1695 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1696 {
1697 	dt_node_t *pnp;
1698 
1699 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1700 		dt_compile_one_clause(dtp, cnp, pnp);
1701 }
1702 
1703 static void
1704 dt_compile_xlator(dt_node_t *dnp)
1705 {
1706 	dt_xlator_t *dxp = dnp->dn_xlator;
1707 	dt_node_t *mnp;
1708 
1709 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1710 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1711 		dt_cg(yypcb, mnp);
1712 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1713 	}
1714 }
1715 
1716 void
1717 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1718 {
1719 	const dtrace_pattr_t *pap;
1720 	dt_probe_t *prp;
1721 	dt_provider_t *pvp;
1722 	dt_ident_t *idp;
1723 	char attrstr[8];
1724 	int err;
1725 
1726 	/*
1727 	 * Both kernel and pid based providers are allowed to have names
1728 	 * ending with what could be interpreted as a number. We assume it's
1729 	 * a pid and that we may need to dynamically create probes for
1730 	 * that process if:
1731 	 *
1732 	 * (1) The provider doesn't exist, or,
1733 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1734 	 *
1735 	 * On an error, dt_pid_create_probes() will set the error message
1736 	 * and tag -- we just have to longjmp() out of here.
1737 	 */
1738 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1739 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1740 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1741 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1742 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1743 	}
1744 
1745 	/*
1746 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
1747 	 * a representative probe is found, set 'pap' to the probe provider's
1748 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
1749 	 */
1750 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1751 		pap = &_dtrace_prvdesc;
1752 		err = dtrace_errno(dtp);
1753 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1754 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1755 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1756 	} else {
1757 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1758 		err = 0;
1759 	}
1760 
1761 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1762 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1763 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1764 		    pdp->dtpd_func, pdp->dtpd_name);
1765 	}
1766 
1767 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1768 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1769 
1770 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1771 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1772 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1773 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1774 
1775 	/*
1776 	 * Reset the stability attributes of D global variables that vary
1777 	 * based on the attributes of the provider and context itself.
1778 	 */
1779 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1780 		idp->di_attr = pap->dtpa_provider;
1781 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1782 		idp->di_attr = pap->dtpa_mod;
1783 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1784 		idp->di_attr = pap->dtpa_func;
1785 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1786 		idp->di_attr = pap->dtpa_name;
1787 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1788 		idp->di_attr = pap->dtpa_args;
1789 
1790 	yypcb->pcb_pdesc = pdp;
1791 	yypcb->pcb_probe = prp;
1792 }
1793 
1794 /*
1795  * Reset context-dependent variables and state at the end of cooking a D probe
1796  * definition clause.  This ensures that external declarations between clauses
1797  * do not reference any stale context-dependent data from the previous clause.
1798  */
1799 void
1800 dt_endcontext(dtrace_hdl_t *dtp)
1801 {
1802 	static const char *const cvars[] = {
1803 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
1804 	};
1805 
1806 	dt_ident_t *idp;
1807 	int i;
1808 
1809 	for (i = 0; cvars[i] != NULL; i++) {
1810 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1811 			idp->di_attr = _dtrace_defattr;
1812 	}
1813 
1814 	yypcb->pcb_pdesc = NULL;
1815 	yypcb->pcb_probe = NULL;
1816 }
1817 
1818 static int
1819 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1820 {
1821 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1822 		dt_idhash_delete(dhp, idp);
1823 
1824 	return (0);
1825 }
1826 
1827 /*
1828  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1829  * any identifiers or translators that have been previously defined as bound to
1830  * a version greater than the specified version.  Therefore, in our current
1831  * version implementation, establishing a binding is a one-way transformation.
1832  * In addition, no versioning is currently provided for types as our .d library
1833  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1834  * for our exclusive use.  If required, type versioning will require more work.
1835  */
1836 int
1837 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1838 {
1839 	char s[DT_VERSION_STRMAX];
1840 	dt_xlator_t *dxp, *nxp;
1841 
1842 	if (v > dtp->dt_vmax)
1843 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
1844 	else if (v == dtp->dt_vmax)
1845 		return (0); /* no reduction necessary */
1846 
1847 	dt_dprintf("reducing api version to %s\n",
1848 	    dt_version_num2str(v, s, sizeof (s)));
1849 
1850 	dtp->dt_vmax = v;
1851 
1852 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1853 		nxp = dt_list_next(dxp);
1854 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1855 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1856 			dt_list_delete(&dtp->dt_xlators, dxp);
1857 	}
1858 
1859 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1860 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1861 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1862 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1863 
1864 	return (0);
1865 }
1866 
1867 /*
1868  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1869  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
1870  * here to simplify the code by leveraging file descriptor inheritance.
1871  */
1872 static FILE *
1873 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1874 {
1875 	int argc = dtp->dt_cpp_argc;
1876 	char **argv = malloc(sizeof (char *) * (argc + 5));
1877 	FILE *ofp = tmpfile();
1878 
1879 #if defined(sun)
1880 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1881 #endif
1882 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1883 
1884 	struct sigaction act, oact;
1885 	sigset_t mask, omask;
1886 
1887 	int wstat, estat;
1888 	pid_t pid;
1889 #if defined(sun)
1890 	off64_t off;
1891 #else
1892 	off_t off = 0;
1893 #endif
1894 	int c;
1895 
1896 	if (argv == NULL || ofp == NULL) {
1897 		(void) dt_set_errno(dtp, errno);
1898 		goto err;
1899 	}
1900 
1901 	/*
1902 	 * If the input is a seekable file, see if it is an interpreter file.
1903 	 * If we see #!, seek past the first line because cpp will choke on it.
1904 	 * We start cpp just prior to the \n at the end of this line so that
1905 	 * it still sees the newline, ensuring that #line values are correct.
1906 	 */
1907 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1908 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1909 			for (off += 2; c != '\n'; off++) {
1910 				if ((c = fgetc(ifp)) == EOF)
1911 					break;
1912 			}
1913 			if (c == '\n')
1914 				off--; /* start cpp just prior to \n */
1915 		}
1916 		(void) fflush(ifp);
1917 		(void) fseeko64(ifp, off, SEEK_SET);
1918 	}
1919 
1920 #if defined(sun)
1921 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1922 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1923 #endif
1924 
1925 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1926 
1927 	(void) snprintf(verdef, sizeof (verdef),
1928 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1929 	argv[argc++] = verdef;
1930 
1931 #if defined(sun)
1932 	switch (dtp->dt_stdcmode) {
1933 	case DT_STDC_XA:
1934 	case DT_STDC_XT:
1935 		argv[argc++] = "-D__STDC__=0";
1936 		break;
1937 	case DT_STDC_XC:
1938 		argv[argc++] = "-D__STDC__=1";
1939 		break;
1940 	}
1941 
1942 	argv[argc++] = ipath;
1943 	argv[argc++] = opath;
1944 #else
1945 	argv[argc++] = "-P";
1946 #endif
1947 	argv[argc] = NULL;
1948 
1949 	/*
1950 	 * libdtrace must be able to be embedded in other programs that may
1951 	 * include application-specific signal handlers.  Therefore, if we
1952 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1953 	 * that could confuse the containing application.  To do this,
1954 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
1955 	 * We restore our signal state once we are done.
1956 	 */
1957 	(void) sigemptyset(&mask);
1958 	(void) sigaddset(&mask, SIGCHLD);
1959 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
1960 
1961 	bzero(&act, sizeof (act));
1962 	act.sa_handler = SIG_DFL;
1963 	(void) sigaction(SIGCHLD, &act, &oact);
1964 
1965 	if ((pid = fork1()) == -1) {
1966 		(void) sigaction(SIGCHLD, &oact, NULL);
1967 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1968 		(void) dt_set_errno(dtp, EDT_CPPFORK);
1969 		goto err;
1970 	}
1971 
1972 	if (pid == 0) {
1973 #if !defined(sun)
1974 		if (isatty(fileno(ifp)) == 0)
1975 			lseek(fileno(ifp), off, SEEK_SET);
1976 		dup2(fileno(ifp), 0);
1977 		dup2(fileno(ofp), 1);
1978 #endif
1979 		(void) execvp(dtp->dt_cpp_path, argv);
1980 		_exit(errno == ENOENT ? 127 : 126);
1981 	}
1982 
1983 	do {
1984 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1985 		    (int)pid);
1986 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1987 
1988 	(void) sigaction(SIGCHLD, &oact, NULL);
1989 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1990 
1991 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1992 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1993 
1994 	if (estat != 0) {
1995 		switch (estat) {
1996 		case 126:
1997 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
1998 			break;
1999 		case 127:
2000 			(void) dt_set_errno(dtp, EDT_CPPENT);
2001 			break;
2002 		default:
2003 			(void) dt_set_errno(dtp, EDT_CPPERR);
2004 		}
2005 		goto err;
2006 	}
2007 
2008 	free(argv);
2009 	(void) fflush(ofp);
2010 	(void) fseek(ofp, 0, SEEK_SET);
2011 	return (ofp);
2012 
2013 err:
2014 	free(argv);
2015 	(void) fclose(ofp);
2016 	return (NULL);
2017 }
2018 
2019 static void
2020 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
2021 {
2022 	va_list ap;
2023 
2024 	va_start(ap, format);
2025 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
2026 	va_end(ap);
2027 }
2028 
2029 int
2030 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
2031 {
2032 	dt_lib_depend_t *dld;
2033 	const char *end;
2034 
2035 	assert(arg != NULL);
2036 
2037 	if ((end = strrchr(arg, '/')) == NULL)
2038 		return (dt_set_errno(dtp, EINVAL));
2039 
2040 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2041 		return (-1);
2042 
2043 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
2044 		dt_free(dtp, dld);
2045 		return (-1);
2046 	}
2047 
2048 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
2049 	if ((dld->dtld_library = strdup(arg)) == NULL) {
2050 		dt_free(dtp, dld->dtld_libpath);
2051 		dt_free(dtp, dld);
2052 		return (dt_set_errno(dtp, EDT_NOMEM));
2053 	}
2054 
2055 	dt_list_append(dlp, dld);
2056 	return (0);
2057 }
2058 
2059 dt_lib_depend_t *
2060 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
2061 {
2062 	dt_lib_depend_t *dldn;
2063 
2064 	for (dldn = dt_list_next(dld); dldn != NULL;
2065 	    dldn = dt_list_next(dldn)) {
2066 		if (strcmp(dldn->dtld_library, arg) == 0)
2067 			return (dldn);
2068 	}
2069 
2070 	return (NULL);
2071 }
2072 
2073 /*
2074  * Go through all the library files, and, if any library dependencies exist for
2075  * that file, add it to that node's list of dependents. The result of this
2076  * will be a graph which can then be topologically sorted to produce a
2077  * compilation order.
2078  */
2079 static int
2080 dt_lib_build_graph(dtrace_hdl_t *dtp)
2081 {
2082 	dt_lib_depend_t *dld, *dpld;
2083 
2084 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2085 	    dld = dt_list_next(dld)) {
2086 		char *library = dld->dtld_library;
2087 
2088 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2089 		    dpld = dt_list_next(dpld)) {
2090 			dt_lib_depend_t *dlda;
2091 
2092 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2093 			    dpld->dtld_library)) == NULL) {
2094 				dt_lib_depend_error(dtp,
2095 				    "Invalid library dependency in %s: %s\n",
2096 				    dld->dtld_library, dpld->dtld_library);
2097 
2098 				return (dt_set_errno(dtp, EDT_COMPILER));
2099 			}
2100 
2101 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2102 			    library)) != 0) {
2103 				return (-1); /* preserve dt_errno */
2104 			}
2105 		}
2106 	}
2107 	return (0);
2108 }
2109 
2110 static int
2111 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2112 {
2113 	dt_lib_depend_t *dpld, *dlda, *new;
2114 
2115 	dld->dtld_start = ++(*count);
2116 
2117 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2118 	    dpld = dt_list_next(dpld)) {
2119 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2120 		    dpld->dtld_library);
2121 		assert(dlda != NULL);
2122 
2123 		if (dlda->dtld_start == 0 &&
2124 		    dt_topo_sort(dtp, dlda, count) == -1)
2125 			return (-1);
2126 	}
2127 
2128 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2129 		return (-1);
2130 
2131 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2132 		dt_free(dtp, new);
2133 		return (dt_set_errno(dtp, EDT_NOMEM));
2134 	}
2135 
2136 	new->dtld_start = dld->dtld_start;
2137 	new->dtld_finish = dld->dtld_finish = ++(*count);
2138 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2139 
2140 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2141 	    new->dtld_start, new->dtld_finish);
2142 
2143 	return (0);
2144 }
2145 
2146 static int
2147 dt_lib_depend_sort(dtrace_hdl_t *dtp)
2148 {
2149 	dt_lib_depend_t *dld, *dpld, *dlda;
2150 	int count = 0;
2151 
2152 	if (dt_lib_build_graph(dtp) == -1)
2153 		return (-1); /* preserve dt_errno */
2154 
2155 	/*
2156 	 * Perform a topological sort of the graph that hangs off
2157 	 * dtp->dt_lib_dep. The result of this process will be a
2158 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2159 	 */
2160 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2161 	    dld = dt_list_next(dld)) {
2162 		if (dld->dtld_start == 0 &&
2163 		    dt_topo_sort(dtp, dld, &count) == -1)
2164 			return (-1); /* preserve dt_errno */;
2165 	}
2166 
2167 	/*
2168 	 * Check the graph for cycles. If an ancestor's finishing time is
2169 	 * less than any of its dependent's finishing times then a back edge
2170 	 * exists in the graph and this is a cycle.
2171 	 */
2172 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2173 	    dld = dt_list_next(dld)) {
2174 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2175 		    dpld = dt_list_next(dpld)) {
2176 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2177 			    dpld->dtld_library);
2178 			assert(dlda != NULL);
2179 
2180 			if (dlda->dtld_finish > dld->dtld_finish) {
2181 				dt_lib_depend_error(dtp,
2182 				    "Cyclic dependency detected: %s => %s\n",
2183 				    dld->dtld_library, dpld->dtld_library);
2184 
2185 				return (dt_set_errno(dtp, EDT_COMPILER));
2186 			}
2187 		}
2188 	}
2189 
2190 	return (0);
2191 }
2192 
2193 static void
2194 dt_lib_depend_free(dtrace_hdl_t *dtp)
2195 {
2196 	dt_lib_depend_t *dld, *dlda;
2197 
2198 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2199 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2200 			dt_list_delete(&dld->dtld_dependencies, dlda);
2201 			dt_free(dtp, dlda->dtld_library);
2202 			dt_free(dtp, dlda->dtld_libpath);
2203 			dt_free(dtp, dlda);
2204 		}
2205 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2206 			dt_list_delete(&dld->dtld_dependents, dlda);
2207 			dt_free(dtp, dlda->dtld_library);
2208 			dt_free(dtp, dlda->dtld_libpath);
2209 			dt_free(dtp, dlda);
2210 		}
2211 		dt_list_delete(&dtp->dt_lib_dep, dld);
2212 		dt_free(dtp, dld->dtld_library);
2213 		dt_free(dtp, dld->dtld_libpath);
2214 		dt_free(dtp, dld);
2215 	}
2216 
2217 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2218 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2219 		dt_free(dtp, dld->dtld_library);
2220 		dt_free(dtp, dld);
2221 	}
2222 }
2223 
2224 /*
2225  * Open all the .d library files found in the specified directory and
2226  * compile each one of them.  We silently ignore any missing directories and
2227  * other files found therein.  We only fail (and thereby fail dt_load_libs()) if
2228  * we fail to compile a library and the error is something other than #pragma D
2229  * depends_on.  Dependency errors are silently ignored to permit a library
2230  * directory to contain libraries which may not be accessible depending on our
2231  * privileges.
2232  */
2233 static int
2234 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2235 {
2236 	struct dirent *dp;
2237 	const char *p, *end;
2238 	DIR *dirp;
2239 
2240 	char fname[PATH_MAX];
2241 	FILE *fp;
2242 	void *rv;
2243 	dt_lib_depend_t *dld;
2244 
2245 	if ((dirp = opendir(path)) == NULL) {
2246 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2247 		return (0);
2248 	}
2249 
2250 	/* First, parse each file for library dependencies. */
2251 	while ((dp = readdir(dirp)) != NULL) {
2252 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2253 			continue; /* skip any filename not ending in .d */
2254 
2255 		(void) snprintf(fname, sizeof (fname),
2256 		    "%s/%s", path, dp->d_name);
2257 
2258 		if ((fp = fopen(fname, "r")) == NULL) {
2259 			dt_dprintf("skipping library %s: %s\n",
2260 			    fname, strerror(errno));
2261 			continue;
2262 		}
2263 
2264 		/*
2265 		 * Skip files whose name match an already processed library
2266 		 */
2267 		for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2268 		    dld = dt_list_next(dld)) {
2269 			end = strrchr(dld->dtld_library, '/');
2270 			/* dt_lib_depend_add ensures this */
2271 			assert(end != NULL);
2272 			if (strcmp(end + 1, dp->d_name) == 0)
2273 				break;
2274 		}
2275 
2276 		if (dld != NULL) {
2277 			dt_dprintf("skipping library %s, already processed "
2278 			    "library with the same name: %s", dp->d_name,
2279 			    dld->dtld_library);
2280 			continue;
2281 		}
2282 
2283 		dtp->dt_filetag = fname;
2284 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
2285 			return (-1); /* preserve dt_errno */
2286 
2287 		rv = dt_compile(dtp, DT_CTX_DPROG,
2288 		    DTRACE_PROBESPEC_NAME, NULL,
2289 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2290 
2291 		if (rv != NULL && dtp->dt_errno &&
2292 		    (dtp->dt_errno != EDT_COMPILER ||
2293 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2294 			return (-1); /* preserve dt_errno */
2295 
2296 		if (dtp->dt_errno)
2297 			dt_dprintf("error parsing library %s: %s\n",
2298 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2299 
2300 		(void) fclose(fp);
2301 		dtp->dt_filetag = NULL;
2302 	}
2303 
2304 	(void) closedir(dirp);
2305 
2306 	return (0);
2307 }
2308 
2309 /*
2310  * Perform a topological sorting of all the libraries found across the entire
2311  * dt_lib_path.  Once sorted, compile each one in topological order to cache its
2312  * inlines and translators, etc.  We silently ignore any missing directories and
2313  * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2314  * we fail to compile a library and the error is something other than #pragma D
2315  * depends_on.  Dependency errors are silently ignored to permit a library
2316  * directory to contain libraries which may not be accessible depending on our
2317  * privileges.
2318  */
2319 static int
2320 dt_load_libs_sort(dtrace_hdl_t *dtp)
2321 {
2322 	dtrace_prog_t *pgp;
2323 	FILE *fp;
2324 	dt_lib_depend_t *dld;
2325 
2326 	/*
2327 	 * Finish building the graph containing the library dependencies
2328 	 * and perform a topological sort to generate an ordered list
2329 	 * for compilation.
2330 	 */
2331 	if (dt_lib_depend_sort(dtp) == -1)
2332 		goto err;
2333 
2334 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2335 	    dld = dt_list_next(dld)) {
2336 
2337 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2338 			dt_dprintf("skipping library %s: %s\n",
2339 			    dld->dtld_library, strerror(errno));
2340 			continue;
2341 		}
2342 
2343 		dtp->dt_filetag = dld->dtld_library;
2344 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2345 		(void) fclose(fp);
2346 		dtp->dt_filetag = NULL;
2347 
2348 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2349 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2350 			goto err;
2351 
2352 		if (pgp == NULL) {
2353 			dt_dprintf("skipping library %s: %s\n",
2354 			    dld->dtld_library,
2355 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2356 		} else {
2357 			dld->dtld_loaded = B_TRUE;
2358 			dt_program_destroy(dtp, pgp);
2359 		}
2360 	}
2361 
2362 	dt_lib_depend_free(dtp);
2363 	return (0);
2364 
2365 err:
2366 	dt_lib_depend_free(dtp);
2367 	return (-1); /* preserve dt_errno */
2368 }
2369 
2370 /*
2371  * Load the contents of any appropriate DTrace .d library files.  These files
2372  * contain inlines and translators that will be cached by the compiler.  We
2373  * defer this activity until the first compile to permit libdtrace clients to
2374  * add their own library directories and so that we can properly report errors.
2375  */
2376 static int
2377 dt_load_libs(dtrace_hdl_t *dtp)
2378 {
2379 	dt_dirpath_t *dirp;
2380 
2381 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2382 		return (0); /* libraries already processed */
2383 
2384 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
2385 
2386 	/*
2387 	 * /usr/lib/dtrace is always at the head of the list. The rest of the
2388 	 * list is specified in the precedence order the user requested. Process
2389 	 * everything other than the head first. DTRACE_C_NOLIBS has already
2390 	 * been spcified so dt_vopen will ensure that there is always one entry
2391 	 * in dt_lib_path.
2392 	 */
2393 	for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
2394 	    dirp != NULL; dirp = dt_list_next(dirp)) {
2395 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2396 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2397 			return (-1); /* errno is set for us */
2398 		}
2399 	}
2400 
2401 	/* Handle /usr/lib/dtrace */
2402 	dirp = dt_list_next(&dtp->dt_lib_path);
2403 	if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2404 		dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2405 		return (-1); /* errno is set for us */
2406 	}
2407 
2408 	if (dt_load_libs_sort(dtp) < 0)
2409 		return (-1); /* errno is set for us */
2410 
2411 	return (0);
2412 }
2413 
2414 static void *
2415 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2416     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2417 {
2418 	dt_node_t *dnp;
2419 	dt_decl_t *ddp;
2420 	dt_pcb_t pcb;
2421 	void *rv;
2422 	int err;
2423 
2424 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2425 		(void) dt_set_errno(dtp, EINVAL);
2426 		return (NULL);
2427 	}
2428 
2429 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2430 		return (NULL); /* errno is set for us */
2431 
2432 	if (dtp->dt_globals->dh_nelems != 0)
2433 		(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2434 
2435 	if (dtp->dt_tls->dh_nelems != 0)
2436 		(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2437 
2438 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2439 		return (NULL); /* errno is set for us */
2440 
2441 	dt_pcb_push(dtp, &pcb);
2442 
2443 	pcb.pcb_fileptr = fp;
2444 	pcb.pcb_string = s;
2445 	pcb.pcb_strptr = s;
2446 	pcb.pcb_strlen = s ? strlen(s) : 0;
2447 	pcb.pcb_sargc = argc;
2448 	pcb.pcb_sargv = argv;
2449 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2450 	pcb.pcb_pspec = pspec;
2451 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
2452 	pcb.pcb_amin = dtp->dt_amin;
2453 	pcb.pcb_yystate = -1;
2454 	pcb.pcb_context = context;
2455 	pcb.pcb_token = context;
2456 
2457 	if (context != DT_CTX_DPROG)
2458 		yybegin(YYS_EXPR);
2459 	else if (cflags & DTRACE_C_CTL)
2460 		yybegin(YYS_CONTROL);
2461 	else
2462 		yybegin(YYS_CLAUSE);
2463 
2464 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2465 		goto out;
2466 
2467 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2468 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2469 
2470 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2471 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2472 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2473 
2474 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2475 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2476 
2477 	/*
2478 	 * Invoke the parser to evaluate the D source code.  If any errors
2479 	 * occur during parsing, an error function will be called and we
2480 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
2481 	 * we optionally display the parse tree if debugging is enabled.
2482 	 */
2483 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
2484 		xyerror(D_EMPTY, "empty D program translation unit\n");
2485 
2486 	yybegin(YYS_DONE);
2487 
2488 	if (cflags & DTRACE_C_CTL)
2489 		goto out;
2490 
2491 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2492 		dt_node_printr(yypcb->pcb_root, stderr, 0);
2493 
2494 	if (yypcb->pcb_pragmas != NULL)
2495 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2496 
2497 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2498 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2499 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2500 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2501 	}
2502 
2503 	/*
2504 	 * If we have successfully created a parse tree for a D program, loop
2505 	 * over the clauses and actions and instantiate the corresponding
2506 	 * libdtrace program.  If we are parsing a D expression, then we
2507 	 * simply run the code generator and assembler on the resulting tree.
2508 	 */
2509 	switch (context) {
2510 	case DT_CTX_DPROG:
2511 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2512 
2513 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2514 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2515 			xyerror(D_EMPTY, "empty D program translation unit\n");
2516 
2517 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2518 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2519 
2520 		for (; dnp != NULL; dnp = dnp->dn_list) {
2521 			switch (dnp->dn_kind) {
2522 			case DT_NODE_CLAUSE:
2523 				dt_compile_clause(dtp, dnp);
2524 				break;
2525 			case DT_NODE_XLATOR:
2526 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2527 					dt_compile_xlator(dnp);
2528 				break;
2529 			case DT_NODE_PROVIDER:
2530 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
2531 				break;
2532 			}
2533 		}
2534 
2535 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2536 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2537 		yypcb->pcb_asxrefs = NULL;
2538 		yypcb->pcb_asxreflen = 0;
2539 
2540 		rv = yypcb->pcb_prog;
2541 		break;
2542 
2543 	case DT_CTX_DEXPR:
2544 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2545 		dt_cg(yypcb, yypcb->pcb_root);
2546 		rv = dt_as(yypcb);
2547 		break;
2548 
2549 	case DT_CTX_DTYPE:
2550 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2551 		err = dt_decl_type(ddp, arg);
2552 		dt_decl_free(ddp);
2553 
2554 		if (err != 0)
2555 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2556 
2557 		rv = NULL;
2558 		break;
2559 	}
2560 
2561 out:
2562 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
2563 		dt_node_printr(yypcb->pcb_root, stderr, 0);
2564 
2565 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2566 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2567 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2568 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2569 
2570 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2571 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2572 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2573 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2574 
2575 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2576 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2577 
2578 	dt_pcb_pop(dtp, err);
2579 	(void) dt_set_errno(dtp, err);
2580 	return (err ? NULL : rv);
2581 }
2582 
2583 dtrace_prog_t *
2584 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2585     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2586 {
2587 	return (dt_compile(dtp, DT_CTX_DPROG,
2588 	    spec, NULL, cflags, argc, argv, NULL, s));
2589 }
2590 
2591 dtrace_prog_t *
2592 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2593     uint_t cflags, int argc, char *const argv[])
2594 {
2595 	return (dt_compile(dtp, DT_CTX_DPROG,
2596 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2597 }
2598 
2599 int
2600 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2601 {
2602 	(void) dt_compile(dtp, DT_CTX_DTYPE,
2603 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2604 	return (dtp->dt_errno ? -1 : 0);
2605 }
2606 
2607 int
2608 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2609 {
2610 	(void) dt_compile(dtp, DT_CTX_DTYPE,
2611 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2612 	return (dtp->dt_errno ? -1 : 0);
2613 }
2614