sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / tools / lib / traceevent / event-parse.c
blob14a4f623c1a57353e49869974125eee71c0e9671
1 /*
2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdint.h>
34 #include <limits.h>
35 #include <linux/string.h>
36 #include <linux/time64.h>
38 #include <netinet/in.h>
39 #include "event-parse.h"
40 #include "event-utils.h"
42 static const char *input_buf;
43 static unsigned long long input_buf_ptr;
44 static unsigned long long input_buf_siz;
46 static int is_flag_field;
47 static int is_symbolic_field;
49 static int show_warning = 1;
51 #define do_warning(fmt, ...) \
52 do { \
53 if (show_warning) \
54 warning(fmt, ##__VA_ARGS__); \
55 } while (0)
57 #define do_warning_event(event, fmt, ...) \
58 do { \
59 if (!show_warning) \
60 continue; \
62 if (event) \
63 warning("[%s:%s] " fmt, event->system, \
64 event->name, ##__VA_ARGS__); \
65 else \
66 warning(fmt, ##__VA_ARGS__); \
67 } while (0)
69 static void init_input_buf(const char *buf, unsigned long long size)
71 input_buf = buf;
72 input_buf_siz = size;
73 input_buf_ptr = 0;
76 const char *pevent_get_input_buf(void)
78 return input_buf;
81 unsigned long long pevent_get_input_buf_ptr(void)
83 return input_buf_ptr;
86 struct event_handler {
87 struct event_handler *next;
88 int id;
89 const char *sys_name;
90 const char *event_name;
91 pevent_event_handler_func func;
92 void *context;
95 struct pevent_func_params {
96 struct pevent_func_params *next;
97 enum pevent_func_arg_type type;
100 struct pevent_function_handler {
101 struct pevent_function_handler *next;
102 enum pevent_func_arg_type ret_type;
103 char *name;
104 pevent_func_handler func;
105 struct pevent_func_params *params;
106 int nr_args;
109 static unsigned long long
110 process_defined_func(struct trace_seq *s, void *data, int size,
111 struct event_format *event, struct print_arg *arg);
113 static void free_func_handle(struct pevent_function_handler *func);
116 * pevent_buffer_init - init buffer for parsing
117 * @buf: buffer to parse
118 * @size: the size of the buffer
120 * For use with pevent_read_token(), this initializes the internal
121 * buffer that pevent_read_token() will parse.
123 void pevent_buffer_init(const char *buf, unsigned long long size)
125 init_input_buf(buf, size);
128 void breakpoint(void)
130 static int x;
131 x++;
134 struct print_arg *alloc_arg(void)
136 return calloc(1, sizeof(struct print_arg));
139 struct cmdline {
140 char *comm;
141 int pid;
144 static int cmdline_cmp(const void *a, const void *b)
146 const struct cmdline *ca = a;
147 const struct cmdline *cb = b;
149 if (ca->pid < cb->pid)
150 return -1;
151 if (ca->pid > cb->pid)
152 return 1;
154 return 0;
157 struct cmdline_list {
158 struct cmdline_list *next;
159 char *comm;
160 int pid;
163 static int cmdline_init(struct pevent *pevent)
165 struct cmdline_list *cmdlist = pevent->cmdlist;
166 struct cmdline_list *item;
167 struct cmdline *cmdlines;
168 int i;
170 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
171 if (!cmdlines)
172 return -1;
174 i = 0;
175 while (cmdlist) {
176 cmdlines[i].pid = cmdlist->pid;
177 cmdlines[i].comm = cmdlist->comm;
178 i++;
179 item = cmdlist;
180 cmdlist = cmdlist->next;
181 free(item);
184 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
186 pevent->cmdlines = cmdlines;
187 pevent->cmdlist = NULL;
189 return 0;
192 static const char *find_cmdline(struct pevent *pevent, int pid)
194 const struct cmdline *comm;
195 struct cmdline key;
197 if (!pid)
198 return "<idle>";
200 if (!pevent->cmdlines && cmdline_init(pevent))
201 return "<not enough memory for cmdlines!>";
203 key.pid = pid;
205 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
206 sizeof(*pevent->cmdlines), cmdline_cmp);
208 if (comm)
209 return comm->comm;
210 return "<...>";
214 * pevent_pid_is_registered - return if a pid has a cmdline registered
215 * @pevent: handle for the pevent
216 * @pid: The pid to check if it has a cmdline registered with.
218 * Returns 1 if the pid has a cmdline mapped to it
219 * 0 otherwise.
221 int pevent_pid_is_registered(struct pevent *pevent, int pid)
223 const struct cmdline *comm;
224 struct cmdline key;
226 if (!pid)
227 return 1;
229 if (!pevent->cmdlines && cmdline_init(pevent))
230 return 0;
232 key.pid = pid;
234 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
235 sizeof(*pevent->cmdlines), cmdline_cmp);
237 if (comm)
238 return 1;
239 return 0;
243 * If the command lines have been converted to an array, then
244 * we must add this pid. This is much slower than when cmdlines
245 * are added before the array is initialized.
247 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
249 struct cmdline *cmdlines = pevent->cmdlines;
250 const struct cmdline *cmdline;
251 struct cmdline key;
253 if (!pid)
254 return 0;
256 /* avoid duplicates */
257 key.pid = pid;
259 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
260 sizeof(*pevent->cmdlines), cmdline_cmp);
261 if (cmdline) {
262 errno = EEXIST;
263 return -1;
266 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
267 if (!cmdlines) {
268 errno = ENOMEM;
269 return -1;
272 cmdlines[pevent->cmdline_count].comm = strdup(comm);
273 if (!cmdlines[pevent->cmdline_count].comm) {
274 free(cmdlines);
275 errno = ENOMEM;
276 return -1;
279 cmdlines[pevent->cmdline_count].pid = pid;
281 if (cmdlines[pevent->cmdline_count].comm)
282 pevent->cmdline_count++;
284 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
285 pevent->cmdlines = cmdlines;
287 return 0;
291 * pevent_register_comm - register a pid / comm mapping
292 * @pevent: handle for the pevent
293 * @comm: the command line to register
294 * @pid: the pid to map the command line to
296 * This adds a mapping to search for command line names with
297 * a given pid. The comm is duplicated.
299 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
301 struct cmdline_list *item;
303 if (pevent->cmdlines)
304 return add_new_comm(pevent, comm, pid);
306 item = malloc(sizeof(*item));
307 if (!item)
308 return -1;
310 if (comm)
311 item->comm = strdup(comm);
312 else
313 item->comm = strdup("<...>");
314 if (!item->comm) {
315 free(item);
316 return -1;
318 item->pid = pid;
319 item->next = pevent->cmdlist;
321 pevent->cmdlist = item;
322 pevent->cmdline_count++;
324 return 0;
327 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
329 pevent->trace_clock = strdup(trace_clock);
330 if (!pevent->trace_clock) {
331 errno = ENOMEM;
332 return -1;
334 return 0;
337 struct func_map {
338 unsigned long long addr;
339 char *func;
340 char *mod;
343 struct func_list {
344 struct func_list *next;
345 unsigned long long addr;
346 char *func;
347 char *mod;
350 static int func_cmp(const void *a, const void *b)
352 const struct func_map *fa = a;
353 const struct func_map *fb = b;
355 if (fa->addr < fb->addr)
356 return -1;
357 if (fa->addr > fb->addr)
358 return 1;
360 return 0;
364 * We are searching for a record in between, not an exact
365 * match.
367 static int func_bcmp(const void *a, const void *b)
369 const struct func_map *fa = a;
370 const struct func_map *fb = b;
372 if ((fa->addr == fb->addr) ||
374 (fa->addr > fb->addr &&
375 fa->addr < (fb+1)->addr))
376 return 0;
378 if (fa->addr < fb->addr)
379 return -1;
381 return 1;
384 static int func_map_init(struct pevent *pevent)
386 struct func_list *funclist;
387 struct func_list *item;
388 struct func_map *func_map;
389 int i;
391 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
392 if (!func_map)
393 return -1;
395 funclist = pevent->funclist;
397 i = 0;
398 while (funclist) {
399 func_map[i].func = funclist->func;
400 func_map[i].addr = funclist->addr;
401 func_map[i].mod = funclist->mod;
402 i++;
403 item = funclist;
404 funclist = funclist->next;
405 free(item);
408 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
411 * Add a special record at the end.
413 func_map[pevent->func_count].func = NULL;
414 func_map[pevent->func_count].addr = 0;
415 func_map[pevent->func_count].mod = NULL;
417 pevent->func_map = func_map;
418 pevent->funclist = NULL;
420 return 0;
423 static struct func_map *
424 __find_func(struct pevent *pevent, unsigned long long addr)
426 struct func_map *func;
427 struct func_map key;
429 if (!pevent->func_map)
430 func_map_init(pevent);
432 key.addr = addr;
434 func = bsearch(&key, pevent->func_map, pevent->func_count,
435 sizeof(*pevent->func_map), func_bcmp);
437 return func;
440 struct func_resolver {
441 pevent_func_resolver_t *func;
442 void *priv;
443 struct func_map map;
447 * pevent_set_function_resolver - set an alternative function resolver
448 * @pevent: handle for the pevent
449 * @resolver: function to be used
450 * @priv: resolver function private state.
452 * Some tools may have already a way to resolve kernel functions, allow them to
453 * keep using it instead of duplicating all the entries inside
454 * pevent->funclist.
456 int pevent_set_function_resolver(struct pevent *pevent,
457 pevent_func_resolver_t *func, void *priv)
459 struct func_resolver *resolver = malloc(sizeof(*resolver));
461 if (resolver == NULL)
462 return -1;
464 resolver->func = func;
465 resolver->priv = priv;
467 free(pevent->func_resolver);
468 pevent->func_resolver = resolver;
470 return 0;
474 * pevent_reset_function_resolver - reset alternative function resolver
475 * @pevent: handle for the pevent
477 * Stop using whatever alternative resolver was set, use the default
478 * one instead.
480 void pevent_reset_function_resolver(struct pevent *pevent)
482 free(pevent->func_resolver);
483 pevent->func_resolver = NULL;
486 static struct func_map *
487 find_func(struct pevent *pevent, unsigned long long addr)
489 struct func_map *map;
491 if (!pevent->func_resolver)
492 return __find_func(pevent, addr);
494 map = &pevent->func_resolver->map;
495 map->mod = NULL;
496 map->addr = addr;
497 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
498 &map->addr, &map->mod);
499 if (map->func == NULL)
500 return NULL;
502 return map;
506 * pevent_find_function - find a function by a given address
507 * @pevent: handle for the pevent
508 * @addr: the address to find the function with
510 * Returns a pointer to the function stored that has the given
511 * address. Note, the address does not have to be exact, it
512 * will select the function that would contain the address.
514 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
516 struct func_map *map;
518 map = find_func(pevent, addr);
519 if (!map)
520 return NULL;
522 return map->func;
526 * pevent_find_function_address - find a function address by a given address
527 * @pevent: handle for the pevent
528 * @addr: the address to find the function with
530 * Returns the address the function starts at. This can be used in
531 * conjunction with pevent_find_function to print both the function
532 * name and the function offset.
534 unsigned long long
535 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
537 struct func_map *map;
539 map = find_func(pevent, addr);
540 if (!map)
541 return 0;
543 return map->addr;
547 * pevent_register_function - register a function with a given address
548 * @pevent: handle for the pevent
549 * @function: the function name to register
550 * @addr: the address the function starts at
551 * @mod: the kernel module the function may be in (NULL for none)
553 * This registers a function name with an address and module.
554 * The @func passed in is duplicated.
556 int pevent_register_function(struct pevent *pevent, char *func,
557 unsigned long long addr, char *mod)
559 struct func_list *item = malloc(sizeof(*item));
561 if (!item)
562 return -1;
564 item->next = pevent->funclist;
565 item->func = strdup(func);
566 if (!item->func)
567 goto out_free;
569 if (mod) {
570 item->mod = strdup(mod);
571 if (!item->mod)
572 goto out_free_func;
573 } else
574 item->mod = NULL;
575 item->addr = addr;
577 pevent->funclist = item;
578 pevent->func_count++;
580 return 0;
582 out_free_func:
583 free(item->func);
584 item->func = NULL;
585 out_free:
586 free(item);
587 errno = ENOMEM;
588 return -1;
592 * pevent_print_funcs - print out the stored functions
593 * @pevent: handle for the pevent
595 * This prints out the stored functions.
597 void pevent_print_funcs(struct pevent *pevent)
599 int i;
601 if (!pevent->func_map)
602 func_map_init(pevent);
604 for (i = 0; i < (int)pevent->func_count; i++) {
605 printf("%016llx %s",
606 pevent->func_map[i].addr,
607 pevent->func_map[i].func);
608 if (pevent->func_map[i].mod)
609 printf(" [%s]\n", pevent->func_map[i].mod);
610 else
611 printf("\n");
615 struct printk_map {
616 unsigned long long addr;
617 char *printk;
620 struct printk_list {
621 struct printk_list *next;
622 unsigned long long addr;
623 char *printk;
626 static int printk_cmp(const void *a, const void *b)
628 const struct printk_map *pa = a;
629 const struct printk_map *pb = b;
631 if (pa->addr < pb->addr)
632 return -1;
633 if (pa->addr > pb->addr)
634 return 1;
636 return 0;
639 static int printk_map_init(struct pevent *pevent)
641 struct printk_list *printklist;
642 struct printk_list *item;
643 struct printk_map *printk_map;
644 int i;
646 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
647 if (!printk_map)
648 return -1;
650 printklist = pevent->printklist;
652 i = 0;
653 while (printklist) {
654 printk_map[i].printk = printklist->printk;
655 printk_map[i].addr = printklist->addr;
656 i++;
657 item = printklist;
658 printklist = printklist->next;
659 free(item);
662 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
664 pevent->printk_map = printk_map;
665 pevent->printklist = NULL;
667 return 0;
670 static struct printk_map *
671 find_printk(struct pevent *pevent, unsigned long long addr)
673 struct printk_map *printk;
674 struct printk_map key;
676 if (!pevent->printk_map && printk_map_init(pevent))
677 return NULL;
679 key.addr = addr;
681 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
682 sizeof(*pevent->printk_map), printk_cmp);
684 return printk;
688 * pevent_register_print_string - register a string by its address
689 * @pevent: handle for the pevent
690 * @fmt: the string format to register
691 * @addr: the address the string was located at
693 * This registers a string by the address it was stored in the kernel.
694 * The @fmt passed in is duplicated.
696 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
697 unsigned long long addr)
699 struct printk_list *item = malloc(sizeof(*item));
700 char *p;
702 if (!item)
703 return -1;
705 item->next = pevent->printklist;
706 item->addr = addr;
708 /* Strip off quotes and '\n' from the end */
709 if (fmt[0] == '"')
710 fmt++;
711 item->printk = strdup(fmt);
712 if (!item->printk)
713 goto out_free;
715 p = item->printk + strlen(item->printk) - 1;
716 if (*p == '"')
717 *p = 0;
719 p -= 2;
720 if (strcmp(p, "\\n") == 0)
721 *p = 0;
723 pevent->printklist = item;
724 pevent->printk_count++;
726 return 0;
728 out_free:
729 free(item);
730 errno = ENOMEM;
731 return -1;
735 * pevent_print_printk - print out the stored strings
736 * @pevent: handle for the pevent
738 * This prints the string formats that were stored.
740 void pevent_print_printk(struct pevent *pevent)
742 int i;
744 if (!pevent->printk_map)
745 printk_map_init(pevent);
747 for (i = 0; i < (int)pevent->printk_count; i++) {
748 printf("%016llx %s\n",
749 pevent->printk_map[i].addr,
750 pevent->printk_map[i].printk);
754 static struct event_format *alloc_event(void)
756 return calloc(1, sizeof(struct event_format));
759 static int add_event(struct pevent *pevent, struct event_format *event)
761 int i;
762 struct event_format **events = realloc(pevent->events, sizeof(event) *
763 (pevent->nr_events + 1));
764 if (!events)
765 return -1;
767 pevent->events = events;
769 for (i = 0; i < pevent->nr_events; i++) {
770 if (pevent->events[i]->id > event->id)
771 break;
773 if (i < pevent->nr_events)
774 memmove(&pevent->events[i + 1],
775 &pevent->events[i],
776 sizeof(event) * (pevent->nr_events - i));
778 pevent->events[i] = event;
779 pevent->nr_events++;
781 event->pevent = pevent;
783 return 0;
786 static int event_item_type(enum event_type type)
788 switch (type) {
789 case EVENT_ITEM ... EVENT_SQUOTE:
790 return 1;
791 case EVENT_ERROR ... EVENT_DELIM:
792 default:
793 return 0;
797 static void free_flag_sym(struct print_flag_sym *fsym)
799 struct print_flag_sym *next;
801 while (fsym) {
802 next = fsym->next;
803 free(fsym->value);
804 free(fsym->str);
805 free(fsym);
806 fsym = next;
810 static void free_arg(struct print_arg *arg)
812 struct print_arg *farg;
814 if (!arg)
815 return;
817 switch (arg->type) {
818 case PRINT_ATOM:
819 free(arg->atom.atom);
820 break;
821 case PRINT_FIELD:
822 free(arg->field.name);
823 break;
824 case PRINT_FLAGS:
825 free_arg(arg->flags.field);
826 free(arg->flags.delim);
827 free_flag_sym(arg->flags.flags);
828 break;
829 case PRINT_SYMBOL:
830 free_arg(arg->symbol.field);
831 free_flag_sym(arg->symbol.symbols);
832 break;
833 case PRINT_HEX:
834 free_arg(arg->hex.field);
835 free_arg(arg->hex.size);
836 break;
837 case PRINT_INT_ARRAY:
838 free_arg(arg->int_array.field);
839 free_arg(arg->int_array.count);
840 free_arg(arg->int_array.el_size);
841 break;
842 case PRINT_TYPE:
843 free(arg->typecast.type);
844 free_arg(arg->typecast.item);
845 break;
846 case PRINT_STRING:
847 case PRINT_BSTRING:
848 free(arg->string.string);
849 break;
850 case PRINT_BITMASK:
851 free(arg->bitmask.bitmask);
852 break;
853 case PRINT_DYNAMIC_ARRAY:
854 case PRINT_DYNAMIC_ARRAY_LEN:
855 free(arg->dynarray.index);
856 break;
857 case PRINT_OP:
858 free(arg->op.op);
859 free_arg(arg->op.left);
860 free_arg(arg->op.right);
861 break;
862 case PRINT_FUNC:
863 while (arg->func.args) {
864 farg = arg->func.args;
865 arg->func.args = farg->next;
866 free_arg(farg);
868 break;
870 case PRINT_NULL:
871 default:
872 break;
875 free(arg);
878 static enum event_type get_type(int ch)
880 if (ch == '\n')
881 return EVENT_NEWLINE;
882 if (isspace(ch))
883 return EVENT_SPACE;
884 if (isalnum(ch) || ch == '_')
885 return EVENT_ITEM;
886 if (ch == '\'')
887 return EVENT_SQUOTE;
888 if (ch == '"')
889 return EVENT_DQUOTE;
890 if (!isprint(ch))
891 return EVENT_NONE;
892 if (ch == '(' || ch == ')' || ch == ',')
893 return EVENT_DELIM;
895 return EVENT_OP;
898 static int __read_char(void)
900 if (input_buf_ptr >= input_buf_siz)
901 return -1;
903 return input_buf[input_buf_ptr++];
906 static int __peek_char(void)
908 if (input_buf_ptr >= input_buf_siz)
909 return -1;
911 return input_buf[input_buf_ptr];
915 * pevent_peek_char - peek at the next character that will be read
917 * Returns the next character read, or -1 if end of buffer.
919 int pevent_peek_char(void)
921 return __peek_char();
924 static int extend_token(char **tok, char *buf, int size)
926 char *newtok = realloc(*tok, size);
928 if (!newtok) {
929 free(*tok);
930 *tok = NULL;
931 return -1;
934 if (!*tok)
935 strcpy(newtok, buf);
936 else
937 strcat(newtok, buf);
938 *tok = newtok;
940 return 0;
943 static enum event_type force_token(const char *str, char **tok);
945 static enum event_type __read_token(char **tok)
947 char buf[BUFSIZ];
948 int ch, last_ch, quote_ch, next_ch;
949 int i = 0;
950 int tok_size = 0;
951 enum event_type type;
953 *tok = NULL;
956 ch = __read_char();
957 if (ch < 0)
958 return EVENT_NONE;
960 type = get_type(ch);
961 if (type == EVENT_NONE)
962 return type;
964 buf[i++] = ch;
966 switch (type) {
967 case EVENT_NEWLINE:
968 case EVENT_DELIM:
969 if (asprintf(tok, "%c", ch) < 0)
970 return EVENT_ERROR;
972 return type;
974 case EVENT_OP:
975 switch (ch) {
976 case '-':
977 next_ch = __peek_char();
978 if (next_ch == '>') {
979 buf[i++] = __read_char();
980 break;
982 /* fall through */
983 case '+':
984 case '|':
985 case '&':
986 case '>':
987 case '<':
988 last_ch = ch;
989 ch = __peek_char();
990 if (ch != last_ch)
991 goto test_equal;
992 buf[i++] = __read_char();
993 switch (last_ch) {
994 case '>':
995 case '<':
996 goto test_equal;
997 default:
998 break;
1000 break;
1001 case '!':
1002 case '=':
1003 goto test_equal;
1004 default: /* what should we do instead? */
1005 break;
1007 buf[i] = 0;
1008 *tok = strdup(buf);
1009 return type;
1011 test_equal:
1012 ch = __peek_char();
1013 if (ch == '=')
1014 buf[i++] = __read_char();
1015 goto out;
1017 case EVENT_DQUOTE:
1018 case EVENT_SQUOTE:
1019 /* don't keep quotes */
1020 i--;
1021 quote_ch = ch;
1022 last_ch = 0;
1023 concat:
1024 do {
1025 if (i == (BUFSIZ - 1)) {
1026 buf[i] = 0;
1027 tok_size += BUFSIZ;
1029 if (extend_token(tok, buf, tok_size) < 0)
1030 return EVENT_NONE;
1031 i = 0;
1033 last_ch = ch;
1034 ch = __read_char();
1035 buf[i++] = ch;
1036 /* the '\' '\' will cancel itself */
1037 if (ch == '\\' && last_ch == '\\')
1038 last_ch = 0;
1039 } while (ch != quote_ch || last_ch == '\\');
1040 /* remove the last quote */
1041 i--;
1044 * For strings (double quotes) check the next token.
1045 * If it is another string, concatinate the two.
1047 if (type == EVENT_DQUOTE) {
1048 unsigned long long save_input_buf_ptr = input_buf_ptr;
1050 do {
1051 ch = __read_char();
1052 } while (isspace(ch));
1053 if (ch == '"')
1054 goto concat;
1055 input_buf_ptr = save_input_buf_ptr;
1058 goto out;
1060 case EVENT_ERROR ... EVENT_SPACE:
1061 case EVENT_ITEM:
1062 default:
1063 break;
1066 while (get_type(__peek_char()) == type) {
1067 if (i == (BUFSIZ - 1)) {
1068 buf[i] = 0;
1069 tok_size += BUFSIZ;
1071 if (extend_token(tok, buf, tok_size) < 0)
1072 return EVENT_NONE;
1073 i = 0;
1075 ch = __read_char();
1076 buf[i++] = ch;
1079 out:
1080 buf[i] = 0;
1081 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1082 return EVENT_NONE;
1084 if (type == EVENT_ITEM) {
1086 * Older versions of the kernel has a bug that
1087 * creates invalid symbols and will break the mac80211
1088 * parsing. This is a work around to that bug.
1090 * See Linux kernel commit:
1091 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1093 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1094 free(*tok);
1095 *tok = NULL;
1096 return force_token("\"\%s\" ", tok);
1097 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1098 free(*tok);
1099 *tok = NULL;
1100 return force_token("\" sta:%pM\" ", tok);
1101 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1102 free(*tok);
1103 *tok = NULL;
1104 return force_token("\" vif:%p(%d)\" ", tok);
1108 return type;
1111 static enum event_type force_token(const char *str, char **tok)
1113 const char *save_input_buf;
1114 unsigned long long save_input_buf_ptr;
1115 unsigned long long save_input_buf_siz;
1116 enum event_type type;
1118 /* save off the current input pointers */
1119 save_input_buf = input_buf;
1120 save_input_buf_ptr = input_buf_ptr;
1121 save_input_buf_siz = input_buf_siz;
1123 init_input_buf(str, strlen(str));
1125 type = __read_token(tok);
1127 /* reset back to original token */
1128 input_buf = save_input_buf;
1129 input_buf_ptr = save_input_buf_ptr;
1130 input_buf_siz = save_input_buf_siz;
1132 return type;
1135 static void free_token(char *tok)
1137 if (tok)
1138 free(tok);
1141 static enum event_type read_token(char **tok)
1143 enum event_type type;
1145 for (;;) {
1146 type = __read_token(tok);
1147 if (type != EVENT_SPACE)
1148 return type;
1150 free_token(*tok);
1153 /* not reached */
1154 *tok = NULL;
1155 return EVENT_NONE;
1159 * pevent_read_token - access to utilites to use the pevent parser
1160 * @tok: The token to return
1162 * This will parse tokens from the string given by
1163 * pevent_init_data().
1165 * Returns the token type.
1167 enum event_type pevent_read_token(char **tok)
1169 return read_token(tok);
1173 * pevent_free_token - free a token returned by pevent_read_token
1174 * @token: the token to free
1176 void pevent_free_token(char *token)
1178 free_token(token);
1181 /* no newline */
1182 static enum event_type read_token_item(char **tok)
1184 enum event_type type;
1186 for (;;) {
1187 type = __read_token(tok);
1188 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1189 return type;
1190 free_token(*tok);
1191 *tok = NULL;
1194 /* not reached */
1195 *tok = NULL;
1196 return EVENT_NONE;
1199 static int test_type(enum event_type type, enum event_type expect)
1201 if (type != expect) {
1202 do_warning("Error: expected type %d but read %d",
1203 expect, type);
1204 return -1;
1206 return 0;
1209 static int test_type_token(enum event_type type, const char *token,
1210 enum event_type expect, const char *expect_tok)
1212 if (type != expect) {
1213 do_warning("Error: expected type %d but read %d",
1214 expect, type);
1215 return -1;
1218 if (strcmp(token, expect_tok) != 0) {
1219 do_warning("Error: expected '%s' but read '%s'",
1220 expect_tok, token);
1221 return -1;
1223 return 0;
1226 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1228 enum event_type type;
1230 if (newline_ok)
1231 type = read_token(tok);
1232 else
1233 type = read_token_item(tok);
1234 return test_type(type, expect);
1237 static int read_expect_type(enum event_type expect, char **tok)
1239 return __read_expect_type(expect, tok, 1);
1242 static int __read_expected(enum event_type expect, const char *str,
1243 int newline_ok)
1245 enum event_type type;
1246 char *token;
1247 int ret;
1249 if (newline_ok)
1250 type = read_token(&token);
1251 else
1252 type = read_token_item(&token);
1254 ret = test_type_token(type, token, expect, str);
1256 free_token(token);
1258 return ret;
1261 static int read_expected(enum event_type expect, const char *str)
1263 return __read_expected(expect, str, 1);
1266 static int read_expected_item(enum event_type expect, const char *str)
1268 return __read_expected(expect, str, 0);
1271 static char *event_read_name(void)
1273 char *token;
1275 if (read_expected(EVENT_ITEM, "name") < 0)
1276 return NULL;
1278 if (read_expected(EVENT_OP, ":") < 0)
1279 return NULL;
1281 if (read_expect_type(EVENT_ITEM, &token) < 0)
1282 goto fail;
1284 return token;
1286 fail:
1287 free_token(token);
1288 return NULL;
1291 static int event_read_id(void)
1293 char *token;
1294 int id;
1296 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1297 return -1;
1299 if (read_expected(EVENT_OP, ":") < 0)
1300 return -1;
1302 if (read_expect_type(EVENT_ITEM, &token) < 0)
1303 goto fail;
1305 id = strtoul(token, NULL, 0);
1306 free_token(token);
1307 return id;
1309 fail:
1310 free_token(token);
1311 return -1;
1314 static int field_is_string(struct format_field *field)
1316 if ((field->flags & FIELD_IS_ARRAY) &&
1317 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1318 strstr(field->type, "s8")))
1319 return 1;
1321 return 0;
1324 static int field_is_dynamic(struct format_field *field)
1326 if (strncmp(field->type, "__data_loc", 10) == 0)
1327 return 1;
1329 return 0;
1332 static int field_is_long(struct format_field *field)
1334 /* includes long long */
1335 if (strstr(field->type, "long"))
1336 return 1;
1338 return 0;
1341 static unsigned int type_size(const char *name)
1343 /* This covers all FIELD_IS_STRING types. */
1344 static struct {
1345 const char *type;
1346 unsigned int size;
1347 } table[] = {
1348 { "u8", 1 },
1349 { "u16", 2 },
1350 { "u32", 4 },
1351 { "u64", 8 },
1352 { "s8", 1 },
1353 { "s16", 2 },
1354 { "s32", 4 },
1355 { "s64", 8 },
1356 { "char", 1 },
1357 { },
1359 int i;
1361 for (i = 0; table[i].type; i++) {
1362 if (!strcmp(table[i].type, name))
1363 return table[i].size;
1366 return 0;
1369 static int event_read_fields(struct event_format *event, struct format_field **fields)
1371 struct format_field *field = NULL;
1372 enum event_type type;
1373 char *token;
1374 char *last_token;
1375 int count = 0;
1377 do {
1378 unsigned int size_dynamic = 0;
1380 type = read_token(&token);
1381 if (type == EVENT_NEWLINE) {
1382 free_token(token);
1383 return count;
1386 count++;
1388 if (test_type_token(type, token, EVENT_ITEM, "field"))
1389 goto fail;
1390 free_token(token);
1392 type = read_token(&token);
1394 * The ftrace fields may still use the "special" name.
1395 * Just ignore it.
1397 if (event->flags & EVENT_FL_ISFTRACE &&
1398 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1399 free_token(token);
1400 type = read_token(&token);
1403 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1404 goto fail;
1406 free_token(token);
1407 if (read_expect_type(EVENT_ITEM, &token) < 0)
1408 goto fail;
1410 last_token = token;
1412 field = calloc(1, sizeof(*field));
1413 if (!field)
1414 goto fail;
1416 field->event = event;
1418 /* read the rest of the type */
1419 for (;;) {
1420 type = read_token(&token);
1421 if (type == EVENT_ITEM ||
1422 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1424 * Some of the ftrace fields are broken and have
1425 * an illegal "." in them.
1427 (event->flags & EVENT_FL_ISFTRACE &&
1428 type == EVENT_OP && strcmp(token, ".") == 0)) {
1430 if (strcmp(token, "*") == 0)
1431 field->flags |= FIELD_IS_POINTER;
1433 if (field->type) {
1434 char *new_type;
1435 new_type = realloc(field->type,
1436 strlen(field->type) +
1437 strlen(last_token) + 2);
1438 if (!new_type) {
1439 free(last_token);
1440 goto fail;
1442 field->type = new_type;
1443 strcat(field->type, " ");
1444 strcat(field->type, last_token);
1445 free(last_token);
1446 } else
1447 field->type = last_token;
1448 last_token = token;
1449 continue;
1452 break;
1455 if (!field->type) {
1456 do_warning_event(event, "%s: no type found", __func__);
1457 goto fail;
1459 field->name = field->alias = last_token;
1461 if (test_type(type, EVENT_OP))
1462 goto fail;
1464 if (strcmp(token, "[") == 0) {
1465 enum event_type last_type = type;
1466 char *brackets = token;
1467 char *new_brackets;
1468 int len;
1470 field->flags |= FIELD_IS_ARRAY;
1472 type = read_token(&token);
1474 if (type == EVENT_ITEM)
1475 field->arraylen = strtoul(token, NULL, 0);
1476 else
1477 field->arraylen = 0;
1479 while (strcmp(token, "]") != 0) {
1480 if (last_type == EVENT_ITEM &&
1481 type == EVENT_ITEM)
1482 len = 2;
1483 else
1484 len = 1;
1485 last_type = type;
1487 new_brackets = realloc(brackets,
1488 strlen(brackets) +
1489 strlen(token) + len);
1490 if (!new_brackets) {
1491 free(brackets);
1492 goto fail;
1494 brackets = new_brackets;
1495 if (len == 2)
1496 strcat(brackets, " ");
1497 strcat(brackets, token);
1498 /* We only care about the last token */
1499 field->arraylen = strtoul(token, NULL, 0);
1500 free_token(token);
1501 type = read_token(&token);
1502 if (type == EVENT_NONE) {
1503 do_warning_event(event, "failed to find token");
1504 goto fail;
1508 free_token(token);
1510 new_brackets = realloc(brackets, strlen(brackets) + 2);
1511 if (!new_brackets) {
1512 free(brackets);
1513 goto fail;
1515 brackets = new_brackets;
1516 strcat(brackets, "]");
1518 /* add brackets to type */
1520 type = read_token(&token);
1522 * If the next token is not an OP, then it is of
1523 * the format: type [] item;
1525 if (type == EVENT_ITEM) {
1526 char *new_type;
1527 new_type = realloc(field->type,
1528 strlen(field->type) +
1529 strlen(field->name) +
1530 strlen(brackets) + 2);
1531 if (!new_type) {
1532 free(brackets);
1533 goto fail;
1535 field->type = new_type;
1536 strcat(field->type, " ");
1537 strcat(field->type, field->name);
1538 size_dynamic = type_size(field->name);
1539 free_token(field->name);
1540 strcat(field->type, brackets);
1541 field->name = field->alias = token;
1542 type = read_token(&token);
1543 } else {
1544 char *new_type;
1545 new_type = realloc(field->type,
1546 strlen(field->type) +
1547 strlen(brackets) + 1);
1548 if (!new_type) {
1549 free(brackets);
1550 goto fail;
1552 field->type = new_type;
1553 strcat(field->type, brackets);
1555 free(brackets);
1558 if (field_is_string(field))
1559 field->flags |= FIELD_IS_STRING;
1560 if (field_is_dynamic(field))
1561 field->flags |= FIELD_IS_DYNAMIC;
1562 if (field_is_long(field))
1563 field->flags |= FIELD_IS_LONG;
1565 if (test_type_token(type, token, EVENT_OP, ";"))
1566 goto fail;
1567 free_token(token);
1569 if (read_expected(EVENT_ITEM, "offset") < 0)
1570 goto fail_expect;
1572 if (read_expected(EVENT_OP, ":") < 0)
1573 goto fail_expect;
1575 if (read_expect_type(EVENT_ITEM, &token))
1576 goto fail;
1577 field->offset = strtoul(token, NULL, 0);
1578 free_token(token);
1580 if (read_expected(EVENT_OP, ";") < 0)
1581 goto fail_expect;
1583 if (read_expected(EVENT_ITEM, "size") < 0)
1584 goto fail_expect;
1586 if (read_expected(EVENT_OP, ":") < 0)
1587 goto fail_expect;
1589 if (read_expect_type(EVENT_ITEM, &token))
1590 goto fail;
1591 field->size = strtoul(token, NULL, 0);
1592 free_token(token);
1594 if (read_expected(EVENT_OP, ";") < 0)
1595 goto fail_expect;
1597 type = read_token(&token);
1598 if (type != EVENT_NEWLINE) {
1599 /* newer versions of the kernel have a "signed" type */
1600 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1601 goto fail;
1603 free_token(token);
1605 if (read_expected(EVENT_OP, ":") < 0)
1606 goto fail_expect;
1608 if (read_expect_type(EVENT_ITEM, &token))
1609 goto fail;
1611 if (strtoul(token, NULL, 0))
1612 field->flags |= FIELD_IS_SIGNED;
1614 free_token(token);
1615 if (read_expected(EVENT_OP, ";") < 0)
1616 goto fail_expect;
1618 if (read_expect_type(EVENT_NEWLINE, &token))
1619 goto fail;
1622 free_token(token);
1624 if (field->flags & FIELD_IS_ARRAY) {
1625 if (field->arraylen)
1626 field->elementsize = field->size / field->arraylen;
1627 else if (field->flags & FIELD_IS_DYNAMIC)
1628 field->elementsize = size_dynamic;
1629 else if (field->flags & FIELD_IS_STRING)
1630 field->elementsize = 1;
1631 else if (field->flags & FIELD_IS_LONG)
1632 field->elementsize = event->pevent ?
1633 event->pevent->long_size :
1634 sizeof(long);
1635 } else
1636 field->elementsize = field->size;
1638 *fields = field;
1639 fields = &field->next;
1641 } while (1);
1643 return 0;
1645 fail:
1646 free_token(token);
1647 fail_expect:
1648 if (field) {
1649 free(field->type);
1650 free(field->name);
1651 free(field);
1653 return -1;
1656 static int event_read_format(struct event_format *event)
1658 char *token;
1659 int ret;
1661 if (read_expected_item(EVENT_ITEM, "format") < 0)
1662 return -1;
1664 if (read_expected(EVENT_OP, ":") < 0)
1665 return -1;
1667 if (read_expect_type(EVENT_NEWLINE, &token))
1668 goto fail;
1669 free_token(token);
1671 ret = event_read_fields(event, &event->format.common_fields);
1672 if (ret < 0)
1673 return ret;
1674 event->format.nr_common = ret;
1676 ret = event_read_fields(event, &event->format.fields);
1677 if (ret < 0)
1678 return ret;
1679 event->format.nr_fields = ret;
1681 return 0;
1683 fail:
1684 free_token(token);
1685 return -1;
1688 static enum event_type
1689 process_arg_token(struct event_format *event, struct print_arg *arg,
1690 char **tok, enum event_type type);
1692 static enum event_type
1693 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1695 enum event_type type;
1696 char *token;
1698 type = read_token(&token);
1699 *tok = token;
1701 return process_arg_token(event, arg, tok, type);
1704 static enum event_type
1705 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1708 * For __print_symbolic() and __print_flags, we need to completely
1709 * evaluate the first argument, which defines what to print next.
1711 static enum event_type
1712 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1714 enum event_type type;
1716 type = process_arg(event, arg, tok);
1718 while (type == EVENT_OP) {
1719 type = process_op(event, arg, tok);
1722 return type;
1725 static enum event_type
1726 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1728 struct print_arg *arg, *left, *right;
1729 enum event_type type;
1730 char *token = NULL;
1732 arg = alloc_arg();
1733 left = alloc_arg();
1734 right = alloc_arg();
1736 if (!arg || !left || !right) {
1737 do_warning_event(event, "%s: not enough memory!", __func__);
1738 /* arg will be freed at out_free */
1739 free_arg(left);
1740 free_arg(right);
1741 goto out_free;
1744 arg->type = PRINT_OP;
1745 arg->op.left = left;
1746 arg->op.right = right;
1748 *tok = NULL;
1749 type = process_arg(event, left, &token);
1751 again:
1752 if (type == EVENT_ERROR)
1753 goto out_free;
1755 /* Handle other operations in the arguments */
1756 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1757 type = process_op(event, left, &token);
1758 goto again;
1761 if (test_type_token(type, token, EVENT_OP, ":"))
1762 goto out_free;
1764 arg->op.op = token;
1766 type = process_arg(event, right, &token);
1768 top->op.right = arg;
1770 *tok = token;
1771 return type;
1773 out_free:
1774 /* Top may point to itself */
1775 top->op.right = NULL;
1776 free_token(token);
1777 free_arg(arg);
1778 return EVENT_ERROR;
1781 static enum event_type
1782 process_array(struct event_format *event, struct print_arg *top, char **tok)
1784 struct print_arg *arg;
1785 enum event_type type;
1786 char *token = NULL;
1788 arg = alloc_arg();
1789 if (!arg) {
1790 do_warning_event(event, "%s: not enough memory!", __func__);
1791 /* '*tok' is set to top->op.op. No need to free. */
1792 *tok = NULL;
1793 return EVENT_ERROR;
1796 *tok = NULL;
1797 type = process_arg(event, arg, &token);
1798 if (test_type_token(type, token, EVENT_OP, "]"))
1799 goto out_free;
1801 top->op.right = arg;
1803 free_token(token);
1804 type = read_token_item(&token);
1805 *tok = token;
1807 return type;
1809 out_free:
1810 free_token(token);
1811 free_arg(arg);
1812 return EVENT_ERROR;
1815 static int get_op_prio(char *op)
1817 if (!op[1]) {
1818 switch (op[0]) {
1819 case '~':
1820 case '!':
1821 return 4;
1822 case '*':
1823 case '/':
1824 case '%':
1825 return 6;
1826 case '+':
1827 case '-':
1828 return 7;
1829 /* '>>' and '<<' are 8 */
1830 case '<':
1831 case '>':
1832 return 9;
1833 /* '==' and '!=' are 10 */
1834 case '&':
1835 return 11;
1836 case '^':
1837 return 12;
1838 case '|':
1839 return 13;
1840 case '?':
1841 return 16;
1842 default:
1843 do_warning("unknown op '%c'", op[0]);
1844 return -1;
1846 } else {
1847 if (strcmp(op, "++") == 0 ||
1848 strcmp(op, "--") == 0) {
1849 return 3;
1850 } else if (strcmp(op, ">>") == 0 ||
1851 strcmp(op, "<<") == 0) {
1852 return 8;
1853 } else if (strcmp(op, ">=") == 0 ||
1854 strcmp(op, "<=") == 0) {
1855 return 9;
1856 } else if (strcmp(op, "==") == 0 ||
1857 strcmp(op, "!=") == 0) {
1858 return 10;
1859 } else if (strcmp(op, "&&") == 0) {
1860 return 14;
1861 } else if (strcmp(op, "||") == 0) {
1862 return 15;
1863 } else {
1864 do_warning("unknown op '%s'", op);
1865 return -1;
1870 static int set_op_prio(struct print_arg *arg)
1873 /* single ops are the greatest */
1874 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1875 arg->op.prio = 0;
1876 else
1877 arg->op.prio = get_op_prio(arg->op.op);
1879 return arg->op.prio;
1882 /* Note, *tok does not get freed, but will most likely be saved */
1883 static enum event_type
1884 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1886 struct print_arg *left, *right = NULL;
1887 enum event_type type;
1888 char *token;
1890 /* the op is passed in via tok */
1891 token = *tok;
1893 if (arg->type == PRINT_OP && !arg->op.left) {
1894 /* handle single op */
1895 if (token[1]) {
1896 do_warning_event(event, "bad op token %s", token);
1897 goto out_free;
1899 switch (token[0]) {
1900 case '~':
1901 case '!':
1902 case '+':
1903 case '-':
1904 break;
1905 default:
1906 do_warning_event(event, "bad op token %s", token);
1907 goto out_free;
1911 /* make an empty left */
1912 left = alloc_arg();
1913 if (!left)
1914 goto out_warn_free;
1916 left->type = PRINT_NULL;
1917 arg->op.left = left;
1919 right = alloc_arg();
1920 if (!right)
1921 goto out_warn_free;
1923 arg->op.right = right;
1925 /* do not free the token, it belongs to an op */
1926 *tok = NULL;
1927 type = process_arg(event, right, tok);
1929 } else if (strcmp(token, "?") == 0) {
1931 left = alloc_arg();
1932 if (!left)
1933 goto out_warn_free;
1935 /* copy the top arg to the left */
1936 *left = *arg;
1938 arg->type = PRINT_OP;
1939 arg->op.op = token;
1940 arg->op.left = left;
1941 arg->op.prio = 0;
1943 /* it will set arg->op.right */
1944 type = process_cond(event, arg, tok);
1946 } else if (strcmp(token, ">>") == 0 ||
1947 strcmp(token, "<<") == 0 ||
1948 strcmp(token, "&") == 0 ||
1949 strcmp(token, "|") == 0 ||
1950 strcmp(token, "&&") == 0 ||
1951 strcmp(token, "||") == 0 ||
1952 strcmp(token, "-") == 0 ||
1953 strcmp(token, "+") == 0 ||
1954 strcmp(token, "*") == 0 ||
1955 strcmp(token, "^") == 0 ||
1956 strcmp(token, "/") == 0 ||
1957 strcmp(token, "%") == 0 ||
1958 strcmp(token, "<") == 0 ||
1959 strcmp(token, ">") == 0 ||
1960 strcmp(token, "<=") == 0 ||
1961 strcmp(token, ">=") == 0 ||
1962 strcmp(token, "==") == 0 ||
1963 strcmp(token, "!=") == 0) {
1965 left = alloc_arg();
1966 if (!left)
1967 goto out_warn_free;
1969 /* copy the top arg to the left */
1970 *left = *arg;
1972 arg->type = PRINT_OP;
1973 arg->op.op = token;
1974 arg->op.left = left;
1975 arg->op.right = NULL;
1977 if (set_op_prio(arg) == -1) {
1978 event->flags |= EVENT_FL_FAILED;
1979 /* arg->op.op (= token) will be freed at out_free */
1980 arg->op.op = NULL;
1981 goto out_free;
1984 type = read_token_item(&token);
1985 *tok = token;
1987 /* could just be a type pointer */
1988 if ((strcmp(arg->op.op, "*") == 0) &&
1989 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1990 char *new_atom;
1992 if (left->type != PRINT_ATOM) {
1993 do_warning_event(event, "bad pointer type");
1994 goto out_free;
1996 new_atom = realloc(left->atom.atom,
1997 strlen(left->atom.atom) + 3);
1998 if (!new_atom)
1999 goto out_warn_free;
2001 left->atom.atom = new_atom;
2002 strcat(left->atom.atom, " *");
2003 free(arg->op.op);
2004 *arg = *left;
2005 free(left);
2007 return type;
2010 right = alloc_arg();
2011 if (!right)
2012 goto out_warn_free;
2014 type = process_arg_token(event, right, tok, type);
2015 if (type == EVENT_ERROR) {
2016 free_arg(right);
2017 /* token was freed in process_arg_token() via *tok */
2018 token = NULL;
2019 goto out_free;
2022 if (right->type == PRINT_OP &&
2023 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2024 struct print_arg tmp;
2026 /* rotate ops according to the priority */
2027 arg->op.right = right->op.left;
2029 tmp = *arg;
2030 *arg = *right;
2031 *right = tmp;
2033 arg->op.left = right;
2034 } else {
2035 arg->op.right = right;
2038 } else if (strcmp(token, "[") == 0) {
2040 left = alloc_arg();
2041 if (!left)
2042 goto out_warn_free;
2044 *left = *arg;
2046 arg->type = PRINT_OP;
2047 arg->op.op = token;
2048 arg->op.left = left;
2050 arg->op.prio = 0;
2052 /* it will set arg->op.right */
2053 type = process_array(event, arg, tok);
2055 } else {
2056 do_warning_event(event, "unknown op '%s'", token);
2057 event->flags |= EVENT_FL_FAILED;
2058 /* the arg is now the left side */
2059 goto out_free;
2062 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2063 int prio;
2065 /* higher prios need to be closer to the root */
2066 prio = get_op_prio(*tok);
2068 if (prio > arg->op.prio)
2069 return process_op(event, arg, tok);
2071 return process_op(event, right, tok);
2074 return type;
2076 out_warn_free:
2077 do_warning_event(event, "%s: not enough memory!", __func__);
2078 out_free:
2079 free_token(token);
2080 *tok = NULL;
2081 return EVENT_ERROR;
2084 static enum event_type
2085 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2086 char **tok)
2088 enum event_type type;
2089 char *field;
2090 char *token;
2092 if (read_expected(EVENT_OP, "->") < 0)
2093 goto out_err;
2095 if (read_expect_type(EVENT_ITEM, &token) < 0)
2096 goto out_free;
2097 field = token;
2099 arg->type = PRINT_FIELD;
2100 arg->field.name = field;
2102 if (is_flag_field) {
2103 arg->field.field = pevent_find_any_field(event, arg->field.name);
2104 arg->field.field->flags |= FIELD_IS_FLAG;
2105 is_flag_field = 0;
2106 } else if (is_symbolic_field) {
2107 arg->field.field = pevent_find_any_field(event, arg->field.name);
2108 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2109 is_symbolic_field = 0;
2112 type = read_token(&token);
2113 *tok = token;
2115 return type;
2117 out_free:
2118 free_token(token);
2119 out_err:
2120 *tok = NULL;
2121 return EVENT_ERROR;
2124 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2125 struct print_arg **print_arg)
2127 struct print_arg *field;
2128 enum event_type type;
2129 char *token;
2130 int ret = 0;
2132 field = alloc_arg();
2133 if (!field) {
2134 do_warning_event(event, "%s: not enough memory!", __func__);
2135 errno = ENOMEM;
2136 return -1;
2139 type = process_arg(event, field, &token);
2141 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2142 errno = EINVAL;
2143 ret = -1;
2144 free_arg(field);
2145 goto out_free_token;
2148 *print_arg = field;
2150 out_free_token:
2151 free_token(token);
2153 return ret;
2156 static char *arg_eval (struct print_arg *arg);
2158 static unsigned long long
2159 eval_type_str(unsigned long long val, const char *type, int pointer)
2161 int sign = 0;
2162 char *ref;
2163 int len;
2165 len = strlen(type);
2167 if (pointer) {
2169 if (type[len-1] != '*') {
2170 do_warning("pointer expected with non pointer type");
2171 return val;
2174 ref = malloc(len);
2175 if (!ref) {
2176 do_warning("%s: not enough memory!", __func__);
2177 return val;
2179 memcpy(ref, type, len);
2181 /* chop off the " *" */
2182 ref[len - 2] = 0;
2184 val = eval_type_str(val, ref, 0);
2185 free(ref);
2186 return val;
2189 /* check if this is a pointer */
2190 if (type[len - 1] == '*')
2191 return val;
2193 /* Try to figure out the arg size*/
2194 if (strncmp(type, "struct", 6) == 0)
2195 /* all bets off */
2196 return val;
2198 if (strcmp(type, "u8") == 0)
2199 return val & 0xff;
2201 if (strcmp(type, "u16") == 0)
2202 return val & 0xffff;
2204 if (strcmp(type, "u32") == 0)
2205 return val & 0xffffffff;
2207 if (strcmp(type, "u64") == 0 ||
2208 strcmp(type, "s64"))
2209 return val;
2211 if (strcmp(type, "s8") == 0)
2212 return (unsigned long long)(char)val & 0xff;
2214 if (strcmp(type, "s16") == 0)
2215 return (unsigned long long)(short)val & 0xffff;
2217 if (strcmp(type, "s32") == 0)
2218 return (unsigned long long)(int)val & 0xffffffff;
2220 if (strncmp(type, "unsigned ", 9) == 0) {
2221 sign = 0;
2222 type += 9;
2225 if (strcmp(type, "char") == 0) {
2226 if (sign)
2227 return (unsigned long long)(char)val & 0xff;
2228 else
2229 return val & 0xff;
2232 if (strcmp(type, "short") == 0) {
2233 if (sign)
2234 return (unsigned long long)(short)val & 0xffff;
2235 else
2236 return val & 0xffff;
2239 if (strcmp(type, "int") == 0) {
2240 if (sign)
2241 return (unsigned long long)(int)val & 0xffffffff;
2242 else
2243 return val & 0xffffffff;
2246 return val;
2250 * Try to figure out the type.
2252 static unsigned long long
2253 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2255 if (arg->type != PRINT_TYPE) {
2256 do_warning("expected type argument");
2257 return 0;
2260 return eval_type_str(val, arg->typecast.type, pointer);
2263 static int arg_num_eval(struct print_arg *arg, long long *val)
2265 long long left, right;
2266 int ret = 1;
2268 switch (arg->type) {
2269 case PRINT_ATOM:
2270 *val = strtoll(arg->atom.atom, NULL, 0);
2271 break;
2272 case PRINT_TYPE:
2273 ret = arg_num_eval(arg->typecast.item, val);
2274 if (!ret)
2275 break;
2276 *val = eval_type(*val, arg, 0);
2277 break;
2278 case PRINT_OP:
2279 switch (arg->op.op[0]) {
2280 case '|':
2281 ret = arg_num_eval(arg->op.left, &left);
2282 if (!ret)
2283 break;
2284 ret = arg_num_eval(arg->op.right, &right);
2285 if (!ret)
2286 break;
2287 if (arg->op.op[1])
2288 *val = left || right;
2289 else
2290 *val = left | right;
2291 break;
2292 case '&':
2293 ret = arg_num_eval(arg->op.left, &left);
2294 if (!ret)
2295 break;
2296 ret = arg_num_eval(arg->op.right, &right);
2297 if (!ret)
2298 break;
2299 if (arg->op.op[1])
2300 *val = left && right;
2301 else
2302 *val = left & right;
2303 break;
2304 case '<':
2305 ret = arg_num_eval(arg->op.left, &left);
2306 if (!ret)
2307 break;
2308 ret = arg_num_eval(arg->op.right, &right);
2309 if (!ret)
2310 break;
2311 switch (arg->op.op[1]) {
2312 case 0:
2313 *val = left < right;
2314 break;
2315 case '<':
2316 *val = left << right;
2317 break;
2318 case '=':
2319 *val = left <= right;
2320 break;
2321 default:
2322 do_warning("unknown op '%s'", arg->op.op);
2323 ret = 0;
2325 break;
2326 case '>':
2327 ret = arg_num_eval(arg->op.left, &left);
2328 if (!ret)
2329 break;
2330 ret = arg_num_eval(arg->op.right, &right);
2331 if (!ret)
2332 break;
2333 switch (arg->op.op[1]) {
2334 case 0:
2335 *val = left > right;
2336 break;
2337 case '>':
2338 *val = left >> right;
2339 break;
2340 case '=':
2341 *val = left >= right;
2342 break;
2343 default:
2344 do_warning("unknown op '%s'", arg->op.op);
2345 ret = 0;
2347 break;
2348 case '=':
2349 ret = arg_num_eval(arg->op.left, &left);
2350 if (!ret)
2351 break;
2352 ret = arg_num_eval(arg->op.right, &right);
2353 if (!ret)
2354 break;
2356 if (arg->op.op[1] != '=') {
2357 do_warning("unknown op '%s'", arg->op.op);
2358 ret = 0;
2359 } else
2360 *val = left == right;
2361 break;
2362 case '!':
2363 ret = arg_num_eval(arg->op.left, &left);
2364 if (!ret)
2365 break;
2366 ret = arg_num_eval(arg->op.right, &right);
2367 if (!ret)
2368 break;
2370 switch (arg->op.op[1]) {
2371 case '=':
2372 *val = left != right;
2373 break;
2374 default:
2375 do_warning("unknown op '%s'", arg->op.op);
2376 ret = 0;
2378 break;
2379 case '-':
2380 /* check for negative */
2381 if (arg->op.left->type == PRINT_NULL)
2382 left = 0;
2383 else
2384 ret = arg_num_eval(arg->op.left, &left);
2385 if (!ret)
2386 break;
2387 ret = arg_num_eval(arg->op.right, &right);
2388 if (!ret)
2389 break;
2390 *val = left - right;
2391 break;
2392 case '+':
2393 if (arg->op.left->type == PRINT_NULL)
2394 left = 0;
2395 else
2396 ret = arg_num_eval(arg->op.left, &left);
2397 if (!ret)
2398 break;
2399 ret = arg_num_eval(arg->op.right, &right);
2400 if (!ret)
2401 break;
2402 *val = left + right;
2403 break;
2404 case '~':
2405 ret = arg_num_eval(arg->op.right, &right);
2406 if (!ret)
2407 break;
2408 *val = ~right;
2409 break;
2410 default:
2411 do_warning("unknown op '%s'", arg->op.op);
2412 ret = 0;
2414 break;
2416 case PRINT_NULL:
2417 case PRINT_FIELD ... PRINT_SYMBOL:
2418 case PRINT_STRING:
2419 case PRINT_BSTRING:
2420 case PRINT_BITMASK:
2421 default:
2422 do_warning("invalid eval type %d", arg->type);
2423 ret = 0;
2426 return ret;
2429 static char *arg_eval (struct print_arg *arg)
2431 long long val;
2432 static char buf[20];
2434 switch (arg->type) {
2435 case PRINT_ATOM:
2436 return arg->atom.atom;
2437 case PRINT_TYPE:
2438 return arg_eval(arg->typecast.item);
2439 case PRINT_OP:
2440 if (!arg_num_eval(arg, &val))
2441 break;
2442 sprintf(buf, "%lld", val);
2443 return buf;
2445 case PRINT_NULL:
2446 case PRINT_FIELD ... PRINT_SYMBOL:
2447 case PRINT_STRING:
2448 case PRINT_BSTRING:
2449 case PRINT_BITMASK:
2450 default:
2451 do_warning("invalid eval type %d", arg->type);
2452 break;
2455 return NULL;
2458 static enum event_type
2459 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2461 enum event_type type;
2462 struct print_arg *arg = NULL;
2463 struct print_flag_sym *field;
2464 char *token = *tok;
2465 char *value;
2467 do {
2468 free_token(token);
2469 type = read_token_item(&token);
2470 if (test_type_token(type, token, EVENT_OP, "{"))
2471 break;
2473 arg = alloc_arg();
2474 if (!arg)
2475 goto out_free;
2477 free_token(token);
2478 type = process_arg(event, arg, &token);
2480 if (type == EVENT_OP)
2481 type = process_op(event, arg, &token);
2483 if (type == EVENT_ERROR)
2484 goto out_free;
2486 if (test_type_token(type, token, EVENT_DELIM, ","))
2487 goto out_free;
2489 field = calloc(1, sizeof(*field));
2490 if (!field)
2491 goto out_free;
2493 value = arg_eval(arg);
2494 if (value == NULL)
2495 goto out_free_field;
2496 field->value = strdup(value);
2497 if (field->value == NULL)
2498 goto out_free_field;
2500 free_arg(arg);
2501 arg = alloc_arg();
2502 if (!arg)
2503 goto out_free;
2505 free_token(token);
2506 type = process_arg(event, arg, &token);
2507 if (test_type_token(type, token, EVENT_OP, "}"))
2508 goto out_free_field;
2510 value = arg_eval(arg);
2511 if (value == NULL)
2512 goto out_free_field;
2513 field->str = strdup(value);
2514 if (field->str == NULL)
2515 goto out_free_field;
2516 free_arg(arg);
2517 arg = NULL;
2519 *list = field;
2520 list = &field->next;
2522 free_token(token);
2523 type = read_token_item(&token);
2524 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2526 *tok = token;
2527 return type;
2529 out_free_field:
2530 free_flag_sym(field);
2531 out_free:
2532 free_arg(arg);
2533 free_token(token);
2534 *tok = NULL;
2536 return EVENT_ERROR;
2539 static enum event_type
2540 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2542 struct print_arg *field;
2543 enum event_type type;
2544 char *token = NULL;
2546 memset(arg, 0, sizeof(*arg));
2547 arg->type = PRINT_FLAGS;
2549 field = alloc_arg();
2550 if (!field) {
2551 do_warning_event(event, "%s: not enough memory!", __func__);
2552 goto out_free;
2555 type = process_field_arg(event, field, &token);
2557 /* Handle operations in the first argument */
2558 while (type == EVENT_OP)
2559 type = process_op(event, field, &token);
2561 if (test_type_token(type, token, EVENT_DELIM, ","))
2562 goto out_free_field;
2563 free_token(token);
2565 arg->flags.field = field;
2567 type = read_token_item(&token);
2568 if (event_item_type(type)) {
2569 arg->flags.delim = token;
2570 type = read_token_item(&token);
2573 if (test_type_token(type, token, EVENT_DELIM, ","))
2574 goto out_free;
2576 type = process_fields(event, &arg->flags.flags, &token);
2577 if (test_type_token(type, token, EVENT_DELIM, ")"))
2578 goto out_free;
2580 free_token(token);
2581 type = read_token_item(tok);
2582 return type;
2584 out_free_field:
2585 free_arg(field);
2586 out_free:
2587 free_token(token);
2588 *tok = NULL;
2589 return EVENT_ERROR;
2592 static enum event_type
2593 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2595 struct print_arg *field;
2596 enum event_type type;
2597 char *token = NULL;
2599 memset(arg, 0, sizeof(*arg));
2600 arg->type = PRINT_SYMBOL;
2602 field = alloc_arg();
2603 if (!field) {
2604 do_warning_event(event, "%s: not enough memory!", __func__);
2605 goto out_free;
2608 type = process_field_arg(event, field, &token);
2610 if (test_type_token(type, token, EVENT_DELIM, ","))
2611 goto out_free_field;
2613 arg->symbol.field = field;
2615 type = process_fields(event, &arg->symbol.symbols, &token);
2616 if (test_type_token(type, token, EVENT_DELIM, ")"))
2617 goto out_free;
2619 free_token(token);
2620 type = read_token_item(tok);
2621 return type;
2623 out_free_field:
2624 free_arg(field);
2625 out_free:
2626 free_token(token);
2627 *tok = NULL;
2628 return EVENT_ERROR;
2631 static enum event_type
2632 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2634 memset(arg, 0, sizeof(*arg));
2635 arg->type = PRINT_HEX;
2637 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2638 goto out;
2640 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2641 goto free_field;
2643 return read_token_item(tok);
2645 free_field:
2646 free_arg(arg->hex.field);
2647 arg->hex.field = NULL;
2648 out:
2649 *tok = NULL;
2650 return EVENT_ERROR;
2653 static enum event_type
2654 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2656 memset(arg, 0, sizeof(*arg));
2657 arg->type = PRINT_INT_ARRAY;
2659 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2660 goto out;
2662 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2663 goto free_field;
2665 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2666 goto free_size;
2668 return read_token_item(tok);
2670 free_size:
2671 free_arg(arg->int_array.count);
2672 arg->int_array.count = NULL;
2673 free_field:
2674 free_arg(arg->int_array.field);
2675 arg->int_array.field = NULL;
2676 out:
2677 *tok = NULL;
2678 return EVENT_ERROR;
2681 static enum event_type
2682 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2684 struct format_field *field;
2685 enum event_type type;
2686 char *token;
2688 memset(arg, 0, sizeof(*arg));
2689 arg->type = PRINT_DYNAMIC_ARRAY;
2692 * The item within the parenthesis is another field that holds
2693 * the index into where the array starts.
2695 type = read_token(&token);
2696 *tok = token;
2697 if (type != EVENT_ITEM)
2698 goto out_free;
2700 /* Find the field */
2702 field = pevent_find_field(event, token);
2703 if (!field)
2704 goto out_free;
2706 arg->dynarray.field = field;
2707 arg->dynarray.index = 0;
2709 if (read_expected(EVENT_DELIM, ")") < 0)
2710 goto out_free;
2712 free_token(token);
2713 type = read_token_item(&token);
2714 *tok = token;
2715 if (type != EVENT_OP || strcmp(token, "[") != 0)
2716 return type;
2718 free_token(token);
2719 arg = alloc_arg();
2720 if (!arg) {
2721 do_warning_event(event, "%s: not enough memory!", __func__);
2722 *tok = NULL;
2723 return EVENT_ERROR;
2726 type = process_arg(event, arg, &token);
2727 if (type == EVENT_ERROR)
2728 goto out_free_arg;
2730 if (!test_type_token(type, token, EVENT_OP, "]"))
2731 goto out_free_arg;
2733 free_token(token);
2734 type = read_token_item(tok);
2735 return type;
2737 out_free_arg:
2738 free_arg(arg);
2739 out_free:
2740 free_token(token);
2741 *tok = NULL;
2742 return EVENT_ERROR;
2745 static enum event_type
2746 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2747 char **tok)
2749 struct format_field *field;
2750 enum event_type type;
2751 char *token;
2753 if (read_expect_type(EVENT_ITEM, &token) < 0)
2754 goto out_free;
2756 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2758 /* Find the field */
2759 field = pevent_find_field(event, token);
2760 if (!field)
2761 goto out_free;
2763 arg->dynarray.field = field;
2764 arg->dynarray.index = 0;
2766 if (read_expected(EVENT_DELIM, ")") < 0)
2767 goto out_err;
2769 type = read_token(&token);
2770 *tok = token;
2772 return type;
2774 out_free:
2775 free_token(token);
2776 out_err:
2777 *tok = NULL;
2778 return EVENT_ERROR;
2781 static enum event_type
2782 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2784 struct print_arg *item_arg;
2785 enum event_type type;
2786 char *token;
2788 type = process_arg(event, arg, &token);
2790 if (type == EVENT_ERROR)
2791 goto out_free;
2793 if (type == EVENT_OP)
2794 type = process_op(event, arg, &token);
2796 if (type == EVENT_ERROR)
2797 goto out_free;
2799 if (test_type_token(type, token, EVENT_DELIM, ")"))
2800 goto out_free;
2802 free_token(token);
2803 type = read_token_item(&token);
2806 * If the next token is an item or another open paren, then
2807 * this was a typecast.
2809 if (event_item_type(type) ||
2810 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2812 /* make this a typecast and contine */
2814 /* prevous must be an atom */
2815 if (arg->type != PRINT_ATOM) {
2816 do_warning_event(event, "previous needed to be PRINT_ATOM");
2817 goto out_free;
2820 item_arg = alloc_arg();
2821 if (!item_arg) {
2822 do_warning_event(event, "%s: not enough memory!",
2823 __func__);
2824 goto out_free;
2827 arg->type = PRINT_TYPE;
2828 arg->typecast.type = arg->atom.atom;
2829 arg->typecast.item = item_arg;
2830 type = process_arg_token(event, item_arg, &token, type);
2834 *tok = token;
2835 return type;
2837 out_free:
2838 free_token(token);
2839 *tok = NULL;
2840 return EVENT_ERROR;
2844 static enum event_type
2845 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2846 char **tok)
2848 enum event_type type;
2849 char *token;
2851 if (read_expect_type(EVENT_ITEM, &token) < 0)
2852 goto out_free;
2854 arg->type = PRINT_STRING;
2855 arg->string.string = token;
2856 arg->string.offset = -1;
2858 if (read_expected(EVENT_DELIM, ")") < 0)
2859 goto out_err;
2861 type = read_token(&token);
2862 *tok = token;
2864 return type;
2866 out_free:
2867 free_token(token);
2868 out_err:
2869 *tok = NULL;
2870 return EVENT_ERROR;
2873 static enum event_type
2874 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2875 char **tok)
2877 enum event_type type;
2878 char *token;
2880 if (read_expect_type(EVENT_ITEM, &token) < 0)
2881 goto out_free;
2883 arg->type = PRINT_BITMASK;
2884 arg->bitmask.bitmask = token;
2885 arg->bitmask.offset = -1;
2887 if (read_expected(EVENT_DELIM, ")") < 0)
2888 goto out_err;
2890 type = read_token(&token);
2891 *tok = token;
2893 return type;
2895 out_free:
2896 free_token(token);
2897 out_err:
2898 *tok = NULL;
2899 return EVENT_ERROR;
2902 static struct pevent_function_handler *
2903 find_func_handler(struct pevent *pevent, char *func_name)
2905 struct pevent_function_handler *func;
2907 if (!pevent)
2908 return NULL;
2910 for (func = pevent->func_handlers; func; func = func->next) {
2911 if (strcmp(func->name, func_name) == 0)
2912 break;
2915 return func;
2918 static void remove_func_handler(struct pevent *pevent, char *func_name)
2920 struct pevent_function_handler *func;
2921 struct pevent_function_handler **next;
2923 next = &pevent->func_handlers;
2924 while ((func = *next)) {
2925 if (strcmp(func->name, func_name) == 0) {
2926 *next = func->next;
2927 free_func_handle(func);
2928 break;
2930 next = &func->next;
2934 static enum event_type
2935 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2936 struct print_arg *arg, char **tok)
2938 struct print_arg **next_arg;
2939 struct print_arg *farg;
2940 enum event_type type;
2941 char *token;
2942 int i;
2944 arg->type = PRINT_FUNC;
2945 arg->func.func = func;
2947 *tok = NULL;
2949 next_arg = &(arg->func.args);
2950 for (i = 0; i < func->nr_args; i++) {
2951 farg = alloc_arg();
2952 if (!farg) {
2953 do_warning_event(event, "%s: not enough memory!",
2954 __func__);
2955 return EVENT_ERROR;
2958 type = process_arg(event, farg, &token);
2959 if (i < (func->nr_args - 1)) {
2960 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2961 do_warning_event(event,
2962 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2963 func->name, func->nr_args,
2964 event->name, i + 1);
2965 goto err;
2967 } else {
2968 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2969 do_warning_event(event,
2970 "Error: function '%s()' only expects %d arguments but event %s has more",
2971 func->name, func->nr_args, event->name);
2972 goto err;
2976 *next_arg = farg;
2977 next_arg = &(farg->next);
2978 free_token(token);
2981 type = read_token(&token);
2982 *tok = token;
2984 return type;
2986 err:
2987 free_arg(farg);
2988 free_token(token);
2989 return EVENT_ERROR;
2992 static enum event_type
2993 process_function(struct event_format *event, struct print_arg *arg,
2994 char *token, char **tok)
2996 struct pevent_function_handler *func;
2998 if (strcmp(token, "__print_flags") == 0) {
2999 free_token(token);
3000 is_flag_field = 1;
3001 return process_flags(event, arg, tok);
3003 if (strcmp(token, "__print_symbolic") == 0) {
3004 free_token(token);
3005 is_symbolic_field = 1;
3006 return process_symbols(event, arg, tok);
3008 if (strcmp(token, "__print_hex") == 0) {
3009 free_token(token);
3010 return process_hex(event, arg, tok);
3012 if (strcmp(token, "__print_array") == 0) {
3013 free_token(token);
3014 return process_int_array(event, arg, tok);
3016 if (strcmp(token, "__get_str") == 0) {
3017 free_token(token);
3018 return process_str(event, arg, tok);
3020 if (strcmp(token, "__get_bitmask") == 0) {
3021 free_token(token);
3022 return process_bitmask(event, arg, tok);
3024 if (strcmp(token, "__get_dynamic_array") == 0) {
3025 free_token(token);
3026 return process_dynamic_array(event, arg, tok);
3028 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3029 free_token(token);
3030 return process_dynamic_array_len(event, arg, tok);
3033 func = find_func_handler(event->pevent, token);
3034 if (func) {
3035 free_token(token);
3036 return process_func_handler(event, func, arg, tok);
3039 do_warning_event(event, "function %s not defined", token);
3040 free_token(token);
3041 return EVENT_ERROR;
3044 static enum event_type
3045 process_arg_token(struct event_format *event, struct print_arg *arg,
3046 char **tok, enum event_type type)
3048 char *token;
3049 char *atom;
3051 token = *tok;
3053 switch (type) {
3054 case EVENT_ITEM:
3055 if (strcmp(token, "REC") == 0) {
3056 free_token(token);
3057 type = process_entry(event, arg, &token);
3058 break;
3060 atom = token;
3061 /* test the next token */
3062 type = read_token_item(&token);
3065 * If the next token is a parenthesis, then this
3066 * is a function.
3068 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3069 free_token(token);
3070 token = NULL;
3071 /* this will free atom. */
3072 type = process_function(event, arg, atom, &token);
3073 break;
3075 /* atoms can be more than one token long */
3076 while (type == EVENT_ITEM) {
3077 char *new_atom;
3078 new_atom = realloc(atom,
3079 strlen(atom) + strlen(token) + 2);
3080 if (!new_atom) {
3081 free(atom);
3082 *tok = NULL;
3083 free_token(token);
3084 return EVENT_ERROR;
3086 atom = new_atom;
3087 strcat(atom, " ");
3088 strcat(atom, token);
3089 free_token(token);
3090 type = read_token_item(&token);
3093 arg->type = PRINT_ATOM;
3094 arg->atom.atom = atom;
3095 break;
3097 case EVENT_DQUOTE:
3098 case EVENT_SQUOTE:
3099 arg->type = PRINT_ATOM;
3100 arg->atom.atom = token;
3101 type = read_token_item(&token);
3102 break;
3103 case EVENT_DELIM:
3104 if (strcmp(token, "(") == 0) {
3105 free_token(token);
3106 type = process_paren(event, arg, &token);
3107 break;
3109 case EVENT_OP:
3110 /* handle single ops */
3111 arg->type = PRINT_OP;
3112 arg->op.op = token;
3113 arg->op.left = NULL;
3114 type = process_op(event, arg, &token);
3116 /* On error, the op is freed */
3117 if (type == EVENT_ERROR)
3118 arg->op.op = NULL;
3120 /* return error type if errored */
3121 break;
3123 case EVENT_ERROR ... EVENT_NEWLINE:
3124 default:
3125 do_warning_event(event, "unexpected type %d", type);
3126 return EVENT_ERROR;
3128 *tok = token;
3130 return type;
3133 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3135 enum event_type type = EVENT_ERROR;
3136 struct print_arg *arg;
3137 char *token;
3138 int args = 0;
3140 do {
3141 if (type == EVENT_NEWLINE) {
3142 type = read_token_item(&token);
3143 continue;
3146 arg = alloc_arg();
3147 if (!arg) {
3148 do_warning_event(event, "%s: not enough memory!",
3149 __func__);
3150 return -1;
3153 type = process_arg(event, arg, &token);
3155 if (type == EVENT_ERROR) {
3156 free_token(token);
3157 free_arg(arg);
3158 return -1;
3161 *list = arg;
3162 args++;
3164 if (type == EVENT_OP) {
3165 type = process_op(event, arg, &token);
3166 free_token(token);
3167 if (type == EVENT_ERROR) {
3168 *list = NULL;
3169 free_arg(arg);
3170 return -1;
3172 list = &arg->next;
3173 continue;
3176 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3177 free_token(token);
3178 *list = arg;
3179 list = &arg->next;
3180 continue;
3182 break;
3183 } while (type != EVENT_NONE);
3185 if (type != EVENT_NONE && type != EVENT_ERROR)
3186 free_token(token);
3188 return args;
3191 static int event_read_print(struct event_format *event)
3193 enum event_type type;
3194 char *token;
3195 int ret;
3197 if (read_expected_item(EVENT_ITEM, "print") < 0)
3198 return -1;
3200 if (read_expected(EVENT_ITEM, "fmt") < 0)
3201 return -1;
3203 if (read_expected(EVENT_OP, ":") < 0)
3204 return -1;
3206 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3207 goto fail;
3209 concat:
3210 event->print_fmt.format = token;
3211 event->print_fmt.args = NULL;
3213 /* ok to have no arg */
3214 type = read_token_item(&token);
3216 if (type == EVENT_NONE)
3217 return 0;
3219 /* Handle concatenation of print lines */
3220 if (type == EVENT_DQUOTE) {
3221 char *cat;
3223 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3224 goto fail;
3225 free_token(token);
3226 free_token(event->print_fmt.format);
3227 event->print_fmt.format = NULL;
3228 token = cat;
3229 goto concat;
3232 if (test_type_token(type, token, EVENT_DELIM, ","))
3233 goto fail;
3235 free_token(token);
3237 ret = event_read_print_args(event, &event->print_fmt.args);
3238 if (ret < 0)
3239 return -1;
3241 return ret;
3243 fail:
3244 free_token(token);
3245 return -1;
3249 * pevent_find_common_field - return a common field by event
3250 * @event: handle for the event
3251 * @name: the name of the common field to return
3253 * Returns a common field from the event by the given @name.
3254 * This only searchs the common fields and not all field.
3256 struct format_field *
3257 pevent_find_common_field(struct event_format *event, const char *name)
3259 struct format_field *format;
3261 for (format = event->format.common_fields;
3262 format; format = format->next) {
3263 if (strcmp(format->name, name) == 0)
3264 break;
3267 return format;
3271 * pevent_find_field - find a non-common field
3272 * @event: handle for the event
3273 * @name: the name of the non-common field
3275 * Returns a non-common field by the given @name.
3276 * This does not search common fields.
3278 struct format_field *
3279 pevent_find_field(struct event_format *event, const char *name)
3281 struct format_field *format;
3283 for (format = event->format.fields;
3284 format; format = format->next) {
3285 if (strcmp(format->name, name) == 0)
3286 break;
3289 return format;
3293 * pevent_find_any_field - find any field by name
3294 * @event: handle for the event
3295 * @name: the name of the field
3297 * Returns a field by the given @name.
3298 * This searchs the common field names first, then
3299 * the non-common ones if a common one was not found.
3301 struct format_field *
3302 pevent_find_any_field(struct event_format *event, const char *name)
3304 struct format_field *format;
3306 format = pevent_find_common_field(event, name);
3307 if (format)
3308 return format;
3309 return pevent_find_field(event, name);
3313 * pevent_read_number - read a number from data
3314 * @pevent: handle for the pevent
3315 * @ptr: the raw data
3316 * @size: the size of the data that holds the number
3318 * Returns the number (converted to host) from the
3319 * raw data.
3321 unsigned long long pevent_read_number(struct pevent *pevent,
3322 const void *ptr, int size)
3324 switch (size) {
3325 case 1:
3326 return *(unsigned char *)ptr;
3327 case 2:
3328 return data2host2(pevent, ptr);
3329 case 4:
3330 return data2host4(pevent, ptr);
3331 case 8:
3332 return data2host8(pevent, ptr);
3333 default:
3334 /* BUG! */
3335 return 0;
3340 * pevent_read_number_field - read a number from data
3341 * @field: a handle to the field
3342 * @data: the raw data to read
3343 * @value: the value to place the number in
3345 * Reads raw data according to a field offset and size,
3346 * and translates it into @value.
3348 * Returns 0 on success, -1 otherwise.
3350 int pevent_read_number_field(struct format_field *field, const void *data,
3351 unsigned long long *value)
3353 if (!field)
3354 return -1;
3355 switch (field->size) {
3356 case 1:
3357 case 2:
3358 case 4:
3359 case 8:
3360 *value = pevent_read_number(field->event->pevent,
3361 data + field->offset, field->size);
3362 return 0;
3363 default:
3364 return -1;
3368 static int get_common_info(struct pevent *pevent,
3369 const char *type, int *offset, int *size)
3371 struct event_format *event;
3372 struct format_field *field;
3375 * All events should have the same common elements.
3376 * Pick any event to find where the type is;
3378 if (!pevent->events) {
3379 do_warning("no event_list!");
3380 return -1;
3383 event = pevent->events[0];
3384 field = pevent_find_common_field(event, type);
3385 if (!field)
3386 return -1;
3388 *offset = field->offset;
3389 *size = field->size;
3391 return 0;
3394 static int __parse_common(struct pevent *pevent, void *data,
3395 int *size, int *offset, const char *name)
3397 int ret;
3399 if (!*size) {
3400 ret = get_common_info(pevent, name, offset, size);
3401 if (ret < 0)
3402 return ret;
3404 return pevent_read_number(pevent, data + *offset, *size);
3407 static int trace_parse_common_type(struct pevent *pevent, void *data)
3409 return __parse_common(pevent, data,
3410 &pevent->type_size, &pevent->type_offset,
3411 "common_type");
3414 static int parse_common_pid(struct pevent *pevent, void *data)
3416 return __parse_common(pevent, data,
3417 &pevent->pid_size, &pevent->pid_offset,
3418 "common_pid");
3421 static int parse_common_pc(struct pevent *pevent, void *data)
3423 return __parse_common(pevent, data,
3424 &pevent->pc_size, &pevent->pc_offset,
3425 "common_preempt_count");
3428 static int parse_common_flags(struct pevent *pevent, void *data)
3430 return __parse_common(pevent, data,
3431 &pevent->flags_size, &pevent->flags_offset,
3432 "common_flags");
3435 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3437 return __parse_common(pevent, data,
3438 &pevent->ld_size, &pevent->ld_offset,
3439 "common_lock_depth");
3442 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3444 return __parse_common(pevent, data,
3445 &pevent->ld_size, &pevent->ld_offset,
3446 "common_migrate_disable");
3449 static int events_id_cmp(const void *a, const void *b);
3452 * pevent_find_event - find an event by given id
3453 * @pevent: a handle to the pevent
3454 * @id: the id of the event
3456 * Returns an event that has a given @id.
3458 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3460 struct event_format **eventptr;
3461 struct event_format key;
3462 struct event_format *pkey = &key;
3464 /* Check cache first */
3465 if (pevent->last_event && pevent->last_event->id == id)
3466 return pevent->last_event;
3468 key.id = id;
3470 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3471 sizeof(*pevent->events), events_id_cmp);
3473 if (eventptr) {
3474 pevent->last_event = *eventptr;
3475 return *eventptr;
3478 return NULL;
3482 * pevent_find_event_by_name - find an event by given name
3483 * @pevent: a handle to the pevent
3484 * @sys: the system name to search for
3485 * @name: the name of the event to search for
3487 * This returns an event with a given @name and under the system
3488 * @sys. If @sys is NULL the first event with @name is returned.
3490 struct event_format *
3491 pevent_find_event_by_name(struct pevent *pevent,
3492 const char *sys, const char *name)
3494 struct event_format *event;
3495 int i;
3497 if (pevent->last_event &&
3498 strcmp(pevent->last_event->name, name) == 0 &&
3499 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3500 return pevent->last_event;
3502 for (i = 0; i < pevent->nr_events; i++) {
3503 event = pevent->events[i];
3504 if (strcmp(event->name, name) == 0) {
3505 if (!sys)
3506 break;
3507 if (strcmp(event->system, sys) == 0)
3508 break;
3511 if (i == pevent->nr_events)
3512 event = NULL;
3514 pevent->last_event = event;
3515 return event;
3518 static unsigned long long
3519 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3521 struct pevent *pevent = event->pevent;
3522 unsigned long long val = 0;
3523 unsigned long long left, right;
3524 struct print_arg *typearg = NULL;
3525 struct print_arg *larg;
3526 unsigned long offset;
3527 unsigned int field_size;
3529 switch (arg->type) {
3530 case PRINT_NULL:
3531 /* ?? */
3532 return 0;
3533 case PRINT_ATOM:
3534 return strtoull(arg->atom.atom, NULL, 0);
3535 case PRINT_FIELD:
3536 if (!arg->field.field) {
3537 arg->field.field = pevent_find_any_field(event, arg->field.name);
3538 if (!arg->field.field)
3539 goto out_warning_field;
3542 /* must be a number */
3543 val = pevent_read_number(pevent, data + arg->field.field->offset,
3544 arg->field.field->size);
3545 break;
3546 case PRINT_FLAGS:
3547 case PRINT_SYMBOL:
3548 case PRINT_INT_ARRAY:
3549 case PRINT_HEX:
3550 break;
3551 case PRINT_TYPE:
3552 val = eval_num_arg(data, size, event, arg->typecast.item);
3553 return eval_type(val, arg, 0);
3554 case PRINT_STRING:
3555 case PRINT_BSTRING:
3556 case PRINT_BITMASK:
3557 return 0;
3558 case PRINT_FUNC: {
3559 struct trace_seq s;
3560 trace_seq_init(&s);
3561 val = process_defined_func(&s, data, size, event, arg);
3562 trace_seq_destroy(&s);
3563 return val;
3565 case PRINT_OP:
3566 if (strcmp(arg->op.op, "[") == 0) {
3568 * Arrays are special, since we don't want
3569 * to read the arg as is.
3571 right = eval_num_arg(data, size, event, arg->op.right);
3573 /* handle typecasts */
3574 larg = arg->op.left;
3575 while (larg->type == PRINT_TYPE) {
3576 if (!typearg)
3577 typearg = larg;
3578 larg = larg->typecast.item;
3581 /* Default to long size */
3582 field_size = pevent->long_size;
3584 switch (larg->type) {
3585 case PRINT_DYNAMIC_ARRAY:
3586 offset = pevent_read_number(pevent,
3587 data + larg->dynarray.field->offset,
3588 larg->dynarray.field->size);
3589 if (larg->dynarray.field->elementsize)
3590 field_size = larg->dynarray.field->elementsize;
3592 * The actual length of the dynamic array is stored
3593 * in the top half of the field, and the offset
3594 * is in the bottom half of the 32 bit field.
3596 offset &= 0xffff;
3597 offset += right;
3598 break;
3599 case PRINT_FIELD:
3600 if (!larg->field.field) {
3601 larg->field.field =
3602 pevent_find_any_field(event, larg->field.name);
3603 if (!larg->field.field) {
3604 arg = larg;
3605 goto out_warning_field;
3608 field_size = larg->field.field->elementsize;
3609 offset = larg->field.field->offset +
3610 right * larg->field.field->elementsize;
3611 break;
3612 default:
3613 goto default_op; /* oops, all bets off */
3615 val = pevent_read_number(pevent,
3616 data + offset, field_size);
3617 if (typearg)
3618 val = eval_type(val, typearg, 1);
3619 break;
3620 } else if (strcmp(arg->op.op, "?") == 0) {
3621 left = eval_num_arg(data, size, event, arg->op.left);
3622 arg = arg->op.right;
3623 if (left)
3624 val = eval_num_arg(data, size, event, arg->op.left);
3625 else
3626 val = eval_num_arg(data, size, event, arg->op.right);
3627 break;
3629 default_op:
3630 left = eval_num_arg(data, size, event, arg->op.left);
3631 right = eval_num_arg(data, size, event, arg->op.right);
3632 switch (arg->op.op[0]) {
3633 case '!':
3634 switch (arg->op.op[1]) {
3635 case 0:
3636 val = !right;
3637 break;
3638 case '=':
3639 val = left != right;
3640 break;
3641 default:
3642 goto out_warning_op;
3644 break;
3645 case '~':
3646 val = ~right;
3647 break;
3648 case '|':
3649 if (arg->op.op[1])
3650 val = left || right;
3651 else
3652 val = left | right;
3653 break;
3654 case '&':
3655 if (arg->op.op[1])
3656 val = left && right;
3657 else
3658 val = left & right;
3659 break;
3660 case '<':
3661 switch (arg->op.op[1]) {
3662 case 0:
3663 val = left < right;
3664 break;
3665 case '<':
3666 val = left << right;
3667 break;
3668 case '=':
3669 val = left <= right;
3670 break;
3671 default:
3672 goto out_warning_op;
3674 break;
3675 case '>':
3676 switch (arg->op.op[1]) {
3677 case 0:
3678 val = left > right;
3679 break;
3680 case '>':
3681 val = left >> right;
3682 break;
3683 case '=':
3684 val = left >= right;
3685 break;
3686 default:
3687 goto out_warning_op;
3689 break;
3690 case '=':
3691 if (arg->op.op[1] != '=')
3692 goto out_warning_op;
3694 val = left == right;
3695 break;
3696 case '-':
3697 val = left - right;
3698 break;
3699 case '+':
3700 val = left + right;
3701 break;
3702 case '/':
3703 val = left / right;
3704 break;
3705 case '%':
3706 val = left % right;
3707 break;
3708 case '*':
3709 val = left * right;
3710 break;
3711 default:
3712 goto out_warning_op;
3714 break;
3715 case PRINT_DYNAMIC_ARRAY_LEN:
3716 offset = pevent_read_number(pevent,
3717 data + arg->dynarray.field->offset,
3718 arg->dynarray.field->size);
3720 * The total allocated length of the dynamic array is
3721 * stored in the top half of the field, and the offset
3722 * is in the bottom half of the 32 bit field.
3724 val = (unsigned long long)(offset >> 16);
3725 break;
3726 case PRINT_DYNAMIC_ARRAY:
3727 /* Without [], we pass the address to the dynamic data */
3728 offset = pevent_read_number(pevent,
3729 data + arg->dynarray.field->offset,
3730 arg->dynarray.field->size);
3732 * The total allocated length of the dynamic array is
3733 * stored in the top half of the field, and the offset
3734 * is in the bottom half of the 32 bit field.
3736 offset &= 0xffff;
3737 val = (unsigned long long)((unsigned long)data + offset);
3738 break;
3739 default: /* not sure what to do there */
3740 return 0;
3742 return val;
3744 out_warning_op:
3745 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3746 return 0;
3748 out_warning_field:
3749 do_warning_event(event, "%s: field %s not found",
3750 __func__, arg->field.name);
3751 return 0;
3754 struct flag {
3755 const char *name;
3756 unsigned long long value;
3759 static const struct flag flags[] = {
3760 { "HI_SOFTIRQ", 0 },
3761 { "TIMER_SOFTIRQ", 1 },
3762 { "NET_TX_SOFTIRQ", 2 },
3763 { "NET_RX_SOFTIRQ", 3 },
3764 { "BLOCK_SOFTIRQ", 4 },
3765 { "IRQ_POLL_SOFTIRQ", 5 },
3766 { "TASKLET_SOFTIRQ", 6 },
3767 { "SCHED_SOFTIRQ", 7 },
3768 { "HRTIMER_SOFTIRQ", 8 },
3769 { "RCU_SOFTIRQ", 9 },
3771 { "HRTIMER_NORESTART", 0 },
3772 { "HRTIMER_RESTART", 1 },
3775 static long long eval_flag(const char *flag)
3777 int i;
3780 * Some flags in the format files do not get converted.
3781 * If the flag is not numeric, see if it is something that
3782 * we already know about.
3784 if (isdigit(flag[0]))
3785 return strtoull(flag, NULL, 0);
3787 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3788 if (strcmp(flags[i].name, flag) == 0)
3789 return flags[i].value;
3791 return -1LL;
3794 static void print_str_to_seq(struct trace_seq *s, const char *format,
3795 int len_arg, const char *str)
3797 if (len_arg >= 0)
3798 trace_seq_printf(s, format, len_arg, str);
3799 else
3800 trace_seq_printf(s, format, str);
3803 static void print_bitmask_to_seq(struct pevent *pevent,
3804 struct trace_seq *s, const char *format,
3805 int len_arg, const void *data, int size)
3807 int nr_bits = size * 8;
3808 int str_size = (nr_bits + 3) / 4;
3809 int len = 0;
3810 char buf[3];
3811 char *str;
3812 int index;
3813 int i;
3816 * The kernel likes to put in commas every 32 bits, we
3817 * can do the same.
3819 str_size += (nr_bits - 1) / 32;
3821 str = malloc(str_size + 1);
3822 if (!str) {
3823 do_warning("%s: not enough memory!", __func__);
3824 return;
3826 str[str_size] = 0;
3828 /* Start out with -2 for the two chars per byte */
3829 for (i = str_size - 2; i >= 0; i -= 2) {
3831 * data points to a bit mask of size bytes.
3832 * In the kernel, this is an array of long words, thus
3833 * endianess is very important.
3835 if (pevent->file_bigendian)
3836 index = size - (len + 1);
3837 else
3838 index = len;
3840 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3841 memcpy(str + i, buf, 2);
3842 len++;
3843 if (!(len & 3) && i > 0) {
3844 i--;
3845 str[i] = ',';
3849 if (len_arg >= 0)
3850 trace_seq_printf(s, format, len_arg, str);
3851 else
3852 trace_seq_printf(s, format, str);
3854 free(str);
3857 static void print_str_arg(struct trace_seq *s, void *data, int size,
3858 struct event_format *event, const char *format,
3859 int len_arg, struct print_arg *arg)
3861 struct pevent *pevent = event->pevent;
3862 struct print_flag_sym *flag;
3863 struct format_field *field;
3864 struct printk_map *printk;
3865 long long val, fval;
3866 unsigned long long addr;
3867 char *str;
3868 unsigned char *hex;
3869 int print;
3870 int i, len;
3872 switch (arg->type) {
3873 case PRINT_NULL:
3874 /* ?? */
3875 return;
3876 case PRINT_ATOM:
3877 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3878 return;
3879 case PRINT_FIELD:
3880 field = arg->field.field;
3881 if (!field) {
3882 field = pevent_find_any_field(event, arg->field.name);
3883 if (!field) {
3884 str = arg->field.name;
3885 goto out_warning_field;
3887 arg->field.field = field;
3889 /* Zero sized fields, mean the rest of the data */
3890 len = field->size ? : size - field->offset;
3893 * Some events pass in pointers. If this is not an array
3894 * and the size is the same as long_size, assume that it
3895 * is a pointer.
3897 if (!(field->flags & FIELD_IS_ARRAY) &&
3898 field->size == pevent->long_size) {
3900 /* Handle heterogeneous recording and processing
3901 * architectures
3903 * CASE I:
3904 * Traces recorded on 32-bit devices (32-bit
3905 * addressing) and processed on 64-bit devices:
3906 * In this case, only 32 bits should be read.
3908 * CASE II:
3909 * Traces recorded on 64 bit devices and processed
3910 * on 32-bit devices:
3911 * In this case, 64 bits must be read.
3913 addr = (pevent->long_size == 8) ?
3914 *(unsigned long long *)(data + field->offset) :
3915 (unsigned long long)*(unsigned int *)(data + field->offset);
3917 /* Check if it matches a print format */
3918 printk = find_printk(pevent, addr);
3919 if (printk)
3920 trace_seq_puts(s, printk->printk);
3921 else
3922 trace_seq_printf(s, "%llx", addr);
3923 break;
3925 str = malloc(len + 1);
3926 if (!str) {
3927 do_warning_event(event, "%s: not enough memory!",
3928 __func__);
3929 return;
3931 memcpy(str, data + field->offset, len);
3932 str[len] = 0;
3933 print_str_to_seq(s, format, len_arg, str);
3934 free(str);
3935 break;
3936 case PRINT_FLAGS:
3937 val = eval_num_arg(data, size, event, arg->flags.field);
3938 print = 0;
3939 for (flag = arg->flags.flags; flag; flag = flag->next) {
3940 fval = eval_flag(flag->value);
3941 if (!val && fval < 0) {
3942 print_str_to_seq(s, format, len_arg, flag->str);
3943 break;
3945 if (fval > 0 && (val & fval) == fval) {
3946 if (print && arg->flags.delim)
3947 trace_seq_puts(s, arg->flags.delim);
3948 print_str_to_seq(s, format, len_arg, flag->str);
3949 print = 1;
3950 val &= ~fval;
3953 break;
3954 case PRINT_SYMBOL:
3955 val = eval_num_arg(data, size, event, arg->symbol.field);
3956 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3957 fval = eval_flag(flag->value);
3958 if (val == fval) {
3959 print_str_to_seq(s, format, len_arg, flag->str);
3960 break;
3963 break;
3964 case PRINT_HEX:
3965 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3966 unsigned long offset;
3967 offset = pevent_read_number(pevent,
3968 data + arg->hex.field->dynarray.field->offset,
3969 arg->hex.field->dynarray.field->size);
3970 hex = data + (offset & 0xffff);
3971 } else {
3972 field = arg->hex.field->field.field;
3973 if (!field) {
3974 str = arg->hex.field->field.name;
3975 field = pevent_find_any_field(event, str);
3976 if (!field)
3977 goto out_warning_field;
3978 arg->hex.field->field.field = field;
3980 hex = data + field->offset;
3982 len = eval_num_arg(data, size, event, arg->hex.size);
3983 for (i = 0; i < len; i++) {
3984 if (i)
3985 trace_seq_putc(s, ' ');
3986 trace_seq_printf(s, "%02x", hex[i]);
3988 break;
3990 case PRINT_INT_ARRAY: {
3991 void *num;
3992 int el_size;
3994 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3995 unsigned long offset;
3996 struct format_field *field =
3997 arg->int_array.field->dynarray.field;
3998 offset = pevent_read_number(pevent,
3999 data + field->offset,
4000 field->size);
4001 num = data + (offset & 0xffff);
4002 } else {
4003 field = arg->int_array.field->field.field;
4004 if (!field) {
4005 str = arg->int_array.field->field.name;
4006 field = pevent_find_any_field(event, str);
4007 if (!field)
4008 goto out_warning_field;
4009 arg->int_array.field->field.field = field;
4011 num = data + field->offset;
4013 len = eval_num_arg(data, size, event, arg->int_array.count);
4014 el_size = eval_num_arg(data, size, event,
4015 arg->int_array.el_size);
4016 for (i = 0; i < len; i++) {
4017 if (i)
4018 trace_seq_putc(s, ' ');
4020 if (el_size == 1) {
4021 trace_seq_printf(s, "%u", *(uint8_t *)num);
4022 } else if (el_size == 2) {
4023 trace_seq_printf(s, "%u", *(uint16_t *)num);
4024 } else if (el_size == 4) {
4025 trace_seq_printf(s, "%u", *(uint32_t *)num);
4026 } else if (el_size == 8) {
4027 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4028 } else {
4029 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4030 el_size, *(uint8_t *)num);
4031 el_size = 1;
4034 num += el_size;
4036 break;
4038 case PRINT_TYPE:
4039 break;
4040 case PRINT_STRING: {
4041 int str_offset;
4043 if (arg->string.offset == -1) {
4044 struct format_field *f;
4046 f = pevent_find_any_field(event, arg->string.string);
4047 arg->string.offset = f->offset;
4049 str_offset = data2host4(pevent, data + arg->string.offset);
4050 str_offset &= 0xffff;
4051 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4052 break;
4054 case PRINT_BSTRING:
4055 print_str_to_seq(s, format, len_arg, arg->string.string);
4056 break;
4057 case PRINT_BITMASK: {
4058 int bitmask_offset;
4059 int bitmask_size;
4061 if (arg->bitmask.offset == -1) {
4062 struct format_field *f;
4064 f = pevent_find_any_field(event, arg->bitmask.bitmask);
4065 arg->bitmask.offset = f->offset;
4067 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4068 bitmask_size = bitmask_offset >> 16;
4069 bitmask_offset &= 0xffff;
4070 print_bitmask_to_seq(pevent, s, format, len_arg,
4071 data + bitmask_offset, bitmask_size);
4072 break;
4074 case PRINT_OP:
4076 * The only op for string should be ? :
4078 if (arg->op.op[0] != '?')
4079 return;
4080 val = eval_num_arg(data, size, event, arg->op.left);
4081 if (val)
4082 print_str_arg(s, data, size, event,
4083 format, len_arg, arg->op.right->op.left);
4084 else
4085 print_str_arg(s, data, size, event,
4086 format, len_arg, arg->op.right->op.right);
4087 break;
4088 case PRINT_FUNC:
4089 process_defined_func(s, data, size, event, arg);
4090 break;
4091 default:
4092 /* well... */
4093 break;
4096 return;
4098 out_warning_field:
4099 do_warning_event(event, "%s: field %s not found",
4100 __func__, arg->field.name);
4103 static unsigned long long
4104 process_defined_func(struct trace_seq *s, void *data, int size,
4105 struct event_format *event, struct print_arg *arg)
4107 struct pevent_function_handler *func_handle = arg->func.func;
4108 struct pevent_func_params *param;
4109 unsigned long long *args;
4110 unsigned long long ret;
4111 struct print_arg *farg;
4112 struct trace_seq str;
4113 struct save_str {
4114 struct save_str *next;
4115 char *str;
4116 } *strings = NULL, *string;
4117 int i;
4119 if (!func_handle->nr_args) {
4120 ret = (*func_handle->func)(s, NULL);
4121 goto out;
4124 farg = arg->func.args;
4125 param = func_handle->params;
4127 ret = ULLONG_MAX;
4128 args = malloc(sizeof(*args) * func_handle->nr_args);
4129 if (!args)
4130 goto out;
4132 for (i = 0; i < func_handle->nr_args; i++) {
4133 switch (param->type) {
4134 case PEVENT_FUNC_ARG_INT:
4135 case PEVENT_FUNC_ARG_LONG:
4136 case PEVENT_FUNC_ARG_PTR:
4137 args[i] = eval_num_arg(data, size, event, farg);
4138 break;
4139 case PEVENT_FUNC_ARG_STRING:
4140 trace_seq_init(&str);
4141 print_str_arg(&str, data, size, event, "%s", -1, farg);
4142 trace_seq_terminate(&str);
4143 string = malloc(sizeof(*string));
4144 if (!string) {
4145 do_warning_event(event, "%s(%d): malloc str",
4146 __func__, __LINE__);
4147 goto out_free;
4149 string->next = strings;
4150 string->str = strdup(str.buffer);
4151 if (!string->str) {
4152 free(string);
4153 do_warning_event(event, "%s(%d): malloc str",
4154 __func__, __LINE__);
4155 goto out_free;
4157 args[i] = (uintptr_t)string->str;
4158 strings = string;
4159 trace_seq_destroy(&str);
4160 break;
4161 default:
4163 * Something went totally wrong, this is not
4164 * an input error, something in this code broke.
4166 do_warning_event(event, "Unexpected end of arguments\n");
4167 goto out_free;
4169 farg = farg->next;
4170 param = param->next;
4173 ret = (*func_handle->func)(s, args);
4174 out_free:
4175 free(args);
4176 while (strings) {
4177 string = strings;
4178 strings = string->next;
4179 free(string->str);
4180 free(string);
4183 out:
4184 /* TBD : handle return type here */
4185 return ret;
4188 static void free_args(struct print_arg *args)
4190 struct print_arg *next;
4192 while (args) {
4193 next = args->next;
4195 free_arg(args);
4196 args = next;
4200 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4202 struct pevent *pevent = event->pevent;
4203 struct format_field *field, *ip_field;
4204 struct print_arg *args, *arg, **next;
4205 unsigned long long ip, val;
4206 char *ptr;
4207 void *bptr;
4208 int vsize;
4210 field = pevent->bprint_buf_field;
4211 ip_field = pevent->bprint_ip_field;
4213 if (!field) {
4214 field = pevent_find_field(event, "buf");
4215 if (!field) {
4216 do_warning_event(event, "can't find buffer field for binary printk");
4217 return NULL;
4219 ip_field = pevent_find_field(event, "ip");
4220 if (!ip_field) {
4221 do_warning_event(event, "can't find ip field for binary printk");
4222 return NULL;
4224 pevent->bprint_buf_field = field;
4225 pevent->bprint_ip_field = ip_field;
4228 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4231 * The first arg is the IP pointer.
4233 args = alloc_arg();
4234 if (!args) {
4235 do_warning_event(event, "%s(%d): not enough memory!",
4236 __func__, __LINE__);
4237 return NULL;
4239 arg = args;
4240 arg->next = NULL;
4241 next = &arg->next;
4243 arg->type = PRINT_ATOM;
4245 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4246 goto out_free;
4248 /* skip the first "%ps: " */
4249 for (ptr = fmt + 5, bptr = data + field->offset;
4250 bptr < data + size && *ptr; ptr++) {
4251 int ls = 0;
4253 if (*ptr == '%') {
4254 process_again:
4255 ptr++;
4256 switch (*ptr) {
4257 case '%':
4258 break;
4259 case 'l':
4260 ls++;
4261 goto process_again;
4262 case 'L':
4263 ls = 2;
4264 goto process_again;
4265 case '0' ... '9':
4266 goto process_again;
4267 case '.':
4268 goto process_again;
4269 case 'z':
4270 case 'Z':
4271 ls = 1;
4272 goto process_again;
4273 case 'p':
4274 ls = 1;
4275 /* fall through */
4276 case 'd':
4277 case 'u':
4278 case 'x':
4279 case 'i':
4280 switch (ls) {
4281 case 0:
4282 vsize = 4;
4283 break;
4284 case 1:
4285 vsize = pevent->long_size;
4286 break;
4287 case 2:
4288 vsize = 8;
4289 break;
4290 default:
4291 vsize = ls; /* ? */
4292 break;
4294 /* fall through */
4295 case '*':
4296 if (*ptr == '*')
4297 vsize = 4;
4299 /* the pointers are always 4 bytes aligned */
4300 bptr = (void *)(((unsigned long)bptr + 3) &
4301 ~3);
4302 val = pevent_read_number(pevent, bptr, vsize);
4303 bptr += vsize;
4304 arg = alloc_arg();
4305 if (!arg) {
4306 do_warning_event(event, "%s(%d): not enough memory!",
4307 __func__, __LINE__);
4308 goto out_free;
4310 arg->next = NULL;
4311 arg->type = PRINT_ATOM;
4312 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4313 free(arg);
4314 goto out_free;
4316 *next = arg;
4317 next = &arg->next;
4319 * The '*' case means that an arg is used as the length.
4320 * We need to continue to figure out for what.
4322 if (*ptr == '*')
4323 goto process_again;
4325 break;
4326 case 's':
4327 arg = alloc_arg();
4328 if (!arg) {
4329 do_warning_event(event, "%s(%d): not enough memory!",
4330 __func__, __LINE__);
4331 goto out_free;
4333 arg->next = NULL;
4334 arg->type = PRINT_BSTRING;
4335 arg->string.string = strdup(bptr);
4336 if (!arg->string.string)
4337 goto out_free;
4338 bptr += strlen(bptr) + 1;
4339 *next = arg;
4340 next = &arg->next;
4341 default:
4342 break;
4347 return args;
4349 out_free:
4350 free_args(args);
4351 return NULL;
4354 static char *
4355 get_bprint_format(void *data, int size __maybe_unused,
4356 struct event_format *event)
4358 struct pevent *pevent = event->pevent;
4359 unsigned long long addr;
4360 struct format_field *field;
4361 struct printk_map *printk;
4362 char *format;
4364 field = pevent->bprint_fmt_field;
4366 if (!field) {
4367 field = pevent_find_field(event, "fmt");
4368 if (!field) {
4369 do_warning_event(event, "can't find format field for binary printk");
4370 return NULL;
4372 pevent->bprint_fmt_field = field;
4375 addr = pevent_read_number(pevent, data + field->offset, field->size);
4377 printk = find_printk(pevent, addr);
4378 if (!printk) {
4379 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4380 return NULL;
4381 return format;
4384 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4385 return NULL;
4387 return format;
4390 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4391 struct event_format *event, struct print_arg *arg)
4393 unsigned char *buf;
4394 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4396 if (arg->type == PRINT_FUNC) {
4397 process_defined_func(s, data, size, event, arg);
4398 return;
4401 if (arg->type != PRINT_FIELD) {
4402 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4403 arg->type);
4404 return;
4407 if (mac == 'm')
4408 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4409 if (!arg->field.field) {
4410 arg->field.field =
4411 pevent_find_any_field(event, arg->field.name);
4412 if (!arg->field.field) {
4413 do_warning_event(event, "%s: field %s not found",
4414 __func__, arg->field.name);
4415 return;
4418 if (arg->field.field->size != 6) {
4419 trace_seq_printf(s, "INVALIDMAC");
4420 return;
4422 buf = data + arg->field.field->offset;
4423 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4426 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4428 const char *fmt;
4430 if (i == 'i')
4431 fmt = "%03d.%03d.%03d.%03d";
4432 else
4433 fmt = "%d.%d.%d.%d";
4435 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4438 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4440 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4441 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4444 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4446 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4449 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4451 int i, j, range;
4452 unsigned char zerolength[8];
4453 int longest = 1;
4454 int colonpos = -1;
4455 uint16_t word;
4456 uint8_t hi, lo;
4457 bool needcolon = false;
4458 bool useIPv4;
4459 struct in6_addr in6;
4461 memcpy(&in6, addr, sizeof(struct in6_addr));
4463 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4465 memset(zerolength, 0, sizeof(zerolength));
4467 if (useIPv4)
4468 range = 6;
4469 else
4470 range = 8;
4472 /* find position of longest 0 run */
4473 for (i = 0; i < range; i++) {
4474 for (j = i; j < range; j++) {
4475 if (in6.s6_addr16[j] != 0)
4476 break;
4477 zerolength[i]++;
4480 for (i = 0; i < range; i++) {
4481 if (zerolength[i] > longest) {
4482 longest = zerolength[i];
4483 colonpos = i;
4486 if (longest == 1) /* don't compress a single 0 */
4487 colonpos = -1;
4489 /* emit address */
4490 for (i = 0; i < range; i++) {
4491 if (i == colonpos) {
4492 if (needcolon || i == 0)
4493 trace_seq_printf(s, ":");
4494 trace_seq_printf(s, ":");
4495 needcolon = false;
4496 i += longest - 1;
4497 continue;
4499 if (needcolon) {
4500 trace_seq_printf(s, ":");
4501 needcolon = false;
4503 /* hex u16 without leading 0s */
4504 word = ntohs(in6.s6_addr16[i]);
4505 hi = word >> 8;
4506 lo = word & 0xff;
4507 if (hi)
4508 trace_seq_printf(s, "%x%02x", hi, lo);
4509 else
4510 trace_seq_printf(s, "%x", lo);
4512 needcolon = true;
4515 if (useIPv4) {
4516 if (needcolon)
4517 trace_seq_printf(s, ":");
4518 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4521 return;
4524 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4526 int j;
4528 for (j = 0; j < 16; j += 2) {
4529 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4530 if (i == 'I' && j < 14)
4531 trace_seq_printf(s, ":");
4536 * %pi4 print an IPv4 address with leading zeros
4537 * %pI4 print an IPv4 address without leading zeros
4538 * %pi6 print an IPv6 address without colons
4539 * %pI6 print an IPv6 address with colons
4540 * %pI6c print an IPv6 address in compressed form with colons
4541 * %pISpc print an IP address based on sockaddr; p adds port.
4543 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4544 void *data, int size, struct event_format *event,
4545 struct print_arg *arg)
4547 unsigned char *buf;
4549 if (arg->type == PRINT_FUNC) {
4550 process_defined_func(s, data, size, event, arg);
4551 return 0;
4554 if (arg->type != PRINT_FIELD) {
4555 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4556 return 0;
4559 if (!arg->field.field) {
4560 arg->field.field =
4561 pevent_find_any_field(event, arg->field.name);
4562 if (!arg->field.field) {
4563 do_warning("%s: field %s not found",
4564 __func__, arg->field.name);
4565 return 0;
4569 buf = data + arg->field.field->offset;
4571 if (arg->field.field->size != 4) {
4572 trace_seq_printf(s, "INVALIDIPv4");
4573 return 0;
4575 print_ip4_addr(s, i, buf);
4577 return 0;
4580 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4581 void *data, int size, struct event_format *event,
4582 struct print_arg *arg)
4584 char have_c = 0;
4585 unsigned char *buf;
4586 int rc = 0;
4588 /* pI6c */
4589 if (i == 'I' && *ptr == 'c') {
4590 have_c = 1;
4591 ptr++;
4592 rc++;
4595 if (arg->type == PRINT_FUNC) {
4596 process_defined_func(s, data, size, event, arg);
4597 return rc;
4600 if (arg->type != PRINT_FIELD) {
4601 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4602 return rc;
4605 if (!arg->field.field) {
4606 arg->field.field =
4607 pevent_find_any_field(event, arg->field.name);
4608 if (!arg->field.field) {
4609 do_warning("%s: field %s not found",
4610 __func__, arg->field.name);
4611 return rc;
4615 buf = data + arg->field.field->offset;
4617 if (arg->field.field->size != 16) {
4618 trace_seq_printf(s, "INVALIDIPv6");
4619 return rc;
4622 if (have_c)
4623 print_ip6c_addr(s, buf);
4624 else
4625 print_ip6_addr(s, i, buf);
4627 return rc;
4630 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4631 void *data, int size, struct event_format *event,
4632 struct print_arg *arg)
4634 char have_c = 0, have_p = 0;
4635 unsigned char *buf;
4636 struct sockaddr_storage *sa;
4637 int rc = 0;
4639 /* pISpc */
4640 if (i == 'I') {
4641 if (*ptr == 'p') {
4642 have_p = 1;
4643 ptr++;
4644 rc++;
4646 if (*ptr == 'c') {
4647 have_c = 1;
4648 ptr++;
4649 rc++;
4653 if (arg->type == PRINT_FUNC) {
4654 process_defined_func(s, data, size, event, arg);
4655 return rc;
4658 if (arg->type != PRINT_FIELD) {
4659 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4660 return rc;
4663 if (!arg->field.field) {
4664 arg->field.field =
4665 pevent_find_any_field(event, arg->field.name);
4666 if (!arg->field.field) {
4667 do_warning("%s: field %s not found",
4668 __func__, arg->field.name);
4669 return rc;
4673 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4675 if (sa->ss_family == AF_INET) {
4676 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4678 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4679 trace_seq_printf(s, "INVALIDIPv4");
4680 return rc;
4683 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4684 if (have_p)
4685 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4688 } else if (sa->ss_family == AF_INET6) {
4689 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4691 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4692 trace_seq_printf(s, "INVALIDIPv6");
4693 return rc;
4696 if (have_p)
4697 trace_seq_printf(s, "[");
4699 buf = (unsigned char *) &sa6->sin6_addr;
4700 if (have_c)
4701 print_ip6c_addr(s, buf);
4702 else
4703 print_ip6_addr(s, i, buf);
4705 if (have_p)
4706 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4709 return rc;
4712 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4713 void *data, int size, struct event_format *event,
4714 struct print_arg *arg)
4716 char i = *ptr; /* 'i' or 'I' */
4717 char ver;
4718 int rc = 0;
4720 ptr++;
4721 rc++;
4723 ver = *ptr;
4724 ptr++;
4725 rc++;
4727 switch (ver) {
4728 case '4':
4729 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4730 break;
4731 case '6':
4732 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4733 break;
4734 case 'S':
4735 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4736 break;
4737 default:
4738 return 0;
4741 return rc;
4744 static int is_printable_array(char *p, unsigned int len)
4746 unsigned int i;
4748 for (i = 0; i < len && p[i]; i++)
4749 if (!isprint(p[i]) && !isspace(p[i]))
4750 return 0;
4751 return 1;
4754 void pevent_print_field(struct trace_seq *s, void *data,
4755 struct format_field *field)
4757 unsigned long long val;
4758 unsigned int offset, len, i;
4759 struct pevent *pevent = field->event->pevent;
4761 if (field->flags & FIELD_IS_ARRAY) {
4762 offset = field->offset;
4763 len = field->size;
4764 if (field->flags & FIELD_IS_DYNAMIC) {
4765 val = pevent_read_number(pevent, data + offset, len);
4766 offset = val;
4767 len = offset >> 16;
4768 offset &= 0xffff;
4770 if (field->flags & FIELD_IS_STRING &&
4771 is_printable_array(data + offset, len)) {
4772 trace_seq_printf(s, "%s", (char *)data + offset);
4773 } else {
4774 trace_seq_puts(s, "ARRAY[");
4775 for (i = 0; i < len; i++) {
4776 if (i)
4777 trace_seq_puts(s, ", ");
4778 trace_seq_printf(s, "%02x",
4779 *((unsigned char *)data + offset + i));
4781 trace_seq_putc(s, ']');
4782 field->flags &= ~FIELD_IS_STRING;
4784 } else {
4785 val = pevent_read_number(pevent, data + field->offset,
4786 field->size);
4787 if (field->flags & FIELD_IS_POINTER) {
4788 trace_seq_printf(s, "0x%llx", val);
4789 } else if (field->flags & FIELD_IS_SIGNED) {
4790 switch (field->size) {
4791 case 4:
4793 * If field is long then print it in hex.
4794 * A long usually stores pointers.
4796 if (field->flags & FIELD_IS_LONG)
4797 trace_seq_printf(s, "0x%x", (int)val);
4798 else
4799 trace_seq_printf(s, "%d", (int)val);
4800 break;
4801 case 2:
4802 trace_seq_printf(s, "%2d", (short)val);
4803 break;
4804 case 1:
4805 trace_seq_printf(s, "%1d", (char)val);
4806 break;
4807 default:
4808 trace_seq_printf(s, "%lld", val);
4810 } else {
4811 if (field->flags & FIELD_IS_LONG)
4812 trace_seq_printf(s, "0x%llx", val);
4813 else
4814 trace_seq_printf(s, "%llu", val);
4819 void pevent_print_fields(struct trace_seq *s, void *data,
4820 int size __maybe_unused, struct event_format *event)
4822 struct format_field *field;
4824 field = event->format.fields;
4825 while (field) {
4826 trace_seq_printf(s, " %s=", field->name);
4827 pevent_print_field(s, data, field);
4828 field = field->next;
4832 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4834 struct pevent *pevent = event->pevent;
4835 struct print_fmt *print_fmt = &event->print_fmt;
4836 struct print_arg *arg = print_fmt->args;
4837 struct print_arg *args = NULL;
4838 const char *ptr = print_fmt->format;
4839 unsigned long long val;
4840 struct func_map *func;
4841 const char *saveptr;
4842 struct trace_seq p;
4843 char *bprint_fmt = NULL;
4844 char format[32];
4845 int show_func;
4846 int len_as_arg;
4847 int len_arg;
4848 int len;
4849 int ls;
4851 if (event->flags & EVENT_FL_FAILED) {
4852 trace_seq_printf(s, "[FAILED TO PARSE]");
4853 pevent_print_fields(s, data, size, event);
4854 return;
4857 if (event->flags & EVENT_FL_ISBPRINT) {
4858 bprint_fmt = get_bprint_format(data, size, event);
4859 args = make_bprint_args(bprint_fmt, data, size, event);
4860 arg = args;
4861 ptr = bprint_fmt;
4864 for (; *ptr; ptr++) {
4865 ls = 0;
4866 if (*ptr == '\\') {
4867 ptr++;
4868 switch (*ptr) {
4869 case 'n':
4870 trace_seq_putc(s, '\n');
4871 break;
4872 case 't':
4873 trace_seq_putc(s, '\t');
4874 break;
4875 case 'r':
4876 trace_seq_putc(s, '\r');
4877 break;
4878 case '\\':
4879 trace_seq_putc(s, '\\');
4880 break;
4881 default:
4882 trace_seq_putc(s, *ptr);
4883 break;
4886 } else if (*ptr == '%') {
4887 saveptr = ptr;
4888 show_func = 0;
4889 len_as_arg = 0;
4890 cont_process:
4891 ptr++;
4892 switch (*ptr) {
4893 case '%':
4894 trace_seq_putc(s, '%');
4895 break;
4896 case '#':
4897 /* FIXME: need to handle properly */
4898 goto cont_process;
4899 case 'h':
4900 ls--;
4901 goto cont_process;
4902 case 'l':
4903 ls++;
4904 goto cont_process;
4905 case 'L':
4906 ls = 2;
4907 goto cont_process;
4908 case '*':
4909 /* The argument is the length. */
4910 if (!arg) {
4911 do_warning_event(event, "no argument match");
4912 event->flags |= EVENT_FL_FAILED;
4913 goto out_failed;
4915 len_arg = eval_num_arg(data, size, event, arg);
4916 len_as_arg = 1;
4917 arg = arg->next;
4918 goto cont_process;
4919 case '.':
4920 case 'z':
4921 case 'Z':
4922 case '0' ... '9':
4923 case '-':
4924 goto cont_process;
4925 case 'p':
4926 if (pevent->long_size == 4)
4927 ls = 1;
4928 else
4929 ls = 2;
4931 if (*(ptr+1) == 'F' || *(ptr+1) == 'f' ||
4932 *(ptr+1) == 'S' || *(ptr+1) == 's') {
4933 ptr++;
4934 show_func = *ptr;
4935 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4936 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4937 ptr++;
4938 arg = arg->next;
4939 break;
4940 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4941 int n;
4943 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4944 if (n > 0) {
4945 ptr += n;
4946 arg = arg->next;
4947 break;
4951 /* fall through */
4952 case 'd':
4953 case 'i':
4954 case 'x':
4955 case 'X':
4956 case 'u':
4957 if (!arg) {
4958 do_warning_event(event, "no argument match");
4959 event->flags |= EVENT_FL_FAILED;
4960 goto out_failed;
4963 len = ((unsigned long)ptr + 1) -
4964 (unsigned long)saveptr;
4966 /* should never happen */
4967 if (len > 31) {
4968 do_warning_event(event, "bad format!");
4969 event->flags |= EVENT_FL_FAILED;
4970 len = 31;
4973 memcpy(format, saveptr, len);
4974 format[len] = 0;
4976 val = eval_num_arg(data, size, event, arg);
4977 arg = arg->next;
4979 if (show_func) {
4980 func = find_func(pevent, val);
4981 if (func) {
4982 trace_seq_puts(s, func->func);
4983 if (show_func == 'F')
4984 trace_seq_printf(s,
4985 "+0x%llx",
4986 val - func->addr);
4987 break;
4990 if (pevent->long_size == 8 && ls == 1 &&
4991 sizeof(long) != 8) {
4992 char *p;
4994 /* make %l into %ll */
4995 if (ls == 1 && (p = strchr(format, 'l')))
4996 memmove(p+1, p, strlen(p)+1);
4997 else if (strcmp(format, "%p") == 0)
4998 strcpy(format, "0x%llx");
4999 ls = 2;
5001 switch (ls) {
5002 case -2:
5003 if (len_as_arg)
5004 trace_seq_printf(s, format, len_arg, (char)val);
5005 else
5006 trace_seq_printf(s, format, (char)val);
5007 break;
5008 case -1:
5009 if (len_as_arg)
5010 trace_seq_printf(s, format, len_arg, (short)val);
5011 else
5012 trace_seq_printf(s, format, (short)val);
5013 break;
5014 case 0:
5015 if (len_as_arg)
5016 trace_seq_printf(s, format, len_arg, (int)val);
5017 else
5018 trace_seq_printf(s, format, (int)val);
5019 break;
5020 case 1:
5021 if (len_as_arg)
5022 trace_seq_printf(s, format, len_arg, (long)val);
5023 else
5024 trace_seq_printf(s, format, (long)val);
5025 break;
5026 case 2:
5027 if (len_as_arg)
5028 trace_seq_printf(s, format, len_arg,
5029 (long long)val);
5030 else
5031 trace_seq_printf(s, format, (long long)val);
5032 break;
5033 default:
5034 do_warning_event(event, "bad count (%d)", ls);
5035 event->flags |= EVENT_FL_FAILED;
5037 break;
5038 case 's':
5039 if (!arg) {
5040 do_warning_event(event, "no matching argument");
5041 event->flags |= EVENT_FL_FAILED;
5042 goto out_failed;
5045 len = ((unsigned long)ptr + 1) -
5046 (unsigned long)saveptr;
5048 /* should never happen */
5049 if (len > 31) {
5050 do_warning_event(event, "bad format!");
5051 event->flags |= EVENT_FL_FAILED;
5052 len = 31;
5055 memcpy(format, saveptr, len);
5056 format[len] = 0;
5057 if (!len_as_arg)
5058 len_arg = -1;
5059 /* Use helper trace_seq */
5060 trace_seq_init(&p);
5061 print_str_arg(&p, data, size, event,
5062 format, len_arg, arg);
5063 trace_seq_terminate(&p);
5064 trace_seq_puts(s, p.buffer);
5065 trace_seq_destroy(&p);
5066 arg = arg->next;
5067 break;
5068 default:
5069 trace_seq_printf(s, ">%c<", *ptr);
5072 } else
5073 trace_seq_putc(s, *ptr);
5076 if (event->flags & EVENT_FL_FAILED) {
5077 out_failed:
5078 trace_seq_printf(s, "[FAILED TO PARSE]");
5081 if (args) {
5082 free_args(args);
5083 free(bprint_fmt);
5088 * pevent_data_lat_fmt - parse the data for the latency format
5089 * @pevent: a handle to the pevent
5090 * @s: the trace_seq to write to
5091 * @record: the record to read from
5093 * This parses out the Latency format (interrupts disabled,
5094 * need rescheduling, in hard/soft interrupt, preempt count
5095 * and lock depth) and places it into the trace_seq.
5097 void pevent_data_lat_fmt(struct pevent *pevent,
5098 struct trace_seq *s, struct pevent_record *record)
5100 static int check_lock_depth = 1;
5101 static int check_migrate_disable = 1;
5102 static int lock_depth_exists;
5103 static int migrate_disable_exists;
5104 unsigned int lat_flags;
5105 unsigned int pc;
5106 int lock_depth;
5107 int migrate_disable;
5108 int hardirq;
5109 int softirq;
5110 void *data = record->data;
5112 lat_flags = parse_common_flags(pevent, data);
5113 pc = parse_common_pc(pevent, data);
5114 /* lock_depth may not always exist */
5115 if (lock_depth_exists)
5116 lock_depth = parse_common_lock_depth(pevent, data);
5117 else if (check_lock_depth) {
5118 lock_depth = parse_common_lock_depth(pevent, data);
5119 if (lock_depth < 0)
5120 check_lock_depth = 0;
5121 else
5122 lock_depth_exists = 1;
5125 /* migrate_disable may not always exist */
5126 if (migrate_disable_exists)
5127 migrate_disable = parse_common_migrate_disable(pevent, data);
5128 else if (check_migrate_disable) {
5129 migrate_disable = parse_common_migrate_disable(pevent, data);
5130 if (migrate_disable < 0)
5131 check_migrate_disable = 0;
5132 else
5133 migrate_disable_exists = 1;
5136 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5137 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5139 trace_seq_printf(s, "%c%c%c",
5140 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5141 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5142 'X' : '.',
5143 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5144 'N' : '.',
5145 (hardirq && softirq) ? 'H' :
5146 hardirq ? 'h' : softirq ? 's' : '.');
5148 if (pc)
5149 trace_seq_printf(s, "%x", pc);
5150 else
5151 trace_seq_putc(s, '.');
5153 if (migrate_disable_exists) {
5154 if (migrate_disable < 0)
5155 trace_seq_putc(s, '.');
5156 else
5157 trace_seq_printf(s, "%d", migrate_disable);
5160 if (lock_depth_exists) {
5161 if (lock_depth < 0)
5162 trace_seq_putc(s, '.');
5163 else
5164 trace_seq_printf(s, "%d", lock_depth);
5167 trace_seq_terminate(s);
5171 * pevent_data_type - parse out the given event type
5172 * @pevent: a handle to the pevent
5173 * @rec: the record to read from
5175 * This returns the event id from the @rec.
5177 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5179 return trace_parse_common_type(pevent, rec->data);
5183 * pevent_data_event_from_type - find the event by a given type
5184 * @pevent: a handle to the pevent
5185 * @type: the type of the event.
5187 * This returns the event form a given @type;
5189 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5191 return pevent_find_event(pevent, type);
5195 * pevent_data_pid - parse the PID from record
5196 * @pevent: a handle to the pevent
5197 * @rec: the record to parse
5199 * This returns the PID from a record.
5201 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5203 return parse_common_pid(pevent, rec->data);
5207 * pevent_data_prempt_count - parse the preempt count from the record
5208 * @pevent: a handle to the pevent
5209 * @rec: the record to parse
5211 * This returns the preempt count from a record.
5213 int pevent_data_prempt_count(struct pevent *pevent, struct pevent_record *rec)
5215 return parse_common_pc(pevent, rec->data);
5219 * pevent_data_flags - parse the latency flags from the record
5220 * @pevent: a handle to the pevent
5221 * @rec: the record to parse
5223 * This returns the latency flags from a record.
5225 * Use trace_flag_type enum for the flags (see event-parse.h).
5227 int pevent_data_flags(struct pevent *pevent, struct pevent_record *rec)
5229 return parse_common_flags(pevent, rec->data);
5233 * pevent_data_comm_from_pid - return the command line from PID
5234 * @pevent: a handle to the pevent
5235 * @pid: the PID of the task to search for
5237 * This returns a pointer to the command line that has the given
5238 * @pid.
5240 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5242 const char *comm;
5244 comm = find_cmdline(pevent, pid);
5245 return comm;
5248 static struct cmdline *
5249 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5251 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5253 if (cmdlist)
5254 cmdlist = cmdlist->next;
5255 else
5256 cmdlist = pevent->cmdlist;
5258 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5259 cmdlist = cmdlist->next;
5261 return (struct cmdline *)cmdlist;
5265 * pevent_data_pid_from_comm - return the pid from a given comm
5266 * @pevent: a handle to the pevent
5267 * @comm: the cmdline to find the pid from
5268 * @next: the cmdline structure to find the next comm
5270 * This returns the cmdline structure that holds a pid for a given
5271 * comm, or NULL if none found. As there may be more than one pid for
5272 * a given comm, the result of this call can be passed back into
5273 * a recurring call in the @next paramater, and then it will find the
5274 * next pid.
5275 * Also, it does a linear seach, so it may be slow.
5277 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5278 struct cmdline *next)
5280 struct cmdline *cmdline;
5283 * If the cmdlines have not been converted yet, then use
5284 * the list.
5286 if (!pevent->cmdlines)
5287 return pid_from_cmdlist(pevent, comm, next);
5289 if (next) {
5291 * The next pointer could have been still from
5292 * a previous call before cmdlines were created
5294 if (next < pevent->cmdlines ||
5295 next >= pevent->cmdlines + pevent->cmdline_count)
5296 next = NULL;
5297 else
5298 cmdline = next++;
5301 if (!next)
5302 cmdline = pevent->cmdlines;
5304 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5305 if (strcmp(cmdline->comm, comm) == 0)
5306 return cmdline;
5307 cmdline++;
5309 return NULL;
5313 * pevent_cmdline_pid - return the pid associated to a given cmdline
5314 * @cmdline: The cmdline structure to get the pid from
5316 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5317 * -1 is returned.
5319 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5321 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5323 if (!cmdline)
5324 return -1;
5327 * If cmdlines have not been created yet, or cmdline is
5328 * not part of the array, then treat it as a cmdlist instead.
5330 if (!pevent->cmdlines ||
5331 cmdline < pevent->cmdlines ||
5332 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5333 return cmdlist->pid;
5335 return cmdline->pid;
5339 * pevent_data_comm_from_pid - parse the data into the print format
5340 * @s: the trace_seq to write to
5341 * @event: the handle to the event
5342 * @record: the record to read from
5344 * This parses the raw @data using the given @event information and
5345 * writes the print format into the trace_seq.
5347 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5348 struct pevent_record *record)
5350 int print_pretty = 1;
5352 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5353 pevent_print_fields(s, record->data, record->size, event);
5354 else {
5356 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5357 print_pretty = event->handler(s, record, event,
5358 event->context);
5360 if (print_pretty)
5361 pretty_print(s, record->data, record->size, event);
5364 trace_seq_terminate(s);
5367 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5369 if (!use_trace_clock)
5370 return true;
5372 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5373 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5374 return true;
5376 /* trace_clock is setting in tsc or counter mode */
5377 return false;
5381 * pevent_find_event_by_record - return the event from a given record
5382 * @pevent: a handle to the pevent
5383 * @record: The record to get the event from
5385 * Returns the associated event for a given record, or NULL if non is
5386 * is found.
5388 struct event_format *
5389 pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
5391 int type;
5393 if (record->size < 0) {
5394 do_warning("ug! negative record size %d", record->size);
5395 return NULL;
5398 type = trace_parse_common_type(pevent, record->data);
5400 return pevent_find_event(pevent, type);
5404 * pevent_print_event_task - Write the event task comm, pid and CPU
5405 * @pevent: a handle to the pevent
5406 * @s: the trace_seq to write to
5407 * @event: the handle to the record's event
5408 * @record: The record to get the event from
5410 * Writes the tasks comm, pid and CPU to @s.
5412 void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
5413 struct event_format *event,
5414 struct pevent_record *record)
5416 void *data = record->data;
5417 const char *comm;
5418 int pid;
5420 pid = parse_common_pid(pevent, data);
5421 comm = find_cmdline(pevent, pid);
5423 if (pevent->latency_format) {
5424 trace_seq_printf(s, "%8.8s-%-5d %3d",
5425 comm, pid, record->cpu);
5426 } else
5427 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5431 * pevent_print_event_time - Write the event timestamp
5432 * @pevent: a handle to the pevent
5433 * @s: the trace_seq to write to
5434 * @event: the handle to the record's event
5435 * @record: The record to get the event from
5436 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5438 * Writes the timestamp of the record into @s.
5440 void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
5441 struct event_format *event,
5442 struct pevent_record *record,
5443 bool use_trace_clock)
5445 unsigned long secs;
5446 unsigned long usecs;
5447 unsigned long nsecs;
5448 int p;
5449 bool use_usec_format;
5451 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5452 use_trace_clock);
5453 if (use_usec_format) {
5454 secs = record->ts / NSEC_PER_SEC;
5455 nsecs = record->ts - secs * NSEC_PER_SEC;
5458 if (pevent->latency_format) {
5459 pevent_data_lat_fmt(pevent, s, record);
5462 if (use_usec_format) {
5463 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5464 usecs = nsecs;
5465 p = 9;
5466 } else {
5467 usecs = (nsecs + 500) / NSEC_PER_USEC;
5468 /* To avoid usecs larger than 1 sec */
5469 if (usecs >= USEC_PER_SEC) {
5470 usecs -= USEC_PER_SEC;
5471 secs++;
5473 p = 6;
5476 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5477 } else
5478 trace_seq_printf(s, " %12llu:", record->ts);
5482 * pevent_print_event_data - Write the event data section
5483 * @pevent: a handle to the pevent
5484 * @s: the trace_seq to write to
5485 * @event: the handle to the record's event
5486 * @record: The record to get the event from
5488 * Writes the parsing of the record's data to @s.
5490 void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
5491 struct event_format *event,
5492 struct pevent_record *record)
5494 static const char *spaces = " "; /* 20 spaces */
5495 int len;
5497 trace_seq_printf(s, " %s: ", event->name);
5499 /* Space out the event names evenly. */
5500 len = strlen(event->name);
5501 if (len < 20)
5502 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5504 pevent_event_info(s, event, record);
5507 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5508 struct pevent_record *record, bool use_trace_clock)
5510 struct event_format *event;
5512 event = pevent_find_event_by_record(pevent, record);
5513 if (!event) {
5514 do_warning("ug! no event found for type %d",
5515 trace_parse_common_type(pevent, record->data));
5516 return;
5519 pevent_print_event_task(pevent, s, event, record);
5520 pevent_print_event_time(pevent, s, event, record, use_trace_clock);
5521 pevent_print_event_data(pevent, s, event, record);
5524 static int events_id_cmp(const void *a, const void *b)
5526 struct event_format * const * ea = a;
5527 struct event_format * const * eb = b;
5529 if ((*ea)->id < (*eb)->id)
5530 return -1;
5532 if ((*ea)->id > (*eb)->id)
5533 return 1;
5535 return 0;
5538 static int events_name_cmp(const void *a, const void *b)
5540 struct event_format * const * ea = a;
5541 struct event_format * const * eb = b;
5542 int res;
5544 res = strcmp((*ea)->name, (*eb)->name);
5545 if (res)
5546 return res;
5548 res = strcmp((*ea)->system, (*eb)->system);
5549 if (res)
5550 return res;
5552 return events_id_cmp(a, b);
5555 static int events_system_cmp(const void *a, const void *b)
5557 struct event_format * const * ea = a;
5558 struct event_format * const * eb = b;
5559 int res;
5561 res = strcmp((*ea)->system, (*eb)->system);
5562 if (res)
5563 return res;
5565 res = strcmp((*ea)->name, (*eb)->name);
5566 if (res)
5567 return res;
5569 return events_id_cmp(a, b);
5572 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5574 struct event_format **events;
5575 int (*sort)(const void *a, const void *b);
5577 events = pevent->sort_events;
5579 if (events && pevent->last_type == sort_type)
5580 return events;
5582 if (!events) {
5583 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5584 if (!events)
5585 return NULL;
5587 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5588 events[pevent->nr_events] = NULL;
5590 pevent->sort_events = events;
5592 /* the internal events are sorted by id */
5593 if (sort_type == EVENT_SORT_ID) {
5594 pevent->last_type = sort_type;
5595 return events;
5599 switch (sort_type) {
5600 case EVENT_SORT_ID:
5601 sort = events_id_cmp;
5602 break;
5603 case EVENT_SORT_NAME:
5604 sort = events_name_cmp;
5605 break;
5606 case EVENT_SORT_SYSTEM:
5607 sort = events_system_cmp;
5608 break;
5609 default:
5610 return events;
5613 qsort(events, pevent->nr_events, sizeof(*events), sort);
5614 pevent->last_type = sort_type;
5616 return events;
5619 static struct format_field **
5620 get_event_fields(const char *type, const char *name,
5621 int count, struct format_field *list)
5623 struct format_field **fields;
5624 struct format_field *field;
5625 int i = 0;
5627 fields = malloc(sizeof(*fields) * (count + 1));
5628 if (!fields)
5629 return NULL;
5631 for (field = list; field; field = field->next) {
5632 fields[i++] = field;
5633 if (i == count + 1) {
5634 do_warning("event %s has more %s fields than specified",
5635 name, type);
5636 i--;
5637 break;
5641 if (i != count)
5642 do_warning("event %s has less %s fields than specified",
5643 name, type);
5645 fields[i] = NULL;
5647 return fields;
5651 * pevent_event_common_fields - return a list of common fields for an event
5652 * @event: the event to return the common fields of.
5654 * Returns an allocated array of fields. The last item in the array is NULL.
5655 * The array must be freed with free().
5657 struct format_field **pevent_event_common_fields(struct event_format *event)
5659 return get_event_fields("common", event->name,
5660 event->format.nr_common,
5661 event->format.common_fields);
5665 * pevent_event_fields - return a list of event specific fields for an event
5666 * @event: the event to return the fields of.
5668 * Returns an allocated array of fields. The last item in the array is NULL.
5669 * The array must be freed with free().
5671 struct format_field **pevent_event_fields(struct event_format *event)
5673 return get_event_fields("event", event->name,
5674 event->format.nr_fields,
5675 event->format.fields);
5678 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5680 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5681 if (field->next) {
5682 trace_seq_puts(s, ", ");
5683 print_fields(s, field->next);
5687 /* for debugging */
5688 static void print_args(struct print_arg *args)
5690 int print_paren = 1;
5691 struct trace_seq s;
5693 switch (args->type) {
5694 case PRINT_NULL:
5695 printf("null");
5696 break;
5697 case PRINT_ATOM:
5698 printf("%s", args->atom.atom);
5699 break;
5700 case PRINT_FIELD:
5701 printf("REC->%s", args->field.name);
5702 break;
5703 case PRINT_FLAGS:
5704 printf("__print_flags(");
5705 print_args(args->flags.field);
5706 printf(", %s, ", args->flags.delim);
5707 trace_seq_init(&s);
5708 print_fields(&s, args->flags.flags);
5709 trace_seq_do_printf(&s);
5710 trace_seq_destroy(&s);
5711 printf(")");
5712 break;
5713 case PRINT_SYMBOL:
5714 printf("__print_symbolic(");
5715 print_args(args->symbol.field);
5716 printf(", ");
5717 trace_seq_init(&s);
5718 print_fields(&s, args->symbol.symbols);
5719 trace_seq_do_printf(&s);
5720 trace_seq_destroy(&s);
5721 printf(")");
5722 break;
5723 case PRINT_HEX:
5724 printf("__print_hex(");
5725 print_args(args->hex.field);
5726 printf(", ");
5727 print_args(args->hex.size);
5728 printf(")");
5729 break;
5730 case PRINT_INT_ARRAY:
5731 printf("__print_array(");
5732 print_args(args->int_array.field);
5733 printf(", ");
5734 print_args(args->int_array.count);
5735 printf(", ");
5736 print_args(args->int_array.el_size);
5737 printf(")");
5738 break;
5739 case PRINT_STRING:
5740 case PRINT_BSTRING:
5741 printf("__get_str(%s)", args->string.string);
5742 break;
5743 case PRINT_BITMASK:
5744 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5745 break;
5746 case PRINT_TYPE:
5747 printf("(%s)", args->typecast.type);
5748 print_args(args->typecast.item);
5749 break;
5750 case PRINT_OP:
5751 if (strcmp(args->op.op, ":") == 0)
5752 print_paren = 0;
5753 if (print_paren)
5754 printf("(");
5755 print_args(args->op.left);
5756 printf(" %s ", args->op.op);
5757 print_args(args->op.right);
5758 if (print_paren)
5759 printf(")");
5760 break;
5761 default:
5762 /* we should warn... */
5763 return;
5765 if (args->next) {
5766 printf("\n");
5767 print_args(args->next);
5771 static void parse_header_field(const char *field,
5772 int *offset, int *size, int mandatory)
5774 unsigned long long save_input_buf_ptr;
5775 unsigned long long save_input_buf_siz;
5776 char *token;
5777 int type;
5779 save_input_buf_ptr = input_buf_ptr;
5780 save_input_buf_siz = input_buf_siz;
5782 if (read_expected(EVENT_ITEM, "field") < 0)
5783 return;
5784 if (read_expected(EVENT_OP, ":") < 0)
5785 return;
5787 /* type */
5788 if (read_expect_type(EVENT_ITEM, &token) < 0)
5789 goto fail;
5790 free_token(token);
5793 * If this is not a mandatory field, then test it first.
5795 if (mandatory) {
5796 if (read_expected(EVENT_ITEM, field) < 0)
5797 return;
5798 } else {
5799 if (read_expect_type(EVENT_ITEM, &token) < 0)
5800 goto fail;
5801 if (strcmp(token, field) != 0)
5802 goto discard;
5803 free_token(token);
5806 if (read_expected(EVENT_OP, ";") < 0)
5807 return;
5808 if (read_expected(EVENT_ITEM, "offset") < 0)
5809 return;
5810 if (read_expected(EVENT_OP, ":") < 0)
5811 return;
5812 if (read_expect_type(EVENT_ITEM, &token) < 0)
5813 goto fail;
5814 *offset = atoi(token);
5815 free_token(token);
5816 if (read_expected(EVENT_OP, ";") < 0)
5817 return;
5818 if (read_expected(EVENT_ITEM, "size") < 0)
5819 return;
5820 if (read_expected(EVENT_OP, ":") < 0)
5821 return;
5822 if (read_expect_type(EVENT_ITEM, &token) < 0)
5823 goto fail;
5824 *size = atoi(token);
5825 free_token(token);
5826 if (read_expected(EVENT_OP, ";") < 0)
5827 return;
5828 type = read_token(&token);
5829 if (type != EVENT_NEWLINE) {
5830 /* newer versions of the kernel have a "signed" type */
5831 if (type != EVENT_ITEM)
5832 goto fail;
5834 if (strcmp(token, "signed") != 0)
5835 goto fail;
5837 free_token(token);
5839 if (read_expected(EVENT_OP, ":") < 0)
5840 return;
5842 if (read_expect_type(EVENT_ITEM, &token))
5843 goto fail;
5845 free_token(token);
5846 if (read_expected(EVENT_OP, ";") < 0)
5847 return;
5849 if (read_expect_type(EVENT_NEWLINE, &token))
5850 goto fail;
5852 fail:
5853 free_token(token);
5854 return;
5856 discard:
5857 input_buf_ptr = save_input_buf_ptr;
5858 input_buf_siz = save_input_buf_siz;
5859 *offset = 0;
5860 *size = 0;
5861 free_token(token);
5865 * pevent_parse_header_page - parse the data stored in the header page
5866 * @pevent: the handle to the pevent
5867 * @buf: the buffer storing the header page format string
5868 * @size: the size of @buf
5869 * @long_size: the long size to use if there is no header
5871 * This parses the header page format for information on the
5872 * ring buffer used. The @buf should be copied from
5874 * /sys/kernel/debug/tracing/events/header_page
5876 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5877 int long_size)
5879 int ignore;
5881 if (!size) {
5883 * Old kernels did not have header page info.
5884 * Sorry but we just use what we find here in user space.
5886 pevent->header_page_ts_size = sizeof(long long);
5887 pevent->header_page_size_size = long_size;
5888 pevent->header_page_data_offset = sizeof(long long) + long_size;
5889 pevent->old_format = 1;
5890 return -1;
5892 init_input_buf(buf, size);
5894 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5895 &pevent->header_page_ts_size, 1);
5896 parse_header_field("commit", &pevent->header_page_size_offset,
5897 &pevent->header_page_size_size, 1);
5898 parse_header_field("overwrite", &pevent->header_page_overwrite,
5899 &ignore, 0);
5900 parse_header_field("data", &pevent->header_page_data_offset,
5901 &pevent->header_page_data_size, 1);
5903 return 0;
5906 static int event_matches(struct event_format *event,
5907 int id, const char *sys_name,
5908 const char *event_name)
5910 if (id >= 0 && id != event->id)
5911 return 0;
5913 if (event_name && (strcmp(event_name, event->name) != 0))
5914 return 0;
5916 if (sys_name && (strcmp(sys_name, event->system) != 0))
5917 return 0;
5919 return 1;
5922 static void free_handler(struct event_handler *handle)
5924 free((void *)handle->sys_name);
5925 free((void *)handle->event_name);
5926 free(handle);
5929 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5931 struct event_handler *handle, **next;
5933 for (next = &pevent->handlers; *next;
5934 next = &(*next)->next) {
5935 handle = *next;
5936 if (event_matches(event, handle->id,
5937 handle->sys_name,
5938 handle->event_name))
5939 break;
5942 if (!(*next))
5943 return 0;
5945 pr_stat("overriding event (%d) %s:%s with new print handler",
5946 event->id, event->system, event->name);
5948 event->handler = handle->func;
5949 event->context = handle->context;
5951 *next = handle->next;
5952 free_handler(handle);
5954 return 1;
5958 * __pevent_parse_format - parse the event format
5959 * @buf: the buffer storing the event format string
5960 * @size: the size of @buf
5961 * @sys: the system the event belongs to
5963 * This parses the event format and creates an event structure
5964 * to quickly parse raw data for a given event.
5966 * These files currently come from:
5968 * /sys/kernel/debug/tracing/events/.../.../format
5970 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5971 struct pevent *pevent, const char *buf,
5972 unsigned long size, const char *sys)
5974 struct event_format *event;
5975 int ret;
5977 init_input_buf(buf, size);
5979 *eventp = event = alloc_event();
5980 if (!event)
5981 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5983 event->name = event_read_name();
5984 if (!event->name) {
5985 /* Bad event? */
5986 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5987 goto event_alloc_failed;
5990 if (strcmp(sys, "ftrace") == 0) {
5991 event->flags |= EVENT_FL_ISFTRACE;
5993 if (strcmp(event->name, "bprint") == 0)
5994 event->flags |= EVENT_FL_ISBPRINT;
5997 event->id = event_read_id();
5998 if (event->id < 0) {
5999 ret = PEVENT_ERRNO__READ_ID_FAILED;
6001 * This isn't an allocation error actually.
6002 * But as the ID is critical, just bail out.
6004 goto event_alloc_failed;
6007 event->system = strdup(sys);
6008 if (!event->system) {
6009 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6010 goto event_alloc_failed;
6013 /* Add pevent to event so that it can be referenced */
6014 event->pevent = pevent;
6016 ret = event_read_format(event);
6017 if (ret < 0) {
6018 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
6019 goto event_parse_failed;
6023 * If the event has an override, don't print warnings if the event
6024 * print format fails to parse.
6026 if (pevent && find_event_handle(pevent, event))
6027 show_warning = 0;
6029 ret = event_read_print(event);
6030 show_warning = 1;
6032 if (ret < 0) {
6033 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
6034 goto event_parse_failed;
6037 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6038 struct format_field *field;
6039 struct print_arg *arg, **list;
6041 /* old ftrace had no args */
6042 list = &event->print_fmt.args;
6043 for (field = event->format.fields; field; field = field->next) {
6044 arg = alloc_arg();
6045 if (!arg) {
6046 event->flags |= EVENT_FL_FAILED;
6047 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6049 arg->type = PRINT_FIELD;
6050 arg->field.name = strdup(field->name);
6051 if (!arg->field.name) {
6052 event->flags |= EVENT_FL_FAILED;
6053 free_arg(arg);
6054 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6056 arg->field.field = field;
6057 *list = arg;
6058 list = &arg->next;
6060 return 0;
6063 return 0;
6065 event_parse_failed:
6066 event->flags |= EVENT_FL_FAILED;
6067 return ret;
6069 event_alloc_failed:
6070 free(event->system);
6071 free(event->name);
6072 free(event);
6073 *eventp = NULL;
6074 return ret;
6077 static enum pevent_errno
6078 __pevent_parse_event(struct pevent *pevent,
6079 struct event_format **eventp,
6080 const char *buf, unsigned long size,
6081 const char *sys)
6083 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
6084 struct event_format *event = *eventp;
6086 if (event == NULL)
6087 return ret;
6089 if (pevent && add_event(pevent, event)) {
6090 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6091 goto event_add_failed;
6094 #define PRINT_ARGS 0
6095 if (PRINT_ARGS && event->print_fmt.args)
6096 print_args(event->print_fmt.args);
6098 return 0;
6100 event_add_failed:
6101 pevent_free_format(event);
6102 return ret;
6106 * pevent_parse_format - parse the event format
6107 * @pevent: the handle to the pevent
6108 * @eventp: returned format
6109 * @buf: the buffer storing the event format string
6110 * @size: the size of @buf
6111 * @sys: the system the event belongs to
6113 * This parses the event format and creates an event structure
6114 * to quickly parse raw data for a given event.
6116 * These files currently come from:
6118 * /sys/kernel/debug/tracing/events/.../.../format
6120 enum pevent_errno pevent_parse_format(struct pevent *pevent,
6121 struct event_format **eventp,
6122 const char *buf,
6123 unsigned long size, const char *sys)
6125 return __pevent_parse_event(pevent, eventp, buf, size, sys);
6129 * pevent_parse_event - parse the event format
6130 * @pevent: the handle to the pevent
6131 * @buf: the buffer storing the event format string
6132 * @size: the size of @buf
6133 * @sys: the system the event belongs to
6135 * This parses the event format and creates an event structure
6136 * to quickly parse raw data for a given event.
6138 * These files currently come from:
6140 * /sys/kernel/debug/tracing/events/.../.../format
6142 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
6143 unsigned long size, const char *sys)
6145 struct event_format *event = NULL;
6146 return __pevent_parse_event(pevent, &event, buf, size, sys);
6149 #undef _PE
6150 #define _PE(code, str) str
6151 static const char * const pevent_error_str[] = {
6152 PEVENT_ERRORS
6154 #undef _PE
6156 int pevent_strerror(struct pevent *pevent __maybe_unused,
6157 enum pevent_errno errnum, char *buf, size_t buflen)
6159 int idx;
6160 const char *msg;
6162 if (errnum >= 0) {
6163 str_error_r(errnum, buf, buflen);
6164 return 0;
6167 if (errnum <= __PEVENT_ERRNO__START ||
6168 errnum >= __PEVENT_ERRNO__END)
6169 return -1;
6171 idx = errnum - __PEVENT_ERRNO__START - 1;
6172 msg = pevent_error_str[idx];
6173 snprintf(buf, buflen, "%s", msg);
6175 return 0;
6178 int get_field_val(struct trace_seq *s, struct format_field *field,
6179 const char *name, struct pevent_record *record,
6180 unsigned long long *val, int err)
6182 if (!field) {
6183 if (err)
6184 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6185 return -1;
6188 if (pevent_read_number_field(field, record->data, val)) {
6189 if (err)
6190 trace_seq_printf(s, " %s=INVALID", name);
6191 return -1;
6194 return 0;
6198 * pevent_get_field_raw - return the raw pointer into the data field
6199 * @s: The seq to print to on error
6200 * @event: the event that the field is for
6201 * @name: The name of the field
6202 * @record: The record with the field name.
6203 * @len: place to store the field length.
6204 * @err: print default error if failed.
6206 * Returns a pointer into record->data of the field and places
6207 * the length of the field in @len.
6209 * On failure, it returns NULL.
6211 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6212 const char *name, struct pevent_record *record,
6213 int *len, int err)
6215 struct format_field *field;
6216 void *data = record->data;
6217 unsigned offset;
6218 int dummy;
6220 if (!event)
6221 return NULL;
6223 field = pevent_find_field(event, name);
6225 if (!field) {
6226 if (err)
6227 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6228 return NULL;
6231 /* Allow @len to be NULL */
6232 if (!len)
6233 len = &dummy;
6235 offset = field->offset;
6236 if (field->flags & FIELD_IS_DYNAMIC) {
6237 offset = pevent_read_number(event->pevent,
6238 data + offset, field->size);
6239 *len = offset >> 16;
6240 offset &= 0xffff;
6241 } else
6242 *len = field->size;
6244 return data + offset;
6248 * pevent_get_field_val - find a field and return its value
6249 * @s: The seq to print to on error
6250 * @event: the event that the field is for
6251 * @name: The name of the field
6252 * @record: The record with the field name.
6253 * @val: place to store the value of the field.
6254 * @err: print default error if failed.
6256 * Returns 0 on success -1 on field not found.
6258 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6259 const char *name, struct pevent_record *record,
6260 unsigned long long *val, int err)
6262 struct format_field *field;
6264 if (!event)
6265 return -1;
6267 field = pevent_find_field(event, name);
6269 return get_field_val(s, field, name, record, val, err);
6273 * pevent_get_common_field_val - find a common field and return its value
6274 * @s: The seq to print to on error
6275 * @event: the event that the field is for
6276 * @name: The name of the field
6277 * @record: The record with the field name.
6278 * @val: place to store the value of the field.
6279 * @err: print default error if failed.
6281 * Returns 0 on success -1 on field not found.
6283 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6284 const char *name, struct pevent_record *record,
6285 unsigned long long *val, int err)
6287 struct format_field *field;
6289 if (!event)
6290 return -1;
6292 field = pevent_find_common_field(event, name);
6294 return get_field_val(s, field, name, record, val, err);
6298 * pevent_get_any_field_val - find a any field and return its value
6299 * @s: The seq to print to on error
6300 * @event: the event that the field is for
6301 * @name: The name of the field
6302 * @record: The record with the field name.
6303 * @val: place to store the value of the field.
6304 * @err: print default error if failed.
6306 * Returns 0 on success -1 on field not found.
6308 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6309 const char *name, struct pevent_record *record,
6310 unsigned long long *val, int err)
6312 struct format_field *field;
6314 if (!event)
6315 return -1;
6317 field = pevent_find_any_field(event, name);
6319 return get_field_val(s, field, name, record, val, err);
6323 * pevent_print_num_field - print a field and a format
6324 * @s: The seq to print to
6325 * @fmt: The printf format to print the field with.
6326 * @event: the event that the field is for
6327 * @name: The name of the field
6328 * @record: The record with the field name.
6329 * @err: print default error if failed.
6331 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6333 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6334 struct event_format *event, const char *name,
6335 struct pevent_record *record, int err)
6337 struct format_field *field = pevent_find_field(event, name);
6338 unsigned long long val;
6340 if (!field)
6341 goto failed;
6343 if (pevent_read_number_field(field, record->data, &val))
6344 goto failed;
6346 return trace_seq_printf(s, fmt, val);
6348 failed:
6349 if (err)
6350 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6351 return -1;
6355 * pevent_print_func_field - print a field and a format for function pointers
6356 * @s: The seq to print to
6357 * @fmt: The printf format to print the field with.
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 * @err: print default error if failed.
6363 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6365 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6366 struct event_format *event, const char *name,
6367 struct pevent_record *record, int err)
6369 struct format_field *field = pevent_find_field(event, name);
6370 struct pevent *pevent = event->pevent;
6371 unsigned long long val;
6372 struct func_map *func;
6373 char tmp[128];
6375 if (!field)
6376 goto failed;
6378 if (pevent_read_number_field(field, record->data, &val))
6379 goto failed;
6381 func = find_func(pevent, val);
6383 if (func)
6384 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6385 else
6386 sprintf(tmp, "0x%08llx", val);
6388 return trace_seq_printf(s, fmt, tmp);
6390 failed:
6391 if (err)
6392 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6393 return -1;
6396 static void free_func_handle(struct pevent_function_handler *func)
6398 struct pevent_func_params *params;
6400 free(func->name);
6402 while (func->params) {
6403 params = func->params;
6404 func->params = params->next;
6405 free(params);
6408 free(func);
6412 * pevent_register_print_function - register a helper function
6413 * @pevent: the handle to the pevent
6414 * @func: the function to process the helper function
6415 * @ret_type: the return type of the helper function
6416 * @name: the name of the helper function
6417 * @parameters: A list of enum pevent_func_arg_type
6419 * Some events may have helper functions in the print format arguments.
6420 * This allows a plugin to dynamically create a way to process one
6421 * of these functions.
6423 * The @parameters is a variable list of pevent_func_arg_type enums that
6424 * must end with PEVENT_FUNC_ARG_VOID.
6426 int pevent_register_print_function(struct pevent *pevent,
6427 pevent_func_handler func,
6428 enum pevent_func_arg_type ret_type,
6429 char *name, ...)
6431 struct pevent_function_handler *func_handle;
6432 struct pevent_func_params **next_param;
6433 struct pevent_func_params *param;
6434 enum pevent_func_arg_type type;
6435 va_list ap;
6436 int ret;
6438 func_handle = find_func_handler(pevent, name);
6439 if (func_handle) {
6441 * This is most like caused by the users own
6442 * plugins updating the function. This overrides the
6443 * system defaults.
6445 pr_stat("override of function helper '%s'", name);
6446 remove_func_handler(pevent, name);
6449 func_handle = calloc(1, sizeof(*func_handle));
6450 if (!func_handle) {
6451 do_warning("Failed to allocate function handler");
6452 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6455 func_handle->ret_type = ret_type;
6456 func_handle->name = strdup(name);
6457 func_handle->func = func;
6458 if (!func_handle->name) {
6459 do_warning("Failed to allocate function name");
6460 free(func_handle);
6461 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6464 next_param = &(func_handle->params);
6465 va_start(ap, name);
6466 for (;;) {
6467 type = va_arg(ap, enum pevent_func_arg_type);
6468 if (type == PEVENT_FUNC_ARG_VOID)
6469 break;
6471 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6472 do_warning("Invalid argument type %d", type);
6473 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6474 goto out_free;
6477 param = malloc(sizeof(*param));
6478 if (!param) {
6479 do_warning("Failed to allocate function param");
6480 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6481 goto out_free;
6483 param->type = type;
6484 param->next = NULL;
6486 *next_param = param;
6487 next_param = &(param->next);
6489 func_handle->nr_args++;
6491 va_end(ap);
6493 func_handle->next = pevent->func_handlers;
6494 pevent->func_handlers = func_handle;
6496 return 0;
6497 out_free:
6498 va_end(ap);
6499 free_func_handle(func_handle);
6500 return ret;
6504 * pevent_unregister_print_function - unregister a helper function
6505 * @pevent: the handle to the pevent
6506 * @func: the function to process the helper function
6507 * @name: the name of the helper function
6509 * This function removes existing print handler for function @name.
6511 * Returns 0 if the handler was removed successully, -1 otherwise.
6513 int pevent_unregister_print_function(struct pevent *pevent,
6514 pevent_func_handler func, char *name)
6516 struct pevent_function_handler *func_handle;
6518 func_handle = find_func_handler(pevent, name);
6519 if (func_handle && func_handle->func == func) {
6520 remove_func_handler(pevent, name);
6521 return 0;
6523 return -1;
6526 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6527 const char *sys_name,
6528 const char *event_name)
6530 struct event_format *event;
6532 if (id >= 0) {
6533 /* search by id */
6534 event = pevent_find_event(pevent, id);
6535 if (!event)
6536 return NULL;
6537 if (event_name && (strcmp(event_name, event->name) != 0))
6538 return NULL;
6539 if (sys_name && (strcmp(sys_name, event->system) != 0))
6540 return NULL;
6541 } else {
6542 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6543 if (!event)
6544 return NULL;
6546 return event;
6550 * pevent_register_event_handler - register a way to parse an event
6551 * @pevent: the handle to the pevent
6552 * @id: the id of the event to register
6553 * @sys_name: the system name the event belongs to
6554 * @event_name: the name of the event
6555 * @func: the function to call to parse the event information
6556 * @context: the data to be passed to @func
6558 * This function allows a developer to override the parsing of
6559 * a given event. If for some reason the default print format
6560 * is not sufficient, this function will register a function
6561 * for an event to be used to parse the data instead.
6563 * If @id is >= 0, then it is used to find the event.
6564 * else @sys_name and @event_name are used.
6566 int pevent_register_event_handler(struct pevent *pevent, int id,
6567 const char *sys_name, const char *event_name,
6568 pevent_event_handler_func func, void *context)
6570 struct event_format *event;
6571 struct event_handler *handle;
6573 event = pevent_search_event(pevent, id, sys_name, event_name);
6574 if (event == NULL)
6575 goto not_found;
6577 pr_stat("overriding event (%d) %s:%s with new print handler",
6578 event->id, event->system, event->name);
6580 event->handler = func;
6581 event->context = context;
6582 return 0;
6584 not_found:
6585 /* Save for later use. */
6586 handle = calloc(1, sizeof(*handle));
6587 if (!handle) {
6588 do_warning("Failed to allocate event handler");
6589 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6592 handle->id = id;
6593 if (event_name)
6594 handle->event_name = strdup(event_name);
6595 if (sys_name)
6596 handle->sys_name = strdup(sys_name);
6598 if ((event_name && !handle->event_name) ||
6599 (sys_name && !handle->sys_name)) {
6600 do_warning("Failed to allocate event/sys name");
6601 free((void *)handle->event_name);
6602 free((void *)handle->sys_name);
6603 free(handle);
6604 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6607 handle->func = func;
6608 handle->next = pevent->handlers;
6609 pevent->handlers = handle;
6610 handle->context = context;
6612 return -1;
6615 static int handle_matches(struct event_handler *handler, int id,
6616 const char *sys_name, const char *event_name,
6617 pevent_event_handler_func func, void *context)
6619 if (id >= 0 && id != handler->id)
6620 return 0;
6622 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6623 return 0;
6625 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6626 return 0;
6628 if (func != handler->func || context != handler->context)
6629 return 0;
6631 return 1;
6635 * pevent_unregister_event_handler - unregister an existing event handler
6636 * @pevent: the handle to the pevent
6637 * @id: the id of the event to unregister
6638 * @sys_name: the system name the handler belongs to
6639 * @event_name: the name of the event handler
6640 * @func: the function to call to parse the event information
6641 * @context: the data to be passed to @func
6643 * This function removes existing event handler (parser).
6645 * If @id is >= 0, then it is used to find the event.
6646 * else @sys_name and @event_name are used.
6648 * Returns 0 if handler was removed successfully, -1 if event was not found.
6650 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6651 const char *sys_name, const char *event_name,
6652 pevent_event_handler_func func, void *context)
6654 struct event_format *event;
6655 struct event_handler *handle;
6656 struct event_handler **next;
6658 event = pevent_search_event(pevent, id, sys_name, event_name);
6659 if (event == NULL)
6660 goto not_found;
6662 if (event->handler == func && event->context == context) {
6663 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6664 event->id, event->system, event->name);
6666 event->handler = NULL;
6667 event->context = NULL;
6668 return 0;
6671 not_found:
6672 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6673 handle = *next;
6674 if (handle_matches(handle, id, sys_name, event_name,
6675 func, context))
6676 break;
6679 if (!(*next))
6680 return -1;
6682 *next = handle->next;
6683 free_handler(handle);
6685 return 0;
6689 * pevent_alloc - create a pevent handle
6691 struct pevent *pevent_alloc(void)
6693 struct pevent *pevent = calloc(1, sizeof(*pevent));
6695 if (pevent)
6696 pevent->ref_count = 1;
6698 return pevent;
6701 void pevent_ref(struct pevent *pevent)
6703 pevent->ref_count++;
6706 void pevent_free_format_field(struct format_field *field)
6708 free(field->type);
6709 if (field->alias != field->name)
6710 free(field->alias);
6711 free(field->name);
6712 free(field);
6715 static void free_format_fields(struct format_field *field)
6717 struct format_field *next;
6719 while (field) {
6720 next = field->next;
6721 pevent_free_format_field(field);
6722 field = next;
6726 static void free_formats(struct format *format)
6728 free_format_fields(format->common_fields);
6729 free_format_fields(format->fields);
6732 void pevent_free_format(struct event_format *event)
6734 free(event->name);
6735 free(event->system);
6737 free_formats(&event->format);
6739 free(event->print_fmt.format);
6740 free_args(event->print_fmt.args);
6742 free(event);
6746 * pevent_free - free a pevent handle
6747 * @pevent: the pevent handle to free
6749 void pevent_free(struct pevent *pevent)
6751 struct cmdline_list *cmdlist, *cmdnext;
6752 struct func_list *funclist, *funcnext;
6753 struct printk_list *printklist, *printknext;
6754 struct pevent_function_handler *func_handler;
6755 struct event_handler *handle;
6756 int i;
6758 if (!pevent)
6759 return;
6761 cmdlist = pevent->cmdlist;
6762 funclist = pevent->funclist;
6763 printklist = pevent->printklist;
6765 pevent->ref_count--;
6766 if (pevent->ref_count)
6767 return;
6769 if (pevent->cmdlines) {
6770 for (i = 0; i < pevent->cmdline_count; i++)
6771 free(pevent->cmdlines[i].comm);
6772 free(pevent->cmdlines);
6775 while (cmdlist) {
6776 cmdnext = cmdlist->next;
6777 free(cmdlist->comm);
6778 free(cmdlist);
6779 cmdlist = cmdnext;
6782 if (pevent->func_map) {
6783 for (i = 0; i < (int)pevent->func_count; i++) {
6784 free(pevent->func_map[i].func);
6785 free(pevent->func_map[i].mod);
6787 free(pevent->func_map);
6790 while (funclist) {
6791 funcnext = funclist->next;
6792 free(funclist->func);
6793 free(funclist->mod);
6794 free(funclist);
6795 funclist = funcnext;
6798 while (pevent->func_handlers) {
6799 func_handler = pevent->func_handlers;
6800 pevent->func_handlers = func_handler->next;
6801 free_func_handle(func_handler);
6804 if (pevent->printk_map) {
6805 for (i = 0; i < (int)pevent->printk_count; i++)
6806 free(pevent->printk_map[i].printk);
6807 free(pevent->printk_map);
6810 while (printklist) {
6811 printknext = printklist->next;
6812 free(printklist->printk);
6813 free(printklist);
6814 printklist = printknext;
6817 for (i = 0; i < pevent->nr_events; i++)
6818 pevent_free_format(pevent->events[i]);
6820 while (pevent->handlers) {
6821 handle = pevent->handlers;
6822 pevent->handlers = handle->next;
6823 free_handler(handle);
6826 free(pevent->trace_clock);
6827 free(pevent->events);
6828 free(pevent->sort_events);
6829 free(pevent->func_resolver);
6831 free(pevent);
6834 void pevent_unref(struct pevent *pevent)
6836 pevent_free(pevent);