VirtualBox

source: vbox/trunk/src/VBox/ExtPacks/VBoxDTrace/onnv/cmd/dtrace/dtrace.c@ 76346

最後變更 在這個檔案從76346是 76346,由 vboxsync 提交於 6 年 前

*: Preparing for iprt/string.h, iprt/json.h and iprt/serialport.h no longer including iprt/err.h and string.h no longer including latin1.h (it needs err.h). bugref:9344

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 49.9 KB
 
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#ifndef VBOX
28#pragma ident "%Z%%M% %I% %E% SMI"
29#endif
30
31#include <sys/types.h>
32#include <sys/stat.h>
33#ifndef _MSC_VER
34# include <sys/wait.h>
35#endif
36
37#include <dtrace.h>
38#include <stdlib.h>
39#include <stdarg.h>
40#include <stdio.h>
41#ifndef VBOX
42# include <strings.h>
43#endif
44#ifndef _MSC_VER
45# include <unistd.h>
46#else
47# include <direct.h>
48# include <io.h>
49#endif
50#include <limits.h>
51#include <fcntl.h>
52#include <errno.h>
53#include <signal.h>
54#ifndef VBOX
55#include <alloca.h>
56#include <libgen.h>
57#include <libproc.h>
58#endif
59
60#ifdef VBOX
61# include <stdio.h>
62
63# include <iprt/alloca.h>
64# include <iprt/err.h>
65# include <iprt/getopt.h>
66# include <iprt/initterm.h>
67# include <iprt/path.h>
68# include <iprt/message.h>
69# include <iprt/process.h>
70# include <iprt/string.h>
71
72# include "VBoxDTraceLibCWrappers.h"
73
74# ifdef _MSC_VER
75# pragma warning(disable:4267) /* size_t conversion warnings */
76# pragma warning(disable:4018) /* signed/unsigned mismatch */
77# endif
78#endif
79
80typedef struct dtrace_cmd {
81 void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
82 dtrace_probespec_t dc_spec; /* probe specifier context */
83 char *dc_arg; /* argument from main argv */
84 const char *dc_name; /* name for error messages */
85 const char *dc_desc; /* desc for error messages */
86 dtrace_prog_t *dc_prog; /* program compiled from arg */
87 char dc_ofile[PATH_MAX]; /* derived output file name */
88} dtrace_cmd_t;
89
90#define DMODE_VERS 0 /* display version information and exit (-V) */
91#define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
92#define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
93#define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
94#define DMODE_LIST 4 /* compile program and list probes (-l) */
95#define DMODE_HEADER 5 /* compile program for headergen (-h) */
96
97#define E_SUCCESS 0
98#define E_ERROR 1
99#define E_USAGE 2
100
101#ifndef VBOX
102static const char DTRACE_OPTSTR[] =
103 "3:6:aAb:Bc:CD:ef:FGhHi:I:lL:m:n:o:p:P:qs:SU:vVwx:X:Z";
104#else
105static const RTGETOPTDEF g_aOptions[] =
106{
107 { "-32", 10064, RTGETOPT_REQ_NOTHING },
108 { "-64", 10032, RTGETOPT_REQ_NOTHING },
109 { NULL, 'a', RTGETOPT_REQ_NOTHING },
110 { NULL, 'A', RTGETOPT_REQ_NOTHING },
111 { NULL, 'b', RTGETOPT_REQ_STRING },
112 { NULL, 'B', RTGETOPT_REQ_NOTHING },
113 { NULL, 'c', RTGETOPT_REQ_STRING },
114 { NULL, 'C', RTGETOPT_REQ_NOTHING },
115 { NULL, 'D', RTGETOPT_REQ_STRING },
116 { NULL, 'e', RTGETOPT_REQ_NOTHING },
117 { NULL, 'f', RTGETOPT_REQ_STRING },
118 { NULL, 'F', RTGETOPT_REQ_NOTHING },
119 { NULL, 'G', RTGETOPT_REQ_NOTHING },
120 { NULL, 'h', RTGETOPT_REQ_NOTHING },
121 { NULL, 'H', RTGETOPT_REQ_NOTHING },
122 { NULL, 'i', RTGETOPT_REQ_STRING },
123 { NULL, 'I', RTGETOPT_REQ_STRING },
124 { NULL, 'l', RTGETOPT_REQ_NOTHING },
125 { NULL, 'L', RTGETOPT_REQ_STRING },
126 { NULL, 'M', RTGETOPT_REQ_STRING },
127 { NULL, 'n', RTGETOPT_REQ_STRING },
128 { NULL, 'o', RTGETOPT_REQ_STRING },
129 { NULL, 'p', RTGETOPT_REQ_STRING },
130 { NULL, 'P', RTGETOPT_REQ_STRING },
131 { NULL, 'q', RTGETOPT_REQ_NOTHING },
132 { NULL, 's', RTGETOPT_REQ_STRING },
133 { NULL, 'S', RTGETOPT_REQ_NOTHING },
134 { NULL, 'U', RTGETOPT_REQ_STRING },
135 { NULL, 'v', RTGETOPT_REQ_NOTHING },
136 { NULL, 'V', RTGETOPT_REQ_NOTHING },
137 { NULL, 'w', RTGETOPT_REQ_NOTHING },
138 { NULL, 'x', RTGETOPT_REQ_STRING },
139 { NULL, 'X', RTGETOPT_REQ_STRING },
140 { NULL, 'Z', RTGETOPT_REQ_NOTHING },
141};
142#endif /* VBOX */
143
144
145static char **g_argv;
146static int g_argc;
147#ifndef VBOX /* No linking. */
148static char **g_objv;
149static int g_objc;
150#endif
151static dtrace_cmd_t *g_cmdv;
152static int g_cmdc;
153static struct ps_prochandle **g_psv;
154static int g_psc;
155static int g_pslive;
156static char *g_pname;
157static int g_quiet;
158static int g_flowindent;
159#ifdef VBOX /* Added volatile to signal handler variables. */
160static int volatile g_intr;
161static int volatile g_impatient;
162static int volatile g_newline;
163#else
164static int g_intr;
165static int g_impatient;
166static int g_newline;
167#endif
168static int g_total;
169static int g_cflags;
170static int g_oflags;
171static int g_verbose;
172static int g_exec = 1;
173static int g_mode = DMODE_EXEC;
174static int g_status = E_SUCCESS;
175static int g_grabanon = 0;
176static const char *g_ofile = NULL;
177#ifndef VBOX /* stdout isn't a necessarily constant usable like this in C code. */
178static FILE *g_ofp = stdout;
179#else
180static FILE *g_ofp = NULL;
181#endif
182static dtrace_hdl_t *g_dtp;
183static char *g_etcfile = "/etc/system";
184static const char *g_etcbegin = "* vvvv Added by DTrace";
185static const char *g_etcend = "* ^^^^ Added by DTrace";
186
187static const char *g_etc[] = {
188"*",
189"* The following forceload directives were added by dtrace(1M) to allow for",
190"* tracing during boot. If these directives are removed, the system will",
191"* continue to function, but tracing will not occur during boot as desired.",
192"* To remove these directives (and this block comment) automatically, run",
193"* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
194"* chapter of the Solaris Dynamic Tracing Guide for details.",
195"*",
196NULL };
197
198static int
199usage(FILE *fp)
200{
201 static const char predact[] = "[[ predicate ] action ]";
202
203 (void) fprintf(fp, "Usage: %s [-32|-64] [-aACeFGhHlqSvVwZ] "
204 "[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
205 "[-o output] [-p pid] [-s script] [-U name]\n\t"
206 "[-x opt[=val]] [-X a|c|s|t]\n\n"
207 "\t[-P provider %s]\n"
208 "\t[-m [ provider: ] module %s]\n"
209 "\t[-f [[ provider: ] module: ] func %s]\n"
210 "\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
211 "\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
212 predact, predact, predact, predact, predact);
213
214 (void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
215 (void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
216
217 (void) fprintf(fp, "\n"
218 "\t-32 generate 32-bit D programs and ELF files\n"
219 "\t-64 generate 64-bit D programs and ELF files\n\n"
220 "\t-a claim anonymous tracing state\n"
221 "\t-A generate driver.conf(4) directives for anonymous tracing\n"
222 "\t-b set trace buffer size\n"
223 "\t-c run specified command and exit upon its completion\n"
224 "\t-C run cpp(1) preprocessor on script files\n"
225 "\t-D define symbol when invoking preprocessor\n"
226 "\t-e exit after compiling request but prior to enabling probes\n"
227 "\t-f enable or list probes matching the specified function name\n"
228 "\t-F coalesce trace output by function\n"
229 "\t-G generate an ELF file containing embedded dtrace program\n"
230 "\t-h generate a header file with definitions for static probes\n"
231 "\t-H print included files when invoking preprocessor\n"
232 "\t-i enable or list probes matching the specified probe id\n"
233 "\t-I add include directory to preprocessor search path\n"
234 "\t-l list probes matching specified criteria\n"
235 "\t-L add library directory to library search path\n"
236 "\t-m enable or list probes matching the specified module name\n"
237 "\t-n enable or list probes matching the specified probe name\n"
238 "\t-o set output file\n"
239 "\t-p grab specified process-ID and cache its symbol tables\n"
240 "\t-P enable or list probes matching the specified provider name\n"
241 "\t-q set quiet mode (only output explicitly traced data)\n"
242 "\t-s enable or list probes according to the specified D script\n"
243 "\t-S print D compiler intermediate code\n"
244 "\t-U undefine symbol when invoking preprocessor\n"
245 "\t-v set verbose mode (report stability attributes, arguments)\n"
246 "\t-V report DTrace API version\n"
247 "\t-w permit destructive actions\n"
248 "\t-x enable or modify compiler and tracing options\n"
249 "\t-X specify ISO C conformance settings for preprocessor\n"
250 "\t-Z permit probe descriptions that match zero probes\n");
251
252 return (E_USAGE);
253}
254
255static void
256verror(const char *fmt, va_list ap)
257{
258 int error = errno;
259
260 (void) fprintf(stderr, "%s: ", g_pname);
261 (void) vfprintf(stderr, fmt, ap);
262
263 if (fmt[strlen(fmt) - 1] != '\n')
264 (void) fprintf(stderr, ": %s\n", strerror(error));
265}
266
267/*PRINTFLIKE1*/
268static void
269fatal(const char *fmt, ...)
270{
271 va_list ap;
272
273 va_start(ap, fmt);
274 verror(fmt, ap);
275 va_end(ap);
276
277 exit(E_ERROR);
278}
279
280/*PRINTFLIKE1*/
281static void
282dfatal(const char *fmt, ...)
283{
284 va_list ap;
285
286 va_start(ap, fmt);
287
288 (void) fprintf(stderr, "%s: ", g_pname);
289 if (fmt != NULL)
290 (void) vfprintf(stderr, fmt, ap);
291
292 va_end(ap);
293
294 if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
295 (void) fprintf(stderr, ": %s\n",
296 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
297 } else if (fmt == NULL) {
298 (void) fprintf(stderr, "%s\n",
299 dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
300 }
301
302 /*
303 * Close the DTrace handle to ensure that any controlled processes are
304 * correctly restored and continued.
305 */
306 dtrace_close(g_dtp);
307
308 exit(E_ERROR);
309}
310
311/*PRINTFLIKE1*/
312static void
313error(const char *fmt, ...)
314{
315 va_list ap;
316
317 va_start(ap, fmt);
318 verror(fmt, ap);
319 va_end(ap);
320}
321
322/*PRINTFLIKE1*/
323static void
324notice(const char *fmt, ...)
325{
326 va_list ap;
327
328 if (g_quiet)
329 return; /* -q or quiet pragma suppresses notice()s */
330
331 va_start(ap, fmt);
332 verror(fmt, ap);
333 va_end(ap);
334}
335
336/*PRINTFLIKE1*/
337static void
338oprintf(const char *fmt, ...)
339{
340 va_list ap;
341 int n;
342
343 if (g_ofp == NULL)
344 return;
345
346 va_start(ap, fmt);
347 n = vfprintf(g_ofp, fmt, ap);
348 va_end(ap);
349
350 if (n < 0) {
351 if (errno != EINTR) {
352 fatal("failed to write to %s",
353 g_ofile ? g_ofile : "<stdout>");
354 }
355 clearerr(g_ofp);
356 }
357}
358
359#ifndef VBOX
360static char **
361make_argv(char *s)
362{
363 const char *ws = "\f\n\r\t\v ";
364 char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
365 int argc = 0;
366 char *p = s;
367
368 if (argv == NULL)
369 return (NULL);
370
371 for (p = strtok(s, ws); p != NULL; p = strtok(NULL, ws))
372 argv[argc++] = p;
373
374 if (argc == 0)
375 argv[argc++] = s;
376
377 argv[argc] = NULL;
378 return (argv);
379}
380#endif /* !VBOX */
381
382static void
383dof_prune(const char *fname)
384{
385 struct stat sbuf;
386 size_t sz, i, j, mark, len;
387 char *buf;
388 int msg = 0, fd;
389
390 if ((fd = open(fname, O_RDONLY)) == -1) {
391 /*
392 * This is okay only if the file doesn't exist at all.
393 */
394 if (errno != ENOENT)
395 fatal("failed to open %s", fname);
396 return;
397 }
398
399 if (fstat(fd, &sbuf) == -1)
400 fatal("failed to fstat %s", fname);
401
402 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
403 fatal("failed to allocate memory for %s", fname);
404
405 if ((size_t/*vbox*/)read(fd, buf, sz) != sz)
406 fatal("failed to read %s", fname);
407
408 buf[sz] = '\0';
409 (void) close(fd);
410
411 if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
412 fatal("failed to open %s for writing", fname);
413
414 len = strlen("dof-data-");
415
416 for (mark = 0, i = 0; i < sz; i++) {
417 if (strncmp(&buf[i], "dof-data-", len) != 0)
418 continue;
419
420 /*
421 * This is only a match if it's in the 0th column.
422 */
423 if (i != 0 && buf[i - 1] != '\n')
424 continue;
425
426 if (msg++ == 0) {
427 error("cleaned up old anonymous "
428 "enabling in %s\n", fname);
429 }
430
431 /*
432 * We have a match. First write out our data up until now.
433 */
434 if (i != mark) {
435 if ((size_t/*vbox*/)write(fd, &buf[mark], i - mark) != i - mark)
436 fatal("failed to write to %s", fname);
437 }
438
439 /*
440 * Now scan forward until we scan past a newline.
441 */
442 for (j = i; j < sz && buf[j] != '\n'; j++)
443 continue;
444
445 /*
446 * Reset our mark.
447 */
448 if ((mark = j + 1) >= sz)
449 break;
450
451 i = j;
452 }
453
454 if (mark < sz) {
455 if ((size_t/*vbox*/)write(fd, &buf[mark], sz - mark) != sz - mark)
456 fatal("failed to write to %s", fname);
457 }
458
459 (void) close(fd);
460 free(buf);
461}
462
463static void
464etcsystem_prune(void)
465{
466 struct stat sbuf;
467 size_t sz;
468 char *buf, *start, *end;
469 int fd;
470 char *fname = g_etcfile, *tmpname;
471
472 if ((fd = open(fname, O_RDONLY)) == -1)
473 fatal("failed to open %s", fname);
474
475 if (fstat(fd, &sbuf) == -1)
476 fatal("failed to fstat %s", fname);
477
478 if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
479 fatal("failed to allocate memory for %s", fname);
480
481 if ((size_t/*vbox*/)read(fd, buf, sz) != sz)
482 fatal("failed to read %s", fname);
483
484 buf[sz] = '\0';
485 (void) close(fd);
486
487 if ((start = strstr(buf, g_etcbegin)) == NULL)
488 goto out;
489
490 if (strlen(buf) != sz) {
491 fatal("embedded nul byte in %s; manual repair of %s "
492 "required\n", fname, fname);
493 }
494
495 if (strstr(start + 1, g_etcbegin) != NULL) {
496 fatal("multiple start sentinels in %s; manual repair of %s "
497 "required\n", fname, fname);
498 }
499
500 if ((end = strstr(buf, g_etcend)) == NULL) {
501 fatal("missing end sentinel in %s; manual repair of %s "
502 "required\n", fname, fname);
503 }
504
505 if (start > end) {
506 fatal("end sentinel preceeds start sentinel in %s; manual "
507 "repair of %s required\n", fname, fname);
508 }
509
510 end += strlen(g_etcend) + 1;
511 bcopy(end, start, strlen(end) + 1);
512
513 tmpname = alloca(sz = strlen(fname) + 80);
514 (void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
515
516 if ((fd = open(tmpname,
517 O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
518 fatal("failed to create %s", tmpname);
519
520 if (write(fd, buf, strlen(buf)) < (ssize_t/*vbox*/)strlen(buf)) {
521 (void) unlink(tmpname);
522 fatal("failed to write to %s", tmpname);
523 }
524
525 (void) close(fd);
526
527#ifndef _MSC_VER
528 if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
529 (void) unlink(tmpname);
530 fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
531 (int)sbuf.st_uid, (int)sbuf.st_gid);
532 }
533#endif
534
535 if (rename(tmpname, fname) == -1)
536 fatal("rename of %s to %s failed", tmpname, fname);
537
538 error("cleaned up forceload directives in %s\n", fname);
539out:
540 free(buf);
541}
542
543static void
544etcsystem_add(void)
545{
546 const char *mods[20];
547 int nmods, line;
548
549 if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
550 fatal("failed to open output file '%s'", g_ofile);
551
552 oprintf("%s\n", g_etcbegin);
553
554 for (line = 0; g_etc[line] != NULL; line++)
555 oprintf("%s\n", g_etc[line]);
556
557 nmods = dtrace_provider_modules(g_dtp, mods,
558 sizeof (mods) / sizeof (char *) - 1);
559
560 if (nmods >= sizeof (mods) / sizeof (char *))
561 fatal("unexpectedly large number of modules!");
562
563 mods[nmods++] = "dtrace";
564
565 for (line = 0; line < nmods; line++)
566 oprintf("forceload: drv/%s\n", mods[line]);
567
568 oprintf("%s\n", g_etcend);
569
570 if (fclose(g_ofp) == EOF)
571 fatal("failed to close output file '%s'", g_ofile);
572
573 error("added forceload directives to %s\n", g_ofile);
574}
575
576static void
577print_probe_info(const dtrace_probeinfo_t *p)
578{
579 char buf[BUFSIZ];
580 int i;
581
582 oprintf("\n\tProbe Description Attributes\n");
583
584 oprintf("\t\tIdentifier Names: %s\n",
585 dtrace_stability_name(p->dtp_attr.dtat_name));
586 oprintf("\t\tData Semantics: %s\n",
587 dtrace_stability_name(p->dtp_attr.dtat_data));
588 oprintf("\t\tDependency Class: %s\n",
589 dtrace_class_name(p->dtp_attr.dtat_class));
590
591 oprintf("\n\tArgument Attributes\n");
592
593 oprintf("\t\tIdentifier Names: %s\n",
594 dtrace_stability_name(p->dtp_arga.dtat_name));
595 oprintf("\t\tData Semantics: %s\n",
596 dtrace_stability_name(p->dtp_arga.dtat_data));
597 oprintf("\t\tDependency Class: %s\n",
598 dtrace_class_name(p->dtp_arga.dtat_class));
599
600 oprintf("\n\tArgument Types\n");
601
602 for (i = 0; i < p->dtp_argc; i++) {
603 if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
604 p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
605 (void) strlcpy(buf, "(unknown)", sizeof (buf));
606 oprintf("\t\targs[%d]: %s\n", i, buf);
607 }
608
609 if (p->dtp_argc == 0)
610 oprintf("\t\tNone\n");
611
612 oprintf("\n");
613}
614
615/*ARGSUSED*/
616static int
617info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
618 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
619{
620 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
621 dtrace_probedesc_t *pdp = &edp->dted_probe;
622 dtrace_probeinfo_t p;
623 RT_NOREF1(pgp);
624
625 if (edp == *last)
626 return (0);
627
628 oprintf("\n%s:%s:%s:%s\n",
629 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
630
631 if (dtrace_probe_info(dtp, pdp, &p) == 0)
632 print_probe_info(&p);
633
634 *last = edp;
635 return (0);
636}
637
638/*
639 * Execute the specified program by enabling the corresponding instrumentation.
640 * If -e has been specified, we get the program info but do not enable it. If
641 * -v has been specified, we print a stability report for the program.
642 */
643static void
644exec_prog(const dtrace_cmd_t *dcp)
645{
646 dtrace_ecbdesc_t *last = NULL;
647 dtrace_proginfo_t dpi;
648
649 if (!g_exec) {
650 dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
651 } else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
652 dfatal("failed to enable '%s'", dcp->dc_name);
653 } else {
654 notice("%s '%s' matched %u probe%s\n",
655 dcp->dc_desc, dcp->dc_name,
656 dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
657 }
658
659 if (g_verbose) {
660 oprintf("\nStability attributes for %s %s:\n",
661 dcp->dc_desc, dcp->dc_name);
662
663 oprintf("\n\tMinimum Probe Description Attributes\n");
664 oprintf("\t\tIdentifier Names: %s\n",
665 dtrace_stability_name(dpi.dpi_descattr.dtat_name));
666 oprintf("\t\tData Semantics: %s\n",
667 dtrace_stability_name(dpi.dpi_descattr.dtat_data));
668 oprintf("\t\tDependency Class: %s\n",
669 dtrace_class_name(dpi.dpi_descattr.dtat_class));
670
671 oprintf("\n\tMinimum Statement Attributes\n");
672
673 oprintf("\t\tIdentifier Names: %s\n",
674 dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
675 oprintf("\t\tData Semantics: %s\n",
676 dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
677 oprintf("\t\tDependency Class: %s\n",
678 dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
679
680 if (!g_exec) {
681 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
682 (dtrace_stmt_f *)info_stmt, &last);
683 } else
684 oprintf("\n");
685 }
686
687 g_total += dpi.dpi_matches;
688}
689
690/*
691 * Print out the specified DOF buffer as a set of ASCII bytes appropriate for
692 * storing in a driver.conf(4) file associated with the dtrace driver.
693 */
694static void
695anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n)
696{
697 const uchar_t *p, *q;
698
699 if (dof == NULL)
700 dfatal("failed to create DOF image for '%s'", dcp->dc_name);
701
702 p = (uchar_t *)dof;
703 q = p + dof->dofh_loadsz;
704
705 oprintf("dof-data-%d=0x%x", n, *p++);
706
707 while (p < q)
708 oprintf(",0x%x", *p++);
709
710 oprintf(";\n");
711 dtrace_dof_destroy(g_dtp, dof);
712}
713
714#ifndef VBOX
715/*
716 * Link the specified D program in DOF form into an ELF file for use in either
717 * helpers, userland provider definitions, or both. If -o was specified, that
718 * path is used as the output file name. If -o wasn't specified and the input
719 * program is from a script whose name is %.d, use basename(%.o) as the output
720 * file name. Otherwise we use "d.out" as the default output file name.
721 */
722static void
723link_prog(dtrace_cmd_t *dcp)
724{
725 char *p;
726
727 if (g_cmdc == 1 && g_ofile != NULL) {
728 (void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
729 } else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
730 strcmp(p, ".d") == 0) {
731 p[0] = '\0'; /* strip .d suffix */
732 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
733 "%s.o", basename(dcp->dc_arg));
734 } else {
735 (void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
736 g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
737 }
738
739 if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
740 dcp->dc_ofile, g_objc, g_objv) != 0)
741 dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
742}
743#endif /* !VBOX */
744
745/*ARGSUSED*/
746static int
747list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
748{
749 dtrace_probeinfo_t p;
750 RT_NOREF1(arg);
751
752 oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
753 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
754
755 if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
756 print_probe_info(&p);
757
758 return (0);
759}
760
761/*ARGSUSED*/
762static int
763list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
764 dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
765{
766 dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
767 RT_NOREF1(pgp);
768
769 if (edp == *last)
770 return (0);
771
772 if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
773 error("failed to match %s:%s:%s:%s: %s\n",
774 edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
775 edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
776 dtrace_errmsg(dtp, dtrace_errno(dtp)));
777 }
778
779 *last = edp;
780 return (0);
781}
782
783/*
784 * List the probes corresponding to the specified program by iterating over
785 * each statement and then matching probes to the statement probe descriptions.
786 */
787static void
788list_prog(const dtrace_cmd_t *dcp)
789{
790 dtrace_ecbdesc_t *last = NULL;
791
792 (void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
793 (dtrace_stmt_f *)list_stmt, &last);
794}
795
796static void
797compile_file(dtrace_cmd_t *dcp)
798{
799 char *arg0;
800 FILE *fp;
801
802 if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
803 fatal("failed to open %s", dcp->dc_arg);
804
805 arg0 = g_argv[0];
806 g_argv[0] = dcp->dc_arg;
807
808 if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
809 g_cflags, g_argc, g_argv)) == NULL)
810 dfatal("failed to compile script %s", dcp->dc_arg);
811
812 g_argv[0] = arg0;
813 (void) fclose(fp);
814
815 dcp->dc_desc = "script";
816 dcp->dc_name = dcp->dc_arg;
817}
818
819static void
820compile_str(dtrace_cmd_t *dcp)
821{
822 char *p;
823
824 if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
825 dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
826 dfatal("invalid probe specifier %s", dcp->dc_arg);
827
828 if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
829 *p = '\0'; /* crop name for reporting */
830
831 dcp->dc_desc = "description";
832 dcp->dc_name = dcp->dc_arg;
833}
834
835/*ARGSUSED*/
836static void
837prochandler(struct ps_prochandle *P, const char *msg, void *arg)
838{
839#ifndef VBOX
840 const psinfo_t *prp = Ppsinfo(P);
841 int pid = Pstatus(P)->pr_pid;
842 char name[SIG2STR_MAX];
843
844 if (msg != NULL) {
845 notice("pid %d: %s\n", pid, msg);
846 return;
847 }
848
849 switch (Pstate(P)) {
850 case PS_UNDEAD:
851 /*
852 * Ideally we would like to always report pr_wstat here, but it
853 * isn't possible given current /proc semantics. If we grabbed
854 * the process, Ppsinfo() will either fail or return a zeroed
855 * psinfo_t depending on how far the parent is in reaping it.
856 * When /proc provides a stable pr_wstat in the status file,
857 * this code can be improved by examining this new pr_wstat.
858 */
859 if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) {
860 notice("pid %d terminated by %s\n", pid,
861 proc_signame(WTERMSIG(prp->pr_wstat),
862 name, sizeof (name)));
863 } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) {
864 notice("pid %d exited with status %d\n",
865 pid, WEXITSTATUS(prp->pr_wstat));
866 } else {
867 notice("pid %d has exited\n", pid);
868 }
869 g_pslive--;
870 break;
871
872 case PS_LOST:
873 notice("pid %d exec'd a set-id or unobservable program\n", pid);
874 g_pslive--;
875 break;
876 }
877#else
878 RT_NOREF3(P, msg, arg);
879#endif /* VBOX */
880}
881
882/*ARGSUSED*/
883static int
884errhandler(const dtrace_errdata_t *data, void *arg)
885{
886 RT_NOREF1(arg);
887 error(data->dteda_msg);
888 return (DTRACE_HANDLE_OK);
889}
890
891/*ARGSUSED*/
892static int
893drophandler(const dtrace_dropdata_t *data, void *arg)
894{
895 RT_NOREF1(arg);
896 error(data->dtdda_msg);
897 return (DTRACE_HANDLE_OK);
898}
899
900/*ARGSUSED*/
901static int
902setopthandler(const dtrace_setoptdata_t *data, void *arg)
903{
904 RT_NOREF1(arg);
905 if (strcmp(data->dtsda_option, "quiet") == 0)
906 g_quiet = data->dtsda_newval != DTRACEOPT_UNSET;
907
908 if (strcmp(data->dtsda_option, "flowindent") == 0)
909 g_flowindent = data->dtsda_newval != DTRACEOPT_UNSET;
910
911 return (DTRACE_HANDLE_OK);
912}
913
914#define BUFDUMPHDR(hdr) \
915 (void) printf("%s: %s%s\n", g_pname, hdr, strlen(hdr) > 0 ? ":" : "");
916
917#define BUFDUMPSTR(ptr, field) \
918 (void) printf("%s: %20s => ", g_pname, #field); \
919 if ((ptr)->field != NULL) { \
920 const char *c = (ptr)->field; \
921 (void) printf("\""); \
922 do { \
923 if (*c == '\n') { \
924 (void) printf("\\n"); \
925 continue; \
926 } \
927 \
928 (void) printf("%c", *c); \
929 } while (*c++ != '\0'); \
930 (void) printf("\"\n"); \
931 } else { \
932 (void) printf("<NULL>\n"); \
933 }
934
935#define BUFDUMPASSTR(ptr, field, str) \
936 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
937
938#define BUFDUMP(ptr, field) \
939 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
940 (long long)(ptr)->field);
941
942#define BUFDUMPPTR(ptr, field) \
943 (void) printf("%s: %20s => %s\n", g_pname, #field, \
944 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
945
946/*ARGSUSED*/
947static int
948bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
949{
950 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
951 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
952 const dtrace_probedesc_t *pd;
953 uint32_t flags = bufdata->dtbda_flags;
954 char buf[512], *c = buf, *end = c + sizeof (buf);
955 int i, printed;
956
957 struct {
958 const char *name;
959 uint32_t value;
960 } flagnames[] = {
961 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
962 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
963 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
964 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
965 { "???", UINT32_MAX },
966 { NULL }
967 };
968 RT_NOREF1(arg);
969
970 if (bufdata->dtbda_probe != NULL) {
971 pd = bufdata->dtbda_probe->dtpda_pdesc;
972 } else if (agg != NULL) {
973 pd = agg->dtada_pdesc;
974 } else {
975 pd = NULL;
976 }
977
978 BUFDUMPHDR(">>> Called buffer handler");
979 BUFDUMPHDR("");
980
981 BUFDUMPHDR(" dtrace_bufdata");
982 BUFDUMPSTR(bufdata, dtbda_buffered);
983 BUFDUMPPTR(bufdata, dtbda_probe);
984 BUFDUMPPTR(bufdata, dtbda_aggdata);
985 BUFDUMPPTR(bufdata, dtbda_recdesc);
986
987 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
988 c += strlen(c);
989
990 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
991 if (!(flags & flagnames[i].value))
992 continue;
993
994 (void) snprintf(c, end - c,
995 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
996 c += strlen(c);
997 flags &= ~flagnames[i].value;
998 }
999
1000 if (printed)
1001 (void) snprintf(c, end - c, ")");
1002
1003 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
1004 BUFDUMPHDR("");
1005
1006 if (pd != NULL) {
1007 BUFDUMPHDR(" dtrace_probedesc");
1008 BUFDUMPSTR(pd, dtpd_provider);
1009 BUFDUMPSTR(pd, dtpd_mod);
1010 BUFDUMPSTR(pd, dtpd_func);
1011 BUFDUMPSTR(pd, dtpd_name);
1012 BUFDUMPHDR("");
1013 }
1014
1015 if (rec != NULL) {
1016 BUFDUMPHDR(" dtrace_recdesc");
1017 BUFDUMP(rec, dtrd_action);
1018 BUFDUMP(rec, dtrd_size);
1019
1020 if (agg != NULL) {
1021 uint8_t *data;
1022 int lim = rec->dtrd_size;
1023
1024 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
1025 c = buf + strlen(buf);
1026
1027 if (lim > sizeof (uint64_t))
1028 lim = sizeof (uint64_t);
1029
1030 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
1031
1032 for (i = 0; i < lim; i++) {
1033 (void) snprintf(c, end - c, "%s%02x",
1034 i == 0 ? "" : " ", *data++);
1035 c += strlen(c);
1036 }
1037
1038 (void) snprintf(c, end - c,
1039 "%s)", lim < rec->dtrd_size ? " ..." : "");
1040 BUFDUMPASSTR(rec, dtrd_offset, buf);
1041 } else {
1042 BUFDUMP(rec, dtrd_offset);
1043 }
1044
1045 BUFDUMPHDR("");
1046 }
1047
1048 if (agg != NULL) {
1049 dtrace_aggdesc_t *desc = agg->dtada_desc;
1050
1051 BUFDUMPHDR(" dtrace_aggdesc");
1052 BUFDUMPSTR(desc, dtagd_name);
1053 BUFDUMP(desc, dtagd_varid);
1054 BUFDUMP(desc, dtagd_id);
1055 BUFDUMP(desc, dtagd_nrecs);
1056 BUFDUMPHDR("");
1057 }
1058
1059 return (DTRACE_HANDLE_OK);
1060}
1061
1062/*ARGSUSED*/
1063static int
1064chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1065{
1066 dtrace_actkind_t act;
1067 uintptr_t addr;
1068 RT_NOREF1(arg);
1069
1070 if (rec == NULL) {
1071 /*
1072 * We have processed the final record; output the newline if
1073 * we're not in quiet mode.
1074 */
1075 if (!g_quiet)
1076 oprintf("\n");
1077
1078 return (DTRACE_CONSUME_NEXT);
1079 }
1080
1081 act = rec->dtrd_action;
1082 addr = (uintptr_t)data->dtpda_data;
1083
1084 if (act == DTRACEACT_EXIT) {
1085 g_status = *((uint32_t *)addr);
1086 return (DTRACE_CONSUME_NEXT);
1087 }
1088
1089 return (DTRACE_CONSUME_THIS);
1090}
1091
1092/*ARGSUSED*/
1093static int
1094chew(const dtrace_probedata_t *data, void *arg)
1095{
1096 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1097 processorid_t cpu = data->dtpda_cpu;
1098 static int heading;
1099 RT_NOREF1(arg);
1100
1101 if (g_impatient) {
1102 g_newline = 0;
1103 return (DTRACE_CONSUME_ABORT);
1104 }
1105
1106 if (heading == 0) {
1107 if (!g_flowindent) {
1108 if (!g_quiet) {
1109 oprintf("%3s %6s %32s\n",
1110 "CPU", "ID", "FUNCTION:NAME");
1111 }
1112 } else {
1113 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1114 }
1115 heading = 1;
1116 }
1117
1118 if (!g_flowindent) {
1119 if (!g_quiet) {
1120 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1121
1122 (void) snprintf(name, sizeof (name), "%s:%s",
1123 pd->dtpd_func, pd->dtpd_name);
1124
1125 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1126 }
1127 } else {
1128 int indent = data->dtpda_indent;
1129 char *name;
1130 size_t len;
1131
1132 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1133 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1134 name = alloca(len);
1135 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1136 data->dtpda_prefix, pd->dtpd_func,
1137 pd->dtpd_name);
1138 } else {
1139 len = indent + DTRACE_FUNCNAMELEN + 5;
1140 name = alloca(len);
1141 (void) snprintf(name, len, "%*s%s%s", indent, "",
1142 data->dtpda_prefix, pd->dtpd_func);
1143 }
1144
1145 oprintf("%3d %-41s ", cpu, name);
1146 }
1147
1148 return (DTRACE_CONSUME_THIS);
1149}
1150
1151static void
1152go(void)
1153{
1154 int i;
1155
1156 struct {
1157 char *name;
1158 char *optname;
1159 dtrace_optval_t val;
1160 } bufs[] = {
1161 { "buffer size", "bufsize" },
1162 { "aggregation size", "aggsize" },
1163 { "speculation size", "specsize" },
1164 { "dynamic variable size", "dynvarsize" },
1165 { NULL }
1166 }, rates[] = {
1167 { "cleaning rate", "cleanrate" },
1168 { "status rate", "statusrate" },
1169 { NULL }
1170 };
1171
1172 for (i = 0; bufs[i].name != NULL; i++) {
1173 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1174 fatal("couldn't get option %s", bufs[i].optname);
1175 }
1176
1177 for (i = 0; rates[i].name != NULL; i++) {
1178 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1179 fatal("couldn't get option %s", rates[i].optname);
1180 }
1181
1182 if (dtrace_go(g_dtp) == -1)
1183 dfatal("could not enable tracing");
1184
1185 for (i = 0; bufs[i].name != NULL; i++) {
1186 dtrace_optval_t j = 0, mul = 10;
1187 dtrace_optval_t nsize;
1188
1189 if (bufs[i].val == DTRACEOPT_UNSET)
1190 continue;
1191
1192 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1193
1194 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1195 continue;
1196
1197 if (nsize >= bufs[i].val - sizeof (uint64_t))
1198 continue;
1199
1200 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1201 continue;
1202
1203 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1204 error("%s lowered to %lld%c\n", bufs[i].name,
1205 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1206 } else {
1207 error("%s lowered to %lld bytes\n", bufs[i].name,
1208 (long long)nsize);
1209 }
1210 }
1211
1212 for (i = 0; rates[i].name != NULL; i++) {
1213 dtrace_optval_t nval;
1214 char *dir;
1215
1216 if (rates[i].val == DTRACEOPT_UNSET)
1217 continue;
1218
1219 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1220
1221 if (nval == DTRACEOPT_UNSET || nval == 0)
1222 continue;
1223
1224 if (rates[i].val == nval)
1225 continue;
1226
1227 dir = nval > rates[i].val ? "reduced" : "increased";
1228
1229 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1230 error("%s %s to %lld hz\n", rates[i].name, dir,
1231 (long long)NANOSEC / (long long)nval);
1232 continue;
1233 }
1234
1235 if ((nval % NANOSEC) == 0) {
1236 error("%s %s to once every %lld seconds\n",
1237 rates[i].name, dir,
1238 (long long)nval / (long long)NANOSEC);
1239 continue;
1240 }
1241
1242 error("%s %s to once every %lld nanoseconds\n",
1243 rates[i].name, dir, (long long)nval);
1244 }
1245}
1246
1247/*ARGSUSED*/
1248static void
1249intr(int signo)
1250{
1251 if (!g_intr)
1252 g_newline = 1;
1253
1254 if (g_intr++)
1255 g_impatient = 1;
1256#ifdef _MSC_VER
1257 /* Reinstall signal handler. Seems MSVCRT is System V style. */
1258 signal(signo, intr);
1259#else
1260 RT_NOREF(signo);
1261#endif
1262}
1263
1264#ifdef VBOX
1265DECLEXPORT(int) RTCALL VBoxDTraceMain(int argc, char **argv)
1266#else
1267int
1268main(int argc, char *argv[])
1269#endif
1270{
1271 dtrace_bufdesc_t buf;
1272#ifndef _MSC_VER
1273 struct sigaction act, oact;
1274#endif
1275 dtrace_status_t status[2];
1276 dtrace_optval_t opt;
1277 dtrace_cmd_t *dcp;
1278
1279 int done = 0, mode = 0;
1280 int err, i;
1281#ifndef VBOX
1282 char c, *p, **v;
1283 struct ps_prochandle *P;
1284 pid_t pid;
1285
1286 g_pname = basename(argv[0]);
1287#else
1288 int c;
1289 char *p;
1290 RTGETOPTUNION ValueUnion;
1291 RTGETOPTSTATE GetState;
1292
1293 err = RTR3InitDll(0);
1294 if (RT_FAILURE(err))
1295 return RTMsgInitFailure(err);
1296 dtrace_init();
1297
1298 g_ofp = stdout;
1299 g_pname = (char *)RTProcShortName();
1300#endif
1301
1302 if (argc == 1)
1303 return (usage(stderr));
1304
1305 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1306 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1307 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1308 fatal("failed to allocate memory for arguments");
1309
1310 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1311 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1312
1313 bzero(status, sizeof (status));
1314 bzero(&buf, sizeof (buf));
1315
1316 /*
1317 * Make an initial pass through argv[] processing any arguments that
1318 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1319 * We also accumulate arguments that are not affiliated with getopt
1320 * options into g_argv[], and abort if any invalid options are found.
1321 */
1322#ifndef VBOX
1323 for (optind = 1; optind < argc; optind++) {
1324 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1325#else
1326 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1327 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1328 {
1329 /* const char *optarg = ValueUnion.psz; - unused */
1330#endif
1331 switch (c) {
1332#ifndef VBOX
1333 case '3':
1334 if (strcmp(optarg, "2") != 0) {
1335 (void) fprintf(stderr,
1336 "%s: illegal option -- 3%s\n",
1337 argv[0], optarg);
1338 return (usage(stderr));
1339 }
1340#else
1341 case 10032:
1342#endif
1343 g_oflags &= ~DTRACE_O_LP64;
1344 g_oflags |= DTRACE_O_ILP32;
1345 break;
1346
1347#ifndef VBOX
1348 case '6':
1349 if (strcmp(optarg, "4") != 0) {
1350 (void) fprintf(stderr,
1351 "%s: illegal option -- 6%s\n",
1352 argv[0], optarg);
1353 return (usage(stderr));
1354 }
1355#else
1356 case 10064:
1357#endif
1358 g_oflags &= ~DTRACE_O_ILP32;
1359 g_oflags |= DTRACE_O_LP64;
1360 break;
1361
1362 case 'a':
1363 g_grabanon++; /* also checked in pass 2 below */
1364 break;
1365
1366 case 'A':
1367 g_mode = DMODE_ANON;
1368 g_exec = 0;
1369 mode++;
1370 break;
1371
1372 case 'e':
1373 g_exec = 0;
1374 done = 1;
1375 break;
1376
1377 case 'h':
1378 g_mode = DMODE_HEADER;
1379 g_oflags |= DTRACE_O_NODEV;
1380 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1381 g_exec = 0;
1382 mode++;
1383 break;
1384
1385#ifndef VBOX
1386 case 'G':
1387 g_mode = DMODE_LINK;
1388 g_oflags |= DTRACE_O_NODEV;
1389 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1390 g_exec = 0;
1391 mode++;
1392 break;
1393#endif
1394
1395 case 'l':
1396 g_mode = DMODE_LIST;
1397 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1398 mode++;
1399 break;
1400
1401 case 'V':
1402 g_mode = DMODE_VERS;
1403 mode++;
1404 break;
1405
1406#ifndef VBOX
1407 default:
1408 if (strchr(DTRACE_OPTSTR, c) == NULL)
1409 return (usage(stderr));
1410#else
1411 case 'c':
1412 case 'p':
1413 case 'G':
1414 fprintf(stderr, "%s: -%c is not supported\n", g_pname, c);
1415 return (E_USAGE);
1416
1417 case VINF_GETOPT_NOT_OPTION:
1418 g_argv[g_argc++] = (char *)ValueUnion.psz;
1419 break;
1420
1421 default:
1422 if (c < 0) { /* Note: Not all options are handled. */
1423 RTGetOptPrintError(c, &ValueUnion);
1424 return (usage(stderr));
1425 }
1426#endif
1427 }
1428 }
1429
1430#ifndef VBOX
1431 if (optind < argc)
1432 g_argv[g_argc++] = argv[optind];
1433#endif
1434 }
1435
1436 if (mode > 1) {
1437 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1438 "can be specified at a time\n", g_pname);
1439 return (E_USAGE);
1440 }
1441
1442 if (g_mode == DMODE_VERS)
1443 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1444
1445#ifndef VBOX
1446 /*
1447 * If we're in linker mode and the data model hasn't been specified,
1448 * we try to guess the appropriate setting by examining the object
1449 * files. We ignore certain errors since we'll catch them later when
1450 * we actually process the object files.
1451 */
1452 if (g_mode == DMODE_LINK &&
1453 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1454 elf_version(EV_CURRENT) != EV_NONE) {
1455 int fd;
1456 Elf *elf;
1457 GElf_Ehdr ehdr;
1458
1459 for (i = 1; i < g_argc; i++) {
1460 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1461 break;
1462
1463 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1464 (void) close(fd);
1465 break;
1466 }
1467
1468 if (elf_kind(elf) != ELF_K_ELF ||
1469 gelf_getehdr(elf, &ehdr) == NULL) {
1470 (void) close(fd);
1471 (void) elf_end(elf);
1472 break;
1473 }
1474
1475 (void) close(fd);
1476 (void) elf_end(elf);
1477
1478 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1479 if (g_oflags & DTRACE_O_ILP32) {
1480 fatal("can't mix 32-bit and 64-bit "
1481 "object files\n");
1482 }
1483 g_oflags |= DTRACE_O_LP64;
1484 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1485 if (g_oflags & DTRACE_O_LP64) {
1486 fatal("can't mix 32-bit and 64-bit "
1487 "object files\n");
1488 }
1489 g_oflags |= DTRACE_O_ILP32;
1490 } else {
1491 break;
1492 }
1493 }
1494 }
1495#endif /* !VBOX */
1496
1497 /*
1498 * Open libdtrace. If we are not actually going to be enabling any
1499 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1500 */
1501 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1502 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1503 g_oflags |= DTRACE_O_NODEV;
1504 continue;
1505 }
1506
1507 fatal("failed to initialize dtrace: %s\n",
1508 dtrace_errmsg(NULL, err));
1509 }
1510
1511 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1512 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1513
1514 /*
1515 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1516 * references to undefined symbols to remain as unresolved relocations.
1517 * If -A is specified, enable -xlink=primary to permit static linking
1518 * only to kernel symbols that are defined in a primary kernel module.
1519 */
1520 if (g_mode == DMODE_LINK) {
1521#ifndef VBOX /* No link mode. */
1522 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1523 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1524
1525 /*
1526 * Use the remaining arguments as the list of object files
1527 * when in linker mode.
1528 */
1529 g_objc = g_argc - 1;
1530 g_objv = g_argv + 1;
1531
1532 /*
1533 * We still use g_argv[0], the name of the executable.
1534 */
1535 g_argc = 1;
1536#else /* VBOX */
1537 AssertFailed();
1538#endif /* VBOX */
1539 } else if (g_mode == DMODE_ANON)
1540 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1541
1542 /*
1543 * Now that we have libdtrace open, make a second pass through argv[]
1544 * to perform any dtrace_setopt() calls and change any compiler flags.
1545 * We also accumulate any program specifications into our g_cmdv[] at
1546 * this time; these will compiled as part of the fourth processing pass.
1547 */
1548#ifndef VBOX
1549 for (optind = 1; optind < argc; optind++) {
1550 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1551#else
1552 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1553 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1554 {
1555 char *optarg = (char *)ValueUnion.psz;
1556#endif
1557
1558 switch (c) {
1559 case 'a':
1560 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1561 dfatal("failed to set -a");
1562 break;
1563
1564 case 'b':
1565 if (dtrace_setopt(g_dtp,
1566 "bufsize", optarg) != 0)
1567 dfatal("failed to set -b %s", optarg);
1568 break;
1569
1570 case 'B':
1571 g_ofp = NULL;
1572 break;
1573
1574 case 'C':
1575 g_cflags |= DTRACE_C_CPP;
1576 break;
1577
1578 case 'D':
1579 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1580 dfatal("failed to set -D %s", optarg);
1581 break;
1582
1583 case 'f':
1584 dcp = &g_cmdv[g_cmdc++];
1585 dcp->dc_func = compile_str;
1586 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1587 dcp->dc_arg = optarg;
1588 break;
1589
1590 case 'F':
1591 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1592 dfatal("failed to set -F");
1593 break;
1594
1595 case 'H':
1596 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1597 dfatal("failed to set -H");
1598 break;
1599
1600 case 'i':
1601 dcp = &g_cmdv[g_cmdc++];
1602 dcp->dc_func = compile_str;
1603 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1604 dcp->dc_arg = optarg;
1605 break;
1606
1607 case 'I':
1608 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1609 dfatal("failed to set -I %s", optarg);
1610 break;
1611
1612 case 'L':
1613 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1614 dfatal("failed to set -L %s", optarg);
1615 break;
1616
1617 case 'm':
1618 dcp = &g_cmdv[g_cmdc++];
1619 dcp->dc_func = compile_str;
1620 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1621 dcp->dc_arg = optarg;
1622 break;
1623
1624 case 'n':
1625 dcp = &g_cmdv[g_cmdc++];
1626 dcp->dc_func = compile_str;
1627 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1628 dcp->dc_arg = optarg;
1629 break;
1630
1631 case 'P':
1632 dcp = &g_cmdv[g_cmdc++];
1633 dcp->dc_func = compile_str;
1634 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1635 dcp->dc_arg = optarg;
1636 break;
1637
1638 case 'q':
1639 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1640 dfatal("failed to set -q");
1641 break;
1642
1643 case 'o':
1644 g_ofile = optarg;
1645 break;
1646
1647 case 's':
1648 dcp = &g_cmdv[g_cmdc++];
1649 dcp->dc_func = compile_file;
1650 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1651 dcp->dc_arg = optarg;
1652 break;
1653
1654 case 'S':
1655 g_cflags |= DTRACE_C_DIFV;
1656 break;
1657
1658 case 'U':
1659 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1660 dfatal("failed to set -U %s", optarg);
1661 break;
1662
1663 case 'v':
1664 g_verbose++;
1665 break;
1666
1667 case 'w':
1668 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1669 dfatal("failed to set -w");
1670 break;
1671
1672 case 'x':
1673 if ((p = strchr(optarg, '=')) != NULL)
1674 *p++ = '\0';
1675
1676 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1677 dfatal("failed to set -x %s", optarg);
1678 break;
1679
1680 case 'X':
1681 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1682 dfatal("failed to set -X %s", optarg);
1683 break;
1684
1685 case 'Z':
1686 g_cflags |= DTRACE_C_ZDEFS;
1687 break;
1688
1689#ifndef VBOX
1690 default:
1691 if (strchr(DTRACE_OPTSTR, c) == NULL)
1692 return (usage(stderr));
1693#else
1694 default:
1695 if (c < 0) { /* Note: Not all options are handled. */
1696 RTGetOptPrintError(c, &ValueUnion);
1697 return (usage(stderr));
1698 }
1699#endif
1700 }
1701 }
1702 }
1703
1704 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1705 (void) fprintf(stderr, "%s: -B not valid in combination"
1706 " with [-AGl] options\n", g_pname);
1707 return (E_USAGE);
1708 }
1709
1710 if (g_ofp == NULL && g_ofile != NULL) {
1711 (void) fprintf(stderr, "%s: -B not valid in combination"
1712 " with -o option\n", g_pname);
1713 return (E_USAGE);
1714 }
1715
1716#ifndef VBOX
1717 /*
1718 * In our third pass we handle any command-line options related to
1719 * grabbing or creating victim processes. The behavior of these calls
1720 * may been affected by any library options set by the second pass.
1721 */
1722# ifndef VBOX
1723 for (optind = 1; optind < argc; optind++) {
1724 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1725# else
1726 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1727 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1728 {
1729 char *optarg = (char *)ValueUnion.psz;
1730# endif
1731 switch (c) {
1732 case 'c':
1733 if ((v = make_argv(optarg)) == NULL)
1734 fatal("failed to allocate memory");
1735
1736 P = dtrace_proc_create(g_dtp, v[0], v);
1737 if (P == NULL)
1738 dfatal(NULL); /* dtrace_errmsg() only */
1739
1740 g_psv[g_psc++] = P;
1741 free(v);
1742 break;
1743
1744 case 'p':
1745 errno = 0;
1746 pid = strtol(optarg, &p, 10);
1747
1748 if (errno != 0 || p == optarg || p[0] != '\0')
1749 fatal("invalid pid: %s\n", optarg);
1750
1751 P = dtrace_proc_grab(g_dtp, pid, 0);
1752 if (P == NULL)
1753 dfatal(NULL); /* dtrace_errmsg() only */
1754
1755 g_psv[g_psc++] = P;
1756 break;
1757 }
1758 }
1759 }
1760#endif /* !VBOX */
1761
1762 /*
1763 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1764 * each string or file specification into a compiled program structure.
1765 */
1766 for (i = 0; i < g_cmdc; i++)
1767 g_cmdv[i].dc_func(&g_cmdv[i]);
1768
1769 if (g_mode != DMODE_LIST) {
1770 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1771 dfatal("failed to establish error handler");
1772
1773 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1774 dfatal("failed to establish drop handler");
1775
1776 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1777 dfatal("failed to establish proc handler");
1778
1779 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1780 dfatal("failed to establish setopt handler");
1781
1782 if (g_ofp == NULL &&
1783 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1784 dfatal("failed to establish buffered handler");
1785 }
1786
1787 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1788 g_flowindent = opt != DTRACEOPT_UNSET;
1789
1790 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1791 g_grabanon = opt != DTRACEOPT_UNSET;
1792
1793 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1794 g_quiet = opt != DTRACEOPT_UNSET;
1795
1796 /*
1797 * Now make a fifth and final pass over the options that have been
1798 * turned into programs and saved in g_cmdv[], performing any mode-
1799 * specific processing. If g_mode is DMODE_EXEC, we will break out
1800 * of the switch() and continue on to the data processing loop. For
1801 * other modes, we will exit dtrace once mode-specific work is done.
1802 */
1803 switch (g_mode) {
1804 case DMODE_EXEC:
1805 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1806 fatal("failed to open output file '%s'", g_ofile);
1807
1808 for (i = 0; i < g_cmdc; i++)
1809 exec_prog(&g_cmdv[i]);
1810
1811 if (done && !g_grabanon) {
1812 dtrace_close(g_dtp);
1813 return (g_status);
1814 }
1815 break;
1816
1817 case DMODE_ANON:
1818 if (g_ofile == NULL)
1819 g_ofile = "/kernel/drv/dtrace.conf";
1820
1821 dof_prune(g_ofile); /* strip out any old DOF directives */
1822 etcsystem_prune(); /* string out any forceload directives */
1823
1824 if (g_cmdc == 0) {
1825 dtrace_close(g_dtp);
1826 return (g_status);
1827 }
1828
1829 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1830 fatal("failed to open output file '%s'", g_ofile);
1831
1832 for (i = 0; i < g_cmdc; i++) {
1833 anon_prog(&g_cmdv[i],
1834 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1835 }
1836
1837 /*
1838 * Dump out the DOF corresponding to the error handler and the
1839 * current options as the final DOF property in the .conf file.
1840 */
1841 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1842 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1843
1844 if (fclose(g_ofp) == EOF)
1845 fatal("failed to close output file '%s'", g_ofile);
1846
1847 /*
1848 * These messages would use notice() rather than error(), but
1849 * we don't want them suppressed when -A is run on a D program
1850 * that itself contains a #pragma D option quiet.
1851 */
1852 error("saved anonymous enabling in %s\n", g_ofile);
1853 etcsystem_add();
1854 error("run update_drv(1M) or reboot to enable changes\n");
1855
1856 dtrace_close(g_dtp);
1857 return (g_status);
1858
1859 case DMODE_LINK:
1860#ifndef VBOX /* No link mode. */
1861 if (g_cmdc == 0) {
1862 (void) fprintf(stderr, "%s: -G requires one or more "
1863 "scripts or enabling options\n", g_pname);
1864 dtrace_close(g_dtp);
1865 return (E_USAGE);
1866 }
1867
1868 for (i = 0; i < g_cmdc; i++)
1869 link_prog(&g_cmdv[i]);
1870
1871 if (g_cmdc > 1 && g_ofile != NULL) {
1872 char **objv = alloca(g_cmdc * sizeof (char *));
1873
1874 for (i = 0; i < g_cmdc; i++)
1875 objv[i] = g_cmdv[i].dc_ofile;
1876
1877 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1878 g_ofile, g_cmdc, objv) != 0)
1879 dfatal(NULL); /* dtrace_errmsg() only */
1880 }
1881
1882 dtrace_close(g_dtp);
1883 return (g_status);
1884#else /* VBOX */
1885 AssertFailed();
1886 dtrace_close(g_dtp);
1887 return (E_USAGE);
1888#endif /* VBOX */
1889
1890 case DMODE_LIST:
1891 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1892 fatal("failed to open output file '%s'", g_ofile);
1893
1894 oprintf("%5s %10s %17s %33s %s\n",
1895 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1896
1897 for (i = 0; i < g_cmdc; i++)
1898 list_prog(&g_cmdv[i]);
1899
1900 if (g_cmdc == 0)
1901 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1902
1903 dtrace_close(g_dtp);
1904 return (g_status);
1905
1906 case DMODE_HEADER:
1907 if (g_cmdc == 0) {
1908 (void) fprintf(stderr, "%s: -h requires one or more "
1909 "scripts or enabling options\n", g_pname);
1910 dtrace_close(g_dtp);
1911 return (E_USAGE);
1912 }
1913
1914 if (g_ofile == NULL) {
1915 char *p;
1916
1917 if (g_cmdc > 1) {
1918 (void) fprintf(stderr, "%s: -h requires an "
1919 "output file if multiple scripts are "
1920 "specified\n", g_pname);
1921 dtrace_close(g_dtp);
1922 return (E_USAGE);
1923 }
1924
1925 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1926 strcmp(p, ".d") != 0) {
1927 (void) fprintf(stderr, "%s: -h requires an "
1928 "output file if no scripts are "
1929 "specified\n", g_pname);
1930 dtrace_close(g_dtp);
1931 return (E_USAGE);
1932 }
1933
1934 p[0] = '\0'; /* strip .d suffix */
1935 g_ofile = p = g_cmdv[0].dc_ofile;
1936 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1937 "%s.h", basename(g_cmdv[0].dc_arg));
1938 }
1939
1940 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1941 fatal("failed to open header file '%s'", g_ofile);
1942
1943 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1944
1945 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1946 fclose(g_ofp) == EOF)
1947 dfatal("failed to create header file %s", g_ofile);
1948
1949 dtrace_close(g_dtp);
1950 return (g_status);
1951 }
1952
1953 /*
1954 * If -a and -Z were not specified and no probes have been matched, no
1955 * probe criteria was specified on the command line and we abort.
1956 */
1957 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1958 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1959
1960 /*
1961 * Start tracing. Once we dtrace_go(), reload any options that affect
1962 * our globals in case consuming anonymous state has changed them.
1963 */
1964 go();
1965
1966 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1967 g_flowindent = opt != DTRACEOPT_UNSET;
1968
1969 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1970 g_grabanon = opt != DTRACEOPT_UNSET;
1971
1972 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1973 g_quiet = opt != DTRACEOPT_UNSET;
1974
1975 (void) dtrace_getopt(g_dtp, "destructive", &opt);
1976 if (opt != DTRACEOPT_UNSET)
1977 notice("allowing destructive actions\n");
1978
1979#ifndef _MSC_VER
1980 (void) sigemptyset(&act.sa_mask);
1981 act.sa_flags = 0;
1982 act.sa_handler = intr;
1983
1984 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1985 (void) sigaction(SIGINT, &act, NULL);
1986
1987 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
1988 (void) sigaction(SIGTERM, &act, NULL);
1989#else
1990 signal(SIGINT, intr);
1991 signal(SIGTERM, intr);
1992#endif
1993
1994 /*
1995 * Now that tracing is active and we are ready to consume trace data,
1996 * continue any grabbed or created processes, setting them running
1997 * using the /proc control mechanism inside of libdtrace.
1998 */
1999#ifndef VBOX
2000 for (i = 0; i < g_psc; i++)
2001 dtrace_proc_continue(g_dtp, g_psv[i]);
2002#endif
2003
2004 g_pslive = g_psc; /* count for prochandler() */
2005
2006 do {
2007 if (!g_intr && !done)
2008 dtrace_sleep(g_dtp);
2009
2010 if (g_newline) {
2011 /*
2012 * Output a newline just to make the output look
2013 * slightly cleaner. Note that we do this even in
2014 * "quiet" mode...
2015 */
2016 oprintf("\n");
2017 g_newline = 0;
2018 }
2019
2020 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
2021 done = 1;
2022 if (dtrace_stop(g_dtp) == -1)
2023 dfatal("couldn't stop tracing");
2024 }
2025
2026 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
2027 case DTRACE_WORKSTATUS_DONE:
2028 done = 1;
2029 break;
2030 case DTRACE_WORKSTATUS_OKAY:
2031 break;
2032 default:
2033 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
2034 dfatal("processing aborted");
2035 }
2036
2037 if (g_ofp != NULL && fflush(g_ofp) == EOF)
2038 clearerr(g_ofp);
2039 } while (!done);
2040
2041 oprintf("\n");
2042
2043 if (!g_impatient) {
2044 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
2045 dtrace_errno(g_dtp) != EINTR)
2046 dfatal("failed to print aggregations");
2047 }
2048
2049 dtrace_close(g_dtp);
2050 return (g_status);
2051}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette