perf: Fix parsing of __print_flags() in TP_printk()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / trace-event-parse.c
blobbf54c48871dc1b65afa02102c506ee3aa5a1353b
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.
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
31 #undef _GNU_SOURCE
32 #include "../perf.h"
33 #include "util.h"
34 #include "trace-event.h"
36 int header_page_ts_offset;
37 int header_page_ts_size;
38 int header_page_size_offset;
39 int header_page_size_size;
40 int header_page_overwrite_offset;
41 int header_page_overwrite_size;
42 int header_page_data_offset;
43 int header_page_data_size;
45 bool latency_format;
47 static char *input_buf;
48 static unsigned long long input_buf_ptr;
49 static unsigned long long input_buf_siz;
51 static int cpus;
52 static int long_size;
53 static int is_flag_field;
54 static int is_symbolic_field;
56 static struct format_field *
57 find_any_field(struct event *event, const char *name);
59 static void init_input_buf(char *buf, unsigned long long size)
61 input_buf = buf;
62 input_buf_siz = size;
63 input_buf_ptr = 0;
66 struct cmdline {
67 char *comm;
68 int pid;
71 static struct cmdline *cmdlines;
72 static int cmdline_count;
74 static int cmdline_cmp(const void *a, const void *b)
76 const struct cmdline *ca = a;
77 const struct cmdline *cb = b;
79 if (ca->pid < cb->pid)
80 return -1;
81 if (ca->pid > cb->pid)
82 return 1;
84 return 0;
87 void parse_cmdlines(char *file, int size __unused)
89 struct cmdline_list {
90 struct cmdline_list *next;
91 char *comm;
92 int pid;
93 } *list = NULL, *item;
94 char *line;
95 char *next = NULL;
96 int i;
98 line = strtok_r(file, "\n", &next);
99 while (line) {
100 item = malloc_or_die(sizeof(*item));
101 sscanf(line, "%d %as", &item->pid,
102 (float *)(void *)&item->comm); /* workaround gcc warning */
103 item->next = list;
104 list = item;
105 line = strtok_r(NULL, "\n", &next);
106 cmdline_count++;
109 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
111 i = 0;
112 while (list) {
113 cmdlines[i].pid = list->pid;
114 cmdlines[i].comm = list->comm;
115 i++;
116 item = list;
117 list = list->next;
118 free(item);
121 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
124 static struct func_map {
125 unsigned long long addr;
126 char *func;
127 char *mod;
128 } *func_list;
129 static unsigned int func_count;
131 static int func_cmp(const void *a, const void *b)
133 const struct func_map *fa = a;
134 const struct func_map *fb = b;
136 if (fa->addr < fb->addr)
137 return -1;
138 if (fa->addr > fb->addr)
139 return 1;
141 return 0;
144 void parse_proc_kallsyms(char *file, unsigned int size __unused)
146 struct func_list {
147 struct func_list *next;
148 unsigned long long addr;
149 char *func;
150 char *mod;
151 } *list = NULL, *item;
152 char *line;
153 char *next = NULL;
154 char *addr_str;
155 char ch;
156 int ret __used;
157 int i;
159 line = strtok_r(file, "\n", &next);
160 while (line) {
161 item = malloc_or_die(sizeof(*item));
162 item->mod = NULL;
163 ret = sscanf(line, "%as %c %as\t[%as",
164 (float *)(void *)&addr_str, /* workaround gcc warning */
165 &ch,
166 (float *)(void *)&item->func,
167 (float *)(void *)&item->mod);
168 item->addr = strtoull(addr_str, NULL, 16);
169 free(addr_str);
171 /* truncate the extra ']' */
172 if (item->mod)
173 item->mod[strlen(item->mod) - 1] = 0;
176 item->next = list;
177 list = item;
178 line = strtok_r(NULL, "\n", &next);
179 func_count++;
182 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
184 i = 0;
185 while (list) {
186 func_list[i].func = list->func;
187 func_list[i].addr = list->addr;
188 func_list[i].mod = list->mod;
189 i++;
190 item = list;
191 list = list->next;
192 free(item);
195 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
198 * Add a special record at the end.
200 func_list[func_count].func = NULL;
201 func_list[func_count].addr = 0;
202 func_list[func_count].mod = NULL;
206 * We are searching for a record in between, not an exact
207 * match.
209 static int func_bcmp(const void *a, const void *b)
211 const struct func_map *fa = a;
212 const struct func_map *fb = b;
214 if ((fa->addr == fb->addr) ||
216 (fa->addr > fb->addr &&
217 fa->addr < (fb+1)->addr))
218 return 0;
220 if (fa->addr < fb->addr)
221 return -1;
223 return 1;
226 static struct func_map *find_func(unsigned long long addr)
228 struct func_map *func;
229 struct func_map key;
231 key.addr = addr;
233 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
234 func_bcmp);
236 return func;
239 void print_funcs(void)
241 int i;
243 for (i = 0; i < (int)func_count; i++) {
244 printf("%016llx %s",
245 func_list[i].addr,
246 func_list[i].func);
247 if (func_list[i].mod)
248 printf(" [%s]\n", func_list[i].mod);
249 else
250 printf("\n");
254 static struct printk_map {
255 unsigned long long addr;
256 char *printk;
257 } *printk_list;
258 static unsigned int printk_count;
260 static int printk_cmp(const void *a, const void *b)
262 const struct func_map *fa = a;
263 const struct func_map *fb = b;
265 if (fa->addr < fb->addr)
266 return -1;
267 if (fa->addr > fb->addr)
268 return 1;
270 return 0;
273 static struct printk_map *find_printk(unsigned long long addr)
275 struct printk_map *printk;
276 struct printk_map key;
278 key.addr = addr;
280 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
281 printk_cmp);
283 return printk;
286 void parse_ftrace_printk(char *file, unsigned int size __unused)
288 struct printk_list {
289 struct printk_list *next;
290 unsigned long long addr;
291 char *printk;
292 } *list = NULL, *item;
293 char *line;
294 char *next = NULL;
295 char *addr_str;
296 int i;
298 line = strtok_r(file, "\n", &next);
299 while (line) {
300 addr_str = strsep(&line, ":");
301 if (!line) {
302 warning("error parsing print strings");
303 break;
305 item = malloc_or_die(sizeof(*item));
306 item->addr = strtoull(addr_str, NULL, 16);
307 /* fmt still has a space, skip it */
308 item->printk = strdup(line+1);
309 item->next = list;
310 list = item;
311 line = strtok_r(NULL, "\n", &next);
312 printk_count++;
315 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
317 i = 0;
318 while (list) {
319 printk_list[i].printk = list->printk;
320 printk_list[i].addr = list->addr;
321 i++;
322 item = list;
323 list = list->next;
324 free(item);
327 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
330 void print_printk(void)
332 int i;
334 for (i = 0; i < (int)printk_count; i++) {
335 printf("%016llx %s\n",
336 printk_list[i].addr,
337 printk_list[i].printk);
341 static struct event *alloc_event(void)
343 struct event *event;
345 event = malloc_or_die(sizeof(*event));
346 memset(event, 0, sizeof(*event));
348 return event;
351 enum event_type {
352 EVENT_ERROR,
353 EVENT_NONE,
354 EVENT_SPACE,
355 EVENT_NEWLINE,
356 EVENT_OP,
357 EVENT_DELIM,
358 EVENT_ITEM,
359 EVENT_DQUOTE,
360 EVENT_SQUOTE,
363 static struct event *event_list;
365 static void add_event(struct event *event)
367 event->next = event_list;
368 event_list = event;
371 static int event_item_type(enum event_type type)
373 switch (type) {
374 case EVENT_ITEM ... EVENT_SQUOTE:
375 return 1;
376 case EVENT_ERROR ... EVENT_DELIM:
377 default:
378 return 0;
382 static void free_arg(struct print_arg *arg)
384 if (!arg)
385 return;
387 switch (arg->type) {
388 case PRINT_ATOM:
389 if (arg->atom.atom)
390 free(arg->atom.atom);
391 break;
392 case PRINT_NULL:
393 case PRINT_FIELD ... PRINT_OP:
394 default:
395 /* todo */
396 break;
399 free(arg);
402 static enum event_type get_type(int ch)
404 if (ch == '\n')
405 return EVENT_NEWLINE;
406 if (isspace(ch))
407 return EVENT_SPACE;
408 if (isalnum(ch) || ch == '_')
409 return EVENT_ITEM;
410 if (ch == '\'')
411 return EVENT_SQUOTE;
412 if (ch == '"')
413 return EVENT_DQUOTE;
414 if (!isprint(ch))
415 return EVENT_NONE;
416 if (ch == '(' || ch == ')' || ch == ',')
417 return EVENT_DELIM;
419 return EVENT_OP;
422 static int __read_char(void)
424 if (input_buf_ptr >= input_buf_siz)
425 return -1;
427 return input_buf[input_buf_ptr++];
430 static int __peek_char(void)
432 if (input_buf_ptr >= input_buf_siz)
433 return -1;
435 return input_buf[input_buf_ptr];
438 static enum event_type __read_token(char **tok)
440 char buf[BUFSIZ];
441 int ch, last_ch, quote_ch, next_ch;
442 int i = 0;
443 int tok_size = 0;
444 enum event_type type;
446 *tok = NULL;
449 ch = __read_char();
450 if (ch < 0)
451 return EVENT_NONE;
453 type = get_type(ch);
454 if (type == EVENT_NONE)
455 return type;
457 buf[i++] = ch;
459 switch (type) {
460 case EVENT_NEWLINE:
461 case EVENT_DELIM:
462 *tok = malloc_or_die(2);
463 (*tok)[0] = ch;
464 (*tok)[1] = 0;
465 return type;
467 case EVENT_OP:
468 switch (ch) {
469 case '-':
470 next_ch = __peek_char();
471 if (next_ch == '>') {
472 buf[i++] = __read_char();
473 break;
475 /* fall through */
476 case '+':
477 case '|':
478 case '&':
479 case '>':
480 case '<':
481 last_ch = ch;
482 ch = __peek_char();
483 if (ch != last_ch)
484 goto test_equal;
485 buf[i++] = __read_char();
486 switch (last_ch) {
487 case '>':
488 case '<':
489 goto test_equal;
490 default:
491 break;
493 break;
494 case '!':
495 case '=':
496 goto test_equal;
497 default: /* what should we do instead? */
498 break;
500 buf[i] = 0;
501 *tok = strdup(buf);
502 return type;
504 test_equal:
505 ch = __peek_char();
506 if (ch == '=')
507 buf[i++] = __read_char();
508 break;
510 case EVENT_DQUOTE:
511 case EVENT_SQUOTE:
512 /* don't keep quotes */
513 i--;
514 quote_ch = ch;
515 last_ch = 0;
516 do {
517 if (i == (BUFSIZ - 1)) {
518 buf[i] = 0;
519 if (*tok) {
520 *tok = realloc(*tok, tok_size + BUFSIZ);
521 if (!*tok)
522 return EVENT_NONE;
523 strcat(*tok, buf);
524 } else
525 *tok = strdup(buf);
527 if (!*tok)
528 return EVENT_NONE;
529 tok_size += BUFSIZ;
530 i = 0;
532 last_ch = ch;
533 ch = __read_char();
534 buf[i++] = ch;
535 /* the '\' '\' will cancel itself */
536 if (ch == '\\' && last_ch == '\\')
537 last_ch = 0;
538 } while (ch != quote_ch || last_ch == '\\');
539 /* remove the last quote */
540 i--;
541 goto out;
543 case EVENT_ERROR ... EVENT_SPACE:
544 case EVENT_ITEM:
545 default:
546 break;
549 while (get_type(__peek_char()) == type) {
550 if (i == (BUFSIZ - 1)) {
551 buf[i] = 0;
552 if (*tok) {
553 *tok = realloc(*tok, tok_size + BUFSIZ);
554 if (!*tok)
555 return EVENT_NONE;
556 strcat(*tok, buf);
557 } else
558 *tok = strdup(buf);
560 if (!*tok)
561 return EVENT_NONE;
562 tok_size += BUFSIZ;
563 i = 0;
565 ch = __read_char();
566 buf[i++] = ch;
569 out:
570 buf[i] = 0;
571 if (*tok) {
572 *tok = realloc(*tok, tok_size + i);
573 if (!*tok)
574 return EVENT_NONE;
575 strcat(*tok, buf);
576 } else
577 *tok = strdup(buf);
578 if (!*tok)
579 return EVENT_NONE;
581 return type;
584 static void free_token(char *tok)
586 if (tok)
587 free(tok);
590 static enum event_type read_token(char **tok)
592 enum event_type type;
594 for (;;) {
595 type = __read_token(tok);
596 if (type != EVENT_SPACE)
597 return type;
599 free_token(*tok);
602 /* not reached */
603 return EVENT_NONE;
606 /* no newline */
607 static enum event_type read_token_item(char **tok)
609 enum event_type type;
611 for (;;) {
612 type = __read_token(tok);
613 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
614 return type;
616 free_token(*tok);
619 /* not reached */
620 return EVENT_NONE;
623 static int test_type(enum event_type type, enum event_type expect)
625 if (type != expect) {
626 warning("Error: expected type %d but read %d",
627 expect, type);
628 return -1;
630 return 0;
633 static int __test_type_token(enum event_type type, char *token,
634 enum event_type expect, const char *expect_tok,
635 bool warn)
637 if (type != expect) {
638 if (warn)
639 warning("Error: expected type %d but read %d",
640 expect, type);
641 return -1;
644 if (strcmp(token, expect_tok) != 0) {
645 if (warn)
646 warning("Error: expected '%s' but read '%s'",
647 expect_tok, token);
648 return -1;
650 return 0;
653 static int test_type_token(enum event_type type, char *token,
654 enum event_type expect, const char *expect_tok)
656 return __test_type_token(type, token, expect, expect_tok, true);
659 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
661 enum event_type type;
663 if (newline_ok)
664 type = read_token(tok);
665 else
666 type = read_token_item(tok);
667 return test_type(type, expect);
670 static int read_expect_type(enum event_type expect, char **tok)
672 return __read_expect_type(expect, tok, 1);
675 static int __read_expected(enum event_type expect, const char *str,
676 int newline_ok, bool warn)
678 enum event_type type;
679 char *token;
680 int ret;
682 if (newline_ok)
683 type = read_token(&token);
684 else
685 type = read_token_item(&token);
687 ret = __test_type_token(type, token, expect, str, warn);
689 free_token(token);
691 return ret;
694 static int read_expected(enum event_type expect, const char *str)
696 return __read_expected(expect, str, 1, true);
699 static int read_expected_item(enum event_type expect, const char *str)
701 return __read_expected(expect, str, 0, true);
704 static char *event_read_name(void)
706 char *token;
708 if (read_expected(EVENT_ITEM, "name") < 0)
709 return NULL;
711 if (read_expected(EVENT_OP, ":") < 0)
712 return NULL;
714 if (read_expect_type(EVENT_ITEM, &token) < 0)
715 goto fail;
717 return token;
719 fail:
720 free_token(token);
721 return NULL;
724 static int event_read_id(void)
726 char *token;
727 int id;
729 if (read_expected_item(EVENT_ITEM, "ID") < 0)
730 return -1;
732 if (read_expected(EVENT_OP, ":") < 0)
733 return -1;
735 if (read_expect_type(EVENT_ITEM, &token) < 0)
736 goto fail;
738 id = strtoul(token, NULL, 0);
739 free_token(token);
740 return id;
742 fail:
743 free_token(token);
744 return -1;
747 static int field_is_string(struct format_field *field)
749 if ((field->flags & FIELD_IS_ARRAY) &&
750 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
751 !strstr(field->type, "s8")))
752 return 1;
754 return 0;
757 static int field_is_dynamic(struct format_field *field)
759 if (!strncmp(field->type, "__data_loc", 10))
760 return 1;
762 return 0;
765 static int event_read_fields(struct event *event, struct format_field **fields)
767 struct format_field *field = NULL;
768 enum event_type type;
769 char *token;
770 char *last_token;
771 int count = 0;
773 do {
774 type = read_token(&token);
775 if (type == EVENT_NEWLINE) {
776 free_token(token);
777 return count;
780 count++;
782 if (test_type_token(type, token, EVENT_ITEM, "field"))
783 goto fail;
784 free_token(token);
786 type = read_token(&token);
788 * The ftrace fields may still use the "special" name.
789 * Just ignore it.
791 if (event->flags & EVENT_FL_ISFTRACE &&
792 type == EVENT_ITEM && strcmp(token, "special") == 0) {
793 free_token(token);
794 type = read_token(&token);
797 if (test_type_token(type, token, EVENT_OP, ":") < 0)
798 return -1;
800 if (read_expect_type(EVENT_ITEM, &token) < 0)
801 goto fail;
803 last_token = token;
805 field = malloc_or_die(sizeof(*field));
806 memset(field, 0, sizeof(*field));
808 /* read the rest of the type */
809 for (;;) {
810 type = read_token(&token);
811 if (type == EVENT_ITEM ||
812 (type == EVENT_OP && strcmp(token, "*") == 0) ||
814 * Some of the ftrace fields are broken and have
815 * an illegal "." in them.
817 (event->flags & EVENT_FL_ISFTRACE &&
818 type == EVENT_OP && strcmp(token, ".") == 0)) {
820 if (strcmp(token, "*") == 0)
821 field->flags |= FIELD_IS_POINTER;
823 if (field->type) {
824 field->type = realloc(field->type,
825 strlen(field->type) +
826 strlen(last_token) + 2);
827 strcat(field->type, " ");
828 strcat(field->type, last_token);
829 } else
830 field->type = last_token;
831 last_token = token;
832 continue;
835 break;
838 if (!field->type) {
839 die("no type found");
840 goto fail;
842 field->name = last_token;
844 if (test_type(type, EVENT_OP))
845 goto fail;
847 if (strcmp(token, "[") == 0) {
848 enum event_type last_type = type;
849 char *brackets = token;
850 int len;
852 field->flags |= FIELD_IS_ARRAY;
854 type = read_token(&token);
855 while (strcmp(token, "]") != 0) {
856 if (last_type == EVENT_ITEM &&
857 type == EVENT_ITEM)
858 len = 2;
859 else
860 len = 1;
861 last_type = type;
863 brackets = realloc(brackets,
864 strlen(brackets) +
865 strlen(token) + len);
866 if (len == 2)
867 strcat(brackets, " ");
868 strcat(brackets, token);
869 free_token(token);
870 type = read_token(&token);
871 if (type == EVENT_NONE) {
872 die("failed to find token");
873 goto fail;
877 free_token(token);
879 brackets = realloc(brackets, strlen(brackets) + 2);
880 strcat(brackets, "]");
882 /* add brackets to type */
884 type = read_token(&token);
886 * If the next token is not an OP, then it is of
887 * the format: type [] item;
889 if (type == EVENT_ITEM) {
890 field->type = realloc(field->type,
891 strlen(field->type) +
892 strlen(field->name) +
893 strlen(brackets) + 2);
894 strcat(field->type, " ");
895 strcat(field->type, field->name);
896 free_token(field->name);
897 strcat(field->type, brackets);
898 field->name = token;
899 type = read_token(&token);
900 } else {
901 field->type = realloc(field->type,
902 strlen(field->type) +
903 strlen(brackets) + 1);
904 strcat(field->type, brackets);
906 free(brackets);
909 if (field_is_string(field)) {
910 field->flags |= FIELD_IS_STRING;
911 if (field_is_dynamic(field))
912 field->flags |= FIELD_IS_DYNAMIC;
915 if (test_type_token(type, token, EVENT_OP, ";"))
916 goto fail;
917 free_token(token);
919 if (read_expected(EVENT_ITEM, "offset") < 0)
920 goto fail_expect;
922 if (read_expected(EVENT_OP, ":") < 0)
923 goto fail_expect;
925 if (read_expect_type(EVENT_ITEM, &token))
926 goto fail;
927 field->offset = strtoul(token, NULL, 0);
928 free_token(token);
930 if (read_expected(EVENT_OP, ";") < 0)
931 goto fail_expect;
933 if (read_expected(EVENT_ITEM, "size") < 0)
934 goto fail_expect;
936 if (read_expected(EVENT_OP, ":") < 0)
937 goto fail_expect;
939 if (read_expect_type(EVENT_ITEM, &token))
940 goto fail;
941 field->size = strtoul(token, NULL, 0);
942 free_token(token);
944 if (read_expected(EVENT_OP, ";") < 0)
945 goto fail_expect;
947 type = read_token(&token);
948 if (type != EVENT_NEWLINE) {
949 /* newer versions of the kernel have a "signed" type */
950 if (test_type_token(type, token, EVENT_ITEM, "signed"))
951 goto fail;
953 free_token(token);
955 if (read_expected(EVENT_OP, ":") < 0)
956 goto fail_expect;
958 if (read_expect_type(EVENT_ITEM, &token))
959 goto fail;
961 if (strtoul(token, NULL, 0))
962 field->flags |= FIELD_IS_SIGNED;
964 free_token(token);
965 if (read_expected(EVENT_OP, ";") < 0)
966 goto fail_expect;
968 if (read_expect_type(EVENT_NEWLINE, &token))
969 goto fail;
972 free_token(token);
974 *fields = field;
975 fields = &field->next;
977 } while (1);
979 return 0;
981 fail:
982 free_token(token);
983 fail_expect:
984 if (field)
985 free(field);
986 return -1;
989 static int event_read_format(struct event *event)
991 char *token;
992 int ret;
994 if (read_expected_item(EVENT_ITEM, "format") < 0)
995 return -1;
997 if (read_expected(EVENT_OP, ":") < 0)
998 return -1;
1000 if (read_expect_type(EVENT_NEWLINE, &token))
1001 goto fail;
1002 free_token(token);
1004 ret = event_read_fields(event, &event->format.common_fields);
1005 if (ret < 0)
1006 return ret;
1007 event->format.nr_common = ret;
1009 ret = event_read_fields(event, &event->format.fields);
1010 if (ret < 0)
1011 return ret;
1012 event->format.nr_fields = ret;
1014 return 0;
1016 fail:
1017 free_token(token);
1018 return -1;
1021 enum event_type
1022 process_arg_token(struct event *event, struct print_arg *arg,
1023 char **tok, enum event_type type);
1025 static enum event_type
1026 process_arg(struct event *event, struct print_arg *arg, char **tok)
1028 enum event_type type;
1029 char *token;
1031 type = read_token(&token);
1032 *tok = token;
1034 return process_arg_token(event, arg, tok, type);
1037 static enum event_type
1038 process_cond(struct event *event, struct print_arg *top, char **tok)
1040 struct print_arg *arg, *left, *right;
1041 enum event_type type;
1042 char *token = NULL;
1044 arg = malloc_or_die(sizeof(*arg));
1045 memset(arg, 0, sizeof(*arg));
1047 left = malloc_or_die(sizeof(*left));
1049 right = malloc_or_die(sizeof(*right));
1051 arg->type = PRINT_OP;
1052 arg->op.left = left;
1053 arg->op.right = right;
1055 *tok = NULL;
1056 type = process_arg(event, left, &token);
1057 if (test_type_token(type, token, EVENT_OP, ":"))
1058 goto out_free;
1060 arg->op.op = token;
1062 type = process_arg(event, right, &token);
1064 top->op.right = arg;
1066 *tok = token;
1067 return type;
1069 out_free:
1070 free_token(*tok);
1071 free(right);
1072 free(left);
1073 free_arg(arg);
1074 return EVENT_ERROR;
1077 static enum event_type
1078 process_array(struct event *event, struct print_arg *top, char **tok)
1080 struct print_arg *arg;
1081 enum event_type type;
1082 char *token = NULL;
1084 arg = malloc_or_die(sizeof(*arg));
1085 memset(arg, 0, sizeof(*arg));
1087 *tok = NULL;
1088 type = process_arg(event, arg, &token);
1089 if (test_type_token(type, token, EVENT_OP, "]"))
1090 goto out_free;
1092 top->op.right = arg;
1094 free_token(token);
1095 type = read_token_item(&token);
1096 *tok = token;
1098 return type;
1100 out_free:
1101 free_token(*tok);
1102 free_arg(arg);
1103 return EVENT_ERROR;
1106 static int get_op_prio(char *op)
1108 if (!op[1]) {
1109 switch (op[0]) {
1110 case '*':
1111 case '/':
1112 case '%':
1113 return 6;
1114 case '+':
1115 case '-':
1116 return 7;
1117 /* '>>' and '<<' are 8 */
1118 case '<':
1119 case '>':
1120 return 9;
1121 /* '==' and '!=' are 10 */
1122 case '&':
1123 return 11;
1124 case '^':
1125 return 12;
1126 case '|':
1127 return 13;
1128 case '?':
1129 return 16;
1130 default:
1131 die("unknown op '%c'", op[0]);
1132 return -1;
1134 } else {
1135 if (strcmp(op, "++") == 0 ||
1136 strcmp(op, "--") == 0) {
1137 return 3;
1138 } else if (strcmp(op, ">>") == 0 ||
1139 strcmp(op, "<<") == 0) {
1140 return 8;
1141 } else if (strcmp(op, ">=") == 0 ||
1142 strcmp(op, "<=") == 0) {
1143 return 9;
1144 } else if (strcmp(op, "==") == 0 ||
1145 strcmp(op, "!=") == 0) {
1146 return 10;
1147 } else if (strcmp(op, "&&") == 0) {
1148 return 14;
1149 } else if (strcmp(op, "||") == 0) {
1150 return 15;
1151 } else {
1152 die("unknown op '%s'", op);
1153 return -1;
1158 static void set_op_prio(struct print_arg *arg)
1161 /* single ops are the greatest */
1162 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1163 arg->op.prio = 0;
1164 return;
1167 arg->op.prio = get_op_prio(arg->op.op);
1170 static enum event_type
1171 process_op(struct event *event, struct print_arg *arg, char **tok)
1173 struct print_arg *left, *right = NULL;
1174 enum event_type type;
1175 char *token;
1177 /* the op is passed in via tok */
1178 token = *tok;
1180 if (arg->type == PRINT_OP && !arg->op.left) {
1181 /* handle single op */
1182 if (token[1]) {
1183 die("bad op token %s", token);
1184 return EVENT_ERROR;
1186 switch (token[0]) {
1187 case '!':
1188 case '+':
1189 case '-':
1190 break;
1191 default:
1192 die("bad op token %s", token);
1193 return EVENT_ERROR;
1196 /* make an empty left */
1197 left = malloc_or_die(sizeof(*left));
1198 left->type = PRINT_NULL;
1199 arg->op.left = left;
1201 right = malloc_or_die(sizeof(*right));
1202 arg->op.right = right;
1204 type = process_arg(event, right, tok);
1206 } else if (strcmp(token, "?") == 0) {
1208 left = malloc_or_die(sizeof(*left));
1209 /* copy the top arg to the left */
1210 *left = *arg;
1212 arg->type = PRINT_OP;
1213 arg->op.op = token;
1214 arg->op.left = left;
1215 arg->op.prio = 0;
1217 type = process_cond(event, arg, tok);
1219 } else if (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 ||
1233 strcmp(token, "!=") == 0) {
1235 left = malloc_or_die(sizeof(*left));
1237 /* copy the top arg to the left */
1238 *left = *arg;
1240 arg->type = PRINT_OP;
1241 arg->op.op = token;
1242 arg->op.left = left;
1244 set_op_prio(arg);
1246 right = malloc_or_die(sizeof(*right));
1248 type = read_token_item(&token);
1249 *tok = token;
1251 /* could just be a type pointer */
1252 if ((strcmp(arg->op.op, "*") == 0) &&
1253 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1254 if (left->type != PRINT_ATOM)
1255 die("bad pointer type");
1256 left->atom.atom = realloc(left->atom.atom,
1257 sizeof(left->atom.atom) + 3);
1258 strcat(left->atom.atom, " *");
1259 *arg = *left;
1260 free(arg);
1262 return type;
1265 type = process_arg_token(event, right, tok, type);
1267 arg->op.right = right;
1269 } else if (strcmp(token, "[") == 0) {
1271 left = malloc_or_die(sizeof(*left));
1272 *left = *arg;
1274 arg->type = PRINT_OP;
1275 arg->op.op = token;
1276 arg->op.left = left;
1278 arg->op.prio = 0;
1279 type = process_array(event, arg, tok);
1281 } else {
1282 warning("unknown op '%s'", token);
1283 event->flags |= EVENT_FL_FAILED;
1284 /* the arg is now the left side */
1285 return EVENT_NONE;
1288 if (type == EVENT_OP) {
1289 int prio;
1291 /* higher prios need to be closer to the root */
1292 prio = get_op_prio(*tok);
1294 if (prio > arg->op.prio)
1295 return process_op(event, arg, tok);
1297 return process_op(event, right, tok);
1300 return type;
1303 static enum event_type
1304 process_entry(struct event *event __unused, struct print_arg *arg,
1305 char **tok)
1307 enum event_type type;
1308 char *field;
1309 char *token;
1311 if (read_expected(EVENT_OP, "->") < 0)
1312 return EVENT_ERROR;
1314 if (read_expect_type(EVENT_ITEM, &token) < 0)
1315 goto fail;
1316 field = token;
1318 arg->type = PRINT_FIELD;
1319 arg->field.name = field;
1321 if (is_flag_field) {
1322 arg->field.field = find_any_field(event, arg->field.name);
1323 arg->field.field->flags |= FIELD_IS_FLAG;
1324 is_flag_field = 0;
1325 } else if (is_symbolic_field) {
1326 arg->field.field = find_any_field(event, arg->field.name);
1327 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1328 is_symbolic_field = 0;
1331 type = read_token(&token);
1332 *tok = token;
1334 return type;
1336 fail:
1337 free_token(token);
1338 return EVENT_ERROR;
1341 static char *arg_eval (struct print_arg *arg);
1343 static long long arg_num_eval(struct print_arg *arg)
1345 long long left, right;
1346 long long val = 0;
1348 switch (arg->type) {
1349 case PRINT_ATOM:
1350 val = strtoll(arg->atom.atom, NULL, 0);
1351 break;
1352 case PRINT_TYPE:
1353 val = arg_num_eval(arg->typecast.item);
1354 break;
1355 case PRINT_OP:
1356 switch (arg->op.op[0]) {
1357 case '|':
1358 left = arg_num_eval(arg->op.left);
1359 right = arg_num_eval(arg->op.right);
1360 if (arg->op.op[1])
1361 val = left || right;
1362 else
1363 val = left | right;
1364 break;
1365 case '&':
1366 left = arg_num_eval(arg->op.left);
1367 right = arg_num_eval(arg->op.right);
1368 if (arg->op.op[1])
1369 val = left && right;
1370 else
1371 val = left & right;
1372 break;
1373 case '<':
1374 left = arg_num_eval(arg->op.left);
1375 right = arg_num_eval(arg->op.right);
1376 switch (arg->op.op[1]) {
1377 case 0:
1378 val = left < right;
1379 break;
1380 case '<':
1381 val = left << right;
1382 break;
1383 case '=':
1384 val = left <= right;
1385 break;
1386 default:
1387 die("unknown op '%s'", arg->op.op);
1389 break;
1390 case '>':
1391 left = arg_num_eval(arg->op.left);
1392 right = arg_num_eval(arg->op.right);
1393 switch (arg->op.op[1]) {
1394 case 0:
1395 val = left > right;
1396 break;
1397 case '>':
1398 val = left >> right;
1399 break;
1400 case '=':
1401 val = left >= right;
1402 break;
1403 default:
1404 die("unknown op '%s'", arg->op.op);
1406 break;
1407 case '=':
1408 left = arg_num_eval(arg->op.left);
1409 right = arg_num_eval(arg->op.right);
1411 if (arg->op.op[1] != '=')
1412 die("unknown op '%s'", arg->op.op);
1414 val = left == right;
1415 break;
1416 case '!':
1417 left = arg_num_eval(arg->op.left);
1418 right = arg_num_eval(arg->op.right);
1420 switch (arg->op.op[1]) {
1421 case '=':
1422 val = left != right;
1423 break;
1424 default:
1425 die("unknown op '%s'", arg->op.op);
1427 break;
1428 default:
1429 die("unknown op '%s'", arg->op.op);
1431 break;
1433 case PRINT_NULL:
1434 case PRINT_FIELD ... PRINT_SYMBOL:
1435 case PRINT_STRING:
1436 default:
1437 die("invalid eval type %d", arg->type);
1440 return val;
1443 static char *arg_eval (struct print_arg *arg)
1445 long long val;
1446 static char buf[20];
1448 switch (arg->type) {
1449 case PRINT_ATOM:
1450 return arg->atom.atom;
1451 case PRINT_TYPE:
1452 return arg_eval(arg->typecast.item);
1453 case PRINT_OP:
1454 val = arg_num_eval(arg);
1455 sprintf(buf, "%lld", val);
1456 return buf;
1458 case PRINT_NULL:
1459 case PRINT_FIELD ... PRINT_SYMBOL:
1460 case PRINT_STRING:
1461 default:
1462 die("invalid eval type %d", arg->type);
1463 break;
1466 return NULL;
1469 static enum event_type
1470 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1472 enum event_type type;
1473 struct print_arg *arg = NULL;
1474 struct print_flag_sym *field;
1475 char *token = NULL;
1476 char *value;
1478 do {
1479 free_token(token);
1480 type = read_token_item(&token);
1481 if (test_type_token(type, token, EVENT_OP, "{"))
1482 break;
1484 arg = malloc_or_die(sizeof(*arg));
1486 free_token(token);
1487 type = process_arg(event, arg, &token);
1488 if (test_type_token(type, token, EVENT_DELIM, ","))
1489 goto out_free;
1491 field = malloc_or_die(sizeof(*field));
1492 memset(field, 0, sizeof(*field));
1494 value = arg_eval(arg);
1495 field->value = strdup(value);
1497 free_token(token);
1498 type = process_arg(event, arg, &token);
1499 if (test_type_token(type, token, EVENT_OP, "}"))
1500 goto out_free;
1502 value = arg_eval(arg);
1503 field->str = strdup(value);
1504 free_arg(arg);
1505 arg = NULL;
1507 *list = field;
1508 list = &field->next;
1510 free_token(token);
1511 type = read_token_item(&token);
1512 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1514 *tok = token;
1515 return type;
1517 out_free:
1518 free_arg(arg);
1519 free_token(token);
1521 return EVENT_ERROR;
1524 static enum event_type
1525 process_flags(struct event *event, struct print_arg *arg, char **tok)
1527 struct print_arg *field;
1528 enum event_type type;
1529 char *token;
1531 memset(arg, 0, sizeof(*arg));
1532 arg->type = PRINT_FLAGS;
1534 if (read_expected_item(EVENT_DELIM, "(") < 0)
1535 return EVENT_ERROR;
1537 field = malloc_or_die(sizeof(*field));
1539 type = process_arg(event, field, &token);
1540 while (type == EVENT_OP)
1541 type = process_op(event, field, &token);
1542 if (test_type_token(type, token, EVENT_DELIM, ","))
1543 goto out_free;
1545 arg->flags.field = field;
1547 type = read_token_item(&token);
1548 if (event_item_type(type)) {
1549 arg->flags.delim = token;
1550 type = read_token_item(&token);
1553 if (test_type_token(type, token, EVENT_DELIM, ","))
1554 goto out_free;
1556 type = process_fields(event, &arg->flags.flags, &token);
1557 if (test_type_token(type, token, EVENT_DELIM, ")"))
1558 goto out_free;
1560 free_token(token);
1561 type = read_token_item(tok);
1562 return type;
1564 out_free:
1565 free_token(token);
1566 return EVENT_ERROR;
1569 static enum event_type
1570 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1572 struct print_arg *field;
1573 enum event_type type;
1574 char *token;
1576 memset(arg, 0, sizeof(*arg));
1577 arg->type = PRINT_SYMBOL;
1579 if (read_expected_item(EVENT_DELIM, "(") < 0)
1580 return EVENT_ERROR;
1582 field = malloc_or_die(sizeof(*field));
1584 type = process_arg(event, field, &token);
1585 while (type == EVENT_OP)
1586 type = process_op(event, field, &token);
1587 if (test_type_token(type, token, EVENT_DELIM, ","))
1588 goto out_free;
1590 arg->symbol.field = field;
1592 type = process_fields(event, &arg->symbol.symbols, &token);
1593 if (test_type_token(type, token, EVENT_DELIM, ")"))
1594 goto out_free;
1596 free_token(token);
1597 type = read_token_item(tok);
1598 return type;
1600 out_free:
1601 free_token(token);
1602 return EVENT_ERROR;
1605 static enum event_type
1606 process_paren(struct event *event, struct print_arg *arg, char **tok)
1608 struct print_arg *item_arg;
1609 enum event_type type;
1610 char *token;
1612 type = process_arg(event, arg, &token);
1614 if (type == EVENT_ERROR)
1615 return EVENT_ERROR;
1617 if (type == EVENT_OP)
1618 type = process_op(event, arg, &token);
1620 if (type == EVENT_ERROR)
1621 return EVENT_ERROR;
1623 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1624 free_token(token);
1625 return EVENT_ERROR;
1628 free_token(token);
1629 type = read_token_item(&token);
1632 * If the next token is an item or another open paren, then
1633 * this was a typecast.
1635 if (event_item_type(type) ||
1636 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1638 /* make this a typecast and contine */
1640 /* prevous must be an atom */
1641 if (arg->type != PRINT_ATOM)
1642 die("previous needed to be PRINT_ATOM");
1644 item_arg = malloc_or_die(sizeof(*item_arg));
1646 arg->type = PRINT_TYPE;
1647 arg->typecast.type = arg->atom.atom;
1648 arg->typecast.item = item_arg;
1649 type = process_arg_token(event, item_arg, &token, type);
1653 *tok = token;
1654 return type;
1658 static enum event_type
1659 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1661 enum event_type type;
1662 char *token;
1664 if (read_expected(EVENT_DELIM, "(") < 0)
1665 return EVENT_ERROR;
1667 if (read_expect_type(EVENT_ITEM, &token) < 0)
1668 goto fail;
1670 arg->type = PRINT_STRING;
1671 arg->string.string = token;
1672 arg->string.offset = -1;
1674 if (read_expected(EVENT_DELIM, ")") < 0)
1675 return EVENT_ERROR;
1677 type = read_token(&token);
1678 *tok = token;
1680 return type;
1681 fail:
1682 free_token(token);
1683 return EVENT_ERROR;
1686 enum event_type
1687 process_arg_token(struct event *event, struct print_arg *arg,
1688 char **tok, enum event_type type)
1690 char *token;
1691 char *atom;
1693 token = *tok;
1695 switch (type) {
1696 case EVENT_ITEM:
1697 if (strcmp(token, "REC") == 0) {
1698 free_token(token);
1699 type = process_entry(event, arg, &token);
1700 } else if (strcmp(token, "__print_flags") == 0) {
1701 free_token(token);
1702 is_flag_field = 1;
1703 type = process_flags(event, arg, &token);
1704 } else if (strcmp(token, "__print_symbolic") == 0) {
1705 free_token(token);
1706 is_symbolic_field = 1;
1707 type = process_symbols(event, arg, &token);
1708 } else if (strcmp(token, "__get_str") == 0) {
1709 free_token(token);
1710 type = process_str(event, arg, &token);
1711 } else {
1712 atom = token;
1713 /* test the next token */
1714 type = read_token_item(&token);
1716 /* atoms can be more than one token long */
1717 while (type == EVENT_ITEM) {
1718 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1719 strcat(atom, " ");
1720 strcat(atom, token);
1721 free_token(token);
1722 type = read_token_item(&token);
1725 /* todo, test for function */
1727 arg->type = PRINT_ATOM;
1728 arg->atom.atom = atom;
1730 break;
1731 case EVENT_DQUOTE:
1732 case EVENT_SQUOTE:
1733 arg->type = PRINT_ATOM;
1734 arg->atom.atom = token;
1735 type = read_token_item(&token);
1736 break;
1737 case EVENT_DELIM:
1738 if (strcmp(token, "(") == 0) {
1739 free_token(token);
1740 type = process_paren(event, arg, &token);
1741 break;
1743 case EVENT_OP:
1744 /* handle single ops */
1745 arg->type = PRINT_OP;
1746 arg->op.op = token;
1747 arg->op.left = NULL;
1748 type = process_op(event, arg, &token);
1750 break;
1752 case EVENT_ERROR ... EVENT_NEWLINE:
1753 default:
1754 die("unexpected type %d", type);
1756 *tok = token;
1758 return type;
1761 static int event_read_print_args(struct event *event, struct print_arg **list)
1763 enum event_type type = EVENT_ERROR;
1764 struct print_arg *arg;
1765 char *token;
1766 int args = 0;
1768 do {
1769 if (type == EVENT_NEWLINE) {
1770 free_token(token);
1771 type = read_token_item(&token);
1772 continue;
1775 arg = malloc_or_die(sizeof(*arg));
1776 memset(arg, 0, sizeof(*arg));
1778 type = process_arg(event, arg, &token);
1780 if (type == EVENT_ERROR) {
1781 free_arg(arg);
1782 return -1;
1785 *list = arg;
1786 args++;
1788 if (type == EVENT_OP) {
1789 type = process_op(event, arg, &token);
1790 list = &arg->next;
1791 continue;
1794 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1795 free_token(token);
1796 *list = arg;
1797 list = &arg->next;
1798 continue;
1800 break;
1801 } while (type != EVENT_NONE);
1803 if (type != EVENT_NONE)
1804 free_token(token);
1806 return args;
1809 static int event_read_print(struct event *event)
1811 enum event_type type;
1812 char *token;
1813 int ret;
1815 if (read_expected_item(EVENT_ITEM, "print") < 0)
1816 return -1;
1818 if (read_expected(EVENT_ITEM, "fmt") < 0)
1819 return -1;
1821 if (read_expected(EVENT_OP, ":") < 0)
1822 return -1;
1824 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1825 goto fail;
1827 concat:
1828 event->print_fmt.format = token;
1829 event->print_fmt.args = NULL;
1831 /* ok to have no arg */
1832 type = read_token_item(&token);
1834 if (type == EVENT_NONE)
1835 return 0;
1837 /* Handle concatination of print lines */
1838 if (type == EVENT_DQUOTE) {
1839 char *cat;
1841 cat = malloc_or_die(strlen(event->print_fmt.format) +
1842 strlen(token) + 1);
1843 strcpy(cat, event->print_fmt.format);
1844 strcat(cat, token);
1845 free_token(token);
1846 free_token(event->print_fmt.format);
1847 event->print_fmt.format = NULL;
1848 token = cat;
1849 goto concat;
1852 if (test_type_token(type, token, EVENT_DELIM, ","))
1853 goto fail;
1855 free_token(token);
1857 ret = event_read_print_args(event, &event->print_fmt.args);
1858 if (ret < 0)
1859 return -1;
1861 return ret;
1863 fail:
1864 free_token(token);
1865 return -1;
1868 static struct format_field *
1869 find_common_field(struct event *event, const char *name)
1871 struct format_field *format;
1873 for (format = event->format.common_fields;
1874 format; format = format->next) {
1875 if (strcmp(format->name, name) == 0)
1876 break;
1879 return format;
1882 static struct format_field *
1883 find_field(struct event *event, const char *name)
1885 struct format_field *format;
1887 for (format = event->format.fields;
1888 format; format = format->next) {
1889 if (strcmp(format->name, name) == 0)
1890 break;
1893 return format;
1896 static struct format_field *
1897 find_any_field(struct event *event, const char *name)
1899 struct format_field *format;
1901 format = find_common_field(event, name);
1902 if (format)
1903 return format;
1904 return find_field(event, name);
1907 unsigned long long read_size(void *ptr, int size)
1909 switch (size) {
1910 case 1:
1911 return *(unsigned char *)ptr;
1912 case 2:
1913 return data2host2(ptr);
1914 case 4:
1915 return data2host4(ptr);
1916 case 8:
1917 return data2host8(ptr);
1918 default:
1919 /* BUG! */
1920 return 0;
1924 unsigned long long
1925 raw_field_value(struct event *event, const char *name, void *data)
1927 struct format_field *field;
1929 field = find_any_field(event, name);
1930 if (!field)
1931 return 0ULL;
1933 return read_size(data + field->offset, field->size);
1936 void *raw_field_ptr(struct event *event, const char *name, void *data)
1938 struct format_field *field;
1940 field = find_any_field(event, name);
1941 if (!field)
1942 return NULL;
1944 if (field->flags & FIELD_IS_DYNAMIC) {
1945 int offset;
1947 offset = *(int *)(data + field->offset);
1948 offset &= 0xffff;
1950 return data + offset;
1953 return data + field->offset;
1956 static int get_common_info(const char *type, int *offset, int *size)
1958 struct event *event;
1959 struct format_field *field;
1962 * All events should have the same common elements.
1963 * Pick any event to find where the type is;
1965 if (!event_list)
1966 die("no event_list!");
1968 event = event_list;
1969 field = find_common_field(event, type);
1970 if (!field)
1971 die("field '%s' not found", type);
1973 *offset = field->offset;
1974 *size = field->size;
1976 return 0;
1979 static int __parse_common(void *data, int *size, int *offset,
1980 const char *name)
1982 int ret;
1984 if (!*size) {
1985 ret = get_common_info(name, offset, size);
1986 if (ret < 0)
1987 return ret;
1989 return read_size(data + *offset, *size);
1992 int trace_parse_common_type(void *data)
1994 static int type_offset;
1995 static int type_size;
1997 return __parse_common(data, &type_size, &type_offset,
1998 "common_type");
2001 int trace_parse_common_pid(void *data)
2003 static int pid_offset;
2004 static int pid_size;
2006 return __parse_common(data, &pid_size, &pid_offset,
2007 "common_pid");
2010 int parse_common_pc(void *data)
2012 static int pc_offset;
2013 static int pc_size;
2015 return __parse_common(data, &pc_size, &pc_offset,
2016 "common_preempt_count");
2019 int parse_common_flags(void *data)
2021 static int flags_offset;
2022 static int flags_size;
2024 return __parse_common(data, &flags_size, &flags_offset,
2025 "common_flags");
2028 int parse_common_lock_depth(void *data)
2030 static int ld_offset;
2031 static int ld_size;
2032 int ret;
2034 ret = __parse_common(data, &ld_size, &ld_offset,
2035 "common_lock_depth");
2036 if (ret < 0)
2037 return -1;
2039 return ret;
2042 struct event *trace_find_event(int id)
2044 struct event *event;
2046 for (event = event_list; event; event = event->next) {
2047 if (event->id == id)
2048 break;
2050 return event;
2053 struct event *trace_find_next_event(struct event *event)
2055 if (!event)
2056 return event_list;
2058 return event->next;
2061 static unsigned long long eval_num_arg(void *data, int size,
2062 struct event *event, struct print_arg *arg)
2064 unsigned long long val = 0;
2065 unsigned long long left, right;
2066 struct print_arg *larg;
2068 switch (arg->type) {
2069 case PRINT_NULL:
2070 /* ?? */
2071 return 0;
2072 case PRINT_ATOM:
2073 return strtoull(arg->atom.atom, NULL, 0);
2074 case PRINT_FIELD:
2075 if (!arg->field.field) {
2076 arg->field.field = find_any_field(event, arg->field.name);
2077 if (!arg->field.field)
2078 die("field %s not found", arg->field.name);
2080 /* must be a number */
2081 val = read_size(data + arg->field.field->offset,
2082 arg->field.field->size);
2083 break;
2084 case PRINT_FLAGS:
2085 case PRINT_SYMBOL:
2086 break;
2087 case PRINT_TYPE:
2088 return eval_num_arg(data, size, event, arg->typecast.item);
2089 case PRINT_STRING:
2090 return 0;
2091 break;
2092 case PRINT_OP:
2093 if (strcmp(arg->op.op, "[") == 0) {
2095 * Arrays are special, since we don't want
2096 * to read the arg as is.
2098 if (arg->op.left->type != PRINT_FIELD)
2099 goto default_op; /* oops, all bets off */
2100 larg = arg->op.left;
2101 if (!larg->field.field) {
2102 larg->field.field =
2103 find_any_field(event, larg->field.name);
2104 if (!larg->field.field)
2105 die("field %s not found", larg->field.name);
2107 right = eval_num_arg(data, size, event, arg->op.right);
2108 val = read_size(data + larg->field.field->offset +
2109 right * long_size, long_size);
2110 break;
2112 default_op:
2113 left = eval_num_arg(data, size, event, arg->op.left);
2114 right = eval_num_arg(data, size, event, arg->op.right);
2115 switch (arg->op.op[0]) {
2116 case '|':
2117 if (arg->op.op[1])
2118 val = left || right;
2119 else
2120 val = left | right;
2121 break;
2122 case '&':
2123 if (arg->op.op[1])
2124 val = left && right;
2125 else
2126 val = left & right;
2127 break;
2128 case '<':
2129 switch (arg->op.op[1]) {
2130 case 0:
2131 val = left < right;
2132 break;
2133 case '<':
2134 val = left << right;
2135 break;
2136 case '=':
2137 val = left <= right;
2138 break;
2139 default:
2140 die("unknown op '%s'", arg->op.op);
2142 break;
2143 case '>':
2144 switch (arg->op.op[1]) {
2145 case 0:
2146 val = left > right;
2147 break;
2148 case '>':
2149 val = left >> right;
2150 break;
2151 case '=':
2152 val = left >= right;
2153 break;
2154 default:
2155 die("unknown op '%s'", arg->op.op);
2157 break;
2158 case '=':
2159 if (arg->op.op[1] != '=')
2160 die("unknown op '%s'", arg->op.op);
2161 val = left == right;
2162 break;
2163 case '-':
2164 val = left - right;
2165 break;
2166 case '+':
2167 val = left + right;
2168 break;
2169 default:
2170 die("unknown op '%s'", arg->op.op);
2172 break;
2173 default: /* not sure what to do there */
2174 return 0;
2176 return val;
2179 struct flag {
2180 const char *name;
2181 unsigned long long value;
2184 static const struct flag flags[] = {
2185 { "HI_SOFTIRQ", 0 },
2186 { "TIMER_SOFTIRQ", 1 },
2187 { "NET_TX_SOFTIRQ", 2 },
2188 { "NET_RX_SOFTIRQ", 3 },
2189 { "BLOCK_SOFTIRQ", 4 },
2190 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2191 { "TASKLET_SOFTIRQ", 6 },
2192 { "SCHED_SOFTIRQ", 7 },
2193 { "HRTIMER_SOFTIRQ", 8 },
2194 { "RCU_SOFTIRQ", 9 },
2196 { "HRTIMER_NORESTART", 0 },
2197 { "HRTIMER_RESTART", 1 },
2200 unsigned long long eval_flag(const char *flag)
2202 int i;
2205 * Some flags in the format files do not get converted.
2206 * If the flag is not numeric, see if it is something that
2207 * we already know about.
2209 if (isdigit(flag[0]))
2210 return strtoull(flag, NULL, 0);
2212 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2213 if (strcmp(flags[i].name, flag) == 0)
2214 return flags[i].value;
2216 return 0;
2219 static void print_str_arg(void *data, int size,
2220 struct event *event, struct print_arg *arg)
2222 struct print_flag_sym *flag;
2223 unsigned long long val, fval;
2224 char *str;
2225 int print;
2227 switch (arg->type) {
2228 case PRINT_NULL:
2229 /* ?? */
2230 return;
2231 case PRINT_ATOM:
2232 printf("%s", arg->atom.atom);
2233 return;
2234 case PRINT_FIELD:
2235 if (!arg->field.field) {
2236 arg->field.field = find_any_field(event, arg->field.name);
2237 if (!arg->field.field)
2238 die("field %s not found", arg->field.name);
2240 str = malloc_or_die(arg->field.field->size + 1);
2241 memcpy(str, data + arg->field.field->offset,
2242 arg->field.field->size);
2243 str[arg->field.field->size] = 0;
2244 printf("%s", str);
2245 free(str);
2246 break;
2247 case PRINT_FLAGS:
2248 val = eval_num_arg(data, size, event, arg->flags.field);
2249 print = 0;
2250 for (flag = arg->flags.flags; flag; flag = flag->next) {
2251 fval = eval_flag(flag->value);
2252 if (!val && !fval) {
2253 printf("%s", flag->str);
2254 break;
2256 if (fval && (val & fval) == fval) {
2257 if (print && arg->flags.delim)
2258 printf("%s", arg->flags.delim);
2259 printf("%s", flag->str);
2260 print = 1;
2261 val &= ~fval;
2264 break;
2265 case PRINT_SYMBOL:
2266 val = eval_num_arg(data, size, event, arg->symbol.field);
2267 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2268 fval = eval_flag(flag->value);
2269 if (val == fval) {
2270 printf("%s", flag->str);
2271 break;
2274 break;
2276 case PRINT_TYPE:
2277 break;
2278 case PRINT_STRING: {
2279 int str_offset;
2281 if (arg->string.offset == -1) {
2282 struct format_field *f;
2284 f = find_any_field(event, arg->string.string);
2285 arg->string.offset = f->offset;
2287 str_offset = *(int *)(data + arg->string.offset);
2288 str_offset &= 0xffff;
2289 printf("%s", ((char *)data) + str_offset);
2290 break;
2292 case PRINT_OP:
2294 * The only op for string should be ? :
2296 if (arg->op.op[0] != '?')
2297 return;
2298 val = eval_num_arg(data, size, event, arg->op.left);
2299 if (val)
2300 print_str_arg(data, size, event, arg->op.right->op.left);
2301 else
2302 print_str_arg(data, size, event, arg->op.right->op.right);
2303 break;
2304 default:
2305 /* well... */
2306 break;
2310 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2312 static struct format_field *field, *ip_field;
2313 struct print_arg *args, *arg, **next;
2314 unsigned long long ip, val;
2315 char *ptr;
2316 void *bptr;
2318 if (!field) {
2319 field = find_field(event, "buf");
2320 if (!field)
2321 die("can't find buffer field for binary printk");
2322 ip_field = find_field(event, "ip");
2323 if (!ip_field)
2324 die("can't find ip field for binary printk");
2327 ip = read_size(data + ip_field->offset, ip_field->size);
2330 * The first arg is the IP pointer.
2332 args = malloc_or_die(sizeof(*args));
2333 arg = args;
2334 arg->next = NULL;
2335 next = &arg->next;
2337 arg->type = PRINT_ATOM;
2338 arg->atom.atom = malloc_or_die(32);
2339 sprintf(arg->atom.atom, "%lld", ip);
2341 /* skip the first "%pf : " */
2342 for (ptr = fmt + 6, bptr = data + field->offset;
2343 bptr < data + size && *ptr; ptr++) {
2344 int ls = 0;
2346 if (*ptr == '%') {
2347 process_again:
2348 ptr++;
2349 switch (*ptr) {
2350 case '%':
2351 break;
2352 case 'l':
2353 ls++;
2354 goto process_again;
2355 case 'L':
2356 ls = 2;
2357 goto process_again;
2358 case '0' ... '9':
2359 goto process_again;
2360 case 'p':
2361 ls = 1;
2362 /* fall through */
2363 case 'd':
2364 case 'u':
2365 case 'x':
2366 case 'i':
2367 /* the pointers are always 4 bytes aligned */
2368 bptr = (void *)(((unsigned long)bptr + 3) &
2369 ~3);
2370 switch (ls) {
2371 case 0:
2372 case 1:
2373 ls = long_size;
2374 break;
2375 case 2:
2376 ls = 8;
2377 default:
2378 break;
2380 val = read_size(bptr, ls);
2381 bptr += ls;
2382 arg = malloc_or_die(sizeof(*arg));
2383 arg->next = NULL;
2384 arg->type = PRINT_ATOM;
2385 arg->atom.atom = malloc_or_die(32);
2386 sprintf(arg->atom.atom, "%lld", val);
2387 *next = arg;
2388 next = &arg->next;
2389 break;
2390 case 's':
2391 arg = malloc_or_die(sizeof(*arg));
2392 arg->next = NULL;
2393 arg->type = PRINT_STRING;
2394 arg->string.string = strdup(bptr);
2395 bptr += strlen(bptr) + 1;
2396 *next = arg;
2397 next = &arg->next;
2398 default:
2399 break;
2404 return args;
2407 static void free_args(struct print_arg *args)
2409 struct print_arg *next;
2411 while (args) {
2412 next = args->next;
2414 if (args->type == PRINT_ATOM)
2415 free(args->atom.atom);
2416 else
2417 free(args->string.string);
2418 free(args);
2419 args = next;
2423 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2425 unsigned long long addr;
2426 static struct format_field *field;
2427 struct printk_map *printk;
2428 char *format;
2429 char *p;
2431 if (!field) {
2432 field = find_field(event, "fmt");
2433 if (!field)
2434 die("can't find format field for binary printk");
2435 printf("field->offset = %d size=%d\n", field->offset, field->size);
2438 addr = read_size(data + field->offset, field->size);
2440 printk = find_printk(addr);
2441 if (!printk) {
2442 format = malloc_or_die(45);
2443 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2444 addr);
2445 return format;
2448 p = printk->printk;
2449 /* Remove any quotes. */
2450 if (*p == '"')
2451 p++;
2452 format = malloc_or_die(strlen(p) + 10);
2453 sprintf(format, "%s : %s", "%pf", p);
2454 /* remove ending quotes and new line since we will add one too */
2455 p = format + strlen(format) - 1;
2456 if (*p == '"')
2457 *p = 0;
2459 p -= 2;
2460 if (strcmp(p, "\\n") == 0)
2461 *p = 0;
2463 return format;
2466 static void pretty_print(void *data, int size, struct event *event)
2468 struct print_fmt *print_fmt = &event->print_fmt;
2469 struct print_arg *arg = print_fmt->args;
2470 struct print_arg *args = NULL;
2471 const char *ptr = print_fmt->format;
2472 unsigned long long val;
2473 struct func_map *func;
2474 const char *saveptr;
2475 char *bprint_fmt = NULL;
2476 char format[32];
2477 int show_func;
2478 int len;
2479 int ls;
2481 if (event->flags & EVENT_FL_ISFUNC)
2482 ptr = " %pF <-- %pF";
2484 if (event->flags & EVENT_FL_ISBPRINT) {
2485 bprint_fmt = get_bprint_format(data, size, event);
2486 args = make_bprint_args(bprint_fmt, data, size, event);
2487 arg = args;
2488 ptr = bprint_fmt;
2491 for (; *ptr; ptr++) {
2492 ls = 0;
2493 if (*ptr == '\\') {
2494 ptr++;
2495 switch (*ptr) {
2496 case 'n':
2497 printf("\n");
2498 break;
2499 case 't':
2500 printf("\t");
2501 break;
2502 case 'r':
2503 printf("\r");
2504 break;
2505 case '\\':
2506 printf("\\");
2507 break;
2508 default:
2509 printf("%c", *ptr);
2510 break;
2513 } else if (*ptr == '%') {
2514 saveptr = ptr;
2515 show_func = 0;
2516 cont_process:
2517 ptr++;
2518 switch (*ptr) {
2519 case '%':
2520 printf("%%");
2521 break;
2522 case 'l':
2523 ls++;
2524 goto cont_process;
2525 case 'L':
2526 ls = 2;
2527 goto cont_process;
2528 case 'z':
2529 case 'Z':
2530 case '0' ... '9':
2531 goto cont_process;
2532 case 'p':
2533 if (long_size == 4)
2534 ls = 1;
2535 else
2536 ls = 2;
2538 if (*(ptr+1) == 'F' ||
2539 *(ptr+1) == 'f') {
2540 ptr++;
2541 show_func = *ptr;
2544 /* fall through */
2545 case 'd':
2546 case 'i':
2547 case 'x':
2548 case 'X':
2549 case 'u':
2550 if (!arg)
2551 die("no argument match");
2553 len = ((unsigned long)ptr + 1) -
2554 (unsigned long)saveptr;
2556 /* should never happen */
2557 if (len > 32)
2558 die("bad format!");
2560 memcpy(format, saveptr, len);
2561 format[len] = 0;
2563 val = eval_num_arg(data, size, event, arg);
2564 arg = arg->next;
2566 if (show_func) {
2567 func = find_func(val);
2568 if (func) {
2569 printf("%s", func->func);
2570 if (show_func == 'F')
2571 printf("+0x%llx",
2572 val - func->addr);
2573 break;
2576 switch (ls) {
2577 case 0:
2578 printf(format, (int)val);
2579 break;
2580 case 1:
2581 printf(format, (long)val);
2582 break;
2583 case 2:
2584 printf(format, (long long)val);
2585 break;
2586 default:
2587 die("bad count (%d)", ls);
2589 break;
2590 case 's':
2591 if (!arg)
2592 die("no matching argument");
2594 print_str_arg(data, size, event, arg);
2595 arg = arg->next;
2596 break;
2597 default:
2598 printf(">%c<", *ptr);
2601 } else
2602 printf("%c", *ptr);
2605 if (args) {
2606 free_args(args);
2607 free(bprint_fmt);
2611 static inline int log10_cpu(int nb)
2613 if (nb / 100)
2614 return 3;
2615 if (nb / 10)
2616 return 2;
2617 return 1;
2620 static void print_lat_fmt(void *data, int size __unused)
2622 unsigned int lat_flags;
2623 unsigned int pc;
2624 int lock_depth;
2625 int hardirq;
2626 int softirq;
2628 lat_flags = parse_common_flags(data);
2629 pc = parse_common_pc(data);
2630 lock_depth = parse_common_lock_depth(data);
2632 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2633 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2635 printf("%c%c%c",
2636 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2637 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2638 'X' : '.',
2639 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2640 'N' : '.',
2641 (hardirq && softirq) ? 'H' :
2642 hardirq ? 'h' : softirq ? 's' : '.');
2644 if (pc)
2645 printf("%x", pc);
2646 else
2647 printf(".");
2649 if (lock_depth < 0)
2650 printf(". ");
2651 else
2652 printf("%d ", lock_depth);
2655 #define TRACE_GRAPH_INDENT 2
2657 static struct record *
2658 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2659 struct record *next)
2661 struct format_field *field;
2662 struct event *event;
2663 unsigned long val;
2664 int type;
2665 int pid;
2667 type = trace_parse_common_type(next->data);
2668 event = trace_find_event(type);
2669 if (!event)
2670 return NULL;
2672 if (!(event->flags & EVENT_FL_ISFUNCRET))
2673 return NULL;
2675 pid = trace_parse_common_pid(next->data);
2676 field = find_field(event, "func");
2677 if (!field)
2678 die("function return does not have field func");
2680 val = read_size(next->data + field->offset, field->size);
2682 if (cur_pid != pid || cur_func != val)
2683 return NULL;
2685 /* this is a leaf, now advance the iterator */
2686 return trace_read_data(cpu);
2689 /* Signal a overhead of time execution to the output */
2690 static void print_graph_overhead(unsigned long long duration)
2692 /* Non nested entry or return */
2693 if (duration == ~0ULL)
2694 return (void)printf(" ");
2696 /* Duration exceeded 100 msecs */
2697 if (duration > 100000ULL)
2698 return (void)printf("! ");
2700 /* Duration exceeded 10 msecs */
2701 if (duration > 10000ULL)
2702 return (void)printf("+ ");
2704 printf(" ");
2707 static void print_graph_duration(unsigned long long duration)
2709 unsigned long usecs = duration / 1000;
2710 unsigned long nsecs_rem = duration % 1000;
2711 /* log10(ULONG_MAX) + '\0' */
2712 char msecs_str[21];
2713 char nsecs_str[5];
2714 int len;
2715 int i;
2717 sprintf(msecs_str, "%lu", usecs);
2719 /* Print msecs */
2720 len = printf("%lu", usecs);
2722 /* Print nsecs (we don't want to exceed 7 numbers) */
2723 if (len < 7) {
2724 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2725 len += printf(".%s", nsecs_str);
2728 printf(" us ");
2730 /* Print remaining spaces to fit the row's width */
2731 for (i = len; i < 7; i++)
2732 printf(" ");
2734 printf("| ");
2737 static void
2738 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2740 unsigned long long rettime, calltime;
2741 unsigned long long duration, depth;
2742 unsigned long long val;
2743 struct format_field *field;
2744 struct func_map *func;
2745 struct event *ret_event;
2746 int type;
2747 int i;
2749 type = trace_parse_common_type(ret_rec->data);
2750 ret_event = trace_find_event(type);
2752 field = find_field(ret_event, "rettime");
2753 if (!field)
2754 die("can't find rettime in return graph");
2755 rettime = read_size(ret_rec->data + field->offset, field->size);
2757 field = find_field(ret_event, "calltime");
2758 if (!field)
2759 die("can't find rettime in return graph");
2760 calltime = read_size(ret_rec->data + field->offset, field->size);
2762 duration = rettime - calltime;
2764 /* Overhead */
2765 print_graph_overhead(duration);
2767 /* Duration */
2768 print_graph_duration(duration);
2770 field = find_field(event, "depth");
2771 if (!field)
2772 die("can't find depth in entry graph");
2773 depth = read_size(data + field->offset, field->size);
2775 /* Function */
2776 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2777 printf(" ");
2779 field = find_field(event, "func");
2780 if (!field)
2781 die("can't find func in entry graph");
2782 val = read_size(data + field->offset, field->size);
2783 func = find_func(val);
2785 if (func)
2786 printf("%s();", func->func);
2787 else
2788 printf("%llx();", val);
2791 static void print_graph_nested(struct event *event, void *data)
2793 struct format_field *field;
2794 unsigned long long depth;
2795 unsigned long long val;
2796 struct func_map *func;
2797 int i;
2799 /* No overhead */
2800 print_graph_overhead(-1);
2802 /* No time */
2803 printf(" | ");
2805 field = find_field(event, "depth");
2806 if (!field)
2807 die("can't find depth in entry graph");
2808 depth = read_size(data + field->offset, field->size);
2810 /* Function */
2811 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2812 printf(" ");
2814 field = find_field(event, "func");
2815 if (!field)
2816 die("can't find func in entry graph");
2817 val = read_size(data + field->offset, field->size);
2818 func = find_func(val);
2820 if (func)
2821 printf("%s() {", func->func);
2822 else
2823 printf("%llx() {", val);
2826 static void
2827 pretty_print_func_ent(void *data, int size, struct event *event,
2828 int cpu, int pid)
2830 struct format_field *field;
2831 struct record *rec;
2832 void *copy_data;
2833 unsigned long val;
2835 if (latency_format) {
2836 print_lat_fmt(data, size);
2837 printf(" | ");
2840 field = find_field(event, "func");
2841 if (!field)
2842 die("function entry does not have func field");
2844 val = read_size(data + field->offset, field->size);
2847 * peek_data may unmap the data pointer. Copy it first.
2849 copy_data = malloc_or_die(size);
2850 memcpy(copy_data, data, size);
2851 data = copy_data;
2853 rec = trace_peek_data(cpu);
2854 if (rec) {
2855 rec = get_return_for_leaf(cpu, pid, val, rec);
2856 if (rec) {
2857 print_graph_entry_leaf(event, data, rec);
2858 goto out_free;
2861 print_graph_nested(event, data);
2862 out_free:
2863 free(data);
2866 static void
2867 pretty_print_func_ret(void *data, int size __unused, struct event *event)
2869 unsigned long long rettime, calltime;
2870 unsigned long long duration, depth;
2871 struct format_field *field;
2872 int i;
2874 if (latency_format) {
2875 print_lat_fmt(data, size);
2876 printf(" | ");
2879 field = find_field(event, "rettime");
2880 if (!field)
2881 die("can't find rettime in return graph");
2882 rettime = read_size(data + field->offset, field->size);
2884 field = find_field(event, "calltime");
2885 if (!field)
2886 die("can't find calltime in return graph");
2887 calltime = read_size(data + field->offset, field->size);
2889 duration = rettime - calltime;
2891 /* Overhead */
2892 print_graph_overhead(duration);
2894 /* Duration */
2895 print_graph_duration(duration);
2897 field = find_field(event, "depth");
2898 if (!field)
2899 die("can't find depth in entry graph");
2900 depth = read_size(data + field->offset, field->size);
2902 /* Function */
2903 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2904 printf(" ");
2906 printf("}");
2909 static void
2910 pretty_print_func_graph(void *data, int size, struct event *event,
2911 int cpu, int pid)
2913 if (event->flags & EVENT_FL_ISFUNCENT)
2914 pretty_print_func_ent(data, size, event, cpu, pid);
2915 else if (event->flags & EVENT_FL_ISFUNCRET)
2916 pretty_print_func_ret(data, size, event);
2917 printf("\n");
2920 void print_trace_event(int cpu, void *data, int size)
2922 struct event *event;
2923 int type;
2924 int pid;
2926 type = trace_parse_common_type(data);
2928 event = trace_find_event(type);
2929 if (!event) {
2930 warning("ug! no event found for type %d", type);
2931 return;
2934 pid = trace_parse_common_pid(data);
2936 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2937 return pretty_print_func_graph(data, size, event, cpu, pid);
2939 if (latency_format)
2940 print_lat_fmt(data, size);
2942 if (event->flags & EVENT_FL_FAILED) {
2943 printf("EVENT '%s' FAILED TO PARSE\n",
2944 event->name);
2945 return;
2948 pretty_print(data, size, event);
2951 static void print_fields(struct print_flag_sym *field)
2953 printf("{ %s, %s }", field->value, field->str);
2954 if (field->next) {
2955 printf(", ");
2956 print_fields(field->next);
2960 static void print_args(struct print_arg *args)
2962 int print_paren = 1;
2964 switch (args->type) {
2965 case PRINT_NULL:
2966 printf("null");
2967 break;
2968 case PRINT_ATOM:
2969 printf("%s", args->atom.atom);
2970 break;
2971 case PRINT_FIELD:
2972 printf("REC->%s", args->field.name);
2973 break;
2974 case PRINT_FLAGS:
2975 printf("__print_flags(");
2976 print_args(args->flags.field);
2977 printf(", %s, ", args->flags.delim);
2978 print_fields(args->flags.flags);
2979 printf(")");
2980 break;
2981 case PRINT_SYMBOL:
2982 printf("__print_symbolic(");
2983 print_args(args->symbol.field);
2984 printf(", ");
2985 print_fields(args->symbol.symbols);
2986 printf(")");
2987 break;
2988 case PRINT_STRING:
2989 printf("__get_str(%s)", args->string.string);
2990 break;
2991 case PRINT_TYPE:
2992 printf("(%s)", args->typecast.type);
2993 print_args(args->typecast.item);
2994 break;
2995 case PRINT_OP:
2996 if (strcmp(args->op.op, ":") == 0)
2997 print_paren = 0;
2998 if (print_paren)
2999 printf("(");
3000 print_args(args->op.left);
3001 printf(" %s ", args->op.op);
3002 print_args(args->op.right);
3003 if (print_paren)
3004 printf(")");
3005 break;
3006 default:
3007 /* we should warn... */
3008 return;
3010 if (args->next) {
3011 printf("\n");
3012 print_args(args->next);
3016 int parse_ftrace_file(char *buf, unsigned long size)
3018 struct format_field *field;
3019 struct print_arg *arg, **list;
3020 struct event *event;
3021 int ret;
3023 init_input_buf(buf, size);
3025 event = alloc_event();
3026 if (!event)
3027 return -ENOMEM;
3029 event->flags |= EVENT_FL_ISFTRACE;
3031 event->name = event_read_name();
3032 if (!event->name)
3033 die("failed to read ftrace event name");
3035 if (strcmp(event->name, "function") == 0)
3036 event->flags |= EVENT_FL_ISFUNC;
3038 else if (strcmp(event->name, "funcgraph_entry") == 0)
3039 event->flags |= EVENT_FL_ISFUNCENT;
3041 else if (strcmp(event->name, "funcgraph_exit") == 0)
3042 event->flags |= EVENT_FL_ISFUNCRET;
3044 else if (strcmp(event->name, "bprint") == 0)
3045 event->flags |= EVENT_FL_ISBPRINT;
3047 event->id = event_read_id();
3048 if (event->id < 0)
3049 die("failed to read ftrace event id");
3051 add_event(event);
3053 ret = event_read_format(event);
3054 if (ret < 0)
3055 die("failed to read ftrace event format");
3057 ret = event_read_print(event);
3058 if (ret < 0)
3059 die("failed to read ftrace event print fmt");
3061 /* New ftrace handles args */
3062 if (ret > 0)
3063 return 0;
3065 * The arguments for ftrace files are parsed by the fields.
3066 * Set up the fields as their arguments.
3068 list = &event->print_fmt.args;
3069 for (field = event->format.fields; field; field = field->next) {
3070 arg = malloc_or_die(sizeof(*arg));
3071 memset(arg, 0, sizeof(*arg));
3072 *list = arg;
3073 list = &arg->next;
3074 arg->type = PRINT_FIELD;
3075 arg->field.name = field->name;
3076 arg->field.field = field;
3078 return 0;
3081 int parse_event_file(char *buf, unsigned long size, char *sys)
3083 struct event *event;
3084 int ret;
3086 init_input_buf(buf, size);
3088 event = alloc_event();
3089 if (!event)
3090 return -ENOMEM;
3092 event->name = event_read_name();
3093 if (!event->name)
3094 die("failed to read event name");
3096 event->id = event_read_id();
3097 if (event->id < 0)
3098 die("failed to read event id");
3100 ret = event_read_format(event);
3101 if (ret < 0) {
3102 warning("failed to read event format for %s", event->name);
3103 goto event_failed;
3106 ret = event_read_print(event);
3107 if (ret < 0) {
3108 warning("failed to read event print fmt for %s", event->name);
3109 goto event_failed;
3112 event->system = strdup(sys);
3114 #define PRINT_ARGS 0
3115 if (PRINT_ARGS && event->print_fmt.args)
3116 print_args(event->print_fmt.args);
3118 add_event(event);
3119 return 0;
3121 event_failed:
3122 event->flags |= EVENT_FL_FAILED;
3123 /* still add it even if it failed */
3124 add_event(event);
3125 return -1;
3128 void parse_set_info(int nr_cpus, int long_sz)
3130 cpus = nr_cpus;
3131 long_size = long_sz;
3134 int common_pc(struct scripting_context *context)
3136 return parse_common_pc(context->event_data);
3139 int common_flags(struct scripting_context *context)
3141 return parse_common_flags(context->event_data);
3144 int common_lock_depth(struct scripting_context *context)
3146 return parse_common_lock_depth(context->event_data);