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; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 194 | format = malloc(strlen(sys) + strlen(dent->d_name) + 10); |
| 195 | if (!format) { |
| 196 | err = -ENOMEM; |
| 197 | goto out; |
| 198 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 199 | 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 207 | if (write(output_fd, &count, 4) != 4) { |
| 208 | err = -EIO; |
| 209 | pr_debug("can't write count\n"); |
| 210 | goto out; |
| 211 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 212 | |
| 213 | rewinddir(dir); |
| 214 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 215 | if (dent->d_type != DT_DIR || |
| 216 | strcmp(dent->d_name, ".") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 217 | strcmp(dent->d_name, "..") == 0 || |
| 218 | !name_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 219 | continue; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 220 | format = malloc(strlen(sys) + strlen(dent->d_name) + 10); |
| 221 | if (!format) { |
| 222 | err = -ENOMEM; |
| 223 | goto out; |
| 224 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 225 | sprintf(format, "%s/%s/format", sys, dent->d_name); |
| 226 | ret = stat(format, &st); |
| 227 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 228 | if (ret >= 0) { |
| 229 | err = record_file(format, 8); |
| 230 | if (err) { |
| 231 | free(format); |
| 232 | goto out; |
| 233 | } |
| 234 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 235 | free(format); |
| 236 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 237 | err = 0; |
| 238 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 239 | closedir(dir); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 240 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 243 | static int record_ftrace_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 244 | { |
| 245 | char *path; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 246 | int ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 247 | |
| 248 | path = get_tracing_file("events/ftrace"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 249 | if (!path) { |
| 250 | pr_debug("can't get tracing/events/ftrace"); |
| 251 | return -ENOMEM; |
| 252 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 253 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 254 | ret = copy_event_system(path, tps); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 255 | |
| 256 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 257 | |
| 258 | return ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 261 | static 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 Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 272 | static int record_event_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 273 | { |
| 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 Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 281 | int err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 282 | |
| 283 | path = get_tracing_file("events"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 284 | if (!path) { |
| 285 | pr_debug("can't get tracing/events"); |
| 286 | return -ENOMEM; |
| 287 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 288 | |
| 289 | dir = opendir(path); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 290 | if (!dir) { |
| 291 | err = -errno; |
| 292 | pr_debug("can't read directory '%s'", path); |
| 293 | goto out; |
| 294 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 295 | |
| 296 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 297 | if (dent->d_type != DT_DIR || |
| 298 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 299 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 300 | strcmp(dent->d_name, "ftrace") == 0 || |
| 301 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 302 | continue; |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 303 | count++; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 306 | if (write(output_fd, &count, 4) != 4) { |
| 307 | err = -EIO; |
| 308 | pr_debug("can't write count\n"); |
| 309 | goto out; |
| 310 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 311 | |
| 312 | rewinddir(dir); |
| 313 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 314 | if (dent->d_type != DT_DIR || |
| 315 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 316 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 317 | strcmp(dent->d_name, "ftrace") == 0 || |
| 318 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 319 | continue; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 320 | sys = malloc(strlen(path) + strlen(dent->d_name) + 2); |
| 321 | if (!sys) { |
| 322 | err = -ENOMEM; |
| 323 | goto out; |
| 324 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 325 | sprintf(sys, "%s/%s", path, dent->d_name); |
| 326 | ret = stat(sys, &st); |
| 327 | if (ret >= 0) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 328 | 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 Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 336 | } |
| 337 | free(sys); |
| 338 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 339 | err = 0; |
| 340 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 341 | closedir(dir); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 342 | put_tracing_file(path); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 343 | |
| 344 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 347 | static int record_proc_kallsyms(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 348 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 349 | unsigned int size; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 350 | const char *path = "/proc/kallsyms"; |
| 351 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 352 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 353 | |
| 354 | ret = stat(path, &st); |
| 355 | if (ret < 0) { |
| 356 | /* not found */ |
| 357 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 358 | if (write(output_fd, &size, 4) != 4) |
| 359 | err = -EIO; |
| 360 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 361 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 362 | return record_file(path, 4); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 365 | static int record_ftrace_printk(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 366 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 367 | unsigned int size; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 368 | char *path; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 369 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 370 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 371 | |
| 372 | path = get_tracing_file("printk_formats"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 373 | if (!path) { |
| 374 | pr_debug("can't get tracing/printk_formats"); |
| 375 | return -ENOMEM; |
| 376 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 377 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 378 | ret = stat(path, &st); |
| 379 | if (ret < 0) { |
| 380 | /* not found */ |
| 381 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 382 | if (write(output_fd, &size, 4) != 4) |
| 383 | err = -EIO; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 384 | goto out; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 385 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 386 | err = record_file(path, 4); |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 387 | |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 388 | out: |
| 389 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 390 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 393 | static void |
| 394 | put_tracepoints_path(struct tracepoint_path *tps) |
| 395 | { |
| 396 | while (tps) { |
| 397 | struct tracepoint_path *t = tps; |
| 398 | |
| 399 | tps = tps->next; |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 400 | zfree(&t->name); |
| 401 | zfree(&t->system); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 402 | free(t); |
| 403 | } |
| 404 | } |
| 405 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 406 | static struct tracepoint_path * |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 407 | get_tracepoints_path(struct list_head *pattrs) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 408 | { |
| 409 | struct tracepoint_path path, *ppath = &path; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 410 | struct perf_evsel *pos; |
| 411 | int nr_tracepoints = 0; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 412 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 413 | list_for_each_entry(pos, pattrs, node) { |
| 414 | if (pos->attr.type != PERF_TYPE_TRACEPOINT) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 415 | continue; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 416 | ++nr_tracepoints; |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 417 | |
| 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 | |
| 429 | try_id: |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 430 | ppath->next = tracepoint_id_to_path(pos->attr.config); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 431 | if (!ppath->next) { |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 432 | error: |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 433 | pr_debug("No memory to alloc tracepoints list\n"); |
| 434 | put_tracepoints_path(&path); |
| 435 | return NULL; |
| 436 | } |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 437 | next: |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 438 | ppath = ppath->next; |
| 439 | } |
| 440 | |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 441 | return nr_tracepoints > 0 ? path.next : NULL; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 442 | } |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 443 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 444 | bool have_tracepoints(struct list_head *pattrs) |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 445 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 446 | struct perf_evsel *pos; |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 447 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 448 | list_for_each_entry(pos, pattrs, node) |
| 449 | if (pos->attr.type == PERF_TYPE_TRACEPOINT) |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 450 | return true; |
| 451 | |
| 452 | return false; |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 453 | } |
| 454 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 455 | static int tracing_data_header(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 456 | { |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 457 | char buf[20]; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 458 | ssize_t size; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 459 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 460 | /* just guessing this is someone's birthday.. ;) */ |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 461 | buf[0] = 23; |
| 462 | buf[1] = 8; |
| 463 | buf[2] = 68; |
| 464 | memcpy(buf + 3, "tracing", 7); |
| 465 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 466 | if (write(output_fd, buf, 10) != 10) |
| 467 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 468 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 469 | size = strlen(VERSION) + 1; |
| 470 | if (write(output_fd, VERSION, size) != size) |
| 471 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 472 | |
| 473 | /* save endian */ |
| 474 | if (bigendian()) |
| 475 | buf[0] = 1; |
| 476 | else |
| 477 | buf[0] = 0; |
| 478 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 479 | if (write(output_fd, buf, 1) != 1) |
| 480 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 481 | |
| 482 | /* save size of long */ |
| 483 | buf[0] = sizeof(long); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 484 | if (write(output_fd, buf, 1) != 1) |
| 485 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 486 | |
| 487 | /* save page_size */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 488 | if (write(output_fd, &page_size, 4) != 4) |
| 489 | return -1; |
| 490 | |
| 491 | return 0; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 492 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 493 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 494 | struct 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 499 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 500 | |
| 501 | output_fd = fd; |
| 502 | |
| 503 | tps = get_tracepoints_path(pattrs); |
| 504 | if (!tps) |
| 505 | return NULL; |
| 506 | |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 507 | tdata = malloc(sizeof(*tdata)); |
| 508 | if (!tdata) |
| 509 | return NULL; |
| 510 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 511 | 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 Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 519 | if (!mkstemp(tdata->temp_file)) { |
| 520 | pr_debug("Can't make temp file"); |
| 521 | return NULL; |
| 522 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 523 | |
| 524 | temp_fd = open(tdata->temp_file, O_RDWR); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 525 | if (temp_fd < 0) { |
| 526 | pr_debug("Can't read '%s'", tdata->temp_file); |
| 527 | return NULL; |
| 528 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 529 | |
| 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 537 | err = tracing_data_header(); |
| 538 | if (err) |
| 539 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 540 | err = record_header_files(); |
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_ftrace_files(tps); |
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_event_files(tps); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 547 | if (err) |
| 548 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 549 | err = record_proc_kallsyms(); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 550 | if (err) |
| 551 | goto out; |
Namhyung Kim | 077f159 | 2013-06-04 14:20:29 +0900 | [diff] [blame] | 552 | err = record_ftrace_printk(); |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 553 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 554 | out: |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 555 | /* |
| 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 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 565 | if (err) |
| 566 | zfree(&tdata); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 567 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 568 | put_tracepoints_path(tps); |
| 569 | return tdata; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 570 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 571 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 572 | int tracing_data_put(struct tracing_data *tdata) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 573 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 574 | int err = 0; |
| 575 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 576 | if (tdata->temp) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 577 | err = record_file(tdata->temp_file, 0); |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 578 | unlink(tdata->temp_file); |
| 579 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 580 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 581 | free(tdata); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 582 | return err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 583 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 584 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 585 | int read_tracing_data(int fd, struct list_head *pattrs) |
| 586 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 587 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 588 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 589 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 590 | /* |
| 591 | * We work over the real file, so we can write data |
| 592 | * directly, no temp file is needed. |
| 593 | */ |
| 594 | tdata = tracing_data_get(pattrs, fd, false); |
| 595 | if (!tdata) |
| 596 | return -ENOMEM; |
| 597 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 598 | err = tracing_data_put(tdata); |
| 599 | return err; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 600 | } |