blob: 3917eb9a8479f7a4b11e6bd1f6e39f146f159523 [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 */
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020021#include "util.h"
Steven Rostedt520509432009-08-17 16:18:05 +020022#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>
Steven Rostedt520509432009-08-17 16:18:05 +020034#include <errno.h>
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020035#include <stdbool.h>
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020036#include <linux/list.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"
Borislav Petkov85c66be2013-02-20 16:32:30 +010041#include <lk/debugfs.h>
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020042#include "evsel.h"
Steven Rostedt520509432009-08-17 16:18:05 +020043
Steven Rostedt520509432009-08-17 16:18:05 +020044#define VERSION "0.5"
45
Steven Rostedt520509432009-08-17 16:18:05 +020046static int output_fd;
47
Steven Rostedt520509432009-08-17 16:18:05 +020048
Steven Rostedt520509432009-08-17 16:18:05 +020049static const char *find_debugfs(void)
50{
Borislav Petkov13559152013-02-20 16:32:31 +010051 const char *path = perf_debugfs_mount(NULL);
Steven Rostedt520509432009-08-17 16:18:05 +020052
Xiao Guangrong61be3e52009-12-28 16:48:30 +080053 if (!path)
Namhyung Kim454f8c72013-03-21 16:18:44 +090054 pr_debug("Your kernel does not support the debugfs filesystem");
Steven Rostedt520509432009-08-17 16:18:05 +020055
Xiao Guangrong61be3e52009-12-28 16:48:30 +080056 return path;
Steven Rostedt520509432009-08-17 16:18:05 +020057}
58
59/*
60 * Finds the path to the debugfs/tracing
61 * Allocates the string and stores it.
62 */
63static const char *find_tracing_dir(void)
64{
65 static char *tracing;
66 static int tracing_found;
67 const char *debugfs;
68
69 if (tracing_found)
70 return tracing;
71
72 debugfs = find_debugfs();
Namhyung Kim454f8c72013-03-21 16:18:44 +090073 if (!debugfs)
74 return NULL;
Steven Rostedt520509432009-08-17 16:18:05 +020075
Namhyung Kim454f8c72013-03-21 16:18:44 +090076 tracing = malloc(strlen(debugfs) + 9);
77 if (!tracing)
78 return NULL;
Steven Rostedt520509432009-08-17 16:18:05 +020079
80 sprintf(tracing, "%s/tracing", debugfs);
81
82 tracing_found = 1;
83 return tracing;
84}
85
86static char *get_tracing_file(const char *name)
87{
88 const char *tracing;
89 char *file;
90
91 tracing = find_tracing_dir();
92 if (!tracing)
93 return NULL;
94
Namhyung Kim454f8c72013-03-21 16:18:44 +090095 file = malloc(strlen(tracing) + strlen(name) + 2);
96 if (!file)
97 return NULL;
Steven Rostedt520509432009-08-17 16:18:05 +020098
99 sprintf(file, "%s/%s", tracing, name);
100 return file;
101}
102
103static void put_tracing_file(char *file)
104{
105 free(file);
106}
107
Steven Rostedt520509432009-08-17 16:18:05 +0200108int bigendian(void)
109{
110 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
111 unsigned int *ptr;
112
Ingo Molnar65014ab2009-09-02 14:55:55 +0200113 ptr = (unsigned int *)(void *)str;
Steven Rostedt520509432009-08-17 16:18:05 +0200114 return *ptr == 0x01020304;
115}
116
Sonny Rao259032b2011-07-14 13:34:43 +1000117/* unfortunately, you can not stat debugfs or proc files for size */
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900118static int record_file(const char *file, ssize_t hdr_sz)
Steven Rostedt520509432009-08-17 16:18:05 +0200119{
120 unsigned long long size = 0;
Sonny Rao259032b2011-07-14 13:34:43 +1000121 char buf[BUFSIZ], *sizep;
122 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
123 int r, fd;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900124 int err = -EIO;
Sonny Rao259032b2011-07-14 13:34:43 +1000125
126 fd = open(file, O_RDONLY);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900127 if (fd < 0) {
128 pr_debug("Can't read '%s'", file);
129 return -errno;
130 }
Sonny Rao259032b2011-07-14 13:34:43 +1000131
132 /* put in zeros for file size, then fill true size later */
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900133 if (hdr_sz) {
134 if (write(output_fd, &size, hdr_sz) != hdr_sz)
135 goto out;
136 }
Steven Rostedt520509432009-08-17 16:18:05 +0200137
138 do {
139 r = read(fd, buf, BUFSIZ);
140 if (r > 0) {
141 size += r;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900142 if (write(output_fd, buf, r) != r)
143 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +0200144 }
145 } while (r > 0);
Steven Rostedt520509432009-08-17 16:18:05 +0200146
Sonny Rao259032b2011-07-14 13:34:43 +1000147 /* ugh, handle big-endian hdr_size == 4 */
148 sizep = (char*)&size;
149 if (bigendian())
150 sizep += sizeof(u64) - hdr_sz;
Steven Rostedt520509432009-08-17 16:18:05 +0200151
Namhyung Kim7f42b952013-03-21 16:18:47 +0900152 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
153 pr_debug("writing file size failed\n");
154 goto out;
155 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900156
157 err = 0;
158out:
159 close(fd);
160 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200161}
162
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900163static int read_header_files(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200164{
Steven Rostedt520509432009-08-17 16:18:05 +0200165 char *path;
Sonny Rao259032b2011-07-14 13:34:43 +1000166 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900167 int err = -EIO;
Steven Rostedt520509432009-08-17 16:18:05 +0200168
169 path = get_tracing_file("events/header_page");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900170 if (!path) {
171 pr_debug("can't get tracing/events/header_page");
172 return -ENOMEM;
173 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900174
Namhyung Kim7f42b952013-03-21 16:18:47 +0900175 if (stat(path, &st) < 0) {
176 pr_debug("can't read '%s'", path);
177 goto out;
178 }
Steven Rostedt520509432009-08-17 16:18:05 +0200179
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900180 if (write(output_fd, "header_page", 12) != 12) {
181 pr_debug("can't write header_page\n");
182 goto out;
183 }
184
185 if (record_file(path, 8) < 0) {
186 pr_debug("can't record header_page file\n");
187 goto out;
188 }
189
Steven Rostedt520509432009-08-17 16:18:05 +0200190 put_tracing_file(path);
191
192 path = get_tracing_file("events/header_event");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900193 if (!path) {
194 pr_debug("can't get tracing/events/header_event");
195 err = -ENOMEM;
196 goto out;
197 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900198
Namhyung Kim7f42b952013-03-21 16:18:47 +0900199 if (stat(path, &st) < 0) {
200 pr_debug("can't read '%s'", path);
201 goto out;
202 }
Steven Rostedt520509432009-08-17 16:18:05 +0200203
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900204 if (write(output_fd, "header_event", 13) != 13) {
205 pr_debug("can't write header_event\n");
206 goto out;
207 }
208
209 if (record_file(path, 8) < 0) {
210 pr_debug("can't record header_event file\n");
211 goto out;
212 }
213
214 err = 0;
215out:
Steven Rostedt520509432009-08-17 16:18:05 +0200216 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900217 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200218}
219
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200220static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
221{
222 while (tps) {
223 if (!strcmp(sys, tps->name))
224 return true;
225 tps = tps->next;
226 }
227
228 return false;
229}
230
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900231static int copy_event_system(const char *sys, struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200232{
Steven Rostedt520509432009-08-17 16:18:05 +0200233 struct dirent *dent;
234 struct stat st;
235 char *format;
236 DIR *dir;
237 int count = 0;
238 int ret;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900239 int err;
Steven Rostedt520509432009-08-17 16:18:05 +0200240
241 dir = opendir(sys);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900242 if (!dir) {
243 pr_debug("can't read directory '%s'", sys);
244 return -errno;
245 }
Steven Rostedt520509432009-08-17 16:18:05 +0200246
247 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500248 if (dent->d_type != DT_DIR ||
249 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200250 strcmp(dent->d_name, "..") == 0 ||
251 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200252 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900253 format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
254 if (!format) {
255 err = -ENOMEM;
256 goto out;
257 }
Steven Rostedt520509432009-08-17 16:18:05 +0200258 sprintf(format, "%s/%s/format", sys, dent->d_name);
259 ret = stat(format, &st);
260 free(format);
261 if (ret < 0)
262 continue;
263 count++;
264 }
265
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900266 if (write(output_fd, &count, 4) != 4) {
267 err = -EIO;
268 pr_debug("can't write count\n");
269 goto out;
270 }
Steven Rostedt520509432009-08-17 16:18:05 +0200271
272 rewinddir(dir);
273 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500274 if (dent->d_type != DT_DIR ||
275 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200276 strcmp(dent->d_name, "..") == 0 ||
277 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200278 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900279 format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
280 if (!format) {
281 err = -ENOMEM;
282 goto out;
283 }
Steven Rostedt520509432009-08-17 16:18:05 +0200284 sprintf(format, "%s/%s/format", sys, dent->d_name);
285 ret = stat(format, &st);
286
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900287 if (ret >= 0) {
288 err = record_file(format, 8);
289 if (err) {
290 free(format);
291 goto out;
292 }
293 }
Steven Rostedt520509432009-08-17 16:18:05 +0200294 free(format);
295 }
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900296 err = 0;
297out:
Xiao Guangrong99674112009-12-28 16:49:38 +0800298 closedir(dir);
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900299 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200300}
301
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900302static int read_ftrace_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200303{
304 char *path;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900305 int ret;
Steven Rostedt520509432009-08-17 16:18:05 +0200306
307 path = get_tracing_file("events/ftrace");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900308 if (!path) {
309 pr_debug("can't get tracing/events/ftrace");
310 return -ENOMEM;
311 }
Steven Rostedt520509432009-08-17 16:18:05 +0200312
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900313 ret = copy_event_system(path, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200314
315 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900316
317 return ret;
Steven Rostedt520509432009-08-17 16:18:05 +0200318}
319
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200320static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
321{
322 while (tps) {
323 if (!strcmp(sys, tps->system))
324 return true;
325 tps = tps->next;
326 }
327
328 return false;
329}
330
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900331static int read_event_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200332{
333 struct dirent *dent;
334 struct stat st;
335 char *path;
336 char *sys;
337 DIR *dir;
338 int count = 0;
339 int ret;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900340 int err;
Steven Rostedt520509432009-08-17 16:18:05 +0200341
342 path = get_tracing_file("events");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900343 if (!path) {
344 pr_debug("can't get tracing/events");
345 return -ENOMEM;
346 }
Steven Rostedt520509432009-08-17 16:18:05 +0200347
348 dir = opendir(path);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900349 if (!dir) {
350 err = -errno;
351 pr_debug("can't read directory '%s'", path);
352 goto out;
353 }
Steven Rostedt520509432009-08-17 16:18:05 +0200354
355 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500356 if (dent->d_type != DT_DIR ||
357 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200358 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200359 strcmp(dent->d_name, "ftrace") == 0 ||
360 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200361 continue;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500362 count++;
Steven Rostedt520509432009-08-17 16:18:05 +0200363 }
364
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900365 if (write(output_fd, &count, 4) != 4) {
366 err = -EIO;
367 pr_debug("can't write count\n");
368 goto out;
369 }
Steven Rostedt520509432009-08-17 16:18:05 +0200370
371 rewinddir(dir);
372 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500373 if (dent->d_type != DT_DIR ||
374 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200375 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200376 strcmp(dent->d_name, "ftrace") == 0 ||
377 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200378 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900379 sys = malloc(strlen(path) + strlen(dent->d_name) + 2);
380 if (!sys) {
381 err = -ENOMEM;
382 goto out;
383 }
Steven Rostedt520509432009-08-17 16:18:05 +0200384 sprintf(sys, "%s/%s", path, dent->d_name);
385 ret = stat(sys, &st);
386 if (ret >= 0) {
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900387 ssize_t size = strlen(dent->d_name) + 1;
388
389 if (write(output_fd, dent->d_name, size) != size ||
390 copy_event_system(sys, tps) < 0) {
391 err = -EIO;
392 free(sys);
393 goto out;
394 }
Steven Rostedt520509432009-08-17 16:18:05 +0200395 }
396 free(sys);
397 }
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900398 err = 0;
399out:
Xiao Guangrong99674112009-12-28 16:49:38 +0800400 closedir(dir);
Steven Rostedt520509432009-08-17 16:18:05 +0200401 put_tracing_file(path);
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900402
403 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200404}
405
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900406static int read_proc_kallsyms(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200407{
Sonny Rao259032b2011-07-14 13:34:43 +1000408 unsigned int size;
Steven Rostedt520509432009-08-17 16:18:05 +0200409 const char *path = "/proc/kallsyms";
410 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900411 int ret, err = 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200412
413 ret = stat(path, &st);
414 if (ret < 0) {
415 /* not found */
416 size = 0;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900417 if (write(output_fd, &size, 4) != 4)
418 err = -EIO;
419 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200420 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900421 return record_file(path, 4);
Steven Rostedt520509432009-08-17 16:18:05 +0200422}
423
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900424static int read_ftrace_printk(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200425{
Sonny Rao259032b2011-07-14 13:34:43 +1000426 unsigned int size;
Li Zefan6706ccf2009-09-17 16:34:23 +0800427 char *path;
Steven Rostedt520509432009-08-17 16:18:05 +0200428 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900429 int ret, err = 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200430
431 path = get_tracing_file("printk_formats");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900432 if (!path) {
433 pr_debug("can't get tracing/printk_formats");
434 return -ENOMEM;
435 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900436
Steven Rostedt520509432009-08-17 16:18:05 +0200437 ret = stat(path, &st);
438 if (ret < 0) {
439 /* not found */
440 size = 0;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900441 if (write(output_fd, &size, 4) != 4)
442 err = -EIO;
Li Zefan6706ccf2009-09-17 16:34:23 +0800443 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +0200444 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900445 err = record_file(path, 4);
Sonny Rao259032b2011-07-14 13:34:43 +1000446
Li Zefan6706ccf2009-09-17 16:34:23 +0800447out:
448 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900449 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200450}
451
Namhyung Kim7f42b952013-03-21 16:18:47 +0900452static void
453put_tracepoints_path(struct tracepoint_path *tps)
454{
455 while (tps) {
456 struct tracepoint_path *t = tps;
457
458 tps = tps->next;
459 free(t->name);
460 free(t->system);
461 free(t);
462 }
463}
464
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200465static struct tracepoint_path *
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200466get_tracepoints_path(struct list_head *pattrs)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200467{
468 struct tracepoint_path path, *ppath = &path;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200469 struct perf_evsel *pos;
470 int nr_tracepoints = 0;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200471
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200472 list_for_each_entry(pos, pattrs, node) {
473 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200474 continue;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200475 ++nr_tracepoints;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200476 ppath->next = tracepoint_id_to_path(pos->attr.config);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900477 if (!ppath->next) {
478 pr_debug("No memory to alloc tracepoints list\n");
479 put_tracepoints_path(&path);
480 return NULL;
481 }
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200482 ppath = ppath->next;
483 }
484
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200485 return nr_tracepoints > 0 ? path.next : NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200486}
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200487
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200488bool have_tracepoints(struct list_head *pattrs)
Tom Zanussi63e0c772010-05-03 00:14:48 -0500489{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200490 struct perf_evsel *pos;
Tom Zanussidb620b12010-05-04 22:20:16 -0500491
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200492 list_for_each_entry(pos, pattrs, node)
493 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
Tom Zanussidb620b12010-05-04 22:20:16 -0500494 return true;
495
496 return false;
Tom Zanussi63e0c772010-05-03 00:14:48 -0500497}
498
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900499static int tracing_data_header(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200500{
Jiri Olsa29208e52011-10-20 15:59:43 +0200501 char buf[20];
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900502 ssize_t size;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200503
Jiri Olsa29208e52011-10-20 15:59:43 +0200504 /* just guessing this is someone's birthday.. ;) */
Steven Rostedt520509432009-08-17 16:18:05 +0200505 buf[0] = 23;
506 buf[1] = 8;
507 buf[2] = 68;
508 memcpy(buf + 3, "tracing", 7);
509
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900510 if (write(output_fd, buf, 10) != 10)
511 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200512
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900513 size = strlen(VERSION) + 1;
514 if (write(output_fd, VERSION, size) != size)
515 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200516
517 /* save endian */
518 if (bigendian())
519 buf[0] = 1;
520 else
521 buf[0] = 0;
522
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200523 read_trace_init(buf[0], buf[0]);
524
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900525 if (write(output_fd, buf, 1) != 1)
526 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200527
528 /* save size of long */
529 buf[0] = sizeof(long);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900530 if (write(output_fd, buf, 1) != 1)
531 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200532
533 /* save page_size */
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900534 if (write(output_fd, &page_size, 4) != 4)
535 return -1;
536
537 return 0;
Jiri Olsa29208e52011-10-20 15:59:43 +0200538}
Steven Rostedt520509432009-08-17 16:18:05 +0200539
Jiri Olsa29208e52011-10-20 15:59:43 +0200540struct tracing_data *tracing_data_get(struct list_head *pattrs,
541 int fd, bool temp)
542{
543 struct tracepoint_path *tps;
544 struct tracing_data *tdata;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900545 int err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200546
547 output_fd = fd;
548
549 tps = get_tracepoints_path(pattrs);
550 if (!tps)
551 return NULL;
552
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900553 tdata = malloc(sizeof(*tdata));
554 if (!tdata)
555 return NULL;
556
Jiri Olsa29208e52011-10-20 15:59:43 +0200557 tdata->temp = temp;
558 tdata->size = 0;
559
560 if (temp) {
561 int temp_fd;
562
563 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
564 "/tmp/perf-XXXXXX");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900565 if (!mkstemp(tdata->temp_file)) {
566 pr_debug("Can't make temp file");
567 return NULL;
568 }
Jiri Olsa29208e52011-10-20 15:59:43 +0200569
570 temp_fd = open(tdata->temp_file, O_RDWR);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900571 if (temp_fd < 0) {
572 pr_debug("Can't read '%s'", tdata->temp_file);
573 return NULL;
574 }
Jiri Olsa29208e52011-10-20 15:59:43 +0200575
576 /*
577 * Set the temp file the default output, so all the
578 * tracing data are stored into it.
579 */
580 output_fd = temp_fd;
581 }
582
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900583 err = tracing_data_header();
584 if (err)
585 goto out;
586 err = read_header_files();
587 if (err)
588 goto out;
589 err = read_ftrace_files(tps);
590 if (err)
591 goto out;
592 err = read_event_files(tps);
593 if (err)
594 goto out;
595 err = read_proc_kallsyms();
596 if (err)
597 goto out;
598 err = read_ftrace_printk();
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200599
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900600out:
Jiri Olsa29208e52011-10-20 15:59:43 +0200601 /*
602 * All tracing data are stored by now, we can restore
603 * the default output file in case we used temp file.
604 */
605 if (temp) {
606 tdata->size = lseek(output_fd, 0, SEEK_CUR);
607 close(output_fd);
608 output_fd = fd;
609 }
610
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900611 if (err) {
612 free(tdata);
613 tdata = NULL;
614 }
615
Jiri Olsa29208e52011-10-20 15:59:43 +0200616 put_tracepoints_path(tps);
617 return tdata;
Steven Rostedt520509432009-08-17 16:18:05 +0200618}
Tom Zanussi92155452010-04-01 23:59:21 -0500619
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900620int tracing_data_put(struct tracing_data *tdata)
Tom Zanussi92155452010-04-01 23:59:21 -0500621{
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900622 int err = 0;
623
Jiri Olsa29208e52011-10-20 15:59:43 +0200624 if (tdata->temp) {
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900625 err = record_file(tdata->temp_file, 0);
Jiri Olsa29208e52011-10-20 15:59:43 +0200626 unlink(tdata->temp_file);
627 }
Tom Zanussi92155452010-04-01 23:59:21 -0500628
Jiri Olsa29208e52011-10-20 15:59:43 +0200629 free(tdata);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900630 return err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200631}
Tom Zanussi92155452010-04-01 23:59:21 -0500632
Jiri Olsa29208e52011-10-20 15:59:43 +0200633int read_tracing_data(int fd, struct list_head *pattrs)
634{
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900635 int err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200636 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -0500637
Jiri Olsa29208e52011-10-20 15:59:43 +0200638 /*
639 * We work over the real file, so we can write data
640 * directly, no temp file is needed.
641 */
642 tdata = tracing_data_get(pattrs, fd, false);
643 if (!tdata)
644 return -ENOMEM;
645
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900646 err = tracing_data_put(tdata);
647 return err;
Tom Zanussi92155452010-04-01 23:59:21 -0500648}