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.
24*c3bd3abdSMatthew Ahrens  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25a386cc11SRobert Mustacchi  * Copyright (c) 2013, Joyent Inc. All rights reserved.
2667a4bb8fSGary Mills  * Copyright 2015 Gary Mills
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * DTrace D Language Compiler
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * The code in this source file implements the main engine for the D language
337c478bd9Sstevel@tonic-gate  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
347c478bd9Sstevel@tonic-gate  * compiler operates on either stdio FILEs or in-memory strings as its input
357c478bd9Sstevel@tonic-gate  * and can produce either dtrace_prog_t structures from a D program or a single
367c478bd9Sstevel@tonic-gate  * dtrace_difo_t structure from a D expression.  Multiple entry points are
377c478bd9Sstevel@tonic-gate  * provided as wrappers around dt_compile() for the various input/output pairs.
387c478bd9Sstevel@tonic-gate  * The compiler itself is implemented across the following source files:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * dt_lex.l - lex scanner
417c478bd9Sstevel@tonic-gate  * dt_grammar.y - yacc grammar
427c478bd9Sstevel@tonic-gate  * dt_parser.c - parse tree creation and semantic checking
437c478bd9Sstevel@tonic-gate  * dt_decl.c - declaration stack processing
447c478bd9Sstevel@tonic-gate  * dt_xlator.c - D translator lookup and creation
457c478bd9Sstevel@tonic-gate  * dt_ident.c - identifier and symbol table routines
467c478bd9Sstevel@tonic-gate  * dt_pragma.c - #pragma processing and D pragmas
477c478bd9Sstevel@tonic-gate  * dt_printf.c - D printf() and printa() argument checking and processing
487c478bd9Sstevel@tonic-gate  * dt_cc.c - compiler driver and dtrace_prog_t construction
497c478bd9Sstevel@tonic-gate  * dt_cg.c - DIF code generator
507c478bd9Sstevel@tonic-gate  * dt_as.c - DIF assembler
517c478bd9Sstevel@tonic-gate  * dt_dof.c - dtrace_prog_t -> DOF conversion
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Several other source files provide collections of utility routines used by
547c478bd9Sstevel@tonic-gate  * these major files.  The compiler itself is implemented in multiple passes:
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
577c478bd9Sstevel@tonic-gate  *     and parse tree nodes are constructed using the routines in dt_parser.c.
587c478bd9Sstevel@tonic-gate  *     This node construction pass is described further in dt_parser.c.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * (2) The parse tree is "cooked" by assigning each clause a context (see the
617c478bd9Sstevel@tonic-gate  *     routine dt_setcontext(), below) based on its probe description and then
627c478bd9Sstevel@tonic-gate  *     recursively descending the tree performing semantic checking.  The cook
637c478bd9Sstevel@tonic-gate  *     routines are also implemented in dt_parser.c and described there.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * (3) For actions that are DIF expression statements, the DIF code generator
667c478bd9Sstevel@tonic-gate  *     and assembler are invoked to create a finished DIFO for the statement.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * (4) The dtrace_prog_t data structures for the program clauses and actions
697c478bd9Sstevel@tonic-gate  *     are built, containing pointers to any DIFOs created in step (3).
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
727c478bd9Sstevel@tonic-gate  *     into DOF format for use in anonymous tracing or enabling in the kernel.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * In the implementation, steps 2-4 are intertwined in that they are performed
757c478bd9Sstevel@tonic-gate  * in order for each clause as part of a loop that executes over the clauses.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * The D compiler currently implements nearly no optimization.  The compiler
787c478bd9Sstevel@tonic-gate  * implements integer constant folding as part of pass (1), and a set of very
797c478bd9Sstevel@tonic-gate  * simple peephole optimizations as part of pass (3).  As with any C compiler,
807c478bd9Sstevel@tonic-gate  * a large number of optimizations are possible on both the intermediate data
817c478bd9Sstevel@tonic-gate  * structures and the generated DIF code.  These possibilities should be
827c478bd9Sstevel@tonic-gate  * investigated in the context of whether they will have any substantive effect
837c478bd9Sstevel@tonic-gate  * on the overall DTrace probe effect before they are undertaken.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <sys/types.h>
877c478bd9Sstevel@tonic-gate #include <sys/wait.h>
882b6389efSBryan Cantrill #include <sys/sysmacros.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <assert.h>
917c478bd9Sstevel@tonic-gate #include <strings.h>
927c478bd9Sstevel@tonic-gate #include <signal.h>
937c478bd9Sstevel@tonic-gate #include <unistd.h>
947c478bd9Sstevel@tonic-gate #include <stdlib.h>
957c478bd9Sstevel@tonic-gate #include <stdio.h>
967c478bd9Sstevel@tonic-gate #include <errno.h>
977c478bd9Sstevel@tonic-gate #include <ucontext.h>
987c478bd9Sstevel@tonic-gate #include <limits.h>
997c478bd9Sstevel@tonic-gate #include <ctype.h>
1007c478bd9Sstevel@tonic-gate #include <dirent.h>
1017c478bd9Sstevel@tonic-gate #include <dt_module.h>
1021a7c1b72Smws #include <dt_program.h>
1037c478bd9Sstevel@tonic-gate #include <dt_provider.h>
1047c478bd9Sstevel@tonic-gate #include <dt_printf.h>
1057c478bd9Sstevel@tonic-gate #include <dt_pid.h>
1067c478bd9Sstevel@tonic-gate #include <dt_grammar.h>
1077c478bd9Sstevel@tonic-gate #include <dt_ident.h>
1087c478bd9Sstevel@tonic-gate #include <dt_string.h>
1097c478bd9Sstevel@tonic-gate #include <dt_impl.h>
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static const dtrace_diftype_t dt_void_rtype = {
1127c478bd9Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static const dtrace_diftype_t dt_int_rtype = {
1167c478bd9Sstevel@tonic-gate 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
119c9d6cd77Sjhaslam static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120c9d6cd77Sjhaslam     uint_t, int, char *const[], FILE *, const char *);
121c9d6cd77Sjhaslam 
1227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1237c478bd9Sstevel@tonic-gate static int
dt_idreset(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)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
dt_idpragma(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)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 *
dt_stmt_create(dtrace_hdl_t * dtp,dtrace_ecbdesc_t * edp,dtrace_attribute_t descattr,dtrace_attribute_t stmtattr)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 *
dt_stmt_action(dtrace_hdl_t * dtp,dtrace_stmtdesc_t * sdp)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
dt_action_destructive(const dtrace_actdesc_t * ap)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
dt_stmt_append(dtrace_stmtdesc_t * sdp,const dt_node_t * dnp)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
dt_action_difconst(dtrace_actdesc_t * ap,uint_t id,dtrace_actkind_t kind)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
dt_action_clear(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)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
dt_action_normalize(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)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
dt_action_trunc(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)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
dt_action_printa(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)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
dt_action_printflike(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)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
dt_action_trace(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)6637c478bd9Sstevel@tonic-gate dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6647c478bd9Sstevel@tonic-gate {
665a386cc11SRobert Mustacchi 	int ctflib;
666a386cc11SRobert Mustacchi 
6677c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
668e5803b76SAdam H. Leventhal 	boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
669e5803b76SAdam H. Leventhal 	const char *act = istrace ?  "trace" : "print";
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (dt_node_is_void(dnp->dn_args)) {
672e5803b76SAdam H. Leventhal 		dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
673e5803b76SAdam H. Leventhal 		    "%s( ) may not be applied to a void expression\n", act);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
676e5803b76SAdam H. Leventhal 	if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
677e5803b76SAdam H. Leventhal 		dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
678e5803b76SAdam H. Leventhal 		    "%s( ) may not be applied to a translated pointer\n", act);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
681e98f46ccSAdam Leventhal 	if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
682e98f46ccSAdam Leventhal 		dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
683e98f46ccSAdam Leventhal 		    "%s( ) may not be applied to an aggregation%s\n", act,
684e98f46ccSAdam Leventhal 		    istrace ? "" : " -- did you mean printa()?");
685e98f46ccSAdam Leventhal 	}
686e98f46ccSAdam Leventhal 
6877c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
6887c478bd9Sstevel@tonic-gate 
689e5803b76SAdam H. Leventhal 	/*
690e5803b76SAdam H. Leventhal 	 * The print() action behaves identically to trace(), except that it
691e5803b76SAdam H. Leventhal 	 * stores the CTF type of the argument (if present) within the DOF for
692e5803b76SAdam H. Leventhal 	 * the DIFEXPR action.  To do this, we set the 'dtsd_strdata' to point
693e5803b76SAdam H. Leventhal 	 * to the fully-qualified CTF type ID for the result of the DIF
694e5803b76SAdam H. Leventhal 	 * action.  We use the ID instead of the name to handles complex types
695e5803b76SAdam H. Leventhal 	 * like arrays and function pointers that can't be resolved by
696e5803b76SAdam H. Leventhal 	 * ctf_type_lookup().  This is later processed by dtrace_dof_create()
697e5803b76SAdam H. Leventhal 	 * and turned into a reference into the string table so that we can
698a386cc11SRobert Mustacchi 	 * get the type information when we process the data after the fact.  In
699a386cc11SRobert Mustacchi 	 * the case where we are referring to userland CTF data, we also need to
700a386cc11SRobert Mustacchi 	 * to identify which ctf container in question we care about and encode
701a386cc11SRobert Mustacchi 	 * that within the name.
702e5803b76SAdam H. Leventhal 	 */
703e5803b76SAdam H. Leventhal 	if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
704e5803b76SAdam H. Leventhal 		dt_node_t *dret;
705e5803b76SAdam H. Leventhal 		size_t n;
706e5803b76SAdam H. Leventhal 		dt_module_t *dmp;
707deef35fdSEric Schrock 
708e5803b76SAdam H. Leventhal 		dret = yypcb->pcb_dret;
709e5803b76SAdam H. Leventhal 		dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
710deef35fdSEric Schrock 
711a386cc11SRobert Mustacchi 		if (dmp->dm_pid != 0) {
712a386cc11SRobert Mustacchi 			ctflib = dt_module_getlibid(dtp, dmp, dret->dn_ctfp);
713a386cc11SRobert Mustacchi 			assert(ctflib >= 0);
714a386cc11SRobert Mustacchi 			n = snprintf(NULL, 0, "%s`%d`%d", dmp->dm_name,
715a386cc11SRobert Mustacchi 			    ctflib, dret->dn_type) + 1;
716a386cc11SRobert Mustacchi 		} else {
717a386cc11SRobert Mustacchi 			n = snprintf(NULL, 0, "%s`%d", dmp->dm_name,
718a386cc11SRobert Mustacchi 			    dret->dn_type) + 1;
719a386cc11SRobert Mustacchi 		}
720e5803b76SAdam H. Leventhal 		sdp->dtsd_strdata = dt_alloc(dtp, n);
721e5803b76SAdam H. Leventhal 		if (sdp->dtsd_strdata == NULL)
722e5803b76SAdam H. Leventhal 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
723a386cc11SRobert Mustacchi 		if (dmp->dm_pid != 0) {
724a386cc11SRobert Mustacchi 			(void) snprintf(sdp->dtsd_strdata, n, "%s`%d`%d",
725a386cc11SRobert Mustacchi 			    dmp->dm_name, ctflib, dret->dn_type);
726a386cc11SRobert Mustacchi 		} else {
727a386cc11SRobert Mustacchi 			(void) snprintf(sdp->dtsd_strdata, n, "%s`%d",
728a386cc11SRobert Mustacchi 			    dmp->dm_name, dret->dn_type);
729a386cc11SRobert Mustacchi 		}
730deef35fdSEric Schrock 	}
731deef35fdSEric Schrock 
732deef35fdSEric Schrock 	ap->dtad_difo = dt_as(yypcb);
733deef35fdSEric Schrock 	ap->dtad_kind = DTRACEACT_DIFEXPR;
734deef35fdSEric Schrock }
735deef35fdSEric Schrock 
7367c478bd9Sstevel@tonic-gate static void
dt_action_tracemem(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)7377c478bd9Sstevel@tonic-gate dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	dt_node_t *addr = dnp->dn_args;
7421ea5f93dSBryan Cantrill 	dt_node_t *max = dnp->dn_args->dn_list;
7431ea5f93dSBryan Cantrill 	dt_node_t *size;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
7487c478bd9Sstevel@tonic-gate 		dnerror(addr, D_TRACEMEM_ADDR,
7497c478bd9Sstevel@tonic-gate 		    "tracemem( ) argument #1 is incompatible with "
7507c478bd9Sstevel@tonic-gate 		    "prototype:\n\tprototype: pointer or integer\n"
7517c478bd9Sstevel@tonic-gate 		    "\t argument: %s\n",
7527c478bd9Sstevel@tonic-gate 		    dt_node_type_name(addr, n, sizeof (n)));
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7551ea5f93dSBryan Cantrill 	if (dt_node_is_posconst(max) == 0) {
7561ea5f93dSBryan Cantrill 		dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
7577c478bd9Sstevel@tonic-gate 		    "be a non-zero positive integral constant expression\n");
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7601ea5f93dSBryan Cantrill 	if ((size = max->dn_list) != NULL) {
7611ea5f93dSBryan Cantrill 		if (size->dn_list != NULL) {
7621ea5f93dSBryan Cantrill 			dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
7631ea5f93dSBryan Cantrill 			    "mismatch: expected at most 3 args\n");
7641ea5f93dSBryan Cantrill 		}
7651ea5f93dSBryan Cantrill 
7661ea5f93dSBryan Cantrill 		if (!dt_node_is_scalar(size)) {
7671ea5f93dSBryan Cantrill 			dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
7681ea5f93dSBryan Cantrill 			    "dynamic size (argument #3) must be of "
7691ea5f93dSBryan Cantrill 			    "scalar type\n");
7701ea5f93dSBryan Cantrill 		}
7711ea5f93dSBryan Cantrill 
7721ea5f93dSBryan Cantrill 		dt_cg(yypcb, size);
7731ea5f93dSBryan Cantrill 		ap->dtad_difo = dt_as(yypcb);
7741ea5f93dSBryan Cantrill 		ap->dtad_difo->dtdo_rtype = dt_int_rtype;
7751ea5f93dSBryan Cantrill 		ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
7761ea5f93dSBryan Cantrill 
7771ea5f93dSBryan Cantrill 		ap = dt_stmt_action(dtp, sdp);
7781ea5f93dSBryan Cantrill 	}
7791ea5f93dSBryan Cantrill 
7807c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, addr);
7817c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
7821ea5f93dSBryan Cantrill 	ap->dtad_kind = DTRACEACT_TRACEMEM;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
7851ea5f93dSBryan Cantrill 	ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static void
dt_action_stack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * arg0)7897c478bd9Sstevel@tonic-gate dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STACK;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7947c478bd9Sstevel@tonic-gate 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7957c478bd9Sstevel@tonic-gate 	} else {
7967c478bd9Sstevel@tonic-gate 		ap->dtad_arg = 0;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
8007c478bd9Sstevel@tonic-gate 		if (arg0->dn_list != NULL) {
8017c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
8027c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		if (dt_node_is_posconst(arg0) == 0) {
8067c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
8077c478bd9Sstevel@tonic-gate 			    "non-zero positive integral constant expression\n");
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		ap->dtad_arg = arg0->dn_value;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static void
dt_action_stack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8157c478bd9Sstevel@tonic-gate dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8187c478bd9Sstevel@tonic-gate 	dt_action_stack_args(dtp, ap, dnp->dn_args);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate static void
dt_action_ustack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp)8227c478bd9Sstevel@tonic-gate dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	uint32_t nframes = 0;
8257c478bd9Sstevel@tonic-gate 	uint32_t strsize = 0;	/* default string table size */
8267c478bd9Sstevel@tonic-gate 	dt_node_t *arg0 = dnp->dn_args;
8277c478bd9Sstevel@tonic-gate 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
8307c478bd9Sstevel@tonic-gate 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
8337c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
8347c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
8377c478bd9Sstevel@tonic-gate 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_JSTACK;
8407c478bd9Sstevel@tonic-gate 	} else {
8417c478bd9Sstevel@tonic-gate 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
8447c478bd9Sstevel@tonic-gate 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 		ap->dtad_kind = DTRACEACT_USTACK;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (arg0 != NULL) {
8507c478bd9Sstevel@tonic-gate 		if (!dt_node_is_posconst(arg0)) {
8517c478bd9Sstevel@tonic-gate 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
8527c478bd9Sstevel@tonic-gate 			    "must be a non-zero positive integer constant\n");
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 		nframes = (uint32_t)arg0->dn_value;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (arg1 != NULL) {
8587c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT ||
8597c478bd9Sstevel@tonic-gate 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
8607c478bd9Sstevel@tonic-gate 		    (int64_t)arg1->dn_value < 0)) {
8617c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
8627c478bd9Sstevel@tonic-gate 			    "must be a positive integer constant\n");
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		if (arg1->dn_list != NULL) {
8667c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
8677c478bd9Sstevel@tonic-gate 			    "mismatch: too many arguments\n");
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		strsize = (uint32_t)arg1->dn_value;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate static void
dt_action_ustack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8777c478bd9Sstevel@tonic-gate dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8807c478bd9Sstevel@tonic-gate 	dt_action_ustack_args(dtp, ap, dnp);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
883a1b5e537Sbmc static void
dt_action_setopt(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)884a1b5e537Sbmc dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
885a1b5e537Sbmc {
886a1b5e537Sbmc 	dtrace_actdesc_t *ap;
887a1b5e537Sbmc 	dt_node_t *arg0, *arg1;
888a1b5e537Sbmc 
889a1b5e537Sbmc 	/*
890a1b5e537Sbmc 	 * The prototype guarantees that we are called with either one or
891a1b5e537Sbmc 	 * two arguments, and that any arguments that are present are strings.
892a1b5e537Sbmc 	 */
893a1b5e537Sbmc 	arg0 = dnp->dn_args;
894a1b5e537Sbmc 	arg1 = arg0->dn_list;
895a1b5e537Sbmc 
896a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
897a1b5e537Sbmc 	dt_cg(yypcb, arg0);
898a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
899a1b5e537Sbmc 	ap->dtad_kind = DTRACEACT_LIBACT;
900a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
901a1b5e537Sbmc 
902a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
903a1b5e537Sbmc 
904a1b5e537Sbmc 	if (arg1 == NULL) {
905a1b5e537Sbmc 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
906a1b5e537Sbmc 	} else {
907a1b5e537Sbmc 		dt_cg(yypcb, arg1);
908a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
909a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_LIBACT;
910a1b5e537Sbmc 	}
911a1b5e537Sbmc 
912a1b5e537Sbmc 	ap->dtad_arg = DT_ACT_SETOPT;
913a1b5e537Sbmc }
914a1b5e537Sbmc 
915a1b5e537Sbmc /*ARGSUSED*/
916a1b5e537Sbmc static void
dt_action_symmod_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp,dtrace_actkind_t kind)917a1b5e537Sbmc dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
918a1b5e537Sbmc     dt_node_t *dnp, dtrace_actkind_t kind)
919a1b5e537Sbmc {
920a1b5e537Sbmc 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
921a1b5e537Sbmc 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
922a1b5e537Sbmc 	    kind == DTRACEACT_UADDR);
923a1b5e537Sbmc 
924a1b5e537Sbmc 	dt_cg(yypcb, dnp);
925a1b5e537Sbmc 	ap->dtad_difo = dt_as(yypcb);
926a1b5e537Sbmc 	ap->dtad_kind = kind;
927a1b5e537Sbmc 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
928a1b5e537Sbmc }
929a1b5e537Sbmc 
930a1b5e537Sbmc static void
dt_action_symmod(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)931a1b5e537Sbmc dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
932a1b5e537Sbmc     dtrace_actkind_t kind)
933a1b5e537Sbmc {
934a1b5e537Sbmc 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
935a1b5e537Sbmc 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
936a1b5e537Sbmc }
937a1b5e537Sbmc 
9387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9397c478bd9Sstevel@tonic-gate static void
dt_action_ftruncate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9407c478bd9Sstevel@tonic-gate dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * Library actions need a DIFO that serves as an argument.  As
9467c478bd9Sstevel@tonic-gate 	 * ftruncate() doesn't take an argument, we generate the constant 0
9477c478bd9Sstevel@tonic-gate 	 * in a DIFO; this constant will be ignored when the ftruncate() is
9487c478bd9Sstevel@tonic-gate 	 * processed.
9497c478bd9Sstevel@tonic-gate 	 */
9507c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
9517c478bd9Sstevel@tonic-gate 	ap->dtad_arg = DT_ACT_FTRUNCATE;
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9557c478bd9Sstevel@tonic-gate static void
dt_action_stop(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9567c478bd9Sstevel@tonic-gate dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9577c478bd9Sstevel@tonic-gate {
9587c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_STOP;
9617c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9657c478bd9Sstevel@tonic-gate static void
dt_action_breakpoint(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9667c478bd9Sstevel@tonic-gate dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
9717c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9757c478bd9Sstevel@tonic-gate static void
dt_action_panic(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9767c478bd9Sstevel@tonic-gate dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_PANIC;
9817c478bd9Sstevel@tonic-gate 	ap->dtad_arg = 0;
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate static void
dt_action_chill(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9857c478bd9Sstevel@tonic-gate dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
9907c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
9917c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_CHILL;
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate static void
dt_action_raise(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9957c478bd9Sstevel@tonic-gate dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10007c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10017c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_RAISE;
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate static void
dt_action_exit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10057c478bd9Sstevel@tonic-gate dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10107c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10117c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_EXIT;
10127c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate static void
dt_action_speculate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10167c478bd9Sstevel@tonic-gate dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10217c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10227c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_SPECULATE;
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate static void
dt_action_commit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10267c478bd9Sstevel@tonic-gate dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10317c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10327c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_COMMIT;
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static void
dt_action_discard(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10367c478bd9Sstevel@tonic-gate dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_args);
10417c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
10427c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DISCARD;
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate static void
dt_compile_fun(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10467c478bd9Sstevel@tonic-gate dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	switch (dnp->dn_expr->dn_ident->di_id) {
10497c478bd9Sstevel@tonic-gate 	case DT_ACT_BREAKPOINT:
10507c478bd9Sstevel@tonic-gate 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
10517c478bd9Sstevel@tonic-gate 		break;
10527c478bd9Sstevel@tonic-gate 	case DT_ACT_CHILL:
10537c478bd9Sstevel@tonic-gate 		dt_action_chill(dtp, dnp->dn_expr, sdp);
10547c478bd9Sstevel@tonic-gate 		break;
10557c478bd9Sstevel@tonic-gate 	case DT_ACT_CLEAR:
10567c478bd9Sstevel@tonic-gate 		dt_action_clear(dtp, dnp->dn_expr, sdp);
10577c478bd9Sstevel@tonic-gate 		break;
10587c478bd9Sstevel@tonic-gate 	case DT_ACT_COMMIT:
10597c478bd9Sstevel@tonic-gate 		dt_action_commit(dtp, dnp->dn_expr, sdp);
10607c478bd9Sstevel@tonic-gate 		break;
10617c478bd9Sstevel@tonic-gate 	case DT_ACT_DENORMALIZE:
10627c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10637c478bd9Sstevel@tonic-gate 		break;
10647c478bd9Sstevel@tonic-gate 	case DT_ACT_DISCARD:
10657c478bd9Sstevel@tonic-gate 		dt_action_discard(dtp, dnp->dn_expr, sdp);
10667c478bd9Sstevel@tonic-gate 		break;
10677c478bd9Sstevel@tonic-gate 	case DT_ACT_EXIT:
10687c478bd9Sstevel@tonic-gate 		dt_action_exit(dtp, dnp->dn_expr, sdp);
10697c478bd9Sstevel@tonic-gate 		break;
10707c478bd9Sstevel@tonic-gate 	case DT_ACT_FREOPEN:
10717c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
10727c478bd9Sstevel@tonic-gate 		break;
10737c478bd9Sstevel@tonic-gate 	case DT_ACT_FTRUNCATE:
10747c478bd9Sstevel@tonic-gate 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
10757c478bd9Sstevel@tonic-gate 		break;
1076a1b5e537Sbmc 	case DT_ACT_MOD:
1077a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1078a1b5e537Sbmc 		break;
10797c478bd9Sstevel@tonic-gate 	case DT_ACT_NORMALIZE:
10807c478bd9Sstevel@tonic-gate 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10817c478bd9Sstevel@tonic-gate 		break;
10827c478bd9Sstevel@tonic-gate 	case DT_ACT_PANIC:
10837c478bd9Sstevel@tonic-gate 		dt_action_panic(dtp, dnp->dn_expr, sdp);
10847c478bd9Sstevel@tonic-gate 		break;
1085e5803b76SAdam H. Leventhal 	case DT_ACT_PRINT:
1086e5803b76SAdam H. Leventhal 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1087e5803b76SAdam H. Leventhal 		break;
10887c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTA:
10897c478bd9Sstevel@tonic-gate 		dt_action_printa(dtp, dnp->dn_expr, sdp);
10907c478bd9Sstevel@tonic-gate 		break;
10917c478bd9Sstevel@tonic-gate 	case DT_ACT_PRINTF:
10927c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
10937c478bd9Sstevel@tonic-gate 		break;
10947c478bd9Sstevel@tonic-gate 	case DT_ACT_RAISE:
10957c478bd9Sstevel@tonic-gate 		dt_action_raise(dtp, dnp->dn_expr, sdp);
10967c478bd9Sstevel@tonic-gate 		break;
1097a1b5e537Sbmc 	case DT_ACT_SETOPT:
1098a1b5e537Sbmc 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1099a1b5e537Sbmc 		break;
11007c478bd9Sstevel@tonic-gate 	case DT_ACT_SPECULATE:
11017c478bd9Sstevel@tonic-gate 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
11027c478bd9Sstevel@tonic-gate 		break;
11037c478bd9Sstevel@tonic-gate 	case DT_ACT_STACK:
11047c478bd9Sstevel@tonic-gate 		dt_action_stack(dtp, dnp->dn_expr, sdp);
11057c478bd9Sstevel@tonic-gate 		break;
11067c478bd9Sstevel@tonic-gate 	case DT_ACT_STOP:
11077c478bd9Sstevel@tonic-gate 		dt_action_stop(dtp, dnp->dn_expr, sdp);
11087c478bd9Sstevel@tonic-gate 		break;
1109a1b5e537Sbmc 	case DT_ACT_SYM:
1110a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1111a1b5e537Sbmc 		break;
11127c478bd9Sstevel@tonic-gate 	case DT_ACT_SYSTEM:
11137c478bd9Sstevel@tonic-gate 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
11147c478bd9Sstevel@tonic-gate 		break;
11157c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACE:
11167c478bd9Sstevel@tonic-gate 		dt_action_trace(dtp, dnp->dn_expr, sdp);
11177c478bd9Sstevel@tonic-gate 		break;
11187c478bd9Sstevel@tonic-gate 	case DT_ACT_TRACEMEM:
11197c478bd9Sstevel@tonic-gate 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
11207c478bd9Sstevel@tonic-gate 		break;
11217c478bd9Sstevel@tonic-gate 	case DT_ACT_TRUNC:
11227c478bd9Sstevel@tonic-gate 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
11237c478bd9Sstevel@tonic-gate 		break;
1124a1b5e537Sbmc 	case DT_ACT_UADDR:
1125a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1126a1b5e537Sbmc 		break;
1127a1b5e537Sbmc 	case DT_ACT_UMOD:
1128a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1129a1b5e537Sbmc 		break;
1130a1b5e537Sbmc 	case DT_ACT_USYM:
1131a1b5e537Sbmc 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1132a1b5e537Sbmc 		break;
11337c478bd9Sstevel@tonic-gate 	case DT_ACT_USTACK:
11347c478bd9Sstevel@tonic-gate 	case DT_ACT_JSTACK:
11357c478bd9Sstevel@tonic-gate 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
11367c478bd9Sstevel@tonic-gate 		break;
11377c478bd9Sstevel@tonic-gate 	default:
11387c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
11397c478bd9Sstevel@tonic-gate 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate static void
dt_compile_exp(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)11447c478bd9Sstevel@tonic-gate dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	dt_cg(yypcb, dnp->dn_expr);
11497c478bd9Sstevel@tonic-gate 	ap->dtad_difo = dt_as(yypcb);
11507c478bd9Sstevel@tonic-gate 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
11517c478bd9Sstevel@tonic-gate 	ap->dtad_kind = DTRACEACT_DIFEXPR;
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate static void
dt_compile_agg(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)11557c478bd9Sstevel@tonic-gate dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	dt_ident_t *aid, *fid;
1158a1b5e537Sbmc 	dt_node_t *anp, *incr = NULL;
11597c478bd9Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
1160a1b5e537Sbmc 	uint_t n = 1, argmax;
1161a1b5e537Sbmc 	uint64_t arg = 0;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * If the aggregation has no aggregating function applied to it, then
11657c478bd9Sstevel@tonic-gate 	 * this statement has no effect.  Flag this as a programming error.
11667c478bd9Sstevel@tonic-gate 	 */
11677c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun == NULL) {
11687c478bd9Sstevel@tonic-gate 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
11697c478bd9Sstevel@tonic-gate 		    dnp->dn_ident->di_name);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	aid = dnp->dn_ident;
11737c478bd9Sstevel@tonic-gate 	fid = dnp->dn_aggfun->dn_ident;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (dnp->dn_aggfun->dn_args != NULL &&
11767c478bd9Sstevel@tonic-gate 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
11777c478bd9Sstevel@tonic-gate 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
11787c478bd9Sstevel@tonic-gate 		    "be of scalar type\n", fid->di_name);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * The ID of the aggregation itself is implicitly recorded as the first
11837c478bd9Sstevel@tonic-gate 	 * member of each aggregation tuple so we can distinguish them later.
11847c478bd9Sstevel@tonic-gate 	 */
11857c478bd9Sstevel@tonic-gate 	ap = dt_stmt_action(dtp, sdp);
11867c478bd9Sstevel@tonic-gate 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
11897c478bd9Sstevel@tonic-gate 		ap = dt_stmt_action(dtp, sdp);
11907c478bd9Sstevel@tonic-gate 		n++;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		if (anp->dn_kind == DT_NODE_FUNC) {
11937c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
11947c478bd9Sstevel@tonic-gate 				dt_action_stack_args(dtp, ap, anp->dn_args);
11957c478bd9Sstevel@tonic-gate 				continue;
11967c478bd9Sstevel@tonic-gate 			}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
11997c478bd9Sstevel@tonic-gate 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
12007c478bd9Sstevel@tonic-gate 				dt_action_ustack_args(dtp, ap, anp);
12017c478bd9Sstevel@tonic-gate 				continue;
12027c478bd9Sstevel@tonic-gate 			}
12037c478bd9Sstevel@tonic-gate 
1204a1b5e537Sbmc 			switch (anp->dn_ident->di_id) {
1205a1b5e537Sbmc 			case DT_ACT_UADDR:
1206a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1207a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UADDR);
1208a1b5e537Sbmc 				continue;
12097c478bd9Sstevel@tonic-gate 
1210a1b5e537Sbmc 			case DT_ACT_USYM:
1211a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1212a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_USYM);
1213a1b5e537Sbmc 				continue;
12147c478bd9Sstevel@tonic-gate 
1215a1b5e537Sbmc 			case DT_ACT_UMOD:
1216a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1217a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_UMOD);
1218a1b5e537Sbmc 				continue;
12197c478bd9Sstevel@tonic-gate 
1220a1b5e537Sbmc 			case DT_ACT_SYM:
1221a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1222a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_SYM);
1223a1b5e537Sbmc 				continue;
1224a1b5e537Sbmc 
1225a1b5e537Sbmc 			case DT_ACT_MOD:
1226a1b5e537Sbmc 				dt_action_symmod_args(dtp, ap,
1227a1b5e537Sbmc 				    anp->dn_args, DTRACEACT_MOD);
1228a1b5e537Sbmc 				continue;
1229a1b5e537Sbmc 
1230a1b5e537Sbmc 			default:
1231a1b5e537Sbmc 				break;
1232a1b5e537Sbmc 			}
1233a1b5e537Sbmc 		}
1234a1b5e537Sbmc 
1235a1b5e537Sbmc 		dt_cg(yypcb, anp);
12367c478bd9Sstevel@tonic-gate 		ap->dtad_difo = dt_as(yypcb);
1237a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
12417c478bd9Sstevel@tonic-gate 		/*
1242a1b5e537Sbmc 		 * For linear quantization, we have between two and four
1243a1b5e537Sbmc 		 * arguments in addition to the expression:
12447c478bd9Sstevel@tonic-gate 		 *
12457c478bd9Sstevel@tonic-gate 		 *    arg1 => Base value
12467c478bd9Sstevel@tonic-gate 		 *    arg2 => Limit value
12477c478bd9Sstevel@tonic-gate 		 *    arg3 => Quantization level step size (defaults to 1)
1248a1b5e537Sbmc 		 *    arg4 => Quantization increment value (defaults to 1)
12497c478bd9Sstevel@tonic-gate 		 */
12507c478bd9Sstevel@tonic-gate 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
12517c478bd9Sstevel@tonic-gate 		dt_node_t *arg2 = arg1->dn_list;
12527c478bd9Sstevel@tonic-gate 		dt_node_t *arg3 = arg2->dn_list;
125330ef842dSbmc 		dt_idsig_t *isp;
125430ef842dSbmc 		uint64_t nlevels, step = 1, oarg;
12557c478bd9Sstevel@tonic-gate 		int64_t baseval, limitval;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		if (arg1->dn_kind != DT_NODE_INT) {
12587c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
12597c478bd9Sstevel@tonic-gate 			    "argument #1 must be an integer constant\n");
12607c478bd9Sstevel@tonic-gate 		}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		baseval = (int64_t)arg1->dn_value;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
12657c478bd9Sstevel@tonic-gate 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
12667c478bd9Sstevel@tonic-gate 			    "argument #1 must be a 32-bit quantity\n");
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		if (arg2->dn_kind != DT_NODE_INT) {
12707c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
12717c478bd9Sstevel@tonic-gate 			    "argument #2 must be an integer constant\n");
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		limitval = (int64_t)arg2->dn_value;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
12777c478bd9Sstevel@tonic-gate 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
12787c478bd9Sstevel@tonic-gate 			    "argument #2 must be a 32-bit quantity\n");
12797c478bd9Sstevel@tonic-gate 		}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		if (limitval < baseval) {
12827c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_MISMATCH,
12837c478bd9Sstevel@tonic-gate 			    "lquantize( ) base (argument #1) must be less "
12847c478bd9Sstevel@tonic-gate 			    "than limit (argument #2)\n");
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		if (arg3 != NULL) {
12887c478bd9Sstevel@tonic-gate 			if (!dt_node_is_posconst(arg3)) {
12897c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
12907c478bd9Sstevel@tonic-gate 				    "argument #3 must be a non-zero positive "
12917c478bd9Sstevel@tonic-gate 				    "integer constant\n");
12927c478bd9Sstevel@tonic-gate 			}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 			if ((step = arg3->dn_value) > UINT16_MAX) {
12957c478bd9Sstevel@tonic-gate 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
12967c478bd9Sstevel@tonic-gate 				    "argument #3 must be a 16-bit quantity\n");
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 		}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 		nlevels = (limitval - baseval) / step;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 		if (nlevels == 0) {
13037c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPLARGE,
13047c478bd9Sstevel@tonic-gate 			    "lquantize( ) step (argument #3) too large: must "
13057c478bd9Sstevel@tonic-gate 			    "have at least one quantization level\n");
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		if (nlevels > UINT16_MAX) {
13097c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
13107c478bd9Sstevel@tonic-gate 			    "(argument #3) too small: number of quantization "
13117c478bd9Sstevel@tonic-gate 			    "levels must be a 16-bit quantity\n");
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 
1314a1b5e537Sbmc 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
13157c478bd9Sstevel@tonic-gate 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
13167c478bd9Sstevel@tonic-gate 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
13177c478bd9Sstevel@tonic-gate 		    DTRACE_LQUANTIZE_BASEMASK);
1318a1b5e537Sbmc 
131930ef842dSbmc 		assert(arg != 0);
132030ef842dSbmc 
132130ef842dSbmc 		isp = (dt_idsig_t *)aid->di_data;
132230ef842dSbmc 
132330ef842dSbmc 		if (isp->dis_auxinfo == 0) {
132430ef842dSbmc 			/*
132530ef842dSbmc 			 * This is the first time we've seen an lquantize()
132630ef842dSbmc 			 * for this aggregation; we'll store our argument
132730ef842dSbmc 			 * as the auxiliary signature information.
132830ef842dSbmc 			 */
132930ef842dSbmc 			isp->dis_auxinfo = arg;
133030ef842dSbmc 		} else if ((oarg = isp->dis_auxinfo) != arg) {
133130ef842dSbmc 			/*
133230ef842dSbmc 			 * If we have seen this lquantize() before and the
133330ef842dSbmc 			 * argument doesn't match the original argument, pick
133430ef842dSbmc 			 * the original argument apart to concisely report the
133530ef842dSbmc 			 * mismatch.
133630ef842dSbmc 			 */
133730ef842dSbmc 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
133830ef842dSbmc 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
133930ef842dSbmc 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
134030ef842dSbmc 
134130ef842dSbmc 			if (obaseval != baseval) {
134230ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
134330ef842dSbmc 				    "base (argument #1) doesn't match previous "
134430ef842dSbmc 				    "declaration: expected %d, found %d\n",
134530ef842dSbmc 				    obaseval, (int)baseval);
134630ef842dSbmc 			}
134730ef842dSbmc 
134830ef842dSbmc 			if (onlevels * ostep != nlevels * step) {
134930ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
135030ef842dSbmc 				    "limit (argument #2) doesn't match previous"
135130ef842dSbmc 				    " declaration: expected %d, found %d\n",
135230ef842dSbmc 				    obaseval + onlevels * ostep,
135330ef842dSbmc 				    (int)baseval + (int)nlevels * (int)step);
135430ef842dSbmc 			}
135530ef842dSbmc 
135630ef842dSbmc 			if (ostep != step) {
135730ef842dSbmc 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
135830ef842dSbmc 				    "step (argument #3) doesn't match previous "
135930ef842dSbmc 				    "declaration: expected %d, found %d\n",
136030ef842dSbmc 				    ostep, (int)step);
136130ef842dSbmc 			}
136230ef842dSbmc 
136330ef842dSbmc 			/*
136430ef842dSbmc 			 * We shouldn't be able to get here -- one of the
136530ef842dSbmc 			 * parameters must be mismatched if the arguments
136630ef842dSbmc 			 * didn't match.
136730ef842dSbmc 			 */
136830ef842dSbmc 			assert(0);
136930ef842dSbmc 		}
137030ef842dSbmc 
1371a1b5e537Sbmc 		incr = arg3 != NULL ? arg3->dn_list : NULL;
1372a1b5e537Sbmc 		argmax = 5;
1373a1b5e537Sbmc 	}
1374a1b5e537Sbmc 
13752b6389efSBryan Cantrill 	if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
13762b6389efSBryan Cantrill 		/*
13772b6389efSBryan Cantrill 		 * For log/linear quantizations, we have between one and five
13782b6389efSBryan Cantrill 		 * arguments in addition to the expression:
13792b6389efSBryan Cantrill 		 *
13802b6389efSBryan Cantrill 		 *    arg1 => Factor
13812b6389efSBryan Cantrill 		 *    arg2 => Low magnitude
13822b6389efSBryan Cantrill 		 *    arg3 => High magnitude
13832b6389efSBryan Cantrill 		 *    arg4 => Number of steps per magnitude
13842b6389efSBryan Cantrill 		 *    arg5 => Quantization increment value (defaults to 1)
13852b6389efSBryan Cantrill 		 */
13862b6389efSBryan Cantrill 		dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
13872b6389efSBryan Cantrill 		uint64_t oarg, order, v;
13882b6389efSBryan Cantrill 		dt_idsig_t *isp;
13892b6389efSBryan Cantrill 		int i;
13902b6389efSBryan Cantrill 
13912b6389efSBryan Cantrill 		struct {
13922b6389efSBryan Cantrill 			char *str;		/* string identifier */
13932b6389efSBryan Cantrill 			int badtype;		/* error on bad type */
13942b6389efSBryan Cantrill 			int badval;		/* error on bad value */
13952b6389efSBryan Cantrill 			int mismatch;		/* error on bad match */
13962b6389efSBryan Cantrill 			int shift;		/* shift value */
13972b6389efSBryan Cantrill 			uint16_t value;		/* value itself */
13982b6389efSBryan Cantrill 		} args[] = {
13992b6389efSBryan Cantrill 			{ "factor", D_LLQUANT_FACTORTYPE,
14002b6389efSBryan Cantrill 			    D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
14012b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_FACTORSHIFT },
14022b6389efSBryan Cantrill 			{ "low magnitude", D_LLQUANT_LOWTYPE,
14032b6389efSBryan Cantrill 			    D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
14042b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_LOWSHIFT },
14052b6389efSBryan Cantrill 			{ "high magnitude", D_LLQUANT_HIGHTYPE,
14062b6389efSBryan Cantrill 			    D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
14072b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_HIGHSHIFT },
14082b6389efSBryan Cantrill 			{ "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
14092b6389efSBryan Cantrill 			    D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
14102b6389efSBryan Cantrill 			    DTRACE_LLQUANTIZE_NSTEPSHIFT },
14112b6389efSBryan Cantrill 			{ NULL }
14122b6389efSBryan Cantrill 		};
14132b6389efSBryan Cantrill 
14142b6389efSBryan Cantrill 		assert(arg == 0);
14152b6389efSBryan Cantrill 
14162b6389efSBryan Cantrill 		for (i = 0; args[i].str != NULL; i++) {
14172b6389efSBryan Cantrill 			if (llarg->dn_kind != DT_NODE_INT) {
14182b6389efSBryan Cantrill 				dnerror(llarg, args[i].badtype, "llquantize( ) "
14192b6389efSBryan Cantrill 				    "argument #%d (%s) must be an "
14202b6389efSBryan Cantrill 				    "integer constant\n", i + 1, args[i].str);
14212b6389efSBryan Cantrill 			}
14222b6389efSBryan Cantrill 
14232b6389efSBryan Cantrill 			if ((uint64_t)llarg->dn_value > UINT16_MAX) {
14242b6389efSBryan Cantrill 				dnerror(llarg, args[i].badval, "llquantize( ) "
14252b6389efSBryan Cantrill 				    "argument #%d (%s) must be an unsigned "
14262b6389efSBryan Cantrill 				    "16-bit quantity\n", i + 1, args[i].str);
14272b6389efSBryan Cantrill 			}
14282b6389efSBryan Cantrill 
14292b6389efSBryan Cantrill 			args[i].value = (uint16_t)llarg->dn_value;
14302b6389efSBryan Cantrill 
14312b6389efSBryan Cantrill 			assert(!(arg & (UINT16_MAX << args[i].shift)));
14322b6389efSBryan Cantrill 			arg |= ((uint64_t)args[i].value << args[i].shift);
14332b6389efSBryan Cantrill 			llarg = llarg->dn_list;
14342b6389efSBryan Cantrill 		}
14352b6389efSBryan Cantrill 
14362b6389efSBryan Cantrill 		assert(arg != 0);
14372b6389efSBryan Cantrill 
14382b6389efSBryan Cantrill 		if (args[0].value < 2) {
14392b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
14402b6389efSBryan Cantrill 			    "factor (argument #1) must be two or more\n");
14412b6389efSBryan Cantrill 		}
14422b6389efSBryan Cantrill 
14432b6389efSBryan Cantrill 		if (args[1].value >= args[2].value) {
14442b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
14452b6389efSBryan Cantrill 			    "high magnitude (argument #3) must be greater "
14462b6389efSBryan Cantrill 			    "than low magnitude (argument #2)\n");
14472b6389efSBryan Cantrill 		}
14482b6389efSBryan Cantrill 
14492b6389efSBryan Cantrill 		if (args[3].value < args[0].value) {
14502b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
14512b6389efSBryan Cantrill 			    "factor (argument #1) must be less than or "
14522b6389efSBryan Cantrill 			    "equal to the number of linear steps per "
14532b6389efSBryan Cantrill 			    "magnitude (argument #4)\n");
14542b6389efSBryan Cantrill 		}
14552b6389efSBryan Cantrill 
14562b6389efSBryan Cantrill 		for (v = args[0].value; v < args[3].value; v *= args[0].value)
14572b6389efSBryan Cantrill 			continue;
14582b6389efSBryan Cantrill 
14592b6389efSBryan Cantrill 		if ((args[3].value % args[0].value) || (v % args[3].value)) {
14602b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
14612b6389efSBryan Cantrill 			    "factor (argument #1) must evenly divide the "
14622b6389efSBryan Cantrill 			    "number of steps per magnitude (argument #4), "
14632b6389efSBryan Cantrill 			    "and the number of steps per magnitude must evenly "
14642b6389efSBryan Cantrill 			    "divide a power of the factor\n");
14652b6389efSBryan Cantrill 		}
14662b6389efSBryan Cantrill 
14672b6389efSBryan Cantrill 		for (i = 0, order = 1; i < args[2].value; i++) {
14682b6389efSBryan Cantrill 			if (order * args[0].value > order) {
14692b6389efSBryan Cantrill 				order *= args[0].value;
14702b6389efSBryan Cantrill 				continue;
14712b6389efSBryan Cantrill 			}
14722b6389efSBryan Cantrill 
14732b6389efSBryan Cantrill 			dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
14742b6389efSBryan Cantrill 			    "factor (%d) raised to power of high magnitude "
14752b6389efSBryan Cantrill 			    "(%d) overflows 64-bits\n", args[0].value,
14762b6389efSBryan Cantrill 			    args[2].value);
14772b6389efSBryan Cantrill 		}
14782b6389efSBryan Cantrill 
14792b6389efSBryan Cantrill 		isp = (dt_idsig_t *)aid->di_data;
14802b6389efSBryan Cantrill 
14812b6389efSBryan Cantrill 		if (isp->dis_auxinfo == 0) {
14822b6389efSBryan Cantrill 			/*
14832b6389efSBryan Cantrill 			 * This is the first time we've seen an llquantize()
14842b6389efSBryan Cantrill 			 * for this aggregation; we'll store our argument
14852b6389efSBryan Cantrill 			 * as the auxiliary signature information.
14862b6389efSBryan Cantrill 			 */
14872b6389efSBryan Cantrill 			isp->dis_auxinfo = arg;
14882b6389efSBryan Cantrill 		} else if ((oarg = isp->dis_auxinfo) != arg) {
14892b6389efSBryan Cantrill 			/*
14902b6389efSBryan Cantrill 			 * If we have seen this llquantize() before and the
14912b6389efSBryan Cantrill 			 * argument doesn't match the original argument, pick
14922b6389efSBryan Cantrill 			 * the original argument apart to concisely report the
14932b6389efSBryan Cantrill 			 * mismatch.
14942b6389efSBryan Cantrill 			 */
14952b6389efSBryan Cantrill 			int expected = 0, found = 0;
14962b6389efSBryan Cantrill 
14972b6389efSBryan Cantrill 			for (i = 0; expected == found; i++) {
14982b6389efSBryan Cantrill 				assert(args[i].str != NULL);
14992b6389efSBryan Cantrill 
15002b6389efSBryan Cantrill 				expected = (oarg >> args[i].shift) & UINT16_MAX;
15012b6389efSBryan Cantrill 				found = (arg >> args[i].shift) & UINT16_MAX;
15022b6389efSBryan Cantrill 			}
15032b6389efSBryan Cantrill 
15042b6389efSBryan Cantrill 			dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
15052b6389efSBryan Cantrill 			    "%s (argument #%d) doesn't match previous "
15062b6389efSBryan Cantrill 			    "declaration: expected %d, found %d\n",
15072b6389efSBryan Cantrill 			    args[i - 1].str, i, expected, found);
15082b6389efSBryan Cantrill 		}
15092b6389efSBryan Cantrill 
15102b6389efSBryan Cantrill 		incr = llarg;
15112b6389efSBryan Cantrill 		argmax = 6;
15122b6389efSBryan Cantrill 	}
15132b6389efSBryan Cantrill 
1514a1b5e537Sbmc 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1515a1b5e537Sbmc 		incr = dnp->dn_aggfun->dn_args->dn_list;
1516a1b5e537Sbmc 		argmax = 2;
1517a1b5e537Sbmc 	}
1518a1b5e537Sbmc 
1519a1b5e537Sbmc 	if (incr != NULL) {
1520a1b5e537Sbmc 		if (!dt_node_is_scalar(incr)) {
1521a1b5e537Sbmc 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1522a1b5e537Sbmc 			    "(argument #%d) must be of scalar type\n",
1523a1b5e537Sbmc 			    fid->di_name, argmax);
1524a1b5e537Sbmc 		}
1525a1b5e537Sbmc 
1526a1b5e537Sbmc 		if ((anp = incr->dn_list) != NULL) {
1527a1b5e537Sbmc 			int argc = argmax;
1528a1b5e537Sbmc 
1529a1b5e537Sbmc 			for (; anp != NULL; anp = anp->dn_list)
1530a1b5e537Sbmc 				argc++;
1531a1b5e537Sbmc 
1532a1b5e537Sbmc 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1533a1b5e537Sbmc 			    "mismatch: %d args passed, at most %d expected",
1534a1b5e537Sbmc 			    fid->di_name, argc, argmax);
1535a1b5e537Sbmc 		}
1536a1b5e537Sbmc 
1537a1b5e537Sbmc 		ap = dt_stmt_action(dtp, sdp);
1538a1b5e537Sbmc 		n++;
1539a1b5e537Sbmc 
1540a1b5e537Sbmc 		dt_cg(yypcb, incr);
1541a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
1542a1b5e537Sbmc 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1543a1b5e537Sbmc 		ap->dtad_kind = DTRACEACT_DIFEXPR;
1544a1b5e537Sbmc 	}
1545a1b5e537Sbmc 
1546a1b5e537Sbmc 	assert(sdp->dtsd_aggdata == NULL);
1547a1b5e537Sbmc 	sdp->dtsd_aggdata = aid;
1548a1b5e537Sbmc 
1549a1b5e537Sbmc 	ap = dt_stmt_action(dtp, sdp);
1550a1b5e537Sbmc 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1551a1b5e537Sbmc 	assert(DTRACEACT_ISAGG(fid->di_id));
1552a1b5e537Sbmc 	ap->dtad_kind = fid->di_id;
1553a1b5e537Sbmc 	ap->dtad_ntuple = n;
1554a1b5e537Sbmc 	ap->dtad_arg = arg;
1555a1b5e537Sbmc 
1556a1b5e537Sbmc 	if (dnp->dn_aggfun->dn_args != NULL) {
1557a1b5e537Sbmc 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1558a1b5e537Sbmc 		ap->dtad_difo = dt_as(yypcb);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate static void
dt_compile_one_clause(dtrace_hdl_t * dtp,dt_node_t * cnp,dt_node_t * pnp)15637c478bd9Sstevel@tonic-gate dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
15667c478bd9Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
15677c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	yylineno = pnp->dn_line;
15707c478bd9Sstevel@tonic-gate 	dt_setcontext(dtp, pnp->dn_desc);
15717c478bd9Sstevel@tonic-gate 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	if (DT_TREEDUMP_PASS(dtp, 2))
15747c478bd9Sstevel@tonic-gate 		dt_node_printr(cnp, stderr, 0);
15757c478bd9Sstevel@tonic-gate 
15761a7c1b72Smws 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
15777c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == NULL);
15807c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = edp;
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	if (cnp->dn_pred != NULL) {
15837c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, cnp->dn_pred);
15847c478bd9Sstevel@tonic-gate 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	if (cnp->dn_acts == NULL) {
15887c478bd9Sstevel@tonic-gate 		dt_stmt_append(dt_stmt_create(dtp, edp,
15897c478bd9Sstevel@tonic-gate 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
15937c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == NULL);
15947c478bd9Sstevel@tonic-gate 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 		switch (dnp->dn_kind) {
15977c478bd9Sstevel@tonic-gate 		case DT_NODE_DEXPR:
15987c478bd9Sstevel@tonic-gate 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
15997c478bd9Sstevel@tonic-gate 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
16007c478bd9Sstevel@tonic-gate 			else
16017c478bd9Sstevel@tonic-gate 				dt_compile_exp(dtp, dnp, sdp);
16027c478bd9Sstevel@tonic-gate 			break;
16037c478bd9Sstevel@tonic-gate 		case DT_NODE_DFUNC:
16047c478bd9Sstevel@tonic-gate 			dt_compile_fun(dtp, dnp, sdp);
16057c478bd9Sstevel@tonic-gate 			break;
16067c478bd9Sstevel@tonic-gate 		case DT_NODE_AGG:
16077c478bd9Sstevel@tonic-gate 			dt_compile_agg(dtp, dnp, sdp);
16087c478bd9Sstevel@tonic-gate 			break;
16097c478bd9Sstevel@tonic-gate 		default:
16107c478bd9Sstevel@tonic-gate 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
16117c478bd9Sstevel@tonic-gate 			    "%u is not a valid statement\n", dnp->dn_kind);
16127c478bd9Sstevel@tonic-gate 		}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_stmt == sdp);
16157c478bd9Sstevel@tonic-gate 		dt_stmt_append(sdp, dnp);
16167c478bd9Sstevel@tonic-gate 	}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	assert(yypcb->pcb_ecbdesc == edp);
16191a7c1b72Smws 	dt_ecbdesc_release(dtp, edp);
16207c478bd9Sstevel@tonic-gate 	dt_endcontext(dtp);
16217c478bd9Sstevel@tonic-gate 	yypcb->pcb_ecbdesc = NULL;
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate static void
dt_compile_clause(dtrace_hdl_t * dtp,dt_node_t * cnp)16257c478bd9Sstevel@tonic-gate dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	dt_node_t *pnp;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
16307c478bd9Sstevel@tonic-gate 		dt_compile_one_clause(dtp, cnp, pnp);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate 
16331a7c1b72Smws static void
dt_compile_xlator(dt_node_t * dnp)16341a7c1b72Smws dt_compile_xlator(dt_node_t *dnp)
16351a7c1b72Smws {
16361a7c1b72Smws 	dt_xlator_t *dxp = dnp->dn_xlator;
16371a7c1b72Smws 	dt_node_t *mnp;
16381a7c1b72Smws 
16391a7c1b72Smws 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
16401a7c1b72Smws 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
16411a7c1b72Smws 		dt_cg(yypcb, mnp);
16421a7c1b72Smws 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
16431a7c1b72Smws 	}
16441a7c1b72Smws }
16451a7c1b72Smws 
16467c478bd9Sstevel@tonic-gate void
dt_setcontext(dtrace_hdl_t * dtp,dtrace_probedesc_t * pdp)16477c478bd9Sstevel@tonic-gate dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	const dtrace_pattr_t *pap;
16507c478bd9Sstevel@tonic-gate 	dt_probe_t *prp;
1651c9d6cd77Sjhaslam 	dt_provider_t *pvp;
16527c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
16537c478bd9Sstevel@tonic-gate 	char attrstr[8];
16547c478bd9Sstevel@tonic-gate 	int err;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	/*
1657c9d6cd77Sjhaslam 	 * Both kernel and pid based providers are allowed to have names
1658c9d6cd77Sjhaslam 	 * ending with what could be interpreted as a number. We assume it's
1659c9d6cd77Sjhaslam 	 * a pid and that we may need to dynamically create probes for
1660c9d6cd77Sjhaslam 	 * that process if:
1661c9d6cd77Sjhaslam 	 *
1662c9d6cd77Sjhaslam 	 * (1) The provider doesn't exist, or,
1663c9d6cd77Sjhaslam 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1664c9d6cd77Sjhaslam 	 *
1665c9d6cd77Sjhaslam 	 * On an error, dt_pid_create_probes() will set the error message
1666c9d6cd77Sjhaslam 	 * and tag -- we just have to longjmp() out of here.
16677c478bd9Sstevel@tonic-gate 	 */
1668900524f3Sahl 	if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1669c9d6cd77Sjhaslam 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1670c9d6cd77Sjhaslam 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1671900524f3Sahl 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1672900524f3Sahl 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1673900524f3Sahl 	}
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	/*
16767c478bd9Sstevel@tonic-gate 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
16777c478bd9Sstevel@tonic-gate 	 * a representative probe is found, set 'pap' to the probe provider's
16787c478bd9Sstevel@tonic-gate 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
16797c478bd9Sstevel@tonic-gate 	 */
16807c478bd9Sstevel@tonic-gate 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
16817c478bd9Sstevel@tonic-gate 		pap = &_dtrace_prvdesc;
16827c478bd9Sstevel@tonic-gate 		err = dtrace_errno(dtp);
16837c478bd9Sstevel@tonic-gate 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
16847c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
16857c478bd9Sstevel@tonic-gate 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
16867c478bd9Sstevel@tonic-gate 	} else {
16877c478bd9Sstevel@tonic-gate 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
16887c478bd9Sstevel@tonic-gate 		err = 0;
16897c478bd9Sstevel@tonic-gate 	}
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
16927c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
16937c478bd9Sstevel@tonic-gate 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
16947c478bd9Sstevel@tonic-gate 		    pdp->dtpd_func, pdp->dtpd_name);
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
16987c478bd9Sstevel@tonic-gate 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
17017c478bd9Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
17027c478bd9Sstevel@tonic-gate 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
17037c478bd9Sstevel@tonic-gate 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * Reset the stability attributes of D global variables that vary
17077c478bd9Sstevel@tonic-gate 	 * based on the attributes of the provider and context itself.
17087c478bd9Sstevel@tonic-gate 	 */
17097c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
17107c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_provider;
17117c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
17127c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_mod;
17137c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
17147c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_func;
17157c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
17167c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_name;
17177c478bd9Sstevel@tonic-gate 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
17187c478bd9Sstevel@tonic-gate 		idp->di_attr = pap->dtpa_args;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = pdp;
17217c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = prp;
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate /*
17257c478bd9Sstevel@tonic-gate  * Reset context-dependent variables and state at the end of cooking a D probe
17267c478bd9Sstevel@tonic-gate  * definition clause.  This ensures that external declarations between clauses
17277c478bd9Sstevel@tonic-gate  * do not reference any stale context-dependent data from the previous clause.
17287c478bd9Sstevel@tonic-gate  */
17297c478bd9Sstevel@tonic-gate void
dt_endcontext(dtrace_hdl_t * dtp)17307c478bd9Sstevel@tonic-gate dt_endcontext(dtrace_hdl_t *dtp)
17317c478bd9Sstevel@tonic-gate {
17327c478bd9Sstevel@tonic-gate 	static const char *const cvars[] = {
17337c478bd9Sstevel@tonic-gate 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
17347c478bd9Sstevel@tonic-gate 	};
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
17377c478bd9Sstevel@tonic-gate 	int i;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	for (i = 0; cvars[i] != NULL; i++) {
17407c478bd9Sstevel@tonic-gate 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
17417c478bd9Sstevel@tonic-gate 			idp->di_attr = _dtrace_defattr;
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	yypcb->pcb_pdesc = NULL;
17457c478bd9Sstevel@tonic-gate 	yypcb->pcb_probe = NULL;
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate static int
dt_reduceid(dt_idhash_t * dhp,dt_ident_t * idp,dtrace_hdl_t * dtp)17497c478bd9Sstevel@tonic-gate dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
17527c478bd9Sstevel@tonic-gate 		dt_idhash_delete(dhp, idp);
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	return (0);
17557c478bd9Sstevel@tonic-gate }
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate /*
17587c478bd9Sstevel@tonic-gate  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
17597c478bd9Sstevel@tonic-gate  * any identifiers or translators that have been previously defined as bound to
17607c478bd9Sstevel@tonic-gate  * a version greater than the specified version.  Therefore, in our current
17617c478bd9Sstevel@tonic-gate  * version implementation, establishing a binding is a one-way transformation.
17627c478bd9Sstevel@tonic-gate  * In addition, no versioning is currently provided for types as our .d library
17637c478bd9Sstevel@tonic-gate  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
17647c478bd9Sstevel@tonic-gate  * for our exclusive use.  If required, type versioning will require more work.
17657c478bd9Sstevel@tonic-gate  */
17667c478bd9Sstevel@tonic-gate int
dt_reduce(dtrace_hdl_t * dtp,dt_version_t v)17677c478bd9Sstevel@tonic-gate dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	char s[DT_VERSION_STRMAX];
17707c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp, *nxp;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (v > dtp->dt_vmax)
17737c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
17747c478bd9Sstevel@tonic-gate 	else if (v == dtp->dt_vmax)
17757c478bd9Sstevel@tonic-gate 		return (0); /* no reduction necessary */
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	dt_dprintf("reducing api version to %s\n",
17787c478bd9Sstevel@tonic-gate 	    dt_version_num2str(v, s, sizeof (s)));
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	dtp->dt_vmax = v;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
17837c478bd9Sstevel@tonic-gate 		nxp = dt_list_next(dxp);
17847c478bd9Sstevel@tonic-gate 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
17857c478bd9Sstevel@tonic-gate 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
17867c478bd9Sstevel@tonic-gate 			dt_list_delete(&dtp->dt_xlators, dxp);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
17907c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
17917c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
17927c478bd9Sstevel@tonic-gate 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	return (0);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate /*
17987c478bd9Sstevel@tonic-gate  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
17997c478bd9Sstevel@tonic-gate  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
18007c478bd9Sstevel@tonic-gate  * here to simplify the code by leveraging file descriptor inheritance.
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate static FILE *
dt_preproc(dtrace_hdl_t * dtp,FILE * ifp)18037c478bd9Sstevel@tonic-gate dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	int argc = dtp->dt_cpp_argc;
18067c478bd9Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (argc + 5));
18077c478bd9Sstevel@tonic-gate 	FILE *ofp = tmpfile();
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
18107c478bd9Sstevel@tonic-gate 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	struct sigaction act, oact;
18137c478bd9Sstevel@tonic-gate 	sigset_t mask, omask;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	int wstat, estat;
18167c478bd9Sstevel@tonic-gate 	pid_t pid;
18177c478bd9Sstevel@tonic-gate 	off64_t off;
18187c478bd9Sstevel@tonic-gate 	int c;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if (argv == NULL || ofp == NULL) {
18217c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, errno);
18227c478bd9Sstevel@tonic-gate 		goto err;
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/*
18267c478bd9Sstevel@tonic-gate 	 * If the input is a seekable file, see if it is an interpreter file.
18277c478bd9Sstevel@tonic-gate 	 * If we see #!, seek past the first line because cpp will choke on it.
18287c478bd9Sstevel@tonic-gate 	 * We start cpp just prior to the \n at the end of this line so that
18297c478bd9Sstevel@tonic-gate 	 * it still sees the newline, ensuring that #line values are correct.
18307c478bd9Sstevel@tonic-gate 	 */
18317c478bd9Sstevel@tonic-gate 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
18327c478bd9Sstevel@tonic-gate 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
18337c478bd9Sstevel@tonic-gate 			for (off += 2; c != '\n'; off++) {
18347c478bd9Sstevel@tonic-gate 				if ((c = fgetc(ifp)) == EOF)
18357c478bd9Sstevel@tonic-gate 					break;
18367c478bd9Sstevel@tonic-gate 			}
18377c478bd9Sstevel@tonic-gate 			if (c == '\n')
18387c478bd9Sstevel@tonic-gate 				off--; /* start cpp just prior to \n */
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 		(void) fflush(ifp);
18417c478bd9Sstevel@tonic-gate 		(void) fseeko64(ifp, off, SEEK_SET);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
18457c478bd9Sstevel@tonic-gate 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	(void) snprintf(verdef, sizeof (verdef),
18507c478bd9Sstevel@tonic-gate 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
18517c478bd9Sstevel@tonic-gate 	argv[argc++] = verdef;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	switch (dtp->dt_stdcmode) {
18547c478bd9Sstevel@tonic-gate 	case DT_STDC_XA:
18557c478bd9Sstevel@tonic-gate 	case DT_STDC_XT:
18567c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=0";
18577c478bd9Sstevel@tonic-gate 		break;
18587c478bd9Sstevel@tonic-gate 	case DT_STDC_XC:
18597c478bd9Sstevel@tonic-gate 		argv[argc++] = "-D__STDC__=1";
18607c478bd9Sstevel@tonic-gate 		break;
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	argv[argc++] = ipath;
18647c478bd9Sstevel@tonic-gate 	argv[argc++] = opath;
18657c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 * libdtrace must be able to be embedded in other programs that may
18697c478bd9Sstevel@tonic-gate 	 * include application-specific signal handlers.  Therefore, if we
18707c478bd9Sstevel@tonic-gate 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
18717c478bd9Sstevel@tonic-gate 	 * that could confuse the containing application.  To do this,
18727c478bd9Sstevel@tonic-gate 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
18737c478bd9Sstevel@tonic-gate 	 * We restore our signal state once we are done.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
18767c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGCHLD);
18777c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	bzero(&act, sizeof (act));
18807c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
18817c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &act, &oact);
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
18847c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGCHLD, &oact, NULL);
18857c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
18867c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CPPFORK);
18877c478bd9Sstevel@tonic-gate 		goto err;
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	if (pid == 0) {
18917c478bd9Sstevel@tonic-gate 		(void) execvp(dtp->dt_cpp_path, argv);
18927c478bd9Sstevel@tonic-gate 		_exit(errno == ENOENT ? 127 : 126);
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	do {
18967c478bd9Sstevel@tonic-gate 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
18977c478bd9Sstevel@tonic-gate 		    (int)pid);
18987c478bd9Sstevel@tonic-gate 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCHLD, &oact, NULL);
19017c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
19047c478bd9Sstevel@tonic-gate 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (estat != 0) {
19077c478bd9Sstevel@tonic-gate 		switch (estat) {
19087c478bd9Sstevel@tonic-gate 		case 126:
19097c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
19107c478bd9Sstevel@tonic-gate 			break;
19117c478bd9Sstevel@tonic-gate 		case 127:
19127c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPENT);
19137c478bd9Sstevel@tonic-gate 			break;
19147c478bd9Sstevel@tonic-gate 		default:
19157c478bd9Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CPPERR);
19167c478bd9Sstevel@tonic-gate 		}
19177c478bd9Sstevel@tonic-gate 		goto err;
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	free(argv);
19217c478bd9Sstevel@tonic-gate 	(void) fflush(ofp);
19227c478bd9Sstevel@tonic-gate 	(void) fseek(ofp, 0, SEEK_SET);
19237c478bd9Sstevel@tonic-gate 	return (ofp);
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate err:
19267c478bd9Sstevel@tonic-gate 	free(argv);
19277c478bd9Sstevel@tonic-gate 	(void) fclose(ofp);
19287c478bd9Sstevel@tonic-gate 	return (NULL);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
1931c9d6cd77Sjhaslam static void
dt_lib_depend_error(dtrace_hdl_t * dtp,const char * format,...)1932c9d6cd77Sjhaslam dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1933c9d6cd77Sjhaslam {
1934c9d6cd77Sjhaslam 	va_list ap;
1935c9d6cd77Sjhaslam 
1936c9d6cd77Sjhaslam 	va_start(ap, format);
1937c9d6cd77Sjhaslam 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1938c9d6cd77Sjhaslam 	va_end(ap);
1939c9d6cd77Sjhaslam }
1940c9d6cd77Sjhaslam 
1941c9d6cd77Sjhaslam int
dt_lib_depend_add(dtrace_hdl_t * dtp,dt_list_t * dlp,const char * arg)1942c9d6cd77Sjhaslam dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1943c9d6cd77Sjhaslam {
1944c9d6cd77Sjhaslam 	dt_lib_depend_t *dld;
1945c9d6cd77Sjhaslam 	const char *end;
1946c9d6cd77Sjhaslam 
1947c9d6cd77Sjhaslam 	assert(arg != NULL);
1948c9d6cd77Sjhaslam 
1949c9d6cd77Sjhaslam 	if ((end = strrchr(arg, '/')) == NULL)
1950c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EINVAL));
1951c9d6cd77Sjhaslam 
1952c9d6cd77Sjhaslam 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1953c9d6cd77Sjhaslam 		return (-1);
1954c9d6cd77Sjhaslam 
1955c9d6cd77Sjhaslam 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
1956c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1957c9d6cd77Sjhaslam 		return (-1);
1958c9d6cd77Sjhaslam 	}
1959c9d6cd77Sjhaslam 
1960c9d6cd77Sjhaslam 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
1961c9d6cd77Sjhaslam 	if ((dld->dtld_library = strdup(arg)) == NULL) {
1962c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
1963c9d6cd77Sjhaslam 		dt_free(dtp, dld);
1964c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
1965c9d6cd77Sjhaslam 	}
1966c9d6cd77Sjhaslam 
1967c9d6cd77Sjhaslam 	dt_list_append(dlp, dld);
1968c9d6cd77Sjhaslam 	return (0);
1969c9d6cd77Sjhaslam }
1970c9d6cd77Sjhaslam 
1971c9d6cd77Sjhaslam dt_lib_depend_t *
dt_lib_depend_lookup(dt_list_t * dld,const char * arg)1972c9d6cd77Sjhaslam dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1973c9d6cd77Sjhaslam {
1974c9d6cd77Sjhaslam 	dt_lib_depend_t *dldn;
1975c9d6cd77Sjhaslam 
1976c9d6cd77Sjhaslam 	for (dldn = dt_list_next(dld); dldn != NULL;
1977c9d6cd77Sjhaslam 	    dldn = dt_list_next(dldn)) {
1978c9d6cd77Sjhaslam 		if (strcmp(dldn->dtld_library, arg) == 0)
1979c9d6cd77Sjhaslam 			return (dldn);
1980c9d6cd77Sjhaslam 	}
1981c9d6cd77Sjhaslam 
1982c9d6cd77Sjhaslam 	return (NULL);
1983c9d6cd77Sjhaslam }
1984c9d6cd77Sjhaslam 
1985c9d6cd77Sjhaslam /*
1986c9d6cd77Sjhaslam  * Go through all the library files, and, if any library dependencies exist for
1987c9d6cd77Sjhaslam  * that file, add it to that node's list of dependents. The result of this
1988c9d6cd77Sjhaslam  * will be a graph which can then be topologically sorted to produce a
1989c9d6cd77Sjhaslam  * compilation order.
1990c9d6cd77Sjhaslam  */
1991c9d6cd77Sjhaslam static int
dt_lib_build_graph(dtrace_hdl_t * dtp)1992c9d6cd77Sjhaslam dt_lib_build_graph(dtrace_hdl_t *dtp)
1993c9d6cd77Sjhaslam {
1994c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld;
1995c9d6cd77Sjhaslam 
1996c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1997c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
1998c9d6cd77Sjhaslam 		char *library = dld->dtld_library;
1999c9d6cd77Sjhaslam 
2000c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2001c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
2002c9d6cd77Sjhaslam 			dt_lib_depend_t *dlda;
2003c9d6cd77Sjhaslam 
2004c9d6cd77Sjhaslam 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2005c9d6cd77Sjhaslam 			    dpld->dtld_library)) == NULL) {
2006c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
2007c9d6cd77Sjhaslam 				    "Invalid library dependency in %s: %s\n",
2008c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
2009c9d6cd77Sjhaslam 
2010c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
2011c9d6cd77Sjhaslam 			}
2012c9d6cd77Sjhaslam 
2013c9d6cd77Sjhaslam 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2014c9d6cd77Sjhaslam 			    library)) != 0) {
2015c9d6cd77Sjhaslam 				return (-1); /* preserve dt_errno */
2016c9d6cd77Sjhaslam 			}
2017c9d6cd77Sjhaslam 		}
2018c9d6cd77Sjhaslam 	}
2019c9d6cd77Sjhaslam 	return (0);
2020c9d6cd77Sjhaslam }
2021c9d6cd77Sjhaslam 
2022c9d6cd77Sjhaslam static int
dt_topo_sort(dtrace_hdl_t * dtp,dt_lib_depend_t * dld,int * count)2023c9d6cd77Sjhaslam dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2024c9d6cd77Sjhaslam {
2025c9d6cd77Sjhaslam 	dt_lib_depend_t *dpld, *dlda, *new;
2026c9d6cd77Sjhaslam 
2027c9d6cd77Sjhaslam 	dld->dtld_start = ++(*count);
2028c9d6cd77Sjhaslam 
2029c9d6cd77Sjhaslam 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2030c9d6cd77Sjhaslam 	    dpld = dt_list_next(dpld)) {
2031c9d6cd77Sjhaslam 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2032c9d6cd77Sjhaslam 		    dpld->dtld_library);
2033c9d6cd77Sjhaslam 		assert(dlda != NULL);
2034c9d6cd77Sjhaslam 
2035c9d6cd77Sjhaslam 		if (dlda->dtld_start == 0 &&
2036c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dlda, count) == -1)
2037c9d6cd77Sjhaslam 			return (-1);
2038c9d6cd77Sjhaslam 	}
2039c9d6cd77Sjhaslam 
2040c9d6cd77Sjhaslam 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2041c9d6cd77Sjhaslam 		return (-1);
2042c9d6cd77Sjhaslam 
2043c9d6cd77Sjhaslam 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2044c9d6cd77Sjhaslam 		dt_free(dtp, new);
2045c9d6cd77Sjhaslam 		return (dt_set_errno(dtp, EDT_NOMEM));
2046c9d6cd77Sjhaslam 	}
2047c9d6cd77Sjhaslam 
2048c9d6cd77Sjhaslam 	new->dtld_start = dld->dtld_start;
2049c9d6cd77Sjhaslam 	new->dtld_finish = dld->dtld_finish = ++(*count);
2050c9d6cd77Sjhaslam 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2051c9d6cd77Sjhaslam 
2052c9d6cd77Sjhaslam 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2053c9d6cd77Sjhaslam 	    new->dtld_start, new->dtld_finish);
2054c9d6cd77Sjhaslam 
2055c9d6cd77Sjhaslam 	return (0);
2056c9d6cd77Sjhaslam }
2057c9d6cd77Sjhaslam 
2058c9d6cd77Sjhaslam static int
dt_lib_depend_sort(dtrace_hdl_t * dtp)2059c9d6cd77Sjhaslam dt_lib_depend_sort(dtrace_hdl_t *dtp)
2060c9d6cd77Sjhaslam {
2061c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dpld, *dlda;
2062c9d6cd77Sjhaslam 	int count = 0;
2063c9d6cd77Sjhaslam 
2064c9d6cd77Sjhaslam 	if (dt_lib_build_graph(dtp) == -1)
2065c9d6cd77Sjhaslam 		return (-1); /* preserve dt_errno */
2066c9d6cd77Sjhaslam 
2067c9d6cd77Sjhaslam 	/*
2068c9d6cd77Sjhaslam 	 * Perform a topological sort of the graph that hangs off
2069c9d6cd77Sjhaslam 	 * dtp->dt_lib_dep. The result of this process will be a
2070c9d6cd77Sjhaslam 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2071c9d6cd77Sjhaslam 	 */
2072c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2073c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2074c9d6cd77Sjhaslam 		if (dld->dtld_start == 0 &&
2075c9d6cd77Sjhaslam 		    dt_topo_sort(dtp, dld, &count) == -1)
2076c9d6cd77Sjhaslam 			return (-1); /* preserve dt_errno */;
2077c9d6cd77Sjhaslam 	}
2078c9d6cd77Sjhaslam 
2079c9d6cd77Sjhaslam 	/*
2080c9d6cd77Sjhaslam 	 * Check the graph for cycles. If an ancestor's finishing time is
2081c9d6cd77Sjhaslam 	 * less than any of its dependent's finishing times then a back edge
2082c9d6cd77Sjhaslam 	 * exists in the graph and this is a cycle.
2083c9d6cd77Sjhaslam 	 */
2084c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2085c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2086c9d6cd77Sjhaslam 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2087c9d6cd77Sjhaslam 		    dpld = dt_list_next(dpld)) {
2088c9d6cd77Sjhaslam 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2089c9d6cd77Sjhaslam 			    dpld->dtld_library);
2090c9d6cd77Sjhaslam 			assert(dlda != NULL);
2091c9d6cd77Sjhaslam 
2092c9d6cd77Sjhaslam 			if (dlda->dtld_finish > dld->dtld_finish) {
2093c9d6cd77Sjhaslam 				dt_lib_depend_error(dtp,
2094c9d6cd77Sjhaslam 				    "Cyclic dependency detected: %s => %s\n",
2095c9d6cd77Sjhaslam 				    dld->dtld_library, dpld->dtld_library);
2096c9d6cd77Sjhaslam 
2097c9d6cd77Sjhaslam 				return (dt_set_errno(dtp, EDT_COMPILER));
2098c9d6cd77Sjhaslam 			}
2099c9d6cd77Sjhaslam 		}
2100c9d6cd77Sjhaslam 	}
2101c9d6cd77Sjhaslam 
2102c9d6cd77Sjhaslam 	return (0);
2103c9d6cd77Sjhaslam }
2104c9d6cd77Sjhaslam 
2105c9d6cd77Sjhaslam static void
dt_lib_depend_free(dtrace_hdl_t * dtp)2106c9d6cd77Sjhaslam dt_lib_depend_free(dtrace_hdl_t *dtp)
2107c9d6cd77Sjhaslam {
2108c9d6cd77Sjhaslam 	dt_lib_depend_t *dld, *dlda;
2109c9d6cd77Sjhaslam 
2110c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2111c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2112c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependencies, dlda);
2113c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
2114c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
2115c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
2116c9d6cd77Sjhaslam 		}
2117c9d6cd77Sjhaslam 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2118c9d6cd77Sjhaslam 			dt_list_delete(&dld->dtld_dependents, dlda);
2119c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_library);
2120c9d6cd77Sjhaslam 			dt_free(dtp, dlda->dtld_libpath);
2121c9d6cd77Sjhaslam 			dt_free(dtp, dlda);
2122c9d6cd77Sjhaslam 		}
2123c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep, dld);
2124c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
2125c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_libpath);
2126c9d6cd77Sjhaslam 		dt_free(dtp, dld);
2127c9d6cd77Sjhaslam 	}
2128c9d6cd77Sjhaslam 
2129c9d6cd77Sjhaslam 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2130c9d6cd77Sjhaslam 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2131c9d6cd77Sjhaslam 		dt_free(dtp, dld->dtld_library);
2132c9d6cd77Sjhaslam 		dt_free(dtp, dld);
2133c9d6cd77Sjhaslam 	}
2134c9d6cd77Sjhaslam }
2135c9d6cd77Sjhaslam 
21367c478bd9Sstevel@tonic-gate /*
21375eb667acSRobert Mustacchi  * Open all the .d library files found in the specified directory and
21385eb667acSRobert Mustacchi  * compile each one of them.  We silently ignore any missing directories and
21395eb667acSRobert Mustacchi  * other files found therein.  We only fail (and thereby fail dt_load_libs()) if
21405eb667acSRobert Mustacchi  * we fail to compile a library and the error is something other than #pragma D
21415eb667acSRobert Mustacchi  * depends_on.  Dependency errors are silently ignored to permit a library
21425eb667acSRobert Mustacchi  * directory to contain libraries which may not be accessible depending on our
21435eb667acSRobert Mustacchi  * privileges.
21447c478bd9Sstevel@tonic-gate  */
21457c478bd9Sstevel@tonic-gate static int
dt_load_libs_dir(dtrace_hdl_t * dtp,const char * path)21467c478bd9Sstevel@tonic-gate dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
21477c478bd9Sstevel@tonic-gate {
21484bc0a2efScasper 	struct dirent *dp;
2149afab0816SRobert Mustacchi 	const char *p, *end;
21507c478bd9Sstevel@tonic-gate 	DIR *dirp;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	char fname[PATH_MAX];
21537c478bd9Sstevel@tonic-gate 	FILE *fp;
2154c9d6cd77Sjhaslam 	void *rv;
2155afab0816SRobert Mustacchi 	dt_lib_depend_t *dld;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL) {
21587c478bd9Sstevel@tonic-gate 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
21597c478bd9Sstevel@tonic-gate 		return (0);
21607c478bd9Sstevel@tonic-gate 	}
21617c478bd9Sstevel@tonic-gate 
2162c9d6cd77Sjhaslam 	/* First, parse each file for library dependencies. */
21634bc0a2efScasper 	while ((dp = readdir(dirp)) != NULL) {
21647c478bd9Sstevel@tonic-gate 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
21657c478bd9Sstevel@tonic-gate 			continue; /* skip any filename not ending in .d */
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 		(void) snprintf(fname, sizeof (fname),
21687c478bd9Sstevel@tonic-gate 		    "%s/%s", path, dp->d_name);
21697c478bd9Sstevel@tonic-gate 
217093ed8d0dSRobert Mustacchi 		if ((fp = fopen(fname, "rF")) == NULL) {
21717c478bd9Sstevel@tonic-gate 			dt_dprintf("skipping library %s: %s\n",
21727c478bd9Sstevel@tonic-gate 			    fname, strerror(errno));
21737c478bd9Sstevel@tonic-gate 			continue;
21747c478bd9Sstevel@tonic-gate 		}
21757c478bd9Sstevel@tonic-gate 
2176afab0816SRobert Mustacchi 		/*
2177afab0816SRobert Mustacchi 		 * Skip files whose name match an already processed library
2178afab0816SRobert Mustacchi 		 */
2179afab0816SRobert Mustacchi 		for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2180afab0816SRobert Mustacchi 		    dld = dt_list_next(dld)) {
2181afab0816SRobert Mustacchi 			end = strrchr(dld->dtld_library, '/');
2182afab0816SRobert Mustacchi 			/* dt_lib_depend_add ensures this */
2183afab0816SRobert Mustacchi 			assert(end != NULL);
2184afab0816SRobert Mustacchi 			if (strcmp(end + 1, dp->d_name) == 0)
2185afab0816SRobert Mustacchi 				break;
2186afab0816SRobert Mustacchi 		}
2187afab0816SRobert Mustacchi 
2188afab0816SRobert Mustacchi 		if (dld != NULL) {
2189afab0816SRobert Mustacchi 			dt_dprintf("skipping library %s, already processed "
2190afab0816SRobert Mustacchi 			    "library with the same name: %s", dp->d_name,
2191afab0816SRobert Mustacchi 			    dld->dtld_library);
219293ed8d0dSRobert Mustacchi 			(void) fclose(fp);
2193afab0816SRobert Mustacchi 			continue;
2194afab0816SRobert Mustacchi 		}
2195afab0816SRobert Mustacchi 
21967c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = fname;
219793ed8d0dSRobert Mustacchi 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0) {
219893ed8d0dSRobert Mustacchi 			(void) fclose(fp);
21995eb667acSRobert Mustacchi 			return (-1); /* preserve dt_errno */
220093ed8d0dSRobert Mustacchi 		}
2201c9d6cd77Sjhaslam 
2202c9d6cd77Sjhaslam 		rv = dt_compile(dtp, DT_CTX_DPROG,
2203c9d6cd77Sjhaslam 		    DTRACE_PROBESPEC_NAME, NULL,
2204c9d6cd77Sjhaslam 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2205c9d6cd77Sjhaslam 
2206c9d6cd77Sjhaslam 		if (rv != NULL && dtp->dt_errno &&
2207c9d6cd77Sjhaslam 		    (dtp->dt_errno != EDT_COMPILER ||
220893ed8d0dSRobert Mustacchi 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
220993ed8d0dSRobert Mustacchi 			(void) fclose(fp);
22105eb667acSRobert Mustacchi 			return (-1); /* preserve dt_errno */
221193ed8d0dSRobert Mustacchi 		}
2212c9d6cd77Sjhaslam 
2213c9d6cd77Sjhaslam 		if (dtp->dt_errno)
2214c9d6cd77Sjhaslam 			dt_dprintf("error parsing library %s: %s\n",
2215c9d6cd77Sjhaslam 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2216c9d6cd77Sjhaslam 
2217c9d6cd77Sjhaslam 		(void) fclose(fp);
2218c9d6cd77Sjhaslam 		dtp->dt_filetag = NULL;
2219c9d6cd77Sjhaslam 	}
2220c9d6cd77Sjhaslam 
2221c9d6cd77Sjhaslam 	(void) closedir(dirp);
22225eb667acSRobert Mustacchi 
22235eb667acSRobert Mustacchi 	return (0);
22245eb667acSRobert Mustacchi }
22255eb667acSRobert Mustacchi 
22265eb667acSRobert Mustacchi /*
22275eb667acSRobert Mustacchi  * Perform a topological sorting of all the libraries found across the entire
22285eb667acSRobert Mustacchi  * dt_lib_path.  Once sorted, compile each one in topological order to cache its
22295eb667acSRobert Mustacchi  * inlines and translators, etc.  We silently ignore any missing directories and
22305eb667acSRobert Mustacchi  * other files found therein. We only fail (and thereby fail dt_load_libs()) if
22315eb667acSRobert Mustacchi  * we fail to compile a library and the error is something other than #pragma D
22325eb667acSRobert Mustacchi  * depends_on.  Dependency errors are silently ignored to permit a library
22335eb667acSRobert Mustacchi  * directory to contain libraries which may not be accessible depending on our
22345eb667acSRobert Mustacchi  * privileges.
22355eb667acSRobert Mustacchi  */
22365eb667acSRobert Mustacchi static int
dt_load_libs_sort(dtrace_hdl_t * dtp)22375eb667acSRobert Mustacchi dt_load_libs_sort(dtrace_hdl_t *dtp)
22385eb667acSRobert Mustacchi {
22395eb667acSRobert Mustacchi 	dtrace_prog_t *pgp;
22405eb667acSRobert Mustacchi 	FILE *fp;
22415eb667acSRobert Mustacchi 	dt_lib_depend_t *dld;
22425eb667acSRobert Mustacchi 
2243c9d6cd77Sjhaslam 	/*
2244c9d6cd77Sjhaslam 	 * Finish building the graph containing the library dependencies
2245c9d6cd77Sjhaslam 	 * and perform a topological sort to generate an ordered list
2246c9d6cd77Sjhaslam 	 * for compilation.
2247c9d6cd77Sjhaslam 	 */
2248c9d6cd77Sjhaslam 	if (dt_lib_depend_sort(dtp) == -1)
2249c9d6cd77Sjhaslam 		goto err;
2250c9d6cd77Sjhaslam 
2251c9d6cd77Sjhaslam 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2252c9d6cd77Sjhaslam 	    dld = dt_list_next(dld)) {
2253c9d6cd77Sjhaslam 
2254c9d6cd77Sjhaslam 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2255c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
2256c9d6cd77Sjhaslam 			    dld->dtld_library, strerror(errno));
2257c9d6cd77Sjhaslam 			continue;
2258c9d6cd77Sjhaslam 		}
2259c9d6cd77Sjhaslam 
2260c9d6cd77Sjhaslam 		dtp->dt_filetag = dld->dtld_library;
22617c478bd9Sstevel@tonic-gate 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
22627c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
22637c478bd9Sstevel@tonic-gate 		dtp->dt_filetag = NULL;
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2266c9d6cd77Sjhaslam 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2267c9d6cd77Sjhaslam 			goto err;
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 		if (pgp == NULL) {
2270c9d6cd77Sjhaslam 			dt_dprintf("skipping library %s: %s\n",
2271c9d6cd77Sjhaslam 			    dld->dtld_library,
22727c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
22736009dbc6Sahl 		} else {
22746009dbc6Sahl 			dld->dtld_loaded = B_TRUE;
22751a7c1b72Smws 			dt_program_destroy(dtp, pgp);
22766009dbc6Sahl 		}
22777c478bd9Sstevel@tonic-gate 	}
22787c478bd9Sstevel@tonic-gate 
2279c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
22807c478bd9Sstevel@tonic-gate 	return (0);
2281c9d6cd77Sjhaslam 
2282c9d6cd77Sjhaslam err:
2283c9d6cd77Sjhaslam 	dt_lib_depend_free(dtp);
2284c9d6cd77Sjhaslam 	return (-1); /* preserve dt_errno */
22857c478bd9Sstevel@tonic-gate }
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate /*
22887c478bd9Sstevel@tonic-gate  * Load the contents of any appropriate DTrace .d library files.  These files
22897c478bd9Sstevel@tonic-gate  * contain inlines and translators that will be cached by the compiler.  We
22907c478bd9Sstevel@tonic-gate  * defer this activity until the first compile to permit libdtrace clients to
22917c478bd9Sstevel@tonic-gate  * add their own library directories and so that we can properly report errors.
22927c478bd9Sstevel@tonic-gate  */
22937c478bd9Sstevel@tonic-gate static int
dt_load_libs(dtrace_hdl_t * dtp)22947c478bd9Sstevel@tonic-gate dt_load_libs(dtrace_hdl_t *dtp)
22957c478bd9Sstevel@tonic-gate {
22967c478bd9Sstevel@tonic-gate 	dt_dirpath_t *dirp;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
22997c478bd9Sstevel@tonic-gate 		return (0); /* libraries already processed */
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
23027c478bd9Sstevel@tonic-gate 
2303afab0816SRobert Mustacchi 	/*
2304afab0816SRobert Mustacchi 	 * /usr/lib/dtrace is always at the head of the list. The rest of the
2305afab0816SRobert Mustacchi 	 * list is specified in the precedence order the user requested. Process
2306afab0816SRobert Mustacchi 	 * everything other than the head first. DTRACE_C_NOLIBS has already
2307afab0816SRobert Mustacchi 	 * been spcified so dt_vopen will ensure that there is always one entry
2308afab0816SRobert Mustacchi 	 * in dt_lib_path.
2309afab0816SRobert Mustacchi 	 */
2310afab0816SRobert Mustacchi 	for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
23117c478bd9Sstevel@tonic-gate 	    dirp != NULL; dirp = dt_list_next(dirp)) {
23127c478bd9Sstevel@tonic-gate 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
23137c478bd9Sstevel@tonic-gate 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
23147c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
23157c478bd9Sstevel@tonic-gate 		}
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 
2318afab0816SRobert Mustacchi 	/* Handle /usr/lib/dtrace */
2319afab0816SRobert Mustacchi 	dirp = dt_list_next(&dtp->dt_lib_path);
2320afab0816SRobert Mustacchi 	if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2321afab0816SRobert Mustacchi 		dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2322afab0816SRobert Mustacchi 		return (-1); /* errno is set for us */
2323afab0816SRobert Mustacchi 	}
2324afab0816SRobert Mustacchi 
23255eb667acSRobert Mustacchi 	if (dt_load_libs_sort(dtp) < 0)
23265eb667acSRobert Mustacchi 		return (-1); /* errno is set for us */
23275eb667acSRobert Mustacchi 
23287c478bd9Sstevel@tonic-gate 	return (0);
23297c478bd9Sstevel@tonic-gate }
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate static void *
dt_compile(dtrace_hdl_t * dtp,int context,dtrace_probespec_t pspec,void * arg,uint_t cflags,int argc,char * const argv[],FILE * fp,const char * s)23327c478bd9Sstevel@tonic-gate dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
23337c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
23347c478bd9Sstevel@tonic-gate {
23357c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
23367c478bd9Sstevel@tonic-gate 	dt_decl_t *ddp;
23377c478bd9Sstevel@tonic-gate 	dt_pcb_t pcb;
233867a4bb8fSGary Mills 	void *volatile rv;
23397c478bd9Sstevel@tonic-gate 	int err;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
23427c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EINVAL);
23437c478bd9Sstevel@tonic-gate 		return (NULL);
23447c478bd9Sstevel@tonic-gate 	}
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
23477c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
23487c478bd9Sstevel@tonic-gate 
2349c9a6ea2eSBryan Cantrill 	if (dtp->dt_globals->dh_nelems != 0)
2350c9a6ea2eSBryan Cantrill 		(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
23517c478bd9Sstevel@tonic-gate 
2352c9a6ea2eSBryan Cantrill 	if (dtp->dt_tls->dh_nelems != 0)
2353c9a6ea2eSBryan Cantrill 		(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
23567c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	dt_pcb_push(dtp, &pcb);
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	pcb.pcb_fileptr = fp;
23617c478bd9Sstevel@tonic-gate 	pcb.pcb_string = s;
23627c478bd9Sstevel@tonic-gate 	pcb.pcb_strptr = s;
23637c478bd9Sstevel@tonic-gate 	pcb.pcb_strlen = s ? strlen(s) : 0;
23647c478bd9Sstevel@tonic-gate 	pcb.pcb_sargc = argc;
23657c478bd9Sstevel@tonic-gate 	pcb.pcb_sargv = argv;
23667c478bd9Sstevel@tonic-gate 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
23677c478bd9Sstevel@tonic-gate 	pcb.pcb_pspec = pspec;
23687c478bd9Sstevel@tonic-gate 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
23697c478bd9Sstevel@tonic-gate 	pcb.pcb_amin = dtp->dt_amin;
23707c478bd9Sstevel@tonic-gate 	pcb.pcb_yystate = -1;
23717c478bd9Sstevel@tonic-gate 	pcb.pcb_context = context;
23727c478bd9Sstevel@tonic-gate 	pcb.pcb_token = context;
23737c478bd9Sstevel@tonic-gate 
2374c9d6cd77Sjhaslam 	if (context != DT_CTX_DPROG)
23757c478bd9Sstevel@tonic-gate 		yybegin(YYS_EXPR);
2376c9d6cd77Sjhaslam 	else if (cflags & DTRACE_C_CTL)
2377c9d6cd77Sjhaslam 		yybegin(YYS_CONTROL);
2378c9d6cd77Sjhaslam 	else
2379c9d6cd77Sjhaslam 		yybegin(YYS_CLAUSE);
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
23827c478bd9Sstevel@tonic-gate 		goto out;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
23857c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
23887c478bd9Sstevel@tonic-gate 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
23897c478bd9Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
23927c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	/*
23957c478bd9Sstevel@tonic-gate 	 * Invoke the parser to evaluate the D source code.  If any errors
23967c478bd9Sstevel@tonic-gate 	 * occur during parsing, an error function will be called and we
23977c478bd9Sstevel@tonic-gate 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
23987c478bd9Sstevel@tonic-gate 	 * we optionally display the parse tree if debugging is enabled.
23997c478bd9Sstevel@tonic-gate 	 */
24007c478bd9Sstevel@tonic-gate 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
24017c478bd9Sstevel@tonic-gate 		xyerror(D_EMPTY, "empty D program translation unit\n");
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	yybegin(YYS_DONE);
24047c478bd9Sstevel@tonic-gate 
2405c9d6cd77Sjhaslam 	if (cflags & DTRACE_C_CTL)
2406c9d6cd77Sjhaslam 		goto out;
2407c9d6cd77Sjhaslam 
24087c478bd9Sstevel@tonic-gate 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
24097c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pragmas != NULL)
24127c478bd9Sstevel@tonic-gate 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
24157c478bd9Sstevel@tonic-gate 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
24167c478bd9Sstevel@tonic-gate 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
24177c478bd9Sstevel@tonic-gate 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 
2420*c3bd3abdSMatthew Ahrens 	/*
2421*c3bd3abdSMatthew Ahrens 	 * Perform sugar transformations (for "if" / "else") and replace the
2422*c3bd3abdSMatthew Ahrens 	 * existing clause chain with the new one.
2423*c3bd3abdSMatthew Ahrens 	 */
2424*c3bd3abdSMatthew Ahrens 	if (context == DT_CTX_DPROG) {
2425*c3bd3abdSMatthew Ahrens 		dt_node_t *dnp, *next_dnp;
2426*c3bd3abdSMatthew Ahrens 		dt_node_t *new_list = NULL;
2427*c3bd3abdSMatthew Ahrens 
2428*c3bd3abdSMatthew Ahrens 		for (dnp = yypcb->pcb_root->dn_list;
2429*c3bd3abdSMatthew Ahrens 		    dnp != NULL; dnp = next_dnp) {
2430*c3bd3abdSMatthew Ahrens 			/* remove this node from the list */
2431*c3bd3abdSMatthew Ahrens 			next_dnp = dnp->dn_list;
2432*c3bd3abdSMatthew Ahrens 			dnp->dn_list = NULL;
2433*c3bd3abdSMatthew Ahrens 
2434*c3bd3abdSMatthew Ahrens 			if (dnp->dn_kind == DT_NODE_CLAUSE)
2435*c3bd3abdSMatthew Ahrens 				dnp = dt_compile_sugar(dtp, dnp);
2436*c3bd3abdSMatthew Ahrens 			/* append node to the new list */
2437*c3bd3abdSMatthew Ahrens 			new_list = dt_node_link(new_list, dnp);
2438*c3bd3abdSMatthew Ahrens 		}
2439*c3bd3abdSMatthew Ahrens 		yypcb->pcb_root->dn_list = new_list;
2440*c3bd3abdSMatthew Ahrens 	}
2441*c3bd3abdSMatthew Ahrens 
24427c478bd9Sstevel@tonic-gate 	/*
24437c478bd9Sstevel@tonic-gate 	 * If we have successfully created a parse tree for a D program, loop
24447c478bd9Sstevel@tonic-gate 	 * over the clauses and actions and instantiate the corresponding
24457c478bd9Sstevel@tonic-gate 	 * libdtrace program.  If we are parsing a D expression, then we
24467c478bd9Sstevel@tonic-gate 	 * simply run the code generator and assembler on the resulting tree.
24477c478bd9Sstevel@tonic-gate 	 */
24487c478bd9Sstevel@tonic-gate 	switch (context) {
24497c478bd9Sstevel@tonic-gate 	case DT_CTX_DPROG:
24507c478bd9Sstevel@tonic-gate 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
24537c478bd9Sstevel@tonic-gate 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
24547c478bd9Sstevel@tonic-gate 			xyerror(D_EMPTY, "empty D program translation unit\n");
24557c478bd9Sstevel@tonic-gate 
24561a7c1b72Smws 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
24577c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 		for (; dnp != NULL; dnp = dnp->dn_list) {
24607c478bd9Sstevel@tonic-gate 			switch (dnp->dn_kind) {
24617c478bd9Sstevel@tonic-gate 			case DT_NODE_CLAUSE:
2462*c3bd3abdSMatthew Ahrens 				if (DT_TREEDUMP_PASS(dtp, 4))
2463*c3bd3abdSMatthew Ahrens 					dt_printd(dnp, stderr, 0);
24647c478bd9Sstevel@tonic-gate 				dt_compile_clause(dtp, dnp);
24657c478bd9Sstevel@tonic-gate 				break;
24661a7c1b72Smws 			case DT_NODE_XLATOR:
24671a7c1b72Smws 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
24681a7c1b72Smws 					dt_compile_xlator(dnp);
24691a7c1b72Smws 				break;
24707c478bd9Sstevel@tonic-gate 			case DT_NODE_PROVIDER:
24717c478bd9Sstevel@tonic-gate 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
24727c478bd9Sstevel@tonic-gate 				break;
24737c478bd9Sstevel@tonic-gate 			}
24747c478bd9Sstevel@tonic-gate 		}
24757c478bd9Sstevel@tonic-gate 
24761a7c1b72Smws 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
24771a7c1b72Smws 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
24781a7c1b72Smws 		yypcb->pcb_asxrefs = NULL;
24791a7c1b72Smws 		yypcb->pcb_asxreflen = 0;
24801a7c1b72Smws 
24811a7c1b72Smws 		rv = yypcb->pcb_prog;
24827c478bd9Sstevel@tonic-gate 		break;
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	case DT_CTX_DEXPR:
24857c478bd9Sstevel@tonic-gate 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
24867c478bd9Sstevel@tonic-gate 		dt_cg(yypcb, yypcb->pcb_root);
24877c478bd9Sstevel@tonic-gate 		rv = dt_as(yypcb);
24887c478bd9Sstevel@tonic-gate 		break;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	case DT_CTX_DTYPE:
24917c478bd9Sstevel@tonic-gate 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
24927c478bd9Sstevel@tonic-gate 		err = dt_decl_type(ddp, arg);
24937c478bd9Sstevel@tonic-gate 		dt_decl_free(ddp);
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 		if (err != 0)
24967c478bd9Sstevel@tonic-gate 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 		rv = NULL;
24997c478bd9Sstevel@tonic-gate 		break;
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate out:
2503e5803b76SAdam H. Leventhal 	if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2504e5803b76SAdam H. Leventhal 	    DT_TREEDUMP_PASS(dtp, 3))
25057c478bd9Sstevel@tonic-gate 		dt_node_printr(yypcb->pcb_root, stderr, 0);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
25087c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
25097c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
25107c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
25137c478bd9Sstevel@tonic-gate 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
25147c478bd9Sstevel@tonic-gate 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
25157c478bd9Sstevel@tonic-gate 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
25187c478bd9Sstevel@tonic-gate 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	dt_pcb_pop(dtp, err);
25217c478bd9Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
25227c478bd9Sstevel@tonic-gate 	return (err ? NULL : rv);
25237c478bd9Sstevel@tonic-gate }
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate dtrace_prog_t *
dtrace_program_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_probespec_t spec,uint_t cflags,int argc,char * const argv[])25267c478bd9Sstevel@tonic-gate dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
25277c478bd9Sstevel@tonic-gate     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
25287c478bd9Sstevel@tonic-gate {
25297c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
25307c478bd9Sstevel@tonic-gate 	    spec, NULL, cflags, argc, argv, NULL, s));
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate dtrace_prog_t *
dtrace_program_fcompile(dtrace_hdl_t * dtp,FILE * fp,uint_t cflags,int argc,char * const argv[])25347c478bd9Sstevel@tonic-gate dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
25357c478bd9Sstevel@tonic-gate     uint_t cflags, int argc, char *const argv[])
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	return (dt_compile(dtp, DT_CTX_DPROG,
25387c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate int
dtrace_type_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_typeinfo_t * dtt)25427c478bd9Sstevel@tonic-gate dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
25457c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
25467c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate int
dtrace_type_fcompile(dtrace_hdl_t * dtp,FILE * fp,dtrace_typeinfo_t * dtt)25507c478bd9Sstevel@tonic-gate dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
25517c478bd9Sstevel@tonic-gate {
25527c478bd9Sstevel@tonic-gate 	(void) dt_compile(dtp, DT_CTX_DTYPE,
25537c478bd9Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
25547c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno ? -1 : 0);
25557c478bd9Sstevel@tonic-gate }
2556