]> Git Repo - linux.git/blame - tools/perf/util/trace-event-info.c
perf tools: Mount debugfs automatically
[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 */
21#define _GNU_SOURCE
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>
34#include <ctype.h>
35#include <errno.h>
1ef2ed10 36#include <stdbool.h>
e2561368 37#include <linux/kernel.h>
52050943 38
1ef2ed10 39#include "../perf.h"
52050943
SR
40#include "trace-event.h"
41
52050943
SR
42#define VERSION "0.5"
43
44#define _STR(x) #x
45#define STR(x) _STR(x)
46#define MAX_PATH 256
47
48#define TRACE_CTRL "tracing_on"
49#define TRACE "trace"
50#define AVAILABLE "available_tracers"
51#define CURRENT "current_tracer"
52#define ITER_CTRL "trace_options"
53#define MAX_LATENCY "tracing_max_latency"
54
55unsigned int page_size;
56
57static const char *output_file = "trace.info";
58static int output_fd;
59
60struct event_list {
61 struct event_list *next;
62 const char *event;
63};
64
65struct events {
66 struct events *sibling;
67 struct events *children;
68 struct events *next;
69 char *name;
70};
71
72
73
74static void die(const char *fmt, ...)
75{
76 va_list ap;
77 int ret = errno;
78
79 if (errno)
80 perror("trace-cmd");
81 else
82 ret = -1;
83
84 va_start(ap, fmt);
85 fprintf(stderr, " ");
86 vfprintf(stderr, fmt, ap);
87 va_end(ap);
88
89 fprintf(stderr, "\n");
90 exit(ret);
91}
92
93void *malloc_or_die(unsigned int size)
94{
95 void *data;
96
97 data = malloc(size);
98 if (!data)
99 die("malloc");
100 return data;
101}
102
103static const char *find_debugfs(void)
104{
105 static char debugfs[MAX_PATH+1];
106 static int debugfs_found;
52050943 107 FILE *fp;
659d8cfb 108 struct mntent *m;
52050943
SR
109
110 if (debugfs_found)
111 return debugfs;
112
659d8cfb
UD
113 fp = setmntent("/proc/mounts", "r");
114 if (!fp)
52050943
SR
115 die("Can't open /proc/mounts for read");
116
659d8cfb
UD
117 while ((m = getmntent(fp)) != NULL) {
118 if (strcmp(m->mnt_type, "debugfs") == 0) {
119 strcpy(debugfs, m->mnt_dir);
120 debugfs_found = 1;
52050943 121 break;
659d8cfb 122 }
52050943 123 }
52050943 124
659d8cfb 125 endmntent(fp);
52050943 126
659d8cfb
UD
127 if (!debugfs_found)
128 die("debugfs not mounted, please mount");
52050943
SR
129
130 return debugfs;
131}
132
133/*
134 * Finds the path to the debugfs/tracing
135 * Allocates the string and stores it.
136 */
137static const char *find_tracing_dir(void)
138{
139 static char *tracing;
140 static int tracing_found;
141 const char *debugfs;
142
143 if (tracing_found)
144 return tracing;
145
146 debugfs = find_debugfs();
147
148 tracing = malloc_or_die(strlen(debugfs) + 9);
149
150 sprintf(tracing, "%s/tracing", debugfs);
151
152 tracing_found = 1;
153 return tracing;
154}
155
156static char *get_tracing_file(const char *name)
157{
158 const char *tracing;
159 char *file;
160
161 tracing = find_tracing_dir();
162 if (!tracing)
163 return NULL;
164
165 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
166
167 sprintf(file, "%s/%s", tracing, name);
168 return file;
169}
170
171static void put_tracing_file(char *file)
172{
173 free(file);
174}
175
176static ssize_t write_or_die(const void *buf, size_t len)
177{
178 int ret;
179
180 ret = write(output_fd, buf, len);
181 if (ret < 0)
182 die("writing to '%s'", output_file);
183
184 return ret;
185}
186
187int bigendian(void)
188{
189 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
190 unsigned int *ptr;
191
65014ab3 192 ptr = (unsigned int *)(void *)str;
52050943
SR
193 return *ptr == 0x01020304;
194}
195
196static unsigned long long copy_file_fd(int fd)
197{
198 unsigned long long size = 0;
199 char buf[BUFSIZ];
200 int r;
201
202 do {
203 r = read(fd, buf, BUFSIZ);
204 if (r > 0) {
205 size += r;
206 write_or_die(buf, r);
207 }
208 } while (r > 0);
209
210 return size;
211}
212
213static unsigned long long copy_file(const char *file)
214{
215 unsigned long long size = 0;
216 int fd;
217
218 fd = open(file, O_RDONLY);
219 if (fd < 0)
220 die("Can't read '%s'", file);
221 size = copy_file_fd(fd);
222 close(fd);
223
224 return size;
225}
226
227static unsigned long get_size_fd(int fd)
228{
229 unsigned long long size = 0;
230 char buf[BUFSIZ];
231 int r;
232
233 do {
234 r = read(fd, buf, BUFSIZ);
235 if (r > 0)
236 size += r;
237 } while (r > 0);
238
239 lseek(fd, 0, SEEK_SET);
240
241 return size;
242}
243
244static unsigned long get_size(const char *file)
245{
246 unsigned long long size = 0;
247 int fd;
248
249 fd = open(file, O_RDONLY);
250 if (fd < 0)
251 die("Can't read '%s'", file);
252 size = get_size_fd(fd);
253 close(fd);
254
255 return size;
256}
257
258static void read_header_files(void)
259{
260 unsigned long long size, check_size;
261 char *path;
262 int fd;
263
264 path = get_tracing_file("events/header_page");
265 fd = open(path, O_RDONLY);
266 if (fd < 0)
267 die("can't read '%s'", path);
268
269 /* unfortunately, you can not stat debugfs files for size */
270 size = get_size_fd(fd);
271
272 write_or_die("header_page", 12);
273 write_or_die(&size, 8);
274 check_size = copy_file_fd(fd);
275 if (size != check_size)
276 die("wrong size for '%s' size=%lld read=%lld",
277 path, size, check_size);
278 put_tracing_file(path);
279
280 path = get_tracing_file("events/header_event");
281 fd = open(path, O_RDONLY);
282 if (fd < 0)
283 die("can't read '%s'", path);
284
285 size = get_size_fd(fd);
286
287 write_or_die("header_event", 13);
288 write_or_die(&size, 8);
289 check_size = copy_file_fd(fd);
290 if (size != check_size)
291 die("wrong size for '%s'", path);
292 put_tracing_file(path);
293}
294
1ef2ed10
FW
295static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
296{
297 while (tps) {
298 if (!strcmp(sys, tps->name))
299 return true;
300 tps = tps->next;
301 }
302
303 return false;
304}
305
306static void copy_event_system(const char *sys, struct tracepoint_path *tps)
52050943
SR
307{
308 unsigned long long size, check_size;
309 struct dirent *dent;
310 struct stat st;
311 char *format;
312 DIR *dir;
313 int count = 0;
314 int ret;
315
316 dir = opendir(sys);
317 if (!dir)
318 die("can't read directory '%s'", sys);
319
320 while ((dent = readdir(dir))) {
659d8cfb
UD
321 if (dent->d_type != DT_DIR ||
322 strcmp(dent->d_name, ".") == 0 ||
1ef2ed10
FW
323 strcmp(dent->d_name, "..") == 0 ||
324 !name_in_tp_list(dent->d_name, tps))
52050943
SR
325 continue;
326 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
327 sprintf(format, "%s/%s/format", sys, dent->d_name);
328 ret = stat(format, &st);
329 free(format);
330 if (ret < 0)
331 continue;
332 count++;
333 }
334
335 write_or_die(&count, 4);
336
337 rewinddir(dir);
338 while ((dent = readdir(dir))) {
659d8cfb
UD
339 if (dent->d_type != DT_DIR ||
340 strcmp(dent->d_name, ".") == 0 ||
1ef2ed10
FW
341 strcmp(dent->d_name, "..") == 0 ||
342 !name_in_tp_list(dent->d_name, tps))
52050943
SR
343 continue;
344 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
345 sprintf(format, "%s/%s/format", sys, dent->d_name);
346 ret = stat(format, &st);
347
348 if (ret >= 0) {
349 /* unfortunately, you can not stat debugfs files for size */
350 size = get_size(format);
351 write_or_die(&size, 8);
352 check_size = copy_file(format);
353 if (size != check_size)
354 die("error in size of file '%s'", format);
355 }
356
357 free(format);
358 }
359}
360
1ef2ed10 361static void read_ftrace_files(struct tracepoint_path *tps)
52050943
SR
362{
363 char *path;
364
365 path = get_tracing_file("events/ftrace");
366
1ef2ed10 367 copy_event_system(path, tps);
52050943
SR
368
369 put_tracing_file(path);
370}
371
1ef2ed10
FW
372static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
373{
374 while (tps) {
375 if (!strcmp(sys, tps->system))
376 return true;
377 tps = tps->next;
378 }
379
380 return false;
381}
382
383static void read_event_files(struct tracepoint_path *tps)
52050943
SR
384{
385 struct dirent *dent;
386 struct stat st;
387 char *path;
388 char *sys;
389 DIR *dir;
390 int count = 0;
391 int ret;
392
393 path = get_tracing_file("events");
394
395 dir = opendir(path);
396 if (!dir)
397 die("can't read directory '%s'", path);
398
399 while ((dent = readdir(dir))) {
659d8cfb
UD
400 if (dent->d_type != DT_DIR ||
401 strcmp(dent->d_name, ".") == 0 ||
52050943 402 strcmp(dent->d_name, "..") == 0 ||
1ef2ed10
FW
403 strcmp(dent->d_name, "ftrace") == 0 ||
404 !system_in_tp_list(dent->d_name, tps))
52050943 405 continue;
659d8cfb 406 count++;
52050943
SR
407 }
408
409 write_or_die(&count, 4);
410
411 rewinddir(dir);
412 while ((dent = readdir(dir))) {
659d8cfb
UD
413 if (dent->d_type != DT_DIR ||
414 strcmp(dent->d_name, ".") == 0 ||
52050943 415 strcmp(dent->d_name, "..") == 0 ||
1ef2ed10
FW
416 strcmp(dent->d_name, "ftrace") == 0 ||
417 !system_in_tp_list(dent->d_name, tps))
52050943
SR
418 continue;
419 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
420 sprintf(sys, "%s/%s", path, dent->d_name);
421 ret = stat(sys, &st);
422 if (ret >= 0) {
659d8cfb
UD
423 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
424 copy_event_system(sys, tps);
52050943
SR
425 }
426 free(sys);
427 }
428
429 put_tracing_file(path);
430}
431
432static void read_proc_kallsyms(void)
433{
434 unsigned int size, check_size;
435 const char *path = "/proc/kallsyms";
436 struct stat st;
437 int ret;
438
439 ret = stat(path, &st);
440 if (ret < 0) {
441 /* not found */
442 size = 0;
443 write_or_die(&size, 4);
444 return;
445 }
446 size = get_size(path);
447 write_or_die(&size, 4);
448 check_size = copy_file(path);
449 if (size != check_size)
450 die("error in size of file '%s'", path);
451
452}
453
454static void read_ftrace_printk(void)
455{
456 unsigned int size, check_size;
6706ccf8 457 char *path;
52050943
SR
458 struct stat st;
459 int ret;
460
461 path = get_tracing_file("printk_formats");
462 ret = stat(path, &st);
463 if (ret < 0) {
464 /* not found */
465 size = 0;
466 write_or_die(&size, 4);
6706ccf8 467 goto out;
52050943
SR
468 }
469 size = get_size(path);
470 write_or_die(&size, 4);
471 check_size = copy_file(path);
472 if (size != check_size)
473 die("error in size of file '%s'", path);
6706ccf8
LZ
474out:
475 put_tracing_file(path);
52050943
SR
476}
477
1ef2ed10 478static struct tracepoint_path *
cdd6c482 479get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
1ef2ed10
FW
480{
481 struct tracepoint_path path, *ppath = &path;
e2561368 482 int i, nr_tracepoints = 0;
1ef2ed10 483
cdd6c482 484 for (i = 0; i < nb_events; i++) {
1ef2ed10
FW
485 if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
486 continue;
e2561368 487 ++nr_tracepoints;
1ef2ed10
FW
488 ppath->next = tracepoint_id_to_path(pattrs[i].config);
489 if (!ppath->next)
490 die("%s\n", "No memory to alloc tracepoints list");
491 ppath = ppath->next;
492 }
493
e2561368 494 return nr_tracepoints > 0 ? path.next : NULL;
1ef2ed10 495}
e2561368
ACM
496
497int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
52050943
SR
498{
499 char buf[BUFSIZ];
e2561368
ACM
500 struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events);
501
502 /*
503 * What? No tracepoints? No sense writing anything here, bail out.
504 */
505 if (tps == NULL)
506 return -1;
52050943 507
03456a15 508 output_fd = fd;
52050943
SR
509
510 buf[0] = 23;
511 buf[1] = 8;
512 buf[2] = 68;
513 memcpy(buf + 3, "tracing", 7);
514
515 write_or_die(buf, 10);
516
517 write_or_die(VERSION, strlen(VERSION) + 1);
518
519 /* save endian */
520 if (bigendian())
521 buf[0] = 1;
522 else
523 buf[0] = 0;
524
525 write_or_die(buf, 1);
526
527 /* save size of long */
528 buf[0] = sizeof(long);
529 write_or_die(buf, 1);
530
531 /* save page_size */
532 page_size = getpagesize();
533 write_or_die(&page_size, 4);
534
535 read_header_files();
1ef2ed10
FW
536 read_ftrace_files(tps);
537 read_event_files(tps);
52050943
SR
538 read_proc_kallsyms();
539 read_ftrace_printk();
e2561368
ACM
540
541 return 0;
52050943 542}
This page took 0.163677 seconds and 4 git commands to generate.