xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_cc.c (revision c9d6cd77e4180c3831afde367c7eb129e72f0b2c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*c9d6cd77Sjhaslam  * Common Development and Distribution License (the "License").
6*c9d6cd77Sjhaslam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21900524f3Sahl 
227c478bd9Sstevel@tonic-gate /*
23*c9d6cd77Sjhaslam  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * DTrace D Language Compiler
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * The code in this source file implements the main engine for the D language
337c478bd9Sstevel@tonic-gate  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
347c478bd9Sstevel@tonic-gate  * compiler operates on either stdio FILEs or in-memory strings as its input
357c478bd9Sstevel@tonic-gate  * and can produce either dtrace_prog_t structures from a D program or a single
367c478bd9Sstevel@tonic-gate  * dtrace_difo_t structure from a D expression.  Multiple entry points are
377c478bd9Sstevel@tonic-gate  * provided as wrappers around dt_compile() for the various input/output pairs.
387c478bd9Sstevel@tonic-gate  * The compiler itself is implemented across the following source files:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * dt_lex.l - lex scanner
417c478bd9Sstevel@tonic-gate  * dt_grammar.y - yacc grammar
427c478bd9Sstevel@tonic-gate  * dt_parser.c - parse tree creation and semantic checking
437c478bd9Sstevel@tonic-gate  * dt_decl.c - declaration stack processing
447c478bd9Sstevel@tonic-gate  * dt_xlator.c - D translator lookup and creation
457c478bd9Sstevel@tonic-gate  * dt_ident.c - identifier and symbol table routines
467c478bd9Sstevel@tonic-gate  * dt_pragma.c - #pragma processing and D pragmas
477c478bd9Sstevel@tonic-gate  * dt_printf.c - D printf() and printa() argument checking and processing
487c478bd9Sstevel@tonic-gate  * dt_cc.c - compiler driver and dtrace_prog_t construction
497c478bd9Sstevel@tonic-gate  * dt_cg.c - DIF code generator
507c478bd9Sstevel@tonic-gate  * dt_as.c - DIF assembler
517c478bd9Sstevel@tonic-gate  * dt_dof.c - dtrace_prog_t -> DOF conversion
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Several other source files provide collections of utility routines used by
547c478bd9Sstevel@tonic-gate  * these major files.  The compiler itself is implemented in multiple passes:
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
577c478bd9Sstevel@tonic-gate  *     and parse tree nodes are constructed using the routines in dt_parser.c.
587c478bd9Sstevel@tonic-gate  *     This node construction pass is described further in dt_parser.c.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * (2) The parse tree is "cooked" by assigning each clause a context (see the
617c478bd9Sstevel@tonic-gate  *     routine dt_setcontext(), below) based on its probe description and then
627c478bd9Sstevel@tonic-gate  *     recursively descending the tree performing semantic checking.  The cook
637c478bd9Sstevel@tonic-gate  *     routines are also implemented in dt_parser.c and described there.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * (3) For actions that are DIF expression statements, the DIF code generator
667c478bd9Sstevel@tonic-gate  *     and assembler are invoked to create a finished DIFO for the statement.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * (4) The dtrace_prog_t data structures for the program clauses and actions
697c478bd9Sstevel@tonic-gate  *     are built, containing pointers to any DIFOs created in step (3).
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
727c478bd9Sstevel@tonic-gate  *     into DOF format for use in anonymous tracing or enabling in the kernel.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * In the implementation, steps 2-4 are intertwined in that they are performed
757c478bd9Sstevel@tonic-gate  * in order for each clause as part of a loop that executes over the clauses.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * The D compiler currently implements nearly no optimization.  The compiler
787c478bd9Sstevel@tonic-gate  * implements integer constant folding as part of pass (1), and a set of very
797c478bd9Sstevel@tonic-gate  * simple peephole optimizations as part of pass (3).  As with any C compiler,
807c478bd9Sstevel@tonic-gate  * a large number of optimizations are possible on both the intermediate data
817c478bd9Sstevel@tonic-gate  * structures and the generated DIF code.  These possibilities should be
827c478bd9Sstevel@tonic-gate  * investigated in the context of whether they will have any substantive effect
837c478bd9Sstevel@tonic-gate  * on the overall DTrace probe effect before they are undertaken.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <sys/types.h>
877c478bd9Sstevel@tonic-gate #include <sys/wait.h>
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #include <assert.h>
907c478bd9Sstevel@tonic-gate #include <strings.h>
917c478bd9Sstevel@tonic-gate #include <signal.h>
927c478bd9Sstevel@tonic-gate #include <unistd.h>
937c478bd9Sstevel@tonic-gate #include <stdlib.h>
947c478bd9Sstevel@tonic-gate #include <stdio.h>
957c478bd9Sstevel@tonic-gate #include <errno.h>
967c478bd9Sstevel@tonic-gate #include <ucontext.h>
977c478bd9Sstevel@tonic-gate #include <limits.h>
987c478bd9Sstevel@tonic-gate #include <ctype.h>
997c478bd9Sstevel@tonic-gate #include <dirent.h>
1007c478bd9Sstevel@tonic-gate #include <dt_module.h>
1011a7c1b72Smws #include <dt_program.h>
1027c478bd9Sstevel@tonic-gate #include <dt_provider.h>
1037c478bd9Sstevel@tonic-gate #include <dt_printf.h>
1047c478bd9Sstevel@tonic-gate #include <dt_pid.h>
1057c478bd9Sstevel@tonic-gate #include <dt_grammar.h>
1067c478bd9Sstevel@tonic-gate #include <dt_ident.h>
1077c478bd9Sstevel@tonic-gate #include <dt_string.h>
1087c478bd9Sstevel@tonic-gate #include <dt_impl.h>
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static const dtrace_diftype_t dt_void_rtype = {
1117c478bd9Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static const dtrace_diftype_t dt_int_rtype = {
1157c478bd9Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
118*c9d6cd77Sjhaslam static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
119*c9d6cd77Sjhaslam     uint_t, int, char *const[], FILE *, const char *);
120*c9d6cd77Sjhaslam 
121*c9d6cd77Sjhaslam 
1227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1237c478bd9Sstevel@tonic-gate static int
1247c478bd9Sstevel@tonic-gate dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
1277c478bd9Sstevel@tonic-gate 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
1287c478bd9Sstevel@tonic-gate 	return (0);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1327c478bd9Sstevel@tonic-gate static int
1337c478bd9Sstevel@tonic-gate dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	yylineno = idp->di_lineno;
1367c478bd9Sstevel@tonic-gate 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static dtrace_stmtdesc_t *
1417c478bd9Sstevel@tonic-gate dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
1427c478bd9Sstevel@tonic-gate     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (sdp == NULL)
1477c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_stmt == NULL);
1507c478bd9Sstevel@tonic-gate 	yypcb->pcb_stmt = sdp;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	sdp->dtsd_descattr = descattr;
1537c478bd9Sstevel@tonic-gate 	sdp->dtsd_stmtattr = stmtattr;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (sdp);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static dtrace_actdesc_t *
1597c478bd9Sstevel@tonic-gate dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *new;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
1647c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (new);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Utility function to determine if a given action description is destructive.
1717c478bd9Sstevel@tonic-gate  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate static int
1747c478bd9Sstevel@tonic-gate dt_action_destructive(const dtrace_actdesc_t *ap)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
1777c478bd9Sstevel@tonic-gate 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static void
1817c478bd9Sstevel@tonic-gate dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
1847c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap, *tap;
1857c478bd9Sstevel@tonic-gate 	int commit = 0;
1867c478bd9Sstevel@tonic-gate 	int speculate = 0;
1877c478bd9Sstevel@tonic-gate 	int datarec = 0;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * Make sure that the new statement jibes with the rest of the ECB.
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
1937c478bd9Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
1947c478bd9Sstevel@tonic-gate 			if (commit) {
1957c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
1967c478bd9Sstevel@tonic-gate 				    "not follow commit( )\n");
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 			if (datarec) {
2007c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
2017c478bd9Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
2057c478bd9Sstevel@tonic-gate 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
2067c478bd9Sstevel@tonic-gate 					continue;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
2097c478bd9Sstevel@tonic-gate 				    "may not follow commit( )\n");
2107c478bd9Sstevel@tonic-gate 			}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 			commit = 1;
2137c478bd9Sstevel@tonic-gate 			continue;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
2177c478bd9Sstevel@tonic-gate 			if (speculate) {
2187c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
2197c478bd9Sstevel@tonic-gate 				    "not follow speculate( )\n");
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 			if (commit) {
2237c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
2247c478bd9Sstevel@tonic-gate 				    "not follow commit( )\n");
2257c478bd9Sstevel@tonic-gate 			}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 			if (datarec) {
2287c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
2297c478bd9Sstevel@tonic-gate 				    "not follow data-recording action(s)\n");
2307c478bd9Sstevel@tonic-gate 			}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 			speculate = 1;
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
2377c478bd9Sstevel@tonic-gate 			if (speculate) {
2387c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
2397c478bd9Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			datarec = 1;
2437c478bd9Sstevel@tonic-gate 			continue;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		if (speculate) {
2477c478bd9Sstevel@tonic-gate 			if (dt_action_destructive(ap)) {
2487c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
2497c478bd9Sstevel@tonic-gate 				    "may not follow speculate( )\n");
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 			if (ap->dtad_kind == DTRACEACT_EXIT) {
2537c478bd9Sstevel@tonic-gate 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
2547c478bd9Sstevel@tonic-gate 				    "follow speculate( )\n");
2557c478bd9Sstevel@tonic-gate 			}
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * Exclude all non data-recording actions.
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		if (dt_action_destructive(ap) ||
2627c478bd9Sstevel@tonic-gate 		    ap->dtad_kind == DTRACEACT_DISCARD)
2637c478bd9Sstevel@tonic-gate 			continue;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
2667c478bd9Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
2677c478bd9Sstevel@tonic-gate 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (commit) {
2717c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
2727c478bd9Sstevel@tonic-gate 			    "may not follow commit( )\n");
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		if (!speculate)
2767c478bd9Sstevel@tonic-gate 			datarec = 1;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
2807c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_stmt == sdp)
2837c478bd9Sstevel@tonic-gate 		yypcb->pcb_stmt = NULL;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * For the first element of an aggregation tuple or for printa(), we create a
2887c478bd9Sstevel@tonic-gate  * simple DIF program that simply returns the immediate value that is the ID
2897c478bd9Sstevel@tonic-gate  * of the aggregation itself.  This could be optimized in the future by
2907c478bd9Sstevel@tonic-gate  * creating a new in-kernel dtad_kind that just returns an integer.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static void
2937c478bd9Sstevel@tonic-gate dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
2947c478bd9Sstevel@tonic-gate {
2951a7c1b72Smws 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2961a7c1b72Smws 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (dp == NULL)
2997c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3007c478bd9Sstevel@tonic-gate 
3011a7c1b72Smws 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
3021a7c1b72Smws 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
3051a7c1b72Smws 		dt_difo_free(dtp, dp);
3067c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
3107c478bd9Sstevel@tonic-gate 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
3117c478bd9Sstevel@tonic-gate 	dp->dtdo_len = 2;
3127c478bd9Sstevel@tonic-gate 	dp->dtdo_inttab[0] = id;
3137c478bd9Sstevel@tonic-gate 	dp->dtdo_intlen = 1;
3147c478bd9Sstevel@tonic-gate 	dp->dtdo_rtype = dt_int_rtype;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dp;
3177c478bd9Sstevel@tonic-gate 	ap->dtad_kind = kind;
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static void
3217c478bd9Sstevel@tonic-gate dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	dt_ident_t *aid;
3247c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3257c478bd9Sstevel@tonic-gate 	dt_node_t *anp;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3287c478bd9Sstevel@tonic-gate 	int argc = 0;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3317c478bd9Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (argc != 1) {
3347c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_PROTO,
3357c478bd9Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
3367c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	anp = dnp->dn_args;
3407c478bd9Sstevel@tonic-gate 	assert(anp != NULL);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3437c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGARG,
3447c478bd9Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3457c478bd9Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3467c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3477c478bd9Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	aid = anp->dn_ident;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3537c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_CLEAR_AGGBAD,
3547c478bd9Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
3587c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
3597c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_CLEAR;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate static void
3637c478bd9Sstevel@tonic-gate dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	dt_ident_t *aid;
3667c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
3677c478bd9Sstevel@tonic-gate 	dt_node_t *anp, *normal;
3687c478bd9Sstevel@tonic-gate 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3717c478bd9Sstevel@tonic-gate 	int argc = 0;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3747c478bd9Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
3777c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_PROTO,
3787c478bd9Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
3797c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	anp = dnp->dn_args;
3837c478bd9Sstevel@tonic-gate 	assert(anp != NULL);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
3867c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGARG,
3877c478bd9Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
3887c478bd9Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
3897c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
3907c478bd9Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
3947c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_SCALAR,
3957c478bd9Sstevel@tonic-gate 		    "%s( ) argument #2 must be of scalar type\n",
3967c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	aid = anp->dn_ident;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4027c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_NORMALIZE_AGGBAD,
4037c478bd9Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4077c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (denormal) {
4107c478bd9Sstevel@tonic-gate 		ap->dtad_arg = DT_ACT_DENORMALIZE;
4117c478bd9Sstevel@tonic-gate 		return;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	assert(normal != NULL);
4177c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4187c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, normal);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
4217c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_LIBACT;
4227c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_NORMALIZE;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static void
4267c478bd9Sstevel@tonic-gate dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	dt_ident_t *aid;
4297c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4307c478bd9Sstevel@tonic-gate 	dt_node_t *anp, *trunc;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4337c478bd9Sstevel@tonic-gate 	int argc = 0;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
4367c478bd9Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (argc > 2 || argc < 1) {
4397c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_PROTO,
4407c478bd9Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
4417c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc,
4427c478bd9Sstevel@tonic-gate 		    argc < 1 ? "at least 1" : "no more than 2");
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	anp = dnp->dn_args;
4467c478bd9Sstevel@tonic-gate 	assert(anp != NULL);
4477c478bd9Sstevel@tonic-gate 	trunc = anp->dn_list;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (anp->dn_kind != DT_NODE_AGG) {
4507c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGARG,
4517c478bd9Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
4527c478bd9Sstevel@tonic-gate 		    "\tprototype: aggregation\n\t argument: %s\n",
4537c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
4547c478bd9Sstevel@tonic-gate 		    dt_node_type_name(anp, n, sizeof (n)));
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (argc == 2) {
4587c478bd9Sstevel@tonic-gate 		assert(trunc != NULL);
4597c478bd9Sstevel@tonic-gate 		if (!dt_node_is_scalar(trunc)) {
4607c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_TRUNC_SCALAR,
4617c478bd9Sstevel@tonic-gate 			    "%s( ) argument #2 must be of scalar type\n",
4627c478bd9Sstevel@tonic-gate 			    dnp->dn_ident->di_name);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	aid = anp->dn_ident;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4697c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_TRUNC_AGGBAD,
4707c478bd9Sstevel@tonic-gate 		    "undefined aggregation: @%s\n", aid->di_name);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4747c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4757c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (argc == 1) {
4807c478bd9Sstevel@tonic-gate 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		assert(trunc != NULL);
4837c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, trunc);
4847c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
4857c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_LIBACT;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_TRUNC;
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static void
4927c478bd9Sstevel@tonic-gate dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
4957c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
4967c478bd9Sstevel@tonic-gate 	const char *format;
49730ef842dSbmc 	dt_node_t *anp, *proto = NULL;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
5007c478bd9Sstevel@tonic-gate 	int argc = 0, argr = 0;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
5037c478bd9Sstevel@tonic-gate 		argc++; /* count up arguments for error messages below */
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	switch (dnp->dn_args->dn_kind) {
5067c478bd9Sstevel@tonic-gate 	case DT_NODE_STRING:
5077c478bd9Sstevel@tonic-gate 		format = dnp->dn_args->dn_string;
5087c478bd9Sstevel@tonic-gate 		anp = dnp->dn_args->dn_list;
5097c478bd9Sstevel@tonic-gate 		argr = 2;
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 	case DT_NODE_AGG:
5127c478bd9Sstevel@tonic-gate 		format = NULL;
5137c478bd9Sstevel@tonic-gate 		anp = dnp->dn_args;
5147c478bd9Sstevel@tonic-gate 		argr = 1;
5157c478bd9Sstevel@tonic-gate 		break;
5167c478bd9Sstevel@tonic-gate 	default:
5177c478bd9Sstevel@tonic-gate 		format = NULL;
5187c478bd9Sstevel@tonic-gate 		anp = dnp->dn_args;
5197c478bd9Sstevel@tonic-gate 		argr = 1;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
52230ef842dSbmc 	if (argc < argr) {
5237c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_PRINTA_PROTO,
5247c478bd9Sstevel@tonic-gate 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
5257c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name, argc, argr);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
52830ef842dSbmc 	assert(anp != NULL);
5297c478bd9Sstevel@tonic-gate 
53030ef842dSbmc 	while (anp != NULL) {
53130ef842dSbmc 		if (anp->dn_kind != DT_NODE_AGG) {
53230ef842dSbmc 			dnerror(dnp, D_PRINTA_AGGARG,
53330ef842dSbmc 			    "%s( ) argument #%d is incompatible with "
53430ef842dSbmc 			    "prototype:\n\tprototype: aggregation\n"
53530ef842dSbmc 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
53630ef842dSbmc 			    dt_node_type_name(anp, n, sizeof (n)));
53730ef842dSbmc 		}
5387c478bd9Sstevel@tonic-gate 
53930ef842dSbmc 		aid = anp->dn_ident;
54030ef842dSbmc 		fid = aid->di_iarg;
5417c478bd9Sstevel@tonic-gate 
54230ef842dSbmc 		if (aid->di_gen == dtp->dt_gen &&
54330ef842dSbmc 		    !(aid->di_flags & DT_IDFLG_MOD)) {
54430ef842dSbmc 			dnerror(dnp, D_PRINTA_AGGBAD,
54530ef842dSbmc 			    "undefined aggregation: @%s\n", aid->di_name);
54630ef842dSbmc 		}
5477c478bd9Sstevel@tonic-gate 
54830ef842dSbmc 		/*
54930ef842dSbmc 		 * If we have multiple aggregations, we must be sure that
55030ef842dSbmc 		 * their key signatures match.
55130ef842dSbmc 		 */
55230ef842dSbmc 		if (proto != NULL) {
55330ef842dSbmc 			dt_printa_validate(proto, anp);
55430ef842dSbmc 		} else {
55530ef842dSbmc 			proto = anp;
55630ef842dSbmc 		}
5577c478bd9Sstevel@tonic-gate 
55830ef842dSbmc 		if (format != NULL) {
55930ef842dSbmc 			yylineno = dnp->dn_line;
56030ef842dSbmc 
56130ef842dSbmc 			sdp->dtsd_fmtdata =
56230ef842dSbmc 			    dt_printf_create(yypcb->pcb_hdl, format);
56330ef842dSbmc 			dt_printf_validate(sdp->dtsd_fmtdata,
56430ef842dSbmc 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
56530ef842dSbmc 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
56630ef842dSbmc 			format = NULL;
56730ef842dSbmc 		}
56830ef842dSbmc 
56930ef842dSbmc 		ap = dt_stmt_action(dtp, sdp);
57030ef842dSbmc 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
57130ef842dSbmc 
57230ef842dSbmc 		anp = anp->dn_list;
57330ef842dSbmc 		argr++;
57430ef842dSbmc 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate static void
5787c478bd9Sstevel@tonic-gate dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
5797c478bd9Sstevel@tonic-gate     dtrace_actkind_t kind)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	dt_node_t *anp, *arg1;
5827c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = NULL;
5837c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN], *str;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	assert(DTRACEACT_ISPRINTFLIKE(kind));
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
5887c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_PRINTF_ARG_FMT,
5897c478bd9Sstevel@tonic-gate 		    "%s( ) argument #1 is incompatible with prototype:\n"
5907c478bd9Sstevel@tonic-gate 		    "\tprototype: string constant\n\t argument: %s\n",
5917c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name,
5927c478bd9Sstevel@tonic-gate 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	arg1 = dnp->dn_args->dn_list;
5967c478bd9Sstevel@tonic-gate 	yylineno = dnp->dn_line;
5977c478bd9Sstevel@tonic-gate 	str = dnp->dn_args->dn_string;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * If this is an freopen(), we use an empty string to denote that
6027c478bd9Sstevel@tonic-gate 	 * stdout should be restored.  For other printf()-like actions, an
6037c478bd9Sstevel@tonic-gate 	 * empty format string is illegal:  an empty format string would
6047c478bd9Sstevel@tonic-gate 	 * result in malformed DOF, and the compiler thus flags an empty
6057c478bd9Sstevel@tonic-gate 	 * format string as a compile-time error.  To avoid propagating the
6067c478bd9Sstevel@tonic-gate 	 * freopen() special case throughout the system, we simply transpose
6077c478bd9Sstevel@tonic-gate 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
6087c478bd9Sstevel@tonic-gate 	 * denotes that stdout should be restored.
6097c478bd9Sstevel@tonic-gate 	 */
6107c478bd9Sstevel@tonic-gate 	if (kind == DTRACEACT_FREOPEN) {
6117c478bd9Sstevel@tonic-gate 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
6127c478bd9Sstevel@tonic-gate 			/*
6137c478bd9Sstevel@tonic-gate 			 * Our sentinel is always an invalid argument to
6147c478bd9Sstevel@tonic-gate 			 * freopen(), but if it's been manually specified, we
6157c478bd9Sstevel@tonic-gate 			 * must fail now instead of when the freopen() is
6167c478bd9Sstevel@tonic-gate 			 * actually evaluated.
6177c478bd9Sstevel@tonic-gate 			 */
6187c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_FREOPEN_INVALID,
6197c478bd9Sstevel@tonic-gate 			    "%s( ) argument #1 cannot be \"%s\"\n",
6207c478bd9Sstevel@tonic-gate 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		if (str[0] == '\0')
6247c478bd9Sstevel@tonic-gate 			str = DT_FREOPEN_RESTORE;
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6271a7c1b72Smws 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
6307c478bd9Sstevel@tonic-gate 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (arg1 == NULL) {
6337c478bd9Sstevel@tonic-gate 		dif_instr_t *dbuf;
6347c478bd9Sstevel@tonic-gate 		dtrace_difo_t *dp;
6357c478bd9Sstevel@tonic-gate 
6361a7c1b72Smws 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
6371a7c1b72Smws 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
6381a7c1b72Smws 			dt_free(dtp, dbuf);
6397c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		dp->dtdo_buf = dbuf;
6457c478bd9Sstevel@tonic-gate 		dp->dtdo_len = 1;
6467c478bd9Sstevel@tonic-gate 		dp->dtdo_rtype = dt_int_rtype;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6497c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dp;
6507c478bd9Sstevel@tonic-gate 		ap->dtad_kind = kind;
6517c478bd9Sstevel@tonic-gate 		return;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
6557c478bd9Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
6567c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, anp);
6577c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
6587c478bd9Sstevel@tonic-gate 		ap->dtad_kind = kind;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate static void
6637c478bd9Sstevel@tonic-gate dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (dt_node_is_void(dnp->dn_args)) {
6687c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_VOID,
6697c478bd9Sstevel@tonic-gate 		    "trace( ) may not be applied to a void expression\n");
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (dt_node_is_dynamic(dnp->dn_args)) {
6737c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_args, D_TRACE_DYN,
6747c478bd9Sstevel@tonic-gate 		    "trace( ) may not be applied to a dynamic expression\n");
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
6787c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
6797c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static void
6837c478bd9Sstevel@tonic-gate dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	dt_node_t *addr = dnp->dn_args;
6887c478bd9Sstevel@tonic-gate 	dt_node_t *size = dnp->dn_args->dn_list;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
6937c478bd9Sstevel@tonic-gate 		dnerror(addr, D_TRACEMEM_ADDR,
6947c478bd9Sstevel@tonic-gate 		    "tracemem( ) argument #1 is incompatible with "
6957c478bd9Sstevel@tonic-gate 		    "prototype:\n\tprototype: pointer or integer\n"
6967c478bd9Sstevel@tonic-gate 		    "\t argument: %s\n",
6977c478bd9Sstevel@tonic-gate 		    dt_node_type_name(addr, n, sizeof (n)));
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if (dt_node_is_posconst(size) == 0) {
7017c478bd9Sstevel@tonic-gate 		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
7027c478bd9Sstevel@tonic-gate 		    "be a non-zero positive integral constant expression\n");
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, addr);
7067c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
7077c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
7107c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate static void
7147c478bd9Sstevel@tonic-gate dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STACK;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7197c478bd9Sstevel@tonic-gate 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7207c478bd9Sstevel@tonic-gate 	} else {
7217c478bd9Sstevel@tonic-gate 		ap->dtad_arg = 0;
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
7257c478bd9Sstevel@tonic-gate 		if (arg0->dn_list != NULL) {
7267c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
7277c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if (dt_node_is_posconst(arg0) == 0) {
7317c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
7327c478bd9Sstevel@tonic-gate 			    "non-zero positive integral constant expression\n");
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		ap->dtad_arg = arg0->dn_value;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate static void
7407c478bd9Sstevel@tonic-gate dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7437c478bd9Sstevel@tonic-gate 	dt_action_stack_args(dtp, ap, dnp->dn_args);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate static void
7477c478bd9Sstevel@tonic-gate dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	uint32_t nframes = 0;
7507c478bd9Sstevel@tonic-gate 	uint32_t strsize = 0;	/* default string table size */
7517c478bd9Sstevel@tonic-gate 	dt_node_t *arg0 = dnp->dn_args;
7527c478bd9Sstevel@tonic-gate 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
7557c478bd9Sstevel@tonic-gate 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
7587c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
7597c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
7627c478bd9Sstevel@tonic-gate 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_JSTACK;
7657c478bd9Sstevel@tonic-gate 	} else {
7667c478bd9Sstevel@tonic-gate 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
7697c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_USTACK;
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
7757c478bd9Sstevel@tonic-gate 		if (!dt_node_is_posconst(arg0)) {
7767c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
7777c478bd9Sstevel@tonic-gate 			    "must be a non-zero positive integer constant\n");
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 		nframes = (uint32_t)arg0->dn_value;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (arg1 != NULL) {
7837c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT ||
7847c478bd9Sstevel@tonic-gate 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
7857c478bd9Sstevel@tonic-gate 		    (int64_t)arg1->dn_value < 0)) {
7867c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
7877c478bd9Sstevel@tonic-gate 			    "must be a positive integer constant\n");
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		if (arg1->dn_list != NULL) {
7917c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
7927c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		strsize = (uint32_t)arg1->dn_value;
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate static void
8027c478bd9Sstevel@tonic-gate dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8057c478bd9Sstevel@tonic-gate 	dt_action_ustack_args(dtp, ap, dnp);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
808a1b5e537Sbmc static void
809a1b5e537Sbmc dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
810a1b5e537Sbmc {
811a1b5e537Sbmc 	dtrace_actdesc_t *ap;
812a1b5e537Sbmc 	dt_node_t *arg0, *arg1;
813a1b5e537Sbmc 
814a1b5e537Sbmc 	/*
815a1b5e537Sbmc 	 * The prototype guarantees that we are called with either one or
816a1b5e537Sbmc 	 * two arguments, and that any arguments that are present are strings.
817a1b5e537Sbmc 	 */
818a1b5e537Sbmc 	arg0 = dnp->dn_args;
819a1b5e537Sbmc 	arg1 = arg0->dn_list;
820a1b5e537Sbmc 
821a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
822a1b5e537Sbmc 	dt_cg(yypcb, arg0);
823a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
824a1b5e537Sbmc 	ap->dtad_kind = DTRACEACT_LIBACT;
825a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
826a1b5e537Sbmc 
827a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
828a1b5e537Sbmc 
829a1b5e537Sbmc 	if (arg1 == NULL) {
830a1b5e537Sbmc 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
831a1b5e537Sbmc 	} else {
832a1b5e537Sbmc 		dt_cg(yypcb, arg1);
833a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
834a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_LIBACT;
835a1b5e537Sbmc 	}
836a1b5e537Sbmc 
837a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
838a1b5e537Sbmc }
839a1b5e537Sbmc 
840a1b5e537Sbmc /*ARGSUSED*/
841a1b5e537Sbmc static void
842a1b5e537Sbmc dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
843a1b5e537Sbmc     dt_node_t *dnp, dtrace_actkind_t kind)
844a1b5e537Sbmc {
845a1b5e537Sbmc 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
846a1b5e537Sbmc 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
847a1b5e537Sbmc 	    kind == DTRACEACT_UADDR);
848a1b5e537Sbmc 
849a1b5e537Sbmc 	dt_cg(yypcb, dnp);
850a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
851a1b5e537Sbmc 	ap->dtad_kind = kind;
852a1b5e537Sbmc 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
853a1b5e537Sbmc }
854a1b5e537Sbmc 
855a1b5e537Sbmc static void
856a1b5e537Sbmc dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
857a1b5e537Sbmc     dtrace_actkind_t kind)
858a1b5e537Sbmc {
859a1b5e537Sbmc 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
860a1b5e537Sbmc 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
861a1b5e537Sbmc }
862a1b5e537Sbmc 
8637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8647c478bd9Sstevel@tonic-gate static void
8657c478bd9Sstevel@tonic-gate dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/*
8707c478bd9Sstevel@tonic-gate 	 * Library actions need a DIFO that serves as an argument.  As
8717c478bd9Sstevel@tonic-gate 	 * ftruncate() doesn't take an argument, we generate the constant 0
8727c478bd9Sstevel@tonic-gate 	 * in a DIFO; this constant will be ignored when the ftruncate() is
8737c478bd9Sstevel@tonic-gate 	 * processed.
8747c478bd9Sstevel@tonic-gate 	 */
8757c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
8767c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_FTRUNCATE;
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8807c478bd9Sstevel@tonic-gate static void
8817c478bd9Sstevel@tonic-gate dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STOP;
8867c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8907c478bd9Sstevel@tonic-gate static void
8917c478bd9Sstevel@tonic-gate dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
8967c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9007c478bd9Sstevel@tonic-gate static void
9017c478bd9Sstevel@tonic-gate dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_PANIC;
9067c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate static void
9107c478bd9Sstevel@tonic-gate dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9157c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9167c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_CHILL;
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate static void
9207c478bd9Sstevel@tonic-gate dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9257c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9267c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_RAISE;
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate static void
9307c478bd9Sstevel@tonic-gate dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9357c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9367c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_EXIT;
9377c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate static void
9417c478bd9Sstevel@tonic-gate dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9467c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9477c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_SPECULATE;
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate static void
9517c478bd9Sstevel@tonic-gate dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9567c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9577c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_COMMIT;
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate static void
9617c478bd9Sstevel@tonic-gate dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9667c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9677c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DISCARD;
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate static void
9717c478bd9Sstevel@tonic-gate dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	switch (dnp->dn_expr->dn_ident->di_id) {
9747c478bd9Sstevel@tonic-gate 	case DT_ACT_BREAKPOINT:
9757c478bd9Sstevel@tonic-gate 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
9767c478bd9Sstevel@tonic-gate 		break;
9777c478bd9Sstevel@tonic-gate 	case DT_ACT_CHILL:
9787c478bd9Sstevel@tonic-gate 		dt_action_chill(dtp, dnp->dn_expr, sdp);
9797c478bd9Sstevel@tonic-gate 		break;
9807c478bd9Sstevel@tonic-gate 	case DT_ACT_CLEAR:
9817c478bd9Sstevel@tonic-gate 		dt_action_clear(dtp, dnp->dn_expr, sdp);
9827c478bd9Sstevel@tonic-gate 		break;
9837c478bd9Sstevel@tonic-gate 	case DT_ACT_COMMIT:
9847c478bd9Sstevel@tonic-gate 		dt_action_commit(dtp, dnp->dn_expr, sdp);
9857c478bd9Sstevel@tonic-gate 		break;
9867c478bd9Sstevel@tonic-gate 	case DT_ACT_DENORMALIZE:
9877c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
9887c478bd9Sstevel@tonic-gate 		break;
9897c478bd9Sstevel@tonic-gate 	case DT_ACT_DISCARD:
9907c478bd9Sstevel@tonic-gate 		dt_action_discard(dtp, dnp->dn_expr, sdp);
9917c478bd9Sstevel@tonic-gate 		break;
9927c478bd9Sstevel@tonic-gate 	case DT_ACT_EXIT:
9937c478bd9Sstevel@tonic-gate 		dt_action_exit(dtp, dnp->dn_expr, sdp);
9947c478bd9Sstevel@tonic-gate 		break;
9957c478bd9Sstevel@tonic-gate 	case DT_ACT_FREOPEN:
9967c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
9977c478bd9Sstevel@tonic-gate 		break;
9987c478bd9Sstevel@tonic-gate 	case DT_ACT_FTRUNCATE:
9997c478bd9Sstevel@tonic-gate 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
10007c478bd9Sstevel@tonic-gate 		break;
1001a1b5e537Sbmc 	case DT_ACT_MOD:
1002a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1003a1b5e537Sbmc 		break;
10047c478bd9Sstevel@tonic-gate 	case DT_ACT_NORMALIZE:
10057c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10067c478bd9Sstevel@tonic-gate 		break;
10077c478bd9Sstevel@tonic-gate 	case DT_ACT_PANIC:
10087c478bd9Sstevel@tonic-gate 		dt_action_panic(dtp, dnp->dn_expr, sdp);
10097c478bd9Sstevel@tonic-gate 		break;
10107c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTA:
10117c478bd9Sstevel@tonic-gate 		dt_action_printa(dtp, dnp->dn_expr, sdp);
10127c478bd9Sstevel@tonic-gate 		break;
10137c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTF:
10147c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
10157c478bd9Sstevel@tonic-gate 		break;
10167c478bd9Sstevel@tonic-gate 	case DT_ACT_RAISE:
10177c478bd9Sstevel@tonic-gate 		dt_action_raise(dtp, dnp->dn_expr, sdp);
10187c478bd9Sstevel@tonic-gate 		break;
1019a1b5e537Sbmc 	case DT_ACT_SETOPT:
1020a1b5e537Sbmc 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1021a1b5e537Sbmc 		break;
10227c478bd9Sstevel@tonic-gate 	case DT_ACT_SPECULATE:
10237c478bd9Sstevel@tonic-gate 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
10247c478bd9Sstevel@tonic-gate 		break;
10257c478bd9Sstevel@tonic-gate 	case DT_ACT_STACK:
10267c478bd9Sstevel@tonic-gate 		dt_action_stack(dtp, dnp->dn_expr, sdp);
10277c478bd9Sstevel@tonic-gate 		break;
10287c478bd9Sstevel@tonic-gate 	case DT_ACT_STOP:
10297c478bd9Sstevel@tonic-gate 		dt_action_stop(dtp, dnp->dn_expr, sdp);
10307c478bd9Sstevel@tonic-gate 		break;
1031a1b5e537Sbmc 	case DT_ACT_SYM:
1032a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1033a1b5e537Sbmc 		break;
10347c478bd9Sstevel@tonic-gate 	case DT_ACT_SYSTEM:
10357c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
10367c478bd9Sstevel@tonic-gate 		break;
10377c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACE:
10387c478bd9Sstevel@tonic-gate 		dt_action_trace(dtp, dnp->dn_expr, sdp);
10397c478bd9Sstevel@tonic-gate 		break;
10407c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACEMEM:
10417c478bd9Sstevel@tonic-gate 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
10427c478bd9Sstevel@tonic-gate 		break;
10437c478bd9Sstevel@tonic-gate 	case DT_ACT_TRUNC:
10447c478bd9Sstevel@tonic-gate 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
10457c478bd9Sstevel@tonic-gate 		break;
1046a1b5e537Sbmc 	case DT_ACT_UADDR:
1047a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1048a1b5e537Sbmc 		break;
1049a1b5e537Sbmc 	case DT_ACT_UMOD:
1050a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1051a1b5e537Sbmc 		break;
1052a1b5e537Sbmc 	case DT_ACT_USYM:
1053a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1054a1b5e537Sbmc 		break;
10557c478bd9Sstevel@tonic-gate 	case DT_ACT_USTACK:
10567c478bd9Sstevel@tonic-gate 	case DT_ACT_JSTACK:
10577c478bd9Sstevel@tonic-gate 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
10587c478bd9Sstevel@tonic-gate 		break;
10597c478bd9Sstevel@tonic-gate 	default:
10607c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
10617c478bd9Sstevel@tonic-gate 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate static void
10667c478bd9Sstevel@tonic-gate dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_expr);
10717c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10727c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
10737c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate static void
10777c478bd9Sstevel@tonic-gate dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
1080a1b5e537Sbmc 	dt_node_t *anp, *incr = NULL;
10817c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
1082a1b5e537Sbmc 	uint_t n = 1, argmax;
1083a1b5e537Sbmc 	uint64_t arg = 0;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 * If the aggregation has no aggregating function applied to it, then
10877c478bd9Sstevel@tonic-gate 	 * this statement has no effect.  Flag this as a programming error.
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun == NULL) {
10907c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
10917c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	aid = dnp->dn_ident;
10957c478bd9Sstevel@tonic-gate 	fid = dnp->dn_aggfun->dn_ident;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun->dn_args != NULL &&
10987c478bd9Sstevel@tonic-gate 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
10997c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
11007c478bd9Sstevel@tonic-gate 		    "be of scalar type\n", fid->di_name);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/*
11047c478bd9Sstevel@tonic-gate 	 * The ID of the aggregation itself is implicitly recorded as the first
11057c478bd9Sstevel@tonic-gate 	 * member of each aggregation tuple so we can distinguish them later.
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
11087c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
11117c478bd9Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
11127c478bd9Sstevel@tonic-gate 		n++;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		if (anp->dn_kind == DT_NODE_FUNC) {
11157c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
11167c478bd9Sstevel@tonic-gate 				dt_action_stack_args(dtp, ap, anp->dn_args);
11177c478bd9Sstevel@tonic-gate 				continue;
11187c478bd9Sstevel@tonic-gate 			}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
11217c478bd9Sstevel@tonic-gate 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
11227c478bd9Sstevel@tonic-gate 				dt_action_ustack_args(dtp, ap, anp);
11237c478bd9Sstevel@tonic-gate 				continue;
11247c478bd9Sstevel@tonic-gate 			}
11257c478bd9Sstevel@tonic-gate 
1126a1b5e537Sbmc 			switch (anp->dn_ident->di_id) {
1127a1b5e537Sbmc 			case DT_ACT_UADDR:
1128a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1129a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UADDR);
1130a1b5e537Sbmc 				continue;
11317c478bd9Sstevel@tonic-gate 
1132a1b5e537Sbmc 			case DT_ACT_USYM:
1133a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1134a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_USYM);
1135a1b5e537Sbmc 				continue;
11367c478bd9Sstevel@tonic-gate 
1137a1b5e537Sbmc 			case DT_ACT_UMOD:
1138a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1139a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UMOD);
1140a1b5e537Sbmc 				continue;
11417c478bd9Sstevel@tonic-gate 
1142a1b5e537Sbmc 			case DT_ACT_SYM:
1143a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1144a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_SYM);
1145a1b5e537Sbmc 				continue;
1146a1b5e537Sbmc 
1147a1b5e537Sbmc 			case DT_ACT_MOD:
1148a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1149a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_MOD);
1150a1b5e537Sbmc 				continue;
1151a1b5e537Sbmc 
1152a1b5e537Sbmc 			default:
1153a1b5e537Sbmc 				break;
1154a1b5e537Sbmc 			}
1155a1b5e537Sbmc 		}
1156a1b5e537Sbmc 
1157a1b5e537Sbmc 		dt_cg(yypcb, anp);
11587c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
1159a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
11637c478bd9Sstevel@tonic-gate 		/*
1164a1b5e537Sbmc 		 * For linear quantization, we have between two and four
1165a1b5e537Sbmc 		 * arguments in addition to the expression:
11667c478bd9Sstevel@tonic-gate 		 *
11677c478bd9Sstevel@tonic-gate 		 *    arg1 => Base value
11687c478bd9Sstevel@tonic-gate 		 *    arg2 => Limit value
11697c478bd9Sstevel@tonic-gate 		 *    arg3 => Quantization level step size (defaults to 1)
1170a1b5e537Sbmc 		 *    arg4 => Quantization increment value (defaults to 1)
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
11737c478bd9Sstevel@tonic-gate 		dt_node_t *arg2 = arg1->dn_list;
11747c478bd9Sstevel@tonic-gate 		dt_node_t *arg3 = arg2->dn_list;
117530ef842dSbmc 		dt_idsig_t *isp;
117630ef842dSbmc 		uint64_t nlevels, step = 1, oarg;
11777c478bd9Sstevel@tonic-gate 		int64_t baseval, limitval;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT) {
11807c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
11817c478bd9Sstevel@tonic-gate 			    "argument #1 must be an integer constant\n");
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		baseval = (int64_t)arg1->dn_value;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
11877c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
11887c478bd9Sstevel@tonic-gate 			    "argument #1 must be a 32-bit quantity\n");
11897c478bd9Sstevel@tonic-gate 		}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		if (arg2->dn_kind != DT_NODE_INT) {
11927c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
11937c478bd9Sstevel@tonic-gate 			    "argument #2 must be an integer constant\n");
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		limitval = (int64_t)arg2->dn_value;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
11997c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
12007c478bd9Sstevel@tonic-gate 			    "argument #2 must be a 32-bit quantity\n");
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 		if (limitval < baseval) {
12047c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_MISMATCH,
12057c478bd9Sstevel@tonic-gate 			    "lquantize( ) base (argument #1) must be less "
12067c478bd9Sstevel@tonic-gate 			    "than limit (argument #2)\n");
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		if (arg3 != NULL) {
12107c478bd9Sstevel@tonic-gate 			if (!dt_node_is_posconst(arg3)) {
12117c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
12127c478bd9Sstevel@tonic-gate 				    "argument #3 must be a non-zero positive "
12137c478bd9Sstevel@tonic-gate 				    "integer constant\n");
12147c478bd9Sstevel@tonic-gate 			}
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			if ((step = arg3->dn_value) > UINT16_MAX) {
12177c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
12187c478bd9Sstevel@tonic-gate 				    "argument #3 must be a 16-bit quantity\n");
12197c478bd9Sstevel@tonic-gate 			}
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		nlevels = (limitval - baseval) / step;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		if (nlevels == 0) {
12257c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPLARGE,
12267c478bd9Sstevel@tonic-gate 			    "lquantize( ) step (argument #3) too large: must "
12277c478bd9Sstevel@tonic-gate 			    "have at least one quantization level\n");
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		if (nlevels > UINT16_MAX) {
12317c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
12327c478bd9Sstevel@tonic-gate 			    "(argument #3) too small: number of quantization "
12337c478bd9Sstevel@tonic-gate 			    "levels must be a 16-bit quantity\n");
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 
1236a1b5e537Sbmc 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
12377c478bd9Sstevel@tonic-gate 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
12387c478bd9Sstevel@tonic-gate 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
12397c478bd9Sstevel@tonic-gate 		    DTRACE_LQUANTIZE_BASEMASK);
1240a1b5e537Sbmc 
124130ef842dSbmc 		assert(arg != 0);
124230ef842dSbmc 
124330ef842dSbmc 		isp = (dt_idsig_t *)aid->di_data;
124430ef842dSbmc 
124530ef842dSbmc 		if (isp->dis_auxinfo == 0) {
124630ef842dSbmc 			/*
124730ef842dSbmc 			 * This is the first time we've seen an lquantize()
124830ef842dSbmc 			 * for this aggregation; we'll store our argument
124930ef842dSbmc 			 * as the auxiliary signature information.
125030ef842dSbmc 			 */
125130ef842dSbmc 			isp->dis_auxinfo = arg;
125230ef842dSbmc 		} else if ((oarg = isp->dis_auxinfo) != arg) {
125330ef842dSbmc 			/*
125430ef842dSbmc 			 * If we have seen this lquantize() before and the
125530ef842dSbmc 			 * argument doesn't match the original argument, pick
125630ef842dSbmc 			 * the original argument apart to concisely report the
125730ef842dSbmc 			 * mismatch.
125830ef842dSbmc 			 */
125930ef842dSbmc 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
126030ef842dSbmc 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
126130ef842dSbmc 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
126230ef842dSbmc 
126330ef842dSbmc 			if (obaseval != baseval) {
126430ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
126530ef842dSbmc 				    "base (argument #1) doesn't match previous "
126630ef842dSbmc 				    "declaration: expected %d, found %d\n",
126730ef842dSbmc 				    obaseval, (int)baseval);
126830ef842dSbmc 			}
126930ef842dSbmc 
127030ef842dSbmc 			if (onlevels * ostep != nlevels * step) {
127130ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
127230ef842dSbmc 				    "limit (argument #2) doesn't match previous"
127330ef842dSbmc 				    " declaration: expected %d, found %d\n",
127430ef842dSbmc 				    obaseval + onlevels * ostep,
127530ef842dSbmc 				    (int)baseval + (int)nlevels * (int)step);
127630ef842dSbmc 			}
127730ef842dSbmc 
127830ef842dSbmc 			if (ostep != step) {
127930ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
128030ef842dSbmc 				    "step (argument #3) doesn't match previous "
128130ef842dSbmc 				    "declaration: expected %d, found %d\n",
128230ef842dSbmc 				    ostep, (int)step);
128330ef842dSbmc 			}
128430ef842dSbmc 
128530ef842dSbmc 			/*
128630ef842dSbmc 			 * We shouldn't be able to get here -- one of the
128730ef842dSbmc 			 * parameters must be mismatched if the arguments
128830ef842dSbmc 			 * didn't match.
128930ef842dSbmc 			 */
129030ef842dSbmc 			assert(0);
129130ef842dSbmc 		}
129230ef842dSbmc 
1293a1b5e537Sbmc 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1294a1b5e537Sbmc 		argmax = 5;
1295a1b5e537Sbmc 	}
1296a1b5e537Sbmc 
1297a1b5e537Sbmc 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1298a1b5e537Sbmc 		incr = dnp->dn_aggfun->dn_args->dn_list;
1299a1b5e537Sbmc 		argmax = 2;
1300a1b5e537Sbmc 	}
1301a1b5e537Sbmc 
1302a1b5e537Sbmc 	if (incr != NULL) {
1303a1b5e537Sbmc 		if (!dt_node_is_scalar(incr)) {
1304a1b5e537Sbmc 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1305a1b5e537Sbmc 			    "(argument #%d) must be of scalar type\n",
1306a1b5e537Sbmc 			    fid->di_name, argmax);
1307a1b5e537Sbmc 		}
1308a1b5e537Sbmc 
1309a1b5e537Sbmc 		if ((anp = incr->dn_list) != NULL) {
1310a1b5e537Sbmc 			int argc = argmax;
1311a1b5e537Sbmc 
1312a1b5e537Sbmc 			for (; anp != NULL; anp = anp->dn_list)
1313a1b5e537Sbmc 				argc++;
1314a1b5e537Sbmc 
1315a1b5e537Sbmc 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1316a1b5e537Sbmc 			    "mismatch: %d args passed, at most %d expected",
1317a1b5e537Sbmc 			    fid->di_name, argc, argmax);
1318a1b5e537Sbmc 		}
1319a1b5e537Sbmc 
1320a1b5e537Sbmc 		ap = dt_stmt_action(dtp, sdp);
1321a1b5e537Sbmc 		n++;
1322a1b5e537Sbmc 
1323a1b5e537Sbmc 		dt_cg(yypcb, incr);
1324a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
1325a1b5e537Sbmc 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1326a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1327a1b5e537Sbmc 	}
1328a1b5e537Sbmc 
1329a1b5e537Sbmc 	assert(sdp->dtsd_aggdata == NULL);
1330a1b5e537Sbmc 	sdp->dtsd_aggdata = aid;
1331a1b5e537Sbmc 
1332a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
1333a1b5e537Sbmc 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1334a1b5e537Sbmc 	assert(DTRACEACT_ISAGG(fid->di_id));
1335a1b5e537Sbmc 	ap->dtad_kind = fid->di_id;
1336a1b5e537Sbmc 	ap->dtad_ntuple = n;
1337a1b5e537Sbmc 	ap->dtad_arg = arg;
1338a1b5e537Sbmc 
1339a1b5e537Sbmc 	if (dnp->dn_aggfun->dn_args != NULL) {
1340a1b5e537Sbmc 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1341a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate static void
13467c478bd9Sstevel@tonic-gate dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
13497c478bd9Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
13507c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	yylineno = pnp->dn_line;
13537c478bd9Sstevel@tonic-gate 	dt_setcontext(dtp, pnp->dn_desc);
13547c478bd9Sstevel@tonic-gate 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (DT_TREEDUMP_PASS(dtp, 2))
13577c478bd9Sstevel@tonic-gate 		dt_node_printr(cnp, stderr, 0);
13587c478bd9Sstevel@tonic-gate 
13591a7c1b72Smws 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
13607c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == NULL);
13637c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = edp;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	if (cnp->dn_pred != NULL) {
13667c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, cnp->dn_pred);
13677c478bd9Sstevel@tonic-gate 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if (cnp->dn_acts == NULL) {
13717c478bd9Sstevel@tonic-gate 		dt_stmt_append(dt_stmt_create(dtp, edp,
13727c478bd9Sstevel@tonic-gate 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
13767c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == NULL);
13777c478bd9Sstevel@tonic-gate 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 		switch (dnp->dn_kind) {
13807c478bd9Sstevel@tonic-gate 		case DT_NODE_DEXPR:
13817c478bd9Sstevel@tonic-gate 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
13827c478bd9Sstevel@tonic-gate 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
13837c478bd9Sstevel@tonic-gate 			else
13847c478bd9Sstevel@tonic-gate 				dt_compile_exp(dtp, dnp, sdp);
13857c478bd9Sstevel@tonic-gate 			break;
13867c478bd9Sstevel@tonic-gate 		case DT_NODE_DFUNC:
13877c478bd9Sstevel@tonic-gate 			dt_compile_fun(dtp, dnp, sdp);
13887c478bd9Sstevel@tonic-gate 			break;
13897c478bd9Sstevel@tonic-gate 		case DT_NODE_AGG:
13907c478bd9Sstevel@tonic-gate 			dt_compile_agg(dtp, dnp, sdp);
13917c478bd9Sstevel@tonic-gate 			break;
13927c478bd9Sstevel@tonic-gate 		default:
13937c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
13947c478bd9Sstevel@tonic-gate 			    "%u is not a valid statement\n", dnp->dn_kind);
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == sdp);
13987c478bd9Sstevel@tonic-gate 		dt_stmt_append(sdp, dnp);
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == edp);
14021a7c1b72Smws 	dt_ecbdesc_release(dtp, edp);
14037c478bd9Sstevel@tonic-gate 	dt_endcontext(dtp);
14047c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = NULL;
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate static void
14087c478bd9Sstevel@tonic-gate dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate 	dt_node_t *pnp;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
14137c478bd9Sstevel@tonic-gate 		dt_compile_one_clause(dtp, cnp, pnp);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14161a7c1b72Smws static void
14171a7c1b72Smws dt_compile_xlator(dt_node_t *dnp)
14181a7c1b72Smws {
14191a7c1b72Smws 	dt_xlator_t *dxp = dnp->dn_xlator;
14201a7c1b72Smws 	dt_node_t *mnp;
14211a7c1b72Smws 
14221a7c1b72Smws 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
14231a7c1b72Smws 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
14241a7c1b72Smws 		dt_cg(yypcb, mnp);
14251a7c1b72Smws 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
14261a7c1b72Smws 	}
14271a7c1b72Smws }
14281a7c1b72Smws 
14297c478bd9Sstevel@tonic-gate void
14307c478bd9Sstevel@tonic-gate dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
14317c478bd9Sstevel@tonic-gate {
14327c478bd9Sstevel@tonic-gate 	const dtrace_pattr_t *pap;
14337c478bd9Sstevel@tonic-gate 	dt_probe_t *prp;
1434*c9d6cd77Sjhaslam 	dt_provider_t *pvp;
14357c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
14367c478bd9Sstevel@tonic-gate 	char attrstr[8];
14377c478bd9Sstevel@tonic-gate 	int err;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	/*
1440*c9d6cd77Sjhaslam 	 * Both kernel and pid based providers are allowed to have names
1441*c9d6cd77Sjhaslam 	 * ending with what could be interpreted as a number. We assume it's
1442*c9d6cd77Sjhaslam 	 * a pid and that we may need to dynamically create probes for
1443*c9d6cd77Sjhaslam 	 * that process if:
1444*c9d6cd77Sjhaslam 	 *
1445*c9d6cd77Sjhaslam 	 * (1) The provider doesn't exist, or,
1446*c9d6cd77Sjhaslam 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1447*c9d6cd77Sjhaslam 	 *
1448*c9d6cd77Sjhaslam 	 * On an error, dt_pid_create_probes() will set the error message
1449*c9d6cd77Sjhaslam 	 * and tag -- we just have to longjmp() out of here.
14507c478bd9Sstevel@tonic-gate 	 */
1451900524f3Sahl 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1452*c9d6cd77Sjhaslam 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1453*c9d6cd77Sjhaslam 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1454900524f3Sahl 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1455900524f3Sahl 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1456900524f3Sahl 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
14607c478bd9Sstevel@tonic-gate 	 * a representative probe is found, set 'pap' to the probe provider's
14617c478bd9Sstevel@tonic-gate 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
14627c478bd9Sstevel@tonic-gate 	 */
14637c478bd9Sstevel@tonic-gate 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
14647c478bd9Sstevel@tonic-gate 		pap = &_dtrace_prvdesc;
14657c478bd9Sstevel@tonic-gate 		err = dtrace_errno(dtp);
14667c478bd9Sstevel@tonic-gate 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
14677c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
14687c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
14697c478bd9Sstevel@tonic-gate 	} else {
14707c478bd9Sstevel@tonic-gate 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
14717c478bd9Sstevel@tonic-gate 		err = 0;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
14757c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
14767c478bd9Sstevel@tonic-gate 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
14777c478bd9Sstevel@tonic-gate 		    pdp->dtpd_func, pdp->dtpd_name);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
14817c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
14847c478bd9Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
14857c478bd9Sstevel@tonic-gate 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
14867c478bd9Sstevel@tonic-gate 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	/*
14897c478bd9Sstevel@tonic-gate 	 * Reset the stability attributes of D global variables that vary
14907c478bd9Sstevel@tonic-gate 	 * based on the attributes of the provider and context itself.
14917c478bd9Sstevel@tonic-gate 	 */
14927c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
14937c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_provider;
14947c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
14957c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_mod;
14967c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
14977c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_func;
14987c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
14997c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_name;
15007c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
15017c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_args;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = pdp;
15047c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = prp;
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * Reset context-dependent variables and state at the end of cooking a D probe
15097c478bd9Sstevel@tonic-gate  * definition clause.  This ensures that external declarations between clauses
15107c478bd9Sstevel@tonic-gate  * do not reference any stale context-dependent data from the previous clause.
15117c478bd9Sstevel@tonic-gate  */
15127c478bd9Sstevel@tonic-gate void
15137c478bd9Sstevel@tonic-gate dt_endcontext(dtrace_hdl_t *dtp)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate 	static const char *const cvars[] = {
15167c478bd9Sstevel@tonic-gate 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
15177c478bd9Sstevel@tonic-gate 	};
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
15207c478bd9Sstevel@tonic-gate 	int i;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	for (i = 0; cvars[i] != NULL; i++) {
15237c478bd9Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
15247c478bd9Sstevel@tonic-gate 			idp->di_attr = _dtrace_defattr;
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = NULL;
15287c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = NULL;
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate static int
15327c478bd9Sstevel@tonic-gate dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
15357c478bd9Sstevel@tonic-gate 		dt_idhash_delete(dhp, idp);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	return (0);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate /*
15417c478bd9Sstevel@tonic-gate  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
15427c478bd9Sstevel@tonic-gate  * any identifiers or translators that have been previously defined as bound to
15437c478bd9Sstevel@tonic-gate  * a version greater than the specified version.  Therefore, in our current
15447c478bd9Sstevel@tonic-gate  * version implementation, establishing a binding is a one-way transformation.
15457c478bd9Sstevel@tonic-gate  * In addition, no versioning is currently provided for types as our .d library
15467c478bd9Sstevel@tonic-gate  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
15477c478bd9Sstevel@tonic-gate  * for our exclusive use.  If required, type versioning will require more work.
15487c478bd9Sstevel@tonic-gate  */
15497c478bd9Sstevel@tonic-gate int
15507c478bd9Sstevel@tonic-gate dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	char s[DT_VERSION_STRMAX];
15537c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp, *nxp;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	if (v > dtp->dt_vmax)
15567c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
15577c478bd9Sstevel@tonic-gate 	else if (v == dtp->dt_vmax)
15587c478bd9Sstevel@tonic-gate 		return (0); /* no reduction necessary */
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	dt_dprintf("reducing api version to %s\n",
15617c478bd9Sstevel@tonic-gate 	    dt_version_num2str(v, s, sizeof (s)));
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	dtp->dt_vmax = v;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
15667c478bd9Sstevel@tonic-gate 		nxp = dt_list_next(dxp);
15677c478bd9Sstevel@tonic-gate 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
15687c478bd9Sstevel@tonic-gate 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
15697c478bd9Sstevel@tonic-gate 			dt_list_delete(&dtp->dt_xlators, dxp);
15707c478bd9Sstevel@tonic-gate 	}
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
15737c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
15747c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
15757c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	return (0);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate /*
15817c478bd9Sstevel@tonic-gate  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
15827c478bd9Sstevel@tonic-gate  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
15837c478bd9Sstevel@tonic-gate  * here to simplify the code by leveraging file descriptor inheritance.
15847c478bd9Sstevel@tonic-gate  */
15857c478bd9Sstevel@tonic-gate static FILE *
15867c478bd9Sstevel@tonic-gate dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	int argc = dtp->dt_cpp_argc;
15897c478bd9Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (argc + 5));
15907c478bd9Sstevel@tonic-gate 	FILE *ofp = tmpfile();
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
15937c478bd9Sstevel@tonic-gate 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	struct sigaction act, oact;
15967c478bd9Sstevel@tonic-gate 	sigset_t mask, omask;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	int wstat, estat;
15997c478bd9Sstevel@tonic-gate 	pid_t pid;
16007c478bd9Sstevel@tonic-gate 	off64_t off;
16017c478bd9Sstevel@tonic-gate 	int c;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (argv == NULL || ofp == NULL) {
16047c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, errno);
16057c478bd9Sstevel@tonic-gate 		goto err;
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	/*
16097c478bd9Sstevel@tonic-gate 	 * If the input is a seekable file, see if it is an interpreter file.
16107c478bd9Sstevel@tonic-gate 	 * If we see #!, seek past the first line because cpp will choke on it.
16117c478bd9Sstevel@tonic-gate 	 * We start cpp just prior to the \n at the end of this line so that
16127c478bd9Sstevel@tonic-gate 	 * it still sees the newline, ensuring that #line values are correct.
16137c478bd9Sstevel@tonic-gate 	 */
16147c478bd9Sstevel@tonic-gate 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
16157c478bd9Sstevel@tonic-gate 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
16167c478bd9Sstevel@tonic-gate 			for (off += 2; c != '\n'; off++) {
16177c478bd9Sstevel@tonic-gate 				if ((c = fgetc(ifp)) == EOF)
16187c478bd9Sstevel@tonic-gate 					break;
16197c478bd9Sstevel@tonic-gate 			}
16207c478bd9Sstevel@tonic-gate 			if (c == '\n')
16217c478bd9Sstevel@tonic-gate 				off--; /* start cpp just prior to \n */
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 		(void) fflush(ifp);
16247c478bd9Sstevel@tonic-gate 		(void) fseeko64(ifp, off, SEEK_SET);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
16287c478bd9Sstevel@tonic-gate 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	(void) snprintf(verdef, sizeof (verdef),
16337c478bd9Sstevel@tonic-gate 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
16347c478bd9Sstevel@tonic-gate 	argv[argc++] = verdef;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	switch (dtp->dt_stdcmode) {
16377c478bd9Sstevel@tonic-gate 	case DT_STDC_XA:
16387c478bd9Sstevel@tonic-gate 	case DT_STDC_XT:
16397c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=0";
16407c478bd9Sstevel@tonic-gate 		break;
16417c478bd9Sstevel@tonic-gate 	case DT_STDC_XC:
16427c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=1";
16437c478bd9Sstevel@tonic-gate 		break;
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	argv[argc++] = ipath;
16477c478bd9Sstevel@tonic-gate 	argv[argc++] = opath;
16487c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	/*
16517c478bd9Sstevel@tonic-gate 	 * libdtrace must be able to be embedded in other programs that may
16527c478bd9Sstevel@tonic-gate 	 * include application-specific signal handlers.  Therefore, if we
16537c478bd9Sstevel@tonic-gate 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
16547c478bd9Sstevel@tonic-gate 	 * that could confuse the containing application.  To do this,
16557c478bd9Sstevel@tonic-gate 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
16567c478bd9Sstevel@tonic-gate 	 * We restore our signal state once we are done.
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
16597c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
16607c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	bzero(&act, sizeof (act));
16637c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
16647c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, &oact);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
16677c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &oact, NULL);
16687c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16697c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CPPFORK);
16707c478bd9Sstevel@tonic-gate 		goto err;
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	if (pid == 0) {
16747c478bd9Sstevel@tonic-gate 		(void) execvp(dtp->dt_cpp_path, argv);
16757c478bd9Sstevel@tonic-gate 		_exit(errno == ENOENT ? 127 : 126);
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	do {
16797c478bd9Sstevel@tonic-gate 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
16807c478bd9Sstevel@tonic-gate 		    (int)pid);
16817c478bd9Sstevel@tonic-gate 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &oact, NULL);
16847c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
16877c478bd9Sstevel@tonic-gate 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	if (estat != 0) {
16907c478bd9Sstevel@tonic-gate 		switch (estat) {
16917c478bd9Sstevel@tonic-gate 		case 126:
16927c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
16937c478bd9Sstevel@tonic-gate 			break;
16947c478bd9Sstevel@tonic-gate 		case 127:
16957c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPENT);
16967c478bd9Sstevel@tonic-gate 			break;
16977c478bd9Sstevel@tonic-gate 		default:
16987c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPERR);
16997c478bd9Sstevel@tonic-gate 		}
17007c478bd9Sstevel@tonic-gate 		goto err;
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	free(argv);
17047c478bd9Sstevel@tonic-gate 	(void) fflush(ofp);
17057c478bd9Sstevel@tonic-gate 	(void) fseek(ofp, 0, SEEK_SET);
17067c478bd9Sstevel@tonic-gate 	return (ofp);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate err:
17097c478bd9Sstevel@tonic-gate 	free(argv);
17107c478bd9Sstevel@tonic-gate 	(void) fclose(ofp);
17117c478bd9Sstevel@tonic-gate 	return (NULL);
17127c478bd9Sstevel@tonic-gate }
17137c478bd9Sstevel@tonic-gate 
1714*c9d6cd77Sjhaslam static void
1715*c9d6cd77Sjhaslam dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1716*c9d6cd77Sjhaslam {
1717*c9d6cd77Sjhaslam 	va_list ap;
1718*c9d6cd77Sjhaslam 
1719*c9d6cd77Sjhaslam 	va_start(ap, format);
1720*c9d6cd77Sjhaslam 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1721*c9d6cd77Sjhaslam 	va_end(ap);
1722*c9d6cd77Sjhaslam }
1723*c9d6cd77Sjhaslam 
1724*c9d6cd77Sjhaslam int
1725*c9d6cd77Sjhaslam dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1726*c9d6cd77Sjhaslam {
1727*c9d6cd77Sjhaslam 	dt_lib_depend_t *dld;
1728*c9d6cd77Sjhaslam 	const char *end;
1729*c9d6cd77Sjhaslam 
1730*c9d6cd77Sjhaslam 	assert(arg != NULL);
1731*c9d6cd77Sjhaslam 
1732*c9d6cd77Sjhaslam 	if ((end = strrchr(arg, '/')) == NULL)
1733*c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EINVAL));
1734*c9d6cd77Sjhaslam 
1735*c9d6cd77Sjhaslam 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1736*c9d6cd77Sjhaslam 		return (-1);
1737*c9d6cd77Sjhaslam 
1738*c9d6cd77Sjhaslam 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
1739*c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1740*c9d6cd77Sjhaslam 		return (-1);
1741*c9d6cd77Sjhaslam 	}
1742*c9d6cd77Sjhaslam 
1743*c9d6cd77Sjhaslam 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
1744*c9d6cd77Sjhaslam 	if ((dld->dtld_library = strdup(arg)) == NULL) {
1745*c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
1746*c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1747*c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
1748*c9d6cd77Sjhaslam 	}
1749*c9d6cd77Sjhaslam 
1750*c9d6cd77Sjhaslam 	dt_list_append(dlp, dld);
1751*c9d6cd77Sjhaslam 	return (0);
1752*c9d6cd77Sjhaslam }
1753*c9d6cd77Sjhaslam 
1754*c9d6cd77Sjhaslam dt_lib_depend_t *
1755*c9d6cd77Sjhaslam dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1756*c9d6cd77Sjhaslam {
1757*c9d6cd77Sjhaslam 	dt_lib_depend_t *dldn;
1758*c9d6cd77Sjhaslam 
1759*c9d6cd77Sjhaslam 	for (dldn = dt_list_next(dld); dldn != NULL;
1760*c9d6cd77Sjhaslam 	    dldn = dt_list_next(dldn)) {
1761*c9d6cd77Sjhaslam 		if (strcmp(dldn->dtld_library, arg) == 0)
1762*c9d6cd77Sjhaslam 			return (dldn);
1763*c9d6cd77Sjhaslam 	}
1764*c9d6cd77Sjhaslam 
1765*c9d6cd77Sjhaslam 	return (NULL);
1766*c9d6cd77Sjhaslam }
1767*c9d6cd77Sjhaslam 
1768*c9d6cd77Sjhaslam /*
1769*c9d6cd77Sjhaslam  * Go through all the library files, and, if any library dependencies exist for
1770*c9d6cd77Sjhaslam  * that file, add it to that node's list of dependents. The result of this
1771*c9d6cd77Sjhaslam  * will be a graph which can then be topologically sorted to produce a
1772*c9d6cd77Sjhaslam  * compilation order.
1773*c9d6cd77Sjhaslam  */
1774*c9d6cd77Sjhaslam static int
1775*c9d6cd77Sjhaslam dt_lib_build_graph(dtrace_hdl_t *dtp)
1776*c9d6cd77Sjhaslam {
1777*c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld;
1778*c9d6cd77Sjhaslam 
1779*c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1780*c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1781*c9d6cd77Sjhaslam 		char *library = dld->dtld_library;
1782*c9d6cd77Sjhaslam 
1783*c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
1784*c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
1785*c9d6cd77Sjhaslam 			dt_lib_depend_t *dlda;
1786*c9d6cd77Sjhaslam 
1787*c9d6cd77Sjhaslam 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
1788*c9d6cd77Sjhaslam 			    dpld->dtld_library)) == NULL) {
1789*c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
1790*c9d6cd77Sjhaslam 				    "Invalid library dependency in %s: %s\n",
1791*c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
1792*c9d6cd77Sjhaslam 
1793*c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
1794*c9d6cd77Sjhaslam 			}
1795*c9d6cd77Sjhaslam 
1796*c9d6cd77Sjhaslam 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
1797*c9d6cd77Sjhaslam 			    library)) != 0) {
1798*c9d6cd77Sjhaslam 				return (-1); /* preserve dt_errno */
1799*c9d6cd77Sjhaslam 			}
1800*c9d6cd77Sjhaslam 		}
1801*c9d6cd77Sjhaslam 	}
1802*c9d6cd77Sjhaslam 	return (0);
1803*c9d6cd77Sjhaslam }
1804*c9d6cd77Sjhaslam 
1805*c9d6cd77Sjhaslam static int
1806*c9d6cd77Sjhaslam dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
1807*c9d6cd77Sjhaslam {
1808*c9d6cd77Sjhaslam 	dt_lib_depend_t *dpld, *dlda, *new;
1809*c9d6cd77Sjhaslam 
1810*c9d6cd77Sjhaslam 	dld->dtld_start = ++(*count);
1811*c9d6cd77Sjhaslam 
1812*c9d6cd77Sjhaslam 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
1813*c9d6cd77Sjhaslam 	    dpld = dt_list_next(dpld)) {
1814*c9d6cd77Sjhaslam 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
1815*c9d6cd77Sjhaslam 		    dpld->dtld_library);
1816*c9d6cd77Sjhaslam 		assert(dlda != NULL);
1817*c9d6cd77Sjhaslam 
1818*c9d6cd77Sjhaslam 		if (dlda->dtld_start == 0 &&
1819*c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dlda, count) == -1)
1820*c9d6cd77Sjhaslam 			return (-1);
1821*c9d6cd77Sjhaslam 	}
1822*c9d6cd77Sjhaslam 
1823*c9d6cd77Sjhaslam 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1824*c9d6cd77Sjhaslam 		return (-1);
1825*c9d6cd77Sjhaslam 
1826*c9d6cd77Sjhaslam 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
1827*c9d6cd77Sjhaslam 		dt_free(dtp, new);
1828*c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
1829*c9d6cd77Sjhaslam 	}
1830*c9d6cd77Sjhaslam 
1831*c9d6cd77Sjhaslam 	new->dtld_start = dld->dtld_start;
1832*c9d6cd77Sjhaslam 	new->dtld_finish = dld->dtld_finish = ++(*count);
1833*c9d6cd77Sjhaslam 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
1834*c9d6cd77Sjhaslam 
1835*c9d6cd77Sjhaslam 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
1836*c9d6cd77Sjhaslam 	    new->dtld_start, new->dtld_finish);
1837*c9d6cd77Sjhaslam 
1838*c9d6cd77Sjhaslam 	return (0);
1839*c9d6cd77Sjhaslam }
1840*c9d6cd77Sjhaslam 
1841*c9d6cd77Sjhaslam static int
1842*c9d6cd77Sjhaslam dt_lib_depend_sort(dtrace_hdl_t *dtp)
1843*c9d6cd77Sjhaslam {
1844*c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld, *dlda;
1845*c9d6cd77Sjhaslam 	int count = 0;
1846*c9d6cd77Sjhaslam 
1847*c9d6cd77Sjhaslam 	if (dt_lib_build_graph(dtp) == -1)
1848*c9d6cd77Sjhaslam 		return (-1); /* preserve dt_errno */
1849*c9d6cd77Sjhaslam 
1850*c9d6cd77Sjhaslam 	/*
1851*c9d6cd77Sjhaslam 	 * Perform a topological sort of the graph that hangs off
1852*c9d6cd77Sjhaslam 	 * dtp->dt_lib_dep. The result of this process will be a
1853*c9d6cd77Sjhaslam 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
1854*c9d6cd77Sjhaslam 	 */
1855*c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1856*c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1857*c9d6cd77Sjhaslam 		if (dld->dtld_start == 0 &&
1858*c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dld, &count) == -1)
1859*c9d6cd77Sjhaslam 			return (-1); /* preserve dt_errno */;
1860*c9d6cd77Sjhaslam 	}
1861*c9d6cd77Sjhaslam 
1862*c9d6cd77Sjhaslam 	/*
1863*c9d6cd77Sjhaslam 	 * Check the graph for cycles. If an ancestor's finishing time is
1864*c9d6cd77Sjhaslam 	 * less than any of its dependent's finishing times then a back edge
1865*c9d6cd77Sjhaslam 	 * exists in the graph and this is a cycle.
1866*c9d6cd77Sjhaslam 	 */
1867*c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1868*c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1869*c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
1870*c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
1871*c9d6cd77Sjhaslam 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
1872*c9d6cd77Sjhaslam 			    dpld->dtld_library);
1873*c9d6cd77Sjhaslam 			assert(dlda != NULL);
1874*c9d6cd77Sjhaslam 
1875*c9d6cd77Sjhaslam 			if (dlda->dtld_finish > dld->dtld_finish) {
1876*c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
1877*c9d6cd77Sjhaslam 				    "Cyclic dependency detected: %s => %s\n",
1878*c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
1879*c9d6cd77Sjhaslam 
1880*c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
1881*c9d6cd77Sjhaslam 			}
1882*c9d6cd77Sjhaslam 		}
1883*c9d6cd77Sjhaslam 	}
1884*c9d6cd77Sjhaslam 
1885*c9d6cd77Sjhaslam 	return (0);
1886*c9d6cd77Sjhaslam }
1887*c9d6cd77Sjhaslam 
1888*c9d6cd77Sjhaslam static void
1889*c9d6cd77Sjhaslam dt_lib_depend_free(dtrace_hdl_t *dtp)
1890*c9d6cd77Sjhaslam {
1891*c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dlda;
1892*c9d6cd77Sjhaslam 
1893*c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
1894*c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
1895*c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependencies, dlda);
1896*c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
1897*c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
1898*c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
1899*c9d6cd77Sjhaslam 		}
1900*c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
1901*c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependents, dlda);
1902*c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
1903*c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
1904*c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
1905*c9d6cd77Sjhaslam 		}
1906*c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep, dld);
1907*c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
1908*c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
1909*c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1910*c9d6cd77Sjhaslam 	}
1911*c9d6cd77Sjhaslam 
1912*c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
1913*c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
1914*c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
1915*c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1916*c9d6cd77Sjhaslam 	}
1917*c9d6cd77Sjhaslam }
1918*c9d6cd77Sjhaslam 
1919*c9d6cd77Sjhaslam 
19207c478bd9Sstevel@tonic-gate /*
1921*c9d6cd77Sjhaslam  * Open all of the .d library files found in the specified directory and
1922*c9d6cd77Sjhaslam  * compile each one in topological order to cache its inlines and translators,
1923*c9d6cd77Sjhaslam  * etc.  We silently ignore any missing directories and other files found
1924*c9d6cd77Sjhaslam  * therein. We only fail (and thereby fail dt_load_libs()) if we fail to
1925*c9d6cd77Sjhaslam  * compile a library and the error is something other than #pragma D depends_on.
19267c478bd9Sstevel@tonic-gate  * Dependency errors are silently ignored to permit a library directory to
19277c478bd9Sstevel@tonic-gate  * contain libraries which may not be accessible depending on our privileges.
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate static int
19307c478bd9Sstevel@tonic-gate dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
19317c478bd9Sstevel@tonic-gate {
19324bc0a2efScasper 	struct dirent *dp;
19337c478bd9Sstevel@tonic-gate 	const char *p;
19347c478bd9Sstevel@tonic-gate 	DIR *dirp;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	char fname[PATH_MAX];
19377c478bd9Sstevel@tonic-gate 	dtrace_prog_t *pgp;
19387c478bd9Sstevel@tonic-gate 	FILE *fp;
1939*c9d6cd77Sjhaslam 	void *rv;
1940*c9d6cd77Sjhaslam 	dt_lib_depend_t *dld;
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL) {
19437c478bd9Sstevel@tonic-gate 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
19447c478bd9Sstevel@tonic-gate 		return (0);
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 
1947*c9d6cd77Sjhaslam 	/* First, parse each file for library dependencies. */
19484bc0a2efScasper 	while ((dp = readdir(dirp)) != NULL) {
19497c478bd9Sstevel@tonic-gate 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
19507c478bd9Sstevel@tonic-gate 			continue; /* skip any filename not ending in .d */
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		(void) snprintf(fname, sizeof (fname),
19537c478bd9Sstevel@tonic-gate 		    "%s/%s", path, dp->d_name);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 		if ((fp = fopen(fname, "r")) == NULL) {
19567c478bd9Sstevel@tonic-gate 			dt_dprintf("skipping library %s: %s\n",
19577c478bd9Sstevel@tonic-gate 			    fname, strerror(errno));
19587c478bd9Sstevel@tonic-gate 			continue;
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = fname;
1962*c9d6cd77Sjhaslam 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
1963*c9d6cd77Sjhaslam 			goto err;
1964*c9d6cd77Sjhaslam 
1965*c9d6cd77Sjhaslam 		rv = dt_compile(dtp, DT_CTX_DPROG,
1966*c9d6cd77Sjhaslam 		    DTRACE_PROBESPEC_NAME, NULL,
1967*c9d6cd77Sjhaslam 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
1968*c9d6cd77Sjhaslam 
1969*c9d6cd77Sjhaslam 		if (rv != NULL && dtp->dt_errno &&
1970*c9d6cd77Sjhaslam 		    (dtp->dt_errno != EDT_COMPILER ||
1971*c9d6cd77Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
1972*c9d6cd77Sjhaslam 			goto err;
1973*c9d6cd77Sjhaslam 
1974*c9d6cd77Sjhaslam 		if (dtp->dt_errno)
1975*c9d6cd77Sjhaslam 			dt_dprintf("error parsing library %s: %s\n",
1976*c9d6cd77Sjhaslam 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
1977*c9d6cd77Sjhaslam 
1978*c9d6cd77Sjhaslam 		(void) fclose(fp);
1979*c9d6cd77Sjhaslam 		dtp->dt_filetag = NULL;
1980*c9d6cd77Sjhaslam 	}
1981*c9d6cd77Sjhaslam 
1982*c9d6cd77Sjhaslam 	(void) closedir(dirp);
1983*c9d6cd77Sjhaslam 	/*
1984*c9d6cd77Sjhaslam 	 * Finish building the graph containing the library dependencies
1985*c9d6cd77Sjhaslam 	 * and perform a topological sort to generate an ordered list
1986*c9d6cd77Sjhaslam 	 * for compilation.
1987*c9d6cd77Sjhaslam 	 */
1988*c9d6cd77Sjhaslam 	if (dt_lib_depend_sort(dtp) == -1)
1989*c9d6cd77Sjhaslam 		goto err;
1990*c9d6cd77Sjhaslam 
1991*c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
1992*c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1993*c9d6cd77Sjhaslam 
1994*c9d6cd77Sjhaslam 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
1995*c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
1996*c9d6cd77Sjhaslam 			    dld->dtld_library, strerror(errno));
1997*c9d6cd77Sjhaslam 			continue;
1998*c9d6cd77Sjhaslam 		}
1999*c9d6cd77Sjhaslam 
2000*c9d6cd77Sjhaslam 		dtp->dt_filetag = dld->dtld_library;
20017c478bd9Sstevel@tonic-gate 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
20027c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
20037c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = NULL;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2006*c9d6cd77Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2007*c9d6cd77Sjhaslam 			goto err;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		if (pgp == NULL) {
2010*c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
2011*c9d6cd77Sjhaslam 			    dld->dtld_library,
20127c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
20137c478bd9Sstevel@tonic-gate 		} else
20141a7c1b72Smws 			dt_program_destroy(dtp, pgp);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
2017*c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
20187c478bd9Sstevel@tonic-gate 	return (0);
2019*c9d6cd77Sjhaslam 
2020*c9d6cd77Sjhaslam err:
2021*c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
2022*c9d6cd77Sjhaslam 	return (-1); /* preserve dt_errno */
20237c478bd9Sstevel@tonic-gate }
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate /*
20267c478bd9Sstevel@tonic-gate  * Load the contents of any appropriate DTrace .d library files.  These files
20277c478bd9Sstevel@tonic-gate  * contain inlines and translators that will be cached by the compiler.  We
20287c478bd9Sstevel@tonic-gate  * defer this activity until the first compile to permit libdtrace clients to
20297c478bd9Sstevel@tonic-gate  * add their own library directories and so that we can properly report errors.
20307c478bd9Sstevel@tonic-gate  */
20317c478bd9Sstevel@tonic-gate static int
20327c478bd9Sstevel@tonic-gate dt_load_libs(dtrace_hdl_t *dtp)
20337c478bd9Sstevel@tonic-gate {
20347c478bd9Sstevel@tonic-gate 	dt_dirpath_t *dirp;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
20377c478bd9Sstevel@tonic-gate 		return (0); /* libraries already processed */
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	for (dirp = dt_list_next(&dtp->dt_lib_path);
20427c478bd9Sstevel@tonic-gate 	    dirp != NULL; dirp = dt_list_next(dirp)) {
20437c478bd9Sstevel@tonic-gate 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
20447c478bd9Sstevel@tonic-gate 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
20457c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
20467c478bd9Sstevel@tonic-gate 		}
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	return (0);
20507c478bd9Sstevel@tonic-gate }
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate static void *
20537c478bd9Sstevel@tonic-gate dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
20547c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
20557c478bd9Sstevel@tonic-gate {
20567c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
20577c478bd9Sstevel@tonic-gate 	dt_decl_t *ddp;
20587c478bd9Sstevel@tonic-gate 	dt_pcb_t pcb;
20597c478bd9Sstevel@tonic-gate 	void *rv;
20607c478bd9Sstevel@tonic-gate 	int err;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
20637c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
20647c478bd9Sstevel@tonic-gate 		return (NULL);
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
20687c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
20717c478bd9Sstevel@tonic-gate 	(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
20747c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
20777c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	dt_pcb_push(dtp, &pcb);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	pcb.pcb_fileptr = fp;
20827c478bd9Sstevel@tonic-gate 	pcb.pcb_string = s;
20837c478bd9Sstevel@tonic-gate 	pcb.pcb_strptr = s;
20847c478bd9Sstevel@tonic-gate 	pcb.pcb_strlen = s ? strlen(s) : 0;
20857c478bd9Sstevel@tonic-gate 	pcb.pcb_sargc = argc;
20867c478bd9Sstevel@tonic-gate 	pcb.pcb_sargv = argv;
20877c478bd9Sstevel@tonic-gate 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
20887c478bd9Sstevel@tonic-gate 	pcb.pcb_pspec = pspec;
20897c478bd9Sstevel@tonic-gate 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
20907c478bd9Sstevel@tonic-gate 	pcb.pcb_amin = dtp->dt_amin;
20917c478bd9Sstevel@tonic-gate 	pcb.pcb_yystate = -1;
20927c478bd9Sstevel@tonic-gate 	pcb.pcb_context = context;
20937c478bd9Sstevel@tonic-gate 	pcb.pcb_token = context;
20947c478bd9Sstevel@tonic-gate 
2095*c9d6cd77Sjhaslam 	if (context != DT_CTX_DPROG)
20967c478bd9Sstevel@tonic-gate 		yybegin(YYS_EXPR);
2097*c9d6cd77Sjhaslam 	else if (cflags & DTRACE_C_CTL)
2098*c9d6cd77Sjhaslam 		yybegin(YYS_CONTROL);
2099*c9d6cd77Sjhaslam 	else
2100*c9d6cd77Sjhaslam 		yybegin(YYS_CLAUSE);
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
21037c478bd9Sstevel@tonic-gate 		goto out;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
21067c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
21097c478bd9Sstevel@tonic-gate 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
21107c478bd9Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
21137c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	/*
21167c478bd9Sstevel@tonic-gate 	 * Invoke the parser to evaluate the D source code.  If any errors
21177c478bd9Sstevel@tonic-gate 	 * occur during parsing, an error function will be called and we
21187c478bd9Sstevel@tonic-gate 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
21197c478bd9Sstevel@tonic-gate 	 * we optionally display the parse tree if debugging is enabled.
21207c478bd9Sstevel@tonic-gate 	 */
21217c478bd9Sstevel@tonic-gate 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
21227c478bd9Sstevel@tonic-gate 		xyerror(D_EMPTY, "empty D program translation unit\n");
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	yybegin(YYS_DONE);
21257c478bd9Sstevel@tonic-gate 
2126*c9d6cd77Sjhaslam 	if (cflags & DTRACE_C_CTL)
2127*c9d6cd77Sjhaslam 		goto out;
2128*c9d6cd77Sjhaslam 
21297c478bd9Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
21307c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pragmas != NULL)
21337c478bd9Sstevel@tonic-gate 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
21367c478bd9Sstevel@tonic-gate 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
21377c478bd9Sstevel@tonic-gate 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
21387c478bd9Sstevel@tonic-gate 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	/*
21427c478bd9Sstevel@tonic-gate 	 * If we have successfully created a parse tree for a D program, loop
21437c478bd9Sstevel@tonic-gate 	 * over the clauses and actions and instantiate the corresponding
21447c478bd9Sstevel@tonic-gate 	 * libdtrace program.  If we are parsing a D expression, then we
21457c478bd9Sstevel@tonic-gate 	 * simply run the code generator and assembler on the resulting tree.
21467c478bd9Sstevel@tonic-gate 	 */
21477c478bd9Sstevel@tonic-gate 	switch (context) {
21487c478bd9Sstevel@tonic-gate 	case DT_CTX_DPROG:
21497c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
21527c478bd9Sstevel@tonic-gate 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
21537c478bd9Sstevel@tonic-gate 			xyerror(D_EMPTY, "empty D program translation unit\n");
21547c478bd9Sstevel@tonic-gate 
21551a7c1b72Smws 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
21567c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 		for (; dnp != NULL; dnp = dnp->dn_list) {
21597c478bd9Sstevel@tonic-gate 			switch (dnp->dn_kind) {
21607c478bd9Sstevel@tonic-gate 			case DT_NODE_CLAUSE:
21617c478bd9Sstevel@tonic-gate 				dt_compile_clause(dtp, dnp);
21627c478bd9Sstevel@tonic-gate 				break;
21631a7c1b72Smws 			case DT_NODE_XLATOR:
21641a7c1b72Smws 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
21651a7c1b72Smws 					dt_compile_xlator(dnp);
21661a7c1b72Smws 				break;
21677c478bd9Sstevel@tonic-gate 			case DT_NODE_PROVIDER:
21687c478bd9Sstevel@tonic-gate 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
21697c478bd9Sstevel@tonic-gate 				break;
21707c478bd9Sstevel@tonic-gate 			}
21717c478bd9Sstevel@tonic-gate 		}
21727c478bd9Sstevel@tonic-gate 
21731a7c1b72Smws 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
21741a7c1b72Smws 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
21751a7c1b72Smws 		yypcb->pcb_asxrefs = NULL;
21761a7c1b72Smws 		yypcb->pcb_asxreflen = 0;
21771a7c1b72Smws 
21781a7c1b72Smws 		rv = yypcb->pcb_prog;
21797c478bd9Sstevel@tonic-gate 		break;
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	case DT_CTX_DEXPR:
21827c478bd9Sstevel@tonic-gate 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
21837c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, yypcb->pcb_root);
21847c478bd9Sstevel@tonic-gate 		rv = dt_as(yypcb);
21857c478bd9Sstevel@tonic-gate 		break;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	case DT_CTX_DTYPE:
21887c478bd9Sstevel@tonic-gate 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
21897c478bd9Sstevel@tonic-gate 		err = dt_decl_type(ddp, arg);
21907c478bd9Sstevel@tonic-gate 		dt_decl_free(ddp);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 		if (err != 0)
21937c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		rv = NULL;
21967c478bd9Sstevel@tonic-gate 		break;
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate out:
22007c478bd9Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
22017c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
22047c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
22057c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
22067c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
22097c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
22107c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
22117c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
22147c478bd9Sstevel@tonic-gate 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	dt_pcb_pop(dtp, err);
22177c478bd9Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
22187c478bd9Sstevel@tonic-gate 	return (err ? NULL : rv);
22197c478bd9Sstevel@tonic-gate }
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate dtrace_prog_t *
22227c478bd9Sstevel@tonic-gate dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
22237c478bd9Sstevel@tonic-gate     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
22247c478bd9Sstevel@tonic-gate {
22257c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
22267c478bd9Sstevel@tonic-gate 	    spec, NULL, cflags, argc, argv, NULL, s));
22277c478bd9Sstevel@tonic-gate }
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate dtrace_prog_t *
22307c478bd9Sstevel@tonic-gate dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
22317c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[])
22327c478bd9Sstevel@tonic-gate {
22337c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
22347c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
22357c478bd9Sstevel@tonic-gate }
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate int
22387c478bd9Sstevel@tonic-gate dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
22397c478bd9Sstevel@tonic-gate {
22407c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
22417c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
22427c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate int
22467c478bd9Sstevel@tonic-gate dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
22497c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
22507c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
22517c478bd9Sstevel@tonic-gate }
2252