]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/lib/traceevent/event-parse.c
perf tools: Use __maybe_used for unused variables
[karo-tx-linux.git] / tools / lib / traceevent / event-parse.c
1 /*
2  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
3  *
4  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation;
8  * version 2.1 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20  *
21  *  The parts for function graph printing was taken and modified from the
22  *  Linux Kernel that were written by
23  *    - Copyright (C) 2009  Frederic Weisbecker,
24  *  Frederic Weisbecker gave his permission to relicense the code to
25  *  the Lesser General Public License.
26  */
27 #define _GNU_SOURCE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdint.h>
35
36 #include "event-parse.h"
37 #include "event-utils.h"
38
39 static const char *input_buf;
40 static unsigned long long input_buf_ptr;
41 static unsigned long long input_buf_siz;
42
43 static int is_flag_field;
44 static int is_symbolic_field;
45
46 static int show_warning = 1;
47
48 #define do_warning(fmt, ...)                            \
49         do {                                            \
50                 if (show_warning)                       \
51                         warning(fmt, ##__VA_ARGS__);    \
52         } while (0)
53
54 static void init_input_buf(const char *buf, unsigned long long size)
55 {
56         input_buf = buf;
57         input_buf_siz = size;
58         input_buf_ptr = 0;
59 }
60
61 const char *pevent_get_input_buf(void)
62 {
63         return input_buf;
64 }
65
66 unsigned long long pevent_get_input_buf_ptr(void)
67 {
68         return input_buf_ptr;
69 }
70
71 struct event_handler {
72         struct event_handler            *next;
73         int                             id;
74         const char                      *sys_name;
75         const char                      *event_name;
76         pevent_event_handler_func       func;
77         void                            *context;
78 };
79
80 struct pevent_func_params {
81         struct pevent_func_params       *next;
82         enum pevent_func_arg_type       type;
83 };
84
85 struct pevent_function_handler {
86         struct pevent_function_handler  *next;
87         enum pevent_func_arg_type       ret_type;
88         char                            *name;
89         pevent_func_handler             func;
90         struct pevent_func_params       *params;
91         int                             nr_args;
92 };
93
94 static unsigned long long
95 process_defined_func(struct trace_seq *s, void *data, int size,
96                      struct event_format *event, struct print_arg *arg);
97
98 static void free_func_handle(struct pevent_function_handler *func);
99
100 /**
101  * pevent_buffer_init - init buffer for parsing
102  * @buf: buffer to parse
103  * @size: the size of the buffer
104  *
105  * For use with pevent_read_token(), this initializes the internal
106  * buffer that pevent_read_token() will parse.
107  */
108 void pevent_buffer_init(const char *buf, unsigned long long size)
109 {
110         init_input_buf(buf, size);
111 }
112
113 void breakpoint(void)
114 {
115         static int x;
116         x++;
117 }
118
119 struct print_arg *alloc_arg(void)
120 {
121         struct print_arg *arg;
122
123         arg = malloc_or_die(sizeof(*arg));
124         if (!arg)
125                 return NULL;
126         memset(arg, 0, sizeof(*arg));
127
128         return arg;
129 }
130
131 struct cmdline {
132         char *comm;
133         int pid;
134 };
135
136 static int cmdline_cmp(const void *a, const void *b)
137 {
138         const struct cmdline *ca = a;
139         const struct cmdline *cb = b;
140
141         if (ca->pid < cb->pid)
142                 return -1;
143         if (ca->pid > cb->pid)
144                 return 1;
145
146         return 0;
147 }
148
149 struct cmdline_list {
150         struct cmdline_list     *next;
151         char                    *comm;
152         int                     pid;
153 };
154
155 static int cmdline_init(struct pevent *pevent)
156 {
157         struct cmdline_list *cmdlist = pevent->cmdlist;
158         struct cmdline_list *item;
159         struct cmdline *cmdlines;
160         int i;
161
162         cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
163
164         i = 0;
165         while (cmdlist) {
166                 cmdlines[i].pid = cmdlist->pid;
167                 cmdlines[i].comm = cmdlist->comm;
168                 i++;
169                 item = cmdlist;
170                 cmdlist = cmdlist->next;
171                 free(item);
172         }
173
174         qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
175
176         pevent->cmdlines = cmdlines;
177         pevent->cmdlist = NULL;
178
179         return 0;
180 }
181
182 static char *find_cmdline(struct pevent *pevent, int pid)
183 {
184         const struct cmdline *comm;
185         struct cmdline key;
186
187         if (!pid)
188                 return "<idle>";
189
190         if (!pevent->cmdlines)
191                 cmdline_init(pevent);
192
193         key.pid = pid;
194
195         comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
196                        sizeof(*pevent->cmdlines), cmdline_cmp);
197
198         if (comm)
199                 return comm->comm;
200         return "<...>";
201 }
202
203 /**
204  * pevent_pid_is_registered - return if a pid has a cmdline registered
205  * @pevent: handle for the pevent
206  * @pid: The pid to check if it has a cmdline registered with.
207  *
208  * Returns 1 if the pid has a cmdline mapped to it
209  * 0 otherwise.
210  */
211 int pevent_pid_is_registered(struct pevent *pevent, int pid)
212 {
213         const struct cmdline *comm;
214         struct cmdline key;
215
216         if (!pid)
217                 return 1;
218
219         if (!pevent->cmdlines)
220                 cmdline_init(pevent);
221
222         key.pid = pid;
223
224         comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
225                        sizeof(*pevent->cmdlines), cmdline_cmp);
226
227         if (comm)
228                 return 1;
229         return 0;
230 }
231
232 /*
233  * If the command lines have been converted to an array, then
234  * we must add this pid. This is much slower than when cmdlines
235  * are added before the array is initialized.
236  */
237 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
238 {
239         struct cmdline *cmdlines = pevent->cmdlines;
240         const struct cmdline *cmdline;
241         struct cmdline key;
242
243         if (!pid)
244                 return 0;
245
246         /* avoid duplicates */
247         key.pid = pid;
248
249         cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
250                        sizeof(*pevent->cmdlines), cmdline_cmp);
251         if (cmdline) {
252                 errno = EEXIST;
253                 return -1;
254         }
255
256         cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
257         if (!cmdlines) {
258                 errno = ENOMEM;
259                 return -1;
260         }
261
262         cmdlines[pevent->cmdline_count].pid = pid;
263         cmdlines[pevent->cmdline_count].comm = strdup(comm);
264         if (!cmdlines[pevent->cmdline_count].comm)
265                 die("malloc comm");
266                 
267         if (cmdlines[pevent->cmdline_count].comm)
268                 pevent->cmdline_count++;
269
270         qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
271         pevent->cmdlines = cmdlines;
272
273         return 0;
274 }
275
276 /**
277  * pevent_register_comm - register a pid / comm mapping
278  * @pevent: handle for the pevent
279  * @comm: the command line to register
280  * @pid: the pid to map the command line to
281  *
282  * This adds a mapping to search for command line names with
283  * a given pid. The comm is duplicated.
284  */
285 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
286 {
287         struct cmdline_list *item;
288
289         if (pevent->cmdlines)
290                 return add_new_comm(pevent, comm, pid);
291
292         item = malloc_or_die(sizeof(*item));
293         item->comm = strdup(comm);
294         if (!item->comm)
295                 die("malloc comm");
296         item->pid = pid;
297         item->next = pevent->cmdlist;
298
299         pevent->cmdlist = item;
300         pevent->cmdline_count++;
301
302         return 0;
303 }
304
305 struct func_map {
306         unsigned long long              addr;
307         char                            *func;
308         char                            *mod;
309 };
310
311 struct func_list {
312         struct func_list        *next;
313         unsigned long long      addr;
314         char                    *func;
315         char                    *mod;
316 };
317
318 static int func_cmp(const void *a, const void *b)
319 {
320         const struct func_map *fa = a;
321         const struct func_map *fb = b;
322
323         if (fa->addr < fb->addr)
324                 return -1;
325         if (fa->addr > fb->addr)
326                 return 1;
327
328         return 0;
329 }
330
331 /*
332  * We are searching for a record in between, not an exact
333  * match.
334  */
335 static int func_bcmp(const void *a, const void *b)
336 {
337         const struct func_map *fa = a;
338         const struct func_map *fb = b;
339
340         if ((fa->addr == fb->addr) ||
341
342             (fa->addr > fb->addr &&
343              fa->addr < (fb+1)->addr))
344                 return 0;
345
346         if (fa->addr < fb->addr)
347                 return -1;
348
349         return 1;
350 }
351
352 static int func_map_init(struct pevent *pevent)
353 {
354         struct func_list *funclist;
355         struct func_list *item;
356         struct func_map *func_map;
357         int i;
358
359         func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
360         funclist = pevent->funclist;
361
362         i = 0;
363         while (funclist) {
364                 func_map[i].func = funclist->func;
365                 func_map[i].addr = funclist->addr;
366                 func_map[i].mod = funclist->mod;
367                 i++;
368                 item = funclist;
369                 funclist = funclist->next;
370                 free(item);
371         }
372
373         qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
374
375         /*
376          * Add a special record at the end.
377          */
378         func_map[pevent->func_count].func = NULL;
379         func_map[pevent->func_count].addr = 0;
380         func_map[pevent->func_count].mod = NULL;
381
382         pevent->func_map = func_map;
383         pevent->funclist = NULL;
384
385         return 0;
386 }
387
388 static struct func_map *
389 find_func(struct pevent *pevent, unsigned long long addr)
390 {
391         struct func_map *func;
392         struct func_map key;
393
394         if (!pevent->func_map)
395                 func_map_init(pevent);
396
397         key.addr = addr;
398
399         func = bsearch(&key, pevent->func_map, pevent->func_count,
400                        sizeof(*pevent->func_map), func_bcmp);
401
402         return func;
403 }
404
405 /**
406  * pevent_find_function - find a function by a given address
407  * @pevent: handle for the pevent
408  * @addr: the address to find the function with
409  *
410  * Returns a pointer to the function stored that has the given
411  * address. Note, the address does not have to be exact, it
412  * will select the function that would contain the address.
413  */
414 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
415 {
416         struct func_map *map;
417
418         map = find_func(pevent, addr);
419         if (!map)
420                 return NULL;
421
422         return map->func;
423 }
424
425 /**
426  * pevent_find_function_address - find a function address by a given address
427  * @pevent: handle for the pevent
428  * @addr: the address to find the function with
429  *
430  * Returns the address the function starts at. This can be used in
431  * conjunction with pevent_find_function to print both the function
432  * name and the function offset.
433  */
434 unsigned long long
435 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
436 {
437         struct func_map *map;
438
439         map = find_func(pevent, addr);
440         if (!map)
441                 return 0;
442
443         return map->addr;
444 }
445
446 /**
447  * pevent_register_function - register a function with a given address
448  * @pevent: handle for the pevent
449  * @function: the function name to register
450  * @addr: the address the function starts at
451  * @mod: the kernel module the function may be in (NULL for none)
452  *
453  * This registers a function name with an address and module.
454  * The @func passed in is duplicated.
455  */
456 int pevent_register_function(struct pevent *pevent, char *func,
457                              unsigned long long addr, char *mod)
458 {
459         struct func_list *item;
460
461         item = malloc_or_die(sizeof(*item));
462
463         item->next = pevent->funclist;
464         item->func = strdup(func);
465         if (mod)
466                 item->mod = strdup(mod);
467         else
468                 item->mod = NULL;
469         item->addr = addr;
470
471         if (!item->func || (mod && !item->mod))
472                 die("malloc func");
473
474         pevent->funclist = item;
475         pevent->func_count++;
476
477         return 0;
478 }
479
480 /**
481  * pevent_print_funcs - print out the stored functions
482  * @pevent: handle for the pevent
483  *
484  * This prints out the stored functions.
485  */
486 void pevent_print_funcs(struct pevent *pevent)
487 {
488         int i;
489
490         if (!pevent->func_map)
491                 func_map_init(pevent);
492
493         for (i = 0; i < (int)pevent->func_count; i++) {
494                 printf("%016llx %s",
495                        pevent->func_map[i].addr,
496                        pevent->func_map[i].func);
497                 if (pevent->func_map[i].mod)
498                         printf(" [%s]\n", pevent->func_map[i].mod);
499                 else
500                         printf("\n");
501         }
502 }
503
504 struct printk_map {
505         unsigned long long              addr;
506         char                            *printk;
507 };
508
509 struct printk_list {
510         struct printk_list      *next;
511         unsigned long long      addr;
512         char                    *printk;
513 };
514
515 static int printk_cmp(const void *a, const void *b)
516 {
517         const struct printk_map *pa = a;
518         const struct printk_map *pb = b;
519
520         if (pa->addr < pb->addr)
521                 return -1;
522         if (pa->addr > pb->addr)
523                 return 1;
524
525         return 0;
526 }
527
528 static void printk_map_init(struct pevent *pevent)
529 {
530         struct printk_list *printklist;
531         struct printk_list *item;
532         struct printk_map *printk_map;
533         int i;
534
535         printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
536
537         printklist = pevent->printklist;
538
539         i = 0;
540         while (printklist) {
541                 printk_map[i].printk = printklist->printk;
542                 printk_map[i].addr = printklist->addr;
543                 i++;
544                 item = printklist;
545                 printklist = printklist->next;
546                 free(item);
547         }
548
549         qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
550
551         pevent->printk_map = printk_map;
552         pevent->printklist = NULL;
553 }
554
555 static struct printk_map *
556 find_printk(struct pevent *pevent, unsigned long long addr)
557 {
558         struct printk_map *printk;
559         struct printk_map key;
560
561         if (!pevent->printk_map)
562                 printk_map_init(pevent);
563
564         key.addr = addr;
565
566         printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
567                          sizeof(*pevent->printk_map), printk_cmp);
568
569         return printk;
570 }
571
572 /**
573  * pevent_register_print_string - register a string by its address
574  * @pevent: handle for the pevent
575  * @fmt: the string format to register
576  * @addr: the address the string was located at
577  *
578  * This registers a string by the address it was stored in the kernel.
579  * The @fmt passed in is duplicated.
580  */
581 int pevent_register_print_string(struct pevent *pevent, char *fmt,
582                                  unsigned long long addr)
583 {
584         struct printk_list *item;
585
586         item = malloc_or_die(sizeof(*item));
587
588         item->next = pevent->printklist;
589         item->printk = strdup(fmt);
590         item->addr = addr;
591
592         if (!item->printk)
593                 die("malloc fmt");
594
595         pevent->printklist = item;
596         pevent->printk_count++;
597
598         return 0;
599 }
600
601 /**
602  * pevent_print_printk - print out the stored strings
603  * @pevent: handle for the pevent
604  *
605  * This prints the string formats that were stored.
606  */
607 void pevent_print_printk(struct pevent *pevent)
608 {
609         int i;
610
611         if (!pevent->printk_map)
612                 printk_map_init(pevent);
613
614         for (i = 0; i < (int)pevent->printk_count; i++) {
615                 printf("%016llx %s\n",
616                        pevent->printk_map[i].addr,
617                        pevent->printk_map[i].printk);
618         }
619 }
620
621 static struct event_format *alloc_event(void)
622 {
623         struct event_format *event;
624
625         event = malloc(sizeof(*event));
626         if (!event)
627                 return NULL;
628         memset(event, 0, sizeof(*event));
629
630         return event;
631 }
632
633 static void add_event(struct pevent *pevent, struct event_format *event)
634 {
635         int i;
636
637         pevent->events = realloc(pevent->events, sizeof(event) *
638                                  (pevent->nr_events + 1));
639         if (!pevent->events)
640                 die("Can not allocate events");
641
642         for (i = 0; i < pevent->nr_events; i++) {
643                 if (pevent->events[i]->id > event->id)
644                         break;
645         }
646         if (i < pevent->nr_events)
647                 memmove(&pevent->events[i + 1],
648                         &pevent->events[i],
649                         sizeof(event) * (pevent->nr_events - i));
650
651         pevent->events[i] = event;
652         pevent->nr_events++;
653
654         event->pevent = pevent;
655 }
656
657 static int event_item_type(enum event_type type)
658 {
659         switch (type) {
660         case EVENT_ITEM ... EVENT_SQUOTE:
661                 return 1;
662         case EVENT_ERROR ... EVENT_DELIM:
663         default:
664                 return 0;
665         }
666 }
667
668 static void free_flag_sym(struct print_flag_sym *fsym)
669 {
670         struct print_flag_sym *next;
671
672         while (fsym) {
673                 next = fsym->next;
674                 free(fsym->value);
675                 free(fsym->str);
676                 free(fsym);
677                 fsym = next;
678         }
679 }
680
681 static void free_arg(struct print_arg *arg)
682 {
683         struct print_arg *farg;
684
685         if (!arg)
686                 return;
687
688         switch (arg->type) {
689         case PRINT_ATOM:
690                 free(arg->atom.atom);
691                 break;
692         case PRINT_FIELD:
693                 free(arg->field.name);
694                 break;
695         case PRINT_FLAGS:
696                 free_arg(arg->flags.field);
697                 free(arg->flags.delim);
698                 free_flag_sym(arg->flags.flags);
699                 break;
700         case PRINT_SYMBOL:
701                 free_arg(arg->symbol.field);
702                 free_flag_sym(arg->symbol.symbols);
703                 break;
704         case PRINT_HEX:
705                 free_arg(arg->hex.field);
706                 free_arg(arg->hex.size);
707                 break;
708         case PRINT_TYPE:
709                 free(arg->typecast.type);
710                 free_arg(arg->typecast.item);
711                 break;
712         case PRINT_STRING:
713         case PRINT_BSTRING:
714                 free(arg->string.string);
715                 break;
716         case PRINT_DYNAMIC_ARRAY:
717                 free(arg->dynarray.index);
718                 break;
719         case PRINT_OP:
720                 free(arg->op.op);
721                 free_arg(arg->op.left);
722                 free_arg(arg->op.right);
723                 break;
724         case PRINT_FUNC:
725                 while (arg->func.args) {
726                         farg = arg->func.args;
727                         arg->func.args = farg->next;
728                         free_arg(farg);
729                 }
730                 break;
731
732         case PRINT_NULL:
733         default:
734                 break;
735         }
736
737         free(arg);
738 }
739
740 static enum event_type get_type(int ch)
741 {
742         if (ch == '\n')
743                 return EVENT_NEWLINE;
744         if (isspace(ch))
745                 return EVENT_SPACE;
746         if (isalnum(ch) || ch == '_')
747                 return EVENT_ITEM;
748         if (ch == '\'')
749                 return EVENT_SQUOTE;
750         if (ch == '"')
751                 return EVENT_DQUOTE;
752         if (!isprint(ch))
753                 return EVENT_NONE;
754         if (ch == '(' || ch == ')' || ch == ',')
755                 return EVENT_DELIM;
756
757         return EVENT_OP;
758 }
759
760 static int __read_char(void)
761 {
762         if (input_buf_ptr >= input_buf_siz)
763                 return -1;
764
765         return input_buf[input_buf_ptr++];
766 }
767
768 static int __peek_char(void)
769 {
770         if (input_buf_ptr >= input_buf_siz)
771                 return -1;
772
773         return input_buf[input_buf_ptr];
774 }
775
776 /**
777  * pevent_peek_char - peek at the next character that will be read
778  *
779  * Returns the next character read, or -1 if end of buffer.
780  */
781 int pevent_peek_char(void)
782 {
783         return __peek_char();
784 }
785
786 static int extend_token(char **tok, char *buf, int size)
787 {
788         char *newtok = realloc(*tok, size);
789
790         if (!newtok) {
791                 free(*tok);
792                 *tok = NULL;
793                 return -1;
794         }
795
796         if (!*tok)
797                 strcpy(newtok, buf);
798         else
799                 strcat(newtok, buf);
800         *tok = newtok;
801
802         return 0;
803 }
804
805 static enum event_type force_token(const char *str, char **tok);
806
807 static enum event_type __read_token(char **tok)
808 {
809         char buf[BUFSIZ];
810         int ch, last_ch, quote_ch, next_ch;
811         int i = 0;
812         int tok_size = 0;
813         enum event_type type;
814
815         *tok = NULL;
816
817
818         ch = __read_char();
819         if (ch < 0)
820                 return EVENT_NONE;
821
822         type = get_type(ch);
823         if (type == EVENT_NONE)
824                 return type;
825
826         buf[i++] = ch;
827
828         switch (type) {
829         case EVENT_NEWLINE:
830         case EVENT_DELIM:
831                 *tok = malloc_or_die(2);
832                 (*tok)[0] = ch;
833                 (*tok)[1] = 0;
834                 return type;
835
836         case EVENT_OP:
837                 switch (ch) {
838                 case '-':
839                         next_ch = __peek_char();
840                         if (next_ch == '>') {
841                                 buf[i++] = __read_char();
842                                 break;
843                         }
844                         /* fall through */
845                 case '+':
846                 case '|':
847                 case '&':
848                 case '>':
849                 case '<':
850                         last_ch = ch;
851                         ch = __peek_char();
852                         if (ch != last_ch)
853                                 goto test_equal;
854                         buf[i++] = __read_char();
855                         switch (last_ch) {
856                         case '>':
857                         case '<':
858                                 goto test_equal;
859                         default:
860                                 break;
861                         }
862                         break;
863                 case '!':
864                 case '=':
865                         goto test_equal;
866                 default: /* what should we do instead? */
867                         break;
868                 }
869                 buf[i] = 0;
870                 *tok = strdup(buf);
871                 return type;
872
873  test_equal:
874                 ch = __peek_char();
875                 if (ch == '=')
876                         buf[i++] = __read_char();
877                 goto out;
878
879         case EVENT_DQUOTE:
880         case EVENT_SQUOTE:
881                 /* don't keep quotes */
882                 i--;
883                 quote_ch = ch;
884                 last_ch = 0;
885  concat:
886                 do {
887                         if (i == (BUFSIZ - 1)) {
888                                 buf[i] = 0;
889                                 tok_size += BUFSIZ;
890
891                                 if (extend_token(tok, buf, tok_size) < 0)
892                                         return EVENT_NONE;
893                                 i = 0;
894                         }
895                         last_ch = ch;
896                         ch = __read_char();
897                         buf[i++] = ch;
898                         /* the '\' '\' will cancel itself */
899                         if (ch == '\\' && last_ch == '\\')
900                                 last_ch = 0;
901                 } while (ch != quote_ch || last_ch == '\\');
902                 /* remove the last quote */
903                 i--;
904
905                 /*
906                  * For strings (double quotes) check the next token.
907                  * If it is another string, concatinate the two.
908                  */
909                 if (type == EVENT_DQUOTE) {
910                         unsigned long long save_input_buf_ptr = input_buf_ptr;
911
912                         do {
913                                 ch = __read_char();
914                         } while (isspace(ch));
915                         if (ch == '"')
916                                 goto concat;
917                         input_buf_ptr = save_input_buf_ptr;
918                 }
919
920                 goto out;
921
922         case EVENT_ERROR ... EVENT_SPACE:
923         case EVENT_ITEM:
924         default:
925                 break;
926         }
927
928         while (get_type(__peek_char()) == type) {
929                 if (i == (BUFSIZ - 1)) {
930                         buf[i] = 0;
931                         tok_size += BUFSIZ;
932
933                         if (extend_token(tok, buf, tok_size) < 0)
934                                 return EVENT_NONE;
935                         i = 0;
936                 }
937                 ch = __read_char();
938                 buf[i++] = ch;
939         }
940
941  out:
942         buf[i] = 0;
943         if (extend_token(tok, buf, tok_size + i + 1) < 0)
944                 return EVENT_NONE;
945
946         if (type == EVENT_ITEM) {
947                 /*
948                  * Older versions of the kernel has a bug that
949                  * creates invalid symbols and will break the mac80211
950                  * parsing. This is a work around to that bug.
951                  *
952                  * See Linux kernel commit:
953                  *  811cb50baf63461ce0bdb234927046131fc7fa8b
954                  */
955                 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
956                         free(*tok);
957                         *tok = NULL;
958                         return force_token("\"\%s\" ", tok);
959                 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
960                         free(*tok);
961                         *tok = NULL;
962                         return force_token("\" sta:%pM\" ", tok);
963                 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
964                         free(*tok);
965                         *tok = NULL;
966                         return force_token("\" vif:%p(%d)\" ", tok);
967                 }
968         }
969
970         return type;
971 }
972
973 static enum event_type force_token(const char *str, char **tok)
974 {
975         const char *save_input_buf;
976         unsigned long long save_input_buf_ptr;
977         unsigned long long save_input_buf_siz;
978         enum event_type type;
979         
980         /* save off the current input pointers */
981         save_input_buf = input_buf;
982         save_input_buf_ptr = input_buf_ptr;
983         save_input_buf_siz = input_buf_siz;
984
985         init_input_buf(str, strlen(str));
986
987         type = __read_token(tok);
988
989         /* reset back to original token */
990         input_buf = save_input_buf;
991         input_buf_ptr = save_input_buf_ptr;
992         input_buf_siz = save_input_buf_siz;
993
994         return type;
995 }
996
997 static void free_token(char *tok)
998 {
999         if (tok)
1000                 free(tok);
1001 }
1002
1003 static enum event_type read_token(char **tok)
1004 {
1005         enum event_type type;
1006
1007         for (;;) {
1008                 type = __read_token(tok);
1009                 if (type != EVENT_SPACE)
1010                         return type;
1011
1012                 free_token(*tok);
1013         }
1014
1015         /* not reached */
1016         *tok = NULL;
1017         return EVENT_NONE;
1018 }
1019
1020 /**
1021  * pevent_read_token - access to utilites to use the pevent parser
1022  * @tok: The token to return
1023  *
1024  * This will parse tokens from the string given by
1025  * pevent_init_data().
1026  *
1027  * Returns the token type.
1028  */
1029 enum event_type pevent_read_token(char **tok)
1030 {
1031         return read_token(tok);
1032 }
1033
1034 /**
1035  * pevent_free_token - free a token returned by pevent_read_token
1036  * @token: the token to free
1037  */
1038 void pevent_free_token(char *token)
1039 {
1040         free_token(token);
1041 }
1042
1043 /* no newline */
1044 static enum event_type read_token_item(char **tok)
1045 {
1046         enum event_type type;
1047
1048         for (;;) {
1049                 type = __read_token(tok);
1050                 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1051                         return type;
1052                 free_token(*tok);
1053                 *tok = NULL;
1054         }
1055
1056         /* not reached */
1057         *tok = NULL;
1058         return EVENT_NONE;
1059 }
1060
1061 static int test_type(enum event_type type, enum event_type expect)
1062 {
1063         if (type != expect) {
1064                 do_warning("Error: expected type %d but read %d",
1065                     expect, type);
1066                 return -1;
1067         }
1068         return 0;
1069 }
1070
1071 static int test_type_token(enum event_type type, const char *token,
1072                     enum event_type expect, const char *expect_tok)
1073 {
1074         if (type != expect) {
1075                 do_warning("Error: expected type %d but read %d",
1076                     expect, type);
1077                 return -1;
1078         }
1079
1080         if (strcmp(token, expect_tok) != 0) {
1081                 do_warning("Error: expected '%s' but read '%s'",
1082                     expect_tok, token);
1083                 return -1;
1084         }
1085         return 0;
1086 }
1087
1088 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1089 {
1090         enum event_type type;
1091
1092         if (newline_ok)
1093                 type = read_token(tok);
1094         else
1095                 type = read_token_item(tok);
1096         return test_type(type, expect);
1097 }
1098
1099 static int read_expect_type(enum event_type expect, char **tok)
1100 {
1101         return __read_expect_type(expect, tok, 1);
1102 }
1103
1104 static int __read_expected(enum event_type expect, const char *str,
1105                            int newline_ok)
1106 {
1107         enum event_type type;
1108         char *token;
1109         int ret;
1110
1111         if (newline_ok)
1112                 type = read_token(&token);
1113         else
1114                 type = read_token_item(&token);
1115
1116         ret = test_type_token(type, token, expect, str);
1117
1118         free_token(token);
1119
1120         return ret;
1121 }
1122
1123 static int read_expected(enum event_type expect, const char *str)
1124 {
1125         return __read_expected(expect, str, 1);
1126 }
1127
1128 static int read_expected_item(enum event_type expect, const char *str)
1129 {
1130         return __read_expected(expect, str, 0);
1131 }
1132
1133 static char *event_read_name(void)
1134 {
1135         char *token;
1136
1137         if (read_expected(EVENT_ITEM, "name") < 0)
1138                 return NULL;
1139
1140         if (read_expected(EVENT_OP, ":") < 0)
1141                 return NULL;
1142
1143         if (read_expect_type(EVENT_ITEM, &token) < 0)
1144                 goto fail;
1145
1146         return token;
1147
1148  fail:
1149         free_token(token);
1150         return NULL;
1151 }
1152
1153 static int event_read_id(void)
1154 {
1155         char *token;
1156         int id;
1157
1158         if (read_expected_item(EVENT_ITEM, "ID") < 0)
1159                 return -1;
1160
1161         if (read_expected(EVENT_OP, ":") < 0)
1162                 return -1;
1163
1164         if (read_expect_type(EVENT_ITEM, &token) < 0)
1165                 goto fail;
1166
1167         id = strtoul(token, NULL, 0);
1168         free_token(token);
1169         return id;
1170
1171  fail:
1172         free_token(token);
1173         return -1;
1174 }
1175
1176 static int field_is_string(struct format_field *field)
1177 {
1178         if ((field->flags & FIELD_IS_ARRAY) &&
1179             (strstr(field->type, "char") || strstr(field->type, "u8") ||
1180              strstr(field->type, "s8")))
1181                 return 1;
1182
1183         return 0;
1184 }
1185
1186 static int field_is_dynamic(struct format_field *field)
1187 {
1188         if (strncmp(field->type, "__data_loc", 10) == 0)
1189                 return 1;
1190
1191         return 0;
1192 }
1193
1194 static int field_is_long(struct format_field *field)
1195 {
1196         /* includes long long */
1197         if (strstr(field->type, "long"))
1198                 return 1;
1199
1200         return 0;
1201 }
1202
1203 static int event_read_fields(struct event_format *event, struct format_field **fields)
1204 {
1205         struct format_field *field = NULL;
1206         enum event_type type;
1207         char *token;
1208         char *last_token;
1209         int count = 0;
1210
1211         do {
1212                 type = read_token(&token);
1213                 if (type == EVENT_NEWLINE) {
1214                         free_token(token);
1215                         return count;
1216                 }
1217
1218                 count++;
1219
1220                 if (test_type_token(type, token, EVENT_ITEM, "field"))
1221                         goto fail;
1222                 free_token(token);
1223
1224                 type = read_token(&token);
1225                 /*
1226                  * The ftrace fields may still use the "special" name.
1227                  * Just ignore it.
1228                  */
1229                 if (event->flags & EVENT_FL_ISFTRACE &&
1230                     type == EVENT_ITEM && strcmp(token, "special") == 0) {
1231                         free_token(token);
1232                         type = read_token(&token);
1233                 }
1234
1235                 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1236                         goto fail;
1237
1238                 free_token(token);
1239                 if (read_expect_type(EVENT_ITEM, &token) < 0)
1240                         goto fail;
1241
1242                 last_token = token;
1243
1244                 field = malloc_or_die(sizeof(*field));
1245                 memset(field, 0, sizeof(*field));
1246                 field->event = event;
1247
1248                 /* read the rest of the type */
1249                 for (;;) {
1250                         type = read_token(&token);
1251                         if (type == EVENT_ITEM ||
1252                             (type == EVENT_OP && strcmp(token, "*") == 0) ||
1253                             /*
1254                              * Some of the ftrace fields are broken and have
1255                              * an illegal "." in them.
1256                              */
1257                             (event->flags & EVENT_FL_ISFTRACE &&
1258                              type == EVENT_OP && strcmp(token, ".") == 0)) {
1259
1260                                 if (strcmp(token, "*") == 0)
1261                                         field->flags |= FIELD_IS_POINTER;
1262
1263                                 if (field->type) {
1264                                         char *new_type;
1265                                         new_type = realloc(field->type,
1266                                                            strlen(field->type) +
1267                                                            strlen(last_token) + 2);
1268                                         if (!new_type) {
1269                                                 free(last_token);
1270                                                 goto fail;
1271                                         }
1272                                         field->type = new_type;
1273                                         strcat(field->type, " ");
1274                                         strcat(field->type, last_token);
1275                                         free(last_token);
1276                                 } else
1277                                         field->type = last_token;
1278                                 last_token = token;
1279                                 continue;
1280                         }
1281
1282                         break;
1283                 }
1284
1285                 if (!field->type) {
1286                         die("no type found");
1287                         goto fail;
1288                 }
1289                 field->name = last_token;
1290
1291                 if (test_type(type, EVENT_OP))
1292                         goto fail;
1293
1294                 if (strcmp(token, "[") == 0) {
1295                         enum event_type last_type = type;
1296                         char *brackets = token;
1297                         char *new_brackets;
1298                         int len;
1299
1300                         field->flags |= FIELD_IS_ARRAY;
1301
1302                         type = read_token(&token);
1303
1304                         if (type == EVENT_ITEM)
1305                                 field->arraylen = strtoul(token, NULL, 0);
1306                         else
1307                                 field->arraylen = 0;
1308
1309                         while (strcmp(token, "]") != 0) {
1310                                 if (last_type == EVENT_ITEM &&
1311                                     type == EVENT_ITEM)
1312                                         len = 2;
1313                                 else
1314                                         len = 1;
1315                                 last_type = type;
1316
1317                                 new_brackets = realloc(brackets,
1318                                                        strlen(brackets) +
1319                                                        strlen(token) + len);
1320                                 if (!new_brackets) {
1321                                         free(brackets);
1322                                         goto fail;
1323                                 }
1324                                 brackets = new_brackets;
1325                                 if (len == 2)
1326                                         strcat(brackets, " ");
1327                                 strcat(brackets, token);
1328                                 /* We only care about the last token */
1329                                 field->arraylen = strtoul(token, NULL, 0);
1330                                 free_token(token);
1331                                 type = read_token(&token);
1332                                 if (type == EVENT_NONE) {
1333                                         die("failed to find token");
1334                                         goto fail;
1335                                 }
1336                         }
1337
1338                         free_token(token);
1339
1340                         new_brackets = realloc(brackets, strlen(brackets) + 2);
1341                         if (!new_brackets) {
1342                                 free(brackets);
1343                                 goto fail;
1344                         }
1345                         brackets = new_brackets;
1346                         strcat(brackets, "]");
1347
1348                         /* add brackets to type */
1349
1350                         type = read_token(&token);
1351                         /*
1352                          * If the next token is not an OP, then it is of
1353                          * the format: type [] item;
1354                          */
1355                         if (type == EVENT_ITEM) {
1356                                 char *new_type;
1357                                 new_type = realloc(field->type,
1358                                                    strlen(field->type) +
1359                                                    strlen(field->name) +
1360                                                    strlen(brackets) + 2);
1361                                 if (!new_type) {
1362                                         free(brackets);
1363                                         goto fail;
1364                                 }
1365                                 field->type = new_type;
1366                                 strcat(field->type, " ");
1367                                 strcat(field->type, field->name);
1368                                 free_token(field->name);
1369                                 strcat(field->type, brackets);
1370                                 field->name = token;
1371                                 type = read_token(&token);
1372                         } else {
1373                                 char *new_type;
1374                                 new_type = realloc(field->type,
1375                                                    strlen(field->type) +
1376                                                    strlen(brackets) + 1);
1377                                 if (!new_type) {
1378                                         free(brackets);
1379                                         goto fail;
1380                                 }
1381                                 field->type = new_type;
1382                                 strcat(field->type, brackets);
1383                         }
1384                         free(brackets);
1385                 }
1386
1387                 if (field_is_string(field))
1388                         field->flags |= FIELD_IS_STRING;
1389                 if (field_is_dynamic(field))
1390                         field->flags |= FIELD_IS_DYNAMIC;
1391                 if (field_is_long(field))
1392                         field->flags |= FIELD_IS_LONG;
1393
1394                 if (test_type_token(type, token,  EVENT_OP, ";"))
1395                         goto fail;
1396                 free_token(token);
1397
1398                 if (read_expected(EVENT_ITEM, "offset") < 0)
1399                         goto fail_expect;
1400
1401                 if (read_expected(EVENT_OP, ":") < 0)
1402                         goto fail_expect;
1403
1404                 if (read_expect_type(EVENT_ITEM, &token))
1405                         goto fail;
1406                 field->offset = strtoul(token, NULL, 0);
1407                 free_token(token);
1408
1409                 if (read_expected(EVENT_OP, ";") < 0)
1410                         goto fail_expect;
1411
1412                 if (read_expected(EVENT_ITEM, "size") < 0)
1413                         goto fail_expect;
1414
1415                 if (read_expected(EVENT_OP, ":") < 0)
1416                         goto fail_expect;
1417
1418                 if (read_expect_type(EVENT_ITEM, &token))
1419                         goto fail;
1420                 field->size = strtoul(token, NULL, 0);
1421                 free_token(token);
1422
1423                 if (read_expected(EVENT_OP, ";") < 0)
1424                         goto fail_expect;
1425
1426                 type = read_token(&token);
1427                 if (type != EVENT_NEWLINE) {
1428                         /* newer versions of the kernel have a "signed" type */
1429                         if (test_type_token(type, token, EVENT_ITEM, "signed"))
1430                                 goto fail;
1431
1432                         free_token(token);
1433
1434                         if (read_expected(EVENT_OP, ":") < 0)
1435                                 goto fail_expect;
1436
1437                         if (read_expect_type(EVENT_ITEM, &token))
1438                                 goto fail;
1439
1440                         /* add signed type */
1441
1442                         free_token(token);
1443                         if (read_expected(EVENT_OP, ";") < 0)
1444                                 goto fail_expect;
1445
1446                         if (read_expect_type(EVENT_NEWLINE, &token))
1447                                 goto fail;
1448                 }
1449
1450                 free_token(token);
1451
1452                 if (field->flags & FIELD_IS_ARRAY) {
1453                         if (field->arraylen)
1454                                 field->elementsize = field->size / field->arraylen;
1455                         else if (field->flags & FIELD_IS_STRING)
1456                                 field->elementsize = 1;
1457                         else
1458                                 field->elementsize = event->pevent->long_size;
1459                 } else
1460                         field->elementsize = field->size;
1461
1462                 *fields = field;
1463                 fields = &field->next;
1464
1465         } while (1);
1466
1467         return 0;
1468
1469 fail:
1470         free_token(token);
1471 fail_expect:
1472         if (field) {
1473                 free(field->type);
1474                 free(field->name);
1475                 free(field);
1476         }
1477         return -1;
1478 }
1479
1480 static int event_read_format(struct event_format *event)
1481 {
1482         char *token;
1483         int ret;
1484
1485         if (read_expected_item(EVENT_ITEM, "format") < 0)
1486                 return -1;
1487
1488         if (read_expected(EVENT_OP, ":") < 0)
1489                 return -1;
1490
1491         if (read_expect_type(EVENT_NEWLINE, &token))
1492                 goto fail;
1493         free_token(token);
1494
1495         ret = event_read_fields(event, &event->format.common_fields);
1496         if (ret < 0)
1497                 return ret;
1498         event->format.nr_common = ret;
1499
1500         ret = event_read_fields(event, &event->format.fields);
1501         if (ret < 0)
1502                 return ret;
1503         event->format.nr_fields = ret;
1504
1505         return 0;
1506
1507  fail:
1508         free_token(token);
1509         return -1;
1510 }
1511
1512 static enum event_type
1513 process_arg_token(struct event_format *event, struct print_arg *arg,
1514                   char **tok, enum event_type type);
1515
1516 static enum event_type
1517 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1518 {
1519         enum event_type type;
1520         char *token;
1521
1522         type = read_token(&token);
1523         *tok = token;
1524
1525         return process_arg_token(event, arg, tok, type);
1526 }
1527
1528 static enum event_type
1529 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1530
1531 static enum event_type
1532 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1533 {
1534         struct print_arg *arg, *left, *right;
1535         enum event_type type;
1536         char *token = NULL;
1537
1538         arg = alloc_arg();
1539         left = alloc_arg();
1540         right = alloc_arg();
1541
1542         arg->type = PRINT_OP;
1543         arg->op.left = left;
1544         arg->op.right = right;
1545
1546         *tok = NULL;
1547         type = process_arg(event, left, &token);
1548
1549  again:
1550         /* Handle other operations in the arguments */
1551         if (type == EVENT_OP && strcmp(token, ":") != 0) {
1552                 type = process_op(event, left, &token);
1553                 goto again;
1554         }
1555
1556         if (test_type_token(type, token, EVENT_OP, ":"))
1557                 goto out_free;
1558
1559         arg->op.op = token;
1560
1561         type = process_arg(event, right, &token);
1562
1563         top->op.right = arg;
1564
1565         *tok = token;
1566         return type;
1567
1568 out_free:
1569         /* Top may point to itself */
1570         top->op.right = NULL;
1571         free_token(token);
1572         free_arg(arg);
1573         return EVENT_ERROR;
1574 }
1575
1576 static enum event_type
1577 process_array(struct event_format *event, struct print_arg *top, char **tok)
1578 {
1579         struct print_arg *arg;
1580         enum event_type type;
1581         char *token = NULL;
1582
1583         arg = alloc_arg();
1584
1585         *tok = NULL;
1586         type = process_arg(event, arg, &token);
1587         if (test_type_token(type, token, EVENT_OP, "]"))
1588                 goto out_free;
1589
1590         top->op.right = arg;
1591
1592         free_token(token);
1593         type = read_token_item(&token);
1594         *tok = token;
1595
1596         return type;
1597
1598 out_free:
1599         free_token(*tok);
1600         *tok = NULL;
1601         free_arg(arg);
1602         return EVENT_ERROR;
1603 }
1604
1605 static int get_op_prio(char *op)
1606 {
1607         if (!op[1]) {
1608                 switch (op[0]) {
1609                 case '~':
1610                 case '!':
1611                         return 4;
1612                 case '*':
1613                 case '/':
1614                 case '%':
1615                         return 6;
1616                 case '+':
1617                 case '-':
1618                         return 7;
1619                         /* '>>' and '<<' are 8 */
1620                 case '<':
1621                 case '>':
1622                         return 9;
1623                         /* '==' and '!=' are 10 */
1624                 case '&':
1625                         return 11;
1626                 case '^':
1627                         return 12;
1628                 case '|':
1629                         return 13;
1630                 case '?':
1631                         return 16;
1632                 default:
1633                         do_warning("unknown op '%c'", op[0]);
1634                         return -1;
1635                 }
1636         } else {
1637                 if (strcmp(op, "++") == 0 ||
1638                     strcmp(op, "--") == 0) {
1639                         return 3;
1640                 } else if (strcmp(op, ">>") == 0 ||
1641                            strcmp(op, "<<") == 0) {
1642                         return 8;
1643                 } else if (strcmp(op, ">=") == 0 ||
1644                            strcmp(op, "<=") == 0) {
1645                         return 9;
1646                 } else if (strcmp(op, "==") == 0 ||
1647                            strcmp(op, "!=") == 0) {
1648                         return 10;
1649                 } else if (strcmp(op, "&&") == 0) {
1650                         return 14;
1651                 } else if (strcmp(op, "||") == 0) {
1652                         return 15;
1653                 } else {
1654                         do_warning("unknown op '%s'", op);
1655                         return -1;
1656                 }
1657         }
1658 }
1659
1660 static int set_op_prio(struct print_arg *arg)
1661 {
1662
1663         /* single ops are the greatest */
1664         if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1665                 arg->op.prio = 0;
1666         else
1667                 arg->op.prio = get_op_prio(arg->op.op);
1668
1669         return arg->op.prio;
1670 }
1671
1672 /* Note, *tok does not get freed, but will most likely be saved */
1673 static enum event_type
1674 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1675 {
1676         struct print_arg *left, *right = NULL;
1677         enum event_type type;
1678         char *token;
1679
1680         /* the op is passed in via tok */
1681         token = *tok;
1682
1683         if (arg->type == PRINT_OP && !arg->op.left) {
1684                 /* handle single op */
1685                 if (token[1]) {
1686                         die("bad op token %s", token);
1687                         goto out_free;
1688                 }
1689                 switch (token[0]) {
1690                 case '~':
1691                 case '!':
1692                 case '+':
1693                 case '-':
1694                         break;
1695                 default:
1696                         do_warning("bad op token %s", token);
1697                         goto out_free;
1698
1699                 }
1700
1701                 /* make an empty left */
1702                 left = alloc_arg();
1703                 left->type = PRINT_NULL;
1704                 arg->op.left = left;
1705
1706                 right = alloc_arg();
1707                 arg->op.right = right;
1708
1709                 /* do not free the token, it belongs to an op */
1710                 *tok = NULL;
1711                 type = process_arg(event, right, tok);
1712
1713         } else if (strcmp(token, "?") == 0) {
1714
1715                 left = alloc_arg();
1716                 /* copy the top arg to the left */
1717                 *left = *arg;
1718
1719                 arg->type = PRINT_OP;
1720                 arg->op.op = token;
1721                 arg->op.left = left;
1722                 arg->op.prio = 0;
1723
1724                 type = process_cond(event, arg, tok);
1725
1726         } else if (strcmp(token, ">>") == 0 ||
1727                    strcmp(token, "<<") == 0 ||
1728                    strcmp(token, "&") == 0 ||
1729                    strcmp(token, "|") == 0 ||
1730                    strcmp(token, "&&") == 0 ||
1731                    strcmp(token, "||") == 0 ||
1732                    strcmp(token, "-") == 0 ||
1733                    strcmp(token, "+") == 0 ||
1734                    strcmp(token, "*") == 0 ||
1735                    strcmp(token, "^") == 0 ||
1736                    strcmp(token, "/") == 0 ||
1737                    strcmp(token, "<") == 0 ||
1738                    strcmp(token, ">") == 0 ||
1739                    strcmp(token, "==") == 0 ||
1740                    strcmp(token, "!=") == 0) {
1741
1742                 left = alloc_arg();
1743
1744                 /* copy the top arg to the left */
1745                 *left = *arg;
1746
1747                 arg->type = PRINT_OP;
1748                 arg->op.op = token;
1749                 arg->op.left = left;
1750
1751                 if (set_op_prio(arg) == -1) {
1752                         event->flags |= EVENT_FL_FAILED;
1753                         /* arg->op.op (= token) will be freed at out_free */
1754                         arg->op.op = NULL;
1755                         goto out_free;
1756                 }
1757
1758                 type = read_token_item(&token);
1759                 *tok = token;
1760
1761                 /* could just be a type pointer */
1762                 if ((strcmp(arg->op.op, "*") == 0) &&
1763                     type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1764                         char *new_atom;
1765
1766                         if (left->type != PRINT_ATOM)
1767                                 die("bad pointer type");
1768                         new_atom = realloc(left->atom.atom,
1769                                             strlen(left->atom.atom) + 3);
1770                         if (!new_atom)
1771                                 goto out_free;
1772
1773                         left->atom.atom = new_atom;
1774                         strcat(left->atom.atom, " *");
1775                         free(arg->op.op);
1776                         *arg = *left;
1777                         free(left);
1778
1779                         return type;
1780                 }
1781
1782                 right = alloc_arg();
1783                 type = process_arg_token(event, right, tok, type);
1784                 arg->op.right = right;
1785
1786         } else if (strcmp(token, "[") == 0) {
1787
1788                 left = alloc_arg();
1789                 *left = *arg;
1790
1791                 arg->type = PRINT_OP;
1792                 arg->op.op = token;
1793                 arg->op.left = left;
1794
1795                 arg->op.prio = 0;
1796
1797                 type = process_array(event, arg, tok);
1798
1799         } else {
1800                 do_warning("unknown op '%s'", token);
1801                 event->flags |= EVENT_FL_FAILED;
1802                 /* the arg is now the left side */
1803                 goto out_free;
1804         }
1805
1806         if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1807                 int prio;
1808
1809                 /* higher prios need to be closer to the root */
1810                 prio = get_op_prio(*tok);
1811
1812                 if (prio > arg->op.prio)
1813                         return process_op(event, arg, tok);
1814
1815                 return process_op(event, right, tok);
1816         }
1817
1818         return type;
1819
1820  out_free:
1821         free_token(token);
1822         *tok = NULL;
1823         return EVENT_ERROR;
1824 }
1825
1826 static enum event_type
1827 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1828               char **tok)
1829 {
1830         enum event_type type;
1831         char *field;
1832         char *token;
1833
1834         if (read_expected(EVENT_OP, "->") < 0)
1835                 goto out_err;
1836
1837         if (read_expect_type(EVENT_ITEM, &token) < 0)
1838                 goto out_free;
1839         field = token;
1840
1841         arg->type = PRINT_FIELD;
1842         arg->field.name = field;
1843
1844         if (is_flag_field) {
1845                 arg->field.field = pevent_find_any_field(event, arg->field.name);
1846                 arg->field.field->flags |= FIELD_IS_FLAG;
1847                 is_flag_field = 0;
1848         } else if (is_symbolic_field) {
1849                 arg->field.field = pevent_find_any_field(event, arg->field.name);
1850                 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1851                 is_symbolic_field = 0;
1852         }
1853
1854         type = read_token(&token);
1855         *tok = token;
1856
1857         return type;
1858
1859  out_free:
1860         free_token(token);
1861  out_err:
1862         *tok = NULL;
1863         return EVENT_ERROR;
1864 }
1865
1866 static char *arg_eval (struct print_arg *arg);
1867
1868 static unsigned long long
1869 eval_type_str(unsigned long long val, const char *type, int pointer)
1870 {
1871         int sign = 0;
1872         char *ref;
1873         int len;
1874
1875         len = strlen(type);
1876
1877         if (pointer) {
1878
1879                 if (type[len-1] != '*') {
1880                         do_warning("pointer expected with non pointer type");
1881                         return val;
1882                 }
1883
1884                 ref = malloc_or_die(len);
1885                 memcpy(ref, type, len);
1886
1887                 /* chop off the " *" */
1888                 ref[len - 2] = 0;
1889
1890                 val = eval_type_str(val, ref, 0);
1891                 free(ref);
1892                 return val;
1893         }
1894
1895         /* check if this is a pointer */
1896         if (type[len - 1] == '*')
1897                 return val;
1898
1899         /* Try to figure out the arg size*/
1900         if (strncmp(type, "struct", 6) == 0)
1901                 /* all bets off */
1902                 return val;
1903
1904         if (strcmp(type, "u8") == 0)
1905                 return val & 0xff;
1906
1907         if (strcmp(type, "u16") == 0)
1908                 return val & 0xffff;
1909
1910         if (strcmp(type, "u32") == 0)
1911                 return val & 0xffffffff;
1912
1913         if (strcmp(type, "u64") == 0 ||
1914             strcmp(type, "s64"))
1915                 return val;
1916
1917         if (strcmp(type, "s8") == 0)
1918                 return (unsigned long long)(char)val & 0xff;
1919
1920         if (strcmp(type, "s16") == 0)
1921                 return (unsigned long long)(short)val & 0xffff;
1922
1923         if (strcmp(type, "s32") == 0)
1924                 return (unsigned long long)(int)val & 0xffffffff;
1925
1926         if (strncmp(type, "unsigned ", 9) == 0) {
1927                 sign = 0;
1928                 type += 9;
1929         }
1930
1931         if (strcmp(type, "char") == 0) {
1932                 if (sign)
1933                         return (unsigned long long)(char)val & 0xff;
1934                 else
1935                         return val & 0xff;
1936         }
1937
1938         if (strcmp(type, "short") == 0) {
1939                 if (sign)
1940                         return (unsigned long long)(short)val & 0xffff;
1941                 else
1942                         return val & 0xffff;
1943         }
1944
1945         if (strcmp(type, "int") == 0) {
1946                 if (sign)
1947                         return (unsigned long long)(int)val & 0xffffffff;
1948                 else
1949                         return val & 0xffffffff;
1950         }
1951
1952         return val;
1953 }
1954
1955 /*
1956  * Try to figure out the type.
1957  */
1958 static unsigned long long
1959 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1960 {
1961         if (arg->type != PRINT_TYPE)
1962                 die("expected type argument");
1963
1964         return eval_type_str(val, arg->typecast.type, pointer);
1965 }
1966
1967 static int arg_num_eval(struct print_arg *arg, long long *val)
1968 {
1969         long long left, right;
1970         int ret = 1;
1971
1972         switch (arg->type) {
1973         case PRINT_ATOM:
1974                 *val = strtoll(arg->atom.atom, NULL, 0);
1975                 break;
1976         case PRINT_TYPE:
1977                 ret = arg_num_eval(arg->typecast.item, val);
1978                 if (!ret)
1979                         break;
1980                 *val = eval_type(*val, arg, 0);
1981                 break;
1982         case PRINT_OP:
1983                 switch (arg->op.op[0]) {
1984                 case '|':
1985                         ret = arg_num_eval(arg->op.left, &left);
1986                         if (!ret)
1987                                 break;
1988                         ret = arg_num_eval(arg->op.right, &right);
1989                         if (!ret)
1990                                 break;
1991                         if (arg->op.op[1])
1992                                 *val = left || right;
1993                         else
1994                                 *val = left | right;
1995                         break;
1996                 case '&':
1997                         ret = arg_num_eval(arg->op.left, &left);
1998                         if (!ret)
1999                                 break;
2000                         ret = arg_num_eval(arg->op.right, &right);
2001                         if (!ret)
2002                                 break;
2003                         if (arg->op.op[1])
2004                                 *val = left && right;
2005                         else
2006                                 *val = left & right;
2007                         break;
2008                 case '<':
2009                         ret = arg_num_eval(arg->op.left, &left);
2010                         if (!ret)
2011                                 break;
2012                         ret = arg_num_eval(arg->op.right, &right);
2013                         if (!ret)
2014                                 break;
2015                         switch (arg->op.op[1]) {
2016                         case 0:
2017                                 *val = left < right;
2018                                 break;
2019                         case '<':
2020                                 *val = left << right;
2021                                 break;
2022                         case '=':
2023                                 *val = left <= right;
2024                                 break;
2025                         default:
2026                                 do_warning("unknown op '%s'", arg->op.op);
2027                                 ret = 0;
2028                         }
2029                         break;
2030                 case '>':
2031                         ret = arg_num_eval(arg->op.left, &left);
2032                         if (!ret)
2033                                 break;
2034                         ret = arg_num_eval(arg->op.right, &right);
2035                         if (!ret)
2036                                 break;
2037                         switch (arg->op.op[1]) {
2038                         case 0:
2039                                 *val = left > right;
2040                                 break;
2041                         case '>':
2042                                 *val = left >> right;
2043                                 break;
2044                         case '=':
2045                                 *val = left >= right;
2046                                 break;
2047                         default:
2048                                 do_warning("unknown op '%s'", arg->op.op);
2049                                 ret = 0;
2050                         }
2051                         break;
2052                 case '=':
2053                         ret = arg_num_eval(arg->op.left, &left);
2054                         if (!ret)
2055                                 break;
2056                         ret = arg_num_eval(arg->op.right, &right);
2057                         if (!ret)
2058                                 break;
2059
2060                         if (arg->op.op[1] != '=') {
2061                                 do_warning("unknown op '%s'", arg->op.op);
2062                                 ret = 0;
2063                         } else
2064                                 *val = left == right;
2065                         break;
2066                 case '!':
2067                         ret = arg_num_eval(arg->op.left, &left);
2068                         if (!ret)
2069                                 break;
2070                         ret = arg_num_eval(arg->op.right, &right);
2071                         if (!ret)
2072                                 break;
2073
2074                         switch (arg->op.op[1]) {
2075                         case '=':
2076                                 *val = left != right;
2077                                 break;
2078                         default:
2079                                 do_warning("unknown op '%s'", arg->op.op);
2080                                 ret = 0;
2081                         }
2082                         break;
2083                 case '-':
2084                         /* check for negative */
2085                         if (arg->op.left->type == PRINT_NULL)
2086                                 left = 0;
2087                         else
2088                                 ret = arg_num_eval(arg->op.left, &left);
2089                         if (!ret)
2090                                 break;
2091                         ret = arg_num_eval(arg->op.right, &right);
2092                         if (!ret)
2093                                 break;
2094                         *val = left - right;
2095                         break;
2096                 case '+':
2097                         if (arg->op.left->type == PRINT_NULL)
2098                                 left = 0;
2099                         else
2100                                 ret = arg_num_eval(arg->op.left, &left);
2101                         if (!ret)
2102                                 break;
2103                         ret = arg_num_eval(arg->op.right, &right);
2104                         if (!ret)
2105                                 break;
2106                         *val = left + right;
2107                         break;
2108                 default:
2109                         do_warning("unknown op '%s'", arg->op.op);
2110                         ret = 0;
2111                 }
2112                 break;
2113
2114         case PRINT_NULL:
2115         case PRINT_FIELD ... PRINT_SYMBOL:
2116         case PRINT_STRING:
2117         case PRINT_BSTRING:
2118         default:
2119                 do_warning("invalid eval type %d", arg->type);
2120                 ret = 0;
2121
2122         }
2123         return ret;
2124 }
2125
2126 static char *arg_eval (struct print_arg *arg)
2127 {
2128         long long val;
2129         static char buf[20];
2130
2131         switch (arg->type) {
2132         case PRINT_ATOM:
2133                 return arg->atom.atom;
2134         case PRINT_TYPE:
2135                 return arg_eval(arg->typecast.item);
2136         case PRINT_OP:
2137                 if (!arg_num_eval(arg, &val))
2138                         break;
2139                 sprintf(buf, "%lld", val);
2140                 return buf;
2141
2142         case PRINT_NULL:
2143         case PRINT_FIELD ... PRINT_SYMBOL:
2144         case PRINT_STRING:
2145         case PRINT_BSTRING:
2146         default:
2147                 die("invalid eval type %d", arg->type);
2148                 break;
2149         }
2150
2151         return NULL;
2152 }
2153
2154 static enum event_type
2155 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2156 {
2157         enum event_type type;
2158         struct print_arg *arg = NULL;
2159         struct print_flag_sym *field;
2160         char *token = *tok;
2161         char *value;
2162
2163         do {
2164                 free_token(token);
2165                 type = read_token_item(&token);
2166                 if (test_type_token(type, token, EVENT_OP, "{"))
2167                         break;
2168
2169                 arg = alloc_arg();
2170
2171                 free_token(token);
2172                 type = process_arg(event, arg, &token);
2173
2174                 if (type == EVENT_OP)
2175                         type = process_op(event, arg, &token);
2176
2177                 if (type == EVENT_ERROR)
2178                         goto out_free;
2179
2180                 if (test_type_token(type, token, EVENT_DELIM, ","))
2181                         goto out_free;
2182
2183                 field = malloc_or_die(sizeof(*field));
2184                 memset(field, 0, sizeof(*field));
2185
2186                 value = arg_eval(arg);
2187                 if (value == NULL)
2188                         goto out_free;
2189                 field->value = strdup(value);
2190                 if (field->value == NULL)
2191                         goto out_free;
2192
2193                 free_arg(arg);
2194                 arg = alloc_arg();
2195
2196                 free_token(token);
2197                 type = process_arg(event, arg, &token);
2198                 if (test_type_token(type, token, EVENT_OP, "}"))
2199                         goto out_free;
2200
2201                 value = arg_eval(arg);
2202                 if (value == NULL)
2203                         goto out_free;
2204                 field->str = strdup(value);
2205                 if (field->str == NULL)
2206                         goto out_free;
2207                 free_arg(arg);
2208                 arg = NULL;
2209
2210                 *list = field;
2211                 list = &field->next;
2212
2213                 free_token(token);
2214                 type = read_token_item(&token);
2215         } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2216
2217         *tok = token;
2218         return type;
2219
2220 out_free:
2221         free_arg(arg);
2222         free_token(token);
2223         *tok = NULL;
2224
2225         return EVENT_ERROR;
2226 }
2227
2228 static enum event_type
2229 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2230 {
2231         struct print_arg *field;
2232         enum event_type type;
2233         char *token;
2234
2235         memset(arg, 0, sizeof(*arg));
2236         arg->type = PRINT_FLAGS;
2237
2238         field = alloc_arg();
2239
2240         type = process_arg(event, field, &token);
2241
2242         /* Handle operations in the first argument */
2243         while (type == EVENT_OP)
2244                 type = process_op(event, field, &token);
2245
2246         if (test_type_token(type, token, EVENT_DELIM, ","))
2247                 goto out_free;
2248         free_token(token);
2249
2250         arg->flags.field = field;
2251
2252         type = read_token_item(&token);
2253         if (event_item_type(type)) {
2254                 arg->flags.delim = token;
2255                 type = read_token_item(&token);
2256         }
2257
2258         if (test_type_token(type, token, EVENT_DELIM, ","))
2259                 goto out_free;
2260
2261         type = process_fields(event, &arg->flags.flags, &token);
2262         if (test_type_token(type, token, EVENT_DELIM, ")"))
2263                 goto out_free;
2264
2265         free_token(token);
2266         type = read_token_item(tok);
2267         return type;
2268
2269  out_free:
2270         free_token(token);
2271         *tok = NULL;
2272         return EVENT_ERROR;
2273 }
2274
2275 static enum event_type
2276 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2277 {
2278         struct print_arg *field;
2279         enum event_type type;
2280         char *token;
2281
2282         memset(arg, 0, sizeof(*arg));
2283         arg->type = PRINT_SYMBOL;
2284
2285         field = alloc_arg();
2286
2287         type = process_arg(event, field, &token);
2288         if (test_type_token(type, token, EVENT_DELIM, ","))
2289                 goto out_free;
2290
2291         arg->symbol.field = field;
2292
2293         type = process_fields(event, &arg->symbol.symbols, &token);
2294         if (test_type_token(type, token, EVENT_DELIM, ")"))
2295                 goto out_free;
2296
2297         free_token(token);
2298         type = read_token_item(tok);
2299         return type;
2300
2301  out_free:
2302         free_token(token);
2303         *tok = NULL;
2304         return EVENT_ERROR;
2305 }
2306
2307 static enum event_type
2308 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2309 {
2310         struct print_arg *field;
2311         enum event_type type;
2312         char *token;
2313
2314         memset(arg, 0, sizeof(*arg));
2315         arg->type = PRINT_HEX;
2316
2317         field = alloc_arg();
2318         type = process_arg(event, field, &token);
2319
2320         if (test_type_token(type, token, EVENT_DELIM, ","))
2321                 goto out_free;
2322
2323         arg->hex.field = field;
2324
2325         free_token(token);
2326
2327         field = alloc_arg();
2328         type = process_arg(event, field, &token);
2329
2330         if (test_type_token(type, token, EVENT_DELIM, ")"))
2331                 goto out_free;
2332
2333         arg->hex.size = field;
2334
2335         free_token(token);
2336         type = read_token_item(tok);
2337         return type;
2338
2339  out_free:
2340         free_arg(field);
2341         free_token(token);
2342         *tok = NULL;
2343         return EVENT_ERROR;
2344 }
2345
2346 static enum event_type
2347 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2348 {
2349         struct format_field *field;
2350         enum event_type type;
2351         char *token;
2352
2353         memset(arg, 0, sizeof(*arg));
2354         arg->type = PRINT_DYNAMIC_ARRAY;
2355
2356         /*
2357          * The item within the parenthesis is another field that holds
2358          * the index into where the array starts.
2359          */
2360         type = read_token(&token);
2361         *tok = token;
2362         if (type != EVENT_ITEM)
2363                 goto out_free;
2364
2365         /* Find the field */
2366
2367         field = pevent_find_field(event, token);
2368         if (!field)
2369                 goto out_free;
2370
2371         arg->dynarray.field = field;
2372         arg->dynarray.index = 0;
2373
2374         if (read_expected(EVENT_DELIM, ")") < 0)
2375                 goto out_free;
2376
2377         free_token(token);
2378         type = read_token_item(&token);
2379         *tok = token;
2380         if (type != EVENT_OP || strcmp(token, "[") != 0)
2381                 return type;
2382
2383         free_token(token);
2384         arg = alloc_arg();
2385         type = process_arg(event, arg, &token);
2386         if (type == EVENT_ERROR)
2387                 goto out_free_arg;
2388
2389         if (!test_type_token(type, token, EVENT_OP, "]"))
2390                 goto out_free_arg;
2391
2392         free_token(token);
2393         type = read_token_item(tok);
2394         return type;
2395
2396  out_free_arg:
2397         free_arg(arg);
2398  out_free:
2399         free_token(token);
2400         *tok = NULL;
2401         return EVENT_ERROR;
2402 }
2403
2404 static enum event_type
2405 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2406 {
2407         struct print_arg *item_arg;
2408         enum event_type type;
2409         char *token;
2410
2411         type = process_arg(event, arg, &token);
2412
2413         if (type == EVENT_ERROR)
2414                 goto out_free;
2415
2416         if (type == EVENT_OP)
2417                 type = process_op(event, arg, &token);
2418
2419         if (type == EVENT_ERROR)
2420                 goto out_free;
2421
2422         if (test_type_token(type, token, EVENT_DELIM, ")"))
2423                 goto out_free;
2424
2425         free_token(token);
2426         type = read_token_item(&token);
2427
2428         /*
2429          * If the next token is an item or another open paren, then
2430          * this was a typecast.
2431          */
2432         if (event_item_type(type) ||
2433             (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2434
2435                 /* make this a typecast and contine */
2436
2437                 /* prevous must be an atom */
2438                 if (arg->type != PRINT_ATOM)
2439                         die("previous needed to be PRINT_ATOM");
2440
2441                 item_arg = alloc_arg();
2442
2443                 arg->type = PRINT_TYPE;
2444                 arg->typecast.type = arg->atom.atom;
2445                 arg->typecast.item = item_arg;
2446                 type = process_arg_token(event, item_arg, &token, type);
2447
2448         }
2449
2450         *tok = token;
2451         return type;
2452
2453  out_free:
2454         free_token(token);
2455         *tok = NULL;
2456         return EVENT_ERROR;
2457 }
2458
2459
2460 static enum event_type
2461 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2462             char **tok)
2463 {
2464         enum event_type type;
2465         char *token;
2466
2467         if (read_expect_type(EVENT_ITEM, &token) < 0)
2468                 goto out_free;
2469
2470         arg->type = PRINT_STRING;
2471         arg->string.string = token;
2472         arg->string.offset = -1;
2473
2474         if (read_expected(EVENT_DELIM, ")") < 0)
2475                 goto out_err;
2476
2477         type = read_token(&token);
2478         *tok = token;
2479
2480         return type;
2481
2482  out_free:
2483         free_token(token);
2484  out_err:
2485         *tok = NULL;
2486         return EVENT_ERROR;
2487 }
2488
2489 static struct pevent_function_handler *
2490 find_func_handler(struct pevent *pevent, char *func_name)
2491 {
2492         struct pevent_function_handler *func;
2493
2494         for (func = pevent->func_handlers; func; func = func->next) {
2495                 if (strcmp(func->name, func_name) == 0)
2496                         break;
2497         }
2498
2499         return func;
2500 }
2501
2502 static void remove_func_handler(struct pevent *pevent, char *func_name)
2503 {
2504         struct pevent_function_handler *func;
2505         struct pevent_function_handler **next;
2506
2507         next = &pevent->func_handlers;
2508         while ((func = *next)) {
2509                 if (strcmp(func->name, func_name) == 0) {
2510                         *next = func->next;
2511                         free_func_handle(func);
2512                         break;
2513                 }
2514                 next = &func->next;
2515         }
2516 }
2517
2518 static enum event_type
2519 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2520                      struct print_arg *arg, char **tok)
2521 {
2522         struct print_arg **next_arg;
2523         struct print_arg *farg;
2524         enum event_type type;
2525         char *token;
2526         char *test;
2527         int i;
2528
2529         arg->type = PRINT_FUNC;
2530         arg->func.func = func;
2531
2532         *tok = NULL;
2533
2534         next_arg = &(arg->func.args);
2535         for (i = 0; i < func->nr_args; i++) {
2536                 farg = alloc_arg();
2537                 type = process_arg(event, farg, &token);
2538                 if (i < (func->nr_args - 1))
2539                         test = ",";
2540                 else
2541                         test = ")";
2542
2543                 if (test_type_token(type, token, EVENT_DELIM, test)) {
2544                         free_arg(farg);
2545                         free_token(token);
2546                         return EVENT_ERROR;
2547                 }
2548
2549                 *next_arg = farg;
2550                 next_arg = &(farg->next);
2551                 free_token(token);
2552         }
2553
2554         type = read_token(&token);
2555         *tok = token;
2556
2557         return type;
2558 }
2559
2560 static enum event_type
2561 process_function(struct event_format *event, struct print_arg *arg,
2562                  char *token, char **tok)
2563 {
2564         struct pevent_function_handler *func;
2565
2566         if (strcmp(token, "__print_flags") == 0) {
2567                 free_token(token);
2568                 is_flag_field = 1;
2569                 return process_flags(event, arg, tok);
2570         }
2571         if (strcmp(token, "__print_symbolic") == 0) {
2572                 free_token(token);
2573                 is_symbolic_field = 1;
2574                 return process_symbols(event, arg, tok);
2575         }
2576         if (strcmp(token, "__print_hex") == 0) {
2577                 free_token(token);
2578                 return process_hex(event, arg, tok);
2579         }
2580         if (strcmp(token, "__get_str") == 0) {
2581                 free_token(token);
2582                 return process_str(event, arg, tok);
2583         }
2584         if (strcmp(token, "__get_dynamic_array") == 0) {
2585                 free_token(token);
2586                 return process_dynamic_array(event, arg, tok);
2587         }
2588
2589         func = find_func_handler(event->pevent, token);
2590         if (func) {
2591                 free_token(token);
2592                 return process_func_handler(event, func, arg, tok);
2593         }
2594
2595         do_warning("function %s not defined", token);
2596         free_token(token);
2597         return EVENT_ERROR;
2598 }
2599
2600 static enum event_type
2601 process_arg_token(struct event_format *event, struct print_arg *arg,
2602                   char **tok, enum event_type type)
2603 {
2604         char *token;
2605         char *atom;
2606
2607         token = *tok;
2608
2609         switch (type) {
2610         case EVENT_ITEM:
2611                 if (strcmp(token, "REC") == 0) {
2612                         free_token(token);
2613                         type = process_entry(event, arg, &token);
2614                         break;
2615                 }
2616                 atom = token;
2617                 /* test the next token */
2618                 type = read_token_item(&token);
2619
2620                 /*
2621                  * If the next token is a parenthesis, then this
2622                  * is a function.
2623                  */
2624                 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2625                         free_token(token);
2626                         token = NULL;
2627                         /* this will free atom. */
2628                         type = process_function(event, arg, atom, &token);
2629                         break;
2630                 }
2631                 /* atoms can be more than one token long */
2632                 while (type == EVENT_ITEM) {
2633                         char *new_atom;
2634                         new_atom = realloc(atom,
2635                                            strlen(atom) + strlen(token) + 2);
2636                         if (!new_atom) {
2637                                 free(atom);
2638                                 *tok = NULL;
2639                                 free_token(token);
2640                                 return EVENT_ERROR;
2641                         }
2642                         atom = new_atom;
2643                         strcat(atom, " ");
2644                         strcat(atom, token);
2645                         free_token(token);
2646                         type = read_token_item(&token);
2647                 }
2648
2649                 arg->type = PRINT_ATOM;
2650                 arg->atom.atom = atom;
2651                 break;
2652
2653         case EVENT_DQUOTE:
2654         case EVENT_SQUOTE:
2655                 arg->type = PRINT_ATOM;
2656                 arg->atom.atom = token;
2657                 type = read_token_item(&token);
2658                 break;
2659         case EVENT_DELIM:
2660                 if (strcmp(token, "(") == 0) {
2661                         free_token(token);
2662                         type = process_paren(event, arg, &token);
2663                         break;
2664                 }
2665         case EVENT_OP:
2666                 /* handle single ops */
2667                 arg->type = PRINT_OP;
2668                 arg->op.op = token;
2669                 arg->op.left = NULL;
2670                 type = process_op(event, arg, &token);
2671
2672                 /* On error, the op is freed */
2673                 if (type == EVENT_ERROR)
2674                         arg->op.op = NULL;
2675
2676                 /* return error type if errored */
2677                 break;
2678
2679         case EVENT_ERROR ... EVENT_NEWLINE:
2680         default:
2681                 die("unexpected type %d", type);
2682         }
2683         *tok = token;
2684
2685         return type;
2686 }
2687
2688 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2689 {
2690         enum event_type type = EVENT_ERROR;
2691         struct print_arg *arg;
2692         char *token;
2693         int args = 0;
2694
2695         do {
2696                 if (type == EVENT_NEWLINE) {
2697                         type = read_token_item(&token);
2698                         continue;
2699                 }
2700
2701                 arg = alloc_arg();
2702
2703                 type = process_arg(event, arg, &token);
2704
2705                 if (type == EVENT_ERROR) {
2706                         free_token(token);
2707                         free_arg(arg);
2708                         return -1;
2709                 }
2710
2711                 *list = arg;
2712                 args++;
2713
2714                 if (type == EVENT_OP) {
2715                         type = process_op(event, arg, &token);
2716                         free_token(token);
2717                         if (type == EVENT_ERROR) {
2718                                 *list = NULL;
2719                                 free_arg(arg);
2720                                 return -1;
2721                         }
2722                         list = &arg->next;
2723                         continue;
2724                 }
2725
2726                 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2727                         free_token(token);
2728                         *list = arg;
2729                         list = &arg->next;
2730                         continue;
2731                 }
2732                 break;
2733         } while (type != EVENT_NONE);
2734
2735         if (type != EVENT_NONE && type != EVENT_ERROR)
2736                 free_token(token);
2737
2738         return args;
2739 }
2740
2741 static int event_read_print(struct event_format *event)
2742 {
2743         enum event_type type;
2744         char *token;
2745         int ret;
2746
2747         if (read_expected_item(EVENT_ITEM, "print") < 0)
2748                 return -1;
2749
2750         if (read_expected(EVENT_ITEM, "fmt") < 0)
2751                 return -1;
2752
2753         if (read_expected(EVENT_OP, ":") < 0)
2754                 return -1;
2755
2756         if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2757                 goto fail;
2758
2759  concat:
2760         event->print_fmt.format = token;
2761         event->print_fmt.args = NULL;
2762
2763         /* ok to have no arg */
2764         type = read_token_item(&token);
2765
2766         if (type == EVENT_NONE)
2767                 return 0;
2768
2769         /* Handle concatenation of print lines */
2770         if (type == EVENT_DQUOTE) {
2771                 char *cat;
2772
2773                 cat = malloc_or_die(strlen(event->print_fmt.format) +
2774                                     strlen(token) + 1);
2775                 strcpy(cat, event->print_fmt.format);
2776                 strcat(cat, token);
2777                 free_token(token);
2778                 free_token(event->print_fmt.format);
2779                 event->print_fmt.format = NULL;
2780                 token = cat;
2781                 goto concat;
2782         }
2783                              
2784         if (test_type_token(type, token, EVENT_DELIM, ","))
2785                 goto fail;
2786
2787         free_token(token);
2788
2789         ret = event_read_print_args(event, &event->print_fmt.args);
2790         if (ret < 0)
2791                 return -1;
2792
2793         return ret;
2794
2795  fail:
2796         free_token(token);
2797         return -1;
2798 }
2799
2800 /**
2801  * pevent_find_common_field - return a common field by event
2802  * @event: handle for the event
2803  * @name: the name of the common field to return
2804  *
2805  * Returns a common field from the event by the given @name.
2806  * This only searchs the common fields and not all field.
2807  */
2808 struct format_field *
2809 pevent_find_common_field(struct event_format *event, const char *name)
2810 {
2811         struct format_field *format;
2812
2813         for (format = event->format.common_fields;
2814              format; format = format->next) {
2815                 if (strcmp(format->name, name) == 0)
2816                         break;
2817         }
2818
2819         return format;
2820 }
2821
2822 /**
2823  * pevent_find_field - find a non-common field
2824  * @event: handle for the event
2825  * @name: the name of the non-common field
2826  *
2827  * Returns a non-common field by the given @name.
2828  * This does not search common fields.
2829  */
2830 struct format_field *
2831 pevent_find_field(struct event_format *event, const char *name)
2832 {
2833         struct format_field *format;
2834
2835         for (format = event->format.fields;
2836              format; format = format->next) {
2837                 if (strcmp(format->name, name) == 0)
2838                         break;
2839         }
2840
2841         return format;
2842 }
2843
2844 /**
2845  * pevent_find_any_field - find any field by name
2846  * @event: handle for the event
2847  * @name: the name of the field
2848  *
2849  * Returns a field by the given @name.
2850  * This searchs the common field names first, then
2851  * the non-common ones if a common one was not found.
2852  */
2853 struct format_field *
2854 pevent_find_any_field(struct event_format *event, const char *name)
2855 {
2856         struct format_field *format;
2857
2858         format = pevent_find_common_field(event, name);
2859         if (format)
2860                 return format;
2861         return pevent_find_field(event, name);
2862 }
2863
2864 /**
2865  * pevent_read_number - read a number from data
2866  * @pevent: handle for the pevent
2867  * @ptr: the raw data
2868  * @size: the size of the data that holds the number
2869  *
2870  * Returns the number (converted to host) from the
2871  * raw data.
2872  */
2873 unsigned long long pevent_read_number(struct pevent *pevent,
2874                                       const void *ptr, int size)
2875 {
2876         switch (size) {
2877         case 1:
2878                 return *(unsigned char *)ptr;
2879         case 2:
2880                 return data2host2(pevent, ptr);
2881         case 4:
2882                 return data2host4(pevent, ptr);
2883         case 8:
2884                 return data2host8(pevent, ptr);
2885         default:
2886                 /* BUG! */
2887                 return 0;
2888         }
2889 }
2890
2891 /**
2892  * pevent_read_number_field - read a number from data
2893  * @field: a handle to the field
2894  * @data: the raw data to read
2895  * @value: the value to place the number in
2896  *
2897  * Reads raw data according to a field offset and size,
2898  * and translates it into @value.
2899  *
2900  * Returns 0 on success, -1 otherwise.
2901  */
2902 int pevent_read_number_field(struct format_field *field, const void *data,
2903                              unsigned long long *value)
2904 {
2905         if (!field)
2906                 return -1;
2907         switch (field->size) {
2908         case 1:
2909         case 2:
2910         case 4:
2911         case 8:
2912                 *value = pevent_read_number(field->event->pevent,
2913                                             data + field->offset, field->size);
2914                 return 0;
2915         default:
2916                 return -1;
2917         }
2918 }
2919
2920 static int get_common_info(struct pevent *pevent,
2921                            const char *type, int *offset, int *size)
2922 {
2923         struct event_format *event;
2924         struct format_field *field;
2925
2926         /*
2927          * All events should have the same common elements.
2928          * Pick any event to find where the type is;
2929          */
2930         if (!pevent->events)
2931                 die("no event_list!");
2932
2933         event = pevent->events[0];
2934         field = pevent_find_common_field(event, type);
2935         if (!field)
2936                 return -1;
2937
2938         *offset = field->offset;
2939         *size = field->size;
2940
2941         return 0;
2942 }
2943
2944 static int __parse_common(struct pevent *pevent, void *data,
2945                           int *size, int *offset, const char *name)
2946 {
2947         int ret;
2948
2949         if (!*size) {
2950                 ret = get_common_info(pevent, name, offset, size);
2951                 if (ret < 0)
2952                         return ret;
2953         }
2954         return pevent_read_number(pevent, data + *offset, *size);
2955 }
2956
2957 static int trace_parse_common_type(struct pevent *pevent, void *data)
2958 {
2959         return __parse_common(pevent, data,
2960                               &pevent->type_size, &pevent->type_offset,
2961                               "common_type");
2962 }
2963
2964 static int parse_common_pid(struct pevent *pevent, void *data)
2965 {
2966         return __parse_common(pevent, data,
2967                               &pevent->pid_size, &pevent->pid_offset,
2968                               "common_pid");
2969 }
2970
2971 static int parse_common_pc(struct pevent *pevent, void *data)
2972 {
2973         return __parse_common(pevent, data,
2974                               &pevent->pc_size, &pevent->pc_offset,
2975                               "common_preempt_count");
2976 }
2977
2978 static int parse_common_flags(struct pevent *pevent, void *data)
2979 {
2980         return __parse_common(pevent, data,
2981                               &pevent->flags_size, &pevent->flags_offset,
2982                               "common_flags");
2983 }
2984
2985 static int parse_common_lock_depth(struct pevent *pevent, void *data)
2986 {
2987         return __parse_common(pevent, data,
2988                               &pevent->ld_size, &pevent->ld_offset,
2989                               "common_lock_depth");
2990 }
2991
2992 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2993 {
2994         return __parse_common(pevent, data,
2995                               &pevent->ld_size, &pevent->ld_offset,
2996                               "common_migrate_disable");
2997 }
2998
2999 static int events_id_cmp(const void *a, const void *b);
3000
3001 /**
3002  * pevent_find_event - find an event by given id
3003  * @pevent: a handle to the pevent
3004  * @id: the id of the event
3005  *
3006  * Returns an event that has a given @id.
3007  */
3008 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3009 {
3010         struct event_format **eventptr;
3011         struct event_format key;
3012         struct event_format *pkey = &key;
3013
3014         /* Check cache first */
3015         if (pevent->last_event && pevent->last_event->id == id)
3016                 return pevent->last_event;
3017
3018         key.id = id;
3019
3020         eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3021                            sizeof(*pevent->events), events_id_cmp);
3022
3023         if (eventptr) {
3024                 pevent->last_event = *eventptr;
3025                 return *eventptr;
3026         }
3027
3028         return NULL;
3029 }
3030
3031 /**
3032  * pevent_find_event_by_name - find an event by given name
3033  * @pevent: a handle to the pevent
3034  * @sys: the system name to search for
3035  * @name: the name of the event to search for
3036  *
3037  * This returns an event with a given @name and under the system
3038  * @sys. If @sys is NULL the first event with @name is returned.
3039  */
3040 struct event_format *
3041 pevent_find_event_by_name(struct pevent *pevent,
3042                           const char *sys, const char *name)
3043 {
3044         struct event_format *event;
3045         int i;
3046
3047         if (pevent->last_event &&
3048             strcmp(pevent->last_event->name, name) == 0 &&
3049             (!sys || strcmp(pevent->last_event->system, sys) == 0))
3050                 return pevent->last_event;
3051
3052         for (i = 0; i < pevent->nr_events; i++) {
3053                 event = pevent->events[i];
3054                 if (strcmp(event->name, name) == 0) {
3055                         if (!sys)
3056                                 break;
3057                         if (strcmp(event->system, sys) == 0)
3058                                 break;
3059                 }
3060         }
3061         if (i == pevent->nr_events)
3062                 event = NULL;
3063
3064         pevent->last_event = event;
3065         return event;
3066 }
3067
3068 static unsigned long long
3069 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3070 {
3071         struct pevent *pevent = event->pevent;
3072         unsigned long long val = 0;
3073         unsigned long long left, right;
3074         struct print_arg *typearg = NULL;
3075         struct print_arg *larg;
3076         unsigned long offset;
3077         unsigned int field_size;
3078
3079         switch (arg->type) {
3080         case PRINT_NULL:
3081                 /* ?? */
3082                 return 0;
3083         case PRINT_ATOM:
3084                 return strtoull(arg->atom.atom, NULL, 0);
3085         case PRINT_FIELD:
3086                 if (!arg->field.field) {
3087                         arg->field.field = pevent_find_any_field(event, arg->field.name);
3088                         if (!arg->field.field)
3089                                 die("field %s not found", arg->field.name);
3090                 }
3091                 /* must be a number */
3092                 val = pevent_read_number(pevent, data + arg->field.field->offset,
3093                                 arg->field.field->size);
3094                 break;
3095         case PRINT_FLAGS:
3096         case PRINT_SYMBOL:
3097         case PRINT_HEX:
3098                 break;
3099         case PRINT_TYPE:
3100                 val = eval_num_arg(data, size, event, arg->typecast.item);
3101                 return eval_type(val, arg, 0);
3102         case PRINT_STRING:
3103         case PRINT_BSTRING:
3104                 return 0;
3105         case PRINT_FUNC: {
3106                 struct trace_seq s;
3107                 trace_seq_init(&s);
3108                 val = process_defined_func(&s, data, size, event, arg);
3109                 trace_seq_destroy(&s);
3110                 return val;
3111         }
3112         case PRINT_OP:
3113                 if (strcmp(arg->op.op, "[") == 0) {
3114                         /*
3115                          * Arrays are special, since we don't want
3116                          * to read the arg as is.
3117                          */
3118                         right = eval_num_arg(data, size, event, arg->op.right);
3119
3120                         /* handle typecasts */
3121                         larg = arg->op.left;
3122                         while (larg->type == PRINT_TYPE) {
3123                                 if (!typearg)
3124                                         typearg = larg;
3125                                 larg = larg->typecast.item;
3126                         }
3127
3128                         /* Default to long size */
3129                         field_size = pevent->long_size;
3130
3131                         switch (larg->type) {
3132                         case PRINT_DYNAMIC_ARRAY:
3133                                 offset = pevent_read_number(pevent,
3134                                                    data + larg->dynarray.field->offset,
3135                                                    larg->dynarray.field->size);
3136                                 if (larg->dynarray.field->elementsize)
3137                                         field_size = larg->dynarray.field->elementsize;
3138                                 /*
3139                                  * The actual length of the dynamic array is stored
3140                                  * in the top half of the field, and the offset
3141                                  * is in the bottom half of the 32 bit field.
3142                                  */
3143                                 offset &= 0xffff;
3144                                 offset += right;
3145                                 break;
3146                         case PRINT_FIELD:
3147                                 if (!larg->field.field) {
3148                                         larg->field.field =
3149                                                 pevent_find_any_field(event, larg->field.name);
3150                                         if (!larg->field.field)
3151                                                 die("field %s not found", larg->field.name);
3152                                 }
3153                                 field_size = larg->field.field->elementsize;
3154                                 offset = larg->field.field->offset +
3155                                         right * larg->field.field->elementsize;
3156                                 break;
3157                         default:
3158                                 goto default_op; /* oops, all bets off */
3159                         }
3160                         val = pevent_read_number(pevent,
3161                                                  data + offset, field_size);
3162                         if (typearg)
3163                                 val = eval_type(val, typearg, 1);
3164                         break;
3165                 } else if (strcmp(arg->op.op, "?") == 0) {
3166                         left = eval_num_arg(data, size, event, arg->op.left);
3167                         arg = arg->op.right;
3168                         if (left)
3169                                 val = eval_num_arg(data, size, event, arg->op.left);
3170                         else
3171                                 val = eval_num_arg(data, size, event, arg->op.right);
3172                         break;
3173                 }
3174  default_op:
3175                 left = eval_num_arg(data, size, event, arg->op.left);
3176                 right = eval_num_arg(data, size, event, arg->op.right);
3177                 switch (arg->op.op[0]) {
3178                 case '!':
3179                         switch (arg->op.op[1]) {
3180                         case 0:
3181                                 val = !right;
3182                                 break;
3183                         case '=':
3184                                 val = left != right;
3185                                 break;
3186                         default:
3187                                 die("unknown op '%s'", arg->op.op);
3188                         }
3189                         break;
3190                 case '~':
3191                         val = ~right;
3192                         break;
3193                 case '|':
3194                         if (arg->op.op[1])
3195                                 val = left || right;
3196                         else
3197                                 val = left | right;
3198                         break;
3199                 case '&':
3200                         if (arg->op.op[1])
3201                                 val = left && right;
3202                         else
3203                                 val = left & right;
3204                         break;
3205                 case '<':
3206                         switch (arg->op.op[1]) {
3207                         case 0:
3208                                 val = left < right;
3209                                 break;
3210                         case '<':
3211                                 val = left << right;
3212                                 break;
3213                         case '=':
3214                                 val = left <= right;
3215                                 break;
3216                         default:
3217                                 die("unknown op '%s'", arg->op.op);
3218                         }
3219                         break;
3220                 case '>':
3221                         switch (arg->op.op[1]) {
3222                         case 0:
3223                                 val = left > right;
3224                                 break;
3225                         case '>':
3226                                 val = left >> right;
3227                                 break;
3228                         case '=':
3229                                 val = left >= right;
3230                                 break;
3231                         default:
3232                                 die("unknown op '%s'", arg->op.op);
3233                         }
3234                         break;
3235                 case '=':
3236                         if (arg->op.op[1] != '=')
3237                                 die("unknown op '%s'", arg->op.op);
3238                         val = left == right;
3239                         break;
3240                 case '-':
3241                         val = left - right;
3242                         break;
3243                 case '+':
3244                         val = left + right;
3245                         break;
3246                 case '/':
3247                         val = left / right;
3248                         break;
3249                 case '*':
3250                         val = left * right;
3251                         break;
3252                 default:
3253                         die("unknown op '%s'", arg->op.op);
3254                 }
3255                 break;
3256         default: /* not sure what to do there */
3257                 return 0;
3258         }
3259         return val;
3260 }
3261
3262 struct flag {
3263         const char *name;
3264         unsigned long long value;
3265 };
3266
3267 static const struct flag flags[] = {
3268         { "HI_SOFTIRQ", 0 },
3269         { "TIMER_SOFTIRQ", 1 },
3270         { "NET_TX_SOFTIRQ", 2 },
3271         { "NET_RX_SOFTIRQ", 3 },
3272         { "BLOCK_SOFTIRQ", 4 },
3273         { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3274         { "TASKLET_SOFTIRQ", 6 },
3275         { "SCHED_SOFTIRQ", 7 },
3276         { "HRTIMER_SOFTIRQ", 8 },
3277         { "RCU_SOFTIRQ", 9 },
3278
3279         { "HRTIMER_NORESTART", 0 },
3280         { "HRTIMER_RESTART", 1 },
3281 };
3282
3283 static unsigned long long eval_flag(const char *flag)
3284 {
3285         int i;
3286
3287         /*
3288          * Some flags in the format files do not get converted.
3289          * If the flag is not numeric, see if it is something that
3290          * we already know about.
3291          */
3292         if (isdigit(flag[0]))
3293                 return strtoull(flag, NULL, 0);
3294
3295         for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3296                 if (strcmp(flags[i].name, flag) == 0)
3297                         return flags[i].value;
3298
3299         return 0;
3300 }
3301
3302 static void print_str_to_seq(struct trace_seq *s, const char *format,
3303                              int len_arg, const char *str)
3304 {
3305         if (len_arg >= 0)
3306                 trace_seq_printf(s, format, len_arg, str);
3307         else
3308                 trace_seq_printf(s, format, str);
3309 }
3310
3311 static void print_str_arg(struct trace_seq *s, void *data, int size,
3312                           struct event_format *event, const char *format,
3313                           int len_arg, struct print_arg *arg)
3314 {
3315         struct pevent *pevent = event->pevent;
3316         struct print_flag_sym *flag;
3317         struct format_field *field;
3318         unsigned long long val, fval;
3319         unsigned long addr;
3320         char *str;
3321         unsigned char *hex;
3322         int print;
3323         int i, len;
3324
3325         switch (arg->type) {
3326         case PRINT_NULL:
3327                 /* ?? */
3328                 return;
3329         case PRINT_ATOM:
3330                 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3331                 return;
3332         case PRINT_FIELD:
3333                 field = arg->field.field;
3334                 if (!field) {
3335                         field = pevent_find_any_field(event, arg->field.name);
3336                         if (!field)
3337                                 die("field %s not found", arg->field.name);
3338                         arg->field.field = field;
3339                 }
3340                 /* Zero sized fields, mean the rest of the data */
3341                 len = field->size ? : size - field->offset;
3342
3343                 /*
3344                  * Some events pass in pointers. If this is not an array
3345                  * and the size is the same as long_size, assume that it
3346                  * is a pointer.
3347                  */
3348                 if (!(field->flags & FIELD_IS_ARRAY) &&
3349                     field->size == pevent->long_size) {
3350                         addr = *(unsigned long *)(data + field->offset);
3351                         trace_seq_printf(s, "%lx", addr);
3352                         break;
3353                 }
3354                 str = malloc_or_die(len + 1);
3355                 memcpy(str, data + field->offset, len);
3356                 str[len] = 0;
3357                 print_str_to_seq(s, format, len_arg, str);
3358                 free(str);
3359                 break;
3360         case PRINT_FLAGS:
3361                 val = eval_num_arg(data, size, event, arg->flags.field);
3362                 print = 0;
3363                 for (flag = arg->flags.flags; flag; flag = flag->next) {
3364                         fval = eval_flag(flag->value);
3365                         if (!val && !fval) {
3366                                 print_str_to_seq(s, format, len_arg, flag->str);
3367                                 break;
3368                         }
3369                         if (fval && (val & fval) == fval) {
3370                                 if (print && arg->flags.delim)
3371                                         trace_seq_puts(s, arg->flags.delim);
3372                                 print_str_to_seq(s, format, len_arg, flag->str);
3373                                 print = 1;
3374                                 val &= ~fval;
3375                         }
3376                 }
3377                 break;
3378         case PRINT_SYMBOL:
3379                 val = eval_num_arg(data, size, event, arg->symbol.field);
3380                 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3381                         fval = eval_flag(flag->value);
3382                         if (val == fval) {
3383                                 print_str_to_seq(s, format, len_arg, flag->str);
3384                                 break;
3385                         }
3386                 }
3387                 break;
3388         case PRINT_HEX:
3389                 field = arg->hex.field->field.field;
3390                 if (!field) {
3391                         str = arg->hex.field->field.name;
3392                         field = pevent_find_any_field(event, str);
3393                         if (!field)
3394                                 die("field %s not found", str);
3395                         arg->hex.field->field.field = field;
3396                 }
3397                 hex = data + field->offset;
3398                 len = eval_num_arg(data, size, event, arg->hex.size);
3399                 for (i = 0; i < len; i++) {
3400                         if (i)
3401                                 trace_seq_putc(s, ' ');
3402                         trace_seq_printf(s, "%02x", hex[i]);
3403                 }
3404                 break;
3405
3406         case PRINT_TYPE:
3407                 break;
3408         case PRINT_STRING: {
3409                 int str_offset;
3410
3411                 if (arg->string.offset == -1) {
3412                         struct format_field *f;
3413
3414                         f = pevent_find_any_field(event, arg->string.string);
3415                         arg->string.offset = f->offset;
3416                 }
3417                 str_offset = data2host4(pevent, data + arg->string.offset);
3418                 str_offset &= 0xffff;
3419                 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3420                 break;
3421         }
3422         case PRINT_BSTRING:
3423                 print_str_to_seq(s, format, len_arg, arg->string.string);
3424                 break;
3425         case PRINT_OP:
3426                 /*
3427                  * The only op for string should be ? :
3428                  */
3429                 if (arg->op.op[0] != '?')
3430                         return;
3431                 val = eval_num_arg(data, size, event, arg->op.left);
3432                 if (val)
3433                         print_str_arg(s, data, size, event,
3434                                       format, len_arg, arg->op.right->op.left);
3435                 else
3436                         print_str_arg(s, data, size, event,
3437                                       format, len_arg, arg->op.right->op.right);
3438                 break;
3439         case PRINT_FUNC:
3440                 process_defined_func(s, data, size, event, arg);
3441                 break;
3442         default:
3443                 /* well... */
3444                 break;
3445         }
3446 }
3447
3448 static unsigned long long
3449 process_defined_func(struct trace_seq *s, void *data, int size,
3450                      struct event_format *event, struct print_arg *arg)
3451 {
3452         struct pevent_function_handler *func_handle = arg->func.func;
3453         struct pevent_func_params *param;
3454         unsigned long long *args;
3455         unsigned long long ret;
3456         struct print_arg *farg;
3457         struct trace_seq str;
3458         struct save_str {
3459                 struct save_str *next;
3460                 char *str;
3461         } *strings = NULL, *string;
3462         int i;
3463
3464         if (!func_handle->nr_args) {
3465                 ret = (*func_handle->func)(s, NULL);
3466                 goto out;
3467         }
3468
3469         farg = arg->func.args;
3470         param = func_handle->params;
3471
3472         args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3473         for (i = 0; i < func_handle->nr_args; i++) {
3474                 switch (param->type) {
3475                 case PEVENT_FUNC_ARG_INT:
3476                 case PEVENT_FUNC_ARG_LONG:
3477                 case PEVENT_FUNC_ARG_PTR:
3478                         args[i] = eval_num_arg(data, size, event, farg);
3479                         break;
3480                 case PEVENT_FUNC_ARG_STRING:
3481                         trace_seq_init(&str);
3482                         print_str_arg(&str, data, size, event, "%s", -1, farg);
3483                         trace_seq_terminate(&str);
3484                         string = malloc_or_die(sizeof(*string));
3485                         string->next = strings;
3486                         string->str = strdup(str.buffer);
3487                         if (!string->str)
3488                                 die("malloc str");
3489
3490                         args[i] = (uintptr_t)string->str;
3491                         strings = string;
3492                         trace_seq_destroy(&str);
3493                         break;
3494                 default:
3495                         /*
3496                          * Something went totally wrong, this is not
3497                          * an input error, something in this code broke.
3498                          */
3499                         die("Unexpected end of arguments\n");
3500                         break;
3501                 }
3502                 farg = farg->next;
3503                 param = param->next;
3504         }
3505
3506         ret = (*func_handle->func)(s, args);
3507         free(args);
3508         while (strings) {
3509                 string = strings;
3510                 strings = string->next;
3511                 free(string->str);
3512                 free(string);
3513         }
3514
3515  out:
3516         /* TBD : handle return type here */
3517         return ret;
3518 }
3519
3520 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3521 {
3522         struct pevent *pevent = event->pevent;
3523         struct format_field *field, *ip_field;
3524         struct print_arg *args, *arg, **next;
3525         unsigned long long ip, val;
3526         char *ptr;
3527         void *bptr;
3528         int vsize;
3529
3530         field = pevent->bprint_buf_field;
3531         ip_field = pevent->bprint_ip_field;
3532
3533         if (!field) {
3534                 field = pevent_find_field(event, "buf");
3535                 if (!field)
3536                         die("can't find buffer field for binary printk");
3537                 ip_field = pevent_find_field(event, "ip");
3538                 if (!ip_field)
3539                         die("can't find ip field for binary printk");
3540                 pevent->bprint_buf_field = field;
3541                 pevent->bprint_ip_field = ip_field;
3542         }
3543
3544         ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3545
3546         /*
3547          * The first arg is the IP pointer.
3548          */
3549         args = alloc_arg();
3550         arg = args;
3551         arg->next = NULL;
3552         next = &arg->next;
3553
3554         arg->type = PRINT_ATOM;
3555         arg->atom.atom = malloc_or_die(32);
3556         sprintf(arg->atom.atom, "%lld", ip);
3557
3558         /* skip the first "%pf : " */
3559         for (ptr = fmt + 6, bptr = data + field->offset;
3560              bptr < data + size && *ptr; ptr++) {
3561                 int ls = 0;
3562
3563                 if (*ptr == '%') {
3564  process_again:
3565                         ptr++;
3566                         switch (*ptr) {
3567                         case '%':
3568                                 break;
3569                         case 'l':
3570                                 ls++;
3571                                 goto process_again;
3572                         case 'L':
3573                                 ls = 2;
3574                                 goto process_again;
3575                         case '0' ... '9':
3576                                 goto process_again;
3577                         case '.':
3578                                 goto process_again;
3579                         case 'p':
3580                                 ls = 1;
3581                                 /* fall through */
3582                         case 'd':
3583                         case 'u':
3584                         case 'x':
3585                         case 'i':
3586                                 switch (ls) {
3587                                 case 0:
3588                                         vsize = 4;
3589                                         break;
3590                                 case 1:
3591                                         vsize = pevent->long_size;
3592                                         break;
3593                                 case 2:
3594                                         vsize = 8;
3595                                         break;
3596                                 default:
3597                                         vsize = ls; /* ? */
3598                                         break;
3599                                 }
3600                         /* fall through */
3601                         case '*':
3602                                 if (*ptr == '*')
3603                                         vsize = 4;
3604
3605                                 /* the pointers are always 4 bytes aligned */
3606                                 bptr = (void *)(((unsigned long)bptr + 3) &
3607                                                 ~3);
3608                                 val = pevent_read_number(pevent, bptr, vsize);
3609                                 bptr += vsize;
3610                                 arg = alloc_arg();
3611                                 arg->next = NULL;
3612                                 arg->type = PRINT_ATOM;
3613                                 arg->atom.atom = malloc_or_die(32);
3614                                 sprintf(arg->atom.atom, "%lld", val);
3615                                 *next = arg;
3616                                 next = &arg->next;
3617                                 /*
3618                                  * The '*' case means that an arg is used as the length.
3619                                  * We need to continue to figure out for what.
3620                                  */
3621                                 if (*ptr == '*')
3622                                         goto process_again;
3623
3624                                 break;
3625                         case 's':
3626                                 arg = alloc_arg();
3627                                 arg->next = NULL;
3628                                 arg->type = PRINT_BSTRING;
3629                                 arg->string.string = strdup(bptr);
3630                                 if (!arg->string.string)
3631                                         break;
3632                                 bptr += strlen(bptr) + 1;
3633                                 *next = arg;
3634                                 next = &arg->next;
3635                         default:
3636                                 break;
3637                         }
3638                 }
3639         }
3640
3641         return args;
3642 }
3643
3644 static void free_args(struct print_arg *args)
3645 {
3646         struct print_arg *next;
3647
3648         while (args) {
3649                 next = args->next;
3650
3651                 free_arg(args);
3652                 args = next;
3653         }
3654 }
3655
3656 static char *
3657 get_bprint_format(void *data, int size __maybe_unused,
3658                   struct event_format *event)
3659 {
3660         struct pevent *pevent = event->pevent;
3661         unsigned long long addr;
3662         struct format_field *field;
3663         struct printk_map *printk;
3664         char *format;
3665         char *p;
3666
3667         field = pevent->bprint_fmt_field;
3668
3669         if (!field) {
3670                 field = pevent_find_field(event, "fmt");
3671                 if (!field)
3672                         die("can't find format field for binary printk");
3673                 pevent->bprint_fmt_field = field;
3674         }
3675
3676         addr = pevent_read_number(pevent, data + field->offset, field->size);
3677
3678         printk = find_printk(pevent, addr);
3679         if (!printk) {
3680                 format = malloc_or_die(45);
3681                 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3682                         addr);
3683                 return format;
3684         }
3685
3686         p = printk->printk;
3687         /* Remove any quotes. */
3688         if (*p == '"')
3689                 p++;
3690         format = malloc_or_die(strlen(p) + 10);
3691         sprintf(format, "%s : %s", "%pf", p);
3692         /* remove ending quotes and new line since we will add one too */
3693         p = format + strlen(format) - 1;
3694         if (*p == '"')
3695                 *p = 0;
3696
3697         p -= 2;
3698         if (strcmp(p, "\\n") == 0)
3699                 *p = 0;
3700
3701         return format;
3702 }
3703
3704 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3705                           struct event_format *event, struct print_arg *arg)
3706 {
3707         unsigned char *buf;
3708         char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3709
3710         if (arg->type == PRINT_FUNC) {
3711                 process_defined_func(s, data, size, event, arg);
3712                 return;
3713         }
3714
3715         if (arg->type != PRINT_FIELD) {
3716                 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3717                                  arg->type);
3718                 return;
3719         }
3720
3721         if (mac == 'm')
3722                 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3723         if (!arg->field.field) {
3724                 arg->field.field =
3725                         pevent_find_any_field(event, arg->field.name);
3726                 if (!arg->field.field)
3727                         die("field %s not found", arg->field.name);
3728         }
3729         if (arg->field.field->size != 6) {
3730                 trace_seq_printf(s, "INVALIDMAC");
3731                 return;
3732         }
3733         buf = data + arg->field.field->offset;
3734         trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3735 }
3736
3737 static int is_printable_array(char *p, unsigned int len)
3738 {
3739         unsigned int i;
3740
3741         for (i = 0; i < len && p[i]; i++)
3742                 if (!isprint(p[i]))
3743                     return 0;
3744         return 1;
3745 }
3746
3747 static void print_event_fields(struct trace_seq *s, void *data, int size,
3748                                struct event_format *event)
3749 {
3750         struct format_field *field;
3751         unsigned long long val;
3752         unsigned int offset, len, i;
3753
3754         field = event->format.fields;
3755         while (field) {
3756                 trace_seq_printf(s, " %s=", field->name);
3757                 if (field->flags & FIELD_IS_ARRAY) {
3758                         offset = field->offset;
3759                         len = field->size;
3760                         if (field->flags & FIELD_IS_DYNAMIC) {
3761                                 val = pevent_read_number(event->pevent, data + offset, len);
3762                                 offset = val;
3763                                 len = offset >> 16;
3764                                 offset &= 0xffff;
3765                         }
3766                         if (field->flags & FIELD_IS_STRING &&
3767                             is_printable_array(data + offset, len)) {
3768                                 trace_seq_printf(s, "%s", (char *)data + offset);
3769                         } else {
3770                                 trace_seq_puts(s, "ARRAY[");
3771                                 for (i = 0; i < len; i++) {
3772                                         if (i)
3773                                                 trace_seq_puts(s, ", ");
3774                                         trace_seq_printf(s, "%02x",
3775                                                          *((unsigned char *)data + offset + i));
3776                                 }
3777                                 trace_seq_putc(s, ']');
3778                                 field->flags &= ~FIELD_IS_STRING;
3779                         }
3780                 } else {
3781                         val = pevent_read_number(event->pevent, data + field->offset,
3782                                                  field->size);
3783                         if (field->flags & FIELD_IS_POINTER) {
3784                                 trace_seq_printf(s, "0x%llx", val);
3785                         } else if (field->flags & FIELD_IS_SIGNED) {
3786                                 switch (field->size) {
3787                                 case 4:
3788                                         /*
3789                                          * If field is long then print it in hex.
3790                                          * A long usually stores pointers.
3791                                          */
3792                                         if (field->flags & FIELD_IS_LONG)
3793                                                 trace_seq_printf(s, "0x%x", (int)val);
3794                                         else
3795                                                 trace_seq_printf(s, "%d", (int)val);
3796                                         break;
3797                                 case 2:
3798                                         trace_seq_printf(s, "%2d", (short)val);
3799                                         break;
3800                                 case 1:
3801                                         trace_seq_printf(s, "%1d", (char)val);
3802                                         break;
3803                                 default:
3804                                         trace_seq_printf(s, "%lld", val);
3805                                 }
3806                         } else {
3807                                 if (field->flags & FIELD_IS_LONG)
3808                                         trace_seq_printf(s, "0x%llx", val);
3809                                 else
3810                                         trace_seq_printf(s, "%llu", val);
3811                         }
3812                 }
3813                 field = field->next;
3814         }
3815 }
3816
3817 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3818 {
3819         struct pevent *pevent = event->pevent;
3820         struct print_fmt *print_fmt = &event->print_fmt;
3821         struct print_arg *arg = print_fmt->args;
3822         struct print_arg *args = NULL;
3823         const char *ptr = print_fmt->format;
3824         unsigned long long val;
3825         struct func_map *func;
3826         const char *saveptr;
3827         char *bprint_fmt = NULL;
3828         char format[32];
3829         int show_func;
3830         int len_as_arg;
3831         int len_arg;
3832         int len;
3833         int ls;
3834
3835         if (event->flags & EVENT_FL_FAILED) {
3836                 trace_seq_printf(s, "[FAILED TO PARSE]");
3837                 print_event_fields(s, data, size, event);
3838                 return;
3839         }
3840
3841         if (event->flags & EVENT_FL_ISBPRINT) {
3842                 bprint_fmt = get_bprint_format(data, size, event);
3843                 args = make_bprint_args(bprint_fmt, data, size, event);
3844                 arg = args;
3845                 ptr = bprint_fmt;
3846         }
3847
3848         for (; *ptr; ptr++) {
3849                 ls = 0;
3850                 if (*ptr == '\\') {
3851                         ptr++;
3852                         switch (*ptr) {
3853                         case 'n':
3854                                 trace_seq_putc(s, '\n');
3855                                 break;
3856                         case 't':
3857                                 trace_seq_putc(s, '\t');
3858                                 break;
3859                         case 'r':
3860                                 trace_seq_putc(s, '\r');
3861                                 break;
3862                         case '\\':
3863                                 trace_seq_putc(s, '\\');
3864                                 break;
3865                         default:
3866                                 trace_seq_putc(s, *ptr);
3867                                 break;
3868                         }
3869
3870                 } else if (*ptr == '%') {
3871                         saveptr = ptr;
3872                         show_func = 0;
3873                         len_as_arg = 0;
3874  cont_process:
3875                         ptr++;
3876                         switch (*ptr) {
3877                         case '%':
3878                                 trace_seq_putc(s, '%');
3879                                 break;
3880                         case '#':
3881                                 /* FIXME: need to handle properly */
3882                                 goto cont_process;
3883                         case 'h':
3884                                 ls--;
3885                                 goto cont_process;
3886                         case 'l':
3887                                 ls++;
3888                                 goto cont_process;
3889                         case 'L':
3890                                 ls = 2;
3891                                 goto cont_process;
3892                         case '*':
3893                                 /* The argument is the length. */
3894                                 if (!arg) {
3895                                         do_warning("no argument match");
3896                                         event->flags |= EVENT_FL_FAILED;
3897                                         goto out_failed;
3898                                 }
3899                                 len_arg = eval_num_arg(data, size, event, arg);
3900                                 len_as_arg = 1;
3901                                 arg = arg->next;
3902                                 goto cont_process;
3903                         case '.':
3904                         case 'z':
3905                         case 'Z':
3906                         case '0' ... '9':
3907                                 goto cont_process;
3908                         case 'p':
3909                                 if (pevent->long_size == 4)
3910                                         ls = 1;
3911                                 else
3912                                         ls = 2;
3913
3914                                 if (*(ptr+1) == 'F' ||
3915                                     *(ptr+1) == 'f') {
3916                                         ptr++;
3917                                         show_func = *ptr;
3918                                 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3919                                         print_mac_arg(s, *(ptr+1), data, size, event, arg);
3920                                         ptr++;
3921                                         arg = arg->next;
3922                                         break;
3923                                 }
3924
3925                                 /* fall through */
3926                         case 'd':
3927                         case 'i':
3928                         case 'x':
3929                         case 'X':
3930                         case 'u':
3931                                 if (!arg) {
3932                                         do_warning("no argument match");
3933                                         event->flags |= EVENT_FL_FAILED;
3934                                         goto out_failed;
3935                                 }
3936
3937                                 len = ((unsigned long)ptr + 1) -
3938                                         (unsigned long)saveptr;
3939
3940                                 /* should never happen */
3941                                 if (len > 31) {
3942                                         do_warning("bad format!");
3943                                         event->flags |= EVENT_FL_FAILED;
3944                                         len = 31;
3945                                 }
3946
3947                                 memcpy(format, saveptr, len);
3948                                 format[len] = 0;
3949
3950                                 val = eval_num_arg(data, size, event, arg);
3951                                 arg = arg->next;
3952
3953                                 if (show_func) {
3954                                         func = find_func(pevent, val);
3955                                         if (func) {
3956                                                 trace_seq_puts(s, func->func);
3957                                                 if (show_func == 'F')
3958                                                         trace_seq_printf(s,
3959                                                                "+0x%llx",
3960                                                                val - func->addr);
3961                                                 break;
3962                                         }
3963                                 }
3964                                 if (pevent->long_size == 8 && ls &&
3965                                     sizeof(long) != 8) {
3966                                         char *p;
3967
3968                                         ls = 2;
3969                                         /* make %l into %ll */
3970                                         p = strchr(format, 'l');
3971                                         if (p)
3972                                                 memmove(p+1, p, strlen(p)+1);
3973                                         else if (strcmp(format, "%p") == 0)
3974                                                 strcpy(format, "0x%llx");
3975                                 }
3976                                 switch (ls) {
3977                                 case -2:
3978                                         if (len_as_arg)
3979                                                 trace_seq_printf(s, format, len_arg, (char)val);
3980                                         else
3981                                                 trace_seq_printf(s, format, (char)val);
3982                                         break;
3983                                 case -1:
3984                                         if (len_as_arg)
3985                                                 trace_seq_printf(s, format, len_arg, (short)val);
3986                                         else
3987                                                 trace_seq_printf(s, format, (short)val);
3988                                         break;
3989                                 case 0:
3990                                         if (len_as_arg)
3991                                                 trace_seq_printf(s, format, len_arg, (int)val);
3992                                         else
3993                                                 trace_seq_printf(s, format, (int)val);
3994                                         break;
3995                                 case 1:
3996                                         if (len_as_arg)
3997                                                 trace_seq_printf(s, format, len_arg, (long)val);
3998                                         else
3999                                                 trace_seq_printf(s, format, (long)val);
4000                                         break;
4001                                 case 2:
4002                                         if (len_as_arg)
4003                                                 trace_seq_printf(s, format, len_arg,
4004                                                                  (long long)val);
4005                                         else
4006                                                 trace_seq_printf(s, format, (long long)val);
4007                                         break;
4008                                 default:
4009                                         do_warning("bad count (%d)", ls);
4010                                         event->flags |= EVENT_FL_FAILED;
4011                                 }
4012                                 break;
4013                         case 's':
4014                                 if (!arg) {
4015                                         do_warning("no matching argument");
4016                                         event->flags |= EVENT_FL_FAILED;
4017                                         goto out_failed;
4018                                 }
4019
4020                                 len = ((unsigned long)ptr + 1) -
4021                                         (unsigned long)saveptr;
4022
4023                                 /* should never happen */
4024                                 if (len > 31) {
4025                                         do_warning("bad format!");
4026                                         event->flags |= EVENT_FL_FAILED;
4027                                         len = 31;
4028                                 }
4029
4030                                 memcpy(format, saveptr, len);
4031                                 format[len] = 0;
4032                                 if (!len_as_arg)
4033                                         len_arg = -1;
4034                                 print_str_arg(s, data, size, event,
4035                                               format, len_arg, arg);
4036                                 arg = arg->next;
4037                                 break;
4038                         default:
4039                                 trace_seq_printf(s, ">%c<", *ptr);
4040
4041                         }
4042                 } else
4043                         trace_seq_putc(s, *ptr);
4044         }
4045
4046         if (event->flags & EVENT_FL_FAILED) {
4047 out_failed:
4048                 trace_seq_printf(s, "[FAILED TO PARSE]");
4049         }
4050
4051         if (args) {
4052                 free_args(args);
4053                 free(bprint_fmt);
4054         }
4055 }
4056
4057 /**
4058  * pevent_data_lat_fmt - parse the data for the latency format
4059  * @pevent: a handle to the pevent
4060  * @s: the trace_seq to write to
4061  * @record: the record to read from
4062  *
4063  * This parses out the Latency format (interrupts disabled,
4064  * need rescheduling, in hard/soft interrupt, preempt count
4065  * and lock depth) and places it into the trace_seq.
4066  */
4067 void pevent_data_lat_fmt(struct pevent *pevent,
4068                          struct trace_seq *s, struct pevent_record *record)
4069 {
4070         static int check_lock_depth = 1;
4071         static int check_migrate_disable = 1;
4072         static int lock_depth_exists;
4073         static int migrate_disable_exists;
4074         unsigned int lat_flags;
4075         unsigned int pc;
4076         int lock_depth;
4077         int migrate_disable;
4078         int hardirq;
4079         int softirq;
4080         void *data = record->data;
4081
4082         lat_flags = parse_common_flags(pevent, data);
4083         pc = parse_common_pc(pevent, data);
4084         /* lock_depth may not always exist */
4085         if (lock_depth_exists)
4086                 lock_depth = parse_common_lock_depth(pevent, data);
4087         else if (check_lock_depth) {
4088                 lock_depth = parse_common_lock_depth(pevent, data);
4089                 if (lock_depth < 0)
4090                         check_lock_depth = 0;
4091                 else
4092                         lock_depth_exists = 1;
4093         }
4094
4095         /* migrate_disable may not always exist */
4096         if (migrate_disable_exists)
4097                 migrate_disable = parse_common_migrate_disable(pevent, data);
4098         else if (check_migrate_disable) {
4099                 migrate_disable = parse_common_migrate_disable(pevent, data);
4100                 if (migrate_disable < 0)
4101                         check_migrate_disable = 0;
4102                 else
4103                         migrate_disable_exists = 1;
4104         }
4105
4106         hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4107         softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4108
4109         trace_seq_printf(s, "%c%c%c",
4110                (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4111                (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4112                'X' : '.',
4113                (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4114                'N' : '.',
4115                (hardirq && softirq) ? 'H' :
4116                hardirq ? 'h' : softirq ? 's' : '.');
4117
4118         if (pc)
4119                 trace_seq_printf(s, "%x", pc);
4120         else
4121                 trace_seq_putc(s, '.');
4122
4123         if (migrate_disable_exists) {
4124                 if (migrate_disable < 0)
4125                         trace_seq_putc(s, '.');
4126                 else
4127                         trace_seq_printf(s, "%d", migrate_disable);
4128         }
4129
4130         if (lock_depth_exists) {
4131                 if (lock_depth < 0)
4132                         trace_seq_putc(s, '.');
4133                 else
4134                         trace_seq_printf(s, "%d", lock_depth);
4135         }
4136
4137         trace_seq_terminate(s);
4138 }
4139
4140 /**
4141  * pevent_data_type - parse out the given event type
4142  * @pevent: a handle to the pevent
4143  * @rec: the record to read from
4144  *
4145  * This returns the event id from the @rec.
4146  */
4147 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4148 {
4149         return trace_parse_common_type(pevent, rec->data);
4150 }
4151
4152 /**
4153  * pevent_data_event_from_type - find the event by a given type
4154  * @pevent: a handle to the pevent
4155  * @type: the type of the event.
4156  *
4157  * This returns the event form a given @type;
4158  */
4159 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4160 {
4161         return pevent_find_event(pevent, type);
4162 }
4163
4164 /**
4165  * pevent_data_pid - parse the PID from raw data
4166  * @pevent: a handle to the pevent
4167  * @rec: the record to parse
4168  *
4169  * This returns the PID from a raw data.
4170  */
4171 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4172 {
4173         return parse_common_pid(pevent, rec->data);
4174 }
4175
4176 /**
4177  * pevent_data_comm_from_pid - return the command line from PID
4178  * @pevent: a handle to the pevent
4179  * @pid: the PID of the task to search for
4180  *
4181  * This returns a pointer to the command line that has the given
4182  * @pid.
4183  */
4184 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4185 {
4186         const char *comm;
4187
4188         comm = find_cmdline(pevent, pid);
4189         return comm;
4190 }
4191
4192 /**
4193  * pevent_data_comm_from_pid - parse the data into the print format
4194  * @s: the trace_seq to write to
4195  * @event: the handle to the event
4196  * @record: the record to read from
4197  *
4198  * This parses the raw @data using the given @event information and
4199  * writes the print format into the trace_seq.
4200  */
4201 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4202                        struct pevent_record *record)
4203 {
4204         int print_pretty = 1;
4205
4206         if (event->pevent->print_raw)
4207                 print_event_fields(s, record->data, record->size, event);
4208         else {
4209
4210                 if (event->handler)
4211                         print_pretty = event->handler(s, record, event,
4212                                                       event->context);
4213
4214                 if (print_pretty)
4215                         pretty_print(s, record->data, record->size, event);
4216         }
4217
4218         trace_seq_terminate(s);
4219 }
4220
4221 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4222                         struct pevent_record *record)
4223 {
4224         static char *spaces = "                    "; /* 20 spaces */
4225         struct event_format *event;
4226         unsigned long secs;
4227         unsigned long usecs;
4228         unsigned long nsecs;
4229         const char *comm;
4230         void *data = record->data;
4231         int type;
4232         int pid;
4233         int len;
4234         int p;
4235
4236         secs = record->ts / NSECS_PER_SEC;
4237         nsecs = record->ts - secs * NSECS_PER_SEC;
4238
4239         if (record->size < 0) {
4240                 do_warning("ug! negative record size %d", record->size);
4241                 return;
4242         }
4243
4244         type = trace_parse_common_type(pevent, data);
4245
4246         event = pevent_find_event(pevent, type);
4247         if (!event) {
4248                 do_warning("ug! no event found for type %d", type);
4249                 return;
4250         }
4251
4252         pid = parse_common_pid(pevent, data);
4253         comm = find_cmdline(pevent, pid);
4254
4255         if (pevent->latency_format) {
4256                 trace_seq_printf(s, "%8.8s-%-5d %3d",
4257                        comm, pid, record->cpu);
4258                 pevent_data_lat_fmt(pevent, s, record);
4259         } else
4260                 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4261
4262         if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4263                 usecs = nsecs;
4264                 p = 9;
4265         } else {
4266                 usecs = (nsecs + 500) / NSECS_PER_USEC;
4267                 p = 6;
4268         }
4269
4270         trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4271
4272         /* Space out the event names evenly. */
4273         len = strlen(event->name);
4274         if (len < 20)
4275                 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4276
4277         pevent_event_info(s, event, record);
4278 }
4279
4280 static int events_id_cmp(const void *a, const void *b)
4281 {
4282         struct event_format * const * ea = a;
4283         struct event_format * const * eb = b;
4284
4285         if ((*ea)->id < (*eb)->id)
4286                 return -1;
4287
4288         if ((*ea)->id > (*eb)->id)
4289                 return 1;
4290
4291         return 0;
4292 }
4293
4294 static int events_name_cmp(const void *a, const void *b)
4295 {
4296         struct event_format * const * ea = a;
4297         struct event_format * const * eb = b;
4298         int res;
4299
4300         res = strcmp((*ea)->name, (*eb)->name);
4301         if (res)
4302                 return res;
4303
4304         res = strcmp((*ea)->system, (*eb)->system);
4305         if (res)
4306                 return res;
4307
4308         return events_id_cmp(a, b);
4309 }
4310
4311 static int events_system_cmp(const void *a, const void *b)
4312 {
4313         struct event_format * const * ea = a;
4314         struct event_format * const * eb = b;
4315         int res;
4316
4317         res = strcmp((*ea)->system, (*eb)->system);
4318         if (res)
4319                 return res;
4320
4321         res = strcmp((*ea)->name, (*eb)->name);
4322         if (res)
4323                 return res;
4324
4325         return events_id_cmp(a, b);
4326 }
4327
4328 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4329 {
4330         struct event_format **events;
4331         int (*sort)(const void *a, const void *b);
4332
4333         events = pevent->sort_events;
4334
4335         if (events && pevent->last_type == sort_type)
4336                 return events;
4337
4338         if (!events) {
4339                 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4340                 if (!events)
4341                         return NULL;
4342
4343                 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4344                 events[pevent->nr_events] = NULL;
4345
4346                 pevent->sort_events = events;
4347
4348                 /* the internal events are sorted by id */
4349                 if (sort_type == EVENT_SORT_ID) {
4350                         pevent->last_type = sort_type;
4351                         return events;
4352                 }
4353         }
4354
4355         switch (sort_type) {
4356         case EVENT_SORT_ID:
4357                 sort = events_id_cmp;
4358                 break;
4359         case EVENT_SORT_NAME:
4360                 sort = events_name_cmp;
4361                 break;
4362         case EVENT_SORT_SYSTEM:
4363                 sort = events_system_cmp;
4364                 break;
4365         default:
4366                 return events;
4367         }
4368
4369         qsort(events, pevent->nr_events, sizeof(*events), sort);
4370         pevent->last_type = sort_type;
4371
4372         return events;
4373 }
4374
4375 static struct format_field **
4376 get_event_fields(const char *type, const char *name,
4377                  int count, struct format_field *list)
4378 {
4379         struct format_field **fields;
4380         struct format_field *field;
4381         int i = 0;
4382
4383         fields = malloc_or_die(sizeof(*fields) * (count + 1));
4384         for (field = list; field; field = field->next) {
4385                 fields[i++] = field;
4386                 if (i == count + 1) {
4387                         do_warning("event %s has more %s fields than specified",
4388                                 name, type);
4389                         i--;
4390                         break;
4391                 }
4392         }
4393
4394         if (i != count)
4395                 do_warning("event %s has less %s fields than specified",
4396                         name, type);
4397
4398         fields[i] = NULL;
4399
4400         return fields;
4401 }
4402
4403 /**
4404  * pevent_event_common_fields - return a list of common fields for an event
4405  * @event: the event to return the common fields of.
4406  *
4407  * Returns an allocated array of fields. The last item in the array is NULL.
4408  * The array must be freed with free().
4409  */
4410 struct format_field **pevent_event_common_fields(struct event_format *event)
4411 {
4412         return get_event_fields("common", event->name,
4413                                 event->format.nr_common,
4414                                 event->format.common_fields);
4415 }
4416
4417 /**
4418  * pevent_event_fields - return a list of event specific fields for an event
4419  * @event: the event to return the fields of.
4420  *
4421  * Returns an allocated array of fields. The last item in the array is NULL.
4422  * The array must be freed with free().
4423  */
4424 struct format_field **pevent_event_fields(struct event_format *event)
4425 {
4426         return get_event_fields("event", event->name,
4427                                 event->format.nr_fields,
4428                                 event->format.fields);
4429 }
4430
4431 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4432 {
4433         trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4434         if (field->next) {
4435                 trace_seq_puts(s, ", ");
4436                 print_fields(s, field->next);
4437         }
4438 }
4439
4440 /* for debugging */
4441 static void print_args(struct print_arg *args)
4442 {
4443         int print_paren = 1;
4444         struct trace_seq s;
4445
4446         switch (args->type) {
4447         case PRINT_NULL:
4448                 printf("null");
4449                 break;
4450         case PRINT_ATOM:
4451                 printf("%s", args->atom.atom);
4452                 break;
4453         case PRINT_FIELD:
4454                 printf("REC->%s", args->field.name);
4455                 break;
4456         case PRINT_FLAGS:
4457                 printf("__print_flags(");
4458                 print_args(args->flags.field);
4459                 printf(", %s, ", args->flags.delim);
4460                 trace_seq_init(&s);
4461                 print_fields(&s, args->flags.flags);
4462                 trace_seq_do_printf(&s);
4463                 trace_seq_destroy(&s);
4464                 printf(")");
4465                 break;
4466         case PRINT_SYMBOL:
4467                 printf("__print_symbolic(");
4468                 print_args(args->symbol.field);
4469                 printf(", ");
4470                 trace_seq_init(&s);
4471                 print_fields(&s, args->symbol.symbols);
4472                 trace_seq_do_printf(&s);
4473                 trace_seq_destroy(&s);
4474                 printf(")");
4475                 break;
4476         case PRINT_HEX:
4477                 printf("__print_hex(");
4478                 print_args(args->hex.field);
4479                 printf(", ");
4480                 print_args(args->hex.size);
4481                 printf(")");
4482                 break;
4483         case PRINT_STRING:
4484         case PRINT_BSTRING:
4485                 printf("__get_str(%s)", args->string.string);
4486                 break;
4487         case PRINT_TYPE:
4488                 printf("(%s)", args->typecast.type);
4489                 print_args(args->typecast.item);
4490                 break;
4491         case PRINT_OP:
4492                 if (strcmp(args->op.op, ":") == 0)
4493                         print_paren = 0;
4494                 if (print_paren)
4495                         printf("(");
4496                 print_args(args->op.left);
4497                 printf(" %s ", args->op.op);
4498                 print_args(args->op.right);
4499                 if (print_paren)
4500                         printf(")");
4501                 break;
4502         default:
4503                 /* we should warn... */
4504                 return;
4505         }
4506         if (args->next) {
4507                 printf("\n");
4508                 print_args(args->next);
4509         }
4510 }
4511
4512 static void parse_header_field(const char *field,
4513                                int *offset, int *size, int mandatory)
4514 {
4515         unsigned long long save_input_buf_ptr;
4516         unsigned long long save_input_buf_siz;
4517         char *token;
4518         int type;
4519
4520         save_input_buf_ptr = input_buf_ptr;
4521         save_input_buf_siz = input_buf_siz;
4522
4523         if (read_expected(EVENT_ITEM, "field") < 0)
4524                 return;
4525         if (read_expected(EVENT_OP, ":") < 0)
4526                 return;
4527
4528         /* type */
4529         if (read_expect_type(EVENT_ITEM, &token) < 0)
4530                 goto fail;
4531         free_token(token);
4532
4533         /*
4534          * If this is not a mandatory field, then test it first.
4535          */
4536         if (mandatory) {
4537                 if (read_expected(EVENT_ITEM, field) < 0)
4538                         return;
4539         } else {
4540                 if (read_expect_type(EVENT_ITEM, &token) < 0)
4541                         goto fail;
4542                 if (strcmp(token, field) != 0)
4543                         goto discard;
4544                 free_token(token);
4545         }
4546
4547         if (read_expected(EVENT_OP, ";") < 0)
4548                 return;
4549         if (read_expected(EVENT_ITEM, "offset") < 0)
4550                 return;
4551         if (read_expected(EVENT_OP, ":") < 0)
4552                 return;
4553         if (read_expect_type(EVENT_ITEM, &token) < 0)
4554                 goto fail;
4555         *offset = atoi(token);
4556         free_token(token);
4557         if (read_expected(EVENT_OP, ";") < 0)
4558                 return;
4559         if (read_expected(EVENT_ITEM, "size") < 0)
4560                 return;
4561         if (read_expected(EVENT_OP, ":") < 0)
4562                 return;
4563         if (read_expect_type(EVENT_ITEM, &token) < 0)
4564                 goto fail;
4565         *size = atoi(token);
4566         free_token(token);
4567         if (read_expected(EVENT_OP, ";") < 0)
4568                 return;
4569         type = read_token(&token);
4570         if (type != EVENT_NEWLINE) {
4571                 /* newer versions of the kernel have a "signed" type */
4572                 if (type != EVENT_ITEM)
4573                         goto fail;
4574
4575                 if (strcmp(token, "signed") != 0)
4576                         goto fail;
4577
4578                 free_token(token);
4579
4580                 if (read_expected(EVENT_OP, ":") < 0)
4581                         return;
4582
4583                 if (read_expect_type(EVENT_ITEM, &token))
4584                         goto fail;
4585
4586                 free_token(token);
4587                 if (read_expected(EVENT_OP, ";") < 0)
4588                         return;
4589
4590                 if (read_expect_type(EVENT_NEWLINE, &token))
4591                         goto fail;
4592         }
4593  fail:
4594         free_token(token);
4595         return;
4596
4597  discard:
4598         input_buf_ptr = save_input_buf_ptr;
4599         input_buf_siz = save_input_buf_siz;
4600         *offset = 0;
4601         *size = 0;
4602         free_token(token);
4603 }
4604
4605 /**
4606  * pevent_parse_header_page - parse the data stored in the header page
4607  * @pevent: the handle to the pevent
4608  * @buf: the buffer storing the header page format string
4609  * @size: the size of @buf
4610  * @long_size: the long size to use if there is no header
4611  *
4612  * This parses the header page format for information on the
4613  * ring buffer used. The @buf should be copied from
4614  *
4615  * /sys/kernel/debug/tracing/events/header_page
4616  */
4617 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4618                              int long_size)
4619 {
4620         int ignore;
4621
4622         if (!size) {
4623                 /*
4624                  * Old kernels did not have header page info.
4625                  * Sorry but we just use what we find here in user space.
4626                  */
4627                 pevent->header_page_ts_size = sizeof(long long);
4628                 pevent->header_page_size_size = long_size;
4629                 pevent->header_page_data_offset = sizeof(long long) + long_size;
4630                 pevent->old_format = 1;
4631                 return -1;
4632         }
4633         init_input_buf(buf, size);
4634
4635         parse_header_field("timestamp", &pevent->header_page_ts_offset,
4636                            &pevent->header_page_ts_size, 1);
4637         parse_header_field("commit", &pevent->header_page_size_offset,
4638                            &pevent->header_page_size_size, 1);
4639         parse_header_field("overwrite", &pevent->header_page_overwrite,
4640                            &ignore, 0);
4641         parse_header_field("data", &pevent->header_page_data_offset,
4642                            &pevent->header_page_data_size, 1);
4643
4644         return 0;
4645 }
4646
4647 static int event_matches(struct event_format *event,
4648                          int id, const char *sys_name,
4649                          const char *event_name)
4650 {
4651         if (id >= 0 && id != event->id)
4652                 return 0;
4653
4654         if (event_name && (strcmp(event_name, event->name) != 0))
4655                 return 0;
4656
4657         if (sys_name && (strcmp(sys_name, event->system) != 0))
4658                 return 0;
4659
4660         return 1;
4661 }
4662
4663 static void free_handler(struct event_handler *handle)
4664 {
4665         free((void *)handle->sys_name);
4666         free((void *)handle->event_name);
4667         free(handle);
4668 }
4669
4670 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4671 {
4672         struct event_handler *handle, **next;
4673
4674         for (next = &pevent->handlers; *next;
4675              next = &(*next)->next) {
4676                 handle = *next;
4677                 if (event_matches(event, handle->id,
4678                                   handle->sys_name,
4679                                   handle->event_name))
4680                         break;
4681         }
4682
4683         if (!(*next))
4684                 return 0;
4685
4686         pr_stat("overriding event (%d) %s:%s with new print handler",
4687                 event->id, event->system, event->name);
4688
4689         event->handler = handle->func;
4690         event->context = handle->context;
4691
4692         *next = handle->next;
4693         free_handler(handle);
4694
4695         return 1;
4696 }
4697
4698 /**
4699  * pevent_parse_event - parse the event format
4700  * @pevent: the handle to the pevent
4701  * @buf: the buffer storing the event format string
4702  * @size: the size of @buf
4703  * @sys: the system the event belongs to
4704  *
4705  * This parses the event format and creates an event structure
4706  * to quickly parse raw data for a given event.
4707  *
4708  * These files currently come from:
4709  *
4710  * /sys/kernel/debug/tracing/events/.../.../format
4711  */
4712 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4713                                      unsigned long size, const char *sys)
4714 {
4715         struct event_format *event;
4716         int ret;
4717
4718         init_input_buf(buf, size);
4719
4720         event = alloc_event();
4721         if (!event)
4722                 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4723
4724         event->name = event_read_name();
4725         if (!event->name) {
4726                 /* Bad event? */
4727                 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4728                 goto event_alloc_failed;
4729         }
4730
4731         if (strcmp(sys, "ftrace") == 0) {
4732                 event->flags |= EVENT_FL_ISFTRACE;
4733
4734                 if (strcmp(event->name, "bprint") == 0)
4735                         event->flags |= EVENT_FL_ISBPRINT;
4736         }
4737                 
4738         event->id = event_read_id();
4739         if (event->id < 0) {
4740                 ret = PEVENT_ERRNO__READ_ID_FAILED;
4741                 /*
4742                  * This isn't an allocation error actually.
4743                  * But as the ID is critical, just bail out.
4744                  */
4745                 goto event_alloc_failed;
4746         }
4747
4748         event->system = strdup(sys);
4749         if (!event->system) {
4750                 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4751                 goto event_alloc_failed;
4752         }
4753
4754         /* Add pevent to event so that it can be referenced */
4755         event->pevent = pevent;
4756
4757         ret = event_read_format(event);
4758         if (ret < 0) {
4759                 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4760                 goto event_parse_failed;
4761         }
4762
4763         /*
4764          * If the event has an override, don't print warnings if the event
4765          * print format fails to parse.
4766          */
4767         if (find_event_handle(pevent, event))
4768                 show_warning = 0;
4769
4770         ret = event_read_print(event);
4771         if (ret < 0) {
4772                 show_warning = 1;
4773                 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4774                 goto event_parse_failed;
4775         }
4776         show_warning = 1;
4777
4778         add_event(pevent, event);
4779
4780         if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4781                 struct format_field *field;
4782                 struct print_arg *arg, **list;
4783
4784                 /* old ftrace had no args */
4785                 list = &event->print_fmt.args;
4786                 for (field = event->format.fields; field; field = field->next) {
4787                         arg = alloc_arg();
4788                         arg->type = PRINT_FIELD;
4789                         arg->field.name = strdup(field->name);
4790                         if (!arg->field.name) {
4791                                 event->flags |= EVENT_FL_FAILED;
4792                                 free_arg(arg);
4793                                 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4794                         }
4795                         arg->field.field = field;
4796                         *list = arg;
4797                         list = &arg->next;
4798                 }
4799                 return 0;
4800         }
4801
4802 #define PRINT_ARGS 0
4803         if (PRINT_ARGS && event->print_fmt.args)
4804                 print_args(event->print_fmt.args);
4805
4806         return 0;
4807
4808  event_parse_failed:
4809         event->flags |= EVENT_FL_FAILED;
4810         /* still add it even if it failed */
4811         add_event(pevent, event);
4812         return ret;
4813
4814  event_alloc_failed:
4815         free(event->system);
4816         free(event->name);
4817         free(event);
4818         return ret;
4819 }
4820
4821 #undef _PE
4822 #define _PE(code, str) str
4823 static const char * const pevent_error_str[] = {
4824         PEVENT_ERRORS
4825 };
4826 #undef _PE
4827
4828 int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4829                     char *buf, size_t buflen)
4830 {
4831         int idx;
4832         const char *msg;
4833
4834         if (errnum >= 0) {
4835                 msg = strerror_r(errnum, buf, buflen);
4836                 if (msg != buf) {
4837                         size_t len = strlen(msg);
4838                         memcpy(buf, msg, min(buflen - 1, len));
4839                         *(buf + min(buflen - 1, len)) = '\0';
4840                 }
4841                 return 0;
4842         }
4843
4844         if (errnum <= __PEVENT_ERRNO__START ||
4845             errnum >= __PEVENT_ERRNO__END)
4846                 return -1;
4847
4848         idx = errnum - __PEVENT_ERRNO__START - 1;
4849         msg = pevent_error_str[idx];
4850
4851         switch (errnum) {
4852         case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4853         case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4854         case PEVENT_ERRNO__READ_ID_FAILED:
4855         case PEVENT_ERRNO__READ_FORMAT_FAILED:
4856         case PEVENT_ERRNO__READ_PRINT_FAILED:
4857         case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4858                 snprintf(buf, buflen, "%s", msg);
4859                 break;
4860
4861         default:
4862                 /* cannot reach here */
4863                 break;
4864         }
4865
4866         return 0;
4867 }
4868
4869 int get_field_val(struct trace_seq *s, struct format_field *field,
4870                   const char *name, struct pevent_record *record,
4871                   unsigned long long *val, int err)
4872 {
4873         if (!field) {
4874                 if (err)
4875                         trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4876                 return -1;
4877         }
4878
4879         if (pevent_read_number_field(field, record->data, val)) {
4880                 if (err)
4881                         trace_seq_printf(s, " %s=INVALID", name);
4882                 return -1;
4883         }
4884
4885         return 0;
4886 }
4887
4888 /**
4889  * pevent_get_field_raw - return the raw pointer into the data field
4890  * @s: The seq to print to on error
4891  * @event: the event that the field is for
4892  * @name: The name of the field
4893  * @record: The record with the field name.
4894  * @len: place to store the field length.
4895  * @err: print default error if failed.
4896  *
4897  * Returns a pointer into record->data of the field and places
4898  * the length of the field in @len.
4899  *
4900  * On failure, it returns NULL.
4901  */
4902 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4903                            const char *name, struct pevent_record *record,
4904                            int *len, int err)
4905 {
4906         struct format_field *field;
4907         void *data = record->data;
4908         unsigned offset;
4909         int dummy;
4910
4911         if (!event)
4912                 return NULL;
4913
4914         field = pevent_find_field(event, name);
4915
4916         if (!field) {
4917                 if (err)
4918                         trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4919                 return NULL;
4920         }
4921
4922         /* Allow @len to be NULL */
4923         if (!len)
4924                 len = &dummy;
4925
4926         offset = field->offset;
4927         if (field->flags & FIELD_IS_DYNAMIC) {
4928                 offset = pevent_read_number(event->pevent,
4929                                             data + offset, field->size);
4930                 *len = offset >> 16;
4931                 offset &= 0xffff;
4932         } else
4933                 *len = field->size;
4934
4935         return data + offset;
4936 }
4937
4938 /**
4939  * pevent_get_field_val - find a field and return its value
4940  * @s: The seq to print to on error
4941  * @event: the event that the field is for
4942  * @name: The name of the field
4943  * @record: The record with the field name.
4944  * @val: place to store the value of the field.
4945  * @err: print default error if failed.
4946  *
4947  * Returns 0 on success -1 on field not found.
4948  */
4949 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4950                          const char *name, struct pevent_record *record,
4951                          unsigned long long *val, int err)
4952 {
4953         struct format_field *field;
4954
4955         if (!event)
4956                 return -1;
4957
4958         field = pevent_find_field(event, name);
4959
4960         return get_field_val(s, field, name, record, val, err);
4961 }
4962
4963 /**
4964  * pevent_get_common_field_val - find a common field and return its value
4965  * @s: The seq to print to on error
4966  * @event: the event that the field is for
4967  * @name: The name of the field
4968  * @record: The record with the field name.
4969  * @val: place to store the value of the field.
4970  * @err: print default error if failed.
4971  *
4972  * Returns 0 on success -1 on field not found.
4973  */
4974 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4975                                 const char *name, struct pevent_record *record,
4976                                 unsigned long long *val, int err)
4977 {
4978         struct format_field *field;
4979
4980         if (!event)
4981                 return -1;
4982
4983         field = pevent_find_common_field(event, name);
4984
4985         return get_field_val(s, field, name, record, val, err);
4986 }
4987
4988 /**
4989  * pevent_get_any_field_val - find a any field and return its value
4990  * @s: The seq to print to on error
4991  * @event: the event that the field is for
4992  * @name: The name of the field
4993  * @record: The record with the field name.
4994  * @val: place to store the value of the field.
4995  * @err: print default error if failed.
4996  *
4997  * Returns 0 on success -1 on field not found.
4998  */
4999 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5000                              const char *name, struct pevent_record *record,
5001                              unsigned long long *val, int err)
5002 {
5003         struct format_field *field;
5004
5005         if (!event)
5006                 return -1;
5007
5008         field = pevent_find_any_field(event, name);
5009
5010         return get_field_val(s, field, name, record, val, err);
5011 }
5012
5013 /**
5014  * pevent_print_num_field - print a field and a format
5015  * @s: The seq to print to
5016  * @fmt: The printf format to print the field with.
5017  * @event: the event that the field is for
5018  * @name: The name of the field
5019  * @record: The record with the field name.
5020  * @err: print default error if failed.
5021  *
5022  * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5023  */
5024 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5025                            struct event_format *event, const char *name,
5026                            struct pevent_record *record, int err)
5027 {
5028         struct format_field *field = pevent_find_field(event, name);
5029         unsigned long long val;
5030
5031         if (!field)
5032                 goto failed;
5033
5034         if (pevent_read_number_field(field, record->data, &val))
5035                 goto failed;
5036
5037         return trace_seq_printf(s, fmt, val);
5038
5039  failed:
5040         if (err)
5041                 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5042         return -1;
5043 }
5044
5045 static void free_func_handle(struct pevent_function_handler *func)
5046 {
5047         struct pevent_func_params *params;
5048
5049         free(func->name);
5050
5051         while (func->params) {
5052                 params = func->params;
5053                 func->params = params->next;
5054                 free(params);
5055         }
5056
5057         free(func);
5058 }
5059
5060 /**
5061  * pevent_register_print_function - register a helper function
5062  * @pevent: the handle to the pevent
5063  * @func: the function to process the helper function
5064  * @ret_type: the return type of the helper function
5065  * @name: the name of the helper function
5066  * @parameters: A list of enum pevent_func_arg_type
5067  *
5068  * Some events may have helper functions in the print format arguments.
5069  * This allows a plugin to dynamically create a way to process one
5070  * of these functions.
5071  *
5072  * The @parameters is a variable list of pevent_func_arg_type enums that
5073  * must end with PEVENT_FUNC_ARG_VOID.
5074  */
5075 int pevent_register_print_function(struct pevent *pevent,
5076                                    pevent_func_handler func,
5077                                    enum pevent_func_arg_type ret_type,
5078                                    char *name, ...)
5079 {
5080         struct pevent_function_handler *func_handle;
5081         struct pevent_func_params **next_param;
5082         struct pevent_func_params *param;
5083         enum pevent_func_arg_type type;
5084         va_list ap;
5085         int ret;
5086
5087         func_handle = find_func_handler(pevent, name);
5088         if (func_handle) {
5089                 /*
5090                  * This is most like caused by the users own
5091                  * plugins updating the function. This overrides the
5092                  * system defaults.
5093                  */
5094                 pr_stat("override of function helper '%s'", name);
5095                 remove_func_handler(pevent, name);
5096         }
5097
5098         func_handle = malloc(sizeof(*func_handle));
5099         if (!func_handle) {
5100                 do_warning("Failed to allocate function handler");
5101                 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5102         }
5103         memset(func_handle, 0, sizeof(*func_handle));
5104
5105         func_handle->ret_type = ret_type;
5106         func_handle->name = strdup(name);
5107         func_handle->func = func;
5108         if (!func_handle->name) {
5109                 do_warning("Failed to allocate function name");
5110                 free(func_handle);
5111                 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5112         }
5113
5114         next_param = &(func_handle->params);
5115         va_start(ap, name);
5116         for (;;) {
5117                 type = va_arg(ap, enum pevent_func_arg_type);
5118                 if (type == PEVENT_FUNC_ARG_VOID)
5119                         break;
5120
5121                 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5122                         do_warning("Invalid argument type %d", type);
5123                         ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5124                         goto out_free;
5125                 }
5126
5127                 param = malloc(sizeof(*param));
5128                 if (!param) {
5129                         do_warning("Failed to allocate function param");
5130                         ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5131                         goto out_free;
5132                 }
5133                 param->type = type;
5134                 param->next = NULL;
5135
5136                 *next_param = param;
5137                 next_param = &(param->next);
5138
5139                 func_handle->nr_args++;
5140         }
5141         va_end(ap);
5142
5143         func_handle->next = pevent->func_handlers;
5144         pevent->func_handlers = func_handle;
5145
5146         return 0;
5147  out_free:
5148         va_end(ap);
5149         free_func_handle(func_handle);
5150         return ret;
5151 }
5152
5153 /**
5154  * pevent_register_event_handler - register a way to parse an event
5155  * @pevent: the handle to the pevent
5156  * @id: the id of the event to register
5157  * @sys_name: the system name the event belongs to
5158  * @event_name: the name of the event
5159  * @func: the function to call to parse the event information
5160  * @context: the data to be passed to @func
5161  *
5162  * This function allows a developer to override the parsing of
5163  * a given event. If for some reason the default print format
5164  * is not sufficient, this function will register a function
5165  * for an event to be used to parse the data instead.
5166  *
5167  * If @id is >= 0, then it is used to find the event.
5168  * else @sys_name and @event_name are used.
5169  */
5170 int pevent_register_event_handler(struct pevent *pevent,
5171                                   int id, char *sys_name, char *event_name,
5172                                   pevent_event_handler_func func,
5173                                   void *context)
5174 {
5175         struct event_format *event;
5176         struct event_handler *handle;
5177
5178         if (id >= 0) {
5179                 /* search by id */
5180                 event = pevent_find_event(pevent, id);
5181                 if (!event)
5182                         goto not_found;
5183                 if (event_name && (strcmp(event_name, event->name) != 0))
5184                         goto not_found;
5185                 if (sys_name && (strcmp(sys_name, event->system) != 0))
5186                         goto not_found;
5187         } else {
5188                 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5189                 if (!event)
5190                         goto not_found;
5191         }
5192
5193         pr_stat("overriding event (%d) %s:%s with new print handler",
5194                 event->id, event->system, event->name);
5195
5196         event->handler = func;
5197         event->context = context;
5198         return 0;
5199
5200  not_found:
5201         /* Save for later use. */
5202         handle = malloc(sizeof(*handle));
5203         if (!handle) {
5204                 do_warning("Failed to allocate event handler");
5205                 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5206         }
5207
5208         memset(handle, 0, sizeof(*handle));
5209         handle->id = id;
5210         if (event_name)
5211                 handle->event_name = strdup(event_name);
5212         if (sys_name)
5213                 handle->sys_name = strdup(sys_name);
5214
5215         if ((event_name && !handle->event_name) ||
5216             (sys_name && !handle->sys_name)) {
5217                 do_warning("Failed to allocate event/sys name");
5218                 free((void *)handle->event_name);
5219                 free((void *)handle->sys_name);
5220                 free(handle);
5221                 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5222         }
5223
5224         handle->func = func;
5225         handle->next = pevent->handlers;
5226         pevent->handlers = handle;
5227         handle->context = context;
5228
5229         return -1;
5230 }
5231
5232 /**
5233  * pevent_alloc - create a pevent handle
5234  */
5235 struct pevent *pevent_alloc(void)
5236 {
5237         struct pevent *pevent;
5238
5239         pevent = malloc(sizeof(*pevent));
5240         if (!pevent)
5241                 return NULL;
5242         memset(pevent, 0, sizeof(*pevent));
5243         pevent->ref_count = 1;
5244
5245         return pevent;
5246 }
5247
5248 void pevent_ref(struct pevent *pevent)
5249 {
5250         pevent->ref_count++;
5251 }
5252
5253 static void free_format_fields(struct format_field *field)
5254 {
5255         struct format_field *next;
5256
5257         while (field) {
5258                 next = field->next;
5259                 free(field->type);
5260                 free(field->name);
5261                 free(field);
5262                 field = next;
5263         }
5264 }
5265
5266 static void free_formats(struct format *format)
5267 {
5268         free_format_fields(format->common_fields);
5269         free_format_fields(format->fields);
5270 }
5271
5272 static void free_event(struct event_format *event)
5273 {
5274         free(event->name);
5275         free(event->system);
5276
5277         free_formats(&event->format);
5278
5279         free(event->print_fmt.format);
5280         free_args(event->print_fmt.args);
5281
5282         free(event);
5283 }
5284
5285 /**
5286  * pevent_free - free a pevent handle
5287  * @pevent: the pevent handle to free
5288  */
5289 void pevent_free(struct pevent *pevent)
5290 {
5291         struct cmdline_list *cmdlist, *cmdnext;
5292         struct func_list *funclist, *funcnext;
5293         struct printk_list *printklist, *printknext;
5294         struct pevent_function_handler *func_handler;
5295         struct event_handler *handle;
5296         int i;
5297
5298         if (!pevent)
5299                 return;
5300
5301         cmdlist = pevent->cmdlist;
5302         funclist = pevent->funclist;
5303         printklist = pevent->printklist;
5304
5305         pevent->ref_count--;
5306         if (pevent->ref_count)
5307                 return;
5308
5309         if (pevent->cmdlines) {
5310                 for (i = 0; i < pevent->cmdline_count; i++)
5311                         free(pevent->cmdlines[i].comm);
5312                 free(pevent->cmdlines);
5313         }
5314
5315         while (cmdlist) {
5316                 cmdnext = cmdlist->next;
5317                 free(cmdlist->comm);
5318                 free(cmdlist);
5319                 cmdlist = cmdnext;
5320         }
5321
5322         if (pevent->func_map) {
5323                 for (i = 0; i < pevent->func_count; i++) {
5324                         free(pevent->func_map[i].func);
5325                         free(pevent->func_map[i].mod);
5326                 }
5327                 free(pevent->func_map);
5328         }
5329
5330         while (funclist) {
5331                 funcnext = funclist->next;
5332                 free(funclist->func);
5333                 free(funclist->mod);
5334                 free(funclist);
5335                 funclist = funcnext;
5336         }
5337
5338         while (pevent->func_handlers) {
5339                 func_handler = pevent->func_handlers;
5340                 pevent->func_handlers = func_handler->next;
5341                 free_func_handle(func_handler);
5342         }
5343
5344         if (pevent->printk_map) {
5345                 for (i = 0; i < pevent->printk_count; i++)
5346                         free(pevent->printk_map[i].printk);
5347                 free(pevent->printk_map);
5348         }
5349
5350         while (printklist) {
5351                 printknext = printklist->next;
5352                 free(printklist->printk);
5353                 free(printklist);
5354                 printklist = printknext;
5355         }
5356
5357         for (i = 0; i < pevent->nr_events; i++)
5358                 free_event(pevent->events[i]);
5359
5360         while (pevent->handlers) {
5361                 handle = pevent->handlers;
5362                 pevent->handlers = handle->next;
5363                 free_handler(handle);
5364         }
5365
5366         free(pevent->events);
5367         free(pevent->sort_events);
5368
5369         free(pevent);
5370 }
5371
5372 void pevent_unref(struct pevent *pevent)
5373 {
5374         pevent_free(pevent);
5375 }