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