1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <tracefs.h>
4
read_subbuf(struct tep_handle * tep,struct kbuffer * kbuf)5 static void read_subbuf(struct tep_handle *tep, struct kbuffer *kbuf)
6 {
7 static struct trace_seq seq;
8 struct tep_record record;
9 int missed_events;
10
11 if (seq.buffer)
12 trace_seq_reset(&seq);
13 else
14 trace_seq_init(&seq);
15
16 while ((record.data = kbuffer_read_event(kbuf, &record.ts))) {
17 record.size = kbuffer_event_size(kbuf);
18 missed_events = kbuffer_missed_events(kbuf);
19 if (missed_events) {
20 printf("[MISSED EVENTS");
21 if (missed_events > 0)
22 printf(": %d]\n", missed_events);
23 else
24 printf("]\n");
25 }
26 kbuffer_next_event(kbuf, NULL);
27 tep_print_event(tep, &seq, &record,
28 "%s-%d %6.1000d\t%s: %s\n",
29 TEP_PRINT_COMM,
30 TEP_PRINT_PID,
31 TEP_PRINT_TIME,
32 TEP_PRINT_NAME,
33 TEP_PRINT_INFO);
34 trace_seq_do_printf(&seq);
35 trace_seq_reset(&seq);
36 }
37 }
38
main(int argc,char ** argv)39 int main (int argc, char **argv)
40 {
41 struct tracefs_cpu *tcpu;
42 struct tep_handle *tep;
43 struct kbuffer *kbuf;
44 bool mapped;
45 int cpu;
46
47 if (argc < 2 || !isdigit(argv[1][0])) {
48 printf("usage: %s cpu\n\n", argv[0]);
49 exit(-1);
50 }
51
52 cpu = atoi(argv[1]);
53
54 tep = tracefs_local_events(NULL);
55 if (!tep) {
56 perror("Reading trace event formats");
57 exit(-1);
58 }
59
60 tcpu = tracefs_cpu_open_mapped(NULL, cpu, 0);
61 if (!tcpu) {
62 perror("Open CPU 0 file");
63 exit(-1);
64 }
65
66 /*
67 * If this kernel supports mapping, use normal read,
68 * otherwise use the piped buffer read, although if
69 * the mapping succeeded, tracefs_cpu_buffered_read_buf()
70 * acts the same as tracefs_cpu_read_buf(). But this is just
71 * an example on how to use tracefs_cpu_is_mapped().
72 */
73 mapped = tracefs_cpu_is_mapped(tcpu);
74 if (!mapped)
75 printf("Was not able to map, falling back to buffered read\n");
76 while ((kbuf = mapped ? tracefs_cpu_read_buf(tcpu, true) :
77 tracefs_cpu_buffered_read_buf(tcpu, true))) {
78 read_subbuf(tep, kbuf);
79 }
80
81 kbuf = tracefs_cpu_flush_buf(tcpu);
82 if (kbuf)
83 read_subbuf(tep, kbuf);
84
85 tracefs_cpu_close(tcpu);
86 tep_free(tep);
87
88 return 0;
89 }
90
91