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