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