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