tools lib traceevent: Introduce pevent_strerror
[linux-2.6/btrfs-unstable.git] / tools / lib / traceevent / event-parse.c
blob1373e4cf109e6fca7fe7aaecc0b73e39b9a400c9
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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by
23 * - Copyright (C) 2009 Frederic Weisbecker,
24 * Frederic Weisbecker gave his permission to relicense the code to
25 * the Lesser General Public License.
27 #define _GNU_SOURCE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdint.h>
36 #include "event-parse.h"
37 #include "event-utils.h"
39 static const char *input_buf;
40 static unsigned long long input_buf_ptr;
41 static unsigned long long input_buf_siz;
43 static int is_flag_field;
44 static int is_symbolic_field;
46 static int show_warning = 1;
48 #define do_warning(fmt, ...) \
49 do { \
50 if (show_warning) \
51 warning(fmt, ##__VA_ARGS__); \
52 } while (0)
54 static void init_input_buf(const char *buf, unsigned long long size)
56 input_buf = buf;
57 input_buf_siz = size;
58 input_buf_ptr = 0;
61 const char *pevent_get_input_buf(void)
63 return input_buf;
66 unsigned long long pevent_get_input_buf_ptr(void)
68 return input_buf_ptr;
71 struct event_handler {
72 struct event_handler *next;
73 int id;
74 const char *sys_name;
75 const char *event_name;
76 pevent_event_handler_func func;
77 void *context;
80 struct pevent_func_params {
81 struct pevent_func_params *next;
82 enum pevent_func_arg_type type;
85 struct pevent_function_handler {
86 struct pevent_function_handler *next;
87 enum pevent_func_arg_type ret_type;
88 char *name;
89 pevent_func_handler func;
90 struct pevent_func_params *params;
91 int nr_args;
94 static unsigned long long
95 process_defined_func(struct trace_seq *s, void *data, int size,
96 struct event_format *event, struct print_arg *arg);
98 static void free_func_handle(struct pevent_function_handler *func);
101 * pevent_buffer_init - init buffer for parsing
102 * @buf: buffer to parse
103 * @size: the size of the buffer
105 * For use with pevent_read_token(), this initializes the internal
106 * buffer that pevent_read_token() will parse.
108 void pevent_buffer_init(const char *buf, unsigned long long size)
110 init_input_buf(buf, size);
113 void breakpoint(void)
115 static int x;
116 x++;
119 struct print_arg *alloc_arg(void)
121 struct print_arg *arg;
123 arg = malloc_or_die(sizeof(*arg));
124 if (!arg)
125 return NULL;
126 memset(arg, 0, sizeof(*arg));
128 return arg;
131 struct cmdline {
132 char *comm;
133 int pid;
136 static int cmdline_cmp(const void *a, const void *b)
138 const struct cmdline *ca = a;
139 const struct cmdline *cb = b;
141 if (ca->pid < cb->pid)
142 return -1;
143 if (ca->pid > cb->pid)
144 return 1;
146 return 0;
149 struct cmdline_list {
150 struct cmdline_list *next;
151 char *comm;
152 int pid;
155 static int cmdline_init(struct pevent *pevent)
157 struct cmdline_list *cmdlist = pevent->cmdlist;
158 struct cmdline_list *item;
159 struct cmdline *cmdlines;
160 int i;
162 cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
164 i = 0;
165 while (cmdlist) {
166 cmdlines[i].pid = cmdlist->pid;
167 cmdlines[i].comm = cmdlist->comm;
168 i++;
169 item = cmdlist;
170 cmdlist = cmdlist->next;
171 free(item);
174 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
176 pevent->cmdlines = cmdlines;
177 pevent->cmdlist = NULL;
179 return 0;
182 static char *find_cmdline(struct pevent *pevent, int pid)
184 const struct cmdline *comm;
185 struct cmdline key;
187 if (!pid)
188 return "<idle>";
190 if (!pevent->cmdlines)
191 cmdline_init(pevent);
193 key.pid = pid;
195 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
196 sizeof(*pevent->cmdlines), cmdline_cmp);
198 if (comm)
199 return comm->comm;
200 return "<...>";
204 * pevent_pid_is_registered - return if a pid has a cmdline registered
205 * @pevent: handle for the pevent
206 * @pid: The pid to check if it has a cmdline registered with.
208 * Returns 1 if the pid has a cmdline mapped to it
209 * 0 otherwise.
211 int pevent_pid_is_registered(struct pevent *pevent, int pid)
213 const struct cmdline *comm;
214 struct cmdline key;
216 if (!pid)
217 return 1;
219 if (!pevent->cmdlines)
220 cmdline_init(pevent);
222 key.pid = pid;
224 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
225 sizeof(*pevent->cmdlines), cmdline_cmp);
227 if (comm)
228 return 1;
229 return 0;
233 * If the command lines have been converted to an array, then
234 * we must add this pid. This is much slower than when cmdlines
235 * are added before the array is initialized.
237 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
239 struct cmdline *cmdlines = pevent->cmdlines;
240 const struct cmdline *cmdline;
241 struct cmdline key;
243 if (!pid)
244 return 0;
246 /* avoid duplicates */
247 key.pid = pid;
249 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
250 sizeof(*pevent->cmdlines), cmdline_cmp);
251 if (cmdline) {
252 errno = EEXIST;
253 return -1;
256 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
257 if (!cmdlines) {
258 errno = ENOMEM;
259 return -1;
262 cmdlines[pevent->cmdline_count].pid = pid;
263 cmdlines[pevent->cmdline_count].comm = strdup(comm);
264 if (!cmdlines[pevent->cmdline_count].comm)
265 die("malloc comm");
267 if (cmdlines[pevent->cmdline_count].comm)
268 pevent->cmdline_count++;
270 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
271 pevent->cmdlines = cmdlines;
273 return 0;
277 * pevent_register_comm - register a pid / comm mapping
278 * @pevent: handle for the pevent
279 * @comm: the command line to register
280 * @pid: the pid to map the command line to
282 * This adds a mapping to search for command line names with
283 * a given pid. The comm is duplicated.
285 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
287 struct cmdline_list *item;
289 if (pevent->cmdlines)
290 return add_new_comm(pevent, comm, pid);
292 item = malloc_or_die(sizeof(*item));
293 item->comm = strdup(comm);
294 if (!item->comm)
295 die("malloc comm");
296 item->pid = pid;
297 item->next = pevent->cmdlist;
299 pevent->cmdlist = item;
300 pevent->cmdline_count++;
302 return 0;
305 struct func_map {
306 unsigned long long addr;
307 char *func;
308 char *mod;
311 struct func_list {
312 struct func_list *next;
313 unsigned long long addr;
314 char *func;
315 char *mod;
318 static int func_cmp(const void *a, const void *b)
320 const struct func_map *fa = a;
321 const struct func_map *fb = b;
323 if (fa->addr < fb->addr)
324 return -1;
325 if (fa->addr > fb->addr)
326 return 1;
328 return 0;
332 * We are searching for a record in between, not an exact
333 * match.
335 static int func_bcmp(const void *a, const void *b)
337 const struct func_map *fa = a;
338 const struct func_map *fb = b;
340 if ((fa->addr == fb->addr) ||
342 (fa->addr > fb->addr &&
343 fa->addr < (fb+1)->addr))
344 return 0;
346 if (fa->addr < fb->addr)
347 return -1;
349 return 1;
352 static int func_map_init(struct pevent *pevent)
354 struct func_list *funclist;
355 struct func_list *item;
356 struct func_map *func_map;
357 int i;
359 func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
360 funclist = pevent->funclist;
362 i = 0;
363 while (funclist) {
364 func_map[i].func = funclist->func;
365 func_map[i].addr = funclist->addr;
366 func_map[i].mod = funclist->mod;
367 i++;
368 item = funclist;
369 funclist = funclist->next;
370 free(item);
373 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
376 * Add a special record at the end.
378 func_map[pevent->func_count].func = NULL;
379 func_map[pevent->func_count].addr = 0;
380 func_map[pevent->func_count].mod = NULL;
382 pevent->func_map = func_map;
383 pevent->funclist = NULL;
385 return 0;
388 static struct func_map *
389 find_func(struct pevent *pevent, unsigned long long addr)
391 struct func_map *func;
392 struct func_map key;
394 if (!pevent->func_map)
395 func_map_init(pevent);
397 key.addr = addr;
399 func = bsearch(&key, pevent->func_map, pevent->func_count,
400 sizeof(*pevent->func_map), func_bcmp);
402 return func;
406 * pevent_find_function - find a function by a given address
407 * @pevent: handle for the pevent
408 * @addr: the address to find the function with
410 * Returns a pointer to the function stored that has the given
411 * address. Note, the address does not have to be exact, it
412 * will select the function that would contain the address.
414 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
416 struct func_map *map;
418 map = find_func(pevent, addr);
419 if (!map)
420 return NULL;
422 return map->func;
426 * pevent_find_function_address - find a function address by a given address
427 * @pevent: handle for the pevent
428 * @addr: the address to find the function with
430 * Returns the address the function starts at. This can be used in
431 * conjunction with pevent_find_function to print both the function
432 * name and the function offset.
434 unsigned long long
435 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
437 struct func_map *map;
439 map = find_func(pevent, addr);
440 if (!map)
441 return 0;
443 return map->addr;
447 * pevent_register_function - register a function with a given address
448 * @pevent: handle for the pevent
449 * @function: the function name to register
450 * @addr: the address the function starts at
451 * @mod: the kernel module the function may be in (NULL for none)
453 * This registers a function name with an address and module.
454 * The @func passed in is duplicated.
456 int pevent_register_function(struct pevent *pevent, char *func,
457 unsigned long long addr, char *mod)
459 struct func_list *item;
461 item = malloc_or_die(sizeof(*item));
463 item->next = pevent->funclist;
464 item->func = strdup(func);
465 if (mod)
466 item->mod = strdup(mod);
467 else
468 item->mod = NULL;
469 item->addr = addr;
471 if (!item->func || (mod && !item->mod))
472 die("malloc func");
474 pevent->funclist = item;
475 pevent->func_count++;
477 return 0;
481 * pevent_print_funcs - print out the stored functions
482 * @pevent: handle for the pevent
484 * This prints out the stored functions.
486 void pevent_print_funcs(struct pevent *pevent)
488 int i;
490 if (!pevent->func_map)
491 func_map_init(pevent);
493 for (i = 0; i < (int)pevent->func_count; i++) {
494 printf("%016llx %s",
495 pevent->func_map[i].addr,
496 pevent->func_map[i].func);
497 if (pevent->func_map[i].mod)
498 printf(" [%s]\n", pevent->func_map[i].mod);
499 else
500 printf("\n");
504 struct printk_map {
505 unsigned long long addr;
506 char *printk;
509 struct printk_list {
510 struct printk_list *next;
511 unsigned long long addr;
512 char *printk;
515 static int printk_cmp(const void *a, const void *b)
517 const struct printk_map *pa = a;
518 const struct printk_map *pb = b;
520 if (pa->addr < pb->addr)
521 return -1;
522 if (pa->addr > pb->addr)
523 return 1;
525 return 0;
528 static void printk_map_init(struct pevent *pevent)
530 struct printk_list *printklist;
531 struct printk_list *item;
532 struct printk_map *printk_map;
533 int i;
535 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
537 printklist = pevent->printklist;
539 i = 0;
540 while (printklist) {
541 printk_map[i].printk = printklist->printk;
542 printk_map[i].addr = printklist->addr;
543 i++;
544 item = printklist;
545 printklist = printklist->next;
546 free(item);
549 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
551 pevent->printk_map = printk_map;
552 pevent->printklist = NULL;
555 static struct printk_map *
556 find_printk(struct pevent *pevent, unsigned long long addr)
558 struct printk_map *printk;
559 struct printk_map key;
561 if (!pevent->printk_map)
562 printk_map_init(pevent);
564 key.addr = addr;
566 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
567 sizeof(*pevent->printk_map), printk_cmp);
569 return printk;
573 * pevent_register_print_string - register a string by its address
574 * @pevent: handle for the pevent
575 * @fmt: the string format to register
576 * @addr: the address the string was located at
578 * This registers a string by the address it was stored in the kernel.
579 * The @fmt passed in is duplicated.
581 int pevent_register_print_string(struct pevent *pevent, char *fmt,
582 unsigned long long addr)
584 struct printk_list *item;
586 item = malloc_or_die(sizeof(*item));
588 item->next = pevent->printklist;
589 item->printk = strdup(fmt);
590 item->addr = addr;
592 if (!item->printk)
593 die("malloc fmt");
595 pevent->printklist = item;
596 pevent->printk_count++;
598 return 0;
602 * pevent_print_printk - print out the stored strings
603 * @pevent: handle for the pevent
605 * This prints the string formats that were stored.
607 void pevent_print_printk(struct pevent *pevent)
609 int i;
611 if (!pevent->printk_map)
612 printk_map_init(pevent);
614 for (i = 0; i < (int)pevent->printk_count; i++) {
615 printf("%016llx %s\n",
616 pevent->printk_map[i].addr,
617 pevent->printk_map[i].printk);
621 static struct event_format *alloc_event(void)
623 struct event_format *event;
625 event = malloc(sizeof(*event));
626 if (!event)
627 return NULL;
628 memset(event, 0, sizeof(*event));
630 return event;
633 static void add_event(struct pevent *pevent, struct event_format *event)
635 int i;
637 pevent->events = realloc(pevent->events, sizeof(event) *
638 (pevent->nr_events + 1));
639 if (!pevent->events)
640 die("Can not allocate events");
642 for (i = 0; i < pevent->nr_events; i++) {
643 if (pevent->events[i]->id > event->id)
644 break;
646 if (i < pevent->nr_events)
647 memmove(&pevent->events[i + 1],
648 &pevent->events[i],
649 sizeof(event) * (pevent->nr_events - i));
651 pevent->events[i] = event;
652 pevent->nr_events++;
654 event->pevent = pevent;
657 static int event_item_type(enum event_type type)
659 switch (type) {
660 case EVENT_ITEM ... EVENT_SQUOTE:
661 return 1;
662 case EVENT_ERROR ... EVENT_DELIM:
663 default:
664 return 0;
668 static void free_flag_sym(struct print_flag_sym *fsym)
670 struct print_flag_sym *next;
672 while (fsym) {
673 next = fsym->next;
674 free(fsym->value);
675 free(fsym->str);
676 free(fsym);
677 fsym = next;
681 static void free_arg(struct print_arg *arg)
683 struct print_arg *farg;
685 if (!arg)
686 return;
688 switch (arg->type) {
689 case PRINT_ATOM:
690 free(arg->atom.atom);
691 break;
692 case PRINT_FIELD:
693 free(arg->field.name);
694 break;
695 case PRINT_FLAGS:
696 free_arg(arg->flags.field);
697 free(arg->flags.delim);
698 free_flag_sym(arg->flags.flags);
699 break;
700 case PRINT_SYMBOL:
701 free_arg(arg->symbol.field);
702 free_flag_sym(arg->symbol.symbols);
703 break;
704 case PRINT_HEX:
705 free_arg(arg->hex.field);
706 free_arg(arg->hex.size);
707 break;
708 case PRINT_TYPE:
709 free(arg->typecast.type);
710 free_arg(arg->typecast.item);
711 break;
712 case PRINT_STRING:
713 case PRINT_BSTRING:
714 free(arg->string.string);
715 break;
716 case PRINT_DYNAMIC_ARRAY:
717 free(arg->dynarray.index);
718 break;
719 case PRINT_OP:
720 free(arg->op.op);
721 free_arg(arg->op.left);
722 free_arg(arg->op.right);
723 break;
724 case PRINT_FUNC:
725 while (arg->func.args) {
726 farg = arg->func.args;
727 arg->func.args = farg->next;
728 free_arg(farg);
730 break;
732 case PRINT_NULL:
733 default:
734 break;
737 free(arg);
740 static enum event_type get_type(int ch)
742 if (ch == '\n')
743 return EVENT_NEWLINE;
744 if (isspace(ch))
745 return EVENT_SPACE;
746 if (isalnum(ch) || ch == '_')
747 return EVENT_ITEM;
748 if (ch == '\'')
749 return EVENT_SQUOTE;
750 if (ch == '"')
751 return EVENT_DQUOTE;
752 if (!isprint(ch))
753 return EVENT_NONE;
754 if (ch == '(' || ch == ')' || ch == ',')
755 return EVENT_DELIM;
757 return EVENT_OP;
760 static int __read_char(void)
762 if (input_buf_ptr >= input_buf_siz)
763 return -1;
765 return input_buf[input_buf_ptr++];
768 static int __peek_char(void)
770 if (input_buf_ptr >= input_buf_siz)
771 return -1;
773 return input_buf[input_buf_ptr];
777 * pevent_peek_char - peek at the next character that will be read
779 * Returns the next character read, or -1 if end of buffer.
781 int pevent_peek_char(void)
783 return __peek_char();
786 static int extend_token(char **tok, char *buf, int size)
788 char *newtok = realloc(*tok, size);
790 if (!newtok) {
791 free(*tok);
792 *tok = NULL;
793 return -1;
796 if (!*tok)
797 strcpy(newtok, buf);
798 else
799 strcat(newtok, buf);
800 *tok = newtok;
802 return 0;
805 static enum event_type force_token(const char *str, char **tok);
807 static enum event_type __read_token(char **tok)
809 char buf[BUFSIZ];
810 int ch, last_ch, quote_ch, next_ch;
811 int i = 0;
812 int tok_size = 0;
813 enum event_type type;
815 *tok = NULL;
818 ch = __read_char();
819 if (ch < 0)
820 return EVENT_NONE;
822 type = get_type(ch);
823 if (type == EVENT_NONE)
824 return type;
826 buf[i++] = ch;
828 switch (type) {
829 case EVENT_NEWLINE:
830 case EVENT_DELIM:
831 *tok = malloc_or_die(2);
832 (*tok)[0] = ch;
833 (*tok)[1] = 0;
834 return type;
836 case EVENT_OP:
837 switch (ch) {
838 case '-':
839 next_ch = __peek_char();
840 if (next_ch == '>') {
841 buf[i++] = __read_char();
842 break;
844 /* fall through */
845 case '+':
846 case '|':
847 case '&':
848 case '>':
849 case '<':
850 last_ch = ch;
851 ch = __peek_char();
852 if (ch != last_ch)
853 goto test_equal;
854 buf[i++] = __read_char();
855 switch (last_ch) {
856 case '>':
857 case '<':
858 goto test_equal;
859 default:
860 break;
862 break;
863 case '!':
864 case '=':
865 goto test_equal;
866 default: /* what should we do instead? */
867 break;
869 buf[i] = 0;
870 *tok = strdup(buf);
871 return type;
873 test_equal:
874 ch = __peek_char();
875 if (ch == '=')
876 buf[i++] = __read_char();
877 goto out;
879 case EVENT_DQUOTE:
880 case EVENT_SQUOTE:
881 /* don't keep quotes */
882 i--;
883 quote_ch = ch;
884 last_ch = 0;
885 concat:
886 do {
887 if (i == (BUFSIZ - 1)) {
888 buf[i] = 0;
889 tok_size += BUFSIZ;
891 if (extend_token(tok, buf, tok_size) < 0)
892 return EVENT_NONE;
893 i = 0;
895 last_ch = ch;
896 ch = __read_char();
897 buf[i++] = ch;
898 /* the '\' '\' will cancel itself */
899 if (ch == '\\' && last_ch == '\\')
900 last_ch = 0;
901 } while (ch != quote_ch || last_ch == '\\');
902 /* remove the last quote */
903 i--;
906 * For strings (double quotes) check the next token.
907 * If it is another string, concatinate the two.
909 if (type == EVENT_DQUOTE) {
910 unsigned long long save_input_buf_ptr = input_buf_ptr;
912 do {
913 ch = __read_char();
914 } while (isspace(ch));
915 if (ch == '"')
916 goto concat;
917 input_buf_ptr = save_input_buf_ptr;
920 goto out;
922 case EVENT_ERROR ... EVENT_SPACE:
923 case EVENT_ITEM:
924 default:
925 break;
928 while (get_type(__peek_char()) == type) {
929 if (i == (BUFSIZ - 1)) {
930 buf[i] = 0;
931 tok_size += BUFSIZ;
933 if (extend_token(tok, buf, tok_size) < 0)
934 return EVENT_NONE;
935 i = 0;
937 ch = __read_char();
938 buf[i++] = ch;
941 out:
942 buf[i] = 0;
943 if (extend_token(tok, buf, tok_size + i + 1) < 0)
944 return EVENT_NONE;
946 if (type == EVENT_ITEM) {
948 * Older versions of the kernel has a bug that
949 * creates invalid symbols and will break the mac80211
950 * parsing. This is a work around to that bug.
952 * See Linux kernel commit:
953 * 811cb50baf63461ce0bdb234927046131fc7fa8b
955 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
956 free(*tok);
957 *tok = NULL;
958 return force_token("\"\%s\" ", tok);
959 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
960 free(*tok);
961 *tok = NULL;
962 return force_token("\" sta:%pM\" ", tok);
963 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
964 free(*tok);
965 *tok = NULL;
966 return force_token("\" vif:%p(%d)\" ", tok);
970 return type;
973 static enum event_type force_token(const char *str, char **tok)
975 const char *save_input_buf;
976 unsigned long long save_input_buf_ptr;
977 unsigned long long save_input_buf_siz;
978 enum event_type type;
980 /* save off the current input pointers */
981 save_input_buf = input_buf;
982 save_input_buf_ptr = input_buf_ptr;
983 save_input_buf_siz = input_buf_siz;
985 init_input_buf(str, strlen(str));
987 type = __read_token(tok);
989 /* reset back to original token */
990 input_buf = save_input_buf;
991 input_buf_ptr = save_input_buf_ptr;
992 input_buf_siz = save_input_buf_siz;
994 return type;
997 static void free_token(char *tok)
999 if (tok)
1000 free(tok);
1003 static enum event_type read_token(char **tok)
1005 enum event_type type;
1007 for (;;) {
1008 type = __read_token(tok);
1009 if (type != EVENT_SPACE)
1010 return type;
1012 free_token(*tok);
1015 /* not reached */
1016 *tok = NULL;
1017 return EVENT_NONE;
1021 * pevent_read_token - access to utilites to use the pevent parser
1022 * @tok: The token to return
1024 * This will parse tokens from the string given by
1025 * pevent_init_data().
1027 * Returns the token type.
1029 enum event_type pevent_read_token(char **tok)
1031 return read_token(tok);
1035 * pevent_free_token - free a token returned by pevent_read_token
1036 * @token: the token to free
1038 void pevent_free_token(char *token)
1040 free_token(token);
1043 /* no newline */
1044 static enum event_type read_token_item(char **tok)
1046 enum event_type type;
1048 for (;;) {
1049 type = __read_token(tok);
1050 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1051 return type;
1052 free_token(*tok);
1053 *tok = NULL;
1056 /* not reached */
1057 *tok = NULL;
1058 return EVENT_NONE;
1061 static int test_type(enum event_type type, enum event_type expect)
1063 if (type != expect) {
1064 do_warning("Error: expected type %d but read %d",
1065 expect, type);
1066 return -1;
1068 return 0;
1071 static int test_type_token(enum event_type type, const char *token,
1072 enum event_type expect, const char *expect_tok)
1074 if (type != expect) {
1075 do_warning("Error: expected type %d but read %d",
1076 expect, type);
1077 return -1;
1080 if (strcmp(token, expect_tok) != 0) {
1081 do_warning("Error: expected '%s' but read '%s'",
1082 expect_tok, token);
1083 return -1;
1085 return 0;
1088 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1090 enum event_type type;
1092 if (newline_ok)
1093 type = read_token(tok);
1094 else
1095 type = read_token_item(tok);
1096 return test_type(type, expect);
1099 static int read_expect_type(enum event_type expect, char **tok)
1101 return __read_expect_type(expect, tok, 1);
1104 static int __read_expected(enum event_type expect, const char *str,
1105 int newline_ok)
1107 enum event_type type;
1108 char *token;
1109 int ret;
1111 if (newline_ok)
1112 type = read_token(&token);
1113 else
1114 type = read_token_item(&token);
1116 ret = test_type_token(type, token, expect, str);
1118 free_token(token);
1120 return ret;
1123 static int read_expected(enum event_type expect, const char *str)
1125 return __read_expected(expect, str, 1);
1128 static int read_expected_item(enum event_type expect, const char *str)
1130 return __read_expected(expect, str, 0);
1133 static char *event_read_name(void)
1135 char *token;
1137 if (read_expected(EVENT_ITEM, "name") < 0)
1138 return NULL;
1140 if (read_expected(EVENT_OP, ":") < 0)
1141 return NULL;
1143 if (read_expect_type(EVENT_ITEM, &token) < 0)
1144 goto fail;
1146 return token;
1148 fail:
1149 free_token(token);
1150 return NULL;
1153 static int event_read_id(void)
1155 char *token;
1156 int id;
1158 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1159 return -1;
1161 if (read_expected(EVENT_OP, ":") < 0)
1162 return -1;
1164 if (read_expect_type(EVENT_ITEM, &token) < 0)
1165 goto fail;
1167 id = strtoul(token, NULL, 0);
1168 free_token(token);
1169 return id;
1171 fail:
1172 free_token(token);
1173 return -1;
1176 static int field_is_string(struct format_field *field)
1178 if ((field->flags & FIELD_IS_ARRAY) &&
1179 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1180 strstr(field->type, "s8")))
1181 return 1;
1183 return 0;
1186 static int field_is_dynamic(struct format_field *field)
1188 if (strncmp(field->type, "__data_loc", 10) == 0)
1189 return 1;
1191 return 0;
1194 static int field_is_long(struct format_field *field)
1196 /* includes long long */
1197 if (strstr(field->type, "long"))
1198 return 1;
1200 return 0;
1203 static int event_read_fields(struct event_format *event, struct format_field **fields)
1205 struct format_field *field = NULL;
1206 enum event_type type;
1207 char *token;
1208 char *last_token;
1209 int count = 0;
1211 do {
1212 type = read_token(&token);
1213 if (type == EVENT_NEWLINE) {
1214 free_token(token);
1215 return count;
1218 count++;
1220 if (test_type_token(type, token, EVENT_ITEM, "field"))
1221 goto fail;
1222 free_token(token);
1224 type = read_token(&token);
1226 * The ftrace fields may still use the "special" name.
1227 * Just ignore it.
1229 if (event->flags & EVENT_FL_ISFTRACE &&
1230 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1231 free_token(token);
1232 type = read_token(&token);
1235 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1236 goto fail;
1238 free_token(token);
1239 if (read_expect_type(EVENT_ITEM, &token) < 0)
1240 goto fail;
1242 last_token = token;
1244 field = malloc_or_die(sizeof(*field));
1245 memset(field, 0, sizeof(*field));
1246 field->event = event;
1248 /* read the rest of the type */
1249 for (;;) {
1250 type = read_token(&token);
1251 if (type == EVENT_ITEM ||
1252 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1254 * Some of the ftrace fields are broken and have
1255 * an illegal "." in them.
1257 (event->flags & EVENT_FL_ISFTRACE &&
1258 type == EVENT_OP && strcmp(token, ".") == 0)) {
1260 if (strcmp(token, "*") == 0)
1261 field->flags |= FIELD_IS_POINTER;
1263 if (field->type) {
1264 char *new_type;
1265 new_type = realloc(field->type,
1266 strlen(field->type) +
1267 strlen(last_token) + 2);
1268 if (!new_type) {
1269 free(last_token);
1270 goto fail;
1272 field->type = new_type;
1273 strcat(field->type, " ");
1274 strcat(field->type, last_token);
1275 free(last_token);
1276 } else
1277 field->type = last_token;
1278 last_token = token;
1279 continue;
1282 break;
1285 if (!field->type) {
1286 die("no type found");
1287 goto fail;
1289 field->name = last_token;
1291 if (test_type(type, EVENT_OP))
1292 goto fail;
1294 if (strcmp(token, "[") == 0) {
1295 enum event_type last_type = type;
1296 char *brackets = token;
1297 char *new_brackets;
1298 int len;
1300 field->flags |= FIELD_IS_ARRAY;
1302 type = read_token(&token);
1304 if (type == EVENT_ITEM)
1305 field->arraylen = strtoul(token, NULL, 0);
1306 else
1307 field->arraylen = 0;
1309 while (strcmp(token, "]") != 0) {
1310 if (last_type == EVENT_ITEM &&
1311 type == EVENT_ITEM)
1312 len = 2;
1313 else
1314 len = 1;
1315 last_type = type;
1317 new_brackets = realloc(brackets,
1318 strlen(brackets) +
1319 strlen(token) + len);
1320 if (!new_brackets) {
1321 free(brackets);
1322 goto fail;
1324 brackets = new_brackets;
1325 if (len == 2)
1326 strcat(brackets, " ");
1327 strcat(brackets, token);
1328 /* We only care about the last token */
1329 field->arraylen = strtoul(token, NULL, 0);
1330 free_token(token);
1331 type = read_token(&token);
1332 if (type == EVENT_NONE) {
1333 die("failed to find token");
1334 goto fail;
1338 free_token(token);
1340 new_brackets = realloc(brackets, strlen(brackets) + 2);
1341 if (!new_brackets) {
1342 free(brackets);
1343 goto fail;
1345 brackets = new_brackets;
1346 strcat(brackets, "]");
1348 /* add brackets to type */
1350 type = read_token(&token);
1352 * If the next token is not an OP, then it is of
1353 * the format: type [] item;
1355 if (type == EVENT_ITEM) {
1356 char *new_type;
1357 new_type = realloc(field->type,
1358 strlen(field->type) +
1359 strlen(field->name) +
1360 strlen(brackets) + 2);
1361 if (!new_type) {
1362 free(brackets);
1363 goto fail;
1365 field->type = new_type;
1366 strcat(field->type, " ");
1367 strcat(field->type, field->name);
1368 free_token(field->name);
1369 strcat(field->type, brackets);
1370 field->name = token;
1371 type = read_token(&token);
1372 } else {
1373 char *new_type;
1374 new_type = realloc(field->type,
1375 strlen(field->type) +
1376 strlen(brackets) + 1);
1377 if (!new_type) {
1378 free(brackets);
1379 goto fail;
1381 field->type = new_type;
1382 strcat(field->type, brackets);
1384 free(brackets);
1387 if (field_is_string(field))
1388 field->flags |= FIELD_IS_STRING;
1389 if (field_is_dynamic(field))
1390 field->flags |= FIELD_IS_DYNAMIC;
1391 if (field_is_long(field))
1392 field->flags |= FIELD_IS_LONG;
1394 if (test_type_token(type, token, EVENT_OP, ";"))
1395 goto fail;
1396 free_token(token);
1398 if (read_expected(EVENT_ITEM, "offset") < 0)
1399 goto fail_expect;
1401 if (read_expected(EVENT_OP, ":") < 0)
1402 goto fail_expect;
1404 if (read_expect_type(EVENT_ITEM, &token))
1405 goto fail;
1406 field->offset = strtoul(token, NULL, 0);
1407 free_token(token);
1409 if (read_expected(EVENT_OP, ";") < 0)
1410 goto fail_expect;
1412 if (read_expected(EVENT_ITEM, "size") < 0)
1413 goto fail_expect;
1415 if (read_expected(EVENT_OP, ":") < 0)
1416 goto fail_expect;
1418 if (read_expect_type(EVENT_ITEM, &token))
1419 goto fail;
1420 field->size = strtoul(token, NULL, 0);
1421 free_token(token);
1423 if (read_expected(EVENT_OP, ";") < 0)
1424 goto fail_expect;
1426 type = read_token(&token);
1427 if (type != EVENT_NEWLINE) {
1428 /* newer versions of the kernel have a "signed" type */
1429 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1430 goto fail;
1432 free_token(token);
1434 if (read_expected(EVENT_OP, ":") < 0)
1435 goto fail_expect;
1437 if (read_expect_type(EVENT_ITEM, &token))
1438 goto fail;
1440 /* add signed type */
1442 free_token(token);
1443 if (read_expected(EVENT_OP, ";") < 0)
1444 goto fail_expect;
1446 if (read_expect_type(EVENT_NEWLINE, &token))
1447 goto fail;
1450 free_token(token);
1452 if (field->flags & FIELD_IS_ARRAY) {
1453 if (field->arraylen)
1454 field->elementsize = field->size / field->arraylen;
1455 else if (field->flags & FIELD_IS_STRING)
1456 field->elementsize = 1;
1457 else
1458 field->elementsize = event->pevent->long_size;
1459 } else
1460 field->elementsize = field->size;
1462 *fields = field;
1463 fields = &field->next;
1465 } while (1);
1467 return 0;
1469 fail:
1470 free_token(token);
1471 fail_expect:
1472 if (field) {
1473 free(field->type);
1474 free(field->name);
1475 free(field);
1477 return -1;
1480 static int event_read_format(struct event_format *event)
1482 char *token;
1483 int ret;
1485 if (read_expected_item(EVENT_ITEM, "format") < 0)
1486 return -1;
1488 if (read_expected(EVENT_OP, ":") < 0)
1489 return -1;
1491 if (read_expect_type(EVENT_NEWLINE, &token))
1492 goto fail;
1493 free_token(token);
1495 ret = event_read_fields(event, &event->format.common_fields);
1496 if (ret < 0)
1497 return ret;
1498 event->format.nr_common = ret;
1500 ret = event_read_fields(event, &event->format.fields);
1501 if (ret < 0)
1502 return ret;
1503 event->format.nr_fields = ret;
1505 return 0;
1507 fail:
1508 free_token(token);
1509 return -1;
1512 static enum event_type
1513 process_arg_token(struct event_format *event, struct print_arg *arg,
1514 char **tok, enum event_type type);
1516 static enum event_type
1517 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1519 enum event_type type;
1520 char *token;
1522 type = read_token(&token);
1523 *tok = token;
1525 return process_arg_token(event, arg, tok, type);
1528 static enum event_type
1529 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1531 static enum event_type
1532 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1534 struct print_arg *arg, *left, *right;
1535 enum event_type type;
1536 char *token = NULL;
1538 arg = alloc_arg();
1539 left = alloc_arg();
1540 right = alloc_arg();
1542 arg->type = PRINT_OP;
1543 arg->op.left = left;
1544 arg->op.right = right;
1546 *tok = NULL;
1547 type = process_arg(event, left, &token);
1549 again:
1550 /* Handle other operations in the arguments */
1551 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1552 type = process_op(event, left, &token);
1553 goto again;
1556 if (test_type_token(type, token, EVENT_OP, ":"))
1557 goto out_free;
1559 arg->op.op = token;
1561 type = process_arg(event, right, &token);
1563 top->op.right = arg;
1565 *tok = token;
1566 return type;
1568 out_free:
1569 /* Top may point to itself */
1570 top->op.right = NULL;
1571 free_token(token);
1572 free_arg(arg);
1573 return EVENT_ERROR;
1576 static enum event_type
1577 process_array(struct event_format *event, struct print_arg *top, char **tok)
1579 struct print_arg *arg;
1580 enum event_type type;
1581 char *token = NULL;
1583 arg = alloc_arg();
1585 *tok = NULL;
1586 type = process_arg(event, arg, &token);
1587 if (test_type_token(type, token, EVENT_OP, "]"))
1588 goto out_free;
1590 top->op.right = arg;
1592 free_token(token);
1593 type = read_token_item(&token);
1594 *tok = token;
1596 return type;
1598 out_free:
1599 free_token(*tok);
1600 *tok = NULL;
1601 free_arg(arg);
1602 return EVENT_ERROR;
1605 static int get_op_prio(char *op)
1607 if (!op[1]) {
1608 switch (op[0]) {
1609 case '~':
1610 case '!':
1611 return 4;
1612 case '*':
1613 case '/':
1614 case '%':
1615 return 6;
1616 case '+':
1617 case '-':
1618 return 7;
1619 /* '>>' and '<<' are 8 */
1620 case '<':
1621 case '>':
1622 return 9;
1623 /* '==' and '!=' are 10 */
1624 case '&':
1625 return 11;
1626 case '^':
1627 return 12;
1628 case '|':
1629 return 13;
1630 case '?':
1631 return 16;
1632 default:
1633 do_warning("unknown op '%c'", op[0]);
1634 return -1;
1636 } else {
1637 if (strcmp(op, "++") == 0 ||
1638 strcmp(op, "--") == 0) {
1639 return 3;
1640 } else if (strcmp(op, ">>") == 0 ||
1641 strcmp(op, "<<") == 0) {
1642 return 8;
1643 } else if (strcmp(op, ">=") == 0 ||
1644 strcmp(op, "<=") == 0) {
1645 return 9;
1646 } else if (strcmp(op, "==") == 0 ||
1647 strcmp(op, "!=") == 0) {
1648 return 10;
1649 } else if (strcmp(op, "&&") == 0) {
1650 return 14;
1651 } else if (strcmp(op, "||") == 0) {
1652 return 15;
1653 } else {
1654 do_warning("unknown op '%s'", op);
1655 return -1;
1660 static int set_op_prio(struct print_arg *arg)
1663 /* single ops are the greatest */
1664 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1665 arg->op.prio = 0;
1666 else
1667 arg->op.prio = get_op_prio(arg->op.op);
1669 return arg->op.prio;
1672 /* Note, *tok does not get freed, but will most likely be saved */
1673 static enum event_type
1674 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1676 struct print_arg *left, *right = NULL;
1677 enum event_type type;
1678 char *token;
1680 /* the op is passed in via tok */
1681 token = *tok;
1683 if (arg->type == PRINT_OP && !arg->op.left) {
1684 /* handle single op */
1685 if (token[1]) {
1686 die("bad op token %s", token);
1687 goto out_free;
1689 switch (token[0]) {
1690 case '~':
1691 case '!':
1692 case '+':
1693 case '-':
1694 break;
1695 default:
1696 do_warning("bad op token %s", token);
1697 goto out_free;
1701 /* make an empty left */
1702 left = alloc_arg();
1703 left->type = PRINT_NULL;
1704 arg->op.left = left;
1706 right = alloc_arg();
1707 arg->op.right = right;
1709 /* do not free the token, it belongs to an op */
1710 *tok = NULL;
1711 type = process_arg(event, right, tok);
1713 } else if (strcmp(token, "?") == 0) {
1715 left = alloc_arg();
1716 /* copy the top arg to the left */
1717 *left = *arg;
1719 arg->type = PRINT_OP;
1720 arg->op.op = token;
1721 arg->op.left = left;
1722 arg->op.prio = 0;
1724 type = process_cond(event, arg, tok);
1726 } else if (strcmp(token, ">>") == 0 ||
1727 strcmp(token, "<<") == 0 ||
1728 strcmp(token, "&") == 0 ||
1729 strcmp(token, "|") == 0 ||
1730 strcmp(token, "&&") == 0 ||
1731 strcmp(token, "||") == 0 ||
1732 strcmp(token, "-") == 0 ||
1733 strcmp(token, "+") == 0 ||
1734 strcmp(token, "*") == 0 ||
1735 strcmp(token, "^") == 0 ||
1736 strcmp(token, "/") == 0 ||
1737 strcmp(token, "<") == 0 ||
1738 strcmp(token, ">") == 0 ||
1739 strcmp(token, "==") == 0 ||
1740 strcmp(token, "!=") == 0) {
1742 left = alloc_arg();
1744 /* copy the top arg to the left */
1745 *left = *arg;
1747 arg->type = PRINT_OP;
1748 arg->op.op = token;
1749 arg->op.left = left;
1751 if (set_op_prio(arg) == -1) {
1752 event->flags |= EVENT_FL_FAILED;
1753 /* arg->op.op (= token) will be freed at out_free */
1754 arg->op.op = NULL;
1755 goto out_free;
1758 type = read_token_item(&token);
1759 *tok = token;
1761 /* could just be a type pointer */
1762 if ((strcmp(arg->op.op, "*") == 0) &&
1763 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1764 char *new_atom;
1766 if (left->type != PRINT_ATOM)
1767 die("bad pointer type");
1768 new_atom = realloc(left->atom.atom,
1769 strlen(left->atom.atom) + 3);
1770 if (!new_atom)
1771 goto out_free;
1773 left->atom.atom = new_atom;
1774 strcat(left->atom.atom, " *");
1775 free(arg->op.op);
1776 *arg = *left;
1777 free(left);
1779 return type;
1782 right = alloc_arg();
1783 type = process_arg_token(event, right, tok, type);
1784 arg->op.right = right;
1786 } else if (strcmp(token, "[") == 0) {
1788 left = alloc_arg();
1789 *left = *arg;
1791 arg->type = PRINT_OP;
1792 arg->op.op = token;
1793 arg->op.left = left;
1795 arg->op.prio = 0;
1797 type = process_array(event, arg, tok);
1799 } else {
1800 do_warning("unknown op '%s'", token);
1801 event->flags |= EVENT_FL_FAILED;
1802 /* the arg is now the left side */
1803 goto out_free;
1806 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1807 int prio;
1809 /* higher prios need to be closer to the root */
1810 prio = get_op_prio(*tok);
1812 if (prio > arg->op.prio)
1813 return process_op(event, arg, tok);
1815 return process_op(event, right, tok);
1818 return type;
1820 out_free:
1821 free_token(token);
1822 *tok = NULL;
1823 return EVENT_ERROR;
1826 static enum event_type
1827 process_entry(struct event_format *event __unused, struct print_arg *arg,
1828 char **tok)
1830 enum event_type type;
1831 char *field;
1832 char *token;
1834 if (read_expected(EVENT_OP, "->") < 0)
1835 goto out_err;
1837 if (read_expect_type(EVENT_ITEM, &token) < 0)
1838 goto out_free;
1839 field = token;
1841 arg->type = PRINT_FIELD;
1842 arg->field.name = field;
1844 if (is_flag_field) {
1845 arg->field.field = pevent_find_any_field(event, arg->field.name);
1846 arg->field.field->flags |= FIELD_IS_FLAG;
1847 is_flag_field = 0;
1848 } else if (is_symbolic_field) {
1849 arg->field.field = pevent_find_any_field(event, arg->field.name);
1850 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1851 is_symbolic_field = 0;
1854 type = read_token(&token);
1855 *tok = token;
1857 return type;
1859 out_free:
1860 free_token(token);
1861 out_err:
1862 *tok = NULL;
1863 return EVENT_ERROR;
1866 static char *arg_eval (struct print_arg *arg);
1868 static unsigned long long
1869 eval_type_str(unsigned long long val, const char *type, int pointer)
1871 int sign = 0;
1872 char *ref;
1873 int len;
1875 len = strlen(type);
1877 if (pointer) {
1879 if (type[len-1] != '*') {
1880 do_warning("pointer expected with non pointer type");
1881 return val;
1884 ref = malloc_or_die(len);
1885 memcpy(ref, type, len);
1887 /* chop off the " *" */
1888 ref[len - 2] = 0;
1890 val = eval_type_str(val, ref, 0);
1891 free(ref);
1892 return val;
1895 /* check if this is a pointer */
1896 if (type[len - 1] == '*')
1897 return val;
1899 /* Try to figure out the arg size*/
1900 if (strncmp(type, "struct", 6) == 0)
1901 /* all bets off */
1902 return val;
1904 if (strcmp(type, "u8") == 0)
1905 return val & 0xff;
1907 if (strcmp(type, "u16") == 0)
1908 return val & 0xffff;
1910 if (strcmp(type, "u32") == 0)
1911 return val & 0xffffffff;
1913 if (strcmp(type, "u64") == 0 ||
1914 strcmp(type, "s64"))
1915 return val;
1917 if (strcmp(type, "s8") == 0)
1918 return (unsigned long long)(char)val & 0xff;
1920 if (strcmp(type, "s16") == 0)
1921 return (unsigned long long)(short)val & 0xffff;
1923 if (strcmp(type, "s32") == 0)
1924 return (unsigned long long)(int)val & 0xffffffff;
1926 if (strncmp(type, "unsigned ", 9) == 0) {
1927 sign = 0;
1928 type += 9;
1931 if (strcmp(type, "char") == 0) {
1932 if (sign)
1933 return (unsigned long long)(char)val & 0xff;
1934 else
1935 return val & 0xff;
1938 if (strcmp(type, "short") == 0) {
1939 if (sign)
1940 return (unsigned long long)(short)val & 0xffff;
1941 else
1942 return val & 0xffff;
1945 if (strcmp(type, "int") == 0) {
1946 if (sign)
1947 return (unsigned long long)(int)val & 0xffffffff;
1948 else
1949 return val & 0xffffffff;
1952 return val;
1956 * Try to figure out the type.
1958 static unsigned long long
1959 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1961 if (arg->type != PRINT_TYPE)
1962 die("expected type argument");
1964 return eval_type_str(val, arg->typecast.type, pointer);
1967 static int arg_num_eval(struct print_arg *arg, long long *val)
1969 long long left, right;
1970 int ret = 1;
1972 switch (arg->type) {
1973 case PRINT_ATOM:
1974 *val = strtoll(arg->atom.atom, NULL, 0);
1975 break;
1976 case PRINT_TYPE:
1977 ret = arg_num_eval(arg->typecast.item, val);
1978 if (!ret)
1979 break;
1980 *val = eval_type(*val, arg, 0);
1981 break;
1982 case PRINT_OP:
1983 switch (arg->op.op[0]) {
1984 case '|':
1985 ret = arg_num_eval(arg->op.left, &left);
1986 if (!ret)
1987 break;
1988 ret = arg_num_eval(arg->op.right, &right);
1989 if (!ret)
1990 break;
1991 if (arg->op.op[1])
1992 *val = left || right;
1993 else
1994 *val = left | right;
1995 break;
1996 case '&':
1997 ret = arg_num_eval(arg->op.left, &left);
1998 if (!ret)
1999 break;
2000 ret = arg_num_eval(arg->op.right, &right);
2001 if (!ret)
2002 break;
2003 if (arg->op.op[1])
2004 *val = left && right;
2005 else
2006 *val = left & right;
2007 break;
2008 case '<':
2009 ret = arg_num_eval(arg->op.left, &left);
2010 if (!ret)
2011 break;
2012 ret = arg_num_eval(arg->op.right, &right);
2013 if (!ret)
2014 break;
2015 switch (arg->op.op[1]) {
2016 case 0:
2017 *val = left < right;
2018 break;
2019 case '<':
2020 *val = left << right;
2021 break;
2022 case '=':
2023 *val = left <= right;
2024 break;
2025 default:
2026 do_warning("unknown op '%s'", arg->op.op);
2027 ret = 0;
2029 break;
2030 case '>':
2031 ret = arg_num_eval(arg->op.left, &left);
2032 if (!ret)
2033 break;
2034 ret = arg_num_eval(arg->op.right, &right);
2035 if (!ret)
2036 break;
2037 switch (arg->op.op[1]) {
2038 case 0:
2039 *val = left > right;
2040 break;
2041 case '>':
2042 *val = left >> right;
2043 break;
2044 case '=':
2045 *val = left >= right;
2046 break;
2047 default:
2048 do_warning("unknown op '%s'", arg->op.op);
2049 ret = 0;
2051 break;
2052 case '=':
2053 ret = arg_num_eval(arg->op.left, &left);
2054 if (!ret)
2055 break;
2056 ret = arg_num_eval(arg->op.right, &right);
2057 if (!ret)
2058 break;
2060 if (arg->op.op[1] != '=') {
2061 do_warning("unknown op '%s'", arg->op.op);
2062 ret = 0;
2063 } else
2064 *val = left == right;
2065 break;
2066 case '!':
2067 ret = arg_num_eval(arg->op.left, &left);
2068 if (!ret)
2069 break;
2070 ret = arg_num_eval(arg->op.right, &right);
2071 if (!ret)
2072 break;
2074 switch (arg->op.op[1]) {
2075 case '=':
2076 *val = left != right;
2077 break;
2078 default:
2079 do_warning("unknown op '%s'", arg->op.op);
2080 ret = 0;
2082 break;
2083 case '-':
2084 /* check for negative */
2085 if (arg->op.left->type == PRINT_NULL)
2086 left = 0;
2087 else
2088 ret = arg_num_eval(arg->op.left, &left);
2089 if (!ret)
2090 break;
2091 ret = arg_num_eval(arg->op.right, &right);
2092 if (!ret)
2093 break;
2094 *val = left - right;
2095 break;
2096 case '+':
2097 if (arg->op.left->type == PRINT_NULL)
2098 left = 0;
2099 else
2100 ret = arg_num_eval(arg->op.left, &left);
2101 if (!ret)
2102 break;
2103 ret = arg_num_eval(arg->op.right, &right);
2104 if (!ret)
2105 break;
2106 *val = left + right;
2107 break;
2108 default:
2109 do_warning("unknown op '%s'", arg->op.op);
2110 ret = 0;
2112 break;
2114 case PRINT_NULL:
2115 case PRINT_FIELD ... PRINT_SYMBOL:
2116 case PRINT_STRING:
2117 case PRINT_BSTRING:
2118 default:
2119 do_warning("invalid eval type %d", arg->type);
2120 ret = 0;
2123 return ret;
2126 static char *arg_eval (struct print_arg *arg)
2128 long long val;
2129 static char buf[20];
2131 switch (arg->type) {
2132 case PRINT_ATOM:
2133 return arg->atom.atom;
2134 case PRINT_TYPE:
2135 return arg_eval(arg->typecast.item);
2136 case PRINT_OP:
2137 if (!arg_num_eval(arg, &val))
2138 break;
2139 sprintf(buf, "%lld", val);
2140 return buf;
2142 case PRINT_NULL:
2143 case PRINT_FIELD ... PRINT_SYMBOL:
2144 case PRINT_STRING:
2145 case PRINT_BSTRING:
2146 default:
2147 die("invalid eval type %d", arg->type);
2148 break;
2151 return NULL;
2154 static enum event_type
2155 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2157 enum event_type type;
2158 struct print_arg *arg = NULL;
2159 struct print_flag_sym *field;
2160 char *token = *tok;
2161 char *value;
2163 do {
2164 free_token(token);
2165 type = read_token_item(&token);
2166 if (test_type_token(type, token, EVENT_OP, "{"))
2167 break;
2169 arg = alloc_arg();
2171 free_token(token);
2172 type = process_arg(event, arg, &token);
2174 if (type == EVENT_OP)
2175 type = process_op(event, arg, &token);
2177 if (type == EVENT_ERROR)
2178 goto out_free;
2180 if (test_type_token(type, token, EVENT_DELIM, ","))
2181 goto out_free;
2183 field = malloc_or_die(sizeof(*field));
2184 memset(field, 0, sizeof(*field));
2186 value = arg_eval(arg);
2187 if (value == NULL)
2188 goto out_free;
2189 field->value = strdup(value);
2190 if (field->value == NULL)
2191 goto out_free;
2193 free_arg(arg);
2194 arg = alloc_arg();
2196 free_token(token);
2197 type = process_arg(event, arg, &token);
2198 if (test_type_token(type, token, EVENT_OP, "}"))
2199 goto out_free;
2201 value = arg_eval(arg);
2202 if (value == NULL)
2203 goto out_free;
2204 field->str = strdup(value);
2205 if (field->str == NULL)
2206 goto out_free;
2207 free_arg(arg);
2208 arg = NULL;
2210 *list = field;
2211 list = &field->next;
2213 free_token(token);
2214 type = read_token_item(&token);
2215 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2217 *tok = token;
2218 return type;
2220 out_free:
2221 free_arg(arg);
2222 free_token(token);
2223 *tok = NULL;
2225 return EVENT_ERROR;
2228 static enum event_type
2229 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2231 struct print_arg *field;
2232 enum event_type type;
2233 char *token;
2235 memset(arg, 0, sizeof(*arg));
2236 arg->type = PRINT_FLAGS;
2238 field = alloc_arg();
2240 type = process_arg(event, field, &token);
2242 /* Handle operations in the first argument */
2243 while (type == EVENT_OP)
2244 type = process_op(event, field, &token);
2246 if (test_type_token(type, token, EVENT_DELIM, ","))
2247 goto out_free;
2248 free_token(token);
2250 arg->flags.field = field;
2252 type = read_token_item(&token);
2253 if (event_item_type(type)) {
2254 arg->flags.delim = token;
2255 type = read_token_item(&token);
2258 if (test_type_token(type, token, EVENT_DELIM, ","))
2259 goto out_free;
2261 type = process_fields(event, &arg->flags.flags, &token);
2262 if (test_type_token(type, token, EVENT_DELIM, ")"))
2263 goto out_free;
2265 free_token(token);
2266 type = read_token_item(tok);
2267 return type;
2269 out_free:
2270 free_token(token);
2271 *tok = NULL;
2272 return EVENT_ERROR;
2275 static enum event_type
2276 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2278 struct print_arg *field;
2279 enum event_type type;
2280 char *token;
2282 memset(arg, 0, sizeof(*arg));
2283 arg->type = PRINT_SYMBOL;
2285 field = alloc_arg();
2287 type = process_arg(event, field, &token);
2288 if (test_type_token(type, token, EVENT_DELIM, ","))
2289 goto out_free;
2291 arg->symbol.field = field;
2293 type = process_fields(event, &arg->symbol.symbols, &token);
2294 if (test_type_token(type, token, EVENT_DELIM, ")"))
2295 goto out_free;
2297 free_token(token);
2298 type = read_token_item(tok);
2299 return type;
2301 out_free:
2302 free_token(token);
2303 *tok = NULL;
2304 return EVENT_ERROR;
2307 static enum event_type
2308 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2310 struct print_arg *field;
2311 enum event_type type;
2312 char *token;
2314 memset(arg, 0, sizeof(*arg));
2315 arg->type = PRINT_HEX;
2317 field = alloc_arg();
2318 type = process_arg(event, field, &token);
2320 if (test_type_token(type, token, EVENT_DELIM, ","))
2321 goto out_free;
2323 arg->hex.field = field;
2325 free_token(token);
2327 field = alloc_arg();
2328 type = process_arg(event, field, &token);
2330 if (test_type_token(type, token, EVENT_DELIM, ")"))
2331 goto out_free;
2333 arg->hex.size = field;
2335 free_token(token);
2336 type = read_token_item(tok);
2337 return type;
2339 out_free:
2340 free_arg(field);
2341 free_token(token);
2342 *tok = NULL;
2343 return EVENT_ERROR;
2346 static enum event_type
2347 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2349 struct format_field *field;
2350 enum event_type type;
2351 char *token;
2353 memset(arg, 0, sizeof(*arg));
2354 arg->type = PRINT_DYNAMIC_ARRAY;
2357 * The item within the parenthesis is another field that holds
2358 * the index into where the array starts.
2360 type = read_token(&token);
2361 *tok = token;
2362 if (type != EVENT_ITEM)
2363 goto out_free;
2365 /* Find the field */
2367 field = pevent_find_field(event, token);
2368 if (!field)
2369 goto out_free;
2371 arg->dynarray.field = field;
2372 arg->dynarray.index = 0;
2374 if (read_expected(EVENT_DELIM, ")") < 0)
2375 goto out_free;
2377 free_token(token);
2378 type = read_token_item(&token);
2379 *tok = token;
2380 if (type != EVENT_OP || strcmp(token, "[") != 0)
2381 return type;
2383 free_token(token);
2384 arg = alloc_arg();
2385 type = process_arg(event, arg, &token);
2386 if (type == EVENT_ERROR)
2387 goto out_free_arg;
2389 if (!test_type_token(type, token, EVENT_OP, "]"))
2390 goto out_free_arg;
2392 free_token(token);
2393 type = read_token_item(tok);
2394 return type;
2396 out_free_arg:
2397 free_arg(arg);
2398 out_free:
2399 free_token(token);
2400 *tok = NULL;
2401 return EVENT_ERROR;
2404 static enum event_type
2405 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2407 struct print_arg *item_arg;
2408 enum event_type type;
2409 char *token;
2411 type = process_arg(event, arg, &token);
2413 if (type == EVENT_ERROR)
2414 goto out_free;
2416 if (type == EVENT_OP)
2417 type = process_op(event, arg, &token);
2419 if (type == EVENT_ERROR)
2420 goto out_free;
2422 if (test_type_token(type, token, EVENT_DELIM, ")"))
2423 goto out_free;
2425 free_token(token);
2426 type = read_token_item(&token);
2429 * If the next token is an item or another open paren, then
2430 * this was a typecast.
2432 if (event_item_type(type) ||
2433 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2435 /* make this a typecast and contine */
2437 /* prevous must be an atom */
2438 if (arg->type != PRINT_ATOM)
2439 die("previous needed to be PRINT_ATOM");
2441 item_arg = alloc_arg();
2443 arg->type = PRINT_TYPE;
2444 arg->typecast.type = arg->atom.atom;
2445 arg->typecast.item = item_arg;
2446 type = process_arg_token(event, item_arg, &token, type);
2450 *tok = token;
2451 return type;
2453 out_free:
2454 free_token(token);
2455 *tok = NULL;
2456 return EVENT_ERROR;
2460 static enum event_type
2461 process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2463 enum event_type type;
2464 char *token;
2466 if (read_expect_type(EVENT_ITEM, &token) < 0)
2467 goto out_free;
2469 arg->type = PRINT_STRING;
2470 arg->string.string = token;
2471 arg->string.offset = -1;
2473 if (read_expected(EVENT_DELIM, ")") < 0)
2474 goto out_err;
2476 type = read_token(&token);
2477 *tok = token;
2479 return type;
2481 out_free:
2482 free_token(token);
2483 out_err:
2484 *tok = NULL;
2485 return EVENT_ERROR;
2488 static struct pevent_function_handler *
2489 find_func_handler(struct pevent *pevent, char *func_name)
2491 struct pevent_function_handler *func;
2493 for (func = pevent->func_handlers; func; func = func->next) {
2494 if (strcmp(func->name, func_name) == 0)
2495 break;
2498 return func;
2501 static void remove_func_handler(struct pevent *pevent, char *func_name)
2503 struct pevent_function_handler *func;
2504 struct pevent_function_handler **next;
2506 next = &pevent->func_handlers;
2507 while ((func = *next)) {
2508 if (strcmp(func->name, func_name) == 0) {
2509 *next = func->next;
2510 free_func_handle(func);
2511 break;
2513 next = &func->next;
2517 static enum event_type
2518 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2519 struct print_arg *arg, char **tok)
2521 struct print_arg **next_arg;
2522 struct print_arg *farg;
2523 enum event_type type;
2524 char *token;
2525 char *test;
2526 int i;
2528 arg->type = PRINT_FUNC;
2529 arg->func.func = func;
2531 *tok = NULL;
2533 next_arg = &(arg->func.args);
2534 for (i = 0; i < func->nr_args; i++) {
2535 farg = alloc_arg();
2536 type = process_arg(event, farg, &token);
2537 if (i < (func->nr_args - 1))
2538 test = ",";
2539 else
2540 test = ")";
2542 if (test_type_token(type, token, EVENT_DELIM, test)) {
2543 free_arg(farg);
2544 free_token(token);
2545 return EVENT_ERROR;
2548 *next_arg = farg;
2549 next_arg = &(farg->next);
2550 free_token(token);
2553 type = read_token(&token);
2554 *tok = token;
2556 return type;
2559 static enum event_type
2560 process_function(struct event_format *event, struct print_arg *arg,
2561 char *token, char **tok)
2563 struct pevent_function_handler *func;
2565 if (strcmp(token, "__print_flags") == 0) {
2566 free_token(token);
2567 is_flag_field = 1;
2568 return process_flags(event, arg, tok);
2570 if (strcmp(token, "__print_symbolic") == 0) {
2571 free_token(token);
2572 is_symbolic_field = 1;
2573 return process_symbols(event, arg, tok);
2575 if (strcmp(token, "__print_hex") == 0) {
2576 free_token(token);
2577 return process_hex(event, arg, tok);
2579 if (strcmp(token, "__get_str") == 0) {
2580 free_token(token);
2581 return process_str(event, arg, tok);
2583 if (strcmp(token, "__get_dynamic_array") == 0) {
2584 free_token(token);
2585 return process_dynamic_array(event, arg, tok);
2588 func = find_func_handler(event->pevent, token);
2589 if (func) {
2590 free_token(token);
2591 return process_func_handler(event, func, arg, tok);
2594 do_warning("function %s not defined", token);
2595 free_token(token);
2596 return EVENT_ERROR;
2599 static enum event_type
2600 process_arg_token(struct event_format *event, struct print_arg *arg,
2601 char **tok, enum event_type type)
2603 char *token;
2604 char *atom;
2606 token = *tok;
2608 switch (type) {
2609 case EVENT_ITEM:
2610 if (strcmp(token, "REC") == 0) {
2611 free_token(token);
2612 type = process_entry(event, arg, &token);
2613 break;
2615 atom = token;
2616 /* test the next token */
2617 type = read_token_item(&token);
2620 * If the next token is a parenthesis, then this
2621 * is a function.
2623 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2624 free_token(token);
2625 token = NULL;
2626 /* this will free atom. */
2627 type = process_function(event, arg, atom, &token);
2628 break;
2630 /* atoms can be more than one token long */
2631 while (type == EVENT_ITEM) {
2632 char *new_atom;
2633 new_atom = realloc(atom,
2634 strlen(atom) + strlen(token) + 2);
2635 if (!new_atom) {
2636 free(atom);
2637 *tok = NULL;
2638 free_token(token);
2639 return EVENT_ERROR;
2641 atom = new_atom;
2642 strcat(atom, " ");
2643 strcat(atom, token);
2644 free_token(token);
2645 type = read_token_item(&token);
2648 arg->type = PRINT_ATOM;
2649 arg->atom.atom = atom;
2650 break;
2652 case EVENT_DQUOTE:
2653 case EVENT_SQUOTE:
2654 arg->type = PRINT_ATOM;
2655 arg->atom.atom = token;
2656 type = read_token_item(&token);
2657 break;
2658 case EVENT_DELIM:
2659 if (strcmp(token, "(") == 0) {
2660 free_token(token);
2661 type = process_paren(event, arg, &token);
2662 break;
2664 case EVENT_OP:
2665 /* handle single ops */
2666 arg->type = PRINT_OP;
2667 arg->op.op = token;
2668 arg->op.left = NULL;
2669 type = process_op(event, arg, &token);
2671 /* On error, the op is freed */
2672 if (type == EVENT_ERROR)
2673 arg->op.op = NULL;
2675 /* return error type if errored */
2676 break;
2678 case EVENT_ERROR ... EVENT_NEWLINE:
2679 default:
2680 die("unexpected type %d", type);
2682 *tok = token;
2684 return type;
2687 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2689 enum event_type type = EVENT_ERROR;
2690 struct print_arg *arg;
2691 char *token;
2692 int args = 0;
2694 do {
2695 if (type == EVENT_NEWLINE) {
2696 type = read_token_item(&token);
2697 continue;
2700 arg = alloc_arg();
2702 type = process_arg(event, arg, &token);
2704 if (type == EVENT_ERROR) {
2705 free_token(token);
2706 free_arg(arg);
2707 return -1;
2710 *list = arg;
2711 args++;
2713 if (type == EVENT_OP) {
2714 type = process_op(event, arg, &token);
2715 free_token(token);
2716 if (type == EVENT_ERROR) {
2717 *list = NULL;
2718 free_arg(arg);
2719 return -1;
2721 list = &arg->next;
2722 continue;
2725 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2726 free_token(token);
2727 *list = arg;
2728 list = &arg->next;
2729 continue;
2731 break;
2732 } while (type != EVENT_NONE);
2734 if (type != EVENT_NONE && type != EVENT_ERROR)
2735 free_token(token);
2737 return args;
2740 static int event_read_print(struct event_format *event)
2742 enum event_type type;
2743 char *token;
2744 int ret;
2746 if (read_expected_item(EVENT_ITEM, "print") < 0)
2747 return -1;
2749 if (read_expected(EVENT_ITEM, "fmt") < 0)
2750 return -1;
2752 if (read_expected(EVENT_OP, ":") < 0)
2753 return -1;
2755 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2756 goto fail;
2758 concat:
2759 event->print_fmt.format = token;
2760 event->print_fmt.args = NULL;
2762 /* ok to have no arg */
2763 type = read_token_item(&token);
2765 if (type == EVENT_NONE)
2766 return 0;
2768 /* Handle concatenation of print lines */
2769 if (type == EVENT_DQUOTE) {
2770 char *cat;
2772 cat = malloc_or_die(strlen(event->print_fmt.format) +
2773 strlen(token) + 1);
2774 strcpy(cat, event->print_fmt.format);
2775 strcat(cat, token);
2776 free_token(token);
2777 free_token(event->print_fmt.format);
2778 event->print_fmt.format = NULL;
2779 token = cat;
2780 goto concat;
2783 if (test_type_token(type, token, EVENT_DELIM, ","))
2784 goto fail;
2786 free_token(token);
2788 ret = event_read_print_args(event, &event->print_fmt.args);
2789 if (ret < 0)
2790 return -1;
2792 return ret;
2794 fail:
2795 free_token(token);
2796 return -1;
2800 * pevent_find_common_field - return a common field by event
2801 * @event: handle for the event
2802 * @name: the name of the common field to return
2804 * Returns a common field from the event by the given @name.
2805 * This only searchs the common fields and not all field.
2807 struct format_field *
2808 pevent_find_common_field(struct event_format *event, const char *name)
2810 struct format_field *format;
2812 for (format = event->format.common_fields;
2813 format; format = format->next) {
2814 if (strcmp(format->name, name) == 0)
2815 break;
2818 return format;
2822 * pevent_find_field - find a non-common field
2823 * @event: handle for the event
2824 * @name: the name of the non-common field
2826 * Returns a non-common field by the given @name.
2827 * This does not search common fields.
2829 struct format_field *
2830 pevent_find_field(struct event_format *event, const char *name)
2832 struct format_field *format;
2834 for (format = event->format.fields;
2835 format; format = format->next) {
2836 if (strcmp(format->name, name) == 0)
2837 break;
2840 return format;
2844 * pevent_find_any_field - find any field by name
2845 * @event: handle for the event
2846 * @name: the name of the field
2848 * Returns a field by the given @name.
2849 * This searchs the common field names first, then
2850 * the non-common ones if a common one was not found.
2852 struct format_field *
2853 pevent_find_any_field(struct event_format *event, const char *name)
2855 struct format_field *format;
2857 format = pevent_find_common_field(event, name);
2858 if (format)
2859 return format;
2860 return pevent_find_field(event, name);
2864 * pevent_read_number - read a number from data
2865 * @pevent: handle for the pevent
2866 * @ptr: the raw data
2867 * @size: the size of the data that holds the number
2869 * Returns the number (converted to host) from the
2870 * raw data.
2872 unsigned long long pevent_read_number(struct pevent *pevent,
2873 const void *ptr, int size)
2875 switch (size) {
2876 case 1:
2877 return *(unsigned char *)ptr;
2878 case 2:
2879 return data2host2(pevent, ptr);
2880 case 4:
2881 return data2host4(pevent, ptr);
2882 case 8:
2883 return data2host8(pevent, ptr);
2884 default:
2885 /* BUG! */
2886 return 0;
2891 * pevent_read_number_field - read a number from data
2892 * @field: a handle to the field
2893 * @data: the raw data to read
2894 * @value: the value to place the number in
2896 * Reads raw data according to a field offset and size,
2897 * and translates it into @value.
2899 * Returns 0 on success, -1 otherwise.
2901 int pevent_read_number_field(struct format_field *field, const void *data,
2902 unsigned long long *value)
2904 if (!field)
2905 return -1;
2906 switch (field->size) {
2907 case 1:
2908 case 2:
2909 case 4:
2910 case 8:
2911 *value = pevent_read_number(field->event->pevent,
2912 data + field->offset, field->size);
2913 return 0;
2914 default:
2915 return -1;
2919 static int get_common_info(struct pevent *pevent,
2920 const char *type, int *offset, int *size)
2922 struct event_format *event;
2923 struct format_field *field;
2926 * All events should have the same common elements.
2927 * Pick any event to find where the type is;
2929 if (!pevent->events)
2930 die("no event_list!");
2932 event = pevent->events[0];
2933 field = pevent_find_common_field(event, type);
2934 if (!field)
2935 return -1;
2937 *offset = field->offset;
2938 *size = field->size;
2940 return 0;
2943 static int __parse_common(struct pevent *pevent, void *data,
2944 int *size, int *offset, const char *name)
2946 int ret;
2948 if (!*size) {
2949 ret = get_common_info(pevent, name, offset, size);
2950 if (ret < 0)
2951 return ret;
2953 return pevent_read_number(pevent, data + *offset, *size);
2956 static int trace_parse_common_type(struct pevent *pevent, void *data)
2958 return __parse_common(pevent, data,
2959 &pevent->type_size, &pevent->type_offset,
2960 "common_type");
2963 static int parse_common_pid(struct pevent *pevent, void *data)
2965 return __parse_common(pevent, data,
2966 &pevent->pid_size, &pevent->pid_offset,
2967 "common_pid");
2970 static int parse_common_pc(struct pevent *pevent, void *data)
2972 return __parse_common(pevent, data,
2973 &pevent->pc_size, &pevent->pc_offset,
2974 "common_preempt_count");
2977 static int parse_common_flags(struct pevent *pevent, void *data)
2979 return __parse_common(pevent, data,
2980 &pevent->flags_size, &pevent->flags_offset,
2981 "common_flags");
2984 static int parse_common_lock_depth(struct pevent *pevent, void *data)
2986 return __parse_common(pevent, data,
2987 &pevent->ld_size, &pevent->ld_offset,
2988 "common_lock_depth");
2991 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2993 return __parse_common(pevent, data,
2994 &pevent->ld_size, &pevent->ld_offset,
2995 "common_migrate_disable");
2998 static int events_id_cmp(const void *a, const void *b);
3001 * pevent_find_event - find an event by given id
3002 * @pevent: a handle to the pevent
3003 * @id: the id of the event
3005 * Returns an event that has a given @id.
3007 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3009 struct event_format **eventptr;
3010 struct event_format key;
3011 struct event_format *pkey = &key;
3013 /* Check cache first */
3014 if (pevent->last_event && pevent->last_event->id == id)
3015 return pevent->last_event;
3017 key.id = id;
3019 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3020 sizeof(*pevent->events), events_id_cmp);
3022 if (eventptr) {
3023 pevent->last_event = *eventptr;
3024 return *eventptr;
3027 return NULL;
3031 * pevent_find_event_by_name - find an event by given name
3032 * @pevent: a handle to the pevent
3033 * @sys: the system name to search for
3034 * @name: the name of the event to search for
3036 * This returns an event with a given @name and under the system
3037 * @sys. If @sys is NULL the first event with @name is returned.
3039 struct event_format *
3040 pevent_find_event_by_name(struct pevent *pevent,
3041 const char *sys, const char *name)
3043 struct event_format *event;
3044 int i;
3046 if (pevent->last_event &&
3047 strcmp(pevent->last_event->name, name) == 0 &&
3048 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3049 return pevent->last_event;
3051 for (i = 0; i < pevent->nr_events; i++) {
3052 event = pevent->events[i];
3053 if (strcmp(event->name, name) == 0) {
3054 if (!sys)
3055 break;
3056 if (strcmp(event->system, sys) == 0)
3057 break;
3060 if (i == pevent->nr_events)
3061 event = NULL;
3063 pevent->last_event = event;
3064 return event;
3067 static unsigned long long
3068 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3070 struct pevent *pevent = event->pevent;
3071 unsigned long long val = 0;
3072 unsigned long long left, right;
3073 struct print_arg *typearg = NULL;
3074 struct print_arg *larg;
3075 unsigned long offset;
3076 unsigned int field_size;
3078 switch (arg->type) {
3079 case PRINT_NULL:
3080 /* ?? */
3081 return 0;
3082 case PRINT_ATOM:
3083 return strtoull(arg->atom.atom, NULL, 0);
3084 case PRINT_FIELD:
3085 if (!arg->field.field) {
3086 arg->field.field = pevent_find_any_field(event, arg->field.name);
3087 if (!arg->field.field)
3088 die("field %s not found", arg->field.name);
3090 /* must be a number */
3091 val = pevent_read_number(pevent, data + arg->field.field->offset,
3092 arg->field.field->size);
3093 break;
3094 case PRINT_FLAGS:
3095 case PRINT_SYMBOL:
3096 case PRINT_HEX:
3097 break;
3098 case PRINT_TYPE:
3099 val = eval_num_arg(data, size, event, arg->typecast.item);
3100 return eval_type(val, arg, 0);
3101 case PRINT_STRING:
3102 case PRINT_BSTRING:
3103 return 0;
3104 case PRINT_FUNC: {
3105 struct trace_seq s;
3106 trace_seq_init(&s);
3107 val = process_defined_func(&s, data, size, event, arg);
3108 trace_seq_destroy(&s);
3109 return val;
3111 case PRINT_OP:
3112 if (strcmp(arg->op.op, "[") == 0) {
3114 * Arrays are special, since we don't want
3115 * to read the arg as is.
3117 right = eval_num_arg(data, size, event, arg->op.right);
3119 /* handle typecasts */
3120 larg = arg->op.left;
3121 while (larg->type == PRINT_TYPE) {
3122 if (!typearg)
3123 typearg = larg;
3124 larg = larg->typecast.item;
3127 /* Default to long size */
3128 field_size = pevent->long_size;
3130 switch (larg->type) {
3131 case PRINT_DYNAMIC_ARRAY:
3132 offset = pevent_read_number(pevent,
3133 data + larg->dynarray.field->offset,
3134 larg->dynarray.field->size);
3135 if (larg->dynarray.field->elementsize)
3136 field_size = larg->dynarray.field->elementsize;
3138 * The actual length of the dynamic array is stored
3139 * in the top half of the field, and the offset
3140 * is in the bottom half of the 32 bit field.
3142 offset &= 0xffff;
3143 offset += right;
3144 break;
3145 case PRINT_FIELD:
3146 if (!larg->field.field) {
3147 larg->field.field =
3148 pevent_find_any_field(event, larg->field.name);
3149 if (!larg->field.field)
3150 die("field %s not found", larg->field.name);
3152 field_size = larg->field.field->elementsize;
3153 offset = larg->field.field->offset +
3154 right * larg->field.field->elementsize;
3155 break;
3156 default:
3157 goto default_op; /* oops, all bets off */
3159 val = pevent_read_number(pevent,
3160 data + offset, field_size);
3161 if (typearg)
3162 val = eval_type(val, typearg, 1);
3163 break;
3164 } else if (strcmp(arg->op.op, "?") == 0) {
3165 left = eval_num_arg(data, size, event, arg->op.left);
3166 arg = arg->op.right;
3167 if (left)
3168 val = eval_num_arg(data, size, event, arg->op.left);
3169 else
3170 val = eval_num_arg(data, size, event, arg->op.right);
3171 break;
3173 default_op:
3174 left = eval_num_arg(data, size, event, arg->op.left);
3175 right = eval_num_arg(data, size, event, arg->op.right);
3176 switch (arg->op.op[0]) {
3177 case '!':
3178 switch (arg->op.op[1]) {
3179 case 0:
3180 val = !right;
3181 break;
3182 case '=':
3183 val = left != right;
3184 break;
3185 default:
3186 die("unknown op '%s'", arg->op.op);
3188 break;
3189 case '~':
3190 val = ~right;
3191 break;
3192 case '|':
3193 if (arg->op.op[1])
3194 val = left || right;
3195 else
3196 val = left | right;
3197 break;
3198 case '&':
3199 if (arg->op.op[1])
3200 val = left && right;
3201 else
3202 val = left & right;
3203 break;
3204 case '<':
3205 switch (arg->op.op[1]) {
3206 case 0:
3207 val = left < right;
3208 break;
3209 case '<':
3210 val = left << right;
3211 break;
3212 case '=':
3213 val = left <= right;
3214 break;
3215 default:
3216 die("unknown op '%s'", arg->op.op);
3218 break;
3219 case '>':
3220 switch (arg->op.op[1]) {
3221 case 0:
3222 val = left > right;
3223 break;
3224 case '>':
3225 val = left >> right;
3226 break;
3227 case '=':
3228 val = left >= right;
3229 break;
3230 default:
3231 die("unknown op '%s'", arg->op.op);
3233 break;
3234 case '=':
3235 if (arg->op.op[1] != '=')
3236 die("unknown op '%s'", arg->op.op);
3237 val = left == right;
3238 break;
3239 case '-':
3240 val = left - right;
3241 break;
3242 case '+':
3243 val = left + right;
3244 break;
3245 case '/':
3246 val = left / right;
3247 break;
3248 case '*':
3249 val = left * right;
3250 break;
3251 default:
3252 die("unknown op '%s'", arg->op.op);
3254 break;
3255 default: /* not sure what to do there */
3256 return 0;
3258 return val;
3261 struct flag {
3262 const char *name;
3263 unsigned long long value;
3266 static const struct flag flags[] = {
3267 { "HI_SOFTIRQ", 0 },
3268 { "TIMER_SOFTIRQ", 1 },
3269 { "NET_TX_SOFTIRQ", 2 },
3270 { "NET_RX_SOFTIRQ", 3 },
3271 { "BLOCK_SOFTIRQ", 4 },
3272 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3273 { "TASKLET_SOFTIRQ", 6 },
3274 { "SCHED_SOFTIRQ", 7 },
3275 { "HRTIMER_SOFTIRQ", 8 },
3276 { "RCU_SOFTIRQ", 9 },
3278 { "HRTIMER_NORESTART", 0 },
3279 { "HRTIMER_RESTART", 1 },
3282 static unsigned long long eval_flag(const char *flag)
3284 int i;
3287 * Some flags in the format files do not get converted.
3288 * If the flag is not numeric, see if it is something that
3289 * we already know about.
3291 if (isdigit(flag[0]))
3292 return strtoull(flag, NULL, 0);
3294 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3295 if (strcmp(flags[i].name, flag) == 0)
3296 return flags[i].value;
3298 return 0;
3301 static void print_str_to_seq(struct trace_seq *s, const char *format,
3302 int len_arg, const char *str)
3304 if (len_arg >= 0)
3305 trace_seq_printf(s, format, len_arg, str);
3306 else
3307 trace_seq_printf(s, format, str);
3310 static void print_str_arg(struct trace_seq *s, void *data, int size,
3311 struct event_format *event, const char *format,
3312 int len_arg, struct print_arg *arg)
3314 struct pevent *pevent = event->pevent;
3315 struct print_flag_sym *flag;
3316 struct format_field *field;
3317 unsigned long long val, fval;
3318 unsigned long addr;
3319 char *str;
3320 unsigned char *hex;
3321 int print;
3322 int i, len;
3324 switch (arg->type) {
3325 case PRINT_NULL:
3326 /* ?? */
3327 return;
3328 case PRINT_ATOM:
3329 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3330 return;
3331 case PRINT_FIELD:
3332 field = arg->field.field;
3333 if (!field) {
3334 field = pevent_find_any_field(event, arg->field.name);
3335 if (!field)
3336 die("field %s not found", arg->field.name);
3337 arg->field.field = field;
3339 /* Zero sized fields, mean the rest of the data */
3340 len = field->size ? : size - field->offset;
3343 * Some events pass in pointers. If this is not an array
3344 * and the size is the same as long_size, assume that it
3345 * is a pointer.
3347 if (!(field->flags & FIELD_IS_ARRAY) &&
3348 field->size == pevent->long_size) {
3349 addr = *(unsigned long *)(data + field->offset);
3350 trace_seq_printf(s, "%lx", addr);
3351 break;
3353 str = malloc_or_die(len + 1);
3354 memcpy(str, data + field->offset, len);
3355 str[len] = 0;
3356 print_str_to_seq(s, format, len_arg, str);
3357 free(str);
3358 break;
3359 case PRINT_FLAGS:
3360 val = eval_num_arg(data, size, event, arg->flags.field);
3361 print = 0;
3362 for (flag = arg->flags.flags; flag; flag = flag->next) {
3363 fval = eval_flag(flag->value);
3364 if (!val && !fval) {
3365 print_str_to_seq(s, format, len_arg, flag->str);
3366 break;
3368 if (fval && (val & fval) == fval) {
3369 if (print && arg->flags.delim)
3370 trace_seq_puts(s, arg->flags.delim);
3371 print_str_to_seq(s, format, len_arg, flag->str);
3372 print = 1;
3373 val &= ~fval;
3376 break;
3377 case PRINT_SYMBOL:
3378 val = eval_num_arg(data, size, event, arg->symbol.field);
3379 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3380 fval = eval_flag(flag->value);
3381 if (val == fval) {
3382 print_str_to_seq(s, format, len_arg, flag->str);
3383 break;
3386 break;
3387 case PRINT_HEX:
3388 field = arg->hex.field->field.field;
3389 if (!field) {
3390 str = arg->hex.field->field.name;
3391 field = pevent_find_any_field(event, str);
3392 if (!field)
3393 die("field %s not found", str);
3394 arg->hex.field->field.field = field;
3396 hex = data + field->offset;
3397 len = eval_num_arg(data, size, event, arg->hex.size);
3398 for (i = 0; i < len; i++) {
3399 if (i)
3400 trace_seq_putc(s, ' ');
3401 trace_seq_printf(s, "%02x", hex[i]);
3403 break;
3405 case PRINT_TYPE:
3406 break;
3407 case PRINT_STRING: {
3408 int str_offset;
3410 if (arg->string.offset == -1) {
3411 struct format_field *f;
3413 f = pevent_find_any_field(event, arg->string.string);
3414 arg->string.offset = f->offset;
3416 str_offset = data2host4(pevent, data + arg->string.offset);
3417 str_offset &= 0xffff;
3418 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3419 break;
3421 case PRINT_BSTRING:
3422 print_str_to_seq(s, format, len_arg, arg->string.string);
3423 break;
3424 case PRINT_OP:
3426 * The only op for string should be ? :
3428 if (arg->op.op[0] != '?')
3429 return;
3430 val = eval_num_arg(data, size, event, arg->op.left);
3431 if (val)
3432 print_str_arg(s, data, size, event,
3433 format, len_arg, arg->op.right->op.left);
3434 else
3435 print_str_arg(s, data, size, event,
3436 format, len_arg, arg->op.right->op.right);
3437 break;
3438 case PRINT_FUNC:
3439 process_defined_func(s, data, size, event, arg);
3440 break;
3441 default:
3442 /* well... */
3443 break;
3447 static unsigned long long
3448 process_defined_func(struct trace_seq *s, void *data, int size,
3449 struct event_format *event, struct print_arg *arg)
3451 struct pevent_function_handler *func_handle = arg->func.func;
3452 struct pevent_func_params *param;
3453 unsigned long long *args;
3454 unsigned long long ret;
3455 struct print_arg *farg;
3456 struct trace_seq str;
3457 struct save_str {
3458 struct save_str *next;
3459 char *str;
3460 } *strings = NULL, *string;
3461 int i;
3463 if (!func_handle->nr_args) {
3464 ret = (*func_handle->func)(s, NULL);
3465 goto out;
3468 farg = arg->func.args;
3469 param = func_handle->params;
3471 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3472 for (i = 0; i < func_handle->nr_args; i++) {
3473 switch (param->type) {
3474 case PEVENT_FUNC_ARG_INT:
3475 case PEVENT_FUNC_ARG_LONG:
3476 case PEVENT_FUNC_ARG_PTR:
3477 args[i] = eval_num_arg(data, size, event, farg);
3478 break;
3479 case PEVENT_FUNC_ARG_STRING:
3480 trace_seq_init(&str);
3481 print_str_arg(&str, data, size, event, "%s", -1, farg);
3482 trace_seq_terminate(&str);
3483 string = malloc_or_die(sizeof(*string));
3484 string->next = strings;
3485 string->str = strdup(str.buffer);
3486 if (!string->str)
3487 die("malloc str");
3489 args[i] = (uintptr_t)string->str;
3490 strings = string;
3491 trace_seq_destroy(&str);
3492 break;
3493 default:
3495 * Something went totally wrong, this is not
3496 * an input error, something in this code broke.
3498 die("Unexpected end of arguments\n");
3499 break;
3501 farg = farg->next;
3502 param = param->next;
3505 ret = (*func_handle->func)(s, args);
3506 free(args);
3507 while (strings) {
3508 string = strings;
3509 strings = string->next;
3510 free(string->str);
3511 free(string);
3514 out:
3515 /* TBD : handle return type here */
3516 return ret;
3519 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3521 struct pevent *pevent = event->pevent;
3522 struct format_field *field, *ip_field;
3523 struct print_arg *args, *arg, **next;
3524 unsigned long long ip, val;
3525 char *ptr;
3526 void *bptr;
3527 int vsize;
3529 field = pevent->bprint_buf_field;
3530 ip_field = pevent->bprint_ip_field;
3532 if (!field) {
3533 field = pevent_find_field(event, "buf");
3534 if (!field)
3535 die("can't find buffer field for binary printk");
3536 ip_field = pevent_find_field(event, "ip");
3537 if (!ip_field)
3538 die("can't find ip field for binary printk");
3539 pevent->bprint_buf_field = field;
3540 pevent->bprint_ip_field = ip_field;
3543 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3546 * The first arg is the IP pointer.
3548 args = alloc_arg();
3549 arg = args;
3550 arg->next = NULL;
3551 next = &arg->next;
3553 arg->type = PRINT_ATOM;
3554 arg->atom.atom = malloc_or_die(32);
3555 sprintf(arg->atom.atom, "%lld", ip);
3557 /* skip the first "%pf : " */
3558 for (ptr = fmt + 6, bptr = data + field->offset;
3559 bptr < data + size && *ptr; ptr++) {
3560 int ls = 0;
3562 if (*ptr == '%') {
3563 process_again:
3564 ptr++;
3565 switch (*ptr) {
3566 case '%':
3567 break;
3568 case 'l':
3569 ls++;
3570 goto process_again;
3571 case 'L':
3572 ls = 2;
3573 goto process_again;
3574 case '0' ... '9':
3575 goto process_again;
3576 case '.':
3577 goto process_again;
3578 case 'p':
3579 ls = 1;
3580 /* fall through */
3581 case 'd':
3582 case 'u':
3583 case 'x':
3584 case 'i':
3585 switch (ls) {
3586 case 0:
3587 vsize = 4;
3588 break;
3589 case 1:
3590 vsize = pevent->long_size;
3591 break;
3592 case 2:
3593 vsize = 8;
3594 break;
3595 default:
3596 vsize = ls; /* ? */
3597 break;
3599 /* fall through */
3600 case '*':
3601 if (*ptr == '*')
3602 vsize = 4;
3604 /* the pointers are always 4 bytes aligned */
3605 bptr = (void *)(((unsigned long)bptr + 3) &
3606 ~3);
3607 val = pevent_read_number(pevent, bptr, vsize);
3608 bptr += vsize;
3609 arg = alloc_arg();
3610 arg->next = NULL;
3611 arg->type = PRINT_ATOM;
3612 arg->atom.atom = malloc_or_die(32);
3613 sprintf(arg->atom.atom, "%lld", val);
3614 *next = arg;
3615 next = &arg->next;
3617 * The '*' case means that an arg is used as the length.
3618 * We need to continue to figure out for what.
3620 if (*ptr == '*')
3621 goto process_again;
3623 break;
3624 case 's':
3625 arg = alloc_arg();
3626 arg->next = NULL;
3627 arg->type = PRINT_BSTRING;
3628 arg->string.string = strdup(bptr);
3629 if (!arg->string.string)
3630 break;
3631 bptr += strlen(bptr) + 1;
3632 *next = arg;
3633 next = &arg->next;
3634 default:
3635 break;
3640 return args;
3643 static void free_args(struct print_arg *args)
3645 struct print_arg *next;
3647 while (args) {
3648 next = args->next;
3650 free_arg(args);
3651 args = next;
3655 static char *
3656 get_bprint_format(void *data, int size __unused, struct event_format *event)
3658 struct pevent *pevent = event->pevent;
3659 unsigned long long addr;
3660 struct format_field *field;
3661 struct printk_map *printk;
3662 char *format;
3663 char *p;
3665 field = pevent->bprint_fmt_field;
3667 if (!field) {
3668 field = pevent_find_field(event, "fmt");
3669 if (!field)
3670 die("can't find format field for binary printk");
3671 pevent->bprint_fmt_field = field;
3674 addr = pevent_read_number(pevent, data + field->offset, field->size);
3676 printk = find_printk(pevent, addr);
3677 if (!printk) {
3678 format = malloc_or_die(45);
3679 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3680 addr);
3681 return format;
3684 p = printk->printk;
3685 /* Remove any quotes. */
3686 if (*p == '"')
3687 p++;
3688 format = malloc_or_die(strlen(p) + 10);
3689 sprintf(format, "%s : %s", "%pf", p);
3690 /* remove ending quotes and new line since we will add one too */
3691 p = format + strlen(format) - 1;
3692 if (*p == '"')
3693 *p = 0;
3695 p -= 2;
3696 if (strcmp(p, "\\n") == 0)
3697 *p = 0;
3699 return format;
3702 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3703 struct event_format *event, struct print_arg *arg)
3705 unsigned char *buf;
3706 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3708 if (arg->type == PRINT_FUNC) {
3709 process_defined_func(s, data, size, event, arg);
3710 return;
3713 if (arg->type != PRINT_FIELD) {
3714 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3715 arg->type);
3716 return;
3719 if (mac == 'm')
3720 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3721 if (!arg->field.field) {
3722 arg->field.field =
3723 pevent_find_any_field(event, arg->field.name);
3724 if (!arg->field.field)
3725 die("field %s not found", arg->field.name);
3727 if (arg->field.field->size != 6) {
3728 trace_seq_printf(s, "INVALIDMAC");
3729 return;
3731 buf = data + arg->field.field->offset;
3732 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3735 static int is_printable_array(char *p, unsigned int len)
3737 unsigned int i;
3739 for (i = 0; i < len && p[i]; i++)
3740 if (!isprint(p[i]))
3741 return 0;
3742 return 1;
3745 static void print_event_fields(struct trace_seq *s, void *data, int size,
3746 struct event_format *event)
3748 struct format_field *field;
3749 unsigned long long val;
3750 unsigned int offset, len, i;
3752 field = event->format.fields;
3753 while (field) {
3754 trace_seq_printf(s, " %s=", field->name);
3755 if (field->flags & FIELD_IS_ARRAY) {
3756 offset = field->offset;
3757 len = field->size;
3758 if (field->flags & FIELD_IS_DYNAMIC) {
3759 val = pevent_read_number(event->pevent, data + offset, len);
3760 offset = val;
3761 len = offset >> 16;
3762 offset &= 0xffff;
3764 if (field->flags & FIELD_IS_STRING &&
3765 is_printable_array(data + offset, len)) {
3766 trace_seq_printf(s, "%s", (char *)data + offset);
3767 } else {
3768 trace_seq_puts(s, "ARRAY[");
3769 for (i = 0; i < len; i++) {
3770 if (i)
3771 trace_seq_puts(s, ", ");
3772 trace_seq_printf(s, "%02x",
3773 *((unsigned char *)data + offset + i));
3775 trace_seq_putc(s, ']');
3776 field->flags &= ~FIELD_IS_STRING;
3778 } else {
3779 val = pevent_read_number(event->pevent, data + field->offset,
3780 field->size);
3781 if (field->flags & FIELD_IS_POINTER) {
3782 trace_seq_printf(s, "0x%llx", val);
3783 } else if (field->flags & FIELD_IS_SIGNED) {
3784 switch (field->size) {
3785 case 4:
3787 * If field is long then print it in hex.
3788 * A long usually stores pointers.
3790 if (field->flags & FIELD_IS_LONG)
3791 trace_seq_printf(s, "0x%x", (int)val);
3792 else
3793 trace_seq_printf(s, "%d", (int)val);
3794 break;
3795 case 2:
3796 trace_seq_printf(s, "%2d", (short)val);
3797 break;
3798 case 1:
3799 trace_seq_printf(s, "%1d", (char)val);
3800 break;
3801 default:
3802 trace_seq_printf(s, "%lld", val);
3804 } else {
3805 if (field->flags & FIELD_IS_LONG)
3806 trace_seq_printf(s, "0x%llx", val);
3807 else
3808 trace_seq_printf(s, "%llu", val);
3811 field = field->next;
3815 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3817 struct pevent *pevent = event->pevent;
3818 struct print_fmt *print_fmt = &event->print_fmt;
3819 struct print_arg *arg = print_fmt->args;
3820 struct print_arg *args = NULL;
3821 const char *ptr = print_fmt->format;
3822 unsigned long long val;
3823 struct func_map *func;
3824 const char *saveptr;
3825 char *bprint_fmt = NULL;
3826 char format[32];
3827 int show_func;
3828 int len_as_arg;
3829 int len_arg;
3830 int len;
3831 int ls;
3833 if (event->flags & EVENT_FL_FAILED) {
3834 trace_seq_printf(s, "[FAILED TO PARSE]");
3835 print_event_fields(s, data, size, event);
3836 return;
3839 if (event->flags & EVENT_FL_ISBPRINT) {
3840 bprint_fmt = get_bprint_format(data, size, event);
3841 args = make_bprint_args(bprint_fmt, data, size, event);
3842 arg = args;
3843 ptr = bprint_fmt;
3846 for (; *ptr; ptr++) {
3847 ls = 0;
3848 if (*ptr == '\\') {
3849 ptr++;
3850 switch (*ptr) {
3851 case 'n':
3852 trace_seq_putc(s, '\n');
3853 break;
3854 case 't':
3855 trace_seq_putc(s, '\t');
3856 break;
3857 case 'r':
3858 trace_seq_putc(s, '\r');
3859 break;
3860 case '\\':
3861 trace_seq_putc(s, '\\');
3862 break;
3863 default:
3864 trace_seq_putc(s, *ptr);
3865 break;
3868 } else if (*ptr == '%') {
3869 saveptr = ptr;
3870 show_func = 0;
3871 len_as_arg = 0;
3872 cont_process:
3873 ptr++;
3874 switch (*ptr) {
3875 case '%':
3876 trace_seq_putc(s, '%');
3877 break;
3878 case '#':
3879 /* FIXME: need to handle properly */
3880 goto cont_process;
3881 case 'h':
3882 ls--;
3883 goto cont_process;
3884 case 'l':
3885 ls++;
3886 goto cont_process;
3887 case 'L':
3888 ls = 2;
3889 goto cont_process;
3890 case '*':
3891 /* The argument is the length. */
3892 if (!arg)
3893 die("no argument match");
3894 len_arg = eval_num_arg(data, size, event, arg);
3895 len_as_arg = 1;
3896 arg = arg->next;
3897 goto cont_process;
3898 case '.':
3899 case 'z':
3900 case 'Z':
3901 case '0' ... '9':
3902 goto cont_process;
3903 case 'p':
3904 if (pevent->long_size == 4)
3905 ls = 1;
3906 else
3907 ls = 2;
3909 if (*(ptr+1) == 'F' ||
3910 *(ptr+1) == 'f') {
3911 ptr++;
3912 show_func = *ptr;
3913 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3914 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3915 ptr++;
3916 arg = arg->next;
3917 break;
3920 /* fall through */
3921 case 'd':
3922 case 'i':
3923 case 'x':
3924 case 'X':
3925 case 'u':
3926 if (!arg)
3927 die("no argument match");
3929 len = ((unsigned long)ptr + 1) -
3930 (unsigned long)saveptr;
3932 /* should never happen */
3933 if (len > 31)
3934 die("bad format!");
3936 memcpy(format, saveptr, len);
3937 format[len] = 0;
3939 val = eval_num_arg(data, size, event, arg);
3940 arg = arg->next;
3942 if (show_func) {
3943 func = find_func(pevent, val);
3944 if (func) {
3945 trace_seq_puts(s, func->func);
3946 if (show_func == 'F')
3947 trace_seq_printf(s,
3948 "+0x%llx",
3949 val - func->addr);
3950 break;
3953 if (pevent->long_size == 8 && ls &&
3954 sizeof(long) != 8) {
3955 char *p;
3957 ls = 2;
3958 /* make %l into %ll */
3959 p = strchr(format, 'l');
3960 if (p)
3961 memmove(p+1, p, strlen(p)+1);
3962 else if (strcmp(format, "%p") == 0)
3963 strcpy(format, "0x%llx");
3965 switch (ls) {
3966 case -2:
3967 if (len_as_arg)
3968 trace_seq_printf(s, format, len_arg, (char)val);
3969 else
3970 trace_seq_printf(s, format, (char)val);
3971 break;
3972 case -1:
3973 if (len_as_arg)
3974 trace_seq_printf(s, format, len_arg, (short)val);
3975 else
3976 trace_seq_printf(s, format, (short)val);
3977 break;
3978 case 0:
3979 if (len_as_arg)
3980 trace_seq_printf(s, format, len_arg, (int)val);
3981 else
3982 trace_seq_printf(s, format, (int)val);
3983 break;
3984 case 1:
3985 if (len_as_arg)
3986 trace_seq_printf(s, format, len_arg, (long)val);
3987 else
3988 trace_seq_printf(s, format, (long)val);
3989 break;
3990 case 2:
3991 if (len_as_arg)
3992 trace_seq_printf(s, format, len_arg,
3993 (long long)val);
3994 else
3995 trace_seq_printf(s, format, (long long)val);
3996 break;
3997 default:
3998 die("bad count (%d)", ls);
4000 break;
4001 case 's':
4002 if (!arg)
4003 die("no matching argument");
4005 len = ((unsigned long)ptr + 1) -
4006 (unsigned long)saveptr;
4008 /* should never happen */
4009 if (len > 31)
4010 die("bad format!");
4012 memcpy(format, saveptr, len);
4013 format[len] = 0;
4014 if (!len_as_arg)
4015 len_arg = -1;
4016 print_str_arg(s, data, size, event,
4017 format, len_arg, arg);
4018 arg = arg->next;
4019 break;
4020 default:
4021 trace_seq_printf(s, ">%c<", *ptr);
4024 } else
4025 trace_seq_putc(s, *ptr);
4028 if (args) {
4029 free_args(args);
4030 free(bprint_fmt);
4035 * pevent_data_lat_fmt - parse the data for the latency format
4036 * @pevent: a handle to the pevent
4037 * @s: the trace_seq to write to
4038 * @record: the record to read from
4040 * This parses out the Latency format (interrupts disabled,
4041 * need rescheduling, in hard/soft interrupt, preempt count
4042 * and lock depth) and places it into the trace_seq.
4044 void pevent_data_lat_fmt(struct pevent *pevent,
4045 struct trace_seq *s, struct pevent_record *record)
4047 static int check_lock_depth = 1;
4048 static int check_migrate_disable = 1;
4049 static int lock_depth_exists;
4050 static int migrate_disable_exists;
4051 unsigned int lat_flags;
4052 unsigned int pc;
4053 int lock_depth;
4054 int migrate_disable;
4055 int hardirq;
4056 int softirq;
4057 void *data = record->data;
4059 lat_flags = parse_common_flags(pevent, data);
4060 pc = parse_common_pc(pevent, data);
4061 /* lock_depth may not always exist */
4062 if (lock_depth_exists)
4063 lock_depth = parse_common_lock_depth(pevent, data);
4064 else if (check_lock_depth) {
4065 lock_depth = parse_common_lock_depth(pevent, data);
4066 if (lock_depth < 0)
4067 check_lock_depth = 0;
4068 else
4069 lock_depth_exists = 1;
4072 /* migrate_disable may not always exist */
4073 if (migrate_disable_exists)
4074 migrate_disable = parse_common_migrate_disable(pevent, data);
4075 else if (check_migrate_disable) {
4076 migrate_disable = parse_common_migrate_disable(pevent, data);
4077 if (migrate_disable < 0)
4078 check_migrate_disable = 0;
4079 else
4080 migrate_disable_exists = 1;
4083 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4084 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4086 trace_seq_printf(s, "%c%c%c",
4087 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4088 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4089 'X' : '.',
4090 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4091 'N' : '.',
4092 (hardirq && softirq) ? 'H' :
4093 hardirq ? 'h' : softirq ? 's' : '.');
4095 if (pc)
4096 trace_seq_printf(s, "%x", pc);
4097 else
4098 trace_seq_putc(s, '.');
4100 if (migrate_disable_exists) {
4101 if (migrate_disable < 0)
4102 trace_seq_putc(s, '.');
4103 else
4104 trace_seq_printf(s, "%d", migrate_disable);
4107 if (lock_depth_exists) {
4108 if (lock_depth < 0)
4109 trace_seq_putc(s, '.');
4110 else
4111 trace_seq_printf(s, "%d", lock_depth);
4114 trace_seq_terminate(s);
4118 * pevent_data_type - parse out the given event type
4119 * @pevent: a handle to the pevent
4120 * @rec: the record to read from
4122 * This returns the event id from the @rec.
4124 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4126 return trace_parse_common_type(pevent, rec->data);
4130 * pevent_data_event_from_type - find the event by a given type
4131 * @pevent: a handle to the pevent
4132 * @type: the type of the event.
4134 * This returns the event form a given @type;
4136 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4138 return pevent_find_event(pevent, type);
4142 * pevent_data_pid - parse the PID from raw data
4143 * @pevent: a handle to the pevent
4144 * @rec: the record to parse
4146 * This returns the PID from a raw data.
4148 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4150 return parse_common_pid(pevent, rec->data);
4154 * pevent_data_comm_from_pid - return the command line from PID
4155 * @pevent: a handle to the pevent
4156 * @pid: the PID of the task to search for
4158 * This returns a pointer to the command line that has the given
4159 * @pid.
4161 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4163 const char *comm;
4165 comm = find_cmdline(pevent, pid);
4166 return comm;
4170 * pevent_data_comm_from_pid - parse the data into the print format
4171 * @s: the trace_seq to write to
4172 * @event: the handle to the event
4173 * @record: the record to read from
4175 * This parses the raw @data using the given @event information and
4176 * writes the print format into the trace_seq.
4178 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4179 struct pevent_record *record)
4181 int print_pretty = 1;
4183 if (event->pevent->print_raw)
4184 print_event_fields(s, record->data, record->size, event);
4185 else {
4187 if (event->handler)
4188 print_pretty = event->handler(s, record, event,
4189 event->context);
4191 if (print_pretty)
4192 pretty_print(s, record->data, record->size, event);
4195 trace_seq_terminate(s);
4198 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4199 struct pevent_record *record)
4201 static char *spaces = " "; /* 20 spaces */
4202 struct event_format *event;
4203 unsigned long secs;
4204 unsigned long usecs;
4205 unsigned long nsecs;
4206 const char *comm;
4207 void *data = record->data;
4208 int type;
4209 int pid;
4210 int len;
4211 int p;
4213 secs = record->ts / NSECS_PER_SEC;
4214 nsecs = record->ts - secs * NSECS_PER_SEC;
4216 if (record->size < 0) {
4217 do_warning("ug! negative record size %d", record->size);
4218 return;
4221 type = trace_parse_common_type(pevent, data);
4223 event = pevent_find_event(pevent, type);
4224 if (!event) {
4225 do_warning("ug! no event found for type %d", type);
4226 return;
4229 pid = parse_common_pid(pevent, data);
4230 comm = find_cmdline(pevent, pid);
4232 if (pevent->latency_format) {
4233 trace_seq_printf(s, "%8.8s-%-5d %3d",
4234 comm, pid, record->cpu);
4235 pevent_data_lat_fmt(pevent, s, record);
4236 } else
4237 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4239 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4240 usecs = nsecs;
4241 p = 9;
4242 } else {
4243 usecs = (nsecs + 500) / NSECS_PER_USEC;
4244 p = 6;
4247 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4249 /* Space out the event names evenly. */
4250 len = strlen(event->name);
4251 if (len < 20)
4252 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4254 pevent_event_info(s, event, record);
4257 static int events_id_cmp(const void *a, const void *b)
4259 struct event_format * const * ea = a;
4260 struct event_format * const * eb = b;
4262 if ((*ea)->id < (*eb)->id)
4263 return -1;
4265 if ((*ea)->id > (*eb)->id)
4266 return 1;
4268 return 0;
4271 static int events_name_cmp(const void *a, const void *b)
4273 struct event_format * const * ea = a;
4274 struct event_format * const * eb = b;
4275 int res;
4277 res = strcmp((*ea)->name, (*eb)->name);
4278 if (res)
4279 return res;
4281 res = strcmp((*ea)->system, (*eb)->system);
4282 if (res)
4283 return res;
4285 return events_id_cmp(a, b);
4288 static int events_system_cmp(const void *a, const void *b)
4290 struct event_format * const * ea = a;
4291 struct event_format * const * eb = b;
4292 int res;
4294 res = strcmp((*ea)->system, (*eb)->system);
4295 if (res)
4296 return res;
4298 res = strcmp((*ea)->name, (*eb)->name);
4299 if (res)
4300 return res;
4302 return events_id_cmp(a, b);
4305 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4307 struct event_format **events;
4308 int (*sort)(const void *a, const void *b);
4310 events = pevent->sort_events;
4312 if (events && pevent->last_type == sort_type)
4313 return events;
4315 if (!events) {
4316 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4317 if (!events)
4318 return NULL;
4320 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4321 events[pevent->nr_events] = NULL;
4323 pevent->sort_events = events;
4325 /* the internal events are sorted by id */
4326 if (sort_type == EVENT_SORT_ID) {
4327 pevent->last_type = sort_type;
4328 return events;
4332 switch (sort_type) {
4333 case EVENT_SORT_ID:
4334 sort = events_id_cmp;
4335 break;
4336 case EVENT_SORT_NAME:
4337 sort = events_name_cmp;
4338 break;
4339 case EVENT_SORT_SYSTEM:
4340 sort = events_system_cmp;
4341 break;
4342 default:
4343 return events;
4346 qsort(events, pevent->nr_events, sizeof(*events), sort);
4347 pevent->last_type = sort_type;
4349 return events;
4352 static struct format_field **
4353 get_event_fields(const char *type, const char *name,
4354 int count, struct format_field *list)
4356 struct format_field **fields;
4357 struct format_field *field;
4358 int i = 0;
4360 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4361 for (field = list; field; field = field->next) {
4362 fields[i++] = field;
4363 if (i == count + 1) {
4364 do_warning("event %s has more %s fields than specified",
4365 name, type);
4366 i--;
4367 break;
4371 if (i != count)
4372 do_warning("event %s has less %s fields than specified",
4373 name, type);
4375 fields[i] = NULL;
4377 return fields;
4381 * pevent_event_common_fields - return a list of common fields for an event
4382 * @event: the event to return the common fields of.
4384 * Returns an allocated array of fields. The last item in the array is NULL.
4385 * The array must be freed with free().
4387 struct format_field **pevent_event_common_fields(struct event_format *event)
4389 return get_event_fields("common", event->name,
4390 event->format.nr_common,
4391 event->format.common_fields);
4395 * pevent_event_fields - return a list of event specific fields for an event
4396 * @event: the event to return the fields of.
4398 * Returns an allocated array of fields. The last item in the array is NULL.
4399 * The array must be freed with free().
4401 struct format_field **pevent_event_fields(struct event_format *event)
4403 return get_event_fields("event", event->name,
4404 event->format.nr_fields,
4405 event->format.fields);
4408 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4410 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4411 if (field->next) {
4412 trace_seq_puts(s, ", ");
4413 print_fields(s, field->next);
4417 /* for debugging */
4418 static void print_args(struct print_arg *args)
4420 int print_paren = 1;
4421 struct trace_seq s;
4423 switch (args->type) {
4424 case PRINT_NULL:
4425 printf("null");
4426 break;
4427 case PRINT_ATOM:
4428 printf("%s", args->atom.atom);
4429 break;
4430 case PRINT_FIELD:
4431 printf("REC->%s", args->field.name);
4432 break;
4433 case PRINT_FLAGS:
4434 printf("__print_flags(");
4435 print_args(args->flags.field);
4436 printf(", %s, ", args->flags.delim);
4437 trace_seq_init(&s);
4438 print_fields(&s, args->flags.flags);
4439 trace_seq_do_printf(&s);
4440 trace_seq_destroy(&s);
4441 printf(")");
4442 break;
4443 case PRINT_SYMBOL:
4444 printf("__print_symbolic(");
4445 print_args(args->symbol.field);
4446 printf(", ");
4447 trace_seq_init(&s);
4448 print_fields(&s, args->symbol.symbols);
4449 trace_seq_do_printf(&s);
4450 trace_seq_destroy(&s);
4451 printf(")");
4452 break;
4453 case PRINT_HEX:
4454 printf("__print_hex(");
4455 print_args(args->hex.field);
4456 printf(", ");
4457 print_args(args->hex.size);
4458 printf(")");
4459 break;
4460 case PRINT_STRING:
4461 case PRINT_BSTRING:
4462 printf("__get_str(%s)", args->string.string);
4463 break;
4464 case PRINT_TYPE:
4465 printf("(%s)", args->typecast.type);
4466 print_args(args->typecast.item);
4467 break;
4468 case PRINT_OP:
4469 if (strcmp(args->op.op, ":") == 0)
4470 print_paren = 0;
4471 if (print_paren)
4472 printf("(");
4473 print_args(args->op.left);
4474 printf(" %s ", args->op.op);
4475 print_args(args->op.right);
4476 if (print_paren)
4477 printf(")");
4478 break;
4479 default:
4480 /* we should warn... */
4481 return;
4483 if (args->next) {
4484 printf("\n");
4485 print_args(args->next);
4489 static void parse_header_field(const char *field,
4490 int *offset, int *size, int mandatory)
4492 unsigned long long save_input_buf_ptr;
4493 unsigned long long save_input_buf_siz;
4494 char *token;
4495 int type;
4497 save_input_buf_ptr = input_buf_ptr;
4498 save_input_buf_siz = input_buf_siz;
4500 if (read_expected(EVENT_ITEM, "field") < 0)
4501 return;
4502 if (read_expected(EVENT_OP, ":") < 0)
4503 return;
4505 /* type */
4506 if (read_expect_type(EVENT_ITEM, &token) < 0)
4507 goto fail;
4508 free_token(token);
4511 * If this is not a mandatory field, then test it first.
4513 if (mandatory) {
4514 if (read_expected(EVENT_ITEM, field) < 0)
4515 return;
4516 } else {
4517 if (read_expect_type(EVENT_ITEM, &token) < 0)
4518 goto fail;
4519 if (strcmp(token, field) != 0)
4520 goto discard;
4521 free_token(token);
4524 if (read_expected(EVENT_OP, ";") < 0)
4525 return;
4526 if (read_expected(EVENT_ITEM, "offset") < 0)
4527 return;
4528 if (read_expected(EVENT_OP, ":") < 0)
4529 return;
4530 if (read_expect_type(EVENT_ITEM, &token) < 0)
4531 goto fail;
4532 *offset = atoi(token);
4533 free_token(token);
4534 if (read_expected(EVENT_OP, ";") < 0)
4535 return;
4536 if (read_expected(EVENT_ITEM, "size") < 0)
4537 return;
4538 if (read_expected(EVENT_OP, ":") < 0)
4539 return;
4540 if (read_expect_type(EVENT_ITEM, &token) < 0)
4541 goto fail;
4542 *size = atoi(token);
4543 free_token(token);
4544 if (read_expected(EVENT_OP, ";") < 0)
4545 return;
4546 type = read_token(&token);
4547 if (type != EVENT_NEWLINE) {
4548 /* newer versions of the kernel have a "signed" type */
4549 if (type != EVENT_ITEM)
4550 goto fail;
4552 if (strcmp(token, "signed") != 0)
4553 goto fail;
4555 free_token(token);
4557 if (read_expected(EVENT_OP, ":") < 0)
4558 return;
4560 if (read_expect_type(EVENT_ITEM, &token))
4561 goto fail;
4563 free_token(token);
4564 if (read_expected(EVENT_OP, ";") < 0)
4565 return;
4567 if (read_expect_type(EVENT_NEWLINE, &token))
4568 goto fail;
4570 fail:
4571 free_token(token);
4572 return;
4574 discard:
4575 input_buf_ptr = save_input_buf_ptr;
4576 input_buf_siz = save_input_buf_siz;
4577 *offset = 0;
4578 *size = 0;
4579 free_token(token);
4583 * pevent_parse_header_page - parse the data stored in the header page
4584 * @pevent: the handle to the pevent
4585 * @buf: the buffer storing the header page format string
4586 * @size: the size of @buf
4587 * @long_size: the long size to use if there is no header
4589 * This parses the header page format for information on the
4590 * ring buffer used. The @buf should be copied from
4592 * /sys/kernel/debug/tracing/events/header_page
4594 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4595 int long_size)
4597 int ignore;
4599 if (!size) {
4601 * Old kernels did not have header page info.
4602 * Sorry but we just use what we find here in user space.
4604 pevent->header_page_ts_size = sizeof(long long);
4605 pevent->header_page_size_size = long_size;
4606 pevent->header_page_data_offset = sizeof(long long) + long_size;
4607 pevent->old_format = 1;
4608 return -1;
4610 init_input_buf(buf, size);
4612 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4613 &pevent->header_page_ts_size, 1);
4614 parse_header_field("commit", &pevent->header_page_size_offset,
4615 &pevent->header_page_size_size, 1);
4616 parse_header_field("overwrite", &pevent->header_page_overwrite,
4617 &ignore, 0);
4618 parse_header_field("data", &pevent->header_page_data_offset,
4619 &pevent->header_page_data_size, 1);
4621 return 0;
4624 static int event_matches(struct event_format *event,
4625 int id, const char *sys_name,
4626 const char *event_name)
4628 if (id >= 0 && id != event->id)
4629 return 0;
4631 if (event_name && (strcmp(event_name, event->name) != 0))
4632 return 0;
4634 if (sys_name && (strcmp(sys_name, event->system) != 0))
4635 return 0;
4637 return 1;
4640 static void free_handler(struct event_handler *handle)
4642 free((void *)handle->sys_name);
4643 free((void *)handle->event_name);
4644 free(handle);
4647 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4649 struct event_handler *handle, **next;
4651 for (next = &pevent->handlers; *next;
4652 next = &(*next)->next) {
4653 handle = *next;
4654 if (event_matches(event, handle->id,
4655 handle->sys_name,
4656 handle->event_name))
4657 break;
4660 if (!(*next))
4661 return 0;
4663 pr_stat("overriding event (%d) %s:%s with new print handler",
4664 event->id, event->system, event->name);
4666 event->handler = handle->func;
4667 event->context = handle->context;
4669 *next = handle->next;
4670 free_handler(handle);
4672 return 1;
4676 * pevent_parse_event - parse the event format
4677 * @pevent: the handle to the pevent
4678 * @buf: the buffer storing the event format string
4679 * @size: the size of @buf
4680 * @sys: the system the event belongs to
4682 * This parses the event format and creates an event structure
4683 * to quickly parse raw data for a given event.
4685 * These files currently come from:
4687 * /sys/kernel/debug/tracing/events/.../.../format
4689 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4690 unsigned long size, const char *sys)
4692 struct event_format *event;
4693 int ret;
4695 init_input_buf(buf, size);
4697 event = alloc_event();
4698 if (!event)
4699 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4701 event->name = event_read_name();
4702 if (!event->name) {
4703 /* Bad event? */
4704 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4705 goto event_alloc_failed;
4708 if (strcmp(sys, "ftrace") == 0) {
4709 event->flags |= EVENT_FL_ISFTRACE;
4711 if (strcmp(event->name, "bprint") == 0)
4712 event->flags |= EVENT_FL_ISBPRINT;
4715 event->id = event_read_id();
4716 if (event->id < 0) {
4717 ret = PEVENT_ERRNO__READ_ID_FAILED;
4719 * This isn't an allocation error actually.
4720 * But as the ID is critical, just bail out.
4722 goto event_alloc_failed;
4725 event->system = strdup(sys);
4726 if (!event->system) {
4727 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4728 goto event_alloc_failed;
4731 /* Add pevent to event so that it can be referenced */
4732 event->pevent = pevent;
4734 ret = event_read_format(event);
4735 if (ret < 0) {
4736 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4737 goto event_parse_failed;
4741 * If the event has an override, don't print warnings if the event
4742 * print format fails to parse.
4744 if (find_event_handle(pevent, event))
4745 show_warning = 0;
4747 ret = event_read_print(event);
4748 if (ret < 0) {
4749 show_warning = 1;
4750 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4751 goto event_parse_failed;
4753 show_warning = 1;
4755 add_event(pevent, event);
4757 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4758 struct format_field *field;
4759 struct print_arg *arg, **list;
4761 /* old ftrace had no args */
4762 list = &event->print_fmt.args;
4763 for (field = event->format.fields; field; field = field->next) {
4764 arg = alloc_arg();
4765 arg->type = PRINT_FIELD;
4766 arg->field.name = strdup(field->name);
4767 if (!arg->field.name) {
4768 event->flags |= EVENT_FL_FAILED;
4769 free_arg(arg);
4770 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4772 arg->field.field = field;
4773 *list = arg;
4774 list = &arg->next;
4776 return 0;
4779 #define PRINT_ARGS 0
4780 if (PRINT_ARGS && event->print_fmt.args)
4781 print_args(event->print_fmt.args);
4783 return 0;
4785 event_parse_failed:
4786 event->flags |= EVENT_FL_FAILED;
4787 /* still add it even if it failed */
4788 add_event(pevent, event);
4789 return ret;
4791 event_alloc_failed:
4792 free(event->system);
4793 free(event->name);
4794 free(event);
4795 return ret;
4798 #undef _PE
4799 #define _PE(code, str) str
4800 static const char * const pevent_error_str[] = {
4801 PEVENT_ERRORS
4803 #undef _PE
4805 int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4806 char *buf, size_t buflen)
4808 int idx;
4809 const char *msg;
4811 if (errnum >= 0) {
4812 strerror_r(errnum, buf, buflen);
4813 return 0;
4816 if (errnum <= __PEVENT_ERRNO__START ||
4817 errnum >= __PEVENT_ERRNO__END)
4818 return -1;
4820 idx = errnum - __PEVENT_ERRNO__START;
4821 msg = pevent_error_str[idx];
4823 switch (errnum) {
4824 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4825 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4826 case PEVENT_ERRNO__READ_ID_FAILED:
4827 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4828 case PEVENT_ERRNO__READ_PRINT_FAILED:
4829 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4830 snprintf(buf, buflen, "%s", msg);
4831 break;
4833 default:
4834 /* cannot reach here */
4835 break;
4838 return 0;
4841 int get_field_val(struct trace_seq *s, struct format_field *field,
4842 const char *name, struct pevent_record *record,
4843 unsigned long long *val, int err)
4845 if (!field) {
4846 if (err)
4847 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4848 return -1;
4851 if (pevent_read_number_field(field, record->data, val)) {
4852 if (err)
4853 trace_seq_printf(s, " %s=INVALID", name);
4854 return -1;
4857 return 0;
4861 * pevent_get_field_raw - return the raw pointer into the data field
4862 * @s: The seq to print to on error
4863 * @event: the event that the field is for
4864 * @name: The name of the field
4865 * @record: The record with the field name.
4866 * @len: place to store the field length.
4867 * @err: print default error if failed.
4869 * Returns a pointer into record->data of the field and places
4870 * the length of the field in @len.
4872 * On failure, it returns NULL.
4874 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4875 const char *name, struct pevent_record *record,
4876 int *len, int err)
4878 struct format_field *field;
4879 void *data = record->data;
4880 unsigned offset;
4881 int dummy;
4883 if (!event)
4884 return NULL;
4886 field = pevent_find_field(event, name);
4888 if (!field) {
4889 if (err)
4890 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4891 return NULL;
4894 /* Allow @len to be NULL */
4895 if (!len)
4896 len = &dummy;
4898 offset = field->offset;
4899 if (field->flags & FIELD_IS_DYNAMIC) {
4900 offset = pevent_read_number(event->pevent,
4901 data + offset, field->size);
4902 *len = offset >> 16;
4903 offset &= 0xffff;
4904 } else
4905 *len = field->size;
4907 return data + offset;
4911 * pevent_get_field_val - find a field and return its value
4912 * @s: The seq to print to on error
4913 * @event: the event that the field is for
4914 * @name: The name of the field
4915 * @record: The record with the field name.
4916 * @val: place to store the value of the field.
4917 * @err: print default error if failed.
4919 * Returns 0 on success -1 on field not found.
4921 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4922 const char *name, struct pevent_record *record,
4923 unsigned long long *val, int err)
4925 struct format_field *field;
4927 if (!event)
4928 return -1;
4930 field = pevent_find_field(event, name);
4932 return get_field_val(s, field, name, record, val, err);
4936 * pevent_get_common_field_val - find a common field and return its value
4937 * @s: The seq to print to on error
4938 * @event: the event that the field is for
4939 * @name: The name of the field
4940 * @record: The record with the field name.
4941 * @val: place to store the value of the field.
4942 * @err: print default error if failed.
4944 * Returns 0 on success -1 on field not found.
4946 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4947 const char *name, struct pevent_record *record,
4948 unsigned long long *val, int err)
4950 struct format_field *field;
4952 if (!event)
4953 return -1;
4955 field = pevent_find_common_field(event, name);
4957 return get_field_val(s, field, name, record, val, err);
4961 * pevent_get_any_field_val - find a any field and return its value
4962 * @s: The seq to print to on error
4963 * @event: the event that the field is for
4964 * @name: The name of the field
4965 * @record: The record with the field name.
4966 * @val: place to store the value of the field.
4967 * @err: print default error if failed.
4969 * Returns 0 on success -1 on field not found.
4971 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
4972 const char *name, struct pevent_record *record,
4973 unsigned long long *val, int err)
4975 struct format_field *field;
4977 if (!event)
4978 return -1;
4980 field = pevent_find_any_field(event, name);
4982 return get_field_val(s, field, name, record, val, err);
4986 * pevent_print_num_field - print a field and a format
4987 * @s: The seq to print to
4988 * @fmt: The printf format to print the field with.
4989 * @event: the event that the field is for
4990 * @name: The name of the field
4991 * @record: The record with the field name.
4992 * @err: print default error if failed.
4994 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
4996 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4997 struct event_format *event, const char *name,
4998 struct pevent_record *record, int err)
5000 struct format_field *field = pevent_find_field(event, name);
5001 unsigned long long val;
5003 if (!field)
5004 goto failed;
5006 if (pevent_read_number_field(field, record->data, &val))
5007 goto failed;
5009 return trace_seq_printf(s, fmt, val);
5011 failed:
5012 if (err)
5013 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5014 return -1;
5017 static void free_func_handle(struct pevent_function_handler *func)
5019 struct pevent_func_params *params;
5021 free(func->name);
5023 while (func->params) {
5024 params = func->params;
5025 func->params = params->next;
5026 free(params);
5029 free(func);
5033 * pevent_register_print_function - register a helper function
5034 * @pevent: the handle to the pevent
5035 * @func: the function to process the helper function
5036 * @ret_type: the return type of the helper function
5037 * @name: the name of the helper function
5038 * @parameters: A list of enum pevent_func_arg_type
5040 * Some events may have helper functions in the print format arguments.
5041 * This allows a plugin to dynamically create a way to process one
5042 * of these functions.
5044 * The @parameters is a variable list of pevent_func_arg_type enums that
5045 * must end with PEVENT_FUNC_ARG_VOID.
5047 int pevent_register_print_function(struct pevent *pevent,
5048 pevent_func_handler func,
5049 enum pevent_func_arg_type ret_type,
5050 char *name, ...)
5052 struct pevent_function_handler *func_handle;
5053 struct pevent_func_params **next_param;
5054 struct pevent_func_params *param;
5055 enum pevent_func_arg_type type;
5056 va_list ap;
5058 func_handle = find_func_handler(pevent, name);
5059 if (func_handle) {
5061 * This is most like caused by the users own
5062 * plugins updating the function. This overrides the
5063 * system defaults.
5065 pr_stat("override of function helper '%s'", name);
5066 remove_func_handler(pevent, name);
5069 func_handle = malloc_or_die(sizeof(*func_handle));
5070 memset(func_handle, 0, sizeof(*func_handle));
5072 func_handle->ret_type = ret_type;
5073 func_handle->name = strdup(name);
5074 func_handle->func = func;
5075 if (!func_handle->name)
5076 die("Failed to allocate function name");
5078 next_param = &(func_handle->params);
5079 va_start(ap, name);
5080 for (;;) {
5081 type = va_arg(ap, enum pevent_func_arg_type);
5082 if (type == PEVENT_FUNC_ARG_VOID)
5083 break;
5085 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5086 warning("Invalid argument type %d", type);
5087 goto out_free;
5090 param = malloc_or_die(sizeof(*param));
5091 param->type = type;
5092 param->next = NULL;
5094 *next_param = param;
5095 next_param = &(param->next);
5097 func_handle->nr_args++;
5099 va_end(ap);
5101 func_handle->next = pevent->func_handlers;
5102 pevent->func_handlers = func_handle;
5104 return 0;
5105 out_free:
5106 va_end(ap);
5107 free_func_handle(func_handle);
5108 return -1;
5112 * pevent_register_event_handler - register a way to parse an event
5113 * @pevent: the handle to the pevent
5114 * @id: the id of the event to register
5115 * @sys_name: the system name the event belongs to
5116 * @event_name: the name of the event
5117 * @func: the function to call to parse the event information
5118 * @context: the data to be passed to @func
5120 * This function allows a developer to override the parsing of
5121 * a given event. If for some reason the default print format
5122 * is not sufficient, this function will register a function
5123 * for an event to be used to parse the data instead.
5125 * If @id is >= 0, then it is used to find the event.
5126 * else @sys_name and @event_name are used.
5128 int pevent_register_event_handler(struct pevent *pevent,
5129 int id, char *sys_name, char *event_name,
5130 pevent_event_handler_func func,
5131 void *context)
5133 struct event_format *event;
5134 struct event_handler *handle;
5136 if (id >= 0) {
5137 /* search by id */
5138 event = pevent_find_event(pevent, id);
5139 if (!event)
5140 goto not_found;
5141 if (event_name && (strcmp(event_name, event->name) != 0))
5142 goto not_found;
5143 if (sys_name && (strcmp(sys_name, event->system) != 0))
5144 goto not_found;
5145 } else {
5146 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5147 if (!event)
5148 goto not_found;
5151 pr_stat("overriding event (%d) %s:%s with new print handler",
5152 event->id, event->system, event->name);
5154 event->handler = func;
5155 event->context = context;
5156 return 0;
5158 not_found:
5159 /* Save for later use. */
5160 handle = malloc_or_die(sizeof(*handle));
5161 memset(handle, 0, sizeof(*handle));
5162 handle->id = id;
5163 if (event_name)
5164 handle->event_name = strdup(event_name);
5165 if (sys_name)
5166 handle->sys_name = strdup(sys_name);
5168 if ((event_name && !handle->event_name) ||
5169 (sys_name && !handle->sys_name)) {
5170 die("Failed to allocate event/sys name");
5173 handle->func = func;
5174 handle->next = pevent->handlers;
5175 pevent->handlers = handle;
5176 handle->context = context;
5178 return -1;
5182 * pevent_alloc - create a pevent handle
5184 struct pevent *pevent_alloc(void)
5186 struct pevent *pevent;
5188 pevent = malloc(sizeof(*pevent));
5189 if (!pevent)
5190 return NULL;
5191 memset(pevent, 0, sizeof(*pevent));
5192 pevent->ref_count = 1;
5194 return pevent;
5197 void pevent_ref(struct pevent *pevent)
5199 pevent->ref_count++;
5202 static void free_format_fields(struct format_field *field)
5204 struct format_field *next;
5206 while (field) {
5207 next = field->next;
5208 free(field->type);
5209 free(field->name);
5210 free(field);
5211 field = next;
5215 static void free_formats(struct format *format)
5217 free_format_fields(format->common_fields);
5218 free_format_fields(format->fields);
5221 static void free_event(struct event_format *event)
5223 free(event->name);
5224 free(event->system);
5226 free_formats(&event->format);
5228 free(event->print_fmt.format);
5229 free_args(event->print_fmt.args);
5231 free(event);
5235 * pevent_free - free a pevent handle
5236 * @pevent: the pevent handle to free
5238 void pevent_free(struct pevent *pevent)
5240 struct cmdline_list *cmdlist, *cmdnext;
5241 struct func_list *funclist, *funcnext;
5242 struct printk_list *printklist, *printknext;
5243 struct pevent_function_handler *func_handler;
5244 struct event_handler *handle;
5245 int i;
5247 if (!pevent)
5248 return;
5250 cmdlist = pevent->cmdlist;
5251 funclist = pevent->funclist;
5252 printklist = pevent->printklist;
5254 pevent->ref_count--;
5255 if (pevent->ref_count)
5256 return;
5258 if (pevent->cmdlines) {
5259 for (i = 0; i < pevent->cmdline_count; i++)
5260 free(pevent->cmdlines[i].comm);
5261 free(pevent->cmdlines);
5264 while (cmdlist) {
5265 cmdnext = cmdlist->next;
5266 free(cmdlist->comm);
5267 free(cmdlist);
5268 cmdlist = cmdnext;
5271 if (pevent->func_map) {
5272 for (i = 0; i < pevent->func_count; i++) {
5273 free(pevent->func_map[i].func);
5274 free(pevent->func_map[i].mod);
5276 free(pevent->func_map);
5279 while (funclist) {
5280 funcnext = funclist->next;
5281 free(funclist->func);
5282 free(funclist->mod);
5283 free(funclist);
5284 funclist = funcnext;
5287 while (pevent->func_handlers) {
5288 func_handler = pevent->func_handlers;
5289 pevent->func_handlers = func_handler->next;
5290 free_func_handle(func_handler);
5293 if (pevent->printk_map) {
5294 for (i = 0; i < pevent->printk_count; i++)
5295 free(pevent->printk_map[i].printk);
5296 free(pevent->printk_map);
5299 while (printklist) {
5300 printknext = printklist->next;
5301 free(printklist->printk);
5302 free(printklist);
5303 printklist = printknext;
5306 for (i = 0; i < pevent->nr_events; i++)
5307 free_event(pevent->events[i]);
5309 while (pevent->handlers) {
5310 handle = pevent->handlers;
5311 pevent->handlers = handle->next;
5312 free_handler(handle);
5315 free(pevent->events);
5316 free(pevent->sort_events);
5318 free(pevent);
5321 void pevent_unref(struct pevent *pevent)
5323 pevent_free(pevent);