blob: 26503d647090a5f5bacbdb01d88a64920aeeb050 [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 */
David Daney2ef1ea32012-01-17 13:41:01 -080024
Steven Rostedtea4010d2009-08-17 16:18:07 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Steven Rostedtea4010d2009-08-17 16:18:07 +020028#include <errno.h>
29
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020030#include "../perf.h"
Steven Rostedtea4010d2009-08-17 16:18:07 +020031#include "util.h"
32#include "trace-event.h"
33
34int header_page_ts_offset;
35int header_page_ts_size;
36int header_page_size_offset;
37int header_page_size_size;
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -030038int header_page_overwrite_offset;
39int header_page_overwrite_size;
Steven Rostedtea4010d2009-08-17 16:18:07 +020040int header_page_data_offset;
41int header_page_data_size;
42
Ian Munsiec0555642010-04-13 18:37:33 +100043bool latency_format;
Steven Rostedtcda48462009-10-14 15:43:42 -040044
Steven Rostedtea4010d2009-08-17 16:18:07 +020045static char *input_buf;
46static unsigned long long input_buf_ptr;
47static unsigned long long input_buf_siz;
48
49static int cpus;
50static int long_size;
Tom Zanussieb9a42c2009-11-25 01:15:47 -060051static int is_flag_field;
52static int is_symbolic_field;
53
54static struct format_field *
55find_any_field(struct event *event, const char *name);
Steven Rostedtea4010d2009-08-17 16:18:07 +020056
57static void init_input_buf(char *buf, unsigned long long size)
58{
59 input_buf = buf;
60 input_buf_siz = size;
61 input_buf_ptr = 0;
62}
63
64struct cmdline {
65 char *comm;
66 int pid;
67};
68
69static struct cmdline *cmdlines;
70static int cmdline_count;
71
72static int cmdline_cmp(const void *a, const void *b)
73{
74 const struct cmdline *ca = a;
75 const struct cmdline *cb = b;
76
77 if (ca->pid < cb->pid)
78 return -1;
79 if (ca->pid > cb->pid)
80 return 1;
81
82 return 0;
83}
84
85void parse_cmdlines(char *file, int size __unused)
86{
87 struct cmdline_list {
88 struct cmdline_list *next;
89 char *comm;
90 int pid;
91 } *list = NULL, *item;
92 char *line;
93 char *next = NULL;
94 int i;
95
96 line = strtok_r(file, "\n", &next);
97 while (line) {
98 item = malloc_or_die(sizeof(*item));
99 sscanf(line, "%d %as", &item->pid,
Ingo Molnar65014ab2009-09-02 14:55:55 +0200100 (float *)(void *)&item->comm); /* workaround gcc warning */
Steven Rostedtea4010d2009-08-17 16:18:07 +0200101 item->next = list;
102 list = item;
103 line = strtok_r(NULL, "\n", &next);
104 cmdline_count++;
105 }
106
107 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
108
109 i = 0;
110 while (list) {
111 cmdlines[i].pid = list->pid;
112 cmdlines[i].comm = list->comm;
113 i++;
114 item = list;
115 list = list->next;
116 free(item);
117 }
118
119 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
120}
121
122static struct func_map {
123 unsigned long long addr;
124 char *func;
125 char *mod;
126} *func_list;
127static unsigned int func_count;
128
129static int func_cmp(const void *a, const void *b)
130{
131 const struct func_map *fa = a;
132 const struct func_map *fb = b;
133
134 if (fa->addr < fb->addr)
135 return -1;
136 if (fa->addr > fb->addr)
137 return 1;
138
139 return 0;
140}
141
142void parse_proc_kallsyms(char *file, unsigned int size __unused)
143{
144 struct func_list {
145 struct func_list *next;
146 unsigned long long addr;
147 char *func;
148 char *mod;
149 } *list = NULL, *item;
150 char *line;
151 char *next = NULL;
152 char *addr_str;
153 char ch;
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500154 int ret __used;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200155 int i;
156
157 line = strtok_r(file, "\n", &next);
158 while (line) {
159 item = malloc_or_die(sizeof(*item));
160 item->mod = NULL;
161 ret = sscanf(line, "%as %c %as\t[%as",
Ingo Molnar65014ab2009-09-02 14:55:55 +0200162 (float *)(void *)&addr_str, /* workaround gcc warning */
Steven Rostedtea4010d2009-08-17 16:18:07 +0200163 &ch,
Ingo Molnar65014ab2009-09-02 14:55:55 +0200164 (float *)(void *)&item->func,
165 (float *)(void *)&item->mod);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200166 item->addr = strtoull(addr_str, NULL, 16);
167 free(addr_str);
168
169 /* truncate the extra ']' */
170 if (item->mod)
171 item->mod[strlen(item->mod) - 1] = 0;
172
173
174 item->next = list;
175 list = item;
176 line = strtok_r(NULL, "\n", &next);
177 func_count++;
178 }
179
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900180 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
Steven Rostedtea4010d2009-08-17 16:18:07 +0200181
182 i = 0;
183 while (list) {
184 func_list[i].func = list->func;
185 func_list[i].addr = list->addr;
186 func_list[i].mod = list->mod;
187 i++;
188 item = list;
189 list = list->next;
190 free(item);
191 }
192
193 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
194
195 /*
196 * Add a special record at the end.
197 */
198 func_list[func_count].func = NULL;
199 func_list[func_count].addr = 0;
200 func_list[func_count].mod = NULL;
201}
202
203/*
204 * We are searching for a record in between, not an exact
205 * match.
206 */
207static int func_bcmp(const void *a, const void *b)
208{
209 const struct func_map *fa = a;
210 const struct func_map *fb = b;
211
212 if ((fa->addr == fb->addr) ||
213
214 (fa->addr > fb->addr &&
215 fa->addr < (fb+1)->addr))
216 return 0;
217
218 if (fa->addr < fb->addr)
219 return -1;
220
221 return 1;
222}
223
224static struct func_map *find_func(unsigned long long addr)
225{
226 struct func_map *func;
227 struct func_map key;
228
229 key.addr = addr;
230
231 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
232 func_bcmp);
233
234 return func;
235}
236
237void print_funcs(void)
238{
239 int i;
240
241 for (i = 0; i < (int)func_count; i++) {
242 printf("%016llx %s",
243 func_list[i].addr,
244 func_list[i].func);
245 if (func_list[i].mod)
246 printf(" [%s]\n", func_list[i].mod);
247 else
248 printf("\n");
249 }
250}
251
252static struct printk_map {
253 unsigned long long addr;
254 char *printk;
255} *printk_list;
256static unsigned int printk_count;
257
258static int printk_cmp(const void *a, const void *b)
259{
260 const struct func_map *fa = a;
261 const struct func_map *fb = b;
262
263 if (fa->addr < fb->addr)
264 return -1;
265 if (fa->addr > fb->addr)
266 return 1;
267
268 return 0;
269}
270
271static struct printk_map *find_printk(unsigned long long addr)
272{
273 struct printk_map *printk;
274 struct printk_map key;
275
276 key.addr = addr;
277
278 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
279 printk_cmp);
280
281 return printk;
282}
283
284void parse_ftrace_printk(char *file, unsigned int size __unused)
285{
286 struct printk_list {
287 struct printk_list *next;
288 unsigned long long addr;
289 char *printk;
290 } *list = NULL, *item;
291 char *line;
292 char *next = NULL;
293 char *addr_str;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200294 int i;
295
296 line = strtok_r(file, "\n", &next);
297 while (line) {
Steven Rostedt4e3b7992009-10-20 19:19:35 -0400298 addr_str = strsep(&line, ":");
299 if (!line) {
300 warning("error parsing print strings");
301 break;
302 }
Steven Rostedtea4010d2009-08-17 16:18:07 +0200303 item = malloc_or_die(sizeof(*item));
Steven Rostedtea4010d2009-08-17 16:18:07 +0200304 item->addr = strtoull(addr_str, NULL, 16);
Steven Rostedtffa18952009-10-14 15:43:40 -0400305 /* fmt still has a space, skip it */
Steven Rostedt4e3b7992009-10-20 19:19:35 -0400306 item->printk = strdup(line+1);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200307 item->next = list;
308 list = item;
309 line = strtok_r(NULL, "\n", &next);
310 printk_count++;
311 }
312
313 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
314
315 i = 0;
316 while (list) {
317 printk_list[i].printk = list->printk;
318 printk_list[i].addr = list->addr;
319 i++;
320 item = list;
321 list = list->next;
322 free(item);
323 }
324
325 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
326}
327
328void print_printk(void)
329{
330 int i;
331
332 for (i = 0; i < (int)printk_count; i++) {
333 printf("%016llx %s\n",
334 printk_list[i].addr,
335 printk_list[i].printk);
336 }
337}
338
339static struct event *alloc_event(void)
340{
341 struct event *event;
342
343 event = malloc_or_die(sizeof(*event));
344 memset(event, 0, sizeof(*event));
345
346 return event;
347}
348
349enum event_type {
350 EVENT_ERROR,
351 EVENT_NONE,
352 EVENT_SPACE,
353 EVENT_NEWLINE,
354 EVENT_OP,
355 EVENT_DELIM,
356 EVENT_ITEM,
357 EVENT_DQUOTE,
358 EVENT_SQUOTE,
359};
360
361static struct event *event_list;
362
363static void add_event(struct event *event)
364{
365 event->next = event_list;
366 event_list = event;
367}
368
369static int event_item_type(enum event_type type)
370{
371 switch (type) {
372 case EVENT_ITEM ... EVENT_SQUOTE:
373 return 1;
374 case EVENT_ERROR ... EVENT_DELIM:
375 default:
376 return 0;
377 }
378}
379
380static void free_arg(struct print_arg *arg)
381{
382 if (!arg)
383 return;
384
385 switch (arg->type) {
386 case PRINT_ATOM:
387 if (arg->atom.atom)
388 free(arg->atom.atom);
389 break;
390 case PRINT_NULL:
391 case PRINT_FIELD ... PRINT_OP:
392 default:
393 /* todo */
394 break;
395 }
396
397 free(arg);
398}
399
400static enum event_type get_type(int ch)
401{
402 if (ch == '\n')
403 return EVENT_NEWLINE;
404 if (isspace(ch))
405 return EVENT_SPACE;
406 if (isalnum(ch) || ch == '_')
407 return EVENT_ITEM;
408 if (ch == '\'')
409 return EVENT_SQUOTE;
410 if (ch == '"')
411 return EVENT_DQUOTE;
412 if (!isprint(ch))
413 return EVENT_NONE;
414 if (ch == '(' || ch == ')' || ch == ',')
415 return EVENT_DELIM;
416
417 return EVENT_OP;
418}
419
420static int __read_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 int __peek_char(void)
429{
430 if (input_buf_ptr >= input_buf_siz)
431 return -1;
432
433 return input_buf[input_buf_ptr];
434}
435
436static enum event_type __read_token(char **tok)
437{
438 char buf[BUFSIZ];
439 int ch, last_ch, quote_ch, next_ch;
440 int i = 0;
441 int tok_size = 0;
442 enum event_type type;
443
444 *tok = NULL;
445
446
447 ch = __read_char();
448 if (ch < 0)
449 return EVENT_NONE;
450
451 type = get_type(ch);
452 if (type == EVENT_NONE)
453 return type;
454
455 buf[i++] = ch;
456
457 switch (type) {
458 case EVENT_NEWLINE:
459 case EVENT_DELIM:
460 *tok = malloc_or_die(2);
461 (*tok)[0] = ch;
462 (*tok)[1] = 0;
463 return type;
464
465 case EVENT_OP:
466 switch (ch) {
467 case '-':
468 next_ch = __peek_char();
469 if (next_ch == '>') {
470 buf[i++] = __read_char();
471 break;
472 }
473 /* fall through */
474 case '+':
475 case '|':
476 case '&':
477 case '>':
478 case '<':
479 last_ch = ch;
480 ch = __peek_char();
481 if (ch != last_ch)
482 goto test_equal;
483 buf[i++] = __read_char();
484 switch (last_ch) {
485 case '>':
486 case '<':
487 goto test_equal;
488 default:
489 break;
490 }
491 break;
492 case '!':
493 case '=':
494 goto test_equal;
495 default: /* what should we do instead? */
496 break;
497 }
498 buf[i] = 0;
499 *tok = strdup(buf);
500 return type;
501
502 test_equal:
503 ch = __peek_char();
504 if (ch == '=')
505 buf[i++] = __read_char();
506 break;
507
508 case EVENT_DQUOTE:
509 case EVENT_SQUOTE:
510 /* don't keep quotes */
511 i--;
512 quote_ch = ch;
513 last_ch = 0;
514 do {
515 if (i == (BUFSIZ - 1)) {
516 buf[i] = 0;
517 if (*tok) {
518 *tok = realloc(*tok, tok_size + BUFSIZ);
519 if (!*tok)
520 return EVENT_NONE;
521 strcat(*tok, buf);
522 } else
523 *tok = strdup(buf);
524
525 if (!*tok)
526 return EVENT_NONE;
527 tok_size += BUFSIZ;
528 i = 0;
529 }
530 last_ch = ch;
531 ch = __read_char();
532 buf[i++] = ch;
Steven Rostedt91ff2bc2009-10-14 15:43:33 -0400533 /* the '\' '\' will cancel itself */
534 if (ch == '\\' && last_ch == '\\')
535 last_ch = 0;
536 } while (ch != quote_ch || last_ch == '\\');
Steven Rostedtea4010d2009-08-17 16:18:07 +0200537 /* remove the last quote */
538 i--;
539 goto out;
540
541 case EVENT_ERROR ... EVENT_SPACE:
542 case EVENT_ITEM:
543 default:
544 break;
545 }
546
547 while (get_type(__peek_char()) == type) {
548 if (i == (BUFSIZ - 1)) {
549 buf[i] = 0;
550 if (*tok) {
551 *tok = realloc(*tok, tok_size + BUFSIZ);
552 if (!*tok)
553 return EVENT_NONE;
554 strcat(*tok, buf);
555 } else
556 *tok = strdup(buf);
557
558 if (!*tok)
559 return EVENT_NONE;
560 tok_size += BUFSIZ;
561 i = 0;
562 }
563 ch = __read_char();
564 buf[i++] = ch;
565 }
566
567 out:
568 buf[i] = 0;
569 if (*tok) {
570 *tok = realloc(*tok, tok_size + i);
571 if (!*tok)
572 return EVENT_NONE;
573 strcat(*tok, buf);
574 } else
575 *tok = strdup(buf);
576 if (!*tok)
577 return EVENT_NONE;
578
579 return type;
580}
581
582static void free_token(char *tok)
583{
584 if (tok)
585 free(tok);
586}
587
588static enum event_type read_token(char **tok)
589{
590 enum event_type type;
591
592 for (;;) {
593 type = __read_token(tok);
594 if (type != EVENT_SPACE)
595 return type;
596
597 free_token(*tok);
598 }
599
600 /* not reached */
601 return EVENT_NONE;
602}
603
604/* no newline */
605static enum event_type read_token_item(char **tok)
606{
607 enum event_type type;
608
609 for (;;) {
610 type = __read_token(tok);
611 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
612 return type;
613
614 free_token(*tok);
615 }
616
617 /* not reached */
618 return EVENT_NONE;
619}
620
621static int test_type(enum event_type type, enum event_type expect)
622{
623 if (type != expect) {
Steven Rostedt07a4bdd2009-10-14 15:43:39 -0400624 warning("Error: expected type %d but read %d",
Steven Rostedtea4010d2009-08-17 16:18:07 +0200625 expect, type);
626 return -1;
627 }
628 return 0;
629}
630
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300631static int __test_type_token(enum event_type type, char *token,
632 enum event_type expect, const char *expect_tok,
633 bool warn)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200634{
635 if (type != expect) {
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300636 if (warn)
637 warning("Error: expected type %d but read %d",
638 expect, type);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200639 return -1;
640 }
641
642 if (strcmp(token, expect_tok) != 0) {
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300643 if (warn)
644 warning("Error: expected '%s' but read '%s'",
645 expect_tok, token);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200646 return -1;
647 }
648 return 0;
649}
650
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300651static int test_type_token(enum event_type type, char *token,
652 enum event_type expect, const char *expect_tok)
653{
654 return __test_type_token(type, token, expect, expect_tok, true);
655}
656
Steven Rostedtea4010d2009-08-17 16:18:07 +0200657static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
658{
659 enum event_type type;
660
661 if (newline_ok)
662 type = read_token(tok);
663 else
664 type = read_token_item(tok);
665 return test_type(type, expect);
666}
667
668static int read_expect_type(enum event_type expect, char **tok)
669{
670 return __read_expect_type(expect, tok, 1);
671}
672
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300673static int __read_expected(enum event_type expect, const char *str,
674 int newline_ok, bool warn)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200675{
676 enum event_type type;
677 char *token;
678 int ret;
679
680 if (newline_ok)
681 type = read_token(&token);
682 else
683 type = read_token_item(&token);
684
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300685 ret = __test_type_token(type, token, expect, str, warn);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200686
687 free_token(token);
688
Steven Rostedt07a4bdd2009-10-14 15:43:39 -0400689 return ret;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200690}
691
Randy Dunlapcbef79a2009-10-05 13:17:29 -0700692static int read_expected(enum event_type expect, const char *str)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200693{
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300694 return __read_expected(expect, str, 1, true);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200695}
696
Randy Dunlapcbef79a2009-10-05 13:17:29 -0700697static int read_expected_item(enum event_type expect, const char *str)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200698{
Arnaldo Carvalho de Meloe9e94e32010-04-05 18:01:10 -0300699 return __read_expected(expect, str, 0, true);
Steven Rostedtea4010d2009-08-17 16:18:07 +0200700}
701
702static char *event_read_name(void)
703{
704 char *token;
705
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400706 if (read_expected(EVENT_ITEM, "name") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200707 return NULL;
708
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400709 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200710 return NULL;
711
712 if (read_expect_type(EVENT_ITEM, &token) < 0)
713 goto fail;
714
715 return token;
716
717 fail:
718 free_token(token);
719 return NULL;
720}
721
722static int event_read_id(void)
723{
724 char *token;
Borislav Petkov842f07f2012-03-21 15:15:47 +0100725 int id = -1;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200726
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400727 if (read_expected_item(EVENT_ITEM, "ID") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200728 return -1;
729
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400730 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200731 return -1;
732
733 if (read_expect_type(EVENT_ITEM, &token) < 0)
Borislav Petkov842f07f2012-03-21 15:15:47 +0100734 goto free;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200735
736 id = strtoul(token, NULL, 0);
Borislav Petkov842f07f2012-03-21 15:15:47 +0100737
738 free:
Steven Rostedtea4010d2009-08-17 16:18:07 +0200739 free_token(token);
740 return id;
Steven Rostedtea4010d2009-08-17 16:18:07 +0200741}
742
Tom Zanussi064739b2009-10-06 01:09:52 -0500743static int field_is_string(struct format_field *field)
744{
745 if ((field->flags & FIELD_IS_ARRAY) &&
746 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
747 !strstr(field->type, "s8")))
748 return 1;
749
750 return 0;
751}
752
753static int field_is_dynamic(struct format_field *field)
754{
Thomas Gleixnera1e2f602010-04-14 23:58:03 +0200755 if (!strncmp(field->type, "__data_loc", 10))
Tom Zanussi064739b2009-10-06 01:09:52 -0500756 return 1;
757
758 return 0;
759}
760
Steven Rostedtea4010d2009-08-17 16:18:07 +0200761static int event_read_fields(struct event *event, struct format_field **fields)
762{
763 struct format_field *field = NULL;
764 enum event_type type;
765 char *token;
766 char *last_token;
767 int count = 0;
768
769 do {
770 type = read_token(&token);
771 if (type == EVENT_NEWLINE) {
772 free_token(token);
773 return count;
774 }
775
776 count++;
777
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400778 if (test_type_token(type, token, EVENT_ITEM, "field"))
Steven Rostedtea4010d2009-08-17 16:18:07 +0200779 goto fail;
780 free_token(token);
781
782 type = read_token(&token);
783 /*
784 * The ftrace fields may still use the "special" name.
785 * Just ignore it.
786 */
787 if (event->flags & EVENT_FL_ISFTRACE &&
788 type == EVENT_ITEM && strcmp(token, "special") == 0) {
789 free_token(token);
790 type = read_token(&token);
791 }
792
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400793 if (test_type_token(type, token, EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200794 return -1;
795
796 if (read_expect_type(EVENT_ITEM, &token) < 0)
797 goto fail;
798
799 last_token = token;
800
801 field = malloc_or_die(sizeof(*field));
802 memset(field, 0, sizeof(*field));
803
804 /* read the rest of the type */
805 for (;;) {
806 type = read_token(&token);
807 if (type == EVENT_ITEM ||
808 (type == EVENT_OP && strcmp(token, "*") == 0) ||
809 /*
810 * Some of the ftrace fields are broken and have
811 * an illegal "." in them.
812 */
813 (event->flags & EVENT_FL_ISFTRACE &&
814 type == EVENT_OP && strcmp(token, ".") == 0)) {
815
816 if (strcmp(token, "*") == 0)
817 field->flags |= FIELD_IS_POINTER;
818
819 if (field->type) {
820 field->type = realloc(field->type,
821 strlen(field->type) +
822 strlen(last_token) + 2);
823 strcat(field->type, " ");
824 strcat(field->type, last_token);
825 } else
826 field->type = last_token;
827 last_token = token;
828 continue;
829 }
830
831 break;
832 }
833
834 if (!field->type) {
835 die("no type found");
836 goto fail;
837 }
838 field->name = last_token;
839
840 if (test_type(type, EVENT_OP))
841 goto fail;
842
843 if (strcmp(token, "[") == 0) {
844 enum event_type last_type = type;
845 char *brackets = token;
846 int len;
847
848 field->flags |= FIELD_IS_ARRAY;
849
850 type = read_token(&token);
851 while (strcmp(token, "]") != 0) {
852 if (last_type == EVENT_ITEM &&
853 type == EVENT_ITEM)
854 len = 2;
855 else
856 len = 1;
857 last_type = type;
858
859 brackets = realloc(brackets,
860 strlen(brackets) +
861 strlen(token) + len);
862 if (len == 2)
863 strcat(brackets, " ");
864 strcat(brackets, token);
865 free_token(token);
866 type = read_token(&token);
867 if (type == EVENT_NONE) {
868 die("failed to find token");
869 goto fail;
870 }
871 }
872
873 free_token(token);
874
875 brackets = realloc(brackets, strlen(brackets) + 2);
876 strcat(brackets, "]");
877
878 /* add brackets to type */
879
880 type = read_token(&token);
881 /*
882 * If the next token is not an OP, then it is of
883 * the format: type [] item;
884 */
885 if (type == EVENT_ITEM) {
886 field->type = realloc(field->type,
887 strlen(field->type) +
888 strlen(field->name) +
889 strlen(brackets) + 2);
890 strcat(field->type, " ");
891 strcat(field->type, field->name);
892 free_token(field->name);
893 strcat(field->type, brackets);
894 field->name = token;
895 type = read_token(&token);
896 } else {
897 field->type = realloc(field->type,
898 strlen(field->type) +
899 strlen(brackets) + 1);
900 strcat(field->type, brackets);
901 }
902 free(brackets);
903 }
904
Tom Zanussi064739b2009-10-06 01:09:52 -0500905 if (field_is_string(field)) {
906 field->flags |= FIELD_IS_STRING;
907 if (field_is_dynamic(field))
908 field->flags |= FIELD_IS_DYNAMIC;
909 }
910
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400911 if (test_type_token(type, token, EVENT_OP, ";"))
Steven Rostedtea4010d2009-08-17 16:18:07 +0200912 goto fail;
913 free_token(token);
914
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400915 if (read_expected(EVENT_ITEM, "offset") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200916 goto fail_expect;
917
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400918 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200919 goto fail_expect;
920
921 if (read_expect_type(EVENT_ITEM, &token))
922 goto fail;
923 field->offset = strtoul(token, NULL, 0);
924 free_token(token);
925
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400926 if (read_expected(EVENT_OP, ";") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200927 goto fail_expect;
928
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400929 if (read_expected(EVENT_ITEM, "size") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200930 goto fail_expect;
931
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400932 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200933 goto fail_expect;
934
935 if (read_expect_type(EVENT_ITEM, &token))
936 goto fail;
937 field->size = strtoul(token, NULL, 0);
938 free_token(token);
939
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400940 if (read_expected(EVENT_OP, ";") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200941 goto fail_expect;
942
Steven Rostedt13999e52009-10-14 15:43:38 -0400943 type = read_token(&token);
944 if (type != EVENT_NEWLINE) {
945 /* newer versions of the kernel have a "signed" type */
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400946 if (test_type_token(type, token, EVENT_ITEM, "signed"))
Steven Rostedt13999e52009-10-14 15:43:38 -0400947 goto fail;
Tom Zanussi26a50742009-10-06 01:09:50 -0500948
Steven Rostedt13999e52009-10-14 15:43:38 -0400949 free_token(token);
Tom Zanussi26a50742009-10-06 01:09:50 -0500950
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400951 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedt13999e52009-10-14 15:43:38 -0400952 goto fail_expect;
Tom Zanussi26a50742009-10-06 01:09:50 -0500953
Steven Rostedt13999e52009-10-14 15:43:38 -0400954 if (read_expect_type(EVENT_ITEM, &token))
955 goto fail;
Tom Zanussi26a50742009-10-06 01:09:50 -0500956
Tom Zanussi0d0bea52009-11-25 01:14:58 -0600957 if (strtoul(token, NULL, 0))
958 field->flags |= FIELD_IS_SIGNED;
Steven Rostedt13999e52009-10-14 15:43:38 -0400959
960 free_token(token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400961 if (read_expected(EVENT_OP, ";") < 0)
Steven Rostedt13999e52009-10-14 15:43:38 -0400962 goto fail_expect;
963
964 if (read_expect_type(EVENT_NEWLINE, &token))
965 goto fail;
966 }
967
Steven Rostedtea4010d2009-08-17 16:18:07 +0200968 free_token(token);
969
970 *fields = field;
971 fields = &field->next;
972
973 } while (1);
974
975 return 0;
976
977fail:
978 free_token(token);
979fail_expect:
980 if (field)
981 free(field);
982 return -1;
983}
984
985static int event_read_format(struct event *event)
986{
987 char *token;
988 int ret;
989
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400990 if (read_expected_item(EVENT_ITEM, "format") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200991 return -1;
992
Steven Rostedtc4dc7752009-10-14 15:43:44 -0400993 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +0200994 return -1;
995
996 if (read_expect_type(EVENT_NEWLINE, &token))
997 goto fail;
998 free_token(token);
999
1000 ret = event_read_fields(event, &event->format.common_fields);
1001 if (ret < 0)
1002 return ret;
1003 event->format.nr_common = ret;
1004
1005 ret = event_read_fields(event, &event->format.fields);
1006 if (ret < 0)
1007 return ret;
1008 event->format.nr_fields = ret;
1009
1010 return 0;
1011
1012 fail:
1013 free_token(token);
1014 return -1;
1015}
1016
1017enum event_type
1018process_arg_token(struct event *event, struct print_arg *arg,
1019 char **tok, enum event_type type);
1020
1021static enum event_type
1022process_arg(struct event *event, struct print_arg *arg, char **tok)
1023{
1024 enum event_type type;
1025 char *token;
1026
1027 type = read_token(&token);
1028 *tok = token;
1029
1030 return process_arg_token(event, arg, tok, type);
1031}
1032
1033static enum event_type
1034process_cond(struct event *event, struct print_arg *top, char **tok)
1035{
1036 struct print_arg *arg, *left, *right;
1037 enum event_type type;
1038 char *token = NULL;
1039
1040 arg = malloc_or_die(sizeof(*arg));
1041 memset(arg, 0, sizeof(*arg));
1042
1043 left = malloc_or_die(sizeof(*left));
1044
1045 right = malloc_or_die(sizeof(*right));
1046
1047 arg->type = PRINT_OP;
1048 arg->op.left = left;
1049 arg->op.right = right;
1050
1051 *tok = NULL;
1052 type = process_arg(event, left, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001053 if (test_type_token(type, token, EVENT_OP, ":"))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001054 goto out_free;
1055
1056 arg->op.op = token;
1057
1058 type = process_arg(event, right, &token);
1059
1060 top->op.right = arg;
1061
1062 *tok = token;
1063 return type;
1064
1065out_free:
1066 free_token(*tok);
1067 free(right);
1068 free(left);
1069 free_arg(arg);
1070 return EVENT_ERROR;
1071}
1072
Steven Rostedt0959b8d2009-10-14 15:43:35 -04001073static enum event_type
1074process_array(struct event *event, struct print_arg *top, char **tok)
1075{
1076 struct print_arg *arg;
1077 enum event_type type;
1078 char *token = NULL;
1079
1080 arg = malloc_or_die(sizeof(*arg));
1081 memset(arg, 0, sizeof(*arg));
1082
1083 *tok = NULL;
1084 type = process_arg(event, arg, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001085 if (test_type_token(type, token, EVENT_OP, "]"))
Steven Rostedt0959b8d2009-10-14 15:43:35 -04001086 goto out_free;
1087
1088 top->op.right = arg;
1089
1090 free_token(token);
1091 type = read_token_item(&token);
1092 *tok = token;
1093
1094 return type;
1095
1096out_free:
1097 free_token(*tok);
1098 free_arg(arg);
1099 return EVENT_ERROR;
1100}
1101
Steven Rostedtea4010d2009-08-17 16:18:07 +02001102static int get_op_prio(char *op)
1103{
1104 if (!op[1]) {
1105 switch (op[0]) {
1106 case '*':
1107 case '/':
1108 case '%':
1109 return 6;
1110 case '+':
1111 case '-':
1112 return 7;
1113 /* '>>' and '<<' are 8 */
1114 case '<':
1115 case '>':
1116 return 9;
1117 /* '==' and '!=' are 10 */
1118 case '&':
1119 return 11;
1120 case '^':
1121 return 12;
1122 case '|':
1123 return 13;
1124 case '?':
1125 return 16;
1126 default:
1127 die("unknown op '%c'", op[0]);
1128 return -1;
1129 }
1130 } else {
1131 if (strcmp(op, "++") == 0 ||
1132 strcmp(op, "--") == 0) {
1133 return 3;
1134 } else if (strcmp(op, ">>") == 0 ||
1135 strcmp(op, "<<") == 0) {
1136 return 8;
1137 } else if (strcmp(op, ">=") == 0 ||
1138 strcmp(op, "<=") == 0) {
1139 return 9;
1140 } else if (strcmp(op, "==") == 0 ||
1141 strcmp(op, "!=") == 0) {
1142 return 10;
1143 } else if (strcmp(op, "&&") == 0) {
1144 return 14;
1145 } else if (strcmp(op, "||") == 0) {
1146 return 15;
1147 } else {
1148 die("unknown op '%s'", op);
1149 return -1;
1150 }
1151 }
1152}
1153
1154static void set_op_prio(struct print_arg *arg)
1155{
1156
1157 /* single ops are the greatest */
1158 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1159 arg->op.prio = 0;
1160 return;
1161 }
1162
1163 arg->op.prio = get_op_prio(arg->op.op);
1164}
1165
1166static enum event_type
1167process_op(struct event *event, struct print_arg *arg, char **tok)
1168{
1169 struct print_arg *left, *right = NULL;
1170 enum event_type type;
1171 char *token;
1172
1173 /* the op is passed in via tok */
1174 token = *tok;
1175
1176 if (arg->type == PRINT_OP && !arg->op.left) {
1177 /* handle single op */
1178 if (token[1]) {
1179 die("bad op token %s", token);
1180 return EVENT_ERROR;
1181 }
1182 switch (token[0]) {
1183 case '!':
1184 case '+':
1185 case '-':
1186 break;
1187 default:
1188 die("bad op token %s", token);
1189 return EVENT_ERROR;
1190 }
1191
1192 /* make an empty left */
1193 left = malloc_or_die(sizeof(*left));
1194 left->type = PRINT_NULL;
1195 arg->op.left = left;
1196
1197 right = malloc_or_die(sizeof(*right));
1198 arg->op.right = right;
1199
1200 type = process_arg(event, right, tok);
1201
1202 } else if (strcmp(token, "?") == 0) {
1203
1204 left = malloc_or_die(sizeof(*left));
1205 /* copy the top arg to the left */
1206 *left = *arg;
1207
1208 arg->type = PRINT_OP;
1209 arg->op.op = token;
1210 arg->op.left = left;
1211 arg->op.prio = 0;
1212
1213 type = process_cond(event, arg, tok);
1214
1215 } else if (strcmp(token, ">>") == 0 ||
1216 strcmp(token, "<<") == 0 ||
1217 strcmp(token, "&") == 0 ||
1218 strcmp(token, "|") == 0 ||
1219 strcmp(token, "&&") == 0 ||
1220 strcmp(token, "||") == 0 ||
1221 strcmp(token, "-") == 0 ||
1222 strcmp(token, "+") == 0 ||
1223 strcmp(token, "*") == 0 ||
1224 strcmp(token, "^") == 0 ||
1225 strcmp(token, "/") == 0 ||
Steven Rostedt298ebc32009-10-14 15:43:34 -04001226 strcmp(token, "<") == 0 ||
1227 strcmp(token, ">") == 0 ||
Steven Rostedtea4010d2009-08-17 16:18:07 +02001228 strcmp(token, "==") == 0 ||
1229 strcmp(token, "!=") == 0) {
1230
1231 left = malloc_or_die(sizeof(*left));
1232
1233 /* copy the top arg to the left */
1234 *left = *arg;
1235
1236 arg->type = PRINT_OP;
1237 arg->op.op = token;
1238 arg->op.left = left;
1239
1240 set_op_prio(arg);
1241
1242 right = malloc_or_die(sizeof(*right));
1243
Steven Rostedtb99af872009-10-14 15:43:36 -04001244 type = read_token_item(&token);
1245 *tok = token;
1246
1247 /* could just be a type pointer */
1248 if ((strcmp(arg->op.op, "*") == 0) &&
1249 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1250 if (left->type != PRINT_ATOM)
1251 die("bad pointer type");
1252 left->atom.atom = realloc(left->atom.atom,
1253 sizeof(left->atom.atom) + 3);
1254 strcat(left->atom.atom, " *");
1255 *arg = *left;
1256 free(arg);
1257
1258 return type;
1259 }
1260
1261 type = process_arg_token(event, right, tok, type);
Steven Rostedtea4010d2009-08-17 16:18:07 +02001262
1263 arg->op.right = right;
1264
Steven Rostedt0959b8d2009-10-14 15:43:35 -04001265 } else if (strcmp(token, "[") == 0) {
1266
1267 left = malloc_or_die(sizeof(*left));
1268 *left = *arg;
1269
1270 arg->type = PRINT_OP;
1271 arg->op.op = token;
1272 arg->op.left = left;
1273
1274 arg->op.prio = 0;
1275 type = process_array(event, arg, tok);
1276
Steven Rostedtea4010d2009-08-17 16:18:07 +02001277 } else {
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04001278 warning("unknown op '%s'", token);
1279 event->flags |= EVENT_FL_FAILED;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001280 /* the arg is now the left side */
1281 return EVENT_NONE;
1282 }
1283
Steven Rostedtea4010d2009-08-17 16:18:07 +02001284 if (type == EVENT_OP) {
1285 int prio;
1286
1287 /* higher prios need to be closer to the root */
1288 prio = get_op_prio(*tok);
1289
1290 if (prio > arg->op.prio)
1291 return process_op(event, arg, tok);
1292
1293 return process_op(event, right, tok);
1294 }
1295
1296 return type;
1297}
1298
1299static enum event_type
1300process_entry(struct event *event __unused, struct print_arg *arg,
1301 char **tok)
1302{
1303 enum event_type type;
1304 char *field;
1305 char *token;
1306
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001307 if (read_expected(EVENT_OP, "->") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001308 return EVENT_ERROR;
1309
1310 if (read_expect_type(EVENT_ITEM, &token) < 0)
1311 goto fail;
1312 field = token;
1313
1314 arg->type = PRINT_FIELD;
1315 arg->field.name = field;
1316
Tom Zanussieb9a42c2009-11-25 01:15:47 -06001317 if (is_flag_field) {
1318 arg->field.field = find_any_field(event, arg->field.name);
1319 arg->field.field->flags |= FIELD_IS_FLAG;
1320 is_flag_field = 0;
1321 } else if (is_symbolic_field) {
1322 arg->field.field = find_any_field(event, arg->field.name);
1323 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1324 is_symbolic_field = 0;
1325 }
1326
Steven Rostedtea4010d2009-08-17 16:18:07 +02001327 type = read_token(&token);
1328 *tok = token;
1329
1330 return type;
1331
1332fail:
1333 free_token(token);
1334 return EVENT_ERROR;
1335}
1336
1337static char *arg_eval (struct print_arg *arg);
1338
1339static long long arg_num_eval(struct print_arg *arg)
1340{
1341 long long left, right;
1342 long long val = 0;
1343
1344 switch (arg->type) {
1345 case PRINT_ATOM:
1346 val = strtoll(arg->atom.atom, NULL, 0);
1347 break;
1348 case PRINT_TYPE:
1349 val = arg_num_eval(arg->typecast.item);
1350 break;
1351 case PRINT_OP:
1352 switch (arg->op.op[0]) {
1353 case '|':
1354 left = arg_num_eval(arg->op.left);
1355 right = arg_num_eval(arg->op.right);
1356 if (arg->op.op[1])
1357 val = left || right;
1358 else
1359 val = left | right;
1360 break;
1361 case '&':
1362 left = arg_num_eval(arg->op.left);
1363 right = arg_num_eval(arg->op.right);
1364 if (arg->op.op[1])
1365 val = left && right;
1366 else
1367 val = left & right;
1368 break;
1369 case '<':
1370 left = arg_num_eval(arg->op.left);
1371 right = arg_num_eval(arg->op.right);
1372 switch (arg->op.op[1]) {
1373 case 0:
1374 val = left < right;
1375 break;
1376 case '<':
1377 val = left << right;
1378 break;
1379 case '=':
1380 val = left <= right;
1381 break;
1382 default:
1383 die("unknown op '%s'", arg->op.op);
1384 }
1385 break;
1386 case '>':
1387 left = arg_num_eval(arg->op.left);
1388 right = arg_num_eval(arg->op.right);
1389 switch (arg->op.op[1]) {
1390 case 0:
1391 val = left > right;
1392 break;
1393 case '>':
1394 val = left >> right;
1395 break;
1396 case '=':
1397 val = left >= right;
1398 break;
1399 default:
1400 die("unknown op '%s'", arg->op.op);
1401 }
1402 break;
1403 case '=':
1404 left = arg_num_eval(arg->op.left);
1405 right = arg_num_eval(arg->op.right);
1406
1407 if (arg->op.op[1] != '=')
1408 die("unknown op '%s'", arg->op.op);
1409
1410 val = left == right;
1411 break;
1412 case '!':
1413 left = arg_num_eval(arg->op.left);
1414 right = arg_num_eval(arg->op.right);
1415
1416 switch (arg->op.op[1]) {
1417 case '=':
1418 val = left != right;
1419 break;
1420 default:
1421 die("unknown op '%s'", arg->op.op);
1422 }
1423 break;
Stefan Hajnoczia5a178e2011-09-04 16:08:59 +01001424 case '+':
1425 left = arg_num_eval(arg->op.left);
1426 right = arg_num_eval(arg->op.right);
1427 val = left + right;
1428 break;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001429 default:
1430 die("unknown op '%s'", arg->op.op);
1431 }
1432 break;
1433
1434 case PRINT_NULL:
1435 case PRINT_FIELD ... PRINT_SYMBOL:
1436 case PRINT_STRING:
1437 default:
1438 die("invalid eval type %d", arg->type);
1439
1440 }
1441 return val;
1442}
1443
1444static char *arg_eval (struct print_arg *arg)
1445{
1446 long long val;
1447 static char buf[20];
1448
1449 switch (arg->type) {
1450 case PRINT_ATOM:
1451 return arg->atom.atom;
1452 case PRINT_TYPE:
1453 return arg_eval(arg->typecast.item);
1454 case PRINT_OP:
1455 val = arg_num_eval(arg);
1456 sprintf(buf, "%lld", val);
1457 return buf;
1458
1459 case PRINT_NULL:
1460 case PRINT_FIELD ... PRINT_SYMBOL:
1461 case PRINT_STRING:
1462 default:
1463 die("invalid eval type %d", arg->type);
1464 break;
1465 }
1466
1467 return NULL;
1468}
1469
1470static enum event_type
1471process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1472{
1473 enum event_type type;
1474 struct print_arg *arg = NULL;
1475 struct print_flag_sym *field;
1476 char *token = NULL;
1477 char *value;
1478
1479 do {
1480 free_token(token);
1481 type = read_token_item(&token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001482 if (test_type_token(type, token, EVENT_OP, "{"))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001483 break;
1484
1485 arg = malloc_or_die(sizeof(*arg));
1486
1487 free_token(token);
1488 type = process_arg(event, arg, &token);
Stefan Hajnoczia5a178e2011-09-04 16:08:59 +01001489
1490 if (type == EVENT_OP)
1491 type = process_op(event, arg, &token);
1492
1493 if (type == EVENT_ERROR)
1494 goto out_free;
1495
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001496 if (test_type_token(type, token, EVENT_DELIM, ","))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001497 goto out_free;
1498
1499 field = malloc_or_die(sizeof(*field));
Julia Lawall5660ce32009-12-09 20:26:18 +01001500 memset(field, 0, sizeof(*field));
Steven Rostedtea4010d2009-08-17 16:18:07 +02001501
1502 value = arg_eval(arg);
1503 field->value = strdup(value);
1504
1505 free_token(token);
1506 type = process_arg(event, arg, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001507 if (test_type_token(type, token, EVENT_OP, "}"))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001508 goto out_free;
1509
1510 value = arg_eval(arg);
1511 field->str = strdup(value);
1512 free_arg(arg);
1513 arg = NULL;
1514
1515 *list = field;
1516 list = &field->next;
1517
1518 free_token(token);
1519 type = read_token_item(&token);
1520 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1521
1522 *tok = token;
1523 return type;
1524
1525out_free:
1526 free_arg(arg);
1527 free_token(token);
1528
1529 return EVENT_ERROR;
1530}
1531
1532static enum event_type
1533process_flags(struct event *event, struct print_arg *arg, char **tok)
1534{
1535 struct print_arg *field;
1536 enum event_type type;
1537 char *token;
1538
1539 memset(arg, 0, sizeof(*arg));
1540 arg->type = PRINT_FLAGS;
1541
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001542 if (read_expected_item(EVENT_DELIM, "(") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001543 return EVENT_ERROR;
1544
1545 field = malloc_or_die(sizeof(*field));
1546
1547 type = process_arg(event, field, &token);
Steven Rostedt49908a12011-11-04 16:32:25 -04001548 while (type == EVENT_OP)
1549 type = process_op(event, field, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001550 if (test_type_token(type, token, EVENT_DELIM, ","))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001551 goto out_free;
1552
1553 arg->flags.field = field;
1554
1555 type = read_token_item(&token);
1556 if (event_item_type(type)) {
1557 arg->flags.delim = token;
1558 type = read_token_item(&token);
1559 }
1560
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001561 if (test_type_token(type, token, EVENT_DELIM, ","))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001562 goto out_free;
1563
1564 type = process_fields(event, &arg->flags.flags, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001565 if (test_type_token(type, token, EVENT_DELIM, ")"))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001566 goto out_free;
1567
1568 free_token(token);
1569 type = read_token_item(tok);
1570 return type;
1571
1572out_free:
1573 free_token(token);
1574 return EVENT_ERROR;
1575}
1576
1577static enum event_type
1578process_symbols(struct event *event, struct print_arg *arg, char **tok)
1579{
1580 struct print_arg *field;
1581 enum event_type type;
1582 char *token;
1583
1584 memset(arg, 0, sizeof(*arg));
1585 arg->type = PRINT_SYMBOL;
1586
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001587 if (read_expected_item(EVENT_DELIM, "(") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001588 return EVENT_ERROR;
1589
1590 field = malloc_or_die(sizeof(*field));
1591
1592 type = process_arg(event, field, &token);
Steven Rostedt24314962011-11-04 16:32:25 -04001593 while (type == EVENT_OP)
1594 type = process_op(event, field, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001595 if (test_type_token(type, token, EVENT_DELIM, ","))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001596 goto out_free;
1597
1598 arg->symbol.field = field;
1599
1600 type = process_fields(event, &arg->symbol.symbols, &token);
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001601 if (test_type_token(type, token, EVENT_DELIM, ")"))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001602 goto out_free;
1603
1604 free_token(token);
1605 type = read_token_item(tok);
1606 return type;
1607
1608out_free:
1609 free_token(token);
1610 return EVENT_ERROR;
1611}
1612
1613static enum event_type
1614process_paren(struct event *event, struct print_arg *arg, char **tok)
1615{
1616 struct print_arg *item_arg;
1617 enum event_type type;
1618 char *token;
1619
1620 type = process_arg(event, arg, &token);
1621
1622 if (type == EVENT_ERROR)
1623 return EVENT_ERROR;
1624
Steven Rostedtb99af872009-10-14 15:43:36 -04001625 if (type == EVENT_OP)
1626 type = process_op(event, arg, &token);
Steven Rostedtea4010d2009-08-17 16:18:07 +02001627
Steven Rostedtb99af872009-10-14 15:43:36 -04001628 if (type == EVENT_ERROR)
1629 return EVENT_ERROR;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001630
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001631 if (test_type_token(type, token, EVENT_DELIM, ")")) {
Steven Rostedtea4010d2009-08-17 16:18:07 +02001632 free_token(token);
1633 return EVENT_ERROR;
1634 }
1635
1636 free_token(token);
1637 type = read_token_item(&token);
1638
1639 /*
1640 * If the next token is an item or another open paren, then
1641 * this was a typecast.
1642 */
1643 if (event_item_type(type) ||
1644 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1645
1646 /* make this a typecast and contine */
1647
1648 /* prevous must be an atom */
1649 if (arg->type != PRINT_ATOM)
1650 die("previous needed to be PRINT_ATOM");
1651
1652 item_arg = malloc_or_die(sizeof(*item_arg));
1653
1654 arg->type = PRINT_TYPE;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001655 arg->typecast.type = arg->atom.atom;
1656 arg->typecast.item = item_arg;
1657 type = process_arg_token(event, item_arg, &token, type);
1658
1659 }
1660
1661 *tok = token;
1662 return type;
1663}
1664
1665
1666static enum event_type
1667process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1668{
1669 enum event_type type;
1670 char *token;
1671
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001672 if (read_expected(EVENT_DELIM, "(") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001673 return EVENT_ERROR;
1674
1675 if (read_expect_type(EVENT_ITEM, &token) < 0)
1676 goto fail;
1677
1678 arg->type = PRINT_STRING;
1679 arg->string.string = token;
Frederic Weisbecker561f7322009-08-31 06:45:21 +02001680 arg->string.offset = -1;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001681
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001682 if (read_expected(EVENT_DELIM, ")") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001683 return EVENT_ERROR;
1684
1685 type = read_token(&token);
1686 *tok = token;
1687
1688 return type;
1689fail:
1690 free_token(token);
1691 return EVENT_ERROR;
1692}
1693
1694enum event_type
1695process_arg_token(struct event *event, struct print_arg *arg,
1696 char **tok, enum event_type type)
1697{
1698 char *token;
1699 char *atom;
1700
1701 token = *tok;
1702
1703 switch (type) {
1704 case EVENT_ITEM:
1705 if (strcmp(token, "REC") == 0) {
1706 free_token(token);
1707 type = process_entry(event, arg, &token);
1708 } else if (strcmp(token, "__print_flags") == 0) {
1709 free_token(token);
Tom Zanussieb9a42c2009-11-25 01:15:47 -06001710 is_flag_field = 1;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001711 type = process_flags(event, arg, &token);
1712 } else if (strcmp(token, "__print_symbolic") == 0) {
1713 free_token(token);
Tom Zanussieb9a42c2009-11-25 01:15:47 -06001714 is_symbolic_field = 1;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001715 type = process_symbols(event, arg, &token);
1716 } else if (strcmp(token, "__get_str") == 0) {
1717 free_token(token);
1718 type = process_str(event, arg, &token);
1719 } else {
1720 atom = token;
1721 /* test the next token */
1722 type = read_token_item(&token);
1723
1724 /* atoms can be more than one token long */
1725 while (type == EVENT_ITEM) {
1726 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1727 strcat(atom, " ");
1728 strcat(atom, token);
1729 free_token(token);
1730 type = read_token_item(&token);
1731 }
1732
1733 /* todo, test for function */
1734
1735 arg->type = PRINT_ATOM;
1736 arg->atom.atom = atom;
1737 }
1738 break;
1739 case EVENT_DQUOTE:
1740 case EVENT_SQUOTE:
1741 arg->type = PRINT_ATOM;
1742 arg->atom.atom = token;
1743 type = read_token_item(&token);
1744 break;
1745 case EVENT_DELIM:
1746 if (strcmp(token, "(") == 0) {
1747 free_token(token);
1748 type = process_paren(event, arg, &token);
1749 break;
1750 }
1751 case EVENT_OP:
1752 /* handle single ops */
1753 arg->type = PRINT_OP;
1754 arg->op.op = token;
1755 arg->op.left = NULL;
1756 type = process_op(event, arg, &token);
1757
1758 break;
1759
1760 case EVENT_ERROR ... EVENT_NEWLINE:
1761 default:
1762 die("unexpected type %d", type);
1763 }
1764 *tok = token;
1765
1766 return type;
1767}
1768
1769static int event_read_print_args(struct event *event, struct print_arg **list)
1770{
Steven Rostedtf1d1fee2009-10-14 15:43:37 -04001771 enum event_type type = EVENT_ERROR;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001772 struct print_arg *arg;
1773 char *token;
1774 int args = 0;
1775
1776 do {
Steven Rostedtf1d1fee2009-10-14 15:43:37 -04001777 if (type == EVENT_NEWLINE) {
1778 free_token(token);
1779 type = read_token_item(&token);
1780 continue;
1781 }
1782
Steven Rostedtea4010d2009-08-17 16:18:07 +02001783 arg = malloc_or_die(sizeof(*arg));
1784 memset(arg, 0, sizeof(*arg));
1785
1786 type = process_arg(event, arg, &token);
1787
1788 if (type == EVENT_ERROR) {
1789 free_arg(arg);
1790 return -1;
1791 }
1792
1793 *list = arg;
1794 args++;
1795
1796 if (type == EVENT_OP) {
1797 type = process_op(event, arg, &token);
1798 list = &arg->next;
1799 continue;
1800 }
1801
1802 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1803 free_token(token);
1804 *list = arg;
1805 list = &arg->next;
1806 continue;
1807 }
1808 break;
1809 } while (type != EVENT_NONE);
1810
1811 if (type != EVENT_NONE)
1812 free_token(token);
1813
1814 return args;
1815}
1816
1817static int event_read_print(struct event *event)
1818{
1819 enum event_type type;
1820 char *token;
1821 int ret;
1822
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001823 if (read_expected_item(EVENT_ITEM, "print") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001824 return -1;
1825
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001826 if (read_expected(EVENT_ITEM, "fmt") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001827 return -1;
1828
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001829 if (read_expected(EVENT_OP, ":") < 0)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001830 return -1;
1831
1832 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1833 goto fail;
1834
Steven Rostedt924a79a2009-10-14 15:43:32 -04001835 concat:
Steven Rostedtea4010d2009-08-17 16:18:07 +02001836 event->print_fmt.format = token;
1837 event->print_fmt.args = NULL;
1838
1839 /* ok to have no arg */
1840 type = read_token_item(&token);
1841
1842 if (type == EVENT_NONE)
1843 return 0;
1844
Steven Rostedt924a79a2009-10-14 15:43:32 -04001845 /* Handle concatination of print lines */
1846 if (type == EVENT_DQUOTE) {
1847 char *cat;
1848
1849 cat = malloc_or_die(strlen(event->print_fmt.format) +
1850 strlen(token) + 1);
1851 strcpy(cat, event->print_fmt.format);
1852 strcat(cat, token);
1853 free_token(token);
1854 free_token(event->print_fmt.format);
1855 event->print_fmt.format = NULL;
1856 token = cat;
1857 goto concat;
1858 }
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001859
1860 if (test_type_token(type, token, EVENT_DELIM, ","))
Steven Rostedtea4010d2009-08-17 16:18:07 +02001861 goto fail;
1862
1863 free_token(token);
1864
1865 ret = event_read_print_args(event, &event->print_fmt.args);
1866 if (ret < 0)
1867 return -1;
1868
Steven Rostedt0d1da912009-10-14 15:43:41 -04001869 return ret;
Steven Rostedtea4010d2009-08-17 16:18:07 +02001870
1871 fail:
1872 free_token(token);
1873 return -1;
1874}
1875
1876static struct format_field *
1877find_common_field(struct event *event, const char *name)
1878{
1879 struct format_field *format;
1880
1881 for (format = event->format.common_fields;
1882 format; format = format->next) {
1883 if (strcmp(format->name, name) == 0)
1884 break;
1885 }
1886
1887 return format;
1888}
1889
1890static struct format_field *
1891find_field(struct event *event, const char *name)
1892{
1893 struct format_field *format;
1894
1895 for (format = event->format.fields;
1896 format; format = format->next) {
1897 if (strcmp(format->name, name) == 0)
1898 break;
1899 }
1900
1901 return format;
1902}
1903
1904static struct format_field *
1905find_any_field(struct event *event, const char *name)
1906{
1907 struct format_field *format;
1908
1909 format = find_common_field(event, name);
1910 if (format)
1911 return format;
1912 return find_field(event, name);
1913}
1914
Tom Zanussi16c632d2009-11-25 01:15:48 -06001915unsigned long long read_size(void *ptr, int size)
Steven Rostedtea4010d2009-08-17 16:18:07 +02001916{
1917 switch (size) {
1918 case 1:
1919 return *(unsigned char *)ptr;
1920 case 2:
1921 return data2host2(ptr);
1922 case 4:
1923 return data2host4(ptr);
1924 case 8:
1925 return data2host8(ptr);
1926 default:
1927 /* BUG! */
1928 return 0;
1929 }
1930}
1931
Frederic Weisbecker46538812009-09-12 02:43:45 +02001932unsigned long long
1933raw_field_value(struct event *event, const char *name, void *data)
1934{
1935 struct format_field *field;
1936
1937 field = find_any_field(event, name);
1938 if (!field)
1939 return 0ULL;
1940
1941 return read_size(data + field->offset, field->size);
1942}
1943
1944void *raw_field_ptr(struct event *event, const char *name, void *data)
1945{
1946 struct format_field *field;
1947
1948 field = find_any_field(event, name);
1949 if (!field)
1950 return NULL;
1951
Frederic Weisbeckerde068ec2010-05-05 22:07:39 +02001952 if (field->flags & FIELD_IS_DYNAMIC) {
Hitoshi Mitake86d8d292010-01-30 20:43:23 +09001953 int offset;
1954
1955 offset = *(int *)(data + field->offset);
1956 offset &= 0xffff;
1957
1958 return data + offset;
1959 }
1960
Frederic Weisbecker46538812009-09-12 02:43:45 +02001961 return data + field->offset;
1962}
1963
Steven Rostedtea4010d2009-08-17 16:18:07 +02001964static int get_common_info(const char *type, int *offset, int *size)
1965{
1966 struct event *event;
1967 struct format_field *field;
1968
1969 /*
1970 * All events should have the same common elements.
1971 * Pick any event to find where the type is;
1972 */
1973 if (!event_list)
1974 die("no event_list!");
1975
1976 event = event_list;
1977 field = find_common_field(event, type);
1978 if (!field)
1979 die("field '%s' not found", type);
1980
1981 *offset = field->offset;
1982 *size = field->size;
1983
1984 return 0;
1985}
1986
Steven Rostedtcda48462009-10-14 15:43:42 -04001987static int __parse_common(void *data, int *size, int *offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04001988 const char *name)
Steven Rostedtcda48462009-10-14 15:43:42 -04001989{
1990 int ret;
1991
1992 if (!*size) {
1993 ret = get_common_info(name, offset, size);
1994 if (ret < 0)
1995 return ret;
1996 }
1997 return read_size(data + *offset, *size);
1998}
1999
Ingo Molnarec156762009-09-11 12:12:54 +02002000int trace_parse_common_type(void *data)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002001{
2002 static int type_offset;
2003 static int type_size;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002004
Steven Rostedtcda48462009-10-14 15:43:42 -04002005 return __parse_common(data, &type_size, &type_offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04002006 "common_type");
Steven Rostedtea4010d2009-08-17 16:18:07 +02002007}
2008
Tom Zanussi16c632d2009-11-25 01:15:48 -06002009int trace_parse_common_pid(void *data)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002010{
2011 static int pid_offset;
2012 static int pid_size;
Steven Rostedtcda48462009-10-14 15:43:42 -04002013
2014 return __parse_common(data, &pid_size, &pid_offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04002015 "common_pid");
Steven Rostedtcda48462009-10-14 15:43:42 -04002016}
2017
Tom Zanussid1b93772009-11-25 01:15:50 -06002018int parse_common_pc(void *data)
Steven Rostedtcda48462009-10-14 15:43:42 -04002019{
2020 static int pc_offset;
2021 static int pc_size;
2022
2023 return __parse_common(data, &pc_size, &pc_offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04002024 "common_preempt_count");
Steven Rostedtcda48462009-10-14 15:43:42 -04002025}
2026
Tom Zanussid1b93772009-11-25 01:15:50 -06002027int parse_common_flags(void *data)
Steven Rostedtcda48462009-10-14 15:43:42 -04002028{
2029 static int flags_offset;
2030 static int flags_size;
2031
2032 return __parse_common(data, &flags_size, &flags_offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04002033 "common_flags");
Steven Rostedtcda48462009-10-14 15:43:42 -04002034}
2035
Tom Zanussid1b93772009-11-25 01:15:50 -06002036int parse_common_lock_depth(void *data)
Steven Rostedtcda48462009-10-14 15:43:42 -04002037{
2038 static int ld_offset;
2039 static int ld_size;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002040 int ret;
2041
Steven Rostedtcda48462009-10-14 15:43:42 -04002042 ret = __parse_common(data, &ld_size, &ld_offset,
Steven Rostedtc4dc7752009-10-14 15:43:44 -04002043 "common_lock_depth");
Steven Rostedtcda48462009-10-14 15:43:42 -04002044 if (ret < 0)
2045 return -1;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002046
Steven Rostedtcda48462009-10-14 15:43:42 -04002047 return ret;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002048}
2049
Ingo Molnarec156762009-09-11 12:12:54 +02002050struct event *trace_find_event(int id)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002051{
2052 struct event *event;
2053
2054 for (event = event_list; event; event = event->next) {
2055 if (event->id == id)
2056 break;
2057 }
2058 return event;
2059}
2060
Tom Zanussi16c632d2009-11-25 01:15:48 -06002061struct event *trace_find_next_event(struct event *event)
2062{
2063 if (!event)
2064 return event_list;
2065
2066 return event->next;
2067}
2068
Steven Rostedtea4010d2009-08-17 16:18:07 +02002069static unsigned long long eval_num_arg(void *data, int size,
2070 struct event *event, struct print_arg *arg)
2071{
2072 unsigned long long val = 0;
2073 unsigned long long left, right;
Steven Rostedt0959b8d2009-10-14 15:43:35 -04002074 struct print_arg *larg;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002075
2076 switch (arg->type) {
2077 case PRINT_NULL:
2078 /* ?? */
2079 return 0;
2080 case PRINT_ATOM:
2081 return strtoull(arg->atom.atom, NULL, 0);
2082 case PRINT_FIELD:
2083 if (!arg->field.field) {
2084 arg->field.field = find_any_field(event, arg->field.name);
2085 if (!arg->field.field)
2086 die("field %s not found", arg->field.name);
2087 }
2088 /* must be a number */
2089 val = read_size(data + arg->field.field->offset,
2090 arg->field.field->size);
2091 break;
2092 case PRINT_FLAGS:
2093 case PRINT_SYMBOL:
2094 break;
2095 case PRINT_TYPE:
2096 return eval_num_arg(data, size, event, arg->typecast.item);
2097 case PRINT_STRING:
2098 return 0;
2099 break;
2100 case PRINT_OP:
Steven Rostedt0959b8d2009-10-14 15:43:35 -04002101 if (strcmp(arg->op.op, "[") == 0) {
2102 /*
2103 * Arrays are special, since we don't want
2104 * to read the arg as is.
2105 */
2106 if (arg->op.left->type != PRINT_FIELD)
2107 goto default_op; /* oops, all bets off */
2108 larg = arg->op.left;
2109 if (!larg->field.field) {
2110 larg->field.field =
2111 find_any_field(event, larg->field.name);
2112 if (!larg->field.field)
2113 die("field %s not found", larg->field.name);
2114 }
2115 right = eval_num_arg(data, size, event, arg->op.right);
2116 val = read_size(data + larg->field.field->offset +
2117 right * long_size, long_size);
2118 break;
2119 }
2120 default_op:
Steven Rostedtea4010d2009-08-17 16:18:07 +02002121 left = eval_num_arg(data, size, event, arg->op.left);
2122 right = eval_num_arg(data, size, event, arg->op.right);
2123 switch (arg->op.op[0]) {
2124 case '|':
2125 if (arg->op.op[1])
2126 val = left || right;
2127 else
2128 val = left | right;
2129 break;
2130 case '&':
2131 if (arg->op.op[1])
2132 val = left && right;
2133 else
2134 val = left & right;
2135 break;
2136 case '<':
2137 switch (arg->op.op[1]) {
2138 case 0:
2139 val = left < right;
2140 break;
2141 case '<':
2142 val = left << right;
2143 break;
2144 case '=':
2145 val = left <= right;
2146 break;
2147 default:
2148 die("unknown op '%s'", arg->op.op);
2149 }
2150 break;
2151 case '>':
2152 switch (arg->op.op[1]) {
2153 case 0:
2154 val = left > right;
2155 break;
2156 case '>':
2157 val = left >> right;
2158 break;
2159 case '=':
2160 val = left >= right;
2161 break;
2162 default:
2163 die("unknown op '%s'", arg->op.op);
2164 }
2165 break;
2166 case '=':
2167 if (arg->op.op[1] != '=')
2168 die("unknown op '%s'", arg->op.op);
2169 val = left == right;
2170 break;
Steven Rostedtafdf1a42009-10-14 15:43:43 -04002171 case '-':
2172 val = left - right;
2173 break;
2174 case '+':
2175 val = left + right;
2176 break;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002177 default:
2178 die("unknown op '%s'", arg->op.op);
2179 }
2180 break;
2181 default: /* not sure what to do there */
2182 return 0;
2183 }
2184 return val;
2185}
2186
2187struct flag {
2188 const char *name;
2189 unsigned long long value;
2190};
2191
2192static const struct flag flags[] = {
2193 { "HI_SOFTIRQ", 0 },
2194 { "TIMER_SOFTIRQ", 1 },
2195 { "NET_TX_SOFTIRQ", 2 },
2196 { "NET_RX_SOFTIRQ", 3 },
2197 { "BLOCK_SOFTIRQ", 4 },
Tom Zanussib934cdd2009-10-06 01:00:48 -05002198 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2199 { "TASKLET_SOFTIRQ", 6 },
2200 { "SCHED_SOFTIRQ", 7 },
2201 { "HRTIMER_SOFTIRQ", 8 },
Shaohua Li09223372011-06-14 13:26:25 +08002202 { "RCU_SOFTIRQ", 9 },
Steven Rostedtea4010d2009-08-17 16:18:07 +02002203
2204 { "HRTIMER_NORESTART", 0 },
2205 { "HRTIMER_RESTART", 1 },
2206};
2207
Tom Zanussi16c632d2009-11-25 01:15:48 -06002208unsigned long long eval_flag(const char *flag)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002209{
2210 int i;
2211
2212 /*
2213 * Some flags in the format files do not get converted.
2214 * If the flag is not numeric, see if it is something that
2215 * we already know about.
2216 */
2217 if (isdigit(flag[0]))
2218 return strtoull(flag, NULL, 0);
2219
2220 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2221 if (strcmp(flags[i].name, flag) == 0)
2222 return flags[i].value;
2223
2224 return 0;
2225}
2226
2227static void print_str_arg(void *data, int size,
2228 struct event *event, struct print_arg *arg)
2229{
2230 struct print_flag_sym *flag;
2231 unsigned long long val, fval;
2232 char *str;
2233 int print;
2234
2235 switch (arg->type) {
2236 case PRINT_NULL:
2237 /* ?? */
2238 return;
2239 case PRINT_ATOM:
2240 printf("%s", arg->atom.atom);
2241 return;
2242 case PRINT_FIELD:
2243 if (!arg->field.field) {
2244 arg->field.field = find_any_field(event, arg->field.name);
2245 if (!arg->field.field)
2246 die("field %s not found", arg->field.name);
2247 }
2248 str = malloc_or_die(arg->field.field->size + 1);
2249 memcpy(str, data + arg->field.field->offset,
2250 arg->field.field->size);
2251 str[arg->field.field->size] = 0;
Frederic Weisbeckerd498bc12009-08-28 04:46:07 +02002252 printf("%s", str);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002253 free(str);
2254 break;
2255 case PRINT_FLAGS:
2256 val = eval_num_arg(data, size, event, arg->flags.field);
2257 print = 0;
2258 for (flag = arg->flags.flags; flag; flag = flag->next) {
2259 fval = eval_flag(flag->value);
2260 if (!val && !fval) {
2261 printf("%s", flag->str);
2262 break;
2263 }
2264 if (fval && (val & fval) == fval) {
2265 if (print && arg->flags.delim)
2266 printf("%s", arg->flags.delim);
2267 printf("%s", flag->str);
2268 print = 1;
2269 val &= ~fval;
2270 }
2271 }
2272 break;
2273 case PRINT_SYMBOL:
2274 val = eval_num_arg(data, size, event, arg->symbol.field);
2275 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2276 fval = eval_flag(flag->value);
2277 if (val == fval) {
2278 printf("%s", flag->str);
2279 break;
2280 }
2281 }
2282 break;
2283
2284 case PRINT_TYPE:
2285 break;
Frederic Weisbecker561f7322009-08-31 06:45:21 +02002286 case PRINT_STRING: {
2287 int str_offset;
2288
2289 if (arg->string.offset == -1) {
2290 struct format_field *f;
2291
2292 f = find_any_field(event, arg->string.string);
2293 arg->string.offset = f->offset;
2294 }
2295 str_offset = *(int *)(data + arg->string.offset);
2296 str_offset &= 0xffff;
2297 printf("%s", ((char *)data) + str_offset);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002298 break;
Frederic Weisbecker561f7322009-08-31 06:45:21 +02002299 }
Steven Rostedtea4010d2009-08-17 16:18:07 +02002300 case PRINT_OP:
2301 /*
2302 * The only op for string should be ? :
2303 */
2304 if (arg->op.op[0] != '?')
2305 return;
2306 val = eval_num_arg(data, size, event, arg->op.left);
2307 if (val)
2308 print_str_arg(data, size, event, arg->op.right->op.left);
2309 else
2310 print_str_arg(data, size, event, arg->op.right->op.right);
2311 break;
2312 default:
2313 /* well... */
2314 break;
2315 }
2316}
2317
2318static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2319{
2320 static struct format_field *field, *ip_field;
2321 struct print_arg *args, *arg, **next;
2322 unsigned long long ip, val;
2323 char *ptr;
2324 void *bptr;
2325
2326 if (!field) {
2327 field = find_field(event, "buf");
2328 if (!field)
2329 die("can't find buffer field for binary printk");
2330 ip_field = find_field(event, "ip");
2331 if (!ip_field)
2332 die("can't find ip field for binary printk");
2333 }
2334
2335 ip = read_size(data + ip_field->offset, ip_field->size);
2336
2337 /*
2338 * The first arg is the IP pointer.
2339 */
2340 args = malloc_or_die(sizeof(*args));
2341 arg = args;
2342 arg->next = NULL;
2343 next = &arg->next;
2344
2345 arg->type = PRINT_ATOM;
2346 arg->atom.atom = malloc_or_die(32);
2347 sprintf(arg->atom.atom, "%lld", ip);
2348
2349 /* skip the first "%pf : " */
2350 for (ptr = fmt + 6, bptr = data + field->offset;
2351 bptr < data + size && *ptr; ptr++) {
2352 int ls = 0;
2353
2354 if (*ptr == '%') {
2355 process_again:
2356 ptr++;
2357 switch (*ptr) {
2358 case '%':
2359 break;
2360 case 'l':
2361 ls++;
2362 goto process_again;
2363 case 'L':
2364 ls = 2;
2365 goto process_again;
2366 case '0' ... '9':
2367 goto process_again;
2368 case 'p':
2369 ls = 1;
2370 /* fall through */
2371 case 'd':
2372 case 'u':
2373 case 'x':
2374 case 'i':
Steven Rostedtffa18952009-10-14 15:43:40 -04002375 /* the pointers are always 4 bytes aligned */
2376 bptr = (void *)(((unsigned long)bptr + 3) &
2377 ~3);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002378 switch (ls) {
2379 case 0:
2380 case 1:
2381 ls = long_size;
2382 break;
2383 case 2:
2384 ls = 8;
2385 default:
2386 break;
2387 }
2388 val = read_size(bptr, ls);
2389 bptr += ls;
2390 arg = malloc_or_die(sizeof(*arg));
2391 arg->next = NULL;
2392 arg->type = PRINT_ATOM;
2393 arg->atom.atom = malloc_or_die(32);
2394 sprintf(arg->atom.atom, "%lld", val);
2395 *next = arg;
2396 next = &arg->next;
2397 break;
2398 case 's':
2399 arg = malloc_or_die(sizeof(*arg));
2400 arg->next = NULL;
2401 arg->type = PRINT_STRING;
2402 arg->string.string = strdup(bptr);
2403 bptr += strlen(bptr) + 1;
2404 *next = arg;
2405 next = &arg->next;
2406 default:
2407 break;
2408 }
2409 }
2410 }
2411
2412 return args;
2413}
2414
2415static void free_args(struct print_arg *args)
2416{
2417 struct print_arg *next;
2418
2419 while (args) {
2420 next = args->next;
2421
2422 if (args->type == PRINT_ATOM)
2423 free(args->atom.atom);
2424 else
2425 free(args->string.string);
2426 free(args);
2427 args = next;
2428 }
2429}
2430
2431static char *get_bprint_format(void *data, int size __unused, struct event *event)
2432{
2433 unsigned long long addr;
2434 static struct format_field *field;
2435 struct printk_map *printk;
2436 char *format;
2437 char *p;
2438
2439 if (!field) {
2440 field = find_field(event, "fmt");
2441 if (!field)
2442 die("can't find format field for binary printk");
2443 printf("field->offset = %d size=%d\n", field->offset, field->size);
2444 }
2445
2446 addr = read_size(data + field->offset, field->size);
2447
2448 printk = find_printk(addr);
2449 if (!printk) {
2450 format = malloc_or_die(45);
2451 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2452 addr);
2453 return format;
2454 }
2455
2456 p = printk->printk;
2457 /* Remove any quotes. */
2458 if (*p == '"')
2459 p++;
2460 format = malloc_or_die(strlen(p) + 10);
2461 sprintf(format, "%s : %s", "%pf", p);
2462 /* remove ending quotes and new line since we will add one too */
2463 p = format + strlen(format) - 1;
2464 if (*p == '"')
2465 *p = 0;
2466
2467 p -= 2;
2468 if (strcmp(p, "\\n") == 0)
2469 *p = 0;
2470
2471 return format;
2472}
2473
2474static void pretty_print(void *data, int size, struct event *event)
2475{
2476 struct print_fmt *print_fmt = &event->print_fmt;
2477 struct print_arg *arg = print_fmt->args;
2478 struct print_arg *args = NULL;
2479 const char *ptr = print_fmt->format;
2480 unsigned long long val;
2481 struct func_map *func;
2482 const char *saveptr;
2483 char *bprint_fmt = NULL;
2484 char format[32];
2485 int show_func;
2486 int len;
2487 int ls;
2488
2489 if (event->flags & EVENT_FL_ISFUNC)
2490 ptr = " %pF <-- %pF";
2491
2492 if (event->flags & EVENT_FL_ISBPRINT) {
2493 bprint_fmt = get_bprint_format(data, size, event);
2494 args = make_bprint_args(bprint_fmt, data, size, event);
2495 arg = args;
2496 ptr = bprint_fmt;
2497 }
2498
2499 for (; *ptr; ptr++) {
2500 ls = 0;
Steven Rostedt91ff2bc2009-10-14 15:43:33 -04002501 if (*ptr == '\\') {
2502 ptr++;
2503 switch (*ptr) {
2504 case 'n':
2505 printf("\n");
2506 break;
2507 case 't':
2508 printf("\t");
2509 break;
2510 case 'r':
2511 printf("\r");
2512 break;
2513 case '\\':
2514 printf("\\");
2515 break;
2516 default:
2517 printf("%c", *ptr);
2518 break;
2519 }
2520
2521 } else if (*ptr == '%') {
Steven Rostedtea4010d2009-08-17 16:18:07 +02002522 saveptr = ptr;
2523 show_func = 0;
2524 cont_process:
2525 ptr++;
2526 switch (*ptr) {
2527 case '%':
2528 printf("%%");
2529 break;
2530 case 'l':
2531 ls++;
2532 goto cont_process;
2533 case 'L':
2534 ls = 2;
2535 goto cont_process;
2536 case 'z':
2537 case 'Z':
2538 case '0' ... '9':
2539 goto cont_process;
2540 case 'p':
2541 if (long_size == 4)
2542 ls = 1;
2543 else
2544 ls = 2;
2545
2546 if (*(ptr+1) == 'F' ||
2547 *(ptr+1) == 'f') {
2548 ptr++;
2549 show_func = *ptr;
2550 }
2551
2552 /* fall through */
2553 case 'd':
2554 case 'i':
2555 case 'x':
2556 case 'X':
2557 case 'u':
2558 if (!arg)
2559 die("no argument match");
2560
2561 len = ((unsigned long)ptr + 1) -
2562 (unsigned long)saveptr;
2563
2564 /* should never happen */
2565 if (len > 32)
2566 die("bad format!");
2567
2568 memcpy(format, saveptr, len);
2569 format[len] = 0;
2570
2571 val = eval_num_arg(data, size, event, arg);
2572 arg = arg->next;
2573
2574 if (show_func) {
2575 func = find_func(val);
2576 if (func) {
2577 printf("%s", func->func);
2578 if (show_func == 'F')
2579 printf("+0x%llx",
2580 val - func->addr);
2581 break;
2582 }
2583 }
2584 switch (ls) {
2585 case 0:
2586 printf(format, (int)val);
2587 break;
2588 case 1:
2589 printf(format, (long)val);
2590 break;
2591 case 2:
2592 printf(format, (long long)val);
2593 break;
2594 default:
2595 die("bad count (%d)", ls);
2596 }
2597 break;
2598 case 's':
2599 if (!arg)
2600 die("no matching argument");
2601
2602 print_str_arg(data, size, event, arg);
2603 arg = arg->next;
2604 break;
2605 default:
2606 printf(">%c<", *ptr);
2607
2608 }
2609 } else
2610 printf("%c", *ptr);
2611 }
2612
2613 if (args) {
2614 free_args(args);
2615 free(bprint_fmt);
2616 }
2617}
2618
2619static inline int log10_cpu(int nb)
2620{
2621 if (nb / 100)
2622 return 3;
2623 if (nb / 10)
2624 return 2;
2625 return 1;
2626}
2627
Steven Rostedtcda48462009-10-14 15:43:42 -04002628static void print_lat_fmt(void *data, int size __unused)
2629{
2630 unsigned int lat_flags;
2631 unsigned int pc;
2632 int lock_depth;
2633 int hardirq;
2634 int softirq;
2635
2636 lat_flags = parse_common_flags(data);
2637 pc = parse_common_pc(data);
2638 lock_depth = parse_common_lock_depth(data);
2639
2640 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2641 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2642
2643 printf("%c%c%c",
2644 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2645 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2646 'X' : '.',
2647 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2648 'N' : '.',
2649 (hardirq && softirq) ? 'H' :
2650 hardirq ? 'h' : softirq ? 's' : '.');
2651
2652 if (pc)
2653 printf("%x", pc);
2654 else
2655 printf(".");
2656
2657 if (lock_depth < 0)
David Ahernc70c94b2011-03-09 22:23:25 -07002658 printf(". ");
Steven Rostedtcda48462009-10-14 15:43:42 -04002659 else
David Ahernc70c94b2011-03-09 22:23:25 -07002660 printf("%d ", lock_depth);
Steven Rostedtcda48462009-10-14 15:43:42 -04002661}
2662
Steven Rostedtea4010d2009-08-17 16:18:07 +02002663#define TRACE_GRAPH_INDENT 2
2664
Steven Rostedtea4010d2009-08-17 16:18:07 +02002665static struct record *
2666get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2667 struct record *next)
2668{
2669 struct format_field *field;
2670 struct event *event;
2671 unsigned long val;
2672 int type;
2673 int pid;
2674
Ingo Molnarec156762009-09-11 12:12:54 +02002675 type = trace_parse_common_type(next->data);
2676 event = trace_find_event(type);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002677 if (!event)
2678 return NULL;
2679
2680 if (!(event->flags & EVENT_FL_ISFUNCRET))
2681 return NULL;
2682
Tom Zanussi16c632d2009-11-25 01:15:48 -06002683 pid = trace_parse_common_pid(next->data);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002684 field = find_field(event, "func");
2685 if (!field)
2686 die("function return does not have field func");
2687
2688 val = read_size(next->data + field->offset, field->size);
2689
2690 if (cur_pid != pid || cur_func != val)
2691 return NULL;
2692
2693 /* this is a leaf, now advance the iterator */
2694 return trace_read_data(cpu);
2695}
2696
2697/* Signal a overhead of time execution to the output */
2698static void print_graph_overhead(unsigned long long duration)
2699{
2700 /* Non nested entry or return */
2701 if (duration == ~0ULL)
2702 return (void)printf(" ");
2703
2704 /* Duration exceeded 100 msecs */
2705 if (duration > 100000ULL)
2706 return (void)printf("! ");
2707
2708 /* Duration exceeded 10 msecs */
2709 if (duration > 10000ULL)
2710 return (void)printf("+ ");
2711
2712 printf(" ");
2713}
2714
2715static void print_graph_duration(unsigned long long duration)
2716{
2717 unsigned long usecs = duration / 1000;
2718 unsigned long nsecs_rem = duration % 1000;
2719 /* log10(ULONG_MAX) + '\0' */
2720 char msecs_str[21];
2721 char nsecs_str[5];
2722 int len;
2723 int i;
2724
2725 sprintf(msecs_str, "%lu", usecs);
2726
2727 /* Print msecs */
2728 len = printf("%lu", usecs);
2729
2730 /* Print nsecs (we don't want to exceed 7 numbers) */
2731 if (len < 7) {
2732 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2733 len += printf(".%s", nsecs_str);
2734 }
2735
2736 printf(" us ");
2737
2738 /* Print remaining spaces to fit the row's width */
2739 for (i = len; i < 7; i++)
2740 printf(" ");
2741
2742 printf("| ");
2743}
2744
2745static void
2746print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2747{
2748 unsigned long long rettime, calltime;
2749 unsigned long long duration, depth;
2750 unsigned long long val;
2751 struct format_field *field;
2752 struct func_map *func;
2753 struct event *ret_event;
2754 int type;
2755 int i;
2756
Ingo Molnarec156762009-09-11 12:12:54 +02002757 type = trace_parse_common_type(ret_rec->data);
2758 ret_event = trace_find_event(type);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002759
2760 field = find_field(ret_event, "rettime");
2761 if (!field)
2762 die("can't find rettime in return graph");
2763 rettime = read_size(ret_rec->data + field->offset, field->size);
2764
2765 field = find_field(ret_event, "calltime");
2766 if (!field)
2767 die("can't find rettime in return graph");
2768 calltime = read_size(ret_rec->data + field->offset, field->size);
2769
2770 duration = rettime - calltime;
2771
2772 /* Overhead */
2773 print_graph_overhead(duration);
2774
2775 /* Duration */
2776 print_graph_duration(duration);
2777
2778 field = find_field(event, "depth");
2779 if (!field)
2780 die("can't find depth in entry graph");
2781 depth = read_size(data + field->offset, field->size);
2782
2783 /* Function */
2784 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2785 printf(" ");
2786
2787 field = find_field(event, "func");
2788 if (!field)
2789 die("can't find func in entry graph");
2790 val = read_size(data + field->offset, field->size);
2791 func = find_func(val);
2792
2793 if (func)
2794 printf("%s();", func->func);
2795 else
2796 printf("%llx();", val);
2797}
2798
2799static void print_graph_nested(struct event *event, void *data)
2800{
2801 struct format_field *field;
2802 unsigned long long depth;
2803 unsigned long long val;
2804 struct func_map *func;
2805 int i;
2806
2807 /* No overhead */
2808 print_graph_overhead(-1);
2809
2810 /* No time */
2811 printf(" | ");
2812
2813 field = find_field(event, "depth");
2814 if (!field)
2815 die("can't find depth in entry graph");
2816 depth = read_size(data + field->offset, field->size);
2817
2818 /* Function */
2819 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2820 printf(" ");
2821
2822 field = find_field(event, "func");
2823 if (!field)
2824 die("can't find func in entry graph");
2825 val = read_size(data + field->offset, field->size);
2826 func = find_func(val);
2827
2828 if (func)
2829 printf("%s() {", func->func);
2830 else
2831 printf("%llx() {", val);
2832}
2833
2834static void
2835pretty_print_func_ent(void *data, int size, struct event *event,
David Ahernc70c94b2011-03-09 22:23:25 -07002836 int cpu, int pid)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002837{
2838 struct format_field *field;
2839 struct record *rec;
2840 void *copy_data;
2841 unsigned long val;
2842
Steven Rostedtcda48462009-10-14 15:43:42 -04002843 if (latency_format) {
2844 print_lat_fmt(data, size);
2845 printf(" | ");
2846 }
2847
Steven Rostedtea4010d2009-08-17 16:18:07 +02002848 field = find_field(event, "func");
2849 if (!field)
2850 die("function entry does not have func field");
2851
2852 val = read_size(data + field->offset, field->size);
2853
2854 /*
2855 * peek_data may unmap the data pointer. Copy it first.
2856 */
2857 copy_data = malloc_or_die(size);
2858 memcpy(copy_data, data, size);
2859 data = copy_data;
2860
2861 rec = trace_peek_data(cpu);
2862 if (rec) {
2863 rec = get_return_for_leaf(cpu, pid, val, rec);
2864 if (rec) {
2865 print_graph_entry_leaf(event, data, rec);
2866 goto out_free;
2867 }
2868 }
2869 print_graph_nested(event, data);
2870out_free:
2871 free(data);
2872}
2873
2874static void
David Ahernc70c94b2011-03-09 22:23:25 -07002875pretty_print_func_ret(void *data, int size __unused, struct event *event)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002876{
2877 unsigned long long rettime, calltime;
2878 unsigned long long duration, depth;
2879 struct format_field *field;
2880 int i;
2881
Steven Rostedtcda48462009-10-14 15:43:42 -04002882 if (latency_format) {
2883 print_lat_fmt(data, size);
2884 printf(" | ");
2885 }
2886
Steven Rostedtea4010d2009-08-17 16:18:07 +02002887 field = find_field(event, "rettime");
2888 if (!field)
2889 die("can't find rettime in return graph");
2890 rettime = read_size(data + field->offset, field->size);
2891
2892 field = find_field(event, "calltime");
2893 if (!field)
2894 die("can't find calltime in return graph");
2895 calltime = read_size(data + field->offset, field->size);
2896
2897 duration = rettime - calltime;
2898
2899 /* Overhead */
2900 print_graph_overhead(duration);
2901
2902 /* Duration */
2903 print_graph_duration(duration);
2904
2905 field = find_field(event, "depth");
2906 if (!field)
2907 die("can't find depth in entry graph");
2908 depth = read_size(data + field->offset, field->size);
2909
2910 /* Function */
2911 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2912 printf(" ");
2913
2914 printf("}");
2915}
2916
2917static void
2918pretty_print_func_graph(void *data, int size, struct event *event,
David Ahernc70c94b2011-03-09 22:23:25 -07002919 int cpu, int pid)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002920{
2921 if (event->flags & EVENT_FL_ISFUNCENT)
David Ahernc70c94b2011-03-09 22:23:25 -07002922 pretty_print_func_ent(data, size, event, cpu, pid);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002923 else if (event->flags & EVENT_FL_ISFUNCRET)
David Ahernc70c94b2011-03-09 22:23:25 -07002924 pretty_print_func_ret(data, size, event);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002925 printf("\n");
2926}
2927
David Ahernc70c94b2011-03-09 22:23:25 -07002928void print_trace_event(int cpu, void *data, int size)
Steven Rostedtea4010d2009-08-17 16:18:07 +02002929{
2930 struct event *event;
Steven Rostedtea4010d2009-08-17 16:18:07 +02002931 int type;
2932 int pid;
2933
Ingo Molnarec156762009-09-11 12:12:54 +02002934 type = trace_parse_common_type(data);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002935
Ingo Molnarec156762009-09-11 12:12:54 +02002936 event = trace_find_event(type);
Ingo Molnarea57c4f2009-09-13 18:15:54 +02002937 if (!event) {
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04002938 warning("ug! no event found for type %d", type);
Ingo Molnarea57c4f2009-09-13 18:15:54 +02002939 return;
2940 }
Steven Rostedtea4010d2009-08-17 16:18:07 +02002941
Tom Zanussi16c632d2009-11-25 01:15:48 -06002942 pid = trace_parse_common_pid(data);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002943
2944 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
David Ahernc70c94b2011-03-09 22:23:25 -07002945 return pretty_print_func_graph(data, size, event, cpu, pid);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002946
David Ahernc70c94b2011-03-09 22:23:25 -07002947 if (latency_format)
Steven Rostedtcda48462009-10-14 15:43:42 -04002948 print_lat_fmt(data, size);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002949
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04002950 if (event->flags & EVENT_FL_FAILED) {
2951 printf("EVENT '%s' FAILED TO PARSE\n",
2952 event->name);
2953 return;
2954 }
2955
Steven Rostedtea4010d2009-08-17 16:18:07 +02002956 pretty_print(data, size, event);
Steven Rostedtea4010d2009-08-17 16:18:07 +02002957}
2958
2959static void print_fields(struct print_flag_sym *field)
2960{
2961 printf("{ %s, %s }", field->value, field->str);
2962 if (field->next) {
2963 printf(", ");
2964 print_fields(field->next);
2965 }
2966}
2967
2968static void print_args(struct print_arg *args)
2969{
2970 int print_paren = 1;
2971
2972 switch (args->type) {
2973 case PRINT_NULL:
2974 printf("null");
2975 break;
2976 case PRINT_ATOM:
2977 printf("%s", args->atom.atom);
2978 break;
2979 case PRINT_FIELD:
2980 printf("REC->%s", args->field.name);
2981 break;
2982 case PRINT_FLAGS:
2983 printf("__print_flags(");
2984 print_args(args->flags.field);
2985 printf(", %s, ", args->flags.delim);
2986 print_fields(args->flags.flags);
2987 printf(")");
2988 break;
2989 case PRINT_SYMBOL:
2990 printf("__print_symbolic(");
2991 print_args(args->symbol.field);
2992 printf(", ");
2993 print_fields(args->symbol.symbols);
2994 printf(")");
2995 break;
2996 case PRINT_STRING:
2997 printf("__get_str(%s)", args->string.string);
2998 break;
2999 case PRINT_TYPE:
3000 printf("(%s)", args->typecast.type);
3001 print_args(args->typecast.item);
3002 break;
3003 case PRINT_OP:
3004 if (strcmp(args->op.op, ":") == 0)
3005 print_paren = 0;
3006 if (print_paren)
3007 printf("(");
3008 print_args(args->op.left);
3009 printf(" %s ", args->op.op);
3010 print_args(args->op.right);
3011 if (print_paren)
3012 printf(")");
3013 break;
3014 default:
3015 /* we should warn... */
3016 return;
3017 }
3018 if (args->next) {
3019 printf("\n");
3020 print_args(args->next);
3021 }
3022}
3023
Steven Rostedtea4010d2009-08-17 16:18:07 +02003024int parse_ftrace_file(char *buf, unsigned long size)
3025{
3026 struct format_field *field;
3027 struct print_arg *arg, **list;
3028 struct event *event;
3029 int ret;
3030
3031 init_input_buf(buf, size);
3032
3033 event = alloc_event();
3034 if (!event)
3035 return -ENOMEM;
3036
3037 event->flags |= EVENT_FL_ISFTRACE;
3038
3039 event->name = event_read_name();
3040 if (!event->name)
3041 die("failed to read ftrace event name");
3042
3043 if (strcmp(event->name, "function") == 0)
3044 event->flags |= EVENT_FL_ISFUNC;
3045
3046 else if (strcmp(event->name, "funcgraph_entry") == 0)
3047 event->flags |= EVENT_FL_ISFUNCENT;
3048
3049 else if (strcmp(event->name, "funcgraph_exit") == 0)
3050 event->flags |= EVENT_FL_ISFUNCRET;
3051
3052 else if (strcmp(event->name, "bprint") == 0)
3053 event->flags |= EVENT_FL_ISBPRINT;
3054
3055 event->id = event_read_id();
3056 if (event->id < 0)
3057 die("failed to read ftrace event id");
3058
3059 add_event(event);
3060
3061 ret = event_read_format(event);
3062 if (ret < 0)
3063 die("failed to read ftrace event format");
3064
3065 ret = event_read_print(event);
3066 if (ret < 0)
3067 die("failed to read ftrace event print fmt");
3068
Steven Rostedt0d1da912009-10-14 15:43:41 -04003069 /* New ftrace handles args */
3070 if (ret > 0)
3071 return 0;
Steven Rostedtea4010d2009-08-17 16:18:07 +02003072 /*
3073 * The arguments for ftrace files are parsed by the fields.
3074 * Set up the fields as their arguments.
3075 */
3076 list = &event->print_fmt.args;
3077 for (field = event->format.fields; field; field = field->next) {
3078 arg = malloc_or_die(sizeof(*arg));
3079 memset(arg, 0, sizeof(*arg));
3080 *list = arg;
3081 list = &arg->next;
3082 arg->type = PRINT_FIELD;
3083 arg->field.name = field->name;
3084 arg->field.field = field;
3085 }
3086 return 0;
3087}
3088
Tom Zanussi27746012009-10-06 01:09:51 -05003089int parse_event_file(char *buf, unsigned long size, char *sys)
Steven Rostedtea4010d2009-08-17 16:18:07 +02003090{
3091 struct event *event;
3092 int ret;
3093
3094 init_input_buf(buf, size);
3095
3096 event = alloc_event();
3097 if (!event)
3098 return -ENOMEM;
3099
3100 event->name = event_read_name();
3101 if (!event->name)
3102 die("failed to read event name");
3103
3104 event->id = event_read_id();
3105 if (event->id < 0)
3106 die("failed to read event id");
3107
3108 ret = event_read_format(event);
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04003109 if (ret < 0) {
3110 warning("failed to read event format for %s", event->name);
3111 goto event_failed;
3112 }
Steven Rostedtea4010d2009-08-17 16:18:07 +02003113
3114 ret = event_read_print(event);
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04003115 if (ret < 0) {
3116 warning("failed to read event print fmt for %s", event->name);
3117 goto event_failed;
3118 }
Steven Rostedtea4010d2009-08-17 16:18:07 +02003119
Tom Zanussi27746012009-10-06 01:09:51 -05003120 event->system = strdup(sys);
3121
Steven Rostedtea4010d2009-08-17 16:18:07 +02003122#define PRINT_ARGS 0
3123 if (PRINT_ARGS && event->print_fmt.args)
3124 print_args(event->print_fmt.args);
3125
3126 add_event(event);
3127 return 0;
Steven Rostedt07a4bdd2009-10-14 15:43:39 -04003128
3129 event_failed:
3130 event->flags |= EVENT_FL_FAILED;
3131 /* still add it even if it failed */
3132 add_event(event);
3133 return -1;
Steven Rostedtea4010d2009-08-17 16:18:07 +02003134}
3135
3136void parse_set_info(int nr_cpus, int long_sz)
3137{
3138 cpus = nr_cpus;
3139 long_size = long_sz;
3140}
Tom Zanussi7397d802010-01-27 02:27:54 -06003141
3142int common_pc(struct scripting_context *context)
3143{
3144 return parse_common_pc(context->event_data);
3145}
3146
3147int common_flags(struct scripting_context *context)
3148{
3149 return parse_common_flags(context->event_data);
3150}
3151
3152int common_lock_depth(struct scripting_context *context)
3153{
3154 return parse_common_lock_depth(context->event_data);
3155}