]>
Commit | Line | Data |
---|---|---|
5f9c39dc FW |
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" | |
cf72344d IM |
8 | #include "util/exec_cmd.h" |
9 | #include "util/trace-event.h" | |
94c744b6 | 10 | #include "util/session.h" |
5f9c39dc | 11 | |
956ffd02 TZ |
12 | static char const *script_name; |
13 | static char const *generate_script_lang; | |
14 | ||
586bc5cc TZ |
15 | static int default_start_script(const char *script __unused, |
16 | int argc __unused, | |
17 | const char **argv __unused) | |
956ffd02 TZ |
18 | { |
19 | return 0; | |
20 | } | |
21 | ||
22 | static int default_stop_script(void) | |
23 | { | |
24 | return 0; | |
25 | } | |
26 | ||
586bc5cc | 27 | static int default_generate_script(const char *outfile __unused) |
956ffd02 TZ |
28 | { |
29 | return 0; | |
30 | } | |
31 | ||
32 | static struct scripting_ops default_scripting_ops = { | |
33 | .start_script = default_start_script, | |
34 | .stop_script = default_stop_script, | |
35 | .process_event = print_event, | |
36 | .generate_script = default_generate_script, | |
37 | }; | |
38 | ||
39 | static struct scripting_ops *scripting_ops; | |
40 | ||
41 | static void setup_scripting(void) | |
42 | { | |
43 | /* make sure PERF_EXEC_PATH is set for scripts */ | |
44 | perf_set_argv_exec_path(perf_exec_path()); | |
45 | ||
16c632de TZ |
46 | setup_perl_scripting(); |
47 | ||
956ffd02 TZ |
48 | scripting_ops = &default_scripting_ops; |
49 | } | |
50 | ||
51 | static int cleanup_scripting(void) | |
52 | { | |
53 | return scripting_ops->stop_script(); | |
54 | } | |
55 | ||
5f9c39dc FW |
56 | #include "util/parse-options.h" |
57 | ||
58 | #include "perf.h" | |
59 | #include "util/debug.h" | |
60 | ||
61 | #include "util/trace-event.h" | |
956ffd02 | 62 | #include "util/exec_cmd.h" |
5f9c39dc | 63 | |
956ffd02 | 64 | static char const *input_name = "perf.data"; |
5f9c39dc | 65 | |
b3165f41 | 66 | static int process_sample_event(event_t *event, struct perf_session *session) |
5f9c39dc | 67 | { |
180f95e2 OH |
68 | struct sample_data data; |
69 | struct thread *thread; | |
6ddf259d | 70 | |
180f95e2 OH |
71 | memset(&data, 0, sizeof(data)); |
72 | data.time = -1; | |
73 | data.cpu = -1; | |
74 | data.period = 1; | |
cd6feeea | 75 | |
c019879b | 76 | event__parse_sample(event, session->sample_type, &data); |
5f9c39dc | 77 | |
62daacb5 | 78 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
5f9c39dc | 79 | event->header.misc, |
180f95e2 OH |
80 | data.pid, data.tid, |
81 | (void *)(long)data.ip, | |
82 | (long long)data.period); | |
5f9c39dc | 83 | |
b3165f41 | 84 | thread = perf_session__findnew(session, event->ip.pid); |
5f9c39dc | 85 | if (thread == NULL) { |
6beba7ad ACM |
86 | pr_debug("problem processing %d event, skipping it.\n", |
87 | event->header.type); | |
5f9c39dc FW |
88 | return -1; |
89 | } | |
90 | ||
c019879b | 91 | if (session->sample_type & PERF_SAMPLE_RAW) { |
5f9c39dc FW |
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 | */ | |
180f95e2 OH |
97 | scripting_ops->process_event(data.cpu, data.raw_data, |
98 | data.raw_size, | |
99 | data.time, thread->comm); | |
5f9c39dc | 100 | } |
5f9c39dc | 101 | |
f823e441 | 102 | session->events_stats.total += data.period; |
5f9c39dc FW |
103 | return 0; |
104 | } | |
105 | ||
301a0b02 | 106 | static struct perf_event_ops event_ops = { |
016e92fb | 107 | .process_sample_event = process_sample_event, |
62daacb5 | 108 | .process_comm_event = event__process_comm, |
016e92fb FW |
109 | }; |
110 | ||
d8f66248 | 111 | static int __cmd_trace(struct perf_session *session) |
5f9c39dc | 112 | { |
ec913369 | 113 | return perf_session__process_events(session, &event_ops); |
5f9c39dc FW |
114 | } |
115 | ||
956ffd02 TZ |
116 | struct script_spec { |
117 | struct list_head node; | |
118 | struct scripting_ops *ops; | |
119 | char spec[0]; | |
120 | }; | |
121 | ||
122 | LIST_HEAD(script_specs); | |
123 | ||
124 | static struct script_spec *script_spec__new(const char *spec, | |
125 | struct scripting_ops *ops) | |
126 | { | |
127 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | |
128 | ||
129 | if (s != NULL) { | |
130 | strcpy(s->spec, spec); | |
131 | s->ops = ops; | |
132 | } | |
133 | ||
134 | return s; | |
135 | } | |
136 | ||
137 | static void script_spec__delete(struct script_spec *s) | |
138 | { | |
139 | free(s->spec); | |
140 | free(s); | |
141 | } | |
142 | ||
143 | static void script_spec__add(struct script_spec *s) | |
144 | { | |
145 | list_add_tail(&s->node, &script_specs); | |
146 | } | |
147 | ||
148 | static struct script_spec *script_spec__find(const char *spec) | |
149 | { | |
150 | struct script_spec *s; | |
151 | ||
152 | list_for_each_entry(s, &script_specs, node) | |
153 | if (strcasecmp(s->spec, spec) == 0) | |
154 | return s; | |
155 | return NULL; | |
156 | } | |
157 | ||
158 | static struct script_spec *script_spec__findnew(const char *spec, | |
159 | struct scripting_ops *ops) | |
160 | { | |
161 | struct script_spec *s = script_spec__find(spec); | |
162 | ||
163 | if (s) | |
164 | return s; | |
165 | ||
166 | s = script_spec__new(spec, ops); | |
167 | if (!s) | |
168 | goto out_delete_spec; | |
169 | ||
170 | script_spec__add(s); | |
171 | ||
172 | return s; | |
173 | ||
174 | out_delete_spec: | |
175 | script_spec__delete(s); | |
176 | ||
177 | return NULL; | |
178 | } | |
179 | ||
180 | int script_spec_register(const char *spec, struct scripting_ops *ops) | |
181 | { | |
182 | struct script_spec *s; | |
183 | ||
184 | s = script_spec__find(spec); | |
185 | if (s) | |
186 | return -1; | |
187 | ||
188 | s = script_spec__findnew(spec, ops); | |
189 | if (!s) | |
190 | return -1; | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | static struct scripting_ops *script_spec__lookup(const char *spec) | |
196 | { | |
197 | struct script_spec *s = script_spec__find(spec); | |
198 | if (!s) | |
199 | return NULL; | |
200 | ||
201 | return s->ops; | |
202 | } | |
203 | ||
204 | static void list_available_languages(void) | |
205 | { | |
206 | struct script_spec *s; | |
207 | ||
208 | fprintf(stderr, "\n"); | |
209 | fprintf(stderr, "Scripting language extensions (used in " | |
210 | "perf trace -s [spec:]script.[spec]):\n\n"); | |
211 | ||
212 | list_for_each_entry(s, &script_specs, node) | |
213 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | |
214 | ||
215 | fprintf(stderr, "\n"); | |
216 | } | |
217 | ||
218 | static int parse_scriptname(const struct option *opt __used, | |
219 | const char *str, int unset __used) | |
220 | { | |
221 | char spec[PATH_MAX]; | |
222 | const char *script, *ext; | |
223 | int len; | |
224 | ||
225 | if (strcmp(str, "list") == 0) { | |
226 | list_available_languages(); | |
227 | return 0; | |
228 | } | |
229 | ||
230 | script = strchr(str, ':'); | |
231 | if (script) { | |
232 | len = script - str; | |
233 | if (len >= PATH_MAX) { | |
234 | fprintf(stderr, "invalid language specifier"); | |
235 | return -1; | |
236 | } | |
237 | strncpy(spec, str, len); | |
238 | spec[len] = '\0'; | |
239 | scripting_ops = script_spec__lookup(spec); | |
240 | if (!scripting_ops) { | |
241 | fprintf(stderr, "invalid language specifier"); | |
242 | return -1; | |
243 | } | |
244 | script++; | |
245 | } else { | |
246 | script = str; | |
247 | ext = strchr(script, '.'); | |
248 | if (!ext) { | |
249 | fprintf(stderr, "invalid script extension"); | |
250 | return -1; | |
251 | } | |
252 | scripting_ops = script_spec__lookup(++ext); | |
253 | if (!scripting_ops) { | |
254 | fprintf(stderr, "invalid script extension"); | |
255 | return -1; | |
256 | } | |
257 | } | |
258 | ||
259 | script_name = strdup(script); | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
4b9c0c59 TZ |
264 | #define for_each_lang(scripts_dir, lang_dirent, lang_next) \ |
265 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ | |
266 | lang_next) \ | |
267 | if (lang_dirent.d_type == DT_DIR && \ | |
268 | (strcmp(lang_dirent.d_name, ".")) && \ | |
269 | (strcmp(lang_dirent.d_name, ".."))) | |
270 | ||
271 | #define for_each_script(lang_dir, script_dirent, script_next) \ | |
272 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ | |
273 | script_next) \ | |
274 | if (script_dirent.d_type != DT_DIR) | |
275 | ||
276 | ||
277 | #define RECORD_SUFFIX "-record" | |
278 | #define REPORT_SUFFIX "-report" | |
279 | ||
280 | struct script_desc { | |
281 | struct list_head node; | |
282 | char *name; | |
283 | char *half_liner; | |
284 | char *args; | |
285 | }; | |
286 | ||
287 | LIST_HEAD(script_descs); | |
288 | ||
289 | static struct script_desc *script_desc__new(const char *name) | |
290 | { | |
291 | struct script_desc *s = zalloc(sizeof(*s)); | |
292 | ||
293 | if (s != NULL) | |
294 | s->name = strdup(name); | |
295 | ||
296 | return s; | |
297 | } | |
298 | ||
299 | static void script_desc__delete(struct script_desc *s) | |
300 | { | |
301 | free(s->name); | |
302 | free(s); | |
303 | } | |
304 | ||
305 | static void script_desc__add(struct script_desc *s) | |
306 | { | |
307 | list_add_tail(&s->node, &script_descs); | |
308 | } | |
309 | ||
310 | static struct script_desc *script_desc__find(const char *name) | |
311 | { | |
312 | struct script_desc *s; | |
313 | ||
314 | list_for_each_entry(s, &script_descs, node) | |
315 | if (strcasecmp(s->name, name) == 0) | |
316 | return s; | |
317 | return NULL; | |
318 | } | |
319 | ||
320 | static struct script_desc *script_desc__findnew(const char *name) | |
321 | { | |
322 | struct script_desc *s = script_desc__find(name); | |
323 | ||
324 | if (s) | |
325 | return s; | |
326 | ||
327 | s = script_desc__new(name); | |
328 | if (!s) | |
329 | goto out_delete_desc; | |
330 | ||
331 | script_desc__add(s); | |
332 | ||
333 | return s; | |
334 | ||
335 | out_delete_desc: | |
336 | script_desc__delete(s); | |
337 | ||
338 | return NULL; | |
339 | } | |
340 | ||
341 | static char *ends_with(char *str, const char *suffix) | |
342 | { | |
343 | size_t suffix_len = strlen(suffix); | |
344 | char *p = str; | |
345 | ||
346 | if (strlen(str) > suffix_len) { | |
347 | p = str + strlen(str) - suffix_len; | |
348 | if (!strncmp(p, suffix, suffix_len)) | |
349 | return p; | |
350 | } | |
351 | ||
352 | return NULL; | |
353 | } | |
354 | ||
355 | static char *ltrim(char *str) | |
356 | { | |
357 | int len = strlen(str); | |
358 | ||
359 | while (len && isspace(*str)) { | |
360 | len--; | |
361 | str++; | |
362 | } | |
363 | ||
364 | return str; | |
365 | } | |
366 | ||
367 | static int read_script_info(struct script_desc *desc, const char *filename) | |
368 | { | |
369 | char line[BUFSIZ], *p; | |
370 | FILE *fp; | |
371 | ||
372 | fp = fopen(filename, "r"); | |
373 | if (!fp) | |
374 | return -1; | |
375 | ||
376 | while (fgets(line, sizeof(line), fp)) { | |
377 | p = ltrim(line); | |
378 | if (strlen(p) == 0) | |
379 | continue; | |
380 | if (*p != '#') | |
381 | continue; | |
382 | p++; | |
383 | if (strlen(p) && *p == '!') | |
384 | continue; | |
385 | ||
386 | p = ltrim(p); | |
387 | if (strlen(p) && p[strlen(p) - 1] == '\n') | |
388 | p[strlen(p) - 1] = '\0'; | |
389 | ||
390 | if (!strncmp(p, "description:", strlen("description:"))) { | |
391 | p += strlen("description:"); | |
392 | desc->half_liner = strdup(ltrim(p)); | |
393 | continue; | |
394 | } | |
395 | ||
396 | if (!strncmp(p, "args:", strlen("args:"))) { | |
397 | p += strlen("args:"); | |
398 | desc->args = strdup(ltrim(p)); | |
399 | continue; | |
400 | } | |
401 | } | |
402 | ||
403 | fclose(fp); | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | static int list_available_scripts(const struct option *opt __used, | |
409 | const char *s __used, int unset __used) | |
410 | { | |
411 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
412 | char scripts_path[MAXPATHLEN]; | |
413 | DIR *scripts_dir, *lang_dir; | |
414 | char script_path[MAXPATHLEN]; | |
415 | char lang_path[MAXPATHLEN]; | |
416 | struct script_desc *desc; | |
417 | char first_half[BUFSIZ]; | |
418 | char *script_root; | |
419 | char *str; | |
420 | ||
421 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
422 | ||
423 | scripts_dir = opendir(scripts_path); | |
424 | if (!scripts_dir) | |
425 | return -1; | |
426 | ||
427 | for_each_lang(scripts_dir, lang_dirent, lang_next) { | |
428 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | |
429 | lang_dirent.d_name); | |
430 | lang_dir = opendir(lang_path); | |
431 | if (!lang_dir) | |
432 | continue; | |
433 | ||
434 | for_each_script(lang_dir, script_dirent, script_next) { | |
435 | script_root = strdup(script_dirent.d_name); | |
436 | str = ends_with(script_root, REPORT_SUFFIX); | |
437 | if (str) { | |
438 | *str = '\0'; | |
439 | desc = script_desc__findnew(script_root); | |
440 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
441 | lang_path, script_dirent.d_name); | |
442 | read_script_info(desc, script_path); | |
443 | } | |
444 | free(script_root); | |
445 | } | |
446 | } | |
447 | ||
448 | fprintf(stdout, "List of available trace scripts:\n"); | |
449 | list_for_each_entry(desc, &script_descs, node) { | |
450 | sprintf(first_half, "%s %s", desc->name, | |
451 | desc->args ? desc->args : ""); | |
452 | fprintf(stdout, " %-36s %s\n", first_half, | |
453 | desc->half_liner ? desc->half_liner : ""); | |
454 | } | |
455 | ||
456 | exit(0); | |
457 | } | |
458 | ||
3875294f TZ |
459 | static char *get_script_path(const char *script_root, const char *suffix) |
460 | { | |
461 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
462 | char scripts_path[MAXPATHLEN]; | |
463 | char script_path[MAXPATHLEN]; | |
464 | DIR *scripts_dir, *lang_dir; | |
465 | char lang_path[MAXPATHLEN]; | |
466 | char *str, *__script_root; | |
467 | char *path = NULL; | |
468 | ||
469 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
470 | ||
471 | scripts_dir = opendir(scripts_path); | |
472 | if (!scripts_dir) | |
473 | return NULL; | |
474 | ||
475 | for_each_lang(scripts_dir, lang_dirent, lang_next) { | |
476 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | |
477 | lang_dirent.d_name); | |
478 | lang_dir = opendir(lang_path); | |
479 | if (!lang_dir) | |
480 | continue; | |
481 | ||
482 | for_each_script(lang_dir, script_dirent, script_next) { | |
483 | __script_root = strdup(script_dirent.d_name); | |
484 | str = ends_with(__script_root, suffix); | |
485 | if (str) { | |
486 | *str = '\0'; | |
487 | if (strcmp(__script_root, script_root)) | |
488 | continue; | |
489 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
490 | lang_path, script_dirent.d_name); | |
491 | path = strdup(script_path); | |
492 | free(__script_root); | |
493 | break; | |
494 | } | |
495 | free(__script_root); | |
496 | } | |
497 | } | |
498 | ||
499 | return path; | |
500 | } | |
501 | ||
0422a4fc | 502 | static const char * const trace_usage[] = { |
5f9c39dc FW |
503 | "perf trace [<options>] <command>", |
504 | NULL | |
505 | }; | |
506 | ||
507 | static const struct option options[] = { | |
508 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
509 | "dump raw trace in ASCII"), | |
510 | OPT_BOOLEAN('v', "verbose", &verbose, | |
511 | "be more verbose (show symbol address, etc)"), | |
4b9c0c59 | 512 | OPT_BOOLEAN('L', "Latency", &latency_format, |
cda48461 | 513 | "show latency attributes (irqs/preemption disabled, etc)"), |
4b9c0c59 TZ |
514 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
515 | list_available_scripts), | |
956ffd02 TZ |
516 | OPT_CALLBACK('s', "script", NULL, "name", |
517 | "script file name (lang:script name, script name, or *)", | |
518 | parse_scriptname), | |
519 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | |
520 | "generate perf-trace.xx script in specified language"), | |
521 | ||
1909629f | 522 | OPT_END() |
5f9c39dc FW |
523 | }; |
524 | ||
525 | int cmd_trace(int argc, const char **argv, const char *prefix __used) | |
526 | { | |
d8f66248 | 527 | struct perf_session *session; |
3875294f TZ |
528 | const char *suffix = NULL; |
529 | const char **__argv; | |
530 | char *script_path; | |
531 | int i, err; | |
532 | ||
533 | if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) { | |
534 | if (argc < 3) { | |
535 | fprintf(stderr, | |
536 | "Please specify a record script\n"); | |
537 | return -1; | |
538 | } | |
539 | suffix = RECORD_SUFFIX; | |
540 | } | |
541 | ||
542 | if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) { | |
543 | if (argc < 3) { | |
544 | fprintf(stderr, | |
545 | "Please specify a report script\n"); | |
546 | return -1; | |
547 | } | |
548 | suffix = REPORT_SUFFIX; | |
549 | } | |
550 | ||
551 | if (suffix) { | |
552 | script_path = get_script_path(argv[2], suffix); | |
553 | if (!script_path) { | |
554 | fprintf(stderr, "script not found\n"); | |
555 | return -1; | |
556 | } | |
557 | ||
558 | __argv = malloc((argc + 1) * sizeof(const char *)); | |
559 | __argv[0] = "/bin/sh"; | |
560 | __argv[1] = script_path; | |
561 | for (i = 3; i < argc; i++) | |
562 | __argv[i - 1] = argv[i]; | |
563 | __argv[argc - 1] = NULL; | |
564 | ||
565 | execvp("/bin/sh", (char **)__argv); | |
566 | exit(-1); | |
567 | } | |
956ffd02 | 568 | |
956ffd02 TZ |
569 | setup_scripting(); |
570 | ||
0422a4fc | 571 | argc = parse_options(argc, argv, options, trace_usage, |
586bc5cc | 572 | PARSE_OPT_STOP_AT_NON_OPTION); |
5f9c39dc | 573 | |
655000e7 ACM |
574 | if (symbol__init() < 0) |
575 | return -1; | |
5f9c39dc FW |
576 | setup_pager(); |
577 | ||
75be6cf4 | 578 | session = perf_session__new(input_name, O_RDONLY, 0); |
d8f66248 ACM |
579 | if (session == NULL) |
580 | return -ENOMEM; | |
581 | ||
d549c769 ACM |
582 | if (!perf_session__has_traces(session, "record -R")) |
583 | return -EINVAL; | |
584 | ||
956ffd02 TZ |
585 | if (generate_script_lang) { |
586 | struct stat perf_stat; | |
587 | ||
588 | int input = open(input_name, O_RDONLY); | |
589 | if (input < 0) { | |
590 | perror("failed to open file"); | |
591 | exit(-1); | |
592 | } | |
593 | ||
594 | err = fstat(input, &perf_stat); | |
595 | if (err < 0) { | |
596 | perror("failed to stat file"); | |
597 | exit(-1); | |
598 | } | |
599 | ||
600 | if (!perf_stat.st_size) { | |
601 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
602 | exit(0); | |
603 | } | |
604 | ||
605 | scripting_ops = script_spec__lookup(generate_script_lang); | |
606 | if (!scripting_ops) { | |
607 | fprintf(stderr, "invalid language specifier"); | |
608 | return -1; | |
609 | } | |
610 | ||
94c744b6 | 611 | perf_header__read(&session->header, input); |
956ffd02 TZ |
612 | err = scripting_ops->generate_script("perf-trace"); |
613 | goto out; | |
614 | } | |
615 | ||
616 | if (script_name) { | |
586bc5cc | 617 | err = scripting_ops->start_script(script_name, argc, argv); |
956ffd02 TZ |
618 | if (err) |
619 | goto out; | |
620 | } | |
621 | ||
d8f66248 | 622 | err = __cmd_trace(session); |
956ffd02 | 623 | |
d8f66248 | 624 | perf_session__delete(session); |
956ffd02 TZ |
625 | cleanup_scripting(); |
626 | out: | |
627 | return err; | |
5f9c39dc | 628 | } |