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