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

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

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

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

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp