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