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