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