xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_cc.c (revision e5803b76927480e8f9b67b22201c484ccf4c2bcf)
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
5c9d6cd77Sjhaslam  * Common Development and Distribution License (the "License").
6c9d6cd77Sjhaslam  * 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 /*
23c9a6ea2eSBryan Cantrill  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
245eb667acSRobert Mustacchi  * Copyright (c) 2011, Joyent Inc. All rights reserved.
25*e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * DTrace D Language Compiler
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * The code in this source file implements the main engine for the D language
327c478bd9Sstevel@tonic-gate  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
337c478bd9Sstevel@tonic-gate  * compiler operates on either stdio FILEs or in-memory strings as its input
347c478bd9Sstevel@tonic-gate  * and can produce either dtrace_prog_t structures from a D program or a single
357c478bd9Sstevel@tonic-gate  * dtrace_difo_t structure from a D expression.  Multiple entry points are
367c478bd9Sstevel@tonic-gate  * provided as wrappers around dt_compile() for the various input/output pairs.
377c478bd9Sstevel@tonic-gate  * The compiler itself is implemented across the following source files:
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * dt_lex.l - lex scanner
407c478bd9Sstevel@tonic-gate  * dt_grammar.y - yacc grammar
417c478bd9Sstevel@tonic-gate  * dt_parser.c - parse tree creation and semantic checking
427c478bd9Sstevel@tonic-gate  * dt_decl.c - declaration stack processing
437c478bd9Sstevel@tonic-gate  * dt_xlator.c - D translator lookup and creation
447c478bd9Sstevel@tonic-gate  * dt_ident.c - identifier and symbol table routines
457c478bd9Sstevel@tonic-gate  * dt_pragma.c - #pragma processing and D pragmas
467c478bd9Sstevel@tonic-gate  * dt_printf.c - D printf() and printa() argument checking and processing
477c478bd9Sstevel@tonic-gate  * dt_cc.c - compiler driver and dtrace_prog_t construction
487c478bd9Sstevel@tonic-gate  * dt_cg.c - DIF code generator
497c478bd9Sstevel@tonic-gate  * dt_as.c - DIF assembler
507c478bd9Sstevel@tonic-gate  * dt_dof.c - dtrace_prog_t -> DOF conversion
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Several other source files provide collections of utility routines used by
537c478bd9Sstevel@tonic-gate  * these major files.  The compiler itself is implemented in multiple passes:
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
567c478bd9Sstevel@tonic-gate  *     and parse tree nodes are constructed using the routines in dt_parser.c.
577c478bd9Sstevel@tonic-gate  *     This node construction pass is described further in dt_parser.c.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * (2) The parse tree is "cooked" by assigning each clause a context (see the
607c478bd9Sstevel@tonic-gate  *     routine dt_setcontext(), below) based on its probe description and then
617c478bd9Sstevel@tonic-gate  *     recursively descending the tree performing semantic checking.  The cook
627c478bd9Sstevel@tonic-gate  *     routines are also implemented in dt_parser.c and described there.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * (3) For actions that are DIF expression statements, the DIF code generator
657c478bd9Sstevel@tonic-gate  *     and assembler are invoked to create a finished DIFO for the statement.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * (4) The dtrace_prog_t data structures for the program clauses and actions
687c478bd9Sstevel@tonic-gate  *     are built, containing pointers to any DIFOs created in step (3).
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
717c478bd9Sstevel@tonic-gate  *     into DOF format for use in anonymous tracing or enabling in the kernel.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * In the implementation, steps 2-4 are intertwined in that they are performed
747c478bd9Sstevel@tonic-gate  * in order for each clause as part of a loop that executes over the clauses.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * The D compiler currently implements nearly no optimization.  The compiler
777c478bd9Sstevel@tonic-gate  * implements integer constant folding as part of pass (1), and a set of very
787c478bd9Sstevel@tonic-gate  * simple peephole optimizations as part of pass (3).  As with any C compiler,
797c478bd9Sstevel@tonic-gate  * a large number of optimizations are possible on both the intermediate data
807c478bd9Sstevel@tonic-gate  * structures and the generated DIF code.  These possibilities should be
817c478bd9Sstevel@tonic-gate  * investigated in the context of whether they will have any substantive effect
827c478bd9Sstevel@tonic-gate  * on the overall DTrace probe effect before they are undertaken.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #include <sys/types.h>
867c478bd9Sstevel@tonic-gate #include <sys/wait.h>
872b6389efSBryan Cantrill #include <sys/sysmacros.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 
118c9d6cd77Sjhaslam static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
119c9d6cd77Sjhaslam     uint_t, int, char *const[], FILE *, const char *);
120c9d6cd77Sjhaslam 
121c9d6cd77Sjhaslam 
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);
666*e5803b76SAdam H. Leventhal 	boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
667*e5803b76SAdam H. Leventhal 	const char *act = istrace ?  "trace" : "print";
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (dt_node_is_void(dnp->dn_args)) {
670*e5803b76SAdam H. Leventhal 		dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
671*e5803b76SAdam H. Leventhal 		    "%s( ) may not be applied to a void expression\n", act);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
674*e5803b76SAdam H. Leventhal 	if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
675*e5803b76SAdam H. Leventhal 		dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
676*e5803b76SAdam H. Leventhal 		    "%s( ) may not be applied to a translated pointer\n", act);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
6807c478bd9Sstevel@tonic-gate 
681*e5803b76SAdam H. Leventhal 	/*
682*e5803b76SAdam H. Leventhal 	 * The print() action behaves identically to trace(), except that it
683*e5803b76SAdam H. Leventhal 	 * stores the CTF type of the argument (if present) within the DOF for
684*e5803b76SAdam H. Leventhal 	 * the DIFEXPR action.  To do this, we set the 'dtsd_strdata' to point
685*e5803b76SAdam H. Leventhal 	 * to the fully-qualified CTF type ID for the result of the DIF
686*e5803b76SAdam H. Leventhal 	 * action.  We use the ID instead of the name to handles complex types
687*e5803b76SAdam H. Leventhal 	 * like arrays and function pointers that can't be resolved by
688*e5803b76SAdam H. Leventhal 	 * ctf_type_lookup().  This is later processed by dtrace_dof_create()
689*e5803b76SAdam H. Leventhal 	 * and turned into a reference into the string table so that we can
690*e5803b76SAdam H. Leventhal 	 * get the type information when we process the data after the fact.
691*e5803b76SAdam H. Leventhal 	 */
692*e5803b76SAdam H. Leventhal 	if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
693*e5803b76SAdam H. Leventhal 		dt_node_t *dret;
694*e5803b76SAdam H. Leventhal 		size_t n;
695*e5803b76SAdam H. Leventhal 		dt_module_t *dmp;
696deef35fdSEric Schrock 
697*e5803b76SAdam H. Leventhal 		dret = yypcb->pcb_dret;
698*e5803b76SAdam H. Leventhal 		dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
699deef35fdSEric Schrock 
700*e5803b76SAdam H. Leventhal 		n = snprintf(NULL, 0, "%s`%d", dmp->dm_name, dret->dn_type) + 1;
701*e5803b76SAdam H. Leventhal 		sdp->dtsd_strdata = dt_alloc(dtp, n);
702*e5803b76SAdam H. Leventhal 		if (sdp->dtsd_strdata == NULL)
703*e5803b76SAdam H. Leventhal 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
704*e5803b76SAdam H. Leventhal 		(void) snprintf(sdp->dtsd_strdata, n, "%s`%d", dmp->dm_name,
705*e5803b76SAdam H. Leventhal 		    dret->dn_type);
706deef35fdSEric Schrock 	}
707deef35fdSEric Schrock 
708deef35fdSEric Schrock 	ap->dtad_difo = dt_as(yypcb);
709deef35fdSEric Schrock 	ap->dtad_kind = DTRACEACT_DIFEXPR;
710deef35fdSEric Schrock }
711deef35fdSEric Schrock 
7127c478bd9Sstevel@tonic-gate static void
7137c478bd9Sstevel@tonic-gate dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	dt_node_t *addr = dnp->dn_args;
7181ea5f93dSBryan Cantrill 	dt_node_t *max = dnp->dn_args->dn_list;
7191ea5f93dSBryan Cantrill 	dt_node_t *size;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
7247c478bd9Sstevel@tonic-gate 		dnerror(addr, D_TRACEMEM_ADDR,
7257c478bd9Sstevel@tonic-gate 		    "tracemem( ) argument #1 is incompatible with "
7267c478bd9Sstevel@tonic-gate 		    "prototype:\n\tprototype: pointer or integer\n"
7277c478bd9Sstevel@tonic-gate 		    "\t argument: %s\n",
7287c478bd9Sstevel@tonic-gate 		    dt_node_type_name(addr, n, sizeof (n)));
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7311ea5f93dSBryan Cantrill 	if (dt_node_is_posconst(max) == 0) {
7321ea5f93dSBryan Cantrill 		dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
7337c478bd9Sstevel@tonic-gate 		    "be a non-zero positive integral constant expression\n");
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7361ea5f93dSBryan Cantrill 	if ((size = max->dn_list) != NULL) {
7371ea5f93dSBryan Cantrill 		if (size->dn_list != NULL) {
7381ea5f93dSBryan Cantrill 			dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
7391ea5f93dSBryan Cantrill 			    "mismatch: expected at most 3 args\n");
7401ea5f93dSBryan Cantrill 		}
7411ea5f93dSBryan Cantrill 
7421ea5f93dSBryan Cantrill 		if (!dt_node_is_scalar(size)) {
7431ea5f93dSBryan Cantrill 			dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
7441ea5f93dSBryan Cantrill 			    "dynamic size (argument #3) must be of "
7451ea5f93dSBryan Cantrill 			    "scalar type\n");
7461ea5f93dSBryan Cantrill 		}
7471ea5f93dSBryan Cantrill 
7481ea5f93dSBryan Cantrill 		dt_cg(yypcb, size);
7491ea5f93dSBryan Cantrill 		ap->dtad_difo = dt_as(yypcb);
7501ea5f93dSBryan Cantrill 		ap->dtad_difo->dtdo_rtype = dt_int_rtype;
7511ea5f93dSBryan Cantrill 		ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
7521ea5f93dSBryan Cantrill 
7531ea5f93dSBryan Cantrill 		ap = dt_stmt_action(dtp, sdp);
7541ea5f93dSBryan Cantrill 	}
7551ea5f93dSBryan Cantrill 
7567c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, addr);
7577c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
7581ea5f93dSBryan Cantrill 	ap->dtad_kind = DTRACEACT_TRACEMEM;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
7611ea5f93dSBryan Cantrill 	ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate static void
7657c478bd9Sstevel@tonic-gate dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STACK;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7707c478bd9Sstevel@tonic-gate 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7717c478bd9Sstevel@tonic-gate 	} else {
7727c478bd9Sstevel@tonic-gate 		ap->dtad_arg = 0;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
7767c478bd9Sstevel@tonic-gate 		if (arg0->dn_list != NULL) {
7777c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
7787c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (dt_node_is_posconst(arg0) == 0) {
7827c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
7837c478bd9Sstevel@tonic-gate 			    "non-zero positive integral constant expression\n");
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		ap->dtad_arg = arg0->dn_value;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate static void
7917c478bd9Sstevel@tonic-gate dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7947c478bd9Sstevel@tonic-gate 	dt_action_stack_args(dtp, ap, dnp->dn_args);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static void
7987c478bd9Sstevel@tonic-gate dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	uint32_t nframes = 0;
8017c478bd9Sstevel@tonic-gate 	uint32_t strsize = 0;	/* default string table size */
8027c478bd9Sstevel@tonic-gate 	dt_node_t *arg0 = dnp->dn_args;
8037c478bd9Sstevel@tonic-gate 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
8067c478bd9Sstevel@tonic-gate 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
8097c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
8107c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
8137c478bd9Sstevel@tonic-gate 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_JSTACK;
8167c478bd9Sstevel@tonic-gate 	} else {
8177c478bd9Sstevel@tonic-gate 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
8207c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_USTACK;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
8267c478bd9Sstevel@tonic-gate 		if (!dt_node_is_posconst(arg0)) {
8277c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
8287c478bd9Sstevel@tonic-gate 			    "must be a non-zero positive integer constant\n");
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 		nframes = (uint32_t)arg0->dn_value;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (arg1 != NULL) {
8347c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT ||
8357c478bd9Sstevel@tonic-gate 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
8367c478bd9Sstevel@tonic-gate 		    (int64_t)arg1->dn_value < 0)) {
8377c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
8387c478bd9Sstevel@tonic-gate 			    "must be a positive integer constant\n");
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		if (arg1->dn_list != NULL) {
8427c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
8437c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 		strsize = (uint32_t)arg1->dn_value;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate static void
8537c478bd9Sstevel@tonic-gate dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8567c478bd9Sstevel@tonic-gate 	dt_action_ustack_args(dtp, ap, dnp);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
859a1b5e537Sbmc static void
860a1b5e537Sbmc dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
861a1b5e537Sbmc {
862a1b5e537Sbmc 	dtrace_actdesc_t *ap;
863a1b5e537Sbmc 	dt_node_t *arg0, *arg1;
864a1b5e537Sbmc 
865a1b5e537Sbmc 	/*
866a1b5e537Sbmc 	 * The prototype guarantees that we are called with either one or
867a1b5e537Sbmc 	 * two arguments, and that any arguments that are present are strings.
868a1b5e537Sbmc 	 */
869a1b5e537Sbmc 	arg0 = dnp->dn_args;
870a1b5e537Sbmc 	arg1 = arg0->dn_list;
871a1b5e537Sbmc 
872a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
873a1b5e537Sbmc 	dt_cg(yypcb, arg0);
874a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
875a1b5e537Sbmc 	ap->dtad_kind = DTRACEACT_LIBACT;
876a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
877a1b5e537Sbmc 
878a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
879a1b5e537Sbmc 
880a1b5e537Sbmc 	if (arg1 == NULL) {
881a1b5e537Sbmc 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
882a1b5e537Sbmc 	} else {
883a1b5e537Sbmc 		dt_cg(yypcb, arg1);
884a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
885a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_LIBACT;
886a1b5e537Sbmc 	}
887a1b5e537Sbmc 
888a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
889a1b5e537Sbmc }
890a1b5e537Sbmc 
891a1b5e537Sbmc /*ARGSUSED*/
892a1b5e537Sbmc static void
893a1b5e537Sbmc dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
894a1b5e537Sbmc     dt_node_t *dnp, dtrace_actkind_t kind)
895a1b5e537Sbmc {
896a1b5e537Sbmc 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
897a1b5e537Sbmc 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
898a1b5e537Sbmc 	    kind == DTRACEACT_UADDR);
899a1b5e537Sbmc 
900a1b5e537Sbmc 	dt_cg(yypcb, dnp);
901a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
902a1b5e537Sbmc 	ap->dtad_kind = kind;
903a1b5e537Sbmc 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
904a1b5e537Sbmc }
905a1b5e537Sbmc 
906a1b5e537Sbmc static void
907a1b5e537Sbmc dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
908a1b5e537Sbmc     dtrace_actkind_t kind)
909a1b5e537Sbmc {
910a1b5e537Sbmc 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
911a1b5e537Sbmc 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
912a1b5e537Sbmc }
913a1b5e537Sbmc 
9147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9157c478bd9Sstevel@tonic-gate static void
9167c478bd9Sstevel@tonic-gate dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * Library actions need a DIFO that serves as an argument.  As
9227c478bd9Sstevel@tonic-gate 	 * ftruncate() doesn't take an argument, we generate the constant 0
9237c478bd9Sstevel@tonic-gate 	 * in a DIFO; this constant will be ignored when the ftruncate() is
9247c478bd9Sstevel@tonic-gate 	 * processed.
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
9277c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_FTRUNCATE;
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9317c478bd9Sstevel@tonic-gate static void
9327c478bd9Sstevel@tonic-gate dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STOP;
9377c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9417c478bd9Sstevel@tonic-gate static void
9427c478bd9Sstevel@tonic-gate dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
9477c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9517c478bd9Sstevel@tonic-gate static void
9527c478bd9Sstevel@tonic-gate dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_PANIC;
9577c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate static void
9617c478bd9Sstevel@tonic-gate dt_action_chill(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_CHILL;
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate static void
9717c478bd9Sstevel@tonic-gate dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9767c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9777c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_RAISE;
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate static void
9817c478bd9Sstevel@tonic-gate dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9867c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9877c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_EXIT;
9887c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate static void
9927c478bd9Sstevel@tonic-gate dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9977c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9987c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_SPECULATE;
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate static void
10027c478bd9Sstevel@tonic-gate dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10077c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10087c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_COMMIT;
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate static void
10127c478bd9Sstevel@tonic-gate dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10177c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10187c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DISCARD;
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate static void
10227c478bd9Sstevel@tonic-gate dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10237c478bd9Sstevel@tonic-gate {
10247c478bd9Sstevel@tonic-gate 	switch (dnp->dn_expr->dn_ident->di_id) {
10257c478bd9Sstevel@tonic-gate 	case DT_ACT_BREAKPOINT:
10267c478bd9Sstevel@tonic-gate 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
10277c478bd9Sstevel@tonic-gate 		break;
10287c478bd9Sstevel@tonic-gate 	case DT_ACT_CHILL:
10297c478bd9Sstevel@tonic-gate 		dt_action_chill(dtp, dnp->dn_expr, sdp);
10307c478bd9Sstevel@tonic-gate 		break;
10317c478bd9Sstevel@tonic-gate 	case DT_ACT_CLEAR:
10327c478bd9Sstevel@tonic-gate 		dt_action_clear(dtp, dnp->dn_expr, sdp);
10337c478bd9Sstevel@tonic-gate 		break;
10347c478bd9Sstevel@tonic-gate 	case DT_ACT_COMMIT:
10357c478bd9Sstevel@tonic-gate 		dt_action_commit(dtp, dnp->dn_expr, sdp);
10367c478bd9Sstevel@tonic-gate 		break;
10377c478bd9Sstevel@tonic-gate 	case DT_ACT_DENORMALIZE:
10387c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10397c478bd9Sstevel@tonic-gate 		break;
10407c478bd9Sstevel@tonic-gate 	case DT_ACT_DISCARD:
10417c478bd9Sstevel@tonic-gate 		dt_action_discard(dtp, dnp->dn_expr, sdp);
10427c478bd9Sstevel@tonic-gate 		break;
10437c478bd9Sstevel@tonic-gate 	case DT_ACT_EXIT:
10447c478bd9Sstevel@tonic-gate 		dt_action_exit(dtp, dnp->dn_expr, sdp);
10457c478bd9Sstevel@tonic-gate 		break;
10467c478bd9Sstevel@tonic-gate 	case DT_ACT_FREOPEN:
10477c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
10487c478bd9Sstevel@tonic-gate 		break;
10497c478bd9Sstevel@tonic-gate 	case DT_ACT_FTRUNCATE:
10507c478bd9Sstevel@tonic-gate 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
10517c478bd9Sstevel@tonic-gate 		break;
1052a1b5e537Sbmc 	case DT_ACT_MOD:
1053a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1054a1b5e537Sbmc 		break;
10557c478bd9Sstevel@tonic-gate 	case DT_ACT_NORMALIZE:
10567c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10577c478bd9Sstevel@tonic-gate 		break;
10587c478bd9Sstevel@tonic-gate 	case DT_ACT_PANIC:
10597c478bd9Sstevel@tonic-gate 		dt_action_panic(dtp, dnp->dn_expr, sdp);
10607c478bd9Sstevel@tonic-gate 		break;
1061*e5803b76SAdam H. Leventhal 	case DT_ACT_PRINT:
1062*e5803b76SAdam H. Leventhal 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1063*e5803b76SAdam H. Leventhal 		break;
10647c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTA:
10657c478bd9Sstevel@tonic-gate 		dt_action_printa(dtp, dnp->dn_expr, sdp);
10667c478bd9Sstevel@tonic-gate 		break;
10677c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTF:
10687c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
10697c478bd9Sstevel@tonic-gate 		break;
10707c478bd9Sstevel@tonic-gate 	case DT_ACT_RAISE:
10717c478bd9Sstevel@tonic-gate 		dt_action_raise(dtp, dnp->dn_expr, sdp);
10727c478bd9Sstevel@tonic-gate 		break;
1073a1b5e537Sbmc 	case DT_ACT_SETOPT:
1074a1b5e537Sbmc 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1075a1b5e537Sbmc 		break;
10767c478bd9Sstevel@tonic-gate 	case DT_ACT_SPECULATE:
10777c478bd9Sstevel@tonic-gate 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
10787c478bd9Sstevel@tonic-gate 		break;
10797c478bd9Sstevel@tonic-gate 	case DT_ACT_STACK:
10807c478bd9Sstevel@tonic-gate 		dt_action_stack(dtp, dnp->dn_expr, sdp);
10817c478bd9Sstevel@tonic-gate 		break;
10827c478bd9Sstevel@tonic-gate 	case DT_ACT_STOP:
10837c478bd9Sstevel@tonic-gate 		dt_action_stop(dtp, dnp->dn_expr, sdp);
10847c478bd9Sstevel@tonic-gate 		break;
1085a1b5e537Sbmc 	case DT_ACT_SYM:
1086a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1087a1b5e537Sbmc 		break;
10887c478bd9Sstevel@tonic-gate 	case DT_ACT_SYSTEM:
10897c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
10907c478bd9Sstevel@tonic-gate 		break;
10917c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACE:
10927c478bd9Sstevel@tonic-gate 		dt_action_trace(dtp, dnp->dn_expr, sdp);
10937c478bd9Sstevel@tonic-gate 		break;
10947c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACEMEM:
10957c478bd9Sstevel@tonic-gate 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
10967c478bd9Sstevel@tonic-gate 		break;
10977c478bd9Sstevel@tonic-gate 	case DT_ACT_TRUNC:
10987c478bd9Sstevel@tonic-gate 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
10997c478bd9Sstevel@tonic-gate 		break;
1100a1b5e537Sbmc 	case DT_ACT_UADDR:
1101a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1102a1b5e537Sbmc 		break;
1103a1b5e537Sbmc 	case DT_ACT_UMOD:
1104a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1105a1b5e537Sbmc 		break;
1106a1b5e537Sbmc 	case DT_ACT_USYM:
1107a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1108a1b5e537Sbmc 		break;
11097c478bd9Sstevel@tonic-gate 	case DT_ACT_USTACK:
11107c478bd9Sstevel@tonic-gate 	case DT_ACT_JSTACK:
11117c478bd9Sstevel@tonic-gate 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
11127c478bd9Sstevel@tonic-gate 		break;
11137c478bd9Sstevel@tonic-gate 	default:
11147c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
11157c478bd9Sstevel@tonic-gate 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate static void
11207c478bd9Sstevel@tonic-gate dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_expr);
11257c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
11267c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
11277c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate static void
11317c478bd9Sstevel@tonic-gate dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
1134a1b5e537Sbmc 	dt_node_t *anp, *incr = NULL;
11357c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
1136a1b5e537Sbmc 	uint_t n = 1, argmax;
1137a1b5e537Sbmc 	uint64_t arg = 0;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * If the aggregation has no aggregating function applied to it, then
11417c478bd9Sstevel@tonic-gate 	 * this statement has no effect.  Flag this as a programming error.
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun == NULL) {
11447c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
11457c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	aid = dnp->dn_ident;
11497c478bd9Sstevel@tonic-gate 	fid = dnp->dn_aggfun->dn_ident;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun->dn_args != NULL &&
11527c478bd9Sstevel@tonic-gate 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
11537c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
11547c478bd9Sstevel@tonic-gate 		    "be of scalar type\n", fid->di_name);
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * The ID of the aggregation itself is implicitly recorded as the first
11597c478bd9Sstevel@tonic-gate 	 * member of each aggregation tuple so we can distinguish them later.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
11627c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
11657c478bd9Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
11667c478bd9Sstevel@tonic-gate 		n++;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if (anp->dn_kind == DT_NODE_FUNC) {
11697c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
11707c478bd9Sstevel@tonic-gate 				dt_action_stack_args(dtp, ap, anp->dn_args);
11717c478bd9Sstevel@tonic-gate 				continue;
11727c478bd9Sstevel@tonic-gate 			}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
11757c478bd9Sstevel@tonic-gate 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
11767c478bd9Sstevel@tonic-gate 				dt_action_ustack_args(dtp, ap, anp);
11777c478bd9Sstevel@tonic-gate 				continue;
11787c478bd9Sstevel@tonic-gate 			}
11797c478bd9Sstevel@tonic-gate 
1180a1b5e537Sbmc 			switch (anp->dn_ident->di_id) {
1181a1b5e537Sbmc 			case DT_ACT_UADDR:
1182a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1183a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UADDR);
1184a1b5e537Sbmc 				continue;
11857c478bd9Sstevel@tonic-gate 
1186a1b5e537Sbmc 			case DT_ACT_USYM:
1187a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1188a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_USYM);
1189a1b5e537Sbmc 				continue;
11907c478bd9Sstevel@tonic-gate 
1191a1b5e537Sbmc 			case DT_ACT_UMOD:
1192a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1193a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UMOD);
1194a1b5e537Sbmc 				continue;
11957c478bd9Sstevel@tonic-gate 
1196a1b5e537Sbmc 			case DT_ACT_SYM:
1197a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1198a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_SYM);
1199a1b5e537Sbmc 				continue;
1200a1b5e537Sbmc 
1201a1b5e537Sbmc 			case DT_ACT_MOD:
1202a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1203a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_MOD);
1204a1b5e537Sbmc 				continue;
1205a1b5e537Sbmc 
1206a1b5e537Sbmc 			default:
1207a1b5e537Sbmc 				break;
1208a1b5e537Sbmc 			}
1209a1b5e537Sbmc 		}
1210a1b5e537Sbmc 
1211a1b5e537Sbmc 		dt_cg(yypcb, anp);
12127c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
1213a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
12147c478bd9Sstevel@tonic-gate 	}
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
12177c478bd9Sstevel@tonic-gate 		/*
1218a1b5e537Sbmc 		 * For linear quantization, we have between two and four
1219a1b5e537Sbmc 		 * arguments in addition to the expression:
12207c478bd9Sstevel@tonic-gate 		 *
12217c478bd9Sstevel@tonic-gate 		 *    arg1 => Base value
12227c478bd9Sstevel@tonic-gate 		 *    arg2 => Limit value
12237c478bd9Sstevel@tonic-gate 		 *    arg3 => Quantization level step size (defaults to 1)
1224a1b5e537Sbmc 		 *    arg4 => Quantization increment value (defaults to 1)
12257c478bd9Sstevel@tonic-gate 		 */
12267c478bd9Sstevel@tonic-gate 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
12277c478bd9Sstevel@tonic-gate 		dt_node_t *arg2 = arg1->dn_list;
12287c478bd9Sstevel@tonic-gate 		dt_node_t *arg3 = arg2->dn_list;
122930ef842dSbmc 		dt_idsig_t *isp;
123030ef842dSbmc 		uint64_t nlevels, step = 1, oarg;
12317c478bd9Sstevel@tonic-gate 		int64_t baseval, limitval;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT) {
12347c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
12357c478bd9Sstevel@tonic-gate 			    "argument #1 must be an integer constant\n");
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		baseval = (int64_t)arg1->dn_value;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
12417c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
12427c478bd9Sstevel@tonic-gate 			    "argument #1 must be a 32-bit quantity\n");
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 		if (arg2->dn_kind != DT_NODE_INT) {
12467c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
12477c478bd9Sstevel@tonic-gate 			    "argument #2 must be an integer constant\n");
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		limitval = (int64_t)arg2->dn_value;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
12537c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
12547c478bd9Sstevel@tonic-gate 			    "argument #2 must be a 32-bit quantity\n");
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		if (limitval < baseval) {
12587c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_MISMATCH,
12597c478bd9Sstevel@tonic-gate 			    "lquantize( ) base (argument #1) must be less "
12607c478bd9Sstevel@tonic-gate 			    "than limit (argument #2)\n");
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		if (arg3 != NULL) {
12647c478bd9Sstevel@tonic-gate 			if (!dt_node_is_posconst(arg3)) {
12657c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
12667c478bd9Sstevel@tonic-gate 				    "argument #3 must be a non-zero positive "
12677c478bd9Sstevel@tonic-gate 				    "integer constant\n");
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 			if ((step = arg3->dn_value) > UINT16_MAX) {
12717c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
12727c478bd9Sstevel@tonic-gate 				    "argument #3 must be a 16-bit quantity\n");
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		nlevels = (limitval - baseval) / step;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		if (nlevels == 0) {
12797c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPLARGE,
12807c478bd9Sstevel@tonic-gate 			    "lquantize( ) step (argument #3) too large: must "
12817c478bd9Sstevel@tonic-gate 			    "have at least one quantization level\n");
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		if (nlevels > UINT16_MAX) {
12857c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
12867c478bd9Sstevel@tonic-gate 			    "(argument #3) too small: number of quantization "
12877c478bd9Sstevel@tonic-gate 			    "levels must be a 16-bit quantity\n");
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 
1290a1b5e537Sbmc 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
12917c478bd9Sstevel@tonic-gate 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
12927c478bd9Sstevel@tonic-gate 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
12937c478bd9Sstevel@tonic-gate 		    DTRACE_LQUANTIZE_BASEMASK);
1294a1b5e537Sbmc 
129530ef842dSbmc 		assert(arg != 0);
129630ef842dSbmc 
129730ef842dSbmc 		isp = (dt_idsig_t *)aid->di_data;
129830ef842dSbmc 
129930ef842dSbmc 		if (isp->dis_auxinfo == 0) {
130030ef842dSbmc 			/*
130130ef842dSbmc 			 * This is the first time we've seen an lquantize()
130230ef842dSbmc 			 * for this aggregation; we'll store our argument
130330ef842dSbmc 			 * as the auxiliary signature information.
130430ef842dSbmc 			 */
130530ef842dSbmc 			isp->dis_auxinfo = arg;
130630ef842dSbmc 		} else if ((oarg = isp->dis_auxinfo) != arg) {
130730ef842dSbmc 			/*
130830ef842dSbmc 			 * If we have seen this lquantize() before and the
130930ef842dSbmc 			 * argument doesn't match the original argument, pick
131030ef842dSbmc 			 * the original argument apart to concisely report the
131130ef842dSbmc 			 * mismatch.
131230ef842dSbmc 			 */
131330ef842dSbmc 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
131430ef842dSbmc 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
131530ef842dSbmc 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
131630ef842dSbmc 
131730ef842dSbmc 			if (obaseval != baseval) {
131830ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
131930ef842dSbmc 				    "base (argument #1) doesn't match previous "
132030ef842dSbmc 				    "declaration: expected %d, found %d\n",
132130ef842dSbmc 				    obaseval, (int)baseval);
132230ef842dSbmc 			}
132330ef842dSbmc 
132430ef842dSbmc 			if (onlevels * ostep != nlevels * step) {
132530ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
132630ef842dSbmc 				    "limit (argument #2) doesn't match previous"
132730ef842dSbmc 				    " declaration: expected %d, found %d\n",
132830ef842dSbmc 				    obaseval + onlevels * ostep,
132930ef842dSbmc 				    (int)baseval + (int)nlevels * (int)step);
133030ef842dSbmc 			}
133130ef842dSbmc 
133230ef842dSbmc 			if (ostep != step) {
133330ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
133430ef842dSbmc 				    "step (argument #3) doesn't match previous "
133530ef842dSbmc 				    "declaration: expected %d, found %d\n",
133630ef842dSbmc 				    ostep, (int)step);
133730ef842dSbmc 			}
133830ef842dSbmc 
133930ef842dSbmc 			/*
134030ef842dSbmc 			 * We shouldn't be able to get here -- one of the
134130ef842dSbmc 			 * parameters must be mismatched if the arguments
134230ef842dSbmc 			 * didn't match.
134330ef842dSbmc 			 */
134430ef842dSbmc 			assert(0);
134530ef842dSbmc 		}
134630ef842dSbmc 
1347a1b5e537Sbmc 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1348a1b5e537Sbmc 		argmax = 5;
1349a1b5e537Sbmc 	}
1350a1b5e537Sbmc 
13512b6389efSBryan Cantrill 	if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
13522b6389efSBryan Cantrill 		/*
13532b6389efSBryan Cantrill 		 * For log/linear quantizations, we have between one and five
13542b6389efSBryan Cantrill 		 * arguments in addition to the expression:
13552b6389efSBryan Cantrill 		 *
13562b6389efSBryan Cantrill 		 *    arg1 => Factor
13572b6389efSBryan Cantrill 		 *    arg2 => Low magnitude
13582b6389efSBryan Cantrill 		 *    arg3 => High magnitude
13592b6389efSBryan Cantrill 		 *    arg4 => Number of steps per magnitude
13602b6389efSBryan Cantrill 		 *    arg5 => Quantization increment value (defaults to 1)
13612b6389efSBryan Cantrill 		 */
13622b6389efSBryan Cantrill 		dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
13632b6389efSBryan Cantrill 		uint64_t oarg, order, v;
13642b6389efSBryan Cantrill 		dt_idsig_t *isp;
13652b6389efSBryan Cantrill 		int i;
13662b6389efSBryan Cantrill 
13672b6389efSBryan Cantrill 		struct {
13682b6389efSBryan Cantrill 			char *str;		/* string identifier */
13692b6389efSBryan Cantrill 			int badtype;		/* error on bad type */
13702b6389efSBryan Cantrill 			int badval;		/* error on bad value */
13712b6389efSBryan Cantrill 			int mismatch;		/* error on bad match */
13722b6389efSBryan Cantrill 			int shift;		/* shift value */
13732b6389efSBryan Cantrill 			uint16_t value;		/* value itself */
13742b6389efSBryan Cantrill 		} args[] = {
13752b6389efSBryan Cantrill 			{ "factor", D_LLQUANT_FACTORTYPE,
13762b6389efSBryan Cantrill 			    D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
13772b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_FACTORSHIFT },
13782b6389efSBryan Cantrill 			{ "low magnitude", D_LLQUANT_LOWTYPE,
13792b6389efSBryan Cantrill 			    D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
13802b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_LOWSHIFT },
13812b6389efSBryan Cantrill 			{ "high magnitude", D_LLQUANT_HIGHTYPE,
13822b6389efSBryan Cantrill 			    D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
13832b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_HIGHSHIFT },
13842b6389efSBryan Cantrill 			{ "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
13852b6389efSBryan Cantrill 			    D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
13862b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_NSTEPSHIFT },
13872b6389efSBryan Cantrill 			{ NULL }
13882b6389efSBryan Cantrill 		};
13892b6389efSBryan Cantrill 
13902b6389efSBryan Cantrill 		assert(arg == 0);
13912b6389efSBryan Cantrill 
13922b6389efSBryan Cantrill 		for (i = 0; args[i].str != NULL; i++) {
13932b6389efSBryan Cantrill 			if (llarg->dn_kind != DT_NODE_INT) {
13942b6389efSBryan Cantrill 				dnerror(llarg, args[i].badtype, "llquantize( ) "
13952b6389efSBryan Cantrill 				    "argument #%d (%s) must be an "
13962b6389efSBryan Cantrill 				    "integer constant\n", i + 1, args[i].str);
13972b6389efSBryan Cantrill 			}
13982b6389efSBryan Cantrill 
13992b6389efSBryan Cantrill 			if ((uint64_t)llarg->dn_value > UINT16_MAX) {
14002b6389efSBryan Cantrill 				dnerror(llarg, args[i].badval, "llquantize( ) "
14012b6389efSBryan Cantrill 				    "argument #%d (%s) must be an unsigned "
14022b6389efSBryan Cantrill 				    "16-bit quantity\n", i + 1, args[i].str);
14032b6389efSBryan Cantrill 			}
14042b6389efSBryan Cantrill 
14052b6389efSBryan Cantrill 			args[i].value = (uint16_t)llarg->dn_value;
14062b6389efSBryan Cantrill 
14072b6389efSBryan Cantrill 			assert(!(arg & (UINT16_MAX << args[i].shift)));
14082b6389efSBryan Cantrill 			arg |= ((uint64_t)args[i].value << args[i].shift);
14092b6389efSBryan Cantrill 			llarg = llarg->dn_list;
14102b6389efSBryan Cantrill 		}
14112b6389efSBryan Cantrill 
14122b6389efSBryan Cantrill 		assert(arg != 0);
14132b6389efSBryan Cantrill 
14142b6389efSBryan Cantrill 		if (args[0].value < 2) {
14152b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
14162b6389efSBryan Cantrill 			    "factor (argument #1) must be two or more\n");
14172b6389efSBryan Cantrill 		}
14182b6389efSBryan Cantrill 
14192b6389efSBryan Cantrill 		if (args[1].value >= args[2].value) {
14202b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
14212b6389efSBryan Cantrill 			    "high magnitude (argument #3) must be greater "
14222b6389efSBryan Cantrill 			    "than low magnitude (argument #2)\n");
14232b6389efSBryan Cantrill 		}
14242b6389efSBryan Cantrill 
14252b6389efSBryan Cantrill 		if (args[3].value < args[0].value) {
14262b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
14272b6389efSBryan Cantrill 			    "factor (argument #1) must be less than or "
14282b6389efSBryan Cantrill 			    "equal to the number of linear steps per "
14292b6389efSBryan Cantrill 			    "magnitude (argument #4)\n");
14302b6389efSBryan Cantrill 		}
14312b6389efSBryan Cantrill 
14322b6389efSBryan Cantrill 		for (v = args[0].value; v < args[3].value; v *= args[0].value)
14332b6389efSBryan Cantrill 			continue;
14342b6389efSBryan Cantrill 
14352b6389efSBryan Cantrill 		if ((args[3].value % args[0].value) || (v % args[3].value)) {
14362b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
14372b6389efSBryan Cantrill 			    "factor (argument #1) must evenly divide the "
14382b6389efSBryan Cantrill 			    "number of steps per magnitude (argument #4), "
14392b6389efSBryan Cantrill 			    "and the number of steps per magnitude must evenly "
14402b6389efSBryan Cantrill 			    "divide a power of the factor\n");
14412b6389efSBryan Cantrill 		}
14422b6389efSBryan Cantrill 
14432b6389efSBryan Cantrill 		for (i = 0, order = 1; i < args[2].value; i++) {
14442b6389efSBryan Cantrill 			if (order * args[0].value > order) {
14452b6389efSBryan Cantrill 				order *= args[0].value;
14462b6389efSBryan Cantrill 				continue;
14472b6389efSBryan Cantrill 			}
14482b6389efSBryan Cantrill 
14492b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
14502b6389efSBryan Cantrill 			    "factor (%d) raised to power of high magnitude "
14512b6389efSBryan Cantrill 			    "(%d) overflows 64-bits\n", args[0].value,
14522b6389efSBryan Cantrill 			    args[2].value);
14532b6389efSBryan Cantrill 		}
14542b6389efSBryan Cantrill 
14552b6389efSBryan Cantrill 		isp = (dt_idsig_t *)aid->di_data;
14562b6389efSBryan Cantrill 
14572b6389efSBryan Cantrill 		if (isp->dis_auxinfo == 0) {
14582b6389efSBryan Cantrill 			/*
14592b6389efSBryan Cantrill 			 * This is the first time we've seen an llquantize()
14602b6389efSBryan Cantrill 			 * for this aggregation; we'll store our argument
14612b6389efSBryan Cantrill 			 * as the auxiliary signature information.
14622b6389efSBryan Cantrill 			 */
14632b6389efSBryan Cantrill 			isp->dis_auxinfo = arg;
14642b6389efSBryan Cantrill 		} else if ((oarg = isp->dis_auxinfo) != arg) {
14652b6389efSBryan Cantrill 			/*
14662b6389efSBryan Cantrill 			 * If we have seen this llquantize() before and the
14672b6389efSBryan Cantrill 			 * argument doesn't match the original argument, pick
14682b6389efSBryan Cantrill 			 * the original argument apart to concisely report the
14692b6389efSBryan Cantrill 			 * mismatch.
14702b6389efSBryan Cantrill 			 */
14712b6389efSBryan Cantrill 			int expected = 0, found = 0;
14722b6389efSBryan Cantrill 
14732b6389efSBryan Cantrill 			for (i = 0; expected == found; i++) {
14742b6389efSBryan Cantrill 				assert(args[i].str != NULL);
14752b6389efSBryan Cantrill 
14762b6389efSBryan Cantrill 				expected = (oarg >> args[i].shift) & UINT16_MAX;
14772b6389efSBryan Cantrill 				found = (arg >> args[i].shift) & UINT16_MAX;
14782b6389efSBryan Cantrill 			}
14792b6389efSBryan Cantrill 
14802b6389efSBryan Cantrill 			dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
14812b6389efSBryan Cantrill 			    "%s (argument #%d) doesn't match previous "
14822b6389efSBryan Cantrill 			    "declaration: expected %d, found %d\n",
14832b6389efSBryan Cantrill 			    args[i - 1].str, i, expected, found);
14842b6389efSBryan Cantrill 		}
14852b6389efSBryan Cantrill 
14862b6389efSBryan Cantrill 		incr = llarg;
14872b6389efSBryan Cantrill 		argmax = 6;
14882b6389efSBryan Cantrill 	}
14892b6389efSBryan Cantrill 
1490a1b5e537Sbmc 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1491a1b5e537Sbmc 		incr = dnp->dn_aggfun->dn_args->dn_list;
1492a1b5e537Sbmc 		argmax = 2;
1493a1b5e537Sbmc 	}
1494a1b5e537Sbmc 
1495a1b5e537Sbmc 	if (incr != NULL) {
1496a1b5e537Sbmc 		if (!dt_node_is_scalar(incr)) {
1497a1b5e537Sbmc 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1498a1b5e537Sbmc 			    "(argument #%d) must be of scalar type\n",
1499a1b5e537Sbmc 			    fid->di_name, argmax);
1500a1b5e537Sbmc 		}
1501a1b5e537Sbmc 
1502a1b5e537Sbmc 		if ((anp = incr->dn_list) != NULL) {
1503a1b5e537Sbmc 			int argc = argmax;
1504a1b5e537Sbmc 
1505a1b5e537Sbmc 			for (; anp != NULL; anp = anp->dn_list)
1506a1b5e537Sbmc 				argc++;
1507a1b5e537Sbmc 
1508a1b5e537Sbmc 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1509a1b5e537Sbmc 			    "mismatch: %d args passed, at most %d expected",
1510a1b5e537Sbmc 			    fid->di_name, argc, argmax);
1511a1b5e537Sbmc 		}
1512a1b5e537Sbmc 
1513a1b5e537Sbmc 		ap = dt_stmt_action(dtp, sdp);
1514a1b5e537Sbmc 		n++;
1515a1b5e537Sbmc 
1516a1b5e537Sbmc 		dt_cg(yypcb, incr);
1517a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
1518a1b5e537Sbmc 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1519a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1520a1b5e537Sbmc 	}
1521a1b5e537Sbmc 
1522a1b5e537Sbmc 	assert(sdp->dtsd_aggdata == NULL);
1523a1b5e537Sbmc 	sdp->dtsd_aggdata = aid;
1524a1b5e537Sbmc 
1525a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
1526a1b5e537Sbmc 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1527a1b5e537Sbmc 	assert(DTRACEACT_ISAGG(fid->di_id));
1528a1b5e537Sbmc 	ap->dtad_kind = fid->di_id;
1529a1b5e537Sbmc 	ap->dtad_ntuple = n;
1530a1b5e537Sbmc 	ap->dtad_arg = arg;
1531a1b5e537Sbmc 
1532a1b5e537Sbmc 	if (dnp->dn_aggfun->dn_args != NULL) {
1533a1b5e537Sbmc 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1534a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate static void
15397c478bd9Sstevel@tonic-gate dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
15427c478bd9Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
15437c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	yylineno = pnp->dn_line;
15467c478bd9Sstevel@tonic-gate 	dt_setcontext(dtp, pnp->dn_desc);
15477c478bd9Sstevel@tonic-gate 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (DT_TREEDUMP_PASS(dtp, 2))
15507c478bd9Sstevel@tonic-gate 		dt_node_printr(cnp, stderr, 0);
15517c478bd9Sstevel@tonic-gate 
15521a7c1b72Smws 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
15537c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == NULL);
15567c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = edp;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (cnp->dn_pred != NULL) {
15597c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, cnp->dn_pred);
15607c478bd9Sstevel@tonic-gate 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	if (cnp->dn_acts == NULL) {
15647c478bd9Sstevel@tonic-gate 		dt_stmt_append(dt_stmt_create(dtp, edp,
15657c478bd9Sstevel@tonic-gate 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
15667c478bd9Sstevel@tonic-gate 	}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
15697c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == NULL);
15707c478bd9Sstevel@tonic-gate 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		switch (dnp->dn_kind) {
15737c478bd9Sstevel@tonic-gate 		case DT_NODE_DEXPR:
15747c478bd9Sstevel@tonic-gate 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
15757c478bd9Sstevel@tonic-gate 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
15767c478bd9Sstevel@tonic-gate 			else
15777c478bd9Sstevel@tonic-gate 				dt_compile_exp(dtp, dnp, sdp);
15787c478bd9Sstevel@tonic-gate 			break;
15797c478bd9Sstevel@tonic-gate 		case DT_NODE_DFUNC:
15807c478bd9Sstevel@tonic-gate 			dt_compile_fun(dtp, dnp, sdp);
15817c478bd9Sstevel@tonic-gate 			break;
15827c478bd9Sstevel@tonic-gate 		case DT_NODE_AGG:
15837c478bd9Sstevel@tonic-gate 			dt_compile_agg(dtp, dnp, sdp);
15847c478bd9Sstevel@tonic-gate 			break;
15857c478bd9Sstevel@tonic-gate 		default:
15867c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
15877c478bd9Sstevel@tonic-gate 			    "%u is not a valid statement\n", dnp->dn_kind);
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == sdp);
15917c478bd9Sstevel@tonic-gate 		dt_stmt_append(sdp, dnp);
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == edp);
15951a7c1b72Smws 	dt_ecbdesc_release(dtp, edp);
15967c478bd9Sstevel@tonic-gate 	dt_endcontext(dtp);
15977c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = NULL;
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate static void
16017c478bd9Sstevel@tonic-gate dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	dt_node_t *pnp;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
16067c478bd9Sstevel@tonic-gate 		dt_compile_one_clause(dtp, cnp, pnp);
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16091a7c1b72Smws static void
16101a7c1b72Smws dt_compile_xlator(dt_node_t *dnp)
16111a7c1b72Smws {
16121a7c1b72Smws 	dt_xlator_t *dxp = dnp->dn_xlator;
16131a7c1b72Smws 	dt_node_t *mnp;
16141a7c1b72Smws 
16151a7c1b72Smws 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
16161a7c1b72Smws 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
16171a7c1b72Smws 		dt_cg(yypcb, mnp);
16181a7c1b72Smws 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
16191a7c1b72Smws 	}
16201a7c1b72Smws }
16211a7c1b72Smws 
16227c478bd9Sstevel@tonic-gate void
16237c478bd9Sstevel@tonic-gate dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
16247c478bd9Sstevel@tonic-gate {
16257c478bd9Sstevel@tonic-gate 	const dtrace_pattr_t *pap;
16267c478bd9Sstevel@tonic-gate 	dt_probe_t *prp;
1627c9d6cd77Sjhaslam 	dt_provider_t *pvp;
16287c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
16297c478bd9Sstevel@tonic-gate 	char attrstr[8];
16307c478bd9Sstevel@tonic-gate 	int err;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/*
1633c9d6cd77Sjhaslam 	 * Both kernel and pid based providers are allowed to have names
1634c9d6cd77Sjhaslam 	 * ending with what could be interpreted as a number. We assume it's
1635c9d6cd77Sjhaslam 	 * a pid and that we may need to dynamically create probes for
1636c9d6cd77Sjhaslam 	 * that process if:
1637c9d6cd77Sjhaslam 	 *
1638c9d6cd77Sjhaslam 	 * (1) The provider doesn't exist, or,
1639c9d6cd77Sjhaslam 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1640c9d6cd77Sjhaslam 	 *
1641c9d6cd77Sjhaslam 	 * On an error, dt_pid_create_probes() will set the error message
1642c9d6cd77Sjhaslam 	 * and tag -- we just have to longjmp() out of here.
16437c478bd9Sstevel@tonic-gate 	 */
1644900524f3Sahl 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1645c9d6cd77Sjhaslam 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1646c9d6cd77Sjhaslam 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1647900524f3Sahl 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1648900524f3Sahl 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1649900524f3Sahl 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	/*
16527c478bd9Sstevel@tonic-gate 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
16537c478bd9Sstevel@tonic-gate 	 * a representative probe is found, set 'pap' to the probe provider's
16547c478bd9Sstevel@tonic-gate 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
16557c478bd9Sstevel@tonic-gate 	 */
16567c478bd9Sstevel@tonic-gate 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
16577c478bd9Sstevel@tonic-gate 		pap = &_dtrace_prvdesc;
16587c478bd9Sstevel@tonic-gate 		err = dtrace_errno(dtp);
16597c478bd9Sstevel@tonic-gate 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
16607c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
16617c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
16627c478bd9Sstevel@tonic-gate 	} else {
16637c478bd9Sstevel@tonic-gate 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
16647c478bd9Sstevel@tonic-gate 		err = 0;
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
16687c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
16697c478bd9Sstevel@tonic-gate 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
16707c478bd9Sstevel@tonic-gate 		    pdp->dtpd_func, pdp->dtpd_name);
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
16747c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
16777c478bd9Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
16787c478bd9Sstevel@tonic-gate 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
16797c478bd9Sstevel@tonic-gate 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * Reset the stability attributes of D global variables that vary
16837c478bd9Sstevel@tonic-gate 	 * based on the attributes of the provider and context itself.
16847c478bd9Sstevel@tonic-gate 	 */
16857c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
16867c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_provider;
16877c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
16887c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_mod;
16897c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
16907c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_func;
16917c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
16927c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_name;
16937c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
16947c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_args;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = pdp;
16977c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = prp;
16987c478bd9Sstevel@tonic-gate }
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate /*
17017c478bd9Sstevel@tonic-gate  * Reset context-dependent variables and state at the end of cooking a D probe
17027c478bd9Sstevel@tonic-gate  * definition clause.  This ensures that external declarations between clauses
17037c478bd9Sstevel@tonic-gate  * do not reference any stale context-dependent data from the previous clause.
17047c478bd9Sstevel@tonic-gate  */
17057c478bd9Sstevel@tonic-gate void
17067c478bd9Sstevel@tonic-gate dt_endcontext(dtrace_hdl_t *dtp)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate 	static const char *const cvars[] = {
17097c478bd9Sstevel@tonic-gate 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
17107c478bd9Sstevel@tonic-gate 	};
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
17137c478bd9Sstevel@tonic-gate 	int i;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	for (i = 0; cvars[i] != NULL; i++) {
17167c478bd9Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
17177c478bd9Sstevel@tonic-gate 			idp->di_attr = _dtrace_defattr;
17187c478bd9Sstevel@tonic-gate 	}
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = NULL;
17217c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = NULL;
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate static int
17257c478bd9Sstevel@tonic-gate dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
17267c478bd9Sstevel@tonic-gate {
17277c478bd9Sstevel@tonic-gate 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
17287c478bd9Sstevel@tonic-gate 		dt_idhash_delete(dhp, idp);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	return (0);
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
17357c478bd9Sstevel@tonic-gate  * any identifiers or translators that have been previously defined as bound to
17367c478bd9Sstevel@tonic-gate  * a version greater than the specified version.  Therefore, in our current
17377c478bd9Sstevel@tonic-gate  * version implementation, establishing a binding is a one-way transformation.
17387c478bd9Sstevel@tonic-gate  * In addition, no versioning is currently provided for types as our .d library
17397c478bd9Sstevel@tonic-gate  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
17407c478bd9Sstevel@tonic-gate  * for our exclusive use.  If required, type versioning will require more work.
17417c478bd9Sstevel@tonic-gate  */
17427c478bd9Sstevel@tonic-gate int
17437c478bd9Sstevel@tonic-gate dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	char s[DT_VERSION_STRMAX];
17467c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp, *nxp;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if (v > dtp->dt_vmax)
17497c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
17507c478bd9Sstevel@tonic-gate 	else if (v == dtp->dt_vmax)
17517c478bd9Sstevel@tonic-gate 		return (0); /* no reduction necessary */
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	dt_dprintf("reducing api version to %s\n",
17547c478bd9Sstevel@tonic-gate 	    dt_version_num2str(v, s, sizeof (s)));
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	dtp->dt_vmax = v;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
17597c478bd9Sstevel@tonic-gate 		nxp = dt_list_next(dxp);
17607c478bd9Sstevel@tonic-gate 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
17617c478bd9Sstevel@tonic-gate 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
17627c478bd9Sstevel@tonic-gate 			dt_list_delete(&dtp->dt_xlators, dxp);
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
17667c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
17677c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
17687c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	return (0);
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate /*
17747c478bd9Sstevel@tonic-gate  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
17757c478bd9Sstevel@tonic-gate  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
17767c478bd9Sstevel@tonic-gate  * here to simplify the code by leveraging file descriptor inheritance.
17777c478bd9Sstevel@tonic-gate  */
17787c478bd9Sstevel@tonic-gate static FILE *
17797c478bd9Sstevel@tonic-gate dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	int argc = dtp->dt_cpp_argc;
17827c478bd9Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (argc + 5));
17837c478bd9Sstevel@tonic-gate 	FILE *ofp = tmpfile();
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
17867c478bd9Sstevel@tonic-gate 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	struct sigaction act, oact;
17897c478bd9Sstevel@tonic-gate 	sigset_t mask, omask;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	int wstat, estat;
17927c478bd9Sstevel@tonic-gate 	pid_t pid;
17937c478bd9Sstevel@tonic-gate 	off64_t off;
17947c478bd9Sstevel@tonic-gate 	int c;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (argv == NULL || ofp == NULL) {
17977c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, errno);
17987c478bd9Sstevel@tonic-gate 		goto err;
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	/*
18027c478bd9Sstevel@tonic-gate 	 * If the input is a seekable file, see if it is an interpreter file.
18037c478bd9Sstevel@tonic-gate 	 * If we see #!, seek past the first line because cpp will choke on it.
18047c478bd9Sstevel@tonic-gate 	 * We start cpp just prior to the \n at the end of this line so that
18057c478bd9Sstevel@tonic-gate 	 * it still sees the newline, ensuring that #line values are correct.
18067c478bd9Sstevel@tonic-gate 	 */
18077c478bd9Sstevel@tonic-gate 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
18087c478bd9Sstevel@tonic-gate 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
18097c478bd9Sstevel@tonic-gate 			for (off += 2; c != '\n'; off++) {
18107c478bd9Sstevel@tonic-gate 				if ((c = fgetc(ifp)) == EOF)
18117c478bd9Sstevel@tonic-gate 					break;
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 			if (c == '\n')
18147c478bd9Sstevel@tonic-gate 				off--; /* start cpp just prior to \n */
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 		(void) fflush(ifp);
18177c478bd9Sstevel@tonic-gate 		(void) fseeko64(ifp, off, SEEK_SET);
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
18217c478bd9Sstevel@tonic-gate 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	(void) snprintf(verdef, sizeof (verdef),
18267c478bd9Sstevel@tonic-gate 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
18277c478bd9Sstevel@tonic-gate 	argv[argc++] = verdef;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	switch (dtp->dt_stdcmode) {
18307c478bd9Sstevel@tonic-gate 	case DT_STDC_XA:
18317c478bd9Sstevel@tonic-gate 	case DT_STDC_XT:
18327c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=0";
18337c478bd9Sstevel@tonic-gate 		break;
18347c478bd9Sstevel@tonic-gate 	case DT_STDC_XC:
18357c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=1";
18367c478bd9Sstevel@tonic-gate 		break;
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	argv[argc++] = ipath;
18407c478bd9Sstevel@tonic-gate 	argv[argc++] = opath;
18417c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	/*
18447c478bd9Sstevel@tonic-gate 	 * libdtrace must be able to be embedded in other programs that may
18457c478bd9Sstevel@tonic-gate 	 * include application-specific signal handlers.  Therefore, if we
18467c478bd9Sstevel@tonic-gate 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
18477c478bd9Sstevel@tonic-gate 	 * that could confuse the containing application.  To do this,
18487c478bd9Sstevel@tonic-gate 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
18497c478bd9Sstevel@tonic-gate 	 * We restore our signal state once we are done.
18507c478bd9Sstevel@tonic-gate 	 */
18517c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
18527c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
18537c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	bzero(&act, sizeof (act));
18567c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
18577c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, &oact);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
18607c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &oact, NULL);
18617c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
18627c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CPPFORK);
18637c478bd9Sstevel@tonic-gate 		goto err;
18647c478bd9Sstevel@tonic-gate 	}
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if (pid == 0) {
18677c478bd9Sstevel@tonic-gate 		(void) execvp(dtp->dt_cpp_path, argv);
18687c478bd9Sstevel@tonic-gate 		_exit(errno == ENOENT ? 127 : 126);
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	do {
18727c478bd9Sstevel@tonic-gate 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
18737c478bd9Sstevel@tonic-gate 		    (int)pid);
18747c478bd9Sstevel@tonic-gate 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &oact, NULL);
18777c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
18807c478bd9Sstevel@tonic-gate 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (estat != 0) {
18837c478bd9Sstevel@tonic-gate 		switch (estat) {
18847c478bd9Sstevel@tonic-gate 		case 126:
18857c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
18867c478bd9Sstevel@tonic-gate 			break;
18877c478bd9Sstevel@tonic-gate 		case 127:
18887c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPENT);
18897c478bd9Sstevel@tonic-gate 			break;
18907c478bd9Sstevel@tonic-gate 		default:
18917c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPERR);
18927c478bd9Sstevel@tonic-gate 		}
18937c478bd9Sstevel@tonic-gate 		goto err;
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	free(argv);
18977c478bd9Sstevel@tonic-gate 	(void) fflush(ofp);
18987c478bd9Sstevel@tonic-gate 	(void) fseek(ofp, 0, SEEK_SET);
18997c478bd9Sstevel@tonic-gate 	return (ofp);
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate err:
19027c478bd9Sstevel@tonic-gate 	free(argv);
19037c478bd9Sstevel@tonic-gate 	(void) fclose(ofp);
19047c478bd9Sstevel@tonic-gate 	return (NULL);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
1907c9d6cd77Sjhaslam static void
1908c9d6cd77Sjhaslam dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1909c9d6cd77Sjhaslam {
1910c9d6cd77Sjhaslam 	va_list ap;
1911c9d6cd77Sjhaslam 
1912c9d6cd77Sjhaslam 	va_start(ap, format);
1913c9d6cd77Sjhaslam 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1914c9d6cd77Sjhaslam 	va_end(ap);
1915c9d6cd77Sjhaslam }
1916c9d6cd77Sjhaslam 
1917c9d6cd77Sjhaslam int
1918c9d6cd77Sjhaslam dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1919c9d6cd77Sjhaslam {
1920c9d6cd77Sjhaslam 	dt_lib_depend_t *dld;
1921c9d6cd77Sjhaslam 	const char *end;
1922c9d6cd77Sjhaslam 
1923c9d6cd77Sjhaslam 	assert(arg != NULL);
1924c9d6cd77Sjhaslam 
1925c9d6cd77Sjhaslam 	if ((end = strrchr(arg, '/')) == NULL)
1926c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EINVAL));
1927c9d6cd77Sjhaslam 
1928c9d6cd77Sjhaslam 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1929c9d6cd77Sjhaslam 		return (-1);
1930c9d6cd77Sjhaslam 
1931c9d6cd77Sjhaslam 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
1932c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1933c9d6cd77Sjhaslam 		return (-1);
1934c9d6cd77Sjhaslam 	}
1935c9d6cd77Sjhaslam 
1936c9d6cd77Sjhaslam 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
1937c9d6cd77Sjhaslam 	if ((dld->dtld_library = strdup(arg)) == NULL) {
1938c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
1939c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1940c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
1941c9d6cd77Sjhaslam 	}
1942c9d6cd77Sjhaslam 
1943c9d6cd77Sjhaslam 	dt_list_append(dlp, dld);
1944c9d6cd77Sjhaslam 	return (0);
1945c9d6cd77Sjhaslam }
1946c9d6cd77Sjhaslam 
1947c9d6cd77Sjhaslam dt_lib_depend_t *
1948c9d6cd77Sjhaslam dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1949c9d6cd77Sjhaslam {
1950c9d6cd77Sjhaslam 	dt_lib_depend_t *dldn;
1951c9d6cd77Sjhaslam 
1952c9d6cd77Sjhaslam 	for (dldn = dt_list_next(dld); dldn != NULL;
1953c9d6cd77Sjhaslam 	    dldn = dt_list_next(dldn)) {
1954c9d6cd77Sjhaslam 		if (strcmp(dldn->dtld_library, arg) == 0)
1955c9d6cd77Sjhaslam 			return (dldn);
1956c9d6cd77Sjhaslam 	}
1957c9d6cd77Sjhaslam 
1958c9d6cd77Sjhaslam 	return (NULL);
1959c9d6cd77Sjhaslam }
1960c9d6cd77Sjhaslam 
1961c9d6cd77Sjhaslam /*
1962c9d6cd77Sjhaslam  * Go through all the library files, and, if any library dependencies exist for
1963c9d6cd77Sjhaslam  * that file, add it to that node's list of dependents. The result of this
1964c9d6cd77Sjhaslam  * will be a graph which can then be topologically sorted to produce a
1965c9d6cd77Sjhaslam  * compilation order.
1966c9d6cd77Sjhaslam  */
1967c9d6cd77Sjhaslam static int
1968c9d6cd77Sjhaslam dt_lib_build_graph(dtrace_hdl_t *dtp)
1969c9d6cd77Sjhaslam {
1970c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld;
1971c9d6cd77Sjhaslam 
1972c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1973c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1974c9d6cd77Sjhaslam 		char *library = dld->dtld_library;
1975c9d6cd77Sjhaslam 
1976c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
1977c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
1978c9d6cd77Sjhaslam 			dt_lib_depend_t *dlda;
1979c9d6cd77Sjhaslam 
1980c9d6cd77Sjhaslam 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
1981c9d6cd77Sjhaslam 			    dpld->dtld_library)) == NULL) {
1982c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
1983c9d6cd77Sjhaslam 				    "Invalid library dependency in %s: %s\n",
1984c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
1985c9d6cd77Sjhaslam 
1986c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
1987c9d6cd77Sjhaslam 			}
1988c9d6cd77Sjhaslam 
1989c9d6cd77Sjhaslam 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
1990c9d6cd77Sjhaslam 			    library)) != 0) {
1991c9d6cd77Sjhaslam 				return (-1); /* preserve dt_errno */
1992c9d6cd77Sjhaslam 			}
1993c9d6cd77Sjhaslam 		}
1994c9d6cd77Sjhaslam 	}
1995c9d6cd77Sjhaslam 	return (0);
1996c9d6cd77Sjhaslam }
1997c9d6cd77Sjhaslam 
1998c9d6cd77Sjhaslam static int
1999c9d6cd77Sjhaslam dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2000c9d6cd77Sjhaslam {
2001c9d6cd77Sjhaslam 	dt_lib_depend_t *dpld, *dlda, *new;
2002c9d6cd77Sjhaslam 
2003c9d6cd77Sjhaslam 	dld->dtld_start = ++(*count);
2004c9d6cd77Sjhaslam 
2005c9d6cd77Sjhaslam 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2006c9d6cd77Sjhaslam 	    dpld = dt_list_next(dpld)) {
2007c9d6cd77Sjhaslam 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2008c9d6cd77Sjhaslam 		    dpld->dtld_library);
2009c9d6cd77Sjhaslam 		assert(dlda != NULL);
2010c9d6cd77Sjhaslam 
2011c9d6cd77Sjhaslam 		if (dlda->dtld_start == 0 &&
2012c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dlda, count) == -1)
2013c9d6cd77Sjhaslam 			return (-1);
2014c9d6cd77Sjhaslam 	}
2015c9d6cd77Sjhaslam 
2016c9d6cd77Sjhaslam 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2017c9d6cd77Sjhaslam 		return (-1);
2018c9d6cd77Sjhaslam 
2019c9d6cd77Sjhaslam 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2020c9d6cd77Sjhaslam 		dt_free(dtp, new);
2021c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
2022c9d6cd77Sjhaslam 	}
2023c9d6cd77Sjhaslam 
2024c9d6cd77Sjhaslam 	new->dtld_start = dld->dtld_start;
2025c9d6cd77Sjhaslam 	new->dtld_finish = dld->dtld_finish = ++(*count);
2026c9d6cd77Sjhaslam 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2027c9d6cd77Sjhaslam 
2028c9d6cd77Sjhaslam 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2029c9d6cd77Sjhaslam 	    new->dtld_start, new->dtld_finish);
2030c9d6cd77Sjhaslam 
2031c9d6cd77Sjhaslam 	return (0);
2032c9d6cd77Sjhaslam }
2033c9d6cd77Sjhaslam 
2034c9d6cd77Sjhaslam static int
2035c9d6cd77Sjhaslam dt_lib_depend_sort(dtrace_hdl_t *dtp)
2036c9d6cd77Sjhaslam {
2037c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld, *dlda;
2038c9d6cd77Sjhaslam 	int count = 0;
2039c9d6cd77Sjhaslam 
2040c9d6cd77Sjhaslam 	if (dt_lib_build_graph(dtp) == -1)
2041c9d6cd77Sjhaslam 		return (-1); /* preserve dt_errno */
2042c9d6cd77Sjhaslam 
2043c9d6cd77Sjhaslam 	/*
2044c9d6cd77Sjhaslam 	 * Perform a topological sort of the graph that hangs off
2045c9d6cd77Sjhaslam 	 * dtp->dt_lib_dep. The result of this process will be a
2046c9d6cd77Sjhaslam 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2047c9d6cd77Sjhaslam 	 */
2048c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2049c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2050c9d6cd77Sjhaslam 		if (dld->dtld_start == 0 &&
2051c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dld, &count) == -1)
2052c9d6cd77Sjhaslam 			return (-1); /* preserve dt_errno */;
2053c9d6cd77Sjhaslam 	}
2054c9d6cd77Sjhaslam 
2055c9d6cd77Sjhaslam 	/*
2056c9d6cd77Sjhaslam 	 * Check the graph for cycles. If an ancestor's finishing time is
2057c9d6cd77Sjhaslam 	 * less than any of its dependent's finishing times then a back edge
2058c9d6cd77Sjhaslam 	 * exists in the graph and this is a cycle.
2059c9d6cd77Sjhaslam 	 */
2060c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2061c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2062c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2063c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
2064c9d6cd77Sjhaslam 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2065c9d6cd77Sjhaslam 			    dpld->dtld_library);
2066c9d6cd77Sjhaslam 			assert(dlda != NULL);
2067c9d6cd77Sjhaslam 
2068c9d6cd77Sjhaslam 			if (dlda->dtld_finish > dld->dtld_finish) {
2069c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
2070c9d6cd77Sjhaslam 				    "Cyclic dependency detected: %s => %s\n",
2071c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
2072c9d6cd77Sjhaslam 
2073c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
2074c9d6cd77Sjhaslam 			}
2075c9d6cd77Sjhaslam 		}
2076c9d6cd77Sjhaslam 	}
2077c9d6cd77Sjhaslam 
2078c9d6cd77Sjhaslam 	return (0);
2079c9d6cd77Sjhaslam }
2080c9d6cd77Sjhaslam 
2081c9d6cd77Sjhaslam static void
2082c9d6cd77Sjhaslam dt_lib_depend_free(dtrace_hdl_t *dtp)
2083c9d6cd77Sjhaslam {
2084c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dlda;
2085c9d6cd77Sjhaslam 
2086c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2087c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2088c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependencies, dlda);
2089c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
2090c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
2091c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
2092c9d6cd77Sjhaslam 		}
2093c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2094c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependents, dlda);
2095c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
2096c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
2097c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
2098c9d6cd77Sjhaslam 		}
2099c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep, dld);
2100c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
2101c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
2102c9d6cd77Sjhaslam 		dt_free(dtp, dld);
2103c9d6cd77Sjhaslam 	}
2104c9d6cd77Sjhaslam 
2105c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2106c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2107c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
2108c9d6cd77Sjhaslam 		dt_free(dtp, dld);
2109c9d6cd77Sjhaslam 	}
2110c9d6cd77Sjhaslam }
2111c9d6cd77Sjhaslam 
21127c478bd9Sstevel@tonic-gate /*
21135eb667acSRobert Mustacchi  * Open all the .d library files found in the specified directory and
21145eb667acSRobert Mustacchi  * compile each one of them.  We silently ignore any missing directories and
21155eb667acSRobert Mustacchi  * other files found therein.  We only fail (and thereby fail dt_load_libs()) if
21165eb667acSRobert Mustacchi  * we fail to compile a library and the error is something other than #pragma D
21175eb667acSRobert Mustacchi  * depends_on.  Dependency errors are silently ignored to permit a library
21185eb667acSRobert Mustacchi  * directory to contain libraries which may not be accessible depending on our
21195eb667acSRobert Mustacchi  * privileges.
21207c478bd9Sstevel@tonic-gate  */
21217c478bd9Sstevel@tonic-gate static int
21227c478bd9Sstevel@tonic-gate dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
21237c478bd9Sstevel@tonic-gate {
21244bc0a2efScasper 	struct dirent *dp;
2125afab0816SRobert Mustacchi 	const char *p, *end;
21267c478bd9Sstevel@tonic-gate 	DIR *dirp;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	char fname[PATH_MAX];
21297c478bd9Sstevel@tonic-gate 	FILE *fp;
2130c9d6cd77Sjhaslam 	void *rv;
2131afab0816SRobert Mustacchi 	dt_lib_depend_t *dld;
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL) {
21347c478bd9Sstevel@tonic-gate 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
21357c478bd9Sstevel@tonic-gate 		return (0);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 
2138c9d6cd77Sjhaslam 	/* First, parse each file for library dependencies. */
21394bc0a2efScasper 	while ((dp = readdir(dirp)) != NULL) {
21407c478bd9Sstevel@tonic-gate 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
21417c478bd9Sstevel@tonic-gate 			continue; /* skip any filename not ending in .d */
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 		(void) snprintf(fname, sizeof (fname),
21447c478bd9Sstevel@tonic-gate 		    "%s/%s", path, dp->d_name);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		if ((fp = fopen(fname, "r")) == NULL) {
21477c478bd9Sstevel@tonic-gate 			dt_dprintf("skipping library %s: %s\n",
21487c478bd9Sstevel@tonic-gate 			    fname, strerror(errno));
21497c478bd9Sstevel@tonic-gate 			continue;
21507c478bd9Sstevel@tonic-gate 		}
21517c478bd9Sstevel@tonic-gate 
2152afab0816SRobert Mustacchi 		/*
2153afab0816SRobert Mustacchi 		 * Skip files whose name match an already processed library
2154afab0816SRobert Mustacchi 		 */
2155afab0816SRobert Mustacchi 		for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2156afab0816SRobert Mustacchi 		    dld = dt_list_next(dld)) {
2157afab0816SRobert Mustacchi 			end = strrchr(dld->dtld_library, '/');
2158afab0816SRobert Mustacchi 			/* dt_lib_depend_add ensures this */
2159afab0816SRobert Mustacchi 			assert(end != NULL);
2160afab0816SRobert Mustacchi 			if (strcmp(end + 1, dp->d_name) == 0)
2161afab0816SRobert Mustacchi 				break;
2162afab0816SRobert Mustacchi 		}
2163afab0816SRobert Mustacchi 
2164afab0816SRobert Mustacchi 		if (dld != NULL) {
2165afab0816SRobert Mustacchi 			dt_dprintf("skipping library %s, already processed "
2166afab0816SRobert Mustacchi 			    "library with the same name: %s", dp->d_name,
2167afab0816SRobert Mustacchi 			    dld->dtld_library);
2168afab0816SRobert Mustacchi 			continue;
2169afab0816SRobert Mustacchi 		}
2170afab0816SRobert Mustacchi 
21717c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = fname;
2172c9d6cd77Sjhaslam 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
21735eb667acSRobert Mustacchi 			return (-1); /* preserve dt_errno */
2174c9d6cd77Sjhaslam 
2175c9d6cd77Sjhaslam 		rv = dt_compile(dtp, DT_CTX_DPROG,
2176c9d6cd77Sjhaslam 		    DTRACE_PROBESPEC_NAME, NULL,
2177c9d6cd77Sjhaslam 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2178c9d6cd77Sjhaslam 
2179c9d6cd77Sjhaslam 		if (rv != NULL && dtp->dt_errno &&
2180c9d6cd77Sjhaslam 		    (dtp->dt_errno != EDT_COMPILER ||
2181c9d6cd77Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
21825eb667acSRobert Mustacchi 			return (-1); /* preserve dt_errno */
2183c9d6cd77Sjhaslam 
2184c9d6cd77Sjhaslam 		if (dtp->dt_errno)
2185c9d6cd77Sjhaslam 			dt_dprintf("error parsing library %s: %s\n",
2186c9d6cd77Sjhaslam 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2187c9d6cd77Sjhaslam 
2188c9d6cd77Sjhaslam 		(void) fclose(fp);
2189c9d6cd77Sjhaslam 		dtp->dt_filetag = NULL;
2190c9d6cd77Sjhaslam 	}
2191c9d6cd77Sjhaslam 
2192c9d6cd77Sjhaslam 	(void) closedir(dirp);
21935eb667acSRobert Mustacchi 
21945eb667acSRobert Mustacchi 	return (0);
21955eb667acSRobert Mustacchi }
21965eb667acSRobert Mustacchi 
21975eb667acSRobert Mustacchi /*
21985eb667acSRobert Mustacchi  * Perform a topological sorting of all the libraries found across the entire
21995eb667acSRobert Mustacchi  * dt_lib_path.  Once sorted, compile each one in topological order to cache its
22005eb667acSRobert Mustacchi  * inlines and translators, etc.  We silently ignore any missing directories and
22015eb667acSRobert Mustacchi  * other files found therein. We only fail (and thereby fail dt_load_libs()) if
22025eb667acSRobert Mustacchi  * we fail to compile a library and the error is something other than #pragma D
22035eb667acSRobert Mustacchi  * depends_on.  Dependency errors are silently ignored to permit a library
22045eb667acSRobert Mustacchi  * directory to contain libraries which may not be accessible depending on our
22055eb667acSRobert Mustacchi  * privileges.
22065eb667acSRobert Mustacchi  */
22075eb667acSRobert Mustacchi static int
22085eb667acSRobert Mustacchi dt_load_libs_sort(dtrace_hdl_t *dtp)
22095eb667acSRobert Mustacchi {
22105eb667acSRobert Mustacchi 	dtrace_prog_t *pgp;
22115eb667acSRobert Mustacchi 	FILE *fp;
22125eb667acSRobert Mustacchi 	dt_lib_depend_t *dld;
22135eb667acSRobert Mustacchi 
2214c9d6cd77Sjhaslam 	/*
2215c9d6cd77Sjhaslam 	 * Finish building the graph containing the library dependencies
2216c9d6cd77Sjhaslam 	 * and perform a topological sort to generate an ordered list
2217c9d6cd77Sjhaslam 	 * for compilation.
2218c9d6cd77Sjhaslam 	 */
2219c9d6cd77Sjhaslam 	if (dt_lib_depend_sort(dtp) == -1)
2220c9d6cd77Sjhaslam 		goto err;
2221c9d6cd77Sjhaslam 
2222c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2223c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2224c9d6cd77Sjhaslam 
2225c9d6cd77Sjhaslam 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2226c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
2227c9d6cd77Sjhaslam 			    dld->dtld_library, strerror(errno));
2228c9d6cd77Sjhaslam 			continue;
2229c9d6cd77Sjhaslam 		}
2230c9d6cd77Sjhaslam 
2231c9d6cd77Sjhaslam 		dtp->dt_filetag = dld->dtld_library;
22327c478bd9Sstevel@tonic-gate 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
22337c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
22347c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = NULL;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2237c9d6cd77Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2238c9d6cd77Sjhaslam 			goto err;
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		if (pgp == NULL) {
2241c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
2242c9d6cd77Sjhaslam 			    dld->dtld_library,
22437c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
22446009dbc6Sahl 		} else {
22456009dbc6Sahl 			dld->dtld_loaded = B_TRUE;
22461a7c1b72Smws 			dt_program_destroy(dtp, pgp);
22476009dbc6Sahl 		}
22487c478bd9Sstevel@tonic-gate 	}
22497c478bd9Sstevel@tonic-gate 
2250c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
22517c478bd9Sstevel@tonic-gate 	return (0);
2252c9d6cd77Sjhaslam 
2253c9d6cd77Sjhaslam err:
2254c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
2255c9d6cd77Sjhaslam 	return (-1); /* preserve dt_errno */
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate /*
22597c478bd9Sstevel@tonic-gate  * Load the contents of any appropriate DTrace .d library files.  These files
22607c478bd9Sstevel@tonic-gate  * contain inlines and translators that will be cached by the compiler.  We
22617c478bd9Sstevel@tonic-gate  * defer this activity until the first compile to permit libdtrace clients to
22627c478bd9Sstevel@tonic-gate  * add their own library directories and so that we can properly report errors.
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate static int
22657c478bd9Sstevel@tonic-gate dt_load_libs(dtrace_hdl_t *dtp)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate 	dt_dirpath_t *dirp;
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
22707c478bd9Sstevel@tonic-gate 		return (0); /* libraries already processed */
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
22737c478bd9Sstevel@tonic-gate 
2274afab0816SRobert Mustacchi 	/*
2275afab0816SRobert Mustacchi 	 * /usr/lib/dtrace is always at the head of the list. The rest of the
2276afab0816SRobert Mustacchi 	 * list is specified in the precedence order the user requested. Process
2277afab0816SRobert Mustacchi 	 * everything other than the head first. DTRACE_C_NOLIBS has already
2278afab0816SRobert Mustacchi 	 * been spcified so dt_vopen will ensure that there is always one entry
2279afab0816SRobert Mustacchi 	 * in dt_lib_path.
2280afab0816SRobert Mustacchi 	 */
2281afab0816SRobert Mustacchi 	for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
22827c478bd9Sstevel@tonic-gate 	    dirp != NULL; dirp = dt_list_next(dirp)) {
22837c478bd9Sstevel@tonic-gate 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
22847c478bd9Sstevel@tonic-gate 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
22857c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
22867c478bd9Sstevel@tonic-gate 		}
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
2289afab0816SRobert Mustacchi 	/* Handle /usr/lib/dtrace */
2290afab0816SRobert Mustacchi 	dirp = dt_list_next(&dtp->dt_lib_path);
2291afab0816SRobert Mustacchi 	if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2292afab0816SRobert Mustacchi 		dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2293afab0816SRobert Mustacchi 		return (-1); /* errno is set for us */
2294afab0816SRobert Mustacchi 	}
2295afab0816SRobert Mustacchi 
22965eb667acSRobert Mustacchi 	if (dt_load_libs_sort(dtp) < 0)
22975eb667acSRobert Mustacchi 		return (-1); /* errno is set for us */
22985eb667acSRobert Mustacchi 
22997c478bd9Sstevel@tonic-gate 	return (0);
23007c478bd9Sstevel@tonic-gate }
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate static void *
23037c478bd9Sstevel@tonic-gate dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
23047c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
23057c478bd9Sstevel@tonic-gate {
23067c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
23077c478bd9Sstevel@tonic-gate 	dt_decl_t *ddp;
23087c478bd9Sstevel@tonic-gate 	dt_pcb_t pcb;
23097c478bd9Sstevel@tonic-gate 	void *rv;
23107c478bd9Sstevel@tonic-gate 	int err;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
23137c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
23147c478bd9Sstevel@tonic-gate 		return (NULL);
23157c478bd9Sstevel@tonic-gate 	}
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
23187c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
23197c478bd9Sstevel@tonic-gate 
2320c9a6ea2eSBryan Cantrill 	if (dtp->dt_globals->dh_nelems != 0)
2321c9a6ea2eSBryan Cantrill 		(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
23227c478bd9Sstevel@tonic-gate 
2323c9a6ea2eSBryan Cantrill 	if (dtp->dt_tls->dh_nelems != 0)
2324c9a6ea2eSBryan Cantrill 		(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
23277c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	dt_pcb_push(dtp, &pcb);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	pcb.pcb_fileptr = fp;
23327c478bd9Sstevel@tonic-gate 	pcb.pcb_string = s;
23337c478bd9Sstevel@tonic-gate 	pcb.pcb_strptr = s;
23347c478bd9Sstevel@tonic-gate 	pcb.pcb_strlen = s ? strlen(s) : 0;
23357c478bd9Sstevel@tonic-gate 	pcb.pcb_sargc = argc;
23367c478bd9Sstevel@tonic-gate 	pcb.pcb_sargv = argv;
23377c478bd9Sstevel@tonic-gate 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
23387c478bd9Sstevel@tonic-gate 	pcb.pcb_pspec = pspec;
23397c478bd9Sstevel@tonic-gate 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
23407c478bd9Sstevel@tonic-gate 	pcb.pcb_amin = dtp->dt_amin;
23417c478bd9Sstevel@tonic-gate 	pcb.pcb_yystate = -1;
23427c478bd9Sstevel@tonic-gate 	pcb.pcb_context = context;
23437c478bd9Sstevel@tonic-gate 	pcb.pcb_token = context;
23447c478bd9Sstevel@tonic-gate 
2345c9d6cd77Sjhaslam 	if (context != DT_CTX_DPROG)
23467c478bd9Sstevel@tonic-gate 		yybegin(YYS_EXPR);
2347c9d6cd77Sjhaslam 	else if (cflags & DTRACE_C_CTL)
2348c9d6cd77Sjhaslam 		yybegin(YYS_CONTROL);
2349c9d6cd77Sjhaslam 	else
2350c9d6cd77Sjhaslam 		yybegin(YYS_CLAUSE);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
23537c478bd9Sstevel@tonic-gate 		goto out;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
23567c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
23597c478bd9Sstevel@tonic-gate 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
23607c478bd9Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
23637c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	/*
23667c478bd9Sstevel@tonic-gate 	 * Invoke the parser to evaluate the D source code.  If any errors
23677c478bd9Sstevel@tonic-gate 	 * occur during parsing, an error function will be called and we
23687c478bd9Sstevel@tonic-gate 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
23697c478bd9Sstevel@tonic-gate 	 * we optionally display the parse tree if debugging is enabled.
23707c478bd9Sstevel@tonic-gate 	 */
23717c478bd9Sstevel@tonic-gate 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
23727c478bd9Sstevel@tonic-gate 		xyerror(D_EMPTY, "empty D program translation unit\n");
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	yybegin(YYS_DONE);
23757c478bd9Sstevel@tonic-gate 
2376c9d6cd77Sjhaslam 	if (cflags & DTRACE_C_CTL)
2377c9d6cd77Sjhaslam 		goto out;
2378c9d6cd77Sjhaslam 
23797c478bd9Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
23807c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pragmas != NULL)
23837c478bd9Sstevel@tonic-gate 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
23867c478bd9Sstevel@tonic-gate 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
23877c478bd9Sstevel@tonic-gate 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
23887c478bd9Sstevel@tonic-gate 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
23897c478bd9Sstevel@tonic-gate 	}
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	/*
23927c478bd9Sstevel@tonic-gate 	 * If we have successfully created a parse tree for a D program, loop
23937c478bd9Sstevel@tonic-gate 	 * over the clauses and actions and instantiate the corresponding
23947c478bd9Sstevel@tonic-gate 	 * libdtrace program.  If we are parsing a D expression, then we
23957c478bd9Sstevel@tonic-gate 	 * simply run the code generator and assembler on the resulting tree.
23967c478bd9Sstevel@tonic-gate 	 */
23977c478bd9Sstevel@tonic-gate 	switch (context) {
23987c478bd9Sstevel@tonic-gate 	case DT_CTX_DPROG:
23997c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
24027c478bd9Sstevel@tonic-gate 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
24037c478bd9Sstevel@tonic-gate 			xyerror(D_EMPTY, "empty D program translation unit\n");
24047c478bd9Sstevel@tonic-gate 
24051a7c1b72Smws 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
24067c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 		for (; dnp != NULL; dnp = dnp->dn_list) {
24097c478bd9Sstevel@tonic-gate 			switch (dnp->dn_kind) {
24107c478bd9Sstevel@tonic-gate 			case DT_NODE_CLAUSE:
24117c478bd9Sstevel@tonic-gate 				dt_compile_clause(dtp, dnp);
24127c478bd9Sstevel@tonic-gate 				break;
24131a7c1b72Smws 			case DT_NODE_XLATOR:
24141a7c1b72Smws 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
24151a7c1b72Smws 					dt_compile_xlator(dnp);
24161a7c1b72Smws 				break;
24177c478bd9Sstevel@tonic-gate 			case DT_NODE_PROVIDER:
24187c478bd9Sstevel@tonic-gate 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
24197c478bd9Sstevel@tonic-gate 				break;
24207c478bd9Sstevel@tonic-gate 			}
24217c478bd9Sstevel@tonic-gate 		}
24227c478bd9Sstevel@tonic-gate 
24231a7c1b72Smws 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
24241a7c1b72Smws 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
24251a7c1b72Smws 		yypcb->pcb_asxrefs = NULL;
24261a7c1b72Smws 		yypcb->pcb_asxreflen = 0;
24271a7c1b72Smws 
24281a7c1b72Smws 		rv = yypcb->pcb_prog;
24297c478bd9Sstevel@tonic-gate 		break;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	case DT_CTX_DEXPR:
24327c478bd9Sstevel@tonic-gate 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
24337c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, yypcb->pcb_root);
24347c478bd9Sstevel@tonic-gate 		rv = dt_as(yypcb);
24357c478bd9Sstevel@tonic-gate 		break;
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	case DT_CTX_DTYPE:
24387c478bd9Sstevel@tonic-gate 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
24397c478bd9Sstevel@tonic-gate 		err = dt_decl_type(ddp, arg);
24407c478bd9Sstevel@tonic-gate 		dt_decl_free(ddp);
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 		if (err != 0)
24437c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 		rv = NULL;
24467c478bd9Sstevel@tonic-gate 		break;
24477c478bd9Sstevel@tonic-gate 	}
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate out:
2450*e5803b76SAdam H. Leventhal 	if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2451*e5803b76SAdam H. Leventhal 	    DT_TREEDUMP_PASS(dtp, 3))
24527c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
24557c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
24567c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
24577c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
24607c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
24617c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
24627c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
24657c478bd9Sstevel@tonic-gate 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	dt_pcb_pop(dtp, err);
24687c478bd9Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
24697c478bd9Sstevel@tonic-gate 	return (err ? NULL : rv);
24707c478bd9Sstevel@tonic-gate }
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate dtrace_prog_t *
24737c478bd9Sstevel@tonic-gate dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
24747c478bd9Sstevel@tonic-gate     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
24777c478bd9Sstevel@tonic-gate 	    spec, NULL, cflags, argc, argv, NULL, s));
24787c478bd9Sstevel@tonic-gate }
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate dtrace_prog_t *
24817c478bd9Sstevel@tonic-gate dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
24827c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[])
24837c478bd9Sstevel@tonic-gate {
24847c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
24857c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
24867c478bd9Sstevel@tonic-gate }
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate int
24897c478bd9Sstevel@tonic-gate dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
24907c478bd9Sstevel@tonic-gate {
24917c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
24927c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
24937c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate int
24977c478bd9Sstevel@tonic-gate dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
25007c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
25017c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
25027c478bd9Sstevel@tonic-gate }
2503