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