blob: dfef238ce1582152a3a64c5bdefa5b0c5409315d [file] [log] [blame]
Steven Rostedt520509432009-08-17 16:18:05 +02001/*
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#define _GNU_SOURCE
22#include <dirent.h>
Ulrich Drepper659d8cf2009-12-19 16:40:28 -050023#include <mntent.h>
Steven Rostedt520509432009-08-17 16:18:05 +020024#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>
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020036#include <stdbool.h>
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -020037#include <linux/kernel.h>
Steven Rostedt520509432009-08-17 16:18:05 +020038
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020039#include "../perf.h"
Steven Rostedt520509432009-08-17 16:18:05 +020040#include "trace-event.h"
41
Steven Rostedt520509432009-08-17 16:18:05 +020042#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;
Steven Rostedt520509432009-08-17 16:18:05 +0200107 FILE *fp;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500108 struct mntent *m;
Steven Rostedt520509432009-08-17 16:18:05 +0200109
110 if (debugfs_found)
111 return debugfs;
112
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500113 fp = setmntent("/proc/mounts", "r");
114 if (!fp)
Steven Rostedt520509432009-08-17 16:18:05 +0200115 die("Can't open /proc/mounts for read");
116
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500117 while ((m = getmntent(fp)) != NULL) {
118 if (strcmp(m->mnt_type, "debugfs") == 0) {
119 strcpy(debugfs, m->mnt_dir);
120 debugfs_found = 1;
Steven Rostedt520509432009-08-17 16:18:05 +0200121 break;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500122 }
Steven Rostedt520509432009-08-17 16:18:05 +0200123 }
Steven Rostedt520509432009-08-17 16:18:05 +0200124
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500125 endmntent(fp);
126
127 if (!debugfs_found)
Steven Rostedt520509432009-08-17 16:18:05 +0200128 die("debugfs not mounted, please mount");
129
Steven Rostedt520509432009-08-17 16:18:05 +0200130 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
Ingo Molnar65014ab2009-09-02 14:55:55 +0200192 ptr = (unsigned int *)(void *)str;
Steven Rostedt520509432009-08-17 16:18:05 +0200193 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
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200295static 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)
Steven Rostedt520509432009-08-17 16:18:05 +0200307{
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))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500321 if (dent->d_type != DT_DIR ||
322 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200323 strcmp(dent->d_name, "..") == 0 ||
324 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200325 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))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500339 if (dent->d_type != DT_DIR ||
340 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200341 strcmp(dent->d_name, "..") == 0 ||
342 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200343 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
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200361static void read_ftrace_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200362{
363 char *path;
364
365 path = get_tracing_file("events/ftrace");
366
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200367 copy_event_system(path, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200368
369 put_tracing_file(path);
370}
371
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200372static 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)
Steven Rostedt520509432009-08-17 16:18:05 +0200384{
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))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500400 if (dent->d_type != DT_DIR ||
401 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200402 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200403 strcmp(dent->d_name, "ftrace") == 0 ||
404 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200405 continue;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500406 count++;
Steven Rostedt520509432009-08-17 16:18:05 +0200407 }
408
409 write_or_die(&count, 4);
410
411 rewinddir(dir);
412 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500413 if (dent->d_type != DT_DIR ||
414 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200415 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200416 strcmp(dent->d_name, "ftrace") == 0 ||
417 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200418 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) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500423 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
424 copy_event_system(sys, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200425 }
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;
Li Zefan6706ccf2009-09-17 16:34:23 +0800457 char *path;
Steven Rostedt520509432009-08-17 16:18:05 +0200458 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);
Li Zefan6706ccf2009-09-17 16:34:23 +0800467 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +0200468 }
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);
Li Zefan6706ccf2009-09-17 16:34:23 +0800474out:
475 put_tracing_file(path);
Steven Rostedt520509432009-08-17 16:18:05 +0200476}
477
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200478static struct tracepoint_path *
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200479get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200480{
481 struct tracepoint_path path, *ppath = &path;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200482 int i, nr_tracepoints = 0;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200483
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200484 for (i = 0; i < nb_events; i++) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200485 if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
486 continue;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200487 ++nr_tracepoints;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200488 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
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200494 return nr_tracepoints > 0 ? path.next : NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200495}
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200496
497int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
Steven Rostedt520509432009-08-17 16:18:05 +0200498{
499 char buf[BUFSIZ];
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200500 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;
Steven Rostedt520509432009-08-17 16:18:05 +0200507
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200508 output_fd = fd;
Steven Rostedt520509432009-08-17 16:18:05 +0200509
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();
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200536 read_ftrace_files(tps);
537 read_event_files(tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200538 read_proc_kallsyms();
539 read_ftrace_printk();
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200540
541 return 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200542}