diff options
Diffstat (limited to 'tools/perf/util/trace-event-read.c')
-rw-r--r-- | tools/perf/util/trace-event-read.c | 116 |
1 files changed, 68 insertions, 48 deletions
diff --git a/tools/perf/util/trace-event-read.c b/tools/perf/util/trace-event-read.c index 7cd1193918c7..cb54cd002f49 100644 --- a/tools/perf/util/trace-event-read.c +++ b/tools/perf/util/trace-event-read.c | |||
@@ -50,14 +50,51 @@ static int long_size; | |||
50 | 50 | ||
51 | static unsigned long page_size; | 51 | static unsigned long page_size; |
52 | 52 | ||
53 | static ssize_t calc_data_size; | ||
54 | static bool repipe; | ||
55 | |||
56 | /* If it fails, the next read will report it */ | ||
57 | static void skip(int size) | ||
58 | { | ||
59 | lseek(input_fd, size, SEEK_CUR); | ||
60 | } | ||
61 | |||
62 | static int do_read(int fd, void *buf, int size) | ||
63 | { | ||
64 | int rsize = size; | ||
65 | |||
66 | while (size) { | ||
67 | int ret = read(fd, buf, size); | ||
68 | |||
69 | if (ret <= 0) | ||
70 | return -1; | ||
71 | |||
72 | if (repipe) { | ||
73 | int retw = write(STDOUT_FILENO, buf, ret); | ||
74 | |||
75 | if (retw <= 0 || retw != ret) | ||
76 | die("repiping input file"); | ||
77 | } | ||
78 | |||
79 | size -= ret; | ||
80 | buf += ret; | ||
81 | } | ||
82 | |||
83 | return rsize; | ||
84 | } | ||
85 | |||
53 | static int read_or_die(void *data, int size) | 86 | static int read_or_die(void *data, int size) |
54 | { | 87 | { |
55 | int r; | 88 | int r; |
56 | 89 | ||
57 | r = read(input_fd, data, size); | 90 | r = do_read(input_fd, data, size); |
58 | if (r != size) | 91 | if (r <= 0) |
59 | die("reading input file (size expected=%d received=%d)", | 92 | die("reading input file (size expected=%d received=%d)", |
60 | size, r); | 93 | size, r); |
94 | |||
95 | if (calc_data_size) | ||
96 | calc_data_size += r; | ||
97 | |||
61 | return r; | 98 | return r; |
62 | } | 99 | } |
63 | 100 | ||
@@ -82,57 +119,36 @@ static char *read_string(void) | |||
82 | char buf[BUFSIZ]; | 119 | char buf[BUFSIZ]; |
83 | char *str = NULL; | 120 | char *str = NULL; |
84 | int size = 0; | 121 | int size = 0; |
85 | int i; | ||
86 | off_t r; | 122 | off_t r; |
123 | char c; | ||
87 | 124 | ||
88 | for (;;) { | 125 | for (;;) { |
89 | r = read(input_fd, buf, BUFSIZ); | 126 | r = read(input_fd, &c, 1); |
90 | if (r < 0) | 127 | if (r < 0) |
91 | die("reading input file"); | 128 | die("reading input file"); |
92 | 129 | ||
93 | if (!r) | 130 | if (!r) |
94 | die("no data"); | 131 | die("no data"); |
95 | 132 | ||
96 | for (i = 0; i < r; i++) { | 133 | if (repipe) { |
97 | if (!buf[i]) | 134 | int retw = write(STDOUT_FILENO, &c, 1); |
98 | break; | ||
99 | } | ||
100 | if (i < r) | ||
101 | break; | ||
102 | 135 | ||
103 | if (str) { | 136 | if (retw <= 0 || retw != r) |
104 | size += BUFSIZ; | 137 | die("repiping input file string"); |
105 | str = realloc(str, size); | ||
106 | if (!str) | ||
107 | die("malloc of size %d", size); | ||
108 | memcpy(str + (size - BUFSIZ), buf, BUFSIZ); | ||
109 | } else { | ||
110 | size = BUFSIZ; | ||
111 | str = malloc_or_die(size); | ||
112 | memcpy(str, buf, size); | ||
113 | } | 138 | } |
114 | } | ||
115 | 139 | ||
116 | /* trailing \0: */ | 140 | buf[size++] = c; |
117 | i++; | 141 | |
118 | 142 | if (!c) | |
119 | /* move the file descriptor to the end of the string */ | 143 | break; |
120 | r = lseek(input_fd, -(r - i), SEEK_CUR); | ||
121 | if (r == (off_t)-1) | ||
122 | die("lseek"); | ||
123 | |||
124 | if (str) { | ||
125 | size += i; | ||
126 | str = realloc(str, size); | ||
127 | if (!str) | ||
128 | die("malloc of size %d", size); | ||
129 | memcpy(str + (size - i), buf, i); | ||
130 | } else { | ||
131 | size = i; | ||
132 | str = malloc_or_die(i); | ||
133 | memcpy(str, buf, i); | ||
134 | } | 144 | } |
135 | 145 | ||
146 | if (calc_data_size) | ||
147 | calc_data_size += size; | ||
148 | |||
149 | str = malloc_or_die(size); | ||
150 | memcpy(str, buf, size); | ||
151 | |||
136 | return str; | 152 | return str; |
137 | } | 153 | } |
138 | 154 | ||
@@ -174,7 +190,6 @@ static void read_ftrace_printk(void) | |||
174 | static void read_header_files(void) | 190 | static void read_header_files(void) |
175 | { | 191 | { |
176 | unsigned long long size; | 192 | unsigned long long size; |
177 | char *header_page; | ||
178 | char *header_event; | 193 | char *header_event; |
179 | char buf[BUFSIZ]; | 194 | char buf[BUFSIZ]; |
180 | 195 | ||
@@ -184,10 +199,7 @@ static void read_header_files(void) | |||
184 | die("did not read header page"); | 199 | die("did not read header page"); |
185 | 200 | ||
186 | size = read8(); | 201 | size = read8(); |
187 | header_page = malloc_or_die(size); | 202 | skip(size); |
188 | read_or_die(header_page, size); | ||
189 | parse_header_page(header_page, size); | ||
190 | free(header_page); | ||
191 | 203 | ||
192 | /* | 204 | /* |
193 | * The size field in the page is of type long, | 205 | * The size field in the page is of type long, |
@@ -459,7 +471,7 @@ struct record *trace_read_data(int cpu) | |||
459 | return data; | 471 | return data; |
460 | } | 472 | } |
461 | 473 | ||
462 | void trace_report(int fd) | 474 | ssize_t trace_report(int fd, bool __repipe) |
463 | { | 475 | { |
464 | char buf[BUFSIZ]; | 476 | char buf[BUFSIZ]; |
465 | char test[] = { 23, 8, 68 }; | 477 | char test[] = { 23, 8, 68 }; |
@@ -467,6 +479,10 @@ void trace_report(int fd) | |||
467 | int show_version = 0; | 479 | int show_version = 0; |
468 | int show_funcs = 0; | 480 | int show_funcs = 0; |
469 | int show_printk = 0; | 481 | int show_printk = 0; |
482 | ssize_t size; | ||
483 | |||
484 | calc_data_size = 1; | ||
485 | repipe = __repipe; | ||
470 | 486 | ||
471 | input_fd = fd; | 487 | input_fd = fd; |
472 | 488 | ||
@@ -499,14 +515,18 @@ void trace_report(int fd) | |||
499 | read_proc_kallsyms(); | 515 | read_proc_kallsyms(); |
500 | read_ftrace_printk(); | 516 | read_ftrace_printk(); |
501 | 517 | ||
518 | size = calc_data_size - 1; | ||
519 | calc_data_size = 0; | ||
520 | repipe = false; | ||
521 | |||
502 | if (show_funcs) { | 522 | if (show_funcs) { |
503 | print_funcs(); | 523 | print_funcs(); |
504 | return; | 524 | return size; |
505 | } | 525 | } |
506 | if (show_printk) { | 526 | if (show_printk) { |
507 | print_printk(); | 527 | print_printk(); |
508 | return; | 528 | return size; |
509 | } | 529 | } |
510 | 530 | ||
511 | return; | 531 | return size; |
512 | } | 532 | } |