]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - cddl/contrib/opensolaris/lib/libdtrace/common/dt_cc.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / cddl / contrib / opensolaris / lib / libdtrace / common / dt_cc.c
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, Joyent Inc. All rights reserved.
25  * Copyright (c) 2012 by Delphix. All rights reserved.
26  */
27
28 /*
29  * DTrace D Language Compiler
30  *
31  * The code in this source file implements the main engine for the D language
32  * compiler.  The driver routine for the compiler is dt_compile(), below.  The
33  * compiler operates on either stdio FILEs or in-memory strings as its input
34  * and can produce either dtrace_prog_t structures from a D program or a single
35  * dtrace_difo_t structure from a D expression.  Multiple entry points are
36  * provided as wrappers around dt_compile() for the various input/output pairs.
37  * The compiler itself is implemented across the following source files:
38  *
39  * dt_lex.l - lex scanner
40  * dt_grammar.y - yacc grammar
41  * dt_parser.c - parse tree creation and semantic checking
42  * dt_decl.c - declaration stack processing
43  * dt_xlator.c - D translator lookup and creation
44  * dt_ident.c - identifier and symbol table routines
45  * dt_pragma.c - #pragma processing and D pragmas
46  * dt_printf.c - D printf() and printa() argument checking and processing
47  * dt_cc.c - compiler driver and dtrace_prog_t construction
48  * dt_cg.c - DIF code generator
49  * dt_as.c - DIF assembler
50  * dt_dof.c - dtrace_prog_t -> DOF conversion
51  *
52  * Several other source files provide collections of utility routines used by
53  * these major files.  The compiler itself is implemented in multiple passes:
54  *
55  * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
56  *     and parse tree nodes are constructed using the routines in dt_parser.c.
57  *     This node construction pass is described further in dt_parser.c.
58  *
59  * (2) The parse tree is "cooked" by assigning each clause a context (see the
60  *     routine dt_setcontext(), below) based on its probe description and then
61  *     recursively descending the tree performing semantic checking.  The cook
62  *     routines are also implemented in dt_parser.c and described there.
63  *
64  * (3) For actions that are DIF expression statements, the DIF code generator
65  *     and assembler are invoked to create a finished DIFO for the statement.
66  *
67  * (4) The dtrace_prog_t data structures for the program clauses and actions
68  *     are built, containing pointers to any DIFOs created in step (3).
69  *
70  * (5) The caller invokes a routine in dt_dof.c to convert the finished program
71  *     into DOF format for use in anonymous tracing or enabling in the kernel.
72  *
73  * In the implementation, steps 2-4 are intertwined in that they are performed
74  * in order for each clause as part of a loop that executes over the clauses.
75  *
76  * The D compiler currently implements nearly no optimization.  The compiler
77  * implements integer constant folding as part of pass (1), and a set of very
78  * simple peephole optimizations as part of pass (3).  As with any C compiler,
79  * a large number of optimizations are possible on both the intermediate data
80  * structures and the generated DIF code.  These possibilities should be
81  * investigated in the context of whether they will have any substantive effect
82  * on the overall DTrace probe effect before they are undertaken.
83  */
84
85 #include <sys/types.h>
86 #include <sys/wait.h>
87 #include <sys/sysmacros.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
111 static const dtrace_diftype_t dt_void_rtype = {
112         DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
113 };
114
115 static const dtrace_diftype_t dt_int_rtype = {
116         DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
117 };
118
119 static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120     uint_t, int, char *const[], FILE *, const char *);
121
122
123 /*ARGSUSED*/
124 static int
125 dt_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*/
133 static int
134 dt_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
141 static dtrace_stmtdesc_t *
142 dt_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
159 static dtrace_actdesc_t *
160 dt_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  */
174 static int
175 dt_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
181 static void
182 dt_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  */
293 static void
294 dt_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
321 static void
322 dt_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
363 static void
364 dt_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
426 static void
427 dt_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
492 static void
493 dt_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
578 static void
579 dt_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 = 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
663 static void
664 dt_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         boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
668         const char *act = istrace ?  "trace" : "print";
669
670         if (dt_node_is_void(dnp->dn_args)) {
671                 dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
672                     "%s( ) may not be applied to a void expression\n", act);
673         }
674
675         if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
676                 dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
677                     "%s( ) may not be applied to a translated pointer\n", act);
678         }
679
680         if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
681                 dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
682                     "%s( ) may not be applied to an aggregation%s\n", act,
683                     istrace ? "" : " -- did you mean printa()?");
684         }
685
686         dt_cg(yypcb, dnp->dn_args);
687
688         /*
689          * The print() action behaves identically to trace(), except that it
690          * stores the CTF type of the argument (if present) within the DOF for
691          * the DIFEXPR action.  To do this, we set the 'dtsd_strdata' to point
692          * to the fully-qualified CTF type ID for the result of the DIF
693          * action.  We use the ID instead of the name to handles complex types
694          * like arrays and function pointers that can't be resolved by
695          * ctf_type_lookup().  This is later processed by dtrace_dof_create()
696          * and turned into a reference into the string table so that we can
697          * get the type information when we process the data after the fact.
698          */
699         if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
700                 dt_node_t *dret;
701                 size_t n;
702                 dt_module_t *dmp;
703
704                 dret = yypcb->pcb_dret;
705                 dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
706
707                 n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name, dret->dn_type) + 1;
708                 sdp->dtsd_strdata = dt_alloc(dtp, n);
709                 if (sdp->dtsd_strdata == NULL)
710                         longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
711                 (void) snprintf(sdp->dtsd_strdata, n, "%s`%ld", dmp->dm_name,
712                     dret->dn_type);
713         }
714
715         ap->dtad_difo = dt_as(yypcb);
716         ap->dtad_kind = DTRACEACT_DIFEXPR;
717 }
718
719 static void
720 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
721 {
722         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
723
724         dt_node_t *addr = dnp->dn_args;
725         dt_node_t *max = dnp->dn_args->dn_list;
726         dt_node_t *size;
727
728         char n[DT_TYPE_NAMELEN];
729
730         if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
731                 dnerror(addr, D_TRACEMEM_ADDR,
732                     "tracemem( ) argument #1 is incompatible with "
733                     "prototype:\n\tprototype: pointer or integer\n"
734                     "\t argument: %s\n",
735                     dt_node_type_name(addr, n, sizeof (n)));
736         }
737
738         if (dt_node_is_posconst(max) == 0) {
739                 dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
740                     "be a non-zero positive integral constant expression\n");
741         }
742
743         if ((size = max->dn_list) != NULL) {
744                 if (size->dn_list != NULL) {
745                         dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
746                             "mismatch: expected at most 3 args\n");
747                 }
748
749                 if (!dt_node_is_scalar(size)) {
750                         dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
751                             "dynamic size (argument #3) must be of "
752                             "scalar type\n");
753                 }
754
755                 dt_cg(yypcb, size);
756                 ap->dtad_difo = dt_as(yypcb);
757                 ap->dtad_difo->dtdo_rtype = dt_int_rtype;
758                 ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
759
760                 ap = dt_stmt_action(dtp, sdp);
761         }
762
763         dt_cg(yypcb, addr);
764         ap->dtad_difo = dt_as(yypcb);
765         ap->dtad_kind = DTRACEACT_TRACEMEM;
766
767         ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
768         ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
769 }
770
771 static void
772 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
773 {
774         ap->dtad_kind = DTRACEACT_STACK;
775
776         if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
777                 ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
778         } else {
779                 ap->dtad_arg = 0;
780         }
781
782         if (arg0 != NULL) {
783                 if (arg0->dn_list != NULL) {
784                         dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
785                             "mismatch: too many arguments\n");
786                 }
787
788                 if (dt_node_is_posconst(arg0) == 0) {
789                         dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
790                             "non-zero positive integral constant expression\n");
791                 }
792
793                 ap->dtad_arg = arg0->dn_value;
794         }
795 }
796
797 static void
798 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
799 {
800         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
801         dt_action_stack_args(dtp, ap, dnp->dn_args);
802 }
803
804 static void
805 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
806 {
807         uint32_t nframes = 0;
808         uint32_t strsize = 0;   /* default string table size */
809         dt_node_t *arg0 = dnp->dn_args;
810         dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
811
812         assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
813             dnp->dn_ident->di_id == DT_ACT_USTACK);
814
815         if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
816                 if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
817                         nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
818
819                 if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
820                         strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
821
822                 ap->dtad_kind = DTRACEACT_JSTACK;
823         } else {
824                 assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
825
826                 if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
827                         nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
828
829                 ap->dtad_kind = DTRACEACT_USTACK;
830         }
831
832         if (arg0 != NULL) {
833                 if (!dt_node_is_posconst(arg0)) {
834                         dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
835                             "must be a non-zero positive integer constant\n");
836                 }
837                 nframes = (uint32_t)arg0->dn_value;
838         }
839
840         if (arg1 != NULL) {
841                 if (arg1->dn_kind != DT_NODE_INT ||
842                     ((arg1->dn_flags & DT_NF_SIGNED) &&
843                     (int64_t)arg1->dn_value < 0)) {
844                         dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
845                             "must be a positive integer constant\n");
846                 }
847
848                 if (arg1->dn_list != NULL) {
849                         dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
850                             "mismatch: too many arguments\n");
851                 }
852
853                 strsize = (uint32_t)arg1->dn_value;
854         }
855
856         ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
857 }
858
859 static void
860 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
861 {
862         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
863         dt_action_ustack_args(dtp, ap, dnp);
864 }
865
866 static void
867 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
868 {
869         dtrace_actdesc_t *ap;
870         dt_node_t *arg0, *arg1;
871
872         /*
873          * The prototype guarantees that we are called with either one or
874          * two arguments, and that any arguments that are present are strings.
875          */
876         arg0 = dnp->dn_args;
877         arg1 = arg0->dn_list;
878
879         ap = dt_stmt_action(dtp, sdp);
880         dt_cg(yypcb, arg0);
881         ap->dtad_difo = dt_as(yypcb);
882         ap->dtad_kind = DTRACEACT_LIBACT;
883         ap->dtad_arg = DT_ACT_SETOPT;
884
885         ap = dt_stmt_action(dtp, sdp);
886
887         if (arg1 == NULL) {
888                 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
889         } else {
890                 dt_cg(yypcb, arg1);
891                 ap->dtad_difo = dt_as(yypcb);
892                 ap->dtad_kind = DTRACEACT_LIBACT;
893         }
894
895         ap->dtad_arg = DT_ACT_SETOPT;
896 }
897
898 /*ARGSUSED*/
899 static void
900 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
901     dt_node_t *dnp, dtrace_actkind_t kind)
902 {
903         assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
904             kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
905             kind == DTRACEACT_UADDR);
906
907         dt_cg(yypcb, dnp);
908         ap->dtad_difo = dt_as(yypcb);
909         ap->dtad_kind = kind;
910         ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
911 }
912
913 static void
914 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
915     dtrace_actkind_t kind)
916 {
917         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
918         dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
919 }
920
921 /*ARGSUSED*/
922 static void
923 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
924 {
925         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
926
927         /*
928          * Library actions need a DIFO that serves as an argument.  As
929          * ftruncate() doesn't take an argument, we generate the constant 0
930          * in a DIFO; this constant will be ignored when the ftruncate() is
931          * processed.
932          */
933         dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
934         ap->dtad_arg = DT_ACT_FTRUNCATE;
935 }
936
937 /*ARGSUSED*/
938 static void
939 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
940 {
941         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
942
943         ap->dtad_kind = DTRACEACT_STOP;
944         ap->dtad_arg = 0;
945 }
946
947 /*ARGSUSED*/
948 static void
949 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
950 {
951         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
952
953         ap->dtad_kind = DTRACEACT_BREAKPOINT;
954         ap->dtad_arg = 0;
955 }
956
957 /*ARGSUSED*/
958 static void
959 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
960 {
961         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
962
963         ap->dtad_kind = DTRACEACT_PANIC;
964         ap->dtad_arg = 0;
965 }
966
967 static void
968 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
969 {
970         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
971
972         dt_cg(yypcb, dnp->dn_args);
973         ap->dtad_difo = dt_as(yypcb);
974         ap->dtad_kind = DTRACEACT_CHILL;
975 }
976
977 static void
978 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
979 {
980         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
981
982         dt_cg(yypcb, dnp->dn_args);
983         ap->dtad_difo = dt_as(yypcb);
984         ap->dtad_kind = DTRACEACT_RAISE;
985 }
986
987 static void
988 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
989 {
990         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
991
992         dt_cg(yypcb, dnp->dn_args);
993         ap->dtad_difo = dt_as(yypcb);
994         ap->dtad_kind = DTRACEACT_EXIT;
995         ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
996 }
997
998 static void
999 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1000 {
1001         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1002
1003         dt_cg(yypcb, dnp->dn_args);
1004         ap->dtad_difo = dt_as(yypcb);
1005         ap->dtad_kind = DTRACEACT_SPECULATE;
1006 }
1007
1008 static void
1009 dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1010 {
1011         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1012
1013         dt_node_t *size = dnp->dn_args;
1014         dt_node_t *addr = dnp->dn_args->dn_list;
1015
1016         char n[DT_TYPE_NAMELEN];
1017
1018         if (dt_node_is_posconst(size) == 0) {
1019                 dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
1020                     "be a non-zero positive integral constant expression\n");
1021         }
1022
1023         if (dt_node_is_pointer(addr) == 0) {
1024                 dnerror(addr, D_PRINTM_ADDR,
1025                     "printm( ) argument #2 is incompatible with "
1026                     "prototype:\n\tprototype: pointer\n"
1027                     "\t argument: %s\n",
1028                     dt_node_type_name(addr, n, sizeof (n)));
1029         }
1030
1031         dt_cg(yypcb, addr);
1032         ap->dtad_difo = dt_as(yypcb);
1033         ap->dtad_kind = DTRACEACT_PRINTM;
1034
1035         ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1036         ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
1037 }
1038
1039 static void
1040 dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1041 {
1042         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1043
1044         dt_node_t *size = dnp->dn_args;
1045         dt_node_t *addr = dnp->dn_args->dn_list;
1046
1047         char n[DT_TYPE_NAMELEN];
1048
1049         if (dt_node_is_posconst(size) == 0) {
1050                 dnerror(size, D_PRINTT_SIZE, "printt( ) argument #1 must "
1051                     "be a non-zero positive integral constant expression\n");
1052         }
1053
1054         if (addr == NULL || addr->dn_kind != DT_NODE_FUNC ||
1055             addr->dn_ident != dt_idhash_lookup(dtp->dt_globals, "typeref")) {
1056                 dnerror(addr, D_PRINTT_ADDR,
1057                     "printt( ) argument #2 is incompatible with "
1058                     "prototype:\n\tprototype: typeref()\n"
1059                     "\t argument: %s\n",
1060                     dt_node_type_name(addr, n, sizeof (n)));
1061         }
1062
1063         dt_cg(yypcb, addr);
1064         ap->dtad_difo = dt_as(yypcb);
1065         ap->dtad_kind = DTRACEACT_PRINTT;
1066
1067         ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1068
1069         /*
1070          * Allow additional buffer space for the data size, type size,
1071          * type string length and a stab in the dark (32 bytes) for the
1072          * type string. The type string is part of the typeref() that
1073          * this action references.
1074          */
1075         ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1076
1077 }
1078
1079 static void
1080 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1081 {
1082         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1083
1084         dt_cg(yypcb, dnp->dn_args);
1085         ap->dtad_difo = dt_as(yypcb);
1086         ap->dtad_kind = DTRACEACT_COMMIT;
1087 }
1088
1089 static void
1090 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1091 {
1092         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1093
1094         dt_cg(yypcb, dnp->dn_args);
1095         ap->dtad_difo = dt_as(yypcb);
1096         ap->dtad_kind = DTRACEACT_DISCARD;
1097 }
1098
1099 static void
1100 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1101 {
1102         switch (dnp->dn_expr->dn_ident->di_id) {
1103         case DT_ACT_BREAKPOINT:
1104                 dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1105                 break;
1106         case DT_ACT_CHILL:
1107                 dt_action_chill(dtp, dnp->dn_expr, sdp);
1108                 break;
1109         case DT_ACT_CLEAR:
1110                 dt_action_clear(dtp, dnp->dn_expr, sdp);
1111                 break;
1112         case DT_ACT_COMMIT:
1113                 dt_action_commit(dtp, dnp->dn_expr, sdp);
1114                 break;
1115         case DT_ACT_DENORMALIZE:
1116                 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1117                 break;
1118         case DT_ACT_DISCARD:
1119                 dt_action_discard(dtp, dnp->dn_expr, sdp);
1120                 break;
1121         case DT_ACT_EXIT:
1122                 dt_action_exit(dtp, dnp->dn_expr, sdp);
1123                 break;
1124         case DT_ACT_FREOPEN:
1125                 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1126                 break;
1127         case DT_ACT_FTRUNCATE:
1128                 dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1129                 break;
1130         case DT_ACT_MOD:
1131                 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1132                 break;
1133         case DT_ACT_NORMALIZE:
1134                 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1135                 break;
1136         case DT_ACT_PANIC:
1137                 dt_action_panic(dtp, dnp->dn_expr, sdp);
1138                 break;
1139         case DT_ACT_PRINT:
1140                 dt_action_trace(dtp, dnp->dn_expr, sdp);
1141                 break;
1142         case DT_ACT_PRINTA:
1143                 dt_action_printa(dtp, dnp->dn_expr, sdp);
1144                 break;
1145         case DT_ACT_PRINTF:
1146                 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1147                 break;
1148         case DT_ACT_PRINTM:
1149                 dt_action_printm(dtp, dnp->dn_expr, sdp);
1150                 break;
1151         case DT_ACT_PRINTT:
1152                 dt_action_printt(dtp, dnp->dn_expr, sdp);
1153                 break;
1154         case DT_ACT_RAISE:
1155                 dt_action_raise(dtp, dnp->dn_expr, sdp);
1156                 break;
1157         case DT_ACT_SETOPT:
1158                 dt_action_setopt(dtp, dnp->dn_expr, sdp);
1159                 break;
1160         case DT_ACT_SPECULATE:
1161                 dt_action_speculate(dtp, dnp->dn_expr, sdp);
1162                 break;
1163         case DT_ACT_STACK:
1164                 dt_action_stack(dtp, dnp->dn_expr, sdp);
1165                 break;
1166         case DT_ACT_STOP:
1167                 dt_action_stop(dtp, dnp->dn_expr, sdp);
1168                 break;
1169         case DT_ACT_SYM:
1170                 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1171                 break;
1172         case DT_ACT_SYSTEM:
1173                 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1174                 break;
1175         case DT_ACT_TRACE:
1176                 dt_action_trace(dtp, dnp->dn_expr, sdp);
1177                 break;
1178         case DT_ACT_TRACEMEM:
1179                 dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1180                 break;
1181         case DT_ACT_TRUNC:
1182                 dt_action_trunc(dtp, dnp->dn_expr, sdp);
1183                 break;
1184         case DT_ACT_UADDR:
1185                 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1186                 break;
1187         case DT_ACT_UMOD:
1188                 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1189                 break;
1190         case DT_ACT_USYM:
1191                 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1192                 break;
1193         case DT_ACT_USTACK:
1194         case DT_ACT_JSTACK:
1195                 dt_action_ustack(dtp, dnp->dn_expr, sdp);
1196                 break;
1197         default:
1198                 dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1199                     "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1200         }
1201 }
1202
1203 static void
1204 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1205 {
1206         dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1207
1208         dt_cg(yypcb, dnp->dn_expr);
1209         ap->dtad_difo = dt_as(yypcb);
1210         ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1211         ap->dtad_kind = DTRACEACT_DIFEXPR;
1212 }
1213
1214 static void
1215 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1216 {
1217         dt_ident_t *aid, *fid;
1218         dt_node_t *anp, *incr = NULL;
1219         dtrace_actdesc_t *ap;
1220         uint_t n = 1, argmax;
1221         uint64_t arg = 0;
1222
1223         /*
1224          * If the aggregation has no aggregating function applied to it, then
1225          * this statement has no effect.  Flag this as a programming error.
1226          */
1227         if (dnp->dn_aggfun == NULL) {
1228                 dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1229                     dnp->dn_ident->di_name);
1230         }
1231
1232         aid = dnp->dn_ident;
1233         fid = dnp->dn_aggfun->dn_ident;
1234
1235         if (dnp->dn_aggfun->dn_args != NULL &&
1236             dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1237                 dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1238                     "be of scalar type\n", fid->di_name);
1239         }
1240
1241         /*
1242          * The ID of the aggregation itself is implicitly recorded as the first
1243          * member of each aggregation tuple so we can distinguish them later.
1244          */
1245         ap = dt_stmt_action(dtp, sdp);
1246         dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1247
1248         for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1249                 ap = dt_stmt_action(dtp, sdp);
1250                 n++;
1251
1252                 if (anp->dn_kind == DT_NODE_FUNC) {
1253                         if (anp->dn_ident->di_id == DT_ACT_STACK) {
1254                                 dt_action_stack_args(dtp, ap, anp->dn_args);
1255                                 continue;
1256                         }
1257
1258                         if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1259                             anp->dn_ident->di_id == DT_ACT_JSTACK) {
1260                                 dt_action_ustack_args(dtp, ap, anp);
1261                                 continue;
1262                         }
1263
1264                         switch (anp->dn_ident->di_id) {
1265                         case DT_ACT_UADDR:
1266                                 dt_action_symmod_args(dtp, ap,
1267                                     anp->dn_args, DTRACEACT_UADDR);
1268                                 continue;
1269
1270                         case DT_ACT_USYM:
1271                                 dt_action_symmod_args(dtp, ap,
1272                                     anp->dn_args, DTRACEACT_USYM);
1273                                 continue;
1274
1275                         case DT_ACT_UMOD:
1276                                 dt_action_symmod_args(dtp, ap,
1277                                     anp->dn_args, DTRACEACT_UMOD);
1278                                 continue;
1279
1280                         case DT_ACT_SYM:
1281                                 dt_action_symmod_args(dtp, ap,
1282                                     anp->dn_args, DTRACEACT_SYM);
1283                                 continue;
1284
1285                         case DT_ACT_MOD:
1286                                 dt_action_symmod_args(dtp, ap,
1287                                     anp->dn_args, DTRACEACT_MOD);
1288                                 continue;
1289
1290                         default:
1291                                 break;
1292                         }
1293                 }
1294
1295                 dt_cg(yypcb, anp);
1296                 ap->dtad_difo = dt_as(yypcb);
1297                 ap->dtad_kind = DTRACEACT_DIFEXPR;
1298         }
1299
1300         if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1301                 /*
1302                  * For linear quantization, we have between two and four
1303                  * arguments in addition to the expression:
1304                  *
1305                  *    arg1 => Base value
1306                  *    arg2 => Limit value
1307                  *    arg3 => Quantization level step size (defaults to 1)
1308                  *    arg4 => Quantization increment value (defaults to 1)
1309                  */
1310                 dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1311                 dt_node_t *arg2 = arg1->dn_list;
1312                 dt_node_t *arg3 = arg2->dn_list;
1313                 dt_idsig_t *isp;
1314                 uint64_t nlevels, step = 1, oarg;
1315                 int64_t baseval, limitval;
1316
1317                 if (arg1->dn_kind != DT_NODE_INT) {
1318                         dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1319                             "argument #1 must be an integer constant\n");
1320                 }
1321
1322                 baseval = (int64_t)arg1->dn_value;
1323
1324                 if (baseval < INT32_MIN || baseval > INT32_MAX) {
1325                         dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1326                             "argument #1 must be a 32-bit quantity\n");
1327                 }
1328
1329                 if (arg2->dn_kind != DT_NODE_INT) {
1330                         dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1331                             "argument #2 must be an integer constant\n");
1332                 }
1333
1334                 limitval = (int64_t)arg2->dn_value;
1335
1336                 if (limitval < INT32_MIN || limitval > INT32_MAX) {
1337                         dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1338                             "argument #2 must be a 32-bit quantity\n");
1339                 }
1340
1341                 if (limitval < baseval) {
1342                         dnerror(dnp, D_LQUANT_MISMATCH,
1343                             "lquantize( ) base (argument #1) must be less "
1344                             "than limit (argument #2)\n");
1345                 }
1346
1347                 if (arg3 != NULL) {
1348                         if (!dt_node_is_posconst(arg3)) {
1349                                 dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1350                                     "argument #3 must be a non-zero positive "
1351                                     "integer constant\n");
1352                         }
1353
1354                         if ((step = arg3->dn_value) > UINT16_MAX) {
1355                                 dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1356                                     "argument #3 must be a 16-bit quantity\n");
1357                         }
1358                 }
1359
1360                 nlevels = (limitval - baseval) / step;
1361
1362                 if (nlevels == 0) {
1363                         dnerror(dnp, D_LQUANT_STEPLARGE,
1364                             "lquantize( ) step (argument #3) too large: must "
1365                             "have at least one quantization level\n");
1366                 }
1367
1368                 if (nlevels > UINT16_MAX) {
1369                         dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1370                             "(argument #3) too small: number of quantization "
1371                             "levels must be a 16-bit quantity\n");
1372                 }
1373
1374                 arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1375                     (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1376                     ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1377                     DTRACE_LQUANTIZE_BASEMASK);
1378
1379                 assert(arg != 0);
1380
1381                 isp = (dt_idsig_t *)aid->di_data;
1382
1383                 if (isp->dis_auxinfo == 0) {
1384                         /*
1385                          * This is the first time we've seen an lquantize()
1386                          * for this aggregation; we'll store our argument
1387                          * as the auxiliary signature information.
1388                          */
1389                         isp->dis_auxinfo = arg;
1390                 } else if ((oarg = isp->dis_auxinfo) != arg) {
1391                         /*
1392                          * If we have seen this lquantize() before and the
1393                          * argument doesn't match the original argument, pick
1394                          * the original argument apart to concisely report the
1395                          * mismatch.
1396                          */
1397                         int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1398                         int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1399                         int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1400
1401                         if (obaseval != baseval) {
1402                                 dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1403                                     "base (argument #1) doesn't match previous "
1404                                     "declaration: expected %d, found %d\n",
1405                                     obaseval, (int)baseval);
1406                         }
1407
1408                         if (onlevels * ostep != nlevels * step) {
1409                                 dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1410                                     "limit (argument #2) doesn't match previous"
1411                                     " declaration: expected %d, found %d\n",
1412                                     obaseval + onlevels * ostep,
1413                                     (int)baseval + (int)nlevels * (int)step);
1414                         }
1415
1416                         if (ostep != step) {
1417                                 dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1418                                     "step (argument #3) doesn't match previous "
1419                                     "declaration: expected %d, found %d\n",
1420                                     ostep, (int)step);
1421                         }
1422
1423                         /*
1424                          * We shouldn't be able to get here -- one of the
1425                          * parameters must be mismatched if the arguments
1426                          * didn't match.
1427                          */
1428                         assert(0);
1429                 }
1430
1431                 incr = arg3 != NULL ? arg3->dn_list : NULL;
1432                 argmax = 5;
1433         }
1434
1435         if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1436                 /*
1437                  * For log/linear quantizations, we have between one and five
1438                  * arguments in addition to the expression:
1439                  *
1440                  *    arg1 => Factor
1441                  *    arg2 => Low magnitude
1442                  *    arg3 => High magnitude
1443                  *    arg4 => Number of steps per magnitude
1444                  *    arg5 => Quantization increment value (defaults to 1)
1445                  */
1446                 dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1447                 uint64_t oarg, order, v;
1448                 dt_idsig_t *isp;
1449                 int i;
1450
1451                 struct {
1452                         char *str;              /* string identifier */
1453                         int badtype;            /* error on bad type */
1454                         int badval;             /* error on bad value */
1455                         int mismatch;           /* error on bad match */
1456                         int shift;              /* shift value */
1457                         uint16_t value;         /* value itself */
1458                 } args[] = {
1459                         { "factor", D_LLQUANT_FACTORTYPE,
1460                             D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1461                             DTRACE_LLQUANTIZE_FACTORSHIFT },
1462                         { "low magnitude", D_LLQUANT_LOWTYPE,
1463                             D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1464                             DTRACE_LLQUANTIZE_LOWSHIFT },
1465                         { "high magnitude", D_LLQUANT_HIGHTYPE,
1466                             D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1467                             DTRACE_LLQUANTIZE_HIGHSHIFT },
1468                         { "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1469                             D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1470                             DTRACE_LLQUANTIZE_NSTEPSHIFT },
1471                         { NULL }
1472                 };
1473
1474                 assert(arg == 0);
1475
1476                 for (i = 0; args[i].str != NULL; i++) {
1477                         if (llarg->dn_kind != DT_NODE_INT) {
1478                                 dnerror(llarg, args[i].badtype, "llquantize( ) "
1479                                     "argument #%d (%s) must be an "
1480                                     "integer constant\n", i + 1, args[i].str);
1481                         }
1482
1483                         if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1484                                 dnerror(llarg, args[i].badval, "llquantize( ) "
1485                                     "argument #%d (%s) must be an unsigned "
1486                                     "16-bit quantity\n", i + 1, args[i].str);
1487                         }
1488
1489                         args[i].value = (uint16_t)llarg->dn_value;
1490
1491                         assert(!(arg & ((uint64_t)UINT16_MAX <<
1492                             args[i].shift)));
1493                         arg |= ((uint64_t)args[i].value << args[i].shift);
1494                         llarg = llarg->dn_list;
1495                 }
1496
1497                 assert(arg != 0);
1498
1499                 if (args[0].value < 2) {
1500                         dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1501                             "factor (argument #1) must be two or more\n");
1502                 }
1503
1504                 if (args[1].value >= args[2].value) {
1505                         dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1506                             "high magnitude (argument #3) must be greater "
1507                             "than low magnitude (argument #2)\n");
1508                 }
1509
1510                 if (args[3].value < args[0].value) {
1511                         dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1512                             "factor (argument #1) must be less than or "
1513                             "equal to the number of linear steps per "
1514                             "magnitude (argument #4)\n");
1515                 }
1516
1517                 for (v = args[0].value; v < args[3].value; v *= args[0].value)
1518                         continue;
1519
1520                 if ((args[3].value % args[0].value) || (v % args[3].value)) {
1521                         dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1522                             "factor (argument #1) must evenly divide the "
1523                             "number of steps per magnitude (argument #4), "
1524                             "and the number of steps per magnitude must evenly "
1525                             "divide a power of the factor\n");
1526                 }
1527
1528                 for (i = 0, order = 1; i < args[2].value; i++) {
1529                         if (order * args[0].value > order) {
1530                                 order *= args[0].value;
1531                                 continue;
1532                         }
1533
1534                         dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1535                             "factor (%d) raised to power of high magnitude "
1536                             "(%d) overflows 64-bits\n", args[0].value,
1537                             args[2].value);
1538                 }
1539
1540                 isp = (dt_idsig_t *)aid->di_data;
1541
1542                 if (isp->dis_auxinfo == 0) {
1543                         /*
1544                          * This is the first time we've seen an llquantize()
1545                          * for this aggregation; we'll store our argument
1546                          * as the auxiliary signature information.
1547                          */
1548                         isp->dis_auxinfo = arg;
1549                 } else if ((oarg = isp->dis_auxinfo) != arg) {
1550                         /*
1551                          * If we have seen this llquantize() before and the
1552                          * argument doesn't match the original argument, pick
1553                          * the original argument apart to concisely report the
1554                          * mismatch.
1555                          */
1556                         int expected = 0, found = 0;
1557
1558                         for (i = 0; expected == found; i++) {
1559                                 assert(args[i].str != NULL);
1560
1561                                 expected = (oarg >> args[i].shift) & UINT16_MAX;
1562                                 found = (arg >> args[i].shift) & UINT16_MAX;
1563                         }
1564
1565                         dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1566                             "%s (argument #%d) doesn't match previous "
1567                             "declaration: expected %d, found %d\n",
1568                             args[i - 1].str, i, expected, found);
1569                 }
1570
1571                 incr = llarg;
1572                 argmax = 6;
1573         }
1574
1575         if (fid->di_id == DTRACEAGG_QUANTIZE) {
1576                 incr = dnp->dn_aggfun->dn_args->dn_list;
1577                 argmax = 2;
1578         }
1579
1580         if (incr != NULL) {
1581                 if (!dt_node_is_scalar(incr)) {
1582                         dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1583                             "(argument #%d) must be of scalar type\n",
1584                             fid->di_name, argmax);
1585                 }
1586
1587                 if ((anp = incr->dn_list) != NULL) {
1588                         int argc = argmax;
1589
1590                         for (; anp != NULL; anp = anp->dn_list)
1591                                 argc++;
1592
1593                         dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1594                             "mismatch: %d args passed, at most %d expected",
1595                             fid->di_name, argc, argmax);
1596                 }
1597
1598                 ap = dt_stmt_action(dtp, sdp);
1599                 n++;
1600
1601                 dt_cg(yypcb, incr);
1602                 ap->dtad_difo = dt_as(yypcb);
1603                 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1604                 ap->dtad_kind = DTRACEACT_DIFEXPR;
1605         }
1606
1607         assert(sdp->dtsd_aggdata == NULL);
1608         sdp->dtsd_aggdata = aid;
1609
1610         ap = dt_stmt_action(dtp, sdp);
1611         assert(fid->di_kind == DT_IDENT_AGGFUNC);
1612         assert(DTRACEACT_ISAGG(fid->di_id));
1613         ap->dtad_kind = fid->di_id;
1614         ap->dtad_ntuple = n;
1615         ap->dtad_arg = arg;
1616
1617         if (dnp->dn_aggfun->dn_args != NULL) {
1618                 dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1619                 ap->dtad_difo = dt_as(yypcb);
1620         }
1621 }
1622
1623 static void
1624 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1625 {
1626         dtrace_ecbdesc_t *edp;
1627         dtrace_stmtdesc_t *sdp;
1628         dt_node_t *dnp;
1629
1630         yylineno = pnp->dn_line;
1631         dt_setcontext(dtp, pnp->dn_desc);
1632         (void) dt_node_cook(cnp, DT_IDFLG_REF);
1633
1634         if (DT_TREEDUMP_PASS(dtp, 2))
1635                 dt_node_printr(cnp, stderr, 0);
1636
1637         if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1638                 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1639
1640         assert(yypcb->pcb_ecbdesc == NULL);
1641         yypcb->pcb_ecbdesc = edp;
1642
1643         if (cnp->dn_pred != NULL) {
1644                 dt_cg(yypcb, cnp->dn_pred);
1645                 edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1646         }
1647
1648         if (cnp->dn_acts == NULL) {
1649                 dt_stmt_append(dt_stmt_create(dtp, edp,
1650                     cnp->dn_ctxattr, _dtrace_defattr), cnp);
1651         }
1652
1653         for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1654                 assert(yypcb->pcb_stmt == NULL);
1655                 sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1656
1657                 switch (dnp->dn_kind) {
1658                 case DT_NODE_DEXPR:
1659                         if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1660                                 dt_compile_agg(dtp, dnp->dn_expr, sdp);
1661                         else
1662                                 dt_compile_exp(dtp, dnp, sdp);
1663                         break;
1664                 case DT_NODE_DFUNC:
1665                         dt_compile_fun(dtp, dnp, sdp);
1666                         break;
1667                 case DT_NODE_AGG:
1668                         dt_compile_agg(dtp, dnp, sdp);
1669                         break;
1670                 default:
1671                         dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1672                             "%u is not a valid statement\n", dnp->dn_kind);
1673                 }
1674
1675                 assert(yypcb->pcb_stmt == sdp);
1676                 dt_stmt_append(sdp, dnp);
1677         }
1678
1679         assert(yypcb->pcb_ecbdesc == edp);
1680         dt_ecbdesc_release(dtp, edp);
1681         dt_endcontext(dtp);
1682         yypcb->pcb_ecbdesc = NULL;
1683 }
1684
1685 static void
1686 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1687 {
1688         dt_node_t *pnp;
1689
1690         for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1691                 dt_compile_one_clause(dtp, cnp, pnp);
1692 }
1693
1694 static void
1695 dt_compile_xlator(dt_node_t *dnp)
1696 {
1697         dt_xlator_t *dxp = dnp->dn_xlator;
1698         dt_node_t *mnp;
1699
1700         for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1701                 assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1702                 dt_cg(yypcb, mnp);
1703                 dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1704         }
1705 }
1706
1707 void
1708 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1709 {
1710         const dtrace_pattr_t *pap;
1711         dt_probe_t *prp;
1712         dt_provider_t *pvp;
1713         dt_ident_t *idp;
1714         char attrstr[8];
1715         int err;
1716
1717         /*
1718          * Both kernel and pid based providers are allowed to have names
1719          * ending with what could be interpreted as a number. We assume it's
1720          * a pid and that we may need to dynamically create probes for
1721          * that process if:
1722          *
1723          * (1) The provider doesn't exist, or,
1724          * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1725          *
1726          * On an error, dt_pid_create_probes() will set the error message
1727          * and tag -- we just have to longjmp() out of here.
1728          */
1729         if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1730             ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1731             pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1732             dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1733                 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1734         }
1735
1736         /*
1737          * Call dt_probe_info() to get the probe arguments and attributes.  If
1738          * a representative probe is found, set 'pap' to the probe provider's
1739          * attributes.  Otherwise set 'pap' to default Unstable attributes.
1740          */
1741         if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1742                 pap = &_dtrace_prvdesc;
1743                 err = dtrace_errno(dtp);
1744                 bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1745                 yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1746                 yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1747         } else {
1748                 pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1749                 err = 0;
1750         }
1751
1752         if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1753                 xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1754                     "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1755                     pdp->dtpd_func, pdp->dtpd_name);
1756         }
1757
1758         if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1759                 xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1760
1761         dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1762             pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1763             pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1764             attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1765
1766         /*
1767          * Reset the stability attributes of D global variables that vary
1768          * based on the attributes of the provider and context itself.
1769          */
1770         if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1771                 idp->di_attr = pap->dtpa_provider;
1772         if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1773                 idp->di_attr = pap->dtpa_mod;
1774         if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1775                 idp->di_attr = pap->dtpa_func;
1776         if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1777                 idp->di_attr = pap->dtpa_name;
1778         if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1779                 idp->di_attr = pap->dtpa_args;
1780
1781         yypcb->pcb_pdesc = pdp;
1782         yypcb->pcb_probe = prp;
1783 }
1784
1785 /*
1786  * Reset context-dependent variables and state at the end of cooking a D probe
1787  * definition clause.  This ensures that external declarations between clauses
1788  * do not reference any stale context-dependent data from the previous clause.
1789  */
1790 void
1791 dt_endcontext(dtrace_hdl_t *dtp)
1792 {
1793         static const char *const cvars[] = {
1794                 "probeprov", "probemod", "probefunc", "probename", "args", NULL
1795         };
1796
1797         dt_ident_t *idp;
1798         int i;
1799
1800         for (i = 0; cvars[i] != NULL; i++) {
1801                 if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1802                         idp->di_attr = _dtrace_defattr;
1803         }
1804
1805         yypcb->pcb_pdesc = NULL;
1806         yypcb->pcb_probe = NULL;
1807 }
1808
1809 static int
1810 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1811 {
1812         if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1813                 dt_idhash_delete(dhp, idp);
1814
1815         return (0);
1816 }
1817
1818 /*
1819  * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1820  * any identifiers or translators that have been previously defined as bound to
1821  * a version greater than the specified version.  Therefore, in our current
1822  * version implementation, establishing a binding is a one-way transformation.
1823  * In addition, no versioning is currently provided for types as our .d library
1824  * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1825  * for our exclusive use.  If required, type versioning will require more work.
1826  */
1827 int
1828 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1829 {
1830         char s[DT_VERSION_STRMAX];
1831         dt_xlator_t *dxp, *nxp;
1832
1833         if (v > dtp->dt_vmax)
1834                 return (dt_set_errno(dtp, EDT_VERSREDUCED));
1835         else if (v == dtp->dt_vmax)
1836                 return (0); /* no reduction necessary */
1837
1838         dt_dprintf("reducing api version to %s\n",
1839             dt_version_num2str(v, s, sizeof (s)));
1840
1841         dtp->dt_vmax = v;
1842
1843         for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1844                 nxp = dt_list_next(dxp);
1845                 if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1846                     (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1847                         dt_list_delete(&dtp->dt_xlators, dxp);
1848         }
1849
1850         (void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1851         (void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1852         (void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1853         (void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1854
1855         return (0);
1856 }
1857
1858 /*
1859  * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1860  * and return a FILE handle for the cpp output.  We use the /dev/fd filesystem
1861  * here to simplify the code by leveraging file descriptor inheritance.
1862  */
1863 static FILE *
1864 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1865 {
1866         int argc = dtp->dt_cpp_argc;
1867         char **argv = malloc(sizeof (char *) * (argc + 5));
1868         FILE *ofp = tmpfile();
1869
1870 #if defined(sun)
1871         char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1872 #endif
1873         char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1874
1875         struct sigaction act, oact;
1876         sigset_t mask, omask;
1877
1878         int wstat, estat;
1879         pid_t pid;
1880 #if defined(sun)
1881         off64_t off;
1882 #else
1883         off_t off = 0;
1884 #endif
1885         int c;
1886
1887         if (argv == NULL || ofp == NULL) {
1888                 (void) dt_set_errno(dtp, errno);
1889                 goto err;
1890         }
1891
1892         /*
1893          * If the input is a seekable file, see if it is an interpreter file.
1894          * If we see #!, seek past the first line because cpp will choke on it.
1895          * We start cpp just prior to the \n at the end of this line so that
1896          * it still sees the newline, ensuring that #line values are correct.
1897          */
1898         if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1899                 if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1900                         for (off += 2; c != '\n'; off++) {
1901                                 if ((c = fgetc(ifp)) == EOF)
1902                                         break;
1903                         }
1904                         if (c == '\n')
1905                                 off--; /* start cpp just prior to \n */
1906                 }
1907                 (void) fflush(ifp);
1908                 (void) fseeko64(ifp, off, SEEK_SET);
1909         }
1910
1911 #if defined(sun)
1912         (void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1913         (void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1914 #endif
1915
1916         bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1917
1918         (void) snprintf(verdef, sizeof (verdef),
1919             "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1920         argv[argc++] = verdef;
1921
1922 #if defined(sun)
1923         switch (dtp->dt_stdcmode) {
1924         case DT_STDC_XA:
1925         case DT_STDC_XT:
1926                 argv[argc++] = "-D__STDC__=0";
1927                 break;
1928         case DT_STDC_XC:
1929                 argv[argc++] = "-D__STDC__=1";
1930                 break;
1931         }
1932
1933         argv[argc++] = ipath;
1934         argv[argc++] = opath;
1935 #else
1936         argv[argc++] = "-P";
1937 #endif
1938         argv[argc] = NULL;
1939
1940         /*
1941          * libdtrace must be able to be embedded in other programs that may
1942          * include application-specific signal handlers.  Therefore, if we
1943          * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1944          * that could confuse the containing application.  To do this,
1945          * we block SIGCHLD and reset its disposition to SIG_DFL.
1946          * We restore our signal state once we are done.
1947          */
1948         (void) sigemptyset(&mask);
1949         (void) sigaddset(&mask, SIGCHLD);
1950         (void) sigprocmask(SIG_BLOCK, &mask, &omask);
1951
1952         bzero(&act, sizeof (act));
1953         act.sa_handler = SIG_DFL;
1954         (void) sigaction(SIGCHLD, &act, &oact);
1955
1956         if ((pid = fork1()) == -1) {
1957                 (void) sigaction(SIGCHLD, &oact, NULL);
1958                 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1959                 (void) dt_set_errno(dtp, EDT_CPPFORK);
1960                 goto err;
1961         }
1962
1963         if (pid == 0) {
1964 #if !defined(sun)
1965                 if (isatty(fileno(ifp)) == 0)
1966                         lseek(fileno(ifp), off, SEEK_SET);
1967                 dup2(fileno(ifp), 0);
1968                 dup2(fileno(ofp), 1);
1969 #endif
1970                 (void) execvp(dtp->dt_cpp_path, argv);
1971                 _exit(errno == ENOENT ? 127 : 126);
1972         }
1973
1974         do {
1975                 dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1976                     (int)pid);
1977         } while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1978
1979         (void) sigaction(SIGCHLD, &oact, NULL);
1980         (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1981
1982         dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1983         estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1984
1985         if (estat != 0) {
1986                 switch (estat) {
1987                 case 126:
1988                         (void) dt_set_errno(dtp, EDT_CPPEXEC);
1989                         break;
1990                 case 127:
1991                         (void) dt_set_errno(dtp, EDT_CPPENT);
1992                         break;
1993                 default:
1994                         (void) dt_set_errno(dtp, EDT_CPPERR);
1995                 }
1996                 goto err;
1997         }
1998
1999         free(argv);
2000         (void) fflush(ofp);
2001         (void) fseek(ofp, 0, SEEK_SET);
2002         return (ofp);
2003
2004 err:
2005         free(argv);
2006         (void) fclose(ofp);
2007         return (NULL);
2008 }
2009
2010 static void
2011 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
2012 {
2013         va_list ap;
2014
2015         va_start(ap, format);
2016         dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
2017         va_end(ap);
2018 }
2019
2020 int
2021 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
2022 {
2023         dt_lib_depend_t *dld;
2024         const char *end;
2025
2026         assert(arg != NULL);
2027
2028         if ((end = strrchr(arg, '/')) == NULL)
2029                 return (dt_set_errno(dtp, EINVAL));
2030
2031         if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2032                 return (-1);
2033
2034         if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
2035                 dt_free(dtp, dld);
2036                 return (-1);
2037         }
2038
2039         (void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
2040         if ((dld->dtld_library = strdup(arg)) == NULL) {
2041                 dt_free(dtp, dld->dtld_libpath);
2042                 dt_free(dtp, dld);
2043                 return (dt_set_errno(dtp, EDT_NOMEM));
2044         }
2045
2046         dt_list_append(dlp, dld);
2047         return (0);
2048 }
2049
2050 dt_lib_depend_t *
2051 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
2052 {
2053         dt_lib_depend_t *dldn;
2054
2055         for (dldn = dt_list_next(dld); dldn != NULL;
2056             dldn = dt_list_next(dldn)) {
2057                 if (strcmp(dldn->dtld_library, arg) == 0)
2058                         return (dldn);
2059         }
2060
2061         return (NULL);
2062 }
2063
2064 /*
2065  * Go through all the library files, and, if any library dependencies exist for
2066  * that file, add it to that node's list of dependents. The result of this
2067  * will be a graph which can then be topologically sorted to produce a
2068  * compilation order.
2069  */
2070 static int
2071 dt_lib_build_graph(dtrace_hdl_t *dtp)
2072 {
2073         dt_lib_depend_t *dld, *dpld;
2074
2075         for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2076             dld = dt_list_next(dld)) {
2077                 char *library = dld->dtld_library;
2078
2079                 for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2080                     dpld = dt_list_next(dpld)) {
2081                         dt_lib_depend_t *dlda;
2082
2083                         if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2084                             dpld->dtld_library)) == NULL) {
2085                                 dt_lib_depend_error(dtp,
2086                                     "Invalid library dependency in %s: %s\n",
2087                                     dld->dtld_library, dpld->dtld_library);
2088
2089                                 return (dt_set_errno(dtp, EDT_COMPILER));
2090                         }
2091
2092                         if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2093                             library)) != 0) {
2094                                 return (-1); /* preserve dt_errno */
2095                         }
2096                 }
2097         }
2098         return (0);
2099 }
2100
2101 static int
2102 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2103 {
2104         dt_lib_depend_t *dpld, *dlda, *new;
2105
2106         dld->dtld_start = ++(*count);
2107
2108         for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2109             dpld = dt_list_next(dpld)) {
2110                 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2111                     dpld->dtld_library);
2112                 assert(dlda != NULL);
2113
2114                 if (dlda->dtld_start == 0 &&
2115                     dt_topo_sort(dtp, dlda, count) == -1)
2116                         return (-1);
2117         }
2118
2119         if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2120                 return (-1);
2121
2122         if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2123                 dt_free(dtp, new);
2124                 return (dt_set_errno(dtp, EDT_NOMEM));
2125         }
2126
2127         new->dtld_start = dld->dtld_start;
2128         new->dtld_finish = dld->dtld_finish = ++(*count);
2129         dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2130
2131         dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2132             new->dtld_start, new->dtld_finish);
2133
2134         return (0);
2135 }
2136
2137 static int
2138 dt_lib_depend_sort(dtrace_hdl_t *dtp)
2139 {
2140         dt_lib_depend_t *dld, *dpld, *dlda;
2141         int count = 0;
2142
2143         if (dt_lib_build_graph(dtp) == -1)
2144                 return (-1); /* preserve dt_errno */
2145
2146         /*
2147          * Perform a topological sort of the graph that hangs off
2148          * dtp->dt_lib_dep. The result of this process will be a
2149          * dependency ordered list located at dtp->dt_lib_dep_sorted.
2150          */
2151         for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2152             dld = dt_list_next(dld)) {
2153                 if (dld->dtld_start == 0 &&
2154                     dt_topo_sort(dtp, dld, &count) == -1)
2155                         return (-1); /* preserve dt_errno */;
2156         }
2157
2158         /*
2159          * Check the graph for cycles. If an ancestor's finishing time is
2160          * less than any of its dependent's finishing times then a back edge
2161          * exists in the graph and this is a cycle.
2162          */
2163         for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2164             dld = dt_list_next(dld)) {
2165                 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2166                     dpld = dt_list_next(dpld)) {
2167                         dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2168                             dpld->dtld_library);
2169                         assert(dlda != NULL);
2170
2171                         if (dlda->dtld_finish > dld->dtld_finish) {
2172                                 dt_lib_depend_error(dtp,
2173                                     "Cyclic dependency detected: %s => %s\n",
2174                                     dld->dtld_library, dpld->dtld_library);
2175
2176                                 return (dt_set_errno(dtp, EDT_COMPILER));
2177                         }
2178                 }
2179         }
2180
2181         return (0);
2182 }
2183
2184 static void
2185 dt_lib_depend_free(dtrace_hdl_t *dtp)
2186 {
2187         dt_lib_depend_t *dld, *dlda;
2188
2189         while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2190                 while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2191                         dt_list_delete(&dld->dtld_dependencies, dlda);
2192                         dt_free(dtp, dlda->dtld_library);
2193                         dt_free(dtp, dlda->dtld_libpath);
2194                         dt_free(dtp, dlda);
2195                 }
2196                 while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2197                         dt_list_delete(&dld->dtld_dependents, dlda);
2198                         dt_free(dtp, dlda->dtld_library);
2199                         dt_free(dtp, dlda->dtld_libpath);
2200                         dt_free(dtp, dlda);
2201                 }
2202                 dt_list_delete(&dtp->dt_lib_dep, dld);
2203                 dt_free(dtp, dld->dtld_library);
2204                 dt_free(dtp, dld->dtld_libpath);
2205                 dt_free(dtp, dld);
2206         }
2207
2208         while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2209                 dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2210                 dt_free(dtp, dld->dtld_library);
2211                 dt_free(dtp, dld);
2212         }
2213 }
2214
2215 /*
2216  * Open all the .d library files found in the specified directory and
2217  * compile each one of them.  We silently ignore any missing directories and
2218  * other files found therein.  We only fail (and thereby fail dt_load_libs()) if
2219  * we fail to compile a library and the error is something other than #pragma D
2220  * depends_on.  Dependency errors are silently ignored to permit a library
2221  * directory to contain libraries which may not be accessible depending on our
2222  * privileges.
2223  */
2224 static int
2225 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2226 {
2227         struct dirent *dp;
2228         const char *p, *end;
2229         DIR *dirp;
2230
2231         char fname[PATH_MAX];
2232         FILE *fp;
2233         void *rv;
2234         dt_lib_depend_t *dld;
2235
2236         if ((dirp = opendir(path)) == NULL) {
2237                 dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2238                 return (0);
2239         }
2240
2241         /* First, parse each file for library dependencies. */
2242         while ((dp = readdir(dirp)) != NULL) {
2243                 if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2244                         continue; /* skip any filename not ending in .d */
2245
2246                 (void) snprintf(fname, sizeof (fname),
2247                     "%s/%s", path, dp->d_name);
2248
2249                 if ((fp = fopen(fname, "r")) == NULL) {
2250                         dt_dprintf("skipping library %s: %s\n",
2251                             fname, strerror(errno));
2252                         continue;
2253                 }
2254
2255                 /*
2256                  * Skip files whose name match an already processed library
2257                  */
2258                 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2259                     dld = dt_list_next(dld)) {
2260                         end = strrchr(dld->dtld_library, '/');
2261                         /* dt_lib_depend_add ensures this */
2262                         assert(end != NULL);
2263                         if (strcmp(end + 1, dp->d_name) == 0)
2264                                 break;
2265                 }
2266
2267                 if (dld != NULL) {
2268                         dt_dprintf("skipping library %s, already processed "
2269                             "library with the same name: %s", dp->d_name,
2270                             dld->dtld_library);
2271                         continue;
2272                 }
2273
2274                 dtp->dt_filetag = fname;
2275                 if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
2276                         return (-1); /* preserve dt_errno */
2277
2278                 rv = dt_compile(dtp, DT_CTX_DPROG,
2279                     DTRACE_PROBESPEC_NAME, NULL,
2280                     DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2281
2282                 if (rv != NULL && dtp->dt_errno &&
2283                     (dtp->dt_errno != EDT_COMPILER ||
2284                     dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2285                         return (-1); /* preserve dt_errno */
2286
2287                 if (dtp->dt_errno)
2288                         dt_dprintf("error parsing library %s: %s\n",
2289                             fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2290
2291                 (void) fclose(fp);
2292                 dtp->dt_filetag = NULL;
2293         }
2294
2295         (void) closedir(dirp);
2296
2297         return (0);
2298 }
2299
2300 /*
2301  * Perform a topological sorting of all the libraries found across the entire
2302  * dt_lib_path.  Once sorted, compile each one in topological order to cache its
2303  * inlines and translators, etc.  We silently ignore any missing directories and
2304  * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2305  * we fail to compile a library and the error is something other than #pragma D
2306  * depends_on.  Dependency errors are silently ignored to permit a library
2307  * directory to contain libraries which may not be accessible depending on our
2308  * privileges.
2309  */
2310 static int
2311 dt_load_libs_sort(dtrace_hdl_t *dtp)
2312 {
2313         dtrace_prog_t *pgp;
2314         FILE *fp;
2315         dt_lib_depend_t *dld;
2316
2317         /*
2318          * Finish building the graph containing the library dependencies
2319          * and perform a topological sort to generate an ordered list
2320          * for compilation.
2321          */
2322         if (dt_lib_depend_sort(dtp) == -1)
2323                 goto err;
2324
2325         for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2326             dld = dt_list_next(dld)) {
2327
2328                 if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2329                         dt_dprintf("skipping library %s: %s\n",
2330                             dld->dtld_library, strerror(errno));
2331                         continue;
2332                 }
2333
2334                 dtp->dt_filetag = dld->dtld_library;
2335                 pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2336                 (void) fclose(fp);
2337                 dtp->dt_filetag = NULL;
2338
2339                 if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2340                     dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2341                         goto err;
2342
2343                 if (pgp == NULL) {
2344                         dt_dprintf("skipping library %s: %s\n",
2345                             dld->dtld_library,
2346                             dtrace_errmsg(dtp, dtrace_errno(dtp)));
2347                 } else {
2348                         dld->dtld_loaded = B_TRUE;
2349                         dt_program_destroy(dtp, pgp);
2350                 }
2351         }
2352
2353         dt_lib_depend_free(dtp);
2354         return (0);
2355
2356 err:
2357         dt_lib_depend_free(dtp);
2358         return (-1); /* preserve dt_errno */
2359 }
2360
2361 /*
2362  * Load the contents of any appropriate DTrace .d library files.  These files
2363  * contain inlines and translators that will be cached by the compiler.  We
2364  * defer this activity until the first compile to permit libdtrace clients to
2365  * add their own library directories and so that we can properly report errors.
2366  */
2367 static int
2368 dt_load_libs(dtrace_hdl_t *dtp)
2369 {
2370         dt_dirpath_t *dirp;
2371
2372         if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2373                 return (0); /* libraries already processed */
2374
2375         dtp->dt_cflags |= DTRACE_C_NOLIBS;
2376
2377         /*
2378          * /usr/lib/dtrace is always at the head of the list. The rest of the
2379          * list is specified in the precedence order the user requested. Process
2380          * everything other than the head first. DTRACE_C_NOLIBS has already
2381          * been spcified so dt_vopen will ensure that there is always one entry
2382          * in dt_lib_path.
2383          */
2384         for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
2385             dirp != NULL; dirp = dt_list_next(dirp)) {
2386                 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2387                         dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2388                         return (-1); /* errno is set for us */
2389                 }
2390         }
2391
2392         /* Handle /usr/lib/dtrace */
2393         dirp = dt_list_next(&dtp->dt_lib_path);
2394         if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2395                 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2396                 return (-1); /* errno is set for us */
2397         }
2398
2399         if (dt_load_libs_sort(dtp) < 0)
2400                 return (-1); /* errno is set for us */
2401
2402         return (0);
2403 }
2404
2405 static void *
2406 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2407     uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2408 {
2409         dt_node_t *dnp;
2410         dt_decl_t *ddp;
2411         dt_pcb_t pcb;
2412         void *rv;
2413         int err;
2414
2415         if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2416                 (void) dt_set_errno(dtp, EINVAL);
2417                 return (NULL);
2418         }
2419
2420         if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2421                 return (NULL); /* errno is set for us */
2422
2423         if (dtp->dt_globals->dh_nelems != 0)
2424                 (void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2425
2426         if (dtp->dt_tls->dh_nelems != 0)
2427                 (void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2428
2429         if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2430                 return (NULL); /* errno is set for us */
2431
2432         dt_pcb_push(dtp, &pcb);
2433
2434         pcb.pcb_fileptr = fp;
2435         pcb.pcb_string = s;
2436         pcb.pcb_strptr = s;
2437         pcb.pcb_strlen = s ? strlen(s) : 0;
2438         pcb.pcb_sargc = argc;
2439         pcb.pcb_sargv = argv;
2440         pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2441         pcb.pcb_pspec = pspec;
2442         pcb.pcb_cflags = dtp->dt_cflags | cflags;
2443         pcb.pcb_amin = dtp->dt_amin;
2444         pcb.pcb_yystate = -1;
2445         pcb.pcb_context = context;
2446         pcb.pcb_token = context;
2447
2448         if (context != DT_CTX_DPROG)
2449                 yybegin(YYS_EXPR);
2450         else if (cflags & DTRACE_C_CTL)
2451                 yybegin(YYS_CONTROL);
2452         else
2453                 yybegin(YYS_CLAUSE);
2454
2455         if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2456                 goto out;
2457
2458         if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2459                 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2460
2461         yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2462         yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2463             DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2464
2465         if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2466                 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2467
2468         /*
2469          * Invoke the parser to evaluate the D source code.  If any errors
2470          * occur during parsing, an error function will be called and we
2471          * will longjmp back to pcb_jmpbuf to abort.  If parsing succeeds,
2472          * we optionally display the parse tree if debugging is enabled.
2473          */
2474         if (yyparse() != 0 || yypcb->pcb_root == NULL)
2475                 xyerror(D_EMPTY, "empty D program translation unit\n");
2476
2477         yybegin(YYS_DONE);
2478
2479         if (cflags & DTRACE_C_CTL)
2480                 goto out;
2481
2482         if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2483                 dt_node_printr(yypcb->pcb_root, stderr, 0);
2484
2485         if (yypcb->pcb_pragmas != NULL)
2486                 (void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2487
2488         if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2489             !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2490                 xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2491                     "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2492         }
2493
2494         /*
2495          * If we have successfully created a parse tree for a D program, loop
2496          * over the clauses and actions and instantiate the corresponding
2497          * libdtrace program.  If we are parsing a D expression, then we
2498          * simply run the code generator and assembler on the resulting tree.
2499          */
2500         switch (context) {
2501         case DT_CTX_DPROG:
2502                 assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2503
2504                 if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2505                     !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2506                         xyerror(D_EMPTY, "empty D program translation unit\n");
2507
2508                 if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2509                         longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2510
2511                 for (; dnp != NULL; dnp = dnp->dn_list) {
2512                         switch (dnp->dn_kind) {
2513                         case DT_NODE_CLAUSE:
2514                                 dt_compile_clause(dtp, dnp);
2515                                 break;
2516                         case DT_NODE_XLATOR:
2517                                 if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2518                                         dt_compile_xlator(dnp);
2519                                 break;
2520                         case DT_NODE_PROVIDER:
2521                                 (void) dt_node_cook(dnp, DT_IDFLG_REF);
2522                                 break;
2523                         }
2524                 }
2525
2526                 yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2527                 yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2528                 yypcb->pcb_asxrefs = NULL;
2529                 yypcb->pcb_asxreflen = 0;
2530
2531                 rv = yypcb->pcb_prog;
2532                 break;
2533
2534         case DT_CTX_DEXPR:
2535                 (void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2536                 dt_cg(yypcb, yypcb->pcb_root);
2537                 rv = dt_as(yypcb);
2538                 break;
2539
2540         case DT_CTX_DTYPE:
2541                 ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2542                 err = dt_decl_type(ddp, arg);
2543                 dt_decl_free(ddp);
2544
2545                 if (err != 0)
2546                         longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2547
2548                 rv = NULL;
2549                 break;
2550         }
2551
2552 out:
2553         if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2554             DT_TREEDUMP_PASS(dtp, 3))
2555                 dt_node_printr(yypcb->pcb_root, stderr, 0);
2556
2557         if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2558             lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2559             ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2560                 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2561
2562         if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2563             lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2564             ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2565                 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2566
2567         if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2568                 (void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2569
2570         dt_pcb_pop(dtp, err);
2571         (void) dt_set_errno(dtp, err);
2572         return (err ? NULL : rv);
2573 }
2574
2575 dtrace_prog_t *
2576 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2577     dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2578 {
2579         return (dt_compile(dtp, DT_CTX_DPROG,
2580             spec, NULL, cflags, argc, argv, NULL, s));
2581 }
2582
2583 dtrace_prog_t *
2584 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2585     uint_t cflags, int argc, char *const argv[])
2586 {
2587         return (dt_compile(dtp, DT_CTX_DPROG,
2588             DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2589 }
2590
2591 int
2592 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2593 {
2594         (void) dt_compile(dtp, DT_CTX_DTYPE,
2595             DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2596         return (dtp->dt_errno ? -1 : 0);
2597 }
2598
2599 int
2600 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2601 {
2602         (void) dt_compile(dtp, DT_CTX_DTYPE,
2603             DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2604         return (dtp->dt_errno ? -1 : 0);
2605 }