Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 1 | /* |
| 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 Melo | c168fbf | 2011-11-16 12:55:59 -0200 | [diff] [blame] | 21 | #include "util.h" |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 22 | #include <dirent.h> |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 23 | #include <mntent.h> |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 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> |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 34 | #include <errno.h> |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 35 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 36 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 37 | #include <linux/kernel.h> |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 38 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 39 | #include "../perf.h" |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 40 | #include "trace-event.h" |
Borislav Petkov | 553873e | 2013-12-09 17:14:23 +0100 | [diff] [blame] | 41 | #include <api/fs/debugfs.h> |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 42 | #include "evsel.h" |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 43 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 44 | #define VERSION "0.5" |
| 45 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 46 | static int output_fd; |
| 47 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 48 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 49 | int bigendian(void) |
| 50 | { |
| 51 | unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0}; |
| 52 | unsigned int *ptr; |
| 53 | |
Ingo Molnar | 65014ab | 2009-09-02 14:55:55 +0200 | [diff] [blame] | 54 | ptr = (unsigned int *)(void *)str; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 55 | return *ptr == 0x01020304; |
| 56 | } |
| 57 | |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 58 | /* unfortunately, you can not stat debugfs or proc files for size */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 59 | static int record_file(const char *file, ssize_t hdr_sz) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 60 | { |
| 61 | unsigned long long size = 0; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 62 | char buf[BUFSIZ], *sizep; |
| 63 | off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); |
| 64 | int r, fd; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 65 | int err = -EIO; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 66 | |
| 67 | fd = open(file, O_RDONLY); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 68 | if (fd < 0) { |
| 69 | pr_debug("Can't read '%s'", file); |
| 70 | return -errno; |
| 71 | } |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 72 | |
| 73 | /* put in zeros for file size, then fill true size later */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 74 | if (hdr_sz) { |
| 75 | if (write(output_fd, &size, hdr_sz) != hdr_sz) |
| 76 | goto out; |
| 77 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 78 | |
| 79 | do { |
| 80 | r = read(fd, buf, BUFSIZ); |
| 81 | if (r > 0) { |
| 82 | size += r; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 83 | if (write(output_fd, buf, r) != r) |
| 84 | goto out; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 85 | } |
| 86 | } while (r > 0); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 87 | |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 88 | /* ugh, handle big-endian hdr_size == 4 */ |
| 89 | sizep = (char*)&size; |
| 90 | if (bigendian()) |
| 91 | sizep += sizeof(u64) - hdr_sz; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 92 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 93 | 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 97 | |
| 98 | err = 0; |
| 99 | out: |
| 100 | close(fd); |
| 101 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 104 | static int record_header_files(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 105 | { |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 106 | char *path; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 107 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 108 | int err = -EIO; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 109 | |
| 110 | path = get_tracing_file("events/header_page"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 111 | if (!path) { |
| 112 | pr_debug("can't get tracing/events/header_page"); |
| 113 | return -ENOMEM; |
| 114 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 115 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 116 | if (stat(path, &st) < 0) { |
| 117 | pr_debug("can't read '%s'", path); |
| 118 | goto out; |
| 119 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 120 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 121 | 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 Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 131 | put_tracing_file(path); |
| 132 | |
| 133 | path = get_tracing_file("events/header_event"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 134 | if (!path) { |
| 135 | pr_debug("can't get tracing/events/header_event"); |
| 136 | err = -ENOMEM; |
| 137 | goto out; |
| 138 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 139 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 140 | if (stat(path, &st) < 0) { |
| 141 | pr_debug("can't read '%s'", path); |
| 142 | goto out; |
| 143 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 144 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 145 | 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; |
| 156 | out: |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 157 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 158 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 161 | static 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 Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 172 | static int copy_event_system(const char *sys, struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 173 | { |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 174 | struct dirent *dent; |
| 175 | struct stat st; |
| 176 | char *format; |
| 177 | DIR *dir; |
| 178 | int count = 0; |
| 179 | int ret; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 180 | int err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 181 | |
| 182 | dir = opendir(sys); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 183 | if (!dir) { |
| 184 | pr_debug("can't read directory '%s'", sys); |
| 185 | return -errno; |
| 186 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 187 | |
| 188 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 189 | if (dent->d_type != DT_DIR || |
| 190 | strcmp(dent->d_name, ".") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 191 | strcmp(dent->d_name, "..") == 0 || |
| 192 | !name_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 193 | continue; |
Andy Shevchenko | d400a68 | 2014-07-04 14:43:48 +0300 | [diff] [blame^] | 194 | if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) { |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 195 | err = -ENOMEM; |
| 196 | goto out; |
| 197 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 198 | ret = stat(format, &st); |
| 199 | free(format); |
| 200 | if (ret < 0) |
| 201 | continue; |
| 202 | count++; |
| 203 | } |
| 204 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 205 | if (write(output_fd, &count, 4) != 4) { |
| 206 | err = -EIO; |
| 207 | pr_debug("can't write count\n"); |
| 208 | goto out; |
| 209 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 210 | |
| 211 | rewinddir(dir); |
| 212 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 213 | if (dent->d_type != DT_DIR || |
| 214 | strcmp(dent->d_name, ".") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 215 | strcmp(dent->d_name, "..") == 0 || |
| 216 | !name_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 217 | continue; |
Andy Shevchenko | d400a68 | 2014-07-04 14:43:48 +0300 | [diff] [blame^] | 218 | if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) { |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 219 | err = -ENOMEM; |
| 220 | goto out; |
| 221 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 222 | ret = stat(format, &st); |
| 223 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 224 | if (ret >= 0) { |
| 225 | err = record_file(format, 8); |
| 226 | if (err) { |
| 227 | free(format); |
| 228 | goto out; |
| 229 | } |
| 230 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 231 | free(format); |
| 232 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 233 | err = 0; |
| 234 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 235 | closedir(dir); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 236 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 239 | static int record_ftrace_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 240 | { |
| 241 | char *path; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 242 | int ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 243 | |
| 244 | path = get_tracing_file("events/ftrace"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 245 | if (!path) { |
| 246 | pr_debug("can't get tracing/events/ftrace"); |
| 247 | return -ENOMEM; |
| 248 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 249 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 250 | ret = copy_event_system(path, tps); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 251 | |
| 252 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 253 | |
| 254 | return ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 257 | static bool system_in_tp_list(char *sys, struct tracepoint_path *tps) |
| 258 | { |
| 259 | while (tps) { |
| 260 | if (!strcmp(sys, tps->system)) |
| 261 | return true; |
| 262 | tps = tps->next; |
| 263 | } |
| 264 | |
| 265 | return false; |
| 266 | } |
| 267 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 268 | static int record_event_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 269 | { |
| 270 | struct dirent *dent; |
| 271 | struct stat st; |
| 272 | char *path; |
| 273 | char *sys; |
| 274 | DIR *dir; |
| 275 | int count = 0; |
| 276 | int ret; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 277 | int err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 278 | |
| 279 | path = get_tracing_file("events"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 280 | if (!path) { |
| 281 | pr_debug("can't get tracing/events"); |
| 282 | return -ENOMEM; |
| 283 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 284 | |
| 285 | dir = opendir(path); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 286 | if (!dir) { |
| 287 | err = -errno; |
| 288 | pr_debug("can't read directory '%s'", path); |
| 289 | goto out; |
| 290 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 291 | |
| 292 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 293 | if (dent->d_type != DT_DIR || |
| 294 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 295 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 296 | strcmp(dent->d_name, "ftrace") == 0 || |
| 297 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 298 | continue; |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 299 | count++; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 302 | if (write(output_fd, &count, 4) != 4) { |
| 303 | err = -EIO; |
| 304 | pr_debug("can't write count\n"); |
| 305 | goto out; |
| 306 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 307 | |
| 308 | rewinddir(dir); |
| 309 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 310 | if (dent->d_type != DT_DIR || |
| 311 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 312 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 313 | strcmp(dent->d_name, "ftrace") == 0 || |
| 314 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 315 | continue; |
Andy Shevchenko | d400a68 | 2014-07-04 14:43:48 +0300 | [diff] [blame^] | 316 | if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) { |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 317 | err = -ENOMEM; |
| 318 | goto out; |
| 319 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 320 | ret = stat(sys, &st); |
| 321 | if (ret >= 0) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 322 | ssize_t size = strlen(dent->d_name) + 1; |
| 323 | |
| 324 | if (write(output_fd, dent->d_name, size) != size || |
| 325 | copy_event_system(sys, tps) < 0) { |
| 326 | err = -EIO; |
| 327 | free(sys); |
| 328 | goto out; |
| 329 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 330 | } |
| 331 | free(sys); |
| 332 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 333 | err = 0; |
| 334 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 335 | closedir(dir); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 336 | put_tracing_file(path); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 337 | |
| 338 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 341 | static int record_proc_kallsyms(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 342 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 343 | unsigned int size; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 344 | const char *path = "/proc/kallsyms"; |
| 345 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 346 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 347 | |
| 348 | ret = stat(path, &st); |
| 349 | if (ret < 0) { |
| 350 | /* not found */ |
| 351 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 352 | if (write(output_fd, &size, 4) != 4) |
| 353 | err = -EIO; |
| 354 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 355 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 356 | return record_file(path, 4); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 359 | static int record_ftrace_printk(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 360 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 361 | unsigned int size; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 362 | char *path; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 363 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 364 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 365 | |
| 366 | path = get_tracing_file("printk_formats"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 367 | if (!path) { |
| 368 | pr_debug("can't get tracing/printk_formats"); |
| 369 | return -ENOMEM; |
| 370 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 371 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 372 | ret = stat(path, &st); |
| 373 | if (ret < 0) { |
| 374 | /* not found */ |
| 375 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 376 | if (write(output_fd, &size, 4) != 4) |
| 377 | err = -EIO; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 378 | goto out; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 379 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 380 | err = record_file(path, 4); |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 381 | |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 382 | out: |
| 383 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 384 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 387 | static void |
| 388 | put_tracepoints_path(struct tracepoint_path *tps) |
| 389 | { |
| 390 | while (tps) { |
| 391 | struct tracepoint_path *t = tps; |
| 392 | |
| 393 | tps = tps->next; |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 394 | zfree(&t->name); |
| 395 | zfree(&t->system); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 396 | free(t); |
| 397 | } |
| 398 | } |
| 399 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 400 | static struct tracepoint_path * |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 401 | get_tracepoints_path(struct list_head *pattrs) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 402 | { |
| 403 | struct tracepoint_path path, *ppath = &path; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 404 | struct perf_evsel *pos; |
| 405 | int nr_tracepoints = 0; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 406 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 407 | list_for_each_entry(pos, pattrs, node) { |
| 408 | if (pos->attr.type != PERF_TYPE_TRACEPOINT) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 409 | continue; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 410 | ++nr_tracepoints; |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 411 | |
| 412 | if (pos->name) { |
| 413 | ppath->next = tracepoint_name_to_path(pos->name); |
| 414 | if (ppath->next) |
| 415 | goto next; |
| 416 | |
| 417 | if (strchr(pos->name, ':') == NULL) |
| 418 | goto try_id; |
| 419 | |
| 420 | goto error; |
| 421 | } |
| 422 | |
| 423 | try_id: |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 424 | ppath->next = tracepoint_id_to_path(pos->attr.config); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 425 | if (!ppath->next) { |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 426 | error: |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 427 | pr_debug("No memory to alloc tracepoints list\n"); |
| 428 | put_tracepoints_path(&path); |
| 429 | return NULL; |
| 430 | } |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 431 | next: |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 432 | ppath = ppath->next; |
| 433 | } |
| 434 | |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 435 | return nr_tracepoints > 0 ? path.next : NULL; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 436 | } |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 437 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 438 | bool have_tracepoints(struct list_head *pattrs) |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 439 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 440 | struct perf_evsel *pos; |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 441 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 442 | list_for_each_entry(pos, pattrs, node) |
| 443 | if (pos->attr.type == PERF_TYPE_TRACEPOINT) |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 444 | return true; |
| 445 | |
| 446 | return false; |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 449 | static int tracing_data_header(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 450 | { |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 451 | char buf[20]; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 452 | ssize_t size; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 453 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 454 | /* just guessing this is someone's birthday.. ;) */ |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 455 | buf[0] = 23; |
| 456 | buf[1] = 8; |
| 457 | buf[2] = 68; |
| 458 | memcpy(buf + 3, "tracing", 7); |
| 459 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 460 | if (write(output_fd, buf, 10) != 10) |
| 461 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 462 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 463 | size = strlen(VERSION) + 1; |
| 464 | if (write(output_fd, VERSION, size) != size) |
| 465 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 466 | |
| 467 | /* save endian */ |
| 468 | if (bigendian()) |
| 469 | buf[0] = 1; |
| 470 | else |
| 471 | buf[0] = 0; |
| 472 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 473 | if (write(output_fd, buf, 1) != 1) |
| 474 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 475 | |
| 476 | /* save size of long */ |
| 477 | buf[0] = sizeof(long); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 478 | if (write(output_fd, buf, 1) != 1) |
| 479 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 480 | |
| 481 | /* save page_size */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 482 | if (write(output_fd, &page_size, 4) != 4) |
| 483 | return -1; |
| 484 | |
| 485 | return 0; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 486 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 487 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 488 | struct tracing_data *tracing_data_get(struct list_head *pattrs, |
| 489 | int fd, bool temp) |
| 490 | { |
| 491 | struct tracepoint_path *tps; |
| 492 | struct tracing_data *tdata; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 493 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 494 | |
| 495 | output_fd = fd; |
| 496 | |
| 497 | tps = get_tracepoints_path(pattrs); |
| 498 | if (!tps) |
| 499 | return NULL; |
| 500 | |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 501 | tdata = malloc(sizeof(*tdata)); |
| 502 | if (!tdata) |
| 503 | return NULL; |
| 504 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 505 | tdata->temp = temp; |
| 506 | tdata->size = 0; |
| 507 | |
| 508 | if (temp) { |
| 509 | int temp_fd; |
| 510 | |
| 511 | snprintf(tdata->temp_file, sizeof(tdata->temp_file), |
| 512 | "/tmp/perf-XXXXXX"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 513 | if (!mkstemp(tdata->temp_file)) { |
| 514 | pr_debug("Can't make temp file"); |
| 515 | return NULL; |
| 516 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 517 | |
| 518 | temp_fd = open(tdata->temp_file, O_RDWR); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 519 | if (temp_fd < 0) { |
| 520 | pr_debug("Can't read '%s'", tdata->temp_file); |
| 521 | return NULL; |
| 522 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 523 | |
| 524 | /* |
| 525 | * Set the temp file the default output, so all the |
| 526 | * tracing data are stored into it. |
| 527 | */ |
| 528 | output_fd = temp_fd; |
| 529 | } |
| 530 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 531 | err = tracing_data_header(); |
| 532 | if (err) |
| 533 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 534 | err = record_header_files(); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 535 | if (err) |
| 536 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 537 | err = record_ftrace_files(tps); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 538 | if (err) |
| 539 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 540 | err = record_event_files(tps); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 541 | if (err) |
| 542 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 543 | err = record_proc_kallsyms(); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 544 | if (err) |
| 545 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 546 | err = record_ftrace_printk(); |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 547 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 548 | out: |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 549 | /* |
| 550 | * All tracing data are stored by now, we can restore |
| 551 | * the default output file in case we used temp file. |
| 552 | */ |
| 553 | if (temp) { |
| 554 | tdata->size = lseek(output_fd, 0, SEEK_CUR); |
| 555 | close(output_fd); |
| 556 | output_fd = fd; |
| 557 | } |
| 558 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 559 | if (err) |
| 560 | zfree(&tdata); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 561 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 562 | put_tracepoints_path(tps); |
| 563 | return tdata; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 564 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 565 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 566 | int tracing_data_put(struct tracing_data *tdata) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 567 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 568 | int err = 0; |
| 569 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 570 | if (tdata->temp) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 571 | err = record_file(tdata->temp_file, 0); |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 572 | unlink(tdata->temp_file); |
| 573 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 574 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 575 | free(tdata); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 576 | return err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 577 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 578 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 579 | int read_tracing_data(int fd, struct list_head *pattrs) |
| 580 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 581 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 582 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 583 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 584 | /* |
| 585 | * We work over the real file, so we can write data |
| 586 | * directly, no temp file is needed. |
| 587 | */ |
| 588 | tdata = tracing_data_get(pattrs, fd, false); |
| 589 | if (!tdata) |
| 590 | return -ENOMEM; |
| 591 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 592 | err = tracing_data_put(tdata); |
| 593 | return err; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 594 | } |