Do not increment pool indexes twice
[pohmelfs.git] / tools / perf / util / trace-event-parse.c
blob1a8d4dc4f386b5bee9897686986cf4fa31548b9b
1 /*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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!)
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.
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
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
31 #include "../perf.h"
32 #include "util.h"
33 #include "trace-event.h"
35 int header_page_ts_offset;
36 int header_page_ts_size;
37 int header_page_size_offset;
38 int header_page_size_size;
39 int header_page_overwrite_offset;
40 int header_page_overwrite_size;
41 int header_page_data_offset;
42 int header_page_data_size;
44 bool latency_format;
46 static char *input_buf;
47 static unsigned long long input_buf_ptr;
48 static unsigned long long input_buf_siz;
50 static int cpus;
51 static int long_size;
52 static int is_flag_field;
53 static int is_symbolic_field;
55 static struct format_field *
56 find_any_field(struct event *event, const char *name);
58 static void init_input_buf(char *buf, unsigned long long size)
60 input_buf = buf;
61 input_buf_siz = size;
62 input_buf_ptr = 0;
65 struct cmdline {
66 char *comm;
67 int pid;
70 static struct cmdline *cmdlines;
71 static int cmdline_count;
73 static int cmdline_cmp(const void *a, const void *b)
75 const struct cmdline *ca = a;
76 const struct cmdline *cb = b;
78 if (ca->pid < cb->pid)
79 return -1;
80 if (ca->pid > cb->pid)
81 return 1;
83 return 0;
86 void parse_cmdlines(char *file, int size __unused)
88 struct cmdline_list {
89 struct cmdline_list *next;
90 char *comm;
91 int pid;
92 } *list = NULL, *item;
93 char *line;
94 char *next = NULL;
95 int i;
97 line = strtok_r(file, "\n", &next);
98 while (line) {
99 item = malloc_or_die(sizeof(*item));
100 sscanf(line, "%d %as", &item->pid,
101 (float *)(void *)&item->comm); /* workaround gcc warning */
102 item->next = list;
103 list = item;
104 line = strtok_r(NULL, "\n", &next);
105 cmdline_count++;
108 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
110 i = 0;
111 while (list) {
112 cmdlines[i].pid = list->pid;
113 cmdlines[i].comm = list->comm;
114 i++;
115 item = list;
116 list = list->next;
117 free(item);
120 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
123 static struct func_map {
124 unsigned long long addr;
125 char *func;
126 char *mod;
127 } *func_list;
128 static unsigned int func_count;
130 static int func_cmp(const void *a, const void *b)
132 const struct func_map *fa = a;
133 const struct func_map *fb = b;
135 if (fa->addr < fb->addr)
136 return -1;
137 if (fa->addr > fb->addr)
138 return 1;
140 return 0;
143 void parse_proc_kallsyms(char *file, unsigned int size __unused)
145 struct func_list {
146 struct func_list *next;
147 unsigned long long addr;
148 char *func;
149 char *mod;
150 } *list = NULL, *item;
151 char *line;
152 char *next = NULL;
153 char *addr_str;
154 char ch;
155 int ret __used;
156 int i;
158 line = strtok_r(file, "\n", &next);
159 while (line) {
160 item = malloc_or_die(sizeof(*item));
161 item->mod = NULL;
162 ret = sscanf(line, "%as %c %as\t[%as",
163 (float *)(void *)&addr_str, /* workaround gcc warning */
164 &ch,
165 (float *)(void *)&item->func,
166 (float *)(void *)&item->mod);
167 item->addr = strtoull(addr_str, NULL, 16);
168 free(addr_str);
170 /* truncate the extra ']' */
171 if (item->mod)
172 item->mod[strlen(item->mod) - 1] = 0;
175 item->next = list;
176 list = item;
177 line = strtok_r(NULL, "\n", &next);
178 func_count++;
181 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
183 i = 0;
184 while (list) {
185 func_list[i].func = list->func;
186 func_list[i].addr = list->addr;
187 func_list[i].mod = list->mod;
188 i++;
189 item = list;
190 list = list->next;
191 free(item);
194 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
197 * Add a special record at the end.
199 func_list[func_count].func = NULL;
200 func_list[func_count].addr = 0;
201 func_list[func_count].mod = NULL;
205 * We are searching for a record in between, not an exact
206 * match.
208 static int func_bcmp(const void *a, const void *b)
210 const struct func_map *fa = a;
211 const struct func_map *fb = b;
213 if ((fa->addr == fb->addr) ||
215 (fa->addr > fb->addr &&
216 fa->addr < (fb+1)->addr))
217 return 0;
219 if (fa->addr < fb->addr)
220 return -1;
222 return 1;
225 static struct func_map *find_func(unsigned long long addr)
227 struct func_map *func;
228 struct func_map key;
230 key.addr = addr;
232 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
233 func_bcmp);
235 return func;
238 void print_funcs(void)
240 int i;
242 for (i = 0; i < (int)func_count; i++) {
243 printf("%016llx %s",
244 func_list[i].addr,
245 func_list[i].func);
246 if (func_list[i].mod)
247 printf(" [%s]\n", func_list[i].mod);
248 else
249 printf("\n");
253 static struct printk_map {
254 unsigned long long addr;
255 char *printk;
256 } *printk_list;
257 static unsigned int printk_count;
259 static int printk_cmp(const void *a, const void *b)
261 const struct func_map *fa = a;
262 const struct func_map *fb = b;
264 if (fa->addr < fb->addr)
265 return -1;
266 if (fa->addr > fb->addr)
267 return 1;
269 return 0;
272 static struct printk_map *find_printk(unsigned long long addr)
274 struct printk_map *printk;
275 struct printk_map key;
277 key.addr = addr;
279 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
280 printk_cmp);
282 return printk;
285 void parse_ftrace_printk(char *file, unsigned int size __unused)
287 struct printk_list {
288 struct printk_list *next;
289 unsigned long long addr;
290 char *printk;
291 } *list = NULL, *item;
292 char *line;
293 char *next = NULL;
294 char *addr_str;
295 int i;
297 line = strtok_r(file, "\n", &next);
298 while (line) {
299 addr_str = strsep(&line, ":");
300 if (!line) {
301 warning("error parsing print strings");
302 break;
304 item = malloc_or_die(sizeof(*item));
305 item->addr = strtoull(addr_str, NULL, 16);
306 /* fmt still has a space, skip it */
307 item->printk = strdup(line+1);
308 item->next = list;
309 list = item;
310 line = strtok_r(NULL, "\n", &next);
311 printk_count++;
314 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
316 i = 0;
317 while (list) {
318 printk_list[i].printk = list->printk;
319 printk_list[i].addr = list->addr;
320 i++;
321 item = list;
322 list = list->next;
323 free(item);
326 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
329 void print_printk(void)
331 int i;
333 for (i = 0; i < (int)printk_count; i++) {
334 printf("%016llx %s\n",
335 printk_list[i].addr,
336 printk_list[i].printk);
340 static struct event *alloc_event(void)
342 struct event *event;
344 event = malloc_or_die(sizeof(*event));
345 memset(event, 0, sizeof(*event));
347 return event;
350 enum event_type {
351 EVENT_ERROR,
352 EVENT_NONE,
353 EVENT_SPACE,
354 EVENT_NEWLINE,
355 EVENT_OP,
356 EVENT_DELIM,
357 EVENT_ITEM,
358 EVENT_DQUOTE,
359 EVENT_SQUOTE,
362 static struct event *event_list;
364 static void add_event(struct event *event)
366 event->next = event_list;
367 event_list = event;
370 static int event_item_type(enum event_type type)
372 switch (type) {
373 case EVENT_ITEM ... EVENT_SQUOTE:
374 return 1;
375 case EVENT_ERROR ... EVENT_DELIM:
376 default:
377 return 0;
381 static void free_arg(struct print_arg *arg)
383 if (!arg)
384 return;
386 switch (arg->type) {
387 case PRINT_ATOM:
388 if (arg->atom.atom)
389 free(arg->atom.atom);
390 break;
391 case PRINT_NULL:
392 case PRINT_FIELD ... PRINT_OP:
393 default:
394 /* todo */
395 break;
398 free(arg);
401 static enum event_type get_type(int ch)
403 if (ch == '\n')
404 return EVENT_NEWLINE;
405 if (isspace(ch))
406 return EVENT_SPACE;
407 if (isalnum(ch) || ch == '_')
408 return EVENT_ITEM;
409 if (ch == '\'')
410 return EVENT_SQUOTE;
411 if (ch == '"')
412 return EVENT_DQUOTE;
413 if (!isprint(ch))
414 return EVENT_NONE;
415 if (ch == '(' || ch == ')' || ch == ',')
416 return EVENT_DELIM;
418 return EVENT_OP;
421 static int __read_char(void)
423 if (input_buf_ptr >= input_buf_siz)
424 return -1;
426 return input_buf[input_buf_ptr++];
429 static int __peek_char(void)
431 if (input_buf_ptr >= input_buf_siz)
432 return -1;
434 return input_buf[input_buf_ptr];
437 static enum event_type __read_token(char **tok)
439 char buf[BUFSIZ];
440 int ch, last_ch, quote_ch, next_ch;
441 int i = 0;
442 int tok_size = 0;
443 enum event_type type;
445 *tok = NULL;
448 ch = __read_char();
449 if (ch < 0)
450 return EVENT_NONE;
452 type = get_type(ch);
453 if (type == EVENT_NONE)
454 return type;
456 buf[i++] = ch;
458 switch (type) {
459 case EVENT_NEWLINE:
460 case EVENT_DELIM:
461 *tok = malloc_or_die(2);
462 (*tok)[0] = ch;
463 (*tok)[1] = 0;
464 return type;
466 case EVENT_OP:
467 switch (ch) {
468 case '-':
469 next_ch = __peek_char();
470 if (next_ch == '>') {
471 buf[i++] = __read_char();
472 break;
474 /* fall through */
475 case '+':
476 case '|':
477 case '&':
478 case '>':
479 case '<':
480 last_ch = ch;
481 ch = __peek_char();
482 if (ch != last_ch)
483 goto test_equal;
484 buf[i++] = __read_char();
485 switch (last_ch) {
486 case '>':
487 case '<':
488 goto test_equal;
489 default:
490 break;
492 break;
493 case '!':
494 case '=':
495 goto test_equal;
496 default: /* what should we do instead? */
497 break;
499 buf[i] = 0;
500 *tok = strdup(buf);
501 return type;
503 test_equal:
504 ch = __peek_char();
505 if (ch == '=')
506 buf[i++] = __read_char();
507 break;
509 case EVENT_DQUOTE:
510 case EVENT_SQUOTE:
511 /* don't keep quotes */
512 i--;
513 quote_ch = ch;
514 last_ch = 0;
515 do {
516 if (i == (BUFSIZ - 1)) {
517 buf[i] = 0;
518 if (*tok) {
519 *tok = realloc(*tok, tok_size + BUFSIZ);
520 if (!*tok)
521 return EVENT_NONE;
522 strcat(*tok, buf);
523 } else
524 *tok = strdup(buf);
526 if (!*tok)
527 return EVENT_NONE;
528 tok_size += BUFSIZ;
529 i = 0;
531 last_ch = ch;
532 ch = __read_char();
533 buf[i++] = ch;
534 /* the '\' '\' will cancel itself */
535 if (ch == '\\' && last_ch == '\\')
536 last_ch = 0;
537 } while (ch != quote_ch || last_ch == '\\');
538 /* remove the last quote */
539 i--;
540 goto out;
542 case EVENT_ERROR ... EVENT_SPACE:
543 case EVENT_ITEM:
544 default:
545 break;
548 while (get_type(__peek_char()) == type) {
549 if (i == (BUFSIZ - 1)) {
550 buf[i] = 0;
551 if (*tok) {
552 *tok = realloc(*tok, tok_size + BUFSIZ);
553 if (!*tok)
554 return EVENT_NONE;
555 strcat(*tok, buf);
556 } else
557 *tok = strdup(buf);
559 if (!*tok)
560 return EVENT_NONE;
561 tok_size += BUFSIZ;
562 i = 0;
564 ch = __read_char();
565 buf[i++] = ch;
568 out:
569 buf[i] = 0;
570 if (*tok) {
571 *tok = realloc(*tok, tok_size + i);
572 if (!*tok)
573 return EVENT_NONE;
574 strcat(*tok, buf);
575 } else
576 *tok = strdup(buf);
577 if (!*tok)
578 return EVENT_NONE;
580 return type;
583 static void free_token(char *tok)
585 if (tok)
586 free(tok);
589 static enum event_type read_token(char **tok)
591 enum event_type type;
593 for (;;) {
594 type = __read_token(tok);
595 if (type != EVENT_SPACE)
596 return type;
598 free_token(*tok);
601 /* not reached */
602 return EVENT_NONE;
605 /* no newline */
606 static enum event_type read_token_item(char **tok)
608 enum event_type type;
610 for (;;) {
611 type = __read_token(tok);
612 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
613 return type;
615 free_token(*tok);
618 /* not reached */
619 return EVENT_NONE;
622 static int test_type(enum event_type type, enum event_type expect)
624 if (type != expect) {
625 warning("Error: expected type %d but read %d",
626 expect, type);
627 return -1;
629 return 0;
632 static int __test_type_token(enum event_type type, char *token,
633 enum event_type expect, const char *expect_tok,
634 bool warn)
636 if (type != expect) {
637 if (warn)
638 warning("Error: expected type %d but read %d",
639 expect, type);
640 return -1;
643 if (strcmp(token, expect_tok) != 0) {
644 if (warn)
645 warning("Error: expected '%s' but read '%s'",
646 expect_tok, token);
647 return -1;
649 return 0;
652 static int test_type_token(enum event_type type, char *token,
653 enum event_type expect, const char *expect_tok)
655 return __test_type_token(type, token, expect, expect_tok, true);
658 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
660 enum event_type type;
662 if (newline_ok)
663 type = read_token(tok);
664 else
665 type = read_token_item(tok);
666 return test_type(type, expect);
669 static int read_expect_type(enum event_type expect, char **tok)
671 return __read_expect_type(expect, tok, 1);
674 static int __read_expected(enum event_type expect, const char *str,
675 int newline_ok, bool warn)
677 enum event_type type;
678 char *token;
679 int ret;
681 if (newline_ok)
682 type = read_token(&token);
683 else
684 type = read_token_item(&token);
686 ret = __test_type_token(type, token, expect, str, warn);
688 free_token(token);
690 return ret;
693 static int read_expected(enum event_type expect, const char *str)
695 return __read_expected(expect, str, 1, true);
698 static int read_expected_item(enum event_type expect, const char *str)
700 return __read_expected(expect, str, 0, true);
703 static char *event_read_name(void)
705 char *token;
707 if (read_expected(EVENT_ITEM, "name") < 0)
708 return NULL;
710 if (read_expected(EVENT_OP, ":") < 0)
711 return NULL;
713 if (read_expect_type(EVENT_ITEM, &token) < 0)
714 goto fail;
716 return token;
718 fail:
719 free_token(token);
720 return NULL;
723 static int event_read_id(void)
725 char *token;
726 int id;
728 if (read_expected_item(EVENT_ITEM, "ID") < 0)
729 return -1;
731 if (read_expected(EVENT_OP, ":") < 0)
732 return -1;
734 if (read_expect_type(EVENT_ITEM, &token) < 0)
735 goto fail;
737 id = strtoul(token, NULL, 0);
738 free_token(token);
739 return id;
741 fail:
742 free_token(token);
743 return -1;
746 static int field_is_string(struct format_field *field)
748 if ((field->flags & FIELD_IS_ARRAY) &&
749 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
750 !strstr(field->type, "s8")))
751 return 1;
753 return 0;
756 static int field_is_dynamic(struct format_field *field)
758 if (!strncmp(field->type, "__data_loc", 10))
759 return 1;
761 return 0;
764 static int event_read_fields(struct event *event, struct format_field **fields)
766 struct format_field *field = NULL;
767 enum event_type type;
768 char *token;
769 char *last_token;
770 int count = 0;
772 do {
773 type = read_token(&token);
774 if (type == EVENT_NEWLINE) {
775 free_token(token);
776 return count;
779 count++;
781 if (test_type_token(type, token, EVENT_ITEM, "field"))
782 goto fail;
783 free_token(token);
785 type = read_token(&token);
787 * The ftrace fields may still use the "special" name.
788 * Just ignore it.
790 if (event->flags & EVENT_FL_ISFTRACE &&
791 type == EVENT_ITEM && strcmp(token, "special") == 0) {
792 free_token(token);
793 type = read_token(&token);
796 if (test_type_token(type, token, EVENT_OP, ":") < 0)
797 return -1;
799 if (read_expect_type(EVENT_ITEM, &token) < 0)
800 goto fail;
802 last_token = token;
804 field = malloc_or_die(sizeof(*field));
805 memset(field, 0, sizeof(*field));
807 /* read the rest of the type */
808 for (;;) {
809 type = read_token(&token);
810 if (type == EVENT_ITEM ||
811 (type == EVENT_OP && strcmp(token, "*") == 0) ||
813 * Some of the ftrace fields are broken and have
814 * an illegal "." in them.
816 (event->flags & EVENT_FL_ISFTRACE &&
817 type == EVENT_OP && strcmp(token, ".") == 0)) {
819 if (strcmp(token, "*") == 0)
820 field->flags |= FIELD_IS_POINTER;
822 if (field->type) {
823 field->type = realloc(field->type,
824 strlen(field->type) +
825 strlen(last_token) + 2);
826 strcat(field->type, " ");
827 strcat(field->type, last_token);
828 } else
829 field->type = last_token;
830 last_token = token;
831 continue;
834 break;
837 if (!field->type) {
838 die("no type found");
839 goto fail;
841 field->name = last_token;
843 if (test_type(type, EVENT_OP))
844 goto fail;
846 if (strcmp(token, "[") == 0) {
847 enum event_type last_type = type;
848 char *brackets = token;
849 int len;
851 field->flags |= FIELD_IS_ARRAY;
853 type = read_token(&token);
854 while (strcmp(token, "]") != 0) {
855 if (last_type == EVENT_ITEM &&
856 type == EVENT_ITEM)
857 len = 2;
858 else
859 len = 1;
860 last_type = type;
862 brackets = realloc(brackets,
863 strlen(brackets) +
864 strlen(token) + len);
865 if (len == 2)
866 strcat(brackets, " ");
867 strcat(brackets, token);
868 free_token(token);
869 type = read_token(&token);
870 if (type == EVENT_NONE) {
871 die("failed to find token");
872 goto fail;
876 free_token(token);
878 brackets = realloc(brackets, strlen(brackets) + 2);
879 strcat(brackets, "]");
881 /* add brackets to type */
883 type = read_token(&token);
885 * If the next token is not an OP, then it is of
886 * the format: type [] item;
888 if (type == EVENT_ITEM) {
889 field->type = realloc(field->type,
890 strlen(field->type) +
891 strlen(field->name) +
892 strlen(brackets) + 2);
893 strcat(field->type, " ");
894 strcat(field->type, field->name);
895 free_token(field->name);
896 strcat(field->type, brackets);
897 field->name = token;
898 type = read_token(&token);
899 } else {
900 field->type = realloc(field->type,
901 strlen(field->type) +
902 strlen(brackets) + 1);
903 strcat(field->type, brackets);
905 free(brackets);
908 if (field_is_string(field)) {
909 field->flags |= FIELD_IS_STRING;
910 if (field_is_dynamic(field))
911 field->flags |= FIELD_IS_DYNAMIC;
914 if (test_type_token(type, token, EVENT_OP, ";"))
915 goto fail;
916 free_token(token);
918 if (read_expected(EVENT_ITEM, "offset") < 0)
919 goto fail_expect;
921 if (read_expected(EVENT_OP, ":") < 0)
922 goto fail_expect;
924 if (read_expect_type(EVENT_ITEM, &token))
925 goto fail;
926 field->offset = strtoul(token, NULL, 0);
927 free_token(token);
929 if (read_expected(EVENT_OP, ";") < 0)
930 goto fail_expect;
932 if (read_expected(EVENT_ITEM, "size") < 0)
933 goto fail_expect;
935 if (read_expected(EVENT_OP, ":") < 0)
936 goto fail_expect;
938 if (read_expect_type(EVENT_ITEM, &token))
939 goto fail;
940 field->size = strtoul(token, NULL, 0);
941 free_token(token);
943 if (read_expected(EVENT_OP, ";") < 0)
944 goto fail_expect;
946 type = read_token(&token);
947 if (type != EVENT_NEWLINE) {
948 /* newer versions of the kernel have a "signed" type */
949 if (test_type_token(type, token, EVENT_ITEM, "signed"))
950 goto fail;
952 free_token(token);
954 if (read_expected(EVENT_OP, ":") < 0)
955 goto fail_expect;
957 if (read_expect_type(EVENT_ITEM, &token))
958 goto fail;
960 if (strtoul(token, NULL, 0))
961 field->flags |= FIELD_IS_SIGNED;
963 free_token(token);
964 if (read_expected(EVENT_OP, ";") < 0)
965 goto fail_expect;
967 if (read_expect_type(EVENT_NEWLINE, &token))
968 goto fail;
971 free_token(token);
973 *fields = field;
974 fields = &field->next;
976 } while (1);
978 return 0;
980 fail:
981 free_token(token);
982 fail_expect:
983 if (field)
984 free(field);
985 return -1;
988 static int event_read_format(struct event *event)
990 char *token;
991 int ret;
993 if (read_expected_item(EVENT_ITEM, "format") < 0)
994 return -1;
996 if (read_expected(EVENT_OP, ":") < 0)
997 return -1;
999 if (read_expect_type(EVENT_NEWLINE, &token))
1000 goto fail;
1001 free_token(token);
1003 ret = event_read_fields(event, &event->format.common_fields);
1004 if (ret < 0)
1005 return ret;
1006 event->format.nr_common = ret;
1008 ret = event_read_fields(event, &event->format.fields);
1009 if (ret < 0)
1010 return ret;
1011 event->format.nr_fields = ret;
1013 return 0;
1015 fail:
1016 free_token(token);
1017 return -1;
1020 enum event_type
1021 process_arg_token(struct event *event, struct print_arg *arg,
1022 char **tok, enum event_type type);
1024 static enum event_type
1025 process_arg(struct event *event, struct print_arg *arg, char **tok)
1027 enum event_type type;
1028 char *token;
1030 type = read_token(&token);
1031 *tok = token;
1033 return process_arg_token(event, arg, tok, type);
1036 static enum event_type
1037 process_cond(struct event *event, struct print_arg *top, char **tok)
1039 struct print_arg *arg, *left, *right;
1040 enum event_type type;
1041 char *token = NULL;
1043 arg = malloc_or_die(sizeof(*arg));
1044 memset(arg, 0, sizeof(*arg));
1046 left = malloc_or_die(sizeof(*left));
1048 right = malloc_or_die(sizeof(*right));
1050 arg->type = PRINT_OP;
1051 arg->op.left = left;
1052 arg->op.right = right;
1054 *tok = NULL;
1055 type = process_arg(event, left, &token);
1056 if (test_type_token(type, token, EVENT_OP, ":"))
1057 goto out_free;
1059 arg->op.op = token;
1061 type = process_arg(event, right, &token);
1063 top->op.right = arg;
1065 *tok = token;
1066 return type;
1068 out_free:
1069 free_token(*tok);
1070 free(right);
1071 free(left);
1072 free_arg(arg);
1073 return EVENT_ERROR;
1076 static enum event_type
1077 process_array(struct event *event, struct print_arg *top, char **tok)
1079 struct print_arg *arg;
1080 enum event_type type;
1081 char *token = NULL;
1083 arg = malloc_or_die(sizeof(*arg));
1084 memset(arg, 0, sizeof(*arg));
1086 *tok = NULL;
1087 type = process_arg(event, arg, &token);
1088 if (test_type_token(type, token, EVENT_OP, "]"))
1089 goto out_free;
1091 top->op.right = arg;
1093 free_token(token);
1094 type = read_token_item(&token);
1095 *tok = token;
1097 return type;
1099 out_free:
1100 free_token(*tok);
1101 free_arg(arg);
1102 return EVENT_ERROR;
1105 static int get_op_prio(char *op)
1107 if (!op[1]) {
1108 switch (op[0]) {
1109 case '*':
1110 case '/':
1111 case '%':
1112 return 6;
1113 case '+':
1114 case '-':
1115 return 7;
1116 /* '>>' and '<<' are 8 */
1117 case '<':
1118 case '>':
1119 return 9;
1120 /* '==' and '!=' are 10 */
1121 case '&':
1122 return 11;
1123 case '^':
1124 return 12;
1125 case '|':
1126 return 13;
1127 case '?':
1128 return 16;
1129 default:
1130 die("unknown op '%c'", op[0]);
1131 return -1;
1133 } else {
1134 if (strcmp(op, "++") == 0 ||
1135 strcmp(op, "--") == 0) {
1136 return 3;
1137 } else if (strcmp(op, ">>") == 0 ||
1138 strcmp(op, "<<") == 0) {
1139 return 8;
1140 } else if (strcmp(op, ">=") == 0 ||
1141 strcmp(op, "<=") == 0) {
1142 return 9;
1143 } else if (strcmp(op, "==") == 0 ||
1144 strcmp(op, "!=") == 0) {
1145 return 10;
1146 } else if (strcmp(op, "&&") == 0) {
1147 return 14;
1148 } else if (strcmp(op, "||") == 0) {
1149 return 15;
1150 } else {
1151 die("unknown op '%s'", op);
1152 return -1;
1157 static void set_op_prio(struct print_arg *arg)
1160 /* single ops are the greatest */
1161 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1162 arg->op.prio = 0;
1163 return;
1166 arg->op.prio = get_op_prio(arg->op.op);
1169 static enum event_type
1170 process_op(struct event *event, struct print_arg *arg, char **tok)
1172 struct print_arg *left, *right = NULL;
1173 enum event_type type;
1174 char *token;
1176 /* the op is passed in via tok */
1177 token = *tok;
1179 if (arg->type == PRINT_OP && !arg->op.left) {
1180 /* handle single op */
1181 if (token[1]) {
1182 die("bad op token %s", token);
1183 return EVENT_ERROR;
1185 switch (token[0]) {
1186 case '!':
1187 case '+':
1188 case '-':
1189 break;
1190 default:
1191 die("bad op token %s", token);
1192 return EVENT_ERROR;
1195 /* make an empty left */
1196 left = malloc_or_die(sizeof(*left));
1197 left->type = PRINT_NULL;
1198 arg->op.left = left;
1200 right = malloc_or_die(sizeof(*right));
1201 arg->op.right = right;
1203 type = process_arg(event, right, tok);
1205 } else if (strcmp(token, "?") == 0) {
1207 left = malloc_or_die(sizeof(*left));
1208 /* copy the top arg to the left */
1209 *left = *arg;
1211 arg->type = PRINT_OP;
1212 arg->op.op = token;
1213 arg->op.left = left;
1214 arg->op.prio = 0;
1216 type = process_cond(event, arg, tok);
1218 } else if (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 strcmp(token, "==") == 0 ||
1232 strcmp(token, "!=") == 0) {
1234 left = malloc_or_die(sizeof(*left));
1236 /* copy the top arg to the left */
1237 *left = *arg;
1239 arg->type = PRINT_OP;
1240 arg->op.op = token;
1241 arg->op.left = left;
1243 set_op_prio(arg);
1245 right = malloc_or_die(sizeof(*right));
1247 type = read_token_item(&token);
1248 *tok = token;
1250 /* could just be a type pointer */
1251 if ((strcmp(arg->op.op, "*") == 0) &&
1252 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1253 if (left->type != PRINT_ATOM)
1254 die("bad pointer type");
1255 left->atom.atom = realloc(left->atom.atom,
1256 sizeof(left->atom.atom) + 3);
1257 strcat(left->atom.atom, " *");
1258 *arg = *left;
1259 free(arg);
1261 return type;
1264 type = process_arg_token(event, right, tok, type);
1266 arg->op.right = right;
1268 } else if (strcmp(token, "[") == 0) {
1270 left = malloc_or_die(sizeof(*left));
1271 *left = *arg;
1273 arg->type = PRINT_OP;
1274 arg->op.op = token;
1275 arg->op.left = left;
1277 arg->op.prio = 0;
1278 type = process_array(event, arg, tok);
1280 } else {
1281 warning("unknown op '%s'", token);
1282 event->flags |= EVENT_FL_FAILED;
1283 /* the arg is now the left side */
1284 return EVENT_NONE;
1287 if (type == EVENT_OP) {
1288 int prio;
1290 /* higher prios need to be closer to the root */
1291 prio = get_op_prio(*tok);
1293 if (prio > arg->op.prio)
1294 return process_op(event, arg, tok);
1296 return process_op(event, right, tok);
1299 return type;
1302 static enum event_type
1303 process_entry(struct event *event __unused, struct print_arg *arg,
1304 char **tok)
1306 enum event_type type;
1307 char *field;
1308 char *token;
1310 if (read_expected(EVENT_OP, "->") < 0)
1311 return EVENT_ERROR;
1313 if (read_expect_type(EVENT_ITEM, &token) < 0)
1314 goto fail;
1315 field = token;
1317 arg->type = PRINT_FIELD;
1318 arg->field.name = field;
1320 if (is_flag_field) {
1321 arg->field.field = find_any_field(event, arg->field.name);
1322 arg->field.field->flags |= FIELD_IS_FLAG;
1323 is_flag_field = 0;
1324 } else if (is_symbolic_field) {
1325 arg->field.field = find_any_field(event, arg->field.name);
1326 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1327 is_symbolic_field = 0;
1330 type = read_token(&token);
1331 *tok = token;
1333 return type;
1335 fail:
1336 free_token(token);
1337 return EVENT_ERROR;
1340 static char *arg_eval (struct print_arg *arg);
1342 static long long arg_num_eval(struct print_arg *arg)
1344 long long left, right;
1345 long long val = 0;
1347 switch (arg->type) {
1348 case PRINT_ATOM:
1349 val = strtoll(arg->atom.atom, NULL, 0);
1350 break;
1351 case PRINT_TYPE:
1352 val = arg_num_eval(arg->typecast.item);
1353 break;
1354 case PRINT_OP:
1355 switch (arg->op.op[0]) {
1356 case '|':
1357 left = arg_num_eval(arg->op.left);
1358 right = arg_num_eval(arg->op.right);
1359 if (arg->op.op[1])
1360 val = left || right;
1361 else
1362 val = left | right;
1363 break;
1364 case '&':
1365 left = arg_num_eval(arg->op.left);
1366 right = arg_num_eval(arg->op.right);
1367 if (arg->op.op[1])
1368 val = left && right;
1369 else
1370 val = left & right;
1371 break;
1372 case '<':
1373 left = arg_num_eval(arg->op.left);
1374 right = arg_num_eval(arg->op.right);
1375 switch (arg->op.op[1]) {
1376 case 0:
1377 val = left < right;
1378 break;
1379 case '<':
1380 val = left << right;
1381 break;
1382 case '=':
1383 val = left <= right;
1384 break;
1385 default:
1386 die("unknown op '%s'", arg->op.op);
1388 break;
1389 case '>':
1390 left = arg_num_eval(arg->op.left);
1391 right = arg_num_eval(arg->op.right);
1392 switch (arg->op.op[1]) {
1393 case 0:
1394 val = left > right;
1395 break;
1396 case '>':
1397 val = left >> right;
1398 break;
1399 case '=':
1400 val = left >= right;
1401 break;
1402 default:
1403 die("unknown op '%s'", arg->op.op);
1405 break;
1406 case '=':
1407 left = arg_num_eval(arg->op.left);
1408 right = arg_num_eval(arg->op.right);
1410 if (arg->op.op[1] != '=')
1411 die("unknown op '%s'", arg->op.op);
1413 val = left == right;
1414 break;
1415 case '!':
1416 left = arg_num_eval(arg->op.left);
1417 right = arg_num_eval(arg->op.right);
1419 switch (arg->op.op[1]) {
1420 case '=':
1421 val = left != right;
1422 break;
1423 default:
1424 die("unknown op '%s'", arg->op.op);
1426 break;
1427 default:
1428 die("unknown op '%s'", arg->op.op);
1430 break;
1432 case PRINT_NULL:
1433 case PRINT_FIELD ... PRINT_SYMBOL:
1434 case PRINT_STRING:
1435 default:
1436 die("invalid eval type %d", arg->type);
1439 return val;
1442 static char *arg_eval (struct print_arg *arg)
1444 long long val;
1445 static char buf[20];
1447 switch (arg->type) {
1448 case PRINT_ATOM:
1449 return arg->atom.atom;
1450 case PRINT_TYPE:
1451 return arg_eval(arg->typecast.item);
1452 case PRINT_OP:
1453 val = arg_num_eval(arg);
1454 sprintf(buf, "%lld", val);
1455 return buf;
1457 case PRINT_NULL:
1458 case PRINT_FIELD ... PRINT_SYMBOL:
1459 case PRINT_STRING:
1460 default:
1461 die("invalid eval type %d", arg->type);
1462 break;
1465 return NULL;
1468 static enum event_type
1469 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1471 enum event_type type;
1472 struct print_arg *arg = NULL;
1473 struct print_flag_sym *field;
1474 char *token = NULL;
1475 char *value;
1477 do {
1478 free_token(token);
1479 type = read_token_item(&token);
1480 if (test_type_token(type, token, EVENT_OP, "{"))
1481 break;
1483 arg = malloc_or_die(sizeof(*arg));
1485 free_token(token);
1486 type = process_arg(event, arg, &token);
1487 if (test_type_token(type, token, EVENT_DELIM, ","))
1488 goto out_free;
1490 field = malloc_or_die(sizeof(*field));
1491 memset(field, 0, sizeof(*field));
1493 value = arg_eval(arg);
1494 field->value = strdup(value);
1496 free_token(token);
1497 type = process_arg(event, arg, &token);
1498 if (test_type_token(type, token, EVENT_OP, "}"))
1499 goto out_free;
1501 value = arg_eval(arg);
1502 field->str = strdup(value);
1503 free_arg(arg);
1504 arg = NULL;
1506 *list = field;
1507 list = &field->next;
1509 free_token(token);
1510 type = read_token_item(&token);
1511 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1513 *tok = token;
1514 return type;
1516 out_free:
1517 free_arg(arg);
1518 free_token(token);
1520 return EVENT_ERROR;
1523 static enum event_type
1524 process_flags(struct event *event, struct print_arg *arg, char **tok)
1526 struct print_arg *field;
1527 enum event_type type;
1528 char *token;
1530 memset(arg, 0, sizeof(*arg));
1531 arg->type = PRINT_FLAGS;
1533 if (read_expected_item(EVENT_DELIM, "(") < 0)
1534 return EVENT_ERROR;
1536 field = malloc_or_die(sizeof(*field));
1538 type = process_arg(event, field, &token);
1539 while (type == EVENT_OP)
1540 type = process_op(event, field, &token);
1541 if (test_type_token(type, token, EVENT_DELIM, ","))
1542 goto out_free;
1544 arg->flags.field = field;
1546 type = read_token_item(&token);
1547 if (event_item_type(type)) {
1548 arg->flags.delim = token;
1549 type = read_token_item(&token);
1552 if (test_type_token(type, token, EVENT_DELIM, ","))
1553 goto out_free;
1555 type = process_fields(event, &arg->flags.flags, &token);
1556 if (test_type_token(type, token, EVENT_DELIM, ")"))
1557 goto out_free;
1559 free_token(token);
1560 type = read_token_item(tok);
1561 return type;
1563 out_free:
1564 free_token(token);
1565 return EVENT_ERROR;
1568 static enum event_type
1569 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1571 struct print_arg *field;
1572 enum event_type type;
1573 char *token;
1575 memset(arg, 0, sizeof(*arg));
1576 arg->type = PRINT_SYMBOL;
1578 if (read_expected_item(EVENT_DELIM, "(") < 0)
1579 return EVENT_ERROR;
1581 field = malloc_or_die(sizeof(*field));
1583 type = process_arg(event, field, &token);
1584 if (test_type_token(type, token, EVENT_DELIM, ","))
1585 goto out_free;
1587 arg->symbol.field = field;
1589 type = process_fields(event, &arg->symbol.symbols, &token);
1590 if (test_type_token(type, token, EVENT_DELIM, ")"))
1591 goto out_free;
1593 free_token(token);
1594 type = read_token_item(tok);
1595 return type;
1597 out_free:
1598 free_token(token);
1599 return EVENT_ERROR;
1602 static enum event_type
1603 process_paren(struct event *event, struct print_arg *arg, char **tok)
1605 struct print_arg *item_arg;
1606 enum event_type type;
1607 char *token;
1609 type = process_arg(event, arg, &token);
1611 if (type == EVENT_ERROR)
1612 return EVENT_ERROR;
1614 if (type == EVENT_OP)
1615 type = process_op(event, arg, &token);
1617 if (type == EVENT_ERROR)
1618 return EVENT_ERROR;
1620 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1621 free_token(token);
1622 return EVENT_ERROR;
1625 free_token(token);
1626 type = read_token_item(&token);
1629 * If the next token is an item or another open paren, then
1630 * this was a typecast.
1632 if (event_item_type(type) ||
1633 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1635 /* make this a typecast and contine */
1637 /* prevous must be an atom */
1638 if (arg->type != PRINT_ATOM)
1639 die("previous needed to be PRINT_ATOM");
1641 item_arg = malloc_or_die(sizeof(*item_arg));
1643 arg->type = PRINT_TYPE;
1644 arg->typecast.type = arg->atom.atom;
1645 arg->typecast.item = item_arg;
1646 type = process_arg_token(event, item_arg, &token, type);
1650 *tok = token;
1651 return type;
1655 static enum event_type
1656 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1658 enum event_type type;
1659 char *token;
1661 if (read_expected(EVENT_DELIM, "(") < 0)
1662 return EVENT_ERROR;
1664 if (read_expect_type(EVENT_ITEM, &token) < 0)
1665 goto fail;
1667 arg->type = PRINT_STRING;
1668 arg->string.string = token;
1669 arg->string.offset = -1;
1671 if (read_expected(EVENT_DELIM, ")") < 0)
1672 return EVENT_ERROR;
1674 type = read_token(&token);
1675 *tok = token;
1677 return type;
1678 fail:
1679 free_token(token);
1680 return EVENT_ERROR;
1683 enum event_type
1684 process_arg_token(struct event *event, struct print_arg *arg,
1685 char **tok, enum event_type type)
1687 char *token;
1688 char *atom;
1690 token = *tok;
1692 switch (type) {
1693 case EVENT_ITEM:
1694 if (strcmp(token, "REC") == 0) {
1695 free_token(token);
1696 type = process_entry(event, arg, &token);
1697 } else if (strcmp(token, "__print_flags") == 0) {
1698 free_token(token);
1699 is_flag_field = 1;
1700 type = process_flags(event, arg, &token);
1701 } else if (strcmp(token, "__print_symbolic") == 0) {
1702 free_token(token);
1703 is_symbolic_field = 1;
1704 type = process_symbols(event, arg, &token);
1705 } else if (strcmp(token, "__get_str") == 0) {
1706 free_token(token);
1707 type = process_str(event, arg, &token);
1708 } else {
1709 atom = token;
1710 /* test the next token */
1711 type = read_token_item(&token);
1713 /* atoms can be more than one token long */
1714 while (type == EVENT_ITEM) {
1715 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1716 strcat(atom, " ");
1717 strcat(atom, token);
1718 free_token(token);
1719 type = read_token_item(&token);
1722 /* todo, test for function */
1724 arg->type = PRINT_ATOM;
1725 arg->atom.atom = atom;
1727 break;
1728 case EVENT_DQUOTE:
1729 case EVENT_SQUOTE:
1730 arg->type = PRINT_ATOM;
1731 arg->atom.atom = token;
1732 type = read_token_item(&token);
1733 break;
1734 case EVENT_DELIM:
1735 if (strcmp(token, "(") == 0) {
1736 free_token(token);
1737 type = process_paren(event, arg, &token);
1738 break;
1740 case EVENT_OP:
1741 /* handle single ops */
1742 arg->type = PRINT_OP;
1743 arg->op.op = token;
1744 arg->op.left = NULL;
1745 type = process_op(event, arg, &token);
1747 break;
1749 case EVENT_ERROR ... EVENT_NEWLINE:
1750 default:
1751 die("unexpected type %d", type);
1753 *tok = token;
1755 return type;
1758 static int event_read_print_args(struct event *event, struct print_arg **list)
1760 enum event_type type = EVENT_ERROR;
1761 struct print_arg *arg;
1762 char *token;
1763 int args = 0;
1765 do {
1766 if (type == EVENT_NEWLINE) {
1767 free_token(token);
1768 type = read_token_item(&token);
1769 continue;
1772 arg = malloc_or_die(sizeof(*arg));
1773 memset(arg, 0, sizeof(*arg));
1775 type = process_arg(event, arg, &token);
1777 if (type == EVENT_ERROR) {
1778 free_arg(arg);
1779 return -1;
1782 *list = arg;
1783 args++;
1785 if (type == EVENT_OP) {
1786 type = process_op(event, arg, &token);
1787 list = &arg->next;
1788 continue;
1791 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1792 free_token(token);
1793 *list = arg;
1794 list = &arg->next;
1795 continue;
1797 break;
1798 } while (type != EVENT_NONE);
1800 if (type != EVENT_NONE)
1801 free_token(token);
1803 return args;
1806 static int event_read_print(struct event *event)
1808 enum event_type type;
1809 char *token;
1810 int ret;
1812 if (read_expected_item(EVENT_ITEM, "print") < 0)
1813 return -1;
1815 if (read_expected(EVENT_ITEM, "fmt") < 0)
1816 return -1;
1818 if (read_expected(EVENT_OP, ":") < 0)
1819 return -1;
1821 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1822 goto fail;
1824 concat:
1825 event->print_fmt.format = token;
1826 event->print_fmt.args = NULL;
1828 /* ok to have no arg */
1829 type = read_token_item(&token);
1831 if (type == EVENT_NONE)
1832 return 0;
1834 /* Handle concatination of print lines */
1835 if (type == EVENT_DQUOTE) {
1836 char *cat;
1838 cat = malloc_or_die(strlen(event->print_fmt.format) +
1839 strlen(token) + 1);
1840 strcpy(cat, event->print_fmt.format);
1841 strcat(cat, token);
1842 free_token(token);
1843 free_token(event->print_fmt.format);
1844 event->print_fmt.format = NULL;
1845 token = cat;
1846 goto concat;
1849 if (test_type_token(type, token, EVENT_DELIM, ","))
1850 goto fail;
1852 free_token(token);
1854 ret = event_read_print_args(event, &event->print_fmt.args);
1855 if (ret < 0)
1856 return -1;
1858 return ret;
1860 fail:
1861 free_token(token);
1862 return -1;
1865 static struct format_field *
1866 find_common_field(struct event *event, const char *name)
1868 struct format_field *format;
1870 for (format = event->format.common_fields;
1871 format; format = format->next) {
1872 if (strcmp(format->name, name) == 0)
1873 break;
1876 return format;
1879 static struct format_field *
1880 find_field(struct event *event, const char *name)
1882 struct format_field *format;
1884 for (format = event->format.fields;
1885 format; format = format->next) {
1886 if (strcmp(format->name, name) == 0)
1887 break;
1890 return format;
1893 static struct format_field *
1894 find_any_field(struct event *event, const char *name)
1896 struct format_field *format;
1898 format = find_common_field(event, name);
1899 if (format)
1900 return format;
1901 return find_field(event, name);
1904 unsigned long long read_size(void *ptr, int size)
1906 switch (size) {
1907 case 1:
1908 return *(unsigned char *)ptr;
1909 case 2:
1910 return data2host2(ptr);
1911 case 4:
1912 return data2host4(ptr);
1913 case 8:
1914 return data2host8(ptr);
1915 default:
1916 /* BUG! */
1917 return 0;
1921 unsigned long long
1922 raw_field_value(struct event *event, const char *name, void *data)
1924 struct format_field *field;
1926 field = find_any_field(event, name);
1927 if (!field)
1928 return 0ULL;
1930 return read_size(data + field->offset, field->size);
1933 void *raw_field_ptr(struct event *event, const char *name, void *data)
1935 struct format_field *field;
1937 field = find_any_field(event, name);
1938 if (!field)
1939 return NULL;
1941 if (field->flags & FIELD_IS_DYNAMIC) {
1942 int offset;
1944 offset = *(int *)(data + field->offset);
1945 offset &= 0xffff;
1947 return data + offset;
1950 return data + field->offset;
1953 static int get_common_info(const char *type, int *offset, int *size)
1955 struct event *event;
1956 struct format_field *field;
1959 * All events should have the same common elements.
1960 * Pick any event to find where the type is;
1962 if (!event_list)
1963 die("no event_list!");
1965 event = event_list;
1966 field = find_common_field(event, type);
1967 if (!field)
1968 die("field '%s' not found", type);
1970 *offset = field->offset;
1971 *size = field->size;
1973 return 0;
1976 static int __parse_common(void *data, int *size, int *offset,
1977 const char *name)
1979 int ret;
1981 if (!*size) {
1982 ret = get_common_info(name, offset, size);
1983 if (ret < 0)
1984 return ret;
1986 return read_size(data + *offset, *size);
1989 int trace_parse_common_type(void *data)
1991 static int type_offset;
1992 static int type_size;
1994 return __parse_common(data, &type_size, &type_offset,
1995 "common_type");
1998 int trace_parse_common_pid(void *data)
2000 static int pid_offset;
2001 static int pid_size;
2003 return __parse_common(data, &pid_size, &pid_offset,
2004 "common_pid");
2007 int parse_common_pc(void *data)
2009 static int pc_offset;
2010 static int pc_size;
2012 return __parse_common(data, &pc_size, &pc_offset,
2013 "common_preempt_count");
2016 int parse_common_flags(void *data)
2018 static int flags_offset;
2019 static int flags_size;
2021 return __parse_common(data, &flags_size, &flags_offset,
2022 "common_flags");
2025 int parse_common_lock_depth(void *data)
2027 static int ld_offset;
2028 static int ld_size;
2029 int ret;
2031 ret = __parse_common(data, &ld_size, &ld_offset,
2032 "common_lock_depth");
2033 if (ret < 0)
2034 return -1;
2036 return ret;
2039 struct event *trace_find_event(int id)
2041 struct event *event;
2043 for (event = event_list; event; event = event->next) {
2044 if (event->id == id)
2045 break;
2047 return event;
2050 struct event *trace_find_next_event(struct event *event)
2052 if (!event)
2053 return event_list;
2055 return event->next;
2058 static unsigned long long eval_num_arg(void *data, int size,
2059 struct event *event, struct print_arg *arg)
2061 unsigned long long val = 0;
2062 unsigned long long left, right;
2063 struct print_arg *larg;
2065 switch (arg->type) {
2066 case PRINT_NULL:
2067 /* ?? */
2068 return 0;
2069 case PRINT_ATOM:
2070 return strtoull(arg->atom.atom, NULL, 0);
2071 case PRINT_FIELD:
2072 if (!arg->field.field) {
2073 arg->field.field = find_any_field(event, arg->field.name);
2074 if (!arg->field.field)
2075 die("field %s not found", arg->field.name);
2077 /* must be a number */
2078 val = read_size(data + arg->field.field->offset,
2079 arg->field.field->size);
2080 break;
2081 case PRINT_FLAGS:
2082 case PRINT_SYMBOL:
2083 break;
2084 case PRINT_TYPE:
2085 return eval_num_arg(data, size, event, arg->typecast.item);
2086 case PRINT_STRING:
2087 return 0;
2088 break;
2089 case PRINT_OP:
2090 if (strcmp(arg->op.op, "[") == 0) {
2092 * Arrays are special, since we don't want
2093 * to read the arg as is.
2095 if (arg->op.left->type != PRINT_FIELD)
2096 goto default_op; /* oops, all bets off */
2097 larg = arg->op.left;
2098 if (!larg->field.field) {
2099 larg->field.field =
2100 find_any_field(event, larg->field.name);
2101 if (!larg->field.field)
2102 die("field %s not found", larg->field.name);
2104 right = eval_num_arg(data, size, event, arg->op.right);
2105 val = read_size(data + larg->field.field->offset +
2106 right * long_size, long_size);
2107 break;
2109 default_op:
2110 left = eval_num_arg(data, size, event, arg->op.left);
2111 right = eval_num_arg(data, size, event, arg->op.right);
2112 switch (arg->op.op[0]) {
2113 case '|':
2114 if (arg->op.op[1])
2115 val = left || right;
2116 else
2117 val = left | right;
2118 break;
2119 case '&':
2120 if (arg->op.op[1])
2121 val = left && right;
2122 else
2123 val = left & right;
2124 break;
2125 case '<':
2126 switch (arg->op.op[1]) {
2127 case 0:
2128 val = left < right;
2129 break;
2130 case '<':
2131 val = left << right;
2132 break;
2133 case '=':
2134 val = left <= right;
2135 break;
2136 default:
2137 die("unknown op '%s'", arg->op.op);
2139 break;
2140 case '>':
2141 switch (arg->op.op[1]) {
2142 case 0:
2143 val = left > right;
2144 break;
2145 case '>':
2146 val = left >> right;
2147 break;
2148 case '=':
2149 val = left >= right;
2150 break;
2151 default:
2152 die("unknown op '%s'", arg->op.op);
2154 break;
2155 case '=':
2156 if (arg->op.op[1] != '=')
2157 die("unknown op '%s'", arg->op.op);
2158 val = left == right;
2159 break;
2160 case '-':
2161 val = left - right;
2162 break;
2163 case '+':
2164 val = left + right;
2165 break;
2166 default:
2167 die("unknown op '%s'", arg->op.op);
2169 break;
2170 default: /* not sure what to do there */
2171 return 0;
2173 return val;
2176 struct flag {
2177 const char *name;
2178 unsigned long long value;
2181 static const struct flag flags[] = {
2182 { "HI_SOFTIRQ", 0 },
2183 { "TIMER_SOFTIRQ", 1 },
2184 { "NET_TX_SOFTIRQ", 2 },
2185 { "NET_RX_SOFTIRQ", 3 },
2186 { "BLOCK_SOFTIRQ", 4 },
2187 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2188 { "TASKLET_SOFTIRQ", 6 },
2189 { "SCHED_SOFTIRQ", 7 },
2190 { "HRTIMER_SOFTIRQ", 8 },
2191 { "RCU_SOFTIRQ", 9 },
2193 { "HRTIMER_NORESTART", 0 },
2194 { "HRTIMER_RESTART", 1 },
2197 unsigned long long eval_flag(const char *flag)
2199 int i;
2202 * Some flags in the format files do not get converted.
2203 * If the flag is not numeric, see if it is something that
2204 * we already know about.
2206 if (isdigit(flag[0]))
2207 return strtoull(flag, NULL, 0);
2209 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2210 if (strcmp(flags[i].name, flag) == 0)
2211 return flags[i].value;
2213 return 0;
2216 static void print_str_arg(void *data, int size,
2217 struct event *event, struct print_arg *arg)
2219 struct print_flag_sym *flag;
2220 unsigned long long val, fval;
2221 char *str;
2222 int print;
2224 switch (arg->type) {
2225 case PRINT_NULL:
2226 /* ?? */
2227 return;
2228 case PRINT_ATOM:
2229 printf("%s", arg->atom.atom);
2230 return;
2231 case PRINT_FIELD:
2232 if (!arg->field.field) {
2233 arg->field.field = find_any_field(event, arg->field.name);
2234 if (!arg->field.field)
2235 die("field %s not found", arg->field.name);
2237 str = malloc_or_die(arg->field.field->size + 1);
2238 memcpy(str, data + arg->field.field->offset,
2239 arg->field.field->size);
2240 str[arg->field.field->size] = 0;
2241 printf("%s", str);
2242 free(str);
2243 break;
2244 case PRINT_FLAGS:
2245 val = eval_num_arg(data, size, event, arg->flags.field);
2246 print = 0;
2247 for (flag = arg->flags.flags; flag; flag = flag->next) {
2248 fval = eval_flag(flag->value);
2249 if (!val && !fval) {
2250 printf("%s", flag->str);
2251 break;
2253 if (fval && (val & fval) == fval) {
2254 if (print && arg->flags.delim)
2255 printf("%s", arg->flags.delim);
2256 printf("%s", flag->str);
2257 print = 1;
2258 val &= ~fval;
2261 break;
2262 case PRINT_SYMBOL:
2263 val = eval_num_arg(data, size, event, arg->symbol.field);
2264 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2265 fval = eval_flag(flag->value);
2266 if (val == fval) {
2267 printf("%s", flag->str);
2268 break;
2271 break;
2273 case PRINT_TYPE:
2274 break;
2275 case PRINT_STRING: {
2276 int str_offset;
2278 if (arg->string.offset == -1) {
2279 struct format_field *f;
2281 f = find_any_field(event, arg->string.string);
2282 arg->string.offset = f->offset;
2284 str_offset = *(int *)(data + arg->string.offset);
2285 str_offset &= 0xffff;
2286 printf("%s", ((char *)data) + str_offset);
2287 break;
2289 case PRINT_OP:
2291 * The only op for string should be ? :
2293 if (arg->op.op[0] != '?')
2294 return;
2295 val = eval_num_arg(data, size, event, arg->op.left);
2296 if (val)
2297 print_str_arg(data, size, event, arg->op.right->op.left);
2298 else
2299 print_str_arg(data, size, event, arg->op.right->op.right);
2300 break;
2301 default:
2302 /* well... */
2303 break;
2307 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2309 static struct format_field *field, *ip_field;
2310 struct print_arg *args, *arg, **next;
2311 unsigned long long ip, val;
2312 char *ptr;
2313 void *bptr;
2315 if (!field) {
2316 field = find_field(event, "buf");
2317 if (!field)
2318 die("can't find buffer field for binary printk");
2319 ip_field = find_field(event, "ip");
2320 if (!ip_field)
2321 die("can't find ip field for binary printk");
2324 ip = read_size(data + ip_field->offset, ip_field->size);
2327 * The first arg is the IP pointer.
2329 args = malloc_or_die(sizeof(*args));
2330 arg = args;
2331 arg->next = NULL;
2332 next = &arg->next;
2334 arg->type = PRINT_ATOM;
2335 arg->atom.atom = malloc_or_die(32);
2336 sprintf(arg->atom.atom, "%lld", ip);
2338 /* skip the first "%pf : " */
2339 for (ptr = fmt + 6, bptr = data + field->offset;
2340 bptr < data + size && *ptr; ptr++) {
2341 int ls = 0;
2343 if (*ptr == '%') {
2344 process_again:
2345 ptr++;
2346 switch (*ptr) {
2347 case '%':
2348 break;
2349 case 'l':
2350 ls++;
2351 goto process_again;
2352 case 'L':
2353 ls = 2;
2354 goto process_again;
2355 case '0' ... '9':
2356 goto process_again;
2357 case 'p':
2358 ls = 1;
2359 /* fall through */
2360 case 'd':
2361 case 'u':
2362 case 'x':
2363 case 'i':
2364 /* the pointers are always 4 bytes aligned */
2365 bptr = (void *)(((unsigned long)bptr + 3) &
2366 ~3);
2367 switch (ls) {
2368 case 0:
2369 case 1:
2370 ls = long_size;
2371 break;
2372 case 2:
2373 ls = 8;
2374 default:
2375 break;
2377 val = read_size(bptr, ls);
2378 bptr += ls;
2379 arg = malloc_or_die(sizeof(*arg));
2380 arg->next = NULL;
2381 arg->type = PRINT_ATOM;
2382 arg->atom.atom = malloc_or_die(32);
2383 sprintf(arg->atom.atom, "%lld", val);
2384 *next = arg;
2385 next = &arg->next;
2386 break;
2387 case 's':
2388 arg = malloc_or_die(sizeof(*arg));
2389 arg->next = NULL;
2390 arg->type = PRINT_STRING;
2391 arg->string.string = strdup(bptr);
2392 bptr += strlen(bptr) + 1;
2393 *next = arg;
2394 next = &arg->next;
2395 default:
2396 break;
2401 return args;
2404 static void free_args(struct print_arg *args)
2406 struct print_arg *next;
2408 while (args) {
2409 next = args->next;
2411 if (args->type == PRINT_ATOM)
2412 free(args->atom.atom);
2413 else
2414 free(args->string.string);
2415 free(args);
2416 args = next;
2420 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2422 unsigned long long addr;
2423 static struct format_field *field;
2424 struct printk_map *printk;
2425 char *format;
2426 char *p;
2428 if (!field) {
2429 field = find_field(event, "fmt");
2430 if (!field)
2431 die("can't find format field for binary printk");
2432 printf("field->offset = %d size=%d\n", field->offset, field->size);
2435 addr = read_size(data + field->offset, field->size);
2437 printk = find_printk(addr);
2438 if (!printk) {
2439 format = malloc_or_die(45);
2440 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2441 addr);
2442 return format;
2445 p = printk->printk;
2446 /* Remove any quotes. */
2447 if (*p == '"')
2448 p++;
2449 format = malloc_or_die(strlen(p) + 10);
2450 sprintf(format, "%s : %s", "%pf", p);
2451 /* remove ending quotes and new line since we will add one too */
2452 p = format + strlen(format) - 1;
2453 if (*p == '"')
2454 *p = 0;
2456 p -= 2;
2457 if (strcmp(p, "\\n") == 0)
2458 *p = 0;
2460 return format;
2463 static void pretty_print(void *data, int size, struct event *event)
2465 struct print_fmt *print_fmt = &event->print_fmt;
2466 struct print_arg *arg = print_fmt->args;
2467 struct print_arg *args = NULL;
2468 const char *ptr = print_fmt->format;
2469 unsigned long long val;
2470 struct func_map *func;
2471 const char *saveptr;
2472 char *bprint_fmt = NULL;
2473 char format[32];
2474 int show_func;
2475 int len;
2476 int ls;
2478 if (event->flags & EVENT_FL_ISFUNC)
2479 ptr = " %pF <-- %pF";
2481 if (event->flags & EVENT_FL_ISBPRINT) {
2482 bprint_fmt = get_bprint_format(data, size, event);
2483 args = make_bprint_args(bprint_fmt, data, size, event);
2484 arg = args;
2485 ptr = bprint_fmt;
2488 for (; *ptr; ptr++) {
2489 ls = 0;
2490 if (*ptr == '\\') {
2491 ptr++;
2492 switch (*ptr) {
2493 case 'n':
2494 printf("\n");
2495 break;
2496 case 't':
2497 printf("\t");
2498 break;
2499 case 'r':
2500 printf("\r");
2501 break;
2502 case '\\':
2503 printf("\\");
2504 break;
2505 default:
2506 printf("%c", *ptr);
2507 break;
2510 } else if (*ptr == '%') {
2511 saveptr = ptr;
2512 show_func = 0;
2513 cont_process:
2514 ptr++;
2515 switch (*ptr) {
2516 case '%':
2517 printf("%%");
2518 break;
2519 case 'l':
2520 ls++;
2521 goto cont_process;
2522 case 'L':
2523 ls = 2;
2524 goto cont_process;
2525 case 'z':
2526 case 'Z':
2527 case '0' ... '9':
2528 goto cont_process;
2529 case 'p':
2530 if (long_size == 4)
2531 ls = 1;
2532 else
2533 ls = 2;
2535 if (*(ptr+1) == 'F' ||
2536 *(ptr+1) == 'f') {
2537 ptr++;
2538 show_func = *ptr;
2541 /* fall through */
2542 case 'd':
2543 case 'i':
2544 case 'x':
2545 case 'X':
2546 case 'u':
2547 if (!arg)
2548 die("no argument match");
2550 len = ((unsigned long)ptr + 1) -
2551 (unsigned long)saveptr;
2553 /* should never happen */
2554 if (len > 32)
2555 die("bad format!");
2557 memcpy(format, saveptr, len);
2558 format[len] = 0;
2560 val = eval_num_arg(data, size, event, arg);
2561 arg = arg->next;
2563 if (show_func) {
2564 func = find_func(val);
2565 if (func) {
2566 printf("%s", func->func);
2567 if (show_func == 'F')
2568 printf("+0x%llx",
2569 val - func->addr);
2570 break;
2573 switch (ls) {
2574 case 0:
2575 printf(format, (int)val);
2576 break;
2577 case 1:
2578 printf(format, (long)val);
2579 break;
2580 case 2:
2581 printf(format, (long long)val);
2582 break;
2583 default:
2584 die("bad count (%d)", ls);
2586 break;
2587 case 's':
2588 if (!arg)
2589 die("no matching argument");
2591 print_str_arg(data, size, event, arg);
2592 arg = arg->next;
2593 break;
2594 default:
2595 printf(">%c<", *ptr);
2598 } else
2599 printf("%c", *ptr);
2602 if (args) {
2603 free_args(args);
2604 free(bprint_fmt);
2608 static inline int log10_cpu(int nb)
2610 if (nb / 100)
2611 return 3;
2612 if (nb / 10)
2613 return 2;
2614 return 1;
2617 static void print_lat_fmt(void *data, int size __unused)
2619 unsigned int lat_flags;
2620 unsigned int pc;
2621 int lock_depth;
2622 int hardirq;
2623 int softirq;
2625 lat_flags = parse_common_flags(data);
2626 pc = parse_common_pc(data);
2627 lock_depth = parse_common_lock_depth(data);
2629 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2630 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2632 printf("%c%c%c",
2633 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2634 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2635 'X' : '.',
2636 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2637 'N' : '.',
2638 (hardirq && softirq) ? 'H' :
2639 hardirq ? 'h' : softirq ? 's' : '.');
2641 if (pc)
2642 printf("%x", pc);
2643 else
2644 printf(".");
2646 if (lock_depth < 0)
2647 printf(". ");
2648 else
2649 printf("%d ", lock_depth);
2652 #define TRACE_GRAPH_INDENT 2
2654 static struct record *
2655 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2656 struct record *next)
2658 struct format_field *field;
2659 struct event *event;
2660 unsigned long val;
2661 int type;
2662 int pid;
2664 type = trace_parse_common_type(next->data);
2665 event = trace_find_event(type);
2666 if (!event)
2667 return NULL;
2669 if (!(event->flags & EVENT_FL_ISFUNCRET))
2670 return NULL;
2672 pid = trace_parse_common_pid(next->data);
2673 field = find_field(event, "func");
2674 if (!field)
2675 die("function return does not have field func");
2677 val = read_size(next->data + field->offset, field->size);
2679 if (cur_pid != pid || cur_func != val)
2680 return NULL;
2682 /* this is a leaf, now advance the iterator */
2683 return trace_read_data(cpu);
2686 /* Signal a overhead of time execution to the output */
2687 static void print_graph_overhead(unsigned long long duration)
2689 /* Non nested entry or return */
2690 if (duration == ~0ULL)
2691 return (void)printf(" ");
2693 /* Duration exceeded 100 msecs */
2694 if (duration > 100000ULL)
2695 return (void)printf("! ");
2697 /* Duration exceeded 10 msecs */
2698 if (duration > 10000ULL)
2699 return (void)printf("+ ");
2701 printf(" ");
2704 static void print_graph_duration(unsigned long long duration)
2706 unsigned long usecs = duration / 1000;
2707 unsigned long nsecs_rem = duration % 1000;
2708 /* log10(ULONG_MAX) + '\0' */
2709 char msecs_str[21];
2710 char nsecs_str[5];
2711 int len;
2712 int i;
2714 sprintf(msecs_str, "%lu", usecs);
2716 /* Print msecs */
2717 len = printf("%lu", usecs);
2719 /* Print nsecs (we don't want to exceed 7 numbers) */
2720 if (len < 7) {
2721 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2722 len += printf(".%s", nsecs_str);
2725 printf(" us ");
2727 /* Print remaining spaces to fit the row's width */
2728 for (i = len; i < 7; i++)
2729 printf(" ");
2731 printf("| ");
2734 static void
2735 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2737 unsigned long long rettime, calltime;
2738 unsigned long long duration, depth;
2739 unsigned long long val;
2740 struct format_field *field;
2741 struct func_map *func;
2742 struct event *ret_event;
2743 int type;
2744 int i;
2746 type = trace_parse_common_type(ret_rec->data);
2747 ret_event = trace_find_event(type);
2749 field = find_field(ret_event, "rettime");
2750 if (!field)
2751 die("can't find rettime in return graph");
2752 rettime = read_size(ret_rec->data + field->offset, field->size);
2754 field = find_field(ret_event, "calltime");
2755 if (!field)
2756 die("can't find rettime in return graph");
2757 calltime = read_size(ret_rec->data + field->offset, field->size);
2759 duration = rettime - calltime;
2761 /* Overhead */
2762 print_graph_overhead(duration);
2764 /* Duration */
2765 print_graph_duration(duration);
2767 field = find_field(event, "depth");
2768 if (!field)
2769 die("can't find depth in entry graph");
2770 depth = read_size(data + field->offset, field->size);
2772 /* Function */
2773 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2774 printf(" ");
2776 field = find_field(event, "func");
2777 if (!field)
2778 die("can't find func in entry graph");
2779 val = read_size(data + field->offset, field->size);
2780 func = find_func(val);
2782 if (func)
2783 printf("%s();", func->func);
2784 else
2785 printf("%llx();", val);
2788 static void print_graph_nested(struct event *event, void *data)
2790 struct format_field *field;
2791 unsigned long long depth;
2792 unsigned long long val;
2793 struct func_map *func;
2794 int i;
2796 /* No overhead */
2797 print_graph_overhead(-1);
2799 /* No time */
2800 printf(" | ");
2802 field = find_field(event, "depth");
2803 if (!field)
2804 die("can't find depth in entry graph");
2805 depth = read_size(data + field->offset, field->size);
2807 /* Function */
2808 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2809 printf(" ");
2811 field = find_field(event, "func");
2812 if (!field)
2813 die("can't find func in entry graph");
2814 val = read_size(data + field->offset, field->size);
2815 func = find_func(val);
2817 if (func)
2818 printf("%s() {", func->func);
2819 else
2820 printf("%llx() {", val);
2823 static void
2824 pretty_print_func_ent(void *data, int size, struct event *event,
2825 int cpu, int pid)
2827 struct format_field *field;
2828 struct record *rec;
2829 void *copy_data;
2830 unsigned long val;
2832 if (latency_format) {
2833 print_lat_fmt(data, size);
2834 printf(" | ");
2837 field = find_field(event, "func");
2838 if (!field)
2839 die("function entry does not have func field");
2841 val = read_size(data + field->offset, field->size);
2844 * peek_data may unmap the data pointer. Copy it first.
2846 copy_data = malloc_or_die(size);
2847 memcpy(copy_data, data, size);
2848 data = copy_data;
2850 rec = trace_peek_data(cpu);
2851 if (rec) {
2852 rec = get_return_for_leaf(cpu, pid, val, rec);
2853 if (rec) {
2854 print_graph_entry_leaf(event, data, rec);
2855 goto out_free;
2858 print_graph_nested(event, data);
2859 out_free:
2860 free(data);
2863 static void
2864 pretty_print_func_ret(void *data, int size __unused, struct event *event)
2866 unsigned long long rettime, calltime;
2867 unsigned long long duration, depth;
2868 struct format_field *field;
2869 int i;
2871 if (latency_format) {
2872 print_lat_fmt(data, size);
2873 printf(" | ");
2876 field = find_field(event, "rettime");
2877 if (!field)
2878 die("can't find rettime in return graph");
2879 rettime = read_size(data + field->offset, field->size);
2881 field = find_field(event, "calltime");
2882 if (!field)
2883 die("can't find calltime in return graph");
2884 calltime = read_size(data + field->offset, field->size);
2886 duration = rettime - calltime;
2888 /* Overhead */
2889 print_graph_overhead(duration);
2891 /* Duration */
2892 print_graph_duration(duration);
2894 field = find_field(event, "depth");
2895 if (!field)
2896 die("can't find depth in entry graph");
2897 depth = read_size(data + field->offset, field->size);
2899 /* Function */
2900 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2901 printf(" ");
2903 printf("}");
2906 static void
2907 pretty_print_func_graph(void *data, int size, struct event *event,
2908 int cpu, int pid)
2910 if (event->flags & EVENT_FL_ISFUNCENT)
2911 pretty_print_func_ent(data, size, event, cpu, pid);
2912 else if (event->flags & EVENT_FL_ISFUNCRET)
2913 pretty_print_func_ret(data, size, event);
2914 printf("\n");
2917 void print_trace_event(int cpu, void *data, int size)
2919 struct event *event;
2920 int type;
2921 int pid;
2923 type = trace_parse_common_type(data);
2925 event = trace_find_event(type);
2926 if (!event) {
2927 warning("ug! no event found for type %d", type);
2928 return;
2931 pid = trace_parse_common_pid(data);
2933 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2934 return pretty_print_func_graph(data, size, event, cpu, pid);
2936 if (latency_format)
2937 print_lat_fmt(data, size);
2939 if (event->flags & EVENT_FL_FAILED) {
2940 printf("EVENT '%s' FAILED TO PARSE\n",
2941 event->name);
2942 return;
2945 pretty_print(data, size, event);
2948 static void print_fields(struct print_flag_sym *field)
2950 printf("{ %s, %s }", field->value, field->str);
2951 if (field->next) {
2952 printf(", ");
2953 print_fields(field->next);
2957 static void print_args(struct print_arg *args)
2959 int print_paren = 1;
2961 switch (args->type) {
2962 case PRINT_NULL:
2963 printf("null");
2964 break;
2965 case PRINT_ATOM:
2966 printf("%s", args->atom.atom);
2967 break;
2968 case PRINT_FIELD:
2969 printf("REC->%s", args->field.name);
2970 break;
2971 case PRINT_FLAGS:
2972 printf("__print_flags(");
2973 print_args(args->flags.field);
2974 printf(", %s, ", args->flags.delim);
2975 print_fields(args->flags.flags);
2976 printf(")");
2977 break;
2978 case PRINT_SYMBOL:
2979 printf("__print_symbolic(");
2980 print_args(args->symbol.field);
2981 printf(", ");
2982 print_fields(args->symbol.symbols);
2983 printf(")");
2984 break;
2985 case PRINT_STRING:
2986 printf("__get_str(%s)", args->string.string);
2987 break;
2988 case PRINT_TYPE:
2989 printf("(%s)", args->typecast.type);
2990 print_args(args->typecast.item);
2991 break;
2992 case PRINT_OP:
2993 if (strcmp(args->op.op, ":") == 0)
2994 print_paren = 0;
2995 if (print_paren)
2996 printf("(");
2997 print_args(args->op.left);
2998 printf(" %s ", args->op.op);
2999 print_args(args->op.right);
3000 if (print_paren)
3001 printf(")");
3002 break;
3003 default:
3004 /* we should warn... */
3005 return;
3007 if (args->next) {
3008 printf("\n");
3009 print_args(args->next);
3013 int parse_ftrace_file(char *buf, unsigned long size)
3015 struct format_field *field;
3016 struct print_arg *arg, **list;
3017 struct event *event;
3018 int ret;
3020 init_input_buf(buf, size);
3022 event = alloc_event();
3023 if (!event)
3024 return -ENOMEM;
3026 event->flags |= EVENT_FL_ISFTRACE;
3028 event->name = event_read_name();
3029 if (!event->name)
3030 die("failed to read ftrace event name");
3032 if (strcmp(event->name, "function") == 0)
3033 event->flags |= EVENT_FL_ISFUNC;
3035 else if (strcmp(event->name, "funcgraph_entry") == 0)
3036 event->flags |= EVENT_FL_ISFUNCENT;
3038 else if (strcmp(event->name, "funcgraph_exit") == 0)
3039 event->flags |= EVENT_FL_ISFUNCRET;
3041 else if (strcmp(event->name, "bprint") == 0)
3042 event->flags |= EVENT_FL_ISBPRINT;
3044 event->id = event_read_id();
3045 if (event->id < 0)
3046 die("failed to read ftrace event id");
3048 add_event(event);
3050 ret = event_read_format(event);
3051 if (ret < 0)
3052 die("failed to read ftrace event format");
3054 ret = event_read_print(event);
3055 if (ret < 0)
3056 die("failed to read ftrace event print fmt");
3058 /* New ftrace handles args */
3059 if (ret > 0)
3060 return 0;
3062 * The arguments for ftrace files are parsed by the fields.
3063 * Set up the fields as their arguments.
3065 list = &event->print_fmt.args;
3066 for (field = event->format.fields; field; field = field->next) {
3067 arg = malloc_or_die(sizeof(*arg));
3068 memset(arg, 0, sizeof(*arg));
3069 *list = arg;
3070 list = &arg->next;
3071 arg->type = PRINT_FIELD;
3072 arg->field.name = field->name;
3073 arg->field.field = field;
3075 return 0;
3078 int parse_event_file(char *buf, unsigned long size, char *sys)
3080 struct event *event;
3081 int ret;
3083 init_input_buf(buf, size);
3085 event = alloc_event();
3086 if (!event)
3087 return -ENOMEM;
3089 event->name = event_read_name();
3090 if (!event->name)
3091 die("failed to read event name");
3093 event->id = event_read_id();
3094 if (event->id < 0)
3095 die("failed to read event id");
3097 ret = event_read_format(event);
3098 if (ret < 0) {
3099 warning("failed to read event format for %s", event->name);
3100 goto event_failed;
3103 ret = event_read_print(event);
3104 if (ret < 0) {
3105 warning("failed to read event print fmt for %s", event->name);
3106 goto event_failed;
3109 event->system = strdup(sys);
3111 #define PRINT_ARGS 0
3112 if (PRINT_ARGS && event->print_fmt.args)
3113 print_args(event->print_fmt.args);
3115 add_event(event);
3116 return 0;
3118 event_failed:
3119 event->flags |= EVENT_FL_FAILED;
3120 /* still add it even if it failed */
3121 add_event(event);
3122 return -1;
3125 void parse_set_info(int nr_cpus, int long_sz)
3127 cpus = nr_cpus;
3128 long_size = long_sz;
3131 int common_pc(struct scripting_context *context)
3133 return parse_common_pc(context->event_data);
3136 int common_flags(struct scripting_context *context)
3138 return parse_common_flags(context->event_data);
3141 int common_lock_depth(struct scripting_context *context)
3143 return parse_common_lock_depth(context->event_data);