~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/traceevent/event-parse.c

Version: ~ [ linux-5.3 ] ~ [ linux-5.2.15 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.73 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.144 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.193 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.193 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.73 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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