-
Notifications
You must be signed in to change notification settings - Fork 1
/
trace-record.c
2256 lines (1885 loc) · 44.4 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define _GNU_SOURCE
#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 *pids;
static char *host;
static int *client_ports;
static int sfd;
static int do_ptrace;
static int filter_task;
static int filter_pid = -1;
static int finished;
/* 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;
struct event_list {
struct event_list *next;
const char *event;
char *filter;
char *filter_file;
char *enable_file;
int neg;
};
static struct event_list *sched_switch_event;
static struct event_list *sched_wakeup_event;
static struct event_list *sched_wakeup_new_event;
static struct event_list *sched_event;
static struct event_list *event_selection;
struct tracecmd_event_list *listed_events;
struct events {
struct events *sibling;
struct events *children;
struct events *next;
char *name;
};
static struct tracecmd_recorder *recorder;
static int ignore_event_not_found = 0;
static char *get_temp_file(int cpu)
{
char *file = NULL;
int size;
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(int cpu)
{
char file[MAX_PATH];
snprintf(file, MAX_PATH, "%s.cpu%d", output_file, cpu);
unlink(file);
}
static void kill_threads(void)
{
int i;
if (!cpu_count || !pids)
return;
for (i = 0; i < cpu_count; i++) {
if (pids[i] > 0) {
kill(pids[i], SIGKILL);
delete_temp_file(i);
pids[i] = 0;
}
}
}
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 void delete_thread_data(void)
{
int i;
if (!cpu_count)
return;
for (i = 0; i < cpu_count; i++) {
if (pids) {
if (pids[i]) {
delete_temp_file(i);
if (pids[i] < 0)
pids[i] = 0;
}
} else
/* Extract does not allocate pids */
delete_temp_file(i);
}
}
static void stop_threads(void)
{
int i;
if (!cpu_count)
return;
for (i = 0; i < cpu_count; i++) {
if (pids[i] > 0) {
kill(pids[i], SIGINT);
waitpid(pids[i], NULL, 0);
pids[i] = -1;
}
}
}
static int create_recorder(int cpu, int extract);
static void flush_threads(void)
{
long ret;
int i;
if (!cpu_count)
return;
for (i = 0; i < cpu_count; i++) {
ret = create_recorder(i, 1);
if (ret < 0)
die("error reading ring buffer");
}
}
static int set_ftrace(int set)
{
struct stat buf;
char *path = "/proc/sys/kernel/ftrace_enabled";
int fd;
char *val = set ? "1" : "0";
/* if ftace_enable does not exist, simply ignore it */
fd = stat(path, &buf);
if (fd < 0)
return -ENODEV;
fd = open(path, O_WRONLY);
if (fd < 0)
die ("Can't %s ftrace", set ? "enable" : "disable");
write(fd, val, 1);
close(fd);
return 0;
}
static void clear_trace(void)
{
FILE *fp;
char *path;
/* reset the trace */
path = tracecmd_get_tracing_file("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 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_event_filters(const char *pid_filter);
static void update_pid_event_filters(const char *pid);
static void enable_tracing(void);
static void
update_sched_event(struct event_list **event, const char *file,
const char *pid_filter, const char *field_filter);
static void update_task_filter(void)
{
int pid = getpid();
char spid[100];
if (!filter_task && filter_pid < 0) {
update_ftrace_pid("", 1);
enable_tracing();
return;
}
if (filter_pid >= 0)
pid = filter_pid;
snprintf(spid, 100, "%d", pid);
update_ftrace_pid(spid, 1);
update_pid_event_filters(spid);
enable_tracing();
}
#ifndef NO_PTRACE
static char *make_pid_filter(const char *field)
{
struct filter_pids *p;
char *filter;
char *orit;
char *str;
int len;
filter = malloc_or_die(len_filter_pids +
(strlen(field) + strlen("(==)||")) * nr_filter_pids);
/* Last '||' that is not used will cover the \0 */
str = filter;
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;
}
return filter;
}
static void add_new_filter_pid(int pid)
{
char *pid_filter;
char *filter;
char buf[100];
add_filter_pid(pid);
sprintf(buf, "%d", pid);
update_ftrace_pid(buf, 0);
pid_filter = make_pid_filter("common_pid");
update_event_filters(pid_filter);
if (!sched_event && !sched_switch_event
&& !sched_wakeup_event && !sched_wakeup_new_event)
return;
/*
* 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.
*/
filter = make_pid_filter("next_pid");
update_sched_event(&sched_switch_event, "sched/sched_switch", pid_filter, filter);
free(filter);
filter = make_pid_filter("pid");
update_sched_event(&sched_wakeup_event, "sched/sched_wakeup",
pid_filter, filter);
update_sched_event(&sched_wakeup_new_event, "sched/sched_wakeup_new",
pid_filter, filter);
free(pid_filter);
free(filter);
}
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_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()
{
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;
}
if (filter && event->filter_file)
write_filter(event->filter_file, filter);
if (filter_only || !event->enable_file)
return;
path = event->enable_file;
fp = fopen(path, "w");
if (!fp)
die("writing to '%s'", path);
ret = fwrite(&update, 1, 1, fp);
fclose(fp);
if (ret < 0)
die("writing to '%s'", path);
}
/*
* The debugfs file tracing_enabled needs to be deprecated.
* But just in case anyone fiddled with it. If it exists,
* make sure it is one.
* No error checking needed here.
*/
static void check_tracing_enabled(void)
{
static int fd = -1;
char *path;
if (fd < 0) {
path = tracecmd_get_tracing_file("tracing_enabled");
fd = open(path, O_WRONLY | O_CLOEXEC);
tracecmd_put_tracing_file(path);
if (fd < 0)
return;
}
write(fd, "1", 1);
}
static int tracing_on_fd = -1;
static int open_tracing_on(void)
{
int fd = tracing_on_fd;
char *path;
if (fd >= 0)
return fd;
path = tracecmd_get_tracing_file("tracing_on");
fd = open(path, O_RDWR | O_CLOEXEC);
if (fd < 0)
die("opening '%s'", path);
tracecmd_put_tracing_file(path);
tracing_on_fd = fd;
return fd;
}
static void write_tracing_on(int on)
{
int ret;
int fd;
fd = open_tracing_on();
if (fd < 0)
return;
if (on)
ret = write(fd, "1", 1);
else
ret = write(fd, "0", 1);
if (ret < 0)
die("writing 'tracing_on'");
}
static int read_tracing_on(void)
{
int fd;
char buf[10];
int ret;
fd = open_tracing_on();
if (fd < 0)
return 0;
ret = read(fd, buf, 10);
if (ret <= 0)
die("Reading 'tracing_on'");
buf[9] = 0;
ret = atoi(buf);
return ret;
}
static void enable_tracing(void)
{
check_tracing_enabled();
write_tracing_on(1);
if (latency)
reset_max_latency();
}
static void disable_tracing(void)
{
write_tracing_on(0);
}
static void disable_all(void)
{
disable_tracing();
set_plugin("nop");
reset_events();
/* Force close and reset of ftrace pid file */
update_ftrace_pid("", 1);
update_ftrace_pid(NULL, 0);
clear_trace();
}
static void
update_sched_event(struct event_list **event, const char *file,
const char *pid_filter, const char *field_filter)
{
char *event_filter;
char *filter;
char *path;
char *p;
if (!*event) {
/* No sched events are being processed, ignore */
if (!sched_event)
return;
*event = malloc_or_die(sizeof(**event));
memset(*event, 0, sizeof(**event));
(*event)->event = file;
p = malloc_or_die(strlen(file) + strlen("events//filter") + 1);
sprintf(p, "events/%s/filter", file);
path = tracecmd_get_tracing_file(p);
free(p);
(*event)->filter_file = strdup(path);
if (sched_event->filter)
(*event)->filter = strdup(sched_event->filter);
tracecmd_put_tracing_file(path);
}
path = (*event)->filter_file;
if (!path)
return;
filter = (*event)->filter;
if (filter) {
event_filter = malloc_or_die(strlen(pid_filter) +
strlen(field_filter) +
strlen(filter) +
strlen("(()||())&&()") + 1);
sprintf(event_filter, "((%s)||(%s))&&(%s)",
pid_filter, field_filter, filter);
} else {
event_filter = malloc_or_die(strlen(pid_filter) +
strlen(field_filter) +
strlen("(()||())") + 1);
sprintf(event_filter, "((%s)||(%s))",
pid_filter, field_filter);
}
write_filter(path, event_filter);
free(event_filter);
}
static void update_event_filters(const char *pid_filter)
{
struct event_list *event;
char *event_filter;
int free_it;
int len;
len = strlen(pid_filter);
for (event = event_selection; event; event = event->next) {
if (!event->neg) {
free_it = 0;
if (event->filter) {
event_filter = malloc_or_die(len +
strlen(event->filter) + strlen("()&&()" + 1));
free_it = 1;
sprintf(event_filter, "(%s)&&(%s)",
pid_filter, event->filter);
} else
event_filter = (char *)pid_filter;
update_event(event, event_filter, 1, '1');
if (free_it)
free(event_filter);
}
}
}
static void update_pid_event_filters(const char *pid)
{
char *pid_filter;
char *filter;
pid_filter = malloc_or_die(strlen(pid) + strlen("common_pid==") + 1);
sprintf(pid_filter, "common_pid==%s", pid);
update_event_filters(pid_filter);
/*
* Also make sure that the sched_switch to this pid