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