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