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, ...)
203 dfatal(const char *fmt, ...)
205 #if !defined(sun) && defined(NEED_ERRLOC)
206 char *p_errfile = NULL;
213 (void) fprintf(stderr, "%s: ", g_pname);
215 (void) vfprintf(stderr, fmt, ap);
219 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
220 (void) fprintf(stderr, ": %s\n",
221 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
222 } else if (fmt == NULL) {
223 (void) fprintf(stderr, "%s\n",
224 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
226 #if !defined(sun) && defined(NEED_ERRLOC)
227 dt_get_errloc(g_dtp, &p_errfile, &errline);
228 if (p_errfile != NULL)
229 printf("File '%s', line %d\n", p_errfile, errline);
233 * Close the DTrace handle to ensure that any controlled processes are
234 * correctly restored and continued.
243 error(const char *fmt, ...)
254 notice(const char *fmt, ...)
259 return; /* -q or quiet pragma suppresses notice()s */
268 oprintf(const char *fmt, ...)
277 n = vfprintf(g_ofp, fmt, ap);
281 if (errno != EINTR) {
282 fatal("failed to write to %s",
283 g_ofile ? g_ofile : "<stdout>");
292 const char *ws = "\f\n\r\t\v ";
293 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
300 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
311 dof_prune(const char *fname)
314 size_t sz, i, j, mark, len;
318 if ((fd = open(fname, O_RDONLY)) == -1) {
320 * This is okay only if the file doesn't exist at all.
323 fatal("failed to open %s", fname);
327 if (fstat(fd, &sbuf) == -1)
328 fatal("failed to fstat %s", fname);
330 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
331 fatal("failed to allocate memory for %s", fname);
333 if (read(fd, buf, sz) != sz)
334 fatal("failed to read %s", fname);
339 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
340 fatal("failed to open %s for writing", fname);
342 len = strlen("dof-data-");
344 for (mark = 0, i = 0; i < sz; i++) {
345 if (strncmp(&buf[i], "dof-data-", len) != 0)
349 * This is only a match if it's in the 0th column.
351 if (i != 0 && buf[i - 1] != '\n')
355 error("cleaned up old anonymous "
356 "enabling in %s\n", fname);
360 * We have a match. First write out our data up until now.
363 if (write(fd, &buf[mark], i - mark) != i - mark)
364 fatal("failed to write to %s", fname);
368 * Now scan forward until we scan past a newline.
370 for (j = i; j < sz && buf[j] != '\n'; j++)
376 if ((mark = j + 1) >= sz)
383 if (write(fd, &buf[mark], sz - mark) != sz - mark)
384 fatal("failed to write to %s", fname);
393 etcsystem_prune(void)
397 char *buf, *start, *end;
399 char *fname = g_etcfile, *tmpname;
401 if ((fd = open(fname, O_RDONLY)) == -1)
402 fatal("failed to open %s", fname);
404 if (fstat(fd, &sbuf) == -1)
405 fatal("failed to fstat %s", fname);
407 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
408 fatal("failed to allocate memory for %s", fname);
410 if (read(fd, buf, sz) != sz)
411 fatal("failed to read %s", fname);
416 if ((start = strstr(buf, g_etcbegin)) == NULL)
419 if (strlen(buf) != sz) {
420 fatal("embedded nul byte in %s; manual repair of %s "
421 "required\n", fname, fname);
424 if (strstr(start + 1, g_etcbegin) != NULL) {
425 fatal("multiple start sentinels in %s; manual repair of %s "
426 "required\n", fname, fname);
429 if ((end = strstr(buf, g_etcend)) == NULL) {
430 fatal("missing end sentinel in %s; manual repair of %s "
431 "required\n", fname, fname);
435 fatal("end sentinel preceeds start sentinel in %s; manual "
436 "repair of %s required\n", fname, fname);
439 end += strlen(g_etcend) + 1;
440 bcopy(end, start, strlen(end) + 1);
442 tmpname = alloca(sz = strlen(fname) + 80);
443 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
445 if ((fd = open(tmpname,
446 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
447 fatal("failed to create %s", tmpname);
449 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
450 (void) unlink(tmpname);
451 fatal("failed to write to %s", tmpname);
456 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
457 (void) unlink(tmpname);
458 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
459 (int)sbuf.st_uid, (int)sbuf.st_gid);
462 if (rename(tmpname, fname) == -1)
463 fatal("rename of %s to %s failed", tmpname, fname);
465 error("cleaned up forceload directives in %s\n", fname);
473 const char *mods[20];
476 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
477 fatal("failed to open output file '%s'", g_ofile);
479 oprintf("%s\n", g_etcbegin);
481 for (line = 0; g_etc[line] != NULL; line++)
482 oprintf("%s\n", g_etc[line]);
484 nmods = dtrace_provider_modules(g_dtp, mods,
485 sizeof (mods) / sizeof (char *) - 1);
487 if (nmods >= sizeof (mods) / sizeof (char *))
488 fatal("unexpectedly large number of modules!");
490 mods[nmods++] = "dtrace";
492 for (line = 0; line < nmods; line++)
493 oprintf("forceload: drv/%s\n", mods[line]);
495 oprintf("%s\n", g_etcend);
497 if (fclose(g_ofp) == EOF)
498 fatal("failed to close output file '%s'", g_ofile);
500 error("added forceload directives to %s\n", g_ofile);
505 print_probe_info(const dtrace_probeinfo_t *p)
510 oprintf("\n\tProbe Description Attributes\n");
512 oprintf("\t\tIdentifier Names: %s\n",
513 dtrace_stability_name(p->dtp_attr.dtat_name));
514 oprintf("\t\tData Semantics: %s\n",
515 dtrace_stability_name(p->dtp_attr.dtat_data));
516 oprintf("\t\tDependency Class: %s\n",
517 dtrace_class_name(p->dtp_attr.dtat_class));
519 oprintf("\n\tArgument Attributes\n");
521 oprintf("\t\tIdentifier Names: %s\n",
522 dtrace_stability_name(p->dtp_arga.dtat_name));
523 oprintf("\t\tData Semantics: %s\n",
524 dtrace_stability_name(p->dtp_arga.dtat_data));
525 oprintf("\t\tDependency Class: %s\n",
526 dtrace_class_name(p->dtp_arga.dtat_class));
528 oprintf("\n\tArgument Types\n");
530 for (i = 0; i < p->dtp_argc; i++) {
531 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
532 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
533 (void) strlcpy(buf, "(unknown)", sizeof (buf));
534 oprintf("\t\targs[%d]: %s\n", i, buf);
537 if (p->dtp_argc == 0)
538 oprintf("\t\tNone\n");
545 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
546 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
548 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
549 dtrace_probedesc_t *pdp = &edp->dted_probe;
550 dtrace_probeinfo_t p;
555 oprintf("\n%s:%s:%s:%s\n",
556 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
558 if (dtrace_probe_info(dtp, pdp, &p) == 0)
559 print_probe_info(&p);
566 * Execute the specified program by enabling the corresponding instrumentation.
567 * If -e has been specified, we get the program info but do not enable it. If
568 * -v has been specified, we print a stability report for the program.
571 exec_prog(const dtrace_cmd_t *dcp)
573 dtrace_ecbdesc_t *last = NULL;
574 dtrace_proginfo_t dpi;
577 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
578 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
579 dfatal("failed to enable '%s'", dcp->dc_name);
581 notice("%s '%s' matched %u probe%s\n",
582 dcp->dc_desc, dcp->dc_name,
583 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
587 oprintf("\nStability attributes for %s %s:\n",
588 dcp->dc_desc, dcp->dc_name);
590 oprintf("\n\tMinimum Probe Description Attributes\n");
591 oprintf("\t\tIdentifier Names: %s\n",
592 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
593 oprintf("\t\tData Semantics: %s\n",
594 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
595 oprintf("\t\tDependency Class: %s\n",
596 dtrace_class_name(dpi.dpi_descattr.dtat_class));
598 oprintf("\n\tMinimum Statement Attributes\n");
600 oprintf("\t\tIdentifier Names: %s\n",
601 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
602 oprintf("\t\tData Semantics: %s\n",
603 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
604 oprintf("\t\tDependency Class: %s\n",
605 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
608 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
609 (dtrace_stmt_f *)info_stmt, &last);
614 g_total += dpi.dpi_matches;
618 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
619 * storing in a driver.conf(4) file associated with the dtrace driver.
622 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
624 const uchar_t *p, *q;
627 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
630 q = p + dof->dofh_loadsz;
633 oprintf("dof-data-%d=0x%x", n, *p++);
636 oprintf(",0x%x", *p++);
641 * On FreeBSD, the DOF data is handled as a kernel environment (kenv)
642 * string. We use two hex characters per DOF byte.
644 oprintf("dof-data-%d=%02x", n, *p++);
647 oprintf("%02x", *p++);
652 dtrace_dof_destroy(g_dtp, dof);
656 * Link the specified D program in DOF form into an ELF file for use in either
657 * helpers, userland provider definitions, or both. If -o was specified, that
658 * path is used as the output file name. If -o wasn't specified and the input
659 * program is from a script whose name is %.d, use basename(%.o) as the output
660 * file name. Otherwise we use "d.out" as the default output file name.
663 link_prog(dtrace_cmd_t *dcp)
667 if (g_cmdc == 1 && g_ofile != NULL) {
668 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
669 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
670 strcmp(p, ".d") == 0) {
671 p[0] = '\0'; /* strip .d suffix */
672 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
673 "%s.o", basename(dcp->dc_arg));
675 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
676 g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
679 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
680 dcp->dc_ofile, g_objc, g_objv) != 0)
681 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
686 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
688 dtrace_probeinfo_t p;
690 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
691 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
693 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
694 print_probe_info(&p);
701 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
702 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
704 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
709 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
710 error("failed to match %s:%s:%s:%s: %s\n",
711 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
712 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
713 dtrace_errmsg(dtp, dtrace_errno(dtp)));
721 * List the probes corresponding to the specified program by iterating over
722 * each statement and then matching probes to the statement probe descriptions.
725 list_prog(const dtrace_cmd_t *dcp)
727 dtrace_ecbdesc_t *last = NULL;
729 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
730 (dtrace_stmt_f *)list_stmt, &last);
734 compile_file(dtrace_cmd_t *dcp)
739 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
740 fatal("failed to open %s", dcp->dc_arg);
743 g_argv[0] = dcp->dc_arg;
745 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
746 g_cflags, g_argc, g_argv)) == NULL)
747 dfatal("failed to compile script %s", dcp->dc_arg);
752 dcp->dc_desc = "script";
753 dcp->dc_name = dcp->dc_arg;
757 compile_str(dtrace_cmd_t *dcp)
761 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
762 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
763 dfatal("invalid probe specifier %s", dcp->dc_arg);
765 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
766 *p = '\0'; /* crop name for reporting */
768 dcp->dc_desc = "description";
769 dcp->dc_name = dcp->dc_arg;
774 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
776 fatal("DOODAD in function %s, file %s, line %d\n",__FUNCTION__,__FILE__,__LINE__);
778 const psinfo_t *prp = Ppsinfo(P);
779 int pid = Pstatus(P)->pr_pid;
780 char name[SIG2STR_MAX];
783 notice("pid %d: %s\n", pid, msg);
790 * Ideally we would like to always report pr_wstat here, but it
791 * isn't possible given current /proc semantics. If we grabbed
792 * the process, Ppsinfo() will either fail or return a zeroed
793 * psinfo_t depending on how far the parent is in reaping it.
794 * When /proc provides a stable pr_wstat in the status file,
795 * this code can be improved by examining this new pr_wstat.
797 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
798 notice("pid %d terminated by %s\n", pid,
799 proc_signame(WTERMSIG(prp->pr_wstat),
800 name, sizeof (name)));
801 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
802 notice("pid %d exited with status %d\n",
803 pid, WEXITSTATUS(prp->pr_wstat));
805 notice("pid %d has exited\n", pid);
811 notice("pid %d exec'd a set-id or unobservable program\n", pid);
820 errhandler(const dtrace_errdata_t *data, void *arg)
822 error(data->dteda_msg);
823 return (DTRACE_HANDLE_OK);
828 drophandler(const dtrace_dropdata_t *data, void *arg)
830 error(data->dtdda_msg);
831 return (DTRACE_HANDLE_OK);
836 setopthandler(const dtrace_setoptdata_t *data, void *arg)
838 if (strcmp(data->dtsda_option, "quiet") == 0)
839 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
841 if (strcmp(data->dtsda_option, "flowindent") == 0)
842 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
844 return (DTRACE_HANDLE_OK);
847 #define BUFDUMPHDR(hdr) \
848 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
850 #define BUFDUMPSTR(ptr, field) \
851 (void) printf("%s: %20s => ", g_pname, #field); \
852 if ((ptr)->field != NULL) { \
853 const char *c = (ptr)->field; \
854 (void) printf("\""); \
857 (void) printf("\\n"); \
861 (void) printf("%c", *c); \
862 } while (*c++ != '\0'); \
863 (void) printf("\"\n"); \
865 (void) printf("<NULL>\n"); \
868 #define BUFDUMPASSTR(ptr, field, str) \
869 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
871 #define BUFDUMP(ptr, field) \
872 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
873 (long long)(ptr)->field);
875 #define BUFDUMPPTR(ptr, field) \
876 (void) printf("%s: %20s => %s\n", g_pname, #field, \
877 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
881 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
883 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
884 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
885 const dtrace_probedesc_t *pd;
886 uint32_t flags = bufdata->dtbda_flags;
887 char buf[512], *c = buf, *end = c + sizeof (buf);
894 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
895 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
896 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
897 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
898 { "???", UINT32_MAX },
902 if (bufdata->dtbda_probe != NULL) {
903 pd = bufdata->dtbda_probe->dtpda_pdesc;
904 } else if (agg != NULL) {
905 pd = agg->dtada_pdesc;
910 BUFDUMPHDR(">>> Called buffer handler");
913 BUFDUMPHDR(" dtrace_bufdata");
914 BUFDUMPSTR(bufdata, dtbda_buffered);
915 BUFDUMPPTR(bufdata, dtbda_probe);
916 BUFDUMPPTR(bufdata, dtbda_aggdata);
917 BUFDUMPPTR(bufdata, dtbda_recdesc);
919 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
922 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
923 if (!(flags & flagnames[i].value))
926 (void) snprintf(c, end - c,
927 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
929 flags &= ~flagnames[i].value;
933 (void) snprintf(c, end - c, ")");
935 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
939 BUFDUMPHDR(" dtrace_probedesc");
940 BUFDUMPSTR(pd, dtpd_provider);
941 BUFDUMPSTR(pd, dtpd_mod);
942 BUFDUMPSTR(pd, dtpd_func);
943 BUFDUMPSTR(pd, dtpd_name);
948 BUFDUMPHDR(" dtrace_recdesc");
949 BUFDUMP(rec, dtrd_action);
950 BUFDUMP(rec, dtrd_size);
954 int lim = rec->dtrd_size;
956 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
957 c = buf + strlen(buf);
959 if (lim > sizeof (uint64_t))
960 lim = sizeof (uint64_t);
962 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
964 for (i = 0; i < lim; i++) {
965 (void) snprintf(c, end - c, "%s%02x",
966 i == 0 ? "" : " ", *data++);
970 (void) snprintf(c, end - c,
971 "%s)", lim < rec->dtrd_size ? " ..." : "");
972 BUFDUMPASSTR(rec, dtrd_offset, buf);
974 BUFDUMP(rec, dtrd_offset);
981 dtrace_aggdesc_t *desc = agg->dtada_desc;
983 BUFDUMPHDR(" dtrace_aggdesc");
984 BUFDUMPSTR(desc, dtagd_name);
985 BUFDUMP(desc, dtagd_varid);
986 BUFDUMP(desc, dtagd_id);
987 BUFDUMP(desc, dtagd_nrecs);
991 return (DTRACE_HANDLE_OK);
996 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
998 dtrace_actkind_t act;
1003 * We have processed the final record; output the newline if
1004 * we're not in quiet mode.
1009 return (DTRACE_CONSUME_NEXT);
1012 act = rec->dtrd_action;
1013 addr = (uintptr_t)data->dtpda_data;
1015 if (act == DTRACEACT_EXIT) {
1016 g_status = *((uint32_t *)addr);
1017 return (DTRACE_CONSUME_NEXT);
1020 return (DTRACE_CONSUME_THIS);
1025 chew(const dtrace_probedata_t *data, void *arg)
1027 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1028 processorid_t cpu = data->dtpda_cpu;
1033 return (DTRACE_CONSUME_ABORT);
1037 if (!g_flowindent) {
1039 oprintf("%3s %6s %32s\n",
1040 "CPU", "ID", "FUNCTION:NAME");
1043 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1048 if (!g_flowindent) {
1050 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1052 (void) snprintf(name, sizeof (name), "%s:%s",
1053 pd->dtpd_func, pd->dtpd_name);
1055 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1058 int indent = data->dtpda_indent;
1062 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1063 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1065 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1066 data->dtpda_prefix, pd->dtpd_func,
1069 len = indent + DTRACE_FUNCNAMELEN + 5;
1071 (void) snprintf(name, len, "%*s%s%s", indent, "",
1072 data->dtpda_prefix, pd->dtpd_func);
1075 oprintf("%3d %-41s ", cpu, name);
1078 return (DTRACE_CONSUME_THIS);
1089 dtrace_optval_t val;
1091 { "buffer size", "bufsize" },
1092 { "aggregation size", "aggsize" },
1093 { "speculation size", "specsize" },
1094 { "dynamic variable size", "dynvarsize" },
1097 { "cleaning rate", "cleanrate" },
1098 { "status rate", "statusrate" },
1102 for (i = 0; bufs[i].name != NULL; i++) {
1103 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1104 fatal("couldn't get option %s", bufs[i].optname);
1107 for (i = 0; rates[i].name != NULL; i++) {
1108 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1109 fatal("couldn't get option %s", rates[i].optname);
1112 if (dtrace_go(g_dtp) == -1)
1113 dfatal("could not enable tracing");
1115 for (i = 0; bufs[i].name != NULL; i++) {
1116 dtrace_optval_t j = 0, mul = 10;
1117 dtrace_optval_t nsize;
1119 if (bufs[i].val == DTRACEOPT_UNSET)
1122 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1124 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1127 if (nsize >= bufs[i].val - sizeof (uint64_t))
1130 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1133 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1134 error("%s lowered to %lld%c\n", bufs[i].name,
1135 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1137 error("%s lowered to %lld bytes\n", bufs[i].name,
1142 for (i = 0; rates[i].name != NULL; i++) {
1143 dtrace_optval_t nval;
1146 if (rates[i].val == DTRACEOPT_UNSET)
1149 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1151 if (nval == DTRACEOPT_UNSET || nval == 0)
1154 if (rates[i].val == nval)
1157 dir = nval > rates[i].val ? "reduced" : "increased";
1159 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1160 error("%s %s to %lld hz\n", rates[i].name, dir,
1161 (long long)NANOSEC / (long long)nval);
1165 if ((nval % NANOSEC) == 0) {
1166 error("%s %s to once every %lld seconds\n",
1168 (long long)nval / (long long)NANOSEC);
1172 error("%s %s to once every %lld nanoseconds\n",
1173 rates[i].name, dir, (long long)nval);
1189 main(int argc, char *argv[])
1191 dtrace_bufdesc_t buf;
1192 struct sigaction act, oact;
1193 dtrace_status_t status[2];
1194 dtrace_optval_t opt;
1198 int done = 0, mode = 0;
1201 struct ps_prochandle *P;
1204 g_pname = basename(argv[0]);
1207 return (usage(stderr));
1209 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1210 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1211 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1212 fatal("failed to allocate memory for arguments");
1214 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1215 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1217 bzero(status, sizeof (status));
1218 bzero(&buf, sizeof (buf));
1221 * Make an initial pass through argv[] processing any arguments that
1222 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1223 * We also accumulate arguments that are not affiliated with getopt
1224 * options into g_argv[], and abort if any invalid options are found.
1226 for (optind = 1; optind < argc; optind++) {
1227 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1230 if (strcmp(optarg, "2") != 0) {
1231 (void) fprintf(stderr,
1232 "%s: illegal option -- 3%s\n",
1234 return (usage(stderr));
1236 g_oflags &= ~DTRACE_O_LP64;
1237 g_oflags |= DTRACE_O_ILP32;
1241 if (strcmp(optarg, "4") != 0) {
1242 (void) fprintf(stderr,
1243 "%s: illegal option -- 6%s\n",
1245 return (usage(stderr));
1247 g_oflags &= ~DTRACE_O_ILP32;
1248 g_oflags |= DTRACE_O_LP64;
1252 g_grabanon++; /* also checked in pass 2 below */
1256 g_mode = DMODE_ANON;
1267 g_mode = DMODE_HEADER;
1268 g_oflags |= DTRACE_O_NODEV;
1269 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1275 g_mode = DMODE_LINK;
1276 g_oflags |= DTRACE_O_NODEV;
1277 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1283 g_mode = DMODE_LIST;
1284 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1289 g_mode = DMODE_VERS;
1294 if (strchr(DTRACE_OPTSTR, c) == NULL)
1295 return (usage(stderr));
1300 g_argv[g_argc++] = argv[optind];
1304 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1305 "can be specified at a time\n", g_pname);
1309 if (g_mode == DMODE_VERS)
1310 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1313 * If we're in linker mode and the data model hasn't been specified,
1314 * we try to guess the appropriate setting by examining the object
1315 * files. We ignore certain errors since we'll catch them later when
1316 * we actually process the object files.
1318 if (g_mode == DMODE_LINK &&
1319 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1320 elf_version(EV_CURRENT) != EV_NONE) {
1325 for (i = 1; i < g_argc; i++) {
1326 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1329 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1334 if (elf_kind(elf) != ELF_K_ELF ||
1335 gelf_getehdr(elf, &ehdr) == NULL) {
1337 (void) elf_end(elf);
1342 (void) elf_end(elf);
1344 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1345 if (g_oflags & DTRACE_O_ILP32) {
1346 fatal("can't mix 32-bit and 64-bit "
1349 g_oflags |= DTRACE_O_LP64;
1350 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1351 if (g_oflags & DTRACE_O_LP64) {
1352 fatal("can't mix 32-bit and 64-bit "
1355 g_oflags |= DTRACE_O_ILP32;
1363 * Open libdtrace. If we are not actually going to be enabling any
1364 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1366 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1367 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1368 g_oflags |= DTRACE_O_NODEV;
1372 fatal("failed to initialize dtrace: %s\n",
1373 dtrace_errmsg(NULL, err));
1376 #if defined(__i386__)
1377 /* XXX The 32-bit seems to need more buffer space by default -sson */
1378 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1379 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1381 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1382 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1386 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1387 * references to undefined symbols to remain as unresolved relocations.
1388 * If -A is specified, enable -xlink=primary to permit static linking
1389 * only to kernel symbols that are defined in a primary kernel module.
1391 if (g_mode == DMODE_LINK) {
1392 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1393 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1396 * Use the remaining arguments as the list of object files
1397 * when in linker mode.
1399 g_objc = g_argc - 1;
1400 g_objv = g_argv + 1;
1403 * We still use g_argv[0], the name of the executable.
1406 } else if (g_mode == DMODE_ANON)
1407 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1410 * Now that we have libdtrace open, make a second pass through argv[]
1411 * to perform any dtrace_setopt() calls and change any compiler flags.
1412 * We also accumulate any program specifications into our g_cmdv[] at
1413 * this time; these will compiled as part of the fourth processing pass.
1415 for (optind = 1; optind < argc; optind++) {
1416 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1419 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1420 dfatal("failed to set -a");
1424 if (dtrace_setopt(g_dtp,
1425 "bufsize", optarg) != 0)
1426 dfatal("failed to set -b %s", optarg);
1434 g_cflags |= DTRACE_C_CPP;
1438 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1439 dfatal("failed to set -D %s", optarg);
1443 dcp = &g_cmdv[g_cmdc++];
1444 dcp->dc_func = compile_str;
1445 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1446 dcp->dc_arg = optarg;
1450 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1451 dfatal("failed to set -F");
1455 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1456 dfatal("failed to set -H");
1460 dcp = &g_cmdv[g_cmdc++];
1461 dcp->dc_func = compile_str;
1462 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1463 dcp->dc_arg = optarg;
1467 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1468 dfatal("failed to set -I %s", optarg);
1472 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1473 dfatal("failed to set -L %s", optarg);
1477 dcp = &g_cmdv[g_cmdc++];
1478 dcp->dc_func = compile_str;
1479 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1480 dcp->dc_arg = optarg;
1484 dcp = &g_cmdv[g_cmdc++];
1485 dcp->dc_func = compile_str;
1486 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1487 dcp->dc_arg = optarg;
1491 dcp = &g_cmdv[g_cmdc++];
1492 dcp->dc_func = compile_str;
1493 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1494 dcp->dc_arg = optarg;
1498 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1499 dfatal("failed to set -q");
1507 dcp = &g_cmdv[g_cmdc++];
1508 dcp->dc_func = compile_file;
1509 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1510 dcp->dc_arg = optarg;
1514 g_cflags |= DTRACE_C_DIFV;
1518 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1519 dfatal("failed to set -U %s", optarg);
1527 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1528 dfatal("failed to set -w");
1532 if ((p = strchr(optarg, '=')) != NULL)
1535 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1536 dfatal("failed to set -x %s", optarg);
1540 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1541 dfatal("failed to set -X %s", optarg);
1545 g_cflags |= DTRACE_C_ZDEFS;
1549 if (strchr(DTRACE_OPTSTR, c) == NULL)
1550 return (usage(stderr));
1555 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1556 (void) fprintf(stderr, "%s: -B not valid in combination"
1557 " with [-AGl] options\n", g_pname);
1561 if (g_ofp == NULL && g_ofile != NULL) {
1562 (void) fprintf(stderr, "%s: -B not valid in combination"
1563 " with -o option\n", g_pname);
1568 * In our third pass we handle any command-line options related to
1569 * grabbing or creating victim processes. The behavior of these calls
1570 * may been affected by any library options set by the second pass.
1572 for (optind = 1; optind < argc; optind++) {
1573 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1576 if ((v = make_argv(optarg)) == NULL)
1577 fatal("failed to allocate memory");
1579 P = dtrace_proc_create(g_dtp, v[0], v);
1581 dfatal(NULL); /* dtrace_errmsg() only */
1589 pid = strtol(optarg, &p, 10);
1591 if (errno != 0 || p == optarg || p[0] != '\0')
1592 fatal("invalid pid: %s\n", optarg);
1594 P = dtrace_proc_grab(g_dtp, pid, 0);
1596 dfatal(NULL); /* dtrace_errmsg() only */
1605 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1606 * each string or file specification into a compiled program structure.
1608 for (i = 0; i < g_cmdc; i++)
1609 g_cmdv[i].dc_func(&g_cmdv[i]);
1611 if (g_mode != DMODE_LIST) {
1612 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1613 dfatal("failed to establish error handler");
1615 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1616 dfatal("failed to establish drop handler");
1618 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1619 dfatal("failed to establish proc handler");
1621 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1622 dfatal("failed to establish setopt handler");
1624 if (g_ofp == NULL &&
1625 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1626 dfatal("failed to establish buffered handler");
1629 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1630 g_flowindent = opt != DTRACEOPT_UNSET;
1632 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1633 g_grabanon = opt != DTRACEOPT_UNSET;
1635 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1636 g_quiet = opt != DTRACEOPT_UNSET;
1639 * Now make a fifth and final pass over the options that have been
1640 * turned into programs and saved in g_cmdv[], performing any mode-
1641 * specific processing. If g_mode is DMODE_EXEC, we will break out
1642 * of the switch() and continue on to the data processing loop. For
1643 * other modes, we will exit dtrace once mode-specific work is done.
1647 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1648 fatal("failed to open output file '%s'", g_ofile);
1650 for (i = 0; i < g_cmdc; i++)
1651 exec_prog(&g_cmdv[i]);
1653 if (done && !g_grabanon) {
1654 dtrace_close(g_dtp);
1660 if (g_ofile == NULL)
1662 g_ofile = "/kernel/drv/dtrace.conf";
1665 * On FreeBSD, anonymous DOF data is written to
1666 * the DTrace DOF file that the boot loader will
1667 * read if booting with the DTrace option.
1669 g_ofile = "/boot/dtrace.dof";
1672 dof_prune(g_ofile); /* strip out any old DOF directives */
1674 etcsystem_prune(); /* string out any forceload directives */
1678 dtrace_close(g_dtp);
1682 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1683 fatal("failed to open output file '%s'", g_ofile);
1685 for (i = 0; i < g_cmdc; i++) {
1686 anon_prog(&g_cmdv[i],
1687 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1691 * Dump out the DOF corresponding to the error handler and the
1692 * current options as the final DOF property in the .conf file.
1694 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1695 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1697 if (fclose(g_ofp) == EOF)
1698 fatal("failed to close output file '%s'", g_ofile);
1701 * These messages would use notice() rather than error(), but
1702 * we don't want them suppressed when -A is run on a D program
1703 * that itself contains a #pragma D option quiet.
1705 error("saved anonymous enabling in %s\n", g_ofile);
1708 error("run update_drv(1M) or reboot to enable changes\n");
1711 dtrace_close(g_dtp);
1716 (void) fprintf(stderr, "%s: -G requires one or more "
1717 "scripts or enabling options\n", g_pname);
1718 dtrace_close(g_dtp);
1722 for (i = 0; i < g_cmdc; i++)
1723 link_prog(&g_cmdv[i]);
1725 if (g_cmdc > 1 && g_ofile != NULL) {
1726 char **objv = alloca(g_cmdc * sizeof (char *));
1728 for (i = 0; i < g_cmdc; i++)
1729 objv[i] = g_cmdv[i].dc_ofile;
1731 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1732 g_ofile, g_cmdc, objv) != 0)
1733 dfatal(NULL); /* dtrace_errmsg() only */
1736 dtrace_close(g_dtp);
1740 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1741 fatal("failed to open output file '%s'", g_ofile);
1743 oprintf("%5s %10s %17s %33s %s\n",
1744 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1746 for (i = 0; i < g_cmdc; i++)
1747 list_prog(&g_cmdv[i]);
1750 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1752 dtrace_close(g_dtp);
1757 (void) fprintf(stderr, "%s: -h requires one or more "
1758 "scripts or enabling options\n", g_pname);
1759 dtrace_close(g_dtp);
1763 if (g_ofile == NULL) {
1767 (void) fprintf(stderr, "%s: -h requires an "
1768 "output file if multiple scripts are "
1769 "specified\n", g_pname);
1770 dtrace_close(g_dtp);
1774 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1775 strcmp(p, ".d") != 0) {
1776 (void) fprintf(stderr, "%s: -h requires an "
1777 "output file if no scripts are "
1778 "specified\n", g_pname);
1779 dtrace_close(g_dtp);
1783 p[0] = '\0'; /* strip .d suffix */
1784 g_ofile = p = g_cmdv[0].dc_ofile;
1785 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1786 "%s.h", basename(g_cmdv[0].dc_arg));
1789 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1790 fatal("failed to open header file '%s'", g_ofile);
1792 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1794 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1795 fclose(g_ofp) == EOF)
1796 dfatal("failed to create header file %s", g_ofile);
1798 dtrace_close(g_dtp);
1803 * If -a and -Z were not specified and no probes have been matched, no
1804 * probe criteria was specified on the command line and we abort.
1806 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1807 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1810 * Start tracing. Once we dtrace_go(), reload any options that affect
1811 * our globals in case consuming anonymous state has changed them.
1815 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1816 g_flowindent = opt != DTRACEOPT_UNSET;
1818 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1819 g_grabanon = opt != DTRACEOPT_UNSET;
1821 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1822 g_quiet = opt != DTRACEOPT_UNSET;
1824 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1825 if (opt != DTRACEOPT_UNSET)
1826 notice("allowing destructive actions\n");
1828 (void) sigemptyset(&act.sa_mask);
1830 act.sa_handler = intr;
1832 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1833 (void) sigaction(SIGINT, &act, NULL);
1835 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1836 (void) sigaction(SIGTERM, &act, NULL);
1839 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1840 (void) sigaction(SIGUSR1, &act, NULL);
1844 * Now that tracing is active and we are ready to consume trace data,
1845 * continue any grabbed or created processes, setting them running
1846 * using the /proc control mechanism inside of libdtrace.
1848 for (i = 0; i < g_psc; i++)
1849 dtrace_proc_continue(g_dtp, g_psv[i]);
1851 g_pslive = g_psc; /* count for prochandler() */
1854 if (!g_intr && !done)
1855 dtrace_sleep(g_dtp);
1859 * Output a newline just to make the output look
1860 * slightly cleaner. Note that we do this even in
1867 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1869 if (dtrace_stop(g_dtp) == -1)
1870 dfatal("couldn't stop tracing");
1873 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1874 case DTRACE_WORKSTATUS_DONE:
1877 case DTRACE_WORKSTATUS_OKAY:
1880 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1881 dfatal("processing aborted");
1884 if (g_ofp != NULL && fflush(g_ofp) == EOF)
1891 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1892 dtrace_errno(g_dtp) != EINTR)
1893 dfatal("failed to print aggregations");
1896 dtrace_close(g_dtp);