xref: /onnv-gate/usr/src/cmd/dtrace/dtrace.c (revision 265)
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[] =
680Sstevel@tonic-gate 	"3:6:aAb:c: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 
2490Sstevel@tonic-gate 	va_start(ap, fmt);
2500Sstevel@tonic-gate 	n = vfprintf(g_ofp, fmt, ap);
2510Sstevel@tonic-gate 	va_end(ap);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (n < 0) {
2540Sstevel@tonic-gate 		if (errno != EINTR) {
2550Sstevel@tonic-gate 			fatal("failed to write to %s",
2560Sstevel@tonic-gate 			    g_ofile ? g_ofile : "<stdout>");
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 		clearerr(g_ofp);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static char **
2630Sstevel@tonic-gate make_argv(char *s)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	const char *ws = "\f\n\r\t\v ";
2660Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
2670Sstevel@tonic-gate 	int argc = 0;
2680Sstevel@tonic-gate 	char *p = s;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (argv == NULL)
2710Sstevel@tonic-gate 		return (NULL);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
2740Sstevel@tonic-gate 		argv[argc++] = p;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (argc == 0)
2770Sstevel@tonic-gate 		argv[argc++] = s;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	argv[argc] = NULL;
2800Sstevel@tonic-gate 	return (argv);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static void
2840Sstevel@tonic-gate dof_prune(const char *fname)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	struct stat sbuf;
2870Sstevel@tonic-gate 	size_t sz, i, j, mark, len;
2880Sstevel@tonic-gate 	char *buf;
2890Sstevel@tonic-gate 	int msg = 0, fd;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
2920Sstevel@tonic-gate 		/*
2930Sstevel@tonic-gate 		 * This is okay only if the file doesn't exist at all.
2940Sstevel@tonic-gate 		 */
2950Sstevel@tonic-gate 		if (errno != ENOENT)
2960Sstevel@tonic-gate 			fatal("failed to open %s", fname);
2970Sstevel@tonic-gate 		return;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3010Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3040Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3070Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	buf[sz] = '\0';
3100Sstevel@tonic-gate 	(void) close(fd);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
3130Sstevel@tonic-gate 		fatal("failed to open %s for writing", fname);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	len = strlen("dof-data-");
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	for (mark = 0, i = 0; i < sz; i++) {
3180Sstevel@tonic-gate 		if (strncmp(&buf[i], "dof-data-", len) != 0)
3190Sstevel@tonic-gate 			continue;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		/*
3220Sstevel@tonic-gate 		 * This is only a match if it's in the 0th column.
3230Sstevel@tonic-gate 		 */
3240Sstevel@tonic-gate 		if (i != 0 && buf[i - 1] != '\n')
3250Sstevel@tonic-gate 			continue;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		if (msg++ == 0) {
3280Sstevel@tonic-gate 			error("cleaned up old anonymous "
3290Sstevel@tonic-gate 			    "enabling in %s\n", fname);
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		/*
3330Sstevel@tonic-gate 		 * We have a match.  First write out our data up until now.
3340Sstevel@tonic-gate 		 */
3350Sstevel@tonic-gate 		if (i != mark) {
3360Sstevel@tonic-gate 			if (write(fd, &buf[mark], i - mark) != i - mark)
3370Sstevel@tonic-gate 				fatal("failed to write to %s", fname);
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * Now scan forward until we scan past a newline.
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		for (j = i; j < sz && buf[j] != '\n'; j++)
3440Sstevel@tonic-gate 			continue;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		/*
3470Sstevel@tonic-gate 		 * Reset our mark.
3480Sstevel@tonic-gate 		 */
3490Sstevel@tonic-gate 		if ((mark = j + 1) >= sz)
3500Sstevel@tonic-gate 			break;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		i = j;
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (mark < sz) {
3560Sstevel@tonic-gate 		if (write(fd, &buf[mark], sz - mark) != sz - mark)
3570Sstevel@tonic-gate 			fatal("failed to write to %s", fname);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	(void) close(fd);
3610Sstevel@tonic-gate 	free(buf);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate static void
3650Sstevel@tonic-gate etcsystem_prune(void)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	struct stat sbuf;
3680Sstevel@tonic-gate 	size_t sz;
3690Sstevel@tonic-gate 	char *buf, *start, *end;
3700Sstevel@tonic-gate 	int fd;
3710Sstevel@tonic-gate 	char *fname = g_etcfile, *tmpname;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1)
3740Sstevel@tonic-gate 		fatal("failed to open %s", fname);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3770Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3800Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3830Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	buf[sz] = '\0';
3860Sstevel@tonic-gate 	(void) close(fd);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if ((start = strstr(buf, g_etcbegin)) == NULL)
3890Sstevel@tonic-gate 		goto out;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (strlen(buf) != sz) {
3920Sstevel@tonic-gate 		fatal("embedded nul byte in %s; manual repair of %s "
3930Sstevel@tonic-gate 		    "required\n", fname, fname);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (strstr(start + 1, g_etcbegin) != NULL) {
3970Sstevel@tonic-gate 		fatal("multiple start sentinels in %s; manual repair of %s "
3980Sstevel@tonic-gate 		    "required\n", fname, fname);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if ((end = strstr(buf, g_etcend)) == NULL) {
4020Sstevel@tonic-gate 		fatal("missing end sentinel in %s; manual repair of %s "
4030Sstevel@tonic-gate 		    "required\n", fname, fname);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (start > end) {
4070Sstevel@tonic-gate 		fatal("end sentinel preceeds start sentinel in %s; manual "
4080Sstevel@tonic-gate 		    "repair of %s required\n", fname, fname);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	end += strlen(g_etcend) + 1;
4120Sstevel@tonic-gate 	bcopy(end, start, strlen(end) + 1);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	tmpname = alloca(sz = strlen(fname) + 80);
4150Sstevel@tonic-gate 	(void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if ((fd = open(tmpname,
4180Sstevel@tonic-gate 	    O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
4190Sstevel@tonic-gate 		fatal("failed to create %s", tmpname);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (write(fd, buf, strlen(buf)) < strlen(buf)) {
4220Sstevel@tonic-gate 		(void) unlink(tmpname);
4230Sstevel@tonic-gate 		fatal("failed to write to %s", tmpname);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	(void) close(fd);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
4290Sstevel@tonic-gate 		(void) unlink(tmpname);
4300Sstevel@tonic-gate 		fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
4310Sstevel@tonic-gate 		    (int)sbuf.st_uid, (int)sbuf.st_gid);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (rename(tmpname, fname) == -1)
4350Sstevel@tonic-gate 		fatal("rename of %s to %s failed", tmpname, fname);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	error("cleaned up forceload directives in %s\n", fname);
4380Sstevel@tonic-gate out:
4390Sstevel@tonic-gate 	free(buf);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate static void
4430Sstevel@tonic-gate etcsystem_add(void)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	const char *mods[20];
4460Sstevel@tonic-gate 	int nmods, line;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
4490Sstevel@tonic-gate 		fatal("failed to open output file '%s'", g_ofile);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	oprintf("%s\n", g_etcbegin);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	for (line = 0; g_etc[line] != NULL; line++)
4540Sstevel@tonic-gate 		oprintf("%s\n", g_etc[line]);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	nmods = dtrace_provider_modules(g_dtp, mods,
4570Sstevel@tonic-gate 	    sizeof (mods) / sizeof (char *) - 1);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (nmods >= sizeof (mods) / sizeof (char *))
4600Sstevel@tonic-gate 		fatal("unexpectedly large number of modules!");
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	mods[nmods++] = "dtrace";
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	for (line = 0; line < nmods; line++)
4650Sstevel@tonic-gate 		oprintf("forceload: drv/%s\n", mods[line]);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	oprintf("%s\n", g_etcend);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (fclose(g_ofp) == EOF)
4700Sstevel@tonic-gate 		fatal("failed to close output file '%s'", g_ofile);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	error("added forceload directives to %s\n", g_ofile);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate static void
4760Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	char buf[BUFSIZ];
4790Sstevel@tonic-gate 	int i;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	oprintf("\n\tProbe Description Attributes\n");
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
4840Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_name));
4850Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
4860Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_data));
4870Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
4880Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_attr.dtat_class));
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	oprintf("\n\tArgument Attributes\n");
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
4930Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_name));
4940Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
4950Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_data));
4960Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
4970Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_arga.dtat_class));
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	oprintf("\n\tArgument Types\n");
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	for (i = 0; i < p->dtp_argc; i++) {
5020Sstevel@tonic-gate 		if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
5030Sstevel@tonic-gate 		    p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
5040Sstevel@tonic-gate 			(void) strlcpy(buf, "(unknown)", sizeof (buf));
5050Sstevel@tonic-gate 		oprintf("\t\targs[%d]: %s\n", i, buf);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (p->dtp_argc == 0)
5090Sstevel@tonic-gate 		oprintf("\t\tNone\n");
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	oprintf("\n");
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*ARGSUSED*/
5150Sstevel@tonic-gate static int
5160Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
5170Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
5200Sstevel@tonic-gate 	dtrace_probedesc_t *pdp = &edp->dted_probe;
5210Sstevel@tonic-gate 	dtrace_probeinfo_t p;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (edp == *last)
5240Sstevel@tonic-gate 		return (0);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	oprintf("\n%s:%s:%s:%s\n",
5270Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (dtrace_probe_info(dtp, pdp, &p) == 0)
5300Sstevel@tonic-gate 		print_probe_info(&p);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	*last = edp;
5330Sstevel@tonic-gate 	return (0);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate /*
5370Sstevel@tonic-gate  * Execute the specified program by enabling the corresponding instrumentation.
5380Sstevel@tonic-gate  * If -e has been specified, we get the program info but do not enable it.  If
5390Sstevel@tonic-gate  * -v has been specified, we print a stability report for the program.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate static void
5420Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
5450Sstevel@tonic-gate 	dtrace_proginfo_t dpi;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (!g_exec) {
5480Sstevel@tonic-gate 		dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
5490Sstevel@tonic-gate 	} else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
5500Sstevel@tonic-gate 		dfatal("failed to enable '%s'", dcp->dc_name);
5510Sstevel@tonic-gate 	} else {
5520Sstevel@tonic-gate 		notice("%s '%s' matched %u probe%s\n",
5530Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name,
5540Sstevel@tonic-gate 		    dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (g_verbose) {
5580Sstevel@tonic-gate 		oprintf("\nStability attributes for %s %s:\n",
5590Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		oprintf("\n\tMinimum Probe Description Attributes\n");
5620Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5630Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_name));
5640Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5650Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_data));
5660Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5670Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_descattr.dtat_class));
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		oprintf("\n\tMinimum Statement Attributes\n");
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5720Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
5730Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5740Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
5750Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5760Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		if (!g_exec) {
5790Sstevel@tonic-gate 			(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
5800Sstevel@tonic-gate 			    (dtrace_stmt_f *)info_stmt, &last);
5810Sstevel@tonic-gate 		} else
5820Sstevel@tonic-gate 			oprintf("\n");
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	g_total += dpi.dpi_matches;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate  * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
5900Sstevel@tonic-gate  * storing in a driver.conf(4) file associated with the dtrace driver.
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate static void
5930Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	const uchar_t *p, *q;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (dof == NULL)
5980Sstevel@tonic-gate 		dfatal("failed to create DOF image for '%s'", dcp->dc_name);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	p = (uchar_t *)dof;
6010Sstevel@tonic-gate 	q = p + dof->dofh_loadsz;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	oprintf("dof-data-%d=0x%x", n, *p++);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	while (p < q)
6060Sstevel@tonic-gate 		oprintf(",0x%x", *p++);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	oprintf(";\n");
6090Sstevel@tonic-gate 	dtrace_dof_destroy(g_dtp, dof);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate  * Link the specified D program in DOF form into an ELF file for use in either
6140Sstevel@tonic-gate  * helpers, userland provider definitions, or both.  If -o was specified, that
6150Sstevel@tonic-gate  * path is used as the output file name.  If -o wasn't specified and the input
6160Sstevel@tonic-gate  * program is from a script whose name is %.d, use basename(%.o) as the output
6170Sstevel@tonic-gate  * file name.  Otherwise we use "d.out" as the default output file name.
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate static void
620191Sahl link_prog(dtrace_cmd_t *dcp)
6210Sstevel@tonic-gate {
622191Sahl 	char *p;
6230Sstevel@tonic-gate 
624191Sahl 	if (g_cmdc == 1 && g_ofile != NULL) {
625191Sahl 		(void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
6260Sstevel@tonic-gate 	} else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
6270Sstevel@tonic-gate 	    strcmp(p, ".d") == 0) {
6280Sstevel@tonic-gate 		p[0] = '\0'; /* strip .d suffix */
629191Sahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
6300Sstevel@tonic-gate 		    "%s.o", basename(dcp->dc_arg));
6310Sstevel@tonic-gate 	} else {
632191Sahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
633191Sahl 		    g_cmdc > 1 ?  "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
637191Sahl 	    dcp->dc_ofile, g_objc - 1, g_objv + 1) != 0)
6380Sstevel@tonic-gate 		dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*ARGSUSED*/
6420Sstevel@tonic-gate static int
6430Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	dtrace_probeinfo_t p;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
6480Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
6510Sstevel@tonic-gate 		print_probe_info(&p);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	return (0);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*ARGSUSED*/
6570Sstevel@tonic-gate static int
6580Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
6590Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (edp == *last)
6640Sstevel@tonic-gate 		return (0);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
6670Sstevel@tonic-gate 		error("failed to match %s:%s:%s:%s: %s\n",
6680Sstevel@tonic-gate 		    edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
6690Sstevel@tonic-gate 		    edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
6700Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	*last = edp;
6740Sstevel@tonic-gate 	return (0);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate  * List the probes corresponding to the specified program by iterating over
6790Sstevel@tonic-gate  * each statement and then matching probes to the statement probe descriptions.
6800Sstevel@tonic-gate  */
6810Sstevel@tonic-gate static void
6820Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
6870Sstevel@tonic-gate 	    (dtrace_stmt_f *)list_stmt, &last);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate static void
6910Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	char *arg0;
6940Sstevel@tonic-gate 	FILE *fp;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
6970Sstevel@tonic-gate 		fatal("failed to open %s", dcp->dc_arg);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	arg0 = g_argv[0];
7000Sstevel@tonic-gate 	g_argv[0] = dcp->dc_arg;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
7030Sstevel@tonic-gate 	    g_cflags, g_argc, g_argv)) == NULL)
7040Sstevel@tonic-gate 		dfatal("failed to compile script %s", dcp->dc_arg);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	g_argv[0] = arg0;
7070Sstevel@tonic-gate 	(void) fclose(fp);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	dcp->dc_desc = "script";
7100Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate static void
7140Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	char *p;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
7190Sstevel@tonic-gate 	    dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
7200Sstevel@tonic-gate 		dfatal("invalid probe specifier %s", dcp->dc_arg);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
7230Sstevel@tonic-gate 		*p = '\0'; /* crop name for reporting */
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	dcp->dc_desc = "description";
7260Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*ARGSUSED*/
7300Sstevel@tonic-gate static void
7310Sstevel@tonic-gate prochandler(struct ps_prochandle *P, void *arg)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
7340Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
7350Sstevel@tonic-gate 	char name[SIG2STR_MAX];
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	switch (Pstate(P)) {
7380Sstevel@tonic-gate 	case PS_UNDEAD:
7390Sstevel@tonic-gate 		/*
7400Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
7410Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
7420Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
7430Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
7440Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
7450Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
7460Sstevel@tonic-gate 		 */
7470Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
7480Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
7490Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
7500Sstevel@tonic-gate 			    name, sizeof (name)));
7510Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
7520Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
7530Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
7540Sstevel@tonic-gate 		} else {
7550Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		g_pslive--;
7580Sstevel@tonic-gate 		break;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	case PS_LOST:
7610Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
7620Sstevel@tonic-gate 		g_pslive--;
7630Sstevel@tonic-gate 		break;
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate /*ARGSUSED*/
7680Sstevel@tonic-gate static int
7690Sstevel@tonic-gate errhandler(dtrace_errdata_t *data, void *arg)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	error(data->dteda_msg);
7720Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*ARGSUSED*/
7760Sstevel@tonic-gate static int
7770Sstevel@tonic-gate drophandler(dtrace_dropdata_t *data, void *arg)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	error(data->dtdda_msg);
7800Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*ARGSUSED*/
7840Sstevel@tonic-gate static int
7850Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	dtrace_actkind_t act;
7880Sstevel@tonic-gate 	uintptr_t addr;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (rec == NULL) {
7910Sstevel@tonic-gate 		/*
7920Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
7930Sstevel@tonic-gate 		 * we're not in quiet mode.
7940Sstevel@tonic-gate 		 */
7950Sstevel@tonic-gate 		if (!g_quiet)
7960Sstevel@tonic-gate 			oprintf("\n");
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	act = rec->dtrd_action;
8020Sstevel@tonic-gate 	addr = (uintptr_t)data->dtpda_data;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (act == DTRACEACT_EXIT) {
8050Sstevel@tonic-gate 		g_status = *((uint32_t *)addr);
8060Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /*ARGSUSED*/
8130Sstevel@tonic-gate static int
8140Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg)
8150Sstevel@tonic-gate {
8160Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
8170Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
8180Sstevel@tonic-gate 	static int heading;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (g_impatient) {
8210Sstevel@tonic-gate 		g_newline = 0;
8220Sstevel@tonic-gate 		return (DTRACE_CONSUME_ABORT);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if (heading == 0) {
8260Sstevel@tonic-gate 		if (!g_flowindent) {
8270Sstevel@tonic-gate 			if (!g_quiet) {
8280Sstevel@tonic-gate 				oprintf("%3s %6s %32s\n",
8290Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
8300Sstevel@tonic-gate 			}
8310Sstevel@tonic-gate 		} else {
8320Sstevel@tonic-gate 			oprintf("%3s %-41s\n", "CPU", "FUNCTION");
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 		heading = 1;
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (!g_flowindent) {
8380Sstevel@tonic-gate 		if (!g_quiet) {
8390Sstevel@tonic-gate 			char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s:%s",
8420Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 			oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
8450Sstevel@tonic-gate 		}
8460Sstevel@tonic-gate 	} else {
8470Sstevel@tonic-gate 		int indent = data->dtpda_indent;
8480Sstevel@tonic-gate 		char *name;
8490Sstevel@tonic-gate 		size_t len;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
8520Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
8530Sstevel@tonic-gate 			name = alloca(len);
8540Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s:%s", indent, "",
8550Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func,
8560Sstevel@tonic-gate 			    pd->dtpd_name);
8570Sstevel@tonic-gate 		} else {
8580Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + 5;
8590Sstevel@tonic-gate 			name = alloca(len);
8600Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s", indent, "",
8610Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		oprintf("%3d %-41s ", cpu, name);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate static void
8710Sstevel@tonic-gate go(void)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	int i;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	struct {
8760Sstevel@tonic-gate 		char *name;
8770Sstevel@tonic-gate 		char *optname;
8780Sstevel@tonic-gate 		dtrace_optval_t val;
8790Sstevel@tonic-gate 	} bufs[] = {
8800Sstevel@tonic-gate 		{ "buffer size", "bufsize" },
8810Sstevel@tonic-gate 		{ "aggregation size", "aggsize" },
8820Sstevel@tonic-gate 		{ "speculation size", "specsize" },
8830Sstevel@tonic-gate 		{ "dynamic variable size", "dynvarsize" },
8840Sstevel@tonic-gate 		{ NULL }
8850Sstevel@tonic-gate 	}, rates[] = {
8860Sstevel@tonic-gate 		{ "cleaning rate", "cleanrate" },
8870Sstevel@tonic-gate 		{ "status rate", "statusrate" },
8880Sstevel@tonic-gate 		{ NULL }
8890Sstevel@tonic-gate 	};
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
8920Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
8930Sstevel@tonic-gate 			fatal("couldn't get option %s", bufs[i].optname);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
8970Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
8980Sstevel@tonic-gate 			fatal("couldn't get option %s", rates[i].optname);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	if (dtrace_go(g_dtp) == -1)
9020Sstevel@tonic-gate 		dfatal("could not enable tracing");
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
9050Sstevel@tonic-gate 		dtrace_optval_t j = 0, mul = 10;
9060Sstevel@tonic-gate 		dtrace_optval_t nsize;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (bufs[i].val == DTRACEOPT_UNSET)
9090Sstevel@tonic-gate 			continue;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		if (nsize == DTRACEOPT_UNSET || nsize == 0)
9140Sstevel@tonic-gate 			continue;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		if (nsize >= bufs[i].val - sizeof (uint64_t))
9170Sstevel@tonic-gate 			continue;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
9200Sstevel@tonic-gate 			continue;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
9230Sstevel@tonic-gate 			error("%s lowered to %lld%c\n", bufs[i].name,
9240Sstevel@tonic-gate 			    (long long)nsize >> (mul - 10), " kmgtpe"[j]);
9250Sstevel@tonic-gate 		} else {
9260Sstevel@tonic-gate 			error("%s lowered to %lld bytes\n", bufs[i].name,
9270Sstevel@tonic-gate 			    (long long)nsize);
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
9320Sstevel@tonic-gate 		dtrace_optval_t nval;
9330Sstevel@tonic-gate 		char *dir;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 		if (rates[i].val == DTRACEOPT_UNSET)
9360Sstevel@tonic-gate 			continue;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		if (nval == DTRACEOPT_UNSET || nval == 0)
9410Sstevel@tonic-gate 			continue;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		if (rates[i].val == nval)
9440Sstevel@tonic-gate 			continue;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		dir = nval > rates[i].val ? "reduced" : "increased";
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
9490Sstevel@tonic-gate 			error("%s %s to %lld hz\n", rates[i].name, dir,
9500Sstevel@tonic-gate 			    (long long)NANOSEC / (long long)nval);
9510Sstevel@tonic-gate 			continue;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		if ((nval % NANOSEC) == 0) {
9550Sstevel@tonic-gate 			error("%s %s to once every %lld seconds\n",
9560Sstevel@tonic-gate 			    rates[i].name, dir,
9570Sstevel@tonic-gate 			    (long long)nval / (long long)NANOSEC);
9580Sstevel@tonic-gate 			continue;
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		error("%s %s to once every %lld nanoseconds\n",
9620Sstevel@tonic-gate 		    rates[i].name, dir, (long long)nval);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate /*ARGSUSED*/
9670Sstevel@tonic-gate static void
9680Sstevel@tonic-gate intr(int signo)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	if (!g_intr)
9710Sstevel@tonic-gate 		g_newline = 1;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (g_intr++)
9740Sstevel@tonic-gate 		g_impatient = 1;
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate int
9780Sstevel@tonic-gate main(int argc, char *argv[])
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	dtrace_bufdesc_t buf;
9810Sstevel@tonic-gate 	struct sigaction act;
9820Sstevel@tonic-gate 	dtrace_status_t status[2];
9830Sstevel@tonic-gate 	dtrace_optval_t opt;
9840Sstevel@tonic-gate 	dtrace_cmd_t *dcp;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	int done = 0, mode = 0;
9870Sstevel@tonic-gate 	int err, i;
9880Sstevel@tonic-gate 	char c, *p, **v;
9890Sstevel@tonic-gate 	struct ps_prochandle *P;
9900Sstevel@tonic-gate 	pid_t pid;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	g_pname = basename(argv[0]);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (argc == 1)
9950Sstevel@tonic-gate 		return (usage(stderr));
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
9980Sstevel@tonic-gate 	    (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
9990Sstevel@tonic-gate 	    (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
10000Sstevel@tonic-gate 		fatal("failed to allocate memory for arguments");
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	g_argv[g_argc++] = argv[0];	/* propagate argv[0] to D as $0/$$0 */
10030Sstevel@tonic-gate 	argv[0] = g_pname;		/* rewrite argv[0] for getopt errors */
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	bzero(status, sizeof (status));
10060Sstevel@tonic-gate 	bzero(&buf, sizeof (buf));
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Make an initial pass through argv[] processing any arguments that
10100Sstevel@tonic-gate 	 * affect our behavior mode (g_mode) and flags used for dtrace_open().
10110Sstevel@tonic-gate 	 * We also accumulate arguments that are not affiliated with getopt
10120Sstevel@tonic-gate 	 * options into g_argv[], and abort if any invalid options are found.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
10150Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
10160Sstevel@tonic-gate 			switch (c) {
10170Sstevel@tonic-gate 			case '3':
10180Sstevel@tonic-gate 				if (strcmp(optarg, "2") != 0) {
10190Sstevel@tonic-gate 					(void) fprintf(stderr,
10200Sstevel@tonic-gate 					    "%s: illegal option -- 3%s\n",
10210Sstevel@tonic-gate 					    argv[0], optarg);
10220Sstevel@tonic-gate 					return (usage(stderr));
10230Sstevel@tonic-gate 				}
10240Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_LP64;
10250Sstevel@tonic-gate 				g_oflags |= DTRACE_O_ILP32;
10260Sstevel@tonic-gate 				break;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			case '6':
10290Sstevel@tonic-gate 				if (strcmp(optarg, "4") != 0) {
10300Sstevel@tonic-gate 					(void) fprintf(stderr,
10310Sstevel@tonic-gate 					    "%s: illegal option -- 6%s\n",
10320Sstevel@tonic-gate 					    argv[0], optarg);
10330Sstevel@tonic-gate 					return (usage(stderr));
10340Sstevel@tonic-gate 				}
10350Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_ILP32;
10360Sstevel@tonic-gate 				g_oflags |= DTRACE_O_LP64;
10370Sstevel@tonic-gate 				break;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 			case 'a':
10400Sstevel@tonic-gate 				g_grabanon++; /* also checked in pass 2 below */
10410Sstevel@tonic-gate 				break;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			case 'A':
10440Sstevel@tonic-gate 				g_mode = DMODE_ANON;
10450Sstevel@tonic-gate 				g_exec = 0;
10460Sstevel@tonic-gate 				mode++;
10470Sstevel@tonic-gate 				break;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 			case 'e':
10500Sstevel@tonic-gate 				g_exec = 0;
10510Sstevel@tonic-gate 				done = 1;
10520Sstevel@tonic-gate 				break;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 			case 'G':
10550Sstevel@tonic-gate 				g_mode = DMODE_LINK;
10560Sstevel@tonic-gate 				g_oflags |= DTRACE_O_NODEV;
10570Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
10580Sstevel@tonic-gate 				g_exec = 0;
10590Sstevel@tonic-gate 				mode++;
10600Sstevel@tonic-gate 				break;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			case 'l':
10630Sstevel@tonic-gate 				g_mode = DMODE_LIST;
10640Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
10650Sstevel@tonic-gate 				mode++;
10660Sstevel@tonic-gate 				break;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 			case 'V':
10690Sstevel@tonic-gate 				g_mode = DMODE_VERS;
10700Sstevel@tonic-gate 				mode++;
10710Sstevel@tonic-gate 				break;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 			default:
10740Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
10750Sstevel@tonic-gate 					return (usage(stderr));
10760Sstevel@tonic-gate 			}
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		if (optind < argc)
10800Sstevel@tonic-gate 			g_argv[g_argc++] = argv[optind];
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (mode > 1) {
10840Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: only one of the [-AGlV] options "
10850Sstevel@tonic-gate 		    "can be specified at a time\n", g_pname);
10860Sstevel@tonic-gate 		return (E_USAGE);
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	if (g_mode == DMODE_VERS)
10900Sstevel@tonic-gate 		return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	/*
10930Sstevel@tonic-gate 	 * Open libdtrace.  If we are not actually going to be enabling any
10940Sstevel@tonic-gate 	 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
10950Sstevel@tonic-gate 	 */
10960Sstevel@tonic-gate 	while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
10970Sstevel@tonic-gate 		if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
10980Sstevel@tonic-gate 			g_oflags |= DTRACE_O_NODEV;
10990Sstevel@tonic-gate 			continue;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
11030Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "bufsize", "4m");
11070Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "aggsize", "4m");
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	/*
11100Sstevel@tonic-gate 	 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
11110Sstevel@tonic-gate 	 * references to undefined symbols to remain as unresolved relocations.
11120Sstevel@tonic-gate 	 * If -A is specified, enable -xlink=primary to permit static linking
11130Sstevel@tonic-gate 	 * only to kernel symbols that are defined in a primary kernel module.
11140Sstevel@tonic-gate 	 */
11150Sstevel@tonic-gate 	if (g_mode == DMODE_LINK) {
11160Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
11170Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "unodefs", NULL);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		g_objc = g_argc;
11200Sstevel@tonic-gate 		g_objv = g_argv;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		/*
11230Sstevel@tonic-gate 		 * We still use g_argv[0], the name of the executable.
11240Sstevel@tonic-gate 		 */
11250Sstevel@tonic-gate 		g_argc = 1;
11260Sstevel@tonic-gate 	} else if (g_mode == DMODE_ANON)
11270Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "primary");
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/*
11300Sstevel@tonic-gate 	 * Now that we have libdtrace open, make a second pass through argv[]
11310Sstevel@tonic-gate 	 * to perform any dtrace_setopt() calls and change any compiler flags.
11320Sstevel@tonic-gate 	 * We also accumulate any program specifications into our g_cmdv[] at
11330Sstevel@tonic-gate 	 * this time; these will compiled as part of the fourth processing pass.
11340Sstevel@tonic-gate 	 */
11350Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
11360Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
11370Sstevel@tonic-gate 			switch (c) {
11380Sstevel@tonic-gate 			case 'a':
11390Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
11400Sstevel@tonic-gate 					dfatal("failed to set -a");
11410Sstevel@tonic-gate 				break;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			case 'b':
11440Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp,
11450Sstevel@tonic-gate 				    "bufsize", optarg) != 0)
11460Sstevel@tonic-gate 					dfatal("failed to set -b %s", optarg);
11470Sstevel@tonic-gate 				break;
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 			case 'C':
11500Sstevel@tonic-gate 				g_cflags |= DTRACE_C_CPP;
11510Sstevel@tonic-gate 				break;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 			case 'D':
11540Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "define", optarg) != 0)
11550Sstevel@tonic-gate 					dfatal("failed to set -D %s", optarg);
11560Sstevel@tonic-gate 				break;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 			case 'f':
11590Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
11600Sstevel@tonic-gate 				dcp->dc_func = compile_str;
11610Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
11620Sstevel@tonic-gate 				dcp->dc_arg = optarg;
11630Sstevel@tonic-gate 				break;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 			case 'F':
11660Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
11670Sstevel@tonic-gate 					dfatal("failed to set -F");
11680Sstevel@tonic-gate 				break;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 			case 'H':
11710Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
11720Sstevel@tonic-gate 					dfatal("failed to set -H");
11730Sstevel@tonic-gate 				break;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			case 'i':
11760Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
11770Sstevel@tonic-gate 				dcp->dc_func = compile_str;
11780Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
11790Sstevel@tonic-gate 				dcp->dc_arg = optarg;
11800Sstevel@tonic-gate 				break;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 			case 'I':
11830Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
11840Sstevel@tonic-gate 					dfatal("failed to set -I %s", optarg);
11850Sstevel@tonic-gate 				break;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 			case 'L':
11880Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
11890Sstevel@tonic-gate 					dfatal("failed to set -L %s", optarg);
11900Sstevel@tonic-gate 				break;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			case 'm':
11930Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
11940Sstevel@tonic-gate 				dcp->dc_func = compile_str;
11950Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_MOD;
11960Sstevel@tonic-gate 				dcp->dc_arg = optarg;
11970Sstevel@tonic-gate 				break;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 			case 'n':
12000Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
12010Sstevel@tonic-gate 				dcp->dc_func = compile_str;
12020Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
12030Sstevel@tonic-gate 				dcp->dc_arg = optarg;
12040Sstevel@tonic-gate 				break;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 			case 'P':
12070Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
12080Sstevel@tonic-gate 				dcp->dc_func = compile_str;
12090Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
12100Sstevel@tonic-gate 				dcp->dc_arg = optarg;
12110Sstevel@tonic-gate 				break;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			case 'q':
12140Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
12150Sstevel@tonic-gate 					dfatal("failed to set -q");
12160Sstevel@tonic-gate 				break;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 			case 'o':
12190Sstevel@tonic-gate 				g_ofile = optarg;
12200Sstevel@tonic-gate 				break;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 			case 's':
12230Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
12240Sstevel@tonic-gate 				dcp->dc_func = compile_file;
12250Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NONE;
12260Sstevel@tonic-gate 				dcp->dc_arg = optarg;
12270Sstevel@tonic-gate 				break;
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 			case 'S':
12300Sstevel@tonic-gate 				g_cflags |= DTRACE_C_DIFV;
12310Sstevel@tonic-gate 				break;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 			case 'U':
12340Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
12350Sstevel@tonic-gate 					dfatal("failed to set -U %s", optarg);
12360Sstevel@tonic-gate 				break;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 			case 'v':
12390Sstevel@tonic-gate 				g_verbose++;
12400Sstevel@tonic-gate 				break;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 			case 'w':
12430Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
12440Sstevel@tonic-gate 					dfatal("failed to set -w");
12450Sstevel@tonic-gate 				break;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 			case 'x':
12480Sstevel@tonic-gate 				if ((p = strchr(optarg, '=')) != NULL)
12490Sstevel@tonic-gate 					*p++ = '\0';
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, optarg, p) != 0)
12520Sstevel@tonic-gate 					dfatal("failed to set -x %s", optarg);
12530Sstevel@tonic-gate 				break;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 			case 'X':
12560Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
12570Sstevel@tonic-gate 					dfatal("failed to set -X %s", optarg);
12580Sstevel@tonic-gate 				break;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 			case 'Z':
12610Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS;
12620Sstevel@tonic-gate 				break;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 			default:
12650Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
12660Sstevel@tonic-gate 					return (usage(stderr));
12670Sstevel@tonic-gate 			}
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	/*
12720Sstevel@tonic-gate 	 * In our third pass we handle any command-line options related to
12730Sstevel@tonic-gate 	 * grabbing or creating victim processes.  The behavior of these calls
12740Sstevel@tonic-gate 	 * may been affected by any library options set by the second pass.
12750Sstevel@tonic-gate 	 */
12760Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
12770Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
12780Sstevel@tonic-gate 			switch (c) {
12790Sstevel@tonic-gate 			case 'c':
12800Sstevel@tonic-gate 				if ((v = make_argv(optarg)) == NULL)
12810Sstevel@tonic-gate 					fatal("failed to allocate memory");
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 				P = dtrace_proc_create(g_dtp, v[0], v);
12840Sstevel@tonic-gate 				if (P == NULL)
12850Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 				g_psv[g_psc++] = P;
12880Sstevel@tonic-gate 				free(v);
12890Sstevel@tonic-gate 				break;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 			case 'p':
12920Sstevel@tonic-gate 				errno = 0;
12930Sstevel@tonic-gate 				pid = strtol(optarg, &p, 10);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 				if (errno != 0 || p == optarg || p[0] != '\0')
12960Sstevel@tonic-gate 					fatal("invalid pid: %s\n", optarg);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 				P = dtrace_proc_grab(g_dtp, pid, 0);
12990Sstevel@tonic-gate 				if (P == NULL)
13000Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 				g_psv[g_psc++] = P;
13030Sstevel@tonic-gate 				break;
13040Sstevel@tonic-gate 			}
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
13100Sstevel@tonic-gate 	 * each string or file specification into a compiled program structure.
13110Sstevel@tonic-gate 	 */
13120Sstevel@tonic-gate 	for (i = 0; i < g_cmdc; i++)
13130Sstevel@tonic-gate 		g_cmdv[i].dc_func(&g_cmdv[i]);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	if (g_mode != DMODE_LIST) {
13160Sstevel@tonic-gate 		if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
13170Sstevel@tonic-gate 			dfatal("failed to establish error handler");
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
13200Sstevel@tonic-gate 			dfatal("failed to establish drop handler");
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
13230Sstevel@tonic-gate 			dfatal("failed to establish proc handler");
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
13270Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
13300Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
13330Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Now make a fifth and final pass over the options that have been
13370Sstevel@tonic-gate 	 * turned into programs and saved in g_cmdv[], performing any mode-
13380Sstevel@tonic-gate 	 * specific processing.  If g_mode is DMODE_EXEC, we will break out
13390Sstevel@tonic-gate 	 * of the switch() and continue on to the data processing loop.  For
13400Sstevel@tonic-gate 	 * other modes, we will exit dtrace once mode-specific work is done.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	switch (g_mode) {
13430Sstevel@tonic-gate 	case DMODE_EXEC:
13440Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
13450Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
13480Sstevel@tonic-gate 			exec_prog(&g_cmdv[i]);
13490Sstevel@tonic-gate 
1350*265Smws 		if (done && !g_grabanon) {
1351*265Smws 			dtrace_close(g_dtp);
13520Sstevel@tonic-gate 			return (g_status);
1353*265Smws 		}
13540Sstevel@tonic-gate 		break;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	case DMODE_ANON:
13570Sstevel@tonic-gate 		if (g_ofile == NULL)
13580Sstevel@tonic-gate 			g_ofile = "/kernel/drv/dtrace.conf";
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 		dof_prune(g_ofile); /* strip out any old DOF directives */
13610Sstevel@tonic-gate 		etcsystem_prune(); /* string out any forceload directives */
13620Sstevel@tonic-gate 
1363*265Smws 		if (g_cmdc == 0) {
1364*265Smws 			dtrace_close(g_dtp);
13650Sstevel@tonic-gate 			return (g_status);
1366*265Smws 		}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 		if ((g_ofp = fopen(g_ofile, "a")) == NULL)
13690Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++) {
13720Sstevel@tonic-gate 			anon_prog(&g_cmdv[i],
13730Sstevel@tonic-gate 			    dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 		/*
13770Sstevel@tonic-gate 		 * Dump out the DOF corresponding to the error handler and the
13780Sstevel@tonic-gate 		 * current options as the final DOF property in the .conf file.
13790Sstevel@tonic-gate 		 */
13800Sstevel@tonic-gate 		anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
13810Sstevel@tonic-gate 		anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		if (fclose(g_ofp) == EOF)
13840Sstevel@tonic-gate 			fatal("failed to close output file '%s'", g_ofile);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		/*
13870Sstevel@tonic-gate 		 * These messages would use notice() rather than error(), but
13880Sstevel@tonic-gate 		 * we don't want them suppressed when -A is run on a D program
13890Sstevel@tonic-gate 		 * that itself contains a #pragma D option quiet.
13900Sstevel@tonic-gate 		 */
13910Sstevel@tonic-gate 		error("saved anonymous enabling in %s\n", g_ofile);
13920Sstevel@tonic-gate 		etcsystem_add();
13930Sstevel@tonic-gate 		error("run update_drv(1M) or reboot to enable changes\n");
13940Sstevel@tonic-gate 
1395*265Smws 		dtrace_close(g_dtp);
13960Sstevel@tonic-gate 		return (g_status);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	case DMODE_LINK:
13990Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
14000Sstevel@tonic-gate 			link_prog(&g_cmdv[i]);
1401191Sahl 
1402191Sahl 		if (g_cmdc > 1 && g_ofile != NULL) {
1403191Sahl 			char **objv = alloca(g_cmdc * sizeof (char *));
1404191Sahl 
1405191Sahl 			for (i = 0; i < g_cmdc; i++)
1406191Sahl 				objv[i] = g_cmdv[i].dc_ofile;
1407191Sahl 
1408191Sahl 			if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1409191Sahl 			    g_ofile, g_cmdc, objv) != 0)
1410*265Smws 				dfatal(NULL); /* dtrace_errmsg() only */
1411191Sahl 		}
1412191Sahl 
1413*265Smws 		dtrace_close(g_dtp);
14140Sstevel@tonic-gate 		return (g_status);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	case DMODE_LIST:
14170Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
14180Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 		oprintf("%5s %10s %17s %33s %s\n",
14210Sstevel@tonic-gate 		    "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
14240Sstevel@tonic-gate 			list_prog(&g_cmdv[i]);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		if (g_cmdc == 0)
14270Sstevel@tonic-gate 			(void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
14280Sstevel@tonic-gate 
1429*265Smws 		dtrace_close(g_dtp);
14300Sstevel@tonic-gate 		return (g_status);
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	/*
14340Sstevel@tonic-gate 	 * If -a and -Z were not specified and no probes have been matched, no
14350Sstevel@tonic-gate 	 * probe criteria was specified on the command line and we abort.
14360Sstevel@tonic-gate 	 */
14370Sstevel@tonic-gate 	if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
14380Sstevel@tonic-gate 		dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	/*
14410Sstevel@tonic-gate 	 * Start tracing.  Once we dtrace_go(), reload any options that affect
14420Sstevel@tonic-gate 	 * our globals in case consuming anonymous state has changed them.
14430Sstevel@tonic-gate 	 */
14440Sstevel@tonic-gate 	go();
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
14470Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
14500Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
14530Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "destructive", &opt);
14560Sstevel@tonic-gate 	if (opt != DTRACEOPT_UNSET)
14570Sstevel@tonic-gate 		notice("allowing destructive actions\n");
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
14600Sstevel@tonic-gate 	act.sa_flags = 0;
14610Sstevel@tonic-gate 	act.sa_handler = intr;
14620Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
14630Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Now that tracing is active and we are ready to consume trace data,
14670Sstevel@tonic-gate 	 * continue any grabbed or created processes, setting them running
14680Sstevel@tonic-gate 	 * using the /proc control mechanism inside of libdtrace.
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	for (i = 0; i < g_psc; i++)
14710Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_psv[i]);
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	g_pslive = g_psc; /* count for prochandler() */
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	do {
14760Sstevel@tonic-gate 		if (!g_intr && !done)
14770Sstevel@tonic-gate 			dtrace_sleep(g_dtp);
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		if (g_newline) {
14800Sstevel@tonic-gate 			/*
14810Sstevel@tonic-gate 			 * Output a newline just to make the output look
14820Sstevel@tonic-gate 			 * slightly cleaner.  Note that we do this even in
14830Sstevel@tonic-gate 			 * "quiet" mode...
14840Sstevel@tonic-gate 			 */
14850Sstevel@tonic-gate 			oprintf("\n");
14860Sstevel@tonic-gate 			g_newline = 0;
14870Sstevel@tonic-gate 		}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 		if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
14900Sstevel@tonic-gate 			done = 1;
14910Sstevel@tonic-gate 			if (dtrace_stop(g_dtp) == -1)
14920Sstevel@tonic-gate 				dfatal("couldn't stop tracing");
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 		switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
14960Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_DONE:
14970Sstevel@tonic-gate 			done = 1;
14980Sstevel@tonic-gate 			break;
14990Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_OKAY:
15000Sstevel@tonic-gate 			break;
15010Sstevel@tonic-gate 		default:
15020Sstevel@tonic-gate 			if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
15030Sstevel@tonic-gate 				dfatal("processing aborted");
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 		if (fflush(g_ofp) == EOF)
15070Sstevel@tonic-gate 			clearerr(g_ofp);
15080Sstevel@tonic-gate 	} while (!done);
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	oprintf("\n");
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (!g_impatient) {
15130Sstevel@tonic-gate 		if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
15140Sstevel@tonic-gate 		    dtrace_errno(g_dtp) != EINTR)
15150Sstevel@tonic-gate 			dfatal("failed to print aggregations");
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	dtrace_close(g_dtp);
15190Sstevel@tonic-gate 	return (g_status);
15200Sstevel@tonic-gate }
1521