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