Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[linux-2.6.git] / tools / lib / traceevent / event-parse.c
blob82b0606dcb8ab04ed08920abf9c2c193e4ee42bf
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 struct func_map {
309 unsigned long long addr;
310 char *func;
311 char *mod;
314 struct func_list {
315 struct func_list *next;
316 unsigned long long addr;
317 char *func;
318 char *mod;
321 static int func_cmp(const void *a, const void *b)
323 const struct func_map *fa = a;
324 const struct func_map *fb = b;
326 if (fa->addr < fb->addr)
327 return -1;
328 if (fa->addr > fb->addr)
329 return 1;
331 return 0;
335 * We are searching for a record in between, not an exact
336 * match.
338 static int func_bcmp(const void *a, const void *b)
340 const struct func_map *fa = a;
341 const struct func_map *fb = b;
343 if ((fa->addr == fb->addr) ||
345 (fa->addr > fb->addr &&
346 fa->addr < (fb+1)->addr))
347 return 0;
349 if (fa->addr < fb->addr)
350 return -1;
352 return 1;
355 static int func_map_init(struct pevent *pevent)
357 struct func_list *funclist;
358 struct func_list *item;
359 struct func_map *func_map;
360 int i;
362 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
363 if (!func_map)
364 return -1;
366 funclist = pevent->funclist;
368 i = 0;
369 while (funclist) {
370 func_map[i].func = funclist->func;
371 func_map[i].addr = funclist->addr;
372 func_map[i].mod = funclist->mod;
373 i++;
374 item = funclist;
375 funclist = funclist->next;
376 free(item);
379 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
382 * Add a special record at the end.
384 func_map[pevent->func_count].func = NULL;
385 func_map[pevent->func_count].addr = 0;
386 func_map[pevent->func_count].mod = NULL;
388 pevent->func_map = func_map;
389 pevent->funclist = NULL;
391 return 0;
394 static struct func_map *
395 find_func(struct pevent *pevent, unsigned long long addr)
397 struct func_map *func;
398 struct func_map key;
400 if (!pevent->func_map)
401 func_map_init(pevent);
403 key.addr = addr;
405 func = bsearch(&key, pevent->func_map, pevent->func_count,
406 sizeof(*pevent->func_map), func_bcmp);
408 return func;
412 * pevent_find_function - find a function by a given address
413 * @pevent: handle for the pevent
414 * @addr: the address to find the function with
416 * Returns a pointer to the function stored that has the given
417 * address. Note, the address does not have to be exact, it
418 * will select the function that would contain the address.
420 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
422 struct func_map *map;
424 map = find_func(pevent, addr);
425 if (!map)
426 return NULL;
428 return map->func;
432 * pevent_find_function_address - find a function address by a given address
433 * @pevent: handle for the pevent
434 * @addr: the address to find the function with
436 * Returns the address the function starts at. This can be used in
437 * conjunction with pevent_find_function to print both the function
438 * name and the function offset.
440 unsigned long long
441 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
443 struct func_map *map;
445 map = find_func(pevent, addr);
446 if (!map)
447 return 0;
449 return map->addr;
453 * pevent_register_function - register a function with a given address
454 * @pevent: handle for the pevent
455 * @function: the function name to register
456 * @addr: the address the function starts at
457 * @mod: the kernel module the function may be in (NULL for none)
459 * This registers a function name with an address and module.
460 * The @func passed in is duplicated.
462 int pevent_register_function(struct pevent *pevent, char *func,
463 unsigned long long addr, char *mod)
465 struct func_list *item = malloc(sizeof(*item));
467 if (!item)
468 return -1;
470 item->next = pevent->funclist;
471 item->func = strdup(func);
472 if (!item->func)
473 goto out_free;
475 if (mod) {
476 item->mod = strdup(mod);
477 if (!item->mod)
478 goto out_free_func;
479 } else
480 item->mod = NULL;
481 item->addr = addr;
483 pevent->funclist = item;
484 pevent->func_count++;
486 return 0;
488 out_free_func:
489 free(item->func);
490 item->func = NULL;
491 out_free:
492 free(item);
493 errno = ENOMEM;
494 return -1;
498 * pevent_print_funcs - print out the stored functions
499 * @pevent: handle for the pevent
501 * This prints out the stored functions.
503 void pevent_print_funcs(struct pevent *pevent)
505 int i;
507 if (!pevent->func_map)
508 func_map_init(pevent);
510 for (i = 0; i < (int)pevent->func_count; i++) {
511 printf("%016llx %s",
512 pevent->func_map[i].addr,
513 pevent->func_map[i].func);
514 if (pevent->func_map[i].mod)
515 printf(" [%s]\n", pevent->func_map[i].mod);
516 else
517 printf("\n");
521 struct printk_map {
522 unsigned long long addr;
523 char *printk;
526 struct printk_list {
527 struct printk_list *next;
528 unsigned long long addr;
529 char *printk;
532 static int printk_cmp(const void *a, const void *b)
534 const struct printk_map *pa = a;
535 const struct printk_map *pb = b;
537 if (pa->addr < pb->addr)
538 return -1;
539 if (pa->addr > pb->addr)
540 return 1;
542 return 0;
545 static int printk_map_init(struct pevent *pevent)
547 struct printk_list *printklist;
548 struct printk_list *item;
549 struct printk_map *printk_map;
550 int i;
552 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
553 if (!printk_map)
554 return -1;
556 printklist = pevent->printklist;
558 i = 0;
559 while (printklist) {
560 printk_map[i].printk = printklist->printk;
561 printk_map[i].addr = printklist->addr;
562 i++;
563 item = printklist;
564 printklist = printklist->next;
565 free(item);
568 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
570 pevent->printk_map = printk_map;
571 pevent->printklist = NULL;
573 return 0;
576 static struct printk_map *
577 find_printk(struct pevent *pevent, unsigned long long addr)
579 struct printk_map *printk;
580 struct printk_map key;
582 if (!pevent->printk_map && printk_map_init(pevent))
583 return NULL;
585 key.addr = addr;
587 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
588 sizeof(*pevent->printk_map), printk_cmp);
590 return printk;
594 * pevent_register_print_string - register a string by its address
595 * @pevent: handle for the pevent
596 * @fmt: the string format to register
597 * @addr: the address the string was located at
599 * This registers a string by the address it was stored in the kernel.
600 * The @fmt passed in is duplicated.
602 int pevent_register_print_string(struct pevent *pevent, char *fmt,
603 unsigned long long addr)
605 struct printk_list *item = malloc(sizeof(*item));
607 if (!item)
608 return -1;
610 item->next = pevent->printklist;
611 item->addr = addr;
613 item->printk = strdup(fmt);
614 if (!item->printk)
615 goto out_free;
617 pevent->printklist = item;
618 pevent->printk_count++;
620 return 0;
622 out_free:
623 free(item);
624 errno = ENOMEM;
625 return -1;
629 * pevent_print_printk - print out the stored strings
630 * @pevent: handle for the pevent
632 * This prints the string formats that were stored.
634 void pevent_print_printk(struct pevent *pevent)
636 int i;
638 if (!pevent->printk_map)
639 printk_map_init(pevent);
641 for (i = 0; i < (int)pevent->printk_count; i++) {
642 printf("%016llx %s\n",
643 pevent->printk_map[i].addr,
644 pevent->printk_map[i].printk);
648 static struct event_format *alloc_event(void)
650 return calloc(1, sizeof(struct event_format));
653 static int add_event(struct pevent *pevent, struct event_format *event)
655 int i;
656 struct event_format **events = realloc(pevent->events, sizeof(event) *
657 (pevent->nr_events + 1));
658 if (!events)
659 return -1;
661 pevent->events = events;
663 for (i = 0; i < pevent->nr_events; i++) {
664 if (pevent->events[i]->id > event->id)
665 break;
667 if (i < pevent->nr_events)
668 memmove(&pevent->events[i + 1],
669 &pevent->events[i],
670 sizeof(event) * (pevent->nr_events - i));
672 pevent->events[i] = event;
673 pevent->nr_events++;
675 event->pevent = pevent;
677 return 0;
680 static int event_item_type(enum event_type type)
682 switch (type) {
683 case EVENT_ITEM ... EVENT_SQUOTE:
684 return 1;
685 case EVENT_ERROR ... EVENT_DELIM:
686 default:
687 return 0;
691 static void free_flag_sym(struct print_flag_sym *fsym)
693 struct print_flag_sym *next;
695 while (fsym) {
696 next = fsym->next;
697 free(fsym->value);
698 free(fsym->str);
699 free(fsym);
700 fsym = next;
704 static void free_arg(struct print_arg *arg)
706 struct print_arg *farg;
708 if (!arg)
709 return;
711 switch (arg->type) {
712 case PRINT_ATOM:
713 free(arg->atom.atom);
714 break;
715 case PRINT_FIELD:
716 free(arg->field.name);
717 break;
718 case PRINT_FLAGS:
719 free_arg(arg->flags.field);
720 free(arg->flags.delim);
721 free_flag_sym(arg->flags.flags);
722 break;
723 case PRINT_SYMBOL:
724 free_arg(arg->symbol.field);
725 free_flag_sym(arg->symbol.symbols);
726 break;
727 case PRINT_HEX:
728 free_arg(arg->hex.field);
729 free_arg(arg->hex.size);
730 break;
731 case PRINT_TYPE:
732 free(arg->typecast.type);
733 free_arg(arg->typecast.item);
734 break;
735 case PRINT_STRING:
736 case PRINT_BSTRING:
737 free(arg->string.string);
738 break;
739 case PRINT_DYNAMIC_ARRAY:
740 free(arg->dynarray.index);
741 break;
742 case PRINT_OP:
743 free(arg->op.op);
744 free_arg(arg->op.left);
745 free_arg(arg->op.right);
746 break;
747 case PRINT_FUNC:
748 while (arg->func.args) {
749 farg = arg->func.args;
750 arg->func.args = farg->next;
751 free_arg(farg);
753 break;
755 case PRINT_NULL:
756 default:
757 break;
760 free(arg);
763 static enum event_type get_type(int ch)
765 if (ch == '\n')
766 return EVENT_NEWLINE;
767 if (isspace(ch))
768 return EVENT_SPACE;
769 if (isalnum(ch) || ch == '_')
770 return EVENT_ITEM;
771 if (ch == '\'')
772 return EVENT_SQUOTE;
773 if (ch == '"')
774 return EVENT_DQUOTE;
775 if (!isprint(ch))
776 return EVENT_NONE;
777 if (ch == '(' || ch == ')' || ch == ',')
778 return EVENT_DELIM;
780 return EVENT_OP;
783 static int __read_char(void)
785 if (input_buf_ptr >= input_buf_siz)
786 return -1;
788 return input_buf[input_buf_ptr++];
791 static int __peek_char(void)
793 if (input_buf_ptr >= input_buf_siz)
794 return -1;
796 return input_buf[input_buf_ptr];
800 * pevent_peek_char - peek at the next character that will be read
802 * Returns the next character read, or -1 if end of buffer.
804 int pevent_peek_char(void)
806 return __peek_char();
809 static int extend_token(char **tok, char *buf, int size)
811 char *newtok = realloc(*tok, size);
813 if (!newtok) {
814 free(*tok);
815 *tok = NULL;
816 return -1;
819 if (!*tok)
820 strcpy(newtok, buf);
821 else
822 strcat(newtok, buf);
823 *tok = newtok;
825 return 0;
828 static enum event_type force_token(const char *str, char **tok);
830 static enum event_type __read_token(char **tok)
832 char buf[BUFSIZ];
833 int ch, last_ch, quote_ch, next_ch;
834 int i = 0;
835 int tok_size = 0;
836 enum event_type type;
838 *tok = NULL;
841 ch = __read_char();
842 if (ch < 0)
843 return EVENT_NONE;
845 type = get_type(ch);
846 if (type == EVENT_NONE)
847 return type;
849 buf[i++] = ch;
851 switch (type) {
852 case EVENT_NEWLINE:
853 case EVENT_DELIM:
854 if (asprintf(tok, "%c", ch) < 0)
855 return EVENT_ERROR;
857 return type;
859 case EVENT_OP:
860 switch (ch) {
861 case '-':
862 next_ch = __peek_char();
863 if (next_ch == '>') {
864 buf[i++] = __read_char();
865 break;
867 /* fall through */
868 case '+':
869 case '|':
870 case '&':
871 case '>':
872 case '<':
873 last_ch = ch;
874 ch = __peek_char();
875 if (ch != last_ch)
876 goto test_equal;
877 buf[i++] = __read_char();
878 switch (last_ch) {
879 case '>':
880 case '<':
881 goto test_equal;
882 default:
883 break;
885 break;
886 case '!':
887 case '=':
888 goto test_equal;
889 default: /* what should we do instead? */
890 break;
892 buf[i] = 0;
893 *tok = strdup(buf);
894 return type;
896 test_equal:
897 ch = __peek_char();
898 if (ch == '=')
899 buf[i++] = __read_char();
900 goto out;
902 case EVENT_DQUOTE:
903 case EVENT_SQUOTE:
904 /* don't keep quotes */
905 i--;
906 quote_ch = ch;
907 last_ch = 0;
908 concat:
909 do {
910 if (i == (BUFSIZ - 1)) {
911 buf[i] = 0;
912 tok_size += BUFSIZ;
914 if (extend_token(tok, buf, tok_size) < 0)
915 return EVENT_NONE;
916 i = 0;
918 last_ch = ch;
919 ch = __read_char();
920 buf[i++] = ch;
921 /* the '\' '\' will cancel itself */
922 if (ch == '\\' && last_ch == '\\')
923 last_ch = 0;
924 } while (ch != quote_ch || last_ch == '\\');
925 /* remove the last quote */
926 i--;
929 * For strings (double quotes) check the next token.
930 * If it is another string, concatinate the two.
932 if (type == EVENT_DQUOTE) {
933 unsigned long long save_input_buf_ptr = input_buf_ptr;
935 do {
936 ch = __read_char();
937 } while (isspace(ch));
938 if (ch == '"')
939 goto concat;
940 input_buf_ptr = save_input_buf_ptr;
943 goto out;
945 case EVENT_ERROR ... EVENT_SPACE:
946 case EVENT_ITEM:
947 default:
948 break;
951 while (get_type(__peek_char()) == type) {
952 if (i == (BUFSIZ - 1)) {
953 buf[i] = 0;
954 tok_size += BUFSIZ;
956 if (extend_token(tok, buf, tok_size) < 0)
957 return EVENT_NONE;
958 i = 0;
960 ch = __read_char();
961 buf[i++] = ch;
964 out:
965 buf[i] = 0;
966 if (extend_token(tok, buf, tok_size + i + 1) < 0)
967 return EVENT_NONE;
969 if (type == EVENT_ITEM) {
971 * Older versions of the kernel has a bug that
972 * creates invalid symbols and will break the mac80211
973 * parsing. This is a work around to that bug.
975 * See Linux kernel commit:
976 * 811cb50baf63461ce0bdb234927046131fc7fa8b
978 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
979 free(*tok);
980 *tok = NULL;
981 return force_token("\"\%s\" ", tok);
982 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
983 free(*tok);
984 *tok = NULL;
985 return force_token("\" sta:%pM\" ", tok);
986 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
987 free(*tok);
988 *tok = NULL;
989 return force_token("\" vif:%p(%d)\" ", tok);
993 return type;
996 static enum event_type force_token(const char *str, char **tok)
998 const char *save_input_buf;
999 unsigned long long save_input_buf_ptr;
1000 unsigned long long save_input_buf_siz;
1001 enum event_type type;
1003 /* save off the current input pointers */
1004 save_input_buf = input_buf;
1005 save_input_buf_ptr = input_buf_ptr;
1006 save_input_buf_siz = input_buf_siz;
1008 init_input_buf(str, strlen(str));
1010 type = __read_token(tok);
1012 /* reset back to original token */
1013 input_buf = save_input_buf;
1014 input_buf_ptr = save_input_buf_ptr;
1015 input_buf_siz = save_input_buf_siz;
1017 return type;
1020 static void free_token(char *tok)
1022 if (tok)
1023 free(tok);
1026 static enum event_type read_token(char **tok)
1028 enum event_type type;
1030 for (;;) {
1031 type = __read_token(tok);
1032 if (type != EVENT_SPACE)
1033 return type;
1035 free_token(*tok);
1038 /* not reached */
1039 *tok = NULL;
1040 return EVENT_NONE;
1044 * pevent_read_token - access to utilites to use the pevent parser
1045 * @tok: The token to return
1047 * This will parse tokens from the string given by
1048 * pevent_init_data().
1050 * Returns the token type.
1052 enum event_type pevent_read_token(char **tok)
1054 return read_token(tok);
1058 * pevent_free_token - free a token returned by pevent_read_token
1059 * @token: the token to free
1061 void pevent_free_token(char *token)
1063 free_token(token);
1066 /* no newline */
1067 static enum event_type read_token_item(char **tok)
1069 enum event_type type;
1071 for (;;) {
1072 type = __read_token(tok);
1073 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1074 return type;
1075 free_token(*tok);
1076 *tok = NULL;
1079 /* not reached */
1080 *tok = NULL;
1081 return EVENT_NONE;
1084 static int test_type(enum event_type type, enum event_type expect)
1086 if (type != expect) {
1087 do_warning("Error: expected type %d but read %d",
1088 expect, type);
1089 return -1;
1091 return 0;
1094 static int test_type_token(enum event_type type, const char *token,
1095 enum event_type expect, const char *expect_tok)
1097 if (type != expect) {
1098 do_warning("Error: expected type %d but read %d",
1099 expect, type);
1100 return -1;
1103 if (strcmp(token, expect_tok) != 0) {
1104 do_warning("Error: expected '%s' but read '%s'",
1105 expect_tok, token);
1106 return -1;
1108 return 0;
1111 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1113 enum event_type type;
1115 if (newline_ok)
1116 type = read_token(tok);
1117 else
1118 type = read_token_item(tok);
1119 return test_type(type, expect);
1122 static int read_expect_type(enum event_type expect, char **tok)
1124 return __read_expect_type(expect, tok, 1);
1127 static int __read_expected(enum event_type expect, const char *str,
1128 int newline_ok)
1130 enum event_type type;
1131 char *token;
1132 int ret;
1134 if (newline_ok)
1135 type = read_token(&token);
1136 else
1137 type = read_token_item(&token);
1139 ret = test_type_token(type, token, expect, str);
1141 free_token(token);
1143 return ret;
1146 static int read_expected(enum event_type expect, const char *str)
1148 return __read_expected(expect, str, 1);
1151 static int read_expected_item(enum event_type expect, const char *str)
1153 return __read_expected(expect, str, 0);
1156 static char *event_read_name(void)
1158 char *token;
1160 if (read_expected(EVENT_ITEM, "name") < 0)
1161 return NULL;
1163 if (read_expected(EVENT_OP, ":") < 0)
1164 return NULL;
1166 if (read_expect_type(EVENT_ITEM, &token) < 0)
1167 goto fail;
1169 return token;
1171 fail:
1172 free_token(token);
1173 return NULL;
1176 static int event_read_id(void)
1178 char *token;
1179 int id;
1181 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1182 return -1;
1184 if (read_expected(EVENT_OP, ":") < 0)
1185 return -1;
1187 if (read_expect_type(EVENT_ITEM, &token) < 0)
1188 goto fail;
1190 id = strtoul(token, NULL, 0);
1191 free_token(token);
1192 return id;
1194 fail:
1195 free_token(token);
1196 return -1;
1199 static int field_is_string(struct format_field *field)
1201 if ((field->flags & FIELD_IS_ARRAY) &&
1202 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1203 strstr(field->type, "s8")))
1204 return 1;
1206 return 0;
1209 static int field_is_dynamic(struct format_field *field)
1211 if (strncmp(field->type, "__data_loc", 10) == 0)
1212 return 1;
1214 return 0;
1217 static int field_is_long(struct format_field *field)
1219 /* includes long long */
1220 if (strstr(field->type, "long"))
1221 return 1;
1223 return 0;
1226 static unsigned int type_size(const char *name)
1228 /* This covers all FIELD_IS_STRING types. */
1229 static struct {
1230 const char *type;
1231 unsigned int size;
1232 } table[] = {
1233 { "u8", 1 },
1234 { "u16", 2 },
1235 { "u32", 4 },
1236 { "u64", 8 },
1237 { "s8", 1 },
1238 { "s16", 2 },
1239 { "s32", 4 },
1240 { "s64", 8 },
1241 { "char", 1 },
1242 { },
1244 int i;
1246 for (i = 0; table[i].type; i++) {
1247 if (!strcmp(table[i].type, name))
1248 return table[i].size;
1251 return 0;
1254 static int event_read_fields(struct event_format *event, struct format_field **fields)
1256 struct format_field *field = NULL;
1257 enum event_type type;
1258 char *token;
1259 char *last_token;
1260 int count = 0;
1262 do {
1263 unsigned int size_dynamic = 0;
1265 type = read_token(&token);
1266 if (type == EVENT_NEWLINE) {
1267 free_token(token);
1268 return count;
1271 count++;
1273 if (test_type_token(type, token, EVENT_ITEM, "field"))
1274 goto fail;
1275 free_token(token);
1277 type = read_token(&token);
1279 * The ftrace fields may still use the "special" name.
1280 * Just ignore it.
1282 if (event->flags & EVENT_FL_ISFTRACE &&
1283 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1284 free_token(token);
1285 type = read_token(&token);
1288 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1289 goto fail;
1291 free_token(token);
1292 if (read_expect_type(EVENT_ITEM, &token) < 0)
1293 goto fail;
1295 last_token = token;
1297 field = calloc(1, sizeof(*field));
1298 if (!field)
1299 goto fail;
1301 field->event = event;
1303 /* read the rest of the type */
1304 for (;;) {
1305 type = read_token(&token);
1306 if (type == EVENT_ITEM ||
1307 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1309 * Some of the ftrace fields are broken and have
1310 * an illegal "." in them.
1312 (event->flags & EVENT_FL_ISFTRACE &&
1313 type == EVENT_OP && strcmp(token, ".") == 0)) {
1315 if (strcmp(token, "*") == 0)
1316 field->flags |= FIELD_IS_POINTER;
1318 if (field->type) {
1319 char *new_type;
1320 new_type = realloc(field->type,
1321 strlen(field->type) +
1322 strlen(last_token) + 2);
1323 if (!new_type) {
1324 free(last_token);
1325 goto fail;
1327 field->type = new_type;
1328 strcat(field->type, " ");
1329 strcat(field->type, last_token);
1330 free(last_token);
1331 } else
1332 field->type = last_token;
1333 last_token = token;
1334 continue;
1337 break;
1340 if (!field->type) {
1341 do_warning("%s: no type found", __func__);
1342 goto fail;
1344 field->name = last_token;
1346 if (test_type(type, EVENT_OP))
1347 goto fail;
1349 if (strcmp(token, "[") == 0) {
1350 enum event_type last_type = type;
1351 char *brackets = token;
1352 char *new_brackets;
1353 int len;
1355 field->flags |= FIELD_IS_ARRAY;
1357 type = read_token(&token);
1359 if (type == EVENT_ITEM)
1360 field->arraylen = strtoul(token, NULL, 0);
1361 else
1362 field->arraylen = 0;
1364 while (strcmp(token, "]") != 0) {
1365 if (last_type == EVENT_ITEM &&
1366 type == EVENT_ITEM)
1367 len = 2;
1368 else
1369 len = 1;
1370 last_type = type;
1372 new_brackets = realloc(brackets,
1373 strlen(brackets) +
1374 strlen(token) + len);
1375 if (!new_brackets) {
1376 free(brackets);
1377 goto fail;
1379 brackets = new_brackets;
1380 if (len == 2)
1381 strcat(brackets, " ");
1382 strcat(brackets, token);
1383 /* We only care about the last token */
1384 field->arraylen = strtoul(token, NULL, 0);
1385 free_token(token);
1386 type = read_token(&token);
1387 if (type == EVENT_NONE) {
1388 do_warning("failed to find token");
1389 goto fail;
1393 free_token(token);
1395 new_brackets = realloc(brackets, strlen(brackets) + 2);
1396 if (!new_brackets) {
1397 free(brackets);
1398 goto fail;
1400 brackets = new_brackets;
1401 strcat(brackets, "]");
1403 /* add brackets to type */
1405 type = read_token(&token);
1407 * If the next token is not an OP, then it is of
1408 * the format: type [] item;
1410 if (type == EVENT_ITEM) {
1411 char *new_type;
1412 new_type = realloc(field->type,
1413 strlen(field->type) +
1414 strlen(field->name) +
1415 strlen(brackets) + 2);
1416 if (!new_type) {
1417 free(brackets);
1418 goto fail;
1420 field->type = new_type;
1421 strcat(field->type, " ");
1422 strcat(field->type, field->name);
1423 size_dynamic = type_size(field->name);
1424 free_token(field->name);
1425 strcat(field->type, brackets);
1426 field->name = token;
1427 type = read_token(&token);
1428 } else {
1429 char *new_type;
1430 new_type = realloc(field->type,
1431 strlen(field->type) +
1432 strlen(brackets) + 1);
1433 if (!new_type) {
1434 free(brackets);
1435 goto fail;
1437 field->type = new_type;
1438 strcat(field->type, brackets);
1440 free(brackets);
1443 if (field_is_string(field))
1444 field->flags |= FIELD_IS_STRING;
1445 if (field_is_dynamic(field))
1446 field->flags |= FIELD_IS_DYNAMIC;
1447 if (field_is_long(field))
1448 field->flags |= FIELD_IS_LONG;
1450 if (test_type_token(type, token, EVENT_OP, ";"))
1451 goto fail;
1452 free_token(token);
1454 if (read_expected(EVENT_ITEM, "offset") < 0)
1455 goto fail_expect;
1457 if (read_expected(EVENT_OP, ":") < 0)
1458 goto fail_expect;
1460 if (read_expect_type(EVENT_ITEM, &token))
1461 goto fail;
1462 field->offset = strtoul(token, NULL, 0);
1463 free_token(token);
1465 if (read_expected(EVENT_OP, ";") < 0)
1466 goto fail_expect;
1468 if (read_expected(EVENT_ITEM, "size") < 0)
1469 goto fail_expect;
1471 if (read_expected(EVENT_OP, ":") < 0)
1472 goto fail_expect;
1474 if (read_expect_type(EVENT_ITEM, &token))
1475 goto fail;
1476 field->size = strtoul(token, NULL, 0);
1477 free_token(token);
1479 if (read_expected(EVENT_OP, ";") < 0)
1480 goto fail_expect;
1482 type = read_token(&token);
1483 if (type != EVENT_NEWLINE) {
1484 /* newer versions of the kernel have a "signed" type */
1485 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1486 goto fail;
1488 free_token(token);
1490 if (read_expected(EVENT_OP, ":") < 0)
1491 goto fail_expect;
1493 if (read_expect_type(EVENT_ITEM, &token))
1494 goto fail;
1496 if (strtoul(token, NULL, 0))
1497 field->flags |= FIELD_IS_SIGNED;
1499 free_token(token);
1500 if (read_expected(EVENT_OP, ";") < 0)
1501 goto fail_expect;
1503 if (read_expect_type(EVENT_NEWLINE, &token))
1504 goto fail;
1507 free_token(token);
1509 if (field->flags & FIELD_IS_ARRAY) {
1510 if (field->arraylen)
1511 field->elementsize = field->size / field->arraylen;
1512 else if (field->flags & FIELD_IS_DYNAMIC)
1513 field->elementsize = size_dynamic;
1514 else if (field->flags & FIELD_IS_STRING)
1515 field->elementsize = 1;
1516 else if (field->flags & FIELD_IS_LONG)
1517 field->elementsize = event->pevent ?
1518 event->pevent->long_size :
1519 sizeof(long);
1520 } else
1521 field->elementsize = field->size;
1523 *fields = field;
1524 fields = &field->next;
1526 } while (1);
1528 return 0;
1530 fail:
1531 free_token(token);
1532 fail_expect:
1533 if (field) {
1534 free(field->type);
1535 free(field->name);
1536 free(field);
1538 return -1;
1541 static int event_read_format(struct event_format *event)
1543 char *token;
1544 int ret;
1546 if (read_expected_item(EVENT_ITEM, "format") < 0)
1547 return -1;
1549 if (read_expected(EVENT_OP, ":") < 0)
1550 return -1;
1552 if (read_expect_type(EVENT_NEWLINE, &token))
1553 goto fail;
1554 free_token(token);
1556 ret = event_read_fields(event, &event->format.common_fields);
1557 if (ret < 0)
1558 return ret;
1559 event->format.nr_common = ret;
1561 ret = event_read_fields(event, &event->format.fields);
1562 if (ret < 0)
1563 return ret;
1564 event->format.nr_fields = ret;
1566 return 0;
1568 fail:
1569 free_token(token);
1570 return -1;
1573 static enum event_type
1574 process_arg_token(struct event_format *event, struct print_arg *arg,
1575 char **tok, enum event_type type);
1577 static enum event_type
1578 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1580 enum event_type type;
1581 char *token;
1583 type = read_token(&token);
1584 *tok = token;
1586 return process_arg_token(event, arg, tok, type);
1589 static enum event_type
1590 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1592 static enum event_type
1593 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1595 struct print_arg *arg, *left, *right;
1596 enum event_type type;
1597 char *token = NULL;
1599 arg = alloc_arg();
1600 left = alloc_arg();
1601 right = alloc_arg();
1603 if (!arg || !left || !right) {
1604 do_warning("%s: not enough memory!", __func__);
1605 /* arg will be freed at out_free */
1606 free_arg(left);
1607 free_arg(right);
1608 goto out_free;
1611 arg->type = PRINT_OP;
1612 arg->op.left = left;
1613 arg->op.right = right;
1615 *tok = NULL;
1616 type = process_arg(event, left, &token);
1618 again:
1619 /* Handle other operations in the arguments */
1620 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1621 type = process_op(event, left, &token);
1622 goto again;
1625 if (test_type_token(type, token, EVENT_OP, ":"))
1626 goto out_free;
1628 arg->op.op = token;
1630 type = process_arg(event, right, &token);
1632 top->op.right = arg;
1634 *tok = token;
1635 return type;
1637 out_free:
1638 /* Top may point to itself */
1639 top->op.right = NULL;
1640 free_token(token);
1641 free_arg(arg);
1642 return EVENT_ERROR;
1645 static enum event_type
1646 process_array(struct event_format *event, struct print_arg *top, char **tok)
1648 struct print_arg *arg;
1649 enum event_type type;
1650 char *token = NULL;
1652 arg = alloc_arg();
1653 if (!arg) {
1654 do_warning("%s: not enough memory!", __func__);
1655 /* '*tok' is set to top->op.op. No need to free. */
1656 *tok = NULL;
1657 return EVENT_ERROR;
1660 *tok = NULL;
1661 type = process_arg(event, arg, &token);
1662 if (test_type_token(type, token, EVENT_OP, "]"))
1663 goto out_free;
1665 top->op.right = arg;
1667 free_token(token);
1668 type = read_token_item(&token);
1669 *tok = token;
1671 return type;
1673 out_free:
1674 free_token(token);
1675 free_arg(arg);
1676 return EVENT_ERROR;
1679 static int get_op_prio(char *op)
1681 if (!op[1]) {
1682 switch (op[0]) {
1683 case '~':
1684 case '!':
1685 return 4;
1686 case '*':
1687 case '/':
1688 case '%':
1689 return 6;
1690 case '+':
1691 case '-':
1692 return 7;
1693 /* '>>' and '<<' are 8 */
1694 case '<':
1695 case '>':
1696 return 9;
1697 /* '==' and '!=' are 10 */
1698 case '&':
1699 return 11;
1700 case '^':
1701 return 12;
1702 case '|':
1703 return 13;
1704 case '?':
1705 return 16;
1706 default:
1707 do_warning("unknown op '%c'", op[0]);
1708 return -1;
1710 } else {
1711 if (strcmp(op, "++") == 0 ||
1712 strcmp(op, "--") == 0) {
1713 return 3;
1714 } else if (strcmp(op, ">>") == 0 ||
1715 strcmp(op, "<<") == 0) {
1716 return 8;
1717 } else if (strcmp(op, ">=") == 0 ||
1718 strcmp(op, "<=") == 0) {
1719 return 9;
1720 } else if (strcmp(op, "==") == 0 ||
1721 strcmp(op, "!=") == 0) {
1722 return 10;
1723 } else if (strcmp(op, "&&") == 0) {
1724 return 14;
1725 } else if (strcmp(op, "||") == 0) {
1726 return 15;
1727 } else {
1728 do_warning("unknown op '%s'", op);
1729 return -1;
1734 static int set_op_prio(struct print_arg *arg)
1737 /* single ops are the greatest */
1738 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1739 arg->op.prio = 0;
1740 else
1741 arg->op.prio = get_op_prio(arg->op.op);
1743 return arg->op.prio;
1746 /* Note, *tok does not get freed, but will most likely be saved */
1747 static enum event_type
1748 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1750 struct print_arg *left, *right = NULL;
1751 enum event_type type;
1752 char *token;
1754 /* the op is passed in via tok */
1755 token = *tok;
1757 if (arg->type == PRINT_OP && !arg->op.left) {
1758 /* handle single op */
1759 if (token[1]) {
1760 do_warning("bad op token %s", token);
1761 goto out_free;
1763 switch (token[0]) {
1764 case '~':
1765 case '!':
1766 case '+':
1767 case '-':
1768 break;
1769 default:
1770 do_warning("bad op token %s", token);
1771 goto out_free;
1775 /* make an empty left */
1776 left = alloc_arg();
1777 if (!left)
1778 goto out_warn_free;
1780 left->type = PRINT_NULL;
1781 arg->op.left = left;
1783 right = alloc_arg();
1784 if (!right)
1785 goto out_warn_free;
1787 arg->op.right = right;
1789 /* do not free the token, it belongs to an op */
1790 *tok = NULL;
1791 type = process_arg(event, right, tok);
1793 } else if (strcmp(token, "?") == 0) {
1795 left = alloc_arg();
1796 if (!left)
1797 goto out_warn_free;
1799 /* copy the top arg to the left */
1800 *left = *arg;
1802 arg->type = PRINT_OP;
1803 arg->op.op = token;
1804 arg->op.left = left;
1805 arg->op.prio = 0;
1807 /* it will set arg->op.right */
1808 type = process_cond(event, arg, tok);
1810 } else if (strcmp(token, ">>") == 0 ||
1811 strcmp(token, "<<") == 0 ||
1812 strcmp(token, "&") == 0 ||
1813 strcmp(token, "|") == 0 ||
1814 strcmp(token, "&&") == 0 ||
1815 strcmp(token, "||") == 0 ||
1816 strcmp(token, "-") == 0 ||
1817 strcmp(token, "+") == 0 ||
1818 strcmp(token, "*") == 0 ||
1819 strcmp(token, "^") == 0 ||
1820 strcmp(token, "/") == 0 ||
1821 strcmp(token, "<") == 0 ||
1822 strcmp(token, ">") == 0 ||
1823 strcmp(token, "<=") == 0 ||
1824 strcmp(token, ">=") == 0 ||
1825 strcmp(token, "==") == 0 ||
1826 strcmp(token, "!=") == 0) {
1828 left = alloc_arg();
1829 if (!left)
1830 goto out_warn_free;
1832 /* copy the top arg to the left */
1833 *left = *arg;
1835 arg->type = PRINT_OP;
1836 arg->op.op = token;
1837 arg->op.left = left;
1838 arg->op.right = NULL;
1840 if (set_op_prio(arg) == -1) {
1841 event->flags |= EVENT_FL_FAILED;
1842 /* arg->op.op (= token) will be freed at out_free */
1843 arg->op.op = NULL;
1844 goto out_free;
1847 type = read_token_item(&token);
1848 *tok = token;
1850 /* could just be a type pointer */
1851 if ((strcmp(arg->op.op, "*") == 0) &&
1852 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1853 char *new_atom;
1855 if (left->type != PRINT_ATOM) {
1856 do_warning("bad pointer type");
1857 goto out_free;
1859 new_atom = realloc(left->atom.atom,
1860 strlen(left->atom.atom) + 3);
1861 if (!new_atom)
1862 goto out_warn_free;
1864 left->atom.atom = new_atom;
1865 strcat(left->atom.atom, " *");
1866 free(arg->op.op);
1867 *arg = *left;
1868 free(left);
1870 return type;
1873 right = alloc_arg();
1874 if (!right)
1875 goto out_warn_free;
1877 type = process_arg_token(event, right, tok, type);
1878 arg->op.right = right;
1880 } else if (strcmp(token, "[") == 0) {
1882 left = alloc_arg();
1883 if (!left)
1884 goto out_warn_free;
1886 *left = *arg;
1888 arg->type = PRINT_OP;
1889 arg->op.op = token;
1890 arg->op.left = left;
1892 arg->op.prio = 0;
1894 /* it will set arg->op.right */
1895 type = process_array(event, arg, tok);
1897 } else {
1898 do_warning("unknown op '%s'", token);
1899 event->flags |= EVENT_FL_FAILED;
1900 /* the arg is now the left side */
1901 goto out_free;
1904 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1905 int prio;
1907 /* higher prios need to be closer to the root */
1908 prio = get_op_prio(*tok);
1910 if (prio > arg->op.prio)
1911 return process_op(event, arg, tok);
1913 return process_op(event, right, tok);
1916 return type;
1918 out_warn_free:
1919 do_warning("%s: not enough memory!", __func__);
1920 out_free:
1921 free_token(token);
1922 *tok = NULL;
1923 return EVENT_ERROR;
1926 static enum event_type
1927 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1928 char **tok)
1930 enum event_type type;
1931 char *field;
1932 char *token;
1934 if (read_expected(EVENT_OP, "->") < 0)
1935 goto out_err;
1937 if (read_expect_type(EVENT_ITEM, &token) < 0)
1938 goto out_free;
1939 field = token;
1941 arg->type = PRINT_FIELD;
1942 arg->field.name = field;
1944 if (is_flag_field) {
1945 arg->field.field = pevent_find_any_field(event, arg->field.name);
1946 arg->field.field->flags |= FIELD_IS_FLAG;
1947 is_flag_field = 0;
1948 } else if (is_symbolic_field) {
1949 arg->field.field = pevent_find_any_field(event, arg->field.name);
1950 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1951 is_symbolic_field = 0;
1954 type = read_token(&token);
1955 *tok = token;
1957 return type;
1959 out_free:
1960 free_token(token);
1961 out_err:
1962 *tok = NULL;
1963 return EVENT_ERROR;
1966 static char *arg_eval (struct print_arg *arg);
1968 static unsigned long long
1969 eval_type_str(unsigned long long val, const char *type, int pointer)
1971 int sign = 0;
1972 char *ref;
1973 int len;
1975 len = strlen(type);
1977 if (pointer) {
1979 if (type[len-1] != '*') {
1980 do_warning("pointer expected with non pointer type");
1981 return val;
1984 ref = malloc(len);
1985 if (!ref) {
1986 do_warning("%s: not enough memory!", __func__);
1987 return val;
1989 memcpy(ref, type, len);
1991 /* chop off the " *" */
1992 ref[len - 2] = 0;
1994 val = eval_type_str(val, ref, 0);
1995 free(ref);
1996 return val;
1999 /* check if this is a pointer */
2000 if (type[len - 1] == '*')
2001 return val;
2003 /* Try to figure out the arg size*/
2004 if (strncmp(type, "struct", 6) == 0)
2005 /* all bets off */
2006 return val;
2008 if (strcmp(type, "u8") == 0)
2009 return val & 0xff;
2011 if (strcmp(type, "u16") == 0)
2012 return val & 0xffff;
2014 if (strcmp(type, "u32") == 0)
2015 return val & 0xffffffff;
2017 if (strcmp(type, "u64") == 0 ||
2018 strcmp(type, "s64"))
2019 return val;
2021 if (strcmp(type, "s8") == 0)
2022 return (unsigned long long)(char)val & 0xff;
2024 if (strcmp(type, "s16") == 0)
2025 return (unsigned long long)(short)val & 0xffff;
2027 if (strcmp(type, "s32") == 0)
2028 return (unsigned long long)(int)val & 0xffffffff;
2030 if (strncmp(type, "unsigned ", 9) == 0) {
2031 sign = 0;
2032 type += 9;
2035 if (strcmp(type, "char") == 0) {
2036 if (sign)
2037 return (unsigned long long)(char)val & 0xff;
2038 else
2039 return val & 0xff;
2042 if (strcmp(type, "short") == 0) {
2043 if (sign)
2044 return (unsigned long long)(short)val & 0xffff;
2045 else
2046 return val & 0xffff;
2049 if (strcmp(type, "int") == 0) {
2050 if (sign)
2051 return (unsigned long long)(int)val & 0xffffffff;
2052 else
2053 return val & 0xffffffff;
2056 return val;
2060 * Try to figure out the type.
2062 static unsigned long long
2063 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2065 if (arg->type != PRINT_TYPE) {
2066 do_warning("expected type argument");
2067 return 0;
2070 return eval_type_str(val, arg->typecast.type, pointer);
2073 static int arg_num_eval(struct print_arg *arg, long long *val)
2075 long long left, right;
2076 int ret = 1;
2078 switch (arg->type) {
2079 case PRINT_ATOM:
2080 *val = strtoll(arg->atom.atom, NULL, 0);
2081 break;
2082 case PRINT_TYPE:
2083 ret = arg_num_eval(arg->typecast.item, val);
2084 if (!ret)
2085 break;
2086 *val = eval_type(*val, arg, 0);
2087 break;
2088 case PRINT_OP:
2089 switch (arg->op.op[0]) {
2090 case '|':
2091 ret = arg_num_eval(arg->op.left, &left);
2092 if (!ret)
2093 break;
2094 ret = arg_num_eval(arg->op.right, &right);
2095 if (!ret)
2096 break;
2097 if (arg->op.op[1])
2098 *val = left || right;
2099 else
2100 *val = left | right;
2101 break;
2102 case '&':
2103 ret = arg_num_eval(arg->op.left, &left);
2104 if (!ret)
2105 break;
2106 ret = arg_num_eval(arg->op.right, &right);
2107 if (!ret)
2108 break;
2109 if (arg->op.op[1])
2110 *val = left && right;
2111 else
2112 *val = left & right;
2113 break;
2114 case '<':
2115 ret = arg_num_eval(arg->op.left, &left);
2116 if (!ret)
2117 break;
2118 ret = arg_num_eval(arg->op.right, &right);
2119 if (!ret)
2120 break;
2121 switch (arg->op.op[1]) {
2122 case 0:
2123 *val = left < right;
2124 break;
2125 case '<':
2126 *val = left << right;
2127 break;
2128 case '=':
2129 *val = left <= right;
2130 break;
2131 default:
2132 do_warning("unknown op '%s'", arg->op.op);
2133 ret = 0;
2135 break;
2136 case '>':
2137 ret = arg_num_eval(arg->op.left, &left);
2138 if (!ret)
2139 break;
2140 ret = arg_num_eval(arg->op.right, &right);
2141 if (!ret)
2142 break;
2143 switch (arg->op.op[1]) {
2144 case 0:
2145 *val = left > right;
2146 break;
2147 case '>':
2148 *val = left >> right;
2149 break;
2150 case '=':
2151 *val = left >= right;
2152 break;
2153 default:
2154 do_warning("unknown op '%s'", arg->op.op);
2155 ret = 0;
2157 break;
2158 case '=':
2159 ret = arg_num_eval(arg->op.left, &left);
2160 if (!ret)
2161 break;
2162 ret = arg_num_eval(arg->op.right, &right);
2163 if (!ret)
2164 break;
2166 if (arg->op.op[1] != '=') {
2167 do_warning("unknown op '%s'", arg->op.op);
2168 ret = 0;
2169 } else
2170 *val = left == right;
2171 break;
2172 case '!':
2173 ret = arg_num_eval(arg->op.left, &left);
2174 if (!ret)
2175 break;
2176 ret = arg_num_eval(arg->op.right, &right);
2177 if (!ret)
2178 break;
2180 switch (arg->op.op[1]) {
2181 case '=':
2182 *val = left != right;
2183 break;
2184 default:
2185 do_warning("unknown op '%s'", arg->op.op);
2186 ret = 0;
2188 break;
2189 case '-':
2190 /* check for negative */
2191 if (arg->op.left->type == PRINT_NULL)
2192 left = 0;
2193 else
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;
2200 *val = left - right;
2201 break;
2202 case '+':
2203 if (arg->op.left->type == PRINT_NULL)
2204 left = 0;
2205 else
2206 ret = arg_num_eval(arg->op.left, &left);
2207 if (!ret)
2208 break;
2209 ret = arg_num_eval(arg->op.right, &right);
2210 if (!ret)
2211 break;
2212 *val = left + right;
2213 break;
2214 default:
2215 do_warning("unknown op '%s'", arg->op.op);
2216 ret = 0;
2218 break;
2220 case PRINT_NULL:
2221 case PRINT_FIELD ... PRINT_SYMBOL:
2222 case PRINT_STRING:
2223 case PRINT_BSTRING:
2224 default:
2225 do_warning("invalid eval type %d", arg->type);
2226 ret = 0;
2229 return ret;
2232 static char *arg_eval (struct print_arg *arg)
2234 long long val;
2235 static char buf[20];
2237 switch (arg->type) {
2238 case PRINT_ATOM:
2239 return arg->atom.atom;
2240 case PRINT_TYPE:
2241 return arg_eval(arg->typecast.item);
2242 case PRINT_OP:
2243 if (!arg_num_eval(arg, &val))
2244 break;
2245 sprintf(buf, "%lld", val);
2246 return buf;
2248 case PRINT_NULL:
2249 case PRINT_FIELD ... PRINT_SYMBOL:
2250 case PRINT_STRING:
2251 case PRINT_BSTRING:
2252 default:
2253 do_warning("invalid eval type %d", arg->type);
2254 break;
2257 return NULL;
2260 static enum event_type
2261 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2263 enum event_type type;
2264 struct print_arg *arg = NULL;
2265 struct print_flag_sym *field;
2266 char *token = *tok;
2267 char *value;
2269 do {
2270 free_token(token);
2271 type = read_token_item(&token);
2272 if (test_type_token(type, token, EVENT_OP, "{"))
2273 break;
2275 arg = alloc_arg();
2276 if (!arg)
2277 goto out_free;
2279 free_token(token);
2280 type = process_arg(event, arg, &token);
2282 if (type == EVENT_OP)
2283 type = process_op(event, arg, &token);
2285 if (type == EVENT_ERROR)
2286 goto out_free;
2288 if (test_type_token(type, token, EVENT_DELIM, ","))
2289 goto out_free;
2291 field = calloc(1, sizeof(*field));
2292 if (!field)
2293 goto out_free;
2295 value = arg_eval(arg);
2296 if (value == NULL)
2297 goto out_free_field;
2298 field->value = strdup(value);
2299 if (field->value == NULL)
2300 goto out_free_field;
2302 free_arg(arg);
2303 arg = alloc_arg();
2304 if (!arg)
2305 goto out_free;
2307 free_token(token);
2308 type = process_arg(event, arg, &token);
2309 if (test_type_token(type, token, EVENT_OP, "}"))
2310 goto out_free_field;
2312 value = arg_eval(arg);
2313 if (value == NULL)
2314 goto out_free_field;
2315 field->str = strdup(value);
2316 if (field->str == NULL)
2317 goto out_free_field;
2318 free_arg(arg);
2319 arg = NULL;
2321 *list = field;
2322 list = &field->next;
2324 free_token(token);
2325 type = read_token_item(&token);
2326 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2328 *tok = token;
2329 return type;
2331 out_free_field:
2332 free_flag_sym(field);
2333 out_free:
2334 free_arg(arg);
2335 free_token(token);
2336 *tok = NULL;
2338 return EVENT_ERROR;
2341 static enum event_type
2342 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2344 struct print_arg *field;
2345 enum event_type type;
2346 char *token;
2348 memset(arg, 0, sizeof(*arg));
2349 arg->type = PRINT_FLAGS;
2351 field = alloc_arg();
2352 if (!field) {
2353 do_warning("%s: not enough memory!", __func__);
2354 goto out_free;
2357 type = process_arg(event, field, &token);
2359 /* Handle operations in the first argument */
2360 while (type == EVENT_OP)
2361 type = process_op(event, field, &token);
2363 if (test_type_token(type, token, EVENT_DELIM, ","))
2364 goto out_free_field;
2365 free_token(token);
2367 arg->flags.field = field;
2369 type = read_token_item(&token);
2370 if (event_item_type(type)) {
2371 arg->flags.delim = token;
2372 type = read_token_item(&token);
2375 if (test_type_token(type, token, EVENT_DELIM, ","))
2376 goto out_free;
2378 type = process_fields(event, &arg->flags.flags, &token);
2379 if (test_type_token(type, token, EVENT_DELIM, ")"))
2380 goto out_free;
2382 free_token(token);
2383 type = read_token_item(tok);
2384 return type;
2386 out_free_field:
2387 free_arg(field);
2388 out_free:
2389 free_token(token);
2390 *tok = NULL;
2391 return EVENT_ERROR;
2394 static enum event_type
2395 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2397 struct print_arg *field;
2398 enum event_type type;
2399 char *token;
2401 memset(arg, 0, sizeof(*arg));
2402 arg->type = PRINT_SYMBOL;
2404 field = alloc_arg();
2405 if (!field) {
2406 do_warning("%s: not enough memory!", __func__);
2407 goto out_free;
2410 type = process_arg(event, field, &token);
2411 if (test_type_token(type, token, EVENT_DELIM, ","))
2412 goto out_free_field;
2414 arg->symbol.field = field;
2416 type = process_fields(event, &arg->symbol.symbols, &token);
2417 if (test_type_token(type, token, EVENT_DELIM, ")"))
2418 goto out_free;
2420 free_token(token);
2421 type = read_token_item(tok);
2422 return type;
2424 out_free_field:
2425 free_arg(field);
2426 out_free:
2427 free_token(token);
2428 *tok = NULL;
2429 return EVENT_ERROR;
2432 static enum event_type
2433 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2435 struct print_arg *field;
2436 enum event_type type;
2437 char *token;
2439 memset(arg, 0, sizeof(*arg));
2440 arg->type = PRINT_HEX;
2442 field = alloc_arg();
2443 if (!field) {
2444 do_warning("%s: not enough memory!", __func__);
2445 goto out_free;
2448 type = process_arg(event, field, &token);
2450 if (test_type_token(type, token, EVENT_DELIM, ","))
2451 goto out_free;
2453 arg->hex.field = field;
2455 free_token(token);
2457 field = alloc_arg();
2458 if (!field) {
2459 do_warning("%s: not enough memory!", __func__);
2460 *tok = NULL;
2461 return EVENT_ERROR;
2464 type = process_arg(event, field, &token);
2466 if (test_type_token(type, token, EVENT_DELIM, ")"))
2467 goto out_free;
2469 arg->hex.size = field;
2471 free_token(token);
2472 type = read_token_item(tok);
2473 return type;
2475 out_free:
2476 free_arg(field);
2477 free_token(token);
2478 *tok = NULL;
2479 return EVENT_ERROR;
2482 static enum event_type
2483 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2485 struct format_field *field;
2486 enum event_type type;
2487 char *token;
2489 memset(arg, 0, sizeof(*arg));
2490 arg->type = PRINT_DYNAMIC_ARRAY;
2493 * The item within the parenthesis is another field that holds
2494 * the index into where the array starts.
2496 type = read_token(&token);
2497 *tok = token;
2498 if (type != EVENT_ITEM)
2499 goto out_free;
2501 /* Find the field */
2503 field = pevent_find_field(event, token);
2504 if (!field)
2505 goto out_free;
2507 arg->dynarray.field = field;
2508 arg->dynarray.index = 0;
2510 if (read_expected(EVENT_DELIM, ")") < 0)
2511 goto out_free;
2513 free_token(token);
2514 type = read_token_item(&token);
2515 *tok = token;
2516 if (type != EVENT_OP || strcmp(token, "[") != 0)
2517 return type;
2519 free_token(token);
2520 arg = alloc_arg();
2521 if (!arg) {
2522 do_warning("%s: not enough memory!", __func__);
2523 *tok = NULL;
2524 return EVENT_ERROR;
2527 type = process_arg(event, arg, &token);
2528 if (type == EVENT_ERROR)
2529 goto out_free_arg;
2531 if (!test_type_token(type, token, EVENT_OP, "]"))
2532 goto out_free_arg;
2534 free_token(token);
2535 type = read_token_item(tok);
2536 return type;
2538 out_free_arg:
2539 free_arg(arg);
2540 out_free:
2541 free_token(token);
2542 *tok = NULL;
2543 return EVENT_ERROR;
2546 static enum event_type
2547 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2549 struct print_arg *item_arg;
2550 enum event_type type;
2551 char *token;
2553 type = process_arg(event, arg, &token);
2555 if (type == EVENT_ERROR)
2556 goto out_free;
2558 if (type == EVENT_OP)
2559 type = process_op(event, arg, &token);
2561 if (type == EVENT_ERROR)
2562 goto out_free;
2564 if (test_type_token(type, token, EVENT_DELIM, ")"))
2565 goto out_free;
2567 free_token(token);
2568 type = read_token_item(&token);
2571 * If the next token is an item or another open paren, then
2572 * this was a typecast.
2574 if (event_item_type(type) ||
2575 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2577 /* make this a typecast and contine */
2579 /* prevous must be an atom */
2580 if (arg->type != PRINT_ATOM) {
2581 do_warning("previous needed to be PRINT_ATOM");
2582 goto out_free;
2585 item_arg = alloc_arg();
2586 if (!item_arg) {
2587 do_warning("%s: not enough memory!", __func__);
2588 goto out_free;
2591 arg->type = PRINT_TYPE;
2592 arg->typecast.type = arg->atom.atom;
2593 arg->typecast.item = item_arg;
2594 type = process_arg_token(event, item_arg, &token, type);
2598 *tok = token;
2599 return type;
2601 out_free:
2602 free_token(token);
2603 *tok = NULL;
2604 return EVENT_ERROR;
2608 static enum event_type
2609 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2610 char **tok)
2612 enum event_type type;
2613 char *token;
2615 if (read_expect_type(EVENT_ITEM, &token) < 0)
2616 goto out_free;
2618 arg->type = PRINT_STRING;
2619 arg->string.string = token;
2620 arg->string.offset = -1;
2622 if (read_expected(EVENT_DELIM, ")") < 0)
2623 goto out_err;
2625 type = read_token(&token);
2626 *tok = token;
2628 return type;
2630 out_free:
2631 free_token(token);
2632 out_err:
2633 *tok = NULL;
2634 return EVENT_ERROR;
2637 static struct pevent_function_handler *
2638 find_func_handler(struct pevent *pevent, char *func_name)
2640 struct pevent_function_handler *func;
2642 if (!pevent)
2643 return NULL;
2645 for (func = pevent->func_handlers; func; func = func->next) {
2646 if (strcmp(func->name, func_name) == 0)
2647 break;
2650 return func;
2653 static void remove_func_handler(struct pevent *pevent, char *func_name)
2655 struct pevent_function_handler *func;
2656 struct pevent_function_handler **next;
2658 next = &pevent->func_handlers;
2659 while ((func = *next)) {
2660 if (strcmp(func->name, func_name) == 0) {
2661 *next = func->next;
2662 free_func_handle(func);
2663 break;
2665 next = &func->next;
2669 static enum event_type
2670 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2671 struct print_arg *arg, char **tok)
2673 struct print_arg **next_arg;
2674 struct print_arg *farg;
2675 enum event_type type;
2676 char *token;
2677 const char *test;
2678 int i;
2680 arg->type = PRINT_FUNC;
2681 arg->func.func = func;
2683 *tok = NULL;
2685 next_arg = &(arg->func.args);
2686 for (i = 0; i < func->nr_args; i++) {
2687 farg = alloc_arg();
2688 if (!farg) {
2689 do_warning("%s: not enough memory!", __func__);
2690 return EVENT_ERROR;
2693 type = process_arg(event, farg, &token);
2694 if (i < (func->nr_args - 1))
2695 test = ",";
2696 else
2697 test = ")";
2699 if (test_type_token(type, token, EVENT_DELIM, test)) {
2700 free_arg(farg);
2701 free_token(token);
2702 return EVENT_ERROR;
2705 *next_arg = farg;
2706 next_arg = &(farg->next);
2707 free_token(token);
2710 type = read_token(&token);
2711 *tok = token;
2713 return type;
2716 static enum event_type
2717 process_function(struct event_format *event, struct print_arg *arg,
2718 char *token, char **tok)
2720 struct pevent_function_handler *func;
2722 if (strcmp(token, "__print_flags") == 0) {
2723 free_token(token);
2724 is_flag_field = 1;
2725 return process_flags(event, arg, tok);
2727 if (strcmp(token, "__print_symbolic") == 0) {
2728 free_token(token);
2729 is_symbolic_field = 1;
2730 return process_symbols(event, arg, tok);
2732 if (strcmp(token, "__print_hex") == 0) {
2733 free_token(token);
2734 return process_hex(event, arg, tok);
2736 if (strcmp(token, "__get_str") == 0) {
2737 free_token(token);
2738 return process_str(event, arg, tok);
2740 if (strcmp(token, "__get_dynamic_array") == 0) {
2741 free_token(token);
2742 return process_dynamic_array(event, arg, tok);
2745 func = find_func_handler(event->pevent, token);
2746 if (func) {
2747 free_token(token);
2748 return process_func_handler(event, func, arg, tok);
2751 do_warning("function %s not defined", token);
2752 free_token(token);
2753 return EVENT_ERROR;
2756 static enum event_type
2757 process_arg_token(struct event_format *event, struct print_arg *arg,
2758 char **tok, enum event_type type)
2760 char *token;
2761 char *atom;
2763 token = *tok;
2765 switch (type) {
2766 case EVENT_ITEM:
2767 if (strcmp(token, "REC") == 0) {
2768 free_token(token);
2769 type = process_entry(event, arg, &token);
2770 break;
2772 atom = token;
2773 /* test the next token */
2774 type = read_token_item(&token);
2777 * If the next token is a parenthesis, then this
2778 * is a function.
2780 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2781 free_token(token);
2782 token = NULL;
2783 /* this will free atom. */
2784 type = process_function(event, arg, atom, &token);
2785 break;
2787 /* atoms can be more than one token long */
2788 while (type == EVENT_ITEM) {
2789 char *new_atom;
2790 new_atom = realloc(atom,
2791 strlen(atom) + strlen(token) + 2);
2792 if (!new_atom) {
2793 free(atom);
2794 *tok = NULL;
2795 free_token(token);
2796 return EVENT_ERROR;
2798 atom = new_atom;
2799 strcat(atom, " ");
2800 strcat(atom, token);
2801 free_token(token);
2802 type = read_token_item(&token);
2805 arg->type = PRINT_ATOM;
2806 arg->atom.atom = atom;
2807 break;
2809 case EVENT_DQUOTE:
2810 case EVENT_SQUOTE:
2811 arg->type = PRINT_ATOM;
2812 arg->atom.atom = token;
2813 type = read_token_item(&token);
2814 break;
2815 case EVENT_DELIM:
2816 if (strcmp(token, "(") == 0) {
2817 free_token(token);
2818 type = process_paren(event, arg, &token);
2819 break;
2821 case EVENT_OP:
2822 /* handle single ops */
2823 arg->type = PRINT_OP;
2824 arg->op.op = token;
2825 arg->op.left = NULL;
2826 type = process_op(event, arg, &token);
2828 /* On error, the op is freed */
2829 if (type == EVENT_ERROR)
2830 arg->op.op = NULL;
2832 /* return error type if errored */
2833 break;
2835 case EVENT_ERROR ... EVENT_NEWLINE:
2836 default:
2837 do_warning("unexpected type %d", type);
2838 return EVENT_ERROR;
2840 *tok = token;
2842 return type;
2845 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2847 enum event_type type = EVENT_ERROR;
2848 struct print_arg *arg;
2849 char *token;
2850 int args = 0;
2852 do {
2853 if (type == EVENT_NEWLINE) {
2854 type = read_token_item(&token);
2855 continue;
2858 arg = alloc_arg();
2859 if (!arg) {
2860 do_warning("%s: not enough memory!", __func__);
2861 return -1;
2864 type = process_arg(event, arg, &token);
2866 if (type == EVENT_ERROR) {
2867 free_token(token);
2868 free_arg(arg);
2869 return -1;
2872 *list = arg;
2873 args++;
2875 if (type == EVENT_OP) {
2876 type = process_op(event, arg, &token);
2877 free_token(token);
2878 if (type == EVENT_ERROR) {
2879 *list = NULL;
2880 free_arg(arg);
2881 return -1;
2883 list = &arg->next;
2884 continue;
2887 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2888 free_token(token);
2889 *list = arg;
2890 list = &arg->next;
2891 continue;
2893 break;
2894 } while (type != EVENT_NONE);
2896 if (type != EVENT_NONE && type != EVENT_ERROR)
2897 free_token(token);
2899 return args;
2902 static int event_read_print(struct event_format *event)
2904 enum event_type type;
2905 char *token;
2906 int ret;
2908 if (read_expected_item(EVENT_ITEM, "print") < 0)
2909 return -1;
2911 if (read_expected(EVENT_ITEM, "fmt") < 0)
2912 return -1;
2914 if (read_expected(EVENT_OP, ":") < 0)
2915 return -1;
2917 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2918 goto fail;
2920 concat:
2921 event->print_fmt.format = token;
2922 event->print_fmt.args = NULL;
2924 /* ok to have no arg */
2925 type = read_token_item(&token);
2927 if (type == EVENT_NONE)
2928 return 0;
2930 /* Handle concatenation of print lines */
2931 if (type == EVENT_DQUOTE) {
2932 char *cat;
2934 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2935 goto fail;
2936 free_token(token);
2937 free_token(event->print_fmt.format);
2938 event->print_fmt.format = NULL;
2939 token = cat;
2940 goto concat;
2943 if (test_type_token(type, token, EVENT_DELIM, ","))
2944 goto fail;
2946 free_token(token);
2948 ret = event_read_print_args(event, &event->print_fmt.args);
2949 if (ret < 0)
2950 return -1;
2952 return ret;
2954 fail:
2955 free_token(token);
2956 return -1;
2960 * pevent_find_common_field - return a common field by event
2961 * @event: handle for the event
2962 * @name: the name of the common field to return
2964 * Returns a common field from the event by the given @name.
2965 * This only searchs the common fields and not all field.
2967 struct format_field *
2968 pevent_find_common_field(struct event_format *event, const char *name)
2970 struct format_field *format;
2972 for (format = event->format.common_fields;
2973 format; format = format->next) {
2974 if (strcmp(format->name, name) == 0)
2975 break;
2978 return format;
2982 * pevent_find_field - find a non-common field
2983 * @event: handle for the event
2984 * @name: the name of the non-common field
2986 * Returns a non-common field by the given @name.
2987 * This does not search common fields.
2989 struct format_field *
2990 pevent_find_field(struct event_format *event, const char *name)
2992 struct format_field *format;
2994 for (format = event->format.fields;
2995 format; format = format->next) {
2996 if (strcmp(format->name, name) == 0)
2997 break;
3000 return format;
3004 * pevent_find_any_field - find any field by name
3005 * @event: handle for the event
3006 * @name: the name of the field
3008 * Returns a field by the given @name.
3009 * This searchs the common field names first, then
3010 * the non-common ones if a common one was not found.
3012 struct format_field *
3013 pevent_find_any_field(struct event_format *event, const char *name)
3015 struct format_field *format;
3017 format = pevent_find_common_field(event, name);
3018 if (format)
3019 return format;
3020 return pevent_find_field(event, name);
3024 * pevent_read_number - read a number from data
3025 * @pevent: handle for the pevent
3026 * @ptr: the raw data
3027 * @size: the size of the data that holds the number
3029 * Returns the number (converted to host) from the
3030 * raw data.
3032 unsigned long long pevent_read_number(struct pevent *pevent,
3033 const void *ptr, int size)
3035 switch (size) {
3036 case 1:
3037 return *(unsigned char *)ptr;
3038 case 2:
3039 return data2host2(pevent, ptr);
3040 case 4:
3041 return data2host4(pevent, ptr);
3042 case 8:
3043 return data2host8(pevent, ptr);
3044 default:
3045 /* BUG! */
3046 return 0;
3051 * pevent_read_number_field - read a number from data
3052 * @field: a handle to the field
3053 * @data: the raw data to read
3054 * @value: the value to place the number in
3056 * Reads raw data according to a field offset and size,
3057 * and translates it into @value.
3059 * Returns 0 on success, -1 otherwise.
3061 int pevent_read_number_field(struct format_field *field, const void *data,
3062 unsigned long long *value)
3064 if (!field)
3065 return -1;
3066 switch (field->size) {
3067 case 1:
3068 case 2:
3069 case 4:
3070 case 8:
3071 *value = pevent_read_number(field->event->pevent,
3072 data + field->offset, field->size);
3073 return 0;
3074 default:
3075 return -1;
3079 static int get_common_info(struct pevent *pevent,
3080 const char *type, int *offset, int *size)
3082 struct event_format *event;
3083 struct format_field *field;
3086 * All events should have the same common elements.
3087 * Pick any event to find where the type is;
3089 if (!pevent->events) {
3090 do_warning("no event_list!");
3091 return -1;
3094 event = pevent->events[0];
3095 field = pevent_find_common_field(event, type);
3096 if (!field)
3097 return -1;
3099 *offset = field->offset;
3100 *size = field->size;
3102 return 0;
3105 static int __parse_common(struct pevent *pevent, void *data,
3106 int *size, int *offset, const char *name)
3108 int ret;
3110 if (!*size) {
3111 ret = get_common_info(pevent, name, offset, size);
3112 if (ret < 0)
3113 return ret;
3115 return pevent_read_number(pevent, data + *offset, *size);
3118 static int trace_parse_common_type(struct pevent *pevent, void *data)
3120 return __parse_common(pevent, data,
3121 &pevent->type_size, &pevent->type_offset,
3122 "common_type");
3125 static int parse_common_pid(struct pevent *pevent, void *data)
3127 return __parse_common(pevent, data,
3128 &pevent->pid_size, &pevent->pid_offset,
3129 "common_pid");
3132 static int parse_common_pc(struct pevent *pevent, void *data)
3134 return __parse_common(pevent, data,
3135 &pevent->pc_size, &pevent->pc_offset,
3136 "common_preempt_count");
3139 static int parse_common_flags(struct pevent *pevent, void *data)
3141 return __parse_common(pevent, data,
3142 &pevent->flags_size, &pevent->flags_offset,
3143 "common_flags");
3146 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3148 return __parse_common(pevent, data,
3149 &pevent->ld_size, &pevent->ld_offset,
3150 "common_lock_depth");
3153 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3155 return __parse_common(pevent, data,
3156 &pevent->ld_size, &pevent->ld_offset,
3157 "common_migrate_disable");
3160 static int events_id_cmp(const void *a, const void *b);
3163 * pevent_find_event - find an event by given id
3164 * @pevent: a handle to the pevent
3165 * @id: the id of the event
3167 * Returns an event that has a given @id.
3169 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3171 struct event_format **eventptr;
3172 struct event_format key;
3173 struct event_format *pkey = &key;
3175 /* Check cache first */
3176 if (pevent->last_event && pevent->last_event->id == id)
3177 return pevent->last_event;
3179 key.id = id;
3181 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3182 sizeof(*pevent->events), events_id_cmp);
3184 if (eventptr) {
3185 pevent->last_event = *eventptr;
3186 return *eventptr;
3189 return NULL;
3193 * pevent_find_event_by_name - find an event by given name
3194 * @pevent: a handle to the pevent
3195 * @sys: the system name to search for
3196 * @name: the name of the event to search for
3198 * This returns an event with a given @name and under the system
3199 * @sys. If @sys is NULL the first event with @name is returned.
3201 struct event_format *
3202 pevent_find_event_by_name(struct pevent *pevent,
3203 const char *sys, const char *name)
3205 struct event_format *event;
3206 int i;
3208 if (pevent->last_event &&
3209 strcmp(pevent->last_event->name, name) == 0 &&
3210 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3211 return pevent->last_event;
3213 for (i = 0; i < pevent->nr_events; i++) {
3214 event = pevent->events[i];
3215 if (strcmp(event->name, name) == 0) {
3216 if (!sys)
3217 break;
3218 if (strcmp(event->system, sys) == 0)
3219 break;
3222 if (i == pevent->nr_events)
3223 event = NULL;
3225 pevent->last_event = event;
3226 return event;
3229 static unsigned long long
3230 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3232 struct pevent *pevent = event->pevent;
3233 unsigned long long val = 0;
3234 unsigned long long left, right;
3235 struct print_arg *typearg = NULL;
3236 struct print_arg *larg;
3237 unsigned long offset;
3238 unsigned int field_size;
3240 switch (arg->type) {
3241 case PRINT_NULL:
3242 /* ?? */
3243 return 0;
3244 case PRINT_ATOM:
3245 return strtoull(arg->atom.atom, NULL, 0);
3246 case PRINT_FIELD:
3247 if (!arg->field.field) {
3248 arg->field.field = pevent_find_any_field(event, arg->field.name);
3249 if (!arg->field.field)
3250 goto out_warning_field;
3253 /* must be a number */
3254 val = pevent_read_number(pevent, data + arg->field.field->offset,
3255 arg->field.field->size);
3256 break;
3257 case PRINT_FLAGS:
3258 case PRINT_SYMBOL:
3259 case PRINT_HEX:
3260 break;
3261 case PRINT_TYPE:
3262 val = eval_num_arg(data, size, event, arg->typecast.item);
3263 return eval_type(val, arg, 0);
3264 case PRINT_STRING:
3265 case PRINT_BSTRING:
3266 return 0;
3267 case PRINT_FUNC: {
3268 struct trace_seq s;
3269 trace_seq_init(&s);
3270 val = process_defined_func(&s, data, size, event, arg);
3271 trace_seq_destroy(&s);
3272 return val;
3274 case PRINT_OP:
3275 if (strcmp(arg->op.op, "[") == 0) {
3277 * Arrays are special, since we don't want
3278 * to read the arg as is.
3280 right = eval_num_arg(data, size, event, arg->op.right);
3282 /* handle typecasts */
3283 larg = arg->op.left;
3284 while (larg->type == PRINT_TYPE) {
3285 if (!typearg)
3286 typearg = larg;
3287 larg = larg->typecast.item;
3290 /* Default to long size */
3291 field_size = pevent->long_size;
3293 switch (larg->type) {
3294 case PRINT_DYNAMIC_ARRAY:
3295 offset = pevent_read_number(pevent,
3296 data + larg->dynarray.field->offset,
3297 larg->dynarray.field->size);
3298 if (larg->dynarray.field->elementsize)
3299 field_size = larg->dynarray.field->elementsize;
3301 * The actual length of the dynamic array is stored
3302 * in the top half of the field, and the offset
3303 * is in the bottom half of the 32 bit field.
3305 offset &= 0xffff;
3306 offset += right;
3307 break;
3308 case PRINT_FIELD:
3309 if (!larg->field.field) {
3310 larg->field.field =
3311 pevent_find_any_field(event, larg->field.name);
3312 if (!larg->field.field) {
3313 arg = larg;
3314 goto out_warning_field;
3317 field_size = larg->field.field->elementsize;
3318 offset = larg->field.field->offset +
3319 right * larg->field.field->elementsize;
3320 break;
3321 default:
3322 goto default_op; /* oops, all bets off */
3324 val = pevent_read_number(pevent,
3325 data + offset, field_size);
3326 if (typearg)
3327 val = eval_type(val, typearg, 1);
3328 break;
3329 } else if (strcmp(arg->op.op, "?") == 0) {
3330 left = eval_num_arg(data, size, event, arg->op.left);
3331 arg = arg->op.right;
3332 if (left)
3333 val = eval_num_arg(data, size, event, arg->op.left);
3334 else
3335 val = eval_num_arg(data, size, event, arg->op.right);
3336 break;
3338 default_op:
3339 left = eval_num_arg(data, size, event, arg->op.left);
3340 right = eval_num_arg(data, size, event, arg->op.right);
3341 switch (arg->op.op[0]) {
3342 case '!':
3343 switch (arg->op.op[1]) {
3344 case 0:
3345 val = !right;
3346 break;
3347 case '=':
3348 val = left != right;
3349 break;
3350 default:
3351 goto out_warning_op;
3353 break;
3354 case '~':
3355 val = ~right;
3356 break;
3357 case '|':
3358 if (arg->op.op[1])
3359 val = left || right;
3360 else
3361 val = left | right;
3362 break;
3363 case '&':
3364 if (arg->op.op[1])
3365 val = left && right;
3366 else
3367 val = left & right;
3368 break;
3369 case '<':
3370 switch (arg->op.op[1]) {
3371 case 0:
3372 val = left < right;
3373 break;
3374 case '<':
3375 val = left << right;
3376 break;
3377 case '=':
3378 val = left <= right;
3379 break;
3380 default:
3381 goto out_warning_op;
3383 break;
3384 case '>':
3385 switch (arg->op.op[1]) {
3386 case 0:
3387 val = left > right;
3388 break;
3389 case '>':
3390 val = left >> right;
3391 break;
3392 case '=':
3393 val = left >= right;
3394 break;
3395 default:
3396 goto out_warning_op;
3398 break;
3399 case '=':
3400 if (arg->op.op[1] != '=')
3401 goto out_warning_op;
3403 val = left == right;
3404 break;
3405 case '-':
3406 val = left - right;
3407 break;
3408 case '+':
3409 val = left + right;
3410 break;
3411 case '/':
3412 val = left / right;
3413 break;
3414 case '*':
3415 val = left * right;
3416 break;
3417 default:
3418 goto out_warning_op;
3420 break;
3421 default: /* not sure what to do there */
3422 return 0;
3424 return val;
3426 out_warning_op:
3427 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3428 return 0;
3430 out_warning_field:
3431 do_warning("%s: field %s not found", __func__, arg->field.name);
3432 return 0;
3435 struct flag {
3436 const char *name;
3437 unsigned long long value;
3440 static const struct flag flags[] = {
3441 { "HI_SOFTIRQ", 0 },
3442 { "TIMER_SOFTIRQ", 1 },
3443 { "NET_TX_SOFTIRQ", 2 },
3444 { "NET_RX_SOFTIRQ", 3 },
3445 { "BLOCK_SOFTIRQ", 4 },
3446 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3447 { "TASKLET_SOFTIRQ", 6 },
3448 { "SCHED_SOFTIRQ", 7 },
3449 { "HRTIMER_SOFTIRQ", 8 },
3450 { "RCU_SOFTIRQ", 9 },
3452 { "HRTIMER_NORESTART", 0 },
3453 { "HRTIMER_RESTART", 1 },
3456 static unsigned long long eval_flag(const char *flag)
3458 int i;
3461 * Some flags in the format files do not get converted.
3462 * If the flag is not numeric, see if it is something that
3463 * we already know about.
3465 if (isdigit(flag[0]))
3466 return strtoull(flag, NULL, 0);
3468 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3469 if (strcmp(flags[i].name, flag) == 0)
3470 return flags[i].value;
3472 return 0;
3475 static void print_str_to_seq(struct trace_seq *s, const char *format,
3476 int len_arg, const char *str)
3478 if (len_arg >= 0)
3479 trace_seq_printf(s, format, len_arg, str);
3480 else
3481 trace_seq_printf(s, format, str);
3484 static void print_str_arg(struct trace_seq *s, void *data, int size,
3485 struct event_format *event, const char *format,
3486 int len_arg, struct print_arg *arg)
3488 struct pevent *pevent = event->pevent;
3489 struct print_flag_sym *flag;
3490 struct format_field *field;
3491 unsigned long long val, fval;
3492 unsigned long addr;
3493 char *str;
3494 unsigned char *hex;
3495 int print;
3496 int i, len;
3498 switch (arg->type) {
3499 case PRINT_NULL:
3500 /* ?? */
3501 return;
3502 case PRINT_ATOM:
3503 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3504 return;
3505 case PRINT_FIELD:
3506 field = arg->field.field;
3507 if (!field) {
3508 field = pevent_find_any_field(event, arg->field.name);
3509 if (!field) {
3510 str = arg->field.name;
3511 goto out_warning_field;
3513 arg->field.field = field;
3515 /* Zero sized fields, mean the rest of the data */
3516 len = field->size ? : size - field->offset;
3519 * Some events pass in pointers. If this is not an array
3520 * and the size is the same as long_size, assume that it
3521 * is a pointer.
3523 if (!(field->flags & FIELD_IS_ARRAY) &&
3524 field->size == pevent->long_size) {
3525 addr = *(unsigned long *)(data + field->offset);
3526 trace_seq_printf(s, "%lx", addr);
3527 break;
3529 str = malloc(len + 1);
3530 if (!str) {
3531 do_warning("%s: not enough memory!", __func__);
3532 return;
3534 memcpy(str, data + field->offset, len);
3535 str[len] = 0;
3536 print_str_to_seq(s, format, len_arg, str);
3537 free(str);
3538 break;
3539 case PRINT_FLAGS:
3540 val = eval_num_arg(data, size, event, arg->flags.field);
3541 print = 0;
3542 for (flag = arg->flags.flags; flag; flag = flag->next) {
3543 fval = eval_flag(flag->value);
3544 if (!val && !fval) {
3545 print_str_to_seq(s, format, len_arg, flag->str);
3546 break;
3548 if (fval && (val & fval) == fval) {
3549 if (print && arg->flags.delim)
3550 trace_seq_puts(s, arg->flags.delim);
3551 print_str_to_seq(s, format, len_arg, flag->str);
3552 print = 1;
3553 val &= ~fval;
3556 break;
3557 case PRINT_SYMBOL:
3558 val = eval_num_arg(data, size, event, arg->symbol.field);
3559 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3560 fval = eval_flag(flag->value);
3561 if (val == fval) {
3562 print_str_to_seq(s, format, len_arg, flag->str);
3563 break;
3566 break;
3567 case PRINT_HEX:
3568 field = arg->hex.field->field.field;
3569 if (!field) {
3570 str = arg->hex.field->field.name;
3571 field = pevent_find_any_field(event, str);
3572 if (!field)
3573 goto out_warning_field;
3574 arg->hex.field->field.field = field;
3576 hex = data + field->offset;
3577 len = eval_num_arg(data, size, event, arg->hex.size);
3578 for (i = 0; i < len; i++) {
3579 if (i)
3580 trace_seq_putc(s, ' ');
3581 trace_seq_printf(s, "%02x", hex[i]);
3583 break;
3585 case PRINT_TYPE:
3586 break;
3587 case PRINT_STRING: {
3588 int str_offset;
3590 if (arg->string.offset == -1) {
3591 struct format_field *f;
3593 f = pevent_find_any_field(event, arg->string.string);
3594 arg->string.offset = f->offset;
3596 str_offset = data2host4(pevent, data + arg->string.offset);
3597 str_offset &= 0xffff;
3598 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3599 break;
3601 case PRINT_BSTRING:
3602 print_str_to_seq(s, format, len_arg, arg->string.string);
3603 break;
3604 case PRINT_OP:
3606 * The only op for string should be ? :
3608 if (arg->op.op[0] != '?')
3609 return;
3610 val = eval_num_arg(data, size, event, arg->op.left);
3611 if (val)
3612 print_str_arg(s, data, size, event,
3613 format, len_arg, arg->op.right->op.left);
3614 else
3615 print_str_arg(s, data, size, event,
3616 format, len_arg, arg->op.right->op.right);
3617 break;
3618 case PRINT_FUNC:
3619 process_defined_func(s, data, size, event, arg);
3620 break;
3621 default:
3622 /* well... */
3623 break;
3626 return;
3628 out_warning_field:
3629 do_warning("%s: field %s not found", __func__, arg->field.name);
3632 static unsigned long long
3633 process_defined_func(struct trace_seq *s, void *data, int size,
3634 struct event_format *event, struct print_arg *arg)
3636 struct pevent_function_handler *func_handle = arg->func.func;
3637 struct pevent_func_params *param;
3638 unsigned long long *args;
3639 unsigned long long ret;
3640 struct print_arg *farg;
3641 struct trace_seq str;
3642 struct save_str {
3643 struct save_str *next;
3644 char *str;
3645 } *strings = NULL, *string;
3646 int i;
3648 if (!func_handle->nr_args) {
3649 ret = (*func_handle->func)(s, NULL);
3650 goto out;
3653 farg = arg->func.args;
3654 param = func_handle->params;
3656 ret = ULLONG_MAX;
3657 args = malloc(sizeof(*args) * func_handle->nr_args);
3658 if (!args)
3659 goto out;
3661 for (i = 0; i < func_handle->nr_args; i++) {
3662 switch (param->type) {
3663 case PEVENT_FUNC_ARG_INT:
3664 case PEVENT_FUNC_ARG_LONG:
3665 case PEVENT_FUNC_ARG_PTR:
3666 args[i] = eval_num_arg(data, size, event, farg);
3667 break;
3668 case PEVENT_FUNC_ARG_STRING:
3669 trace_seq_init(&str);
3670 print_str_arg(&str, data, size, event, "%s", -1, farg);
3671 trace_seq_terminate(&str);
3672 string = malloc(sizeof(*string));
3673 if (!string) {
3674 do_warning("%s(%d): malloc str", __func__, __LINE__);
3675 goto out_free;
3677 string->next = strings;
3678 string->str = strdup(str.buffer);
3679 if (!string->str) {
3680 free(string);
3681 do_warning("%s(%d): malloc str", __func__, __LINE__);
3682 goto out_free;
3684 args[i] = (uintptr_t)string->str;
3685 strings = string;
3686 trace_seq_destroy(&str);
3687 break;
3688 default:
3690 * Something went totally wrong, this is not
3691 * an input error, something in this code broke.
3693 do_warning("Unexpected end of arguments\n");
3694 goto out_free;
3696 farg = farg->next;
3697 param = param->next;
3700 ret = (*func_handle->func)(s, args);
3701 out_free:
3702 free(args);
3703 while (strings) {
3704 string = strings;
3705 strings = string->next;
3706 free(string->str);
3707 free(string);
3710 out:
3711 /* TBD : handle return type here */
3712 return ret;
3715 static void free_args(struct print_arg *args)
3717 struct print_arg *next;
3719 while (args) {
3720 next = args->next;
3722 free_arg(args);
3723 args = next;
3727 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3729 struct pevent *pevent = event->pevent;
3730 struct format_field *field, *ip_field;
3731 struct print_arg *args, *arg, **next;
3732 unsigned long long ip, val;
3733 char *ptr;
3734 void *bptr;
3735 int vsize;
3737 field = pevent->bprint_buf_field;
3738 ip_field = pevent->bprint_ip_field;
3740 if (!field) {
3741 field = pevent_find_field(event, "buf");
3742 if (!field) {
3743 do_warning("can't find buffer field for binary printk");
3744 return NULL;
3746 ip_field = pevent_find_field(event, "ip");
3747 if (!ip_field) {
3748 do_warning("can't find ip field for binary printk");
3749 return NULL;
3751 pevent->bprint_buf_field = field;
3752 pevent->bprint_ip_field = ip_field;
3755 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3758 * The first arg is the IP pointer.
3760 args = alloc_arg();
3761 if (!args) {
3762 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3763 return NULL;
3765 arg = args;
3766 arg->next = NULL;
3767 next = &arg->next;
3769 arg->type = PRINT_ATOM;
3771 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3772 goto out_free;
3774 /* skip the first "%pf : " */
3775 for (ptr = fmt + 6, bptr = data + field->offset;
3776 bptr < data + size && *ptr; ptr++) {
3777 int ls = 0;
3779 if (*ptr == '%') {
3780 process_again:
3781 ptr++;
3782 switch (*ptr) {
3783 case '%':
3784 break;
3785 case 'l':
3786 ls++;
3787 goto process_again;
3788 case 'L':
3789 ls = 2;
3790 goto process_again;
3791 case '0' ... '9':
3792 goto process_again;
3793 case '.':
3794 goto process_again;
3795 case 'p':
3796 ls = 1;
3797 /* fall through */
3798 case 'd':
3799 case 'u':
3800 case 'x':
3801 case 'i':
3802 switch (ls) {
3803 case 0:
3804 vsize = 4;
3805 break;
3806 case 1:
3807 vsize = pevent->long_size;
3808 break;
3809 case 2:
3810 vsize = 8;
3811 break;
3812 default:
3813 vsize = ls; /* ? */
3814 break;
3816 /* fall through */
3817 case '*':
3818 if (*ptr == '*')
3819 vsize = 4;
3821 /* the pointers are always 4 bytes aligned */
3822 bptr = (void *)(((unsigned long)bptr + 3) &
3823 ~3);
3824 val = pevent_read_number(pevent, bptr, vsize);
3825 bptr += vsize;
3826 arg = alloc_arg();
3827 if (!arg) {
3828 do_warning("%s(%d): not enough memory!",
3829 __func__, __LINE__);
3830 goto out_free;
3832 arg->next = NULL;
3833 arg->type = PRINT_ATOM;
3834 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3835 free(arg);
3836 goto out_free;
3838 *next = arg;
3839 next = &arg->next;
3841 * The '*' case means that an arg is used as the length.
3842 * We need to continue to figure out for what.
3844 if (*ptr == '*')
3845 goto process_again;
3847 break;
3848 case 's':
3849 arg = alloc_arg();
3850 if (!arg) {
3851 do_warning("%s(%d): not enough memory!",
3852 __func__, __LINE__);
3853 goto out_free;
3855 arg->next = NULL;
3856 arg->type = PRINT_BSTRING;
3857 arg->string.string = strdup(bptr);
3858 if (!arg->string.string)
3859 goto out_free;
3860 bptr += strlen(bptr) + 1;
3861 *next = arg;
3862 next = &arg->next;
3863 default:
3864 break;
3869 return args;
3871 out_free:
3872 free_args(args);
3873 return NULL;
3876 static char *
3877 get_bprint_format(void *data, int size __maybe_unused,
3878 struct event_format *event)
3880 struct pevent *pevent = event->pevent;
3881 unsigned long long addr;
3882 struct format_field *field;
3883 struct printk_map *printk;
3884 char *format;
3885 char *p;
3887 field = pevent->bprint_fmt_field;
3889 if (!field) {
3890 field = pevent_find_field(event, "fmt");
3891 if (!field) {
3892 do_warning("can't find format field for binary printk");
3893 return NULL;
3895 pevent->bprint_fmt_field = field;
3898 addr = pevent_read_number(pevent, data + field->offset, field->size);
3900 printk = find_printk(pevent, addr);
3901 if (!printk) {
3902 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3903 return NULL;
3904 return format;
3907 p = printk->printk;
3908 /* Remove any quotes. */
3909 if (*p == '"')
3910 p++;
3911 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3912 return NULL;
3913 /* remove ending quotes and new line since we will add one too */
3914 p = format + strlen(format) - 1;
3915 if (*p == '"')
3916 *p = 0;
3918 p -= 2;
3919 if (strcmp(p, "\\n") == 0)
3920 *p = 0;
3922 return format;
3925 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3926 struct event_format *event, struct print_arg *arg)
3928 unsigned char *buf;
3929 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3931 if (arg->type == PRINT_FUNC) {
3932 process_defined_func(s, data, size, event, arg);
3933 return;
3936 if (arg->type != PRINT_FIELD) {
3937 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3938 arg->type);
3939 return;
3942 if (mac == 'm')
3943 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3944 if (!arg->field.field) {
3945 arg->field.field =
3946 pevent_find_any_field(event, arg->field.name);
3947 if (!arg->field.field) {
3948 do_warning("%s: field %s not found",
3949 __func__, arg->field.name);
3950 return;
3953 if (arg->field.field->size != 6) {
3954 trace_seq_printf(s, "INVALIDMAC");
3955 return;
3957 buf = data + arg->field.field->offset;
3958 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3961 static int is_printable_array(char *p, unsigned int len)
3963 unsigned int i;
3965 for (i = 0; i < len && p[i]; i++)
3966 if (!isprint(p[i]))
3967 return 0;
3968 return 1;
3971 static void print_event_fields(struct trace_seq *s, void *data,
3972 int size __maybe_unused,
3973 struct event_format *event)
3975 struct format_field *field;
3976 unsigned long long val;
3977 unsigned int offset, len, i;
3979 field = event->format.fields;
3980 while (field) {
3981 trace_seq_printf(s, " %s=", field->name);
3982 if (field->flags & FIELD_IS_ARRAY) {
3983 offset = field->offset;
3984 len = field->size;
3985 if (field->flags & FIELD_IS_DYNAMIC) {
3986 val = pevent_read_number(event->pevent, data + offset, len);
3987 offset = val;
3988 len = offset >> 16;
3989 offset &= 0xffff;
3991 if (field->flags & FIELD_IS_STRING &&
3992 is_printable_array(data + offset, len)) {
3993 trace_seq_printf(s, "%s", (char *)data + offset);
3994 } else {
3995 trace_seq_puts(s, "ARRAY[");
3996 for (i = 0; i < len; i++) {
3997 if (i)
3998 trace_seq_puts(s, ", ");
3999 trace_seq_printf(s, "%02x",
4000 *((unsigned char *)data + offset + i));
4002 trace_seq_putc(s, ']');
4003 field->flags &= ~FIELD_IS_STRING;
4005 } else {
4006 val = pevent_read_number(event->pevent, data + field->offset,
4007 field->size);
4008 if (field->flags & FIELD_IS_POINTER) {
4009 trace_seq_printf(s, "0x%llx", val);
4010 } else if (field->flags & FIELD_IS_SIGNED) {
4011 switch (field->size) {
4012 case 4:
4014 * If field is long then print it in hex.
4015 * A long usually stores pointers.
4017 if (field->flags & FIELD_IS_LONG)
4018 trace_seq_printf(s, "0x%x", (int)val);
4019 else
4020 trace_seq_printf(s, "%d", (int)val);
4021 break;
4022 case 2:
4023 trace_seq_printf(s, "%2d", (short)val);
4024 break;
4025 case 1:
4026 trace_seq_printf(s, "%1d", (char)val);
4027 break;
4028 default:
4029 trace_seq_printf(s, "%lld", val);
4031 } else {
4032 if (field->flags & FIELD_IS_LONG)
4033 trace_seq_printf(s, "0x%llx", val);
4034 else
4035 trace_seq_printf(s, "%llu", val);
4038 field = field->next;
4042 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4044 struct pevent *pevent = event->pevent;
4045 struct print_fmt *print_fmt = &event->print_fmt;
4046 struct print_arg *arg = print_fmt->args;
4047 struct print_arg *args = NULL;
4048 const char *ptr = print_fmt->format;
4049 unsigned long long val;
4050 struct func_map *func;
4051 const char *saveptr;
4052 char *bprint_fmt = NULL;
4053 char format[32];
4054 int show_func;
4055 int len_as_arg;
4056 int len_arg;
4057 int len;
4058 int ls;
4060 if (event->flags & EVENT_FL_FAILED) {
4061 trace_seq_printf(s, "[FAILED TO PARSE]");
4062 print_event_fields(s, data, size, event);
4063 return;
4066 if (event->flags & EVENT_FL_ISBPRINT) {
4067 bprint_fmt = get_bprint_format(data, size, event);
4068 args = make_bprint_args(bprint_fmt, data, size, event);
4069 arg = args;
4070 ptr = bprint_fmt;
4073 for (; *ptr; ptr++) {
4074 ls = 0;
4075 if (*ptr == '\\') {
4076 ptr++;
4077 switch (*ptr) {
4078 case 'n':
4079 trace_seq_putc(s, '\n');
4080 break;
4081 case 't':
4082 trace_seq_putc(s, '\t');
4083 break;
4084 case 'r':
4085 trace_seq_putc(s, '\r');
4086 break;
4087 case '\\':
4088 trace_seq_putc(s, '\\');
4089 break;
4090 default:
4091 trace_seq_putc(s, *ptr);
4092 break;
4095 } else if (*ptr == '%') {
4096 saveptr = ptr;
4097 show_func = 0;
4098 len_as_arg = 0;
4099 cont_process:
4100 ptr++;
4101 switch (*ptr) {
4102 case '%':
4103 trace_seq_putc(s, '%');
4104 break;
4105 case '#':
4106 /* FIXME: need to handle properly */
4107 goto cont_process;
4108 case 'h':
4109 ls--;
4110 goto cont_process;
4111 case 'l':
4112 ls++;
4113 goto cont_process;
4114 case 'L':
4115 ls = 2;
4116 goto cont_process;
4117 case '*':
4118 /* The argument is the length. */
4119 if (!arg) {
4120 do_warning("no argument match");
4121 event->flags |= EVENT_FL_FAILED;
4122 goto out_failed;
4124 len_arg = eval_num_arg(data, size, event, arg);
4125 len_as_arg = 1;
4126 arg = arg->next;
4127 goto cont_process;
4128 case '.':
4129 case 'z':
4130 case 'Z':
4131 case '0' ... '9':
4132 goto cont_process;
4133 case 'p':
4134 if (pevent->long_size == 4)
4135 ls = 1;
4136 else
4137 ls = 2;
4139 if (*(ptr+1) == 'F' ||
4140 *(ptr+1) == 'f') {
4141 ptr++;
4142 show_func = *ptr;
4143 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4144 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4145 ptr++;
4146 arg = arg->next;
4147 break;
4150 /* fall through */
4151 case 'd':
4152 case 'i':
4153 case 'x':
4154 case 'X':
4155 case 'u':
4156 if (!arg) {
4157 do_warning("no argument match");
4158 event->flags |= EVENT_FL_FAILED;
4159 goto out_failed;
4162 len = ((unsigned long)ptr + 1) -
4163 (unsigned long)saveptr;
4165 /* should never happen */
4166 if (len > 31) {
4167 do_warning("bad format!");
4168 event->flags |= EVENT_FL_FAILED;
4169 len = 31;
4172 memcpy(format, saveptr, len);
4173 format[len] = 0;
4175 val = eval_num_arg(data, size, event, arg);
4176 arg = arg->next;
4178 if (show_func) {
4179 func = find_func(pevent, val);
4180 if (func) {
4181 trace_seq_puts(s, func->func);
4182 if (show_func == 'F')
4183 trace_seq_printf(s,
4184 "+0x%llx",
4185 val - func->addr);
4186 break;
4189 if (pevent->long_size == 8 && ls &&
4190 sizeof(long) != 8) {
4191 char *p;
4193 ls = 2;
4194 /* make %l into %ll */
4195 p = strchr(format, 'l');
4196 if (p)
4197 memmove(p+1, p, strlen(p)+1);
4198 else if (strcmp(format, "%p") == 0)
4199 strcpy(format, "0x%llx");
4201 switch (ls) {
4202 case -2:
4203 if (len_as_arg)
4204 trace_seq_printf(s, format, len_arg, (char)val);
4205 else
4206 trace_seq_printf(s, format, (char)val);
4207 break;
4208 case -1:
4209 if (len_as_arg)
4210 trace_seq_printf(s, format, len_arg, (short)val);
4211 else
4212 trace_seq_printf(s, format, (short)val);
4213 break;
4214 case 0:
4215 if (len_as_arg)
4216 trace_seq_printf(s, format, len_arg, (int)val);
4217 else
4218 trace_seq_printf(s, format, (int)val);
4219 break;
4220 case 1:
4221 if (len_as_arg)
4222 trace_seq_printf(s, format, len_arg, (long)val);
4223 else
4224 trace_seq_printf(s, format, (long)val);
4225 break;
4226 case 2:
4227 if (len_as_arg)
4228 trace_seq_printf(s, format, len_arg,
4229 (long long)val);
4230 else
4231 trace_seq_printf(s, format, (long long)val);
4232 break;
4233 default:
4234 do_warning("bad count (%d)", ls);
4235 event->flags |= EVENT_FL_FAILED;
4237 break;
4238 case 's':
4239 if (!arg) {
4240 do_warning("no matching argument");
4241 event->flags |= EVENT_FL_FAILED;
4242 goto out_failed;
4245 len = ((unsigned long)ptr + 1) -
4246 (unsigned long)saveptr;
4248 /* should never happen */
4249 if (len > 31) {
4250 do_warning("bad format!");
4251 event->flags |= EVENT_FL_FAILED;
4252 len = 31;
4255 memcpy(format, saveptr, len);
4256 format[len] = 0;
4257 if (!len_as_arg)
4258 len_arg = -1;
4259 print_str_arg(s, data, size, event,
4260 format, len_arg, arg);
4261 arg = arg->next;
4262 break;
4263 default:
4264 trace_seq_printf(s, ">%c<", *ptr);
4267 } else
4268 trace_seq_putc(s, *ptr);
4271 if (event->flags & EVENT_FL_FAILED) {
4272 out_failed:
4273 trace_seq_printf(s, "[FAILED TO PARSE]");
4276 if (args) {
4277 free_args(args);
4278 free(bprint_fmt);
4283 * pevent_data_lat_fmt - parse the data for the latency format
4284 * @pevent: a handle to the pevent
4285 * @s: the trace_seq to write to
4286 * @record: the record to read from
4288 * This parses out the Latency format (interrupts disabled,
4289 * need rescheduling, in hard/soft interrupt, preempt count
4290 * and lock depth) and places it into the trace_seq.
4292 void pevent_data_lat_fmt(struct pevent *pevent,
4293 struct trace_seq *s, struct pevent_record *record)
4295 static int check_lock_depth = 1;
4296 static int check_migrate_disable = 1;
4297 static int lock_depth_exists;
4298 static int migrate_disable_exists;
4299 unsigned int lat_flags;
4300 unsigned int pc;
4301 int lock_depth;
4302 int migrate_disable;
4303 int hardirq;
4304 int softirq;
4305 void *data = record->data;
4307 lat_flags = parse_common_flags(pevent, data);
4308 pc = parse_common_pc(pevent, data);
4309 /* lock_depth may not always exist */
4310 if (lock_depth_exists)
4311 lock_depth = parse_common_lock_depth(pevent, data);
4312 else if (check_lock_depth) {
4313 lock_depth = parse_common_lock_depth(pevent, data);
4314 if (lock_depth < 0)
4315 check_lock_depth = 0;
4316 else
4317 lock_depth_exists = 1;
4320 /* migrate_disable may not always exist */
4321 if (migrate_disable_exists)
4322 migrate_disable = parse_common_migrate_disable(pevent, data);
4323 else if (check_migrate_disable) {
4324 migrate_disable = parse_common_migrate_disable(pevent, data);
4325 if (migrate_disable < 0)
4326 check_migrate_disable = 0;
4327 else
4328 migrate_disable_exists = 1;
4331 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4332 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4334 trace_seq_printf(s, "%c%c%c",
4335 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4336 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4337 'X' : '.',
4338 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4339 'N' : '.',
4340 (hardirq && softirq) ? 'H' :
4341 hardirq ? 'h' : softirq ? 's' : '.');
4343 if (pc)
4344 trace_seq_printf(s, "%x", pc);
4345 else
4346 trace_seq_putc(s, '.');
4348 if (migrate_disable_exists) {
4349 if (migrate_disable < 0)
4350 trace_seq_putc(s, '.');
4351 else
4352 trace_seq_printf(s, "%d", migrate_disable);
4355 if (lock_depth_exists) {
4356 if (lock_depth < 0)
4357 trace_seq_putc(s, '.');
4358 else
4359 trace_seq_printf(s, "%d", lock_depth);
4362 trace_seq_terminate(s);
4366 * pevent_data_type - parse out the given event type
4367 * @pevent: a handle to the pevent
4368 * @rec: the record to read from
4370 * This returns the event id from the @rec.
4372 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4374 return trace_parse_common_type(pevent, rec->data);
4378 * pevent_data_event_from_type - find the event by a given type
4379 * @pevent: a handle to the pevent
4380 * @type: the type of the event.
4382 * This returns the event form a given @type;
4384 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4386 return pevent_find_event(pevent, type);
4390 * pevent_data_pid - parse the PID from raw data
4391 * @pevent: a handle to the pevent
4392 * @rec: the record to parse
4394 * This returns the PID from a raw data.
4396 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4398 return parse_common_pid(pevent, rec->data);
4402 * pevent_data_comm_from_pid - return the command line from PID
4403 * @pevent: a handle to the pevent
4404 * @pid: the PID of the task to search for
4406 * This returns a pointer to the command line that has the given
4407 * @pid.
4409 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4411 const char *comm;
4413 comm = find_cmdline(pevent, pid);
4414 return comm;
4418 * pevent_data_comm_from_pid - parse the data into the print format
4419 * @s: the trace_seq to write to
4420 * @event: the handle to the event
4421 * @record: the record to read from
4423 * This parses the raw @data using the given @event information and
4424 * writes the print format into the trace_seq.
4426 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4427 struct pevent_record *record)
4429 int print_pretty = 1;
4431 if (event->pevent->print_raw)
4432 print_event_fields(s, record->data, record->size, event);
4433 else {
4435 if (event->handler)
4436 print_pretty = event->handler(s, record, event,
4437 event->context);
4439 if (print_pretty)
4440 pretty_print(s, record->data, record->size, event);
4443 trace_seq_terminate(s);
4446 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4447 struct pevent_record *record)
4449 static const char *spaces = " "; /* 20 spaces */
4450 struct event_format *event;
4451 unsigned long secs;
4452 unsigned long usecs;
4453 unsigned long nsecs;
4454 const char *comm;
4455 void *data = record->data;
4456 int type;
4457 int pid;
4458 int len;
4459 int p;
4461 secs = record->ts / NSECS_PER_SEC;
4462 nsecs = record->ts - secs * NSECS_PER_SEC;
4464 if (record->size < 0) {
4465 do_warning("ug! negative record size %d", record->size);
4466 return;
4469 type = trace_parse_common_type(pevent, data);
4471 event = pevent_find_event(pevent, type);
4472 if (!event) {
4473 do_warning("ug! no event found for type %d", type);
4474 return;
4477 pid = parse_common_pid(pevent, data);
4478 comm = find_cmdline(pevent, pid);
4480 if (pevent->latency_format) {
4481 trace_seq_printf(s, "%8.8s-%-5d %3d",
4482 comm, pid, record->cpu);
4483 pevent_data_lat_fmt(pevent, s, record);
4484 } else
4485 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4487 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4488 usecs = nsecs;
4489 p = 9;
4490 } else {
4491 usecs = (nsecs + 500) / NSECS_PER_USEC;
4492 p = 6;
4495 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4497 /* Space out the event names evenly. */
4498 len = strlen(event->name);
4499 if (len < 20)
4500 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4502 pevent_event_info(s, event, record);
4505 static int events_id_cmp(const void *a, const void *b)
4507 struct event_format * const * ea = a;
4508 struct event_format * const * eb = b;
4510 if ((*ea)->id < (*eb)->id)
4511 return -1;
4513 if ((*ea)->id > (*eb)->id)
4514 return 1;
4516 return 0;
4519 static int events_name_cmp(const void *a, const void *b)
4521 struct event_format * const * ea = a;
4522 struct event_format * const * eb = b;
4523 int res;
4525 res = strcmp((*ea)->name, (*eb)->name);
4526 if (res)
4527 return res;
4529 res = strcmp((*ea)->system, (*eb)->system);
4530 if (res)
4531 return res;
4533 return events_id_cmp(a, b);
4536 static int events_system_cmp(const void *a, const void *b)
4538 struct event_format * const * ea = a;
4539 struct event_format * const * eb = b;
4540 int res;
4542 res = strcmp((*ea)->system, (*eb)->system);
4543 if (res)
4544 return res;
4546 res = strcmp((*ea)->name, (*eb)->name);
4547 if (res)
4548 return res;
4550 return events_id_cmp(a, b);
4553 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4555 struct event_format **events;
4556 int (*sort)(const void *a, const void *b);
4558 events = pevent->sort_events;
4560 if (events && pevent->last_type == sort_type)
4561 return events;
4563 if (!events) {
4564 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4565 if (!events)
4566 return NULL;
4568 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4569 events[pevent->nr_events] = NULL;
4571 pevent->sort_events = events;
4573 /* the internal events are sorted by id */
4574 if (sort_type == EVENT_SORT_ID) {
4575 pevent->last_type = sort_type;
4576 return events;
4580 switch (sort_type) {
4581 case EVENT_SORT_ID:
4582 sort = events_id_cmp;
4583 break;
4584 case EVENT_SORT_NAME:
4585 sort = events_name_cmp;
4586 break;
4587 case EVENT_SORT_SYSTEM:
4588 sort = events_system_cmp;
4589 break;
4590 default:
4591 return events;
4594 qsort(events, pevent->nr_events, sizeof(*events), sort);
4595 pevent->last_type = sort_type;
4597 return events;
4600 static struct format_field **
4601 get_event_fields(const char *type, const char *name,
4602 int count, struct format_field *list)
4604 struct format_field **fields;
4605 struct format_field *field;
4606 int i = 0;
4608 fields = malloc(sizeof(*fields) * (count + 1));
4609 if (!fields)
4610 return NULL;
4612 for (field = list; field; field = field->next) {
4613 fields[i++] = field;
4614 if (i == count + 1) {
4615 do_warning("event %s has more %s fields than specified",
4616 name, type);
4617 i--;
4618 break;
4622 if (i != count)
4623 do_warning("event %s has less %s fields than specified",
4624 name, type);
4626 fields[i] = NULL;
4628 return fields;
4632 * pevent_event_common_fields - return a list of common fields for an event
4633 * @event: the event to return the common fields of.
4635 * Returns an allocated array of fields. The last item in the array is NULL.
4636 * The array must be freed with free().
4638 struct format_field **pevent_event_common_fields(struct event_format *event)
4640 return get_event_fields("common", event->name,
4641 event->format.nr_common,
4642 event->format.common_fields);
4646 * pevent_event_fields - return a list of event specific fields for an event
4647 * @event: the event to return the fields of.
4649 * Returns an allocated array of fields. The last item in the array is NULL.
4650 * The array must be freed with free().
4652 struct format_field **pevent_event_fields(struct event_format *event)
4654 return get_event_fields("event", event->name,
4655 event->format.nr_fields,
4656 event->format.fields);
4659 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4661 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4662 if (field->next) {
4663 trace_seq_puts(s, ", ");
4664 print_fields(s, field->next);
4668 /* for debugging */
4669 static void print_args(struct print_arg *args)
4671 int print_paren = 1;
4672 struct trace_seq s;
4674 switch (args->type) {
4675 case PRINT_NULL:
4676 printf("null");
4677 break;
4678 case PRINT_ATOM:
4679 printf("%s", args->atom.atom);
4680 break;
4681 case PRINT_FIELD:
4682 printf("REC->%s", args->field.name);
4683 break;
4684 case PRINT_FLAGS:
4685 printf("__print_flags(");
4686 print_args(args->flags.field);
4687 printf(", %s, ", args->flags.delim);
4688 trace_seq_init(&s);
4689 print_fields(&s, args->flags.flags);
4690 trace_seq_do_printf(&s);
4691 trace_seq_destroy(&s);
4692 printf(")");
4693 break;
4694 case PRINT_SYMBOL:
4695 printf("__print_symbolic(");
4696 print_args(args->symbol.field);
4697 printf(", ");
4698 trace_seq_init(&s);
4699 print_fields(&s, args->symbol.symbols);
4700 trace_seq_do_printf(&s);
4701 trace_seq_destroy(&s);
4702 printf(")");
4703 break;
4704 case PRINT_HEX:
4705 printf("__print_hex(");
4706 print_args(args->hex.field);
4707 printf(", ");
4708 print_args(args->hex.size);
4709 printf(")");
4710 break;
4711 case PRINT_STRING:
4712 case PRINT_BSTRING:
4713 printf("__get_str(%s)", args->string.string);
4714 break;
4715 case PRINT_TYPE:
4716 printf("(%s)", args->typecast.type);
4717 print_args(args->typecast.item);
4718 break;
4719 case PRINT_OP:
4720 if (strcmp(args->op.op, ":") == 0)
4721 print_paren = 0;
4722 if (print_paren)
4723 printf("(");
4724 print_args(args->op.left);
4725 printf(" %s ", args->op.op);
4726 print_args(args->op.right);
4727 if (print_paren)
4728 printf(")");
4729 break;
4730 default:
4731 /* we should warn... */
4732 return;
4734 if (args->next) {
4735 printf("\n");
4736 print_args(args->next);
4740 static void parse_header_field(const char *field,
4741 int *offset, int *size, int mandatory)
4743 unsigned long long save_input_buf_ptr;
4744 unsigned long long save_input_buf_siz;
4745 char *token;
4746 int type;
4748 save_input_buf_ptr = input_buf_ptr;
4749 save_input_buf_siz = input_buf_siz;
4751 if (read_expected(EVENT_ITEM, "field") < 0)
4752 return;
4753 if (read_expected(EVENT_OP, ":") < 0)
4754 return;
4756 /* type */
4757 if (read_expect_type(EVENT_ITEM, &token) < 0)
4758 goto fail;
4759 free_token(token);
4762 * If this is not a mandatory field, then test it first.
4764 if (mandatory) {
4765 if (read_expected(EVENT_ITEM, field) < 0)
4766 return;
4767 } else {
4768 if (read_expect_type(EVENT_ITEM, &token) < 0)
4769 goto fail;
4770 if (strcmp(token, field) != 0)
4771 goto discard;
4772 free_token(token);
4775 if (read_expected(EVENT_OP, ";") < 0)
4776 return;
4777 if (read_expected(EVENT_ITEM, "offset") < 0)
4778 return;
4779 if (read_expected(EVENT_OP, ":") < 0)
4780 return;
4781 if (read_expect_type(EVENT_ITEM, &token) < 0)
4782 goto fail;
4783 *offset = atoi(token);
4784 free_token(token);
4785 if (read_expected(EVENT_OP, ";") < 0)
4786 return;
4787 if (read_expected(EVENT_ITEM, "size") < 0)
4788 return;
4789 if (read_expected(EVENT_OP, ":") < 0)
4790 return;
4791 if (read_expect_type(EVENT_ITEM, &token) < 0)
4792 goto fail;
4793 *size = atoi(token);
4794 free_token(token);
4795 if (read_expected(EVENT_OP, ";") < 0)
4796 return;
4797 type = read_token(&token);
4798 if (type != EVENT_NEWLINE) {
4799 /* newer versions of the kernel have a "signed" type */
4800 if (type != EVENT_ITEM)
4801 goto fail;
4803 if (strcmp(token, "signed") != 0)
4804 goto fail;
4806 free_token(token);
4808 if (read_expected(EVENT_OP, ":") < 0)
4809 return;
4811 if (read_expect_type(EVENT_ITEM, &token))
4812 goto fail;
4814 free_token(token);
4815 if (read_expected(EVENT_OP, ";") < 0)
4816 return;
4818 if (read_expect_type(EVENT_NEWLINE, &token))
4819 goto fail;
4821 fail:
4822 free_token(token);
4823 return;
4825 discard:
4826 input_buf_ptr = save_input_buf_ptr;
4827 input_buf_siz = save_input_buf_siz;
4828 *offset = 0;
4829 *size = 0;
4830 free_token(token);
4834 * pevent_parse_header_page - parse the data stored in the header page
4835 * @pevent: the handle to the pevent
4836 * @buf: the buffer storing the header page format string
4837 * @size: the size of @buf
4838 * @long_size: the long size to use if there is no header
4840 * This parses the header page format for information on the
4841 * ring buffer used. The @buf should be copied from
4843 * /sys/kernel/debug/tracing/events/header_page
4845 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4846 int long_size)
4848 int ignore;
4850 if (!size) {
4852 * Old kernels did not have header page info.
4853 * Sorry but we just use what we find here in user space.
4855 pevent->header_page_ts_size = sizeof(long long);
4856 pevent->header_page_size_size = long_size;
4857 pevent->header_page_data_offset = sizeof(long long) + long_size;
4858 pevent->old_format = 1;
4859 return -1;
4861 init_input_buf(buf, size);
4863 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4864 &pevent->header_page_ts_size, 1);
4865 parse_header_field("commit", &pevent->header_page_size_offset,
4866 &pevent->header_page_size_size, 1);
4867 parse_header_field("overwrite", &pevent->header_page_overwrite,
4868 &ignore, 0);
4869 parse_header_field("data", &pevent->header_page_data_offset,
4870 &pevent->header_page_data_size, 1);
4872 return 0;
4875 static int event_matches(struct event_format *event,
4876 int id, const char *sys_name,
4877 const char *event_name)
4879 if (id >= 0 && id != event->id)
4880 return 0;
4882 if (event_name && (strcmp(event_name, event->name) != 0))
4883 return 0;
4885 if (sys_name && (strcmp(sys_name, event->system) != 0))
4886 return 0;
4888 return 1;
4891 static void free_handler(struct event_handler *handle)
4893 free((void *)handle->sys_name);
4894 free((void *)handle->event_name);
4895 free(handle);
4898 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4900 struct event_handler *handle, **next;
4902 for (next = &pevent->handlers; *next;
4903 next = &(*next)->next) {
4904 handle = *next;
4905 if (event_matches(event, handle->id,
4906 handle->sys_name,
4907 handle->event_name))
4908 break;
4911 if (!(*next))
4912 return 0;
4914 pr_stat("overriding event (%d) %s:%s with new print handler",
4915 event->id, event->system, event->name);
4917 event->handler = handle->func;
4918 event->context = handle->context;
4920 *next = handle->next;
4921 free_handler(handle);
4923 return 1;
4927 * __pevent_parse_format - parse the event format
4928 * @buf: the buffer storing the event format string
4929 * @size: the size of @buf
4930 * @sys: the system the event belongs to
4932 * This parses the event format and creates an event structure
4933 * to quickly parse raw data for a given event.
4935 * These files currently come from:
4937 * /sys/kernel/debug/tracing/events/.../.../format
4939 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4940 struct pevent *pevent, const char *buf,
4941 unsigned long size, const char *sys)
4943 struct event_format *event;
4944 int ret;
4946 init_input_buf(buf, size);
4948 *eventp = event = alloc_event();
4949 if (!event)
4950 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4952 event->name = event_read_name();
4953 if (!event->name) {
4954 /* Bad event? */
4955 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4956 goto event_alloc_failed;
4959 if (strcmp(sys, "ftrace") == 0) {
4960 event->flags |= EVENT_FL_ISFTRACE;
4962 if (strcmp(event->name, "bprint") == 0)
4963 event->flags |= EVENT_FL_ISBPRINT;
4966 event->id = event_read_id();
4967 if (event->id < 0) {
4968 ret = PEVENT_ERRNO__READ_ID_FAILED;
4970 * This isn't an allocation error actually.
4971 * But as the ID is critical, just bail out.
4973 goto event_alloc_failed;
4976 event->system = strdup(sys);
4977 if (!event->system) {
4978 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4979 goto event_alloc_failed;
4982 /* Add pevent to event so that it can be referenced */
4983 event->pevent = pevent;
4985 ret = event_read_format(event);
4986 if (ret < 0) {
4987 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4988 goto event_parse_failed;
4992 * If the event has an override, don't print warnings if the event
4993 * print format fails to parse.
4995 if (pevent && find_event_handle(pevent, event))
4996 show_warning = 0;
4998 ret = event_read_print(event);
4999 show_warning = 1;
5001 if (ret < 0) {
5002 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5003 goto event_parse_failed;
5006 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5007 struct format_field *field;
5008 struct print_arg *arg, **list;
5010 /* old ftrace had no args */
5011 list = &event->print_fmt.args;
5012 for (field = event->format.fields; field; field = field->next) {
5013 arg = alloc_arg();
5014 if (!arg) {
5015 event->flags |= EVENT_FL_FAILED;
5016 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5018 arg->type = PRINT_FIELD;
5019 arg->field.name = strdup(field->name);
5020 if (!arg->field.name) {
5021 event->flags |= EVENT_FL_FAILED;
5022 free_arg(arg);
5023 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5025 arg->field.field = field;
5026 *list = arg;
5027 list = &arg->next;
5029 return 0;
5032 return 0;
5034 event_parse_failed:
5035 event->flags |= EVENT_FL_FAILED;
5036 return ret;
5038 event_alloc_failed:
5039 free(event->system);
5040 free(event->name);
5041 free(event);
5042 *eventp = NULL;
5043 return ret;
5047 * pevent_parse_format - parse the event format
5048 * @buf: the buffer storing the event format string
5049 * @size: the size of @buf
5050 * @sys: the system the event belongs to
5052 * This parses the event format and creates an event structure
5053 * to quickly parse raw data for a given event.
5055 * These files currently come from:
5057 * /sys/kernel/debug/tracing/events/.../.../format
5059 enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5060 unsigned long size, const char *sys)
5062 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5066 * pevent_parse_event - parse the event format
5067 * @pevent: the handle to the pevent
5068 * @buf: the buffer storing the event format string
5069 * @size: the size of @buf
5070 * @sys: the system the event belongs to
5072 * This parses the event format and creates an event structure
5073 * to quickly parse raw data for a given event.
5075 * These files currently come from:
5077 * /sys/kernel/debug/tracing/events/.../.../format
5079 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5080 unsigned long size, const char *sys)
5082 struct event_format *event = NULL;
5083 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5085 if (event == NULL)
5086 return ret;
5088 if (add_event(pevent, event)) {
5089 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5090 goto event_add_failed;
5093 #define PRINT_ARGS 0
5094 if (PRINT_ARGS && event->print_fmt.args)
5095 print_args(event->print_fmt.args);
5097 return 0;
5099 event_add_failed:
5100 pevent_free_format(event);
5101 return ret;
5104 #undef _PE
5105 #define _PE(code, str) str
5106 static const char * const pevent_error_str[] = {
5107 PEVENT_ERRORS
5109 #undef _PE
5111 int pevent_strerror(struct pevent *pevent __maybe_unused,
5112 enum pevent_errno errnum, char *buf, size_t buflen)
5114 int idx;
5115 const char *msg;
5117 if (errnum >= 0) {
5118 msg = strerror_r(errnum, buf, buflen);
5119 if (msg != buf) {
5120 size_t len = strlen(msg);
5121 memcpy(buf, msg, min(buflen - 1, len));
5122 *(buf + min(buflen - 1, len)) = '\0';
5124 return 0;
5127 if (errnum <= __PEVENT_ERRNO__START ||
5128 errnum >= __PEVENT_ERRNO__END)
5129 return -1;
5131 idx = errnum - __PEVENT_ERRNO__START - 1;
5132 msg = pevent_error_str[idx];
5134 switch (errnum) {
5135 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5136 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5137 case PEVENT_ERRNO__READ_ID_FAILED:
5138 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5139 case PEVENT_ERRNO__READ_PRINT_FAILED:
5140 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
5141 case PEVENT_ERRNO__INVALID_ARG_TYPE:
5142 snprintf(buf, buflen, "%s", msg);
5143 break;
5145 default:
5146 /* cannot reach here */
5147 break;
5150 return 0;
5153 int get_field_val(struct trace_seq *s, struct format_field *field,
5154 const char *name, struct pevent_record *record,
5155 unsigned long long *val, int err)
5157 if (!field) {
5158 if (err)
5159 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5160 return -1;
5163 if (pevent_read_number_field(field, record->data, val)) {
5164 if (err)
5165 trace_seq_printf(s, " %s=INVALID", name);
5166 return -1;
5169 return 0;
5173 * pevent_get_field_raw - return the raw pointer into the data field
5174 * @s: The seq to print to on error
5175 * @event: the event that the field is for
5176 * @name: The name of the field
5177 * @record: The record with the field name.
5178 * @len: place to store the field length.
5179 * @err: print default error if failed.
5181 * Returns a pointer into record->data of the field and places
5182 * the length of the field in @len.
5184 * On failure, it returns NULL.
5186 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5187 const char *name, struct pevent_record *record,
5188 int *len, int err)
5190 struct format_field *field;
5191 void *data = record->data;
5192 unsigned offset;
5193 int dummy;
5195 if (!event)
5196 return NULL;
5198 field = pevent_find_field(event, name);
5200 if (!field) {
5201 if (err)
5202 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5203 return NULL;
5206 /* Allow @len to be NULL */
5207 if (!len)
5208 len = &dummy;
5210 offset = field->offset;
5211 if (field->flags & FIELD_IS_DYNAMIC) {
5212 offset = pevent_read_number(event->pevent,
5213 data + offset, field->size);
5214 *len = offset >> 16;
5215 offset &= 0xffff;
5216 } else
5217 *len = field->size;
5219 return data + offset;
5223 * pevent_get_field_val - find a field and return its value
5224 * @s: The seq to print to on error
5225 * @event: the event that the field is for
5226 * @name: The name of the field
5227 * @record: The record with the field name.
5228 * @val: place to store the value of the field.
5229 * @err: print default error if failed.
5231 * Returns 0 on success -1 on field not found.
5233 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5234 const char *name, struct pevent_record *record,
5235 unsigned long long *val, int err)
5237 struct format_field *field;
5239 if (!event)
5240 return -1;
5242 field = pevent_find_field(event, name);
5244 return get_field_val(s, field, name, record, val, err);
5248 * pevent_get_common_field_val - find a common field and return its value
5249 * @s: The seq to print to on error
5250 * @event: the event that the field is for
5251 * @name: The name of the field
5252 * @record: The record with the field name.
5253 * @val: place to store the value of the field.
5254 * @err: print default error if failed.
5256 * Returns 0 on success -1 on field not found.
5258 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5259 const char *name, struct pevent_record *record,
5260 unsigned long long *val, int err)
5262 struct format_field *field;
5264 if (!event)
5265 return -1;
5267 field = pevent_find_common_field(event, name);
5269 return get_field_val(s, field, name, record, val, err);
5273 * pevent_get_any_field_val - find a any field and return its value
5274 * @s: The seq to print to on error
5275 * @event: the event that the field is for
5276 * @name: The name of the field
5277 * @record: The record with the field name.
5278 * @val: place to store the value of the field.
5279 * @err: print default error if failed.
5281 * Returns 0 on success -1 on field not found.
5283 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5284 const char *name, struct pevent_record *record,
5285 unsigned long long *val, int err)
5287 struct format_field *field;
5289 if (!event)
5290 return -1;
5292 field = pevent_find_any_field(event, name);
5294 return get_field_val(s, field, name, record, val, err);
5298 * pevent_print_num_field - print a field and a format
5299 * @s: The seq to print to
5300 * @fmt: The printf format to print the field with.
5301 * @event: the event that the field is for
5302 * @name: The name of the field
5303 * @record: The record with the field name.
5304 * @err: print default error if failed.
5306 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5308 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5309 struct event_format *event, const char *name,
5310 struct pevent_record *record, int err)
5312 struct format_field *field = pevent_find_field(event, name);
5313 unsigned long long val;
5315 if (!field)
5316 goto failed;
5318 if (pevent_read_number_field(field, record->data, &val))
5319 goto failed;
5321 return trace_seq_printf(s, fmt, val);
5323 failed:
5324 if (err)
5325 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5326 return -1;
5329 static void free_func_handle(struct pevent_function_handler *func)
5331 struct pevent_func_params *params;
5333 free(func->name);
5335 while (func->params) {
5336 params = func->params;
5337 func->params = params->next;
5338 free(params);
5341 free(func);
5345 * pevent_register_print_function - register a helper function
5346 * @pevent: the handle to the pevent
5347 * @func: the function to process the helper function
5348 * @ret_type: the return type of the helper function
5349 * @name: the name of the helper function
5350 * @parameters: A list of enum pevent_func_arg_type
5352 * Some events may have helper functions in the print format arguments.
5353 * This allows a plugin to dynamically create a way to process one
5354 * of these functions.
5356 * The @parameters is a variable list of pevent_func_arg_type enums that
5357 * must end with PEVENT_FUNC_ARG_VOID.
5359 int pevent_register_print_function(struct pevent *pevent,
5360 pevent_func_handler func,
5361 enum pevent_func_arg_type ret_type,
5362 char *name, ...)
5364 struct pevent_function_handler *func_handle;
5365 struct pevent_func_params **next_param;
5366 struct pevent_func_params *param;
5367 enum pevent_func_arg_type type;
5368 va_list ap;
5369 int ret;
5371 func_handle = find_func_handler(pevent, name);
5372 if (func_handle) {
5374 * This is most like caused by the users own
5375 * plugins updating the function. This overrides the
5376 * system defaults.
5378 pr_stat("override of function helper '%s'", name);
5379 remove_func_handler(pevent, name);
5382 func_handle = calloc(1, sizeof(*func_handle));
5383 if (!func_handle) {
5384 do_warning("Failed to allocate function handler");
5385 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5388 func_handle->ret_type = ret_type;
5389 func_handle->name = strdup(name);
5390 func_handle->func = func;
5391 if (!func_handle->name) {
5392 do_warning("Failed to allocate function name");
5393 free(func_handle);
5394 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5397 next_param = &(func_handle->params);
5398 va_start(ap, name);
5399 for (;;) {
5400 type = va_arg(ap, enum pevent_func_arg_type);
5401 if (type == PEVENT_FUNC_ARG_VOID)
5402 break;
5404 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5405 do_warning("Invalid argument type %d", type);
5406 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5407 goto out_free;
5410 param = malloc(sizeof(*param));
5411 if (!param) {
5412 do_warning("Failed to allocate function param");
5413 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5414 goto out_free;
5416 param->type = type;
5417 param->next = NULL;
5419 *next_param = param;
5420 next_param = &(param->next);
5422 func_handle->nr_args++;
5424 va_end(ap);
5426 func_handle->next = pevent->func_handlers;
5427 pevent->func_handlers = func_handle;
5429 return 0;
5430 out_free:
5431 va_end(ap);
5432 free_func_handle(func_handle);
5433 return ret;
5437 * pevent_register_event_handler - register a way to parse an event
5438 * @pevent: the handle to the pevent
5439 * @id: the id of the event to register
5440 * @sys_name: the system name the event belongs to
5441 * @event_name: the name of the event
5442 * @func: the function to call to parse the event information
5443 * @context: the data to be passed to @func
5445 * This function allows a developer to override the parsing of
5446 * a given event. If for some reason the default print format
5447 * is not sufficient, this function will register a function
5448 * for an event to be used to parse the data instead.
5450 * If @id is >= 0, then it is used to find the event.
5451 * else @sys_name and @event_name are used.
5453 int pevent_register_event_handler(struct pevent *pevent,
5454 int id, char *sys_name, char *event_name,
5455 pevent_event_handler_func func,
5456 void *context)
5458 struct event_format *event;
5459 struct event_handler *handle;
5461 if (id >= 0) {
5462 /* search by id */
5463 event = pevent_find_event(pevent, id);
5464 if (!event)
5465 goto not_found;
5466 if (event_name && (strcmp(event_name, event->name) != 0))
5467 goto not_found;
5468 if (sys_name && (strcmp(sys_name, event->system) != 0))
5469 goto not_found;
5470 } else {
5471 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5472 if (!event)
5473 goto not_found;
5476 pr_stat("overriding event (%d) %s:%s with new print handler",
5477 event->id, event->system, event->name);
5479 event->handler = func;
5480 event->context = context;
5481 return 0;
5483 not_found:
5484 /* Save for later use. */
5485 handle = calloc(1, sizeof(*handle));
5486 if (!handle) {
5487 do_warning("Failed to allocate event handler");
5488 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5491 handle->id = id;
5492 if (event_name)
5493 handle->event_name = strdup(event_name);
5494 if (sys_name)
5495 handle->sys_name = strdup(sys_name);
5497 if ((event_name && !handle->event_name) ||
5498 (sys_name && !handle->sys_name)) {
5499 do_warning("Failed to allocate event/sys name");
5500 free((void *)handle->event_name);
5501 free((void *)handle->sys_name);
5502 free(handle);
5503 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5506 handle->func = func;
5507 handle->next = pevent->handlers;
5508 pevent->handlers = handle;
5509 handle->context = context;
5511 return -1;
5515 * pevent_alloc - create a pevent handle
5517 struct pevent *pevent_alloc(void)
5519 struct pevent *pevent = calloc(1, sizeof(*pevent));
5521 if (pevent)
5522 pevent->ref_count = 1;
5524 return pevent;
5527 void pevent_ref(struct pevent *pevent)
5529 pevent->ref_count++;
5532 static void free_format_fields(struct format_field *field)
5534 struct format_field *next;
5536 while (field) {
5537 next = field->next;
5538 free(field->type);
5539 free(field->name);
5540 free(field);
5541 field = next;
5545 static void free_formats(struct format *format)
5547 free_format_fields(format->common_fields);
5548 free_format_fields(format->fields);
5551 void pevent_free_format(struct event_format *event)
5553 free(event->name);
5554 free(event->system);
5556 free_formats(&event->format);
5558 free(event->print_fmt.format);
5559 free_args(event->print_fmt.args);
5561 free(event);
5565 * pevent_free - free a pevent handle
5566 * @pevent: the pevent handle to free
5568 void pevent_free(struct pevent *pevent)
5570 struct cmdline_list *cmdlist, *cmdnext;
5571 struct func_list *funclist, *funcnext;
5572 struct printk_list *printklist, *printknext;
5573 struct pevent_function_handler *func_handler;
5574 struct event_handler *handle;
5575 int i;
5577 if (!pevent)
5578 return;
5580 cmdlist = pevent->cmdlist;
5581 funclist = pevent->funclist;
5582 printklist = pevent->printklist;
5584 pevent->ref_count--;
5585 if (pevent->ref_count)
5586 return;
5588 if (pevent->cmdlines) {
5589 for (i = 0; i < pevent->cmdline_count; i++)
5590 free(pevent->cmdlines[i].comm);
5591 free(pevent->cmdlines);
5594 while (cmdlist) {
5595 cmdnext = cmdlist->next;
5596 free(cmdlist->comm);
5597 free(cmdlist);
5598 cmdlist = cmdnext;
5601 if (pevent->func_map) {
5602 for (i = 0; i < (int)pevent->func_count; i++) {
5603 free(pevent->func_map[i].func);
5604 free(pevent->func_map[i].mod);
5606 free(pevent->func_map);
5609 while (funclist) {
5610 funcnext = funclist->next;
5611 free(funclist->func);
5612 free(funclist->mod);
5613 free(funclist);
5614 funclist = funcnext;
5617 while (pevent->func_handlers) {
5618 func_handler = pevent->func_handlers;
5619 pevent->func_handlers = func_handler->next;
5620 free_func_handle(func_handler);
5623 if (pevent->printk_map) {
5624 for (i = 0; i < (int)pevent->printk_count; i++)
5625 free(pevent->printk_map[i].printk);
5626 free(pevent->printk_map);
5629 while (printklist) {
5630 printknext = printklist->next;
5631 free(printklist->printk);
5632 free(printklist);
5633 printklist = printknext;
5636 for (i = 0; i < pevent->nr_events; i++)
5637 pevent_free_format(pevent->events[i]);
5639 while (pevent->handlers) {
5640 handle = pevent->handlers;
5641 pevent->handlers = handle->next;
5642 free_handler(handle);
5645 free(pevent->events);
5646 free(pevent->sort_events);
5648 free(pevent);
5651 void pevent_unref(struct pevent *pevent)
5653 pevent_free(pevent);