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