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) 2013, 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)
668 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
669 boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
670 const char *act = istrace ? "trace" : "print";
672 if (dt_node_is_void(dnp->dn_args)) {
673 dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
674 "%s( ) may not be applied to a void expression\n", act);
677 if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
678 dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
679 "%s( ) may not be applied to a translated pointer\n", act);
682 if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
683 dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
684 "%s( ) may not be applied to an aggregation%s\n", act,
685 istrace ? "" : " -- did you mean printa()?");
688 dt_cg(yypcb, dnp->dn_args);
691 * The print() action behaves identically to trace(), except that it
692 * stores the CTF type of the argument (if present) within the DOF for
693 * the DIFEXPR action. To do this, we set the 'dtsd_strdata' to point
694 * to the fully-qualified CTF type ID for the result of the DIF
695 * action. We use the ID instead of the name to handles complex types
696 * like arrays and function pointers that can't be resolved by
697 * ctf_type_lookup(). This is later processed by dtrace_dof_create()
698 * and turned into a reference into the string table so that we can
699 * get the type information when we process the data after the fact. In
700 * the case where we are referring to userland CTF data, we also need to
701 * to identify which ctf container in question we care about and encode
702 * that within the name.
704 if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
709 dret = yypcb->pcb_dret;
710 dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
712 n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name, dret->dn_type) + 1;
713 if (dmp->dm_pid != 0) {
714 ctflib = dt_module_getlibid(dtp, dmp, dret->dn_ctfp);
716 n = snprintf(NULL, 0, "%s`%d`%ld", dmp->dm_name,
717 ctflib, dret->dn_type) + 1;
719 n = snprintf(NULL, 0, "%s`%ld", dmp->dm_name,
722 sdp->dtsd_strdata = dt_alloc(dtp, n);
723 if (sdp->dtsd_strdata == NULL)
724 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
725 (void) snprintf(sdp->dtsd_strdata, n, "%s`%ld", dmp->dm_name,
727 if (dmp->dm_pid != 0) {
728 (void) snprintf(sdp->dtsd_strdata, n, "%s`%d`%ld",
729 dmp->dm_name, ctflib, dret->dn_type);
731 (void) snprintf(sdp->dtsd_strdata, n, "%s`%ld",
732 dmp->dm_name, dret->dn_type);
736 ap->dtad_difo = dt_as(yypcb);
737 ap->dtad_kind = DTRACEACT_DIFEXPR;
741 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
743 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
745 dt_node_t *addr = dnp->dn_args;
746 dt_node_t *max = dnp->dn_args->dn_list;
749 char n[DT_TYPE_NAMELEN];
751 if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
752 dnerror(addr, D_TRACEMEM_ADDR,
753 "tracemem( ) argument #1 is incompatible with "
754 "prototype:\n\tprototype: pointer or integer\n"
756 dt_node_type_name(addr, n, sizeof (n)));
759 if (dt_node_is_posconst(max) == 0) {
760 dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
761 "be a non-zero positive integral constant expression\n");
764 if ((size = max->dn_list) != NULL) {
765 if (size->dn_list != NULL) {
766 dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
767 "mismatch: expected at most 3 args\n");
770 if (!dt_node_is_scalar(size)) {
771 dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
772 "dynamic size (argument #3) must be of "
777 ap->dtad_difo = dt_as(yypcb);
778 ap->dtad_difo->dtdo_rtype = dt_int_rtype;
779 ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
781 ap = dt_stmt_action(dtp, sdp);
785 ap->dtad_difo = dt_as(yypcb);
786 ap->dtad_kind = DTRACEACT_TRACEMEM;
788 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
789 ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
793 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
795 ap->dtad_kind = DTRACEACT_STACK;
797 if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
798 ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
804 if (arg0->dn_list != NULL) {
805 dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
806 "mismatch: too many arguments\n");
809 if (dt_node_is_posconst(arg0) == 0) {
810 dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
811 "non-zero positive integral constant expression\n");
814 ap->dtad_arg = arg0->dn_value;
819 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
821 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
822 dt_action_stack_args(dtp, ap, dnp->dn_args);
826 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
828 uint32_t nframes = 0;
829 uint32_t strsize = 0; /* default string table size */
830 dt_node_t *arg0 = dnp->dn_args;
831 dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
833 assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
834 dnp->dn_ident->di_id == DT_ACT_USTACK);
836 if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
837 if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
838 nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
840 if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
841 strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
843 ap->dtad_kind = DTRACEACT_JSTACK;
845 assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
847 if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
848 nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
850 ap->dtad_kind = DTRACEACT_USTACK;
854 if (!dt_node_is_posconst(arg0)) {
855 dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
856 "must be a non-zero positive integer constant\n");
858 nframes = (uint32_t)arg0->dn_value;
862 if (arg1->dn_kind != DT_NODE_INT ||
863 ((arg1->dn_flags & DT_NF_SIGNED) &&
864 (int64_t)arg1->dn_value < 0)) {
865 dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
866 "must be a positive integer constant\n");
869 if (arg1->dn_list != NULL) {
870 dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
871 "mismatch: too many arguments\n");
874 strsize = (uint32_t)arg1->dn_value;
877 ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
881 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
883 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
884 dt_action_ustack_args(dtp, ap, dnp);
888 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
890 dtrace_actdesc_t *ap;
891 dt_node_t *arg0, *arg1;
894 * The prototype guarantees that we are called with either one or
895 * two arguments, and that any arguments that are present are strings.
898 arg1 = arg0->dn_list;
900 ap = dt_stmt_action(dtp, sdp);
902 ap->dtad_difo = dt_as(yypcb);
903 ap->dtad_kind = DTRACEACT_LIBACT;
904 ap->dtad_arg = DT_ACT_SETOPT;
906 ap = dt_stmt_action(dtp, sdp);
909 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
912 ap->dtad_difo = dt_as(yypcb);
913 ap->dtad_kind = DTRACEACT_LIBACT;
916 ap->dtad_arg = DT_ACT_SETOPT;
921 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
922 dt_node_t *dnp, dtrace_actkind_t kind)
924 assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
925 kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
926 kind == DTRACEACT_UADDR);
929 ap->dtad_difo = dt_as(yypcb);
930 ap->dtad_kind = kind;
931 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
935 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
936 dtrace_actkind_t kind)
938 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
939 dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
944 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
946 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
949 * Library actions need a DIFO that serves as an argument. As
950 * ftruncate() doesn't take an argument, we generate the constant 0
951 * in a DIFO; this constant will be ignored when the ftruncate() is
954 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
955 ap->dtad_arg = DT_ACT_FTRUNCATE;
960 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
962 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
964 ap->dtad_kind = DTRACEACT_STOP;
970 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
972 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
974 ap->dtad_kind = DTRACEACT_BREAKPOINT;
980 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
982 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
984 ap->dtad_kind = DTRACEACT_PANIC;
989 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
991 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
993 dt_cg(yypcb, dnp->dn_args);
994 ap->dtad_difo = dt_as(yypcb);
995 ap->dtad_kind = DTRACEACT_CHILL;
999 dt_action_raise(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_RAISE;
1009 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1011 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1013 dt_cg(yypcb, dnp->dn_args);
1014 ap->dtad_difo = dt_as(yypcb);
1015 ap->dtad_kind = DTRACEACT_EXIT;
1016 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
1020 dt_action_speculate(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_SPECULATE;
1030 dt_action_printm(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1032 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1034 dt_node_t *size = dnp->dn_args;
1035 dt_node_t *addr = dnp->dn_args->dn_list;
1037 char n[DT_TYPE_NAMELEN];
1039 if (dt_node_is_posconst(size) == 0) {
1040 dnerror(size, D_PRINTM_SIZE, "printm( ) argument #1 must "
1041 "be a non-zero positive integral constant expression\n");
1044 if (dt_node_is_pointer(addr) == 0) {
1045 dnerror(addr, D_PRINTM_ADDR,
1046 "printm( ) argument #2 is incompatible with "
1047 "prototype:\n\tprototype: pointer\n"
1048 "\t argument: %s\n",
1049 dt_node_type_name(addr, n, sizeof (n)));
1053 ap->dtad_difo = dt_as(yypcb);
1054 ap->dtad_kind = DTRACEACT_PRINTM;
1056 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1057 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + sizeof(uintptr_t);
1061 dt_action_printt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1063 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1065 dt_node_t *size = dnp->dn_args;
1066 dt_node_t *addr = dnp->dn_args->dn_list;
1068 char n[DT_TYPE_NAMELEN];
1070 if (dt_node_is_posconst(size) == 0) {
1071 dnerror(size, D_PRINTT_SIZE, "printt( ) argument #1 must "
1072 "be a non-zero positive integral constant expression\n");
1075 if (addr == NULL || addr->dn_kind != DT_NODE_FUNC ||
1076 addr->dn_ident != dt_idhash_lookup(dtp->dt_globals, "typeref")) {
1077 dnerror(addr, D_PRINTT_ADDR,
1078 "printt( ) argument #2 is incompatible with "
1079 "prototype:\n\tprototype: typeref()\n"
1080 "\t argument: %s\n",
1081 dt_node_type_name(addr, n, sizeof (n)));
1085 ap->dtad_difo = dt_as(yypcb);
1086 ap->dtad_kind = DTRACEACT_PRINTT;
1088 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
1091 * Allow additional buffer space for the data size, type size,
1092 * type string length and a stab in the dark (32 bytes) for the
1093 * type string. The type string is part of the typeref() that
1094 * this action references.
1096 ap->dtad_difo->dtdo_rtype.dtdt_size = size->dn_value + 3 * sizeof(uintptr_t) + 32;
1101 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1103 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1105 dt_cg(yypcb, dnp->dn_args);
1106 ap->dtad_difo = dt_as(yypcb);
1107 ap->dtad_kind = DTRACEACT_COMMIT;
1111 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1113 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1115 dt_cg(yypcb, dnp->dn_args);
1116 ap->dtad_difo = dt_as(yypcb);
1117 ap->dtad_kind = DTRACEACT_DISCARD;
1121 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1123 switch (dnp->dn_expr->dn_ident->di_id) {
1124 case DT_ACT_BREAKPOINT:
1125 dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1128 dt_action_chill(dtp, dnp->dn_expr, sdp);
1131 dt_action_clear(dtp, dnp->dn_expr, sdp);
1134 dt_action_commit(dtp, dnp->dn_expr, sdp);
1136 case DT_ACT_DENORMALIZE:
1137 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1139 case DT_ACT_DISCARD:
1140 dt_action_discard(dtp, dnp->dn_expr, sdp);
1143 dt_action_exit(dtp, dnp->dn_expr, sdp);
1145 case DT_ACT_FREOPEN:
1146 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1148 case DT_ACT_FTRUNCATE:
1149 dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1152 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1154 case DT_ACT_NORMALIZE:
1155 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1158 dt_action_panic(dtp, dnp->dn_expr, sdp);
1161 dt_action_trace(dtp, dnp->dn_expr, sdp);
1164 dt_action_printa(dtp, dnp->dn_expr, sdp);
1167 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1170 dt_action_printm(dtp, dnp->dn_expr, sdp);
1173 dt_action_printt(dtp, dnp->dn_expr, sdp);
1176 dt_action_raise(dtp, dnp->dn_expr, sdp);
1179 dt_action_setopt(dtp, dnp->dn_expr, sdp);
1181 case DT_ACT_SPECULATE:
1182 dt_action_speculate(dtp, dnp->dn_expr, sdp);
1185 dt_action_stack(dtp, dnp->dn_expr, sdp);
1188 dt_action_stop(dtp, dnp->dn_expr, sdp);
1191 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1194 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1197 dt_action_trace(dtp, dnp->dn_expr, sdp);
1199 case DT_ACT_TRACEMEM:
1200 dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1203 dt_action_trunc(dtp, dnp->dn_expr, sdp);
1206 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1209 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1212 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1216 dt_action_ustack(dtp, dnp->dn_expr, sdp);
1219 dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1220 "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1225 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1227 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1229 dt_cg(yypcb, dnp->dn_expr);
1230 ap->dtad_difo = dt_as(yypcb);
1231 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1232 ap->dtad_kind = DTRACEACT_DIFEXPR;
1236 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1238 dt_ident_t *aid, *fid;
1239 dt_node_t *anp, *incr = NULL;
1240 dtrace_actdesc_t *ap;
1241 uint_t n = 1, argmax;
1245 * If the aggregation has no aggregating function applied to it, then
1246 * this statement has no effect. Flag this as a programming error.
1248 if (dnp->dn_aggfun == NULL) {
1249 dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1250 dnp->dn_ident->di_name);
1253 aid = dnp->dn_ident;
1254 fid = dnp->dn_aggfun->dn_ident;
1256 if (dnp->dn_aggfun->dn_args != NULL &&
1257 dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1258 dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1259 "be of scalar type\n", fid->di_name);
1263 * The ID of the aggregation itself is implicitly recorded as the first
1264 * member of each aggregation tuple so we can distinguish them later.
1266 ap = dt_stmt_action(dtp, sdp);
1267 dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1269 for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1270 ap = dt_stmt_action(dtp, sdp);
1273 if (anp->dn_kind == DT_NODE_FUNC) {
1274 if (anp->dn_ident->di_id == DT_ACT_STACK) {
1275 dt_action_stack_args(dtp, ap, anp->dn_args);
1279 if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1280 anp->dn_ident->di_id == DT_ACT_JSTACK) {
1281 dt_action_ustack_args(dtp, ap, anp);
1285 switch (anp->dn_ident->di_id) {
1287 dt_action_symmod_args(dtp, ap,
1288 anp->dn_args, DTRACEACT_UADDR);
1292 dt_action_symmod_args(dtp, ap,
1293 anp->dn_args, DTRACEACT_USYM);
1297 dt_action_symmod_args(dtp, ap,
1298 anp->dn_args, DTRACEACT_UMOD);
1302 dt_action_symmod_args(dtp, ap,
1303 anp->dn_args, DTRACEACT_SYM);
1307 dt_action_symmod_args(dtp, ap,
1308 anp->dn_args, DTRACEACT_MOD);
1317 ap->dtad_difo = dt_as(yypcb);
1318 ap->dtad_kind = DTRACEACT_DIFEXPR;
1321 if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1323 * For linear quantization, we have between two and four
1324 * arguments in addition to the expression:
1326 * arg1 => Base value
1327 * arg2 => Limit value
1328 * arg3 => Quantization level step size (defaults to 1)
1329 * arg4 => Quantization increment value (defaults to 1)
1331 dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1332 dt_node_t *arg2 = arg1->dn_list;
1333 dt_node_t *arg3 = arg2->dn_list;
1335 uint64_t nlevels, step = 1, oarg;
1336 int64_t baseval, limitval;
1338 if (arg1->dn_kind != DT_NODE_INT) {
1339 dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1340 "argument #1 must be an integer constant\n");
1343 baseval = (int64_t)arg1->dn_value;
1345 if (baseval < INT32_MIN || baseval > INT32_MAX) {
1346 dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1347 "argument #1 must be a 32-bit quantity\n");
1350 if (arg2->dn_kind != DT_NODE_INT) {
1351 dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1352 "argument #2 must be an integer constant\n");
1355 limitval = (int64_t)arg2->dn_value;
1357 if (limitval < INT32_MIN || limitval > INT32_MAX) {
1358 dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1359 "argument #2 must be a 32-bit quantity\n");
1362 if (limitval < baseval) {
1363 dnerror(dnp, D_LQUANT_MISMATCH,
1364 "lquantize( ) base (argument #1) must be less "
1365 "than limit (argument #2)\n");
1369 if (!dt_node_is_posconst(arg3)) {
1370 dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1371 "argument #3 must be a non-zero positive "
1372 "integer constant\n");
1375 if ((step = arg3->dn_value) > UINT16_MAX) {
1376 dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1377 "argument #3 must be a 16-bit quantity\n");
1381 nlevels = (limitval - baseval) / step;
1384 dnerror(dnp, D_LQUANT_STEPLARGE,
1385 "lquantize( ) step (argument #3) too large: must "
1386 "have at least one quantization level\n");
1389 if (nlevels > UINT16_MAX) {
1390 dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1391 "(argument #3) too small: number of quantization "
1392 "levels must be a 16-bit quantity\n");
1395 arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1396 (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1397 ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1398 DTRACE_LQUANTIZE_BASEMASK);
1402 isp = (dt_idsig_t *)aid->di_data;
1404 if (isp->dis_auxinfo == 0) {
1406 * This is the first time we've seen an lquantize()
1407 * for this aggregation; we'll store our argument
1408 * as the auxiliary signature information.
1410 isp->dis_auxinfo = arg;
1411 } else if ((oarg = isp->dis_auxinfo) != arg) {
1413 * If we have seen this lquantize() before and the
1414 * argument doesn't match the original argument, pick
1415 * the original argument apart to concisely report the
1418 int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1419 int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1420 int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1422 if (obaseval != baseval) {
1423 dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1424 "base (argument #1) doesn't match previous "
1425 "declaration: expected %d, found %d\n",
1426 obaseval, (int)baseval);
1429 if (onlevels * ostep != nlevels * step) {
1430 dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1431 "limit (argument #2) doesn't match previous"
1432 " declaration: expected %d, found %d\n",
1433 obaseval + onlevels * ostep,
1434 (int)baseval + (int)nlevels * (int)step);
1437 if (ostep != step) {
1438 dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1439 "step (argument #3) doesn't match previous "
1440 "declaration: expected %d, found %d\n",
1445 * We shouldn't be able to get here -- one of the
1446 * parameters must be mismatched if the arguments
1452 incr = arg3 != NULL ? arg3->dn_list : NULL;
1456 if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1458 * For log/linear quantizations, we have between one and five
1459 * arguments in addition to the expression:
1462 * arg2 => Low magnitude
1463 * arg3 => High magnitude
1464 * arg4 => Number of steps per magnitude
1465 * arg5 => Quantization increment value (defaults to 1)
1467 dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1468 uint64_t oarg, order, v;
1473 char *str; /* string identifier */
1474 int badtype; /* error on bad type */
1475 int badval; /* error on bad value */
1476 int mismatch; /* error on bad match */
1477 int shift; /* shift value */
1478 uint16_t value; /* value itself */
1480 { "factor", D_LLQUANT_FACTORTYPE,
1481 D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1482 DTRACE_LLQUANTIZE_FACTORSHIFT },
1483 { "low magnitude", D_LLQUANT_LOWTYPE,
1484 D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1485 DTRACE_LLQUANTIZE_LOWSHIFT },
1486 { "high magnitude", D_LLQUANT_HIGHTYPE,
1487 D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1488 DTRACE_LLQUANTIZE_HIGHSHIFT },
1489 { "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1490 D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1491 DTRACE_LLQUANTIZE_NSTEPSHIFT },
1497 for (i = 0; args[i].str != NULL; i++) {
1498 if (llarg->dn_kind != DT_NODE_INT) {
1499 dnerror(llarg, args[i].badtype, "llquantize( ) "
1500 "argument #%d (%s) must be an "
1501 "integer constant\n", i + 1, args[i].str);
1504 if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1505 dnerror(llarg, args[i].badval, "llquantize( ) "
1506 "argument #%d (%s) must be an unsigned "
1507 "16-bit quantity\n", i + 1, args[i].str);
1510 args[i].value = (uint16_t)llarg->dn_value;
1512 assert(!(arg & ((uint64_t)UINT16_MAX <<
1514 arg |= ((uint64_t)args[i].value << args[i].shift);
1515 llarg = llarg->dn_list;
1520 if (args[0].value < 2) {
1521 dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1522 "factor (argument #1) must be two or more\n");
1525 if (args[1].value >= args[2].value) {
1526 dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1527 "high magnitude (argument #3) must be greater "
1528 "than low magnitude (argument #2)\n");
1531 if (args[3].value < args[0].value) {
1532 dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1533 "factor (argument #1) must be less than or "
1534 "equal to the number of linear steps per "
1535 "magnitude (argument #4)\n");
1538 for (v = args[0].value; v < args[3].value; v *= args[0].value)
1541 if ((args[3].value % args[0].value) || (v % args[3].value)) {
1542 dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1543 "factor (argument #1) must evenly divide the "
1544 "number of steps per magnitude (argument #4), "
1545 "and the number of steps per magnitude must evenly "
1546 "divide a power of the factor\n");
1549 for (i = 0, order = 1; i < args[2].value; i++) {
1550 if (order * args[0].value > order) {
1551 order *= args[0].value;
1555 dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1556 "factor (%d) raised to power of high magnitude "
1557 "(%d) overflows 64-bits\n", args[0].value,
1561 isp = (dt_idsig_t *)aid->di_data;
1563 if (isp->dis_auxinfo == 0) {
1565 * This is the first time we've seen an llquantize()
1566 * for this aggregation; we'll store our argument
1567 * as the auxiliary signature information.
1569 isp->dis_auxinfo = arg;
1570 } else if ((oarg = isp->dis_auxinfo) != arg) {
1572 * If we have seen this llquantize() before and the
1573 * argument doesn't match the original argument, pick
1574 * the original argument apart to concisely report the
1577 int expected = 0, found = 0;
1579 for (i = 0; expected == found; i++) {
1580 assert(args[i].str != NULL);
1582 expected = (oarg >> args[i].shift) & UINT16_MAX;
1583 found = (arg >> args[i].shift) & UINT16_MAX;
1586 dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1587 "%s (argument #%d) doesn't match previous "
1588 "declaration: expected %d, found %d\n",
1589 args[i - 1].str, i, expected, found);
1596 if (fid->di_id == DTRACEAGG_QUANTIZE) {
1597 incr = dnp->dn_aggfun->dn_args->dn_list;
1602 if (!dt_node_is_scalar(incr)) {
1603 dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1604 "(argument #%d) must be of scalar type\n",
1605 fid->di_name, argmax);
1608 if ((anp = incr->dn_list) != NULL) {
1611 for (; anp != NULL; anp = anp->dn_list)
1614 dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1615 "mismatch: %d args passed, at most %d expected",
1616 fid->di_name, argc, argmax);
1619 ap = dt_stmt_action(dtp, sdp);
1623 ap->dtad_difo = dt_as(yypcb);
1624 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1625 ap->dtad_kind = DTRACEACT_DIFEXPR;
1628 assert(sdp->dtsd_aggdata == NULL);
1629 sdp->dtsd_aggdata = aid;
1631 ap = dt_stmt_action(dtp, sdp);
1632 assert(fid->di_kind == DT_IDENT_AGGFUNC);
1633 assert(DTRACEACT_ISAGG(fid->di_id));
1634 ap->dtad_kind = fid->di_id;
1635 ap->dtad_ntuple = n;
1638 if (dnp->dn_aggfun->dn_args != NULL) {
1639 dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1640 ap->dtad_difo = dt_as(yypcb);
1645 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1647 dtrace_ecbdesc_t *edp;
1648 dtrace_stmtdesc_t *sdp;
1651 yylineno = pnp->dn_line;
1652 dt_setcontext(dtp, pnp->dn_desc);
1653 (void) dt_node_cook(cnp, DT_IDFLG_REF);
1655 if (DT_TREEDUMP_PASS(dtp, 2))
1656 dt_node_printr(cnp, stderr, 0);
1658 if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1659 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1661 assert(yypcb->pcb_ecbdesc == NULL);
1662 yypcb->pcb_ecbdesc = edp;
1664 if (cnp->dn_pred != NULL) {
1665 dt_cg(yypcb, cnp->dn_pred);
1666 edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1669 if (cnp->dn_acts == NULL) {
1670 dt_stmt_append(dt_stmt_create(dtp, edp,
1671 cnp->dn_ctxattr, _dtrace_defattr), cnp);
1674 for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1675 assert(yypcb->pcb_stmt == NULL);
1676 sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1678 switch (dnp->dn_kind) {
1680 if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1681 dt_compile_agg(dtp, dnp->dn_expr, sdp);
1683 dt_compile_exp(dtp, dnp, sdp);
1686 dt_compile_fun(dtp, dnp, sdp);
1689 dt_compile_agg(dtp, dnp, sdp);
1692 dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1693 "%u is not a valid statement\n", dnp->dn_kind);
1696 assert(yypcb->pcb_stmt == sdp);
1697 dt_stmt_append(sdp, dnp);
1700 assert(yypcb->pcb_ecbdesc == edp);
1701 dt_ecbdesc_release(dtp, edp);
1703 yypcb->pcb_ecbdesc = NULL;
1707 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1711 for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1712 dt_compile_one_clause(dtp, cnp, pnp);
1716 dt_compile_xlator(dt_node_t *dnp)
1718 dt_xlator_t *dxp = dnp->dn_xlator;
1721 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1722 assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1724 dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1729 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1731 const dtrace_pattr_t *pap;
1739 * Both kernel and pid based providers are allowed to have names
1740 * ending with what could be interpreted as a number. We assume it's
1741 * a pid and that we may need to dynamically create probes for
1744 * (1) The provider doesn't exist, or,
1745 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1747 * On an error, dt_pid_create_probes() will set the error message
1748 * and tag -- we just have to longjmp() out of here.
1750 if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1751 ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1752 pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1753 dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1754 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1758 * Call dt_probe_info() to get the probe arguments and attributes. If
1759 * a representative probe is found, set 'pap' to the probe provider's
1760 * attributes. Otherwise set 'pap' to default Unstable attributes.
1762 if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1763 pap = &_dtrace_prvdesc;
1764 err = dtrace_errno(dtp);
1765 bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1766 yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1767 yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1769 pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1773 if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1774 xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1775 "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1776 pdp->dtpd_func, pdp->dtpd_name);
1779 if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1780 xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1782 dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1783 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1784 pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1785 attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1788 * Reset the stability attributes of D global variables that vary
1789 * based on the attributes of the provider and context itself.
1791 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1792 idp->di_attr = pap->dtpa_provider;
1793 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1794 idp->di_attr = pap->dtpa_mod;
1795 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1796 idp->di_attr = pap->dtpa_func;
1797 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1798 idp->di_attr = pap->dtpa_name;
1799 if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1800 idp->di_attr = pap->dtpa_args;
1802 yypcb->pcb_pdesc = pdp;
1803 yypcb->pcb_probe = prp;
1807 * Reset context-dependent variables and state at the end of cooking a D probe
1808 * definition clause. This ensures that external declarations between clauses
1809 * do not reference any stale context-dependent data from the previous clause.
1812 dt_endcontext(dtrace_hdl_t *dtp)
1814 static const char *const cvars[] = {
1815 "probeprov", "probemod", "probefunc", "probename", "args", NULL
1821 for (i = 0; cvars[i] != NULL; i++) {
1822 if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1823 idp->di_attr = _dtrace_defattr;
1826 yypcb->pcb_pdesc = NULL;
1827 yypcb->pcb_probe = NULL;
1831 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1833 if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1834 dt_idhash_delete(dhp, idp);
1840 * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1841 * any identifiers or translators that have been previously defined as bound to
1842 * a version greater than the specified version. Therefore, in our current
1843 * version implementation, establishing a binding is a one-way transformation.
1844 * In addition, no versioning is currently provided for types as our .d library
1845 * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1846 * for our exclusive use. If required, type versioning will require more work.
1849 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1851 char s[DT_VERSION_STRMAX];
1852 dt_xlator_t *dxp, *nxp;
1854 if (v > dtp->dt_vmax)
1855 return (dt_set_errno(dtp, EDT_VERSREDUCED));
1856 else if (v == dtp->dt_vmax)
1857 return (0); /* no reduction necessary */
1859 dt_dprintf("reducing api version to %s\n",
1860 dt_version_num2str(v, s, sizeof (s)));
1864 for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1865 nxp = dt_list_next(dxp);
1866 if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1867 (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1868 dt_list_delete(&dtp->dt_xlators, dxp);
1871 (void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1872 (void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1873 (void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1874 (void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1880 * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1881 * and return a FILE handle for the cpp output. We use the /dev/fd filesystem
1882 * here to simplify the code by leveraging file descriptor inheritance.
1885 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1887 int argc = dtp->dt_cpp_argc;
1888 char **argv = malloc(sizeof (char *) * (argc + 5));
1889 FILE *ofp = tmpfile();
1892 char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1894 char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1896 struct sigaction act, oact;
1897 sigset_t mask, omask;
1908 if (argv == NULL || ofp == NULL) {
1909 (void) dt_set_errno(dtp, errno);
1914 * If the input is a seekable file, see if it is an interpreter file.
1915 * If we see #!, seek past the first line because cpp will choke on it.
1916 * We start cpp just prior to the \n at the end of this line so that
1917 * it still sees the newline, ensuring that #line values are correct.
1919 if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1920 if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1921 for (off += 2; c != '\n'; off++) {
1922 if ((c = fgetc(ifp)) == EOF)
1926 off--; /* start cpp just prior to \n */
1929 (void) fseeko64(ifp, off, SEEK_SET);
1933 (void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1934 (void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1937 bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1939 (void) snprintf(verdef, sizeof (verdef),
1940 "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1941 argv[argc++] = verdef;
1944 switch (dtp->dt_stdcmode) {
1947 argv[argc++] = "-D__STDC__=0";
1950 argv[argc++] = "-D__STDC__=1";
1954 argv[argc++] = ipath;
1955 argv[argc++] = opath;
1957 argv[argc++] = "-P";
1962 * libdtrace must be able to be embedded in other programs that may
1963 * include application-specific signal handlers. Therefore, if we
1964 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1965 * that could confuse the containing application. To do this,
1966 * we block SIGCHLD and reset its disposition to SIG_DFL.
1967 * We restore our signal state once we are done.
1969 (void) sigemptyset(&mask);
1970 (void) sigaddset(&mask, SIGCHLD);
1971 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
1973 bzero(&act, sizeof (act));
1974 act.sa_handler = SIG_DFL;
1975 (void) sigaction(SIGCHLD, &act, &oact);
1977 if ((pid = fork1()) == -1) {
1978 (void) sigaction(SIGCHLD, &oact, NULL);
1979 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1980 (void) dt_set_errno(dtp, EDT_CPPFORK);
1986 if (isatty(fileno(ifp)) == 0)
1987 lseek(fileno(ifp), off, SEEK_SET);
1988 dup2(fileno(ifp), 0);
1989 dup2(fileno(ofp), 1);
1991 (void) execvp(dtp->dt_cpp_path, argv);
1992 _exit(errno == ENOENT ? 127 : 126);
1996 dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1998 } while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
2000 (void) sigaction(SIGCHLD, &oact, NULL);
2001 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
2003 dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
2004 estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
2009 (void) dt_set_errno(dtp, EDT_CPPEXEC);
2012 (void) dt_set_errno(dtp, EDT_CPPENT);
2015 (void) dt_set_errno(dtp, EDT_CPPERR);
2022 (void) fseek(ofp, 0, SEEK_SET);
2032 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
2036 va_start(ap, format);
2037 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
2042 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
2044 dt_lib_depend_t *dld;
2047 assert(arg != NULL);
2049 if ((end = strrchr(arg, '/')) == NULL)
2050 return (dt_set_errno(dtp, EINVAL));
2052 if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2055 if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
2060 (void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
2061 if ((dld->dtld_library = strdup(arg)) == NULL) {
2062 dt_free(dtp, dld->dtld_libpath);
2064 return (dt_set_errno(dtp, EDT_NOMEM));
2067 dt_list_append(dlp, dld);
2072 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
2074 dt_lib_depend_t *dldn;
2076 for (dldn = dt_list_next(dld); dldn != NULL;
2077 dldn = dt_list_next(dldn)) {
2078 if (strcmp(dldn->dtld_library, arg) == 0)
2086 * Go through all the library files, and, if any library dependencies exist for
2087 * that file, add it to that node's list of dependents. The result of this
2088 * will be a graph which can then be topologically sorted to produce a
2089 * compilation order.
2092 dt_lib_build_graph(dtrace_hdl_t *dtp)
2094 dt_lib_depend_t *dld, *dpld;
2096 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2097 dld = dt_list_next(dld)) {
2098 char *library = dld->dtld_library;
2100 for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2101 dpld = dt_list_next(dpld)) {
2102 dt_lib_depend_t *dlda;
2104 if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2105 dpld->dtld_library)) == NULL) {
2106 dt_lib_depend_error(dtp,
2107 "Invalid library dependency in %s: %s\n",
2108 dld->dtld_library, dpld->dtld_library);
2110 return (dt_set_errno(dtp, EDT_COMPILER));
2113 if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2115 return (-1); /* preserve dt_errno */
2123 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2125 dt_lib_depend_t *dpld, *dlda, *new;
2127 dld->dtld_start = ++(*count);
2129 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2130 dpld = dt_list_next(dpld)) {
2131 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2132 dpld->dtld_library);
2133 assert(dlda != NULL);
2135 if (dlda->dtld_start == 0 &&
2136 dt_topo_sort(dtp, dlda, count) == -1)
2140 if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2143 if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2145 return (dt_set_errno(dtp, EDT_NOMEM));
2148 new->dtld_start = dld->dtld_start;
2149 new->dtld_finish = dld->dtld_finish = ++(*count);
2150 dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2152 dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2153 new->dtld_start, new->dtld_finish);
2159 dt_lib_depend_sort(dtrace_hdl_t *dtp)
2161 dt_lib_depend_t *dld, *dpld, *dlda;
2164 if (dt_lib_build_graph(dtp) == -1)
2165 return (-1); /* preserve dt_errno */
2168 * Perform a topological sort of the graph that hangs off
2169 * dtp->dt_lib_dep. The result of this process will be a
2170 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2172 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2173 dld = dt_list_next(dld)) {
2174 if (dld->dtld_start == 0 &&
2175 dt_topo_sort(dtp, dld, &count) == -1)
2176 return (-1); /* preserve dt_errno */;
2180 * Check the graph for cycles. If an ancestor's finishing time is
2181 * less than any of its dependent's finishing times then a back edge
2182 * exists in the graph and this is a cycle.
2184 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2185 dld = dt_list_next(dld)) {
2186 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2187 dpld = dt_list_next(dpld)) {
2188 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2189 dpld->dtld_library);
2190 assert(dlda != NULL);
2192 if (dlda->dtld_finish > dld->dtld_finish) {
2193 dt_lib_depend_error(dtp,
2194 "Cyclic dependency detected: %s => %s\n",
2195 dld->dtld_library, dpld->dtld_library);
2197 return (dt_set_errno(dtp, EDT_COMPILER));
2206 dt_lib_depend_free(dtrace_hdl_t *dtp)
2208 dt_lib_depend_t *dld, *dlda;
2210 while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2211 while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2212 dt_list_delete(&dld->dtld_dependencies, dlda);
2213 dt_free(dtp, dlda->dtld_library);
2214 dt_free(dtp, dlda->dtld_libpath);
2217 while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2218 dt_list_delete(&dld->dtld_dependents, dlda);
2219 dt_free(dtp, dlda->dtld_library);
2220 dt_free(dtp, dlda->dtld_libpath);
2223 dt_list_delete(&dtp->dt_lib_dep, dld);
2224 dt_free(dtp, dld->dtld_library);
2225 dt_free(dtp, dld->dtld_libpath);
2229 while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2230 dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2231 dt_free(dtp, dld->dtld_library);
2237 * Open all the .d library files found in the specified directory and
2238 * compile each one of them. We silently ignore any missing directories and
2239 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2240 * we fail to compile a library and the error is something other than #pragma D
2241 * depends_on. Dependency errors are silently ignored to permit a library
2242 * directory to contain libraries which may not be accessible depending on our
2246 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2249 const char *p, *end;
2252 char fname[PATH_MAX];
2255 dt_lib_depend_t *dld;
2257 if ((dirp = opendir(path)) == NULL) {
2258 dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2262 /* First, parse each file for library dependencies. */
2263 while ((dp = readdir(dirp)) != NULL) {
2264 if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2265 continue; /* skip any filename not ending in .d */
2267 (void) snprintf(fname, sizeof (fname),
2268 "%s/%s", path, dp->d_name);
2270 if ((fp = fopen(fname, "r")) == NULL) {
2271 dt_dprintf("skipping library %s: %s\n",
2272 fname, strerror(errno));
2277 * Skip files whose name match an already processed library
2279 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2280 dld = dt_list_next(dld)) {
2281 end = strrchr(dld->dtld_library, '/');
2282 /* dt_lib_depend_add ensures this */
2283 assert(end != NULL);
2284 if (strcmp(end + 1, dp->d_name) == 0)
2289 dt_dprintf("skipping library %s, already processed "
2290 "library with the same name: %s", dp->d_name,
2296 dtp->dt_filetag = fname;
2297 if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0) {
2299 return (-1); /* preserve dt_errno */
2302 rv = dt_compile(dtp, DT_CTX_DPROG,
2303 DTRACE_PROBESPEC_NAME, NULL,
2304 DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2306 if (rv != NULL && dtp->dt_errno &&
2307 (dtp->dt_errno != EDT_COMPILER ||
2308 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
2310 return (-1); /* preserve dt_errno */
2314 dt_dprintf("error parsing library %s: %s\n",
2315 fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2318 dtp->dt_filetag = NULL;
2321 (void) closedir(dirp);
2327 * Perform a topological sorting of all the libraries found across the entire
2328 * dt_lib_path. Once sorted, compile each one in topological order to cache its
2329 * inlines and translators, etc. We silently ignore any missing directories and
2330 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2331 * we fail to compile a library and the error is something other than #pragma D
2332 * depends_on. Dependency errors are silently ignored to permit a library
2333 * directory to contain libraries which may not be accessible depending on our
2337 dt_load_libs_sort(dtrace_hdl_t *dtp)
2341 dt_lib_depend_t *dld;
2344 * Finish building the graph containing the library dependencies
2345 * and perform a topological sort to generate an ordered list
2348 if (dt_lib_depend_sort(dtp) == -1)
2351 for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2352 dld = dt_list_next(dld)) {
2354 if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2355 dt_dprintf("skipping library %s: %s\n",
2356 dld->dtld_library, strerror(errno));
2360 dtp->dt_filetag = dld->dtld_library;
2361 pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2363 dtp->dt_filetag = NULL;
2365 if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2366 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2370 dt_dprintf("skipping library %s: %s\n",
2372 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2374 dld->dtld_loaded = B_TRUE;
2375 dt_program_destroy(dtp, pgp);
2379 dt_lib_depend_free(dtp);
2383 dt_lib_depend_free(dtp);
2384 return (-1); /* preserve dt_errno */
2388 * Load the contents of any appropriate DTrace .d library files. These files
2389 * contain inlines and translators that will be cached by the compiler. We
2390 * defer this activity until the first compile to permit libdtrace clients to
2391 * add their own library directories and so that we can properly report errors.
2394 dt_load_libs(dtrace_hdl_t *dtp)
2398 if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2399 return (0); /* libraries already processed */
2401 dtp->dt_cflags |= DTRACE_C_NOLIBS;
2404 * /usr/lib/dtrace is always at the head of the list. The rest of the
2405 * list is specified in the precedence order the user requested. Process
2406 * everything other than the head first. DTRACE_C_NOLIBS has already
2407 * been spcified so dt_vopen will ensure that there is always one entry
2410 for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
2411 dirp != NULL; dirp = dt_list_next(dirp)) {
2412 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2413 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2414 return (-1); /* errno is set for us */
2418 /* Handle /usr/lib/dtrace */
2419 dirp = dt_list_next(&dtp->dt_lib_path);
2420 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2421 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2422 return (-1); /* errno is set for us */
2425 if (dt_load_libs_sort(dtp) < 0)
2426 return (-1); /* errno is set for us */
2432 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2433 uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2441 if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2442 (void) dt_set_errno(dtp, EINVAL);
2446 if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2447 return (NULL); /* errno is set for us */
2449 if (dtp->dt_globals->dh_nelems != 0)
2450 (void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2452 if (dtp->dt_tls->dh_nelems != 0)
2453 (void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2455 if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2456 return (NULL); /* errno is set for us */
2458 dt_pcb_push(dtp, &pcb);
2460 pcb.pcb_fileptr = fp;
2463 pcb.pcb_strlen = s ? strlen(s) : 0;
2464 pcb.pcb_sargc = argc;
2465 pcb.pcb_sargv = argv;
2466 pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2467 pcb.pcb_pspec = pspec;
2468 pcb.pcb_cflags = dtp->dt_cflags | cflags;
2469 pcb.pcb_amin = dtp->dt_amin;
2470 pcb.pcb_yystate = -1;
2471 pcb.pcb_context = context;
2472 pcb.pcb_token = context;
2474 if (context != DT_CTX_DPROG)
2476 else if (cflags & DTRACE_C_CTL)
2477 yybegin(YYS_CONTROL);
2479 yybegin(YYS_CLAUSE);
2481 if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2484 if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2485 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2487 yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2488 yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2489 DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2491 if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2492 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2495 * Invoke the parser to evaluate the D source code. If any errors
2496 * occur during parsing, an error function will be called and we
2497 * will longjmp back to pcb_jmpbuf to abort. If parsing succeeds,
2498 * we optionally display the parse tree if debugging is enabled.
2500 if (yyparse() != 0 || yypcb->pcb_root == NULL)
2501 xyerror(D_EMPTY, "empty D program translation unit\n");
2505 if (cflags & DTRACE_C_CTL)
2508 if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2509 dt_node_printr(yypcb->pcb_root, stderr, 0);
2511 if (yypcb->pcb_pragmas != NULL)
2512 (void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2514 if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2515 !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2516 xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2517 "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2521 * If we have successfully created a parse tree for a D program, loop
2522 * over the clauses and actions and instantiate the corresponding
2523 * libdtrace program. If we are parsing a D expression, then we
2524 * simply run the code generator and assembler on the resulting tree.
2528 assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2530 if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2531 !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2532 xyerror(D_EMPTY, "empty D program translation unit\n");
2534 if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2535 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2537 for (; dnp != NULL; dnp = dnp->dn_list) {
2538 switch (dnp->dn_kind) {
2539 case DT_NODE_CLAUSE:
2540 dt_compile_clause(dtp, dnp);
2542 case DT_NODE_XLATOR:
2543 if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2544 dt_compile_xlator(dnp);
2546 case DT_NODE_PROVIDER:
2547 (void) dt_node_cook(dnp, DT_IDFLG_REF);
2552 yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2553 yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2554 yypcb->pcb_asxrefs = NULL;
2555 yypcb->pcb_asxreflen = 0;
2557 rv = yypcb->pcb_prog;
2561 (void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2562 dt_cg(yypcb, yypcb->pcb_root);
2567 ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2568 err = dt_decl_type(ddp, arg);
2572 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2579 if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2580 DT_TREEDUMP_PASS(dtp, 3))
2581 dt_node_printr(yypcb->pcb_root, stderr, 0);
2583 if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2584 lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2585 ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2586 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2588 if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2589 lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2590 ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2591 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2593 if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2594 (void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2596 dt_pcb_pop(dtp, err);
2597 (void) dt_set_errno(dtp, err);
2598 return (err ? NULL : rv);
2602 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2603 dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2605 return (dt_compile(dtp, DT_CTX_DPROG,
2606 spec, NULL, cflags, argc, argv, NULL, s));
2610 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2611 uint_t cflags, int argc, char *const argv[])
2613 return (dt_compile(dtp, DT_CTX_DPROG,
2614 DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2618 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2620 (void) dt_compile(dtp, DT_CTX_DTYPE,
2621 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2622 return (dtp->dt_errno ? -1 : 0);
2626 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2628 (void) dt_compile(dtp, DT_CTX_DTYPE,
2629 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2630 return (dtp->dt_errno ? -1 : 0);