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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/wait.h> 32 33 #include <dtrace.h> 34 #include <stdlib.h> 35 #include <stdarg.h> 36 #include <stdio.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <limits.h> 40 #include <fcntl.h> 41 #include <errno.h> 42 #include <signal.h> 43 #include <alloca.h> 44 #include <libgen.h> 45 #include <libproc.h> 46 47 typedef struct dtrace_cmd { 48 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */ 49 dtrace_probespec_t dc_spec; /* probe specifier context */ 50 char *dc_arg; /* argument from main argv */ 51 const char *dc_name; /* name for error messages */ 52 const char *dc_desc; /* desc for error messages */ 53 dtrace_prog_t *dc_prog; /* program compiled from arg */ 54 char dc_ofile[PATH_MAX]; /* derived output file name */ 55 } dtrace_cmd_t; 56 57 #define DMODE_VERS 0 /* display version information and exit (-V) */ 58 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */ 59 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */ 60 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */ 61 #define DMODE_LIST 4 /* compile program and list probes (-l) */ 62 #define DMODE_HEADER 5 /* compile program for headergen (-h) */ 63 64 #define E_SUCCESS 0 65 #define E_ERROR 1 66 #define E_USAGE 2 67 68 static const char DTRACE_OPTSTR[] = 69 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z"; 70 71 static char **g_argv; 72 static int g_argc; 73 static char **g_objv; 74 static int g_objc; 75 static dtrace_cmd_t *g_cmdv; 76 static int g_cmdc; 77 static struct ps_prochandle **g_psv; 78 static int g_psc; 79 static int g_pslive; 80 static char *g_pname; 81 static int g_quiet; 82 static int g_flowindent; 83 static int g_intr; 84 static int g_impatient; 85 static int g_newline; 86 static int g_total; 87 static int g_cflags; 88 static int g_oflags; 89 static int g_verbose; 90 static int g_exec = 1; 91 static int g_mode = DMODE_EXEC; 92 static int g_status = E_SUCCESS; 93 static int g_grabanon = 0; 94 static const char *g_ofile = NULL; 95 static FILE *g_ofp = stdout; 96 static dtrace_hdl_t *g_dtp; 97 static char *g_etcfile = "/etc/system"; 98 static const char *g_etcbegin = "* vvvv Added by DTrace"; 99 static const char *g_etcend = "* ^^^^ Added by DTrace"; 100 101 static const char *g_etc[] = { 102 "*", 103 "* The following forceload directives were added by dtrace(1M) to allow for", 104 "* tracing during boot. If these directives are removed, the system will", 105 "* continue to function, but tracing will not occur during boot as desired.", 106 "* To remove these directives (and this block comment) automatically, run", 107 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"", 108 "* chapter of the Solaris Dynamic Tracing Guide for details.", 109 "*", 110 NULL }; 111 112 static int 113 usage(FILE *fp) 114 { 115 static const char predact[] = "[[ predicate ] action ]"; 116 117 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] " 118 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] " 119 "[-o output] [-p pid] [-s script] [-U name]\n\t" 120 "[-x opt[=val]] [-X a|c|s|t]\n\n" 121 "\t[-P provider %s]\n" 122 "\t[-m [ provider: ] module %s]\n" 123 "\t[-f [[ provider: ] module: ] func %s]\n" 124 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n" 125 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname, 126 predact, predact, predact, predact, predact); 127 128 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n"); 129 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n"); 130 131 (void) fprintf(fp, "\n" 132 "\t-32 generate 32-bit D programs and ELF files\n" 133 "\t-64 generate 64-bit D programs and ELF files\n\n" 134 "\t-a claim anonymous tracing state\n" 135 "\t-A generate driver.conf(4) directives for anonymous tracing\n" 136 "\t-b set trace buffer size\n" 137 "\t-c run specified command and exit upon its completion\n" 138 "\t-C run cpp(1) preprocessor on script files\n" 139 "\t-D define symbol when invoking preprocessor\n" 140 "\t-e exit after compiling request but prior to enabling probes\n" 141 "\t-f enable or list probes matching the specified function name\n" 142 "\t-F coalesce trace output by function\n" 143 "\t-G generate an ELF file containing embedded dtrace program\n" 144 "\t-h generate a header file with definitions for static probes\n" 145 "\t-H print included files when invoking preprocessor\n" 146 "\t-i enable or list probes matching the specified probe id\n" 147 "\t-I add include directory to preprocessor search path\n" 148 "\t-l list probes matching specified criteria\n" 149 "\t-L add library directory to library search path\n" 150 "\t-m enable or list probes matching the specified module name\n" 151 "\t-n enable or list probes matching the specified probe name\n" 152 "\t-o set output file\n" 153 "\t-p grab specified process-ID and cache its symbol tables\n" 154 "\t-P enable or list probes matching the specified provider name\n" 155 "\t-q set quiet mode (only output explicitly traced data)\n" 156 "\t-s enable or list probes according to the specified D script\n" 157 "\t-S print D compiler intermediate code\n" 158 "\t-U undefine symbol when invoking preprocessor\n" 159 "\t-v set verbose mode (report stability attributes, arguments)\n" 160 "\t-V report DTrace API version\n" 161 "\t-w permit destructive actions\n" 162 "\t-x enable or modify compiler and tracing options\n" 163 "\t-X specify ISO C conformance settings for preprocessor\n" 164 "\t-Z permit probe descriptions that match zero probes\n"); 165 166 return (E_USAGE); 167 } 168 169 static void 170 verror(const char *fmt, va_list ap) 171 { 172 int error = errno; 173 174 (void) fprintf(stderr, "%s: ", g_pname); 175 (void) vfprintf(stderr, fmt, ap); 176 177 if (fmt[strlen(fmt) - 1] != '\n') 178 (void) fprintf(stderr, ": %s\n", strerror(error)); 179 } 180 181 /*PRINTFLIKE1*/ 182 static void 183 fatal(const char *fmt, ...) 184 { 185 va_list ap; 186 187 va_start(ap, fmt); 188 verror(fmt, ap); 189 va_end(ap); 190 191 exit(E_ERROR); 192 } 193 194 /*PRINTFLIKE1*/ 195 static void 196 dfatal(const char *fmt, ...) 197 { 198 va_list ap; 199 200 va_start(ap, fmt); 201 202 (void) fprintf(stderr, "%s: ", g_pname); 203 if (fmt != NULL) 204 (void) vfprintf(stderr, fmt, ap); 205 206 va_end(ap); 207 208 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') { 209 (void) fprintf(stderr, ": %s\n", 210 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 211 } else if (fmt == NULL) { 212 (void) fprintf(stderr, "%s\n", 213 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 214 } 215 216 /* 217 * Close the DTrace handle to ensure that any controlled processes are 218 * correctly restored and continued. 219 */ 220 dtrace_close(g_dtp); 221 222 exit(E_ERROR); 223 } 224 225 /*PRINTFLIKE1*/ 226 static void 227 error(const char *fmt, ...) 228 { 229 va_list ap; 230 231 va_start(ap, fmt); 232 verror(fmt, ap); 233 va_end(ap); 234 } 235 236 /*PRINTFLIKE1*/ 237 static void 238 notice(const char *fmt, ...) 239 { 240 va_list ap; 241 242 if (g_quiet) 243 return; /* -q or quiet pragma suppresses notice()s */ 244 245 va_start(ap, fmt); 246 verror(fmt, ap); 247 va_end(ap); 248 } 249 250 /*PRINTFLIKE1*/ 251 static void 252 oprintf(const char *fmt, ...) 253 { 254 va_list ap; 255 int n; 256 257 if (g_ofp == NULL) 258 return; 259 260 va_start(ap, fmt); 261 n = vfprintf(g_ofp, fmt, ap); 262 va_end(ap); 263 264 if (n < 0) { 265 if (errno != EINTR) { 266 fatal("failed to write to %s", 267 g_ofile ? g_ofile : "<stdout>"); 268 } 269 clearerr(g_ofp); 270 } 271 } 272 273 static char ** 274 make_argv(char *s) 275 { 276 const char *ws = "\f\n\r\t\v "; 277 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1)); 278 int argc = 0; 279 char *p = s; 280 281 if (argv == NULL) 282 return (NULL); 283 284 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws)) 285 argv[argc++] = p; 286 287 if (argc == 0) 288 argv[argc++] = s; 289 290 argv[argc] = NULL; 291 return (argv); 292 } 293 294 static void 295 dof_prune(const char *fname) 296 { 297 struct stat sbuf; 298 size_t sz, i, j, mark, len; 299 char *buf; 300 int msg = 0, fd; 301 302 if ((fd = open(fname, O_RDONLY)) == -1) { 303 /* 304 * This is okay only if the file doesn't exist at all. 305 */ 306 if (errno != ENOENT) 307 fatal("failed to open %s", fname); 308 return; 309 } 310 311 if (fstat(fd, &sbuf) == -1) 312 fatal("failed to fstat %s", fname); 313 314 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 315 fatal("failed to allocate memory for %s", fname); 316 317 if (read(fd, buf, sz) != sz) 318 fatal("failed to read %s", fname); 319 320 buf[sz] = '\0'; 321 (void) close(fd); 322 323 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1) 324 fatal("failed to open %s for writing", fname); 325 326 len = strlen("dof-data-"); 327 328 for (mark = 0, i = 0; i < sz; i++) { 329 if (strncmp(&buf[i], "dof-data-", len) != 0) 330 continue; 331 332 /* 333 * This is only a match if it's in the 0th column. 334 */ 335 if (i != 0 && buf[i - 1] != '\n') 336 continue; 337 338 if (msg++ == 0) { 339 error("cleaned up old anonymous " 340 "enabling in %s\n", fname); 341 } 342 343 /* 344 * We have a match. First write out our data up until now. 345 */ 346 if (i != mark) { 347 if (write(fd, &buf[mark], i - mark) != i - mark) 348 fatal("failed to write to %s", fname); 349 } 350 351 /* 352 * Now scan forward until we scan past a newline. 353 */ 354 for (j = i; j < sz && buf[j] != '\n'; j++) 355 continue; 356 357 /* 358 * Reset our mark. 359 */ 360 if ((mark = j + 1) >= sz) 361 break; 362 363 i = j; 364 } 365 366 if (mark < sz) { 367 if (write(fd, &buf[mark], sz - mark) != sz - mark) 368 fatal("failed to write to %s", fname); 369 } 370 371 (void) close(fd); 372 free(buf); 373 } 374 375 static void 376 etcsystem_prune(void) 377 { 378 struct stat sbuf; 379 size_t sz; 380 char *buf, *start, *end; 381 int fd; 382 char *fname = g_etcfile, *tmpname; 383 384 if ((fd = open(fname, O_RDONLY)) == -1) 385 fatal("failed to open %s", fname); 386 387 if (fstat(fd, &sbuf) == -1) 388 fatal("failed to fstat %s", fname); 389 390 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 391 fatal("failed to allocate memory for %s", fname); 392 393 if (read(fd, buf, sz) != sz) 394 fatal("failed to read %s", fname); 395 396 buf[sz] = '\0'; 397 (void) close(fd); 398 399 if ((start = strstr(buf, g_etcbegin)) == NULL) 400 goto out; 401 402 if (strlen(buf) != sz) { 403 fatal("embedded nul byte in %s; manual repair of %s " 404 "required\n", fname, fname); 405 } 406 407 if (strstr(start + 1, g_etcbegin) != NULL) { 408 fatal("multiple start sentinels in %s; manual repair of %s " 409 "required\n", fname, fname); 410 } 411 412 if ((end = strstr(buf, g_etcend)) == NULL) { 413 fatal("missing end sentinel in %s; manual repair of %s " 414 "required\n", fname, fname); 415 } 416 417 if (start > end) { 418 fatal("end sentinel preceeds start sentinel in %s; manual " 419 "repair of %s required\n", fname, fname); 420 } 421 422 end += strlen(g_etcend) + 1; 423 bcopy(end, start, strlen(end) + 1); 424 425 tmpname = alloca(sz = strlen(fname) + 80); 426 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid()); 427 428 if ((fd = open(tmpname, 429 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1) 430 fatal("failed to create %s", tmpname); 431 432 if (write(fd, buf, strlen(buf)) < strlen(buf)) { 433 (void) unlink(tmpname); 434 fatal("failed to write to %s", tmpname); 435 } 436 437 (void) close(fd); 438 439 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) { 440 (void) unlink(tmpname); 441 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname, 442 (int)sbuf.st_uid, (int)sbuf.st_gid); 443 } 444 445 if (rename(tmpname, fname) == -1) 446 fatal("rename of %s to %s failed", tmpname, fname); 447 448 error("cleaned up forceload directives in %s\n", fname); 449 out: 450 free(buf); 451 } 452 453 static void 454 etcsystem_add(void) 455 { 456 const char *mods[20]; 457 int nmods, line; 458 459 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL) 460 fatal("failed to open output file '%s'", g_ofile); 461 462 oprintf("%s\n", g_etcbegin); 463 464 for (line = 0; g_etc[line] != NULL; line++) 465 oprintf("%s\n", g_etc[line]); 466 467 nmods = dtrace_provider_modules(g_dtp, mods, 468 sizeof (mods) / sizeof (char *) - 1); 469 470 if (nmods >= sizeof (mods) / sizeof (char *)) 471 fatal("unexpectedly large number of modules!"); 472 473 mods[nmods++] = "dtrace"; 474 475 for (line = 0; line < nmods; line++) 476 oprintf("forceload: drv/%s\n", mods[line]); 477 478 oprintf("%s\n", g_etcend); 479 480 if (fclose(g_ofp) == EOF) 481 fatal("failed to close output file '%s'", g_ofile); 482 483 error("added forceload directives to %s\n", g_ofile); 484 } 485 486 static void 487 print_probe_info(const dtrace_probeinfo_t *p) 488 { 489 char buf[BUFSIZ]; 490 int i; 491 492 oprintf("\n\tProbe Description Attributes\n"); 493 494 oprintf("\t\tIdentifier Names: %s\n", 495 dtrace_stability_name(p->dtp_attr.dtat_name)); 496 oprintf("\t\tData Semantics: %s\n", 497 dtrace_stability_name(p->dtp_attr.dtat_data)); 498 oprintf("\t\tDependency Class: %s\n", 499 dtrace_class_name(p->dtp_attr.dtat_class)); 500 501 oprintf("\n\tArgument Attributes\n"); 502 503 oprintf("\t\tIdentifier Names: %s\n", 504 dtrace_stability_name(p->dtp_arga.dtat_name)); 505 oprintf("\t\tData Semantics: %s\n", 506 dtrace_stability_name(p->dtp_arga.dtat_data)); 507 oprintf("\t\tDependency Class: %s\n", 508 dtrace_class_name(p->dtp_arga.dtat_class)); 509 510 oprintf("\n\tArgument Types\n"); 511 512 for (i = 0; i < p->dtp_argc; i++) { 513 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp, 514 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL) 515 (void) strlcpy(buf, "(unknown)", sizeof (buf)); 516 oprintf("\t\targs[%d]: %s\n", i, buf); 517 } 518 519 if (p->dtp_argc == 0) 520 oprintf("\t\tNone\n"); 521 522 oprintf("\n"); 523 } 524 525 /*ARGSUSED*/ 526 static int 527 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 528 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 529 { 530 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 531 dtrace_probedesc_t *pdp = &edp->dted_probe; 532 dtrace_probeinfo_t p; 533 534 if (edp == *last) 535 return (0); 536 537 oprintf("\n%s:%s:%s:%s\n", 538 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 539 540 if (dtrace_probe_info(dtp, pdp, &p) == 0) 541 print_probe_info(&p); 542 543 *last = edp; 544 return (0); 545 } 546 547 /* 548 * Execute the specified program by enabling the corresponding instrumentation. 549 * If -e has been specified, we get the program info but do not enable it. If 550 * -v has been specified, we print a stability report for the program. 551 */ 552 static void 553 exec_prog(const dtrace_cmd_t *dcp) 554 { 555 dtrace_ecbdesc_t *last = NULL; 556 dtrace_proginfo_t dpi; 557 558 if (!g_exec) { 559 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi); 560 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) { 561 dfatal("failed to enable '%s'", dcp->dc_name); 562 } else { 563 notice("%s '%s' matched %u probe%s\n", 564 dcp->dc_desc, dcp->dc_name, 565 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s"); 566 } 567 568 if (g_verbose) { 569 oprintf("\nStability attributes for %s %s:\n", 570 dcp->dc_desc, dcp->dc_name); 571 572 oprintf("\n\tMinimum Probe Description Attributes\n"); 573 oprintf("\t\tIdentifier Names: %s\n", 574 dtrace_stability_name(dpi.dpi_descattr.dtat_name)); 575 oprintf("\t\tData Semantics: %s\n", 576 dtrace_stability_name(dpi.dpi_descattr.dtat_data)); 577 oprintf("\t\tDependency Class: %s\n", 578 dtrace_class_name(dpi.dpi_descattr.dtat_class)); 579 580 oprintf("\n\tMinimum Statement Attributes\n"); 581 582 oprintf("\t\tIdentifier Names: %s\n", 583 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name)); 584 oprintf("\t\tData Semantics: %s\n", 585 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data)); 586 oprintf("\t\tDependency Class: %s\n", 587 dtrace_class_name(dpi.dpi_stmtattr.dtat_class)); 588 589 if (!g_exec) { 590 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 591 (dtrace_stmt_f *)info_stmt, &last); 592 } else 593 oprintf("\n"); 594 } 595 596 g_total += dpi.dpi_matches; 597 } 598 599 /* 600 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for 601 * storing in a driver.conf(4) file associated with the dtrace driver. 602 */ 603 static void 604 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n) 605 { 606 const uchar_t *p, *q; 607 608 if (dof == NULL) 609 dfatal("failed to create DOF image for '%s'", dcp->dc_name); 610 611 p = (uchar_t *)dof; 612 q = p + dof->dofh_loadsz; 613 614 oprintf("dof-data-%d=0x%x", n, *p++); 615 616 while (p < q) 617 oprintf(",0x%x", *p++); 618 619 oprintf(";\n"); 620 dtrace_dof_destroy(g_dtp, dof); 621 } 622 623 /* 624 * Link the specified D program in DOF form into an ELF file for use in either 625 * helpers, userland provider definitions, or both. If -o was specified, that 626 * path is used as the output file name. If -o wasn't specified and the input 627 * program is from a script whose name is %.d, use basename(%.o) as the output 628 * file name. Otherwise we use "d.out" as the default output file name. 629 */ 630 static void 631 link_prog(dtrace_cmd_t *dcp) 632 { 633 char *p; 634 635 if (g_cmdc == 1 && g_ofile != NULL) { 636 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile)); 637 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL && 638 strcmp(p, ".d") == 0) { 639 p[0] = '\0'; /* strip .d suffix */ 640 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 641 "%s.o", basename(dcp->dc_arg)); 642 } else { 643 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 644 g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv)); 645 } 646 647 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES, 648 dcp->dc_ofile, g_objc, g_objv) != 0) 649 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name); 650 } 651 652 /*ARGSUSED*/ 653 static int 654 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg) 655 { 656 dtrace_probeinfo_t p; 657 658 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id, 659 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 660 661 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0) 662 print_probe_info(&p); 663 664 return (0); 665 } 666 667 /*ARGSUSED*/ 668 static int 669 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 670 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 671 { 672 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 673 674 if (edp == *last) 675 return (0); 676 677 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) { 678 error("failed to match %s:%s:%s:%s: %s\n", 679 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod, 680 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name, 681 dtrace_errmsg(dtp, dtrace_errno(dtp))); 682 } 683 684 *last = edp; 685 return (0); 686 } 687 688 /* 689 * List the probes corresponding to the specified program by iterating over 690 * each statement and then matching probes to the statement probe descriptions. 691 */ 692 static void 693 list_prog(const dtrace_cmd_t *dcp) 694 { 695 dtrace_ecbdesc_t *last = NULL; 696 697 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 698 (dtrace_stmt_f *)list_stmt, &last); 699 } 700 701 static void 702 compile_file(dtrace_cmd_t *dcp) 703 { 704 char *arg0; 705 FILE *fp; 706 707 if ((fp = fopen(dcp->dc_arg, "r")) == NULL) 708 fatal("failed to open %s", dcp->dc_arg); 709 710 arg0 = g_argv[0]; 711 g_argv[0] = dcp->dc_arg; 712 713 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp, 714 g_cflags, g_argc, g_argv)) == NULL) 715 dfatal("failed to compile script %s", dcp->dc_arg); 716 717 g_argv[0] = arg0; 718 (void) fclose(fp); 719 720 dcp->dc_desc = "script"; 721 dcp->dc_name = dcp->dc_arg; 722 } 723 724 static void 725 compile_str(dtrace_cmd_t *dcp) 726 { 727 char *p; 728 729 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg, 730 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL) 731 dfatal("invalid probe specifier %s", dcp->dc_arg); 732 733 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL) 734 *p = '\0'; /* crop name for reporting */ 735 736 dcp->dc_desc = "description"; 737 dcp->dc_name = dcp->dc_arg; 738 } 739 740 /*ARGSUSED*/ 741 static void 742 prochandler(struct ps_prochandle *P, const char *msg, void *arg) 743 { 744 const psinfo_t *prp = Ppsinfo(P); 745 int pid = Pstatus(P)->pr_pid; 746 char name[SIG2STR_MAX]; 747 748 if (msg != NULL) { 749 notice("pid %d: %s\n", pid, msg); 750 return; 751 } 752 753 switch (Pstate(P)) { 754 case PS_UNDEAD: 755 /* 756 * Ideally we would like to always report pr_wstat here, but it 757 * isn't possible given current /proc semantics. If we grabbed 758 * the process, Ppsinfo() will either fail or return a zeroed 759 * psinfo_t depending on how far the parent is in reaping it. 760 * When /proc provides a stable pr_wstat in the status file, 761 * this code can be improved by examining this new pr_wstat. 762 */ 763 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) { 764 notice("pid %d terminated by %s\n", pid, 765 proc_signame(WTERMSIG(prp->pr_wstat), 766 name, sizeof (name))); 767 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) { 768 notice("pid %d exited with status %d\n", 769 pid, WEXITSTATUS(prp->pr_wstat)); 770 } else { 771 notice("pid %d has exited\n", pid); 772 } 773 g_pslive--; 774 break; 775 776 case PS_LOST: 777 notice("pid %d exec'd a set-id or unobservable program\n", pid); 778 g_pslive--; 779 break; 780 } 781 } 782 783 /*ARGSUSED*/ 784 static int 785 errhandler(const dtrace_errdata_t *data, void *arg) 786 { 787 error(data->dteda_msg); 788 return (DTRACE_HANDLE_OK); 789 } 790 791 /*ARGSUSED*/ 792 static int 793 drophandler(const dtrace_dropdata_t *data, void *arg) 794 { 795 error(data->dtdda_msg); 796 return (DTRACE_HANDLE_OK); 797 } 798 799 /*ARGSUSED*/ 800 static int 801 setopthandler(const dtrace_setoptdata_t *data, void *arg) 802 { 803 if (strcmp(data->dtsda_option, "quiet") == 0) 804 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET; 805 806 if (strcmp(data->dtsda_option, "flowindent") == 0) 807 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET; 808 809 return (DTRACE_HANDLE_OK); 810 } 811 812 #define BUFDUMPHDR(hdr) \ 813 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : ""); 814 815 #define BUFDUMPSTR(ptr, field) \ 816 (void) printf("%s: %20s => ", g_pname, #field); \ 817 if ((ptr)->field != NULL) { \ 818 const char *c = (ptr)->field; \ 819 (void) printf("\""); \ 820 do { \ 821 if (*c == '\n') { \ 822 (void) printf("\\n"); \ 823 continue; \ 824 } \ 825 \ 826 (void) printf("%c", *c); \ 827 } while (*c++ != '\0'); \ 828 (void) printf("\"\n"); \ 829 } else { \ 830 (void) printf("<NULL>\n"); \ 831 } 832 833 #define BUFDUMPASSTR(ptr, field, str) \ 834 (void) printf("%s: %20s => %s\n", g_pname, #field, str); 835 836 #define BUFDUMP(ptr, field) \ 837 (void) printf("%s: %20s => %lld\n", g_pname, #field, \ 838 (long long)(ptr)->field); 839 840 #define BUFDUMPPTR(ptr, field) \ 841 (void) printf("%s: %20s => %s\n", g_pname, #field, \ 842 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>"); 843 844 /*ARGSUSED*/ 845 static int 846 bufhandler(const dtrace_bufdata_t *bufdata, void *arg) 847 { 848 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata; 849 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc; 850 const dtrace_probedesc_t *pd; 851 uint32_t flags = bufdata->dtbda_flags; 852 char buf[512], *c = buf, *end = c + sizeof (buf); 853 int i, printed; 854 855 struct { 856 const char *name; 857 uint32_t value; 858 } flagnames[] = { 859 { "AGGVAL", DTRACE_BUFDATA_AGGVAL }, 860 { "AGGKEY", DTRACE_BUFDATA_AGGKEY }, 861 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT }, 862 { "AGGLAST", DTRACE_BUFDATA_AGGLAST }, 863 { "???", UINT32_MAX }, 864 { NULL } 865 }; 866 867 if (bufdata->dtbda_probe != NULL) { 868 pd = bufdata->dtbda_probe->dtpda_pdesc; 869 } else if (agg != NULL) { 870 pd = agg->dtada_pdesc; 871 } else { 872 pd = NULL; 873 } 874 875 BUFDUMPHDR(">>> Called buffer handler"); 876 BUFDUMPHDR(""); 877 878 BUFDUMPHDR(" dtrace_bufdata"); 879 BUFDUMPSTR(bufdata, dtbda_buffered); 880 BUFDUMPPTR(bufdata, dtbda_probe); 881 BUFDUMPPTR(bufdata, dtbda_aggdata); 882 BUFDUMPPTR(bufdata, dtbda_recdesc); 883 884 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags); 885 c += strlen(c); 886 887 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) { 888 if (!(flags & flagnames[i].value)) 889 continue; 890 891 (void) snprintf(c, end - c, 892 "%s%s", printed++ ? " | " : "(", flagnames[i].name); 893 c += strlen(c); 894 flags &= ~flagnames[i].value; 895 } 896 897 if (printed) 898 (void) snprintf(c, end - c, ")"); 899 900 BUFDUMPASSTR(bufdata, dtbda_flags, buf); 901 BUFDUMPHDR(""); 902 903 if (pd != NULL) { 904 BUFDUMPHDR(" dtrace_probedesc"); 905 BUFDUMPSTR(pd, dtpd_provider); 906 BUFDUMPSTR(pd, dtpd_mod); 907 BUFDUMPSTR(pd, dtpd_func); 908 BUFDUMPSTR(pd, dtpd_name); 909 BUFDUMPHDR(""); 910 } 911 912 if (rec != NULL) { 913 BUFDUMPHDR(" dtrace_recdesc"); 914 BUFDUMP(rec, dtrd_action); 915 BUFDUMP(rec, dtrd_size); 916 917 if (agg != NULL) { 918 uint8_t *data; 919 int lim = rec->dtrd_size; 920 921 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset); 922 c = buf + strlen(buf); 923 924 if (lim > sizeof (uint64_t)) 925 lim = sizeof (uint64_t); 926 927 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset; 928 929 for (i = 0; i < lim; i++) { 930 (void) snprintf(c, end - c, "%s%02x", 931 i == 0 ? "" : " ", *data++); 932 c += strlen(c); 933 } 934 935 (void) snprintf(c, end - c, 936 "%s)", lim < rec->dtrd_size ? " ..." : ""); 937 BUFDUMPASSTR(rec, dtrd_offset, buf); 938 } else { 939 BUFDUMP(rec, dtrd_offset); 940 } 941 942 BUFDUMPHDR(""); 943 } 944 945 if (agg != NULL) { 946 dtrace_aggdesc_t *desc = agg->dtada_desc; 947 948 BUFDUMPHDR(" dtrace_aggdesc"); 949 BUFDUMPSTR(desc, dtagd_name); 950 BUFDUMP(desc, dtagd_varid); 951 BUFDUMP(desc, dtagd_id); 952 BUFDUMP(desc, dtagd_nrecs); 953 BUFDUMPHDR(""); 954 } 955 956 return (DTRACE_HANDLE_OK); 957 } 958 959 /*ARGSUSED*/ 960 static int 961 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg) 962 { 963 dtrace_actkind_t act; 964 uintptr_t addr; 965 966 if (rec == NULL) { 967 /* 968 * We have processed the final record; output the newline if 969 * we're not in quiet mode. 970 */ 971 if (!g_quiet) 972 oprintf("\n"); 973 974 return (DTRACE_CONSUME_NEXT); 975 } 976 977 act = rec->dtrd_action; 978 addr = (uintptr_t)data->dtpda_data; 979 980 if (act == DTRACEACT_EXIT) { 981 g_status = *((uint32_t *)addr); 982 return (DTRACE_CONSUME_NEXT); 983 } 984 985 return (DTRACE_CONSUME_THIS); 986 } 987 988 /*ARGSUSED*/ 989 static int 990 chew(const dtrace_probedata_t *data, void *arg) 991 { 992 dtrace_probedesc_t *pd = data->dtpda_pdesc; 993 processorid_t cpu = data->dtpda_cpu; 994 static int heading; 995 996 if (g_impatient) { 997 g_newline = 0; 998 return (DTRACE_CONSUME_ABORT); 999 } 1000 1001 if (heading == 0) { 1002 if (!g_flowindent) { 1003 if (!g_quiet) { 1004 oprintf("%3s %6s %32s\n", 1005 "CPU", "ID", "FUNCTION:NAME"); 1006 } 1007 } else { 1008 oprintf("%3s %-41s\n", "CPU", "FUNCTION"); 1009 } 1010 heading = 1; 1011 } 1012 1013 if (!g_flowindent) { 1014 if (!g_quiet) { 1015 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2]; 1016 1017 (void) snprintf(name, sizeof (name), "%s:%s", 1018 pd->dtpd_func, pd->dtpd_name); 1019 1020 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name); 1021 } 1022 } else { 1023 int indent = data->dtpda_indent; 1024 char *name; 1025 size_t len; 1026 1027 if (data->dtpda_flow == DTRACEFLOW_NONE) { 1028 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5; 1029 name = alloca(len); 1030 (void) snprintf(name, len, "%*s%s%s:%s", indent, "", 1031 data->dtpda_prefix, pd->dtpd_func, 1032 pd->dtpd_name); 1033 } else { 1034 len = indent + DTRACE_FUNCNAMELEN + 5; 1035 name = alloca(len); 1036 (void) snprintf(name, len, "%*s%s%s", indent, "", 1037 data->dtpda_prefix, pd->dtpd_func); 1038 } 1039 1040 oprintf("%3d %-41s ", cpu, name); 1041 } 1042 1043 return (DTRACE_CONSUME_THIS); 1044 } 1045 1046 static void 1047 go(void) 1048 { 1049 int i; 1050 1051 struct { 1052 char *name; 1053 char *optname; 1054 dtrace_optval_t val; 1055 } bufs[] = { 1056 { "buffer size", "bufsize" }, 1057 { "aggregation size", "aggsize" }, 1058 { "speculation size", "specsize" }, 1059 { "dynamic variable size", "dynvarsize" }, 1060 { NULL } 1061 }, rates[] = { 1062 { "cleaning rate", "cleanrate" }, 1063 { "status rate", "statusrate" }, 1064 { NULL } 1065 }; 1066 1067 for (i = 0; bufs[i].name != NULL; i++) { 1068 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1) 1069 fatal("couldn't get option %s", bufs[i].optname); 1070 } 1071 1072 for (i = 0; rates[i].name != NULL; i++) { 1073 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1) 1074 fatal("couldn't get option %s", rates[i].optname); 1075 } 1076 1077 if (dtrace_go(g_dtp) == -1) 1078 dfatal("could not enable tracing"); 1079 1080 for (i = 0; bufs[i].name != NULL; i++) { 1081 dtrace_optval_t j = 0, mul = 10; 1082 dtrace_optval_t nsize; 1083 1084 if (bufs[i].val == DTRACEOPT_UNSET) 1085 continue; 1086 1087 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize); 1088 1089 if (nsize == DTRACEOPT_UNSET || nsize == 0) 1090 continue; 1091 1092 if (nsize >= bufs[i].val - sizeof (uint64_t)) 1093 continue; 1094 1095 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10) 1096 continue; 1097 1098 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) { 1099 error("%s lowered to %lld%c\n", bufs[i].name, 1100 (long long)nsize >> (mul - 10), " kmgtpe"[j]); 1101 } else { 1102 error("%s lowered to %lld bytes\n", bufs[i].name, 1103 (long long)nsize); 1104 } 1105 } 1106 1107 for (i = 0; rates[i].name != NULL; i++) { 1108 dtrace_optval_t nval; 1109 char *dir; 1110 1111 if (rates[i].val == DTRACEOPT_UNSET) 1112 continue; 1113 1114 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval); 1115 1116 if (nval == DTRACEOPT_UNSET || nval == 0) 1117 continue; 1118 1119 if (rates[i].val == nval) 1120 continue; 1121 1122 dir = nval > rates[i].val ? "reduced" : "increased"; 1123 1124 if (nval <= NANOSEC && (NANOSEC % nval) == 0) { 1125 error("%s %s to %lld hz\n", rates[i].name, dir, 1126 (long long)NANOSEC / (long long)nval); 1127 continue; 1128 } 1129 1130 if ((nval % NANOSEC) == 0) { 1131 error("%s %s to once every %lld seconds\n", 1132 rates[i].name, dir, 1133 (long long)nval / (long long)NANOSEC); 1134 continue; 1135 } 1136 1137 error("%s %s to once every %lld nanoseconds\n", 1138 rates[i].name, dir, (long long)nval); 1139 } 1140 } 1141 1142 /*ARGSUSED*/ 1143 static void 1144 intr(int signo) 1145 { 1146 if (!g_intr) 1147 g_newline = 1; 1148 1149 if (g_intr++) 1150 g_impatient = 1; 1151 } 1152 1153 int 1154 main(int argc, char *argv[]) 1155 { 1156 dtrace_bufdesc_t buf; 1157 struct sigaction act, oact; 1158 dtrace_status_t status[2]; 1159 dtrace_optval_t opt; 1160 dtrace_cmd_t *dcp; 1161 1162 int done = 0, mode = 0; 1163 int err, i; 1164 char c, *p, **v; 1165 struct ps_prochandle *P; 1166 pid_t pid; 1167 1168 g_pname = basename(argv[0]); 1169 1170 if (argc == 1) 1171 return (usage(stderr)); 1172 1173 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL || 1174 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL || 1175 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL) 1176 fatal("failed to allocate memory for arguments"); 1177 1178 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */ 1179 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */ 1180 1181 bzero(status, sizeof (status)); 1182 bzero(&buf, sizeof (buf)); 1183 1184 /* 1185 * Make an initial pass through argv[] processing any arguments that 1186 * affect our behavior mode (g_mode) and flags used for dtrace_open(). 1187 * We also accumulate arguments that are not affiliated with getopt 1188 * options into g_argv[], and abort if any invalid options are found. 1189 */ 1190 for (optind = 1; optind < argc; optind++) { 1191 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 1192 switch (c) { 1193 case '3': 1194 if (strcmp(optarg, "2") != 0) { 1195 (void) fprintf(stderr, 1196 "%s: illegal option -- 3%s\n", 1197 argv[0], optarg); 1198 return (usage(stderr)); 1199 } 1200 g_oflags &= ~DTRACE_O_LP64; 1201 g_oflags |= DTRACE_O_ILP32; 1202 break; 1203 1204 case '6': 1205 if (strcmp(optarg, "4") != 0) { 1206 (void) fprintf(stderr, 1207 "%s: illegal option -- 6%s\n", 1208 argv[0], optarg); 1209 return (usage(stderr)); 1210 } 1211 g_oflags &= ~DTRACE_O_ILP32; 1212 g_oflags |= DTRACE_O_LP64; 1213 break; 1214 1215 case 'a': 1216 g_grabanon++; /* also checked in pass 2 below */ 1217 break; 1218 1219 case 'A': 1220 g_mode = DMODE_ANON; 1221 g_exec = 0; 1222 mode++; 1223 break; 1224 1225 case 'e': 1226 g_exec = 0; 1227 done = 1; 1228 break; 1229 1230 case 'h': 1231 g_mode = DMODE_HEADER; 1232 g_oflags |= DTRACE_O_NODEV; 1233 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */ 1234 g_exec = 0; 1235 mode++; 1236 break; 1237 1238 case 'G': 1239 g_mode = DMODE_LINK; 1240 g_oflags |= DTRACE_O_NODEV; 1241 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */ 1242 g_exec = 0; 1243 mode++; 1244 break; 1245 1246 case 'l': 1247 g_mode = DMODE_LIST; 1248 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */ 1249 mode++; 1250 break; 1251 1252 case 'V': 1253 g_mode = DMODE_VERS; 1254 mode++; 1255 break; 1256 1257 default: 1258 if (strchr(DTRACE_OPTSTR, c) == NULL) 1259 return (usage(stderr)); 1260 } 1261 } 1262 1263 if (optind < argc) 1264 g_argv[g_argc++] = argv[optind]; 1265 } 1266 1267 if (mode > 1) { 1268 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options " 1269 "can be specified at a time\n", g_pname); 1270 return (E_USAGE); 1271 } 1272 1273 if (g_mode == DMODE_VERS) 1274 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0); 1275 1276 /* 1277 * If we're in linker mode and the data model hasn't been specified, 1278 * we try to guess the appropriate setting by examining the object 1279 * files. We ignore certain errors since we'll catch them later when 1280 * we actually process the object files. 1281 */ 1282 if (g_mode == DMODE_LINK && 1283 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 && 1284 elf_version(EV_CURRENT) != EV_NONE) { 1285 int fd; 1286 Elf *elf; 1287 GElf_Ehdr ehdr; 1288 1289 for (i = 1; i < g_argc; i++) { 1290 if ((fd = open64(g_argv[i], O_RDONLY)) == -1) 1291 break; 1292 1293 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1294 (void) close(fd); 1295 break; 1296 } 1297 1298 if (elf_kind(elf) != ELF_K_ELF || 1299 gelf_getehdr(elf, &ehdr) == NULL) { 1300 (void) close(fd); 1301 (void) elf_end(elf); 1302 break; 1303 } 1304 1305 (void) close(fd); 1306 (void) elf_end(elf); 1307 1308 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { 1309 if (g_oflags & DTRACE_O_ILP32) { 1310 fatal("can't mix 32-bit and 64-bit " 1311 "object files\n"); 1312 } 1313 g_oflags |= DTRACE_O_LP64; 1314 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) { 1315 if (g_oflags & DTRACE_O_LP64) { 1316 fatal("can't mix 32-bit and 64-bit " 1317 "object files\n"); 1318 } 1319 g_oflags |= DTRACE_O_ILP32; 1320 } else { 1321 break; 1322 } 1323 } 1324 } 1325 1326 /* 1327 * Open libdtrace. If we are not actually going to be enabling any 1328 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV. 1329 */ 1330 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) { 1331 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) { 1332 g_oflags |= DTRACE_O_NODEV; 1333 continue; 1334 } 1335 1336 fatal("failed to initialize dtrace: %s\n", 1337 dtrace_errmsg(NULL, err)); 1338 } 1339 1340 (void) dtrace_setopt(g_dtp, "bufsize", "4m"); 1341 (void) dtrace_setopt(g_dtp, "aggsize", "4m"); 1342 1343 /* 1344 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit 1345 * references to undefined symbols to remain as unresolved relocations. 1346 * If -A is specified, enable -xlink=primary to permit static linking 1347 * only to kernel symbols that are defined in a primary kernel module. 1348 */ 1349 if (g_mode == DMODE_LINK) { 1350 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic"); 1351 (void) dtrace_setopt(g_dtp, "unodefs", NULL); 1352 1353 /* 1354 * Use the remaining arguments as the list of object files 1355 * when in linker mode. 1356 */ 1357 g_objc = g_argc - 1; 1358 g_objv = g_argv + 1; 1359 1360 /* 1361 * We still use g_argv[0], the name of the executable. 1362 */ 1363 g_argc = 1; 1364 } else if (g_mode == DMODE_ANON) 1365 (void) dtrace_setopt(g_dtp, "linkmode", "primary"); 1366 1367 /* 1368 * Now that we have libdtrace open, make a second pass through argv[] 1369 * to perform any dtrace_setopt() calls and change any compiler flags. 1370 * We also accumulate any program specifications into our g_cmdv[] at 1371 * this time; these will compiled as part of the fourth processing pass. 1372 */ 1373 for (optind = 1; optind < argc; optind++) { 1374 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 1375 switch (c) { 1376 case 'a': 1377 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0) 1378 dfatal("failed to set -a"); 1379 break; 1380 1381 case 'b': 1382 if (dtrace_setopt(g_dtp, 1383 "bufsize", optarg) != 0) 1384 dfatal("failed to set -b %s", optarg); 1385 break; 1386 1387 case 'B': 1388 g_ofp = NULL; 1389 break; 1390 1391 case 'C': 1392 g_cflags |= DTRACE_C_CPP; 1393 break; 1394 1395 case 'D': 1396 if (dtrace_setopt(g_dtp, "define", optarg) != 0) 1397 dfatal("failed to set -D %s", optarg); 1398 break; 1399 1400 case 'f': 1401 dcp = &g_cmdv[g_cmdc++]; 1402 dcp->dc_func = compile_str; 1403 dcp->dc_spec = DTRACE_PROBESPEC_FUNC; 1404 dcp->dc_arg = optarg; 1405 break; 1406 1407 case 'F': 1408 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0) 1409 dfatal("failed to set -F"); 1410 break; 1411 1412 case 'H': 1413 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0) 1414 dfatal("failed to set -H"); 1415 break; 1416 1417 case 'i': 1418 dcp = &g_cmdv[g_cmdc++]; 1419 dcp->dc_func = compile_str; 1420 dcp->dc_spec = DTRACE_PROBESPEC_NAME; 1421 dcp->dc_arg = optarg; 1422 break; 1423 1424 case 'I': 1425 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0) 1426 dfatal("failed to set -I %s", optarg); 1427 break; 1428 1429 case 'L': 1430 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0) 1431 dfatal("failed to set -L %s", optarg); 1432 break; 1433 1434 case 'm': 1435 dcp = &g_cmdv[g_cmdc++]; 1436 dcp->dc_func = compile_str; 1437 dcp->dc_spec = DTRACE_PROBESPEC_MOD; 1438 dcp->dc_arg = optarg; 1439 break; 1440 1441 case 'n': 1442 dcp = &g_cmdv[g_cmdc++]; 1443 dcp->dc_func = compile_str; 1444 dcp->dc_spec = DTRACE_PROBESPEC_NAME; 1445 dcp->dc_arg = optarg; 1446 break; 1447 1448 case 'P': 1449 dcp = &g_cmdv[g_cmdc++]; 1450 dcp->dc_func = compile_str; 1451 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER; 1452 dcp->dc_arg = optarg; 1453 break; 1454 1455 case 'q': 1456 if (dtrace_setopt(g_dtp, "quiet", 0) != 0) 1457 dfatal("failed to set -q"); 1458 break; 1459 1460 case 'o': 1461 g_ofile = optarg; 1462 break; 1463 1464 case 's': 1465 dcp = &g_cmdv[g_cmdc++]; 1466 dcp->dc_func = compile_file; 1467 dcp->dc_spec = DTRACE_PROBESPEC_NONE; 1468 dcp->dc_arg = optarg; 1469 break; 1470 1471 case 'S': 1472 g_cflags |= DTRACE_C_DIFV; 1473 break; 1474 1475 case 'U': 1476 if (dtrace_setopt(g_dtp, "undef", optarg) != 0) 1477 dfatal("failed to set -U %s", optarg); 1478 break; 1479 1480 case 'v': 1481 g_verbose++; 1482 break; 1483 1484 case 'w': 1485 if (dtrace_setopt(g_dtp, "destructive", 0) != 0) 1486 dfatal("failed to set -w"); 1487 break; 1488 1489 case 'x': 1490 if ((p = strchr(optarg, '=')) != NULL) 1491 *p++ = '\0'; 1492 1493 if (dtrace_setopt(g_dtp, optarg, p) != 0) 1494 dfatal("failed to set -x %s", optarg); 1495 break; 1496 1497 case 'X': 1498 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0) 1499 dfatal("failed to set -X %s", optarg); 1500 break; 1501 1502 case 'Z': 1503 g_cflags |= DTRACE_C_ZDEFS; 1504 break; 1505 1506 default: 1507 if (strchr(DTRACE_OPTSTR, c) == NULL) 1508 return (usage(stderr)); 1509 } 1510 } 1511 } 1512 1513 if (g_ofp == NULL && g_mode != DMODE_EXEC) { 1514 (void) fprintf(stderr, "%s: -B not valid in combination" 1515 " with [-AGl] options\n", g_pname); 1516 return (E_USAGE); 1517 } 1518 1519 if (g_ofp == NULL && g_ofile != NULL) { 1520 (void) fprintf(stderr, "%s: -B not valid in combination" 1521 " with -o option\n", g_pname); 1522 return (E_USAGE); 1523 } 1524 1525 /* 1526 * In our third pass we handle any command-line options related to 1527 * grabbing or creating victim processes. The behavior of these calls 1528 * may been affected by any library options set by the second pass. 1529 */ 1530 for (optind = 1; optind < argc; optind++) { 1531 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 1532 switch (c) { 1533 case 'c': 1534 if ((v = make_argv(optarg)) == NULL) 1535 fatal("failed to allocate memory"); 1536 1537 P = dtrace_proc_create(g_dtp, v[0], v); 1538 if (P == NULL) 1539 dfatal(NULL); /* dtrace_errmsg() only */ 1540 1541 g_psv[g_psc++] = P; 1542 free(v); 1543 break; 1544 1545 case 'p': 1546 errno = 0; 1547 pid = strtol(optarg, &p, 10); 1548 1549 if (errno != 0 || p == optarg || p[0] != '\0') 1550 fatal("invalid pid: %s\n", optarg); 1551 1552 P = dtrace_proc_grab(g_dtp, pid, 0); 1553 if (P == NULL) 1554 dfatal(NULL); /* dtrace_errmsg() only */ 1555 1556 g_psv[g_psc++] = P; 1557 break; 1558 } 1559 } 1560 } 1561 1562 /* 1563 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert 1564 * each string or file specification into a compiled program structure. 1565 */ 1566 for (i = 0; i < g_cmdc; i++) 1567 g_cmdv[i].dc_func(&g_cmdv[i]); 1568 1569 if (g_mode != DMODE_LIST) { 1570 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1) 1571 dfatal("failed to establish error handler"); 1572 1573 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1) 1574 dfatal("failed to establish drop handler"); 1575 1576 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1) 1577 dfatal("failed to establish proc handler"); 1578 1579 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1) 1580 dfatal("failed to establish setopt handler"); 1581 1582 if (g_ofp == NULL && 1583 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1) 1584 dfatal("failed to establish buffered handler"); 1585 } 1586 1587 (void) dtrace_getopt(g_dtp, "flowindent", &opt); 1588 g_flowindent = opt != DTRACEOPT_UNSET; 1589 1590 (void) dtrace_getopt(g_dtp, "grabanon", &opt); 1591 g_grabanon = opt != DTRACEOPT_UNSET; 1592 1593 (void) dtrace_getopt(g_dtp, "quiet", &opt); 1594 g_quiet = opt != DTRACEOPT_UNSET; 1595 1596 /* 1597 * Now make a fifth and final pass over the options that have been 1598 * turned into programs and saved in g_cmdv[], performing any mode- 1599 * specific processing. If g_mode is DMODE_EXEC, we will break out 1600 * of the switch() and continue on to the data processing loop. For 1601 * other modes, we will exit dtrace once mode-specific work is done. 1602 */ 1603 switch (g_mode) { 1604 case DMODE_EXEC: 1605 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 1606 fatal("failed to open output file '%s'", g_ofile); 1607 1608 for (i = 0; i < g_cmdc; i++) 1609 exec_prog(&g_cmdv[i]); 1610 1611 if (done && !g_grabanon) { 1612 dtrace_close(g_dtp); 1613 return (g_status); 1614 } 1615 break; 1616 1617 case DMODE_ANON: 1618 if (g_ofile == NULL) 1619 g_ofile = "/kernel/drv/dtrace.conf"; 1620 1621 dof_prune(g_ofile); /* strip out any old DOF directives */ 1622 etcsystem_prune(); /* string out any forceload directives */ 1623 1624 if (g_cmdc == 0) { 1625 dtrace_close(g_dtp); 1626 return (g_status); 1627 } 1628 1629 if ((g_ofp = fopen(g_ofile, "a")) == NULL) 1630 fatal("failed to open output file '%s'", g_ofile); 1631 1632 for (i = 0; i < g_cmdc; i++) { 1633 anon_prog(&g_cmdv[i], 1634 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i); 1635 } 1636 1637 /* 1638 * Dump out the DOF corresponding to the error handler and the 1639 * current options as the final DOF property in the .conf file. 1640 */ 1641 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++); 1642 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++); 1643 1644 if (fclose(g_ofp) == EOF) 1645 fatal("failed to close output file '%s'", g_ofile); 1646 1647 /* 1648 * These messages would use notice() rather than error(), but 1649 * we don't want them suppressed when -A is run on a D program 1650 * that itself contains a #pragma D option quiet. 1651 */ 1652 error("saved anonymous enabling in %s\n", g_ofile); 1653 etcsystem_add(); 1654 error("run update_drv(1M) or reboot to enable changes\n"); 1655 1656 dtrace_close(g_dtp); 1657 return (g_status); 1658 1659 case DMODE_LINK: 1660 if (g_cmdc == 0) { 1661 (void) fprintf(stderr, "%s: -G requires one or more " 1662 "scripts or enabling options\n", g_pname); 1663 dtrace_close(g_dtp); 1664 return (E_USAGE); 1665 } 1666 1667 for (i = 0; i < g_cmdc; i++) 1668 link_prog(&g_cmdv[i]); 1669 1670 if (g_cmdc > 1 && g_ofile != NULL) { 1671 char **objv = alloca(g_cmdc * sizeof (char *)); 1672 1673 for (i = 0; i < g_cmdc; i++) 1674 objv[i] = g_cmdv[i].dc_ofile; 1675 1676 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES, 1677 g_ofile, g_cmdc, objv) != 0) 1678 dfatal(NULL); /* dtrace_errmsg() only */ 1679 } 1680 1681 dtrace_close(g_dtp); 1682 return (g_status); 1683 1684 case DMODE_LIST: 1685 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 1686 fatal("failed to open output file '%s'", g_ofile); 1687 1688 oprintf("%5s %10s %17s %33s %s\n", 1689 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME"); 1690 1691 for (i = 0; i < g_cmdc; i++) 1692 list_prog(&g_cmdv[i]); 1693 1694 if (g_cmdc == 0) 1695 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL); 1696 1697 dtrace_close(g_dtp); 1698 return (g_status); 1699 1700 case DMODE_HEADER: 1701 if (g_cmdc == 0) { 1702 (void) fprintf(stderr, "%s: -h requires one or more " 1703 "scripts or enabling options\n", g_pname); 1704 dtrace_close(g_dtp); 1705 return (E_USAGE); 1706 } 1707 1708 if (g_ofile == NULL) { 1709 char *p; 1710 1711 if (g_cmdc > 1) { 1712 (void) fprintf(stderr, "%s: -h requires an " 1713 "output file if multiple scripts are " 1714 "specified\n", g_pname); 1715 dtrace_close(g_dtp); 1716 return (E_USAGE); 1717 } 1718 1719 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL || 1720 strcmp(p, ".d") != 0) { 1721 (void) fprintf(stderr, "%s: -h requires an " 1722 "output file if no scripts are " 1723 "specified\n", g_pname); 1724 dtrace_close(g_dtp); 1725 return (E_USAGE); 1726 } 1727 1728 p[0] = '\0'; /* strip .d suffix */ 1729 g_ofile = p = g_cmdv[0].dc_ofile; 1730 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile), 1731 "%s.h", basename(g_cmdv[0].dc_arg)); 1732 } 1733 1734 if ((g_ofp = fopen(g_ofile, "w")) == NULL) 1735 fatal("failed to open header file '%s'", g_ofile); 1736 1737 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n"); 1738 1739 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 || 1740 fclose(g_ofp) == EOF) 1741 dfatal("failed to create header file %s", g_ofile); 1742 1743 dtrace_close(g_dtp); 1744 return (g_status); 1745 } 1746 1747 /* 1748 * If -a and -Z were not specified and no probes have been matched, no 1749 * probe criteria was specified on the command line and we abort. 1750 */ 1751 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS)) 1752 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified"); 1753 1754 /* 1755 * Start tracing. Once we dtrace_go(), reload any options that affect 1756 * our globals in case consuming anonymous state has changed them. 1757 */ 1758 go(); 1759 1760 (void) dtrace_getopt(g_dtp, "flowindent", &opt); 1761 g_flowindent = opt != DTRACEOPT_UNSET; 1762 1763 (void) dtrace_getopt(g_dtp, "grabanon", &opt); 1764 g_grabanon = opt != DTRACEOPT_UNSET; 1765 1766 (void) dtrace_getopt(g_dtp, "quiet", &opt); 1767 g_quiet = opt != DTRACEOPT_UNSET; 1768 1769 (void) dtrace_getopt(g_dtp, "destructive", &opt); 1770 if (opt != DTRACEOPT_UNSET) 1771 notice("allowing destructive actions\n"); 1772 1773 (void) sigemptyset(&act.sa_mask); 1774 act.sa_flags = 0; 1775 act.sa_handler = intr; 1776 1777 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN) 1778 (void) sigaction(SIGINT, &act, NULL); 1779 1780 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN) 1781 (void) sigaction(SIGTERM, &act, NULL); 1782 1783 /* 1784 * Now that tracing is active and we are ready to consume trace data, 1785 * continue any grabbed or created processes, setting them running 1786 * using the /proc control mechanism inside of libdtrace. 1787 */ 1788 for (i = 0; i < g_psc; i++) 1789 dtrace_proc_continue(g_dtp, g_psv[i]); 1790 1791 g_pslive = g_psc; /* count for prochandler() */ 1792 1793 do { 1794 if (!g_intr && !done) 1795 dtrace_sleep(g_dtp); 1796 1797 if (g_newline) { 1798 /* 1799 * Output a newline just to make the output look 1800 * slightly cleaner. Note that we do this even in 1801 * "quiet" mode... 1802 */ 1803 oprintf("\n"); 1804 g_newline = 0; 1805 } 1806 1807 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) { 1808 done = 1; 1809 if (dtrace_stop(g_dtp) == -1) 1810 dfatal("couldn't stop tracing"); 1811 } 1812 1813 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) { 1814 case DTRACE_WORKSTATUS_DONE: 1815 done = 1; 1816 break; 1817 case DTRACE_WORKSTATUS_OKAY: 1818 break; 1819 default: 1820 if (!g_impatient && dtrace_errno(g_dtp) != EINTR) 1821 dfatal("processing aborted"); 1822 } 1823 1824 if (g_ofp != NULL && fflush(g_ofp) == EOF) 1825 clearerr(g_ofp); 1826 } while (!done); 1827 1828 oprintf("\n"); 1829 1830 if (!g_impatient) { 1831 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 && 1832 dtrace_errno(g_dtp) != EINTR) 1833 dfatal("failed to print aggregations"); 1834 } 1835 1836 dtrace_close(g_dtp); 1837 return (g_status); 1838 } 1839