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