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>
49 typedef struct dtrace_cmd
{
50 void (*dc_func
)(struct dtrace_cmd
*); /* function to compile arg */
51 dtrace_probespec_t dc_spec
; /* probe specifier context */
52 char *dc_arg
; /* argument from main argv */
53 const char *dc_name
; /* name for error messages */
54 const char *dc_desc
; /* desc for error messages */
55 dtrace_prog_t
*dc_prog
; /* program compiled from arg */
56 char dc_ofile
[PATH_MAX
]; /* derived output file name */
59 #define DMODE_VERS 0 /* display version information and exit (-V) */
60 #define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
61 #define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
62 #define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
63 #define DMODE_LIST 4 /* compile program and list probes (-l) */
64 #define DMODE_HEADER 5 /* compile program for headergen (-h) */
70 static const char DTRACE_OPTSTR
[] =
71 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
77 static dtrace_cmd_t
*g_cmdv
;
79 static struct ps_prochandle
**g_psv
;
84 static int g_flowindent
;
86 static int g_impatient
;
92 static int g_exec
= 1;
93 static int g_mode
= DMODE_EXEC
;
94 static int g_status
= E_SUCCESS
;
95 static int g_grabanon
= 0;
96 static const char *g_ofile
= NULL
;
98 static dtrace_hdl_t
*g_dtp
;
99 static char *g_etcfile
= "/etc/system";
100 static const char *g_etcbegin
= "* vvvv Added by DTrace";
101 static const char *g_etcend
= "* ^^^^ Added by DTrace";
103 static const char *g_etc
[] = {
105 "* The following forceload directives were added by dtrace(1M) to allow for",
106 "* tracing during boot. If these directives are removed, the system will",
107 "* continue to function, but tracing will not occur during boot as desired.",
108 "* To remove these directives (and this block comment) automatically, run",
109 "* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
110 "* chapter of the Solaris Dynamic Tracing Guide for details.",
117 static const char predact
[] = "[[ predicate ] action ]";
119 (void) fprintf(fp
, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
120 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
121 "[-o output] [-p pid] [-s script] [-U name]\n\t"
122 "[-x opt[=val]] [-X a|c|s|t]\n\n"
123 "\t[-P provider %s]\n"
124 "\t[-m [ provider: ] module %s]\n"
125 "\t[-f [[ provider: ] module: ] func %s]\n"
126 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
127 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname
,
128 predact
, predact
, predact
, predact
, predact
);
130 (void) fprintf(fp
, "\tpredicate -> '/' D-expression '/'\n");
131 (void) fprintf(fp
, "\t action -> '{' D-statements '}'\n");
133 (void) fprintf(fp
, "\n"
134 "\t-32 generate 32-bit D programs and ELF files\n"
135 "\t-64 generate 64-bit D programs and ELF files\n\n"
136 "\t-a claim anonymous tracing state\n"
137 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
138 "\t-b set trace buffer size\n"
139 "\t-c run specified command and exit upon its completion\n"
140 "\t-C run cpp(1) preprocessor on script files\n"
141 "\t-D define symbol when invoking preprocessor\n"
142 "\t-e exit after compiling request but prior to enabling probes\n"
143 "\t-f enable or list probes matching the specified function name\n"
144 "\t-F coalesce trace output by function\n"
145 "\t-G generate an ELF file containing embedded dtrace program\n"
146 "\t-h generate a header file with definitions for static probes\n"
147 "\t-H print included files when invoking preprocessor\n"
148 "\t-i enable or list probes matching the specified probe id\n"
149 "\t-I add include directory to preprocessor search path\n"
150 "\t-l list probes matching specified criteria\n"
151 "\t-L add library directory to library search path\n"
152 "\t-m enable or list probes matching the specified module name\n"
153 "\t-n enable or list probes matching the specified probe name\n"
154 "\t-o set output file\n"
155 "\t-p grab specified process-ID and cache its symbol tables\n"
156 "\t-P enable or list probes matching the specified provider name\n"
157 "\t-q set quiet mode (only output explicitly traced data)\n"
158 "\t-s enable or list probes according to the specified D script\n"
159 "\t-S print D compiler intermediate code\n"
160 "\t-U undefine symbol when invoking preprocessor\n"
161 "\t-v set verbose mode (report stability attributes, arguments)\n"
162 "\t-V report DTrace API version\n"
163 "\t-w permit destructive actions\n"
164 "\t-x enable or modify compiler and tracing options\n"
165 "\t-X specify ISO C conformance settings for preprocessor\n"
166 "\t-Z permit probe descriptions that match zero probes\n");
172 verror(const char *fmt
, va_list ap
)
176 (void) fprintf(stderr
, "%s: ", g_pname
);
177 (void) vfprintf(stderr
, fmt
, ap
);
179 if (fmt
[strlen(fmt
) - 1] != '\n')
180 (void) fprintf(stderr
, ": %s\n", strerror(error
));
185 fatal(const char *fmt
, ...)
198 dfatal(const char *fmt
, ...)
204 (void) fprintf(stderr
, "%s: ", g_pname
);
206 (void) vfprintf(stderr
, fmt
, ap
);
210 if (fmt
!= NULL
&& fmt
[strlen(fmt
) - 1] != '\n') {
211 (void) fprintf(stderr
, ": %s\n",
212 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
213 } else if (fmt
== NULL
) {
214 (void) fprintf(stderr
, "%s\n",
215 dtrace_errmsg(g_dtp
, dtrace_errno(g_dtp
)));
219 * Close the DTrace handle to ensure that any controlled processes are
220 * correctly restored and continued.
229 error(const char *fmt
, ...)
240 notice(const char *fmt
, ...)
245 return; /* -q or quiet pragma suppresses notice()s */
254 oprintf(const char *fmt
, ...)
263 n
= vfprintf(g_ofp
, fmt
, ap
);
267 if (errno
!= EINTR
) {
268 fatal("failed to write to %s",
269 g_ofile
? g_ofile
: "<stdout>");
278 const char *ws
= "\f\n\r\t\v ";
279 char **argv
= malloc(sizeof (char *) * (strlen(s
) / 2 + 1));
286 for (p
= strtok(s
, ws
); p
!= NULL
; p
= strtok(NULL
, ws
))
297 dof_prune(const char *fname
)
300 size_t sz
, i
, j
, mark
, len
;
304 if ((fd
= open(fname
, O_RDONLY
)) == -1) {
306 * This is okay only if the file doesn't exist at all.
309 fatal("failed to open %s", fname
);
313 if (fstat(fd
, &sbuf
) == -1)
314 fatal("failed to fstat %s", fname
);
316 if ((buf
= malloc((sz
= sbuf
.st_size
) + 1)) == NULL
)
317 fatal("failed to allocate memory for %s", fname
);
319 if (read(fd
, buf
, sz
) != sz
)
320 fatal("failed to read %s", fname
);
325 if ((fd
= open(fname
, O_WRONLY
| O_TRUNC
)) == -1)
326 fatal("failed to open %s for writing", fname
);
328 len
= strlen("dof-data-");
330 for (mark
= 0, i
= 0; i
< sz
; i
++) {
331 if (strncmp(&buf
[i
], "dof-data-", len
) != 0)
335 * This is only a match if it's in the 0th column.
337 if (i
!= 0 && buf
[i
- 1] != '\n')
341 error("cleaned up old anonymous "
342 "enabling in %s\n", fname
);
346 * We have a match. First write out our data up until now.
349 if (write(fd
, &buf
[mark
], i
- mark
) != i
- mark
)
350 fatal("failed to write to %s", fname
);
354 * Now scan forward until we scan past a newline.
356 for (j
= i
; j
< sz
&& buf
[j
] != '\n'; j
++)
362 if ((mark
= j
+ 1) >= sz
)
369 if (write(fd
, &buf
[mark
], sz
- mark
) != sz
- mark
)
370 fatal("failed to write to %s", fname
);
378 etcsystem_prune(void)
382 char *buf
, *start
, *end
;
384 char *fname
= g_etcfile
, *tmpname
;
386 if ((fd
= open(fname
, O_RDONLY
)) == -1)
387 fatal("failed to open %s", fname
);
389 if (fstat(fd
, &sbuf
) == -1)
390 fatal("failed to fstat %s", fname
);
392 if ((buf
= malloc((sz
= sbuf
.st_size
) + 1)) == NULL
)
393 fatal("failed to allocate memory for %s", fname
);
395 if (read(fd
, buf
, sz
) != sz
)
396 fatal("failed to read %s", fname
);
401 if ((start
= strstr(buf
, g_etcbegin
)) == NULL
)
404 if (strlen(buf
) != sz
) {
405 fatal("embedded nul byte in %s; manual repair of %s "
406 "required\n", fname
, fname
);
409 if (strstr(start
+ 1, g_etcbegin
) != NULL
) {
410 fatal("multiple start sentinels in %s; manual repair of %s "
411 "required\n", fname
, fname
);
414 if ((end
= strstr(buf
, g_etcend
)) == NULL
) {
415 fatal("missing end sentinel in %s; manual repair of %s "
416 "required\n", fname
, fname
);
420 fatal("end sentinel preceeds start sentinel in %s; manual "
421 "repair of %s required\n", fname
, fname
);
424 end
+= strlen(g_etcend
) + 1;
425 bcopy(end
, start
, strlen(end
) + 1);
427 tmpname
= alloca(sz
= strlen(fname
) + 80);
428 (void) snprintf(tmpname
, sz
, "%s.dtrace.%d", fname
, getpid());
430 if ((fd
= open(tmpname
,
431 O_WRONLY
| O_CREAT
| O_EXCL
, sbuf
.st_mode
)) == -1)
432 fatal("failed to create %s", tmpname
);
434 if (write(fd
, buf
, strlen(buf
)) < strlen(buf
)) {
435 (void) unlink(tmpname
);
436 fatal("failed to write to %s", tmpname
);
441 if (chown(tmpname
, sbuf
.st_uid
, sbuf
.st_gid
) != 0) {
442 (void) unlink(tmpname
);
443 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname
,
444 (int)sbuf
.st_uid
, (int)sbuf
.st_gid
);
447 if (rename(tmpname
, fname
) == -1)
448 fatal("rename of %s to %s failed", tmpname
, fname
);
450 error("cleaned up forceload directives in %s\n", fname
);
458 const char *mods
[20];
461 if ((g_ofp
= fopen(g_ofile
= g_etcfile
, "a")) == NULL
)
462 fatal("failed to open output file '%s'", g_ofile
);
464 oprintf("%s\n", g_etcbegin
);
466 for (line
= 0; g_etc
[line
] != NULL
; line
++)
467 oprintf("%s\n", g_etc
[line
]);
469 nmods
= dtrace_provider_modules(g_dtp
, mods
,
470 sizeof (mods
) / sizeof (char *) - 1);
472 if (nmods
>= sizeof (mods
) / sizeof (char *))
473 fatal("unexpectedly large number of modules!");
475 mods
[nmods
++] = "dtrace";
477 for (line
= 0; line
< nmods
; line
++)
478 oprintf("forceload: drv/%s\n", mods
[line
]);
480 oprintf("%s\n", g_etcend
);
482 if (fclose(g_ofp
) == EOF
)
483 fatal("failed to close output file '%s'", g_ofile
);
485 error("added forceload directives to %s\n", g_ofile
);
489 print_probe_info(const dtrace_probeinfo_t
*p
)
495 oprintf("\n\tProbe Description Attributes\n");
497 oprintf("\t\tIdentifier Names: %s\n",
498 dtrace_stability_name(p
->dtp_attr
.dtat_name
));
499 oprintf("\t\tData Semantics: %s\n",
500 dtrace_stability_name(p
->dtp_attr
.dtat_data
));
501 oprintf("\t\tDependency Class: %s\n",
502 dtrace_class_name(p
->dtp_attr
.dtat_class
));
504 oprintf("\n\tArgument Attributes\n");
506 oprintf("\t\tIdentifier Names: %s\n",
507 dtrace_stability_name(p
->dtp_arga
.dtat_name
));
508 oprintf("\t\tData Semantics: %s\n",
509 dtrace_stability_name(p
->dtp_arga
.dtat_data
));
510 oprintf("\t\tDependency Class: %s\n",
511 dtrace_class_name(p
->dtp_arga
.dtat_class
));
513 oprintf("\n\tArgument Types\n");
515 for (i
= 0; i
< p
->dtp_argc
; i
++) {
516 if (p
->dtp_argv
[i
].dtt_flags
& DTT_FL_USER
)
520 if (ctf_type_name(p
->dtp_argv
[i
].dtt_ctfp
,
521 p
->dtp_argv
[i
].dtt_type
, buf
, sizeof (buf
)) == NULL
)
522 (void) strlcpy(buf
, "(unknown)", sizeof (buf
));
523 oprintf("\t\targs[%d]: %s%s\n", i
, user
, buf
);
526 if (p
->dtp_argc
== 0)
527 oprintf("\t\tNone\n");
534 info_stmt(dtrace_hdl_t
*dtp
, dtrace_prog_t
*pgp
,
535 dtrace_stmtdesc_t
*stp
, dtrace_ecbdesc_t
**last
)
537 dtrace_ecbdesc_t
*edp
= stp
->dtsd_ecbdesc
;
538 dtrace_probedesc_t
*pdp
= &edp
->dted_probe
;
539 dtrace_probeinfo_t p
;
544 oprintf("\n%s:%s:%s:%s\n",
545 pdp
->dtpd_provider
, pdp
->dtpd_mod
, pdp
->dtpd_func
, pdp
->dtpd_name
);
547 if (dtrace_probe_info(dtp
, pdp
, &p
) == 0)
548 print_probe_info(&p
);
555 * Execute the specified program by enabling the corresponding instrumentation.
556 * If -e has been specified, we get the program info but do not enable it. If
557 * -v has been specified, we print a stability report for the program.
560 exec_prog(const dtrace_cmd_t
*dcp
)
562 dtrace_ecbdesc_t
*last
= NULL
;
563 dtrace_proginfo_t dpi
;
566 dtrace_program_info(g_dtp
, dcp
->dc_prog
, &dpi
);
567 } else if (dtrace_program_exec(g_dtp
, dcp
->dc_prog
, &dpi
) == -1) {
568 dfatal("failed to enable '%s'", dcp
->dc_name
);
570 notice("%s '%s' matched %u probe%s\n",
571 dcp
->dc_desc
, dcp
->dc_name
,
572 dpi
.dpi_matches
, dpi
.dpi_matches
== 1 ? "" : "s");
576 oprintf("\nStability attributes for %s %s:\n",
577 dcp
->dc_desc
, dcp
->dc_name
);
579 oprintf("\n\tMinimum Probe Description Attributes\n");
580 oprintf("\t\tIdentifier Names: %s\n",
581 dtrace_stability_name(dpi
.dpi_descattr
.dtat_name
));
582 oprintf("\t\tData Semantics: %s\n",
583 dtrace_stability_name(dpi
.dpi_descattr
.dtat_data
));
584 oprintf("\t\tDependency Class: %s\n",
585 dtrace_class_name(dpi
.dpi_descattr
.dtat_class
));
587 oprintf("\n\tMinimum Statement Attributes\n");
589 oprintf("\t\tIdentifier Names: %s\n",
590 dtrace_stability_name(dpi
.dpi_stmtattr
.dtat_name
));
591 oprintf("\t\tData Semantics: %s\n",
592 dtrace_stability_name(dpi
.dpi_stmtattr
.dtat_data
));
593 oprintf("\t\tDependency Class: %s\n",
594 dtrace_class_name(dpi
.dpi_stmtattr
.dtat_class
));
597 (void) dtrace_stmt_iter(g_dtp
, dcp
->dc_prog
,
598 (dtrace_stmt_f
*)info_stmt
, &last
);
603 g_total
+= dpi
.dpi_matches
;
607 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
608 * storing in a driver.conf(4) file associated with the dtrace driver.
611 anon_prog(const dtrace_cmd_t
*dcp
, dof_hdr_t
*dof
, int n
)
613 const uchar_t
*p
, *q
;
616 dfatal("failed to create DOF image for '%s'", dcp
->dc_name
);
619 q
= p
+ dof
->dofh_loadsz
;
621 oprintf("dof-data-%d=0x%x", n
, *p
++);
624 oprintf(",0x%x", *p
++);
627 dtrace_dof_destroy(g_dtp
, dof
);
631 * Link the specified D program in DOF form into an ELF file for use in either
632 * helpers, userland provider definitions, or both. If -o was specified, that
633 * path is used as the output file name. If -o wasn't specified and the input
634 * program is from a script whose name is %.d, use basename(%.o) as the output
635 * file name. Otherwise we use "d.out" as the default output file name.
638 link_prog(dtrace_cmd_t
*dcp
)
642 if (g_cmdc
== 1 && g_ofile
!= NULL
) {
643 (void) strlcpy(dcp
->dc_ofile
, g_ofile
, sizeof (dcp
->dc_ofile
));
644 } else if ((p
= strrchr(dcp
->dc_arg
, '.')) != NULL
&&
645 strcmp(p
, ".d") == 0) {
646 p
[0] = '\0'; /* strip .d suffix */
647 (void) snprintf(dcp
->dc_ofile
, sizeof (dcp
->dc_ofile
),
648 "%s.o", basename(dcp
->dc_arg
));
650 (void) snprintf(dcp
->dc_ofile
, sizeof (dcp
->dc_ofile
),
651 g_cmdc
> 1 ? "%s.%d" : "%s", "d.out", (int)(dcp
- g_cmdv
));
654 if (dtrace_program_link(g_dtp
, dcp
->dc_prog
, DTRACE_D_PROBES
,
655 dcp
->dc_ofile
, g_objc
, g_objv
) != 0)
656 dfatal("failed to link %s %s", dcp
->dc_desc
, dcp
->dc_name
);
661 list_probe(dtrace_hdl_t
*dtp
, const dtrace_probedesc_t
*pdp
, void *arg
)
663 dtrace_probeinfo_t p
;
665 oprintf("%5d %10s %17s %33s %s\n", pdp
->dtpd_id
,
666 pdp
->dtpd_provider
, pdp
->dtpd_mod
, pdp
->dtpd_func
, pdp
->dtpd_name
);
668 if (g_verbose
&& dtrace_probe_info(dtp
, pdp
, &p
) == 0)
669 print_probe_info(&p
);
676 list_stmt(dtrace_hdl_t
*dtp
, dtrace_prog_t
*pgp
,
677 dtrace_stmtdesc_t
*stp
, dtrace_ecbdesc_t
**last
)
679 dtrace_ecbdesc_t
*edp
= stp
->dtsd_ecbdesc
;
684 if (dtrace_probe_iter(g_dtp
, &edp
->dted_probe
, list_probe
, NULL
) != 0) {
685 error("failed to match %s:%s:%s:%s: %s\n",
686 edp
->dted_probe
.dtpd_provider
, edp
->dted_probe
.dtpd_mod
,
687 edp
->dted_probe
.dtpd_func
, edp
->dted_probe
.dtpd_name
,
688 dtrace_errmsg(dtp
, dtrace_errno(dtp
)));
696 * List the probes corresponding to the specified program by iterating over
697 * each statement and then matching probes to the statement probe descriptions.
700 list_prog(const dtrace_cmd_t
*dcp
)
702 dtrace_ecbdesc_t
*last
= NULL
;
704 (void) dtrace_stmt_iter(g_dtp
, dcp
->dc_prog
,
705 (dtrace_stmt_f
*)list_stmt
, &last
);
709 compile_file(dtrace_cmd_t
*dcp
)
714 if ((fp
= fopen(dcp
->dc_arg
, "r")) == NULL
)
715 fatal("failed to open %s", dcp
->dc_arg
);
718 g_argv
[0] = dcp
->dc_arg
;
720 if ((dcp
->dc_prog
= dtrace_program_fcompile(g_dtp
, fp
,
721 g_cflags
, g_argc
, g_argv
)) == NULL
)
722 dfatal("failed to compile script %s", dcp
->dc_arg
);
727 dcp
->dc_desc
= "script";
728 dcp
->dc_name
= dcp
->dc_arg
;
732 compile_str(dtrace_cmd_t
*dcp
)
736 if ((dcp
->dc_prog
= dtrace_program_strcompile(g_dtp
, dcp
->dc_arg
,
737 dcp
->dc_spec
, g_cflags
| DTRACE_C_PSPEC
, g_argc
, g_argv
)) == NULL
)
738 dfatal("invalid probe specifier %s", dcp
->dc_arg
);
740 if ((p
= strpbrk(dcp
->dc_arg
, "{/;")) != NULL
)
741 *p
= '\0'; /* crop name for reporting */
743 dcp
->dc_desc
= "description";
744 dcp
->dc_name
= dcp
->dc_arg
;
749 prochandler(struct ps_prochandle
*P
, const char *msg
, void *arg
)
751 const psinfo_t
*prp
= Ppsinfo(P
);
752 int pid
= Pstatus(P
)->pr_pid
;
753 char name
[SIG2STR_MAX
];
756 notice("pid %d: %s\n", pid
, msg
);
763 * Ideally we would like to always report pr_wstat here, but it
764 * isn't possible given current /proc semantics. If we grabbed
765 * the process, Ppsinfo() will either fail or return a zeroed
766 * psinfo_t depending on how far the parent is in reaping it.
767 * When /proc provides a stable pr_wstat in the status file,
768 * this code can be improved by examining this new pr_wstat.
770 if (prp
!= NULL
&& WIFSIGNALED(prp
->pr_wstat
)) {
771 notice("pid %d terminated by %s\n", pid
,
772 proc_signame(WTERMSIG(prp
->pr_wstat
),
773 name
, sizeof (name
)));
774 } else if (prp
!= NULL
&& WEXITSTATUS(prp
->pr_wstat
) != 0) {
775 notice("pid %d exited with status %d\n",
776 pid
, WEXITSTATUS(prp
->pr_wstat
));
778 notice("pid %d has exited\n", pid
);
784 notice("pid %d exec'd a set-id or unobservable program\n", pid
);
792 errhandler(const dtrace_errdata_t
*data
, void *arg
)
794 error(data
->dteda_msg
);
795 return (DTRACE_HANDLE_OK
);
800 drophandler(const dtrace_dropdata_t
*data
, void *arg
)
802 error(data
->dtdda_msg
);
803 return (DTRACE_HANDLE_OK
);
808 setopthandler(const dtrace_setoptdata_t
*data
, void *arg
)
810 if (strcmp(data
->dtsda_option
, "quiet") == 0)
811 g_quiet
= data
->dtsda_newval
!= DTRACEOPT_UNSET
;
813 if (strcmp(data
->dtsda_option
, "flowindent") == 0)
814 g_flowindent
= data
->dtsda_newval
!= DTRACEOPT_UNSET
;
816 return (DTRACE_HANDLE_OK
);
819 #define BUFDUMPHDR(hdr) \
820 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
822 #define BUFDUMPSTR(ptr, field) \
823 (void) printf("%s: %20s => ", g_pname, #field); \
824 if ((ptr)->field != NULL) { \
825 const char *c = (ptr)->field; \
826 (void) printf("\""); \
829 (void) printf("\\n"); \
833 (void) printf("%c", *c); \
834 } while (*c++ != '\0'); \
835 (void) printf("\"\n"); \
837 (void) printf("<NULL>\n"); \
840 #define BUFDUMPASSTR(ptr, field, str) \
841 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
843 #define BUFDUMP(ptr, field) \
844 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
845 (long long)(ptr)->field);
847 #define BUFDUMPPTR(ptr, field) \
848 (void) printf("%s: %20s => %s\n", g_pname, #field, \
849 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
853 bufhandler(const dtrace_bufdata_t
*bufdata
, void *arg
)
855 const dtrace_aggdata_t
*agg
= bufdata
->dtbda_aggdata
;
856 const dtrace_recdesc_t
*rec
= bufdata
->dtbda_recdesc
;
857 const dtrace_probedesc_t
*pd
;
858 uint32_t flags
= bufdata
->dtbda_flags
;
859 char buf
[512], *c
= buf
, *end
= c
+ sizeof (buf
);
866 { "AGGVAL", DTRACE_BUFDATA_AGGVAL
},
867 { "AGGKEY", DTRACE_BUFDATA_AGGKEY
},
868 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT
},
869 { "AGGLAST", DTRACE_BUFDATA_AGGLAST
},
870 { "???", UINT32_MAX
},
874 if (bufdata
->dtbda_probe
!= NULL
) {
875 pd
= bufdata
->dtbda_probe
->dtpda_pdesc
;
876 } else if (agg
!= NULL
) {
877 pd
= agg
->dtada_pdesc
;
882 BUFDUMPHDR(">>> Called buffer handler");
885 BUFDUMPHDR(" dtrace_bufdata");
886 BUFDUMPSTR(bufdata
, dtbda_buffered
);
887 BUFDUMPPTR(bufdata
, dtbda_probe
);
888 BUFDUMPPTR(bufdata
, dtbda_aggdata
);
889 BUFDUMPPTR(bufdata
, dtbda_recdesc
);
891 (void) snprintf(c
, end
- c
, "0x%x ", bufdata
->dtbda_flags
);
894 for (i
= 0, printed
= 0; flagnames
[i
].name
!= NULL
; i
++) {
895 if (!(flags
& flagnames
[i
].value
))
898 (void) snprintf(c
, end
- c
,
899 "%s%s", printed
++ ? " | " : "(", flagnames
[i
].name
);
901 flags
&= ~flagnames
[i
].value
;
905 (void) snprintf(c
, end
- c
, ")");
907 BUFDUMPASSTR(bufdata
, dtbda_flags
, buf
);
911 BUFDUMPHDR(" dtrace_probedesc");
912 BUFDUMPSTR(pd
, dtpd_provider
);
913 BUFDUMPSTR(pd
, dtpd_mod
);
914 BUFDUMPSTR(pd
, dtpd_func
);
915 BUFDUMPSTR(pd
, dtpd_name
);
920 BUFDUMPHDR(" dtrace_recdesc");
921 BUFDUMP(rec
, dtrd_action
);
922 BUFDUMP(rec
, dtrd_size
);
926 int lim
= rec
->dtrd_size
;
928 (void) sprintf(buf
, "%d (data: ", rec
->dtrd_offset
);
929 c
= buf
+ strlen(buf
);
931 if (lim
> sizeof (uint64_t))
932 lim
= sizeof (uint64_t);
934 data
= (uint8_t *)agg
->dtada_data
+ rec
->dtrd_offset
;
936 for (i
= 0; i
< lim
; i
++) {
937 (void) snprintf(c
, end
- c
, "%s%02x",
938 i
== 0 ? "" : " ", *data
++);
942 (void) snprintf(c
, end
- c
,
943 "%s)", lim
< rec
->dtrd_size
? " ..." : "");
944 BUFDUMPASSTR(rec
, dtrd_offset
, buf
);
946 BUFDUMP(rec
, dtrd_offset
);
953 dtrace_aggdesc_t
*desc
= agg
->dtada_desc
;
955 BUFDUMPHDR(" dtrace_aggdesc");
956 BUFDUMPSTR(desc
, dtagd_name
);
957 BUFDUMP(desc
, dtagd_varid
);
958 BUFDUMP(desc
, dtagd_id
);
959 BUFDUMP(desc
, dtagd_nrecs
);
963 return (DTRACE_HANDLE_OK
);
968 chewrec(const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*rec
, void *arg
)
970 dtrace_actkind_t act
;
975 * We have processed the final record; output the newline if
976 * we're not in quiet mode.
981 return (DTRACE_CONSUME_NEXT
);
984 act
= rec
->dtrd_action
;
985 addr
= (uintptr_t)data
->dtpda_data
;
987 if (act
== DTRACEACT_EXIT
) {
988 g_status
= *((uint32_t *)addr
);
989 return (DTRACE_CONSUME_NEXT
);
992 return (DTRACE_CONSUME_THIS
);
997 chew(const dtrace_probedata_t
*data
, void *arg
)
999 dtrace_probedesc_t
*pd
= data
->dtpda_pdesc
;
1000 processorid_t cpu
= data
->dtpda_cpu
;
1005 return (DTRACE_CONSUME_ABORT
);
1009 if (!g_flowindent
) {
1011 oprintf("%3s %6s %32s\n",
1012 "CPU", "ID", "FUNCTION:NAME");
1015 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1020 if (!g_flowindent
) {
1022 char name
[DTRACE_FUNCNAMELEN
+ DTRACE_NAMELEN
+ 2];
1024 (void) snprintf(name
, sizeof (name
), "%s:%s",
1025 pd
->dtpd_func
, pd
->dtpd_name
);
1027 oprintf("%3d %6d %32s ", cpu
, pd
->dtpd_id
, name
);
1030 int indent
= data
->dtpda_indent
;
1034 if (data
->dtpda_flow
== DTRACEFLOW_NONE
) {
1035 len
= indent
+ DTRACE_FUNCNAMELEN
+ DTRACE_NAMELEN
+ 5;
1037 (void) snprintf(name
, len
, "%*s%s%s:%s", indent
, "",
1038 data
->dtpda_prefix
, pd
->dtpd_func
,
1041 len
= indent
+ DTRACE_FUNCNAMELEN
+ 5;
1043 (void) snprintf(name
, len
, "%*s%s%s", indent
, "",
1044 data
->dtpda_prefix
, pd
->dtpd_func
);
1047 oprintf("%3d %-41s ", cpu
, name
);
1050 return (DTRACE_CONSUME_THIS
);
1061 dtrace_optval_t val
;
1063 { "buffer size", "bufsize" },
1064 { "aggregation size", "aggsize" },
1065 { "speculation size", "specsize" },
1066 { "dynamic variable size", "dynvarsize" },
1069 { "cleaning rate", "cleanrate" },
1070 { "status rate", "statusrate" },
1074 for (i
= 0; bufs
[i
].name
!= NULL
; i
++) {
1075 if (dtrace_getopt(g_dtp
, bufs
[i
].optname
, &bufs
[i
].val
) == -1)
1076 fatal("couldn't get option %s", bufs
[i
].optname
);
1079 for (i
= 0; rates
[i
].name
!= NULL
; i
++) {
1080 if (dtrace_getopt(g_dtp
, rates
[i
].optname
, &rates
[i
].val
) == -1)
1081 fatal("couldn't get option %s", rates
[i
].optname
);
1084 if (dtrace_go(g_dtp
) == -1)
1085 dfatal("could not enable tracing");
1087 for (i
= 0; bufs
[i
].name
!= NULL
; i
++) {
1088 dtrace_optval_t j
= 0, mul
= 10;
1089 dtrace_optval_t nsize
;
1091 if (bufs
[i
].val
== DTRACEOPT_UNSET
)
1094 (void) dtrace_getopt(g_dtp
, bufs
[i
].optname
, &nsize
);
1096 if (nsize
== DTRACEOPT_UNSET
|| nsize
== 0)
1099 if (nsize
>= bufs
[i
].val
- sizeof (uint64_t))
1102 for (; (INT64_C(1) << mul
) <= nsize
; j
++, mul
+= 10)
1105 if (!(nsize
& ((INT64_C(1) << (mul
- 10)) - 1))) {
1106 error("%s lowered to %lld%c\n", bufs
[i
].name
,
1107 (long long)nsize
>> (mul
- 10), " kmgtpe"[j
]);
1109 error("%s lowered to %lld bytes\n", bufs
[i
].name
,
1114 for (i
= 0; rates
[i
].name
!= NULL
; i
++) {
1115 dtrace_optval_t nval
;
1118 if (rates
[i
].val
== DTRACEOPT_UNSET
)
1121 (void) dtrace_getopt(g_dtp
, rates
[i
].optname
, &nval
);
1123 if (nval
== DTRACEOPT_UNSET
|| nval
== 0)
1126 if (rates
[i
].val
== nval
)
1129 dir
= nval
> rates
[i
].val
? "reduced" : "increased";
1131 if (nval
<= NANOSEC
&& (NANOSEC
% nval
) == 0) {
1132 error("%s %s to %lld hz\n", rates
[i
].name
, dir
,
1133 (long long)NANOSEC
/ (long long)nval
);
1137 if ((nval
% NANOSEC
) == 0) {
1138 error("%s %s to once every %lld seconds\n",
1140 (long long)nval
/ (long long)NANOSEC
);
1144 error("%s %s to once every %lld nanoseconds\n",
1145 rates
[i
].name
, dir
, (long long)nval
);
1161 main(int argc
, char *argv
[])
1163 dtrace_bufdesc_t buf
;
1164 struct sigaction act
, oact
;
1165 dtrace_status_t status
[2];
1166 dtrace_optval_t opt
;
1169 int done
= 0, mode
= 0;
1172 struct ps_prochandle
*P
;
1177 g_pname
= basename(argv
[0]);
1180 return (usage(stderr
));
1182 if ((g_argv
= malloc(sizeof (char *) * argc
)) == NULL
||
1183 (g_cmdv
= malloc(sizeof (dtrace_cmd_t
) * argc
)) == NULL
||
1184 (g_psv
= malloc(sizeof (struct ps_prochandle
*) * argc
)) == NULL
)
1185 fatal("failed to allocate memory for arguments");
1187 g_argv
[g_argc
++] = argv
[0]; /* propagate argv[0] to D as $0/$$0 */
1188 argv
[0] = g_pname
; /* rewrite argv[0] for getopt errors */
1190 bzero(status
, sizeof (status
));
1191 bzero(&buf
, sizeof (buf
));
1194 * Make an initial pass through argv[] processing any arguments that
1195 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1196 * We also accumulate arguments that are not affiliated with getopt
1197 * options into g_argv[], and abort if any invalid options are found.
1199 for (optind
= 1; optind
< argc
; optind
++) {
1200 while ((c
= getopt(argc
, argv
, DTRACE_OPTSTR
)) != EOF
) {
1203 if (strcmp(optarg
, "2") != 0) {
1204 (void) fprintf(stderr
,
1205 "%s: illegal option -- 3%s\n",
1207 return (usage(stderr
));
1209 g_oflags
&= ~DTRACE_O_LP64
;
1210 g_oflags
|= DTRACE_O_ILP32
;
1214 if (strcmp(optarg
, "4") != 0) {
1215 (void) fprintf(stderr
,
1216 "%s: illegal option -- 6%s\n",
1218 return (usage(stderr
));
1220 g_oflags
&= ~DTRACE_O_ILP32
;
1221 g_oflags
|= DTRACE_O_LP64
;
1225 g_grabanon
++; /* also checked in pass 2 below */
1229 g_mode
= DMODE_ANON
;
1240 g_mode
= DMODE_HEADER
;
1241 g_oflags
|= DTRACE_O_NODEV
;
1242 g_cflags
|= DTRACE_C_ZDEFS
; /* -h implies -Z */
1248 g_mode
= DMODE_LINK
;
1249 g_oflags
|= DTRACE_O_NODEV
;
1250 g_cflags
|= DTRACE_C_ZDEFS
; /* -G implies -Z */
1256 g_mode
= DMODE_LIST
;
1257 g_cflags
|= DTRACE_C_ZDEFS
; /* -l implies -Z */
1262 g_mode
= DMODE_VERS
;
1267 if (strchr(DTRACE_OPTSTR
, c
) == NULL
)
1268 return (usage(stderr
));
1273 g_argv
[g_argc
++] = argv
[optind
];
1277 (void) fprintf(stderr
, "%s: only one of the [-AGhlV] options "
1278 "can be specified at a time\n", g_pname
);
1282 if (g_mode
== DMODE_VERS
)
1283 return (printf("%s: %s\n", g_pname
, _dtrace_version
) <= 0);
1286 * If we're in linker mode and the data model hasn't been specified,
1287 * we try to guess the appropriate setting by examining the object
1288 * files. We ignore certain errors since we'll catch them later when
1289 * we actually process the object files.
1291 if (g_mode
== DMODE_LINK
&&
1292 (g_oflags
& (DTRACE_O_ILP32
| DTRACE_O_LP64
)) == 0 &&
1293 elf_version(EV_CURRENT
) != EV_NONE
) {
1298 for (i
= 1; i
< g_argc
; i
++) {
1299 if ((fd
= open64(g_argv
[i
], O_RDONLY
)) == -1)
1302 if ((elf
= elf_begin(fd
, ELF_C_READ
, NULL
)) == NULL
) {
1307 if (elf_kind(elf
) != ELF_K_ELF
||
1308 gelf_getehdr(elf
, &ehdr
) == NULL
) {
1310 (void) elf_end(elf
);
1315 (void) elf_end(elf
);
1317 if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS64
) {
1318 if (g_oflags
& DTRACE_O_ILP32
) {
1319 fatal("can't mix 32-bit and 64-bit "
1322 g_oflags
|= DTRACE_O_LP64
;
1323 } else if (ehdr
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
1324 if (g_oflags
& DTRACE_O_LP64
) {
1325 fatal("can't mix 32-bit and 64-bit "
1328 g_oflags
|= DTRACE_O_ILP32
;
1336 * Open libdtrace. If we are not actually going to be enabling any
1337 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1339 while ((g_dtp
= dtrace_open(DTRACE_VERSION
, g_oflags
, &err
)) == NULL
) {
1340 if (!(g_oflags
& DTRACE_O_NODEV
) && !g_exec
&& !g_grabanon
) {
1341 g_oflags
|= DTRACE_O_NODEV
;
1345 fatal("failed to initialize dtrace: %s\n",
1346 dtrace_errmsg(NULL
, err
));
1349 (void) dtrace_setopt(g_dtp
, "bufsize", "4m");
1350 (void) dtrace_setopt(g_dtp
, "aggsize", "4m");
1351 (void) dtrace_setopt(g_dtp
, "temporal", "yes");
1354 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1355 * references to undefined symbols to remain as unresolved relocations.
1356 * If -A is specified, enable -xlink=primary to permit static linking
1357 * only to kernel symbols that are defined in a primary kernel module.
1359 if (g_mode
== DMODE_LINK
) {
1360 (void) dtrace_setopt(g_dtp
, "linkmode", "dynamic");
1361 (void) dtrace_setopt(g_dtp
, "unodefs", NULL
);
1364 * Use the remaining arguments as the list of object files
1365 * when in linker mode.
1367 g_objc
= g_argc
- 1;
1368 g_objv
= g_argv
+ 1;
1371 * We still use g_argv[0], the name of the executable.
1374 } else if (g_mode
== DMODE_ANON
)
1375 (void) dtrace_setopt(g_dtp
, "linkmode", "primary");
1378 * Now that we have libdtrace open, make a second pass through argv[]
1379 * to perform any dtrace_setopt() calls and change any compiler flags.
1380 * We also accumulate any program specifications into our g_cmdv[] at
1381 * this time; these will compiled as part of the fourth processing pass.
1383 for (optind
= 1; optind
< argc
; optind
++) {
1384 while ((c
= getopt(argc
, argv
, DTRACE_OPTSTR
)) != EOF
) {
1387 if (dtrace_setopt(g_dtp
, "grabanon", 0) != 0)
1388 dfatal("failed to set -a");
1392 if (dtrace_setopt(g_dtp
,
1393 "bufsize", optarg
) != 0)
1394 dfatal("failed to set -b %s", optarg
);
1402 g_cflags
|= DTRACE_C_CPP
;
1406 if (dtrace_setopt(g_dtp
, "define", optarg
) != 0)
1407 dfatal("failed to set -D %s", optarg
);
1411 dcp
= &g_cmdv
[g_cmdc
++];
1412 dcp
->dc_func
= compile_str
;
1413 dcp
->dc_spec
= DTRACE_PROBESPEC_FUNC
;
1414 dcp
->dc_arg
= optarg
;
1418 if (dtrace_setopt(g_dtp
, "flowindent", 0) != 0)
1419 dfatal("failed to set -F");
1423 if (dtrace_setopt(g_dtp
, "cpphdrs", 0) != 0)
1424 dfatal("failed to set -H");
1428 dcp
= &g_cmdv
[g_cmdc
++];
1429 dcp
->dc_func
= compile_str
;
1430 dcp
->dc_spec
= DTRACE_PROBESPEC_NAME
;
1431 dcp
->dc_arg
= optarg
;
1435 if (dtrace_setopt(g_dtp
, "incdir", optarg
) != 0)
1436 dfatal("failed to set -I %s", optarg
);
1440 if (dtrace_setopt(g_dtp
, "libdir", optarg
) != 0)
1441 dfatal("failed to set -L %s", optarg
);
1445 dcp
= &g_cmdv
[g_cmdc
++];
1446 dcp
->dc_func
= compile_str
;
1447 dcp
->dc_spec
= DTRACE_PROBESPEC_MOD
;
1448 dcp
->dc_arg
= optarg
;
1452 dcp
= &g_cmdv
[g_cmdc
++];
1453 dcp
->dc_func
= compile_str
;
1454 dcp
->dc_spec
= DTRACE_PROBESPEC_NAME
;
1455 dcp
->dc_arg
= optarg
;
1459 dcp
= &g_cmdv
[g_cmdc
++];
1460 dcp
->dc_func
= compile_str
;
1461 dcp
->dc_spec
= DTRACE_PROBESPEC_PROVIDER
;
1462 dcp
->dc_arg
= optarg
;
1466 if (dtrace_setopt(g_dtp
, "quiet", 0) != 0)
1467 dfatal("failed to set -q");
1475 dcp
= &g_cmdv
[g_cmdc
++];
1476 dcp
->dc_func
= compile_file
;
1477 dcp
->dc_spec
= DTRACE_PROBESPEC_NONE
;
1478 dcp
->dc_arg
= optarg
;
1482 g_cflags
|= DTRACE_C_DIFV
;
1486 if (dtrace_setopt(g_dtp
, "undef", optarg
) != 0)
1487 dfatal("failed to set -U %s", optarg
);
1495 if (dtrace_setopt(g_dtp
, "destructive", 0) != 0)
1496 dfatal("failed to set -w");
1500 if ((p
= strchr(optarg
, '=')) != NULL
)
1503 if (dtrace_setopt(g_dtp
, optarg
, p
) != 0)
1504 dfatal("failed to set -x %s", optarg
);
1508 if (dtrace_setopt(g_dtp
, "stdc", optarg
) != 0)
1509 dfatal("failed to set -X %s", optarg
);
1513 g_cflags
|= DTRACE_C_ZDEFS
;
1517 if (strchr(DTRACE_OPTSTR
, c
) == NULL
)
1518 return (usage(stderr
));
1523 if (g_ofp
== NULL
&& g_mode
!= DMODE_EXEC
) {
1524 (void) fprintf(stderr
, "%s: -B not valid in combination"
1525 " with [-AGl] options\n", g_pname
);
1529 if (g_ofp
== NULL
&& g_ofile
!= NULL
) {
1530 (void) fprintf(stderr
, "%s: -B not valid in combination"
1531 " with -o option\n", g_pname
);
1536 * In our third pass we handle any command-line options related to
1537 * grabbing or creating victim processes. The behavior of these calls
1538 * may been affected by any library options set by the second pass.
1540 for (optind
= 1; optind
< argc
; optind
++) {
1541 while ((c
= getopt(argc
, argv
, DTRACE_OPTSTR
)) != EOF
) {
1544 if ((v
= make_argv(optarg
)) == NULL
)
1545 fatal("failed to allocate memory");
1547 P
= dtrace_proc_create(g_dtp
, v
[0], v
);
1549 dfatal(NULL
); /* dtrace_errmsg() only */
1557 pid
= strtol(optarg
, &p
, 10);
1559 if (errno
!= 0 || p
== optarg
|| p
[0] != '\0')
1560 fatal("invalid pid: %s\n", optarg
);
1562 P
= dtrace_proc_grab(g_dtp
, pid
, 0);
1564 dfatal(NULL
); /* dtrace_errmsg() only */
1573 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1574 * each string or file specification into a compiled program structure.
1576 for (i
= 0; i
< g_cmdc
; i
++)
1577 g_cmdv
[i
].dc_func(&g_cmdv
[i
]);
1579 if (g_mode
!= DMODE_LIST
) {
1580 if (dtrace_handle_err(g_dtp
, &errhandler
, NULL
) == -1)
1581 dfatal("failed to establish error handler");
1583 if (dtrace_handle_drop(g_dtp
, &drophandler
, NULL
) == -1)
1584 dfatal("failed to establish drop handler");
1586 if (dtrace_handle_proc(g_dtp
, &prochandler
, NULL
) == -1)
1587 dfatal("failed to establish proc handler");
1589 if (dtrace_handle_setopt(g_dtp
, &setopthandler
, NULL
) == -1)
1590 dfatal("failed to establish setopt handler");
1592 if (g_ofp
== NULL
&&
1593 dtrace_handle_buffered(g_dtp
, &bufhandler
, NULL
) == -1)
1594 dfatal("failed to establish buffered handler");
1597 (void) dtrace_getopt(g_dtp
, "flowindent", &opt
);
1598 g_flowindent
= opt
!= DTRACEOPT_UNSET
;
1600 (void) dtrace_getopt(g_dtp
, "grabanon", &opt
);
1601 g_grabanon
= opt
!= DTRACEOPT_UNSET
;
1603 (void) dtrace_getopt(g_dtp
, "quiet", &opt
);
1604 g_quiet
= opt
!= DTRACEOPT_UNSET
;
1607 * Now make a fifth and final pass over the options that have been
1608 * turned into programs and saved in g_cmdv[], performing any mode-
1609 * specific processing. If g_mode is DMODE_EXEC, we will break out
1610 * of the switch() and continue on to the data processing loop. For
1611 * other modes, we will exit dtrace once mode-specific work is done.
1615 if (g_ofile
!= NULL
&& (g_ofp
= fopen(g_ofile
, "a")) == NULL
)
1616 fatal("failed to open output file '%s'", g_ofile
);
1618 for (i
= 0; i
< g_cmdc
; i
++)
1619 exec_prog(&g_cmdv
[i
]);
1621 if (done
&& !g_grabanon
) {
1622 dtrace_close(g_dtp
);
1628 if (g_ofile
== NULL
)
1629 g_ofile
= "/kernel/drv/dtrace.conf";
1631 dof_prune(g_ofile
); /* strip out any old DOF directives */
1632 etcsystem_prune(); /* string out any forceload directives */
1635 dtrace_close(g_dtp
);
1639 if ((g_ofp
= fopen(g_ofile
, "a")) == NULL
)
1640 fatal("failed to open output file '%s'", g_ofile
);
1642 for (i
= 0; i
< g_cmdc
; i
++) {
1643 anon_prog(&g_cmdv
[i
],
1644 dtrace_dof_create(g_dtp
, g_cmdv
[i
].dc_prog
, 0), i
);
1648 * Dump out the DOF corresponding to the error handler and the
1649 * current options as the final DOF property in the .conf file.
1651 anon_prog(NULL
, dtrace_geterr_dof(g_dtp
), i
++);
1652 anon_prog(NULL
, dtrace_getopt_dof(g_dtp
), i
++);
1654 if (fclose(g_ofp
) == EOF
)
1655 fatal("failed to close output file '%s'", g_ofile
);
1658 * These messages would use notice() rather than error(), but
1659 * we don't want them suppressed when -A is run on a D program
1660 * that itself contains a #pragma D option quiet.
1662 error("saved anonymous enabling in %s\n", g_ofile
);
1664 error("run update_drv(1M) or reboot to enable changes\n");
1666 dtrace_close(g_dtp
);
1671 (void) fprintf(stderr
, "%s: -G requires one or more "
1672 "scripts or enabling options\n", g_pname
);
1673 dtrace_close(g_dtp
);
1677 for (i
= 0; i
< g_cmdc
; i
++)
1678 link_prog(&g_cmdv
[i
]);
1680 if (g_cmdc
> 1 && g_ofile
!= NULL
) {
1681 char **objv
= alloca(g_cmdc
* sizeof (char *));
1683 for (i
= 0; i
< g_cmdc
; i
++)
1684 objv
[i
] = g_cmdv
[i
].dc_ofile
;
1686 if (dtrace_program_link(g_dtp
, NULL
, DTRACE_D_PROBES
,
1687 g_ofile
, g_cmdc
, objv
) != 0)
1688 dfatal(NULL
); /* dtrace_errmsg() only */
1691 dtrace_close(g_dtp
);
1695 if (g_ofile
!= NULL
&& (g_ofp
= fopen(g_ofile
, "a")) == NULL
)
1696 fatal("failed to open output file '%s'", g_ofile
);
1698 oprintf("%5s %10s %17s %33s %s\n",
1699 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1701 for (i
= 0; i
< g_cmdc
; i
++)
1702 list_prog(&g_cmdv
[i
]);
1705 (void) dtrace_probe_iter(g_dtp
, NULL
, list_probe
, NULL
);
1707 dtrace_close(g_dtp
);
1712 (void) fprintf(stderr
, "%s: -h requires one or more "
1713 "scripts or enabling options\n", g_pname
);
1714 dtrace_close(g_dtp
);
1718 if (g_ofile
== NULL
) {
1722 (void) fprintf(stderr
, "%s: -h requires an "
1723 "output file if multiple scripts are "
1724 "specified\n", g_pname
);
1725 dtrace_close(g_dtp
);
1729 if ((p
= strrchr(g_cmdv
[0].dc_arg
, '.')) == NULL
||
1730 strcmp(p
, ".d") != 0) {
1731 (void) fprintf(stderr
, "%s: -h requires an "
1732 "output file if no scripts are "
1733 "specified\n", g_pname
);
1734 dtrace_close(g_dtp
);
1738 p
[0] = '\0'; /* strip .d suffix */
1739 g_ofile
= p
= g_cmdv
[0].dc_ofile
;
1740 (void) snprintf(p
, sizeof (g_cmdv
[0].dc_ofile
),
1741 "%s.h", basename(g_cmdv
[0].dc_arg
));
1744 if ((g_ofp
= fopen(g_ofile
, "w")) == NULL
)
1745 fatal("failed to open header file '%s'", g_ofile
);
1747 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1749 if (dtrace_program_header(g_dtp
, g_ofp
, g_ofile
) != 0 ||
1750 fclose(g_ofp
) == EOF
)
1751 dfatal("failed to create header file %s", g_ofile
);
1753 dtrace_close(g_dtp
);
1758 * If -a and -Z were not specified and no probes have been matched, no
1759 * probe criteria was specified on the command line and we abort.
1761 if (g_total
== 0 && !g_grabanon
&& !(g_cflags
& DTRACE_C_ZDEFS
))
1762 dfatal("no probes %s\n", g_cmdc
? "matched" : "specified");
1765 * Start tracing. Once we dtrace_go(), reload any options that affect
1766 * our globals in case consuming anonymous state has changed them.
1770 (void) dtrace_getopt(g_dtp
, "flowindent", &opt
);
1771 g_flowindent
= opt
!= DTRACEOPT_UNSET
;
1773 (void) dtrace_getopt(g_dtp
, "grabanon", &opt
);
1774 g_grabanon
= opt
!= DTRACEOPT_UNSET
;
1776 (void) dtrace_getopt(g_dtp
, "quiet", &opt
);
1777 g_quiet
= opt
!= DTRACEOPT_UNSET
;
1779 (void) dtrace_getopt(g_dtp
, "destructive", &opt
);
1780 if (opt
!= DTRACEOPT_UNSET
)
1781 notice("allowing destructive actions\n");
1783 (void) sigemptyset(&act
.sa_mask
);
1785 act
.sa_handler
= intr
;
1787 if (sigaction(SIGINT
, NULL
, &oact
) == 0 && oact
.sa_handler
!= SIG_IGN
)
1788 (void) sigaction(SIGINT
, &act
, NULL
);
1790 if (sigaction(SIGTERM
, NULL
, &oact
) == 0 && oact
.sa_handler
!= SIG_IGN
)
1791 (void) sigaction(SIGTERM
, &act
, NULL
);
1794 * Now that tracing is active and we are ready to consume trace data,
1795 * continue any grabbed or created processes, setting them running
1796 * using the /proc control mechanism inside of libdtrace.
1798 for (i
= 0; i
< g_psc
; i
++)
1799 dtrace_proc_continue(g_dtp
, g_psv
[i
]);
1801 g_pslive
= g_psc
; /* count for prochandler() */
1804 if (!g_intr
&& !done
)
1805 dtrace_sleep(g_dtp
);
1809 * Output a newline just to make the output look
1810 * slightly cleaner. Note that we do this even in
1817 if (done
|| g_intr
|| (g_psc
!= 0 && g_pslive
== 0)) {
1819 if (dtrace_stop(g_dtp
) == -1)
1820 dfatal("couldn't stop tracing");
1823 switch (dtrace_work(g_dtp
, g_ofp
, chew
, chewrec
, NULL
)) {
1824 case DTRACE_WORKSTATUS_DONE
:
1827 case DTRACE_WORKSTATUS_OKAY
:
1830 if (!g_impatient
&& dtrace_errno(g_dtp
) != EINTR
)
1831 dfatal("processing aborted");
1834 if (g_ofp
!= NULL
&& fflush(g_ofp
) == EOF
)
1841 if (dtrace_aggregate_print(g_dtp
, g_ofp
, NULL
) == -1 &&
1842 dtrace_errno(g_dtp
) != EINTR
)
1843 dfatal("failed to print aggregations");
1846 dtrace_close(g_dtp
);