dt_cc.c revision 1.3
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/*
30 * DTrace D Language Compiler
31 *
32 * The code in this source file implements the main engine for the D language
33 * compiler.  The driver routine for the compiler is dt_compile(), below.  The
34 * compiler operates on either stdio FILEs or in-memory strings as its input
35 * and can produce either dtrace_prog_t structures from a D program or a single
36 * dtrace_difo_t structure from a D expression.  Multiple entry points are
37 * provided as wrappers around dt_compile() for the various input/output pairs.
38 * The compiler itself is implemented across the following source files:
39 *
40 * dt_lex.l - lex scanner
41 * dt_grammar.y - yacc grammar
42 * dt_parser.c - parse tree creation and semantic checking
43 * dt_decl.c - declaration stack processing
44 * dt_xlator.c - D translator lookup and creation
45 * dt_ident.c - identifier and symbol table routines
46 * dt_pragma.c - #pragma processing and D pragmas
47 * dt_printf.c - D printf() and printa() argument checking and processing
48 * dt_cc.c - compiler driver and dtrace_prog_t construction
49 * dt_cg.c - DIF code generator
50 * dt_as.c - DIF assembler
51 * dt_dof.c - dtrace_prog_t -> DOF conversion
52 *
53 * Several other source files provide collections of utility routines used by
54 * these major files.  The compiler itself is implemented in multiple passes:
55 *
56 * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
57 *     and parse tree nodes are constructed using the routines in dt_parser.c.
58 *     This node construction pass is described further in dt_parser.c.
59 *
60 * (2) The parse tree is "cooked" by assigning each clause a context (see the
61 *     routine dt_setcontext(), below) based on its probe description and then
62 *     recursively descending the tree performing semantic checking.  The cook
63 *     routines are also implemented in dt_parser.c and described there.
64 *
65 * (3) For actions that are DIF expression statements, the DIF code generator
66 *     and assembler are invoked to create a finished DIFO for the statement.
67 *
68 * (4) The dtrace_prog_t data structures for the program clauses and actions
69 *     are built, containing pointers to any DIFOs created in step (3).
70 *
71 * (5) The caller invokes a routine in dt_dof.c to convert the finished program
72 *     into DOF format for use in anonymous tracing or enabling in the kernel.
73 *
74 * In the implementation, steps 2-4 are intertwined in that they are performed
75 * in order for each clause as part of a loop that executes over the clauses.
76 *
77 * The D compiler currently implements nearly no optimization.  The compiler
78 * implements integer constant folding as part of pass (1), and a set of very
79 * simple peephole optimizations as part of pass (3).  As with any C compiler,
80 * a large number of optimizations are possible on both the intermediate data
81 * structures and the generated DIF code.  These possibilities should be
82 * investigated in the context of whether they will have any substantive effect
83 * on the overall DTrace probe effect before they are undertaken.
84 */
85
86#include <sys/types.h>
87#include <sys/wait.h>
88
89#include <assert.h>
90#include <string.h>
91#include <strings.h>
92#include <signal.h>
93#include <unistd.h>
94#include <stdlib.h>
95#include <stdio.h>
96#include <errno.h>
97#include <ucontext.h>
98#include <limits.h>
99#include <ctype.h>
100#include <dirent.h>
101#include <dt_module.h>
102#include <dt_program.h>
103#include <dt_provider.h>
104#include <dt_printf.h>
105#include <dt_pid.h>
106#include <dt_grammar.h>
107#include <dt_ident.h>
108#include <dt_string.h>
109#include <dt_impl.h>
110
111static const dtrace_diftype_t dt_void_rtype = {
112	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
113};
114
115static const dtrace_diftype_t dt_int_rtype = {
116	DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
117};
118
119static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120    uint_t, int, char *const[], FILE *, const char *);
121
122
123/*ARGSUSED*/
124static int
125dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
126{
127	idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
128	    DT_IDFLG_DIFR | DT_IDFLG_DIFW);
129	return (0);
130}
131
132/*ARGSUSED*/
133static int
134dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
135{
136	yylineno = idp->di_lineno;
137	xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
138	return (0);
139}
140
141static dtrace_stmtdesc_t *
142dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
143    dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
144{
145	dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
146
147	if (sdp == NULL)
148		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
149
150	assert(yypcb->pcb_stmt == NULL);
151	yypcb->pcb_stmt = sdp;
152
153	sdp->dtsd_descattr = descattr;
154	sdp->dtsd_stmtattr = stmtattr;
155
156	return (sdp);
157}
158
159static dtrace_actdesc_t *
160dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
161{
162	dtrace_actdesc_t *new;
163
164	if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
165		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
166
167	return (new);
168}
169
170/*
171 * Utility function to determine if a given action description is destructive.
172 * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
173 */
174static int
175dt_action_destructive(const dtrace_actdesc_t *ap)
176{
177	return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
178	    DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
179}
180
181static void
182dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
183{
184	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
185	dtrace_actdesc_t *ap, *tap;
186	int commit = 0;
187	int speculate = 0;
188	int datarec = 0;
189
190	/*
191	 * Make sure that the new statement jibes with the rest of the ECB.
192	 */
193	for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
194		if (ap->dtad_kind == DTRACEACT_COMMIT) {
195			if (commit) {
196				dnerror(dnp, D_COMM_COMM, "commit( ) may "
197				    "not follow commit( )\n");
198			}
199
200			if (datarec) {
201				dnerror(dnp, D_COMM_DREC, "commit( ) may "
202				    "not follow data-recording action(s)\n");
203			}
204
205			for (tap = ap; tap != NULL; tap = tap->dtad_next) {
206				if (!DTRACEACT_ISAGG(tap->dtad_kind))
207					continue;
208
209				dnerror(dnp, D_AGG_COMM, "aggregating actions "
210				    "may not follow commit( )\n");
211			}
212
213			commit = 1;
214			continue;
215		}
216
217		if (ap->dtad_kind == DTRACEACT_SPECULATE) {
218			if (speculate) {
219				dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
220				    "not follow speculate( )\n");
221			}
222
223			if (commit) {
224				dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
225				    "not follow commit( )\n");
226			}
227
228			if (datarec) {
229				dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
230				    "not follow data-recording action(s)\n");
231			}
232
233			speculate = 1;
234			continue;
235		}
236
237		if (DTRACEACT_ISAGG(ap->dtad_kind)) {
238			if (speculate) {
239				dnerror(dnp, D_AGG_SPEC, "aggregating actions "
240				    "may not follow speculate( )\n");
241			}
242
243			datarec = 1;
244			continue;
245		}
246
247		if (speculate) {
248			if (dt_action_destructive(ap)) {
249				dnerror(dnp, D_ACT_SPEC, "destructive actions "
250				    "may not follow speculate( )\n");
251			}
252
253			if (ap->dtad_kind == DTRACEACT_EXIT) {
254				dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
255				    "follow speculate( )\n");
256			}
257		}
258
259		/*
260		 * Exclude all non data-recording actions.
261		 */
262		if (dt_action_destructive(ap) ||
263		    ap->dtad_kind == DTRACEACT_DISCARD)
264			continue;
265
266		if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
267		    ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
268		    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
269			continue;
270
271		if (commit) {
272			dnerror(dnp, D_DREC_COMM, "data-recording actions "
273			    "may not follow commit( )\n");
274		}
275
276		if (!speculate)
277			datarec = 1;
278	}
279
280	if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
281		longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
282
283	if (yypcb->pcb_stmt == sdp)
284		yypcb->pcb_stmt = NULL;
285}
286
287/*
288 * For the first element of an aggregation tuple or for printa(), we create a
289 * simple DIF program that simply returns the immediate value that is the ID
290 * of the aggregation itself.  This could be optimized in the future by
291 * creating a new in-kernel dtad_kind that just returns an integer.
292 */
293static void
294dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
295{
296	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
297	dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
298
299	if (dp == NULL)
300		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
301
302	dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
303	dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
304
305	if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
306		dt_difo_free(dtp, dp);
307		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
308	}
309
310	dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx	DIF_INTEGER[0], %r1 */
311	dp->dtdo_buf[1] = DIF_INSTR_RET(1);	/* ret	%r1 */
312	dp->dtdo_len = 2;
313	dp->dtdo_inttab[0] = id;
314	dp->dtdo_intlen = 1;
315	dp->dtdo_rtype = dt_int_rtype;
316
317	ap->dtad_difo = dp;
318	ap->dtad_kind = kind;
319}
320
321static void
322dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
323{
324	dt_ident_t *aid;
325	dtrace_actdesc_t *ap;
326	dt_node_t *anp;
327
328	char n[DT_TYPE_NAMELEN];
329	int argc = 0;
330
331	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
332		argc++; /* count up arguments for error messages below */
333
334	if (argc != 1) {
335		dnerror(dnp, D_CLEAR_PROTO,
336		    "%s( ) prototype mismatch: %d args passed, 1 expected\n",
337		    dnp->dn_ident->di_name, argc);
338	}
339
340	anp = dnp->dn_args;
341	assert(anp != NULL);
342
343	if (anp->dn_kind != DT_NODE_AGG) {
344		dnerror(dnp, D_CLEAR_AGGARG,
345		    "%s( ) argument #1 is incompatible with prototype:\n"
346		    "\tprototype: aggregation\n\t argument: %s\n",
347		    dnp->dn_ident->di_name,
348		    dt_node_type_name(anp, n, sizeof (n)));
349	}
350
351	aid = anp->dn_ident;
352
353	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
354		dnerror(dnp, D_CLEAR_AGGBAD,
355		    "undefined aggregation: @%s\n", aid->di_name);
356	}
357
358	ap = dt_stmt_action(dtp, sdp);
359	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
360	ap->dtad_arg = DT_ACT_CLEAR;
361}
362
363static void
364dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
365{
366	dt_ident_t *aid;
367	dtrace_actdesc_t *ap;
368	dt_node_t *anp, *normal;
369	int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
370
371	char n[DT_TYPE_NAMELEN];
372	int argc = 0;
373
374	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
375		argc++; /* count up arguments for error messages below */
376
377	if ((denormal && argc != 1) || (!denormal && argc != 2)) {
378		dnerror(dnp, D_NORMALIZE_PROTO,
379		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
380		    dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
381	}
382
383	anp = dnp->dn_args;
384	assert(anp != NULL);
385
386	if (anp->dn_kind != DT_NODE_AGG) {
387		dnerror(dnp, D_NORMALIZE_AGGARG,
388		    "%s( ) argument #1 is incompatible with prototype:\n"
389		    "\tprototype: aggregation\n\t argument: %s\n",
390		    dnp->dn_ident->di_name,
391		    dt_node_type_name(anp, n, sizeof (n)));
392	}
393
394	if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
395		dnerror(dnp, D_NORMALIZE_SCALAR,
396		    "%s( ) argument #2 must be of scalar type\n",
397		    dnp->dn_ident->di_name);
398	}
399
400	aid = anp->dn_ident;
401
402	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
403		dnerror(dnp, D_NORMALIZE_AGGBAD,
404		    "undefined aggregation: @%s\n", aid->di_name);
405	}
406
407	ap = dt_stmt_action(dtp, sdp);
408	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
409
410	if (denormal) {
411		ap->dtad_arg = DT_ACT_DENORMALIZE;
412		return;
413	}
414
415	ap->dtad_arg = DT_ACT_NORMALIZE;
416
417	assert(normal != NULL);
418	ap = dt_stmt_action(dtp, sdp);
419	dt_cg(yypcb, normal);
420
421	ap->dtad_difo = dt_as(yypcb);
422	ap->dtad_kind = DTRACEACT_LIBACT;
423	ap->dtad_arg = DT_ACT_NORMALIZE;
424}
425
426static void
427dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
428{
429	dt_ident_t *aid;
430	dtrace_actdesc_t *ap;
431	dt_node_t *anp, *trunc;
432
433	char n[DT_TYPE_NAMELEN];
434	int argc = 0;
435
436	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
437		argc++; /* count up arguments for error messages below */
438
439	if (argc > 2 || argc < 1) {
440		dnerror(dnp, D_TRUNC_PROTO,
441		    "%s( ) prototype mismatch: %d args passed, %s expected\n",
442		    dnp->dn_ident->di_name, argc,
443		    argc < 1 ? "at least 1" : "no more than 2");
444	}
445
446	anp = dnp->dn_args;
447	assert(anp != NULL);
448	trunc = anp->dn_list;
449
450	if (anp->dn_kind != DT_NODE_AGG) {
451		dnerror(dnp, D_TRUNC_AGGARG,
452		    "%s( ) argument #1 is incompatible with prototype:\n"
453		    "\tprototype: aggregation\n\t argument: %s\n",
454		    dnp->dn_ident->di_name,
455		    dt_node_type_name(anp, n, sizeof (n)));
456	}
457
458	if (argc == 2) {
459		assert(trunc != NULL);
460		if (!dt_node_is_scalar(trunc)) {
461			dnerror(dnp, D_TRUNC_SCALAR,
462			    "%s( ) argument #2 must be of scalar type\n",
463			    dnp->dn_ident->di_name);
464		}
465	}
466
467	aid = anp->dn_ident;
468
469	if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
470		dnerror(dnp, D_TRUNC_AGGBAD,
471		    "undefined aggregation: @%s\n", aid->di_name);
472	}
473
474	ap = dt_stmt_action(dtp, sdp);
475	dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
476	ap->dtad_arg = DT_ACT_TRUNC;
477
478	ap = dt_stmt_action(dtp, sdp);
479
480	if (argc == 1) {
481		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
482	} else {
483		assert(trunc != NULL);
484		dt_cg(yypcb, trunc);
485		ap->dtad_difo = dt_as(yypcb);
486		ap->dtad_kind = DTRACEACT_LIBACT;
487	}
488
489	ap->dtad_arg = DT_ACT_TRUNC;
490}
491
492static void
493dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
494{
495	dt_ident_t *aid, *fid;
496	dtrace_actdesc_t *ap;
497	const char *format;
498	dt_node_t *anp, *proto = NULL;
499
500	char n[DT_TYPE_NAMELEN];
501	int argc = 0, argr = 0;
502
503	for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
504		argc++; /* count up arguments for error messages below */
505
506	switch (dnp->dn_args->dn_kind) {
507	case DT_NODE_STRING:
508		format = dnp->dn_args->dn_string;
509		anp = dnp->dn_args->dn_list;
510		argr = 2;
511		break;
512	case DT_NODE_AGG:
513		format = NULL;
514		anp = dnp->dn_args;
515		argr = 1;
516		break;
517	default:
518		format = NULL;
519		anp = dnp->dn_args;
520		argr = 1;
521	}
522
523	if (argc < argr) {
524		dnerror(dnp, D_PRINTA_PROTO,
525		    "%s( ) prototype mismatch: %d args passed, %d expected\n",
526		    dnp->dn_ident->di_name, argc, argr);
527	}
528
529	assert(anp != NULL);
530
531	while (anp != NULL) {
532		if (anp->dn_kind != DT_NODE_AGG) {
533			dnerror(dnp, D_PRINTA_AGGARG,
534			    "%s( ) argument #%d is incompatible with "
535			    "prototype:\n\tprototype: aggregation\n"
536			    "\t argument: %s\n", dnp->dn_ident->di_name, argr,
537			    dt_node_type_name(anp, n, sizeof (n)));
538		}
539
540		aid = anp->dn_ident;
541		fid = aid->di_iarg;
542
543		if (aid->di_gen == dtp->dt_gen &&
544		    !(aid->di_flags & DT_IDFLG_MOD)) {
545			dnerror(dnp, D_PRINTA_AGGBAD,
546			    "undefined aggregation: @%s\n", aid->di_name);
547		}
548
549		/*
550		 * If we have multiple aggregations, we must be sure that
551		 * their key signatures match.
552		 */
553		if (proto != NULL) {
554			dt_printa_validate(proto, anp);
555		} else {
556			proto = anp;
557		}
558
559		if (format != NULL) {
560			yylineno = dnp->dn_line;
561
562			sdp->dtsd_fmtdata =
563			    dt_printf_create(yypcb->pcb_hdl, format);
564			dt_printf_validate(sdp->dtsd_fmtdata,
565			    DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
566			    fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
567			format = NULL;
568		}
569
570		ap = dt_stmt_action(dtp, sdp);
571		dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
572
573		anp = anp->dn_list;
574		argr++;
575	}
576}
577
578static void
579dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
580    dtrace_actkind_t kind)
581{
582	dt_node_t *anp, *arg1;
583	dtrace_actdesc_t *ap = NULL;
584	char n[DT_TYPE_NAMELEN], *str;
585
586	assert(DTRACEACT_ISPRINTFLIKE(kind));
587
588	if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
589		dnerror(dnp, D_PRINTF_ARG_FMT,
590		    "%s( ) argument #1 is incompatible with prototype:\n"
591		    "\tprototype: string constant\n\t argument: %s\n",
592		    dnp->dn_ident->di_name,
593		    dt_node_type_name(dnp->dn_args, n, sizeof (n)));
594	}
595
596	arg1 = dnp->dn_args->dn_list;
597	yylineno = dnp->dn_line;
598	str = dnp->dn_args->dn_string;
599
600
601	/*
602	 * If this is an freopen(), we use an empty string to denote that
603	 * stdout should be restored.  For other printf()-like actions, an
604	 * empty format string is illegal:  an empty format string would
605	 * result in malformed DOF, and the compiler thus flags an empty
606	 * format string as a compile-time error.  To avoid propagating the
607	 * freopen() special case throughout the system, we simply transpose
608	 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
609	 * denotes that stdout should be restored.
610	 */
611	if (kind == DTRACEACT_FREOPEN) {
612		if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
613			/*
614			 * Our sentinel is always an invalid argument to
615			 * freopen(), but if it's been manually specified, we
616			 * must fail now instead of when the freopen() is
617			 * actually evaluated.
618			 */
619			dnerror(dnp, D_FREOPEN_INVALID,
620			    "%s( ) argument #1 cannot be \"%s\"\n",
621			    dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
622		}
623
624		if (str[0] == '\0')
625			str = __UNCONST(DT_FREOPEN_RESTORE);
626	}
627
628	sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
629
630	dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
631	    dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
632
633	if (arg1 == NULL) {
634		dif_instr_t *dbuf;
635		dtrace_difo_t *dp;
636
637		if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
638		    (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
639			dt_free(dtp, dbuf);
640			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
641		}
642
643		dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
644
645		dp->dtdo_buf = dbuf;
646		dp->dtdo_len = 1;
647		dp->dtdo_rtype = dt_int_rtype;
648
649		ap = dt_stmt_action(dtp, sdp);
650		ap->dtad_difo = dp;
651		ap->dtad_kind = kind;
652		return;
653	}
654
655	for (anp = arg1; anp != NULL; anp = anp->dn_list) {
656		ap = dt_stmt_action(dtp, sdp);
657		dt_cg(yypcb, anp);
658		ap->dtad_difo = dt_as(yypcb);
659		ap->dtad_kind = kind;
660	}
661}
662
663static void
664dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
665{
666	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
667
668	if (dt_node_is_void(dnp->dn_args)) {
669		dnerror(dnp->dn_args, D_TRACE_VOID,
670		    "trace( ) may not be applied to a void expression\n");
671	}
672
673	if (dt_node_is_dynamic(dnp->dn_args)) {
674		dnerror(dnp->dn_args, D_TRACE_DYN,
675		    "trace( ) may not be applied to a dynamic expression\n");
676	}
677
678	dt_cg(yypcb, dnp->dn_args);
679	ap->dtad_difo = dt_as(yypcb);
680	ap->dtad_kind = DTRACEACT_DIFEXPR;
681}
682
683static void
684dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
685{
686	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
687
688	dt_node_t *addr = dnp->dn_args;
689	dt_node_t *size = dnp->dn_args->dn_list;
690
691	char n[DT_TYPE_NAMELEN];
692
693	if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
694		dnerror(addr, D_TRACEMEM_ADDR,
695		    "tracemem( ) argument #1 is incompatible with "
696		    "prototype:\n\tprototype: pointer or integer\n"
697		    "\t argument: %s\n",
698		    dt_node_type_name(addr, n, sizeof (n)));
699	}
700
701	if (dt_node_is_posconst(size) == 0) {
702		dnerror(size, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
703		    "be a non-zero positive integral constant expression\n");
704	}
705
706	dt_cg(yypcb, addr);
707	ap->dtad_difo = dt_as(yypcb);
708	ap->dtad_kind = DTRACEACT_DIFEXPR;
709
710	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
711	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
712}
713
714static void
715dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
716{
717	ap->dtad_kind = DTRACEACT_STACK;
718
719	if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
720		ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
721	} else {
722		ap->dtad_arg = 0;
723	}
724
725	if (arg0 != NULL) {
726		if (arg0->dn_list != NULL) {
727			dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
728			    "mismatch: too many arguments\n");
729		}
730
731		if (dt_node_is_posconst(arg0) == 0) {
732			dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
733			    "non-zero positive integral constant expression\n");
734		}
735
736		ap->dtad_arg = arg0->dn_value;
737	}
738}
739
740static void
741dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
742{
743	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
744	dt_action_stack_args(dtp, ap, dnp->dn_args);
745}
746
747static void
748dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
749{
750	uint32_t nframes = 0;
751	uint32_t strsize = 0;	/* default string table size */
752	dt_node_t *arg0 = dnp->dn_args;
753	dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
754
755	assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
756	    dnp->dn_ident->di_id == DT_ACT_USTACK);
757
758	if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
759		if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
760			nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
761
762		if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
763			strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
764
765		ap->dtad_kind = DTRACEACT_JSTACK;
766	} else {
767		assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
768
769		if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
770			nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
771
772		ap->dtad_kind = DTRACEACT_USTACK;
773	}
774
775	if (arg0 != NULL) {
776		if (!dt_node_is_posconst(arg0)) {
777			dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
778			    "must be a non-zero positive integer constant\n");
779		}
780		nframes = (uint32_t)arg0->dn_value;
781	}
782
783	if (arg1 != NULL) {
784		if (arg1->dn_kind != DT_NODE_INT ||
785		    ((arg1->dn_flags & DT_NF_SIGNED) &&
786		    (int64_t)arg1->dn_value < 0)) {
787			dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
788			    "must be a positive integer constant\n");
789		}
790
791		if (arg1->dn_list != NULL) {
792			dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
793			    "mismatch: too many arguments\n");
794		}
795
796		strsize = (uint32_t)arg1->dn_value;
797	}
798
799	ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
800}
801
802static void
803dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
804{
805	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
806	dt_action_ustack_args(dtp, ap, dnp);
807}
808
809static void
810dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
811{
812	dtrace_actdesc_t *ap;
813	dt_node_t *arg0, *arg1;
814
815	/*
816	 * The prototype guarantees that we are called with either one or
817	 * two arguments, and that any arguments that are present are strings.
818	 */
819	arg0 = dnp->dn_args;
820	arg1 = arg0->dn_list;
821
822	ap = dt_stmt_action(dtp, sdp);
823	dt_cg(yypcb, arg0);
824	ap->dtad_difo = dt_as(yypcb);
825	ap->dtad_kind = DTRACEACT_LIBACT;
826	ap->dtad_arg = DT_ACT_SETOPT;
827
828	ap = dt_stmt_action(dtp, sdp);
829
830	if (arg1 == NULL) {
831		dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
832	} else {
833		dt_cg(yypcb, arg1);
834		ap->dtad_difo = dt_as(yypcb);
835		ap->dtad_kind = DTRACEACT_LIBACT;
836	}
837
838	ap->dtad_arg = DT_ACT_SETOPT;
839}
840
841/*ARGSUSED*/
842static void
843dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
844    dt_node_t *dnp, dtrace_actkind_t kind)
845{
846	assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
847	    kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
848	    kind == DTRACEACT_UADDR);
849
850	dt_cg(yypcb, dnp);
851	ap->dtad_difo = dt_as(yypcb);
852	ap->dtad_kind = kind;
853	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
854}
855
856static void
857dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
858    dtrace_actkind_t kind)
859{
860	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
861	dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
862}
863
864/*ARGSUSED*/
865static void
866dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
867{
868	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
869
870	/*
871	 * Library actions need a DIFO that serves as an argument.  As
872	 * ftruncate() doesn't take an argument, we generate the constant 0
873	 * in a DIFO; this constant will be ignored when the ftruncate() is
874	 * processed.
875	 */
876	dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
877	ap->dtad_arg = DT_ACT_FTRUNCATE;
878}
879
880/*ARGSUSED*/
881static void
882dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
883{
884	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
885
886	ap->dtad_kind = DTRACEACT_STOP;
887	ap->dtad_arg = 0;
888}
889
890/*ARGSUSED*/
891static void
892dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
893{
894	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
895
896	ap->dtad_kind = DTRACEACT_BREAKPOINT;
897	ap->dtad_arg = 0;
898}
899
900/*ARGSUSED*/
901static void
902dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
903{
904	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
905
906	ap->dtad_kind = DTRACEACT_PANIC;
907	ap->dtad_arg = 0;
908}
909
910static void
911dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
912{
913	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
914
915	dt_cg(yypcb, dnp->dn_args);
916	ap->dtad_difo = dt_as(yypcb);
917	ap->dtad_kind = DTRACEACT_CHILL;
918}
919
920static void
921dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
922{
923	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
924
925	dt_cg(yypcb, dnp->dn_args);
926	ap->dtad_difo = dt_as(yypcb);
927	ap->dtad_kind = DTRACEACT_RAISE;
928}
929
930static void
931dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
932{
933	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
934
935	dt_cg(yypcb, dnp->dn_args);
936	ap->dtad_difo = dt_as(yypcb);
937	ap->dtad_kind = DTRACEACT_EXIT;
938	ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
939}
940
941static void
942dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
943{
944	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
945
946	dt_cg(yypcb, dnp->dn_args);
947	ap->dtad_difo = dt_as(yypcb);
948	ap->dtad_kind = DTRACEACT_SPECULATE;
949}
950
951static void
952dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
953{
954	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
955
956	dt_node_t *size = dnp->dn_args;
957	dt_node_t *addr = dnp->dn_args->dn_list;
958
959	char n[DT_TYPE_NAMELEN];
960
961	if (dt_node_is_posconst(size) == 0) {
962		dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
963		    "be a non-zero positive integral constant expression\n");
964	}
965
966	if (dt_node_is_pointer(addr) == 0) {
967		dnerror(addr, D_PRINTM_ADDR,
968		    "printm( ) argument #2 is incompatible with "
969		    "prototype:\n\tprototype: pointer\n"
970		    "\t argument: %s\n",
971		    dt_node_type_name(addr, n, sizeof (n)));
972	}
973
974	dt_cg(yypcb, addr);
975	ap->dtad_difo = dt_as(yypcb);
976	ap->dtad_kind = DTRACEACT_PRINTM;
977
978	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
979	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
980}
981
982static void
983dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
984{
985	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
986
987	dt_node_t *size = dnp->dn_args;
988	dt_node_t *addr = dnp->dn_args->dn_list;
989
990	char n[DT_TYPE_NAMELEN];
991
992	if (dt_node_is_posconst(size) == 0) {
993		dnerror(size, D_PRINTT_SIZE, "printt( ) argument #1 must "
994		    "be a non-zero positive integral constant expression\n");
995	}
996
997	if (addr == NULL || addr->dn_kind != DT_NODE_FUNC ||
998	    addr->dn_ident != dt_idhash_lookup(dtp->dt_globals, "typeref")) {
999		dnerror(addr, D_PRINTT_ADDR,
1000		    "printt( ) argument #2 is incompatible with "
1001		    "prototype:\n\tprototype: typeref()\n"
1002		    "\t argument: %s\n",
1003		    dt_node_type_name(addr, n, sizeof (n)));
1004	}
1005
1006	dt_cg(yypcb, addr);
1007	ap->dtad_difo = dt_as(yypcb);
1008	ap->dtad_kind = DTRACEACT_PRINTT;
1009
1010	ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1011
1012	/*
1013	 * Allow additional buffer space for the data size, type size,
1014	 * type string length and a stab in the dark (32 bytes) for the
1015	 * type string. The type string is part of the typeref() that
1016	 * this action references.
1017	 */
1018	ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1019
1020}
1021
1022static void
1023dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1024{
1025	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1026
1027	dt_cg(yypcb, dnp->dn_args);
1028	ap->dtad_difo = dt_as(yypcb);
1029	ap->dtad_kind = DTRACEACT_COMMIT;
1030}
1031
1032static void
1033dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1034{
1035	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1036
1037	dt_cg(yypcb, dnp->dn_args);
1038	ap->dtad_difo = dt_as(yypcb);
1039	ap->dtad_kind = DTRACEACT_DISCARD;
1040}
1041
1042static void
1043dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1044{
1045	switch (dnp->dn_expr->dn_ident->di_id) {
1046	case DT_ACT_BREAKPOINT:
1047		dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1048		break;
1049	case DT_ACT_CHILL:
1050		dt_action_chill(dtp, dnp->dn_expr, sdp);
1051		break;
1052	case DT_ACT_CLEAR:
1053		dt_action_clear(dtp, dnp->dn_expr, sdp);
1054		break;
1055	case DT_ACT_COMMIT:
1056		dt_action_commit(dtp, dnp->dn_expr, sdp);
1057		break;
1058	case DT_ACT_DENORMALIZE:
1059		dt_action_normalize(dtp, dnp->dn_expr, sdp);
1060		break;
1061	case DT_ACT_DISCARD:
1062		dt_action_discard(dtp, dnp->dn_expr, sdp);
1063		break;
1064	case DT_ACT_EXIT:
1065		dt_action_exit(dtp, dnp->dn_expr, sdp);
1066		break;
1067	case DT_ACT_FREOPEN:
1068		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1069		break;
1070	case DT_ACT_FTRUNCATE:
1071		dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1072		break;
1073	case DT_ACT_MOD:
1074		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1075		break;
1076	case DT_ACT_NORMALIZE:
1077		dt_action_normalize(dtp, dnp->dn_expr, sdp);
1078		break;
1079	case DT_ACT_PANIC:
1080		dt_action_panic(dtp, dnp->dn_expr, sdp);
1081		break;
1082	case DT_ACT_PRINTA:
1083		dt_action_printa(dtp, dnp->dn_expr, sdp);
1084		break;
1085	case DT_ACT_PRINTF:
1086		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1087		break;
1088	case DT_ACT_PRINTM:
1089		dt_action_printm(dtp, dnp->dn_expr, sdp);
1090		break;
1091	case DT_ACT_PRINTT:
1092		dt_action_printt(dtp, dnp->dn_expr, sdp);
1093		break;
1094	case DT_ACT_RAISE:
1095		dt_action_raise(dtp, dnp->dn_expr, sdp);
1096		break;
1097	case DT_ACT_SETOPT:
1098		dt_action_setopt(dtp, dnp->dn_expr, sdp);
1099		break;
1100	case DT_ACT_SPECULATE:
1101		dt_action_speculate(dtp, dnp->dn_expr, sdp);
1102		break;
1103	case DT_ACT_STACK:
1104		dt_action_stack(dtp, dnp->dn_expr, sdp);
1105		break;
1106	case DT_ACT_STOP:
1107		dt_action_stop(dtp, dnp->dn_expr, sdp);
1108		break;
1109	case DT_ACT_SYM:
1110		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1111		break;
1112	case DT_ACT_SYSTEM:
1113		dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1114		break;
1115	case DT_ACT_TRACE:
1116		dt_action_trace(dtp, dnp->dn_expr, sdp);
1117		break;
1118	case DT_ACT_TRACEMEM:
1119		dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1120		break;
1121	case DT_ACT_TRUNC:
1122		dt_action_trunc(dtp, dnp->dn_expr, sdp);
1123		break;
1124	case DT_ACT_UADDR:
1125		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1126		break;
1127	case DT_ACT_UMOD:
1128		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1129		break;
1130	case DT_ACT_USYM:
1131		dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1132		break;
1133	case DT_ACT_USTACK:
1134	case DT_ACT_JSTACK:
1135		dt_action_ustack(dtp, dnp->dn_expr, sdp);
1136		break;
1137	default:
1138		dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1139		    "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1140	}
1141}
1142
1143static void
1144dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1145{
1146	dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1147
1148	dt_cg(yypcb, dnp->dn_expr);
1149	ap->dtad_difo = dt_as(yypcb);
1150	ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1151	ap->dtad_kind = DTRACEACT_DIFEXPR;
1152}
1153
1154static void
1155dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1156{
1157	dt_ident_t *aid, *fid;
1158	dt_node_t *anp, *incr = NULL;
1159	dtrace_actdesc_t *ap;
1160	uint_t n = 1, argmax;
1161	uint64_t arg = 0;
1162
1163	/*
1164	 * If the aggregation has no aggregating function applied to it, then
1165	 * this statement has no effect.  Flag this as a programming error.
1166	 */
1167	if (dnp->dn_aggfun == NULL) {
1168		dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1169		    dnp->dn_ident->di_name);
1170	}
1171
1172	aid = dnp->dn_ident;
1173	fid = dnp->dn_aggfun->dn_ident;
1174
1175	if (dnp->dn_aggfun->dn_args != NULL &&
1176	    dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1177		dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1178		    "be of scalar type\n", fid->di_name);
1179	}
1180
1181	/*
1182	 * The ID of the aggregation itself is implicitly recorded as the first
1183	 * member of each aggregation tuple so we can distinguish them later.
1184	 */
1185	ap = dt_stmt_action(dtp, sdp);
1186	dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1187
1188	for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1189		ap = dt_stmt_action(dtp, sdp);
1190		n++;
1191
1192		if (anp->dn_kind == DT_NODE_FUNC) {
1193			if (anp->dn_ident->di_id == DT_ACT_STACK) {
1194				dt_action_stack_args(dtp, ap, anp->dn_args);
1195				continue;
1196			}
1197
1198			if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1199			    anp->dn_ident->di_id == DT_ACT_JSTACK) {
1200				dt_action_ustack_args(dtp, ap, anp);
1201				continue;
1202			}
1203
1204			switch (anp->dn_ident->di_id) {
1205			case DT_ACT_UADDR:
1206				dt_action_symmod_args(dtp, ap,
1207				    anp->dn_args, DTRACEACT_UADDR);
1208				continue;
1209
1210			case DT_ACT_USYM:
1211				dt_action_symmod_args(dtp, ap,
1212				    anp->dn_args, DTRACEACT_USYM);
1213				continue;
1214
1215			case DT_ACT_UMOD:
1216				dt_action_symmod_args(dtp, ap,
1217				    anp->dn_args, DTRACEACT_UMOD);
1218				continue;
1219
1220			case DT_ACT_SYM:
1221				dt_action_symmod_args(dtp, ap,
1222				    anp->dn_args, DTRACEACT_SYM);
1223				continue;
1224
1225			case DT_ACT_MOD:
1226				dt_action_symmod_args(dtp, ap,
1227				    anp->dn_args, DTRACEACT_MOD);
1228				continue;
1229
1230			default:
1231				break;
1232			}
1233		}
1234
1235		dt_cg(yypcb, anp);
1236		ap->dtad_difo = dt_as(yypcb);
1237		ap->dtad_kind = DTRACEACT_DIFEXPR;
1238	}
1239
1240	if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1241		/*
1242		 * For linear quantization, we have between two and four
1243		 * arguments in addition to the expression:
1244		 *
1245		 *    arg1 => Base value
1246		 *    arg2 => Limit value
1247		 *    arg3 => Quantization level step size (defaults to 1)
1248		 *    arg4 => Quantization increment value (defaults to 1)
1249		 */
1250		dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1251		dt_node_t *arg2 = arg1->dn_list;
1252		dt_node_t *arg3 = arg2->dn_list;
1253		dt_idsig_t *isp;
1254		uint64_t nlevels, step = 1, oarg;
1255		int64_t baseval, limitval;
1256
1257		if (arg1->dn_kind != DT_NODE_INT) {
1258			dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1259			    "argument #1 must be an integer constant\n");
1260		}
1261
1262		baseval = (int64_t)arg1->dn_value;
1263
1264		if (baseval < INT32_MIN || baseval > INT32_MAX) {
1265			dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1266			    "argument #1 must be a 32-bit quantity\n");
1267		}
1268
1269		if (arg2->dn_kind != DT_NODE_INT) {
1270			dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1271			    "argument #2 must be an integer constant\n");
1272		}
1273
1274		limitval = (int64_t)arg2->dn_value;
1275
1276		if (limitval < INT32_MIN || limitval > INT32_MAX) {
1277			dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1278			    "argument #2 must be a 32-bit quantity\n");
1279		}
1280
1281		if (limitval < baseval) {
1282			dnerror(dnp, D_LQUANT_MISMATCH,
1283			    "lquantize( ) base (argument #1) must be less "
1284			    "than limit (argument #2)\n");
1285		}
1286
1287		if (arg3 != NULL) {
1288			if (!dt_node_is_posconst(arg3)) {
1289				dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1290				    "argument #3 must be a non-zero positive "
1291				    "integer constant\n");
1292			}
1293
1294			if ((step = arg3->dn_value) > UINT16_MAX) {
1295				dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1296				    "argument #3 must be a 16-bit quantity\n");
1297			}
1298		}
1299
1300		nlevels = (limitval - baseval) / step;
1301
1302		if (nlevels == 0) {
1303			dnerror(dnp, D_LQUANT_STEPLARGE,
1304			    "lquantize( ) step (argument #3) too large: must "
1305			    "have at least one quantization level\n");
1306		}
1307
1308		if (nlevels > UINT16_MAX) {
1309			dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1310			    "(argument #3) too small: number of quantization "
1311			    "levels must be a 16-bit quantity\n");
1312		}
1313
1314		arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1315		    (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1316		    ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1317		    DTRACE_LQUANTIZE_BASEMASK);
1318
1319		assert(arg != 0);
1320
1321		isp = (dt_idsig_t *)aid->di_data;
1322
1323		if (isp->dis_auxinfo == 0) {
1324			/*
1325			 * This is the first time we've seen an lquantize()
1326			 * for this aggregation; we'll store our argument
1327			 * as the auxiliary signature information.
1328			 */
1329			isp->dis_auxinfo = arg;
1330		} else if ((oarg = isp->dis_auxinfo) != arg) {
1331			/*
1332			 * If we have seen this lquantize() before and the
1333			 * argument doesn't match the original argument, pick
1334			 * the original argument apart to concisely report the
1335			 * mismatch.
1336			 */
1337			int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1338			int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1339			int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1340
1341			if (obaseval != baseval) {
1342				dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1343				    "base (argument #1) doesn't match previous "
1344				    "declaration: expected %d, found %d\n",
1345				    obaseval, (int)baseval);
1346			}
1347
1348			if (onlevels * ostep != nlevels * step) {
1349				dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1350				    "limit (argument #2) doesn't match previous"
1351				    " declaration: expected %d, found %d\n",
1352				    obaseval + onlevels * ostep,
1353				    (int)baseval + (int)nlevels * (int)step);
1354			}
1355
1356			if (ostep != step) {
1357				dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1358				    "step (argument #3) doesn't match previous "
1359				    "declaration: expected %d, found %d\n",
1360				    ostep, (int)step);
1361			}
1362
1363			/*
1364			 * We shouldn't be able to get here -- one of the
1365			 * parameters must be mismatched if the arguments
1366			 * didn't match.
1367			 */
1368			assert(0);
1369		}
1370
1371		incr = arg3 != NULL ? arg3->dn_list : NULL;
1372		argmax = 5;
1373	}
1374
1375	if (fid->di_id == DTRACEAGG_QUANTIZE) {
1376		incr = dnp->dn_aggfun->dn_args->dn_list;
1377		argmax = 2;
1378	}
1379
1380	if (incr != NULL) {
1381		if (!dt_node_is_scalar(incr)) {
1382			dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1383			    "(argument #%d) must be of scalar type\n",
1384			    fid->di_name, argmax);
1385		}
1386
1387		if ((anp = incr->dn_list) != NULL) {
1388			int argc = argmax;
1389
1390			for (; anp != NULL; anp = anp->dn_list)
1391				argc++;
1392
1393			dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1394			    "mismatch: %d args passed, at most %d expected",
1395			    fid->di_name, argc, argmax);
1396		}
1397
1398		ap = dt_stmt_action(dtp, sdp);
1399		n++;
1400
1401		dt_cg(yypcb, incr);
1402		ap->dtad_difo = dt_as(yypcb);
1403		ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1404		ap->dtad_kind = DTRACEACT_DIFEXPR;
1405	}
1406
1407	assert(sdp->dtsd_aggdata == NULL);
1408	sdp->dtsd_aggdata = aid;
1409
1410	ap = dt_stmt_action(dtp, sdp);
1411	assert(fid->di_kind == DT_IDENT_AGGFUNC);
1412	assert(DTRACEACT_ISAGG(fid->di_id));
1413	ap->dtad_kind = fid->di_id;
1414	ap->dtad_ntuple = n;
1415	ap->dtad_arg = arg;
1416
1417	if (dnp->dn_aggfun->dn_args != NULL) {
1418		dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1419		ap->dtad_difo = dt_as(yypcb);
1420	}
1421}
1422
1423static void
1424dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1425{
1426	dtrace_ecbdesc_t *edp;
1427	dtrace_stmtdesc_t *sdp;
1428	dt_node_t *dnp;
1429
1430	yylineno = pnp->dn_line;
1431	dt_setcontext(dtp, pnp->dn_desc);
1432	(void) dt_node_cook(cnp, DT_IDFLG_REF);
1433
1434	if (DT_TREEDUMP_PASS(dtp, 2))
1435		dt_node_printr(cnp, stderr, 0);
1436
1437	if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1438		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1439
1440	assert(yypcb->pcb_ecbdesc == NULL);
1441	yypcb->pcb_ecbdesc = edp;
1442
1443	if (cnp->dn_pred != NULL) {
1444		dt_cg(yypcb, cnp->dn_pred);
1445		edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1446	}
1447
1448	if (cnp->dn_acts == NULL) {
1449		dt_stmt_append(dt_stmt_create(dtp, edp,
1450		    cnp->dn_ctxattr, _dtrace_defattr), cnp);
1451	}
1452
1453	for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1454		assert(yypcb->pcb_stmt == NULL);
1455		sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1456
1457		switch (dnp->dn_kind) {
1458		case DT_NODE_DEXPR:
1459			if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1460				dt_compile_agg(dtp, dnp->dn_expr, sdp);
1461			else
1462				dt_compile_exp(dtp, dnp, sdp);
1463			break;
1464		case DT_NODE_DFUNC:
1465			dt_compile_fun(dtp, dnp, sdp);
1466			break;
1467		case DT_NODE_AGG:
1468			dt_compile_agg(dtp, dnp, sdp);
1469			break;
1470		default:
1471			dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1472			    "%u is not a valid statement\n", dnp->dn_kind);
1473		}
1474
1475		assert(yypcb->pcb_stmt == sdp);
1476		dt_stmt_append(sdp, dnp);
1477	}
1478
1479	assert(yypcb->pcb_ecbdesc == edp);
1480	dt_ecbdesc_release(dtp, edp);
1481	dt_endcontext(dtp);
1482	yypcb->pcb_ecbdesc = NULL;
1483}
1484
1485static void
1486dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1487{
1488	dt_node_t *pnp;
1489
1490	for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1491		dt_compile_one_clause(dtp, cnp, pnp);
1492}
1493
1494static void
1495dt_compile_xlator(dt_node_t *dnp)
1496{
1497	dt_xlator_t *dxp = dnp->dn_xlator;
1498	dt_node_t *mnp;
1499
1500	for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1501		assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1502		dt_cg(yypcb, mnp);
1503		dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1504	}
1505}
1506
1507void
1508dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1509{
1510	const dtrace_pattr_t *pap;
1511	dt_probe_t *prp;
1512	dt_provider_t *pvp;
1513	dt_ident_t *idp;
1514	char attrstr[8];
1515	int err;
1516
1517	/*
1518	 * Both kernel and pid based providers are allowed to have names
1519	 * ending with what could be interpreted as a number. We assume it's
1520	 * a pid and that we may need to dynamically create probes for
1521	 * that process if:
1522	 *
1523	 * (1) The provider doesn't exist, or,
1524	 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1525	 *
1526	 * On an error, dt_pid_create_probes() will set the error message
1527	 * and tag -- we just have to longjmp() out of here.
1528	 */
1529	if (isdigit((unsigned char)pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1530	    ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1531	    pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1532	    dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1533		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1534	}
1535
1536	/*
1537	 * Call dt_probe_info() to get the probe arguments and attributes.  If
1538	 * a representative probe is found, set 'pap' to the probe provider's
1539	 * attributes.  Otherwise set 'pap' to default Unstable attributes.
1540	 */
1541	if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1542		pap = &_dtrace_prvdesc;
1543		err = dtrace_errno(dtp);
1544		bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1545		yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1546		yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1547	} else {
1548		pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1549		err = 0;
1550	}
1551
1552	if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1553		xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1554		    "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1555		    pdp->dtpd_func, pdp->dtpd_name);
1556	}
1557
1558	if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1559		xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1560
1561	dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1562	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1563	    pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1564	    attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1565
1566	/*
1567	 * Reset the stability attributes of D global variables that vary
1568	 * based on the attributes of the provider and context itself.
1569	 */
1570	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1571		idp->di_attr = pap->dtpa_provider;
1572	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1573		idp->di_attr = pap->dtpa_mod;
1574	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1575		idp->di_attr = pap->dtpa_func;
1576	if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1577		idp->di_attr = pap->dtpa_name;
1578	if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1579		idp->di_attr = pap->dtpa_args;
1580
1581	yypcb->pcb_pdesc = pdp;
1582	yypcb->pcb_probe = prp;
1583}
1584
1585/*
1586 * Reset context-dependent variables and state at the end of cooking a D probe
1587 * definition clause.  This ensures that external declarations between clauses
1588 * do not reference any stale context-dependent data from the previous clause.
1589 */
1590void
1591dt_endcontext(dtrace_hdl_t *dtp)
1592{
1593	static const char *const cvars[] = {
1594		"probeprov", "probemod", "probefunc", "probename", "args", NULL
1595	};
1596
1597	dt_ident_t *idp;
1598	int i;
1599
1600	for (i = 0; cvars[i] != NULL; i++) {
1601		if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1602			idp->di_attr = _dtrace_defattr;
1603	}
1604
1605	yypcb->pcb_pdesc = NULL;
1606	yypcb->pcb_probe = NULL;
1607}
1608
1609static int
1610dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1611{
1612	if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1613		dt_idhash_delete(dhp, idp);
1614
1615	return (0);
1616}
1617
1618/*
1619 * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1620 * any identifiers or translators that have been previously defined as bound to
1621 * a version greater than the specified version.  Therefore, in our current
1622 * version implementation, establishing a binding is a one-way transformation.
1623 * In addition, no versioning is currently provided for types as our .d library
1624 * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1625 * for our exclusive use.  If required, type versioning will require more work.
1626 */
1627int
1628dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1629{
1630	char s[DT_VERSION_STRMAX];
1631	dt_xlator_t *dxp, *nxp;
1632
1633	if (v > dtp->dt_vmax)
1634		return (dt_set_errno(dtp, EDT_VERSREDUCED));
1635	else if (v == dtp->dt_vmax)
1636		return (0); /* no reduction necessary */
1637
1638	dt_dprintf("reducing api version to %s\n",
1639	    dt_version_num2str(v, s, sizeof (s)));
1640
1641	dtp->dt_vmax = v;
1642
1643	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1644		nxp = dt_list_next(dxp);
1645		if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1646		    (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1647			dt_list_delete(&dtp->dt_xlators, dxp);
1648	}
1649
1650	(void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1651	(void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1652	(void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1653	(void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1654
1655	return (0);
1656}
1657
1658/*
1659 * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1660 * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
1661 * here to simplify the code by leveraging file descriptor inheritance.
1662 */
1663static FILE *
1664dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1665{
1666	int argc = dtp->dt_cpp_argc;
1667	char **argv = malloc(sizeof (char *) * (argc + 5));
1668	FILE *ofp = tmpfile();
1669
1670#if defined(sun)
1671	char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1672#endif
1673	char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1674
1675	struct sigaction act, oact;
1676	sigset_t mask, omask;
1677
1678	int wstat, estat;
1679	pid_t pid;
1680#if defined(sun)
1681	off64_t off;
1682#else
1683	off_t off = 0;
1684#endif
1685	int c;
1686
1687	if (argv == NULL || ofp == NULL) {
1688		(void) dt_set_errno(dtp, errno);
1689		goto err;
1690	}
1691
1692	/*
1693	 * If the input is a seekable file, see if it is an interpreter file.
1694	 * If we see #!, seek past the first line because cpp will choke on it.
1695	 * We start cpp just prior to the \n at the end of this line so that
1696	 * it still sees the newline, ensuring that #line values are correct.
1697	 */
1698	if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1699		if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1700			for (off += 2; c != '\n'; off++) {
1701				if ((c = fgetc(ifp)) == EOF)
1702					break;
1703			}
1704			if (c == '\n')
1705				off--; /* start cpp just prior to \n */
1706		}
1707		(void) fflush(ifp);
1708		(void) fseeko64(ifp, off, SEEK_SET);
1709	}
1710
1711#if defined(sun)
1712	(void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1713	(void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1714#endif
1715
1716	bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1717
1718	(void) snprintf(verdef, sizeof (verdef),
1719	    "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1720	argv[argc++] = verdef;
1721
1722#if defined(sun)
1723	switch (dtp->dt_stdcmode) {
1724	case DT_STDC_XA:
1725	case DT_STDC_XT:
1726		argv[argc++] = "-D__STDC__=0";
1727		break;
1728	case DT_STDC_XC:
1729		argv[argc++] = "-D__STDC__=1";
1730		break;
1731	}
1732
1733	argv[argc++] = ipath;
1734	argv[argc++] = opath;
1735#else
1736	argv[argc++] = __UNCONST("-P");
1737#endif
1738	argv[argc] = NULL;
1739
1740	/*
1741	 * libdtrace must be able to be embedded in other programs that may
1742	 * include application-specific signal handlers.  Therefore, if we
1743	 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1744	 * that could confuse the containing application.  To do this,
1745	 * we block SIGCHLD and reset its disposition to SIG_DFL.
1746	 * We restore our signal state once we are done.
1747	 */
1748	(void) sigemptyset(&mask);
1749	(void) sigaddset(&mask, SIGCHLD);
1750	(void) sigprocmask(SIG_BLOCK, &mask, &omask);
1751
1752	bzero(&act, sizeof (act));
1753	act.sa_handler = SIG_DFL;
1754	(void) sigaction(SIGCHLD, &act, &oact);
1755
1756	if ((pid = fork1()) == -1) {
1757		(void) sigaction(SIGCHLD, &oact, NULL);
1758		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1759		(void) dt_set_errno(dtp, EDT_CPPFORK);
1760		goto err;
1761	}
1762
1763	if (pid == 0) {
1764#if !defined(sun)
1765		if (isatty(fileno(ifp)) == 0)
1766			lseek(fileno(ifp), off, SEEK_SET);
1767		dup2(fileno(ifp), 0);
1768		dup2(fileno(ofp), 1);
1769#endif
1770		(void) execvp(dtp->dt_cpp_path, argv);
1771		_exit(errno == ENOENT ? 127 : 126);
1772	}
1773
1774	do {
1775		dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1776		    (int)pid);
1777	} while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1778
1779	(void) sigaction(SIGCHLD, &oact, NULL);
1780	(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1781
1782	dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1783	estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1784
1785	if (estat != 0) {
1786		switch (estat) {
1787		case 126:
1788			(void) dt_set_errno(dtp, EDT_CPPEXEC);
1789			break;
1790		case 127:
1791			(void) dt_set_errno(dtp, EDT_CPPENT);
1792			break;
1793		default:
1794			(void) dt_set_errno(dtp, EDT_CPPERR);
1795		}
1796		goto err;
1797	}
1798
1799	free(argv);
1800	(void) fflush(ofp);
1801	(void) fseek(ofp, 0, SEEK_SET);
1802	return (ofp);
1803
1804err:
1805	free(argv);
1806	(void) fclose(ofp);
1807	return (NULL);
1808}
1809
1810static void
1811dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1812{
1813	va_list ap;
1814
1815	va_start(ap, format);
1816	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1817	va_end(ap);
1818}
1819
1820int
1821dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1822{
1823	dt_lib_depend_t *dld;
1824	const char *end;
1825
1826	assert(arg != NULL);
1827
1828	if ((end = strrchr(arg, '/')) == NULL)
1829		return (dt_set_errno(dtp, EINVAL));
1830
1831	if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1832		return (-1);
1833
1834	if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
1835		dt_free(dtp, dld);
1836		return (-1);
1837	}
1838
1839	(void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
1840	if ((dld->dtld_library = strdup(arg)) == NULL) {
1841		dt_free(dtp, dld->dtld_libpath);
1842		dt_free(dtp, dld);
1843		return (dt_set_errno(dtp, EDT_NOMEM));
1844	}
1845
1846	dt_list_append(dlp, dld);
1847	return (0);
1848}
1849
1850dt_lib_depend_t *
1851dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1852{
1853	dt_lib_depend_t *dldn;
1854
1855	for (dldn = dt_list_next(dld); dldn != NULL;
1856	    dldn = dt_list_next(dldn)) {
1857		if (strcmp(dldn->dtld_library, arg) == 0)
1858			return (dldn);
1859	}
1860
1861	return (NULL);
1862}
1863
1864/*
1865 * Go through all the library files, and, if any library dependencies exist for
1866 * that file, add it to that node's list of dependents. The result of this
1867 * will be a graph which can then be topologically sorted to produce a
1868 * compilation order.
1869 */
1870static int
1871dt_lib_build_graph(dtrace_hdl_t *dtp)
1872{
1873	dt_lib_depend_t *dld, *dpld;
1874
1875	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1876	    dld = dt_list_next(dld)) {
1877		char *library = dld->dtld_library;
1878
1879		for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
1880		    dpld = dt_list_next(dpld)) {
1881			dt_lib_depend_t *dlda;
1882
1883			if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
1884			    dpld->dtld_library)) == NULL) {
1885				dt_lib_depend_error(dtp,
1886				    "Invalid library dependency in %s: %s\n",
1887				    dld->dtld_library, dpld->dtld_library);
1888
1889				return (dt_set_errno(dtp, EDT_COMPILER));
1890			}
1891
1892			if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
1893			    library)) != 0) {
1894				return (-1); /* preserve dt_errno */
1895			}
1896		}
1897	}
1898	return (0);
1899}
1900
1901static int
1902dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
1903{
1904	dt_lib_depend_t *dpld, *dlda, *new;
1905
1906	dld->dtld_start = ++(*count);
1907
1908	for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
1909	    dpld = dt_list_next(dpld)) {
1910		dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
1911		    dpld->dtld_library);
1912		assert(dlda != NULL);
1913
1914		if (dlda->dtld_start == 0 &&
1915		    dt_topo_sort(dtp, dlda, count) == -1)
1916			return (-1);
1917	}
1918
1919	if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1920		return (-1);
1921
1922	if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
1923		dt_free(dtp, new);
1924		return (dt_set_errno(dtp, EDT_NOMEM));
1925	}
1926
1927	new->dtld_start = dld->dtld_start;
1928	new->dtld_finish = dld->dtld_finish = ++(*count);
1929	dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
1930
1931	dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
1932	    new->dtld_start, new->dtld_finish);
1933
1934	return (0);
1935}
1936
1937static int
1938dt_lib_depend_sort(dtrace_hdl_t *dtp)
1939{
1940	dt_lib_depend_t *dld, *dpld, *dlda;
1941	int count = 0;
1942
1943	if (dt_lib_build_graph(dtp) == -1)
1944		return (-1); /* preserve dt_errno */
1945
1946	/*
1947	 * Perform a topological sort of the graph that hangs off
1948	 * dtp->dt_lib_dep. The result of this process will be a
1949	 * dependency ordered list located at dtp->dt_lib_dep_sorted.
1950	 */
1951	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1952	    dld = dt_list_next(dld)) {
1953		if (dld->dtld_start == 0 &&
1954		    dt_topo_sort(dtp, dld, &count) == -1)
1955			return (-1); /* preserve dt_errno */;
1956	}
1957
1958	/*
1959	 * Check the graph for cycles. If an ancestor's finishing time is
1960	 * less than any of its dependent's finishing times then a back edge
1961	 * exists in the graph and this is a cycle.
1962	 */
1963	for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1964	    dld = dt_list_next(dld)) {
1965		for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
1966		    dpld = dt_list_next(dpld)) {
1967			dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
1968			    dpld->dtld_library);
1969			assert(dlda != NULL);
1970
1971			if (dlda->dtld_finish > dld->dtld_finish) {
1972				dt_lib_depend_error(dtp,
1973				    "Cyclic dependency detected: %s => %s\n",
1974				    dld->dtld_library, dpld->dtld_library);
1975
1976				return (dt_set_errno(dtp, EDT_COMPILER));
1977			}
1978		}
1979	}
1980
1981	return (0);
1982}
1983
1984static void
1985dt_lib_depend_free(dtrace_hdl_t *dtp)
1986{
1987	dt_lib_depend_t *dld, *dlda;
1988
1989	while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
1990		while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
1991			dt_list_delete(&dld->dtld_dependencies, dlda);
1992			dt_free(dtp, dlda->dtld_library);
1993			dt_free(dtp, dlda->dtld_libpath);
1994			dt_free(dtp, dlda);
1995		}
1996		while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
1997			dt_list_delete(&dld->dtld_dependents, dlda);
1998			dt_free(dtp, dlda->dtld_library);
1999			dt_free(dtp, dlda->dtld_libpath);
2000			dt_free(dtp, dlda);
2001		}
2002		dt_list_delete(&dtp->dt_lib_dep, dld);
2003		dt_free(dtp, dld->dtld_library);
2004		dt_free(dtp, dld->dtld_libpath);
2005		dt_free(dtp, dld);
2006	}
2007
2008	while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2009		dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2010		dt_free(dtp, dld->dtld_library);
2011		dt_free(dtp, dld);
2012	}
2013}
2014
2015
2016/*
2017 * Open all of the .d library files found in the specified directory and
2018 * compile each one in topological order to cache its inlines and translators,
2019 * etc.  We silently ignore any missing directories and other files found
2020 * therein. We only fail (and thereby fail dt_load_libs()) if we fail to
2021 * compile a library and the error is something other than #pragma D depends_on.
2022 * Dependency errors are silently ignored to permit a library directory to
2023 * contain libraries which may not be accessible depending on our privileges.
2024 */
2025static int
2026dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2027{
2028	struct dirent *dp;
2029	const char *p;
2030	DIR *dirp;
2031
2032	char fname[PATH_MAX];
2033	dtrace_prog_t *pgp;
2034	FILE *fp;
2035	void *rv;
2036	dt_lib_depend_t *dld;
2037
2038	if ((dirp = opendir(path)) == NULL) {
2039		dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2040		return (0);
2041	}
2042
2043	/* First, parse each file for library dependencies. */
2044	while ((dp = readdir(dirp)) != NULL) {
2045		if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2046			continue; /* skip any filename not ending in .d */
2047
2048		(void) snprintf(fname, sizeof (fname),
2049		    "%s/%s", path, dp->d_name);
2050
2051		if ((fp = fopen(fname, "r")) == NULL) {
2052			dt_dprintf("skipping library %s: %s\n",
2053			    fname, strerror(errno));
2054			continue;
2055		}
2056
2057		dtp->dt_filetag = fname;
2058		if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
2059			goto err;
2060
2061		rv = dt_compile(dtp, DT_CTX_DPROG,
2062		    DTRACE_PROBESPEC_NAME, NULL,
2063		    DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2064
2065		if (rv != NULL && dtp->dt_errno &&
2066		    (dtp->dt_errno != EDT_COMPILER ||
2067		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2068			goto err;
2069
2070		if (dtp->dt_errno)
2071			dt_dprintf("error parsing library %s: %s\n",
2072			    fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2073
2074		(void) fclose(fp);
2075		dtp->dt_filetag = NULL;
2076	}
2077
2078	(void) closedir(dirp);
2079	/*
2080	 * Finish building the graph containing the library dependencies
2081	 * and perform a topological sort to generate an ordered list
2082	 * for compilation.
2083	 */
2084	if (dt_lib_depend_sort(dtp) == -1)
2085		goto err;
2086
2087	for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2088	    dld = dt_list_next(dld)) {
2089
2090		if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2091			dt_dprintf("skipping library %s: %s\n",
2092			    dld->dtld_library, strerror(errno));
2093			continue;
2094		}
2095
2096		dtp->dt_filetag = dld->dtld_library;
2097		pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2098		(void) fclose(fp);
2099		dtp->dt_filetag = NULL;
2100
2101		if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2102		    dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2103			goto err;
2104
2105		if (pgp == NULL) {
2106			dt_dprintf("skipping library %s: %s\n",
2107			    dld->dtld_library,
2108			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2109		} else {
2110			dld->dtld_loaded = B_TRUE;
2111			dt_program_destroy(dtp, pgp);
2112		}
2113	}
2114
2115	dt_lib_depend_free(dtp);
2116	return (0);
2117
2118err:
2119	dt_lib_depend_free(dtp);
2120	return (-1); /* preserve dt_errno */
2121}
2122
2123/*
2124 * Load the contents of any appropriate DTrace .d library files.  These files
2125 * contain inlines and translators that will be cached by the compiler.  We
2126 * defer this activity until the first compile to permit libdtrace clients to
2127 * add their own library directories and so that we can properly report errors.
2128 */
2129static int
2130dt_load_libs(dtrace_hdl_t *dtp)
2131{
2132	dt_dirpath_t *dirp;
2133
2134	if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2135		return (0); /* libraries already processed */
2136
2137	dtp->dt_cflags |= DTRACE_C_NOLIBS;
2138
2139	for (dirp = dt_list_next(&dtp->dt_lib_path);
2140	    dirp != NULL; dirp = dt_list_next(dirp)) {
2141		if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2142			dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2143			return (-1); /* errno is set for us */
2144		}
2145	}
2146
2147	return (0);
2148}
2149
2150static void *
2151dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2152    uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2153{
2154	dt_node_t *dnp;
2155	dt_decl_t *ddp;
2156	dt_pcb_t pcb;
2157	void *rv = NULL;
2158	int err;
2159
2160	if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2161		(void) dt_set_errno(dtp, EINVAL);
2162		return (NULL);
2163	}
2164
2165	if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2166		return (NULL); /* errno is set for us */
2167
2168	(void) ctf_discard(dtp->dt_cdefs->dm_ctfp);
2169	(void) ctf_discard(dtp->dt_ddefs->dm_ctfp);
2170
2171	(void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2172	(void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2173
2174	if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2175		return (NULL); /* errno is set for us */
2176
2177	dt_pcb_push(dtp, &pcb);
2178
2179	pcb.pcb_fileptr = fp;
2180	pcb.pcb_string = s;
2181	pcb.pcb_strptr = s;
2182	pcb.pcb_strlen = s ? strlen(s) : 0;
2183	pcb.pcb_sargc = argc;
2184	pcb.pcb_sargv = argv;
2185	pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2186	pcb.pcb_pspec = pspec;
2187	pcb.pcb_cflags = dtp->dt_cflags | cflags;
2188	pcb.pcb_amin = dtp->dt_amin;
2189	pcb.pcb_yystate = -1;
2190	pcb.pcb_context = context;
2191	pcb.pcb_token = context;
2192
2193	if (context != DT_CTX_DPROG)
2194		yybegin(YYS_EXPR);
2195	else if (cflags & DTRACE_C_CTL)
2196		yybegin(YYS_CONTROL);
2197	else
2198		yybegin(YYS_CLAUSE);
2199
2200	if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2201		goto out;
2202
2203	if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2204		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2205
2206	yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2207	yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2208	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2209
2210	if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2211		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2212
2213	/*
2214	 * Invoke the parser to evaluate the D source code.  If any errors
2215	 * occur during parsing, an error function will be called and we
2216	 * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
2217	 * we optionally display the parse tree if debugging is enabled.
2218	 */
2219	if (yyparse() != 0 || yypcb->pcb_root == NULL)
2220		xyerror(D_EMPTY, "empty D program translation unit\n");
2221
2222	yybegin(YYS_DONE);
2223
2224	if (cflags & DTRACE_C_CTL)
2225		goto out;
2226
2227	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2228		dt_node_printr(yypcb->pcb_root, stderr, 0);
2229
2230	if (yypcb->pcb_pragmas != NULL)
2231		(void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2232
2233	if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2234	    !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2235		xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2236		    "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2237	}
2238
2239	/*
2240	 * If we have successfully created a parse tree for a D program, loop
2241	 * over the clauses and actions and instantiate the corresponding
2242	 * libdtrace program.  If we are parsing a D expression, then we
2243	 * simply run the code generator and assembler on the resulting tree.
2244	 */
2245	switch (context) {
2246	case DT_CTX_DPROG:
2247		assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2248
2249		if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2250		    !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2251			xyerror(D_EMPTY, "empty D program translation unit\n");
2252
2253		if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2254			longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2255
2256		for (; dnp != NULL; dnp = dnp->dn_list) {
2257			switch (dnp->dn_kind) {
2258			case DT_NODE_CLAUSE:
2259				dt_compile_clause(dtp, dnp);
2260				break;
2261			case DT_NODE_XLATOR:
2262				if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2263					dt_compile_xlator(dnp);
2264				break;
2265			case DT_NODE_PROVIDER:
2266				(void) dt_node_cook(dnp, DT_IDFLG_REF);
2267				break;
2268			}
2269		}
2270
2271		yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2272		yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2273		yypcb->pcb_asxrefs = NULL;
2274		yypcb->pcb_asxreflen = 0;
2275
2276		rv = yypcb->pcb_prog;
2277		break;
2278
2279	case DT_CTX_DEXPR:
2280		(void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2281		dt_cg(yypcb, yypcb->pcb_root);
2282		rv = dt_as(yypcb);
2283		break;
2284
2285	case DT_CTX_DTYPE:
2286		ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2287		err = dt_decl_type(ddp, arg);
2288		dt_decl_free(ddp);
2289
2290		if (err != 0)
2291			longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2292
2293		rv = NULL;
2294		break;
2295	}
2296
2297out:
2298	if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
2299		dt_node_printr(yypcb->pcb_root, stderr, 0);
2300
2301	if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2302	    lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2303	    ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2304		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2305
2306	if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2307	    lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2308	    ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2309		dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2310
2311	if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2312		(void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2313
2314	dt_pcb_pop(dtp, err);
2315	(void) dt_set_errno(dtp, err);
2316	return (err ? NULL : rv);
2317}
2318
2319dtrace_prog_t *
2320dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2321    dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2322{
2323	return (dt_compile(dtp, DT_CTX_DPROG,
2324	    spec, NULL, cflags, argc, argv, NULL, s));
2325}
2326
2327dtrace_prog_t *
2328dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2329    uint_t cflags, int argc, char *const argv[])
2330{
2331	return (dt_compile(dtp, DT_CTX_DPROG,
2332	    DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2333}
2334
2335int
2336dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2337{
2338	(void) dt_compile(dtp, DT_CTX_DTYPE,
2339	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2340	return (dtp->dt_errno ? -1 : 0);
2341}
2342
2343int
2344dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2345{
2346	(void) dt_compile(dtp, DT_CTX_DTYPE,
2347	    DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2348	return (dtp->dt_errno ? -1 : 0);
2349}
2350