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