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

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

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