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