4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
31 #include <sys/types.h>
54 typedef struct dtrace_cmd {
55 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
56 dtrace_probespec_t dc_spec; /* probe specifier context */
57 char *dc_arg; /* argument from main argv */
58 const char *dc_name; /* name for error messages */
59 const char *dc_desc; /* desc for error messages */
60 dtrace_prog_t *dc_prog; /* program compiled from arg */
61 char dc_ofile[PATH_MAX]; /* derived output file name */
64 #define DMODE_VERS 0 /* display version information and exit (-V) */
65 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
66 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
67 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
68 #define DMODE_LIST 4 /* compile program and list probes (-l) */
69 #define DMODE_HEADER 5 /* compile program for headergen (-h) */
75 static const char DTRACE_OPTSTR[] =
76 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
82 static dtrace_cmd_t *g_cmdv;
84 static struct ps_prochandle **g_psv;
89 static int g_flowindent;
91 static int g_impatient;
97 static int g_exec = 1;
98 static int g_mode = DMODE_EXEC;
99 static int g_status = E_SUCCESS;
100 static int g_grabanon = 0;
101 static const char *g_ofile = NULL;
103 static dtrace_hdl_t *g_dtp;
105 static char *g_etcfile = "/etc/system";
106 static const char *g_etcbegin = "* vvvv Added by DTrace";
107 static const char *g_etcend = "* ^^^^ Added by DTrace";
109 static const char *g_etc[] = {
111 "* The following forceload directives were added by dtrace(1M) to allow for",
112 "* tracing during boot. If these directives are removed, the system will",
113 "* continue to function, but tracing will not occur during boot as desired.",
114 "* To remove these directives (and this block comment) automatically, run",
115 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
116 "* chapter of the Solaris Dynamic Tracing Guide for details.",
124 static const char predact[] = "[[ predicate ] action ]";
126 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
127 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
128 "[-o output] [-p pid] [-s script] [-U name]\n\t"
129 "[-x opt[=val]] [-X a|c|s|t]\n\n"
130 "\t[-P provider %s]\n"
131 "\t[-m [ provider: ] module %s]\n"
132 "\t[-f [[ provider: ] module: ] func %s]\n"
133 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
134 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
135 predact, predact, predact, predact, predact);
137 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
138 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
140 (void) fprintf(fp, "\n"
141 "\t-32 generate 32-bit D programs and ELF files\n"
142 "\t-64 generate 64-bit D programs and ELF files\n\n"
143 "\t-a claim anonymous tracing state\n"
144 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
145 "\t-b set trace buffer size\n"
146 "\t-c run specified command and exit upon its completion\n"
147 "\t-C run cpp(1) preprocessor on script files\n"
148 "\t-D define symbol when invoking preprocessor\n"
149 "\t-e exit after compiling request but prior to enabling probes\n"
150 "\t-f enable or list probes matching the specified function name\n"
151 "\t-F coalesce trace output by function\n"
152 "\t-G generate an ELF file containing embedded dtrace program\n"
153 "\t-h generate a header file with definitions for static probes\n"
154 "\t-H print included files when invoking preprocessor\n"
155 "\t-i enable or list probes matching the specified probe id\n"
156 "\t-I add include directory to preprocessor search path\n"
157 "\t-l list probes matching specified criteria\n"
158 "\t-L add library directory to library search path\n"
159 "\t-m enable or list probes matching the specified module name\n"
160 "\t-n enable or list probes matching the specified probe name\n"
161 "\t-o set output file\n"
162 "\t-p grab specified process-ID and cache its symbol tables\n"
163 "\t-P enable or list probes matching the specified provider name\n"
164 "\t-q set quiet mode (only output explicitly traced data)\n"
165 "\t-s enable or list probes according to the specified D script\n"
166 "\t-S print D compiler intermediate code\n"
167 "\t-U undefine symbol when invoking preprocessor\n"
168 "\t-v set verbose mode (report stability attributes, arguments)\n"
169 "\t-V report DTrace API version\n"
170 "\t-w permit destructive actions\n"
171 "\t-x enable or modify compiler and tracing options\n"
172 "\t-X specify ISO C conformance settings for preprocessor\n"
173 "\t-Z permit probe descriptions that match zero probes\n");
179 verror(const char *fmt, va_list ap)
183 (void) fprintf(stderr, "%s: ", g_pname);
184 (void) vfprintf(stderr, fmt, ap);
186 if (fmt[strlen(fmt) - 1] != '\n')
187 (void) fprintf(stderr, ": %s\n", strerror(error));
192 fatal(const char *fmt, ...)
201 * Close the DTrace handle to ensure that any controlled processes are
202 * correctly restored and continued.
212 dfatal(const char *fmt, ...)
214 #if !defined(illumos) && defined(NEED_ERRLOC)
215 char *p_errfile = NULL;
222 (void) fprintf(stderr, "%s: ", g_pname);
224 (void) vfprintf(stderr, fmt, ap);
228 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
229 (void) fprintf(stderr, ": %s\n",
230 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
231 } else if (fmt == NULL) {
232 (void) fprintf(stderr, "%s\n",
233 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
235 #if !defined(illumos) && defined(NEED_ERRLOC)
236 dt_get_errloc(g_dtp, &p_errfile, &errline);
237 if (p_errfile != NULL)
238 printf("File '%s', line %d\n", p_errfile, errline);
242 * Close the DTrace handle to ensure that any controlled processes are
243 * correctly restored and continued.
252 error(const char *fmt, ...)
263 notice(const char *fmt, ...)
268 return; /* -q or quiet pragma suppresses notice()s */
277 oprintf(const char *fmt, ...)
286 n = vfprintf(g_ofp, fmt, ap);
290 if (errno != EINTR) {
291 fatal("failed to write to %s",
292 g_ofile ? g_ofile : "<stdout>");
301 const char *ws = "\f\n\r\t\v ";
302 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
309 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
320 dof_prune(const char *fname)
323 size_t sz, i, j, mark, len;
327 if ((fd = open(fname, O_RDONLY)) == -1) {
329 * This is okay only if the file doesn't exist at all.
332 fatal("failed to open %s", fname);
336 if (fstat(fd, &sbuf) == -1)
337 fatal("failed to fstat %s", fname);
339 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
340 fatal("failed to allocate memory for %s", fname);
342 if (read(fd, buf, sz) != sz)
343 fatal("failed to read %s", fname);
348 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
349 fatal("failed to open %s for writing", fname);
351 len = strlen("dof-data-");
353 for (mark = 0, i = 0; i < sz; i++) {
354 if (strncmp(&buf[i], "dof-data-", len) != 0)
358 * This is only a match if it's in the 0th column.
360 if (i != 0 && buf[i - 1] != '\n')
364 error("cleaned up old anonymous "
365 "enabling in %s\n", fname);
369 * We have a match. First write out our data up until now.
372 if (write(fd, &buf[mark], i - mark) != i - mark)
373 fatal("failed to write to %s", fname);
377 * Now scan forward until we scan past a newline.
379 for (j = i; j < sz && buf[j] != '\n'; j++)
385 if ((mark = j + 1) >= sz)
392 if (write(fd, &buf[mark], sz - mark) != sz - mark)
393 fatal("failed to write to %s", fname);
402 etcsystem_prune(void)
406 char *buf, *start, *end;
408 char *fname = g_etcfile, *tmpname;
410 if ((fd = open(fname, O_RDONLY)) == -1)
411 fatal("failed to open %s", fname);
413 if (fstat(fd, &sbuf) == -1)
414 fatal("failed to fstat %s", fname);
416 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
417 fatal("failed to allocate memory for %s", fname);
419 if (read(fd, buf, sz) != sz)
420 fatal("failed to read %s", fname);
425 if ((start = strstr(buf, g_etcbegin)) == NULL)
428 if (strlen(buf) != sz) {
429 fatal("embedded nul byte in %s; manual repair of %s "
430 "required\n", fname, fname);
433 if (strstr(start + 1, g_etcbegin) != NULL) {
434 fatal("multiple start sentinels in %s; manual repair of %s "
435 "required\n", fname, fname);
438 if ((end = strstr(buf, g_etcend)) == NULL) {
439 fatal("missing end sentinel in %s; manual repair of %s "
440 "required\n", fname, fname);
444 fatal("end sentinel preceeds start sentinel in %s; manual "
445 "repair of %s required\n", fname, fname);
448 end += strlen(g_etcend) + 1;
449 bcopy(end, start, strlen(end) + 1);
451 tmpname = alloca(sz = strlen(fname) + 80);
452 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
454 if ((fd = open(tmpname,
455 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
456 fatal("failed to create %s", tmpname);
458 if (write(fd, buf, strlen(buf)) < strlen(buf)) {
459 (void) unlink(tmpname);
460 fatal("failed to write to %s", tmpname);
465 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
466 (void) unlink(tmpname);
467 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
468 (int)sbuf.st_uid, (int)sbuf.st_gid);
471 if (rename(tmpname, fname) == -1)
472 fatal("rename of %s to %s failed", tmpname, fname);
474 error("cleaned up forceload directives in %s\n", fname);
482 const char *mods[20];
485 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
486 fatal("failed to open output file '%s'", g_ofile);
488 oprintf("%s\n", g_etcbegin);
490 for (line = 0; g_etc[line] != NULL; line++)
491 oprintf("%s\n", g_etc[line]);
493 nmods = dtrace_provider_modules(g_dtp, mods,
494 sizeof (mods) / sizeof (char *) - 1);
496 if (nmods >= sizeof (mods) / sizeof (char *))
497 fatal("unexpectedly large number of modules!");
499 mods[nmods++] = "dtrace";
501 for (line = 0; line < nmods; line++)
502 oprintf("forceload: drv/%s\n", mods[line]);
504 oprintf("%s\n", g_etcend);
506 if (fclose(g_ofp) == EOF)
507 fatal("failed to close output file '%s'", g_ofile);
509 error("added forceload directives to %s\n", g_ofile);
514 print_probe_info(const dtrace_probeinfo_t *p)
520 oprintf("\n\tProbe Description Attributes\n");
522 oprintf("\t\tIdentifier Names: %s\n",
523 dtrace_stability_name(p->dtp_attr.dtat_name));
524 oprintf("\t\tData Semantics: %s\n",
525 dtrace_stability_name(p->dtp_attr.dtat_data));
526 oprintf("\t\tDependency Class: %s\n",
527 dtrace_class_name(p->dtp_attr.dtat_class));
529 oprintf("\n\tArgument Attributes\n");
531 oprintf("\t\tIdentifier Names: %s\n",
532 dtrace_stability_name(p->dtp_arga.dtat_name));
533 oprintf("\t\tData Semantics: %s\n",
534 dtrace_stability_name(p->dtp_arga.dtat_data));
535 oprintf("\t\tDependency Class: %s\n",
536 dtrace_class_name(p->dtp_arga.dtat_class));
538 oprintf("\n\tArgument Types\n");
540 for (i = 0; i < p->dtp_argc; i++) {
541 if (p->dtp_argv[i].dtt_flags & DTT_FL_USER)
545 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
546 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
547 (void) strlcpy(buf, "(unknown)", sizeof (buf));
548 oprintf("\t\targs[%d]: %s%s\n", i, user, buf);
551 if (p->dtp_argc == 0)
552 oprintf("\t\tNone\n");
559 info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
560 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
562 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
563 dtrace_probedesc_t *pdp = &edp->dted_probe;
564 dtrace_probeinfo_t p;
569 oprintf("\n%s:%s:%s:%s\n",
570 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
572 if (dtrace_probe_info(dtp, pdp, &p) == 0)
573 print_probe_info(&p);
580 * Execute the specified program by enabling the corresponding instrumentation.
581 * If -e has been specified, we get the program info but do not enable it. If
582 * -v has been specified, we print a stability report for the program.
585 exec_prog(const dtrace_cmd_t *dcp)
587 dtrace_ecbdesc_t *last = NULL;
588 dtrace_proginfo_t dpi;
591 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
592 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
593 dfatal("failed to enable '%s'", dcp->dc_name);
595 notice("%s '%s' matched %u probe%s\n",
596 dcp->dc_desc, dcp->dc_name,
597 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
601 oprintf("\nStability attributes for %s %s:\n",
602 dcp->dc_desc, dcp->dc_name);
604 oprintf("\n\tMinimum Probe Description Attributes\n");
605 oprintf("\t\tIdentifier Names: %s\n",
606 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
607 oprintf("\t\tData Semantics: %s\n",
608 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
609 oprintf("\t\tDependency Class: %s\n",
610 dtrace_class_name(dpi.dpi_descattr.dtat_class));
612 oprintf("\n\tMinimum Statement Attributes\n");
614 oprintf("\t\tIdentifier Names: %s\n",
615 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
616 oprintf("\t\tData Semantics: %s\n",
617 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
618 oprintf("\t\tDependency Class: %s\n",
619 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
622 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
623 (dtrace_stmt_f *)info_stmt, &last);
628 g_total += dpi.dpi_matches;
632 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
633 * storing in a driver.conf(4) file associated with the dtrace driver.
636 anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
638 const uchar_t *p, *q;
641 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
644 q = p + dof->dofh_loadsz;
647 oprintf("dof-data-%d=0x%x", n, *p++);
650 oprintf(",0x%x", *p++);
655 * On FreeBSD, the DOF data is handled as a kernel environment (kenv)
656 * string. We use two hex characters per DOF byte.
658 oprintf("dof-data-%d=%02x", n, *p++);
661 oprintf("%02x", *p++);
666 dtrace_dof_destroy(g_dtp, dof);
670 * Link the specified D program in DOF form into an ELF file for use in either
671 * helpers, userland provider definitions, or both. If -o was specified, that
672 * path is used as the output file name. If -o wasn't specified and the input
673 * program is from a script whose name is %.d, use basename(%.o) as the output
674 * file name. Otherwise we use "d.out" as the default output file name.
677 link_prog(dtrace_cmd_t *dcp)
681 if (g_cmdc == 1 && g_ofile != NULL) {
682 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
683 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
684 strcmp(p, ".d") == 0) {
685 p[0] = '\0'; /* strip .d suffix */
686 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
687 "%s.o", basename(dcp->dc_arg));
688 } else if (g_cmdc > 1) {
689 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
690 "d.out.%td", dcp - g_cmdv);
692 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
696 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
697 dcp->dc_ofile, g_objc, g_objv) != 0)
698 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
703 list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
705 dtrace_probeinfo_t p;
707 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
708 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
710 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
711 print_probe_info(&p);
721 list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
722 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
724 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
729 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
730 error("failed to match %s:%s:%s:%s: %s\n",
731 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
732 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
733 dtrace_errmsg(dtp, dtrace_errno(dtp)));
741 * List the probes corresponding to the specified program by iterating over
742 * each statement and then matching probes to the statement probe descriptions.
745 list_prog(const dtrace_cmd_t *dcp)
747 dtrace_ecbdesc_t *last = NULL;
749 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
750 (dtrace_stmt_f *)list_stmt, &last);
754 compile_file(dtrace_cmd_t *dcp)
759 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
760 fatal("failed to open %s", dcp->dc_arg);
763 g_argv[0] = dcp->dc_arg;
765 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
766 g_cflags, g_argc, g_argv)) == NULL)
767 dfatal("failed to compile script %s", dcp->dc_arg);
772 dcp->dc_desc = "script";
773 dcp->dc_name = dcp->dc_arg;
777 compile_str(dtrace_cmd_t *dcp)
781 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
782 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
783 dfatal("invalid probe specifier %s", dcp->dc_arg);
785 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
786 *p = '\0'; /* crop name for reporting */
788 dcp->dc_desc = "description";
789 dcp->dc_name = dcp->dc_arg;
794 prochandler(struct ps_prochandle *P, const char *msg, void *arg)
797 const psinfo_t *prp = Ppsinfo(P);
798 int pid = Pstatus(P)->pr_pid;
799 char name[SIG2STR_MAX];
801 int wstatus = proc_getwstat(P);
802 int pid = proc_getpid(P);
806 notice("pid %d: %s\n", pid, msg);
813 switch (proc_state(P)) {
818 * Ideally we would like to always report pr_wstat here, but it
819 * isn't possible given current /proc semantics. If we grabbed
820 * the process, Ppsinfo() will either fail or return a zeroed
821 * psinfo_t depending on how far the parent is in reaping it.
822 * When /proc provides a stable pr_wstat in the status file,
823 * this code can be improved by examining this new pr_wstat.
825 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
826 notice("pid %d terminated by %s\n", pid,
827 proc_signame(WTERMSIG(prp->pr_wstat),
828 name, sizeof (name)));
830 if (WIFSIGNALED(wstatus)) {
831 notice("pid %d terminated by %d\n", pid,
835 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
836 notice("pid %d exited with status %d\n",
837 pid, WEXITSTATUS(prp->pr_wstat));
839 } else if (WEXITSTATUS(wstatus) != 0) {
840 notice("pid %d exited with status %d\n",
841 pid, WEXITSTATUS(wstatus));
844 notice("pid %d has exited\n", pid);
850 notice("pid %d exec'd a set-id or unobservable program\n", pid);
858 errhandler(const dtrace_errdata_t *data, void *arg)
860 error(data->dteda_msg);
861 return (DTRACE_HANDLE_OK);
866 drophandler(const dtrace_dropdata_t *data, void *arg)
868 error(data->dtdda_msg);
869 return (DTRACE_HANDLE_OK);
874 setopthandler(const dtrace_setoptdata_t *data, void *arg)
876 if (strcmp(data->dtsda_option, "quiet") == 0)
877 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
879 if (strcmp(data->dtsda_option, "flowindent") == 0)
880 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
882 return (DTRACE_HANDLE_OK);
885 #define BUFDUMPHDR(hdr) \
886 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
888 #define BUFDUMPSTR(ptr, field) \
889 (void) printf("%s: %20s => ", g_pname, #field); \
890 if ((ptr)->field != NULL) { \
891 const char *c = (ptr)->field; \
892 (void) printf("\""); \
895 (void) printf("\\n"); \
899 (void) printf("%c", *c); \
900 } while (*c++ != '\0'); \
901 (void) printf("\"\n"); \
903 (void) printf("<NULL>\n"); \
906 #define BUFDUMPASSTR(ptr, field, str) \
907 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
909 #define BUFDUMP(ptr, field) \
910 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
911 (long long)(ptr)->field);
913 #define BUFDUMPPTR(ptr, field) \
914 (void) printf("%s: %20s => %s\n", g_pname, #field, \
915 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
919 bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
921 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
922 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
923 const dtrace_probedesc_t *pd;
924 uint32_t flags = bufdata->dtbda_flags;
925 char buf[512], *c = buf, *end = c + sizeof (buf);
932 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
933 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
934 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
935 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
936 { "???", UINT32_MAX },
940 if (bufdata->dtbda_probe != NULL) {
941 pd = bufdata->dtbda_probe->dtpda_pdesc;
942 } else if (agg != NULL) {
943 pd = agg->dtada_pdesc;
948 BUFDUMPHDR(">>> Called buffer handler");
951 BUFDUMPHDR(" dtrace_bufdata");
952 BUFDUMPSTR(bufdata, dtbda_buffered);
953 BUFDUMPPTR(bufdata, dtbda_probe);
954 BUFDUMPPTR(bufdata, dtbda_aggdata);
955 BUFDUMPPTR(bufdata, dtbda_recdesc);
957 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
960 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
961 if (!(flags & flagnames[i].value))
964 (void) snprintf(c, end - c,
965 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
967 flags &= ~flagnames[i].value;
971 (void) snprintf(c, end - c, ")");
973 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
977 BUFDUMPHDR(" dtrace_probedesc");
978 BUFDUMPSTR(pd, dtpd_provider);
979 BUFDUMPSTR(pd, dtpd_mod);
980 BUFDUMPSTR(pd, dtpd_func);
981 BUFDUMPSTR(pd, dtpd_name);
986 BUFDUMPHDR(" dtrace_recdesc");
987 BUFDUMP(rec, dtrd_action);
988 BUFDUMP(rec, dtrd_size);
992 int lim = rec->dtrd_size;
994 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
995 c = buf + strlen(buf);
997 if (lim > sizeof (uint64_t))
998 lim = sizeof (uint64_t);
1000 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
1002 for (i = 0; i < lim; i++) {
1003 (void) snprintf(c, end - c, "%s%02x",
1004 i == 0 ? "" : " ", *data++);
1008 (void) snprintf(c, end - c,
1009 "%s)", lim < rec->dtrd_size ? " ..." : "");
1010 BUFDUMPASSTR(rec, dtrd_offset, buf);
1012 BUFDUMP(rec, dtrd_offset);
1019 dtrace_aggdesc_t *desc = agg->dtada_desc;
1021 BUFDUMPHDR(" dtrace_aggdesc");
1022 BUFDUMPSTR(desc, dtagd_name);
1023 BUFDUMP(desc, dtagd_varid);
1024 BUFDUMP(desc, dtagd_id);
1025 BUFDUMP(desc, dtagd_nrecs);
1029 return (DTRACE_HANDLE_OK);
1034 chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1036 dtrace_actkind_t act;
1041 * We have processed the final record; output the newline if
1042 * we're not in quiet mode.
1047 return (DTRACE_CONSUME_NEXT);
1050 act = rec->dtrd_action;
1051 addr = (uintptr_t)data->dtpda_data;
1053 if (act == DTRACEACT_EXIT) {
1054 g_status = *((uint32_t *)addr);
1055 return (DTRACE_CONSUME_NEXT);
1058 return (DTRACE_CONSUME_THIS);
1063 chew(const dtrace_probedata_t *data, void *arg)
1065 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1066 processorid_t cpu = data->dtpda_cpu;
1071 return (DTRACE_CONSUME_ABORT);
1075 if (!g_flowindent) {
1077 oprintf("%3s %6s %32s\n",
1078 "CPU", "ID", "FUNCTION:NAME");
1081 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1086 if (!g_flowindent) {
1088 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1090 (void) snprintf(name, sizeof (name), "%s:%s",
1091 pd->dtpd_func, pd->dtpd_name);
1093 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1096 int indent = data->dtpda_indent;
1100 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1101 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1103 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1104 data->dtpda_prefix, pd->dtpd_func,
1107 len = indent + DTRACE_FUNCNAMELEN + 5;
1109 (void) snprintf(name, len, "%*s%s%s", indent, "",
1110 data->dtpda_prefix, pd->dtpd_func);
1113 oprintf("%3d %-41s ", cpu, name);
1116 return (DTRACE_CONSUME_THIS);
1127 dtrace_optval_t val;
1129 { "buffer size", "bufsize" },
1130 { "aggregation size", "aggsize" },
1131 { "speculation size", "specsize" },
1132 { "dynamic variable size", "dynvarsize" },
1135 { "cleaning rate", "cleanrate" },
1136 { "status rate", "statusrate" },
1140 for (i = 0; bufs[i].name != NULL; i++) {
1141 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1142 fatal("couldn't get option %s", bufs[i].optname);
1145 for (i = 0; rates[i].name != NULL; i++) {
1146 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1147 fatal("couldn't get option %s", rates[i].optname);
1150 if (dtrace_go(g_dtp) == -1)
1151 dfatal("could not enable tracing");
1153 for (i = 0; bufs[i].name != NULL; i++) {
1154 dtrace_optval_t j = 0, mul = 10;
1155 dtrace_optval_t nsize;
1157 if (bufs[i].val == DTRACEOPT_UNSET)
1160 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1162 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1165 if (nsize >= bufs[i].val - sizeof (uint64_t))
1168 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1171 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1172 error("%s lowered to %lld%c\n", bufs[i].name,
1173 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1175 error("%s lowered to %lld bytes\n", bufs[i].name,
1180 for (i = 0; rates[i].name != NULL; i++) {
1181 dtrace_optval_t nval;
1184 if (rates[i].val == DTRACEOPT_UNSET)
1187 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1189 if (nval == DTRACEOPT_UNSET || nval == 0)
1192 if (rates[i].val == nval)
1195 dir = nval > rates[i].val ? "reduced" : "increased";
1197 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1198 error("%s %s to %lld hz\n", rates[i].name, dir,
1199 (long long)NANOSEC / (long long)nval);
1203 if ((nval % NANOSEC) == 0) {
1204 error("%s %s to once every %lld seconds\n",
1206 (long long)nval / (long long)NANOSEC);
1210 error("%s %s to once every %lld nanoseconds\n",
1211 rates[i].name, dir, (long long)nval);
1227 installsighands(void)
1229 struct sigaction act, oact;
1231 (void) sigemptyset(&act.sa_mask);
1233 act.sa_handler = intr;
1235 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1236 (void) sigaction(SIGINT, &act, NULL);
1238 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1239 (void) sigaction(SIGTERM, &act, NULL);
1242 if (sigaction(SIGPIPE, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1243 (void) sigaction(SIGPIPE, &act, NULL);
1245 if (sigaction(SIGUSR1, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1246 (void) sigaction(SIGUSR1, &act, NULL);
1251 main(int argc, char *argv[])
1253 dtrace_bufdesc_t buf;
1254 dtrace_status_t status[2];
1255 dtrace_optval_t opt;
1259 int done = 0, mode = 0;
1262 struct ps_prochandle *P;
1265 g_pname = basename(argv[0]);
1268 return (usage(stderr));
1270 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1271 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1272 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1273 fatal("failed to allocate memory for arguments");
1275 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1276 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1278 bzero(status, sizeof (status));
1279 bzero(&buf, sizeof (buf));
1282 * Make an initial pass through argv[] processing any arguments that
1283 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1284 * We also accumulate arguments that are not affiliated with getopt
1285 * options into g_argv[], and abort if any invalid options are found.
1287 for (optind = 1; optind < argc; optind++) {
1288 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1291 if (strcmp(optarg, "2") != 0) {
1292 (void) fprintf(stderr,
1293 "%s: illegal option -- 3%s\n",
1295 return (usage(stderr));
1297 g_oflags &= ~DTRACE_O_LP64;
1298 g_oflags |= DTRACE_O_ILP32;
1302 if (strcmp(optarg, "4") != 0) {
1303 (void) fprintf(stderr,
1304 "%s: illegal option -- 6%s\n",
1306 return (usage(stderr));
1308 g_oflags &= ~DTRACE_O_ILP32;
1309 g_oflags |= DTRACE_O_LP64;
1313 g_grabanon++; /* also checked in pass 2 below */
1317 g_mode = DMODE_ANON;
1328 g_mode = DMODE_HEADER;
1329 g_oflags |= DTRACE_O_NODEV;
1330 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1336 g_mode = DMODE_LINK;
1337 g_oflags |= DTRACE_O_NODEV;
1338 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1344 g_mode = DMODE_LIST;
1345 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1350 g_mode = DMODE_VERS;
1355 if (strchr(DTRACE_OPTSTR, c) == NULL)
1356 return (usage(stderr));
1361 g_argv[g_argc++] = argv[optind];
1365 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1366 "can be specified at a time\n", g_pname);
1370 if (g_mode == DMODE_VERS)
1371 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1374 * If we're in linker mode and the data model hasn't been specified,
1375 * we try to guess the appropriate setting by examining the object
1376 * files. We ignore certain errors since we'll catch them later when
1377 * we actually process the object files.
1379 if (g_mode == DMODE_LINK &&
1380 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1381 elf_version(EV_CURRENT) != EV_NONE) {
1386 for (i = 1; i < g_argc; i++) {
1387 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1390 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1395 if (elf_kind(elf) != ELF_K_ELF ||
1396 gelf_getehdr(elf, &ehdr) == NULL) {
1398 (void) elf_end(elf);
1403 (void) elf_end(elf);
1405 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1406 if (g_oflags & DTRACE_O_ILP32) {
1407 fatal("can't mix 32-bit and 64-bit "
1410 g_oflags |= DTRACE_O_LP64;
1411 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1412 if (g_oflags & DTRACE_O_LP64) {
1413 fatal("can't mix 32-bit and 64-bit "
1416 g_oflags |= DTRACE_O_ILP32;
1424 * Open libdtrace. If we are not actually going to be enabling any
1425 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1427 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1428 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1429 g_oflags |= DTRACE_O_NODEV;
1433 fatal("failed to initialize dtrace: %s\n",
1434 dtrace_errmsg(NULL, err));
1437 #if defined(__i386__)
1438 /* XXX The 32-bit seems to need more buffer space by default -sson */
1439 (void) dtrace_setopt(g_dtp, "bufsize", "12m");
1440 (void) dtrace_setopt(g_dtp, "aggsize", "12m");
1442 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1443 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1445 (void) dtrace_setopt(g_dtp, "temporal", "yes");
1448 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1449 * references to undefined symbols to remain as unresolved relocations.
1450 * If -A is specified, enable -xlink=primary to permit static linking
1451 * only to kernel symbols that are defined in a primary kernel module.
1453 if (g_mode == DMODE_LINK) {
1454 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1455 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1458 * Use the remaining arguments as the list of object files
1459 * when in linker mode.
1461 g_objc = g_argc - 1;
1462 g_objv = g_argv + 1;
1465 * We still use g_argv[0], the name of the executable.
1468 } else if (g_mode == DMODE_ANON)
1469 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1472 * Now that we have libdtrace open, make a second pass through argv[]
1473 * to perform any dtrace_setopt() calls and change any compiler flags.
1474 * We also accumulate any program specifications into our g_cmdv[] at
1475 * this time; these will compiled as part of the fourth processing pass.
1477 for (optind = 1; optind < argc; optind++) {
1478 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1481 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1482 dfatal("failed to set -a");
1486 if (dtrace_setopt(g_dtp,
1487 "bufsize", optarg) != 0)
1488 dfatal("failed to set -b %s", optarg);
1496 g_cflags |= DTRACE_C_CPP;
1500 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1501 dfatal("failed to set -D %s", optarg);
1505 dcp = &g_cmdv[g_cmdc++];
1506 dcp->dc_func = compile_str;
1507 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1508 dcp->dc_arg = optarg;
1512 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1513 dfatal("failed to set -F");
1517 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1518 dfatal("failed to set -H");
1522 dcp = &g_cmdv[g_cmdc++];
1523 dcp->dc_func = compile_str;
1524 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1525 dcp->dc_arg = optarg;
1529 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1530 dfatal("failed to set -I %s", optarg);
1534 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1535 dfatal("failed to set -L %s", optarg);
1539 dcp = &g_cmdv[g_cmdc++];
1540 dcp->dc_func = compile_str;
1541 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1542 dcp->dc_arg = optarg;
1546 dcp = &g_cmdv[g_cmdc++];
1547 dcp->dc_func = compile_str;
1548 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1549 dcp->dc_arg = optarg;
1553 dcp = &g_cmdv[g_cmdc++];
1554 dcp->dc_func = compile_str;
1555 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1556 dcp->dc_arg = optarg;
1560 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1561 dfatal("failed to set -q");
1569 dcp = &g_cmdv[g_cmdc++];
1570 dcp->dc_func = compile_file;
1571 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1572 dcp->dc_arg = optarg;
1576 g_cflags |= DTRACE_C_DIFV;
1580 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1581 dfatal("failed to set -U %s", optarg);
1589 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1590 dfatal("failed to set -w");
1594 if ((p = strchr(optarg, '=')) != NULL)
1597 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1598 dfatal("failed to set -x %s", optarg);
1602 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1603 dfatal("failed to set -X %s", optarg);
1607 g_cflags |= DTRACE_C_ZDEFS;
1611 if (strchr(DTRACE_OPTSTR, c) == NULL)
1612 return (usage(stderr));
1617 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1618 (void) fprintf(stderr, "%s: -B not valid in combination"
1619 " with [-AGl] options\n", g_pname);
1623 if (g_ofp == NULL && g_ofile != NULL) {
1624 (void) fprintf(stderr, "%s: -B not valid in combination"
1625 " with -o option\n", g_pname);
1630 * In our third pass we handle any command-line options related to
1631 * grabbing or creating victim processes. The behavior of these calls
1632 * may been affected by any library options set by the second pass.
1634 for (optind = 1; optind < argc; optind++) {
1635 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != -1) {
1638 if ((v = make_argv(optarg)) == NULL)
1639 fatal("failed to allocate memory");
1641 P = dtrace_proc_create(g_dtp, v[0], v, NULL, NULL);
1643 dfatal(NULL); /* dtrace_errmsg() only */
1651 pid = strtol(optarg, &p, 10);
1653 if (errno != 0 || p == optarg || p[0] != '\0')
1654 fatal("invalid pid: %s\n", optarg);
1656 P = dtrace_proc_grab(g_dtp, pid, 0);
1658 dfatal(NULL); /* dtrace_errmsg() only */
1667 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1668 * each string or file specification into a compiled program structure.
1670 for (i = 0; i < g_cmdc; i++)
1671 g_cmdv[i].dc_func(&g_cmdv[i]);
1673 if (g_mode != DMODE_LIST) {
1674 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1675 dfatal("failed to establish error handler");
1677 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1678 dfatal("failed to establish drop handler");
1680 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1681 dfatal("failed to establish proc handler");
1683 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1684 dfatal("failed to establish setopt handler");
1686 if (g_ofp == NULL &&
1687 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1688 dfatal("failed to establish buffered handler");
1691 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1692 g_flowindent = opt != DTRACEOPT_UNSET;
1694 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1695 g_grabanon = opt != DTRACEOPT_UNSET;
1697 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1698 g_quiet = opt != DTRACEOPT_UNSET;
1701 * Now make a fifth and final pass over the options that have been
1702 * turned into programs and saved in g_cmdv[], performing any mode-
1703 * specific processing. If g_mode is DMODE_EXEC, we will break out
1704 * of the switch() and continue on to the data processing loop. For
1705 * other modes, we will exit dtrace once mode-specific work is done.
1709 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1710 fatal("failed to open output file '%s'", g_ofile);
1712 for (i = 0; i < g_cmdc; i++)
1713 exec_prog(&g_cmdv[i]);
1715 if (done && !g_grabanon) {
1716 dtrace_close(g_dtp);
1722 if (g_ofile == NULL)
1724 g_ofile = "/kernel/drv/dtrace.conf";
1727 * On FreeBSD, anonymous DOF data is written to
1728 * the DTrace DOF file that the boot loader will
1729 * read if booting with the DTrace option.
1731 g_ofile = "/boot/dtrace.dof";
1734 dof_prune(g_ofile); /* strip out any old DOF directives */
1736 etcsystem_prune(); /* string out any forceload directives */
1740 dtrace_close(g_dtp);
1744 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1745 fatal("failed to open output file '%s'", g_ofile);
1747 for (i = 0; i < g_cmdc; i++) {
1748 anon_prog(&g_cmdv[i],
1749 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1753 * Dump out the DOF corresponding to the error handler and the
1754 * current options as the final DOF property in the .conf file.
1756 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1757 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1759 if (fclose(g_ofp) == EOF)
1760 fatal("failed to close output file '%s'", g_ofile);
1763 * These messages would use notice() rather than error(), but
1764 * we don't want them suppressed when -A is run on a D program
1765 * that itself contains a #pragma D option quiet.
1767 error("saved anonymous enabling in %s\n", g_ofile);
1770 error("run update_drv(1M) or reboot to enable changes\n");
1773 dtrace_close(g_dtp);
1778 (void) fprintf(stderr, "%s: -G requires one or more "
1779 "scripts or enabling options\n", g_pname);
1780 dtrace_close(g_dtp);
1784 for (i = 0; i < g_cmdc; i++)
1785 link_prog(&g_cmdv[i]);
1787 if (g_cmdc > 1 && g_ofile != NULL) {
1788 char **objv = alloca(g_cmdc * sizeof (char *));
1790 for (i = 0; i < g_cmdc; i++)
1791 objv[i] = g_cmdv[i].dc_ofile;
1793 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1794 g_ofile, g_cmdc, objv) != 0)
1795 dfatal(NULL); /* dtrace_errmsg() only */
1798 dtrace_close(g_dtp);
1802 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1803 fatal("failed to open output file '%s'", g_ofile);
1807 oprintf("%5s %10s %17s %33s %s\n",
1808 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1810 for (i = 0; i < g_cmdc; i++)
1811 list_prog(&g_cmdv[i]);
1814 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1816 dtrace_close(g_dtp);
1821 (void) fprintf(stderr, "%s: -h requires one or more "
1822 "scripts or enabling options\n", g_pname);
1823 dtrace_close(g_dtp);
1827 if (g_ofile == NULL) {
1831 (void) fprintf(stderr, "%s: -h requires an "
1832 "output file if multiple scripts are "
1833 "specified\n", g_pname);
1834 dtrace_close(g_dtp);
1838 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1839 strcmp(p, ".d") != 0) {
1840 (void) fprintf(stderr, "%s: -h requires an "
1841 "output file if no scripts are "
1842 "specified\n", g_pname);
1843 dtrace_close(g_dtp);
1847 p[0] = '\0'; /* strip .d suffix */
1848 g_ofile = p = g_cmdv[0].dc_ofile;
1849 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1850 "%s.h", basename(g_cmdv[0].dc_arg));
1853 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1854 fatal("failed to open header file '%s'", g_ofile);
1856 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1858 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1859 fclose(g_ofp) == EOF)
1860 dfatal("failed to create header file %s", g_ofile);
1862 dtrace_close(g_dtp);
1867 * If -a and -Z were not specified and no probes have been matched, no
1868 * probe criteria was specified on the command line and we abort.
1870 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1871 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1874 * Start tracing. Once we dtrace_go(), reload any options that affect
1875 * our globals in case consuming anonymous state has changed them.
1879 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1880 g_flowindent = opt != DTRACEOPT_UNSET;
1882 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1883 g_grabanon = opt != DTRACEOPT_UNSET;
1885 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1886 g_quiet = opt != DTRACEOPT_UNSET;
1888 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1889 if (opt != DTRACEOPT_UNSET)
1890 notice("allowing destructive actions\n");
1895 * Now that tracing is active and we are ready to consume trace data,
1896 * continue any grabbed or created processes, setting them running
1897 * using the /proc control mechanism inside of libdtrace.
1899 for (i = 0; i < g_psc; i++)
1900 dtrace_proc_continue(g_dtp, g_psv[i]);
1902 g_pslive = g_psc; /* count for prochandler() */
1905 if (!g_intr && !done)
1906 dtrace_sleep(g_dtp);
1910 * Output a newline just to make the output look
1911 * slightly cleaner. Note that we do this even in
1918 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
1920 if (dtrace_stop(g_dtp) == -1)
1921 dfatal("couldn't stop tracing");
1924 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
1925 case DTRACE_WORKSTATUS_DONE:
1928 case DTRACE_WORKSTATUS_OKAY:
1931 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
1932 dfatal("processing aborted");
1935 if (g_ofp != NULL && fflush(g_ofp) == EOF)
1942 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
1943 dtrace_errno(g_dtp) != EINTR)
1944 dfatal("failed to print aggregations");
1947 dtrace_close(g_dtp);