-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace-stat.c
931 lines (731 loc) · 16.9 KB
/
trace-stat.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
/*
* Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License (not later!)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "trace-local.h"
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
static inline int is_top_instance(struct buffer_instance *instance)
{
return instance == &top_instance;
}
static int get_instance_file_fd(struct buffer_instance *instance,
const char *file)
{
char *path;
int fd;
path = get_instance_file(instance, file);
fd = open(path, O_RDONLY);
tracecmd_put_tracing_file(path);
return fd;
}
char *strstrip(char *str)
{
char *s;
if (!str)
return NULL;
s = str + strlen(str) - 1;
while (s >= str && isspace(*s))
s--;
s++;
*s = '\0';
for (s = str; *s && isspace(*s); s++)
;
return s;
}
char *append_file(const char *dir, const char *name)
{
char *file;
file = malloc(strlen(dir) + strlen(name) + 2);
if (!file)
die("Failed to allocate %s/%s", dir, name);
sprintf(file, "%s/%s", dir, name);
return file;
}
static char *get_fd_content(int fd, const char *file)
{
char *str = NULL;
int cnt = 0;
int ret;
for (;;) {
str = realloc(str, BUFSIZ * ++cnt);
if (!str)
die("malloc");
ret = read(fd, str + BUFSIZ * (cnt - 1), BUFSIZ);
if (ret < 0)
die("reading %s\n", file);
if (ret < BUFSIZ)
break;
}
str[BUFSIZ * (cnt-1) + ret] = 0;
return str;
}
char *get_file_content(const char *file)
{
char *str;
int fd;
fd = open(file, O_RDONLY);
if (fd < 0)
return NULL;
str = get_fd_content(fd, file);
close(fd);
return str;
}
static char *get_instance_file_content(struct buffer_instance *instance,
const char *file)
{
char *str = NULL;
int fd;
fd = get_instance_file_fd(instance, file);
if (fd < 0)
return NULL;
str = get_fd_content(fd, file);
close(fd);
return str;
}
static void report_plugin(struct buffer_instance *instance)
{
char *str;
char *cont;
str = get_instance_file_content(instance, "current_tracer");
if (!str)
return;
cont = strstrip(str);
/* We only care if the plugin is something other than nop */
if (strcmp(cont, "nop") == 0)
goto out;
printf("\nTracer: %s\n", cont);
out:
free(str);
}
struct event_iter *trace_event_iter_alloc(const char *path)
{
struct event_iter *iter;
iter = malloc(sizeof(*iter));
if (!iter)
die("Failed to allocate event_iter for path %s", path);
memset(iter, 0, sizeof(*iter));
iter->system_dir = opendir(path);
if (!iter->system_dir)
die("opendir");
return iter;
}
enum event_iter_type
trace_event_iter_next(struct event_iter *iter, const char *path, const char *system)
{
struct dirent *dent;
if (system && !iter->event_dir) {
char *event;
struct stat st;
event = append_file(path, system);
stat(event, &st);
if (!S_ISDIR(st.st_mode)) {
free(event);
goto do_system;
}
iter->event_dir = opendir(event);
if (!iter->event_dir)
die("opendir %s", event);
free(event);
}
if (iter->event_dir) {
while ((dent = readdir(iter->event_dir))) {
const char *name = dent->d_name;
if (strcmp(name, ".") == 0 ||
strcmp(name, "..") == 0)
continue;
iter->event_dent = dent;
return EVENT_ITER_EVENT;
}
closedir(iter->event_dir);
iter->event_dir = NULL;
}
do_system:
while ((dent = readdir(iter->system_dir))) {
const char *name = dent->d_name;
if (strcmp(name, ".") == 0 ||
strcmp(name, "..") == 0)
continue;
iter->system_dent = dent;
return EVENT_ITER_SYSTEM;
}
return EVENT_ITER_NONE;
}
void trace_event_iter_free(struct event_iter *iter)
{
if (!iter)
return;
if (iter->event_dir)
closedir(iter->event_dir);
closedir(iter->system_dir);
free(iter);
}
static void reset_event_iter(struct event_iter *iter)
{
if (iter->event_dir) {
closedir(iter->event_dir);
iter->event_dir = NULL;
}
rewinddir(iter->system_dir);
}
static int process_individual_events(const char *path, struct event_iter *iter)
{
struct stat st;
const char *system = iter->system_dent->d_name;
char *file;
char *enable = NULL;
char *str;
int ret = 0;
file = append_file(path, system);
stat(file, &st);
if (!S_ISDIR(st.st_mode))
goto out;
enable = append_file(file, "enable");
str = get_file_content(enable);
if (!str)
goto out;
if (*str != '1' && *str != '0')
ret = 1;
free(str);
out:
free(enable);
free(file);
return ret;
}
static void
process_event_enable(char *path, const char *system, const char *name,
enum event_process *processed)
{
struct stat st;
char *enable = NULL;
char *file;
char *str;
if (system)
path = append_file(path, system);
file = append_file(path, name);
if (system)
free(path);
stat(file, &st);
if (!S_ISDIR(st.st_mode))
goto out;
enable = append_file(file, "enable");
str = get_file_content(enable);
if (!str)
goto out;
if (*str == '1') {
if (!system) {
if (!*processed)
printf(" Individual systems:\n");
printf( " %s\n", name);
*processed = PROCESSED_SYSTEM;
} else {
if (!*processed) {
printf(" Individual events:\n");
*processed = PROCESSED_SYSTEM;
}
if (*processed == PROCESSED_SYSTEM) {
printf(" %s\n", system);
*processed = PROCESSED_EVENT;
}
printf( " %s\n", name);
}
}
free(str);
out:
free(enable);
free(file);
}
static void report_events(struct buffer_instance *instance)
{
struct event_iter *iter;
char *str;
char *cont;
char *path;
char *system;
enum event_iter_type type;
enum event_process processed = PROCESSED_NONE;
str = get_instance_file_content(instance, "events/enable");
if (!str)
return;
cont = strstrip(str);
printf("\nEvents:\n");
switch(*cont) {
case '1':
printf(" All enabled\n");
free(str);
return;
case '0':
printf(" All disabled\n");
free(str);
return;
}
free(str);
path = get_instance_file(instance, "events");
if (!path)
die("malloc");
iter = trace_event_iter_alloc(path);
while (trace_event_iter_next(iter, path, NULL)) {
process_event_enable(path, NULL, iter->system_dent->d_name, &processed);
}
reset_event_iter(iter);
processed = PROCESSED_NONE;
system = NULL;
while ((type = trace_event_iter_next(iter, path, system))) {
if (type == EVENT_ITER_SYSTEM) {
/* Only process systems that are not fully enabled */
if (!process_individual_events(path, iter))
continue;
system = iter->system_dent->d_name;
if (processed)
processed = PROCESSED_SYSTEM;
continue;
}
process_event_enable(path, iter->system_dent->d_name,
iter->event_dent->d_name, &processed);
}
trace_event_iter_free(iter);
if (!processed)
printf(" (none enabled)\n");
tracecmd_put_tracing_file(path);
}
static void
process_event_filter(char *path, struct event_iter *iter, enum event_process *processed)
{
const char *system = iter->system_dent->d_name;
const char *event = iter->event_dent->d_name;
struct stat st;
char *filter = NULL;
char *file;
char *str;
char *cont;
path = append_file(path, system);
file = append_file(path, event);
free(path);
stat(file, &st);
if (!S_ISDIR(st.st_mode))
goto out;
filter = append_file(file, "filter");
str = get_file_content(filter);
if (!str)
goto out;
cont = strstrip(str);
if (strcmp(cont, "none") == 0) {
free(str);
goto out;
}
if (!*processed)
printf("\nFilters:\n");
printf( " %s:%s \"%s\"\n", system, event, cont);
*processed = PROCESSED_SYSTEM;
free(str);
out:
free(filter);
free(file);
}
static void report_event_filters(struct buffer_instance *instance)
{
struct event_iter *iter;
char *path;
char *system;
enum event_iter_type type;
enum event_process processed = PROCESSED_NONE;
path = get_instance_file(instance, "events");
if (!path)
die("malloc");
iter = trace_event_iter_alloc(path);
processed = PROCESSED_NONE;
system = NULL;
while ((type = trace_event_iter_next(iter, path, system))) {
if (type == EVENT_ITER_SYSTEM) {
system = iter->system_dent->d_name;
continue;
}
process_event_filter(path, iter, &processed);
}
trace_event_iter_free(iter);
tracecmd_put_tracing_file(path);
}
static void
process_event_trigger(char *path, struct event_iter *iter, enum event_process *processed)
{
const char *system = iter->system_dent->d_name;
const char *event = iter->event_dent->d_name;
struct stat st;
char *trigger = NULL;
char *file;
char *str;
char *cont;
path = append_file(path, system);
file = append_file(path, event);
free(path);
stat(file, &st);
if (!S_ISDIR(st.st_mode))
goto out;
trigger = append_file(file, "trigger");
str = get_file_content(trigger);
if (!str)
goto out;
cont = strstrip(str);
if (cont[0] == '#') {
free(str);
goto out;
}
if (!*processed)
printf("\nTriggers:\n");
printf( " %s:%s \"%s\"\n", system, event, cont);
*processed = PROCESSED_SYSTEM;
free(str);
out:
free(trigger);
free(file);
}
static void report_event_triggers(struct buffer_instance *instance)
{
struct event_iter *iter;
char *path;
char *system;
enum event_iter_type type;
enum event_process processed = PROCESSED_NONE;
path = get_instance_file(instance, "events");
if (!path)
die("malloc");
iter = trace_event_iter_alloc(path);
processed = PROCESSED_NONE;
system = NULL;
while ((type = trace_event_iter_next(iter, path, system))) {
if (type == EVENT_ITER_SYSTEM) {
system = iter->system_dent->d_name;
continue;
}
process_event_trigger(path, iter, &processed);
}
trace_event_iter_free(iter);
tracecmd_put_tracing_file(path);
}
enum func_states {
FUNC_STATE_START,
FUNC_STATE_SKIP,
FUNC_STATE_PRINT,
};
static void list_functions(const char *path, char *string)
{
enum func_states state;
struct stat st;
char *str;
int ret = 0;
int len;
int i;
int first = 0;
/* Ignore if it does not exist. */
ret = stat(path, &st);
if (ret < 0)
return;
str = get_file_content(path);
if (!str)
return;
len = strlen(str);
state = FUNC_STATE_START;
/* Skip all lines that start with '#' */
for (i = 0; i < len; i++) {
if (state == FUNC_STATE_PRINT)
putchar(str[i]);
if (str[i] == '\n') {
state = FUNC_STATE_START;
continue;
}
if (state == FUNC_STATE_SKIP)
continue;
if (state == FUNC_STATE_START && str[i] == '#') {
state = FUNC_STATE_SKIP;
continue;
}
if (!first) {
printf("\n%s:\n", string);
first = 1;
}
if (state != FUNC_STATE_PRINT) {
state = FUNC_STATE_PRINT;
printf(" ");
putchar(str[i]);
}
}
free(str);
}
static void report_graph_funcs(struct buffer_instance *instance)
{
char *path;
path = get_instance_file(instance, "set_graph_function");
if (!path)
die("malloc");
list_functions(path, "Function Graph Filter");
tracecmd_put_tracing_file(path);
path = get_instance_file(instance, "set_graph_notrace");
if (!path)
die("malloc");
list_functions(path, "Function Graph No Trace");
tracecmd_put_tracing_file(path);
}
static void report_ftrace_filters(struct buffer_instance *instance)
{
char *path;
path = get_instance_file(instance, "set_ftrace_filter");
if (!path)
die("malloc");
list_functions(path, "Function Filter");
tracecmd_put_tracing_file(path);
path = get_instance_file(instance, "set_ftrace_notrace");
if (!path)
die("malloc");
list_functions(path, "Function No Trace");
tracecmd_put_tracing_file(path);
}
static void report_buffers(struct buffer_instance *instance)
{
#define FILE_SIZE 100
char *str;
char *cont;
char file[FILE_SIZE];
int cpu;
str = get_instance_file_content(instance, "buffer_size_kb");
if (!str)
return;
cont = strstrip(str);
/* If it's not expanded yet, just skip */
if (strstr(cont, "expanded") != NULL)
goto out;
if (strcmp(cont, "X") != 0) {
printf("\nBuffer size in kilobytes (per cpu):\n");
printf(" %s\n", str);
goto total;
}
/* Read the sizes of each CPU buffer */
for (cpu = 0; ; cpu++) {
snprintf(file, FILE_SIZE, "per_cpu/cpu%d/buffer_size_kb", cpu);
str = get_instance_file_content(instance, file);
if (!str)
break;
cont = strstrip(str);
if (!cpu)
putchar('\n');
printf("CPU %d buffer size (kb): %s\n", cpu, cont);
free(str);
}
total:
free(str);
str = get_instance_file_content(instance, "buffer_total_size_kb");
if (!str)
return;
cont = strstrip(str);
printf("\nBuffer total size in kilobytes:\n");
printf(" %s\n", str);
out:
free(str);
}
static void report_clock(struct buffer_instance *instance)
{
char *str;
char *cont;
char *clock;
str = get_instance_file_content(instance, "trace_clock");
if (!str)
return;
clock = strstr(str, "[");
if (!clock)
goto out;
clock++;
cont = strstr(clock, "]");
if (!cont) /* should never happen */
goto out;
*cont = '\0';
/* Default clock is "local", only show others */
if (strcmp(clock, "local") == 0)
goto out;
printf("\nClock: %s\n", clock);
out:
free(str);
}
static void report_cpumask(struct buffer_instance *instance)
{
char *str;
char *cont;
int cpus;
int n;
int i;
str = get_instance_file_content(instance, "tracing_cpumask");
if (!str)
return;
cont = strstrip(str);
/* check to make sure all CPUs on this machine are set */
cpus = count_cpus();
for (i = strlen(cont) - 1; i >= 0 && cpus > 0; i--) {
if (cont[i] == ',')
continue;
if (cont[i] == 'f') {
cpus -= 4;
continue;
}
if (cpus >= 4)
break;
if (cont[i] >= '0' && cont[i] <= '9')
n = cont[i] - '0';
else
n = 10 + (cont[i] - 'a');
while (cpus > 0) {
if (!(n & 1))
break;
n >>= 1;
cpus--;
}
break;
}
/* If cpus is greater than zero, one isn't set */
if (cpus > 0)
printf("\nCPU mask: %s\n", cont);
free(str);
}
static void report_latency(struct buffer_instance *instance)
{
char *str;
char *cont;
str = get_instance_file_content(instance, "tracing_max_latency");
if (!str)
return;
cont = strstrip(str);
if (strcmp(cont, "0") != 0)
printf("\nMax Latency: %s\n", cont);
free(str);
}
static void report_probes(struct buffer_instance *instance,
const char *file, const char *string)
{
char *str;
char *cont;
int newline;
int i;
str = get_instance_file_content(instance, file);
if (!str)
return;
cont = strstrip(str);
if (strlen(cont) == 0)
goto out;
printf("\n%s:\n", string);
newline = 1;
for (i = 0; cont[i]; i++) {
if (newline)
printf(" ");
putchar(cont[i]);
if (cont[i] == '\n')
newline = 1;
else
newline = 0;
}
putchar('\n');
out:
free(str);
}
static void report_kprobes(struct buffer_instance *instance)
{
report_probes(instance, "kprobe_events", "Kprobe events");
}
static void report_uprobes(struct buffer_instance *instance)
{
report_probes(instance, "uprobe_events", "Uprobe events");
}
static void report_traceon(struct buffer_instance *instance)
{
char *str;
char *cont;
str = get_instance_file_content(instance, "tracing_on");
if (!str)
return;
cont = strstrip(str);
/* double newline as this is the last thing printed */
if (strcmp(cont, "0") == 0)
printf("\nTracing is disabled\n\n");
else
printf("\nTracing is enabled\n\n");
free(str);
}
static void stat_instance(struct buffer_instance *instance)
{
if (instance != &top_instance) {
if (instance != first_instance)
printf("---------------\n");
printf("Instance: %s\n", instance->name);
}
report_plugin(instance);
report_events(instance);
report_event_filters(instance);
report_event_triggers(instance);
report_ftrace_filters(instance);
report_graph_funcs(instance);
report_buffers(instance);
report_clock(instance);
report_cpumask(instance);
report_latency(instance);
report_kprobes(instance);
report_uprobes(instance);
report_traceon(instance);
}
void trace_stat (int argc, char **argv)
{
struct buffer_instance *instance = &top_instance;
int topt = 0;
int c;
for (;;) {
c = getopt(argc-1, argv+1, "tB:");
if (c == -1)
break;
switch (c) {
case 'h':
usage(argv);
break;
case 'B':
instance = create_instance(optarg);
if (!instance)
die("Failed to create instance");
add_instance(instance);
/* top instance requires direct access */
if (!topt && is_top_instance(first_instance))
first_instance = instance;
break;
case 't':
/* Force to use top instance */
topt = 1;
instance = &top_instance;
break;
default:
usage(argv);
}
}
update_first_instance(instance, topt);
for_all_instances(instance) {
stat_instance(instance);
}
exit(0);
}