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