Merge branch 'perf/urgent' into perf/core
[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 /* add signed type */
945
946 free_token(token);
947 if (read_expected(EVENT_OP, ";") < 0)
948 goto fail_expect;
949
950 if (read_expect_type(EVENT_NEWLINE, &token))
951 goto fail;
952 }
953
954 free_token(token);
955
956 *fields = field;
957 fields = &field->next;
958
959 } while (1);
960
961 return 0;
962
963 fail:
964 free_token(token);
965 fail_expect:
966 if (field)
967 free(field);
968 return -1;
969 }
970
971 static int event_read_format(struct event *event)
972 {
973 char *token;
974 int ret;
975
976 if (read_expected_item(EVENT_ITEM, "format") < 0)
977 return -1;
978
979 if (read_expected(EVENT_OP, ":") < 0)
980 return -1;
981
982 if (read_expect_type(EVENT_NEWLINE, &token))
983 goto fail;
984 free_token(token);
985
986 ret = event_read_fields(event, &event->format.common_fields);
987 if (ret < 0)
988 return ret;
989 event->format.nr_common = ret;
990
991 ret = event_read_fields(event, &event->format.fields);
992 if (ret < 0)
993 return ret;
994 event->format.nr_fields = ret;
995
996 return 0;
997
998 fail:
999 free_token(token);
1000 return -1;
1001 }
1002
1003 enum event_type
1004 process_arg_token(struct event *event, struct print_arg *arg,
1005 char **tok, enum event_type type);
1006
1007 static enum event_type
1008 process_arg(struct event *event, struct print_arg *arg, char **tok)
1009 {
1010 enum event_type type;
1011 char *token;
1012
1013 type = read_token(&token);
1014 *tok = token;
1015
1016 return process_arg_token(event, arg, tok, type);
1017 }
1018
1019 static enum event_type
1020 process_cond(struct event *event, struct print_arg *top, char **tok)
1021 {
1022 struct print_arg *arg, *left, *right;
1023 enum event_type type;
1024 char *token = NULL;
1025
1026 arg = malloc_or_die(sizeof(*arg));
1027 memset(arg, 0, sizeof(*arg));
1028
1029 left = malloc_or_die(sizeof(*left));
1030
1031 right = malloc_or_die(sizeof(*right));
1032
1033 arg->type = PRINT_OP;
1034 arg->op.left = left;
1035 arg->op.right = right;
1036
1037 *tok = NULL;
1038 type = process_arg(event, left, &token);
1039 if (test_type_token(type, token, EVENT_OP, ":"))
1040 goto out_free;
1041
1042 arg->op.op = token;
1043
1044 type = process_arg(event, right, &token);
1045
1046 top->op.right = arg;
1047
1048 *tok = token;
1049 return type;
1050
1051 out_free:
1052 free_token(*tok);
1053 free(right);
1054 free(left);
1055 free_arg(arg);
1056 return EVENT_ERROR;
1057 }
1058
1059 static enum event_type
1060 process_array(struct event *event, struct print_arg *top, char **tok)
1061 {
1062 struct print_arg *arg;
1063 enum event_type type;
1064 char *token = NULL;
1065
1066 arg = malloc_or_die(sizeof(*arg));
1067 memset(arg, 0, sizeof(*arg));
1068
1069 *tok = NULL;
1070 type = process_arg(event, arg, &token);
1071 if (test_type_token(type, token, EVENT_OP, "]"))
1072 goto out_free;
1073
1074 top->op.right = arg;
1075
1076 free_token(token);
1077 type = read_token_item(&token);
1078 *tok = token;
1079
1080 return type;
1081
1082 out_free:
1083 free_token(*tok);
1084 free_arg(arg);
1085 return EVENT_ERROR;
1086 }
1087
1088 static int get_op_prio(char *op)
1089 {
1090 if (!op[1]) {
1091 switch (op[0]) {
1092 case '*':
1093 case '/':
1094 case '%':
1095 return 6;
1096 case '+':
1097 case '-':
1098 return 7;
1099 /* '>>' and '<<' are 8 */
1100 case '<':
1101 case '>':
1102 return 9;
1103 /* '==' and '!=' are 10 */
1104 case '&':
1105 return 11;
1106 case '^':
1107 return 12;
1108 case '|':
1109 return 13;
1110 case '?':
1111 return 16;
1112 default:
1113 die("unknown op '%c'", op[0]);
1114 return -1;
1115 }
1116 } else {
1117 if (strcmp(op, "++") == 0 ||
1118 strcmp(op, "--") == 0) {
1119 return 3;
1120 } else if (strcmp(op, ">>") == 0 ||
1121 strcmp(op, "<<") == 0) {
1122 return 8;
1123 } else if (strcmp(op, ">=") == 0 ||
1124 strcmp(op, "<=") == 0) {
1125 return 9;
1126 } else if (strcmp(op, "==") == 0 ||
1127 strcmp(op, "!=") == 0) {
1128 return 10;
1129 } else if (strcmp(op, "&&") == 0) {
1130 return 14;
1131 } else if (strcmp(op, "||") == 0) {
1132 return 15;
1133 } else {
1134 die("unknown op '%s'", op);
1135 return -1;
1136 }
1137 }
1138 }
1139
1140 static void set_op_prio(struct print_arg *arg)
1141 {
1142
1143 /* single ops are the greatest */
1144 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1145 arg->op.prio = 0;
1146 return;
1147 }
1148
1149 arg->op.prio = get_op_prio(arg->op.op);
1150 }
1151
1152 static enum event_type
1153 process_op(struct event *event, struct print_arg *arg, char **tok)
1154 {
1155 struct print_arg *left, *right = NULL;
1156 enum event_type type;
1157 char *token;
1158
1159 /* the op is passed in via tok */
1160 token = *tok;
1161
1162 if (arg->type == PRINT_OP && !arg->op.left) {
1163 /* handle single op */
1164 if (token[1]) {
1165 die("bad op token %s", token);
1166 return EVENT_ERROR;
1167 }
1168 switch (token[0]) {
1169 case '!':
1170 case '+':
1171 case '-':
1172 break;
1173 default:
1174 die("bad op token %s", token);
1175 return EVENT_ERROR;
1176 }
1177
1178 /* make an empty left */
1179 left = malloc_or_die(sizeof(*left));
1180 left->type = PRINT_NULL;
1181 arg->op.left = left;
1182
1183 right = malloc_or_die(sizeof(*right));
1184 arg->op.right = right;
1185
1186 type = process_arg(event, right, tok);
1187
1188 } else if (strcmp(token, "?") == 0) {
1189
1190 left = malloc_or_die(sizeof(*left));
1191 /* copy the top arg to the left */
1192 *left = *arg;
1193
1194 arg->type = PRINT_OP;
1195 arg->op.op = token;
1196 arg->op.left = left;
1197 arg->op.prio = 0;
1198
1199 type = process_cond(event, arg, tok);
1200
1201 } else if (strcmp(token, ">>") == 0 ||
1202 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
1217 left = malloc_or_die(sizeof(*left));
1218
1219 /* copy the top arg to the left */
1220 *left = *arg;
1221
1222 arg->type = PRINT_OP;
1223 arg->op.op = token;
1224 arg->op.left = left;
1225
1226 set_op_prio(arg);
1227
1228 right = malloc_or_die(sizeof(*right));
1229
1230 type = read_token_item(&token);
1231 *tok = token;
1232
1233 /* could just be a type pointer */
1234 if ((strcmp(arg->op.op, "*") == 0) &&
1235 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1236 if (left->type != PRINT_ATOM)
1237 die("bad pointer type");
1238 left->atom.atom = realloc(left->atom.atom,
1239 sizeof(left->atom.atom) + 3);
1240 strcat(left->atom.atom, " *");
1241 *arg = *left;
1242 free(arg);
1243
1244 return type;
1245 }
1246
1247 type = process_arg_token(event, right, tok, type);
1248
1249 arg->op.right = right;
1250
1251 } else if (strcmp(token, "[") == 0) {
1252
1253 left = malloc_or_die(sizeof(*left));
1254 *left = *arg;
1255
1256 arg->type = PRINT_OP;
1257 arg->op.op = token;
1258 arg->op.left = left;
1259
1260 arg->op.prio = 0;
1261 type = process_array(event, arg, tok);
1262
1263 } else {
1264 warning("unknown op '%s'", token);
1265 event->flags |= EVENT_FL_FAILED;
1266 /* the arg is now the left side */
1267 return EVENT_NONE;
1268 }
1269
1270 if (type == EVENT_OP) {
1271 int prio;
1272
1273 /* higher prios need to be closer to the root */
1274 prio = get_op_prio(*tok);
1275
1276 if (prio > arg->op.prio)
1277 return process_op(event, arg, tok);
1278
1279 return process_op(event, right, tok);
1280 }
1281
1282 return type;
1283 }
1284
1285 static enum event_type
1286 process_entry(struct event *event __unused, struct print_arg *arg,
1287 char **tok)
1288 {
1289 enum event_type type;
1290 char *field;
1291 char *token;
1292
1293 if (read_expected(EVENT_OP, "->") < 0)
1294 return EVENT_ERROR;
1295
1296 if (read_expect_type(EVENT_ITEM, &token) < 0)
1297 goto fail;
1298 field = token;
1299
1300 arg->type = PRINT_FIELD;
1301 arg->field.name = field;
1302
1303 type = read_token(&token);
1304 *tok = token;
1305
1306 return type;
1307
1308 fail:
1309 free_token(token);
1310 return EVENT_ERROR;
1311 }
1312
1313 static char *arg_eval (struct print_arg *arg);
1314
1315 static long long arg_num_eval(struct print_arg *arg)
1316 {
1317 long long left, right;
1318 long long val = 0;
1319
1320 switch (arg->type) {
1321 case PRINT_ATOM:
1322 val = strtoll(arg->atom.atom, NULL, 0);
1323 break;
1324 case PRINT_TYPE:
1325 val = arg_num_eval(arg->typecast.item);
1326 break;
1327 case PRINT_OP:
1328 switch (arg->op.op[0]) {
1329 case '|':
1330 left = arg_num_eval(arg->op.left);
1331 right = arg_num_eval(arg->op.right);
1332 if (arg->op.op[1])
1333 val = left || right;
1334 else
1335 val = left | right;
1336 break;
1337 case '&':
1338 left = arg_num_eval(arg->op.left);
1339 right = arg_num_eval(arg->op.right);
1340 if (arg->op.op[1])
1341 val = left && right;
1342 else
1343 val = left & right;
1344 break;
1345 case '<':
1346 left = arg_num_eval(arg->op.left);
1347 right = arg_num_eval(arg->op.right);
1348 switch (arg->op.op[1]) {
1349 case 0:
1350 val = left < right;
1351 break;
1352 case '<':
1353 val = left << right;
1354 break;
1355 case '=':
1356 val = left <= right;
1357 break;
1358 default:
1359 die("unknown op '%s'", arg->op.op);
1360 }
1361 break;
1362 case '>':
1363 left = arg_num_eval(arg->op.left);
1364 right = arg_num_eval(arg->op.right);
1365 switch (arg->op.op[1]) {
1366 case 0:
1367 val = left > right;
1368 break;
1369 case '>':
1370 val = left >> right;
1371 break;
1372 case '=':
1373 val = left >= right;
1374 break;
1375 default:
1376 die("unknown op '%s'", arg->op.op);
1377 }
1378 break;
1379 case '=':
1380 left = arg_num_eval(arg->op.left);
1381 right = arg_num_eval(arg->op.right);
1382
1383 if (arg->op.op[1] != '=')
1384 die("unknown op '%s'", arg->op.op);
1385
1386 val = left == right;
1387 break;
1388 case '!':
1389 left = arg_num_eval(arg->op.left);
1390 right = arg_num_eval(arg->op.right);
1391
1392 switch (arg->op.op[1]) {
1393 case '=':
1394 val = left != right;
1395 break;
1396 default:
1397 die("unknown op '%s'", arg->op.op);
1398 }
1399 break;
1400 default:
1401 die("unknown op '%s'", arg->op.op);
1402 }
1403 break;
1404
1405 case PRINT_NULL:
1406 case PRINT_FIELD ... PRINT_SYMBOL:
1407 case PRINT_STRING:
1408 default:
1409 die("invalid eval type %d", arg->type);
1410
1411 }
1412 return val;
1413 }
1414
1415 static char *arg_eval (struct print_arg *arg)
1416 {
1417 long long val;
1418 static char buf[20];
1419
1420 switch (arg->type) {
1421 case PRINT_ATOM:
1422 return arg->atom.atom;
1423 case PRINT_TYPE:
1424 return arg_eval(arg->typecast.item);
1425 case PRINT_OP:
1426 val = arg_num_eval(arg);
1427 sprintf(buf, "%lld", val);
1428 return buf;
1429
1430 case PRINT_NULL:
1431 case PRINT_FIELD ... PRINT_SYMBOL:
1432 case PRINT_STRING:
1433 default:
1434 die("invalid eval type %d", arg->type);
1435 break;
1436 }
1437
1438 return NULL;
1439 }
1440
1441 static enum event_type
1442 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1443 {
1444 enum event_type type;
1445 struct print_arg *arg = NULL;
1446 struct print_flag_sym *field;
1447 char *token = NULL;
1448 char *value;
1449
1450 do {
1451 free_token(token);
1452 type = read_token_item(&token);
1453 if (test_type_token(type, token, EVENT_OP, "{"))
1454 break;
1455
1456 arg = malloc_or_die(sizeof(*arg));
1457
1458 free_token(token);
1459 type = process_arg(event, arg, &token);
1460 if (test_type_token(type, token, EVENT_DELIM, ","))
1461 goto out_free;
1462
1463 field = malloc_or_die(sizeof(*field));
1464 memset(field, 0, sizeof(field));
1465
1466 value = arg_eval(arg);
1467 field->value = strdup(value);
1468
1469 free_token(token);
1470 type = process_arg(event, arg, &token);
1471 if (test_type_token(type, token, EVENT_OP, "}"))
1472 goto out_free;
1473
1474 value = arg_eval(arg);
1475 field->str = strdup(value);
1476 free_arg(arg);
1477 arg = NULL;
1478
1479 *list = field;
1480 list = &field->next;
1481
1482 free_token(token);
1483 type = read_token_item(&token);
1484 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1485
1486 *tok = token;
1487 return type;
1488
1489 out_free:
1490 free_arg(arg);
1491 free_token(token);
1492
1493 return EVENT_ERROR;
1494 }
1495
1496 static enum event_type
1497 process_flags(struct event *event, struct print_arg *arg, char **tok)
1498 {
1499 struct print_arg *field;
1500 enum event_type type;
1501 char *token;
1502
1503 memset(arg, 0, sizeof(*arg));
1504 arg->type = PRINT_FLAGS;
1505
1506 if (read_expected_item(EVENT_DELIM, "(") < 0)
1507 return EVENT_ERROR;
1508
1509 field = malloc_or_die(sizeof(*field));
1510
1511 type = process_arg(event, field, &token);
1512 if (test_type_token(type, token, EVENT_DELIM, ","))
1513 goto out_free;
1514
1515 arg->flags.field = field;
1516
1517 type = read_token_item(&token);
1518 if (event_item_type(type)) {
1519 arg->flags.delim = token;
1520 type = read_token_item(&token);
1521 }
1522
1523 if (test_type_token(type, token, EVENT_DELIM, ","))
1524 goto out_free;
1525
1526 type = process_fields(event, &arg->flags.flags, &token);
1527 if (test_type_token(type, token, EVENT_DELIM, ")"))
1528 goto out_free;
1529
1530 free_token(token);
1531 type = read_token_item(tok);
1532 return type;
1533
1534 out_free:
1535 free_token(token);
1536 return EVENT_ERROR;
1537 }
1538
1539 static enum event_type
1540 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1541 {
1542 struct print_arg *field;
1543 enum event_type type;
1544 char *token;
1545
1546 memset(arg, 0, sizeof(*arg));
1547 arg->type = PRINT_SYMBOL;
1548
1549 if (read_expected_item(EVENT_DELIM, "(") < 0)
1550 return EVENT_ERROR;
1551
1552 field = malloc_or_die(sizeof(*field));
1553
1554 type = process_arg(event, field, &token);
1555 if (test_type_token(type, token, EVENT_DELIM, ","))
1556 goto out_free;
1557
1558 arg->symbol.field = field;
1559
1560 type = process_fields(event, &arg->symbol.symbols, &token);
1561 if (test_type_token(type, token, EVENT_DELIM, ")"))
1562 goto out_free;
1563
1564 free_token(token);
1565 type = read_token_item(tok);
1566 return type;
1567
1568 out_free:
1569 free_token(token);
1570 return EVENT_ERROR;
1571 }
1572
1573 static enum event_type
1574 process_paren(struct event *event, struct print_arg *arg, char **tok)
1575 {
1576 struct print_arg *item_arg;
1577 enum event_type type;
1578 char *token;
1579
1580 type = process_arg(event, arg, &token);
1581
1582 if (type == EVENT_ERROR)
1583 return EVENT_ERROR;
1584
1585 if (type == EVENT_OP)
1586 type = process_op(event, arg, &token);
1587
1588 if (type == EVENT_ERROR)
1589 return EVENT_ERROR;
1590
1591 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1592 free_token(token);
1593 return EVENT_ERROR;
1594 }
1595
1596 free_token(token);
1597 type = read_token_item(&token);
1598
1599 /*
1600 * If the next token is an item or another open paren, then
1601 * this was a typecast.
1602 */
1603 if (event_item_type(type) ||
1604 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1605
1606 /* make this a typecast and contine */
1607
1608 /* prevous must be an atom */
1609 if (arg->type != PRINT_ATOM)
1610 die("previous needed to be PRINT_ATOM");
1611
1612 item_arg = malloc_or_die(sizeof(*item_arg));
1613
1614 arg->type = PRINT_TYPE;
1615 arg->typecast.type = arg->atom.atom;
1616 arg->typecast.item = item_arg;
1617 type = process_arg_token(event, item_arg, &token, type);
1618
1619 }
1620
1621 *tok = token;
1622 return type;
1623 }
1624
1625
1626 static enum event_type
1627 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1628 {
1629 enum event_type type;
1630 char *token;
1631
1632 if (read_expected(EVENT_DELIM, "(") < 0)
1633 return EVENT_ERROR;
1634
1635 if (read_expect_type(EVENT_ITEM, &token) < 0)
1636 goto fail;
1637
1638 arg->type = PRINT_STRING;
1639 arg->string.string = token;
1640 arg->string.offset = -1;
1641
1642 if (read_expected(EVENT_DELIM, ")") < 0)
1643 return EVENT_ERROR;
1644
1645 type = read_token(&token);
1646 *tok = token;
1647
1648 return type;
1649 fail:
1650 free_token(token);
1651 return EVENT_ERROR;
1652 }
1653
1654 enum event_type
1655 process_arg_token(struct event *event, struct print_arg *arg,
1656 char **tok, enum event_type type)
1657 {
1658 char *token;
1659 char *atom;
1660
1661 token = *tok;
1662
1663 switch (type) {
1664 case EVENT_ITEM:
1665 if (strcmp(token, "REC") == 0) {
1666 free_token(token);
1667 type = process_entry(event, arg, &token);
1668 } else if (strcmp(token, "__print_flags") == 0) {
1669 free_token(token);
1670 type = process_flags(event, arg, &token);
1671 } else if (strcmp(token, "__print_symbolic") == 0) {
1672 free_token(token);
1673 type = process_symbols(event, arg, &token);
1674 } else if (strcmp(token, "__get_str") == 0) {
1675 free_token(token);
1676 type = process_str(event, arg, &token);
1677 } else {
1678 atom = token;
1679 /* test the next token */
1680 type = read_token_item(&token);
1681
1682 /* atoms can be more than one token long */
1683 while (type == EVENT_ITEM) {
1684 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1685 strcat(atom, " ");
1686 strcat(atom, token);
1687 free_token(token);
1688 type = read_token_item(&token);
1689 }
1690
1691 /* todo, test for function */
1692
1693 arg->type = PRINT_ATOM;
1694 arg->atom.atom = atom;
1695 }
1696 break;
1697 case EVENT_DQUOTE:
1698 case EVENT_SQUOTE:
1699 arg->type = PRINT_ATOM;
1700 arg->atom.atom = token;
1701 type = read_token_item(&token);
1702 break;
1703 case EVENT_DELIM:
1704 if (strcmp(token, "(") == 0) {
1705 free_token(token);
1706 type = process_paren(event, arg, &token);
1707 break;
1708 }
1709 case EVENT_OP:
1710 /* handle single ops */
1711 arg->type = PRINT_OP;
1712 arg->op.op = token;
1713 arg->op.left = NULL;
1714 type = process_op(event, arg, &token);
1715
1716 break;
1717
1718 case EVENT_ERROR ... EVENT_NEWLINE:
1719 default:
1720 die("unexpected type %d", type);
1721 }
1722 *tok = token;
1723
1724 return type;
1725 }
1726
1727 static int event_read_print_args(struct event *event, struct print_arg **list)
1728 {
1729 enum event_type type = EVENT_ERROR;
1730 struct print_arg *arg;
1731 char *token;
1732 int args = 0;
1733
1734 do {
1735 if (type == EVENT_NEWLINE) {
1736 free_token(token);
1737 type = read_token_item(&token);
1738 continue;
1739 }
1740
1741 arg = malloc_or_die(sizeof(*arg));
1742 memset(arg, 0, sizeof(*arg));
1743
1744 type = process_arg(event, arg, &token);
1745
1746 if (type == EVENT_ERROR) {
1747 free_arg(arg);
1748 return -1;
1749 }
1750
1751 *list = arg;
1752 args++;
1753
1754 if (type == EVENT_OP) {
1755 type = process_op(event, arg, &token);
1756 list = &arg->next;
1757 continue;
1758 }
1759
1760 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1761 free_token(token);
1762 *list = arg;
1763 list = &arg->next;
1764 continue;
1765 }
1766 break;
1767 } while (type != EVENT_NONE);
1768
1769 if (type != EVENT_NONE)
1770 free_token(token);
1771
1772 return args;
1773 }
1774
1775 static int event_read_print(struct event *event)
1776 {
1777 enum event_type type;
1778 char *token;
1779 int ret;
1780
1781 if (read_expected_item(EVENT_ITEM, "print") < 0)
1782 return -1;
1783
1784 if (read_expected(EVENT_ITEM, "fmt") < 0)
1785 return -1;
1786
1787 if (read_expected(EVENT_OP, ":") < 0)
1788 return -1;
1789
1790 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1791 goto fail;
1792
1793 concat:
1794 event->print_fmt.format = token;
1795 event->print_fmt.args = NULL;
1796
1797 /* ok to have no arg */
1798 type = read_token_item(&token);
1799
1800 if (type == EVENT_NONE)
1801 return 0;
1802
1803 /* Handle concatination of print lines */
1804 if (type == EVENT_DQUOTE) {
1805 char *cat;
1806
1807 cat = malloc_or_die(strlen(event->print_fmt.format) +
1808 strlen(token) + 1);
1809 strcpy(cat, event->print_fmt.format);
1810 strcat(cat, token);
1811 free_token(token);
1812 free_token(event->print_fmt.format);
1813 event->print_fmt.format = NULL;
1814 token = cat;
1815 goto concat;
1816 }
1817
1818 if (test_type_token(type, token, EVENT_DELIM, ","))
1819 goto fail;
1820
1821 free_token(token);
1822
1823 ret = event_read_print_args(event, &event->print_fmt.args);
1824 if (ret < 0)
1825 return -1;
1826
1827 return ret;
1828
1829 fail:
1830 free_token(token);
1831 return -1;
1832 }
1833
1834 static struct format_field *
1835 find_common_field(struct event *event, const char *name)
1836 {
1837 struct format_field *format;
1838
1839 for (format = event->format.common_fields;
1840 format; format = format->next) {
1841 if (strcmp(format->name, name) == 0)
1842 break;
1843 }
1844
1845 return format;
1846 }
1847
1848 static struct format_field *
1849 find_field(struct event *event, const char *name)
1850 {
1851 struct format_field *format;
1852
1853 for (format = event->format.fields;
1854 format; format = format->next) {
1855 if (strcmp(format->name, name) == 0)
1856 break;
1857 }
1858
1859 return format;
1860 }
1861
1862 static struct format_field *
1863 find_any_field(struct event *event, const char *name)
1864 {
1865 struct format_field *format;
1866
1867 format = find_common_field(event, name);
1868 if (format)
1869 return format;
1870 return find_field(event, name);
1871 }
1872
1873 static unsigned long long read_size(void *ptr, int size)
1874 {
1875 switch (size) {
1876 case 1:
1877 return *(unsigned char *)ptr;
1878 case 2:
1879 return data2host2(ptr);
1880 case 4:
1881 return data2host4(ptr);
1882 case 8:
1883 return data2host8(ptr);
1884 default:
1885 /* BUG! */
1886 return 0;
1887 }
1888 }
1889
1890 unsigned long long
1891 raw_field_value(struct event *event, const char *name, void *data)
1892 {
1893 struct format_field *field;
1894
1895 field = find_any_field(event, name);
1896 if (!field)
1897 return 0ULL;
1898
1899 return read_size(data + field->offset, field->size);
1900 }
1901
1902 void *raw_field_ptr(struct event *event, const char *name, void *data)
1903 {
1904 struct format_field *field;
1905
1906 field = find_any_field(event, name);
1907 if (!field)
1908 return NULL;
1909
1910 return data + field->offset;
1911 }
1912
1913 static int get_common_info(const char *type, int *offset, int *size)
1914 {
1915 struct event *event;
1916 struct format_field *field;
1917
1918 /*
1919 * All events should have the same common elements.
1920 * Pick any event to find where the type is;
1921 */
1922 if (!event_list)
1923 die("no event_list!");
1924
1925 event = event_list;
1926 field = find_common_field(event, type);
1927 if (!field)
1928 die("field '%s' not found", type);
1929
1930 *offset = field->offset;
1931 *size = field->size;
1932
1933 return 0;
1934 }
1935
1936 static int __parse_common(void *data, int *size, int *offset,
1937 const char *name)
1938 {
1939 int ret;
1940
1941 if (!*size) {
1942 ret = get_common_info(name, offset, size);
1943 if (ret < 0)
1944 return ret;
1945 }
1946 return read_size(data + *offset, *size);
1947 }
1948
1949 int trace_parse_common_type(void *data)
1950 {
1951 static int type_offset;
1952 static int type_size;
1953
1954 return __parse_common(data, &type_size, &type_offset,
1955 "common_type");
1956 }
1957
1958 static int parse_common_pid(void *data)
1959 {
1960 static int pid_offset;
1961 static int pid_size;
1962
1963 return __parse_common(data, &pid_size, &pid_offset,
1964 "common_pid");
1965 }
1966
1967 static int parse_common_pc(void *data)
1968 {
1969 static int pc_offset;
1970 static int pc_size;
1971
1972 return __parse_common(data, &pc_size, &pc_offset,
1973 "common_preempt_count");
1974 }
1975
1976 static int parse_common_flags(void *data)
1977 {
1978 static int flags_offset;
1979 static int flags_size;
1980
1981 return __parse_common(data, &flags_size, &flags_offset,
1982 "common_flags");
1983 }
1984
1985 static int parse_common_lock_depth(void *data)
1986 {
1987 static int ld_offset;
1988 static int ld_size;
1989 int ret;
1990
1991 ret = __parse_common(data, &ld_size, &ld_offset,
1992 "common_lock_depth");
1993 if (ret < 0)
1994 return -1;
1995
1996 return ret;
1997 }
1998
1999 struct event *trace_find_event(int id)
2000 {
2001 struct event *event;
2002
2003 for (event = event_list; event; event = event->next) {
2004 if (event->id == id)
2005 break;
2006 }
2007 return event;
2008 }
2009
2010 static unsigned long long eval_num_arg(void *data, int size,
2011 struct event *event, struct print_arg *arg)
2012 {
2013 unsigned long long val = 0;
2014 unsigned long long left, right;
2015 struct print_arg *larg;
2016
2017 switch (arg->type) {
2018 case PRINT_NULL:
2019 /* ?? */
2020 return 0;
2021 case PRINT_ATOM:
2022 return strtoull(arg->atom.atom, NULL, 0);
2023 case PRINT_FIELD:
2024 if (!arg->field.field) {
2025 arg->field.field = find_any_field(event, arg->field.name);
2026 if (!arg->field.field)
2027 die("field %s not found", arg->field.name);
2028 }
2029 /* must be a number */
2030 val = read_size(data + arg->field.field->offset,
2031 arg->field.field->size);
2032 break;
2033 case PRINT_FLAGS:
2034 case PRINT_SYMBOL:
2035 break;
2036 case PRINT_TYPE:
2037 return eval_num_arg(data, size, event, arg->typecast.item);
2038 case PRINT_STRING:
2039 return 0;
2040 break;
2041 case PRINT_OP:
2042 if (strcmp(arg->op.op, "[") == 0) {
2043 /*
2044 * Arrays are special, since we don't want
2045 * to read the arg as is.
2046 */
2047 if (arg->op.left->type != PRINT_FIELD)
2048 goto default_op; /* oops, all bets off */
2049 larg = arg->op.left;
2050 if (!larg->field.field) {
2051 larg->field.field =
2052 find_any_field(event, larg->field.name);
2053 if (!larg->field.field)
2054 die("field %s not found", larg->field.name);
2055 }
2056 right = eval_num_arg(data, size, event, arg->op.right);
2057 val = read_size(data + larg->field.field->offset +
2058 right * long_size, long_size);
2059 break;
2060 }
2061 default_op:
2062 left = eval_num_arg(data, size, event, arg->op.left);
2063 right = eval_num_arg(data, size, event, arg->op.right);
2064 switch (arg->op.op[0]) {
2065 case '|':
2066 if (arg->op.op[1])
2067 val = left || right;
2068 else
2069 val = left | right;
2070 break;
2071 case '&':
2072 if (arg->op.op[1])
2073 val = left && right;
2074 else
2075 val = left & right;
2076 break;
2077 case '<':
2078 switch (arg->op.op[1]) {
2079 case 0:
2080 val = left < right;
2081 break;
2082 case '<':
2083 val = left << right;
2084 break;
2085 case '=':
2086 val = left <= right;
2087 break;
2088 default:
2089 die("unknown op '%s'", arg->op.op);
2090 }
2091 break;
2092 case '>':
2093 switch (arg->op.op[1]) {
2094 case 0:
2095 val = left > right;
2096 break;
2097 case '>':
2098 val = left >> right;
2099 break;
2100 case '=':
2101 val = left >= right;
2102 break;
2103 default:
2104 die("unknown op '%s'", arg->op.op);
2105 }
2106 break;
2107 case '=':
2108 if (arg->op.op[1] != '=')
2109 die("unknown op '%s'", arg->op.op);
2110 val = left == right;
2111 break;
2112 case '-':
2113 val = left - right;
2114 break;
2115 case '+':
2116 val = left + right;
2117 break;
2118 default:
2119 die("unknown op '%s'", arg->op.op);
2120 }
2121 break;
2122 default: /* not sure what to do there */
2123 return 0;
2124 }
2125 return val;
2126 }
2127
2128 struct flag {
2129 const char *name;
2130 unsigned long long value;
2131 };
2132
2133 static const struct flag flags[] = {
2134 { "HI_SOFTIRQ", 0 },
2135 { "TIMER_SOFTIRQ", 1 },
2136 { "NET_TX_SOFTIRQ", 2 },
2137 { "NET_RX_SOFTIRQ", 3 },
2138 { "BLOCK_SOFTIRQ", 4 },
2139 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2140 { "TASKLET_SOFTIRQ", 6 },
2141 { "SCHED_SOFTIRQ", 7 },
2142 { "HRTIMER_SOFTIRQ", 8 },
2143 { "RCU_SOFTIRQ", 9 },
2144
2145 { "HRTIMER_NORESTART", 0 },
2146 { "HRTIMER_RESTART", 1 },
2147 };
2148
2149 static unsigned long long eval_flag(const char *flag)
2150 {
2151 int i;
2152
2153 /*
2154 * Some flags in the format files do not get converted.
2155 * If the flag is not numeric, see if it is something that
2156 * we already know about.
2157 */
2158 if (isdigit(flag[0]))
2159 return strtoull(flag, NULL, 0);
2160
2161 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2162 if (strcmp(flags[i].name, flag) == 0)
2163 return flags[i].value;
2164
2165 return 0;
2166 }
2167
2168 static void print_str_arg(void *data, int size,
2169 struct event *event, struct print_arg *arg)
2170 {
2171 struct print_flag_sym *flag;
2172 unsigned long long val, fval;
2173 char *str;
2174 int print;
2175
2176 switch (arg->type) {
2177 case PRINT_NULL:
2178 /* ?? */
2179 return;
2180 case PRINT_ATOM:
2181 printf("%s", arg->atom.atom);
2182 return;
2183 case PRINT_FIELD:
2184 if (!arg->field.field) {
2185 arg->field.field = find_any_field(event, arg->field.name);
2186 if (!arg->field.field)
2187 die("field %s not found", arg->field.name);
2188 }
2189 str = malloc_or_die(arg->field.field->size + 1);
2190 memcpy(str, data + arg->field.field->offset,
2191 arg->field.field->size);
2192 str[arg->field.field->size] = 0;
2193 printf("%s", str);
2194 free(str);
2195 break;
2196 case PRINT_FLAGS:
2197 val = eval_num_arg(data, size, event, arg->flags.field);
2198 print = 0;
2199 for (flag = arg->flags.flags; flag; flag = flag->next) {
2200 fval = eval_flag(flag->value);
2201 if (!val && !fval) {
2202 printf("%s", flag->str);
2203 break;
2204 }
2205 if (fval && (val & fval) == fval) {
2206 if (print && arg->flags.delim)
2207 printf("%s", arg->flags.delim);
2208 printf("%s", flag->str);
2209 print = 1;
2210 val &= ~fval;
2211 }
2212 }
2213 break;
2214 case PRINT_SYMBOL:
2215 val = eval_num_arg(data, size, event, arg->symbol.field);
2216 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2217 fval = eval_flag(flag->value);
2218 if (val == fval) {
2219 printf("%s", flag->str);
2220 break;
2221 }
2222 }
2223 break;
2224
2225 case PRINT_TYPE:
2226 break;
2227 case PRINT_STRING: {
2228 int str_offset;
2229
2230 if (arg->string.offset == -1) {
2231 struct format_field *f;
2232
2233 f = find_any_field(event, arg->string.string);
2234 arg->string.offset = f->offset;
2235 }
2236 str_offset = *(int *)(data + arg->string.offset);
2237 str_offset &= 0xffff;
2238 printf("%s", ((char *)data) + str_offset);
2239 break;
2240 }
2241 case PRINT_OP:
2242 /*
2243 * The only op for string should be ? :
2244 */
2245 if (arg->op.op[0] != '?')
2246 return;
2247 val = eval_num_arg(data, size, event, arg->op.left);
2248 if (val)
2249 print_str_arg(data, size, event, arg->op.right->op.left);
2250 else
2251 print_str_arg(data, size, event, arg->op.right->op.right);
2252 break;
2253 default:
2254 /* well... */
2255 break;
2256 }
2257 }
2258
2259 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2260 {
2261 static struct format_field *field, *ip_field;
2262 struct print_arg *args, *arg, **next;
2263 unsigned long long ip, val;
2264 char *ptr;
2265 void *bptr;
2266
2267 if (!field) {
2268 field = find_field(event, "buf");
2269 if (!field)
2270 die("can't find buffer field for binary printk");
2271 ip_field = find_field(event, "ip");
2272 if (!ip_field)
2273 die("can't find ip field for binary printk");
2274 }
2275
2276 ip = read_size(data + ip_field->offset, ip_field->size);
2277
2278 /*
2279 * The first arg is the IP pointer.
2280 */
2281 args = malloc_or_die(sizeof(*args));
2282 arg = args;
2283 arg->next = NULL;
2284 next = &arg->next;
2285
2286 arg->type = PRINT_ATOM;
2287 arg->atom.atom = malloc_or_die(32);
2288 sprintf(arg->atom.atom, "%lld", ip);
2289
2290 /* skip the first "%pf : " */
2291 for (ptr = fmt + 6, bptr = data + field->offset;
2292 bptr < data + size && *ptr; ptr++) {
2293 int ls = 0;
2294
2295 if (*ptr == '%') {
2296 process_again:
2297 ptr++;
2298 switch (*ptr) {
2299 case '%':
2300 break;
2301 case 'l':
2302 ls++;
2303 goto process_again;
2304 case 'L':
2305 ls = 2;
2306 goto process_again;
2307 case '0' ... '9':
2308 goto process_again;
2309 case 'p':
2310 ls = 1;
2311 /* fall through */
2312 case 'd':
2313 case 'u':
2314 case 'x':
2315 case 'i':
2316 /* the pointers are always 4 bytes aligned */
2317 bptr = (void *)(((unsigned long)bptr + 3) &
2318 ~3);
2319 switch (ls) {
2320 case 0:
2321 case 1:
2322 ls = long_size;
2323 break;
2324 case 2:
2325 ls = 8;
2326 default:
2327 break;
2328 }
2329 val = read_size(bptr, ls);
2330 bptr += ls;
2331 arg = malloc_or_die(sizeof(*arg));
2332 arg->next = NULL;
2333 arg->type = PRINT_ATOM;
2334 arg->atom.atom = malloc_or_die(32);
2335 sprintf(arg->atom.atom, "%lld", val);
2336 *next = arg;
2337 next = &arg->next;
2338 break;
2339 case 's':
2340 arg = malloc_or_die(sizeof(*arg));
2341 arg->next = NULL;
2342 arg->type = PRINT_STRING;
2343 arg->string.string = strdup(bptr);
2344 bptr += strlen(bptr) + 1;
2345 *next = arg;
2346 next = &arg->next;
2347 default:
2348 break;
2349 }
2350 }
2351 }
2352
2353 return args;
2354 }
2355
2356 static void free_args(struct print_arg *args)
2357 {
2358 struct print_arg *next;
2359
2360 while (args) {
2361 next = args->next;
2362
2363 if (args->type == PRINT_ATOM)
2364 free(args->atom.atom);
2365 else
2366 free(args->string.string);
2367 free(args);
2368 args = next;
2369 }
2370 }
2371
2372 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2373 {
2374 unsigned long long addr;
2375 static struct format_field *field;
2376 struct printk_map *printk;
2377 char *format;
2378 char *p;
2379
2380 if (!field) {
2381 field = find_field(event, "fmt");
2382 if (!field)
2383 die("can't find format field for binary printk");
2384 printf("field->offset = %d size=%d\n", field->offset, field->size);
2385 }
2386
2387 addr = read_size(data + field->offset, field->size);
2388
2389 printk = find_printk(addr);
2390 if (!printk) {
2391 format = malloc_or_die(45);
2392 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2393 addr);
2394 return format;
2395 }
2396
2397 p = printk->printk;
2398 /* Remove any quotes. */
2399 if (*p == '"')
2400 p++;
2401 format = malloc_or_die(strlen(p) + 10);
2402 sprintf(format, "%s : %s", "%pf", p);
2403 /* remove ending quotes and new line since we will add one too */
2404 p = format + strlen(format) - 1;
2405 if (*p == '"')
2406 *p = 0;
2407
2408 p -= 2;
2409 if (strcmp(p, "\\n") == 0)
2410 *p = 0;
2411
2412 return format;
2413 }
2414
2415 static void pretty_print(void *data, int size, struct event *event)
2416 {
2417 struct print_fmt *print_fmt = &event->print_fmt;
2418 struct print_arg *arg = print_fmt->args;
2419 struct print_arg *args = NULL;
2420 const char *ptr = print_fmt->format;
2421 unsigned long long val;
2422 struct func_map *func;
2423 const char *saveptr;
2424 char *bprint_fmt = NULL;
2425 char format[32];
2426 int show_func;
2427 int len;
2428 int ls;
2429
2430 if (event->flags & EVENT_FL_ISFUNC)
2431 ptr = " %pF <-- %pF";
2432
2433 if (event->flags & EVENT_FL_ISBPRINT) {
2434 bprint_fmt = get_bprint_format(data, size, event);
2435 args = make_bprint_args(bprint_fmt, data, size, event);
2436 arg = args;
2437 ptr = bprint_fmt;
2438 }
2439
2440 for (; *ptr; ptr++) {
2441 ls = 0;
2442 if (*ptr == '\\') {
2443 ptr++;
2444 switch (*ptr) {
2445 case 'n':
2446 printf("\n");
2447 break;
2448 case 't':
2449 printf("\t");
2450 break;
2451 case 'r':
2452 printf("\r");
2453 break;
2454 case '\\':
2455 printf("\\");
2456 break;
2457 default:
2458 printf("%c", *ptr);
2459 break;
2460 }
2461
2462 } else if (*ptr == '%') {
2463 saveptr = ptr;
2464 show_func = 0;
2465 cont_process:
2466 ptr++;
2467 switch (*ptr) {
2468 case '%':
2469 printf("%%");
2470 break;
2471 case 'l':
2472 ls++;
2473 goto cont_process;
2474 case 'L':
2475 ls = 2;
2476 goto cont_process;
2477 case 'z':
2478 case 'Z':
2479 case '0' ... '9':
2480 goto cont_process;
2481 case 'p':
2482 if (long_size == 4)
2483 ls = 1;
2484 else
2485 ls = 2;
2486
2487 if (*(ptr+1) == 'F' ||
2488 *(ptr+1) == 'f') {
2489 ptr++;
2490 show_func = *ptr;
2491 }
2492
2493 /* fall through */
2494 case 'd':
2495 case 'i':
2496 case 'x':
2497 case 'X':
2498 case 'u':
2499 if (!arg)
2500 die("no argument match");
2501
2502 len = ((unsigned long)ptr + 1) -
2503 (unsigned long)saveptr;
2504
2505 /* should never happen */
2506 if (len > 32)
2507 die("bad format!");
2508
2509 memcpy(format, saveptr, len);
2510 format[len] = 0;
2511
2512 val = eval_num_arg(data, size, event, arg);
2513 arg = arg->next;
2514
2515 if (show_func) {
2516 func = find_func(val);
2517 if (func) {
2518 printf("%s", func->func);
2519 if (show_func == 'F')
2520 printf("+0x%llx",
2521 val - func->addr);
2522 break;
2523 }
2524 }
2525 switch (ls) {
2526 case 0:
2527 printf(format, (int)val);
2528 break;
2529 case 1:
2530 printf(format, (long)val);
2531 break;
2532 case 2:
2533 printf(format, (long long)val);
2534 break;
2535 default:
2536 die("bad count (%d)", ls);
2537 }
2538 break;
2539 case 's':
2540 if (!arg)
2541 die("no matching argument");
2542
2543 print_str_arg(data, size, event, arg);
2544 arg = arg->next;
2545 break;
2546 default:
2547 printf(">%c<", *ptr);
2548
2549 }
2550 } else
2551 printf("%c", *ptr);
2552 }
2553
2554 if (args) {
2555 free_args(args);
2556 free(bprint_fmt);
2557 }
2558 }
2559
2560 static inline int log10_cpu(int nb)
2561 {
2562 if (nb / 100)
2563 return 3;
2564 if (nb / 10)
2565 return 2;
2566 return 1;
2567 }
2568
2569 static void print_lat_fmt(void *data, int size __unused)
2570 {
2571 unsigned int lat_flags;
2572 unsigned int pc;
2573 int lock_depth;
2574 int hardirq;
2575 int softirq;
2576
2577 lat_flags = parse_common_flags(data);
2578 pc = parse_common_pc(data);
2579 lock_depth = parse_common_lock_depth(data);
2580
2581 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2582 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2583
2584 printf("%c%c%c",
2585 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2586 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2587 'X' : '.',
2588 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2589 'N' : '.',
2590 (hardirq && softirq) ? 'H' :
2591 hardirq ? 'h' : softirq ? 's' : '.');
2592
2593 if (pc)
2594 printf("%x", pc);
2595 else
2596 printf(".");
2597
2598 if (lock_depth < 0)
2599 printf(".");
2600 else
2601 printf("%d", lock_depth);
2602 }
2603
2604 /* taken from Linux, written by Frederic Weisbecker */
2605 static void print_graph_cpu(int cpu)
2606 {
2607 int i;
2608 int log10_this = log10_cpu(cpu);
2609 int log10_all = log10_cpu(cpus);
2610
2611
2612 /*
2613 * Start with a space character - to make it stand out
2614 * to the right a bit when trace output is pasted into
2615 * email:
2616 */
2617 printf(" ");
2618
2619 /*
2620 * Tricky - we space the CPU field according to the max
2621 * number of online CPUs. On a 2-cpu system it would take
2622 * a maximum of 1 digit - on a 128 cpu system it would
2623 * take up to 3 digits:
2624 */
2625 for (i = 0; i < log10_all - log10_this; i++)
2626 printf(" ");
2627
2628 printf("%d) ", cpu);
2629 }
2630
2631 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2632 #define TRACE_GRAPH_INDENT 2
2633
2634 static void print_graph_proc(int pid, const char *comm)
2635 {
2636 /* sign + log10(MAX_INT) + '\0' */
2637 char pid_str[11];
2638 int spaces = 0;
2639 int len;
2640 int i;
2641
2642 sprintf(pid_str, "%d", pid);
2643
2644 /* 1 stands for the "-" character */
2645 len = strlen(comm) + strlen(pid_str) + 1;
2646
2647 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2648 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2649
2650 /* First spaces to align center */
2651 for (i = 0; i < spaces / 2; i++)
2652 printf(" ");
2653
2654 printf("%s-%s", comm, pid_str);
2655
2656 /* Last spaces to align center */
2657 for (i = 0; i < spaces - (spaces / 2); i++)
2658 printf(" ");
2659 }
2660
2661 static struct record *
2662 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2663 struct record *next)
2664 {
2665 struct format_field *field;
2666 struct event *event;
2667 unsigned long val;
2668 int type;
2669 int pid;
2670
2671 type = trace_parse_common_type(next->data);
2672 event = trace_find_event(type);
2673 if (!event)
2674 return NULL;
2675
2676 if (!(event->flags & EVENT_FL_ISFUNCRET))
2677 return NULL;
2678
2679 pid = parse_common_pid(next->data);
2680 field = find_field(event, "func");
2681 if (!field)
2682 die("function return does not have field func");
2683
2684 val = read_size(next->data + field->offset, field->size);
2685
2686 if (cur_pid != pid || cur_func != val)
2687 return NULL;
2688
2689 /* this is a leaf, now advance the iterator */
2690 return trace_read_data(cpu);
2691 }
2692
2693 /* Signal a overhead of time execution to the output */
2694 static void print_graph_overhead(unsigned long long duration)
2695 {
2696 /* Non nested entry or return */
2697 if (duration == ~0ULL)
2698 return (void)printf(" ");
2699
2700 /* Duration exceeded 100 msecs */
2701 if (duration > 100000ULL)
2702 return (void)printf("! ");
2703
2704 /* Duration exceeded 10 msecs */
2705 if (duration > 10000ULL)
2706 return (void)printf("+ ");
2707
2708 printf(" ");
2709 }
2710
2711 static void print_graph_duration(unsigned long long duration)
2712 {
2713 unsigned long usecs = duration / 1000;
2714 unsigned long nsecs_rem = duration % 1000;
2715 /* log10(ULONG_MAX) + '\0' */
2716 char msecs_str[21];
2717 char nsecs_str[5];
2718 int len;
2719 int i;
2720
2721 sprintf(msecs_str, "%lu", usecs);
2722
2723 /* Print msecs */
2724 len = printf("%lu", usecs);
2725
2726 /* Print nsecs (we don't want to exceed 7 numbers) */
2727 if (len < 7) {
2728 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2729 len += printf(".%s", nsecs_str);
2730 }
2731
2732 printf(" us ");
2733
2734 /* Print remaining spaces to fit the row's width */
2735 for (i = len; i < 7; i++)
2736 printf(" ");
2737
2738 printf("| ");
2739 }
2740
2741 static void
2742 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2743 {
2744 unsigned long long rettime, calltime;
2745 unsigned long long duration, depth;
2746 unsigned long long val;
2747 struct format_field *field;
2748 struct func_map *func;
2749 struct event *ret_event;
2750 int type;
2751 int i;
2752
2753 type = trace_parse_common_type(ret_rec->data);
2754 ret_event = trace_find_event(type);
2755
2756 field = find_field(ret_event, "rettime");
2757 if (!field)
2758 die("can't find rettime in return graph");
2759 rettime = read_size(ret_rec->data + field->offset, field->size);
2760
2761 field = find_field(ret_event, "calltime");
2762 if (!field)
2763 die("can't find rettime in return graph");
2764 calltime = read_size(ret_rec->data + field->offset, field->size);
2765
2766 duration = rettime - calltime;
2767
2768 /* Overhead */
2769 print_graph_overhead(duration);
2770
2771 /* Duration */
2772 print_graph_duration(duration);
2773
2774 field = find_field(event, "depth");
2775 if (!field)
2776 die("can't find depth in entry graph");
2777 depth = read_size(data + field->offset, field->size);
2778
2779 /* Function */
2780 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2781 printf(" ");
2782
2783 field = find_field(event, "func");
2784 if (!field)
2785 die("can't find func in entry graph");
2786 val = read_size(data + field->offset, field->size);
2787 func = find_func(val);
2788
2789 if (func)
2790 printf("%s();", func->func);
2791 else
2792 printf("%llx();", val);
2793 }
2794
2795 static void print_graph_nested(struct event *event, void *data)
2796 {
2797 struct format_field *field;
2798 unsigned long long depth;
2799 unsigned long long val;
2800 struct func_map *func;
2801 int i;
2802
2803 /* No overhead */
2804 print_graph_overhead(-1);
2805
2806 /* No time */
2807 printf(" | ");
2808
2809 field = find_field(event, "depth");
2810 if (!field)
2811 die("can't find depth in entry graph");
2812 depth = read_size(data + field->offset, field->size);
2813
2814 /* Function */
2815 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2816 printf(" ");
2817
2818 field = find_field(event, "func");
2819 if (!field)
2820 die("can't find func in entry graph");
2821 val = read_size(data + field->offset, field->size);
2822 func = find_func(val);
2823
2824 if (func)
2825 printf("%s() {", func->func);
2826 else
2827 printf("%llx() {", val);
2828 }
2829
2830 static void
2831 pretty_print_func_ent(void *data, int size, struct event *event,
2832 int cpu, int pid, const char *comm,
2833 unsigned long secs, unsigned long usecs)
2834 {
2835 struct format_field *field;
2836 struct record *rec;
2837 void *copy_data;
2838 unsigned long val;
2839
2840 printf("%5lu.%06lu | ", secs, usecs);
2841
2842 print_graph_cpu(cpu);
2843 print_graph_proc(pid, comm);
2844
2845 printf(" | ");
2846
2847 if (latency_format) {
2848 print_lat_fmt(data, size);
2849 printf(" | ");
2850 }
2851
2852 field = find_field(event, "func");
2853 if (!field)
2854 die("function entry does not have func field");
2855
2856 val = read_size(data + field->offset, field->size);
2857
2858 /*
2859 * peek_data may unmap the data pointer. Copy it first.
2860 */
2861 copy_data = malloc_or_die(size);
2862 memcpy(copy_data, data, size);
2863 data = copy_data;
2864
2865 rec = trace_peek_data(cpu);
2866 if (rec) {
2867 rec = get_return_for_leaf(cpu, pid, val, rec);
2868 if (rec) {
2869 print_graph_entry_leaf(event, data, rec);
2870 goto out_free;
2871 }
2872 }
2873 print_graph_nested(event, data);
2874 out_free:
2875 free(data);
2876 }
2877
2878 static void
2879 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2880 int cpu, int pid, const char *comm,
2881 unsigned long secs, unsigned long usecs)
2882 {
2883 unsigned long long rettime, calltime;
2884 unsigned long long duration, depth;
2885 struct format_field *field;
2886 int i;
2887
2888 printf("%5lu.%06lu | ", secs, usecs);
2889
2890 print_graph_cpu(cpu);
2891 print_graph_proc(pid, comm);
2892
2893 printf(" | ");
2894
2895 if (latency_format) {
2896 print_lat_fmt(data, size);
2897 printf(" | ");
2898 }
2899
2900 field = find_field(event, "rettime");
2901 if (!field)
2902 die("can't find rettime in return graph");
2903 rettime = read_size(data + field->offset, field->size);
2904
2905 field = find_field(event, "calltime");
2906 if (!field)
2907 die("can't find calltime in return graph");
2908 calltime = read_size(data + field->offset, field->size);
2909
2910 duration = rettime - calltime;
2911
2912 /* Overhead */
2913 print_graph_overhead(duration);
2914
2915 /* Duration */
2916 print_graph_duration(duration);
2917
2918 field = find_field(event, "depth");
2919 if (!field)
2920 die("can't find depth in entry graph");
2921 depth = read_size(data + field->offset, field->size);
2922
2923 /* Function */
2924 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2925 printf(" ");
2926
2927 printf("}");
2928 }
2929
2930 static void
2931 pretty_print_func_graph(void *data, int size, struct event *event,
2932 int cpu, int pid, const char *comm,
2933 unsigned long secs, unsigned long usecs)
2934 {
2935 if (event->flags & EVENT_FL_ISFUNCENT)
2936 pretty_print_func_ent(data, size, event,
2937 cpu, pid, comm, secs, usecs);
2938 else if (event->flags & EVENT_FL_ISFUNCRET)
2939 pretty_print_func_ret(data, size, event,
2940 cpu, pid, comm, secs, usecs);
2941 printf("\n");
2942 }
2943
2944 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2945 char *comm)
2946 {
2947 struct event *event;
2948 unsigned long secs;
2949 unsigned long usecs;
2950 int type;
2951 int pid;
2952
2953 secs = nsecs / NSECS_PER_SEC;
2954 nsecs -= secs * NSECS_PER_SEC;
2955 usecs = nsecs / NSECS_PER_USEC;
2956
2957 type = trace_parse_common_type(data);
2958
2959 event = trace_find_event(type);
2960 if (!event) {
2961 warning("ug! no event found for type %d", type);
2962 return;
2963 }
2964
2965 pid = parse_common_pid(data);
2966
2967 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2968 return pretty_print_func_graph(data, size, event, cpu,
2969 pid, comm, secs, usecs);
2970
2971 if (latency_format) {
2972 printf("%8.8s-%-5d %3d",
2973 comm, pid, cpu);
2974 print_lat_fmt(data, size);
2975 } else
2976 printf("%16s-%-5d [%03d]", comm, pid, cpu);
2977
2978 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name);
2979
2980 if (event->flags & EVENT_FL_FAILED) {
2981 printf("EVENT '%s' FAILED TO PARSE\n",
2982 event->name);
2983 return;
2984 }
2985
2986 pretty_print(data, size, event);
2987 printf("\n");
2988 }
2989
2990 static void print_fields(struct print_flag_sym *field)
2991 {
2992 printf("{ %s, %s }", field->value, field->str);
2993 if (field->next) {
2994 printf(", ");
2995 print_fields(field->next);
2996 }
2997 }
2998
2999 static void print_args(struct print_arg *args)
3000 {
3001 int print_paren = 1;
3002
3003 switch (args->type) {
3004 case PRINT_NULL:
3005 printf("null");
3006 break;
3007 case PRINT_ATOM:
3008 printf("%s", args->atom.atom);
3009 break;
3010 case PRINT_FIELD:
3011 printf("REC->%s", args->field.name);
3012 break;
3013 case PRINT_FLAGS:
3014 printf("__print_flags(");
3015 print_args(args->flags.field);
3016 printf(", %s, ", args->flags.delim);
3017 print_fields(args->flags.flags);
3018 printf(")");
3019 break;
3020 case PRINT_SYMBOL:
3021 printf("__print_symbolic(");
3022 print_args(args->symbol.field);
3023 printf(", ");
3024 print_fields(args->symbol.symbols);
3025 printf(")");
3026 break;
3027 case PRINT_STRING:
3028 printf("__get_str(%s)", args->string.string);
3029 break;
3030 case PRINT_TYPE:
3031 printf("(%s)", args->typecast.type);
3032 print_args(args->typecast.item);
3033 break;
3034 case PRINT_OP:
3035 if (strcmp(args->op.op, ":") == 0)
3036 print_paren = 0;
3037 if (print_paren)
3038 printf("(");
3039 print_args(args->op.left);
3040 printf(" %s ", args->op.op);
3041 print_args(args->op.right);
3042 if (print_paren)
3043 printf(")");
3044 break;
3045 default:
3046 /* we should warn... */
3047 return;
3048 }
3049 if (args->next) {
3050 printf("\n");
3051 print_args(args->next);
3052 }
3053 }
3054
3055 static void parse_header_field(const char *field,
3056 int *offset, int *size)
3057 {
3058 char *token;
3059 int type;
3060
3061 if (read_expected(EVENT_ITEM, "field") < 0)
3062 return;
3063 if (read_expected(EVENT_OP, ":") < 0)
3064 return;
3065
3066 /* type */
3067 if (read_expect_type(EVENT_ITEM, &token) < 0)
3068 goto fail;
3069 free_token(token);
3070
3071 if (read_expected(EVENT_ITEM, field) < 0)
3072 return;
3073 if (read_expected(EVENT_OP, ";") < 0)
3074 return;
3075 if (read_expected(EVENT_ITEM, "offset") < 0)
3076 return;
3077 if (read_expected(EVENT_OP, ":") < 0)
3078 return;
3079 if (read_expect_type(EVENT_ITEM, &token) < 0)
3080 goto fail;
3081 *offset = atoi(token);
3082 free_token(token);
3083 if (read_expected(EVENT_OP, ";") < 0)
3084 return;
3085 if (read_expected(EVENT_ITEM, "size") < 0)
3086 return;
3087 if (read_expected(EVENT_OP, ":") < 0)
3088 return;
3089 if (read_expect_type(EVENT_ITEM, &token) < 0)
3090 goto fail;
3091 *size = atoi(token);
3092 free_token(token);
3093 if (read_expected(EVENT_OP, ";") < 0)
3094 return;
3095 type = read_token(&token);
3096 if (type != EVENT_NEWLINE) {
3097 /* newer versions of the kernel have a "signed" type */
3098 if (type != EVENT_ITEM)
3099 goto fail;
3100
3101 if (strcmp(token, "signed") != 0)
3102 goto fail;
3103
3104 free_token(token);
3105
3106 if (read_expected(EVENT_OP, ":") < 0)
3107 return;
3108
3109 if (read_expect_type(EVENT_ITEM, &token))
3110 goto fail;
3111
3112 free_token(token);
3113 if (read_expected(EVENT_OP, ";") < 0)
3114 return;
3115
3116 if (read_expect_type(EVENT_NEWLINE, &token))
3117 goto fail;
3118 }
3119 fail:
3120 free_token(token);
3121 }
3122
3123 int parse_header_page(char *buf, unsigned long size)
3124 {
3125 init_input_buf(buf, size);
3126
3127 parse_header_field("timestamp", &header_page_ts_offset,
3128 &header_page_ts_size);
3129 parse_header_field("commit", &header_page_size_offset,
3130 &header_page_size_size);
3131 parse_header_field("data", &header_page_data_offset,
3132 &header_page_data_size);
3133
3134 return 0;
3135 }
3136
3137 int parse_ftrace_file(char *buf, unsigned long size)
3138 {
3139 struct format_field *field;
3140 struct print_arg *arg, **list;
3141 struct event *event;
3142 int ret;
3143
3144 init_input_buf(buf, size);
3145
3146 event = alloc_event();
3147 if (!event)
3148 return -ENOMEM;
3149
3150 event->flags |= EVENT_FL_ISFTRACE;
3151
3152 event->name = event_read_name();
3153 if (!event->name)
3154 die("failed to read ftrace event name");
3155
3156 if (strcmp(event->name, "function") == 0)
3157 event->flags |= EVENT_FL_ISFUNC;
3158
3159 else if (strcmp(event->name, "funcgraph_entry") == 0)
3160 event->flags |= EVENT_FL_ISFUNCENT;
3161
3162 else if (strcmp(event->name, "funcgraph_exit") == 0)
3163 event->flags |= EVENT_FL_ISFUNCRET;
3164
3165 else if (strcmp(event->name, "bprint") == 0)
3166 event->flags |= EVENT_FL_ISBPRINT;
3167
3168 event->id = event_read_id();
3169 if (event->id < 0)
3170 die("failed to read ftrace event id");
3171
3172 add_event(event);
3173
3174 ret = event_read_format(event);
3175 if (ret < 0)
3176 die("failed to read ftrace event format");
3177
3178 ret = event_read_print(event);
3179 if (ret < 0)
3180 die("failed to read ftrace event print fmt");
3181
3182 /* New ftrace handles args */
3183 if (ret > 0)
3184 return 0;
3185 /*
3186 * The arguments for ftrace files are parsed by the fields.
3187 * Set up the fields as their arguments.
3188 */
3189 list = &event->print_fmt.args;
3190 for (field = event->format.fields; field; field = field->next) {
3191 arg = malloc_or_die(sizeof(*arg));
3192 memset(arg, 0, sizeof(*arg));
3193 *list = arg;
3194 list = &arg->next;
3195 arg->type = PRINT_FIELD;
3196 arg->field.name = field->name;
3197 arg->field.field = field;
3198 }
3199 return 0;
3200 }
3201
3202 int parse_event_file(char *buf, unsigned long size, char *sys)
3203 {
3204 struct event *event;
3205 int ret;
3206
3207 init_input_buf(buf, size);
3208
3209 event = alloc_event();
3210 if (!event)
3211 return -ENOMEM;
3212
3213 event->name = event_read_name();
3214 if (!event->name)
3215 die("failed to read event name");
3216
3217 event->id = event_read_id();
3218 if (event->id < 0)
3219 die("failed to read event id");
3220
3221 ret = event_read_format(event);
3222 if (ret < 0) {
3223 warning("failed to read event format for %s", event->name);
3224 goto event_failed;
3225 }
3226
3227 ret = event_read_print(event);
3228 if (ret < 0) {
3229 warning("failed to read event print fmt for %s", event->name);
3230 goto event_failed;
3231 }
3232
3233 event->system = strdup(sys);
3234
3235 #define PRINT_ARGS 0
3236 if (PRINT_ARGS && event->print_fmt.args)
3237 print_args(event->print_fmt.args);
3238
3239 add_event(event);
3240 return 0;
3241
3242 event_failed:
3243 event->flags |= EVENT_FL_FAILED;
3244 /* still add it even if it failed */
3245 add_event(event);
3246 return -1;
3247 }
3248
3249 void parse_set_info(int nr_cpus, int long_sz)
3250 {
3251 cpus = nr_cpus;
3252 long_size = long_sz;
3253 }
This page took 0.1024 seconds and 6 git commands to generate.