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.
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.
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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
31 #include <sys/types.h>
57 typedef struct dtrace_cmd {
58 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
59 dtrace_probespec_t dc_spec; /* probe specifier context */
60 char *dc_arg; /* argument from main argv */
61 const char *dc_name; /* name for error messages */
62 const char *dc_desc; /* desc for error messages */
63 dtrace_prog_t *dc_prog; /* program compiled from arg */
64 char dc_ofile[PATH_MAX]; /* derived output file name */
67 #define DMODE_VERS 0 /* display version information and exit (-V) */
68 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
69 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
70 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
71 #define DMODE_LIST 4 /* compile program and list probes (-l) */
72 #define DMODE_HEADER 5 /* compile program for headergen (-h) */
78 static const char DTRACE_OPTSTR[] =
79 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
85 static dtrace_cmd_t *g_cmdv;
87 static struct ps_prochandle **g_psv;
92 static int g_flowindent;
94 static int g_impatient;
102 static int g_verbose;
103 static int g_exec = 1;
104 static int g_mode = DMODE_EXEC;
105 static int g_status = E_SUCCESS;
106 static int g_grabanon = 0;
107 static const char *g_ofile = NULL;
109 static dtrace_hdl_t *g_dtp;
111 static char *g_etcfile = "/etc/system";
112 static const char *g_etcbegin = "* vvvv Added by DTrace";
113 static const char *g_etcend = "* ^^^^ Added by DTrace";
115 static const char *g_etc[] = {
117 "* The following forceload directives were added by dtrace(1M) to allow for",
118 "* tracing during boot. If these directives are removed, the system will",
119 "* continue to function, but tracing will not occur during boot as desired.",
120 "* To remove these directives (and this block comment) automatically, run",
121 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
122 "* chapter of the Solaris Dynamic Tracing Guide for details.",
130 static const char predact[] = "[[ predicate ] action ]";
132 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
133 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
134 "[-o output] [-p pid] [-s script] [-U name]\n\t"
135 "[-x opt[=val]] [-X a|c|s|t]\n\n"
136 "\t[-P provider %s]\n"
137 "\t[-m [ provider: ] module %s]\n"
138 "\t[-f [[ provider: ] module: ] func %s]\n"
139 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
140 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
141 predact, predact, predact, predact, predact);
143 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
144 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
146 (void) fprintf(fp, "\n"
147 "\t-32 generate 32-bit D programs and ELF files\n"
148 "\t-64 generate 64-bit D programs and ELF files\n\n"
149 "\t-a claim anonymous tracing state\n"
150 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
151 "\t-b set trace buffer size\n"
152 "\t-c run specified command and exit upon its completion\n"
153 "\t-C run cpp(1) preprocessor on script files\n"
154 "\t-D define symbol when invoking preprocessor\n"
155 "\t-e exit after compiling request but prior to enabling probes\n"
156 "\t-f enable or list probes matching the specified function name\n"
157 "\t-F coalesce trace output by function\n"
158 "\t-G generate an ELF file containing embedded dtrace program\n"
159 "\t-h generate a header file with definitions for static probes\n"
160 "\t-H print included files when invoking preprocessor\n"
161 "\t-i enable or list probes matching the specified probe id\n"
162 "\t-I add include directory to preprocessor search path\n"
163 "\t-l list probes matching specified criteria\n"
164 "\t-L add library directory to library search path\n"
165 "\t-m enable or list probes matching the specified module name\n"
166 "\t-n enable or list probes matching the specified probe name\n"
167 "\t-o set output file\n"
168 "\t-p grab specified process-ID and cache its symbol tables\n"
169 "\t-P enable or list probes matching the specified provider name\n"
170 "\t-q set quiet mode (only output explicitly traced data)\n"
171 "\t-s enable or list probes according to the specified D script\n"
172 "\t-S print D compiler intermediate code\n"
173 "\t-U undefine symbol when invoking preprocessor\n"
174 "\t-v set verbose mode (report stability attributes, arguments)\n"
175 "\t-V report DTrace API version\n"
176 "\t-w permit destructive actions\n"
177 "\t-x enable or modify compiler and tracing options\n"
178 "\t-X specify ISO C conformance settings for preprocessor\n"
179 "\t-Z permit probe descriptions that match zero probes\n");
185 verror(const char *fmt, va_list ap)
189 (void) fprintf(stderr, "%s: ", g_pname);
190 (void) vfprintf(stderr, fmt, ap);
192 if (fmt[strlen(fmt) - 1] != '\n')
193 (void) fprintf(stderr, ": %s\n", strerror(error));
198 fatal(const char *fmt, ...)
207 * Close the DTrace handle to ensure that any controlled processes are
208 * correctly restored and continued.
218 dfatal(const char *fmt, ...)
220 #if !defined(illumos) && defined(NEED_ERRLOC)
221 char *p_errfile = NULL;
228 (void) fprintf(stderr, "%s: ", g_pname);
230 (void) vfprintf(stderr, fmt, ap);
234 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
235 (void) fprintf(stderr, ": %s\n",
236 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
237 } else if (fmt == NULL) {
238 (void) fprintf(stderr, "%s\n",
239 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
241 #if !defined(illumos) && defined(NEED_ERRLOC)
242 dt_get_errloc(g_dtp, &p_errfile, &errline);
243 if (p_errfile != NULL)
244 printf("File '%s', line %d\n", p_errfile, errline);
248 * Close the DTrace handle to ensure that any controlled processes are
249 * correctly restored and continued.
258 error(const char *fmt, ...)
269 notice(const char *fmt, ...)
274 return; /* -q or quiet pragma suppresses notice()s */
283 oprintf(const char *fmt, ...)
292 n = vfprintf(g_ofp, fmt, ap);
296 if (errno != EINTR) {
297 fatal("failed to write to %s",
298 g_ofile ? g_ofile : "<stdout>");
307 const char *ws = "\f\n\r\t\v ";
308 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
315 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
326 dof_prune(const char *fname)
329 size_t sz, i, j, mark, len;
333 if ((fd = open(fname, O_RDONLY)) == -1) {
335 * This is okay only if the file doesn't exist at all.
338 fatal("failed to open %s", fname);
342 if (fstat(fd, &sbuf) == -1)
343 fatal("failed to fstat %s", fname);
345 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
346 fatal("failed to allocate memory for %s", fname);
348 if (read(fd, buf, sz) != sz)
349 fatal("failed to read %s", fname);
354 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
355 fatal("failed to open %s for writing", fname);
357 len = strlen("dof-data-");
359 for (mark = 0, i = 0; i < sz; i++) {
360 if (strncmp(&buf[i], "dof-data-", len) != 0)
364 * This is only a match if it's in the 0th column.
366 if (i != 0 && buf[i - 1] != '\n')
370 error("cleaned up old anonymous "
371 "enabling in %s\n", fname);
375 * We have a match. First write out our data up until now.
378 if (write(fd, &buf[mark], i - mark) != i - mark)
379 fatal("failed to write to %s", fname);
383 * Now scan forward until we scan past a newline.
385 for (j = i; j < sz && buf[j] != '\n'; j++)
391 if ((mark = j + 1) >= sz)
398 if (write(fd, &buf[mark], sz - mark) != sz - mark)
399 fatal("failed to write to %s", fname);
408 * Use nextboot(8) to tell the loader to load DTrace kernel modules during
409 * the next boot of the system. The nextboot(8) configuration is removed during
410 * boot, so it will not persist indefinitely.
415 char * const nbargv[] = {
417 "-e", "dtraceall_load=\"YES\"",
418 "-e", "dtrace_dof_load=\"YES\"",
419 "-e", "dtrace_dof_name=\"/boot/dtrace.dof\"",
420 "-e", "dtrace_dof_type=\"dtrace_dof\"",
426 err = posix_spawnp(&child, "nextboot", NULL, NULL, nbargv,
429 error("failed to execute nextboot: %s", strerror(err));
433 if (waitpid(child, &status, 0) != child)
434 fatal("waiting for nextboot");
435 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
436 error("nextboot returned with status %d", status);
442 etcsystem_prune(void)
446 char *buf, *start, *end;
448 char *fname = g_etcfile, *tmpname;
450 if ((fd = open(fname, O_RDONLY)) == -1)
451 fatal("failed to open %s", fname);
453 if (fstat(fd, &sbuf) == -1)
454 fatal("failed to fstat %s", fname);
456 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
457 fatal("failed to allocate memory for %s", fname);
459 if (read(fd, buf, sz) != sz)
460 fatal("failed to read %s", fname);
465 if ((start = strstr(buf, g_etcbegin)) == NULL)
468 if (strlen(buf) != sz) {
469 fatal("embedded nul byte in %s; manual repair of %s "
470 "required\n", fname, fname);
473 if (strstr(start + 1, g_etcbegin) != NULL) {
474 fatal("multiple start sentinels in %s; manual repair of %s "
475 "required\n", fname, fname);
478 if ((end = strstr(buf, g_etcend)) == NULL) {
479 fatal("missing end sentinel in %s; manual repair of %s "
480 "required\n", fname, fname);
484 fatal("end sentinel preceeds start sentinel in %s; manual "
485 "repair of %s required\n", fname, fname);
488 end += strlen(g_etcend) + 1;
489 bcopy(end, start, strlen(end) + 1);
491 tmpname = alloca(sz = strlen(fname) + 80);
492 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
494 if ((fd = open(tmpname,
495 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
496 fatal("failed to create %s", tmpname);
498 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
499 (void) unlink(tmpname);
500 fatal("failed to write to %s", tmpname);
505 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
506 (void) unlink(tmpname);
507 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
508 (int)sbuf.st_uid, (int)sbuf.st_gid);
511 if (rename(tmpname, fname) == -1)
512 fatal("rename of %s to %s failed", tmpname, fname);
514 error("cleaned up forceload directives in %s\n", fname);
522 const char *mods[20];
525 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
526 fatal("failed to open output file '%s'", g_ofile);
528 oprintf("%s\n", g_etcbegin);
530 for (line = 0; g_etc[line] != NULL; line++)
531 oprintf("%s\n", g_etc[line]);
533 nmods = dtrace_provider_modules(g_dtp, mods,
534 sizeof (mods) / sizeof (char *) - 1);
536 if (nmods >= sizeof (mods) / sizeof (char *))
537 fatal("unexpectedly large number of modules!");
539 mods[nmods++] = "dtrace";
541 for (line = 0; line < nmods; line++)
542 oprintf("forceload: drv/%s\n", mods[line]);
544 oprintf("%s\n", g_etcend);
546 if (fclose(g_ofp) == EOF)
547 fatal("failed to close output file '%s'", g_ofile);
549 error("added forceload directives to %s\n", g_ofile);
551 #endif /* !__FreeBSD__ */
554 print_probe_info(const dtrace_probeinfo_t *p)
560 oprintf("\n\tProbe Description Attributes\n");
562 oprintf("\t\tIdentifier Names: %s\n",
563 dtrace_stability_name(p->dtp_attr.dtat_name));
564 oprintf("\t\tData Semantics: %s\n",
565 dtrace_stability_name(p->dtp_attr.dtat_data));
566 oprintf("\t\tDependency Class: %s\n",
567 dtrace_class_name(p->dtp_attr.dtat_class));
569 oprintf("\n\tArgument Attributes\n");
571 oprintf("\t\tIdentifier Names: %s\n",
572 dtrace_stability_name(p->dtp_arga.dtat_name));
573 oprintf("\t\tData Semantics: %s\n",
574 dtrace_stability_name(p->dtp_arga.dtat_data));
575 oprintf("\t\tDependency Class: %s\n",
576 dtrace_class_name(p->dtp_arga.dtat_class));
578 oprintf("\n\tArgument Types\n");
580 for (i = 0; i < p->dtp_argc; i++) {
581 if (p->dtp_argv[i].dtt_flags & DTT_FL_USER)
585 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
586 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
587 (void) strlcpy(buf, "(unknown)", sizeof (buf));
588 oprintf("\t\targs[%d]: %s%s\n", i, user, buf);
591 if (p->dtp_argc == 0)
592 oprintf("\t\tNone\n");
599 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
600 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
602 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
603 dtrace_probedesc_t *pdp = &edp->dted_probe;
604 dtrace_probeinfo_t p;
609 oprintf("\n%s:%s:%s:%s\n",
610 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
612 if (dtrace_probe_info(dtp, pdp, &p) == 0)
613 print_probe_info(&p);
620 * Execute the specified program by enabling the corresponding instrumentation.
621 * If -e has been specified, we get the program info but do not enable it. If
622 * -v has been specified, we print a stability report for the program.
625 exec_prog(const dtrace_cmd_t *dcp)
627 dtrace_ecbdesc_t *last = NULL;
628 dtrace_proginfo_t dpi;
631 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
632 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
633 dfatal("failed to enable '%s'", dcp->dc_name);
635 notice("%s '%s' matched %u probe%s\n",
636 dcp->dc_desc, dcp->dc_name,
637 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
641 oprintf("\nStability attributes for %s %s:\n",
642 dcp->dc_desc, dcp->dc_name);
644 oprintf("\n\tMinimum Probe Description Attributes\n");
645 oprintf("\t\tIdentifier Names: %s\n",
646 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
647 oprintf("\t\tData Semantics: %s\n",
648 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
649 oprintf("\t\tDependency Class: %s\n",
650 dtrace_class_name(dpi.dpi_descattr.dtat_class));
652 oprintf("\n\tMinimum Statement Attributes\n");
654 oprintf("\t\tIdentifier Names: %s\n",
655 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
656 oprintf("\t\tData Semantics: %s\n",
657 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
658 oprintf("\t\tDependency Class: %s\n",
659 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
662 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
663 (dtrace_stmt_f *)info_stmt, &last);
668 g_total += dpi.dpi_matches;
672 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
673 * storing in a driver.conf(4) file associated with the dtrace driver.
676 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
678 const uchar_t *p, *q;
681 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
684 q = p + dof->dofh_filesz;
688 * On FreeBSD, the DOF file is read directly during boot - just write
689 * two hex characters per byte.
691 oprintf("dof-data-%d=", n);
694 oprintf("%02x", *p++);
698 oprintf("dof-data-%d=0x%x", n, *p++);
701 oprintf(",0x%x", *p++);
706 dtrace_dof_destroy(g_dtp, dof);
710 * Link the specified D program in DOF form into an ELF file for use in either
711 * helpers, userland provider definitions, or both. If -o was specified, that
712 * path is used as the output file name. If -o wasn't specified and the input
713 * program is from a script whose name is %.d, use basename(%.o) as the output
714 * file name. Otherwise we use "d.out" as the default output file name.
717 link_prog(dtrace_cmd_t *dcp)
721 if (g_cmdc == 1 && g_ofile != NULL) {
722 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
723 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
724 strcmp(p, ".d") == 0) {
725 p[0] = '\0'; /* strip .d suffix */
726 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
727 "%s.o", basename(dcp->dc_arg));
728 } else if (g_cmdc > 1) {
729 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
730 "d.out.%td", dcp - g_cmdv);
732 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
736 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
737 dcp->dc_ofile, g_objc, g_objv) != 0)
738 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
743 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
745 dtrace_probeinfo_t p;
747 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
748 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
750 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
751 print_probe_info(&p);
761 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
762 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
764 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
769 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
770 error("failed to match %s:%s:%s:%s: %s\n",
771 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
772 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
773 dtrace_errmsg(dtp, dtrace_errno(dtp)));
781 * List the probes corresponding to the specified program by iterating over
782 * each statement and then matching probes to the statement probe descriptions.
785 list_prog(const dtrace_cmd_t *dcp)
787 dtrace_ecbdesc_t *last = NULL;
789 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
790 (dtrace_stmt_f *)list_stmt, &last);
794 compile_file(dtrace_cmd_t *dcp)
799 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
800 fatal("failed to open %s", dcp->dc_arg);
803 g_argv[0] = dcp->dc_arg;
805 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
806 g_cflags, g_argc, g_argv)) == NULL)
807 dfatal("failed to compile script %s", dcp->dc_arg);
812 dcp->dc_desc = "script";
813 dcp->dc_name = dcp->dc_arg;
817 compile_str(dtrace_cmd_t *dcp)
821 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
822 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
823 dfatal("invalid probe specifier %s", dcp->dc_arg);
825 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
826 *p = '\0'; /* crop name for reporting */
828 dcp->dc_desc = "description";
829 dcp->dc_name = dcp->dc_arg;
834 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
837 const psinfo_t *prp = Ppsinfo(P);
838 int pid = Pstatus(P)->pr_pid;
839 char name[SIG2STR_MAX];
841 int wstatus = proc_getwstat(P);
842 int pid = proc_getpid(P);
846 notice("pid %d: %s\n", pid, msg);
853 switch (proc_state(P)) {
858 * Ideally we would like to always report pr_wstat here, but it
859 * isn't possible given current /proc semantics. If we grabbed
860 * the process, Ppsinfo() will either fail or return a zeroed
861 * psinfo_t depending on how far the parent is in reaping it.
862 * When /proc provides a stable pr_wstat in the status file,
863 * this code can be improved by examining this new pr_wstat.
865 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
866 notice("pid %d terminated by %s\n", pid,
867 proc_signame(WTERMSIG(prp->pr_wstat),
868 name, sizeof (name)));
870 if (WIFSIGNALED(wstatus)) {
871 notice("pid %d terminated by %d\n", pid,
875 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
876 notice("pid %d exited with status %d\n",
877 pid, WEXITSTATUS(prp->pr_wstat));
879 } else if (WEXITSTATUS(wstatus) != 0) {
880 notice("pid %d exited with status %d\n",
881 pid, WEXITSTATUS(wstatus));
884 notice("pid %d has exited\n", pid);
890 notice("pid %d exec'd a set-id or unobservable program\n", pid);
898 errhandler(const dtrace_errdata_t *data, void *arg)
900 error(data->dteda_msg);
901 return (DTRACE_HANDLE_OK);
906 drophandler(const dtrace_dropdata_t *data, void *arg)
908 error(data->dtdda_msg);
909 return (DTRACE_HANDLE_OK);
914 setopthandler(const dtrace_setoptdata_t *data, void *arg)
916 if (strcmp(data->dtsda_option, "quiet") == 0)
917 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
919 if (strcmp(data->dtsda_option, "flowindent") == 0)
920 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
922 return (DTRACE_HANDLE_OK);
925 #define BUFDUMPHDR(hdr) \
926 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
928 #define BUFDUMPSTR(ptr, field) \
929 (void) printf("%s: %20s => ", g_pname, #field); \
930 if ((ptr)->field != NULL) { \
931 const char *c = (ptr)->field; \
932 (void) printf("\""); \
935 (void) printf("\\n"); \
939 (void) printf("%c", *c); \
940 } while (*c++ != '\0'); \
941 (void) printf("\"\n"); \
943 (void) printf("<NULL>\n"); \
946 #define BUFDUMPASSTR(ptr, field, str) \
947 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
949 #define BUFDUMP(ptr, field) \
950 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
951 (long long)(ptr)->field);
953 #define BUFDUMPPTR(ptr, field) \
954 (void) printf("%s: %20s => %s\n", g_pname, #field, \
955 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
959 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
961 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
962 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
963 const dtrace_probedesc_t *pd;
964 uint32_t flags = bufdata->dtbda_flags;
965 char buf[512], *c = buf, *end = c + sizeof (buf);
972 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
973 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
974 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
975 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
976 { "???", UINT32_MAX },
980 if (bufdata->dtbda_probe != NULL) {
981 pd = bufdata->dtbda_probe->dtpda_pdesc;
982 } else if (agg != NULL) {
983 pd = agg->dtada_pdesc;
988 BUFDUMPHDR(">>> Called buffer handler");
991 BUFDUMPHDR(" dtrace_bufdata");
992 BUFDUMPSTR(bufdata, dtbda_buffered);
993 BUFDUMPPTR(bufdata, dtbda_probe);
994 BUFDUMPPTR(bufdata, dtbda_aggdata);
995 BUFDUMPPTR(bufdata, dtbda_recdesc);
997 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
1000 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
1001 if (!(flags & flagnames[i].value))
1004 (void) snprintf(c, end - c,
1005 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
1007 flags &= ~flagnames[i].value;
1011 (void) snprintf(c, end - c, ")");
1013 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
1017 BUFDUMPHDR(" dtrace_probedesc");
1018 BUFDUMPSTR(pd, dtpd_provider);
1019 BUFDUMPSTR(pd, dtpd_mod);
1020 BUFDUMPSTR(pd, dtpd_func);
1021 BUFDUMPSTR(pd, dtpd_name);
1026 BUFDUMPHDR(" dtrace_recdesc");
1027 BUFDUMP(rec, dtrd_action);
1028 BUFDUMP(rec, dtrd_size);
1032 int lim = rec->dtrd_size;
1034 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
1035 c = buf + strlen(buf);
1037 if (lim > sizeof (uint64_t))
1038 lim = sizeof (uint64_t);
1040 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
1042 for (i = 0; i < lim; i++) {
1043 (void) snprintf(c, end - c, "%s%02x",
1044 i == 0 ? "" : " ", *data++);
1048 (void) snprintf(c, end - c,
1049 "%s)", lim < rec->dtrd_size ? " ..." : "");
1050 BUFDUMPASSTR(rec, dtrd_offset, buf);
1052 BUFDUMP(rec, dtrd_offset);
1059 dtrace_aggdesc_t *desc = agg->dtada_desc;
1061 BUFDUMPHDR(" dtrace_aggdesc");
1062 BUFDUMPSTR(desc, dtagd_name);
1063 BUFDUMP(desc, dtagd_varid);
1064 BUFDUMP(desc, dtagd_id);
1065 BUFDUMP(desc, dtagd_nrecs);
1069 return (DTRACE_HANDLE_OK);
1074 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1076 dtrace_actkind_t act;
1081 * We have processed the final record; output the newline if
1082 * we're not in quiet mode.
1087 return (DTRACE_CONSUME_NEXT);
1090 act = rec->dtrd_action;
1091 addr = (uintptr_t)data->dtpda_data;
1093 if (act == DTRACEACT_EXIT) {
1094 g_status = *((uint32_t *)addr);
1095 return (DTRACE_CONSUME_NEXT);
1098 return (DTRACE_CONSUME_THIS);
1103 chew(const dtrace_probedata_t *data, void *arg)
1105 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1106 processorid_t cpu = data->dtpda_cpu;
1111 return (DTRACE_CONSUME_ABORT);
1115 if (!g_flowindent) {
1117 oprintf("%3s %6s %32s\n",
1118 "CPU", "ID", "FUNCTION:NAME");
1121 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1126 if (!g_flowindent) {
1128 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1130 (void) snprintf(name, sizeof (name), "%s:%s",
1131 pd->dtpd_func, pd->dtpd_name);
1133 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1136 int indent = data->dtpda_indent;
1140 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1141 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1143 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1144 data->dtpda_prefix, pd->dtpd_func,
1147 len = indent + DTRACE_FUNCNAMELEN + 5;
1149 (void) snprintf(name, len, "%*s%s%s", indent, "",
1150 data->dtpda_prefix, pd->dtpd_func);
1153 oprintf("%3d %-41s ", cpu, name);
1156 return (DTRACE_CONSUME_THIS);
1167 dtrace_optval_t val;
1169 { "buffer size", "bufsize" },
1170 { "aggregation size", "aggsize" },
1171 { "speculation size", "specsize" },
1172 { "dynamic variable size", "dynvarsize" },
1175 { "cleaning rate", "cleanrate" },
1176 { "status rate", "statusrate" },
1180 for (i = 0; bufs[i].name != NULL; i++) {
1181 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1182 fatal("couldn't get option %s", bufs[i].optname);
1185 for (i = 0; rates[i].name != NULL; i++) {
1186 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1187 fatal("couldn't get option %s", rates[i].optname);
1190 if (dtrace_go(g_dtp) == -1)
1191 dfatal("could not enable tracing");
1193 for (i = 0; bufs[i].name != NULL; i++) {
1194 dtrace_optval_t j = 0, mul = 10;
1195 dtrace_optval_t nsize;
1197 if (bufs[i].val == DTRACEOPT_UNSET)
1200 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1202 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1205 if (nsize >= bufs[i].val - sizeof (uint64_t))
1208 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1211 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1212 error("%s lowered to %lld%c\n", bufs[i].name,
1213 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1215 error("%s lowered to %lld bytes\n", bufs[i].name,
1220 for (i = 0; rates[i].name != NULL; i++) {
1221 dtrace_optval_t nval;
1224 if (rates[i].val == DTRACEOPT_UNSET)
1227 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1229 if (nval == DTRACEOPT_UNSET || nval == 0)
1232 if (rates[i].val == nval)
1235 dir = nval > rates[i].val ? "reduced" : "increased";
1237 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1238 error("%s %s to %lld hz\n", rates[i].name, dir,
1239 (long long)NANOSEC / (long long)nval);
1243 if ((nval % NANOSEC) == 0) {
1244 error("%s %s to once every %lld seconds\n",
1246 (long long)nval / (long long)NANOSEC);
1250 error("%s %s to once every %lld nanoseconds\n",
1251 rates[i].name, dir, (long long)nval);
1268 siginfo(int signo __unused)
1277 installsighands(void)
1279 struct sigaction act, oact;
1281 (void) sigemptyset(&act.sa_mask);
1283 act.sa_handler = intr;
1285 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1286 (void) sigaction(SIGINT, &act, NULL);
1288 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1289 (void) sigaction(SIGTERM, &act, NULL);
1292 if (sigaction(SIGPIPE, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1293 (void) sigaction(SIGPIPE, &act, NULL);
1295 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1296 (void) sigaction(SIGUSR1, &act, NULL);
1298 act.sa_handler = siginfo;
1299 if (sigaction(SIGINFO, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1300 (void) sigaction(SIGINFO, &act, NULL);
1305 main(int argc, char *argv[])
1307 dtrace_bufdesc_t buf;
1308 dtrace_status_t status[2];
1309 dtrace_optval_t opt;
1313 int done = 0, mode = 0;
1316 struct ps_prochandle *P;
1319 g_pname = basename(argv[0]);
1322 return (usage(stderr));
1324 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1325 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1326 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1327 fatal("failed to allocate memory for arguments");
1329 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1330 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1332 bzero(status, sizeof (status));
1333 bzero(&buf, sizeof (buf));
1336 * Make an initial pass through argv[] processing any arguments that
1337 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1338 * We also accumulate arguments that are not affiliated with getopt
1339 * options into g_argv[], and abort if any invalid options are found.
1341 for (optind = 1; optind < argc; optind++) {
1342 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1345 if (strcmp(optarg, "2") != 0) {
1346 (void) fprintf(stderr,
1347 "%s: illegal option -- 3%s\n",
1349 return (usage(stderr));
1351 g_oflags &= ~DTRACE_O_LP64;
1352 g_oflags |= DTRACE_O_ILP32;
1356 if (strcmp(optarg, "4") != 0) {
1357 (void) fprintf(stderr,
1358 "%s: illegal option -- 6%s\n",
1360 return (usage(stderr));
1362 g_oflags &= ~DTRACE_O_ILP32;
1363 g_oflags |= DTRACE_O_LP64;
1367 g_grabanon++; /* also checked in pass 2 below */
1371 g_mode = DMODE_ANON;
1382 g_mode = DMODE_HEADER;
1383 g_oflags |= DTRACE_O_NODEV;
1384 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1390 g_mode = DMODE_LINK;
1391 g_oflags |= DTRACE_O_NODEV;
1392 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1398 g_mode = DMODE_LIST;
1399 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1404 g_mode = DMODE_VERS;
1409 if (strchr(DTRACE_OPTSTR, c) == NULL)
1410 return (usage(stderr));
1415 g_argv[g_argc++] = argv[optind];
1419 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1420 "can be specified at a time\n", g_pname);
1424 if (g_mode == DMODE_VERS)
1425 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1428 * If we're in linker mode and the data model hasn't been specified,
1429 * we try to guess the appropriate setting by examining the object
1430 * files. We ignore certain errors since we'll catch them later when
1431 * we actually process the object files.
1433 if (g_mode == DMODE_LINK &&
1434 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1435 elf_version(EV_CURRENT) != EV_NONE) {
1440 for (i = 1; i < g_argc; i++) {
1441 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1444 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1449 if (elf_kind(elf) != ELF_K_ELF ||
1450 gelf_getehdr(elf, &ehdr) == NULL) {
1452 (void) elf_end(elf);
1457 (void) elf_end(elf);
1459 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1460 if (g_oflags & DTRACE_O_ILP32) {
1461 fatal("can't mix 32-bit and 64-bit "
1464 g_oflags |= DTRACE_O_LP64;
1465 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1466 if (g_oflags & DTRACE_O_LP64) {
1467 fatal("can't mix 32-bit and 64-bit "
1470 g_oflags |= DTRACE_O_ILP32;
1478 * Open libdtrace. If we are not actually going to be enabling any
1479 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1481 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1482 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1483 g_oflags |= DTRACE_O_NODEV;
1487 fatal("failed to initialize dtrace: %s\n",
1488 dtrace_errmsg(NULL, err));
1491 #if defined(__i386__)
1492 /* XXX The 32-bit seems to need more buffer space by default -sson */
1493 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1494 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1496 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1497 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1499 (void) dtrace_setopt(g_dtp, "temporal", "yes");
1502 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1503 * references to undefined symbols to remain as unresolved relocations.
1504 * If -A is specified, enable -xlink=primary to permit static linking
1505 * only to kernel symbols that are defined in a primary kernel module.
1507 if (g_mode == DMODE_LINK) {
1508 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1509 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1512 * Use the remaining arguments as the list of object files
1513 * when in linker mode.
1515 g_objc = g_argc - 1;
1516 g_objv = g_argv + 1;
1519 * We still use g_argv[0], the name of the executable.
1522 } else if (g_mode == DMODE_ANON)
1523 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1526 * Now that we have libdtrace open, make a second pass through argv[]
1527 * to perform any dtrace_setopt() calls and change any compiler flags.
1528 * We also accumulate any program specifications into our g_cmdv[] at
1529 * this time; these will compiled as part of the fourth processing pass.
1531 for (optind = 1; optind < argc; optind++) {
1532 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1535 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1536 dfatal("failed to set -a");
1540 if (dtrace_setopt(g_dtp,
1541 "bufsize", optarg) != 0)
1542 dfatal("failed to set -b %s", optarg);
1550 g_cflags |= DTRACE_C_CPP;
1554 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1555 dfatal("failed to set -D %s", optarg);
1559 dcp = &g_cmdv[g_cmdc++];
1560 dcp->dc_func = compile_str;
1561 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1562 dcp->dc_arg = optarg;
1566 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1567 dfatal("failed to set -F");
1571 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1572 dfatal("failed to set -H");
1576 dcp = &g_cmdv[g_cmdc++];
1577 dcp->dc_func = compile_str;
1578 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1579 dcp->dc_arg = optarg;
1583 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1584 dfatal("failed to set -I %s", optarg);
1588 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1589 dfatal("failed to set -L %s", optarg);
1593 dcp = &g_cmdv[g_cmdc++];
1594 dcp->dc_func = compile_str;
1595 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1596 dcp->dc_arg = optarg;
1600 dcp = &g_cmdv[g_cmdc++];
1601 dcp->dc_func = compile_str;
1602 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1603 dcp->dc_arg = optarg;
1607 dcp = &g_cmdv[g_cmdc++];
1608 dcp->dc_func = compile_str;
1609 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1610 dcp->dc_arg = optarg;
1614 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1615 dfatal("failed to set -q");
1623 dcp = &g_cmdv[g_cmdc++];
1624 dcp->dc_func = compile_file;
1625 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1626 dcp->dc_arg = optarg;
1630 g_cflags |= DTRACE_C_DIFV;
1634 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1635 dfatal("failed to set -U %s", optarg);
1643 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1644 dfatal("failed to set -w");
1648 if ((p = strchr(optarg, '=')) != NULL)
1651 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1652 dfatal("failed to set -x %s", optarg);
1656 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1657 dfatal("failed to set -X %s", optarg);
1661 g_cflags |= DTRACE_C_ZDEFS;
1665 if (strchr(DTRACE_OPTSTR, c) == NULL)
1666 return (usage(stderr));
1671 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1672 (void) fprintf(stderr, "%s: -B not valid in combination"
1673 " with [-AGl] options\n", g_pname);
1677 if (g_ofp == NULL && g_ofile != NULL) {
1678 (void) fprintf(stderr, "%s: -B not valid in combination"
1679 " with -o option\n", g_pname);
1684 * In our third pass we handle any command-line options related to
1685 * grabbing or creating victim processes. The behavior of these calls
1686 * may been affected by any library options set by the second pass.
1688 for (optind = 1; optind < argc; optind++) {
1689 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1692 if ((v = make_argv(optarg)) == NULL)
1693 fatal("failed to allocate memory");
1695 P = dtrace_proc_create(g_dtp, v[0], v, NULL, NULL);
1697 dfatal(NULL); /* dtrace_errmsg() only */
1705 pid = strtol(optarg, &p, 10);
1707 if (errno != 0 || p == optarg || p[0] != '\0')
1708 fatal("invalid pid: %s\n", optarg);
1710 P = dtrace_proc_grab(g_dtp, pid, 0);
1712 dfatal(NULL); /* dtrace_errmsg() only */
1721 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1722 * each string or file specification into a compiled program structure.
1724 for (i = 0; i < g_cmdc; i++)
1725 g_cmdv[i].dc_func(&g_cmdv[i]);
1727 if (g_mode != DMODE_LIST) {
1728 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1729 dfatal("failed to establish error handler");
1731 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1732 dfatal("failed to establish drop handler");
1734 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1735 dfatal("failed to establish proc handler");
1737 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1738 dfatal("failed to establish setopt handler");
1740 if (g_ofp == NULL &&
1741 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1742 dfatal("failed to establish buffered handler");
1745 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1746 g_flowindent = opt != DTRACEOPT_UNSET;
1748 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1749 g_grabanon = opt != DTRACEOPT_UNSET;
1751 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1752 g_quiet = opt != DTRACEOPT_UNSET;
1755 * Now make a fifth and final pass over the options that have been
1756 * turned into programs and saved in g_cmdv[], performing any mode-
1757 * specific processing. If g_mode is DMODE_EXEC, we will break out
1758 * of the switch() and continue on to the data processing loop. For
1759 * other modes, we will exit dtrace once mode-specific work is done.
1763 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1764 fatal("failed to open output file '%s'", g_ofile);
1766 for (i = 0; i < g_cmdc; i++)
1767 exec_prog(&g_cmdv[i]);
1769 if (done && !g_grabanon) {
1770 dtrace_close(g_dtp);
1776 if (g_ofile == NULL)
1778 g_ofile = "/kernel/drv/dtrace.conf";
1781 * On FreeBSD, anonymous DOF data is written to
1782 * the DTrace DOF file.
1784 g_ofile = "/boot/dtrace.dof";
1787 dof_prune(g_ofile); /* strip out any old DOF directives */
1789 etcsystem_prune(); /* string out any forceload directives */
1793 dtrace_close(g_dtp);
1797 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1798 fatal("failed to open output file '%s'", g_ofile);
1800 for (i = 0; i < g_cmdc; i++) {
1801 anon_prog(&g_cmdv[i],
1802 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1806 * Dump out the DOF corresponding to the error handler and the
1807 * current options as the final DOF property in the .conf file.
1809 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1810 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1812 if (fclose(g_ofp) == EOF)
1813 fatal("failed to close output file '%s'", g_ofile);
1816 * These messages would use notice() rather than error(), but
1817 * we don't want them suppressed when -A is run on a D program
1818 * that itself contains a #pragma D option quiet.
1820 error("saved anonymous enabling in %s\n", g_ofile);
1826 error("run update_drv(1M) or reboot to enable changes\n");
1829 dtrace_close(g_dtp);
1834 (void) fprintf(stderr, "%s: -G requires one or more "
1835 "scripts or enabling options\n", g_pname);
1836 dtrace_close(g_dtp);
1840 for (i = 0; i < g_cmdc; i++)
1841 link_prog(&g_cmdv[i]);
1843 if (g_cmdc > 1 && g_ofile != NULL) {
1844 char **objv = alloca(g_cmdc * sizeof (char *));
1846 for (i = 0; i < g_cmdc; i++)
1847 objv[i] = g_cmdv[i].dc_ofile;
1849 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1850 g_ofile, g_cmdc, objv) != 0)
1851 dfatal(NULL); /* dtrace_errmsg() only */
1854 dtrace_close(g_dtp);
1858 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1859 fatal("failed to open output file '%s'", g_ofile);
1863 oprintf("%5s %10s %17s %33s %s\n",
1864 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1866 for (i = 0; i < g_cmdc; i++)
1867 list_prog(&g_cmdv[i]);
1870 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1872 dtrace_close(g_dtp);
1877 (void) fprintf(stderr, "%s: -h requires one or more "
1878 "scripts or enabling options\n", g_pname);
1879 dtrace_close(g_dtp);
1883 if (g_ofile == NULL) {
1887 (void) fprintf(stderr, "%s: -h requires an "
1888 "output file if multiple scripts are "
1889 "specified\n", g_pname);
1890 dtrace_close(g_dtp);
1894 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1895 strcmp(p, ".d") != 0) {
1896 (void) fprintf(stderr, "%s: -h requires an "
1897 "output file if no scripts are "
1898 "specified\n", g_pname);
1899 dtrace_close(g_dtp);
1903 p[0] = '\0'; /* strip .d suffix */
1904 g_ofile = p = g_cmdv[0].dc_ofile;
1905 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1906 "%s.h", basename(g_cmdv[0].dc_arg));
1909 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1910 fatal("failed to open header file '%s'", g_ofile);
1912 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1914 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1915 fclose(g_ofp) == EOF)
1916 dfatal("failed to create header file %s", g_ofile);
1918 dtrace_close(g_dtp);
1923 * If -a and -Z were not specified and no probes have been matched, no
1924 * probe criteria was specified on the command line and we abort.
1926 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1927 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1930 * Start tracing. Once we dtrace_go(), reload any options that affect
1931 * our globals in case consuming anonymous state has changed them.
1935 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1936 g_flowindent = opt != DTRACEOPT_UNSET;
1938 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1939 g_grabanon = opt != DTRACEOPT_UNSET;
1941 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1942 g_quiet = opt != DTRACEOPT_UNSET;
1944 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1945 if (opt != DTRACEOPT_UNSET)
1946 notice("allowing destructive actions\n");
1951 * Now that tracing is active and we are ready to consume trace data,
1952 * continue any grabbed or created processes, setting them running
1953 * using the /proc control mechanism inside of libdtrace.
1955 for (i = 0; i < g_psc; i++)
1956 dtrace_proc_continue(g_dtp, g_psv[i]);
1958 g_pslive = g_psc; /* count for prochandler() */
1961 if (!g_intr && !done)
1962 dtrace_sleep(g_dtp);
1966 (void)dtrace_aggregate_print(g_dtp, g_ofp, NULL);
1973 * Output a newline just to make the output look
1974 * slightly cleaner. Note that we do this even in
1981 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1983 if (dtrace_stop(g_dtp) == -1)
1984 dfatal("couldn't stop tracing");
1987 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1988 case DTRACE_WORKSTATUS_DONE:
1991 case DTRACE_WORKSTATUS_OKAY:
1994 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1995 dfatal("processing aborted");
1998 if (g_ofp != NULL && fflush(g_ofp) == EOF)
2005 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
2006 dtrace_errno(g_dtp) != EINTR)
2007 dfatal("failed to print aggregations");
2010 dtrace_close(g_dtp);