-
Notifications
You must be signed in to change notification settings - Fork 4
/
trace-record.c
2775 lines (2309 loc) · 56.7 KB
/
trace-record.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
/*
* Copyright (C) 2008, 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 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 <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/socket.h>
#ifndef NO_PTRACE
#include <sys/ptrace.h>
#else
#ifdef WARN_NO_PTRACE
#warning ptrace not supported. -c feature will not work
#endif
#endif
#include <netdb.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <sched.h>
#include <glob.h>
#include <errno.h>
#include "trace-local.h"
#define _STR(x) #x
#define STR(x) _STR(x)
#define MAX_PATH 256
#define TRACE_CTRL "tracing_on"
#define TRACE "trace"
#define AVAILABLE "available_tracers"
#define CURRENT "current_tracer"
#define ITER_CTRL "trace_options"
#define MAX_LATENCY "tracing_max_latency"
#define STAMP "stamp"
#define FUNC_STACK_TRACE "func_stack_trace"
#define UDP_MAX_PACKET (65536 - 20)
static int tracing_on_init_val;
static int rt_prio;
static int use_tcp;
static unsigned int page_size;
static int buffer_size;
static const char *output_file = "trace.dat";
static int latency;
static int sleep_time = 1000;
static int cpu_count;
static int recorder_threads;
static int *pids;
static int buffers;
static char *host;
static int *client_ports;
static int sfd;
/* Max size to let a per cpu file get */
static int max_kb;
static int do_ptrace;
static int filter_task;
static int filter_pid = -1;
static int finished;
static unsigned recorder_flags;
/* Try a few times to get an accurate date */
static int date2ts_tries = 5;
struct func_list {
struct func_list *next;
const char *func;
};
static struct func_list *filter_funcs;
static struct func_list *notrace_funcs;
static struct func_list *graph_funcs;
static int func_stack;
struct filter_pids {
struct filter_pids *next;
int pid;
};
static struct filter_pids *filter_pids;
static int nr_filter_pids;
static int len_filter_pids;
struct opt_list {
struct opt_list *next;
const char *option;
};
static struct opt_list *options;
static char *common_pid_filter;
struct event_list {
struct event_list *next;
const char *event;
char *filter;
char *pid_filter;
char *filter_file;
char *enable_file;
int neg;
};
struct tracecmd_event_list *listed_events;
struct events {
struct events *sibling;
struct events *children;
struct events *next;
char *name;
};
struct buffer_instance {
struct buffer_instance *next;
const char *name;
const char *cpumask;
struct event_list *events;
struct event_list **event_next;
struct event_list *sched_switch_event;
struct event_list *sched_wakeup_event;
struct event_list *sched_wakeup_new_event;
int tracing_on_fd;
int keep;
};
static struct buffer_instance top_instance;
static struct buffer_instance *instances;
#define for_each_instance(i) for (i = instances; i; i = (i)->next)
static struct tracecmd_recorder *recorder;
static int ignore_event_not_found = 0;
static void init_instance(struct buffer_instance *instance)
{
instance->event_next = &instance->events;
}
static void add_instance(struct buffer_instance *instance)
{
init_instance(instance);
instance->next = instances;
instances = instance;
buffers++;
}
static void add_event(struct buffer_instance *instance, struct event_list *event)
{
*instance->event_next = event;
instance->event_next = &event->next;
event->next = NULL;
}
static void reset_event_list(struct buffer_instance *instance)
{
instance->events = NULL;
init_instance(instance);
}
static char *get_temp_file(struct buffer_instance *instance, int cpu)
{
const char *name = instance->name;
char *file = NULL;
int size;
if (name) {
size = snprintf(file, 0, "%s.%s.cpu%d", output_file, name, cpu);
file = malloc_or_die(size + 1);
sprintf(file, "%s.%s.cpu%d", output_file, name, cpu);
} else {
size = snprintf(file, 0, "%s.cpu%d", output_file, cpu);
file = malloc_or_die(size + 1);
sprintf(file, "%s.cpu%d", output_file, cpu);
}
return file;
}
static void put_temp_file(char *file)
{
free(file);
}
static void delete_temp_file(struct buffer_instance *instance, int cpu)
{
const char *name = instance->name;
char file[MAX_PATH];
if (name)
snprintf(file, MAX_PATH, "%s.%s.cpu%d", output_file, name, cpu);
else
snprintf(file, MAX_PATH, "%s.cpu%d", output_file, cpu);
unlink(file);
}
static int kill_thread_instance(int start, struct buffer_instance *instance)
{
int n = start;
int i;
for (i = 0; i < cpu_count; i++) {
if (pids[n] > 0) {
kill(pids[n], SIGKILL);
delete_temp_file(instance, i);
pids[n] = 0;
}
n++;
}
return n;
}
static void kill_threads(void)
{
struct buffer_instance *instance;
int i;
if (!recorder_threads || !pids)
return;
i = kill_thread_instance(0, &top_instance);
for_each_instance(instance)
i = kill_thread_instance(i, instance);
}
void die(const char *fmt, ...)
{
va_list ap;
int ret = errno;
if (errno)
perror("trace-cmd");
else
ret = -1;
kill_threads();
va_start(ap, fmt);
fprintf(stderr, " ");
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(ret);
}
static int delete_thread_instance(int start, struct buffer_instance *instance)
{
int n = start;
int i;
for (i = 0; i < cpu_count; i++) {
if (pids) {
if (pids[n]) {
delete_temp_file(instance, i);
if (pids[n] < 0)
pids[n] = 0;
}
n++;
} else
/* Extract does not allocate pids */
delete_temp_file(instance, i);
}
return n;
}
static void delete_thread_data(void)
{
struct buffer_instance *instance;
int i;
i = delete_thread_instance(0, &top_instance);
for_each_instance(instance)
i = delete_thread_instance(i, instance);
}
static void stop_threads(void)
{
int i;
if (!cpu_count)
return;
for (i = 0; i < recorder_threads; i++) {
if (pids[i] > 0) {
kill(pids[i], SIGINT);
waitpid(pids[i], NULL, 0);
pids[i] = -1;
}
}
}
static int create_recorder(struct buffer_instance *instance, int cpu, int extract);
static void flush_threads(void)
{
long ret;
int i;
if (!cpu_count)
return;
for (i = 0; i < cpu_count; i++) {
/* Extract doesn't support sub buffers yet */
ret = create_recorder(&top_instance, i, 1);
if (ret < 0)
die("error reading ring buffer");
}
}
static int set_ftrace_enable(const char *path, int set)
{
struct stat st;
int fd;
char *val = set ? "1" : "0";
int ret;
/* if ftace_enable does not exist, simply ignore it */
fd = stat(path, &st);
if (fd < 0)
return -ENODEV;
ret = -1;
fd = open(path, O_WRONLY);
if (fd < 0)
goto out;
/* Now set or clear the function option */
ret = write(fd, val, 1);
close(fd);
out:
return ret < 0 ? ret : 0;
}
static int set_ftrace_proc(int set)
{
const char *path = "/proc/sys/kernel/ftrace_enabled";
int ret;
ret = set_ftrace_enable(path, set);
if (ret == -1)
die ("Can't %s ftrace", set ? "enable" : "disable");
return ret;
}
static int set_ftrace(int set, int use_proc)
{
char *path;
int ret;
/* First check if the function-trace option exists */
path = tracecmd_get_tracing_file("options/function-trace");
ret = set_ftrace_enable(path, set);
tracecmd_put_tracing_file(path);
/* Always enable ftrace_enable proc file when set is true */
if (ret < 0 || set || use_proc)
ret = set_ftrace_proc(set);
return 0;
}
static char *
get_instance_file(struct buffer_instance *instance, const char *file)
{
char *buf;
char *path;
if (instance->name) {
buf = malloc_or_die(strlen(instance->name) +
strlen(file) + strlen("instances//") + 1);
sprintf(buf, "instances/%s/%s", instance->name, file);
path = tracecmd_get_tracing_file(buf);
free(buf);
} else
path = tracecmd_get_tracing_file(file);
return path;
}
static void __clear_trace(struct buffer_instance *instance)
{
FILE *fp;
char *path;
/* reset the trace */
path = get_instance_file(instance, "trace");
fp = fopen(path, "w");
if (!fp)
die("writing to '%s'", path);
tracecmd_put_tracing_file(path);
fwrite("0", 1, 1, fp);
fclose(fp);
}
static void clear_trace(void)
{
struct buffer_instance *instance;
__clear_trace(&top_instance);
for_each_instance(instance)
__clear_trace(instance);
}
static void reset_max_latency(void)
{
FILE *fp;
char *path;
/* reset the trace */
path = tracecmd_get_tracing_file("tracing_max_latency");
fp = fopen(path, "w");
if (!fp)
die("writing to '%s'", path);
tracecmd_put_tracing_file(path);
fwrite("0", 1, 1, fp);
fclose(fp);
}
static void add_filter_pid(int pid)
{
struct filter_pids *p;
char buf[100];
p = malloc_or_die(sizeof(*p));
p->next = filter_pids;
p->pid = pid;
filter_pids = p;
nr_filter_pids++;
len_filter_pids += sprintf(buf, "%d", pid);
}
static void update_ftrace_pid(const char *pid, int reset)
{
static char *path;
int ret;
static int fd = -1;
if (!pid) {
if (fd >= 0)
close(fd);
if (path)
tracecmd_put_tracing_file(path);
fd = -1;
path = NULL;
return;
}
/* Force reopen on reset */
if (reset && fd >= 0) {
close(fd);
fd = -1;
}
if (fd < 0) {
if (!path)
path = tracecmd_get_tracing_file("set_ftrace_pid");
if (!path)
return;
fd = open(path, O_WRONLY | O_CLOEXEC | (reset ? O_TRUNC : 0));
if (fd < 0)
return;
}
ret = write(fd, pid, strlen(pid));
/*
* Older kernels required "-1" to disable pid
*/
if (ret < 0 && !strlen(pid))
ret = write(fd, "-1", 2);
if (ret < 0)
die("error writing to %s", path);
/* add whitespace in case another pid is written */
write(fd, " ", 1);
}
static void update_ftrace_pids(int reset)
{
char buf[100];
struct filter_pids *pid;
for (pid = filter_pids; pid; pid = pid->next) {
snprintf(buf, 100, "%d ", pid->pid);
update_ftrace_pid(buf, reset);
/* Only reset the first entry */
reset = 0;
}
}
static void update_event_filters(struct buffer_instance *instance);
static void update_pid_event_filters(struct buffer_instance *instance);
static void enable_tracing(void);
/**
* make_pid_filter - create a filter string to all pids against @field
* @curr_filter: Append to a previous filter (may realloc). Can be NULL
* @field: The fild to compare the pids against
*
* Creates a new string or appends to an existing one if @curr_filter
* is not NULL. The new string will contain a filter with all pids
* in pid_filter list with the format (@field == pid) || ..
* If @curr_filter is not NULL, it will add this string as:
* (@curr_filter) && ((@field == pid) || ...)
*/
static char *make_pid_filter(char *curr_filter, const char *field)
{
struct filter_pids *p;
char *filter;
char *orit;
char *str;
int curr_len = 0;
int len;
len = len_filter_pids + (strlen(field) + strlen("(==)||")) * nr_filter_pids;
if (curr_filter) {
curr_len = strlen(curr_filter);
filter = realloc(curr_filter, curr_len + len + strlen("(&&())"));
if (!filter)
die("realloc");
memmove(filter+1, curr_filter, curr_len);
filter[0] = '(';
strcat(filter, ")&&(");
curr_len = strlen(filter);
} else
filter = malloc_or_die(len);
/* Last '||' that is not used will cover the \0 */
str = filter + curr_len;
for (p = filter_pids; p; p = p->next) {
if (p == filter_pids)
orit = "";
else
orit = "||";
len = sprintf(str, "%s(%s==%d)", orit, field, p->pid);
str += len;
}
if (curr_len)
sprintf(str, ")");
return filter;
}
static void update_task_filter(void)
{
struct buffer_instance *instance;
int pid = getpid();
if (filter_task)
add_filter_pid(pid);
if (!filter_pids)
return;
common_pid_filter = make_pid_filter(NULL, "common_pid");
update_ftrace_pids(1);
update_pid_event_filters(&top_instance);
for_each_instance(instance)
update_pid_event_filters(&top_instance);
}
#ifndef NO_PTRACE
/**
* append_pid_filter - add a new pid to an existing filter
* @curr_filter: the filter to append to. If NULL, then allocate one
* @field: The fild to compare the pid to
* @pid: The pid to add to.
*/
static char *append_pid_filter(char *curr_filter, const char *field, int pid)
{
char *filter;
int len;
len = snprintf(NULL, 0, "(%s==%d)||", field, pid);
if (!curr_filter) {
/* No need for +1 as we don't use the "||" */
filter = malloc_or_die(len);
sprintf(filter, "(%s==%d)", field, pid);
} else {
int indx = strlen(curr_filter);
len += indx;
filter = realloc(curr_filter, len + indx + 1);
if (!filter)
die("realloc");
sprintf(filter + indx, "||(%s==%d)", field, pid);
}
return filter;
}
static void append_sched_event(struct event_list *event, const char *field, int pid)
{
if (!event || !event->pid_filter)
return;
event->pid_filter = append_pid_filter(event->pid_filter, field, pid);
}
static void update_sched_events(struct buffer_instance *instance, int pid)
{
/*
* Also make sure that the sched_switch to this pid
* and wakeups of this pid are also traced.
* Only need to do this if the events are active.
*/
append_sched_event(instance->sched_switch_event, "next_pid", pid);
append_sched_event(instance->sched_wakeup_event, "pid", pid);
append_sched_event(instance->sched_wakeup_new_event, "pid", pid);
}
static void add_new_filter_pid(int pid)
{
struct buffer_instance *instance;
char buf[100];
add_filter_pid(pid);
sprintf(buf, "%d", pid);
update_ftrace_pid(buf, 0);
common_pid_filter = append_pid_filter(common_pid_filter, "common_pid", pid);
update_sched_events(&top_instance, pid);
update_event_filters(&top_instance);
for_each_instance(instance) {
update_sched_events(instance, pid);
update_event_filters(instance);
}
}
static void ptrace_attach(int pid)
{
int ret;
ret = ptrace(PTRACE_ATTACH, pid, NULL, 0);
if (ret < 0) {
warning("Unable to trace process %d children", pid);
do_ptrace = 0;
return;
}
add_filter_pid(pid);
}
static void enable_ptrace(void)
{
if (!do_ptrace || !filter_task)
return;
ptrace(PTRACE_TRACEME, 0, NULL, 0);
}
static void ptrace_wait(int main_pid)
{
unsigned long send_sig;
unsigned long child;
siginfo_t sig;
int cstatus;
int status;
int event;
int pid;
int ret;
do {
ret = waitpid(-1, &status, WSTOPPED | __WALL);
if (ret < 0)
continue;
pid = ret;
if (WIFSTOPPED(status)) {
event = (status >> 16) & 0xff;
ptrace(PTRACE_GETSIGINFO, pid, NULL, &sig);
send_sig = sig.si_signo;
/* Don't send ptrace sigs to child */
if (send_sig == SIGTRAP || send_sig == SIGSTOP)
send_sig = 0;
switch (event) {
case PTRACE_EVENT_FORK:
case PTRACE_EVENT_VFORK:
case PTRACE_EVENT_CLONE:
/* forked a child */
ptrace(PTRACE_GETEVENTMSG, pid, NULL, &child);
ptrace(PTRACE_SETOPTIONS, child, NULL,
PTRACE_O_TRACEFORK |
PTRACE_O_TRACEVFORK |
PTRACE_O_TRACECLONE |
PTRACE_O_TRACEEXIT);
add_new_filter_pid(child);
ptrace(PTRACE_CONT, child, NULL, 0);
break;
case PTRACE_EVENT_EXIT:
ptrace(PTRACE_GETEVENTMSG, pid, NULL, &cstatus);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
break;
}
ptrace(PTRACE_SETOPTIONS, pid, NULL,
PTRACE_O_TRACEFORK |
PTRACE_O_TRACEVFORK |
PTRACE_O_TRACECLONE |
PTRACE_O_TRACEEXIT);
ptrace(PTRACE_CONT, pid, NULL, send_sig);
}
} while (!finished && ret > 0 &&
(!WIFEXITED(status) || pid != main_pid));
}
#else
static inline void ptrace_wait(int main_pid) { }
static inline void enable_ptrace(void) { }
static inline void ptrace_attach(int pid) { }
#endif /* NO_PTRACE */
void trace_or_sleep(void)
{
if (do_ptrace && filter_pid >= 0)
ptrace_wait(filter_pid);
else
sleep(10);
}
void run_cmd(int argc, char **argv)
{
int status;
int pid;
if ((pid = fork()) < 0)
die("failed to fork");
if (!pid) {
/* child */
update_task_filter();
enable_tracing();
enable_ptrace();
if (execvp(argv[0], argv)) {
fprintf(stderr, "\n********************\n");
fprintf(stderr, " Unable to exec %s\n", argv[0]);
fprintf(stderr, "********************\n");
die("Failed to exec %s", argv[0]);
}
}
if (do_ptrace) {
add_filter_pid(pid);
ptrace_wait(pid);
} else
waitpid(pid, &status, 0);
}
static void set_plugin(const char *name)
{
FILE *fp;
char *path;
char zero = '0';
path = tracecmd_get_tracing_file("current_tracer");
fp = fopen(path, "w");
if (!fp)
die("writing to '%s'", path);
tracecmd_put_tracing_file(path);
fwrite(name, 1, strlen(name), fp);
fclose(fp);
if (strncmp(name, "function", 8) != 0)
return;
/* Make sure func_stack_trace option is disabled */
path = tracecmd_get_tracing_file("options/func_stack_trace");
fp = fopen(path, "w");
tracecmd_put_tracing_file(path);
if (!fp)
return;
fwrite(&zero, 1, 1, fp);
fclose(fp);
}
static void save_option(const char *option)
{
struct opt_list *opt;
opt = malloc_or_die(sizeof(*opt));
opt->next = options;
options = opt;
opt->option = option;
}
static void set_option(const char *option)
{
FILE *fp;
char *path;
path = tracecmd_get_tracing_file("trace_options");
fp = fopen(path, "w");
if (!fp)
die("writing to '%s'", path);
tracecmd_put_tracing_file(path);
fwrite(option, 1, strlen(option), fp);
fclose(fp);
}
static void set_options(void)
{
struct opt_list *opt;
while (options) {
opt = options;
options = opt->next;
set_option(opt->option);
free(opt);
}
}
static int use_old_event_method(void)
{
static int old_event_method;
static int processed;
struct stat st;
char *path;
int ret;
if (processed)
return old_event_method;
/* Check if the kernel has the events/enable file */
path = tracecmd_get_tracing_file("events/enable");
ret = stat(path, &st);
tracecmd_put_tracing_file(path);
if (ret < 0)
old_event_method = 1;
processed = 1;
return old_event_method;
}
static void old_update_events(const char *name, char update)
{
char *path;
FILE *fp;
int ret;
if (strcmp(name, "all") == 0)
name = "*:*";
/* need to use old way */
path = tracecmd_get_tracing_file("set_event");
fp = fopen(path, "w");
if (!fp)
die("opening '%s'", path);
tracecmd_put_tracing_file(path);
/* Disable the event with "!" */
if (update == '0')
fwrite("!", 1, 1, fp);
ret = fwrite(name, 1, strlen(name), fp);
if (ret < 0)
die("bad event '%s'", name);
ret = fwrite("\n", 1, 1, fp);
if (ret < 0)
die("bad event '%s'", name);
fclose(fp);
return;
}
static void reset_events(void)
{
glob_t globbuf;
char *path;
char c;
int fd;
int i;
int ret;
if (use_old_event_method()) {
old_update_events("all", '0');
return;
}
c = '0';
path = tracecmd_get_tracing_file("events/enable");
fd = open(path, O_WRONLY);
if (fd < 0)
die("opening to '%s'", path);
ret = write(fd, &c, 1);
close(fd);
tracecmd_put_tracing_file(path);
path = tracecmd_get_tracing_file("events/*/filter");
globbuf.gl_offs = 0;
ret = glob(path, 0, NULL, &globbuf);
tracecmd_put_tracing_file(path);
if (ret < 0)
return;
for (i = 0; i < globbuf.gl_pathc; i++) {
path = globbuf.gl_pathv[i];
fd = open(path, O_WRONLY);
if (fd < 0)
die("opening to '%s'", path);
ret = write(fd, &c, 1);
close(fd);
}
globfree(&globbuf);
}
static void write_filter(const char *file, const char *filter)
{
char buf[BUFSIZ];
int fd;
int ret;
fd = open(file, O_WRONLY);
if (fd < 0)
die("opening to '%s'", file);
ret = write(fd, filter, strlen(filter));
close(fd);
if (ret < 0) {
/* filter failed */
fd = open(file, O_RDONLY);
if (fd < 0)
die("writing to '%s'", file);
/* the filter has the error */
while ((ret = read(fd, buf, BUFSIZ)) > 0)
fprintf(stderr, "%.*s", ret, buf);
die("Failed filter of %s\n", file);
close(fd);
}
}
static void
update_event(struct event_list *event, const char *filter,
int filter_only, char update)
{
const char *name = event->event;
FILE *fp;
char *path;
int ret;
if (use_old_event_method()) {
if (filter_only)
return;
old_update_events(name, update);
return;
}