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.
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.
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]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
27 * DTrace D Language Compiler
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:
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
50 * Several other source files provide collections of utility routines used by
51 * these major files. The compiler itself is implemented in multiple passes:
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.
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.
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.
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).
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.
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.
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.
83 #include <sys/types.h>
98 #include <dt_module.h>
99 #include <dt_program.h>
100 #include <dt_provider.h>
101 #include <dt_printf.h>
103 #include <dt_grammar.h>
104 #include <dt_ident.h>
105 #include <dt_string.h>
108 static const dtrace_diftype_t dt_void_rtype = {
109 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
112 static const dtrace_diftype_t dt_int_rtype = {
113 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
116 static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
117 uint_t, int, char *const[], FILE *, const char *);
122 dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
124 idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
125 DT_IDFLG_DIFR | DT_IDFLG_DIFW);
131 dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
133 yylineno = idp->di_lineno;
134 xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
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)
142 dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
145 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
147 assert(yypcb->pcb_stmt == NULL);
148 yypcb->pcb_stmt = sdp;
150 sdp->dtsd_descattr = descattr;
151 sdp->dtsd_stmtattr = stmtattr;
156 static dtrace_actdesc_t *
157 dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
159 dtrace_actdesc_t *new;
161 if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
162 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
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).
172 dt_action_destructive(const dtrace_actdesc_t *ap)
174 return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
175 DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
179 dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
181 dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
182 dtrace_actdesc_t *ap, *tap;
188 * Make sure that the new statement jibes with the rest of the ECB.
190 for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
191 if (ap->dtad_kind == DTRACEACT_COMMIT) {
193 dnerror(dnp, D_COMM_COMM, "commit( ) may "
194 "not follow commit( )\n");
198 dnerror(dnp, D_COMM_DREC, "commit( ) may "
199 "not follow data-recording action(s)\n");
202 for (tap = ap; tap != NULL; tap = tap->dtad_next) {
203 if (!DTRACEACT_ISAGG(tap->dtad_kind))
206 dnerror(dnp, D_AGG_COMM, "aggregating actions "
207 "may not follow commit( )\n");
214 if (ap->dtad_kind == DTRACEACT_SPECULATE) {
216 dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
217 "not follow speculate( )\n");
221 dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
222 "not follow commit( )\n");
226 dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
227 "not follow data-recording action(s)\n");
234 if (DTRACEACT_ISAGG(ap->dtad_kind)) {
236 dnerror(dnp, D_AGG_SPEC, "aggregating actions "
237 "may not follow speculate( )\n");
245 if (dt_action_destructive(ap)) {
246 dnerror(dnp, D_ACT_SPEC, "destructive actions "
247 "may not follow speculate( )\n");
250 if (ap->dtad_kind == DTRACEACT_EXIT) {
251 dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
252 "follow speculate( )\n");
257 * Exclude all non data-recording actions.
259 if (dt_action_destructive(ap) ||
260 ap->dtad_kind == DTRACEACT_DISCARD)
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)
269 dnerror(dnp, D_DREC_COMM, "data-recording actions "
270 "may not follow commit( )\n");
277 if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
278 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
280 if (yypcb->pcb_stmt == sdp)
281 yypcb->pcb_stmt = NULL;
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.
291 dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
293 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
294 dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
297 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
299 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
300 dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
302 if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
303 dt_difo_free(dtp, dp);
304 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
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 */
310 dp->dtdo_inttab[0] = id;
312 dp->dtdo_rtype = dt_int_rtype;
315 ap->dtad_kind = kind;
319 dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
322 dtrace_actdesc_t *ap;
325 char n[DT_TYPE_NAMELEN];
328 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
329 argc++; /* count up arguments for error messages below */
332 dnerror(dnp, D_CLEAR_PROTO,
333 "%s( ) prototype mismatch: %d args passed, 1 expected\n",
334 dnp->dn_ident->di_name, argc);
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)));
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);
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;
361 dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
364 dtrace_actdesc_t *ap;
365 dt_node_t *anp, *normal;
366 int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
368 char n[DT_TYPE_NAMELEN];
371 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
372 argc++; /* count up arguments for error messages below */
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);
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)));
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);
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);
404 ap = dt_stmt_action(dtp, sdp);
405 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
408 ap->dtad_arg = DT_ACT_DENORMALIZE;
412 ap->dtad_arg = DT_ACT_NORMALIZE;
414 assert(normal != NULL);
415 ap = dt_stmt_action(dtp, sdp);
416 dt_cg(yypcb, normal);
418 ap->dtad_difo = dt_as(yypcb);
419 ap->dtad_kind = DTRACEACT_LIBACT;
420 ap->dtad_arg = DT_ACT_NORMALIZE;
424 dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
427 dtrace_actdesc_t *ap;
428 dt_node_t *anp, *trunc;
430 char n[DT_TYPE_NAMELEN];
433 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
434 argc++; /* count up arguments for error messages below */
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");
445 trunc = anp->dn_list;
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)));
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);
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);
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;
475 ap = dt_stmt_action(dtp, sdp);
478 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
480 assert(trunc != NULL);
482 ap->dtad_difo = dt_as(yypcb);
483 ap->dtad_kind = DTRACEACT_LIBACT;
486 ap->dtad_arg = DT_ACT_TRUNC;
490 dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
492 dt_ident_t *aid, *fid;
493 dtrace_actdesc_t *ap;
495 dt_node_t *anp, *proto = NULL;
497 char n[DT_TYPE_NAMELEN];
498 int argc = 0, argr = 0;
500 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
501 argc++; /* count up arguments for error messages below */
503 switch (dnp->dn_args->dn_kind) {
505 format = dnp->dn_args->dn_string;
506 anp = dnp->dn_args->dn_list;
521 dnerror(dnp, D_PRINTA_PROTO,
522 "%s( ) prototype mismatch: %d args passed, %d expected\n",
523 dnp->dn_ident->di_name, argc, argr);
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)));
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);
547 * If we have multiple aggregations, we must be sure that
548 * their key signatures match.
551 dt_printa_validate(proto, anp);
556 if (format != NULL) {
557 yylineno = dnp->dn_line;
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);
567 ap = dt_stmt_action(dtp, sdp);
568 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
576 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
577 dtrace_actkind_t kind)
579 dt_node_t *anp, *arg1;
580 dtrace_actdesc_t *ap = NULL;
581 char n[DT_TYPE_NAMELEN], *str;
583 assert(DTRACEACT_ISPRINTFLIKE(kind));
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)));
593 arg1 = dnp->dn_args->dn_list;
594 yylineno = dnp->dn_line;
595 str = dnp->dn_args->dn_string;
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.
608 if (kind == DTRACEACT_FREOPEN) {
609 if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
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.
616 dnerror(dnp, D_FREOPEN_INVALID,
617 "%s( ) argument #1 cannot be \"%s\"\n",
618 dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
622 str = DT_FREOPEN_RESTORE;
625 sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
627 dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
628 dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
634 if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
635 (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
637 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
640 dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
644 dp->dtdo_rtype = dt_int_rtype;
646 ap = dt_stmt_action(dtp, sdp);
648 ap->dtad_kind = kind;
652 for (anp = arg1; anp != NULL; anp = anp->dn_list) {
653 ap = dt_stmt_action(dtp, sdp);
655 ap->dtad_difo = dt_as(yypcb);
656 ap->dtad_kind = kind;
661 dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
663 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
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");
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");
675 dt_cg(yypcb, dnp->dn_args);
676 ap->dtad_difo = dt_as(yypcb);
677 ap->dtad_kind = DTRACEACT_DIFEXPR;
681 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
683 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
685 dt_node_t *addr = dnp->dn_args;
686 dt_node_t *size = dnp->dn_args->dn_list;
688 char n[DT_TYPE_NAMELEN];
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"
695 dt_node_type_name(addr, n, sizeof (n)));
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");
704 ap->dtad_difo = dt_as(yypcb);
705 ap->dtad_kind = DTRACEACT_DIFEXPR;
707 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
708 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value;
712 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
714 ap->dtad_kind = DTRACEACT_STACK;
716 if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
717 ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
723 if (arg0->dn_list != NULL) {
724 dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
725 "mismatch: too many arguments\n");
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");
733 ap->dtad_arg = arg0->dn_value;
738 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
740 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
741 dt_action_stack_args(dtp, ap, dnp->dn_args);
745 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
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;
752 assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
753 dnp->dn_ident->di_id == DT_ACT_USTACK);
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];
759 if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
760 strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
762 ap->dtad_kind = DTRACEACT_JSTACK;
764 assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
766 if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
767 nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
769 ap->dtad_kind = DTRACEACT_USTACK;
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");
777 nframes = (uint32_t)arg0->dn_value;
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");
788 if (arg1->dn_list != NULL) {
789 dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
790 "mismatch: too many arguments\n");
793 strsize = (uint32_t)arg1->dn_value;
796 ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
800 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
802 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
803 dt_action_ustack_args(dtp, ap, dnp);
807 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
809 dtrace_actdesc_t *ap;
810 dt_node_t *arg0, *arg1;
813 * The prototype guarantees that we are called with either one or
814 * two arguments, and that any arguments that are present are strings.
817 arg1 = arg0->dn_list;
819 ap = dt_stmt_action(dtp, sdp);
821 ap->dtad_difo = dt_as(yypcb);
822 ap->dtad_kind = DTRACEACT_LIBACT;
823 ap->dtad_arg = DT_ACT_SETOPT;
825 ap = dt_stmt_action(dtp, sdp);
828 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
831 ap->dtad_difo = dt_as(yypcb);
832 ap->dtad_kind = DTRACEACT_LIBACT;
835 ap->dtad_arg = DT_ACT_SETOPT;
840 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
841 dt_node_t *dnp, dtrace_actkind_t kind)
843 assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
844 kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
845 kind == DTRACEACT_UADDR);
848 ap->dtad_difo = dt_as(yypcb);
849 ap->dtad_kind = kind;
850 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
854 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
855 dtrace_actkind_t kind)
857 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
858 dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
863 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
865 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
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
873 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
874 ap->dtad_arg = DT_ACT_FTRUNCATE;
879 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
881 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
883 ap->dtad_kind = DTRACEACT_STOP;
889 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
891 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
893 ap->dtad_kind = DTRACEACT_BREAKPOINT;
899 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
901 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
903 ap->dtad_kind = DTRACEACT_PANIC;
908 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
910 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
912 dt_cg(yypcb, dnp->dn_args);
913 ap->dtad_difo = dt_as(yypcb);
914 ap->dtad_kind = DTRACEACT_CHILL;
918 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
920 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
922 dt_cg(yypcb, dnp->dn_args);
923 ap->dtad_difo = dt_as(yypcb);
924 ap->dtad_kind = DTRACEACT_RAISE;
928 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
930 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
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);
939 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
941 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
943 dt_cg(yypcb, dnp->dn_args);
944 ap->dtad_difo = dt_as(yypcb);
945 ap->dtad_kind = DTRACEACT_SPECULATE;
949 dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
951 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
953 dt_node_t *size = dnp->dn_args;
954 dt_node_t *addr = dnp->dn_args->dn_list;
956 char n[DT_TYPE_NAMELEN];
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");
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"
968 dt_node_type_name(addr, n, sizeof (n)));
972 ap->dtad_difo = dt_as(yypcb);
973 ap->dtad_kind = DTRACEACT_PRINTM;
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);
980 dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
982 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
984 dt_node_t *size = dnp->dn_args;
985 dt_node_t *addr = dnp->dn_args->dn_list;
987 char n[DT_TYPE_NAMELEN];
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");
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"
1000 dt_node_type_name(addr, n, sizeof (n)));
1004 ap->dtad_difo = dt_as(yypcb);
1005 ap->dtad_kind = DTRACEACT_PRINTT;
1007 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
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.
1015 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1020 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1022 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1024 dt_cg(yypcb, dnp->dn_args);
1025 ap->dtad_difo = dt_as(yypcb);
1026 ap->dtad_kind = DTRACEACT_COMMIT;
1030 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1032 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1034 dt_cg(yypcb, dnp->dn_args);
1035 ap->dtad_difo = dt_as(yypcb);
1036 ap->dtad_kind = DTRACEACT_DISCARD;
1040 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1042 switch (dnp->dn_expr->dn_ident->di_id) {
1043 case DT_ACT_BREAKPOINT:
1044 dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1047 dt_action_chill(dtp, dnp->dn_expr, sdp);
1050 dt_action_clear(dtp, dnp->dn_expr, sdp);
1053 dt_action_commit(dtp, dnp->dn_expr, sdp);
1055 case DT_ACT_DENORMALIZE:
1056 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1058 case DT_ACT_DISCARD:
1059 dt_action_discard(dtp, dnp->dn_expr, sdp);
1062 dt_action_exit(dtp, dnp->dn_expr, sdp);
1064 case DT_ACT_FREOPEN:
1065 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1067 case DT_ACT_FTRUNCATE:
1068 dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1071 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1073 case DT_ACT_NORMALIZE:
1074 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1077 dt_action_panic(dtp, dnp->dn_expr, sdp);
1080 dt_action_printa(dtp, dnp->dn_expr, sdp);
1083 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1086 dt_action_printm(dtp, dnp->dn_expr, sdp);
1089 dt_action_printt(dtp, dnp->dn_expr, sdp);
1092 dt_action_raise(dtp, dnp->dn_expr, sdp);
1095 dt_action_setopt(dtp, dnp->dn_expr, sdp);
1097 case DT_ACT_SPECULATE:
1098 dt_action_speculate(dtp, dnp->dn_expr, sdp);
1101 dt_action_stack(dtp, dnp->dn_expr, sdp);
1104 dt_action_stop(dtp, dnp->dn_expr, sdp);
1107 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1110 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1113 dt_action_trace(dtp, dnp->dn_expr, sdp);
1115 case DT_ACT_TRACEMEM:
1116 dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1119 dt_action_trunc(dtp, dnp->dn_expr, sdp);
1122 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1125 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1128 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1132 dt_action_ustack(dtp, dnp->dn_expr, sdp);
1135 dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1136 "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1141 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1143 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
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;
1152 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1154 dt_ident_t *aid, *fid;
1155 dt_node_t *anp, *incr = NULL;
1156 dtrace_actdesc_t *ap;
1157 uint_t n = 1, argmax;
1161 * If the aggregation has no aggregating function applied to it, then
1162 * this statement has no effect. Flag this as a programming error.
1164 if (dnp->dn_aggfun == NULL) {
1165 dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1166 dnp->dn_ident->di_name);
1169 aid = dnp->dn_ident;
1170 fid = dnp->dn_aggfun->dn_ident;
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);
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.
1182 ap = dt_stmt_action(dtp, sdp);
1183 dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1185 for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1186 ap = dt_stmt_action(dtp, sdp);
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);
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);
1201 switch (anp->dn_ident->di_id) {
1203 dt_action_symmod_args(dtp, ap,
1204 anp->dn_args, DTRACEACT_UADDR);
1208 dt_action_symmod_args(dtp, ap,
1209 anp->dn_args, DTRACEACT_USYM);
1213 dt_action_symmod_args(dtp, ap,
1214 anp->dn_args, DTRACEACT_UMOD);
1218 dt_action_symmod_args(dtp, ap,
1219 anp->dn_args, DTRACEACT_SYM);
1223 dt_action_symmod_args(dtp, ap,
1224 anp->dn_args, DTRACEACT_MOD);
1233 ap->dtad_difo = dt_as(yypcb);
1234 ap->dtad_kind = DTRACEACT_DIFEXPR;
1237 if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1239 * For linear quantization, we have between two and four
1240 * arguments in addition to the expression:
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)
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;
1251 uint64_t nlevels, step = 1, oarg;
1252 int64_t baseval, limitval;
1254 if (arg1->dn_kind != DT_NODE_INT) {
1255 dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1256 "argument #1 must be an integer constant\n");
1259 baseval = (int64_t)arg1->dn_value;
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");
1266 if (arg2->dn_kind != DT_NODE_INT) {
1267 dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1268 "argument #2 must be an integer constant\n");
1271 limitval = (int64_t)arg2->dn_value;
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");
1278 if (limitval < baseval) {
1279 dnerror(dnp, D_LQUANT_MISMATCH,
1280 "lquantize( ) base (argument #1) must be less "
1281 "than limit (argument #2)\n");
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");
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");
1297 nlevels = (limitval - baseval) / step;
1300 dnerror(dnp, D_LQUANT_STEPLARGE,
1301 "lquantize( ) step (argument #3) too large: must "
1302 "have at least one quantization level\n");
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");
1311 arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1312 (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1313 ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1314 DTRACE_LQUANTIZE_BASEMASK);
1318 isp = (dt_idsig_t *)aid->di_data;
1320 if (isp->dis_auxinfo == 0) {
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.
1326 isp->dis_auxinfo = arg;
1327 } else if ((oarg = isp->dis_auxinfo) != arg) {
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
1334 int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1335 int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1336 int ostep = DTRACE_LQUANTIZE_STEP(oarg);
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);
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);
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",
1361 * We shouldn't be able to get here -- one of the
1362 * parameters must be mismatched if the arguments
1368 incr = arg3 != NULL ? arg3->dn_list : NULL;
1372 if (fid->di_id == DTRACEAGG_QUANTIZE) {
1373 incr = dnp->dn_aggfun->dn_args->dn_list;
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);
1384 if ((anp = incr->dn_list) != NULL) {
1387 for (; anp != NULL; anp = anp->dn_list)
1390 dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1391 "mismatch: %d args passed, at most %d expected",
1392 fid->di_name, argc, argmax);
1395 ap = dt_stmt_action(dtp, sdp);
1399 ap->dtad_difo = dt_as(yypcb);
1400 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1401 ap->dtad_kind = DTRACEACT_DIFEXPR;
1404 assert(sdp->dtsd_aggdata == NULL);
1405 sdp->dtsd_aggdata = aid;
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;
1414 if (dnp->dn_aggfun->dn_args != NULL) {
1415 dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1416 ap->dtad_difo = dt_as(yypcb);
1421 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1423 dtrace_ecbdesc_t *edp;
1424 dtrace_stmtdesc_t *sdp;
1427 yylineno = pnp->dn_line;
1428 dt_setcontext(dtp, pnp->dn_desc);
1429 (void) dt_node_cook(cnp, DT_IDFLG_REF);
1431 if (DT_TREEDUMP_PASS(dtp, 2))
1432 dt_node_printr(cnp, stderr, 0);
1434 if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1435 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1437 assert(yypcb->pcb_ecbdesc == NULL);
1438 yypcb->pcb_ecbdesc = edp;
1440 if (cnp->dn_pred != NULL) {
1441 dt_cg(yypcb, cnp->dn_pred);
1442 edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1445 if (cnp->dn_acts == NULL) {
1446 dt_stmt_append(dt_stmt_create(dtp, edp,
1447 cnp->dn_ctxattr, _dtrace_defattr), cnp);
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);
1454 switch (dnp->dn_kind) {
1456 if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1457 dt_compile_agg(dtp, dnp->dn_expr, sdp);
1459 dt_compile_exp(dtp, dnp, sdp);
1462 dt_compile_fun(dtp, dnp, sdp);
1465 dt_compile_agg(dtp, dnp, sdp);
1468 dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1469 "%u is not a valid statement\n", dnp->dn_kind);
1472 assert(yypcb->pcb_stmt == sdp);
1473 dt_stmt_append(sdp, dnp);
1476 assert(yypcb->pcb_ecbdesc == edp);
1477 dt_ecbdesc_release(dtp, edp);
1479 yypcb->pcb_ecbdesc = NULL;
1483 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1487 for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1488 dt_compile_one_clause(dtp, cnp, pnp);
1492 dt_compile_xlator(dt_node_t *dnp)
1494 dt_xlator_t *dxp = dnp->dn_xlator;
1497 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1498 assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1500 dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1505 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1507 const dtrace_pattr_t *pap;
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
1520 * (1) The provider doesn't exist, or,
1521 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1523 * On an error, dt_pid_create_probes() will set the error message
1524 * and tag -- we just have to longjmp() out of here.
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);
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.
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;
1545 pap = &prp->pr_pvp->pv_desc.dtvd_attr;
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);
1555 if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1556 xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
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);
1564 * Reset the stability attributes of D global variables that vary
1565 * based on the attributes of the provider and context itself.
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;
1578 yypcb->pcb_pdesc = pdp;
1579 yypcb->pcb_probe = prp;
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.
1588 dt_endcontext(dtrace_hdl_t *dtp)
1590 static const char *const cvars[] = {
1591 "probeprov", "probemod", "probefunc", "probename", "args", NULL
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;
1602 yypcb->pcb_pdesc = NULL;
1603 yypcb->pcb_probe = NULL;
1607 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1609 if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1610 dt_idhash_delete(dhp, idp);
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.
1625 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1627 char s[DT_VERSION_STRMAX];
1628 dt_xlator_t *dxp, *nxp;
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 */
1635 dt_dprintf("reducing api version to %s\n",
1636 dt_version_num2str(v, s, sizeof (s)));
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);
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);
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.
1661 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1663 int argc = dtp->dt_cpp_argc;
1664 char **argv = malloc(sizeof (char *) * (argc + 5));
1665 FILE *ofp = tmpfile();
1668 char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1670 char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1672 struct sigaction act, oact;
1673 sigset_t mask, omask;
1684 if (argv == NULL || ofp == NULL) {
1685 (void) dt_set_errno(dtp, errno);
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.
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)
1702 off--; /* start cpp just prior to \n */
1705 (void) fseeko64(ifp, off, SEEK_SET);
1709 (void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1710 (void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1713 bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1715 (void) snprintf(verdef, sizeof (verdef),
1716 "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1717 argv[argc++] = verdef;
1720 switch (dtp->dt_stdcmode) {
1723 argv[argc++] = "-D__STDC__=0";
1726 argv[argc++] = "-D__STDC__=1";
1730 argv[argc++] = ipath;
1731 argv[argc++] = opath;
1733 argv[argc++] = "-P";
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.
1745 (void) sigemptyset(&mask);
1746 (void) sigaddset(&mask, SIGCHLD);
1747 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
1749 bzero(&act, sizeof (act));
1750 act.sa_handler = SIG_DFL;
1751 (void) sigaction(SIGCHLD, &act, &oact);
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);
1762 if (isatty(fileno(ifp)) == 0)
1763 lseek(fileno(ifp), off, SEEK_SET);
1764 dup2(fileno(ifp), 0);
1765 dup2(fileno(ofp), 1);
1767 (void) execvp(dtp->dt_cpp_path, argv);
1768 _exit(errno == ENOENT ? 127 : 126);
1772 dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1774 } while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1776 (void) sigaction(SIGCHLD, &oact, NULL);
1777 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1779 dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1780 estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1785 (void) dt_set_errno(dtp, EDT_CPPEXEC);
1788 (void) dt_set_errno(dtp, EDT_CPPENT);
1791 (void) dt_set_errno(dtp, EDT_CPPERR);
1798 (void) fseek(ofp, 0, SEEK_SET);
1808 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1812 va_start(ap, format);
1813 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1818 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1820 dt_lib_depend_t *dld;
1823 assert(arg != NULL);
1825 if ((end = strrchr(arg, '/')) == NULL)
1826 return (dt_set_errno(dtp, EINVAL));
1828 if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1831 if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
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);
1840 return (dt_set_errno(dtp, EDT_NOMEM));
1843 dt_list_append(dlp, dld);
1848 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1850 dt_lib_depend_t *dldn;
1852 for (dldn = dt_list_next(dld); dldn != NULL;
1853 dldn = dt_list_next(dldn)) {
1854 if (strcmp(dldn->dtld_library, arg) == 0)
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.
1868 dt_lib_build_graph(dtrace_hdl_t *dtp)
1870 dt_lib_depend_t *dld, *dpld;
1872 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1873 dld = dt_list_next(dld)) {
1874 char *library = dld->dtld_library;
1876 for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
1877 dpld = dt_list_next(dpld)) {
1878 dt_lib_depend_t *dlda;
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);
1886 return (dt_set_errno(dtp, EDT_COMPILER));
1889 if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
1891 return (-1); /* preserve dt_errno */
1899 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
1901 dt_lib_depend_t *dpld, *dlda, *new;
1903 dld->dtld_start = ++(*count);
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);
1911 if (dlda->dtld_start == 0 &&
1912 dt_topo_sort(dtp, dlda, count) == -1)
1916 if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1919 if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
1921 return (dt_set_errno(dtp, EDT_NOMEM));
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);
1928 dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
1929 new->dtld_start, new->dtld_finish);
1935 dt_lib_depend_sort(dtrace_hdl_t *dtp)
1937 dt_lib_depend_t *dld, *dpld, *dlda;
1940 if (dt_lib_build_graph(dtp) == -1)
1941 return (-1); /* preserve dt_errno */
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.
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 */;
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.
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);
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);
1973 return (dt_set_errno(dtp, EDT_COMPILER));
1982 dt_lib_depend_free(dtrace_hdl_t *dtp)
1984 dt_lib_depend_t *dld, *dlda;
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);
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);
1999 dt_list_delete(&dtp->dt_lib_dep, dld);
2000 dt_free(dtp, dld->dtld_library);
2001 dt_free(dtp, dld->dtld_libpath);
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);
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.
2023 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2029 char fname[PATH_MAX];
2033 dt_lib_depend_t *dld;
2035 if ((dirp = opendir(path)) == NULL) {
2036 dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
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 */
2045 (void) snprintf(fname, sizeof (fname),
2046 "%s/%s", path, dp->d_name);
2048 if ((fp = fopen(fname, "r")) == NULL) {
2049 dt_dprintf("skipping library %s: %s\n",
2050 fname, strerror(errno));
2054 dtp->dt_filetag = fname;
2055 if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
2058 rv = dt_compile(dtp, DT_CTX_DPROG,
2059 DTRACE_PROBESPEC_NAME, NULL,
2060 DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2062 if (rv != NULL && dtp->dt_errno &&
2063 (dtp->dt_errno != EDT_COMPILER ||
2064 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2068 dt_dprintf("error parsing library %s: %s\n",
2069 fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2072 dtp->dt_filetag = NULL;
2075 (void) closedir(dirp);
2077 * Finish building the graph containing the library dependencies
2078 * and perform a topological sort to generate an ordered list
2081 if (dt_lib_depend_sort(dtp) == -1)
2084 for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2085 dld = dt_list_next(dld)) {
2087 if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2088 dt_dprintf("skipping library %s: %s\n",
2089 dld->dtld_library, strerror(errno));
2093 dtp->dt_filetag = dld->dtld_library;
2094 pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2096 dtp->dt_filetag = NULL;
2098 if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2099 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2103 dt_dprintf("skipping library %s: %s\n",
2105 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2107 dld->dtld_loaded = B_TRUE;
2108 dt_program_destroy(dtp, pgp);
2112 dt_lib_depend_free(dtp);
2116 dt_lib_depend_free(dtp);
2117 return (-1); /* preserve dt_errno */
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.
2127 dt_load_libs(dtrace_hdl_t *dtp)
2131 if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2132 return (0); /* libraries already processed */
2134 dtp->dt_cflags |= DTRACE_C_NOLIBS;
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 */
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)
2157 if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2158 (void) dt_set_errno(dtp, EINVAL);
2162 if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2163 return (NULL); /* errno is set for us */
2165 if (dtp->dt_globals->dh_nelems != 0)
2166 (void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2168 if (dtp->dt_tls->dh_nelems != 0)
2169 (void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2171 if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2172 return (NULL); /* errno is set for us */
2174 dt_pcb_push(dtp, &pcb);
2176 pcb.pcb_fileptr = fp;
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;
2190 if (context != DT_CTX_DPROG)
2192 else if (cflags & DTRACE_C_CTL)
2193 yybegin(YYS_CONTROL);
2195 yybegin(YYS_CLAUSE);
2197 if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2200 if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2201 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
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);
2207 if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2208 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
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.
2216 if (yyparse() != 0 || yypcb->pcb_root == NULL)
2217 xyerror(D_EMPTY, "empty D program translation unit\n");
2221 if (cflags & DTRACE_C_CTL)
2224 if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2225 dt_node_printr(yypcb->pcb_root, stderr, 0);
2227 if (yypcb->pcb_pragmas != NULL)
2228 (void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
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);
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.
2244 assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
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");
2250 if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2251 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2253 for (; dnp != NULL; dnp = dnp->dn_list) {
2254 switch (dnp->dn_kind) {
2255 case DT_NODE_CLAUSE:
2256 dt_compile_clause(dtp, dnp);
2258 case DT_NODE_XLATOR:
2259 if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2260 dt_compile_xlator(dnp);
2262 case DT_NODE_PROVIDER:
2263 (void) dt_node_cook(dnp, DT_IDFLG_REF);
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;
2273 rv = yypcb->pcb_prog;
2277 (void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2278 dt_cg(yypcb, yypcb->pcb_root);
2283 ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2284 err = dt_decl_type(ddp, arg);
2288 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2295 if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 3))
2296 dt_node_printr(yypcb->pcb_root, stderr, 0);
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));
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));
2308 if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2309 (void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2311 dt_pcb_pop(dtp, err);
2312 (void) dt_set_errno(dtp, err);
2313 return (err ? NULL : rv);
2317 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2318 dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2320 return (dt_compile(dtp, DT_CTX_DPROG,
2321 spec, NULL, cflags, argc, argv, NULL, s));
2325 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2326 uint_t cflags, int argc, char *const argv[])
2328 return (dt_compile(dtp, DT_CTX_DPROG,
2329 DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2333 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
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);
2341 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
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);