]> Git Repo - linux.git/blame - tools/perf/util/trace-event-info.c
perf tools: Get rid of redundant _FILE_OFFSET_BITS definition
[linux.git] / tools / perf / util / trace-event-info.c
CommitLineData
52050943
SR
1/*
2 * Copyright (C) 2008,2009, Steven Rostedt <[email protected]>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
c168fbfb 21#include "util.h"
52050943 22#include <dirent.h>
659d8cfb 23#include <mntent.h>
52050943
SR
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdarg.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/wait.h>
31#include <pthread.h>
32#include <fcntl.h>
33#include <unistd.h>
52050943 34#include <errno.h>
1ef2ed10 35#include <stdbool.h>
69aad6f1 36#include <linux/list.h>
e2561368 37#include <linux/kernel.h>
52050943 38
1ef2ed10 39#include "../perf.h"
52050943 40#include "trace-event.h"
85c66be1 41#include <lk/debugfs.h>
69aad6f1 42#include "evsel.h"
52050943 43
52050943
SR
44#define VERSION "0.5"
45
52050943
SR
46static const char *output_file = "trace.info";
47static int output_fd;
48
52050943 49
aaf045f7 50static void *malloc_or_die(unsigned int size)
52050943
SR
51{
52 void *data;
53
54 data = malloc(size);
55 if (!data)
56 die("malloc");
57 return data;
58}
59
60static const char *find_debugfs(void)
61{
1355915a 62 const char *path = perf_debugfs_mount(NULL);
52050943 63
61be3e59
XG
64 if (!path)
65 die("Your kernel not support debugfs filesystem");
52050943 66
61be3e59 67 return path;
52050943
SR
68}
69
70/*
71 * Finds the path to the debugfs/tracing
72 * Allocates the string and stores it.
73 */
74static const char *find_tracing_dir(void)
75{
76 static char *tracing;
77 static int tracing_found;
78 const char *debugfs;
79
80 if (tracing_found)
81 return tracing;
82
83 debugfs = find_debugfs();
84
85 tracing = malloc_or_die(strlen(debugfs) + 9);
86
87 sprintf(tracing, "%s/tracing", debugfs);
88
89 tracing_found = 1;
90 return tracing;
91}
92
93static char *get_tracing_file(const char *name)
94{
95 const char *tracing;
96 char *file;
97
98 tracing = find_tracing_dir();
99 if (!tracing)
100 return NULL;
101
102 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
103
104 sprintf(file, "%s/%s", tracing, name);
105 return file;
106}
107
108static void put_tracing_file(char *file)
109{
110 free(file);
111}
112
113static ssize_t write_or_die(const void *buf, size_t len)
114{
115 int ret;
116
117 ret = write(output_fd, buf, len);
118 if (ret < 0)
119 die("writing to '%s'", output_file);
120
121 return ret;
122}
123
124int bigendian(void)
125{
126 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
127 unsigned int *ptr;
128
65014ab3 129 ptr = (unsigned int *)(void *)str;
52050943
SR
130 return *ptr == 0x01020304;
131}
132
259032bf
SR
133/* unfortunately, you can not stat debugfs or proc files for size */
134static void record_file(const char *file, size_t hdr_sz)
52050943
SR
135{
136 unsigned long long size = 0;
259032bf
SR
137 char buf[BUFSIZ], *sizep;
138 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
139 int r, fd;
52050943
SR
140
141 fd = open(file, O_RDONLY);
142 if (fd < 0)
143 die("Can't read '%s'", file);
52050943 144
259032bf 145 /* put in zeros for file size, then fill true size later */
29208e57
JO
146 if (hdr_sz)
147 write_or_die(&size, hdr_sz);
52050943
SR
148
149 do {
150 r = read(fd, buf, BUFSIZ);
259032bf 151 if (r > 0) {
52050943 152 size += r;
259032bf
SR
153 write_or_die(buf, r);
154 }
52050943 155 } while (r > 0);
52050943
SR
156 close(fd);
157
259032bf
SR
158 /* ugh, handle big-endian hdr_size == 4 */
159 sizep = (char*)&size;
160 if (bigendian())
161 sizep += sizeof(u64) - hdr_sz;
162
29208e57 163 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
259032bf 164 die("writing to %s", output_file);
52050943
SR
165}
166
167static void read_header_files(void)
168{
52050943 169 char *path;
259032bf 170 struct stat st;
52050943
SR
171
172 path = get_tracing_file("events/header_page");
259032bf 173 if (stat(path, &st) < 0)
52050943
SR
174 die("can't read '%s'", path);
175
52050943 176 write_or_die("header_page", 12);
259032bf 177 record_file(path, 8);
52050943
SR
178 put_tracing_file(path);
179
180 path = get_tracing_file("events/header_event");
259032bf 181 if (stat(path, &st) < 0)
52050943
SR
182 die("can't read '%s'", path);
183
52050943 184 write_or_die("header_event", 13);
259032bf 185 record_file(path, 8);
52050943
SR
186 put_tracing_file(path);
187}
188
1ef2ed10
FW
189static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
190{
191 while (tps) {
192 if (!strcmp(sys, tps->name))
193 return true;
194 tps = tps->next;
195 }
196
197 return false;
198}
199
200static void copy_event_system(const char *sys, struct tracepoint_path *tps)
52050943 201{
52050943
SR
202 struct dirent *dent;
203 struct stat st;
204 char *format;
205 DIR *dir;
206 int count = 0;
207 int ret;
208
209 dir = opendir(sys);
210 if (!dir)
211 die("can't read directory '%s'", sys);
212
213 while ((dent = readdir(dir))) {
659d8cfb
UD
214 if (dent->d_type != DT_DIR ||
215 strcmp(dent->d_name, ".") == 0 ||
1ef2ed10
FW
216 strcmp(dent->d_name, "..") == 0 ||
217 !name_in_tp_list(dent->d_name, tps))
52050943
SR
218 continue;
219 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
220 sprintf(format, "%s/%s/format", sys, dent->d_name);
221 ret = stat(format, &st);
222 free(format);
223 if (ret < 0)
224 continue;
225 count++;
226 }
227
228 write_or_die(&count, 4);
229
230 rewinddir(dir);
231 while ((dent = readdir(dir))) {
659d8cfb
UD
232 if (dent->d_type != DT_DIR ||
233 strcmp(dent->d_name, ".") == 0 ||
1ef2ed10
FW
234 strcmp(dent->d_name, "..") == 0 ||
235 !name_in_tp_list(dent->d_name, tps))
52050943
SR
236 continue;
237 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
238 sprintf(format, "%s/%s/format", sys, dent->d_name);
239 ret = stat(format, &st);
240
259032bf
SR
241 if (ret >= 0)
242 record_file(format, 8);
52050943
SR
243
244 free(format);
245 }
9967411e 246 closedir(dir);
52050943
SR
247}
248
1ef2ed10 249static void read_ftrace_files(struct tracepoint_path *tps)
52050943
SR
250{
251 char *path;
252
253 path = get_tracing_file("events/ftrace");
254
1ef2ed10 255 copy_event_system(path, tps);
52050943
SR
256
257 put_tracing_file(path);
258}
259
1ef2ed10
FW
260static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
261{
262 while (tps) {
263 if (!strcmp(sys, tps->system))
264 return true;
265 tps = tps->next;
266 }
267
268 return false;
269}
270
271static void read_event_files(struct tracepoint_path *tps)
52050943
SR
272{
273 struct dirent *dent;
274 struct stat st;
275 char *path;
276 char *sys;
277 DIR *dir;
278 int count = 0;
279 int ret;
280
281 path = get_tracing_file("events");
282
283 dir = opendir(path);
284 if (!dir)
285 die("can't read directory '%s'", path);
286
287 while ((dent = readdir(dir))) {
659d8cfb
UD
288 if (dent->d_type != DT_DIR ||
289 strcmp(dent->d_name, ".") == 0 ||
52050943 290 strcmp(dent->d_name, "..") == 0 ||
1ef2ed10
FW
291 strcmp(dent->d_name, "ftrace") == 0 ||
292 !system_in_tp_list(dent->d_name, tps))
52050943 293 continue;
659d8cfb 294 count++;
52050943
SR
295 }
296
297 write_or_die(&count, 4);
298
299 rewinddir(dir);
300 while ((dent = readdir(dir))) {
659d8cfb
UD
301 if (dent->d_type != DT_DIR ||
302 strcmp(dent->d_name, ".") == 0 ||
52050943 303 strcmp(dent->d_name, "..") == 0 ||
1ef2ed10
FW
304 strcmp(dent->d_name, "ftrace") == 0 ||
305 !system_in_tp_list(dent->d_name, tps))
52050943
SR
306 continue;
307 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
308 sprintf(sys, "%s/%s", path, dent->d_name);
309 ret = stat(sys, &st);
310 if (ret >= 0) {
659d8cfb
UD
311 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
312 copy_event_system(sys, tps);
52050943
SR
313 }
314 free(sys);
315 }
316
9967411e 317 closedir(dir);
52050943
SR
318 put_tracing_file(path);
319}
320
321static void read_proc_kallsyms(void)
322{
259032bf 323 unsigned int size;
52050943
SR
324 const char *path = "/proc/kallsyms";
325 struct stat st;
326 int ret;
327
328 ret = stat(path, &st);
329 if (ret < 0) {
330 /* not found */
331 size = 0;
332 write_or_die(&size, 4);
333 return;
334 }
259032bf 335 record_file(path, 4);
52050943
SR
336}
337
338static void read_ftrace_printk(void)
339{
259032bf 340 unsigned int size;
6706ccf8 341 char *path;
52050943
SR
342 struct stat st;
343 int ret;
344
345 path = get_tracing_file("printk_formats");
346 ret = stat(path, &st);
347 if (ret < 0) {
348 /* not found */
349 size = 0;
350 write_or_die(&size, 4);
6706ccf8 351 goto out;
52050943 352 }
259032bf
SR
353 record_file(path, 4);
354
6706ccf8
LZ
355out:
356 put_tracing_file(path);
52050943
SR
357}
358
1ef2ed10 359static struct tracepoint_path *
69aad6f1 360get_tracepoints_path(struct list_head *pattrs)
1ef2ed10
FW
361{
362 struct tracepoint_path path, *ppath = &path;
69aad6f1
ACM
363 struct perf_evsel *pos;
364 int nr_tracepoints = 0;
1ef2ed10 365
69aad6f1
ACM
366 list_for_each_entry(pos, pattrs, node) {
367 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
1ef2ed10 368 continue;
e2561368 369 ++nr_tracepoints;
69aad6f1 370 ppath->next = tracepoint_id_to_path(pos->attr.config);
1ef2ed10
FW
371 if (!ppath->next)
372 die("%s\n", "No memory to alloc tracepoints list");
373 ppath = ppath->next;
374 }
375
e2561368 376 return nr_tracepoints > 0 ? path.next : NULL;
1ef2ed10 377}
e2561368 378
29208e57
JO
379static void
380put_tracepoints_path(struct tracepoint_path *tps)
381{
382 while (tps) {
383 struct tracepoint_path *t = tps;
384
385 tps = tps->next;
386 free(t->name);
387 free(t->system);
388 free(t);
389 }
390}
391
69aad6f1 392bool have_tracepoints(struct list_head *pattrs)
63e0c771 393{
69aad6f1 394 struct perf_evsel *pos;
db620b1c 395
69aad6f1
ACM
396 list_for_each_entry(pos, pattrs, node)
397 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
db620b1c
TZ
398 return true;
399
400 return false;
63e0c771
TZ
401}
402
29208e57 403static void tracing_data_header(void)
52050943 404{
29208e57 405 char buf[20];
52050943 406
29208e57 407 /* just guessing this is someone's birthday.. ;) */
52050943
SR
408 buf[0] = 23;
409 buf[1] = 8;
410 buf[2] = 68;
411 memcpy(buf + 3, "tracing", 7);
412
413 write_or_die(buf, 10);
414
415 write_or_die(VERSION, strlen(VERSION) + 1);
416
417 /* save endian */
418 if (bigendian())
419 buf[0] = 1;
420 else
421 buf[0] = 0;
422
aaf045f7
SR
423 read_trace_init(buf[0], buf[0]);
424
52050943
SR
425 write_or_die(buf, 1);
426
427 /* save size of long */
428 buf[0] = sizeof(long);
429 write_or_die(buf, 1);
430
431 /* save page_size */
52050943 432 write_or_die(&page_size, 4);
29208e57
JO
433}
434
435struct tracing_data *tracing_data_get(struct list_head *pattrs,
436 int fd, bool temp)
437{
438 struct tracepoint_path *tps;
439 struct tracing_data *tdata;
440
441 output_fd = fd;
442
443 tps = get_tracepoints_path(pattrs);
444 if (!tps)
445 return NULL;
52050943 446
29208e57
JO
447 tdata = malloc_or_die(sizeof(*tdata));
448 tdata->temp = temp;
449 tdata->size = 0;
450
451 if (temp) {
452 int temp_fd;
453
454 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
455 "/tmp/perf-XXXXXX");
456 if (!mkstemp(tdata->temp_file))
457 die("Can't make temp file");
458
459 temp_fd = open(tdata->temp_file, O_RDWR);
460 if (temp_fd < 0)
461 die("Can't read '%s'", tdata->temp_file);
462
463 /*
464 * Set the temp file the default output, so all the
465 * tracing data are stored into it.
466 */
467 output_fd = temp_fd;
468 }
469
470 tracing_data_header();
52050943 471 read_header_files();
1ef2ed10
FW
472 read_ftrace_files(tps);
473 read_event_files(tps);
52050943
SR
474 read_proc_kallsyms();
475 read_ftrace_printk();
e2561368 476
29208e57
JO
477 /*
478 * All tracing data are stored by now, we can restore
479 * the default output file in case we used temp file.
480 */
481 if (temp) {
482 tdata->size = lseek(output_fd, 0, SEEK_CUR);
483 close(output_fd);
484 output_fd = fd;
485 }
486
487 put_tracepoints_path(tps);
488 return tdata;
52050943 489}
9215545e 490
29208e57 491void tracing_data_put(struct tracing_data *tdata)
9215545e 492{
29208e57
JO
493 if (tdata->temp) {
494 record_file(tdata->temp_file, 0);
495 unlink(tdata->temp_file);
496 }
9215545e 497
29208e57
JO
498 free(tdata);
499}
9215545e 500
29208e57
JO
501int read_tracing_data(int fd, struct list_head *pattrs)
502{
503 struct tracing_data *tdata;
9215545e 504
29208e57
JO
505 /*
506 * We work over the real file, so we can write data
507 * directly, no temp file is needed.
508 */
509 tdata = tracing_data_get(pattrs, fd, false);
510 if (!tdata)
511 return -ENOMEM;
512
513 tracing_data_put(tdata);
514 return 0;
9215545e 515}
This page took 0.288234 seconds and 4 git commands to generate.