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