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