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 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
52 typedef struct dtrace_cmd {
53 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
54 dtrace_probespec_t dc_spec; /* probe specifier context */
55 char *dc_arg; /* argument from main argv */
56 const char *dc_name; /* name for error messages */
57 const char *dc_desc; /* desc for error messages */
58 dtrace_prog_t *dc_prog; /* program compiled from arg */
59 char dc_ofile[PATH_MAX]; /* derived output file name */
62 #define DMODE_VERS 0 /* display version information and exit (-V) */
63 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
64 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
65 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
66 #define DMODE_LIST 4 /* compile program and list probes (-l) */
67 #define DMODE_HEADER 5 /* compile program for headergen (-h) */
73 static const char DTRACE_OPTSTR[] =
74 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
80 static dtrace_cmd_t *g_cmdv;
82 static struct ps_prochandle **g_psv;
87 static int g_flowindent;
89 static int g_impatient;
95 static int g_exec = 1;
96 static int g_mode = DMODE_EXEC;
97 static int g_status = E_SUCCESS;
98 static int g_grabanon = 0;
99 static const char *g_ofile = NULL;
101 static dtrace_hdl_t *g_dtp;
103 static char *g_etcfile = "/etc/system";
104 static const char *g_etcbegin = "* vvvv Added by DTrace";
105 static const char *g_etcend = "* ^^^^ Added by DTrace";
107 static const char *g_etc[] = {
109 "* The following forceload directives were added by dtrace(1M) to allow for",
110 "* tracing during boot. If these directives are removed, the system will",
111 "* continue to function, but tracing will not occur during boot as desired.",
112 "* To remove these directives (and this block comment) automatically, run",
113 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
114 "* chapter of the Solaris Dynamic Tracing Guide for details.",
122 static const char predact[] = "[[ predicate ] action ]";
124 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
125 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
126 "[-o output] [-p pid] [-s script] [-U name]\n\t"
127 "[-x opt[=val]] [-X a|c|s|t]\n\n"
128 "\t[-P provider %s]\n"
129 "\t[-m [ provider: ] module %s]\n"
130 "\t[-f [[ provider: ] module: ] func %s]\n"
131 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
132 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
133 predact, predact, predact, predact, predact);
135 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
136 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
138 (void) fprintf(fp, "\n"
139 "\t-32 generate 32-bit D programs and ELF files\n"
140 "\t-64 generate 64-bit D programs and ELF files\n\n"
141 "\t-a claim anonymous tracing state\n"
142 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
143 "\t-b set trace buffer size\n"
144 "\t-c run specified command and exit upon its completion\n"
145 "\t-C run cpp(1) preprocessor on script files\n"
146 "\t-D define symbol when invoking preprocessor\n"
147 "\t-e exit after compiling request but prior to enabling probes\n"
148 "\t-f enable or list probes matching the specified function name\n"
149 "\t-F coalesce trace output by function\n"
150 "\t-G generate an ELF file containing embedded dtrace program\n"
151 "\t-h generate a header file with definitions for static probes\n"
152 "\t-H print included files when invoking preprocessor\n"
153 "\t-i enable or list probes matching the specified probe id\n"
154 "\t-I add include directory to preprocessor search path\n"
155 "\t-l list probes matching specified criteria\n"
156 "\t-L add library directory to library search path\n"
157 "\t-m enable or list probes matching the specified module name\n"
158 "\t-n enable or list probes matching the specified probe name\n"
159 "\t-o set output file\n"
160 "\t-p grab specified process-ID and cache its symbol tables\n"
161 "\t-P enable or list probes matching the specified provider name\n"
162 "\t-q set quiet mode (only output explicitly traced data)\n"
163 "\t-s enable or list probes according to the specified D script\n"
164 "\t-S print D compiler intermediate code\n"
165 "\t-U undefine symbol when invoking preprocessor\n"
166 "\t-v set verbose mode (report stability attributes, arguments)\n"
167 "\t-V report DTrace API version\n"
168 "\t-w permit destructive actions\n"
169 "\t-x enable or modify compiler and tracing options\n"
170 "\t-X specify ISO C conformance settings for preprocessor\n"
171 "\t-Z permit probe descriptions that match zero probes\n");
177 verror(const char *fmt, va_list ap)
181 (void) fprintf(stderr, "%s: ", g_pname);
182 (void) vfprintf(stderr, fmt, ap);
184 if (fmt[strlen(fmt) - 1] != '\n')
185 (void) fprintf(stderr, ": %s\n", strerror(error));
190 fatal(const char *fmt, ...)
199 * Close the DTrace handle to ensure that any controlled processes are
200 * correctly restored and continued.
210 dfatal(const char *fmt, ...)
212 #if !defined(sun) && defined(NEED_ERRLOC)
213 char *p_errfile = NULL;
220 (void) fprintf(stderr, "%s: ", g_pname);
222 (void) vfprintf(stderr, fmt, ap);
226 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
227 (void) fprintf(stderr, ": %s\n",
228 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
229 } else if (fmt == NULL) {
230 (void) fprintf(stderr, "%s\n",
231 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
233 #if !defined(sun) && defined(NEED_ERRLOC)
234 dt_get_errloc(g_dtp, &p_errfile, &errline);
235 if (p_errfile != NULL)
236 printf("File '%s', line %d\n", p_errfile, errline);
240 * Close the DTrace handle to ensure that any controlled processes are
241 * correctly restored and continued.
250 error(const char *fmt, ...)
261 notice(const char *fmt, ...)
266 return; /* -q or quiet pragma suppresses notice()s */
275 oprintf(const char *fmt, ...)
284 n = vfprintf(g_ofp, fmt, ap);
288 if (errno != EINTR) {
289 fatal("failed to write to %s",
290 g_ofile ? g_ofile : "<stdout>");
299 const char *ws = "\f\n\r\t\v ";
300 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
307 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
318 dof_prune(const char *fname)
321 size_t sz, i, j, mark, len;
325 if ((fd = open(fname, O_RDONLY)) == -1) {
327 * This is okay only if the file doesn't exist at all.
330 fatal("failed to open %s", fname);
334 if (fstat(fd, &sbuf) == -1)
335 fatal("failed to fstat %s", fname);
337 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
338 fatal("failed to allocate memory for %s", fname);
340 if (read(fd, buf, sz) != sz)
341 fatal("failed to read %s", fname);
346 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
347 fatal("failed to open %s for writing", fname);
349 len = strlen("dof-data-");
351 for (mark = 0, i = 0; i < sz; i++) {
352 if (strncmp(&buf[i], "dof-data-", len) != 0)
356 * This is only a match if it's in the 0th column.
358 if (i != 0 && buf[i - 1] != '\n')
362 error("cleaned up old anonymous "
363 "enabling in %s\n", fname);
367 * We have a match. First write out our data up until now.
370 if (write(fd, &buf[mark], i - mark) != i - mark)
371 fatal("failed to write to %s", fname);
375 * Now scan forward until we scan past a newline.
377 for (j = i; j < sz && buf[j] != '\n'; j++)
383 if ((mark = j + 1) >= sz)
390 if (write(fd, &buf[mark], sz - mark) != sz - mark)
391 fatal("failed to write to %s", fname);
400 etcsystem_prune(void)
404 char *buf, *start, *end;
406 char *fname = g_etcfile, *tmpname;
408 if ((fd = open(fname, O_RDONLY)) == -1)
409 fatal("failed to open %s", fname);
411 if (fstat(fd, &sbuf) == -1)
412 fatal("failed to fstat %s", fname);
414 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
415 fatal("failed to allocate memory for %s", fname);
417 if (read(fd, buf, sz) != sz)
418 fatal("failed to read %s", fname);
423 if ((start = strstr(buf, g_etcbegin)) == NULL)
426 if (strlen(buf) != sz) {
427 fatal("embedded nul byte in %s; manual repair of %s "
428 "required\n", fname, fname);
431 if (strstr(start + 1, g_etcbegin) != NULL) {
432 fatal("multiple start sentinels in %s; manual repair of %s "
433 "required\n", fname, fname);
436 if ((end = strstr(buf, g_etcend)) == NULL) {
437 fatal("missing end sentinel in %s; manual repair of %s "
438 "required\n", fname, fname);
442 fatal("end sentinel preceeds start sentinel in %s; manual "
443 "repair of %s required\n", fname, fname);
446 end += strlen(g_etcend) + 1;
447 bcopy(end, start, strlen(end) + 1);
449 tmpname = alloca(sz = strlen(fname) + 80);
450 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
452 if ((fd = open(tmpname,
453 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
454 fatal("failed to create %s", tmpname);
456 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
457 (void) unlink(tmpname);
458 fatal("failed to write to %s", tmpname);
463 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
464 (void) unlink(tmpname);
465 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
466 (int)sbuf.st_uid, (int)sbuf.st_gid);
469 if (rename(tmpname, fname) == -1)
470 fatal("rename of %s to %s failed", tmpname, fname);
472 error("cleaned up forceload directives in %s\n", fname);
480 const char *mods[20];
483 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
484 fatal("failed to open output file '%s'", g_ofile);
486 oprintf("%s\n", g_etcbegin);
488 for (line = 0; g_etc[line] != NULL; line++)
489 oprintf("%s\n", g_etc[line]);
491 nmods = dtrace_provider_modules(g_dtp, mods,
492 sizeof (mods) / sizeof (char *) - 1);
494 if (nmods >= sizeof (mods) / sizeof (char *))
495 fatal("unexpectedly large number of modules!");
497 mods[nmods++] = "dtrace";
499 for (line = 0; line < nmods; line++)
500 oprintf("forceload: drv/%s\n", mods[line]);
502 oprintf("%s\n", g_etcend);
504 if (fclose(g_ofp) == EOF)
505 fatal("failed to close output file '%s'", g_ofile);
507 error("added forceload directives to %s\n", g_ofile);
512 print_probe_info(const dtrace_probeinfo_t *p)
517 oprintf("\n\tProbe Description Attributes\n");
519 oprintf("\t\tIdentifier Names: %s\n",
520 dtrace_stability_name(p->dtp_attr.dtat_name));
521 oprintf("\t\tData Semantics: %s\n",
522 dtrace_stability_name(p->dtp_attr.dtat_data));
523 oprintf("\t\tDependency Class: %s\n",
524 dtrace_class_name(p->dtp_attr.dtat_class));
526 oprintf("\n\tArgument Attributes\n");
528 oprintf("\t\tIdentifier Names: %s\n",
529 dtrace_stability_name(p->dtp_arga.dtat_name));
530 oprintf("\t\tData Semantics: %s\n",
531 dtrace_stability_name(p->dtp_arga.dtat_data));
532 oprintf("\t\tDependency Class: %s\n",
533 dtrace_class_name(p->dtp_arga.dtat_class));
535 oprintf("\n\tArgument Types\n");
537 for (i = 0; i < p->dtp_argc; i++) {
538 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
539 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
540 (void) strlcpy(buf, "(unknown)", sizeof (buf));
541 oprintf("\t\targs[%d]: %s\n", i, buf);
544 if (p->dtp_argc == 0)
545 oprintf("\t\tNone\n");
552 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
553 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
555 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
556 dtrace_probedesc_t *pdp = &edp->dted_probe;
557 dtrace_probeinfo_t p;
562 oprintf("\n%s:%s:%s:%s\n",
563 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
565 if (dtrace_probe_info(dtp, pdp, &p) == 0)
566 print_probe_info(&p);
573 * Execute the specified program by enabling the corresponding instrumentation.
574 * If -e has been specified, we get the program info but do not enable it. If
575 * -v has been specified, we print a stability report for the program.
578 exec_prog(const dtrace_cmd_t *dcp)
580 dtrace_ecbdesc_t *last = NULL;
581 dtrace_proginfo_t dpi;
584 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
585 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
586 dfatal("failed to enable '%s'", dcp->dc_name);
588 notice("%s '%s' matched %u probe%s\n",
589 dcp->dc_desc, dcp->dc_name,
590 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
594 oprintf("\nStability attributes for %s %s:\n",
595 dcp->dc_desc, dcp->dc_name);
597 oprintf("\n\tMinimum Probe Description Attributes\n");
598 oprintf("\t\tIdentifier Names: %s\n",
599 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
600 oprintf("\t\tData Semantics: %s\n",
601 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
602 oprintf("\t\tDependency Class: %s\n",
603 dtrace_class_name(dpi.dpi_descattr.dtat_class));
605 oprintf("\n\tMinimum Statement Attributes\n");
607 oprintf("\t\tIdentifier Names: %s\n",
608 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
609 oprintf("\t\tData Semantics: %s\n",
610 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
611 oprintf("\t\tDependency Class: %s\n",
612 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
615 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
616 (dtrace_stmt_f *)info_stmt, &last);
621 g_total += dpi.dpi_matches;
625 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
626 * storing in a driver.conf(4) file associated with the dtrace driver.
629 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
631 const uchar_t *p, *q;
634 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
637 q = p + dof->dofh_loadsz;
640 oprintf("dof-data-%d=0x%x", n, *p++);
643 oprintf(",0x%x", *p++);
648 * On FreeBSD, the DOF data is handled as a kernel environment (kenv)
649 * string. We use two hex characters per DOF byte.
651 oprintf("dof-data-%d=%02x", n, *p++);
654 oprintf("%02x", *p++);
659 dtrace_dof_destroy(g_dtp, dof);
663 * Link the specified D program in DOF form into an ELF file for use in either
664 * helpers, userland provider definitions, or both. If -o was specified, that
665 * path is used as the output file name. If -o wasn't specified and the input
666 * program is from a script whose name is %.d, use basename(%.o) as the output
667 * file name. Otherwise we use "d.out" as the default output file name.
670 link_prog(dtrace_cmd_t *dcp)
674 if (g_cmdc == 1 && g_ofile != NULL) {
675 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
676 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
677 strcmp(p, ".d") == 0) {
678 p[0] = '\0'; /* strip .d suffix */
679 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
680 "%s.o", basename(dcp->dc_arg));
681 } else if (g_cmdc > 1) {
682 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
683 "d.out.%td", dcp - g_cmdv);
685 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
689 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
690 dcp->dc_ofile, g_objc, g_objv) != 0)
691 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
696 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
698 dtrace_probeinfo_t p;
700 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
701 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
703 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
704 print_probe_info(&p);
711 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
712 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
714 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
719 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
720 error("failed to match %s:%s:%s:%s: %s\n",
721 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
722 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
723 dtrace_errmsg(dtp, dtrace_errno(dtp)));
731 * List the probes corresponding to the specified program by iterating over
732 * each statement and then matching probes to the statement probe descriptions.
735 list_prog(const dtrace_cmd_t *dcp)
737 dtrace_ecbdesc_t *last = NULL;
739 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
740 (dtrace_stmt_f *)list_stmt, &last);
744 compile_file(dtrace_cmd_t *dcp)
749 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
750 fatal("failed to open %s", dcp->dc_arg);
753 g_argv[0] = dcp->dc_arg;
755 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
756 g_cflags, g_argc, g_argv)) == NULL)
757 dfatal("failed to compile script %s", dcp->dc_arg);
762 dcp->dc_desc = "script";
763 dcp->dc_name = dcp->dc_arg;
767 compile_str(dtrace_cmd_t *dcp)
771 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
772 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
773 dfatal("invalid probe specifier %s", dcp->dc_arg);
775 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
776 *p = '\0'; /* crop name for reporting */
778 dcp->dc_desc = "description";
779 dcp->dc_name = dcp->dc_arg;
784 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
787 const psinfo_t *prp = Ppsinfo(P);
788 int pid = Pstatus(P)->pr_pid;
789 char name[SIG2STR_MAX];
791 int wstatus = proc_getwstat(P);
792 int pid = proc_getpid(P);
796 notice("pid %d: %s\n", pid, msg);
803 switch (proc_state(P)) {
808 * Ideally we would like to always report pr_wstat here, but it
809 * isn't possible given current /proc semantics. If we grabbed
810 * the process, Ppsinfo() will either fail or return a zeroed
811 * psinfo_t depending on how far the parent is in reaping it.
812 * When /proc provides a stable pr_wstat in the status file,
813 * this code can be improved by examining this new pr_wstat.
815 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
816 notice("pid %d terminated by %s\n", pid,
817 proc_signame(WTERMSIG(prp->pr_wstat),
818 name, sizeof (name)));
820 if (WIFSIGNALED(wstatus)) {
821 notice("pid %d terminated by %d\n", pid,
825 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
826 notice("pid %d exited with status %d\n",
827 pid, WEXITSTATUS(prp->pr_wstat));
829 } else if (WEXITSTATUS(wstatus) != 0) {
830 notice("pid %d exited with status %d\n",
831 pid, WEXITSTATUS(wstatus));
834 notice("pid %d has exited\n", pid);
840 notice("pid %d exec'd a set-id or unobservable program\n", pid);
848 errhandler(const dtrace_errdata_t *data, void *arg)
850 error(data->dteda_msg);
851 return (DTRACE_HANDLE_OK);
856 drophandler(const dtrace_dropdata_t *data, void *arg)
858 error(data->dtdda_msg);
859 return (DTRACE_HANDLE_OK);
864 setopthandler(const dtrace_setoptdata_t *data, void *arg)
866 if (strcmp(data->dtsda_option, "quiet") == 0)
867 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
869 if (strcmp(data->dtsda_option, "flowindent") == 0)
870 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
872 return (DTRACE_HANDLE_OK);
875 #define BUFDUMPHDR(hdr) \
876 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
878 #define BUFDUMPSTR(ptr, field) \
879 (void) printf("%s: %20s => ", g_pname, #field); \
880 if ((ptr)->field != NULL) { \
881 const char *c = (ptr)->field; \
882 (void) printf("\""); \
885 (void) printf("\\n"); \
889 (void) printf("%c", *c); \
890 } while (*c++ != '\0'); \
891 (void) printf("\"\n"); \
893 (void) printf("<NULL>\n"); \
896 #define BUFDUMPASSTR(ptr, field, str) \
897 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
899 #define BUFDUMP(ptr, field) \
900 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
901 (long long)(ptr)->field);
903 #define BUFDUMPPTR(ptr, field) \
904 (void) printf("%s: %20s => %s\n", g_pname, #field, \
905 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
909 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
911 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
912 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
913 const dtrace_probedesc_t *pd;
914 uint32_t flags = bufdata->dtbda_flags;
915 char buf[512], *c = buf, *end = c + sizeof (buf);
922 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
923 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
924 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
925 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
926 { "???", UINT32_MAX },
930 if (bufdata->dtbda_probe != NULL) {
931 pd = bufdata->dtbda_probe->dtpda_pdesc;
932 } else if (agg != NULL) {
933 pd = agg->dtada_pdesc;
938 BUFDUMPHDR(">>> Called buffer handler");
941 BUFDUMPHDR(" dtrace_bufdata");
942 BUFDUMPSTR(bufdata, dtbda_buffered);
943 BUFDUMPPTR(bufdata, dtbda_probe);
944 BUFDUMPPTR(bufdata, dtbda_aggdata);
945 BUFDUMPPTR(bufdata, dtbda_recdesc);
947 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
950 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
951 if (!(flags & flagnames[i].value))
954 (void) snprintf(c, end - c,
955 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
957 flags &= ~flagnames[i].value;
961 (void) snprintf(c, end - c, ")");
963 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
967 BUFDUMPHDR(" dtrace_probedesc");
968 BUFDUMPSTR(pd, dtpd_provider);
969 BUFDUMPSTR(pd, dtpd_mod);
970 BUFDUMPSTR(pd, dtpd_func);
971 BUFDUMPSTR(pd, dtpd_name);
976 BUFDUMPHDR(" dtrace_recdesc");
977 BUFDUMP(rec, dtrd_action);
978 BUFDUMP(rec, dtrd_size);
982 int lim = rec->dtrd_size;
984 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
985 c = buf + strlen(buf);
987 if (lim > sizeof (uint64_t))
988 lim = sizeof (uint64_t);
990 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
992 for (i = 0; i < lim; i++) {
993 (void) snprintf(c, end - c, "%s%02x",
994 i == 0 ? "" : " ", *data++);
998 (void) snprintf(c, end - c,
999 "%s)", lim < rec->dtrd_size ? " ..." : "");
1000 BUFDUMPASSTR(rec, dtrd_offset, buf);
1002 BUFDUMP(rec, dtrd_offset);
1009 dtrace_aggdesc_t *desc = agg->dtada_desc;
1011 BUFDUMPHDR(" dtrace_aggdesc");
1012 BUFDUMPSTR(desc, dtagd_name);
1013 BUFDUMP(desc, dtagd_varid);
1014 BUFDUMP(desc, dtagd_id);
1015 BUFDUMP(desc, dtagd_nrecs);
1019 return (DTRACE_HANDLE_OK);
1024 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1026 dtrace_actkind_t act;
1031 * We have processed the final record; output the newline if
1032 * we're not in quiet mode.
1037 return (DTRACE_CONSUME_NEXT);
1040 act = rec->dtrd_action;
1041 addr = (uintptr_t)data->dtpda_data;
1043 if (act == DTRACEACT_EXIT) {
1044 g_status = *((uint32_t *)addr);
1045 return (DTRACE_CONSUME_NEXT);
1048 return (DTRACE_CONSUME_THIS);
1053 chew(const dtrace_probedata_t *data, void *arg)
1055 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1056 processorid_t cpu = data->dtpda_cpu;
1061 return (DTRACE_CONSUME_ABORT);
1065 if (!g_flowindent) {
1067 oprintf("%3s %6s %32s\n",
1068 "CPU", "ID", "FUNCTION:NAME");
1071 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1076 if (!g_flowindent) {
1078 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1080 (void) snprintf(name, sizeof (name), "%s:%s",
1081 pd->dtpd_func, pd->dtpd_name);
1083 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1086 int indent = data->dtpda_indent;
1090 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1091 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1093 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1094 data->dtpda_prefix, pd->dtpd_func,
1097 len = indent + DTRACE_FUNCNAMELEN + 5;
1099 (void) snprintf(name, len, "%*s%s%s", indent, "",
1100 data->dtpda_prefix, pd->dtpd_func);
1103 oprintf("%3d %-41s ", cpu, name);
1106 return (DTRACE_CONSUME_THIS);
1117 dtrace_optval_t val;
1119 { "buffer size", "bufsize" },
1120 { "aggregation size", "aggsize" },
1121 { "speculation size", "specsize" },
1122 { "dynamic variable size", "dynvarsize" },
1125 { "cleaning rate", "cleanrate" },
1126 { "status rate", "statusrate" },
1130 for (i = 0; bufs[i].name != NULL; i++) {
1131 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1132 fatal("couldn't get option %s", bufs[i].optname);
1135 for (i = 0; rates[i].name != NULL; i++) {
1136 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1137 fatal("couldn't get option %s", rates[i].optname);
1140 if (dtrace_go(g_dtp) == -1)
1141 dfatal("could not enable tracing");
1143 for (i = 0; bufs[i].name != NULL; i++) {
1144 dtrace_optval_t j = 0, mul = 10;
1145 dtrace_optval_t nsize;
1147 if (bufs[i].val == DTRACEOPT_UNSET)
1150 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1152 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1155 if (nsize >= bufs[i].val - sizeof (uint64_t))
1158 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1161 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1162 error("%s lowered to %lld%c\n", bufs[i].name,
1163 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1165 error("%s lowered to %lld bytes\n", bufs[i].name,
1170 for (i = 0; rates[i].name != NULL; i++) {
1171 dtrace_optval_t nval;
1174 if (rates[i].val == DTRACEOPT_UNSET)
1177 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1179 if (nval == DTRACEOPT_UNSET || nval == 0)
1182 if (rates[i].val == nval)
1185 dir = nval > rates[i].val ? "reduced" : "increased";
1187 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1188 error("%s %s to %lld hz\n", rates[i].name, dir,
1189 (long long)NANOSEC / (long long)nval);
1193 if ((nval % NANOSEC) == 0) {
1194 error("%s %s to once every %lld seconds\n",
1196 (long long)nval / (long long)NANOSEC);
1200 error("%s %s to once every %lld nanoseconds\n",
1201 rates[i].name, dir, (long long)nval);
1217 main(int argc, char *argv[])
1219 dtrace_bufdesc_t buf;
1220 struct sigaction act, oact;
1221 dtrace_status_t status[2];
1222 dtrace_optval_t opt;
1226 int done = 0, mode = 0;
1229 struct ps_prochandle *P;
1232 g_pname = basename(argv[0]);
1235 return (usage(stderr));
1237 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1238 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1239 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1240 fatal("failed to allocate memory for arguments");
1242 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1243 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1245 bzero(status, sizeof (status));
1246 bzero(&buf, sizeof (buf));
1249 * Make an initial pass through argv[] processing any arguments that
1250 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1251 * We also accumulate arguments that are not affiliated with getopt
1252 * options into g_argv[], and abort if any invalid options are found.
1254 for (optind = 1; optind < argc; optind++) {
1255 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1258 if (strcmp(optarg, "2") != 0) {
1259 (void) fprintf(stderr,
1260 "%s: illegal option -- 3%s\n",
1262 return (usage(stderr));
1264 g_oflags &= ~DTRACE_O_LP64;
1265 g_oflags |= DTRACE_O_ILP32;
1269 if (strcmp(optarg, "4") != 0) {
1270 (void) fprintf(stderr,
1271 "%s: illegal option -- 6%s\n",
1273 return (usage(stderr));
1275 g_oflags &= ~DTRACE_O_ILP32;
1276 g_oflags |= DTRACE_O_LP64;
1280 g_grabanon++; /* also checked in pass 2 below */
1284 g_mode = DMODE_ANON;
1295 g_mode = DMODE_HEADER;
1296 g_oflags |= DTRACE_O_NODEV;
1297 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1303 g_mode = DMODE_LINK;
1304 g_oflags |= DTRACE_O_NODEV;
1305 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1311 g_mode = DMODE_LIST;
1312 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1317 g_mode = DMODE_VERS;
1322 if (strchr(DTRACE_OPTSTR, c) == NULL)
1323 return (usage(stderr));
1328 g_argv[g_argc++] = argv[optind];
1332 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1333 "can be specified at a time\n", g_pname);
1337 if (g_mode == DMODE_VERS)
1338 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1341 * If we're in linker mode and the data model hasn't been specified,
1342 * we try to guess the appropriate setting by examining the object
1343 * files. We ignore certain errors since we'll catch them later when
1344 * we actually process the object files.
1346 if (g_mode == DMODE_LINK &&
1347 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1348 elf_version(EV_CURRENT) != EV_NONE) {
1353 for (i = 1; i < g_argc; i++) {
1354 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1357 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1362 if (elf_kind(elf) != ELF_K_ELF ||
1363 gelf_getehdr(elf, &ehdr) == NULL) {
1365 (void) elf_end(elf);
1370 (void) elf_end(elf);
1372 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1373 if (g_oflags & DTRACE_O_ILP32) {
1374 fatal("can't mix 32-bit and 64-bit "
1377 g_oflags |= DTRACE_O_LP64;
1378 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1379 if (g_oflags & DTRACE_O_LP64) {
1380 fatal("can't mix 32-bit and 64-bit "
1383 g_oflags |= DTRACE_O_ILP32;
1391 * Open libdtrace. If we are not actually going to be enabling any
1392 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1394 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1395 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1396 g_oflags |= DTRACE_O_NODEV;
1400 fatal("failed to initialize dtrace: %s\n",
1401 dtrace_errmsg(NULL, err));
1404 #if defined(__i386__)
1405 /* XXX The 32-bit seems to need more buffer space by default -sson */
1406 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1407 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1409 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1410 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1414 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1415 * references to undefined symbols to remain as unresolved relocations.
1416 * If -A is specified, enable -xlink=primary to permit static linking
1417 * only to kernel symbols that are defined in a primary kernel module.
1419 if (g_mode == DMODE_LINK) {
1420 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1421 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1424 * Use the remaining arguments as the list of object files
1425 * when in linker mode.
1427 g_objc = g_argc - 1;
1428 g_objv = g_argv + 1;
1431 * We still use g_argv[0], the name of the executable.
1434 } else if (g_mode == DMODE_ANON)
1435 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1438 * Now that we have libdtrace open, make a second pass through argv[]
1439 * to perform any dtrace_setopt() calls and change any compiler flags.
1440 * We also accumulate any program specifications into our g_cmdv[] at
1441 * this time; these will compiled as part of the fourth processing pass.
1443 for (optind = 1; optind < argc; optind++) {
1444 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1447 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1448 dfatal("failed to set -a");
1452 if (dtrace_setopt(g_dtp,
1453 "bufsize", optarg) != 0)
1454 dfatal("failed to set -b %s", optarg);
1462 g_cflags |= DTRACE_C_CPP;
1466 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1467 dfatal("failed to set -D %s", optarg);
1471 dcp = &g_cmdv[g_cmdc++];
1472 dcp->dc_func = compile_str;
1473 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1474 dcp->dc_arg = optarg;
1478 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1479 dfatal("failed to set -F");
1483 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1484 dfatal("failed to set -H");
1488 dcp = &g_cmdv[g_cmdc++];
1489 dcp->dc_func = compile_str;
1490 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1491 dcp->dc_arg = optarg;
1495 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1496 dfatal("failed to set -I %s", optarg);
1500 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1501 dfatal("failed to set -L %s", optarg);
1505 dcp = &g_cmdv[g_cmdc++];
1506 dcp->dc_func = compile_str;
1507 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1508 dcp->dc_arg = optarg;
1512 dcp = &g_cmdv[g_cmdc++];
1513 dcp->dc_func = compile_str;
1514 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1515 dcp->dc_arg = optarg;
1519 dcp = &g_cmdv[g_cmdc++];
1520 dcp->dc_func = compile_str;
1521 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1522 dcp->dc_arg = optarg;
1526 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1527 dfatal("failed to set -q");
1535 dcp = &g_cmdv[g_cmdc++];
1536 dcp->dc_func = compile_file;
1537 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1538 dcp->dc_arg = optarg;
1542 g_cflags |= DTRACE_C_DIFV;
1546 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1547 dfatal("failed to set -U %s", optarg);
1555 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1556 dfatal("failed to set -w");
1560 if ((p = strchr(optarg, '=')) != NULL)
1563 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1564 dfatal("failed to set -x %s", optarg);
1568 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1569 dfatal("failed to set -X %s", optarg);
1573 g_cflags |= DTRACE_C_ZDEFS;
1577 if (strchr(DTRACE_OPTSTR, c) == NULL)
1578 return (usage(stderr));
1583 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1584 (void) fprintf(stderr, "%s: -B not valid in combination"
1585 " with [-AGl] options\n", g_pname);
1589 if (g_ofp == NULL && g_ofile != NULL) {
1590 (void) fprintf(stderr, "%s: -B not valid in combination"
1591 " with -o option\n", g_pname);
1596 * In our third pass we handle any command-line options related to
1597 * grabbing or creating victim processes. The behavior of these calls
1598 * may been affected by any library options set by the second pass.
1600 for (optind = 1; optind < argc; optind++) {
1601 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1604 if ((v = make_argv(optarg)) == NULL)
1605 fatal("failed to allocate memory");
1607 P = dtrace_proc_create(g_dtp, v[0], v, NULL, NULL);
1609 dfatal(NULL); /* dtrace_errmsg() only */
1617 pid = strtol(optarg, &p, 10);
1619 if (errno != 0 || p == optarg || p[0] != '\0')
1620 fatal("invalid pid: %s\n", optarg);
1622 P = dtrace_proc_grab(g_dtp, pid, 0);
1624 dfatal(NULL); /* dtrace_errmsg() only */
1633 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1634 * each string or file specification into a compiled program structure.
1636 for (i = 0; i < g_cmdc; i++)
1637 g_cmdv[i].dc_func(&g_cmdv[i]);
1639 if (g_mode != DMODE_LIST) {
1640 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1641 dfatal("failed to establish error handler");
1643 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1644 dfatal("failed to establish drop handler");
1646 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1647 dfatal("failed to establish proc handler");
1649 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1650 dfatal("failed to establish setopt handler");
1652 if (g_ofp == NULL &&
1653 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1654 dfatal("failed to establish buffered handler");
1657 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1658 g_flowindent = opt != DTRACEOPT_UNSET;
1660 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1661 g_grabanon = opt != DTRACEOPT_UNSET;
1663 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1664 g_quiet = opt != DTRACEOPT_UNSET;
1667 * Now make a fifth and final pass over the options that have been
1668 * turned into programs and saved in g_cmdv[], performing any mode-
1669 * specific processing. If g_mode is DMODE_EXEC, we will break out
1670 * of the switch() and continue on to the data processing loop. For
1671 * other modes, we will exit dtrace once mode-specific work is done.
1675 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1676 fatal("failed to open output file '%s'", g_ofile);
1678 for (i = 0; i < g_cmdc; i++)
1679 exec_prog(&g_cmdv[i]);
1681 if (done && !g_grabanon) {
1682 dtrace_close(g_dtp);
1688 if (g_ofile == NULL)
1690 g_ofile = "/kernel/drv/dtrace.conf";
1693 * On FreeBSD, anonymous DOF data is written to
1694 * the DTrace DOF file that the boot loader will
1695 * read if booting with the DTrace option.
1697 g_ofile = "/boot/dtrace.dof";
1700 dof_prune(g_ofile); /* strip out any old DOF directives */
1702 etcsystem_prune(); /* string out any forceload directives */
1706 dtrace_close(g_dtp);
1710 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1711 fatal("failed to open output file '%s'", g_ofile);
1713 for (i = 0; i < g_cmdc; i++) {
1714 anon_prog(&g_cmdv[i],
1715 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1719 * Dump out the DOF corresponding to the error handler and the
1720 * current options as the final DOF property in the .conf file.
1722 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1723 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1725 if (fclose(g_ofp) == EOF)
1726 fatal("failed to close output file '%s'", g_ofile);
1729 * These messages would use notice() rather than error(), but
1730 * we don't want them suppressed when -A is run on a D program
1731 * that itself contains a #pragma D option quiet.
1733 error("saved anonymous enabling in %s\n", g_ofile);
1736 error("run update_drv(1M) or reboot to enable changes\n");
1739 dtrace_close(g_dtp);
1744 (void) fprintf(stderr, "%s: -G requires one or more "
1745 "scripts or enabling options\n", g_pname);
1746 dtrace_close(g_dtp);
1750 for (i = 0; i < g_cmdc; i++)
1751 link_prog(&g_cmdv[i]);
1753 if (g_cmdc > 1 && g_ofile != NULL) {
1754 char **objv = alloca(g_cmdc * sizeof (char *));
1756 for (i = 0; i < g_cmdc; i++)
1757 objv[i] = g_cmdv[i].dc_ofile;
1759 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1760 g_ofile, g_cmdc, objv) != 0)
1761 dfatal(NULL); /* dtrace_errmsg() only */
1764 dtrace_close(g_dtp);
1768 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1769 fatal("failed to open output file '%s'", g_ofile);
1771 oprintf("%5s %10s %17s %33s %s\n",
1772 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1774 for (i = 0; i < g_cmdc; i++)
1775 list_prog(&g_cmdv[i]);
1778 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1780 dtrace_close(g_dtp);
1785 (void) fprintf(stderr, "%s: -h requires one or more "
1786 "scripts or enabling options\n", g_pname);
1787 dtrace_close(g_dtp);
1791 if (g_ofile == NULL) {
1795 (void) fprintf(stderr, "%s: -h requires an "
1796 "output file if multiple scripts are "
1797 "specified\n", g_pname);
1798 dtrace_close(g_dtp);
1802 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1803 strcmp(p, ".d") != 0) {
1804 (void) fprintf(stderr, "%s: -h requires an "
1805 "output file if no scripts are "
1806 "specified\n", g_pname);
1807 dtrace_close(g_dtp);
1811 p[0] = '\0'; /* strip .d suffix */
1812 g_ofile = p = g_cmdv[0].dc_ofile;
1813 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1814 "%s.h", basename(g_cmdv[0].dc_arg));
1817 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1818 fatal("failed to open header file '%s'", g_ofile);
1820 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1822 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1823 fclose(g_ofp) == EOF)
1824 dfatal("failed to create header file %s", g_ofile);
1826 dtrace_close(g_dtp);
1831 * If -a and -Z were not specified and no probes have been matched, no
1832 * probe criteria was specified on the command line and we abort.
1834 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1835 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1838 * Start tracing. Once we dtrace_go(), reload any options that affect
1839 * our globals in case consuming anonymous state has changed them.
1843 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1844 g_flowindent = opt != DTRACEOPT_UNSET;
1846 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1847 g_grabanon = opt != DTRACEOPT_UNSET;
1849 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1850 g_quiet = opt != DTRACEOPT_UNSET;
1852 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1853 if (opt != DTRACEOPT_UNSET)
1854 notice("allowing destructive actions\n");
1856 (void) sigemptyset(&act.sa_mask);
1858 act.sa_handler = intr;
1860 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1861 (void) sigaction(SIGINT, &act, NULL);
1863 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1864 (void) sigaction(SIGTERM, &act, NULL);
1867 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1868 (void) sigaction(SIGUSR1, &act, NULL);
1872 * Now that tracing is active and we are ready to consume trace data,
1873 * continue any grabbed or created processes, setting them running
1874 * using the /proc control mechanism inside of libdtrace.
1876 for (i = 0; i < g_psc; i++)
1877 dtrace_proc_continue(g_dtp, g_psv[i]);
1879 g_pslive = g_psc; /* count for prochandler() */
1882 if (!g_intr && !done)
1883 dtrace_sleep(g_dtp);
1887 * Output a newline just to make the output look
1888 * slightly cleaner. Note that we do this even in
1895 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1897 if (dtrace_stop(g_dtp) == -1)
1898 dfatal("couldn't stop tracing");
1901 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1902 case DTRACE_WORKSTATUS_DONE:
1905 case DTRACE_WORKSTATUS_OKAY:
1908 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1909 dfatal("processing aborted");
1912 if (g_ofp != NULL && fflush(g_ofp) == EOF)
1919 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1920 dtrace_errno(g_dtp) != EINTR)
1921 dfatal("failed to print aggregations");
1924 dtrace_close(g_dtp);