aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorNamhyung Kim <namhyung.kim@lge.com>2013-03-21 03:18:46 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2013-03-21 12:15:42 -0400
commit8755d5e202c3ef62e33d75426c2f0005e3f70ca9 (patch)
treeb8ddc1a13229bd4fc47fa48476c5ed910f307e86 /tools
parent5a6fd27ad73fef0ed39a00236acbc3a17834672a (diff)
perf tools: Get rid of write_or_die() from trace-event-info.c
Check return value of write and fail if error. Signed-off-by: Namhyung Kim <namhyung@kernel.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Steven Rostedt <rostedt@goodmis.org> Link: http://lkml.kernel.org/r/1363850332-25297-4-git-send-email-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/util/trace-event-info.c180
-rw-r--r--tools/perf/util/trace-event.h2
2 files changed, 127 insertions, 55 deletions
diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
index 91db6e8e4493..090e80d743db 100644
--- a/tools/perf/util/trace-event-info.c
+++ b/tools/perf/util/trace-event-info.c
@@ -106,17 +106,6 @@ static void put_tracing_file(char *file)
106 free(file); 106 free(file);
107} 107}
108 108
109static ssize_t write_or_die(const void *buf, size_t len)
110{
111 int ret;
112
113 ret = write(output_fd, buf, len);
114 if (ret < 0)
115 die("writing to '%s'", output_file);
116
117 return ret;
118}
119
120int bigendian(void) 109int bigendian(void)
121{ 110{
122 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0}; 111 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
@@ -127,29 +116,32 @@ int bigendian(void)
127} 116}
128 117
129/* unfortunately, you can not stat debugfs or proc files for size */ 118/* unfortunately, you can not stat debugfs or proc files for size */
130static void record_file(const char *file, size_t hdr_sz) 119static int record_file(const char *file, ssize_t hdr_sz)
131{ 120{
132 unsigned long long size = 0; 121 unsigned long long size = 0;
133 char buf[BUFSIZ], *sizep; 122 char buf[BUFSIZ], *sizep;
134 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); 123 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
135 int r, fd; 124 int r, fd;
125 int err = -EIO;
136 126
137 fd = open(file, O_RDONLY); 127 fd = open(file, O_RDONLY);
138 if (fd < 0) 128 if (fd < 0)
139 die("Can't read '%s'", file); 129 die("Can't read '%s'", file);
140 130
141 /* put in zeros for file size, then fill true size later */ 131 /* put in zeros for file size, then fill true size later */
142 if (hdr_sz) 132 if (hdr_sz) {
143 write_or_die(&size, hdr_sz); 133 if (write(output_fd, &size, hdr_sz) != hdr_sz)
134 goto out;
135 }
144 136
145 do { 137 do {
146 r = read(fd, buf, BUFSIZ); 138 r = read(fd, buf, BUFSIZ);
147 if (r > 0) { 139 if (r > 0) {
148 size += r; 140 size += r;
149 write_or_die(buf, r); 141 if (write(output_fd, buf, r) != r)
142 goto out;
150 } 143 }
151 } while (r > 0); 144 } while (r > 0);
152 close(fd);
153 145
154 /* ugh, handle big-endian hdr_size == 4 */ 146 /* ugh, handle big-endian hdr_size == 4 */
155 sizep = (char*)&size; 147 sizep = (char*)&size;
@@ -158,12 +150,18 @@ static void record_file(const char *file, size_t hdr_sz)
158 150
159 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) 151 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
160 die("writing to %s", output_file); 152 die("writing to %s", output_file);
153
154 err = 0;
155out:
156 close(fd);
157 return err;
161} 158}
162 159
163static void read_header_files(void) 160static int read_header_files(void)
164{ 161{
165 char *path; 162 char *path;
166 struct stat st; 163 struct stat st;
164 int err = -EIO;
167 165
168 path = get_tracing_file("events/header_page"); 166 path = get_tracing_file("events/header_page");
169 if (!path) 167 if (!path)
@@ -172,8 +170,16 @@ static void read_header_files(void)
172 if (stat(path, &st) < 0) 170 if (stat(path, &st) < 0)
173 die("can't read '%s'", path); 171 die("can't read '%s'", path);
174 172
175 write_or_die("header_page", 12); 173 if (write(output_fd, "header_page", 12) != 12) {
176 record_file(path, 8); 174 pr_debug("can't write header_page\n");
175 goto out;
176 }
177
178 if (record_file(path, 8) < 0) {
179 pr_debug("can't record header_page file\n");
180 goto out;
181 }
182
177 put_tracing_file(path); 183 put_tracing_file(path);
178 184
179 path = get_tracing_file("events/header_event"); 185 path = get_tracing_file("events/header_event");
@@ -183,9 +189,20 @@ static void read_header_files(void)
183 if (stat(path, &st) < 0) 189 if (stat(path, &st) < 0)
184 die("can't read '%s'", path); 190 die("can't read '%s'", path);
185 191
186 write_or_die("header_event", 13); 192 if (write(output_fd, "header_event", 13) != 13) {
187 record_file(path, 8); 193 pr_debug("can't write header_event\n");
194 goto out;
195 }
196
197 if (record_file(path, 8) < 0) {
198 pr_debug("can't record header_event file\n");
199 goto out;
200 }
201
202 err = 0;
203out:
188 put_tracing_file(path); 204 put_tracing_file(path);
205 return err;
189} 206}
190 207
191static bool name_in_tp_list(char *sys, struct tracepoint_path *tps) 208static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
@@ -232,7 +249,11 @@ static int copy_event_system(const char *sys, struct tracepoint_path *tps)
232 count++; 249 count++;
233 } 250 }
234 251
235 write_or_die(&count, 4); 252 if (write(output_fd, &count, 4) != 4) {
253 err = -EIO;
254 pr_debug("can't write count\n");
255 goto out;
256 }
236 257
237 rewinddir(dir); 258 rewinddir(dir);
238 while ((dent = readdir(dir))) { 259 while ((dent = readdir(dir))) {
@@ -249,8 +270,13 @@ static int copy_event_system(const char *sys, struct tracepoint_path *tps)
249 sprintf(format, "%s/%s/format", sys, dent->d_name); 270 sprintf(format, "%s/%s/format", sys, dent->d_name);
250 ret = stat(format, &st); 271 ret = stat(format, &st);
251 272
252 if (ret >= 0) 273 if (ret >= 0) {
253 record_file(format, 8); 274 err = record_file(format, 8);
275 if (err) {
276 free(format);
277 goto out;
278 }
279 }
254 free(format); 280 free(format);
255 } 281 }
256 err = 0; 282 err = 0;
@@ -259,17 +285,20 @@ out:
259 return err; 285 return err;
260} 286}
261 287
262static void read_ftrace_files(struct tracepoint_path *tps) 288static int read_ftrace_files(struct tracepoint_path *tps)
263{ 289{
264 char *path; 290 char *path;
291 int ret;
265 292
266 path = get_tracing_file("events/ftrace"); 293 path = get_tracing_file("events/ftrace");
267 if (!path) 294 if (!path)
268 die("can't get tracing/events/ftrace"); 295 die("can't get tracing/events/ftrace");
269 296
270 copy_event_system(path, tps); 297 ret = copy_event_system(path, tps);
271 298
272 put_tracing_file(path); 299 put_tracing_file(path);
300
301 return ret;
273} 302}
274 303
275static bool system_in_tp_list(char *sys, struct tracepoint_path *tps) 304static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
@@ -312,7 +341,11 @@ static int read_event_files(struct tracepoint_path *tps)
312 count++; 341 count++;
313 } 342 }
314 343
315 write_or_die(&count, 4); 344 if (write(output_fd, &count, 4) != 4) {
345 err = -EIO;
346 pr_debug("can't write count\n");
347 goto out;
348 }
316 349
317 rewinddir(dir); 350 rewinddir(dir);
318 while ((dent = readdir(dir))) { 351 while ((dent = readdir(dir))) {
@@ -330,8 +363,14 @@ static int read_event_files(struct tracepoint_path *tps)
330 sprintf(sys, "%s/%s", path, dent->d_name); 363 sprintf(sys, "%s/%s", path, dent->d_name);
331 ret = stat(sys, &st); 364 ret = stat(sys, &st);
332 if (ret >= 0) { 365 if (ret >= 0) {
333 write_or_die(dent->d_name, strlen(dent->d_name) + 1); 366 ssize_t size = strlen(dent->d_name) + 1;
334 copy_event_system(sys, tps); 367
368 if (write(output_fd, dent->d_name, size) != size ||
369 copy_event_system(sys, tps) < 0) {
370 err = -EIO;
371 free(sys);
372 goto out;
373 }
335 } 374 }
336 free(sys); 375 free(sys);
337 } 376 }
@@ -343,29 +382,30 @@ out:
343 return err; 382 return err;
344} 383}
345 384
346static void read_proc_kallsyms(void) 385static int read_proc_kallsyms(void)
347{ 386{
348 unsigned int size; 387 unsigned int size;
349 const char *path = "/proc/kallsyms"; 388 const char *path = "/proc/kallsyms";
350 struct stat st; 389 struct stat st;
351 int ret; 390 int ret, err = 0;
352 391
353 ret = stat(path, &st); 392 ret = stat(path, &st);
354 if (ret < 0) { 393 if (ret < 0) {
355 /* not found */ 394 /* not found */
356 size = 0; 395 size = 0;
357 write_or_die(&size, 4); 396 if (write(output_fd, &size, 4) != 4)
358 return; 397 err = -EIO;
398 return err;
359 } 399 }
360 record_file(path, 4); 400 return record_file(path, 4);
361} 401}
362 402
363static void read_ftrace_printk(void) 403static int read_ftrace_printk(void)
364{ 404{
365 unsigned int size; 405 unsigned int size;
366 char *path; 406 char *path;
367 struct stat st; 407 struct stat st;
368 int ret; 408 int ret, err = 0;
369 409
370 path = get_tracing_file("printk_formats"); 410 path = get_tracing_file("printk_formats");
371 if (!path) 411 if (!path)
@@ -375,13 +415,15 @@ static void read_ftrace_printk(void)
375 if (ret < 0) { 415 if (ret < 0) {
376 /* not found */ 416 /* not found */
377 size = 0; 417 size = 0;
378 write_or_die(&size, 4); 418 if (write(output_fd, &size, 4) != 4)
419 err = -EIO;
379 goto out; 420 goto out;
380 } 421 }
381 record_file(path, 4); 422 err = record_file(path, 4);
382 423
383out: 424out:
384 put_tracing_file(path); 425 put_tracing_file(path);
426 return err;
385} 427}
386 428
387static struct tracepoint_path * 429static struct tracepoint_path *
@@ -428,9 +470,10 @@ bool have_tracepoints(struct list_head *pattrs)
428 return false; 470 return false;
429} 471}
430 472
431static void tracing_data_header(void) 473static int tracing_data_header(void)
432{ 474{
433 char buf[20]; 475 char buf[20];
476 ssize_t size;
434 477
435 /* just guessing this is someone's birthday.. ;) */ 478 /* just guessing this is someone's birthday.. ;) */
436 buf[0] = 23; 479 buf[0] = 23;
@@ -438,9 +481,12 @@ static void tracing_data_header(void)
438 buf[2] = 68; 481 buf[2] = 68;
439 memcpy(buf + 3, "tracing", 7); 482 memcpy(buf + 3, "tracing", 7);
440 483
441 write_or_die(buf, 10); 484 if (write(output_fd, buf, 10) != 10)
485 return -1;
442 486
443 write_or_die(VERSION, strlen(VERSION) + 1); 487 size = strlen(VERSION) + 1;
488 if (write(output_fd, VERSION, size) != size)
489 return -1;
444 490
445 /* save endian */ 491 /* save endian */
446 if (bigendian()) 492 if (bigendian())
@@ -450,14 +496,19 @@ static void tracing_data_header(void)
450 496
451 read_trace_init(buf[0], buf[0]); 497 read_trace_init(buf[0], buf[0]);
452 498
453 write_or_die(buf, 1); 499 if (write(output_fd, buf, 1) != 1)
500 return -1;
454 501
455 /* save size of long */ 502 /* save size of long */
456 buf[0] = sizeof(long); 503 buf[0] = sizeof(long);
457 write_or_die(buf, 1); 504 if (write(output_fd, buf, 1) != 1)
505 return -1;
458 506
459 /* save page_size */ 507 /* save page_size */
460 write_or_die(&page_size, 4); 508 if (write(output_fd, &page_size, 4) != 4)
509 return -1;
510
511 return 0;
461} 512}
462 513
463struct tracing_data *tracing_data_get(struct list_head *pattrs, 514struct tracing_data *tracing_data_get(struct list_head *pattrs,
@@ -465,6 +516,7 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
465{ 516{
466 struct tracepoint_path *tps; 517 struct tracepoint_path *tps;
467 struct tracing_data *tdata; 518 struct tracing_data *tdata;
519 int err;
468 520
469 output_fd = fd; 521 output_fd = fd;
470 522
@@ -498,13 +550,24 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
498 output_fd = temp_fd; 550 output_fd = temp_fd;
499 } 551 }
500 552
501 tracing_data_header(); 553 err = tracing_data_header();
502 read_header_files(); 554 if (err)
503 read_ftrace_files(tps); 555 goto out;
504 read_event_files(tps); 556 err = read_header_files();
505 read_proc_kallsyms(); 557 if (err)
506 read_ftrace_printk(); 558 goto out;
559 err = read_ftrace_files(tps);
560 if (err)
561 goto out;
562 err = read_event_files(tps);
563 if (err)
564 goto out;
565 err = read_proc_kallsyms();
566 if (err)
567 goto out;
568 err = read_ftrace_printk();
507 569
570out:
508 /* 571 /*
509 * All tracing data are stored by now, we can restore 572 * All tracing data are stored by now, we can restore
510 * the default output file in case we used temp file. 573 * the default output file in case we used temp file.
@@ -515,22 +578,31 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
515 output_fd = fd; 578 output_fd = fd;
516 } 579 }
517 580
581 if (err) {
582 free(tdata);
583 tdata = NULL;
584 }
585
518 put_tracepoints_path(tps); 586 put_tracepoints_path(tps);
519 return tdata; 587 return tdata;
520} 588}
521 589
522void tracing_data_put(struct tracing_data *tdata) 590int tracing_data_put(struct tracing_data *tdata)
523{ 591{
592 int err = 0;
593
524 if (tdata->temp) { 594 if (tdata->temp) {
525 record_file(tdata->temp_file, 0); 595 err = record_file(tdata->temp_file, 0);
526 unlink(tdata->temp_file); 596 unlink(tdata->temp_file);
527 } 597 }
528 598
529 free(tdata); 599 free(tdata);
600 return err;
530} 601}
531 602
532int read_tracing_data(int fd, struct list_head *pattrs) 603int read_tracing_data(int fd, struct list_head *pattrs)
533{ 604{
605 int err;
534 struct tracing_data *tdata; 606 struct tracing_data *tdata;
535 607
536 /* 608 /*
@@ -541,6 +613,6 @@ int read_tracing_data(int fd, struct list_head *pattrs)
541 if (!tdata) 613 if (!tdata)
542 return -ENOMEM; 614 return -ENOMEM;
543 615
544 tracing_data_put(tdata); 616 err = tracing_data_put(tdata);
545 return 0; 617 return err;
546} 618}
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 28ccde8ba20f..1978c398ad87 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -68,7 +68,7 @@ struct tracing_data {
68 68
69struct tracing_data *tracing_data_get(struct list_head *pattrs, 69struct tracing_data *tracing_data_get(struct list_head *pattrs,
70 int fd, bool temp); 70 int fd, bool temp);
71void tracing_data_put(struct tracing_data *tdata); 71int tracing_data_put(struct tracing_data *tdata);
72 72
73 73
74struct addr_location; 74struct addr_location;