xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_cc.c (revision 12902:3bb859a7330c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55478Sjhaslam  * Common Development and Distribution License (the "License").
65478Sjhaslam  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211399Sahl 
220Sstevel@tonic-gate /*
23*12902SBryan.Cantrill@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * DTrace D Language Compiler
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * The code in this source file implements the main engine for the D language
300Sstevel@tonic-gate  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
310Sstevel@tonic-gate  * compiler operates on either stdio FILEs or in-memory strings as its input
320Sstevel@tonic-gate  * and can produce either dtrace_prog_t structures from a D program or a single
330Sstevel@tonic-gate  * dtrace_difo_t structure from a D expression.  Multiple entry points are
340Sstevel@tonic-gate  * provided as wrappers around dt_compile() for the various input/output pairs.
350Sstevel@tonic-gate  * The compiler itself is implemented across the following source files:
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * dt_lex.l - lex scanner
380Sstevel@tonic-gate  * dt_grammar.y - yacc grammar
390Sstevel@tonic-gate  * dt_parser.c - parse tree creation and semantic checking
400Sstevel@tonic-gate  * dt_decl.c - declaration stack processing
410Sstevel@tonic-gate  * dt_xlator.c - D translator lookup and creation
420Sstevel@tonic-gate  * dt_ident.c - identifier and symbol table routines
430Sstevel@tonic-gate  * dt_pragma.c - #pragma processing and D pragmas
440Sstevel@tonic-gate  * dt_printf.c - D printf() and printa() argument checking and processing
450Sstevel@tonic-gate  * dt_cc.c - compiler driver and dtrace_prog_t construction
460Sstevel@tonic-gate  * dt_cg.c - DIF code generator
470Sstevel@tonic-gate  * dt_as.c - DIF assembler
480Sstevel@tonic-gate  * dt_dof.c - dtrace_prog_t -> DOF conversion
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Several other source files provide collections of utility routines used by
510Sstevel@tonic-gate  * these major files.  The compiler itself is implemented in multiple passes:
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
540Sstevel@tonic-gate  *     and parse tree nodes are constructed using the routines in dt_parser.c.
550Sstevel@tonic-gate  *     This node construction pass is described further in dt_parser.c.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * (2) The parse tree is "cooked" by assigning each clause a context (see the
580Sstevel@tonic-gate  *     routine dt_setcontext(), below) based on its probe description and then
590Sstevel@tonic-gate  *     recursively descending the tree performing semantic checking.  The cook
600Sstevel@tonic-gate  *     routines are also implemented in dt_parser.c and described there.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * (3) For actions that are DIF expression statements, the DIF code generator
630Sstevel@tonic-gate  *     and assembler are invoked to create a finished DIFO for the statement.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * (4) The dtrace_prog_t data structures for the program clauses and actions
660Sstevel@tonic-gate  *     are built, containing pointers to any DIFOs created in step (3).
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
690Sstevel@tonic-gate  *     into DOF format for use in anonymous tracing or enabling in the kernel.
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * In the implementation, steps 2-4 are intertwined in that they are performed
720Sstevel@tonic-gate  * in order for each clause as part of a loop that executes over the clauses.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * The D compiler currently implements nearly no optimization.  The compiler
750Sstevel@tonic-gate  * implements integer constant folding as part of pass (1), and a set of very
760Sstevel@tonic-gate  * simple peephole optimizations as part of pass (3).  As with any C compiler,
770Sstevel@tonic-gate  * a large number of optimizations are possible on both the intermediate data
780Sstevel@tonic-gate  * structures and the generated DIF code.  These possibilities should be
790Sstevel@tonic-gate  * investigated in the context of whether they will have any substantive effect
800Sstevel@tonic-gate  * on the overall DTrace probe effect before they are undertaken.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #include <sys/types.h>
840Sstevel@tonic-gate #include <sys/wait.h>
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <assert.h>
870Sstevel@tonic-gate #include <strings.h>
880Sstevel@tonic-gate #include <signal.h>
890Sstevel@tonic-gate #include <unistd.h>
900Sstevel@tonic-gate #include <stdlib.h>
910Sstevel@tonic-gate #include <stdio.h>
920Sstevel@tonic-gate #include <errno.h>
930Sstevel@tonic-gate #include <ucontext.h>
940Sstevel@tonic-gate #include <limits.h>
950Sstevel@tonic-gate #include <ctype.h>
960Sstevel@tonic-gate #include <dirent.h>
970Sstevel@tonic-gate #include <dt_module.h>
98265Smws #include <dt_program.h>
990Sstevel@tonic-gate #include <dt_provider.h>
1000Sstevel@tonic-gate #include <dt_printf.h>
1010Sstevel@tonic-gate #include <dt_pid.h>
1020Sstevel@tonic-gate #include <dt_grammar.h>
1030Sstevel@tonic-gate #include <dt_ident.h>
1040Sstevel@tonic-gate #include <dt_string.h>
1050Sstevel@tonic-gate #include <dt_impl.h>
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static const dtrace_diftype_t dt_void_rtype = {
1080Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static const dtrace_diftype_t dt_int_rtype = {
1120Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
1130Sstevel@tonic-gate };
1140Sstevel@tonic-gate 
1155478Sjhaslam static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
1165478Sjhaslam     uint_t, int, char *const[], FILE *, const char *);
1175478Sjhaslam 
1185478Sjhaslam 
1190Sstevel@tonic-gate /*ARGSUSED*/
1200Sstevel@tonic-gate static int
dt_idreset(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)1210Sstevel@tonic-gate dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
1240Sstevel@tonic-gate 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
1250Sstevel@tonic-gate 	return (0);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*ARGSUSED*/
1290Sstevel@tonic-gate static int
dt_idpragma(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)1300Sstevel@tonic-gate dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	yylineno = idp->di_lineno;
1330Sstevel@tonic-gate 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
1340Sstevel@tonic-gate 	return (0);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static dtrace_stmtdesc_t *
dt_stmt_create(dtrace_hdl_t * dtp,dtrace_ecbdesc_t * edp,dtrace_attribute_t descattr,dtrace_attribute_t stmtattr)1380Sstevel@tonic-gate dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
1390Sstevel@tonic-gate     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (sdp == NULL)
1440Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	assert(yypcb->pcb_stmt == NULL);
1470Sstevel@tonic-gate 	yypcb->pcb_stmt = sdp;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	sdp->dtsd_descattr = descattr;
1500Sstevel@tonic-gate 	sdp->dtsd_stmtattr = stmtattr;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	return (sdp);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static dtrace_actdesc_t *
dt_stmt_action(dtrace_hdl_t * dtp,dtrace_stmtdesc_t * sdp)1560Sstevel@tonic-gate dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	dtrace_actdesc_t *new;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
1610Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return (new);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Utility function to determine if a given action description is destructive.
1680Sstevel@tonic-gate  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate static int
dt_action_destructive(const dtrace_actdesc_t * ap)1710Sstevel@tonic-gate dt_action_destructive(const dtrace_actdesc_t *ap)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
1740Sstevel@tonic-gate 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static void
dt_stmt_append(dtrace_stmtdesc_t * sdp,const dt_node_t * dnp)1780Sstevel@tonic-gate dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
1810Sstevel@tonic-gate 	dtrace_actdesc_t *ap, *tap;
1820Sstevel@tonic-gate 	int commit = 0;
1830Sstevel@tonic-gate 	int speculate = 0;
1840Sstevel@tonic-gate 	int datarec = 0;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * Make sure that the new statement jibes with the rest of the ECB.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
1900Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
1910Sstevel@tonic-gate 			if (commit) {
1920Sstevel@tonic-gate 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
1930Sstevel@tonic-gate 				    "not follow commit( )\n");
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 			if (datarec) {
1970Sstevel@tonic-gate 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
1980Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
2020Sstevel@tonic-gate 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
2030Sstevel@tonic-gate 					continue;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
2060Sstevel@tonic-gate 				    "may not follow commit( )\n");
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 			commit = 1;
2100Sstevel@tonic-gate 			continue;
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
2140Sstevel@tonic-gate 			if (speculate) {
2150Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
2160Sstevel@tonic-gate 				    "not follow speculate( )\n");
2170Sstevel@tonic-gate 			}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 			if (commit) {
2200Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
2210Sstevel@tonic-gate 				    "not follow commit( )\n");
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 			if (datarec) {
2250Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
2260Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 			speculate = 1;
2300Sstevel@tonic-gate 			continue;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
2340Sstevel@tonic-gate 			if (speculate) {
2350Sstevel@tonic-gate 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
2360Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2370Sstevel@tonic-gate 			}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 			datarec = 1;
2400Sstevel@tonic-gate 			continue;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		if (speculate) {
2440Sstevel@tonic-gate 			if (dt_action_destructive(ap)) {
2450Sstevel@tonic-gate 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
2460Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 			if (ap->dtad_kind == DTRACEACT_EXIT) {
2500Sstevel@tonic-gate 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
2510Sstevel@tonic-gate 				    "follow speculate( )\n");
2520Sstevel@tonic-gate 			}
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		/*
2560Sstevel@tonic-gate 		 * Exclude all non data-recording actions.
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 		if (dt_action_destructive(ap) ||
2590Sstevel@tonic-gate 		    ap->dtad_kind == DTRACEACT_DISCARD)
2600Sstevel@tonic-gate 			continue;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
2630Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
2640Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
2650Sstevel@tonic-gate 			continue;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		if (commit) {
2680Sstevel@tonic-gate 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
2690Sstevel@tonic-gate 			    "may not follow commit( )\n");
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		if (!speculate)
2730Sstevel@tonic-gate 			datarec = 1;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
2770Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (yypcb->pcb_stmt == sdp)
2800Sstevel@tonic-gate 		yypcb->pcb_stmt = NULL;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * For the first element of an aggregation tuple or for printa(), we create a
2850Sstevel@tonic-gate  * simple DIF program that simply returns the immediate value that is the ID
2860Sstevel@tonic-gate  * of the aggregation itself.  This could be optimized in the future by
2870Sstevel@tonic-gate  * creating a new in-kernel dtad_kind that just returns an integer.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate static void
dt_action_difconst(dtrace_actdesc_t * ap,uint_t id,dtrace_actkind_t kind)2900Sstevel@tonic-gate dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
2910Sstevel@tonic-gate {
292265Smws 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
293265Smws 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (dp == NULL)
2960Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2970Sstevel@tonic-gate 
298265Smws 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
299265Smws 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
302265Smws 		dt_difo_free(dtp, dp);
3030Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
3070Sstevel@tonic-gate 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
3080Sstevel@tonic-gate 	dp->dtdo_len = 2;
3090Sstevel@tonic-gate 	dp->dtdo_inttab[0] = id;
3100Sstevel@tonic-gate 	dp->dtdo_intlen = 1;
3110Sstevel@tonic-gate 	dp->dtdo_rtype = dt_int_rtype;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	ap->dtad_difo = dp;
3140Sstevel@tonic-gate 	ap->dtad_kind = kind;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate static void
dt_action_clear(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)3180Sstevel@tonic-gate dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	dt_ident_t *aid;
3210Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3220Sstevel@tonic-gate 	dt_node_t *anp;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3250Sstevel@tonic-gate 	int argc = 0;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3280Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (argc != 1) {
3310Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_PROTO,
3320Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
3330Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	anp = dnp->dn_args;
3370Sstevel@tonic-gate 	assert(anp != NULL);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3400Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGARG,
3410Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3420Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3430Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3440Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	aid = anp->dn_ident;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3500Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGBAD,
3510Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
3550Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
3560Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_CLEAR;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate static void
dt_action_normalize(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)3600Sstevel@tonic-gate dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	dt_ident_t *aid;
3630Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3640Sstevel@tonic-gate 	dt_node_t *anp, *normal;
3650Sstevel@tonic-gate 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3680Sstevel@tonic-gate 	int argc = 0;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3710Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
3740Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_PROTO,
3750Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
3760Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	anp = dnp->dn_args;
3800Sstevel@tonic-gate 	assert(anp != NULL);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3830Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGARG,
3840Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3850Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3860Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3870Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
3910Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_SCALAR,
3920Sstevel@tonic-gate 		    "%s( ) argument #2 must be of scalar type\n",
3930Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	aid = anp->dn_ident;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3990Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGBAD,
4000Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4040Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (denormal) {
4070Sstevel@tonic-gate 		ap->dtad_arg = DT_ACT_DENORMALIZE;
4080Sstevel@tonic-gate 		return;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	assert(normal != NULL);
4140Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4150Sstevel@tonic-gate 	dt_cg(yypcb, normal);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
4180Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_LIBACT;
4190Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate static void
dt_action_trunc(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)4230Sstevel@tonic-gate dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	dt_ident_t *aid;
4260Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4270Sstevel@tonic-gate 	dt_node_t *anp, *trunc;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4300Sstevel@tonic-gate 	int argc = 0;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
4330Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (argc > 2 || argc < 1) {
4360Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_PROTO,
4370Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
4380Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc,
4390Sstevel@tonic-gate 		    argc < 1 ? "at least 1" : "no more than 2");
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	anp = dnp->dn_args;
4430Sstevel@tonic-gate 	assert(anp != NULL);
4440Sstevel@tonic-gate 	trunc = anp->dn_list;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
4470Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGARG,
4480Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
4490Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
4500Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
4510Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (argc == 2) {
4550Sstevel@tonic-gate 		assert(trunc != NULL);
4560Sstevel@tonic-gate 		if (!dt_node_is_scalar(trunc)) {
4570Sstevel@tonic-gate 			dnerror(dnp, D_TRUNC_SCALAR,
4580Sstevel@tonic-gate 			    "%s( ) argument #2 must be of scalar type\n",
4590Sstevel@tonic-gate 			    dnp->dn_ident->di_name);
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	aid = anp->dn_ident;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4660Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGBAD,
4670Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4710Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4720Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (argc == 1) {
4770Sstevel@tonic-gate 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
4780Sstevel@tonic-gate 	} else {
4790Sstevel@tonic-gate 		assert(trunc != NULL);
4800Sstevel@tonic-gate 		dt_cg(yypcb, trunc);
4810Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
4820Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_LIBACT;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate static void
dt_action_printa(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)4890Sstevel@tonic-gate dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
4920Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4930Sstevel@tonic-gate 	const char *format;
4941017Sbmc 	dt_node_t *anp, *proto = NULL;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4970Sstevel@tonic-gate 	int argc = 0, argr = 0;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
5000Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	switch (dnp->dn_args->dn_kind) {
5030Sstevel@tonic-gate 	case DT_NODE_STRING:
5040Sstevel@tonic-gate 		format = dnp->dn_args->dn_string;
5050Sstevel@tonic-gate 		anp = dnp->dn_args->dn_list;
5060Sstevel@tonic-gate 		argr = 2;
5070Sstevel@tonic-gate 		break;
5080Sstevel@tonic-gate 	case DT_NODE_AGG:
5090Sstevel@tonic-gate 		format = NULL;
5100Sstevel@tonic-gate 		anp = dnp->dn_args;
5110Sstevel@tonic-gate 		argr = 1;
5120Sstevel@tonic-gate 		break;
5130Sstevel@tonic-gate 	default:
5140Sstevel@tonic-gate 		format = NULL;
5150Sstevel@tonic-gate 		anp = dnp->dn_args;
5160Sstevel@tonic-gate 		argr = 1;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5191017Sbmc 	if (argc < argr) {
5200Sstevel@tonic-gate 		dnerror(dnp, D_PRINTA_PROTO,
5210Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
5220Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, argr);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5251017Sbmc 	assert(anp != NULL);
5260Sstevel@tonic-gate 
5271017Sbmc 	while (anp != NULL) {
5281017Sbmc 		if (anp->dn_kind != DT_NODE_AGG) {
5291017Sbmc 			dnerror(dnp, D_PRINTA_AGGARG,
5301017Sbmc 			    "%s( ) argument #%d is incompatible with "
5311017Sbmc 			    "prototype:\n\tprototype: aggregation\n"
5321017Sbmc 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
5331017Sbmc 			    dt_node_type_name(anp, n, sizeof (n)));
5341017Sbmc 		}
5351017Sbmc 
5361017Sbmc 		aid = anp->dn_ident;
5371017Sbmc 		fid = aid->di_iarg;
5381017Sbmc 
5391017Sbmc 		if (aid->di_gen == dtp->dt_gen &&
5401017Sbmc 		    !(aid->di_flags & DT_IDFLG_MOD)) {
5411017Sbmc 			dnerror(dnp, D_PRINTA_AGGBAD,
5421017Sbmc 			    "undefined aggregation: @%s\n", aid->di_name);
5431017Sbmc 		}
5440Sstevel@tonic-gate 
5451017Sbmc 		/*
5461017Sbmc 		 * If we have multiple aggregations, we must be sure that
5471017Sbmc 		 * their key signatures match.
5481017Sbmc 		 */
5491017Sbmc 		if (proto != NULL) {
5501017Sbmc 			dt_printa_validate(proto, anp);
5511017Sbmc 		} else {
5521017Sbmc 			proto = anp;
5531017Sbmc 		}
5540Sstevel@tonic-gate 
5551017Sbmc 		if (format != NULL) {
5561017Sbmc 			yylineno = dnp->dn_line;
5570Sstevel@tonic-gate 
5581017Sbmc 			sdp->dtsd_fmtdata =
5591017Sbmc 			    dt_printf_create(yypcb->pcb_hdl, format);
5601017Sbmc 			dt_printf_validate(sdp->dtsd_fmtdata,
5611017Sbmc 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
5621017Sbmc 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
5631017Sbmc 			format = NULL;
5641017Sbmc 		}
5651017Sbmc 
5661017Sbmc 		ap = dt_stmt_action(dtp, sdp);
5671017Sbmc 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
5681017Sbmc 
5691017Sbmc 		anp = anp->dn_list;
5701017Sbmc 		argr++;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate static void
dt_action_printflike(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)5750Sstevel@tonic-gate dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
5760Sstevel@tonic-gate     dtrace_actkind_t kind)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	dt_node_t *anp, *arg1;
5790Sstevel@tonic-gate 	dtrace_actdesc_t *ap = NULL;
5800Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN], *str;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	assert(DTRACEACT_ISPRINTFLIKE(kind));
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
5850Sstevel@tonic-gate 		dnerror(dnp, D_PRINTF_ARG_FMT,
5860Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
5870Sstevel@tonic-gate 		    "\tprototype: string constant\n\t argument: %s\n",
5880Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
5890Sstevel@tonic-gate 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	arg1 = dnp->dn_args->dn_list;
5930Sstevel@tonic-gate 	yylineno = dnp->dn_line;
5940Sstevel@tonic-gate 	str = dnp->dn_args->dn_string;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * If this is an freopen(), we use an empty string to denote that
5990Sstevel@tonic-gate 	 * stdout should be restored.  For other printf()-like actions, an
6000Sstevel@tonic-gate 	 * empty format string is illegal:  an empty format string would
6010Sstevel@tonic-gate 	 * result in malformed DOF, and the compiler thus flags an empty
6020Sstevel@tonic-gate 	 * format string as a compile-time error.  To avoid propagating the
6030Sstevel@tonic-gate 	 * freopen() special case throughout the system, we simply transpose
6040Sstevel@tonic-gate 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
6050Sstevel@tonic-gate 	 * denotes that stdout should be restored.
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	if (kind == DTRACEACT_FREOPEN) {
6080Sstevel@tonic-gate 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
6090Sstevel@tonic-gate 			/*
6100Sstevel@tonic-gate 			 * Our sentinel is always an invalid argument to
6110Sstevel@tonic-gate 			 * freopen(), but if it's been manually specified, we
6120Sstevel@tonic-gate 			 * must fail now instead of when the freopen() is
6130Sstevel@tonic-gate 			 * actually evaluated.
6140Sstevel@tonic-gate 			 */
6150Sstevel@tonic-gate 			dnerror(dnp, D_FREOPEN_INVALID,
6160Sstevel@tonic-gate 			    "%s( ) argument #1 cannot be \"%s\"\n",
6170Sstevel@tonic-gate 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		if (str[0] == '\0')
6210Sstevel@tonic-gate 			str = DT_FREOPEN_RESTORE;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
624265Smws 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
6270Sstevel@tonic-gate 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (arg1 == NULL) {
6300Sstevel@tonic-gate 		dif_instr_t *dbuf;
6310Sstevel@tonic-gate 		dtrace_difo_t *dp;
6320Sstevel@tonic-gate 
633265Smws 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
634265Smws 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
635265Smws 			dt_free(dtp, dbuf);
6360Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		dp->dtdo_buf = dbuf;
6420Sstevel@tonic-gate 		dp->dtdo_len = 1;
6430Sstevel@tonic-gate 		dp->dtdo_rtype = dt_int_rtype;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6460Sstevel@tonic-gate 		ap->dtad_difo = dp;
6470Sstevel@tonic-gate 		ap->dtad_kind = kind;
6480Sstevel@tonic-gate 		return;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
6520Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6530Sstevel@tonic-gate 		dt_cg(yypcb, anp);
6540Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
6550Sstevel@tonic-gate 		ap->dtad_kind = kind;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate static void
dt_action_trace(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)6600Sstevel@tonic-gate dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (dt_node_is_void(dnp->dn_args)) {
6650Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_VOID,
6660Sstevel@tonic-gate 		    "trace( ) may not be applied to a void expression\n");
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp->dn_args)) {
6700Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_DYN,
6710Sstevel@tonic-gate 		    "trace( ) may not be applied to a dynamic expression\n");
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
6750Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
6760Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate static void
dt_action_tracemem(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)6800Sstevel@tonic-gate dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	dt_node_t *addr = dnp->dn_args;
6850Sstevel@tonic-gate 	dt_node_t *size = dnp->dn_args->dn_list;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
6900Sstevel@tonic-gate 		dnerror(addr, D_TRACEMEM_ADDR,
6910Sstevel@tonic-gate 		    "tracemem( ) argument #1 is incompatible with "
6920Sstevel@tonic-gate 		    "prototype:\n\tprototype: pointer or integer\n"
6930Sstevel@tonic-gate 		    "\t argument: %s\n",
6940Sstevel@tonic-gate 		    dt_node_type_name(addr, n, sizeof (n)));
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (dt_node_is_posconst(size) == 0) {
6980Sstevel@tonic-gate 		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
6990Sstevel@tonic-gate 		    "be a non-zero positive integral constant expression\n");
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	dt_cg(yypcb, addr);
7030Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
7040Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
7070Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate static void
dt_action_stack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * arg0)7110Sstevel@tonic-gate dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STACK;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7160Sstevel@tonic-gate 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7170Sstevel@tonic-gate 	} else {
7180Sstevel@tonic-gate 		ap->dtad_arg = 0;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (arg0 != NULL) {
7220Sstevel@tonic-gate 		if (arg0->dn_list != NULL) {
7230Sstevel@tonic-gate 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
7240Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		if (dt_node_is_posconst(arg0) == 0) {
7280Sstevel@tonic-gate 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
7290Sstevel@tonic-gate 			    "non-zero positive integral constant expression\n");
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		ap->dtad_arg = arg0->dn_value;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static void
dt_action_stack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)7370Sstevel@tonic-gate dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7400Sstevel@tonic-gate 	dt_action_stack_args(dtp, ap, dnp->dn_args);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate static void
dt_action_ustack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp)7440Sstevel@tonic-gate dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	uint32_t nframes = 0;
7470Sstevel@tonic-gate 	uint32_t strsize = 0;	/* default string table size */
7480Sstevel@tonic-gate 	dt_node_t *arg0 = dnp->dn_args;
7490Sstevel@tonic-gate 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
7520Sstevel@tonic-gate 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
7550Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
7560Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
7590Sstevel@tonic-gate 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_JSTACK;
7620Sstevel@tonic-gate 	} else {
7630Sstevel@tonic-gate 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
7660Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_USTACK;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (arg0 != NULL) {
7720Sstevel@tonic-gate 		if (!dt_node_is_posconst(arg0)) {
7730Sstevel@tonic-gate 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
7740Sstevel@tonic-gate 			    "must be a non-zero positive integer constant\n");
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 		nframes = (uint32_t)arg0->dn_value;
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	if (arg1 != NULL) {
7800Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT ||
7810Sstevel@tonic-gate 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
7820Sstevel@tonic-gate 		    (int64_t)arg1->dn_value < 0)) {
7830Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
7840Sstevel@tonic-gate 			    "must be a positive integer constant\n");
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		if (arg1->dn_list != NULL) {
7880Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
7890Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		strsize = (uint32_t)arg1->dn_value;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate static void
dt_action_ustack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)7990Sstevel@tonic-gate dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8020Sstevel@tonic-gate 	dt_action_ustack_args(dtp, ap, dnp);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
805457Sbmc static void
dt_action_setopt(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)806457Sbmc dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
807457Sbmc {
808457Sbmc 	dtrace_actdesc_t *ap;
809457Sbmc 	dt_node_t *arg0, *arg1;
810457Sbmc 
811457Sbmc 	/*
812457Sbmc 	 * The prototype guarantees that we are called with either one or
813457Sbmc 	 * two arguments, and that any arguments that are present are strings.
814457Sbmc 	 */
815457Sbmc 	arg0 = dnp->dn_args;
816457Sbmc 	arg1 = arg0->dn_list;
817457Sbmc 
818457Sbmc 	ap = dt_stmt_action(dtp, sdp);
819457Sbmc 	dt_cg(yypcb, arg0);
820457Sbmc 	ap->dtad_difo = dt_as(yypcb);
821457Sbmc 	ap->dtad_kind = DTRACEACT_LIBACT;
822457Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
823457Sbmc 
824457Sbmc 	ap = dt_stmt_action(dtp, sdp);
825457Sbmc 
826457Sbmc 	if (arg1 == NULL) {
827457Sbmc 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
828457Sbmc 	} else {
829457Sbmc 		dt_cg(yypcb, arg1);
830457Sbmc 		ap->dtad_difo = dt_as(yypcb);
831457Sbmc 		ap->dtad_kind = DTRACEACT_LIBACT;
832457Sbmc 	}
833457Sbmc 
834457Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
835457Sbmc }
836457Sbmc 
837457Sbmc /*ARGSUSED*/
838457Sbmc static void
dt_action_symmod_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp,dtrace_actkind_t kind)839457Sbmc dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
840457Sbmc     dt_node_t *dnp, dtrace_actkind_t kind)
841457Sbmc {
842457Sbmc 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
843457Sbmc 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
844457Sbmc 	    kind == DTRACEACT_UADDR);
845457Sbmc 
846457Sbmc 	dt_cg(yypcb, dnp);
847457Sbmc 	ap->dtad_difo = dt_as(yypcb);
848457Sbmc 	ap->dtad_kind = kind;
849457Sbmc 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
850457Sbmc }
851457Sbmc 
852457Sbmc static void
dt_action_symmod(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)853457Sbmc dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
854457Sbmc     dtrace_actkind_t kind)
855457Sbmc {
856457Sbmc 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
857457Sbmc 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
858457Sbmc }
859457Sbmc 
8600Sstevel@tonic-gate /*ARGSUSED*/
8610Sstevel@tonic-gate static void
dt_action_ftruncate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8620Sstevel@tonic-gate dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * Library actions need a DIFO that serves as an argument.  As
8680Sstevel@tonic-gate 	 * ftruncate() doesn't take an argument, we generate the constant 0
8690Sstevel@tonic-gate 	 * in a DIFO; this constant will be ignored when the ftruncate() is
8700Sstevel@tonic-gate 	 * processed.
8710Sstevel@tonic-gate 	 */
8720Sstevel@tonic-gate 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
8730Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_FTRUNCATE;
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate /*ARGSUSED*/
8770Sstevel@tonic-gate static void
dt_action_stop(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8780Sstevel@tonic-gate dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STOP;
8830Sstevel@tonic-gate 	ap->dtad_arg = 0;
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate /*ARGSUSED*/
8870Sstevel@tonic-gate static void
dt_action_breakpoint(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8880Sstevel@tonic-gate dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
8930Sstevel@tonic-gate 	ap->dtad_arg = 0;
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /*ARGSUSED*/
8970Sstevel@tonic-gate static void
dt_action_panic(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8980Sstevel@tonic-gate dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_PANIC;
9030Sstevel@tonic-gate 	ap->dtad_arg = 0;
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate static void
dt_action_chill(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9070Sstevel@tonic-gate dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9080Sstevel@tonic-gate {
9090Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9120Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9130Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_CHILL;
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate static void
dt_action_raise(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9170Sstevel@tonic-gate dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9220Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9230Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_RAISE;
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate static void
dt_action_exit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9270Sstevel@tonic-gate dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9320Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9330Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_EXIT;
9340Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate static void
dt_action_speculate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9380Sstevel@tonic-gate dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9430Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9440Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_SPECULATE;
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate static void
dt_action_commit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9480Sstevel@tonic-gate dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9530Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9540Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_COMMIT;
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate static void
dt_action_discard(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9580Sstevel@tonic-gate dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9630Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9640Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DISCARD;
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate static void
dt_compile_fun(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9680Sstevel@tonic-gate dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	switch (dnp->dn_expr->dn_ident->di_id) {
9710Sstevel@tonic-gate 	case DT_ACT_BREAKPOINT:
9720Sstevel@tonic-gate 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
9730Sstevel@tonic-gate 		break;
9740Sstevel@tonic-gate 	case DT_ACT_CHILL:
9750Sstevel@tonic-gate 		dt_action_chill(dtp, dnp->dn_expr, sdp);
9760Sstevel@tonic-gate 		break;
9770Sstevel@tonic-gate 	case DT_ACT_CLEAR:
9780Sstevel@tonic-gate 		dt_action_clear(dtp, dnp->dn_expr, sdp);
9790Sstevel@tonic-gate 		break;
9800Sstevel@tonic-gate 	case DT_ACT_COMMIT:
9810Sstevel@tonic-gate 		dt_action_commit(dtp, dnp->dn_expr, sdp);
9820Sstevel@tonic-gate 		break;
9830Sstevel@tonic-gate 	case DT_ACT_DENORMALIZE:
9840Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
9850Sstevel@tonic-gate 		break;
9860Sstevel@tonic-gate 	case DT_ACT_DISCARD:
9870Sstevel@tonic-gate 		dt_action_discard(dtp, dnp->dn_expr, sdp);
9880Sstevel@tonic-gate 		break;
9890Sstevel@tonic-gate 	case DT_ACT_EXIT:
9900Sstevel@tonic-gate 		dt_action_exit(dtp, dnp->dn_expr, sdp);
9910Sstevel@tonic-gate 		break;
9920Sstevel@tonic-gate 	case DT_ACT_FREOPEN:
9930Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
9940Sstevel@tonic-gate 		break;
9950Sstevel@tonic-gate 	case DT_ACT_FTRUNCATE:
9960Sstevel@tonic-gate 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
9970Sstevel@tonic-gate 		break;
998457Sbmc 	case DT_ACT_MOD:
999457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1000457Sbmc 		break;
10010Sstevel@tonic-gate 	case DT_ACT_NORMALIZE:
10020Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10030Sstevel@tonic-gate 		break;
10040Sstevel@tonic-gate 	case DT_ACT_PANIC:
10050Sstevel@tonic-gate 		dt_action_panic(dtp, dnp->dn_expr, sdp);
10060Sstevel@tonic-gate 		break;
10070Sstevel@tonic-gate 	case DT_ACT_PRINTA:
10080Sstevel@tonic-gate 		dt_action_printa(dtp, dnp->dn_expr, sdp);
10090Sstevel@tonic-gate 		break;
10100Sstevel@tonic-gate 	case DT_ACT_PRINTF:
10110Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
10120Sstevel@tonic-gate 		break;
10130Sstevel@tonic-gate 	case DT_ACT_RAISE:
10140Sstevel@tonic-gate 		dt_action_raise(dtp, dnp->dn_expr, sdp);
10150Sstevel@tonic-gate 		break;
1016457Sbmc 	case DT_ACT_SETOPT:
1017457Sbmc 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1018457Sbmc 		break;
10190Sstevel@tonic-gate 	case DT_ACT_SPECULATE:
10200Sstevel@tonic-gate 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
10210Sstevel@tonic-gate 		break;
10220Sstevel@tonic-gate 	case DT_ACT_STACK:
10230Sstevel@tonic-gate 		dt_action_stack(dtp, dnp->dn_expr, sdp);
10240Sstevel@tonic-gate 		break;
10250Sstevel@tonic-gate 	case DT_ACT_STOP:
10260Sstevel@tonic-gate 		dt_action_stop(dtp, dnp->dn_expr, sdp);
10270Sstevel@tonic-gate 		break;
1028457Sbmc 	case DT_ACT_SYM:
1029457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1030457Sbmc 		break;
10310Sstevel@tonic-gate 	case DT_ACT_SYSTEM:
10320Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
10330Sstevel@tonic-gate 		break;
10340Sstevel@tonic-gate 	case DT_ACT_TRACE:
10350Sstevel@tonic-gate 		dt_action_trace(dtp, dnp->dn_expr, sdp);
10360Sstevel@tonic-gate 		break;
10370Sstevel@tonic-gate 	case DT_ACT_TRACEMEM:
10380Sstevel@tonic-gate 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
10390Sstevel@tonic-gate 		break;
10400Sstevel@tonic-gate 	case DT_ACT_TRUNC:
10410Sstevel@tonic-gate 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
10420Sstevel@tonic-gate 		break;
1043457Sbmc 	case DT_ACT_UADDR:
1044457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1045457Sbmc 		break;
1046457Sbmc 	case DT_ACT_UMOD:
1047457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1048457Sbmc 		break;
1049457Sbmc 	case DT_ACT_USYM:
1050457Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1051457Sbmc 		break;
10520Sstevel@tonic-gate 	case DT_ACT_USTACK:
10530Sstevel@tonic-gate 	case DT_ACT_JSTACK:
10540Sstevel@tonic-gate 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
10550Sstevel@tonic-gate 		break;
10560Sstevel@tonic-gate 	default:
10570Sstevel@tonic-gate 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
10580Sstevel@tonic-gate 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate static void
dt_compile_exp(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10630Sstevel@tonic-gate dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_expr);
10680Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10690Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
10700Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate static void
dt_compile_agg(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10740Sstevel@tonic-gate dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
1077457Sbmc 	dt_node_t *anp, *incr = NULL;
10780Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
1079457Sbmc 	uint_t n = 1, argmax;
1080457Sbmc 	uint64_t arg = 0;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * If the aggregation has no aggregating function applied to it, then
10840Sstevel@tonic-gate 	 * this statement has no effect.  Flag this as a programming error.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	if (dnp->dn_aggfun == NULL) {
10870Sstevel@tonic-gate 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
10880Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	aid = dnp->dn_ident;
10920Sstevel@tonic-gate 	fid = dnp->dn_aggfun->dn_ident;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (dnp->dn_aggfun->dn_args != NULL &&
10950Sstevel@tonic-gate 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
10960Sstevel@tonic-gate 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
10970Sstevel@tonic-gate 		    "be of scalar type\n", fid->di_name);
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/*
11010Sstevel@tonic-gate 	 * The ID of the aggregation itself is implicitly recorded as the first
11020Sstevel@tonic-gate 	 * member of each aggregation tuple so we can distinguish them later.
11030Sstevel@tonic-gate 	 */
11040Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
11050Sstevel@tonic-gate 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
11080Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
11090Sstevel@tonic-gate 		n++;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		if (anp->dn_kind == DT_NODE_FUNC) {
11120Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
11130Sstevel@tonic-gate 				dt_action_stack_args(dtp, ap, anp->dn_args);
11140Sstevel@tonic-gate 				continue;
11150Sstevel@tonic-gate 			}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
11180Sstevel@tonic-gate 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
11190Sstevel@tonic-gate 				dt_action_ustack_args(dtp, ap, anp);
11200Sstevel@tonic-gate 				continue;
11210Sstevel@tonic-gate 			}
1122457Sbmc 
1123457Sbmc 			switch (anp->dn_ident->di_id) {
1124457Sbmc 			case DT_ACT_UADDR:
1125457Sbmc 				dt_action_symmod_args(dtp, ap,
1126457Sbmc 				    anp->dn_args, DTRACEACT_UADDR);
1127457Sbmc 				continue;
1128457Sbmc 
1129457Sbmc 			case DT_ACT_USYM:
1130457Sbmc 				dt_action_symmod_args(dtp, ap,
1131457Sbmc 				    anp->dn_args, DTRACEACT_USYM);
1132457Sbmc 				continue;
1133457Sbmc 
1134457Sbmc 			case DT_ACT_UMOD:
1135457Sbmc 				dt_action_symmod_args(dtp, ap,
1136457Sbmc 				    anp->dn_args, DTRACEACT_UMOD);
1137457Sbmc 				continue;
1138457Sbmc 
1139457Sbmc 			case DT_ACT_SYM:
1140457Sbmc 				dt_action_symmod_args(dtp, ap,
1141457Sbmc 				    anp->dn_args, DTRACEACT_SYM);
1142457Sbmc 				continue;
1143457Sbmc 
1144457Sbmc 			case DT_ACT_MOD:
1145457Sbmc 				dt_action_symmod_args(dtp, ap,
1146457Sbmc 				    anp->dn_args, DTRACEACT_MOD);
1147457Sbmc 				continue;
1148457Sbmc 
1149457Sbmc 			default:
1150457Sbmc 				break;
1151457Sbmc 			}
11520Sstevel@tonic-gate 		}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 		dt_cg(yypcb, anp);
11550Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
11560Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_DIFEXPR;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
11600Sstevel@tonic-gate 		/*
1161457Sbmc 		 * For linear quantization, we have between two and four
1162457Sbmc 		 * arguments in addition to the expression:
11630Sstevel@tonic-gate 		 *
11640Sstevel@tonic-gate 		 *    arg1 => Base value
11650Sstevel@tonic-gate 		 *    arg2 => Limit value
11660Sstevel@tonic-gate 		 *    arg3 => Quantization level step size (defaults to 1)
1167457Sbmc 		 *    arg4 => Quantization increment value (defaults to 1)
11680Sstevel@tonic-gate 		 */
11690Sstevel@tonic-gate 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
11700Sstevel@tonic-gate 		dt_node_t *arg2 = arg1->dn_list;
11710Sstevel@tonic-gate 		dt_node_t *arg3 = arg2->dn_list;
11721017Sbmc 		dt_idsig_t *isp;
11731017Sbmc 		uint64_t nlevels, step = 1, oarg;
11740Sstevel@tonic-gate 		int64_t baseval, limitval;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT) {
11770Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
11780Sstevel@tonic-gate 			    "argument #1 must be an integer constant\n");
11790Sstevel@tonic-gate 		}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 		baseval = (int64_t)arg1->dn_value;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
11840Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
11850Sstevel@tonic-gate 			    "argument #1 must be a 32-bit quantity\n");
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 		if (arg2->dn_kind != DT_NODE_INT) {
11890Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
11900Sstevel@tonic-gate 			    "argument #2 must be an integer constant\n");
11910Sstevel@tonic-gate 		}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		limitval = (int64_t)arg2->dn_value;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
11960Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
11970Sstevel@tonic-gate 			    "argument #2 must be a 32-bit quantity\n");
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		if (limitval < baseval) {
12010Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_MISMATCH,
12020Sstevel@tonic-gate 			    "lquantize( ) base (argument #1) must be less "
12030Sstevel@tonic-gate 			    "than limit (argument #2)\n");
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		if (arg3 != NULL) {
12070Sstevel@tonic-gate 			if (!dt_node_is_posconst(arg3)) {
12080Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
12090Sstevel@tonic-gate 				    "argument #3 must be a non-zero positive "
12100Sstevel@tonic-gate 				    "integer constant\n");
12110Sstevel@tonic-gate 			}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			if ((step = arg3->dn_value) > UINT16_MAX) {
12140Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
12150Sstevel@tonic-gate 				    "argument #3 must be a 16-bit quantity\n");
12160Sstevel@tonic-gate 			}
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		nlevels = (limitval - baseval) / step;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		if (nlevels == 0) {
12220Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPLARGE,
12230Sstevel@tonic-gate 			    "lquantize( ) step (argument #3) too large: must "
12240Sstevel@tonic-gate 			    "have at least one quantization level\n");
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		if (nlevels > UINT16_MAX) {
12280Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
12290Sstevel@tonic-gate 			    "(argument #3) too small: number of quantization "
12300Sstevel@tonic-gate 			    "levels must be a 16-bit quantity\n");
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 
1233457Sbmc 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
12340Sstevel@tonic-gate 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
12350Sstevel@tonic-gate 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
12360Sstevel@tonic-gate 		    DTRACE_LQUANTIZE_BASEMASK);
1237457Sbmc 
12381017Sbmc 		assert(arg != 0);
12391017Sbmc 
12401017Sbmc 		isp = (dt_idsig_t *)aid->di_data;
12411017Sbmc 
12421017Sbmc 		if (isp->dis_auxinfo == 0) {
12431017Sbmc 			/*
12441017Sbmc 			 * This is the first time we've seen an lquantize()
12451017Sbmc 			 * for this aggregation; we'll store our argument
12461017Sbmc 			 * as the auxiliary signature information.
12471017Sbmc 			 */
12481017Sbmc 			isp->dis_auxinfo = arg;
12491017Sbmc 		} else if ((oarg = isp->dis_auxinfo) != arg) {
12501017Sbmc 			/*
12511017Sbmc 			 * If we have seen this lquantize() before and the
12521017Sbmc 			 * argument doesn't match the original argument, pick
12531017Sbmc 			 * the original argument apart to concisely report the
12541017Sbmc 			 * mismatch.
12551017Sbmc 			 */
12561017Sbmc 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
12571017Sbmc 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
12581017Sbmc 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
12591017Sbmc 
12601017Sbmc 			if (obaseval != baseval) {
12611017Sbmc 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
12621017Sbmc 				    "base (argument #1) doesn't match previous "
12631017Sbmc 				    "declaration: expected %d, found %d\n",
12641017Sbmc 				    obaseval, (int)baseval);
12651017Sbmc 			}
12661017Sbmc 
12671017Sbmc 			if (onlevels * ostep != nlevels * step) {
12681017Sbmc 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
12691017Sbmc 				    "limit (argument #2) doesn't match previous"
12701017Sbmc 				    " declaration: expected %d, found %d\n",
12711017Sbmc 				    obaseval + onlevels * ostep,
12721017Sbmc 				    (int)baseval + (int)nlevels * (int)step);
12731017Sbmc 			}
12741017Sbmc 
12751017Sbmc 			if (ostep != step) {
12761017Sbmc 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
12771017Sbmc 				    "step (argument #3) doesn't match previous "
12781017Sbmc 				    "declaration: expected %d, found %d\n",
12791017Sbmc 				    ostep, (int)step);
12801017Sbmc 			}
12811017Sbmc 
12821017Sbmc 			/*
12831017Sbmc 			 * We shouldn't be able to get here -- one of the
12841017Sbmc 			 * parameters must be mismatched if the arguments
12851017Sbmc 			 * didn't match.
12861017Sbmc 			 */
12871017Sbmc 			assert(0);
12881017Sbmc 		}
12891017Sbmc 
1290457Sbmc 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1291457Sbmc 		argmax = 5;
1292457Sbmc 	}
1293457Sbmc 
1294457Sbmc 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1295457Sbmc 		incr = dnp->dn_aggfun->dn_args->dn_list;
1296457Sbmc 		argmax = 2;
1297457Sbmc 	}
1298457Sbmc 
1299457Sbmc 	if (incr != NULL) {
1300457Sbmc 		if (!dt_node_is_scalar(incr)) {
1301457Sbmc 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1302457Sbmc 			    "(argument #%d) must be of scalar type\n",
1303457Sbmc 			    fid->di_name, argmax);
1304457Sbmc 		}
1305457Sbmc 
1306457Sbmc 		if ((anp = incr->dn_list) != NULL) {
1307457Sbmc 			int argc = argmax;
1308457Sbmc 
1309457Sbmc 			for (; anp != NULL; anp = anp->dn_list)
1310457Sbmc 				argc++;
1311457Sbmc 
1312457Sbmc 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1313457Sbmc 			    "mismatch: %d args passed, at most %d expected",
1314457Sbmc 			    fid->di_name, argc, argmax);
1315457Sbmc 		}
1316457Sbmc 
1317457Sbmc 		ap = dt_stmt_action(dtp, sdp);
1318457Sbmc 		n++;
1319457Sbmc 
1320457Sbmc 		dt_cg(yypcb, incr);
1321457Sbmc 		ap->dtad_difo = dt_as(yypcb);
1322457Sbmc 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1323457Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1324457Sbmc 	}
1325457Sbmc 
1326457Sbmc 	assert(sdp->dtsd_aggdata == NULL);
1327457Sbmc 	sdp->dtsd_aggdata = aid;
1328457Sbmc 
1329457Sbmc 	ap = dt_stmt_action(dtp, sdp);
1330457Sbmc 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1331457Sbmc 	assert(DTRACEACT_ISAGG(fid->di_id));
1332457Sbmc 	ap->dtad_kind = fid->di_id;
1333457Sbmc 	ap->dtad_ntuple = n;
1334457Sbmc 	ap->dtad_arg = arg;
1335457Sbmc 
1336457Sbmc 	if (dnp->dn_aggfun->dn_args != NULL) {
1337457Sbmc 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1338457Sbmc 		ap->dtad_difo = dt_as(yypcb);
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate static void
dt_compile_one_clause(dtrace_hdl_t * dtp,dt_node_t * cnp,dt_node_t * pnp)13430Sstevel@tonic-gate dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
13460Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
13470Sstevel@tonic-gate 	dt_node_t *dnp;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	yylineno = pnp->dn_line;
13500Sstevel@tonic-gate 	dt_setcontext(dtp, pnp->dn_desc);
13510Sstevel@tonic-gate 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	if (DT_TREEDUMP_PASS(dtp, 2))
13540Sstevel@tonic-gate 		dt_node_printr(cnp, stderr, 0);
13550Sstevel@tonic-gate 
1356265Smws 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
13570Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == NULL);
13600Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = edp;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	if (cnp->dn_pred != NULL) {
13630Sstevel@tonic-gate 		dt_cg(yypcb, cnp->dn_pred);
13640Sstevel@tonic-gate 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	if (cnp->dn_acts == NULL) {
13680Sstevel@tonic-gate 		dt_stmt_append(dt_stmt_create(dtp, edp,
13690Sstevel@tonic-gate 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
13730Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == NULL);
13740Sstevel@tonic-gate 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 		switch (dnp->dn_kind) {
13770Sstevel@tonic-gate 		case DT_NODE_DEXPR:
13780Sstevel@tonic-gate 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
13790Sstevel@tonic-gate 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
13800Sstevel@tonic-gate 			else
13810Sstevel@tonic-gate 				dt_compile_exp(dtp, dnp, sdp);
13820Sstevel@tonic-gate 			break;
13830Sstevel@tonic-gate 		case DT_NODE_DFUNC:
13840Sstevel@tonic-gate 			dt_compile_fun(dtp, dnp, sdp);
13850Sstevel@tonic-gate 			break;
13860Sstevel@tonic-gate 		case DT_NODE_AGG:
13870Sstevel@tonic-gate 			dt_compile_agg(dtp, dnp, sdp);
13880Sstevel@tonic-gate 			break;
13890Sstevel@tonic-gate 		default:
13900Sstevel@tonic-gate 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
13910Sstevel@tonic-gate 			    "%u is not a valid statement\n", dnp->dn_kind);
13920Sstevel@tonic-gate 		}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == sdp);
13950Sstevel@tonic-gate 		dt_stmt_append(sdp, dnp);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == edp);
1399265Smws 	dt_ecbdesc_release(dtp, edp);
14000Sstevel@tonic-gate 	dt_endcontext(dtp);
14010Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = NULL;
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate static void
dt_compile_clause(dtrace_hdl_t * dtp,dt_node_t * cnp)14050Sstevel@tonic-gate dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
14060Sstevel@tonic-gate {
14070Sstevel@tonic-gate 	dt_node_t *pnp;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
14100Sstevel@tonic-gate 		dt_compile_one_clause(dtp, cnp, pnp);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
1413265Smws static void
dt_compile_xlator(dt_node_t * dnp)1414265Smws dt_compile_xlator(dt_node_t *dnp)
1415265Smws {
1416265Smws 	dt_xlator_t *dxp = dnp->dn_xlator;
1417265Smws 	dt_node_t *mnp;
1418265Smws 
1419265Smws 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1420265Smws 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1421265Smws 		dt_cg(yypcb, mnp);
1422265Smws 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1423265Smws 	}
1424265Smws }
1425265Smws 
14260Sstevel@tonic-gate void
dt_setcontext(dtrace_hdl_t * dtp,dtrace_probedesc_t * pdp)14270Sstevel@tonic-gate dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	const dtrace_pattr_t *pap;
14300Sstevel@tonic-gate 	dt_probe_t *prp;
14315478Sjhaslam 	dt_provider_t *pvp;
14320Sstevel@tonic-gate 	dt_ident_t *idp;
14330Sstevel@tonic-gate 	char attrstr[8];
14340Sstevel@tonic-gate 	int err;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/*
14375478Sjhaslam 	 * Both kernel and pid based providers are allowed to have names
14385478Sjhaslam 	 * ending with what could be interpreted as a number. We assume it's
14395478Sjhaslam 	 * a pid and that we may need to dynamically create probes for
14405478Sjhaslam 	 * that process if:
14415478Sjhaslam 	 *
14425478Sjhaslam 	 * (1) The provider doesn't exist, or,
14435478Sjhaslam 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
14445478Sjhaslam 	 *
14455478Sjhaslam 	 * On an error, dt_pid_create_probes() will set the error message
14465478Sjhaslam 	 * and tag -- we just have to longjmp() out of here.
14470Sstevel@tonic-gate 	 */
14481399Sahl 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
14495478Sjhaslam 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
14505478Sjhaslam 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
14511399Sahl 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
14521399Sahl 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
14531399Sahl 	}
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
14570Sstevel@tonic-gate 	 * a representative probe is found, set 'pap' to the probe provider's
14580Sstevel@tonic-gate 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
14590Sstevel@tonic-gate 	 */
14600Sstevel@tonic-gate 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
14610Sstevel@tonic-gate 		pap = &_dtrace_prvdesc;
14620Sstevel@tonic-gate 		err = dtrace_errno(dtp);
14630Sstevel@tonic-gate 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
14640Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
14650Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
14660Sstevel@tonic-gate 	} else {
14670Sstevel@tonic-gate 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
14680Sstevel@tonic-gate 		err = 0;
14690Sstevel@tonic-gate 	}
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
14720Sstevel@tonic-gate 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
14730Sstevel@tonic-gate 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
14740Sstevel@tonic-gate 		    pdp->dtpd_func, pdp->dtpd_name);
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
14780Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
14810Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
14820Sstevel@tonic-gate 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
14830Sstevel@tonic-gate 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	/*
14860Sstevel@tonic-gate 	 * Reset the stability attributes of D global variables that vary
14870Sstevel@tonic-gate 	 * based on the attributes of the provider and context itself.
14880Sstevel@tonic-gate 	 */
14890Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
14900Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_provider;
14910Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
14920Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_mod;
14930Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
14940Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_func;
14950Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
14960Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_name;
14970Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
14980Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_args;
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	yypcb->pcb_pdesc = pdp;
15010Sstevel@tonic-gate 	yypcb->pcb_probe = prp;
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate /*
15050Sstevel@tonic-gate  * Reset context-dependent variables and state at the end of cooking a D probe
15060Sstevel@tonic-gate  * definition clause.  This ensures that external declarations between clauses
15070Sstevel@tonic-gate  * do not reference any stale context-dependent data from the previous clause.
15080Sstevel@tonic-gate  */
15090Sstevel@tonic-gate void
dt_endcontext(dtrace_hdl_t * dtp)15100Sstevel@tonic-gate dt_endcontext(dtrace_hdl_t *dtp)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate 	static const char *const cvars[] = {
15130Sstevel@tonic-gate 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
15140Sstevel@tonic-gate 	};
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	dt_ident_t *idp;
15170Sstevel@tonic-gate 	int i;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	for (i = 0; cvars[i] != NULL; i++) {
15200Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
15210Sstevel@tonic-gate 			idp->di_attr = _dtrace_defattr;
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	yypcb->pcb_pdesc = NULL;
15250Sstevel@tonic-gate 	yypcb->pcb_probe = NULL;
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate static int
dt_reduceid(dt_idhash_t * dhp,dt_ident_t * idp,dtrace_hdl_t * dtp)15290Sstevel@tonic-gate dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
15300Sstevel@tonic-gate {
15310Sstevel@tonic-gate 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
15320Sstevel@tonic-gate 		dt_idhash_delete(dhp, idp);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	return (0);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate /*
15380Sstevel@tonic-gate  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
15390Sstevel@tonic-gate  * any identifiers or translators that have been previously defined as bound to
15400Sstevel@tonic-gate  * a version greater than the specified version.  Therefore, in our current
15410Sstevel@tonic-gate  * version implementation, establishing a binding is a one-way transformation.
15420Sstevel@tonic-gate  * In addition, no versioning is currently provided for types as our .d library
15430Sstevel@tonic-gate  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
15440Sstevel@tonic-gate  * for our exclusive use.  If required, type versioning will require more work.
15450Sstevel@tonic-gate  */
15460Sstevel@tonic-gate int
dt_reduce(dtrace_hdl_t * dtp,dt_version_t v)15470Sstevel@tonic-gate dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	char s[DT_VERSION_STRMAX];
15500Sstevel@tonic-gate 	dt_xlator_t *dxp, *nxp;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	if (v > dtp->dt_vmax)
15530Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
15540Sstevel@tonic-gate 	else if (v == dtp->dt_vmax)
15550Sstevel@tonic-gate 		return (0); /* no reduction necessary */
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	dt_dprintf("reducing api version to %s\n",
15580Sstevel@tonic-gate 	    dt_version_num2str(v, s, sizeof (s)));
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	dtp->dt_vmax = v;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
15630Sstevel@tonic-gate 		nxp = dt_list_next(dxp);
15640Sstevel@tonic-gate 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
15650Sstevel@tonic-gate 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
15660Sstevel@tonic-gate 			dt_list_delete(&dtp->dt_xlators, dxp);
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
15700Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
15710Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
15720Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	return (0);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate /*
15780Sstevel@tonic-gate  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
15790Sstevel@tonic-gate  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
15800Sstevel@tonic-gate  * here to simplify the code by leveraging file descriptor inheritance.
15810Sstevel@tonic-gate  */
15820Sstevel@tonic-gate static FILE *
dt_preproc(dtrace_hdl_t * dtp,FILE * ifp)15830Sstevel@tonic-gate dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
15840Sstevel@tonic-gate {
15850Sstevel@tonic-gate 	int argc = dtp->dt_cpp_argc;
15860Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (argc + 5));
15870Sstevel@tonic-gate 	FILE *ofp = tmpfile();
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
15900Sstevel@tonic-gate 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	struct sigaction act, oact;
15930Sstevel@tonic-gate 	sigset_t mask, omask;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	int wstat, estat;
15960Sstevel@tonic-gate 	pid_t pid;
15970Sstevel@tonic-gate 	off64_t off;
15980Sstevel@tonic-gate 	int c;
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	if (argv == NULL || ofp == NULL) {
16010Sstevel@tonic-gate 		(void) dt_set_errno(dtp, errno);
16020Sstevel@tonic-gate 		goto err;
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * If the input is a seekable file, see if it is an interpreter file.
16070Sstevel@tonic-gate 	 * If we see #!, seek past the first line because cpp will choke on it.
16080Sstevel@tonic-gate 	 * We start cpp just prior to the \n at the end of this line so that
16090Sstevel@tonic-gate 	 * it still sees the newline, ensuring that #line values are correct.
16100Sstevel@tonic-gate 	 */
16110Sstevel@tonic-gate 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
16120Sstevel@tonic-gate 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
16130Sstevel@tonic-gate 			for (off += 2; c != '\n'; off++) {
16140Sstevel@tonic-gate 				if ((c = fgetc(ifp)) == EOF)
16150Sstevel@tonic-gate 					break;
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 			if (c == '\n')
16180Sstevel@tonic-gate 				off--; /* start cpp just prior to \n */
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 		(void) fflush(ifp);
16210Sstevel@tonic-gate 		(void) fseeko64(ifp, off, SEEK_SET);
16220Sstevel@tonic-gate 	}
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
16250Sstevel@tonic-gate 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	(void) snprintf(verdef, sizeof (verdef),
16300Sstevel@tonic-gate 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
16310Sstevel@tonic-gate 	argv[argc++] = verdef;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	switch (dtp->dt_stdcmode) {
16340Sstevel@tonic-gate 	case DT_STDC_XA:
16350Sstevel@tonic-gate 	case DT_STDC_XT:
16360Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=0";
16370Sstevel@tonic-gate 		break;
16380Sstevel@tonic-gate 	case DT_STDC_XC:
16390Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=1";
16400Sstevel@tonic-gate 		break;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	argv[argc++] = ipath;
16440Sstevel@tonic-gate 	argv[argc++] = opath;
16450Sstevel@tonic-gate 	argv[argc] = NULL;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * libdtrace must be able to be embedded in other programs that may
16490Sstevel@tonic-gate 	 * include application-specific signal handlers.  Therefore, if we
16500Sstevel@tonic-gate 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
16510Sstevel@tonic-gate 	 * that could confuse the containing application.  To do this,
16520Sstevel@tonic-gate 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
16530Sstevel@tonic-gate 	 * We restore our signal state once we are done.
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	(void) sigemptyset(&mask);
16560Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
16570Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	bzero(&act, sizeof (act));
16600Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
16610Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, &oact);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
16640Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &oact, NULL);
16650Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16660Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CPPFORK);
16670Sstevel@tonic-gate 		goto err;
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	if (pid == 0) {
16710Sstevel@tonic-gate 		(void) execvp(dtp->dt_cpp_path, argv);
16720Sstevel@tonic-gate 		_exit(errno == ENOENT ? 127 : 126);
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	do {
16760Sstevel@tonic-gate 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
16770Sstevel@tonic-gate 		    (int)pid);
16780Sstevel@tonic-gate 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &oact, NULL);
16810Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
16840Sstevel@tonic-gate 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (estat != 0) {
16870Sstevel@tonic-gate 		switch (estat) {
16880Sstevel@tonic-gate 		case 126:
16890Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
16900Sstevel@tonic-gate 			break;
16910Sstevel@tonic-gate 		case 127:
16920Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPENT);
16930Sstevel@tonic-gate 			break;
16940Sstevel@tonic-gate 		default:
16950Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPERR);
16960Sstevel@tonic-gate 		}
16970Sstevel@tonic-gate 		goto err;
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	free(argv);
17010Sstevel@tonic-gate 	(void) fflush(ofp);
17020Sstevel@tonic-gate 	(void) fseek(ofp, 0, SEEK_SET);
17030Sstevel@tonic-gate 	return (ofp);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate err:
17060Sstevel@tonic-gate 	free(argv);
17070Sstevel@tonic-gate 	(void) fclose(ofp);
17080Sstevel@tonic-gate 	return (NULL);
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate 
17115478Sjhaslam static void
dt_lib_depend_error(dtrace_hdl_t * dtp,const char * format,...)17125478Sjhaslam dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
17135478Sjhaslam {
17145478Sjhaslam 	va_list ap;
17155478Sjhaslam 
17165478Sjhaslam 	va_start(ap, format);
17175478Sjhaslam 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
17185478Sjhaslam 	va_end(ap);
17195478Sjhaslam }
17205478Sjhaslam 
17215478Sjhaslam int
dt_lib_depend_add(dtrace_hdl_t * dtp,dt_list_t * dlp,const char * arg)17225478Sjhaslam dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
17235478Sjhaslam {
17245478Sjhaslam 	dt_lib_depend_t *dld;
17255478Sjhaslam 	const char *end;
17265478Sjhaslam 
17275478Sjhaslam 	assert(arg != NULL);
17285478Sjhaslam 
17295478Sjhaslam 	if ((end = strrchr(arg, '/')) == NULL)
17305478Sjhaslam 		return (dt_set_errno(dtp, EINVAL));
17315478Sjhaslam 
17325478Sjhaslam 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
17335478Sjhaslam 		return (-1);
17345478Sjhaslam 
17355478Sjhaslam 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
17365478Sjhaslam 		dt_free(dtp, dld);
17375478Sjhaslam 		return (-1);
17385478Sjhaslam 	}
17395478Sjhaslam 
17405478Sjhaslam 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
17415478Sjhaslam 	if ((dld->dtld_library = strdup(arg)) == NULL) {
17425478Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
17435478Sjhaslam 		dt_free(dtp, dld);
17445478Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
17455478Sjhaslam 	}
17465478Sjhaslam 
17475478Sjhaslam 	dt_list_append(dlp, dld);
17485478Sjhaslam 	return (0);
17495478Sjhaslam }
17505478Sjhaslam 
17515478Sjhaslam dt_lib_depend_t *
dt_lib_depend_lookup(dt_list_t * dld,const char * arg)17525478Sjhaslam dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
17535478Sjhaslam {
17545478Sjhaslam 	dt_lib_depend_t *dldn;
17555478Sjhaslam 
17565478Sjhaslam 	for (dldn = dt_list_next(dld); dldn != NULL;
17575478Sjhaslam 	    dldn = dt_list_next(dldn)) {
17585478Sjhaslam 		if (strcmp(dldn->dtld_library, arg) == 0)
17595478Sjhaslam 			return (dldn);
17605478Sjhaslam 	}
17615478Sjhaslam 
17625478Sjhaslam 	return (NULL);
17635478Sjhaslam }
17645478Sjhaslam 
17650Sstevel@tonic-gate /*
17665478Sjhaslam  * Go through all the library files, and, if any library dependencies exist for
17675478Sjhaslam  * that file, add it to that node's list of dependents. The result of this
17685478Sjhaslam  * will be a graph which can then be topologically sorted to produce a
17695478Sjhaslam  * compilation order.
17705478Sjhaslam  */
17715478Sjhaslam static int
dt_lib_build_graph(dtrace_hdl_t * dtp)17725478Sjhaslam dt_lib_build_graph(dtrace_hdl_t *dtp)
17735478Sjhaslam {
17745478Sjhaslam 	dt_lib_depend_t *dld, *dpld;
17755478Sjhaslam 
17765478Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
17775478Sjhaslam 	    dld = dt_list_next(dld)) {
17785478Sjhaslam 		char *library = dld->dtld_library;
17795478Sjhaslam 
17805478Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
17815478Sjhaslam 		    dpld = dt_list_next(dpld)) {
17825478Sjhaslam 			dt_lib_depend_t *dlda;
17835478Sjhaslam 
17845478Sjhaslam 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
17855478Sjhaslam 			    dpld->dtld_library)) == NULL) {
17865478Sjhaslam 				dt_lib_depend_error(dtp,
17875478Sjhaslam 				    "Invalid library dependency in %s: %s\n",
17885478Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
17895478Sjhaslam 
17905478Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
17915478Sjhaslam 			}
17925478Sjhaslam 
17935478Sjhaslam 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
17945478Sjhaslam 			    library)) != 0) {
17955478Sjhaslam 				return (-1); /* preserve dt_errno */
17965478Sjhaslam 			}
17975478Sjhaslam 		}
17985478Sjhaslam 	}
17995478Sjhaslam 	return (0);
18005478Sjhaslam }
18015478Sjhaslam 
18025478Sjhaslam static int
dt_topo_sort(dtrace_hdl_t * dtp,dt_lib_depend_t * dld,int * count)18035478Sjhaslam dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
18045478Sjhaslam {
18055478Sjhaslam 	dt_lib_depend_t *dpld, *dlda, *new;
18065478Sjhaslam 
18075478Sjhaslam 	dld->dtld_start = ++(*count);
18085478Sjhaslam 
18095478Sjhaslam 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
18105478Sjhaslam 	    dpld = dt_list_next(dpld)) {
18115478Sjhaslam 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
18125478Sjhaslam 		    dpld->dtld_library);
18135478Sjhaslam 		assert(dlda != NULL);
18145478Sjhaslam 
18155478Sjhaslam 		if (dlda->dtld_start == 0 &&
18165478Sjhaslam 		    dt_topo_sort(dtp, dlda, count) == -1)
18175478Sjhaslam 			return (-1);
18185478Sjhaslam 	}
18195478Sjhaslam 
18205478Sjhaslam 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
18215478Sjhaslam 		return (-1);
18225478Sjhaslam 
18235478Sjhaslam 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
18245478Sjhaslam 		dt_free(dtp, new);
18255478Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
18265478Sjhaslam 	}
18275478Sjhaslam 
18285478Sjhaslam 	new->dtld_start = dld->dtld_start;
18295478Sjhaslam 	new->dtld_finish = dld->dtld_finish = ++(*count);
18305478Sjhaslam 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
18315478Sjhaslam 
18325478Sjhaslam 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
18335478Sjhaslam 	    new->dtld_start, new->dtld_finish);
18345478Sjhaslam 
18355478Sjhaslam 	return (0);
18365478Sjhaslam }
18375478Sjhaslam 
18385478Sjhaslam static int
dt_lib_depend_sort(dtrace_hdl_t * dtp)18395478Sjhaslam dt_lib_depend_sort(dtrace_hdl_t *dtp)
18405478Sjhaslam {
18415478Sjhaslam 	dt_lib_depend_t *dld, *dpld, *dlda;
18425478Sjhaslam 	int count = 0;
18435478Sjhaslam 
18445478Sjhaslam 	if (dt_lib_build_graph(dtp) == -1)
18455478Sjhaslam 		return (-1); /* preserve dt_errno */
18465478Sjhaslam 
18475478Sjhaslam 	/*
18485478Sjhaslam 	 * Perform a topological sort of the graph that hangs off
18495478Sjhaslam 	 * dtp->dt_lib_dep. The result of this process will be a
18505478Sjhaslam 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
18515478Sjhaslam 	 */
18525478Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
18535478Sjhaslam 	    dld = dt_list_next(dld)) {
18545478Sjhaslam 		if (dld->dtld_start == 0 &&
18555478Sjhaslam 		    dt_topo_sort(dtp, dld, &count) == -1)
18565478Sjhaslam 			return (-1); /* preserve dt_errno */;
18575478Sjhaslam 	}
18585478Sjhaslam 
18595478Sjhaslam 	/*
18605478Sjhaslam 	 * Check the graph for cycles. If an ancestor's finishing time is
18615478Sjhaslam 	 * less than any of its dependent's finishing times then a back edge
18625478Sjhaslam 	 * exists in the graph and this is a cycle.
18635478Sjhaslam 	 */
18645478Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
18655478Sjhaslam 	    dld = dt_list_next(dld)) {
18665478Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
18675478Sjhaslam 		    dpld = dt_list_next(dpld)) {
18685478Sjhaslam 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
18695478Sjhaslam 			    dpld->dtld_library);
18705478Sjhaslam 			assert(dlda != NULL);
18715478Sjhaslam 
18725478Sjhaslam 			if (dlda->dtld_finish > dld->dtld_finish) {
18735478Sjhaslam 				dt_lib_depend_error(dtp,
18745478Sjhaslam 				    "Cyclic dependency detected: %s => %s\n",
18755478Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
18765478Sjhaslam 
18775478Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
18785478Sjhaslam 			}
18795478Sjhaslam 		}
18805478Sjhaslam 	}
18815478Sjhaslam 
18825478Sjhaslam 	return (0);
18835478Sjhaslam }
18845478Sjhaslam 
18855478Sjhaslam static void
dt_lib_depend_free(dtrace_hdl_t * dtp)18865478Sjhaslam dt_lib_depend_free(dtrace_hdl_t *dtp)
18875478Sjhaslam {
18885478Sjhaslam 	dt_lib_depend_t *dld, *dlda;
18895478Sjhaslam 
18905478Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
18915478Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
18925478Sjhaslam 			dt_list_delete(&dld->dtld_dependencies, dlda);
18935478Sjhaslam 			dt_free(dtp, dlda->dtld_library);
18945478Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
18955478Sjhaslam 			dt_free(dtp, dlda);
18965478Sjhaslam 		}
18975478Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
18985478Sjhaslam 			dt_list_delete(&dld->dtld_dependents, dlda);
18995478Sjhaslam 			dt_free(dtp, dlda->dtld_library);
19005478Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
19015478Sjhaslam 			dt_free(dtp, dlda);
19025478Sjhaslam 		}
19035478Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep, dld);
19045478Sjhaslam 		dt_free(dtp, dld->dtld_library);
19055478Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
19065478Sjhaslam 		dt_free(dtp, dld);
19075478Sjhaslam 	}
19085478Sjhaslam 
19095478Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
19105478Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
19115478Sjhaslam 		dt_free(dtp, dld->dtld_library);
19125478Sjhaslam 		dt_free(dtp, dld);
19135478Sjhaslam 	}
19145478Sjhaslam }
19155478Sjhaslam 
19165478Sjhaslam 
19175478Sjhaslam /*
19185478Sjhaslam  * Open all of the .d library files found in the specified directory and
19195478Sjhaslam  * compile each one in topological order to cache its inlines and translators,
19205478Sjhaslam  * etc.  We silently ignore any missing directories and other files found
19215478Sjhaslam  * therein. We only fail (and thereby fail dt_load_libs()) if we fail to
19225478Sjhaslam  * compile a library and the error is something other than #pragma D depends_on.
19230Sstevel@tonic-gate  * Dependency errors are silently ignored to permit a library directory to
19240Sstevel@tonic-gate  * contain libraries which may not be accessible depending on our privileges.
19250Sstevel@tonic-gate  */
19260Sstevel@tonic-gate static int
dt_load_libs_dir(dtrace_hdl_t * dtp,const char * path)19270Sstevel@tonic-gate dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
19280Sstevel@tonic-gate {
1929871Scasper 	struct dirent *dp;
19300Sstevel@tonic-gate 	const char *p;
19310Sstevel@tonic-gate 	DIR *dirp;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	char fname[PATH_MAX];
19340Sstevel@tonic-gate 	dtrace_prog_t *pgp;
19350Sstevel@tonic-gate 	FILE *fp;
19365478Sjhaslam 	void *rv;
19375478Sjhaslam 	dt_lib_depend_t *dld;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL) {
19400Sstevel@tonic-gate 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
19410Sstevel@tonic-gate 		return (0);
19420Sstevel@tonic-gate 	}
19430Sstevel@tonic-gate 
19445478Sjhaslam 	/* First, parse each file for library dependencies. */
1945871Scasper 	while ((dp = readdir(dirp)) != NULL) {
19460Sstevel@tonic-gate 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
19470Sstevel@tonic-gate 			continue; /* skip any filename not ending in .d */
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 		(void) snprintf(fname, sizeof (fname),
19500Sstevel@tonic-gate 		    "%s/%s", path, dp->d_name);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 		if ((fp = fopen(fname, "r")) == NULL) {
19530Sstevel@tonic-gate 			dt_dprintf("skipping library %s: %s\n",
19540Sstevel@tonic-gate 			    fname, strerror(errno));
19550Sstevel@tonic-gate 			continue;
19560Sstevel@tonic-gate 		}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		dtp->dt_filetag = fname;
19595478Sjhaslam 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
19605478Sjhaslam 			goto err;
19615478Sjhaslam 
19625478Sjhaslam 		rv = dt_compile(dtp, DT_CTX_DPROG,
19635478Sjhaslam 		    DTRACE_PROBESPEC_NAME, NULL,
19645478Sjhaslam 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
19655478Sjhaslam 
19665478Sjhaslam 		if (rv != NULL && dtp->dt_errno &&
19675478Sjhaslam 		    (dtp->dt_errno != EDT_COMPILER ||
19685478Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
19695478Sjhaslam 			goto err;
19705478Sjhaslam 
19715478Sjhaslam 		if (dtp->dt_errno)
19725478Sjhaslam 			dt_dprintf("error parsing library %s: %s\n",
19735478Sjhaslam 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
19745478Sjhaslam 
19755478Sjhaslam 		(void) fclose(fp);
19765478Sjhaslam 		dtp->dt_filetag = NULL;
19775478Sjhaslam 	}
19785478Sjhaslam 
19795478Sjhaslam 	(void) closedir(dirp);
19805478Sjhaslam 	/*
19815478Sjhaslam 	 * Finish building the graph containing the library dependencies
19825478Sjhaslam 	 * and perform a topological sort to generate an ordered list
19835478Sjhaslam 	 * for compilation.
19845478Sjhaslam 	 */
19855478Sjhaslam 	if (dt_lib_depend_sort(dtp) == -1)
19865478Sjhaslam 		goto err;
19875478Sjhaslam 
19885478Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
19895478Sjhaslam 	    dld = dt_list_next(dld)) {
19905478Sjhaslam 
19915478Sjhaslam 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
19925478Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
19935478Sjhaslam 			    dld->dtld_library, strerror(errno));
19945478Sjhaslam 			continue;
19955478Sjhaslam 		}
19965478Sjhaslam 
19975478Sjhaslam 		dtp->dt_filetag = dld->dtld_library;
19980Sstevel@tonic-gate 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
19990Sstevel@tonic-gate 		(void) fclose(fp);
20000Sstevel@tonic-gate 		dtp->dt_filetag = NULL;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
20035478Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
20045478Sjhaslam 			goto err;
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		if (pgp == NULL) {
20075478Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
20085478Sjhaslam 			    dld->dtld_library,
20090Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
20106390Sahl 		} else {
20116390Sahl 			dld->dtld_loaded = B_TRUE;
2012265Smws 			dt_program_destroy(dtp, pgp);
20136390Sahl 		}
20140Sstevel@tonic-gate 	}
20150Sstevel@tonic-gate 
20165478Sjhaslam 	dt_lib_depend_free(dtp);
20170Sstevel@tonic-gate 	return (0);
20185478Sjhaslam 
20195478Sjhaslam err:
20205478Sjhaslam 	dt_lib_depend_free(dtp);
20215478Sjhaslam 	return (-1); /* preserve dt_errno */
20220Sstevel@tonic-gate }
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate /*
20250Sstevel@tonic-gate  * Load the contents of any appropriate DTrace .d library files.  These files
20260Sstevel@tonic-gate  * contain inlines and translators that will be cached by the compiler.  We
20270Sstevel@tonic-gate  * defer this activity until the first compile to permit libdtrace clients to
20280Sstevel@tonic-gate  * add their own library directories and so that we can properly report errors.
20290Sstevel@tonic-gate  */
20300Sstevel@tonic-gate static int
dt_load_libs(dtrace_hdl_t * dtp)20310Sstevel@tonic-gate dt_load_libs(dtrace_hdl_t *dtp)
20320Sstevel@tonic-gate {
20330Sstevel@tonic-gate 	dt_dirpath_t *dirp;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
20360Sstevel@tonic-gate 		return (0); /* libraries already processed */
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	for (dirp = dt_list_next(&dtp->dt_lib_path);
20410Sstevel@tonic-gate 	    dirp != NULL; dirp = dt_list_next(dirp)) {
20420Sstevel@tonic-gate 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
20430Sstevel@tonic-gate 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
20440Sstevel@tonic-gate 			return (-1); /* errno is set for us */
20450Sstevel@tonic-gate 		}
20460Sstevel@tonic-gate 	}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	return (0);
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate static void *
dt_compile(dtrace_hdl_t * dtp,int context,dtrace_probespec_t pspec,void * arg,uint_t cflags,int argc,char * const argv[],FILE * fp,const char * s)20520Sstevel@tonic-gate dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
20530Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
20540Sstevel@tonic-gate {
20550Sstevel@tonic-gate 	dt_node_t *dnp;
20560Sstevel@tonic-gate 	dt_decl_t *ddp;
20570Sstevel@tonic-gate 	dt_pcb_t pcb;
20580Sstevel@tonic-gate 	void *rv;
20590Sstevel@tonic-gate 	int err;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
20620Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
20630Sstevel@tonic-gate 		return (NULL);
20640Sstevel@tonic-gate 	}
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
20670Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
20680Sstevel@tonic-gate 
2069*12902SBryan.Cantrill@Sun.COM 	if (dtp->dt_globals->dh_nelems != 0)
2070*12902SBryan.Cantrill@Sun.COM 		(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
20710Sstevel@tonic-gate 
2072*12902SBryan.Cantrill@Sun.COM 	if (dtp->dt_tls->dh_nelems != 0)
2073*12902SBryan.Cantrill@Sun.COM 		(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
20760Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	dt_pcb_push(dtp, &pcb);
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	pcb.pcb_fileptr = fp;
20810Sstevel@tonic-gate 	pcb.pcb_string = s;
20820Sstevel@tonic-gate 	pcb.pcb_strptr = s;
20830Sstevel@tonic-gate 	pcb.pcb_strlen = s ? strlen(s) : 0;
20840Sstevel@tonic-gate 	pcb.pcb_sargc = argc;
20850Sstevel@tonic-gate 	pcb.pcb_sargv = argv;
20860Sstevel@tonic-gate 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
20870Sstevel@tonic-gate 	pcb.pcb_pspec = pspec;
20880Sstevel@tonic-gate 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
20890Sstevel@tonic-gate 	pcb.pcb_amin = dtp->dt_amin;
20900Sstevel@tonic-gate 	pcb.pcb_yystate = -1;
20910Sstevel@tonic-gate 	pcb.pcb_context = context;
20920Sstevel@tonic-gate 	pcb.pcb_token = context;
20930Sstevel@tonic-gate 
20945478Sjhaslam 	if (context != DT_CTX_DPROG)
20955478Sjhaslam 		yybegin(YYS_EXPR);
20965478Sjhaslam 	else if (cflags & DTRACE_C_CTL)
20975478Sjhaslam 		yybegin(YYS_CONTROL);
20985478Sjhaslam 	else
20990Sstevel@tonic-gate 		yybegin(YYS_CLAUSE);
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
21020Sstevel@tonic-gate 		goto out;
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
21050Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
21080Sstevel@tonic-gate 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
21090Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
21120Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	/*
21150Sstevel@tonic-gate 	 * Invoke the parser to evaluate the D source code.  If any errors
21160Sstevel@tonic-gate 	 * occur during parsing, an error function will be called and we
21170Sstevel@tonic-gate 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
21180Sstevel@tonic-gate 	 * we optionally display the parse tree if debugging is enabled.
21190Sstevel@tonic-gate 	 */
21200Sstevel@tonic-gate 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
21210Sstevel@tonic-gate 		xyerror(D_EMPTY, "empty D program translation unit\n");
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	yybegin(YYS_DONE);
21240Sstevel@tonic-gate 
21255478Sjhaslam 	if (cflags & DTRACE_C_CTL)
21265478Sjhaslam 		goto out;
21275478Sjhaslam 
21280Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
21290Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	if (yypcb->pcb_pragmas != NULL)
21320Sstevel@tonic-gate 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
21350Sstevel@tonic-gate 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
21360Sstevel@tonic-gate 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
21370Sstevel@tonic-gate 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	/*
21410Sstevel@tonic-gate 	 * If we have successfully created a parse tree for a D program, loop
21420Sstevel@tonic-gate 	 * over the clauses and actions and instantiate the corresponding
21430Sstevel@tonic-gate 	 * libdtrace program.  If we are parsing a D expression, then we
21440Sstevel@tonic-gate 	 * simply run the code generator and assembler on the resulting tree.
21450Sstevel@tonic-gate 	 */
21460Sstevel@tonic-gate 	switch (context) {
21470Sstevel@tonic-gate 	case DT_CTX_DPROG:
21480Sstevel@tonic-gate 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
21510Sstevel@tonic-gate 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
21520Sstevel@tonic-gate 			xyerror(D_EMPTY, "empty D program translation unit\n");
21530Sstevel@tonic-gate 
2154265Smws 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
21550Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 		for (; dnp != NULL; dnp = dnp->dn_list) {
21580Sstevel@tonic-gate 			switch (dnp->dn_kind) {
21590Sstevel@tonic-gate 			case DT_NODE_CLAUSE:
21600Sstevel@tonic-gate 				dt_compile_clause(dtp, dnp);
21610Sstevel@tonic-gate 				break;
2162265Smws 			case DT_NODE_XLATOR:
2163265Smws 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2164265Smws 					dt_compile_xlator(dnp);
2165265Smws 				break;
21660Sstevel@tonic-gate 			case DT_NODE_PROVIDER:
21670Sstevel@tonic-gate 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
21680Sstevel@tonic-gate 				break;
21690Sstevel@tonic-gate 			}
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
2172265Smws 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2173265Smws 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2174265Smws 		yypcb->pcb_asxrefs = NULL;
2175265Smws 		yypcb->pcb_asxreflen = 0;
2176265Smws 
2177265Smws 		rv = yypcb->pcb_prog;
21780Sstevel@tonic-gate 		break;
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	case DT_CTX_DEXPR:
21810Sstevel@tonic-gate 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
21820Sstevel@tonic-gate 		dt_cg(yypcb, yypcb->pcb_root);
21830Sstevel@tonic-gate 		rv = dt_as(yypcb);
21840Sstevel@tonic-gate 		break;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	case DT_CTX_DTYPE:
21870Sstevel@tonic-gate 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
21880Sstevel@tonic-gate 		err = dt_decl_type(ddp, arg);
21890Sstevel@tonic-gate 		dt_decl_free(ddp);
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 		if (err != 0)
21920Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 		rv = NULL;
21950Sstevel@tonic-gate 		break;
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate out:
21990Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
22000Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
22030Sstevel@tonic-gate 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
22040Sstevel@tonic-gate 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
22050Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
22080Sstevel@tonic-gate 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
22090Sstevel@tonic-gate 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
22100Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
22130Sstevel@tonic-gate 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	dt_pcb_pop(dtp, err);
22160Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
22170Sstevel@tonic-gate 	return (err ? NULL : rv);
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate dtrace_prog_t *
dtrace_program_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_probespec_t spec,uint_t cflags,int argc,char * const argv[])22210Sstevel@tonic-gate dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
22220Sstevel@tonic-gate     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
22230Sstevel@tonic-gate {
22240Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
22250Sstevel@tonic-gate 	    spec, NULL, cflags, argc, argv, NULL, s));
22260Sstevel@tonic-gate }
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate dtrace_prog_t *
dtrace_program_fcompile(dtrace_hdl_t * dtp,FILE * fp,uint_t cflags,int argc,char * const argv[])22290Sstevel@tonic-gate dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
22300Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[])
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
22330Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate int
dtrace_type_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_typeinfo_t * dtt)22370Sstevel@tonic-gate dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
22380Sstevel@tonic-gate {
22390Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
22400Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
22410Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
22420Sstevel@tonic-gate }
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate int
dtrace_type_fcompile(dtrace_hdl_t * dtp,FILE * fp,dtrace_typeinfo_t * dtt)22450Sstevel@tonic-gate dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
22460Sstevel@tonic-gate {
22470Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
22480Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
22490Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
22500Sstevel@tonic-gate }
2251