xref: /onnv-gate/usr/src/cmd/dtrace/dtrace.c (revision 2769:b2e3d55c6e12)
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
5*2769Sahl  * Common Development and Distribution License (the "License").
6*2769Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211222Smws 
220Sstevel@tonic-gate /*
231222Smws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/wait.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <dtrace.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <alloca.h>
440Sstevel@tonic-gate #include <libgen.h>
450Sstevel@tonic-gate #include <libproc.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate typedef struct dtrace_cmd {
480Sstevel@tonic-gate 	void (*dc_func)(struct dtrace_cmd *);	/* function to compile arg */
490Sstevel@tonic-gate 	dtrace_probespec_t dc_spec;		/* probe specifier context */
500Sstevel@tonic-gate 	char *dc_arg;				/* argument from main argv */
510Sstevel@tonic-gate 	const char *dc_name;			/* name for error messages */
520Sstevel@tonic-gate 	const char *dc_desc;			/* desc for error messages */
530Sstevel@tonic-gate 	dtrace_prog_t *dc_prog;			/* program compiled from arg */
54191Sahl 	char dc_ofile[PATH_MAX];		/* derived output file name */
550Sstevel@tonic-gate } dtrace_cmd_t;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define	DMODE_VERS	0	/* display version information and exit (-V) */
580Sstevel@tonic-gate #define	DMODE_EXEC	1	/* compile program for enabling (-a/e/E) */
590Sstevel@tonic-gate #define	DMODE_ANON	2	/* compile program for anonymous tracing (-A) */
600Sstevel@tonic-gate #define	DMODE_LINK	3	/* compile program for linking with ELF (-G) */
610Sstevel@tonic-gate #define	DMODE_LIST	4	/* compile program and list probes (-l) */
621399Sahl #define	DMODE_HEADER	5	/* compile program for headergen (-h) */
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[] =
691399Sahl 	"3:6:aAb:Bc:CD:ef:FGhHi: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
usage(FILE * fp)1130Sstevel@tonic-gate usage(FILE *fp)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	static const char predact[] = "[[ predicate ] action ]";
1160Sstevel@tonic-gate 
1171399Sahl 	(void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
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"
1441399Sahl 	    "\t-h  generate a header file with definitions for static probes\n"
1450Sstevel@tonic-gate 	    "\t-H  print included files when invoking preprocessor\n"
1460Sstevel@tonic-gate 	    "\t-i  enable or list probes matching the specified probe id\n"
1470Sstevel@tonic-gate 	    "\t-I  add include directory to preprocessor search path\n"
1480Sstevel@tonic-gate 	    "\t-l  list probes matching specified criteria\n"
1490Sstevel@tonic-gate 	    "\t-L  add library directory to library search path\n"
1500Sstevel@tonic-gate 	    "\t-m  enable or list probes matching the specified module name\n"
1510Sstevel@tonic-gate 	    "\t-n  enable or list probes matching the specified probe name\n"
1520Sstevel@tonic-gate 	    "\t-o  set output file\n"
1530Sstevel@tonic-gate 	    "\t-p  grab specified process-ID and cache its symbol tables\n"
1540Sstevel@tonic-gate 	    "\t-P  enable or list probes matching the specified provider name\n"
1550Sstevel@tonic-gate 	    "\t-q  set quiet mode (only output explicitly traced data)\n"
1560Sstevel@tonic-gate 	    "\t-s  enable or list probes according to the specified D script\n"
1570Sstevel@tonic-gate 	    "\t-S  print D compiler intermediate code\n"
1580Sstevel@tonic-gate 	    "\t-U  undefine symbol when invoking preprocessor\n"
1590Sstevel@tonic-gate 	    "\t-v  set verbose mode (report stability attributes, arguments)\n"
1600Sstevel@tonic-gate 	    "\t-V  report DTrace API version\n"
1610Sstevel@tonic-gate 	    "\t-w  permit destructive actions\n"
1620Sstevel@tonic-gate 	    "\t-x  enable or modify compiler and tracing options\n"
1630Sstevel@tonic-gate 	    "\t-X  specify ISO C conformance settings for preprocessor\n"
1640Sstevel@tonic-gate 	    "\t-Z  permit probe descriptions that match zero probes\n");
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	return (E_USAGE);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static void
verror(const char * fmt,va_list ap)1700Sstevel@tonic-gate verror(const char *fmt, va_list ap)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int error = errno;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
1750Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
1780Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*PRINTFLIKE1*/
1820Sstevel@tonic-gate static void
fatal(const char * fmt,...)1830Sstevel@tonic-gate fatal(const char *fmt, ...)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	va_list ap;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	va_start(ap, fmt);
1880Sstevel@tonic-gate 	verror(fmt, ap);
1890Sstevel@tonic-gate 	va_end(ap);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	exit(E_ERROR);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*PRINTFLIKE1*/
1950Sstevel@tonic-gate static void
dfatal(const char * fmt,...)1960Sstevel@tonic-gate dfatal(const char *fmt, ...)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	va_list ap;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	va_start(ap, fmt);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
2030Sstevel@tonic-gate 	if (fmt != NULL)
2040Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	va_end(ap);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
2090Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
2100Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2110Sstevel@tonic-gate 	} else if (fmt == NULL) {
2120Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n",
2130Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
216*2769Sahl 	/*
217*2769Sahl 	 * Close the DTrace handle to ensure that any controlled processes are
218*2769Sahl 	 * correctly restored and continued.
219*2769Sahl 	 */
220*2769Sahl 	dtrace_close(g_dtp);
221*2769Sahl 
2220Sstevel@tonic-gate 	exit(E_ERROR);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*PRINTFLIKE1*/
2260Sstevel@tonic-gate static void
error(const char * fmt,...)2270Sstevel@tonic-gate error(const char *fmt, ...)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	va_list ap;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	va_start(ap, fmt);
2320Sstevel@tonic-gate 	verror(fmt, ap);
2330Sstevel@tonic-gate 	va_end(ap);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*PRINTFLIKE1*/
2370Sstevel@tonic-gate static void
notice(const char * fmt,...)2380Sstevel@tonic-gate notice(const char *fmt, ...)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	va_list ap;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (g_quiet)
2430Sstevel@tonic-gate 		return; /* -q or quiet pragma suppresses notice()s */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	va_start(ap, fmt);
2460Sstevel@tonic-gate 	verror(fmt, ap);
2470Sstevel@tonic-gate 	va_end(ap);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*PRINTFLIKE1*/
2510Sstevel@tonic-gate static void
oprintf(const char * fmt,...)2520Sstevel@tonic-gate oprintf(const char *fmt, ...)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	va_list ap;
2550Sstevel@tonic-gate 	int n;
2560Sstevel@tonic-gate 
2571017Sbmc 	if (g_ofp == NULL)
2581017Sbmc 		return;
2591017Sbmc 
2600Sstevel@tonic-gate 	va_start(ap, fmt);
2610Sstevel@tonic-gate 	n = vfprintf(g_ofp, fmt, ap);
2620Sstevel@tonic-gate 	va_end(ap);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (n < 0) {
2650Sstevel@tonic-gate 		if (errno != EINTR) {
2660Sstevel@tonic-gate 			fatal("failed to write to %s",
2670Sstevel@tonic-gate 			    g_ofile ? g_ofile : "<stdout>");
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		clearerr(g_ofp);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static char **
make_argv(char * s)2740Sstevel@tonic-gate make_argv(char *s)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	const char *ws = "\f\n\r\t\v ";
2770Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
2780Sstevel@tonic-gate 	int argc = 0;
2790Sstevel@tonic-gate 	char *p = s;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if (argv == NULL)
2820Sstevel@tonic-gate 		return (NULL);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
2850Sstevel@tonic-gate 		argv[argc++] = p;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (argc == 0)
2880Sstevel@tonic-gate 		argv[argc++] = s;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	argv[argc] = NULL;
2910Sstevel@tonic-gate 	return (argv);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static void
dof_prune(const char * fname)2950Sstevel@tonic-gate dof_prune(const char *fname)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	struct stat sbuf;
2980Sstevel@tonic-gate 	size_t sz, i, j, mark, len;
2990Sstevel@tonic-gate 	char *buf;
3000Sstevel@tonic-gate 	int msg = 0, fd;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
3030Sstevel@tonic-gate 		/*
3040Sstevel@tonic-gate 		 * This is okay only if the file doesn't exist at all.
3050Sstevel@tonic-gate 		 */
3060Sstevel@tonic-gate 		if (errno != ENOENT)
3070Sstevel@tonic-gate 			fatal("failed to open %s", fname);
3080Sstevel@tonic-gate 		return;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3120Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3150Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3180Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	buf[sz] = '\0';
3210Sstevel@tonic-gate 	(void) close(fd);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
3240Sstevel@tonic-gate 		fatal("failed to open %s for writing", fname);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	len = strlen("dof-data-");
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	for (mark = 0, i = 0; i < sz; i++) {
3290Sstevel@tonic-gate 		if (strncmp(&buf[i], "dof-data-", len) != 0)
3300Sstevel@tonic-gate 			continue;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		/*
3330Sstevel@tonic-gate 		 * This is only a match if it's in the 0th column.
3340Sstevel@tonic-gate 		 */
3350Sstevel@tonic-gate 		if (i != 0 && buf[i - 1] != '\n')
3360Sstevel@tonic-gate 			continue;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		if (msg++ == 0) {
3390Sstevel@tonic-gate 			error("cleaned up old anonymous "
3400Sstevel@tonic-gate 			    "enabling in %s\n", fname);
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * We have a match.  First write out our data up until now.
3450Sstevel@tonic-gate 		 */
3460Sstevel@tonic-gate 		if (i != mark) {
3470Sstevel@tonic-gate 			if (write(fd, &buf[mark], i - mark) != i - mark)
3480Sstevel@tonic-gate 				fatal("failed to write to %s", fname);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		/*
3520Sstevel@tonic-gate 		 * Now scan forward until we scan past a newline.
3530Sstevel@tonic-gate 		 */
3540Sstevel@tonic-gate 		for (j = i; j < sz && buf[j] != '\n'; j++)
3550Sstevel@tonic-gate 			continue;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		/*
3580Sstevel@tonic-gate 		 * Reset our mark.
3590Sstevel@tonic-gate 		 */
3600Sstevel@tonic-gate 		if ((mark = j + 1) >= sz)
3610Sstevel@tonic-gate 			break;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		i = j;
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (mark < sz) {
3670Sstevel@tonic-gate 		if (write(fd, &buf[mark], sz - mark) != sz - mark)
3680Sstevel@tonic-gate 			fatal("failed to write to %s", fname);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	(void) close(fd);
3720Sstevel@tonic-gate 	free(buf);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate static void
etcsystem_prune(void)3760Sstevel@tonic-gate etcsystem_prune(void)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	struct stat sbuf;
3790Sstevel@tonic-gate 	size_t sz;
3800Sstevel@tonic-gate 	char *buf, *start, *end;
3810Sstevel@tonic-gate 	int fd;
3820Sstevel@tonic-gate 	char *fname = g_etcfile, *tmpname;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1)
3850Sstevel@tonic-gate 		fatal("failed to open %s", fname);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3880Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3910Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3940Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	buf[sz] = '\0';
3970Sstevel@tonic-gate 	(void) close(fd);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if ((start = strstr(buf, g_etcbegin)) == NULL)
4000Sstevel@tonic-gate 		goto out;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (strlen(buf) != sz) {
4030Sstevel@tonic-gate 		fatal("embedded nul byte in %s; manual repair of %s "
4040Sstevel@tonic-gate 		    "required\n", fname, fname);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (strstr(start + 1, g_etcbegin) != NULL) {
4080Sstevel@tonic-gate 		fatal("multiple start sentinels in %s; manual repair of %s "
4090Sstevel@tonic-gate 		    "required\n", fname, fname);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if ((end = strstr(buf, g_etcend)) == NULL) {
4130Sstevel@tonic-gate 		fatal("missing end sentinel in %s; manual repair of %s "
4140Sstevel@tonic-gate 		    "required\n", fname, fname);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (start > end) {
4180Sstevel@tonic-gate 		fatal("end sentinel preceeds start sentinel in %s; manual "
4190Sstevel@tonic-gate 		    "repair of %s required\n", fname, fname);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	end += strlen(g_etcend) + 1;
4230Sstevel@tonic-gate 	bcopy(end, start, strlen(end) + 1);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	tmpname = alloca(sz = strlen(fname) + 80);
4260Sstevel@tonic-gate 	(void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if ((fd = open(tmpname,
4290Sstevel@tonic-gate 	    O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
4300Sstevel@tonic-gate 		fatal("failed to create %s", tmpname);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (write(fd, buf, strlen(buf)) < strlen(buf)) {
4330Sstevel@tonic-gate 		(void) unlink(tmpname);
4340Sstevel@tonic-gate 		fatal("failed to write to %s", tmpname);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	(void) close(fd);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
4400Sstevel@tonic-gate 		(void) unlink(tmpname);
4410Sstevel@tonic-gate 		fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
4420Sstevel@tonic-gate 		    (int)sbuf.st_uid, (int)sbuf.st_gid);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (rename(tmpname, fname) == -1)
4460Sstevel@tonic-gate 		fatal("rename of %s to %s failed", tmpname, fname);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	error("cleaned up forceload directives in %s\n", fname);
4490Sstevel@tonic-gate out:
4500Sstevel@tonic-gate 	free(buf);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate static void
etcsystem_add(void)4540Sstevel@tonic-gate etcsystem_add(void)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	const char *mods[20];
4570Sstevel@tonic-gate 	int nmods, line;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
4600Sstevel@tonic-gate 		fatal("failed to open output file '%s'", g_ofile);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	oprintf("%s\n", g_etcbegin);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	for (line = 0; g_etc[line] != NULL; line++)
4650Sstevel@tonic-gate 		oprintf("%s\n", g_etc[line]);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	nmods = dtrace_provider_modules(g_dtp, mods,
4680Sstevel@tonic-gate 	    sizeof (mods) / sizeof (char *) - 1);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (nmods >= sizeof (mods) / sizeof (char *))
4710Sstevel@tonic-gate 		fatal("unexpectedly large number of modules!");
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	mods[nmods++] = "dtrace";
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	for (line = 0; line < nmods; line++)
4760Sstevel@tonic-gate 		oprintf("forceload: drv/%s\n", mods[line]);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	oprintf("%s\n", g_etcend);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (fclose(g_ofp) == EOF)
4810Sstevel@tonic-gate 		fatal("failed to close output file '%s'", g_ofile);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	error("added forceload directives to %s\n", g_ofile);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate static void
print_probe_info(const dtrace_probeinfo_t * p)4870Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	char buf[BUFSIZ];
4900Sstevel@tonic-gate 	int i;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	oprintf("\n\tProbe Description Attributes\n");
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
4950Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_name));
4960Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
4970Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_data));
4980Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
4990Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_attr.dtat_class));
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	oprintf("\n\tArgument Attributes\n");
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
5040Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_name));
5050Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
5060Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_data));
5070Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
5080Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_arga.dtat_class));
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	oprintf("\n\tArgument Types\n");
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	for (i = 0; i < p->dtp_argc; i++) {
5130Sstevel@tonic-gate 		if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
5140Sstevel@tonic-gate 		    p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
5150Sstevel@tonic-gate 			(void) strlcpy(buf, "(unknown)", sizeof (buf));
5160Sstevel@tonic-gate 		oprintf("\t\targs[%d]: %s\n", i, buf);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (p->dtp_argc == 0)
5200Sstevel@tonic-gate 		oprintf("\t\tNone\n");
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	oprintf("\n");
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*ARGSUSED*/
5260Sstevel@tonic-gate static int
info_stmt(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,dtrace_stmtdesc_t * stp,dtrace_ecbdesc_t ** last)5270Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
5280Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
5310Sstevel@tonic-gate 	dtrace_probedesc_t *pdp = &edp->dted_probe;
5320Sstevel@tonic-gate 	dtrace_probeinfo_t p;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (edp == *last)
5350Sstevel@tonic-gate 		return (0);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	oprintf("\n%s:%s:%s:%s\n",
5380Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (dtrace_probe_info(dtp, pdp, &p) == 0)
5410Sstevel@tonic-gate 		print_probe_info(&p);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	*last = edp;
5440Sstevel@tonic-gate 	return (0);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * Execute the specified program by enabling the corresponding instrumentation.
5490Sstevel@tonic-gate  * If -e has been specified, we get the program info but do not enable it.  If
5500Sstevel@tonic-gate  * -v has been specified, we print a stability report for the program.
5510Sstevel@tonic-gate  */
5520Sstevel@tonic-gate static void
exec_prog(const dtrace_cmd_t * dcp)5530Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
5560Sstevel@tonic-gate 	dtrace_proginfo_t dpi;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (!g_exec) {
5590Sstevel@tonic-gate 		dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
5600Sstevel@tonic-gate 	} else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
5610Sstevel@tonic-gate 		dfatal("failed to enable '%s'", dcp->dc_name);
5620Sstevel@tonic-gate 	} else {
5630Sstevel@tonic-gate 		notice("%s '%s' matched %u probe%s\n",
5640Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name,
5650Sstevel@tonic-gate 		    dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (g_verbose) {
5690Sstevel@tonic-gate 		oprintf("\nStability attributes for %s %s:\n",
5700Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		oprintf("\n\tMinimum Probe Description Attributes\n");
5730Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5740Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_name));
5750Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5760Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_data));
5770Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5780Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_descattr.dtat_class));
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 		oprintf("\n\tMinimum Statement Attributes\n");
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5830Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
5840Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5850Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
5860Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5870Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		if (!g_exec) {
5900Sstevel@tonic-gate 			(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
5910Sstevel@tonic-gate 			    (dtrace_stmt_f *)info_stmt, &last);
5920Sstevel@tonic-gate 		} else
5930Sstevel@tonic-gate 			oprintf("\n");
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	g_total += dpi.dpi_matches;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
6010Sstevel@tonic-gate  * storing in a driver.conf(4) file associated with the dtrace driver.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate static void
anon_prog(const dtrace_cmd_t * dcp,dof_hdr_t * dof,int n)6040Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	const uchar_t *p, *q;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (dof == NULL)
6090Sstevel@tonic-gate 		dfatal("failed to create DOF image for '%s'", dcp->dc_name);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	p = (uchar_t *)dof;
6120Sstevel@tonic-gate 	q = p + dof->dofh_loadsz;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	oprintf("dof-data-%d=0x%x", n, *p++);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	while (p < q)
6170Sstevel@tonic-gate 		oprintf(",0x%x", *p++);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	oprintf(";\n");
6200Sstevel@tonic-gate 	dtrace_dof_destroy(g_dtp, dof);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Link the specified D program in DOF form into an ELF file for use in either
6250Sstevel@tonic-gate  * helpers, userland provider definitions, or both.  If -o was specified, that
6260Sstevel@tonic-gate  * path is used as the output file name.  If -o wasn't specified and the input
6270Sstevel@tonic-gate  * program is from a script whose name is %.d, use basename(%.o) as the output
6280Sstevel@tonic-gate  * file name.  Otherwise we use "d.out" as the default output file name.
6290Sstevel@tonic-gate  */
6300Sstevel@tonic-gate static void
link_prog(dtrace_cmd_t * dcp)631191Sahl link_prog(dtrace_cmd_t *dcp)
6320Sstevel@tonic-gate {
633191Sahl 	char *p;
6340Sstevel@tonic-gate 
635191Sahl 	if (g_cmdc == 1 && g_ofile != NULL) {
636191Sahl 		(void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
6370Sstevel@tonic-gate 	} else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
6380Sstevel@tonic-gate 	    strcmp(p, ".d") == 0) {
6390Sstevel@tonic-gate 		p[0] = '\0'; /* strip .d suffix */
640191Sahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
6410Sstevel@tonic-gate 		    "%s.o", basename(dcp->dc_arg));
6420Sstevel@tonic-gate 	} else {
643191Sahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
644191Sahl 		    g_cmdc > 1 ?  "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
648*2769Sahl 	    dcp->dc_ofile, g_objc, g_objv) != 0)
6490Sstevel@tonic-gate 		dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /*ARGSUSED*/
6530Sstevel@tonic-gate static int
list_probe(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * arg)6540Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	dtrace_probeinfo_t p;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
6590Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
6620Sstevel@tonic-gate 		print_probe_info(&p);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	return (0);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*ARGSUSED*/
6680Sstevel@tonic-gate static int
list_stmt(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,dtrace_stmtdesc_t * stp,dtrace_ecbdesc_t ** last)6690Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
6700Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (edp == *last)
6750Sstevel@tonic-gate 		return (0);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
6780Sstevel@tonic-gate 		error("failed to match %s:%s:%s:%s: %s\n",
6790Sstevel@tonic-gate 		    edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
6800Sstevel@tonic-gate 		    edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
6810Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	*last = edp;
6850Sstevel@tonic-gate 	return (0);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * List the probes corresponding to the specified program by iterating over
6900Sstevel@tonic-gate  * each statement and then matching probes to the statement probe descriptions.
6910Sstevel@tonic-gate  */
6920Sstevel@tonic-gate static void
list_prog(const dtrace_cmd_t * dcp)6930Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
6980Sstevel@tonic-gate 	    (dtrace_stmt_f *)list_stmt, &last);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate static void
compile_file(dtrace_cmd_t * dcp)7020Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	char *arg0;
7050Sstevel@tonic-gate 	FILE *fp;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
7080Sstevel@tonic-gate 		fatal("failed to open %s", dcp->dc_arg);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	arg0 = g_argv[0];
7110Sstevel@tonic-gate 	g_argv[0] = dcp->dc_arg;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
7140Sstevel@tonic-gate 	    g_cflags, g_argc, g_argv)) == NULL)
7150Sstevel@tonic-gate 		dfatal("failed to compile script %s", dcp->dc_arg);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	g_argv[0] = arg0;
7180Sstevel@tonic-gate 	(void) fclose(fp);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	dcp->dc_desc = "script";
7210Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate static void
compile_str(dtrace_cmd_t * dcp)7250Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	char *p;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
7300Sstevel@tonic-gate 	    dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
7310Sstevel@tonic-gate 		dfatal("invalid probe specifier %s", dcp->dc_arg);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
7340Sstevel@tonic-gate 		*p = '\0'; /* crop name for reporting */
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	dcp->dc_desc = "description";
7370Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate /*ARGSUSED*/
7410Sstevel@tonic-gate static void
prochandler(struct ps_prochandle * P,const char * msg,void * arg)7421399Sahl prochandler(struct ps_prochandle *P, const char *msg, void *arg)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
7450Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
7460Sstevel@tonic-gate 	char name[SIG2STR_MAX];
7470Sstevel@tonic-gate 
7481399Sahl 	if (msg != NULL) {
7491399Sahl 		notice("pid %d: %s\n", pid, msg);
7501399Sahl 		return;
7511399Sahl 	}
7521399Sahl 
7530Sstevel@tonic-gate 	switch (Pstate(P)) {
7540Sstevel@tonic-gate 	case PS_UNDEAD:
7550Sstevel@tonic-gate 		/*
7560Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
7570Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
7580Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
7590Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
7600Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
7610Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
7640Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
7650Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
7660Sstevel@tonic-gate 			    name, sizeof (name)));
7670Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
7680Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
7690Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
7700Sstevel@tonic-gate 		} else {
7710Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 		g_pslive--;
7740Sstevel@tonic-gate 		break;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	case PS_LOST:
7770Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
7780Sstevel@tonic-gate 		g_pslive--;
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*ARGSUSED*/
7840Sstevel@tonic-gate static int
errhandler(const dtrace_errdata_t * data,void * arg)785457Sbmc errhandler(const dtrace_errdata_t *data, void *arg)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	error(data->dteda_msg);
7880Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /*ARGSUSED*/
7920Sstevel@tonic-gate static int
drophandler(const dtrace_dropdata_t * data,void * arg)793457Sbmc drophandler(const dtrace_dropdata_t *data, void *arg)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	error(data->dtdda_msg);
7960Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate /*ARGSUSED*/
8000Sstevel@tonic-gate static int
setopthandler(const dtrace_setoptdata_t * data,void * arg)801457Sbmc setopthandler(const dtrace_setoptdata_t *data, void *arg)
802457Sbmc {
803457Sbmc 	if (strcmp(data->dtsda_option, "quiet") == 0)
804457Sbmc 		g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
805457Sbmc 
806457Sbmc 	if (strcmp(data->dtsda_option, "flowindent") == 0)
807457Sbmc 		g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
808457Sbmc 
809457Sbmc 	return (DTRACE_HANDLE_OK);
810457Sbmc }
811457Sbmc 
8121017Sbmc #define	BUFDUMPHDR(hdr) \
8131017Sbmc 	(void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
8141017Sbmc 
8151017Sbmc #define	BUFDUMPSTR(ptr, field) \
8161017Sbmc 	(void) printf("%s: %20s => ", g_pname, #field);	\
8171017Sbmc 	if ((ptr)->field != NULL) {			\
8181017Sbmc 		const char *c = (ptr)->field;		\
8191017Sbmc 		(void) printf("\"");			\
8201017Sbmc 		do {					\
8211017Sbmc 			if (*c == '\n') {		\
8221017Sbmc 				(void) printf("\\n");	\
8231017Sbmc 				continue;		\
8241017Sbmc 			}				\
8251017Sbmc 							\
8261017Sbmc 			(void) printf("%c", *c);	\
8271017Sbmc 		} while (*c++ != '\0');			\
8281017Sbmc 		(void) printf("\"\n");			\
8291017Sbmc 	} else {					\
8301017Sbmc 		(void) printf("<NULL>\n");		\
8311017Sbmc 	}
8321017Sbmc 
8331017Sbmc #define	BUFDUMPASSTR(ptr, field, str) \
8341017Sbmc 	(void) printf("%s: %20s => %s\n", g_pname, #field, str);
8351017Sbmc 
8361017Sbmc #define	BUFDUMP(ptr, field) \
8371017Sbmc 	(void) printf("%s: %20s => %lld\n", g_pname, #field, \
8381017Sbmc 	    (long long)(ptr)->field);
8391017Sbmc 
8401017Sbmc #define	BUFDUMPPTR(ptr, field) \
8411017Sbmc 	(void) printf("%s: %20s => %s\n", g_pname, #field, \
8421017Sbmc 	    (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
8431017Sbmc 
8441017Sbmc /*ARGSUSED*/
8451017Sbmc static int
bufhandler(const dtrace_bufdata_t * bufdata,void * arg)8461017Sbmc bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
8471017Sbmc {
8481017Sbmc 	const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
8491017Sbmc 	const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
8501017Sbmc 	const dtrace_probedesc_t *pd;
8511017Sbmc 	uint32_t flags = bufdata->dtbda_flags;
8521017Sbmc 	char buf[512], *c = buf, *end = c + sizeof (buf);
8531017Sbmc 	int i, printed;
8541017Sbmc 
8551017Sbmc 	struct {
8561017Sbmc 		const char *name;
8571017Sbmc 		uint32_t value;
8581017Sbmc 	} flagnames[] = {
8591017Sbmc 	    { "AGGVAL",		DTRACE_BUFDATA_AGGVAL },
8601017Sbmc 	    { "AGGKEY",		DTRACE_BUFDATA_AGGKEY },
8611017Sbmc 	    { "AGGFORMAT",	DTRACE_BUFDATA_AGGFORMAT },
8621017Sbmc 	    { "AGGLAST",	DTRACE_BUFDATA_AGGLAST },
8631017Sbmc 	    { "???",		UINT32_MAX },
8641017Sbmc 	    { NULL }
8651017Sbmc 	};
8661017Sbmc 
8671017Sbmc 	if (bufdata->dtbda_probe != NULL) {
8681017Sbmc 		pd = bufdata->dtbda_probe->dtpda_pdesc;
8691017Sbmc 	} else if (agg != NULL) {
8701017Sbmc 		pd = agg->dtada_pdesc;
8711017Sbmc 	} else {
8721017Sbmc 		pd = NULL;
8731017Sbmc 	}
8741017Sbmc 
8751017Sbmc 	BUFDUMPHDR(">>> Called buffer handler");
8761017Sbmc 	BUFDUMPHDR("");
8771017Sbmc 
8781017Sbmc 	BUFDUMPHDR("  dtrace_bufdata");
8791017Sbmc 	BUFDUMPSTR(bufdata, dtbda_buffered);
8801017Sbmc 	BUFDUMPPTR(bufdata, dtbda_probe);
8811017Sbmc 	BUFDUMPPTR(bufdata, dtbda_aggdata);
8821017Sbmc 	BUFDUMPPTR(bufdata, dtbda_recdesc);
8831017Sbmc 
8841017Sbmc 	(void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
8851017Sbmc 	c += strlen(c);
8861017Sbmc 
8871017Sbmc 	for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
8881017Sbmc 		if (!(flags & flagnames[i].value))
8891017Sbmc 			continue;
8901017Sbmc 
8911017Sbmc 		(void) snprintf(c, end - c,
8921017Sbmc 		    "%s%s", printed++ ? " | " : "(", flagnames[i].name);
8931017Sbmc 		c += strlen(c);
8941017Sbmc 		flags &= ~flagnames[i].value;
8951017Sbmc 	}
8961017Sbmc 
8971017Sbmc 	if (printed)
8981017Sbmc 		(void) snprintf(c, end - c, ")");
8991017Sbmc 
9001017Sbmc 	BUFDUMPASSTR(bufdata, dtbda_flags, buf);
9011017Sbmc 	BUFDUMPHDR("");
9021017Sbmc 
9031017Sbmc 	if (pd != NULL) {
9041017Sbmc 		BUFDUMPHDR("  dtrace_probedesc");
9051017Sbmc 		BUFDUMPSTR(pd, dtpd_provider);
9061017Sbmc 		BUFDUMPSTR(pd, dtpd_mod);
9071017Sbmc 		BUFDUMPSTR(pd, dtpd_func);
9081017Sbmc 		BUFDUMPSTR(pd, dtpd_name);
9091017Sbmc 		BUFDUMPHDR("");
9101017Sbmc 	}
9111017Sbmc 
9121017Sbmc 	if (rec != NULL) {
9131017Sbmc 		BUFDUMPHDR("  dtrace_recdesc");
9141017Sbmc 		BUFDUMP(rec, dtrd_action);
9151017Sbmc 		BUFDUMP(rec, dtrd_size);
9161028Sbmc 
9171028Sbmc 		if (agg != NULL) {
9181028Sbmc 			uint8_t *data;
9191028Sbmc 			int lim = rec->dtrd_size;
9201028Sbmc 
9211028Sbmc 			(void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
9221028Sbmc 			c = buf + strlen(buf);
9231028Sbmc 
9241028Sbmc 			if (lim > sizeof (uint64_t))
9251028Sbmc 				lim = sizeof (uint64_t);
9261028Sbmc 
9271028Sbmc 			data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
9281028Sbmc 
9291028Sbmc 			for (i = 0; i < lim; i++) {
9301028Sbmc 				(void) snprintf(c, end - c, "%s%02x",
9311028Sbmc 				    i == 0 ? "" : " ", *data++);
9321028Sbmc 				c += strlen(c);
9331028Sbmc 			}
9341028Sbmc 
9351028Sbmc 			(void) snprintf(c, end - c,
9361028Sbmc 			    "%s)", lim < rec->dtrd_size ? " ..." : "");
9371028Sbmc 			BUFDUMPASSTR(rec, dtrd_offset, buf);
9381028Sbmc 		} else {
9391028Sbmc 			BUFDUMP(rec, dtrd_offset);
9401028Sbmc 		}
9411028Sbmc 
9421017Sbmc 		BUFDUMPHDR("");
9431017Sbmc 	}
9441017Sbmc 
9451017Sbmc 	if (agg != NULL) {
9461017Sbmc 		dtrace_aggdesc_t *desc = agg->dtada_desc;
9471017Sbmc 
9481017Sbmc 		BUFDUMPHDR("  dtrace_aggdesc");
9491017Sbmc 		BUFDUMPSTR(desc, dtagd_name);
9501017Sbmc 		BUFDUMP(desc, dtagd_varid);
9511017Sbmc 		BUFDUMP(desc, dtagd_id);
9521017Sbmc 		BUFDUMP(desc, dtagd_nrecs);
9531017Sbmc 		BUFDUMPHDR("");
9541017Sbmc 	}
9551017Sbmc 
9561017Sbmc 	return (DTRACE_HANDLE_OK);
9571017Sbmc }
9581017Sbmc 
959457Sbmc /*ARGSUSED*/
960457Sbmc static int
chewrec(const dtrace_probedata_t * data,const dtrace_recdesc_t * rec,void * arg)9610Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	dtrace_actkind_t act;
9640Sstevel@tonic-gate 	uintptr_t addr;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (rec == NULL) {
9670Sstevel@tonic-gate 		/*
9680Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
9690Sstevel@tonic-gate 		 * we're not in quiet mode.
9700Sstevel@tonic-gate 		 */
9710Sstevel@tonic-gate 		if (!g_quiet)
9720Sstevel@tonic-gate 			oprintf("\n");
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	act = rec->dtrd_action;
9780Sstevel@tonic-gate 	addr = (uintptr_t)data->dtpda_data;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (act == DTRACEACT_EXIT) {
9810Sstevel@tonic-gate 		g_status = *((uint32_t *)addr);
9820Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*ARGSUSED*/
9890Sstevel@tonic-gate static int
chew(const dtrace_probedata_t * data,void * arg)9900Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
9930Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
9940Sstevel@tonic-gate 	static int heading;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if (g_impatient) {
9970Sstevel@tonic-gate 		g_newline = 0;
9980Sstevel@tonic-gate 		return (DTRACE_CONSUME_ABORT);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	if (heading == 0) {
10020Sstevel@tonic-gate 		if (!g_flowindent) {
10030Sstevel@tonic-gate 			if (!g_quiet) {
10040Sstevel@tonic-gate 				oprintf("%3s %6s %32s\n",
10050Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
10060Sstevel@tonic-gate 			}
10070Sstevel@tonic-gate 		} else {
10080Sstevel@tonic-gate 			oprintf("%3s %-41s\n", "CPU", "FUNCTION");
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 		heading = 1;
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	if (!g_flowindent) {
10140Sstevel@tonic-gate 		if (!g_quiet) {
10150Sstevel@tonic-gate 			char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s:%s",
10180Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 			oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
10210Sstevel@tonic-gate 		}
10220Sstevel@tonic-gate 	} else {
10230Sstevel@tonic-gate 		int indent = data->dtpda_indent;
10240Sstevel@tonic-gate 		char *name;
10250Sstevel@tonic-gate 		size_t len;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
10280Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
10290Sstevel@tonic-gate 			name = alloca(len);
10300Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s:%s", indent, "",
10310Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func,
10320Sstevel@tonic-gate 			    pd->dtpd_name);
10330Sstevel@tonic-gate 		} else {
10340Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + 5;
10350Sstevel@tonic-gate 			name = alloca(len);
10360Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s", indent, "",
10370Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func);
10380Sstevel@tonic-gate 		}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 		oprintf("%3d %-41s ", cpu, name);
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate static void
go(void)10470Sstevel@tonic-gate go(void)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	int i;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	struct {
10520Sstevel@tonic-gate 		char *name;
10530Sstevel@tonic-gate 		char *optname;
10540Sstevel@tonic-gate 		dtrace_optval_t val;
10550Sstevel@tonic-gate 	} bufs[] = {
10560Sstevel@tonic-gate 		{ "buffer size", "bufsize" },
10570Sstevel@tonic-gate 		{ "aggregation size", "aggsize" },
10580Sstevel@tonic-gate 		{ "speculation size", "specsize" },
10590Sstevel@tonic-gate 		{ "dynamic variable size", "dynvarsize" },
10600Sstevel@tonic-gate 		{ NULL }
10610Sstevel@tonic-gate 	}, rates[] = {
10620Sstevel@tonic-gate 		{ "cleaning rate", "cleanrate" },
10630Sstevel@tonic-gate 		{ "status rate", "statusrate" },
10640Sstevel@tonic-gate 		{ NULL }
10650Sstevel@tonic-gate 	};
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
10680Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
10690Sstevel@tonic-gate 			fatal("couldn't get option %s", bufs[i].optname);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
10730Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
10740Sstevel@tonic-gate 			fatal("couldn't get option %s", rates[i].optname);
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (dtrace_go(g_dtp) == -1)
10780Sstevel@tonic-gate 		dfatal("could not enable tracing");
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
10810Sstevel@tonic-gate 		dtrace_optval_t j = 0, mul = 10;
10820Sstevel@tonic-gate 		dtrace_optval_t nsize;
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		if (bufs[i].val == DTRACEOPT_UNSET)
10850Sstevel@tonic-gate 			continue;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 		if (nsize == DTRACEOPT_UNSET || nsize == 0)
10900Sstevel@tonic-gate 			continue;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 		if (nsize >= bufs[i].val - sizeof (uint64_t))
10930Sstevel@tonic-gate 			continue;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
10960Sstevel@tonic-gate 			continue;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
10990Sstevel@tonic-gate 			error("%s lowered to %lld%c\n", bufs[i].name,
11000Sstevel@tonic-gate 			    (long long)nsize >> (mul - 10), " kmgtpe"[j]);
11010Sstevel@tonic-gate 		} else {
11020Sstevel@tonic-gate 			error("%s lowered to %lld bytes\n", bufs[i].name,
11030Sstevel@tonic-gate 			    (long long)nsize);
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
11080Sstevel@tonic-gate 		dtrace_optval_t nval;
11090Sstevel@tonic-gate 		char *dir;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		if (rates[i].val == DTRACEOPT_UNSET)
11120Sstevel@tonic-gate 			continue;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 		if (nval == DTRACEOPT_UNSET || nval == 0)
11170Sstevel@tonic-gate 			continue;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		if (rates[i].val == nval)
11200Sstevel@tonic-gate 			continue;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		dir = nval > rates[i].val ? "reduced" : "increased";
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
11250Sstevel@tonic-gate 			error("%s %s to %lld hz\n", rates[i].name, dir,
11260Sstevel@tonic-gate 			    (long long)NANOSEC / (long long)nval);
11270Sstevel@tonic-gate 			continue;
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 		if ((nval % NANOSEC) == 0) {
11310Sstevel@tonic-gate 			error("%s %s to once every %lld seconds\n",
11320Sstevel@tonic-gate 			    rates[i].name, dir,
11330Sstevel@tonic-gate 			    (long long)nval / (long long)NANOSEC);
11340Sstevel@tonic-gate 			continue;
11350Sstevel@tonic-gate 		}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 		error("%s %s to once every %lld nanoseconds\n",
11380Sstevel@tonic-gate 		    rates[i].name, dir, (long long)nval);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate /*ARGSUSED*/
11430Sstevel@tonic-gate static void
intr(int signo)11440Sstevel@tonic-gate intr(int signo)
11450Sstevel@tonic-gate {
11460Sstevel@tonic-gate 	if (!g_intr)
11470Sstevel@tonic-gate 		g_newline = 1;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (g_intr++)
11500Sstevel@tonic-gate 		g_impatient = 1;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate int
main(int argc,char * argv[])11540Sstevel@tonic-gate main(int argc, char *argv[])
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	dtrace_bufdesc_t buf;
11571222Smws 	struct sigaction act, oact;
11580Sstevel@tonic-gate 	dtrace_status_t status[2];
11590Sstevel@tonic-gate 	dtrace_optval_t opt;
11600Sstevel@tonic-gate 	dtrace_cmd_t *dcp;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	int done = 0, mode = 0;
11630Sstevel@tonic-gate 	int err, i;
11640Sstevel@tonic-gate 	char c, *p, **v;
11650Sstevel@tonic-gate 	struct ps_prochandle *P;
11660Sstevel@tonic-gate 	pid_t pid;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	g_pname = basename(argv[0]);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (argc == 1)
11710Sstevel@tonic-gate 		return (usage(stderr));
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
11740Sstevel@tonic-gate 	    (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
11750Sstevel@tonic-gate 	    (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
11760Sstevel@tonic-gate 		fatal("failed to allocate memory for arguments");
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	g_argv[g_argc++] = argv[0];	/* propagate argv[0] to D as $0/$$0 */
11790Sstevel@tonic-gate 	argv[0] = g_pname;		/* rewrite argv[0] for getopt errors */
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	bzero(status, sizeof (status));
11820Sstevel@tonic-gate 	bzero(&buf, sizeof (buf));
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	/*
11850Sstevel@tonic-gate 	 * Make an initial pass through argv[] processing any arguments that
11860Sstevel@tonic-gate 	 * affect our behavior mode (g_mode) and flags used for dtrace_open().
11870Sstevel@tonic-gate 	 * We also accumulate arguments that are not affiliated with getopt
11880Sstevel@tonic-gate 	 * options into g_argv[], and abort if any invalid options are found.
11890Sstevel@tonic-gate 	 */
11900Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
11910Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
11920Sstevel@tonic-gate 			switch (c) {
11930Sstevel@tonic-gate 			case '3':
11940Sstevel@tonic-gate 				if (strcmp(optarg, "2") != 0) {
11950Sstevel@tonic-gate 					(void) fprintf(stderr,
11960Sstevel@tonic-gate 					    "%s: illegal option -- 3%s\n",
11970Sstevel@tonic-gate 					    argv[0], optarg);
11980Sstevel@tonic-gate 					return (usage(stderr));
11990Sstevel@tonic-gate 				}
12000Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_LP64;
12010Sstevel@tonic-gate 				g_oflags |= DTRACE_O_ILP32;
12020Sstevel@tonic-gate 				break;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 			case '6':
12050Sstevel@tonic-gate 				if (strcmp(optarg, "4") != 0) {
12060Sstevel@tonic-gate 					(void) fprintf(stderr,
12070Sstevel@tonic-gate 					    "%s: illegal option -- 6%s\n",
12080Sstevel@tonic-gate 					    argv[0], optarg);
12090Sstevel@tonic-gate 					return (usage(stderr));
12100Sstevel@tonic-gate 				}
12110Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_ILP32;
12120Sstevel@tonic-gate 				g_oflags |= DTRACE_O_LP64;
12130Sstevel@tonic-gate 				break;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 			case 'a':
12160Sstevel@tonic-gate 				g_grabanon++; /* also checked in pass 2 below */
12170Sstevel@tonic-gate 				break;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 			case 'A':
12200Sstevel@tonic-gate 				g_mode = DMODE_ANON;
12210Sstevel@tonic-gate 				g_exec = 0;
12220Sstevel@tonic-gate 				mode++;
12230Sstevel@tonic-gate 				break;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 			case 'e':
12260Sstevel@tonic-gate 				g_exec = 0;
12270Sstevel@tonic-gate 				done = 1;
12280Sstevel@tonic-gate 				break;
12290Sstevel@tonic-gate 
12301399Sahl 			case 'h':
12311399Sahl 				g_mode = DMODE_HEADER;
12321399Sahl 				g_oflags |= DTRACE_O_NODEV;
12331399Sahl 				g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
12341399Sahl 				g_exec = 0;
12351399Sahl 				mode++;
12361399Sahl 				break;
12371399Sahl 
12380Sstevel@tonic-gate 			case 'G':
12390Sstevel@tonic-gate 				g_mode = DMODE_LINK;
12400Sstevel@tonic-gate 				g_oflags |= DTRACE_O_NODEV;
12410Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
12420Sstevel@tonic-gate 				g_exec = 0;
12430Sstevel@tonic-gate 				mode++;
12440Sstevel@tonic-gate 				break;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 			case 'l':
12470Sstevel@tonic-gate 				g_mode = DMODE_LIST;
12480Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
12490Sstevel@tonic-gate 				mode++;
12500Sstevel@tonic-gate 				break;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 			case 'V':
12530Sstevel@tonic-gate 				g_mode = DMODE_VERS;
12540Sstevel@tonic-gate 				mode++;
12550Sstevel@tonic-gate 				break;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 			default:
12580Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
12590Sstevel@tonic-gate 					return (usage(stderr));
12600Sstevel@tonic-gate 			}
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		if (optind < argc)
12640Sstevel@tonic-gate 			g_argv[g_argc++] = argv[optind];
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (mode > 1) {
12681399Sahl 		(void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
12690Sstevel@tonic-gate 		    "can be specified at a time\n", g_pname);
12700Sstevel@tonic-gate 		return (E_USAGE);
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (g_mode == DMODE_VERS)
12740Sstevel@tonic-gate 		return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	/*
1277*2769Sahl 	 * If we're in linker mode and the data model hasn't been specified,
1278*2769Sahl 	 * we try to guess the appropriate setting by examining the object
1279*2769Sahl 	 * files. We ignore certain errors since we'll catch them later when
1280*2769Sahl 	 * we actually process the object files.
1281*2769Sahl 	 */
1282*2769Sahl 	if (g_mode == DMODE_LINK &&
1283*2769Sahl 	    (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1284*2769Sahl 	    elf_version(EV_CURRENT) != EV_NONE) {
1285*2769Sahl 		int fd;
1286*2769Sahl 		Elf *elf;
1287*2769Sahl 		GElf_Ehdr ehdr;
1288*2769Sahl 
1289*2769Sahl 		for (i = 1; i < g_argc; i++) {
1290*2769Sahl 			if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1291*2769Sahl 				break;
1292*2769Sahl 
1293*2769Sahl 			if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1294*2769Sahl 				(void) close(fd);
1295*2769Sahl 				break;
1296*2769Sahl 			}
1297*2769Sahl 
1298*2769Sahl 			if (elf_kind(elf) != ELF_K_ELF ||
1299*2769Sahl 			    gelf_getehdr(elf, &ehdr) == NULL) {
1300*2769Sahl 				(void) close(fd);
1301*2769Sahl 				(void) elf_end(elf);
1302*2769Sahl 				break;
1303*2769Sahl 			}
1304*2769Sahl 
1305*2769Sahl 			(void) close(fd);
1306*2769Sahl 			(void) elf_end(elf);
1307*2769Sahl 
1308*2769Sahl 			if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1309*2769Sahl 				if (g_oflags & DTRACE_O_ILP32) {
1310*2769Sahl 					fatal("can't mix 32-bit and 64-bit "
1311*2769Sahl 					    "object files\n");
1312*2769Sahl 				}
1313*2769Sahl 				g_oflags |= DTRACE_O_LP64;
1314*2769Sahl 			} else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1315*2769Sahl 				if (g_oflags & DTRACE_O_LP64) {
1316*2769Sahl 					fatal("can't mix 32-bit and 64-bit "
1317*2769Sahl 					    "object files\n");
1318*2769Sahl 				}
1319*2769Sahl 				g_oflags |= DTRACE_O_ILP32;
1320*2769Sahl 			} else {
1321*2769Sahl 				break;
1322*2769Sahl 			}
1323*2769Sahl 		}
1324*2769Sahl 	}
1325*2769Sahl 
1326*2769Sahl 	/*
13270Sstevel@tonic-gate 	 * Open libdtrace.  If we are not actually going to be enabling any
13280Sstevel@tonic-gate 	 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
13310Sstevel@tonic-gate 		if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
13320Sstevel@tonic-gate 			g_oflags |= DTRACE_O_NODEV;
13330Sstevel@tonic-gate 			continue;
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
13370Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "bufsize", "4m");
13410Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "aggsize", "4m");
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
13450Sstevel@tonic-gate 	 * references to undefined symbols to remain as unresolved relocations.
13460Sstevel@tonic-gate 	 * If -A is specified, enable -xlink=primary to permit static linking
13470Sstevel@tonic-gate 	 * only to kernel symbols that are defined in a primary kernel module.
13480Sstevel@tonic-gate 	 */
13490Sstevel@tonic-gate 	if (g_mode == DMODE_LINK) {
13500Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
13510Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "unodefs", NULL);
13520Sstevel@tonic-gate 
1353*2769Sahl 		/*
1354*2769Sahl 		 * Use the remaining arguments as the list of object files
1355*2769Sahl 		 * when in linker mode.
1356*2769Sahl 		 */
1357*2769Sahl 		g_objc = g_argc - 1;
1358*2769Sahl 		g_objv = g_argv + 1;
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 		/*
13610Sstevel@tonic-gate 		 * We still use g_argv[0], the name of the executable.
13620Sstevel@tonic-gate 		 */
13630Sstevel@tonic-gate 		g_argc = 1;
13640Sstevel@tonic-gate 	} else if (g_mode == DMODE_ANON)
13650Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "primary");
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * Now that we have libdtrace open, make a second pass through argv[]
13690Sstevel@tonic-gate 	 * to perform any dtrace_setopt() calls and change any compiler flags.
13700Sstevel@tonic-gate 	 * We also accumulate any program specifications into our g_cmdv[] at
13710Sstevel@tonic-gate 	 * this time; these will compiled as part of the fourth processing pass.
13720Sstevel@tonic-gate 	 */
13730Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
13740Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
13750Sstevel@tonic-gate 			switch (c) {
13760Sstevel@tonic-gate 			case 'a':
13770Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
13780Sstevel@tonic-gate 					dfatal("failed to set -a");
13790Sstevel@tonic-gate 				break;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 			case 'b':
13820Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp,
13830Sstevel@tonic-gate 				    "bufsize", optarg) != 0)
13840Sstevel@tonic-gate 					dfatal("failed to set -b %s", optarg);
13850Sstevel@tonic-gate 				break;
13860Sstevel@tonic-gate 
13871017Sbmc 			case 'B':
13881017Sbmc 				g_ofp = NULL;
13891017Sbmc 				break;
13901017Sbmc 
13910Sstevel@tonic-gate 			case 'C':
13920Sstevel@tonic-gate 				g_cflags |= DTRACE_C_CPP;
13930Sstevel@tonic-gate 				break;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 			case 'D':
13960Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "define", optarg) != 0)
13970Sstevel@tonic-gate 					dfatal("failed to set -D %s", optarg);
13980Sstevel@tonic-gate 				break;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 			case 'f':
14010Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14020Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14030Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
14040Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14050Sstevel@tonic-gate 				break;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 			case 'F':
14080Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
14090Sstevel@tonic-gate 					dfatal("failed to set -F");
14100Sstevel@tonic-gate 				break;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 			case 'H':
14130Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
14140Sstevel@tonic-gate 					dfatal("failed to set -H");
14150Sstevel@tonic-gate 				break;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 			case 'i':
14180Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14190Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14200Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
14210Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14220Sstevel@tonic-gate 				break;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 			case 'I':
14250Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
14260Sstevel@tonic-gate 					dfatal("failed to set -I %s", optarg);
14270Sstevel@tonic-gate 				break;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 			case 'L':
14300Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
14310Sstevel@tonic-gate 					dfatal("failed to set -L %s", optarg);
14320Sstevel@tonic-gate 				break;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 			case 'm':
14350Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14360Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14370Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_MOD;
14380Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14390Sstevel@tonic-gate 				break;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 			case 'n':
14420Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14430Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14440Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
14450Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14460Sstevel@tonic-gate 				break;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 			case 'P':
14490Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14500Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14510Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
14520Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14530Sstevel@tonic-gate 				break;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 			case 'q':
14560Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
14570Sstevel@tonic-gate 					dfatal("failed to set -q");
14580Sstevel@tonic-gate 				break;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 			case 'o':
14610Sstevel@tonic-gate 				g_ofile = optarg;
14620Sstevel@tonic-gate 				break;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 			case 's':
14650Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14660Sstevel@tonic-gate 				dcp->dc_func = compile_file;
14670Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NONE;
14680Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14690Sstevel@tonic-gate 				break;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 			case 'S':
14720Sstevel@tonic-gate 				g_cflags |= DTRACE_C_DIFV;
14730Sstevel@tonic-gate 				break;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 			case 'U':
14760Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
14770Sstevel@tonic-gate 					dfatal("failed to set -U %s", optarg);
14780Sstevel@tonic-gate 				break;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 			case 'v':
14810Sstevel@tonic-gate 				g_verbose++;
14820Sstevel@tonic-gate 				break;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 			case 'w':
14850Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
14860Sstevel@tonic-gate 					dfatal("failed to set -w");
14870Sstevel@tonic-gate 				break;
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 			case 'x':
14900Sstevel@tonic-gate 				if ((p = strchr(optarg, '=')) != NULL)
14910Sstevel@tonic-gate 					*p++ = '\0';
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, optarg, p) != 0)
14940Sstevel@tonic-gate 					dfatal("failed to set -x %s", optarg);
14950Sstevel@tonic-gate 				break;
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 			case 'X':
14980Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
14990Sstevel@tonic-gate 					dfatal("failed to set -X %s", optarg);
15000Sstevel@tonic-gate 				break;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 			case 'Z':
15030Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS;
15040Sstevel@tonic-gate 				break;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 			default:
15070Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
15080Sstevel@tonic-gate 					return (usage(stderr));
15090Sstevel@tonic-gate 			}
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate 
15131017Sbmc 	if (g_ofp == NULL && g_mode != DMODE_EXEC) {
15141017Sbmc 		(void) fprintf(stderr, "%s: -B not valid in combination"
15151017Sbmc 		    " with [-AGl] options\n", g_pname);
15161017Sbmc 		return (E_USAGE);
15171017Sbmc 	}
15181017Sbmc 
15191017Sbmc 	if (g_ofp == NULL && g_ofile != NULL) {
15201017Sbmc 		(void) fprintf(stderr, "%s: -B not valid in combination"
15211017Sbmc 		    " with -o option\n", g_pname);
15221017Sbmc 		return (E_USAGE);
15231017Sbmc 	}
15241017Sbmc 
15250Sstevel@tonic-gate 	/*
15260Sstevel@tonic-gate 	 * In our third pass we handle any command-line options related to
15270Sstevel@tonic-gate 	 * grabbing or creating victim processes.  The behavior of these calls
15280Sstevel@tonic-gate 	 * may been affected by any library options set by the second pass.
15290Sstevel@tonic-gate 	 */
15300Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
15310Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
15320Sstevel@tonic-gate 			switch (c) {
15330Sstevel@tonic-gate 			case 'c':
15340Sstevel@tonic-gate 				if ((v = make_argv(optarg)) == NULL)
15350Sstevel@tonic-gate 					fatal("failed to allocate memory");
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 				P = dtrace_proc_create(g_dtp, v[0], v);
15380Sstevel@tonic-gate 				if (P == NULL)
15390Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 				g_psv[g_psc++] = P;
15420Sstevel@tonic-gate 				free(v);
15430Sstevel@tonic-gate 				break;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 			case 'p':
15460Sstevel@tonic-gate 				errno = 0;
15470Sstevel@tonic-gate 				pid = strtol(optarg, &p, 10);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 				if (errno != 0 || p == optarg || p[0] != '\0')
15500Sstevel@tonic-gate 					fatal("invalid pid: %s\n", optarg);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 				P = dtrace_proc_grab(g_dtp, pid, 0);
15530Sstevel@tonic-gate 				if (P == NULL)
15540Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 				g_psv[g_psc++] = P;
15570Sstevel@tonic-gate 				break;
15580Sstevel@tonic-gate 			}
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15630Sstevel@tonic-gate 	 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
15640Sstevel@tonic-gate 	 * each string or file specification into a compiled program structure.
15650Sstevel@tonic-gate 	 */
15660Sstevel@tonic-gate 	for (i = 0; i < g_cmdc; i++)
15670Sstevel@tonic-gate 		g_cmdv[i].dc_func(&g_cmdv[i]);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	if (g_mode != DMODE_LIST) {
15700Sstevel@tonic-gate 		if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
15710Sstevel@tonic-gate 			dfatal("failed to establish error handler");
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
15740Sstevel@tonic-gate 			dfatal("failed to establish drop handler");
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 		if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
15770Sstevel@tonic-gate 			dfatal("failed to establish proc handler");
1578457Sbmc 
1579457Sbmc 		if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1580457Sbmc 			dfatal("failed to establish setopt handler");
15811017Sbmc 
15821017Sbmc 		if (g_ofp == NULL &&
15831017Sbmc 		    dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
15841017Sbmc 			dfatal("failed to establish buffered handler");
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
15880Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
15910Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
15940Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	/*
15970Sstevel@tonic-gate 	 * Now make a fifth and final pass over the options that have been
15980Sstevel@tonic-gate 	 * turned into programs and saved in g_cmdv[], performing any mode-
15990Sstevel@tonic-gate 	 * specific processing.  If g_mode is DMODE_EXEC, we will break out
16000Sstevel@tonic-gate 	 * of the switch() and continue on to the data processing loop.  For
16010Sstevel@tonic-gate 	 * other modes, we will exit dtrace once mode-specific work is done.
16020Sstevel@tonic-gate 	 */
16030Sstevel@tonic-gate 	switch (g_mode) {
16040Sstevel@tonic-gate 	case DMODE_EXEC:
16050Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
16060Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
16090Sstevel@tonic-gate 			exec_prog(&g_cmdv[i]);
16100Sstevel@tonic-gate 
1611265Smws 		if (done && !g_grabanon) {
1612265Smws 			dtrace_close(g_dtp);
16130Sstevel@tonic-gate 			return (g_status);
1614265Smws 		}
16150Sstevel@tonic-gate 		break;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	case DMODE_ANON:
16180Sstevel@tonic-gate 		if (g_ofile == NULL)
16190Sstevel@tonic-gate 			g_ofile = "/kernel/drv/dtrace.conf";
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		dof_prune(g_ofile); /* strip out any old DOF directives */
16220Sstevel@tonic-gate 		etcsystem_prune(); /* string out any forceload directives */
16230Sstevel@tonic-gate 
1624265Smws 		if (g_cmdc == 0) {
1625265Smws 			dtrace_close(g_dtp);
16260Sstevel@tonic-gate 			return (g_status);
1627265Smws 		}
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 		if ((g_ofp = fopen(g_ofile, "a")) == NULL)
16300Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++) {
16330Sstevel@tonic-gate 			anon_prog(&g_cmdv[i],
16340Sstevel@tonic-gate 			    dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 		/*
16380Sstevel@tonic-gate 		 * Dump out the DOF corresponding to the error handler and the
16390Sstevel@tonic-gate 		 * current options as the final DOF property in the .conf file.
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 		anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
16420Sstevel@tonic-gate 		anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 		if (fclose(g_ofp) == EOF)
16450Sstevel@tonic-gate 			fatal("failed to close output file '%s'", g_ofile);
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 		/*
16480Sstevel@tonic-gate 		 * These messages would use notice() rather than error(), but
16490Sstevel@tonic-gate 		 * we don't want them suppressed when -A is run on a D program
16500Sstevel@tonic-gate 		 * that itself contains a #pragma D option quiet.
16510Sstevel@tonic-gate 		 */
16520Sstevel@tonic-gate 		error("saved anonymous enabling in %s\n", g_ofile);
16530Sstevel@tonic-gate 		etcsystem_add();
16540Sstevel@tonic-gate 		error("run update_drv(1M) or reboot to enable changes\n");
16550Sstevel@tonic-gate 
1656265Smws 		dtrace_close(g_dtp);
16570Sstevel@tonic-gate 		return (g_status);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	case DMODE_LINK:
16601222Smws 		if (g_cmdc == 0) {
16611222Smws 			(void) fprintf(stderr, "%s: -G requires one or more "
16621222Smws 			    "scripts or enabling options\n", g_pname);
16631222Smws 			dtrace_close(g_dtp);
16641222Smws 			return (E_USAGE);
16651222Smws 		}
16661222Smws 
16670Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
16680Sstevel@tonic-gate 			link_prog(&g_cmdv[i]);
1669191Sahl 
1670191Sahl 		if (g_cmdc > 1 && g_ofile != NULL) {
1671191Sahl 			char **objv = alloca(g_cmdc * sizeof (char *));
1672191Sahl 
1673191Sahl 			for (i = 0; i < g_cmdc; i++)
1674191Sahl 				objv[i] = g_cmdv[i].dc_ofile;
1675191Sahl 
1676191Sahl 			if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1677191Sahl 			    g_ofile, g_cmdc, objv) != 0)
1678265Smws 				dfatal(NULL); /* dtrace_errmsg() only */
1679191Sahl 		}
1680191Sahl 
1681265Smws 		dtrace_close(g_dtp);
16820Sstevel@tonic-gate 		return (g_status);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	case DMODE_LIST:
16850Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
16860Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 		oprintf("%5s %10s %17s %33s %s\n",
16890Sstevel@tonic-gate 		    "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
16920Sstevel@tonic-gate 			list_prog(&g_cmdv[i]);
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 		if (g_cmdc == 0)
16950Sstevel@tonic-gate 			(void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
16960Sstevel@tonic-gate 
1697265Smws 		dtrace_close(g_dtp);
16980Sstevel@tonic-gate 		return (g_status);
16991399Sahl 
17001399Sahl 	case DMODE_HEADER:
17011399Sahl 		if (g_cmdc == 0) {
17021399Sahl 			(void) fprintf(stderr, "%s: -h requires one or more "
17031399Sahl 			    "scripts or enabling options\n", g_pname);
17041399Sahl 			dtrace_close(g_dtp);
17051399Sahl 			return (E_USAGE);
17061399Sahl 		}
17071399Sahl 
17081399Sahl 		if (g_ofile == NULL) {
17091399Sahl 			char *p;
17101399Sahl 
17111399Sahl 			if (g_cmdc > 1) {
17121399Sahl 				(void) fprintf(stderr, "%s: -h requires an "
17131399Sahl 				    "output file if multiple scripts are "
17141399Sahl 				    "specified\n", g_pname);
17151399Sahl 				dtrace_close(g_dtp);
17161399Sahl 				return (E_USAGE);
17171399Sahl 			}
17181399Sahl 
17191399Sahl 			if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
17201399Sahl 			    strcmp(p, ".d") != 0) {
17211399Sahl 				(void) fprintf(stderr, "%s: -h requires an "
17221399Sahl 				    "output file if no scripts are "
17231399Sahl 				    "specified\n", g_pname);
17241399Sahl 				dtrace_close(g_dtp);
17251399Sahl 				return (E_USAGE);
17261399Sahl 			}
17271399Sahl 
17281399Sahl 			p[0] = '\0'; /* strip .d suffix */
17291399Sahl 			g_ofile = p = g_cmdv[0].dc_ofile;
17301399Sahl 			(void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
17311399Sahl 			    "%s.h", basename(g_cmdv[0].dc_arg));
17321399Sahl 		}
17331399Sahl 
17341399Sahl 		if ((g_ofp = fopen(g_ofile, "w")) == NULL)
17351399Sahl 			fatal("failed to open header file '%s'", g_ofile);
17361399Sahl 
17371399Sahl 		oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
17381399Sahl 
17391399Sahl 		if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
17401399Sahl 		    fclose(g_ofp) == EOF)
17411399Sahl 			dfatal("failed to create header file %s", g_ofile);
17421399Sahl 
17431399Sahl 		dtrace_close(g_dtp);
17441399Sahl 		return (g_status);
17450Sstevel@tonic-gate 	}
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	/*
17480Sstevel@tonic-gate 	 * If -a and -Z were not specified and no probes have been matched, no
17490Sstevel@tonic-gate 	 * probe criteria was specified on the command line and we abort.
17500Sstevel@tonic-gate 	 */
17510Sstevel@tonic-gate 	if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
17520Sstevel@tonic-gate 		dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/*
17550Sstevel@tonic-gate 	 * Start tracing.  Once we dtrace_go(), reload any options that affect
17560Sstevel@tonic-gate 	 * our globals in case consuming anonymous state has changed them.
17570Sstevel@tonic-gate 	 */
17580Sstevel@tonic-gate 	go();
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
17610Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
17640Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
17670Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "destructive", &opt);
17700Sstevel@tonic-gate 	if (opt != DTRACEOPT_UNSET)
17710Sstevel@tonic-gate 		notice("allowing destructive actions\n");
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
17740Sstevel@tonic-gate 	act.sa_flags = 0;
17750Sstevel@tonic-gate 	act.sa_handler = intr;
17761222Smws 
17771222Smws 	if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
17781222Smws 		(void) sigaction(SIGINT, &act, NULL);
17791222Smws 
17801222Smws 	if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
17811222Smws 		(void) sigaction(SIGTERM, &act, NULL);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * Now that tracing is active and we are ready to consume trace data,
17850Sstevel@tonic-gate 	 * continue any grabbed or created processes, setting them running
17860Sstevel@tonic-gate 	 * using the /proc control mechanism inside of libdtrace.
17870Sstevel@tonic-gate 	 */
17880Sstevel@tonic-gate 	for (i = 0; i < g_psc; i++)
17890Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_psv[i]);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	g_pslive = g_psc; /* count for prochandler() */
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	do {
17940Sstevel@tonic-gate 		if (!g_intr && !done)
17950Sstevel@tonic-gate 			dtrace_sleep(g_dtp);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 		if (g_newline) {
17980Sstevel@tonic-gate 			/*
17990Sstevel@tonic-gate 			 * Output a newline just to make the output look
18000Sstevel@tonic-gate 			 * slightly cleaner.  Note that we do this even in
18010Sstevel@tonic-gate 			 * "quiet" mode...
18020Sstevel@tonic-gate 			 */
18030Sstevel@tonic-gate 			oprintf("\n");
18040Sstevel@tonic-gate 			g_newline = 0;
18050Sstevel@tonic-gate 		}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 		if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
18080Sstevel@tonic-gate 			done = 1;
18090Sstevel@tonic-gate 			if (dtrace_stop(g_dtp) == -1)
18100Sstevel@tonic-gate 				dfatal("couldn't stop tracing");
18110Sstevel@tonic-gate 		}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 		switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
18140Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_DONE:
18150Sstevel@tonic-gate 			done = 1;
18160Sstevel@tonic-gate 			break;
18170Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_OKAY:
18180Sstevel@tonic-gate 			break;
18190Sstevel@tonic-gate 		default:
18200Sstevel@tonic-gate 			if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
18210Sstevel@tonic-gate 				dfatal("processing aborted");
18220Sstevel@tonic-gate 		}
18230Sstevel@tonic-gate 
18241017Sbmc 		if (g_ofp != NULL && fflush(g_ofp) == EOF)
18250Sstevel@tonic-gate 			clearerr(g_ofp);
18260Sstevel@tonic-gate 	} while (!done);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	oprintf("\n");
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	if (!g_impatient) {
18310Sstevel@tonic-gate 		if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
18320Sstevel@tonic-gate 		    dtrace_errno(g_dtp) != EINTR)
18330Sstevel@tonic-gate 			dfatal("failed to print aggregations");
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	dtrace_close(g_dtp);
18370Sstevel@tonic-gate 	return (g_status);
18380Sstevel@tonic-gate }
1839