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