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