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