1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <sys/wait.h> 32 33 #include <dtrace.h> 34 #include <stdlib.h> 35 #include <stdarg.h> 36 #include <stdio.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <limits.h> 40 #include <fcntl.h> 41 #include <errno.h> 42 #include <signal.h> 43 #include <alloca.h> 44 #include <libgen.h> 45 #include <libproc.h> 46 47 typedef struct dtrace_cmd { 48 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */ 49 dtrace_probespec_t dc_spec; /* probe specifier context */ 50 char *dc_arg; /* argument from main argv */ 51 const char *dc_name; /* name for error messages */ 52 const char *dc_desc; /* desc for error messages */ 53 dtrace_prog_t *dc_prog; /* program compiled from arg */ 54 char dc_ofile[PATH_MAX]; /* derived output file name */ 55 } dtrace_cmd_t; 56 57 #define DMODE_VERS 0 /* display version information and exit (-V) */ 58 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */ 59 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */ 60 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */ 61 #define DMODE_LIST 4 /* compile program and list probes (-l) */ 62 #define DMODE_HEADER 5 /* compile program for headergen (-h) */ 63 64 #define E_SUCCESS 0 65 #define E_ERROR 1 66 #define E_USAGE 2 67 68 static const char DTRACE_OPTSTR[] = 69 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z"; 70 71 static char **g_argv; 72 static int g_argc; 73 static char **g_objv; 74 static int g_objc; 75 static dtrace_cmd_t *g_cmdv; 76 static int g_cmdc; 77 static struct ps_prochandle **g_psv; 78 static int g_psc; 79 static int g_pslive; 80 static char *g_pname; 81 static int g_quiet; 82 static int g_flowindent; 83 static int g_intr; 84 static int g_impatient; 85 static int g_newline; 86 static int g_total; 87 static int g_cflags; 88 static int g_oflags; 89 static int g_verbose; 90 static int g_exec = 1; 91 static int g_mode = DMODE_EXEC; 92 static int g_status = E_SUCCESS; 93 static int g_grabanon = 0; 94 static const char *g_ofile = NULL; 95 static FILE *g_ofp = stdout; 96 static dtrace_hdl_t *g_dtp; 97 static char *g_etcfile = "/etc/system"; 98 static const char *g_etcbegin = "* vvvv Added by DTrace"; 99 static const char *g_etcend = "* ^^^^ Added by DTrace"; 100 101 static const char *g_etc[] = { 102 "*", 103 "* The following forceload directives were added by dtrace(1M) to allow for", 104 "* tracing during boot. If these directives are removed, the system will", 105 "* continue to function, but tracing will not occur during boot as desired.", 106 "* To remove these directives (and this block comment) automatically, run", 107 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"", 108 "* chapter of the Solaris Dynamic Tracing Guide for details.", 109 "*", 110 NULL }; 111 112 static int 113 usage(FILE *fp) 114 { 115 static const char predact[] = "[[ predicate ] action ]"; 116 117 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] " 118 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] " 119 "[-o output] [-p pid] [-s script] [-U name]\n\t" 120 "[-x opt[=val]] [-X a|c|s|t]\n\n" 121 "\t[-P provider %s]\n" 122 "\t[-m [ provider: ] module %s]\n" 123 "\t[-f [[ provider: ] module: ] func %s]\n" 124 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n" 125 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname, 126 predact, predact, predact, predact, predact); 127 128 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n"); 129 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n"); 130 131 (void) fprintf(fp, "\n" 132 "\t-32 generate 32-bit D programs and ELF files\n" 133 "\t-64 generate 64-bit D programs and ELF files\n\n" 134 "\t-a claim anonymous tracing state\n" 135 "\t-A generate driver.conf(4) directives for anonymous tracing\n" 136 "\t-b set trace buffer size\n" 137 "\t-c run specified command and exit upon its completion\n" 138 "\t-C run cpp(1) preprocessor on script files\n" 139 "\t-D define symbol when invoking preprocessor\n" 140 "\t-e exit after compiling request but prior to enabling probes\n" 141 "\t-f enable or list probes matching the specified function name\n" 142 "\t-F coalesce trace output by function\n" 143 "\t-G generate an ELF file containing embedded dtrace program\n" 144 "\t-h generate a header file with definitions for static probes\n" 145 "\t-H print included files when invoking preprocessor\n" 146 "\t-i enable or list probes matching the specified probe id\n" 147 "\t-I add include directory to preprocessor search path\n" 148 "\t-l list probes matching specified criteria\n" 149 "\t-L add library directory to library search path\n" 150 "\t-m enable or list probes matching the specified module name\n" 151 "\t-n enable or list probes matching the specified probe name\n" 152 "\t-o set output file\n" 153 "\t-p grab specified process-ID and cache its symbol tables\n" 154 "\t-P enable or list probes matching the specified provider name\n" 155 "\t-q set quiet mode (only output explicitly traced data)\n" 156 "\t-s enable or list probes according to the specified D script\n" 157 "\t-S print D compiler intermediate code\n" 158 "\t-U undefine symbol when invoking preprocessor\n" 159 "\t-v set verbose mode (report stability attributes, arguments)\n" 160 "\t-V report DTrace API version\n" 161 "\t-w permit destructive actions\n" 162 "\t-x enable or modify compiler and tracing options\n" 163 "\t-X specify ISO C conformance settings for preprocessor\n" 164 "\t-Z permit probe descriptions that match zero probes\n"); 165 166 return (E_USAGE); 167 } 168 169 static void 170 verror(const char *fmt, va_list ap) 171 { 172 int error = errno; 173 174 (void) fprintf(stderr, "%s: ", g_pname); 175 (void) vfprintf(stderr, fmt, ap); 176 177 if (fmt[strlen(fmt) - 1] != '\n') 178 (void) fprintf(stderr, ": %s\n", strerror(error)); 179 } 180 181 /*PRINTFLIKE1*/ 182 static void 183 fatal(const char *fmt, ...) 184 { 185 va_list ap; 186 187 va_start(ap, fmt); 188 verror(fmt, ap); 189 va_end(ap); 190 191 exit(E_ERROR); 192 } 193 194 /*PRINTFLIKE1*/ 195 static void 196 dfatal(const char *fmt, ...) 197 { 198 va_list ap; 199 200 va_start(ap, fmt); 201 202 (void) fprintf(stderr, "%s: ", g_pname); 203 if (fmt != NULL) 204 (void) vfprintf(stderr, fmt, ap); 205 206 va_end(ap); 207 208 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') { 209 (void) fprintf(stderr, ": %s\n", 210 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 211 } else if (fmt == NULL) { 212 (void) fprintf(stderr, "%s\n", 213 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 214 } 215 216 /* 217 * Close the DTrace handle to ensure that any controlled processes are 218 * correctly restored and continued. 219 */ 220 dtrace_close(g_dtp); 221 222 exit(E_ERROR); 223 } 224 225 /*PRINTFLIKE1*/ 226 static void 227 error(const char *fmt, ...) 228 { 229 va_list ap; 230 231 va_start(ap, fmt); 232 verror(fmt, ap); 233 va_end(ap); 234 } 235 236 /*PRINTFLIKE1*/ 237 static void 238 notice(const char *fmt, ...) 239 { 240 va_list ap; 241 242 if (g_quiet) 243 return; /* -q or quiet pragma suppresses notice()s */ 244 245 va_start(ap, fmt); 246 verror(fmt, ap); 247 va_end(ap); 248 } 249 250 /*PRINTFLIKE1*/ 251 static void 252 oprintf(const char *fmt, ...) 253 { 254 va_list ap; 255 int n; 256 257 if (g_ofp == NULL) 258 return; 259 260 va_start(ap, fmt); 261 n = vfprintf(g_ofp, fmt, ap); 262 va_end(ap); 263 264 if (n < 0) { 265 if (errno != EINTR) { 266 fatal("failed to write to %s", 267 g_ofile ? g_ofile : "<stdout>"); 268 } 269 clearerr(g_ofp); 270 } 271 } 272 273 static char ** 274 make_argv(char *s) 275 { 276 const char *ws = "\f\n\r\t\v "; 277 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1)); 278 int argc = 0; 279 char *p = s; 280 281 if (argv == NULL) 282 return (NULL); 283 284 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws)) 285 argv[argc++] = p; 286 287 if (argc == 0) 288 argv[argc++] = s; 289 290 argv[argc] = NULL; 291 return (argv); 292 } 293 294 static void 295 dof_prune(const char *fname) 296 { 297 struct stat sbuf; 298 size_t sz, i, j, mark, len; 299 char *buf; 300 int msg = 0, fd; 301 302 if ((fd = open(fname, O_RDONLY)) == -1) { 303 /* 304 * This is okay only if the file doesn't exist at all. 305 */ 306 if (errno != ENOENT) 307 fatal("failed to open %s", fname); 308 return; 309 } 310 311 if (fstat(fd, &sbuf) == -1) 312 fatal("failed to fstat %s", fname); 313 314 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 315 fatal("failed to allocate memory for %s", fname); 316 317 if (read(fd, buf, sz) != sz) 318 fatal("failed to read %s", fname); 319 320 buf[sz] = '\0'; 321 (void) close(fd); 322 323 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1) 324 fatal("failed to open %s for writing", fname); 325 326 len = strlen("dof-data-"); 327 328 for (mark = 0, i = 0; i < sz; i++) { 329 if (strncmp(&buf[i], "dof-data-", len) != 0) 330 continue; 331 332 /* 333 * This is only a match if it's in the 0th column. 334 */ 335 if (i != 0 && buf[i - 1] != '\n') 336 continue; 337 338 if (msg++ == 0) { 339 error("cleaned up old anonymous " 340 "enabling in %s\n", fname); 341 } 342 343 /* 344 * We have a match. First write out our data up until now. 345 */ 346 if (i != mark) { 347 if (write(fd, &buf[mark], i - mark) != i - mark) 348 fatal("failed to write to %s", fname); 349 } 350 351 /* 352 * Now scan forward until we scan past a newline. 353 */ 354 for (j = i; j < sz && buf[j] != '\n'; j++) 355 continue; 356 357 /* 358 * Reset our mark. 359 */ 360 if ((mark = j + 1) >= sz) 361 break; 362 363 i = j; 364 } 365 366 if (mark < sz) { 367 if (write(fd, &buf[mark], sz - mark) != sz - mark) 368 fatal("failed to write to %s", fname); 369 } 370 371 (void) close(fd); 372 free(buf); 373 } 374 375 static void 376 etcsystem_prune(void) 377 { 378 struct stat sbuf; 379 size_t sz; 380 char *buf, *start, *end; 381 int fd; 382 char *fname = g_etcfile, *tmpname; 383 384 if ((fd = open(fname, O_RDONLY)) == -1) 385 fatal("failed to open %s", fname); 386 387 if (fstat(fd, &sbuf) == -1) 388 fatal("failed to fstat %s", fname); 389 390 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 391 fatal("failed to allocate memory for %s", fname); 392 393 if (read(fd, buf, sz) != sz) 394 fatal("failed to read %s", fname); 395 396 buf[sz] = '\0'; 397 (void) close(fd); 398 399 if ((start = strstr(buf, g_etcbegin)) == NULL) 400 goto out; 401 402 if (strlen(buf) != sz) { 403 fatal("embedded nul byte in %s; manual repair of %s " 404 "required\n", fname, fname); 405 } 406 407 if (strstr(start + 1, g_etcbegin) != NULL) { 408 fatal("multiple start sentinels in %s; manual repair of %s " 409 "required\n", fname, fname); 410 } 411 412 if ((end = strstr(buf, g_etcend)) == NULL) { 413 fatal("missing end sentinel in %s; manual repair of %s " 414 "required\n", fname, fname); 415 } 416 417 if (start > end) { 418 fatal("end sentinel preceeds start sentinel in %s; manual " 419 "repair of %s required\n", fname, fname); 420 } 421 422 end += strlen(g_etcend) + 1; 423 bcopy(end, start, strlen(end) + 1); 424 425 tmpname = alloca(sz = strlen(fname) + 80); 426 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid()); 427 428 if ((fd = open(tmpname, 429 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1) 430 fatal("failed to create %s", tmpname); 431 432 if (write(fd, buf, strlen(buf)) < strlen(buf)) { 433 (void) unlink(tmpname); 434 fatal("failed to write to %s", tmpname); 435 } 436 437 (void) close(fd); 438 439 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) { 440 (void) unlink(tmpname); 441 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname, 442 (int)sbuf.st_uid, (int)sbuf.st_gid); 443 } 444 445 if (rename(tmpname, fname) == -1) 446 fatal("rename of %s to %s failed", tmpname, fname); 447 448 error("cleaned up forceload directives in %s\n", fname); 449 out: 450 free(buf); 451 } 452 453 static void 454 etcsystem_add(void) 455 { 456 const char *mods[20]; 457 int nmods, line; 458 459 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL) 460 fatal("failed to open output file '%s'", g_ofile); 461 462 oprintf("%s\n", g_etcbegin); 463 464 for (line = 0; g_etc[line] != NULL; line++) 465 oprintf("%s\n", g_etc[line]); 466 467 nmods = dtrace_provider_modules(g_dtp, mods, 468 sizeof (mods) / sizeof (char *) - 1); 469 470 if (nmods >= sizeof (mods) / sizeof (char *)) 471 fatal("unexpectedly large number of modules!"); 472 473 mods[nmods++] = "dtrace"; 474 475 for (line = 0; line < nmods; line++) 476 oprintf("forceload: drv/%s\n", mods[line]); 477 478 oprintf("%s\n", g_etcend); 479 480 if (fclose(g_ofp) == EOF) 481 fatal("failed to close output file '%s'", g_ofile); 482 483 error("added forceload directives to %s\n", g_ofile); 484 } 485 486 static void 487 print_probe_info(const dtrace_probeinfo_t *p) 488 { 489 char buf[BUFSIZ]; 490 int i; 491 492 oprintf("\n\tProbe Description Attributes\n"); 493 494 oprintf("\t\tIdentifier Names: %s\n", 495 dtrace_stability_name(p->dtp_attr.dtat_name)); 496 oprintf("\t\tData Semantics: %s\n", 497 dtrace_stability_name(p->dtp_attr.dtat_data)); 498 oprintf("\t\tDependency Class: %s\n", 499 dtrace_class_name(p->dtp_attr.dtat_class)); 500 501 oprintf("\n\tArgument Attributes\n"); 502 503 oprintf("\t\tIdentifier Names: %s\n", 504 dtrace_stability_name(p->dtp_arga.dtat_name)); 505 oprintf("\t\tData Semantics: %s\n", 506 dtrace_stability_name(p->dtp_arga.dtat_data)); 507 oprintf("\t\tDependency Class: %s\n", 508 dtrace_class_name(p->dtp_arga.dtat_class)); 509 510 oprintf("\n\tArgument Types\n"); 511 512 for (i = 0; i < p->dtp_argc; i++) { 513 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp, 514 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL) 515 (void) strlcpy(buf, "(unknown)", sizeof (buf)); 516 oprintf("\t\targs[%d]: %s\n", i, buf); 517 } 518 519 if (p->dtp_argc == 0) 520 oprintf("\t\tNone\n"); 521 522 oprintf("\n"); 523 } 524 525 /*ARGSUSED*/ 526 static int 527 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 528 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 529 { 530 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 531 dtrace_probedesc_t *pdp = &edp->dted_probe; 532 dtrace_probeinfo_t p; 533 534 if (edp == *last) 535 return (0); 536 537 oprintf("\n%s:%s:%s:%s\n", 538 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 539 540 if (dtrace_probe_info(dtp, pdp, &p) == 0) 541 print_probe_info(&p); 542 543 *last = edp; 544 return (0); 545 } 546 547 /* 548 * Execute the specified program by enabling the corresponding instrumentation. 549 * If -e has been specified, we get the program info but do not enable it. If 550 * -v has been specified, we print a stability report for the program. 551 */ 552 static void 553 exec_prog(const dtrace_cmd_t *dcp) 554 { 555 dtrace_ecbdesc_t *last = NULL; 556 dtrace_proginfo_t dpi; 557 558 if (!g_exec) { 559 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi); 560 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) { 561 dfatal("failed to enable '%s'", dcp->dc_name); 562 } else { 563 notice("%s '%s' matched %u probe%s\n", 564 dcp->dc_desc, dcp->dc_name, 565 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s"); 566 } 567 568 if (g_verbose) { 569 oprintf("\nStability attributes for %s %s:\n", 570 dcp->dc_desc, dcp->dc_name); 571 572 oprintf("\n\tMinimum Probe Description Attributes\n"); 573 oprintf("\t\tIdentifier Names: %s\n", 574 dtrace_stability_name(dpi.dpi_descattr.dtat_name)); 575 oprintf("\t\tData Semantics: %s\n", 576 dtrace_stability_name(dpi.dpi_descattr.dtat_data)); 577 oprintf("\t\tDependency Class: %s\n", 578 dtrace_class_name(dpi.dpi_descattr.dtat_class)); 579 580 oprintf("\n\tMinimum Statement Attributes\n"); 581 582 oprintf("\t\tIdentifier Names: %s\n", 583 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name)); 584 oprintf("\t\tData Semantics: %s\n", 585 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data)); 586 oprintf("\t\tDependency Class: %s\n", 587 dtrace_class_name(dpi.dpi_stmtattr.dtat_class)); 588 589 if (!g_exec) { 590 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 591 (dtrace_stmt_f *)info_stmt, &last); 592 } else 593 oprintf("\n"); 594 } 595 596 g_total += dpi.dpi_matches; 597 } 598 599 /* 600 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for 601 * storing in a driver.conf(4) file associated with the dtrace driver. 602 */ 603 static void 604 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n) 605 { 606 const uchar_t *p, *q; 607 608 if (dof == NULL) 609 dfatal("failed to create DOF image for '%s'", dcp->dc_name); 610 611 p = (uchar_t *)dof; 612 q = p + dof->dofh_loadsz; 613 614 oprintf("dof-data-%d=0x%x", n, *p++); 615 616 while (p < q) 617 oprintf(",0x%x", *p++); 618 619 oprintf(";\n"); 620 dtrace_dof_destroy(g_dtp, dof); 621 } 622 623 /* 624 * Link the specified D program in DOF form into an ELF file for use in either 625 * helpers, userland provider definitions, or both. If -o was specified, that 626 * path is used as the output file name. If -o wasn't specified and the input 627 * program is from a script whose name is %.d, use basename(%.o) as the output 628 * file name. Otherwise we use "d.out" as the default output file name. 629 */ 630 static void 631 link_prog(dtrace_cmd_t *dcp) 632 { 633 char *p; 634 635 if (g_cmdc == 1 && g_ofile != NULL) { 636 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile)); 637 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL && 638 strcmp(p, ".d") == 0) { 639 p[0] = '\0'; /* strip .d suffix */ 640 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 641 "%s.o", basename(dcp->dc_arg)); 642 } else { 643 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 644 g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv)); 645 } 646 647 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES, 648 dcp->dc_ofile, g_objc, g_objv) != 0) 649 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name); 650 } 651 652 /*ARGSUSED*/ 653 static int 654 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg) 655 { 656 dtrace_probeinfo_t p; 657 658 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id, 659 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 660 661 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0) 662 print_probe_info(&p); 663 664 return (0); 665 } 666 667 /*ARGSUSED*/ 668 static int 669 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 670 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 671 { 672 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 673 674 if (edp == *last) 675 return (0); 676 677 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) { 678 error("failed to match %s:%s:%s:%s: %s\n", 679 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod, 680 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name, 681 dtrace_errmsg(dtp, dtrace_errno(dtp))); 682 } 683 684 *last = edp; 685 return (0); 686 } 687 688 /* 689 * List the probes corresponding to the specified program by iterating over 690 * each statement and then matching probes to the statement probe descriptions. 691 */ 692 static void 693 list_prog(const dtrace_cmd_t *dcp) 694 { 695 dtrace_ecbdesc_t *last = NULL; 696 697 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 698 (dtrace_stmt_f *)list_stmt, &last); 699 } 700 701 static void 702 compile_file(dtrace_cmd_t *dcp) 703 { 704 char *arg0; 705 FILE *fp; 706 707 if ((fp = fopen(dcp->dc_arg, "r")) == NULL) 708 fatal("failed to open %s", dcp->dc_arg); 709 710 arg0 = g_argv[0]; 711 g_argv[0] = dcp->dc_arg; 712 713 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp, 714 g_cflags, g_argc, g_argv)) == NULL) 715 dfatal("failed to compile script %s", dcp->dc_arg); 716 717 g_argv[0] = arg0; 718 (void) fclose(fp); 719 720 dcp->dc_desc = "script"; 721 dcp->dc_name = dcp->dc_arg; 722 } 723 724 static void 725 compile_str(dtrace_cmd_t *dcp) 726 { 727 char *p; 728 729 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg, 730 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL) 731 dfatal("invalid probe specifier %s", dcp->dc_arg); 732 733 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL) 734 *p = '\0'; /* crop name for reporting */ 735 736 dcp->dc_desc = "description"; 737 dcp->dc_name = dcp->dc_arg; 738 } 739 740 /*ARGSUSED*/ 741 static void 742 prochandler(struct ps_prochandle *P, const char *msg, void *arg) 743 { 744 const psinfo_t *prp = Ppsinfo(P); 745 int pid = Pstatus(P)->pr_pid; 746 char name[SIG2STR_MAX]; 747 748 if (msg != NULL) { 749 notice("pid %d: %s\n", pid, msg); 750 return; 751 } 752 753 switch (Pstate(P)) { 754 case PS_UNDEAD: 755 /* 756 * Ideally we would like to always report pr_wstat here, but it 757 * isn't possible given current /proc semantics. If we grabbed 758 * the process, Ppsinfo() will either fail or return a zeroed 759 * psinfo_t depending on how far the parent is in reaping it. 760 * When /proc provides a stable pr_wstat in the status file, 761 * this code can be improved by examining this new pr_wstat. 762 */ 763 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) { 764 notice("pid %d terminated by %s\n", pid, 765 proc_signame(WTERMSIG(prp->pr_wstat), 766 name, sizeof (name))); 767 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) { 768 notice("pid %d exited with status %d\n", 769 pid, WEXITSTATUS(prp->pr_wstat)); 770 } else { 771 notice("pid %d has exited\n", pid); 772 } 773 g_pslive--; 774 break; 775 776 case PS_LOST: 777 notice("pid %d exec'd a set-id or unobservable program\n", pid); 778 g_pslive--; 779 break; 780 } 781 } 782 783 /*ARGSUSED*/ 784 static int 785 errhandler(const dtrace_errdata_t *data, void *arg) 786 { 787 error(data->dteda_msg); 788 return (DTRACE_HANDLE_OK); 789 } 790 791 /*ARGSUSED*/ 792 static int 793 drophandler(const dtrace_dropdata_t *data, void *arg) 794 { 795 error(data->dtdda_msg); 796 return (DTRACE_HANDLE_OK); 797 } 798 799 /*ARGSUSED*/ 800 static int 801 setopthandler(const dtrace_setoptdata_t *data, void *arg) 802 { 803 if (strcmp(data->dtsda_option, "quiet") == 0) 804 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET; 805 806 if (strcmp(data->dtsda_option, "flowindent") == 0) 807 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET; 808 809 return (DTRACE_HANDLE_OK); 810 } 811 812 #define BUFDUMPHDR(hdr) \ 813 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : ""); 814 815 #define BUFDUMPSTR(ptr, field) \ 816 (void) printf("%s: %20s => ", g_pname, #field); \ 817 if ((ptr)->field != NULL) { \ 818 const char *c = (ptr)->field; \ 819 (void) printf("\""); \ 820 do { \ 821 if (*c == '\n') { \ 822 (void) printf("\\n"); \ 823 continue; \ 824 } \ 825 \ 826 (void) printf("%c", *c); \ 827 } while (*c++ != '\0'); \ 828 (void) printf("\"\n"); \ 829 } else { \ 830 (void) printf("<NULL>\n"); \ 831 } 832 833 #define BUFDUMPASSTR(ptr, field, str) \ 834 (void) printf("%s: %20s => %s\n", g_pname, #field, str); 835 836 #define BUFDUMP(ptr, field) \ 837 (void) printf("%s: %20s => %lld\n", g_pname, #field, \ 838 (long long)(ptr)->field); 839 840 #define BUFDUMPPTR(ptr, field) \ 841 (void) printf("%s: %20s => %s\n", g_pname, #field, \ 842 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>"); 843 844 /*ARGSUSED*/ 845 static int 846 bufhandler(const dtrace_bufdata_t *bufdata, void *arg) 847 { 848 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata; 849 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc; 850 const dtrace_probedesc_t *pd; 851 uint32_t flags = bufdata->dtbda_flags; 852 char buf[512], *c = buf, *end = c + sizeof (buf); 853 int i, printed; 854 855 struct { 856 const char *name; 857 uint32_t value; 858 } flagnames[] = { 859 { "AGGVAL", DTRACE_BUFDATA_AGGVAL }, 860 { "AGGKEY", DTRACE_BUFDATA_AGGKEY }, 861 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT }, 862 { "AGGLAST", DTRACE_BUFDATA_AGGLAST }, 863 { "???", UINT32_MAX }, 864 { NULL } 865 }; 866 867 if (bufdata->dtbda_probe != NULL) { 868 pd = bufdata->dtbda_probe->dtpda_pdesc; 869 } else if (agg != NULL) { 870 pd = agg->dtada_pdesc; 871 } else { 872 pd = NULL; 873 } 874 875 BUFDUMPHDR(">>> Called buffer handler"); 876 BUFDUMPHDR(""); 877 878 BUFDUMPHDR(" dtrace_bufdata"); 879 BUFDUMPSTR(bufdata, dtbda_buffered); 880 BUFDUMPPTR(bufdata, dtbda_probe); 881 BUFDUMPPTR(bufdata, dtbda_aggdata); 882 BUFDUMPPTR(bufdata, dtbda_recdesc); 883 884 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags); 885 c += strlen(c); 886 887 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) { 888 if (!(flags & flagnames[i].value)) 889 continue; 890 891 (void) snprintf(c, end - c, 892 "%s%s", printed++ ? " | " : "(", flagnames[i].name); 893 c += strlen(c); 894 flags &= ~flagnames[i].value; 895 } 896 897 if (printed) 898 (void) snprintf(c, end - c, ")"); 899 900 BUFDUMPASSTR(bufdata, dtbda_flags, buf); 901 BUFDUMPHDR(""); 902 903 if (pd != NULL) { 904 BUFDUMPHDR(" dtrace_probedesc"); 905 BUFDUMPSTR(pd, dtpd_provider); 906 BUFDUMPSTR(pd, dtpd_mod); 907 BUFDUMPSTR(pd, dtpd_func); 908 BUFDUMPSTR(pd, dtpd_name); 909 BUFDUMPHDR(""); 910 } 911 912 if (rec != NULL) { 913 BUFDUMPHDR(" dtrace_recdesc"); 914 BUFDUMP(rec, dtrd_action); 915 BUFDUMP(rec, dtrd_size); 916 917 if (agg != NULL) { 918 uint8_t *data; 919 int lim = rec->dtrd_size; 920 921 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset); 922 c = buf + strlen(buf); 923 924 if (lim > sizeof (uint64_t)) 925 lim = sizeof (uint64_t); 926 927 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset; 928 929 for (i = 0; i < lim; i++) { 930 (void) snprintf(c, end - c, "%s%02x", 931 i == 0 ? "" : " ", *data++); 932 c += strlen(c); 933 } 934 935 (void) snprintf(c, end - c, 936 "%s)", lim < rec->dtrd_size ? " ..." : ""); 937 BUFDUMPASSTR(rec, dtrd_offset, buf); 938 } else { 939 BUFDUMP(rec, dtrd_offset); 940 } 941 942 BUFDUMPHDR(""); 943 } 944 945 if (agg != NULL) { 946 dtrace_aggdesc_t *desc = agg->dtada_desc; 947 948 BUFDUMPHDR(" dtrace_aggdesc"); 949 BUFDUMPSTR(desc, dtagd_name); 950 BUFDUMP(desc, dtagd_varid); 951 BUFDUMP(desc, dtagd_id); 952 BUFDUMP(desc, dtagd_nrecs); 953 BUFDUMPHDR(""); 954 } 955 956 return (DTRACE_HANDLE_OK); 957 } 958 959 /*ARGSUSED*/ 960 static int 961 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg) 962 { 963 dtrace_actkind_t act; 964 uintptr_t addr; 965 966 if (rec == NULL) { 967 /* 968 * We have processed the final record; output the newline if 969 * we're not in quiet mode. 970 */ 971 if (!g_quiet) 972 oprintf("\n"); 973 974 return (DTRACE_CONSUME_NEXT); 975 } 976 977 act = rec->dtrd_action; 978 addr = (uintptr_t)data->dtpda_data; 979 980 if (act == DTRACEACT_EXIT) { 981 g_status = *((uint32_t *)addr); 982 return (DTRACE_CONSUME_NEXT); 983 } 984 985 return (DTRACE_CONSUME_THIS); 986 } 987 988 /*ARGSUSED*/ 989 static int 990 chew(const dtrace_probedata_t *data, void *arg) 991 { 992 dtrace_probedesc_t *pd = data->dtpda_pdesc; 993 processorid_t cpu = data->dtpda_cpu; 994 static int heading; 995 996 if (g_impatient) { 997 g_newline = 0; 998 return (DTRACE_CONSUME_ABORT); 999 } 1000 1001 if (heading == 0) { 1002 if (!g_flowindent) { 1003 if (!g_quiet) { 1004 oprintf("%3s %6s %32s\n", 1005 "CPU", "ID", "FUNCTION:NAME"); 1006 } 1007 } else { 1008 oprintf("%3s %-41s\n", "CPU", "FUNCTION"); 1009 } 1010 heading = 1; 1011 } 1012 1013 if (!g_flowindent) { 1014 if (!g_quiet) { 1015 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2]; 1016 1017 (void) snprintf(name, sizeof (name), "%s:%s", 1018 pd->dtpd_func, pd->dtpd_name); 1019 1020 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name); 1021 } 1022 } else { 1023 int indent = data->dtpda_indent; 1024 char *name; 1025 size_t len; 1026 1027 if (data->dtpda_flow == DTRACEFLOW_NONE) { 1028 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5; 1029 name = alloca(len); 1030 (void) snprintf(name, len, "%*s%s%s:%s", indent, "", 1031 data->dtpda_prefix, pd->dtpd_func, 1032 pd->dtpd_name); 1033 } else { 1034 len = indent + DTRACE_FUNCNAMELEN + 5; 1035 name = alloca(len); 1036 (void) snprintf(name, len, "%*s%s%s", indent, "", 1037 data->dtpda_prefix, pd->dtpd_func); 1038 } 1039 1040 oprintf("%3d %-41s ", cpu, name); 1041 } 1042 1043 return (DTRACE_CONSUME_THIS); 1044 } 1045 1046 static void 1047 go(void) 1048 { 1049 int i; 1050 1051 struct { 1052 char *