xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_cc.c (revision 1e136a9cbd3a9d137037e47a53c1dba3be7f6925)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
231670a1c2SRui Paulo  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24650f66acSMark Johnston  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
258e648814SRui Paulo  * Copyright (c) 2013, Joyent Inc. All rights reserved.
26ba69823eSMark Johnston  * Copyright 2015 Gary Mills
276ff6d951SJohn Birrell  */
286ff6d951SJohn Birrell 
296ff6d951SJohn Birrell /*
306ff6d951SJohn Birrell  * DTrace D Language Compiler
316ff6d951SJohn Birrell  *
326ff6d951SJohn Birrell  * The code in this source file implements the main engine for the D language
336ff6d951SJohn Birrell  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
346ff6d951SJohn Birrell  * compiler operates on either stdio FILEs or in-memory strings as its input
356ff6d951SJohn Birrell  * and can produce either dtrace_prog_t structures from a D program or a single
366ff6d951SJohn Birrell  * dtrace_difo_t structure from a D expression.  Multiple entry points are
376ff6d951SJohn Birrell  * provided as wrappers around dt_compile() for the various input/output pairs.
386ff6d951SJohn Birrell  * The compiler itself is implemented across the following source files:
396ff6d951SJohn Birrell  *
406ff6d951SJohn Birrell  * dt_lex.l - lex scanner
416ff6d951SJohn Birrell  * dt_grammar.y - yacc grammar
426ff6d951SJohn Birrell  * dt_parser.c - parse tree creation and semantic checking
436ff6d951SJohn Birrell  * dt_decl.c - declaration stack processing
446ff6d951SJohn Birrell  * dt_xlator.c - D translator lookup and creation
456ff6d951SJohn Birrell  * dt_ident.c - identifier and symbol table routines
466ff6d951SJohn Birrell  * dt_pragma.c - #pragma processing and D pragmas
476ff6d951SJohn Birrell  * dt_printf.c - D printf() and printa() argument checking and processing
486ff6d951SJohn Birrell  * dt_cc.c - compiler driver and dtrace_prog_t construction
496ff6d951SJohn Birrell  * dt_cg.c - DIF code generator
506ff6d951SJohn Birrell  * dt_as.c - DIF assembler
516ff6d951SJohn Birrell  * dt_dof.c - dtrace_prog_t -> DOF conversion
526ff6d951SJohn Birrell  *
536ff6d951SJohn Birrell  * Several other source files provide collections of utility routines used by
546ff6d951SJohn Birrell  * these major files.  The compiler itself is implemented in multiple passes:
556ff6d951SJohn Birrell  *
566ff6d951SJohn Birrell  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
576ff6d951SJohn Birrell  *     and parse tree nodes are constructed using the routines in dt_parser.c.
586ff6d951SJohn Birrell  *     This node construction pass is described further in dt_parser.c.
596ff6d951SJohn Birrell  *
606ff6d951SJohn Birrell  * (2) The parse tree is "cooked" by assigning each clause a context (see the
616ff6d951SJohn Birrell  *     routine dt_setcontext(), below) based on its probe description and then
626ff6d951SJohn Birrell  *     recursively descending the tree performing semantic checking.  The cook
636ff6d951SJohn Birrell  *     routines are also implemented in dt_parser.c and described there.
646ff6d951SJohn Birrell  *
656ff6d951SJohn Birrell  * (3) For actions that are DIF expression statements, the DIF code generator
666ff6d951SJohn Birrell  *     and assembler are invoked to create a finished DIFO for the statement.
676ff6d951SJohn Birrell  *
686ff6d951SJohn Birrell  * (4) The dtrace_prog_t data structures for the program clauses and actions
696ff6d951SJohn Birrell  *     are built, containing pointers to any DIFOs created in step (3).
706ff6d951SJohn Birrell  *
716ff6d951SJohn Birrell  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
726ff6d951SJohn Birrell  *     into DOF format for use in anonymous tracing or enabling in the kernel.
736ff6d951SJohn Birrell  *
746ff6d951SJohn Birrell  * In the implementation, steps 2-4 are intertwined in that they are performed
756ff6d951SJohn Birrell  * in order for each clause as part of a loop that executes over the clauses.
766ff6d951SJohn Birrell  *
776ff6d951SJohn Birrell  * The D compiler currently implements nearly no optimization.  The compiler
786ff6d951SJohn Birrell  * implements integer constant folding as part of pass (1), and a set of very
796ff6d951SJohn Birrell  * simple peephole optimizations as part of pass (3).  As with any C compiler,
806ff6d951SJohn Birrell  * a large number of optimizations are possible on both the intermediate data
816ff6d951SJohn Birrell  * structures and the generated DIF code.  These possibilities should be
826ff6d951SJohn Birrell  * investigated in the context of whether they will have any substantive effect
836ff6d951SJohn Birrell  * on the overall DTrace probe effect before they are undertaken.
846ff6d951SJohn Birrell  */
856ff6d951SJohn Birrell 
866ff6d951SJohn Birrell #include <sys/types.h>
876ff6d951SJohn Birrell #include <sys/wait.h>
88675cf915SPedro F. Giffuni #include <sys/sysmacros.h>
896ff6d951SJohn Birrell 
906ff6d951SJohn Birrell #include <assert.h>
9118737969SJohn Birrell #include <string.h>
926ff6d951SJohn Birrell #include <strings.h>
936ff6d951SJohn Birrell #include <signal.h>
946ff6d951SJohn Birrell #include <unistd.h>
956ff6d951SJohn Birrell #include <stdlib.h>
966ff6d951SJohn Birrell #include <stdio.h>
976ff6d951SJohn Birrell #include <errno.h>
986ff6d951SJohn Birrell #include <ucontext.h>
996ff6d951SJohn Birrell #include <limits.h>
1006ff6d951SJohn Birrell #include <ctype.h>
1016ff6d951SJohn Birrell #include <dirent.h>
1026ff6d951SJohn Birrell #include <dt_module.h>
1036ff6d951SJohn Birrell #include <dt_program.h>
1046ff6d951SJohn Birrell #include <dt_provider.h>
1056ff6d951SJohn Birrell #include <dt_printf.h>
1066ff6d951SJohn Birrell #include <dt_pid.h>
1076ff6d951SJohn Birrell #include <dt_grammar.h>
1086ff6d951SJohn Birrell #include <dt_ident.h>
1096ff6d951SJohn Birrell #include <dt_string.h>
1106ff6d951SJohn Birrell #include <dt_impl.h>
1116ff6d951SJohn Birrell 
1126ff6d951SJohn Birrell static const dtrace_diftype_t dt_void_rtype = {
1136ff6d951SJohn Birrell 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
1146ff6d951SJohn Birrell };
1156ff6d951SJohn Birrell 
1166ff6d951SJohn Birrell static const dtrace_diftype_t dt_int_rtype = {
1176ff6d951SJohn Birrell 	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
1186ff6d951SJohn Birrell };
1196ff6d951SJohn Birrell 
1206ff6d951SJohn Birrell static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
1216ff6d951SJohn Birrell     uint_t, int, char *const[], FILE *, const char *);
1226ff6d951SJohn Birrell 
1236ff6d951SJohn Birrell /*ARGSUSED*/
1246ff6d951SJohn Birrell static int
dt_idreset(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)1256ff6d951SJohn Birrell dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1266ff6d951SJohn Birrell {
1276ff6d951SJohn Birrell 	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
1286ff6d951SJohn Birrell 	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
1296ff6d951SJohn Birrell 	return (0);
1306ff6d951SJohn Birrell }
1316ff6d951SJohn Birrell 
1326ff6d951SJohn Birrell /*ARGSUSED*/
1336ff6d951SJohn Birrell static int
dt_idpragma(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)1346ff6d951SJohn Birrell dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
1356ff6d951SJohn Birrell {
1366ff6d951SJohn Birrell 	yylineno = idp->di_lineno;
1376ff6d951SJohn Birrell 	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
1386ff6d951SJohn Birrell 	return (0);
1396ff6d951SJohn Birrell }
1406ff6d951SJohn Birrell 
1416ff6d951SJohn Birrell static dtrace_stmtdesc_t *
dt_stmt_create(dtrace_hdl_t * dtp,dtrace_ecbdesc_t * edp,dtrace_attribute_t descattr,dtrace_attribute_t stmtattr)1426ff6d951SJohn Birrell dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
1436ff6d951SJohn Birrell     dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
1446ff6d951SJohn Birrell {
1456ff6d951SJohn Birrell 	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
1466ff6d951SJohn Birrell 
1476ff6d951SJohn Birrell 	if (sdp == NULL)
1486ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1496ff6d951SJohn Birrell 
1506ff6d951SJohn Birrell 	assert(yypcb->pcb_stmt == NULL);
1516ff6d951SJohn Birrell 	yypcb->pcb_stmt = sdp;
1526ff6d951SJohn Birrell 
1536ff6d951SJohn Birrell 	sdp->dtsd_descattr = descattr;
1546ff6d951SJohn Birrell 	sdp->dtsd_stmtattr = stmtattr;
1556ff6d951SJohn Birrell 
1566ff6d951SJohn Birrell 	return (sdp);
1576ff6d951SJohn Birrell }
1586ff6d951SJohn Birrell 
1596ff6d951SJohn Birrell static dtrace_actdesc_t *
dt_stmt_action(dtrace_hdl_t * dtp,dtrace_stmtdesc_t * sdp)1606ff6d951SJohn Birrell dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
1616ff6d951SJohn Birrell {
1626ff6d951SJohn Birrell 	dtrace_actdesc_t *new;
1636ff6d951SJohn Birrell 
1646ff6d951SJohn Birrell 	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
1656ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1666ff6d951SJohn Birrell 
1676ff6d951SJohn Birrell 	return (new);
1686ff6d951SJohn Birrell }
1696ff6d951SJohn Birrell 
1706ff6d951SJohn Birrell /*
1716ff6d951SJohn Birrell  * Utility function to determine if a given action description is destructive.
1726ff6d951SJohn Birrell  * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
1736ff6d951SJohn Birrell  */
1746ff6d951SJohn Birrell static int
dt_action_destructive(const dtrace_actdesc_t * ap)1756ff6d951SJohn Birrell dt_action_destructive(const dtrace_actdesc_t *ap)
1766ff6d951SJohn Birrell {
1776ff6d951SJohn Birrell 	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
1786ff6d951SJohn Birrell 	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
1796ff6d951SJohn Birrell }
1806ff6d951SJohn Birrell 
1816ff6d951SJohn Birrell static void
dt_stmt_append(dtrace_stmtdesc_t * sdp,const dt_node_t * dnp)1826ff6d951SJohn Birrell dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
1836ff6d951SJohn Birrell {
1846ff6d951SJohn Birrell 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
1856ff6d951SJohn Birrell 	dtrace_actdesc_t *ap, *tap;
1866ff6d951SJohn Birrell 	int commit = 0;
1876ff6d951SJohn Birrell 	int speculate = 0;
1886ff6d951SJohn Birrell 	int datarec = 0;
1896ff6d951SJohn Birrell 
1906ff6d951SJohn Birrell 	/*
1916ff6d951SJohn Birrell 	 * Make sure that the new statement jibes with the rest of the ECB.
1926ff6d951SJohn Birrell 	 */
1936ff6d951SJohn Birrell 	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
1946ff6d951SJohn Birrell 		if (ap->dtad_kind == DTRACEACT_COMMIT) {
1956ff6d951SJohn Birrell 			if (commit) {
1966ff6d951SJohn Birrell 				dnerror(dnp, D_COMM_COMM, "commit( ) may "
1976ff6d951SJohn Birrell 				    "not follow commit( )\n");
1986ff6d951SJohn Birrell 			}
1996ff6d951SJohn Birrell 
2006ff6d951SJohn Birrell 			if (datarec) {
2016ff6d951SJohn Birrell 				dnerror(dnp, D_COMM_DREC, "commit( ) may "
2026ff6d951SJohn Birrell 				    "not follow data-recording action(s)\n");
2036ff6d951SJohn Birrell 			}
2046ff6d951SJohn Birrell 
2056ff6d951SJohn Birrell 			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
2066ff6d951SJohn Birrell 				if (!DTRACEACT_ISAGG(tap->dtad_kind))
2076ff6d951SJohn Birrell 					continue;
2086ff6d951SJohn Birrell 
2096ff6d951SJohn Birrell 				dnerror(dnp, D_AGG_COMM, "aggregating actions "
2106ff6d951SJohn Birrell 				    "may not follow commit( )\n");
2116ff6d951SJohn Birrell 			}
2126ff6d951SJohn Birrell 
2136ff6d951SJohn Birrell 			commit = 1;
2146ff6d951SJohn Birrell 			continue;
2156ff6d951SJohn Birrell 		}
2166ff6d951SJohn Birrell 
2176ff6d951SJohn Birrell 		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
2186ff6d951SJohn Birrell 			if (speculate) {
2196ff6d951SJohn Birrell 				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
2206ff6d951SJohn Birrell 				    "not follow speculate( )\n");
2216ff6d951SJohn Birrell 			}
2226ff6d951SJohn Birrell 
2236ff6d951SJohn Birrell 			if (commit) {
2246ff6d951SJohn Birrell 				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
2256ff6d951SJohn Birrell 				    "not follow commit( )\n");
2266ff6d951SJohn Birrell 			}
2276ff6d951SJohn Birrell 
2286ff6d951SJohn Birrell 			if (datarec) {
2296ff6d951SJohn Birrell 				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
2306ff6d951SJohn Birrell 				    "not follow data-recording action(s)\n");
2316ff6d951SJohn Birrell 			}
2326ff6d951SJohn Birrell 
2336ff6d951SJohn Birrell 			speculate = 1;
2346ff6d951SJohn Birrell 			continue;
2356ff6d951SJohn Birrell 		}
2366ff6d951SJohn Birrell 
2376ff6d951SJohn Birrell 		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
2386ff6d951SJohn Birrell 			if (speculate) {
2396ff6d951SJohn Birrell 				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
2406ff6d951SJohn Birrell 				    "may not follow speculate( )\n");
2416ff6d951SJohn Birrell 			}
2426ff6d951SJohn Birrell 
2436ff6d951SJohn Birrell 			datarec = 1;
2446ff6d951SJohn Birrell 			continue;
2456ff6d951SJohn Birrell 		}
2466ff6d951SJohn Birrell 
2476ff6d951SJohn Birrell 		if (speculate) {
2486ff6d951SJohn Birrell 			if (dt_action_destructive(ap)) {
2496ff6d951SJohn Birrell 				dnerror(dnp, D_ACT_SPEC, "destructive actions "
2506ff6d951SJohn Birrell 				    "may not follow speculate( )\n");
2516ff6d951SJohn Birrell 			}
2526ff6d951SJohn Birrell 
2536ff6d951SJohn Birrell 			if (ap->dtad_kind == DTRACEACT_EXIT) {
2546ff6d951SJohn Birrell 				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
2556ff6d951SJohn Birrell 				    "follow speculate( )\n");
2566ff6d951SJohn Birrell 			}
2576ff6d951SJohn Birrell 		}
2586ff6d951SJohn Birrell 
2596ff6d951SJohn Birrell 		/*
2606ff6d951SJohn Birrell 		 * Exclude all non data-recording actions.
2616ff6d951SJohn Birrell 		 */
2626ff6d951SJohn Birrell 		if (dt_action_destructive(ap) ||
2636ff6d951SJohn Birrell 		    ap->dtad_kind == DTRACEACT_DISCARD)
2646ff6d951SJohn Birrell 			continue;
2656ff6d951SJohn Birrell 
2666ff6d951SJohn Birrell 		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
2676ff6d951SJohn Birrell 		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
2686ff6d951SJohn Birrell 		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
2696ff6d951SJohn Birrell 			continue;
2706ff6d951SJohn Birrell 
2716ff6d951SJohn Birrell 		if (commit) {
2726ff6d951SJohn Birrell 			dnerror(dnp, D_DREC_COMM, "data-recording actions "
2736ff6d951SJohn Birrell 			    "may not follow commit( )\n");
2746ff6d951SJohn Birrell 		}
2756ff6d951SJohn Birrell 
2766ff6d951SJohn Birrell 		if (!speculate)
2776ff6d951SJohn Birrell 			datarec = 1;
2786ff6d951SJohn Birrell 	}
2796ff6d951SJohn Birrell 
2806ff6d951SJohn Birrell 	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
2816ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
2826ff6d951SJohn Birrell 
2836ff6d951SJohn Birrell 	if (yypcb->pcb_stmt == sdp)
2846ff6d951SJohn Birrell 		yypcb->pcb_stmt = NULL;
2856ff6d951SJohn Birrell }
2866ff6d951SJohn Birrell 
2876ff6d951SJohn Birrell /*
2886ff6d951SJohn Birrell  * For the first element of an aggregation tuple or for printa(), we create a
2896ff6d951SJohn Birrell  * simple DIF program that simply returns the immediate value that is the ID
2906ff6d951SJohn Birrell  * of the aggregation itself.  This could be optimized in the future by
2916ff6d951SJohn Birrell  * creating a new in-kernel dtad_kind that just returns an integer.
2926ff6d951SJohn Birrell  */
2936ff6d951SJohn Birrell static void
dt_action_difconst(dtrace_actdesc_t * ap,uint_t id,dtrace_actkind_t kind)2946ff6d951SJohn Birrell dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
2956ff6d951SJohn Birrell {
2966ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2976ff6d951SJohn Birrell 	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
2986ff6d951SJohn Birrell 
2996ff6d951SJohn Birrell 	if (dp == NULL)
3006ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3016ff6d951SJohn Birrell 
3026ff6d951SJohn Birrell 	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
3036ff6d951SJohn Birrell 	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
3046ff6d951SJohn Birrell 
3056ff6d951SJohn Birrell 	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
3066ff6d951SJohn Birrell 		dt_difo_free(dtp, dp);
3076ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
3086ff6d951SJohn Birrell 	}
3096ff6d951SJohn Birrell 
3106ff6d951SJohn Birrell 	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
3116ff6d951SJohn Birrell 	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
3126ff6d951SJohn Birrell 	dp->dtdo_len = 2;
3136ff6d951SJohn Birrell 	dp->dtdo_inttab[0] = id;
3146ff6d951SJohn Birrell 	dp->dtdo_intlen = 1;
3156ff6d951SJohn Birrell 	dp->dtdo_rtype = dt_int_rtype;
3166ff6d951SJohn Birrell 
3176ff6d951SJohn Birrell 	ap->dtad_difo = dp;
3186ff6d951SJohn Birrell 	ap->dtad_kind = kind;
3196ff6d951SJohn Birrell }
3206ff6d951SJohn Birrell 
3216ff6d951SJohn Birrell static void
dt_action_clear(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)3226ff6d951SJohn Birrell dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3236ff6d951SJohn Birrell {
3246ff6d951SJohn Birrell 	dt_ident_t *aid;
3256ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
3266ff6d951SJohn Birrell 	dt_node_t *anp;
3276ff6d951SJohn Birrell 
3286ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
3296ff6d951SJohn Birrell 	int argc = 0;
3306ff6d951SJohn Birrell 
3316ff6d951SJohn Birrell 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3326ff6d951SJohn Birrell 		argc++; /* count up arguments for error messages below */
3336ff6d951SJohn Birrell 
3346ff6d951SJohn Birrell 	if (argc != 1) {
3356ff6d951SJohn Birrell 		dnerror(dnp, D_CLEAR_PROTO,
3366ff6d951SJohn Birrell 		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
3376ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, argc);
3386ff6d951SJohn Birrell 	}
3396ff6d951SJohn Birrell 
3406ff6d951SJohn Birrell 	anp = dnp->dn_args;
3416ff6d951SJohn Birrell 	assert(anp != NULL);
3426ff6d951SJohn Birrell 
3436ff6d951SJohn Birrell 	if (anp->dn_kind != DT_NODE_AGG) {
3446ff6d951SJohn Birrell 		dnerror(dnp, D_CLEAR_AGGARG,
3456ff6d951SJohn Birrell 		    "%s( ) argument #1 is incompatible with prototype:\n"
3466ff6d951SJohn Birrell 		    "\tprototype: aggregation\n\t argument: %s\n",
3476ff6d951SJohn Birrell 		    dnp->dn_ident->di_name,
3486ff6d951SJohn Birrell 		    dt_node_type_name(anp, n, sizeof (n)));
3496ff6d951SJohn Birrell 	}
3506ff6d951SJohn Birrell 
3516ff6d951SJohn Birrell 	aid = anp->dn_ident;
3526ff6d951SJohn Birrell 
3536ff6d951SJohn Birrell 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
3546ff6d951SJohn Birrell 		dnerror(dnp, D_CLEAR_AGGBAD,
3556ff6d951SJohn Birrell 		    "undefined aggregation: @%s\n", aid->di_name);
3566ff6d951SJohn Birrell 	}
3576ff6d951SJohn Birrell 
3586ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
3596ff6d951SJohn Birrell 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
3606ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_CLEAR;
3616ff6d951SJohn Birrell }
3626ff6d951SJohn Birrell 
3636ff6d951SJohn Birrell static void
dt_action_normalize(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)3646ff6d951SJohn Birrell dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
3656ff6d951SJohn Birrell {
3666ff6d951SJohn Birrell 	dt_ident_t *aid;
3676ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
3686ff6d951SJohn Birrell 	dt_node_t *anp, *normal;
3696ff6d951SJohn Birrell 	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
3706ff6d951SJohn Birrell 
3716ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
3726ff6d951SJohn Birrell 	int argc = 0;
3736ff6d951SJohn Birrell 
3746ff6d951SJohn Birrell 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
3756ff6d951SJohn Birrell 		argc++; /* count up arguments for error messages below */
3766ff6d951SJohn Birrell 
3776ff6d951SJohn Birrell 	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
3786ff6d951SJohn Birrell 		dnerror(dnp, D_NORMALIZE_PROTO,
3796ff6d951SJohn Birrell 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
3806ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
3816ff6d951SJohn Birrell 	}
3826ff6d951SJohn Birrell 
3836ff6d951SJohn Birrell 	anp = dnp->dn_args;
3846ff6d951SJohn Birrell 	assert(anp != NULL);
3856ff6d951SJohn Birrell 
3866ff6d951SJohn Birrell 	if (anp->dn_kind != DT_NODE_AGG) {
3876ff6d951SJohn Birrell 		dnerror(dnp, D_NORMALIZE_AGGARG,
3886ff6d951SJohn Birrell 		    "%s( ) argument #1 is incompatible with prototype:\n"
3896ff6d951SJohn Birrell 		    "\tprototype: aggregation\n\t argument: %s\n",
3906ff6d951SJohn Birrell 		    dnp->dn_ident->di_name,
3916ff6d951SJohn Birrell 		    dt_node_type_name(anp, n, sizeof (n)));
3926ff6d951SJohn Birrell 	}
3936ff6d951SJohn Birrell 
3946ff6d951SJohn Birrell 	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
3956ff6d951SJohn Birrell 		dnerror(dnp, D_NORMALIZE_SCALAR,
3966ff6d951SJohn Birrell 		    "%s( ) argument #2 must be of scalar type\n",
3976ff6d951SJohn Birrell 		    dnp->dn_ident->di_name);
3986ff6d951SJohn Birrell 	}
3996ff6d951SJohn Birrell 
4006ff6d951SJohn Birrell 	aid = anp->dn_ident;
4016ff6d951SJohn Birrell 
4026ff6d951SJohn Birrell 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4036ff6d951SJohn Birrell 		dnerror(dnp, D_NORMALIZE_AGGBAD,
4046ff6d951SJohn Birrell 		    "undefined aggregation: @%s\n", aid->di_name);
4056ff6d951SJohn Birrell 	}
4066ff6d951SJohn Birrell 
4076ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
4086ff6d951SJohn Birrell 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4096ff6d951SJohn Birrell 
4106ff6d951SJohn Birrell 	if (denormal) {
4116ff6d951SJohn Birrell 		ap->dtad_arg = DT_ACT_DENORMALIZE;
4126ff6d951SJohn Birrell 		return;
4136ff6d951SJohn Birrell 	}
4146ff6d951SJohn Birrell 
4156ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_NORMALIZE;
4166ff6d951SJohn Birrell 
4176ff6d951SJohn Birrell 	assert(normal != NULL);
4186ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
4196ff6d951SJohn Birrell 	dt_cg(yypcb, normal);
4206ff6d951SJohn Birrell 
4216ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
4226ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_LIBACT;
4236ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_NORMALIZE;
4246ff6d951SJohn Birrell }
4256ff6d951SJohn Birrell 
4266ff6d951SJohn Birrell static void
dt_action_trunc(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)4276ff6d951SJohn Birrell dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4286ff6d951SJohn Birrell {
4296ff6d951SJohn Birrell 	dt_ident_t *aid;
4306ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
4316ff6d951SJohn Birrell 	dt_node_t *anp, *trunc;
4326ff6d951SJohn Birrell 
4336ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
4346ff6d951SJohn Birrell 	int argc = 0;
4356ff6d951SJohn Birrell 
4366ff6d951SJohn Birrell 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
4376ff6d951SJohn Birrell 		argc++; /* count up arguments for error messages below */
4386ff6d951SJohn Birrell 
4396ff6d951SJohn Birrell 	if (argc > 2 || argc < 1) {
4406ff6d951SJohn Birrell 		dnerror(dnp, D_TRUNC_PROTO,
4416ff6d951SJohn Birrell 		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
4426ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, argc,
4436ff6d951SJohn Birrell 		    argc < 1 ? "at least 1" : "no more than 2");
4446ff6d951SJohn Birrell 	}
4456ff6d951SJohn Birrell 
4466ff6d951SJohn Birrell 	anp = dnp->dn_args;
4476ff6d951SJohn Birrell 	assert(anp != NULL);
4486ff6d951SJohn Birrell 	trunc = anp->dn_list;
4496ff6d951SJohn Birrell 
4506ff6d951SJohn Birrell 	if (anp->dn_kind != DT_NODE_AGG) {
4516ff6d951SJohn Birrell 		dnerror(dnp, D_TRUNC_AGGARG,
4526ff6d951SJohn Birrell 		    "%s( ) argument #1 is incompatible with prototype:\n"
4536ff6d951SJohn Birrell 		    "\tprototype: aggregation\n\t argument: %s\n",
4546ff6d951SJohn Birrell 		    dnp->dn_ident->di_name,
4556ff6d951SJohn Birrell 		    dt_node_type_name(anp, n, sizeof (n)));
4566ff6d951SJohn Birrell 	}
4576ff6d951SJohn Birrell 
4586ff6d951SJohn Birrell 	if (argc == 2) {
4596ff6d951SJohn Birrell 		assert(trunc != NULL);
4606ff6d951SJohn Birrell 		if (!dt_node_is_scalar(trunc)) {
4616ff6d951SJohn Birrell 			dnerror(dnp, D_TRUNC_SCALAR,
4626ff6d951SJohn Birrell 			    "%s( ) argument #2 must be of scalar type\n",
4636ff6d951SJohn Birrell 			    dnp->dn_ident->di_name);
4646ff6d951SJohn Birrell 		}
4656ff6d951SJohn Birrell 	}
4666ff6d951SJohn Birrell 
4676ff6d951SJohn Birrell 	aid = anp->dn_ident;
4686ff6d951SJohn Birrell 
4696ff6d951SJohn Birrell 	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
4706ff6d951SJohn Birrell 		dnerror(dnp, D_TRUNC_AGGBAD,
4716ff6d951SJohn Birrell 		    "undefined aggregation: @%s\n", aid->di_name);
4726ff6d951SJohn Birrell 	}
4736ff6d951SJohn Birrell 
4746ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
4756ff6d951SJohn Birrell 	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
4766ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_TRUNC;
4776ff6d951SJohn Birrell 
4786ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
4796ff6d951SJohn Birrell 
4806ff6d951SJohn Birrell 	if (argc == 1) {
4816ff6d951SJohn Birrell 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
4826ff6d951SJohn Birrell 	} else {
4836ff6d951SJohn Birrell 		assert(trunc != NULL);
4846ff6d951SJohn Birrell 		dt_cg(yypcb, trunc);
4856ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
4866ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_LIBACT;
4876ff6d951SJohn Birrell 	}
4886ff6d951SJohn Birrell 
4896ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_TRUNC;
4906ff6d951SJohn Birrell }
4916ff6d951SJohn Birrell 
4926ff6d951SJohn Birrell static void
dt_action_printa(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)4936ff6d951SJohn Birrell dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
4946ff6d951SJohn Birrell {
4956ff6d951SJohn Birrell 	dt_ident_t *aid, *fid;
4966ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
4976ff6d951SJohn Birrell 	const char *format;
4986ff6d951SJohn Birrell 	dt_node_t *anp, *proto = NULL;
4996ff6d951SJohn Birrell 
5006ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
5016ff6d951SJohn Birrell 	int argc = 0, argr = 0;
5026ff6d951SJohn Birrell 
5036ff6d951SJohn Birrell 	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
5046ff6d951SJohn Birrell 		argc++; /* count up arguments for error messages below */
5056ff6d951SJohn Birrell 
5066ff6d951SJohn Birrell 	switch (dnp->dn_args->dn_kind) {
5076ff6d951SJohn Birrell 	case DT_NODE_STRING:
5086ff6d951SJohn Birrell 		format = dnp->dn_args->dn_string;
5096ff6d951SJohn Birrell 		anp = dnp->dn_args->dn_list;
5106ff6d951SJohn Birrell 		argr = 2;
5116ff6d951SJohn Birrell 		break;
5126ff6d951SJohn Birrell 	case DT_NODE_AGG:
5136ff6d951SJohn Birrell 		format = NULL;
5146ff6d951SJohn Birrell 		anp = dnp->dn_args;
5156ff6d951SJohn Birrell 		argr = 1;
5166ff6d951SJohn Birrell 		break;
5176ff6d951SJohn Birrell 	default:
5186ff6d951SJohn Birrell 		format = NULL;
5196ff6d951SJohn Birrell 		anp = dnp->dn_args;
5206ff6d951SJohn Birrell 		argr = 1;
5216ff6d951SJohn Birrell 	}
5226ff6d951SJohn Birrell 
5236ff6d951SJohn Birrell 	if (argc < argr) {
5246ff6d951SJohn Birrell 		dnerror(dnp, D_PRINTA_PROTO,
5256ff6d951SJohn Birrell 		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
5266ff6d951SJohn Birrell 		    dnp->dn_ident->di_name, argc, argr);
5276ff6d951SJohn Birrell 	}
5286ff6d951SJohn Birrell 
5296ff6d951SJohn Birrell 	assert(anp != NULL);
5306ff6d951SJohn Birrell 
5316ff6d951SJohn Birrell 	while (anp != NULL) {
5326ff6d951SJohn Birrell 		if (anp->dn_kind != DT_NODE_AGG) {
5336ff6d951SJohn Birrell 			dnerror(dnp, D_PRINTA_AGGARG,
5346ff6d951SJohn Birrell 			    "%s( ) argument #%d is incompatible with "
5356ff6d951SJohn Birrell 			    "prototype:\n\tprototype: aggregation\n"
5366ff6d951SJohn Birrell 			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
5376ff6d951SJohn Birrell 			    dt_node_type_name(anp, n, sizeof (n)));
5386ff6d951SJohn Birrell 		}
5396ff6d951SJohn Birrell 
5406ff6d951SJohn Birrell 		aid = anp->dn_ident;
5416ff6d951SJohn Birrell 		fid = aid->di_iarg;
5426ff6d951SJohn Birrell 
5436ff6d951SJohn Birrell 		if (aid->di_gen == dtp->dt_gen &&
5446ff6d951SJohn Birrell 		    !(aid->di_flags & DT_IDFLG_MOD)) {
5456ff6d951SJohn Birrell 			dnerror(dnp, D_PRINTA_AGGBAD,
5466ff6d951SJohn Birrell 			    "undefined aggregation: @%s\n", aid->di_name);
5476ff6d951SJohn Birrell 		}
5486ff6d951SJohn Birrell 
5496ff6d951SJohn Birrell 		/*
5506ff6d951SJohn Birrell 		 * If we have multiple aggregations, we must be sure that
5516ff6d951SJohn Birrell 		 * their key signatures match.
5526ff6d951SJohn Birrell 		 */
5536ff6d951SJohn Birrell 		if (proto != NULL) {
5546ff6d951SJohn Birrell 			dt_printa_validate(proto, anp);
5556ff6d951SJohn Birrell 		} else {
5566ff6d951SJohn Birrell 			proto = anp;
5576ff6d951SJohn Birrell 		}
5586ff6d951SJohn Birrell 
5596ff6d951SJohn Birrell 		if (format != NULL) {
5606ff6d951SJohn Birrell 			yylineno = dnp->dn_line;
5616ff6d951SJohn Birrell 
5626ff6d951SJohn Birrell 			sdp->dtsd_fmtdata =
5636ff6d951SJohn Birrell 			    dt_printf_create(yypcb->pcb_hdl, format);
5646ff6d951SJohn Birrell 			dt_printf_validate(sdp->dtsd_fmtdata,
5656ff6d951SJohn Birrell 			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
5666ff6d951SJohn Birrell 			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
5676ff6d951SJohn Birrell 			format = NULL;
5686ff6d951SJohn Birrell 		}
5696ff6d951SJohn Birrell 
5706ff6d951SJohn Birrell 		ap = dt_stmt_action(dtp, sdp);
5716ff6d951SJohn Birrell 		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
5726ff6d951SJohn Birrell 
5736ff6d951SJohn Birrell 		anp = anp->dn_list;
5746ff6d951SJohn Birrell 		argr++;
5756ff6d951SJohn Birrell 	}
5766ff6d951SJohn Birrell }
5776ff6d951SJohn Birrell 
5786ff6d951SJohn Birrell static void
dt_action_printflike(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)5796ff6d951SJohn Birrell dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
5806ff6d951SJohn Birrell     dtrace_actkind_t kind)
5816ff6d951SJohn Birrell {
5826ff6d951SJohn Birrell 	dt_node_t *anp, *arg1;
5836ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = NULL;
5846ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN], *str;
5856ff6d951SJohn Birrell 
5866ff6d951SJohn Birrell 	assert(DTRACEACT_ISPRINTFLIKE(kind));
5876ff6d951SJohn Birrell 
5886ff6d951SJohn Birrell 	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
5896ff6d951SJohn Birrell 		dnerror(dnp, D_PRINTF_ARG_FMT,
5906ff6d951SJohn Birrell 		    "%s( ) argument #1 is incompatible with prototype:\n"
5916ff6d951SJohn Birrell 		    "\tprototype: string constant\n\t argument: %s\n",
5926ff6d951SJohn Birrell 		    dnp->dn_ident->di_name,
5936ff6d951SJohn Birrell 		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
5946ff6d951SJohn Birrell 	}
5956ff6d951SJohn Birrell 
5966ff6d951SJohn Birrell 	arg1 = dnp->dn_args->dn_list;
5976ff6d951SJohn Birrell 	yylineno = dnp->dn_line;
5986ff6d951SJohn Birrell 	str = dnp->dn_args->dn_string;
5996ff6d951SJohn Birrell 
6006ff6d951SJohn Birrell 
6016ff6d951SJohn Birrell 	/*
6026ff6d951SJohn Birrell 	 * If this is an freopen(), we use an empty string to denote that
6036ff6d951SJohn Birrell 	 * stdout should be restored.  For other printf()-like actions, an
6046ff6d951SJohn Birrell 	 * empty format string is illegal:  an empty format string would
6056ff6d951SJohn Birrell 	 * result in malformed DOF, and the compiler thus flags an empty
6066ff6d951SJohn Birrell 	 * format string as a compile-time error.  To avoid propagating the
6076ff6d951SJohn Birrell 	 * freopen() special case throughout the system, we simply transpose
6086ff6d951SJohn Birrell 	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
6096ff6d951SJohn Birrell 	 * denotes that stdout should be restored.
6106ff6d951SJohn Birrell 	 */
6116ff6d951SJohn Birrell 	if (kind == DTRACEACT_FREOPEN) {
6126ff6d951SJohn Birrell 		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
6136ff6d951SJohn Birrell 			/*
6146ff6d951SJohn Birrell 			 * Our sentinel is always an invalid argument to
6156ff6d951SJohn Birrell 			 * freopen(), but if it's been manually specified, we
6166ff6d951SJohn Birrell 			 * must fail now instead of when the freopen() is
6176ff6d951SJohn Birrell 			 * actually evaluated.
6186ff6d951SJohn Birrell 			 */
6196ff6d951SJohn Birrell 			dnerror(dnp, D_FREOPEN_INVALID,
6206ff6d951SJohn Birrell 			    "%s( ) argument #1 cannot be \"%s\"\n",
6216ff6d951SJohn Birrell 			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
6226ff6d951SJohn Birrell 		}
6236ff6d951SJohn Birrell 
6246ff6d951SJohn Birrell 		if (str[0] == '\0')
6256ff6d951SJohn Birrell 			str = DT_FREOPEN_RESTORE;
6266ff6d951SJohn Birrell 	}
6276ff6d951SJohn Birrell 
6286ff6d951SJohn Birrell 	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
6296ff6d951SJohn Birrell 
6306ff6d951SJohn Birrell 	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
6316ff6d951SJohn Birrell 	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
6326ff6d951SJohn Birrell 
6336ff6d951SJohn Birrell 	if (arg1 == NULL) {
6346ff6d951SJohn Birrell 		dif_instr_t *dbuf;
6356ff6d951SJohn Birrell 		dtrace_difo_t *dp;
6366ff6d951SJohn Birrell 
6376ff6d951SJohn Birrell 		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
6386ff6d951SJohn Birrell 		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
6396ff6d951SJohn Birrell 			dt_free(dtp, dbuf);
6406ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
6416ff6d951SJohn Birrell 		}
6426ff6d951SJohn Birrell 
6436ff6d951SJohn Birrell 		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
6446ff6d951SJohn Birrell 
6456ff6d951SJohn Birrell 		dp->dtdo_buf = dbuf;
6466ff6d951SJohn Birrell 		dp->dtdo_len = 1;
6476ff6d951SJohn Birrell 		dp->dtdo_rtype = dt_int_rtype;
6486ff6d951SJohn Birrell 
6496ff6d951SJohn Birrell 		ap = dt_stmt_action(dtp, sdp);
6506ff6d951SJohn Birrell 		ap->dtad_difo = dp;
6516ff6d951SJohn Birrell 		ap->dtad_kind = kind;
6526ff6d951SJohn Birrell 		return;
6536ff6d951SJohn Birrell 	}
6546ff6d951SJohn Birrell 
6556ff6d951SJohn Birrell 	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
6566ff6d951SJohn Birrell 		ap = dt_stmt_action(dtp, sdp);
6576ff6d951SJohn Birrell 		dt_cg(yypcb, anp);
6586ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
6596ff6d951SJohn Birrell 		ap->dtad_kind = kind;
6606ff6d951SJohn Birrell 	}
6616ff6d951SJohn Birrell }
6626ff6d951SJohn Birrell 
6636ff6d951SJohn Birrell static void
dt_action_trace(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)6646ff6d951SJohn Birrell dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
6656ff6d951SJohn Birrell {
6668e648814SRui Paulo 	int ctflib;
6678e648814SRui Paulo 
6686ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
669a98ff317SPedro F. Giffuni 	boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
670a98ff317SPedro F. Giffuni 	const char *act = istrace ?  "trace" : "print";
6716ff6d951SJohn Birrell 
6726ff6d951SJohn Birrell 	if (dt_node_is_void(dnp->dn_args)) {
673a98ff317SPedro F. Giffuni 		dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
674a98ff317SPedro F. Giffuni 		    "%s( ) may not be applied to a void expression\n", act);
6756ff6d951SJohn Birrell 	}
6766ff6d951SJohn Birrell 
677a98ff317SPedro F. Giffuni 	if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
678a98ff317SPedro F. Giffuni 		dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
679a98ff317SPedro F. Giffuni 		    "%s( ) may not be applied to a translated pointer\n", act);
6806ff6d951SJohn Birrell 	}
6816ff6d951SJohn Birrell 
68241840d75SPedro F. Giffuni 	if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
68341840d75SPedro F. Giffuni 		dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
68441840d75SPedro F. Giffuni 		    "%s( ) may not be applied to an aggregation%s\n", act,
68541840d75SPedro F. Giffuni 		    istrace ? "" : " -- did you mean printa()?");
68641840d75SPedro F. Giffuni 	}
68741840d75SPedro F. Giffuni 
6886ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
6896ff6d951SJohn Birrell 
69054727873SPedro F. Giffuni 	/*
691a98ff317SPedro F. Giffuni 	 * The print() action behaves identically to trace(), except that it
692a98ff317SPedro F. Giffuni 	 * stores the CTF type of the argument (if present) within the DOF for
693a98ff317SPedro F. Giffuni 	 * the DIFEXPR action.  To do this, we set the 'dtsd_strdata' to point
694a98ff317SPedro F. Giffuni 	 * to the fully-qualified CTF type ID for the result of the DIF
695a98ff317SPedro F. Giffuni 	 * action.  We use the ID instead of the name to handles complex types
696a98ff317SPedro F. Giffuni 	 * like arrays and function pointers that can't be resolved by
697a98ff317SPedro F. Giffuni 	 * ctf_type_lookup().  This is later processed by dtrace_dof_create()
698a98ff317SPedro F. Giffuni 	 * and turned into a reference into the string table so that we can
6998e648814SRui Paulo 	 * get the type information when we process the data after the fact.  In
7008e648814SRui Paulo 	 * the case where we are referring to userland CTF data, we also need to
7018e648814SRui Paulo 	 * to identify which ctf container in question we care about and encode
7028e648814SRui Paulo 	 * that within the name.
70354727873SPedro F. Giffuni 	 */
704a98ff317SPedro F. Giffuni 	if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
70554727873SPedro F. Giffuni 		dt_node_t *dret;
706a98ff317SPedro F. Giffuni 		size_t n;
70754727873SPedro F. Giffuni 		dt_module_t *dmp;
70854727873SPedro F. Giffuni 
70954727873SPedro F. Giffuni 		dret = yypcb->pcb_dret;
71054727873SPedro F. Giffuni 		dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
71154727873SPedro F. Giffuni 
712a98ff317SPedro F. Giffuni 		n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name, dret->dn_type) + 1;
7138e648814SRui Paulo 		if (dmp->dm_pid != 0) {
7148e648814SRui Paulo 			ctflib = dt_module_getlibid(dtp, dmp, dret->dn_ctfp);
7158e648814SRui Paulo 			assert(ctflib >= 0);
7168e648814SRui Paulo 			n = snprintf(NULL, 0, "%s`%d`%ld", dmp->dm_name,
7178e648814SRui Paulo 			    ctflib, dret->dn_type) + 1;
7188e648814SRui Paulo 		} else {
7198e648814SRui Paulo 			n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name,
7208e648814SRui Paulo 			    dret->dn_type) + 1;
7218e648814SRui Paulo 		}
722a98ff317SPedro F. Giffuni 		sdp->dtsd_strdata = dt_alloc(dtp, n);
72354727873SPedro F. Giffuni 		if (sdp->dtsd_strdata == NULL)
72454727873SPedro F. Giffuni 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
725a98ff317SPedro F. Giffuni 		(void) snprintf(sdp->dtsd_strdata, n, "%s`%ld", dmp->dm_name,
72654727873SPedro F. Giffuni 		    dret->dn_type);
7278e648814SRui Paulo 		if (dmp->dm_pid != 0) {
7288e648814SRui Paulo 			(void) snprintf(sdp->dtsd_strdata, n, "%s`%d`%ld",
7298e648814SRui Paulo 			    dmp->dm_name, ctflib, dret->dn_type);
7308e648814SRui Paulo 		} else {
7318e648814SRui Paulo 			(void) snprintf(sdp->dtsd_strdata, n, "%s`%ld",
7328e648814SRui Paulo 			    dmp->dm_name, dret->dn_type);
7338e648814SRui Paulo 		}
734a98ff317SPedro F. Giffuni 	}
73554727873SPedro F. Giffuni 
73654727873SPedro F. Giffuni 	ap->dtad_difo = dt_as(yypcb);
73754727873SPedro F. Giffuni 	ap->dtad_kind = DTRACEACT_DIFEXPR;
73854727873SPedro F. Giffuni }
73954727873SPedro F. Giffuni 
7406ff6d951SJohn Birrell static void
dt_action_tracemem(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)7416ff6d951SJohn Birrell dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
7426ff6d951SJohn Birrell {
7436ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
7446ff6d951SJohn Birrell 
7456ff6d951SJohn Birrell 	dt_node_t *addr = dnp->dn_args;
746f2e66d30SPedro F. Giffuni 	dt_node_t *max = dnp->dn_args->dn_list;
747f2e66d30SPedro F. Giffuni 	dt_node_t *size;
7486ff6d951SJohn Birrell 
7496ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
7506ff6d951SJohn Birrell 
7516ff6d951SJohn Birrell 	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
7526ff6d951SJohn Birrell 		dnerror(addr, D_TRACEMEM_ADDR,
7536ff6d951SJohn Birrell 		    "tracemem( ) argument #1 is incompatible with "
7546ff6d951SJohn Birrell 		    "prototype:\n\tprototype: pointer or integer\n"
7556ff6d951SJohn Birrell 		    "\t argument: %s\n",
7566ff6d951SJohn Birrell 		    dt_node_type_name(addr, n, sizeof (n)));
7576ff6d951SJohn Birrell 	}
7586ff6d951SJohn Birrell 
759f2e66d30SPedro F. Giffuni 	if (dt_node_is_posconst(max) == 0) {
760f2e66d30SPedro F. Giffuni 		dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
7616ff6d951SJohn Birrell 		    "be a non-zero positive integral constant expression\n");
7626ff6d951SJohn Birrell 	}
7636ff6d951SJohn Birrell 
764f2e66d30SPedro F. Giffuni 	if ((size = max->dn_list) != NULL) {
765f2e66d30SPedro F. Giffuni 		if (size->dn_list != NULL) {
766f2e66d30SPedro F. Giffuni 			dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
767f2e66d30SPedro F. Giffuni 			    "mismatch: expected at most 3 args\n");
768f2e66d30SPedro F. Giffuni 		}
769f2e66d30SPedro F. Giffuni 
770f2e66d30SPedro F. Giffuni 		if (!dt_node_is_scalar(size)) {
771f2e66d30SPedro F. Giffuni 			dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
772f2e66d30SPedro F. Giffuni 			    "dynamic size (argument #3) must be of "
773f2e66d30SPedro F. Giffuni 			    "scalar type\n");
774f2e66d30SPedro F. Giffuni 		}
775f2e66d30SPedro F. Giffuni 
776f2e66d30SPedro F. Giffuni 		dt_cg(yypcb, size);
777f2e66d30SPedro F. Giffuni 		ap->dtad_difo = dt_as(yypcb);
778f2e66d30SPedro F. Giffuni 		ap->dtad_difo->dtdo_rtype = dt_int_rtype;
779f2e66d30SPedro F. Giffuni 		ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
780f2e66d30SPedro F. Giffuni 
781f2e66d30SPedro F. Giffuni 		ap = dt_stmt_action(dtp, sdp);
782f2e66d30SPedro F. Giffuni 	}
783f2e66d30SPedro F. Giffuni 
7846ff6d951SJohn Birrell 	dt_cg(yypcb, addr);
7856ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
786f2e66d30SPedro F. Giffuni 	ap->dtad_kind = DTRACEACT_TRACEMEM;
7876ff6d951SJohn Birrell 
7886ff6d951SJohn Birrell 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
789f2e66d30SPedro F. Giffuni 	ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
7906ff6d951SJohn Birrell }
7916ff6d951SJohn Birrell 
7926ff6d951SJohn Birrell static void
dt_action_stack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * arg0)7936ff6d951SJohn Birrell dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
7946ff6d951SJohn Birrell {
7956ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_STACK;
7966ff6d951SJohn Birrell 
7976ff6d951SJohn Birrell 	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
7986ff6d951SJohn Birrell 		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
7996ff6d951SJohn Birrell 	} else {
8006ff6d951SJohn Birrell 		ap->dtad_arg = 0;
8016ff6d951SJohn Birrell 	}
8026ff6d951SJohn Birrell 
8036ff6d951SJohn Birrell 	if (arg0 != NULL) {
8046ff6d951SJohn Birrell 		if (arg0->dn_list != NULL) {
8056ff6d951SJohn Birrell 			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
8066ff6d951SJohn Birrell 			    "mismatch: too many arguments\n");
8076ff6d951SJohn Birrell 		}
8086ff6d951SJohn Birrell 
8096ff6d951SJohn Birrell 		if (dt_node_is_posconst(arg0) == 0) {
8106ff6d951SJohn Birrell 			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
8116ff6d951SJohn Birrell 			    "non-zero positive integral constant expression\n");
8126ff6d951SJohn Birrell 		}
8136ff6d951SJohn Birrell 
8146ff6d951SJohn Birrell 		ap->dtad_arg = arg0->dn_value;
8156ff6d951SJohn Birrell 	}
8166ff6d951SJohn Birrell }
8176ff6d951SJohn Birrell 
8186ff6d951SJohn Birrell static void
dt_action_stack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8196ff6d951SJohn Birrell dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8206ff6d951SJohn Birrell {
8216ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8226ff6d951SJohn Birrell 	dt_action_stack_args(dtp, ap, dnp->dn_args);
8236ff6d951SJohn Birrell }
8246ff6d951SJohn Birrell 
8256ff6d951SJohn Birrell static void
dt_action_ustack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp)8266ff6d951SJohn Birrell dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
8276ff6d951SJohn Birrell {
8286ff6d951SJohn Birrell 	uint32_t nframes = 0;
8296ff6d951SJohn Birrell 	uint32_t strsize = 0;	/* default string table size */
8306ff6d951SJohn Birrell 	dt_node_t *arg0 = dnp->dn_args;
8316ff6d951SJohn Birrell 	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
8326ff6d951SJohn Birrell 
8336ff6d951SJohn Birrell 	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
8346ff6d951SJohn Birrell 	    dnp->dn_ident->di_id == DT_ACT_USTACK);
8356ff6d951SJohn Birrell 
8366ff6d951SJohn Birrell 	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
8376ff6d951SJohn Birrell 		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
8386ff6d951SJohn Birrell 			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
8396ff6d951SJohn Birrell 
8406ff6d951SJohn Birrell 		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
8416ff6d951SJohn Birrell 			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
8426ff6d951SJohn Birrell 
8436ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_JSTACK;
8446ff6d951SJohn Birrell 	} else {
8456ff6d951SJohn Birrell 		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
8466ff6d951SJohn Birrell 
8476ff6d951SJohn Birrell 		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
8486ff6d951SJohn Birrell 			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
8496ff6d951SJohn Birrell 
8506ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_USTACK;
8516ff6d951SJohn Birrell 	}
8526ff6d951SJohn Birrell 
8536ff6d951SJohn Birrell 	if (arg0 != NULL) {
8546ff6d951SJohn Birrell 		if (!dt_node_is_posconst(arg0)) {
8556ff6d951SJohn Birrell 			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
8566ff6d951SJohn Birrell 			    "must be a non-zero positive integer constant\n");
8576ff6d951SJohn Birrell 		}
8586ff6d951SJohn Birrell 		nframes = (uint32_t)arg0->dn_value;
8596ff6d951SJohn Birrell 	}
8606ff6d951SJohn Birrell 
8616ff6d951SJohn Birrell 	if (arg1 != NULL) {
8626ff6d951SJohn Birrell 		if (arg1->dn_kind != DT_NODE_INT ||
8636ff6d951SJohn Birrell 		    ((arg1->dn_flags & DT_NF_SIGNED) &&
8646ff6d951SJohn Birrell 		    (int64_t)arg1->dn_value < 0)) {
8656ff6d951SJohn Birrell 			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
8666ff6d951SJohn Birrell 			    "must be a positive integer constant\n");
8676ff6d951SJohn Birrell 		}
8686ff6d951SJohn Birrell 
8696ff6d951SJohn Birrell 		if (arg1->dn_list != NULL) {
8706ff6d951SJohn Birrell 			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
8716ff6d951SJohn Birrell 			    "mismatch: too many arguments\n");
8726ff6d951SJohn Birrell 		}
8736ff6d951SJohn Birrell 
8746ff6d951SJohn Birrell 		strsize = (uint32_t)arg1->dn_value;
8756ff6d951SJohn Birrell 	}
8766ff6d951SJohn Birrell 
8776ff6d951SJohn Birrell 	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
8786ff6d951SJohn Birrell }
8796ff6d951SJohn Birrell 
8806ff6d951SJohn Birrell static void
dt_action_ustack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8816ff6d951SJohn Birrell dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8826ff6d951SJohn Birrell {
8836ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
8846ff6d951SJohn Birrell 	dt_action_ustack_args(dtp, ap, dnp);
8856ff6d951SJohn Birrell }
8866ff6d951SJohn Birrell 
8876ff6d951SJohn Birrell static void
dt_action_setopt(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)8886ff6d951SJohn Birrell dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
8896ff6d951SJohn Birrell {
8906ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
8916ff6d951SJohn Birrell 	dt_node_t *arg0, *arg1;
8926ff6d951SJohn Birrell 
8936ff6d951SJohn Birrell 	/*
8946ff6d951SJohn Birrell 	 * The prototype guarantees that we are called with either one or
8956ff6d951SJohn Birrell 	 * two arguments, and that any arguments that are present are strings.
8966ff6d951SJohn Birrell 	 */
8976ff6d951SJohn Birrell 	arg0 = dnp->dn_args;
8986ff6d951SJohn Birrell 	arg1 = arg0->dn_list;
8996ff6d951SJohn Birrell 
9006ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
9016ff6d951SJohn Birrell 	dt_cg(yypcb, arg0);
9026ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
9036ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_LIBACT;
9046ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_SETOPT;
9056ff6d951SJohn Birrell 
9066ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
9076ff6d951SJohn Birrell 
9086ff6d951SJohn Birrell 	if (arg1 == NULL) {
9096ff6d951SJohn Birrell 		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
9106ff6d951SJohn Birrell 	} else {
9116ff6d951SJohn Birrell 		dt_cg(yypcb, arg1);
9126ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
9136ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_LIBACT;
9146ff6d951SJohn Birrell 	}
9156ff6d951SJohn Birrell 
9166ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_SETOPT;
9176ff6d951SJohn Birrell }
9186ff6d951SJohn Birrell 
9196ff6d951SJohn Birrell /*ARGSUSED*/
9206ff6d951SJohn Birrell static void
dt_action_symmod_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp,dtrace_actkind_t kind)9216ff6d951SJohn Birrell dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
9226ff6d951SJohn Birrell     dt_node_t *dnp, dtrace_actkind_t kind)
9236ff6d951SJohn Birrell {
9246ff6d951SJohn Birrell 	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
9256ff6d951SJohn Birrell 	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
9266ff6d951SJohn Birrell 	    kind == DTRACEACT_UADDR);
9276ff6d951SJohn Birrell 
9286ff6d951SJohn Birrell 	dt_cg(yypcb, dnp);
9296ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
9306ff6d951SJohn Birrell 	ap->dtad_kind = kind;
9316ff6d951SJohn Birrell 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
9326ff6d951SJohn Birrell }
9336ff6d951SJohn Birrell 
9346ff6d951SJohn Birrell static void
dt_action_symmod(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)9356ff6d951SJohn Birrell dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
9366ff6d951SJohn Birrell     dtrace_actkind_t kind)
9376ff6d951SJohn Birrell {
9386ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9396ff6d951SJohn Birrell 	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
9406ff6d951SJohn Birrell }
9416ff6d951SJohn Birrell 
9426ff6d951SJohn Birrell /*ARGSUSED*/
9436ff6d951SJohn Birrell static void
dt_action_ftruncate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9446ff6d951SJohn Birrell dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9456ff6d951SJohn Birrell {
9466ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9476ff6d951SJohn Birrell 
9486ff6d951SJohn Birrell 	/*
9496ff6d951SJohn Birrell 	 * Library actions need a DIFO that serves as an argument.  As
9506ff6d951SJohn Birrell 	 * ftruncate() doesn't take an argument, we generate the constant 0
9516ff6d951SJohn Birrell 	 * in a DIFO; this constant will be ignored when the ftruncate() is
9526ff6d951SJohn Birrell 	 * processed.
9536ff6d951SJohn Birrell 	 */
9546ff6d951SJohn Birrell 	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
9556ff6d951SJohn Birrell 	ap->dtad_arg = DT_ACT_FTRUNCATE;
9566ff6d951SJohn Birrell }
9576ff6d951SJohn Birrell 
9586ff6d951SJohn Birrell /*ARGSUSED*/
9596ff6d951SJohn Birrell static void
dt_action_stop(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9606ff6d951SJohn Birrell dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9616ff6d951SJohn Birrell {
9626ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9636ff6d951SJohn Birrell 
9646ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_STOP;
9656ff6d951SJohn Birrell 	ap->dtad_arg = 0;
9666ff6d951SJohn Birrell }
9676ff6d951SJohn Birrell 
9686ff6d951SJohn Birrell /*ARGSUSED*/
9696ff6d951SJohn Birrell static void
dt_action_breakpoint(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9706ff6d951SJohn Birrell dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9716ff6d951SJohn Birrell {
9726ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9736ff6d951SJohn Birrell 
9746ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_BREAKPOINT;
9756ff6d951SJohn Birrell 	ap->dtad_arg = 0;
9766ff6d951SJohn Birrell }
9776ff6d951SJohn Birrell 
9786ff6d951SJohn Birrell /*ARGSUSED*/
9796ff6d951SJohn Birrell static void
dt_action_panic(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9806ff6d951SJohn Birrell dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9816ff6d951SJohn Birrell {
9826ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9836ff6d951SJohn Birrell 
9846ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_PANIC;
9856ff6d951SJohn Birrell 	ap->dtad_arg = 0;
9866ff6d951SJohn Birrell }
9876ff6d951SJohn Birrell 
9886ff6d951SJohn Birrell static void
dt_action_chill(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9896ff6d951SJohn Birrell dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
9906ff6d951SJohn Birrell {
9916ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
9926ff6d951SJohn Birrell 
9936ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
9946ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
9956ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_CHILL;
9966ff6d951SJohn Birrell }
9976ff6d951SJohn Birrell 
9986ff6d951SJohn Birrell static void
dt_action_raise(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)9996ff6d951SJohn Birrell dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10006ff6d951SJohn Birrell {
10016ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10026ff6d951SJohn Birrell 
10036ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
10046ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
10056ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_RAISE;
10066ff6d951SJohn Birrell }
10076ff6d951SJohn Birrell 
10086ff6d951SJohn Birrell static void
dt_action_exit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10096ff6d951SJohn Birrell dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10106ff6d951SJohn Birrell {
10116ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10126ff6d951SJohn Birrell 
10136ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
10146ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
10156ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_EXIT;
10166ff6d951SJohn Birrell 	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
10176ff6d951SJohn Birrell }
10186ff6d951SJohn Birrell 
10196ff6d951SJohn Birrell static void
dt_action_speculate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10206ff6d951SJohn Birrell dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10216ff6d951SJohn Birrell {
10226ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10236ff6d951SJohn Birrell 
10246ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
10256ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
10266ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_SPECULATE;
10276ff6d951SJohn Birrell }
10286ff6d951SJohn Birrell 
10296ff6d951SJohn Birrell static void
dt_action_printm(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)103018737969SJohn Birrell dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
103118737969SJohn Birrell {
103218737969SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
103318737969SJohn Birrell 
103418737969SJohn Birrell 	dt_node_t *size = dnp->dn_args;
103518737969SJohn Birrell 	dt_node_t *addr = dnp->dn_args->dn_list;
103618737969SJohn Birrell 
103718737969SJohn Birrell 	char n[DT_TYPE_NAMELEN];
103818737969SJohn Birrell 
103918737969SJohn Birrell 	if (dt_node_is_posconst(size) == 0) {
104018737969SJohn Birrell 		dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
104118737969SJohn Birrell 		    "be a non-zero positive integral constant expression\n");
104218737969SJohn Birrell 	}
104318737969SJohn Birrell 
104418737969SJohn Birrell 	if (dt_node_is_pointer(addr) == 0) {
104518737969SJohn Birrell 		dnerror(addr, D_PRINTM_ADDR,
104618737969SJohn Birrell 		    "printm( ) argument #2 is incompatible with "
104718737969SJohn Birrell 		    "prototype:\n\tprototype: pointer\n"
104818737969SJohn Birrell 		    "\t argument: %s\n",
104918737969SJohn Birrell 		    dt_node_type_name(addr, n, sizeof (n)));
105018737969SJohn Birrell 	}
105118737969SJohn Birrell 
105218737969SJohn Birrell 	dt_cg(yypcb, addr);
105318737969SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
105418737969SJohn Birrell 	ap->dtad_kind = DTRACEACT_PRINTM;
105518737969SJohn Birrell 
105618737969SJohn Birrell 	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
105718737969SJohn Birrell 	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
105818737969SJohn Birrell }
105918737969SJohn Birrell 
106018737969SJohn Birrell static void
dt_action_commit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10616ff6d951SJohn Birrell dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10626ff6d951SJohn Birrell {
10636ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10646ff6d951SJohn Birrell 
10656ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
10666ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
10676ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_COMMIT;
10686ff6d951SJohn Birrell }
10696ff6d951SJohn Birrell 
10706ff6d951SJohn Birrell static void
dt_action_discard(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10716ff6d951SJohn Birrell dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10726ff6d951SJohn Birrell {
10736ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
10746ff6d951SJohn Birrell 
10756ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_args);
10766ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
10776ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_DISCARD;
10786ff6d951SJohn Birrell }
10796ff6d951SJohn Birrell 
10806ff6d951SJohn Birrell static void
dt_compile_fun(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)10816ff6d951SJohn Birrell dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
10826ff6d951SJohn Birrell {
10836ff6d951SJohn Birrell 	switch (dnp->dn_expr->dn_ident->di_id) {
10846ff6d951SJohn Birrell 	case DT_ACT_BREAKPOINT:
10856ff6d951SJohn Birrell 		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
10866ff6d951SJohn Birrell 		break;
10876ff6d951SJohn Birrell 	case DT_ACT_CHILL:
10886ff6d951SJohn Birrell 		dt_action_chill(dtp, dnp->dn_expr, sdp);
10896ff6d951SJohn Birrell 		break;
10906ff6d951SJohn Birrell 	case DT_ACT_CLEAR:
10916ff6d951SJohn Birrell 		dt_action_clear(dtp, dnp->dn_expr, sdp);
10926ff6d951SJohn Birrell 		break;
10936ff6d951SJohn Birrell 	case DT_ACT_COMMIT:
10946ff6d951SJohn Birrell 		dt_action_commit(dtp, dnp->dn_expr, sdp);
10956ff6d951SJohn Birrell 		break;
10966ff6d951SJohn Birrell 	case DT_ACT_DENORMALIZE:
10976ff6d951SJohn Birrell 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
10986ff6d951SJohn Birrell 		break;
10996ff6d951SJohn Birrell 	case DT_ACT_DISCARD:
11006ff6d951SJohn Birrell 		dt_action_discard(dtp, dnp->dn_expr, sdp);
11016ff6d951SJohn Birrell 		break;
11026ff6d951SJohn Birrell 	case DT_ACT_EXIT:
11036ff6d951SJohn Birrell 		dt_action_exit(dtp, dnp->dn_expr, sdp);
11046ff6d951SJohn Birrell 		break;
11056ff6d951SJohn Birrell 	case DT_ACT_FREOPEN:
11066ff6d951SJohn Birrell 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
11076ff6d951SJohn Birrell 		break;
11086ff6d951SJohn Birrell 	case DT_ACT_FTRUNCATE:
11096ff6d951SJohn Birrell 		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
11106ff6d951SJohn Birrell 		break;
11116ff6d951SJohn Birrell 	case DT_ACT_MOD:
11126ff6d951SJohn Birrell 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
11136ff6d951SJohn Birrell 		break;
11146ff6d951SJohn Birrell 	case DT_ACT_NORMALIZE:
11156ff6d951SJohn Birrell 		dt_action_normalize(dtp, dnp->dn_expr, sdp);
11166ff6d951SJohn Birrell 		break;
11176ff6d951SJohn Birrell 	case DT_ACT_PANIC:
11186ff6d951SJohn Birrell 		dt_action_panic(dtp, dnp->dn_expr, sdp);
11196ff6d951SJohn Birrell 		break;
1120a98ff317SPedro F. Giffuni 	case DT_ACT_PRINT:
1121a98ff317SPedro F. Giffuni 		dt_action_trace(dtp, dnp->dn_expr, sdp);
1122a98ff317SPedro F. Giffuni 		break;
11236ff6d951SJohn Birrell 	case DT_ACT_PRINTA:
11246ff6d951SJohn Birrell 		dt_action_printa(dtp, dnp->dn_expr, sdp);
11256ff6d951SJohn Birrell 		break;
11266ff6d951SJohn Birrell 	case DT_ACT_PRINTF:
11276ff6d951SJohn Birrell 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
11286ff6d951SJohn Birrell 		break;
112918737969SJohn Birrell 	case DT_ACT_PRINTM:
113018737969SJohn Birrell 		dt_action_printm(dtp, dnp->dn_expr, sdp);
113118737969SJohn Birrell 		break;
11326ff6d951SJohn Birrell 	case DT_ACT_RAISE:
11336ff6d951SJohn Birrell 		dt_action_raise(dtp, dnp->dn_expr, sdp);
11346ff6d951SJohn Birrell 		break;
11356ff6d951SJohn Birrell 	case DT_ACT_SETOPT:
11366ff6d951SJohn Birrell 		dt_action_setopt(dtp, dnp->dn_expr, sdp);
11376ff6d951SJohn Birrell 		break;
11386ff6d951SJohn Birrell 	case DT_ACT_SPECULATE:
11396ff6d951SJohn Birrell 		dt_action_speculate(dtp, dnp->dn_expr, sdp);
11406ff6d951SJohn Birrell 		break;
11416ff6d951SJohn Birrell 	case DT_ACT_STACK:
11426ff6d951SJohn Birrell 		dt_action_stack(dtp, dnp->dn_expr, sdp);
11436ff6d951SJohn Birrell 		break;
11446ff6d951SJohn Birrell 	case DT_ACT_STOP:
11456ff6d951SJohn Birrell 		dt_action_stop(dtp, dnp->dn_expr, sdp);
11466ff6d951SJohn Birrell 		break;
11476ff6d951SJohn Birrell 	case DT_ACT_SYM:
11486ff6d951SJohn Birrell 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
11496ff6d951SJohn Birrell 		break;
11506ff6d951SJohn Birrell 	case DT_ACT_SYSTEM:
11516ff6d951SJohn Birrell 		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
11526ff6d951SJohn Birrell 		break;
11536ff6d951SJohn Birrell 	case DT_ACT_TRACE:
11546ff6d951SJohn Birrell 		dt_action_trace(dtp, dnp->dn_expr, sdp);
11556ff6d951SJohn Birrell 		break;
11566ff6d951SJohn Birrell 	case DT_ACT_TRACEMEM:
11576ff6d951SJohn Birrell 		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
11586ff6d951SJohn Birrell 		break;
11596ff6d951SJohn Birrell 	case DT_ACT_TRUNC:
11606ff6d951SJohn Birrell 		dt_action_trunc(dtp, dnp->dn_expr, sdp);
11616ff6d951SJohn Birrell 		break;
11626ff6d951SJohn Birrell 	case DT_ACT_UADDR:
11636ff6d951SJohn Birrell 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
11646ff6d951SJohn Birrell 		break;
11656ff6d951SJohn Birrell 	case DT_ACT_UMOD:
11666ff6d951SJohn Birrell 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
11676ff6d951SJohn Birrell 		break;
11686ff6d951SJohn Birrell 	case DT_ACT_USYM:
11696ff6d951SJohn Birrell 		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
11706ff6d951SJohn Birrell 		break;
11716ff6d951SJohn Birrell 	case DT_ACT_USTACK:
11726ff6d951SJohn Birrell 	case DT_ACT_JSTACK:
11736ff6d951SJohn Birrell 		dt_action_ustack(dtp, dnp->dn_expr, sdp);
11746ff6d951SJohn Birrell 		break;
11756ff6d951SJohn Birrell 	default:
11766ff6d951SJohn Birrell 		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
11776ff6d951SJohn Birrell 		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
11786ff6d951SJohn Birrell 	}
11796ff6d951SJohn Birrell }
11806ff6d951SJohn Birrell 
11816ff6d951SJohn Birrell static void
dt_compile_exp(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)11826ff6d951SJohn Birrell dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11836ff6d951SJohn Birrell {
11846ff6d951SJohn Birrell 	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
11856ff6d951SJohn Birrell 
11866ff6d951SJohn Birrell 	dt_cg(yypcb, dnp->dn_expr);
11876ff6d951SJohn Birrell 	ap->dtad_difo = dt_as(yypcb);
11886ff6d951SJohn Birrell 	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
11896ff6d951SJohn Birrell 	ap->dtad_kind = DTRACEACT_DIFEXPR;
11906ff6d951SJohn Birrell }
11916ff6d951SJohn Birrell 
11926ff6d951SJohn Birrell static void
dt_compile_agg(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)11936ff6d951SJohn Birrell dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
11946ff6d951SJohn Birrell {
11956ff6d951SJohn Birrell 	dt_ident_t *aid, *fid;
11966ff6d951SJohn Birrell 	dt_node_t *anp, *incr = NULL;
11976ff6d951SJohn Birrell 	dtrace_actdesc_t *ap;
11986ff6d951SJohn Birrell 	uint_t n = 1, argmax;
11996ff6d951SJohn Birrell 	uint64_t arg = 0;
12006ff6d951SJohn Birrell 
12016ff6d951SJohn Birrell 	/*
12026ff6d951SJohn Birrell 	 * If the aggregation has no aggregating function applied to it, then
12036ff6d951SJohn Birrell 	 * this statement has no effect.  Flag this as a programming error.
12046ff6d951SJohn Birrell 	 */
12056ff6d951SJohn Birrell 	if (dnp->dn_aggfun == NULL) {
12066ff6d951SJohn Birrell 		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
12076ff6d951SJohn Birrell 		    dnp->dn_ident->di_name);
12086ff6d951SJohn Birrell 	}
12096ff6d951SJohn Birrell 
12106ff6d951SJohn Birrell 	aid = dnp->dn_ident;
12116ff6d951SJohn Birrell 	fid = dnp->dn_aggfun->dn_ident;
12126ff6d951SJohn Birrell 
12136ff6d951SJohn Birrell 	if (dnp->dn_aggfun->dn_args != NULL &&
12146ff6d951SJohn Birrell 	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
12156ff6d951SJohn Birrell 		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
12166ff6d951SJohn Birrell 		    "be of scalar type\n", fid->di_name);
12176ff6d951SJohn Birrell 	}
12186ff6d951SJohn Birrell 
12196ff6d951SJohn Birrell 	/*
12206ff6d951SJohn Birrell 	 * The ID of the aggregation itself is implicitly recorded as the first
12216ff6d951SJohn Birrell 	 * member of each aggregation tuple so we can distinguish them later.
12226ff6d951SJohn Birrell 	 */
12236ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
12246ff6d951SJohn Birrell 	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
12256ff6d951SJohn Birrell 
12266ff6d951SJohn Birrell 	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
12276ff6d951SJohn Birrell 		ap = dt_stmt_action(dtp, sdp);
12286ff6d951SJohn Birrell 		n++;
12296ff6d951SJohn Birrell 
12306ff6d951SJohn Birrell 		if (anp->dn_kind == DT_NODE_FUNC) {
12316ff6d951SJohn Birrell 			if (anp->dn_ident->di_id == DT_ACT_STACK) {
12326ff6d951SJohn Birrell 				dt_action_stack_args(dtp, ap, anp->dn_args);
12336ff6d951SJohn Birrell 				continue;
12346ff6d951SJohn Birrell 			}
12356ff6d951SJohn Birrell 
12366ff6d951SJohn Birrell 			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
12376ff6d951SJohn Birrell 			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
12386ff6d951SJohn Birrell 				dt_action_ustack_args(dtp, ap, anp);
12396ff6d951SJohn Birrell 				continue;
12406ff6d951SJohn Birrell 			}
12416ff6d951SJohn Birrell 
12426ff6d951SJohn Birrell 			switch (anp->dn_ident->di_id) {
12436ff6d951SJohn Birrell 			case DT_ACT_UADDR:
12446ff6d951SJohn Birrell 				dt_action_symmod_args(dtp, ap,
12456ff6d951SJohn Birrell 				    anp->dn_args, DTRACEACT_UADDR);
12466ff6d951SJohn Birrell 				continue;
12476ff6d951SJohn Birrell 
12486ff6d951SJohn Birrell 			case DT_ACT_USYM:
12496ff6d951SJohn Birrell 				dt_action_symmod_args(dtp, ap,
12506ff6d951SJohn Birrell 				    anp->dn_args, DTRACEACT_USYM);
12516ff6d951SJohn Birrell 				continue;
12526ff6d951SJohn Birrell 
12536ff6d951SJohn Birrell 			case DT_ACT_UMOD:
12546ff6d951SJohn Birrell 				dt_action_symmod_args(dtp, ap,
12556ff6d951SJohn Birrell 				    anp->dn_args, DTRACEACT_UMOD);
12566ff6d951SJohn Birrell 				continue;
12576ff6d951SJohn Birrell 
12586ff6d951SJohn Birrell 			case DT_ACT_SYM:
12596ff6d951SJohn Birrell 				dt_action_symmod_args(dtp, ap,
12606ff6d951SJohn Birrell 				    anp->dn_args, DTRACEACT_SYM);
12616ff6d951SJohn Birrell 				continue;
12626ff6d951SJohn Birrell 
12636ff6d951SJohn Birrell 			case DT_ACT_MOD:
12646ff6d951SJohn Birrell 				dt_action_symmod_args(dtp, ap,
12656ff6d951SJohn Birrell 				    anp->dn_args, DTRACEACT_MOD);
12666ff6d951SJohn Birrell 				continue;
12676ff6d951SJohn Birrell 
12686ff6d951SJohn Birrell 			default:
12696ff6d951SJohn Birrell 				break;
12706ff6d951SJohn Birrell 			}
12716ff6d951SJohn Birrell 		}
12726ff6d951SJohn Birrell 
12736ff6d951SJohn Birrell 		dt_cg(yypcb, anp);
12746ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
12756ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_DIFEXPR;
12766ff6d951SJohn Birrell 	}
12776ff6d951SJohn Birrell 
12786ff6d951SJohn Birrell 	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
12796ff6d951SJohn Birrell 		/*
12806ff6d951SJohn Birrell 		 * For linear quantization, we have between two and four
12816ff6d951SJohn Birrell 		 * arguments in addition to the expression:
12826ff6d951SJohn Birrell 		 *
12836ff6d951SJohn Birrell 		 *    arg1 => Base value
12846ff6d951SJohn Birrell 		 *    arg2 => Limit value
12856ff6d951SJohn Birrell 		 *    arg3 => Quantization level step size (defaults to 1)
12866ff6d951SJohn Birrell 		 *    arg4 => Quantization increment value (defaults to 1)
12876ff6d951SJohn Birrell 		 */
12886ff6d951SJohn Birrell 		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
12896ff6d951SJohn Birrell 		dt_node_t *arg2 = arg1->dn_list;
12906ff6d951SJohn Birrell 		dt_node_t *arg3 = arg2->dn_list;
12916ff6d951SJohn Birrell 		dt_idsig_t *isp;
12926ff6d951SJohn Birrell 		uint64_t nlevels, step = 1, oarg;
12936ff6d951SJohn Birrell 		int64_t baseval, limitval;
12946ff6d951SJohn Birrell 
12956ff6d951SJohn Birrell 		if (arg1->dn_kind != DT_NODE_INT) {
12966ff6d951SJohn Birrell 			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
12976ff6d951SJohn Birrell 			    "argument #1 must be an integer constant\n");
12986ff6d951SJohn Birrell 		}
12996ff6d951SJohn Birrell 
13006ff6d951SJohn Birrell 		baseval = (int64_t)arg1->dn_value;
13016ff6d951SJohn Birrell 
13026ff6d951SJohn Birrell 		if (baseval < INT32_MIN || baseval > INT32_MAX) {
13036ff6d951SJohn Birrell 			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
13046ff6d951SJohn Birrell 			    "argument #1 must be a 32-bit quantity\n");
13056ff6d951SJohn Birrell 		}
13066ff6d951SJohn Birrell 
13076ff6d951SJohn Birrell 		if (arg2->dn_kind != DT_NODE_INT) {
13086ff6d951SJohn Birrell 			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
13096ff6d951SJohn Birrell 			    "argument #2 must be an integer constant\n");
13106ff6d951SJohn Birrell 		}
13116ff6d951SJohn Birrell 
13126ff6d951SJohn Birrell 		limitval = (int64_t)arg2->dn_value;
13136ff6d951SJohn Birrell 
13146ff6d951SJohn Birrell 		if (limitval < INT32_MIN || limitval > INT32_MAX) {
13156ff6d951SJohn Birrell 			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
13166ff6d951SJohn Birrell 			    "argument #2 must be a 32-bit quantity\n");
13176ff6d951SJohn Birrell 		}
13186ff6d951SJohn Birrell 
13196ff6d951SJohn Birrell 		if (limitval < baseval) {
13206ff6d951SJohn Birrell 			dnerror(dnp, D_LQUANT_MISMATCH,
13216ff6d951SJohn Birrell 			    "lquantize( ) base (argument #1) must be less "
13226ff6d951SJohn Birrell 			    "than limit (argument #2)\n");
13236ff6d951SJohn Birrell 		}
13246ff6d951SJohn Birrell 
13256ff6d951SJohn Birrell 		if (arg3 != NULL) {
13266ff6d951SJohn Birrell 			if (!dt_node_is_posconst(arg3)) {
13276ff6d951SJohn Birrell 				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
13286ff6d951SJohn Birrell 				    "argument #3 must be a non-zero positive "
13296ff6d951SJohn Birrell 				    "integer constant\n");
13306ff6d951SJohn Birrell 			}
13316ff6d951SJohn Birrell 
13326ff6d951SJohn Birrell 			if ((step = arg3->dn_value) > UINT16_MAX) {
13336ff6d951SJohn Birrell 				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
13346ff6d951SJohn Birrell 				    "argument #3 must be a 16-bit quantity\n");
13356ff6d951SJohn Birrell 			}
13366ff6d951SJohn Birrell 		}
13376ff6d951SJohn Birrell 
13386ff6d951SJohn Birrell 		nlevels = (limitval - baseval) / step;
13396ff6d951SJohn Birrell 
13406ff6d951SJohn Birrell 		if (nlevels == 0) {
13416ff6d951SJohn Birrell 			dnerror(dnp, D_LQUANT_STEPLARGE,
13426ff6d951SJohn Birrell 			    "lquantize( ) step (argument #3) too large: must "
13436ff6d951SJohn Birrell 			    "have at least one quantization level\n");
13446ff6d951SJohn Birrell 		}
13456ff6d951SJohn Birrell 
13466ff6d951SJohn Birrell 		if (nlevels > UINT16_MAX) {
13476ff6d951SJohn Birrell 			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
13486ff6d951SJohn Birrell 			    "(argument #3) too small: number of quantization "
13496ff6d951SJohn Birrell 			    "levels must be a 16-bit quantity\n");
13506ff6d951SJohn Birrell 		}
13516ff6d951SJohn Birrell 
13526ff6d951SJohn Birrell 		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
13536ff6d951SJohn Birrell 		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
13546ff6d951SJohn Birrell 		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
13556ff6d951SJohn Birrell 		    DTRACE_LQUANTIZE_BASEMASK);
13566ff6d951SJohn Birrell 
13576ff6d951SJohn Birrell 		assert(arg != 0);
13586ff6d951SJohn Birrell 
13596ff6d951SJohn Birrell 		isp = (dt_idsig_t *)aid->di_data;
13606ff6d951SJohn Birrell 
13616ff6d951SJohn Birrell 		if (isp->dis_auxinfo == 0) {
13626ff6d951SJohn Birrell 			/*
13636ff6d951SJohn Birrell 			 * This is the first time we've seen an lquantize()
13646ff6d951SJohn Birrell 			 * for this aggregation; we'll store our argument
13656ff6d951SJohn Birrell 			 * as the auxiliary signature information.
13666ff6d951SJohn Birrell 			 */
13676ff6d951SJohn Birrell 			isp->dis_auxinfo = arg;
13686ff6d951SJohn Birrell 		} else if ((oarg = isp->dis_auxinfo) != arg) {
13696ff6d951SJohn Birrell 			/*
13706ff6d951SJohn Birrell 			 * If we have seen this lquantize() before and the
13716ff6d951SJohn Birrell 			 * argument doesn't match the original argument, pick
13726ff6d951SJohn Birrell 			 * the original argument apart to concisely report the
13736ff6d951SJohn Birrell 			 * mismatch.
13746ff6d951SJohn Birrell 			 */
13756ff6d951SJohn Birrell 			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
13766ff6d951SJohn Birrell 			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
13776ff6d951SJohn Birrell 			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
13786ff6d951SJohn Birrell 
13796ff6d951SJohn Birrell 			if (obaseval != baseval) {
13806ff6d951SJohn Birrell 				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
13816ff6d951SJohn Birrell 				    "base (argument #1) doesn't match previous "
13826ff6d951SJohn Birrell 				    "declaration: expected %d, found %d\n",
13836ff6d951SJohn Birrell 				    obaseval, (int)baseval);
13846ff6d951SJohn Birrell 			}
13856ff6d951SJohn Birrell 
13866ff6d951SJohn Birrell 			if (onlevels * ostep != nlevels * step) {
13876ff6d951SJohn Birrell 				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
13886ff6d951SJohn Birrell 				    "limit (argument #2) doesn't match previous"
13896ff6d951SJohn Birrell 				    " declaration: expected %d, found %d\n",
13906ff6d951SJohn Birrell 				    obaseval + onlevels * ostep,
13916ff6d951SJohn Birrell 				    (int)baseval + (int)nlevels * (int)step);
13926ff6d951SJohn Birrell 			}
13936ff6d951SJohn Birrell 
13946ff6d951SJohn Birrell 			if (ostep != step) {
13956ff6d951SJohn Birrell 				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
13966ff6d951SJohn Birrell 				    "step (argument #3) doesn't match previous "
13976ff6d951SJohn Birrell 				    "declaration: expected %d, found %d\n",
13986ff6d951SJohn Birrell 				    ostep, (int)step);
13996ff6d951SJohn Birrell 			}
14006ff6d951SJohn Birrell 
14016ff6d951SJohn Birrell 			/*
14026ff6d951SJohn Birrell 			 * We shouldn't be able to get here -- one of the
14036ff6d951SJohn Birrell 			 * parameters must be mismatched if the arguments
14046ff6d951SJohn Birrell 			 * didn't match.
14056ff6d951SJohn Birrell 			 */
14066ff6d951SJohn Birrell 			assert(0);
14076ff6d951SJohn Birrell 		}
14086ff6d951SJohn Birrell 
14096ff6d951SJohn Birrell 		incr = arg3 != NULL ? arg3->dn_list : NULL;
14106ff6d951SJohn Birrell 		argmax = 5;
14116ff6d951SJohn Birrell 	}
14126ff6d951SJohn Birrell 
1413675cf915SPedro F. Giffuni 	if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1414675cf915SPedro F. Giffuni 		/*
1415675cf915SPedro F. Giffuni 		 * For log/linear quantizations, we have between one and five
1416675cf915SPedro F. Giffuni 		 * arguments in addition to the expression:
1417675cf915SPedro F. Giffuni 		 *
1418675cf915SPedro F. Giffuni 		 *    arg1 => Factor
1419675cf915SPedro F. Giffuni 		 *    arg2 => Low magnitude
1420675cf915SPedro F. Giffuni 		 *    arg3 => High magnitude
1421675cf915SPedro F. Giffuni 		 *    arg4 => Number of steps per magnitude
1422675cf915SPedro F. Giffuni 		 *    arg5 => Quantization increment value (defaults to 1)
1423675cf915SPedro F. Giffuni 		 */
1424675cf915SPedro F. Giffuni 		dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1425675cf915SPedro F. Giffuni 		uint64_t oarg, order, v;
1426675cf915SPedro F. Giffuni 		dt_idsig_t *isp;
1427675cf915SPedro F. Giffuni 		int i;
1428675cf915SPedro F. Giffuni 
1429675cf915SPedro F. Giffuni 		struct {
1430675cf915SPedro F. Giffuni 			char *str;		/* string identifier */
1431675cf915SPedro F. Giffuni 			int badtype;		/* error on bad type */
1432675cf915SPedro F. Giffuni 			int badval;		/* error on bad value */
1433675cf915SPedro F. Giffuni 			int mismatch;		/* error on bad match */
1434675cf915SPedro F. Giffuni 			int shift;		/* shift value */
1435675cf915SPedro F. Giffuni 			uint16_t value;		/* value itself */
1436675cf915SPedro F. Giffuni 		} args[] = {
1437675cf915SPedro F. Giffuni 			{ "factor", D_LLQUANT_FACTORTYPE,
1438675cf915SPedro F. Giffuni 			    D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1439675cf915SPedro F. Giffuni 			    DTRACE_LLQUANTIZE_FACTORSHIFT },
1440675cf915SPedro F. Giffuni 			{ "low magnitude", D_LLQUANT_LOWTYPE,
1441675cf915SPedro F. Giffuni 			    D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1442675cf915SPedro F. Giffuni 			    DTRACE_LLQUANTIZE_LOWSHIFT },
1443675cf915SPedro F. Giffuni 			{ "high magnitude", D_LLQUANT_HIGHTYPE,
1444675cf915SPedro F. Giffuni 			    D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1445675cf915SPedro F. Giffuni 			    DTRACE_LLQUANTIZE_HIGHSHIFT },
1446675cf915SPedro F. Giffuni 			{ "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1447675cf915SPedro F. Giffuni 			    D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1448675cf915SPedro F. Giffuni 			    DTRACE_LLQUANTIZE_NSTEPSHIFT },
1449675cf915SPedro F. Giffuni 			{ NULL }
1450675cf915SPedro F. Giffuni 		};
1451675cf915SPedro F. Giffuni 
1452675cf915SPedro F. Giffuni 		assert(arg == 0);
1453675cf915SPedro F. Giffuni 
1454675cf915SPedro F. Giffuni 		for (i = 0; args[i].str != NULL; i++) {
1455675cf915SPedro F. Giffuni 			if (llarg->dn_kind != DT_NODE_INT) {
1456675cf915SPedro F. Giffuni 				dnerror(llarg, args[i].badtype, "llquantize( ) "
1457675cf915SPedro F. Giffuni 				    "argument #%d (%s) must be an "
1458675cf915SPedro F. Giffuni 				    "integer constant\n", i + 1, args[i].str);
1459675cf915SPedro F. Giffuni 			}
1460675cf915SPedro F. Giffuni 
1461675cf915SPedro F. Giffuni 			if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1462675cf915SPedro F. Giffuni 				dnerror(llarg, args[i].badval, "llquantize( ) "
1463675cf915SPedro F. Giffuni 				    "argument #%d (%s) must be an unsigned "
1464675cf915SPedro F. Giffuni 				    "16-bit quantity\n", i + 1, args[i].str);
1465675cf915SPedro F. Giffuni 			}
1466675cf915SPedro F. Giffuni 
1467675cf915SPedro F. Giffuni 			args[i].value = (uint16_t)llarg->dn_value;
1468675cf915SPedro F. Giffuni 
1469675cf915SPedro F. Giffuni 			assert(!(arg & ((uint64_t)UINT16_MAX <<
1470675cf915SPedro F. Giffuni 			    args[i].shift)));
1471675cf915SPedro F. Giffuni 			arg |= ((uint64_t)args[i].value << args[i].shift);
1472675cf915SPedro F. Giffuni 			llarg = llarg->dn_list;
1473675cf915SPedro F. Giffuni 		}
1474675cf915SPedro F. Giffuni 
1475675cf915SPedro F. Giffuni 		assert(arg != 0);
1476675cf915SPedro F. Giffuni 
1477675cf915SPedro F. Giffuni 		if (args[0].value < 2) {
1478675cf915SPedro F. Giffuni 			dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1479675cf915SPedro F. Giffuni 			    "factor (argument #1) must be two or more\n");
1480675cf915SPedro F. Giffuni 		}
1481675cf915SPedro F. Giffuni 
1482675cf915SPedro F. Giffuni 		if (args[1].value >= args[2].value) {
1483675cf915SPedro F. Giffuni 			dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1484675cf915SPedro F. Giffuni 			    "high magnitude (argument #3) must be greater "
1485675cf915SPedro F. Giffuni 			    "than low magnitude (argument #2)\n");
1486675cf915SPedro F. Giffuni 		}
1487675cf915SPedro F. Giffuni 
1488675cf915SPedro F. Giffuni 		if (args[3].value < args[0].value) {
1489675cf915SPedro F. Giffuni 			dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1490675cf915SPedro F. Giffuni 			    "factor (argument #1) must be less than or "
1491675cf915SPedro F. Giffuni 			    "equal to the number of linear steps per "
1492675cf915SPedro F. Giffuni 			    "magnitude (argument #4)\n");
1493675cf915SPedro F. Giffuni 		}
1494675cf915SPedro F. Giffuni 
1495675cf915SPedro F. Giffuni 		for (v = args[0].value; v < args[3].value; v *= args[0].value)
1496675cf915SPedro F. Giffuni 			continue;
1497675cf915SPedro F. Giffuni 
1498675cf915SPedro F. Giffuni 		if ((args[3].value % args[0].value) || (v % args[3].value)) {
1499675cf915SPedro F. Giffuni 			dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1500675cf915SPedro F. Giffuni 			    "factor (argument #1) must evenly divide the "
1501675cf915SPedro F. Giffuni 			    "number of steps per magnitude (argument #4), "
1502675cf915SPedro F. Giffuni 			    "and the number of steps per magnitude must evenly "
1503675cf915SPedro F. Giffuni 			    "divide a power of the factor\n");
1504675cf915SPedro F. Giffuni 		}
1505675cf915SPedro F. Giffuni 
1506ce4da6fcSMark Johnston 		for (i = 0, order = 1; i <= args[2].value + 1; i++) {
1507675cf915SPedro F. Giffuni 			if (order * args[0].value > order) {
1508675cf915SPedro F. Giffuni 				order *= args[0].value;
1509675cf915SPedro F. Giffuni 				continue;
1510675cf915SPedro F. Giffuni 			}
1511675cf915SPedro F. Giffuni 
1512675cf915SPedro F. Giffuni 			dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1513675cf915SPedro F. Giffuni 			    "factor (%d) raised to power of high magnitude "
1514ce4da6fcSMark Johnston 			    "(%d) plus 1 overflows 64-bits\n", args[0].value,
1515675cf915SPedro F. Giffuni 			    args[2].value);
1516675cf915SPedro F. Giffuni 		}
1517675cf915SPedro F. Giffuni 
1518675cf915SPedro F. Giffuni 		isp = (dt_idsig_t *)aid->di_data;
1519675cf915SPedro F. Giffuni 
1520675cf915SPedro F. Giffuni 		if (isp->dis_auxinfo == 0) {
1521675cf915SPedro F. Giffuni 			/*
1522675cf915SPedro F. Giffuni 			 * This is the first time we've seen an llquantize()
1523675cf915SPedro F. Giffuni 			 * for this aggregation; we'll store our argument
1524675cf915SPedro F. Giffuni 			 * as the auxiliary signature information.
1525675cf915SPedro F. Giffuni 			 */
1526675cf915SPedro F. Giffuni 			isp->dis_auxinfo = arg;
1527675cf915SPedro F. Giffuni 		} else if ((oarg = isp->dis_auxinfo) != arg) {
1528675cf915SPedro F. Giffuni 			/*
1529675cf915SPedro F. Giffuni 			 * If we have seen this llquantize() before and the
1530675cf915SPedro F. Giffuni 			 * argument doesn't match the original argument, pick
1531675cf915SPedro F. Giffuni 			 * the original argument apart to concisely report the
1532675cf915SPedro F. Giffuni 			 * mismatch.
1533675cf915SPedro F. Giffuni 			 */
1534675cf915SPedro F. Giffuni 			int expected = 0, found = 0;
1535675cf915SPedro F. Giffuni 
1536675cf915SPedro F. Giffuni 			for (i = 0; expected == found; i++) {
1537675cf915SPedro F. Giffuni 				assert(args[i].str != NULL);
1538675cf915SPedro F. Giffuni 
1539675cf915SPedro F. Giffuni 				expected = (oarg >> args[i].shift) & UINT16_MAX;
1540675cf915SPedro F. Giffuni 				found = (arg >> args[i].shift) & UINT16_MAX;
1541675cf915SPedro F. Giffuni 			}
1542675cf915SPedro F. Giffuni 
1543675cf915SPedro F. Giffuni 			dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1544675cf915SPedro F. Giffuni 			    "%s (argument #%d) doesn't match previous "
1545675cf915SPedro F. Giffuni 			    "declaration: expected %d, found %d\n",
1546675cf915SPedro F. Giffuni 			    args[i - 1].str, i, expected, found);
1547675cf915SPedro F. Giffuni 		}
1548675cf915SPedro F. Giffuni 
1549675cf915SPedro F. Giffuni 		incr = llarg;
1550675cf915SPedro F. Giffuni 		argmax = 6;
1551675cf915SPedro F. Giffuni 	}
1552675cf915SPedro F. Giffuni 
15536ff6d951SJohn Birrell 	if (fid->di_id == DTRACEAGG_QUANTIZE) {
15546ff6d951SJohn Birrell 		incr = dnp->dn_aggfun->dn_args->dn_list;
15556ff6d951SJohn Birrell 		argmax = 2;
15566ff6d951SJohn Birrell 	}
15576ff6d951SJohn Birrell 
15586ff6d951SJohn Birrell 	if (incr != NULL) {
15596ff6d951SJohn Birrell 		if (!dt_node_is_scalar(incr)) {
15606ff6d951SJohn Birrell 			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
15616ff6d951SJohn Birrell 			    "(argument #%d) must be of scalar type\n",
15626ff6d951SJohn Birrell 			    fid->di_name, argmax);
15636ff6d951SJohn Birrell 		}
15646ff6d951SJohn Birrell 
15656ff6d951SJohn Birrell 		if ((anp = incr->dn_list) != NULL) {
15666ff6d951SJohn Birrell 			int argc = argmax;
15676ff6d951SJohn Birrell 
15686ff6d951SJohn Birrell 			for (; anp != NULL; anp = anp->dn_list)
15696ff6d951SJohn Birrell 				argc++;
15706ff6d951SJohn Birrell 
15716ff6d951SJohn Birrell 			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
15726ff6d951SJohn Birrell 			    "mismatch: %d args passed, at most %d expected",
15736ff6d951SJohn Birrell 			    fid->di_name, argc, argmax);
15746ff6d951SJohn Birrell 		}
15756ff6d951SJohn Birrell 
15766ff6d951SJohn Birrell 		ap = dt_stmt_action(dtp, sdp);
15776ff6d951SJohn Birrell 		n++;
15786ff6d951SJohn Birrell 
15796ff6d951SJohn Birrell 		dt_cg(yypcb, incr);
15806ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
15816ff6d951SJohn Birrell 		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
15826ff6d951SJohn Birrell 		ap->dtad_kind = DTRACEACT_DIFEXPR;
15836ff6d951SJohn Birrell 	}
15846ff6d951SJohn Birrell 
15856ff6d951SJohn Birrell 	assert(sdp->dtsd_aggdata == NULL);
15866ff6d951SJohn Birrell 	sdp->dtsd_aggdata = aid;
15876ff6d951SJohn Birrell 
15886ff6d951SJohn Birrell 	ap = dt_stmt_action(dtp, sdp);
15896ff6d951SJohn Birrell 	assert(fid->di_kind == DT_IDENT_AGGFUNC);
15906ff6d951SJohn Birrell 	assert(DTRACEACT_ISAGG(fid->di_id));
15916ff6d951SJohn Birrell 	ap->dtad_kind = fid->di_id;
15926ff6d951SJohn Birrell 	ap->dtad_ntuple = n;
15936ff6d951SJohn Birrell 	ap->dtad_arg = arg;
15946ff6d951SJohn Birrell 
15956ff6d951SJohn Birrell 	if (dnp->dn_aggfun->dn_args != NULL) {
15966ff6d951SJohn Birrell 		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
15976ff6d951SJohn Birrell 		ap->dtad_difo = dt_as(yypcb);
15986ff6d951SJohn Birrell 	}
15996ff6d951SJohn Birrell }
16006ff6d951SJohn Birrell 
16016ff6d951SJohn Birrell static void
dt_compile_one_clause(dtrace_hdl_t * dtp,dt_node_t * cnp,dt_node_t * pnp)16026ff6d951SJohn Birrell dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
16036ff6d951SJohn Birrell {
16046ff6d951SJohn Birrell 	dtrace_ecbdesc_t *edp;
16056ff6d951SJohn Birrell 	dtrace_stmtdesc_t *sdp;
16066ff6d951SJohn Birrell 	dt_node_t *dnp;
16076ff6d951SJohn Birrell 
16086ff6d951SJohn Birrell 	yylineno = pnp->dn_line;
16096ff6d951SJohn Birrell 	dt_setcontext(dtp, pnp->dn_desc);
16106ff6d951SJohn Birrell 	(void) dt_node_cook(cnp, DT_IDFLG_REF);
16116ff6d951SJohn Birrell 
16126ff6d951SJohn Birrell 	if (DT_TREEDUMP_PASS(dtp, 2))
16136ff6d951SJohn Birrell 		dt_node_printr(cnp, stderr, 0);
16146ff6d951SJohn Birrell 
16156ff6d951SJohn Birrell 	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
16166ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
16176ff6d951SJohn Birrell 
16186ff6d951SJohn Birrell 	assert(yypcb->pcb_ecbdesc == NULL);
16196ff6d951SJohn Birrell 	yypcb->pcb_ecbdesc = edp;
16206ff6d951SJohn Birrell 
16216ff6d951SJohn Birrell 	if (cnp->dn_pred != NULL) {
16226ff6d951SJohn Birrell 		dt_cg(yypcb, cnp->dn_pred);
16236ff6d951SJohn Birrell 		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
16246ff6d951SJohn Birrell 	}
16256ff6d951SJohn Birrell 
16266ff6d951SJohn Birrell 	if (cnp->dn_acts == NULL) {
16276ff6d951SJohn Birrell 		dt_stmt_append(dt_stmt_create(dtp, edp,
16286ff6d951SJohn Birrell 		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
16296ff6d951SJohn Birrell 	}
16306ff6d951SJohn Birrell 
16316ff6d951SJohn Birrell 	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
16326ff6d951SJohn Birrell 		assert(yypcb->pcb_stmt == NULL);
16336ff6d951SJohn Birrell 		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
16346ff6d951SJohn Birrell 
16356ff6d951SJohn Birrell 		switch (dnp->dn_kind) {
16366ff6d951SJohn Birrell 		case DT_NODE_DEXPR:
16376ff6d951SJohn Birrell 			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
16386ff6d951SJohn Birrell 				dt_compile_agg(dtp, dnp->dn_expr, sdp);
16396ff6d951SJohn Birrell 			else
16406ff6d951SJohn Birrell 				dt_compile_exp(dtp, dnp, sdp);
16416ff6d951SJohn Birrell 			break;
16426ff6d951SJohn Birrell 		case DT_NODE_DFUNC:
16436ff6d951SJohn Birrell 			dt_compile_fun(dtp, dnp, sdp);
16446ff6d951SJohn Birrell 			break;
16456ff6d951SJohn Birrell 		case DT_NODE_AGG:
16466ff6d951SJohn Birrell 			dt_compile_agg(dtp, dnp, sdp);
16476ff6d951SJohn Birrell 			break;
16486ff6d951SJohn Birrell 		default:
16496ff6d951SJohn Birrell 			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
16506ff6d951SJohn Birrell 			    "%u is not a valid statement\n", dnp->dn_kind);
16516ff6d951SJohn Birrell 		}
16526ff6d951SJohn Birrell 
16536ff6d951SJohn Birrell 		assert(yypcb->pcb_stmt == sdp);
16546ff6d951SJohn Birrell 		dt_stmt_append(sdp, dnp);
16556ff6d951SJohn Birrell 	}
16566ff6d951SJohn Birrell 
16576ff6d951SJohn Birrell 	assert(yypcb->pcb_ecbdesc == edp);
16586ff6d951SJohn Birrell 	dt_ecbdesc_release(dtp, edp);
16596ff6d951SJohn Birrell 	dt_endcontext(dtp);
16606ff6d951SJohn Birrell 	yypcb->pcb_ecbdesc = NULL;
16616ff6d951SJohn Birrell }
16626ff6d951SJohn Birrell 
16636ff6d951SJohn Birrell static void
dt_compile_clause(dtrace_hdl_t * dtp,dt_node_t * cnp)16646ff6d951SJohn Birrell dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
16656ff6d951SJohn Birrell {
16666ff6d951SJohn Birrell 	dt_node_t *pnp;
16676ff6d951SJohn Birrell 
16686ff6d951SJohn Birrell 	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
16696ff6d951SJohn Birrell 		dt_compile_one_clause(dtp, cnp, pnp);
16706ff6d951SJohn Birrell }
16716ff6d951SJohn Birrell 
16726ff6d951SJohn Birrell static void
dt_compile_xlator(dt_node_t * dnp)16736ff6d951SJohn Birrell dt_compile_xlator(dt_node_t *dnp)
16746ff6d951SJohn Birrell {
16756ff6d951SJohn Birrell 	dt_xlator_t *dxp = dnp->dn_xlator;
16766ff6d951SJohn Birrell 	dt_node_t *mnp;
16776ff6d951SJohn Birrell 
16786ff6d951SJohn Birrell 	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
16796ff6d951SJohn Birrell 		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
16806ff6d951SJohn Birrell 		dt_cg(yypcb, mnp);
16816ff6d951SJohn Birrell 		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
16826ff6d951SJohn Birrell 	}
16836ff6d951SJohn Birrell }
16846ff6d951SJohn Birrell 
16856ff6d951SJohn Birrell void
dt_setcontext(dtrace_hdl_t * dtp,dtrace_probedesc_t * pdp)16866ff6d951SJohn Birrell dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
16876ff6d951SJohn Birrell {
16886ff6d951SJohn Birrell 	const dtrace_pattr_t *pap;
16896ff6d951SJohn Birrell 	dt_probe_t *prp;
16906ff6d951SJohn Birrell 	dt_provider_t *pvp;
16916ff6d951SJohn Birrell 	dt_ident_t *idp;
16926ff6d951SJohn Birrell 	char attrstr[8];
16936ff6d951SJohn Birrell 	int err;
1694a877965fSDomagoj Stolfa 	size_t prov_len;
16956ff6d951SJohn Birrell 
16966ff6d951SJohn Birrell 	/*
16976ff6d951SJohn Birrell 	 * Both kernel and pid based providers are allowed to have names
16986ff6d951SJohn Birrell 	 * ending with what could be interpreted as a number. We assume it's
16996ff6d951SJohn Birrell 	 * a pid and that we may need to dynamically create probes for
17006ff6d951SJohn Birrell 	 * that process if:
17016ff6d951SJohn Birrell 	 *
17026ff6d951SJohn Birrell 	 * (1) The provider doesn't exist, or,
17036ff6d951SJohn Birrell 	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
17046ff6d951SJohn Birrell 	 *
17056ff6d951SJohn Birrell 	 * On an error, dt_pid_create_probes() will set the error message
17066ff6d951SJohn Birrell 	 * and tag -- we just have to longjmp() out of here.
17076ff6d951SJohn Birrell 	 */
1708a877965fSDomagoj Stolfa 
1709a877965fSDomagoj Stolfa 	prov_len = strlen(pdp->dtpd_provider);
1710a877965fSDomagoj Stolfa 
1711a877965fSDomagoj Stolfa 	if ((prov_len > 0 && isdigit(pdp->dtpd_provider[prov_len - 1])) &&
17126ff6d951SJohn Birrell 	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
17136ff6d951SJohn Birrell 	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
17146ff6d951SJohn Birrell 	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
17156ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
17166ff6d951SJohn Birrell 	}
17176ff6d951SJohn Birrell 
17186ff6d951SJohn Birrell 	/*
17196ff6d951SJohn Birrell 	 * Call dt_probe_info() to get the probe arguments and attributes.  If
17206ff6d951SJohn Birrell 	 * a representative probe is found, set 'pap' to the probe provider's
17216ff6d951SJohn Birrell 	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
17226ff6d951SJohn Birrell 	 */
17236ff6d951SJohn Birrell 	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
17246ff6d951SJohn Birrell 		pap = &_dtrace_prvdesc;
17256ff6d951SJohn Birrell 		err = dtrace_errno(dtp);
17266ff6d951SJohn Birrell 		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
17276ff6d951SJohn Birrell 		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
17286ff6d951SJohn Birrell 		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
17296ff6d951SJohn Birrell 	} else {
17306ff6d951SJohn Birrell 		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
17316ff6d951SJohn Birrell 		err = 0;
17326ff6d951SJohn Birrell 	}
17336ff6d951SJohn Birrell 
17346ff6d951SJohn Birrell 	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
17356ff6d951SJohn Birrell 		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
17366ff6d951SJohn Birrell 		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
17376ff6d951SJohn Birrell 		    pdp->dtpd_func, pdp->dtpd_name);
17386ff6d951SJohn Birrell 	}
17396ff6d951SJohn Birrell 
17406ff6d951SJohn Birrell 	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
17416ff6d951SJohn Birrell 		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
17426ff6d951SJohn Birrell 
17436ff6d951SJohn Birrell 	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
17446ff6d951SJohn Birrell 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
17456ff6d951SJohn Birrell 	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
17466ff6d951SJohn Birrell 	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
17476ff6d951SJohn Birrell 
17486ff6d951SJohn Birrell 	/*
17496ff6d951SJohn Birrell 	 * Reset the stability attributes of D global variables that vary
17506ff6d951SJohn Birrell 	 * based on the attributes of the provider and context itself.
17516ff6d951SJohn Birrell 	 */
17526ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
17536ff6d951SJohn Birrell 		idp->di_attr = pap->dtpa_provider;
17546ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
17556ff6d951SJohn Birrell 		idp->di_attr = pap->dtpa_mod;
17566ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
17576ff6d951SJohn Birrell 		idp->di_attr = pap->dtpa_func;
17586ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
17596ff6d951SJohn Birrell 		idp->di_attr = pap->dtpa_name;
17606ff6d951SJohn Birrell 	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
17616ff6d951SJohn Birrell 		idp->di_attr = pap->dtpa_args;
17626ff6d951SJohn Birrell 
17636ff6d951SJohn Birrell 	yypcb->pcb_pdesc = pdp;
17646ff6d951SJohn Birrell 	yypcb->pcb_probe = prp;
17656ff6d951SJohn Birrell }
17666ff6d951SJohn Birrell 
17676ff6d951SJohn Birrell /*
17686ff6d951SJohn Birrell  * Reset context-dependent variables and state at the end of cooking a D probe
17696ff6d951SJohn Birrell  * definition clause.  This ensures that external declarations between clauses
17706ff6d951SJohn Birrell  * do not reference any stale context-dependent data from the previous clause.
17716ff6d951SJohn Birrell  */
17726ff6d951SJohn Birrell void
dt_endcontext(dtrace_hdl_t * dtp)17736ff6d951SJohn Birrell dt_endcontext(dtrace_hdl_t *dtp)
17746ff6d951SJohn Birrell {
17756ff6d951SJohn Birrell 	static const char *const cvars[] = {
17766ff6d951SJohn Birrell 		"probeprov", "probemod", "probefunc", "probename", "args", NULL
17776ff6d951SJohn Birrell 	};
17786ff6d951SJohn Birrell 
17796ff6d951SJohn Birrell 	dt_ident_t *idp;
17806ff6d951SJohn Birrell 	int i;
17816ff6d951SJohn Birrell 
17826ff6d951SJohn Birrell 	for (i = 0; cvars[i] != NULL; i++) {
17836ff6d951SJohn Birrell 		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
17846ff6d951SJohn Birrell 			idp->di_attr = _dtrace_defattr;
17856ff6d951SJohn Birrell 	}
17866ff6d951SJohn Birrell 
17876ff6d951SJohn Birrell 	yypcb->pcb_pdesc = NULL;
17886ff6d951SJohn Birrell 	yypcb->pcb_probe = NULL;
17896ff6d951SJohn Birrell }
17906ff6d951SJohn Birrell 
17916ff6d951SJohn Birrell static int
dt_reduceid(dt_idhash_t * dhp,dt_ident_t * idp,dtrace_hdl_t * dtp)17926ff6d951SJohn Birrell dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
17936ff6d951SJohn Birrell {
17946ff6d951SJohn Birrell 	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
17956ff6d951SJohn Birrell 		dt_idhash_delete(dhp, idp);
17966ff6d951SJohn Birrell 
17976ff6d951SJohn Birrell 	return (0);
17986ff6d951SJohn Birrell }
17996ff6d951SJohn Birrell 
18006ff6d951SJohn Birrell /*
18016ff6d951SJohn Birrell  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
18026ff6d951SJohn Birrell  * any identifiers or translators that have been previously defined as bound to
18036ff6d951SJohn Birrell  * a version greater than the specified version.  Therefore, in our current
18046ff6d951SJohn Birrell  * version implementation, establishing a binding is a one-way transformation.
18056ff6d951SJohn Birrell  * In addition, no versioning is currently provided for types as our .d library
18066ff6d951SJohn Birrell  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
18076ff6d951SJohn Birrell  * for our exclusive use.  If required, type versioning will require more work.
18086ff6d951SJohn Birrell  */
18096ff6d951SJohn Birrell int
dt_reduce(dtrace_hdl_t * dtp,dt_version_t v)18106ff6d951SJohn Birrell dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
18116ff6d951SJohn Birrell {
18126ff6d951SJohn Birrell 	char s[DT_VERSION_STRMAX];
18136ff6d951SJohn Birrell 	dt_xlator_t *dxp, *nxp;
18146ff6d951SJohn Birrell 
18156ff6d951SJohn Birrell 	if (v > dtp->dt_vmax)
18166ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_VERSREDUCED));
18176ff6d951SJohn Birrell 	else if (v == dtp->dt_vmax)
18186ff6d951SJohn Birrell 		return (0); /* no reduction necessary */
18196ff6d951SJohn Birrell 
18206ff6d951SJohn Birrell 	dt_dprintf("reducing api version to %s\n",
18216ff6d951SJohn Birrell 	    dt_version_num2str(v, s, sizeof (s)));
18226ff6d951SJohn Birrell 
18236ff6d951SJohn Birrell 	dtp->dt_vmax = v;
18246ff6d951SJohn Birrell 
18256ff6d951SJohn Birrell 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
18266ff6d951SJohn Birrell 		nxp = dt_list_next(dxp);
18276ff6d951SJohn Birrell 		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
18286ff6d951SJohn Birrell 		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
18296ff6d951SJohn Birrell 			dt_list_delete(&dtp->dt_xlators, dxp);
18306ff6d951SJohn Birrell 	}
18316ff6d951SJohn Birrell 
18326ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
18336ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
18346ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
18356ff6d951SJohn Birrell 	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
18366ff6d951SJohn Birrell 
18376ff6d951SJohn Birrell 	return (0);
18386ff6d951SJohn Birrell }
18396ff6d951SJohn Birrell 
18406ff6d951SJohn Birrell /*
18416ff6d951SJohn Birrell  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
18426ff6d951SJohn Birrell  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
18436ff6d951SJohn Birrell  * here to simplify the code by leveraging file descriptor inheritance.
18446ff6d951SJohn Birrell  */
18456ff6d951SJohn Birrell static FILE *
dt_preproc(dtrace_hdl_t * dtp,FILE * ifp)18466ff6d951SJohn Birrell dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
18476ff6d951SJohn Birrell {
18486ff6d951SJohn Birrell 	int argc = dtp->dt_cpp_argc;
18496ff6d951SJohn Birrell 	char **argv = malloc(sizeof (char *) * (argc + 5));
18506ff6d951SJohn Birrell 	FILE *ofp = tmpfile();
18516ff6d951SJohn Birrell 
1852bc96366cSSteven Hartland #ifdef illumos
18536ff6d951SJohn Birrell 	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
185418737969SJohn Birrell #endif
18556ff6d951SJohn Birrell 	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
18566ff6d951SJohn Birrell 
18576ff6d951SJohn Birrell 	struct sigaction act, oact;
18586ff6d951SJohn Birrell 	sigset_t mask, omask;
18596ff6d951SJohn Birrell 
18606ff6d951SJohn Birrell 	int wstat, estat;
18616ff6d951SJohn Birrell 	pid_t pid;
1862bc96366cSSteven Hartland #ifdef illumos
18636ff6d951SJohn Birrell 	off64_t off;
186418737969SJohn Birrell #else
186518737969SJohn Birrell 	off_t off = 0;
186618737969SJohn Birrell #endif
18676ff6d951SJohn Birrell 	int c;
18686ff6d951SJohn Birrell 
18696ff6d951SJohn Birrell 	if (argv == NULL || ofp == NULL) {
18706ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, errno);
18716ff6d951SJohn Birrell 		goto err;
18726ff6d951SJohn Birrell 	}
18736ff6d951SJohn Birrell 
18746ff6d951SJohn Birrell 	/*
18756ff6d951SJohn Birrell 	 * If the input is a seekable file, see if it is an interpreter file.
18766ff6d951SJohn Birrell 	 * If we see #!, seek past the first line because cpp will choke on it.
18776ff6d951SJohn Birrell 	 * We start cpp just prior to the \n at the end of this line so that
18786ff6d951SJohn Birrell 	 * it still sees the newline, ensuring that #line values are correct.
18796ff6d951SJohn Birrell 	 */
18806ff6d951SJohn Birrell 	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
18816ff6d951SJohn Birrell 		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
18826ff6d951SJohn Birrell 			for (off += 2; c != '\n'; off++) {
18836ff6d951SJohn Birrell 				if ((c = fgetc(ifp)) == EOF)
18846ff6d951SJohn Birrell 					break;
18856ff6d951SJohn Birrell 			}
18866ff6d951SJohn Birrell 			if (c == '\n')
18876ff6d951SJohn Birrell 				off--; /* start cpp just prior to \n */
18886ff6d951SJohn Birrell 		}
18896ff6d951SJohn Birrell 		(void) fflush(ifp);
18906ff6d951SJohn Birrell 		(void) fseeko64(ifp, off, SEEK_SET);
18916ff6d951SJohn Birrell 	}
18926ff6d951SJohn Birrell 
1893bc96366cSSteven Hartland #ifdef illumos
18946ff6d951SJohn Birrell 	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
18956ff6d951SJohn Birrell 	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
189618737969SJohn Birrell #endif
18976ff6d951SJohn Birrell 
18986ff6d951SJohn Birrell 	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
18996ff6d951SJohn Birrell 
19006ff6d951SJohn Birrell 	(void) snprintf(verdef, sizeof (verdef),
19016ff6d951SJohn Birrell 	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
19026ff6d951SJohn Birrell 	argv[argc++] = verdef;
19036ff6d951SJohn Birrell 
1904bc96366cSSteven Hartland #ifdef illumos
19056ff6d951SJohn Birrell 	switch (dtp->dt_stdcmode) {
19066ff6d951SJohn Birrell 	case DT_STDC_XA:
19076ff6d951SJohn Birrell 	case DT_STDC_XT:
19086ff6d951SJohn Birrell 		argv[argc++] = "-D__STDC__=0";
19096ff6d951SJohn Birrell 		break;
19106ff6d951SJohn Birrell 	case DT_STDC_XC:
19116ff6d951SJohn Birrell 		argv[argc++] = "-D__STDC__=1";
19126ff6d951SJohn Birrell 		break;
19136ff6d951SJohn Birrell 	}
19146ff6d951SJohn Birrell 
19156ff6d951SJohn Birrell 	argv[argc++] = ipath;
19166ff6d951SJohn Birrell 	argv[argc++] = opath;
191718737969SJohn Birrell #else
191818737969SJohn Birrell 	argv[argc++] = "-P";
191918737969SJohn Birrell #endif
19206ff6d951SJohn Birrell 	argv[argc] = NULL;
19216ff6d951SJohn Birrell 
19226ff6d951SJohn Birrell 	/*
19236ff6d951SJohn Birrell 	 * libdtrace must be able to be embedded in other programs that may
19246ff6d951SJohn Birrell 	 * include application-specific signal handlers.  Therefore, if we
19256ff6d951SJohn Birrell 	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
19266ff6d951SJohn Birrell 	 * that could confuse the containing application.  To do this,
19276ff6d951SJohn Birrell 	 * we block SIGCHLD and reset its disposition to SIG_DFL.
19286ff6d951SJohn Birrell 	 * We restore our signal state once we are done.
19296ff6d951SJohn Birrell 	 */
19306ff6d951SJohn Birrell 	(void) sigemptyset(&mask);
19316ff6d951SJohn Birrell 	(void) sigaddset(&mask, SIGCHLD);
19326ff6d951SJohn Birrell 	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
19336ff6d951SJohn Birrell 
19346ff6d951SJohn Birrell 	bzero(&act, sizeof (act));
19356ff6d951SJohn Birrell 	act.sa_handler = SIG_DFL;
19366ff6d951SJohn Birrell 	(void) sigaction(SIGCHLD, &act, &oact);
19376ff6d951SJohn Birrell 
19386ff6d951SJohn Birrell 	if ((pid = fork1()) == -1) {
19396ff6d951SJohn Birrell 		(void) sigaction(SIGCHLD, &oact, NULL);
19406ff6d951SJohn Birrell 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
19416ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EDT_CPPFORK);
19426ff6d951SJohn Birrell 		goto err;
19436ff6d951SJohn Birrell 	}
19446ff6d951SJohn Birrell 
19456ff6d951SJohn Birrell 	if (pid == 0) {
1946bc96366cSSteven Hartland #ifndef illumos
194718737969SJohn Birrell 		if (isatty(fileno(ifp)) == 0)
194818737969SJohn Birrell 			lseek(fileno(ifp), off, SEEK_SET);
194918737969SJohn Birrell 		dup2(fileno(ifp), 0);
195018737969SJohn Birrell 		dup2(fileno(ofp), 1);
195118737969SJohn Birrell #endif
19526ff6d951SJohn Birrell 		(void) execvp(dtp->dt_cpp_path, argv);
19536ff6d951SJohn Birrell 		_exit(errno == ENOENT ? 127 : 126);
19546ff6d951SJohn Birrell 	}
19556ff6d951SJohn Birrell 
19566ff6d951SJohn Birrell 	do {
19576ff6d951SJohn Birrell 		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
19586ff6d951SJohn Birrell 		    (int)pid);
19596ff6d951SJohn Birrell 	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
19606ff6d951SJohn Birrell 
19616ff6d951SJohn Birrell 	(void) sigaction(SIGCHLD, &oact, NULL);
19626ff6d951SJohn Birrell 	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
19636ff6d951SJohn Birrell 
19646ff6d951SJohn Birrell 	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
19656ff6d951SJohn Birrell 	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
19666ff6d951SJohn Birrell 
19676ff6d951SJohn Birrell 	if (estat != 0) {
19686ff6d951SJohn Birrell 		switch (estat) {
19696ff6d951SJohn Birrell 		case 126:
19706ff6d951SJohn Birrell 			(void) dt_set_errno(dtp, EDT_CPPEXEC);
19716ff6d951SJohn Birrell 			break;
19726ff6d951SJohn Birrell 		case 127:
19736ff6d951SJohn Birrell 			(void) dt_set_errno(dtp, EDT_CPPENT);
19746ff6d951SJohn Birrell 			break;
19756ff6d951SJohn Birrell 		default:
19766ff6d951SJohn Birrell 			(void) dt_set_errno(dtp, EDT_CPPERR);
19776ff6d951SJohn Birrell 		}
19786ff6d951SJohn Birrell 		goto err;
19796ff6d951SJohn Birrell 	}
19806ff6d951SJohn Birrell 
19816ff6d951SJohn Birrell 	free(argv);
19826ff6d951SJohn Birrell 	(void) fflush(ofp);
19836ff6d951SJohn Birrell 	(void) fseek(ofp, 0, SEEK_SET);
19846ff6d951SJohn Birrell 	return (ofp);
19856ff6d951SJohn Birrell 
19866ff6d951SJohn Birrell err:
19876ff6d951SJohn Birrell 	free(argv);
19886ff6d951SJohn Birrell 	(void) fclose(ofp);
19896ff6d951SJohn Birrell 	return (NULL);
19906ff6d951SJohn Birrell }
19916ff6d951SJohn Birrell 
19926ff6d951SJohn Birrell static void
dt_lib_depend_error(dtrace_hdl_t * dtp,const char * format,...)19936ff6d951SJohn Birrell dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
19946ff6d951SJohn Birrell {
19956ff6d951SJohn Birrell 	va_list ap;
19966ff6d951SJohn Birrell 
19976ff6d951SJohn Birrell 	va_start(ap, format);
19986ff6d951SJohn Birrell 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
19996ff6d951SJohn Birrell 	va_end(ap);
20006ff6d951SJohn Birrell }
20016ff6d951SJohn Birrell 
20026ff6d951SJohn Birrell int
dt_lib_depend_add(dtrace_hdl_t * dtp,dt_list_t * dlp,const char * arg)20036ff6d951SJohn Birrell dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
20046ff6d951SJohn Birrell {
20056ff6d951SJohn Birrell 	dt_lib_depend_t *dld;
20066ff6d951SJohn Birrell 	const char *end;
20076ff6d951SJohn Birrell 
20086ff6d951SJohn Birrell 	assert(arg != NULL);
20096ff6d951SJohn Birrell 
20106ff6d951SJohn Birrell 	if ((end = strrchr(arg, '/')) == NULL)
20116ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EINVAL));
20126ff6d951SJohn Birrell 
20136ff6d951SJohn Birrell 	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
20146ff6d951SJohn Birrell 		return (-1);
20156ff6d951SJohn Birrell 
20166ff6d951SJohn Birrell 	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
20176ff6d951SJohn Birrell 		dt_free(dtp, dld);
20186ff6d951SJohn Birrell 		return (-1);
20196ff6d951SJohn Birrell 	}
20206ff6d951SJohn Birrell 
20216ff6d951SJohn Birrell 	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
20226ff6d951SJohn Birrell 	if ((dld->dtld_library = strdup(arg)) == NULL) {
20236ff6d951SJohn Birrell 		dt_free(dtp, dld->dtld_libpath);
20246ff6d951SJohn Birrell 		dt_free(dtp, dld);
20256ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
20266ff6d951SJohn Birrell 	}
20276ff6d951SJohn Birrell 
20286ff6d951SJohn Birrell 	dt_list_append(dlp, dld);
20296ff6d951SJohn Birrell 	return (0);
20306ff6d951SJohn Birrell }
20316ff6d951SJohn Birrell 
20326ff6d951SJohn Birrell dt_lib_depend_t *
dt_lib_depend_lookup(dt_list_t * dld,const char * arg)20336ff6d951SJohn Birrell dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
20346ff6d951SJohn Birrell {
20356ff6d951SJohn Birrell 	dt_lib_depend_t *dldn;
20366ff6d951SJohn Birrell 
20376ff6d951SJohn Birrell 	for (dldn = dt_list_next(dld); dldn != NULL;
20386ff6d951SJohn Birrell 	    dldn = dt_list_next(dldn)) {
20396ff6d951SJohn Birrell 		if (strcmp(dldn->dtld_library, arg) == 0)
20406ff6d951SJohn Birrell 			return (dldn);
20416ff6d951SJohn Birrell 	}
20426ff6d951SJohn Birrell 
20436ff6d951SJohn Birrell 	return (NULL);
20446ff6d951SJohn Birrell }
20456ff6d951SJohn Birrell 
20466ff6d951SJohn Birrell /*
20476ff6d951SJohn Birrell  * Go through all the library files, and, if any library dependencies exist for
20486ff6d951SJohn Birrell  * that file, add it to that node's list of dependents. The result of this
20496ff6d951SJohn Birrell  * will be a graph which can then be topologically sorted to produce a
20506ff6d951SJohn Birrell  * compilation order.
20516ff6d951SJohn Birrell  */
20526ff6d951SJohn Birrell static int
dt_lib_build_graph(dtrace_hdl_t * dtp)20536ff6d951SJohn Birrell dt_lib_build_graph(dtrace_hdl_t *dtp)
20546ff6d951SJohn Birrell {
20556ff6d951SJohn Birrell 	dt_lib_depend_t *dld, *dpld;
20566ff6d951SJohn Birrell 
20576ff6d951SJohn Birrell 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
20586ff6d951SJohn Birrell 	    dld = dt_list_next(dld)) {
20596ff6d951SJohn Birrell 		char *library = dld->dtld_library;
20606ff6d951SJohn Birrell 
20616ff6d951SJohn Birrell 		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
20626ff6d951SJohn Birrell 		    dpld = dt_list_next(dpld)) {
20636ff6d951SJohn Birrell 			dt_lib_depend_t *dlda;
20646ff6d951SJohn Birrell 
20656ff6d951SJohn Birrell 			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
20666ff6d951SJohn Birrell 			    dpld->dtld_library)) == NULL) {
20676ff6d951SJohn Birrell 				dt_lib_depend_error(dtp,
20686ff6d951SJohn Birrell 				    "Invalid library dependency in %s: %s\n",
20696ff6d951SJohn Birrell 				    dld->dtld_library, dpld->dtld_library);
20706ff6d951SJohn Birrell 
20716ff6d951SJohn Birrell 				return (dt_set_errno(dtp, EDT_COMPILER));
20726ff6d951SJohn Birrell 			}
20736ff6d951SJohn Birrell 
20746ff6d951SJohn Birrell 			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
20756ff6d951SJohn Birrell 			    library)) != 0) {
20766ff6d951SJohn Birrell 				return (-1); /* preserve dt_errno */
20776ff6d951SJohn Birrell 			}
20786ff6d951SJohn Birrell 		}
20796ff6d951SJohn Birrell 	}
20806ff6d951SJohn Birrell 	return (0);
20816ff6d951SJohn Birrell }
20826ff6d951SJohn Birrell 
20836ff6d951SJohn Birrell static int
dt_topo_sort(dtrace_hdl_t * dtp,dt_lib_depend_t * dld,int * count)20846ff6d951SJohn Birrell dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
20856ff6d951SJohn Birrell {
20866ff6d951SJohn Birrell 	dt_lib_depend_t *dpld, *dlda, *new;
20876ff6d951SJohn Birrell 
20886ff6d951SJohn Birrell 	dld->dtld_start = ++(*count);
20896ff6d951SJohn Birrell 
20906ff6d951SJohn Birrell 	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
20916ff6d951SJohn Birrell 	    dpld = dt_list_next(dpld)) {
20926ff6d951SJohn Birrell 		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
20936ff6d951SJohn Birrell 		    dpld->dtld_library);
20946ff6d951SJohn Birrell 		assert(dlda != NULL);
20956ff6d951SJohn Birrell 
20966ff6d951SJohn Birrell 		if (dlda->dtld_start == 0 &&
20976ff6d951SJohn Birrell 		    dt_topo_sort(dtp, dlda, count) == -1)
20986ff6d951SJohn Birrell 			return (-1);
20996ff6d951SJohn Birrell 	}
21006ff6d951SJohn Birrell 
21016ff6d951SJohn Birrell 	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
21026ff6d951SJohn Birrell 		return (-1);
21036ff6d951SJohn Birrell 
21046ff6d951SJohn Birrell 	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
21056ff6d951SJohn Birrell 		dt_free(dtp, new);
21066ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
21076ff6d951SJohn Birrell 	}
21086ff6d951SJohn Birrell 
21096ff6d951SJohn Birrell 	new->dtld_start = dld->dtld_start;
21106ff6d951SJohn Birrell 	new->dtld_finish = dld->dtld_finish = ++(*count);
21116ff6d951SJohn Birrell 	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
21126ff6d951SJohn Birrell 
21136ff6d951SJohn Birrell 	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
21146ff6d951SJohn Birrell 	    new->dtld_start, new->dtld_finish);
21156ff6d951SJohn Birrell 
21166ff6d951SJohn Birrell 	return (0);
21176ff6d951SJohn Birrell }
21186ff6d951SJohn Birrell 
21196ff6d951SJohn Birrell static int
dt_lib_depend_sort(dtrace_hdl_t * dtp)21206ff6d951SJohn Birrell dt_lib_depend_sort(dtrace_hdl_t *dtp)
21216ff6d951SJohn Birrell {
21226ff6d951SJohn Birrell 	dt_lib_depend_t *dld, *dpld, *dlda;
21236ff6d951SJohn Birrell 	int count = 0;
21246ff6d951SJohn Birrell 
21256ff6d951SJohn Birrell 	if (dt_lib_build_graph(dtp) == -1)
21266ff6d951SJohn Birrell 		return (-1); /* preserve dt_errno */
21276ff6d951SJohn Birrell 
21286ff6d951SJohn Birrell 	/*
21296ff6d951SJohn Birrell 	 * Perform a topological sort of the graph that hangs off
21306ff6d951SJohn Birrell 	 * dtp->dt_lib_dep. The result of this process will be a
21316ff6d951SJohn Birrell 	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
21326ff6d951SJohn Birrell 	 */
21336ff6d951SJohn Birrell 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
21346ff6d951SJohn Birrell 	    dld = dt_list_next(dld)) {
21356ff6d951SJohn Birrell 		if (dld->dtld_start == 0 &&
21366ff6d951SJohn Birrell 		    dt_topo_sort(dtp, dld, &count) == -1)
21376ff6d951SJohn Birrell 			return (-1); /* preserve dt_errno */;
21386ff6d951SJohn Birrell 	}
21396ff6d951SJohn Birrell 
21406ff6d951SJohn Birrell 	/*
21416ff6d951SJohn Birrell 	 * Check the graph for cycles. If an ancestor's finishing time is
21426ff6d951SJohn Birrell 	 * less than any of its dependent's finishing times then a back edge
21436ff6d951SJohn Birrell 	 * exists in the graph and this is a cycle.
21446ff6d951SJohn Birrell 	 */
21456ff6d951SJohn Birrell 	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
21466ff6d951SJohn Birrell 	    dld = dt_list_next(dld)) {
21476ff6d951SJohn Birrell 		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
21486ff6d951SJohn Birrell 		    dpld = dt_list_next(dpld)) {
21496ff6d951SJohn Birrell 			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
21506ff6d951SJohn Birrell 			    dpld->dtld_library);
21516ff6d951SJohn Birrell 			assert(dlda != NULL);
21526ff6d951SJohn Birrell 
21536ff6d951SJohn Birrell 			if (dlda->dtld_finish > dld->dtld_finish) {
21546ff6d951SJohn Birrell 				dt_lib_depend_error(dtp,
21556ff6d951SJohn Birrell 				    "Cyclic dependency detected: %s => %s\n",
21566ff6d951SJohn Birrell 				    dld->dtld_library, dpld->dtld_library);
21576ff6d951SJohn Birrell 
21586ff6d951SJohn Birrell 				return (dt_set_errno(dtp, EDT_COMPILER));
21596ff6d951SJohn Birrell 			}
21606ff6d951SJohn Birrell 		}
21616ff6d951SJohn Birrell 	}
21626ff6d951SJohn Birrell 
21636ff6d951SJohn Birrell 	return (0);
21646ff6d951SJohn Birrell }
21656ff6d951SJohn Birrell 
21666ff6d951SJohn Birrell static void
dt_lib_depend_free(dtrace_hdl_t * dtp)21676ff6d951SJohn Birrell dt_lib_depend_free(dtrace_hdl_t *dtp)
21686ff6d951SJohn Birrell {
21696ff6d951SJohn Birrell 	dt_lib_depend_t *dld, *dlda;
21706ff6d951SJohn Birrell 
21716ff6d951SJohn Birrell 	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
21726ff6d951SJohn Birrell 		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
21736ff6d951SJohn Birrell 			dt_list_delete(&dld->dtld_dependencies, dlda);
21746ff6d951SJohn Birrell 			dt_free(dtp, dlda->dtld_library);
21756ff6d951SJohn Birrell 			dt_free(dtp, dlda->dtld_libpath);
21766ff6d951SJohn Birrell 			dt_free(dtp, dlda);
21776ff6d951SJohn Birrell 		}
21786ff6d951SJohn Birrell 		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
21796ff6d951SJohn Birrell 			dt_list_delete(&dld->dtld_dependents, dlda);
21806ff6d951SJohn Birrell 			dt_free(dtp, dlda->dtld_library);
21816ff6d951SJohn Birrell 			dt_free(dtp, dlda->dtld_libpath);
21826ff6d951SJohn Birrell 			dt_free(dtp, dlda);
21836ff6d951SJohn Birrell 		}
21846ff6d951SJohn Birrell 		dt_list_delete(&dtp->dt_lib_dep, dld);
21856ff6d951SJohn Birrell 		dt_free(dtp, dld->dtld_library);
21866ff6d951SJohn Birrell 		dt_free(dtp, dld->dtld_libpath);
21876ff6d951SJohn Birrell 		dt_free(dtp, dld);
21886ff6d951SJohn Birrell 	}
21896ff6d951SJohn Birrell 
21906ff6d951SJohn Birrell 	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
21916ff6d951SJohn Birrell 		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
21926ff6d951SJohn Birrell 		dt_free(dtp, dld->dtld_library);
21936ff6d951SJohn Birrell 		dt_free(dtp, dld);
21946ff6d951SJohn Birrell 	}
21956ff6d951SJohn Birrell }
21966ff6d951SJohn Birrell 
21976ff6d951SJohn Birrell /*
2198694a0093SPedro F. Giffuni  * Open all the .d library files found in the specified directory and
2199694a0093SPedro F. Giffuni  * compile each one of them.  We silently ignore any missing directories and
2200694a0093SPedro F. Giffuni  * other files found therein.  We only fail (and thereby fail dt_load_libs()) if
2201694a0093SPedro F. Giffuni  * we fail to compile a library and the error is something other than #pragma D
2202694a0093SPedro F. Giffuni  * depends_on.  Dependency errors are silently ignored to permit a library
2203694a0093SPedro F. Giffuni  * directory to contain libraries which may not be accessible depending on our
2204694a0093SPedro F. Giffuni  * privileges.
22056ff6d951SJohn Birrell  */
22066ff6d951SJohn Birrell static int
dt_load_libs_dir(dtrace_hdl_t * dtp,const char * path)22076ff6d951SJohn Birrell dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
22086ff6d951SJohn Birrell {
22096ff6d951SJohn Birrell 	struct dirent *dp;
2210694a0093SPedro F. Giffuni 	const char *p, *end;
22116ff6d951SJohn Birrell 	DIR *dirp;
22126ff6d951SJohn Birrell 
22136ff6d951SJohn Birrell 	char fname[PATH_MAX];
22146ff6d951SJohn Birrell 	FILE *fp;
22156ff6d951SJohn Birrell 	void *rv;
22166ff6d951SJohn Birrell 	dt_lib_depend_t *dld;
22176ff6d951SJohn Birrell 
22186ff6d951SJohn Birrell 	if ((dirp = opendir(path)) == NULL) {
22196ff6d951SJohn Birrell 		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
22206ff6d951SJohn Birrell 		return (0);
22216ff6d951SJohn Birrell 	}
22226ff6d951SJohn Birrell 
22236ff6d951SJohn Birrell 	/* First, parse each file for library dependencies. */
22246ff6d951SJohn Birrell 	while ((dp = readdir(dirp)) != NULL) {
22256ff6d951SJohn Birrell 		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
22266ff6d951SJohn Birrell 			continue; /* skip any filename not ending in .d */
22276ff6d951SJohn Birrell 
22286ff6d951SJohn Birrell 		(void) snprintf(fname, sizeof (fname),
22296ff6d951SJohn Birrell 		    "%s/%s", path, dp->d_name);
22306ff6d951SJohn Birrell 
22316ff6d951SJohn Birrell 		if ((fp = fopen(fname, "r")) == NULL) {
22326ff6d951SJohn Birrell 			dt_dprintf("skipping library %s: %s\n",
22336ff6d951SJohn Birrell 			    fname, strerror(errno));
22346ff6d951SJohn Birrell 			continue;
22356ff6d951SJohn Birrell 		}
22366ff6d951SJohn Birrell 
2237694a0093SPedro F. Giffuni 		/*
2238694a0093SPedro F. Giffuni 		 * Skip files whose name match an already processed library
2239694a0093SPedro F. Giffuni 		 */
2240694a0093SPedro F. Giffuni 		for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2241694a0093SPedro F. Giffuni 		    dld = dt_list_next(dld)) {
2242694a0093SPedro F. Giffuni 			end = strrchr(dld->dtld_library, '/');
2243694a0093SPedro F. Giffuni 			/* dt_lib_depend_add ensures this */
2244694a0093SPedro F. Giffuni 			assert(end != NULL);
2245694a0093SPedro F. Giffuni 			if (strcmp(end + 1, dp->d_name) == 0)
2246694a0093SPedro F. Giffuni 				break;
2247694a0093SPedro F. Giffuni 		}
2248694a0093SPedro F. Giffuni 
2249694a0093SPedro F. Giffuni 		if (dld != NULL) {
2250694a0093SPedro F. Giffuni 			dt_dprintf("skipping library %s, already processed "
2251694a0093SPedro F. Giffuni 			    "library with the same name: %s", dp->d_name,
2252694a0093SPedro F. Giffuni 			    dld->dtld_library);
2253fcb8a889SPedro F. Giffuni 			(void) fclose(fp);
2254694a0093SPedro F. Giffuni 			continue;
2255694a0093SPedro F. Giffuni 		}
2256694a0093SPedro F. Giffuni 
22576ff6d951SJohn Birrell 		dtp->dt_filetag = fname;
2258fcb8a889SPedro F. Giffuni 		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0) {
2259fcb8a889SPedro F. Giffuni 			(void) fclose(fp);
2260694a0093SPedro F. Giffuni 			return (-1); /* preserve dt_errno */
2261fcb8a889SPedro F. Giffuni 		}
22626ff6d951SJohn Birrell 
22636ff6d951SJohn Birrell 		rv = dt_compile(dtp, DT_CTX_DPROG,
22646ff6d951SJohn Birrell 		    DTRACE_PROBESPEC_NAME, NULL,
22656ff6d951SJohn Birrell 		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
22666ff6d951SJohn Birrell 
22676ff6d951SJohn Birrell 		if (rv != NULL && dtp->dt_errno &&
22686ff6d951SJohn Birrell 		    (dtp->dt_errno != EDT_COMPILER ||
2269fcb8a889SPedro F. Giffuni 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
2270fcb8a889SPedro F. Giffuni 			(void) fclose(fp);
2271694a0093SPedro F. Giffuni 			return (-1); /* preserve dt_errno */
2272fcb8a889SPedro F. Giffuni 		}
22736ff6d951SJohn Birrell 
22746ff6d951SJohn Birrell 		if (dtp->dt_errno)
22756ff6d951SJohn Birrell 			dt_dprintf("error parsing library %s: %s\n",
22766ff6d951SJohn Birrell 			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
22776ff6d951SJohn Birrell 
22786ff6d951SJohn Birrell 		(void) fclose(fp);
22796ff6d951SJohn Birrell 		dtp->dt_filetag = NULL;
22806ff6d951SJohn Birrell 	}
22816ff6d951SJohn Birrell 
22826ff6d951SJohn Birrell 	(void) closedir(dirp);
2283694a0093SPedro F. Giffuni 
2284694a0093SPedro F. Giffuni 	return (0);
2285694a0093SPedro F. Giffuni }
2286694a0093SPedro F. Giffuni 
2287694a0093SPedro F. Giffuni /*
2288694a0093SPedro F. Giffuni  * Perform a topological sorting of all the libraries found across the entire
2289694a0093SPedro F. Giffuni  * dt_lib_path.  Once sorted, compile each one in topological order to cache its
2290694a0093SPedro F. Giffuni  * inlines and translators, etc.  We silently ignore any missing directories and
2291694a0093SPedro F. Giffuni  * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2292694a0093SPedro F. Giffuni  * we fail to compile a library and the error is something other than #pragma D
2293694a0093SPedro F. Giffuni  * depends_on.  Dependency errors are silently ignored to permit a library
2294694a0093SPedro F. Giffuni  * directory to contain libraries which may not be accessible depending on our
2295694a0093SPedro F. Giffuni  * privileges.
2296694a0093SPedro F. Giffuni  */
2297694a0093SPedro F. Giffuni static int
dt_load_libs_sort(dtrace_hdl_t * dtp)2298694a0093SPedro F. Giffuni dt_load_libs_sort(dtrace_hdl_t *dtp)
2299694a0093SPedro F. Giffuni {
2300694a0093SPedro F. Giffuni 	dtrace_prog_t *pgp;
2301694a0093SPedro F. Giffuni 	FILE *fp;
2302694a0093SPedro F. Giffuni 	dt_lib_depend_t *dld;
2303694a0093SPedro F. Giffuni 
23046ff6d951SJohn Birrell 	/*
23056ff6d951SJohn Birrell 	 * Finish building the graph containing the library dependencies
23066ff6d951SJohn Birrell 	 * and perform a topological sort to generate an ordered list
23076ff6d951SJohn Birrell 	 * for compilation.
23086ff6d951SJohn Birrell 	 */
23096ff6d951SJohn Birrell 	if (dt_lib_depend_sort(dtp) == -1)
23106ff6d951SJohn Birrell 		goto err;
23116ff6d951SJohn Birrell 
23126ff6d951SJohn Birrell 	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
23136ff6d951SJohn Birrell 	    dld = dt_list_next(dld)) {
23146ff6d951SJohn Birrell 
23156ff6d951SJohn Birrell 		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
23166ff6d951SJohn Birrell 			dt_dprintf("skipping library %s: %s\n",
23176ff6d951SJohn Birrell 			    dld->dtld_library, strerror(errno));
23186ff6d951SJohn Birrell 			continue;
23196ff6d951SJohn Birrell 		}
23206ff6d951SJohn Birrell 
23216ff6d951SJohn Birrell 		dtp->dt_filetag = dld->dtld_library;
23226ff6d951SJohn Birrell 		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
23236ff6d951SJohn Birrell 		(void) fclose(fp);
23246ff6d951SJohn Birrell 		dtp->dt_filetag = NULL;
23256ff6d951SJohn Birrell 
23266ff6d951SJohn Birrell 		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
23276ff6d951SJohn Birrell 		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
23286ff6d951SJohn Birrell 			goto err;
23296ff6d951SJohn Birrell 
23306ff6d951SJohn Birrell 		if (pgp == NULL) {
23316ff6d951SJohn Birrell 			dt_dprintf("skipping library %s: %s\n",
23326ff6d951SJohn Birrell 			    dld->dtld_library,
23336ff6d951SJohn Birrell 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
233418737969SJohn Birrell 		} else {
233518737969SJohn Birrell 			dld->dtld_loaded = B_TRUE;
23366ff6d951SJohn Birrell 			dt_program_destroy(dtp, pgp);
23376ff6d951SJohn Birrell 		}
233818737969SJohn Birrell 	}
23396ff6d951SJohn Birrell 
23406ff6d951SJohn Birrell 	dt_lib_depend_free(dtp);
23416ff6d951SJohn Birrell 	return (0);
23426ff6d951SJohn Birrell 
23436ff6d951SJohn Birrell err:
23446ff6d951SJohn Birrell 	dt_lib_depend_free(dtp);
23456ff6d951SJohn Birrell 	return (-1); /* preserve dt_errno */
23466ff6d951SJohn Birrell }
23476ff6d951SJohn Birrell 
23486ff6d951SJohn Birrell /*
23496ff6d951SJohn Birrell  * Load the contents of any appropriate DTrace .d library files.  These files
23506ff6d951SJohn Birrell  * contain inlines and translators that will be cached by the compiler.  We
23516ff6d951SJohn Birrell  * defer this activity until the first compile to permit libdtrace clients to
23526ff6d951SJohn Birrell  * add their own library directories and so that we can properly report errors.
23536ff6d951SJohn Birrell  */
23546ff6d951SJohn Birrell static int
dt_load_libs(dtrace_hdl_t * dtp)23556ff6d951SJohn Birrell dt_load_libs(dtrace_hdl_t *dtp)
23566ff6d951SJohn Birrell {
23576ff6d951SJohn Birrell 	dt_dirpath_t *dirp;
23586ff6d951SJohn Birrell 
23596ff6d951SJohn Birrell 	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
23606ff6d951SJohn Birrell 		return (0); /* libraries already processed */
23616ff6d951SJohn Birrell 
23626ff6d951SJohn Birrell 	dtp->dt_cflags |= DTRACE_C_NOLIBS;
23636ff6d951SJohn Birrell 
2364694a0093SPedro F. Giffuni 	/*
2365694a0093SPedro F. Giffuni 	 * /usr/lib/dtrace is always at the head of the list. The rest of the
2366694a0093SPedro F. Giffuni 	 * list is specified in the precedence order the user requested. Process
2367694a0093SPedro F. Giffuni 	 * everything other than the head first. DTRACE_C_NOLIBS has already
2368694a0093SPedro F. Giffuni 	 * been spcified so dt_vopen will ensure that there is always one entry
2369694a0093SPedro F. Giffuni 	 * in dt_lib_path.
2370694a0093SPedro F. Giffuni 	 */
2371694a0093SPedro F. Giffuni 	for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
23726ff6d951SJohn Birrell 	    dirp != NULL; dirp = dt_list_next(dirp)) {
23736ff6d951SJohn Birrell 		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
23746ff6d951SJohn Birrell 			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
23756ff6d951SJohn Birrell 			return (-1); /* errno is set for us */
23766ff6d951SJohn Birrell 		}
23776ff6d951SJohn Birrell 	}
23786ff6d951SJohn Birrell 
2379694a0093SPedro F. Giffuni 	/* Handle /usr/lib/dtrace */
2380694a0093SPedro F. Giffuni 	dirp = dt_list_next(&dtp->dt_lib_path);
2381694a0093SPedro F. Giffuni 	if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2382694a0093SPedro F. Giffuni 		dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2383694a0093SPedro F. Giffuni 		return (-1); /* errno is set for us */
2384694a0093SPedro F. Giffuni 	}
2385694a0093SPedro F. Giffuni 
2386694a0093SPedro F. Giffuni 	if (dt_load_libs_sort(dtp) < 0)
2387694a0093SPedro F. Giffuni 		return (-1); /* errno is set for us */
2388694a0093SPedro F. Giffuni 
23896ff6d951SJohn Birrell 	return (0);
23906ff6d951SJohn Birrell }
23916ff6d951SJohn Birrell 
23926ff6d951SJohn Birrell 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)23936ff6d951SJohn Birrell dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
23946ff6d951SJohn Birrell     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
23956ff6d951SJohn Birrell {
23966ff6d951SJohn Birrell 	dt_node_t *dnp;
23976ff6d951SJohn Birrell 	dt_decl_t *ddp;
23986ff6d951SJohn Birrell 	dt_pcb_t pcb;
2399ba69823eSMark Johnston 	void *volatile rv;
24006ff6d951SJohn Birrell 	int err;
24016ff6d951SJohn Birrell 
24026ff6d951SJohn Birrell 	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
24036ff6d951SJohn Birrell 		(void) dt_set_errno(dtp, EINVAL);
24046ff6d951SJohn Birrell 		return (NULL);
24056ff6d951SJohn Birrell 	}
24066ff6d951SJohn Birrell 
24076ff6d951SJohn Birrell 	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
24086ff6d951SJohn Birrell 		return (NULL); /* errno is set for us */
24096ff6d951SJohn Birrell 
24101670a1c2SRui Paulo 	if (dtp->dt_globals->dh_nelems != 0)
24116ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
24121670a1c2SRui Paulo 
24131670a1c2SRui Paulo 	if (dtp->dt_tls->dh_nelems != 0)
24146ff6d951SJohn Birrell 		(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
24156ff6d951SJohn Birrell 
24166ff6d951SJohn Birrell 	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
24176ff6d951SJohn Birrell 		return (NULL); /* errno is set for us */
24186ff6d951SJohn Birrell 
24196ff6d951SJohn Birrell 	dt_pcb_push(dtp, &pcb);
24206ff6d951SJohn Birrell 
24216ff6d951SJohn Birrell 	pcb.pcb_fileptr = fp;
24226ff6d951SJohn Birrell 	pcb.pcb_string = s;
24236ff6d951SJohn Birrell 	pcb.pcb_strptr = s;
24246ff6d951SJohn Birrell 	pcb.pcb_strlen = s ? strlen(s) : 0;
24256ff6d951SJohn Birrell 	pcb.pcb_sargc = argc;
24266ff6d951SJohn Birrell 	pcb.pcb_sargv = argv;
24276ff6d951SJohn Birrell 	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
24286ff6d951SJohn Birrell 	pcb.pcb_pspec = pspec;
24296ff6d951SJohn Birrell 	pcb.pcb_cflags = dtp->dt_cflags | cflags;
24306ff6d951SJohn Birrell 	pcb.pcb_amin = dtp->dt_amin;
24316ff6d951SJohn Birrell 	pcb.pcb_yystate = -1;
24326ff6d951SJohn Birrell 	pcb.pcb_context = context;
24336ff6d951SJohn Birrell 	pcb.pcb_token = context;
24346ff6d951SJohn Birrell 
24356ff6d951SJohn Birrell 	if (context != DT_CTX_DPROG)
24366ff6d951SJohn Birrell 		yybegin(YYS_EXPR);
24376ff6d951SJohn Birrell 	else if (cflags & DTRACE_C_CTL)
24386ff6d951SJohn Birrell 		yybegin(YYS_CONTROL);
24396ff6d951SJohn Birrell 	else
24406ff6d951SJohn Birrell 		yybegin(YYS_CLAUSE);
24416ff6d951SJohn Birrell 
24426ff6d951SJohn Birrell 	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
24436ff6d951SJohn Birrell 		goto out;
24446ff6d951SJohn Birrell 
24456ff6d951SJohn Birrell 	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
24466ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
24476ff6d951SJohn Birrell 
24486ff6d951SJohn Birrell 	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
24496ff6d951SJohn Birrell 	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
24506ff6d951SJohn Birrell 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
24516ff6d951SJohn Birrell 
24526ff6d951SJohn Birrell 	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
24536ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
24546ff6d951SJohn Birrell 
24556ff6d951SJohn Birrell 	/*
24566ff6d951SJohn Birrell 	 * Invoke the parser to evaluate the D source code.  If any errors
24576ff6d951SJohn Birrell 	 * occur during parsing, an error function will be called and we
24586ff6d951SJohn Birrell 	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
24596ff6d951SJohn Birrell 	 * we optionally display the parse tree if debugging is enabled.
24606ff6d951SJohn Birrell 	 */
24616ff6d951SJohn Birrell 	if (yyparse() != 0 || yypcb->pcb_root == NULL)
24626ff6d951SJohn Birrell 		xyerror(D_EMPTY, "empty D program translation unit\n");
24636ff6d951SJohn Birrell 
24646ff6d951SJohn Birrell 	yybegin(YYS_DONE);
24656ff6d951SJohn Birrell 
24666ff6d951SJohn Birrell 	if (cflags & DTRACE_C_CTL)
24676ff6d951SJohn Birrell 		goto out;
24686ff6d951SJohn Birrell 
24696ff6d951SJohn Birrell 	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
24706ff6d951SJohn Birrell 		dt_node_printr(yypcb->pcb_root, stderr, 0);
24716ff6d951SJohn Birrell 
24726ff6d951SJohn Birrell 	if (yypcb->pcb_pragmas != NULL)
24736ff6d951SJohn Birrell 		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
24746ff6d951SJohn Birrell 
24756ff6d951SJohn Birrell 	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
24766ff6d951SJohn Birrell 	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
24776ff6d951SJohn Birrell 		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
24786ff6d951SJohn Birrell 		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
24796ff6d951SJohn Birrell 	}
24806ff6d951SJohn Birrell 
2481*1e136a9cSChristos Margiolis 	/* Perform sugar transformations. */
2482650f66acSMark Johnston 	if (context == DT_CTX_DPROG) {
2483650f66acSMark Johnston 		dt_node_t *dnp, *next_dnp;
2484650f66acSMark Johnston 		dt_node_t *new_list = NULL;
2485650f66acSMark Johnston 
2486650f66acSMark Johnston 		for (dnp = yypcb->pcb_root->dn_list;
2487650f66acSMark Johnston 		    dnp != NULL; dnp = next_dnp) {
2488650f66acSMark Johnston 			/* remove this node from the list */
2489650f66acSMark Johnston 			next_dnp = dnp->dn_list;
2490650f66acSMark Johnston 			dnp->dn_list = NULL;
2491650f66acSMark Johnston 
2492*1e136a9cSChristos Margiolis 			if (dnp->dn_kind == DT_NODE_CLAUSE) {
2493650f66acSMark Johnston 				dnp = dt_compile_sugar(dtp, dnp);
2494*1e136a9cSChristos Margiolis 				if (cflags & DTRACE_C_SUGAR) {
2495*1e136a9cSChristos Margiolis 					dt_node_t *p;
2496*1e136a9cSChristos Margiolis 
2497*1e136a9cSChristos Margiolis 					dt_printd(dnp, stdout, 0);
2498*1e136a9cSChristos Margiolis 					for (p = dnp->dn_list; p != NULL;
2499*1e136a9cSChristos Margiolis 					    p = p->dn_list)
2500*1e136a9cSChristos Margiolis 						dt_printd(p, stdout, 0);
2501*1e136a9cSChristos Margiolis 				}
2502*1e136a9cSChristos Margiolis 			}
2503650f66acSMark Johnston 			/* append node to the new list */
2504650f66acSMark Johnston 			new_list = dt_node_link(new_list, dnp);
2505650f66acSMark Johnston 		}
2506650f66acSMark Johnston 		yypcb->pcb_root->dn_list = new_list;
2507650f66acSMark Johnston 	}
2508650f66acSMark Johnston 
2509650f66acSMark Johnston 	/*
25106ff6d951SJohn Birrell 	 * If we have successfully created a parse tree for a D program, loop
25116ff6d951SJohn Birrell 	 * over the clauses and actions and instantiate the corresponding
25126ff6d951SJohn Birrell 	 * libdtrace program.  If we are parsing a D expression, then we
25136ff6d951SJohn Birrell 	 * simply run the code generator and assembler on the resulting tree.
25146ff6d951SJohn Birrell 	 */
25156ff6d951SJohn Birrell 	switch (context) {
25166ff6d951SJohn Birrell 	case DT_CTX_DPROG:
25176ff6d951SJohn Birrell 		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
25186ff6d951SJohn Birrell 
25196ff6d951SJohn Birrell 		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
25206ff6d951SJohn Birrell 		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
25216ff6d951SJohn Birrell 			xyerror(D_EMPTY, "empty D program translation unit\n");
25226ff6d951SJohn Birrell 
25236ff6d951SJohn Birrell 		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
25246ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
25256ff6d951SJohn Birrell 
25266ff6d951SJohn Birrell 		for (; dnp != NULL; dnp = dnp->dn_list) {
25276ff6d951SJohn Birrell 			switch (dnp->dn_kind) {
25286ff6d951SJohn Birrell 			case DT_NODE_CLAUSE:
2529650f66acSMark Johnston 				if (DT_TREEDUMP_PASS(dtp, 4))
2530650f66acSMark Johnston 					dt_printd(dnp, stderr, 0);
25316ff6d951SJohn Birrell 				dt_compile_clause(dtp, dnp);
25326ff6d951SJohn Birrell 				break;
25336ff6d951SJohn Birrell 			case DT_NODE_XLATOR:
25346ff6d951SJohn Birrell 				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
25356ff6d951SJohn Birrell 					dt_compile_xlator(dnp);
25366ff6d951SJohn Birrell 				break;
25376ff6d951SJohn Birrell 			case DT_NODE_PROVIDER:
25386ff6d951SJohn Birrell 				(void) dt_node_cook(dnp, DT_IDFLG_REF);
25396ff6d951SJohn Birrell 				break;
25406ff6d951SJohn Birrell 			}
25416ff6d951SJohn Birrell 		}
25426ff6d951SJohn Birrell 
25436ff6d951SJohn Birrell 		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
25446ff6d951SJohn Birrell 		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
25456ff6d951SJohn Birrell 		yypcb->pcb_asxrefs = NULL;
25466ff6d951SJohn Birrell 		yypcb->pcb_asxreflen = 0;
25476ff6d951SJohn Birrell 
25486ff6d951SJohn Birrell 		rv = yypcb->pcb_prog;
25496ff6d951SJohn Birrell 		break;
25506ff6d951SJohn Birrell 
25516ff6d951SJohn Birrell 	case DT_CTX_DEXPR:
25526ff6d951SJohn Birrell 		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
25536ff6d951SJohn Birrell 		dt_cg(yypcb, yypcb->pcb_root);
25546ff6d951SJohn Birrell 		rv = dt_as(yypcb);
25556ff6d951SJohn Birrell 		break;
25566ff6d951SJohn Birrell 
25576ff6d951SJohn Birrell 	case DT_CTX_DTYPE:
25586ff6d951SJohn Birrell 		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
25596ff6d951SJohn Birrell 		err = dt_decl_type(ddp, arg);
25606ff6d951SJohn Birrell 		dt_decl_free(ddp);
25616ff6d951SJohn Birrell 
25626ff6d951SJohn Birrell 		if (err != 0)
25636ff6d951SJohn Birrell 			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
25646ff6d951SJohn Birrell 
25656ff6d951SJohn Birrell 		rv = NULL;
25666ff6d951SJohn Birrell 		break;
25676ff6d951SJohn Birrell 	}
25686ff6d951SJohn Birrell 
25696ff6d951SJohn Birrell out:
2570a98ff317SPedro F. Giffuni 	if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2571a98ff317SPedro F. Giffuni 	    DT_TREEDUMP_PASS(dtp, 3))
25726ff6d951SJohn Birrell 		dt_node_printr(yypcb->pcb_root, stderr, 0);
25736ff6d951SJohn Birrell 
25746ff6d951SJohn Birrell 	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
25756ff6d951SJohn Birrell 	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
25766ff6d951SJohn Birrell 	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
25776ff6d951SJohn Birrell 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
25786ff6d951SJohn Birrell 
25796ff6d951SJohn Birrell 	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
25806ff6d951SJohn Birrell 	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
25816ff6d951SJohn Birrell 	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
25826ff6d951SJohn Birrell 		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
25836ff6d951SJohn Birrell 
25846ff6d951SJohn Birrell 	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
25856ff6d951SJohn Birrell 		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
25866ff6d951SJohn Birrell 
25876ff6d951SJohn Birrell 	dt_pcb_pop(dtp, err);
25886ff6d951SJohn Birrell 	(void) dt_set_errno(dtp, err);
25896ff6d951SJohn Birrell 	return (err ? NULL : rv);
25906ff6d951SJohn Birrell }
25916ff6d951SJohn Birrell 
25926ff6d951SJohn Birrell 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[])25936ff6d951SJohn Birrell dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
25946ff6d951SJohn Birrell     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
25956ff6d951SJohn Birrell {
25966ff6d951SJohn Birrell 	return (dt_compile(dtp, DT_CTX_DPROG,
25976ff6d951SJohn Birrell 	    spec, NULL, cflags, argc, argv, NULL, s));
25986ff6d951SJohn Birrell }
25996ff6d951SJohn Birrell 
26006ff6d951SJohn Birrell dtrace_prog_t *
dtrace_program_fcompile(dtrace_hdl_t * dtp,FILE * fp,uint_t cflags,int argc,char * const argv[])26016ff6d951SJohn Birrell dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
26026ff6d951SJohn Birrell     uint_t cflags, int argc, char *const argv[])
26036ff6d951SJohn Birrell {
26046ff6d951SJohn Birrell 	return (dt_compile(dtp, DT_CTX_DPROG,
26056ff6d951SJohn Birrell 	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
26066ff6d951SJohn Birrell }
26076ff6d951SJohn Birrell 
26086ff6d951SJohn Birrell int
dtrace_type_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_typeinfo_t * dtt)26096ff6d951SJohn Birrell dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
26106ff6d951SJohn Birrell {
26116ff6d951SJohn Birrell 	(void) dt_compile(dtp, DT_CTX_DTYPE,
26126ff6d951SJohn Birrell 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
26136ff6d951SJohn Birrell 	return (dtp->dt_errno ? -1 : 0);
26146ff6d951SJohn Birrell }
26156ff6d951SJohn Birrell 
26166ff6d951SJohn Birrell int
dtrace_type_fcompile(dtrace_hdl_t * dtp,FILE * fp,dtrace_typeinfo_t * dtt)26176ff6d951SJohn Birrell dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
26186ff6d951SJohn Birrell {
26196ff6d951SJohn Birrell 	(void) dt_compile(dtp, DT_CTX_DTYPE,
26206ff6d951SJohn Birrell 	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
26216ff6d951SJohn Birrell 	return (dtp->dt_errno ? -1 : 0);
26226ff6d951SJohn Birrell }
2623