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