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