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