1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/stat.h>
31*0Sstevel@tonic-gate #include <sys/wait.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <dtrace.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdarg.h>
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <limits.h>
40*0Sstevel@tonic-gate #include <fcntl.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <alloca.h>
44*0Sstevel@tonic-gate #include <libgen.h>
45*0Sstevel@tonic-gate #include <libproc.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate typedef struct dtrace_cmd {
48*0Sstevel@tonic-gate 	void (*dc_func)(struct dtrace_cmd *);	/* function to compile arg */
49*0Sstevel@tonic-gate 	dtrace_probespec_t dc_spec;		/* probe specifier context */
50*0Sstevel@tonic-gate 	char *dc_arg;				/* argument from main argv */
51*0Sstevel@tonic-gate 	const char *dc_name;			/* name for error messages */
52*0Sstevel@tonic-gate 	const char *dc_desc;			/* desc for error messages */
53*0Sstevel@tonic-gate 	dtrace_prog_t *dc_prog;			/* program compiled from arg */
54*0Sstevel@tonic-gate } dtrace_cmd_t;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	DMODE_VERS	0	/* display version information and exit (-V) */
57*0Sstevel@tonic-gate #define	DMODE_EXEC	1	/* compile program for enabling (-a/e/E) */
58*0Sstevel@tonic-gate #define	DMODE_ANON	2	/* compile program for anonymous tracing (-A) */
59*0Sstevel@tonic-gate #define	DMODE_LINK	3	/* compile program for linking with ELF (-G) */
60*0Sstevel@tonic-gate #define	DMODE_LIST	4	/* compile program and list probes (-l) */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	E_SUCCESS	0
63*0Sstevel@tonic-gate #define	E_ERROR		1
64*0Sstevel@tonic-gate #define	E_USAGE		2
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static const char DTRACE_OPTSTR[] =
67*0Sstevel@tonic-gate 	"3:6:aAb:c:CD:ef:FGHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static char **g_argv;
70*0Sstevel@tonic-gate static int g_argc;
71*0Sstevel@tonic-gate static char **g_objv;
72*0Sstevel@tonic-gate static int g_objc;
73*0Sstevel@tonic-gate static dtrace_cmd_t *g_cmdv;
74*0Sstevel@tonic-gate static int g_cmdc;
75*0Sstevel@tonic-gate static struct ps_prochandle **g_psv;
76*0Sstevel@tonic-gate static int g_psc;
77*0Sstevel@tonic-gate static int g_pslive;
78*0Sstevel@tonic-gate static char *g_pname;
79*0Sstevel@tonic-gate static int g_quiet;
80*0Sstevel@tonic-gate static int g_flowindent;
81*0Sstevel@tonic-gate static int g_intr;
82*0Sstevel@tonic-gate static int g_impatient;
83*0Sstevel@tonic-gate static int g_newline;
84*0Sstevel@tonic-gate static int g_total;
85*0Sstevel@tonic-gate static int g_cflags;
86*0Sstevel@tonic-gate static int g_oflags;
87*0Sstevel@tonic-gate static int g_verbose;
88*0Sstevel@tonic-gate static int g_exec = 1;
89*0Sstevel@tonic-gate static int g_mode = DMODE_EXEC;
90*0Sstevel@tonic-gate static int g_status = E_SUCCESS;
91*0Sstevel@tonic-gate static int g_grabanon = 0;
92*0Sstevel@tonic-gate static const char *g_ofile = NULL;
93*0Sstevel@tonic-gate static FILE *g_ofp = stdout;
94*0Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
95*0Sstevel@tonic-gate static char *g_etcfile = "/etc/system";
96*0Sstevel@tonic-gate static const char *g_etcbegin = "* vvvv Added by DTrace";
97*0Sstevel@tonic-gate static const char *g_etcend = "* ^^^^ Added by DTrace";
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static const char *g_etc[] =  {
100*0Sstevel@tonic-gate "*",
101*0Sstevel@tonic-gate "* The following forceload directives were added by dtrace(1M) to allow for",
102*0Sstevel@tonic-gate "* tracing during boot.  If these directives are removed, the system will",
103*0Sstevel@tonic-gate "* continue to function, but tracing will not occur during boot as desired.",
104*0Sstevel@tonic-gate "* To remove these directives (and this block comment) automatically, run",
105*0Sstevel@tonic-gate "* \"dtrace -A\" without additional arguments.  See the \"Anonymous Tracing\"",
106*0Sstevel@tonic-gate "* chapter of the Solaris Dynamic Tracing Guide for details.",
107*0Sstevel@tonic-gate "*",
108*0Sstevel@tonic-gate NULL };
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static int
111*0Sstevel@tonic-gate usage(FILE *fp)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	static const char predact[] = "[[ predicate ] action ]";
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	(void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGHlqSvVwZ] "
116*0Sstevel@tonic-gate 	    "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
117*0Sstevel@tonic-gate 	    "[-o output] [-p pid] [-s script] [-U name]\n\t"
118*0Sstevel@tonic-gate 	    "[-x opt[=val]] [-X a|c|s|t]\n\n"
119*0Sstevel@tonic-gate 	    "\t[-P provider %s]\n"
120*0Sstevel@tonic-gate 	    "\t[-m [ provider: ] module %s]\n"
121*0Sstevel@tonic-gate 	    "\t[-f [[ provider: ] module: ] func %s]\n"
122*0Sstevel@tonic-gate 	    "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
123*0Sstevel@tonic-gate 	    "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
124*0Sstevel@tonic-gate 	    predact, predact, predact, predact, predact);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	(void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
127*0Sstevel@tonic-gate 	(void) fprintf(fp, "\t   action -> '{' D-statements '}'\n");
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	(void) fprintf(fp, "\n"
130*0Sstevel@tonic-gate 	    "\t-32 generate 32-bit D programs and ELF files\n"
131*0Sstevel@tonic-gate 	    "\t-64 generate 64-bit D programs and ELF files\n\n"
132*0Sstevel@tonic-gate 	    "\t-a  claim anonymous tracing state\n"
133*0Sstevel@tonic-gate 	    "\t-A  generate driver.conf(4) directives for anonymous tracing\n"
134*0Sstevel@tonic-gate 	    "\t-b  set trace buffer size\n"
135*0Sstevel@tonic-gate 	    "\t-c  run specified command and exit upon its completion\n"
136*0Sstevel@tonic-gate 	    "\t-C  run cpp(1) preprocessor on script files\n"
137*0Sstevel@tonic-gate 	    "\t-D  define symbol when invoking preprocessor\n"
138*0Sstevel@tonic-gate 	    "\t-e  exit after compiling request but prior to enabling probes\n"
139*0Sstevel@tonic-gate 	    "\t-f  enable or list probes matching the specified function name\n"
140*0Sstevel@tonic-gate 	    "\t-F  coalesce trace output by function\n"
141*0Sstevel@tonic-gate 	    "\t-G  generate an ELF file containing embedded dtrace program\n"
142*0Sstevel@tonic-gate 	    "\t-H  print included files when invoking preprocessor\n"
143*0Sstevel@tonic-gate 	    "\t-i  enable or list probes matching the specified probe id\n"
144*0Sstevel@tonic-gate 	    "\t-I  add include directory to preprocessor search path\n"
145*0Sstevel@tonic-gate 	    "\t-l  list probes matching specified criteria\n"
146*0Sstevel@tonic-gate 	    "\t-L  add library directory to library search path\n"
147*0Sstevel@tonic-gate 	    "\t-m  enable or list probes matching the specified module name\n"
148*0Sstevel@tonic-gate 	    "\t-n  enable or list probes matching the specified probe name\n"
149*0Sstevel@tonic-gate 	    "\t-o  set output file\n"
150*0Sstevel@tonic-gate 	    "\t-p  grab specified process-ID and cache its symbol tables\n"
151*0Sstevel@tonic-gate 	    "\t-P  enable or list probes matching the specified provider name\n"
152*0Sstevel@tonic-gate 	    "\t-q  set quiet mode (only output explicitly traced data)\n"
153*0Sstevel@tonic-gate 	    "\t-s  enable or list probes according to the specified D script\n"
154*0Sstevel@tonic-gate 	    "\t-S  print D compiler intermediate code\n"
155*0Sstevel@tonic-gate 	    "\t-U  undefine symbol when invoking preprocessor\n"
156*0Sstevel@tonic-gate 	    "\t-v  set verbose mode (report stability attributes, arguments)\n"
157*0Sstevel@tonic-gate 	    "\t-V  report DTrace API version\n"
158*0Sstevel@tonic-gate 	    "\t-w  permit destructive actions\n"
159*0Sstevel@tonic-gate 	    "\t-x  enable or modify compiler and tracing options\n"
160*0Sstevel@tonic-gate 	    "\t-X  specify ISO C conformance settings for preprocessor\n"
161*0Sstevel@tonic-gate 	    "\t-Z  permit probe descriptions that match zero probes\n");
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	return (E_USAGE);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static void
167*0Sstevel@tonic-gate verror(const char *fmt, va_list ap)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	int error = errno;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
172*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
175*0Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate /*PRINTFLIKE1*/
179*0Sstevel@tonic-gate static void
180*0Sstevel@tonic-gate fatal(const char *fmt, ...)
181*0Sstevel@tonic-gate {
182*0Sstevel@tonic-gate 	va_list ap;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	va_start(ap, fmt);
185*0Sstevel@tonic-gate 	verror(fmt, ap);
186*0Sstevel@tonic-gate 	va_end(ap);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	exit(E_ERROR);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*PRINTFLIKE1*/
192*0Sstevel@tonic-gate static void
193*0Sstevel@tonic-gate dfatal(const char *fmt, ...)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate 	va_list ap;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	va_start(ap, fmt);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
200*0Sstevel@tonic-gate 	if (fmt != NULL)
201*0Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	va_end(ap);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
206*0Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
207*0Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
208*0Sstevel@tonic-gate 	} else if (fmt == NULL) {
209*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n",
210*0Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	exit(E_ERROR);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate /*PRINTFLIKE1*/
217*0Sstevel@tonic-gate static void
218*0Sstevel@tonic-gate error(const char *fmt, ...)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	va_list ap;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	va_start(ap, fmt);
223*0Sstevel@tonic-gate 	verror(fmt, ap);
224*0Sstevel@tonic-gate 	va_end(ap);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate /*PRINTFLIKE1*/
228*0Sstevel@tonic-gate static void
229*0Sstevel@tonic-gate notice(const char *fmt, ...)
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate 	va_list ap;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (g_quiet)
234*0Sstevel@tonic-gate 		return; /* -q or quiet pragma suppresses notice()s */
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	va_start(ap, fmt);
237*0Sstevel@tonic-gate 	verror(fmt, ap);
238*0Sstevel@tonic-gate 	va_end(ap);
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /*PRINTFLIKE1*/
242*0Sstevel@tonic-gate static void
243*0Sstevel@tonic-gate oprintf(const char *fmt, ...)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	va_list ap;
246*0Sstevel@tonic-gate 	int n;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	va_start(ap, fmt);
249*0Sstevel@tonic-gate 	n = vfprintf(g_ofp, fmt, ap);
250*0Sstevel@tonic-gate 	va_end(ap);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	if (n < 0) {
253*0Sstevel@tonic-gate 		if (errno != EINTR) {
254*0Sstevel@tonic-gate 			fatal("failed to write to %s",
255*0Sstevel@tonic-gate 			    g_ofile ? g_ofile : "<stdout>");
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		clearerr(g_ofp);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate static char **
262*0Sstevel@tonic-gate make_argv(char *s)
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate 	const char *ws = "\f\n\r\t\v ";
265*0Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
266*0Sstevel@tonic-gate 	int argc = 0;
267*0Sstevel@tonic-gate 	char *p = s;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	if (argv == NULL)
270*0Sstevel@tonic-gate 		return (NULL);
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
273*0Sstevel@tonic-gate 		argv[argc++] = p;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (argc == 0)
276*0Sstevel@tonic-gate 		argv[argc++] = s;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	argv[argc] = NULL;
279*0Sstevel@tonic-gate 	return (argv);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate static void
283*0Sstevel@tonic-gate dof_prune(const char *fname)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate 	struct stat sbuf;
286*0Sstevel@tonic-gate 	size_t sz, i, j, mark, len;
287*0Sstevel@tonic-gate 	char *buf;
288*0Sstevel@tonic-gate 	int msg = 0, fd;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
291*0Sstevel@tonic-gate 		/*
292*0Sstevel@tonic-gate 		 * This is okay only if the file doesn't exist at all.
293*0Sstevel@tonic-gate 		 */
294*0Sstevel@tonic-gate 		if (errno != ENOENT)
295*0Sstevel@tonic-gate 			fatal("failed to open %s", fname);
296*0Sstevel@tonic-gate 		return;
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
300*0Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
303*0Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
306*0Sstevel@tonic-gate 		fatal("failed to read %s", fname);
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	buf[sz] = '\0';
309*0Sstevel@tonic-gate 	(void) close(fd);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
312*0Sstevel@tonic-gate 		fatal("failed to open %s for writing", fname);
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	len = strlen("dof-data-");
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	for (mark = 0, i = 0; i < sz; i++) {
317*0Sstevel@tonic-gate 		if (strncmp(&buf[i], "dof-data-", len) != 0)
318*0Sstevel@tonic-gate 			continue;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 		/*
321*0Sstevel@tonic-gate 		 * This is only a match if it's in the 0th column.
322*0Sstevel@tonic-gate 		 */
323*0Sstevel@tonic-gate 		if (i != 0 && buf[i - 1] != '\n')
324*0Sstevel@tonic-gate 			continue;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 		if (msg++ == 0) {
327*0Sstevel@tonic-gate 			error("cleaned up old anonymous "
328*0Sstevel@tonic-gate 			    "enabling in %s\n", fname);
329*0Sstevel@tonic-gate 		}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		/*
332*0Sstevel@tonic-gate 		 * We have a match.  First write out our data up until now.
333*0Sstevel@tonic-gate 		 */
334*0Sstevel@tonic-gate 		if (i != mark) {
335*0Sstevel@tonic-gate 			if (write(fd, &buf[mark], i - mark) != i - mark)
336*0Sstevel@tonic-gate 				fatal("failed to write to %s", fname);
337*0Sstevel@tonic-gate 		}
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 		/*
340*0Sstevel@tonic-gate 		 * Now scan forward until we scan past a newline.
341*0Sstevel@tonic-gate 		 */
342*0Sstevel@tonic-gate 		for (j = i; j < sz && buf[j] != '\n'; j++)
343*0Sstevel@tonic-gate 			continue;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 		/*
346*0Sstevel@tonic-gate 		 * Reset our mark.
347*0Sstevel@tonic-gate 		 */
348*0Sstevel@tonic-gate 		if ((mark = j + 1) >= sz)
349*0Sstevel@tonic-gate 			break;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 		i = j;
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (mark < sz) {
355*0Sstevel@tonic-gate 		if (write(fd, &buf[mark], sz - mark) != sz - mark)
356*0Sstevel@tonic-gate 			fatal("failed to write to %s", fname);
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	(void) close(fd);
360*0Sstevel@tonic-gate 	free(buf);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate static void
364*0Sstevel@tonic-gate etcsystem_prune(void)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate 	struct stat sbuf;
367*0Sstevel@tonic-gate 	size_t sz;
368*0Sstevel@tonic-gate 	char *buf, *start, *end;
369*0Sstevel@tonic-gate 	int fd;
370*0Sstevel@tonic-gate 	char *fname = g_etcfile, *tmpname;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1)
373*0Sstevel@tonic-gate 		fatal("failed to open %s", fname);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
376*0Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
379*0Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
382*0Sstevel@tonic-gate 		fatal("failed to read %s", fname);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	buf[sz] = '\0';
385*0Sstevel@tonic-gate 	(void) close(fd);
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	if ((start = strstr(buf, g_etcbegin)) == NULL)
388*0Sstevel@tonic-gate 		goto out;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	if (strlen(buf) != sz) {
391*0Sstevel@tonic-gate 		fatal("embedded nul byte in %s; manual repair of %s "
392*0Sstevel@tonic-gate 		    "required\n", fname, fname);
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	if (strstr(start + 1, g_etcbegin) != NULL) {
396*0Sstevel@tonic-gate 		fatal("multiple start sentinels in %s; manual repair of %s "
397*0Sstevel@tonic-gate 		    "required\n", fname, fname);
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	if ((end = strstr(buf, g_etcend)) == NULL) {
401*0Sstevel@tonic-gate 		fatal("missing end sentinel in %s; manual repair of %s "
402*0Sstevel@tonic-gate 		    "required\n", fname, fname);
403*0Sstevel@tonic-gate 	}
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	if (start > end) {
406*0Sstevel@tonic-gate 		fatal("end sentinel preceeds start sentinel in %s; manual "
407*0Sstevel@tonic-gate 		    "repair of %s required\n", fname, fname);
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	end += strlen(g_etcend) + 1;
411*0Sstevel@tonic-gate 	bcopy(end, start, strlen(end) + 1);
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	tmpname = alloca(sz = strlen(fname) + 80);
414*0Sstevel@tonic-gate 	(void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	if ((fd = open(tmpname,
417*0Sstevel@tonic-gate 	    O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
418*0Sstevel@tonic-gate 		fatal("failed to create %s", tmpname);
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	if (write(fd, buf, strlen(buf)) < strlen(buf)) {
421*0Sstevel@tonic-gate 		(void) unlink(tmpname);
422*0Sstevel@tonic-gate 		fatal("failed to write to %s", tmpname);
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	(void) close(fd);
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
428*0Sstevel@tonic-gate 		(void) unlink(tmpname);
429*0Sstevel@tonic-gate 		fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
430*0Sstevel@tonic-gate 		    (int)sbuf.st_uid, (int)sbuf.st_gid);
431*0Sstevel@tonic-gate 	}
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if (rename(tmpname, fname) == -1)
434*0Sstevel@tonic-gate 		fatal("rename of %s to %s failed", tmpname, fname);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	error("cleaned up forceload directives in %s\n", fname);
437*0Sstevel@tonic-gate out:
438*0Sstevel@tonic-gate 	free(buf);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate static void
442*0Sstevel@tonic-gate etcsystem_add(void)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	const char *mods[20];
445*0Sstevel@tonic-gate 	int nmods, line;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
448*0Sstevel@tonic-gate 		fatal("failed to open output file '%s'", g_ofile);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	oprintf("%s\n", g_etcbegin);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	for (line = 0; g_etc[line] != NULL; line++)
453*0Sstevel@tonic-gate 		oprintf("%s\n", g_etc[line]);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	nmods = dtrace_provider_modules(g_dtp, mods,
456*0Sstevel@tonic-gate 	    sizeof (mods) / sizeof (char *) - 1);
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	if (nmods >= sizeof (mods) / sizeof (char *))
459*0Sstevel@tonic-gate 		fatal("unexpectedly large number of modules!");
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	mods[nmods++] = "dtrace";
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	for (line = 0; line < nmods; line++)
464*0Sstevel@tonic-gate 		oprintf("forceload: drv/%s\n", mods[line]);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	oprintf("%s\n", g_etcend);
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	if (fclose(g_ofp) == EOF)
469*0Sstevel@tonic-gate 		fatal("failed to close output file '%s'", g_ofile);
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	error("added forceload directives to %s\n", g_ofile);
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate static void
475*0Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p)
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate 	char buf[BUFSIZ];
478*0Sstevel@tonic-gate 	int i;
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	oprintf("\n\tProbe Description Attributes\n");
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
483*0Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_name));
484*0Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
485*0Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_data));
486*0Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
487*0Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_attr.dtat_class));
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	oprintf("\n\tArgument Attributes\n");
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
492*0Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_name));
493*0Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
494*0Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_data));
495*0Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
496*0Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_arga.dtat_class));
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	oprintf("\n\tArgument Types\n");
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	for (i = 0; i < p->dtp_argc; i++) {
501*0Sstevel@tonic-gate 		if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
502*0Sstevel@tonic-gate 		    p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
503*0Sstevel@tonic-gate 			(void) strlcpy(buf, "(unknown)", sizeof (buf));
504*0Sstevel@tonic-gate 		oprintf("\t\targs[%d]: %s\n", i, buf);
505*0Sstevel@tonic-gate 	}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	if (p->dtp_argc == 0)
508*0Sstevel@tonic-gate 		oprintf("\t\tNone\n");
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	oprintf("\n");
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate /*ARGSUSED*/
514*0Sstevel@tonic-gate static int
515*0Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
516*0Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
519*0Sstevel@tonic-gate 	dtrace_probedesc_t *pdp = &edp->dted_probe;
520*0Sstevel@tonic-gate 	dtrace_probeinfo_t p;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	if (edp == *last)
523*0Sstevel@tonic-gate 		return (0);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	oprintf("\n%s:%s:%s:%s\n",
526*0Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	if (dtrace_probe_info(dtp, pdp, &p) == 0)
529*0Sstevel@tonic-gate 		print_probe_info(&p);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	*last = edp;
532*0Sstevel@tonic-gate 	return (0);
533*0Sstevel@tonic-gate }
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate /*
536*0Sstevel@tonic-gate  * Execute the specified program by enabling the corresponding instrumentation.
537*0Sstevel@tonic-gate  * If -e has been specified, we get the program info but do not enable it.  If
538*0Sstevel@tonic-gate  * -v has been specified, we print a stability report for the program.
539*0Sstevel@tonic-gate  */
540*0Sstevel@tonic-gate static void
541*0Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp)
542*0Sstevel@tonic-gate {
543*0Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
544*0Sstevel@tonic-gate 	dtrace_proginfo_t dpi;
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (!g_exec) {
547*0Sstevel@tonic-gate 		dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
548*0Sstevel@tonic-gate 	} else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
549*0Sstevel@tonic-gate 		dfatal("failed to enable '%s'", dcp->dc_name);
550*0Sstevel@tonic-gate 	} else {
551*0Sstevel@tonic-gate 		notice("%s '%s' matched %u probe%s\n",
552*0Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name,
553*0Sstevel@tonic-gate 		    dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
554*0Sstevel@tonic-gate 	}
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	if (g_verbose) {
557*0Sstevel@tonic-gate 		oprintf("\nStability attributes for %s %s:\n",
558*0Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 		oprintf("\n\tMinimum Probe Description Attributes\n");
561*0Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
562*0Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_name));
563*0Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
564*0Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_data));
565*0Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
566*0Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_descattr.dtat_class));
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		oprintf("\n\tMinimum Statement Attributes\n");
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
571*0Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
572*0Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
573*0Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
574*0Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
575*0Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 		if (!g_exec) {
578*0Sstevel@tonic-gate 			(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
579*0Sstevel@tonic-gate 			    (dtrace_stmt_f *)info_stmt, &last);
580*0Sstevel@tonic-gate 		} else
581*0Sstevel@tonic-gate 			oprintf("\n");
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	g_total += dpi.dpi_matches;
585*0Sstevel@tonic-gate }
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate /*
588*0Sstevel@tonic-gate  * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
589*0Sstevel@tonic-gate  * storing in a driver.conf(4) file associated with the dtrace driver.
590*0Sstevel@tonic-gate  */
591*0Sstevel@tonic-gate static void
592*0Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
593*0Sstevel@tonic-gate {
594*0Sstevel@tonic-gate 	const uchar_t *p, *q;
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	if (dof == NULL)
597*0Sstevel@tonic-gate 		dfatal("failed to create DOF image for '%s'", dcp->dc_name);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	p = (uchar_t *)dof;
600*0Sstevel@tonic-gate 	q = p + dof->dofh_loadsz;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	oprintf("dof-data-%d=0x%x", n, *p++);
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	while (p < q)
605*0Sstevel@tonic-gate 		oprintf(",0x%x", *p++);
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	oprintf(";\n");
608*0Sstevel@tonic-gate 	dtrace_dof_destroy(g_dtp, dof);
609*0Sstevel@tonic-gate }
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate /*
612*0Sstevel@tonic-gate  * Link the specified D program in DOF form into an ELF file for use in either
613*0Sstevel@tonic-gate  * helpers, userland provider definitions, or both.  If -o was specified, that
614*0Sstevel@tonic-gate  * path is used as the output file name.  If -o wasn't specified and the input
615*0Sstevel@tonic-gate  * program is from a script whose name is %.d, use basename(%.o) as the output
616*0Sstevel@tonic-gate  * file name.  Otherwise we use "d.out" as the default output file name.
617*0Sstevel@tonic-gate  */
618*0Sstevel@tonic-gate static void
619*0Sstevel@tonic-gate link_prog(const dtrace_cmd_t *dcp)
620*0Sstevel@tonic-gate {
621*0Sstevel@tonic-gate 	char file[PATH_MAX], *p;
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if (g_ofile != NULL) {
624*0Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file), g_cmdc > 1 ?
625*0Sstevel@tonic-gate 		    "%s.%d" : "%s", g_ofile, (int)(dcp - g_cmdv));
626*0Sstevel@tonic-gate 	} else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
627*0Sstevel@tonic-gate 	    strcmp(p, ".d") == 0) {
628*0Sstevel@tonic-gate 		p[0] = '\0'; /* strip .d suffix */
629*0Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file),
630*0Sstevel@tonic-gate 		    "%s.o", basename(dcp->dc_arg));
631*0Sstevel@tonic-gate 	} else {
632*0Sstevel@tonic-gate 		(void) snprintf(file, sizeof (file), g_cmdc > 1 ?
633*0Sstevel@tonic-gate 		    "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
637*0Sstevel@tonic-gate 	    file, g_objc - 1, g_objv + 1) != 0)
638*0Sstevel@tonic-gate 		dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
639*0Sstevel@tonic-gate }
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate /*ARGSUSED*/
642*0Sstevel@tonic-gate static int
643*0Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
644*0Sstevel@tonic-gate {
645*0Sstevel@tonic-gate 	dtrace_probeinfo_t p;
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 	oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
648*0Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 	if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
651*0Sstevel@tonic-gate 		print_probe_info(&p);
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	return (0);
654*0Sstevel@tonic-gate }
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate /*ARGSUSED*/
657*0Sstevel@tonic-gate static int
658*0Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
659*0Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
660*0Sstevel@tonic-gate {
661*0Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	if (edp == *last)
664*0Sstevel@tonic-gate 		return (0);
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
667*0Sstevel@tonic-gate 		error("failed to match %s:%s:%s:%s: %s\n",
668*0Sstevel@tonic-gate 		    edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
669*0Sstevel@tonic-gate 		    edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
670*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	*last = edp;
674*0Sstevel@tonic-gate 	return (0);
675*0Sstevel@tonic-gate }
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate /*
678*0Sstevel@tonic-gate  * List the probes corresponding to the specified program by iterating over
679*0Sstevel@tonic-gate  * each statement and then matching probes to the statement probe descriptions.
680*0Sstevel@tonic-gate  */
681*0Sstevel@tonic-gate static void
682*0Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp)
683*0Sstevel@tonic-gate {
684*0Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
687*0Sstevel@tonic-gate 	    (dtrace_stmt_f *)list_stmt, &last);
688*0Sstevel@tonic-gate }
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate static void
691*0Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp)
692*0Sstevel@tonic-gate {
693*0Sstevel@tonic-gate 	char *arg0;
694*0Sstevel@tonic-gate 	FILE *fp;
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 	if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
697*0Sstevel@tonic-gate 		fatal("failed to open %s", dcp->dc_arg);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	arg0 = g_argv[0];
700*0Sstevel@tonic-gate 	g_argv[0] = dcp->dc_arg;
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
703*0Sstevel@tonic-gate 	    g_cflags, g_argc, g_argv)) == NULL)
704*0Sstevel@tonic-gate 		dfatal("failed to compile script %s", dcp->dc_arg);
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	g_argv[0] = arg0;
707*0Sstevel@tonic-gate 	(void) fclose(fp);
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 	dcp->dc_desc = "script";
710*0Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
711*0Sstevel@tonic-gate }
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate static void
714*0Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp)
715*0Sstevel@tonic-gate {
716*0Sstevel@tonic-gate 	char *p;
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
719*0Sstevel@tonic-gate 	    dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
720*0Sstevel@tonic-gate 		dfatal("invalid probe specifier %s", dcp->dc_arg);
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
723*0Sstevel@tonic-gate 		*p = '\0'; /* crop name for reporting */
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	dcp->dc_desc = "description";
726*0Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
727*0Sstevel@tonic-gate }
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate /*ARGSUSED*/
730*0Sstevel@tonic-gate static void
731*0Sstevel@tonic-gate prochandler(struct ps_prochandle *P, void *arg)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
734*0Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
735*0Sstevel@tonic-gate 	char name[SIG2STR_MAX];
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 	switch (Pstate(P)) {
738*0Sstevel@tonic-gate 	case PS_UNDEAD:
739*0Sstevel@tonic-gate 		/*
740*0Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
741*0Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
742*0Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
743*0Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
744*0Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
745*0Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
746*0Sstevel@tonic-gate 		 */
747*0Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
748*0Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
749*0Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
750*0Sstevel@tonic-gate 			    name, sizeof (name)));
751*0Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
752*0Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
753*0Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
754*0Sstevel@tonic-gate 		} else {
755*0Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
756*0Sstevel@tonic-gate 		}
757*0Sstevel@tonic-gate 		g_pslive--;
758*0Sstevel@tonic-gate 		break;
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	case PS_LOST:
761*0Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
762*0Sstevel@tonic-gate 		g_pslive--;
763*0Sstevel@tonic-gate 		break;
764*0Sstevel@tonic-gate 	}
765*0Sstevel@tonic-gate }
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate /*ARGSUSED*/
768*0Sstevel@tonic-gate static int
769*0Sstevel@tonic-gate errhandler(dtrace_errdata_t *data, void *arg)
770*0Sstevel@tonic-gate {
771*0Sstevel@tonic-gate 	error(data->dteda_msg);
772*0Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate /*ARGSUSED*/
776*0Sstevel@tonic-gate static int
777*0Sstevel@tonic-gate drophandler(dtrace_dropdata_t *data, void *arg)
778*0Sstevel@tonic-gate {
779*0Sstevel@tonic-gate 	error(data->dtdda_msg);
780*0Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate /*ARGSUSED*/
784*0Sstevel@tonic-gate static int
785*0Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
786*0Sstevel@tonic-gate {
787*0Sstevel@tonic-gate 	dtrace_actkind_t act;
788*0Sstevel@tonic-gate 	uintptr_t addr;
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	if (rec == NULL) {
791*0Sstevel@tonic-gate 		/*
792*0Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
793*0Sstevel@tonic-gate 		 * we're not in quiet mode.
794*0Sstevel@tonic-gate 		 */
795*0Sstevel@tonic-gate 		if (!g_quiet)
796*0Sstevel@tonic-gate 			oprintf("\n");
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	act = rec->dtrd_action;
802*0Sstevel@tonic-gate 	addr = (uintptr_t)data->dtpda_data;
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	if (act == DTRACEACT_EXIT) {
805*0Sstevel@tonic-gate 		g_status = *((uint32_t *)addr);
806*0Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
810*0Sstevel@tonic-gate }
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate /*ARGSUSED*/
813*0Sstevel@tonic-gate static int
814*0Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg)
815*0Sstevel@tonic-gate {
816*0Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
817*0Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
818*0Sstevel@tonic-gate 	static int heading;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if (g_impatient) {
821*0Sstevel@tonic-gate 		g_newline = 0;
822*0Sstevel@tonic-gate 		return (DTRACE_CONSUME_ABORT);
823*0Sstevel@tonic-gate 	}
824*0Sstevel@tonic-gate 
825*0Sstevel@tonic-gate 	if (heading == 0) {
826*0Sstevel@tonic-gate 		if (!g_flowindent) {
827*0Sstevel@tonic-gate 			if (!g_quiet) {
828*0Sstevel@tonic-gate 				oprintf("%3s %6s %32s\n",
829*0Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
830*0Sstevel@tonic-gate 			}
831*0Sstevel@tonic-gate 		} else {
832*0Sstevel@tonic-gate 			oprintf("%3s %-41s\n", "CPU", "FUNCTION");
833*0Sstevel@tonic-gate 		}
834*0Sstevel@tonic-gate 		heading = 1;
835*0Sstevel@tonic-gate 	}
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 	if (!g_flowindent) {
838*0Sstevel@tonic-gate 		if (!g_quiet) {
839*0Sstevel@tonic-gate 			char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s:%s",
842*0Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 			oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
845*0Sstevel@tonic-gate 		}
846*0Sstevel@tonic-gate 	} else {
847*0Sstevel@tonic-gate 		int indent = data->dtpda_indent;
848*0Sstevel@tonic-gate 		char *name;
849*0Sstevel@tonic-gate 		size_t len;
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
852*0Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
853*0Sstevel@tonic-gate 			name = alloca(len);
854*0Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s:%s", indent, "",
855*0Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func,
856*0Sstevel@tonic-gate 			    pd->dtpd_name);
857*0Sstevel@tonic-gate 		} else {
858*0Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + 5;
859*0Sstevel@tonic-gate 			name = alloca(len);
860*0Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s", indent, "",
861*0Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func);
862*0Sstevel@tonic-gate 		}
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 		oprintf("%3d %-41s ", cpu, name);
865*0Sstevel@tonic-gate 	}
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate static void
871*0Sstevel@tonic-gate go(void)
872*0Sstevel@tonic-gate {
873*0Sstevel@tonic-gate 	int i;
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	struct {
876*0Sstevel@tonic-gate 		char *name;
877*0Sstevel@tonic-gate 		char *optname;
878*0Sstevel@tonic-gate 		dtrace_optval_t val;
879*0Sstevel@tonic-gate 	} bufs[] = {
880*0Sstevel@tonic-gate 		{ "buffer size", "bufsize" },
881*0Sstevel@tonic-gate 		{ "aggregation size", "aggsize" },
882*0Sstevel@tonic-gate 		{ "speculation size", "specsize" },
883*0Sstevel@tonic-gate 		{ "dynamic variable size", "dynvarsize" },
884*0Sstevel@tonic-gate 		{ NULL }
885*0Sstevel@tonic-gate 	}, rates[] = {
886*0Sstevel@tonic-gate 		{ "cleaning rate", "cleanrate" },
887*0Sstevel@tonic-gate 		{ "status rate", "statusrate" },
888*0Sstevel@tonic-gate 		{ NULL }
889*0Sstevel@tonic-gate 	};
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
892*0Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
893*0Sstevel@tonic-gate 			fatal("couldn't get option %s", bufs[i].optname);
894*0Sstevel@tonic-gate 	}
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
897*0Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
898*0Sstevel@tonic-gate 			fatal("couldn't get option %s", rates[i].optname);
899*0Sstevel@tonic-gate 	}
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	if (dtrace_go(g_dtp) == -1)
902*0Sstevel@tonic-gate 		dfatal("could not enable tracing");
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
905*0Sstevel@tonic-gate 		dtrace_optval_t j = 0, mul = 10;
906*0Sstevel@tonic-gate 		dtrace_optval_t nsize;
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 		if (bufs[i].val == DTRACEOPT_UNSET)
909*0Sstevel@tonic-gate 			continue;
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 		if (nsize == DTRACEOPT_UNSET || nsize == 0)
914*0Sstevel@tonic-gate 			continue;
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 		if (nsize >= bufs[i].val - sizeof (uint64_t))
917*0Sstevel@tonic-gate 			continue;
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 		for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
920*0Sstevel@tonic-gate 			continue;
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 		if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
923*0Sstevel@tonic-gate 			error("%s lowered to %lld%c\n", bufs[i].name,
924*0Sstevel@tonic-gate 			    (long long)nsize >> (mul - 10), " kmgtpe"[j]);
925*0Sstevel@tonic-gate 		} else {
926*0Sstevel@tonic-gate 			error("%s lowered to %lld bytes\n", bufs[i].name,
927*0Sstevel@tonic-gate 			    (long long)nsize);
928*0Sstevel@tonic-gate 		}
929*0Sstevel@tonic-gate 	}
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
932*0Sstevel@tonic-gate 		dtrace_optval_t nval;
933*0Sstevel@tonic-gate 		char *dir;
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 		if (rates[i].val == DTRACEOPT_UNSET)
936*0Sstevel@tonic-gate 			continue;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
939*0Sstevel@tonic-gate 
940*0Sstevel@tonic-gate 		if (nval == DTRACEOPT_UNSET || nval == 0)
941*0Sstevel@tonic-gate 			continue;
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 		if (rates[i].val == nval)
944*0Sstevel@tonic-gate 			continue;
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 		dir = nval > rates[i].val ? "reduced" : "increased";
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 		if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
949*0Sstevel@tonic-gate 			error("%s %s to %lld hz\n", rates[i].name, dir,
950*0Sstevel@tonic-gate 			    (long long)NANOSEC / (long long)nval);
951*0Sstevel@tonic-gate 			continue;
952*0Sstevel@tonic-gate 		}
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 		if ((nval % NANOSEC) == 0) {
955*0Sstevel@tonic-gate 			error("%s %s to once every %lld seconds\n",
956*0Sstevel@tonic-gate 			    rates[i].name, dir,
957*0Sstevel@tonic-gate 			    (long long)nval / (long long)NANOSEC);
958*0Sstevel@tonic-gate 			continue;
959*0Sstevel@tonic-gate 		}
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate 		error("%s %s to once every %lld nanoseconds\n",
962*0Sstevel@tonic-gate 		    rates[i].name, dir, (long long)nval);
963*0Sstevel@tonic-gate 	}
964*0Sstevel@tonic-gate }
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate /*ARGSUSED*/
967*0Sstevel@tonic-gate static void
968*0Sstevel@tonic-gate intr(int signo)
969*0Sstevel@tonic-gate {
970*0Sstevel@tonic-gate 	if (!g_intr)
971*0Sstevel@tonic-gate 		g_newline = 1;
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 	if (g_intr++)
974*0Sstevel@tonic-gate 		g_impatient = 1;
975*0Sstevel@tonic-gate }
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate int
978*0Sstevel@tonic-gate main(int argc, char *argv[])
979*0Sstevel@tonic-gate {
980*0Sstevel@tonic-gate 	dtrace_bufdesc_t buf;
981*0Sstevel@tonic-gate 	struct sigaction act;
982*0Sstevel@tonic-gate 	dtrace_status_t status[2];
983*0Sstevel@tonic-gate 	dtrace_optval_t opt;
984*0Sstevel@tonic-gate 	dtrace_cmd_t *dcp;
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	int done = 0, mode = 0;
987*0Sstevel@tonic-gate 	int err, i;
988*0Sstevel@tonic-gate 	char c, *p, **v;
989*0Sstevel@tonic-gate 	struct ps_prochandle *P;
990*0Sstevel@tonic-gate 	pid_t pid;
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 	g_pname = basename(argv[0]);
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 	if (argc == 1)
995*0Sstevel@tonic-gate 		return (usage(stderr));
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 	if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
998*0Sstevel@tonic-gate 	    (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
999*0Sstevel@tonic-gate 	    (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1000*0Sstevel@tonic-gate 		fatal("failed to allocate memory for arguments");
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	g_argv[g_argc++] = argv[0];	/* propagate argv[0] to D as $0/$$0 */
1003*0Sstevel@tonic-gate 	argv[0] = g_pname;		/* rewrite argv[0] for getopt errors */
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 	bzero(status, sizeof (status));
1006*0Sstevel@tonic-gate 	bzero(&buf, sizeof (buf));
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	/*
1009*0Sstevel@tonic-gate 	 * Make an initial pass through argv[] processing any arguments that
1010*0Sstevel@tonic-gate 	 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1011*0Sstevel@tonic-gate 	 * We also accumulate arguments that are not affiliated with getopt
1012*0Sstevel@tonic-gate 	 * options into g_argv[], and abort if any invalid options are found.
1013*0Sstevel@tonic-gate 	 */
1014*0Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
1015*0Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1016*0Sstevel@tonic-gate 			switch (c) {
1017*0Sstevel@tonic-gate 			case '3':
1018*0Sstevel@tonic-gate 				if (strcmp(optarg, "2") != 0) {
1019*0Sstevel@tonic-gate 					(void) fprintf(stderr,
1020*0Sstevel@tonic-gate 					    "%s: illegal option -- 3%s\n",
1021*0Sstevel@tonic-gate 					    argv[0], optarg);
1022*0Sstevel@tonic-gate 					return (usage(stderr));
1023*0Sstevel@tonic-gate 				}
1024*0Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_LP64;
1025*0Sstevel@tonic-gate 				g_oflags |= DTRACE_O_ILP32;
1026*0Sstevel@tonic-gate 				break;
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate 			case '6':
1029*0Sstevel@tonic-gate 				if (strcmp(optarg, "4") != 0) {
1030*0Sstevel@tonic-gate 					(void) fprintf(stderr,
1031*0Sstevel@tonic-gate 					    "%s: illegal option -- 6%s\n",
1032*0Sstevel@tonic-gate 					    argv[0], optarg);
1033*0Sstevel@tonic-gate 					return (usage(stderr));
1034*0Sstevel@tonic-gate 				}
1035*0Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_ILP32;
1036*0Sstevel@tonic-gate 				g_oflags |= DTRACE_O_LP64;
1037*0Sstevel@tonic-gate 				break;
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 			case 'a':
1040*0Sstevel@tonic-gate 				g_grabanon++; /* also checked in pass 2 below */
1041*0Sstevel@tonic-gate 				break;
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 			case 'A':
1044*0Sstevel@tonic-gate 				g_mode = DMODE_ANON;
1045*0Sstevel@tonic-gate 				g_exec = 0;
1046*0Sstevel@tonic-gate 				mode++;
1047*0Sstevel@tonic-gate 				break;
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 			case 'e':
1050*0Sstevel@tonic-gate 				g_exec = 0;
1051*0Sstevel@tonic-gate 				done = 1;
1052*0Sstevel@tonic-gate 				break;
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 			case 'G':
1055*0Sstevel@tonic-gate 				g_mode = DMODE_LINK;
1056*0Sstevel@tonic-gate 				g_oflags |= DTRACE_O_NODEV;
1057*0Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1058*0Sstevel@tonic-gate 				g_exec = 0;
1059*0Sstevel@tonic-gate 				mode++;
1060*0Sstevel@tonic-gate 				break;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 			case 'l':
1063*0Sstevel@tonic-gate 				g_mode = DMODE_LIST;
1064*0Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1065*0Sstevel@tonic-gate 				mode++;
1066*0Sstevel@tonic-gate 				break;
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 			case 'V':
1069*0Sstevel@tonic-gate 				g_mode = DMODE_VERS;
1070*0Sstevel@tonic-gate 				mode++;
1071*0Sstevel@tonic-gate 				break;
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 			default:
1074*0Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
1075*0Sstevel@tonic-gate 					return (usage(stderr));
1076*0Sstevel@tonic-gate 			}
1077*0Sstevel@tonic-gate 		}
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 		if (optind < argc)
1080*0Sstevel@tonic-gate 			g_argv[g_argc++] = argv[optind];
1081*0Sstevel@tonic-gate 	}
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate 	if (mode > 1) {
1084*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: only one of the [-AGlV] options "
1085*0Sstevel@tonic-gate 		    "can be specified at a time\n", g_pname);
1086*0Sstevel@tonic-gate 		return (E_USAGE);
1087*0Sstevel@tonic-gate 	}
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 	if (g_mode == DMODE_VERS)
1090*0Sstevel@tonic-gate 		return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	/*
1093*0Sstevel@tonic-gate 	 * Open libdtrace.  If we are not actually going to be enabling any
1094*0Sstevel@tonic-gate 	 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1095*0Sstevel@tonic-gate 	 */
1096*0Sstevel@tonic-gate 	while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1097*0Sstevel@tonic-gate 		if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1098*0Sstevel@tonic-gate 			g_oflags |= DTRACE_O_NODEV;
1099*0Sstevel@tonic-gate 			continue;
1100*0Sstevel@tonic-gate 		}
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
1103*0Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "bufsize", "4m");
1107*0Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "aggsize", "4m");
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate 	/*
1110*0Sstevel@tonic-gate 	 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1111*0Sstevel@tonic-gate 	 * references to undefined symbols to remain as unresolved relocations.
1112*0Sstevel@tonic-gate 	 * If -A is specified, enable -xlink=primary to permit static linking
1113*0Sstevel@tonic-gate 	 * only to kernel symbols that are defined in a primary kernel module.
1114*0Sstevel@tonic-gate 	 */
1115*0Sstevel@tonic-gate 	if (g_mode == DMODE_LINK) {
1116*0Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1117*0Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "unodefs", NULL);
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate 		g_objc = g_argc;
1120*0Sstevel@tonic-gate 		g_objv = g_argv;
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 		/*
1123*0Sstevel@tonic-gate 		 * We still use g_argv[0], the name of the executable.
1124*0Sstevel@tonic-gate 		 */
1125*0Sstevel@tonic-gate 		g_argc = 1;
1126*0Sstevel@tonic-gate 	} else if (g_mode == DMODE_ANON)
1127*0Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "primary");
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate 	/*
1130*0Sstevel@tonic-gate 	 * Now that we have libdtrace open, make a second pass through argv[]
1131*0Sstevel@tonic-gate 	 * to perform any dtrace_setopt() calls and change any compiler flags.
1132*0Sstevel@tonic-gate 	 * We also accumulate any program specifications into our g_cmdv[] at
1133*0Sstevel@tonic-gate 	 * this time; these will compiled as part of the fourth processing pass.
1134*0Sstevel@tonic-gate 	 */
1135*0Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
1136*0Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1137*0Sstevel@tonic-gate 			switch (c) {
1138*0Sstevel@tonic-gate 			case 'a':
1139*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1140*0Sstevel@tonic-gate 					dfatal("failed to set -a");
1141*0Sstevel@tonic-gate 				break;
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 			case 'b':
1144*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp,
1145*0Sstevel@tonic-gate 				    "bufsize", optarg) != 0)
1146*0Sstevel@tonic-gate 					dfatal("failed to set -b %s", optarg);
1147*0Sstevel@tonic-gate 				break;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 			case 'C':
1150*0Sstevel@tonic-gate 				g_cflags |= DTRACE_C_CPP;
1151*0Sstevel@tonic-gate 				break;
1152*0Sstevel@tonic-gate 
1153*0Sstevel@tonic-gate 			case 'D':
1154*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1155*0Sstevel@tonic-gate 					dfatal("failed to set -D %s", optarg);
1156*0Sstevel@tonic-gate 				break;
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate 			case 'f':
1159*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1160*0Sstevel@tonic-gate 				dcp->dc_func = compile_str;
1161*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1162*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1163*0Sstevel@tonic-gate 				break;
1164*0Sstevel@tonic-gate 
1165*0Sstevel@tonic-gate 			case 'F':
1166*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1167*0Sstevel@tonic-gate 					dfatal("failed to set -F");
1168*0Sstevel@tonic-gate 				break;
1169*0Sstevel@tonic-gate 
1170*0Sstevel@tonic-gate 			case 'H':
1171*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1172*0Sstevel@tonic-gate 					dfatal("failed to set -H");
1173*0Sstevel@tonic-gate 				break;
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 			case 'i':
1176*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1177*0Sstevel@tonic-gate 				dcp->dc_func = compile_str;
1178*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1179*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1180*0Sstevel@tonic-gate 				break;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 			case 'I':
1183*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1184*0Sstevel@tonic-gate 					dfatal("failed to set -I %s", optarg);
1185*0Sstevel@tonic-gate 				break;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 			case 'L':
1188*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1189*0Sstevel@tonic-gate 					dfatal("failed to set -L %s", optarg);
1190*0Sstevel@tonic-gate 				break;
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 			case 'm':
1193*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1194*0Sstevel@tonic-gate 				dcp->dc_func = compile_str;
1195*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1196*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1197*0Sstevel@tonic-gate 				break;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 			case 'n':
1200*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1201*0Sstevel@tonic-gate 				dcp->dc_func = compile_str;
1202*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1203*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1204*0Sstevel@tonic-gate 				break;
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate 			case 'P':
1207*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1208*0Sstevel@tonic-gate 				dcp->dc_func = compile_str;
1209*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1210*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1211*0Sstevel@tonic-gate 				break;
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 			case 'q':
1214*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1215*0Sstevel@tonic-gate 					dfatal("failed to set -q");
1216*0Sstevel@tonic-gate 				break;
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate 			case 'o':
1219*0Sstevel@tonic-gate 				g_ofile = optarg;
1220*0Sstevel@tonic-gate 				break;
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 			case 's':
1223*0Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
1224*0Sstevel@tonic-gate 				dcp->dc_func = compile_file;
1225*0Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1226*0Sstevel@tonic-gate 				dcp->dc_arg = optarg;
1227*0Sstevel@tonic-gate 				break;
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 			case 'S':
1230*0Sstevel@tonic-gate 				g_cflags |= DTRACE_C_DIFV;
1231*0Sstevel@tonic-gate 				break;
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 			case 'U':
1234*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1235*0Sstevel@tonic-gate 					dfatal("failed to set -U %s", optarg);
1236*0Sstevel@tonic-gate 				break;
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 			case 'v':
1239*0Sstevel@tonic-gate 				g_verbose++;
1240*0Sstevel@tonic-gate 				break;
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 			case 'w':
1243*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1244*0Sstevel@tonic-gate 					dfatal("failed to set -w");
1245*0Sstevel@tonic-gate 				break;
1246*0Sstevel@tonic-gate 
1247*0Sstevel@tonic-gate 			case 'x':
1248*0Sstevel@tonic-gate 				if ((p = strchr(optarg, '=')) != NULL)
1249*0Sstevel@tonic-gate 					*p++ = '\0';
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, optarg, p) != 0)
1252*0Sstevel@tonic-gate 					dfatal("failed to set -x %s", optarg);
1253*0Sstevel@tonic-gate 				break;
1254*0Sstevel@tonic-gate 
1255*0Sstevel@tonic-gate 			case 'X':
1256*0Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1257*0Sstevel@tonic-gate 					dfatal("failed to set -X %s", optarg);
1258*0Sstevel@tonic-gate 				break;
1259*0Sstevel@tonic-gate 
1260*0Sstevel@tonic-gate 			case 'Z':
1261*0Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS;
1262*0Sstevel@tonic-gate 				break;
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 			default:
1265*0Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
1266*0Sstevel@tonic-gate 					return (usage(stderr));
1267*0Sstevel@tonic-gate 			}
1268*0Sstevel@tonic-gate 		}
1269*0Sstevel@tonic-gate 	}
1270*0Sstevel@tonic-gate 
1271*0Sstevel@tonic-gate 	/*
1272*0Sstevel@tonic-gate 	 * In our third pass we handle any command-line options related to
1273*0Sstevel@tonic-gate 	 * grabbing or creating victim processes.  The behavior of these calls
1274*0Sstevel@tonic-gate 	 * may been affected by any library options set by the second pass.
1275*0Sstevel@tonic-gate 	 */
1276*0Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
1277*0Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1278*0Sstevel@tonic-gate 			switch (c) {
1279*0Sstevel@tonic-gate 			case 'c':
1280*0Sstevel@tonic-gate 				if ((v = make_argv(optarg)) == NULL)
1281*0Sstevel@tonic-gate 					fatal("failed to allocate memory");
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 				P = dtrace_proc_create(g_dtp, v[0], v);
1284*0Sstevel@tonic-gate 				if (P == NULL)
1285*0Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 				g_psv[g_psc++] = P;
1288*0Sstevel@tonic-gate 				free(v);
1289*0Sstevel@tonic-gate 				break;
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 			case 'p':
1292*0Sstevel@tonic-gate 				errno = 0;
1293*0Sstevel@tonic-gate 				pid = strtol(optarg, &p, 10);
1294*0Sstevel@tonic-gate 
1295*0Sstevel@tonic-gate 				if (errno != 0 || p == optarg || p[0] != '\0')
1296*0Sstevel@tonic-gate 					fatal("invalid pid: %s\n", optarg);
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 				P = dtrace_proc_grab(g_dtp, pid, 0);
1299*0Sstevel@tonic-gate 				if (P == NULL)
1300*0Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 				g_psv[g_psc++] = P;
1303*0Sstevel@tonic-gate 				break;
1304*0Sstevel@tonic-gate 			}
1305*0Sstevel@tonic-gate 		}
1306*0Sstevel@tonic-gate 	}
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	/*
1309*0Sstevel@tonic-gate 	 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1310*0Sstevel@tonic-gate 	 * each string or file specification into a compiled program structure.
1311*0Sstevel@tonic-gate 	 */
1312*0Sstevel@tonic-gate 	for (i = 0; i < g_cmdc; i++)
1313*0Sstevel@tonic-gate 		g_cmdv[i].dc_func(&g_cmdv[i]);
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate 	if (g_mode != DMODE_LIST) {
1316*0Sstevel@tonic-gate 		if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1317*0Sstevel@tonic-gate 			dfatal("failed to establish error handler");
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 		if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1320*0Sstevel@tonic-gate 			dfatal("failed to establish drop handler");
1321*0Sstevel@tonic-gate 
1322*0Sstevel@tonic-gate 		if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1323*0Sstevel@tonic-gate 			dfatal("failed to establish proc handler");
1324*0Sstevel@tonic-gate 	}
1325*0Sstevel@tonic-gate 
1326*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
1327*0Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
1330*0Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
1333*0Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 	/*
1336*0Sstevel@tonic-gate 	 * Now make a fifth and final pass over the options that have been
1337*0Sstevel@tonic-gate 	 * turned into programs and saved in g_cmdv[], performing any mode-
1338*0Sstevel@tonic-gate 	 * specific processing.  If g_mode is DMODE_EXEC, we will break out
1339*0Sstevel@tonic-gate 	 * of the switch() and continue on to the data processing loop.  For
1340*0Sstevel@tonic-gate 	 * other modes, we will exit dtrace once mode-specific work is done.
1341*0Sstevel@tonic-gate 	 */
1342*0Sstevel@tonic-gate 	switch (g_mode) {
1343*0Sstevel@tonic-gate 	case DMODE_EXEC:
1344*0Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1345*0Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
1348*0Sstevel@tonic-gate 			exec_prog(&g_cmdv[i]);
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate 		if (done && !g_grabanon)
1351*0Sstevel@tonic-gate 			return (g_status);
1352*0Sstevel@tonic-gate 		break;
1353*0Sstevel@tonic-gate 
1354*0Sstevel@tonic-gate 	case DMODE_ANON:
1355*0Sstevel@tonic-gate 		if (g_ofile == NULL)
1356*0Sstevel@tonic-gate 			g_ofile = "/kernel/drv/dtrace.conf";
1357*0Sstevel@tonic-gate 
1358*0Sstevel@tonic-gate 		dof_prune(g_ofile); /* strip out any old DOF directives */
1359*0Sstevel@tonic-gate 		etcsystem_prune(); /* string out any forceload directives */
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 		if (g_cmdc == 0)
1362*0Sstevel@tonic-gate 			return (g_status);
1363*0Sstevel@tonic-gate 
1364*0Sstevel@tonic-gate 		if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1365*0Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++) {
1368*0Sstevel@tonic-gate 			anon_prog(&g_cmdv[i],
1369*0Sstevel@tonic-gate 			    dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1370*0Sstevel@tonic-gate 		}
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 		/*
1373*0Sstevel@tonic-gate 		 * Dump out the DOF corresponding to the error handler and the
1374*0Sstevel@tonic-gate 		 * current options as the final DOF property in the .conf file.
1375*0Sstevel@tonic-gate 		 */
1376*0Sstevel@tonic-gate 		anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1377*0Sstevel@tonic-gate 		anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1378*0Sstevel@tonic-gate 
1379*0Sstevel@tonic-gate 		if (fclose(g_ofp) == EOF)
1380*0Sstevel@tonic-gate 			fatal("failed to close output file '%s'", g_ofile);
1381*0Sstevel@tonic-gate 
1382*0Sstevel@tonic-gate 		/*
1383*0Sstevel@tonic-gate 		 * These messages would use notice() rather than error(), but
1384*0Sstevel@tonic-gate 		 * we don't want them suppressed when -A is run on a D program
1385*0Sstevel@tonic-gate 		 * that itself contains a #pragma D option quiet.
1386*0Sstevel@tonic-gate 		 */
1387*0Sstevel@tonic-gate 		error("saved anonymous enabling in %s\n", g_ofile);
1388*0Sstevel@tonic-gate 		etcsystem_add();
1389*0Sstevel@tonic-gate 		error("run update_drv(1M) or reboot to enable changes\n");
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate 		return (g_status);
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	case DMODE_LINK:
1394*0Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
1395*0Sstevel@tonic-gate 			link_prog(&g_cmdv[i]);
1396*0Sstevel@tonic-gate 		return (g_status);
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 	case DMODE_LIST:
1399*0Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1400*0Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
1401*0Sstevel@tonic-gate 
1402*0Sstevel@tonic-gate 		oprintf("%5s %10s %17s %33s %s\n",
1403*0Sstevel@tonic-gate 		    "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1404*0Sstevel@tonic-gate 
1405*0Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
1406*0Sstevel@tonic-gate 			list_prog(&g_cmdv[i]);
1407*0Sstevel@tonic-gate 
1408*0Sstevel@tonic-gate 		if (g_cmdc == 0)
1409*0Sstevel@tonic-gate 			(void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1410*0Sstevel@tonic-gate 
1411*0Sstevel@tonic-gate 		return (g_status);
1412*0Sstevel@tonic-gate 	}
1413*0Sstevel@tonic-gate 
1414*0Sstevel@tonic-gate 	/*
1415*0Sstevel@tonic-gate 	 * If -a and -Z were not specified and no probes have been matched, no
1416*0Sstevel@tonic-gate 	 * probe criteria was specified on the command line and we abort.
1417*0Sstevel@tonic-gate 	 */
1418*0Sstevel@tonic-gate 	if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1419*0Sstevel@tonic-gate 		dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1420*0Sstevel@tonic-gate 
1421*0Sstevel@tonic-gate 	/*
1422*0Sstevel@tonic-gate 	 * Start tracing.  Once we dtrace_go(), reload any options that affect
1423*0Sstevel@tonic-gate 	 * our globals in case consuming anonymous state has changed them.
1424*0Sstevel@tonic-gate 	 */
1425*0Sstevel@tonic-gate 	go();
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
1428*0Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
1431*0Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
1434*0Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "destructive", &opt);
1437*0Sstevel@tonic-gate 	if (opt != DTRACEOPT_UNSET)
1438*0Sstevel@tonic-gate 		notice("allowing destructive actions\n");
1439*0Sstevel@tonic-gate 
1440*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1441*0Sstevel@tonic-gate 	act.sa_flags = 0;
1442*0Sstevel@tonic-gate 	act.sa_handler = intr;
1443*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
1444*0Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 	/*
1447*0Sstevel@tonic-gate 	 * Now that tracing is active and we are ready to consume trace data,
1448*0Sstevel@tonic-gate 	 * continue any grabbed or created processes, setting them running
1449*0Sstevel@tonic-gate 	 * using the /proc control mechanism inside of libdtrace.
1450*0Sstevel@tonic-gate 	 */
1451*0Sstevel@tonic-gate 	for (i = 0; i < g_psc; i++)
1452*0Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_psv[i]);
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate 	g_pslive = g_psc; /* count for prochandler() */
1455*0Sstevel@tonic-gate 
1456*0Sstevel@tonic-gate 	do {
1457*0Sstevel@tonic-gate 		if (!g_intr && !done)
1458*0Sstevel@tonic-gate 			dtrace_sleep(g_dtp);
1459*0Sstevel@tonic-gate 
1460*0Sstevel@tonic-gate 		if (g_newline) {
1461*0Sstevel@tonic-gate 			/*
1462*0Sstevel@tonic-gate 			 * Output a newline just to make the output look
1463*0Sstevel@tonic-gate 			 * slightly cleaner.  Note that we do this even in
1464*0Sstevel@tonic-gate 			 * "quiet" mode...
1465*0Sstevel@tonic-gate 			 */
1466*0Sstevel@tonic-gate 			oprintf("\n");
1467*0Sstevel@tonic-gate 			g_newline = 0;
1468*0Sstevel@tonic-gate 		}
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 		if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1471*0Sstevel@tonic-gate 			done = 1;
1472*0Sstevel@tonic-gate 			if (dtrace_stop(g_dtp) == -1)
1473*0Sstevel@tonic-gate 				dfatal("couldn't stop tracing");
1474*0Sstevel@tonic-gate 		}
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 		switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1477*0Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_DONE:
1478*0Sstevel@tonic-gate 			done = 1;
1479*0Sstevel@tonic-gate 			break;
1480*0Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_OKAY:
1481*0Sstevel@tonic-gate 			break;
1482*0Sstevel@tonic-gate 		default:
1483*0Sstevel@tonic-gate 			if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1484*0Sstevel@tonic-gate 				dfatal("processing aborted");
1485*0Sstevel@tonic-gate 		}
1486*0Sstevel@tonic-gate 
1487*0Sstevel@tonic-gate 		if (fflush(g_ofp) == EOF)
1488*0Sstevel@tonic-gate 			clearerr(g_ofp);
1489*0Sstevel@tonic-gate 	} while (!done);
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate 	oprintf("\n");
1492*0Sstevel@tonic-gate 
1493*0Sstevel@tonic-gate 	if (!g_impatient) {
1494*0Sstevel@tonic-gate 		if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1495*0Sstevel@tonic-gate 		    dtrace_errno(g_dtp) != EINTR)
1496*0Sstevel@tonic-gate 			dfatal("failed to print aggregations");
1497*0Sstevel@tonic-gate 	}
1498*0Sstevel@tonic-gate 
1499*0Sstevel@tonic-gate 	dtrace_close(g_dtp);
1500*0Sstevel@tonic-gate 	return (g_status);
1501*0Sstevel@tonic-gate }
1502