10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*1222Smws 230Sstevel@tonic-gate /* 24*1222Smws * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/stat.h> 320Sstevel@tonic-gate #include <sys/wait.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <dtrace.h> 350Sstevel@tonic-gate #include <stdlib.h> 360Sstevel@tonic-gate #include <stdarg.h> 370Sstevel@tonic-gate #include <stdio.h> 380Sstevel@tonic-gate #include <strings.h> 390Sstevel@tonic-gate #include <unistd.h> 400Sstevel@tonic-gate #include <limits.h> 410Sstevel@tonic-gate #include <fcntl.h> 420Sstevel@tonic-gate #include <errno.h> 430Sstevel@tonic-gate #include <signal.h> 440Sstevel@tonic-gate #include <alloca.h> 450Sstevel@tonic-gate #include <libgen.h> 460Sstevel@tonic-gate #include <libproc.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate typedef struct dtrace_cmd { 490Sstevel@tonic-gate void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */ 500Sstevel@tonic-gate dtrace_probespec_t dc_spec; /* probe specifier context */ 510Sstevel@tonic-gate char *dc_arg; /* argument from main argv */ 520Sstevel@tonic-gate const char *dc_name; /* name for error messages */ 530Sstevel@tonic-gate const char *dc_desc; /* desc for error messages */ 540Sstevel@tonic-gate dtrace_prog_t *dc_prog; /* program compiled from arg */ 55191Sahl char dc_ofile[PATH_MAX]; /* derived output file name */ 560Sstevel@tonic-gate } dtrace_cmd_t; 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define DMODE_VERS 0 /* display version information and exit (-V) */ 590Sstevel@tonic-gate #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */ 600Sstevel@tonic-gate #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */ 610Sstevel@tonic-gate #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */ 620Sstevel@tonic-gate #define DMODE_LIST 4 /* compile program and list probes (-l) */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define E_SUCCESS 0 650Sstevel@tonic-gate #define E_ERROR 1 660Sstevel@tonic-gate #define E_USAGE 2 670Sstevel@tonic-gate 680Sstevel@tonic-gate static const char DTRACE_OPTSTR[] = 691017Sbmc "3:6:aAb:Bc:CD:ef:FGHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z"; 700Sstevel@tonic-gate 710Sstevel@tonic-gate static char **g_argv; 720Sstevel@tonic-gate static int g_argc; 730Sstevel@tonic-gate static char **g_objv; 740Sstevel@tonic-gate static int g_objc; 750Sstevel@tonic-gate static dtrace_cmd_t *g_cmdv; 760Sstevel@tonic-gate static int g_cmdc; 770Sstevel@tonic-gate static struct ps_prochandle **g_psv; 780Sstevel@tonic-gate static int g_psc; 790Sstevel@tonic-gate static int g_pslive; 800Sstevel@tonic-gate static char *g_pname; 810Sstevel@tonic-gate static int g_quiet; 820Sstevel@tonic-gate static int g_flowindent; 830Sstevel@tonic-gate static int g_intr; 840Sstevel@tonic-gate static int g_impatient; 850Sstevel@tonic-gate static int g_newline; 860Sstevel@tonic-gate static int g_total; 870Sstevel@tonic-gate static int g_cflags; 880Sstevel@tonic-gate static int g_oflags; 890Sstevel@tonic-gate static int g_verbose; 900Sstevel@tonic-gate static int g_exec = 1; 910Sstevel@tonic-gate static int g_mode = DMODE_EXEC; 920Sstevel@tonic-gate static int g_status = E_SUCCESS; 930Sstevel@tonic-gate static int g_grabanon = 0; 940Sstevel@tonic-gate static const char *g_ofile = NULL; 950Sstevel@tonic-gate static FILE *g_ofp = stdout; 960Sstevel@tonic-gate static dtrace_hdl_t *g_dtp; 970Sstevel@tonic-gate static char *g_etcfile = "/etc/system"; 980Sstevel@tonic-gate static const char *g_etcbegin = "* vvvv Added by DTrace"; 990Sstevel@tonic-gate static const char *g_etcend = "* ^^^^ Added by DTrace"; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static const char *g_etc[] = { 1020Sstevel@tonic-gate "*", 1030Sstevel@tonic-gate "* The following forceload directives were added by dtrace(1M) to allow for", 1040Sstevel@tonic-gate "* tracing during boot. If these directives are removed, the system will", 1050Sstevel@tonic-gate "* continue to function, but tracing will not occur during boot as desired.", 1060Sstevel@tonic-gate "* To remove these directives (and this block comment) automatically, run", 1070Sstevel@tonic-gate "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"", 1080Sstevel@tonic-gate "* chapter of the Solaris Dynamic Tracing Guide for details.", 1090Sstevel@tonic-gate "*", 1100Sstevel@tonic-gate NULL }; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate static int 1130Sstevel@tonic-gate usage(FILE *fp) 1140Sstevel@tonic-gate { 1150Sstevel@tonic-gate static const char predact[] = "[[ predicate ] action ]"; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGHlqSvVwZ] " 1180Sstevel@tonic-gate "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] " 1190Sstevel@tonic-gate "[-o output] [-p pid] [-s script] [-U name]\n\t" 1200Sstevel@tonic-gate "[-x opt[=val]] [-X a|c|s|t]\n\n" 1210Sstevel@tonic-gate "\t[-P provider %s]\n" 1220Sstevel@tonic-gate "\t[-m [ provider: ] module %s]\n" 1230Sstevel@tonic-gate "\t[-f [[ provider: ] module: ] func %s]\n" 1240Sstevel@tonic-gate "\t[-n [[[ provider: ] module: ] func: ] name %s]\n" 1250Sstevel@tonic-gate "\t[-i probe-id %s] [ args ... ]\n\n", g_pname, 1260Sstevel@tonic-gate predact, predact, predact, predact, predact); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n"); 1290Sstevel@tonic-gate (void) fprintf(fp, "\t action -> '{' D-statements '}'\n"); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate (void) fprintf(fp, "\n" 1320Sstevel@tonic-gate "\t-32 generate 32-bit D programs and ELF files\n" 1330Sstevel@tonic-gate "\t-64 generate 64-bit D programs and ELF files\n\n" 1340Sstevel@tonic-gate "\t-a claim anonymous tracing state\n" 1350Sstevel@tonic-gate "\t-A generate driver.conf(4) directives for anonymous tracing\n" 1360Sstevel@tonic-gate "\t-b set trace buffer size\n" 1370Sstevel@tonic-gate "\t-c run specified command and exit upon its completion\n" 1380Sstevel@tonic-gate "\t-C run cpp(1) preprocessor on script files\n" 1390Sstevel@tonic-gate "\t-D define symbol when invoking preprocessor\n" 1400Sstevel@tonic-gate "\t-e exit after compiling request but prior to enabling probes\n" 1410Sstevel@tonic-gate "\t-f enable or list probes matching the specified function name\n" 1420Sstevel@tonic-gate "\t-F coalesce trace output by function\n" 1430Sstevel@tonic-gate "\t-G generate an ELF file containing embedded dtrace program\n" 1440Sstevel@tonic-gate "\t-H print included files when invoking preprocessor\n" 1450Sstevel@tonic-gate "\t-i enable or list probes matching the specified probe id\n" 1460Sstevel@tonic-gate "\t-I add include directory to preprocessor search path\n" 1470Sstevel@tonic-gate "\t-l list probes matching specified criteria\n" 1480Sstevel@tonic-gate "\t-L add library directory to library search path\n" 1490Sstevel@tonic-gate "\t-m enable or list probes matching the specified module name\n" 1500Sstevel@tonic-gate "\t-n enable or list probes matching the specified probe name\n" 1510Sstevel@tonic-gate "\t-o set output file\n" 1520Sstevel@tonic-gate "\t-p grab specified process-ID and cache its symbol tables\n" 1530Sstevel@tonic-gate "\t-P enable or list probes matching the specified provider name\n" 1540Sstevel@tonic-gate "\t-q set quiet mode (only output explicitly traced data)\n" 1550Sstevel@tonic-gate "\t-s enable or list probes according to the specified D script\n" 1560Sstevel@tonic-gate "\t-S print D compiler intermediate code\n" 1570Sstevel@tonic-gate "\t-U undefine symbol when invoking preprocessor\n" 1580Sstevel@tonic-gate "\t-v set verbose mode (report stability attributes, arguments)\n" 1590Sstevel@tonic-gate "\t-V report DTrace API version\n" 1600Sstevel@tonic-gate "\t-w permit destructive actions\n" 1610Sstevel@tonic-gate "\t-x enable or modify compiler and tracing options\n" 1620Sstevel@tonic-gate "\t-X specify ISO C conformance settings for preprocessor\n" 1630Sstevel@tonic-gate "\t-Z permit probe descriptions that match zero probes\n"); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate return (E_USAGE); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate static void 1690Sstevel@tonic-gate verror(const char *fmt, va_list ap) 1700Sstevel@tonic-gate { 1710Sstevel@tonic-gate int error = errno; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 1740Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n') 1770Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(error)); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /*PRINTFLIKE1*/ 1810Sstevel@tonic-gate static void 1820Sstevel@tonic-gate fatal(const char *fmt, ...) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate va_list ap; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate va_start(ap, fmt); 1870Sstevel@tonic-gate verror(fmt, ap); 1880Sstevel@tonic-gate va_end(ap); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate exit(E_ERROR); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate /*PRINTFLIKE1*/ 1940Sstevel@tonic-gate static void 1950Sstevel@tonic-gate dfatal(const char *fmt, ...) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate va_list ap; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate va_start(ap, fmt); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 2020Sstevel@tonic-gate if (fmt != NULL) 2030Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate va_end(ap); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') { 2080Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", 2090Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 2100Sstevel@tonic-gate } else if (fmt == NULL) { 2110Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", 2120Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate exit(E_ERROR); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /*PRINTFLIKE1*/ 2190Sstevel@tonic-gate static void 2200Sstevel@tonic-gate error(const char *fmt, ...) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate va_list ap; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate va_start(ap, fmt); 2250Sstevel@tonic-gate verror(fmt, ap); 2260Sstevel@tonic-gate va_end(ap); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /*PRINTFLIKE1*/ 2300Sstevel@tonic-gate static void 2310Sstevel@tonic-gate notice(const char *fmt, ...) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate va_list ap; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate if (g_quiet) 2360Sstevel@tonic-gate return; /* -q or quiet pragma suppresses notice()s */ 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate va_start(ap, fmt); 2390Sstevel@tonic-gate verror(fmt, ap); 2400Sstevel@tonic-gate va_end(ap); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /*PRINTFLIKE1*/ 2440Sstevel@tonic-gate static void 2450Sstevel@tonic-gate oprintf(const char *fmt, ...) 2460Sstevel@tonic-gate { 2470Sstevel@tonic-gate va_list ap; 2480Sstevel@tonic-gate int n; 2490Sstevel@tonic-gate 2501017Sbmc if (g_ofp == NULL) 2511017Sbmc return; 2521017Sbmc 2530Sstevel@tonic-gate va_start(ap, fmt); 2540Sstevel@tonic-gate n = vfprintf(g_ofp, fmt, ap); 2550Sstevel@tonic-gate va_end(ap); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate if (n < 0) { 2580Sstevel@tonic-gate if (errno != EINTR) { 2590Sstevel@tonic-gate fatal("failed to write to %s", 2600Sstevel@tonic-gate g_ofile ? g_ofile : "<stdout>"); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate clearerr(g_ofp); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate static char ** 2670Sstevel@tonic-gate make_argv(char *s) 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate const char *ws = "\f\n\r\t\v "; 2700Sstevel@tonic-gate char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1)); 2710Sstevel@tonic-gate int argc = 0; 2720Sstevel@tonic-gate char *p = s; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (argv == NULL) 2750Sstevel@tonic-gate return (NULL); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws)) 2780Sstevel@tonic-gate argv[argc++] = p; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate if (argc == 0) 2810Sstevel@tonic-gate argv[argc++] = s; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate argv[argc] = NULL; 2840Sstevel@tonic-gate return (argv); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate static void 2880Sstevel@tonic-gate dof_prune(const char *fname) 2890Sstevel@tonic-gate { 2900Sstevel@tonic-gate struct stat sbuf; 2910Sstevel@tonic-gate size_t sz, i, j, mark, len; 2920Sstevel@tonic-gate char *buf; 2930Sstevel@tonic-gate int msg = 0, fd; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) == -1) { 2960Sstevel@tonic-gate /* 2970Sstevel@tonic-gate * This is okay only if the file doesn't exist at all. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate if (errno != ENOENT) 3000Sstevel@tonic-gate fatal("failed to open %s", fname); 3010Sstevel@tonic-gate return; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate if (fstat(fd, &sbuf) == -1) 3050Sstevel@tonic-gate fatal("failed to fstat %s", fname); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 3080Sstevel@tonic-gate fatal("failed to allocate memory for %s", fname); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (read(fd, buf, sz) != sz) 3110Sstevel@tonic-gate fatal("failed to read %s", fname); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate buf[sz] = '\0'; 3140Sstevel@tonic-gate (void) close(fd); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1) 3170Sstevel@tonic-gate fatal("failed to open %s for writing", fname); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate len = strlen("dof-data-"); 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate for (mark = 0, i = 0; i < sz; i++) { 3220Sstevel@tonic-gate if (strncmp(&buf[i], "dof-data-", len) != 0) 3230Sstevel@tonic-gate continue; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * This is only a match if it's in the 0th column. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate if (i != 0 && buf[i - 1] != '\n') 3290Sstevel@tonic-gate continue; 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if (msg++ == 0) { 3320Sstevel@tonic-gate error("cleaned up old anonymous " 3330Sstevel@tonic-gate "enabling in %s\n", fname); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * We have a match. First write out our data up until now. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate if (i != mark) { 3400Sstevel@tonic-gate if (write(fd, &buf[mark], i - mark) != i - mark) 3410Sstevel@tonic-gate fatal("failed to write to %s", fname); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * Now scan forward until we scan past a newline. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate for (j = i; j < sz && buf[j] != '\n'; j++) 3480Sstevel@tonic-gate continue; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * Reset our mark. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate if ((mark = j + 1) >= sz) 3540Sstevel@tonic-gate break; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate i = j; 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate if (mark < sz) { 3600Sstevel@tonic-gate if (write(fd, &buf[mark], sz - mark) != sz - mark) 3610Sstevel@tonic-gate fatal("failed to write to %s", fname); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate (void) close(fd); 3650Sstevel@tonic-gate free(buf); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate static void 3690Sstevel@tonic-gate etcsystem_prune(void) 3700Sstevel@tonic-gate { 3710Sstevel@tonic-gate struct stat sbuf; 3720Sstevel@tonic-gate size_t sz; 3730Sstevel@tonic-gate char *buf, *start, *end; 3740Sstevel@tonic-gate int fd; 3750Sstevel@tonic-gate char *fname = g_etcfile, *tmpname; 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) == -1) 3780Sstevel@tonic-gate fatal("failed to open %s", fname); 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate if (fstat(fd, &sbuf) == -1) 3810Sstevel@tonic-gate fatal("failed to fstat %s", fname); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL) 3840Sstevel@tonic-gate fatal("failed to allocate memory for %s", fname); 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate if (read(fd, buf, sz) != sz) 3870Sstevel@tonic-gate fatal("failed to read %s", fname); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate buf[sz] = '\0'; 3900Sstevel@tonic-gate (void) close(fd); 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if ((start = strstr(buf, g_etcbegin)) == NULL) 3930Sstevel@tonic-gate goto out; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (strlen(buf) != sz) { 3960Sstevel@tonic-gate fatal("embedded nul byte in %s; manual repair of %s " 3970Sstevel@tonic-gate "required\n", fname, fname); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate if (strstr(start + 1, g_etcbegin) != NULL) { 4010Sstevel@tonic-gate fatal("multiple start sentinels in %s; manual repair of %s " 4020Sstevel@tonic-gate "required\n", fname, fname); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate if ((end = strstr(buf, g_etcend)) == NULL) { 4060Sstevel@tonic-gate fatal("missing end sentinel in %s; manual repair of %s " 4070Sstevel@tonic-gate "required\n", fname, fname); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate if (start > end) { 4110Sstevel@tonic-gate fatal("end sentinel preceeds start sentinel in %s; manual " 4120Sstevel@tonic-gate "repair of %s required\n", fname, fname); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate end += strlen(g_etcend) + 1; 4160Sstevel@tonic-gate bcopy(end, start, strlen(end) + 1); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate tmpname = alloca(sz = strlen(fname) + 80); 4190Sstevel@tonic-gate (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid()); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate if ((fd = open(tmpname, 4220Sstevel@tonic-gate O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1) 4230Sstevel@tonic-gate fatal("failed to create %s", tmpname); 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate if (write(fd, buf, strlen(buf)) < strlen(buf)) { 4260Sstevel@tonic-gate (void) unlink(tmpname); 4270Sstevel@tonic-gate fatal("failed to write to %s", tmpname); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate (void) close(fd); 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) { 4330Sstevel@tonic-gate (void) unlink(tmpname); 4340Sstevel@tonic-gate fatal("failed to chown(2) %s to uid %d, gid %d", tmpname, 4350Sstevel@tonic-gate (int)sbuf.st_uid, (int)sbuf.st_gid); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (rename(tmpname, fname) == -1) 4390Sstevel@tonic-gate fatal("rename of %s to %s failed", tmpname, fname); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate error("cleaned up forceload directives in %s\n", fname); 4420Sstevel@tonic-gate out: 4430Sstevel@tonic-gate free(buf); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate static void 4470Sstevel@tonic-gate etcsystem_add(void) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate const char *mods[20]; 4500Sstevel@tonic-gate int nmods, line; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL) 4530Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate oprintf("%s\n", g_etcbegin); 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate for (line = 0; g_etc[line] != NULL; line++) 4580Sstevel@tonic-gate oprintf("%s\n", g_etc[line]); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate nmods = dtrace_provider_modules(g_dtp, mods, 4610Sstevel@tonic-gate sizeof (mods) / sizeof (char *) - 1); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (nmods >= sizeof (mods) / sizeof (char *)) 4640Sstevel@tonic-gate fatal("unexpectedly large number of modules!"); 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate mods[nmods++] = "dtrace"; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate for (line = 0; line < nmods; line++) 4690Sstevel@tonic-gate oprintf("forceload: drv/%s\n", mods[line]); 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate oprintf("%s\n", g_etcend); 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if (fclose(g_ofp) == EOF) 4740Sstevel@tonic-gate fatal("failed to close output file '%s'", g_ofile); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate error("added forceload directives to %s\n", g_ofile); 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate static void 4800Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p) 4810Sstevel@tonic-gate { 4820Sstevel@tonic-gate char buf[BUFSIZ]; 4830Sstevel@tonic-gate int i; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate oprintf("\n\tProbe Description Attributes\n"); 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 4880Sstevel@tonic-gate dtrace_stability_name(p->dtp_attr.dtat_name)); 4890Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 4900Sstevel@tonic-gate dtrace_stability_name(p->dtp_attr.dtat_data)); 4910Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 4920Sstevel@tonic-gate dtrace_class_name(p->dtp_attr.dtat_class)); 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate oprintf("\n\tArgument Attributes\n"); 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 4970Sstevel@tonic-gate dtrace_stability_name(p->dtp_arga.dtat_name)); 4980Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 4990Sstevel@tonic-gate dtrace_stability_name(p->dtp_arga.dtat_data)); 5000Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 5010Sstevel@tonic-gate dtrace_class_name(p->dtp_arga.dtat_class)); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate oprintf("\n\tArgument Types\n"); 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate for (i = 0; i < p->dtp_argc; i++) { 5060Sstevel@tonic-gate if (ctf_type_name(p->dtp_argv[i].dtt_ctfp, 5070Sstevel@tonic-gate p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL) 5080Sstevel@tonic-gate (void) strlcpy(buf, "(unknown)", sizeof (buf)); 5090Sstevel@tonic-gate oprintf("\t\targs[%d]: %s\n", i, buf); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate if (p->dtp_argc == 0) 5130Sstevel@tonic-gate oprintf("\t\tNone\n"); 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate oprintf("\n"); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /*ARGSUSED*/ 5190Sstevel@tonic-gate static int 5200Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 5210Sstevel@tonic-gate dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 5240Sstevel@tonic-gate dtrace_probedesc_t *pdp = &edp->dted_probe; 5250Sstevel@tonic-gate dtrace_probeinfo_t p; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate if (edp == *last) 5280Sstevel@tonic-gate return (0); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate oprintf("\n%s:%s:%s:%s\n", 5310Sstevel@tonic-gate pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (dtrace_probe_info(dtp, pdp, &p) == 0) 5340Sstevel@tonic-gate print_probe_info(&p); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate *last = edp; 5370Sstevel@tonic-gate return (0); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * Execute the specified program by enabling the corresponding instrumentation. 5420Sstevel@tonic-gate * If -e has been specified, we get the program info but do not enable it. If 5430Sstevel@tonic-gate * -v has been specified, we print a stability report for the program. 5440Sstevel@tonic-gate */ 5450Sstevel@tonic-gate static void 5460Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp) 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate dtrace_ecbdesc_t *last = NULL; 5490Sstevel@tonic-gate dtrace_proginfo_t dpi; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate if (!g_exec) { 5520Sstevel@tonic-gate dtrace_program_info(g_dtp, dcp->dc_prog, &dpi); 5530Sstevel@tonic-gate } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) { 5540Sstevel@tonic-gate dfatal("failed to enable '%s'", dcp->dc_name); 5550Sstevel@tonic-gate } else { 5560Sstevel@tonic-gate notice("%s '%s' matched %u probe%s\n", 5570Sstevel@tonic-gate dcp->dc_desc, dcp->dc_name, 5580Sstevel@tonic-gate dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s"); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate if (g_verbose) { 5620Sstevel@tonic-gate oprintf("\nStability attributes for %s %s:\n", 5630Sstevel@tonic-gate dcp->dc_desc, dcp->dc_name); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate oprintf("\n\tMinimum Probe Description Attributes\n"); 5660Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 5670Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_descattr.dtat_name)); 5680Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 5690Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_descattr.dtat_data)); 5700Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 5710Sstevel@tonic-gate dtrace_class_name(dpi.dpi_descattr.dtat_class)); 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate oprintf("\n\tMinimum Statement Attributes\n"); 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate oprintf("\t\tIdentifier Names: %s\n", 5760Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_stmtattr.dtat_name)); 5770Sstevel@tonic-gate oprintf("\t\tData Semantics: %s\n", 5780Sstevel@tonic-gate dtrace_stability_name(dpi.dpi_stmtattr.dtat_data)); 5790Sstevel@tonic-gate oprintf("\t\tDependency Class: %s\n", 5800Sstevel@tonic-gate dtrace_class_name(dpi.dpi_stmtattr.dtat_class)); 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (!g_exec) { 5830Sstevel@tonic-gate (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 5840Sstevel@tonic-gate (dtrace_stmt_f *)info_stmt, &last); 5850Sstevel@tonic-gate } else 5860Sstevel@tonic-gate oprintf("\n"); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate g_total += dpi.dpi_matches; 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * Print out the specified DOF buffer as a set of ASCII bytes appropriate for 5940Sstevel@tonic-gate * storing in a driver.conf(4) file associated with the dtrace driver. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate static void 5970Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n) 5980Sstevel@tonic-gate { 5990Sstevel@tonic-gate const uchar_t *p, *q; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (dof == NULL) 6020Sstevel@tonic-gate dfatal("failed to create DOF image for '%s'", dcp->dc_name); 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate p = (uchar_t *)dof; 6050Sstevel@tonic-gate q = p + dof->dofh_loadsz; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate oprintf("dof-data-%d=0x%x", n, *p++); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate while (p < q) 6100Sstevel@tonic-gate oprintf(",0x%x", *p++); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate oprintf(";\n"); 6130Sstevel@tonic-gate dtrace_dof_destroy(g_dtp, dof); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Link the specified D program in DOF form into an ELF file for use in either 6180Sstevel@tonic-gate * helpers, userland provider definitions, or both. If -o was specified, that 6190Sstevel@tonic-gate * path is used as the output file name. If -o wasn't specified and the input 6200Sstevel@tonic-gate * program is from a script whose name is %.d, use basename(%.o) as the output 6210Sstevel@tonic-gate * file name. Otherwise we use "d.out" as the default output file name. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate static void 624191Sahl link_prog(dtrace_cmd_t *dcp) 6250Sstevel@tonic-gate { 626191Sahl char *p; 6270Sstevel@tonic-gate 628191Sahl if (g_cmdc == 1 && g_ofile != NULL) { 629191Sahl (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile)); 6300Sstevel@tonic-gate } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL && 6310Sstevel@tonic-gate strcmp(p, ".d") == 0) { 6320Sstevel@tonic-gate p[0] = '\0'; /* strip .d suffix */ 633191Sahl (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 6340Sstevel@tonic-gate "%s.o", basename(dcp->dc_arg)); 6350Sstevel@tonic-gate } else { 636191Sahl (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile), 637191Sahl g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv)); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES, 641191Sahl dcp->dc_ofile, g_objc - 1, g_objv + 1) != 0) 6420Sstevel@tonic-gate dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /*ARGSUSED*/ 6460Sstevel@tonic-gate static int 6470Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg) 6480Sstevel@tonic-gate { 6490Sstevel@tonic-gate dtrace_probeinfo_t p; 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id, 6520Sstevel@tonic-gate pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0) 6550Sstevel@tonic-gate print_probe_info(&p); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate return (0); 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /*ARGSUSED*/ 6610Sstevel@tonic-gate static int 6620Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, 6630Sstevel@tonic-gate dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last) 6640Sstevel@tonic-gate { 6650Sstevel@tonic-gate dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (edp == *last) 6680Sstevel@tonic-gate return (0); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) { 6710Sstevel@tonic-gate error("failed to match %s:%s:%s:%s: %s\n", 6720Sstevel@tonic-gate edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod, 6730Sstevel@tonic-gate edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name, 6740Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp))); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate *last = edp; 6780Sstevel@tonic-gate return (0); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * List the probes corresponding to the specified program by iterating over 6830Sstevel@tonic-gate * each statement and then matching probes to the statement probe descriptions. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate static void 6860Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp) 6870Sstevel@tonic-gate { 6880Sstevel@tonic-gate dtrace_ecbdesc_t *last = NULL; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog, 6910Sstevel@tonic-gate (dtrace_stmt_f *)list_stmt, &last); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate static void 6950Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate char *arg0; 6980Sstevel@tonic-gate FILE *fp; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate if ((fp = fopen(dcp->dc_arg, "r")) == NULL) 7010Sstevel@tonic-gate fatal("failed to open %s", dcp->dc_arg); 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate arg0 = g_argv[0]; 7040Sstevel@tonic-gate g_argv[0] = dcp->dc_arg; 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp, 7070Sstevel@tonic-gate g_cflags, g_argc, g_argv)) == NULL) 7080Sstevel@tonic-gate dfatal("failed to compile script %s", dcp->dc_arg); 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate g_argv[0] = arg0; 7110Sstevel@tonic-gate (void) fclose(fp); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate dcp->dc_desc = "script"; 7140Sstevel@tonic-gate dcp->dc_name = dcp->dc_arg; 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate static void 7180Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate char *p; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg, 7230Sstevel@tonic-gate dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL) 7240Sstevel@tonic-gate dfatal("invalid probe specifier %s", dcp->dc_arg); 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL) 7270Sstevel@tonic-gate *p = '\0'; /* crop name for reporting */ 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate dcp->dc_desc = "description"; 7300Sstevel@tonic-gate dcp->dc_name = dcp->dc_arg; 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /*ARGSUSED*/ 7340Sstevel@tonic-gate static void 7350Sstevel@tonic-gate prochandler(struct ps_prochandle *P, void *arg) 7360Sstevel@tonic-gate { 7370Sstevel@tonic-gate const psinfo_t *prp = Ppsinfo(P); 7380Sstevel@tonic-gate int pid = Pstatus(P)->pr_pid; 7390Sstevel@tonic-gate char name[SIG2STR_MAX]; 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate switch (Pstate(P)) { 7420Sstevel@tonic-gate case PS_UNDEAD: 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Ideally we would like to always report pr_wstat here, but it 7450Sstevel@tonic-gate * isn't possible given current /proc semantics. If we grabbed 7460Sstevel@tonic-gate * the process, Ppsinfo() will either fail or return a zeroed 7470Sstevel@tonic-gate * psinfo_t depending on how far the parent is in reaping it. 7480Sstevel@tonic-gate * When /proc provides a stable pr_wstat in the status file, 7490Sstevel@tonic-gate * this code can be improved by examining this new pr_wstat. 7500Sstevel@tonic-gate */ 7510Sstevel@tonic-gate if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) { 7520Sstevel@tonic-gate notice("pid %d terminated by %s\n", pid, 7530Sstevel@tonic-gate proc_signame(WTERMSIG(prp->pr_wstat), 7540Sstevel@tonic-gate name, sizeof (name))); 7550Sstevel@tonic-gate } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) { 7560Sstevel@tonic-gate notice("pid %d exited with status %d\n", 7570Sstevel@tonic-gate pid, WEXITSTATUS(prp->pr_wstat)); 7580Sstevel@tonic-gate } else { 7590Sstevel@tonic-gate notice("pid %d has exited\n", pid); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate g_pslive--; 7620Sstevel@tonic-gate break; 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate case PS_LOST: 7650Sstevel@tonic-gate notice("pid %d exec'd a set-id or unobservable program\n", pid); 7660Sstevel@tonic-gate g_pslive--; 7670Sstevel@tonic-gate break; 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /*ARGSUSED*/ 7720Sstevel@tonic-gate static int 773457Sbmc errhandler(const dtrace_errdata_t *data, void *arg) 7740Sstevel@tonic-gate { 7750Sstevel@tonic-gate error(data->dteda_msg); 7760Sstevel@tonic-gate return (DTRACE_HANDLE_OK); 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /*ARGSUSED*/ 7800Sstevel@tonic-gate static int 781457Sbmc drophandler(const dtrace_dropdata_t *data, void *arg) 7820Sstevel@tonic-gate { 7830Sstevel@tonic-gate error(data->dtdda_msg); 7840Sstevel@tonic-gate return (DTRACE_HANDLE_OK); 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate /*ARGSUSED*/ 7880Sstevel@tonic-gate static int 789457Sbmc setopthandler(const dtrace_setoptdata_t *data, void *arg) 790457Sbmc { 791457Sbmc if (strcmp(data->dtsda_option, "quiet") == 0) 792457Sbmc g_quiet = data->dtsda_newval != DTRACEOPT_UNSET; 793457Sbmc 794457Sbmc if (strcmp(data->dtsda_option, "flowindent") == 0) 795457Sbmc g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET; 796457Sbmc 797457Sbmc return (DTRACE_HANDLE_OK); 798457Sbmc } 799457Sbmc 8001017Sbmc #define BUFDUMPHDR(hdr) \ 8011017Sbmc (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : ""); 8021017Sbmc 8031017Sbmc #define BUFDUMPSTR(ptr, field) \ 8041017Sbmc (void) printf("%s: %20s => ", g_pname, #field); \ 8051017Sbmc if ((ptr)->field != NULL) { \ 8061017Sbmc const char *c = (ptr)->field; \ 8071017Sbmc (void) printf("\""); \ 8081017Sbmc do { \ 8091017Sbmc if (*c == '\n') { \ 8101017Sbmc (void) printf("\\n"); \ 8111017Sbmc continue; \ 8121017Sbmc } \ 8131017Sbmc \ 8141017Sbmc (void) printf("%c", *c); \ 8151017Sbmc } while (*c++ != '\0'); \ 8161017Sbmc (void) printf("\"\n"); \ 8171017Sbmc } else { \ 8181017Sbmc (void) printf("<NULL>\n"); \ 8191017Sbmc } 8201017Sbmc 8211017Sbmc #define BUFDUMPASSTR(ptr, field, str) \ 8221017Sbmc (void) printf("%s: %20s => %s\n", g_pname, #field, str); 8231017Sbmc 8241017Sbmc #define BUFDUMP(ptr, field) \ 8251017Sbmc (void) printf("%s: %20s => %lld\n", g_pname, #field, \ 8261017Sbmc (long long)(ptr)->field); 8271017Sbmc 8281017Sbmc #define BUFDUMPPTR(ptr, field) \ 8291017Sbmc (void) printf("%s: %20s => %s\n", g_pname, #field, \ 8301017Sbmc (ptr)->field != NULL ? "<non-NULL>" : "<NULL>"); 8311017Sbmc 8321017Sbmc /*ARGSUSED*/ 8331017Sbmc static int 8341017Sbmc bufhandler(const dtrace_bufdata_t *bufdata, void *arg) 8351017Sbmc { 8361017Sbmc const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata; 8371017Sbmc const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc; 8381017Sbmc const dtrace_probedesc_t *pd; 8391017Sbmc uint32_t flags = bufdata->dtbda_flags; 8401017Sbmc char buf[512], *c = buf, *end = c + sizeof (buf); 8411017Sbmc int i, printed; 8421017Sbmc 8431017Sbmc struct { 8441017Sbmc const char *name; 8451017Sbmc uint32_t value; 8461017Sbmc } flagnames[] = { 8471017Sbmc { "AGGVAL", DTRACE_BUFDATA_AGGVAL }, 8481017Sbmc { "AGGKEY", DTRACE_BUFDATA_AGGKEY }, 8491017Sbmc { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT }, 8501017Sbmc { "AGGLAST", DTRACE_BUFDATA_AGGLAST }, 8511017Sbmc { "???", UINT32_MAX }, 8521017Sbmc { NULL } 8531017Sbmc }; 8541017Sbmc 8551017Sbmc if (bufdata->dtbda_probe != NULL) { 8561017Sbmc pd = bufdata->dtbda_probe->dtpda_pdesc; 8571017Sbmc } else if (agg != NULL) { 8581017Sbmc pd = agg->dtada_pdesc; 8591017Sbmc } else { 8601017Sbmc pd = NULL; 8611017Sbmc } 8621017Sbmc 8631017Sbmc BUFDUMPHDR(">>> Called buffer handler"); 8641017Sbmc BUFDUMPHDR(""); 8651017Sbmc 8661017Sbmc BUFDUMPHDR(" dtrace_bufdata"); 8671017Sbmc BUFDUMPSTR(bufdata, dtbda_buffered); 8681017Sbmc BUFDUMPPTR(bufdata, dtbda_probe); 8691017Sbmc BUFDUMPPTR(bufdata, dtbda_aggdata); 8701017Sbmc BUFDUMPPTR(bufdata, dtbda_recdesc); 8711017Sbmc 8721017Sbmc (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags); 8731017Sbmc c += strlen(c); 8741017Sbmc 8751017Sbmc for (i = 0, printed = 0; flagnames[i].name != NULL; i++) { 8761017Sbmc if (!(flags & flagnames[i].value)) 8771017Sbmc continue; 8781017Sbmc 8791017Sbmc (void) snprintf(c, end - c, 8801017Sbmc "%s%s", printed++ ? " | " : "(", flagnames[i].name); 8811017Sbmc c += strlen(c); 8821017Sbmc flags &= ~flagnames[i].value; 8831017Sbmc } 8841017Sbmc 8851017Sbmc if (printed) 8861017Sbmc (void) snprintf(c, end - c, ")"); 8871017Sbmc 8881017Sbmc BUFDUMPASSTR(bufdata, dtbda_flags, buf); 8891017Sbmc BUFDUMPHDR(""); 8901017Sbmc 8911017Sbmc if (pd != NULL) { 8921017Sbmc BUFDUMPHDR(" dtrace_probedesc"); 8931017Sbmc BUFDUMPSTR(pd, dtpd_provider); 8941017Sbmc BUFDUMPSTR(pd, dtpd_mod); 8951017Sbmc BUFDUMPSTR(pd, dtpd_func); 8961017Sbmc BUFDUMPSTR(pd, dtpd_name); 8971017Sbmc BUFDUMPHDR(""); 8981017Sbmc } 8991017Sbmc 9001017Sbmc if (rec != NULL) { 9011017Sbmc BUFDUMPHDR(" dtrace_recdesc"); 9021017Sbmc BUFDUMP(rec, dtrd_action); 9031017Sbmc BUFDUMP(rec, dtrd_size); 9041028Sbmc 9051028Sbmc if (agg != NULL) { 9061028Sbmc uint8_t *data; 9071028Sbmc int lim = rec->dtrd_size; 9081028Sbmc 9091028Sbmc (void) sprintf(buf, "%d (data: ", rec->dtrd_offset); 9101028Sbmc c = buf + strlen(buf); 9111028Sbmc 9121028Sbmc if (lim > sizeof (uint64_t)) 9131028Sbmc lim = sizeof (uint64_t); 9141028Sbmc 9151028Sbmc data = (uint8_t *)agg->dtada_data + rec->dtrd_offset; 9161028Sbmc 9171028Sbmc for (i = 0; i < lim; i++) { 9181028Sbmc (void) snprintf(c, end - c, "%s%02x", 9191028Sbmc i == 0 ? "" : " ", *data++); 9201028Sbmc c += strlen(c); 9211028Sbmc } 9221028Sbmc 9231028Sbmc (void) snprintf(c, end - c, 9241028Sbmc "%s)", lim < rec->dtrd_size ? " ..." : ""); 9251028Sbmc BUFDUMPASSTR(rec, dtrd_offset, buf); 9261028Sbmc } else { 9271028Sbmc BUFDUMP(rec, dtrd_offset); 9281028Sbmc } 9291028Sbmc 9301017Sbmc BUFDUMPHDR(""); 9311017Sbmc } 9321017Sbmc 9331017Sbmc if (agg != NULL) { 9341017Sbmc dtrace_aggdesc_t *desc = agg->dtada_desc; 9351017Sbmc 9361017Sbmc BUFDUMPHDR(" dtrace_aggdesc"); 9371017Sbmc BUFDUMPSTR(desc, dtagd_name); 9381017Sbmc BUFDUMP(desc, dtagd_varid); 9391017Sbmc BUFDUMP(desc, dtagd_id); 9401017Sbmc BUFDUMP(desc, dtagd_nrecs); 9411017Sbmc BUFDUMPHDR(""); 9421017Sbmc } 9431017Sbmc 9441017Sbmc return (DTRACE_HANDLE_OK); 9451017Sbmc } 9461017Sbmc 947457Sbmc /*ARGSUSED*/ 948457Sbmc static int 9490Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg) 9500Sstevel@tonic-gate { 9510Sstevel@tonic-gate dtrace_actkind_t act; 9520Sstevel@tonic-gate uintptr_t addr; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (rec == NULL) { 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * We have processed the final record; output the newline if 9570Sstevel@tonic-gate * we're not in quiet mode. 9580Sstevel@tonic-gate */ 9590Sstevel@tonic-gate if (!g_quiet) 9600Sstevel@tonic-gate oprintf("\n"); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate act = rec->dtrd_action; 9660Sstevel@tonic-gate addr = (uintptr_t)data->dtpda_data; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate if (act == DTRACEACT_EXIT) { 9690Sstevel@tonic-gate g_status = *((uint32_t *)addr); 9700Sstevel@tonic-gate return (DTRACE_CONSUME_NEXT); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate return (DTRACE_CONSUME_THIS); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate /*ARGSUSED*/ 9770Sstevel@tonic-gate static int 9780Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg) 9790Sstevel@tonic-gate { 9800Sstevel@tonic-gate dtrace_probedesc_t *pd = data->dtpda_pdesc; 9810Sstevel@tonic-gate processorid_t cpu = data->dtpda_cpu; 9820Sstevel@tonic-gate static int heading; 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate if (g_impatient) { 9850Sstevel@tonic-gate g_newline = 0; 9860Sstevel@tonic-gate return (DTRACE_CONSUME_ABORT); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate if (heading == 0) { 9900Sstevel@tonic-gate if (!g_flowindent) { 9910Sstevel@tonic-gate if (!g_quiet) { 9920Sstevel@tonic-gate oprintf("%3s %6s %32s\n", 9930Sstevel@tonic-gate "CPU", "ID", "FUNCTION:NAME"); 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate } else { 9960Sstevel@tonic-gate oprintf("%3s %-41s\n", "CPU", "FUNCTION"); 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate heading = 1; 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate if (!g_flowindent) { 10020Sstevel@tonic-gate if (!g_quiet) { 10030Sstevel@tonic-gate char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2]; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s:%s", 10060Sstevel@tonic-gate pd->dtpd_func, pd->dtpd_name); 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate } else { 10110Sstevel@tonic-gate int indent = data->dtpda_indent; 10120Sstevel@tonic-gate char *name; 10130Sstevel@tonic-gate size_t len; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (data->dtpda_flow == DTRACEFLOW_NONE) { 10160Sstevel@tonic-gate len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5; 10170Sstevel@tonic-gate name = alloca(len); 10180Sstevel@tonic-gate (void) snprintf(name, len, "%*s%s%s:%s", indent, "", 10190Sstevel@tonic-gate data->dtpda_prefix, pd->dtpd_func, 10200Sstevel@tonic-gate pd->dtpd_name); 10210Sstevel@tonic-gate } else { 10220Sstevel@tonic-gate len = indent + DTRACE_FUNCNAMELEN + 5; 10230Sstevel@tonic-gate name = alloca(len); 10240Sstevel@tonic-gate (void) snprintf(name, len, "%*s%s%s", indent, "", 10250Sstevel@tonic-gate data->dtpda_prefix, pd->dtpd_func); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate oprintf("%3d %-41s ", cpu, name); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate return (DTRACE_CONSUME_THIS); 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate static void 10350Sstevel@tonic-gate go(void) 10360Sstevel@tonic-gate { 10370Sstevel@tonic-gate int i; 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate struct { 10400Sstevel@tonic-gate char *name; 10410Sstevel@tonic-gate char *optname; 10420Sstevel@tonic-gate dtrace_optval_t val; 10430Sstevel@tonic-gate } bufs[] = { 10440Sstevel@tonic-gate { "buffer size", "bufsize" }, 10450Sstevel@tonic-gate { "aggregation size", "aggsize" }, 10460Sstevel@tonic-gate { "speculation size", "specsize" }, 10470Sstevel@tonic-gate { "dynamic variable size", "dynvarsize" }, 10480Sstevel@tonic-gate { NULL } 10490Sstevel@tonic-gate }, rates[] = { 10500Sstevel@tonic-gate { "cleaning rate", "cleanrate" }, 10510Sstevel@tonic-gate { "status rate", "statusrate" }, 10520Sstevel@tonic-gate { NULL } 10530Sstevel@tonic-gate }; 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate for (i = 0; bufs[i].name != NULL; i++) { 10560Sstevel@tonic-gate if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1) 10570Sstevel@tonic-gate fatal("couldn't get option %s", bufs[i].optname); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate for (i = 0; rates[i].name != NULL; i++) { 10610Sstevel@tonic-gate if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1) 10620Sstevel@tonic-gate fatal("couldn't get option %s", rates[i].optname); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate if (dtrace_go(g_dtp) == -1) 10660Sstevel@tonic-gate dfatal("could not enable tracing"); 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate for (i = 0; bufs[i].name != NULL; i++) { 10690Sstevel@tonic-gate dtrace_optval_t j = 0, mul = 10; 10700Sstevel@tonic-gate dtrace_optval_t nsize; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate if (bufs[i].val == DTRACEOPT_UNSET) 10730Sstevel@tonic-gate continue; 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize); 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate if (nsize == DTRACEOPT_UNSET || nsize == 0) 10780Sstevel@tonic-gate continue; 10790Sstevel@tonic-gate 10800Sstevel@tonic-gate if (nsize >= bufs[i].val - sizeof (uint64_t)) 10810Sstevel@tonic-gate continue; 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10) 10840Sstevel@tonic-gate continue; 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) { 10870Sstevel@tonic-gate error("%s lowered to %lld%c\n", bufs[i].name, 10880Sstevel@tonic-gate (long long)nsize >> (mul - 10), " kmgtpe"[j]); 10890Sstevel@tonic-gate } else { 10900Sstevel@tonic-gate error("%s lowered to %lld bytes\n", bufs[i].name, 10910Sstevel@tonic-gate (long long)nsize); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate for (i = 0; rates[i].name != NULL; i++) { 10960Sstevel@tonic-gate dtrace_optval_t nval; 10970Sstevel@tonic-gate char *dir; 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate if (rates[i].val == DTRACEOPT_UNSET) 11000Sstevel@tonic-gate continue; 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, rates[i].optname, &nval); 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate if (nval == DTRACEOPT_UNSET || nval == 0) 11050Sstevel@tonic-gate continue; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate if (rates[i].val == nval) 11080Sstevel@tonic-gate continue; 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate dir = nval > rates[i].val ? "reduced" : "increased"; 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate if (nval <= NANOSEC && (NANOSEC % nval) == 0) { 11130Sstevel@tonic-gate error("%s %s to %lld hz\n", rates[i].name, dir, 11140Sstevel@tonic-gate (long long)NANOSEC / (long long)nval); 11150Sstevel@tonic-gate continue; 11160Sstevel@tonic-gate } 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate if ((nval % NANOSEC) == 0) { 11190Sstevel@tonic-gate error("%s %s to once every %lld seconds\n", 11200Sstevel@tonic-gate rates[i].name, dir, 11210Sstevel@tonic-gate (long long)nval / (long long)NANOSEC); 11220Sstevel@tonic-gate continue; 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate error("%s %s to once every %lld nanoseconds\n", 11260Sstevel@tonic-gate rates[i].name, dir, (long long)nval); 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate /*ARGSUSED*/ 11310Sstevel@tonic-gate static void 11320Sstevel@tonic-gate intr(int signo) 11330Sstevel@tonic-gate { 11340Sstevel@tonic-gate if (!g_intr) 11350Sstevel@tonic-gate g_newline = 1; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate if (g_intr++) 11380Sstevel@tonic-gate g_impatient = 1; 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate int 11420Sstevel@tonic-gate main(int argc, char *argv[]) 11430Sstevel@tonic-gate { 11440Sstevel@tonic-gate dtrace_bufdesc_t buf; 1145*1222Smws struct sigaction act, oact; 11460Sstevel@tonic-gate dtrace_status_t status[2]; 11470Sstevel@tonic-gate dtrace_optval_t opt; 11480Sstevel@tonic-gate dtrace_cmd_t *dcp; 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate int done = 0, mode = 0; 11510Sstevel@tonic-gate int err, i; 11520Sstevel@tonic-gate char c, *p, **v; 11530Sstevel@tonic-gate struct ps_prochandle *P; 11540Sstevel@tonic-gate pid_t pid; 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate g_pname = basename(argv[0]); 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate if (argc == 1) 11590Sstevel@tonic-gate return (usage(stderr)); 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate if ((g_argv = malloc(sizeof (char *) * argc)) == NULL || 11620Sstevel@tonic-gate (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL || 11630Sstevel@tonic-gate (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL) 11640Sstevel@tonic-gate fatal("failed to allocate memory for arguments"); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */ 11670Sstevel@tonic-gate argv[0] = g_pname; /* rewrite argv[0] for getopt errors */ 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate bzero(status, sizeof (status)); 11700Sstevel@tonic-gate bzero(&buf, sizeof (buf)); 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Make an initial pass through argv[] processing any arguments that 11740Sstevel@tonic-gate * affect our behavior mode (g_mode) and flags used for dtrace_open(). 11750Sstevel@tonic-gate * We also accumulate arguments that are not affiliated with getopt 11760Sstevel@tonic-gate * options into g_argv[], and abort if any invalid options are found. 11770Sstevel@tonic-gate */ 11780Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 11790Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 11800Sstevel@tonic-gate switch (c) { 11810Sstevel@tonic-gate case '3': 11820Sstevel@tonic-gate if (strcmp(optarg, "2") != 0) { 11830Sstevel@tonic-gate (void) fprintf(stderr, 11840Sstevel@tonic-gate "%s: illegal option -- 3%s\n", 11850Sstevel@tonic-gate argv[0], optarg); 11860Sstevel@tonic-gate return (usage(stderr)); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate g_oflags &= ~DTRACE_O_LP64; 11890Sstevel@tonic-gate g_oflags |= DTRACE_O_ILP32; 11900Sstevel@tonic-gate break; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate case '6': 11930Sstevel@tonic-gate if (strcmp(optarg, "4") != 0) { 11940Sstevel@tonic-gate (void) fprintf(stderr, 11950Sstevel@tonic-gate "%s: illegal option -- 6%s\n", 11960Sstevel@tonic-gate argv[0], optarg); 11970Sstevel@tonic-gate return (usage(stderr)); 11980Sstevel@tonic-gate } 11990Sstevel@tonic-gate g_oflags &= ~DTRACE_O_ILP32; 12000Sstevel@tonic-gate g_oflags |= DTRACE_O_LP64; 12010Sstevel@tonic-gate break; 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate case 'a': 12040Sstevel@tonic-gate g_grabanon++; /* also checked in pass 2 below */ 12050Sstevel@tonic-gate break; 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate case 'A': 12080Sstevel@tonic-gate g_mode = DMODE_ANON; 12090Sstevel@tonic-gate g_exec = 0; 12100Sstevel@tonic-gate mode++; 12110Sstevel@tonic-gate break; 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate case 'e': 12140Sstevel@tonic-gate g_exec = 0; 12150Sstevel@tonic-gate done = 1; 12160Sstevel@tonic-gate break; 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate case 'G': 12190Sstevel@tonic-gate g_mode = DMODE_LINK; 12200Sstevel@tonic-gate g_oflags |= DTRACE_O_NODEV; 12210Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */ 12220Sstevel@tonic-gate g_exec = 0; 12230Sstevel@tonic-gate mode++; 12240Sstevel@tonic-gate break; 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate case 'l': 12270Sstevel@tonic-gate g_mode = DMODE_LIST; 12280Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */ 12290Sstevel@tonic-gate mode++; 12300Sstevel@tonic-gate break; 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate case 'V': 12330Sstevel@tonic-gate g_mode = DMODE_VERS; 12340Sstevel@tonic-gate mode++; 12350Sstevel@tonic-gate break; 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate default: 12380Sstevel@tonic-gate if (strchr(DTRACE_OPTSTR, c) == NULL) 12390Sstevel@tonic-gate return (usage(stderr)); 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate if (optind < argc) 12440Sstevel@tonic-gate g_argv[g_argc++] = argv[optind]; 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate if (mode > 1) { 12480Sstevel@tonic-gate (void) fprintf(stderr, "%s: only one of the [-AGlV] options " 12490Sstevel@tonic-gate "can be specified at a time\n", g_pname); 12500Sstevel@tonic-gate return (E_USAGE); 12510Sstevel@tonic-gate } 12520Sstevel@tonic-gate 12530Sstevel@tonic-gate if (g_mode == DMODE_VERS) 12540Sstevel@tonic-gate return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0); 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate /* 12570Sstevel@tonic-gate * Open libdtrace. If we are not actually going to be enabling any 12580Sstevel@tonic-gate * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV. 12590Sstevel@tonic-gate */ 12600Sstevel@tonic-gate while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) { 12610Sstevel@tonic-gate if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) { 12620Sstevel@tonic-gate g_oflags |= DTRACE_O_NODEV; 12630Sstevel@tonic-gate continue; 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate fatal("failed to initialize dtrace: %s\n", 12670Sstevel@tonic-gate dtrace_errmsg(NULL, err)); 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "bufsize", "4m"); 12710Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "aggsize", "4m"); 12720Sstevel@tonic-gate 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * If -G is specified, enable -xlink=dynamic and -xunodefs to permit 12750Sstevel@tonic-gate * references to undefined symbols to remain as unresolved relocations. 12760Sstevel@tonic-gate * If -A is specified, enable -xlink=primary to permit static linking 12770Sstevel@tonic-gate * only to kernel symbols that are defined in a primary kernel module. 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate if (g_mode == DMODE_LINK) { 12800Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "linkmode", "dynamic"); 12810Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "unodefs", NULL); 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate g_objc = g_argc; 12840Sstevel@tonic-gate g_objv = g_argv; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate /* 12870Sstevel@tonic-gate * We still use g_argv[0], the name of the executable. 12880Sstevel@tonic-gate */ 12890Sstevel@tonic-gate g_argc = 1; 12900Sstevel@tonic-gate } else if (g_mode == DMODE_ANON) 12910Sstevel@tonic-gate (void) dtrace_setopt(g_dtp, "linkmode", "primary"); 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate /* 12940Sstevel@tonic-gate * Now that we have libdtrace open, make a second pass through argv[] 12950Sstevel@tonic-gate * to perform any dtrace_setopt() calls and change any compiler flags. 12960Sstevel@tonic-gate * We also accumulate any program specifications into our g_cmdv[] at 12970Sstevel@tonic-gate * this time; these will compiled as part of the fourth processing pass. 12980Sstevel@tonic-gate */ 12990Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 13000Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 13010Sstevel@tonic-gate switch (c) { 13020Sstevel@tonic-gate case 'a': 13030Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "grabanon", 0) != 0) 13040Sstevel@tonic-gate dfatal("failed to set -a"); 13050Sstevel@tonic-gate break; 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate case 'b': 13080Sstevel@tonic-gate if (dtrace_setopt(g_dtp, 13090Sstevel@tonic-gate "bufsize", optarg) != 0) 13100Sstevel@tonic-gate dfatal("failed to set -b %s", optarg); 13110Sstevel@tonic-gate break; 13120Sstevel@tonic-gate 13131017Sbmc case 'B': 13141017Sbmc g_ofp = NULL; 13151017Sbmc break; 13161017Sbmc 13170Sstevel@tonic-gate case 'C': 13180Sstevel@tonic-gate g_cflags |= DTRACE_C_CPP; 13190Sstevel@tonic-gate break; 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate case 'D': 13220Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "define", optarg) != 0) 13230Sstevel@tonic-gate dfatal("failed to set -D %s", optarg); 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate case 'f': 13270Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13280Sstevel@tonic-gate dcp->dc_func = compile_str; 13290Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_FUNC; 13300Sstevel@tonic-gate dcp->dc_arg = optarg; 13310Sstevel@tonic-gate break; 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate case 'F': 13340Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "flowindent", 0) != 0) 13350Sstevel@tonic-gate dfatal("failed to set -F"); 13360Sstevel@tonic-gate break; 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate case 'H': 13390Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0) 13400Sstevel@tonic-gate dfatal("failed to set -H"); 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate case 'i': 13440Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13450Sstevel@tonic-gate dcp->dc_func = compile_str; 13460Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NAME; 13470Sstevel@tonic-gate dcp->dc_arg = optarg; 13480Sstevel@tonic-gate break; 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate case 'I': 13510Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "incdir", optarg) != 0) 13520Sstevel@tonic-gate dfatal("failed to set -I %s", optarg); 13530Sstevel@tonic-gate break; 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate case 'L': 13560Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "libdir", optarg) != 0) 13570Sstevel@tonic-gate dfatal("failed to set -L %s", optarg); 13580Sstevel@tonic-gate break; 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate case 'm': 13610Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13620Sstevel@tonic-gate dcp->dc_func = compile_str; 13630Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_MOD; 13640Sstevel@tonic-gate dcp->dc_arg = optarg; 13650Sstevel@tonic-gate break; 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate case 'n': 13680Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13690Sstevel@tonic-gate dcp->dc_func = compile_str; 13700Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NAME; 13710Sstevel@tonic-gate dcp->dc_arg = optarg; 13720Sstevel@tonic-gate break; 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate case 'P': 13750Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13760Sstevel@tonic-gate dcp->dc_func = compile_str; 13770Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER; 13780Sstevel@tonic-gate dcp->dc_arg = optarg; 13790Sstevel@tonic-gate break; 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate case 'q': 13820Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "quiet", 0) != 0) 13830Sstevel@tonic-gate dfatal("failed to set -q"); 13840Sstevel@tonic-gate break; 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate case 'o': 13870Sstevel@tonic-gate g_ofile = optarg; 13880Sstevel@tonic-gate break; 13890Sstevel@tonic-gate 13900Sstevel@tonic-gate case 's': 13910Sstevel@tonic-gate dcp = &g_cmdv[g_cmdc++]; 13920Sstevel@tonic-gate dcp->dc_func = compile_file; 13930Sstevel@tonic-gate dcp->dc_spec = DTRACE_PROBESPEC_NONE; 13940Sstevel@tonic-gate dcp->dc_arg = optarg; 13950Sstevel@tonic-gate break; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate case 'S': 13980Sstevel@tonic-gate g_cflags |= DTRACE_C_DIFV; 13990Sstevel@tonic-gate break; 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate case 'U': 14020Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "undef", optarg) != 0) 14030Sstevel@tonic-gate dfatal("failed to set -U %s", optarg); 14040Sstevel@tonic-gate break; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate case 'v': 14070Sstevel@tonic-gate g_verbose++; 14080Sstevel@tonic-gate break; 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate case 'w': 14110Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "destructive", 0) != 0) 14120Sstevel@tonic-gate dfatal("failed to set -w"); 14130Sstevel@tonic-gate break; 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate case 'x': 14160Sstevel@tonic-gate if ((p = strchr(optarg, '=')) != NULL) 14170Sstevel@tonic-gate *p++ = '\0'; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate if (dtrace_setopt(g_dtp, optarg, p) != 0) 14200Sstevel@tonic-gate dfatal("failed to set -x %s", optarg); 14210Sstevel@tonic-gate break; 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate case 'X': 14240Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "stdc", optarg) != 0) 14250Sstevel@tonic-gate dfatal("failed to set -X %s", optarg); 14260Sstevel@tonic-gate break; 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate case 'Z': 14290Sstevel@tonic-gate g_cflags |= DTRACE_C_ZDEFS; 14300Sstevel@tonic-gate break; 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate default: 14330Sstevel@tonic-gate if (strchr(DTRACE_OPTSTR, c) == NULL) 14340Sstevel@tonic-gate return (usage(stderr)); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14391017Sbmc if (g_ofp == NULL && g_mode != DMODE_EXEC) { 14401017Sbmc (void) fprintf(stderr, "%s: -B not valid in combination" 14411017Sbmc " with [-AGl] options\n", g_pname); 14421017Sbmc return (E_USAGE); 14431017Sbmc } 14441017Sbmc 14451017Sbmc if (g_ofp == NULL && g_ofile != NULL) { 14461017Sbmc (void) fprintf(stderr, "%s: -B not valid in combination" 14471017Sbmc " with -o option\n", g_pname); 14481017Sbmc return (E_USAGE); 14491017Sbmc } 14501017Sbmc 14510Sstevel@tonic-gate /* 14520Sstevel@tonic-gate * In our third pass we handle any command-line options related to 14530Sstevel@tonic-gate * grabbing or creating victim processes. The behavior of these calls 14540Sstevel@tonic-gate * may been affected by any library options set by the second pass. 14550Sstevel@tonic-gate */ 14560Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) { 14570Sstevel@tonic-gate while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) { 14580Sstevel@tonic-gate switch (c) { 14590Sstevel@tonic-gate case 'c': 14600Sstevel@tonic-gate if ((v = make_argv(optarg)) == NULL) 14610Sstevel@tonic-gate fatal("failed to allocate memory"); 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate P = dtrace_proc_create(g_dtp, v[0], v); 14640Sstevel@tonic-gate if (P == NULL) 14650Sstevel@tonic-gate dfatal(NULL); /* dtrace_errmsg() only */ 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate g_psv[g_psc++] = P; 14680Sstevel@tonic-gate free(v); 14690Sstevel@tonic-gate break; 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate case 'p': 14720Sstevel@tonic-gate errno = 0; 14730Sstevel@tonic-gate pid = strtol(optarg, &p, 10); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate if (errno != 0 || p == optarg || p[0] != '\0') 14760Sstevel@tonic-gate fatal("invalid pid: %s\n", optarg); 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate P = dtrace_proc_grab(g_dtp, pid, 0); 14790Sstevel@tonic-gate if (P == NULL) 14800Sstevel@tonic-gate dfatal(NULL); /* dtrace_errmsg() only */ 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate g_psv[g_psc++] = P; 14830Sstevel@tonic-gate break; 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate /* 14890Sstevel@tonic-gate * In our fourth pass we finish g_cmdv[] by calling dc_func to convert 14900Sstevel@tonic-gate * each string or file specification into a compiled program structure. 14910Sstevel@tonic-gate */ 14920Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 14930Sstevel@tonic-gate g_cmdv[i].dc_func(&g_cmdv[i]); 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate if (g_mode != DMODE_LIST) { 14960Sstevel@tonic-gate if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1) 14970Sstevel@tonic-gate dfatal("failed to establish error handler"); 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1) 15000Sstevel@tonic-gate dfatal("failed to establish drop handler"); 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1) 15030Sstevel@tonic-gate dfatal("failed to establish proc handler"); 1504457Sbmc 1505457Sbmc if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1) 1506457Sbmc dfatal("failed to establish setopt handler"); 15071017Sbmc 15081017Sbmc if (g_ofp == NULL && 15091017Sbmc dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1) 15101017Sbmc dfatal("failed to establish buffered handler"); 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "flowindent", &opt); 15140Sstevel@tonic-gate g_flowindent = opt != DTRACEOPT_UNSET; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "grabanon", &opt); 15170Sstevel@tonic-gate g_grabanon = opt != DTRACEOPT_UNSET; 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "quiet", &opt); 15200Sstevel@tonic-gate g_quiet = opt != DTRACEOPT_UNSET; 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate /* 15230Sstevel@tonic-gate * Now make a fifth and final pass over the options that have been 15240Sstevel@tonic-gate * turned into programs and saved in g_cmdv[], performing any mode- 15250Sstevel@tonic-gate * specific processing. If g_mode is DMODE_EXEC, we will break out 15260Sstevel@tonic-gate * of the switch() and continue on to the data processing loop. For 15270Sstevel@tonic-gate * other modes, we will exit dtrace once mode-specific work is done. 15280Sstevel@tonic-gate */ 15290Sstevel@tonic-gate switch (g_mode) { 15300Sstevel@tonic-gate case DMODE_EXEC: 15310Sstevel@tonic-gate if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 15320Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 15350Sstevel@tonic-gate exec_prog(&g_cmdv[i]); 15360Sstevel@tonic-gate 1537265Smws if (done && !g_grabanon) { 1538265Smws dtrace_close(g_dtp); 15390Sstevel@tonic-gate return (g_status); 1540265Smws } 15410Sstevel@tonic-gate break; 15420Sstevel@tonic-gate 15430Sstevel@tonic-gate case DMODE_ANON: 15440Sstevel@tonic-gate if (g_ofile == NULL) 15450Sstevel@tonic-gate g_ofile = "/kernel/drv/dtrace.conf"; 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate dof_prune(g_ofile); /* strip out any old DOF directives */ 15480Sstevel@tonic-gate etcsystem_prune(); /* string out any forceload directives */ 15490Sstevel@tonic-gate 1550265Smws if (g_cmdc == 0) { 1551265Smws dtrace_close(g_dtp); 15520Sstevel@tonic-gate return (g_status); 1553265Smws } 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate if ((g_ofp = fopen(g_ofile, "a")) == NULL) 15560Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) { 15590Sstevel@tonic-gate anon_prog(&g_cmdv[i], 15600Sstevel@tonic-gate dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i); 15610Sstevel@tonic-gate } 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate /* 15640Sstevel@tonic-gate * Dump out the DOF corresponding to the error handler and the 15650Sstevel@tonic-gate * current options as the final DOF property in the .conf file. 15660Sstevel@tonic-gate */ 15670Sstevel@tonic-gate anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++); 15680Sstevel@tonic-gate anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if (fclose(g_ofp) == EOF) 15710Sstevel@tonic-gate fatal("failed to close output file '%s'", g_ofile); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * These messages would use notice() rather than error(), but 15750Sstevel@tonic-gate * we don't want them suppressed when -A is run on a D program 15760Sstevel@tonic-gate * that itself contains a #pragma D option quiet. 15770Sstevel@tonic-gate */ 15780Sstevel@tonic-gate error("saved anonymous enabling in %s\n", g_ofile); 15790Sstevel@tonic-gate etcsystem_add(); 15800Sstevel@tonic-gate error("run update_drv(1M) or reboot to enable changes\n"); 15810Sstevel@tonic-gate 1582265Smws dtrace_close(g_dtp); 15830Sstevel@tonic-gate return (g_status); 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate case DMODE_LINK: 1586*1222Smws if (g_cmdc == 0) { 1587*1222Smws (void) fprintf(stderr, "%s: -G requires one or more " 1588*1222Smws "scripts or enabling options\n", g_pname); 1589*1222Smws dtrace_close(g_dtp); 1590*1222Smws return (E_USAGE); 1591*1222Smws } 1592*1222Smws 15930Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 15940Sstevel@tonic-gate link_prog(&g_cmdv[i]); 1595191Sahl 1596191Sahl if (g_cmdc > 1 && g_ofile != NULL) { 1597191Sahl char **objv = alloca(g_cmdc * sizeof (char *)); 1598191Sahl 1599191Sahl for (i = 0; i < g_cmdc; i++) 1600191Sahl objv[i] = g_cmdv[i].dc_ofile; 1601191Sahl 1602191Sahl if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES, 1603191Sahl g_ofile, g_cmdc, objv) != 0) 1604265Smws dfatal(NULL); /* dtrace_errmsg() only */ 1605191Sahl } 1606191Sahl 1607265Smws dtrace_close(g_dtp); 16080Sstevel@tonic-gate return (g_status); 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate case DMODE_LIST: 16110Sstevel@tonic-gate if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL) 16120Sstevel@tonic-gate fatal("failed to open output file '%s'", g_ofile); 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate oprintf("%5s %10s %17s %33s %s\n", 16150Sstevel@tonic-gate "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME"); 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate for (i = 0; i < g_cmdc; i++) 16180Sstevel@tonic-gate list_prog(&g_cmdv[i]); 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate if (g_cmdc == 0) 16210Sstevel@tonic-gate (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL); 16220Sstevel@tonic-gate 1623265Smws dtrace_close(g_dtp); 16240Sstevel@tonic-gate return (g_status); 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate /* 16280Sstevel@tonic-gate * If -a and -Z were not specified and no probes have been matched, no 16290Sstevel@tonic-gate * probe criteria was specified on the command line and we abort. 16300Sstevel@tonic-gate */ 16310Sstevel@tonic-gate if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS)) 16320Sstevel@tonic-gate dfatal("no probes %s\n", g_cmdc ? "matched" : "specified"); 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate /* 16350Sstevel@tonic-gate * Start tracing. Once we dtrace_go(), reload any options that affect 16360Sstevel@tonic-gate * our globals in case consuming anonymous state has changed them. 16370Sstevel@tonic-gate */ 16380Sstevel@tonic-gate go(); 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "flowindent", &opt); 16410Sstevel@tonic-gate g_flowindent = opt != DTRACEOPT_UNSET; 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "grabanon", &opt); 16440Sstevel@tonic-gate g_grabanon = opt != DTRACEOPT_UNSET; 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "quiet", &opt); 16470Sstevel@tonic-gate g_quiet = opt != DTRACEOPT_UNSET; 16480Sstevel@tonic-gate 16490Sstevel@tonic-gate (void) dtrace_getopt(g_dtp, "destructive", &opt); 16500Sstevel@tonic-gate if (opt != DTRACEOPT_UNSET) 16510Sstevel@tonic-gate notice("allowing destructive actions\n"); 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 16540Sstevel@tonic-gate act.sa_flags = 0; 16550Sstevel@tonic-gate act.sa_handler = intr; 1656*1222Smws 1657*1222Smws if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN) 1658*1222Smws (void) sigaction(SIGINT, &act, NULL); 1659*1222Smws 1660*1222Smws if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN) 1661*1222Smws (void) sigaction(SIGTERM, &act, NULL); 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate /* 16640Sstevel@tonic-gate * Now that tracing is active and we are ready to consume trace data, 16650Sstevel@tonic-gate * continue any grabbed or created processes, setting them running 16660Sstevel@tonic-gate * using the /proc control mechanism inside of libdtrace. 16670Sstevel@tonic-gate */ 16680Sstevel@tonic-gate for (i = 0; i < g_psc; i++) 16690Sstevel@tonic-gate dtrace_proc_continue(g_dtp, g_psv[i]); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate g_pslive = g_psc; /* count for prochandler() */ 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate do { 16740Sstevel@tonic-gate if (!g_intr && !done) 16750Sstevel@tonic-gate dtrace_sleep(g_dtp); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate if (g_newline) { 16780Sstevel@tonic-gate /* 16790Sstevel@tonic-gate * Output a newline just to make the output look 16800Sstevel@tonic-gate * slightly cleaner. Note that we do this even in 16810Sstevel@tonic-gate * "quiet" mode... 16820Sstevel@tonic-gate */ 16830Sstevel@tonic-gate oprintf("\n"); 16840Sstevel@tonic-gate g_newline = 0; 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate if (done || g_intr || (g_psc != 0 && g_pslive == 0)) { 16880Sstevel@tonic-gate done = 1; 16890Sstevel@tonic-gate if (dtrace_stop(g_dtp) == -1) 16900Sstevel@tonic-gate dfatal("couldn't stop tracing"); 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) { 16940Sstevel@tonic-gate case DTRACE_WORKSTATUS_DONE: 16950Sstevel@tonic-gate done = 1; 16960Sstevel@tonic-gate break; 16970Sstevel@tonic-gate case DTRACE_WORKSTATUS_OKAY: 16980Sstevel@tonic-gate break; 16990Sstevel@tonic-gate default: 17000Sstevel@tonic-gate if (!g_impatient && dtrace_errno(g_dtp) != EINTR) 17010Sstevel@tonic-gate dfatal("processing aborted"); 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate 17041017Sbmc if (g_ofp != NULL && fflush(g_ofp) == EOF) 17050Sstevel@tonic-gate clearerr(g_ofp); 17060Sstevel@tonic-gate } while (!done); 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate oprintf("\n"); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate if (!g_impatient) { 17110Sstevel@tonic-gate if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 && 17120Sstevel@tonic-gate dtrace_errno(g_dtp) != EINTR) 17130Sstevel@tonic-gate dfatal("failed to print aggregations"); 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate dtrace_close(g_dtp); 17170Sstevel@tonic-gate return (g_status); 17180Sstevel@tonic-gate } 1719