VirtualBox

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

Last change on this file since 92799 was 83810, checked in by vboxsync, 5 years ago

VBoxDTrace: VC++ 14.1 adjustments and a little fix/optimization. bugref:8489

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.7 KB
Line 
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#ifdef VBOX /* The orignal upsets VC++ 14.1 with variable 'c' shadowing variable in the function.
918 * This version is faster and does not try to print '\0' as that doesn't look like it's intentional */
919#define BUFDUMPSTR(ptr, field) do { \
920 const char *pszField = (ptr)->field; \
921 if (pszField) { \
922 const char *pszStart = pszField; \
923 char ch; \
924 printf("%s: %20s => \"", g_pname, #field); \
925 while ((ch = *pszField) != '\0') { \
926 if (ch != '\n') \
927 pszField++; \
928 else { \
929 printf("%*.*s\\n", (int)(pszField - pszStart), \
930 (int)(pszField - pszStart), pszStart); \
931 pszStart = ++pszField; \
932 } \
933 } \
934 printf("%*.*s\"", (int)(pszField - pszStart), \
935 (int)(pszField - pszStart), pszStart); \
936 } else { \
937 printf("%s: %20s => <NULL>\n", g_pname, #field); \
938 } \
939} while (0)
940#else
941#define BUFDUMPSTR(ptr, field) \
942 (void) printf("%s: %20s => ", g_pname, #field); \
943 if ((ptr)->field != NULL) { \
944 const char *c = (ptr)->field; \
945 (void) printf("\""); \
946 do { \
947 if (*c == '\n') { \
948 (void) printf("\\n"); \
949 continue; \
950 } \
951 \
952 (void) printf("%c", *c); \
953 } while (*c++ != '\0'); \
954 (void) printf("\"\n"); \
955 } else { \
956 (void) printf("<NULL>\n"); \
957 }
958#endif
959
960#define BUFDUMPASSTR(ptr, field, str) \
961 (void) printf("%s: %20s => %s\n", g_pname, #field, str);
962
963#define BUFDUMP(ptr, field) \
964 (void) printf("%s: %20s => %lld\n", g_pname, #field, \
965 (long long)(ptr)->field);
966
967#define BUFDUMPPTR(ptr, field) \
968 (void) printf("%s: %20s => %s\n", g_pname, #field, \
969 (ptr)->field != NULL ? "<non-NULL>" : "<NULL>");
970
971/*ARGSUSED*/
972static int
973bufhandler(const dtrace_bufdata_t *bufdata, void *arg)
974{
975 const dtrace_aggdata_t *agg = bufdata->dtbda_aggdata;
976 const dtrace_recdesc_t *rec = bufdata->dtbda_recdesc;
977 const dtrace_probedesc_t *pd;
978 uint32_t flags = bufdata->dtbda_flags;
979 char buf[512], *c = buf, *end = c + sizeof (buf);
980 int i, printed;
981
982 struct {
983 const char *name;
984 uint32_t value;
985 } flagnames[] = {
986 { "AGGVAL", DTRACE_BUFDATA_AGGVAL },
987 { "AGGKEY", DTRACE_BUFDATA_AGGKEY },
988 { "AGGFORMAT", DTRACE_BUFDATA_AGGFORMAT },
989 { "AGGLAST", DTRACE_BUFDATA_AGGLAST },
990 { "???", UINT32_MAX },
991 { NULL }
992 };
993 RT_NOREF1(arg);
994
995 if (bufdata->dtbda_probe != NULL) {
996 pd = bufdata->dtbda_probe->dtpda_pdesc;
997 } else if (agg != NULL) {
998 pd = agg->dtada_pdesc;
999 } else {
1000 pd = NULL;
1001 }
1002
1003 BUFDUMPHDR(">>> Called buffer handler");
1004 BUFDUMPHDR("");
1005
1006 BUFDUMPHDR(" dtrace_bufdata");
1007 BUFDUMPSTR(bufdata, dtbda_buffered);
1008 BUFDUMPPTR(bufdata, dtbda_probe);
1009 BUFDUMPPTR(bufdata, dtbda_aggdata);
1010 BUFDUMPPTR(bufdata, dtbda_recdesc);
1011
1012 (void) snprintf(c, end - c, "0x%x ", bufdata->dtbda_flags);
1013 c += strlen(c);
1014
1015 for (i = 0, printed = 0; flagnames[i].name != NULL; i++) {
1016 if (!(flags & flagnames[i].value))
1017 continue;
1018
1019 (void) snprintf(c, end - c,
1020 "%s%s", printed++ ? " | " : "(", flagnames[i].name);
1021 c += strlen(c);
1022 flags &= ~flagnames[i].value;
1023 }
1024
1025 if (printed)
1026 (void) snprintf(c, end - c, ")");
1027
1028 BUFDUMPASSTR(bufdata, dtbda_flags, buf);
1029 BUFDUMPHDR("");
1030
1031 if (pd != NULL) {
1032 BUFDUMPHDR(" dtrace_probedesc");
1033 BUFDUMPSTR(pd, dtpd_provider);
1034 BUFDUMPSTR(pd, dtpd_mod);
1035 BUFDUMPSTR(pd, dtpd_func);
1036 BUFDUMPSTR(pd, dtpd_name);
1037 BUFDUMPHDR("");
1038 }
1039
1040 if (rec != NULL) {
1041 BUFDUMPHDR(" dtrace_recdesc");
1042 BUFDUMP(rec, dtrd_action);
1043 BUFDUMP(rec, dtrd_size);
1044
1045 if (agg != NULL) {
1046 uint8_t *data;
1047 int lim = rec->dtrd_size;
1048
1049 (void) sprintf(buf, "%d (data: ", rec->dtrd_offset);
1050 c = buf + strlen(buf);
1051
1052 if (lim > sizeof (uint64_t))
1053 lim = sizeof (uint64_t);
1054
1055 data = (uint8_t *)agg->dtada_data + rec->dtrd_offset;
1056
1057 for (i = 0; i < lim; i++) {
1058 (void) snprintf(c, end - c, "%s%02x",
1059 i == 0 ? "" : " ", *data++);
1060 c += strlen(c);
1061 }
1062
1063 (void) snprintf(c, end - c,
1064 "%s)", lim < rec->dtrd_size ? " ..." : "");
1065 BUFDUMPASSTR(rec, dtrd_offset, buf);
1066 } else {
1067 BUFDUMP(rec, dtrd_offset);
1068 }
1069
1070 BUFDUMPHDR("");
1071 }
1072
1073 if (agg != NULL) {
1074 dtrace_aggdesc_t *desc = agg->dtada_desc;
1075
1076 BUFDUMPHDR(" dtrace_aggdesc");
1077 BUFDUMPSTR(desc, dtagd_name);
1078 BUFDUMP(desc, dtagd_varid);
1079 BUFDUMP(desc, dtagd_id);
1080 BUFDUMP(desc, dtagd_nrecs);
1081 BUFDUMPHDR("");
1082 }
1083
1084 return (DTRACE_HANDLE_OK);
1085}
1086
1087/*ARGSUSED*/
1088static int
1089chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
1090{
1091 dtrace_actkind_t act;
1092 uintptr_t addr;
1093 RT_NOREF1(arg);
1094
1095 if (rec == NULL) {
1096 /*
1097 * We have processed the final record; output the newline if
1098 * we're not in quiet mode.
1099 */
1100 if (!g_quiet)
1101 oprintf("\n");
1102
1103 return (DTRACE_CONSUME_NEXT);
1104 }
1105
1106 act = rec->dtrd_action;
1107 addr = (uintptr_t)data->dtpda_data;
1108
1109 if (act == DTRACEACT_EXIT) {
1110 g_status = *((uint32_t *)addr);
1111 return (DTRACE_CONSUME_NEXT);
1112 }
1113
1114 return (DTRACE_CONSUME_THIS);
1115}
1116
1117/*ARGSUSED*/
1118static int
1119chew(const dtrace_probedata_t *data, void *arg)
1120{
1121 dtrace_probedesc_t *pd = data->dtpda_pdesc;
1122 processorid_t cpu = data->dtpda_cpu;
1123 static int heading;
1124 RT_NOREF1(arg);
1125
1126 if (g_impatient) {
1127 g_newline = 0;
1128 return (DTRACE_CONSUME_ABORT);
1129 }
1130
1131 if (heading == 0) {
1132 if (!g_flowindent) {
1133 if (!g_quiet) {
1134 oprintf("%3s %6s %32s\n",
1135 "CPU", "ID", "FUNCTION:NAME");
1136 }
1137 } else {
1138 oprintf("%3s %-41s\n", "CPU", "FUNCTION");
1139 }
1140 heading = 1;
1141 }
1142
1143 if (!g_flowindent) {
1144 if (!g_quiet) {
1145 char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1146
1147 (void) snprintf(name, sizeof (name), "%s:%s",
1148 pd->dtpd_func, pd->dtpd_name);
1149
1150 oprintf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1151 }
1152 } else {
1153 int indent = data->dtpda_indent;
1154 char *name;
1155 size_t len;
1156
1157 if (data->dtpda_flow == DTRACEFLOW_NONE) {
1158 len = indent + DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 5;
1159 name = alloca(len);
1160 (void) snprintf(name, len, "%*s%s%s:%s", indent, "",
1161 data->dtpda_prefix, pd->dtpd_func,
1162 pd->dtpd_name);
1163 } else {
1164 len = indent + DTRACE_FUNCNAMELEN + 5;
1165 name = alloca(len);
1166 (void) snprintf(name, len, "%*s%s%s", indent, "",
1167 data->dtpda_prefix, pd->dtpd_func);
1168 }
1169
1170 oprintf("%3d %-41s ", cpu, name);
1171 }
1172
1173 return (DTRACE_CONSUME_THIS);
1174}
1175
1176static void
1177go(void)
1178{
1179 int i;
1180
1181 struct {
1182 char *name;
1183 char *optname;
1184 dtrace_optval_t val;
1185 } bufs[] = {
1186 { "buffer size", "bufsize" },
1187 { "aggregation size", "aggsize" },
1188 { "speculation size", "specsize" },
1189 { "dynamic variable size", "dynvarsize" },
1190 { NULL }
1191 }, rates[] = {
1192 { "cleaning rate", "cleanrate" },
1193 { "status rate", "statusrate" },
1194 { NULL }
1195 };
1196
1197 for (i = 0; bufs[i].name != NULL; i++) {
1198 if (dtrace_getopt(g_dtp, bufs[i].optname, &bufs[i].val) == -1)
1199 fatal("couldn't get option %s", bufs[i].optname);
1200 }
1201
1202 for (i = 0; rates[i].name != NULL; i++) {
1203 if (dtrace_getopt(g_dtp, rates[i].optname, &rates[i].val) == -1)
1204 fatal("couldn't get option %s", rates[i].optname);
1205 }
1206
1207 if (dtrace_go(g_dtp) == -1)
1208 dfatal("could not enable tracing");
1209
1210 for (i = 0; bufs[i].name != NULL; i++) {
1211 dtrace_optval_t j = 0, mul = 10;
1212 dtrace_optval_t nsize;
1213
1214 if (bufs[i].val == DTRACEOPT_UNSET)
1215 continue;
1216
1217 (void) dtrace_getopt(g_dtp, bufs[i].optname, &nsize);
1218
1219 if (nsize == DTRACEOPT_UNSET || nsize == 0)
1220 continue;
1221
1222 if (nsize >= bufs[i].val - sizeof (uint64_t))
1223 continue;
1224
1225 for (; (INT64_C(1) << mul) <= nsize; j++, mul += 10)
1226 continue;
1227
1228 if (!(nsize & ((INT64_C(1) << (mul - 10)) - 1))) {
1229 error("%s lowered to %lld%c\n", bufs[i].name,
1230 (long long)nsize >> (mul - 10), " kmgtpe"[j]);
1231 } else {
1232 error("%s lowered to %lld bytes\n", bufs[i].name,
1233 (long long)nsize);
1234 }
1235 }
1236
1237 for (i = 0; rates[i].name != NULL; i++) {
1238 dtrace_optval_t nval;
1239 char *dir;
1240
1241 if (rates[i].val == DTRACEOPT_UNSET)
1242 continue;
1243
1244 (void) dtrace_getopt(g_dtp, rates[i].optname, &nval);
1245
1246 if (nval == DTRACEOPT_UNSET || nval == 0)
1247 continue;
1248
1249 if (rates[i].val == nval)
1250 continue;
1251
1252 dir = nval > rates[i].val ? "reduced" : "increased";
1253
1254 if (nval <= NANOSEC && (NANOSEC % nval) == 0) {
1255 error("%s %s to %lld hz\n", rates[i].name, dir,
1256 (long long)NANOSEC / (long long)nval);
1257 continue;
1258 }
1259
1260 if ((nval % NANOSEC) == 0) {
1261 error("%s %s to once every %lld seconds\n",
1262 rates[i].name, dir,
1263 (long long)nval / (long long)NANOSEC);
1264 continue;
1265 }
1266
1267 error("%s %s to once every %lld nanoseconds\n",
1268 rates[i].name, dir, (long long)nval);
1269 }
1270}
1271
1272/*ARGSUSED*/
1273static void
1274intr(int signo)
1275{
1276 if (!g_intr)
1277 g_newline = 1;
1278
1279 if (g_intr++)
1280 g_impatient = 1;
1281#ifdef _MSC_VER
1282 /* Reinstall signal handler. Seems MSVCRT is System V style. */
1283 signal(signo, intr);
1284#else
1285 RT_NOREF(signo);
1286#endif
1287}
1288
1289#ifdef VBOX
1290DECLEXPORT(int) RTCALL VBoxDTraceMain(int argc, char **argv)
1291#else
1292int
1293main(int argc, char *argv[])
1294#endif
1295{
1296 dtrace_bufdesc_t buf;
1297#ifndef _MSC_VER
1298 struct sigaction act, oact;
1299#endif
1300 dtrace_status_t status[2];
1301 dtrace_optval_t opt;
1302 dtrace_cmd_t *dcp;
1303
1304 int done = 0, mode = 0;
1305 int err, i;
1306#ifndef VBOX
1307 char c, *p, **v;
1308 struct ps_prochandle *P;
1309 pid_t pid;
1310
1311 g_pname = basename(argv[0]);
1312#else
1313 int c;
1314 char *p;
1315 RTGETOPTUNION ValueUnion;
1316 RTGETOPTSTATE GetState;
1317
1318 err = RTR3InitDll(0);
1319 if (RT_FAILURE(err))
1320 return RTMsgInitFailure(err);
1321 dtrace_init();
1322
1323 g_ofp = stdout;
1324 g_pname = (char *)RTProcShortName();
1325#endif
1326
1327 if (argc == 1)
1328 return (usage(stderr));
1329
1330 if ((g_argv = malloc(sizeof (char *) * argc)) == NULL ||
1331 (g_cmdv = malloc(sizeof (dtrace_cmd_t) * argc)) == NULL ||
1332 (g_psv = malloc(sizeof (struct ps_prochandle *) * argc)) == NULL)
1333 fatal("failed to allocate memory for arguments");
1334
1335 g_argv[g_argc++] = argv[0]; /* propagate argv[0] to D as $0/$$0 */
1336 argv[0] = g_pname; /* rewrite argv[0] for getopt errors */
1337
1338 bzero(status, sizeof (status));
1339 bzero(&buf, sizeof (buf));
1340
1341 /*
1342 * Make an initial pass through argv[] processing any arguments that
1343 * affect our behavior mode (g_mode) and flags used for dtrace_open().
1344 * We also accumulate arguments that are not affiliated with getopt
1345 * options into g_argv[], and abort if any invalid options are found.
1346 */
1347#ifndef VBOX
1348 for (optind = 1; optind < argc; optind++) {
1349 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1350#else
1351 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1352 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1353 {
1354 /* const char *optarg = ValueUnion.psz; - unused */
1355#endif
1356 switch (c) {
1357#ifndef VBOX
1358 case '3':
1359 if (strcmp(optarg, "2") != 0) {
1360 (void) fprintf(stderr,
1361 "%s: illegal option -- 3%s\n",
1362 argv[0], optarg);
1363 return (usage(stderr));
1364 }
1365#else
1366 case 10032:
1367#endif
1368 g_oflags &= ~DTRACE_O_LP64;
1369 g_oflags |= DTRACE_O_ILP32;
1370 break;
1371
1372#ifndef VBOX
1373 case '6':
1374 if (strcmp(optarg, "4") != 0) {
1375 (void) fprintf(stderr,
1376 "%s: illegal option -- 6%s\n",
1377 argv[0], optarg);
1378 return (usage(stderr));
1379 }
1380#else
1381 case 10064:
1382#endif
1383 g_oflags &= ~DTRACE_O_ILP32;
1384 g_oflags |= DTRACE_O_LP64;
1385 break;
1386
1387 case 'a':
1388 g_grabanon++; /* also checked in pass 2 below */
1389 break;
1390
1391 case 'A':
1392 g_mode = DMODE_ANON;
1393 g_exec = 0;
1394 mode++;
1395 break;
1396
1397 case 'e':
1398 g_exec = 0;
1399 done = 1;
1400 break;
1401
1402 case 'h':
1403 g_mode = DMODE_HEADER;
1404 g_oflags |= DTRACE_O_NODEV;
1405 g_cflags |= DTRACE_C_ZDEFS; /* -h implies -Z */
1406 g_exec = 0;
1407 mode++;
1408 break;
1409
1410#ifndef VBOX
1411 case 'G':
1412 g_mode = DMODE_LINK;
1413 g_oflags |= DTRACE_O_NODEV;
1414 g_cflags |= DTRACE_C_ZDEFS; /* -G implies -Z */
1415 g_exec = 0;
1416 mode++;
1417 break;
1418#endif
1419
1420 case 'l':
1421 g_mode = DMODE_LIST;
1422 g_cflags |= DTRACE_C_ZDEFS; /* -l implies -Z */
1423 mode++;
1424 break;
1425
1426 case 'V':
1427 g_mode = DMODE_VERS;
1428 mode++;
1429 break;
1430
1431#ifndef VBOX
1432 default:
1433 if (strchr(DTRACE_OPTSTR, c) == NULL)
1434 return (usage(stderr));
1435#else
1436 case 'c':
1437 case 'p':
1438 case 'G':
1439 fprintf(stderr, "%s: -%c is not supported\n", g_pname, c);
1440 return (E_USAGE);
1441
1442 case VINF_GETOPT_NOT_OPTION:
1443 g_argv[g_argc++] = (char *)ValueUnion.psz;
1444 break;
1445
1446 default:
1447 if (c < 0) { /* Note: Not all options are handled. */
1448 RTGetOptPrintError(c, &ValueUnion);
1449 return (usage(stderr));
1450 }
1451#endif
1452 }
1453 }
1454
1455#ifndef VBOX
1456 if (optind < argc)
1457 g_argv[g_argc++] = argv[optind];
1458#endif
1459 }
1460
1461 if (mode > 1) {
1462 (void) fprintf(stderr, "%s: only one of the [-AGhlV] options "
1463 "can be specified at a time\n", g_pname);
1464 return (E_USAGE);
1465 }
1466
1467 if (g_mode == DMODE_VERS)
1468 return (printf("%s: %s\n", g_pname, _dtrace_version) <= 0);
1469
1470#ifndef VBOX
1471 /*
1472 * If we're in linker mode and the data model hasn't been specified,
1473 * we try to guess the appropriate setting by examining the object
1474 * files. We ignore certain errors since we'll catch them later when
1475 * we actually process the object files.
1476 */
1477 if (g_mode == DMODE_LINK &&
1478 (g_oflags & (DTRACE_O_ILP32 | DTRACE_O_LP64)) == 0 &&
1479 elf_version(EV_CURRENT) != EV_NONE) {
1480 int fd;
1481 Elf *elf;
1482 GElf_Ehdr ehdr;
1483
1484 for (i = 1; i < g_argc; i++) {
1485 if ((fd = open64(g_argv[i], O_RDONLY)) == -1)
1486 break;
1487
1488 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1489 (void) close(fd);
1490 break;
1491 }
1492
1493 if (elf_kind(elf) != ELF_K_ELF ||
1494 gelf_getehdr(elf, &ehdr) == NULL) {
1495 (void) close(fd);
1496 (void) elf_end(elf);
1497 break;
1498 }
1499
1500 (void) close(fd);
1501 (void) elf_end(elf);
1502
1503 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
1504 if (g_oflags & DTRACE_O_ILP32) {
1505 fatal("can't mix 32-bit and 64-bit "
1506 "object files\n");
1507 }
1508 g_oflags |= DTRACE_O_LP64;
1509 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
1510 if (g_oflags & DTRACE_O_LP64) {
1511 fatal("can't mix 32-bit and 64-bit "
1512 "object files\n");
1513 }
1514 g_oflags |= DTRACE_O_ILP32;
1515 } else {
1516 break;
1517 }
1518 }
1519 }
1520#endif /* !VBOX */
1521
1522 /*
1523 * Open libdtrace. If we are not actually going to be enabling any
1524 * instrumentation attempt to reopen libdtrace using DTRACE_O_NODEV.
1525 */
1526 while ((g_dtp = dtrace_open(DTRACE_VERSION, g_oflags, &err)) == NULL) {
1527 if (!(g_oflags & DTRACE_O_NODEV) && !g_exec && !g_grabanon) {
1528 g_oflags |= DTRACE_O_NODEV;
1529 continue;
1530 }
1531
1532 fatal("failed to initialize dtrace: %s\n",
1533 dtrace_errmsg(NULL, err));
1534 }
1535
1536 (void) dtrace_setopt(g_dtp, "bufsize", "4m");
1537 (void) dtrace_setopt(g_dtp, "aggsize", "4m");
1538
1539 /*
1540 * If -G is specified, enable -xlink=dynamic and -xunodefs to permit
1541 * references to undefined symbols to remain as unresolved relocations.
1542 * If -A is specified, enable -xlink=primary to permit static linking
1543 * only to kernel symbols that are defined in a primary kernel module.
1544 */
1545 if (g_mode == DMODE_LINK) {
1546#ifndef VBOX /* No link mode. */
1547 (void) dtrace_setopt(g_dtp, "linkmode", "dynamic");
1548 (void) dtrace_setopt(g_dtp, "unodefs", NULL);
1549
1550 /*
1551 * Use the remaining arguments as the list of object files
1552 * when in linker mode.
1553 */
1554 g_objc = g_argc - 1;
1555 g_objv = g_argv + 1;
1556
1557 /*
1558 * We still use g_argv[0], the name of the executable.
1559 */
1560 g_argc = 1;
1561#else /* VBOX */
1562 AssertFailed();
1563#endif /* VBOX */
1564 } else if (g_mode == DMODE_ANON)
1565 (void) dtrace_setopt(g_dtp, "linkmode", "primary");
1566
1567 /*
1568 * Now that we have libdtrace open, make a second pass through argv[]
1569 * to perform any dtrace_setopt() calls and change any compiler flags.
1570 * We also accumulate any program specifications into our g_cmdv[] at
1571 * this time; these will compiled as part of the fourth processing pass.
1572 */
1573#ifndef VBOX
1574 for (optind = 1; optind < argc; optind++) {
1575 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1576#else
1577 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1578 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1579 {
1580 char *optarg = (char *)ValueUnion.psz;
1581#endif
1582
1583 switch (c) {
1584 case 'a':
1585 if (dtrace_setopt(g_dtp, "grabanon", 0) != 0)
1586 dfatal("failed to set -a");
1587 break;
1588
1589 case 'b':
1590 if (dtrace_setopt(g_dtp,
1591 "bufsize", optarg) != 0)
1592 dfatal("failed to set -b %s", optarg);
1593 break;
1594
1595 case 'B':
1596 g_ofp = NULL;
1597 break;
1598
1599 case 'C':
1600 g_cflags |= DTRACE_C_CPP;
1601 break;
1602
1603 case 'D':
1604 if (dtrace_setopt(g_dtp, "define", optarg) != 0)
1605 dfatal("failed to set -D %s", optarg);
1606 break;
1607
1608 case 'f':
1609 dcp = &g_cmdv[g_cmdc++];
1610 dcp->dc_func = compile_str;
1611 dcp->dc_spec = DTRACE_PROBESPEC_FUNC;
1612 dcp->dc_arg = optarg;
1613 break;
1614
1615 case 'F':
1616 if (dtrace_setopt(g_dtp, "flowindent", 0) != 0)
1617 dfatal("failed to set -F");
1618 break;
1619
1620 case 'H':
1621 if (dtrace_setopt(g_dtp, "cpphdrs", 0) != 0)
1622 dfatal("failed to set -H");
1623 break;
1624
1625 case 'i':
1626 dcp = &g_cmdv[g_cmdc++];
1627 dcp->dc_func = compile_str;
1628 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1629 dcp->dc_arg = optarg;
1630 break;
1631
1632 case 'I':
1633 if (dtrace_setopt(g_dtp, "incdir", optarg) != 0)
1634 dfatal("failed to set -I %s", optarg);
1635 break;
1636
1637 case 'L':
1638 if (dtrace_setopt(g_dtp, "libdir", optarg) != 0)
1639 dfatal("failed to set -L %s", optarg);
1640 break;
1641
1642 case 'm':
1643 dcp = &g_cmdv[g_cmdc++];
1644 dcp->dc_func = compile_str;
1645 dcp->dc_spec = DTRACE_PROBESPEC_MOD;
1646 dcp->dc_arg = optarg;
1647 break;
1648
1649 case 'n':
1650 dcp = &g_cmdv[g_cmdc++];
1651 dcp->dc_func = compile_str;
1652 dcp->dc_spec = DTRACE_PROBESPEC_NAME;
1653 dcp->dc_arg = optarg;
1654 break;
1655
1656 case 'P':
1657 dcp = &g_cmdv[g_cmdc++];
1658 dcp->dc_func = compile_str;
1659 dcp->dc_spec = DTRACE_PROBESPEC_PROVIDER;
1660 dcp->dc_arg = optarg;
1661 break;
1662
1663 case 'q':
1664 if (dtrace_setopt(g_dtp, "quiet", 0) != 0)
1665 dfatal("failed to set -q");
1666 break;
1667
1668 case 'o':
1669 g_ofile = optarg;
1670 break;
1671
1672 case 's':
1673 dcp = &g_cmdv[g_cmdc++];
1674 dcp->dc_func = compile_file;
1675 dcp->dc_spec = DTRACE_PROBESPEC_NONE;
1676 dcp->dc_arg = optarg;
1677 break;
1678
1679 case 'S':
1680 g_cflags |= DTRACE_C_DIFV;
1681 break;
1682
1683 case 'U':
1684 if (dtrace_setopt(g_dtp, "undef", optarg) != 0)
1685 dfatal("failed to set -U %s", optarg);
1686 break;
1687
1688 case 'v':
1689 g_verbose++;
1690 break;
1691
1692 case 'w':
1693 if (dtrace_setopt(g_dtp, "destructive", 0) != 0)
1694 dfatal("failed to set -w");
1695 break;
1696
1697 case 'x':
1698 if ((p = strchr(optarg, '=')) != NULL)
1699 *p++ = '\0';
1700
1701 if (dtrace_setopt(g_dtp, optarg, p) != 0)
1702 dfatal("failed to set -x %s", optarg);
1703 break;
1704
1705 case 'X':
1706 if (dtrace_setopt(g_dtp, "stdc", optarg) != 0)
1707 dfatal("failed to set -X %s", optarg);
1708 break;
1709
1710 case 'Z':
1711 g_cflags |= DTRACE_C_ZDEFS;
1712 break;
1713
1714#ifndef VBOX
1715 default:
1716 if (strchr(DTRACE_OPTSTR, c) == NULL)
1717 return (usage(stderr));
1718#else
1719 default:
1720 if (c < 0) { /* Note: Not all options are handled. */
1721 RTGetOptPrintError(c, &ValueUnion);
1722 return (usage(stderr));
1723 }
1724#endif
1725 }
1726 }
1727 }
1728
1729 if (g_ofp == NULL && g_mode != DMODE_EXEC) {
1730 (void) fprintf(stderr, "%s: -B not valid in combination"
1731 " with [-AGl] options\n", g_pname);
1732 return (E_USAGE);
1733 }
1734
1735 if (g_ofp == NULL && g_ofile != NULL) {
1736 (void) fprintf(stderr, "%s: -B not valid in combination"
1737 " with -o option\n", g_pname);
1738 return (E_USAGE);
1739 }
1740
1741#ifndef VBOX
1742 /*
1743 * In our third pass we handle any command-line options related to
1744 * grabbing or creating victim processes. The behavior of these calls
1745 * may been affected by any library options set by the second pass.
1746 */
1747# ifndef VBOX
1748 for (optind = 1; optind < argc; optind++) {
1749 while ((c = getopt(argc, argv, DTRACE_OPTSTR)) != EOF) {
1750# else
1751 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0);
1752 while ((c = RTGetOpt(&GetState, &ValueUnion))) {
1753 {
1754 char *optarg = (char *)ValueUnion.psz;
1755# endif
1756 switch (c) {
1757 case 'c':
1758 if ((v = make_argv(optarg)) == NULL)
1759 fatal("failed to allocate memory");
1760
1761 P = dtrace_proc_create(g_dtp, v[0], v);
1762 if (P == NULL)
1763 dfatal(NULL); /* dtrace_errmsg() only */
1764
1765 g_psv[g_psc++] = P;
1766 free(v);
1767 break;
1768
1769 case 'p':
1770 errno = 0;
1771 pid = strtol(optarg, &p, 10);
1772
1773 if (errno != 0 || p == optarg || p[0] != '\0')
1774 fatal("invalid pid: %s\n", optarg);
1775
1776 P = dtrace_proc_grab(g_dtp, pid, 0);
1777 if (P == NULL)
1778 dfatal(NULL); /* dtrace_errmsg() only */
1779
1780 g_psv[g_psc++] = P;
1781 break;
1782 }
1783 }
1784 }
1785#endif /* !VBOX */
1786
1787 /*
1788 * In our fourth pass we finish g_cmdv[] by calling dc_func to convert
1789 * each string or file specification into a compiled program structure.
1790 */
1791 for (i = 0; i < g_cmdc; i++)
1792 g_cmdv[i].dc_func(&g_cmdv[i]);
1793
1794 if (g_mode != DMODE_LIST) {
1795 if (dtrace_handle_err(g_dtp, &errhandler, NULL) == -1)
1796 dfatal("failed to establish error handler");
1797
1798 if (dtrace_handle_drop(g_dtp, &drophandler, NULL) == -1)
1799 dfatal("failed to establish drop handler");
1800
1801 if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1)
1802 dfatal("failed to establish proc handler");
1803
1804 if (dtrace_handle_setopt(g_dtp, &setopthandler, NULL) == -1)
1805 dfatal("failed to establish setopt handler");
1806
1807 if (g_ofp == NULL &&
1808 dtrace_handle_buffered(g_dtp, &bufhandler, NULL) == -1)
1809 dfatal("failed to establish buffered handler");
1810 }
1811
1812 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1813 g_flowindent = opt != DTRACEOPT_UNSET;
1814
1815 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1816 g_grabanon = opt != DTRACEOPT_UNSET;
1817
1818 (void) dtrace_getopt(g_dtp, "quiet", &opt);
1819 g_quiet = opt != DTRACEOPT_UNSET;
1820
1821 /*
1822 * Now make a fifth and final pass over the options that have been
1823 * turned into programs and saved in g_cmdv[], performing any mode-
1824 * specific processing. If g_mode is DMODE_EXEC, we will break out
1825 * of the switch() and continue on to the data processing loop. For
1826 * other modes, we will exit dtrace once mode-specific work is done.
1827 */
1828 switch (g_mode) {
1829 case DMODE_EXEC:
1830 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1831 fatal("failed to open output file '%s'", g_ofile);
1832
1833 for (i = 0; i < g_cmdc; i++)
1834 exec_prog(&g_cmdv[i]);
1835
1836 if (done && !g_grabanon) {
1837 dtrace_close(g_dtp);
1838 return (g_status);
1839 }
1840 break;
1841
1842 case DMODE_ANON:
1843 if (g_ofile == NULL)
1844 g_ofile = "/kernel/drv/dtrace.conf";
1845
1846 dof_prune(g_ofile); /* strip out any old DOF directives */
1847 etcsystem_prune(); /* string out any forceload directives */
1848
1849 if (g_cmdc == 0) {
1850 dtrace_close(g_dtp);
1851 return (g_status);
1852 }
1853
1854 if ((g_ofp = fopen(g_ofile, "a")) == NULL)
1855 fatal("failed to open output file '%s'", g_ofile);
1856
1857 for (i = 0; i < g_cmdc; i++) {
1858 anon_prog(&g_cmdv[i],
1859 dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i);
1860 }
1861
1862 /*
1863 * Dump out the DOF corresponding to the error handler and the
1864 * current options as the final DOF property in the .conf file.
1865 */
1866 anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++);
1867 anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++);
1868
1869 if (fclose(g_ofp) == EOF)
1870 fatal("failed to close output file '%s'", g_ofile);
1871
1872 /*
1873 * These messages would use notice() rather than error(), but
1874 * we don't want them suppressed when -A is run on a D program
1875 * that itself contains a #pragma D option quiet.
1876 */
1877 error("saved anonymous enabling in %s\n", g_ofile);
1878 etcsystem_add();
1879 error("run update_drv(1M) or reboot to enable changes\n");
1880
1881 dtrace_close(g_dtp);
1882 return (g_status);
1883
1884 case DMODE_LINK:
1885#ifndef VBOX /* No link mode. */
1886 if (g_cmdc == 0) {
1887 (void) fprintf(stderr, "%s: -G requires one or more "
1888 "scripts or enabling options\n", g_pname);
1889 dtrace_close(g_dtp);
1890 return (E_USAGE);
1891 }
1892
1893 for (i = 0; i < g_cmdc; i++)
1894 link_prog(&g_cmdv[i]);
1895
1896 if (g_cmdc > 1 && g_ofile != NULL) {
1897 char **objv = alloca(g_cmdc * sizeof (char *));
1898
1899 for (i = 0; i < g_cmdc; i++)
1900 objv[i] = g_cmdv[i].dc_ofile;
1901
1902 if (dtrace_program_link(g_dtp, NULL, DTRACE_D_PROBES,
1903 g_ofile, g_cmdc, objv) != 0)
1904 dfatal(NULL); /* dtrace_errmsg() only */
1905 }
1906
1907 dtrace_close(g_dtp);
1908 return (g_status);
1909#else /* VBOX */
1910 AssertFailed();
1911 dtrace_close(g_dtp);
1912 return (E_USAGE);
1913#endif /* VBOX */
1914
1915 case DMODE_LIST:
1916 if (g_ofile != NULL && (g_ofp = fopen(g_ofile, "a")) == NULL)
1917 fatal("failed to open output file '%s'", g_ofile);
1918
1919 oprintf("%5s %10s %17s %33s %s\n",
1920 "ID", "PROVIDER", "MODULE", "FUNCTION", "NAME");
1921
1922 for (i = 0; i < g_cmdc; i++)
1923 list_prog(&g_cmdv[i]);
1924
1925 if (g_cmdc == 0)
1926 (void) dtrace_probe_iter(g_dtp, NULL, list_probe, NULL);
1927
1928 dtrace_close(g_dtp);
1929 return (g_status);
1930
1931 case DMODE_HEADER:
1932 if (g_cmdc == 0) {
1933 (void) fprintf(stderr, "%s: -h requires one or more "
1934 "scripts or enabling options\n", g_pname);
1935 dtrace_close(g_dtp);
1936 return (E_USAGE);
1937 }
1938
1939 if (g_ofile == NULL) {
1940#ifndef VBOX
1941 char *p;
1942#endif
1943
1944 if (g_cmdc > 1) {
1945 (void) fprintf(stderr, "%s: -h requires an "
1946 "output file if multiple scripts are "
1947 "specified\n", g_pname);
1948 dtrace_close(g_dtp);
1949 return (E_USAGE);
1950 }
1951
1952 if ((p = strrchr(g_cmdv[0].dc_arg, '.')) == NULL ||
1953 strcmp(p, ".d") != 0) {
1954 (void) fprintf(stderr, "%s: -h requires an "
1955 "output file if no scripts are "
1956 "specified\n", g_pname);
1957 dtrace_close(g_dtp);
1958 return (E_USAGE);
1959 }
1960
1961 p[0] = '\0'; /* strip .d suffix */
1962 g_ofile = p = g_cmdv[0].dc_ofile;
1963 (void) snprintf(p, sizeof (g_cmdv[0].dc_ofile),
1964 "%s.h", basename(g_cmdv[0].dc_arg));
1965 }
1966
1967 if ((g_ofp = fopen(g_ofile, "w")) == NULL)
1968 fatal("failed to open header file '%s'", g_ofile);
1969
1970 oprintf("/*\n * Generated by dtrace(1M).\n */\n\n");
1971
1972 if (dtrace_program_header(g_dtp, g_ofp, g_ofile) != 0 ||
1973 fclose(g_ofp) == EOF)
1974 dfatal("failed to create header file %s", g_ofile);
1975
1976 dtrace_close(g_dtp);
1977 return (g_status);
1978 }
1979
1980 /*
1981 * If -a and -Z were not specified and no probes have been matched, no
1982 * probe criteria was specified on the command line and we abort.
1983 */
1984 if (g_total == 0 && !g_grabanon && !(g_cflags & DTRACE_C_ZDEFS))
1985 dfatal("no probes %s\n", g_cmdc ? "matched" : "specified");
1986
1987 /*
1988 * Start tracing. Once we dtrace_go(), reload any options that affect
1989 * our globals in case consuming anonymous state has changed them.
1990 */
1991 go();
1992
1993 (void) dtrace_getopt(g_dtp, "flowindent", &opt);
1994 g_flowindent = opt != DTRACEOPT_UNSET;
1995
1996 (void) dtrace_getopt(g_dtp, "grabanon", &opt);
1997 g_grabanon = opt != DTRACEOPT_UNSET;
1998
1999 (void) dtrace_getopt(g_dtp, "quiet", &opt);
2000 g_quiet = opt != DTRACEOPT_UNSET;
2001
2002 (void) dtrace_getopt(g_dtp, "destructive", &opt);
2003 if (opt != DTRACEOPT_UNSET)
2004 notice("allowing destructive actions\n");
2005
2006#ifndef _MSC_VER
2007 (void) sigemptyset(&act.sa_mask);
2008 act.sa_flags = 0;
2009 act.sa_handler = intr;
2010
2011 if (sigaction(SIGINT, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
2012 (void) sigaction(SIGINT, &act, NULL);
2013
2014 if (sigaction(SIGTERM, NULL, &oact) == 0 && oact.sa_handler != SIG_IGN)
2015 (void) sigaction(SIGTERM, &act, NULL);
2016#else
2017 signal(SIGINT, intr);
2018 signal(SIGTERM, intr);
2019#endif
2020
2021 /*
2022 * Now that tracing is active and we are ready to consume trace data,
2023 * continue any grabbed or created processes, setting them running
2024 * using the /proc control mechanism inside of libdtrace.
2025 */
2026#ifndef VBOX
2027 for (i = 0; i < g_psc; i++)
2028 dtrace_proc_continue(g_dtp, g_psv[i]);
2029#endif
2030
2031 g_pslive = g_psc; /* count for prochandler() */
2032
2033 do {
2034 if (!g_intr && !done)
2035 dtrace_sleep(g_dtp);
2036
2037 if (g_newline) {
2038 /*
2039 * Output a newline just to make the output look
2040 * slightly cleaner. Note that we do this even in
2041 * "quiet" mode...
2042 */
2043 oprintf("\n");
2044 g_newline = 0;
2045 }
2046
2047 if (done || g_intr || (g_psc != 0 && g_pslive == 0)) {
2048 done = 1;
2049 if (dtrace_stop(g_dtp) == -1)
2050 dfatal("couldn't stop tracing");
2051 }
2052
2053 switch (dtrace_work(g_dtp, g_ofp, chew, chewrec, NULL)) {
2054 case DTRACE_WORKSTATUS_DONE:
2055 done = 1;
2056 break;
2057 case DTRACE_WORKSTATUS_OKAY:
2058 break;
2059 default:
2060 if (!g_impatient && dtrace_errno(g_dtp) != EINTR)
2061 dfatal("processing aborted");
2062 }
2063
2064 if (g_ofp != NULL && fflush(g_ofp) == EOF)
2065 clearerr(g_ofp);
2066 } while (!done);
2067
2068 oprintf("\n");
2069
2070 if (!g_impatient) {
2071 if (dtrace_aggregate_print(g_dtp, g_ofp, NULL) == -1 &&
2072 dtrace_errno(g_dtp) != EINTR)
2073 dfatal("failed to print aggregations");
2074 }
2075
2076 dtrace_close(g_dtp);
2077 return (g_status);
2078}
Note: See TracBrowser for help on using the repository browser.

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