Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | |
| 3 | #include "util/util.h" |
| 4 | #include "util/cache.h" |
| 5 | #include "util/symbol.h" |
| 6 | #include "util/thread.h" |
| 7 | #include "util/header.h" |
Ingo Molnar | cf72344 | 2009-11-28 10:11:00 +0100 | [diff] [blame] | 8 | #include "util/exec_cmd.h" |
| 9 | #include "util/trace-event.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 10 | #include "util/session.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 11 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 12 | static char const *script_name; |
| 13 | static char const *generate_script_lang; |
| 14 | |
| 15 | static int default_start_script(const char *script __attribute((unused))) |
| 16 | { |
| 17 | return 0; |
| 18 | } |
| 19 | |
| 20 | static int default_stop_script(void) |
| 21 | { |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int default_generate_script(const char *outfile __attribute ((unused))) |
| 26 | { |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static struct scripting_ops default_scripting_ops = { |
| 31 | .start_script = default_start_script, |
| 32 | .stop_script = default_stop_script, |
| 33 | .process_event = print_event, |
| 34 | .generate_script = default_generate_script, |
| 35 | }; |
| 36 | |
| 37 | static struct scripting_ops *scripting_ops; |
| 38 | |
| 39 | static void setup_scripting(void) |
| 40 | { |
| 41 | /* make sure PERF_EXEC_PATH is set for scripts */ |
| 42 | perf_set_argv_exec_path(perf_exec_path()); |
| 43 | |
Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 44 | setup_perl_scripting(); |
| 45 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 46 | scripting_ops = &default_scripting_ops; |
| 47 | } |
| 48 | |
| 49 | static int cleanup_scripting(void) |
| 50 | { |
| 51 | return scripting_ops->stop_script(); |
| 52 | } |
| 53 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 54 | #include "util/parse-options.h" |
| 55 | |
| 56 | #include "perf.h" |
| 57 | #include "util/debug.h" |
| 58 | |
| 59 | #include "util/trace-event.h" |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 60 | #include "util/exec_cmd.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 61 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 62 | static char const *input_name = "perf.data"; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 63 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 64 | static u64 sample_type; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 66 | static int process_sample_event(event_t *event, struct perf_session *session __used) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 67 | { |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 68 | struct sample_data data; |
| 69 | struct thread *thread; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 70 | |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 71 | memset(&data, 0, sizeof(data)); |
| 72 | data.time = -1; |
| 73 | data.cpu = -1; |
| 74 | data.period = 1; |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 75 | |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 76 | event__parse_sample(event, sample_type, &data); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 77 | |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 78 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 79 | event->header.misc, |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 80 | data.pid, data.tid, |
| 81 | (void *)(long)data.ip, |
| 82 | (long long)data.period); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 83 | |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 84 | thread = threads__findnew(event->ip.pid); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 85 | if (thread == NULL) { |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 86 | pr_debug("problem processing %d event, skipping it.\n", |
| 87 | event->header.type); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 88 | return -1; |
| 89 | } |
| 90 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 91 | if (sample_type & PERF_SAMPLE_RAW) { |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 92 | /* |
| 93 | * FIXME: better resolve from pid from the struct trace_entry |
| 94 | * field, although it should be the same than this perf |
| 95 | * event pid |
| 96 | */ |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 97 | scripting_ops->process_event(data.cpu, data.raw_data, |
| 98 | data.raw_size, |
| 99 | data.time, thread->comm); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 100 | } |
OGAWA Hirofumi | 180f95e | 2009-12-06 20:08:24 +0900 | [diff] [blame] | 101 | event__stats.total += data.period; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 106 | static int sample_type_check(u64 type) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 107 | { |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 108 | sample_type = type; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 109 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 110 | if (!(sample_type & PERF_SAMPLE_RAW)) { |
| 111 | fprintf(stderr, |
| 112 | "No trace sample to read. Did you call perf record " |
| 113 | "without -R?"); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 120 | static struct perf_event_ops event_ops = { |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 121 | .process_sample_event = process_sample_event, |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 122 | .process_comm_event = event__process_comm, |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 123 | .sample_type_check = sample_type_check, |
| 124 | }; |
| 125 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 126 | static int __cmd_trace(struct perf_session *session) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 127 | { |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 128 | return perf_session__process_events(session, &event_ops, 0, |
| 129 | &event__cwdlen, &event__cwd); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 132 | struct script_spec { |
| 133 | struct list_head node; |
| 134 | struct scripting_ops *ops; |
| 135 | char spec[0]; |
| 136 | }; |
| 137 | |
| 138 | LIST_HEAD(script_specs); |
| 139 | |
| 140 | static struct script_spec *script_spec__new(const char *spec, |
| 141 | struct scripting_ops *ops) |
| 142 | { |
| 143 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 144 | |
| 145 | if (s != NULL) { |
| 146 | strcpy(s->spec, spec); |
| 147 | s->ops = ops; |
| 148 | } |
| 149 | |
| 150 | return s; |
| 151 | } |
| 152 | |
| 153 | static void script_spec__delete(struct script_spec *s) |
| 154 | { |
| 155 | free(s->spec); |
| 156 | free(s); |
| 157 | } |
| 158 | |
| 159 | static void script_spec__add(struct script_spec *s) |
| 160 | { |
| 161 | list_add_tail(&s->node, &script_specs); |
| 162 | } |
| 163 | |
| 164 | static struct script_spec *script_spec__find(const char *spec) |
| 165 | { |
| 166 | struct script_spec *s; |
| 167 | |
| 168 | list_for_each_entry(s, &script_specs, node) |
| 169 | if (strcasecmp(s->spec, spec) == 0) |
| 170 | return s; |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | static struct script_spec *script_spec__findnew(const char *spec, |
| 175 | struct scripting_ops *ops) |
| 176 | { |
| 177 | struct script_spec *s = script_spec__find(spec); |
| 178 | |
| 179 | if (s) |
| 180 | return s; |
| 181 | |
| 182 | s = script_spec__new(spec, ops); |
| 183 | if (!s) |
| 184 | goto out_delete_spec; |
| 185 | |
| 186 | script_spec__add(s); |
| 187 | |
| 188 | return s; |
| 189 | |
| 190 | out_delete_spec: |
| 191 | script_spec__delete(s); |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 197 | { |
| 198 | struct script_spec *s; |
| 199 | |
| 200 | s = script_spec__find(spec); |
| 201 | if (s) |
| 202 | return -1; |
| 203 | |
| 204 | s = script_spec__findnew(spec, ops); |
| 205 | if (!s) |
| 206 | return -1; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct scripting_ops *script_spec__lookup(const char *spec) |
| 212 | { |
| 213 | struct script_spec *s = script_spec__find(spec); |
| 214 | if (!s) |
| 215 | return NULL; |
| 216 | |
| 217 | return s->ops; |
| 218 | } |
| 219 | |
| 220 | static void list_available_languages(void) |
| 221 | { |
| 222 | struct script_spec *s; |
| 223 | |
| 224 | fprintf(stderr, "\n"); |
| 225 | fprintf(stderr, "Scripting language extensions (used in " |
| 226 | "perf trace -s [spec:]script.[spec]):\n\n"); |
| 227 | |
| 228 | list_for_each_entry(s, &script_specs, node) |
| 229 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); |
| 230 | |
| 231 | fprintf(stderr, "\n"); |
| 232 | } |
| 233 | |
| 234 | static int parse_scriptname(const struct option *opt __used, |
| 235 | const char *str, int unset __used) |
| 236 | { |
| 237 | char spec[PATH_MAX]; |
| 238 | const char *script, *ext; |
| 239 | int len; |
| 240 | |
| 241 | if (strcmp(str, "list") == 0) { |
| 242 | list_available_languages(); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | script = strchr(str, ':'); |
| 247 | if (script) { |
| 248 | len = script - str; |
| 249 | if (len >= PATH_MAX) { |
| 250 | fprintf(stderr, "invalid language specifier"); |
| 251 | return -1; |
| 252 | } |
| 253 | strncpy(spec, str, len); |
| 254 | spec[len] = '\0'; |
| 255 | scripting_ops = script_spec__lookup(spec); |
| 256 | if (!scripting_ops) { |
| 257 | fprintf(stderr, "invalid language specifier"); |
| 258 | return -1; |
| 259 | } |
| 260 | script++; |
| 261 | } else { |
| 262 | script = str; |
| 263 | ext = strchr(script, '.'); |
| 264 | if (!ext) { |
| 265 | fprintf(stderr, "invalid script extension"); |
| 266 | return -1; |
| 267 | } |
| 268 | scripting_ops = script_spec__lookup(++ext); |
| 269 | if (!scripting_ops) { |
| 270 | fprintf(stderr, "invalid script extension"); |
| 271 | return -1; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | script_name = strdup(script); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 280 | static const char * const annotate_usage[] = { |
| 281 | "perf trace [<options>] <command>", |
| 282 | NULL |
| 283 | }; |
| 284 | |
| 285 | static const struct option options[] = { |
| 286 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 287 | "dump raw trace in ASCII"), |
| 288 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 289 | "be more verbose (show symbol address, etc)"), |
Steven Rostedt | cda4846 | 2009-10-14 15:43:42 -0400 | [diff] [blame] | 290 | OPT_BOOLEAN('l', "latency", &latency_format, |
| 291 | "show latency attributes (irqs/preemption disabled, etc)"), |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 292 | OPT_CALLBACK('s', "script", NULL, "name", |
| 293 | "script file name (lang:script name, script name, or *)", |
| 294 | parse_scriptname), |
| 295 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", |
| 296 | "generate perf-trace.xx script in specified language"), |
| 297 | |
Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 298 | OPT_END() |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
| 302 | { |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 303 | int err; |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 304 | struct perf_session *session; |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 305 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 306 | symbol__init(0); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 307 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 308 | setup_scripting(); |
| 309 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 310 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 311 | if (argc) { |
| 312 | /* |
| 313 | * Special case: if there's an argument left then assume tha |
| 314 | * it's a symbol filter: |
| 315 | */ |
| 316 | if (argc > 1) |
| 317 | usage_with_options(annotate_usage, options); |
| 318 | } |
| 319 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 320 | setup_pager(); |
| 321 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 322 | session = perf_session__new(input_name, O_RDONLY, 0); |
| 323 | if (session == NULL) |
| 324 | return -ENOMEM; |
| 325 | |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 326 | if (generate_script_lang) { |
| 327 | struct stat perf_stat; |
| 328 | |
| 329 | int input = open(input_name, O_RDONLY); |
| 330 | if (input < 0) { |
| 331 | perror("failed to open file"); |
| 332 | exit(-1); |
| 333 | } |
| 334 | |
| 335 | err = fstat(input, &perf_stat); |
| 336 | if (err < 0) { |
| 337 | perror("failed to stat file"); |
| 338 | exit(-1); |
| 339 | } |
| 340 | |
| 341 | if (!perf_stat.st_size) { |
| 342 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 343 | exit(0); |
| 344 | } |
| 345 | |
| 346 | scripting_ops = script_spec__lookup(generate_script_lang); |
| 347 | if (!scripting_ops) { |
| 348 | fprintf(stderr, "invalid language specifier"); |
| 349 | return -1; |
| 350 | } |
| 351 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 352 | perf_header__read(&session->header, input); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 353 | err = scripting_ops->generate_script("perf-trace"); |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | if (script_name) { |
| 358 | err = scripting_ops->start_script(script_name); |
| 359 | if (err) |
| 360 | goto out; |
| 361 | } |
| 362 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 363 | err = __cmd_trace(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 364 | |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 365 | perf_session__delete(session); |
Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 366 | cleanup_scripting(); |
| 367 | out: |
| 368 | return err; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 369 | } |