-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdtrace_1.c
2192 lines (1800 loc) · 55.6 KB
/
dtrace_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "@(#)dtrace.c 1.25 06/09/19 SMI"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dtrace.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <alloca.h>
#include <libgen.h>
#include <libproc.h>
#include "arch.h"
#include <mach/mach.h>
#include <mach/machine.h>
#include <sys/sysctl.h>
#include <pthread.h>
#include <System/sys/csr.h>
#include <TargetConditionals.h>
typedef struct dtrace_cmd {
void (*dc_func)(struct dtrace_cmd *); /* function to compile arg */
dtrace_probespec_t dc_spec; /* probe specifier context */
char *dc_arg; /* argument from main argv */
const char *dc_name; /* name for error messages */
const char *dc_desc; /* desc for error messages */
dtrace_prog_t *dc_prog; /* program compiled from arg */
char dc_ofile[PATH_MAX]; /* derived output file name */
} dtrace_cmd_t;
#define DMODE_VERS 0 /* display version information and exit (-V) */
#define DMODE_EXEC 1 /* compile program for enabling (-a/e/E) */
#define DMODE_ANON 2 /* compile program for anonymous tracing (-A) */
#define DMODE_LINK 3 /* compile program for linking with ELF (-G) */
#define DMODE_LIST 4 /* compile program and list probes (-l) */
#define DMODE_HEADER 5 /* compile program for headergen (-h) */
#define E_SUCCESS 0
#define E_ERROR 1
#define E_USAGE 2
// XXX TODO: BX
static const char DTRACE_OPTSTR[] =
":3:6:a:Ab:c:CD:ef:FhHi:I:lL:m:n:o:p:P:qs:SU:vVwW:x:Z";
char *ctf_type_name(ctf_file_t *fp, ctf_id_t type, char *buf, size_t len);
static char **g_argv;
static int g_argc;
static char **g_objv;
static int g_objc;
static dtrace_cmd_t *g_cmdv;
static int g_cmdc;
static struct ps_prochandle **g_psv;
static int g_psc;
static int g_pslive;
static char *g_pname;
static int g_quiet;
static int g_flowindent;
static int g_intr;
static int g_impatient;
static int g_newline;
static int g_total;
static int g_cflags;
static int g_oflags;
static int g_verbose;
static int g_exec = 1;
static int g_mode = DMODE_EXEC;
static int g_status = E_SUCCESS;
static int g_grabanon = 0;
static const char *g_ofile = NULL;
static const char *g_script_name = NULL;
static FILE *g_ofp = NULL;
static dtrace_hdl_t *g_dtp;
static char *g_etcfile = "/etc/system";
static const char *g_etcbegin = "* vvvv Added by DTrace";
static const char *g_etcend = "* ^^^^ Added by DTrace";
static const char *g_etc[] = {
"*",
"* The following forceload directives were added by dtrace(1M) to allow for",
"* tracing during boot. If these directives are removed, the system will",
"* continue to function, but tracing will not occur during boot as desired.",
"* To remove these directives (and this block comment) automatically, run",
"* \"dtrace -A\" without additional arguments. See the \"Anonymous Tracing\"",
"* chapter of the Solaris Dynamic Tracing Guide for details.",
"*",
NULL };
static int
usage(FILE *fp)
{
static const char predact[] = "[[ predicate ] action ]";
(void) fprintf(fp, "Usage: %s [-aACeFHlqSvVwZ] "
"[-arch i386|x86_64] "
"[-b bufsz] [-c cmd] [-D name[=def]]\n\t[-I path] [-L path] "
"[-o output] [-p pid] [-s script] [-U name]\n\t"
"[-x opt[=val]]\n\n"
"\t[-P provider %s]\n"
"\t[-m [ provider: ] module %s]\n"
"\t[-f [[ provider: ] module: ] func %s]\n"
"\t[-n [[[ provider: ] module: ] func: ] name %s]\n"
"\t[-i probe-id %s] [ args ... ]\n\n", g_pname,
predact, predact, predact, predact, predact);
(void) fprintf(fp, "\tpredicate -> '/' D-expression '/'\n");
(void) fprintf(fp, "\t action -> '{' D-statements '}'\n");
(void) fprintf(fp, "\n"
"\t-arch Generate programs and Mach-O files for the specified architecture\n\n"
"\t-a claim anonymous tracing state\n"
"\t-A generate plist(5) entries for anonymous tracing\n"
"\t-b set trace buffer size\n"
"\t-c run specified command and exit upon its completion\n"
"\t-C run cpp(1) preprocessor on script files\n"
"\t-D define symbol when invoking preprocessor\n"
"\t-e exit after compiling request but prior to enabling probes\n"
"\t-f enable or list probes matching the specified function name\n"
"\t-F coalesce trace output by function\n"
"\t-h generate a header file with definitions for static probes\n"
"\t-H print included files when invoking preprocessor\n"
"\t-i enable or list probes matching the specified probe id\n"
"\t-I add include directory to preprocessor search path\n"
"\t-l list probes matching specified criteria\n"
"\t-L add library directory to library search path\n"
"\t-m enable or list probes matching the specified module name\n"
"\t-n enable or list probes matching the specified probe name\n"
"\t-o set output file\n"
"\t-p grab specified process-ID and cache its symbol tables\n"
"\t-P enable or list probes matching the specified provider name\n"
"\t-q set quiet mode (only output explicitly traced data)\n"
"\t-s enable or list probes according to the specified D script\n"
"\t-S print D compiler intermediate code\n"
"\t-U undefine symbol when invoking preprocessor\n"
"\t-v set verbose mode (report stability attributes, arguments)\n"
"\t-V report DTrace API version\n"
"\t-w permit destructive actions\n"
"\t-W wait for specified process and exit upon its completion\n"
"\t-x enable or modify compiler and tracing options\n"
"\t-Z permit probe descriptions that match zero probes\n");
return (E_USAGE);
}
static cpu_type_t current_kernel_arch(void)
{
struct host_basic_info hi;
unsigned int size;
kern_return_t kret;
cpu_type_t current_arch;
int ret, mib[4];
size_t len;
struct kinfo_proc kp;
size = sizeof(hi)/sizeof(int);
kret = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hi, &size);
if (kret != KERN_SUCCESS) {
return 0;
}
current_arch = hi.cpu_type;
/* Now determine if the kernel is running in 64-bit mode */
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = 0; /* kernproc, pid 0 */
len = sizeof(kp);
ret = sysctl(mib, sizeof(mib)/sizeof(mib[0]), &kp, &len, NULL, 0);
if (ret == -1) {
return 0;
}
if (kp.kp_proc.p_flag & P_LP64) {
current_arch |= CPU_ARCH_ABI64;
}
return current_arch;
}
static void
verror(const char *fmt, va_list ap)
{
int error = errno;
(void) fprintf(stderr, "%s: ", g_pname);
(void) vfprintf(stderr, fmt, ap);
if (fmt[strlen(fmt) - 1] != '\n')
(void) fprintf(stderr, ": %s\n", strerror(error));
}
/*PRINTFLIKE1*/
static void
fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verror(fmt, ap);
va_end(ap);
exit(E_ERROR);
}
/*PRINTFLIKE1*/
static void
dfatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void) fprintf(stderr, "%s: ", g_pname);
if (fmt != NULL)
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') {
(void) fprintf(stderr, ": %s\n",
dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
} else if (fmt == NULL) {
(void) fprintf(stderr, "%s\n",
dtrace_errmsg(g_dtp, dtrace_errno(g_dtp)));
}
if (g_dtp) {
int i;
for (i = 0; i < g_psc; i++) {
dtrace_proc_continue(g_dtp, g_psv[i]);
dtrace_proc_release(g_dtp, g_psv[i]);
}
}
/*
* Close the DTrace handle to ensure that any controlled processes are
* correctly restored and continued.
*/
dtrace_close(g_dtp);
exit(E_ERROR);
}
/*PRINTFLIKE1*/
static void
error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verror(fmt, ap);
va_end(ap);
}
/*PRINTFLIKE1*/
static void
notice(const char *fmt, ...)
{
va_list ap;
if (g_quiet)
return; /* -q or quiet pragma suppresses notice()s */
va_start(ap, fmt);
verror(fmt, ap);
va_end(ap);
}
/*PRINTFLIKE1*/
static void
oprintf(const char *fmt, ...)
{
va_list ap;
int n;
if (g_ofp == NULL)
return;
va_start(ap, fmt);
n = vfprintf(g_ofp, fmt, ap);
va_end(ap);
if (n < 0) {
if (errno != EINTR) {
fatal("failed to write to %s",
g_ofile ? g_ofile : "<stdout>");
}
clearerr(g_ofp);
}
}
/*
* Accommodate embedded escaped whitespace in args.
*/
static char **
make_argv(char *s)
{
/* const char *ws = "\f\n\r\t\v "; */
char **argv = malloc(sizeof (char *) * (strlen(s) / 2 + 1));
int argc = 0;
char *p = s;
char *endp = s + strlen(s);
int i;
int j;
const char esc_char = '\\';
char *current_token;
if (argv == NULL)
return (NULL);
/* Skip over any white space at the beginning of s. */
while (p[0] == '\f' || p[0] == '\n'
|| p[0] == '\r' || p[0] == '\t'
|| p[0] == '\v' || p[0] == ' ')
p++;
/* Skip over any white space at the end of s. */
while (endp[0] == '\f' || endp[0] == '\n'
|| endp[0] == '\r' || endp[0] == '\t'
|| endp[0] == '\v' || endp[0] == ' '
|| endp[0] == '\0')
endp--;
/* Now go through p breaking it up into tokens. */
/* (endp - p + 1) is number of characters preceding NUL */
current_token = (char *) malloc ((endp - p + 1) + 1);
i = 0; /* Index into p */
j = 0; /* Index into current_token */
while (i <= endp - p)
{
/* Look for escape character. If found, skip over
it and copy the character following it into
current_token. */
if (p[i] == esc_char)
{
i++;
current_token[j++] = p[i++];
}
/* Otherwise, if any white space character is
found, we're at the end of the current token. */
else if (p[i] == '\f' || p[i] == '\n'
|| p[i] == '\r' || p[i] == '\t'
|| p[i] == '\v' || p[i] == ' ')
{
current_token[j] = '\0'; /* Terminate current token. */
argv[argc++] = current_token; /* Assign cur token to argv list. */
p += i + 1; /* Advance p to start of next token. */
i = 0; /* Re-set i, j, & current_token */
j = 0;
current_token = (char *) malloc ((endp - p + 1) + 1);
}
/* If we've reached the end of the input string, we've
also reached the end of the current token. */
else if (endp - p == i)
{
current_token[j++] = p[i++]; /* Copy last char to current token */
current_token[j] = '\0'; /* Terminate current token. */
argv[argc++] = current_token; /* Assign cur token to argv list. */
}
/* Otherwise, we're in the middle of a token; keep copying the
characters one at a time. */
else
current_token[j++] = p[i++];
}
if (argc == 0)
argv[argc++] = s;
argv[argc] = NULL;
return (argv);
}
static void
dof_prune(const char *fname)
{
struct stat sbuf;
size_t sz, i, j, mark, len;
char *buf;
int msg = 0, fd;
if ((fd = open(fname, O_RDONLY)) == -1) {
/*
* This is okay only if the file doesn't exist at all.
*/
if (errno != ENOENT)
fatal("failed to open %s", fname);
return;
}
if (fstat(fd, &sbuf) == -1)
fatal("failed to fstat %s", fname);
if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
fatal("failed to allocate memory for %s", fname);
if (read(fd, buf, sz) != sz)
fatal("failed to read %s", fname);
buf[sz] = '\0';
(void) close(fd);
if ((fd = open(fname, O_WRONLY | O_TRUNC)) == -1)
fatal("failed to open %s for writing", fname);
len = strlen("dof-data-");
for (mark = 0, i = 0; i < sz; i++) {
if (strncmp(&buf[i], "dof-data-", len) != 0)
continue;
/*
* This is only a match if it's in the 0th column.
*/
if (i != 0 && buf[i - 1] != '\n')
continue;
if (msg++ == 0) {
error("cleaned up old anonymous "
"enabling in %s\n", fname);
}
/*
* We have a match. First write out our data up until now.
*/
if (i != mark) {
if (write(fd, &buf[mark], i - mark) != i - mark)
fatal("failed to write to %s", fname);
}
/*
* Now scan forward until we scan past a newline.
*/
for (j = i; j < sz && buf[j] != '\n'; j++)
continue;
/*
* Reset our mark.
*/
if ((mark = j + 1) >= sz)
break;
i = j;
}
if (mark < sz) {
if (write(fd, &buf[mark], sz - mark) != sz - mark)
fatal("failed to write to %s", fname);
}
(void) close(fd);
free(buf);
}
static void
etcsystem_prune(void)
{
struct stat sbuf;
size_t sz;
char *buf, *start, *end;
int fd;
char *fname = g_etcfile, *tmpname;
if ((fd = open(fname, O_RDONLY)) == -1)
fatal("failed to open %s", fname);
if (fstat(fd, &sbuf) == -1)
fatal("failed to fstat %s", fname);
if ((buf = malloc((sz = sbuf.st_size) + 1)) == NULL)
fatal("failed to allocate memory for %s", fname);
if (read(fd, buf, sz) != sz)
fatal("failed to read %s", fname);
buf[sz] = '\0';
(void) close(fd);
if ((start = strstr(buf, g_etcbegin)) == NULL)
goto out;
if (strlen(buf) != sz) {
fatal("embedded nul byte in %s; manual repair of %s "
"required\n", fname, fname);
}
if (strstr(start + 1, g_etcbegin) != NULL) {
fatal("multiple start sentinels in %s; manual repair of %s "
"required\n", fname, fname);
}
if ((end = strstr(buf, g_etcend)) == NULL) {
fatal("missing end sentinel in %s; manual repair of %s "
"required\n", fname, fname);
}
if (start > end) {
fatal("end sentinel preceeds start sentinel in %s; manual "
"repair of %s required\n", fname, fname);
}
end += strlen(g_etcend) + 1;
bcopy(end, start, strlen(end) + 1);
tmpname = alloca(sz = strlen(fname) + 80);
(void) snprintf(tmpname, sz, "%s.dtrace.%d", fname, getpid());
if ((fd = open(tmpname,
O_WRONLY | O_CREAT | O_EXCL, sbuf.st_mode)) == -1)
fatal("failed to create %s", tmpname);
if (write(fd, buf, strlen(buf)) < strlen(buf)) {
(void) unlink(tmpname);
fatal("failed to write to %s", tmpname);
}
(void) close(fd);
if (chown(tmpname, sbuf.st_uid, sbuf.st_gid) != 0) {
(void) unlink(tmpname);
fatal("failed to chown(2) %s to uid %d, gid %d", tmpname,
(int)sbuf.st_uid, (int)sbuf.st_gid);
}
if (rename(tmpname, fname) == -1)
fatal("rename of %s to %s failed", tmpname, fname);
error("cleaned up forceload directives in %s\n", fname);
out:
free(buf);
}
static void
etcsystem_add(void)
{
const char *mods[20];
int nmods, line;
if ((g_ofp = fopen(g_ofile = g_etcfile, "a")) == NULL)
fatal("failed to open output file '%s'", g_ofile);
oprintf("%s\n", g_etcbegin);
for (line = 0; g_etc[line] != NULL; line++)
oprintf("%s\n", g_etc[line]);
nmods = dtrace_provider_modules(g_dtp, mods,
sizeof (mods) / sizeof (char *) - 1);
if (nmods >= sizeof (mods) / sizeof (char *))
fatal("unexpectedly large number of modules!");
mods[nmods++] = "dtrace";
for (line = 0; line < nmods; line++)
oprintf("forceload: drv/%s\n", mods[line]);
oprintf("%s\n", g_etcend);
if (fclose(g_ofp) == EOF)
fatal("failed to close output file '%s'", g_ofile);
error("added forceload directives to %s\n", g_ofile);
}
static void
print_probe_info(const dtrace_probeinfo_t *p)
{
char buf[BUFSIZ];
int i;
oprintf("\n\tProbe Description Attributes\n");
oprintf("\t\tIdentifier Names: %s\n",
dtrace_stability_name(p->dtp_attr.dtat_name));
oprintf("\t\tData Semantics: %s\n",
dtrace_stability_name(p->dtp_attr.dtat_data));
oprintf("\t\tDependency Class: %s\n",
dtrace_class_name(p->dtp_attr.dtat_class));
oprintf("\n\tArgument Attributes\n");
oprintf("\t\tIdentifier Names: %s\n",
dtrace_stability_name(p->dtp_arga.dtat_name));
oprintf("\t\tData Semantics: %s\n",
dtrace_stability_name(p->dtp_arga.dtat_data));
oprintf("\t\tDependency Class: %s\n",
dtrace_class_name(p->dtp_arga.dtat_class));
oprintf("\n\tArgument Types\n");
for (i = 0; i < p->dtp_argc; i++) {
if (ctf_type_name(p->dtp_argv[i].dtt_ctfp,
p->dtp_argv[i].dtt_type, buf, sizeof (buf)) == NULL)
(void) strlcpy(buf, "(unknown)", sizeof (buf));
oprintf("\t\targs[%d]: %s\n", i, buf);
}
if (p->dtp_argc == 0)
oprintf("\t\tNone\n");
oprintf("\n");
}
/*ARGSUSED*/
static int
info_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
{
dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
dtrace_probedesc_t *pdp = &edp->dted_probe;
dtrace_probeinfo_t p;
if (edp == *last)
return (0);
oprintf("\n%s:%s:%s:%s\n",
pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
if (dtrace_probe_info(dtp, pdp, &p) == 0)
print_probe_info(&p);
*last = edp;
return (0);
}
/*
* Execute the specified program by enabling the corresponding instrumentation.
* If -e has been specified, we get the program info but do not enable it. If
* -v has been specified, we print a stability report for the program.
*/
static void
exec_prog(const dtrace_cmd_t *dcp)
{
dtrace_ecbdesc_t *last = NULL;
dtrace_proginfo_t dpi;
if (!g_exec) {
dtrace_program_info(g_dtp, dcp->dc_prog, &dpi);
} else if (dtrace_program_exec(g_dtp, dcp->dc_prog, &dpi) == -1) {
dfatal("failed to enable '%s'", dcp->dc_name);
} else {
notice("%s '%s' matched %u probe%s\n",
dcp->dc_desc, dcp->dc_name,
dpi.dpi_matches, dpi.dpi_matches == 1 ? "" : "s");
}
if (g_verbose) {
oprintf("\nStability attributes for %s %s:\n",
dcp->dc_desc, dcp->dc_name);
oprintf("\n\tMinimum Probe Description Attributes\n");
oprintf("\t\tIdentifier Names: %s\n",
dtrace_stability_name(dpi.dpi_descattr.dtat_name));
oprintf("\t\tData Semantics: %s\n",
dtrace_stability_name(dpi.dpi_descattr.dtat_data));
oprintf("\t\tDependency Class: %s\n",
dtrace_class_name(dpi.dpi_descattr.dtat_class));
oprintf("\n\tMinimum Statement Attributes\n");
oprintf("\t\tIdentifier Names: %s\n",
dtrace_stability_name(dpi.dpi_stmtattr.dtat_name));
oprintf("\t\tData Semantics: %s\n",
dtrace_stability_name(dpi.dpi_stmtattr.dtat_data));
oprintf("\t\tDependency Class: %s\n",
dtrace_class_name(dpi.dpi_stmtattr.dtat_class));
if (!g_exec) {
(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
(dtrace_stmt_f *)info_stmt, &last);
} else
oprintf("\n");
}
g_total += dpi.dpi_matches;
}
#include <CoreFoundation/CoreFoundation.h>
// There are byte-order dependancies in the dof_hdr emitted by the byte code compiler.
// Rather than swizzle, we'll instead insist that each endian-ness is stored on a
// distinguished property (and only that property is manipulated or referenced by its
// matching architecture.)
#if defined(__BIG_ENDIAN__)
#define BYTE_CODE_CONTAINER CFSTR("Anonymous DOF")
#else
#define BYTE_CODE_CONTAINER CFSTR("DOF Anonymous")
#endif
static void
dof_update_dictionary(const char *fname, CFMutableDictionaryRef dict)
{
CFURLRef fileURL;
CFPropertyListRef propertyList;
CFDataRef xmlData;
CFStringRef errorString;
CFDataRef resourceData;
Boolean status;
SInt32 errorCode;
fileURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
CFStringCreateWithCStringNoCopy(NULL, fname, 0, NULL), // file path name
kCFURLPOSIXPathStyle, // interpret as POSIX path
false ); // is it a directory?
if (NULL == fileURL)
dfatal("failed to open '%s'", fname);
// Read the XML file.
status = CFURLCreateDataAndPropertiesFromResource(
kCFAllocatorDefault,
fileURL,
&resourceData, // place to put file data
NULL,
NULL,
&errorCode);
if (!status)
dfatal("failed in CFURLCreateDataAndPropertiesFromResource with code %d", errorCode);
// Reconstitute the dictionary using the XML data.
propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
resourceData,
kCFPropertyListMutableContainers,
&errorString);
if (NULL == propertyList)
dfatal("failed in CFPropertyListCreateFromXMLData: %s",
CFStringGetCStringPtr(errorString, CFStringGetSystemEncoding()));
CFRelease( resourceData );
CFDictionaryRef IOKPersonalities =
(CFDictionaryRef)CFDictionaryGetValue( propertyList, CFSTR("IOKitPersonalities") );
if (NULL == IOKPersonalities)
dfatal("failed CFDictionaryGetValue for IOKitPersonalities");
CFMutableDictionaryRef dtraceDOF =
(CFMutableDictionaryRef)CFDictionaryGetValue( IOKPersonalities, CFSTR("dtraceDOF") );
if (NULL == dtraceDOF)
dfatal("failed CFDictionaryGetValue for dtraceDOF");
if (dict)
CFDictionarySetValue( dtraceDOF, BYTE_CODE_CONTAINER, dict );
else
CFDictionaryRemoveValue( dtraceDOF, BYTE_CODE_CONTAINER );
// Convert the property list into XML data.
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList );
CFRelease( propertyList );
// Write the XML data to the file.
status = CFURLWriteDataAndPropertiesToResource (
fileURL, // URL to use
xmlData, // data to write
NULL,
&errorCode);
if (!status)
dfatal("failed in CFURLWriteDataAndPropertiesToResource with code %d", errorCode);
CFRelease( xmlData );
if (strstr(fname, "/System/Library/Extensions/"))
utimes("/System/Library/Extensions/", NULL); // "touch" triggers rebuild of mkext archive
}
static void
anon_prog(const dtrace_cmd_t *dcp, dof_hdr_t *dof, int n, CFMutableDictionaryRef dict)
{
char key[32];
CFDataRef data;
CFStringRef keyStr;
if (NULL == dof)
dfatal("failed to create DOF image for '%s'", dcp->dc_name);
snprintf(key, sizeof(key), "dof-data-%d", n);
data = CFDataCreate( kCFAllocatorDefault, (const UInt8 *)dof, dof->dofh_loadsz );
if (NULL == data)
dfatal("failed CFDataCreate for '%s'", dcp->dc_name);
keyStr = CFStringCreateWithCString(kCFAllocatorDefault, key, 0);
CFDictionarySetValue( dict, keyStr, data );
CFRelease( keyStr );
CFRelease( data );
}
static void
dof_install_dictionary(const char *fname)
{
int i;
CFMutableDictionaryRef AnonDOF =
CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
if (NULL == AnonDOF)
dfatal("failed CFDictionaryCreateMutable");
for (i = 0; i < g_cmdc; i++) {
anon_prog(&g_cmdv[i],
dtrace_dof_create(g_dtp, g_cmdv[i].dc_prog, 0), i, AnonDOF);
}
/*
* Dump out the DOF corresponding to the error handler and the
* current options as the final DOF property in the .conf file.
*/
anon_prog(NULL, dtrace_geterr_dof(g_dtp), i++, AnonDOF);
anon_prog(NULL, dtrace_getopt_dof(g_dtp), i++, AnonDOF);
dof_update_dictionary(fname, AnonDOF);
}
/*
* Link the specified D program in DOF form into an ELF file for use in either
* helpers, userland provider definitions, or both. If -o was specified, that
* path is used as the output file name. If -o wasn't specified and the input
* program is from a script whose name is %.d, use basename(%.o) as the output
* file name. Otherwise we use "d.out" as the default output file name.
*/
static void
link_prog(dtrace_cmd_t *dcp)
{
char *p;
if (g_cmdc == 1 && g_ofile != NULL) {
(void) strlcpy(dcp->dc_ofile, g_ofile, sizeof (dcp->dc_ofile));
} else if ((p = strrchr(dcp->dc_arg, '.')) != NULL &&
strcmp(p, ".d") == 0) {
p[0] = '\0'; /* strip .d suffix */
(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
"%s.o", basename(dcp->dc_arg));
} else {
(void) snprintf(dcp->dc_ofile, sizeof (dcp->dc_ofile),
g_cmdc > 1 ? "%s.%d" : "%s", "d.out", (int)(dcp - g_cmdv));
}
if (dtrace_program_link(g_dtp, dcp->dc_prog, DTRACE_D_PROBES,
dcp->dc_ofile, g_objc, g_objv) != 0)
dfatal("failed to link %s %s", dcp->dc_desc, dcp->dc_name);
}
/*ARGSUSED*/
static int
list_probe(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp, void *arg)
{
dtrace_probeinfo_t p;
char funcname[DTRACE_FUNCNAMELEN + DTRACE_FUNCNAMELEN + 4];
char *filtFunc = demangleSymbolCString((const char *)pdp->dtpd_func);
if (NULL == filtFunc)
strncpy( funcname, pdp->dtpd_func, sizeof(funcname) );
else
snprintf( funcname, sizeof(funcname), "%s [%s]", pdp->dtpd_func, filtFunc );
oprintf("%5d %10s %17s %33s %s\n", pdp->dtpd_id,
pdp->dtpd_provider, pdp->dtpd_mod, funcname, pdp->dtpd_name);
if (g_verbose && dtrace_probe_info(dtp, pdp, &p) == 0)
print_probe_info(&p);
if (NULL != filtFunc)
free(filtFunc);
return (0);
}
/*ARGSUSED*/
static int
list_stmt(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
{
dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
if (edp == *last)
return (0);
if (dtrace_probe_iter(g_dtp, &edp->dted_probe, list_probe, NULL) != 0) {
error("failed to match %s:%s:%s:%s: %s\n",
edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
dtrace_errmsg(dtp, dtrace_errno(dtp)));
}
*last = edp;
return (0);
}
/*
* List the probes corresponding to the specified program by iterating over
* each statement and then matching probes to the statement probe descriptions.
*/
static void
list_prog(const dtrace_cmd_t *dcp)
{
dtrace_ecbdesc_t *last = NULL;
(void) dtrace_stmt_iter(g_dtp, dcp->dc_prog,
(dtrace_stmt_f *)list_stmt, &last);
}
static void
compile_file(dtrace_cmd_t *dcp)
{
char *arg0;
FILE *fp;
if ((fp = fopen(dcp->dc_arg, "r")) == NULL)
fatal("failed to open %s", dcp->dc_arg);
arg0 = g_argv[0];
g_argv[0] = dcp->dc_arg;
if ((dcp->dc_prog = dtrace_program_fcompile(g_dtp, fp,
g_cflags, g_argc, g_argv)) == NULL)
dfatal("failed to compile script %s", dcp->dc_arg);
g_argv[0] = arg0;
(void) fclose(fp);
dcp->dc_desc = "script";
dcp->dc_name = dcp->dc_arg;
}
static void
compile_str(dtrace_cmd_t *dcp)
{
char *p;
if ((dcp->dc_prog = dtrace_program_strcompile(g_dtp, dcp->dc_arg,
dcp->dc_spec, g_cflags | DTRACE_C_PSPEC, g_argc, g_argv)) == NULL)
dfatal("invalid probe specifier %s", dcp->dc_arg);
if ((p = strpbrk(dcp->dc_arg, "{/;")) != NULL)
*p = '\0'; /* crop name for reporting */
dcp->dc_desc = "description";
dcp->dc_name = dcp->dc_arg;
}
/*ARGSUSED*/
static void
prochandler(struct ps_prochandle *P, const char *msg, void *arg)
{
#define SIG2STR_MAX 32 /* Not referenced so long as prp just below is NULL. */
#define proc_signame(x,y,z) "Unknown" /* Not referenced so long as prp just below is NULL. */
typedef struct psinfo { int pr_wstat; } psinfo_t;
const psinfo_t *prp = NULL;
int pid = Pstatus(P)->pr_pid;
if (msg != NULL) {
notice("pid %d: %s\n", pid, msg);
return;