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;
100 static int g_exec = 1;
101 static int g_mode = DMODE_EXEC;
102 static int g_status = E_SUCCESS;
103 static int g_grabanon = 0;
104 static const char *g_ofile = NULL;
106 static dtrace_hdl_t *g_dtp;
108 static char *g_etcfile = "/etc/system";
109 static const char *g_etcbegin = "* vvvv Added by DTrace";
110 static const char *g_etcend = "* ^^^^ Added by DTrace";
112 static const char *g_etc[] = {
114 "* The following forceload directives were added by dtrace(1M) to allow for",
115 "* tracing during boot. If these directives are removed, the system will",
116 "* continue to function, but tracing will not occur during boot as desired.",
117 "* To remove these directives (and this block comment) automatically, run",
118 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
119 "* chapter of the Solaris Dynamic Tracing Guide for details.",
127 static const char predact[] = "[[ predicate ] action ]";
129 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
130 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
131 "[-o output] [-p pid] [-s script] [-U name]\n\t"
132 "[-x opt[=val]] [-X a|c|s|t]\n\n"
133 "\t[-P provider %s]\n"
134 "\t[-m [ provider: ] module %s]\n"
135 "\t[-f [[ provider: ] module: ] func %s]\n"
136 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
137 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
138 predact, predact, predact, predact, predact);
140 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
141 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
143 (void) fprintf(fp, "\n"
144 "\t-32 generate 32-bit D programs and ELF files\n"
145 "\t-64 generate 64-bit D programs and ELF files\n\n"
146 "\t-a claim anonymous tracing state\n"
147 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
148 "\t-b set trace buffer size\n"
149 "\t-c run specified command and exit upon its completion\n"
150 "\t-C run cpp(1) preprocessor on script files\n"
151 "\t-D define symbol when invoking preprocessor\n"
152 "\t-e exit after compiling request but prior to enabling probes\n"
153 "\t-f enable or list probes matching the specified function name\n"
154 "\t-F coalesce trace output by function\n"
155 "\t-G generate an ELF file containing embedded dtrace program\n"
156 "\t-h generate a header file with definitions for static probes\n"
157 "\t-H print included files when invoking preprocessor\n"
158 "\t-i enable or list probes matching the specified probe id\n"
159 "\t-I add include directory to preprocessor search path\n"
160 "\t-l list probes matching specified criteria\n"
161 "\t-L add library directory to library search path\n"
162 "\t-m enable or list probes matching the specified module name\n"
163 "\t-n enable or list probes matching the specified probe name\n"
164 "\t-o set output file\n"
165 "\t-p grab specified process-ID and cache its symbol tables\n"
166 "\t-P enable or list probes matching the specified provider name\n"
167 "\t-q set quiet mode (only output explicitly traced data)\n"
168 "\t-s enable or list probes according to the specified D script\n"
169 "\t-S print D compiler intermediate code\n"
170 "\t-U undefine symbol when invoking preprocessor\n"
171 "\t-v set verbose mode (report stability attributes, arguments)\n"
172 "\t-V report DTrace API version\n"
173 "\t-w permit destructive actions\n"
174 "\t-x enable or modify compiler and tracing options\n"
175 "\t-X specify ISO C conformance settings for preprocessor\n"
176 "\t-Z permit probe descriptions that match zero probes\n");
182 verror(const char *fmt, va_list ap)
186 (void) fprintf(stderr, "%s: ", g_pname);
187 (void) vfprintf(stderr, fmt, ap);
189 if (fmt[strlen(fmt) - 1] != '\n')
190 (void) fprintf(stderr, ": %s\n", strerror(error));
195 fatal(const char *fmt, ...)
204 * Close the DTrace handle to ensure that any controlled processes are
205 * correctly restored and continued.
215 dfatal(const char *fmt, ...)
217 #if !defined(illumos) && defined(NEED_ERRLOC)
218 char *p_errfile = NULL;
225 (void) fprintf(stderr, "%s: ", g_pname);
227 (void) vfprintf(stderr, fmt, ap);
231 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
232 (void) fprintf(stderr, ": %s\n",
233 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
234 } else if (fmt == NULL) {
235 (void) fprintf(stderr, "%s\n",
236 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
238 #if !defined(illumos) && defined(NEED_ERRLOC)
239 dt_get_errloc(g_dtp, &p_errfile, &errline);
240 if (p_errfile != NULL)
241 printf("File '%s', line %d\n", p_errfile, errline);
245 * Close the DTrace handle to ensure that any controlled processes are
246 * correctly restored and continued.
255 error(const char *fmt, ...)
266 notice(const char *fmt, ...)
271 return; /* -q or quiet pragma suppresses notice()s */
280 oprintf(const char *fmt, ...)
289 n = vfprintf(g_ofp, fmt, ap);
293 if (errno != EINTR) {
294 fatal("failed to write to %s",
295 g_ofile ? g_ofile : "<stdout>");
304 const char *ws = "\f\n\r\t\v ";
305 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
312 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
323 dof_prune(const char *fname)
326 size_t sz, i, j, mark, len;
330 if ((fd = open(fname, O_RDONLY)) == -1) {
332 * This is okay only if the file doesn't exist at all.
335 fatal("failed to open %s", fname);
339 if (fstat(fd, &sbuf) == -1)
340 fatal("failed to fstat %s", fname);
342 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
343 fatal("failed to allocate memory for %s", fname);
345 if (read(fd, buf, sz) != sz)
346 fatal("failed to read %s", fname);
351 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
352 fatal("failed to open %s for writing", fname);
354 len = strlen("dof-data-");
356 for (mark = 0, i = 0; i < sz; i++) {
357 if (strncmp(&buf[i], "dof-data-", len) != 0)
361 * This is only a match if it's in the 0th column.
363 if (i != 0 && buf[i - 1] != '\n')
367 error("cleaned up old anonymous "
368 "enabling in %s\n", fname);
372 * We have a match. First write out our data up until now.
375 if (write(fd, &buf[mark], i - mark) != i - mark)
376 fatal("failed to write to %s", fname);
380 * Now scan forward until we scan past a newline.
382 for (j = i; j < sz && buf[j] != '\n'; j++)
388 if ((mark = j + 1) >= sz)
395 if (write(fd, &buf[mark], sz - mark) != sz - mark)
396 fatal("failed to write to %s", fname);
405 * Use nextboot(8) to tell the loader to load DTrace kernel modules during
406 * the next boot of the system. The nextboot(8) configuration is removed during
407 * boot, so it will not persist indefinitely.
412 char * const nbargv[] = {
414 "-e", "dtraceall_load=\"YES\"",
415 "-e", "dtrace_dof_load=\"YES\"",
416 "-e", "dtrace_dof_name=\"/boot/dtrace.dof\"",
417 "-e", "dtrace_dof_type=\"dtrace_dof\"",
423 err = posix_spawnp(&child, "nextboot", NULL, NULL, nbargv,
426 error("failed to execute nextboot: %s", strerror(err));
430 if (waitpid(child, &status, 0) != child)
431 fatal("waiting for nextboot");
432 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
433 error("nextboot returned with status %d", status);
439 etcsystem_prune(void)
443 char *buf, *start, *end;
445 char *fname = g_etcfile, *tmpname;
447 if ((fd = open(fname, O_RDONLY)) == -1)
448 fatal("failed to open %s", fname);
450 if (fstat(fd, &sbuf) == -1)
451 fatal("failed to fstat %s", fname);
453 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
454 fatal("failed to allocate memory for %s", fname);
456 if (read(fd, buf, sz) != sz)
457 fatal("failed to read %s", fname);
462 if ((start = strstr(buf, g_etcbegin)) == NULL)
465 if (strlen(buf) != sz) {
466 fatal("embedded nul byte in %s; manual repair of %s "
467 "required\n", fname, fname);
470 if (strstr(start + 1, g_etcbegin) != NULL) {
471 fatal("multiple start sentinels in %s; manual repair of %s "
472 "required\n", fname, fname);
475 if ((end = strstr(buf, g_etcend)) == NULL) {
476 fatal("missing end sentinel in %s; manual repair of %s "
477 "required\n", fname, fname);
481 fatal("end sentinel preceeds start sentinel in %s; manual "
482 "repair of %s required\n", fname, fname);
485 end += strlen(g_etcend) + 1;
486 bcopy(end, start, strlen(end) + 1);
488 tmpname = alloca(sz = strlen(fname) + 80);
489 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
491 if ((fd = open(tmpname,
492 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
493 fatal("failed to create %s", tmpname);
495 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
496 (void) unlink(tmpname);
497 fatal("failed to write to %s", tmpname);
502 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
503 (void) unlink(tmpname);
504 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
505 (int)sbuf.st_uid, (int)sbuf.st_gid);
508 if (rename(tmpname, fname) == -1)
509 fatal("rename of %s to %s failed", tmpname, fname);
511 error("cleaned up forceload directives in %s\n", fname);
519 const char *mods[20];
522 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
523 fatal("failed to open output file '%s'", g_ofile);
525 oprintf("%s\n", g_etcbegin);
527 for (line = 0; g_etc[line] != NULL; line++)
528 oprintf("%s\n", g_etc[line]);
530 nmods = dtrace_provider_modules(g_dtp, mods,
531 sizeof (mods) / sizeof (char *) - 1);
533 if (nmods >= sizeof (mods) / sizeof (char *))
534 fatal("unexpectedly large number of modules!");
536 mods[nmods++] = "dtrace";
538 for (line = 0; line < nmods; line++)
539 oprintf("forceload: drv/%s\n", mods[line]);
541 oprintf("%s\n", g_etcend);
543 if (fclose(g_ofp) == EOF)
544 fatal("failed to close output file '%s'", g_ofile);
546 error("added forceload directives to %s\n", g_ofile);
548 #endif /* !__FreeBSD__ */
551 print_probe_info(const dtrace_probeinfo_t *p)
557 oprintf("\n\tProbe Description Attributes\n");
559 oprintf("\t\tIdentifier Names: %s\n",
560 dtrace_stability_name(p->dtp_attr.dtat_name));
561 oprintf("\t\tData Semantics: %s\n",
562 dtrace_stability_name(p->dtp_attr.dtat_data));
563 oprintf("\t\tDependency Class: %s\n",
564 dtrace_class_name(p->dtp_attr.dtat_class));
566 oprintf("\n\tArgument Attributes\n");
568 oprintf("\t\tIdentifier Names: %s\n",
569 dtrace_stability_name(p->dtp_arga.dtat_name));
570 oprintf("\t\tData Semantics: %s\n",
571 dtrace_stability_name(p->dtp_arga.dtat_data));
572 oprintf("\t\tDependency Class: %s\n",
573 dtrace_class_name(p->dtp_arga.dtat_class));
575 oprintf("\n\tArgument Types\n");
577 for (i = 0; i < p->dtp_argc; i++) {
578 if (p->dtp_argv[i].dtt_flags & DTT_FL_USER)
582 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
583 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
584 (void) strlcpy(buf, "(unknown)", sizeof (buf));
585 oprintf("\t\targs[%d]: %s%s\n", i, user, buf);
588 if (p->dtp_argc == 0)
589 oprintf("\t\tNone\n");
596 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
597 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
599 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
600 dtrace_probedesc_t *pdp = &edp->dted_probe;
601 dtrace_probeinfo_t p;
606 oprintf("\n%s:%s:%s:%s\n",
607 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
609 if (dtrace_probe_info(dtp, pdp, &p) == 0)
610 print_probe_info(&p);
617 * Execute the specified program by enabling the corresponding instrumentation.
618 * If -e has been specified, we get the program info but do not enable it. If
619 * -v has been specified, we print a stability report for the program.
622 exec_prog(const dtrace_cmd_t *dcp)
624 dtrace_ecbdesc_t *last = NULL;
625 dtrace_proginfo_t dpi;
628 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
629 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
630 dfatal("failed to enable '%s'", dcp->dc_name);
632 notice("%s '%s' matched %u probe%s\n",
633 dcp->dc_desc, dcp->dc_name,
634 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
638 oprintf("\nStability attributes for %s %s:\n",
639 dcp->dc_desc, dcp->dc_name);
641 oprintf("\n\tMinimum Probe Description Attributes\n");
642 oprintf("\t\tIdentifier Names: %s\n",
643 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
644 oprintf("\t\tData Semantics: %s\n",
645 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
646 oprintf("\t\tDependency Class: %s\n",
647 dtrace_class_name(dpi.dpi_descattr.dtat_class));
649 oprintf("\n\tMinimum Statement Attributes\n");
651 oprintf("\t\tIdentifier Names: %s\n",
652 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
653 oprintf("\t\tData Semantics: %s\n",
654 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
655 oprintf("\t\tDependency Class: %s\n",
656 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
659 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
660 (dtrace_stmt_f *)info_stmt, &last);
665 g_total += dpi.dpi_matches;
669 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
670 * storing in a driver.conf(4) file associated with the dtrace driver.
673 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
675 const uchar_t *p, *q;
678 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
681 q = p + dof->dofh_loadsz;
685 * On FreeBSD, the DOF file is read directly during boot - just write
686 * two hex characters per byte.
688 oprintf("dof-data-%d=", n);
691 oprintf("%02x", *p++);
695 oprintf("dof-data-%d=0x%x", n, *p++);
698 oprintf(",0x%x", *p++);
703 dtrace_dof_destroy(g_dtp, dof);
707 * Link the specified D program in DOF form into an ELF file for use in either
708 * helpers, userland provider definitions, or both. If -o was specified, that
709 * path is used as the output file name. If -o wasn't specified and the input
710 * program is from a script whose name is %.d, use basename(%.o) as the output
711 * file name. Otherwise we use "d.out" as the default output file name.
714 link_prog(dtrace_cmd_t *dcp)
718 if (g_cmdc == 1 && g_ofile != NULL) {
719 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
720 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
721 strcmp(p, ".d") == 0) {
722 p[0] = '\0'; /* strip .d suffix */
723 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
724 "%s.o", basename(dcp->dc_arg));
725 } else if (g_cmdc > 1) {
726 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
727 "d.out.%td", dcp - g_cmdv);
729 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
733 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
734 dcp->dc_ofile, g_objc, g_objv) != 0)
735 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
740 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
742 dtrace_probeinfo_t p;
744 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
745 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
747 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
748 print_probe_info(&p);
758 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
759 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
761 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
766 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
767 error("failed to match %s:%s:%s:%s: %s\n",
768 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
769 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
770 dtrace_errmsg(dtp, dtrace_errno(dtp)));
778 * List the probes corresponding to the specified program by iterating over
779 * each statement and then matching probes to the statement probe descriptions.
782 list_prog(const dtrace_cmd_t *dcp)
784 dtrace_ecbdesc_t *last = NULL;
786 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
787 (dtrace_stmt_f *)list_stmt, &last);
791 compile_file(dtrace_cmd_t *dcp)
796 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
797 fatal("failed to open %s", dcp->dc_arg);
800 g_argv[0] = dcp->dc_arg;
802 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
803 g_cflags, g_argc, g_argv)) == NULL)
804 dfatal("failed to compile script %s", dcp->dc_arg);
809 dcp->dc_desc = "script";
810 dcp->dc_name = dcp->dc_arg;
814 compile_str(dtrace_cmd_t *dcp)
818 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
819 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
820 dfatal("invalid probe specifier %s", dcp->dc_arg);
822 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
823 *p = '\0'; /* crop name for reporting */
825 dcp->dc_desc = "description";
826 dcp->dc_name = dcp->dc_arg;
831 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
834 const psinfo_t *prp = Ppsinfo(P);
835 int pid = Pstatus(P)->pr_pid;
836 char name[SIG2STR_MAX];
838 int wstatus = proc_getwstat(P);
839 int pid = proc_getpid(P);
843 notice("pid %d: %s\n", pid, msg);
850 switch (proc_state(P)) {
855 * Ideally we would like to always report pr_wstat here, but it
856 * isn't possible given current /proc semantics. If we grabbed
857 * the process, Ppsinfo() will either fail or return a zeroed
858 * psinfo_t depending on how far the parent is in reaping it.
859 * When /proc provides a stable pr_wstat in the status file,
860 * this code can be improved by examining this new pr_wstat.
862 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
863 notice("pid %d terminated by %s\n", pid,
864 proc_signame(WTERMSIG(prp->pr_wstat),
865 name, sizeof (name)));
867 if (WIFSIGNALED(wstatus)) {
868 notice("pid %d terminated by %d\n", pid,
872 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
873 notice("pid %d exited with status %d\n",
874 pid, WEXITSTATUS(prp->pr_wstat));
876 } else if (WEXITSTATUS(wstatus) != 0) {
877 notice("pid %d exited with status %d\n",
878 pid, WEXITSTATUS(wstatus));
881 notice("pid %d has exited\n", pid);
887 notice("pid %d exec'd a set-id or unobservable program\n", pid);
895 errhandler(const dtrace_errdata_t *data, void *arg)
897 error(data->dteda_msg);
898 return (DTRACE_HANDLE_OK);
903 drophandler(const dtrace_dropdata_t *data, void *arg)
905 error(data->dtdda_msg);
906 return (DTRACE_HANDLE_OK);
911 setopthandler(const dtrace_setoptdata_t *data, void *arg)
913 if (strcmp(data->dtsda_option, "quiet") == 0)
914 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
916 if (strcmp(data->dtsda_option, "flowindent") == 0)
917 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
919 return (DTRACE_HANDLE_OK);
922 #define BUFDUMPHDR(hdr) \
923 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
925 #define BUFDUMPSTR(ptr, field) \
926 (void) printf("%s: %20s => ", g_pname, #field); \
927 if ((ptr)->field != NULL) { \
928 const char *c = (ptr)->field; \
929 (void) printf("\""); \
932 (void) printf("\\n"); \
936 (void) printf("%c", *c); \
937 } while (*c++ != '\0'); \
938 (void) printf("\"\n"); \
940 (void) printf("<NULL>\n"); \
943 #define BUFDUMPASSTR(ptr, field, str) \
944 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
946 #define BUFDUMP(ptr, field) \
947 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
948 (long long)(ptr)->field);
950 #define BUFDUMPPTR(ptr, field) \
951 (void) printf("%s: %20s => %s\n", g_pname, #field, \
952 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
956 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
958 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
959 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
960 const dtrace_probedesc_t *pd;
961 uint32_t flags = bufdata->dtbda_flags;
962 char buf[512], *c = buf, *end = c + sizeof (buf);
969 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
970 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
971 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
972 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
973 { "???", UINT32_MAX },
977 if (bufdata->dtbda_probe != NULL) {
978 pd = bufdata->dtbda_probe->dtpda_pdesc;
979 } else if (agg != NULL) {
980 pd = agg->dtada_pdesc;
985 BUFDUMPHDR(">>> Called buffer handler");
988 BUFDUMPHDR(" dtrace_bufdata");
989 BUFDUMPSTR(bufdata, dtbda_buffered);
990 BUFDUMPPTR(bufdata, dtbda_probe);
991 BUFDUMPPTR(bufdata, dtbda_aggdata);
992 BUFDUMPPTR(bufdata, dtbda_recdesc);
994 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
997 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
998 if (!(flags & flagnames[i].value))
1001 (void) snprintf(c, end - c,
1002 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
1004 flags &= ~flagnames[i].value;
1008 (void) snprintf(c, end - c, ")");
1010 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
1014 BUFDUMPHDR(" dtrace_probedesc");
1015 BUFDUMPSTR(pd, dtpd_provider);
1016 BUFDUMPSTR(pd, dtpd_mod);
1017 BUFDUMPSTR(pd, dtpd_func);
1018 BUFDUMPSTR(pd, dtpd_name);
1023 BUFDUMPHDR(" dtrace_recdesc");
1024 BUFDUMP(rec, dtrd_action);
1025 BUFDUMP(rec, dtrd_size);
1029 int lim = rec->dtrd_size;
1031 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
1032 c = buf + strlen(buf);
1034 if (lim > sizeof (uint64_t))
1035 lim = sizeof (uint64_t);
1037 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
1039 for (i = 0; i < lim; i++) {
1040 (void) snprintf(c, end - c, "%s%02x",
1041 i == 0 ? "" : " ", *data++);
1045 (void) snprintf(c, end - c,
1046 "%s)", lim < rec->dtrd_size ? " ..." : "");
1047 BUFDUMPASSTR(rec, dtrd_offset, buf);
1049 BUFDUMP(rec, dtrd_offset);
1056 dtrace_aggdesc_t *desc = agg->dtada_desc;
1058 BUFDUMPHDR(" dtrace_aggdesc");
1059 BUFDUMPSTR(desc, dtagd_name);
1060 BUFDUMP(desc, dtagd_varid);
1061 BUFDUMP(desc, dtagd_id);
1062 BUFDUMP(desc, dtagd_nrecs);
1066 return (DTRACE_HANDLE_OK);
1071 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1073 dtrace_actkind_t act;
1078 * We have processed the final record; output the newline if
1079 * we're not in quiet mode.
1084 return (DTRACE_CONSUME_NEXT);
1087 act = rec->dtrd_action;
1088 addr = (uintptr_t)data->dtpda_data;
1090 if (act == DTRACEACT_EXIT) {
1091 g_status = *((uint32_t *)addr);
1092 return (DTRACE_CONSUME_NEXT);
1095 return (DTRACE_CONSUME_THIS);
1100 chew(const dtrace_probedata_t *data, void *arg)
1102 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1103 processorid_t cpu = data->dtpda_cpu;
1108 return (DTRACE_CONSUME_ABORT);
1112 if (!g_flowindent) {
1114 oprintf("%3s %6s %32s\n",
1115 "CPU", "ID", "FUNCTION:NAME");
1118 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1123 if (!g_flowindent) {
1125 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1127 (void) snprintf(name, sizeof (name), "%s:%s",
1128 pd->dtpd_func, pd->dtpd_name);
1130 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1133 int indent = data->dtpda_indent;
1137 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1138 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1140 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1141 data->dtpda_prefix, pd->dtpd_func,
1144 len = indent + DTRACE_FUNCNAMELEN + 5;
1146 (void) snprintf(name, len, "%*s%s%s", indent, "",
1147 data->dtpda_prefix, pd->dtpd_func);
1150 oprintf("%3d %-41s ", cpu, name);
1153 return (DTRACE_CONSUME_THIS);
1164 dtrace_optval_t val;
1166 { "buffer size", "bufsize" },
1167 { "aggregation size", "aggsize" },
1168 { "speculation size", "specsize" },
1169 { "dynamic variable size", "dynvarsize" },
1172 { "cleaning rate", "cleanrate" },
1173 { "status rate", "statusrate" },
1177 for (i = 0; bufs[i].name != NULL; i++) {
1178 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1179 fatal("couldn't get option %s", bufs[i].optname);
1182 for (i = 0; rates[i].name != NULL; i++) {
1183 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1184 fatal("couldn't get option %s", rates[i].optname);
1187 if (dtrace_go(g_dtp) == -1)
1188 dfatal("could not enable tracing");
1190 for (i = 0; bufs[i].name != NULL; i++) {
1191 dtrace_optval_t j = 0, mul = 10;
1192 dtrace_optval_t nsize;
1194 if (bufs[i].val == DTRACEOPT_UNSET)
1197 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1199 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1202 if (nsize >= bufs[i].val - sizeof (uint64_t))
1205 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1208 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1209 error("%s lowered to %lld%c\n", bufs[i].name,
1210 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1212 error("%s lowered to %lld bytes\n", bufs[i].name,
1217 for (i = 0; rates[i].name != NULL; i++) {
1218 dtrace_optval_t nval;
1221 if (rates[i].val == DTRACEOPT_UNSET)
1224 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1226 if (nval == DTRACEOPT_UNSET || nval == 0)
1229 if (rates[i].val == nval)
1232 dir = nval > rates[i].val ? "reduced" : "increased";
1234 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1235 error("%s %s to %lld hz\n", rates[i].name, dir,
1236 (long long)NANOSEC / (long long)nval);
1240 if ((nval % NANOSEC) == 0) {
1241 error("%s %s to once every %lld seconds\n",
1243 (long long)nval / (long long)NANOSEC);
1247 error("%s %s to once every %lld nanoseconds\n",
1248 rates[i].name, dir, (long long)nval);
1264 installsighands(void)
1266 struct sigaction act, oact;
1268 (void) sigemptyset(&act.sa_mask);
1270 act.sa_handler = intr;
1272 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1273 (void) sigaction(SIGINT, &act, NULL);
1275 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1276 (void) sigaction(SIGTERM, &act, NULL);
1279 if (sigaction(SIGPIPE, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1280 (void) sigaction(SIGPIPE, &act, NULL);
1282 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1283 (void) sigaction(SIGUSR1, &act, NULL);
1288 main(int argc, char *argv[])
1290 dtrace_bufdesc_t buf;
1291 dtrace_status_t status[2];
1292 dtrace_optval_t opt;
1296 int done = 0, mode = 0;
1299 struct ps_prochandle *P;
1302 g_pname = basename(argv[0]);
1305 return (usage(stderr));
1307 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1308 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1309 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1310 fatal("failed to allocate memory for arguments");
1312 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1313 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1315 bzero(status, sizeof (status));
1316 bzero(&buf, sizeof (buf));
1319 * Make an initial pass through argv[] processing any arguments that
1320 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1321 * We also accumulate arguments that are not affiliated with getopt
1322 * options into g_argv[], and abort if any invalid options are found.
1324 for (optind = 1; optind < argc; optind++) {
1325 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1328 if (strcmp(optarg, "2") != 0) {
1329 (void) fprintf(stderr,
1330 "%s: illegal option -- 3%s\n",
1332 return (usage(stderr));
1334 g_oflags &= ~DTRACE_O_LP64;
1335 g_oflags |= DTRACE_O_ILP32;
1339 if (strcmp(optarg, "4") != 0) {
1340 (void) fprintf(stderr,
1341 "%s: illegal option -- 6%s\n",
1343 return (usage(stderr));
1345 g_oflags &= ~DTRACE_O_ILP32;
1346 g_oflags |= DTRACE_O_LP64;
1350 g_grabanon++; /* also checked in pass 2 below */
1354 g_mode = DMODE_ANON;
1365 g_mode = DMODE_HEADER;
1366 g_oflags |= DTRACE_O_NODEV;
1367 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1373 g_mode = DMODE_LINK;
1374 g_oflags |= DTRACE_O_NODEV;
1375 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1381 g_mode = DMODE_LIST;
1382 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1387 g_mode = DMODE_VERS;
1392 if (strchr(DTRACE_OPTSTR, c) == NULL)
1393 return (usage(stderr));
1398 g_argv[g_argc++] = argv[optind];
1402 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1403 "can be specified at a time\n", g_pname);
1407 if (g_mode == DMODE_VERS)
1408 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1411 * If we're in linker mode and the data model hasn't been specified,
1412 * we try to guess the appropriate setting by examining the object
1413 * files. We ignore certain errors since we'll catch them later when
1414 * we actually process the object files.
1416 if (g_mode == DMODE_LINK &&
1417 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1418 elf_version(EV_CURRENT) != EV_NONE) {
1423 for (i = 1; i < g_argc; i++) {
1424 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1427 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1432 if (elf_kind(elf) != ELF_K_ELF ||
1433 gelf_getehdr(elf, &ehdr) == NULL) {
1435 (void) elf_end(elf);
1440 (void) elf_end(elf);
1442 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1443 if (g_oflags & DTRACE_O_ILP32) {
1444 fatal("can't mix 32-bit and 64-bit "
1447 g_oflags |= DTRACE_O_LP64;
1448 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1449 if (g_oflags & DTRACE_O_LP64) {
1450 fatal("can't mix 32-bit and 64-bit "
1453 g_oflags |= DTRACE_O_ILP32;
1461 * Open libdtrace. If we are not actually going to be enabling any
1462 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1464 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1465 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1466 g_oflags |= DTRACE_O_NODEV;
1470 fatal("failed to initialize dtrace: %s\n",
1471 dtrace_errmsg(NULL, err));
1474 #if defined(__i386__)
1475 /* XXX The 32-bit seems to need more buffer space by default -sson */
1476 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1477 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1479 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1480 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1482 (void) dtrace_setopt(g_dtp, "temporal", "yes");
1485 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1486 * references to undefined symbols to remain as unresolved relocations.
1487 * If -A is specified, enable -xlink=primary to permit static linking
1488 * only to kernel symbols that are defined in a primary kernel module.
1490 if (g_mode == DMODE_LINK) {
1491 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1492 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1495 * Use the remaining arguments as the list of object files
1496 * when in linker mode.
1498 g_objc = g_argc - 1;
1499 g_objv = g_argv + 1;
1502 * We still use g_argv[0], the name of the executable.
1505 } else if (g_mode == DMODE_ANON)
1506 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1509 * Now that we have libdtrace open, make a second pass through argv[]
1510 * to perform any dtrace_setopt() calls and change any compiler flags.
1511 * We also accumulate any program specifications into our g_cmdv[] at
1512 * this time; these will compiled as part of the fourth processing pass.
1514 for (optind = 1; optind < argc; optind++) {
1515 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1518 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1519 dfatal("failed to set -a");
1523 if (dtrace_setopt(g_dtp,
1524 "bufsize", optarg) != 0)
1525 dfatal("failed to set -b %s", optarg);
1533 g_cflags |= DTRACE_C_CPP;
1537 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1538 dfatal("failed to set -D %s", optarg);
1542 dcp = &g_cmdv[g_cmdc++];
1543 dcp->dc_func = compile_str;
1544 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1545 dcp->dc_arg = optarg;
1549 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1550 dfatal("failed to set -F");
1554 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1555 dfatal("failed to set -H");
1559 dcp = &g_cmdv[g_cmdc++];
1560 dcp->dc_func = compile_str;
1561 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1562 dcp->dc_arg = optarg;
1566 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1567 dfatal("failed to set -I %s", optarg);
1571 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1572 dfatal("failed to set -L %s", optarg);
1576 dcp = &g_cmdv[g_cmdc++];
1577 dcp->dc_func = compile_str;
1578 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1579 dcp->dc_arg = optarg;
1583 dcp = &g_cmdv[g_cmdc++];
1584 dcp->dc_func = compile_str;
1585 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1586 dcp->dc_arg = optarg;
1590 dcp = &g_cmdv[g_cmdc++];
1591 dcp->dc_func = compile_str;
1592 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1593 dcp->dc_arg = optarg;
1597 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1598 dfatal("failed to set -q");
1606 dcp = &g_cmdv[g_cmdc++];
1607 dcp->dc_func = compile_file;
1608 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1609 dcp->dc_arg = optarg;
1613 g_cflags |= DTRACE_C_DIFV;
1617 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1618 dfatal("failed to set -U %s", optarg);
1626 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1627 dfatal("failed to set -w");
1631 if ((p = strchr(optarg, '=')) != NULL)
1634 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1635 dfatal("failed to set -x %s", optarg);
1639 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1640 dfatal("failed to set -X %s", optarg);
1644 g_cflags |= DTRACE_C_ZDEFS;
1648 if (strchr(DTRACE_OPTSTR, c) == NULL)
1649 return (usage(stderr));
1654 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1655 (void) fprintf(stderr, "%s: -B not valid in combination"
1656 " with [-AGl] options\n", g_pname);
1660 if (g_ofp == NULL && g_ofile != NULL) {
1661 (void) fprintf(stderr, "%s: -B not valid in combination"
1662 " with -o option\n", g_pname);
1667 * In our third pass we handle any command-line options related to
1668 * grabbing or creating victim processes. The behavior of these calls
1669 * may been affected by any library options set by the second pass.
1671 for (optind = 1; optind < argc; optind++) {
1672 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1675 if ((v = make_argv(optarg)) == NULL)
1676 fatal("failed to allocate memory");
1678 P = dtrace_proc_create(g_dtp, v[0], v, NULL, NULL);
1680 dfatal(NULL); /* dtrace_errmsg() only */
1688 pid = strtol(optarg, &p, 10);
1690 if (errno != 0 || p == optarg || p[0] != '\0')
1691 fatal("invalid pid: %s\n", optarg);
1693 P = dtrace_proc_grab(g_dtp, pid, 0);
1695 dfatal(NULL); /* dtrace_errmsg() only */
1704 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1705 * each string or file specification into a compiled program structure.
1707 for (i = 0; i < g_cmdc; i++)
1708 g_cmdv[i].dc_func(&g_cmdv[i]);
1710 if (g_mode != DMODE_LIST) {
1711 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1712 dfatal("failed to establish error handler");
1714 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1715 dfatal("failed to establish drop handler");
1717 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1718 dfatal("failed to establish proc handler");
1720 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1721 dfatal("failed to establish setopt handler");
1723 if (g_ofp == NULL &&
1724 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1725 dfatal("failed to establish buffered handler");
1728 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1729 g_flowindent = opt != DTRACEOPT_UNSET;
1731 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1732 g_grabanon = opt != DTRACEOPT_UNSET;
1734 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1735 g_quiet = opt != DTRACEOPT_UNSET;
1738 * Now make a fifth and final pass over the options that have been
1739 * turned into programs and saved in g_cmdv[], performing any mode-
1740 * specific processing. If g_mode is DMODE_EXEC, we will break out
1741 * of the switch() and continue on to the data processing loop. For
1742 * other modes, we will exit dtrace once mode-specific work is done.
1746 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1747 fatal("failed to open output file '%s'", g_ofile);
1749 for (i = 0; i < g_cmdc; i++)
1750 exec_prog(&g_cmdv[i]);
1752 if (done && !g_grabanon) {
1753 dtrace_close(g_dtp);
1759 if (g_ofile == NULL)
1761 g_ofile = "/kernel/drv/dtrace.conf";
1764 * On FreeBSD, anonymous DOF data is written to
1765 * the DTrace DOF file.
1767 g_ofile = "/boot/dtrace.dof";
1770 dof_prune(g_ofile); /* strip out any old DOF directives */
1772 etcsystem_prune(); /* string out any forceload directives */
1776 dtrace_close(g_dtp);
1780 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1781 fatal("failed to open output file '%s'", g_ofile);
1783 for (i = 0; i < g_cmdc; i++) {
1784 anon_prog(&g_cmdv[i],
1785 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1789 * Dump out the DOF corresponding to the error handler and the
1790 * current options as the final DOF property in the .conf file.
1792 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1793 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1795 if (fclose(g_ofp) == EOF)
1796 fatal("failed to close output file '%s'", g_ofile);
1799 * These messages would use notice() rather than error(), but
1800 * we don't want them suppressed when -A is run on a D program
1801 * that itself contains a #pragma D option quiet.
1803 error("saved anonymous enabling in %s\n", g_ofile);
1809 error("run update_drv(1M) or reboot to enable changes\n");
1812 dtrace_close(g_dtp);
1817 (void) fprintf(stderr, "%s: -G requires one or more "
1818 "scripts or enabling options\n", g_pname);
1819 dtrace_close(g_dtp);
1823 for (i = 0; i < g_cmdc; i++)
1824 link_prog(&g_cmdv[i]);
1826 if (g_cmdc > 1 && g_ofile != NULL) {
1827 char **objv = alloca(g_cmdc * sizeof (char *));
1829 for (i = 0; i < g_cmdc; i++)
1830 objv[i] = g_cmdv[i].dc_ofile;
1832 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1833 g_ofile, g_cmdc, objv) != 0)
1834 dfatal(NULL); /* dtrace_errmsg() only */
1837 dtrace_close(g_dtp);
1841 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1842 fatal("failed to open output file '%s'", g_ofile);
1846 oprintf("%5s %10s %17s %33s %s\n",
1847 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1849 for (i = 0; i < g_cmdc; i++)
1850 list_prog(&g_cmdv[i]);
1853 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1855 dtrace_close(g_dtp);
1860 (void) fprintf(stderr, "%s: -h requires one or more "
1861 "scripts or enabling options\n", g_pname);
1862 dtrace_close(g_dtp);
1866 if (g_ofile == NULL) {
1870 (void) fprintf(stderr, "%s: -h requires an "
1871 "output file if multiple scripts are "
1872 "specified\n", g_pname);
1873 dtrace_close(g_dtp);
1877 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1878 strcmp(p, ".d") != 0) {
1879 (void) fprintf(stderr, "%s: -h requires an "
1880 "output file if no scripts are "
1881 "specified\n", g_pname);
1882 dtrace_close(g_dtp);
1886 p[0] = '\0'; /* strip .d suffix */
1887 g_ofile = p = g_cmdv[0].dc_ofile;
1888 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1889 "%s.h", basename(g_cmdv[0].dc_arg));
1892 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1893 fatal("failed to open header file '%s'", g_ofile);
1895 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1897 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1898 fclose(g_ofp) == EOF)
1899 dfatal("failed to create header file %s", g_ofile);
1901 dtrace_close(g_dtp);
1906 * If -a and -Z were not specified and no probes have been matched, no
1907 * probe criteria was specified on the command line and we abort.
1909 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1910 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1913 * Start tracing. Once we dtrace_go(), reload any options that affect
1914 * our globals in case consuming anonymous state has changed them.
1918 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1919 g_flowindent = opt != DTRACEOPT_UNSET;
1921 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1922 g_grabanon = opt != DTRACEOPT_UNSET;
1924 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1925 g_quiet = opt != DTRACEOPT_UNSET;
1927 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1928 if (opt != DTRACEOPT_UNSET)
1929 notice("allowing destructive actions\n");
1934 * Now that tracing is active and we are ready to consume trace data,
1935 * continue any grabbed or created processes, setting them running
1936 * using the /proc control mechanism inside of libdtrace.
1938 for (i = 0; i < g_psc; i++)
1939 dtrace_proc_continue(g_dtp, g_psv[i]);
1941 g_pslive = g_psc; /* count for prochandler() */
1944 if (!g_intr && !done)
1945 dtrace_sleep(g_dtp);
1949 * Output a newline just to make the output look
1950 * slightly cleaner. Note that we do this even in
1957 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1959 if (dtrace_stop(g_dtp) == -1)
1960 dfatal("couldn't stop tracing");
1963 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1964 case DTRACE_WORKSTATUS_DONE:
1967 case DTRACE_WORKSTATUS_OKAY:
1970 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1971 dfatal("processing aborted");
1974 if (g_ofp != NULL && fflush(g_ofp) == EOF)
1981 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1982 dtrace_errno(g_dtp) != EINTR)
1983 dfatal("failed to print aggregations");
1986 dtrace_close(g_dtp);