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