17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/stat.h> 317c478bd9Sstevel@tonic-gate #include <sys/wait.h> 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <dtrace.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <stdarg.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <strings.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <limits.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate #include <signal.h> 437c478bd9Sstevel@tonic-gate #include <alloca.h> 447c478bd9Sstevel@tonic-gate #include <libgen.h> 457c478bd9Sstevel@tonic-gate #include <libproc.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate typedef struct dtrace_cmd { 487c478bd9Sstevel@tonic-gate void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */ 497c478bd9Sstevel@tonic-gate dtrace_probespec_t dc_spec; /* probe specifier context */ 507c478bd9Sstevel@tonic-gate char *dc_arg; /* argument from main argv */ 517c478bd9Sstevel@tonic-gate const char *dc_name; /* name for error messages */ 527c478bd9Sstevel@tonic-gate const char *dc_desc; /* desc for error messages */ 537c478bd9Sstevel@tonic-gate dtrace_prog_t *dc_prog; /* program compiled from arg */ 54*0b38a8bdSahl char dc_ofile[PATH_MAX]; /* derived output file name */ 557c478bd9Sstevel@tonic-gate } dtrace_cmd_t; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define DMODE_VERS 0 /* display version information and exit (-V) */ 587c478bd9Sstevel@tonic-gate #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */ 597c478bd9Sstevel@tonic-gate #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */ 607c478bd9Sstevel@tonic-gate #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */ 617c478bd9Sstevel@tonic-gate #define DMODE_LIST 4 /* compile program and list probes (-l) */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #define E_SUCCESS 0 647c478bd9Sstevel@tonic-gate #define E_ERROR 1 657c478bd9Sstevel@tonic-gate #define E_USAGE 2 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate static const char DTRACE_OPTSTR[] = 687c478bd9Sstevel@tonic-gate "3:6:aAb:c:CD:ef:FGHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z"; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static char **g_argv; 717c478bd9Sstevel@tonic-gate static int g_argc; 727c478bd9Sstevel@tonic-gate static char **g_objv; 737c478bd9Sstevel@tonic-gate static int g_objc; 747c478bd9Sstevel@tonic-gate static dtrace_cmd_t *g_cmdv; 757c478bd9Sstevel@tonic-gate static int g_cmdc; 767c478bd9Sstevel@tonic-gate static struct ps_prochandle **g_psv; 777c478bd9Sstevel@tonic-gate static int g_psc; 787c478bd9Sstevel@tonic-gate static int g_pslive; 797c478bd9Sstevel@tonic-gate static char *g_pname; 807c478bd9Sstevel@tonic-gate static int g_quiet; 817c478bd9Sstevel@tonic-gate static int g_flowindent; 827c478bd9Sstevel@tonic-gate static int g_intr; 837c478bd9Sstevel@tonic-gate static int g_impatient; 847c478bd9Sstevel@tonic-gate static int g_newline; 857c478bd9Sstevel@tonic-gate static int g_total; 867c478bd9Sstevel@tonic-gate static int g_cflags; 877c478bd9Sstevel@tonic-gate static int g_oflags; 887c478bd9Sstevel@tonic-gate static int g_verbose; 897c478bd9Sstevel@tonic-gate static int g_exec = 1; 907c478bd9Sstevel@tonic-gate static int g_mode = DMODE_EXEC; 917c478bd9Sstevel@tonic-gate static int g_status = E_SUCCESS; 927c478bd9Sstevel@tonic-gate static int g_grabanon = 0; 937c478bd9Sstevel@tonic-gate static const char *g_ofile = NULL; 947c478bd9Sstevel@tonic-gate static FILE *g_ofp = stdout; 957c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp; 967c478bd9Sstevel@tonic-gate static char *g_etcfile = "/etc/system"; 977c478bd9Sstevel@tonic-gate static const char *g_etcbegin = "* vvvv Added by DTrace"; 987c478bd9Sstevel@tonic-gate static const char *g_etcend = "* ^^^^ Added by DTrace"; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate static const char *g_etc[] = { 1017c478bd9Sstevel@tonic-gate "*", 1027c478bd9Sstevel@tonic-gate "* The following forceload directives were added by dtrace(1M) to allow for", 1037c478bd9Sstevel@tonic-gate "* tracing during boot. If these directives are removed, the system will", 1047c478bd9Sstevel@tonic-gate "* continue to function, but tracing will not occur during boot as desired.", 1057c478bd9Sstevel@tonic-gate "* To remove these directives (and this block comment) automatically, run", 1067c478bd9Sstevel@tonic-gate "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"", 1077c478bd9Sstevel@tonic-gate "* chapter of the Solaris Dynamic Tracing Guide for details.", 1087c478bd9Sstevel@tonic-gate "*", 1097c478bd9Sstevel@tonic-gate NULL }; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static int 1127c478bd9Sstevel@tonic-gate usage(FILE *fp) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate static const char predact[] = "[[ predicate ] action ]"; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGHlqSvVwZ] " 1177c478bd9Sstevel@tonic-gate "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] " 1187c478bd9Sstevel@tonic-gate "[-o output] [-p pid] [-s script] [-U name]\n\t" 1197c478bd9Sstevel@tonic-gate "[-x opt[=val]] [-X a|c|s|t]\n\n" 1207c478bd9Sstevel@tonic-gate "\t[-P provider %s]\n" 1217c478bd9Sstevel@tonic-gate "\t[-m [ provider: ] module %s]\n" 1227c478bd9Sstevel@tonic-gate "\t[-f [[ provider: ] module: ] func %s]\n" 1237c478bd9Sstevel@tonic-gate "\t[-n [[[ provider: ] module: ] func: ] name %s]\n" 1247c478bd9Sstevel@tonic-gate "\t[-i probe-id %s] [ args ... ]\n\n", g_pname, 1257c478bd9Sstevel@tonic-gate predact, predact, predact, predact, predact); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n"); 1287c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\t action -> '{' D-statements '}'\n"); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n" 1317c478bd9Sstevel@tonic-gate "\t-32 generate 32-bit D programs and ELF files\n" 1327c478bd9Sstevel@tonic-gate "\t-64 generate 64-bit D programs and ELF files\n\n" 1337c478bd9Sstevel@tonic-gate "\t-a claim anonymous tracing state\n" 1347c478bd9Sstevel@tonic-gate "\t-A generate driver.conf(4) directives for anonymous tracing\n" 1357c478bd9Sstevel@tonic-gate "\t-b set trace buffer size\n" 1367c478bd9Sstevel@tonic-gate "\t-c run specified command and exit upon its completion\n" 1377c478bd9Sstevel@tonic-gate "\t-C run cpp(1) preprocessor on script files\n" 1387c478bd9Sstevel@tonic-gate "\t-D define symbol when invoking preprocessor\n" 1397c478bd9Sstevel@tonic-gate "\t-e exit after compiling request but prior to enabling probes\n" 1407c478bd9Sstevel@tonic-gate "\t-f enable or list probes matching the specified function name\n" 1417c478bd9Sstevel@tonic-gate "\t-F coalesce trace output by function\n" 1427c478bd9Sstevel@tonic-gate "\t-G generate an ELF file containing embedded dtrace program\n" 1437c478bd9Sstevel@tonic-gate "\t-H print included files when invoking preprocessor\n" 1447c478bd9Sstevel@tonic-gate "\t-i enable or list probes matching the specified probe id\n" 1457c478bd9Sstevel@tonic-gate "\t-I add include directory to preprocessor search path\n" 1467c478bd9Sstevel@tonic-gate "\t-l list probes matching specified criteria\n" 1477c478bd9Sstevel@tonic-gate "\t-L add library directory to library search path\n" 1487c478bd9Sstevel@tonic-gate "\t-m enable or list probes matching the specified module name\n" 1497c478bd9Sstevel@tonic-gate "\t-n enable or list probes matching the specified probe name\n" 1507c478bd9Sstevel@tonic-gate "\t-o set output file\n" 1517c478bd9Sstevel@tonic-gate "\t-p grab specified process-ID and cache its symbol tables\n" 1527c478bd9Sstevel@tonic-gate "\t-P enable or list probes matching the specified provider name\n" 1537c478bd9Sstevel@tonic-gate "\t-q set quiet mode (only output explicitly traced data)\n" 1547c478bd9Sstevel@tonic-gate "\t-s enable or list probes according to the specified D script\n" 1557c478bd9Sstevel@tonic-gate "\t-S print D compiler intermediate code\n" 1567c478bd9Sstevel@tonic-gate "\t-U undefine symbol when invoking preprocessor\n" 1577c478bd9Sstevel@tonic-gate "\t-v set verbose mode (report stability attributes, arguments)\n" 1587c478bd9Sstevel@tonic-gate "\t-V report DTrace API version\n" 1597c478bd9Sstevel@tonic-gate "\t-w permit destructive actions\n" 1607c478bd9Sstevel@tonic-gate "\t-x enable or modify compiler and tracing options\n" 1617c478bd9Sstevel@tonic-gate "\t-X specify ISO C conformance settings for preprocessor\n" 1627c478bd9Sstevel@tonic-gate "\t-Z permit probe descriptions that match zero probes\n"); 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate return (E_USAGE); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate static void 1687c478bd9Sstevel@tonic-gate verror(const char *fmt, va_list ap) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate int error = errno; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 1737c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n') 1767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(error)); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1807c478bd9Sstevel@tonic-gate static void 1817c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate va_list ap; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate va_start(ap, fmt); 1867c478bd9Sstevel@tonic-gate verror(fmt, ap); 1877c478bd9Sstevel@tonic-gate va_end(ap); 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate exit(E_ERROR); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1937c478bd9Sstevel@tonic-gate static void 1947c478bd9Sstevel@tonic-gate dfatal(const char *fmt, ...) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate va_list ap; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate va_start(ap, fmt); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 2017c478bd9Sstevel@tonic-gate if (fmt != NULL) 2027c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate va_end(ap); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') { 2077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", 2087c478bd9Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 2097c478bd9Sstevel@tonic-gate } else if (fmt == NULL) { 2107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", 2117c478bd9Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate exit(E_ERROR); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 2187c478bd9Sstevel@tonic-gate static void 2197c478bd9Sstevel@tonic-gate error(const char *fmt, ...) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate va_list ap; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate va_start(ap, fmt); 2247c478bd9Sstevel@tonic-gate verror(fmt, ap); 2257c478bd9Sstevel@tonic-gate va_end(ap); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 2297c478bd9Sstevel@tonic-gate static void 2307c478bd9Sstevel@tonic-gate notice(const char *fmt, ...) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate va_list ap; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (g_quiet) 2357c478bd9Sstevel@tonic-gate return; /* -q or quiet pragma suppresses notice()s */ 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate va_start(ap, fmt); 2387c478bd9Sstevel@tonic-gate verror(fmt, ap); 2397c478bd9Sstevel@tonic-gate va_end(ap); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 2437c478bd9Sstevel@tonic-gate static void 2447c478bd9Sstevel@tonic-gate oprintf(const char *fmt, ...) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate va_list ap; 2477c478bd9Sstevel@tonic-gate int n; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate va_start(ap, fmt); 2507c478bd9Sstevel@tonic-gate n = vfprintf(g_ofp, fmt, ap); 2517c478bd9Sstevel@tonic-gate va_end(ap); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate if (n < 0) { 2547c478bd9Sstevel@tonic-gate if (errno != EINTR) { 2557c478bd9Sstevel@tonic-gate fatal("failed to write to %s", 2567c478bd9Sstevel@tonic-gate g_ofile ? g_ofile : "<stdout>"); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate clearerr(g_ofp); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate static char ** 2637c478bd9Sstevel@tonic-gate make_argv(char *s) 2647c478bd9Sstevel@tonic-gate { 2657c478bd9Sstevel@tonic-gate const char *ws = "\f\n\r\t\v "; 2667c478bd9Sstevel@tonic-gate char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1)); 2677c478bd9Sstevel@tonic-gate int argc = 0; 2687c478bd9Sstevel@tonic-gate char *p = s; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (argv == NULL) 2717c478bd9Sstevel@tonic-gate return (NULL); 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws)) 2747c478bd9Sstevel@tonic-gate argv[argc++] = p; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate if (argc == 0) 2777c478bd9Sstevel@tonic-gate argv[argc++] = s; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate argv[argc] = NULL; 2807c478bd9Sstevel@tonic-gate return (argv); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate static void 2847c478bd9Sstevel@tonic-gate dof_prune(const char *fname) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate struct stat sbuf; 2877c478bd9Sstevel@tonic-gate size_t sz, i, j, mark, len; 2887c478bd9Sstevel@tonic-gate char *buf; 2897c478bd9Sstevel@tonic-gate int msg = 0, fd; 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) == -1) { 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * This is okay only if the file doesn't exist at all. 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate if (errno != ENOENT) 2967c478bd9Sstevel@tonic-gate fatal("failed to open %s", fname); 2977c478bd9Sstevel@tonic-gate return; 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (fstat(fd, &sbuf) == -1) 3017c478bd9Sstevel@tonic-gate fatal("failed to fstat %s", fname); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 3047c478bd9Sstevel@tonic-gate fatal("failed to allocate memory for %s", fname); 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (read(fd, buf, sz) != sz) 3077c478bd9Sstevel@tonic-gate fatal("failed to read %s", fname); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate buf[sz] = '\0'; 3107c478bd9Sstevel@tonic-gate (void) close(fd); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1) 3137c478bd9Sstevel@tonic-gate fatal("failed to open %s for writing", fname); 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate len = strlen("dof-data-"); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate for (mark = 0, i = 0; i < sz; i++) { 3187c478bd9Sstevel@tonic-gate if (strncmp(&buf[i], "dof-data-", len) != 0) 3197c478bd9Sstevel@tonic-gate continue; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * This is only a match if it's in the 0th column. 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate if (i != 0 && buf[i - 1] != '\n') 3257c478bd9Sstevel@tonic-gate continue; 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (msg++ == 0) { 3287c478bd9Sstevel@tonic-gate error("cleaned up old anonymous " 3297c478bd9Sstevel@tonic-gate "enabling in %s\n", fname); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * We have a match. First write out our data up until now. 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate if (i != mark) { 3367c478bd9Sstevel@tonic-gate if (write(fd, &buf[mark], i - mark) != i - mark) 3377c478bd9Sstevel@tonic-gate fatal("failed to write to %s", fname); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * Now scan forward until we scan past a newline. 3427c478bd9Sstevel@tonic-gate */ 3437c478bd9Sstevel@tonic-gate for (j = i; j < sz && buf[j] != '\n'; j++) 3447c478bd9Sstevel@tonic-gate continue; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate /* 3477c478bd9Sstevel@tonic-gate * Reset our mark. 3487c478bd9Sstevel@tonic-gate */ 3497c478bd9Sstevel@tonic-gate if ((mark = j + 1) >= sz) 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate i = j; 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate if (mark < sz) { 3567c478bd9Sstevel@tonic-gate if (write(fd, &buf[mark], sz - mark) != sz - mark) 3577c478bd9Sstevel@tonic-gate fatal("failed to write to %s", fname); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate (void) close(fd); 3617c478bd9Sstevel@tonic-gate free(buf); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate static void 3657c478bd9Sstevel@tonic-gate etcsystem_prune(void) 3667c478bd9Sstevel@tonic-gate { 3677c478bd9Sstevel@tonic-gate struct stat sbuf; 3687c478bd9Sstevel@tonic-gate size_t sz; 3697c478bd9Sstevel@tonic-gate char *buf, *start, *end; 3707c478bd9Sstevel@tonic-gate int fd; 3717c478bd9Sstevel@tonic-gate char *fname = g_etcfile, *tmpname; 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) == -1) 3747c478bd9Sstevel@tonic-gate fatal("failed to open %s", fname); 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (fstat(fd, &sbuf) == -1) 3777c478bd9Sstevel@tonic-gate fatal("failed to fstat %s", fname); 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 3807c478bd9Sstevel@tonic-gate fatal("failed to allocate memory for %s", fname); 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate if (read(fd, buf, sz) != sz) 3837c478bd9Sstevel@tonic-gate fatal("failed to read %s", fname); 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate buf[sz] = '\0'; 3867c478bd9Sstevel@tonic-gate (void) close(fd); 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate if ((start = strstr(buf, g_etcbegin)) == NULL) 3897c478bd9Sstevel@tonic-gate goto out; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate if (strlen(buf) != sz) { 3927c478bd9Sstevel@tonic-gate fatal("embedded nul byte in %s; manual repair of %s " 3937c478bd9Sstevel@tonic-gate "required\n", fname, fname); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate if (strstr(start + 1, g_etcbegin) != NULL) { 3977c478bd9Sstevel@tonic-gate fatal("multiple start sentinels in %s; manual repair of %s " 3987c478bd9Sstevel@tonic-gate "required\n", fname, fname); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if ((end = strstr(buf, g_etcend)) == NULL) { 4027c478bd9Sstevel@tonic-gate fatal("missing end sentinel in %s; manual repair of %s " 4037c478bd9Sstevel@tonic-gate "required\n", fname, fname); 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if (start > end) { 4077c478bd9Sstevel@tonic-gate fatal("end sentinel preceeds start sentinel in %s; manual " 4087c478bd9Sstevel@tonic-gate "repair of %s required\n", fname, fname); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate end += strlen(g_etcend) + 1; 4127c478bd9Sstevel@tonic-gate bcopy(end, start, strlen(end) + 1); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate tmpname = alloca(sz = strlen(fname) + 80); 4157c478bd9Sstevel@tonic-gate (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid()); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if ((fd = open(tmpname, 4187c478bd9Sstevel@tonic-gate O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1) 4197c478bd9Sstevel@tonic-gate fatal("failed to create %s", tmpname); 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate if (write(fd, buf, strlen(buf)) < strlen(buf)) { 4227c478bd9Sstevel@tonic-gate (void) unlink(tmpname); 4237c478bd9Sstevel@tonic-gate fatal("failed to write to %s", tmpname); 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate (void) close(fd); 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) { 4297c478bd9Sstevel@tonic-gate (void) unlink(tmpname); 4307c478bd9Sstevel@tonic-gate fatal("failed to chown(2) %s to uid %d, gid %d", tmpname, 4317c478bd9Sstevel@tonic-gate (int)sbuf.st_uid, (int)sbuf.st_gid); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate if (rename(tmpname, fname) == -1) 4357c478bd9Sstevel@tonic-gate fatal("rename of %s to %s failed", tmpname, fname); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate error("cleaned up forceload directives in %s\n", fname); 4387c478bd9Sstevel@tonic-gate out: 4397c478bd9Sstevel@tonic-gate free(buf); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate static void 4437c478bd9Sstevel@tonic-gate etcsystem_add(void) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate const char *mods[20]; 4467c478bd9Sstevel@tonic-gate int nmods, line; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL) 4497c478bd9Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate oprintf("%s\n", g_etcbegin); 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate for (line = 0; g_etc[line] != NULL; line++) 4547c478bd9Sstevel@tonic-gate oprintf("%s\n", g_etc[line]); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate nmods = dtrace_provider_modules(g_dtp, mods, 4577c478bd9Sstevel@tonic-gate sizeof (mods) / sizeof (char *) - 1); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (nmods >= sizeof (mods) / sizeof (char *)) 4607c478bd9Sstevel@tonic-gate fatal("unexpectedly large number of modules!"); 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate mods[nmods++] = "dtrace"; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate for (line = 0; line < nmods; line++) 4657c478bd9Sstevel@tonic-gate oprintf("forceload: drv/%s\n", mods[line]); 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate oprintf("%s\n", g_etcend); 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate if (fclose(g_ofp) == EOF) 4707c478bd9Sstevel@tonic-gate fatal("failed to close output file '%s'", g_ofile); 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate error("added forceload directives to %s\n", g_ofile); 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate static void 4767c478bd9Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 4797c478bd9Sstevel@tonic-gate int i; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate oprintf("\n\tProbe Description Attributes\n"); 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 4847c478bd9Sstevel@tonic-gate dtrace_stability_name(p->dtp_attr.dtat_name)); 4857c478bd9Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 4867c478bd9Sstevel@tonic-gate dtrace_stability_name(p->dtp_attr.dtat_data)); 4877c478bd9Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 4887c478bd9Sstevel@tonic-gate dtrace_class_name(p->dtp_attr.dtat_class)); 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate oprintf("\n\tArgument Attributes\n"); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 4937c478bd9Sstevel@tonic-gate dtrace_stability_name(p->dtp_arga.dtat_name)); 4947c478bd9Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 4957c478bd9Sstevel@tonic-gate dtrace_stability_name(p->dtp_arga.dtat_data)); 4967c478bd9Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 4977c478bd9Sstevel@tonic-gate dtrace_class_name(p->dtp_arga.dtat_class)); 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate oprintf("\n\tArgument Types\n"); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate for (i = 0; i < p->dtp_argc; i++) { 5027c478bd9Sstevel@tonic-gate if (ctf_type_name(p->dtp_argv[i].dtt_ctfp, 5037c478bd9Sstevel@tonic-gate p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL) 5047c478bd9Sstevel@tonic-gate (void) strlcpy(buf, "(unknown)", sizeof (buf)); 5057c478bd9Sstevel@tonic-gate oprintf("\t\targs[%d]: %s\n", i, buf); 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate if (p->dtp_argc == 0) 5097c478bd9Sstevel@tonic-gate oprintf("\t\tNone\n"); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate oprintf("\n"); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5157c478bd9Sstevel@tonic-gate static int 5167c478bd9Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 5177c478bd9Sstevel@tonic-gate dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 5187c478bd9Sstevel@tonic-gate { 5197c478bd9Sstevel@tonic-gate dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 5207c478bd9Sstevel@tonic-gate dtrace_probedesc_t *pdp = &edp->dted_probe; 5217c478bd9Sstevel@tonic-gate dtrace_probeinfo_t p; 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate if (edp == *last) 5247c478bd9Sstevel@tonic-gate return (0); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate oprintf("\n%s:%s:%s:%s\n", 5277c478bd9Sstevel@tonic-gate pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate if (dtrace_probe_info(dtp, pdp, &p) == 0) 5307c478bd9Sstevel@tonic-gate print_probe_info(&p); 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate *last = edp; 5337c478bd9Sstevel@tonic-gate return (0); 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate /* 5377c478bd9Sstevel@tonic-gate * Execute the specified program by enabling the corresponding instrumentation. 5387c478bd9Sstevel@tonic-gate * If -e has been specified, we get the program info but do not enable it. If 5397c478bd9Sstevel@tonic-gate * -v has been specified, we print a stability report for the program. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate static void 5427c478bd9Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp) 5437c478bd9Sstevel@tonic-gate { 5447c478bd9Sstevel@tonic-gate dtrace_ecbdesc_t *last = NULL; 5457c478bd9Sstevel@tonic-gate dtrace_proginfo_t dpi; 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate if (!g_exec) { 5487c478bd9Sstevel@tonic-gate dtrace_program_info(g_dtp, dcp->dc_prog, &dpi); 5497c478bd9Sstevel@tonic-gate } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) { 5507c478bd9Sstevel@tonic-gate dfatal("failed to enable '%s'", dcp->dc_name); 5517c478bd9Sstevel@tonic-gate } else { 5527c478bd9Sstevel@tonic-gate notice("%s '%s' matched %u probe%s\n", 5537c478bd9Sstevel@tonic-gate dcp->dc_desc, dcp->dc_name, 5547c478bd9Sstevel@tonic-gate dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s"); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (g_verbose) { 5587c478bd9Sstevel@tonic-gate oprintf("\nStability attributes for %s %s:\n", 5597c478bd9Sstevel@tonic-gate dcp->dc_desc, dcp->dc_name); 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate oprintf("\n\tMinimum Probe Description Attributes\n"); 5627c478bd9Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 5637c478bd9Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_descattr.dtat_name)); 5647c478bd9Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 5657c478bd9Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_descattr.dtat_data)); 5667c478bd9Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 5677c478bd9Sstevel@tonic-gate dtrace_class_name(dpi.dpi_descattr.dtat_class)); 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate oprintf("\n\tMinimum Statement Attributes\n"); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 5727c478bd9Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_stmtattr.dtat_name)); 5737c478bd9Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 5747c478bd9Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_stmtattr.dtat_data)); 5757c478bd9Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 5767c478bd9Sstevel@tonic-gate dtrace_class_name(dpi.dpi_stmtattr.dtat_class)); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if (!g_exec) { 5797c478bd9Sstevel@tonic-gate (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 5807c478bd9Sstevel@tonic-gate (dtrace_stmt_f *)info_stmt, &last); 5817c478bd9Sstevel@tonic-gate } else 5827c478bd9Sstevel@tonic-gate oprintf("\n"); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate g_total += dpi.dpi_matches; 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate /* 5897c478bd9Sstevel@tonic-gate * Print out the specified DOF buffer as a set of ASCII bytes appropriate for 5907c478bd9Sstevel@tonic-gate * storing in a driver.conf(4) file associated with the dtrace driver. 5917c478bd9Sstevel@tonic-gate */ 5927c478bd9Sstevel@tonic-gate static void 5937c478bd9Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n) 5947c478bd9Sstevel@tonic-gate { 5957c478bd9Sstevel@tonic-gate const uchar_t *p, *q; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (dof == NULL) 5987c478bd9Sstevel@tonic-gate dfatal("failed to create DOF image for '%s'", dcp->dc_name); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate p = (uchar_t *)dof; 6017c478bd9Sstevel@tonic-gate q = p + dof->dofh_loadsz; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate oprintf("dof-data-%d=0x%x", n, *p++); 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate while (p < q) 6067c478bd9Sstevel@tonic-gate oprintf(",0x%x", *p++); 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate oprintf(";\n"); 6097c478bd9Sstevel@tonic-gate dtrace_dof_destroy(g_dtp, dof); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate /* 6137c478bd9Sstevel@tonic-gate * Link the specified D program in DOF form into an ELF file for use in either 6147c478bd9Sstevel@tonic-gate * helpers, userland provider definitions, or both. If -o was specified, that 6157c478bd9Sstevel@tonic-gate * path is used as the output file name. If -o wasn't specified and the input 6167c478bd9Sstevel@tonic-gate * program is from a script whose name is %.d, use basename(%.o) as the output 6177c478bd9Sstevel@tonic-gate * file name. Otherwise we use "d.out" as the default output file name. 6187c478bd9Sstevel@tonic-gate */ 6197c478bd9Sstevel@tonic-gate static void 620*0b38a8bdSahl link_prog(dtrace_cmd_t *dcp) 6217c478bd9Sstevel@tonic-gate { 622*0b38a8bdSahl char *p; 6237c478bd9Sstevel@tonic-gate 624*0b38a8bdSahl if (g_cmdc == 1 && g_ofile != NULL) { 625*0b38a8bdSahl (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile)); 6267c478bd9Sstevel@tonic-gate } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL && 6277c478bd9Sstevel@tonic-gate strcmp(p, ".d") == 0) { 6287c478bd9Sstevel@tonic-gate p[0] = '\0'; /* strip .d suffix */ 629*0b38a8bdSahl (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 6307c478bd9Sstevel@tonic-gate "%s.o", basename(dcp->dc_arg)); 6317c478bd9Sstevel@tonic-gate } else { 632*0b38a8bdSahl (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 633*0b38a8bdSahl g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv)); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES, 637*0b38a8bdSahl dcp->dc_ofile, g_objc - 1, g_objv + 1) != 0) 6387c478bd9Sstevel@tonic-gate dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6427c478bd9Sstevel@tonic-gate static int 6437c478bd9Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg) 6447c478bd9Sstevel@tonic-gate { 6457c478bd9Sstevel@tonic-gate dtrace_probeinfo_t p; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id, 6487c478bd9Sstevel@tonic-gate pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0) 6517c478bd9Sstevel@tonic-gate print_probe_info(&p); 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate return (0); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6577c478bd9Sstevel@tonic-gate static int 6587c478bd9Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 6597c478bd9Sstevel@tonic-gate dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 6607c478bd9Sstevel@tonic-gate { 6617c478bd9Sstevel@tonic-gate dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate if (edp == *last) 6647c478bd9Sstevel@tonic-gate return (0); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) { 6677c478bd9Sstevel@tonic-gate error("failed to match %s:%s:%s:%s: %s\n", 6687c478bd9Sstevel@tonic-gate edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod, 6697c478bd9Sstevel@tonic-gate edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name, 6707c478bd9Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp))); 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate *last = edp; 6747c478bd9Sstevel@tonic-gate return (0); 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate /* 6787c478bd9Sstevel@tonic-gate * List the probes corresponding to the specified program by iterating over 6797c478bd9Sstevel@tonic-gate * each statement and then matching probes to the statement probe descriptions. 6807c478bd9Sstevel@tonic-gate */ 6817c478bd9Sstevel@tonic-gate static void 6827c478bd9Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp) 6837c478bd9Sstevel@tonic-gate { 6847c478bd9Sstevel@tonic-gate dtrace_ecbdesc_t *last = NULL; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 6877c478bd9Sstevel@tonic-gate (dtrace_stmt_f *)list_stmt, &last); 6887c478bd9Sstevel@tonic-gate } 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate static void 6917c478bd9Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp) 6927c478bd9Sstevel@tonic-gate { 6937c478bd9Sstevel@tonic-gate char *arg0; 6947c478bd9Sstevel@tonic-gate FILE *fp; 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate if ((fp = fopen(dcp->dc_arg, "r")) == NULL) 6977c478bd9Sstevel@tonic-gate fatal("failed to open %s", dcp->dc_arg); 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate arg0 = g_argv[0]; 7007c478bd9Sstevel@tonic-gate g_argv[0] = dcp->dc_arg; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp, 7037c478bd9Sstevel@tonic-gate g_cflags, g_argc, g_argv)) == NULL) 7047c478bd9Sstevel@tonic-gate dfatal("failed to compile script %s", dcp->dc_arg); 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate g_argv[0] = arg0; 7077c478bd9Sstevel@tonic-gate (void) fclose(fp); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate dcp->dc_desc = "script"; 7107c478bd9Sstevel@tonic-gate dcp->dc_name = dcp->dc_arg; 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate static void 7147c478bd9Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp) 7157c478bd9Sstevel@tonic-gate { 7167c478bd9Sstevel@tonic-gate char *p; 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg, 7197c478bd9Sstevel@tonic-gate dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL) 7207c478bd9Sstevel@tonic-gate dfatal("invalid probe specifier %s", dcp->dc_arg); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL) 7237c478bd9Sstevel@tonic-gate *p = '\0'; /* crop name for reporting */ 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate dcp->dc_desc = "description"; 7267c478bd9Sstevel@tonic-gate dcp->dc_name = dcp->dc_arg; 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7307c478bd9Sstevel@tonic-gate static void 7317c478bd9Sstevel@tonic-gate prochandler(struct ps_prochandle *P, void *arg) 7327c478bd9Sstevel@tonic-gate { 7337c478bd9Sstevel@tonic-gate const psinfo_t *prp = Ppsinfo(P); 7347c478bd9Sstevel@tonic-gate int pid = Pstatus(P)->pr_pid; 7357c478bd9Sstevel@tonic-gate char name[SIG2STR_MAX]; 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate switch (Pstate(P)) { 7387c478bd9Sstevel@tonic-gate case PS_UNDEAD: 7397c478bd9Sstevel@tonic-gate /* 7407c478bd9Sstevel@tonic-gate * Ideally we would like to always report pr_wstat here, but it 7417c478bd9Sstevel@tonic-gate * isn't possible given current /proc semantics. If we grabbed 7427c478bd9Sstevel@tonic-gate * the process, Ppsinfo() will either fail or return a zeroed 7437c478bd9Sstevel@tonic-gate * psinfo_t depending on how far the parent is in reaping it. 7447c478bd9Sstevel@tonic-gate * When /proc provides a stable pr_wstat in the status file, 7457c478bd9Sstevel@tonic-gate * this code can be improved by examining this new pr_wstat. 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) { 7487c478bd9Sstevel@tonic-gate notice("pid %d terminated by %s\n", pid, 7497c478bd9Sstevel@tonic-gate proc_signame(WTERMSIG(prp->pr_wstat), 7507c478bd9Sstevel@tonic-gate name, sizeof (name))); 7517c478bd9Sstevel@tonic-gate } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) { 7527c478bd9Sstevel@tonic-gate notice("pid %d exited with status %d\n", 7537c478bd9Sstevel@tonic-gate pid, WEXITSTATUS(prp->pr_wstat)); 7547c478bd9Sstevel@tonic-gate } else { 7557c478bd9Sstevel@tonic-gate notice("pid %d has exited\n", pid); 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate g_pslive--; 7587c478bd9Sstevel@tonic-gate break; 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate case PS_LOST: 7617c478bd9Sstevel@tonic-gate notice("pid %d exec'd a set-id or unobservable program\n", pid); 7627c478bd9Sstevel@tonic-gate g_pslive--; 7637c478bd9Sstevel@tonic-gate break; 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7687c478bd9Sstevel@tonic-gate static int 7697c478bd9Sstevel@tonic-gate errhandler(dtrace_errdata_t *data, void *arg) 7707c478bd9Sstevel@tonic-gate { 7717c478bd9Sstevel@tonic-gate error(data->dteda_msg); 7727c478bd9Sstevel@tonic-gate return (DTRACE_HANDLE_OK); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7767c478bd9Sstevel@tonic-gate static int 7777c478bd9Sstevel@tonic-gate drophandler(dtrace_dropdata_t *data, void *arg) 7787c478bd9Sstevel@tonic-gate { 7797c478bd9Sstevel@tonic-gate error(data->dtdda_msg); 7807c478bd9Sstevel@tonic-gate return (DTRACE_HANDLE_OK); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7847c478bd9Sstevel@tonic-gate static int 7857c478bd9Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate dtrace_actkind_t act; 7887c478bd9Sstevel@tonic-gate uintptr_t addr; 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate if (rec == NULL) { 7917c478bd9Sstevel@tonic-gate /* 7927c478bd9Sstevel@tonic-gate * We have processed the final record; output the newline if 7937c478bd9Sstevel@tonic-gate * we're not in quiet mode. 7947c478bd9Sstevel@tonic-gate */ 7957c478bd9Sstevel@tonic-gate if (!g_quiet) 7967c478bd9Sstevel@tonic-gate oprintf("\n"); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT); 7997c478bd9Sstevel@tonic-gate } 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate act = rec->dtrd_action; 8027c478bd9Sstevel@tonic-gate addr = (uintptr_t)data->dtpda_data; 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate if (act == DTRACEACT_EXIT) { 8057c478bd9Sstevel@tonic-gate g_status = *((uint32_t *)addr); 8067c478bd9Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT); 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate return (DTRACE_CONSUME_THIS); 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8137c478bd9Sstevel@tonic-gate static int 8147c478bd9Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg) 8157c478bd9Sstevel@tonic-gate { 8167c478bd9Sstevel@tonic-gate dtrace_probedesc_t *pd = data->dtpda_pdesc; 8177c478bd9Sstevel@tonic-gate processorid_t cpu = data->dtpda_cpu; 8187c478bd9Sstevel@tonic-gate static int heading; 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate if (g_impatient) { 8217c478bd9Sstevel@tonic-gate g_newline = 0; 8227c478bd9Sstevel@tonic-gate return (DTRACE_CONSUME_ABORT); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate if (heading == 0) { 8267c478bd9Sstevel@tonic-gate if (!g_flowindent) { 8277c478bd9Sstevel@tonic-gate if (!g_quiet) { 8287c478bd9Sstevel@tonic-gate oprintf("%3s %6s %32s\n", 8297c478bd9Sstevel@tonic-gate "CPU", "ID", "FUNCTION:NAME"); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate } else { 8327c478bd9Sstevel@tonic-gate oprintf("%3s %-41s\n", "CPU", "FUNCTION"); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate heading = 1; 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate if (!g_flowindent) { 8387c478bd9Sstevel@tonic-gate if (!g_quiet) { 8397c478bd9Sstevel@tonic-gate char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2]; 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s:%s", 8427c478bd9Sstevel@tonic-gate pd->dtpd_func, pd->dtpd_name); 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name); 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate } else { 8477c478bd9Sstevel@tonic-gate int indent = data->dtpda_indent; 8487c478bd9Sstevel@tonic-gate char *name; 8497c478bd9Sstevel@tonic-gate size_t len; 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (data->dtpda_flow == DTRACEFLOW_NONE) { 8527c478bd9Sstevel@tonic-gate len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5; 8537c478bd9Sstevel@tonic-gate name = alloca(len); 8547c478bd9Sstevel@tonic-gate (void) snprintf(name, len, "%*s%s%s:%s", indent, "", 8557c478bd9Sstevel@tonic-gate data->dtpda_prefix, pd->dtpd_func, 8567c478bd9Sstevel@tonic-gate pd->dtpd_name); 8577c478bd9Sstevel@tonic-gate } else { 8587c478bd9Sstevel@tonic-gate len = indent + DTRACE_FUNCNAMELEN + 5; 8597c478bd9Sstevel@tonic-gate name = alloca(len); 8607c478bd9Sstevel@tonic-gate (void) snprintf(name, len, "%*s%s%s", indent, "", 8617c478bd9Sstevel@tonic-gate data->dtpda_prefix, pd->dtpd_func); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate oprintf("%3d %-41s ", cpu, name); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate return (DTRACE_CONSUME_THIS); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate static void 8717c478bd9Sstevel@tonic-gate go(void) 8727c478bd9Sstevel@tonic-gate { 8737c478bd9Sstevel@tonic-gate int i; 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate struct { 8767c478bd9Sstevel@tonic-gate char *name; 8777c478bd9Sstevel@tonic-gate char *optname; 8787c478bd9Sstevel@tonic-gate dtrace_optval_t val; 8797c478bd9Sstevel@tonic-gate } bufs[] = { 8807c478bd9Sstevel@tonic-gate { "buffer size", "bufsize" }, 8817c478bd9Sstevel@tonic-gate { "aggregation size", "aggsize" }, 8827c478bd9Sstevel@tonic-gate { "speculation size", "specsize" }, 8837c478bd9Sstevel@tonic-gate { "dynamic variable size", "dynvarsize" }, 8847c478bd9Sstevel@tonic-gate { NULL } 8857c478bd9Sstevel@tonic-gate }, rates[] = { 8867c478bd9Sstevel@tonic-gate { "cleaning rate", "cleanrate" }, 8877c478bd9Sstevel@tonic-gate { "status rate", "statusrate" }, 8887c478bd9Sstevel@tonic-gate { NULL } 8897c478bd9Sstevel@tonic-gate }; 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate for (i = 0; bufs[i].name != NULL; i++) { 8927c478bd9Sstevel@tonic-gate if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1) 8937c478bd9Sstevel@tonic-gate fatal("couldn't get option %s", bufs[i].optname); 8947c478bd9Sstevel@tonic-gate } 8957c478bd9Sstevel@tonic-gate 8967c478bd9Sstevel@tonic-gate for (i = 0; rates[i].name != NULL; i++) { 8977c478bd9Sstevel@tonic-gate if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1) 8987c478bd9Sstevel@tonic-gate fatal("couldn't get option %s", rates[i].optname); 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate if (dtrace_go(g_dtp) == -1) 9027c478bd9Sstevel@tonic-gate dfatal("could not enable tracing"); 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate for (i = 0; bufs[i].name != NULL; i++) { 9057c478bd9Sstevel@tonic-gate dtrace_optval_t j = 0, mul = 10; 9067c478bd9Sstevel@tonic-gate dtrace_optval_t nsize; 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate if (bufs[i].val == DTRACEOPT_UNSET) 9097c478bd9Sstevel@tonic-gate continue; 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize); 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate if (nsize == DTRACEOPT_UNSET || nsize == 0) 9147c478bd9Sstevel@tonic-gate continue; 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate if (nsize >= bufs[i].val - sizeof (uint64_t)) 9177c478bd9Sstevel@tonic-gate continue; 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10) 9207c478bd9Sstevel@tonic-gate continue; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) { 9237c478bd9Sstevel@tonic-gate error("%s lowered to %lld%c\n", bufs[i].name, 9247c478bd9Sstevel@tonic-gate (long long)nsize >> (mul - 10), " kmgtpe"[j]); 9257c478bd9Sstevel@tonic-gate } else { 9267c478bd9Sstevel@tonic-gate error("%s lowered to %lld bytes\n", bufs[i].name, 9277c478bd9Sstevel@tonic-gate (long long)nsize); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate for (i = 0; rates[i].name != NULL; i++) { 9327c478bd9Sstevel@tonic-gate dtrace_optval_t nval; 9337c478bd9Sstevel@tonic-gate char *dir; 9347c478bd9Sstevel@tonic-gate 9357c478bd9Sstevel@tonic-gate if (rates[i].val == DTRACEOPT_UNSET) 9367c478bd9Sstevel@tonic-gate continue; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, rates[i].optname, &nval); 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate if (nval == DTRACEOPT_UNSET || nval == 0) 9417c478bd9Sstevel@tonic-gate continue; 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate if (rates[i].val == nval) 9447c478bd9Sstevel@tonic-gate continue; 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate dir = nval > rates[i].val ? "reduced" : "increased"; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate if (nval <= NANOSEC && (NANOSEC % nval) == 0) { 9497c478bd9Sstevel@tonic-gate error("%s %s to %lld hz\n", rates[i].name, dir, 9507c478bd9Sstevel@tonic-gate (long long)NANOSEC / (long long)nval); 9517c478bd9Sstevel@tonic-gate continue; 9527c478bd9Sstevel@tonic-gate } 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate if ((nval % NANOSEC) == 0) { 9557c478bd9Sstevel@tonic-gate error("%s %s to once every %lld seconds\n", 9567c478bd9Sstevel@tonic-gate rates[i].name, dir, 9577c478bd9Sstevel@tonic-gate (long long)nval / (long long)NANOSEC); 9587c478bd9Sstevel@tonic-gate continue; 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate error("%s %s to once every %lld nanoseconds\n", 9627c478bd9Sstevel@tonic-gate rates[i].name, dir, (long long)nval); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 9677c478bd9Sstevel@tonic-gate static void 9687c478bd9Sstevel@tonic-gate intr(int signo) 9697c478bd9Sstevel@tonic-gate { 9707c478bd9Sstevel@tonic-gate if (!g_intr) 9717c478bd9Sstevel@tonic-gate g_newline = 1; 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate if (g_intr++) 9747c478bd9Sstevel@tonic-gate g_impatient = 1; 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate int 9787c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 9797c478bd9Sstevel@tonic-gate { 9807c478bd9Sstevel@tonic-gate dtrace_bufdesc_t buf; 9817c478bd9Sstevel@tonic-gate struct sigaction act; 9827c478bd9Sstevel@tonic-gate dtrace_status_t status[2]; 9837c478bd9Sstevel@tonic-gate dtrace_optval_t opt; 9847c478bd9Sstevel@tonic-gate dtrace_cmd_t *dcp; 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate int done = 0, mode = 0; 9877c478bd9Sstevel@tonic-gate int err, i; 9887c478bd9Sstevel@tonic-gate char c, *p, **v; 9897c478bd9Sstevel@tonic-gate struct ps_prochandle *P; 9907c478bd9Sstevel@tonic-gate pid_t pid; 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate g_pname = basename(argv[0]); 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate if (argc == 1) 9957c478bd9Sstevel@tonic-gate return (usage(stderr)); 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate if ((g_argv = malloc(sizeof (char *) * argc)) == NULL || 9987c478bd9Sstevel@tonic-gate (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL || 9997c478bd9Sstevel@tonic-gate (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL) 10007c478bd9Sstevel@tonic-gate fatal("failed to allocate memory for arguments"); 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */ 10037c478bd9Sstevel@tonic-gate argv[0] = g_pname; /* rewrite argv[0] for getopt errors */ 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate bzero(status, sizeof (status)); 10067c478bd9Sstevel@tonic-gate bzero(&buf, sizeof (buf)); 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate /* 10097c478bd9Sstevel@tonic-gate * Make an initial pass through argv[] processing any arguments that 10107c478bd9Sstevel@tonic-gate * affect our behavior mode (g_mode) and flags used for dtrace_open(). 10117c478bd9Sstevel@tonic-gate * We also accumulate arguments that are not affiliated with getopt 10127c478bd9Sstevel@tonic-gate * options into g_argv[], and abort if any invalid options are found. 10137c478bd9Sstevel@tonic-gate */ 10147c478bd9Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 10157c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 10167c478bd9Sstevel@tonic-gate switch (c) { 10177c478bd9Sstevel@tonic-gate case '3': 10187c478bd9Sstevel@tonic-gate if (strcmp(optarg, "2") != 0) { 10197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10207c478bd9Sstevel@tonic-gate "%s: illegal option -- 3%s\n", 10217c478bd9Sstevel@tonic-gate argv[0], optarg); 10227c478bd9Sstevel@tonic-gate return (usage(stderr)); 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate g_oflags &= ~DTRACE_O_LP64; 10257c478bd9Sstevel@tonic-gate g_oflags |= DTRACE_O_ILP32; 10267c478bd9Sstevel@tonic-gate break; 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate case '6': 10297c478bd9Sstevel@tonic-gate if (strcmp(optarg, "4") != 0) { 10307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10317c478bd9Sstevel@tonic-gate "%s: illegal option -- 6%s\n", 10327c478bd9Sstevel@tonic-gate argv[0], optarg); 10337c478bd9Sstevel@tonic-gate return (usage(stderr)); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate g_oflags &= ~DTRACE_O_ILP32; 10367c478bd9Sstevel@tonic-gate g_oflags |= DTRACE_O_LP64; 10377c478bd9Sstevel@tonic-gate break; 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate case 'a': 10407c478bd9Sstevel@tonic-gate g_grabanon++; /* also checked in pass 2 below */ 10417c478bd9Sstevel@tonic-gate break; 10427c478bd9Sstevel@tonic-gate 10437c478bd9Sstevel@tonic-gate case 'A': 10447c478bd9Sstevel@tonic-gate g_mode = DMODE_ANON; 10457c478bd9Sstevel@tonic-gate g_exec = 0; 10467c478bd9Sstevel@tonic-gate mode++; 10477c478bd9Sstevel@tonic-gate break; 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate case 'e': 10507c478bd9Sstevel@tonic-gate g_exec = 0; 10517c478bd9Sstevel@tonic-gate done = 1; 10527c478bd9Sstevel@tonic-gate break; 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate case 'G': 10557c478bd9Sstevel@tonic-gate g_mode = DMODE_LINK; 10567c478bd9Sstevel@tonic-gate g_oflags |= DTRACE_O_NODEV; 10577c478bd9Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */ 10587c478bd9Sstevel@tonic-gate g_exec = 0; 10597c478bd9Sstevel@tonic-gate mode++; 10607c478bd9Sstevel@tonic-gate break; 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate case 'l': 10637c478bd9Sstevel@tonic-gate g_mode = DMODE_LIST; 10647c478bd9Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */ 10657c478bd9Sstevel@tonic-gate mode++; 10667c478bd9Sstevel@tonic-gate break; 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate case 'V': 10697c478bd9Sstevel@tonic-gate g_mode = DMODE_VERS; 10707c478bd9Sstevel@tonic-gate mode++; 10717c478bd9Sstevel@tonic-gate break; 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate default: 10747c478bd9Sstevel@tonic-gate if (strchr(DTRACE_OPTSTR, c) == NULL) 10757c478bd9Sstevel@tonic-gate return (usage(stderr)); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate if (optind < argc) 10807c478bd9Sstevel@tonic-gate g_argv[g_argc++] = argv[optind]; 10817c478bd9Sstevel@tonic-gate } 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate if (mode > 1) { 10847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: only one of the [-AGlV] options " 10857c478bd9Sstevel@tonic-gate "can be specified at a time\n", g_pname); 10867c478bd9Sstevel@tonic-gate return (E_USAGE); 10877c478bd9Sstevel@tonic-gate } 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate if (g_mode == DMODE_VERS) 10907c478bd9Sstevel@tonic-gate return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0); 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate /* 10937c478bd9Sstevel@tonic-gate * Open libdtrace. If we are not actually going to be enabling any 10947c478bd9Sstevel@tonic-gate * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV. 10957c478bd9Sstevel@tonic-gate */ 10967c478bd9Sstevel@tonic-gate while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) { 10977c478bd9Sstevel@tonic-gate if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) { 10987c478bd9Sstevel@tonic-gate g_oflags |= DTRACE_O_NODEV; 10997c478bd9Sstevel@tonic-gate continue; 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate fatal("failed to initialize dtrace: %s\n", 11037c478bd9Sstevel@tonic-gate dtrace_errmsg(NULL, err)); 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "bufsize", "4m"); 11077c478bd9Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "aggsize", "4m"); 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate /* 11107c478bd9Sstevel@tonic-gate * If -G is specified, enable -xlink=dynamic and -xunodefs to permit 11117c478bd9Sstevel@tonic-gate * references to undefined symbols to remain as unresolved relocations. 11127c478bd9Sstevel@tonic-gate * If -A is specified, enable -xlink=primary to permit static linking 11137c478bd9Sstevel@tonic-gate * only to kernel symbols that are defined in a primary kernel module. 11147c478bd9Sstevel@tonic-gate */ 11157c478bd9Sstevel@tonic-gate if (g_mode == DMODE_LINK) { 11167c478bd9Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "linkmode", "dynamic"); 11177c478bd9Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "unodefs", NULL); 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate g_objc = g_argc; 11207c478bd9Sstevel@tonic-gate g_objv = g_argv; 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate /* 11237c478bd9Sstevel@tonic-gate * We still use g_argv[0], the name of the executable. 11247c478bd9Sstevel@tonic-gate */ 11257c478bd9Sstevel@tonic-gate g_argc = 1; 11267c478bd9Sstevel@tonic-gate } else if (g_mode == DMODE_ANON) 11277c478bd9Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "linkmode", "primary"); 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate /* 11307c478bd9Sstevel@tonic-gate * Now that we have libdtrace open, make a second pass through argv[] 11317c478bd9Sstevel@tonic-gate * to perform any dtrace_setopt() calls and change any compiler flags. 11327c478bd9Sstevel@tonic-gate * We also accumulate any program specifications into our g_cmdv[] at 11337c478bd9Sstevel@tonic-gate * this time; these will compiled as part of the fourth processing pass. 11347c478bd9Sstevel@tonic-gate */ 11357c478bd9Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 11367c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 11377c478bd9Sstevel@tonic-gate switch (c) { 11387c478bd9Sstevel@tonic-gate case 'a': 11397c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "grabanon", 0) != 0) 11407c478bd9Sstevel@tonic-gate dfatal("failed to set -a"); 11417c478bd9Sstevel@tonic-gate break; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate case 'b': 11447c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, 11457c478bd9Sstevel@tonic-gate "bufsize", optarg) != 0) 11467c478bd9Sstevel@tonic-gate dfatal("failed to set -b %s", optarg); 11477c478bd9Sstevel@tonic-gate break; 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate case 'C': 11507c478bd9Sstevel@tonic-gate g_cflags |= DTRACE_C_CPP; 11517c478bd9Sstevel@tonic-gate break; 11527c478bd9Sstevel@tonic-gate 11537c478bd9Sstevel@tonic-gate case 'D': 11547c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "define", optarg) != 0) 11557c478bd9Sstevel@tonic-gate dfatal("failed to set -D %s", optarg); 11567c478bd9Sstevel@tonic-gate break; 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate case 'f': 11597c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 11607c478bd9Sstevel@tonic-gate dcp->dc_func = compile_str; 11617c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_FUNC; 11627c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 11637c478bd9Sstevel@tonic-gate break; 11647c478bd9Sstevel@tonic-gate 11657c478bd9Sstevel@tonic-gate case 'F': 11667c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "flowindent", 0) != 0) 11677c478bd9Sstevel@tonic-gate dfatal("failed to set -F"); 11687c478bd9Sstevel@tonic-gate break; 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate case 'H': 11717c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0) 11727c478bd9Sstevel@tonic-gate dfatal("failed to set -H"); 11737c478bd9Sstevel@tonic-gate break; 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate case 'i': 11767c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 11777c478bd9Sstevel@tonic-gate dcp->dc_func = compile_str; 11787c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NAME; 11797c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 11807c478bd9Sstevel@tonic-gate break; 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate case 'I': 11837c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "incdir", optarg) != 0) 11847c478bd9Sstevel@tonic-gate dfatal("failed to set -I %s", optarg); 11857c478bd9Sstevel@tonic-gate break; 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate case 'L': 11887c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "libdir", optarg) != 0) 11897c478bd9Sstevel@tonic-gate dfatal("failed to set -L %s", optarg); 11907c478bd9Sstevel@tonic-gate break; 11917c478bd9Sstevel@tonic-gate 11927c478bd9Sstevel@tonic-gate case 'm': 11937c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 11947c478bd9Sstevel@tonic-gate dcp->dc_func = compile_str; 11957c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_MOD; 11967c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 11977c478bd9Sstevel@tonic-gate break; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate case 'n': 12007c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 12017c478bd9Sstevel@tonic-gate dcp->dc_func = compile_str; 12027c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NAME; 12037c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 12047c478bd9Sstevel@tonic-gate break; 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate case 'P': 12077c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 12087c478bd9Sstevel@tonic-gate dcp->dc_func = compile_str; 12097c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER; 12107c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 12117c478bd9Sstevel@tonic-gate break; 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate case 'q': 12147c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "quiet", 0) != 0) 12157c478bd9Sstevel@tonic-gate dfatal("failed to set -q"); 12167c478bd9Sstevel@tonic-gate break; 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate case 'o': 12197c478bd9Sstevel@tonic-gate g_ofile = optarg; 12207c478bd9Sstevel@tonic-gate break; 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate case 's': 12237c478bd9Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 12247c478bd9Sstevel@tonic-gate dcp->dc_func = compile_file; 12257c478bd9Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NONE; 12267c478bd9Sstevel@tonic-gate dcp->dc_arg = optarg; 12277c478bd9Sstevel@tonic-gate break; 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate case 'S': 12307c478bd9Sstevel@tonic-gate g_cflags |= DTRACE_C_DIFV; 12317c478bd9Sstevel@tonic-gate break; 12327c478bd9Sstevel@tonic-gate 12337c478bd9Sstevel@tonic-gate case 'U': 12347c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "undef", optarg) != 0) 12357c478bd9Sstevel@tonic-gate dfatal("failed to set -U %s", optarg); 12367c478bd9Sstevel@tonic-gate break; 12377c478bd9Sstevel@tonic-gate 12387c478bd9Sstevel@tonic-gate case 'v': 12397c478bd9Sstevel@tonic-gate g_verbose++; 12407c478bd9Sstevel@tonic-gate break; 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate case 'w': 12437c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "destructive", 0) != 0) 12447c478bd9Sstevel@tonic-gate dfatal("failed to set -w"); 12457c478bd9Sstevel@tonic-gate break; 12467c478bd9Sstevel@tonic-gate 12477c478bd9Sstevel@tonic-gate case 'x': 12487c478bd9Sstevel@tonic-gate if ((p = strchr(optarg, '=')) != NULL) 12497c478bd9Sstevel@tonic-gate *p++ = '\0'; 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, optarg, p) != 0) 12527c478bd9Sstevel@tonic-gate dfatal("failed to set -x %s", optarg); 12537c478bd9Sstevel@tonic-gate break; 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate case 'X': 12567c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "stdc", optarg) != 0) 12577c478bd9Sstevel@tonic-gate dfatal("failed to set -X %s", optarg); 12587c478bd9Sstevel@tonic-gate break; 12597c478bd9Sstevel@tonic-gate 12607c478bd9Sstevel@tonic-gate case 'Z': 12617c478bd9Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; 12627c478bd9Sstevel@tonic-gate break; 12637c478bd9Sstevel@tonic-gate 12647c478bd9Sstevel@tonic-gate default: 12657c478bd9Sstevel@tonic-gate if (strchr(DTRACE_OPTSTR, c) == NULL) 12667c478bd9Sstevel@tonic-gate return (usage(stderr)); 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate } 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate 12717c478bd9Sstevel@tonic-gate /* 12727c478bd9Sstevel@tonic-gate * In our third pass we handle any command-line options related to 12737c478bd9Sstevel@tonic-gate * grabbing or creating victim processes. The behavior of these calls 12747c478bd9Sstevel@tonic-gate * may been affected by any library options set by the second pass. 12757c478bd9Sstevel@tonic-gate */ 12767c478bd9Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 12777c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 12787c478bd9Sstevel@tonic-gate switch (c) { 12797c478bd9Sstevel@tonic-gate case 'c': 12807c478bd9Sstevel@tonic-gate if ((v = make_argv(optarg)) == NULL) 12817c478bd9Sstevel@tonic-gate fatal("failed to allocate memory"); 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate P = dtrace_proc_create(g_dtp, v[0], v); 12847c478bd9Sstevel@tonic-gate if (P == NULL) 12857c478bd9Sstevel@tonic-gate dfatal(NULL); /* dtrace_errmsg() only */ 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate g_psv[g_psc++] = P; 12887c478bd9Sstevel@tonic-gate free(v); 12897c478bd9Sstevel@tonic-gate break; 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate case 'p': 12927c478bd9Sstevel@tonic-gate errno = 0; 12937c478bd9Sstevel@tonic-gate pid = strtol(optarg, &p, 10); 12947c478bd9Sstevel@tonic-gate 12957c478bd9Sstevel@tonic-gate if (errno != 0 || p == optarg || p[0] != '\0') 12967c478bd9Sstevel@tonic-gate fatal("invalid pid: %s\n", optarg); 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate P = dtrace_proc_grab(g_dtp, pid, 0); 12997c478bd9Sstevel@tonic-gate if (P == NULL) 13007c478bd9Sstevel@tonic-gate dfatal(NULL); /* dtrace_errmsg() only */ 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate g_psv[g_psc++] = P; 13037c478bd9Sstevel@tonic-gate break; 13047c478bd9Sstevel@tonic-gate } 13057c478bd9Sstevel@tonic-gate } 13067c478bd9Sstevel@tonic-gate } 13077c478bd9Sstevel@tonic-gate 13087c478bd9Sstevel@tonic-gate /* 13097c478bd9Sstevel@tonic-gate * In our fourth pass we finish g_cmdv[] by calling dc_func to convert 13107c478bd9Sstevel@tonic-gate * each string or file specification into a compiled program structure. 13117c478bd9Sstevel@tonic-gate */ 13127c478bd9Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 13137c478bd9Sstevel@tonic-gate g_cmdv[i].dc_func(&g_cmdv[i]); 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate if (g_mode != DMODE_LIST) { 13167c478bd9Sstevel@tonic-gate if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1) 13177c478bd9Sstevel@tonic-gate dfatal("failed to establish error handler"); 13187c478bd9Sstevel@tonic-gate 13197c478bd9Sstevel@tonic-gate if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1) 13207c478bd9Sstevel@tonic-gate dfatal("failed to establish drop handler"); 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1) 13237c478bd9Sstevel@tonic-gate dfatal("failed to establish proc handler"); 13247c478bd9Sstevel@tonic-gate } 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "flowindent", &opt); 13277c478bd9Sstevel@tonic-gate g_flowindent = opt != DTRACEOPT_UNSET; 13287c478bd9Sstevel@tonic-gate 13297c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "grabanon", &opt); 13307c478bd9Sstevel@tonic-gate g_grabanon = opt != DTRACEOPT_UNSET; 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "quiet", &opt); 13337c478bd9Sstevel@tonic-gate g_quiet = opt != DTRACEOPT_UNSET; 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate /* 13367c478bd9Sstevel@tonic-gate * Now make a fifth and final pass over the options that have been 13377c478bd9Sstevel@tonic-gate * turned into programs and saved in g_cmdv[], performing any mode- 13387c478bd9Sstevel@tonic-gate * specific processing. If g_mode is DMODE_EXEC, we will break out 13397c478bd9Sstevel@tonic-gate * of the switch() and continue on to the data processing loop. For 13407c478bd9Sstevel@tonic-gate * other modes, we will exit dtrace once mode-specific work is done. 13417c478bd9Sstevel@tonic-gate */ 13427c478bd9Sstevel@tonic-gate switch (g_mode) { 13437c478bd9Sstevel@tonic-gate case DMODE_EXEC: 13447c478bd9Sstevel@tonic-gate if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 13457c478bd9Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 13467c478bd9Sstevel@tonic-gate 13477c478bd9Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 13487c478bd9Sstevel@tonic-gate exec_prog(&g_cmdv[i]); 13497c478bd9Sstevel@tonic-gate 13507c478bd9Sstevel@tonic-gate if (done && !g_grabanon) 13517c478bd9Sstevel@tonic-gate return (g_status); 13527c478bd9Sstevel@tonic-gate break; 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate case DMODE_ANON: 13557c478bd9Sstevel@tonic-gate if (g_ofile == NULL) 13567c478bd9Sstevel@tonic-gate g_ofile = "/kernel/drv/dtrace.conf"; 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate dof_prune(g_ofile); /* strip out any old DOF directives */ 13597c478bd9Sstevel@tonic-gate etcsystem_prune(); /* string out any forceload directives */ 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate if (g_cmdc == 0) 13627c478bd9Sstevel@tonic-gate return (g_status); 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate if ((g_ofp = fopen(g_ofile, "a")) == NULL) 13657c478bd9Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 13667c478bd9Sstevel@tonic-gate 13677c478bd9Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) { 13687c478bd9Sstevel@tonic-gate anon_prog(&g_cmdv[i], 13697c478bd9Sstevel@tonic-gate dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i); 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate /* 13737c478bd9Sstevel@tonic-gate * Dump out the DOF corresponding to the error handler and the 13747c478bd9Sstevel@tonic-gate * current options as the final DOF property in the .conf file. 13757c478bd9Sstevel@tonic-gate */ 13767c478bd9Sstevel@tonic-gate anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++); 13777c478bd9Sstevel@tonic-gate anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++); 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate if (fclose(g_ofp) == EOF) 13807c478bd9Sstevel@tonic-gate fatal("failed to close output file '%s'", g_ofile); 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate /* 13837c478bd9Sstevel@tonic-gate * These messages would use notice() rather than error(), but 13847c478bd9Sstevel@tonic-gate * we don't want them suppressed when -A is run on a D program 13857c478bd9Sstevel@tonic-gate * that itself contains a #pragma D option quiet. 13867c478bd9Sstevel@tonic-gate */ 13877c478bd9Sstevel@tonic-gate error("saved anonymous enabling in %s\n", g_ofile); 13887c478bd9Sstevel@tonic-gate etcsystem_add(); 13897c478bd9Sstevel@tonic-gate error("run update_drv(1M) or reboot to enable changes\n"); 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate return (g_status); 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate case DMODE_LINK: 13947c478bd9Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 13957c478bd9Sstevel@tonic-gate link_prog(&g_cmdv[i]); 1396*0b38a8bdSahl 1397*0b38a8bdSahl if (g_cmdc > 1 && g_ofile != NULL) { 1398*0b38a8bdSahl char **objv = alloca(g_cmdc * sizeof (char *)); 1399*0b38a8bdSahl 1400*0b38a8bdSahl for (i = 0; i < g_cmdc; i++) 1401*0b38a8bdSahl objv[i] = g_cmdv[i].dc_ofile; 1402*0b38a8bdSahl 1403*0b38a8bdSahl if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES, 1404*0b38a8bdSahl g_ofile, g_cmdc, objv) != 0) 1405*0b38a8bdSahl dfatal("failed to link %s", g_ofile); 1406*0b38a8bdSahl } 1407*0b38a8bdSahl 14087c478bd9Sstevel@tonic-gate return (g_status); 14097c478bd9Sstevel@tonic-gate 14107c478bd9Sstevel@tonic-gate case DMODE_LIST: 14117c478bd9Sstevel@tonic-gate if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 14127c478bd9Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate oprintf("%5s %10s %17s %33s %s\n", 14157c478bd9Sstevel@tonic-gate "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME"); 14167c478bd9Sstevel@tonic-gate 14177c478bd9Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 14187c478bd9Sstevel@tonic-gate list_prog(&g_cmdv[i]); 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate if (g_cmdc == 0) 14217c478bd9Sstevel@tonic-gate (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL); 14227c478bd9Sstevel@tonic-gate 14237c478bd9Sstevel@tonic-gate return (g_status); 14247c478bd9Sstevel@tonic-gate } 14257c478bd9Sstevel@tonic-gate 14267c478bd9Sstevel@tonic-gate /* 14277c478bd9Sstevel@tonic-gate * If -a and -Z were not specified and no probes have been matched, no 14287c478bd9Sstevel@tonic-gate * probe criteria was specified on the command line and we abort. 14297c478bd9Sstevel@tonic-gate */ 14307c478bd9Sstevel@tonic-gate if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS)) 14317c478bd9Sstevel@tonic-gate dfatal("no probes %s\n", g_cmdc ? "matched" : "specified"); 14327c478bd9Sstevel@tonic-gate 14337c478bd9Sstevel@tonic-gate /* 14347c478bd9Sstevel@tonic-gate * Start tracing. Once we dtrace_go(), reload any options that affect 14357c478bd9Sstevel@tonic-gate * our globals in case consuming anonymous state has changed them. 14367c478bd9Sstevel@tonic-gate */ 14377c478bd9Sstevel@tonic-gate go(); 14387c478bd9Sstevel@tonic-gate 14397c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "flowindent", &opt); 14407c478bd9Sstevel@tonic-gate g_flowindent = opt != DTRACEOPT_UNSET; 14417c478bd9Sstevel@tonic-gate 14427c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "grabanon", &opt); 14437c478bd9Sstevel@tonic-gate g_grabanon = opt != DTRACEOPT_UNSET; 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "quiet", &opt); 14467c478bd9Sstevel@tonic-gate g_quiet = opt != DTRACEOPT_UNSET; 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "destructive", &opt); 14497c478bd9Sstevel@tonic-gate if (opt != DTRACEOPT_UNSET) 14507c478bd9Sstevel@tonic-gate notice("allowing destructive actions\n"); 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 14537c478bd9Sstevel@tonic-gate act.sa_flags = 0; 14547c478bd9Sstevel@tonic-gate act.sa_handler = intr; 14557c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 14567c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 14577c478bd9Sstevel@tonic-gate 14587c478bd9Sstevel@tonic-gate /* 14597c478bd9Sstevel@tonic-gate * Now that tracing is active and we are ready to consume trace data, 14607c478bd9Sstevel@tonic-gate * continue any grabbed or created processes, setting them running 14617c478bd9Sstevel@tonic-gate * using the /proc control mechanism inside of libdtrace. 14627c478bd9Sstevel@tonic-gate */ 14637c478bd9Sstevel@tonic-gate for (i = 0; i < g_psc; i++) 14647c478bd9Sstevel@tonic-gate dtrace_proc_continue(g_dtp, g_psv[i]); 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate g_pslive = g_psc; /* count for prochandler() */ 14677c478bd9Sstevel@tonic-gate 14687c478bd9Sstevel@tonic-gate do { 14697c478bd9Sstevel@tonic-gate if (!g_intr && !done) 14707c478bd9Sstevel@tonic-gate dtrace_sleep(g_dtp); 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate if (g_newline) { 14737c478bd9Sstevel@tonic-gate /* 14747c478bd9Sstevel@tonic-gate * Output a newline just to make the output look 14757c478bd9Sstevel@tonic-gate * slightly cleaner. Note that we do this even in 14767c478bd9Sstevel@tonic-gate * "quiet" mode... 14777c478bd9Sstevel@tonic-gate */ 14787c478bd9Sstevel@tonic-gate oprintf("\n"); 14797c478bd9Sstevel@tonic-gate g_newline = 0; 14807c478bd9Sstevel@tonic-gate } 14817c478bd9Sstevel@tonic-gate 14827c478bd9Sstevel@tonic-gate if (done || g_intr || (g_psc != 0 && g_pslive == 0)) { 14837c478bd9Sstevel@tonic-gate done = 1; 14847c478bd9Sstevel@tonic-gate if (dtrace_stop(g_dtp) == -1) 14857c478bd9Sstevel@tonic-gate dfatal("couldn't stop tracing"); 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) { 14897c478bd9Sstevel@tonic-gate case DTRACE_WORKSTATUS_DONE: 14907c478bd9Sstevel@tonic-gate done = 1; 14917c478bd9Sstevel@tonic-gate break; 14927c478bd9Sstevel@tonic-gate case DTRACE_WORKSTATUS_OKAY: 14937c478bd9Sstevel@tonic-gate break; 14947c478bd9Sstevel@tonic-gate default: 14957c478bd9Sstevel@tonic-gate if (!g_impatient && dtrace_errno(g_dtp) != EINTR) 14967c478bd9Sstevel@tonic-gate dfatal("processing aborted"); 14977c478bd9Sstevel@tonic-gate } 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate if (fflush(g_ofp) == EOF) 15007c478bd9Sstevel@tonic-gate clearerr(g_ofp); 15017c478bd9Sstevel@tonic-gate } while (!done); 15027c478bd9Sstevel@tonic-gate 15037c478bd9Sstevel@tonic-gate oprintf("\n"); 15047c478bd9Sstevel@tonic-gate 15057c478bd9Sstevel@tonic-gate if (!g_impatient) { 15067c478bd9Sstevel@tonic-gate if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 && 15077c478bd9Sstevel@tonic-gate dtrace_errno(g_dtp) != EINTR) 15087c478bd9Sstevel@tonic-gate dfatal("failed to print aggregations"); 15097c478bd9Sstevel@tonic-gate } 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate dtrace_close(g_dtp); 15127c478bd9Sstevel@tonic-gate return (g_status); 15137c478bd9Sstevel@tonic-gate } 1514