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