fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / lib / traceevent / event-parse.c
blob1587ea392ad6d83acb0de14b1701db12fa4fc95c
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 static void init_input_buf(const char *buf, unsigned long long size)
55 input_buf = buf;
56 input_buf_siz = size;
57 input_buf_ptr = 0;
60 const char *pevent_get_input_buf(void)
62 return input_buf;
65 unsigned long long pevent_get_input_buf_ptr(void)
67 return input_buf_ptr;
70 struct event_handler {
71 struct event_handler *next;
72 int id;
73 const char *sys_name;
74 const char *event_name;
75 pevent_event_handler_func func;
76 void *context;
79 struct pevent_func_params {
80 struct pevent_func_params *next;
81 enum pevent_func_arg_type type;
84 struct pevent_function_handler {
85 struct pevent_function_handler *next;
86 enum pevent_func_arg_type ret_type;
87 char *name;
88 pevent_func_handler func;
89 struct pevent_func_params *params;
90 int nr_args;
93 static unsigned long long
94 process_defined_func(struct trace_seq *s, void *data, int size,
95 struct event_format *event, struct print_arg *arg);
97 static void free_func_handle(struct pevent_function_handler *func);
99 /**
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
107 void pevent_buffer_init(const char *buf, unsigned long long size)
109 init_input_buf(buf, size);
112 void breakpoint(void)
114 static int x;
115 x++;
118 struct print_arg *alloc_arg(void)
120 return calloc(1, sizeof(struct print_arg));
123 struct cmdline {
124 char *comm;
125 int pid;
128 static int cmdline_cmp(const void *a, const void *b)
130 const struct cmdline *ca = a;
131 const struct cmdline *cb = b;
133 if (ca->pid < cb->pid)
134 return -1;
135 if (ca->pid > cb->pid)
136 return 1;
138 return 0;
141 struct cmdline_list {
142 struct cmdline_list *next;
143 char *comm;
144 int pid;
147 static int cmdline_init(struct pevent *pevent)
149 struct cmdline_list *cmdlist = pevent->cmdlist;
150 struct cmdline_list *item;
151 struct cmdline *cmdlines;
152 int i;
154 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
155 if (!cmdlines)
156 return -1;
158 i = 0;
159 while (cmdlist) {
160 cmdlines[i].pid = cmdlist->pid;
161 cmdlines[i].comm = cmdlist->comm;
162 i++;
163 item = cmdlist;
164 cmdlist = cmdlist->next;
165 free(item);
168 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
170 pevent->cmdlines = cmdlines;
171 pevent->cmdlist = NULL;
173 return 0;
176 static const char *find_cmdline(struct pevent *pevent, int pid)
178 const struct cmdline *comm;
179 struct cmdline key;
181 if (!pid)
182 return "<idle>";
184 if (!pevent->cmdlines && cmdline_init(pevent))
185 return "<not enough memory for cmdlines!>";
187 key.pid = pid;
189 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
190 sizeof(*pevent->cmdlines), cmdline_cmp);
192 if (comm)
193 return comm->comm;
194 return "<...>";
198 * pevent_pid_is_registered - return if a pid has a cmdline registered
199 * @pevent: handle for the pevent
200 * @pid: The pid to check if it has a cmdline registered with.
202 * Returns 1 if the pid has a cmdline mapped to it
203 * 0 otherwise.
205 int pevent_pid_is_registered(struct pevent *pevent, int pid)
207 const struct cmdline *comm;
208 struct cmdline key;
210 if (!pid)
211 return 1;
213 if (!pevent->cmdlines && cmdline_init(pevent))
214 return 0;
216 key.pid = pid;
218 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
219 sizeof(*pevent->cmdlines), cmdline_cmp);
221 if (comm)
222 return 1;
223 return 0;
227 * If the command lines have been converted to an array, then
228 * we must add this pid. This is much slower than when cmdlines
229 * are added before the array is initialized.
231 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
233 struct cmdline *cmdlines = pevent->cmdlines;
234 const struct cmdline *cmdline;
235 struct cmdline key;
237 if (!pid)
238 return 0;
240 /* avoid duplicates */
241 key.pid = pid;
243 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
244 sizeof(*pevent->cmdlines), cmdline_cmp);
245 if (cmdline) {
246 errno = EEXIST;
247 return -1;
250 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
251 if (!cmdlines) {
252 errno = ENOMEM;
253 return -1;
256 cmdlines[pevent->cmdline_count].comm = strdup(comm);
257 if (!cmdlines[pevent->cmdline_count].comm) {
258 free(cmdlines);
259 errno = ENOMEM;
260 return -1;
263 cmdlines[pevent->cmdline_count].pid = pid;
265 if (cmdlines[pevent->cmdline_count].comm)
266 pevent->cmdline_count++;
268 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
269 pevent->cmdlines = cmdlines;
271 return 0;
275 * pevent_register_comm - register a pid / comm mapping
276 * @pevent: handle for the pevent
277 * @comm: the command line to register
278 * @pid: the pid to map the command line to
280 * This adds a mapping to search for command line names with
281 * a given pid. The comm is duplicated.
283 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
285 struct cmdline_list *item;
287 if (pevent->cmdlines)
288 return add_new_comm(pevent, comm, pid);
290 item = malloc(sizeof(*item));
291 if (!item)
292 return -1;
294 item->comm = strdup(comm);
295 if (!item->comm) {
296 free(item);
297 return -1;
299 item->pid = pid;
300 item->next = pevent->cmdlist;
302 pevent->cmdlist = item;
303 pevent->cmdline_count++;
305 return 0;
308 void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
310 pevent->trace_clock = trace_clock;
313 struct func_map {
314 unsigned long long addr;
315 char *func;
316 char *mod;
319 struct func_list {
320 struct func_list *next;
321 unsigned long long addr;
322 char *func;
323 char *mod;
326 static int func_cmp(const void *a, const void *b)
328 const struct func_map *fa = a;
329 const struct func_map *fb = b;
331 if (fa->addr < fb->addr)
332 return -1;
333 if (fa->addr > fb->addr)
334 return 1;
336 return 0;
340 * We are searching for a record in between, not an exact
341 * match.
343 static int func_bcmp(const void *a, const void *b)
345 const struct func_map *fa = a;
346 const struct func_map *fb = b;
348 if ((fa->addr == fb->addr) ||
350 (fa->addr > fb->addr &&
351 fa->addr < (fb+1)->addr))
352 return 0;
354 if (fa->addr < fb->addr)
355 return -1;
357 return 1;
360 static int func_map_init(struct pevent *pevent)
362 struct func_list *funclist;
363 struct func_list *item;
364 struct func_map *func_map;
365 int i;
367 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
368 if (!func_map)
369 return -1;
371 funclist = pevent->funclist;
373 i = 0;
374 while (funclist) {
375 func_map[i].func = funclist->func;
376 func_map[i].addr = funclist->addr;
377 func_map[i].mod = funclist->mod;
378 i++;
379 item = funclist;
380 funclist = funclist->next;
381 free(item);
384 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
387 * Add a special record at the end.
389 func_map[pevent->func_count].func = NULL;
390 func_map[pevent->func_count].addr = 0;
391 func_map[pevent->func_count].mod = NULL;
393 pevent->func_map = func_map;
394 pevent->funclist = NULL;
396 return 0;
399 static struct func_map *
400 find_func(struct pevent *pevent, unsigned long long addr)
402 struct func_map *func;
403 struct func_map key;
405 if (!pevent->func_map)
406 func_map_init(pevent);
408 key.addr = addr;
410 func = bsearch(&key, pevent->func_map, pevent->func_count,
411 sizeof(*pevent->func_map), func_bcmp);
413 return func;
417 * pevent_find_function - find a function by a given address
418 * @pevent: handle for the pevent
419 * @addr: the address to find the function with
421 * Returns a pointer to the function stored that has the given
422 * address. Note, the address does not have to be exact, it
423 * will select the function that would contain the address.
425 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
427 struct func_map *map;
429 map = find_func(pevent, addr);
430 if (!map)
431 return NULL;
433 return map->func;
437 * pevent_find_function_address - find a function address by a given address
438 * @pevent: handle for the pevent
439 * @addr: the address to find the function with
441 * Returns the address the function starts at. This can be used in
442 * conjunction with pevent_find_function to print both the function
443 * name and the function offset.
445 unsigned long long
446 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
448 struct func_map *map;
450 map = find_func(pevent, addr);
451 if (!map)
452 return 0;
454 return map->addr;
458 * pevent_register_function - register a function with a given address
459 * @pevent: handle for the pevent
460 * @function: the function name to register
461 * @addr: the address the function starts at
462 * @mod: the kernel module the function may be in (NULL for none)
464 * This registers a function name with an address and module.
465 * The @func passed in is duplicated.
467 int pevent_register_function(struct pevent *pevent, char *func,
468 unsigned long long addr, char *mod)
470 struct func_list *item = malloc(sizeof(*item));
472 if (!item)
473 return -1;
475 item->next = pevent->funclist;
476 item->func = strdup(func);
477 if (!item->func)
478 goto out_free;
480 if (mod) {
481 item->mod = strdup(mod);
482 if (!item->mod)
483 goto out_free_func;
484 } else
485 item->mod = NULL;
486 item->addr = addr;
488 pevent->funclist = item;
489 pevent->func_count++;
491 return 0;
493 out_free_func:
494 free(item->func);
495 item->func = NULL;
496 out_free:
497 free(item);
498 errno = ENOMEM;
499 return -1;
503 * pevent_print_funcs - print out the stored functions
504 * @pevent: handle for the pevent
506 * This prints out the stored functions.
508 void pevent_print_funcs(struct pevent *pevent)
510 int i;
512 if (!pevent->func_map)
513 func_map_init(pevent);
515 for (i = 0; i < (int)pevent->func_count; i++) {
516 printf("%016llx %s",
517 pevent->func_map[i].addr,
518 pevent->func_map[i].func);
519 if (pevent->func_map[i].mod)
520 printf(" [%s]\n", pevent->func_map[i].mod);
521 else
522 printf("\n");
526 struct printk_map {
527 unsigned long long addr;
528 char *printk;
531 struct printk_list {
532 struct printk_list *next;
533 unsigned long long addr;
534 char *printk;
537 static int printk_cmp(const void *a, const void *b)
539 const struct printk_map *pa = a;
540 const struct printk_map *pb = b;
542 if (pa->addr < pb->addr)
543 return -1;
544 if (pa->addr > pb->addr)
545 return 1;
547 return 0;
550 static int printk_map_init(struct pevent *pevent)
552 struct printk_list *printklist;
553 struct printk_list *item;
554 struct printk_map *printk_map;
555 int i;
557 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
558 if (!printk_map)
559 return -1;
561 printklist = pevent->printklist;
563 i = 0;
564 while (printklist) {
565 printk_map[i].printk = printklist->printk;
566 printk_map[i].addr = printklist->addr;
567 i++;
568 item = printklist;
569 printklist = printklist->next;
570 free(item);
573 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
575 pevent->printk_map = printk_map;
576 pevent->printklist = NULL;
578 return 0;
581 static struct printk_map *
582 find_printk(struct pevent *pevent, unsigned long long addr)
584 struct printk_map *printk;
585 struct printk_map key;
587 if (!pevent->printk_map && printk_map_init(pevent))
588 return NULL;
590 key.addr = addr;
592 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
593 sizeof(*pevent->printk_map), printk_cmp);
595 return printk;
599 * pevent_register_print_string - register a string by its address
600 * @pevent: handle for the pevent
601 * @fmt: the string format to register
602 * @addr: the address the string was located at
604 * This registers a string by the address it was stored in the kernel.
605 * The @fmt passed in is duplicated.
607 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
608 unsigned long long addr)
610 struct printk_list *item = malloc(sizeof(*item));
611 char *p;
613 if (!item)
614 return -1;
616 item->next = pevent->printklist;
617 item->addr = addr;
619 /* Strip off quotes and '\n' from the end */
620 if (fmt[0] == '"')
621 fmt++;
622 item->printk = strdup(fmt);
623 if (!item->printk)
624 goto out_free;
626 p = item->printk + strlen(item->printk) - 1;
627 if (*p == '"')
628 *p = 0;
630 p -= 2;
631 if (strcmp(p, "\\n") == 0)
632 *p = 0;
634 pevent->printklist = item;
635 pevent->printk_count++;
637 return 0;
639 out_free:
640 free(item);
641 errno = ENOMEM;
642 return -1;
646 * pevent_print_printk - print out the stored strings
647 * @pevent: handle for the pevent
649 * This prints the string formats that were stored.
651 void pevent_print_printk(struct pevent *pevent)
653 int i;
655 if (!pevent->printk_map)
656 printk_map_init(pevent);
658 for (i = 0; i < (int)pevent->printk_count; i++) {
659 printf("%016llx %s\n",
660 pevent->printk_map[i].addr,
661 pevent->printk_map[i].printk);
665 static struct event_format *alloc_event(void)
667 return calloc(1, sizeof(struct event_format));
670 static int add_event(struct pevent *pevent, struct event_format *event)
672 int i;
673 struct event_format **events = realloc(pevent->events, sizeof(event) *
674 (pevent->nr_events + 1));
675 if (!events)
676 return -1;
678 pevent->events = events;
680 for (i = 0; i < pevent->nr_events; i++) {
681 if (pevent->events[i]->id > event->id)
682 break;
684 if (i < pevent->nr_events)
685 memmove(&pevent->events[i + 1],
686 &pevent->events[i],
687 sizeof(event) * (pevent->nr_events - i));
689 pevent->events[i] = event;
690 pevent->nr_events++;
692 event->pevent = pevent;
694 return 0;
697 static int event_item_type(enum event_type type)
699 switch (type) {
700 case EVENT_ITEM ... EVENT_SQUOTE:
701 return 1;
702 case EVENT_ERROR ... EVENT_DELIM:
703 default:
704 return 0;
708 static void free_flag_sym(struct print_flag_sym *fsym)
710 struct print_flag_sym *next;
712 while (fsym) {
713 next = fsym->next;
714 free(fsym->value);
715 free(fsym->str);
716 free(fsym);
717 fsym = next;
721 static void free_arg(struct print_arg *arg)
723 struct print_arg *farg;
725 if (!arg)
726 return;
728 switch (arg->type) {
729 case PRINT_ATOM:
730 free(arg->atom.atom);
731 break;
732 case PRINT_FIELD:
733 free(arg->field.name);
734 break;
735 case PRINT_FLAGS:
736 free_arg(arg->flags.field);
737 free(arg->flags.delim);
738 free_flag_sym(arg->flags.flags);
739 break;
740 case PRINT_SYMBOL:
741 free_arg(arg->symbol.field);
742 free_flag_sym(arg->symbol.symbols);
743 break;
744 case PRINT_HEX:
745 free_arg(arg->hex.field);
746 free_arg(arg->hex.size);
747 break;
748 case PRINT_TYPE:
749 free(arg->typecast.type);
750 free_arg(arg->typecast.item);
751 break;
752 case PRINT_STRING:
753 case PRINT_BSTRING:
754 free(arg->string.string);
755 break;
756 case PRINT_DYNAMIC_ARRAY:
757 free(arg->dynarray.index);
758 break;
759 case PRINT_OP:
760 free(arg->op.op);
761 free_arg(arg->op.left);
762 free_arg(arg->op.right);
763 break;
764 case PRINT_FUNC:
765 while (arg->func.args) {
766 farg = arg->func.args;
767 arg->func.args = farg->next;
768 free_arg(farg);
770 break;
772 case PRINT_NULL:
773 default:
774 break;
777 free(arg);
780 static enum event_type get_type(int ch)
782 if (ch == '\n')
783 return EVENT_NEWLINE;
784 if (isspace(ch))
785 return EVENT_SPACE;
786 if (isalnum(ch) || ch == '_')
787 return EVENT_ITEM;
788 if (ch == '\'')
789 return EVENT_SQUOTE;
790 if (ch == '"')
791 return EVENT_DQUOTE;
792 if (!isprint(ch))
793 return EVENT_NONE;
794 if (ch == '(' || ch == ')' || ch == ',')
795 return EVENT_DELIM;
797 return EVENT_OP;
800 static int __read_char(void)
802 if (input_buf_ptr >= input_buf_siz)
803 return -1;
805 return input_buf[input_buf_ptr++];
808 static int __peek_char(void)
810 if (input_buf_ptr >= input_buf_siz)
811 return -1;
813 return input_buf[input_buf_ptr];
817 * pevent_peek_char - peek at the next character that will be read
819 * Returns the next character read, or -1 if end of buffer.
821 int pevent_peek_char(void)
823 return __peek_char();
826 static int extend_token(char **tok, char *buf, int size)
828 char *newtok = realloc(*tok, size);
830 if (!newtok) {
831 free(*tok);
832 *tok = NULL;
833 return -1;
836 if (!*tok)
837 strcpy(newtok, buf);
838 else
839 strcat(newtok, buf);
840 *tok = newtok;
842 return 0;
845 static enum event_type force_token(const char *str, char **tok);
847 static enum event_type __read_token(char **tok)
849 char buf[BUFSIZ];
850 int ch, last_ch, quote_ch, next_ch;
851 int i = 0;
852 int tok_size = 0;
853 enum event_type type;
855 *tok = NULL;
858 ch = __read_char();
859 if (ch < 0)
860 return EVENT_NONE;
862 type = get_type(ch);
863 if (type == EVENT_NONE)
864 return type;
866 buf[i++] = ch;
868 switch (type) {
869 case EVENT_NEWLINE:
870 case EVENT_DELIM:
871 if (asprintf(tok, "%c", ch) < 0)
872 return EVENT_ERROR;
874 return type;
876 case EVENT_OP:
877 switch (ch) {
878 case '-':
879 next_ch = __peek_char();
880 if (next_ch == '>') {
881 buf[i++] = __read_char();
882 break;
884 /* fall through */
885 case '+':
886 case '|':
887 case '&':
888 case '>':
889 case '<':
890 last_ch = ch;
891 ch = __peek_char();
892 if (ch != last_ch)
893 goto test_equal;
894 buf[i++] = __read_char();
895 switch (last_ch) {
896 case '>':
897 case '<':
898 goto test_equal;
899 default:
900 break;
902 break;
903 case '!':
904 case '=':
905 goto test_equal;
906 default: /* what should we do instead? */
907 break;
909 buf[i] = 0;
910 *tok = strdup(buf);
911 return type;
913 test_equal:
914 ch = __peek_char();
915 if (ch == '=')
916 buf[i++] = __read_char();
917 goto out;
919 case EVENT_DQUOTE:
920 case EVENT_SQUOTE:
921 /* don't keep quotes */
922 i--;
923 quote_ch = ch;
924 last_ch = 0;
925 concat:
926 do {
927 if (i == (BUFSIZ - 1)) {
928 buf[i] = 0;
929 tok_size += BUFSIZ;
931 if (extend_token(tok, buf, tok_size) < 0)
932 return EVENT_NONE;
933 i = 0;
935 last_ch = ch;
936 ch = __read_char();
937 buf[i++] = ch;
938 /* the '\' '\' will cancel itself */
939 if (ch == '\\' && last_ch == '\\')
940 last_ch = 0;
941 } while (ch != quote_ch || last_ch == '\\');
942 /* remove the last quote */
943 i--;
946 * For strings (double quotes) check the next token.
947 * If it is another string, concatinate the two.
949 if (type == EVENT_DQUOTE) {
950 unsigned long long save_input_buf_ptr = input_buf_ptr;
952 do {
953 ch = __read_char();
954 } while (isspace(ch));
955 if (ch == '"')
956 goto concat;
957 input_buf_ptr = save_input_buf_ptr;
960 goto out;
962 case EVENT_ERROR ... EVENT_SPACE:
963 case EVENT_ITEM:
964 default:
965 break;
968 while (get_type(__peek_char()) == type) {
969 if (i == (BUFSIZ - 1)) {
970 buf[i] = 0;
971 tok_size += BUFSIZ;
973 if (extend_token(tok, buf, tok_size) < 0)
974 return EVENT_NONE;
975 i = 0;
977 ch = __read_char();
978 buf[i++] = ch;
981 out:
982 buf[i] = 0;
983 if (extend_token(tok, buf, tok_size + i + 1) < 0)
984 return EVENT_NONE;
986 if (type == EVENT_ITEM) {
988 * Older versions of the kernel has a bug that
989 * creates invalid symbols and will break the mac80211
990 * parsing. This is a work around to that bug.
992 * See Linux kernel commit:
993 * 811cb50baf63461ce0bdb234927046131fc7fa8b
995 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
996 free(*tok);
997 *tok = NULL;
998 return force_token("\"\%s\" ", tok);
999 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1000 free(*tok);
1001 *tok = NULL;
1002 return force_token("\" sta:%pM\" ", tok);
1003 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1004 free(*tok);
1005 *tok = NULL;
1006 return force_token("\" vif:%p(%d)\" ", tok);
1010 return type;
1013 static enum event_type force_token(const char *str, char **tok)
1015 const char *save_input_buf;
1016 unsigned long long save_input_buf_ptr;
1017 unsigned long long save_input_buf_siz;
1018 enum event_type type;
1020 /* save off the current input pointers */
1021 save_input_buf = input_buf;
1022 save_input_buf_ptr = input_buf_ptr;
1023 save_input_buf_siz = input_buf_siz;
1025 init_input_buf(str, strlen(str));
1027 type = __read_token(tok);
1029 /* reset back to original token */
1030 input_buf = save_input_buf;
1031 input_buf_ptr = save_input_buf_ptr;
1032 input_buf_siz = save_input_buf_siz;
1034 return type;
1037 static void free_token(char *tok)
1039 if (tok)
1040 free(tok);
1043 static enum event_type read_token(char **tok)
1045 enum event_type type;
1047 for (;;) {
1048 type = __read_token(tok);
1049 if (type != EVENT_SPACE)
1050 return type;
1052 free_token(*tok);
1055 /* not reached */
1056 *tok = NULL;
1057 return EVENT_NONE;
1061 * pevent_read_token - access to utilites to use the pevent parser
1062 * @tok: The token to return
1064 * This will parse tokens from the string given by
1065 * pevent_init_data().
1067 * Returns the token type.
1069 enum event_type pevent_read_token(char **tok)
1071 return read_token(tok);
1075 * pevent_free_token - free a token returned by pevent_read_token
1076 * @token: the token to free
1078 void pevent_free_token(char *token)
1080 free_token(token);
1083 /* no newline */
1084 static enum event_type read_token_item(char **tok)
1086 enum event_type type;
1088 for (;;) {
1089 type = __read_token(tok);
1090 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1091 return type;
1092 free_token(*tok);
1093 *tok = NULL;
1096 /* not reached */
1097 *tok = NULL;
1098 return EVENT_NONE;
1101 static int test_type(enum event_type type, enum event_type expect)
1103 if (type != expect) {
1104 do_warning("Error: expected type %d but read %d",
1105 expect, type);
1106 return -1;
1108 return 0;
1111 static int test_type_token(enum event_type type, const char *token,
1112 enum event_type expect, const char *expect_tok)
1114 if (type != expect) {
1115 do_warning("Error: expected type %d but read %d",
1116 expect, type);
1117 return -1;
1120 if (strcmp(token, expect_tok) != 0) {
1121 do_warning("Error: expected '%s' but read '%s'",
1122 expect_tok, token);
1123 return -1;
1125 return 0;
1128 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1130 enum event_type type;
1132 if (newline_ok)
1133 type = read_token(tok);
1134 else
1135 type = read_token_item(tok);
1136 return test_type(type, expect);
1139 static int read_expect_type(enum event_type expect, char **tok)
1141 return __read_expect_type(expect, tok, 1);
1144 static int __read_expected(enum event_type expect, const char *str,
1145 int newline_ok)
1147 enum event_type type;
1148 char *token;
1149 int ret;
1151 if (newline_ok)
1152 type = read_token(&token);
1153 else
1154 type = read_token_item(&token);
1156 ret = test_type_token(type, token, expect, str);
1158 free_token(token);
1160 return ret;
1163 static int read_expected(enum event_type expect, const char *str)
1165 return __read_expected(expect, str, 1);
1168 static int read_expected_item(enum event_type expect, const char *str)
1170 return __read_expected(expect, str, 0);
1173 static char *event_read_name(void)
1175 char *token;
1177 if (read_expected(EVENT_ITEM, "name") < 0)
1178 return NULL;
1180 if (read_expected(EVENT_OP, ":") < 0)
1181 return NULL;
1183 if (read_expect_type(EVENT_ITEM, &token) < 0)
1184 goto fail;
1186 return token;
1188 fail:
1189 free_token(token);
1190 return NULL;
1193 static int event_read_id(void)
1195 char *token;
1196 int id;
1198 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1199 return -1;
1201 if (read_expected(EVENT_OP, ":") < 0)
1202 return -1;
1204 if (read_expect_type(EVENT_ITEM, &token) < 0)
1205 goto fail;
1207 id = strtoul(token, NULL, 0);
1208 free_token(token);
1209 return id;
1211 fail:
1212 free_token(token);
1213 return -1;
1216 static int field_is_string(struct format_field *field)
1218 if ((field->flags & FIELD_IS_ARRAY) &&
1219 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1220 strstr(field->type, "s8")))
1221 return 1;
1223 return 0;
1226 static int field_is_dynamic(struct format_field *field)
1228 if (strncmp(field->type, "__data_loc", 10) == 0)
1229 return 1;
1231 return 0;
1234 static int field_is_long(struct format_field *field)
1236 /* includes long long */
1237 if (strstr(field->type, "long"))
1238 return 1;
1240 return 0;
1243 static unsigned int type_size(const char *name)
1245 /* This covers all FIELD_IS_STRING types. */
1246 static struct {
1247 const char *type;
1248 unsigned int size;
1249 } table[] = {
1250 { "u8", 1 },
1251 { "u16", 2 },
1252 { "u32", 4 },
1253 { "u64", 8 },
1254 { "s8", 1 },
1255 { "s16", 2 },
1256 { "s32", 4 },
1257 { "s64", 8 },
1258 { "char", 1 },
1259 { },
1261 int i;
1263 for (i = 0; table[i].type; i++) {
1264 if (!strcmp(table[i].type, name))
1265 return table[i].size;
1268 return 0;
1271 static int event_read_fields(struct event_format *event, struct format_field **fields)
1273 struct format_field *field = NULL;
1274 enum event_type type;
1275 char *token;
1276 char *last_token;
1277 int count = 0;
1279 do {
1280 unsigned int size_dynamic = 0;
1282 type = read_token(&token);
1283 if (type == EVENT_NEWLINE) {
1284 free_token(token);
1285 return count;
1288 count++;
1290 if (test_type_token(type, token, EVENT_ITEM, "field"))
1291 goto fail;
1292 free_token(token);
1294 type = read_token(&token);
1296 * The ftrace fields may still use the "special" name.
1297 * Just ignore it.
1299 if (event->flags & EVENT_FL_ISFTRACE &&
1300 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1301 free_token(token);
1302 type = read_token(&token);
1305 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1306 goto fail;
1308 free_token(token);
1309 if (read_expect_type(EVENT_ITEM, &token) < 0)
1310 goto fail;
1312 last_token = token;
1314 field = calloc(1, sizeof(*field));
1315 if (!field)
1316 goto fail;
1318 field->event = event;
1320 /* read the rest of the type */
1321 for (;;) {
1322 type = read_token(&token);
1323 if (type == EVENT_ITEM ||
1324 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1326 * Some of the ftrace fields are broken and have
1327 * an illegal "." in them.
1329 (event->flags & EVENT_FL_ISFTRACE &&
1330 type == EVENT_OP && strcmp(token, ".") == 0)) {
1332 if (strcmp(token, "*") == 0)
1333 field->flags |= FIELD_IS_POINTER;
1335 if (field->type) {
1336 char *new_type;
1337 new_type = realloc(field->type,
1338 strlen(field->type) +
1339 strlen(last_token) + 2);
1340 if (!new_type) {
1341 free(last_token);
1342 goto fail;
1344 field->type = new_type;
1345 strcat(field->type, " ");
1346 strcat(field->type, last_token);
1347 free(last_token);
1348 } else
1349 field->type = last_token;
1350 last_token = token;
1351 continue;
1354 break;
1357 if (!field->type) {
1358 do_warning("%s: no type found", __func__);
1359 goto fail;
1361 field->name = last_token;
1363 if (test_type(type, EVENT_OP))
1364 goto fail;
1366 if (strcmp(token, "[") == 0) {
1367 enum event_type last_type = type;
1368 char *brackets = token;
1369 char *new_brackets;
1370 int len;
1372 field->flags |= FIELD_IS_ARRAY;
1374 type = read_token(&token);
1376 if (type == EVENT_ITEM)
1377 field->arraylen = strtoul(token, NULL, 0);
1378 else
1379 field->arraylen = 0;
1381 while (strcmp(token, "]") != 0) {
1382 if (last_type == EVENT_ITEM &&
1383 type == EVENT_ITEM)
1384 len = 2;
1385 else
1386 len = 1;
1387 last_type = type;
1389 new_brackets = realloc(brackets,
1390 strlen(brackets) +
1391 strlen(token) + len);
1392 if (!new_brackets) {
1393 free(brackets);
1394 goto fail;
1396 brackets = new_brackets;
1397 if (len == 2)
1398 strcat(brackets, " ");
1399 strcat(brackets, token);
1400 /* We only care about the last token */
1401 field->arraylen = strtoul(token, NULL, 0);
1402 free_token(token);
1403 type = read_token(&token);
1404 if (type == EVENT_NONE) {
1405 do_warning("failed to find token");
1406 goto fail;
1410 free_token(token);
1412 new_brackets = realloc(brackets, strlen(brackets) + 2);
1413 if (!new_brackets) {
1414 free(brackets);
1415 goto fail;
1417 brackets = new_brackets;
1418 strcat(brackets, "]");
1420 /* add brackets to type */
1422 type = read_token(&token);
1424 * If the next token is not an OP, then it is of
1425 * the format: type [] item;
1427 if (type == EVENT_ITEM) {
1428 char *new_type;
1429 new_type = realloc(field->type,
1430 strlen(field->type) +
1431 strlen(field->name) +
1432 strlen(brackets) + 2);
1433 if (!new_type) {
1434 free(brackets);
1435 goto fail;
1437 field->type = new_type;
1438 strcat(field->type, " ");
1439 strcat(field->type, field->name);
1440 size_dynamic = type_size(field->name);
1441 free_token(field->name);
1442 strcat(field->type, brackets);
1443 field->name = token;
1444 type = read_token(&token);
1445 } else {
1446 char *new_type;
1447 new_type = realloc(field->type,
1448 strlen(field->type) +
1449 strlen(brackets) + 1);
1450 if (!new_type) {
1451 free(brackets);
1452 goto fail;
1454 field->type = new_type;
1455 strcat(field->type, brackets);
1457 free(brackets);
1460 if (field_is_string(field))
1461 field->flags |= FIELD_IS_STRING;
1462 if (field_is_dynamic(field))
1463 field->flags |= FIELD_IS_DYNAMIC;
1464 if (field_is_long(field))
1465 field->flags |= FIELD_IS_LONG;
1467 if (test_type_token(type, token, EVENT_OP, ";"))
1468 goto fail;
1469 free_token(token);
1471 if (read_expected(EVENT_ITEM, "offset") < 0)
1472 goto fail_expect;
1474 if (read_expected(EVENT_OP, ":") < 0)
1475 goto fail_expect;
1477 if (read_expect_type(EVENT_ITEM, &token))
1478 goto fail;
1479 field->offset = strtoul(token, NULL, 0);
1480 free_token(token);
1482 if (read_expected(EVENT_OP, ";") < 0)
1483 goto fail_expect;
1485 if (read_expected(EVENT_ITEM, "size") < 0)
1486 goto fail_expect;
1488 if (read_expected(EVENT_OP, ":") < 0)
1489 goto fail_expect;
1491 if (read_expect_type(EVENT_ITEM, &token))
1492 goto fail;
1493 field->size = strtoul(token, NULL, 0);
1494 free_token(token);
1496 if (read_expected(EVENT_OP, ";") < 0)
1497 goto fail_expect;
1499 type = read_token(&token);
1500 if (type != EVENT_NEWLINE) {
1501 /* newer versions of the kernel have a "signed" type */
1502 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1503 goto fail;
1505 free_token(token);
1507 if (read_expected(EVENT_OP, ":") < 0)
1508 goto fail_expect;
1510 if (read_expect_type(EVENT_ITEM, &token))
1511 goto fail;
1513 if (strtoul(token, NULL, 0))
1514 field->flags |= FIELD_IS_SIGNED;
1516 free_token(token);
1517 if (read_expected(EVENT_OP, ";") < 0)
1518 goto fail_expect;
1520 if (read_expect_type(EVENT_NEWLINE, &token))
1521 goto fail;
1524 free_token(token);
1526 if (field->flags & FIELD_IS_ARRAY) {
1527 if (field->arraylen)
1528 field->elementsize = field->size / field->arraylen;
1529 else if (field->flags & FIELD_IS_DYNAMIC)
1530 field->elementsize = size_dynamic;
1531 else if (field->flags & FIELD_IS_STRING)
1532 field->elementsize = 1;
1533 else if (field->flags & FIELD_IS_LONG)
1534 field->elementsize = event->pevent ?
1535 event->pevent->long_size :
1536 sizeof(long);
1537 } else
1538 field->elementsize = field->size;
1540 *fields = field;
1541 fields = &field->next;
1543 } while (1);
1545 return 0;
1547 fail:
1548 free_token(token);
1549 fail_expect:
1550 if (field) {
1551 free(field->type);
1552 free(field->name);
1553 free(field);
1555 return -1;
1558 static int event_read_format(struct event_format *event)
1560 char *token;
1561 int ret;
1563 if (read_expected_item(EVENT_ITEM, "format") < 0)
1564 return -1;
1566 if (read_expected(EVENT_OP, ":") < 0)
1567 return -1;
1569 if (read_expect_type(EVENT_NEWLINE, &token))
1570 goto fail;
1571 free_token(token);
1573 ret = event_read_fields(event, &event->format.common_fields);
1574 if (ret < 0)
1575 return ret;
1576 event->format.nr_common = ret;
1578 ret = event_read_fields(event, &event->format.fields);
1579 if (ret < 0)
1580 return ret;
1581 event->format.nr_fields = ret;
1583 return 0;
1585 fail:
1586 free_token(token);
1587 return -1;
1590 static enum event_type
1591 process_arg_token(struct event_format *event, struct print_arg *arg,
1592 char **tok, enum event_type type);
1594 static enum event_type
1595 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1597 enum event_type type;
1598 char *token;
1600 type = read_token(&token);
1601 *tok = token;
1603 return process_arg_token(event, arg, tok, type);
1606 static enum event_type
1607 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1610 * For __print_symbolic() and __print_flags, we need to completely
1611 * evaluate the first argument, which defines what to print next.
1613 static enum event_type
1614 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1616 enum event_type type;
1618 type = process_arg(event, arg, tok);
1620 while (type == EVENT_OP) {
1621 type = process_op(event, arg, tok);
1624 return type;
1627 static enum event_type
1628 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1630 struct print_arg *arg, *left, *right;
1631 enum event_type type;
1632 char *token = NULL;
1634 arg = alloc_arg();
1635 left = alloc_arg();
1636 right = alloc_arg();
1638 if (!arg || !left || !right) {
1639 do_warning("%s: not enough memory!", __func__);
1640 /* arg will be freed at out_free */
1641 free_arg(left);
1642 free_arg(right);
1643 goto out_free;
1646 arg->type = PRINT_OP;
1647 arg->op.left = left;
1648 arg->op.right = right;
1650 *tok = NULL;
1651 type = process_arg(event, left, &token);
1653 again:
1654 /* Handle other operations in the arguments */
1655 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1656 type = process_op(event, left, &token);
1657 goto again;
1660 if (test_type_token(type, token, EVENT_OP, ":"))
1661 goto out_free;
1663 arg->op.op = token;
1665 type = process_arg(event, right, &token);
1667 top->op.right = arg;
1669 *tok = token;
1670 return type;
1672 out_free:
1673 /* Top may point to itself */
1674 top->op.right = NULL;
1675 free_token(token);
1676 free_arg(arg);
1677 return EVENT_ERROR;
1680 static enum event_type
1681 process_array(struct event_format *event, struct print_arg *top, char **tok)
1683 struct print_arg *arg;
1684 enum event_type type;
1685 char *token = NULL;
1687 arg = alloc_arg();
1688 if (!arg) {
1689 do_warning("%s: not enough memory!", __func__);
1690 /* '*tok' is set to top->op.op. No need to free. */
1691 *tok = NULL;
1692 return EVENT_ERROR;
1695 *tok = NULL;
1696 type = process_arg(event, arg, &token);
1697 if (test_type_token(type, token, EVENT_OP, "]"))
1698 goto out_free;
1700 top->op.right = arg;
1702 free_token(token);
1703 type = read_token_item(&token);
1704 *tok = token;
1706 return type;
1708 out_free:
1709 free_token(token);
1710 free_arg(arg);
1711 return EVENT_ERROR;
1714 static int get_op_prio(char *op)
1716 if (!op[1]) {
1717 switch (op[0]) {
1718 case '~':
1719 case '!':
1720 return 4;
1721 case '*':
1722 case '/':
1723 case '%':
1724 return 6;
1725 case '+':
1726 case '-':
1727 return 7;
1728 /* '>>' and '<<' are 8 */
1729 case '<':
1730 case '>':
1731 return 9;
1732 /* '==' and '!=' are 10 */
1733 case '&':
1734 return 11;
1735 case '^':
1736 return 12;
1737 case '|':
1738 return 13;
1739 case '?':
1740 return 16;
1741 default:
1742 do_warning("unknown op '%c'", op[0]);
1743 return -1;
1745 } else {
1746 if (strcmp(op, "++") == 0 ||
1747 strcmp(op, "--") == 0) {
1748 return 3;
1749 } else if (strcmp(op, ">>") == 0 ||
1750 strcmp(op, "<<") == 0) {
1751 return 8;
1752 } else if (strcmp(op, ">=") == 0 ||
1753 strcmp(op, "<=") == 0) {
1754 return 9;
1755 } else if (strcmp(op, "==") == 0 ||
1756 strcmp(op, "!=") == 0) {
1757 return 10;
1758 } else if (strcmp(op, "&&") == 0) {
1759 return 14;
1760 } else if (strcmp(op, "||") == 0) {
1761 return 15;
1762 } else {
1763 do_warning("unknown op '%s'", op);
1764 return -1;
1769 static int set_op_prio(struct print_arg *arg)
1772 /* single ops are the greatest */
1773 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1774 arg->op.prio = 0;
1775 else
1776 arg->op.prio = get_op_prio(arg->op.op);
1778 return arg->op.prio;
1781 /* Note, *tok does not get freed, but will most likely be saved */
1782 static enum event_type
1783 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1785 struct print_arg *left, *right = NULL;
1786 enum event_type type;
1787 char *token;
1789 /* the op is passed in via tok */
1790 token = *tok;
1792 if (arg->type == PRINT_OP && !arg->op.left) {
1793 /* handle single op */
1794 if (token[1]) {
1795 do_warning("bad op token %s", token);
1796 goto out_free;
1798 switch (token[0]) {
1799 case '~':
1800 case '!':
1801 case '+':
1802 case '-':
1803 break;
1804 default:
1805 do_warning("bad op token %s", token);
1806 goto out_free;
1810 /* make an empty left */
1811 left = alloc_arg();
1812 if (!left)
1813 goto out_warn_free;
1815 left->type = PRINT_NULL;
1816 arg->op.left = left;
1818 right = alloc_arg();
1819 if (!right)
1820 goto out_warn_free;
1822 arg->op.right = right;
1824 /* do not free the token, it belongs to an op */
1825 *tok = NULL;
1826 type = process_arg(event, right, tok);
1828 } else if (strcmp(token, "?") == 0) {
1830 left = alloc_arg();
1831 if (!left)
1832 goto out_warn_free;
1834 /* copy the top arg to the left */
1835 *left = *arg;
1837 arg->type = PRINT_OP;
1838 arg->op.op = token;
1839 arg->op.left = left;
1840 arg->op.prio = 0;
1842 /* it will set arg->op.right */
1843 type = process_cond(event, arg, tok);
1845 } else if (strcmp(token, ">>") == 0 ||
1846 strcmp(token, "<<") == 0 ||
1847 strcmp(token, "&") == 0 ||
1848 strcmp(token, "|") == 0 ||
1849 strcmp(token, "&&") == 0 ||
1850 strcmp(token, "||") == 0 ||
1851 strcmp(token, "-") == 0 ||
1852 strcmp(token, "+") == 0 ||
1853 strcmp(token, "*") == 0 ||
1854 strcmp(token, "^") == 0 ||
1855 strcmp(token, "/") == 0 ||
1856 strcmp(token, "<") == 0 ||
1857 strcmp(token, ">") == 0 ||
1858 strcmp(token, "<=") == 0 ||
1859 strcmp(token, ">=") == 0 ||
1860 strcmp(token, "==") == 0 ||
1861 strcmp(token, "!=") == 0) {
1863 left = alloc_arg();
1864 if (!left)
1865 goto out_warn_free;
1867 /* copy the top arg to the left */
1868 *left = *arg;
1870 arg->type = PRINT_OP;
1871 arg->op.op = token;
1872 arg->op.left = left;
1873 arg->op.right = NULL;
1875 if (set_op_prio(arg) == -1) {
1876 event->flags |= EVENT_FL_FAILED;
1877 /* arg->op.op (= token) will be freed at out_free */
1878 arg->op.op = NULL;
1879 goto out_free;
1882 type = read_token_item(&token);
1883 *tok = token;
1885 /* could just be a type pointer */
1886 if ((strcmp(arg->op.op, "*") == 0) &&
1887 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1888 char *new_atom;
1890 if (left->type != PRINT_ATOM) {
1891 do_warning("bad pointer type");
1892 goto out_free;
1894 new_atom = realloc(left->atom.atom,
1895 strlen(left->atom.atom) + 3);
1896 if (!new_atom)
1897 goto out_warn_free;
1899 left->atom.atom = new_atom;
1900 strcat(left->atom.atom, " *");
1901 free(arg->op.op);
1902 *arg = *left;
1903 free(left);
1905 return type;
1908 right = alloc_arg();
1909 if (!right)
1910 goto out_warn_free;
1912 type = process_arg_token(event, right, tok, type);
1913 arg->op.right = right;
1915 } else if (strcmp(token, "[") == 0) {
1917 left = alloc_arg();
1918 if (!left)
1919 goto out_warn_free;
1921 *left = *arg;
1923 arg->type = PRINT_OP;
1924 arg->op.op = token;
1925 arg->op.left = left;
1927 arg->op.prio = 0;
1929 /* it will set arg->op.right */
1930 type = process_array(event, arg, tok);
1932 } else {
1933 do_warning("unknown op '%s'", token);
1934 event->flags |= EVENT_FL_FAILED;
1935 /* the arg is now the left side */
1936 goto out_free;
1939 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1940 int prio;
1942 /* higher prios need to be closer to the root */
1943 prio = get_op_prio(*tok);
1945 if (prio > arg->op.prio)
1946 return process_op(event, arg, tok);
1948 return process_op(event, right, tok);
1951 return type;
1953 out_warn_free:
1954 do_warning("%s: not enough memory!", __func__);
1955 out_free:
1956 free_token(token);
1957 *tok = NULL;
1958 return EVENT_ERROR;
1961 static enum event_type
1962 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1963 char **tok)
1965 enum event_type type;
1966 char *field;
1967 char *token;
1969 if (read_expected(EVENT_OP, "->") < 0)
1970 goto out_err;
1972 if (read_expect_type(EVENT_ITEM, &token) < 0)
1973 goto out_free;
1974 field = token;
1976 arg->type = PRINT_FIELD;
1977 arg->field.name = field;
1979 if (is_flag_field) {
1980 arg->field.field = pevent_find_any_field(event, arg->field.name);
1981 arg->field.field->flags |= FIELD_IS_FLAG;
1982 is_flag_field = 0;
1983 } else if (is_symbolic_field) {
1984 arg->field.field = pevent_find_any_field(event, arg->field.name);
1985 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1986 is_symbolic_field = 0;
1989 type = read_token(&token);
1990 *tok = token;
1992 return type;
1994 out_free:
1995 free_token(token);
1996 out_err:
1997 *tok = NULL;
1998 return EVENT_ERROR;
2001 static char *arg_eval (struct print_arg *arg);
2003 static unsigned long long
2004 eval_type_str(unsigned long long val, const char *type, int pointer)
2006 int sign = 0;
2007 char *ref;
2008 int len;
2010 len = strlen(type);
2012 if (pointer) {
2014 if (type[len-1] != '*') {
2015 do_warning("pointer expected with non pointer type");
2016 return val;
2019 ref = malloc(len);
2020 if (!ref) {
2021 do_warning("%s: not enough memory!", __func__);
2022 return val;
2024 memcpy(ref, type, len);
2026 /* chop off the " *" */
2027 ref[len - 2] = 0;
2029 val = eval_type_str(val, ref, 0);
2030 free(ref);
2031 return val;
2034 /* check if this is a pointer */
2035 if (type[len - 1] == '*')
2036 return val;
2038 /* Try to figure out the arg size*/
2039 if (strncmp(type, "struct", 6) == 0)
2040 /* all bets off */
2041 return val;
2043 if (strcmp(type, "u8") == 0)
2044 return val & 0xff;
2046 if (strcmp(type, "u16") == 0)
2047 return val & 0xffff;
2049 if (strcmp(type, "u32") == 0)
2050 return val & 0xffffffff;
2052 if (strcmp(type, "u64") == 0 ||
2053 strcmp(type, "s64"))
2054 return val;
2056 if (strcmp(type, "s8") == 0)
2057 return (unsigned long long)(char)val & 0xff;
2059 if (strcmp(type, "s16") == 0)
2060 return (unsigned long long)(short)val & 0xffff;
2062 if (strcmp(type, "s32") == 0)
2063 return (unsigned long long)(int)val & 0xffffffff;
2065 if (strncmp(type, "unsigned ", 9) == 0) {
2066 sign = 0;
2067 type += 9;
2070 if (strcmp(type, "char") == 0) {
2071 if (sign)
2072 return (unsigned long long)(char)val & 0xff;
2073 else
2074 return val & 0xff;
2077 if (strcmp(type, "short") == 0) {
2078 if (sign)
2079 return (unsigned long long)(short)val & 0xffff;
2080 else
2081 return val & 0xffff;
2084 if (strcmp(type, "int") == 0) {
2085 if (sign)
2086 return (unsigned long long)(int)val & 0xffffffff;
2087 else
2088 return val & 0xffffffff;
2091 return val;
2095 * Try to figure out the type.
2097 static unsigned long long
2098 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2100 if (arg->type != PRINT_TYPE) {
2101 do_warning("expected type argument");
2102 return 0;
2105 return eval_type_str(val, arg->typecast.type, pointer);
2108 static int arg_num_eval(struct print_arg *arg, long long *val)
2110 long long left, right;
2111 int ret = 1;
2113 switch (arg->type) {
2114 case PRINT_ATOM:
2115 *val = strtoll(arg->atom.atom, NULL, 0);
2116 break;
2117 case PRINT_TYPE:
2118 ret = arg_num_eval(arg->typecast.item, val);
2119 if (!ret)
2120 break;
2121 *val = eval_type(*val, arg, 0);
2122 break;
2123 case PRINT_OP:
2124 switch (arg->op.op[0]) {
2125 case '|':
2126 ret = arg_num_eval(arg->op.left, &left);
2127 if (!ret)
2128 break;
2129 ret = arg_num_eval(arg->op.right, &right);
2130 if (!ret)
2131 break;
2132 if (arg->op.op[1])
2133 *val = left || right;
2134 else
2135 *val = left | right;
2136 break;
2137 case '&':
2138 ret = arg_num_eval(arg->op.left, &left);
2139 if (!ret)
2140 break;
2141 ret = arg_num_eval(arg->op.right, &right);
2142 if (!ret)
2143 break;
2144 if (arg->op.op[1])
2145 *val = left && right;
2146 else
2147 *val = left & right;
2148 break;
2149 case '<':
2150 ret = arg_num_eval(arg->op.left, &left);
2151 if (!ret)
2152 break;
2153 ret = arg_num_eval(arg->op.right, &right);
2154 if (!ret)
2155 break;
2156 switch (arg->op.op[1]) {
2157 case 0:
2158 *val = left < right;
2159 break;
2160 case '<':
2161 *val = left << right;
2162 break;
2163 case '=':
2164 *val = left <= right;
2165 break;
2166 default:
2167 do_warning("unknown op '%s'", arg->op.op);
2168 ret = 0;
2170 break;
2171 case '>':
2172 ret = arg_num_eval(arg->op.left, &left);
2173 if (!ret)
2174 break;
2175 ret = arg_num_eval(arg->op.right, &right);
2176 if (!ret)
2177 break;
2178 switch (arg->op.op[1]) {
2179 case 0:
2180 *val = left > right;
2181 break;
2182 case '>':
2183 *val = left >> right;
2184 break;
2185 case '=':
2186 *val = left >= right;
2187 break;
2188 default:
2189 do_warning("unknown op '%s'", arg->op.op);
2190 ret = 0;
2192 break;
2193 case '=':
2194 ret = arg_num_eval(arg->op.left, &left);
2195 if (!ret)
2196 break;
2197 ret = arg_num_eval(arg->op.right, &right);
2198 if (!ret)
2199 break;
2201 if (arg->op.op[1] != '=') {
2202 do_warning("unknown op '%s'", arg->op.op);
2203 ret = 0;
2204 } else
2205 *val = left == right;
2206 break;
2207 case '!':
2208 ret = arg_num_eval(arg->op.left, &left);
2209 if (!ret)
2210 break;
2211 ret = arg_num_eval(arg->op.right, &right);
2212 if (!ret)
2213 break;
2215 switch (arg->op.op[1]) {
2216 case '=':
2217 *val = left != right;
2218 break;
2219 default:
2220 do_warning("unknown op '%s'", arg->op.op);
2221 ret = 0;
2223 break;
2224 case '-':
2225 /* check for negative */
2226 if (arg->op.left->type == PRINT_NULL)
2227 left = 0;
2228 else
2229 ret = arg_num_eval(arg->op.left, &left);
2230 if (!ret)
2231 break;
2232 ret = arg_num_eval(arg->op.right, &right);
2233 if (!ret)
2234 break;
2235 *val = left - right;
2236 break;
2237 case '+':
2238 if (arg->op.left->type == PRINT_NULL)
2239 left = 0;
2240 else
2241 ret = arg_num_eval(arg->op.left, &left);
2242 if (!ret)
2243 break;
2244 ret = arg_num_eval(arg->op.right, &right);
2245 if (!ret)
2246 break;
2247 *val = left + right;
2248 break;
2249 default:
2250 do_warning("unknown op '%s'", arg->op.op);
2251 ret = 0;
2253 break;
2255 case PRINT_NULL:
2256 case PRINT_FIELD ... PRINT_SYMBOL:
2257 case PRINT_STRING:
2258 case PRINT_BSTRING:
2259 default:
2260 do_warning("invalid eval type %d", arg->type);
2261 ret = 0;
2264 return ret;
2267 static char *arg_eval (struct print_arg *arg)
2269 long long val;
2270 static char buf[20];
2272 switch (arg->type) {
2273 case PRINT_ATOM:
2274 return arg->atom.atom;
2275 case PRINT_TYPE:
2276 return arg_eval(arg->typecast.item);
2277 case PRINT_OP:
2278 if (!arg_num_eval(arg, &val))
2279 break;
2280 sprintf(buf, "%lld", val);
2281 return buf;
2283 case PRINT_NULL:
2284 case PRINT_FIELD ... PRINT_SYMBOL:
2285 case PRINT_STRING:
2286 case PRINT_BSTRING:
2287 default:
2288 do_warning("invalid eval type %d", arg->type);
2289 break;
2292 return NULL;
2295 static enum event_type
2296 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2298 enum event_type type;
2299 struct print_arg *arg = NULL;
2300 struct print_flag_sym *field;
2301 char *token = *tok;
2302 char *value;
2304 do {
2305 free_token(token);
2306 type = read_token_item(&token);
2307 if (test_type_token(type, token, EVENT_OP, "{"))
2308 break;
2310 arg = alloc_arg();
2311 if (!arg)
2312 goto out_free;
2314 free_token(token);
2315 type = process_arg(event, arg, &token);
2317 if (type == EVENT_OP)
2318 type = process_op(event, arg, &token);
2320 if (type == EVENT_ERROR)
2321 goto out_free;
2323 if (test_type_token(type, token, EVENT_DELIM, ","))
2324 goto out_free;
2326 field = calloc(1, sizeof(*field));
2327 if (!field)
2328 goto out_free;
2330 value = arg_eval(arg);
2331 if (value == NULL)
2332 goto out_free_field;
2333 field->value = strdup(value);
2334 if (field->value == NULL)
2335 goto out_free_field;
2337 free_arg(arg);
2338 arg = alloc_arg();
2339 if (!arg)
2340 goto out_free;
2342 free_token(token);
2343 type = process_arg(event, arg, &token);
2344 if (test_type_token(type, token, EVENT_OP, "}"))
2345 goto out_free_field;
2347 value = arg_eval(arg);
2348 if (value == NULL)
2349 goto out_free_field;
2350 field->str = strdup(value);
2351 if (field->str == NULL)
2352 goto out_free_field;
2353 free_arg(arg);
2354 arg = NULL;
2356 *list = field;
2357 list = &field->next;
2359 free_token(token);
2360 type = read_token_item(&token);
2361 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2363 *tok = token;
2364 return type;
2366 out_free_field:
2367 free_flag_sym(field);
2368 out_free:
2369 free_arg(arg);
2370 free_token(token);
2371 *tok = NULL;
2373 return EVENT_ERROR;
2376 static enum event_type
2377 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2379 struct print_arg *field;
2380 enum event_type type;
2381 char *token;
2383 memset(arg, 0, sizeof(*arg));
2384 arg->type = PRINT_FLAGS;
2386 field = alloc_arg();
2387 if (!field) {
2388 do_warning("%s: not enough memory!", __func__);
2389 goto out_free;
2392 type = process_field_arg(event, field, &token);
2394 /* Handle operations in the first argument */
2395 while (type == EVENT_OP)
2396 type = process_op(event, field, &token);
2398 if (test_type_token(type, token, EVENT_DELIM, ","))
2399 goto out_free_field;
2400 free_token(token);
2402 arg->flags.field = field;
2404 type = read_token_item(&token);
2405 if (event_item_type(type)) {
2406 arg->flags.delim = token;
2407 type = read_token_item(&token);
2410 if (test_type_token(type, token, EVENT_DELIM, ","))
2411 goto out_free;
2413 type = process_fields(event, &arg->flags.flags, &token);
2414 if (test_type_token(type, token, EVENT_DELIM, ")"))
2415 goto out_free;
2417 free_token(token);
2418 type = read_token_item(tok);
2419 return type;
2421 out_free_field:
2422 free_arg(field);
2423 out_free:
2424 free_token(token);
2425 *tok = NULL;
2426 return EVENT_ERROR;
2429 static enum event_type
2430 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2432 struct print_arg *field;
2433 enum event_type type;
2434 char *token;
2436 memset(arg, 0, sizeof(*arg));
2437 arg->type = PRINT_SYMBOL;
2439 field = alloc_arg();
2440 if (!field) {
2441 do_warning("%s: not enough memory!", __func__);
2442 goto out_free;
2445 type = process_field_arg(event, field, &token);
2447 if (test_type_token(type, token, EVENT_DELIM, ","))
2448 goto out_free_field;
2450 arg->symbol.field = field;
2452 type = process_fields(event, &arg->symbol.symbols, &token);
2453 if (test_type_token(type, token, EVENT_DELIM, ")"))
2454 goto out_free;
2456 free_token(token);
2457 type = read_token_item(tok);
2458 return type;
2460 out_free_field:
2461 free_arg(field);
2462 out_free:
2463 free_token(token);
2464 *tok = NULL;
2465 return EVENT_ERROR;
2468 static enum event_type
2469 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2471 struct print_arg *field;
2472 enum event_type type;
2473 char *token;
2475 memset(arg, 0, sizeof(*arg));
2476 arg->type = PRINT_HEX;
2478 field = alloc_arg();
2479 if (!field) {
2480 do_warning("%s: not enough memory!", __func__);
2481 goto out_free;
2484 type = process_arg(event, field, &token);
2486 if (test_type_token(type, token, EVENT_DELIM, ","))
2487 goto out_free;
2489 arg->hex.field = field;
2491 free_token(token);
2493 field = alloc_arg();
2494 if (!field) {
2495 do_warning("%s: not enough memory!", __func__);
2496 *tok = NULL;
2497 return EVENT_ERROR;
2500 type = process_arg(event, field, &token);
2502 if (test_type_token(type, token, EVENT_DELIM, ")"))
2503 goto out_free;
2505 arg->hex.size = field;
2507 free_token(token);
2508 type = read_token_item(tok);
2509 return type;
2511 out_free:
2512 free_arg(field);
2513 free_token(token);
2514 *tok = NULL;
2515 return EVENT_ERROR;
2518 static enum event_type
2519 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2521 struct format_field *field;
2522 enum event_type type;
2523 char *token;
2525 memset(arg, 0, sizeof(*arg));
2526 arg->type = PRINT_DYNAMIC_ARRAY;
2529 * The item within the parenthesis is another field that holds
2530 * the index into where the array starts.
2532 type = read_token(&token);
2533 *tok = token;
2534 if (type != EVENT_ITEM)
2535 goto out_free;
2537 /* Find the field */
2539 field = pevent_find_field(event, token);
2540 if (!field)
2541 goto out_free;
2543 arg->dynarray.field = field;
2544 arg->dynarray.index = 0;
2546 if (read_expected(EVENT_DELIM, ")") < 0)
2547 goto out_free;
2549 free_token(token);
2550 type = read_token_item(&token);
2551 *tok = token;
2552 if (type != EVENT_OP || strcmp(token, "[") != 0)
2553 return type;
2555 free_token(token);
2556 arg = alloc_arg();
2557 if (!arg) {
2558 do_warning("%s: not enough memory!", __func__);
2559 *tok = NULL;
2560 return EVENT_ERROR;
2563 type = process_arg(event, arg, &token);
2564 if (type == EVENT_ERROR)
2565 goto out_free_arg;
2567 if (!test_type_token(type, token, EVENT_OP, "]"))
2568 goto out_free_arg;
2570 free_token(token);
2571 type = read_token_item(tok);
2572 return type;
2574 out_free_arg:
2575 free_arg(arg);
2576 out_free:
2577 free_token(token);
2578 *tok = NULL;
2579 return EVENT_ERROR;
2582 static enum event_type
2583 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2585 struct print_arg *item_arg;
2586 enum event_type type;
2587 char *token;
2589 type = process_arg(event, arg, &token);
2591 if (type == EVENT_ERROR)
2592 goto out_free;
2594 if (type == EVENT_OP)
2595 type = process_op(event, arg, &token);
2597 if (type == EVENT_ERROR)
2598 goto out_free;
2600 if (test_type_token(type, token, EVENT_DELIM, ")"))
2601 goto out_free;
2603 free_token(token);
2604 type = read_token_item(&token);
2607 * If the next token is an item or another open paren, then
2608 * this was a typecast.
2610 if (event_item_type(type) ||
2611 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2613 /* make this a typecast and contine */
2615 /* prevous must be an atom */
2616 if (arg->type != PRINT_ATOM) {
2617 do_warning("previous needed to be PRINT_ATOM");
2618 goto out_free;
2621 item_arg = alloc_arg();
2622 if (!item_arg) {
2623 do_warning("%s: not enough memory!", __func__);
2624 goto out_free;
2627 arg->type = PRINT_TYPE;
2628 arg->typecast.type = arg->atom.atom;
2629 arg->typecast.item = item_arg;
2630 type = process_arg_token(event, item_arg, &token, type);
2634 *tok = token;
2635 return type;
2637 out_free:
2638 free_token(token);
2639 *tok = NULL;
2640 return EVENT_ERROR;
2644 static enum event_type
2645 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2646 char **tok)
2648 enum event_type type;
2649 char *token;
2651 if (read_expect_type(EVENT_ITEM, &token) < 0)
2652 goto out_free;
2654 arg->type = PRINT_STRING;
2655 arg->string.string = token;
2656 arg->string.offset = -1;
2658 if (read_expected(EVENT_DELIM, ")") < 0)
2659 goto out_err;
2661 type = read_token(&token);
2662 *tok = token;
2664 return type;
2666 out_free:
2667 free_token(token);
2668 out_err:
2669 *tok = NULL;
2670 return EVENT_ERROR;
2673 static struct pevent_function_handler *
2674 find_func_handler(struct pevent *pevent, char *func_name)
2676 struct pevent_function_handler *func;
2678 if (!pevent)
2679 return NULL;
2681 for (func = pevent->func_handlers; func; func = func->next) {
2682 if (strcmp(func->name, func_name) == 0)
2683 break;
2686 return func;
2689 static void remove_func_handler(struct pevent *pevent, char *func_name)
2691 struct pevent_function_handler *func;
2692 struct pevent_function_handler **next;
2694 next = &pevent->func_handlers;
2695 while ((func = *next)) {
2696 if (strcmp(func->name, func_name) == 0) {
2697 *next = func->next;
2698 free_func_handle(func);
2699 break;
2701 next = &func->next;
2705 static enum event_type
2706 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2707 struct print_arg *arg, char **tok)
2709 struct print_arg **next_arg;
2710 struct print_arg *farg;
2711 enum event_type type;
2712 char *token;
2713 int i;
2715 arg->type = PRINT_FUNC;
2716 arg->func.func = func;
2718 *tok = NULL;
2720 next_arg = &(arg->func.args);
2721 for (i = 0; i < func->nr_args; i++) {
2722 farg = alloc_arg();
2723 if (!farg) {
2724 do_warning("%s: not enough memory!", __func__);
2725 return EVENT_ERROR;
2728 type = process_arg(event, farg, &token);
2729 if (i < (func->nr_args - 1)) {
2730 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2731 warning("Error: function '%s()' expects %d arguments but event %s only uses %d",
2732 func->name, func->nr_args,
2733 event->name, i + 1);
2734 goto err;
2736 } else {
2737 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2738 warning("Error: function '%s()' only expects %d arguments but event %s has more",
2739 func->name, func->nr_args, event->name);
2740 goto err;
2744 *next_arg = farg;
2745 next_arg = &(farg->next);
2746 free_token(token);
2749 type = read_token(&token);
2750 *tok = token;
2752 return type;
2754 err:
2755 free_arg(farg);
2756 free_token(token);
2757 return EVENT_ERROR;
2760 static enum event_type
2761 process_function(struct event_format *event, struct print_arg *arg,
2762 char *token, char **tok)
2764 struct pevent_function_handler *func;
2766 if (strcmp(token, "__print_flags") == 0) {
2767 free_token(token);
2768 is_flag_field = 1;
2769 return process_flags(event, arg, tok);
2771 if (strcmp(token, "__print_symbolic") == 0) {
2772 free_token(token);
2773 is_symbolic_field = 1;
2774 return process_symbols(event, arg, tok);
2776 if (strcmp(token, "__print_hex") == 0) {
2777 free_token(token);
2778 return process_hex(event, arg, tok);
2780 if (strcmp(token, "__get_str") == 0) {
2781 free_token(token);
2782 return process_str(event, arg, tok);
2784 if (strcmp(token, "__get_dynamic_array") == 0) {
2785 free_token(token);
2786 return process_dynamic_array(event, arg, tok);
2789 func = find_func_handler(event->pevent, token);
2790 if (func) {
2791 free_token(token);
2792 return process_func_handler(event, func, arg, tok);
2795 do_warning("function %s not defined", token);
2796 free_token(token);
2797 return EVENT_ERROR;
2800 static enum event_type
2801 process_arg_token(struct event_format *event, struct print_arg *arg,
2802 char **tok, enum event_type type)
2804 char *token;
2805 char *atom;
2807 token = *tok;
2809 switch (type) {
2810 case EVENT_ITEM:
2811 if (strcmp(token, "REC") == 0) {
2812 free_token(token);
2813 type = process_entry(event, arg, &token);
2814 break;
2816 atom = token;
2817 /* test the next token */
2818 type = read_token_item(&token);
2821 * If the next token is a parenthesis, then this
2822 * is a function.
2824 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2825 free_token(token);
2826 token = NULL;
2827 /* this will free atom. */
2828 type = process_function(event, arg, atom, &token);
2829 break;
2831 /* atoms can be more than one token long */
2832 while (type == EVENT_ITEM) {
2833 char *new_atom;
2834 new_atom = realloc(atom,
2835 strlen(atom) + strlen(token) + 2);
2836 if (!new_atom) {
2837 free(atom);
2838 *tok = NULL;
2839 free_token(token);
2840 return EVENT_ERROR;
2842 atom = new_atom;
2843 strcat(atom, " ");
2844 strcat(atom, token);
2845 free_token(token);
2846 type = read_token_item(&token);
2849 arg->type = PRINT_ATOM;
2850 arg->atom.atom = atom;
2851 break;
2853 case EVENT_DQUOTE:
2854 case EVENT_SQUOTE:
2855 arg->type = PRINT_ATOM;
2856 arg->atom.atom = token;
2857 type = read_token_item(&token);
2858 break;
2859 case EVENT_DELIM:
2860 if (strcmp(token, "(") == 0) {
2861 free_token(token);
2862 type = process_paren(event, arg, &token);
2863 break;
2865 case EVENT_OP:
2866 /* handle single ops */
2867 arg->type = PRINT_OP;
2868 arg->op.op = token;
2869 arg->op.left = NULL;
2870 type = process_op(event, arg, &token);
2872 /* On error, the op is freed */
2873 if (type == EVENT_ERROR)
2874 arg->op.op = NULL;
2876 /* return error type if errored */
2877 break;
2879 case EVENT_ERROR ... EVENT_NEWLINE:
2880 default:
2881 do_warning("unexpected type %d", type);
2882 return EVENT_ERROR;
2884 *tok = token;
2886 return type;
2889 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2891 enum event_type type = EVENT_ERROR;
2892 struct print_arg *arg;
2893 char *token;
2894 int args = 0;
2896 do {
2897 if (type == EVENT_NEWLINE) {
2898 type = read_token_item(&token);
2899 continue;
2902 arg = alloc_arg();
2903 if (!arg) {
2904 do_warning("%s: not enough memory!", __func__);
2905 return -1;
2908 type = process_arg(event, arg, &token);
2910 if (type == EVENT_ERROR) {
2911 free_token(token);
2912 free_arg(arg);
2913 return -1;
2916 *list = arg;
2917 args++;
2919 if (type == EVENT_OP) {
2920 type = process_op(event, arg, &token);
2921 free_token(token);
2922 if (type == EVENT_ERROR) {
2923 *list = NULL;
2924 free_arg(arg);
2925 return -1;
2927 list = &arg->next;
2928 continue;
2931 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2932 free_token(token);
2933 *list = arg;
2934 list = &arg->next;
2935 continue;
2937 break;
2938 } while (type != EVENT_NONE);
2940 if (type != EVENT_NONE && type != EVENT_ERROR)
2941 free_token(token);
2943 return args;
2946 static int event_read_print(struct event_format *event)
2948 enum event_type type;
2949 char *token;
2950 int ret;
2952 if (read_expected_item(EVENT_ITEM, "print") < 0)
2953 return -1;
2955 if (read_expected(EVENT_ITEM, "fmt") < 0)
2956 return -1;
2958 if (read_expected(EVENT_OP, ":") < 0)
2959 return -1;
2961 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2962 goto fail;
2964 concat:
2965 event->print_fmt.format = token;
2966 event->print_fmt.args = NULL;
2968 /* ok to have no arg */
2969 type = read_token_item(&token);
2971 if (type == EVENT_NONE)
2972 return 0;
2974 /* Handle concatenation of print lines */
2975 if (type == EVENT_DQUOTE) {
2976 char *cat;
2978 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2979 goto fail;
2980 free_token(token);
2981 free_token(event->print_fmt.format);
2982 event->print_fmt.format = NULL;
2983 token = cat;
2984 goto concat;
2987 if (test_type_token(type, token, EVENT_DELIM, ","))
2988 goto fail;
2990 free_token(token);
2992 ret = event_read_print_args(event, &event->print_fmt.args);
2993 if (ret < 0)
2994 return -1;
2996 return ret;
2998 fail:
2999 free_token(token);
3000 return -1;
3004 * pevent_find_common_field - return a common field by event
3005 * @event: handle for the event
3006 * @name: the name of the common field to return
3008 * Returns a common field from the event by the given @name.
3009 * This only searchs the common fields and not all field.
3011 struct format_field *
3012 pevent_find_common_field(struct event_format *event, const char *name)
3014 struct format_field *format;
3016 for (format = event->format.common_fields;
3017 format; format = format->next) {
3018 if (strcmp(format->name, name) == 0)
3019 break;
3022 return format;
3026 * pevent_find_field - find a non-common field
3027 * @event: handle for the event
3028 * @name: the name of the non-common field
3030 * Returns a non-common field by the given @name.
3031 * This does not search common fields.
3033 struct format_field *
3034 pevent_find_field(struct event_format *event, const char *name)
3036 struct format_field *format;
3038 for (format = event->format.fields;
3039 format; format = format->next) {
3040 if (strcmp(format->name, name) == 0)
3041 break;
3044 return format;
3048 * pevent_find_any_field - find any field by name
3049 * @event: handle for the event
3050 * @name: the name of the field
3052 * Returns a field by the given @name.
3053 * This searchs the common field names first, then
3054 * the non-common ones if a common one was not found.
3056 struct format_field *
3057 pevent_find_any_field(struct event_format *event, const char *name)
3059 struct format_field *format;
3061 format = pevent_find_common_field(event, name);
3062 if (format)
3063 return format;
3064 return pevent_find_field(event, name);
3068 * pevent_read_number - read a number from data
3069 * @pevent: handle for the pevent
3070 * @ptr: the raw data
3071 * @size: the size of the data that holds the number
3073 * Returns the number (converted to host) from the
3074 * raw data.
3076 unsigned long long pevent_read_number(struct pevent *pevent,
3077 const void *ptr, int size)
3079 switch (size) {
3080 case 1:
3081 return *(unsigned char *)ptr;
3082 case 2:
3083 return data2host2(pevent, ptr);
3084 case 4:
3085 return data2host4(pevent, ptr);
3086 case 8:
3087 return data2host8(pevent, ptr);
3088 default:
3089 /* BUG! */
3090 return 0;
3095 * pevent_read_number_field - read a number from data
3096 * @field: a handle to the field
3097 * @data: the raw data to read
3098 * @value: the value to place the number in
3100 * Reads raw data according to a field offset and size,
3101 * and translates it into @value.
3103 * Returns 0 on success, -1 otherwise.
3105 int pevent_read_number_field(struct format_field *field, const void *data,
3106 unsigned long long *value)
3108 if (!field)
3109 return -1;
3110 switch (field->size) {
3111 case 1:
3112 case 2:
3113 case 4:
3114 case 8:
3115 *value = pevent_read_number(field->event->pevent,
3116 data + field->offset, field->size);
3117 return 0;
3118 default:
3119 return -1;
3123 static int get_common_info(struct pevent *pevent,
3124 const char *type, int *offset, int *size)
3126 struct event_format *event;
3127 struct format_field *field;
3130 * All events should have the same common elements.
3131 * Pick any event to find where the type is;
3133 if (!pevent->events) {
3134 do_warning("no event_list!");
3135 return -1;
3138 event = pevent->events[0];
3139 field = pevent_find_common_field(event, type);
3140 if (!field)
3141 return -1;
3143 *offset = field->offset;
3144 *size = field->size;
3146 return 0;
3149 static int __parse_common(struct pevent *pevent, void *data,
3150 int *size, int *offset, const char *name)
3152 int ret;
3154 if (!*size) {
3155 ret = get_common_info(pevent, name, offset, size);
3156 if (ret < 0)
3157 return ret;
3159 return pevent_read_number(pevent, data + *offset, *size);
3162 static int trace_parse_common_type(struct pevent *pevent, void *data)
3164 return __parse_common(pevent, data,
3165 &pevent->type_size, &pevent->type_offset,
3166 "common_type");
3169 static int parse_common_pid(struct pevent *pevent, void *data)
3171 return __parse_common(pevent, data,
3172 &pevent->pid_size, &pevent->pid_offset,
3173 "common_pid");
3176 static int parse_common_pc(struct pevent *pevent, void *data)
3178 return __parse_common(pevent, data,
3179 &pevent->pc_size, &pevent->pc_offset,
3180 "common_preempt_count");
3183 static int parse_common_flags(struct pevent *pevent, void *data)
3185 return __parse_common(pevent, data,
3186 &pevent->flags_size, &pevent->flags_offset,
3187 "common_flags");
3190 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3192 return __parse_common(pevent, data,
3193 &pevent->ld_size, &pevent->ld_offset,
3194 "common_lock_depth");
3197 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3199 return __parse_common(pevent, data,
3200 &pevent->ld_size, &pevent->ld_offset,
3201 "common_migrate_disable");
3204 static int events_id_cmp(const void *a, const void *b);
3207 * pevent_find_event - find an event by given id
3208 * @pevent: a handle to the pevent
3209 * @id: the id of the event
3211 * Returns an event that has a given @id.
3213 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3215 struct event_format **eventptr;
3216 struct event_format key;
3217 struct event_format *pkey = &key;
3219 /* Check cache first */
3220 if (pevent->last_event && pevent->last_event->id == id)
3221 return pevent->last_event;
3223 key.id = id;
3225 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3226 sizeof(*pevent->events), events_id_cmp);
3228 if (eventptr) {
3229 pevent->last_event = *eventptr;
3230 return *eventptr;
3233 return NULL;
3237 * pevent_find_event_by_name - find an event by given name
3238 * @pevent: a handle to the pevent
3239 * @sys: the system name to search for
3240 * @name: the name of the event to search for
3242 * This returns an event with a given @name and under the system
3243 * @sys. If @sys is NULL the first event with @name is returned.
3245 struct event_format *
3246 pevent_find_event_by_name(struct pevent *pevent,
3247 const char *sys, const char *name)
3249 struct event_format *event;
3250 int i;
3252 if (pevent->last_event &&
3253 strcmp(pevent->last_event->name, name) == 0 &&
3254 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3255 return pevent->last_event;
3257 for (i = 0; i < pevent->nr_events; i++) {
3258 event = pevent->events[i];
3259 if (strcmp(event->name, name) == 0) {
3260 if (!sys)
3261 break;
3262 if (strcmp(event->system, sys) == 0)
3263 break;
3266 if (i == pevent->nr_events)
3267 event = NULL;
3269 pevent->last_event = event;
3270 return event;
3273 static unsigned long long
3274 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3276 struct pevent *pevent = event->pevent;
3277 unsigned long long val = 0;
3278 unsigned long long left, right;
3279 struct print_arg *typearg = NULL;
3280 struct print_arg *larg;
3281 unsigned long offset;
3282 unsigned int field_size;
3284 switch (arg->type) {
3285 case PRINT_NULL:
3286 /* ?? */
3287 return 0;
3288 case PRINT_ATOM:
3289 return strtoull(arg->atom.atom, NULL, 0);
3290 case PRINT_FIELD:
3291 if (!arg->field.field) {
3292 arg->field.field = pevent_find_any_field(event, arg->field.name);
3293 if (!arg->field.field)
3294 goto out_warning_field;
3297 /* must be a number */
3298 val = pevent_read_number(pevent, data + arg->field.field->offset,
3299 arg->field.field->size);
3300 break;
3301 case PRINT_FLAGS:
3302 case PRINT_SYMBOL:
3303 case PRINT_HEX:
3304 break;
3305 case PRINT_TYPE:
3306 val = eval_num_arg(data, size, event, arg->typecast.item);
3307 return eval_type(val, arg, 0);
3308 case PRINT_STRING:
3309 case PRINT_BSTRING:
3310 return 0;
3311 case PRINT_FUNC: {
3312 struct trace_seq s;
3313 trace_seq_init(&s);
3314 val = process_defined_func(&s, data, size, event, arg);
3315 trace_seq_destroy(&s);
3316 return val;
3318 case PRINT_OP:
3319 if (strcmp(arg->op.op, "[") == 0) {
3321 * Arrays are special, since we don't want
3322 * to read the arg as is.
3324 right = eval_num_arg(data, size, event, arg->op.right);
3326 /* handle typecasts */
3327 larg = arg->op.left;
3328 while (larg->type == PRINT_TYPE) {
3329 if (!typearg)
3330 typearg = larg;
3331 larg = larg->typecast.item;
3334 /* Default to long size */
3335 field_size = pevent->long_size;
3337 switch (larg->type) {
3338 case PRINT_DYNAMIC_ARRAY:
3339 offset = pevent_read_number(pevent,
3340 data + larg->dynarray.field->offset,
3341 larg->dynarray.field->size);
3342 if (larg->dynarray.field->elementsize)
3343 field_size = larg->dynarray.field->elementsize;
3345 * The actual length of the dynamic array is stored
3346 * in the top half of the field, and the offset
3347 * is in the bottom half of the 32 bit field.
3349 offset &= 0xffff;
3350 offset += right;
3351 break;
3352 case PRINT_FIELD:
3353 if (!larg->field.field) {
3354 larg->field.field =
3355 pevent_find_any_field(event, larg->field.name);
3356 if (!larg->field.field) {
3357 arg = larg;
3358 goto out_warning_field;
3361 field_size = larg->field.field->elementsize;
3362 offset = larg->field.field->offset +
3363 right * larg->field.field->elementsize;
3364 break;
3365 default:
3366 goto default_op; /* oops, all bets off */
3368 val = pevent_read_number(pevent,
3369 data + offset, field_size);
3370 if (typearg)
3371 val = eval_type(val, typearg, 1);
3372 break;
3373 } else if (strcmp(arg->op.op, "?") == 0) {
3374 left = eval_num_arg(data, size, event, arg->op.left);
3375 arg = arg->op.right;
3376 if (left)
3377 val = eval_num_arg(data, size, event, arg->op.left);
3378 else
3379 val = eval_num_arg(data, size, event, arg->op.right);
3380 break;
3382 default_op:
3383 left = eval_num_arg(data, size, event, arg->op.left);
3384 right = eval_num_arg(data, size, event, arg->op.right);
3385 switch (arg->op.op[0]) {
3386 case '!':
3387 switch (arg->op.op[1]) {
3388 case 0:
3389 val = !right;
3390 break;
3391 case '=':
3392 val = left != right;
3393 break;
3394 default:
3395 goto out_warning_op;
3397 break;
3398 case '~':
3399 val = ~right;
3400 break;
3401 case '|':
3402 if (arg->op.op[1])
3403 val = left || right;
3404 else
3405 val = left | right;
3406 break;
3407 case '&':
3408 if (arg->op.op[1])
3409 val = left && right;
3410 else
3411 val = left & right;
3412 break;
3413 case '<':
3414 switch (arg->op.op[1]) {
3415 case 0:
3416 val = left < right;
3417 break;
3418 case '<':
3419 val = left << right;
3420 break;
3421 case '=':
3422 val = left <= right;
3423 break;
3424 default:
3425 goto out_warning_op;
3427 break;
3428 case '>':
3429 switch (arg->op.op[1]) {
3430 case 0:
3431 val = left > right;
3432 break;
3433 case '>':
3434 val = left >> right;
3435 break;
3436 case '=':
3437 val = left >= right;
3438 break;
3439 default:
3440 goto out_warning_op;
3442 break;
3443 case '=':
3444 if (arg->op.op[1] != '=')
3445 goto out_warning_op;
3447 val = left == right;
3448 break;
3449 case '-':
3450 val = left - right;
3451 break;
3452 case '+':
3453 val = left + right;
3454 break;
3455 case '/':
3456 val = left / right;
3457 break;
3458 case '*':
3459 val = left * right;
3460 break;
3461 default:
3462 goto out_warning_op;
3464 break;
3465 case PRINT_DYNAMIC_ARRAY:
3466 /* Without [], we pass the address to the dynamic data */
3467 offset = pevent_read_number(pevent,
3468 data + arg->dynarray.field->offset,
3469 arg->dynarray.field->size);
3471 * The actual length of the dynamic array is stored
3472 * in the top half of the field, and the offset
3473 * is in the bottom half of the 32 bit field.
3475 offset &= 0xffff;
3476 val = (unsigned long long)((unsigned long)data + offset);
3477 break;
3478 default: /* not sure what to do there */
3479 return 0;
3481 return val;
3483 out_warning_op:
3484 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3485 return 0;
3487 out_warning_field:
3488 do_warning("%s: field %s not found", __func__, arg->field.name);
3489 return 0;
3492 struct flag {
3493 const char *name;
3494 unsigned long long value;
3497 static const struct flag flags[] = {
3498 { "HI_SOFTIRQ", 0 },
3499 { "TIMER_SOFTIRQ", 1 },
3500 { "NET_TX_SOFTIRQ", 2 },
3501 { "NET_RX_SOFTIRQ", 3 },
3502 { "BLOCK_SOFTIRQ", 4 },
3503 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3504 { "TASKLET_SOFTIRQ", 6 },
3505 { "SCHED_SOFTIRQ", 7 },
3506 { "HRTIMER_SOFTIRQ", 8 },
3507 { "RCU_SOFTIRQ", 9 },
3509 { "HRTIMER_NORESTART", 0 },
3510 { "HRTIMER_RESTART", 1 },
3513 static unsigned long long eval_flag(const char *flag)
3515 int i;
3518 * Some flags in the format files do not get converted.
3519 * If the flag is not numeric, see if it is something that
3520 * we already know about.
3522 if (isdigit(flag[0]))
3523 return strtoull(flag, NULL, 0);
3525 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3526 if (strcmp(flags[i].name, flag) == 0)
3527 return flags[i].value;
3529 return 0;
3532 static void print_str_to_seq(struct trace_seq *s, const char *format,
3533 int len_arg, const char *str)
3535 if (len_arg >= 0)
3536 trace_seq_printf(s, format, len_arg, str);
3537 else
3538 trace_seq_printf(s, format, str);
3541 static void print_str_arg(struct trace_seq *s, void *data, int size,
3542 struct event_format *event, const char *format,
3543 int len_arg, struct print_arg *arg)
3545 struct pevent *pevent = event->pevent;
3546 struct print_flag_sym *flag;
3547 struct format_field *field;
3548 struct printk_map *printk;
3549 unsigned long long val, fval;
3550 unsigned long addr;
3551 char *str;
3552 unsigned char *hex;
3553 int print;
3554 int i, len;
3556 switch (arg->type) {
3557 case PRINT_NULL:
3558 /* ?? */
3559 return;
3560 case PRINT_ATOM:
3561 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3562 return;
3563 case PRINT_FIELD:
3564 field = arg->field.field;
3565 if (!field) {
3566 field = pevent_find_any_field(event, arg->field.name);
3567 if (!field) {
3568 str = arg->field.name;
3569 goto out_warning_field;
3571 arg->field.field = field;
3573 /* Zero sized fields, mean the rest of the data */
3574 len = field->size ? : size - field->offset;
3577 * Some events pass in pointers. If this is not an array
3578 * and the size is the same as long_size, assume that it
3579 * is a pointer.
3581 if (!(field->flags & FIELD_IS_ARRAY) &&
3582 field->size == pevent->long_size) {
3583 addr = *(unsigned long *)(data + field->offset);
3584 /* Check if it matches a print format */
3585 printk = find_printk(pevent, addr);
3586 if (printk)
3587 trace_seq_puts(s, printk->printk);
3588 else
3589 trace_seq_printf(s, "%lx", addr);
3590 break;
3592 str = malloc(len + 1);
3593 if (!str) {
3594 do_warning("%s: not enough memory!", __func__);
3595 return;
3597 memcpy(str, data + field->offset, len);
3598 str[len] = 0;
3599 print_str_to_seq(s, format, len_arg, str);
3600 free(str);
3601 break;
3602 case PRINT_FLAGS:
3603 val = eval_num_arg(data, size, event, arg->flags.field);
3604 print = 0;
3605 for (flag = arg->flags.flags; flag; flag = flag->next) {
3606 fval = eval_flag(flag->value);
3607 if (!val && !fval) {
3608 print_str_to_seq(s, format, len_arg, flag->str);
3609 break;
3611 if (fval && (val & fval) == fval) {
3612 if (print && arg->flags.delim)
3613 trace_seq_puts(s, arg->flags.delim);
3614 print_str_to_seq(s, format, len_arg, flag->str);
3615 print = 1;
3616 val &= ~fval;
3619 break;
3620 case PRINT_SYMBOL:
3621 val = eval_num_arg(data, size, event, arg->symbol.field);
3622 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3623 fval = eval_flag(flag->value);
3624 if (val == fval) {
3625 print_str_to_seq(s, format, len_arg, flag->str);
3626 break;
3629 break;
3630 case PRINT_HEX:
3631 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3632 unsigned long offset;
3633 offset = pevent_read_number(pevent,
3634 data + arg->hex.field->dynarray.field->offset,
3635 arg->hex.field->dynarray.field->size);
3636 hex = data + (offset & 0xffff);
3637 } else {
3638 field = arg->hex.field->field.field;
3639 if (!field) {
3640 str = arg->hex.field->field.name;
3641 field = pevent_find_any_field(event, str);
3642 if (!field)
3643 goto out_warning_field;
3644 arg->hex.field->field.field = field;
3646 hex = data + field->offset;
3648 len = eval_num_arg(data, size, event, arg->hex.size);
3649 for (i = 0; i < len; i++) {
3650 if (i)
3651 trace_seq_putc(s, ' ');
3652 trace_seq_printf(s, "%02x", hex[i]);
3654 break;
3656 case PRINT_TYPE:
3657 break;
3658 case PRINT_STRING: {
3659 int str_offset;
3661 if (arg->string.offset == -1) {
3662 struct format_field *f;
3664 f = pevent_find_any_field(event, arg->string.string);
3665 arg->string.offset = f->offset;
3667 str_offset = data2host4(pevent, data + arg->string.offset);
3668 str_offset &= 0xffff;
3669 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3670 break;
3672 case PRINT_BSTRING:
3673 print_str_to_seq(s, format, len_arg, arg->string.string);
3674 break;
3675 case PRINT_OP:
3677 * The only op for string should be ? :
3679 if (arg->op.op[0] != '?')
3680 return;
3681 val = eval_num_arg(data, size, event, arg->op.left);
3682 if (val)
3683 print_str_arg(s, data, size, event,
3684 format, len_arg, arg->op.right->op.left);
3685 else
3686 print_str_arg(s, data, size, event,
3687 format, len_arg, arg->op.right->op.right);
3688 break;
3689 case PRINT_FUNC:
3690 process_defined_func(s, data, size, event, arg);
3691 break;
3692 default:
3693 /* well... */
3694 break;
3697 return;
3699 out_warning_field:
3700 do_warning("%s: field %s not found", __func__, arg->field.name);
3703 static unsigned long long
3704 process_defined_func(struct trace_seq *s, void *data, int size,
3705 struct event_format *event, struct print_arg *arg)
3707 struct pevent_function_handler *func_handle = arg->func.func;
3708 struct pevent_func_params *param;
3709 unsigned long long *args;
3710 unsigned long long ret;
3711 struct print_arg *farg;
3712 struct trace_seq str;
3713 struct save_str {
3714 struct save_str *next;
3715 char *str;
3716 } *strings = NULL, *string;
3717 int i;
3719 if (!func_handle->nr_args) {
3720 ret = (*func_handle->func)(s, NULL);
3721 goto out;
3724 farg = arg->func.args;
3725 param = func_handle->params;
3727 ret = ULLONG_MAX;
3728 args = malloc(sizeof(*args) * func_handle->nr_args);
3729 if (!args)
3730 goto out;
3732 for (i = 0; i < func_handle->nr_args; i++) {
3733 switch (param->type) {
3734 case PEVENT_FUNC_ARG_INT:
3735 case PEVENT_FUNC_ARG_LONG:
3736 case PEVENT_FUNC_ARG_PTR:
3737 args[i] = eval_num_arg(data, size, event, farg);
3738 break;
3739 case PEVENT_FUNC_ARG_STRING:
3740 trace_seq_init(&str);
3741 print_str_arg(&str, data, size, event, "%s", -1, farg);
3742 trace_seq_terminate(&str);
3743 string = malloc(sizeof(*string));
3744 if (!string) {
3745 do_warning("%s(%d): malloc str", __func__, __LINE__);
3746 goto out_free;
3748 string->next = strings;
3749 string->str = strdup(str.buffer);
3750 if (!string->str) {
3751 free(string);
3752 do_warning("%s(%d): malloc str", __func__, __LINE__);
3753 goto out_free;
3755 args[i] = (uintptr_t)string->str;
3756 strings = string;
3757 trace_seq_destroy(&str);
3758 break;
3759 default:
3761 * Something went totally wrong, this is not
3762 * an input error, something in this code broke.
3764 do_warning("Unexpected end of arguments\n");
3765 goto out_free;
3767 farg = farg->next;
3768 param = param->next;
3771 ret = (*func_handle->func)(s, args);
3772 out_free:
3773 free(args);
3774 while (strings) {
3775 string = strings;
3776 strings = string->next;
3777 free(string->str);
3778 free(string);
3781 out:
3782 /* TBD : handle return type here */
3783 return ret;
3786 static void free_args(struct print_arg *args)
3788 struct print_arg *next;
3790 while (args) {
3791 next = args->next;
3793 free_arg(args);
3794 args = next;
3798 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3800 struct pevent *pevent = event->pevent;
3801 struct format_field *field, *ip_field;
3802 struct print_arg *args, *arg, **next;
3803 unsigned long long ip, val;
3804 char *ptr;
3805 void *bptr;
3806 int vsize;
3808 field = pevent->bprint_buf_field;
3809 ip_field = pevent->bprint_ip_field;
3811 if (!field) {
3812 field = pevent_find_field(event, "buf");
3813 if (!field) {
3814 do_warning("can't find buffer field for binary printk");
3815 return NULL;
3817 ip_field = pevent_find_field(event, "ip");
3818 if (!ip_field) {
3819 do_warning("can't find ip field for binary printk");
3820 return NULL;
3822 pevent->bprint_buf_field = field;
3823 pevent->bprint_ip_field = ip_field;
3826 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3829 * The first arg is the IP pointer.
3831 args = alloc_arg();
3832 if (!args) {
3833 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3834 return NULL;
3836 arg = args;
3837 arg->next = NULL;
3838 next = &arg->next;
3840 arg->type = PRINT_ATOM;
3842 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3843 goto out_free;
3845 /* skip the first "%pf: " */
3846 for (ptr = fmt + 5, bptr = data + field->offset;
3847 bptr < data + size && *ptr; ptr++) {
3848 int ls = 0;
3850 if (*ptr == '%') {
3851 process_again:
3852 ptr++;
3853 switch (*ptr) {
3854 case '%':
3855 break;
3856 case 'l':
3857 ls++;
3858 goto process_again;
3859 case 'L':
3860 ls = 2;
3861 goto process_again;
3862 case '0' ... '9':
3863 goto process_again;
3864 case '.':
3865 goto process_again;
3866 case 'p':
3867 ls = 1;
3868 /* fall through */
3869 case 'd':
3870 case 'u':
3871 case 'x':
3872 case 'i':
3873 switch (ls) {
3874 case 0:
3875 vsize = 4;
3876 break;
3877 case 1:
3878 vsize = pevent->long_size;
3879 break;
3880 case 2:
3881 vsize = 8;
3882 break;
3883 default:
3884 vsize = ls; /* ? */
3885 break;
3887 /* fall through */
3888 case '*':
3889 if (*ptr == '*')
3890 vsize = 4;
3892 /* the pointers are always 4 bytes aligned */
3893 bptr = (void *)(((unsigned long)bptr + 3) &
3894 ~3);
3895 val = pevent_read_number(pevent, bptr, vsize);
3896 bptr += vsize;
3897 arg = alloc_arg();
3898 if (!arg) {
3899 do_warning("%s(%d): not enough memory!",
3900 __func__, __LINE__);
3901 goto out_free;
3903 arg->next = NULL;
3904 arg->type = PRINT_ATOM;
3905 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3906 free(arg);
3907 goto out_free;
3909 *next = arg;
3910 next = &arg->next;
3912 * The '*' case means that an arg is used as the length.
3913 * We need to continue to figure out for what.
3915 if (*ptr == '*')
3916 goto process_again;
3918 break;
3919 case 's':
3920 arg = alloc_arg();
3921 if (!arg) {
3922 do_warning("%s(%d): not enough memory!",
3923 __func__, __LINE__);
3924 goto out_free;
3926 arg->next = NULL;
3927 arg->type = PRINT_BSTRING;
3928 arg->string.string = strdup(bptr);
3929 if (!arg->string.string)
3930 goto out_free;
3931 bptr += strlen(bptr) + 1;
3932 *next = arg;
3933 next = &arg->next;
3934 default:
3935 break;
3940 return args;
3942 out_free:
3943 free_args(args);
3944 return NULL;
3947 static char *
3948 get_bprint_format(void *data, int size __maybe_unused,
3949 struct event_format *event)
3951 struct pevent *pevent = event->pevent;
3952 unsigned long long addr;
3953 struct format_field *field;
3954 struct printk_map *printk;
3955 char *format;
3957 field = pevent->bprint_fmt_field;
3959 if (!field) {
3960 field = pevent_find_field(event, "fmt");
3961 if (!field) {
3962 do_warning("can't find format field for binary printk");
3963 return NULL;
3965 pevent->bprint_fmt_field = field;
3968 addr = pevent_read_number(pevent, data + field->offset, field->size);
3970 printk = find_printk(pevent, addr);
3971 if (!printk) {
3972 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
3973 return NULL;
3974 return format;
3977 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
3978 return NULL;
3980 return format;
3983 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3984 struct event_format *event, struct print_arg *arg)
3986 unsigned char *buf;
3987 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3989 if (arg->type == PRINT_FUNC) {
3990 process_defined_func(s, data, size, event, arg);
3991 return;
3994 if (arg->type != PRINT_FIELD) {
3995 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3996 arg->type);
3997 return;
4000 if (mac == 'm')
4001 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4002 if (!arg->field.field) {
4003 arg->field.field =
4004 pevent_find_any_field(event, arg->field.name);
4005 if (!arg->field.field) {
4006 do_warning("%s: field %s not found",
4007 __func__, arg->field.name);
4008 return;
4011 if (arg->field.field->size != 6) {
4012 trace_seq_printf(s, "INVALIDMAC");
4013 return;
4015 buf = data + arg->field.field->offset;
4016 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4019 static int is_printable_array(char *p, unsigned int len)
4021 unsigned int i;
4023 for (i = 0; i < len && p[i]; i++)
4024 if (!isprint(p[i]) && !isspace(p[i]))
4025 return 0;
4026 return 1;
4029 static void print_event_fields(struct trace_seq *s, void *data,
4030 int size __maybe_unused,
4031 struct event_format *event)
4033 struct format_field *field;
4034 unsigned long long val;
4035 unsigned int offset, len, i;
4037 field = event->format.fields;
4038 while (field) {
4039 trace_seq_printf(s, " %s=", field->name);
4040 if (field->flags & FIELD_IS_ARRAY) {
4041 offset = field->offset;
4042 len = field->size;
4043 if (field->flags & FIELD_IS_DYNAMIC) {
4044 val = pevent_read_number(event->pevent, data + offset, len);
4045 offset = val;
4046 len = offset >> 16;
4047 offset &= 0xffff;
4049 if (field->flags & FIELD_IS_STRING &&
4050 is_printable_array(data + offset, len)) {
4051 trace_seq_printf(s, "%s", (char *)data + offset);
4052 } else {
4053 trace_seq_puts(s, "ARRAY[");
4054 for (i = 0; i < len; i++) {
4055 if (i)
4056 trace_seq_puts(s, ", ");
4057 trace_seq_printf(s, "%02x",
4058 *((unsigned char *)data + offset + i));
4060 trace_seq_putc(s, ']');
4061 field->flags &= ~FIELD_IS_STRING;
4063 } else {
4064 val = pevent_read_number(event->pevent, data + field->offset,
4065 field->size);
4066 if (field->flags & FIELD_IS_POINTER) {
4067 trace_seq_printf(s, "0x%llx", val);
4068 } else if (field->flags & FIELD_IS_SIGNED) {
4069 switch (field->size) {
4070 case 4:
4072 * If field is long then print it in hex.
4073 * A long usually stores pointers.
4075 if (field->flags & FIELD_IS_LONG)
4076 trace_seq_printf(s, "0x%x", (int)val);
4077 else
4078 trace_seq_printf(s, "%d", (int)val);
4079 break;
4080 case 2:
4081 trace_seq_printf(s, "%2d", (short)val);
4082 break;
4083 case 1:
4084 trace_seq_printf(s, "%1d", (char)val);
4085 break;
4086 default:
4087 trace_seq_printf(s, "%lld", val);
4089 } else {
4090 if (field->flags & FIELD_IS_LONG)
4091 trace_seq_printf(s, "0x%llx", val);
4092 else
4093 trace_seq_printf(s, "%llu", val);
4096 field = field->next;
4100 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4102 struct pevent *pevent = event->pevent;
4103 struct print_fmt *print_fmt = &event->print_fmt;
4104 struct print_arg *arg = print_fmt->args;
4105 struct print_arg *args = NULL;
4106 const char *ptr = print_fmt->format;
4107 unsigned long long val;
4108 struct func_map *func;
4109 const char *saveptr;
4110 struct trace_seq p;
4111 char *bprint_fmt = NULL;
4112 char format[32];
4113 int show_func;
4114 int len_as_arg;
4115 int len_arg;
4116 int len;
4117 int ls;
4119 if (event->flags & EVENT_FL_FAILED) {
4120 trace_seq_printf(s, "[FAILED TO PARSE]");
4121 print_event_fields(s, data, size, event);
4122 return;
4125 if (event->flags & EVENT_FL_ISBPRINT) {
4126 bprint_fmt = get_bprint_format(data, size, event);
4127 args = make_bprint_args(bprint_fmt, data, size, event);
4128 arg = args;
4129 ptr = bprint_fmt;
4132 for (; *ptr; ptr++) {
4133 ls = 0;
4134 if (*ptr == '\\') {
4135 ptr++;
4136 switch (*ptr) {
4137 case 'n':
4138 trace_seq_putc(s, '\n');
4139 break;
4140 case 't':
4141 trace_seq_putc(s, '\t');
4142 break;
4143 case 'r':
4144 trace_seq_putc(s, '\r');
4145 break;
4146 case '\\':
4147 trace_seq_putc(s, '\\');
4148 break;
4149 default:
4150 trace_seq_putc(s, *ptr);
4151 break;
4154 } else if (*ptr == '%') {
4155 saveptr = ptr;
4156 show_func = 0;
4157 len_as_arg = 0;
4158 cont_process:
4159 ptr++;
4160 switch (*ptr) {
4161 case '%':
4162 trace_seq_putc(s, '%');
4163 break;
4164 case '#':
4165 /* FIXME: need to handle properly */
4166 goto cont_process;
4167 case 'h':
4168 ls--;
4169 goto cont_process;
4170 case 'l':
4171 ls++;
4172 goto cont_process;
4173 case 'L':
4174 ls = 2;
4175 goto cont_process;
4176 case '*':
4177 /* The argument is the length. */
4178 if (!arg) {
4179 do_warning("no argument match");
4180 event->flags |= EVENT_FL_FAILED;
4181 goto out_failed;
4183 len_arg = eval_num_arg(data, size, event, arg);
4184 len_as_arg = 1;
4185 arg = arg->next;
4186 goto cont_process;
4187 case '.':
4188 case 'z':
4189 case 'Z':
4190 case '0' ... '9':
4191 goto cont_process;
4192 case 'p':
4193 if (pevent->long_size == 4)
4194 ls = 1;
4195 else
4196 ls = 2;
4198 if (*(ptr+1) == 'F' ||
4199 *(ptr+1) == 'f') {
4200 ptr++;
4201 show_func = *ptr;
4202 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4203 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4204 ptr++;
4205 arg = arg->next;
4206 break;
4209 /* fall through */
4210 case 'd':
4211 case 'i':
4212 case 'x':
4213 case 'X':
4214 case 'u':
4215 if (!arg) {
4216 do_warning("no argument match");
4217 event->flags |= EVENT_FL_FAILED;
4218 goto out_failed;
4221 len = ((unsigned long)ptr + 1) -
4222 (unsigned long)saveptr;
4224 /* should never happen */
4225 if (len > 31) {
4226 do_warning("bad format!");
4227 event->flags |= EVENT_FL_FAILED;
4228 len = 31;
4231 memcpy(format, saveptr, len);
4232 format[len] = 0;
4234 val = eval_num_arg(data, size, event, arg);
4235 arg = arg->next;
4237 if (show_func) {
4238 func = find_func(pevent, val);
4239 if (func) {
4240 trace_seq_puts(s, func->func);
4241 if (show_func == 'F')
4242 trace_seq_printf(s,
4243 "+0x%llx",
4244 val - func->addr);
4245 break;
4248 if (pevent->long_size == 8 && ls &&
4249 sizeof(long) != 8) {
4250 char *p;
4252 ls = 2;
4253 /* make %l into %ll */
4254 p = strchr(format, 'l');
4255 if (p)
4256 memmove(p+1, p, strlen(p)+1);
4257 else if (strcmp(format, "%p") == 0)
4258 strcpy(format, "0x%llx");
4260 switch (ls) {
4261 case -2:
4262 if (len_as_arg)
4263 trace_seq_printf(s, format, len_arg, (char)val);
4264 else
4265 trace_seq_printf(s, format, (char)val);
4266 break;
4267 case -1:
4268 if (len_as_arg)
4269 trace_seq_printf(s, format, len_arg, (short)val);
4270 else
4271 trace_seq_printf(s, format, (short)val);
4272 break;
4273 case 0:
4274 if (len_as_arg)
4275 trace_seq_printf(s, format, len_arg, (int)val);
4276 else
4277 trace_seq_printf(s, format, (int)val);
4278 break;
4279 case 1:
4280 if (len_as_arg)
4281 trace_seq_printf(s, format, len_arg, (long)val);
4282 else
4283 trace_seq_printf(s, format, (long)val);
4284 break;
4285 case 2:
4286 if (len_as_arg)
4287 trace_seq_printf(s, format, len_arg,
4288 (long long)val);
4289 else
4290 trace_seq_printf(s, format, (long long)val);
4291 break;
4292 default:
4293 do_warning("bad count (%d)", ls);
4294 event->flags |= EVENT_FL_FAILED;
4296 break;
4297 case 's':
4298 if (!arg) {
4299 do_warning("no matching argument");
4300 event->flags |= EVENT_FL_FAILED;
4301 goto out_failed;
4304 len = ((unsigned long)ptr + 1) -
4305 (unsigned long)saveptr;
4307 /* should never happen */
4308 if (len > 31) {
4309 do_warning("bad format!");
4310 event->flags |= EVENT_FL_FAILED;
4311 len = 31;
4314 memcpy(format, saveptr, len);
4315 format[len] = 0;
4316 if (!len_as_arg)
4317 len_arg = -1;
4318 /* Use helper trace_seq */
4319 trace_seq_init(&p);
4320 print_str_arg(&p, data, size, event,
4321 format, len_arg, arg);
4322 trace_seq_terminate(&p);
4323 trace_seq_puts(s, p.buffer);
4324 arg = arg->next;
4325 break;
4326 default:
4327 trace_seq_printf(s, ">%c<", *ptr);
4330 } else
4331 trace_seq_putc(s, *ptr);
4334 if (event->flags & EVENT_FL_FAILED) {
4335 out_failed:
4336 trace_seq_printf(s, "[FAILED TO PARSE]");
4339 if (args) {
4340 free_args(args);
4341 free(bprint_fmt);
4346 * pevent_data_lat_fmt - parse the data for the latency format
4347 * @pevent: a handle to the pevent
4348 * @s: the trace_seq to write to
4349 * @record: the record to read from
4351 * This parses out the Latency format (interrupts disabled,
4352 * need rescheduling, in hard/soft interrupt, preempt count
4353 * and lock depth) and places it into the trace_seq.
4355 void pevent_data_lat_fmt(struct pevent *pevent,
4356 struct trace_seq *s, struct pevent_record *record)
4358 static int check_lock_depth = 1;
4359 static int check_migrate_disable = 1;
4360 static int lock_depth_exists;
4361 static int migrate_disable_exists;
4362 unsigned int lat_flags;
4363 unsigned int pc;
4364 int lock_depth;
4365 int migrate_disable;
4366 int hardirq;
4367 int softirq;
4368 void *data = record->data;
4370 lat_flags = parse_common_flags(pevent, data);
4371 pc = parse_common_pc(pevent, data);
4372 /* lock_depth may not always exist */
4373 if (lock_depth_exists)
4374 lock_depth = parse_common_lock_depth(pevent, data);
4375 else if (check_lock_depth) {
4376 lock_depth = parse_common_lock_depth(pevent, data);
4377 if (lock_depth < 0)
4378 check_lock_depth = 0;
4379 else
4380 lock_depth_exists = 1;
4383 /* migrate_disable may not always exist */
4384 if (migrate_disable_exists)
4385 migrate_disable = parse_common_migrate_disable(pevent, data);
4386 else if (check_migrate_disable) {
4387 migrate_disable = parse_common_migrate_disable(pevent, data);
4388 if (migrate_disable < 0)
4389 check_migrate_disable = 0;
4390 else
4391 migrate_disable_exists = 1;
4394 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4395 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4397 trace_seq_printf(s, "%c%c%c",
4398 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4399 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4400 'X' : '.',
4401 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4402 'N' : '.',
4403 (hardirq && softirq) ? 'H' :
4404 hardirq ? 'h' : softirq ? 's' : '.');
4406 if (pc)
4407 trace_seq_printf(s, "%x", pc);
4408 else
4409 trace_seq_putc(s, '.');
4411 if (migrate_disable_exists) {
4412 if (migrate_disable < 0)
4413 trace_seq_putc(s, '.');
4414 else
4415 trace_seq_printf(s, "%d", migrate_disable);
4418 if (lock_depth_exists) {
4419 if (lock_depth < 0)
4420 trace_seq_putc(s, '.');
4421 else
4422 trace_seq_printf(s, "%d", lock_depth);
4425 trace_seq_terminate(s);
4429 * pevent_data_type - parse out the given event type
4430 * @pevent: a handle to the pevent
4431 * @rec: the record to read from
4433 * This returns the event id from the @rec.
4435 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4437 return trace_parse_common_type(pevent, rec->data);
4441 * pevent_data_event_from_type - find the event by a given type
4442 * @pevent: a handle to the pevent
4443 * @type: the type of the event.
4445 * This returns the event form a given @type;
4447 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4449 return pevent_find_event(pevent, type);
4453 * pevent_data_pid - parse the PID from raw data
4454 * @pevent: a handle to the pevent
4455 * @rec: the record to parse
4457 * This returns the PID from a raw data.
4459 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4461 return parse_common_pid(pevent, rec->data);
4465 * pevent_data_comm_from_pid - return the command line from PID
4466 * @pevent: a handle to the pevent
4467 * @pid: the PID of the task to search for
4469 * This returns a pointer to the command line that has the given
4470 * @pid.
4472 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4474 const char *comm;
4476 comm = find_cmdline(pevent, pid);
4477 return comm;
4481 * pevent_data_comm_from_pid - parse the data into the print format
4482 * @s: the trace_seq to write to
4483 * @event: the handle to the event
4484 * @record: the record to read from
4486 * This parses the raw @data using the given @event information and
4487 * writes the print format into the trace_seq.
4489 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4490 struct pevent_record *record)
4492 int print_pretty = 1;
4494 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
4495 print_event_fields(s, record->data, record->size, event);
4496 else {
4498 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
4499 print_pretty = event->handler(s, record, event,
4500 event->context);
4502 if (print_pretty)
4503 pretty_print(s, record->data, record->size, event);
4506 trace_seq_terminate(s);
4509 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4511 if (!use_trace_clock)
4512 return true;
4514 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4515 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4516 return true;
4518 /* trace_clock is setting in tsc or counter mode */
4519 return false;
4522 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4523 struct pevent_record *record, bool use_trace_clock)
4525 static const char *spaces = " "; /* 20 spaces */
4526 struct event_format *event;
4527 unsigned long secs;
4528 unsigned long usecs;
4529 unsigned long nsecs;
4530 const char *comm;
4531 void *data = record->data;
4532 int type;
4533 int pid;
4534 int len;
4535 int p;
4536 bool use_usec_format;
4538 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4539 use_trace_clock);
4540 if (use_usec_format) {
4541 secs = record->ts / NSECS_PER_SEC;
4542 nsecs = record->ts - secs * NSECS_PER_SEC;
4545 if (record->size < 0) {
4546 do_warning("ug! negative record size %d", record->size);
4547 return;
4550 type = trace_parse_common_type(pevent, data);
4552 event = pevent_find_event(pevent, type);
4553 if (!event) {
4554 do_warning("ug! no event found for type %d", type);
4555 return;
4558 pid = parse_common_pid(pevent, data);
4559 comm = find_cmdline(pevent, pid);
4561 if (pevent->latency_format) {
4562 trace_seq_printf(s, "%8.8s-%-5d %3d",
4563 comm, pid, record->cpu);
4564 pevent_data_lat_fmt(pevent, s, record);
4565 } else
4566 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4568 if (use_usec_format) {
4569 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4570 usecs = nsecs;
4571 p = 9;
4572 } else {
4573 usecs = (nsecs + 500) / NSECS_PER_USEC;
4574 p = 6;
4577 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4578 secs, p, usecs, event->name);
4579 } else
4580 trace_seq_printf(s, " %12llu: %s: ",
4581 record->ts, event->name);
4583 /* Space out the event names evenly. */
4584 len = strlen(event->name);
4585 if (len < 20)
4586 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4588 pevent_event_info(s, event, record);
4591 static int events_id_cmp(const void *a, const void *b)
4593 struct event_format * const * ea = a;
4594 struct event_format * const * eb = b;
4596 if ((*ea)->id < (*eb)->id)
4597 return -1;
4599 if ((*ea)->id > (*eb)->id)
4600 return 1;
4602 return 0;
4605 static int events_name_cmp(const void *a, const void *b)
4607 struct event_format * const * ea = a;
4608 struct event_format * const * eb = b;
4609 int res;
4611 res = strcmp((*ea)->name, (*eb)->name);
4612 if (res)
4613 return res;
4615 res = strcmp((*ea)->system, (*eb)->system);
4616 if (res)
4617 return res;
4619 return events_id_cmp(a, b);
4622 static int events_system_cmp(const void *a, const void *b)
4624 struct event_format * const * ea = a;
4625 struct event_format * const * eb = b;
4626 int res;
4628 res = strcmp((*ea)->system, (*eb)->system);
4629 if (res)
4630 return res;
4632 res = strcmp((*ea)->name, (*eb)->name);
4633 if (res)
4634 return res;
4636 return events_id_cmp(a, b);
4639 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4641 struct event_format **events;
4642 int (*sort)(const void *a, const void *b);
4644 events = pevent->sort_events;
4646 if (events && pevent->last_type == sort_type)
4647 return events;
4649 if (!events) {
4650 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4651 if (!events)
4652 return NULL;
4654 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4655 events[pevent->nr_events] = NULL;
4657 pevent->sort_events = events;
4659 /* the internal events are sorted by id */
4660 if (sort_type == EVENT_SORT_ID) {
4661 pevent->last_type = sort_type;
4662 return events;
4666 switch (sort_type) {
4667 case EVENT_SORT_ID:
4668 sort = events_id_cmp;
4669 break;
4670 case EVENT_SORT_NAME:
4671 sort = events_name_cmp;
4672 break;
4673 case EVENT_SORT_SYSTEM:
4674 sort = events_system_cmp;
4675 break;
4676 default:
4677 return events;
4680 qsort(events, pevent->nr_events, sizeof(*events), sort);
4681 pevent->last_type = sort_type;
4683 return events;
4686 static struct format_field **
4687 get_event_fields(const char *type, const char *name,
4688 int count, struct format_field *list)
4690 struct format_field **fields;
4691 struct format_field *field;
4692 int i = 0;
4694 fields = malloc(sizeof(*fields) * (count + 1));
4695 if (!fields)
4696 return NULL;
4698 for (field = list; field; field = field->next) {
4699 fields[i++] = field;
4700 if (i == count + 1) {
4701 do_warning("event %s has more %s fields than specified",
4702 name, type);
4703 i--;
4704 break;
4708 if (i != count)
4709 do_warning("event %s has less %s fields than specified",
4710 name, type);
4712 fields[i] = NULL;
4714 return fields;
4718 * pevent_event_common_fields - return a list of common fields for an event
4719 * @event: the event to return the common fields of.
4721 * Returns an allocated array of fields. The last item in the array is NULL.
4722 * The array must be freed with free().
4724 struct format_field **pevent_event_common_fields(struct event_format *event)
4726 return get_event_fields("common", event->name,
4727 event->format.nr_common,
4728 event->format.common_fields);
4732 * pevent_event_fields - return a list of event specific fields for an event
4733 * @event: the event to return the fields of.
4735 * Returns an allocated array of fields. The last item in the array is NULL.
4736 * The array must be freed with free().
4738 struct format_field **pevent_event_fields(struct event_format *event)
4740 return get_event_fields("event", event->name,
4741 event->format.nr_fields,
4742 event->format.fields);
4745 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4747 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4748 if (field->next) {
4749 trace_seq_puts(s, ", ");
4750 print_fields(s, field->next);
4754 /* for debugging */
4755 static void print_args(struct print_arg *args)
4757 int print_paren = 1;
4758 struct trace_seq s;
4760 switch (args->type) {
4761 case PRINT_NULL:
4762 printf("null");
4763 break;
4764 case PRINT_ATOM:
4765 printf("%s", args->atom.atom);
4766 break;
4767 case PRINT_FIELD:
4768 printf("REC->%s", args->field.name);
4769 break;
4770 case PRINT_FLAGS:
4771 printf("__print_flags(");
4772 print_args(args->flags.field);
4773 printf(", %s, ", args->flags.delim);
4774 trace_seq_init(&s);
4775 print_fields(&s, args->flags.flags);
4776 trace_seq_do_printf(&s);
4777 trace_seq_destroy(&s);
4778 printf(")");
4779 break;
4780 case PRINT_SYMBOL:
4781 printf("__print_symbolic(");
4782 print_args(args->symbol.field);
4783 printf(", ");
4784 trace_seq_init(&s);
4785 print_fields(&s, args->symbol.symbols);
4786 trace_seq_do_printf(&s);
4787 trace_seq_destroy(&s);
4788 printf(")");
4789 break;
4790 case PRINT_HEX:
4791 printf("__print_hex(");
4792 print_args(args->hex.field);
4793 printf(", ");
4794 print_args(args->hex.size);
4795 printf(")");
4796 break;
4797 case PRINT_STRING:
4798 case PRINT_BSTRING:
4799 printf("__get_str(%s)", args->string.string);
4800 break;
4801 case PRINT_TYPE:
4802 printf("(%s)", args->typecast.type);
4803 print_args(args->typecast.item);
4804 break;
4805 case PRINT_OP:
4806 if (strcmp(args->op.op, ":") == 0)
4807 print_paren = 0;
4808 if (print_paren)
4809 printf("(");
4810 print_args(args->op.left);
4811 printf(" %s ", args->op.op);
4812 print_args(args->op.right);
4813 if (print_paren)
4814 printf(")");
4815 break;
4816 default:
4817 /* we should warn... */
4818 return;
4820 if (args->next) {
4821 printf("\n");
4822 print_args(args->next);
4826 static void parse_header_field(const char *field,
4827 int *offset, int *size, int mandatory)
4829 unsigned long long save_input_buf_ptr;
4830 unsigned long long save_input_buf_siz;
4831 char *token;
4832 int type;
4834 save_input_buf_ptr = input_buf_ptr;
4835 save_input_buf_siz = input_buf_siz;
4837 if (read_expected(EVENT_ITEM, "field") < 0)
4838 return;
4839 if (read_expected(EVENT_OP, ":") < 0)
4840 return;
4842 /* type */
4843 if (read_expect_type(EVENT_ITEM, &token) < 0)
4844 goto fail;
4845 free_token(token);
4848 * If this is not a mandatory field, then test it first.
4850 if (mandatory) {
4851 if (read_expected(EVENT_ITEM, field) < 0)
4852 return;
4853 } else {
4854 if (read_expect_type(EVENT_ITEM, &token) < 0)
4855 goto fail;
4856 if (strcmp(token, field) != 0)
4857 goto discard;
4858 free_token(token);
4861 if (read_expected(EVENT_OP, ";") < 0)
4862 return;
4863 if (read_expected(EVENT_ITEM, "offset") < 0)
4864 return;
4865 if (read_expected(EVENT_OP, ":") < 0)
4866 return;
4867 if (read_expect_type(EVENT_ITEM, &token) < 0)
4868 goto fail;
4869 *offset = atoi(token);
4870 free_token(token);
4871 if (read_expected(EVENT_OP, ";") < 0)
4872 return;
4873 if (read_expected(EVENT_ITEM, "size") < 0)
4874 return;
4875 if (read_expected(EVENT_OP, ":") < 0)
4876 return;
4877 if (read_expect_type(EVENT_ITEM, &token) < 0)
4878 goto fail;
4879 *size = atoi(token);
4880 free_token(token);
4881 if (read_expected(EVENT_OP, ";") < 0)
4882 return;
4883 type = read_token(&token);
4884 if (type != EVENT_NEWLINE) {
4885 /* newer versions of the kernel have a "signed" type */
4886 if (type != EVENT_ITEM)
4887 goto fail;
4889 if (strcmp(token, "signed") != 0)
4890 goto fail;
4892 free_token(token);
4894 if (read_expected(EVENT_OP, ":") < 0)
4895 return;
4897 if (read_expect_type(EVENT_ITEM, &token))
4898 goto fail;
4900 free_token(token);
4901 if (read_expected(EVENT_OP, ";") < 0)
4902 return;
4904 if (read_expect_type(EVENT_NEWLINE, &token))
4905 goto fail;
4907 fail:
4908 free_token(token);
4909 return;
4911 discard:
4912 input_buf_ptr = save_input_buf_ptr;
4913 input_buf_siz = save_input_buf_siz;
4914 *offset = 0;
4915 *size = 0;
4916 free_token(token);
4920 * pevent_parse_header_page - parse the data stored in the header page
4921 * @pevent: the handle to the pevent
4922 * @buf: the buffer storing the header page format string
4923 * @size: the size of @buf
4924 * @long_size: the long size to use if there is no header
4926 * This parses the header page format for information on the
4927 * ring buffer used. The @buf should be copied from
4929 * /sys/kernel/debug/tracing/events/header_page
4931 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4932 int long_size)
4934 int ignore;
4936 if (!size) {
4938 * Old kernels did not have header page info.
4939 * Sorry but we just use what we find here in user space.
4941 pevent->header_page_ts_size = sizeof(long long);
4942 pevent->header_page_size_size = long_size;
4943 pevent->header_page_data_offset = sizeof(long long) + long_size;
4944 pevent->old_format = 1;
4945 return -1;
4947 init_input_buf(buf, size);
4949 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4950 &pevent->header_page_ts_size, 1);
4951 parse_header_field("commit", &pevent->header_page_size_offset,
4952 &pevent->header_page_size_size, 1);
4953 parse_header_field("overwrite", &pevent->header_page_overwrite,
4954 &ignore, 0);
4955 parse_header_field("data", &pevent->header_page_data_offset,
4956 &pevent->header_page_data_size, 1);
4958 return 0;
4961 static int event_matches(struct event_format *event,
4962 int id, const char *sys_name,
4963 const char *event_name)
4965 if (id >= 0 && id != event->id)
4966 return 0;
4968 if (event_name && (strcmp(event_name, event->name) != 0))
4969 return 0;
4971 if (sys_name && (strcmp(sys_name, event->system) != 0))
4972 return 0;
4974 return 1;
4977 static void free_handler(struct event_handler *handle)
4979 free((void *)handle->sys_name);
4980 free((void *)handle->event_name);
4981 free(handle);
4984 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4986 struct event_handler *handle, **next;
4988 for (next = &pevent->handlers; *next;
4989 next = &(*next)->next) {
4990 handle = *next;
4991 if (event_matches(event, handle->id,
4992 handle->sys_name,
4993 handle->event_name))
4994 break;
4997 if (!(*next))
4998 return 0;
5000 pr_stat("overriding event (%d) %s:%s with new print handler",
5001 event->id, event->system, event->name);
5003 event->handler = handle->func;
5004 event->context = handle->context;
5006 *next = handle->next;
5007 free_handler(handle);
5009 return 1;
5013 * __pevent_parse_format - parse the event format
5014 * @buf: the buffer storing the event format string
5015 * @size: the size of @buf
5016 * @sys: the system the event belongs to
5018 * This parses the event format and creates an event structure
5019 * to quickly parse raw data for a given event.
5021 * These files currently come from:
5023 * /sys/kernel/debug/tracing/events/.../.../format
5025 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5026 struct pevent *pevent, const char *buf,
5027 unsigned long size, const char *sys)
5029 struct event_format *event;
5030 int ret;
5032 init_input_buf(buf, size);
5034 *eventp = event = alloc_event();
5035 if (!event)
5036 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5038 event->name = event_read_name();
5039 if (!event->name) {
5040 /* Bad event? */
5041 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5042 goto event_alloc_failed;
5045 if (strcmp(sys, "ftrace") == 0) {
5046 event->flags |= EVENT_FL_ISFTRACE;
5048 if (strcmp(event->name, "bprint") == 0)
5049 event->flags |= EVENT_FL_ISBPRINT;
5052 event->id = event_read_id();
5053 if (event->id < 0) {
5054 ret = PEVENT_ERRNO__READ_ID_FAILED;
5056 * This isn't an allocation error actually.
5057 * But as the ID is critical, just bail out.
5059 goto event_alloc_failed;
5062 event->system = strdup(sys);
5063 if (!event->system) {
5064 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5065 goto event_alloc_failed;
5068 /* Add pevent to event so that it can be referenced */
5069 event->pevent = pevent;
5071 ret = event_read_format(event);
5072 if (ret < 0) {
5073 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5074 goto event_parse_failed;
5078 * If the event has an override, don't print warnings if the event
5079 * print format fails to parse.
5081 if (pevent && find_event_handle(pevent, event))
5082 show_warning = 0;
5084 ret = event_read_print(event);
5085 show_warning = 1;
5087 if (ret < 0) {
5088 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5089 goto event_parse_failed;
5092 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5093 struct format_field *field;
5094 struct print_arg *arg, **list;
5096 /* old ftrace had no args */
5097 list = &event->print_fmt.args;
5098 for (field = event->format.fields; field; field = field->next) {
5099 arg = alloc_arg();
5100 if (!arg) {
5101 event->flags |= EVENT_FL_FAILED;
5102 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5104 arg->type = PRINT_FIELD;
5105 arg->field.name = strdup(field->name);
5106 if (!arg->field.name) {
5107 event->flags |= EVENT_FL_FAILED;
5108 free_arg(arg);
5109 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5111 arg->field.field = field;
5112 *list = arg;
5113 list = &arg->next;
5115 return 0;
5118 return 0;
5120 event_parse_failed:
5121 event->flags |= EVENT_FL_FAILED;
5122 return ret;
5124 event_alloc_failed:
5125 free(event->system);
5126 free(event->name);
5127 free(event);
5128 *eventp = NULL;
5129 return ret;
5132 static enum pevent_errno
5133 __pevent_parse_event(struct pevent *pevent,
5134 struct event_format **eventp,
5135 const char *buf, unsigned long size,
5136 const char *sys)
5138 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5139 struct event_format *event = *eventp;
5141 if (event == NULL)
5142 return ret;
5144 if (pevent && add_event(pevent, event)) {
5145 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5146 goto event_add_failed;
5149 #define PRINT_ARGS 0
5150 if (PRINT_ARGS && event->print_fmt.args)
5151 print_args(event->print_fmt.args);
5153 return 0;
5155 event_add_failed:
5156 pevent_free_format(event);
5157 return ret;
5161 * pevent_parse_format - parse the event format
5162 * @pevent: the handle to the pevent
5163 * @eventp: returned format
5164 * @buf: the buffer storing the event format string
5165 * @size: the size of @buf
5166 * @sys: the system the event belongs to
5168 * This parses the event format and creates an event structure
5169 * to quickly parse raw data for a given event.
5171 * These files currently come from:
5173 * /sys/kernel/debug/tracing/events/.../.../format
5175 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5176 struct event_format **eventp,
5177 const char *buf,
5178 unsigned long size, const char *sys)
5180 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5184 * pevent_parse_event - parse the event format
5185 * @pevent: the handle to the pevent
5186 * @buf: the buffer storing the event format string
5187 * @size: the size of @buf
5188 * @sys: the system the event belongs to
5190 * This parses the event format and creates an event structure
5191 * to quickly parse raw data for a given event.
5193 * These files currently come from:
5195 * /sys/kernel/debug/tracing/events/.../.../format
5197 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5198 unsigned long size, const char *sys)
5200 struct event_format *event = NULL;
5201 return __pevent_parse_event(pevent, &event, buf, size, sys);
5204 #undef _PE
5205 #define _PE(code, str) str
5206 static const char * const pevent_error_str[] = {
5207 PEVENT_ERRORS
5209 #undef _PE
5211 int pevent_strerror(struct pevent *pevent __maybe_unused,
5212 enum pevent_errno errnum, char *buf, size_t buflen)
5214 int idx;
5215 const char *msg;
5217 if (errnum >= 0) {
5218 msg = strerror_r(errnum, buf, buflen);
5219 if (msg != buf) {
5220 size_t len = strlen(msg);
5221 memcpy(buf, msg, min(buflen - 1, len));
5222 *(buf + min(buflen - 1, len)) = '\0';
5224 return 0;
5227 if (errnum <= __PEVENT_ERRNO__START ||
5228 errnum >= __PEVENT_ERRNO__END)
5229 return -1;
5231 idx = errnum - __PEVENT_ERRNO__START - 1;
5232 msg = pevent_error_str[idx];
5233 snprintf(buf, buflen, "%s", msg);
5235 return 0;
5238 int get_field_val(struct trace_seq *s, struct format_field *field,
5239 const char *name, struct pevent_record *record,
5240 unsigned long long *val, int err)
5242 if (!field) {
5243 if (err)
5244 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5245 return -1;
5248 if (pevent_read_number_field(field, record->data, val)) {
5249 if (err)
5250 trace_seq_printf(s, " %s=INVALID", name);
5251 return -1;
5254 return 0;
5258 * pevent_get_field_raw - return the raw pointer into the data field
5259 * @s: The seq to print to on error
5260 * @event: the event that the field is for
5261 * @name: The name of the field
5262 * @record: The record with the field name.
5263 * @len: place to store the field length.
5264 * @err: print default error if failed.
5266 * Returns a pointer into record->data of the field and places
5267 * the length of the field in @len.
5269 * On failure, it returns NULL.
5271 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5272 const char *name, struct pevent_record *record,
5273 int *len, int err)
5275 struct format_field *field;
5276 void *data = record->data;
5277 unsigned offset;
5278 int dummy;
5280 if (!event)
5281 return NULL;
5283 field = pevent_find_field(event, name);
5285 if (!field) {
5286 if (err)
5287 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5288 return NULL;
5291 /* Allow @len to be NULL */
5292 if (!len)
5293 len = &dummy;
5295 offset = field->offset;
5296 if (field->flags & FIELD_IS_DYNAMIC) {
5297 offset = pevent_read_number(event->pevent,
5298 data + offset, field->size);
5299 *len = offset >> 16;
5300 offset &= 0xffff;
5301 } else
5302 *len = field->size;
5304 return data + offset;
5308 * pevent_get_field_val - find a field and return its value
5309 * @s: The seq to print to on error
5310 * @event: the event that the field is for
5311 * @name: The name of the field
5312 * @record: The record with the field name.
5313 * @val: place to store the value of the field.
5314 * @err: print default error if failed.
5316 * Returns 0 on success -1 on field not found.
5318 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5319 const char *name, struct pevent_record *record,
5320 unsigned long long *val, int err)
5322 struct format_field *field;
5324 if (!event)
5325 return -1;
5327 field = pevent_find_field(event, name);
5329 return get_field_val(s, field, name, record, val, err);
5333 * pevent_get_common_field_val - find a common field and return its value
5334 * @s: The seq to print to on error
5335 * @event: the event that the field is for
5336 * @name: The name of the field
5337 * @record: The record with the field name.
5338 * @val: place to store the value of the field.
5339 * @err: print default error if failed.
5341 * Returns 0 on success -1 on field not found.
5343 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5344 const char *name, struct pevent_record *record,
5345 unsigned long long *val, int err)
5347 struct format_field *field;
5349 if (!event)
5350 return -1;
5352 field = pevent_find_common_field(event, name);
5354 return get_field_val(s, field, name, record, val, err);
5358 * pevent_get_any_field_val - find a any field and return its value
5359 * @s: The seq to print to on error
5360 * @event: the event that the field is for
5361 * @name: The name of the field
5362 * @record: The record with the field name.
5363 * @val: place to store the value of the field.
5364 * @err: print default error if failed.
5366 * Returns 0 on success -1 on field not found.
5368 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5369 const char *name, struct pevent_record *record,
5370 unsigned long long *val, int err)
5372 struct format_field *field;
5374 if (!event)
5375 return -1;
5377 field = pevent_find_any_field(event, name);
5379 return get_field_val(s, field, name, record, val, err);
5383 * pevent_print_num_field - print a field and a format
5384 * @s: The seq to print to
5385 * @fmt: The printf format to print the field with.
5386 * @event: the event that the field is for
5387 * @name: The name of the field
5388 * @record: The record with the field name.
5389 * @err: print default error if failed.
5391 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5393 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5394 struct event_format *event, const char *name,
5395 struct pevent_record *record, int err)
5397 struct format_field *field = pevent_find_field(event, name);
5398 unsigned long long val;
5400 if (!field)
5401 goto failed;
5403 if (pevent_read_number_field(field, record->data, &val))
5404 goto failed;
5406 return trace_seq_printf(s, fmt, val);
5408 failed:
5409 if (err)
5410 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5411 return -1;
5415 * pevent_print_func_field - print a field and a format for function pointers
5416 * @s: The seq to print to
5417 * @fmt: The printf format to print the field with.
5418 * @event: the event that the field is for
5419 * @name: The name of the field
5420 * @record: The record with the field name.
5421 * @err: print default error if failed.
5423 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5425 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5426 struct event_format *event, const char *name,
5427 struct pevent_record *record, int err)
5429 struct format_field *field = pevent_find_field(event, name);
5430 struct pevent *pevent = event->pevent;
5431 unsigned long long val;
5432 struct func_map *func;
5433 char tmp[128];
5435 if (!field)
5436 goto failed;
5438 if (pevent_read_number_field(field, record->data, &val))
5439 goto failed;
5441 func = find_func(pevent, val);
5443 if (func)
5444 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5445 else
5446 sprintf(tmp, "0x%08llx", val);
5448 return trace_seq_printf(s, fmt, tmp);
5450 failed:
5451 if (err)
5452 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5453 return -1;
5456 static void free_func_handle(struct pevent_function_handler *func)
5458 struct pevent_func_params *params;
5460 free(func->name);
5462 while (func->params) {
5463 params = func->params;
5464 func->params = params->next;
5465 free(params);
5468 free(func);
5472 * pevent_register_print_function - register a helper function
5473 * @pevent: the handle to the pevent
5474 * @func: the function to process the helper function
5475 * @ret_type: the return type of the helper function
5476 * @name: the name of the helper function
5477 * @parameters: A list of enum pevent_func_arg_type
5479 * Some events may have helper functions in the print format arguments.
5480 * This allows a plugin to dynamically create a way to process one
5481 * of these functions.
5483 * The @parameters is a variable list of pevent_func_arg_type enums that
5484 * must end with PEVENT_FUNC_ARG_VOID.
5486 int pevent_register_print_function(struct pevent *pevent,
5487 pevent_func_handler func,
5488 enum pevent_func_arg_type ret_type,
5489 char *name, ...)
5491 struct pevent_function_handler *func_handle;
5492 struct pevent_func_params **next_param;
5493 struct pevent_func_params *param;
5494 enum pevent_func_arg_type type;
5495 va_list ap;
5496 int ret;
5498 func_handle = find_func_handler(pevent, name);
5499 if (func_handle) {
5501 * This is most like caused by the users own
5502 * plugins updating the function. This overrides the
5503 * system defaults.
5505 pr_stat("override of function helper '%s'", name);
5506 remove_func_handler(pevent, name);
5509 func_handle = calloc(1, sizeof(*func_handle));
5510 if (!func_handle) {
5511 do_warning("Failed to allocate function handler");
5512 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5515 func_handle->ret_type = ret_type;
5516 func_handle->name = strdup(name);
5517 func_handle->func = func;
5518 if (!func_handle->name) {
5519 do_warning("Failed to allocate function name");
5520 free(func_handle);
5521 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5524 next_param = &(func_handle->params);
5525 va_start(ap, name);
5526 for (;;) {
5527 type = va_arg(ap, enum pevent_func_arg_type);
5528 if (type == PEVENT_FUNC_ARG_VOID)
5529 break;
5531 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5532 do_warning("Invalid argument type %d", type);
5533 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5534 goto out_free;
5537 param = malloc(sizeof(*param));
5538 if (!param) {
5539 do_warning("Failed to allocate function param");
5540 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5541 goto out_free;
5543 param->type = type;
5544 param->next = NULL;
5546 *next_param = param;
5547 next_param = &(param->next);
5549 func_handle->nr_args++;
5551 va_end(ap);
5553 func_handle->next = pevent->func_handlers;
5554 pevent->func_handlers = func_handle;
5556 return 0;
5557 out_free:
5558 va_end(ap);
5559 free_func_handle(func_handle);
5560 return ret;
5564 * pevent_unregister_print_function - unregister a helper function
5565 * @pevent: the handle to the pevent
5566 * @func: the function to process the helper function
5567 * @name: the name of the helper function
5569 * This function removes existing print handler for function @name.
5571 * Returns 0 if the handler was removed successully, -1 otherwise.
5573 int pevent_unregister_print_function(struct pevent *pevent,
5574 pevent_func_handler func, char *name)
5576 struct pevent_function_handler *func_handle;
5578 func_handle = find_func_handler(pevent, name);
5579 if (func_handle && func_handle->func == func) {
5580 remove_func_handler(pevent, name);
5581 return 0;
5583 return -1;
5586 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
5587 const char *sys_name,
5588 const char *event_name)
5590 struct event_format *event;
5592 if (id >= 0) {
5593 /* search by id */
5594 event = pevent_find_event(pevent, id);
5595 if (!event)
5596 return NULL;
5597 if (event_name && (strcmp(event_name, event->name) != 0))
5598 return NULL;
5599 if (sys_name && (strcmp(sys_name, event->system) != 0))
5600 return NULL;
5601 } else {
5602 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5603 if (!event)
5604 return NULL;
5606 return event;
5610 * pevent_register_event_handler - register a way to parse an event
5611 * @pevent: the handle to the pevent
5612 * @id: the id of the event to register
5613 * @sys_name: the system name the event belongs to
5614 * @event_name: the name of the event
5615 * @func: the function to call to parse the event information
5616 * @context: the data to be passed to @func
5618 * This function allows a developer to override the parsing of
5619 * a given event. If for some reason the default print format
5620 * is not sufficient, this function will register a function
5621 * for an event to be used to parse the data instead.
5623 * If @id is >= 0, then it is used to find the event.
5624 * else @sys_name and @event_name are used.
5626 int pevent_register_event_handler(struct pevent *pevent, int id,
5627 const char *sys_name, const char *event_name,
5628 pevent_event_handler_func func, void *context)
5630 struct event_format *event;
5631 struct event_handler *handle;
5633 event = pevent_search_event(pevent, id, sys_name, event_name);
5634 if (event == NULL)
5635 goto not_found;
5637 pr_stat("overriding event (%d) %s:%s with new print handler",
5638 event->id, event->system, event->name);
5640 event->handler = func;
5641 event->context = context;
5642 return 0;
5644 not_found:
5645 /* Save for later use. */
5646 handle = calloc(1, sizeof(*handle));
5647 if (!handle) {
5648 do_warning("Failed to allocate event handler");
5649 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5652 handle->id = id;
5653 if (event_name)
5654 handle->event_name = strdup(event_name);
5655 if (sys_name)
5656 handle->sys_name = strdup(sys_name);
5658 if ((event_name && !handle->event_name) ||
5659 (sys_name && !handle->sys_name)) {
5660 do_warning("Failed to allocate event/sys name");
5661 free((void *)handle->event_name);
5662 free((void *)handle->sys_name);
5663 free(handle);
5664 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5667 handle->func = func;
5668 handle->next = pevent->handlers;
5669 pevent->handlers = handle;
5670 handle->context = context;
5672 return -1;
5675 static int handle_matches(struct event_handler *handler, int id,
5676 const char *sys_name, const char *event_name,
5677 pevent_event_handler_func func, void *context)
5679 if (id >= 0 && id != handler->id)
5680 return 0;
5682 if (event_name && (strcmp(event_name, handler->event_name) != 0))
5683 return 0;
5685 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
5686 return 0;
5688 if (func != handler->func || context != handler->context)
5689 return 0;
5691 return 1;
5695 * pevent_unregister_event_handler - unregister an existing event handler
5696 * @pevent: the handle to the pevent
5697 * @id: the id of the event to unregister
5698 * @sys_name: the system name the handler belongs to
5699 * @event_name: the name of the event handler
5700 * @func: the function to call to parse the event information
5701 * @context: the data to be passed to @func
5703 * This function removes existing event handler (parser).
5705 * If @id is >= 0, then it is used to find the event.
5706 * else @sys_name and @event_name are used.
5708 * Returns 0 if handler was removed successfully, -1 if event was not found.
5710 int pevent_unregister_event_handler(struct pevent *pevent, int id,
5711 const char *sys_name, const char *event_name,
5712 pevent_event_handler_func func, void *context)
5714 struct event_format *event;
5715 struct event_handler *handle;
5716 struct event_handler **next;
5718 event = pevent_search_event(pevent, id, sys_name, event_name);
5719 if (event == NULL)
5720 goto not_found;
5722 if (event->handler == func && event->context == context) {
5723 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
5724 event->id, event->system, event->name);
5726 event->handler = NULL;
5727 event->context = NULL;
5728 return 0;
5731 not_found:
5732 for (next = &pevent->handlers; *next; next = &(*next)->next) {
5733 handle = *next;
5734 if (handle_matches(handle, id, sys_name, event_name,
5735 func, context))
5736 break;
5739 if (!(*next))
5740 return -1;
5742 *next = handle->next;
5743 free_handler(handle);
5745 return 0;
5749 * pevent_alloc - create a pevent handle
5751 struct pevent *pevent_alloc(void)
5753 struct pevent *pevent = calloc(1, sizeof(*pevent));
5755 if (pevent)
5756 pevent->ref_count = 1;
5758 return pevent;
5761 void pevent_ref(struct pevent *pevent)
5763 pevent->ref_count++;
5766 static void free_format_fields(struct format_field *field)
5768 struct format_field *next;
5770 while (field) {
5771 next = field->next;
5772 free(field->type);
5773 free(field->name);
5774 free(field);
5775 field = next;
5779 static void free_formats(struct format *format)
5781 free_format_fields(format->common_fields);
5782 free_format_fields(format->fields);
5785 void pevent_free_format(struct event_format *event)
5787 free(event->name);
5788 free(event->system);
5790 free_formats(&event->format);
5792 free(event->print_fmt.format);
5793 free_args(event->print_fmt.args);
5795 free(event);
5799 * pevent_free - free a pevent handle
5800 * @pevent: the pevent handle to free
5802 void pevent_free(struct pevent *pevent)
5804 struct cmdline_list *cmdlist, *cmdnext;
5805 struct func_list *funclist, *funcnext;
5806 struct printk_list *printklist, *printknext;
5807 struct pevent_function_handler *func_handler;
5808 struct event_handler *handle;
5809 int i;
5811 if (!pevent)
5812 return;
5814 cmdlist = pevent->cmdlist;
5815 funclist = pevent->funclist;
5816 printklist = pevent->printklist;
5818 pevent->ref_count--;
5819 if (pevent->ref_count)
5820 return;
5822 if (pevent->cmdlines) {
5823 for (i = 0; i < pevent->cmdline_count; i++)
5824 free(pevent->cmdlines[i].comm);
5825 free(pevent->cmdlines);
5828 while (cmdlist) {
5829 cmdnext = cmdlist->next;
5830 free(cmdlist->comm);
5831 free(cmdlist);
5832 cmdlist = cmdnext;
5835 if (pevent->func_map) {
5836 for (i = 0; i < (int)pevent->func_count; i++) {
5837 free(pevent->func_map[i].func);
5838 free(pevent->func_map[i].mod);
5840 free(pevent->func_map);
5843 while (funclist) {
5844 funcnext = funclist->next;
5845 free(funclist->func);
5846 free(funclist->mod);
5847 free(funclist);
5848 funclist = funcnext;
5851 while (pevent->func_handlers) {
5852 func_handler = pevent->func_handlers;
5853 pevent->func_handlers = func_handler->next;
5854 free_func_handle(func_handler);
5857 if (pevent->printk_map) {
5858 for (i = 0; i < (int)pevent->printk_count; i++)
5859 free(pevent->printk_map[i].printk);
5860 free(pevent->printk_map);
5863 while (printklist) {
5864 printknext = printklist->next;
5865 free(printklist->printk);
5866 free(printklist);
5867 printklist = printknext;
5870 for (i = 0; i < pevent->nr_events; i++)
5871 pevent_free_format(pevent->events[i]);
5873 while (pevent->handlers) {
5874 handle = pevent->handlers;
5875 pevent->handlers = handle->next;
5876 free_handler(handle);
5879 free(pevent->events);
5880 free(pevent->sort_events);
5882 free(pevent);
5885 void pevent_unref(struct pevent *pevent)
5887 pevent_free(pevent);