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