blob: f3c9e551bd353f39cd68bf52ec4c1c4ea6892a2f [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 +020049int bigendian(void)
50{
51 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
52 unsigned int *ptr;
53
Ingo Molnar65014ab2009-09-02 14:55:55 +020054 ptr = (unsigned int *)(void *)str;
Steven Rostedt520509432009-08-17 16:18:05 +020055 return *ptr == 0x01020304;
56}
57
Sonny Rao259032b2011-07-14 13:34:43 +100058/* unfortunately, you can not stat debugfs or proc files for size */
Namhyung Kim8755d5e2013-03-21 16:18:46 +090059static int record_file(const char *file, ssize_t hdr_sz)
Steven Rostedt520509432009-08-17 16:18:05 +020060{
61 unsigned long long size = 0;
Sonny Rao259032b2011-07-14 13:34:43 +100062 char buf[BUFSIZ], *sizep;
63 off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
64 int r, fd;
Namhyung Kim8755d5e2013-03-21 16:18:46 +090065 int err = -EIO;
Sonny Rao259032b2011-07-14 13:34:43 +100066
67 fd = open(file, O_RDONLY);
Namhyung Kim7f42b952013-03-21 16:18:47 +090068 if (fd < 0) {
69 pr_debug("Can't read '%s'", file);
70 return -errno;
71 }
Sonny Rao259032b2011-07-14 13:34:43 +100072
73 /* put in zeros for file size, then fill true size later */
Namhyung Kim8755d5e2013-03-21 16:18:46 +090074 if (hdr_sz) {
75 if (write(output_fd, &size, hdr_sz) != hdr_sz)
76 goto out;
77 }
Steven Rostedt520509432009-08-17 16:18:05 +020078
79 do {
80 r = read(fd, buf, BUFSIZ);
81 if (r > 0) {
82 size += r;
Namhyung Kim8755d5e2013-03-21 16:18:46 +090083 if (write(output_fd, buf, r) != r)
84 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +020085 }
86 } while (r > 0);
Steven Rostedt520509432009-08-17 16:18:05 +020087
Sonny Rao259032b2011-07-14 13:34:43 +100088 /* ugh, handle big-endian hdr_size == 4 */
89 sizep = (char*)&size;
90 if (bigendian())
91 sizep += sizeof(u64) - hdr_sz;
Steven Rostedt520509432009-08-17 16:18:05 +020092
Namhyung Kim7f42b952013-03-21 16:18:47 +090093 if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
94 pr_debug("writing file size failed\n");
95 goto out;
96 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +090097
98 err = 0;
99out:
100 close(fd);
101 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200102}
103
Namhyung Kim077f1592013-06-04 14:20:29 +0900104static int record_header_files(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200105{
Steven Rostedt520509432009-08-17 16:18:05 +0200106 char *path;
Sonny Rao259032b2011-07-14 13:34:43 +1000107 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900108 int err = -EIO;
Steven Rostedt520509432009-08-17 16:18:05 +0200109
110 path = get_tracing_file("events/header_page");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900111 if (!path) {
112 pr_debug("can't get tracing/events/header_page");
113 return -ENOMEM;
114 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900115
Namhyung Kim7f42b952013-03-21 16:18:47 +0900116 if (stat(path, &st) < 0) {
117 pr_debug("can't read '%s'", path);
118 goto out;
119 }
Steven Rostedt520509432009-08-17 16:18:05 +0200120
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900121 if (write(output_fd, "header_page", 12) != 12) {
122 pr_debug("can't write header_page\n");
123 goto out;
124 }
125
126 if (record_file(path, 8) < 0) {
127 pr_debug("can't record header_page file\n");
128 goto out;
129 }
130
Steven Rostedt520509432009-08-17 16:18:05 +0200131 put_tracing_file(path);
132
133 path = get_tracing_file("events/header_event");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900134 if (!path) {
135 pr_debug("can't get tracing/events/header_event");
136 err = -ENOMEM;
137 goto out;
138 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900139
Namhyung Kim7f42b952013-03-21 16:18:47 +0900140 if (stat(path, &st) < 0) {
141 pr_debug("can't read '%s'", path);
142 goto out;
143 }
Steven Rostedt520509432009-08-17 16:18:05 +0200144
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900145 if (write(output_fd, "header_event", 13) != 13) {
146 pr_debug("can't write header_event\n");
147 goto out;
148 }
149
150 if (record_file(path, 8) < 0) {
151 pr_debug("can't record header_event file\n");
152 goto out;
153 }
154
155 err = 0;
156out:
Steven Rostedt520509432009-08-17 16:18:05 +0200157 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900158 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200159}
160
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200161static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
162{
163 while (tps) {
164 if (!strcmp(sys, tps->name))
165 return true;
166 tps = tps->next;
167 }
168
169 return false;
170}
171
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900172static int copy_event_system(const char *sys, struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200173{
Steven Rostedt520509432009-08-17 16:18:05 +0200174 struct dirent *dent;
175 struct stat st;
176 char *format;
177 DIR *dir;
178 int count = 0;
179 int ret;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900180 int err;
Steven Rostedt520509432009-08-17 16:18:05 +0200181
182 dir = opendir(sys);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900183 if (!dir) {
184 pr_debug("can't read directory '%s'", sys);
185 return -errno;
186 }
Steven Rostedt520509432009-08-17 16:18:05 +0200187
188 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500189 if (dent->d_type != DT_DIR ||
190 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200191 strcmp(dent->d_name, "..") == 0 ||
192 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200193 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900194 format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
195 if (!format) {
196 err = -ENOMEM;
197 goto out;
198 }
Steven Rostedt520509432009-08-17 16:18:05 +0200199 sprintf(format, "%s/%s/format", sys, dent->d_name);
200 ret = stat(format, &st);
201 free(format);
202 if (ret < 0)
203 continue;
204 count++;
205 }
206
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900207 if (write(output_fd, &count, 4) != 4) {
208 err = -EIO;
209 pr_debug("can't write count\n");
210 goto out;
211 }
Steven Rostedt520509432009-08-17 16:18:05 +0200212
213 rewinddir(dir);
214 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500215 if (dent->d_type != DT_DIR ||
216 strcmp(dent->d_name, ".") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200217 strcmp(dent->d_name, "..") == 0 ||
218 !name_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200219 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900220 format = malloc(strlen(sys) + strlen(dent->d_name) + 10);
221 if (!format) {
222 err = -ENOMEM;
223 goto out;
224 }
Steven Rostedt520509432009-08-17 16:18:05 +0200225 sprintf(format, "%s/%s/format", sys, dent->d_name);
226 ret = stat(format, &st);
227
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900228 if (ret >= 0) {
229 err = record_file(format, 8);
230 if (err) {
231 free(format);
232 goto out;
233 }
234 }
Steven Rostedt520509432009-08-17 16:18:05 +0200235 free(format);
236 }
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900237 err = 0;
238out:
Xiao Guangrong99674112009-12-28 16:49:38 +0800239 closedir(dir);
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900240 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200241}
242
Namhyung Kim077f1592013-06-04 14:20:29 +0900243static int record_ftrace_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200244{
245 char *path;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900246 int ret;
Steven Rostedt520509432009-08-17 16:18:05 +0200247
248 path = get_tracing_file("events/ftrace");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900249 if (!path) {
250 pr_debug("can't get tracing/events/ftrace");
251 return -ENOMEM;
252 }
Steven Rostedt520509432009-08-17 16:18:05 +0200253
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900254 ret = copy_event_system(path, tps);
Steven Rostedt520509432009-08-17 16:18:05 +0200255
256 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900257
258 return ret;
Steven Rostedt520509432009-08-17 16:18:05 +0200259}
260
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200261static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
262{
263 while (tps) {
264 if (!strcmp(sys, tps->system))
265 return true;
266 tps = tps->next;
267 }
268
269 return false;
270}
271
Namhyung Kim077f1592013-06-04 14:20:29 +0900272static int record_event_files(struct tracepoint_path *tps)
Steven Rostedt520509432009-08-17 16:18:05 +0200273{
274 struct dirent *dent;
275 struct stat st;
276 char *path;
277 char *sys;
278 DIR *dir;
279 int count = 0;
280 int ret;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900281 int err;
Steven Rostedt520509432009-08-17 16:18:05 +0200282
283 path = get_tracing_file("events");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900284 if (!path) {
285 pr_debug("can't get tracing/events");
286 return -ENOMEM;
287 }
Steven Rostedt520509432009-08-17 16:18:05 +0200288
289 dir = opendir(path);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900290 if (!dir) {
291 err = -errno;
292 pr_debug("can't read directory '%s'", path);
293 goto out;
294 }
Steven Rostedt520509432009-08-17 16:18:05 +0200295
296 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500297 if (dent->d_type != DT_DIR ||
298 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200299 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200300 strcmp(dent->d_name, "ftrace") == 0 ||
301 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200302 continue;
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500303 count++;
Steven Rostedt520509432009-08-17 16:18:05 +0200304 }
305
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900306 if (write(output_fd, &count, 4) != 4) {
307 err = -EIO;
308 pr_debug("can't write count\n");
309 goto out;
310 }
Steven Rostedt520509432009-08-17 16:18:05 +0200311
312 rewinddir(dir);
313 while ((dent = readdir(dir))) {
Ulrich Drepper659d8cf2009-12-19 16:40:28 -0500314 if (dent->d_type != DT_DIR ||
315 strcmp(dent->d_name, ".") == 0 ||
Steven Rostedt520509432009-08-17 16:18:05 +0200316 strcmp(dent->d_name, "..") == 0 ||
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200317 strcmp(dent->d_name, "ftrace") == 0 ||
318 !system_in_tp_list(dent->d_name, tps))
Steven Rostedt520509432009-08-17 16:18:05 +0200319 continue;
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900320 sys = malloc(strlen(path) + strlen(dent->d_name) + 2);
321 if (!sys) {
322 err = -ENOMEM;
323 goto out;
324 }
Steven Rostedt520509432009-08-17 16:18:05 +0200325 sprintf(sys, "%s/%s", path, dent->d_name);
326 ret = stat(sys, &st);
327 if (ret >= 0) {
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900328 ssize_t size = strlen(dent->d_name) + 1;
329
330 if (write(output_fd, dent->d_name, size) != size ||
331 copy_event_system(sys, tps) < 0) {
332 err = -EIO;
333 free(sys);
334 goto out;
335 }
Steven Rostedt520509432009-08-17 16:18:05 +0200336 }
337 free(sys);
338 }
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900339 err = 0;
340out:
Xiao Guangrong99674112009-12-28 16:49:38 +0800341 closedir(dir);
Steven Rostedt520509432009-08-17 16:18:05 +0200342 put_tracing_file(path);
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900343
344 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200345}
346
Namhyung Kim077f1592013-06-04 14:20:29 +0900347static int record_proc_kallsyms(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200348{
Sonny Rao259032b2011-07-14 13:34:43 +1000349 unsigned int size;
Steven Rostedt520509432009-08-17 16:18:05 +0200350 const char *path = "/proc/kallsyms";
351 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900352 int ret, err = 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200353
354 ret = stat(path, &st);
355 if (ret < 0) {
356 /* not found */
357 size = 0;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900358 if (write(output_fd, &size, 4) != 4)
359 err = -EIO;
360 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200361 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900362 return record_file(path, 4);
Steven Rostedt520509432009-08-17 16:18:05 +0200363}
364
Namhyung Kim077f1592013-06-04 14:20:29 +0900365static int record_ftrace_printk(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200366{
Sonny Rao259032b2011-07-14 13:34:43 +1000367 unsigned int size;
Li Zefan6706ccf2009-09-17 16:34:23 +0800368 char *path;
Steven Rostedt520509432009-08-17 16:18:05 +0200369 struct stat st;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900370 int ret, err = 0;
Steven Rostedt520509432009-08-17 16:18:05 +0200371
372 path = get_tracing_file("printk_formats");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900373 if (!path) {
374 pr_debug("can't get tracing/printk_formats");
375 return -ENOMEM;
376 }
Namhyung Kim454f8c72013-03-21 16:18:44 +0900377
Steven Rostedt520509432009-08-17 16:18:05 +0200378 ret = stat(path, &st);
379 if (ret < 0) {
380 /* not found */
381 size = 0;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900382 if (write(output_fd, &size, 4) != 4)
383 err = -EIO;
Li Zefan6706ccf2009-09-17 16:34:23 +0800384 goto out;
Steven Rostedt520509432009-08-17 16:18:05 +0200385 }
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900386 err = record_file(path, 4);
Sonny Rao259032b2011-07-14 13:34:43 +1000387
Li Zefan6706ccf2009-09-17 16:34:23 +0800388out:
389 put_tracing_file(path);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900390 return err;
Steven Rostedt520509432009-08-17 16:18:05 +0200391}
392
Namhyung Kim7f42b952013-03-21 16:18:47 +0900393static void
394put_tracepoints_path(struct tracepoint_path *tps)
395{
396 while (tps) {
397 struct tracepoint_path *t = tps;
398
399 tps = tps->next;
400 free(t->name);
401 free(t->system);
402 free(t);
403 }
404}
405
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200406static struct tracepoint_path *
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200407get_tracepoints_path(struct list_head *pattrs)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200408{
409 struct tracepoint_path path, *ppath = &path;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200410 struct perf_evsel *pos;
411 int nr_tracepoints = 0;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200412
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200413 list_for_each_entry(pos, pattrs, node) {
414 if (pos->attr.type != PERF_TYPE_TRACEPOINT)
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200415 continue;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200416 ++nr_tracepoints;
Namhyung Kime7c93f02013-06-26 16:14:05 +0900417
418 if (pos->name) {
419 ppath->next = tracepoint_name_to_path(pos->name);
420 if (ppath->next)
421 goto next;
422
423 if (strchr(pos->name, ':') == NULL)
424 goto try_id;
425
426 goto error;
427 }
428
429try_id:
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200430 ppath->next = tracepoint_id_to_path(pos->attr.config);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900431 if (!ppath->next) {
Namhyung Kime7c93f02013-06-26 16:14:05 +0900432error:
Namhyung Kim7f42b952013-03-21 16:18:47 +0900433 pr_debug("No memory to alloc tracepoints list\n");
434 put_tracepoints_path(&path);
435 return NULL;
436 }
Namhyung Kime7c93f02013-06-26 16:14:05 +0900437next:
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200438 ppath = ppath->next;
439 }
440
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200441 return nr_tracepoints > 0 ? path.next : NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200442}
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200443
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200444bool have_tracepoints(struct list_head *pattrs)
Tom Zanussi63e0c772010-05-03 00:14:48 -0500445{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200446 struct perf_evsel *pos;
Tom Zanussidb620b12010-05-04 22:20:16 -0500447
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200448 list_for_each_entry(pos, pattrs, node)
449 if (pos->attr.type == PERF_TYPE_TRACEPOINT)
Tom Zanussidb620b12010-05-04 22:20:16 -0500450 return true;
451
452 return false;
Tom Zanussi63e0c772010-05-03 00:14:48 -0500453}
454
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900455static int tracing_data_header(void)
Steven Rostedt520509432009-08-17 16:18:05 +0200456{
Jiri Olsa29208e52011-10-20 15:59:43 +0200457 char buf[20];
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900458 ssize_t size;
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200459
Jiri Olsa29208e52011-10-20 15:59:43 +0200460 /* just guessing this is someone's birthday.. ;) */
Steven Rostedt520509432009-08-17 16:18:05 +0200461 buf[0] = 23;
462 buf[1] = 8;
463 buf[2] = 68;
464 memcpy(buf + 3, "tracing", 7);
465
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900466 if (write(output_fd, buf, 10) != 10)
467 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200468
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900469 size = strlen(VERSION) + 1;
470 if (write(output_fd, VERSION, size) != size)
471 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200472
473 /* save endian */
474 if (bigendian())
475 buf[0] = 1;
476 else
477 buf[0] = 0;
478
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900479 if (write(output_fd, buf, 1) != 1)
480 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200481
482 /* save size of long */
483 buf[0] = sizeof(long);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900484 if (write(output_fd, buf, 1) != 1)
485 return -1;
Steven Rostedt520509432009-08-17 16:18:05 +0200486
487 /* save page_size */
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900488 if (write(output_fd, &page_size, 4) != 4)
489 return -1;
490
491 return 0;
Jiri Olsa29208e52011-10-20 15:59:43 +0200492}
Steven Rostedt520509432009-08-17 16:18:05 +0200493
Jiri Olsa29208e52011-10-20 15:59:43 +0200494struct tracing_data *tracing_data_get(struct list_head *pattrs,
495 int fd, bool temp)
496{
497 struct tracepoint_path *tps;
498 struct tracing_data *tdata;
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900499 int err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200500
501 output_fd = fd;
502
503 tps = get_tracepoints_path(pattrs);
504 if (!tps)
505 return NULL;
506
Namhyung Kim5a6fd272013-03-21 16:18:45 +0900507 tdata = malloc(sizeof(*tdata));
508 if (!tdata)
509 return NULL;
510
Jiri Olsa29208e52011-10-20 15:59:43 +0200511 tdata->temp = temp;
512 tdata->size = 0;
513
514 if (temp) {
515 int temp_fd;
516
517 snprintf(tdata->temp_file, sizeof(tdata->temp_file),
518 "/tmp/perf-XXXXXX");
Namhyung Kim7f42b952013-03-21 16:18:47 +0900519 if (!mkstemp(tdata->temp_file)) {
520 pr_debug("Can't make temp file");
521 return NULL;
522 }
Jiri Olsa29208e52011-10-20 15:59:43 +0200523
524 temp_fd = open(tdata->temp_file, O_RDWR);
Namhyung Kim7f42b952013-03-21 16:18:47 +0900525 if (temp_fd < 0) {
526 pr_debug("Can't read '%s'", tdata->temp_file);
527 return NULL;
528 }
Jiri Olsa29208e52011-10-20 15:59:43 +0200529
530 /*
531 * Set the temp file the default output, so all the
532 * tracing data are stored into it.
533 */
534 output_fd = temp_fd;
535 }
536
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900537 err = tracing_data_header();
538 if (err)
539 goto out;
Namhyung Kim077f1592013-06-04 14:20:29 +0900540 err = record_header_files();
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900541 if (err)
542 goto out;
Namhyung Kim077f1592013-06-04 14:20:29 +0900543 err = record_ftrace_files(tps);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900544 if (err)
545 goto out;
Namhyung Kim077f1592013-06-04 14:20:29 +0900546 err = record_event_files(tps);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900547 if (err)
548 goto out;
Namhyung Kim077f1592013-06-04 14:20:29 +0900549 err = record_proc_kallsyms();
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900550 if (err)
551 goto out;
Namhyung Kim077f1592013-06-04 14:20:29 +0900552 err = record_ftrace_printk();
Arnaldo Carvalho de Meloe2561362009-11-21 14:31:26 -0200553
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900554out:
Jiri Olsa29208e52011-10-20 15:59:43 +0200555 /*
556 * All tracing data are stored by now, we can restore
557 * the default output file in case we used temp file.
558 */
559 if (temp) {
560 tdata->size = lseek(output_fd, 0, SEEK_CUR);
561 close(output_fd);
562 output_fd = fd;
563 }
564
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900565 if (err) {
566 free(tdata);
567 tdata = NULL;
568 }
569
Jiri Olsa29208e52011-10-20 15:59:43 +0200570 put_tracepoints_path(tps);
571 return tdata;
Steven Rostedt520509432009-08-17 16:18:05 +0200572}
Tom Zanussi92155452010-04-01 23:59:21 -0500573
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900574int tracing_data_put(struct tracing_data *tdata)
Tom Zanussi92155452010-04-01 23:59:21 -0500575{
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900576 int err = 0;
577
Jiri Olsa29208e52011-10-20 15:59:43 +0200578 if (tdata->temp) {
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900579 err = record_file(tdata->temp_file, 0);
Jiri Olsa29208e52011-10-20 15:59:43 +0200580 unlink(tdata->temp_file);
581 }
Tom Zanussi92155452010-04-01 23:59:21 -0500582
Jiri Olsa29208e52011-10-20 15:59:43 +0200583 free(tdata);
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900584 return err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200585}
Tom Zanussi92155452010-04-01 23:59:21 -0500586
Jiri Olsa29208e52011-10-20 15:59:43 +0200587int read_tracing_data(int fd, struct list_head *pattrs)
588{
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900589 int err;
Jiri Olsa29208e52011-10-20 15:59:43 +0200590 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -0500591
Jiri Olsa29208e52011-10-20 15:59:43 +0200592 /*
593 * We work over the real file, so we can write data
594 * directly, no temp file is needed.
595 */
596 tdata = tracing_data_get(pattrs, fd, false);
597 if (!tdata)
598 return -ENOMEM;
599
Namhyung Kim8755d5e2013-03-21 16:18:46 +0900600 err = tracing_data_put(tdata);
601 return err;
Tom Zanussi92155452010-04-01 23:59:21 -0500602}