]> Git Repo - linux.git/blob - 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
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  */
21 #include "util.h"
22 #include <dirent.h>
23 #include <mntent.h>
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>
34 #include <errno.h>
35 #include <stdbool.h>
36 #include <linux/list.h>
37 #include <linux/kernel.h>
38
39 #include "../perf.h"
40 #include "trace-event.h"
41 #include <lk/debugfs.h>
42 #include "evsel.h"
43
44 #define VERSION "0.5"
45
46 static const char *output_file = "trace.info";
47 static int output_fd;
48
49
50 static void *malloc_or_die(unsigned int size)
51 {
52         void *data;
53
54         data = malloc(size);
55         if (!data)
56                 die("malloc");
57         return data;
58 }
59
60 static const char *find_debugfs(void)
61 {
62         const char *path = perf_debugfs_mount(NULL);
63
64         if (!path)
65                 die("Your kernel not support debugfs filesystem");
66
67         return path;
68 }
69
70 /*
71  * Finds the path to the debugfs/tracing
72  * Allocates the string and stores it.
73  */
74 static 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
93 static 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
108 static void put_tracing_file(char *file)
109 {
110         free(file);
111 }
112
113 static 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
124 int bigendian(void)
125 {
126         unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
127         unsigned int *ptr;
128
129         ptr = (unsigned int *)(void *)str;
130         return *ptr == 0x01020304;
131 }
132
133 /* unfortunately, you can not stat debugfs or proc files for size */
134 static void record_file(const char *file, size_t hdr_sz)
135 {
136         unsigned long long size = 0;
137         char buf[BUFSIZ], *sizep;
138         off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
139         int r, fd;
140
141         fd = open(file, O_RDONLY);
142         if (fd < 0)
143                 die("Can't read '%s'", file);
144
145         /* put in zeros for file size, then fill true size later */
146         if (hdr_sz)
147                 write_or_die(&size, hdr_sz);
148
149         do {
150                 r = read(fd, buf, BUFSIZ);
151                 if (r > 0) {
152                         size += r;
153                         write_or_die(buf, r);
154                 }
155         } while (r > 0);
156         close(fd);
157
158         /* ugh, handle big-endian hdr_size == 4 */
159         sizep = (char*)&size;
160         if (bigendian())
161                 sizep += sizeof(u64) - hdr_sz;
162
163         if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
164                 die("writing to %s", output_file);
165 }
166
167 static void read_header_files(void)
168 {
169         char *path;
170         struct stat st;
171
172         path = get_tracing_file("events/header_page");
173         if (stat(path, &st) < 0)
174                 die("can't read '%s'", path);
175
176         write_or_die("header_page", 12);
177         record_file(path, 8);
178         put_tracing_file(path);
179
180         path = get_tracing_file("events/header_event");
181         if (stat(path, &st) < 0)
182                 die("can't read '%s'", path);
183
184         write_or_die("header_event", 13);
185         record_file(path, 8);
186         put_tracing_file(path);
187 }
188
189 static 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
200 static void copy_event_system(const char *sys, struct tracepoint_path *tps)
201 {
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))) {
214                 if (dent->d_type != DT_DIR ||
215                     strcmp(dent->d_name, ".") == 0 ||
216                     strcmp(dent->d_name, "..") == 0 ||
217                     !name_in_tp_list(dent->d_name, tps))
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))) {
232                 if (dent->d_type != DT_DIR ||
233                     strcmp(dent->d_name, ".") == 0 ||
234                     strcmp(dent->d_name, "..") == 0 ||
235                     !name_in_tp_list(dent->d_name, tps))
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
241                 if (ret >= 0)
242                         record_file(format, 8);
243
244                 free(format);
245         }
246         closedir(dir);
247 }
248
249 static void read_ftrace_files(struct tracepoint_path *tps)
250 {
251         char *path;
252
253         path = get_tracing_file("events/ftrace");
254
255         copy_event_system(path, tps);
256
257         put_tracing_file(path);
258 }
259
260 static 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
271 static void read_event_files(struct tracepoint_path *tps)
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))) {
288                 if (dent->d_type != DT_DIR ||
289                     strcmp(dent->d_name, ".") == 0 ||
290                     strcmp(dent->d_name, "..") == 0 ||
291                     strcmp(dent->d_name, "ftrace") == 0 ||
292                     !system_in_tp_list(dent->d_name, tps))
293                         continue;
294                 count++;
295         }
296
297         write_or_die(&count, 4);
298
299         rewinddir(dir);
300         while ((dent = readdir(dir))) {
301                 if (dent->d_type != DT_DIR ||
302                     strcmp(dent->d_name, ".") == 0 ||
303                     strcmp(dent->d_name, "..") == 0 ||
304                     strcmp(dent->d_name, "ftrace") == 0 ||
305                     !system_in_tp_list(dent->d_name, tps))
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) {
311                         write_or_die(dent->d_name, strlen(dent->d_name) + 1);
312                         copy_event_system(sys, tps);
313                 }
314                 free(sys);
315         }
316
317         closedir(dir);
318         put_tracing_file(path);
319 }
320
321 static void read_proc_kallsyms(void)
322 {
323         unsigned int size;
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         }
335         record_file(path, 4);
336 }
337
338 static void read_ftrace_printk(void)
339 {
340         unsigned int size;
341         char *path;
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);
351                 goto out;
352         }
353         record_file(path, 4);
354
355 out:
356         put_tracing_file(path);
357 }
358
359 static struct tracepoint_path *
360 get_tracepoints_path(struct list_head *pattrs)
361 {
362         struct tracepoint_path path, *ppath = &path;
363         struct perf_evsel *pos;
364         int nr_tracepoints = 0;
365
366         list_for_each_entry(pos, pattrs, node) {
367                 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
368                         continue;
369                 ++nr_tracepoints;
370                 ppath->next = tracepoint_id_to_path(pos->attr.config);
371                 if (!ppath->next)
372                         die("%s\n", "No memory to alloc tracepoints list");
373                 ppath = ppath->next;
374         }
375
376         return nr_tracepoints > 0 ? path.next : NULL;
377 }
378
379 static void
380 put_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
392 bool have_tracepoints(struct list_head *pattrs)
393 {
394         struct perf_evsel *pos;
395
396         list_for_each_entry(pos, pattrs, node)
397                 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
398                         return true;
399
400         return false;
401 }
402
403 static void tracing_data_header(void)
404 {
405         char buf[20];
406
407         /* just guessing this is someone's birthday.. ;) */
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
423         read_trace_init(buf[0], buf[0]);
424
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 */
432         write_or_die(&page_size, 4);
433 }
434
435 struct 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;
446
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();
471         read_header_files();
472         read_ftrace_files(tps);
473         read_event_files(tps);
474         read_proc_kallsyms();
475         read_ftrace_printk();
476
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;
489 }
490
491 void tracing_data_put(struct tracing_data *tdata)
492 {
493         if (tdata->temp) {
494                 record_file(tdata->temp_file, 0);
495                 unlink(tdata->temp_file);
496         }
497
498         free(tdata);
499 }
500
501 int read_tracing_data(int fd, struct list_head *pattrs)
502 {
503         struct tracing_data *tdata;
504
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;
515 }
This page took 0.058044 seconds and 4 git commands to generate.