drivers/net/ethernet/amd/pcnet32.c: neaten and remove unnecessary OOM messages
[linux/fpc-iii.git] / tools / lib / traceevent / event-parse.c
blobcf3a44bf1ec3f5d65b5bf3fe128ee178834714ef
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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <limits.h>
35 #include "event-parse.h"
36 #include "event-utils.h"
38 static const char *input_buf;
39 static unsigned long long input_buf_ptr;
40 static unsigned long long input_buf_siz;
42 static int is_flag_field;
43 static int is_symbolic_field;
45 static int show_warning = 1;
47 #define do_warning(fmt, ...) \
48 do { \
49 if (show_warning) \
50 warning(fmt, ##__VA_ARGS__); \
51 } while (0)
53 #define do_warning_event(event, fmt, ...) \
54 do { \
55 if (!show_warning) \
56 continue; \
58 if (event) \
59 warning("[%s:%s] " fmt, event->system, \
60 event->name, ##__VA_ARGS__); \
61 else \
62 warning(fmt, ##__VA_ARGS__); \
63 } while (0)
65 static void init_input_buf(const char *buf, unsigned long long size)
67 input_buf = buf;
68 input_buf_siz = size;
69 input_buf_ptr = 0;
72 const char *pevent_get_input_buf(void)
74 return input_buf;
77 unsigned long long pevent_get_input_buf_ptr(void)
79 return input_buf_ptr;
82 struct event_handler {
83 struct event_handler *next;
84 int id;
85 const char *sys_name;
86 const char *event_name;
87 pevent_event_handler_func func;
88 void *context;
91 struct pevent_func_params {
92 struct pevent_func_params *next;
93 enum pevent_func_arg_type type;
96 struct pevent_function_handler {
97 struct pevent_function_handler *next;
98 enum pevent_func_arg_type ret_type;
99 char *name;
100 pevent_func_handler func;
101 struct pevent_func_params *params;
102 int nr_args;
105 static unsigned long long
106 process_defined_func(struct trace_seq *s, void *data, int size,
107 struct event_format *event, struct print_arg *arg);
109 static void free_func_handle(struct pevent_function_handler *func);
112 * pevent_buffer_init - init buffer for parsing
113 * @buf: buffer to parse
114 * @size: the size of the buffer
116 * For use with pevent_read_token(), this initializes the internal
117 * buffer that pevent_read_token() will parse.
119 void pevent_buffer_init(const char *buf, unsigned long long size)
121 init_input_buf(buf, size);
124 void breakpoint(void)
126 static int x;
127 x++;
130 struct print_arg *alloc_arg(void)
132 return calloc(1, sizeof(struct print_arg));
135 struct cmdline {
136 char *comm;
137 int pid;
140 static int cmdline_cmp(const void *a, const void *b)
142 const struct cmdline *ca = a;
143 const struct cmdline *cb = b;
145 if (ca->pid < cb->pid)
146 return -1;
147 if (ca->pid > cb->pid)
148 return 1;
150 return 0;
153 struct cmdline_list {
154 struct cmdline_list *next;
155 char *comm;
156 int pid;
159 static int cmdline_init(struct pevent *pevent)
161 struct cmdline_list *cmdlist = pevent->cmdlist;
162 struct cmdline_list *item;
163 struct cmdline *cmdlines;
164 int i;
166 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
167 if (!cmdlines)
168 return -1;
170 i = 0;
171 while (cmdlist) {
172 cmdlines[i].pid = cmdlist->pid;
173 cmdlines[i].comm = cmdlist->comm;
174 i++;
175 item = cmdlist;
176 cmdlist = cmdlist->next;
177 free(item);
180 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
182 pevent->cmdlines = cmdlines;
183 pevent->cmdlist = NULL;
185 return 0;
188 static const char *find_cmdline(struct pevent *pevent, int pid)
190 const struct cmdline *comm;
191 struct cmdline key;
193 if (!pid)
194 return "<idle>";
196 if (!pevent->cmdlines && cmdline_init(pevent))
197 return "<not enough memory for cmdlines!>";
199 key.pid = pid;
201 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
202 sizeof(*pevent->cmdlines), cmdline_cmp);
204 if (comm)
205 return comm->comm;
206 return "<...>";
210 * pevent_pid_is_registered - return if a pid has a cmdline registered
211 * @pevent: handle for the pevent
212 * @pid: The pid to check if it has a cmdline registered with.
214 * Returns 1 if the pid has a cmdline mapped to it
215 * 0 otherwise.
217 int pevent_pid_is_registered(struct pevent *pevent, int pid)
219 const struct cmdline *comm;
220 struct cmdline key;
222 if (!pid)
223 return 1;
225 if (!pevent->cmdlines && cmdline_init(pevent))
226 return 0;
228 key.pid = pid;
230 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
231 sizeof(*pevent->cmdlines), cmdline_cmp);
233 if (comm)
234 return 1;
235 return 0;
239 * If the command lines have been converted to an array, then
240 * we must add this pid. This is much slower than when cmdlines
241 * are added before the array is initialized.
243 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
245 struct cmdline *cmdlines = pevent->cmdlines;
246 const struct cmdline *cmdline;
247 struct cmdline key;
249 if (!pid)
250 return 0;
252 /* avoid duplicates */
253 key.pid = pid;
255 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
256 sizeof(*pevent->cmdlines), cmdline_cmp);
257 if (cmdline) {
258 errno = EEXIST;
259 return -1;
262 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
263 if (!cmdlines) {
264 errno = ENOMEM;
265 return -1;
268 cmdlines[pevent->cmdline_count].comm = strdup(comm);
269 if (!cmdlines[pevent->cmdline_count].comm) {
270 free(cmdlines);
271 errno = ENOMEM;
272 return -1;
275 cmdlines[pevent->cmdline_count].pid = pid;
277 if (cmdlines[pevent->cmdline_count].comm)
278 pevent->cmdline_count++;
280 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
281 pevent->cmdlines = cmdlines;
283 return 0;
287 * pevent_register_comm - register a pid / comm mapping
288 * @pevent: handle for the pevent
289 * @comm: the command line to register
290 * @pid: the pid to map the command line to
292 * This adds a mapping to search for command line names with
293 * a given pid. The comm is duplicated.
295 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
297 struct cmdline_list *item;
299 if (pevent->cmdlines)
300 return add_new_comm(pevent, comm, pid);
302 item = malloc(sizeof(*item));
303 if (!item)
304 return -1;
306 item->comm = strdup(comm);
307 if (!item->comm) {
308 free(item);
309 return -1;
311 item->pid = pid;
312 item->next = pevent->cmdlist;
314 pevent->cmdlist = item;
315 pevent->cmdline_count++;
317 return 0;
320 void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
322 pevent->trace_clock = trace_clock;
325 struct func_map {
326 unsigned long long addr;
327 char *func;
328 char *mod;
331 struct func_list {
332 struct func_list *next;
333 unsigned long long addr;
334 char *func;
335 char *mod;
338 static int func_cmp(const void *a, const void *b)
340 const struct func_map *fa = a;
341 const struct func_map *fb = b;
343 if (fa->addr < fb->addr)
344 return -1;
345 if (fa->addr > fb->addr)
346 return 1;
348 return 0;
352 * We are searching for a record in between, not an exact
353 * match.
355 static int func_bcmp(const void *a, const void *b)
357 const struct func_map *fa = a;
358 const struct func_map *fb = b;
360 if ((fa->addr == fb->addr) ||
362 (fa->addr > fb->addr &&
363 fa->addr < (fb+1)->addr))
364 return 0;
366 if (fa->addr < fb->addr)
367 return -1;
369 return 1;
372 static int func_map_init(struct pevent *pevent)
374 struct func_list *funclist;
375 struct func_list *item;
376 struct func_map *func_map;
377 int i;
379 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
380 if (!func_map)
381 return -1;
383 funclist = pevent->funclist;
385 i = 0;
386 while (funclist) {
387 func_map[i].func = funclist->func;
388 func_map[i].addr = funclist->addr;
389 func_map[i].mod = funclist->mod;
390 i++;
391 item = funclist;
392 funclist = funclist->next;
393 free(item);
396 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
399 * Add a special record at the end.
401 func_map[pevent->func_count].func = NULL;
402 func_map[pevent->func_count].addr = 0;
403 func_map[pevent->func_count].mod = NULL;
405 pevent->func_map = func_map;
406 pevent->funclist = NULL;
408 return 0;
411 static struct func_map *
412 find_func(struct pevent *pevent, unsigned long long addr)
414 struct func_map *func;
415 struct func_map key;
417 if (!pevent->func_map)
418 func_map_init(pevent);
420 key.addr = addr;
422 func = bsearch(&key, pevent->func_map, pevent->func_count,
423 sizeof(*pevent->func_map), func_bcmp);
425 return func;
429 * pevent_find_function - find a function by a given address
430 * @pevent: handle for the pevent
431 * @addr: the address to find the function with
433 * Returns a pointer to the function stored that has the given
434 * address. Note, the address does not have to be exact, it
435 * will select the function that would contain the address.
437 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
439 struct func_map *map;
441 map = find_func(pevent, addr);
442 if (!map)
443 return NULL;
445 return map->func;
449 * pevent_find_function_address - find a function address by a given address
450 * @pevent: handle for the pevent
451 * @addr: the address to find the function with
453 * Returns the address the function starts at. This can be used in
454 * conjunction with pevent_find_function to print both the function
455 * name and the function offset.
457 unsigned long long
458 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
460 struct func_map *map;
462 map = find_func(pevent, addr);
463 if (!map)
464 return 0;
466 return map->addr;
470 * pevent_register_function - register a function with a given address
471 * @pevent: handle for the pevent
472 * @function: the function name to register
473 * @addr: the address the function starts at
474 * @mod: the kernel module the function may be in (NULL for none)
476 * This registers a function name with an address and module.
477 * The @func passed in is duplicated.
479 int pevent_register_function(struct pevent *pevent, char *func,
480 unsigned long long addr, char *mod)
482 struct func_list *item = malloc(sizeof(*item));
484 if (!item)
485 return -1;
487 item->next = pevent->funclist;
488 item->func = strdup(func);
489 if (!item->func)
490 goto out_free;
492 if (mod) {
493 item->mod = strdup(mod);
494 if (!item->mod)
495 goto out_free_func;
496 } else
497 item->mod = NULL;
498 item->addr = addr;
500 pevent->funclist = item;
501 pevent->func_count++;
503 return 0;
505 out_free_func:
506 free(item->func);
507 item->func = NULL;
508 out_free:
509 free(item);
510 errno = ENOMEM;
511 return -1;
515 * pevent_print_funcs - print out the stored functions
516 * @pevent: handle for the pevent
518 * This prints out the stored functions.
520 void pevent_print_funcs(struct pevent *pevent)
522 int i;
524 if (!pevent->func_map)
525 func_map_init(pevent);
527 for (i = 0; i < (int)pevent->func_count; i++) {
528 printf("%016llx %s",
529 pevent->func_map[i].addr,
530 pevent->func_map[i].func);
531 if (pevent->func_map[i].mod)
532 printf(" [%s]\n", pevent->func_map[i].mod);
533 else
534 printf("\n");
538 struct printk_map {
539 unsigned long long addr;
540 char *printk;
543 struct printk_list {
544 struct printk_list *next;
545 unsigned long long addr;
546 char *printk;
549 static int printk_cmp(const void *a, const void *b)
551 const struct printk_map *pa = a;
552 const struct printk_map *pb = b;
554 if (pa->addr < pb->addr)
555 return -1;
556 if (pa->addr > pb->addr)
557 return 1;
559 return 0;
562 static int printk_map_init(struct pevent *pevent)
564 struct printk_list *printklist;
565 struct printk_list *item;
566 struct printk_map *printk_map;
567 int i;
569 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
570 if (!printk_map)
571 return -1;
573 printklist = pevent->printklist;
575 i = 0;
576 while (printklist) {
577 printk_map[i].printk = printklist->printk;
578 printk_map[i].addr = printklist->addr;
579 i++;
580 item = printklist;
581 printklist = printklist->next;
582 free(item);
585 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
587 pevent->printk_map = printk_map;
588 pevent->printklist = NULL;
590 return 0;
593 static struct printk_map *
594 find_printk(struct pevent *pevent, unsigned long long addr)
596 struct printk_map *printk;
597 struct printk_map key;
599 if (!pevent->printk_map && printk_map_init(pevent))
600 return NULL;
602 key.addr = addr;
604 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
605 sizeof(*pevent->printk_map), printk_cmp);
607 return printk;
611 * pevent_register_print_string - register a string by its address
612 * @pevent: handle for the pevent
613 * @fmt: the string format to register
614 * @addr: the address the string was located at
616 * This registers a string by the address it was stored in the kernel.
617 * The @fmt passed in is duplicated.
619 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
620 unsigned long long addr)
622 struct printk_list *item = malloc(sizeof(*item));
623 char *p;
625 if (!item)
626 return -1;
628 item->next = pevent->printklist;
629 item->addr = addr;
631 /* Strip off quotes and '\n' from the end */
632 if (fmt[0] == '"')
633 fmt++;
634 item->printk = strdup(fmt);
635 if (!item->printk)
636 goto out_free;
638 p = item->printk + strlen(item->printk) - 1;
639 if (*p == '"')
640 *p = 0;
642 p -= 2;
643 if (strcmp(p, "\\n") == 0)
644 *p = 0;
646 pevent->printklist = item;
647 pevent->printk_count++;
649 return 0;
651 out_free:
652 free(item);
653 errno = ENOMEM;
654 return -1;
658 * pevent_print_printk - print out the stored strings
659 * @pevent: handle for the pevent
661 * This prints the string formats that were stored.
663 void pevent_print_printk(struct pevent *pevent)
665 int i;
667 if (!pevent->printk_map)
668 printk_map_init(pevent);
670 for (i = 0; i < (int)pevent->printk_count; i++) {
671 printf("%016llx %s\n",
672 pevent->printk_map[i].addr,
673 pevent->printk_map[i].printk);
677 static struct event_format *alloc_event(void)
679 return calloc(1, sizeof(struct event_format));
682 static int add_event(struct pevent *pevent, struct event_format *event)
684 int i;
685 struct event_format **events = realloc(pevent->events, sizeof(event) *
686 (pevent->nr_events + 1));
687 if (!events)
688 return -1;
690 pevent->events = events;
692 for (i = 0; i < pevent->nr_events; i++) {
693 if (pevent->events[i]->id > event->id)
694 break;
696 if (i < pevent->nr_events)
697 memmove(&pevent->events[i + 1],
698 &pevent->events[i],
699 sizeof(event) * (pevent->nr_events - i));
701 pevent->events[i] = event;
702 pevent->nr_events++;
704 event->pevent = pevent;
706 return 0;
709 static int event_item_type(enum event_type type)
711 switch (type) {
712 case EVENT_ITEM ... EVENT_SQUOTE:
713 return 1;
714 case EVENT_ERROR ... EVENT_DELIM:
715 default:
716 return 0;
720 static void free_flag_sym(struct print_flag_sym *fsym)
722 struct print_flag_sym *next;
724 while (fsym) {
725 next = fsym->next;
726 free(fsym->value);
727 free(fsym->str);
728 free(fsym);
729 fsym = next;
733 static void free_arg(struct print_arg *arg)
735 struct print_arg *farg;
737 if (!arg)
738 return;
740 switch (arg->type) {
741 case PRINT_ATOM:
742 free(arg->atom.atom);
743 break;
744 case PRINT_FIELD:
745 free(arg->field.name);
746 break;
747 case PRINT_FLAGS:
748 free_arg(arg->flags.field);
749 free(arg->flags.delim);
750 free_flag_sym(arg->flags.flags);
751 break;
752 case PRINT_SYMBOL:
753 free_arg(arg->symbol.field);
754 free_flag_sym(arg->symbol.symbols);
755 break;
756 case PRINT_HEX:
757 free_arg(arg->hex.field);
758 free_arg(arg->hex.size);
759 break;
760 case PRINT_TYPE:
761 free(arg->typecast.type);
762 free_arg(arg->typecast.item);
763 break;
764 case PRINT_STRING:
765 case PRINT_BSTRING:
766 free(arg->string.string);
767 break;
768 case PRINT_BITMASK:
769 free(arg->bitmask.bitmask);
770 break;
771 case PRINT_DYNAMIC_ARRAY:
772 free(arg->dynarray.index);
773 break;
774 case PRINT_OP:
775 free(arg->op.op);
776 free_arg(arg->op.left);
777 free_arg(arg->op.right);
778 break;
779 case PRINT_FUNC:
780 while (arg->func.args) {
781 farg = arg->func.args;
782 arg->func.args = farg->next;
783 free_arg(farg);
785 break;
787 case PRINT_NULL:
788 default:
789 break;
792 free(arg);
795 static enum event_type get_type(int ch)
797 if (ch == '\n')
798 return EVENT_NEWLINE;
799 if (isspace(ch))
800 return EVENT_SPACE;
801 if (isalnum(ch) || ch == '_')
802 return EVENT_ITEM;
803 if (ch == '\'')
804 return EVENT_SQUOTE;
805 if (ch == '"')
806 return EVENT_DQUOTE;
807 if (!isprint(ch))
808 return EVENT_NONE;
809 if (ch == '(' || ch == ')' || ch == ',')
810 return EVENT_DELIM;
812 return EVENT_OP;
815 static int __read_char(void)
817 if (input_buf_ptr >= input_buf_siz)
818 return -1;
820 return input_buf[input_buf_ptr++];
823 static int __peek_char(void)
825 if (input_buf_ptr >= input_buf_siz)
826 return -1;
828 return input_buf[input_buf_ptr];
832 * pevent_peek_char - peek at the next character that will be read
834 * Returns the next character read, or -1 if end of buffer.
836 int pevent_peek_char(void)
838 return __peek_char();
841 static int extend_token(char **tok, char *buf, int size)
843 char *newtok = realloc(*tok, size);
845 if (!newtok) {
846 free(*tok);
847 *tok = NULL;
848 return -1;
851 if (!*tok)
852 strcpy(newtok, buf);
853 else
854 strcat(newtok, buf);
855 *tok = newtok;
857 return 0;
860 static enum event_type force_token(const char *str, char **tok);
862 static enum event_type __read_token(char **tok)
864 char buf[BUFSIZ];
865 int ch, last_ch, quote_ch, next_ch;
866 int i = 0;
867 int tok_size = 0;
868 enum event_type type;
870 *tok = NULL;
873 ch = __read_char();
874 if (ch < 0)
875 return EVENT_NONE;
877 type = get_type(ch);
878 if (type == EVENT_NONE)
879 return type;
881 buf[i++] = ch;
883 switch (type) {
884 case EVENT_NEWLINE:
885 case EVENT_DELIM:
886 if (asprintf(tok, "%c", ch) < 0)
887 return EVENT_ERROR;
889 return type;
891 case EVENT_OP:
892 switch (ch) {
893 case '-':
894 next_ch = __peek_char();
895 if (next_ch == '>') {
896 buf[i++] = __read_char();
897 break;
899 /* fall through */
900 case '+':
901 case '|':
902 case '&':
903 case '>':
904 case '<':
905 last_ch = ch;
906 ch = __peek_char();
907 if (ch != last_ch)
908 goto test_equal;
909 buf[i++] = __read_char();
910 switch (last_ch) {
911 case '>':
912 case '<':
913 goto test_equal;
914 default:
915 break;
917 break;
918 case '!':
919 case '=':
920 goto test_equal;
921 default: /* what should we do instead? */
922 break;
924 buf[i] = 0;
925 *tok = strdup(buf);
926 return type;
928 test_equal:
929 ch = __peek_char();
930 if (ch == '=')
931 buf[i++] = __read_char();
932 goto out;
934 case EVENT_DQUOTE:
935 case EVENT_SQUOTE:
936 /* don't keep quotes */
937 i--;
938 quote_ch = ch;
939 last_ch = 0;
940 concat:
941 do {
942 if (i == (BUFSIZ - 1)) {
943 buf[i] = 0;
944 tok_size += BUFSIZ;
946 if (extend_token(tok, buf, tok_size) < 0)
947 return EVENT_NONE;
948 i = 0;
950 last_ch = ch;
951 ch = __read_char();
952 buf[i++] = ch;
953 /* the '\' '\' will cancel itself */
954 if (ch == '\\' && last_ch == '\\')
955 last_ch = 0;
956 } while (ch != quote_ch || last_ch == '\\');
957 /* remove the last quote */
958 i--;
961 * For strings (double quotes) check the next token.
962 * If it is another string, concatinate the two.
964 if (type == EVENT_DQUOTE) {
965 unsigned long long save_input_buf_ptr = input_buf_ptr;
967 do {
968 ch = __read_char();
969 } while (isspace(ch));
970 if (ch == '"')
971 goto concat;
972 input_buf_ptr = save_input_buf_ptr;
975 goto out;
977 case EVENT_ERROR ... EVENT_SPACE:
978 case EVENT_ITEM:
979 default:
980 break;
983 while (get_type(__peek_char()) == type) {
984 if (i == (BUFSIZ - 1)) {
985 buf[i] = 0;
986 tok_size += BUFSIZ;
988 if (extend_token(tok, buf, tok_size) < 0)
989 return EVENT_NONE;
990 i = 0;
992 ch = __read_char();
993 buf[i++] = ch;
996 out:
997 buf[i] = 0;
998 if (extend_token(tok, buf, tok_size + i + 1) < 0)
999 return EVENT_NONE;
1001 if (type == EVENT_ITEM) {
1003 * Older versions of the kernel has a bug that
1004 * creates invalid symbols and will break the mac80211
1005 * parsing. This is a work around to that bug.
1007 * See Linux kernel commit:
1008 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1010 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1011 free(*tok);
1012 *tok = NULL;
1013 return force_token("\"\%s\" ", tok);
1014 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1015 free(*tok);
1016 *tok = NULL;
1017 return force_token("\" sta:%pM\" ", tok);
1018 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1019 free(*tok);
1020 *tok = NULL;
1021 return force_token("\" vif:%p(%d)\" ", tok);
1025 return type;
1028 static enum event_type force_token(const char *str, char **tok)
1030 const char *save_input_buf;
1031 unsigned long long save_input_buf_ptr;
1032 unsigned long long save_input_buf_siz;
1033 enum event_type type;
1035 /* save off the current input pointers */
1036 save_input_buf = input_buf;
1037 save_input_buf_ptr = input_buf_ptr;
1038 save_input_buf_siz = input_buf_siz;
1040 init_input_buf(str, strlen(str));
1042 type = __read_token(tok);
1044 /* reset back to original token */
1045 input_buf = save_input_buf;
1046 input_buf_ptr = save_input_buf_ptr;
1047 input_buf_siz = save_input_buf_siz;
1049 return type;
1052 static void free_token(char *tok)
1054 if (tok)
1055 free(tok);
1058 static enum event_type read_token(char **tok)
1060 enum event_type type;
1062 for (;;) {
1063 type = __read_token(tok);
1064 if (type != EVENT_SPACE)
1065 return type;
1067 free_token(*tok);
1070 /* not reached */
1071 *tok = NULL;
1072 return EVENT_NONE;
1076 * pevent_read_token - access to utilites to use the pevent parser
1077 * @tok: The token to return
1079 * This will parse tokens from the string given by
1080 * pevent_init_data().
1082 * Returns the token type.
1084 enum event_type pevent_read_token(char **tok)
1086 return read_token(tok);
1090 * pevent_free_token - free a token returned by pevent_read_token
1091 * @token: the token to free
1093 void pevent_free_token(char *token)
1095 free_token(token);
1098 /* no newline */
1099 static enum event_type read_token_item(char **tok)
1101 enum event_type type;
1103 for (;;) {
1104 type = __read_token(tok);
1105 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1106 return type;
1107 free_token(*tok);
1108 *tok = NULL;
1111 /* not reached */
1112 *tok = NULL;
1113 return EVENT_NONE;
1116 static int test_type(enum event_type type, enum event_type expect)
1118 if (type != expect) {
1119 do_warning("Error: expected type %d but read %d",
1120 expect, type);
1121 return -1;
1123 return 0;
1126 static int test_type_token(enum event_type type, const char *token,
1127 enum event_type expect, const char *expect_tok)
1129 if (type != expect) {
1130 do_warning("Error: expected type %d but read %d",
1131 expect, type);
1132 return -1;
1135 if (strcmp(token, expect_tok) != 0) {
1136 do_warning("Error: expected '%s' but read '%s'",
1137 expect_tok, token);
1138 return -1;
1140 return 0;
1143 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1145 enum event_type type;
1147 if (newline_ok)
1148 type = read_token(tok);
1149 else
1150 type = read_token_item(tok);
1151 return test_type(type, expect);
1154 static int read_expect_type(enum event_type expect, char **tok)
1156 return __read_expect_type(expect, tok, 1);
1159 static int __read_expected(enum event_type expect, const char *str,
1160 int newline_ok)
1162 enum event_type type;
1163 char *token;
1164 int ret;
1166 if (newline_ok)
1167 type = read_token(&token);
1168 else
1169 type = read_token_item(&token);
1171 ret = test_type_token(type, token, expect, str);
1173 free_token(token);
1175 return ret;
1178 static int read_expected(enum event_type expect, const char *str)
1180 return __read_expected(expect, str, 1);
1183 static int read_expected_item(enum event_type expect, const char *str)
1185 return __read_expected(expect, str, 0);
1188 static char *event_read_name(void)
1190 char *token;
1192 if (read_expected(EVENT_ITEM, "name") < 0)
1193 return NULL;
1195 if (read_expected(EVENT_OP, ":") < 0)
1196 return NULL;
1198 if (read_expect_type(EVENT_ITEM, &token) < 0)
1199 goto fail;
1201 return token;
1203 fail:
1204 free_token(token);
1205 return NULL;
1208 static int event_read_id(void)
1210 char *token;
1211 int id;
1213 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1214 return -1;
1216 if (read_expected(EVENT_OP, ":") < 0)
1217 return -1;
1219 if (read_expect_type(EVENT_ITEM, &token) < 0)
1220 goto fail;
1222 id = strtoul(token, NULL, 0);
1223 free_token(token);
1224 return id;
1226 fail:
1227 free_token(token);
1228 return -1;
1231 static int field_is_string(struct format_field *field)
1233 if ((field->flags & FIELD_IS_ARRAY) &&
1234 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1235 strstr(field->type, "s8")))
1236 return 1;
1238 return 0;
1241 static int field_is_dynamic(struct format_field *field)
1243 if (strncmp(field->type, "__data_loc", 10) == 0)
1244 return 1;
1246 return 0;
1249 static int field_is_long(struct format_field *field)
1251 /* includes long long */
1252 if (strstr(field->type, "long"))
1253 return 1;
1255 return 0;
1258 static unsigned int type_size(const char *name)
1260 /* This covers all FIELD_IS_STRING types. */
1261 static struct {
1262 const char *type;
1263 unsigned int size;
1264 } table[] = {
1265 { "u8", 1 },
1266 { "u16", 2 },
1267 { "u32", 4 },
1268 { "u64", 8 },
1269 { "s8", 1 },
1270 { "s16", 2 },
1271 { "s32", 4 },
1272 { "s64", 8 },
1273 { "char", 1 },
1274 { },
1276 int i;
1278 for (i = 0; table[i].type; i++) {
1279 if (!strcmp(table[i].type, name))
1280 return table[i].size;
1283 return 0;
1286 static int event_read_fields(struct event_format *event, struct format_field **fields)
1288 struct format_field *field = NULL;
1289 enum event_type type;
1290 char *token;
1291 char *last_token;
1292 int count = 0;
1294 do {
1295 unsigned int size_dynamic = 0;
1297 type = read_token(&token);
1298 if (type == EVENT_NEWLINE) {
1299 free_token(token);
1300 return count;
1303 count++;
1305 if (test_type_token(type, token, EVENT_ITEM, "field"))
1306 goto fail;
1307 free_token(token);
1309 type = read_token(&token);
1311 * The ftrace fields may still use the "special" name.
1312 * Just ignore it.
1314 if (event->flags & EVENT_FL_ISFTRACE &&
1315 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1316 free_token(token);
1317 type = read_token(&token);
1320 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1321 goto fail;
1323 free_token(token);
1324 if (read_expect_type(EVENT_ITEM, &token) < 0)
1325 goto fail;
1327 last_token = token;
1329 field = calloc(1, sizeof(*field));
1330 if (!field)
1331 goto fail;
1333 field->event = event;
1335 /* read the rest of the type */
1336 for (;;) {
1337 type = read_token(&token);
1338 if (type == EVENT_ITEM ||
1339 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1341 * Some of the ftrace fields are broken and have
1342 * an illegal "." in them.
1344 (event->flags & EVENT_FL_ISFTRACE &&
1345 type == EVENT_OP && strcmp(token, ".") == 0)) {
1347 if (strcmp(token, "*") == 0)
1348 field->flags |= FIELD_IS_POINTER;
1350 if (field->type) {
1351 char *new_type;
1352 new_type = realloc(field->type,
1353 strlen(field->type) +
1354 strlen(last_token) + 2);
1355 if (!new_type) {
1356 free(last_token);
1357 goto fail;
1359 field->type = new_type;
1360 strcat(field->type, " ");
1361 strcat(field->type, last_token);
1362 free(last_token);
1363 } else
1364 field->type = last_token;
1365 last_token = token;
1366 continue;
1369 break;
1372 if (!field->type) {
1373 do_warning_event(event, "%s: no type found", __func__);
1374 goto fail;
1376 field->name = last_token;
1378 if (test_type(type, EVENT_OP))
1379 goto fail;
1381 if (strcmp(token, "[") == 0) {
1382 enum event_type last_type = type;
1383 char *brackets = token;
1384 char *new_brackets;
1385 int len;
1387 field->flags |= FIELD_IS_ARRAY;
1389 type = read_token(&token);
1391 if (type == EVENT_ITEM)
1392 field->arraylen = strtoul(token, NULL, 0);
1393 else
1394 field->arraylen = 0;
1396 while (strcmp(token, "]") != 0) {
1397 if (last_type == EVENT_ITEM &&
1398 type == EVENT_ITEM)
1399 len = 2;
1400 else
1401 len = 1;
1402 last_type = type;
1404 new_brackets = realloc(brackets,
1405 strlen(brackets) +
1406 strlen(token) + len);
1407 if (!new_brackets) {
1408 free(brackets);
1409 goto fail;
1411 brackets = new_brackets;
1412 if (len == 2)
1413 strcat(brackets, " ");
1414 strcat(brackets, token);
1415 /* We only care about the last token */
1416 field->arraylen = strtoul(token, NULL, 0);
1417 free_token(token);
1418 type = read_token(&token);
1419 if (type == EVENT_NONE) {
1420 do_warning_event(event, "failed to find token");
1421 goto fail;
1425 free_token(token);
1427 new_brackets = realloc(brackets, strlen(brackets) + 2);
1428 if (!new_brackets) {
1429 free(brackets);
1430 goto fail;
1432 brackets = new_brackets;
1433 strcat(brackets, "]");
1435 /* add brackets to type */
1437 type = read_token(&token);
1439 * If the next token is not an OP, then it is of
1440 * the format: type [] item;
1442 if (type == EVENT_ITEM) {
1443 char *new_type;
1444 new_type = realloc(field->type,
1445 strlen(field->type) +
1446 strlen(field->name) +
1447 strlen(brackets) + 2);
1448 if (!new_type) {
1449 free(brackets);
1450 goto fail;
1452 field->type = new_type;
1453 strcat(field->type, " ");
1454 strcat(field->type, field->name);
1455 size_dynamic = type_size(field->name);
1456 free_token(field->name);
1457 strcat(field->type, brackets);
1458 field->name = token;
1459 type = read_token(&token);
1460 } else {
1461 char *new_type;
1462 new_type = realloc(field->type,
1463 strlen(field->type) +
1464 strlen(brackets) + 1);
1465 if (!new_type) {
1466 free(brackets);
1467 goto fail;
1469 field->type = new_type;
1470 strcat(field->type, brackets);
1472 free(brackets);
1475 if (field_is_string(field))
1476 field->flags |= FIELD_IS_STRING;
1477 if (field_is_dynamic(field))
1478 field->flags |= FIELD_IS_DYNAMIC;
1479 if (field_is_long(field))
1480 field->flags |= FIELD_IS_LONG;
1482 if (test_type_token(type, token, EVENT_OP, ";"))
1483 goto fail;
1484 free_token(token);
1486 if (read_expected(EVENT_ITEM, "offset") < 0)
1487 goto fail_expect;
1489 if (read_expected(EVENT_OP, ":") < 0)
1490 goto fail_expect;
1492 if (read_expect_type(EVENT_ITEM, &token))
1493 goto fail;
1494 field->offset = strtoul(token, NULL, 0);
1495 free_token(token);
1497 if (read_expected(EVENT_OP, ";") < 0)
1498 goto fail_expect;
1500 if (read_expected(EVENT_ITEM, "size") < 0)
1501 goto fail_expect;
1503 if (read_expected(EVENT_OP, ":") < 0)
1504 goto fail_expect;
1506 if (read_expect_type(EVENT_ITEM, &token))
1507 goto fail;
1508 field->size = strtoul(token, NULL, 0);
1509 free_token(token);
1511 if (read_expected(EVENT_OP, ";") < 0)
1512 goto fail_expect;
1514 type = read_token(&token);
1515 if (type != EVENT_NEWLINE) {
1516 /* newer versions of the kernel have a "signed" type */
1517 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1518 goto fail;
1520 free_token(token);
1522 if (read_expected(EVENT_OP, ":") < 0)
1523 goto fail_expect;
1525 if (read_expect_type(EVENT_ITEM, &token))
1526 goto fail;
1528 if (strtoul(token, NULL, 0))
1529 field->flags |= FIELD_IS_SIGNED;
1531 free_token(token);
1532 if (read_expected(EVENT_OP, ";") < 0)
1533 goto fail_expect;
1535 if (read_expect_type(EVENT_NEWLINE, &token))
1536 goto fail;
1539 free_token(token);
1541 if (field->flags & FIELD_IS_ARRAY) {
1542 if (field->arraylen)
1543 field->elementsize = field->size / field->arraylen;
1544 else if (field->flags & FIELD_IS_DYNAMIC)
1545 field->elementsize = size_dynamic;
1546 else if (field->flags & FIELD_IS_STRING)
1547 field->elementsize = 1;
1548 else if (field->flags & FIELD_IS_LONG)
1549 field->elementsize = event->pevent ?
1550 event->pevent->long_size :
1551 sizeof(long);
1552 } else
1553 field->elementsize = field->size;
1555 *fields = field;
1556 fields = &field->next;
1558 } while (1);
1560 return 0;
1562 fail:
1563 free_token(token);
1564 fail_expect:
1565 if (field) {
1566 free(field->type);
1567 free(field->name);
1568 free(field);
1570 return -1;
1573 static int event_read_format(struct event_format *event)
1575 char *token;
1576 int ret;
1578 if (read_expected_item(EVENT_ITEM, "format") < 0)
1579 return -1;
1581 if (read_expected(EVENT_OP, ":") < 0)
1582 return -1;
1584 if (read_expect_type(EVENT_NEWLINE, &token))
1585 goto fail;
1586 free_token(token);
1588 ret = event_read_fields(event, &event->format.common_fields);
1589 if (ret < 0)
1590 return ret;
1591 event->format.nr_common = ret;
1593 ret = event_read_fields(event, &event->format.fields);
1594 if (ret < 0)
1595 return ret;
1596 event->format.nr_fields = ret;
1598 return 0;
1600 fail:
1601 free_token(token);
1602 return -1;
1605 static enum event_type
1606 process_arg_token(struct event_format *event, struct print_arg *arg,
1607 char **tok, enum event_type type);
1609 static enum event_type
1610 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1612 enum event_type type;
1613 char *token;
1615 type = read_token(&token);
1616 *tok = token;
1618 return process_arg_token(event, arg, tok, type);
1621 static enum event_type
1622 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1625 * For __print_symbolic() and __print_flags, we need to completely
1626 * evaluate the first argument, which defines what to print next.
1628 static enum event_type
1629 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1631 enum event_type type;
1633 type = process_arg(event, arg, tok);
1635 while (type == EVENT_OP) {
1636 type = process_op(event, arg, tok);
1639 return type;
1642 static enum event_type
1643 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1645 struct print_arg *arg, *left, *right;
1646 enum event_type type;
1647 char *token = NULL;
1649 arg = alloc_arg();
1650 left = alloc_arg();
1651 right = alloc_arg();
1653 if (!arg || !left || !right) {
1654 do_warning_event(event, "%s: not enough memory!", __func__);
1655 /* arg will be freed at out_free */
1656 free_arg(left);
1657 free_arg(right);
1658 goto out_free;
1661 arg->type = PRINT_OP;
1662 arg->op.left = left;
1663 arg->op.right = right;
1665 *tok = NULL;
1666 type = process_arg(event, left, &token);
1668 again:
1669 /* Handle other operations in the arguments */
1670 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1671 type = process_op(event, left, &token);
1672 goto again;
1675 if (test_type_token(type, token, EVENT_OP, ":"))
1676 goto out_free;
1678 arg->op.op = token;
1680 type = process_arg(event, right, &token);
1682 top->op.right = arg;
1684 *tok = token;
1685 return type;
1687 out_free:
1688 /* Top may point to itself */
1689 top->op.right = NULL;
1690 free_token(token);
1691 free_arg(arg);
1692 return EVENT_ERROR;
1695 static enum event_type
1696 process_array(struct event_format *event, struct print_arg *top, char **tok)
1698 struct print_arg *arg;
1699 enum event_type type;
1700 char *token = NULL;
1702 arg = alloc_arg();
1703 if (!arg) {
1704 do_warning_event(event, "%s: not enough memory!", __func__);
1705 /* '*tok' is set to top->op.op. No need to free. */
1706 *tok = NULL;
1707 return EVENT_ERROR;
1710 *tok = NULL;
1711 type = process_arg(event, arg, &token);
1712 if (test_type_token(type, token, EVENT_OP, "]"))
1713 goto out_free;
1715 top->op.right = arg;
1717 free_token(token);
1718 type = read_token_item(&token);
1719 *tok = token;
1721 return type;
1723 out_free:
1724 free_token(token);
1725 free_arg(arg);
1726 return EVENT_ERROR;
1729 static int get_op_prio(char *op)
1731 if (!op[1]) {
1732 switch (op[0]) {
1733 case '~':
1734 case '!':
1735 return 4;
1736 case '*':
1737 case '/':
1738 case '%':
1739 return 6;
1740 case '+':
1741 case '-':
1742 return 7;
1743 /* '>>' and '<<' are 8 */
1744 case '<':
1745 case '>':
1746 return 9;
1747 /* '==' and '!=' are 10 */
1748 case '&':
1749 return 11;
1750 case '^':
1751 return 12;
1752 case '|':
1753 return 13;
1754 case '?':
1755 return 16;
1756 default:
1757 do_warning("unknown op '%c'", op[0]);
1758 return -1;
1760 } else {
1761 if (strcmp(op, "++") == 0 ||
1762 strcmp(op, "--") == 0) {
1763 return 3;
1764 } else if (strcmp(op, ">>") == 0 ||
1765 strcmp(op, "<<") == 0) {
1766 return 8;
1767 } else if (strcmp(op, ">=") == 0 ||
1768 strcmp(op, "<=") == 0) {
1769 return 9;
1770 } else if (strcmp(op, "==") == 0 ||
1771 strcmp(op, "!=") == 0) {
1772 return 10;
1773 } else if (strcmp(op, "&&") == 0) {
1774 return 14;
1775 } else if (strcmp(op, "||") == 0) {
1776 return 15;
1777 } else {
1778 do_warning("unknown op '%s'", op);
1779 return -1;
1784 static int set_op_prio(struct print_arg *arg)
1787 /* single ops are the greatest */
1788 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1789 arg->op.prio = 0;
1790 else
1791 arg->op.prio = get_op_prio(arg->op.op);
1793 return arg->op.prio;
1796 /* Note, *tok does not get freed, but will most likely be saved */
1797 static enum event_type
1798 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1800 struct print_arg *left, *right = NULL;
1801 enum event_type type;
1802 char *token;
1804 /* the op is passed in via tok */
1805 token = *tok;
1807 if (arg->type == PRINT_OP && !arg->op.left) {
1808 /* handle single op */
1809 if (token[1]) {
1810 do_warning_event(event, "bad op token %s", token);
1811 goto out_free;
1813 switch (token[0]) {
1814 case '~':
1815 case '!':
1816 case '+':
1817 case '-':
1818 break;
1819 default:
1820 do_warning_event(event, "bad op token %s", token);
1821 goto out_free;
1825 /* make an empty left */
1826 left = alloc_arg();
1827 if (!left)
1828 goto out_warn_free;
1830 left->type = PRINT_NULL;
1831 arg->op.left = left;
1833 right = alloc_arg();
1834 if (!right)
1835 goto out_warn_free;
1837 arg->op.right = right;
1839 /* do not free the token, it belongs to an op */
1840 *tok = NULL;
1841 type = process_arg(event, right, tok);
1843 } else if (strcmp(token, "?") == 0) {
1845 left = alloc_arg();
1846 if (!left)
1847 goto out_warn_free;
1849 /* copy the top arg to the left */
1850 *left = *arg;
1852 arg->type = PRINT_OP;
1853 arg->op.op = token;
1854 arg->op.left = left;
1855 arg->op.prio = 0;
1857 /* it will set arg->op.right */
1858 type = process_cond(event, arg, tok);
1860 } else if (strcmp(token, ">>") == 0 ||
1861 strcmp(token, "<<") == 0 ||
1862 strcmp(token, "&") == 0 ||
1863 strcmp(token, "|") == 0 ||
1864 strcmp(token, "&&") == 0 ||
1865 strcmp(token, "||") == 0 ||
1866 strcmp(token, "-") == 0 ||
1867 strcmp(token, "+") == 0 ||
1868 strcmp(token, "*") == 0 ||
1869 strcmp(token, "^") == 0 ||
1870 strcmp(token, "/") == 0 ||
1871 strcmp(token, "<") == 0 ||
1872 strcmp(token, ">") == 0 ||
1873 strcmp(token, "<=") == 0 ||
1874 strcmp(token, ">=") == 0 ||
1875 strcmp(token, "==") == 0 ||
1876 strcmp(token, "!=") == 0) {
1878 left = alloc_arg();
1879 if (!left)
1880 goto out_warn_free;
1882 /* copy the top arg to the left */
1883 *left = *arg;
1885 arg->type = PRINT_OP;
1886 arg->op.op = token;
1887 arg->op.left = left;
1888 arg->op.right = NULL;
1890 if (set_op_prio(arg) == -1) {
1891 event->flags |= EVENT_FL_FAILED;
1892 /* arg->op.op (= token) will be freed at out_free */
1893 arg->op.op = NULL;
1894 goto out_free;
1897 type = read_token_item(&token);
1898 *tok = token;
1900 /* could just be a type pointer */
1901 if ((strcmp(arg->op.op, "*") == 0) &&
1902 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1903 char *new_atom;
1905 if (left->type != PRINT_ATOM) {
1906 do_warning_event(event, "bad pointer type");
1907 goto out_free;
1909 new_atom = realloc(left->atom.atom,
1910 strlen(left->atom.atom) + 3);
1911 if (!new_atom)
1912 goto out_warn_free;
1914 left->atom.atom = new_atom;
1915 strcat(left->atom.atom, " *");
1916 free(arg->op.op);
1917 *arg = *left;
1918 free(left);
1920 return type;
1923 right = alloc_arg();
1924 if (!right)
1925 goto out_warn_free;
1927 type = process_arg_token(event, right, tok, type);
1928 arg->op.right = right;
1930 } else if (strcmp(token, "[") == 0) {
1932 left = alloc_arg();
1933 if (!left)
1934 goto out_warn_free;
1936 *left = *arg;
1938 arg->type = PRINT_OP;
1939 arg->op.op = token;
1940 arg->op.left = left;
1942 arg->op.prio = 0;
1944 /* it will set arg->op.right */
1945 type = process_array(event, arg, tok);
1947 } else {
1948 do_warning_event(event, "unknown op '%s'", token);
1949 event->flags |= EVENT_FL_FAILED;
1950 /* the arg is now the left side */
1951 goto out_free;
1954 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1955 int prio;
1957 /* higher prios need to be closer to the root */
1958 prio = get_op_prio(*tok);
1960 if (prio > arg->op.prio)
1961 return process_op(event, arg, tok);
1963 return process_op(event, right, tok);
1966 return type;
1968 out_warn_free:
1969 do_warning_event(event, "%s: not enough memory!", __func__);
1970 out_free:
1971 free_token(token);
1972 *tok = NULL;
1973 return EVENT_ERROR;
1976 static enum event_type
1977 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1978 char **tok)
1980 enum event_type type;
1981 char *field;
1982 char *token;
1984 if (read_expected(EVENT_OP, "->") < 0)
1985 goto out_err;
1987 if (read_expect_type(EVENT_ITEM, &token) < 0)
1988 goto out_free;
1989 field = token;
1991 arg->type = PRINT_FIELD;
1992 arg->field.name = field;
1994 if (is_flag_field) {
1995 arg->field.field = pevent_find_any_field(event, arg->field.name);
1996 arg->field.field->flags |= FIELD_IS_FLAG;
1997 is_flag_field = 0;
1998 } else if (is_symbolic_field) {
1999 arg->field.field = pevent_find_any_field(event, arg->field.name);
2000 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2001 is_symbolic_field = 0;
2004 type = read_token(&token);
2005 *tok = token;
2007 return type;
2009 out_free:
2010 free_token(token);
2011 out_err:
2012 *tok = NULL;
2013 return EVENT_ERROR;
2016 static char *arg_eval (struct print_arg *arg);
2018 static unsigned long long
2019 eval_type_str(unsigned long long val, const char *type, int pointer)
2021 int sign = 0;
2022 char *ref;
2023 int len;
2025 len = strlen(type);
2027 if (pointer) {
2029 if (type[len-1] != '*') {
2030 do_warning("pointer expected with non pointer type");
2031 return val;
2034 ref = malloc(len);
2035 if (!ref) {
2036 do_warning("%s: not enough memory!", __func__);
2037 return val;
2039 memcpy(ref, type, len);
2041 /* chop off the " *" */
2042 ref[len - 2] = 0;
2044 val = eval_type_str(val, ref, 0);
2045 free(ref);
2046 return val;
2049 /* check if this is a pointer */
2050 if (type[len - 1] == '*')
2051 return val;
2053 /* Try to figure out the arg size*/
2054 if (strncmp(type, "struct", 6) == 0)
2055 /* all bets off */
2056 return val;
2058 if (strcmp(type, "u8") == 0)
2059 return val & 0xff;
2061 if (strcmp(type, "u16") == 0)
2062 return val & 0xffff;
2064 if (strcmp(type, "u32") == 0)
2065 return val & 0xffffffff;
2067 if (strcmp(type, "u64") == 0 ||
2068 strcmp(type, "s64"))
2069 return val;
2071 if (strcmp(type, "s8") == 0)
2072 return (unsigned long long)(char)val & 0xff;
2074 if (strcmp(type, "s16") == 0)
2075 return (unsigned long long)(short)val & 0xffff;
2077 if (strcmp(type, "s32") == 0)
2078 return (unsigned long long)(int)val & 0xffffffff;
2080 if (strncmp(type, "unsigned ", 9) == 0) {
2081 sign = 0;
2082 type += 9;
2085 if (strcmp(type, "char") == 0) {
2086 if (sign)
2087 return (unsigned long long)(char)val & 0xff;
2088 else
2089 return val & 0xff;
2092 if (strcmp(type, "short") == 0) {
2093 if (sign)
2094 return (unsigned long long)(short)val & 0xffff;
2095 else
2096 return val & 0xffff;
2099 if (strcmp(type, "int") == 0) {
2100 if (sign)
2101 return (unsigned long long)(int)val & 0xffffffff;
2102 else
2103 return val & 0xffffffff;
2106 return val;
2110 * Try to figure out the type.
2112 static unsigned long long
2113 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2115 if (arg->type != PRINT_TYPE) {
2116 do_warning("expected type argument");
2117 return 0;
2120 return eval_type_str(val, arg->typecast.type, pointer);
2123 static int arg_num_eval(struct print_arg *arg, long long *val)
2125 long long left, right;
2126 int ret = 1;
2128 switch (arg->type) {
2129 case PRINT_ATOM:
2130 *val = strtoll(arg->atom.atom, NULL, 0);
2131 break;
2132 case PRINT_TYPE:
2133 ret = arg_num_eval(arg->typecast.item, val);
2134 if (!ret)
2135 break;
2136 *val = eval_type(*val, arg, 0);
2137 break;
2138 case PRINT_OP:
2139 switch (arg->op.op[0]) {
2140 case '|':
2141 ret = arg_num_eval(arg->op.left, &left);
2142 if (!ret)
2143 break;
2144 ret = arg_num_eval(arg->op.right, &right);
2145 if (!ret)
2146 break;
2147 if (arg->op.op[1])
2148 *val = left || right;
2149 else
2150 *val = left | right;
2151 break;
2152 case '&':
2153 ret = arg_num_eval(arg->op.left, &left);
2154 if (!ret)
2155 break;
2156 ret = arg_num_eval(arg->op.right, &right);
2157 if (!ret)
2158 break;
2159 if (arg->op.op[1])
2160 *val = left && right;
2161 else
2162 *val = left & right;
2163 break;
2164 case '<':
2165 ret = arg_num_eval(arg->op.left, &left);
2166 if (!ret)
2167 break;
2168 ret = arg_num_eval(arg->op.right, &right);
2169 if (!ret)
2170 break;
2171 switch (arg->op.op[1]) {
2172 case 0:
2173 *val = left < right;
2174 break;
2175 case '<':
2176 *val = left << right;
2177 break;
2178 case '=':
2179 *val = left <= right;
2180 break;
2181 default:
2182 do_warning("unknown op '%s'", arg->op.op);
2183 ret = 0;
2185 break;
2186 case '>':
2187 ret = arg_num_eval(arg->op.left, &left);
2188 if (!ret)
2189 break;
2190 ret = arg_num_eval(arg->op.right, &right);
2191 if (!ret)
2192 break;
2193 switch (arg->op.op[1]) {
2194 case 0:
2195 *val = left > right;
2196 break;
2197 case '>':
2198 *val = left >> right;
2199 break;
2200 case '=':
2201 *val = left >= right;
2202 break;
2203 default:
2204 do_warning("unknown op '%s'", arg->op.op);
2205 ret = 0;
2207 break;
2208 case '=':
2209 ret = arg_num_eval(arg->op.left, &left);
2210 if (!ret)
2211 break;
2212 ret = arg_num_eval(arg->op.right, &right);
2213 if (!ret)
2214 break;
2216 if (arg->op.op[1] != '=') {
2217 do_warning("unknown op '%s'", arg->op.op);
2218 ret = 0;
2219 } else
2220 *val = left == right;
2221 break;
2222 case '!':
2223 ret = arg_num_eval(arg->op.left, &left);
2224 if (!ret)
2225 break;
2226 ret = arg_num_eval(arg->op.right, &right);
2227 if (!ret)
2228 break;
2230 switch (arg->op.op[1]) {
2231 case '=':
2232 *val = left != right;
2233 break;
2234 default:
2235 do_warning("unknown op '%s'", arg->op.op);
2236 ret = 0;
2238 break;
2239 case '-':
2240 /* check for negative */
2241 if (arg->op.left->type == PRINT_NULL)
2242 left = 0;
2243 else
2244 ret = arg_num_eval(arg->op.left, &left);
2245 if (!ret)
2246 break;
2247 ret = arg_num_eval(arg->op.right, &right);
2248 if (!ret)
2249 break;
2250 *val = left - right;
2251 break;
2252 case '+':
2253 if (arg->op.left->type == PRINT_NULL)
2254 left = 0;
2255 else
2256 ret = arg_num_eval(arg->op.left, &left);
2257 if (!ret)
2258 break;
2259 ret = arg_num_eval(arg->op.right, &right);
2260 if (!ret)
2261 break;
2262 *val = left + right;
2263 break;
2264 default:
2265 do_warning("unknown op '%s'", arg->op.op);
2266 ret = 0;
2268 break;
2270 case PRINT_NULL:
2271 case PRINT_FIELD ... PRINT_SYMBOL:
2272 case PRINT_STRING:
2273 case PRINT_BSTRING:
2274 case PRINT_BITMASK:
2275 default:
2276 do_warning("invalid eval type %d", arg->type);
2277 ret = 0;
2280 return ret;
2283 static char *arg_eval (struct print_arg *arg)
2285 long long val;
2286 static char buf[20];
2288 switch (arg->type) {
2289 case PRINT_ATOM:
2290 return arg->atom.atom;
2291 case PRINT_TYPE:
2292 return arg_eval(arg->typecast.item);
2293 case PRINT_OP:
2294 if (!arg_num_eval(arg, &val))
2295 break;
2296 sprintf(buf, "%lld", val);
2297 return buf;
2299 case PRINT_NULL:
2300 case PRINT_FIELD ... PRINT_SYMBOL:
2301 case PRINT_STRING:
2302 case PRINT_BSTRING:
2303 case PRINT_BITMASK:
2304 default:
2305 do_warning("invalid eval type %d", arg->type);
2306 break;
2309 return NULL;
2312 static enum event_type
2313 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2315 enum event_type type;
2316 struct print_arg *arg = NULL;
2317 struct print_flag_sym *field;
2318 char *token = *tok;
2319 char *value;
2321 do {
2322 free_token(token);
2323 type = read_token_item(&token);
2324 if (test_type_token(type, token, EVENT_OP, "{"))
2325 break;
2327 arg = alloc_arg();
2328 if (!arg)
2329 goto out_free;
2331 free_token(token);
2332 type = process_arg(event, arg, &token);
2334 if (type == EVENT_OP)
2335 type = process_op(event, arg, &token);
2337 if (type == EVENT_ERROR)
2338 goto out_free;
2340 if (test_type_token(type, token, EVENT_DELIM, ","))
2341 goto out_free;
2343 field = calloc(1, sizeof(*field));
2344 if (!field)
2345 goto out_free;
2347 value = arg_eval(arg);
2348 if (value == NULL)
2349 goto out_free_field;
2350 field->value = strdup(value);
2351 if (field->value == NULL)
2352 goto out_free_field;
2354 free_arg(arg);
2355 arg = alloc_arg();
2356 if (!arg)
2357 goto out_free;
2359 free_token(token);
2360 type = process_arg(event, arg, &token);
2361 if (test_type_token(type, token, EVENT_OP, "}"))
2362 goto out_free_field;
2364 value = arg_eval(arg);
2365 if (value == NULL)
2366 goto out_free_field;
2367 field->str = strdup(value);
2368 if (field->str == NULL)
2369 goto out_free_field;
2370 free_arg(arg);
2371 arg = NULL;
2373 *list = field;
2374 list = &field->next;
2376 free_token(token);
2377 type = read_token_item(&token);
2378 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2380 *tok = token;
2381 return type;
2383 out_free_field:
2384 free_flag_sym(field);
2385 out_free:
2386 free_arg(arg);
2387 free_token(token);
2388 *tok = NULL;
2390 return EVENT_ERROR;
2393 static enum event_type
2394 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2396 struct print_arg *field;
2397 enum event_type type;
2398 char *token = NULL;
2400 memset(arg, 0, sizeof(*arg));
2401 arg->type = PRINT_FLAGS;
2403 field = alloc_arg();
2404 if (!field) {
2405 do_warning_event(event, "%s: not enough memory!", __func__);
2406 goto out_free;
2409 type = process_field_arg(event, field, &token);
2411 /* Handle operations in the first argument */
2412 while (type == EVENT_OP)
2413 type = process_op(event, field, &token);
2415 if (test_type_token(type, token, EVENT_DELIM, ","))
2416 goto out_free_field;
2417 free_token(token);
2419 arg->flags.field = field;
2421 type = read_token_item(&token);
2422 if (event_item_type(type)) {
2423 arg->flags.delim = token;
2424 type = read_token_item(&token);
2427 if (test_type_token(type, token, EVENT_DELIM, ","))
2428 goto out_free;
2430 type = process_fields(event, &arg->flags.flags, &token);
2431 if (test_type_token(type, token, EVENT_DELIM, ")"))
2432 goto out_free;
2434 free_token(token);
2435 type = read_token_item(tok);
2436 return type;
2438 out_free_field:
2439 free_arg(field);
2440 out_free:
2441 free_token(token);
2442 *tok = NULL;
2443 return EVENT_ERROR;
2446 static enum event_type
2447 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2449 struct print_arg *field;
2450 enum event_type type;
2451 char *token = NULL;
2453 memset(arg, 0, sizeof(*arg));
2454 arg->type = PRINT_SYMBOL;
2456 field = alloc_arg();
2457 if (!field) {
2458 do_warning_event(event, "%s: not enough memory!", __func__);
2459 goto out_free;
2462 type = process_field_arg(event, field, &token);
2464 if (test_type_token(type, token, EVENT_DELIM, ","))
2465 goto out_free_field;
2467 arg->symbol.field = field;
2469 type = process_fields(event, &arg->symbol.symbols, &token);
2470 if (test_type_token(type, token, EVENT_DELIM, ")"))
2471 goto out_free;
2473 free_token(token);
2474 type = read_token_item(tok);
2475 return type;
2477 out_free_field:
2478 free_arg(field);
2479 out_free:
2480 free_token(token);
2481 *tok = NULL;
2482 return EVENT_ERROR;
2485 static enum event_type
2486 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2488 struct print_arg *field;
2489 enum event_type type;
2490 char *token = NULL;
2492 memset(arg, 0, sizeof(*arg));
2493 arg->type = PRINT_HEX;
2495 field = alloc_arg();
2496 if (!field) {
2497 do_warning_event(event, "%s: not enough memory!", __func__);
2498 goto out_free;
2501 type = process_arg(event, field, &token);
2503 if (test_type_token(type, token, EVENT_DELIM, ","))
2504 goto out_free;
2506 arg->hex.field = field;
2508 free_token(token);
2510 field = alloc_arg();
2511 if (!field) {
2512 do_warning_event(event, "%s: not enough memory!", __func__);
2513 *tok = NULL;
2514 return EVENT_ERROR;
2517 type = process_arg(event, field, &token);
2519 if (test_type_token(type, token, EVENT_DELIM, ")"))
2520 goto out_free;
2522 arg->hex.size = field;
2524 free_token(token);
2525 type = read_token_item(tok);
2526 return type;
2528 out_free:
2529 free_arg(field);
2530 free_token(token);
2531 *tok = NULL;
2532 return EVENT_ERROR;
2535 static enum event_type
2536 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2538 struct format_field *field;
2539 enum event_type type;
2540 char *token;
2542 memset(arg, 0, sizeof(*arg));
2543 arg->type = PRINT_DYNAMIC_ARRAY;
2546 * The item within the parenthesis is another field that holds
2547 * the index into where the array starts.
2549 type = read_token(&token);
2550 *tok = token;
2551 if (type != EVENT_ITEM)
2552 goto out_free;
2554 /* Find the field */
2556 field = pevent_find_field(event, token);
2557 if (!field)
2558 goto out_free;
2560 arg->dynarray.field = field;
2561 arg->dynarray.index = 0;
2563 if (read_expected(EVENT_DELIM, ")") < 0)
2564 goto out_free;
2566 free_token(token);
2567 type = read_token_item(&token);
2568 *tok = token;
2569 if (type != EVENT_OP || strcmp(token, "[") != 0)
2570 return type;
2572 free_token(token);
2573 arg = alloc_arg();
2574 if (!arg) {
2575 do_warning_event(event, "%s: not enough memory!", __func__);
2576 *tok = NULL;
2577 return EVENT_ERROR;
2580 type = process_arg(event, arg, &token);
2581 if (type == EVENT_ERROR)
2582 goto out_free_arg;
2584 if (!test_type_token(type, token, EVENT_OP, "]"))
2585 goto out_free_arg;
2587 free_token(token);
2588 type = read_token_item(tok);
2589 return type;
2591 out_free_arg:
2592 free_arg(arg);
2593 out_free:
2594 free_token(token);
2595 *tok = NULL;
2596 return EVENT_ERROR;
2599 static enum event_type
2600 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2602 struct print_arg *item_arg;
2603 enum event_type type;
2604 char *token;
2606 type = process_arg(event, arg, &token);
2608 if (type == EVENT_ERROR)
2609 goto out_free;
2611 if (type == EVENT_OP)
2612 type = process_op(event, arg, &token);
2614 if (type == EVENT_ERROR)
2615 goto out_free;
2617 if (test_type_token(type, token, EVENT_DELIM, ")"))
2618 goto out_free;
2620 free_token(token);
2621 type = read_token_item(&token);
2624 * If the next token is an item or another open paren, then
2625 * this was a typecast.
2627 if (event_item_type(type) ||
2628 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2630 /* make this a typecast and contine */
2632 /* prevous must be an atom */
2633 if (arg->type != PRINT_ATOM) {
2634 do_warning_event(event, "previous needed to be PRINT_ATOM");
2635 goto out_free;
2638 item_arg = alloc_arg();
2639 if (!item_arg) {
2640 do_warning_event(event, "%s: not enough memory!",
2641 __func__);
2642 goto out_free;
2645 arg->type = PRINT_TYPE;
2646 arg->typecast.type = arg->atom.atom;
2647 arg->typecast.item = item_arg;
2648 type = process_arg_token(event, item_arg, &token, type);
2652 *tok = token;
2653 return type;
2655 out_free:
2656 free_token(token);
2657 *tok = NULL;
2658 return EVENT_ERROR;
2662 static enum event_type
2663 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2664 char **tok)
2666 enum event_type type;
2667 char *token;
2669 if (read_expect_type(EVENT_ITEM, &token) < 0)
2670 goto out_free;
2672 arg->type = PRINT_STRING;
2673 arg->string.string = token;
2674 arg->string.offset = -1;
2676 if (read_expected(EVENT_DELIM, ")") < 0)
2677 goto out_err;
2679 type = read_token(&token);
2680 *tok = token;
2682 return type;
2684 out_free:
2685 free_token(token);
2686 out_err:
2687 *tok = NULL;
2688 return EVENT_ERROR;
2691 static enum event_type
2692 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2693 char **tok)
2695 enum event_type type;
2696 char *token;
2698 if (read_expect_type(EVENT_ITEM, &token) < 0)
2699 goto out_free;
2701 arg->type = PRINT_BITMASK;
2702 arg->bitmask.bitmask = token;
2703 arg->bitmask.offset = -1;
2705 if (read_expected(EVENT_DELIM, ")") < 0)
2706 goto out_err;
2708 type = read_token(&token);
2709 *tok = token;
2711 return type;
2713 out_free:
2714 free_token(token);
2715 out_err:
2716 *tok = NULL;
2717 return EVENT_ERROR;
2720 static struct pevent_function_handler *
2721 find_func_handler(struct pevent *pevent, char *func_name)
2723 struct pevent_function_handler *func;
2725 if (!pevent)
2726 return NULL;
2728 for (func = pevent->func_handlers; func; func = func->next) {
2729 if (strcmp(func->name, func_name) == 0)
2730 break;
2733 return func;
2736 static void remove_func_handler(struct pevent *pevent, char *func_name)
2738 struct pevent_function_handler *func;
2739 struct pevent_function_handler **next;
2741 next = &pevent->func_handlers;
2742 while ((func = *next)) {
2743 if (strcmp(func->name, func_name) == 0) {
2744 *next = func->next;
2745 free_func_handle(func);
2746 break;
2748 next = &func->next;
2752 static enum event_type
2753 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2754 struct print_arg *arg, char **tok)
2756 struct print_arg **next_arg;
2757 struct print_arg *farg;
2758 enum event_type type;
2759 char *token;
2760 int i;
2762 arg->type = PRINT_FUNC;
2763 arg->func.func = func;
2765 *tok = NULL;
2767 next_arg = &(arg->func.args);
2768 for (i = 0; i < func->nr_args; i++) {
2769 farg = alloc_arg();
2770 if (!farg) {
2771 do_warning_event(event, "%s: not enough memory!",
2772 __func__);
2773 return EVENT_ERROR;
2776 type = process_arg(event, farg, &token);
2777 if (i < (func->nr_args - 1)) {
2778 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2779 do_warning_event(event,
2780 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2781 func->name, func->nr_args,
2782 event->name, i + 1);
2783 goto err;
2785 } else {
2786 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2787 do_warning_event(event,
2788 "Error: function '%s()' only expects %d arguments but event %s has more",
2789 func->name, func->nr_args, event->name);
2790 goto err;
2794 *next_arg = farg;
2795 next_arg = &(farg->next);
2796 free_token(token);
2799 type = read_token(&token);
2800 *tok = token;
2802 return type;
2804 err:
2805 free_arg(farg);
2806 free_token(token);
2807 return EVENT_ERROR;
2810 static enum event_type
2811 process_function(struct event_format *event, struct print_arg *arg,
2812 char *token, char **tok)
2814 struct pevent_function_handler *func;
2816 if (strcmp(token, "__print_flags") == 0) {
2817 free_token(token);
2818 is_flag_field = 1;
2819 return process_flags(event, arg, tok);
2821 if (strcmp(token, "__print_symbolic") == 0) {
2822 free_token(token);
2823 is_symbolic_field = 1;
2824 return process_symbols(event, arg, tok);
2826 if (strcmp(token, "__print_hex") == 0) {
2827 free_token(token);
2828 return process_hex(event, arg, tok);
2830 if (strcmp(token, "__get_str") == 0) {
2831 free_token(token);
2832 return process_str(event, arg, tok);
2834 if (strcmp(token, "__get_bitmask") == 0) {
2835 free_token(token);
2836 return process_bitmask(event, arg, tok);
2838 if (strcmp(token, "__get_dynamic_array") == 0) {
2839 free_token(token);
2840 return process_dynamic_array(event, arg, tok);
2843 func = find_func_handler(event->pevent, token);
2844 if (func) {
2845 free_token(token);
2846 return process_func_handler(event, func, arg, tok);
2849 do_warning_event(event, "function %s not defined", token);
2850 free_token(token);
2851 return EVENT_ERROR;
2854 static enum event_type
2855 process_arg_token(struct event_format *event, struct print_arg *arg,
2856 char **tok, enum event_type type)
2858 char *token;
2859 char *atom;
2861 token = *tok;
2863 switch (type) {
2864 case EVENT_ITEM:
2865 if (strcmp(token, "REC") == 0) {
2866 free_token(token);
2867 type = process_entry(event, arg, &token);
2868 break;
2870 atom = token;
2871 /* test the next token */
2872 type = read_token_item(&token);
2875 * If the next token is a parenthesis, then this
2876 * is a function.
2878 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2879 free_token(token);
2880 token = NULL;
2881 /* this will free atom. */
2882 type = process_function(event, arg, atom, &token);
2883 break;
2885 /* atoms can be more than one token long */
2886 while (type == EVENT_ITEM) {
2887 char *new_atom;
2888 new_atom = realloc(atom,
2889 strlen(atom) + strlen(token) + 2);
2890 if (!new_atom) {
2891 free(atom);
2892 *tok = NULL;
2893 free_token(token);
2894 return EVENT_ERROR;
2896 atom = new_atom;
2897 strcat(atom, " ");
2898 strcat(atom, token);
2899 free_token(token);
2900 type = read_token_item(&token);
2903 arg->type = PRINT_ATOM;
2904 arg->atom.atom = atom;
2905 break;
2907 case EVENT_DQUOTE:
2908 case EVENT_SQUOTE:
2909 arg->type = PRINT_ATOM;
2910 arg->atom.atom = token;
2911 type = read_token_item(&token);
2912 break;
2913 case EVENT_DELIM:
2914 if (strcmp(token, "(") == 0) {
2915 free_token(token);
2916 type = process_paren(event, arg, &token);
2917 break;
2919 case EVENT_OP:
2920 /* handle single ops */
2921 arg->type = PRINT_OP;
2922 arg->op.op = token;
2923 arg->op.left = NULL;
2924 type = process_op(event, arg, &token);
2926 /* On error, the op is freed */
2927 if (type == EVENT_ERROR)
2928 arg->op.op = NULL;
2930 /* return error type if errored */
2931 break;
2933 case EVENT_ERROR ... EVENT_NEWLINE:
2934 default:
2935 do_warning_event(event, "unexpected type %d", type);
2936 return EVENT_ERROR;
2938 *tok = token;
2940 return type;
2943 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2945 enum event_type type = EVENT_ERROR;
2946 struct print_arg *arg;
2947 char *token;
2948 int args = 0;
2950 do {
2951 if (type == EVENT_NEWLINE) {
2952 type = read_token_item(&token);
2953 continue;
2956 arg = alloc_arg();
2957 if (!arg) {
2958 do_warning_event(event, "%s: not enough memory!",
2959 __func__);
2960 return -1;
2963 type = process_arg(event, arg, &token);
2965 if (type == EVENT_ERROR) {
2966 free_token(token);
2967 free_arg(arg);
2968 return -1;
2971 *list = arg;
2972 args++;
2974 if (type == EVENT_OP) {
2975 type = process_op(event, arg, &token);
2976 free_token(token);
2977 if (type == EVENT_ERROR) {
2978 *list = NULL;
2979 free_arg(arg);
2980 return -1;
2982 list = &arg->next;
2983 continue;
2986 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2987 free_token(token);
2988 *list = arg;
2989 list = &arg->next;
2990 continue;
2992 break;
2993 } while (type != EVENT_NONE);
2995 if (type != EVENT_NONE && type != EVENT_ERROR)
2996 free_token(token);
2998 return args;
3001 static int event_read_print(struct event_format *event)
3003 enum event_type type;
3004 char *token;
3005 int ret;
3007 if (read_expected_item(EVENT_ITEM, "print") < 0)
3008 return -1;
3010 if (read_expected(EVENT_ITEM, "fmt") < 0)
3011 return -1;
3013 if (read_expected(EVENT_OP, ":") < 0)
3014 return -1;
3016 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3017 goto fail;
3019 concat:
3020 event->print_fmt.format = token;
3021 event->print_fmt.args = NULL;
3023 /* ok to have no arg */
3024 type = read_token_item(&token);
3026 if (type == EVENT_NONE)
3027 return 0;
3029 /* Handle concatenation of print lines */
3030 if (type == EVENT_DQUOTE) {
3031 char *cat;
3033 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3034 goto fail;
3035 free_token(token);
3036 free_token(event->print_fmt.format);
3037 event->print_fmt.format = NULL;
3038 token = cat;
3039 goto concat;
3042 if (test_type_token(type, token, EVENT_DELIM, ","))
3043 goto fail;
3045 free_token(token);
3047 ret = event_read_print_args(event, &event->print_fmt.args);
3048 if (ret < 0)
3049 return -1;
3051 return ret;
3053 fail:
3054 free_token(token);
3055 return -1;
3059 * pevent_find_common_field - return a common field by event
3060 * @event: handle for the event
3061 * @name: the name of the common field to return
3063 * Returns a common field from the event by the given @name.
3064 * This only searchs the common fields and not all field.
3066 struct format_field *
3067 pevent_find_common_field(struct event_format *event, const char *name)
3069 struct format_field *format;
3071 for (format = event->format.common_fields;
3072 format; format = format->next) {
3073 if (strcmp(format->name, name) == 0)
3074 break;
3077 return format;
3081 * pevent_find_field - find a non-common field
3082 * @event: handle for the event
3083 * @name: the name of the non-common field
3085 * Returns a non-common field by the given @name.
3086 * This does not search common fields.
3088 struct format_field *
3089 pevent_find_field(struct event_format *event, const char *name)
3091 struct format_field *format;
3093 for (format = event->format.fields;
3094 format; format = format->next) {
3095 if (strcmp(format->name, name) == 0)
3096 break;
3099 return format;
3103 * pevent_find_any_field - find any field by name
3104 * @event: handle for the event
3105 * @name: the name of the field
3107 * Returns a field by the given @name.
3108 * This searchs the common field names first, then
3109 * the non-common ones if a common one was not found.
3111 struct format_field *
3112 pevent_find_any_field(struct event_format *event, const char *name)
3114 struct format_field *format;
3116 format = pevent_find_common_field(event, name);
3117 if (format)
3118 return format;
3119 return pevent_find_field(event, name);
3123 * pevent_read_number - read a number from data
3124 * @pevent: handle for the pevent
3125 * @ptr: the raw data
3126 * @size: the size of the data that holds the number
3128 * Returns the number (converted to host) from the
3129 * raw data.
3131 unsigned long long pevent_read_number(struct pevent *pevent,
3132 const void *ptr, int size)
3134 switch (size) {
3135 case 1:
3136 return *(unsigned char *)ptr;
3137 case 2:
3138 return data2host2(pevent, ptr);
3139 case 4:
3140 return data2host4(pevent, ptr);
3141 case 8:
3142 return data2host8(pevent, ptr);
3143 default:
3144 /* BUG! */
3145 return 0;
3150 * pevent_read_number_field - read a number from data
3151 * @field: a handle to the field
3152 * @data: the raw data to read
3153 * @value: the value to place the number in
3155 * Reads raw data according to a field offset and size,
3156 * and translates it into @value.
3158 * Returns 0 on success, -1 otherwise.
3160 int pevent_read_number_field(struct format_field *field, const void *data,
3161 unsigned long long *value)
3163 if (!field)
3164 return -1;
3165 switch (field->size) {
3166 case 1:
3167 case 2:
3168 case 4:
3169 case 8:
3170 *value = pevent_read_number(field->event->pevent,
3171 data + field->offset, field->size);
3172 return 0;
3173 default:
3174 return -1;
3178 static int get_common_info(struct pevent *pevent,
3179 const char *type, int *offset, int *size)
3181 struct event_format *event;
3182 struct format_field *field;
3185 * All events should have the same common elements.
3186 * Pick any event to find where the type is;
3188 if (!pevent->events) {
3189 do_warning("no event_list!");
3190 return -1;
3193 event = pevent->events[0];
3194 field = pevent_find_common_field(event, type);
3195 if (!field)
3196 return -1;
3198 *offset = field->offset;
3199 *size = field->size;
3201 return 0;
3204 static int __parse_common(struct pevent *pevent, void *data,
3205 int *size, int *offset, const char *name)
3207 int ret;
3209 if (!*size) {
3210 ret = get_common_info(pevent, name, offset, size);
3211 if (ret < 0)
3212 return ret;
3214 return pevent_read_number(pevent, data + *offset, *size);
3217 static int trace_parse_common_type(struct pevent *pevent, void *data)
3219 return __parse_common(pevent, data,
3220 &pevent->type_size, &pevent->type_offset,
3221 "common_type");
3224 static int parse_common_pid(struct pevent *pevent, void *data)
3226 return __parse_common(pevent, data,
3227 &pevent->pid_size, &pevent->pid_offset,
3228 "common_pid");
3231 static int parse_common_pc(struct pevent *pevent, void *data)
3233 return __parse_common(pevent, data,
3234 &pevent->pc_size, &pevent->pc_offset,
3235 "common_preempt_count");
3238 static int parse_common_flags(struct pevent *pevent, void *data)
3240 return __parse_common(pevent, data,
3241 &pevent->flags_size, &pevent->flags_offset,
3242 "common_flags");
3245 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3247 return __parse_common(pevent, data,
3248 &pevent->ld_size, &pevent->ld_offset,
3249 "common_lock_depth");
3252 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3254 return __parse_common(pevent, data,
3255 &pevent->ld_size, &pevent->ld_offset,
3256 "common_migrate_disable");
3259 static int events_id_cmp(const void *a, const void *b);
3262 * pevent_find_event - find an event by given id
3263 * @pevent: a handle to the pevent
3264 * @id: the id of the event
3266 * Returns an event that has a given @id.
3268 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3270 struct event_format **eventptr;
3271 struct event_format key;
3272 struct event_format *pkey = &key;
3274 /* Check cache first */
3275 if (pevent->last_event && pevent->last_event->id == id)
3276 return pevent->last_event;
3278 key.id = id;
3280 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3281 sizeof(*pevent->events), events_id_cmp);
3283 if (eventptr) {
3284 pevent->last_event = *eventptr;
3285 return *eventptr;
3288 return NULL;
3292 * pevent_find_event_by_name - find an event by given name
3293 * @pevent: a handle to the pevent
3294 * @sys: the system name to search for
3295 * @name: the name of the event to search for
3297 * This returns an event with a given @name and under the system
3298 * @sys. If @sys is NULL the first event with @name is returned.
3300 struct event_format *
3301 pevent_find_event_by_name(struct pevent *pevent,
3302 const char *sys, const char *name)
3304 struct event_format *event;
3305 int i;
3307 if (pevent->last_event &&
3308 strcmp(pevent->last_event->name, name) == 0 &&
3309 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3310 return pevent->last_event;
3312 for (i = 0; i < pevent->nr_events; i++) {
3313 event = pevent->events[i];
3314 if (strcmp(event->name, name) == 0) {
3315 if (!sys)
3316 break;
3317 if (strcmp(event->system, sys) == 0)
3318 break;
3321 if (i == pevent->nr_events)
3322 event = NULL;
3324 pevent->last_event = event;
3325 return event;
3328 static unsigned long long
3329 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3331 struct pevent *pevent = event->pevent;
3332 unsigned long long val = 0;
3333 unsigned long long left, right;
3334 struct print_arg *typearg = NULL;
3335 struct print_arg *larg;
3336 unsigned long offset;
3337 unsigned int field_size;
3339 switch (arg->type) {
3340 case PRINT_NULL:
3341 /* ?? */
3342 return 0;
3343 case PRINT_ATOM:
3344 return strtoull(arg->atom.atom, NULL, 0);
3345 case PRINT_FIELD:
3346 if (!arg->field.field) {
3347 arg->field.field = pevent_find_any_field(event, arg->field.name);
3348 if (!arg->field.field)
3349 goto out_warning_field;
3352 /* must be a number */
3353 val = pevent_read_number(pevent, data + arg->field.field->offset,
3354 arg->field.field->size);
3355 break;
3356 case PRINT_FLAGS:
3357 case PRINT_SYMBOL:
3358 case PRINT_HEX:
3359 break;
3360 case PRINT_TYPE:
3361 val = eval_num_arg(data, size, event, arg->typecast.item);
3362 return eval_type(val, arg, 0);
3363 case PRINT_STRING:
3364 case PRINT_BSTRING:
3365 case PRINT_BITMASK:
3366 return 0;
3367 case PRINT_FUNC: {
3368 struct trace_seq s;
3369 trace_seq_init(&s);
3370 val = process_defined_func(&s, data, size, event, arg);
3371 trace_seq_destroy(&s);
3372 return val;
3374 case PRINT_OP:
3375 if (strcmp(arg->op.op, "[") == 0) {
3377 * Arrays are special, since we don't want
3378 * to read the arg as is.
3380 right = eval_num_arg(data, size, event, arg->op.right);
3382 /* handle typecasts */
3383 larg = arg->op.left;
3384 while (larg->type == PRINT_TYPE) {
3385 if (!typearg)
3386 typearg = larg;
3387 larg = larg->typecast.item;
3390 /* Default to long size */
3391 field_size = pevent->long_size;
3393 switch (larg->type) {
3394 case PRINT_DYNAMIC_ARRAY:
3395 offset = pevent_read_number(pevent,
3396 data + larg->dynarray.field->offset,
3397 larg->dynarray.field->size);
3398 if (larg->dynarray.field->elementsize)
3399 field_size = larg->dynarray.field->elementsize;
3401 * The actual length of the dynamic array is stored
3402 * in the top half of the field, and the offset
3403 * is in the bottom half of the 32 bit field.
3405 offset &= 0xffff;
3406 offset += right;
3407 break;
3408 case PRINT_FIELD:
3409 if (!larg->field.field) {
3410 larg->field.field =
3411 pevent_find_any_field(event, larg->field.name);
3412 if (!larg->field.field) {
3413 arg = larg;
3414 goto out_warning_field;
3417 field_size = larg->field.field->elementsize;
3418 offset = larg->field.field->offset +
3419 right * larg->field.field->elementsize;
3420 break;
3421 default:
3422 goto default_op; /* oops, all bets off */
3424 val = pevent_read_number(pevent,
3425 data + offset, field_size);
3426 if (typearg)
3427 val = eval_type(val, typearg, 1);
3428 break;
3429 } else if (strcmp(arg->op.op, "?") == 0) {
3430 left = eval_num_arg(data, size, event, arg->op.left);
3431 arg = arg->op.right;
3432 if (left)
3433 val = eval_num_arg(data, size, event, arg->op.left);
3434 else
3435 val = eval_num_arg(data, size, event, arg->op.right);
3436 break;
3438 default_op:
3439 left = eval_num_arg(data, size, event, arg->op.left);
3440 right = eval_num_arg(data, size, event, arg->op.right);
3441 switch (arg->op.op[0]) {
3442 case '!':
3443 switch (arg->op.op[1]) {
3444 case 0:
3445 val = !right;
3446 break;
3447 case '=':
3448 val = left != right;
3449 break;
3450 default:
3451 goto out_warning_op;
3453 break;
3454 case '~':
3455 val = ~right;
3456 break;
3457 case '|':
3458 if (arg->op.op[1])
3459 val = left || right;
3460 else
3461 val = left | right;
3462 break;
3463 case '&':
3464 if (arg->op.op[1])
3465 val = left && right;
3466 else
3467 val = left & right;
3468 break;
3469 case '<':
3470 switch (arg->op.op[1]) {
3471 case 0:
3472 val = left < right;
3473 break;
3474 case '<':
3475 val = left << right;
3476 break;
3477 case '=':
3478 val = left <= right;
3479 break;
3480 default:
3481 goto out_warning_op;
3483 break;
3484 case '>':
3485 switch (arg->op.op[1]) {
3486 case 0:
3487 val = left > right;
3488 break;
3489 case '>':
3490 val = left >> right;
3491 break;
3492 case '=':
3493 val = left >= right;
3494 break;
3495 default:
3496 goto out_warning_op;
3498 break;
3499 case '=':
3500 if (arg->op.op[1] != '=')
3501 goto out_warning_op;
3503 val = left == right;
3504 break;
3505 case '-':
3506 val = left - right;
3507 break;
3508 case '+':
3509 val = left + right;
3510 break;
3511 case '/':
3512 val = left / right;
3513 break;
3514 case '*':
3515 val = left * right;
3516 break;
3517 default:
3518 goto out_warning_op;
3520 break;
3521 case PRINT_DYNAMIC_ARRAY:
3522 /* Without [], we pass the address to the dynamic data */
3523 offset = pevent_read_number(pevent,
3524 data + arg->dynarray.field->offset,
3525 arg->dynarray.field->size);
3527 * The actual length of the dynamic array is stored
3528 * in the top half of the field, and the offset
3529 * is in the bottom half of the 32 bit field.
3531 offset &= 0xffff;
3532 val = (unsigned long long)((unsigned long)data + offset);
3533 break;
3534 default: /* not sure what to do there */
3535 return 0;
3537 return val;
3539 out_warning_op:
3540 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3541 return 0;
3543 out_warning_field:
3544 do_warning_event(event, "%s: field %s not found",
3545 __func__, arg->field.name);
3546 return 0;
3549 struct flag {
3550 const char *name;
3551 unsigned long long value;
3554 static const struct flag flags[] = {
3555 { "HI_SOFTIRQ", 0 },
3556 { "TIMER_SOFTIRQ", 1 },
3557 { "NET_TX_SOFTIRQ", 2 },
3558 { "NET_RX_SOFTIRQ", 3 },
3559 { "BLOCK_SOFTIRQ", 4 },
3560 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3561 { "TASKLET_SOFTIRQ", 6 },
3562 { "SCHED_SOFTIRQ", 7 },
3563 { "HRTIMER_SOFTIRQ", 8 },
3564 { "RCU_SOFTIRQ", 9 },
3566 { "HRTIMER_NORESTART", 0 },
3567 { "HRTIMER_RESTART", 1 },
3570 static unsigned long long eval_flag(const char *flag)
3572 int i;
3575 * Some flags in the format files do not get converted.
3576 * If the flag is not numeric, see if it is something that
3577 * we already know about.
3579 if (isdigit(flag[0]))
3580 return strtoull(flag, NULL, 0);
3582 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3583 if (strcmp(flags[i].name, flag) == 0)
3584 return flags[i].value;
3586 return 0;
3589 static void print_str_to_seq(struct trace_seq *s, const char *format,
3590 int len_arg, const char *str)
3592 if (len_arg >= 0)
3593 trace_seq_printf(s, format, len_arg, str);
3594 else
3595 trace_seq_printf(s, format, str);
3598 static void print_bitmask_to_seq(struct pevent *pevent,
3599 struct trace_seq *s, const char *format,
3600 int len_arg, const void *data, int size)
3602 int nr_bits = size * 8;
3603 int str_size = (nr_bits + 3) / 4;
3604 int len = 0;
3605 char buf[3];
3606 char *str;
3607 int index;
3608 int i;
3611 * The kernel likes to put in commas every 32 bits, we
3612 * can do the same.
3614 str_size += (nr_bits - 1) / 32;
3616 str = malloc(str_size + 1);
3617 if (!str) {
3618 do_warning("%s: not enough memory!", __func__);
3619 return;
3621 str[str_size] = 0;
3623 /* Start out with -2 for the two chars per byte */
3624 for (i = str_size - 2; i >= 0; i -= 2) {
3626 * data points to a bit mask of size bytes.
3627 * In the kernel, this is an array of long words, thus
3628 * endianess is very important.
3630 if (pevent->file_bigendian)
3631 index = size - (len + 1);
3632 else
3633 index = len;
3635 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3636 memcpy(str + i, buf, 2);
3637 len++;
3638 if (!(len & 3) && i > 0) {
3639 i--;
3640 str[i] = ',';
3644 if (len_arg >= 0)
3645 trace_seq_printf(s, format, len_arg, str);
3646 else
3647 trace_seq_printf(s, format, str);
3649 free(str);
3652 static void print_str_arg(struct trace_seq *s, void *data, int size,
3653 struct event_format *event, const char *format,
3654 int len_arg, struct print_arg *arg)
3656 struct pevent *pevent = event->pevent;
3657 struct print_flag_sym *flag;
3658 struct format_field *field;
3659 struct printk_map *printk;
3660 unsigned long long val, fval;
3661 unsigned long addr;
3662 char *str;
3663 unsigned char *hex;
3664 int print;
3665 int i, len;
3667 switch (arg->type) {
3668 case PRINT_NULL:
3669 /* ?? */
3670 return;
3671 case PRINT_ATOM:
3672 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3673 return;
3674 case PRINT_FIELD:
3675 field = arg->field.field;
3676 if (!field) {
3677 field = pevent_find_any_field(event, arg->field.name);
3678 if (!field) {
3679 str = arg->field.name;
3680 goto out_warning_field;
3682 arg->field.field = field;
3684 /* Zero sized fields, mean the rest of the data */
3685 len = field->size ? : size - field->offset;
3688 * Some events pass in pointers. If this is not an array
3689 * and the size is the same as long_size, assume that it
3690 * is a pointer.
3692 if (!(field->flags & FIELD_IS_ARRAY) &&
3693 field->size == pevent->long_size) {
3694 addr = *(unsigned long *)(data + field->offset);
3695 /* Check if it matches a print format */
3696 printk = find_printk(pevent, addr);
3697 if (printk)
3698 trace_seq_puts(s, printk->printk);
3699 else
3700 trace_seq_printf(s, "%lx", addr);
3701 break;
3703 str = malloc(len + 1);
3704 if (!str) {
3705 do_warning_event(event, "%s: not enough memory!",
3706 __func__);
3707 return;
3709 memcpy(str, data + field->offset, len);
3710 str[len] = 0;
3711 print_str_to_seq(s, format, len_arg, str);
3712 free(str);
3713 break;
3714 case PRINT_FLAGS:
3715 val = eval_num_arg(data, size, event, arg->flags.field);
3716 print = 0;
3717 for (flag = arg->flags.flags; flag; flag = flag->next) {
3718 fval = eval_flag(flag->value);
3719 if (!val && !fval) {
3720 print_str_to_seq(s, format, len_arg, flag->str);
3721 break;
3723 if (fval && (val & fval) == fval) {
3724 if (print && arg->flags.delim)
3725 trace_seq_puts(s, arg->flags.delim);
3726 print_str_to_seq(s, format, len_arg, flag->str);
3727 print = 1;
3728 val &= ~fval;
3731 break;
3732 case PRINT_SYMBOL:
3733 val = eval_num_arg(data, size, event, arg->symbol.field);
3734 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3735 fval = eval_flag(flag->value);
3736 if (val == fval) {
3737 print_str_to_seq(s, format, len_arg, flag->str);
3738 break;
3741 break;
3742 case PRINT_HEX:
3743 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3744 unsigned long offset;
3745 offset = pevent_read_number(pevent,
3746 data + arg->hex.field->dynarray.field->offset,
3747 arg->hex.field->dynarray.field->size);
3748 hex = data + (offset & 0xffff);
3749 } else {
3750 field = arg->hex.field->field.field;
3751 if (!field) {
3752 str = arg->hex.field->field.name;
3753 field = pevent_find_any_field(event, str);
3754 if (!field)
3755 goto out_warning_field;
3756 arg->hex.field->field.field = field;
3758 hex = data + field->offset;
3760 len = eval_num_arg(data, size, event, arg->hex.size);
3761 for (i = 0; i < len; i++) {
3762 if (i)
3763 trace_seq_putc(s, ' ');
3764 trace_seq_printf(s, "%02x", hex[i]);
3766 break;
3768 case PRINT_TYPE:
3769 break;
3770 case PRINT_STRING: {
3771 int str_offset;
3773 if (arg->string.offset == -1) {
3774 struct format_field *f;
3776 f = pevent_find_any_field(event, arg->string.string);
3777 arg->string.offset = f->offset;
3779 str_offset = data2host4(pevent, data + arg->string.offset);
3780 str_offset &= 0xffff;
3781 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3782 break;
3784 case PRINT_BSTRING:
3785 print_str_to_seq(s, format, len_arg, arg->string.string);
3786 break;
3787 case PRINT_BITMASK: {
3788 int bitmask_offset;
3789 int bitmask_size;
3791 if (arg->bitmask.offset == -1) {
3792 struct format_field *f;
3794 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3795 arg->bitmask.offset = f->offset;
3797 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3798 bitmask_size = bitmask_offset >> 16;
3799 bitmask_offset &= 0xffff;
3800 print_bitmask_to_seq(pevent, s, format, len_arg,
3801 data + bitmask_offset, bitmask_size);
3802 break;
3804 case PRINT_OP:
3806 * The only op for string should be ? :
3808 if (arg->op.op[0] != '?')
3809 return;
3810 val = eval_num_arg(data, size, event, arg->op.left);
3811 if (val)
3812 print_str_arg(s, data, size, event,
3813 format, len_arg, arg->op.right->op.left);
3814 else
3815 print_str_arg(s, data, size, event,
3816 format, len_arg, arg->op.right->op.right);
3817 break;
3818 case PRINT_FUNC:
3819 process_defined_func(s, data, size, event, arg);
3820 break;
3821 default:
3822 /* well... */
3823 break;
3826 return;
3828 out_warning_field:
3829 do_warning_event(event, "%s: field %s not found",
3830 __func__, arg->field.name);
3833 static unsigned long long
3834 process_defined_func(struct trace_seq *s, void *data, int size,
3835 struct event_format *event, struct print_arg *arg)
3837 struct pevent_function_handler *func_handle = arg->func.func;
3838 struct pevent_func_params *param;
3839 unsigned long long *args;
3840 unsigned long long ret;
3841 struct print_arg *farg;
3842 struct trace_seq str;
3843 struct save_str {
3844 struct save_str *next;
3845 char *str;
3846 } *strings = NULL, *string;
3847 int i;
3849 if (!func_handle->nr_args) {
3850 ret = (*func_handle->func)(s, NULL);
3851 goto out;
3854 farg = arg->func.args;
3855 param = func_handle->params;
3857 ret = ULLONG_MAX;
3858 args = malloc(sizeof(*args) * func_handle->nr_args);
3859 if (!args)
3860 goto out;
3862 for (i = 0; i < func_handle->nr_args; i++) {
3863 switch (param->type) {
3864 case PEVENT_FUNC_ARG_INT:
3865 case PEVENT_FUNC_ARG_LONG:
3866 case PEVENT_FUNC_ARG_PTR:
3867 args[i] = eval_num_arg(data, size, event, farg);
3868 break;
3869 case PEVENT_FUNC_ARG_STRING:
3870 trace_seq_init(&str);
3871 print_str_arg(&str, data, size, event, "%s", -1, farg);
3872 trace_seq_terminate(&str);
3873 string = malloc(sizeof(*string));
3874 if (!string) {
3875 do_warning_event(event, "%s(%d): malloc str",
3876 __func__, __LINE__);
3877 goto out_free;
3879 string->next = strings;
3880 string->str = strdup(str.buffer);
3881 if (!string->str) {
3882 free(string);
3883 do_warning_event(event, "%s(%d): malloc str",
3884 __func__, __LINE__);
3885 goto out_free;
3887 args[i] = (uintptr_t)string->str;
3888 strings = string;
3889 trace_seq_destroy(&str);
3890 break;
3891 default:
3893 * Something went totally wrong, this is not
3894 * an input error, something in this code broke.
3896 do_warning_event(event, "Unexpected end of arguments\n");
3897 goto out_free;
3899 farg = farg->next;
3900 param = param->next;
3903 ret = (*func_handle->func)(s, args);
3904 out_free:
3905 free(args);
3906 while (strings) {
3907 string = strings;
3908 strings = string->next;
3909 free(string->str);
3910 free(string);
3913 out:
3914 /* TBD : handle return type here */
3915 return ret;
3918 static void free_args(struct print_arg *args)
3920 struct print_arg *next;
3922 while (args) {
3923 next = args->next;
3925 free_arg(args);
3926 args = next;
3930 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3932 struct pevent *pevent = event->pevent;
3933 struct format_field *field, *ip_field;
3934 struct print_arg *args, *arg, **next;
3935 unsigned long long ip, val;
3936 char *ptr;
3937 void *bptr;
3938 int vsize;
3940 field = pevent->bprint_buf_field;
3941 ip_field = pevent->bprint_ip_field;
3943 if (!field) {
3944 field = pevent_find_field(event, "buf");
3945 if (!field) {
3946 do_warning_event(event, "can't find buffer field for binary printk");
3947 return NULL;
3949 ip_field = pevent_find_field(event, "ip");
3950 if (!ip_field) {
3951 do_warning_event(event, "can't find ip field for binary printk");
3952 return NULL;
3954 pevent->bprint_buf_field = field;
3955 pevent->bprint_ip_field = ip_field;
3958 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3961 * The first arg is the IP pointer.
3963 args = alloc_arg();
3964 if (!args) {
3965 do_warning_event(event, "%s(%d): not enough memory!",
3966 __func__, __LINE__);
3967 return NULL;
3969 arg = args;
3970 arg->next = NULL;
3971 next = &arg->next;
3973 arg->type = PRINT_ATOM;
3975 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3976 goto out_free;
3978 /* skip the first "%pf: " */
3979 for (ptr = fmt + 5, bptr = data + field->offset;
3980 bptr < data + size && *ptr; ptr++) {
3981 int ls = 0;
3983 if (*ptr == '%') {
3984 process_again:
3985 ptr++;
3986 switch (*ptr) {
3987 case '%':
3988 break;
3989 case 'l':
3990 ls++;
3991 goto process_again;
3992 case 'L':
3993 ls = 2;
3994 goto process_again;
3995 case '0' ... '9':
3996 goto process_again;
3997 case '.':
3998 goto process_again;
3999 case 'p':
4000 ls = 1;
4001 /* fall through */
4002 case 'd':
4003 case 'u':
4004 case 'x':
4005 case 'i':
4006 switch (ls) {
4007 case 0:
4008 vsize = 4;
4009 break;
4010 case 1:
4011 vsize = pevent->long_size;
4012 break;
4013 case 2:
4014 vsize = 8;
4015 break;
4016 default:
4017 vsize = ls; /* ? */
4018 break;
4020 /* fall through */
4021 case '*':
4022 if (*ptr == '*')
4023 vsize = 4;
4025 /* the pointers are always 4 bytes aligned */
4026 bptr = (void *)(((unsigned long)bptr + 3) &
4027 ~3);
4028 val = pevent_read_number(pevent, bptr, vsize);
4029 bptr += vsize;
4030 arg = alloc_arg();
4031 if (!arg) {
4032 do_warning_event(event, "%s(%d): not enough memory!",
4033 __func__, __LINE__);
4034 goto out_free;
4036 arg->next = NULL;
4037 arg->type = PRINT_ATOM;
4038 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4039 free(arg);
4040 goto out_free;
4042 *next = arg;
4043 next = &arg->next;
4045 * The '*' case means that an arg is used as the length.
4046 * We need to continue to figure out for what.
4048 if (*ptr == '*')
4049 goto process_again;
4051 break;
4052 case 's':
4053 arg = alloc_arg();
4054 if (!arg) {
4055 do_warning_event(event, "%s(%d): not enough memory!",
4056 __func__, __LINE__);
4057 goto out_free;
4059 arg->next = NULL;
4060 arg->type = PRINT_BSTRING;
4061 arg->string.string = strdup(bptr);
4062 if (!arg->string.string)
4063 goto out_free;
4064 bptr += strlen(bptr) + 1;
4065 *next = arg;
4066 next = &arg->next;
4067 default:
4068 break;
4073 return args;
4075 out_free:
4076 free_args(args);
4077 return NULL;
4080 static char *
4081 get_bprint_format(void *data, int size __maybe_unused,
4082 struct event_format *event)
4084 struct pevent *pevent = event->pevent;
4085 unsigned long long addr;
4086 struct format_field *field;
4087 struct printk_map *printk;
4088 char *format;
4090 field = pevent->bprint_fmt_field;
4092 if (!field) {
4093 field = pevent_find_field(event, "fmt");
4094 if (!field) {
4095 do_warning_event(event, "can't find format field for binary printk");
4096 return NULL;
4098 pevent->bprint_fmt_field = field;
4101 addr = pevent_read_number(pevent, data + field->offset, field->size);
4103 printk = find_printk(pevent, addr);
4104 if (!printk) {
4105 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4106 return NULL;
4107 return format;
4110 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4111 return NULL;
4113 return format;
4116 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4117 struct event_format *event, struct print_arg *arg)
4119 unsigned char *buf;
4120 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4122 if (arg->type == PRINT_FUNC) {
4123 process_defined_func(s, data, size, event, arg);
4124 return;
4127 if (arg->type != PRINT_FIELD) {
4128 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4129 arg->type);
4130 return;
4133 if (mac == 'm')
4134 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4135 if (!arg->field.field) {
4136 arg->field.field =
4137 pevent_find_any_field(event, arg->field.name);
4138 if (!arg->field.field) {
4139 do_warning_event(event, "%s: field %s not found",
4140 __func__, arg->field.name);
4141 return;
4144 if (arg->field.field->size != 6) {
4145 trace_seq_printf(s, "INVALIDMAC");
4146 return;
4148 buf = data + arg->field.field->offset;
4149 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4152 static int is_printable_array(char *p, unsigned int len)
4154 unsigned int i;
4156 for (i = 0; i < len && p[i]; i++)
4157 if (!isprint(p[i]) && !isspace(p[i]))
4158 return 0;
4159 return 1;
4162 static void print_event_fields(struct trace_seq *s, void *data,
4163 int size __maybe_unused,
4164 struct event_format *event)
4166 struct format_field *field;
4167 unsigned long long val;
4168 unsigned int offset, len, i;
4170 field = event->format.fields;
4171 while (field) {
4172 trace_seq_printf(s, " %s=", field->name);
4173 if (field->flags & FIELD_IS_ARRAY) {
4174 offset = field->offset;
4175 len = field->size;
4176 if (field->flags & FIELD_IS_DYNAMIC) {
4177 val = pevent_read_number(event->pevent, data + offset, len);
4178 offset = val;
4179 len = offset >> 16;
4180 offset &= 0xffff;
4182 if (field->flags & FIELD_IS_STRING &&
4183 is_printable_array(data + offset, len)) {
4184 trace_seq_printf(s, "%s", (char *)data + offset);
4185 } else {
4186 trace_seq_puts(s, "ARRAY[");
4187 for (i = 0; i < len; i++) {
4188 if (i)
4189 trace_seq_puts(s, ", ");
4190 trace_seq_printf(s, "%02x",
4191 *((unsigned char *)data + offset + i));
4193 trace_seq_putc(s, ']');
4194 field->flags &= ~FIELD_IS_STRING;
4196 } else {
4197 val = pevent_read_number(event->pevent, data + field->offset,
4198 field->size);
4199 if (field->flags & FIELD_IS_POINTER) {
4200 trace_seq_printf(s, "0x%llx", val);
4201 } else if (field->flags & FIELD_IS_SIGNED) {
4202 switch (field->size) {
4203 case 4:
4205 * If field is long then print it in hex.
4206 * A long usually stores pointers.
4208 if (field->flags & FIELD_IS_LONG)
4209 trace_seq_printf(s, "0x%x", (int)val);
4210 else
4211 trace_seq_printf(s, "%d", (int)val);
4212 break;
4213 case 2:
4214 trace_seq_printf(s, "%2d", (short)val);
4215 break;
4216 case 1:
4217 trace_seq_printf(s, "%1d", (char)val);
4218 break;
4219 default:
4220 trace_seq_printf(s, "%lld", val);
4222 } else {
4223 if (field->flags & FIELD_IS_LONG)
4224 trace_seq_printf(s, "0x%llx", val);
4225 else
4226 trace_seq_printf(s, "%llu", val);
4229 field = field->next;
4233 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4235 struct pevent *pevent = event->pevent;
4236 struct print_fmt *print_fmt = &event->print_fmt;
4237 struct print_arg *arg = print_fmt->args;
4238 struct print_arg *args = NULL;
4239 const char *ptr = print_fmt->format;
4240 unsigned long long val;
4241 struct func_map *func;
4242 const char *saveptr;
4243 struct trace_seq p;
4244 char *bprint_fmt = NULL;
4245 char format[32];
4246 int show_func;
4247 int len_as_arg;
4248 int len_arg;
4249 int len;
4250 int ls;
4252 if (event->flags & EVENT_FL_FAILED) {
4253 trace_seq_printf(s, "[FAILED TO PARSE]");
4254 print_event_fields(s, data, size, event);
4255 return;
4258 if (event->flags & EVENT_FL_ISBPRINT) {
4259 bprint_fmt = get_bprint_format(data, size, event);
4260 args = make_bprint_args(bprint_fmt, data, size, event);
4261 arg = args;
4262 ptr = bprint_fmt;
4265 for (; *ptr; ptr++) {
4266 ls = 0;
4267 if (*ptr == '\\') {
4268 ptr++;
4269 switch (*ptr) {
4270 case 'n':
4271 trace_seq_putc(s, '\n');
4272 break;
4273 case 't':
4274 trace_seq_putc(s, '\t');
4275 break;
4276 case 'r':
4277 trace_seq_putc(s, '\r');
4278 break;
4279 case '\\':
4280 trace_seq_putc(s, '\\');
4281 break;
4282 default:
4283 trace_seq_putc(s, *ptr);
4284 break;
4287 } else if (*ptr == '%') {
4288 saveptr = ptr;
4289 show_func = 0;
4290 len_as_arg = 0;
4291 cont_process:
4292 ptr++;
4293 switch (*ptr) {
4294 case '%':
4295 trace_seq_putc(s, '%');
4296 break;
4297 case '#':
4298 /* FIXME: need to handle properly */
4299 goto cont_process;
4300 case 'h':
4301 ls--;
4302 goto cont_process;
4303 case 'l':
4304 ls++;
4305 goto cont_process;
4306 case 'L':
4307 ls = 2;
4308 goto cont_process;
4309 case '*':
4310 /* The argument is the length. */
4311 if (!arg) {
4312 do_warning_event(event, "no argument match");
4313 event->flags |= EVENT_FL_FAILED;
4314 goto out_failed;
4316 len_arg = eval_num_arg(data, size, event, arg);
4317 len_as_arg = 1;
4318 arg = arg->next;
4319 goto cont_process;
4320 case '.':
4321 case 'z':
4322 case 'Z':
4323 case '0' ... '9':
4324 goto cont_process;
4325 case 'p':
4326 if (pevent->long_size == 4)
4327 ls = 1;
4328 else
4329 ls = 2;
4331 if (*(ptr+1) == 'F' ||
4332 *(ptr+1) == 'f') {
4333 ptr++;
4334 show_func = *ptr;
4335 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4336 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4337 ptr++;
4338 arg = arg->next;
4339 break;
4342 /* fall through */
4343 case 'd':
4344 case 'i':
4345 case 'x':
4346 case 'X':
4347 case 'u':
4348 if (!arg) {
4349 do_warning_event(event, "no argument match");
4350 event->flags |= EVENT_FL_FAILED;
4351 goto out_failed;
4354 len = ((unsigned long)ptr + 1) -
4355 (unsigned long)saveptr;
4357 /* should never happen */
4358 if (len > 31) {
4359 do_warning_event(event, "bad format!");
4360 event->flags |= EVENT_FL_FAILED;
4361 len = 31;
4364 memcpy(format, saveptr, len);
4365 format[len] = 0;
4367 val = eval_num_arg(data, size, event, arg);
4368 arg = arg->next;
4370 if (show_func) {
4371 func = find_func(pevent, val);
4372 if (func) {
4373 trace_seq_puts(s, func->func);
4374 if (show_func == 'F')
4375 trace_seq_printf(s,
4376 "+0x%llx",
4377 val - func->addr);
4378 break;
4381 if (pevent->long_size == 8 && ls &&
4382 sizeof(long) != 8) {
4383 char *p;
4385 ls = 2;
4386 /* make %l into %ll */
4387 p = strchr(format, 'l');
4388 if (p)
4389 memmove(p+1, p, strlen(p)+1);
4390 else if (strcmp(format, "%p") == 0)
4391 strcpy(format, "0x%llx");
4393 switch (ls) {
4394 case -2:
4395 if (len_as_arg)
4396 trace_seq_printf(s, format, len_arg, (char)val);
4397 else
4398 trace_seq_printf(s, format, (char)val);
4399 break;
4400 case -1:
4401 if (len_as_arg)
4402 trace_seq_printf(s, format, len_arg, (short)val);
4403 else
4404 trace_seq_printf(s, format, (short)val);
4405 break;
4406 case 0:
4407 if (len_as_arg)
4408 trace_seq_printf(s, format, len_arg, (int)val);
4409 else
4410 trace_seq_printf(s, format, (int)val);
4411 break;
4412 case 1:
4413 if (len_as_arg)
4414 trace_seq_printf(s, format, len_arg, (long)val);
4415 else
4416 trace_seq_printf(s, format, (long)val);
4417 break;
4418 case 2:
4419 if (len_as_arg)
4420 trace_seq_printf(s, format, len_arg,
4421 (long long)val);
4422 else
4423 trace_seq_printf(s, format, (long long)val);
4424 break;
4425 default:
4426 do_warning_event(event, "bad count (%d)", ls);
4427 event->flags |= EVENT_FL_FAILED;
4429 break;
4430 case 's':
4431 if (!arg) {
4432 do_warning_event(event, "no matching argument");
4433 event->flags |= EVENT_FL_FAILED;
4434 goto out_failed;
4437 len = ((unsigned long)ptr + 1) -
4438 (unsigned long)saveptr;
4440 /* should never happen */
4441 if (len > 31) {
4442 do_warning_event(event, "bad format!");
4443 event->flags |= EVENT_FL_FAILED;
4444 len = 31;
4447 memcpy(format, saveptr, len);
4448 format[len] = 0;
4449 if (!len_as_arg)
4450 len_arg = -1;
4451 /* Use helper trace_seq */
4452 trace_seq_init(&p);
4453 print_str_arg(&p, data, size, event,
4454 format, len_arg, arg);
4455 trace_seq_terminate(&p);
4456 trace_seq_puts(s, p.buffer);
4457 trace_seq_destroy(&p);
4458 arg = arg->next;
4459 break;
4460 default:
4461 trace_seq_printf(s, ">%c<", *ptr);
4464 } else
4465 trace_seq_putc(s, *ptr);
4468 if (event->flags & EVENT_FL_FAILED) {
4469 out_failed:
4470 trace_seq_printf(s, "[FAILED TO PARSE]");
4473 if (args) {
4474 free_args(args);
4475 free(bprint_fmt);
4480 * pevent_data_lat_fmt - parse the data for the latency format
4481 * @pevent: a handle to the pevent
4482 * @s: the trace_seq to write to
4483 * @record: the record to read from
4485 * This parses out the Latency format (interrupts disabled,
4486 * need rescheduling, in hard/soft interrupt, preempt count
4487 * and lock depth) and places it into the trace_seq.
4489 void pevent_data_lat_fmt(struct pevent *pevent,
4490 struct trace_seq *s, struct pevent_record *record)
4492 static int check_lock_depth = 1;
4493 static int check_migrate_disable = 1;
4494 static int lock_depth_exists;
4495 static int migrate_disable_exists;
4496 unsigned int lat_flags;
4497 unsigned int pc;
4498 int lock_depth;
4499 int migrate_disable;
4500 int hardirq;
4501 int softirq;
4502 void *data = record->data;
4504 lat_flags = parse_common_flags(pevent, data);
4505 pc = parse_common_pc(pevent, data);
4506 /* lock_depth may not always exist */
4507 if (lock_depth_exists)
4508 lock_depth = parse_common_lock_depth(pevent, data);
4509 else if (check_lock_depth) {
4510 lock_depth = parse_common_lock_depth(pevent, data);
4511 if (lock_depth < 0)
4512 check_lock_depth = 0;
4513 else
4514 lock_depth_exists = 1;
4517 /* migrate_disable may not always exist */
4518 if (migrate_disable_exists)
4519 migrate_disable = parse_common_migrate_disable(pevent, data);
4520 else if (check_migrate_disable) {
4521 migrate_disable = parse_common_migrate_disable(pevent, data);
4522 if (migrate_disable < 0)
4523 check_migrate_disable = 0;
4524 else
4525 migrate_disable_exists = 1;
4528 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4529 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4531 trace_seq_printf(s, "%c%c%c",
4532 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4533 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4534 'X' : '.',
4535 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4536 'N' : '.',
4537 (hardirq && softirq) ? 'H' :
4538 hardirq ? 'h' : softirq ? 's' : '.');
4540 if (pc)
4541 trace_seq_printf(s, "%x", pc);
4542 else
4543 trace_seq_putc(s, '.');
4545 if (migrate_disable_exists) {
4546 if (migrate_disable < 0)
4547 trace_seq_putc(s, '.');
4548 else
4549 trace_seq_printf(s, "%d", migrate_disable);
4552 if (lock_depth_exists) {
4553 if (lock_depth < 0)
4554 trace_seq_putc(s, '.');
4555 else
4556 trace_seq_printf(s, "%d", lock_depth);
4559 trace_seq_terminate(s);
4563 * pevent_data_type - parse out the given event type
4564 * @pevent: a handle to the pevent
4565 * @rec: the record to read from
4567 * This returns the event id from the @rec.
4569 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4571 return trace_parse_common_type(pevent, rec->data);
4575 * pevent_data_event_from_type - find the event by a given type
4576 * @pevent: a handle to the pevent
4577 * @type: the type of the event.
4579 * This returns the event form a given @type;
4581 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4583 return pevent_find_event(pevent, type);
4587 * pevent_data_pid - parse the PID from raw data
4588 * @pevent: a handle to the pevent
4589 * @rec: the record to parse
4591 * This returns the PID from a raw data.
4593 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4595 return parse_common_pid(pevent, rec->data);
4599 * pevent_data_comm_from_pid - return the command line from PID
4600 * @pevent: a handle to the pevent
4601 * @pid: the PID of the task to search for
4603 * This returns a pointer to the command line that has the given
4604 * @pid.
4606 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4608 const char *comm;
4610 comm = find_cmdline(pevent, pid);
4611 return comm;
4615 * pevent_data_comm_from_pid - parse the data into the print format
4616 * @s: the trace_seq to write to
4617 * @event: the handle to the event
4618 * @record: the record to read from
4620 * This parses the raw @data using the given @event information and
4621 * writes the print format into the trace_seq.
4623 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4624 struct pevent_record *record)
4626 int print_pretty = 1;
4628 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
4629 print_event_fields(s, record->data, record->size, event);
4630 else {
4632 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
4633 print_pretty = event->handler(s, record, event,
4634 event->context);
4636 if (print_pretty)
4637 pretty_print(s, record->data, record->size, event);
4640 trace_seq_terminate(s);
4643 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4645 if (!use_trace_clock)
4646 return true;
4648 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4649 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4650 return true;
4652 /* trace_clock is setting in tsc or counter mode */
4653 return false;
4656 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4657 struct pevent_record *record, bool use_trace_clock)
4659 static const char *spaces = " "; /* 20 spaces */
4660 struct event_format *event;
4661 unsigned long secs;
4662 unsigned long usecs;
4663 unsigned long nsecs;
4664 const char *comm;
4665 void *data = record->data;
4666 int type;
4667 int pid;
4668 int len;
4669 int p;
4670 bool use_usec_format;
4672 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4673 use_trace_clock);
4674 if (use_usec_format) {
4675 secs = record->ts / NSECS_PER_SEC;
4676 nsecs = record->ts - secs * NSECS_PER_SEC;
4679 if (record->size < 0) {
4680 do_warning("ug! negative record size %d", record->size);
4681 return;
4684 type = trace_parse_common_type(pevent, data);
4686 event = pevent_find_event(pevent, type);
4687 if (!event) {
4688 do_warning("ug! no event found for type %d", type);
4689 return;
4692 pid = parse_common_pid(pevent, data);
4693 comm = find_cmdline(pevent, pid);
4695 if (pevent->latency_format) {
4696 trace_seq_printf(s, "%8.8s-%-5d %3d",
4697 comm, pid, record->cpu);
4698 pevent_data_lat_fmt(pevent, s, record);
4699 } else
4700 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4702 if (use_usec_format) {
4703 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4704 usecs = nsecs;
4705 p = 9;
4706 } else {
4707 usecs = (nsecs + 500) / NSECS_PER_USEC;
4708 p = 6;
4711 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4712 secs, p, usecs, event->name);
4713 } else
4714 trace_seq_printf(s, " %12llu: %s: ",
4715 record->ts, event->name);
4717 /* Space out the event names evenly. */
4718 len = strlen(event->name);
4719 if (len < 20)
4720 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4722 pevent_event_info(s, event, record);
4725 static int events_id_cmp(const void *a, const void *b)
4727 struct event_format * const * ea = a;
4728 struct event_format * const * eb = b;
4730 if ((*ea)->id < (*eb)->id)
4731 return -1;
4733 if ((*ea)->id > (*eb)->id)
4734 return 1;
4736 return 0;
4739 static int events_name_cmp(const void *a, const void *b)
4741 struct event_format * const * ea = a;
4742 struct event_format * const * eb = b;
4743 int res;
4745 res = strcmp((*ea)->name, (*eb)->name);
4746 if (res)
4747 return res;
4749 res = strcmp((*ea)->system, (*eb)->system);
4750 if (res)
4751 return res;
4753 return events_id_cmp(a, b);
4756 static int events_system_cmp(const void *a, const void *b)
4758 struct event_format * const * ea = a;
4759 struct event_format * const * eb = b;
4760 int res;
4762 res = strcmp((*ea)->system, (*eb)->system);
4763 if (res)
4764 return res;
4766 res = strcmp((*ea)->name, (*eb)->name);
4767 if (res)
4768 return res;
4770 return events_id_cmp(a, b);
4773 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4775 struct event_format **events;
4776 int (*sort)(const void *a, const void *b);
4778 events = pevent->sort_events;
4780 if (events && pevent->last_type == sort_type)
4781 return events;
4783 if (!events) {
4784 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4785 if (!events)
4786 return NULL;
4788 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4789 events[pevent->nr_events] = NULL;
4791 pevent->sort_events = events;
4793 /* the internal events are sorted by id */
4794 if (sort_type == EVENT_SORT_ID) {
4795 pevent->last_type = sort_type;
4796 return events;
4800 switch (sort_type) {
4801 case EVENT_SORT_ID:
4802 sort = events_id_cmp;
4803 break;
4804 case EVENT_SORT_NAME:
4805 sort = events_name_cmp;
4806 break;
4807 case EVENT_SORT_SYSTEM:
4808 sort = events_system_cmp;
4809 break;
4810 default:
4811 return events;
4814 qsort(events, pevent->nr_events, sizeof(*events), sort);
4815 pevent->last_type = sort_type;
4817 return events;
4820 static struct format_field **
4821 get_event_fields(const char *type, const char *name,
4822 int count, struct format_field *list)
4824 struct format_field **fields;
4825 struct format_field *field;
4826 int i = 0;
4828 fields = malloc(sizeof(*fields) * (count + 1));
4829 if (!fields)
4830 return NULL;
4832 for (field = list; field; field = field->next) {
4833 fields[i++] = field;
4834 if (i == count + 1) {
4835 do_warning("event %s has more %s fields than specified",
4836 name, type);
4837 i--;
4838 break;
4842 if (i != count)
4843 do_warning("event %s has less %s fields than specified",
4844 name, type);
4846 fields[i] = NULL;
4848 return fields;
4852 * pevent_event_common_fields - return a list of common fields for an event
4853 * @event: the event to return the common fields of.
4855 * Returns an allocated array of fields. The last item in the array is NULL.
4856 * The array must be freed with free().
4858 struct format_field **pevent_event_common_fields(struct event_format *event)
4860 return get_event_fields("common", event->name,
4861 event->format.nr_common,
4862 event->format.common_fields);
4866 * pevent_event_fields - return a list of event specific fields for an event
4867 * @event: the event to return the fields of.
4869 * Returns an allocated array of fields. The last item in the array is NULL.
4870 * The array must be freed with free().
4872 struct format_field **pevent_event_fields(struct event_format *event)
4874 return get_event_fields("event", event->name,
4875 event->format.nr_fields,
4876 event->format.fields);
4879 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4881 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4882 if (field->next) {
4883 trace_seq_puts(s, ", ");
4884 print_fields(s, field->next);
4888 /* for debugging */
4889 static void print_args(struct print_arg *args)
4891 int print_paren = 1;
4892 struct trace_seq s;
4894 switch (args->type) {
4895 case PRINT_NULL:
4896 printf("null");
4897 break;
4898 case PRINT_ATOM:
4899 printf("%s", args->atom.atom);
4900 break;
4901 case PRINT_FIELD:
4902 printf("REC->%s", args->field.name);
4903 break;
4904 case PRINT_FLAGS:
4905 printf("__print_flags(");
4906 print_args(args->flags.field);
4907 printf(", %s, ", args->flags.delim);
4908 trace_seq_init(&s);
4909 print_fields(&s, args->flags.flags);
4910 trace_seq_do_printf(&s);
4911 trace_seq_destroy(&s);
4912 printf(")");
4913 break;
4914 case PRINT_SYMBOL:
4915 printf("__print_symbolic(");
4916 print_args(args->symbol.field);
4917 printf(", ");
4918 trace_seq_init(&s);
4919 print_fields(&s, args->symbol.symbols);
4920 trace_seq_do_printf(&s);
4921 trace_seq_destroy(&s);
4922 printf(")");
4923 break;
4924 case PRINT_HEX:
4925 printf("__print_hex(");
4926 print_args(args->hex.field);
4927 printf(", ");
4928 print_args(args->hex.size);
4929 printf(")");
4930 break;
4931 case PRINT_STRING:
4932 case PRINT_BSTRING:
4933 printf("__get_str(%s)", args->string.string);
4934 break;
4935 case PRINT_BITMASK:
4936 printf("__get_bitmask(%s)", args->bitmask.bitmask);
4937 break;
4938 case PRINT_TYPE:
4939 printf("(%s)", args->typecast.type);
4940 print_args(args->typecast.item);
4941 break;
4942 case PRINT_OP:
4943 if (strcmp(args->op.op, ":") == 0)
4944 print_paren = 0;
4945 if (print_paren)
4946 printf("(");
4947 print_args(args->op.left);
4948 printf(" %s ", args->op.op);
4949 print_args(args->op.right);
4950 if (print_paren)
4951 printf(")");
4952 break;
4953 default:
4954 /* we should warn... */
4955 return;
4957 if (args->next) {
4958 printf("\n");
4959 print_args(args->next);
4963 static void parse_header_field(const char *field,
4964 int *offset, int *size, int mandatory)
4966 unsigned long long save_input_buf_ptr;
4967 unsigned long long save_input_buf_siz;
4968 char *token;
4969 int type;
4971 save_input_buf_ptr = input_buf_ptr;
4972 save_input_buf_siz = input_buf_siz;
4974 if (read_expected(EVENT_ITEM, "field") < 0)
4975 return;
4976 if (read_expected(EVENT_OP, ":") < 0)
4977 return;
4979 /* type */
4980 if (read_expect_type(EVENT_ITEM, &token) < 0)
4981 goto fail;
4982 free_token(token);
4985 * If this is not a mandatory field, then test it first.
4987 if (mandatory) {
4988 if (read_expected(EVENT_ITEM, field) < 0)
4989 return;
4990 } else {
4991 if (read_expect_type(EVENT_ITEM, &token) < 0)
4992 goto fail;
4993 if (strcmp(token, field) != 0)
4994 goto discard;
4995 free_token(token);
4998 if (read_expected(EVENT_OP, ";") < 0)
4999 return;
5000 if (read_expected(EVENT_ITEM, "offset") < 0)
5001 return;
5002 if (read_expected(EVENT_OP, ":") < 0)
5003 return;
5004 if (read_expect_type(EVENT_ITEM, &token) < 0)
5005 goto fail;
5006 *offset = atoi(token);
5007 free_token(token);
5008 if (read_expected(EVENT_OP, ";") < 0)
5009 return;
5010 if (read_expected(EVENT_ITEM, "size") < 0)
5011 return;
5012 if (read_expected(EVENT_OP, ":") < 0)
5013 return;
5014 if (read_expect_type(EVENT_ITEM, &token) < 0)
5015 goto fail;
5016 *size = atoi(token);
5017 free_token(token);
5018 if (read_expected(EVENT_OP, ";") < 0)
5019 return;
5020 type = read_token(&token);
5021 if (type != EVENT_NEWLINE) {
5022 /* newer versions of the kernel have a "signed" type */
5023 if (type != EVENT_ITEM)
5024 goto fail;
5026 if (strcmp(token, "signed") != 0)
5027 goto fail;
5029 free_token(token);
5031 if (read_expected(EVENT_OP, ":") < 0)
5032 return;
5034 if (read_expect_type(EVENT_ITEM, &token))
5035 goto fail;
5037 free_token(token);
5038 if (read_expected(EVENT_OP, ";") < 0)
5039 return;
5041 if (read_expect_type(EVENT_NEWLINE, &token))
5042 goto fail;
5044 fail:
5045 free_token(token);
5046 return;
5048 discard:
5049 input_buf_ptr = save_input_buf_ptr;
5050 input_buf_siz = save_input_buf_siz;
5051 *offset = 0;
5052 *size = 0;
5053 free_token(token);
5057 * pevent_parse_header_page - parse the data stored in the header page
5058 * @pevent: the handle to the pevent
5059 * @buf: the buffer storing the header page format string
5060 * @size: the size of @buf
5061 * @long_size: the long size to use if there is no header
5063 * This parses the header page format for information on the
5064 * ring buffer used. The @buf should be copied from
5066 * /sys/kernel/debug/tracing/events/header_page
5068 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5069 int long_size)
5071 int ignore;
5073 if (!size) {
5075 * Old kernels did not have header page info.
5076 * Sorry but we just use what we find here in user space.
5078 pevent->header_page_ts_size = sizeof(long long);
5079 pevent->header_page_size_size = long_size;
5080 pevent->header_page_data_offset = sizeof(long long) + long_size;
5081 pevent->old_format = 1;
5082 return -1;
5084 init_input_buf(buf, size);
5086 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5087 &pevent->header_page_ts_size, 1);
5088 parse_header_field("commit", &pevent->header_page_size_offset,
5089 &pevent->header_page_size_size, 1);
5090 parse_header_field("overwrite", &pevent->header_page_overwrite,
5091 &ignore, 0);
5092 parse_header_field("data", &pevent->header_page_data_offset,
5093 &pevent->header_page_data_size, 1);
5095 return 0;
5098 static int event_matches(struct event_format *event,
5099 int id, const char *sys_name,
5100 const char *event_name)
5102 if (id >= 0 && id != event->id)
5103 return 0;
5105 if (event_name && (strcmp(event_name, event->name) != 0))
5106 return 0;
5108 if (sys_name && (strcmp(sys_name, event->system) != 0))
5109 return 0;
5111 return 1;
5114 static void free_handler(struct event_handler *handle)
5116 free((void *)handle->sys_name);
5117 free((void *)handle->event_name);
5118 free(handle);
5121 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5123 struct event_handler *handle, **next;
5125 for (next = &pevent->handlers; *next;
5126 next = &(*next)->next) {
5127 handle = *next;
5128 if (event_matches(event, handle->id,
5129 handle->sys_name,
5130 handle->event_name))
5131 break;
5134 if (!(*next))
5135 return 0;
5137 pr_stat("overriding event (%d) %s:%s with new print handler",
5138 event->id, event->system, event->name);
5140 event->handler = handle->func;
5141 event->context = handle->context;
5143 *next = handle->next;
5144 free_handler(handle);
5146 return 1;
5150 * __pevent_parse_format - parse the event format
5151 * @buf: the buffer storing the event format string
5152 * @size: the size of @buf
5153 * @sys: the system the event belongs to
5155 * This parses the event format and creates an event structure
5156 * to quickly parse raw data for a given event.
5158 * These files currently come from:
5160 * /sys/kernel/debug/tracing/events/.../.../format
5162 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5163 struct pevent *pevent, const char *buf,
5164 unsigned long size, const char *sys)
5166 struct event_format *event;
5167 int ret;
5169 init_input_buf(buf, size);
5171 *eventp = event = alloc_event();
5172 if (!event)
5173 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5175 event->name = event_read_name();
5176 if (!event->name) {
5177 /* Bad event? */
5178 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5179 goto event_alloc_failed;
5182 if (strcmp(sys, "ftrace") == 0) {
5183 event->flags |= EVENT_FL_ISFTRACE;
5185 if (strcmp(event->name, "bprint") == 0)
5186 event->flags |= EVENT_FL_ISBPRINT;
5189 event->id = event_read_id();
5190 if (event->id < 0) {
5191 ret = PEVENT_ERRNO__READ_ID_FAILED;
5193 * This isn't an allocation error actually.
5194 * But as the ID is critical, just bail out.
5196 goto event_alloc_failed;
5199 event->system = strdup(sys);
5200 if (!event->system) {
5201 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5202 goto event_alloc_failed;
5205 /* Add pevent to event so that it can be referenced */
5206 event->pevent = pevent;
5208 ret = event_read_format(event);
5209 if (ret < 0) {
5210 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5211 goto event_parse_failed;
5215 * If the event has an override, don't print warnings if the event
5216 * print format fails to parse.
5218 if (pevent && find_event_handle(pevent, event))
5219 show_warning = 0;
5221 ret = event_read_print(event);
5222 show_warning = 1;
5224 if (ret < 0) {
5225 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5226 goto event_parse_failed;
5229 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5230 struct format_field *field;
5231 struct print_arg *arg, **list;
5233 /* old ftrace had no args */
5234 list = &event->print_fmt.args;
5235 for (field = event->format.fields; field; field = field->next) {
5236 arg = alloc_arg();
5237 if (!arg) {
5238 event->flags |= EVENT_FL_FAILED;
5239 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5241 arg->type = PRINT_FIELD;
5242 arg->field.name = strdup(field->name);
5243 if (!arg->field.name) {
5244 event->flags |= EVENT_FL_FAILED;
5245 free_arg(arg);
5246 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5248 arg->field.field = field;
5249 *list = arg;
5250 list = &arg->next;
5252 return 0;
5255 return 0;
5257 event_parse_failed:
5258 event->flags |= EVENT_FL_FAILED;
5259 return ret;
5261 event_alloc_failed:
5262 free(event->system);
5263 free(event->name);
5264 free(event);
5265 *eventp = NULL;
5266 return ret;
5269 static enum pevent_errno
5270 __pevent_parse_event(struct pevent *pevent,
5271 struct event_format **eventp,
5272 const char *buf, unsigned long size,
5273 const char *sys)
5275 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5276 struct event_format *event = *eventp;
5278 if (event == NULL)
5279 return ret;
5281 if (pevent && add_event(pevent, event)) {
5282 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5283 goto event_add_failed;
5286 #define PRINT_ARGS 0
5287 if (PRINT_ARGS && event->print_fmt.args)
5288 print_args(event->print_fmt.args);
5290 return 0;
5292 event_add_failed:
5293 pevent_free_format(event);
5294 return ret;
5298 * pevent_parse_format - parse the event format
5299 * @pevent: the handle to the pevent
5300 * @eventp: returned format
5301 * @buf: the buffer storing the event format string
5302 * @size: the size of @buf
5303 * @sys: the system the event belongs to
5305 * This parses the event format and creates an event structure
5306 * to quickly parse raw data for a given event.
5308 * These files currently come from:
5310 * /sys/kernel/debug/tracing/events/.../.../format
5312 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5313 struct event_format **eventp,
5314 const char *buf,
5315 unsigned long size, const char *sys)
5317 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5321 * pevent_parse_event - parse the event format
5322 * @pevent: the handle to the pevent
5323 * @buf: the buffer storing the event format string
5324 * @size: the size of @buf
5325 * @sys: the system the event belongs to
5327 * This parses the event format and creates an event structure
5328 * to quickly parse raw data for a given event.
5330 * These files currently come from:
5332 * /sys/kernel/debug/tracing/events/.../.../format
5334 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5335 unsigned long size, const char *sys)
5337 struct event_format *event = NULL;
5338 return __pevent_parse_event(pevent, &event, buf, size, sys);
5341 #undef _PE
5342 #define _PE(code, str) str
5343 static const char * const pevent_error_str[] = {
5344 PEVENT_ERRORS
5346 #undef _PE
5348 int pevent_strerror(struct pevent *pevent __maybe_unused,
5349 enum pevent_errno errnum, char *buf, size_t buflen)
5351 int idx;
5352 const char *msg;
5354 if (errnum >= 0) {
5355 msg = strerror_r(errnum, buf, buflen);
5356 if (msg != buf) {
5357 size_t len = strlen(msg);
5358 memcpy(buf, msg, min(buflen - 1, len));
5359 *(buf + min(buflen - 1, len)) = '\0';
5361 return 0;
5364 if (errnum <= __PEVENT_ERRNO__START ||
5365 errnum >= __PEVENT_ERRNO__END)
5366 return -1;
5368 idx = errnum - __PEVENT_ERRNO__START - 1;
5369 msg = pevent_error_str[idx];
5370 snprintf(buf, buflen, "%s", msg);
5372 return 0;
5375 int get_field_val(struct trace_seq *s, struct format_field *field,
5376 const char *name, struct pevent_record *record,
5377 unsigned long long *val, int err)
5379 if (!field) {
5380 if (err)
5381 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5382 return -1;
5385 if (pevent_read_number_field(field, record->data, val)) {
5386 if (err)
5387 trace_seq_printf(s, " %s=INVALID", name);
5388 return -1;
5391 return 0;
5395 * pevent_get_field_raw - return the raw pointer into the data field
5396 * @s: The seq to print to on error
5397 * @event: the event that the field is for
5398 * @name: The name of the field
5399 * @record: The record with the field name.
5400 * @len: place to store the field length.
5401 * @err: print default error if failed.
5403 * Returns a pointer into record->data of the field and places
5404 * the length of the field in @len.
5406 * On failure, it returns NULL.
5408 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5409 const char *name, struct pevent_record *record,
5410 int *len, int err)
5412 struct format_field *field;
5413 void *data = record->data;
5414 unsigned offset;
5415 int dummy;
5417 if (!event)
5418 return NULL;
5420 field = pevent_find_field(event, name);
5422 if (!field) {
5423 if (err)
5424 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5425 return NULL;
5428 /* Allow @len to be NULL */
5429 if (!len)
5430 len = &dummy;
5432 offset = field->offset;
5433 if (field->flags & FIELD_IS_DYNAMIC) {
5434 offset = pevent_read_number(event->pevent,
5435 data + offset, field->size);
5436 *len = offset >> 16;
5437 offset &= 0xffff;
5438 } else
5439 *len = field->size;
5441 return data + offset;
5445 * pevent_get_field_val - find a field and return its value
5446 * @s: The seq to print to on error
5447 * @event: the event that the field is for
5448 * @name: The name of the field
5449 * @record: The record with the field name.
5450 * @val: place to store the value of the field.
5451 * @err: print default error if failed.
5453 * Returns 0 on success -1 on field not found.
5455 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5456 const char *name, struct pevent_record *record,
5457 unsigned long long *val, int err)
5459 struct format_field *field;
5461 if (!event)
5462 return -1;
5464 field = pevent_find_field(event, name);
5466 return get_field_val(s, field, name, record, val, err);
5470 * pevent_get_common_field_val - find a common field and return its value
5471 * @s: The seq to print to on error
5472 * @event: the event that the field is for
5473 * @name: The name of the field
5474 * @record: The record with the field name.
5475 * @val: place to store the value of the field.
5476 * @err: print default error if failed.
5478 * Returns 0 on success -1 on field not found.
5480 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5481 const char *name, struct pevent_record *record,
5482 unsigned long long *val, int err)
5484 struct format_field *field;
5486 if (!event)
5487 return -1;
5489 field = pevent_find_common_field(event, name);
5491 return get_field_val(s, field, name, record, val, err);
5495 * pevent_get_any_field_val - find a any field and return its value
5496 * @s: The seq to print to on error
5497 * @event: the event that the field is for
5498 * @name: The name of the field
5499 * @record: The record with the field name.
5500 * @val: place to store the value of the field.
5501 * @err: print default error if failed.
5503 * Returns 0 on success -1 on field not found.
5505 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5506 const char *name, struct pevent_record *record,
5507 unsigned long long *val, int err)
5509 struct format_field *field;
5511 if (!event)
5512 return -1;
5514 field = pevent_find_any_field(event, name);
5516 return get_field_val(s, field, name, record, val, err);
5520 * pevent_print_num_field - print a field and a format
5521 * @s: The seq to print to
5522 * @fmt: The printf format to print the field with.
5523 * @event: the event that the field is for
5524 * @name: The name of the field
5525 * @record: The record with the field name.
5526 * @err: print default error if failed.
5528 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5530 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5531 struct event_format *event, const char *name,
5532 struct pevent_record *record, int err)
5534 struct format_field *field = pevent_find_field(event, name);
5535 unsigned long long val;
5537 if (!field)
5538 goto failed;
5540 if (pevent_read_number_field(field, record->data, &val))
5541 goto failed;
5543 return trace_seq_printf(s, fmt, val);
5545 failed:
5546 if (err)
5547 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5548 return -1;
5552 * pevent_print_func_field - print a field and a format for function pointers
5553 * @s: The seq to print to
5554 * @fmt: The printf format to print the field with.
5555 * @event: the event that the field is for
5556 * @name: The name of the field
5557 * @record: The record with the field name.
5558 * @err: print default error if failed.
5560 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5562 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5563 struct event_format *event, const char *name,
5564 struct pevent_record *record, int err)
5566 struct format_field *field = pevent_find_field(event, name);
5567 struct pevent *pevent = event->pevent;
5568 unsigned long long val;
5569 struct func_map *func;
5570 char tmp[128];
5572 if (!field)
5573 goto failed;
5575 if (pevent_read_number_field(field, record->data, &val))
5576 goto failed;
5578 func = find_func(pevent, val);
5580 if (func)
5581 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5582 else
5583 sprintf(tmp, "0x%08llx", val);
5585 return trace_seq_printf(s, fmt, tmp);
5587 failed:
5588 if (err)
5589 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5590 return -1;
5593 static void free_func_handle(struct pevent_function_handler *func)
5595 struct pevent_func_params *params;
5597 free(func->name);
5599 while (func->params) {
5600 params = func->params;
5601 func->params = params->next;
5602 free(params);
5605 free(func);
5609 * pevent_register_print_function - register a helper function
5610 * @pevent: the handle to the pevent
5611 * @func: the function to process the helper function
5612 * @ret_type: the return type of the helper function
5613 * @name: the name of the helper function
5614 * @parameters: A list of enum pevent_func_arg_type
5616 * Some events may have helper functions in the print format arguments.
5617 * This allows a plugin to dynamically create a way to process one
5618 * of these functions.
5620 * The @parameters is a variable list of pevent_func_arg_type enums that
5621 * must end with PEVENT_FUNC_ARG_VOID.
5623 int pevent_register_print_function(struct pevent *pevent,
5624 pevent_func_handler func,
5625 enum pevent_func_arg_type ret_type,
5626 char *name, ...)
5628 struct pevent_function_handler *func_handle;
5629 struct pevent_func_params **next_param;
5630 struct pevent_func_params *param;
5631 enum pevent_func_arg_type type;
5632 va_list ap;
5633 int ret;
5635 func_handle = find_func_handler(pevent, name);
5636 if (func_handle) {
5638 * This is most like caused by the users own
5639 * plugins updating the function. This overrides the
5640 * system defaults.
5642 pr_stat("override of function helper '%s'", name);
5643 remove_func_handler(pevent, name);
5646 func_handle = calloc(1, sizeof(*func_handle));
5647 if (!func_handle) {
5648 do_warning("Failed to allocate function handler");
5649 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5652 func_handle->ret_type = ret_type;
5653 func_handle->name = strdup(name);
5654 func_handle->func = func;
5655 if (!func_handle->name) {
5656 do_warning("Failed to allocate function name");
5657 free(func_handle);
5658 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5661 next_param = &(func_handle->params);
5662 va_start(ap, name);
5663 for (;;) {
5664 type = va_arg(ap, enum pevent_func_arg_type);
5665 if (type == PEVENT_FUNC_ARG_VOID)
5666 break;
5668 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5669 do_warning("Invalid argument type %d", type);
5670 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5671 goto out_free;
5674 param = malloc(sizeof(*param));
5675 if (!param) {
5676 do_warning("Failed to allocate function param");
5677 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5678 goto out_free;
5680 param->type = type;
5681 param->next = NULL;
5683 *next_param = param;
5684 next_param = &(param->next);
5686 func_handle->nr_args++;
5688 va_end(ap);
5690 func_handle->next = pevent->func_handlers;
5691 pevent->func_handlers = func_handle;
5693 return 0;
5694 out_free:
5695 va_end(ap);
5696 free_func_handle(func_handle);
5697 return ret;
5701 * pevent_unregister_print_function - unregister a helper function
5702 * @pevent: the handle to the pevent
5703 * @func: the function to process the helper function
5704 * @name: the name of the helper function
5706 * This function removes existing print handler for function @name.
5708 * Returns 0 if the handler was removed successully, -1 otherwise.
5710 int pevent_unregister_print_function(struct pevent *pevent,
5711 pevent_func_handler func, char *name)
5713 struct pevent_function_handler *func_handle;
5715 func_handle = find_func_handler(pevent, name);
5716 if (func_handle && func_handle->func == func) {
5717 remove_func_handler(pevent, name);
5718 return 0;
5720 return -1;
5723 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
5724 const char *sys_name,
5725 const char *event_name)
5727 struct event_format *event;
5729 if (id >= 0) {
5730 /* search by id */
5731 event = pevent_find_event(pevent, id);
5732 if (!event)
5733 return NULL;
5734 if (event_name && (strcmp(event_name, event->name) != 0))
5735 return NULL;
5736 if (sys_name && (strcmp(sys_name, event->system) != 0))
5737 return NULL;
5738 } else {
5739 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5740 if (!event)
5741 return NULL;
5743 return event;
5747 * pevent_register_event_handler - register a way to parse an event
5748 * @pevent: the handle to the pevent
5749 * @id: the id of the event to register
5750 * @sys_name: the system name the event belongs to
5751 * @event_name: the name of the event
5752 * @func: the function to call to parse the event information
5753 * @context: the data to be passed to @func
5755 * This function allows a developer to override the parsing of
5756 * a given event. If for some reason the default print format
5757 * is not sufficient, this function will register a function
5758 * for an event to be used to parse the data instead.
5760 * If @id is >= 0, then it is used to find the event.
5761 * else @sys_name and @event_name are used.
5763 int pevent_register_event_handler(struct pevent *pevent, int id,
5764 const char *sys_name, const char *event_name,
5765 pevent_event_handler_func func, void *context)
5767 struct event_format *event;
5768 struct event_handler *handle;
5770 event = pevent_search_event(pevent, id, sys_name, event_name);
5771 if (event == NULL)
5772 goto not_found;
5774 pr_stat("overriding event (%d) %s:%s with new print handler",
5775 event->id, event->system, event->name);
5777 event->handler = func;
5778 event->context = context;
5779 return 0;
5781 not_found:
5782 /* Save for later use. */
5783 handle = calloc(1, sizeof(*handle));
5784 if (!handle) {
5785 do_warning("Failed to allocate event handler");
5786 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5789 handle->id = id;
5790 if (event_name)
5791 handle->event_name = strdup(event_name);
5792 if (sys_name)
5793 handle->sys_name = strdup(sys_name);
5795 if ((event_name && !handle->event_name) ||
5796 (sys_name && !handle->sys_name)) {
5797 do_warning("Failed to allocate event/sys name");
5798 free((void *)handle->event_name);
5799 free((void *)handle->sys_name);
5800 free(handle);
5801 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5804 handle->func = func;
5805 handle->next = pevent->handlers;
5806 pevent->handlers = handle;
5807 handle->context = context;
5809 return -1;
5812 static int handle_matches(struct event_handler *handler, int id,
5813 const char *sys_name, const char *event_name,
5814 pevent_event_handler_func func, void *context)
5816 if (id >= 0 && id != handler->id)
5817 return 0;
5819 if (event_name && (strcmp(event_name, handler->event_name) != 0))
5820 return 0;
5822 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
5823 return 0;
5825 if (func != handler->func || context != handler->context)
5826 return 0;
5828 return 1;
5832 * pevent_unregister_event_handler - unregister an existing event handler
5833 * @pevent: the handle to the pevent
5834 * @id: the id of the event to unregister
5835 * @sys_name: the system name the handler belongs to
5836 * @event_name: the name of the event handler
5837 * @func: the function to call to parse the event information
5838 * @context: the data to be passed to @func
5840 * This function removes existing event handler (parser).
5842 * If @id is >= 0, then it is used to find the event.
5843 * else @sys_name and @event_name are used.
5845 * Returns 0 if handler was removed successfully, -1 if event was not found.
5847 int pevent_unregister_event_handler(struct pevent *pevent, int id,
5848 const char *sys_name, const char *event_name,
5849 pevent_event_handler_func func, void *context)
5851 struct event_format *event;
5852 struct event_handler *handle;
5853 struct event_handler **next;
5855 event = pevent_search_event(pevent, id, sys_name, event_name);
5856 if (event == NULL)
5857 goto not_found;
5859 if (event->handler == func && event->context == context) {
5860 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
5861 event->id, event->system, event->name);
5863 event->handler = NULL;
5864 event->context = NULL;
5865 return 0;
5868 not_found:
5869 for (next = &pevent->handlers; *next; next = &(*next)->next) {
5870 handle = *next;
5871 if (handle_matches(handle, id, sys_name, event_name,
5872 func, context))
5873 break;
5876 if (!(*next))
5877 return -1;
5879 *next = handle->next;
5880 free_handler(handle);
5882 return 0;
5886 * pevent_alloc - create a pevent handle
5888 struct pevent *pevent_alloc(void)
5890 struct pevent *pevent = calloc(1, sizeof(*pevent));
5892 if (pevent)
5893 pevent->ref_count = 1;
5895 return pevent;
5898 void pevent_ref(struct pevent *pevent)
5900 pevent->ref_count++;
5903 static void free_format_fields(struct format_field *field)
5905 struct format_field *next;
5907 while (field) {
5908 next = field->next;
5909 free(field->type);
5910 free(field->name);
5911 free(field);
5912 field = next;
5916 static void free_formats(struct format *format)
5918 free_format_fields(format->common_fields);
5919 free_format_fields(format->fields);
5922 void pevent_free_format(struct event_format *event)
5924 free(event->name);
5925 free(event->system);
5927 free_formats(&event->format);
5929 free(event->print_fmt.format);
5930 free_args(event->print_fmt.args);
5932 free(event);
5936 * pevent_free - free a pevent handle
5937 * @pevent: the pevent handle to free
5939 void pevent_free(struct pevent *pevent)
5941 struct cmdline_list *cmdlist, *cmdnext;
5942 struct func_list *funclist, *funcnext;
5943 struct printk_list *printklist, *printknext;
5944 struct pevent_function_handler *func_handler;
5945 struct event_handler *handle;
5946 int i;
5948 if (!pevent)
5949 return;
5951 cmdlist = pevent->cmdlist;
5952 funclist = pevent->funclist;
5953 printklist = pevent->printklist;
5955 pevent->ref_count--;
5956 if (pevent->ref_count)
5957 return;
5959 if (pevent->cmdlines) {
5960 for (i = 0; i < pevent->cmdline_count; i++)
5961 free(pevent->cmdlines[i].comm);
5962 free(pevent->cmdlines);
5965 while (cmdlist) {
5966 cmdnext = cmdlist->next;
5967 free(cmdlist->comm);
5968 free(cmdlist);
5969 cmdlist = cmdnext;
5972 if (pevent->func_map) {
5973 for (i = 0; i < (int)pevent->func_count; i++) {
5974 free(pevent->func_map[i].func);
5975 free(pevent->func_map[i].mod);
5977 free(pevent->func_map);
5980 while (funclist) {
5981 funcnext = funclist->next;
5982 free(funclist->func);
5983 free(funclist->mod);
5984 free(funclist);
5985 funclist = funcnext;
5988 while (pevent->func_handlers) {
5989 func_handler = pevent->func_handlers;
5990 pevent->func_handlers = func_handler->next;
5991 free_func_handle(func_handler);
5994 if (pevent->printk_map) {
5995 for (i = 0; i < (int)pevent->printk_count; i++)
5996 free(pevent->printk_map[i].printk);
5997 free(pevent->printk_map);
6000 while (printklist) {
6001 printknext = printklist->next;
6002 free(printklist->printk);
6003 free(printklist);
6004 printklist = printknext;
6007 for (i = 0; i < pevent->nr_events; i++)
6008 pevent_free_format(pevent->events[i]);
6010 while (pevent->handlers) {
6011 handle = pevent->handlers;
6012 pevent->handlers = handle->next;
6013 free_handler(handle);
6016 free(pevent->events);
6017 free(pevent->sort_events);
6019 free(pevent);
6022 void pevent_unref(struct pevent *pevent)
6024 pevent_free(pevent);