aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util
diff options
context:
space:
mode:
authorSteven Rostedt <srostedt@redhat.com>2012-04-05 18:47:52 -0400
committerFrederic Weisbecker <fweisbec@gmail.com>2012-04-25 06:28:18 -0400
commit4ace73eef52c651b8f58415fb4476f4791c95e72 (patch)
tree0ca1b5eed67f89f42193b68728e4ba9f31297519 /tools/perf/util
parent3dbe927b1eddcbd66da1653168e33122aca84f4e (diff)
perf: Separate out trace-cmd parse-events from perf files
Move the trace-event-parse.c code that originally came from trace-cmd into their own files. The new file will be called trace-parse-events.c, as the name of trace-cmd's file was parse-events.c too, but it conflicted with the parse-events.c file in perf that parses the command line. This tries to update the code with mimimal changes. Perf specific code stays in the trace-event-parse.[ch] files and the common parsing code is now in trace-parse-events.c and trace-parse-events.h. Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Arnaldo Carvalho de Melo <acme@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Arun Sharma <asharma@fb.com> Cc: Namhyung Kim <namhyung.kim@lge.com> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Diffstat (limited to 'tools/perf/util')
-rw-r--r--tools/perf/util/trace-event-parse.c3135
-rw-r--r--tools/perf/util/trace-event.h275
-rw-r--r--tools/perf/util/trace-parse-events.c3125
-rw-r--r--tools/perf/util/trace-parse-events.h273
4 files changed, 3403 insertions, 3405 deletions
diff --git a/tools/perf/util/trace-event-parse.c b/tools/perf/util/trace-event-parse.c
index dfd1bd8371a4..94775199644e 100644
--- a/tools/perf/util/trace-event-parse.c
+++ b/tools/perf/util/trace-event-parse.c
@@ -1,3142 +1,7 @@
1/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 *
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29
30#include "../perf.h" 1#include "../perf.h"
31#include "util.h" 2#include "util.h"
32#include "trace-event.h" 3#include "trace-event.h"
33 4
34int header_page_ts_offset;
35int header_page_ts_size;
36int header_page_size_offset;
37int header_page_size_size;
38int header_page_overwrite_offset;
39int header_page_overwrite_size;
40int header_page_data_offset;
41int header_page_data_size;
42
43bool latency_format;
44
45static char *input_buf;
46static unsigned long long input_buf_ptr;
47static unsigned long long input_buf_siz;
48
49static int cpus;
50static int long_size;
51static int is_flag_field;
52static int is_symbolic_field;
53
54static struct format_field *
55find_any_field(struct event *event, const char *name);
56
57static void init_input_buf(char *buf, unsigned long long size)
58{
59 input_buf = buf;
60 input_buf_siz = size;
61 input_buf_ptr = 0;
62}
63
64struct cmdline {
65 char *comm;
66 int pid;
67};
68
69static struct cmdline *cmdlines;
70static int cmdline_count;
71
72static int cmdline_cmp(const void *a, const void *b)
73{
74 const struct cmdline *ca = a;
75 const struct cmdline *cb = b;
76
77 if (ca->pid < cb->pid)
78 return -1;
79 if (ca->pid > cb->pid)
80 return 1;
81
82 return 0;
83}
84
85void parse_cmdlines(char *file, int size __unused)
86{
87 struct cmdline_list {
88 struct cmdline_list *next;
89 char *comm;
90 int pid;
91 } *list = NULL, *item;
92 char *line;
93 char *next = NULL;
94 int i;
95
96 line = strtok_r(file, "\n", &next);
97 while (line) {
98 item = malloc_or_die(sizeof(*item));
99 sscanf(line, "%d %as", &item->pid,
100 (float *)(void *)&item->comm); /* workaround gcc warning */
101 item->next = list;
102 list = item;
103 line = strtok_r(NULL, "\n", &next);
104 cmdline_count++;
105 }
106
107 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
108
109 i = 0;
110 while (list) {
111 cmdlines[i].pid = list->pid;
112 cmdlines[i].comm = list->comm;
113 i++;
114 item = list;
115 list = list->next;
116 free(item);
117 }
118
119 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
120}
121
122static struct func_map {
123 unsigned long long addr;
124 char *func;
125 char *mod;
126} *func_list;
127static unsigned int func_count;
128
129static int func_cmp(const void *a, const void *b)
130{
131 const struct func_map *fa = a;
132 const struct func_map *fb = b;
133
134 if (fa->addr < fb->addr)
135 return -1;
136 if (fa->addr > fb->addr)
137 return 1;
138
139 return 0;
140}
141
142void parse_proc_kallsyms(char *file, unsigned int size __unused)
143{
144 struct func_list {
145 struct func_list *next;
146 unsigned long long addr;
147 char *func;
148 char *mod;
149 } *list = NULL, *item;
150 char *line;
151 char *next = NULL;
152 char *addr_str;
153 char ch;
154 int ret __used;
155 int i;
156
157 line = strtok_r(file, "\n", &next);
158 while (line) {
159 item = malloc_or_die(sizeof(*item));
160 item->mod = NULL;
161 ret = sscanf(line, "%as %c %as\t[%as",
162 (float *)(void *)&addr_str, /* workaround gcc warning */
163 &ch,
164 (float *)(void *)&item->func,
165 (float *)(void *)&item->mod);
166 item->addr = strtoull(addr_str, NULL, 16);
167 free(addr_str);
168
169 /* truncate the extra ']' */
170 if (item->mod)
171 item->mod[strlen(item->mod) - 1] = 0;
172
173
174 item->next = list;
175 list = item;
176 line = strtok_r(NULL, "\n", &next);
177 func_count++;
178 }
179
180 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
181
182 i = 0;
183 while (list) {
184 func_list[i].func = list->func;
185 func_list[i].addr = list->addr;
186 func_list[i].mod = list->mod;
187 i++;
188 item = list;
189 list = list->next;
190 free(item);
191 }
192
193 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
194
195 /*
196 * Add a special record at the end.
197 */
198 func_list[func_count].func = NULL;
199 func_list[func_count].addr = 0;
200 func_list[func_count].mod = NULL;
201}
202
203/*
204 * We are searching for a record in between, not an exact
205 * match.
206 */
207static int func_bcmp(const void *a, const void *b)
208{
209 const struct func_map *fa = a;
210 const struct func_map *fb = b;
211
212 if ((fa->addr == fb->addr) ||
213
214 (fa->addr > fb->addr &&
215 fa->addr < (fb+1)->addr))
216 return 0;
217
218 if (fa->addr < fb->addr)
219 return -1;
220
221 return 1;
222}
223
224static struct func_map *find_func(unsigned long long addr)
225{
226 struct func_map *func;
227 struct func_map key;
228
229 key.addr = addr;
230
231 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
232 func_bcmp);
233
234 return func;
235}
236
237void print_funcs(void)
238{
239 int i;
240
241 for (i = 0; i < (int)func_count; i++) {
242 printf("%016llx %s",
243 func_list[i].addr,
244 func_list[i].func);
245 if (func_list[i].mod)
246 printf(" [%s]\n", func_list[i].mod);
247 else
248 printf("\n");
249 }
250}
251
252static struct printk_map {
253 unsigned long long addr;
254 char *printk;
255} *printk_list;
256static unsigned int printk_count;
257
258static int printk_cmp(const void *a, const void *b)
259{
260 const struct func_map *fa = a;
261 const struct func_map *fb = b;
262
263 if (fa->addr < fb->addr)
264 return -1;
265 if (fa->addr > fb->addr)
266 return 1;
267
268 return 0;
269}
270
271static struct printk_map *find_printk(unsigned long long addr)
272{
273 struct printk_map *printk;
274 struct printk_map key;
275
276 key.addr = addr;
277
278 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
279 printk_cmp);
280
281 return printk;
282}
283
284void parse_ftrace_printk(char *file, unsigned int size __unused)
285{
286 struct printk_list {
287 struct printk_list *next;
288 unsigned long long addr;
289 char *printk;
290 } *list = NULL, *item;
291 char *line;
292 char *next = NULL;
293 char *addr_str;
294 int i;
295
296 line = strtok_r(file, "\n", &next);
297 while (line) {
298 addr_str = strsep(&line, ":");
299 if (!line) {
300 warning("error parsing print strings");
301 break;
302 }
303 item = malloc_or_die(sizeof(*item));
304 item->addr = strtoull(addr_str, NULL, 16);
305 /* fmt still has a space, skip it */
306 item->printk = strdup(line+1);
307 item->next = list;
308 list = item;
309 line = strtok_r(NULL, "\n", &next);
310 printk_count++;
311 }
312
313 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
314
315 i = 0;
316 while (list) {
317 printk_list[i].printk = list->printk;
318 printk_list[i].addr = list->addr;
319 i++;
320 item = list;
321 list = list->next;
322 free(item);
323 }
324
325 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
326}
327
328void print_printk(void)
329{
330 int i;
331
332 for (i = 0; i < (int)printk_count; i++) {
333 printf("%016llx %s\n",
334 printk_list[i].addr,
335 printk_list[i].printk);
336 }
337}
338
339static struct event *alloc_event(void)
340{
341 struct event *event;
342
343 event = malloc_or_die(sizeof(*event));
344 memset(event, 0, sizeof(*event));
345
346 return event;
347}
348
349enum event_type {
350 EVENT_ERROR,
351 EVENT_NONE,
352 EVENT_SPACE,
353 EVENT_NEWLINE,
354 EVENT_OP,
355 EVENT_DELIM,
356 EVENT_ITEM,
357 EVENT_DQUOTE,
358 EVENT_SQUOTE,
359};
360
361static struct event *event_list;
362
363static void add_event(struct event *event)
364{
365 event->next = event_list;
366 event_list = event;
367}
368
369static int event_item_type(enum event_type type)
370{
371 switch (type) {
372 case EVENT_ITEM ... EVENT_SQUOTE:
373 return 1;
374 case EVENT_ERROR ... EVENT_DELIM:
375 default:
376 return 0;
377 }
378}
379
380static void free_arg(struct print_arg *arg)
381{
382 if (!arg)
383 return;
384
385 switch (arg->type) {
386 case PRINT_ATOM:
387 if (arg->atom.atom)
388 free(arg->atom.atom);
389 break;
390 case PRINT_NULL:
391 case PRINT_FIELD ... PRINT_OP:
392 default:
393 /* todo */
394 break;
395 }
396
397 free(arg);
398}
399
400static enum event_type get_type(int ch)
401{
402 if (ch == '\n')
403 return EVENT_NEWLINE;
404 if (isspace(ch))
405 return EVENT_SPACE;
406 if (isalnum(ch) || ch == '_')
407 return EVENT_ITEM;
408 if (ch == '\'')
409 return EVENT_SQUOTE;
410 if (ch == '"')
411 return EVENT_DQUOTE;
412 if (!isprint(ch))
413 return EVENT_NONE;
414 if (ch == '(' || ch == ')' || ch == ',')
415 return EVENT_DELIM;
416
417 return EVENT_OP;
418}
419
420static int __read_char(void)
421{
422 if (input_buf_ptr >= input_buf_siz)
423 return -1;
424
425 return input_buf[input_buf_ptr++];
426}
427
428static int __peek_char(void)
429{
430 if (input_buf_ptr >= input_buf_siz)
431 return -1;
432
433 return input_buf[input_buf_ptr];
434}
435
436static enum event_type __read_token(char **tok)
437{
438 char buf[BUFSIZ];
439 int ch, last_ch, quote_ch, next_ch;
440 int i = 0;
441 int tok_size = 0;
442 enum event_type type;
443
444 *tok = NULL;
445
446
447 ch = __read_char();
448 if (ch < 0)
449 return EVENT_NONE;
450
451 type = get_type(ch);
452 if (type == EVENT_NONE)
453 return type;
454
455 buf[i++] = ch;
456
457 switch (type) {
458 case EVENT_NEWLINE:
459 case EVENT_DELIM:
460 *tok = malloc_or_die(2);
461 (*tok)[0] = ch;
462 (*tok)[1] = 0;
463 return type;
464
465 case EVENT_OP:
466 switch (ch) {
467 case '-':
468 next_ch = __peek_char();
469 if (next_ch == '>') {
470 buf[i++] = __read_char();
471 break;
472 }
473 /* fall through */
474 case '+':
475 case '|':
476 case '&':
477 case '>':
478 case '<':
479 last_ch = ch;
480 ch = __peek_char();
481 if (ch != last_ch)
482 goto test_equal;
483 buf[i++] = __read_char();
484 switch (last_ch) {
485 case '>':
486 case '<':
487 goto test_equal;
488 default:
489 break;
490 }
491 break;
492 case '!':
493 case '=':
494 goto test_equal;
495 default: /* what should we do instead? */
496 break;
497 }
498 buf[i] = 0;
499 *tok = strdup(buf);
500 return type;
501
502 test_equal:
503 ch = __peek_char();
504 if (ch == '=')
505 buf[i++] = __read_char();
506 break;
507
508 case EVENT_DQUOTE:
509 case EVENT_SQUOTE:
510 /* don't keep quotes */
511 i--;
512 quote_ch = ch;
513 last_ch = 0;
514 do {
515 if (i == (BUFSIZ - 1)) {
516 buf[i] = 0;
517 if (*tok) {
518 *tok = realloc(*tok, tok_size + BUFSIZ);
519 if (!*tok)
520 return EVENT_NONE;
521 strcat(*tok, buf);
522 } else
523 *tok = strdup(buf);
524
525 if (!*tok)
526 return EVENT_NONE;
527 tok_size += BUFSIZ;
528 i = 0;
529 }
530 last_ch = ch;
531 ch = __read_char();
532 buf[i++] = ch;
533 /* the '\' '\' will cancel itself */
534 if (ch == '\\' && last_ch == '\\')
535 last_ch = 0;
536 } while (ch != quote_ch || last_ch == '\\');
537 /* remove the last quote */
538 i--;
539 goto out;
540
541 case EVENT_ERROR ... EVENT_SPACE:
542 case EVENT_ITEM:
543 default:
544 break;
545 }
546
547 while (get_type(__peek_char()) == type) {
548 if (i == (BUFSIZ - 1)) {
549 buf[i] = 0;
550 if (*tok) {
551 *tok = realloc(*tok, tok_size + BUFSIZ);
552 if (!*tok)
553 return EVENT_NONE;
554 strcat(*tok, buf);
555 } else
556 *tok = strdup(buf);
557
558 if (!*tok)
559 return EVENT_NONE;
560 tok_size += BUFSIZ;
561 i = 0;
562 }
563 ch = __read_char();
564 buf[i++] = ch;
565 }
566
567 out:
568 buf[i] = 0;
569 if (*tok) {
570 *tok = realloc(*tok, tok_size + i);
571 if (!*tok)
572 return EVENT_NONE;
573 strcat(*tok, buf);
574 } else
575 *tok = strdup(buf);
576 if (!*tok)
577 return EVENT_NONE;
578
579 return type;
580}
581
582static void free_token(char *tok)
583{
584 if (tok)
585 free(tok);
586}
587
588static enum event_type read_token(char **tok)
589{
590 enum event_type type;
591
592 for (;;) {
593 type = __read_token(tok);
594 if (type != EVENT_SPACE)
595 return type;
596
597 free_token(*tok);
598 }
599
600 /* not reached */
601 return EVENT_NONE;
602}
603
604/* no newline */
605static enum event_type read_token_item(char **tok)
606{
607 enum event_type type;
608
609 for (;;) {
610 type = __read_token(tok);
611 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
612 return type;
613
614 free_token(*tok);
615 }
616
617 /* not reached */
618 return EVENT_NONE;
619}
620
621static int test_type(enum event_type type, enum event_type expect)
622{
623 if (type != expect) {
624 warning("Error: expected type %d but read %d",
625 expect, type);
626 return -1;
627 }
628 return 0;
629}
630
631static int __test_type_token(enum event_type type, char *token,
632 enum event_type expect, const char *expect_tok,
633 bool warn)
634{
635 if (type != expect) {
636 if (warn)
637 warning("Error: expected type %d but read %d",
638 expect, type);
639 return -1;
640 }
641
642 if (strcmp(token, expect_tok) != 0) {
643 if (warn)
644 warning("Error: expected '%s' but read '%s'",
645 expect_tok, token);
646 return -1;
647 }
648 return 0;
649}
650
651static int test_type_token(enum event_type type, char *token,
652 enum event_type expect, const char *expect_tok)
653{
654 return __test_type_token(type, token, expect, expect_tok, true);
655}
656
657static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
658{
659 enum event_type type;
660
661 if (newline_ok)
662 type = read_token(tok);
663 else
664 type = read_token_item(tok);
665 return test_type(type, expect);
666}
667
668static int read_expect_type(enum event_type expect, char **tok)
669{
670 return __read_expect_type(expect, tok, 1);
671}
672
673static int __read_expected(enum event_type expect, const char *str,
674 int newline_ok, bool warn)
675{
676 enum event_type type;
677 char *token;
678 int ret;
679
680 if (newline_ok)
681 type = read_token(&token);
682 else
683 type = read_token_item(&token);
684
685 ret = __test_type_token(type, token, expect, str, warn);
686
687 free_token(token);
688
689 return ret;
690}
691
692static int read_expected(enum event_type expect, const char *str)
693{
694 return __read_expected(expect, str, 1, true);
695}
696
697static int read_expected_item(enum event_type expect, const char *str)
698{
699 return __read_expected(expect, str, 0, true);
700}
701
702static char *event_read_name(void)
703{
704 char *token;
705
706 if (read_expected(EVENT_ITEM, "name") < 0)
707 return NULL;
708
709 if (read_expected(EVENT_OP, ":") < 0)
710 return NULL;
711
712 if (read_expect_type(EVENT_ITEM, &token) < 0)
713 goto fail;
714
715 return token;
716
717 fail:
718 free_token(token);
719 return NULL;
720}
721
722static int event_read_id(void)
723{
724 char *token;
725 int id = -1;
726
727 if (read_expected_item(EVENT_ITEM, "ID") < 0)
728 return -1;
729
730 if (read_expected(EVENT_OP, ":") < 0)
731 return -1;
732
733 if (read_expect_type(EVENT_ITEM, &token) < 0)
734 goto free;
735
736 id = strtoul(token, NULL, 0);
737
738 free:
739 free_token(token);
740 return id;
741}
742
743static int field_is_string(struct format_field *field)
744{
745 if ((field->flags & FIELD_IS_ARRAY) &&
746 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
747 !strstr(field->type, "s8")))
748 return 1;
749
750 return 0;
751}
752
753static int field_is_dynamic(struct format_field *field)
754{
755 if (!strncmp(field->type, "__data_loc", 10))
756 return 1;
757
758 return 0;
759}
760
761static int event_read_fields(struct event *event, struct format_field **fields)
762{
763 struct format_field *field = NULL;
764 enum event_type type;
765 char *token;
766 char *last_token;
767 int count = 0;
768
769 do {
770 type = read_token(&token);
771 if (type == EVENT_NEWLINE) {
772 free_token(token);
773 return count;
774 }
775
776 count++;
777
778 if (test_type_token(type, token, EVENT_ITEM, "field"))
779 goto fail;
780 free_token(token);
781
782 type = read_token(&token);
783 /*
784 * The ftrace fields may still use the "special" name.
785 * Just ignore it.
786 */
787 if (event->flags & EVENT_FL_ISFTRACE &&
788 type == EVENT_ITEM && strcmp(token, "special") == 0) {
789 free_token(token);
790 type = read_token(&token);
791 }
792
793 if (test_type_token(type, token, EVENT_OP, ":") < 0)
794 return -1;
795
796 if (read_expect_type(EVENT_ITEM, &token) < 0)
797 goto fail;
798
799 last_token = token;
800
801 field = malloc_or_die(sizeof(*field));
802 memset(field, 0, sizeof(*field));
803
804 /* read the rest of the type */
805 for (;;) {
806 type = read_token(&token);
807 if (type == EVENT_ITEM ||
808 (type == EVENT_OP && strcmp(token, "*") == 0) ||
809 /*
810 * Some of the ftrace fields are broken and have
811 * an illegal "." in them.
812 */
813 (event->flags & EVENT_FL_ISFTRACE &&
814 type == EVENT_OP && strcmp(token, ".") == 0)) {
815
816 if (strcmp(token, "*") == 0)
817 field->flags |= FIELD_IS_POINTER;
818
819 if (field->type) {
820 field->type = realloc(field->type,
821 strlen(field->type) +
822 strlen(last_token) + 2);
823 strcat(field->type, " ");
824 strcat(field->type, last_token);
825 } else
826 field->type = last_token;
827 last_token = token;
828 continue;
829 }
830
831 break;
832 }
833
834 if (!field->type) {
835 die("no type found");
836 goto fail;
837 }
838 field->name = last_token;
839
840 if (test_type(type, EVENT_OP))
841 goto fail;
842
843 if (strcmp(token, "[") == 0) {
844 enum event_type last_type = type;
845 char *brackets = token;
846 int len;
847
848 field->flags |= FIELD_IS_ARRAY;
849
850 type = read_token(&token);
851 while (strcmp(token, "]") != 0) {
852 if (last_type == EVENT_ITEM &&
853 type == EVENT_ITEM)
854 len = 2;
855 else
856 len = 1;
857 last_type = type;
858
859 brackets = realloc(brackets,
860 strlen(brackets) +
861 strlen(token) + len);
862 if (len == 2)
863 strcat(brackets, " ");
864 strcat(brackets, token);
865 free_token(token);
866 type = read_token(&token);
867 if (type == EVENT_NONE) {
868 die("failed to find token");
869 goto fail;
870 }
871 }
872
873 free_token(token);
874
875 brackets = realloc(brackets, strlen(brackets) + 2);
876 strcat(brackets, "]");
877
878 /* add brackets to type */
879
880 type = read_token(&token);
881 /*
882 * If the next token is not an OP, then it is of
883 * the format: type [] item;
884 */
885 if (type == EVENT_ITEM) {
886 field->type = realloc(field->type,
887 strlen(field->type) +
888 strlen(field->name) +
889 strlen(brackets) + 2);
890 strcat(field->type, " ");
891 strcat(field->type, field->name);
892 free_token(field->name);
893 strcat(field->type, brackets);
894 field->name = token;
895 type = read_token(&token);
896 } else {
897 field->type = realloc(field->type,
898 strlen(field->type) +
899 strlen(brackets) + 1);
900 strcat(field->type, brackets);
901 }
902 free(brackets);
903 }
904
905 if (field_is_string(field)) {
906 field->flags |= FIELD_IS_STRING;
907 if (field_is_dynamic(field))
908 field->flags |= FIELD_IS_DYNAMIC;
909 }
910
911 if (test_type_token(type, token, EVENT_OP, ";"))
912 goto fail;
913 free_token(token);
914
915 if (read_expected(EVENT_ITEM, "offset") < 0)
916 goto fail_expect;
917
918 if (read_expected(EVENT_OP, ":") < 0)
919 goto fail_expect;
920
921 if (read_expect_type(EVENT_ITEM, &token))
922 goto fail;
923 field->offset = strtoul(token, NULL, 0);
924 free_token(token);
925
926 if (read_expected(EVENT_OP, ";") < 0)
927 goto fail_expect;
928
929 if (read_expected(EVENT_ITEM, "size") < 0)
930 goto fail_expect;
931
932 if (read_expected(EVENT_OP, ":") < 0)
933 goto fail_expect;
934
935 if (read_expect_type(EVENT_ITEM, &token))
936 goto fail;
937 field->size = strtoul(token, NULL, 0);
938 free_token(token);
939
940 if (read_expected(EVENT_OP, ";") < 0)
941 goto fail_expect;
942
943 type = read_token(&token);
944 if (type != EVENT_NEWLINE) {
945 /* newer versions of the kernel have a "signed" type */
946 if (test_type_token(type, token, EVENT_ITEM, "signed"))
947 goto fail;
948
949 free_token(token);
950
951 if (read_expected(EVENT_OP, ":") < 0)
952 goto fail_expect;
953
954 if (read_expect_type(EVENT_ITEM, &token))
955 goto fail;
956
957 if (strtoul(token, NULL, 0))
958 field->flags |= FIELD_IS_SIGNED;
959
960 free_token(token);
961 if (read_expected(EVENT_OP, ";") < 0)
962 goto fail_expect;
963
964 if (read_expect_type(EVENT_NEWLINE, &token))
965 goto fail;
966 }
967
968 free_token(token);
969
970 *fields = field;
971 fields = &field->next;
972
973 } while (1);
974
975 return 0;
976
977fail:
978 free_token(token);
979fail_expect:
980 if (field)
981 free(field);
982 return -1;
983}
984
985static int event_read_format(struct event *event)
986{
987 char *token;
988 int ret;
989
990 if (read_expected_item(EVENT_ITEM, "format") < 0)
991 return -1;
992
993 if (read_expected(EVENT_OP, ":") < 0)
994 return -1;
995
996 if (read_expect_type(EVENT_NEWLINE, &token))
997 goto fail;
998 free_token(token);
999
1000 ret = event_read_fields(event, &event->format.common_fields);
1001 if (ret < 0)
1002 return ret;
1003 event->format.nr_common = ret;
1004
1005 ret = event_read_fields(event, &event->format.fields);
1006 if (ret < 0)
1007 return ret;
1008 event->format.nr_fields = ret;
1009
1010 return 0;
1011
1012 fail:
1013 free_token(token);
1014 return -1;
1015}
1016
1017enum event_type
1018process_arg_token(struct event *event, struct print_arg *arg,
1019 char **tok, enum event_type type);
1020
1021static enum event_type
1022process_arg(struct event *event, struct print_arg *arg, char **tok)
1023{
1024 enum event_type type;
1025 char *token;
1026
1027 type = read_token(&token);
1028 *tok = token;
1029
1030 return process_arg_token(event, arg, tok, type);
1031}
1032
1033static enum event_type
1034process_cond(struct event *event, struct print_arg *top, char **tok)
1035{
1036 struct print_arg *arg, *left, *right;
1037 enum event_type type;
1038 char *token = NULL;
1039
1040 arg = malloc_or_die(sizeof(*arg));
1041 memset(arg, 0, sizeof(*arg));
1042
1043 left = malloc_or_die(sizeof(*left));
1044
1045 right = malloc_or_die(sizeof(*right));
1046
1047 arg->type = PRINT_OP;
1048 arg->op.left = left;
1049 arg->op.right = right;
1050
1051 *tok = NULL;
1052 type = process_arg(event, left, &token);
1053 if (test_type_token(type, token, EVENT_OP, ":"))
1054 goto out_free;
1055
1056 arg->op.op = token;
1057
1058 type = process_arg(event, right, &token);
1059
1060 top->op.right = arg;
1061
1062 *tok = token;
1063 return type;
1064
1065out_free:
1066 free_token(*tok);
1067 free(right);
1068 free(left);
1069 free_arg(arg);
1070 return EVENT_ERROR;
1071}
1072
1073static enum event_type
1074process_array(struct event *event, struct print_arg *top, char **tok)
1075{
1076 struct print_arg *arg;
1077 enum event_type type;
1078 char *token = NULL;
1079
1080 arg = malloc_or_die(sizeof(*arg));
1081 memset(arg, 0, sizeof(*arg));
1082
1083 *tok = NULL;
1084 type = process_arg(event, arg, &token);
1085 if (test_type_token(type, token, EVENT_OP, "]"))
1086 goto out_free;
1087
1088 top->op.right = arg;
1089
1090 free_token(token);
1091 type = read_token_item(&token);
1092 *tok = token;
1093
1094 return type;
1095
1096out_free:
1097 free_token(*tok);
1098 free_arg(arg);
1099 return EVENT_ERROR;
1100}
1101
1102static int get_op_prio(char *op)
1103{
1104 if (!op[1]) {
1105 switch (op[0]) {
1106 case '*':
1107 case '/':
1108 case '%':
1109 return 6;
1110 case '+':
1111 case '-':
1112 return 7;
1113 /* '>>' and '<<' are 8 */
1114 case '<':
1115 case '>':
1116 return 9;
1117 /* '==' and '!=' are 10 */
1118 case '&':
1119 return 11;
1120 case '^':
1121 return 12;
1122 case '|':
1123 return 13;
1124 case '?':
1125 return 16;
1126 default:
1127 die("unknown op '%c'", op[0]);
1128 return -1;
1129 }
1130 } else {
1131 if (strcmp(op, "++") == 0 ||
1132 strcmp(op, "--") == 0) {
1133 return 3;
1134 } else if (strcmp(op, ">>") == 0 ||
1135 strcmp(op, "<<") == 0) {
1136 return 8;
1137 } else if (strcmp(op, ">=") == 0 ||
1138 strcmp(op, "<=") == 0) {
1139 return 9;
1140 } else if (strcmp(op, "==") == 0 ||
1141 strcmp(op, "!=") == 0) {
1142 return 10;
1143 } else if (strcmp(op, "&&") == 0) {
1144 return 14;
1145 } else if (strcmp(op, "||") == 0) {
1146 return 15;
1147 } else {
1148 die("unknown op '%s'", op);
1149 return -1;
1150 }
1151 }
1152}
1153
1154static void set_op_prio(struct print_arg *arg)
1155{
1156
1157 /* single ops are the greatest */
1158 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1159 arg->op.prio = 0;
1160 return;
1161 }
1162
1163 arg->op.prio = get_op_prio(arg->op.op);
1164}
1165
1166static enum event_type
1167process_op(struct event *event, struct print_arg *arg, char **tok)
1168{
1169 struct print_arg *left, *right = NULL;
1170 enum event_type type;
1171 char *token;
1172
1173 /* the op is passed in via tok */
1174 token = *tok;
1175
1176 if (arg->type == PRINT_OP && !arg->op.left) {
1177 /* handle single op */
1178 if (token[1]) {
1179 die("bad op token %s", token);
1180 return EVENT_ERROR;
1181 }
1182 switch (token[0]) {
1183 case '!':
1184 case '+':
1185 case '-':
1186 break;
1187 default:
1188 die("bad op token %s", token);
1189 return EVENT_ERROR;
1190 }
1191
1192 /* make an empty left */
1193 left = malloc_or_die(sizeof(*left));
1194 left->type = PRINT_NULL;
1195 arg->op.left = left;
1196
1197 right = malloc_or_die(sizeof(*right));
1198 arg->op.right = right;
1199
1200 type = process_arg(event, right, tok);
1201
1202 } else if (strcmp(token, "?") == 0) {
1203
1204 left = malloc_or_die(sizeof(*left));
1205 /* copy the top arg to the left */
1206 *left = *arg;
1207
1208 arg->type = PRINT_OP;
1209 arg->op.op = token;
1210 arg->op.left = left;
1211 arg->op.prio = 0;
1212
1213 type = process_cond(event, arg, tok);
1214
1215 } else if (strcmp(token, ">>") == 0 ||
1216 strcmp(token, "<<") == 0 ||
1217 strcmp(token, "&") == 0 ||
1218 strcmp(token, "|") == 0 ||
1219 strcmp(token, "&&") == 0 ||
1220 strcmp(token, "||") == 0 ||
1221 strcmp(token, "-") == 0 ||
1222 strcmp(token, "+") == 0 ||
1223 strcmp(token, "*") == 0 ||
1224 strcmp(token, "^") == 0 ||
1225 strcmp(token, "/") == 0 ||
1226 strcmp(token, "<") == 0 ||
1227 strcmp(token, ">") == 0 ||
1228 strcmp(token, "==") == 0 ||
1229 strcmp(token, "!=") == 0) {
1230
1231 left = malloc_or_die(sizeof(*left));
1232
1233 /* copy the top arg to the left */
1234 *left = *arg;
1235
1236 arg->type = PRINT_OP;
1237 arg->op.op = token;
1238 arg->op.left = left;
1239
1240 set_op_prio(arg);
1241
1242 right = malloc_or_die(sizeof(*right));
1243
1244 type = read_token_item(&token);
1245 *tok = token;
1246
1247 /* could just be a type pointer */
1248 if ((strcmp(arg->op.op, "*") == 0) &&
1249 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1250 if (left->type != PRINT_ATOM)
1251 die("bad pointer type");
1252 left->atom.atom = realloc(left->atom.atom,
1253 sizeof(left->atom.atom) + 3);
1254 strcat(left->atom.atom, " *");
1255 *arg = *left;
1256 free(arg);
1257
1258 return type;
1259 }
1260
1261 type = process_arg_token(event, right, tok, type);
1262
1263 arg->op.right = right;
1264
1265 } else if (strcmp(token, "[") == 0) {
1266
1267 left = malloc_or_die(sizeof(*left));
1268 *left = *arg;
1269
1270 arg->type = PRINT_OP;
1271 arg->op.op = token;
1272 arg->op.left = left;
1273
1274 arg->op.prio = 0;
1275 type = process_array(event, arg, tok);
1276
1277 } else {
1278 warning("unknown op '%s'", token);
1279 event->flags |= EVENT_FL_FAILED;
1280 /* the arg is now the left side */
1281 return EVENT_NONE;
1282 }
1283
1284 if (type == EVENT_OP) {
1285 int prio;
1286
1287 /* higher prios need to be closer to the root */
1288 prio = get_op_prio(*tok);
1289
1290 if (prio > arg->op.prio)
1291 return process_op(event, arg, tok);
1292
1293 return process_op(event, right, tok);
1294 }
1295
1296 return type;
1297}
1298
1299static enum event_type
1300process_entry(struct event *event __unused, struct print_arg *arg,
1301 char **tok)
1302{
1303 enum event_type type;
1304 char *field;
1305 char *token;
1306
1307 if (read_expected(EVENT_OP, "->") < 0)
1308 return EVENT_ERROR;
1309
1310 if (read_expect_type(EVENT_ITEM, &token) < 0)
1311 goto fail;
1312 field = token;
1313
1314 arg->type = PRINT_FIELD;
1315 arg->field.name = field;
1316
1317 if (is_flag_field) {
1318 arg->field.field = find_any_field(event, arg->field.name);
1319 arg->field.field->flags |= FIELD_IS_FLAG;
1320 is_flag_field = 0;
1321 } else if (is_symbolic_field) {
1322 arg->field.field = find_any_field(event, arg->field.name);
1323 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1324 is_symbolic_field = 0;
1325 }
1326
1327 type = read_token(&token);
1328 *tok = token;
1329
1330 return type;
1331
1332fail:
1333 free_token(token);
1334 return EVENT_ERROR;
1335}
1336
1337static char *arg_eval (struct print_arg *arg);
1338
1339static long long arg_num_eval(struct print_arg *arg)
1340{
1341 long long left, right;
1342 long long val = 0;
1343
1344 switch (arg->type) {
1345 case PRINT_ATOM:
1346 val = strtoll(arg->atom.atom, NULL, 0);
1347 break;
1348 case PRINT_TYPE:
1349 val = arg_num_eval(arg->typecast.item);
1350 break;
1351 case PRINT_OP:
1352 switch (arg->op.op[0]) {
1353 case '|':
1354 left = arg_num_eval(arg->op.left);
1355 right = arg_num_eval(arg->op.right);
1356 if (arg->op.op[1])
1357 val = left || right;
1358 else
1359 val = left | right;
1360 break;
1361 case '&':
1362 left = arg_num_eval(arg->op.left);
1363 right = arg_num_eval(arg->op.right);
1364 if (arg->op.op[1])
1365 val = left && right;
1366 else
1367 val = left & right;
1368 break;
1369 case '<':
1370 left = arg_num_eval(arg->op.left);
1371 right = arg_num_eval(arg->op.right);
1372 switch (arg->op.op[1]) {
1373 case 0:
1374 val = left < right;
1375 break;
1376 case '<':
1377 val = left << right;
1378 break;
1379 case '=':
1380 val = left <= right;
1381 break;
1382 default:
1383 die("unknown op '%s'", arg->op.op);
1384 }
1385 break;
1386 case '>':
1387 left = arg_num_eval(arg->op.left);
1388 right = arg_num_eval(arg->op.right);
1389 switch (arg->op.op[1]) {
1390 case 0:
1391 val = left > right;
1392 break;
1393 case '>':
1394 val = left >> right;
1395 break;
1396 case '=':
1397 val = left >= right;
1398 break;
1399 default:
1400 die("unknown op '%s'", arg->op.op);
1401 }
1402 break;
1403 case '=':
1404 left = arg_num_eval(arg->op.left);
1405 right = arg_num_eval(arg->op.right);
1406
1407 if (arg->op.op[1] != '=')
1408 die("unknown op '%s'", arg->op.op);
1409
1410 val = left == right;
1411 break;
1412 case '!':
1413 left = arg_num_eval(arg->op.left);
1414 right = arg_num_eval(arg->op.right);
1415
1416 switch (arg->op.op[1]) {
1417 case '=':
1418 val = left != right;
1419 break;
1420 default:
1421 die("unknown op '%s'", arg->op.op);
1422 }
1423 break;
1424 case '+':
1425 left = arg_num_eval(arg->op.left);
1426 right = arg_num_eval(arg->op.right);
1427 val = left + right;
1428 break;
1429 default:
1430 die("unknown op '%s'", arg->op.op);
1431 }
1432 break;
1433
1434 case PRINT_NULL:
1435 case PRINT_FIELD ... PRINT_SYMBOL:
1436 case PRINT_STRING:
1437 default:
1438 die("invalid eval type %d", arg->type);
1439
1440 }
1441 return val;
1442}
1443
1444static char *arg_eval (struct print_arg *arg)
1445{
1446 long long val;
1447 static char buf[20];
1448
1449 switch (arg->type) {
1450 case PRINT_ATOM:
1451 return arg->atom.atom;
1452 case PRINT_TYPE:
1453 return arg_eval(arg->typecast.item);
1454 case PRINT_OP:
1455 val = arg_num_eval(arg);
1456 sprintf(buf, "%lld", val);
1457 return buf;
1458
1459 case PRINT_NULL:
1460 case PRINT_FIELD ... PRINT_SYMBOL:
1461 case PRINT_STRING:
1462 default:
1463 die("invalid eval type %d", arg->type);
1464 break;
1465 }
1466
1467 return NULL;
1468}
1469
1470static enum event_type
1471process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1472{
1473 enum event_type type;
1474 struct print_arg *arg = NULL;
1475 struct print_flag_sym *field;
1476 char *token = NULL;
1477 char *value;
1478
1479 do {
1480 free_token(token);
1481 type = read_token_item(&token);
1482 if (test_type_token(type, token, EVENT_OP, "{"))
1483 break;
1484
1485 arg = malloc_or_die(sizeof(*arg));
1486
1487 free_token(token);
1488 type = process_arg(event, arg, &token);
1489
1490 if (type == EVENT_OP)
1491 type = process_op(event, arg, &token);
1492
1493 if (type == EVENT_ERROR)
1494 goto out_free;
1495
1496 if (test_type_token(type, token, EVENT_DELIM, ","))
1497 goto out_free;
1498
1499 field = malloc_or_die(sizeof(*field));
1500 memset(field, 0, sizeof(*field));
1501
1502 value = arg_eval(arg);
1503 field->value = strdup(value);
1504
1505 free_token(token);
1506 type = process_arg(event, arg, &token);
1507 if (test_type_token(type, token, EVENT_OP, "}"))
1508 goto out_free;
1509
1510 value = arg_eval(arg);
1511 field->str = strdup(value);
1512 free_arg(arg);
1513 arg = NULL;
1514
1515 *list = field;
1516 list = &field->next;
1517
1518 free_token(token);
1519 type = read_token_item(&token);
1520 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1521
1522 *tok = token;
1523 return type;
1524
1525out_free:
1526 free_arg(arg);
1527 free_token(token);
1528
1529 return EVENT_ERROR;
1530}
1531
1532static enum event_type
1533process_flags(struct event *event, struct print_arg *arg, char **tok)
1534{
1535 struct print_arg *field;
1536 enum event_type type;
1537 char *token;
1538
1539 memset(arg, 0, sizeof(*arg));
1540 arg->type = PRINT_FLAGS;
1541
1542 if (read_expected_item(EVENT_DELIM, "(") < 0)
1543 return EVENT_ERROR;
1544
1545 field = malloc_or_die(sizeof(*field));
1546
1547 type = process_arg(event, field, &token);
1548 while (type == EVENT_OP)
1549 type = process_op(event, field, &token);
1550 if (test_type_token(type, token, EVENT_DELIM, ","))
1551 goto out_free;
1552
1553 arg->flags.field = field;
1554
1555 type = read_token_item(&token);
1556 if (event_item_type(type)) {
1557 arg->flags.delim = token;
1558 type = read_token_item(&token);
1559 }
1560
1561 if (test_type_token(type, token, EVENT_DELIM, ","))
1562 goto out_free;
1563
1564 type = process_fields(event, &arg->flags.flags, &token);
1565 if (test_type_token(type, token, EVENT_DELIM, ")"))
1566 goto out_free;
1567
1568 free_token(token);
1569 type = read_token_item(tok);
1570 return type;
1571
1572out_free:
1573 free_token(token);
1574 return EVENT_ERROR;
1575}
1576
1577static enum event_type
1578process_symbols(struct event *event, struct print_arg *arg, char **tok)
1579{
1580 struct print_arg *field;
1581 enum event_type type;
1582 char *token;
1583
1584 memset(arg, 0, sizeof(*arg));
1585 arg->type = PRINT_SYMBOL;
1586
1587 if (read_expected_item(EVENT_DELIM, "(") < 0)
1588 return EVENT_ERROR;
1589
1590 field = malloc_or_die(sizeof(*field));
1591
1592 type = process_arg(event, field, &token);
1593 if (test_type_token(type, token, EVENT_DELIM, ","))
1594 goto out_free;
1595
1596 arg->symbol.field = field;
1597
1598 type = process_fields(event, &arg->symbol.symbols, &token);
1599 if (test_type_token(type, token, EVENT_DELIM, ")"))
1600 goto out_free;
1601
1602 free_token(token);
1603 type = read_token_item(tok);
1604 return type;
1605
1606out_free:
1607 free_token(token);
1608 return EVENT_ERROR;
1609}
1610
1611static enum event_type
1612process_paren(struct event *event, struct print_arg *arg, char **tok)
1613{
1614 struct print_arg *item_arg;
1615 enum event_type type;
1616 char *token;
1617
1618 type = process_arg(event, arg, &token);
1619
1620 if (type == EVENT_ERROR)
1621 return EVENT_ERROR;
1622
1623 if (type == EVENT_OP)
1624 type = process_op(event, arg, &token);
1625
1626 if (type == EVENT_ERROR)
1627 return EVENT_ERROR;
1628
1629 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1630 free_token(token);
1631 return EVENT_ERROR;
1632 }
1633
1634 free_token(token);
1635 type = read_token_item(&token);
1636
1637 /*
1638 * If the next token is an item or another open paren, then
1639 * this was a typecast.
1640 */
1641 if (event_item_type(type) ||
1642 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1643
1644 /* make this a typecast and contine */
1645
1646 /* prevous must be an atom */
1647 if (arg->type != PRINT_ATOM)
1648 die("previous needed to be PRINT_ATOM");
1649
1650 item_arg = malloc_or_die(sizeof(*item_arg));
1651
1652 arg->type = PRINT_TYPE;
1653 arg->typecast.type = arg->atom.atom;
1654 arg->typecast.item = item_arg;
1655 type = process_arg_token(event, item_arg, &token, type);
1656
1657 }
1658
1659 *tok = token;
1660 return type;
1661}
1662
1663
1664static enum event_type
1665process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1666{
1667 enum event_type type;
1668 char *token;
1669
1670 if (read_expected(EVENT_DELIM, "(") < 0)
1671 return EVENT_ERROR;
1672
1673 if (read_expect_type(EVENT_ITEM, &token) < 0)
1674 goto fail;
1675
1676 arg->type = PRINT_STRING;
1677 arg->string.string = token;
1678 arg->string.offset = -1;
1679
1680 if (read_expected(EVENT_DELIM, ")") < 0)
1681 return EVENT_ERROR;
1682
1683 type = read_token(&token);
1684 *tok = token;
1685
1686 return type;
1687fail:
1688 free_token(token);
1689 return EVENT_ERROR;
1690}
1691
1692enum event_type
1693process_arg_token(struct event *event, struct print_arg *arg,
1694 char **tok, enum event_type type)
1695{
1696 char *token;
1697 char *atom;
1698
1699 token = *tok;
1700
1701 switch (type) {
1702 case EVENT_ITEM:
1703 if (strcmp(token, "REC") == 0) {
1704 free_token(token);
1705 type = process_entry(event, arg, &token);
1706 } else if (strcmp(token, "__print_flags") == 0) {
1707 free_token(token);
1708 is_flag_field = 1;
1709 type = process_flags(event, arg, &token);
1710 } else if (strcmp(token, "__print_symbolic") == 0) {
1711 free_token(token);
1712 is_symbolic_field = 1;
1713 type = process_symbols(event, arg, &token);
1714 } else if (strcmp(token, "__get_str") == 0) {
1715 free_token(token);
1716 type = process_str(event, arg, &token);
1717 } else {
1718 atom = token;
1719 /* test the next token */
1720 type = read_token_item(&token);
1721
1722 /* atoms can be more than one token long */
1723 while (type == EVENT_ITEM) {
1724 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1725 strcat(atom, " ");
1726 strcat(atom, token);
1727 free_token(token);
1728 type = read_token_item(&token);
1729 }
1730
1731 /* todo, test for function */
1732
1733 arg->type = PRINT_ATOM;
1734 arg->atom.atom = atom;
1735 }
1736 break;
1737 case EVENT_DQUOTE:
1738 case EVENT_SQUOTE:
1739 arg->type = PRINT_ATOM;
1740 arg->atom.atom = token;
1741 type = read_token_item(&token);
1742 break;
1743 case EVENT_DELIM:
1744 if (strcmp(token, "(") == 0) {
1745 free_token(token);
1746 type = process_paren(event, arg, &token);
1747 break;
1748 }
1749 case EVENT_OP:
1750 /* handle single ops */
1751 arg->type = PRINT_OP;
1752 arg->op.op = token;
1753 arg->op.left = NULL;
1754 type = process_op(event, arg, &token);
1755
1756 break;
1757
1758 case EVENT_ERROR ... EVENT_NEWLINE:
1759 default:
1760 die("unexpected type %d", type);
1761 }
1762 *tok = token;
1763
1764 return type;
1765}
1766
1767static int event_read_print_args(struct event *event, struct print_arg **list)
1768{
1769 enum event_type type = EVENT_ERROR;
1770 struct print_arg *arg;
1771 char *token;
1772 int args = 0;
1773
1774 do {
1775 if (type == EVENT_NEWLINE) {
1776 free_token(token);
1777 type = read_token_item(&token);
1778 continue;
1779 }
1780
1781 arg = malloc_or_die(sizeof(*arg));
1782 memset(arg, 0, sizeof(*arg));
1783
1784 type = process_arg(event, arg, &token);
1785
1786 if (type == EVENT_ERROR) {
1787 free_arg(arg);
1788 return -1;
1789 }
1790
1791 *list = arg;
1792 args++;
1793
1794 if (type == EVENT_OP) {
1795 type = process_op(event, arg, &token);
1796 list = &arg->next;
1797 continue;
1798 }
1799
1800 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1801 free_token(token);
1802 *list = arg;
1803 list = &arg->next;
1804 continue;
1805 }
1806 break;
1807 } while (type != EVENT_NONE);
1808
1809 if (type != EVENT_NONE)
1810 free_token(token);
1811
1812 return args;
1813}
1814
1815static int event_read_print(struct event *event)
1816{
1817 enum event_type type;
1818 char *token;
1819 int ret;
1820
1821 if (read_expected_item(EVENT_ITEM, "print") < 0)
1822 return -1;
1823
1824 if (read_expected(EVENT_ITEM, "fmt") < 0)
1825 return -1;
1826
1827 if (read_expected(EVENT_OP, ":") < 0)
1828 return -1;
1829
1830 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1831 goto fail;
1832
1833 concat:
1834 event->print_fmt.format = token;
1835 event->print_fmt.args = NULL;
1836
1837 /* ok to have no arg */
1838 type = read_token_item(&token);
1839
1840 if (type == EVENT_NONE)
1841 return 0;
1842
1843 /* Handle concatination of print lines */
1844 if (type == EVENT_DQUOTE) {
1845 char *cat;
1846
1847 cat = malloc_or_die(strlen(event->print_fmt.format) +
1848 strlen(token) + 1);
1849 strcpy(cat, event->print_fmt.format);
1850 strcat(cat, token);
1851 free_token(token);
1852 free_token(event->print_fmt.format);
1853 event->print_fmt.format = NULL;
1854 token = cat;
1855 goto concat;
1856 }
1857
1858 if (test_type_token(type, token, EVENT_DELIM, ","))
1859 goto fail;
1860
1861 free_token(token);
1862
1863 ret = event_read_print_args(event, &event->print_fmt.args);
1864 if (ret < 0)
1865 return -1;
1866
1867 return ret;
1868
1869 fail:
1870 free_token(token);
1871 return -1;
1872}
1873
1874static struct format_field *
1875find_common_field(struct event *event, const char *name)
1876{
1877 struct format_field *format;
1878
1879 for (format = event->format.common_fields;
1880 format; format = format->next) {
1881 if (strcmp(format->name, name) == 0)
1882 break;
1883 }
1884
1885 return format;
1886}
1887
1888static struct format_field *
1889find_field(struct event *event, const char *name)
1890{
1891 struct format_field *format;
1892
1893 for (format = event->format.fields;
1894 format; format = format->next) {
1895 if (strcmp(format->name, name) == 0)
1896 break;
1897 }
1898
1899 return format;
1900}
1901
1902static struct format_field *
1903find_any_field(struct event *event, const char *name)
1904{
1905 struct format_field *format;
1906
1907 format = find_common_field(event, name);
1908 if (format)
1909 return format;
1910 return find_field(event, name);
1911}
1912
1913unsigned long long read_size(void *ptr, int size)
1914{
1915 switch (size) {
1916 case 1:
1917 return *(unsigned char *)ptr;
1918 case 2:
1919 return data2host2(ptr);
1920 case 4:
1921 return data2host4(ptr);
1922 case 8:
1923 return data2host8(ptr);
1924 default:
1925 /* BUG! */
1926 return 0;
1927 }
1928}
1929
1930unsigned long long
1931raw_field_value(struct event *event, const char *name, void *data)
1932{
1933 struct format_field *field;
1934
1935 field = find_any_field(event, name);
1936 if (!field)
1937 return 0ULL;
1938
1939 return read_size(data + field->offset, field->size);
1940}
1941
1942void *raw_field_ptr(struct event *event, const char *name, void *data)
1943{
1944 struct format_field *field;
1945
1946 field = find_any_field(event, name);
1947 if (!field)
1948 return NULL;
1949
1950 if (field->flags & FIELD_IS_DYNAMIC) {
1951 int offset;
1952
1953 offset = *(int *)(data + field->offset);
1954 offset &= 0xffff;
1955
1956 return data + offset;
1957 }
1958
1959 return data + field->offset;
1960}
1961
1962static int get_common_info(const char *type, int *offset, int *size)
1963{
1964 struct event *event;
1965 struct format_field *field;
1966
1967 /*
1968 * All events should have the same common elements.
1969 * Pick any event to find where the type is;
1970 */
1971 if (!event_list)
1972 die("no event_list!");
1973
1974 event = event_list;
1975 field = find_common_field(event, type);
1976 if (!field)
1977 die("field '%s' not found", type);
1978
1979 *offset = field->offset;
1980 *size = field->size;
1981
1982 return 0;
1983}
1984
1985static int __parse_common(void *data, int *size, int *offset,
1986 const char *name)
1987{
1988 int ret;
1989
1990 if (!*size) {
1991 ret = get_common_info(name, offset, size);
1992 if (ret < 0)
1993 return ret;
1994 }
1995 return read_size(data + *offset, *size);
1996}
1997
1998int trace_parse_common_type(void *data)
1999{
2000 static int type_offset;
2001 static int type_size;
2002
2003 return __parse_common(data, &type_size, &type_offset,
2004 "common_type");
2005}
2006
2007int trace_parse_common_pid(void *data)
2008{
2009 static int pid_offset;
2010 static int pid_size;
2011
2012 return __parse_common(data, &pid_size, &pid_offset,
2013 "common_pid");
2014}
2015
2016int parse_common_pc(void *data)
2017{
2018 static int pc_offset;
2019 static int pc_size;
2020
2021 return __parse_common(data, &pc_size, &pc_offset,
2022 "common_preempt_count");
2023}
2024
2025int parse_common_flags(void *data)
2026{
2027 static int flags_offset;
2028 static int flags_size;
2029
2030 return __parse_common(data, &flags_size, &flags_offset,
2031 "common_flags");
2032}
2033
2034int parse_common_lock_depth(void *data)
2035{
2036 static int ld_offset;
2037 static int ld_size;
2038 int ret;
2039
2040 ret = __parse_common(data, &ld_size, &ld_offset,
2041 "common_lock_depth");
2042 if (ret < 0)
2043 return -1;
2044
2045 return ret;
2046}
2047
2048struct event *trace_find_event(int id)
2049{
2050 struct event *event;
2051
2052 for (event = event_list; event; event = event->next) {
2053 if (event->id == id)
2054 break;
2055 }
2056 return event;
2057}
2058
2059struct event *trace_find_next_event(struct event *event)
2060{
2061 if (!event)
2062 return event_list;
2063
2064 return event->next;
2065}
2066
2067static unsigned long long eval_num_arg(void *data, int size,
2068 struct event *event, struct print_arg *arg)
2069{
2070 unsigned long long val = 0;
2071 unsigned long long left, right;
2072 struct print_arg *larg;
2073
2074 switch (arg->type) {
2075 case PRINT_NULL:
2076 /* ?? */
2077 return 0;
2078 case PRINT_ATOM:
2079 return strtoull(arg->atom.atom, NULL, 0);
2080 case PRINT_FIELD:
2081 if (!arg->field.field) {
2082 arg->field.field = find_any_field(event, arg->field.name);
2083 if (!arg->field.field)
2084 die("field %s not found", arg->field.name);
2085 }
2086 /* must be a number */
2087 val = read_size(data + arg->field.field->offset,
2088 arg->field.field->size);
2089 break;
2090 case PRINT_FLAGS:
2091 case PRINT_SYMBOL:
2092 break;
2093 case PRINT_TYPE:
2094 return eval_num_arg(data, size, event, arg->typecast.item);
2095 case PRINT_STRING:
2096 return 0;
2097 break;
2098 case PRINT_OP:
2099 if (strcmp(arg->op.op, "[") == 0) {
2100 /*
2101 * Arrays are special, since we don't want
2102 * to read the arg as is.
2103 */
2104 if (arg->op.left->type != PRINT_FIELD)
2105 goto default_op; /* oops, all bets off */
2106 larg = arg->op.left;
2107 if (!larg->field.field) {
2108 larg->field.field =
2109 find_any_field(event, larg->field.name);
2110 if (!larg->field.field)
2111 die("field %s not found", larg->field.name);
2112 }
2113 right = eval_num_arg(data, size, event, arg->op.right);
2114 val = read_size(data + larg->field.field->offset +
2115 right * long_size, long_size);
2116 break;
2117 }
2118 default_op:
2119 left = eval_num_arg(data, size, event, arg->op.left);
2120 right = eval_num_arg(data, size, event, arg->op.right);
2121 switch (arg->op.op[0]) {
2122 case '|':
2123 if (arg->op.op[1])
2124 val = left || right;
2125 else
2126 val = left | right;
2127 break;
2128 case '&':
2129 if (arg->op.op[1])
2130 val = left && right;
2131 else
2132 val = left & right;
2133 break;
2134 case '<':
2135 switch (arg->op.op[1]) {
2136 case 0:
2137 val = left < right;
2138 break;
2139 case '<':
2140 val = left << right;
2141 break;
2142 case '=':
2143 val = left <= right;
2144 break;
2145 default:
2146 die("unknown op '%s'", arg->op.op);
2147 }
2148 break;
2149 case '>':
2150 switch (arg->op.op[1]) {
2151 case 0:
2152 val = left > right;
2153 break;
2154 case '>':
2155 val = left >> right;
2156 break;
2157 case '=':
2158 val = left >= right;
2159 break;
2160 default:
2161 die("unknown op '%s'", arg->op.op);
2162 }
2163 break;
2164 case '=':
2165 if (arg->op.op[1] != '=')
2166 die("unknown op '%s'", arg->op.op);
2167 val = left == right;
2168 break;
2169 case '-':
2170 val = left - right;
2171 break;
2172 case '+':
2173 val = left + right;
2174 break;
2175 default:
2176 die("unknown op '%s'", arg->op.op);
2177 }
2178 break;
2179 default: /* not sure what to do there */
2180 return 0;
2181 }
2182 return val;
2183}
2184
2185struct flag {
2186 const char *name;
2187 unsigned long long value;
2188};
2189
2190static const struct flag flags[] = {
2191 { "HI_SOFTIRQ", 0 },
2192 { "TIMER_SOFTIRQ", 1 },
2193 { "NET_TX_SOFTIRQ", 2 },
2194 { "NET_RX_SOFTIRQ", 3 },
2195 { "BLOCK_SOFTIRQ", 4 },
2196 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2197 { "TASKLET_SOFTIRQ", 6 },
2198 { "SCHED_SOFTIRQ", 7 },
2199 { "HRTIMER_SOFTIRQ", 8 },
2200 { "RCU_SOFTIRQ", 9 },
2201
2202 { "HRTIMER_NORESTART", 0 },
2203 { "HRTIMER_RESTART", 1 },
2204};
2205
2206unsigned long long eval_flag(const char *flag)
2207{
2208 int i;
2209
2210 /*
2211 * Some flags in the format files do not get converted.
2212 * If the flag is not numeric, see if it is something that
2213 * we already know about.
2214 */
2215 if (isdigit(flag[0]))
2216 return strtoull(flag, NULL, 0);
2217
2218 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2219 if (strcmp(flags[i].name, flag) == 0)
2220 return flags[i].value;
2221
2222 return 0;
2223}
2224
2225static void print_str_arg(void *data, int size,
2226 struct event *event, struct print_arg *arg)
2227{
2228 struct print_flag_sym *flag;
2229 unsigned long long val, fval;
2230 char *str;
2231 int print;
2232
2233 switch (arg->type) {
2234 case PRINT_NULL:
2235 /* ?? */
2236 return;
2237 case PRINT_ATOM:
2238 printf("%s", arg->atom.atom);
2239 return;
2240 case PRINT_FIELD:
2241 if (!arg->field.field) {
2242 arg->field.field = find_any_field(event, arg->field.name);
2243 if (!arg->field.field)
2244 die("field %s not found", arg->field.name);
2245 }
2246 str = malloc_or_die(arg->field.field->size + 1);
2247 memcpy(str, data + arg->field.field->offset,
2248 arg->field.field->size);
2249 str[arg->field.field->size] = 0;
2250 printf("%s", str);
2251 free(str);
2252 break;
2253 case PRINT_FLAGS:
2254 val = eval_num_arg(data, size, event, arg->flags.field);
2255 print = 0;
2256 for (flag = arg->flags.flags; flag; flag = flag->next) {
2257 fval = eval_flag(flag->value);
2258 if (!val && !fval) {
2259 printf("%s", flag->str);
2260 break;
2261 }
2262 if (fval && (val & fval) == fval) {
2263 if (print && arg->flags.delim)
2264 printf("%s", arg->flags.delim);
2265 printf("%s", flag->str);
2266 print = 1;
2267 val &= ~fval;
2268 }
2269 }
2270 break;
2271 case PRINT_SYMBOL:
2272 val = eval_num_arg(data, size, event, arg->symbol.field);
2273 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2274 fval = eval_flag(flag->value);
2275 if (val == fval) {
2276 printf("%s", flag->str);
2277 break;
2278 }
2279 }
2280 break;
2281
2282 case PRINT_TYPE:
2283 break;
2284 case PRINT_STRING: {
2285 int str_offset;
2286
2287 if (arg->string.offset == -1) {
2288 struct format_field *f;
2289
2290 f = find_any_field(event, arg->string.string);
2291 arg->string.offset = f->offset;
2292 }
2293 str_offset = *(int *)(data + arg->string.offset);
2294 str_offset &= 0xffff;
2295 printf("%s", ((char *)data) + str_offset);
2296 break;
2297 }
2298 case PRINT_OP:
2299 /*
2300 * The only op for string should be ? :
2301 */
2302 if (arg->op.op[0] != '?')
2303 return;
2304 val = eval_num_arg(data, size, event, arg->op.left);
2305 if (val)
2306 print_str_arg(data, size, event, arg->op.right->op.left);
2307 else
2308 print_str_arg(data, size, event, arg->op.right->op.right);
2309 break;
2310 default:
2311 /* well... */
2312 break;
2313 }
2314}
2315
2316static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2317{
2318 static struct format_field *field, *ip_field;
2319 struct print_arg *args, *arg, **next;
2320 unsigned long long ip, val;
2321 char *ptr;
2322 void *bptr;
2323
2324 if (!field) {
2325 field = find_field(event, "buf");
2326 if (!field)
2327 die("can't find buffer field for binary printk");
2328 ip_field = find_field(event, "ip");
2329 if (!ip_field)
2330 die("can't find ip field for binary printk");
2331 }
2332
2333 ip = read_size(data + ip_field->offset, ip_field->size);
2334
2335 /*
2336 * The first arg is the IP pointer.
2337 */
2338 args = malloc_or_die(sizeof(*args));
2339 arg = args;
2340 arg->next = NULL;
2341 next = &arg->next;
2342
2343 arg->type = PRINT_ATOM;
2344 arg->atom.atom = malloc_or_die(32);
2345 sprintf(arg->atom.atom, "%lld", ip);
2346
2347 /* skip the first "%pf : " */
2348 for (ptr = fmt + 6, bptr = data + field->offset;
2349 bptr < data + size && *ptr; ptr++) {
2350 int ls = 0;
2351
2352 if (*ptr == '%') {
2353 process_again:
2354 ptr++;
2355 switch (*ptr) {
2356 case '%':
2357 break;
2358 case 'l':
2359 ls++;
2360 goto process_again;
2361 case 'L':
2362 ls = 2;
2363 goto process_again;
2364 case '0' ... '9':
2365 goto process_again;
2366 case 'p':
2367 ls = 1;
2368 /* fall through */
2369 case 'd':
2370 case 'u':
2371 case 'x':
2372 case 'i':
2373 /* the pointers are always 4 bytes aligned */
2374 bptr = (void *)(((unsigned long)bptr + 3) &
2375 ~3);
2376 switch (ls) {
2377 case 0:
2378 case 1:
2379 ls = long_size;
2380 break;
2381 case 2:
2382 ls = 8;
2383 default:
2384 break;
2385 }
2386 val = read_size(bptr, ls);
2387 bptr += ls;
2388 arg = malloc_or_die(sizeof(*arg));
2389 arg->next = NULL;
2390 arg->type = PRINT_ATOM;
2391 arg->atom.atom = malloc_or_die(32);
2392 sprintf(arg->atom.atom, "%lld", val);
2393 *next = arg;
2394 next = &arg->next;
2395 break;
2396 case 's':
2397 arg = malloc_or_die(sizeof(*arg));
2398 arg->next = NULL;
2399 arg->type = PRINT_STRING;
2400 arg->string.string = strdup(bptr);
2401 bptr += strlen(bptr) + 1;
2402 *next = arg;
2403 next = &arg->next;
2404 default:
2405 break;
2406 }
2407 }
2408 }
2409
2410 return args;
2411}
2412
2413static void free_args(struct print_arg *args)
2414{
2415 struct print_arg *next;
2416
2417 while (args) {
2418 next = args->next;
2419
2420 if (args->type == PRINT_ATOM)
2421 free(args->atom.atom);
2422 else
2423 free(args->string.string);
2424 free(args);
2425 args = next;
2426 }
2427}
2428
2429static char *get_bprint_format(void *data, int size __unused, struct event *event)
2430{
2431 unsigned long long addr;
2432 static struct format_field *field;
2433 struct printk_map *printk;
2434 char *format;
2435 char *p;
2436
2437 if (!field) {
2438 field = find_field(event, "fmt");
2439 if (!field)
2440 die("can't find format field for binary printk");
2441 printf("field->offset = %d size=%d\n", field->offset, field->size);
2442 }
2443
2444 addr = read_size(data + field->offset, field->size);
2445
2446 printk = find_printk(addr);
2447 if (!printk) {
2448 format = malloc_or_die(45);
2449 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2450 addr);
2451 return format;
2452 }
2453
2454 p = printk->printk;
2455 /* Remove any quotes. */
2456 if (*p == '"')
2457 p++;
2458 format = malloc_or_die(strlen(p) + 10);
2459 sprintf(format, "%s : %s", "%pf", p);
2460 /* remove ending quotes and new line since we will add one too */
2461 p = format + strlen(format) - 1;
2462 if (*p == '"')
2463 *p = 0;
2464
2465 p -= 2;
2466 if (strcmp(p, "\\n") == 0)
2467 *p = 0;
2468
2469 return format;
2470}
2471
2472static void pretty_print(void *data, int size, struct event *event)
2473{
2474 struct print_fmt *print_fmt = &event->print_fmt;
2475 struct print_arg *arg = print_fmt->args;
2476 struct print_arg *args = NULL;
2477 const char *ptr = print_fmt->format;
2478 unsigned long long val;
2479 struct func_map *func;
2480 const char *saveptr;
2481 char *bprint_fmt = NULL;
2482 char format[32];
2483 int show_func;
2484 int len;
2485 int ls;
2486
2487 if (event->flags & EVENT_FL_ISFUNC)
2488 ptr = " %pF <-- %pF";
2489
2490 if (event->flags & EVENT_FL_ISBPRINT) {
2491 bprint_fmt = get_bprint_format(data, size, event);
2492 args = make_bprint_args(bprint_fmt, data, size, event);
2493 arg = args;
2494 ptr = bprint_fmt;
2495 }
2496
2497 for (; *ptr; ptr++) {
2498 ls = 0;
2499 if (*ptr == '\\') {
2500 ptr++;
2501 switch (*ptr) {
2502 case 'n':
2503 printf("\n");
2504 break;
2505 case 't':
2506 printf("\t");
2507 break;
2508 case 'r':
2509 printf("\r");
2510 break;
2511 case '\\':
2512 printf("\\");
2513 break;
2514 default:
2515 printf("%c", *ptr);
2516 break;
2517 }
2518
2519 } else if (*ptr == '%') {
2520 saveptr = ptr;
2521 show_func = 0;
2522 cont_process:
2523 ptr++;
2524 switch (*ptr) {
2525 case '%':
2526 printf("%%");
2527 break;
2528 case 'l':
2529 ls++;
2530 goto cont_process;
2531 case 'L':
2532 ls = 2;
2533 goto cont_process;
2534 case 'z':
2535 case 'Z':
2536 case '0' ... '9':
2537 goto cont_process;
2538 case 'p':
2539 if (long_size == 4)
2540 ls = 1;
2541 else
2542 ls = 2;
2543
2544 if (*(ptr+1) == 'F' ||
2545 *(ptr+1) == 'f') {
2546 ptr++;
2547 show_func = *ptr;
2548 }
2549
2550 /* fall through */
2551 case 'd':
2552 case 'i':
2553 case 'x':
2554 case 'X':
2555 case 'u':
2556 if (!arg)
2557 die("no argument match");
2558
2559 len = ((unsigned long)ptr + 1) -
2560 (unsigned long)saveptr;
2561
2562 /* should never happen */
2563 if (len > 32)
2564 die("bad format!");
2565
2566 memcpy(format, saveptr, len);
2567 format[len] = 0;
2568
2569 val = eval_num_arg(data, size, event, arg);
2570 arg = arg->next;
2571
2572 if (show_func) {
2573 func = find_func(val);
2574 if (func) {
2575 printf("%s", func->func);
2576 if (show_func == 'F')
2577 printf("+0x%llx",
2578 val - func->addr);
2579 break;
2580 }
2581 }
2582 switch (ls) {
2583 case 0:
2584 printf(format, (int)val);
2585 break;
2586 case 1:
2587 printf(format, (long)val);
2588 break;
2589 case 2:
2590 printf(format, (long long)val);
2591 break;
2592 default:
2593 die("bad count (%d)", ls);
2594 }
2595 break;
2596 case 's':
2597 if (!arg)
2598 die("no matching argument");
2599
2600 print_str_arg(data, size, event, arg);
2601 arg = arg->next;
2602 break;
2603 default:
2604 printf(">%c<", *ptr);
2605
2606 }
2607 } else
2608 printf("%c", *ptr);
2609 }
2610
2611 if (args) {
2612 free_args(args);
2613 free(bprint_fmt);
2614 }
2615}
2616
2617static inline int log10_cpu(int nb)
2618{
2619 if (nb / 100)
2620 return 3;
2621 if (nb / 10)
2622 return 2;
2623 return 1;
2624}
2625
2626static void print_lat_fmt(void *data, int size __unused)
2627{
2628 unsigned int lat_flags;
2629 unsigned int pc;
2630 int lock_depth;
2631 int hardirq;
2632 int softirq;
2633
2634 lat_flags = parse_common_flags(data);
2635 pc = parse_common_pc(data);
2636 lock_depth = parse_common_lock_depth(data);
2637
2638 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2639 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2640
2641 printf("%c%c%c",
2642 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2643 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2644 'X' : '.',
2645 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2646 'N' : '.',
2647 (hardirq && softirq) ? 'H' :
2648 hardirq ? 'h' : softirq ? 's' : '.');
2649
2650 if (pc)
2651 printf("%x", pc);
2652 else
2653 printf(".");
2654
2655 if (lock_depth < 0)
2656 printf(". ");
2657 else
2658 printf("%d ", lock_depth);
2659}
2660
2661#define TRACE_GRAPH_INDENT 2
2662
2663static struct record *
2664get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2665 struct record *next)
2666{
2667 struct format_field *field;
2668 struct event *event;
2669 unsigned long val;
2670 int type;
2671 int pid;
2672
2673 type = trace_parse_common_type(next->data);
2674 event = trace_find_event(type);
2675 if (!event)
2676 return NULL;
2677
2678 if (!(event->flags & EVENT_FL_ISFUNCRET))
2679 return NULL;
2680
2681 pid = trace_parse_common_pid(next->data);
2682 field = find_field(event, "func");
2683 if (!field)
2684 die("function return does not have field func");
2685
2686 val = read_size(next->data + field->offset, field->size);
2687
2688 if (cur_pid != pid || cur_func != val)
2689 return NULL;
2690
2691 /* this is a leaf, now advance the iterator */
2692 return trace_read_data(cpu);
2693}
2694
2695/* Signal a overhead of time execution to the output */
2696static void print_graph_overhead(unsigned long long duration)
2697{
2698 /* Non nested entry or return */
2699 if (duration == ~0ULL)
2700 return (void)printf(" ");
2701
2702 /* Duration exceeded 100 msecs */
2703 if (duration > 100000ULL)
2704 return (void)printf("! ");
2705
2706 /* Duration exceeded 10 msecs */
2707 if (duration > 10000ULL)
2708 return (void)printf("+ ");
2709
2710 printf(" ");
2711}
2712
2713static void print_graph_duration(unsigned long long duration)
2714{
2715 unsigned long usecs = duration / 1000;
2716 unsigned long nsecs_rem = duration % 1000;
2717 /* log10(ULONG_MAX) + '\0' */
2718 char msecs_str[21];
2719 char nsecs_str[5];
2720 int len;
2721 int i;
2722
2723 sprintf(msecs_str, "%lu", usecs);
2724
2725 /* Print msecs */
2726 len = printf("%lu", usecs);
2727
2728 /* Print nsecs (we don't want to exceed 7 numbers) */
2729 if (len < 7) {
2730 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2731 len += printf(".%s", nsecs_str);
2732 }
2733
2734 printf(" us ");
2735
2736 /* Print remaining spaces to fit the row's width */
2737 for (i = len; i < 7; i++)
2738 printf(" ");
2739
2740 printf("| ");
2741}
2742
2743static void
2744print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2745{
2746 unsigned long long rettime, calltime;
2747 unsigned long long duration, depth;
2748 unsigned long long val;
2749 struct format_field *field;
2750 struct func_map *func;
2751 struct event *ret_event;
2752 int type;
2753 int i;
2754
2755 type = trace_parse_common_type(ret_rec->data);
2756 ret_event = trace_find_event(type);
2757
2758 field = find_field(ret_event, "rettime");
2759 if (!field)
2760 die("can't find rettime in return graph");
2761 rettime = read_size(ret_rec->data + field->offset, field->size);
2762
2763 field = find_field(ret_event, "calltime");
2764 if (!field)
2765 die("can't find rettime in return graph");
2766 calltime = read_size(ret_rec->data + field->offset, field->size);
2767
2768 duration = rettime - calltime;
2769
2770 /* Overhead */
2771 print_graph_overhead(duration);
2772
2773 /* Duration */
2774 print_graph_duration(duration);
2775
2776 field = find_field(event, "depth");
2777 if (!field)
2778 die("can't find depth in entry graph");
2779 depth = read_size(data + field->offset, field->size);
2780
2781 /* Function */
2782 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2783 printf(" ");
2784
2785 field = find_field(event, "func");
2786 if (!field)
2787 die("can't find func in entry graph");
2788 val = read_size(data + field->offset, field->size);
2789 func = find_func(val);
2790
2791 if (func)
2792 printf("%s();", func->func);
2793 else
2794 printf("%llx();", val);
2795}
2796
2797static void print_graph_nested(struct event *event, void *data)
2798{
2799 struct format_field *field;
2800 unsigned long long depth;
2801 unsigned long long val;
2802 struct func_map *func;
2803 int i;
2804
2805 /* No overhead */
2806 print_graph_overhead(-1);
2807
2808 /* No time */
2809 printf(" | ");
2810
2811 field = find_field(event, "depth");
2812 if (!field)
2813 die("can't find depth in entry graph");
2814 depth = read_size(data + field->offset, field->size);
2815
2816 /* Function */
2817 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2818 printf(" ");
2819
2820 field = find_field(event, "func");
2821 if (!field)
2822 die("can't find func in entry graph");
2823 val = read_size(data + field->offset, field->size);
2824 func = find_func(val);
2825
2826 if (func)
2827 printf("%s() {", func->func);
2828 else
2829 printf("%llx() {", val);
2830}
2831
2832static void
2833pretty_print_func_ent(void *data, int size, struct event *event,
2834 int cpu, int pid)
2835{
2836 struct format_field *field;
2837 struct record *rec;
2838 void *copy_data;
2839 unsigned long val;
2840
2841 if (latency_format) {
2842 print_lat_fmt(data, size);
2843 printf(" | ");
2844 }
2845
2846 field = find_field(event, "func");
2847 if (!field)
2848 die("function entry does not have func field");
2849
2850 val = read_size(data + field->offset, field->size);
2851
2852 /*
2853 * peek_data may unmap the data pointer. Copy it first.
2854 */
2855 copy_data = malloc_or_die(size);
2856 memcpy(copy_data, data, size);
2857 data = copy_data;
2858
2859 rec = trace_peek_data(cpu);
2860 if (rec) {
2861 rec = get_return_for_leaf(cpu, pid, val, rec);
2862 if (rec) {
2863 print_graph_entry_leaf(event, data, rec);
2864 goto out_free;
2865 }
2866 }
2867 print_graph_nested(event, data);
2868out_free:
2869 free(data);
2870}
2871
2872static void
2873pretty_print_func_ret(void *data, int size __unused, struct event *event)
2874{
2875 unsigned long long rettime, calltime;
2876 unsigned long long duration, depth;
2877 struct format_field *field;
2878 int i;
2879
2880 if (latency_format) {
2881 print_lat_fmt(data, size);
2882 printf(" | ");
2883 }
2884
2885 field = find_field(event, "rettime");
2886 if (!field)
2887 die("can't find rettime in return graph");
2888 rettime = read_size(data + field->offset, field->size);
2889
2890 field = find_field(event, "calltime");
2891 if (!field)
2892 die("can't find calltime in return graph");
2893 calltime = read_size(data + field->offset, field->size);
2894
2895 duration = rettime - calltime;
2896
2897 /* Overhead */
2898 print_graph_overhead(duration);
2899
2900 /* Duration */
2901 print_graph_duration(duration);
2902
2903 field = find_field(event, "depth");
2904 if (!field)
2905 die("can't find depth in entry graph");
2906 depth = read_size(data + field->offset, field->size);
2907
2908 /* Function */
2909 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2910 printf(" ");
2911
2912 printf("}");
2913}
2914
2915static void
2916pretty_print_func_graph(void *data, int size, struct event *event,
2917 int cpu, int pid)
2918{
2919 if (event->flags & EVENT_FL_ISFUNCENT)
2920 pretty_print_func_ent(data, size, event, cpu, pid);
2921 else if (event->flags & EVENT_FL_ISFUNCRET)
2922 pretty_print_func_ret(data, size, event);
2923 printf("\n");
2924}
2925
2926void print_trace_event(int cpu, void *data, int size)
2927{
2928 struct event *event;
2929 int type;
2930 int pid;
2931
2932 type = trace_parse_common_type(data);
2933
2934 event = trace_find_event(type);
2935 if (!event) {
2936 warning("ug! no event found for type %d", type);
2937 return;
2938 }
2939
2940 pid = trace_parse_common_pid(data);
2941
2942 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2943 return pretty_print_func_graph(data, size, event, cpu, pid);
2944
2945 if (latency_format)
2946 print_lat_fmt(data, size);
2947
2948 if (event->flags & EVENT_FL_FAILED) {
2949 printf("EVENT '%s' FAILED TO PARSE\n",
2950 event->name);
2951 return;
2952 }
2953
2954 pretty_print(data, size, event);
2955}
2956
2957static void print_fields(struct print_flag_sym *field)
2958{
2959 printf("{ %s, %s }", field->value, field->str);
2960 if (field->next) {
2961 printf(", ");
2962 print_fields(field->next);
2963 }
2964}
2965
2966static void print_args(struct print_arg *args)
2967{
2968 int print_paren = 1;
2969
2970 switch (args->type) {
2971 case PRINT_NULL:
2972 printf("null");
2973 break;
2974 case PRINT_ATOM:
2975 printf("%s", args->atom.atom);
2976 break;
2977 case PRINT_FIELD:
2978 printf("REC->%s", args->field.name);
2979 break;
2980 case PRINT_FLAGS:
2981 printf("__print_flags(");
2982 print_args(args->flags.field);
2983 printf(", %s, ", args->flags.delim);
2984 print_fields(args->flags.flags);
2985 printf(")");
2986 break;
2987 case PRINT_SYMBOL:
2988 printf("__print_symbolic(");
2989 print_args(args->symbol.field);
2990 printf(", ");
2991 print_fields(args->symbol.symbols);
2992 printf(")");
2993 break;
2994 case PRINT_STRING:
2995 printf("__get_str(%s)", args->string.string);
2996 break;
2997 case PRINT_TYPE:
2998 printf("(%s)", args->typecast.type);
2999 print_args(args->typecast.item);
3000 break;
3001 case PRINT_OP:
3002 if (strcmp(args->op.op, ":") == 0)
3003 print_paren = 0;
3004 if (print_paren)
3005 printf("(");
3006 print_args(args->op.left);
3007 printf(" %s ", args->op.op);
3008 print_args(args->op.right);
3009 if (print_paren)
3010 printf(")");
3011 break;
3012 default:
3013 /* we should warn... */
3014 return;
3015 }
3016 if (args->next) {
3017 printf("\n");
3018 print_args(args->next);
3019 }
3020}
3021
3022int parse_ftrace_file(char *buf, unsigned long size)
3023{
3024 struct format_field *field;
3025 struct print_arg *arg, **list;
3026 struct event *event;
3027 int ret;
3028
3029 init_input_buf(buf, size);
3030
3031 event = alloc_event();
3032 if (!event)
3033 return -ENOMEM;
3034
3035 event->flags |= EVENT_FL_ISFTRACE;
3036
3037 event->name = event_read_name();
3038 if (!event->name)
3039 die("failed to read ftrace event name");
3040
3041 if (strcmp(event->name, "function") == 0)
3042 event->flags |= EVENT_FL_ISFUNC;
3043
3044 else if (strcmp(event->name, "funcgraph_entry") == 0)
3045 event->flags |= EVENT_FL_ISFUNCENT;
3046
3047 else if (strcmp(event->name, "funcgraph_exit") == 0)
3048 event->flags |= EVENT_FL_ISFUNCRET;
3049
3050 else if (strcmp(event->name, "bprint") == 0)
3051 event->flags |= EVENT_FL_ISBPRINT;
3052
3053 event->id = event_read_id();
3054 if (event->id < 0)
3055 die("failed to read ftrace event id");
3056
3057 add_event(event);
3058
3059 ret = event_read_format(event);
3060 if (ret < 0)
3061 die("failed to read ftrace event format");
3062
3063 ret = event_read_print(event);
3064 if (ret < 0)
3065 die("failed to read ftrace event print fmt");
3066
3067 /* New ftrace handles args */
3068 if (ret > 0)
3069 return 0;
3070 /*
3071 * The arguments for ftrace files are parsed by the fields.
3072 * Set up the fields as their arguments.
3073 */
3074 list = &event->print_fmt.args;
3075 for (field = event->format.fields; field; field = field->next) {
3076 arg = malloc_or_die(sizeof(*arg));
3077 memset(arg, 0, sizeof(*arg));
3078 *list = arg;
3079 list = &arg->next;
3080 arg->type = PRINT_FIELD;
3081 arg->field.name = field->name;
3082 arg->field.field = field;
3083 }
3084 return 0;
3085}
3086
3087int parse_event_file(char *buf, unsigned long size, char *sys)
3088{
3089 struct event *event;
3090 int ret;
3091
3092 init_input_buf(buf, size);
3093
3094 event = alloc_event();
3095 if (!event)
3096 return -ENOMEM;
3097
3098 event->name = event_read_name();
3099 if (!event->name)
3100 die("failed to read event name");
3101
3102 event->id = event_read_id();
3103 if (event->id < 0)
3104 die("failed to read event id");
3105
3106 ret = event_read_format(event);
3107 if (ret < 0) {
3108 warning("failed to read event format for %s", event->name);
3109 goto event_failed;
3110 }
3111
3112 ret = event_read_print(event);
3113 if (ret < 0) {
3114 warning("failed to read event print fmt for %s", event->name);
3115 goto event_failed;
3116 }
3117
3118 event->system = strdup(sys);
3119
3120#define PRINT_ARGS 0
3121 if (PRINT_ARGS && event->print_fmt.args)
3122 print_args(event->print_fmt.args);
3123
3124 add_event(event);
3125 return 0;
3126
3127 event_failed:
3128 event->flags |= EVENT_FL_FAILED;
3129 /* still add it even if it failed */
3130 add_event(event);
3131 return -1;
3132}
3133
3134void parse_set_info(int nr_cpus, int long_sz)
3135{
3136 cpus = nr_cpus;
3137 long_size = long_sz;
3138}
3139
3140int common_pc(struct scripting_context *context) 5int common_pc(struct scripting_context *context)
3141{ 6{
3142 return parse_common_pc(context->event_data); 7 return parse_common_pc(context->event_data);
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 58ae14c5baac..e78ef1e10ee1 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -1,271 +1,15 @@
1#ifndef __PERF_TRACE_EVENTS_H 1#ifndef _PERF_UTIL_TRACE_EVENT_H
2#define __PERF_TRACE_EVENTS_H 2#define _PERF_UTIL_TRACE_EVENT_H
3 3
4#include <stdbool.h>
5#include "parse-events.h" 4#include "parse-events.h"
5#include "trace-parse-events.h"
6#include "session.h"
6 7
7struct machine; 8struct machine;
8struct perf_sample; 9struct perf_sample;
9union perf_event; 10union perf_event;
10struct thread; 11struct thread;
11 12
12#define __unused __attribute__((unused))
13
14
15#ifndef PAGE_MASK
16#define PAGE_MASK (page_size - 1)
17#endif
18
19enum {
20 RINGBUF_TYPE_PADDING = 29,
21 RINGBUF_TYPE_TIME_EXTEND = 30,
22 RINGBUF_TYPE_TIME_STAMP = 31,
23};
24
25#ifndef TS_SHIFT
26#define TS_SHIFT 27
27#endif
28
29#define NSECS_PER_SEC 1000000000ULL
30#define NSECS_PER_USEC 1000ULL
31
32enum format_flags {
33 FIELD_IS_ARRAY = 1,
34 FIELD_IS_POINTER = 2,
35 FIELD_IS_SIGNED = 4,
36 FIELD_IS_STRING = 8,
37 FIELD_IS_DYNAMIC = 16,
38 FIELD_IS_FLAG = 32,
39 FIELD_IS_SYMBOLIC = 64,
40};
41
42struct format_field {
43 struct format_field *next;
44 char *type;
45 char *name;
46 int offset;
47 int size;
48 unsigned long flags;
49};
50
51struct format {
52 int nr_common;
53 int nr_fields;
54 struct format_field *common_fields;
55 struct format_field *fields;
56};
57
58struct print_arg_atom {
59 char *atom;
60};
61
62struct print_arg_string {
63 char *string;
64 int offset;
65};
66
67struct print_arg_field {
68 char *name;
69 struct format_field *field;
70};
71
72struct print_flag_sym {
73 struct print_flag_sym *next;
74 char *value;
75 char *str;
76};
77
78struct print_arg_typecast {
79 char *type;
80 struct print_arg *item;
81};
82
83struct print_arg_flags {
84 struct print_arg *field;
85 char *delim;
86 struct print_flag_sym *flags;
87};
88
89struct print_arg_symbol {
90 struct print_arg *field;
91 struct print_flag_sym *symbols;
92};
93
94struct print_arg;
95
96struct print_arg_op {
97 char *op;
98 int prio;
99 struct print_arg *left;
100 struct print_arg *right;
101};
102
103struct print_arg_func {
104 char *name;
105 struct print_arg *args;
106};
107
108enum print_arg_type {
109 PRINT_NULL,
110 PRINT_ATOM,
111 PRINT_FIELD,
112 PRINT_FLAGS,
113 PRINT_SYMBOL,
114 PRINT_TYPE,
115 PRINT_STRING,
116 PRINT_OP,
117};
118
119struct print_arg {
120 struct print_arg *next;
121 enum print_arg_type type;
122 union {
123 struct print_arg_atom atom;
124 struct print_arg_field field;
125 struct print_arg_typecast typecast;
126 struct print_arg_flags flags;
127 struct print_arg_symbol symbol;
128 struct print_arg_func func;
129 struct print_arg_string string;
130 struct print_arg_op op;
131 };
132};
133
134struct print_fmt {
135 char *format;
136 struct print_arg *args;
137};
138
139struct event {
140 struct event *next;
141 char *name;
142 int id;
143 int flags;
144 struct format format;
145 struct print_fmt print_fmt;
146 char *system;
147};
148
149enum {
150 EVENT_FL_ISFTRACE = 0x01,
151 EVENT_FL_ISPRINT = 0x02,
152 EVENT_FL_ISBPRINT = 0x04,
153 EVENT_FL_ISFUNC = 0x08,
154 EVENT_FL_ISFUNCENT = 0x10,
155 EVENT_FL_ISFUNCRET = 0x20,
156
157 EVENT_FL_FAILED = 0x80000000
158};
159
160struct record {
161 unsigned long long ts;
162 int size;
163 void *data;
164};
165
166struct record *trace_peek_data(int cpu);
167struct record *trace_read_data(int cpu);
168
169void parse_set_info(int nr_cpus, int long_sz);
170
171ssize_t trace_report(int fd, bool repipe);
172
173void *malloc_or_die(unsigned int size);
174
175void parse_cmdlines(char *file, int size);
176void parse_proc_kallsyms(char *file, unsigned int size);
177void parse_ftrace_printk(char *file, unsigned int size);
178
179void print_funcs(void);
180void print_printk(void);
181
182int parse_ftrace_file(char *buf, unsigned long size);
183int parse_event_file(char *buf, unsigned long size, char *sys);
184void print_trace_event(int cpu, void *data, int size);
185
186extern int file_bigendian;
187extern int host_bigendian;
188
189int bigendian(void);
190
191static inline unsigned short __data2host2(unsigned short data)
192{
193 unsigned short swap;
194
195 if (host_bigendian == file_bigendian)
196 return data;
197
198 swap = ((data & 0xffULL) << 8) |
199 ((data & (0xffULL << 8)) >> 8);
200
201 return swap;
202}
203
204static inline unsigned int __data2host4(unsigned int data)
205{
206 unsigned int swap;
207
208 if (host_bigendian == file_bigendian)
209 return data;
210
211 swap = ((data & 0xffULL) << 24) |
212 ((data & (0xffULL << 8)) << 8) |
213 ((data & (0xffULL << 16)) >> 8) |
214 ((data & (0xffULL << 24)) >> 24);
215
216 return swap;
217}
218
219static inline unsigned long long __data2host8(unsigned long long data)
220{
221 unsigned long long swap;
222
223 if (host_bigendian == file_bigendian)
224 return data;
225
226 swap = ((data & 0xffULL) << 56) |
227 ((data & (0xffULL << 8)) << 40) |
228 ((data & (0xffULL << 16)) << 24) |
229 ((data & (0xffULL << 24)) << 8) |
230 ((data & (0xffULL << 32)) >> 8) |
231 ((data & (0xffULL << 40)) >> 24) |
232 ((data & (0xffULL << 48)) >> 40) |
233 ((data & (0xffULL << 56)) >> 56);
234
235 return swap;
236}
237
238#define data2host2(ptr) __data2host2(*(unsigned short *)ptr)
239#define data2host4(ptr) __data2host4(*(unsigned int *)ptr)
240#define data2host8(ptr) ({ \
241 unsigned long long __val; \
242 \
243 memcpy(&__val, (ptr), sizeof(unsigned long long)); \
244 __data2host8(__val); \
245})
246
247extern int header_page_ts_offset;
248extern int header_page_ts_size;
249extern int header_page_size_offset;
250extern int header_page_size_size;
251extern int header_page_data_offset;
252extern int header_page_data_size;
253
254extern bool latency_format;
255
256int trace_parse_common_type(void *data);
257int trace_parse_common_pid(void *data);
258int parse_common_pc(void *data);
259int parse_common_flags(void *data);
260int parse_common_lock_depth(void *data);
261struct event *trace_find_event(int id);
262struct event *trace_find_next_event(struct event *event);
263unsigned long long read_size(void *ptr, int size);
264unsigned long long
265raw_field_value(struct event *event, const char *name, void *data);
266void *raw_field_ptr(struct event *event, const char *name, void *data);
267unsigned long long eval_flag(const char *flag);
268
269int read_tracing_data(int fd, struct list_head *pattrs); 13int read_tracing_data(int fd, struct list_head *pattrs);
270 14
271struct tracing_data { 15struct tracing_data {
@@ -280,15 +24,6 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
280void tracing_data_put(struct tracing_data *tdata); 24void tracing_data_put(struct tracing_data *tdata);
281 25
282 26
283/* taken from kernel/trace/trace.h */
284enum trace_flag_type {
285 TRACE_FLAG_IRQS_OFF = 0x01,
286 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
287 TRACE_FLAG_NEED_RESCHED = 0x04,
288 TRACE_FLAG_HARDIRQ = 0x08,
289 TRACE_FLAG_SOFTIRQ = 0x10,
290};
291
292struct scripting_ops { 27struct scripting_ops {
293 const char *name; 28 const char *name;
294 int (*start_script) (const char *script, int argc, const char **argv); 29 int (*start_script) (const char *script, int argc, const char **argv);
@@ -314,4 +49,4 @@ int common_pc(struct scripting_context *context);
314int common_flags(struct scripting_context *context); 49int common_flags(struct scripting_context *context);
315int common_lock_depth(struct scripting_context *context); 50int common_lock_depth(struct scripting_context *context);
316 51
317#endif /* __PERF_TRACE_EVENTS_H */ 52#endif /* _PERF_UTIL_TRACE_EVENT_H */
diff --git a/tools/perf/util/trace-parse-events.c b/tools/perf/util/trace-parse-events.c
new file mode 100644
index 000000000000..8a3fbe643a1c
--- /dev/null
+++ b/tools/perf/util/trace-parse-events.c
@@ -0,0 +1,3125 @@
1/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 *
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
23 */
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
28#include <errno.h>
29
30#include "../perf.h"
31#include "util.h"
32#include "trace-parse-events.h"
33
34int header_page_ts_offset;
35int header_page_ts_size;
36int header_page_size_offset;
37int header_page_size_size;
38int header_page_overwrite_offset;
39int header_page_overwrite_size;
40int header_page_data_offset;
41int header_page_data_size;
42
43bool latency_format;
44
45static char *input_buf;
46static unsigned long long input_buf_ptr;
47static unsigned long long input_buf_siz;
48
49static int cpus;
50static int long_size;
51static int is_flag_field;
52static int is_symbolic_field;
53
54static struct format_field *
55find_any_field(struct event *event, const char *name);
56
57static void init_input_buf(char *buf, unsigned long long size)
58{
59 input_buf = buf;
60 input_buf_siz = size;
61 input_buf_ptr = 0;
62}
63
64struct cmdline {
65 char *comm;
66 int pid;
67};
68
69static struct cmdline *cmdlines;
70static int cmdline_count;
71
72static int cmdline_cmp(const void *a, const void *b)
73{
74 const struct cmdline *ca = a;
75 const struct cmdline *cb = b;
76
77 if (ca->pid < cb->pid)
78 return -1;
79 if (ca->pid > cb->pid)
80 return 1;
81
82 return 0;
83}
84
85void parse_cmdlines(char *file, int size __unused)
86{
87 struct cmdline_list {
88 struct cmdline_list *next;
89 char *comm;
90 int pid;
91 } *list = NULL, *item;
92 char *line;
93 char *next = NULL;
94 int i;
95
96 line = strtok_r(file, "\n", &next);
97 while (line) {
98 item = malloc_or_die(sizeof(*item));
99 sscanf(line, "%d %as", &item->pid,
100 (float *)(void *)&item->comm); /* workaround gcc warning */
101 item->next = list;
102 list = item;
103 line = strtok_r(NULL, "\n", &next);
104 cmdline_count++;
105 }
106
107 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
108
109 i = 0;
110 while (list) {
111 cmdlines[i].pid = list->pid;
112 cmdlines[i].comm = list->comm;
113 i++;
114 item = list;
115 list = list->next;
116 free(item);
117 }
118
119 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
120}
121
122static struct func_map {
123 unsigned long long addr;
124 char *func;
125 char *mod;
126} *func_list;
127static unsigned int func_count;
128
129static int func_cmp(const void *a, const void *b)
130{
131 const struct func_map *fa = a;
132 const struct func_map *fb = b;
133
134 if (fa->addr < fb->addr)
135 return -1;
136 if (fa->addr > fb->addr)
137 return 1;
138
139 return 0;
140}
141
142void parse_proc_kallsyms(char *file, unsigned int size __unused)
143{
144 struct func_list {
145 struct func_list *next;
146 unsigned long long addr;
147 char *func;
148 char *mod;
149 } *list = NULL, *item;
150 char *line;
151 char *next = NULL;
152 char *addr_str;
153 char ch;
154 int i;
155
156 line = strtok_r(file, "\n", &next);
157 while (line) {
158 item = malloc_or_die(sizeof(*item));
159 item->mod = NULL;
160 sscanf(line, "%as %c %as\t[%as",
161 (float *)(void *)&addr_str, /* workaround gcc warning */
162 &ch,
163 (float *)(void *)&item->func,
164 (float *)(void *)&item->mod);
165 item->addr = strtoull(addr_str, NULL, 16);
166 free(addr_str);
167
168 /* truncate the extra ']' */
169 if (item->mod)
170 item->mod[strlen(item->mod) - 1] = 0;
171
172
173 item->next = list;
174 list = item;
175 line = strtok_r(NULL, "\n", &next);
176 func_count++;
177 }
178
179 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
180
181 i = 0;
182 while (list) {
183 func_list[i].func = list->func;
184 func_list[i].addr = list->addr;
185 func_list[i].mod = list->mod;
186 i++;
187 item = list;
188 list = list->next;
189 free(item);
190 }
191
192 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
193
194 /*
195 * Add a special record at the end.
196 */
197 func_list[func_count].func = NULL;
198 func_list[func_count].addr = 0;
199 func_list[func_count].mod = NULL;
200}
201
202/*
203 * We are searching for a record in between, not an exact
204 * match.
205 */
206static int func_bcmp(const void *a, const void *b)
207{
208 const struct func_map *fa = a;
209 const struct func_map *fb = b;
210
211 if ((fa->addr == fb->addr) ||
212
213 (fa->addr > fb->addr &&
214 fa->addr < (fb+1)->addr))
215 return 0;
216
217 if (fa->addr < fb->addr)
218 return -1;
219
220 return 1;
221}
222
223static struct func_map *find_func(unsigned long long addr)
224{
225 struct func_map *func;
226 struct func_map key;
227
228 key.addr = addr;
229
230 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
231 func_bcmp);
232
233 return func;
234}
235
236void print_funcs(void)
237{
238 int i;
239
240 for (i = 0; i < (int)func_count; i++) {
241 printf("%016llx %s",
242 func_list[i].addr,
243 func_list[i].func);
244 if (func_list[i].mod)
245 printf(" [%s]\n", func_list[i].mod);
246 else
247 printf("\n");
248 }
249}
250
251static struct printk_map {
252 unsigned long long addr;
253 char *printk;
254} *printk_list;
255static unsigned int printk_count;
256
257static int printk_cmp(const void *a, const void *b)
258{
259 const struct func_map *fa = a;
260 const struct func_map *fb = b;
261
262 if (fa->addr < fb->addr)
263 return -1;
264 if (fa->addr > fb->addr)
265 return 1;
266
267 return 0;
268}
269
270static struct printk_map *find_printk(unsigned long long addr)
271{
272 struct printk_map *printk;
273 struct printk_map key;
274
275 key.addr = addr;
276
277 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
278 printk_cmp);
279
280 return printk;
281}
282
283void parse_ftrace_printk(char *file, unsigned int size __unused)
284{
285 struct printk_list {
286 struct printk_list *next;
287 unsigned long long addr;
288 char *printk;
289 } *list = NULL, *item;
290 char *line;
291 char *next = NULL;
292 char *addr_str;
293 int i;
294
295 line = strtok_r(file, "\n", &next);
296 while (line) {
297 addr_str = strsep(&line, ":");
298 if (!line) {
299 warning("error parsing print strings");
300 break;
301 }
302 item = malloc_or_die(sizeof(*item));
303 item->addr = strtoull(addr_str, NULL, 16);
304 /* fmt still has a space, skip it */
305 item->printk = strdup(line+1);
306 item->next = list;
307 list = item;
308 line = strtok_r(NULL, "\n", &next);
309 printk_count++;
310 }
311
312 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
313
314 i = 0;
315 while (list) {
316 printk_list[i].printk = list->printk;
317 printk_list[i].addr = list->addr;
318 i++;
319 item = list;
320 list = list->next;
321 free(item);
322 }
323
324 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
325}
326
327void print_printk(void)
328{
329 int i;
330
331 for (i = 0; i < (int)printk_count; i++) {
332 printf("%016llx %s\n",
333 printk_list[i].addr,
334 printk_list[i].printk);
335 }
336}
337
338static struct event *alloc_event(void)
339{
340 struct event *event;
341
342 event = malloc_or_die(sizeof(*event));
343 memset(event, 0, sizeof(*event));
344
345 return event;
346}
347
348enum event_type {
349 EVENT_ERROR,
350 EVENT_NONE,
351 EVENT_SPACE,
352 EVENT_NEWLINE,
353 EVENT_OP,
354 EVENT_DELIM,
355 EVENT_ITEM,
356 EVENT_DQUOTE,
357 EVENT_SQUOTE,
358};
359
360static struct event *event_list;
361
362static void add_event(struct event *event)
363{
364 event->next = event_list;
365 event_list = event;
366}
367
368static int event_item_type(enum event_type type)
369{
370 switch (type) {
371 case EVENT_ITEM ... EVENT_SQUOTE:
372 return 1;
373 case EVENT_ERROR ... EVENT_DELIM:
374 default:
375 return 0;
376 }
377}
378
379static void free_arg(struct print_arg *arg)
380{
381 if (!arg)
382 return;
383
384 switch (arg->type) {
385 case PRINT_ATOM:
386 if (arg->atom.atom)
387 free(arg->atom.atom);
388 break;
389 case PRINT_NULL:
390 case PRINT_FIELD ... PRINT_OP:
391 default:
392 /* todo */
393 break;
394 }
395
396 free(arg);
397}
398
399static enum event_type get_type(int ch)
400{
401 if (ch == '\n')
402 return EVENT_NEWLINE;
403 if (isspace(ch))
404 return EVENT_SPACE;
405 if (isalnum(ch) || ch == '_')
406 return EVENT_ITEM;
407 if (ch == '\'')
408 return EVENT_SQUOTE;
409 if (ch == '"')
410 return EVENT_DQUOTE;
411 if (!isprint(ch))
412 return EVENT_NONE;
413 if (ch == '(' || ch == ')' || ch == ',')
414 return EVENT_DELIM;
415
416 return EVENT_OP;
417}
418
419static int __read_char(void)
420{
421 if (input_buf_ptr >= input_buf_siz)
422 return -1;
423
424 return input_buf[input_buf_ptr++];
425}
426
427static int __peek_char(void)
428{
429 if (input_buf_ptr >= input_buf_siz)
430 return -1;
431
432 return input_buf[input_buf_ptr];
433}
434
435static enum event_type __read_token(char **tok)
436{
437 char buf[BUFSIZ];
438 int ch, last_ch, quote_ch, next_ch;
439 int i = 0;
440 int tok_size = 0;
441 enum event_type type;
442
443 *tok = NULL;
444
445
446 ch = __read_char();
447 if (ch < 0)
448 return EVENT_NONE;
449
450 type = get_type(ch);
451 if (type == EVENT_NONE)
452 return type;
453
454 buf[i++] = ch;
455
456 switch (type) {
457 case EVENT_NEWLINE:
458 case EVENT_DELIM:
459 *tok = malloc_or_die(2);
460 (*tok)[0] = ch;
461 (*tok)[1] = 0;
462 return type;
463
464 case EVENT_OP:
465 switch (ch) {
466 case '-':
467 next_ch = __peek_char();
468 if (next_ch == '>') {
469 buf[i++] = __read_char();
470 break;
471 }
472 /* fall through */
473 case '+':
474 case '|':
475 case '&':
476 case '>':
477 case '<':
478 last_ch = ch;
479 ch = __peek_char();
480 if (ch != last_ch)
481 goto test_equal;
482 buf[i++] = __read_char();
483 switch (last_ch) {
484 case '>':
485 case '<':
486 goto test_equal;
487 default:
488 break;
489 }
490 break;
491 case '!':
492 case '=':
493 goto test_equal;
494 default: /* what should we do instead? */
495 break;
496 }
497 buf[i] = 0;
498 *tok = strdup(buf);
499 return type;
500
501 test_equal:
502 ch = __peek_char();
503 if (ch == '=')
504 buf[i++] = __read_char();
505 break;
506
507 case EVENT_DQUOTE:
508 case EVENT_SQUOTE:
509 /* don't keep quotes */
510 i--;
511 quote_ch = ch;
512 last_ch = 0;
513 do {
514 if (i == (BUFSIZ - 1)) {
515 buf[i] = 0;
516 if (*tok) {
517 *tok = realloc(*tok, tok_size + BUFSIZ);
518 if (!*tok)
519 return EVENT_NONE;
520 strcat(*tok, buf);
521 } else
522 *tok = strdup(buf);
523
524 if (!*tok)
525 return EVENT_NONE;
526 tok_size += BUFSIZ;
527 i = 0;
528 }
529 last_ch = ch;
530 ch = __read_char();
531 buf[i++] = ch;
532 /* the '\' '\' will cancel itself */
533 if (ch == '\\' && last_ch == '\\')
534 last_ch = 0;
535 } while (ch != quote_ch || last_ch == '\\');
536 /* remove the last quote */
537 i--;
538 goto out;
539
540 case EVENT_ERROR ... EVENT_SPACE:
541 case EVENT_ITEM:
542 default:
543 break;
544 }
545
546 while (get_type(__peek_char()) == type) {
547 if (i == (BUFSIZ - 1)) {
548 buf[i] = 0;
549 if (*tok) {
550 *tok = realloc(*tok, tok_size + BUFSIZ);
551 if (!*tok)
552 return EVENT_NONE;
553 strcat(*tok, buf);
554 } else
555 *tok = strdup(buf);
556
557 if (!*tok)
558 return EVENT_NONE;
559 tok_size += BUFSIZ;
560 i = 0;
561 }
562 ch = __read_char();
563 buf[i++] = ch;
564 }
565
566 out:
567 buf[i] = 0;
568 if (*tok) {
569 *tok = realloc(*tok, tok_size + i);
570 if (!*tok)
571 return EVENT_NONE;
572 strcat(*tok, buf);
573 } else
574 *tok = strdup(buf);
575 if (!*tok)
576 return EVENT_NONE;
577
578 return type;
579}
580
581static void free_token(char *tok)
582{
583 if (tok)
584 free(tok);
585}
586
587static enum event_type read_token(char **tok)
588{
589 enum event_type type;
590
591 for (;;) {
592 type = __read_token(tok);
593 if (type != EVENT_SPACE)
594 return type;
595
596 free_token(*tok);
597 }
598
599 /* not reached */
600 return EVENT_NONE;
601}
602
603/* no newline */
604static enum event_type read_token_item(char **tok)
605{
606 enum event_type type;
607
608 for (;;) {
609 type = __read_token(tok);
610 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
611 return type;
612
613 free_token(*tok);
614 }
615
616 /* not reached */
617 return EVENT_NONE;
618}
619
620static int test_type(enum event_type type, enum event_type expect)
621{
622 if (type != expect) {
623 warning("Error: expected type %d but read %d",
624 expect, type);
625 return -1;
626 }
627 return 0;
628}
629
630static int __test_type_token(enum event_type type, char *token,
631 enum event_type expect, const char *expect_tok,
632 bool warn)
633{
634 if (type != expect) {
635 if (warn)
636 warning("Error: expected type %d but read %d",
637 expect, type);
638 return -1;
639 }
640
641 if (strcmp(token, expect_tok) != 0) {
642 if (warn)
643 warning("Error: expected '%s' but read '%s'",
644 expect_tok, token);
645 return -1;
646 }
647 return 0;
648}
649
650static int test_type_token(enum event_type type, char *token,
651 enum event_type expect, const char *expect_tok)
652{
653 return __test_type_token(type, token, expect, expect_tok, true);
654}
655
656static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
657{
658 enum event_type type;
659
660 if (newline_ok)
661 type = read_token(tok);
662 else
663 type = read_token_item(tok);
664 return test_type(type, expect);
665}
666
667static int read_expect_type(enum event_type expect, char **tok)
668{
669 return __read_expect_type(expect, tok, 1);
670}
671
672static int __read_expected(enum event_type expect, const char *str,
673 int newline_ok, bool warn)
674{
675 enum event_type type;
676 char *token;
677 int ret;
678
679 if (newline_ok)
680 type = read_token(&token);
681 else
682 type = read_token_item(&token);
683
684 ret = __test_type_token(type, token, expect, str, warn);
685
686 free_token(token);
687
688 return ret;
689}
690
691static int read_expected(enum event_type expect, const char *str)
692{
693 return __read_expected(expect, str, 1, true);
694}
695
696static int read_expected_item(enum event_type expect, const char *str)
697{
698 return __read_expected(expect, str, 0, true);
699}
700
701static char *event_read_name(void)
702{
703 char *token;
704
705 if (read_expected(EVENT_ITEM, "name") < 0)
706 return NULL;
707
708 if (read_expected(EVENT_OP, ":") < 0)
709 return NULL;
710
711 if (read_expect_type(EVENT_ITEM, &token) < 0)
712 goto fail;
713
714 return token;
715
716 fail:
717 free_token(token);
718 return NULL;
719}
720
721static int event_read_id(void)
722{
723 char *token;
724 int id;
725
726 if (read_expected_item(EVENT_ITEM, "ID") < 0)
727 return -1;
728
729 if (read_expected(EVENT_OP, ":") < 0)
730 return -1;
731
732 if (read_expect_type(EVENT_ITEM, &token) < 0)
733 goto fail;
734
735 id = strtoul(token, NULL, 0);
736 free_token(token);
737 return id;
738
739 fail:
740 free_token(token);
741 return -1;
742}
743
744static int field_is_string(struct format_field *field)
745{
746 if ((field->flags & FIELD_IS_ARRAY) &&
747 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
748 !strstr(field->type, "s8")))
749 return 1;
750
751 return 0;
752}
753
754static int field_is_dynamic(struct format_field *field)
755{
756 if (!strncmp(field->type, "__data_loc", 10))
757 return 1;
758
759 return 0;
760}
761
762static int event_read_fields(struct event *event, struct format_field **fields)
763{
764 struct format_field *field = NULL;
765 enum event_type type;
766 char *token;
767 char *last_token;
768 int count = 0;
769
770 do {
771 type = read_token(&token);
772 if (type == EVENT_NEWLINE) {
773 free_token(token);
774 return count;
775 }
776
777 count++;
778
779 if (test_type_token(type, token, EVENT_ITEM, "field"))
780 goto fail;
781 free_token(token);
782
783 type = read_token(&token);
784 /*
785 * The ftrace fields may still use the "special" name.
786 * Just ignore it.
787 */
788 if (event->flags & EVENT_FL_ISFTRACE &&
789 type == EVENT_ITEM && strcmp(token, "special") == 0) {
790 free_token(token);
791 type = read_token(&token);
792 }
793
794 if (test_type_token(type, token, EVENT_OP, ":") < 0)
795 return -1;
796
797 if (read_expect_type(EVENT_ITEM, &token) < 0)
798 goto fail;
799
800 last_token = token;
801
802 field = malloc_or_die(sizeof(*field));
803 memset(field, 0, sizeof(*field));
804
805 /* read the rest of the type */
806 for (;;) {
807 type = read_token(&token);
808 if (type == EVENT_ITEM ||
809 (type == EVENT_OP && strcmp(token, "*") == 0) ||
810 /*
811 * Some of the ftrace fields are broken and have
812 * an illegal "." in them.
813 */
814 (event->flags & EVENT_FL_ISFTRACE &&
815 type == EVENT_OP && strcmp(token, ".") == 0)) {
816
817 if (strcmp(token, "*") == 0)
818 field->flags |= FIELD_IS_POINTER;
819
820 if (field->type) {
821 field->type = realloc(field->type,
822 strlen(field->type) +
823 strlen(last_token) + 2);
824 strcat(field->type, " ");
825 strcat(field->type, last_token);
826 } else
827 field->type = last_token;
828 last_token = token;
829 continue;
830 }
831
832 break;
833 }
834
835 if (!field->type) {
836 die("no type found");
837 goto fail;
838 }
839 field->name = last_token;
840
841 if (test_type(type, EVENT_OP))
842 goto fail;
843
844 if (strcmp(token, "[") == 0) {
845 enum event_type last_type = type;
846 char *brackets = token;
847 int len;
848
849 field->flags |= FIELD_IS_ARRAY;
850
851 type = read_token(&token);
852 while (strcmp(token, "]") != 0) {
853 if (last_type == EVENT_ITEM &&
854 type == EVENT_ITEM)
855 len = 2;
856 else
857 len = 1;
858 last_type = type;
859
860 brackets = realloc(brackets,
861 strlen(brackets) +
862 strlen(token) + len);
863 if (len == 2)
864 strcat(brackets, " ");
865 strcat(brackets, token);
866 free_token(token);
867 type = read_token(&token);
868 if (type == EVENT_NONE) {
869 die("failed to find token");
870 goto fail;
871 }
872 }
873
874 free_token(token);
875
876 brackets = realloc(brackets, strlen(brackets) + 2);
877 strcat(brackets, "]");
878
879 /* add brackets to type */
880
881 type = read_token(&token);
882 /*
883 * If the next token is not an OP, then it is of
884 * the format: type [] item;
885 */
886 if (type == EVENT_ITEM) {
887 field->type = realloc(field->type,
888 strlen(field->type) +
889 strlen(field->name) +
890 strlen(brackets) + 2);
891 strcat(field->type, " ");
892 strcat(field->type, field->name);
893 free_token(field->name);
894 strcat(field->type, brackets);
895 field->name = token;
896 type = read_token(&token);
897 } else {
898 field->type = realloc(field->type,
899 strlen(field->type) +
900 strlen(brackets) + 1);
901 strcat(field->type, brackets);
902 }
903 free(brackets);
904 }
905
906 if (field_is_string(field)) {
907 field->flags |= FIELD_IS_STRING;
908 if (field_is_dynamic(field))
909 field->flags |= FIELD_IS_DYNAMIC;
910 }
911
912 if (test_type_token(type, token, EVENT_OP, ";"))
913 goto fail;
914 free_token(token);
915
916 if (read_expected(EVENT_ITEM, "offset") < 0)
917 goto fail_expect;
918
919 if (read_expected(EVENT_OP, ":") < 0)
920 goto fail_expect;
921
922 if (read_expect_type(EVENT_ITEM, &token))
923 goto fail;
924 field->offset = strtoul(token, NULL, 0);
925 free_token(token);
926
927 if (read_expected(EVENT_OP, ";") < 0)
928 goto fail_expect;
929
930 if (read_expected(EVENT_ITEM, "size") < 0)
931 goto fail_expect;
932
933 if (read_expected(EVENT_OP, ":") < 0)
934 goto fail_expect;
935
936 if (read_expect_type(EVENT_ITEM, &token))
937 goto fail;
938 field->size = strtoul(token, NULL, 0);
939 free_token(token);
940
941 if (read_expected(EVENT_OP, ";") < 0)
942 goto fail_expect;
943
944 type = read_token(&token);
945 if (type != EVENT_NEWLINE) {
946 /* newer versions of the kernel have a "signed" type */
947 if (test_type_token(type, token, EVENT_ITEM, "signed"))
948 goto fail;
949
950 free_token(token);
951
952 if (read_expected(EVENT_OP, ":") < 0)
953 goto fail_expect;
954
955 if (read_expect_type(EVENT_ITEM, &token))
956 goto fail;
957
958 if (strtoul(token, NULL, 0))
959 field->flags |= FIELD_IS_SIGNED;
960
961 free_token(token);
962 if (read_expected(EVENT_OP, ";") < 0)
963 goto fail_expect;
964
965 if (read_expect_type(EVENT_NEWLINE, &token))
966 goto fail;
967 }
968
969 free_token(token);
970
971 *fields = field;
972 fields = &field->next;
973
974 } while (1);
975
976 return 0;
977
978fail:
979 free_token(token);
980fail_expect:
981 if (field)
982 free(field);
983 return -1;
984}
985
986static int event_read_format(struct event *event)
987{
988 char *token;
989 int ret;
990
991 if (read_expected_item(EVENT_ITEM, "format") < 0)
992 return -1;
993
994 if (read_expected(EVENT_OP, ":") < 0)
995 return -1;
996
997 if (read_expect_type(EVENT_NEWLINE, &token))
998 goto fail;
999 free_token(token);
1000
1001 ret = event_read_fields(event, &event->format.common_fields);
1002 if (ret < 0)
1003 return ret;
1004 event->format.nr_common = ret;
1005
1006 ret = event_read_fields(event, &event->format.fields);
1007 if (ret < 0)
1008 return ret;
1009 event->format.nr_fields = ret;
1010
1011 return 0;
1012
1013 fail:
1014 free_token(token);
1015 return -1;
1016}
1017
1018enum event_type
1019process_arg_token(struct event *event, struct print_arg *arg,
1020 char **tok, enum event_type type);
1021
1022static enum event_type
1023process_arg(struct event *event, struct print_arg *arg, char **tok)
1024{
1025 enum event_type type;
1026 char *token;
1027
1028 type = read_token(&token);
1029 *tok = token;
1030
1031 return process_arg_token(event, arg, tok, type);
1032}
1033
1034static enum event_type
1035process_cond(struct event *event, struct print_arg *top, char **tok)
1036{
1037 struct print_arg *arg, *left, *right;
1038 enum event_type type;
1039 char *token = NULL;
1040
1041 arg = malloc_or_die(sizeof(*arg));
1042 memset(arg, 0, sizeof(*arg));
1043
1044 left = malloc_or_die(sizeof(*left));
1045
1046 right = malloc_or_die(sizeof(*right));
1047
1048 arg->type = PRINT_OP;
1049 arg->op.left = left;
1050 arg->op.right = right;
1051
1052 *tok = NULL;
1053 type = process_arg(event, left, &token);
1054 if (test_type_token(type, token, EVENT_OP, ":"))
1055 goto out_free;
1056
1057 arg->op.op = token;
1058
1059 type = process_arg(event, right, &token);
1060
1061 top->op.right = arg;
1062
1063 *tok = token;
1064 return type;
1065
1066out_free:
1067 free_token(*tok);
1068 free(right);
1069 free(left);
1070 free_arg(arg);
1071 return EVENT_ERROR;
1072}
1073
1074static enum event_type
1075process_array(struct event *event, struct print_arg *top, char **tok)
1076{
1077 struct print_arg *arg;
1078 enum event_type type;
1079 char *token = NULL;
1080
1081 arg = malloc_or_die(sizeof(*arg));
1082 memset(arg, 0, sizeof(*arg));
1083
1084 *tok = NULL;
1085 type = process_arg(event, arg, &token);
1086 if (test_type_token(type, token, EVENT_OP, "]"))
1087 goto out_free;
1088
1089 top->op.right = arg;
1090
1091 free_token(token);
1092 type = read_token_item(&token);
1093 *tok = token;
1094
1095 return type;
1096
1097out_free:
1098 free_token(*tok);
1099 free_arg(arg);
1100 return EVENT_ERROR;
1101}
1102
1103static int get_op_prio(char *op)
1104{
1105 if (!op[1]) {
1106 switch (op[0]) {
1107 case '*':
1108 case '/':
1109 case '%':
1110 return 6;
1111 case '+':
1112 case '-':
1113 return 7;
1114 /* '>>' and '<<' are 8 */
1115 case '<':
1116 case '>':
1117 return 9;
1118 /* '==' and '!=' are 10 */
1119 case '&':
1120 return 11;
1121 case '^':
1122 return 12;
1123 case '|':
1124 return 13;
1125 case '?':
1126 return 16;
1127 default:
1128 die("unknown op '%c'", op[0]);
1129 return -1;
1130 }
1131 } else {
1132 if (strcmp(op, "++") == 0 ||
1133 strcmp(op, "--") == 0) {
1134 return 3;
1135 } else if (strcmp(op, ">>") == 0 ||
1136 strcmp(op, "<<") == 0) {
1137 return 8;
1138 } else if (strcmp(op, ">=") == 0 ||
1139 strcmp(op, "<=") == 0) {
1140 return 9;
1141 } else if (strcmp(op, "==") == 0 ||
1142 strcmp(op, "!=") == 0) {
1143 return 10;
1144 } else if (strcmp(op, "&&") == 0) {
1145 return 14;
1146 } else if (strcmp(op, "||") == 0) {
1147 return 15;
1148 } else {
1149 die("unknown op '%s'", op);
1150 return -1;
1151 }
1152 }
1153}
1154
1155static void set_op_prio(struct print_arg *arg)
1156{
1157
1158 /* single ops are the greatest */
1159 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1160 arg->op.prio = 0;
1161 return;
1162 }
1163
1164 arg->op.prio = get_op_prio(arg->op.op);
1165}
1166
1167static enum event_type
1168process_op(struct event *event, struct print_arg *arg, char **tok)
1169{
1170 struct print_arg *left, *right = NULL;
1171 enum event_type type;
1172 char *token;
1173
1174 /* the op is passed in via tok */
1175 token = *tok;
1176
1177 if (arg->type == PRINT_OP && !arg->op.left) {
1178 /* handle single op */
1179 if (token[1]) {
1180 die("bad op token %s", token);
1181 return EVENT_ERROR;
1182 }
1183 switch (token[0]) {
1184 case '!':
1185 case '+':
1186 case '-':
1187 break;
1188 default:
1189 die("bad op token %s", token);
1190 return EVENT_ERROR;
1191 }
1192
1193 /* make an empty left */
1194 left = malloc_or_die(sizeof(*left));
1195 left->type = PRINT_NULL;
1196 arg->op.left = left;
1197
1198 right = malloc_or_die(sizeof(*right));
1199 arg->op.right = right;
1200
1201 type = process_arg(event, right, tok);
1202
1203 } else if (strcmp(token, "?") == 0) {
1204
1205 left = malloc_or_die(sizeof(*left));
1206 /* copy the top arg to the left */
1207 *left = *arg;
1208
1209 arg->type = PRINT_OP;
1210 arg->op.op = token;
1211 arg->op.left = left;
1212 arg->op.prio = 0;
1213
1214 type = process_cond(event, arg, tok);
1215
1216 } else if (strcmp(token, ">>") == 0 ||
1217 strcmp(token, "<<") == 0 ||
1218 strcmp(token, "&") == 0 ||
1219 strcmp(token, "|") == 0 ||
1220 strcmp(token, "&&") == 0 ||
1221 strcmp(token, "||") == 0 ||
1222 strcmp(token, "-") == 0 ||
1223 strcmp(token, "+") == 0 ||
1224 strcmp(token, "*") == 0 ||
1225 strcmp(token, "^") == 0 ||
1226 strcmp(token, "/") == 0 ||
1227 strcmp(token, "<") == 0 ||
1228 strcmp(token, ">") == 0 ||
1229 strcmp(token, "==") == 0 ||
1230 strcmp(token, "!=") == 0) {
1231
1232 left = malloc_or_die(sizeof(*left));
1233
1234 /* copy the top arg to the left */
1235 *left = *arg;
1236
1237 arg->type = PRINT_OP;
1238 arg->op.op = token;
1239 arg->op.left = left;
1240
1241 set_op_prio(arg);
1242
1243 right = malloc_or_die(sizeof(*right));
1244
1245 type = read_token_item(&token);
1246 *tok = token;
1247
1248 /* could just be a type pointer */
1249 if ((strcmp(arg->op.op, "*") == 0) &&
1250 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1251 if (left->type != PRINT_ATOM)
1252 die("bad pointer type");
1253 left->atom.atom = realloc(left->atom.atom,
1254 sizeof(left->atom.atom) + 3);
1255 strcat(left->atom.atom, " *");
1256 *arg = *left;
1257 free(arg);
1258
1259 return type;
1260 }
1261
1262 type = process_arg_token(event, right, tok, type);
1263
1264 arg->op.right = right;
1265
1266 } else if (strcmp(token, "[") == 0) {
1267
1268 left = malloc_or_die(sizeof(*left));
1269 *left = *arg;
1270
1271 arg->type = PRINT_OP;
1272 arg->op.op = token;
1273 arg->op.left = left;
1274
1275 arg->op.prio = 0;
1276 type = process_array(event, arg, tok);
1277
1278 } else {
1279 warning("unknown op '%s'", token);
1280 event->flags |= EVENT_FL_FAILED;
1281 /* the arg is now the left side */
1282 return EVENT_NONE;
1283 }
1284
1285 if (type == EVENT_OP) {
1286 int prio;
1287
1288 /* higher prios need to be closer to the root */
1289 prio = get_op_prio(*tok);
1290
1291 if (prio > arg->op.prio)
1292 return process_op(event, arg, tok);
1293
1294 return process_op(event, right, tok);
1295 }
1296
1297 return type;
1298}
1299
1300static enum event_type
1301process_entry(struct event *event __unused, struct print_arg *arg,
1302 char **tok)
1303{
1304 enum event_type type;
1305 char *field;
1306 char *token;
1307
1308 if (read_expected(EVENT_OP, "->") < 0)
1309 return EVENT_ERROR;
1310
1311 if (read_expect_type(EVENT_ITEM, &token) < 0)
1312 goto fail;
1313 field = token;
1314
1315 arg->type = PRINT_FIELD;
1316 arg->field.name = field;
1317
1318 if (is_flag_field) {
1319 arg->field.field = find_any_field(event, arg->field.name);
1320 arg->field.field->flags |= FIELD_IS_FLAG;
1321 is_flag_field = 0;
1322 } else if (is_symbolic_field) {
1323 arg->field.field = find_any_field(event, arg->field.name);
1324 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1325 is_symbolic_field = 0;
1326 }
1327
1328 type = read_token(&token);
1329 *tok = token;
1330
1331 return type;
1332
1333fail:
1334 free_token(token);
1335 return EVENT_ERROR;
1336}
1337
1338static char *arg_eval (struct print_arg *arg);
1339
1340static long long arg_num_eval(struct print_arg *arg)
1341{
1342 long long left, right;
1343 long long val = 0;
1344
1345 switch (arg->type) {
1346 case PRINT_ATOM:
1347 val = strtoll(arg->atom.atom, NULL, 0);
1348 break;
1349 case PRINT_TYPE:
1350 val = arg_num_eval(arg->typecast.item);
1351 break;
1352 case PRINT_OP:
1353 switch (arg->op.op[0]) {
1354 case '|':
1355 left = arg_num_eval(arg->op.left);
1356 right = arg_num_eval(arg->op.right);
1357 if (arg->op.op[1])
1358 val = left || right;
1359 else
1360 val = left | right;
1361 break;
1362 case '&':
1363 left = arg_num_eval(arg->op.left);
1364 right = arg_num_eval(arg->op.right);
1365 if (arg->op.op[1])
1366 val = left && right;
1367 else
1368 val = left & right;
1369 break;
1370 case '<':
1371 left = arg_num_eval(arg->op.left);
1372 right = arg_num_eval(arg->op.right);
1373 switch (arg->op.op[1]) {
1374 case 0:
1375 val = left < right;
1376 break;
1377 case '<':
1378 val = left << right;
1379 break;
1380 case '=':
1381 val = left <= right;
1382 break;
1383 default:
1384 die("unknown op '%s'", arg->op.op);
1385 }
1386 break;
1387 case '>':
1388 left = arg_num_eval(arg->op.left);
1389 right = arg_num_eval(arg->op.right);
1390 switch (arg->op.op[1]) {
1391 case 0:
1392 val = left > right;
1393 break;
1394 case '>':
1395 val = left >> right;
1396 break;
1397 case '=':
1398 val = left >= right;
1399 break;
1400 default:
1401 die("unknown op '%s'", arg->op.op);
1402 }
1403 break;
1404 case '=':
1405 left = arg_num_eval(arg->op.left);
1406 right = arg_num_eval(arg->op.right);
1407
1408 if (arg->op.op[1] != '=')
1409 die("unknown op '%s'", arg->op.op);
1410
1411 val = left == right;
1412 break;
1413 case '!':
1414 left = arg_num_eval(arg->op.left);
1415 right = arg_num_eval(arg->op.right);
1416
1417 switch (arg->op.op[1]) {
1418 case '=':
1419 val = left != right;
1420 break;
1421 default:
1422 die("unknown op '%s'", arg->op.op);
1423 }
1424 break;
1425 default:
1426 die("unknown op '%s'", arg->op.op);
1427 }
1428 break;
1429
1430 case PRINT_NULL:
1431 case PRINT_FIELD ... PRINT_SYMBOL:
1432 case PRINT_STRING:
1433 default:
1434 die("invalid eval type %d", arg->type);
1435
1436 }
1437 return val;
1438}
1439
1440static char *arg_eval (struct print_arg *arg)
1441{
1442 long long val;
1443 static char buf[20];
1444
1445 switch (arg->type) {
1446 case PRINT_ATOM:
1447 return arg->atom.atom;
1448 case PRINT_TYPE:
1449 return arg_eval(arg->typecast.item);
1450 case PRINT_OP:
1451 val = arg_num_eval(arg);
1452 sprintf(buf, "%lld", val);
1453 return buf;
1454
1455 case PRINT_NULL:
1456 case PRINT_FIELD ... PRINT_SYMBOL:
1457 case PRINT_STRING:
1458 default:
1459 die("invalid eval type %d", arg->type);
1460 break;
1461 }
1462
1463 return NULL;
1464}
1465
1466static enum event_type
1467process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1468{
1469 enum event_type type;
1470 struct print_arg *arg = NULL;
1471 struct print_flag_sym *field;
1472 char *token = NULL;
1473 char *value;
1474
1475 do {
1476 free_token(token);
1477 type = read_token_item(&token);
1478 if (test_type_token(type, token, EVENT_OP, "{"))
1479 break;
1480
1481 arg = malloc_or_die(sizeof(*arg));
1482
1483 free_token(token);
1484 type = process_arg(event, arg, &token);
1485 if (test_type_token(type, token, EVENT_DELIM, ","))
1486 goto out_free;
1487
1488 field = malloc_or_die(sizeof(*field));
1489 memset(field, 0, sizeof(*field));
1490
1491 value = arg_eval(arg);
1492 field->value = strdup(value);
1493
1494 free_token(token);
1495 type = process_arg(event, arg, &token);
1496 if (test_type_token(type, token, EVENT_OP, "}"))
1497 goto out_free;
1498
1499 value = arg_eval(arg);
1500 field->str = strdup(value);
1501 free_arg(arg);
1502 arg = NULL;
1503
1504 *list = field;
1505 list = &field->next;
1506
1507 free_token(token);
1508 type = read_token_item(&token);
1509 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1510
1511 *tok = token;
1512 return type;
1513
1514out_free:
1515 free_arg(arg);
1516 free_token(token);
1517
1518 return EVENT_ERROR;
1519}
1520
1521static enum event_type
1522process_flags(struct event *event, struct print_arg *arg, char **tok)
1523{
1524 struct print_arg *field;
1525 enum event_type type;
1526 char *token;
1527
1528 memset(arg, 0, sizeof(*arg));
1529 arg->type = PRINT_FLAGS;
1530
1531 if (read_expected_item(EVENT_DELIM, "(") < 0)
1532 return EVENT_ERROR;
1533
1534 field = malloc_or_die(sizeof(*field));
1535
1536 type = process_arg(event, field, &token);
1537 if (test_type_token(type, token, EVENT_DELIM, ","))
1538 goto out_free;
1539
1540 arg->flags.field = field;
1541
1542 type = read_token_item(&token);
1543 if (event_item_type(type)) {
1544 arg->flags.delim = token;
1545 type = read_token_item(&token);
1546 }
1547
1548 if (test_type_token(type, token, EVENT_DELIM, ","))
1549 goto out_free;
1550
1551 type = process_fields(event, &arg->flags.flags, &token);
1552 if (test_type_token(type, token, EVENT_DELIM, ")"))
1553 goto out_free;
1554
1555 free_token(token);
1556 type = read_token_item(tok);
1557 return type;
1558
1559out_free:
1560 free_token(token);
1561 return EVENT_ERROR;
1562}
1563
1564static enum event_type
1565process_symbols(struct event *event, struct print_arg *arg, char **tok)
1566{
1567 struct print_arg *field;
1568 enum event_type type;
1569 char *token;
1570
1571 memset(arg, 0, sizeof(*arg));
1572 arg->type = PRINT_SYMBOL;
1573
1574 if (read_expected_item(EVENT_DELIM, "(") < 0)
1575 return EVENT_ERROR;
1576
1577 field = malloc_or_die(sizeof(*field));
1578
1579 type = process_arg(event, field, &token);
1580 if (test_type_token(type, token, EVENT_DELIM, ","))
1581 goto out_free;
1582
1583 arg->symbol.field = field;
1584
1585 type = process_fields(event, &arg->symbol.symbols, &token);
1586 if (test_type_token(type, token, EVENT_DELIM, ")"))
1587 goto out_free;
1588
1589 free_token(token);
1590 type = read_token_item(tok);
1591 return type;
1592
1593out_free:
1594 free_token(token);
1595 return EVENT_ERROR;
1596}
1597
1598static enum event_type
1599process_paren(struct event *event, struct print_arg *arg, char **tok)
1600{
1601 struct print_arg *item_arg;
1602 enum event_type type;
1603 char *token;
1604
1605 type = process_arg(event, arg, &token);
1606
1607 if (type == EVENT_ERROR)
1608 return EVENT_ERROR;
1609
1610 if (type == EVENT_OP)
1611 type = process_op(event, arg, &token);
1612
1613 if (type == EVENT_ERROR)
1614 return EVENT_ERROR;
1615
1616 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1617 free_token(token);
1618 return EVENT_ERROR;
1619 }
1620
1621 free_token(token);
1622 type = read_token_item(&token);
1623
1624 /*
1625 * If the next token is an item or another open paren, then
1626 * this was a typecast.
1627 */
1628 if (event_item_type(type) ||
1629 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1630
1631 /* make this a typecast and contine */
1632
1633 /* prevous must be an atom */
1634 if (arg->type != PRINT_ATOM)
1635 die("previous needed to be PRINT_ATOM");
1636
1637 item_arg = malloc_or_die(sizeof(*item_arg));
1638
1639 arg->type = PRINT_TYPE;
1640 arg->typecast.type = arg->atom.atom;
1641 arg->typecast.item = item_arg;
1642 type = process_arg_token(event, item_arg, &token, type);
1643
1644 }
1645
1646 *tok = token;
1647 return type;
1648}
1649
1650
1651static enum event_type
1652process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1653{
1654 enum event_type type;
1655 char *token;
1656
1657 if (read_expected(EVENT_DELIM, "(") < 0)
1658 return EVENT_ERROR;
1659
1660 if (read_expect_type(EVENT_ITEM, &token) < 0)
1661 goto fail;
1662
1663 arg->type = PRINT_STRING;
1664 arg->string.string = token;
1665 arg->string.offset = -1;
1666
1667 if (read_expected(EVENT_DELIM, ")") < 0)
1668 return EVENT_ERROR;
1669
1670 type = read_token(&token);
1671 *tok = token;
1672
1673 return type;
1674fail:
1675 free_token(token);
1676 return EVENT_ERROR;
1677}
1678
1679enum event_type
1680process_arg_token(struct event *event, struct print_arg *arg,
1681 char **tok, enum event_type type)
1682{
1683 char *token;
1684 char *atom;
1685
1686 token = *tok;
1687
1688 switch (type) {
1689 case EVENT_ITEM:
1690 if (strcmp(token, "REC") == 0) {
1691 free_token(token);
1692 type = process_entry(event, arg, &token);
1693 } else if (strcmp(token, "__print_flags") == 0) {
1694 free_token(token);
1695 is_flag_field = 1;
1696 type = process_flags(event, arg, &token);
1697 } else if (strcmp(token, "__print_symbolic") == 0) {
1698 free_token(token);
1699 is_symbolic_field = 1;
1700 type = process_symbols(event, arg, &token);
1701 } else if (strcmp(token, "__get_str") == 0) {
1702 free_token(token);
1703 type = process_str(event, arg, &token);
1704 } else {
1705 atom = token;
1706 /* test the next token */
1707 type = read_token_item(&token);
1708
1709 /* atoms can be more than one token long */
1710 while (type == EVENT_ITEM) {
1711 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1712 strcat(atom, " ");
1713 strcat(atom, token);
1714 free_token(token);
1715 type = read_token_item(&token);
1716 }
1717
1718 /* todo, test for function */
1719
1720 arg->type = PRINT_ATOM;
1721 arg->atom.atom = atom;
1722 }
1723 break;
1724 case EVENT_DQUOTE:
1725 case EVENT_SQUOTE:
1726 arg->type = PRINT_ATOM;
1727 arg->atom.atom = token;
1728 type = read_token_item(&token);
1729 break;
1730 case EVENT_DELIM:
1731 if (strcmp(token, "(") == 0) {
1732 free_token(token);
1733 type = process_paren(event, arg, &token);
1734 break;
1735 }
1736 case EVENT_OP:
1737 /* handle single ops */
1738 arg->type = PRINT_OP;
1739 arg->op.op = token;
1740 arg->op.left = NULL;
1741 type = process_op(event, arg, &token);
1742
1743 break;
1744
1745 case EVENT_ERROR ... EVENT_NEWLINE:
1746 default:
1747 die("unexpected type %d", type);
1748 }
1749 *tok = token;
1750
1751 return type;
1752}
1753
1754static int event_read_print_args(struct event *event, struct print_arg **list)
1755{
1756 enum event_type type = EVENT_ERROR;
1757 struct print_arg *arg;
1758 char *token;
1759 int args = 0;
1760
1761 do {
1762 if (type == EVENT_NEWLINE) {
1763 free_token(token);
1764 type = read_token_item(&token);
1765 continue;
1766 }
1767
1768 arg = malloc_or_die(sizeof(*arg));
1769 memset(arg, 0, sizeof(*arg));
1770
1771 type = process_arg(event, arg, &token);
1772
1773 if (type == EVENT_ERROR) {
1774 free_arg(arg);
1775 return -1;
1776 }
1777
1778 *list = arg;
1779 args++;
1780
1781 if (type == EVENT_OP) {
1782 type = process_op(event, arg, &token);
1783 list = &arg->next;
1784 continue;
1785 }
1786
1787 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1788 free_token(token);
1789 *list = arg;
1790 list = &arg->next;
1791 continue;
1792 }
1793 break;
1794 } while (type != EVENT_NONE);
1795
1796 if (type != EVENT_NONE)
1797 free_token(token);
1798
1799 return args;
1800}
1801
1802static int event_read_print(struct event *event)
1803{
1804 enum event_type type;
1805 char *token;
1806 int ret;
1807
1808 if (read_expected_item(EVENT_ITEM, "print") < 0)
1809 return -1;
1810
1811 if (read_expected(EVENT_ITEM, "fmt") < 0)
1812 return -1;
1813
1814 if (read_expected(EVENT_OP, ":") < 0)
1815 return -1;
1816
1817 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1818 goto fail;
1819
1820 concat:
1821 event->print_fmt.format = token;
1822 event->print_fmt.args = NULL;
1823
1824 /* ok to have no arg */
1825 type = read_token_item(&token);
1826
1827 if (type == EVENT_NONE)
1828 return 0;
1829
1830 /* Handle concatination of print lines */
1831 if (type == EVENT_DQUOTE) {
1832 char *cat;
1833
1834 cat = malloc_or_die(strlen(event->print_fmt.format) +
1835 strlen(token) + 1);
1836 strcpy(cat, event->print_fmt.format);
1837 strcat(cat, token);
1838 free_token(token);
1839 free_token(event->print_fmt.format);
1840 event->print_fmt.format = NULL;
1841 token = cat;
1842 goto concat;
1843 }
1844
1845 if (test_type_token(type, token, EVENT_DELIM, ","))
1846 goto fail;
1847
1848 free_token(token);
1849
1850 ret = event_read_print_args(event, &event->print_fmt.args);
1851 if (ret < 0)
1852 return -1;
1853
1854 return ret;
1855
1856 fail:
1857 free_token(token);
1858 return -1;
1859}
1860
1861static struct format_field *
1862find_common_field(struct event *event, const char *name)
1863{
1864 struct format_field *format;
1865
1866 for (format = event->format.common_fields;
1867 format; format = format->next) {
1868 if (strcmp(format->name, name) == 0)
1869 break;
1870 }
1871
1872 return format;
1873}
1874
1875static struct format_field *
1876find_field(struct event *event, const char *name)
1877{
1878 struct format_field *format;
1879
1880 for (format = event->format.fields;
1881 format; format = format->next) {
1882 if (strcmp(format->name, name) == 0)
1883 break;
1884 }
1885
1886 return format;
1887}
1888
1889static struct format_field *
1890find_any_field(struct event *event, const char *name)
1891{
1892 struct format_field *format;
1893
1894 format = find_common_field(event, name);
1895 if (format)
1896 return format;
1897 return find_field(event, name);
1898}
1899
1900unsigned long long read_size(void *ptr, int size)
1901{
1902 switch (size) {
1903 case 1:
1904 return *(unsigned char *)ptr;
1905 case 2:
1906 return data2host2(ptr);
1907 case 4:
1908 return data2host4(ptr);
1909 case 8:
1910 return data2host8(ptr);
1911 default:
1912 /* BUG! */
1913 return 0;
1914 }
1915}
1916
1917unsigned long long
1918raw_field_value(struct event *event, const char *name, void *data)
1919{
1920 struct format_field *field;
1921
1922 field = find_any_field(event, name);
1923 if (!field)
1924 return 0ULL;
1925
1926 return read_size(data + field->offset, field->size);
1927}
1928
1929void *raw_field_ptr(struct event *event, const char *name, void *data)
1930{
1931 struct format_field *field;
1932
1933 field = find_any_field(event, name);
1934 if (!field)
1935 return NULL;
1936
1937 if (field->flags & FIELD_IS_DYNAMIC) {
1938 int offset;
1939
1940 offset = *(int *)(data + field->offset);
1941 offset &= 0xffff;
1942
1943 return data + offset;
1944 }
1945
1946 return data + field->offset;
1947}
1948
1949static int get_common_info(const char *type, int *offset, int *size)
1950{
1951 struct event *event;
1952 struct format_field *field;
1953
1954 /*
1955 * All events should have the same common elements.
1956 * Pick any event to find where the type is;
1957 */
1958 if (!event_list)
1959 die("no event_list!");
1960
1961 event = event_list;
1962 field = find_common_field(event, type);
1963 if (!field)
1964 die("field '%s' not found", type);
1965
1966 *offset = field->offset;
1967 *size = field->size;
1968
1969 return 0;
1970}
1971
1972static int __parse_common(void *data, int *size, int *offset,
1973 const char *name)
1974{
1975 int ret;
1976
1977 if (!*size) {
1978 ret = get_common_info(name, offset, size);
1979 if (ret < 0)
1980 return ret;
1981 }
1982 return read_size(data + *offset, *size);
1983}
1984
1985int trace_parse_common_type(void *data)
1986{
1987 static int type_offset;
1988 static int type_size;
1989
1990 return __parse_common(data, &type_size, &type_offset,
1991 "common_type");
1992}
1993
1994int trace_parse_common_pid(void *data)
1995{
1996 static int pid_offset;
1997 static int pid_size;
1998
1999 return __parse_common(data, &pid_size, &pid_offset,
2000 "common_pid");
2001}
2002
2003int parse_common_pc(void *data)
2004{
2005 static int pc_offset;
2006 static int pc_size;
2007
2008 return __parse_common(data, &pc_size, &pc_offset,
2009 "common_preempt_count");
2010}
2011
2012int parse_common_flags(void *data)
2013{
2014 static int flags_offset;
2015 static int flags_size;
2016
2017 return __parse_common(data, &flags_size, &flags_offset,
2018 "common_flags");
2019}
2020
2021int parse_common_lock_depth(void *data)
2022{
2023 static int ld_offset;
2024 static int ld_size;
2025 int ret;
2026
2027 ret = __parse_common(data, &ld_size, &ld_offset,
2028 "common_lock_depth");
2029 if (ret < 0)
2030 return -1;
2031
2032 return ret;
2033}
2034
2035struct event *trace_find_event(int id)
2036{
2037 struct event *event;
2038
2039 for (event = event_list; event; event = event->next) {
2040 if (event->id == id)
2041 break;
2042 }
2043 return event;
2044}
2045
2046struct event *trace_find_next_event(struct event *event)
2047{
2048 if (!event)
2049 return event_list;
2050
2051 return event->next;
2052}
2053
2054static unsigned long long eval_num_arg(void *data, int size,
2055 struct event *event, struct print_arg *arg)
2056{
2057 unsigned long long val = 0;
2058 unsigned long long left, right;
2059 struct print_arg *larg;
2060
2061 switch (arg->type) {
2062 case PRINT_NULL:
2063 /* ?? */
2064 return 0;
2065 case PRINT_ATOM:
2066 return strtoull(arg->atom.atom, NULL, 0);
2067 case PRINT_FIELD:
2068 if (!arg->field.field) {
2069 arg->field.field = find_any_field(event, arg->field.name);
2070 if (!arg->field.field)
2071 die("field %s not found", arg->field.name);
2072 }
2073 /* must be a number */
2074 val = read_size(data + arg->field.field->offset,
2075 arg->field.field->size);
2076 break;
2077 case PRINT_FLAGS:
2078 case PRINT_SYMBOL:
2079 break;
2080 case PRINT_TYPE:
2081 return eval_num_arg(data, size, event, arg->typecast.item);
2082 case PRINT_STRING:
2083 return 0;
2084 break;
2085 case PRINT_OP:
2086 if (strcmp(arg->op.op, "[") == 0) {
2087 /*
2088 * Arrays are special, since we don't want
2089 * to read the arg as is.
2090 */
2091 if (arg->op.left->type != PRINT_FIELD)
2092 goto default_op; /* oops, all bets off */
2093 larg = arg->op.left;
2094 if (!larg->field.field) {
2095 larg->field.field =
2096 find_any_field(event, larg->field.name);
2097 if (!larg->field.field)
2098 die("field %s not found", larg->field.name);
2099 }
2100 right = eval_num_arg(data, size, event, arg->op.right);
2101 val = read_size(data + larg->field.field->offset +
2102 right * long_size, long_size);
2103 break;
2104 }
2105 default_op:
2106 left = eval_num_arg(data, size, event, arg->op.left);
2107 right = eval_num_arg(data, size, event, arg->op.right);
2108 switch (arg->op.op[0]) {
2109 case '|':
2110 if (arg->op.op[1])
2111 val = left || right;
2112 else
2113 val = left | right;
2114 break;
2115 case '&':
2116 if (arg->op.op[1])
2117 val = left && right;
2118 else
2119 val = left & right;
2120 break;
2121 case '<':
2122 switch (arg->op.op[1]) {
2123 case 0:
2124 val = left < right;
2125 break;
2126 case '<':
2127 val = left << right;
2128 break;
2129 case '=':
2130 val = left <= right;
2131 break;
2132 default:
2133 die("unknown op '%s'", arg->op.op);
2134 }
2135 break;
2136 case '>':
2137 switch (arg->op.op[1]) {
2138 case 0:
2139 val = left > right;
2140 break;
2141 case '>':
2142 val = left >> right;
2143 break;
2144 case '=':
2145 val = left >= right;
2146 break;
2147 default:
2148 die("unknown op '%s'", arg->op.op);
2149 }
2150 break;
2151 case '=':
2152 if (arg->op.op[1] != '=')
2153 die("unknown op '%s'", arg->op.op);
2154 val = left == right;
2155 break;
2156 case '-':
2157 val = left - right;
2158 break;
2159 case '+':
2160 val = left + right;
2161 break;
2162 default:
2163 die("unknown op '%s'", arg->op.op);
2164 }
2165 break;
2166 default: /* not sure what to do there */
2167 return 0;
2168 }
2169 return val;
2170}
2171
2172struct flag {
2173 const char *name;
2174 unsigned long long value;
2175};
2176
2177static const struct flag flags[] = {
2178 { "HI_SOFTIRQ", 0 },
2179 { "TIMER_SOFTIRQ", 1 },
2180 { "NET_TX_SOFTIRQ", 2 },
2181 { "NET_RX_SOFTIRQ", 3 },
2182 { "BLOCK_SOFTIRQ", 4 },
2183 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2184 { "TASKLET_SOFTIRQ", 6 },
2185 { "SCHED_SOFTIRQ", 7 },
2186 { "HRTIMER_SOFTIRQ", 8 },
2187 { "RCU_SOFTIRQ", 9 },
2188
2189 { "HRTIMER_NORESTART", 0 },
2190 { "HRTIMER_RESTART", 1 },
2191};
2192
2193unsigned long long eval_flag(const char *flag)
2194{
2195 int i;
2196
2197 /*
2198 * Some flags in the format files do not get converted.
2199 * If the flag is not numeric, see if it is something that
2200 * we already know about.
2201 */
2202 if (isdigit(flag[0]))
2203 return strtoull(flag, NULL, 0);
2204
2205 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2206 if (strcmp(flags[i].name, flag) == 0)
2207 return flags[i].value;
2208
2209 return 0;
2210}
2211
2212static void print_str_arg(void *data, int size,
2213 struct event *event, struct print_arg *arg)
2214{
2215 struct print_flag_sym *flag;
2216 unsigned long long val, fval;
2217 char *str;
2218 int print;
2219
2220 switch (arg->type) {
2221 case PRINT_NULL:
2222 /* ?? */
2223 return;
2224 case PRINT_ATOM:
2225 printf("%s", arg->atom.atom);
2226 return;
2227 case PRINT_FIELD:
2228 if (!arg->field.field) {
2229 arg->field.field = find_any_field(event, arg->field.name);
2230 if (!arg->field.field)
2231 die("field %s not found", arg->field.name);
2232 }
2233 str = malloc_or_die(arg->field.field->size + 1);
2234 memcpy(str, data + arg->field.field->offset,
2235 arg->field.field->size);
2236 str[arg->field.field->size] = 0;
2237 printf("%s", str);
2238 free(str);
2239 break;
2240 case PRINT_FLAGS:
2241 val = eval_num_arg(data, size, event, arg->flags.field);
2242 print = 0;
2243 for (flag = arg->flags.flags; flag; flag = flag->next) {
2244 fval = eval_flag(flag->value);
2245 if (!val && !fval) {
2246 printf("%s", flag->str);
2247 break;
2248 }
2249 if (fval && (val & fval) == fval) {
2250 if (print && arg->flags.delim)
2251 printf("%s", arg->flags.delim);
2252 printf("%s", flag->str);
2253 print = 1;
2254 val &= ~fval;
2255 }
2256 }
2257 break;
2258 case PRINT_SYMBOL:
2259 val = eval_num_arg(data, size, event, arg->symbol.field);
2260 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2261 fval = eval_flag(flag->value);
2262 if (val == fval) {
2263 printf("%s", flag->str);
2264 break;
2265 }
2266 }
2267 break;
2268
2269 case PRINT_TYPE:
2270 break;
2271 case PRINT_STRING: {
2272 int str_offset;
2273
2274 if (arg->string.offset == -1) {
2275 struct format_field *f;
2276
2277 f = find_any_field(event, arg->string.string);
2278 arg->string.offset = f->offset;
2279 }
2280 str_offset = *(int *)(data + arg->string.offset);
2281 str_offset &= 0xffff;
2282 printf("%s", ((char *)data) + str_offset);
2283 break;
2284 }
2285 case PRINT_OP:
2286 /*
2287 * The only op for string should be ? :
2288 */
2289 if (arg->op.op[0] != '?')
2290 return;
2291 val = eval_num_arg(data, size, event, arg->op.left);
2292 if (val)
2293 print_str_arg(data, size, event, arg->op.right->op.left);
2294 else
2295 print_str_arg(data, size, event, arg->op.right->op.right);
2296 break;
2297 default:
2298 /* well... */
2299 break;
2300 }
2301}
2302
2303static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2304{
2305 static struct format_field *field, *ip_field;
2306 struct print_arg *args, *arg, **next;
2307 unsigned long long ip, val;
2308 char *ptr;
2309 void *bptr;
2310
2311 if (!field) {
2312 field = find_field(event, "buf");
2313 if (!field)
2314 die("can't find buffer field for binary printk");
2315 ip_field = find_field(event, "ip");
2316 if (!ip_field)
2317 die("can't find ip field for binary printk");
2318 }
2319
2320 ip = read_size(data + ip_field->offset, ip_field->size);
2321
2322 /*
2323 * The first arg is the IP pointer.
2324 */
2325 args = malloc_or_die(sizeof(*args));
2326 arg = args;
2327 arg->next = NULL;
2328 next = &arg->next;
2329
2330 arg->type = PRINT_ATOM;
2331 arg->atom.atom = malloc_or_die(32);
2332 sprintf(arg->atom.atom, "%lld", ip);
2333
2334 /* skip the first "%pf : " */
2335 for (ptr = fmt + 6, bptr = data + field->offset;
2336 bptr < data + size && *ptr; ptr++) {
2337 int ls = 0;
2338
2339 if (*ptr == '%') {
2340 process_again:
2341 ptr++;
2342 switch (*ptr) {
2343 case '%':
2344 break;
2345 case 'l':
2346 ls++;
2347 goto process_again;
2348 case 'L':
2349 ls = 2;
2350 goto process_again;
2351 case '0' ... '9':
2352 goto process_again;
2353 case 'p':
2354 ls = 1;
2355 /* fall through */
2356 case 'd':
2357 case 'u':
2358 case 'x':
2359 case 'i':
2360 /* the pointers are always 4 bytes aligned */
2361 bptr = (void *)(((unsigned long)bptr + 3) &
2362 ~3);
2363 switch (ls) {
2364 case 0:
2365 case 1:
2366 ls = long_size;
2367 break;
2368 case 2:
2369 ls = 8;
2370 default:
2371 break;
2372 }
2373 val = read_size(bptr, ls);
2374 bptr += ls;
2375 arg = malloc_or_die(sizeof(*arg));
2376 arg->next = NULL;
2377 arg->type = PRINT_ATOM;
2378 arg->atom.atom = malloc_or_die(32);
2379 sprintf(arg->atom.atom, "%lld", val);
2380 *next = arg;
2381 next = &arg->next;
2382 break;
2383 case 's':
2384 arg = malloc_or_die(sizeof(*arg));
2385 arg->next = NULL;
2386 arg->type = PRINT_STRING;
2387 arg->string.string = strdup(bptr);
2388 bptr += strlen(bptr) + 1;
2389 *next = arg;
2390 next = &arg->next;
2391 default:
2392 break;
2393 }
2394 }
2395 }
2396
2397 return args;
2398}
2399
2400static void free_args(struct print_arg *args)
2401{
2402 struct print_arg *next;
2403
2404 while (args) {
2405 next = args->next;
2406
2407 if (args->type == PRINT_ATOM)
2408 free(args->atom.atom);
2409 else
2410 free(args->string.string);
2411 free(args);
2412 args = next;
2413 }
2414}
2415
2416static char *get_bprint_format(void *data, int size __unused, struct event *event)
2417{
2418 unsigned long long addr;
2419 static struct format_field *field;
2420 struct printk_map *printk;
2421 char *format;
2422 char *p;
2423
2424 if (!field) {
2425 field = find_field(event, "fmt");
2426 if (!field)
2427 die("can't find format field for binary printk");
2428 printf("field->offset = %d size=%d\n", field->offset, field->size);
2429 }
2430
2431 addr = read_size(data + field->offset, field->size);
2432
2433 printk = find_printk(addr);
2434 if (!printk) {
2435 format = malloc_or_die(45);
2436 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2437 addr);
2438 return format;
2439 }
2440
2441 p = printk->printk;
2442 /* Remove any quotes. */
2443 if (*p == '"')
2444 p++;
2445 format = malloc_or_die(strlen(p) + 10);
2446 sprintf(format, "%s : %s", "%pf", p);
2447 /* remove ending quotes and new line since we will add one too */
2448 p = format + strlen(format) - 1;
2449 if (*p == '"')
2450 *p = 0;
2451
2452 p -= 2;
2453 if (strcmp(p, "\\n") == 0)
2454 *p = 0;
2455
2456 return format;
2457}
2458
2459static void pretty_print(void *data, int size, struct event *event)
2460{
2461 struct print_fmt *print_fmt = &event->print_fmt;
2462 struct print_arg *arg = print_fmt->args;
2463 struct print_arg *args = NULL;
2464 const char *ptr = print_fmt->format;
2465 unsigned long long val;
2466 struct func_map *func;
2467 const char *saveptr;
2468 char *bprint_fmt = NULL;
2469 char format[32];
2470 int show_func;
2471 int len;
2472 int ls;
2473
2474 if (event->flags & EVENT_FL_ISFUNC)
2475 ptr = " %pF <-- %pF";
2476
2477 if (event->flags & EVENT_FL_ISBPRINT) {
2478 bprint_fmt = get_bprint_format(data, size, event);
2479 args = make_bprint_args(bprint_fmt, data, size, event);
2480 arg = args;
2481 ptr = bprint_fmt;
2482 }
2483
2484 for (; *ptr; ptr++) {
2485 ls = 0;
2486 if (*ptr == '\\') {
2487 ptr++;
2488 switch (*ptr) {
2489 case 'n':
2490 printf("\n");
2491 break;
2492 case 't':
2493 printf("\t");
2494 break;
2495 case 'r':
2496 printf("\r");
2497 break;
2498 case '\\':
2499 printf("\\");
2500 break;
2501 default:
2502 printf("%c", *ptr);
2503 break;
2504 }
2505
2506 } else if (*ptr == '%') {
2507 saveptr = ptr;
2508 show_func = 0;
2509 cont_process:
2510 ptr++;
2511 switch (*ptr) {
2512 case '%':
2513 printf("%%");
2514 break;
2515 case 'l':
2516 ls++;
2517 goto cont_process;
2518 case 'L':
2519 ls = 2;
2520 goto cont_process;
2521 case 'z':
2522 case 'Z':
2523 case '0' ... '9':
2524 goto cont_process;
2525 case 'p':
2526 if (long_size == 4)
2527 ls = 1;
2528 else
2529 ls = 2;
2530
2531 if (*(ptr+1) == 'F' ||
2532 *(ptr+1) == 'f') {
2533 ptr++;
2534 show_func = *ptr;
2535 }
2536
2537 /* fall through */
2538 case 'd':
2539 case 'i':
2540 case 'x':
2541 case 'X':
2542 case 'u':
2543 if (!arg)
2544 die("no argument match");
2545
2546 len = ((unsigned long)ptr + 1) -
2547 (unsigned long)saveptr;
2548
2549 /* should never happen */
2550 if (len > 32)
2551 die("bad format!");
2552
2553 memcpy(format, saveptr, len);
2554 format[len] = 0;
2555
2556 val = eval_num_arg(data, size, event, arg);
2557 arg = arg->next;
2558
2559 if (show_func) {
2560 func = find_func(val);
2561 if (func) {
2562 printf("%s", func->func);
2563 if (show_func == 'F')
2564 printf("+0x%llx",
2565 val - func->addr);
2566 break;
2567 }
2568 }
2569 switch (ls) {
2570 case 0:
2571 printf(format, (int)val);
2572 break;
2573 case 1:
2574 printf(format, (long)val);
2575 break;
2576 case 2:
2577 printf(format, (long long)val);
2578 break;
2579 default:
2580 die("bad count (%d)", ls);
2581 }
2582 break;
2583 case 's':
2584 if (!arg)
2585 die("no matching argument");
2586
2587 print_str_arg(data, size, event, arg);
2588 arg = arg->next;
2589 break;
2590 default:
2591 printf(">%c<", *ptr);
2592
2593 }
2594 } else
2595 printf("%c", *ptr);
2596 }
2597
2598 if (args) {
2599 free_args(args);
2600 free(bprint_fmt);
2601 }
2602}
2603
2604static inline int log10_cpu(int nb)
2605{
2606 if (nb / 100)
2607 return 3;
2608 if (nb / 10)
2609 return 2;
2610 return 1;
2611}
2612
2613static void print_lat_fmt(void *data, int size __unused)
2614{
2615 unsigned int lat_flags;
2616 unsigned int pc;
2617 int lock_depth;
2618 int hardirq;
2619 int softirq;
2620
2621 lat_flags = parse_common_flags(data);
2622 pc = parse_common_pc(data);
2623 lock_depth = parse_common_lock_depth(data);
2624
2625 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2626 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2627
2628 printf("%c%c%c",
2629 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2630 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2631 'X' : '.',
2632 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2633 'N' : '.',
2634 (hardirq && softirq) ? 'H' :
2635 hardirq ? 'h' : softirq ? 's' : '.');
2636
2637 if (pc)
2638 printf("%x", pc);
2639 else
2640 printf(".");
2641
2642 if (lock_depth < 0)
2643 printf(". ");
2644 else
2645 printf("%d ", lock_depth);
2646}
2647
2648#define TRACE_GRAPH_INDENT 2
2649
2650static struct record *
2651get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2652 struct record *next)
2653{
2654 struct format_field *field;
2655 struct event *event;
2656 unsigned long val;
2657 int type;
2658 int pid;
2659
2660 type = trace_parse_common_type(next->data);
2661 event = trace_find_event(type);
2662 if (!event)
2663 return NULL;
2664
2665 if (!(event->flags & EVENT_FL_ISFUNCRET))
2666 return NULL;
2667
2668 pid = trace_parse_common_pid(next->data);
2669 field = find_field(event, "func");
2670 if (!field)
2671 die("function return does not have field func");
2672
2673 val = read_size(next->data + field->offset, field->size);
2674
2675 if (cur_pid != pid || cur_func != val)
2676 return NULL;
2677
2678 /* this is a leaf, now advance the iterator */
2679 return trace_read_data(cpu);
2680}
2681
2682/* Signal a overhead of time execution to the output */
2683static void print_graph_overhead(unsigned long long duration)
2684{
2685 /* Non nested entry or return */
2686 if (duration == ~0ULL)
2687 return (void)printf(" ");
2688
2689 /* Duration exceeded 100 msecs */
2690 if (duration > 100000ULL)
2691 return (void)printf("! ");
2692
2693 /* Duration exceeded 10 msecs */
2694 if (duration > 10000ULL)
2695 return (void)printf("+ ");
2696
2697 printf(" ");
2698}
2699
2700static void print_graph_duration(unsigned long long duration)
2701{
2702 unsigned long usecs = duration / 1000;
2703 unsigned long nsecs_rem = duration % 1000;
2704 /* log10(ULONG_MAX) + '\0' */
2705 char msecs_str[21];
2706 char nsecs_str[5];
2707 int len;
2708 int i;
2709
2710 sprintf(msecs_str, "%lu", usecs);
2711
2712 /* Print msecs */
2713 len = printf("%lu", usecs);
2714
2715 /* Print nsecs (we don't want to exceed 7 numbers) */
2716 if (len < 7) {
2717 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2718 len += printf(".%s", nsecs_str);
2719 }
2720
2721 printf(" us ");
2722
2723 /* Print remaining spaces to fit the row's width */
2724 for (i = len; i < 7; i++)
2725 printf(" ");
2726
2727 printf("| ");
2728}
2729
2730static void
2731print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2732{
2733 unsigned long long rettime, calltime;
2734 unsigned long long duration, depth;
2735 unsigned long long val;
2736 struct format_field *field;
2737 struct func_map *func;
2738 struct event *ret_event;
2739 int type;
2740 int i;
2741
2742 type = trace_parse_common_type(ret_rec->data);
2743 ret_event = trace_find_event(type);
2744
2745 field = find_field(ret_event, "rettime");
2746 if (!field)
2747 die("can't find rettime in return graph");
2748 rettime = read_size(ret_rec->data + field->offset, field->size);
2749
2750 field = find_field(ret_event, "calltime");
2751 if (!field)
2752 die("can't find rettime in return graph");
2753 calltime = read_size(ret_rec->data + field->offset, field->size);
2754
2755 duration = rettime - calltime;
2756
2757 /* Overhead */
2758 print_graph_overhead(duration);
2759
2760 /* Duration */
2761 print_graph_duration(duration);
2762
2763 field = find_field(event, "depth");
2764 if (!field)
2765 die("can't find depth in entry graph");
2766 depth = read_size(data + field->offset, field->size);
2767
2768 /* Function */
2769 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2770 printf(" ");
2771
2772 field = find_field(event, "func");
2773 if (!field)
2774 die("can't find func in entry graph");
2775 val = read_size(data + field->offset, field->size);
2776 func = find_func(val);
2777
2778 if (func)
2779 printf("%s();", func->func);
2780 else
2781 printf("%llx();", val);
2782}
2783
2784static void print_graph_nested(struct event *event, void *data)
2785{
2786 struct format_field *field;
2787 unsigned long long depth;
2788 unsigned long long val;
2789 struct func_map *func;
2790 int i;
2791
2792 /* No overhead */
2793 print_graph_overhead(-1);
2794
2795 /* No time */
2796 printf(" | ");
2797
2798 field = find_field(event, "depth");
2799 if (!field)
2800 die("can't find depth in entry graph");
2801 depth = read_size(data + field->offset, field->size);
2802
2803 /* Function */
2804 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2805 printf(" ");
2806
2807 field = find_field(event, "func");
2808 if (!field)
2809 die("can't find func in entry graph");
2810 val = read_size(data + field->offset, field->size);
2811 func = find_func(val);
2812
2813 if (func)
2814 printf("%s() {", func->func);
2815 else
2816 printf("%llx() {", val);
2817}
2818
2819static void
2820pretty_print_func_ent(void *data, int size, struct event *event,
2821 int cpu, int pid)
2822{
2823 struct format_field *field;
2824 struct record *rec;
2825 void *copy_data;
2826 unsigned long val;
2827
2828 if (latency_format) {
2829 print_lat_fmt(data, size);
2830 printf(" | ");
2831 }
2832
2833 field = find_field(event, "func");
2834 if (!field)
2835 die("function entry does not have func field");
2836
2837 val = read_size(data + field->offset, field->size);
2838
2839 /*
2840 * peek_data may unmap the data pointer. Copy it first.
2841 */
2842 copy_data = malloc_or_die(size);
2843 memcpy(copy_data, data, size);
2844 data = copy_data;
2845
2846 rec = trace_peek_data(cpu);
2847 if (rec) {
2848 rec = get_return_for_leaf(cpu, pid, val, rec);
2849 if (rec) {
2850 print_graph_entry_leaf(event, data, rec);
2851 goto out_free;
2852 }
2853 }
2854 print_graph_nested(event, data);
2855out_free:
2856 free(data);
2857}
2858
2859static void
2860pretty_print_func_ret(void *data, int size __unused, struct event *event)
2861{
2862 unsigned long long rettime, calltime;
2863 unsigned long long duration, depth;
2864 struct format_field *field;
2865 int i;
2866
2867 if (latency_format) {
2868 print_lat_fmt(data, size);
2869 printf(" | ");
2870 }
2871
2872 field = find_field(event, "rettime");
2873 if (!field)
2874 die("can't find rettime in return graph");
2875 rettime = read_size(data + field->offset, field->size);
2876
2877 field = find_field(event, "calltime");
2878 if (!field)
2879 die("can't find calltime in return graph");
2880 calltime = read_size(data + field->offset, field->size);
2881
2882 duration = rettime - calltime;
2883
2884 /* Overhead */
2885 print_graph_overhead(duration);
2886
2887 /* Duration */
2888 print_graph_duration(duration);
2889
2890 field = find_field(event, "depth");
2891 if (!field)
2892 die("can't find depth in entry graph");
2893 depth = read_size(data + field->offset, field->size);
2894
2895 /* Function */
2896 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2897 printf(" ");
2898
2899 printf("}");
2900}
2901
2902static void
2903pretty_print_func_graph(void *data, int size, struct event *event,
2904 int cpu, int pid)
2905{
2906 if (event->flags & EVENT_FL_ISFUNCENT)
2907 pretty_print_func_ent(data, size, event, cpu, pid);
2908 else if (event->flags & EVENT_FL_ISFUNCRET)
2909 pretty_print_func_ret(data, size, event);
2910 printf("\n");
2911}
2912
2913void print_trace_event(int cpu, void *data, int size)
2914{
2915 struct event *event;
2916 int type;
2917 int pid;
2918
2919 type = trace_parse_common_type(data);
2920
2921 event = trace_find_event(type);
2922 if (!event) {
2923 warning("ug! no event found for type %d", type);
2924 return;
2925 }
2926
2927 pid = trace_parse_common_pid(data);
2928
2929 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2930 return pretty_print_func_graph(data, size, event, cpu, pid);
2931
2932 if (latency_format)
2933 print_lat_fmt(data, size);
2934
2935 if (event->flags & EVENT_FL_FAILED) {
2936 printf("EVENT '%s' FAILED TO PARSE\n",
2937 event->name);
2938 return;
2939 }
2940
2941 pretty_print(data, size, event);
2942}
2943
2944static void print_fields(struct print_flag_sym *field)
2945{
2946 printf("{ %s, %s }", field->value, field->str);
2947 if (field->next) {
2948 printf(", ");
2949 print_fields(field->next);
2950 }
2951}
2952
2953static void print_args(struct print_arg *args)
2954{
2955 int print_paren = 1;
2956
2957 switch (args->type) {
2958 case PRINT_NULL:
2959 printf("null");
2960 break;
2961 case PRINT_ATOM:
2962 printf("%s", args->atom.atom);
2963 break;
2964 case PRINT_FIELD:
2965 printf("REC->%s", args->field.name);
2966 break;
2967 case PRINT_FLAGS:
2968 printf("__print_flags(");
2969 print_args(args->flags.field);
2970 printf(", %s, ", args->flags.delim);
2971 print_fields(args->flags.flags);
2972 printf(")");
2973 break;
2974 case PRINT_SYMBOL:
2975 printf("__print_symbolic(");
2976 print_args(args->symbol.field);
2977 printf(", ");
2978 print_fields(args->symbol.symbols);
2979 printf(")");
2980 break;
2981 case PRINT_STRING:
2982 printf("__get_str(%s)", args->string.string);
2983 break;
2984 case PRINT_TYPE:
2985 printf("(%s)", args->typecast.type);
2986 print_args(args->typecast.item);
2987 break;
2988 case PRINT_OP:
2989 if (strcmp(args->op.op, ":") == 0)
2990 print_paren = 0;
2991 if (print_paren)
2992 printf("(");
2993 print_args(args->op.left);
2994 printf(" %s ", args->op.op);
2995 print_args(args->op.right);
2996 if (print_paren)
2997 printf(")");
2998 break;
2999 default:
3000 /* we should warn... */
3001 return;
3002 }
3003 if (args->next) {
3004 printf("\n");
3005 print_args(args->next);
3006 }
3007}
3008
3009int parse_ftrace_file(char *buf, unsigned long size)
3010{
3011 struct format_field *field;
3012 struct print_arg *arg, **list;
3013 struct event *event;
3014 int ret;
3015
3016 init_input_buf(buf, size);
3017
3018 event = alloc_event();
3019 if (!event)
3020 return -ENOMEM;
3021
3022 event->flags |= EVENT_FL_ISFTRACE;
3023
3024 event->name = event_read_name();
3025 if (!event->name)
3026 die("failed to read ftrace event name");
3027
3028 if (strcmp(event->name, "function") == 0)
3029 event->flags |= EVENT_FL_ISFUNC;
3030
3031 else if (strcmp(event->name, "funcgraph_entry") == 0)
3032 event->flags |= EVENT_FL_ISFUNCENT;
3033
3034 else if (strcmp(event->name, "funcgraph_exit") == 0)
3035 event->flags |= EVENT_FL_ISFUNCRET;
3036
3037 else if (strcmp(event->name, "bprint") == 0)
3038 event->flags |= EVENT_FL_ISBPRINT;
3039
3040 event->id = event_read_id();
3041 if (event->id < 0)
3042 die("failed to read ftrace event id");
3043
3044 add_event(event);
3045
3046 ret = event_read_format(event);
3047 if (ret < 0)
3048 die("failed to read ftrace event format");
3049
3050 ret = event_read_print(event);
3051 if (ret < 0)
3052 die("failed to read ftrace event print fmt");
3053
3054 /* New ftrace handles args */
3055 if (ret > 0)
3056 return 0;
3057 /*
3058 * The arguments for ftrace files are parsed by the fields.
3059 * Set up the fields as their arguments.
3060 */
3061 list = &event->print_fmt.args;
3062 for (field = event->format.fields; field; field = field->next) {
3063 arg = malloc_or_die(sizeof(*arg));
3064 memset(arg, 0, sizeof(*arg));
3065 *list = arg;
3066 list = &arg->next;
3067 arg->type = PRINT_FIELD;
3068 arg->field.name = field->name;
3069 arg->field.field = field;
3070 }
3071 return 0;
3072}
3073
3074int parse_event_file(char *buf, unsigned long size, char *sys)
3075{
3076 struct event *event;
3077 int ret;
3078
3079 init_input_buf(buf, size);
3080
3081 event = alloc_event();
3082 if (!event)
3083 return -ENOMEM;
3084
3085 event->name = event_read_name();
3086 if (!event->name)
3087 die("failed to read event name");
3088
3089 event->id = event_read_id();
3090 if (event->id < 0)
3091 die("failed to read event id");
3092
3093 ret = event_read_format(event);
3094 if (ret < 0) {
3095 warning("failed to read event format for %s", event->name);
3096 goto event_failed;
3097 }
3098
3099 ret = event_read_print(event);
3100 if (ret < 0) {
3101 warning("failed to read event print fmt for %s", event->name);
3102 goto event_failed;
3103 }
3104
3105 event->system = strdup(sys);
3106
3107#define PRINT_ARGS 0
3108 if (PRINT_ARGS && event->print_fmt.args)
3109 print_args(event->print_fmt.args);
3110
3111 add_event(event);
3112 return 0;
3113
3114 event_failed:
3115 event->flags |= EVENT_FL_FAILED;
3116 /* still add it even if it failed */
3117 add_event(event);
3118 return -1;
3119}
3120
3121void parse_set_info(int nr_cpus, int long_sz)
3122{
3123 cpus = nr_cpus;
3124 long_size = long_sz;
3125}
diff --git a/tools/perf/util/trace-parse-events.h b/tools/perf/util/trace-parse-events.h
new file mode 100644
index 000000000000..794ec15ef084
--- /dev/null
+++ b/tools/perf/util/trace-parse-events.h
@@ -0,0 +1,273 @@
1#ifndef __PERF_TRACE_EVENTS_H
2#define __PERF_TRACE_EVENTS_H
3
4#include <stdbool.h>
5#include <unistd.h>
6
7#define __unused __attribute__((unused))
8
9
10#ifndef PAGE_MASK
11#define PAGE_MASK (page_size - 1)
12#endif
13
14enum {
15 RINGBUF_TYPE_PADDING = 29,
16 RINGBUF_TYPE_TIME_EXTEND = 30,
17 RINGBUF_TYPE_TIME_STAMP = 31,
18};
19
20#ifndef TS_SHIFT
21#define TS_SHIFT 27
22#endif
23
24#define NSECS_PER_SEC 1000000000ULL
25#define NSECS_PER_USEC 1000ULL
26
27enum format_flags {
28 FIELD_IS_ARRAY = 1,
29 FIELD_IS_POINTER = 2,
30 FIELD_IS_SIGNED = 4,
31 FIELD_IS_STRING = 8,
32 FIELD_IS_DYNAMIC = 16,
33 FIELD_IS_FLAG = 32,
34 FIELD_IS_SYMBOLIC = 64,
35};
36
37struct format_field {
38 struct format_field *next;
39 char *type;
40 char *name;
41 int offset;
42 int size;
43 unsigned long flags;
44};
45
46struct format {
47 int nr_common;
48 int nr_fields;
49 struct format_field *common_fields;
50 struct format_field *fields;
51};
52
53struct print_arg_atom {
54 char *atom;
55};
56
57struct print_arg_string {
58 char *string;
59 int offset;
60};
61
62struct print_arg_field {
63 char *name;
64 struct format_field *field;
65};
66
67struct print_flag_sym {
68 struct print_flag_sym *next;
69 char *value;
70 char *str;
71};
72
73struct print_arg_typecast {
74 char *type;
75 struct print_arg *item;
76};
77
78struct print_arg_flags {
79 struct print_arg *field;
80 char *delim;
81 struct print_flag_sym *flags;
82};
83
84struct print_arg_symbol {
85 struct print_arg *field;
86 struct print_flag_sym *symbols;
87};
88
89struct print_arg;
90
91struct print_arg_op {
92 char *op;
93 int prio;
94 struct print_arg *left;
95 struct print_arg *right;
96};
97
98struct print_arg_func {
99 char *name;
100 struct print_arg *args;
101};
102
103enum print_arg_type {
104 PRINT_NULL,
105 PRINT_ATOM,
106 PRINT_FIELD,
107 PRINT_FLAGS,
108 PRINT_SYMBOL,
109 PRINT_TYPE,
110 PRINT_STRING,
111 PRINT_OP,
112};
113
114struct print_arg {
115 struct print_arg *next;
116 enum print_arg_type type;
117 union {
118 struct print_arg_atom atom;
119 struct print_arg_field field;
120 struct print_arg_typecast typecast;
121 struct print_arg_flags flags;
122 struct print_arg_symbol symbol;
123 struct print_arg_func func;
124 struct print_arg_string string;
125 struct print_arg_op op;
126 };
127};
128
129struct print_fmt {
130 char *format;
131 struct print_arg *args;
132};
133
134struct event {
135 struct event *next;
136 char *name;
137 int id;
138 int flags;
139 struct format format;
140 struct print_fmt print_fmt;
141 char *system;
142};
143
144enum {
145 EVENT_FL_ISFTRACE = 0x01,
146 EVENT_FL_ISPRINT = 0x02,
147 EVENT_FL_ISBPRINT = 0x04,
148 EVENT_FL_ISFUNC = 0x08,
149 EVENT_FL_ISFUNCENT = 0x10,
150 EVENT_FL_ISFUNCRET = 0x20,
151
152 EVENT_FL_FAILED = 0x80000000
153};
154
155struct record {
156 unsigned long long ts;
157 int size;
158 void *data;
159};
160
161struct record *trace_peek_data(int cpu);
162struct record *trace_read_data(int cpu);
163
164void parse_set_info(int nr_cpus, int long_sz);
165
166ssize_t trace_report(int fd, bool repipe);
167
168void *malloc_or_die(unsigned int size);
169
170void parse_cmdlines(char *file, int size);
171void parse_proc_kallsyms(char *file, unsigned int size);
172void parse_ftrace_printk(char *file, unsigned int size);
173
174void print_funcs(void);
175void print_printk(void);
176
177int parse_ftrace_file(char *buf, unsigned long size);
178int parse_event_file(char *buf, unsigned long size, char *sys);
179void print_trace_event(int cpu, void *data, int size);
180
181extern int file_bigendian;
182extern int host_bigendian;
183
184int bigendian(void);
185
186static inline unsigned short __data2host2(unsigned short data)
187{
188 unsigned short swap;
189
190 if (host_bigendian == file_bigendian)
191 return data;
192
193 swap = ((data & 0xffULL) << 8) |
194 ((data & (0xffULL << 8)) >> 8);
195
196 return swap;
197}
198
199static inline unsigned int __data2host4(unsigned int data)
200{
201 unsigned int swap;
202
203 if (host_bigendian == file_bigendian)
204 return data;
205
206 swap = ((data & 0xffULL) << 24) |
207 ((data & (0xffULL << 8)) << 8) |
208 ((data & (0xffULL << 16)) >> 8) |
209 ((data & (0xffULL << 24)) >> 24);
210
211 return swap;
212}
213
214static inline unsigned long long __data2host8(unsigned long long data)
215{
216 unsigned long long swap;
217
218 if (host_bigendian == file_bigendian)
219 return data;
220
221 swap = ((data & 0xffULL) << 56) |
222 ((data & (0xffULL << 8)) << 40) |
223 ((data & (0xffULL << 16)) << 24) |
224 ((data & (0xffULL << 24)) << 8) |
225 ((data & (0xffULL << 32)) >> 8) |
226 ((data & (0xffULL << 40)) >> 24) |
227 ((data & (0xffULL << 48)) >> 40) |
228 ((data & (0xffULL << 56)) >> 56);
229
230 return swap;
231}
232
233#define data2host2(ptr) __data2host2(*(unsigned short *)ptr)
234#define data2host4(ptr) __data2host4(*(unsigned int *)ptr)
235#define data2host8(ptr) ({ \
236 unsigned long long __val; \
237 \
238 memcpy(&__val, (ptr), sizeof(unsigned long long)); \
239 __data2host8(__val); \
240})
241
242extern int header_page_ts_offset;
243extern int header_page_ts_size;
244extern int header_page_size_offset;
245extern int header_page_size_size;
246extern int header_page_data_offset;
247extern int header_page_data_size;
248
249extern bool latency_format;
250
251int trace_parse_common_type(void *data);
252int trace_parse_common_pid(void *data);
253int parse_common_pc(void *data);
254int parse_common_flags(void *data);
255int parse_common_lock_depth(void *data);
256struct event *trace_find_event(int id);
257struct event *trace_find_next_event(struct event *event);
258unsigned long long read_size(void *ptr, int size);
259unsigned long long
260raw_field_value(struct event *event, const char *name, void *data);
261void *raw_field_ptr(struct event *event, const char *name, void *data);
262unsigned long long eval_flag(const char *flag);
263
264/* taken from kernel/trace/trace.h */
265enum trace_flag_type {
266 TRACE_FLAG_IRQS_OFF = 0x01,
267 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
268 TRACE_FLAG_NEED_RESCHED = 0x04,
269 TRACE_FLAG_HARDIRQ = 0x08,
270 TRACE_FLAG_SOFTIRQ = 0x10,
271};
272
273#endif /* __PERF_TRACE_EVENTS_H */