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