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