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