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