VirtualBox

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

Last change on this file was 98182, checked in by vboxsync, 2 years ago

VBoxDTrace: pedantic build fix. bugref:10348

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

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