-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrace.c
2567 lines (2060 loc) · 57 KB
/
trace.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
/*
* trace extension module for crash
*
* Copyright (C) 2009, 2010 FUJITSU LIMITED
* Author: Lai Jiangshan <[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; either version 2, or (at your option)
* any later version.
*/
#define _GNU_SOURCE
#include "defs.h"
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdlib.h>
static int verbose = 0;
static int nr_cpu_ids;
/*
* lockless ring_buffer and old non-lockless ring_buffer are both supported.
*/
static int lockless_ring_buffer;
static int per_cpu_buffer_sizes;
/*
* global and encapsulated current_trace are both supported
*/
static int encapsulated_current_trace;
/*
* trace_buffer is supported
*/
static int trace_buffer_available;
/*
* max_buffer is supported
*/
static int max_buffer_available;
/*
* multiple trace instances are supported
*/
static int multiple_instances_available;
/*
* buffer_page has "real_end"
*/
static int buffer_page_real_end_available;
#define koffset(struct, member) struct##_##member##_offset
static int koffset(trace_array, current_trace);
static int koffset(trace_array, trace_buffer);
static int koffset(trace_array, max_buffer);
static int koffset(trace_buffer, buffer);
static int koffset(trace_array, buffer);
static int koffset(tracer, name);
static int koffset(ring_buffer, pages);
static int koffset(ring_buffer, flags);
static int koffset(ring_buffer, cpus);
static int koffset(ring_buffer, buffers);
static int koffset(ring_buffer_per_cpu, cpu);
static int koffset(ring_buffer_per_cpu, pages);
static int koffset(ring_buffer_per_cpu, nr_pages);
static int koffset(ring_buffer_per_cpu, head_page);
static int koffset(ring_buffer_per_cpu, tail_page);
static int koffset(ring_buffer_per_cpu, commit_page);
static int koffset(ring_buffer_per_cpu, reader_page);
static int koffset(ring_buffer_per_cpu, overrun);
static int koffset(ring_buffer_per_cpu, entries);
static int koffset(buffer_page, read);
static int koffset(buffer_page, list);
static int koffset(buffer_page, page);
static int koffset(buffer_page, real_end);
static int koffset(list_head, next);
static int koffset(ftrace_event_call, list);
static int koffset(ftrace_event_field, link);
static int koffset(ftrace_event_field, name);
static int koffset(ftrace_event_field, type);
static int koffset(ftrace_event_field, offset);
static int koffset(ftrace_event_field, size);
static int koffset(ftrace_event_field, is_signed);
static int koffset(trace_array, name);
static int koffset(POINTER_SYM, POINTER) = 0;
struct ring_buffer_per_cpu {
ulong kaddr;
ulong head_page;
ulong tail_page;
ulong commit_page;
ulong reader_page;
ulong real_head_page;
int head_page_index;
unsigned long nr_pages;
ulong *pages;
ulong *linear_pages;
int nr_linear_pages;
ulong overrun;
ulong entries;
};
static ulong global_trace;
static ulong max_tr_trace;
struct trace_instance {
char name[NAME_MAX + 1];
ulong trace_buffer;
ulong max_buffer;
ulong ring_buffer;
unsigned pages;
struct ring_buffer_per_cpu *buffers;
ulong max_tr_ring_buffer;
unsigned max_tr_pages;
struct ring_buffer_per_cpu *max_tr_buffers;
};
static ulong ftrace_trace_arrays;
static struct trace_instance global_trace_instance;
static struct trace_instance *trace_instances = NULL;
static int instance_count;
static ulong ftrace_events;
static ulong current_trace;
static const char *current_tracer_name;
static void ftrace_destroy_event_types(void);
static int ftrace_init_event_types(void);
/* at = ((struct *)ptr)->member */
#define read_value(at, ptr, struct, member) \
do { \
if (!readmem(ptr + koffset(struct, member), KVADDR, \
&at, sizeof(at), #struct "'s " #member, \
RETURN_ON_ERROR)) \
goto out_fail;\
} while (0)
/* Remove the "const" qualifiers for ptr */
#define free(ptr) free((void *)(ptr))
static int write_and_check(int fd, void *data, size_t size)
{
size_t tot = 0;
size_t w;
do {
w = write(fd, data, size - tot);
tot += w;
if (w <= 0)
return -1;
} while (tot != size);
return 0;
}
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static int init_offsets(void)
{
#define init_offset(struct, member) do { \
koffset(struct, member) = MEMBER_OFFSET(#struct, #member);\
if (koffset(struct, member) < 0) { \
fprintf(fp, "failed to init the offset, struct:"\
#struct ", member:" #member); \
fprintf(fp, "\n"); \
return -1; \
} \
} while (0)
if (encapsulated_current_trace)
init_offset(trace_array, current_trace);
if (trace_buffer_available) {
init_offset(trace_array, trace_buffer);
init_offset(trace_buffer, buffer);
if (max_buffer_available)
init_offset(trace_array, max_buffer);
} else {
init_offset(trace_array, buffer);
}
init_offset(tracer, name);
if (MEMBER_EXISTS("ring_buffer_per_cpu", "nr_pages")) {
per_cpu_buffer_sizes = 1;
if (verbose)
fprintf(fp, "per cpu buffer sizes\n");
}
if (machine_type("PPC64") && kernel_symbol_exists(".ring_buffer_read"))
gdb_set_crash_scope(symbol_value(".ring_buffer_read"), ".ring_buffer_read");
else if (kernel_symbol_exists("ring_buffer_read"))
gdb_set_crash_scope(symbol_value("ring_buffer_read"), "ring_buffer_read");
if (!per_cpu_buffer_sizes)
init_offset(ring_buffer, pages);
init_offset(ring_buffer, flags);
init_offset(ring_buffer, cpus);
init_offset(ring_buffer, buffers);
if (MEMBER_SIZE("ring_buffer_per_cpu", "pages") == sizeof(ulong)) {
lockless_ring_buffer = 1;
if (verbose)
fprintf(fp, "lockless\n");
}
if (per_cpu_buffer_sizes)
init_offset(ring_buffer_per_cpu, nr_pages);
init_offset(ring_buffer_per_cpu, cpu);
init_offset(ring_buffer_per_cpu, pages);
init_offset(ring_buffer_per_cpu, head_page);
init_offset(ring_buffer_per_cpu, tail_page);
init_offset(ring_buffer_per_cpu, commit_page);
init_offset(ring_buffer_per_cpu, reader_page);
init_offset(ring_buffer_per_cpu, overrun);
init_offset(ring_buffer_per_cpu, entries);
init_offset(buffer_page, read);
init_offset(buffer_page, list);
init_offset(buffer_page, page);
init_offset(buffer_page, real_end);
init_offset(list_head, next);
koffset(ftrace_event_call, list) = MAX(MEMBER_OFFSET("ftrace_event_call", "list"),
MEMBER_OFFSET("trace_event_call", "list"));
if (koffset(ftrace_event_call, list) < 0) {
fprintf(fp, "failed to init the offset, struct:[f]trace_event_call member:list)\n");
return -1; \
}
init_offset(ftrace_event_field, link);
init_offset(ftrace_event_field, name);
init_offset(ftrace_event_field, type);
init_offset(ftrace_event_field, offset);
init_offset(ftrace_event_field, size);
init_offset(ftrace_event_field, is_signed);
if (MEMBER_EXISTS("trace_array", "name"))
init_offset(trace_array, name);
return 0;
#undef init_offset
}
static void print_offsets(void)
{
if (!verbose)
return;
#define print_offset(struct, member) fprintf(fp, \
"koffset(" #struct ", " #member ") = %d\n", koffset(struct, member))
print_offset(trace_array, buffer);
print_offset(tracer, name);
print_offset(ring_buffer, pages);
print_offset(ring_buffer, flags);
print_offset(ring_buffer, cpus);
print_offset(ring_buffer, buffers);
print_offset(ring_buffer_per_cpu, cpu);
print_offset(ring_buffer_per_cpu, pages);
print_offset(ring_buffer_per_cpu, head_page);
print_offset(ring_buffer_per_cpu, tail_page);
print_offset(ring_buffer_per_cpu, commit_page);
print_offset(ring_buffer_per_cpu, reader_page);
print_offset(ring_buffer_per_cpu, overrun);
print_offset(ring_buffer_per_cpu, entries);
print_offset(buffer_page, read);
print_offset(buffer_page, list);
print_offset(buffer_page, page);
print_offset(buffer_page, real_end);
print_offset(list_head, next);
print_offset(ftrace_event_call, list);
print_offset(ftrace_event_field, link);
print_offset(ftrace_event_field, name);
print_offset(ftrace_event_field, type);
print_offset(ftrace_event_field, offset);
print_offset(ftrace_event_field, size);
print_offset(ftrace_event_field, is_signed);
#undef print_offset
}
static int buffer_page_has_data(ulong page)
{
uint end;
if (!buffer_page_real_end_available)
return 1;
/* Only write pages with data in it */
read_value(end, page, buffer_page, real_end);
return end;
out_fail:
return 0;
}
static int ftrace_init_pages(struct ring_buffer_per_cpu *cpu_buffer,
unsigned nr_pages)
{
unsigned j = 0, count = 0;
ulong head, page;
ulong real_head_page = cpu_buffer->head_page;
cpu_buffer->pages = calloc(sizeof(ulong), nr_pages);
if (cpu_buffer->pages == NULL)
return -1;
cpu_buffer->linear_pages = calloc(sizeof(ulong), nr_pages + 1);
if (cpu_buffer->linear_pages == NULL) {
return -1;
}
if (lockless_ring_buffer) {
read_value(head, cpu_buffer->kaddr, ring_buffer_per_cpu, pages);
cpu_buffer->pages[j++] = head - koffset(buffer_page, list);
} else
head = cpu_buffer->kaddr + koffset(ring_buffer_per_cpu, pages);
page = head;
for (;;) {
read_value(page, page, list_head, next);
if (page & 3) {
/* lockless_ring_buffer */
page &= ~3;
real_head_page = page - koffset(buffer_page, list);
}
if (j == nr_pages)
break;
if (page == head) {
error(INFO, "Num of pages is less than %d\n", nr_pages);
goto out_fail;
}
cpu_buffer->pages[j++] = page - koffset(buffer_page, list);
}
if (page != head) {
error(INFO, "Num of pages is larger than %d\n", nr_pages);
goto out_fail;
}
/* find head page and head_page_index */
cpu_buffer->real_head_page = real_head_page;
cpu_buffer->head_page_index = -1;
for (j = 0; j < nr_pages; j++) {
if (cpu_buffer->pages[j] == real_head_page) {
cpu_buffer->head_page_index = j;
break;
}
}
if (cpu_buffer->head_page_index == -1) {
error(INFO, "error for resolve head_page_index\n");
goto out_fail;
}
/* Setup linear pages */
if (buffer_page_has_data(cpu_buffer->reader_page))
cpu_buffer->linear_pages[count++] = cpu_buffer->reader_page;
if (cpu_buffer->reader_page == cpu_buffer->commit_page)
goto done;
j = cpu_buffer->head_page_index;
for (;;) {
cpu_buffer->linear_pages[count++] = cpu_buffer->pages[j];
if (cpu_buffer->pages[j] == cpu_buffer->commit_page)
break;
j++;
if (j == nr_pages)
j = 0;
if (j == cpu_buffer->head_page_index) {
/* cpu_buffer->commit_page may be corrupted */
break;
}
}
done:
cpu_buffer->nr_linear_pages = count;
return 0;
out_fail:
return -1;
}
static void ftrace_destroy_buffers(struct ring_buffer_per_cpu *buffers)
{
int i;
for (i = 0; i < nr_cpu_ids; i++) {
if (!buffers[i].kaddr)
continue;
free(buffers[i].pages);
free(buffers[i].linear_pages);
}
}
static int ftrace_init_buffers(struct ring_buffer_per_cpu *buffers,
ulong ring_buffer, unsigned pages)
{
int i;
ulong buffers_array;
read_value(buffers_array, ring_buffer, ring_buffer, buffers);
for (i = 0; i < nr_cpu_ids; i++) {
if (!readmem(buffers_array + sizeof(ulong) * i, KVADDR,
&buffers[i].kaddr, sizeof(ulong),
"ring_buffer's cpu buffer", RETURN_ON_ERROR))
goto out_fail;
if (!buffers[i].kaddr)
continue;
#define buffer_read_value(member) read_value(buffers[i].member, \
buffers[i].kaddr, ring_buffer_per_cpu, member)
buffer_read_value(head_page);
buffer_read_value(tail_page);
buffer_read_value(commit_page);
buffer_read_value(reader_page);
buffer_read_value(overrun);
buffer_read_value(entries);
if (per_cpu_buffer_sizes) {
if (MEMBER_SIZE("ring_buffer_per_cpu", "nr_pages") == sizeof(unsigned int)) {
unsigned int tmp_nr_pages;
read_value(tmp_nr_pages, buffers[i].kaddr, ring_buffer_per_cpu, nr_pages);
buffers[i].nr_pages = (unsigned long) tmp_nr_pages;
} else {
buffer_read_value(nr_pages);
}
pages = buffers[i].nr_pages;
} else
buffers[i].nr_pages = pages;
#undef buffer_read_value
if (ftrace_init_pages(buffers + i, pages) < 0)
goto out_fail;
if (verbose) {
fprintf(fp, "overrun=%lu\n", buffers[i].overrun);
fprintf(fp, "entries=%lu\n", buffers[i].entries);
}
}
return 0;
out_fail:
ftrace_destroy_buffers(buffers);
return -1;
}
static int ftrace_init_trace(struct trace_instance *ti, ulong instance_addr)
{
if (trace_buffer_available) {
ti->trace_buffer = instance_addr +
koffset(trace_array, trace_buffer);
read_value(ti->ring_buffer, ti->trace_buffer,
trace_buffer, buffer);
if (max_buffer_available) {
ti->max_buffer = instance_addr +
koffset(trace_array, max_buffer);
read_value(ti->max_tr_ring_buffer, ti->max_buffer,
trace_buffer, buffer);
}
} else {
read_value(ti->ring_buffer, instance_addr, trace_array, buffer);
read_value(ti->pages, ti->ring_buffer, ring_buffer, pages);
read_value(ti->max_tr_ring_buffer, max_tr_trace, trace_array, buffer);
if (ti->max_tr_ring_buffer)
read_value(ti->max_tr_pages, ti->max_tr_ring_buffer, ring_buffer, pages);
}
ti->buffers = calloc(sizeof(*ti->buffers), nr_cpu_ids);
if (ti->buffers == NULL)
goto out_fail;
if (ftrace_init_buffers(ti->buffers, ti->ring_buffer,
ti->pages) < 0)
goto out_fail;
if (!ti->max_tr_ring_buffer)
return 0;
ti->max_tr_buffers = calloc(sizeof(*ti->max_tr_buffers), nr_cpu_ids);
if (ti->max_tr_buffers == NULL)
goto out_fail;
if (ftrace_init_buffers(ti->max_tr_buffers, ti->max_tr_ring_buffer,
ti->max_tr_pages) < 0)
goto out_fail;
return 0;
out_fail:
free(ti->max_tr_buffers);
free(ti->buffers);
return -1;
}
static void ftrace_destroy_all_instance_buffers()
{
int i;
for (i = 0; i < instance_count; i++)
{
struct trace_instance *ti = &trace_instances[i];
if (ti->max_tr_ring_buffer) {
ftrace_destroy_buffers(ti->max_tr_buffers);
free(ti->max_tr_buffers);
}
ftrace_destroy_buffers(ti->buffers);
free(ti->buffers);
}
}
static void ftrace_destroy_instances()
{
ftrace_destroy_all_instance_buffers();
free(trace_instances);
}
static int ftrace_init_instances()
{
int i;
struct trace_instance *ti;
struct list_data list_data;
struct list_data *ld = &list_data;
if (!multiple_instances_available)
return 0;
BZERO(ld, sizeof(struct list_data));
ld->start = ftrace_trace_arrays;
ld->end = global_trace;
ld->flags = LIST_ALLOCATE;
instance_count = do_list(ld);
/* The do_list count includes the list_head, which is not a
* proper instance */
instance_count--;
if (instance_count <= 0)
return 0;
trace_instances = calloc(sizeof(struct trace_instance), instance_count);
/* We start i at 1 to skip over the list_head and continue to the last
* instance, which lies at index instance_count */
for (i = 1; i <= instance_count; i++)
{
ulong instance_ptr;
ulong name_addr;
int ret;
ti = &trace_instances[i-1];
instance_ptr = ld->list_ptr[i];
read_value(name_addr, instance_ptr, trace_array, name);
if (!name_addr)
{
console("Instance name is NULL\n");
}
else if (!read_string(name_addr, ti->name, sizeof(ti->name)))
{
console("Failed to read instance name at address %p\n", (void*)name_addr);
goto out_fail;
}
ret = ftrace_init_trace(ti, instance_ptr);
if (ret < 0)
goto out_fail;
}
FREEBUF(ld->list_ptr);
return 0;
out_fail:
/* We've already freed the current instance's trace buffer info, so
* we'll clear that out to avoid double freeing in
* ftrace_destroy_instances() */
BZERO(ti, sizeof(struct trace_instance));
ftrace_destroy_instances();
return -1;
}
static int ftrace_init_current_tracer(void)
{
ulong addr;
char tmp[128];
/* Get current tracer name */
if (encapsulated_current_trace) {
read_value(addr, global_trace, trace_array, current_trace);
} else {
read_value(addr, current_trace, POINTER_SYM, POINTER);
}
read_value(addr, addr, tracer, name);
read_string(addr, tmp, 128);
current_tracer_name = strdup(tmp);
if (current_tracer_name == NULL)
goto out_fail;
return 0;
out_fail:
return -1;
}
static int ftrace_init(void)
{
struct syment *sym_global_trace;
struct syment *sym_max_tr_trace;
struct syment *sym_ftrace_events;
struct syment *sym_current_trace;
struct syment *sym_ftrace_trace_arrays;
sym_global_trace = symbol_search("global_trace");
sym_ftrace_events = symbol_search("ftrace_events");
sym_ftrace_trace_arrays = symbol_search("ftrace_trace_arrays");
if (sym_global_trace == NULL || sym_ftrace_events == NULL)
return -1;
global_trace = sym_global_trace->value;
ftrace_events = sym_ftrace_events->value;
if (sym_ftrace_trace_arrays)
{
multiple_instances_available = 1;
ftrace_trace_arrays = sym_ftrace_trace_arrays->value;
}
if (MEMBER_EXISTS("buffer_page", "real_end"))
buffer_page_real_end_available = 1;
if (MEMBER_EXISTS("trace_array", "current_trace")) {
encapsulated_current_trace = 1;
} else {
sym_current_trace = symbol_search("current_trace");
if (sym_current_trace == NULL)
return -1;
current_trace = sym_current_trace->value;
}
if (MEMBER_EXISTS("trace_array", "trace_buffer")) {
trace_buffer_available = 1;
if (MEMBER_EXISTS("trace_array", "max_buffer"))
max_buffer_available = 1;
} else {
sym_max_tr_trace = symbol_search("max_tr");
if (sym_max_tr_trace == NULL)
return -1;
max_tr_trace = sym_max_tr_trace->value;
}
if (!try_get_symbol_data("nr_cpu_ids", sizeof(int), &nr_cpu_ids))
nr_cpu_ids = 1;
if (init_offsets() < 0)
return -1;
print_offsets();
if (ftrace_init_trace(&global_trace_instance, global_trace) < 0)
goto out_0;
if (ftrace_init_instances() < 0)
goto out_1;
if (ftrace_init_event_types() < 0)
goto out_2;
if (ftrace_init_current_tracer() < 0)
goto out_3;
return 0;
out_3:
ftrace_destroy_event_types();
out_2:
ftrace_destroy_instances();
out_1:
if (global_trace_instance.max_tr_ring_buffer) {
ftrace_destroy_buffers(global_trace_instance.max_tr_buffers);
free(global_trace_instance.max_tr_buffers);
}
ftrace_destroy_buffers(global_trace_instance.buffers);
free(global_trace_instance.buffers);
out_0:
return -1;
}
static void ftrace_destroy(void)
{
free(current_tracer_name);
ftrace_destroy_event_types();
ftrace_destroy_instances();
if (global_trace_instance.max_tr_ring_buffer) {
ftrace_destroy_buffers(global_trace_instance.max_tr_buffers);
free(global_trace_instance.max_tr_buffers);
}
ftrace_destroy_buffers(global_trace_instance.buffers);
free(global_trace_instance.buffers);
}
static int ftrace_dump_page(int fd, ulong page, void *page_tmp)
{
ulong raw_page;
read_value(raw_page, page, buffer_page, page);
if (!readmem(raw_page, KVADDR, page_tmp, PAGESIZE(), "get page context",
RETURN_ON_ERROR))
goto out_fail;
if (write_and_check(fd, page_tmp, PAGESIZE()))
return -1;
return 0;
out_fail:
return -1;
}
static
void ftrace_dump_buffer(int fd, struct ring_buffer_per_cpu *cpu_buffer,
unsigned pages, void *page_tmp)
{
int i;
for (i = 0; i < cpu_buffer->nr_linear_pages; i++) {
if (ftrace_dump_page(fd, cpu_buffer->linear_pages[i],
page_tmp) < 0)
break;
}
}
static int try_mkdir(const char *pathname, mode_t mode)
{
int ret;
ret = mkdir(pathname, mode);
if (ret < 0) {
if (errno == EEXIST)
return 0;
error(INFO, "mkdir failed\n");
return -1;
}
return 0;
}
static int ftrace_dump_buffers(const char *per_cpu_path,
struct trace_instance *ti)
{
int i;
void *page_tmp;
char path[PATH_MAX];
int fd;
page_tmp = malloc(PAGESIZE());
if (page_tmp == NULL)
return -1;
for (i = 0; i < nr_cpu_ids; i++) {
struct ring_buffer_per_cpu *cpu_buffer = &ti->buffers[i];
if (!cpu_buffer->kaddr)
continue;
snprintf(path, sizeof(path), "%s/cpu%d", per_cpu_path, i);
if (try_mkdir(path, 0755) < 0)
goto out_fail;
snprintf(path, sizeof(path), "%s/cpu%d/trace_pipe_raw",
per_cpu_path, i);
fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd < 0)
goto out_fail;
ftrace_dump_buffer(fd, cpu_buffer, ti->pages, page_tmp);
close(fd);
}
free(page_tmp);
return 0;
out_fail:
free(page_tmp);
return -1;
}
#define MAX_CACHE_ID 256
struct ftrace_field {
const char *name;
const char *type;
int offset;
int size;
int is_signed;
};
struct event_type {
struct event_type *next;
const char *system;
const char *name;
int plugin;
const char *print_fmt;
int id;
int nfields;
struct ftrace_field *fields;
};
static struct event_type *event_type_cache[MAX_CACHE_ID];
static struct event_type **event_types;
static int nr_event_types;
static struct ftrace_field *ftrace_common_fields;
static int ftrace_common_fields_count;
static int syscall_get_enter_fields(ulong call, ulong *fields)
{
static int inited;
static int data_offset;
static int enter_fields_offset;
ulong metadata;
if (inited)
goto work;
inited = 1;
data_offset = MAX(MEMBER_OFFSET("ftrace_event_call", "data"),
MEMBER_OFFSET("trace_event_call", "data"));
if (data_offset < 0) {
/*
* rhel-7.6 moved the .data member into an anonymous union.
*/
if (MEMBER_EXISTS("ftrace_event_call", "rh_data") &&
MEMBER_EXISTS("ftrace_event_data", "data")) {
data_offset = MEMBER_OFFSET("ftrace_event_call", "rh_data") +
MEMBER_OFFSET("ftrace_event_data", "data");
inited = 2;
} else
return -1;
}
enter_fields_offset = MEMBER_OFFSET("syscall_metadata", "enter_fields");
if (enter_fields_offset < 0)
return -1;
work:
if (data_offset < 0 || enter_fields_offset < 0)
return -1;
if (!readmem(call + data_offset, KVADDR, &metadata, sizeof(metadata),
"read ftrace_event_call data", RETURN_ON_ERROR))
return -1;
if (inited == 2) {
if (!readmem(metadata, KVADDR, &metadata, sizeof(metadata),
"read ftrace_event_call data (indirect rh_data)", RETURN_ON_ERROR))
return -1;
}
*fields = metadata + enter_fields_offset;
return 0;
}
static int syscall_get_exit_fields_old(ulong call, ulong *fields)
{
static int inited;
static int data_offset;
static int exit_fields_offset;
ulong metadata;
if (inited)
goto work;
inited = 1;
data_offset = MAX(MEMBER_OFFSET("ftrace_event_call", "data"),
MEMBER_OFFSET("trace_event_call", "data"));
if (data_offset < 0)
return -1;
exit_fields_offset = MEMBER_OFFSET("syscall_metadata", "exit_fields");
if (exit_fields_offset < 0)
return -1;
work:
if (data_offset < 0 || exit_fields_offset < 0)
return -1;
if (!readmem(call + data_offset, KVADDR, &metadata, sizeof(metadata),
"read ftrace_event_call data", RETURN_ON_ERROR))
return -1;
*fields = metadata + exit_fields_offset;
return 0;
}
static int syscall_get_exit_fields(ulong call, ulong *fields)
{
static int inited;
static ulong syscall_exit_fields_value;
if (!inited) {
struct syment *sp;
if (!(sp = symbol_search("syscall_exit_fields"))) {
inited = -1;
} else {
syscall_exit_fields_value = sp->value;
inited = 1;
}
}
if (inited == -1)
return syscall_get_exit_fields_old(call, fields);
*fields = syscall_exit_fields_value;
return 0;
}
static
int ftrace_get_event_type_fields(ulong call, ulong *fields)
{
static int inited;
static int fields_offset;
static int class_offset;
static int get_fields_offset;
static ulong syscall_get_enter_fields_value;
static ulong syscall_get_exit_fields_value;
struct syment *sp;
ulong class, get_fields;
if (inited)
goto work;
inited = 1;
fields_offset = MAX(MEMBER_OFFSET("ftrace_event_call", "fields"),
MEMBER_OFFSET("trace_event_call", "fields"));
class_offset = MAX(MEMBER_OFFSET("ftrace_event_call", "class"),
MEMBER_OFFSET("trace_event_call", "class"));
if (class_offset < 0)
goto work;
inited = 2;
fields_offset = MAX(MEMBER_OFFSET("ftrace_event_class", "fields"),
MEMBER_OFFSET("trace_event_class", "fields"));