perf/scripts: Move common code out of Perl-specific files
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / trace-event-parse.c
blob9b3c20f42f98a2ab0b4157ca8f03d0f0477c7b75
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_data_offset;
41 int header_page_data_size;
43 int latency_format;
45 static char *input_buf;
46 static unsigned long long input_buf_ptr;
47 static unsigned long long input_buf_siz;
49 static int cpus;
50 static int long_size;
51 static int is_flag_field;
52 static int is_symbolic_field;
54 static struct format_field *
55 find_any_field(struct event *event, const char *name);
57 static void init_input_buf(char *buf, unsigned long long size)
59 input_buf = buf;
60 input_buf_siz = size;
61 input_buf_ptr = 0;
64 struct cmdline {
65 char *comm;
66 int pid;
69 static struct cmdline *cmdlines;
70 static int cmdline_count;
72 static int cmdline_cmp(const void *a, const void *b)
74 const struct cmdline *ca = a;
75 const struct cmdline *cb = b;
77 if (ca->pid < cb->pid)
78 return -1;
79 if (ca->pid > cb->pid)
80 return 1;
82 return 0;
85 void parse_cmdlines(char *file, int size __unused)
87 struct cmdline_list {
88 struct cmdline_list *next;
89 char *comm;
90 int pid;
91 } *list = NULL, *item;
92 char *line;
93 char *next = NULL;
94 int i;
96 line = strtok_r(file, "\n", &next);
97 while (line) {
98 item = malloc_or_die(sizeof(*item));
99 sscanf(line, "%d %as", &item->pid,
100 (float *)(void *)&item->comm); /* workaround gcc warning */
101 item->next = list;
102 list = item;
103 line = strtok_r(NULL, "\n", &next);
104 cmdline_count++;
107 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
109 i = 0;
110 while (list) {
111 cmdlines[i].pid = list->pid;
112 cmdlines[i].comm = list->comm;
113 i++;
114 item = list;
115 list = list->next;
116 free(item);
119 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
122 static struct func_map {
123 unsigned long long addr;
124 char *func;
125 char *mod;
126 } *func_list;
127 static unsigned int func_count;
129 static int func_cmp(const void *a, const void *b)
131 const struct func_map *fa = a;
132 const struct func_map *fb = b;
134 if (fa->addr < fb->addr)
135 return -1;
136 if (fa->addr > fb->addr)
137 return 1;
139 return 0;
142 void parse_proc_kallsyms(char *file, unsigned int size __unused)
144 struct func_list {
145 struct func_list *next;
146 unsigned long long addr;
147 char *func;
148 char *mod;
149 } *list = NULL, *item;
150 char *line;
151 char *next = NULL;
152 char *addr_str;
153 char ch;
154 int ret;
155 int i;
157 line = strtok_r(file, "\n", &next);
158 while (line) {
159 item = malloc_or_die(sizeof(*item));
160 item->mod = NULL;
161 ret = sscanf(line, "%as %c %as\t[%as",
162 (float *)(void *)&addr_str, /* workaround gcc warning */
163 &ch,
164 (float *)(void *)&item->func,
165 (float *)(void *)&item->mod);
166 item->addr = strtoull(addr_str, NULL, 16);
167 free(addr_str);
169 /* truncate the extra ']' */
170 if (item->mod)
171 item->mod[strlen(item->mod) - 1] = 0;
174 item->next = list;
175 list = item;
176 line = strtok_r(NULL, "\n", &next);
177 func_count++;
180 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
182 i = 0;
183 while (list) {
184 func_list[i].func = list->func;
185 func_list[i].addr = list->addr;
186 func_list[i].mod = list->mod;
187 i++;
188 item = list;
189 list = list->next;
190 free(item);
193 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
196 * Add a special record at the end.
198 func_list[func_count].func = NULL;
199 func_list[func_count].addr = 0;
200 func_list[func_count].mod = NULL;
204 * We are searching for a record in between, not an exact
205 * match.
207 static int func_bcmp(const void *a, const void *b)
209 const struct func_map *fa = a;
210 const struct func_map *fb = b;
212 if ((fa->addr == fb->addr) ||
214 (fa->addr > fb->addr &&
215 fa->addr < (fb+1)->addr))
216 return 0;
218 if (fa->addr < fb->addr)
219 return -1;
221 return 1;
224 static struct func_map *find_func(unsigned long long addr)
226 struct func_map *func;
227 struct func_map key;
229 key.addr = addr;
231 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
232 func_bcmp);
234 return func;
237 void print_funcs(void)
239 int i;
241 for (i = 0; i < (int)func_count; i++) {
242 printf("%016llx %s",
243 func_list[i].addr,
244 func_list[i].func);
245 if (func_list[i].mod)
246 printf(" [%s]\n", func_list[i].mod);
247 else
248 printf("\n");
252 static struct printk_map {
253 unsigned long long addr;
254 char *printk;
255 } *printk_list;
256 static unsigned int printk_count;
258 static int printk_cmp(const void *a, const void *b)
260 const struct func_map *fa = a;
261 const struct func_map *fb = b;
263 if (fa->addr < fb->addr)
264 return -1;
265 if (fa->addr > fb->addr)
266 return 1;
268 return 0;
271 static struct printk_map *find_printk(unsigned long long addr)
273 struct printk_map *printk;
274 struct printk_map key;
276 key.addr = addr;
278 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
279 printk_cmp);
281 return printk;
284 void parse_ftrace_printk(char *file, unsigned int size __unused)
286 struct printk_list {
287 struct printk_list *next;
288 unsigned long long addr;
289 char *printk;
290 } *list = NULL, *item;
291 char *line;
292 char *next = NULL;
293 char *addr_str;
294 int i;
296 line = strtok_r(file, "\n", &next);
297 while (line) {
298 addr_str = strsep(&line, ":");
299 if (!line) {
300 warning("error parsing print strings");
301 break;
303 item = malloc_or_die(sizeof(*item));
304 item->addr = strtoull(addr_str, NULL, 16);
305 /* fmt still has a space, skip it */
306 item->printk = strdup(line+1);
307 item->next = list;
308 list = item;
309 line = strtok_r(NULL, "\n", &next);
310 printk_count++;
313 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
315 i = 0;
316 while (list) {
317 printk_list[i].printk = list->printk;
318 printk_list[i].addr = list->addr;
319 i++;
320 item = list;
321 list = list->next;
322 free(item);
325 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
328 void print_printk(void)
330 int i;
332 for (i = 0; i < (int)printk_count; i++) {
333 printf("%016llx %s\n",
334 printk_list[i].addr,
335 printk_list[i].printk);
339 static struct event *alloc_event(void)
341 struct event *event;
343 event = malloc_or_die(sizeof(*event));
344 memset(event, 0, sizeof(*event));
346 return event;
349 enum event_type {
350 EVENT_ERROR,
351 EVENT_NONE,
352 EVENT_SPACE,
353 EVENT_NEWLINE,
354 EVENT_OP,
355 EVENT_DELIM,
356 EVENT_ITEM,
357 EVENT_DQUOTE,
358 EVENT_SQUOTE,
361 static struct event *event_list;
363 static void add_event(struct event *event)
365 event->next = event_list;
366 event_list = event;
369 static int event_item_type(enum event_type type)
371 switch (type) {
372 case EVENT_ITEM ... EVENT_SQUOTE:
373 return 1;
374 case EVENT_ERROR ... EVENT_DELIM:
375 default:
376 return 0;
380 static void free_arg(struct print_arg *arg)
382 if (!arg)
383 return;
385 switch (arg->type) {
386 case PRINT_ATOM:
387 if (arg->atom.atom)
388 free(arg->atom.atom);
389 break;
390 case PRINT_NULL:
391 case PRINT_FIELD ... PRINT_OP:
392 default:
393 /* todo */
394 break;
397 free(arg);
400 static enum event_type get_type(int ch)
402 if (ch == '\n')
403 return EVENT_NEWLINE;
404 if (isspace(ch))
405 return EVENT_SPACE;
406 if (isalnum(ch) || ch == '_')
407 return EVENT_ITEM;
408 if (ch == '\'')
409 return EVENT_SQUOTE;
410 if (ch == '"')
411 return EVENT_DQUOTE;
412 if (!isprint(ch))
413 return EVENT_NONE;
414 if (ch == '(' || ch == ')' || ch == ',')
415 return EVENT_DELIM;
417 return EVENT_OP;
420 static int __read_char(void)
422 if (input_buf_ptr >= input_buf_siz)
423 return -1;
425 return input_buf[input_buf_ptr++];
428 static int __peek_char(void)
430 if (input_buf_ptr >= input_buf_siz)
431 return -1;
433 return input_buf[input_buf_ptr];
436 static enum event_type __read_token(char **tok)
438 char buf[BUFSIZ];
439 int ch, last_ch, quote_ch, next_ch;
440 int i = 0;
441 int tok_size = 0;
442 enum event_type type;
444 *tok = NULL;
447 ch = __read_char();
448 if (ch < 0)
449 return EVENT_NONE;
451 type = get_type(ch);
452 if (type == EVENT_NONE)
453 return type;
455 buf[i++] = ch;
457 switch (type) {
458 case EVENT_NEWLINE:
459 case EVENT_DELIM:
460 *tok = malloc_or_die(2);
461 (*tok)[0] = ch;
462 (*tok)[1] = 0;
463 return type;
465 case EVENT_OP:
466 switch (ch) {
467 case '-':
468 next_ch = __peek_char();
469 if (next_ch == '>') {
470 buf[i++] = __read_char();
471 break;
473 /* fall through */
474 case '+':
475 case '|':
476 case '&':
477 case '>':
478 case '<':
479 last_ch = ch;
480 ch = __peek_char();
481 if (ch != last_ch)
482 goto test_equal;
483 buf[i++] = __read_char();
484 switch (last_ch) {
485 case '>':
486 case '<':
487 goto test_equal;
488 default:
489 break;
491 break;
492 case '!':
493 case '=':
494 goto test_equal;
495 default: /* what should we do instead? */
496 break;
498 buf[i] = 0;
499 *tok = strdup(buf);
500 return type;
502 test_equal:
503 ch = __peek_char();
504 if (ch == '=')
505 buf[i++] = __read_char();
506 break;
508 case EVENT_DQUOTE:
509 case EVENT_SQUOTE:
510 /* don't keep quotes */
511 i--;
512 quote_ch = ch;
513 last_ch = 0;
514 do {
515 if (i == (BUFSIZ - 1)) {
516 buf[i] = 0;
517 if (*tok) {
518 *tok = realloc(*tok, tok_size + BUFSIZ);
519 if (!*tok)
520 return EVENT_NONE;
521 strcat(*tok, buf);
522 } else
523 *tok = strdup(buf);
525 if (!*tok)
526 return EVENT_NONE;
527 tok_size += BUFSIZ;
528 i = 0;
530 last_ch = ch;
531 ch = __read_char();
532 buf[i++] = ch;
533 /* the '\' '\' will cancel itself */
534 if (ch == '\\' && last_ch == '\\')
535 last_ch = 0;
536 } while (ch != quote_ch || last_ch == '\\');
537 /* remove the last quote */
538 i--;
539 goto out;
541 case EVENT_ERROR ... EVENT_SPACE:
542 case EVENT_ITEM:
543 default:
544 break;
547 while (get_type(__peek_char()) == type) {
548 if (i == (BUFSIZ - 1)) {
549 buf[i] = 0;
550 if (*tok) {
551 *tok = realloc(*tok, tok_size + BUFSIZ);
552 if (!*tok)
553 return EVENT_NONE;
554 strcat(*tok, buf);
555 } else
556 *tok = strdup(buf);
558 if (!*tok)
559 return EVENT_NONE;
560 tok_size += BUFSIZ;
561 i = 0;
563 ch = __read_char();
564 buf[i++] = ch;
567 out:
568 buf[i] = 0;
569 if (*tok) {
570 *tok = realloc(*tok, tok_size + i);
571 if (!*tok)
572 return EVENT_NONE;
573 strcat(*tok, buf);
574 } else
575 *tok = strdup(buf);
576 if (!*tok)
577 return EVENT_NONE;
579 return type;
582 static void free_token(char *tok)
584 if (tok)
585 free(tok);
588 static enum event_type read_token(char **tok)
590 enum event_type type;
592 for (;;) {
593 type = __read_token(tok);
594 if (type != EVENT_SPACE)
595 return type;
597 free_token(*tok);
600 /* not reached */
601 return EVENT_NONE;
604 /* no newline */
605 static enum event_type read_token_item(char **tok)
607 enum event_type type;
609 for (;;) {
610 type = __read_token(tok);
611 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
612 return type;
614 free_token(*tok);
617 /* not reached */
618 return EVENT_NONE;
621 static int test_type(enum event_type type, enum event_type expect)
623 if (type != expect) {
624 warning("Error: expected type %d but read %d",
625 expect, type);
626 return -1;
628 return 0;
631 static int test_type_token(enum event_type type, char *token,
632 enum event_type expect, const char *expect_tok)
634 if (type != expect) {
635 warning("Error: expected type %d but read %d",
636 expect, type);
637 return -1;
640 if (strcmp(token, expect_tok) != 0) {
641 warning("Error: expected '%s' but read '%s'",
642 expect_tok, token);
643 return -1;
645 return 0;
648 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
650 enum event_type type;
652 if (newline_ok)
653 type = read_token(tok);
654 else
655 type = read_token_item(tok);
656 return test_type(type, expect);
659 static int read_expect_type(enum event_type expect, char **tok)
661 return __read_expect_type(expect, tok, 1);
664 static int __read_expected(enum event_type expect, const char *str, int newline_ok)
666 enum event_type type;
667 char *token;
668 int ret;
670 if (newline_ok)
671 type = read_token(&token);
672 else
673 type = read_token_item(&token);
675 ret = test_type_token(type, token, expect, str);
677 free_token(token);
679 return ret;
682 static int read_expected(enum event_type expect, const char *str)
684 return __read_expected(expect, str, 1);
687 static int read_expected_item(enum event_type expect, const char *str)
689 return __read_expected(expect, str, 0);
692 static char *event_read_name(void)
694 char *token;
696 if (read_expected(EVENT_ITEM, "name") < 0)
697 return NULL;
699 if (read_expected(EVENT_OP, ":") < 0)
700 return NULL;
702 if (read_expect_type(EVENT_ITEM, &token) < 0)
703 goto fail;
705 return token;
707 fail:
708 free_token(token);
709 return NULL;
712 static int event_read_id(void)
714 char *token;
715 int id;
717 if (read_expected_item(EVENT_ITEM, "ID") < 0)
718 return -1;
720 if (read_expected(EVENT_OP, ":") < 0)
721 return -1;
723 if (read_expect_type(EVENT_ITEM, &token) < 0)
724 goto fail;
726 id = strtoul(token, NULL, 0);
727 free_token(token);
728 return id;
730 fail:
731 free_token(token);
732 return -1;
735 static int field_is_string(struct format_field *field)
737 if ((field->flags & FIELD_IS_ARRAY) &&
738 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
739 !strstr(field->type, "s8")))
740 return 1;
742 return 0;
745 static int field_is_dynamic(struct format_field *field)
747 if (!strcmp(field->type, "__data_loc"))
748 return 1;
750 return 0;
753 static int event_read_fields(struct event *event, struct format_field **fields)
755 struct format_field *field = NULL;
756 enum event_type type;
757 char *token;
758 char *last_token;
759 int count = 0;
761 do {
762 type = read_token(&token);
763 if (type == EVENT_NEWLINE) {
764 free_token(token);
765 return count;
768 count++;
770 if (test_type_token(type, token, EVENT_ITEM, "field"))
771 goto fail;
772 free_token(token);
774 type = read_token(&token);
776 * The ftrace fields may still use the "special" name.
777 * Just ignore it.
779 if (event->flags & EVENT_FL_ISFTRACE &&
780 type == EVENT_ITEM && strcmp(token, "special") == 0) {
781 free_token(token);
782 type = read_token(&token);
785 if (test_type_token(type, token, EVENT_OP, ":") < 0)
786 return -1;
788 if (read_expect_type(EVENT_ITEM, &token) < 0)
789 goto fail;
791 last_token = token;
793 field = malloc_or_die(sizeof(*field));
794 memset(field, 0, sizeof(*field));
796 /* read the rest of the type */
797 for (;;) {
798 type = read_token(&token);
799 if (type == EVENT_ITEM ||
800 (type == EVENT_OP && strcmp(token, "*") == 0) ||
802 * Some of the ftrace fields are broken and have
803 * an illegal "." in them.
805 (event->flags & EVENT_FL_ISFTRACE &&
806 type == EVENT_OP && strcmp(token, ".") == 0)) {
808 if (strcmp(token, "*") == 0)
809 field->flags |= FIELD_IS_POINTER;
811 if (field->type) {
812 field->type = realloc(field->type,
813 strlen(field->type) +
814 strlen(last_token) + 2);
815 strcat(field->type, " ");
816 strcat(field->type, last_token);
817 } else
818 field->type = last_token;
819 last_token = token;
820 continue;
823 break;
826 if (!field->type) {
827 die("no type found");
828 goto fail;
830 field->name = last_token;
832 if (test_type(type, EVENT_OP))
833 goto fail;
835 if (strcmp(token, "[") == 0) {
836 enum event_type last_type = type;
837 char *brackets = token;
838 int len;
840 field->flags |= FIELD_IS_ARRAY;
842 type = read_token(&token);
843 while (strcmp(token, "]") != 0) {
844 if (last_type == EVENT_ITEM &&
845 type == EVENT_ITEM)
846 len = 2;
847 else
848 len = 1;
849 last_type = type;
851 brackets = realloc(brackets,
852 strlen(brackets) +
853 strlen(token) + len);
854 if (len == 2)
855 strcat(brackets, " ");
856 strcat(brackets, token);
857 free_token(token);
858 type = read_token(&token);
859 if (type == EVENT_NONE) {
860 die("failed to find token");
861 goto fail;
865 free_token(token);
867 brackets = realloc(brackets, strlen(brackets) + 2);
868 strcat(brackets, "]");
870 /* add brackets to type */
872 type = read_token(&token);
874 * If the next token is not an OP, then it is of
875 * the format: type [] item;
877 if (type == EVENT_ITEM) {
878 field->type = realloc(field->type,
879 strlen(field->type) +
880 strlen(field->name) +
881 strlen(brackets) + 2);
882 strcat(field->type, " ");
883 strcat(field->type, field->name);
884 free_token(field->name);
885 strcat(field->type, brackets);
886 field->name = token;
887 type = read_token(&token);
888 } else {
889 field->type = realloc(field->type,
890 strlen(field->type) +
891 strlen(brackets) + 1);
892 strcat(field->type, brackets);
894 free(brackets);
897 if (field_is_string(field)) {
898 field->flags |= FIELD_IS_STRING;
899 if (field_is_dynamic(field))
900 field->flags |= FIELD_IS_DYNAMIC;
903 if (test_type_token(type, token, EVENT_OP, ";"))
904 goto fail;
905 free_token(token);
907 if (read_expected(EVENT_ITEM, "offset") < 0)
908 goto fail_expect;
910 if (read_expected(EVENT_OP, ":") < 0)
911 goto fail_expect;
913 if (read_expect_type(EVENT_ITEM, &token))
914 goto fail;
915 field->offset = strtoul(token, NULL, 0);
916 free_token(token);
918 if (read_expected(EVENT_OP, ";") < 0)
919 goto fail_expect;
921 if (read_expected(EVENT_ITEM, "size") < 0)
922 goto fail_expect;
924 if (read_expected(EVENT_OP, ":") < 0)
925 goto fail_expect;
927 if (read_expect_type(EVENT_ITEM, &token))
928 goto fail;
929 field->size = strtoul(token, NULL, 0);
930 free_token(token);
932 if (read_expected(EVENT_OP, ";") < 0)
933 goto fail_expect;
935 type = read_token(&token);
936 if (type != EVENT_NEWLINE) {
937 /* newer versions of the kernel have a "signed" type */
938 if (test_type_token(type, token, EVENT_ITEM, "signed"))
939 goto fail;
941 free_token(token);
943 if (read_expected(EVENT_OP, ":") < 0)
944 goto fail_expect;
946 if (read_expect_type(EVENT_ITEM, &token))
947 goto fail;
949 if (strtoul(token, NULL, 0))
950 field->flags |= FIELD_IS_SIGNED;
952 free_token(token);
953 if (read_expected(EVENT_OP, ";") < 0)
954 goto fail_expect;
956 if (read_expect_type(EVENT_NEWLINE, &token))
957 goto fail;
960 free_token(token);
962 *fields = field;
963 fields = &field->next;
965 } while (1);
967 return 0;
969 fail:
970 free_token(token);
971 fail_expect:
972 if (field)
973 free(field);
974 return -1;
977 static int event_read_format(struct event *event)
979 char *token;
980 int ret;
982 if (read_expected_item(EVENT_ITEM, "format") < 0)
983 return -1;
985 if (read_expected(EVENT_OP, ":") < 0)
986 return -1;
988 if (read_expect_type(EVENT_NEWLINE, &token))
989 goto fail;
990 free_token(token);
992 ret = event_read_fields(event, &event->format.common_fields);
993 if (ret < 0)
994 return ret;
995 event->format.nr_common = ret;
997 ret = event_read_fields(event, &event->format.fields);
998 if (ret < 0)
999 return ret;
1000 event->format.nr_fields = ret;
1002 return 0;
1004 fail:
1005 free_token(token);
1006 return -1;
1009 enum event_type
1010 process_arg_token(struct event *event, struct print_arg *arg,
1011 char **tok, enum event_type type);
1013 static enum event_type
1014 process_arg(struct event *event, struct print_arg *arg, char **tok)
1016 enum event_type type;
1017 char *token;
1019 type = read_token(&token);
1020 *tok = token;
1022 return process_arg_token(event, arg, tok, type);
1025 static enum event_type
1026 process_cond(struct event *event, struct print_arg *top, char **tok)
1028 struct print_arg *arg, *left, *right;
1029 enum event_type type;
1030 char *token = NULL;
1032 arg = malloc_or_die(sizeof(*arg));
1033 memset(arg, 0, sizeof(*arg));
1035 left = malloc_or_die(sizeof(*left));
1037 right = malloc_or_die(sizeof(*right));
1039 arg->type = PRINT_OP;
1040 arg->op.left = left;
1041 arg->op.right = right;
1043 *tok = NULL;
1044 type = process_arg(event, left, &token);
1045 if (test_type_token(type, token, EVENT_OP, ":"))
1046 goto out_free;
1048 arg->op.op = token;
1050 type = process_arg(event, right, &token);
1052 top->op.right = arg;
1054 *tok = token;
1055 return type;
1057 out_free:
1058 free_token(*tok);
1059 free(right);
1060 free(left);
1061 free_arg(arg);
1062 return EVENT_ERROR;
1065 static enum event_type
1066 process_array(struct event *event, struct print_arg *top, char **tok)
1068 struct print_arg *arg;
1069 enum event_type type;
1070 char *token = NULL;
1072 arg = malloc_or_die(sizeof(*arg));
1073 memset(arg, 0, sizeof(*arg));
1075 *tok = NULL;
1076 type = process_arg(event, arg, &token);
1077 if (test_type_token(type, token, EVENT_OP, "]"))
1078 goto out_free;
1080 top->op.right = arg;
1082 free_token(token);
1083 type = read_token_item(&token);
1084 *tok = token;
1086 return type;
1088 out_free:
1089 free_token(*tok);
1090 free_arg(arg);
1091 return EVENT_ERROR;
1094 static int get_op_prio(char *op)
1096 if (!op[1]) {
1097 switch (op[0]) {
1098 case '*':
1099 case '/':
1100 case '%':
1101 return 6;
1102 case '+':
1103 case '-':
1104 return 7;
1105 /* '>>' and '<<' are 8 */
1106 case '<':
1107 case '>':
1108 return 9;
1109 /* '==' and '!=' are 10 */
1110 case '&':
1111 return 11;
1112 case '^':
1113 return 12;
1114 case '|':
1115 return 13;
1116 case '?':
1117 return 16;
1118 default:
1119 die("unknown op '%c'", op[0]);
1120 return -1;
1122 } else {
1123 if (strcmp(op, "++") == 0 ||
1124 strcmp(op, "--") == 0) {
1125 return 3;
1126 } else if (strcmp(op, ">>") == 0 ||
1127 strcmp(op, "<<") == 0) {
1128 return 8;
1129 } else if (strcmp(op, ">=") == 0 ||
1130 strcmp(op, "<=") == 0) {
1131 return 9;
1132 } else if (strcmp(op, "==") == 0 ||
1133 strcmp(op, "!=") == 0) {
1134 return 10;
1135 } else if (strcmp(op, "&&") == 0) {
1136 return 14;
1137 } else if (strcmp(op, "||") == 0) {
1138 return 15;
1139 } else {
1140 die("unknown op '%s'", op);
1141 return -1;
1146 static void set_op_prio(struct print_arg *arg)
1149 /* single ops are the greatest */
1150 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1151 arg->op.prio = 0;
1152 return;
1155 arg->op.prio = get_op_prio(arg->op.op);
1158 static enum event_type
1159 process_op(struct event *event, struct print_arg *arg, char **tok)
1161 struct print_arg *left, *right = NULL;
1162 enum event_type type;
1163 char *token;
1165 /* the op is passed in via tok */
1166 token = *tok;
1168 if (arg->type == PRINT_OP && !arg->op.left) {
1169 /* handle single op */
1170 if (token[1]) {
1171 die("bad op token %s", token);
1172 return EVENT_ERROR;
1174 switch (token[0]) {
1175 case '!':
1176 case '+':
1177 case '-':
1178 break;
1179 default:
1180 die("bad op token %s", token);
1181 return EVENT_ERROR;
1184 /* make an empty left */
1185 left = malloc_or_die(sizeof(*left));
1186 left->type = PRINT_NULL;
1187 arg->op.left = left;
1189 right = malloc_or_die(sizeof(*right));
1190 arg->op.right = right;
1192 type = process_arg(event, right, tok);
1194 } else if (strcmp(token, "?") == 0) {
1196 left = malloc_or_die(sizeof(*left));
1197 /* copy the top arg to the left */
1198 *left = *arg;
1200 arg->type = PRINT_OP;
1201 arg->op.op = token;
1202 arg->op.left = left;
1203 arg->op.prio = 0;
1205 type = process_cond(event, arg, tok);
1207 } else if (strcmp(token, ">>") == 0 ||
1208 strcmp(token, "<<") == 0 ||
1209 strcmp(token, "&") == 0 ||
1210 strcmp(token, "|") == 0 ||
1211 strcmp(token, "&&") == 0 ||
1212 strcmp(token, "||") == 0 ||
1213 strcmp(token, "-") == 0 ||
1214 strcmp(token, "+") == 0 ||
1215 strcmp(token, "*") == 0 ||
1216 strcmp(token, "^") == 0 ||
1217 strcmp(token, "/") == 0 ||
1218 strcmp(token, "<") == 0 ||
1219 strcmp(token, ">") == 0 ||
1220 strcmp(token, "==") == 0 ||
1221 strcmp(token, "!=") == 0) {
1223 left = malloc_or_die(sizeof(*left));
1225 /* copy the top arg to the left */
1226 *left = *arg;
1228 arg->type = PRINT_OP;
1229 arg->op.op = token;
1230 arg->op.left = left;
1232 set_op_prio(arg);
1234 right = malloc_or_die(sizeof(*right));
1236 type = read_token_item(&token);
1237 *tok = token;
1239 /* could just be a type pointer */
1240 if ((strcmp(arg->op.op, "*") == 0) &&
1241 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1242 if (left->type != PRINT_ATOM)
1243 die("bad pointer type");
1244 left->atom.atom = realloc(left->atom.atom,
1245 sizeof(left->atom.atom) + 3);
1246 strcat(left->atom.atom, " *");
1247 *arg = *left;
1248 free(arg);
1250 return type;
1253 type = process_arg_token(event, right, tok, type);
1255 arg->op.right = right;
1257 } else if (strcmp(token, "[") == 0) {
1259 left = malloc_or_die(sizeof(*left));
1260 *left = *arg;
1262 arg->type = PRINT_OP;
1263 arg->op.op = token;
1264 arg->op.left = left;
1266 arg->op.prio = 0;
1267 type = process_array(event, arg, tok);
1269 } else {
1270 warning("unknown op '%s'", token);
1271 event->flags |= EVENT_FL_FAILED;
1272 /* the arg is now the left side */
1273 return EVENT_NONE;
1276 if (type == EVENT_OP) {
1277 int prio;
1279 /* higher prios need to be closer to the root */
1280 prio = get_op_prio(*tok);
1282 if (prio > arg->op.prio)
1283 return process_op(event, arg, tok);
1285 return process_op(event, right, tok);
1288 return type;
1291 static enum event_type
1292 process_entry(struct event *event __unused, struct print_arg *arg,
1293 char **tok)
1295 enum event_type type;
1296 char *field;
1297 char *token;
1299 if (read_expected(EVENT_OP, "->") < 0)
1300 return EVENT_ERROR;
1302 if (read_expect_type(EVENT_ITEM, &token) < 0)
1303 goto fail;
1304 field = token;
1306 arg->type = PRINT_FIELD;
1307 arg->field.name = field;
1309 if (is_flag_field) {
1310 arg->field.field = find_any_field(event, arg->field.name);
1311 arg->field.field->flags |= FIELD_IS_FLAG;
1312 is_flag_field = 0;
1313 } else if (is_symbolic_field) {
1314 arg->field.field = find_any_field(event, arg->field.name);
1315 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1316 is_symbolic_field = 0;
1319 type = read_token(&token);
1320 *tok = token;
1322 return type;
1324 fail:
1325 free_token(token);
1326 return EVENT_ERROR;
1329 static char *arg_eval (struct print_arg *arg);
1331 static long long arg_num_eval(struct print_arg *arg)
1333 long long left, right;
1334 long long val = 0;
1336 switch (arg->type) {
1337 case PRINT_ATOM:
1338 val = strtoll(arg->atom.atom, NULL, 0);
1339 break;
1340 case PRINT_TYPE:
1341 val = arg_num_eval(arg->typecast.item);
1342 break;
1343 case PRINT_OP:
1344 switch (arg->op.op[0]) {
1345 case '|':
1346 left = arg_num_eval(arg->op.left);
1347 right = arg_num_eval(arg->op.right);
1348 if (arg->op.op[1])
1349 val = left || right;
1350 else
1351 val = left | right;
1352 break;
1353 case '&':
1354 left = arg_num_eval(arg->op.left);
1355 right = arg_num_eval(arg->op.right);
1356 if (arg->op.op[1])
1357 val = left && right;
1358 else
1359 val = left & right;
1360 break;
1361 case '<':
1362 left = arg_num_eval(arg->op.left);
1363 right = arg_num_eval(arg->op.right);
1364 switch (arg->op.op[1]) {
1365 case 0:
1366 val = left < right;
1367 break;
1368 case '<':
1369 val = left << right;
1370 break;
1371 case '=':
1372 val = left <= right;
1373 break;
1374 default:
1375 die("unknown op '%s'", arg->op.op);
1377 break;
1378 case '>':
1379 left = arg_num_eval(arg->op.left);
1380 right = arg_num_eval(arg->op.right);
1381 switch (arg->op.op[1]) {
1382 case 0:
1383 val = left > right;
1384 break;
1385 case '>':
1386 val = left >> right;
1387 break;
1388 case '=':
1389 val = left >= right;
1390 break;
1391 default:
1392 die("unknown op '%s'", arg->op.op);
1394 break;
1395 case '=':
1396 left = arg_num_eval(arg->op.left);
1397 right = arg_num_eval(arg->op.right);
1399 if (arg->op.op[1] != '=')
1400 die("unknown op '%s'", arg->op.op);
1402 val = left == right;
1403 break;
1404 case '!':
1405 left = arg_num_eval(arg->op.left);
1406 right = arg_num_eval(arg->op.right);
1408 switch (arg->op.op[1]) {
1409 case '=':
1410 val = left != right;
1411 break;
1412 default:
1413 die("unknown op '%s'", arg->op.op);
1415 break;
1416 default:
1417 die("unknown op '%s'", arg->op.op);
1419 break;
1421 case PRINT_NULL:
1422 case PRINT_FIELD ... PRINT_SYMBOL:
1423 case PRINT_STRING:
1424 default:
1425 die("invalid eval type %d", arg->type);
1428 return val;
1431 static char *arg_eval (struct print_arg *arg)
1433 long long val;
1434 static char buf[20];
1436 switch (arg->type) {
1437 case PRINT_ATOM:
1438 return arg->atom.atom;
1439 case PRINT_TYPE:
1440 return arg_eval(arg->typecast.item);
1441 case PRINT_OP:
1442 val = arg_num_eval(arg);
1443 sprintf(buf, "%lld", val);
1444 return buf;
1446 case PRINT_NULL:
1447 case PRINT_FIELD ... PRINT_SYMBOL:
1448 case PRINT_STRING:
1449 default:
1450 die("invalid eval type %d", arg->type);
1451 break;
1454 return NULL;
1457 static enum event_type
1458 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1460 enum event_type type;
1461 struct print_arg *arg = NULL;
1462 struct print_flag_sym *field;
1463 char *token = NULL;
1464 char *value;
1466 do {
1467 free_token(token);
1468 type = read_token_item(&token);
1469 if (test_type_token(type, token, EVENT_OP, "{"))
1470 break;
1472 arg = malloc_or_die(sizeof(*arg));
1474 free_token(token);
1475 type = process_arg(event, arg, &token);
1476 if (test_type_token(type, token, EVENT_DELIM, ","))
1477 goto out_free;
1479 field = malloc_or_die(sizeof(*field));
1480 memset(field, 0, sizeof(*field));
1482 value = arg_eval(arg);
1483 field->value = strdup(value);
1485 free_token(token);
1486 type = process_arg(event, arg, &token);
1487 if (test_type_token(type, token, EVENT_OP, "}"))
1488 goto out_free;
1490 value = arg_eval(arg);
1491 field->str = strdup(value);
1492 free_arg(arg);
1493 arg = NULL;
1495 *list = field;
1496 list = &field->next;
1498 free_token(token);
1499 type = read_token_item(&token);
1500 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1502 *tok = token;
1503 return type;
1505 out_free:
1506 free_arg(arg);
1507 free_token(token);
1509 return EVENT_ERROR;
1512 static enum event_type
1513 process_flags(struct event *event, struct print_arg *arg, char **tok)
1515 struct print_arg *field;
1516 enum event_type type;
1517 char *token;
1519 memset(arg, 0, sizeof(*arg));
1520 arg->type = PRINT_FLAGS;
1522 if (read_expected_item(EVENT_DELIM, "(") < 0)
1523 return EVENT_ERROR;
1525 field = malloc_or_die(sizeof(*field));
1527 type = process_arg(event, field, &token);
1528 if (test_type_token(type, token, EVENT_DELIM, ","))
1529 goto out_free;
1531 arg->flags.field = field;
1533 type = read_token_item(&token);
1534 if (event_item_type(type)) {
1535 arg->flags.delim = token;
1536 type = read_token_item(&token);
1539 if (test_type_token(type, token, EVENT_DELIM, ","))
1540 goto out_free;
1542 type = process_fields(event, &arg->flags.flags, &token);
1543 if (test_type_token(type, token, EVENT_DELIM, ")"))
1544 goto out_free;
1546 free_token(token);
1547 type = read_token_item(tok);
1548 return type;
1550 out_free:
1551 free_token(token);
1552 return EVENT_ERROR;
1555 static enum event_type
1556 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1558 struct print_arg *field;
1559 enum event_type type;
1560 char *token;
1562 memset(arg, 0, sizeof(*arg));
1563 arg->type = PRINT_SYMBOL;
1565 if (read_expected_item(EVENT_DELIM, "(") < 0)
1566 return EVENT_ERROR;
1568 field = malloc_or_die(sizeof(*field));
1570 type = process_arg(event, field, &token);
1571 if (test_type_token(type, token, EVENT_DELIM, ","))
1572 goto out_free;
1574 arg->symbol.field = field;
1576 type = process_fields(event, &arg->symbol.symbols, &token);
1577 if (test_type_token(type, token, EVENT_DELIM, ")"))
1578 goto out_free;
1580 free_token(token);
1581 type = read_token_item(tok);
1582 return type;
1584 out_free:
1585 free_token(token);
1586 return EVENT_ERROR;
1589 static enum event_type
1590 process_paren(struct event *event, struct print_arg *arg, char **tok)
1592 struct print_arg *item_arg;
1593 enum event_type type;
1594 char *token;
1596 type = process_arg(event, arg, &token);
1598 if (type == EVENT_ERROR)
1599 return EVENT_ERROR;
1601 if (type == EVENT_OP)
1602 type = process_op(event, arg, &token);
1604 if (type == EVENT_ERROR)
1605 return EVENT_ERROR;
1607 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1608 free_token(token);
1609 return EVENT_ERROR;
1612 free_token(token);
1613 type = read_token_item(&token);
1616 * If the next token is an item or another open paren, then
1617 * this was a typecast.
1619 if (event_item_type(type) ||
1620 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1622 /* make this a typecast and contine */
1624 /* prevous must be an atom */
1625 if (arg->type != PRINT_ATOM)
1626 die("previous needed to be PRINT_ATOM");
1628 item_arg = malloc_or_die(sizeof(*item_arg));
1630 arg->type = PRINT_TYPE;
1631 arg->typecast.type = arg->atom.atom;
1632 arg->typecast.item = item_arg;
1633 type = process_arg_token(event, item_arg, &token, type);
1637 *tok = token;
1638 return type;
1642 static enum event_type
1643 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1645 enum event_type type;
1646 char *token;
1648 if (read_expected(EVENT_DELIM, "(") < 0)
1649 return EVENT_ERROR;
1651 if (read_expect_type(EVENT_ITEM, &token) < 0)
1652 goto fail;
1654 arg->type = PRINT_STRING;
1655 arg->string.string = token;
1656 arg->string.offset = -1;
1658 if (read_expected(EVENT_DELIM, ")") < 0)
1659 return EVENT_ERROR;
1661 type = read_token(&token);
1662 *tok = token;
1664 return type;
1665 fail:
1666 free_token(token);
1667 return EVENT_ERROR;
1670 enum event_type
1671 process_arg_token(struct event *event, struct print_arg *arg,
1672 char **tok, enum event_type type)
1674 char *token;
1675 char *atom;
1677 token = *tok;
1679 switch (type) {
1680 case EVENT_ITEM:
1681 if (strcmp(token, "REC") == 0) {
1682 free_token(token);
1683 type = process_entry(event, arg, &token);
1684 } else if (strcmp(token, "__print_flags") == 0) {
1685 free_token(token);
1686 is_flag_field = 1;
1687 type = process_flags(event, arg, &token);
1688 } else if (strcmp(token, "__print_symbolic") == 0) {
1689 free_token(token);
1690 is_symbolic_field = 1;
1691 type = process_symbols(event, arg, &token);
1692 } else if (strcmp(token, "__get_str") == 0) {
1693 free_token(token);
1694 type = process_str(event, arg, &token);
1695 } else {
1696 atom = token;
1697 /* test the next token */
1698 type = read_token_item(&token);
1700 /* atoms can be more than one token long */
1701 while (type == EVENT_ITEM) {
1702 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1703 strcat(atom, " ");
1704 strcat(atom, token);
1705 free_token(token);
1706 type = read_token_item(&token);
1709 /* todo, test for function */
1711 arg->type = PRINT_ATOM;
1712 arg->atom.atom = atom;
1714 break;
1715 case EVENT_DQUOTE:
1716 case EVENT_SQUOTE:
1717 arg->type = PRINT_ATOM;
1718 arg->atom.atom = token;
1719 type = read_token_item(&token);
1720 break;
1721 case EVENT_DELIM:
1722 if (strcmp(token, "(") == 0) {
1723 free_token(token);
1724 type = process_paren(event, arg, &token);
1725 break;
1727 case EVENT_OP:
1728 /* handle single ops */
1729 arg->type = PRINT_OP;
1730 arg->op.op = token;
1731 arg->op.left = NULL;
1732 type = process_op(event, arg, &token);
1734 break;
1736 case EVENT_ERROR ... EVENT_NEWLINE:
1737 default:
1738 die("unexpected type %d", type);
1740 *tok = token;
1742 return type;
1745 static int event_read_print_args(struct event *event, struct print_arg **list)
1747 enum event_type type = EVENT_ERROR;
1748 struct print_arg *arg;
1749 char *token;
1750 int args = 0;
1752 do {
1753 if (type == EVENT_NEWLINE) {
1754 free_token(token);
1755 type = read_token_item(&token);
1756 continue;
1759 arg = malloc_or_die(sizeof(*arg));
1760 memset(arg, 0, sizeof(*arg));
1762 type = process_arg(event, arg, &token);
1764 if (type == EVENT_ERROR) {
1765 free_arg(arg);
1766 return -1;
1769 *list = arg;
1770 args++;
1772 if (type == EVENT_OP) {
1773 type = process_op(event, arg, &token);
1774 list = &arg->next;
1775 continue;
1778 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1779 free_token(token);
1780 *list = arg;
1781 list = &arg->next;
1782 continue;
1784 break;
1785 } while (type != EVENT_NONE);
1787 if (type != EVENT_NONE)
1788 free_token(token);
1790 return args;
1793 static int event_read_print(struct event *event)
1795 enum event_type type;
1796 char *token;
1797 int ret;
1799 if (read_expected_item(EVENT_ITEM, "print") < 0)
1800 return -1;
1802 if (read_expected(EVENT_ITEM, "fmt") < 0)
1803 return -1;
1805 if (read_expected(EVENT_OP, ":") < 0)
1806 return -1;
1808 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1809 goto fail;
1811 concat:
1812 event->print_fmt.format = token;
1813 event->print_fmt.args = NULL;
1815 /* ok to have no arg */
1816 type = read_token_item(&token);
1818 if (type == EVENT_NONE)
1819 return 0;
1821 /* Handle concatination of print lines */
1822 if (type == EVENT_DQUOTE) {
1823 char *cat;
1825 cat = malloc_or_die(strlen(event->print_fmt.format) +
1826 strlen(token) + 1);
1827 strcpy(cat, event->print_fmt.format);
1828 strcat(cat, token);
1829 free_token(token);
1830 free_token(event->print_fmt.format);
1831 event->print_fmt.format = NULL;
1832 token = cat;
1833 goto concat;
1836 if (test_type_token(type, token, EVENT_DELIM, ","))
1837 goto fail;
1839 free_token(token);
1841 ret = event_read_print_args(event, &event->print_fmt.args);
1842 if (ret < 0)
1843 return -1;
1845 return ret;
1847 fail:
1848 free_token(token);
1849 return -1;
1852 static struct format_field *
1853 find_common_field(struct event *event, const char *name)
1855 struct format_field *format;
1857 for (format = event->format.common_fields;
1858 format; format = format->next) {
1859 if (strcmp(format->name, name) == 0)
1860 break;
1863 return format;
1866 static struct format_field *
1867 find_field(struct event *event, const char *name)
1869 struct format_field *format;
1871 for (format = event->format.fields;
1872 format; format = format->next) {
1873 if (strcmp(format->name, name) == 0)
1874 break;
1877 return format;
1880 static struct format_field *
1881 find_any_field(struct event *event, const char *name)
1883 struct format_field *format;
1885 format = find_common_field(event, name);
1886 if (format)
1887 return format;
1888 return find_field(event, name);
1891 unsigned long long read_size(void *ptr, int size)
1893 switch (size) {
1894 case 1:
1895 return *(unsigned char *)ptr;
1896 case 2:
1897 return data2host2(ptr);
1898 case 4:
1899 return data2host4(ptr);
1900 case 8:
1901 return data2host8(ptr);
1902 default:
1903 /* BUG! */
1904 return 0;
1908 unsigned long long
1909 raw_field_value(struct event *event, const char *name, void *data)
1911 struct format_field *field;
1913 field = find_any_field(event, name);
1914 if (!field)
1915 return 0ULL;
1917 return read_size(data + field->offset, field->size);
1920 void *raw_field_ptr(struct event *event, const char *name, void *data)
1922 struct format_field *field;
1924 field = find_any_field(event, name);
1925 if (!field)
1926 return NULL;
1928 if (field->flags & FIELD_IS_STRING) {
1929 int offset;
1931 offset = *(int *)(data + field->offset);
1932 offset &= 0xffff;
1934 return data + offset;
1937 return data + field->offset;
1940 static int get_common_info(const char *type, int *offset, int *size)
1942 struct event *event;
1943 struct format_field *field;
1946 * All events should have the same common elements.
1947 * Pick any event to find where the type is;
1949 if (!event_list)
1950 die("no event_list!");
1952 event = event_list;
1953 field = find_common_field(event, type);
1954 if (!field)
1955 die("field '%s' not found", type);
1957 *offset = field->offset;
1958 *size = field->size;
1960 return 0;
1963 static int __parse_common(void *data, int *size, int *offset,
1964 const char *name)
1966 int ret;
1968 if (!*size) {
1969 ret = get_common_info(name, offset, size);
1970 if (ret < 0)
1971 return ret;
1973 return read_size(data + *offset, *size);
1976 int trace_parse_common_type(void *data)
1978 static int type_offset;
1979 static int type_size;
1981 return __parse_common(data, &type_size, &type_offset,
1982 "common_type");
1985 int trace_parse_common_pid(void *data)
1987 static int pid_offset;
1988 static int pid_size;
1990 return __parse_common(data, &pid_size, &pid_offset,
1991 "common_pid");
1994 int parse_common_pc(void *data)
1996 static int pc_offset;
1997 static int pc_size;
1999 return __parse_common(data, &pc_size, &pc_offset,
2000 "common_preempt_count");
2003 int parse_common_flags(void *data)
2005 static int flags_offset;
2006 static int flags_size;
2008 return __parse_common(data, &flags_size, &flags_offset,
2009 "common_flags");
2012 int parse_common_lock_depth(void *data)
2014 static int ld_offset;
2015 static int ld_size;
2016 int ret;
2018 ret = __parse_common(data, &ld_size, &ld_offset,
2019 "common_lock_depth");
2020 if (ret < 0)
2021 return -1;
2023 return ret;
2026 struct event *trace_find_event(int id)
2028 struct event *event;
2030 for (event = event_list; event; event = event->next) {
2031 if (event->id == id)
2032 break;
2034 return event;
2037 struct event *trace_find_next_event(struct event *event)
2039 if (!event)
2040 return event_list;
2042 return event->next;
2045 static unsigned long long eval_num_arg(void *data, int size,
2046 struct event *event, struct print_arg *arg)
2048 unsigned long long val = 0;
2049 unsigned long long left, right;
2050 struct print_arg *larg;
2052 switch (arg->type) {
2053 case PRINT_NULL:
2054 /* ?? */
2055 return 0;
2056 case PRINT_ATOM:
2057 return strtoull(arg->atom.atom, NULL, 0);
2058 case PRINT_FIELD:
2059 if (!arg->field.field) {
2060 arg->field.field = find_any_field(event, arg->field.name);
2061 if (!arg->field.field)
2062 die("field %s not found", arg->field.name);
2064 /* must be a number */
2065 val = read_size(data + arg->field.field->offset,
2066 arg->field.field->size);
2067 break;
2068 case PRINT_FLAGS:
2069 case PRINT_SYMBOL:
2070 break;
2071 case PRINT_TYPE:
2072 return eval_num_arg(data, size, event, arg->typecast.item);
2073 case PRINT_STRING:
2074 return 0;
2075 break;
2076 case PRINT_OP:
2077 if (strcmp(arg->op.op, "[") == 0) {
2079 * Arrays are special, since we don't want
2080 * to read the arg as is.
2082 if (arg->op.left->type != PRINT_FIELD)
2083 goto default_op; /* oops, all bets off */
2084 larg = arg->op.left;
2085 if (!larg->field.field) {
2086 larg->field.field =
2087 find_any_field(event, larg->field.name);
2088 if (!larg->field.field)
2089 die("field %s not found", larg->field.name);
2091 right = eval_num_arg(data, size, event, arg->op.right);
2092 val = read_size(data + larg->field.field->offset +
2093 right * long_size, long_size);
2094 break;
2096 default_op:
2097 left = eval_num_arg(data, size, event, arg->op.left);
2098 right = eval_num_arg(data, size, event, arg->op.right);
2099 switch (arg->op.op[0]) {
2100 case '|':
2101 if (arg->op.op[1])
2102 val = left || right;
2103 else
2104 val = left | right;
2105 break;
2106 case '&':
2107 if (arg->op.op[1])
2108 val = left && right;
2109 else
2110 val = left & right;
2111 break;
2112 case '<':
2113 switch (arg->op.op[1]) {
2114 case 0:
2115 val = left < right;
2116 break;
2117 case '<':
2118 val = left << right;
2119 break;
2120 case '=':
2121 val = left <= right;
2122 break;
2123 default:
2124 die("unknown op '%s'", arg->op.op);
2126 break;
2127 case '>':
2128 switch (arg->op.op[1]) {
2129 case 0:
2130 val = left > right;
2131 break;
2132 case '>':
2133 val = left >> right;
2134 break;
2135 case '=':
2136 val = left >= right;
2137 break;
2138 default:
2139 die("unknown op '%s'", arg->op.op);
2141 break;
2142 case '=':
2143 if (arg->op.op[1] != '=')
2144 die("unknown op '%s'", arg->op.op);
2145 val = left == right;
2146 break;
2147 case '-':
2148 val = left - right;
2149 break;
2150 case '+':
2151 val = left + right;
2152 break;
2153 default:
2154 die("unknown op '%s'", arg->op.op);
2156 break;
2157 default: /* not sure what to do there */
2158 return 0;
2160 return val;
2163 struct flag {
2164 const char *name;
2165 unsigned long long value;
2168 static const struct flag flags[] = {
2169 { "HI_SOFTIRQ", 0 },
2170 { "TIMER_SOFTIRQ", 1 },
2171 { "NET_TX_SOFTIRQ", 2 },
2172 { "NET_RX_SOFTIRQ", 3 },
2173 { "BLOCK_SOFTIRQ", 4 },
2174 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2175 { "TASKLET_SOFTIRQ", 6 },
2176 { "SCHED_SOFTIRQ", 7 },
2177 { "HRTIMER_SOFTIRQ", 8 },
2178 { "RCU_SOFTIRQ", 9 },
2180 { "HRTIMER_NORESTART", 0 },
2181 { "HRTIMER_RESTART", 1 },
2184 unsigned long long eval_flag(const char *flag)
2186 int i;
2189 * Some flags in the format files do not get converted.
2190 * If the flag is not numeric, see if it is something that
2191 * we already know about.
2193 if (isdigit(flag[0]))
2194 return strtoull(flag, NULL, 0);
2196 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2197 if (strcmp(flags[i].name, flag) == 0)
2198 return flags[i].value;
2200 return 0;
2203 static void print_str_arg(void *data, int size,
2204 struct event *event, struct print_arg *arg)
2206 struct print_flag_sym *flag;
2207 unsigned long long val, fval;
2208 char *str;
2209 int print;
2211 switch (arg->type) {
2212 case PRINT_NULL:
2213 /* ?? */
2214 return;
2215 case PRINT_ATOM:
2216 printf("%s", arg->atom.atom);
2217 return;
2218 case PRINT_FIELD:
2219 if (!arg->field.field) {
2220 arg->field.field = find_any_field(event, arg->field.name);
2221 if (!arg->field.field)
2222 die("field %s not found", arg->field.name);
2224 str = malloc_or_die(arg->field.field->size + 1);
2225 memcpy(str, data + arg->field.field->offset,
2226 arg->field.field->size);
2227 str[arg->field.field->size] = 0;
2228 printf("%s", str);
2229 free(str);
2230 break;
2231 case PRINT_FLAGS:
2232 val = eval_num_arg(data, size, event, arg->flags.field);
2233 print = 0;
2234 for (flag = arg->flags.flags; flag; flag = flag->next) {
2235 fval = eval_flag(flag->value);
2236 if (!val && !fval) {
2237 printf("%s", flag->str);
2238 break;
2240 if (fval && (val & fval) == fval) {
2241 if (print && arg->flags.delim)
2242 printf("%s", arg->flags.delim);
2243 printf("%s", flag->str);
2244 print = 1;
2245 val &= ~fval;
2248 break;
2249 case PRINT_SYMBOL:
2250 val = eval_num_arg(data, size, event, arg->symbol.field);
2251 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2252 fval = eval_flag(flag->value);
2253 if (val == fval) {
2254 printf("%s", flag->str);
2255 break;
2258 break;
2260 case PRINT_TYPE:
2261 break;
2262 case PRINT_STRING: {
2263 int str_offset;
2265 if (arg->string.offset == -1) {
2266 struct format_field *f;
2268 f = find_any_field(event, arg->string.string);
2269 arg->string.offset = f->offset;
2271 str_offset = *(int *)(data + arg->string.offset);
2272 str_offset &= 0xffff;
2273 printf("%s", ((char *)data) + str_offset);
2274 break;
2276 case PRINT_OP:
2278 * The only op for string should be ? :
2280 if (arg->op.op[0] != '?')
2281 return;
2282 val = eval_num_arg(data, size, event, arg->op.left);
2283 if (val)
2284 print_str_arg(data, size, event, arg->op.right->op.left);
2285 else
2286 print_str_arg(data, size, event, arg->op.right->op.right);
2287 break;
2288 default:
2289 /* well... */
2290 break;
2294 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2296 static struct format_field *field, *ip_field;
2297 struct print_arg *args, *arg, **next;
2298 unsigned long long ip, val;
2299 char *ptr;
2300 void *bptr;
2302 if (!field) {
2303 field = find_field(event, "buf");
2304 if (!field)
2305 die("can't find buffer field for binary printk");
2306 ip_field = find_field(event, "ip");
2307 if (!ip_field)
2308 die("can't find ip field for binary printk");
2311 ip = read_size(data + ip_field->offset, ip_field->size);
2314 * The first arg is the IP pointer.
2316 args = malloc_or_die(sizeof(*args));
2317 arg = args;
2318 arg->next = NULL;
2319 next = &arg->next;
2321 arg->type = PRINT_ATOM;
2322 arg->atom.atom = malloc_or_die(32);
2323 sprintf(arg->atom.atom, "%lld", ip);
2325 /* skip the first "%pf : " */
2326 for (ptr = fmt + 6, bptr = data + field->offset;
2327 bptr < data + size && *ptr; ptr++) {
2328 int ls = 0;
2330 if (*ptr == '%') {
2331 process_again:
2332 ptr++;
2333 switch (*ptr) {
2334 case '%':
2335 break;
2336 case 'l':
2337 ls++;
2338 goto process_again;
2339 case 'L':
2340 ls = 2;
2341 goto process_again;
2342 case '0' ... '9':
2343 goto process_again;
2344 case 'p':
2345 ls = 1;
2346 /* fall through */
2347 case 'd':
2348 case 'u':
2349 case 'x':
2350 case 'i':
2351 /* the pointers are always 4 bytes aligned */
2352 bptr = (void *)(((unsigned long)bptr + 3) &
2353 ~3);
2354 switch (ls) {
2355 case 0:
2356 case 1:
2357 ls = long_size;
2358 break;
2359 case 2:
2360 ls = 8;
2361 default:
2362 break;
2364 val = read_size(bptr, ls);
2365 bptr += ls;
2366 arg = malloc_or_die(sizeof(*arg));
2367 arg->next = NULL;
2368 arg->type = PRINT_ATOM;
2369 arg->atom.atom = malloc_or_die(32);
2370 sprintf(arg->atom.atom, "%lld", val);
2371 *next = arg;
2372 next = &arg->next;
2373 break;
2374 case 's':
2375 arg = malloc_or_die(sizeof(*arg));
2376 arg->next = NULL;
2377 arg->type = PRINT_STRING;
2378 arg->string.string = strdup(bptr);
2379 bptr += strlen(bptr) + 1;
2380 *next = arg;
2381 next = &arg->next;
2382 default:
2383 break;
2388 return args;
2391 static void free_args(struct print_arg *args)
2393 struct print_arg *next;
2395 while (args) {
2396 next = args->next;
2398 if (args->type == PRINT_ATOM)
2399 free(args->atom.atom);
2400 else
2401 free(args->string.string);
2402 free(args);
2403 args = next;
2407 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2409 unsigned long long addr;
2410 static struct format_field *field;
2411 struct printk_map *printk;
2412 char *format;
2413 char *p;
2415 if (!field) {
2416 field = find_field(event, "fmt");
2417 if (!field)
2418 die("can't find format field for binary printk");
2419 printf("field->offset = %d size=%d\n", field->offset, field->size);
2422 addr = read_size(data + field->offset, field->size);
2424 printk = find_printk(addr);
2425 if (!printk) {
2426 format = malloc_or_die(45);
2427 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2428 addr);
2429 return format;
2432 p = printk->printk;
2433 /* Remove any quotes. */
2434 if (*p == '"')
2435 p++;
2436 format = malloc_or_die(strlen(p) + 10);
2437 sprintf(format, "%s : %s", "%pf", p);
2438 /* remove ending quotes and new line since we will add one too */
2439 p = format + strlen(format) - 1;
2440 if (*p == '"')
2441 *p = 0;
2443 p -= 2;
2444 if (strcmp(p, "\\n") == 0)
2445 *p = 0;
2447 return format;
2450 static void pretty_print(void *data, int size, struct event *event)
2452 struct print_fmt *print_fmt = &event->print_fmt;
2453 struct print_arg *arg = print_fmt->args;
2454 struct print_arg *args = NULL;
2455 const char *ptr = print_fmt->format;
2456 unsigned long long val;
2457 struct func_map *func;
2458 const char *saveptr;
2459 char *bprint_fmt = NULL;
2460 char format[32];
2461 int show_func;
2462 int len;
2463 int ls;
2465 if (event->flags & EVENT_FL_ISFUNC)
2466 ptr = " %pF <-- %pF";
2468 if (event->flags & EVENT_FL_ISBPRINT) {
2469 bprint_fmt = get_bprint_format(data, size, event);
2470 args = make_bprint_args(bprint_fmt, data, size, event);
2471 arg = args;
2472 ptr = bprint_fmt;
2475 for (; *ptr; ptr++) {
2476 ls = 0;
2477 if (*ptr == '\\') {
2478 ptr++;
2479 switch (*ptr) {
2480 case 'n':
2481 printf("\n");
2482 break;
2483 case 't':
2484 printf("\t");
2485 break;
2486 case 'r':
2487 printf("\r");
2488 break;
2489 case '\\':
2490 printf("\\");
2491 break;
2492 default:
2493 printf("%c", *ptr);
2494 break;
2497 } else if (*ptr == '%') {
2498 saveptr = ptr;
2499 show_func = 0;
2500 cont_process:
2501 ptr++;
2502 switch (*ptr) {
2503 case '%':
2504 printf("%%");
2505 break;
2506 case 'l':
2507 ls++;
2508 goto cont_process;
2509 case 'L':
2510 ls = 2;
2511 goto cont_process;
2512 case 'z':
2513 case 'Z':
2514 case '0' ... '9':
2515 goto cont_process;
2516 case 'p':
2517 if (long_size == 4)
2518 ls = 1;
2519 else
2520 ls = 2;
2522 if (*(ptr+1) == 'F' ||
2523 *(ptr+1) == 'f') {
2524 ptr++;
2525 show_func = *ptr;
2528 /* fall through */
2529 case 'd':
2530 case 'i':
2531 case 'x':
2532 case 'X':
2533 case 'u':
2534 if (!arg)
2535 die("no argument match");
2537 len = ((unsigned long)ptr + 1) -
2538 (unsigned long)saveptr;
2540 /* should never happen */
2541 if (len > 32)
2542 die("bad format!");
2544 memcpy(format, saveptr, len);
2545 format[len] = 0;
2547 val = eval_num_arg(data, size, event, arg);
2548 arg = arg->next;
2550 if (show_func) {
2551 func = find_func(val);
2552 if (func) {
2553 printf("%s", func->func);
2554 if (show_func == 'F')
2555 printf("+0x%llx",
2556 val - func->addr);
2557 break;
2560 switch (ls) {
2561 case 0:
2562 printf(format, (int)val);
2563 break;
2564 case 1:
2565 printf(format, (long)val);
2566 break;
2567 case 2:
2568 printf(format, (long long)val);
2569 break;
2570 default:
2571 die("bad count (%d)", ls);
2573 break;
2574 case 's':
2575 if (!arg)
2576 die("no matching argument");
2578 print_str_arg(data, size, event, arg);
2579 arg = arg->next;
2580 break;
2581 default:
2582 printf(">%c<", *ptr);
2585 } else
2586 printf("%c", *ptr);
2589 if (args) {
2590 free_args(args);
2591 free(bprint_fmt);
2595 static inline int log10_cpu(int nb)
2597 if (nb / 100)
2598 return 3;
2599 if (nb / 10)
2600 return 2;
2601 return 1;
2604 static void print_lat_fmt(void *data, int size __unused)
2606 unsigned int lat_flags;
2607 unsigned int pc;
2608 int lock_depth;
2609 int hardirq;
2610 int softirq;
2612 lat_flags = parse_common_flags(data);
2613 pc = parse_common_pc(data);
2614 lock_depth = parse_common_lock_depth(data);
2616 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2617 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2619 printf("%c%c%c",
2620 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2621 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2622 'X' : '.',
2623 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2624 'N' : '.',
2625 (hardirq && softirq) ? 'H' :
2626 hardirq ? 'h' : softirq ? 's' : '.');
2628 if (pc)
2629 printf("%x", pc);
2630 else
2631 printf(".");
2633 if (lock_depth < 0)
2634 printf(".");
2635 else
2636 printf("%d", lock_depth);
2639 /* taken from Linux, written by Frederic Weisbecker */
2640 static void print_graph_cpu(int cpu)
2642 int i;
2643 int log10_this = log10_cpu(cpu);
2644 int log10_all = log10_cpu(cpus);
2648 * Start with a space character - to make it stand out
2649 * to the right a bit when trace output is pasted into
2650 * email:
2652 printf(" ");
2655 * Tricky - we space the CPU field according to the max
2656 * number of online CPUs. On a 2-cpu system it would take
2657 * a maximum of 1 digit - on a 128 cpu system it would
2658 * take up to 3 digits:
2660 for (i = 0; i < log10_all - log10_this; i++)
2661 printf(" ");
2663 printf("%d) ", cpu);
2666 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2667 #define TRACE_GRAPH_INDENT 2
2669 static void print_graph_proc(int pid, const char *comm)
2671 /* sign + log10(MAX_INT) + '\0' */
2672 char pid_str[11];
2673 int spaces = 0;
2674 int len;
2675 int i;
2677 sprintf(pid_str, "%d", pid);
2679 /* 1 stands for the "-" character */
2680 len = strlen(comm) + strlen(pid_str) + 1;
2682 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2683 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2685 /* First spaces to align center */
2686 for (i = 0; i < spaces / 2; i++)
2687 printf(" ");
2689 printf("%s-%s", comm, pid_str);
2691 /* Last spaces to align center */
2692 for (i = 0; i < spaces - (spaces / 2); i++)
2693 printf(" ");
2696 static struct record *
2697 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2698 struct record *next)
2700 struct format_field *field;
2701 struct event *event;
2702 unsigned long val;
2703 int type;
2704 int pid;
2706 type = trace_parse_common_type(next->data);
2707 event = trace_find_event(type);
2708 if (!event)
2709 return NULL;
2711 if (!(event->flags & EVENT_FL_ISFUNCRET))
2712 return NULL;
2714 pid = trace_parse_common_pid(next->data);
2715 field = find_field(event, "func");
2716 if (!field)
2717 die("function return does not have field func");
2719 val = read_size(next->data + field->offset, field->size);
2721 if (cur_pid != pid || cur_func != val)
2722 return NULL;
2724 /* this is a leaf, now advance the iterator */
2725 return trace_read_data(cpu);
2728 /* Signal a overhead of time execution to the output */
2729 static void print_graph_overhead(unsigned long long duration)
2731 /* Non nested entry or return */
2732 if (duration == ~0ULL)
2733 return (void)printf(" ");
2735 /* Duration exceeded 100 msecs */
2736 if (duration > 100000ULL)
2737 return (void)printf("! ");
2739 /* Duration exceeded 10 msecs */
2740 if (duration > 10000ULL)
2741 return (void)printf("+ ");
2743 printf(" ");
2746 static void print_graph_duration(unsigned long long duration)
2748 unsigned long usecs = duration / 1000;
2749 unsigned long nsecs_rem = duration % 1000;
2750 /* log10(ULONG_MAX) + '\0' */
2751 char msecs_str[21];
2752 char nsecs_str[5];
2753 int len;
2754 int i;
2756 sprintf(msecs_str, "%lu", usecs);
2758 /* Print msecs */
2759 len = printf("%lu", usecs);
2761 /* Print nsecs (we don't want to exceed 7 numbers) */
2762 if (len < 7) {
2763 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2764 len += printf(".%s", nsecs_str);
2767 printf(" us ");
2769 /* Print remaining spaces to fit the row's width */
2770 for (i = len; i < 7; i++)
2771 printf(" ");
2773 printf("| ");
2776 static void
2777 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2779 unsigned long long rettime, calltime;
2780 unsigned long long duration, depth;
2781 unsigned long long val;
2782 struct format_field *field;
2783 struct func_map *func;
2784 struct event *ret_event;
2785 int type;
2786 int i;
2788 type = trace_parse_common_type(ret_rec->data);
2789 ret_event = trace_find_event(type);
2791 field = find_field(ret_event, "rettime");
2792 if (!field)
2793 die("can't find rettime in return graph");
2794 rettime = read_size(ret_rec->data + field->offset, field->size);
2796 field = find_field(ret_event, "calltime");
2797 if (!field)
2798 die("can't find rettime in return graph");
2799 calltime = read_size(ret_rec->data + field->offset, field->size);
2801 duration = rettime - calltime;
2803 /* Overhead */
2804 print_graph_overhead(duration);
2806 /* Duration */
2807 print_graph_duration(duration);
2809 field = find_field(event, "depth");
2810 if (!field)
2811 die("can't find depth in entry graph");
2812 depth = read_size(data + field->offset, field->size);
2814 /* Function */
2815 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2816 printf(" ");
2818 field = find_field(event, "func");
2819 if (!field)
2820 die("can't find func in entry graph");
2821 val = read_size(data + field->offset, field->size);
2822 func = find_func(val);
2824 if (func)
2825 printf("%s();", func->func);
2826 else
2827 printf("%llx();", val);
2830 static void print_graph_nested(struct event *event, void *data)
2832 struct format_field *field;
2833 unsigned long long depth;
2834 unsigned long long val;
2835 struct func_map *func;
2836 int i;
2838 /* No overhead */
2839 print_graph_overhead(-1);
2841 /* No time */
2842 printf(" | ");
2844 field = find_field(event, "depth");
2845 if (!field)
2846 die("can't find depth in entry graph");
2847 depth = read_size(data + field->offset, field->size);
2849 /* Function */
2850 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2851 printf(" ");
2853 field = find_field(event, "func");
2854 if (!field)
2855 die("can't find func in entry graph");
2856 val = read_size(data + field->offset, field->size);
2857 func = find_func(val);
2859 if (func)
2860 printf("%s() {", func->func);
2861 else
2862 printf("%llx() {", val);
2865 static void
2866 pretty_print_func_ent(void *data, int size, struct event *event,
2867 int cpu, int pid, const char *comm,
2868 unsigned long secs, unsigned long usecs)
2870 struct format_field *field;
2871 struct record *rec;
2872 void *copy_data;
2873 unsigned long val;
2875 printf("%5lu.%06lu | ", secs, usecs);
2877 print_graph_cpu(cpu);
2878 print_graph_proc(pid, comm);
2880 printf(" | ");
2882 if (latency_format) {
2883 print_lat_fmt(data, size);
2884 printf(" | ");
2887 field = find_field(event, "func");
2888 if (!field)
2889 die("function entry does not have func field");
2891 val = read_size(data + field->offset, field->size);
2894 * peek_data may unmap the data pointer. Copy it first.
2896 copy_data = malloc_or_die(size);
2897 memcpy(copy_data, data, size);
2898 data = copy_data;
2900 rec = trace_peek_data(cpu);
2901 if (rec) {
2902 rec = get_return_for_leaf(cpu, pid, val, rec);
2903 if (rec) {
2904 print_graph_entry_leaf(event, data, rec);
2905 goto out_free;
2908 print_graph_nested(event, data);
2909 out_free:
2910 free(data);
2913 static void
2914 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2915 int cpu, int pid, const char *comm,
2916 unsigned long secs, unsigned long usecs)
2918 unsigned long long rettime, calltime;
2919 unsigned long long duration, depth;
2920 struct format_field *field;
2921 int i;
2923 printf("%5lu.%06lu | ", secs, usecs);
2925 print_graph_cpu(cpu);
2926 print_graph_proc(pid, comm);
2928 printf(" | ");
2930 if (latency_format) {
2931 print_lat_fmt(data, size);
2932 printf(" | ");
2935 field = find_field(event, "rettime");
2936 if (!field)
2937 die("can't find rettime in return graph");
2938 rettime = read_size(data + field->offset, field->size);
2940 field = find_field(event, "calltime");
2941 if (!field)
2942 die("can't find calltime in return graph");
2943 calltime = read_size(data + field->offset, field->size);
2945 duration = rettime - calltime;
2947 /* Overhead */
2948 print_graph_overhead(duration);
2950 /* Duration */
2951 print_graph_duration(duration);
2953 field = find_field(event, "depth");
2954 if (!field)
2955 die("can't find depth in entry graph");
2956 depth = read_size(data + field->offset, field->size);
2958 /* Function */
2959 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2960 printf(" ");
2962 printf("}");
2965 static void
2966 pretty_print_func_graph(void *data, int size, struct event *event,
2967 int cpu, int pid, const char *comm,
2968 unsigned long secs, unsigned long usecs)
2970 if (event->flags & EVENT_FL_ISFUNCENT)
2971 pretty_print_func_ent(data, size, event,
2972 cpu, pid, comm, secs, usecs);
2973 else if (event->flags & EVENT_FL_ISFUNCRET)
2974 pretty_print_func_ret(data, size, event,
2975 cpu, pid, comm, secs, usecs);
2976 printf("\n");
2979 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2980 char *comm)
2982 struct event *event;
2983 unsigned long secs;
2984 unsigned long usecs;
2985 int type;
2986 int pid;
2988 secs = nsecs / NSECS_PER_SEC;
2989 nsecs -= secs * NSECS_PER_SEC;
2990 usecs = nsecs / NSECS_PER_USEC;
2992 type = trace_parse_common_type(data);
2994 event = trace_find_event(type);
2995 if (!event) {
2996 warning("ug! no event found for type %d", type);
2997 return;
3000 pid = trace_parse_common_pid(data);
3002 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
3003 return pretty_print_func_graph(data, size, event, cpu,
3004 pid, comm, secs, usecs);
3006 if (latency_format) {
3007 printf("%8.8s-%-5d %3d",
3008 comm, pid, cpu);
3009 print_lat_fmt(data, size);
3010 } else
3011 printf("%16s-%-5d [%03d]", comm, pid, cpu);
3013 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name);
3015 if (event->flags & EVENT_FL_FAILED) {
3016 printf("EVENT '%s' FAILED TO PARSE\n",
3017 event->name);
3018 return;
3021 pretty_print(data, size, event);
3022 printf("\n");
3025 static void print_fields(struct print_flag_sym *field)
3027 printf("{ %s, %s }", field->value, field->str);
3028 if (field->next) {
3029 printf(", ");
3030 print_fields(field->next);
3034 static void print_args(struct print_arg *args)
3036 int print_paren = 1;
3038 switch (args->type) {
3039 case PRINT_NULL:
3040 printf("null");
3041 break;
3042 case PRINT_ATOM:
3043 printf("%s", args->atom.atom);
3044 break;
3045 case PRINT_FIELD:
3046 printf("REC->%s", args->field.name);
3047 break;
3048 case PRINT_FLAGS:
3049 printf("__print_flags(");
3050 print_args(args->flags.field);
3051 printf(", %s, ", args->flags.delim);
3052 print_fields(args->flags.flags);
3053 printf(")");
3054 break;
3055 case PRINT_SYMBOL:
3056 printf("__print_symbolic(");
3057 print_args(args->symbol.field);
3058 printf(", ");
3059 print_fields(args->symbol.symbols);
3060 printf(")");
3061 break;
3062 case PRINT_STRING:
3063 printf("__get_str(%s)", args->string.string);
3064 break;
3065 case PRINT_TYPE:
3066 printf("(%s)", args->typecast.type);
3067 print_args(args->typecast.item);
3068 break;
3069 case PRINT_OP:
3070 if (strcmp(args->op.op, ":") == 0)
3071 print_paren = 0;
3072 if (print_paren)
3073 printf("(");
3074 print_args(args->op.left);
3075 printf(" %s ", args->op.op);
3076 print_args(args->op.right);
3077 if (print_paren)
3078 printf(")");
3079 break;
3080 default:
3081 /* we should warn... */
3082 return;
3084 if (args->next) {
3085 printf("\n");
3086 print_args(args->next);
3090 static void parse_header_field(const char *field,
3091 int *offset, int *size)
3093 char *token;
3094 int type;
3096 if (read_expected(EVENT_ITEM, "field") < 0)
3097 return;
3098 if (read_expected(EVENT_OP, ":") < 0)
3099 return;
3101 /* type */
3102 if (read_expect_type(EVENT_ITEM, &token) < 0)
3103 goto fail;
3104 free_token(token);
3106 if (read_expected(EVENT_ITEM, field) < 0)
3107 return;
3108 if (read_expected(EVENT_OP, ";") < 0)
3109 return;
3110 if (read_expected(EVENT_ITEM, "offset") < 0)
3111 return;
3112 if (read_expected(EVENT_OP, ":") < 0)
3113 return;
3114 if (read_expect_type(EVENT_ITEM, &token) < 0)
3115 goto fail;
3116 *offset = atoi(token);
3117 free_token(token);
3118 if (read_expected(EVENT_OP, ";") < 0)
3119 return;
3120 if (read_expected(EVENT_ITEM, "size") < 0)
3121 return;
3122 if (read_expected(EVENT_OP, ":") < 0)
3123 return;
3124 if (read_expect_type(EVENT_ITEM, &token) < 0)
3125 goto fail;
3126 *size = atoi(token);
3127 free_token(token);
3128 if (read_expected(EVENT_OP, ";") < 0)
3129 return;
3130 type = read_token(&token);
3131 if (type != EVENT_NEWLINE) {
3132 /* newer versions of the kernel have a "signed" type */
3133 if (type != EVENT_ITEM)
3134 goto fail;
3136 if (strcmp(token, "signed") != 0)
3137 goto fail;
3139 free_token(token);
3141 if (read_expected(EVENT_OP, ":") < 0)
3142 return;
3144 if (read_expect_type(EVENT_ITEM, &token))
3145 goto fail;
3147 free_token(token);
3148 if (read_expected(EVENT_OP, ";") < 0)
3149 return;
3151 if (read_expect_type(EVENT_NEWLINE, &token))
3152 goto fail;
3154 fail:
3155 free_token(token);
3158 int parse_header_page(char *buf, unsigned long size)
3160 init_input_buf(buf, size);
3162 parse_header_field("timestamp", &header_page_ts_offset,
3163 &header_page_ts_size);
3164 parse_header_field("commit", &header_page_size_offset,
3165 &header_page_size_size);
3166 parse_header_field("data", &header_page_data_offset,
3167 &header_page_data_size);
3169 return 0;
3172 int parse_ftrace_file(char *buf, unsigned long size)
3174 struct format_field *field;
3175 struct print_arg *arg, **list;
3176 struct event *event;
3177 int ret;
3179 init_input_buf(buf, size);
3181 event = alloc_event();
3182 if (!event)
3183 return -ENOMEM;
3185 event->flags |= EVENT_FL_ISFTRACE;
3187 event->name = event_read_name();
3188 if (!event->name)
3189 die("failed to read ftrace event name");
3191 if (strcmp(event->name, "function") == 0)
3192 event->flags |= EVENT_FL_ISFUNC;
3194 else if (strcmp(event->name, "funcgraph_entry") == 0)
3195 event->flags |= EVENT_FL_ISFUNCENT;
3197 else if (strcmp(event->name, "funcgraph_exit") == 0)
3198 event->flags |= EVENT_FL_ISFUNCRET;
3200 else if (strcmp(event->name, "bprint") == 0)
3201 event->flags |= EVENT_FL_ISBPRINT;
3203 event->id = event_read_id();
3204 if (event->id < 0)
3205 die("failed to read ftrace event id");
3207 add_event(event);
3209 ret = event_read_format(event);
3210 if (ret < 0)
3211 die("failed to read ftrace event format");
3213 ret = event_read_print(event);
3214 if (ret < 0)
3215 die("failed to read ftrace event print fmt");
3217 /* New ftrace handles args */
3218 if (ret > 0)
3219 return 0;
3221 * The arguments for ftrace files are parsed by the fields.
3222 * Set up the fields as their arguments.
3224 list = &event->print_fmt.args;
3225 for (field = event->format.fields; field; field = field->next) {
3226 arg = malloc_or_die(sizeof(*arg));
3227 memset(arg, 0, sizeof(*arg));
3228 *list = arg;
3229 list = &arg->next;
3230 arg->type = PRINT_FIELD;
3231 arg->field.name = field->name;
3232 arg->field.field = field;
3234 return 0;
3237 int parse_event_file(char *buf, unsigned long size, char *sys)
3239 struct event *event;
3240 int ret;
3242 init_input_buf(buf, size);
3244 event = alloc_event();
3245 if (!event)
3246 return -ENOMEM;
3248 event->name = event_read_name();
3249 if (!event->name)
3250 die("failed to read event name");
3252 event->id = event_read_id();
3253 if (event->id < 0)
3254 die("failed to read event id");
3256 ret = event_read_format(event);
3257 if (ret < 0) {
3258 warning("failed to read event format for %s", event->name);
3259 goto event_failed;
3262 ret = event_read_print(event);
3263 if (ret < 0) {
3264 warning("failed to read event print fmt for %s", event->name);
3265 goto event_failed;
3268 event->system = strdup(sys);
3270 #define PRINT_ARGS 0
3271 if (PRINT_ARGS && event->print_fmt.args)
3272 print_args(event->print_fmt.args);
3274 add_event(event);
3275 return 0;
3277 event_failed:
3278 event->flags |= EVENT_FL_FAILED;
3279 /* still add it even if it failed */
3280 add_event(event);
3281 return -1;
3284 void parse_set_info(int nr_cpus, int long_sz)
3286 cpus = nr_cpus;
3287 long_size = long_sz;
3290 int common_pc(struct scripting_context *context)
3292 return parse_common_pc(context->event_data);
3295 int common_flags(struct scripting_context *context)
3297 return parse_common_flags(context->event_data);
3300 int common_lock_depth(struct scripting_context *context)
3302 return parse_common_lock_depth(context->event_data);