xref: /illumos-gate/usr/src/cmd/dtrace/dtrace.c (revision ef150c2b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52b6e762cSahl  * Common Development and Distribution License (the "License").
62b6e762cSahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e4586ebfSmws 
227c478bd9Sstevel@tonic-gate /*
23e4586ebfSmws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26e5803b76SAdam H. Leventhal /*
27e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
28a386cc11SRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29e5803b76SAdam H. Leventhal  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/wait.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <dtrace.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <stdarg.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <alloca.h>
467c478bd9Sstevel@tonic-gate #include <libgen.h>
477c478bd9Sstevel@tonic-gate #include <libproc.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct dtrace_cmd {
507c478bd9Sstevel@tonic-gate 	void (*dc_func)(struct dtrace_cmd *);	/* function to compile arg */
517c478bd9Sstevel@tonic-gate 	dtrace_probespec_t dc_spec;		/* probe specifier context */
527c478bd9Sstevel@tonic-gate 	char *dc_arg;				/* argument from main argv */
537c478bd9Sstevel@tonic-gate 	const char *dc_name;			/* name for error messages */
547c478bd9Sstevel@tonic-gate 	const char *dc_desc;			/* desc for error messages */
557c478bd9Sstevel@tonic-gate 	dtrace_prog_t *dc_prog;			/* program compiled from arg */
560b38a8bdSahl 	char dc_ofile[PATH_MAX];		/* derived output file name */
577c478bd9Sstevel@tonic-gate } dtrace_cmd_t;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	DMODE_VERS	0	/* display version information and exit (-V) */
607c478bd9Sstevel@tonic-gate #define	DMODE_EXEC	1	/* compile program for enabling (-a/e/E) */
617c478bd9Sstevel@tonic-gate #define	DMODE_ANON	2	/* compile program for anonymous tracing (-A) */
627c478bd9Sstevel@tonic-gate #define	DMODE_LINK	3	/* compile program for linking with ELF (-G) */
637c478bd9Sstevel@tonic-gate #define	DMODE_LIST	4	/* compile program and list probes (-l) */
64900524f3Sahl #define	DMODE_HEADER	5	/* compile program for headergen (-h) */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	E_SUCCESS	0
677c478bd9Sstevel@tonic-gate #define	E_ERROR		1
687c478bd9Sstevel@tonic-gate #define	E_USAGE		2
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static const char DTRACE_OPTSTR[] =
71900524f3Sahl 	"3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static char **g_argv;
747c478bd9Sstevel@tonic-gate static int g_argc;
757c478bd9Sstevel@tonic-gate static char **g_objv;
767c478bd9Sstevel@tonic-gate static int g_objc;
777c478bd9Sstevel@tonic-gate static dtrace_cmd_t *g_cmdv;
787c478bd9Sstevel@tonic-gate static int g_cmdc;
797c478bd9Sstevel@tonic-gate static struct ps_prochandle **g_psv;
807c478bd9Sstevel@tonic-gate static int g_psc;
817c478bd9Sstevel@tonic-gate static int g_pslive;
827c478bd9Sstevel@tonic-gate static char *g_pname;
837c478bd9Sstevel@tonic-gate static int g_quiet;
847c478bd9Sstevel@tonic-gate static int g_flowindent;
857c478bd9Sstevel@tonic-gate static int g_intr;
867c478bd9Sstevel@tonic-gate static int g_impatient;
877c478bd9Sstevel@tonic-gate static int g_newline;
887c478bd9Sstevel@tonic-gate static int g_total;
897c478bd9Sstevel@tonic-gate static int g_cflags;
907c478bd9Sstevel@tonic-gate static int g_oflags;
917c478bd9Sstevel@tonic-gate static int g_verbose;
927c478bd9Sstevel@tonic-gate static int g_exec = 1;
937c478bd9Sstevel@tonic-gate static int g_mode = DMODE_EXEC;
947c478bd9Sstevel@tonic-gate static int g_status = E_SUCCESS;
957c478bd9Sstevel@tonic-gate static int g_grabanon = 0;
967c478bd9Sstevel@tonic-gate static const char *g_ofile = NULL;
977c478bd9Sstevel@tonic-gate static FILE *g_ofp = stdout;
987c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp;
997c478bd9Sstevel@tonic-gate static char *g_etcfile = "/etc/system";
1007c478bd9Sstevel@tonic-gate static const char *g_etcbegin = "* vvvv Added by DTrace";
1017c478bd9Sstevel@tonic-gate static const char *g_etcend = "* ^^^^ Added by DTrace";
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static const char *g_etc[] =  {
1047c478bd9Sstevel@tonic-gate "*",
105bbf21555SRichard Lowe "* The following forceload directives were added by dtrace(8) to allow for",
1067c478bd9Sstevel@tonic-gate "* tracing during boot.  If these directives are removed, the system will",
1077c478bd9Sstevel@tonic-gate "* continue to function, but tracing will not occur during boot as desired.",
1087c478bd9Sstevel@tonic-gate "* To remove these directives (and this block comment) automatically, run",
1097c478bd9Sstevel@tonic-gate "* \"dtrace -A\" without additional arguments.  See the \"Anonymous Tracing\"",
1107c478bd9Sstevel@tonic-gate "* chapter of the Solaris Dynamic Tracing Guide for details.",
1117c478bd9Sstevel@tonic-gate "*",
1127c478bd9Sstevel@tonic-gate NULL };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static int
usage(FILE * fp)1157c478bd9Sstevel@tonic-gate usage(FILE *fp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	static const char predact[] = "[[ predicate ] action ]";
1187c478bd9Sstevel@tonic-gate 
119900524f3Sahl 	(void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
1207c478bd9Sstevel@tonic-gate 	    "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
1217c478bd9Sstevel@tonic-gate 	    "[-o output] [-p pid] [-s script] [-U name]\n\t"
1227c478bd9Sstevel@tonic-gate 	    "[-x opt[=val]] [-X a|c|s|t]\n\n"
1237c478bd9Sstevel@tonic-gate 	    "\t[-P provider %s]\n"
1247c478bd9Sstevel@tonic-gate 	    "\t[-m [ provider: ] module %s]\n"
1257c478bd9Sstevel@tonic-gate 	    "\t[-f [[ provider: ] module: ] func %s]\n"
1267c478bd9Sstevel@tonic-gate 	    "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
1277c478bd9Sstevel@tonic-gate 	    "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
1287c478bd9Sstevel@tonic-gate 	    predact, predact, predact, predact, predact);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
1317c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\t   action -> '{' D-statements '}'\n");
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\n"
1347c478bd9Sstevel@tonic-gate 	    "\t-32 generate 32-bit D programs and ELF files\n"
1357c478bd9Sstevel@tonic-gate 	    "\t-64 generate 64-bit D programs and ELF files\n\n"
1367c478bd9Sstevel@tonic-gate 	    "\t-a  claim anonymous tracing state\n"
137bbf21555SRichard Lowe 	    "\t-A  generate driver.conf(5) directives for anonymous tracing\n"
1387c478bd9Sstevel@tonic-gate 	    "\t-b  set trace buffer size\n"
1397c478bd9Sstevel@tonic-gate 	    "\t-c  run specified command and exit upon its completion\n"
1407c478bd9Sstevel@tonic-gate 	    "\t-C  run cpp(1) preprocessor on script files\n"
1417c478bd9Sstevel@tonic-gate 	    "\t-D  define symbol when invoking preprocessor\n"
1427c478bd9Sstevel@tonic-gate 	    "\t-e  exit after compiling request but prior to enabling probes\n"
1437c478bd9Sstevel@tonic-gate 	    "\t-f  enable or list probes matching the specified function name\n"
1447c478bd9Sstevel@tonic-gate 	    "\t-F  coalesce trace output by function\n"
1457c478bd9Sstevel@tonic-gate 	    "\t-G  generate an ELF file containing embedded dtrace program\n"
146900524f3Sahl 	    "\t-h  generate a header file with definitions for static probes\n"
1477c478bd9Sstevel@tonic-gate 	    "\t-H  print included files when invoking preprocessor\n"
1487c478bd9Sstevel@tonic-gate 	    "\t-i  enable or list probes matching the specified probe id\n"
1497c478bd9Sstevel@tonic-gate 	    "\t-I  add include directory to preprocessor search path\n"
1507c478bd9Sstevel@tonic-gate 	    "\t-l  list probes matching specified criteria\n"
1517c478bd9Sstevel@tonic-gate 	    "\t-L  add library directory to library search path\n"
1527c478bd9Sstevel@tonic-gate 	    "\t-m  enable or list probes matching the specified module name\n"
1537c478bd9Sstevel@tonic-gate 	    "\t-n  enable or list probes matching the specified probe name\n"
1547c478bd9Sstevel@tonic-gate 	    "\t-o  set output file\n"
1557c478bd9Sstevel@tonic-gate 	    "\t-p  grab specified process-ID and cache its symbol tables\n"
1567c478bd9Sstevel@tonic-gate 	    "\t-P  enable or list probes matching the specified provider name\n"
1577c478bd9Sstevel@tonic-gate 	    "\t-q  set quiet mode (only output explicitly traced data)\n"
1587c478bd9Sstevel@tonic-gate 	    "\t-s  enable or list probes according to the specified D script\n"
1597c478bd9Sstevel@tonic-gate 	    "\t-S  print D compiler intermediate code\n"
1607c478bd9Sstevel@tonic-gate 	    "\t-U  undefine symbol when invoking preprocessor\n"
1617c478bd9Sstevel@tonic-gate 	    "\t-v  set verbose mode (report stability attributes, arguments)\n"
1627c478bd9Sstevel@tonic-gate 	    "\t-V  report DTrace API version\n"
1637c478bd9Sstevel@tonic-gate 	    "\t-w  permit destructive actions\n"
1647c478bd9Sstevel@tonic-gate 	    "\t-x  enable or modify compiler and tracing options\n"
1657c478bd9Sstevel@tonic-gate 	    "\t-X  specify ISO C conformance settings for preprocessor\n"
1667c478bd9Sstevel@tonic-gate 	    "\t-Z  permit probe descriptions that match zero probes\n");
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (E_USAGE);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static void
verror(const char * fmt,va_list ap)1727c478bd9Sstevel@tonic-gate verror(const char *fmt, va_list ap)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	int error = errno;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
1777c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
1807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(error));
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1847c478bd9Sstevel@tonic-gate static void
fatal(const char * fmt,...)1857c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	va_list ap;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1907c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
1917c478bd9Sstevel@tonic-gate 	va_end(ap);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1977c478bd9Sstevel@tonic-gate static void
dfatal(const char * fmt,...)1987c478bd9Sstevel@tonic-gate dfatal(const char *fmt, ...)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	va_list ap;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
2057c478bd9Sstevel@tonic-gate 	if (fmt != NULL)
2067c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	va_end(ap);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n",
2127c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2137c478bd9Sstevel@tonic-gate 	} else if (fmt == NULL) {
2147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n",
2157c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2182b6e762cSahl 	/*
2192b6e762cSahl 	 * Close the DTrace handle to ensure that any controlled processes are
2202b6e762cSahl 	 * correctly restored and continued.
2212b6e762cSahl 	 */
2222b6e762cSahl 	dtrace_close(g_dtp);
2232b6e762cSahl 
2247c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2287c478bd9Sstevel@tonic-gate static void
error(const char * fmt,...)2297c478bd9Sstevel@tonic-gate error(const char *fmt, ...)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	va_list ap;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2347c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
2357c478bd9Sstevel@tonic-gate 	va_end(ap);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2397c478bd9Sstevel@tonic-gate static void
notice(const char * fmt,...)2407c478bd9Sstevel@tonic-gate notice(const char *fmt, ...)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	va_list ap;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (g_quiet)
2457c478bd9Sstevel@tonic-gate 		return; /* -q or quiet pragma suppresses notice()s */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2487c478bd9Sstevel@tonic-gate 	verror(fmt, ap);
2497c478bd9Sstevel@tonic-gate 	va_end(ap);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
2537c478bd9Sstevel@tonic-gate static void
oprintf(const char * fmt,...)2547c478bd9Sstevel@tonic-gate oprintf(const char *fmt, ...)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	va_list ap;
2577c478bd9Sstevel@tonic-gate 	int n;
2587c478bd9Sstevel@tonic-gate 
25930ef842dSbmc 	if (g_ofp == NULL)
26030ef842dSbmc 		return;
26130ef842dSbmc 
2627c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2637c478bd9Sstevel@tonic-gate 	n = vfprintf(g_ofp, fmt, ap);
2647c478bd9Sstevel@tonic-gate 	va_end(ap);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (n < 0) {
2677c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
2687c478bd9Sstevel@tonic-gate 			fatal("failed to write to %s",
2697c478bd9Sstevel@tonic-gate 			    g_ofile ? g_ofile : "<stdout>");
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 		clearerr(g_ofp);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static char **
make_argv(char * s)2767c478bd9Sstevel@tonic-gate make_argv(char *s)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	const char *ws = "\f\n\r\t\v ";
2797c478bd9Sstevel@tonic-gate 	char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
2807c478bd9Sstevel@tonic-gate 	int argc = 0;
2817c478bd9Sstevel@tonic-gate 	char *p = s;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (argv == NULL)
2847c478bd9Sstevel@tonic-gate 		return (NULL);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
2877c478bd9Sstevel@tonic-gate 		argv[argc++] = p;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (argc == 0)
2907c478bd9Sstevel@tonic-gate 		argv[argc++] = s;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
2937c478bd9Sstevel@tonic-gate 	return (argv);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static void
dof_prune(const char * fname)2977c478bd9Sstevel@tonic-gate dof_prune(const char *fname)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	struct stat sbuf;
3007c478bd9Sstevel@tonic-gate 	size_t sz, i, j, mark, len;
3017c478bd9Sstevel@tonic-gate 	char *buf;
3027c478bd9Sstevel@tonic-gate 	int msg = 0, fd;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1) {
3057c478bd9Sstevel@tonic-gate 		/*
3067c478bd9Sstevel@tonic-gate 		 * This is okay only if the file doesn't exist at all.
3077c478bd9Sstevel@tonic-gate 		 */
3087c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
3097c478bd9Sstevel@tonic-gate 			fatal("failed to open %s", fname);
3107c478bd9Sstevel@tonic-gate 		return;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3147c478bd9Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3177c478bd9Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3207c478bd9Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	buf[sz] = '\0';
3237c478bd9Sstevel@tonic-gate 	(void) close(fd);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
3267c478bd9Sstevel@tonic-gate 		fatal("failed to open %s for writing", fname);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	len = strlen("dof-data-");
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	for (mark = 0, i = 0; i < sz; i++) {
3317c478bd9Sstevel@tonic-gate 		if (strncmp(&buf[i], "dof-data-", len) != 0)
3327c478bd9Sstevel@tonic-gate 			continue;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		/*
3357c478bd9Sstevel@tonic-gate 		 * This is only a match if it's in the 0th column.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (i != 0 && buf[i - 1] != '\n')
3387c478bd9Sstevel@tonic-gate 			continue;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		if (msg++ == 0) {
3417c478bd9Sstevel@tonic-gate 			error("cleaned up old anonymous "
3427c478bd9Sstevel@tonic-gate 			    "enabling in %s\n", fname);
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		/*
3467c478bd9Sstevel@tonic-gate 		 * We have a match.  First write out our data up until now.
3477c478bd9Sstevel@tonic-gate 		 */
3487c478bd9Sstevel@tonic-gate 		if (i != mark) {
3497c478bd9Sstevel@tonic-gate 			if (write(fd, &buf[mark], i - mark) != i - mark)
3507c478bd9Sstevel@tonic-gate 				fatal("failed to write to %s", fname);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		/*
3547c478bd9Sstevel@tonic-gate 		 * Now scan forward until we scan past a newline.
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		for (j = i; j < sz && buf[j] != '\n'; j++)
3577c478bd9Sstevel@tonic-gate 			continue;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * Reset our mark.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		if ((mark = j + 1) >= sz)
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		i = j;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (mark < sz) {
3697c478bd9Sstevel@tonic-gate 		if (write(fd, &buf[mark], sz - mark) != sz - mark)
3707c478bd9Sstevel@tonic-gate 			fatal("failed to write to %s", fname);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	(void) close(fd);
3747c478bd9Sstevel@tonic-gate 	free(buf);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static void
etcsystem_prune(void)3787c478bd9Sstevel@tonic-gate etcsystem_prune(void)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	struct stat sbuf;
3817c478bd9Sstevel@tonic-gate 	size_t sz;
3827c478bd9Sstevel@tonic-gate 	char *buf, *start, *end;
3837c478bd9Sstevel@tonic-gate 	int fd;
3847c478bd9Sstevel@tonic-gate 	char *fname = g_etcfile, *tmpname;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1)
3877c478bd9Sstevel@tonic-gate 		fatal("failed to open %s", fname);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sbuf) == -1)
3907c478bd9Sstevel@tonic-gate 		fatal("failed to fstat %s", fname);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
3937c478bd9Sstevel@tonic-gate 		fatal("failed to allocate memory for %s", fname);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (read(fd, buf, sz) != sz)
3967c478bd9Sstevel@tonic-gate 		fatal("failed to read %s", fname);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	buf[sz] = '\0';
3997c478bd9Sstevel@tonic-gate 	(void) close(fd);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if ((start = strstr(buf, g_etcbegin)) == NULL)
4027c478bd9Sstevel@tonic-gate 		goto out;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (strlen(buf) != sz) {
4057c478bd9Sstevel@tonic-gate 		fatal("embedded nul byte in %s; manual repair of %s "
4067c478bd9Sstevel@tonic-gate 		    "required\n", fname, fname);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (strstr(start + 1, g_etcbegin) != NULL) {
4107c478bd9Sstevel@tonic-gate 		fatal("multiple start sentinels in %s; manual repair of %s "
4117c478bd9Sstevel@tonic-gate 		    "required\n", fname, fname);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if ((end = strstr(buf, g_etcend)) == NULL) {
4157c478bd9Sstevel@tonic-gate 		fatal("missing end sentinel in %s; manual repair of %s "
4167c478bd9Sstevel@tonic-gate 		    "required\n", fname, fname);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (start > end) {
4207c478bd9Sstevel@tonic-gate 		fatal("end sentinel preceeds start sentinel in %s; manual "
4217c478bd9Sstevel@tonic-gate 		    "repair of %s required\n", fname, fname);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	end += strlen(g_etcend) + 1;
4257c478bd9Sstevel@tonic-gate 	bcopy(end, start, strlen(end) + 1);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	tmpname = alloca(sz = strlen(fname) + 80);
4287c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if ((fd = open(tmpname,
4317c478bd9Sstevel@tonic-gate 	    O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
4327c478bd9Sstevel@tonic-gate 		fatal("failed to create %s", tmpname);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (write(fd, buf, strlen(buf)) < strlen(buf)) {
4357c478bd9Sstevel@tonic-gate 		(void) unlink(tmpname);
4367c478bd9Sstevel@tonic-gate 		fatal("failed to write to %s", tmpname);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	(void) close(fd);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
4427c478bd9Sstevel@tonic-gate 		(void) unlink(tmpname);
4437c478bd9Sstevel@tonic-gate 		fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
4447c478bd9Sstevel@tonic-gate 		    (int)sbuf.st_uid, (int)sbuf.st_gid);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (rename(tmpname, fname) == -1)
4487c478bd9Sstevel@tonic-gate 		fatal("rename of %s to %s failed", tmpname, fname);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	error("cleaned up forceload directives in %s\n", fname);
4517c478bd9Sstevel@tonic-gate out:
4527c478bd9Sstevel@tonic-gate 	free(buf);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate static void
etcsystem_add(void)4567c478bd9Sstevel@tonic-gate etcsystem_add(void)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	const char *mods[20];
4597c478bd9Sstevel@tonic-gate 	int nmods, line;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
4627c478bd9Sstevel@tonic-gate 		fatal("failed to open output file '%s'", g_ofile);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	oprintf("%s\n", g_etcbegin);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	for (line = 0; g_etc[line] != NULL; line++)
4677c478bd9Sstevel@tonic-gate 		oprintf("%s\n", g_etc[line]);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	nmods = dtrace_provider_modules(g_dtp, mods,
4707c478bd9Sstevel@tonic-gate 	    sizeof (mods) / sizeof (char *) - 1);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (nmods >= sizeof (mods) / sizeof (char *))
4737c478bd9Sstevel@tonic-gate 		fatal("unexpectedly large number of modules!");
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	mods[nmods++] = "dtrace";
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	for (line = 0; line < nmods; line++)
4787c478bd9Sstevel@tonic-gate 		oprintf("forceload: drv/%s\n", mods[line]);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	oprintf("%s\n", g_etcend);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (fclose(g_ofp) == EOF)
4837c478bd9Sstevel@tonic-gate 		fatal("failed to close output file '%s'", g_ofile);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	error("added forceload directives to %s\n", g_ofile);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate static void
print_probe_info(const dtrace_probeinfo_t * p)4897c478bd9Sstevel@tonic-gate print_probe_info(const dtrace_probeinfo_t *p)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
492a386cc11SRobert Mustacchi 	char *user;
4937c478bd9Sstevel@tonic-gate 	int i;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	oprintf("\n\tProbe Description Attributes\n");
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
4987c478bd9Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_name));
4997c478bd9Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
5007c478bd9Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_attr.dtat_data));
5017c478bd9Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
5027c478bd9Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_attr.dtat_class));
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	oprintf("\n\tArgument Attributes\n");
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	oprintf("\t\tIdentifier Names: %s\n",
5077c478bd9Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_name));
5087c478bd9Sstevel@tonic-gate 	oprintf("\t\tData Semantics:   %s\n",
5097c478bd9Sstevel@tonic-gate 	    dtrace_stability_name(p->dtp_arga.dtat_data));
5107c478bd9Sstevel@tonic-gate 	oprintf("\t\tDependency Class: %s\n",
5117c478bd9Sstevel@tonic-gate 	    dtrace_class_name(p->dtp_arga.dtat_class));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	oprintf("\n\tArgument Types\n");
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	for (i = 0; i < p->dtp_argc; i++) {
516a386cc11SRobert Mustacchi 		if (p->dtp_argv[i].dtt_flags & DTT_FL_USER)
517a386cc11SRobert Mustacchi 			user = "userland ";
518a386cc11SRobert Mustacchi 		else
519a386cc11SRobert Mustacchi 			user = "";
5207c478bd9Sstevel@tonic-gate 		if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
5217c478bd9Sstevel@tonic-gate 		    p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
5227c478bd9Sstevel@tonic-gate 			(void) strlcpy(buf, "(unknown)", sizeof (buf));
523a386cc11SRobert Mustacchi 		oprintf("\t\targs[%d]: %s%s\n", i, user, buf);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (p->dtp_argc == 0)
5277c478bd9Sstevel@tonic-gate 		oprintf("\t\tNone\n");
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	oprintf("\n");
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5337c478bd9Sstevel@tonic-gate static int
info_stmt(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,dtrace_stmtdesc_t * stp,dtrace_ecbdesc_t ** last)5347c478bd9Sstevel@tonic-gate info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
5357c478bd9Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
5387c478bd9Sstevel@tonic-gate 	dtrace_probedesc_t *pdp = &edp->dted_probe;
5397c478bd9Sstevel@tonic-gate 	dtrace_probeinfo_t p;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (edp == *last)
5427c478bd9Sstevel@tonic-gate 		return (0);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	oprintf("\n%s:%s:%s:%s\n",
5457c478bd9Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (dtrace_probe_info(dtp, pdp, &p) == 0)
5487c478bd9Sstevel@tonic-gate 		print_probe_info(&p);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	*last = edp;
5517c478bd9Sstevel@tonic-gate 	return (0);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Execute the specified program by enabling the corresponding instrumentation.
5567c478bd9Sstevel@tonic-gate  * If -e has been specified, we get the program info but do not enable it.  If
5577c478bd9Sstevel@tonic-gate  * -v has been specified, we print a stability report for the program.
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static void
exec_prog(const dtrace_cmd_t * dcp)5607c478bd9Sstevel@tonic-gate exec_prog(const dtrace_cmd_t *dcp)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
5637c478bd9Sstevel@tonic-gate 	dtrace_proginfo_t dpi;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (!g_exec) {
5667c478bd9Sstevel@tonic-gate 		dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
5677c478bd9Sstevel@tonic-gate 	} else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
5687c478bd9Sstevel@tonic-gate 		dfatal("failed to enable '%s'", dcp->dc_name);
5697c478bd9Sstevel@tonic-gate 	} else {
5707c478bd9Sstevel@tonic-gate 		notice("%s '%s' matched %u probe%s\n",
5717c478bd9Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name,
5727c478bd9Sstevel@tonic-gate 		    dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (g_verbose) {
5767c478bd9Sstevel@tonic-gate 		oprintf("\nStability attributes for %s %s:\n",
5777c478bd9Sstevel@tonic-gate 		    dcp->dc_desc, dcp->dc_name);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		oprintf("\n\tMinimum Probe Description Attributes\n");
5807c478bd9Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5817c478bd9Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_name));
5827c478bd9Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5837c478bd9Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_descattr.dtat_data));
5847c478bd9Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5857c478bd9Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_descattr.dtat_class));
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		oprintf("\n\tMinimum Statement Attributes\n");
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		oprintf("\t\tIdentifier Names: %s\n",
5907c478bd9Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
5917c478bd9Sstevel@tonic-gate 		oprintf("\t\tData Semantics:   %s\n",
5927c478bd9Sstevel@tonic-gate 		    dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
5937c478bd9Sstevel@tonic-gate 		oprintf("\t\tDependency Class: %s\n",
5947c478bd9Sstevel@tonic-gate 		    dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		if (!g_exec) {
5977c478bd9Sstevel@tonic-gate 			(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
5987c478bd9Sstevel@tonic-gate 			    (dtrace_stmt_f *)info_stmt, &last);
5997c478bd9Sstevel@tonic-gate 		} else
6007c478bd9Sstevel@tonic-gate 			oprintf("\n");
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	g_total += dpi.dpi_matches;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate  * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
608bbf21555SRichard Lowe  * storing in a driver.conf(5) file associated with the dtrace driver.
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate static void
anon_prog(const dtrace_cmd_t * dcp,dof_hdr_t * dof,int n)6117c478bd9Sstevel@tonic-gate anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	const uchar_t *p, *q;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (dof == NULL)
6167c478bd9Sstevel@tonic-gate 		dfatal("failed to create DOF image for '%s'", dcp->dc_name);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	p = (uchar_t *)dof;
6197c478bd9Sstevel@tonic-gate 	q = p + dof->dofh_loadsz;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	oprintf("dof-data-%d=0x%x", n, *p++);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	while (p < q)
6247c478bd9Sstevel@tonic-gate 		oprintf(",0x%x", *p++);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	oprintf(";\n");
6277c478bd9Sstevel@tonic-gate 	dtrace_dof_destroy(g_dtp, dof);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * Link the specified D program in DOF form into an ELF file for use in either
6327c478bd9Sstevel@tonic-gate  * helpers, userland provider definitions, or both.  If -o was specified, that
6337c478bd9Sstevel@tonic-gate  * path is used as the output file name.  If -o wasn't specified and the input
6347c478bd9Sstevel@tonic-gate  * program is from a script whose name is %.d, use basename(%.o) as the output
6357c478bd9Sstevel@tonic-gate  * file name.  Otherwise we use "d.out" as the default output file name.
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate static void
link_prog(dtrace_cmd_t * dcp)6380b38a8bdSahl link_prog(dtrace_cmd_t *dcp)
6397c478bd9Sstevel@tonic-gate {
6400b38a8bdSahl 	char *p;
6417c478bd9Sstevel@tonic-gate 
6420b38a8bdSahl 	if (g_cmdc == 1 && g_ofile != NULL) {
6430b38a8bdSahl 		(void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
6447c478bd9Sstevel@tonic-gate 	} else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
6457c478bd9Sstevel@tonic-gate 	    strcmp(p, ".d") == 0) {
6467c478bd9Sstevel@tonic-gate 		p[0] = '\0'; /* strip .d suffix */
6470b38a8bdSahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
6487c478bd9Sstevel@tonic-gate 		    "%s.o", basename(dcp->dc_arg));
6497c478bd9Sstevel@tonic-gate 	} else {
6500b38a8bdSahl 		(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
6510b38a8bdSahl 		    g_cmdc > 1 ?  "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
6552b6e762cSahl 	    dcp->dc_ofile, g_objc, g_objv) != 0)
6567c478bd9Sstevel@tonic-gate 		dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6607c478bd9Sstevel@tonic-gate static int
list_probe(dtrace_hdl_t * dtp,const dtrace_probedesc_t * pdp,void * arg)6617c478bd9Sstevel@tonic-gate list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	dtrace_probeinfo_t p;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
6667c478bd9Sstevel@tonic-gate 	    pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
6697c478bd9Sstevel@tonic-gate 		print_probe_info(&p);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	return (0);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6757c478bd9Sstevel@tonic-gate static int
list_stmt(dtrace_hdl_t * dtp,dtrace_prog_t * pgp,dtrace_stmtdesc_t * stp,dtrace_ecbdesc_t ** last)6767c478bd9Sstevel@tonic-gate list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
6777c478bd9Sstevel@tonic-gate     dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (edp == *last)
6827c478bd9Sstevel@tonic-gate 		return (0);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
6857c478bd9Sstevel@tonic-gate 		error("failed to match %s:%s:%s:%s: %s\n",
6867c478bd9Sstevel@tonic-gate 		    edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
6877c478bd9Sstevel@tonic-gate 		    edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
6887c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	*last = edp;
6927c478bd9Sstevel@tonic-gate 	return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate  * List the probes corresponding to the specified program by iterating over
6977c478bd9Sstevel@tonic-gate  * each statement and then matching probes to the statement probe descriptions.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate static void
list_prog(const dtrace_cmd_t * dcp)7007c478bd9Sstevel@tonic-gate list_prog(const dtrace_cmd_t *dcp)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
7057c478bd9Sstevel@tonic-gate 	    (dtrace_stmt_f *)list_stmt, &last);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate static void
compile_file(dtrace_cmd_t * dcp)7097c478bd9Sstevel@tonic-gate compile_file(dtrace_cmd_t *dcp)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	char *arg0;
7127c478bd9Sstevel@tonic-gate 	FILE *fp;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
7157c478bd9Sstevel@tonic-gate 		fatal("failed to open %s", dcp->dc_arg);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	arg0 = g_argv[0];
7187c478bd9Sstevel@tonic-gate 	g_argv[0] = dcp->dc_arg;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
7217c478bd9Sstevel@tonic-gate 	    g_cflags, g_argc, g_argv)) == NULL)
7227c478bd9Sstevel@tonic-gate 		dfatal("failed to compile script %s", dcp->dc_arg);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	g_argv[0] = arg0;
7257c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	dcp->dc_desc = "script";
7287c478bd9Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static void
compile_str(dtrace_cmd_t * dcp)7327c478bd9Sstevel@tonic-gate compile_str(dtrace_cmd_t *dcp)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	char *p;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
7377c478bd9Sstevel@tonic-gate 	    dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
7387c478bd9Sstevel@tonic-gate 		dfatal("invalid probe specifier %s", dcp->dc_arg);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
7417c478bd9Sstevel@tonic-gate 		*p = '\0'; /* crop name for reporting */
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	dcp->dc_desc = "description";
7447c478bd9Sstevel@tonic-gate 	dcp->dc_name = dcp->dc_arg;
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7487c478bd9Sstevel@tonic-gate static void
prochandler(struct ps_prochandle * P,const char * msg,void * arg)749900524f3Sahl prochandler(struct ps_prochandle *P, const char *msg, void *arg)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	const psinfo_t *prp = Ppsinfo(P);
7527c478bd9Sstevel@tonic-gate 	int pid = Pstatus(P)->pr_pid;
7537c478bd9Sstevel@tonic-gate 	char name[SIG2STR_MAX];
7547c478bd9Sstevel@tonic-gate 
755900524f3Sahl 	if (msg != NULL) {
756900524f3Sahl 		notice("pid %d: %s\n", pid, msg);
757900524f3Sahl 		return;
758900524f3Sahl 	}
759900524f3Sahl 
7607c478bd9Sstevel@tonic-gate 	switch (Pstate(P)) {
7617c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
7627c478bd9Sstevel@tonic-gate 		/*
7637c478bd9Sstevel@tonic-gate 		 * Ideally we would like to always report pr_wstat here, but it
7647c478bd9Sstevel@tonic-gate 		 * isn't possible given current /proc semantics.  If we grabbed
7657c478bd9Sstevel@tonic-gate 		 * the process, Ppsinfo() will either fail or return a zeroed
7667c478bd9Sstevel@tonic-gate 		 * psinfo_t depending on how far the parent is in reaping it.
7677c478bd9Sstevel@tonic-gate 		 * When /proc provides a stable pr_wstat in the status file,
7687c478bd9Sstevel@tonic-gate 		 * this code can be improved by examining this new pr_wstat.
7697c478bd9Sstevel@tonic-gate 		 */
7707c478bd9Sstevel@tonic-gate 		if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
7717c478bd9Sstevel@tonic-gate 			notice("pid %d terminated by %s\n", pid,
7727c478bd9Sstevel@tonic-gate 			    proc_signame(WTERMSIG(prp->pr_wstat),
7737c478bd9Sstevel@tonic-gate 			    name, sizeof (name)));
7747c478bd9Sstevel@tonic-gate 		} else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
7757c478bd9Sstevel@tonic-gate 			notice("pid %d exited with status %d\n",
7767c478bd9Sstevel@tonic-gate 			    pid, WEXITSTATUS(prp->pr_wstat));
7777c478bd9Sstevel@tonic-gate 		} else {
7787c478bd9Sstevel@tonic-gate 			notice("pid %d has exited\n", pid);
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		g_pslive--;
7817c478bd9Sstevel@tonic-gate 		break;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	case PS_LOST:
7847c478bd9Sstevel@tonic-gate 		notice("pid %d exec'd a set-id or unobservable program\n", pid);
7857c478bd9Sstevel@tonic-gate 		g_pslive--;
7867c478bd9Sstevel@tonic-gate 		break;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7917c478bd9Sstevel@tonic-gate static int
errhandler(const dtrace_errdata_t * data,void * arg)792a1b5e537Sbmc errhandler(const dtrace_errdata_t *data, void *arg)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	error(data->dteda_msg);
7957c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7997c478bd9Sstevel@tonic-gate static int
drophandler(const dtrace_dropdata_t * data,void * arg)800a1b5e537Sbmc drophandler(const dtrace_dropdata_t *data, void *arg)
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	error(data->dtdda_msg);
8037c478bd9Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
806a1b5e537Sbmc /*ARGSUSED*/
807a1b5e537Sbmc static int
setopthandler(const dtrace_setoptdata_t * data,void * arg)808a1b5e537Sbmc setopthandler(const dtrace_setoptdata_t *data, void *arg)
809a1b5e537Sbmc {
810a1b5e537Sbmc 	if (strcmp(data->dtsda_option, "quiet") == 0)
811a1b5e537Sbmc 		g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
812a1b5e537Sbmc 
813a1b5e537Sbmc 	if (strcmp(data->dtsda_option, "flowindent") == 0)
814a1b5e537Sbmc 		g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
815a1b5e537Sbmc 
816a1b5e537Sbmc 	return (DTRACE_HANDLE_OK);
817a1b5e537Sbmc }
818a1b5e537Sbmc 
81930ef842dSbmc #define	BUFDUMPHDR(hdr) \
82030ef842dSbmc 	(void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
82130ef842dSbmc 
82230ef842dSbmc #define	BUFDUMPSTR(ptr, field) \
82330ef842dSbmc 	(void) printf("%s: %20s => ", g_pname, #field);	\
82430ef842dSbmc 	if ((ptr)->field != NULL) {			\
82530ef842dSbmc 		const char *c = (ptr)->field;		\
82630ef842dSbmc 		(void) printf("\"");			\
82730ef842dSbmc 		do {					\
82830ef842dSbmc 			if (*c == '\n') {		\
82930ef842dSbmc 				(void) printf("\\n");	\
83030ef842dSbmc 				continue;		\
83130ef842dSbmc 			}				\
83230ef842dSbmc 							\
83330ef842dSbmc 			(void) printf("%c", *c);	\
83430ef842dSbmc 		} while (*c++ != '\0');			\
83530ef842dSbmc 		(void) printf("\"\n");			\
83630ef842dSbmc 	} else {					\
83730ef842dSbmc 		(void) printf("<NULL>\n");		\
83830ef842dSbmc 	}
83930ef842dSbmc 
84030ef842dSbmc #define	BUFDUMPASSTR(ptr, field, str) \
84130ef842dSbmc 	(void) printf("%s: %20s => %s\n", g_pname, #field, str);
84230ef842dSbmc 
84330ef842dSbmc #define	BUFDUMP(ptr, field) \
84430ef842dSbmc 	(void) printf("%s: %20s => %lld\n", g_pname, #field, \
84530ef842dSbmc 	    (long long)(ptr)->field);
84630ef842dSbmc 
84730ef842dSbmc #define	BUFDUMPPTR(ptr, field) \
84830ef842dSbmc 	(void) printf("%s: %20s => %s\n", g_pname, #field, \
84930ef842dSbmc 	    (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
85030ef842dSbmc 
85130ef842dSbmc /*ARGSUSED*/
85230ef842dSbmc static int
bufhandler(const dtrace_bufdata_t * bufdata,void * arg)85330ef842dSbmc bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
85430ef842dSbmc {
85530ef842dSbmc 	const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
85630ef842dSbmc 	const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
85730ef842dSbmc 	const dtrace_probedesc_t *pd;
85830ef842dSbmc 	uint32_t flags = bufdata->dtbda_flags;
85930ef842dSbmc 	char buf[512], *c = buf, *end = c + sizeof (buf);
86030ef842dSbmc 	int i, printed;
86130ef842dSbmc 
86230ef842dSbmc 	struct {
86330ef842dSbmc 		const char *name;
86430ef842dSbmc 		uint32_t value;
86530ef842dSbmc 	} flagnames[] = {
86630ef842dSbmc 	    { "AGGVAL",		DTRACE_BUFDATA_AGGVAL },
86730ef842dSbmc 	    { "AGGKEY",		DTRACE_BUFDATA_AGGKEY },
86830ef842dSbmc 	    { "AGGFORMAT",	DTRACE_BUFDATA_AGGFORMAT },
86930ef842dSbmc 	    { "AGGLAST",	DTRACE_BUFDATA_AGGLAST },
87030ef842dSbmc 	    { "???",		UINT32_MAX },
87130ef842dSbmc 	    { NULL }
87230ef842dSbmc 	};
87330ef842dSbmc 
87430ef842dSbmc 	if (bufdata->dtbda_probe != NULL) {
87530ef842dSbmc 		pd = bufdata->dtbda_probe->dtpda_pdesc;
87630ef842dSbmc 	} else if (agg != NULL) {
87730ef842dSbmc 		pd = agg->dtada_pdesc;
87830ef842dSbmc 	} else {
87930ef842dSbmc 		pd = NULL;
88030ef842dSbmc 	}
88130ef842dSbmc 
88230ef842dSbmc 	BUFDUMPHDR(">>> Called buffer handler");
88330ef842dSbmc 	BUFDUMPHDR("");
88430ef842dSbmc 
88530ef842dSbmc 	BUFDUMPHDR("  dtrace_bufdata");
88630ef842dSbmc 	BUFDUMPSTR(bufdata, dtbda_buffered);
88730ef842dSbmc 	BUFDUMPPTR(bufdata, dtbda_probe);
88830ef842dSbmc 	BUFDUMPPTR(bufdata, dtbda_aggdata);
88930ef842dSbmc 	BUFDUMPPTR(bufdata, dtbda_recdesc);
89030ef842dSbmc 
89130ef842dSbmc 	(void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
89230ef842dSbmc 	c += strlen(c);
89330ef842dSbmc 
89430ef842dSbmc 	for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
89530ef842dSbmc 		if (!(flags & flagnames[i].value))
89630ef842dSbmc 			continue;
89730ef842dSbmc 
89830ef842dSbmc 		(void) snprintf(c, end - c,
89930ef842dSbmc 		    "%s%s", printed++ ? " | " : "(", flagnames[i].name);
90030ef842dSbmc 		c += strlen(c);
90130ef842dSbmc 		flags &= ~flagnames[i].value;
90230ef842dSbmc 	}
90330ef842dSbmc 
90430ef842dSbmc 	if (printed)
90530ef842dSbmc 		(void) snprintf(c, end - c, ")");
90630ef842dSbmc 
90730ef842dSbmc 	BUFDUMPASSTR(bufdata, dtbda_flags, buf);
90830ef842dSbmc 	BUFDUMPHDR("");
90930ef842dSbmc 
91030ef842dSbmc 	if (pd != NULL) {
91130ef842dSbmc 		BUFDUMPHDR("  dtrace_probedesc");
91230ef842dSbmc 		BUFDUMPSTR(pd, dtpd_provider);
91330ef842dSbmc 		BUFDUMPSTR(pd, dtpd_mod);
91430ef842dSbmc 		BUFDUMPSTR(pd, dtpd_func);
91530ef842dSbmc 		BUFDUMPSTR(pd, dtpd_name);
91630ef842dSbmc 		BUFDUMPHDR("");
91730ef842dSbmc 	}
91830ef842dSbmc 
91930ef842dSbmc 	if (rec != NULL) {
92030ef842dSbmc 		BUFDUMPHDR("  dtrace_recdesc");
92130ef842dSbmc 		BUFDUMP(rec, dtrd_action);
92230ef842dSbmc 		BUFDUMP(rec, dtrd_size);
92302198c0bSbmc 
92402198c0bSbmc 		if (agg != NULL) {
92502198c0bSbmc 			uint8_t *data;
92602198c0bSbmc 			int lim = rec->dtrd_size;
92702198c0bSbmc 
92802198c0bSbmc 			(void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
92902198c0bSbmc 			c = buf + strlen(buf);
93002198c0bSbmc 
93102198c0bSbmc 			if (lim > sizeof (uint64_t))
93202198c0bSbmc 				lim = sizeof (uint64_t);
93302198c0bSbmc 
93402198c0bSbmc 			data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
93502198c0bSbmc 
93602198c0bSbmc 			for (i = 0; i < lim; i++) {
93702198c0bSbmc 				(void) snprintf(c, end - c, "%s%02x",
93802198c0bSbmc 				    i == 0 ? "" : " ", *data++);
93902198c0bSbmc 				c += strlen(c);
94002198c0bSbmc 			}
94102198c0bSbmc 
94202198c0bSbmc 			(void) snprintf(c, end - c,
94302198c0bSbmc 			    "%s)", lim < rec->dtrd_size ? " ..." : "");
94402198c0bSbmc 			BUFDUMPASSTR(rec, dtrd_offset, buf);
94502198c0bSbmc 		} else {
94602198c0bSbmc 			BUFDUMP(rec, dtrd_offset);
94702198c0bSbmc 		}
94802198c0bSbmc 
94930ef842dSbmc 		BUFDUMPHDR("");
95030ef842dSbmc 	}
95130ef842dSbmc 
95230ef842dSbmc 	if (agg != NULL) {
95330ef842dSbmc 		dtrace_aggdesc_t *desc = agg->dtada_desc;
95430ef842dSbmc 
95530ef842dSbmc 		BUFDUMPHDR("  dtrace_aggdesc");
95630ef842dSbmc 		BUFDUMPSTR(desc, dtagd_name);
95730ef842dSbmc 		BUFDUMP(desc, dtagd_varid);
95830ef842dSbmc 		BUFDUMP(desc, dtagd_id);
95930ef842dSbmc 		BUFDUMP(desc, dtagd_nrecs);
96030ef842dSbmc 		BUFDUMPHDR("");
96130ef842dSbmc 	}
96230ef842dSbmc 
96330ef842dSbmc 	return (DTRACE_HANDLE_OK);
96430ef842dSbmc }
96530ef842dSbmc 
9667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9677c478bd9Sstevel@tonic-gate static int
chewrec(const dtrace_probedata_t * data,const dtrace_recdesc_t * rec,void * arg)9687c478bd9Sstevel@tonic-gate chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	dtrace_actkind_t act;
9717c478bd9Sstevel@tonic-gate 	uintptr_t addr;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (rec == NULL) {
9747c478bd9Sstevel@tonic-gate 		/*
9757c478bd9Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
9767c478bd9Sstevel@tonic-gate 		 * we're not in quiet mode.
9777c478bd9Sstevel@tonic-gate 		 */
9787c478bd9Sstevel@tonic-gate 		if (!g_quiet)
9797c478bd9Sstevel@tonic-gate 			oprintf("\n");
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	act = rec->dtrd_action;
9857c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)data->dtpda_data;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (act == DTRACEACT_EXIT) {
9887c478bd9Sstevel@tonic-gate 		g_status = *((uint32_t *)addr);
9897c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9967c478bd9Sstevel@tonic-gate static int
chew(const dtrace_probedata_t * data,void * arg)9977c478bd9Sstevel@tonic-gate chew(const dtrace_probedata_t *data, void *arg)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
10007c478bd9Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
10017c478bd9Sstevel@tonic-gate 	static int heading;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	if (g_impatient) {
10047c478bd9Sstevel@tonic-gate 		g_newline = 0;
10057c478bd9Sstevel@tonic-gate 		return (DTRACE_CONSUME_ABORT);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (heading == 0) {
10097c478bd9Sstevel@tonic-gate 		if (!g_flowindent) {
10107c478bd9Sstevel@tonic-gate 			if (!g_quiet) {
10117c478bd9Sstevel@tonic-gate 				oprintf("%3s %6s %32s\n",
10127c478bd9Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
10137c478bd9Sstevel@tonic-gate 			}
10147c478bd9Sstevel@tonic-gate 		} else {
10157c478bd9Sstevel@tonic-gate 			oprintf("%3s %-41s\n", "CPU", "FUNCTION");
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		heading = 1;
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (!g_flowindent) {
10217c478bd9Sstevel@tonic-gate 		if (!g_quiet) {
10227c478bd9Sstevel@tonic-gate 			char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s:%s",
10257c478bd9Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 			oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 	} else {
10307c478bd9Sstevel@tonic-gate 		int indent = data->dtpda_indent;
10317c478bd9Sstevel@tonic-gate 		char *name;
10327c478bd9Sstevel@tonic-gate 		size_t len;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
10357c478bd9Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
10367c478bd9Sstevel@tonic-gate 			name = alloca(len);
10377c478bd9Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s:%s", indent, "",
10387c478bd9Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func,
10397c478bd9Sstevel@tonic-gate 			    pd->dtpd_name);
10407c478bd9Sstevel@tonic-gate 		} else {
10417c478bd9Sstevel@tonic-gate 			len = indent + DTRACE_FUNCNAMELEN + 5;
10427c478bd9Sstevel@tonic-gate 			name = alloca(len);
10437c478bd9Sstevel@tonic-gate 			(void) snprintf(name, len, "%*s%s%s", indent, "",
10447c478bd9Sstevel@tonic-gate 			    data->dtpda_prefix, pd->dtpd_func);
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		oprintf("%3d %-41s ", cpu, name);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate static void
go(void)10547c478bd9Sstevel@tonic-gate go(void)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	int i;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	struct {
10597c478bd9Sstevel@tonic-gate 		char *name;
10607c478bd9Sstevel@tonic-gate 		char *optname;
10617c478bd9Sstevel@tonic-gate 		dtrace_optval_t val;
10627c478bd9Sstevel@tonic-gate 	} bufs[] = {
10637c478bd9Sstevel@tonic-gate 		{ "buffer size", "bufsize" },
10647c478bd9Sstevel@tonic-gate 		{ "aggregation size", "aggsize" },
10657c478bd9Sstevel@tonic-gate 		{ "speculation size", "specsize" },
10667c478bd9Sstevel@tonic-gate 		{ "dynamic variable size", "dynvarsize" },
10677c478bd9Sstevel@tonic-gate 		{ NULL }
10687c478bd9Sstevel@tonic-gate 	}, rates[] = {
10697c478bd9Sstevel@tonic-gate 		{ "cleaning rate", "cleanrate" },
10707c478bd9Sstevel@tonic-gate 		{ "status rate", "statusrate" },
10717c478bd9Sstevel@tonic-gate 		{ NULL }
10727c478bd9Sstevel@tonic-gate 	};
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
10757c478bd9Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
10767c478bd9Sstevel@tonic-gate 			fatal("couldn't get option %s", bufs[i].optname);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
10807c478bd9Sstevel@tonic-gate 		if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
10817c478bd9Sstevel@tonic-gate 			fatal("couldn't get option %s", rates[i].optname);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (dtrace_go(g_dtp) == -1)
10857c478bd9Sstevel@tonic-gate 		dfatal("could not enable tracing");
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	for (i = 0; bufs[i].name != NULL; i++) {
10887c478bd9Sstevel@tonic-gate 		dtrace_optval_t j = 0, mul = 10;
10897c478bd9Sstevel@tonic-gate 		dtrace_optval_t nsize;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		if (bufs[i].val == DTRACEOPT_UNSET)
10927c478bd9Sstevel@tonic-gate 			continue;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		if (nsize == DTRACEOPT_UNSET || nsize == 0)
10977c478bd9Sstevel@tonic-gate 			continue;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		if (nsize >= bufs[i].val - sizeof (uint64_t))
11007c478bd9Sstevel@tonic-gate 			continue;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
11037c478bd9Sstevel@tonic-gate 			continue;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
11067c478bd9Sstevel@tonic-gate 			error("%s lowered to %lld%c\n", bufs[i].name,
11077c478bd9Sstevel@tonic-gate 			    (long long)nsize >> (mul - 10), " kmgtpe"[j]);
11087c478bd9Sstevel@tonic-gate 		} else {
11097c478bd9Sstevel@tonic-gate 			error("%s lowered to %lld bytes\n", bufs[i].name,
11107c478bd9Sstevel@tonic-gate 			    (long long)nsize);
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	for (i = 0; rates[i].name != NULL; i++) {
11157c478bd9Sstevel@tonic-gate 		dtrace_optval_t nval;
11167c478bd9Sstevel@tonic-gate 		char *dir;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		if (rates[i].val == DTRACEOPT_UNSET)
11197c478bd9Sstevel@tonic-gate 			continue;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		(void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 		if (nval == DTRACEOPT_UNSET || nval == 0)
11247c478bd9Sstevel@tonic-gate 			continue;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		if (rates[i].val == nval)
11277c478bd9Sstevel@tonic-gate 			continue;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 		dir = nval > rates[i].val ? "reduced" : "increased";
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
11327c478bd9Sstevel@tonic-gate 			error("%s %s to %lld hz\n", rates[i].name, dir,
11337c478bd9Sstevel@tonic-gate 			    (long long)NANOSEC / (long long)nval);
11347c478bd9Sstevel@tonic-gate 			continue;
11357c478bd9Sstevel@tonic-gate 		}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 		if ((nval % NANOSEC) == 0) {
11387c478bd9Sstevel@tonic-gate 			error("%s %s to once every %lld seconds\n",
11397c478bd9Sstevel@tonic-gate 			    rates[i].name, dir,
11407c478bd9Sstevel@tonic-gate 			    (long long)nval / (long long)NANOSEC);
11417c478bd9Sstevel@tonic-gate 			continue;
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 		error("%s %s to once every %lld nanoseconds\n",
11457c478bd9Sstevel@tonic-gate 		    rates[i].name, dir, (long long)nval);
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11507c478bd9Sstevel@tonic-gate static void
intr(int signo)11517c478bd9Sstevel@tonic-gate intr(int signo)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	if (!g_intr)
11547c478bd9Sstevel@tonic-gate 		g_newline = 1;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (g_intr++)
11577c478bd9Sstevel@tonic-gate 		g_impatient = 1;
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])11617c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	dtrace_bufdesc_t buf;
1164e4586ebfSmws 	struct sigaction act, oact;
11657c478bd9Sstevel@tonic-gate 	dtrace_status_t status[2];
11667c478bd9Sstevel@tonic-gate 	dtrace_optval_t opt;
11677c478bd9Sstevel@tonic-gate 	dtrace_cmd_t *dcp;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	int done = 0, mode = 0;
11707c478bd9Sstevel@tonic-gate 	int err, i;
1171*ef150c2bSRichard Lowe 	int c;
1172*ef150c2bSRichard Lowe 	char *p, **v;
11737c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
11747c478bd9Sstevel@tonic-gate 	pid_t pid;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	g_pname = basename(argv[0]);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (argc == 1)
11797c478bd9Sstevel@tonic-gate 		return (usage(stderr));
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
11827c478bd9Sstevel@tonic-gate 	    (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
11837c478bd9Sstevel@tonic-gate 	    (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
11847c478bd9Sstevel@tonic-gate 		fatal("failed to allocate memory for arguments");
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	g_argv[g_argc++] = argv[0];	/* propagate argv[0] to D as $0/$$0 */
11877c478bd9Sstevel@tonic-gate 	argv[0] = g_pname;		/* rewrite argv[0] for getopt errors */
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	bzero(status, sizeof (status));
11907c478bd9Sstevel@tonic-gate 	bzero(&buf, sizeof (buf));
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * Make an initial pass through argv[] processing any arguments that
11947c478bd9Sstevel@tonic-gate 	 * affect our behavior mode (g_mode) and flags used for dtrace_open().
11957c478bd9Sstevel@tonic-gate 	 * We also accumulate arguments that are not affiliated with getopt
11967c478bd9Sstevel@tonic-gate 	 * options into g_argv[], and abort if any invalid options are found.
11977c478bd9Sstevel@tonic-gate 	 */
11987c478bd9Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
11997c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
12007c478bd9Sstevel@tonic-gate 			switch (c) {
12017c478bd9Sstevel@tonic-gate 			case '3':
12027c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "2") != 0) {
12037c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12047c478bd9Sstevel@tonic-gate 					    "%s: illegal option -- 3%s\n",
12057c478bd9Sstevel@tonic-gate 					    argv[0], optarg);
12067c478bd9Sstevel@tonic-gate 					return (usage(stderr));
12077c478bd9Sstevel@tonic-gate 				}
12087c478bd9Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_LP64;
12097c478bd9Sstevel@tonic-gate 				g_oflags |= DTRACE_O_ILP32;
12107c478bd9Sstevel@tonic-gate 				break;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 			case '6':
12137c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "4") != 0) {
12147c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12157c478bd9Sstevel@tonic-gate 					    "%s: illegal option -- 6%s\n",
12167c478bd9Sstevel@tonic-gate 					    argv[0], optarg);
12177c478bd9Sstevel@tonic-gate 					return (usage(stderr));
12187c478bd9Sstevel@tonic-gate 				}
12197c478bd9Sstevel@tonic-gate 				g_oflags &= ~DTRACE_O_ILP32;
12207c478bd9Sstevel@tonic-gate 				g_oflags |= DTRACE_O_LP64;
12217c478bd9Sstevel@tonic-gate 				break;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 			case 'a':
12247c478bd9Sstevel@tonic-gate 				g_grabanon++; /* also checked in pass 2 below */
12257c478bd9Sstevel@tonic-gate 				break;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 			case 'A':
12287c478bd9Sstevel@tonic-gate 				g_mode = DMODE_ANON;
12297c478bd9Sstevel@tonic-gate 				g_exec = 0;
12307c478bd9Sstevel@tonic-gate 				mode++;
12317c478bd9Sstevel@tonic-gate 				break;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			case 'e':
12347c478bd9Sstevel@tonic-gate 				g_exec = 0;
12357c478bd9Sstevel@tonic-gate 				done = 1;
12367c478bd9Sstevel@tonic-gate 				break;
12377c478bd9Sstevel@tonic-gate 
1238900524f3Sahl 			case 'h':
1239900524f3Sahl 				g_mode = DMODE_HEADER;
1240900524f3Sahl 				g_oflags |= DTRACE_O_NODEV;
1241900524f3Sahl 				g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1242900524f3Sahl 				g_exec = 0;
1243900524f3Sahl 				mode++;
1244900524f3Sahl 				break;
1245900524f3Sahl 
12467c478bd9Sstevel@tonic-gate 			case 'G':
12477c478bd9Sstevel@tonic-gate 				g_mode = DMODE_LINK;
12487c478bd9Sstevel@tonic-gate 				g_oflags |= DTRACE_O_NODEV;
12497c478bd9Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
12507c478bd9Sstevel@tonic-gate 				g_exec = 0;
12517c478bd9Sstevel@tonic-gate 				mode++;
12527c478bd9Sstevel@tonic-gate 				break;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 			case 'l':
12557c478bd9Sstevel@tonic-gate 				g_mode = DMODE_LIST;
12567c478bd9Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
12577c478bd9Sstevel@tonic-gate 				mode++;
12587c478bd9Sstevel@tonic-gate 				break;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 			case 'V':
12617c478bd9Sstevel@tonic-gate 				g_mode = DMODE_VERS;
12627c478bd9Sstevel@tonic-gate 				mode++;
12637c478bd9Sstevel@tonic-gate 				break;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 			default:
12667c478bd9Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
12677c478bd9Sstevel@tonic-gate 					return (usage(stderr));
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		if (optind < argc)
12727c478bd9Sstevel@tonic-gate 			g_argv[g_argc++] = argv[optind];
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if (mode > 1) {
1276900524f3Sahl 		(void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
12777c478bd9Sstevel@tonic-gate 		    "can be specified at a time\n", g_pname);
12787c478bd9Sstevel@tonic-gate 		return (E_USAGE);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (g_mode == DMODE_VERS)
12827c478bd9Sstevel@tonic-gate 		return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
12837c478bd9Sstevel@tonic-gate 
12842b6e762cSahl 	/*
12852b6e762cSahl 	 * If we're in linker mode and the data model hasn't been specified,
12862b6e762cSahl 	 * we try to guess the appropriate setting by examining the object
12872b6e762cSahl 	 * files. We ignore certain errors since we'll catch them later when
12882b6e762cSahl 	 * we actually process the object files.
12892b6e762cSahl 	 */
12902b6e762cSahl 	if (g_mode == DMODE_LINK &&
12912b6e762cSahl 	    (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
12922b6e762cSahl 	    elf_version(EV_CURRENT) != EV_NONE) {
12932b6e762cSahl 		int fd;
12942b6e762cSahl 		Elf *elf;
12952b6e762cSahl 		GElf_Ehdr ehdr;
12962b6e762cSahl 
12972b6e762cSahl 		for (i = 1; i < g_argc; i++) {
12982b6e762cSahl 			if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
12992b6e762cSahl 				break;
13002b6e762cSahl 
13012b6e762cSahl 			if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
13022b6e762cSahl 				(void) close(fd);
13032b6e762cSahl 				break;
13042b6e762cSahl 			}
13052b6e762cSahl 
13062b6e762cSahl 			if (elf_kind(elf) != ELF_K_ELF ||
13072b6e762cSahl 			    gelf_getehdr(elf, &ehdr) == NULL) {
13082b6e762cSahl 				(void) close(fd);
13092b6e762cSahl 				(void) elf_end(elf);
13102b6e762cSahl 				break;
13112b6e762cSahl 			}
13122b6e762cSahl 
13132b6e762cSahl 			(void) close(fd);
13142b6e762cSahl 			(void) elf_end(elf);
13152b6e762cSahl 
13162b6e762cSahl 			if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
13172b6e762cSahl 				if (g_oflags & DTRACE_O_ILP32) {
13182b6e762cSahl 					fatal("can't mix 32-bit and 64-bit "
13192b6e762cSahl 					    "object files\n");
13202b6e762cSahl 				}
13212b6e762cSahl 				g_oflags |= DTRACE_O_LP64;
13222b6e762cSahl 			} else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
13232b6e762cSahl 				if (g_oflags & DTRACE_O_LP64) {
13242b6e762cSahl 					fatal("can't mix 32-bit and 64-bit "
13252b6e762cSahl 					    "object files\n");
13262b6e762cSahl 				}
13272b6e762cSahl 				g_oflags |= DTRACE_O_ILP32;
13282b6e762cSahl 			} else {
13292b6e762cSahl 				break;
13302b6e762cSahl 			}
13312b6e762cSahl 		}
13322b6e762cSahl 	}
13332b6e762cSahl 
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * Open libdtrace.  If we are not actually going to be enabling any
13367c478bd9Sstevel@tonic-gate 	 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
13377c478bd9Sstevel@tonic-gate 	 */
13387c478bd9Sstevel@tonic-gate 	while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
13397c478bd9Sstevel@tonic-gate 		if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
13407c478bd9Sstevel@tonic-gate 			g_oflags |= DTRACE_O_NODEV;
13417c478bd9Sstevel@tonic-gate 			continue;
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		fatal("failed to initialize dtrace: %s\n",
13457c478bd9Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "bufsize", "4m");
13497c478bd9Sstevel@tonic-gate 	(void) dtrace_setopt(g_dtp, "aggsize", "4m");
1350e5803b76SAdam H. Leventhal 	(void) dtrace_setopt(g_dtp, "temporal", "yes");
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
13547c478bd9Sstevel@tonic-gate 	 * references to undefined symbols to remain as unresolved relocations.
13557c478bd9Sstevel@tonic-gate 	 * If -A is specified, enable -xlink=primary to permit static linking
13567c478bd9Sstevel@tonic-gate 	 * only to kernel symbols that are defined in a primary kernel module.
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 	if (g_mode == DMODE_LINK) {
13597c478bd9Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
13607c478bd9Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "unodefs", NULL);
13617c478bd9Sstevel@tonic-gate 
13622b6e762cSahl 		/*
13632b6e762cSahl 		 * Use the remaining arguments as the list of object files
13642b6e762cSahl 		 * when in linker mode.
13652b6e762cSahl 		 */
13662b6e762cSahl 		g_objc = g_argc - 1;
13672b6e762cSahl 		g_objv = g_argv + 1;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		/*
13707c478bd9Sstevel@tonic-gate 		 * We still use g_argv[0], the name of the executable.
13717c478bd9Sstevel@tonic-gate 		 */
13727c478bd9Sstevel@tonic-gate 		g_argc = 1;
13737c478bd9Sstevel@tonic-gate 	} else if (g_mode == DMODE_ANON)
13747c478bd9Sstevel@tonic-gate 		(void) dtrace_setopt(g_dtp, "linkmode", "primary");
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/*
13777c478bd9Sstevel@tonic-gate 	 * Now that we have libdtrace open, make a second pass through argv[]
13787c478bd9Sstevel@tonic-gate 	 * to perform any dtrace_setopt() calls and change any compiler flags.
13797c478bd9Sstevel@tonic-gate 	 * We also accumulate any program specifications into our g_cmdv[] at
13807c478bd9Sstevel@tonic-gate 	 * this time; these will compiled as part of the fourth processing pass.
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
13837c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
13847c478bd9Sstevel@tonic-gate 			switch (c) {
13857c478bd9Sstevel@tonic-gate 			case 'a':
13867c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
13877c478bd9Sstevel@tonic-gate 					dfatal("failed to set -a");
13887c478bd9Sstevel@tonic-gate 				break;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 			case 'b':
13917c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp,
13927c478bd9Sstevel@tonic-gate 				    "bufsize", optarg) != 0)
13937c478bd9Sstevel@tonic-gate 					dfatal("failed to set -b %s", optarg);
13947c478bd9Sstevel@tonic-gate 				break;
13957c478bd9Sstevel@tonic-gate 
139630ef842dSbmc 			case 'B':
139730ef842dSbmc 				g_ofp = NULL;
139830ef842dSbmc 				break;
139930ef842dSbmc 
14007c478bd9Sstevel@tonic-gate 			case 'C':
14017c478bd9Sstevel@tonic-gate 				g_cflags |= DTRACE_C_CPP;
14027c478bd9Sstevel@tonic-gate 				break;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 			case 'D':
14057c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "define", optarg) != 0)
14067c478bd9Sstevel@tonic-gate 					dfatal("failed to set -D %s", optarg);
14077c478bd9Sstevel@tonic-gate 				break;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 			case 'f':
14107c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14117c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14127c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
14137c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14147c478bd9Sstevel@tonic-gate 				break;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 			case 'F':
14177c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
14187c478bd9Sstevel@tonic-gate 					dfatal("failed to set -F");
14197c478bd9Sstevel@tonic-gate 				break;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 			case 'H':
14227c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
14237c478bd9Sstevel@tonic-gate 					dfatal("failed to set -H");
14247c478bd9Sstevel@tonic-gate 				break;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 			case 'i':
14277c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14287c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14297c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
14307c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14317c478bd9Sstevel@tonic-gate 				break;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 			case 'I':
14347c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
14357c478bd9Sstevel@tonic-gate 					dfatal("failed to set -I %s", optarg);
14367c478bd9Sstevel@tonic-gate 				break;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 			case 'L':
14397c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
14407c478bd9Sstevel@tonic-gate 					dfatal("failed to set -L %s", optarg);
14417c478bd9Sstevel@tonic-gate 				break;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 			case 'm':
14447c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14457c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14467c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_MOD;
14477c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14487c478bd9Sstevel@tonic-gate 				break;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 			case 'n':
14517c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14527c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14537c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NAME;
14547c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14557c478bd9Sstevel@tonic-gate 				break;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 			case 'P':
14587c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14597c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_str;
14607c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
14617c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14627c478bd9Sstevel@tonic-gate 				break;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 			case 'q':
14657c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
14667c478bd9Sstevel@tonic-gate 					dfatal("failed to set -q");
14677c478bd9Sstevel@tonic-gate 				break;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 			case 'o':
14707c478bd9Sstevel@tonic-gate 				g_ofile = optarg;
14717c478bd9Sstevel@tonic-gate 				break;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 			case 's':
14747c478bd9Sstevel@tonic-gate 				dcp = &g_cmdv[g_cmdc++];
14757c478bd9Sstevel@tonic-gate 				dcp->dc_func = compile_file;
14767c478bd9Sstevel@tonic-gate 				dcp->dc_spec = DTRACE_PROBESPEC_NONE;
14777c478bd9Sstevel@tonic-gate 				dcp->dc_arg = optarg;
14787c478bd9Sstevel@tonic-gate 				break;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			case 'S':
14817c478bd9Sstevel@tonic-gate 				g_cflags |= DTRACE_C_DIFV;
14827c478bd9Sstevel@tonic-gate 				break;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 			case 'U':
14857c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
14867c478bd9Sstevel@tonic-gate 					dfatal("failed to set -U %s", optarg);
14877c478bd9Sstevel@tonic-gate 				break;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 			case 'v':
14907c478bd9Sstevel@tonic-gate 				g_verbose++;
14917c478bd9Sstevel@tonic-gate 				break;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 			case 'w':
14947c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
14957c478bd9Sstevel@tonic-gate 					dfatal("failed to set -w");
14967c478bd9Sstevel@tonic-gate 				break;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 			case 'x':
14997c478bd9Sstevel@tonic-gate 				if ((p = strchr(optarg, '=')) != NULL)
15007c478bd9Sstevel@tonic-gate 					*p++ = '\0';
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, optarg, p) != 0)
15037c478bd9Sstevel@tonic-gate 					dfatal("failed to set -x %s", optarg);
15047c478bd9Sstevel@tonic-gate 				break;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 			case 'X':
15077c478bd9Sstevel@tonic-gate 				if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
15087c478bd9Sstevel@tonic-gate 					dfatal("failed to set -X %s", optarg);
15097c478bd9Sstevel@tonic-gate 				break;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 			case 'Z':
15127c478bd9Sstevel@tonic-gate 				g_cflags |= DTRACE_C_ZDEFS;
15137c478bd9Sstevel@tonic-gate 				break;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 			default:
15167c478bd9Sstevel@tonic-gate 				if (strchr(DTRACE_OPTSTR, c) == NULL)
15177c478bd9Sstevel@tonic-gate 					return (usage(stderr));
15187c478bd9Sstevel@tonic-gate 			}
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 	}
15217c478bd9Sstevel@tonic-gate 
152230ef842dSbmc 	if (g_ofp == NULL && g_mode != DMODE_EXEC) {
152330ef842dSbmc 		(void) fprintf(stderr, "%s: -B not valid in combination"
152430ef842dSbmc 		    " with [-AGl] options\n", g_pname);
152530ef842dSbmc 		return (E_USAGE);
152630ef842dSbmc 	}
152730ef842dSbmc 
152830ef842dSbmc 	if (g_ofp == NULL && g_ofile != NULL) {
152930ef842dSbmc 		(void) fprintf(stderr, "%s: -B not valid in combination"
153030ef842dSbmc 		    " with -o option\n", g_pname);
153130ef842dSbmc 		return (E_USAGE);
153230ef842dSbmc 	}
153330ef842dSbmc 
15347c478bd9Sstevel@tonic-gate 	/*
15357c478bd9Sstevel@tonic-gate 	 * In our third pass we handle any command-line options related to
15367c478bd9Sstevel@tonic-gate 	 * grabbing or creating victim processes.  The behavior of these calls
15377c478bd9Sstevel@tonic-gate 	 * may been affected by any library options set by the second pass.
15387c478bd9Sstevel@tonic-gate 	 */
15397c478bd9Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
15407c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
15417c478bd9Sstevel@tonic-gate 			switch (c) {
15427c478bd9Sstevel@tonic-gate 			case 'c':
15437c478bd9Sstevel@tonic-gate 				if ((v = make_argv(optarg)) == NULL)
15447c478bd9Sstevel@tonic-gate 					fatal("failed to allocate memory");
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 				P = dtrace_proc_create(g_dtp, v[0], v);
15477c478bd9Sstevel@tonic-gate 				if (P == NULL)
15487c478bd9Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 				g_psv[g_psc++] = P;
15517c478bd9Sstevel@tonic-gate 				free(v);
15527c478bd9Sstevel@tonic-gate 				break;
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 			case 'p':
15557c478bd9Sstevel@tonic-gate 				errno = 0;
15567c478bd9Sstevel@tonic-gate 				pid = strtol(optarg, &p, 10);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 				if (errno != 0 || p == optarg || p[0] != '\0')
15597c478bd9Sstevel@tonic-gate 					fatal("invalid pid: %s\n", optarg);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 				P = dtrace_proc_grab(g_dtp, pid, 0);
15627c478bd9Sstevel@tonic-gate 				if (P == NULL)
15637c478bd9Sstevel@tonic-gate 					dfatal(NULL); /* dtrace_errmsg() only */
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 				g_psv[g_psc++] = P;
15667c478bd9Sstevel@tonic-gate 				break;
15677c478bd9Sstevel@tonic-gate 			}
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	/*
15727c478bd9Sstevel@tonic-gate 	 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
15737c478bd9Sstevel@tonic-gate 	 * each string or file specification into a compiled program structure.
15747c478bd9Sstevel@tonic-gate 	 */
15757c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_cmdc; i++)
15767c478bd9Sstevel@tonic-gate 		g_cmdv[i].dc_func(&g_cmdv[i]);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	if (g_mode != DMODE_LIST) {
15797c478bd9Sstevel@tonic-gate 		if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
15807c478bd9Sstevel@tonic-gate 			dfatal("failed to establish error handler");
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
15837c478bd9Sstevel@tonic-gate 			dfatal("failed to establish drop handler");
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 		if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
15867c478bd9Sstevel@tonic-gate 			dfatal("failed to establish proc handler");
1587a1b5e537Sbmc 
1588a1b5e537Sbmc 		if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1589a1b5e537Sbmc 			dfatal("failed to establish setopt handler");
159030ef842dSbmc 
159130ef842dSbmc 		if (g_ofp == NULL &&
159230ef842dSbmc 		    dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
159330ef842dSbmc 			dfatal("failed to establish buffered handler");
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
15977c478bd9Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
16007c478bd9Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
16037c478bd9Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	/*
16067c478bd9Sstevel@tonic-gate 	 * Now make a fifth and final pass over the options that have been
16077c478bd9Sstevel@tonic-gate 	 * turned into programs and saved in g_cmdv[], performing any mode-
16087c478bd9Sstevel@tonic-gate 	 * specific processing.  If g_mode is DMODE_EXEC, we will break out
16097c478bd9Sstevel@tonic-gate 	 * of the switch() and continue on to the data processing loop.  For
16107c478bd9Sstevel@tonic-gate 	 * other modes, we will exit dtrace once mode-specific work is done.
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	switch (g_mode) {
16137c478bd9Sstevel@tonic-gate 	case DMODE_EXEC:
16147c478bd9Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
16157c478bd9Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
16187c478bd9Sstevel@tonic-gate 			exec_prog(&g_cmdv[i]);
16197c478bd9Sstevel@tonic-gate 
16201a7c1b72Smws 		if (done && !g_grabanon) {
16211a7c1b72Smws 			dtrace_close(g_dtp);
16227c478bd9Sstevel@tonic-gate 			return (g_status);
16231a7c1b72Smws 		}
16247c478bd9Sstevel@tonic-gate 		break;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	case DMODE_ANON:
16277c478bd9Sstevel@tonic-gate 		if (g_ofile == NULL)
16287c478bd9Sstevel@tonic-gate 			g_ofile = "/kernel/drv/dtrace.conf";
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 		dof_prune(g_ofile); /* strip out any old DOF directives */
16317c478bd9Sstevel@tonic-gate 		etcsystem_prune(); /* string out any forceload directives */
16327c478bd9Sstevel@tonic-gate 
16331a7c1b72Smws 		if (g_cmdc == 0) {
16341a7c1b72Smws 			dtrace_close(g_dtp);
16357c478bd9Sstevel@tonic-gate 			return (g_status);
16361a7c1b72Smws 		}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 		if ((g_ofp = fopen(g_ofile, "a")) == NULL)
16397c478bd9Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++) {
16427c478bd9Sstevel@tonic-gate 			anon_prog(&g_cmdv[i],
16437c478bd9Sstevel@tonic-gate 			    dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 		/*
16477c478bd9Sstevel@tonic-gate 		 * Dump out the DOF corresponding to the error handler and the
16487c478bd9Sstevel@tonic-gate 		 * current options as the final DOF property in the .conf file.
16497c478bd9Sstevel@tonic-gate 		 */
16507c478bd9Sstevel@tonic-gate 		anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
16517c478bd9Sstevel@tonic-gate 		anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		if (fclose(g_ofp) == EOF)
16547c478bd9Sstevel@tonic-gate 			fatal("failed to close output file '%s'", g_ofile);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		/*
16577c478bd9Sstevel@tonic-gate 		 * These messages would use notice() rather than error(), but
16587c478bd9Sstevel@tonic-gate 		 * we don't want them suppressed when -A is run on a D program
16597c478bd9Sstevel@tonic-gate 		 * that itself contains a #pragma D option quiet.
16607c478bd9Sstevel@tonic-gate 		 */
16617c478bd9Sstevel@tonic-gate 		error("saved anonymous enabling in %s\n", g_ofile);
16627c478bd9Sstevel@tonic-gate 		etcsystem_add();
1663bbf21555SRichard Lowe 		error("run update_drv(8) or reboot to enable changes\n");
16647c478bd9Sstevel@tonic-gate 
16651a7c1b72Smws 		dtrace_close(g_dtp);
16667c478bd9Sstevel@tonic-gate 		return (g_status);
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	case DMODE_LINK:
1669e4586ebfSmws 		if (g_cmdc == 0) {
1670e4586ebfSmws 			(void) fprintf(stderr, "%s: -G requires one or more "
1671e4586ebfSmws 			    "scripts or enabling options\n", g_pname);
1672e4586ebfSmws 			dtrace_close(g_dtp);
1673e4586ebfSmws 			return (E_USAGE);
1674e4586ebfSmws 		}
1675e4586ebfSmws 
16767c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
16777c478bd9Sstevel@tonic-gate 			link_prog(&g_cmdv[i]);
16780b38a8bdSahl 
16790b38a8bdSahl 		if (g_cmdc > 1 && g_ofile != NULL) {
16800b38a8bdSahl 			char **objv = alloca(g_cmdc * sizeof (char *));
16810b38a8bdSahl 
16820b38a8bdSahl 			for (i = 0; i < g_cmdc; i++)
16830b38a8bdSahl 				objv[i] = g_cmdv[i].dc_ofile;
16840b38a8bdSahl 
16850b38a8bdSahl 			if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
16860b38a8bdSahl 			    g_ofile, g_cmdc, objv) != 0)
16871a7c1b72Smws 				dfatal(NULL); /* dtrace_errmsg() only */
16880b38a8bdSahl 		}
16890b38a8bdSahl 
16901a7c1b72Smws 		dtrace_close(g_dtp);
16917c478bd9Sstevel@tonic-gate 		return (g_status);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	case DMODE_LIST:
16947c478bd9Sstevel@tonic-gate 		if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
16957c478bd9Sstevel@tonic-gate 			fatal("failed to open output file '%s'", g_ofile);
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 		oprintf("%5s %10s %17s %33s %s\n",
16987c478bd9Sstevel@tonic-gate 		    "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		for (i = 0; i < g_cmdc; i++)
17017c478bd9Sstevel@tonic-gate 			list_prog(&g_cmdv[i]);
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 		if (g_cmdc == 0)
17047c478bd9Sstevel@tonic-gate 			(void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
17057c478bd9Sstevel@tonic-gate 
17061a7c1b72Smws 		dtrace_close(g_dtp);
17077c478bd9Sstevel@tonic-gate 		return (g_status);
1708900524f3Sahl 
1709900524f3Sahl 	case DMODE_HEADER:
1710900524f3Sahl 		if (g_cmdc == 0) {
1711900524f3Sahl 			(void) fprintf(stderr, "%s: -h requires one or more "
1712900524f3Sahl 			    "scripts or enabling options\n", g_pname);
1713900524f3Sahl 			dtrace_close(g_dtp);
1714900524f3Sahl 			return (E_USAGE);
1715900524f3Sahl 		}
1716900524f3Sahl 
1717900524f3Sahl 		if (g_ofile == NULL) {
1718900524f3Sahl 			char *p;
1719900524f3Sahl 
1720900524f3Sahl 			if (g_cmdc > 1) {
1721900524f3Sahl 				(void) fprintf(stderr, "%s: -h requires an "
1722900524f3Sahl 				    "output file if multiple scripts are "
1723900524f3Sahl 				    "specified\n", g_pname);
1724900524f3Sahl 				dtrace_close(g_dtp);
1725900524f3Sahl 				return (E_USAGE);
1726900524f3Sahl 			}
1727900524f3Sahl 
1728900524f3Sahl 			if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1729900524f3Sahl 			    strcmp(p, ".d") != 0) {
1730900524f3Sahl 				(void) fprintf(stderr, "%s: -h requires an "
1731900524f3Sahl 				    "output file if no scripts are "
1732900524f3Sahl 				    "specified\n", g_pname);
1733900524f3Sahl 				dtrace_close(g_dtp);
1734900524f3Sahl 				return (E_USAGE);
1735900524f3Sahl 			}
1736900524f3Sahl 
1737900524f3Sahl 			p[0] = '\0'; /* strip .d suffix */
1738900524f3Sahl 			g_ofile = p = g_cmdv[0].dc_ofile;
1739900524f3Sahl 			(void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1740900524f3Sahl 			    "%s.h", basename(g_cmdv[0].dc_arg));
1741900524f3Sahl 		}
1742900524f3Sahl 
1743900524f3Sahl 		if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1744900524f3Sahl 			fatal("failed to open header file '%s'", g_ofile);
1745900524f3Sahl 
1746bbf21555SRichard Lowe 		oprintf("/*\n * Generated by dtrace(8).\n */\n\n");
1747900524f3Sahl 
1748900524f3Sahl 		if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1749900524f3Sahl 		    fclose(g_ofp) == EOF)
1750900524f3Sahl 			dfatal("failed to create header file %s", g_ofile);
1751900524f3Sahl 
1752900524f3Sahl 		dtrace_close(g_dtp);
1753900524f3Sahl 		return (g_status);
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/*
17577c478bd9Sstevel@tonic-gate 	 * If -a and -Z were not specified and no probes have been matched, no
17587c478bd9Sstevel@tonic-gate 	 * probe criteria was specified on the command line and we abort.
17597c478bd9Sstevel@tonic-gate 	 */
17607c478bd9Sstevel@tonic-gate 	if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
17617c478bd9Sstevel@tonic-gate 		dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/*
17647c478bd9Sstevel@tonic-gate 	 * Start tracing.  Once we dtrace_go(), reload any options that affect
17657c478bd9Sstevel@tonic-gate 	 * our globals in case consuming anonymous state has changed them.
17667c478bd9Sstevel@tonic-gate 	 */
17677c478bd9Sstevel@tonic-gate 	go();
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "flowindent", &opt);
17707c478bd9Sstevel@tonic-gate 	g_flowindent = opt != DTRACEOPT_UNSET;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "grabanon", &opt);
17737c478bd9Sstevel@tonic-gate 	g_grabanon = opt != DTRACEOPT_UNSET;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "quiet", &opt);
17767c478bd9Sstevel@tonic-gate 	g_quiet = opt != DTRACEOPT_UNSET;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	(void) dtrace_getopt(g_dtp, "destructive", &opt);
17797c478bd9Sstevel@tonic-gate 	if (opt != DTRACEOPT_UNSET)
17807c478bd9Sstevel@tonic-gate 		notice("allowing destructive actions\n");
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
17837c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
17847c478bd9Sstevel@tonic-gate 	act.sa_handler = intr;
1785e4586ebfSmws 
1786e4586ebfSmws 	if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1787e4586ebfSmws 		(void) sigaction(SIGINT, &act, NULL);
1788e4586ebfSmws 
1789e4586ebfSmws 	if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1790e4586ebfSmws 		(void) sigaction(SIGTERM, &act, NULL);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * Now that tracing is active and we are ready to consume trace data,
17947c478bd9Sstevel@tonic-gate 	 * continue any grabbed or created processes, setting them running
17957c478bd9Sstevel@tonic-gate 	 * using the /proc control mechanism inside of libdtrace.
17967c478bd9Sstevel@tonic-gate 	 */
17977c478bd9Sstevel@tonic-gate 	for (i = 0; i < g_psc; i++)
17987c478bd9Sstevel@tonic-gate 		dtrace_proc_continue(g_dtp, g_psv[i]);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	g_pslive = g_psc; /* count for prochandler() */
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	do {
18037c478bd9Sstevel@tonic-gate 		if (!g_intr && !done)
18047c478bd9Sstevel@tonic-gate 			dtrace_sleep(g_dtp);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 		if (g_newline) {
18077c478bd9Sstevel@tonic-gate 			/*
18087c478bd9Sstevel@tonic-gate 			 * Output a newline just to make the output look
18097c478bd9Sstevel@tonic-gate 			 * slightly cleaner.  Note that we do this even in
18107c478bd9Sstevel@tonic-gate 			 * "quiet" mode...
18117c478bd9Sstevel@tonic-gate 			 */
18127c478bd9Sstevel@tonic-gate 			oprintf("\n");
18137c478bd9Sstevel@tonic-gate 			g_newline = 0;
18147c478bd9Sstevel@tonic-gate 		}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 		if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
18177c478bd9Sstevel@tonic-gate 			done = 1;
18187c478bd9Sstevel@tonic-gate 			if (dtrace_stop(g_dtp) == -1)
18197c478bd9Sstevel@tonic-gate 				dfatal("couldn't stop tracing");
18207c478bd9Sstevel@tonic-gate 		}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 		switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
18237c478bd9Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_DONE:
18247c478bd9Sstevel@tonic-gate 			done = 1;
18257c478bd9Sstevel@tonic-gate 			break;
18267c478bd9Sstevel@tonic-gate 		case DTRACE_WORKSTATUS_OKAY:
18277c478bd9Sstevel@tonic-gate 			break;
18287c478bd9Sstevel@tonic-gate 		default:
18297c478bd9Sstevel@tonic-gate 			if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
18307c478bd9Sstevel@tonic-gate 				dfatal("processing aborted");
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 
183330ef842dSbmc 		if (g_ofp != NULL && fflush(g_ofp) == EOF)
18347c478bd9Sstevel@tonic-gate 			clearerr(g_ofp);
18357c478bd9Sstevel@tonic-gate 	} while (!done);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	oprintf("\n");
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if (!g_impatient) {
18407c478bd9Sstevel@tonic-gate 		if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
18417c478bd9Sstevel@tonic-gate 		    dtrace_errno(g_dtp) != EINTR)
18427c478bd9Sstevel@tonic-gate 			dfatal("failed to print aggregations");
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	dtrace_close(g_dtp);
18467c478bd9Sstevel@tonic-gate 	return (g_status);
18477c478bd9Sstevel@tonic-gate }
1848