blob: 535176dc95b6e9b54cf1173401af189c95c1d30b [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"
Xiao Guangrong61be3e52009-12-28 16:48:30 +080041#include "debugfs.h"
Steven Rostedt520509432009-08-17 16:18:05 +020042
Steven Rostedt520509432009-08-17 16:18:05 +020043#define VERSION "0.5"
44
45#define _STR(x) #x
46#define STR(x) _STR(x)
47#define MAX_PATH 256
48
49#define TRACE_CTRL "tracing_on"
50#define TRACE "trace"
51#define AVAILABLE "available_tracers"
52#define CURRENT "current_tracer"
53#define ITER_CTRL "trace_options"
54#define MAX_LATENCY "tracing_max_latency"
55
56unsigned int page_size;
57
58static const char *output_file = "trace.info";
59static int output_fd;
60
61struct event_list {
62 struct event_list *next;
63 const char *event;
64};
65
66struct events {
67 struct events *sibling;
68 struct events *children;
69 struct events *next;
70 char *name;
71};
72
73
74
75static void die(const char *fmt, ...)
76{
77 va_list ap;
78 int ret = errno;
79
80 if (errno)
81 perror("trace-cmd");
82 else
83 ret = -1;
84
85 va_start(ap, fmt);
86 fprintf(stderr, " ");
87 vfprintf(stderr, fmt, ap);
88 va_end(ap);
89
90 fprintf(stderr, "\n");
91 exit(ret);
92}
93
94void *malloc_or_die(unsigned int size)
95{
96 void *data;
97
98 data = malloc(size);
99 if (!data)
100 die("malloc");
101 return data;
102}
103
104static const char *find_debugfs(void)
105{
Xiao Guangrong61be3e52009-12-28 16:48:30 +0800106 const char *path = debugfs_mount(NULL);
Steven Rostedt520509432009-08-17 16:18:05 +0200107
Xiao Guangrong61be3e52009-12-28 16:48:30 +0800108 if (!path)
109 die("Your kernel not support debugfs filesystem");
Steven Rostedt520509432009-08-17 16:18:05 +0200110
Xiao Guangrong61be3e52009-12-28 16:48:30 +0800111 return path;
Steven Rostedt520509432009-08-17 16:18:05 +0200112}
113
114/*
115 * Finds the path to the debugfs/tracing
116 * Allocates the string and stores it.
117 */
118static const char *find_tracing_dir(void)
119{
120 static char *tracing;
121 static int tracing_found;
122 const char *debugfs;
123
124 if (tracing_found)
125 return tracing;
126
127 debugfs = find_debugfs();
128
129 tracing = malloc_or_die(strlen(debugfs) + 9);
130
131 sprintf(tracing, "%s/tracing", debugfs);
132
133 tracing_found = 1;
134 return tracing;
135}
136
137static char *get_tracing_file(const char *name)
138{
139 const char *tracing;
140 char *file;
141
142 tracing = find_tracing_dir();
143 if (!tracing)
144 return NULL;
145
146 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
147
148 sprintf(file, "%s/%s", tracing, name);
149 return file;
150}
151
152static void put_tracing_file(char *file)
153{
154 free(file);
155}
156
157static ssize_t write_or_die(const void *buf, size_t len)
158{
159 int ret;
160
161 ret = write(output_fd, buf, len);
162 if (ret < 0)
163 die("writing to '%s'", output_file);
164
165 return ret;
166}
167
168int bigendian(void)
169{
170 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
171 unsigned int *ptr;
172
Ingo Molnar65014ab2009-09-02 14:55:55 +0200173 ptr = (unsigned int *)(void *)str;
Steven Rostedt520509432009-08-17 16:18:05 +0200174 return *ptr == 0x01020304;
175}
176
177static unsigned long long copy_file_fd(int fd)
178{
179 unsigned long long size = 0;
180 char buf[BUFSIZ];
181 int r;
182
183 do {
184 r = read(fd, buf, BUFSIZ);
185 if (r > 0) {
186 size += r;
187 write_or_die(buf, r);
188 }
189 } while (r > 0);
190
191 return size;
192}
193
194static unsigned long long copy_file(const char *file)
195{
196 unsigned long long size = 0;
197 int fd;
198
199 fd = open(file, O_RDONLY);
200 if (fd < 0)
201 die("Can't read '%s'", file);
202 size = copy_file_fd(fd);
203 close(fd);
204
205 return size;
206}
207
208static unsigned long get_size_fd(int fd)
209{
210 unsigned long long size = 0;
211 char buf[BUFSIZ];
212 int r;
213
214 do {
215 r = read(fd, buf, BUFSIZ);
216 if (r > 0)
217 size += r;
218 } while (r > 0);
219
220 lseek(fd, 0, SEEK_SET);
221
222 return size;
223}
224
225static unsigned long get_size(const char *file)
226{
227 unsigned long long size = 0;
228 int fd;
229
230 fd = open(file, O_RDONLY);
231 if (fd < 0)
232 die("Can't read '%s'", file);
233 size = get_size_fd(fd);
234 close(fd);
235
236 return size;
237}
238
239static void read_header_files(void)
240{
241 unsigned long long size, check_size;
242 char *path;
243 int fd;
244
245 path = get_tracing_file("events/header_page");
246 fd = open(path, O_RDONLY);
247 if (fd < 0)
248 die("can't read '%s'", path);
249
250 /* unfortunately, you can not stat debugfs files for size */
251 size = get_size_fd(fd);
252
253 write_or_die("header_page", 12);
254 write_or_die(&size, 8);
255 check_size = copy_file_fd(fd);
256 if (size != check_size)
257 die("wrong size for '%s' size=%lld read=%lld",
258 path, size, check_size);
259 put_tracing_file(path);
260
261 path = get_tracing_file("events/header_event");
262 fd = open(path, O_RDONLY);
263 if (fd < 0)
264 die("can't read '%s'", path);
265
266 size = get_size_fd(fd);
267
268 write_or_die("header_event", 13);
269 write_or_die(&size, 8);
270 check_size = copy_file_fd(fd);
271 if (size != check_size)
272 die("wrong size for '%s'", path);
273 put_tracing_file(path);
274}
275
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200276static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
277{
278 while (tps) {
279 if (!strcmp(sys, tps->name))
280 return true;
281 tps = tps->next;
282 }
283
284 return false;
285}
286
287static void copy_event_system(const char *sys, struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200288{
289 unsigned long long size, check_size;
290 struct dirent *dent;
291 struct stat st;
292 char *format;
293 DIR *dir;
294 int count = 0;
295 int ret;
296
297 dir = opendir(sys);
298 if (!dir)
299 die("can't read directory '%s'", sys);
300
301 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500302 if (dent->d_type != DT_DIR ||
303 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200304 strcmp(dent->d_name, "..") == 0 ||
305 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200306 continue;
307 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
308 sprintf(format, "%s/%s/format", sys, dent->d_name);
309 ret = stat(format, &st);
310 free(format);
311 if (ret < 0)
312 continue;
313 count++;
314 }
315
316 write_or_die(&count, 4);
317
318 rewinddir(dir);
319 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500320 if (dent->d_type != DT_DIR ||
321 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200322 strcmp(dent->d_name, "..") == 0 ||
323 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200324 continue;
325 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
326 sprintf(format, "%s/%s/format", sys, dent->d_name);
327 ret = stat(format, &st);
328
329 if (ret >= 0) {
330 /* unfortunately, you can not stat debugfs files for size */
331 size = get_size(format);
332 write_or_die(&size, 8);
333 check_size = copy_file(format);
334 if (size != check_size)
335 die("error in size of file '%s'", format);
336 }
337
338 free(format);
339 }
340}
341
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200342static void read_ftrace_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200343{
344 char *path;
345
346 path = get_tracing_file("events/ftrace");
347
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200348 copy_event_system(path, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200349
350 put_tracing_file(path);
351}
352
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200353static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
354{
355 while (tps) {
356 if (!strcmp(sys, tps->system))
357 return true;
358 tps = tps->next;
359 }
360
361 return false;
362}
363
364static void read_event_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200365{
366 struct dirent *dent;
367 struct stat st;
368 char *path;
369 char *sys;
370 DIR *dir;
371 int count = 0;
372 int ret;
373
374 path = get_tracing_file("events");
375
376 dir = opendir(path);
377 if (!dir)
378 die("can't read directory '%s'", path);
379
380 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500381 if (dent->d_type != DT_DIR ||
382 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200383 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200384 strcmp(dent->d_name, "ftrace") == 0 ||
385 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200386 continue;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500387 count++;
Steven Rostedt520509432009-08-17 16:18:05 +0200388 }
389
390 write_or_die(&count, 4);
391
392 rewinddir(dir);
393 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500394 if (dent->d_type != DT_DIR ||
395 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200396 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200397 strcmp(dent->d_name, "ftrace") == 0 ||
398 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200399 continue;
400 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
401 sprintf(sys, "%s/%s", path, dent->d_name);
402 ret = stat(sys, &st);
403 if (ret >= 0) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500404 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
405 copy_event_system(sys, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200406 }
407 free(sys);
408 }
409
410 put_tracing_file(path);
411}
412
413static void read_proc_kallsyms(void)
414{
415 unsigned int size, check_size;
416 const char *path = "/proc/kallsyms";
417 struct stat st;
418 int ret;
419
420 ret = stat(path, &st);
421 if (ret < 0) {
422 /* not found */
423 size = 0;
424 write_or_die(&size, 4);
425 return;
426 }
427 size = get_size(path);
428 write_or_die(&size, 4);
429 check_size = copy_file(path);
430 if (size != check_size)
431 die("error in size of file '%s'", path);
432
433}
434
435static void read_ftrace_printk(void)
436{
437 unsigned int size, check_size;
Li Zefan6706ccf2009-09-17 16:34:23 +0800438 char *path;
Steven Rostedt520509432009-08-17 16:18:05 +0200439 struct stat st;
440 int ret;
441
442 path = get_tracing_file("printk_formats");
443 ret = stat(path, &st);
444 if (ret < 0) {
445 /* not found */
446 size = 0;
447 write_or_die(&size, 4);
Li Zefan6706ccf2009-09-17 16:34:23 +0800448 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +0200449 }
450 size = get_size(path);
451 write_or_die(&size, 4);
452 check_size = copy_file(path);
453 if (size != check_size)
454 die("error in size of file '%s'", path);
Li Zefan6706ccf2009-09-17 16:34:23 +0800455out:
456 put_tracing_file(path);
Steven Rostedt520509432009-08-17 16:18:05 +0200457}
458
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200459static struct tracepoint_path *
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200460get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200461{
462 struct tracepoint_path path, *ppath = &path;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200463 int i, nr_tracepoints = 0;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200464
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200465 for (i = 0; i < nb_events; i++) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200466 if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
467 continue;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200468 ++nr_tracepoints;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200469 ppath->next = tracepoint_id_to_path(pattrs[i].config);
470 if (!ppath->next)
471 die("%s\n", "No memory to alloc tracepoints list");
472 ppath = ppath->next;
473 }
474
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200475 return nr_tracepoints > 0 ? path.next : NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200476}
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200477
478int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
Steven Rostedt520509432009-08-17 16:18:05 +0200479{
480 char buf[BUFSIZ];
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200481 struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events);
482
483 /*
484 * What? No tracepoints? No sense writing anything here, bail out.
485 */
486 if (tps == NULL)
487 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200488
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200489 output_fd = fd;
Steven Rostedt520509432009-08-17 16:18:05 +0200490
491 buf[0] = 23;
492 buf[1] = 8;
493 buf[2] = 68;
494 memcpy(buf + 3, "tracing", 7);
495
496 write_or_die(buf, 10);
497
498 write_or_die(VERSION, strlen(VERSION) + 1);
499
500 /* save endian */
501 if (bigendian())
502 buf[0] = 1;
503 else
504 buf[0] = 0;
505
506 write_or_die(buf, 1);
507
508 /* save size of long */
509 buf[0] = sizeof(long);
510 write_or_die(buf, 1);
511
512 /* save page_size */
513 page_size = getpagesize();
514 write_or_die(&page_size, 4);
515
516 read_header_files();
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200517 read_ftrace_files(tps);
518 read_event_files(tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200519 read_proc_kallsyms();
520 read_ftrace_printk();
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200521
522 return 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200523}