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.
24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * DTrace D Language Compiler
31 * The code in this source file implements the main engine for the D language
32 * compiler. The driver routine for the compiler is dt_compile(), below. The
33 * compiler operates on either stdio FILEs or in-memory strings as its input
34 * and can produce either dtrace_prog_t structures from a D program or a single
35 * dtrace_difo_t structure from a D expression. Multiple entry points are
36 * provided as wrappers around dt_compile() for the various input/output pairs.
37 * The compiler itself is implemented across the following source files:
39 * dt_lex.l - lex scanner
40 * dt_grammar.y - yacc grammar
41 * dt_parser.c - parse tree creation and semantic checking
42 * dt_decl.c - declaration stack processing
43 * dt_xlator.c - D translator lookup and creation
44 * dt_ident.c - identifier and symbol table routines
45 * dt_pragma.c - #pragma processing and D pragmas
46 * dt_printf.c - D printf() and printa() argument checking and processing
47 * dt_cc.c - compiler driver and dtrace_prog_t construction
48 * dt_cg.c - DIF code generator
49 * dt_as.c - DIF assembler
50 * dt_dof.c - dtrace_prog_t -> DOF conversion
52 * Several other source files provide collections of utility routines used by
53 * these major files. The compiler itself is implemented in multiple passes:
55 * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
56 * and parse tree nodes are constructed using the routines in dt_parser.c.
57 * This node construction pass is described further in dt_parser.c.
59 * (2) The parse tree is "cooked" by assigning each clause a context (see the
60 * routine dt_setcontext(), below) based on its probe description and then
61 * recursively descending the tree performing semantic checking. The cook
62 * routines are also implemented in dt_parser.c and described there.
64 * (3) For actions that are DIF expression statements, the DIF code generator
65 * and assembler are invoked to create a finished DIFO for the statement.
67 * (4) The dtrace_prog_t data structures for the program clauses and actions
68 * are built, containing pointers to any DIFOs created in step (3).
70 * (5) The caller invokes a routine in dt_dof.c to convert the finished program
71 * into DOF format for use in anonymous tracing or enabling in the kernel.
73 * In the implementation, steps 2-4 are intertwined in that they are performed
74 * in order for each clause as part of a loop that executes over the clauses.
76 * The D compiler currently implements nearly no optimization. The compiler
77 * implements integer constant folding as part of pass (1), and a set of very
78 * simple peephole optimizations as part of pass (3). As with any C compiler,
79 * a large number of optimizations are possible on both the intermediate data
80 * structures and the generated DIF code. These possibilities should be
81 * investigated in the context of whether they will have any substantive effect
82 * on the overall DTrace probe effect before they are undertaken.
85 #include <sys/types.h>
87 #include <sys/sysmacros.h>
101 #include <dt_module.h>
102 #include <dt_program.h>
103 #include <dt_provider.h>
104 #include <dt_printf.h>
106 #include <dt_grammar.h>
107 #include <dt_ident.h>
108 #include <dt_string.h>
111 static const dtrace_diftype_t dt_void_rtype = {
112 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
115 static const dtrace_diftype_t dt_int_rtype = {
116 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
119 static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120 uint_t, int, char *const[], FILE *, const char *);
125 dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
127 idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
128 DT_IDFLG_DIFR | DT_IDFLG_DIFW);
134 dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
136 yylineno = idp->di_lineno;
137 xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
141 static dtrace_stmtdesc_t *
142 dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
143 dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
145 dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
148 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
150 assert(yypcb->pcb_stmt == NULL);
151 yypcb->pcb_stmt = sdp;
153 sdp->dtsd_descattr = descattr;
154 sdp->dtsd_stmtattr = stmtattr;
159 static dtrace_actdesc_t *
160 dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
162 dtrace_actdesc_t *new;
164 if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
165 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
171 * Utility function to determine if a given action description is destructive.
172 * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
175 dt_action_destructive(const dtrace_actdesc_t *ap)
177 return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
178 DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
182 dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
184 dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
185 dtrace_actdesc_t *ap, *tap;
191 * Make sure that the new statement jibes with the rest of the ECB.
193 for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
194 if (ap->dtad_kind == DTRACEACT_COMMIT) {
196 dnerror(dnp, D_COMM_COMM, "commit( ) may "
197 "not follow commit( )\n");
201 dnerror(dnp, D_COMM_DREC, "commit( ) may "
202 "not follow data-recording action(s)\n");
205 for (tap = ap; tap != NULL; tap = tap->dtad_next) {
206 if (!DTRACEACT_ISAGG(tap->dtad_kind))
209 dnerror(dnp, D_AGG_COMM, "aggregating actions "
210 "may not follow commit( )\n");
217 if (ap->dtad_kind == DTRACEACT_SPECULATE) {
219 dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
220 "not follow speculate( )\n");
224 dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
225 "not follow commit( )\n");
229 dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
230 "not follow data-recording action(s)\n");
237 if (DTRACEACT_ISAGG(ap->dtad_kind)) {
239 dnerror(dnp, D_AGG_SPEC, "aggregating actions "
240 "may not follow speculate( )\n");
248 if (dt_action_destructive(ap)) {
249 dnerror(dnp, D_ACT_SPEC, "destructive actions "
250 "may not follow speculate( )\n");
253 if (ap->dtad_kind == DTRACEACT_EXIT) {
254 dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
255 "follow speculate( )\n");
260 * Exclude all non data-recording actions.
262 if (dt_action_destructive(ap) ||
263 ap->dtad_kind == DTRACEACT_DISCARD)
266 if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
267 ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
268 ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
272 dnerror(dnp, D_DREC_COMM, "data-recording actions "
273 "may not follow commit( )\n");
280 if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
281 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
283 if (yypcb->pcb_stmt == sdp)
284 yypcb->pcb_stmt = NULL;
288 * For the first element of an aggregation tuple or for printa(), we create a
289 * simple DIF program that simply returns the immediate value that is the ID
290 * of the aggregation itself. This could be optimized in the future by
291 * creating a new in-kernel dtad_kind that just returns an integer.
294 dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
296 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
297 dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
300 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
302 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
303 dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
305 if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
306 dt_difo_free(dtp, dp);
307 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
310 dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx DIF_INTEGER[0], %r1 */
311 dp->dtdo_buf[1] = DIF_INSTR_RET(1); /* ret %r1 */
313 dp->dtdo_inttab[0] = id;
315 dp->dtdo_rtype = dt_int_rtype;
318 ap->dtad_kind = kind;
322 dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
325 dtrace_actdesc_t *ap;
328 char n[DT_TYPE_NAMELEN];
331 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
332 argc++; /* count up arguments for error messages below */
335 dnerror(dnp, D_CLEAR_PROTO,
336 "%s( ) prototype mismatch: %d args passed, 1 expected\n",
337 dnp->dn_ident->di_name, argc);
343 if (anp->dn_kind != DT_NODE_AGG) {
344 dnerror(dnp, D_CLEAR_AGGARG,
345 "%s( ) argument #1 is incompatible with prototype:\n"
346 "\tprototype: aggregation\n\t argument: %s\n",
347 dnp->dn_ident->di_name,
348 dt_node_type_name(anp, n, sizeof (n)));
353 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
354 dnerror(dnp, D_CLEAR_AGGBAD,
355 "undefined aggregation: @%s\n", aid->di_name);
358 ap = dt_stmt_action(dtp, sdp);
359 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
360 ap->dtad_arg = DT_ACT_CLEAR;
364 dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
367 dtrace_actdesc_t *ap;
368 dt_node_t *anp, *normal;
369 int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
371 char n[DT_TYPE_NAMELEN];
374 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
375 argc++; /* count up arguments for error messages below */
377 if ((denormal && argc != 1) || (!denormal && argc != 2)) {
378 dnerror(dnp, D_NORMALIZE_PROTO,
379 "%s( ) prototype mismatch: %d args passed, %d expected\n",
380 dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
386 if (anp->dn_kind != DT_NODE_AGG) {
387 dnerror(dnp, D_NORMALIZE_AGGARG,
388 "%s( ) argument #1 is incompatible with prototype:\n"
389 "\tprototype: aggregation\n\t argument: %s\n",
390 dnp->dn_ident->di_name,
391 dt_node_type_name(anp, n, sizeof (n)));
394 if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
395 dnerror(dnp, D_NORMALIZE_SCALAR,
396 "%s( ) argument #2 must be of scalar type\n",
397 dnp->dn_ident->di_name);
402 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
403 dnerror(dnp, D_NORMALIZE_AGGBAD,
404 "undefined aggregation: @%s\n", aid->di_name);
407 ap = dt_stmt_action(dtp, sdp);
408 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
411 ap->dtad_arg = DT_ACT_DENORMALIZE;
415 ap->dtad_arg = DT_ACT_NORMALIZE;
417 assert(normal != NULL);
418 ap = dt_stmt_action(dtp, sdp);
419 dt_cg(yypcb, normal);
421 ap->dtad_difo = dt_as(yypcb);
422 ap->dtad_kind = DTRACEACT_LIBACT;
423 ap->dtad_arg = DT_ACT_NORMALIZE;
427 dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
430 dtrace_actdesc_t *ap;
431 dt_node_t *anp, *trunc;
433 char n[DT_TYPE_NAMELEN];
436 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
437 argc++; /* count up arguments for error messages below */
439 if (argc > 2 || argc < 1) {
440 dnerror(dnp, D_TRUNC_PROTO,
441 "%s( ) prototype mismatch: %d args passed, %s expected\n",
442 dnp->dn_ident->di_name, argc,
443 argc < 1 ? "at least 1" : "no more than 2");
448 trunc = anp->dn_list;
450 if (anp->dn_kind != DT_NODE_AGG) {
451 dnerror(dnp, D_TRUNC_AGGARG,
452 "%s( ) argument #1 is incompatible with prototype:\n"
453 "\tprototype: aggregation\n\t argument: %s\n",
454 dnp->dn_ident->di_name,
455 dt_node_type_name(anp, n, sizeof (n)));
459 assert(trunc != NULL);
460 if (!dt_node_is_scalar(trunc)) {
461 dnerror(dnp, D_TRUNC_SCALAR,
462 "%s( ) argument #2 must be of scalar type\n",
463 dnp->dn_ident->di_name);
469 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
470 dnerror(dnp, D_TRUNC_AGGBAD,
471 "undefined aggregation: @%s\n", aid->di_name);
474 ap = dt_stmt_action(dtp, sdp);
475 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
476 ap->dtad_arg = DT_ACT_TRUNC;
478 ap = dt_stmt_action(dtp, sdp);
481 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
483 assert(trunc != NULL);
485 ap->dtad_difo = dt_as(yypcb);
486 ap->dtad_kind = DTRACEACT_LIBACT;
489 ap->dtad_arg = DT_ACT_TRUNC;
493 dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
495 dt_ident_t *aid, *fid;
496 dtrace_actdesc_t *ap;
498 dt_node_t *anp, *proto = NULL;
500 char n[DT_TYPE_NAMELEN];
501 int argc = 0, argr = 0;
503 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
504 argc++; /* count up arguments for error messages below */
506 switch (dnp->dn_args->dn_kind) {
508 format = dnp->dn_args->dn_string;
509 anp = dnp->dn_args->dn_list;
524 dnerror(dnp, D_PRINTA_PROTO,
525 "%s( ) prototype mismatch: %d args passed, %d expected\n",
526 dnp->dn_ident->di_name, argc, argr);
531 while (anp != NULL) {
532 if (anp->dn_kind != DT_NODE_AGG) {
533 dnerror(dnp, D_PRINTA_AGGARG,
534 "%s( ) argument #%d is incompatible with "
535 "prototype:\n\tprototype: aggregation\n"
536 "\t argument: %s\n", dnp->dn_ident->di_name, argr,
537 dt_node_type_name(anp, n, sizeof (n)));
543 if (aid->di_gen == dtp->dt_gen &&
544 !(aid->di_flags & DT_IDFLG_MOD)) {
545 dnerror(dnp, D_PRINTA_AGGBAD,
546 "undefined aggregation: @%s\n", aid->di_name);
550 * If we have multiple aggregations, we must be sure that
551 * their key signatures match.
554 dt_printa_validate(proto, anp);
559 if (format != NULL) {
560 yylineno = dnp->dn_line;
563 dt_printf_create(yypcb->pcb_hdl, format);
564 dt_printf_validate(sdp->dtsd_fmtdata,
565 DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
566 fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
570 ap = dt_stmt_action(dtp, sdp);
571 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
579 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
580 dtrace_actkind_t kind)
582 dt_node_t *anp, *arg1;
583 dtrace_actdesc_t *ap = NULL;
584 char n[DT_TYPE_NAMELEN], *str;
586 assert(DTRACEACT_ISPRINTFLIKE(kind));
588 if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
589 dnerror(dnp, D_PRINTF_ARG_FMT,
590 "%s( ) argument #1 is incompatible with prototype:\n"
591 "\tprototype: string constant\n\t argument: %s\n",
592 dnp->dn_ident->di_name,
593 dt_node_type_name(dnp->dn_args, n, sizeof (n)));
596 arg1 = dnp->dn_args->dn_list;
597 yylineno = dnp->dn_line;
598 str = dnp->dn_args->dn_string;
602 * If this is an freopen(), we use an empty string to denote that
603 * stdout should be restored. For other printf()-like actions, an
604 * empty format string is illegal: an empty format string would
605 * result in malformed DOF, and the compiler thus flags an empty
606 * format string as a compile-time error. To avoid propagating the
607 * freopen() special case throughout the system, we simply transpose
608 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
609 * denotes that stdout should be restored.
611 if (kind == DTRACEACT_FREOPEN) {
612 if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
614 * Our sentinel is always an invalid argument to
615 * freopen(), but if it's been manually specified, we
616 * must fail now instead of when the freopen() is
617 * actually evaluated.
619 dnerror(dnp, D_FREOPEN_INVALID,
620 "%s( ) argument #1 cannot be \"%s\"\n",
621 dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
625 str = DT_FREOPEN_RESTORE;
628 sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
630 dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
631 dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
637 if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
638 (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
640 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
643 dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
647 dp->dtdo_rtype = dt_int_rtype;
649 ap = dt_stmt_action(dtp, sdp);
651 ap->dtad_kind = kind;
655 for (anp = arg1; anp != NULL; anp = anp->dn_list) {
656 ap = dt_stmt_action(dtp, sdp);
658 ap->dtad_difo = dt_as(yypcb);
659 ap->dtad_kind = kind;
664 dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
666 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
667 boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
668 const char *act = istrace ? "trace" : "print";
670 if (dt_node_is_void(dnp->dn_args)) {
671 dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
672 "%s( ) may not be applied to a void expression\n", act);
675 if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
676 dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
677 "%s( ) may not be applied to a translated pointer\n", act);
680 if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
681 dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
682 "%s( ) may not be applied to an aggregation%s\n", act,
683 istrace ? "" : " -- did you mean printa()?");
686 dt_cg(yypcb, dnp->dn_args);
689 * The print() action behaves identically to trace(), except that it
690 * stores the CTF type of the argument (if present) within the DOF for
691 * the DIFEXPR action. To do this, we set the 'dtsd_strdata' to point
692 * to the fully-qualified CTF type ID for the result of the DIF
693 * action. We use the ID instead of the name to handles complex types
694 * like arrays and function pointers that can't be resolved by
695 * ctf_type_lookup(). This is later processed by dtrace_dof_create()
696 * and turned into a reference into the string table so that we can
697 * get the type information when we process the data after the fact.
699 if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
704 dret = yypcb->pcb_dret;
705 dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
707 n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name, dret->dn_type) + 1;
708 sdp->dtsd_strdata = dt_alloc(dtp, n);
709 if (sdp->dtsd_strdata == NULL)
710 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
711 (void) snprintf(sdp->dtsd_strdata, n, "%s`%ld", dmp->dm_name,
715 ap->dtad_difo = dt_as(yypcb);
716 ap->dtad_kind = DTRACEACT_DIFEXPR;
720 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
722 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
724 dt_node_t *addr = dnp->dn_args;
725 dt_node_t *max = dnp->dn_args->dn_list;
728 char n[DT_TYPE_NAMELEN];
730 if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
731 dnerror(addr, D_TRACEMEM_ADDR,
732 "tracemem( ) argument #1 is incompatible with "
733 "prototype:\n\tprototype: pointer or integer\n"
735 dt_node_type_name(addr, n, sizeof (n)));
738 if (dt_node_is_posconst(max) == 0) {
739 dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
740 "be a non-zero positive integral constant expression\n");
743 if ((size = max->dn_list) != NULL) {
744 if (size->dn_list != NULL) {
745 dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
746 "mismatch: expected at most 3 args\n");
749 if (!dt_node_is_scalar(size)) {
750 dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
751 "dynamic size (argument #3) must be of "
756 ap->dtad_difo = dt_as(yypcb);
757 ap->dtad_difo->dtdo_rtype = dt_int_rtype;
758 ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
760 ap = dt_stmt_action(dtp, sdp);
764 ap->dtad_difo = dt_as(yypcb);
765 ap->dtad_kind = DTRACEACT_TRACEMEM;
767 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
768 ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
772 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
774 ap->dtad_kind = DTRACEACT_STACK;
776 if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
777 ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
783 if (arg0->dn_list != NULL) {
784 dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
785 "mismatch: too many arguments\n");
788 if (dt_node_is_posconst(arg0) == 0) {
789 dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
790 "non-zero positive integral constant expression\n");
793 ap->dtad_arg = arg0->dn_value;
798 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
800 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
801 dt_action_stack_args(dtp, ap, dnp->dn_args);
805 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
807 uint32_t nframes = 0;
808 uint32_t strsize = 0; /* default string table size */
809 dt_node_t *arg0 = dnp->dn_args;
810 dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
812 assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
813 dnp->dn_ident->di_id == DT_ACT_USTACK);
815 if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
816 if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
817 nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
819 if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
820 strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
822 ap->dtad_kind = DTRACEACT_JSTACK;
824 assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
826 if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
827 nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
829 ap->dtad_kind = DTRACEACT_USTACK;
833 if (!dt_node_is_posconst(arg0)) {
834 dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
835 "must be a non-zero positive integer constant\n");
837 nframes = (uint32_t)arg0->dn_value;
841 if (arg1->dn_kind != DT_NODE_INT ||
842 ((arg1->dn_flags & DT_NF_SIGNED) &&
843 (int64_t)arg1->dn_value < 0)) {
844 dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
845 "must be a positive integer constant\n");
848 if (arg1->dn_list != NULL) {
849 dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
850 "mismatch: too many arguments\n");
853 strsize = (uint32_t)arg1->dn_value;
856 ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
860 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
862 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
863 dt_action_ustack_args(dtp, ap, dnp);
867 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
869 dtrace_actdesc_t *ap;
870 dt_node_t *arg0, *arg1;
873 * The prototype guarantees that we are called with either one or
874 * two arguments, and that any arguments that are present are strings.
877 arg1 = arg0->dn_list;
879 ap = dt_stmt_action(dtp, sdp);
881 ap->dtad_difo = dt_as(yypcb);
882 ap->dtad_kind = DTRACEACT_LIBACT;
883 ap->dtad_arg = DT_ACT_SETOPT;
885 ap = dt_stmt_action(dtp, sdp);
888 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
891 ap->dtad_difo = dt_as(yypcb);
892 ap->dtad_kind = DTRACEACT_LIBACT;
895 ap->dtad_arg = DT_ACT_SETOPT;
900 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
901 dt_node_t *dnp, dtrace_actkind_t kind)
903 assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
904 kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
905 kind == DTRACEACT_UADDR);
908 ap->dtad_difo = dt_as(yypcb);
909 ap->dtad_kind = kind;
910 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
914 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
915 dtrace_actkind_t kind)
917 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
918 dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
923 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
925 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
928 * Library actions need a DIFO that serves as an argument. As
929 * ftruncate() doesn't take an argument, we generate the constant 0
930 * in a DIFO; this constant will be ignored when the ftruncate() is
933 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
934 ap->dtad_arg = DT_ACT_FTRUNCATE;
939 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
941 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
943 ap->dtad_kind = DTRACEACT_STOP;
949 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
951 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
953 ap->dtad_kind = DTRACEACT_BREAKPOINT;
959 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
961 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
963 ap->dtad_kind = DTRACEACT_PANIC;
968 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
970 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
972 dt_cg(yypcb, dnp->dn_args);
973 ap->dtad_difo = dt_as(yypcb);
974 ap->dtad_kind = DTRACEACT_CHILL;
978 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
980 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
982 dt_cg(yypcb, dnp->dn_args);
983 ap->dtad_difo = dt_as(yypcb);
984 ap->dtad_kind = DTRACEACT_RAISE;
988 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
990 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
992 dt_cg(yypcb, dnp->dn_args);
993 ap->dtad_difo = dt_as(yypcb);
994 ap->dtad_kind = DTRACEACT_EXIT;
995 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
999 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1001 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1003 dt_cg(yypcb, dnp->dn_args);
1004 ap->dtad_difo = dt_as(yypcb);
1005 ap->dtad_kind = DTRACEACT_SPECULATE;
1009 dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1011 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1013 dt_node_t *size = dnp->dn_args;
1014 dt_node_t *addr = dnp->dn_args->dn_list;
1016 char n[DT_TYPE_NAMELEN];
1018 if (dt_node_is_posconst(size) == 0) {
1019 dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
1020 "be a non-zero positive integral constant expression\n");
1023 if (dt_node_is_pointer(addr) == 0) {
1024 dnerror(addr, D_PRINTM_ADDR,
1025 "printm( ) argument #2 is incompatible with "
1026 "prototype:\n\tprototype: pointer\n"
1027 "\t argument: %s\n",
1028 dt_node_type_name(addr, n, sizeof (n)));
1032 ap->dtad_difo = dt_as(yypcb);
1033 ap->dtad_kind = DTRACEACT_PRINTM;
1035 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1036 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
1040 dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1042 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1044 dt_node_t *size = dnp->dn_args;
1045 dt_node_t *addr = dnp->dn_args->dn_list;
1047 char n[DT_TYPE_NAMELEN];
1049 if (dt_node_is_posconst(size) == 0) {
1050 dnerror(size, D_PRINTT_SIZE, "printt( ) argument #1 must "
1051 "be a non-zero positive integral constant expression\n");
1054 if (addr == NULL || addr->dn_kind != DT_NODE_FUNC ||
1055 addr->dn_ident != dt_idhash_lookup(dtp->dt_globals, "typeref")) {
1056 dnerror(addr, D_PRINTT_ADDR,
1057 "printt( ) argument #2 is incompatible with "
1058 "prototype:\n\tprototype: typeref()\n"
1059 "\t argument: %s\n",
1060 dt_node_type_name(addr, n, sizeof (n)));
1064 ap->dtad_difo = dt_as(yypcb);
1065 ap->dtad_kind = DTRACEACT_PRINTT;
1067 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1070 * Allow additional buffer space for the data size, type size,
1071 * type string length and a stab in the dark (32 bytes) for the
1072 * type string. The type string is part of the typeref() that
1073 * this action references.
1075 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1080 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1082 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1084 dt_cg(yypcb, dnp->dn_args);
1085 ap->dtad_difo = dt_as(yypcb);
1086 ap->dtad_kind = DTRACEACT_COMMIT;
1090 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1092 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1094 dt_cg(yypcb, dnp->dn_args);
1095 ap->dtad_difo = dt_as(yypcb);
1096 ap->dtad_kind = DTRACEACT_DISCARD;
1100 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1102 switch (dnp->dn_expr->dn_ident->di_id) {
1103 case DT_ACT_BREAKPOINT:
1104 dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1107 dt_action_chill(dtp, dnp->dn_expr, sdp);
1110 dt_action_clear(dtp, dnp->dn_expr, sdp);
1113 dt_action_commit(dtp, dnp->dn_expr, sdp);
1115 case DT_ACT_DENORMALIZE:
1116 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1118 case DT_ACT_DISCARD:
1119 dt_action_discard(dtp, dnp->dn_expr, sdp);
1122 dt_action_exit(dtp, dnp->dn_expr, sdp);
1124 case DT_ACT_FREOPEN:
1125 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1127 case DT_ACT_FTRUNCATE:
1128 dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1131 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1133 case DT_ACT_NORMALIZE:
1134 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1137 dt_action_panic(dtp, dnp->dn_expr, sdp);
1140 dt_action_trace(dtp, dnp->dn_expr, sdp);
1143 dt_action_printa(dtp, dnp->dn_expr, sdp);
1146 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1149 dt_action_printm(dtp, dnp->dn_expr, sdp);
1152 dt_action_printt(dtp, dnp->dn_expr, sdp);
1155 dt_action_raise(dtp, dnp->dn_expr, sdp);
1158 dt_action_setopt(dtp, dnp->dn_expr, sdp);
1160 case DT_ACT_SPECULATE:
1161 dt_action_speculate(dtp, dnp->dn_expr, sdp);
1164 dt_action_stack(dtp, dnp->dn_expr, sdp);
1167 dt_action_stop(dtp, dnp->dn_expr, sdp);
1170 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1173 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1176 dt_action_trace(dtp, dnp->dn_expr, sdp);
1178 case DT_ACT_TRACEMEM:
1179 dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1182 dt_action_trunc(dtp, dnp->dn_expr, sdp);
1185 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1188 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1191 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1195 dt_action_ustack(dtp, dnp->dn_expr, sdp);
1198 dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1199 "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1204 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1206 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1208 dt_cg(yypcb, dnp->dn_expr);
1209 ap->dtad_difo = dt_as(yypcb);
1210 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1211 ap->dtad_kind = DTRACEACT_DIFEXPR;
1215 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1217 dt_ident_t *aid, *fid;
1218 dt_node_t *anp, *incr = NULL;
1219 dtrace_actdesc_t *ap;
1220 uint_t n = 1, argmax;
1224 * If the aggregation has no aggregating function applied to it, then
1225 * this statement has no effect. Flag this as a programming error.
1227 if (dnp->dn_aggfun == NULL) {
1228 dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1229 dnp->dn_ident->di_name);
1232 aid = dnp->dn_ident;
1233 fid = dnp->dn_aggfun->dn_ident;
1235 if (dnp->dn_aggfun->dn_args != NULL &&
1236 dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1237 dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1238 "be of scalar type\n", fid->di_name);
1242 * The ID of the aggregation itself is implicitly recorded as the first
1243 * member of each aggregation tuple so we can distinguish them later.
1245 ap = dt_stmt_action(dtp, sdp);
1246 dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1248 for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1249 ap = dt_stmt_action(dtp, sdp);
1252 if (anp->dn_kind == DT_NODE_FUNC) {
1253 if (anp->dn_ident->di_id == DT_ACT_STACK) {
1254 dt_action_stack_args(dtp, ap, anp->dn_args);
1258 if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1259 anp->dn_ident->di_id == DT_ACT_JSTACK) {
1260 dt_action_ustack_args(dtp, ap, anp);
1264 switch (anp->dn_ident->di_id) {
1266 dt_action_symmod_args(dtp, ap,
1267 anp->dn_args, DTRACEACT_UADDR);
1271 dt_action_symmod_args(dtp, ap,
1272 anp->dn_args, DTRACEACT_USYM);
1276 dt_action_symmod_args(dtp, ap,
1277 anp->dn_args, DTRACEACT_UMOD);
1281 dt_action_symmod_args(dtp, ap,
1282 anp->dn_args, DTRACEACT_SYM);
1286 dt_action_symmod_args(dtp, ap,
1287 anp->dn_args, DTRACEACT_MOD);
1296 ap->dtad_difo = dt_as(yypcb);
1297 ap->dtad_kind = DTRACEACT_DIFEXPR;
1300 if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1302 * For linear quantization, we have between two and four
1303 * arguments in addition to the expression:
1305 * arg1 => Base value
1306 * arg2 => Limit value
1307 * arg3 => Quantization level step size (defaults to 1)
1308 * arg4 => Quantization increment value (defaults to 1)
1310 dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1311 dt_node_t *arg2 = arg1->dn_list;
1312 dt_node_t *arg3 = arg2->dn_list;
1314 uint64_t nlevels, step = 1, oarg;
1315 int64_t baseval, limitval;
1317 if (arg1->dn_kind != DT_NODE_INT) {
1318 dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1319 "argument #1 must be an integer constant\n");
1322 baseval = (int64_t)arg1->dn_value;
1324 if (baseval < INT32_MIN || baseval > INT32_MAX) {
1325 dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1326 "argument #1 must be a 32-bit quantity\n");
1329 if (arg2->dn_kind != DT_NODE_INT) {
1330 dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1331 "argument #2 must be an integer constant\n");
1334 limitval = (int64_t)arg2->dn_value;
1336 if (limitval < INT32_MIN || limitval > INT32_MAX) {
1337 dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1338 "argument #2 must be a 32-bit quantity\n");
1341 if (limitval < baseval) {
1342 dnerror(dnp, D_LQUANT_MISMATCH,
1343 "lquantize( ) base (argument #1) must be less "
1344 "than limit (argument #2)\n");
1348 if (!dt_node_is_posconst(arg3)) {
1349 dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1350 "argument #3 must be a non-zero positive "
1351 "integer constant\n");
1354 if ((step = arg3->dn_value) > UINT16_MAX) {
1355 dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1356 "argument #3 must be a 16-bit quantity\n");
1360 nlevels = (limitval - baseval) / step;
1363 dnerror(dnp, D_LQUANT_STEPLARGE,
1364 "lquantize( ) step (argument #3) too large: must "
1365 "have at least one quantization level\n");
1368 if (nlevels > UINT16_MAX) {
1369 dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1370 "(argument #3) too small: number of quantization "
1371 "levels must be a 16-bit quantity\n");
1374 arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1375 (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1376 ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1377 DTRACE_LQUANTIZE_BASEMASK);
1381 isp = (dt_idsig_t *)aid->di_data;
1383 if (isp->dis_auxinfo == 0) {
1385 * This is the first time we've seen an lquantize()
1386 * for this aggregation; we'll store our argument
1387 * as the auxiliary signature information.
1389 isp->dis_auxinfo = arg;
1390 } else if ((oarg = isp->dis_auxinfo) != arg) {
1392 * If we have seen this lquantize() before and the
1393 * argument doesn't match the original argument, pick
1394 * the original argument apart to concisely report the
1397 int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1398 int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1399 int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1401 if (obaseval != baseval) {
1402 dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1403 "base (argument #1) doesn't match previous "
1404 "declaration: expected %d, found %d\n",
1405 obaseval, (int)baseval);
1408 if (onlevels * ostep != nlevels * step) {
1409 dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1410 "limit (argument #2) doesn't match previous"
1411 " declaration: expected %d, found %d\n",
1412 obaseval + onlevels * ostep,
1413 (int)baseval + (int)nlevels * (int)step);
1416 if (ostep != step) {
1417 dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1418 "step (argument #3) doesn't match previous "
1419 "declaration: expected %d, found %d\n",
1424 * We shouldn't be able to get here -- one of the
1425 * parameters must be mismatched if the arguments
1431 incr = arg3 != NULL ? arg3->dn_list : NULL;
1435 if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1437 * For log/linear quantizations, we have between one and five
1438 * arguments in addition to the expression:
1441 * arg2 => Low magnitude
1442 * arg3 => High magnitude
1443 * arg4 => Number of steps per magnitude
1444 * arg5 => Quantization increment value (defaults to 1)
1446 dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1447 uint64_t oarg, order, v;
1452 char *str; /* string identifier */
1453 int badtype; /* error on bad type */
1454 int badval; /* error on bad value */
1455 int mismatch; /* error on bad match */
1456 int shift; /* shift value */
1457 uint16_t value; /* value itself */
1459 { "factor", D_LLQUANT_FACTORTYPE,
1460 D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1461 DTRACE_LLQUANTIZE_FACTORSHIFT },
1462 { "low magnitude", D_LLQUANT_LOWTYPE,
1463 D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1464 DTRACE_LLQUANTIZE_LOWSHIFT },
1465 { "high magnitude", D_LLQUANT_HIGHTYPE,
1466 D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1467 DTRACE_LLQUANTIZE_HIGHSHIFT },
1468 { "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1469 D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1470 DTRACE_LLQUANTIZE_NSTEPSHIFT },
1476 for (i = 0; args[i].str != NULL; i++) {
1477 if (llarg->dn_kind != DT_NODE_INT) {
1478 dnerror(llarg, args[i].badtype, "llquantize( ) "
1479 "argument #%d (%s) must be an "
1480 "integer constant\n", i + 1, args[i].str);
1483 if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1484 dnerror(llarg, args[i].badval, "llquantize( ) "
1485 "argument #%d (%s) must be an unsigned "
1486 "16-bit quantity\n", i + 1, args[i].str);
1489 args[i].value = (uint16_t)llarg->dn_value;
1491 assert(!(arg & ((uint64_t)UINT16_MAX <<
1493 arg |= ((uint64_t)args[i].value << args[i].shift);
1494 llarg = llarg->dn_list;
1499 if (args[0].value < 2) {
1500 dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1501 "factor (argument #1) must be two or more\n");
1504 if (args[1].value >= args[2].value) {
1505 dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1506 "high magnitude (argument #3) must be greater "
1507 "than low magnitude (argument #2)\n");
1510 if (args[3].value < args[0].value) {
1511 dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1512 "factor (argument #1) must be less than or "
1513 "equal to the number of linear steps per "
1514 "magnitude (argument #4)\n");
1517 for (v = args[0].value; v < args[3].value; v *= args[0].value)
1520 if ((args[3].value % args[0].value) || (v % args[3].value)) {
1521 dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1522 "factor (argument #1) must evenly divide the "
1523 "number of steps per magnitude (argument #4), "
1524 "and the number of steps per magnitude must evenly "
1525 "divide a power of the factor\n");
1528 for (i = 0, order = 1; i < args[2].value; i++) {
1529 if (order * args[0].value > order) {
1530 order *= args[0].value;
1534 dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1535 "factor (%d) raised to power of high magnitude "
1536 "(%d) overflows 64-bits\n", args[0].value,
1540 isp = (dt_idsig_t *)aid->di_data;
1542 if (isp->dis_auxinfo == 0) {
1544 * This is the first time we've seen an llquantize()
1545 * for this aggregation; we'll store our argument
1546 * as the auxiliary signature information.
1548 isp->dis_auxinfo = arg;
1549 } else if ((oarg = isp->dis_auxinfo) != arg) {
1551 * If we have seen this llquantize() before and the
1552 * argument doesn't match the original argument, pick
1553 * the original argument apart to concisely report the
1556 int expected = 0, found = 0;
1558 for (i = 0; expected == found; i++) {
1559 assert(args[i].str != NULL);
1561 expected = (oarg >> args[i].shift) & UINT16_MAX;
1562 found = (arg >> args[i].shift) & UINT16_MAX;
1565 dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1566 "%s (argument #%d) doesn't match previous "
1567 "declaration: expected %d, found %d\n",
1568 args[i - 1].str, i, expected, found);
1575 if (fid->di_id == DTRACEAGG_QUANTIZE) {
1576 incr = dnp->dn_aggfun->dn_args->dn_list;
1581 if (!dt_node_is_scalar(incr)) {
1582 dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1583 "(argument #%d) must be of scalar type\n",
1584 fid->di_name, argmax);
1587 if ((anp = incr->dn_list) != NULL) {
1590 for (; anp != NULL; anp = anp->dn_list)
1593 dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1594 "mismatch: %d args passed, at most %d expected",
1595 fid->di_name, argc, argmax);
1598 ap = dt_stmt_action(dtp, sdp);
1602 ap->dtad_difo = dt_as(yypcb);
1603 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1604 ap->dtad_kind = DTRACEACT_DIFEXPR;
1607 assert(sdp->dtsd_aggdata == NULL);
1608 sdp->dtsd_aggdata = aid;
1610 ap = dt_stmt_action(dtp, sdp);
1611 assert(fid->di_kind == DT_IDENT_AGGFUNC);
1612 assert(DTRACEACT_ISAGG(fid->di_id));
1613 ap->dtad_kind = fid->di_id;
1614 ap->dtad_ntuple = n;
1617 if (dnp->dn_aggfun->dn_args != NULL) {
1618 dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1619 ap->dtad_difo = dt_as(yypcb);
1624 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1626 dtrace_ecbdesc_t *edp;
1627 dtrace_stmtdesc_t *sdp;
1630 yylineno = pnp->dn_line;
1631 dt_setcontext(dtp, pnp->dn_desc);
1632 (void) dt_node_cook(cnp, DT_IDFLG_REF);
1634 if (DT_TREEDUMP_PASS(dtp, 2))
1635 dt_node_printr(cnp, stderr, 0);
1637 if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1638 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1640 assert(yypcb->pcb_ecbdesc == NULL);
1641 yypcb->pcb_ecbdesc = edp;
1643 if (cnp->dn_pred != NULL) {
1644 dt_cg(yypcb, cnp->dn_pred);
1645 edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1648 if (cnp->dn_acts == NULL) {
1649 dt_stmt_append(dt_stmt_create(dtp, edp,
1650 cnp->dn_ctxattr, _dtrace_defattr), cnp);
1653 for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1654 assert(yypcb->pcb_stmt == NULL);
1655 sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1657 switch (dnp->dn_kind) {
1659 if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1660 dt_compile_agg(dtp, dnp->dn_expr, sdp);
1662 dt_compile_exp(dtp, dnp, sdp);
1665 dt_compile_fun(dtp, dnp, sdp);
1668 dt_compile_agg(dtp, dnp, sdp);
1671 dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1672 "%u is not a valid statement\n", dnp->dn_kind);
1675 assert(yypcb->pcb_stmt == sdp);
1676 dt_stmt_append(sdp, dnp);
1679 assert(yypcb->pcb_ecbdesc == edp);
1680 dt_ecbdesc_release(dtp, edp);
1682 yypcb->pcb_ecbdesc = NULL;
1686 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1690 for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1691 dt_compile_one_clause(dtp, cnp, pnp);
1695 dt_compile_xlator(dt_node_t *dnp)
1697 dt_xlator_t *dxp = dnp->dn_xlator;
1700 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1701 assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1703 dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1708 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1710 const dtrace_pattr_t *pap;
1718 * Both kernel and pid based providers are allowed to have names
1719 * ending with what could be interpreted as a number. We assume it's
1720 * a pid and that we may need to dynamically create probes for
1723 * (1) The provider doesn't exist, or,
1724 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1726 * On an error, dt_pid_create_probes() will set the error message
1727 * and tag -- we just have to longjmp() out of here.
1729 if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1730 ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1731 pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1732 dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1733 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1737 * Call dt_probe_info() to get the probe arguments and attributes. If
1738 * a representative probe is found, set 'pap' to the probe provider's
1739 * attributes. Otherwise set 'pap' to default Unstable attributes.
1741 if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1742 pap = &_dtrace_prvdesc;
1743 err = dtrace_errno(dtp);
1744 bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1745 yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1746 yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1748 pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1752 if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1753 xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1754 "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1755 pdp->dtpd_func, pdp->dtpd_name);
1758 if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1759 xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1761 dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1762 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1763 pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1764 attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1767 * Reset the stability attributes of D global variables that vary
1768 * based on the attributes of the provider and context itself.
1770 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1771 idp->di_attr = pap->dtpa_provider;
1772 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1773 idp->di_attr = pap->dtpa_mod;
1774 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1775 idp->di_attr = pap->dtpa_func;
1776 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1777 idp->di_attr = pap->dtpa_name;
1778 if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1779 idp->di_attr = pap->dtpa_args;
1781 yypcb->pcb_pdesc = pdp;
1782 yypcb->pcb_probe = prp;
1786 * Reset context-dependent variables and state at the end of cooking a D probe
1787 * definition clause. This ensures that external declarations between clauses
1788 * do not reference any stale context-dependent data from the previous clause.
1791 dt_endcontext(dtrace_hdl_t *dtp)
1793 static const char *const cvars[] = {
1794 "probeprov", "probemod", "probefunc", "probename", "args", NULL
1800 for (i = 0; cvars[i] != NULL; i++) {
1801 if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1802 idp->di_attr = _dtrace_defattr;
1805 yypcb->pcb_pdesc = NULL;
1806 yypcb->pcb_probe = NULL;
1810 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1812 if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1813 dt_idhash_delete(dhp, idp);
1819 * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1820 * any identifiers or translators that have been previously defined as bound to
1821 * a version greater than the specified version. Therefore, in our current
1822 * version implementation, establishing a binding is a one-way transformation.
1823 * In addition, no versioning is currently provided for types as our .d library
1824 * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1825 * for our exclusive use. If required, type versioning will require more work.
1828 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1830 char s[DT_VERSION_STRMAX];
1831 dt_xlator_t *dxp, *nxp;
1833 if (v > dtp->dt_vmax)
1834 return (dt_set_errno(dtp, EDT_VERSREDUCED));
1835 else if (v == dtp->dt_vmax)
1836 return (0); /* no reduction necessary */
1838 dt_dprintf("reducing api version to %s\n",
1839 dt_version_num2str(v, s, sizeof (s)));
1843 for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1844 nxp = dt_list_next(dxp);
1845 if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1846 (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1847 dt_list_delete(&dtp->dt_xlators, dxp);
1850 (void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1851 (void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1852 (void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1853 (void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1859 * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1860 * and return a FILE handle for the cpp output. We use the /dev/fd filesystem
1861 * here to simplify the code by leveraging file descriptor inheritance.
1864 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1866 int argc = dtp->dt_cpp_argc;
1867 char **argv = malloc(sizeof (char *) * (argc + 5));
1868 FILE *ofp = tmpfile();
1871 char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1873 char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1875 struct sigaction act, oact;
1876 sigset_t mask, omask;
1887 if (argv == NULL || ofp == NULL) {
1888 (void) dt_set_errno(dtp, errno);
1893 * If the input is a seekable file, see if it is an interpreter file.
1894 * If we see #!, seek past the first line because cpp will choke on it.
1895 * We start cpp just prior to the \n at the end of this line so that
1896 * it still sees the newline, ensuring that #line values are correct.
1898 if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1899 if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1900 for (off += 2; c != '\n'; off++) {
1901 if ((c = fgetc(ifp)) == EOF)
1905 off--; /* start cpp just prior to \n */
1908 (void) fseeko64(ifp, off, SEEK_SET);
1912 (void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1913 (void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1916 bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1918 (void) snprintf(verdef, sizeof (verdef),
1919 "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1920 argv[argc++] = verdef;
1923 switch (dtp->dt_stdcmode) {
1926 argv[argc++] = "-D__STDC__=0";
1929 argv[argc++] = "-D__STDC__=1";
1933 argv[argc++] = ipath;
1934 argv[argc++] = opath;
1936 argv[argc++] = "-P";
1941 * libdtrace must be able to be embedded in other programs that may
1942 * include application-specific signal handlers. Therefore, if we
1943 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1944 * that could confuse the containing application. To do this,
1945 * we block SIGCHLD and reset its disposition to SIG_DFL.
1946 * We restore our signal state once we are done.
1948 (void) sigemptyset(&mask);
1949 (void) sigaddset(&mask, SIGCHLD);
1950 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
1952 bzero(&act, sizeof (act));
1953 act.sa_handler = SIG_DFL;
1954 (void) sigaction(SIGCHLD, &act, &oact);
1956 if ((pid = fork1()) == -1) {
1957 (void) sigaction(SIGCHLD, &oact, NULL);
1958 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1959 (void) dt_set_errno(dtp, EDT_CPPFORK);
1965 if (isatty(fileno(ifp)) == 0)
1966 lseek(fileno(ifp), off, SEEK_SET);
1967 dup2(fileno(ifp), 0);
1968 dup2(fileno(ofp), 1);
1970 (void) execvp(dtp->dt_cpp_path, argv);
1971 _exit(errno == ENOENT ? 127 : 126);
1975 dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1977 } while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1979 (void) sigaction(SIGCHLD, &oact, NULL);
1980 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1982 dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1983 estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1988 (void) dt_set_errno(dtp, EDT_CPPEXEC);
1991 (void) dt_set_errno(dtp, EDT_CPPENT);
1994 (void) dt_set_errno(dtp, EDT_CPPERR);
2001 (void) fseek(ofp, 0, SEEK_SET);
2011 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
2015 va_start(ap, format);
2016 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
2021 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
2023 dt_lib_depend_t *dld;
2026 assert(arg != NULL);
2028 if ((end = strrchr(arg, '/')) == NULL)
2029 return (dt_set_errno(dtp, EINVAL));
2031 if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2034 if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
2039 (void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
2040 if ((dld->dtld_library = strdup(arg)) == NULL) {
2041 dt_free(dtp, dld->dtld_libpath);
2043 return (dt_set_errno(dtp, EDT_NOMEM));
2046 dt_list_append(dlp, dld);
2051 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
2053 dt_lib_depend_t *dldn;
2055 for (dldn = dt_list_next(dld); dldn != NULL;
2056 dldn = dt_list_next(dldn)) {
2057 if (strcmp(dldn->dtld_library, arg) == 0)
2065 * Go through all the library files, and, if any library dependencies exist for
2066 * that file, add it to that node's list of dependents. The result of this
2067 * will be a graph which can then be topologically sorted to produce a
2068 * compilation order.
2071 dt_lib_build_graph(dtrace_hdl_t *dtp)
2073 dt_lib_depend_t *dld, *dpld;
2075 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2076 dld = dt_list_next(dld)) {
2077 char *library = dld->dtld_library;
2079 for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2080 dpld = dt_list_next(dpld)) {
2081 dt_lib_depend_t *dlda;
2083 if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2084 dpld->dtld_library)) == NULL) {
2085 dt_lib_depend_error(dtp,
2086 "Invalid library dependency in %s: %s\n",
2087 dld->dtld_library, dpld->dtld_library);
2089 return (dt_set_errno(dtp, EDT_COMPILER));
2092 if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2094 return (-1); /* preserve dt_errno */
2102 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2104 dt_lib_depend_t *dpld, *dlda, *new;
2106 dld->dtld_start = ++(*count);
2108 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2109 dpld = dt_list_next(dpld)) {
2110 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2111 dpld->dtld_library);
2112 assert(dlda != NULL);
2114 if (dlda->dtld_start == 0 &&
2115 dt_topo_sort(dtp, dlda, count) == -1)
2119 if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2122 if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2124 return (dt_set_errno(dtp, EDT_NOMEM));
2127 new->dtld_start = dld->dtld_start;
2128 new->dtld_finish = dld->dtld_finish = ++(*count);
2129 dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2131 dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2132 new->dtld_start, new->dtld_finish);
2138 dt_lib_depend_sort(dtrace_hdl_t *dtp)
2140 dt_lib_depend_t *dld, *dpld, *dlda;
2143 if (dt_lib_build_graph(dtp) == -1)
2144 return (-1); /* preserve dt_errno */
2147 * Perform a topological sort of the graph that hangs off
2148 * dtp->dt_lib_dep. The result of this process will be a
2149 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2151 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2152 dld = dt_list_next(dld)) {
2153 if (dld->dtld_start == 0 &&
2154 dt_topo_sort(dtp, dld, &count) == -1)
2155 return (-1); /* preserve dt_errno */;
2159 * Check the graph for cycles. If an ancestor's finishing time is
2160 * less than any of its dependent's finishing times then a back edge
2161 * exists in the graph and this is a cycle.
2163 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2164 dld = dt_list_next(dld)) {
2165 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2166 dpld = dt_list_next(dpld)) {
2167 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2168 dpld->dtld_library);
2169 assert(dlda != NULL);
2171 if (dlda->dtld_finish > dld->dtld_finish) {
2172 dt_lib_depend_error(dtp,
2173 "Cyclic dependency detected: %s => %s\n",
2174 dld->dtld_library, dpld->dtld_library);
2176 return (dt_set_errno(dtp, EDT_COMPILER));
2185 dt_lib_depend_free(dtrace_hdl_t *dtp)
2187 dt_lib_depend_t *dld, *dlda;
2189 while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2190 while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2191 dt_list_delete(&dld->dtld_dependencies, dlda);
2192 dt_free(dtp, dlda->dtld_library);
2193 dt_free(dtp, dlda->dtld_libpath);
2196 while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2197 dt_list_delete(&dld->dtld_dependents, dlda);
2198 dt_free(dtp, dlda->dtld_library);
2199 dt_free(dtp, dlda->dtld_libpath);
2202 dt_list_delete(&dtp->dt_lib_dep, dld);
2203 dt_free(dtp, dld->dtld_library);
2204 dt_free(dtp, dld->dtld_libpath);
2208 while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2209 dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2210 dt_free(dtp, dld->dtld_library);
2216 * Open all the .d library files found in the specified directory and
2217 * compile each one of them. We silently ignore any missing directories and
2218 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2219 * we fail to compile a library and the error is something other than #pragma D
2220 * depends_on. Dependency errors are silently ignored to permit a library
2221 * directory to contain libraries which may not be accessible depending on our
2225 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2228 const char *p, *end;
2231 char fname[PATH_MAX];
2234 dt_lib_depend_t *dld;
2236 if ((dirp = opendir(path)) == NULL) {
2237 dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2241 /* First, parse each file for library dependencies. */
2242 while ((dp = readdir(dirp)) != NULL) {
2243 if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2244 continue; /* skip any filename not ending in .d */
2246 (void) snprintf(fname, sizeof (fname),
2247 "%s/%s", path, dp->d_name);
2249 if ((fp = fopen(fname, "r")) == NULL) {
2250 dt_dprintf("skipping library %s: %s\n",
2251 fname, strerror(errno));
2256 * Skip files whose name match an already processed library
2258 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2259 dld = dt_list_next(dld)) {
2260 end = strrchr(dld->dtld_library, '/');
2261 /* dt_lib_depend_add ensures this */
2262 assert(end != NULL);
2263 if (strcmp(end + 1, dp->d_name) == 0)
2268 dt_dprintf("skipping library %s, already processed "
2269 "library with the same name: %s", dp->d_name,
2274 dtp->dt_filetag = fname;
2275 if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0)
2276 return (-1); /* preserve dt_errno */
2278 rv = dt_compile(dtp, DT_CTX_DPROG,
2279 DTRACE_PROBESPEC_NAME, NULL,
2280 DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2282 if (rv != NULL && dtp->dt_errno &&
2283 (dtp->dt_errno != EDT_COMPILER ||
2284 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2285 return (-1); /* preserve dt_errno */
2288 dt_dprintf("error parsing library %s: %s\n",
2289 fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2292 dtp->dt_filetag = NULL;
2295 (void) closedir(dirp);
2301 * Perform a topological sorting of all the libraries found across the entire
2302 * dt_lib_path. Once sorted, compile each one in topological order to cache its
2303 * inlines and translators, etc. We silently ignore any missing directories and
2304 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2305 * we fail to compile a library and the error is something other than #pragma D
2306 * depends_on. Dependency errors are silently ignored to permit a library
2307 * directory to contain libraries which may not be accessible depending on our
2311 dt_load_libs_sort(dtrace_hdl_t *dtp)
2315 dt_lib_depend_t *dld;
2318 * Finish building the graph containing the library dependencies
2319 * and perform a topological sort to generate an ordered list
2322 if (dt_lib_depend_sort(dtp) == -1)
2325 for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2326 dld = dt_list_next(dld)) {
2328 if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2329 dt_dprintf("skipping library %s: %s\n",
2330 dld->dtld_library, strerror(errno));
2334 dtp->dt_filetag = dld->dtld_library;
2335 pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2337 dtp->dt_filetag = NULL;
2339 if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2340 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2344 dt_dprintf("skipping library %s: %s\n",
2346 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2348 dld->dtld_loaded = B_TRUE;
2349 dt_program_destroy(dtp, pgp);
2353 dt_lib_depend_free(dtp);
2357 dt_lib_depend_free(dtp);
2358 return (-1); /* preserve dt_errno */
2362 * Load the contents of any appropriate DTrace .d library files. These files
2363 * contain inlines and translators that will be cached by the compiler. We
2364 * defer this activity until the first compile to permit libdtrace clients to
2365 * add their own library directories and so that we can properly report errors.
2368 dt_load_libs(dtrace_hdl_t *dtp)
2372 if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2373 return (0); /* libraries already processed */
2375 dtp->dt_cflags |= DTRACE_C_NOLIBS;
2378 * /usr/lib/dtrace is always at the head of the list. The rest of the
2379 * list is specified in the precedence order the user requested. Process
2380 * everything other than the head first. DTRACE_C_NOLIBS has already
2381 * been spcified so dt_vopen will ensure that there is always one entry
2384 for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
2385 dirp != NULL; dirp = dt_list_next(dirp)) {
2386 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2387 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2388 return (-1); /* errno is set for us */
2392 /* Handle /usr/lib/dtrace */
2393 dirp = dt_list_next(&dtp->dt_lib_path);
2394 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2395 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2396 return (-1); /* errno is set for us */
2399 if (dt_load_libs_sort(dtp) < 0)
2400 return (-1); /* errno is set for us */
2406 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2407 uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2415 if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2416 (void) dt_set_errno(dtp, EINVAL);
2420 if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2421 return (NULL); /* errno is set for us */
2423 if (dtp->dt_globals->dh_nelems != 0)
2424 (void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2426 if (dtp->dt_tls->dh_nelems != 0)
2427 (void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2429 if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2430 return (NULL); /* errno is set for us */
2432 dt_pcb_push(dtp, &pcb);
2434 pcb.pcb_fileptr = fp;
2437 pcb.pcb_strlen = s ? strlen(s) : 0;
2438 pcb.pcb_sargc = argc;
2439 pcb.pcb_sargv = argv;
2440 pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2441 pcb.pcb_pspec = pspec;
2442 pcb.pcb_cflags = dtp->dt_cflags | cflags;
2443 pcb.pcb_amin = dtp->dt_amin;
2444 pcb.pcb_yystate = -1;
2445 pcb.pcb_context = context;
2446 pcb.pcb_token = context;
2448 if (context != DT_CTX_DPROG)
2450 else if (cflags & DTRACE_C_CTL)
2451 yybegin(YYS_CONTROL);
2453 yybegin(YYS_CLAUSE);
2455 if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2458 if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2459 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2461 yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2462 yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2463 DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2465 if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2466 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2469 * Invoke the parser to evaluate the D source code. If any errors
2470 * occur during parsing, an error function will be called and we
2471 * will longjmp back to pcb_jmpbuf to abort. If parsing succeeds,
2472 * we optionally display the parse tree if debugging is enabled.
2474 if (yyparse() != 0 || yypcb->pcb_root == NULL)
2475 xyerror(D_EMPTY, "empty D program translation unit\n");
2479 if (cflags & DTRACE_C_CTL)
2482 if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2483 dt_node_printr(yypcb->pcb_root, stderr, 0);
2485 if (yypcb->pcb_pragmas != NULL)
2486 (void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2488 if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2489 !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2490 xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2491 "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2495 * If we have successfully created a parse tree for a D program, loop
2496 * over the clauses and actions and instantiate the corresponding
2497 * libdtrace program. If we are parsing a D expression, then we
2498 * simply run the code generator and assembler on the resulting tree.
2502 assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2504 if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2505 !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2506 xyerror(D_EMPTY, "empty D program translation unit\n");
2508 if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2509 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2511 for (; dnp != NULL; dnp = dnp->dn_list) {
2512 switch (dnp->dn_kind) {
2513 case DT_NODE_CLAUSE:
2514 dt_compile_clause(dtp, dnp);
2516 case DT_NODE_XLATOR:
2517 if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2518 dt_compile_xlator(dnp);
2520 case DT_NODE_PROVIDER:
2521 (void) dt_node_cook(dnp, DT_IDFLG_REF);
2526 yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2527 yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2528 yypcb->pcb_asxrefs = NULL;
2529 yypcb->pcb_asxreflen = 0;
2531 rv = yypcb->pcb_prog;
2535 (void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2536 dt_cg(yypcb, yypcb->pcb_root);
2541 ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2542 err = dt_decl_type(ddp, arg);
2546 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2553 if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2554 DT_TREEDUMP_PASS(dtp, 3))
2555 dt_node_printr(yypcb->pcb_root, stderr, 0);
2557 if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2558 lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2559 ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2560 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2562 if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2563 lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2564 ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2565 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2567 if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2568 (void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2570 dt_pcb_pop(dtp, err);
2571 (void) dt_set_errno(dtp, err);
2572 return (err ? NULL : rv);
2576 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2577 dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2579 return (dt_compile(dtp, DT_CTX_DPROG,
2580 spec, NULL, cflags, argc, argv, NULL, s));
2584 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2585 uint_t cflags, int argc, char *const argv[])
2587 return (dt_compile(dtp, DT_CTX_DPROG,
2588 DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2592 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2594 (void) dt_compile(dtp, DT_CTX_DTYPE,
2595 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2596 return (dtp->dt_errno ? -1 : 0);
2600 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2602 (void) dt_compile(dtp, DT_CTX_DTYPE,
2603 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2604 return (dtp->dt_errno ? -1 : 0);