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.
30 #include <sys/types.h>
53 typedef struct dtrace_cmd {
54 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
55 dtrace_probespec_t dc_spec; /* probe specifier context */
56 char *dc_arg; /* argument from main argv */
57 const char *dc_name; /* name for error messages */
58 const char *dc_desc; /* desc for error messages */
59 dtrace_prog_t *dc_prog; /* program compiled from arg */
60 char dc_ofile[PATH_MAX]; /* derived output file name */
63 #define DMODE_VERS 0 /* display version information and exit (-V) */
64 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
65 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
66 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
67 #define DMODE_LIST 4 /* compile program and list probes (-l) */
68 #define DMODE_HEADER 5 /* compile program for headergen (-h) */
74 static const char DTRACE_OPTSTR[] =
75 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
81 static dtrace_cmd_t *g_cmdv;
83 static struct ps_prochandle **g_psv;
88 static int g_flowindent;
90 static int g_impatient;
96 static int g_exec = 1;
97 static int g_mode = DMODE_EXEC;
98 static int g_status = E_SUCCESS;
99 static int g_grabanon = 0;
100 static const char *g_ofile = NULL;
102 static dtrace_hdl_t *g_dtp;
104 static char *g_etcfile = "/etc/system";
105 static const char *g_etcbegin = "* vvvv Added by DTrace";
106 static const char *g_etcend = "* ^^^^ Added by DTrace";
108 static const char *g_etc[] = {
110 "* The following forceload directives were added by dtrace(1M) to allow for",
111 "* tracing during boot. If these directives are removed, the system will",
112 "* continue to function, but tracing will not occur during boot as desired.",
113 "* To remove these directives (and this block comment) automatically, run",
114 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
115 "* chapter of the Solaris Dynamic Tracing Guide for details.",
123 static const char predact[] = "[[ predicate ] action ]";
125 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
126 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
127 "[-o output] [-p pid] [-s script] [-U name]\n\t"
128 "[-x opt[=val]] [-X a|c|s|t]\n\n"
129 "\t[-P provider %s]\n"
130 "\t[-m [ provider: ] module %s]\n"
131 "\t[-f [[ provider: ] module: ] func %s]\n"
132 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
133 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
134 predact, predact, predact, predact, predact);
136 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
137 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
139 (void) fprintf(fp, "\n"
140 "\t-32 generate 32-bit D programs and ELF files\n"
141 "\t-64 generate 64-bit D programs and ELF files\n\n"
142 "\t-a claim anonymous tracing state\n"
143 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
144 "\t-b set trace buffer size\n"
145 "\t-c run specified command and exit upon its completion\n"
146 "\t-C run cpp(1) preprocessor on script files\n"
147 "\t-D define symbol when invoking preprocessor\n"
148 "\t-e exit after compiling request but prior to enabling probes\n"
149 "\t-f enable or list probes matching the specified function name\n"
150 "\t-F coalesce trace output by function\n"
151 "\t-G generate an ELF file containing embedded dtrace program\n"
152 "\t-h generate a header file with definitions for static probes\n"
153 "\t-H print included files when invoking preprocessor\n"
154 "\t-i enable or list probes matching the specified probe id\n"
155 "\t-I add include directory to preprocessor search path\n"
156 "\t-l list probes matching specified criteria\n"
157 "\t-L add library directory to library search path\n"
158 "\t-m enable or list probes matching the specified module name\n"
159 "\t-n enable or list probes matching the specified probe name\n"
160 "\t-o set output file\n"
161 "\t-p grab specified process-ID and cache its symbol tables\n"
162 "\t-P enable or list probes matching the specified provider name\n"
163 "\t-q set quiet mode (only output explicitly traced data)\n"
164 "\t-s enable or list probes according to the specified D script\n"
165 "\t-S print D compiler intermediate code\n"
166 "\t-U undefine symbol when invoking preprocessor\n"
167 "\t-v set verbose mode (report stability attributes, arguments)\n"
168 "\t-V report DTrace API version\n"
169 "\t-w permit destructive actions\n"
170 "\t-x enable or modify compiler and tracing options\n"
171 "\t-X specify ISO C conformance settings for preprocessor\n"
172 "\t-Z permit probe descriptions that match zero probes\n");
178 verror(const char *fmt, va_list ap)
182 (void) fprintf(stderr, "%s: ", g_pname);
183 (void) vfprintf(stderr, fmt, ap);
185 if (fmt[strlen(fmt) - 1] != '\n')
186 (void) fprintf(stderr, ": %s\n", strerror(error));
191 fatal(const char *fmt, ...)
200 * Close the DTrace handle to ensure that any controlled processes are
201 * correctly restored and continued.
211 dfatal(const char *fmt, ...)
213 #if !defined(sun) && defined(NEED_ERRLOC)
214 char *p_errfile = NULL;
221 (void) fprintf(stderr, "%s: ", g_pname);
223 (void) vfprintf(stderr, fmt, ap);
227 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
228 (void) fprintf(stderr, ": %s\n",
229 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
230 } else if (fmt == NULL) {
231 (void) fprintf(stderr, "%s\n",
232 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
234 #if !defined(sun) && defined(NEED_ERRLOC)
235 dt_get_errloc(g_dtp, &p_errfile, &errline);
236 if (p_errfile != NULL)
237 printf("File '%s', line %d\n", p_errfile, errline);
241 * Close the DTrace handle to ensure that any controlled processes are
242 * correctly restored and continued.
251 error(const char *fmt, ...)
262 notice(const char *fmt, ...)
267 return; /* -q or quiet pragma suppresses notice()s */
276 oprintf(const char *fmt, ...)
285 n = vfprintf(g_ofp, fmt, ap);
289 if (errno != EINTR) {
290 fatal("failed to write to %s",
291 g_ofile ? g_ofile : "<stdout>");
300 const char *ws = "\f\n\r\t\v ";
301 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
308 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
319 dof_prune(const char *fname)
322 size_t sz, i, j, mark, len;
326 if ((fd = open(fname, O_RDONLY)) == -1) {
328 * This is okay only if the file doesn't exist at all.
331 fatal("failed to open %s", fname);
335 if (fstat(fd, &sbuf) == -1)
336 fatal("failed to fstat %s", fname);
338 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
339 fatal("failed to allocate memory for %s", fname);
341 if (read(fd, buf, sz) != sz)
342 fatal("failed to read %s", fname);
347 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
348 fatal("failed to open %s for writing", fname);
350 len = strlen("dof-data-");
352 for (mark = 0, i = 0; i < sz; i++) {
353 if (strncmp(&buf[i], "dof-data-", len) != 0)
357 * This is only a match if it's in the 0th column.
359 if (i != 0 && buf[i - 1] != '\n')
363 error("cleaned up old anonymous "
364 "enabling in %s\n", fname);
368 * We have a match. First write out our data up until now.
371 if (write(fd, &buf[mark], i - mark) != i - mark)
372 fatal("failed to write to %s", fname);
376 * Now scan forward until we scan past a newline.
378 for (j = i; j < sz && buf[j] != '\n'; j++)
384 if ((mark = j + 1) >= sz)
391 if (write(fd, &buf[mark], sz - mark) != sz - mark)
392 fatal("failed to write to %s", fname);
401 etcsystem_prune(void)
405 char *buf, *start, *end;
407 char *fname = g_etcfile, *tmpname;
409 if ((fd = open(fname, O_RDONLY)) == -1)
410 fatal("failed to open %s", fname);
412 if (fstat(fd, &sbuf) == -1)
413 fatal("failed to fstat %s", fname);
415 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
416 fatal("failed to allocate memory for %s", fname);
418 if (read(fd, buf, sz) != sz)
419 fatal("failed to read %s", fname);
424 if ((start = strstr(buf, g_etcbegin)) == NULL)
427 if (strlen(buf) != sz) {
428 fatal("embedded nul byte in %s; manual repair of %s "
429 "required\n", fname, fname);
432 if (strstr(start + 1, g_etcbegin) != NULL) {
433 fatal("multiple start sentinels in %s; manual repair of %s "
434 "required\n", fname, fname);
437 if ((end = strstr(buf, g_etcend)) == NULL) {
438 fatal("missing end sentinel in %s; manual repair of %s "
439 "required\n", fname, fname);
443 fatal("end sentinel preceeds start sentinel in %s; manual "
444 "repair of %s required\n", fname, fname);
447 end += strlen(g_etcend) + 1;
448 bcopy(end, start, strlen(end) + 1);
450 tmpname = alloca(sz = strlen(fname) + 80);
451 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
453 if ((fd = open(tmpname,
454 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
455 fatal("failed to create %s", tmpname);
457 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
458 (void) unlink(tmpname);
459 fatal("failed to write to %s", tmpname);
464 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
465 (void) unlink(tmpname);
466 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
467 (int)sbuf.st_uid, (int)sbuf.st_gid);
470 if (rename(tmpname, fname) == -1)
471 fatal("rename of %s to %s failed", tmpname, fname);
473 error("cleaned up forceload directives in %s\n", fname);
481 const char *mods[20];
484 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
485 fatal("failed to open output file '%s'", g_ofile);
487 oprintf("%s\n", g_etcbegin);
489 for (line = 0; g_etc[line] != NULL; line++)
490 oprintf("%s\n", g_etc[line]);
492 nmods = dtrace_provider_modules(g_dtp, mods,
493 sizeof (mods) / sizeof (char *) - 1);
495 if (nmods >= sizeof (mods) / sizeof (char *))
496 fatal("unexpectedly large number of modules!");
498 mods[nmods++] = "dtrace";
500 for (line = 0; line < nmods; line++)
501 oprintf("forceload: drv/%s\n", mods[line]);
503 oprintf("%s\n", g_etcend);
505 if (fclose(g_ofp) == EOF)
506 fatal("failed to close output file '%s'", g_ofile);
508 error("added forceload directives to %s\n", g_ofile);
513 print_probe_info(const dtrace_probeinfo_t *p)
518 oprintf("\n\tProbe Description Attributes\n");
520 oprintf("\t\tIdentifier Names: %s\n",
521 dtrace_stability_name(p->dtp_attr.dtat_name));
522 oprintf("\t\tData Semantics: %s\n",
523 dtrace_stability_name(p->dtp_attr.dtat_data));
524 oprintf("\t\tDependency Class: %s\n",
525 dtrace_class_name(p->dtp_attr.dtat_class));
527 oprintf("\n\tArgument Attributes\n");
529 oprintf("\t\tIdentifier Names: %s\n",
530 dtrace_stability_name(p->dtp_arga.dtat_name));
531 oprintf("\t\tData Semantics: %s\n",
532 dtrace_stability_name(p->dtp_arga.dtat_data));
533 oprintf("\t\tDependency Class: %s\n",
534 dtrace_class_name(p->dtp_arga.dtat_class));
536 oprintf("\n\tArgument Types\n");
538 for (i = 0; i < p->dtp_argc; i++) {
539 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
540 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
541 (void) strlcpy(buf, "(unknown)", sizeof (buf));
542 oprintf("\t\targs[%d]: %s\n", i, buf);
545 if (p->dtp_argc == 0)
546 oprintf("\t\tNone\n");
553 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
554 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
556 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
557 dtrace_probedesc_t *pdp = &edp->dted_probe;
558 dtrace_probeinfo_t p;
563 oprintf("\n%s:%s:%s:%s\n",
564 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
566 if (dtrace_probe_info(dtp, pdp, &p) == 0)
567 print_probe_info(&p);
574 * Execute the specified program by enabling the corresponding instrumentation.
575 * If -e has been specified, we get the program info but do not enable it. If
576 * -v has been specified, we print a stability report for the program.
579 exec_prog(const dtrace_cmd_t *dcp)
581 dtrace_ecbdesc_t *last = NULL;
582 dtrace_proginfo_t dpi;
585 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
586 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
587 dfatal("failed to enable '%s'", dcp->dc_name);
589 notice("%s '%s' matched %u probe%s\n",
590 dcp->dc_desc, dcp->dc_name,
591 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
595 oprintf("\nStability attributes for %s %s:\n",
596 dcp->dc_desc, dcp->dc_name);
598 oprintf("\n\tMinimum Probe Description Attributes\n");
599 oprintf("\t\tIdentifier Names: %s\n",
600 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
601 oprintf("\t\tData Semantics: %s\n",
602 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
603 oprintf("\t\tDependency Class: %s\n",
604 dtrace_class_name(dpi.dpi_descattr.dtat_class));
606 oprintf("\n\tMinimum Statement Attributes\n");
608 oprintf("\t\tIdentifier Names: %s\n",
609 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
610 oprintf("\t\tData Semantics: %s\n",
611 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
612 oprintf("\t\tDependency Class: %s\n",
613 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
616 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
617 (dtrace_stmt_f *)info_stmt, &last);
622 g_total += dpi.dpi_matches;
626 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
627 * storing in a driver.conf(4) file associated with the dtrace driver.
630 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
632 const uchar_t *p, *q;
635 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
638 q = p + dof->dofh_loadsz;
641 oprintf("dof-data-%d=0x%x", n, *p++);
644 oprintf(",0x%x", *p++);
649 * On FreeBSD, the DOF data is handled as a kernel environment (kenv)
650 * string. We use two hex characters per DOF byte.
652 oprintf("dof-data-%d=%02x", n, *p++);
655 oprintf("%02x", *p++);
660 dtrace_dof_destroy(g_dtp, dof);
664 * Link the specified D program in DOF form into an ELF file for use in either
665 * helpers, userland provider definitions, or both. If -o was specified, that
666 * path is used as the output file name. If -o wasn't specified and the input
667 * program is from a script whose name is %.d, use basename(%.o) as the output
668 * file name. Otherwise we use "d.out" as the default output file name.
671 link_prog(dtrace_cmd_t *dcp)
675 if (g_cmdc == 1 && g_ofile != NULL) {
676 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
677 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
678 strcmp(p, ".d") == 0) {
679 p[0] = '\0'; /* strip .d suffix */
680 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
681 "%s.o", basename(dcp->dc_arg));
682 } else if (g_cmdc > 1) {
683 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
684 "d.out.%td", dcp - g_cmdv);
686 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
690 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
691 dcp->dc_ofile, g_objc, g_objv) != 0)
692 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
697 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
699 dtrace_probeinfo_t p;
701 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
702 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
704 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
705 print_probe_info(&p);
712 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
713 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
715 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
720 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
721 error("failed to match %s:%s:%s:%s: %s\n",
722 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
723 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
724 dtrace_errmsg(dtp, dtrace_errno(dtp)));
732 * List the probes corresponding to the specified program by iterating over
733 * each statement and then matching probes to the statement probe descriptions.
736 list_prog(const dtrace_cmd_t *dcp)
738 dtrace_ecbdesc_t *last = NULL;
740 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
741 (dtrace_stmt_f *)list_stmt, &last);
745 compile_file(dtrace_cmd_t *dcp)
750 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
751 fatal("failed to open %s", dcp->dc_arg);
754 g_argv[0] = dcp->dc_arg;
756 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
757 g_cflags, g_argc, g_argv)) == NULL)
758 dfatal("failed to compile script %s", dcp->dc_arg);
763 dcp->dc_desc = "script";
764 dcp->dc_name = dcp->dc_arg;
768 compile_str(dtrace_cmd_t *dcp)
772 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
773 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
774 dfatal("invalid probe specifier %s", dcp->dc_arg);
776 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
777 *p = '\0'; /* crop name for reporting */
779 dcp->dc_desc = "description";
780 dcp->dc_name = dcp->dc_arg;
785 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
788 const psinfo_t *prp = Ppsinfo(P);
789 int pid = Pstatus(P)->pr_pid;
790 char name[SIG2STR_MAX];
792 int wstatus = proc_getwstat(P);
793 int pid = proc_getpid(P);
797 notice("pid %d: %s\n", pid, msg);
804 switch (proc_state(P)) {
809 * Ideally we would like to always report pr_wstat here, but it
810 * isn't possible given current /proc semantics. If we grabbed
811 * the process, Ppsinfo() will either fail or return a zeroed
812 * psinfo_t depending on how far the parent is in reaping it.
813 * When /proc provides a stable pr_wstat in the status file,
814 * this code can be improved by examining this new pr_wstat.
816 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
817 notice("pid %d terminated by %s\n", pid,
818 proc_signame(WTERMSIG(prp->pr_wstat),
819 name, sizeof (name)));
821 if (WIFSIGNALED(wstatus)) {
822 notice("pid %d terminated by %d\n", pid,
826 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
827 notice("pid %d exited with status %d\n",
828 pid, WEXITSTATUS(prp->pr_wstat));
830 } else if (WEXITSTATUS(wstatus) != 0) {
831 notice("pid %d exited with status %d\n",
832 pid, WEXITSTATUS(wstatus));
835 notice("pid %d has exited\n", pid);
841 notice("pid %d exec'd a set-id or unobservable program\n", pid);
849 errhandler(const dtrace_errdata_t *data, void *arg)
851 error(data->dteda_msg);
852 return (DTRACE_HANDLE_OK);
857 drophandler(const dtrace_dropdata_t *data, void *arg)
859 error(data->dtdda_msg);
860 return (DTRACE_HANDLE_OK);
865 setopthandler(const dtrace_setoptdata_t *data, void *arg)
867 if (strcmp(data->dtsda_option, "quiet") == 0)
868 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
870 if (strcmp(data->dtsda_option, "flowindent") == 0)
871 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
873 return (DTRACE_HANDLE_OK);
876 #define BUFDUMPHDR(hdr) \
877 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
879 #define BUFDUMPSTR(ptr, field) \
880 (void) printf("%s: %20s => ", g_pname, #field); \
881 if ((ptr)->field != NULL) { \
882 const char *c = (ptr)->field; \
883 (void) printf("\""); \
886 (void) printf("\\n"); \
890 (void) printf("%c", *c); \
891 } while (*c++ != '\0'); \
892 (void) printf("\"\n"); \
894 (void) printf("<NULL>\n"); \
897 #define BUFDUMPASSTR(ptr, field, str) \
898 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
900 #define BUFDUMP(ptr, field) \
901 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
902 (long long)(ptr)->field);
904 #define BUFDUMPPTR(ptr, field) \
905 (void) printf("%s: %20s => %s\n", g_pname, #field, \
906 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
910 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
912 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
913 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
914 const dtrace_probedesc_t *pd;
915 uint32_t flags = bufdata->dtbda_flags;
916 char buf[512], *c = buf, *end = c + sizeof (buf);
923 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
924 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
925 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
926 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
927 { "???", UINT32_MAX },
931 if (bufdata->dtbda_probe != NULL) {
932 pd = bufdata->dtbda_probe->dtpda_pdesc;
933 } else if (agg != NULL) {
934 pd = agg->dtada_pdesc;
939 BUFDUMPHDR(">>> Called buffer handler");
942 BUFDUMPHDR(" dtrace_bufdata");
943 BUFDUMPSTR(bufdata, dtbda_buffered);
944 BUFDUMPPTR(bufdata, dtbda_probe);
945 BUFDUMPPTR(bufdata, dtbda_aggdata);
946 BUFDUMPPTR(bufdata, dtbda_recdesc);
948 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
951 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
952 if (!(flags & flagnames[i].value))
955 (void) snprintf(c, end - c,
956 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
958 flags &= ~flagnames[i].value;
962 (void) snprintf(c, end - c, ")");
964 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
968 BUFDUMPHDR(" dtrace_probedesc");
969 BUFDUMPSTR(pd, dtpd_provider);
970 BUFDUMPSTR(pd, dtpd_mod);
971 BUFDUMPSTR(pd, dtpd_func);
972 BUFDUMPSTR(pd, dtpd_name);
977 BUFDUMPHDR(" dtrace_recdesc");
978 BUFDUMP(rec, dtrd_action);
979 BUFDUMP(rec, dtrd_size);
983 int lim = rec->dtrd_size;
985 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
986 c = buf + strlen(buf);
988 if (lim > sizeof (uint64_t))
989 lim = sizeof (uint64_t);
991 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
993 for (i = 0; i < lim; i++) {
994 (void) snprintf(c, end - c, "%s%02x",
995 i == 0 ? "" : " ", *data++);
999 (void) snprintf(c, end - c,
1000 "%s)", lim < rec->dtrd_size ? " ..." : "");
1001 BUFDUMPASSTR(rec, dtrd_offset, buf);
1003 BUFDUMP(rec, dtrd_offset);
1010 dtrace_aggdesc_t *desc = agg->dtada_desc;
1012 BUFDUMPHDR(" dtrace_aggdesc");
1013 BUFDUMPSTR(desc, dtagd_name);
1014 BUFDUMP(desc, dtagd_varid);
1015 BUFDUMP(desc, dtagd_id);
1016 BUFDUMP(desc, dtagd_nrecs);
1020 return (DTRACE_HANDLE_OK);
1025 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1027 dtrace_actkind_t act;
1032 * We have processed the final record; output the newline if
1033 * we're not in quiet mode.
1038 return (DTRACE_CONSUME_NEXT);
1041 act = rec->dtrd_action;
1042 addr = (uintptr_t)data->dtpda_data;
1044 if (act == DTRACEACT_EXIT) {
1045 g_status = *((uint32_t *)addr);
1046 return (DTRACE_CONSUME_NEXT);
1049 return (DTRACE_CONSUME_THIS);
1054 chew(const dtrace_probedata_t *data, void *arg)
1056 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1057 processorid_t cpu = data->dtpda_cpu;
1062 return (DTRACE_CONSUME_ABORT);
1066 if (!g_flowindent) {
1068 oprintf("%3s %6s %32s\n",
1069 "CPU", "ID", "FUNCTION:NAME");
1072 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1077 if (!g_flowindent) {
1079 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1081 (void) snprintf(name, sizeof (name), "%s:%s",
1082 pd->dtpd_func, pd->dtpd_name);
1084 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1087 int indent = data->dtpda_indent;
1091 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1092 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1094 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1095 data->dtpda_prefix, pd->dtpd_func,
1098 len = indent + DTRACE_FUNCNAMELEN + 5;
1100 (void) snprintf(name, len, "%*s%s%s", indent, "",
1101 data->dtpda_prefix, pd->dtpd_func);
1104 oprintf("%3d %-41s ", cpu, name);
1107 return (DTRACE_CONSUME_THIS);
1118 dtrace_optval_t val;
1120 { "buffer size", "bufsize" },
1121 { "aggregation size", "aggsize" },
1122 { "speculation size", "specsize" },
1123 { "dynamic variable size", "dynvarsize" },
1126 { "cleaning rate", "cleanrate" },
1127 { "status rate", "statusrate" },
1131 for (i = 0; bufs[i].name != NULL; i++) {
1132 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1133 fatal("couldn't get option %s", bufs[i].optname);
1136 for (i = 0; rates[i].name != NULL; i++) {
1137 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1138 fatal("couldn't get option %s", rates[i].optname);
1141 if (dtrace_go(g_dtp) == -1)
1142 dfatal("could not enable tracing");
1144 for (i = 0; bufs[i].name != NULL; i++) {
1145 dtrace_optval_t j = 0, mul = 10;
1146 dtrace_optval_t nsize;
1148 if (bufs[i].val == DTRACEOPT_UNSET)
1151 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1153 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1156 if (nsize >= bufs[i].val - sizeof (uint64_t))
1159 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1162 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1163 error("%s lowered to %lld%c\n", bufs[i].name,
1164 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1166 error("%s lowered to %lld bytes\n", bufs[i].name,
1171 for (i = 0; rates[i].name != NULL; i++) {
1172 dtrace_optval_t nval;
1175 if (rates[i].val == DTRACEOPT_UNSET)
1178 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1180 if (nval == DTRACEOPT_UNSET || nval == 0)
1183 if (rates[i].val == nval)
1186 dir = nval > rates[i].val ? "reduced" : "increased";
1188 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1189 error("%s %s to %lld hz\n", rates[i].name, dir,
1190 (long long)NANOSEC / (long long)nval);
1194 if ((nval % NANOSEC) == 0) {
1195 error("%s %s to once every %lld seconds\n",
1197 (long long)nval / (long long)NANOSEC);
1201 error("%s %s to once every %lld nanoseconds\n",
1202 rates[i].name, dir, (long long)nval);
1218 main(int argc, char *argv[])
1220 dtrace_bufdesc_t buf;
1221 struct sigaction act, oact;
1222 dtrace_status_t status[2];
1223 dtrace_optval_t opt;
1227 int done = 0, mode = 0;
1230 struct ps_prochandle *P;
1233 g_pname = basename(argv[0]);
1236 return (usage(stderr));
1238 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1239 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1240 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1241 fatal("failed to allocate memory for arguments");
1243 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1244 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1246 bzero(status, sizeof (status));
1247 bzero(&buf, sizeof (buf));
1250 * Make an initial pass through argv[] processing any arguments that
1251 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1252 * We also accumulate arguments that are not affiliated with getopt
1253 * options into g_argv[], and abort if any invalid options are found.
1255 for (optind = 1; optind < argc; optind++) {
1256 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1259 if (strcmp(optarg, "2") != 0) {
1260 (void) fprintf(stderr,
1261 "%s: illegal option -- 3%s\n",
1263 return (usage(stderr));
1265 g_oflags &= ~DTRACE_O_LP64;
1266 g_oflags |= DTRACE_O_ILP32;
1270 if (strcmp(optarg, "4") != 0) {
1271 (void) fprintf(stderr,
1272 "%s: illegal option -- 6%s\n",
1274 return (usage(stderr));
1276 g_oflags &= ~DTRACE_O_ILP32;
1277 g_oflags |= DTRACE_O_LP64;
1281 g_grabanon++; /* also checked in pass 2 below */
1285 g_mode = DMODE_ANON;
1296 g_mode = DMODE_HEADER;
1297 g_oflags |= DTRACE_O_NODEV;
1298 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1304 g_mode = DMODE_LINK;
1305 g_oflags |= DTRACE_O_NODEV;
1306 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1312 g_mode = DMODE_LIST;
1313 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1318 g_mode = DMODE_VERS;
1323 if (strchr(DTRACE_OPTSTR, c) == NULL)
1324 return (usage(stderr));
1329 g_argv[g_argc++] = argv[optind];
1333 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1334 "can be specified at a time\n", g_pname);
1338 if (g_mode == DMODE_VERS)
1339 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1342 * If we're in linker mode and the data model hasn't been specified,
1343 * we try to guess the appropriate setting by examining the object
1344 * files. We ignore certain errors since we'll catch them later when
1345 * we actually process the object files.
1347 if (g_mode == DMODE_LINK &&
1348 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1349 elf_version(EV_CURRENT) != EV_NONE) {
1354 for (i = 1; i < g_argc; i++) {
1355 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1358 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1363 if (elf_kind(elf) != ELF_K_ELF ||
1364 gelf_getehdr(elf, &ehdr) == NULL) {
1366 (void) elf_end(elf);
1371 (void) elf_end(elf);
1373 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1374 if (g_oflags & DTRACE_O_ILP32) {
1375 fatal("can't mix 32-bit and 64-bit "
1378 g_oflags |= DTRACE_O_LP64;
1379 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1380 if (g_oflags & DTRACE_O_LP64) {
1381 fatal("can't mix 32-bit and 64-bit "
1384 g_oflags |= DTRACE_O_ILP32;
1392 * Open libdtrace. If we are not actually going to be enabling any
1393 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1395 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1396 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1397 g_oflags |= DTRACE_O_NODEV;
1401 fatal("failed to initialize dtrace: %s\n",
1402 dtrace_errmsg(NULL, err));
1405 #if defined(__i386__)
1406 /* XXX The 32-bit seems to need more buffer space by default -sson */
1407 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1408 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1410 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1411 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1413 (void) dtrace_setopt(g_dtp, "temporal", "yes");
1416 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1417 * references to undefined symbols to remain as unresolved relocations.
1418 * If -A is specified, enable -xlink=primary to permit static linking
1419 * only to kernel symbols that are defined in a primary kernel module.
1421 if (g_mode == DMODE_LINK) {
1422 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1423 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1426 * Use the remaining arguments as the list of object files
1427 * when in linker mode.
1429 g_objc = g_argc - 1;
1430 g_objv = g_argv + 1;
1433 * We still use g_argv[0], the name of the executable.
1436 } else if (g_mode == DMODE_ANON)
1437 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1440 * Now that we have libdtrace open, make a second pass through argv[]
1441 * to perform any dtrace_setopt() calls and change any compiler flags.
1442 * We also accumulate any program specifications into our g_cmdv[] at
1443 * this time; these will compiled as part of the fourth processing pass.
1445 for (optind = 1; optind < argc; optind++) {
1446 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1449 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1450 dfatal("failed to set -a");
1454 if (dtrace_setopt(g_dtp,
1455 "bufsize", optarg) != 0)
1456 dfatal("failed to set -b %s", optarg);
1464 g_cflags |= DTRACE_C_CPP;
1468 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1469 dfatal("failed to set -D %s", optarg);
1473 dcp = &g_cmdv[g_cmdc++];
1474 dcp->dc_func = compile_str;
1475 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1476 dcp->dc_arg = optarg;
1480 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1481 dfatal("failed to set -F");
1485 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1486 dfatal("failed to set -H");
1490 dcp = &g_cmdv[g_cmdc++];
1491 dcp->dc_func = compile_str;
1492 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1493 dcp->dc_arg = optarg;
1497 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1498 dfatal("failed to set -I %s", optarg);
1502 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1503 dfatal("failed to set -L %s", optarg);
1507 dcp = &g_cmdv[g_cmdc++];
1508 dcp->dc_func = compile_str;
1509 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1510 dcp->dc_arg = optarg;
1514 dcp = &g_cmdv[g_cmdc++];
1515 dcp->dc_func = compile_str;
1516 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1517 dcp->dc_arg = optarg;
1521 dcp = &g_cmdv[g_cmdc++];
1522 dcp->dc_func = compile_str;
1523 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1524 dcp->dc_arg = optarg;
1528 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1529 dfatal("failed to set -q");
1537 dcp = &g_cmdv[g_cmdc++];
1538 dcp->dc_func = compile_file;
1539 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1540 dcp->dc_arg = optarg;
1544 g_cflags |= DTRACE_C_DIFV;
1548 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1549 dfatal("failed to set -U %s", optarg);
1557 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1558 dfatal("failed to set -w");
1562 if ((p = strchr(optarg, '=')) != NULL)
1565 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1566 dfatal("failed to set -x %s", optarg);
1570 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1571 dfatal("failed to set -X %s", optarg);
1575 g_cflags |= DTRACE_C_ZDEFS;
1579 if (strchr(DTRACE_OPTSTR, c) == NULL)
1580 return (usage(stderr));
1585 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1586 (void) fprintf(stderr, "%s: -B not valid in combination"
1587 " with [-AGl] options\n", g_pname);
1591 if (g_ofp == NULL && g_ofile != NULL) {
1592 (void) fprintf(stderr, "%s: -B not valid in combination"
1593 " with -o option\n", g_pname);
1598 * In our third pass we handle any command-line options related to
1599 * grabbing or creating victim processes. The behavior of these calls
1600 * may been affected by any library options set by the second pass.
1602 for (optind = 1; optind < argc; optind++) {
1603 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1606 if ((v = make_argv(optarg)) == NULL)
1607 fatal("failed to allocate memory");
1609 P = dtrace_proc_create(g_dtp, v[0], v, NULL, NULL);
1611 dfatal(NULL); /* dtrace_errmsg() only */
1619 pid = strtol(optarg, &p, 10);
1621 if (errno != 0 || p == optarg || p[0] != '\0')
1622 fatal("invalid pid: %s\n", optarg);
1624 P = dtrace_proc_grab(g_dtp, pid, 0);
1626 dfatal(NULL); /* dtrace_errmsg() only */
1635 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1636 * each string or file specification into a compiled program structure.
1638 for (i = 0; i < g_cmdc; i++)
1639 g_cmdv[i].dc_func(&g_cmdv[i]);
1641 if (g_mode != DMODE_LIST) {
1642 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1643 dfatal("failed to establish error handler");
1645 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1646 dfatal("failed to establish drop handler");
1648 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1649 dfatal("failed to establish proc handler");
1651 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1652 dfatal("failed to establish setopt handler");
1654 if (g_ofp == NULL &&
1655 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1656 dfatal("failed to establish buffered handler");
1659 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1660 g_flowindent = opt != DTRACEOPT_UNSET;
1662 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1663 g_grabanon = opt != DTRACEOPT_UNSET;
1665 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1666 g_quiet = opt != DTRACEOPT_UNSET;
1669 * Now make a fifth and final pass over the options that have been
1670 * turned into programs and saved in g_cmdv[], performing any mode-
1671 * specific processing. If g_mode is DMODE_EXEC, we will break out
1672 * of the switch() and continue on to the data processing loop. For
1673 * other modes, we will exit dtrace once mode-specific work is done.
1677 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1678 fatal("failed to open output file '%s'", g_ofile);
1680 for (i = 0; i < g_cmdc; i++)
1681 exec_prog(&g_cmdv[i]);
1683 if (done && !g_grabanon) {
1684 dtrace_close(g_dtp);
1690 if (g_ofile == NULL)
1692 g_ofile = "/kernel/drv/dtrace.conf";
1695 * On FreeBSD, anonymous DOF data is written to
1696 * the DTrace DOF file that the boot loader will
1697 * read if booting with the DTrace option.
1699 g_ofile = "/boot/dtrace.dof";
1702 dof_prune(g_ofile); /* strip out any old DOF directives */
1704 etcsystem_prune(); /* string out any forceload directives */
1708 dtrace_close(g_dtp);
1712 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1713 fatal("failed to open output file '%s'", g_ofile);
1715 for (i = 0; i < g_cmdc; i++) {
1716 anon_prog(&g_cmdv[i],
1717 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1721 * Dump out the DOF corresponding to the error handler and the
1722 * current options as the final DOF property in the .conf file.
1724 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1725 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1727 if (fclose(g_ofp) == EOF)
1728 fatal("failed to close output file '%s'", g_ofile);
1731 * These messages would use notice() rather than error(), but
1732 * we don't want them suppressed when -A is run on a D program
1733 * that itself contains a #pragma D option quiet.
1735 error("saved anonymous enabling in %s\n", g_ofile);
1738 error("run update_drv(1M) or reboot to enable changes\n");
1741 dtrace_close(g_dtp);
1746 (void) fprintf(stderr, "%s: -G requires one or more "
1747 "scripts or enabling options\n", g_pname);
1748 dtrace_close(g_dtp);
1752 for (i = 0; i < g_cmdc; i++)
1753 link_prog(&g_cmdv[i]);
1755 if (g_cmdc > 1 && g_ofile != NULL) {
1756 char **objv = alloca(g_cmdc * sizeof (char *));
1758 for (i = 0; i < g_cmdc; i++)
1759 objv[i] = g_cmdv[i].dc_ofile;
1761 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1762 g_ofile, g_cmdc, objv) != 0)
1763 dfatal(NULL); /* dtrace_errmsg() only */
1766 dtrace_close(g_dtp);
1770 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1771 fatal("failed to open output file '%s'", g_ofile);
1773 oprintf("%5s %10s %17s %33s %s\n",
1774 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1776 for (i = 0; i < g_cmdc; i++)
1777 list_prog(&g_cmdv[i]);
1780 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1782 dtrace_close(g_dtp);
1787 (void) fprintf(stderr, "%s: -h requires one or more "
1788 "scripts or enabling options\n", g_pname);
1789 dtrace_close(g_dtp);
1793 if (g_ofile == NULL) {
1797 (void) fprintf(stderr, "%s: -h requires an "
1798 "output file if multiple scripts are "
1799 "specified\n", g_pname);
1800 dtrace_close(g_dtp);
1804 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1805 strcmp(p, ".d") != 0) {
1806 (void) fprintf(stderr, "%s: -h requires an "
1807 "output file if no scripts are "
1808 "specified\n", g_pname);
1809 dtrace_close(g_dtp);
1813 p[0] = '\0'; /* strip .d suffix */
1814 g_ofile = p = g_cmdv[0].dc_ofile;
1815 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1816 "%s.h", basename(g_cmdv[0].dc_arg));
1819 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1820 fatal("failed to open header file '%s'", g_ofile);
1822 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1824 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1825 fclose(g_ofp) == EOF)
1826 dfatal("failed to create header file %s", g_ofile);
1828 dtrace_close(g_dtp);
1833 * If -a and -Z were not specified and no probes have been matched, no
1834 * probe criteria was specified on the command line and we abort.
1836 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1837 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1840 * Start tracing. Once we dtrace_go(), reload any options that affect
1841 * our globals in case consuming anonymous state has changed them.
1845 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1846 g_flowindent = opt != DTRACEOPT_UNSET;
1848 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1849 g_grabanon = opt != DTRACEOPT_UNSET;
1851 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1852 g_quiet = opt != DTRACEOPT_UNSET;
1854 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1855 if (opt != DTRACEOPT_UNSET)
1856 notice("allowing destructive actions\n");
1858 (void) sigemptyset(&act.sa_mask);
1860 act.sa_handler = intr;
1862 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1863 (void) sigaction(SIGINT, &act, NULL);
1865 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1866 (void) sigaction(SIGTERM, &act, NULL);
1869 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1870 (void) sigaction(SIGUSR1, &act, NULL);
1874 * Now that tracing is active and we are ready to consume trace data,
1875 * continue any grabbed or created processes, setting them running
1876 * using the /proc control mechanism inside of libdtrace.
1878 for (i = 0; i < g_psc; i++)
1879 dtrace_proc_continue(g_dtp, g_psv[i]);
1881 g_pslive = g_psc; /* count for prochandler() */
1884 if (!g_intr && !done)
1885 dtrace_sleep(g_dtp);
1889 * Output a newline just to make the output look
1890 * slightly cleaner. Note that we do this even in
1897 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1899 if (dtrace_stop(g_dtp) == -1)
1900 dfatal("couldn't stop tracing");
1903 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1904 case DTRACE_WORKSTATUS_DONE:
1907 case DTRACE_WORKSTATUS_OKAY:
1910 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1911 dfatal("processing aborted");
1914 if (g_ofp != NULL && fflush(g_ofp) == EOF)
1921 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1922 dtrace_errno(g_dtp) != EINTR)
1923 dfatal("failed to print aggregations");
1926 dtrace_close(g_dtp);