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 | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 41 | #include <lk/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 | static const char *find_debugfs(void) |
| 50 | { |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 51 | const char *path = perf_debugfs_mount(NULL); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 52 | |
Xiao Guangrong | 61be3e5 | 2009-12-28 16:48:30 +0800 | [diff] [blame] | 53 | if (!path) |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 54 | pr_debug("Your kernel does not support the debugfs filesystem"); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 55 | |
Xiao Guangrong | 61be3e5 | 2009-12-28 16:48:30 +0800 | [diff] [blame] | 56 | return path; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Finds the path to the debugfs/tracing |
| 61 | * Allocates the string and stores it. |
| 62 | */ |
| 63 | static 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 Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 73 | if (!debugfs) |
| 74 | return NULL; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 75 | |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 76 | tracing = malloc(strlen(debugfs) + 9); |
| 77 | if (!tracing) |
| 78 | return NULL; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 79 | |
| 80 | sprintf(tracing, "%s/tracing", debugfs); |
| 81 | |
| 82 | tracing_found = 1; |
| 83 | return tracing; |
| 84 | } |
| 85 | |
| 86 | static 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 Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 95 | file = malloc(strlen(tracing) + strlen(name) + 2); |
| 96 | if (!file) |
| 97 | return NULL; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 98 | |
| 99 | sprintf(file, "%s/%s", tracing, name); |
| 100 | return file; |
| 101 | } |
| 102 | |
| 103 | static void put_tracing_file(char *file) |
| 104 | { |
| 105 | free(file); |
| 106 | } |
| 107 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 108 | int bigendian(void) |
| 109 | { |
| 110 | unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0}; |
| 111 | unsigned int *ptr; |
| 112 | |
Ingo Molnar | 65014ab | 2009-09-02 14:55:55 +0200 | [diff] [blame] | 113 | ptr = (unsigned int *)(void *)str; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 114 | return *ptr == 0x01020304; |
| 115 | } |
| 116 | |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 117 | /* unfortunately, you can not stat debugfs or proc files for size */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 118 | static int record_file(const char *file, ssize_t hdr_sz) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 119 | { |
| 120 | unsigned long long size = 0; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 121 | char buf[BUFSIZ], *sizep; |
| 122 | off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); |
| 123 | int r, fd; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 124 | int err = -EIO; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 125 | |
| 126 | fd = open(file, O_RDONLY); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 127 | if (fd < 0) { |
| 128 | pr_debug("Can't read '%s'", file); |
| 129 | return -errno; |
| 130 | } |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 131 | |
| 132 | /* put in zeros for file size, then fill true size later */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 133 | if (hdr_sz) { |
| 134 | if (write(output_fd, &size, hdr_sz) != hdr_sz) |
| 135 | goto out; |
| 136 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 137 | |
| 138 | do { |
| 139 | r = read(fd, buf, BUFSIZ); |
| 140 | if (r > 0) { |
| 141 | size += r; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 142 | if (write(output_fd, buf, r) != r) |
| 143 | goto out; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 144 | } |
| 145 | } while (r > 0); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 146 | |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 147 | /* ugh, handle big-endian hdr_size == 4 */ |
| 148 | sizep = (char*)&size; |
| 149 | if (bigendian()) |
| 150 | sizep += sizeof(u64) - hdr_sz; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 151 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 152 | 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 156 | |
| 157 | err = 0; |
| 158 | out: |
| 159 | close(fd); |
| 160 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 163 | static int read_header_files(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 164 | { |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 165 | char *path; |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 166 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 167 | int err = -EIO; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 168 | |
| 169 | path = get_tracing_file("events/header_page"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 170 | if (!path) { |
| 171 | pr_debug("can't get tracing/events/header_page"); |
| 172 | return -ENOMEM; |
| 173 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 174 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 175 | if (stat(path, &st) < 0) { |
| 176 | pr_debug("can't read '%s'", path); |
| 177 | goto out; |
| 178 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 179 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 180 | 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 Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 190 | put_tracing_file(path); |
| 191 | |
| 192 | path = get_tracing_file("events/header_event"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 193 | if (!path) { |
| 194 | pr_debug("can't get tracing/events/header_event"); |
| 195 | err = -ENOMEM; |
| 196 | goto out; |
| 197 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 198 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 199 | if (stat(path, &st) < 0) { |
| 200 | pr_debug("can't read '%s'", path); |
| 201 | goto out; |
| 202 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 203 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 204 | 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; |
| 215 | out: |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 216 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 217 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 220 | static 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 Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 231 | static int copy_event_system(const char *sys, struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 232 | { |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 233 | struct dirent *dent; |
| 234 | struct stat st; |
| 235 | char *format; |
| 236 | DIR *dir; |
| 237 | int count = 0; |
| 238 | int ret; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 239 | int err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 240 | |
| 241 | dir = opendir(sys); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 242 | if (!dir) { |
| 243 | pr_debug("can't read directory '%s'", sys); |
| 244 | return -errno; |
| 245 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 246 | |
| 247 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 248 | if (dent->d_type != DT_DIR || |
| 249 | strcmp(dent->d_name, ".") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 250 | strcmp(dent->d_name, "..") == 0 || |
| 251 | !name_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 252 | continue; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 253 | format = malloc(strlen(sys) + strlen(dent->d_name) + 10); |
| 254 | if (!format) { |
| 255 | err = -ENOMEM; |
| 256 | goto out; |
| 257 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 258 | 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 266 | if (write(output_fd, &count, 4) != 4) { |
| 267 | err = -EIO; |
| 268 | pr_debug("can't write count\n"); |
| 269 | goto out; |
| 270 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 271 | |
| 272 | rewinddir(dir); |
| 273 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 274 | if (dent->d_type != DT_DIR || |
| 275 | strcmp(dent->d_name, ".") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 276 | strcmp(dent->d_name, "..") == 0 || |
| 277 | !name_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 278 | continue; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 279 | format = malloc(strlen(sys) + strlen(dent->d_name) + 10); |
| 280 | if (!format) { |
| 281 | err = -ENOMEM; |
| 282 | goto out; |
| 283 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 284 | sprintf(format, "%s/%s/format", sys, dent->d_name); |
| 285 | ret = stat(format, &st); |
| 286 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 287 | if (ret >= 0) { |
| 288 | err = record_file(format, 8); |
| 289 | if (err) { |
| 290 | free(format); |
| 291 | goto out; |
| 292 | } |
| 293 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 294 | free(format); |
| 295 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 296 | err = 0; |
| 297 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 298 | closedir(dir); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 299 | return err; |
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 | static int read_ftrace_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 303 | { |
| 304 | char *path; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 305 | int ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 306 | |
| 307 | path = get_tracing_file("events/ftrace"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 308 | if (!path) { |
| 309 | pr_debug("can't get tracing/events/ftrace"); |
| 310 | return -ENOMEM; |
| 311 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 312 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 313 | ret = copy_event_system(path, tps); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 314 | |
| 315 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 316 | |
| 317 | return ret; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 320 | static 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 Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 331 | static int read_event_files(struct tracepoint_path *tps) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 332 | { |
| 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 Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 340 | int err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 341 | |
| 342 | path = get_tracing_file("events"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 343 | if (!path) { |
| 344 | pr_debug("can't get tracing/events"); |
| 345 | return -ENOMEM; |
| 346 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 347 | |
| 348 | dir = opendir(path); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 349 | if (!dir) { |
| 350 | err = -errno; |
| 351 | pr_debug("can't read directory '%s'", path); |
| 352 | goto out; |
| 353 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 354 | |
| 355 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 356 | if (dent->d_type != DT_DIR || |
| 357 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 358 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 359 | strcmp(dent->d_name, "ftrace") == 0 || |
| 360 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 361 | continue; |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 362 | count++; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 365 | if (write(output_fd, &count, 4) != 4) { |
| 366 | err = -EIO; |
| 367 | pr_debug("can't write count\n"); |
| 368 | goto out; |
| 369 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 370 | |
| 371 | rewinddir(dir); |
| 372 | while ((dent = readdir(dir))) { |
Ulrich Drepper | 659d8cf | 2009-12-19 16:40:28 -0500 | [diff] [blame] | 373 | if (dent->d_type != DT_DIR || |
| 374 | strcmp(dent->d_name, ".") == 0 || |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 375 | strcmp(dent->d_name, "..") == 0 || |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 376 | strcmp(dent->d_name, "ftrace") == 0 || |
| 377 | !system_in_tp_list(dent->d_name, tps)) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 378 | continue; |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 379 | sys = malloc(strlen(path) + strlen(dent->d_name) + 2); |
| 380 | if (!sys) { |
| 381 | err = -ENOMEM; |
| 382 | goto out; |
| 383 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 384 | sprintf(sys, "%s/%s", path, dent->d_name); |
| 385 | ret = stat(sys, &st); |
| 386 | if (ret >= 0) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 387 | 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 Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 395 | } |
| 396 | free(sys); |
| 397 | } |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 398 | err = 0; |
| 399 | out: |
Xiao Guangrong | 9967411 | 2009-12-28 16:49:38 +0800 | [diff] [blame] | 400 | closedir(dir); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 401 | put_tracing_file(path); |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 402 | |
| 403 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 406 | static int read_proc_kallsyms(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 407 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 408 | unsigned int size; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 409 | const char *path = "/proc/kallsyms"; |
| 410 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 411 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 412 | |
| 413 | ret = stat(path, &st); |
| 414 | if (ret < 0) { |
| 415 | /* not found */ |
| 416 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 417 | if (write(output_fd, &size, 4) != 4) |
| 418 | err = -EIO; |
| 419 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 420 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 421 | return record_file(path, 4); |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 424 | static int read_ftrace_printk(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 425 | { |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 426 | unsigned int size; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 427 | char *path; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 428 | struct stat st; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 429 | int ret, err = 0; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 430 | |
| 431 | path = get_tracing_file("printk_formats"); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 432 | if (!path) { |
| 433 | pr_debug("can't get tracing/printk_formats"); |
| 434 | return -ENOMEM; |
| 435 | } |
Namhyung Kim | 454f8c7 | 2013-03-21 16:18:44 +0900 | [diff] [blame] | 436 | |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 437 | ret = stat(path, &st); |
| 438 | if (ret < 0) { |
| 439 | /* not found */ |
| 440 | size = 0; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 441 | if (write(output_fd, &size, 4) != 4) |
| 442 | err = -EIO; |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 443 | goto out; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 444 | } |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 445 | err = record_file(path, 4); |
Sonny Rao | 259032b | 2011-07-14 13:34:43 +1000 | [diff] [blame] | 446 | |
Li Zefan | 6706ccf | 2009-09-17 16:34:23 +0800 | [diff] [blame] | 447 | out: |
| 448 | put_tracing_file(path); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 449 | return err; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 452 | static void |
| 453 | put_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 Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 465 | static struct tracepoint_path * |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 466 | get_tracepoints_path(struct list_head *pattrs) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 467 | { |
| 468 | struct tracepoint_path path, *ppath = &path; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 469 | struct perf_evsel *pos; |
| 470 | int nr_tracepoints = 0; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 471 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 472 | list_for_each_entry(pos, pattrs, node) { |
| 473 | if (pos->attr.type != PERF_TYPE_TRACEPOINT) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 474 | continue; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 475 | ++nr_tracepoints; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 476 | ppath->next = tracepoint_id_to_path(pos->attr.config); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 477 | if (!ppath->next) { |
| 478 | pr_debug("No memory to alloc tracepoints list\n"); |
| 479 | put_tracepoints_path(&path); |
| 480 | return NULL; |
| 481 | } |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 482 | ppath = ppath->next; |
| 483 | } |
| 484 | |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 485 | return nr_tracepoints > 0 ? path.next : NULL; |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 486 | } |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 487 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 488 | bool have_tracepoints(struct list_head *pattrs) |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 489 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 490 | struct perf_evsel *pos; |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 491 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 492 | list_for_each_entry(pos, pattrs, node) |
| 493 | if (pos->attr.type == PERF_TYPE_TRACEPOINT) |
Tom Zanussi | db620b1 | 2010-05-04 22:20:16 -0500 | [diff] [blame] | 494 | return true; |
| 495 | |
| 496 | return false; |
Tom Zanussi | 63e0c77 | 2010-05-03 00:14:48 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 499 | static int tracing_data_header(void) |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 500 | { |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 501 | char buf[20]; |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 502 | ssize_t size; |
Arnaldo Carvalho de Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 503 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 504 | /* just guessing this is someone's birthday.. ;) */ |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 505 | buf[0] = 23; |
| 506 | buf[1] = 8; |
| 507 | buf[2] = 68; |
| 508 | memcpy(buf + 3, "tracing", 7); |
| 509 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 510 | if (write(output_fd, buf, 10) != 10) |
| 511 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 512 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 513 | size = strlen(VERSION) + 1; |
| 514 | if (write(output_fd, VERSION, size) != size) |
| 515 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 516 | |
| 517 | /* save endian */ |
| 518 | if (bigendian()) |
| 519 | buf[0] = 1; |
| 520 | else |
| 521 | buf[0] = 0; |
| 522 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 523 | read_trace_init(buf[0], buf[0]); |
| 524 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 525 | if (write(output_fd, buf, 1) != 1) |
| 526 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 527 | |
| 528 | /* save size of long */ |
| 529 | buf[0] = sizeof(long); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 530 | if (write(output_fd, buf, 1) != 1) |
| 531 | return -1; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 532 | |
| 533 | /* save page_size */ |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 534 | if (write(output_fd, &page_size, 4) != 4) |
| 535 | return -1; |
| 536 | |
| 537 | return 0; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 538 | } |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 539 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 540 | struct 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 545 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 546 | |
| 547 | output_fd = fd; |
| 548 | |
| 549 | tps = get_tracepoints_path(pattrs); |
| 550 | if (!tps) |
| 551 | return NULL; |
| 552 | |
Namhyung Kim | 5a6fd27 | 2013-03-21 16:18:45 +0900 | [diff] [blame] | 553 | tdata = malloc(sizeof(*tdata)); |
| 554 | if (!tdata) |
| 555 | return NULL; |
| 556 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 557 | 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 Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 565 | if (!mkstemp(tdata->temp_file)) { |
| 566 | pr_debug("Can't make temp file"); |
| 567 | return NULL; |
| 568 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 569 | |
| 570 | temp_fd = open(tdata->temp_file, O_RDWR); |
Namhyung Kim | 7f42b95 | 2013-03-21 16:18:47 +0900 | [diff] [blame] | 571 | if (temp_fd < 0) { |
| 572 | pr_debug("Can't read '%s'", tdata->temp_file); |
| 573 | return NULL; |
| 574 | } |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 575 | |
| 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 583 | 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 Melo | e256136 | 2009-11-21 14:31:26 -0200 | [diff] [blame] | 599 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 600 | out: |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 601 | /* |
| 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 611 | if (err) { |
| 612 | free(tdata); |
| 613 | tdata = NULL; |
| 614 | } |
| 615 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 616 | put_tracepoints_path(tps); |
| 617 | return tdata; |
Steven Rostedt | 52050943 | 2009-08-17 16:18:05 +0200 | [diff] [blame] | 618 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 619 | |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 620 | int tracing_data_put(struct tracing_data *tdata) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 621 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 622 | int err = 0; |
| 623 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 624 | if (tdata->temp) { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 625 | err = record_file(tdata->temp_file, 0); |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 626 | unlink(tdata->temp_file); |
| 627 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 628 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 629 | free(tdata); |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 630 | return err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 631 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 632 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 633 | int read_tracing_data(int fd, struct list_head *pattrs) |
| 634 | { |
Namhyung Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 635 | int err; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 636 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 637 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 638 | /* |
| 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 Kim | 8755d5e | 2013-03-21 16:18:46 +0900 | [diff] [blame] | 646 | err = tracing_data_put(tdata); |
| 647 | return err; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 648 | } |