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