Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / tools / lib / traceevent / event-parse.c
blobce1e20227c64d4e0789b6dad21a407988de720d9
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <limits.h>
21 #include <linux/string.h>
22 #include <linux/time64.h>
24 #include <netinet/in.h>
25 #include "event-parse.h"
26 #include "event-utils.h"
28 static const char *input_buf;
29 static unsigned long long input_buf_ptr;
30 static unsigned long long input_buf_siz;
32 static int is_flag_field;
33 static int is_symbolic_field;
35 static int show_warning = 1;
37 #define do_warning(fmt, ...) \
38 do { \
39 if (show_warning) \
40 warning(fmt, ##__VA_ARGS__); \
41 } while (0)
43 #define do_warning_event(event, fmt, ...) \
44 do { \
45 if (!show_warning) \
46 continue; \
48 if (event) \
49 warning("[%s:%s] " fmt, event->system, \
50 event->name, ##__VA_ARGS__); \
51 else \
52 warning(fmt, ##__VA_ARGS__); \
53 } while (0)
55 static void init_input_buf(const char *buf, unsigned long long size)
57 input_buf = buf;
58 input_buf_siz = size;
59 input_buf_ptr = 0;
62 const char *tep_get_input_buf(void)
64 return input_buf;
67 unsigned long long tep_get_input_buf_ptr(void)
69 return input_buf_ptr;
72 struct event_handler {
73 struct event_handler *next;
74 int id;
75 const char *sys_name;
76 const char *event_name;
77 tep_event_handler_func func;
78 void *context;
81 struct func_params {
82 struct func_params *next;
83 enum tep_func_arg_type type;
86 struct tep_function_handler {
87 struct tep_function_handler *next;
88 enum tep_func_arg_type ret_type;
89 char *name;
90 tep_func_handler func;
91 struct func_params *params;
92 int nr_args;
95 static unsigned long long
96 process_defined_func(struct trace_seq *s, void *data, int size,
97 struct event_format *event, struct print_arg *arg);
99 static void free_func_handle(struct tep_function_handler *func);
102 * tep_buffer_init - init buffer for parsing
103 * @buf: buffer to parse
104 * @size: the size of the buffer
106 * For use with tep_read_token(), this initializes the internal
107 * buffer that tep_read_token() will parse.
109 void tep_buffer_init(const char *buf, unsigned long long size)
111 init_input_buf(buf, size);
114 void breakpoint(void)
116 static int x;
117 x++;
120 struct print_arg *alloc_arg(void)
122 return calloc(1, sizeof(struct print_arg));
125 struct cmdline {
126 char *comm;
127 int pid;
130 static int cmdline_cmp(const void *a, const void *b)
132 const struct cmdline *ca = a;
133 const struct cmdline *cb = b;
135 if (ca->pid < cb->pid)
136 return -1;
137 if (ca->pid > cb->pid)
138 return 1;
140 return 0;
143 struct cmdline_list {
144 struct cmdline_list *next;
145 char *comm;
146 int pid;
149 static int cmdline_init(struct tep_handle *pevent)
151 struct cmdline_list *cmdlist = pevent->cmdlist;
152 struct cmdline_list *item;
153 struct cmdline *cmdlines;
154 int i;
156 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
157 if (!cmdlines)
158 return -1;
160 i = 0;
161 while (cmdlist) {
162 cmdlines[i].pid = cmdlist->pid;
163 cmdlines[i].comm = cmdlist->comm;
164 i++;
165 item = cmdlist;
166 cmdlist = cmdlist->next;
167 free(item);
170 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
172 pevent->cmdlines = cmdlines;
173 pevent->cmdlist = NULL;
175 return 0;
178 static const char *find_cmdline(struct tep_handle *pevent, int pid)
180 const struct cmdline *comm;
181 struct cmdline key;
183 if (!pid)
184 return "<idle>";
186 if (!pevent->cmdlines && cmdline_init(pevent))
187 return "<not enough memory for cmdlines!>";
189 key.pid = pid;
191 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
192 sizeof(*pevent->cmdlines), cmdline_cmp);
194 if (comm)
195 return comm->comm;
196 return "<...>";
200 * tep_pid_is_registered - return if a pid has a cmdline registered
201 * @pevent: handle for the pevent
202 * @pid: The pid to check if it has a cmdline registered with.
204 * Returns 1 if the pid has a cmdline mapped to it
205 * 0 otherwise.
207 int tep_pid_is_registered(struct tep_handle *pevent, int pid)
209 const struct cmdline *comm;
210 struct cmdline key;
212 if (!pid)
213 return 1;
215 if (!pevent->cmdlines && cmdline_init(pevent))
216 return 0;
218 key.pid = pid;
220 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
221 sizeof(*pevent->cmdlines), cmdline_cmp);
223 if (comm)
224 return 1;
225 return 0;
229 * If the command lines have been converted to an array, then
230 * we must add this pid. This is much slower than when cmdlines
231 * are added before the array is initialized.
233 static int add_new_comm(struct tep_handle *pevent, const char *comm, int pid)
235 struct cmdline *cmdlines = pevent->cmdlines;
236 const struct cmdline *cmdline;
237 struct cmdline key;
239 if (!pid)
240 return 0;
242 /* avoid duplicates */
243 key.pid = pid;
245 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
246 sizeof(*pevent->cmdlines), cmdline_cmp);
247 if (cmdline) {
248 errno = EEXIST;
249 return -1;
252 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
253 if (!cmdlines) {
254 errno = ENOMEM;
255 return -1;
258 cmdlines[pevent->cmdline_count].comm = strdup(comm);
259 if (!cmdlines[pevent->cmdline_count].comm) {
260 free(cmdlines);
261 errno = ENOMEM;
262 return -1;
265 cmdlines[pevent->cmdline_count].pid = pid;
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 * tep_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 tep_register_comm(struct tep_handle *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(sizeof(*item));
293 if (!item)
294 return -1;
296 if (comm)
297 item->comm = strdup(comm);
298 else
299 item->comm = strdup("<...>");
300 if (!item->comm) {
301 free(item);
302 return -1;
304 item->pid = pid;
305 item->next = pevent->cmdlist;
307 pevent->cmdlist = item;
308 pevent->cmdline_count++;
310 return 0;
313 int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
315 pevent->trace_clock = strdup(trace_clock);
316 if (!pevent->trace_clock) {
317 errno = ENOMEM;
318 return -1;
320 return 0;
323 struct func_map {
324 unsigned long long addr;
325 char *func;
326 char *mod;
329 struct func_list {
330 struct func_list *next;
331 unsigned long long addr;
332 char *func;
333 char *mod;
336 static int func_cmp(const void *a, const void *b)
338 const struct func_map *fa = a;
339 const struct func_map *fb = b;
341 if (fa->addr < fb->addr)
342 return -1;
343 if (fa->addr > fb->addr)
344 return 1;
346 return 0;
350 * We are searching for a record in between, not an exact
351 * match.
353 static int func_bcmp(const void *a, const void *b)
355 const struct func_map *fa = a;
356 const struct func_map *fb = b;
358 if ((fa->addr == fb->addr) ||
360 (fa->addr > fb->addr &&
361 fa->addr < (fb+1)->addr))
362 return 0;
364 if (fa->addr < fb->addr)
365 return -1;
367 return 1;
370 static int func_map_init(struct tep_handle *pevent)
372 struct func_list *funclist;
373 struct func_list *item;
374 struct func_map *func_map;
375 int i;
377 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
378 if (!func_map)
379 return -1;
381 funclist = pevent->funclist;
383 i = 0;
384 while (funclist) {
385 func_map[i].func = funclist->func;
386 func_map[i].addr = funclist->addr;
387 func_map[i].mod = funclist->mod;
388 i++;
389 item = funclist;
390 funclist = funclist->next;
391 free(item);
394 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
397 * Add a special record at the end.
399 func_map[pevent->func_count].func = NULL;
400 func_map[pevent->func_count].addr = 0;
401 func_map[pevent->func_count].mod = NULL;
403 pevent->func_map = func_map;
404 pevent->funclist = NULL;
406 return 0;
409 static struct func_map *
410 __find_func(struct tep_handle *pevent, unsigned long long addr)
412 struct func_map *func;
413 struct func_map key;
415 if (!pevent->func_map)
416 func_map_init(pevent);
418 key.addr = addr;
420 func = bsearch(&key, pevent->func_map, pevent->func_count,
421 sizeof(*pevent->func_map), func_bcmp);
423 return func;
426 struct func_resolver {
427 tep_func_resolver_t *func;
428 void *priv;
429 struct func_map map;
433 * tep_set_function_resolver - set an alternative function resolver
434 * @pevent: handle for the pevent
435 * @resolver: function to be used
436 * @priv: resolver function private state.
438 * Some tools may have already a way to resolve kernel functions, allow them to
439 * keep using it instead of duplicating all the entries inside
440 * pevent->funclist.
442 int tep_set_function_resolver(struct tep_handle *pevent,
443 tep_func_resolver_t *func, void *priv)
445 struct func_resolver *resolver = malloc(sizeof(*resolver));
447 if (resolver == NULL)
448 return -1;
450 resolver->func = func;
451 resolver->priv = priv;
453 free(pevent->func_resolver);
454 pevent->func_resolver = resolver;
456 return 0;
460 * tep_reset_function_resolver - reset alternative function resolver
461 * @pevent: handle for the pevent
463 * Stop using whatever alternative resolver was set, use the default
464 * one instead.
466 void tep_reset_function_resolver(struct tep_handle *pevent)
468 free(pevent->func_resolver);
469 pevent->func_resolver = NULL;
472 static struct func_map *
473 find_func(struct tep_handle *pevent, unsigned long long addr)
475 struct func_map *map;
477 if (!pevent->func_resolver)
478 return __find_func(pevent, addr);
480 map = &pevent->func_resolver->map;
481 map->mod = NULL;
482 map->addr = addr;
483 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
484 &map->addr, &map->mod);
485 if (map->func == NULL)
486 return NULL;
488 return map;
492 * tep_find_function - find a function by a given address
493 * @pevent: handle for the pevent
494 * @addr: the address to find the function with
496 * Returns a pointer to the function stored that has the given
497 * address. Note, the address does not have to be exact, it
498 * will select the function that would contain the address.
500 const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
502 struct func_map *map;
504 map = find_func(pevent, addr);
505 if (!map)
506 return NULL;
508 return map->func;
512 * tep_find_function_address - find a function address by a given address
513 * @pevent: handle for the pevent
514 * @addr: the address to find the function with
516 * Returns the address the function starts at. This can be used in
517 * conjunction with tep_find_function to print both the function
518 * name and the function offset.
520 unsigned long long
521 tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
523 struct func_map *map;
525 map = find_func(pevent, addr);
526 if (!map)
527 return 0;
529 return map->addr;
533 * tep_register_function - register a function with a given address
534 * @pevent: handle for the pevent
535 * @function: the function name to register
536 * @addr: the address the function starts at
537 * @mod: the kernel module the function may be in (NULL for none)
539 * This registers a function name with an address and module.
540 * The @func passed in is duplicated.
542 int tep_register_function(struct tep_handle *pevent, char *func,
543 unsigned long long addr, char *mod)
545 struct func_list *item = malloc(sizeof(*item));
547 if (!item)
548 return -1;
550 item->next = pevent->funclist;
551 item->func = strdup(func);
552 if (!item->func)
553 goto out_free;
555 if (mod) {
556 item->mod = strdup(mod);
557 if (!item->mod)
558 goto out_free_func;
559 } else
560 item->mod = NULL;
561 item->addr = addr;
563 pevent->funclist = item;
564 pevent->func_count++;
566 return 0;
568 out_free_func:
569 free(item->func);
570 item->func = NULL;
571 out_free:
572 free(item);
573 errno = ENOMEM;
574 return -1;
578 * tep_print_funcs - print out the stored functions
579 * @pevent: handle for the pevent
581 * This prints out the stored functions.
583 void tep_print_funcs(struct tep_handle *pevent)
585 int i;
587 if (!pevent->func_map)
588 func_map_init(pevent);
590 for (i = 0; i < (int)pevent->func_count; i++) {
591 printf("%016llx %s",
592 pevent->func_map[i].addr,
593 pevent->func_map[i].func);
594 if (pevent->func_map[i].mod)
595 printf(" [%s]\n", pevent->func_map[i].mod);
596 else
597 printf("\n");
601 struct printk_map {
602 unsigned long long addr;
603 char *printk;
606 struct printk_list {
607 struct printk_list *next;
608 unsigned long long addr;
609 char *printk;
612 static int printk_cmp(const void *a, const void *b)
614 const struct printk_map *pa = a;
615 const struct printk_map *pb = b;
617 if (pa->addr < pb->addr)
618 return -1;
619 if (pa->addr > pb->addr)
620 return 1;
622 return 0;
625 static int printk_map_init(struct tep_handle *pevent)
627 struct printk_list *printklist;
628 struct printk_list *item;
629 struct printk_map *printk_map;
630 int i;
632 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
633 if (!printk_map)
634 return -1;
636 printklist = pevent->printklist;
638 i = 0;
639 while (printklist) {
640 printk_map[i].printk = printklist->printk;
641 printk_map[i].addr = printklist->addr;
642 i++;
643 item = printklist;
644 printklist = printklist->next;
645 free(item);
648 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
650 pevent->printk_map = printk_map;
651 pevent->printklist = NULL;
653 return 0;
656 static struct printk_map *
657 find_printk(struct tep_handle *pevent, unsigned long long addr)
659 struct printk_map *printk;
660 struct printk_map key;
662 if (!pevent->printk_map && printk_map_init(pevent))
663 return NULL;
665 key.addr = addr;
667 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
668 sizeof(*pevent->printk_map), printk_cmp);
670 return printk;
674 * tep_register_print_string - register a string by its address
675 * @pevent: handle for the pevent
676 * @fmt: the string format to register
677 * @addr: the address the string was located at
679 * This registers a string by the address it was stored in the kernel.
680 * The @fmt passed in is duplicated.
682 int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
683 unsigned long long addr)
685 struct printk_list *item = malloc(sizeof(*item));
686 char *p;
688 if (!item)
689 return -1;
691 item->next = pevent->printklist;
692 item->addr = addr;
694 /* Strip off quotes and '\n' from the end */
695 if (fmt[0] == '"')
696 fmt++;
697 item->printk = strdup(fmt);
698 if (!item->printk)
699 goto out_free;
701 p = item->printk + strlen(item->printk) - 1;
702 if (*p == '"')
703 *p = 0;
705 p -= 2;
706 if (strcmp(p, "\\n") == 0)
707 *p = 0;
709 pevent->printklist = item;
710 pevent->printk_count++;
712 return 0;
714 out_free:
715 free(item);
716 errno = ENOMEM;
717 return -1;
721 * tep_print_printk - print out the stored strings
722 * @pevent: handle for the pevent
724 * This prints the string formats that were stored.
726 void tep_print_printk(struct tep_handle *pevent)
728 int i;
730 if (!pevent->printk_map)
731 printk_map_init(pevent);
733 for (i = 0; i < (int)pevent->printk_count; i++) {
734 printf("%016llx %s\n",
735 pevent->printk_map[i].addr,
736 pevent->printk_map[i].printk);
740 static struct event_format *alloc_event(void)
742 return calloc(1, sizeof(struct event_format));
745 static int add_event(struct tep_handle *pevent, struct event_format *event)
747 int i;
748 struct event_format **events = realloc(pevent->events, sizeof(event) *
749 (pevent->nr_events + 1));
750 if (!events)
751 return -1;
753 pevent->events = events;
755 for (i = 0; i < pevent->nr_events; i++) {
756 if (pevent->events[i]->id > event->id)
757 break;
759 if (i < pevent->nr_events)
760 memmove(&pevent->events[i + 1],
761 &pevent->events[i],
762 sizeof(event) * (pevent->nr_events - i));
764 pevent->events[i] = event;
765 pevent->nr_events++;
767 event->pevent = pevent;
769 return 0;
772 static int event_item_type(enum event_type type)
774 switch (type) {
775 case EVENT_ITEM ... EVENT_SQUOTE:
776 return 1;
777 case EVENT_ERROR ... EVENT_DELIM:
778 default:
779 return 0;
783 static void free_flag_sym(struct print_flag_sym *fsym)
785 struct print_flag_sym *next;
787 while (fsym) {
788 next = fsym->next;
789 free(fsym->value);
790 free(fsym->str);
791 free(fsym);
792 fsym = next;
796 static void free_arg(struct print_arg *arg)
798 struct print_arg *farg;
800 if (!arg)
801 return;
803 switch (arg->type) {
804 case PRINT_ATOM:
805 free(arg->atom.atom);
806 break;
807 case PRINT_FIELD:
808 free(arg->field.name);
809 break;
810 case PRINT_FLAGS:
811 free_arg(arg->flags.field);
812 free(arg->flags.delim);
813 free_flag_sym(arg->flags.flags);
814 break;
815 case PRINT_SYMBOL:
816 free_arg(arg->symbol.field);
817 free_flag_sym(arg->symbol.symbols);
818 break;
819 case PRINT_HEX:
820 case PRINT_HEX_STR:
821 free_arg(arg->hex.field);
822 free_arg(arg->hex.size);
823 break;
824 case PRINT_INT_ARRAY:
825 free_arg(arg->int_array.field);
826 free_arg(arg->int_array.count);
827 free_arg(arg->int_array.el_size);
828 break;
829 case PRINT_TYPE:
830 free(arg->typecast.type);
831 free_arg(arg->typecast.item);
832 break;
833 case PRINT_STRING:
834 case PRINT_BSTRING:
835 free(arg->string.string);
836 break;
837 case PRINT_BITMASK:
838 free(arg->bitmask.bitmask);
839 break;
840 case PRINT_DYNAMIC_ARRAY:
841 case PRINT_DYNAMIC_ARRAY_LEN:
842 free(arg->dynarray.index);
843 break;
844 case PRINT_OP:
845 free(arg->op.op);
846 free_arg(arg->op.left);
847 free_arg(arg->op.right);
848 break;
849 case PRINT_FUNC:
850 while (arg->func.args) {
851 farg = arg->func.args;
852 arg->func.args = farg->next;
853 free_arg(farg);
855 break;
857 case PRINT_NULL:
858 default:
859 break;
862 free(arg);
865 static enum event_type get_type(int ch)
867 if (ch == '\n')
868 return EVENT_NEWLINE;
869 if (isspace(ch))
870 return EVENT_SPACE;
871 if (isalnum(ch) || ch == '_')
872 return EVENT_ITEM;
873 if (ch == '\'')
874 return EVENT_SQUOTE;
875 if (ch == '"')
876 return EVENT_DQUOTE;
877 if (!isprint(ch))
878 return EVENT_NONE;
879 if (ch == '(' || ch == ')' || ch == ',')
880 return EVENT_DELIM;
882 return EVENT_OP;
885 static int __read_char(void)
887 if (input_buf_ptr >= input_buf_siz)
888 return -1;
890 return input_buf[input_buf_ptr++];
893 static int __peek_char(void)
895 if (input_buf_ptr >= input_buf_siz)
896 return -1;
898 return input_buf[input_buf_ptr];
902 * tep_peek_char - peek at the next character that will be read
904 * Returns the next character read, or -1 if end of buffer.
906 int tep_peek_char(void)
908 return __peek_char();
911 static int extend_token(char **tok, char *buf, int size)
913 char *newtok = realloc(*tok, size);
915 if (!newtok) {
916 free(*tok);
917 *tok = NULL;
918 return -1;
921 if (!*tok)
922 strcpy(newtok, buf);
923 else
924 strcat(newtok, buf);
925 *tok = newtok;
927 return 0;
930 static enum event_type force_token(const char *str, char **tok);
932 static enum event_type __read_token(char **tok)
934 char buf[BUFSIZ];
935 int ch, last_ch, quote_ch, next_ch;
936 int i = 0;
937 int tok_size = 0;
938 enum event_type type;
940 *tok = NULL;
943 ch = __read_char();
944 if (ch < 0)
945 return EVENT_NONE;
947 type = get_type(ch);
948 if (type == EVENT_NONE)
949 return type;
951 buf[i++] = ch;
953 switch (type) {
954 case EVENT_NEWLINE:
955 case EVENT_DELIM:
956 if (asprintf(tok, "%c", ch) < 0)
957 return EVENT_ERROR;
959 return type;
961 case EVENT_OP:
962 switch (ch) {
963 case '-':
964 next_ch = __peek_char();
965 if (next_ch == '>') {
966 buf[i++] = __read_char();
967 break;
969 /* fall through */
970 case '+':
971 case '|':
972 case '&':
973 case '>':
974 case '<':
975 last_ch = ch;
976 ch = __peek_char();
977 if (ch != last_ch)
978 goto test_equal;
979 buf[i++] = __read_char();
980 switch (last_ch) {
981 case '>':
982 case '<':
983 goto test_equal;
984 default:
985 break;
987 break;
988 case '!':
989 case '=':
990 goto test_equal;
991 default: /* what should we do instead? */
992 break;
994 buf[i] = 0;
995 *tok = strdup(buf);
996 return type;
998 test_equal:
999 ch = __peek_char();
1000 if (ch == '=')
1001 buf[i++] = __read_char();
1002 goto out;
1004 case EVENT_DQUOTE:
1005 case EVENT_SQUOTE:
1006 /* don't keep quotes */
1007 i--;
1008 quote_ch = ch;
1009 last_ch = 0;
1010 concat:
1011 do {
1012 if (i == (BUFSIZ - 1)) {
1013 buf[i] = 0;
1014 tok_size += BUFSIZ;
1016 if (extend_token(tok, buf, tok_size) < 0)
1017 return EVENT_NONE;
1018 i = 0;
1020 last_ch = ch;
1021 ch = __read_char();
1022 buf[i++] = ch;
1023 /* the '\' '\' will cancel itself */
1024 if (ch == '\\' && last_ch == '\\')
1025 last_ch = 0;
1026 } while (ch != quote_ch || last_ch == '\\');
1027 /* remove the last quote */
1028 i--;
1031 * For strings (double quotes) check the next token.
1032 * If it is another string, concatinate the two.
1034 if (type == EVENT_DQUOTE) {
1035 unsigned long long save_input_buf_ptr = input_buf_ptr;
1037 do {
1038 ch = __read_char();
1039 } while (isspace(ch));
1040 if (ch == '"')
1041 goto concat;
1042 input_buf_ptr = save_input_buf_ptr;
1045 goto out;
1047 case EVENT_ERROR ... EVENT_SPACE:
1048 case EVENT_ITEM:
1049 default:
1050 break;
1053 while (get_type(__peek_char()) == type) {
1054 if (i == (BUFSIZ - 1)) {
1055 buf[i] = 0;
1056 tok_size += BUFSIZ;
1058 if (extend_token(tok, buf, tok_size) < 0)
1059 return EVENT_NONE;
1060 i = 0;
1062 ch = __read_char();
1063 buf[i++] = ch;
1066 out:
1067 buf[i] = 0;
1068 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1069 return EVENT_NONE;
1071 if (type == EVENT_ITEM) {
1073 * Older versions of the kernel has a bug that
1074 * creates invalid symbols and will break the mac80211
1075 * parsing. This is a work around to that bug.
1077 * See Linux kernel commit:
1078 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1080 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1081 free(*tok);
1082 *tok = NULL;
1083 return force_token("\"%s\" ", tok);
1084 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1085 free(*tok);
1086 *tok = NULL;
1087 return force_token("\" sta:%pM\" ", tok);
1088 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1089 free(*tok);
1090 *tok = NULL;
1091 return force_token("\" vif:%p(%d)\" ", tok);
1095 return type;
1098 static enum event_type force_token(const char *str, char **tok)
1100 const char *save_input_buf;
1101 unsigned long long save_input_buf_ptr;
1102 unsigned long long save_input_buf_siz;
1103 enum event_type type;
1105 /* save off the current input pointers */
1106 save_input_buf = input_buf;
1107 save_input_buf_ptr = input_buf_ptr;
1108 save_input_buf_siz = input_buf_siz;
1110 init_input_buf(str, strlen(str));
1112 type = __read_token(tok);
1114 /* reset back to original token */
1115 input_buf = save_input_buf;
1116 input_buf_ptr = save_input_buf_ptr;
1117 input_buf_siz = save_input_buf_siz;
1119 return type;
1122 static void free_token(char *tok)
1124 if (tok)
1125 free(tok);
1128 static enum event_type read_token(char **tok)
1130 enum event_type type;
1132 for (;;) {
1133 type = __read_token(tok);
1134 if (type != EVENT_SPACE)
1135 return type;
1137 free_token(*tok);
1140 /* not reached */
1141 *tok = NULL;
1142 return EVENT_NONE;
1146 * tep_read_token - access to utilites to use the pevent parser
1147 * @tok: The token to return
1149 * This will parse tokens from the string given by
1150 * tep_init_data().
1152 * Returns the token type.
1154 enum event_type tep_read_token(char **tok)
1156 return read_token(tok);
1160 * tep_free_token - free a token returned by tep_read_token
1161 * @token: the token to free
1163 void tep_free_token(char *token)
1165 free_token(token);
1168 /* no newline */
1169 static enum event_type read_token_item(char **tok)
1171 enum event_type type;
1173 for (;;) {
1174 type = __read_token(tok);
1175 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1176 return type;
1177 free_token(*tok);
1178 *tok = NULL;
1181 /* not reached */
1182 *tok = NULL;
1183 return EVENT_NONE;
1186 static int test_type(enum event_type type, enum event_type expect)
1188 if (type != expect) {
1189 do_warning("Error: expected type %d but read %d",
1190 expect, type);
1191 return -1;
1193 return 0;
1196 static int test_type_token(enum event_type type, const char *token,
1197 enum event_type expect, const char *expect_tok)
1199 if (type != expect) {
1200 do_warning("Error: expected type %d but read %d",
1201 expect, type);
1202 return -1;
1205 if (strcmp(token, expect_tok) != 0) {
1206 do_warning("Error: expected '%s' but read '%s'",
1207 expect_tok, token);
1208 return -1;
1210 return 0;
1213 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1215 enum event_type type;
1217 if (newline_ok)
1218 type = read_token(tok);
1219 else
1220 type = read_token_item(tok);
1221 return test_type(type, expect);
1224 static int read_expect_type(enum event_type expect, char **tok)
1226 return __read_expect_type(expect, tok, 1);
1229 static int __read_expected(enum event_type expect, const char *str,
1230 int newline_ok)
1232 enum event_type type;
1233 char *token;
1234 int ret;
1236 if (newline_ok)
1237 type = read_token(&token);
1238 else
1239 type = read_token_item(&token);
1241 ret = test_type_token(type, token, expect, str);
1243 free_token(token);
1245 return ret;
1248 static int read_expected(enum event_type expect, const char *str)
1250 return __read_expected(expect, str, 1);
1253 static int read_expected_item(enum event_type expect, const char *str)
1255 return __read_expected(expect, str, 0);
1258 static char *event_read_name(void)
1260 char *token;
1262 if (read_expected(EVENT_ITEM, "name") < 0)
1263 return NULL;
1265 if (read_expected(EVENT_OP, ":") < 0)
1266 return NULL;
1268 if (read_expect_type(EVENT_ITEM, &token) < 0)
1269 goto fail;
1271 return token;
1273 fail:
1274 free_token(token);
1275 return NULL;
1278 static int event_read_id(void)
1280 char *token;
1281 int id;
1283 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1284 return -1;
1286 if (read_expected(EVENT_OP, ":") < 0)
1287 return -1;
1289 if (read_expect_type(EVENT_ITEM, &token) < 0)
1290 goto fail;
1292 id = strtoul(token, NULL, 0);
1293 free_token(token);
1294 return id;
1296 fail:
1297 free_token(token);
1298 return -1;
1301 static int field_is_string(struct format_field *field)
1303 if ((field->flags & FIELD_IS_ARRAY) &&
1304 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1305 strstr(field->type, "s8")))
1306 return 1;
1308 return 0;
1311 static int field_is_dynamic(struct format_field *field)
1313 if (strncmp(field->type, "__data_loc", 10) == 0)
1314 return 1;
1316 return 0;
1319 static int field_is_long(struct format_field *field)
1321 /* includes long long */
1322 if (strstr(field->type, "long"))
1323 return 1;
1325 return 0;
1328 static unsigned int type_size(const char *name)
1330 /* This covers all FIELD_IS_STRING types. */
1331 static struct {
1332 const char *type;
1333 unsigned int size;
1334 } table[] = {
1335 { "u8", 1 },
1336 { "u16", 2 },
1337 { "u32", 4 },
1338 { "u64", 8 },
1339 { "s8", 1 },
1340 { "s16", 2 },
1341 { "s32", 4 },
1342 { "s64", 8 },
1343 { "char", 1 },
1344 { },
1346 int i;
1348 for (i = 0; table[i].type; i++) {
1349 if (!strcmp(table[i].type, name))
1350 return table[i].size;
1353 return 0;
1356 static int event_read_fields(struct event_format *event, struct format_field **fields)
1358 struct format_field *field = NULL;
1359 enum event_type type;
1360 char *token;
1361 char *last_token;
1362 int count = 0;
1364 do {
1365 unsigned int size_dynamic = 0;
1367 type = read_token(&token);
1368 if (type == EVENT_NEWLINE) {
1369 free_token(token);
1370 return count;
1373 count++;
1375 if (test_type_token(type, token, EVENT_ITEM, "field"))
1376 goto fail;
1377 free_token(token);
1379 type = read_token(&token);
1381 * The ftrace fields may still use the "special" name.
1382 * Just ignore it.
1384 if (event->flags & EVENT_FL_ISFTRACE &&
1385 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1386 free_token(token);
1387 type = read_token(&token);
1390 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1391 goto fail;
1393 free_token(token);
1394 if (read_expect_type(EVENT_ITEM, &token) < 0)
1395 goto fail;
1397 last_token = token;
1399 field = calloc(1, sizeof(*field));
1400 if (!field)
1401 goto fail;
1403 field->event = event;
1405 /* read the rest of the type */
1406 for (;;) {
1407 type = read_token(&token);
1408 if (type == EVENT_ITEM ||
1409 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1411 * Some of the ftrace fields are broken and have
1412 * an illegal "." in them.
1414 (event->flags & EVENT_FL_ISFTRACE &&
1415 type == EVENT_OP && strcmp(token, ".") == 0)) {
1417 if (strcmp(token, "*") == 0)
1418 field->flags |= FIELD_IS_POINTER;
1420 if (field->type) {
1421 char *new_type;
1422 new_type = realloc(field->type,
1423 strlen(field->type) +
1424 strlen(last_token) + 2);
1425 if (!new_type) {
1426 free(last_token);
1427 goto fail;
1429 field->type = new_type;
1430 strcat(field->type, " ");
1431 strcat(field->type, last_token);
1432 free(last_token);
1433 } else
1434 field->type = last_token;
1435 last_token = token;
1436 continue;
1439 break;
1442 if (!field->type) {
1443 do_warning_event(event, "%s: no type found", __func__);
1444 goto fail;
1446 field->name = field->alias = last_token;
1448 if (test_type(type, EVENT_OP))
1449 goto fail;
1451 if (strcmp(token, "[") == 0) {
1452 enum event_type last_type = type;
1453 char *brackets = token;
1454 char *new_brackets;
1455 int len;
1457 field->flags |= FIELD_IS_ARRAY;
1459 type = read_token(&token);
1461 if (type == EVENT_ITEM)
1462 field->arraylen = strtoul(token, NULL, 0);
1463 else
1464 field->arraylen = 0;
1466 while (strcmp(token, "]") != 0) {
1467 if (last_type == EVENT_ITEM &&
1468 type == EVENT_ITEM)
1469 len = 2;
1470 else
1471 len = 1;
1472 last_type = type;
1474 new_brackets = realloc(brackets,
1475 strlen(brackets) +
1476 strlen(token) + len);
1477 if (!new_brackets) {
1478 free(brackets);
1479 goto fail;
1481 brackets = new_brackets;
1482 if (len == 2)
1483 strcat(brackets, " ");
1484 strcat(brackets, token);
1485 /* We only care about the last token */
1486 field->arraylen = strtoul(token, NULL, 0);
1487 free_token(token);
1488 type = read_token(&token);
1489 if (type == EVENT_NONE) {
1490 do_warning_event(event, "failed to find token");
1491 goto fail;
1495 free_token(token);
1497 new_brackets = realloc(brackets, strlen(brackets) + 2);
1498 if (!new_brackets) {
1499 free(brackets);
1500 goto fail;
1502 brackets = new_brackets;
1503 strcat(brackets, "]");
1505 /* add brackets to type */
1507 type = read_token(&token);
1509 * If the next token is not an OP, then it is of
1510 * the format: type [] item;
1512 if (type == EVENT_ITEM) {
1513 char *new_type;
1514 new_type = realloc(field->type,
1515 strlen(field->type) +
1516 strlen(field->name) +
1517 strlen(brackets) + 2);
1518 if (!new_type) {
1519 free(brackets);
1520 goto fail;
1522 field->type = new_type;
1523 strcat(field->type, " ");
1524 strcat(field->type, field->name);
1525 size_dynamic = type_size(field->name);
1526 free_token(field->name);
1527 strcat(field->type, brackets);
1528 field->name = field->alias = token;
1529 type = read_token(&token);
1530 } else {
1531 char *new_type;
1532 new_type = realloc(field->type,
1533 strlen(field->type) +
1534 strlen(brackets) + 1);
1535 if (!new_type) {
1536 free(brackets);
1537 goto fail;
1539 field->type = new_type;
1540 strcat(field->type, brackets);
1542 free(brackets);
1545 if (field_is_string(field))
1546 field->flags |= FIELD_IS_STRING;
1547 if (field_is_dynamic(field))
1548 field->flags |= FIELD_IS_DYNAMIC;
1549 if (field_is_long(field))
1550 field->flags |= FIELD_IS_LONG;
1552 if (test_type_token(type, token, EVENT_OP, ";"))
1553 goto fail;
1554 free_token(token);
1556 if (read_expected(EVENT_ITEM, "offset") < 0)
1557 goto fail_expect;
1559 if (read_expected(EVENT_OP, ":") < 0)
1560 goto fail_expect;
1562 if (read_expect_type(EVENT_ITEM, &token))
1563 goto fail;
1564 field->offset = strtoul(token, NULL, 0);
1565 free_token(token);
1567 if (read_expected(EVENT_OP, ";") < 0)
1568 goto fail_expect;
1570 if (read_expected(EVENT_ITEM, "size") < 0)
1571 goto fail_expect;
1573 if (read_expected(EVENT_OP, ":") < 0)
1574 goto fail_expect;
1576 if (read_expect_type(EVENT_ITEM, &token))
1577 goto fail;
1578 field->size = strtoul(token, NULL, 0);
1579 free_token(token);
1581 if (read_expected(EVENT_OP, ";") < 0)
1582 goto fail_expect;
1584 type = read_token(&token);
1585 if (type != EVENT_NEWLINE) {
1586 /* newer versions of the kernel have a "signed" type */
1587 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1588 goto fail;
1590 free_token(token);
1592 if (read_expected(EVENT_OP, ":") < 0)
1593 goto fail_expect;
1595 if (read_expect_type(EVENT_ITEM, &token))
1596 goto fail;
1598 if (strtoul(token, NULL, 0))
1599 field->flags |= FIELD_IS_SIGNED;
1601 free_token(token);
1602 if (read_expected(EVENT_OP, ";") < 0)
1603 goto fail_expect;
1605 if (read_expect_type(EVENT_NEWLINE, &token))
1606 goto fail;
1609 free_token(token);
1611 if (field->flags & FIELD_IS_ARRAY) {
1612 if (field->arraylen)
1613 field->elementsize = field->size / field->arraylen;
1614 else if (field->flags & FIELD_IS_DYNAMIC)
1615 field->elementsize = size_dynamic;
1616 else if (field->flags & FIELD_IS_STRING)
1617 field->elementsize = 1;
1618 else if (field->flags & FIELD_IS_LONG)
1619 field->elementsize = event->pevent ?
1620 event->pevent->long_size :
1621 sizeof(long);
1622 } else
1623 field->elementsize = field->size;
1625 *fields = field;
1626 fields = &field->next;
1628 } while (1);
1630 return 0;
1632 fail:
1633 free_token(token);
1634 fail_expect:
1635 if (field) {
1636 free(field->type);
1637 free(field->name);
1638 free(field);
1640 return -1;
1643 static int event_read_format(struct event_format *event)
1645 char *token;
1646 int ret;
1648 if (read_expected_item(EVENT_ITEM, "format") < 0)
1649 return -1;
1651 if (read_expected(EVENT_OP, ":") < 0)
1652 return -1;
1654 if (read_expect_type(EVENT_NEWLINE, &token))
1655 goto fail;
1656 free_token(token);
1658 ret = event_read_fields(event, &event->format.common_fields);
1659 if (ret < 0)
1660 return ret;
1661 event->format.nr_common = ret;
1663 ret = event_read_fields(event, &event->format.fields);
1664 if (ret < 0)
1665 return ret;
1666 event->format.nr_fields = ret;
1668 return 0;
1670 fail:
1671 free_token(token);
1672 return -1;
1675 static enum event_type
1676 process_arg_token(struct event_format *event, struct print_arg *arg,
1677 char **tok, enum event_type type);
1679 static enum event_type
1680 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1682 enum event_type type;
1683 char *token;
1685 type = read_token(&token);
1686 *tok = token;
1688 return process_arg_token(event, arg, tok, type);
1691 static enum event_type
1692 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1695 * For __print_symbolic() and __print_flags, we need to completely
1696 * evaluate the first argument, which defines what to print next.
1698 static enum event_type
1699 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1701 enum event_type type;
1703 type = process_arg(event, arg, tok);
1705 while (type == EVENT_OP) {
1706 type = process_op(event, arg, tok);
1709 return type;
1712 static enum event_type
1713 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1715 struct print_arg *arg, *left, *right;
1716 enum event_type type;
1717 char *token = NULL;
1719 arg = alloc_arg();
1720 left = alloc_arg();
1721 right = alloc_arg();
1723 if (!arg || !left || !right) {
1724 do_warning_event(event, "%s: not enough memory!", __func__);
1725 /* arg will be freed at out_free */
1726 free_arg(left);
1727 free_arg(right);
1728 goto out_free;
1731 arg->type = PRINT_OP;
1732 arg->op.left = left;
1733 arg->op.right = right;
1735 *tok = NULL;
1736 type = process_arg(event, left, &token);
1738 again:
1739 if (type == EVENT_ERROR)
1740 goto out_free;
1742 /* Handle other operations in the arguments */
1743 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1744 type = process_op(event, left, &token);
1745 goto again;
1748 if (test_type_token(type, token, EVENT_OP, ":"))
1749 goto out_free;
1751 arg->op.op = token;
1753 type = process_arg(event, right, &token);
1755 top->op.right = arg;
1757 *tok = token;
1758 return type;
1760 out_free:
1761 /* Top may point to itself */
1762 top->op.right = NULL;
1763 free_token(token);
1764 free_arg(arg);
1765 return EVENT_ERROR;
1768 static enum event_type
1769 process_array(struct event_format *event, struct print_arg *top, char **tok)
1771 struct print_arg *arg;
1772 enum event_type type;
1773 char *token = NULL;
1775 arg = alloc_arg();
1776 if (!arg) {
1777 do_warning_event(event, "%s: not enough memory!", __func__);
1778 /* '*tok' is set to top->op.op. No need to free. */
1779 *tok = NULL;
1780 return EVENT_ERROR;
1783 *tok = NULL;
1784 type = process_arg(event, arg, &token);
1785 if (test_type_token(type, token, EVENT_OP, "]"))
1786 goto out_free;
1788 top->op.right = arg;
1790 free_token(token);
1791 type = read_token_item(&token);
1792 *tok = token;
1794 return type;
1796 out_free:
1797 free_token(token);
1798 free_arg(arg);
1799 return EVENT_ERROR;
1802 static int get_op_prio(char *op)
1804 if (!op[1]) {
1805 switch (op[0]) {
1806 case '~':
1807 case '!':
1808 return 4;
1809 case '*':
1810 case '/':
1811 case '%':
1812 return 6;
1813 case '+':
1814 case '-':
1815 return 7;
1816 /* '>>' and '<<' are 8 */
1817 case '<':
1818 case '>':
1819 return 9;
1820 /* '==' and '!=' are 10 */
1821 case '&':
1822 return 11;
1823 case '^':
1824 return 12;
1825 case '|':
1826 return 13;
1827 case '?':
1828 return 16;
1829 default:
1830 do_warning("unknown op '%c'", op[0]);
1831 return -1;
1833 } else {
1834 if (strcmp(op, "++") == 0 ||
1835 strcmp(op, "--") == 0) {
1836 return 3;
1837 } else if (strcmp(op, ">>") == 0 ||
1838 strcmp(op, "<<") == 0) {
1839 return 8;
1840 } else if (strcmp(op, ">=") == 0 ||
1841 strcmp(op, "<=") == 0) {
1842 return 9;
1843 } else if (strcmp(op, "==") == 0 ||
1844 strcmp(op, "!=") == 0) {
1845 return 10;
1846 } else if (strcmp(op, "&&") == 0) {
1847 return 14;
1848 } else if (strcmp(op, "||") == 0) {
1849 return 15;
1850 } else {
1851 do_warning("unknown op '%s'", op);
1852 return -1;
1857 static int set_op_prio(struct print_arg *arg)
1860 /* single ops are the greatest */
1861 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1862 arg->op.prio = 0;
1863 else
1864 arg->op.prio = get_op_prio(arg->op.op);
1866 return arg->op.prio;
1869 /* Note, *tok does not get freed, but will most likely be saved */
1870 static enum event_type
1871 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1873 struct print_arg *left, *right = NULL;
1874 enum event_type type;
1875 char *token;
1877 /* the op is passed in via tok */
1878 token = *tok;
1880 if (arg->type == PRINT_OP && !arg->op.left) {
1881 /* handle single op */
1882 if (token[1]) {
1883 do_warning_event(event, "bad op token %s", token);
1884 goto out_free;
1886 switch (token[0]) {
1887 case '~':
1888 case '!':
1889 case '+':
1890 case '-':
1891 break;
1892 default:
1893 do_warning_event(event, "bad op token %s", token);
1894 goto out_free;
1898 /* make an empty left */
1899 left = alloc_arg();
1900 if (!left)
1901 goto out_warn_free;
1903 left->type = PRINT_NULL;
1904 arg->op.left = left;
1906 right = alloc_arg();
1907 if (!right)
1908 goto out_warn_free;
1910 arg->op.right = right;
1912 /* do not free the token, it belongs to an op */
1913 *tok = NULL;
1914 type = process_arg(event, right, tok);
1916 } else if (strcmp(token, "?") == 0) {
1918 left = alloc_arg();
1919 if (!left)
1920 goto out_warn_free;
1922 /* copy the top arg to the left */
1923 *left = *arg;
1925 arg->type = PRINT_OP;
1926 arg->op.op = token;
1927 arg->op.left = left;
1928 arg->op.prio = 0;
1930 /* it will set arg->op.right */
1931 type = process_cond(event, arg, tok);
1933 } else if (strcmp(token, ">>") == 0 ||
1934 strcmp(token, "<<") == 0 ||
1935 strcmp(token, "&") == 0 ||
1936 strcmp(token, "|") == 0 ||
1937 strcmp(token, "&&") == 0 ||
1938 strcmp(token, "||") == 0 ||
1939 strcmp(token, "-") == 0 ||
1940 strcmp(token, "+") == 0 ||
1941 strcmp(token, "*") == 0 ||
1942 strcmp(token, "^") == 0 ||
1943 strcmp(token, "/") == 0 ||
1944 strcmp(token, "%") == 0 ||
1945 strcmp(token, "<") == 0 ||
1946 strcmp(token, ">") == 0 ||
1947 strcmp(token, "<=") == 0 ||
1948 strcmp(token, ">=") == 0 ||
1949 strcmp(token, "==") == 0 ||
1950 strcmp(token, "!=") == 0) {
1952 left = alloc_arg();
1953 if (!left)
1954 goto out_warn_free;
1956 /* copy the top arg to the left */
1957 *left = *arg;
1959 arg->type = PRINT_OP;
1960 arg->op.op = token;
1961 arg->op.left = left;
1962 arg->op.right = NULL;
1964 if (set_op_prio(arg) == -1) {
1965 event->flags |= EVENT_FL_FAILED;
1966 /* arg->op.op (= token) will be freed at out_free */
1967 arg->op.op = NULL;
1968 goto out_free;
1971 type = read_token_item(&token);
1972 *tok = token;
1974 /* could just be a type pointer */
1975 if ((strcmp(arg->op.op, "*") == 0) &&
1976 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1977 char *new_atom;
1979 if (left->type != PRINT_ATOM) {
1980 do_warning_event(event, "bad pointer type");
1981 goto out_free;
1983 new_atom = realloc(left->atom.atom,
1984 strlen(left->atom.atom) + 3);
1985 if (!new_atom)
1986 goto out_warn_free;
1988 left->atom.atom = new_atom;
1989 strcat(left->atom.atom, " *");
1990 free(arg->op.op);
1991 *arg = *left;
1992 free(left);
1994 return type;
1997 right = alloc_arg();
1998 if (!right)
1999 goto out_warn_free;
2001 type = process_arg_token(event, right, tok, type);
2002 if (type == EVENT_ERROR) {
2003 free_arg(right);
2004 /* token was freed in process_arg_token() via *tok */
2005 token = NULL;
2006 goto out_free;
2009 if (right->type == PRINT_OP &&
2010 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2011 struct print_arg tmp;
2013 /* rotate ops according to the priority */
2014 arg->op.right = right->op.left;
2016 tmp = *arg;
2017 *arg = *right;
2018 *right = tmp;
2020 arg->op.left = right;
2021 } else {
2022 arg->op.right = right;
2025 } else if (strcmp(token, "[") == 0) {
2027 left = alloc_arg();
2028 if (!left)
2029 goto out_warn_free;
2031 *left = *arg;
2033 arg->type = PRINT_OP;
2034 arg->op.op = token;
2035 arg->op.left = left;
2037 arg->op.prio = 0;
2039 /* it will set arg->op.right */
2040 type = process_array(event, arg, tok);
2042 } else {
2043 do_warning_event(event, "unknown op '%s'", token);
2044 event->flags |= EVENT_FL_FAILED;
2045 /* the arg is now the left side */
2046 goto out_free;
2049 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2050 int prio;
2052 /* higher prios need to be closer to the root */
2053 prio = get_op_prio(*tok);
2055 if (prio > arg->op.prio)
2056 return process_op(event, arg, tok);
2058 return process_op(event, right, tok);
2061 return type;
2063 out_warn_free:
2064 do_warning_event(event, "%s: not enough memory!", __func__);
2065 out_free:
2066 free_token(token);
2067 *tok = NULL;
2068 return EVENT_ERROR;
2071 static enum event_type
2072 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2073 char **tok)
2075 enum event_type type;
2076 char *field;
2077 char *token;
2079 if (read_expected(EVENT_OP, "->") < 0)
2080 goto out_err;
2082 if (read_expect_type(EVENT_ITEM, &token) < 0)
2083 goto out_free;
2084 field = token;
2086 arg->type = PRINT_FIELD;
2087 arg->field.name = field;
2089 if (is_flag_field) {
2090 arg->field.field = tep_find_any_field(event, arg->field.name);
2091 arg->field.field->flags |= FIELD_IS_FLAG;
2092 is_flag_field = 0;
2093 } else if (is_symbolic_field) {
2094 arg->field.field = tep_find_any_field(event, arg->field.name);
2095 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2096 is_symbolic_field = 0;
2099 type = read_token(&token);
2100 *tok = token;
2102 return type;
2104 out_free:
2105 free_token(token);
2106 out_err:
2107 *tok = NULL;
2108 return EVENT_ERROR;
2111 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2112 struct print_arg **print_arg)
2114 struct print_arg *field;
2115 enum event_type type;
2116 char *token;
2117 int ret = 0;
2119 field = alloc_arg();
2120 if (!field) {
2121 do_warning_event(event, "%s: not enough memory!", __func__);
2122 errno = ENOMEM;
2123 return -1;
2126 type = process_arg(event, field, &token);
2128 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2129 errno = EINVAL;
2130 ret = -1;
2131 free_arg(field);
2132 goto out_free_token;
2135 *print_arg = field;
2137 out_free_token:
2138 free_token(token);
2140 return ret;
2143 static char *arg_eval (struct print_arg *arg);
2145 static unsigned long long
2146 eval_type_str(unsigned long long val, const char *type, int pointer)
2148 int sign = 0;
2149 char *ref;
2150 int len;
2152 len = strlen(type);
2154 if (pointer) {
2156 if (type[len-1] != '*') {
2157 do_warning("pointer expected with non pointer type");
2158 return val;
2161 ref = malloc(len);
2162 if (!ref) {
2163 do_warning("%s: not enough memory!", __func__);
2164 return val;
2166 memcpy(ref, type, len);
2168 /* chop off the " *" */
2169 ref[len - 2] = 0;
2171 val = eval_type_str(val, ref, 0);
2172 free(ref);
2173 return val;
2176 /* check if this is a pointer */
2177 if (type[len - 1] == '*')
2178 return val;
2180 /* Try to figure out the arg size*/
2181 if (strncmp(type, "struct", 6) == 0)
2182 /* all bets off */
2183 return val;
2185 if (strcmp(type, "u8") == 0)
2186 return val & 0xff;
2188 if (strcmp(type, "u16") == 0)
2189 return val & 0xffff;
2191 if (strcmp(type, "u32") == 0)
2192 return val & 0xffffffff;
2194 if (strcmp(type, "u64") == 0 ||
2195 strcmp(type, "s64"))
2196 return val;
2198 if (strcmp(type, "s8") == 0)
2199 return (unsigned long long)(char)val & 0xff;
2201 if (strcmp(type, "s16") == 0)
2202 return (unsigned long long)(short)val & 0xffff;
2204 if (strcmp(type, "s32") == 0)
2205 return (unsigned long long)(int)val & 0xffffffff;
2207 if (strncmp(type, "unsigned ", 9) == 0) {
2208 sign = 0;
2209 type += 9;
2212 if (strcmp(type, "char") == 0) {
2213 if (sign)
2214 return (unsigned long long)(char)val & 0xff;
2215 else
2216 return val & 0xff;
2219 if (strcmp(type, "short") == 0) {
2220 if (sign)
2221 return (unsigned long long)(short)val & 0xffff;
2222 else
2223 return val & 0xffff;
2226 if (strcmp(type, "int") == 0) {
2227 if (sign)
2228 return (unsigned long long)(int)val & 0xffffffff;
2229 else
2230 return val & 0xffffffff;
2233 return val;
2237 * Try to figure out the type.
2239 static unsigned long long
2240 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2242 if (arg->type != PRINT_TYPE) {
2243 do_warning("expected type argument");
2244 return 0;
2247 return eval_type_str(val, arg->typecast.type, pointer);
2250 static int arg_num_eval(struct print_arg *arg, long long *val)
2252 long long left, right;
2253 int ret = 1;
2255 switch (arg->type) {
2256 case PRINT_ATOM:
2257 *val = strtoll(arg->atom.atom, NULL, 0);
2258 break;
2259 case PRINT_TYPE:
2260 ret = arg_num_eval(arg->typecast.item, val);
2261 if (!ret)
2262 break;
2263 *val = eval_type(*val, arg, 0);
2264 break;
2265 case PRINT_OP:
2266 switch (arg->op.op[0]) {
2267 case '|':
2268 ret = arg_num_eval(arg->op.left, &left);
2269 if (!ret)
2270 break;
2271 ret = arg_num_eval(arg->op.right, &right);
2272 if (!ret)
2273 break;
2274 if (arg->op.op[1])
2275 *val = left || right;
2276 else
2277 *val = left | right;
2278 break;
2279 case '&':
2280 ret = arg_num_eval(arg->op.left, &left);
2281 if (!ret)
2282 break;
2283 ret = arg_num_eval(arg->op.right, &right);
2284 if (!ret)
2285 break;
2286 if (arg->op.op[1])
2287 *val = left && right;
2288 else
2289 *val = left & right;
2290 break;
2291 case '<':
2292 ret = arg_num_eval(arg->op.left, &left);
2293 if (!ret)
2294 break;
2295 ret = arg_num_eval(arg->op.right, &right);
2296 if (!ret)
2297 break;
2298 switch (arg->op.op[1]) {
2299 case 0:
2300 *val = left < right;
2301 break;
2302 case '<':
2303 *val = left << right;
2304 break;
2305 case '=':
2306 *val = left <= right;
2307 break;
2308 default:
2309 do_warning("unknown op '%s'", arg->op.op);
2310 ret = 0;
2312 break;
2313 case '>':
2314 ret = arg_num_eval(arg->op.left, &left);
2315 if (!ret)
2316 break;
2317 ret = arg_num_eval(arg->op.right, &right);
2318 if (!ret)
2319 break;
2320 switch (arg->op.op[1]) {
2321 case 0:
2322 *val = left > right;
2323 break;
2324 case '>':
2325 *val = left >> right;
2326 break;
2327 case '=':
2328 *val = left >= right;
2329 break;
2330 default:
2331 do_warning("unknown op '%s'", arg->op.op);
2332 ret = 0;
2334 break;
2335 case '=':
2336 ret = arg_num_eval(arg->op.left, &left);
2337 if (!ret)
2338 break;
2339 ret = arg_num_eval(arg->op.right, &right);
2340 if (!ret)
2341 break;
2343 if (arg->op.op[1] != '=') {
2344 do_warning("unknown op '%s'", arg->op.op);
2345 ret = 0;
2346 } else
2347 *val = left == right;
2348 break;
2349 case '!':
2350 ret = arg_num_eval(arg->op.left, &left);
2351 if (!ret)
2352 break;
2353 ret = arg_num_eval(arg->op.right, &right);
2354 if (!ret)
2355 break;
2357 switch (arg->op.op[1]) {
2358 case '=':
2359 *val = left != right;
2360 break;
2361 default:
2362 do_warning("unknown op '%s'", arg->op.op);
2363 ret = 0;
2365 break;
2366 case '-':
2367 /* check for negative */
2368 if (arg->op.left->type == PRINT_NULL)
2369 left = 0;
2370 else
2371 ret = arg_num_eval(arg->op.left, &left);
2372 if (!ret)
2373 break;
2374 ret = arg_num_eval(arg->op.right, &right);
2375 if (!ret)
2376 break;
2377 *val = left - right;
2378 break;
2379 case '+':
2380 if (arg->op.left->type == PRINT_NULL)
2381 left = 0;
2382 else
2383 ret = arg_num_eval(arg->op.left, &left);
2384 if (!ret)
2385 break;
2386 ret = arg_num_eval(arg->op.right, &right);
2387 if (!ret)
2388 break;
2389 *val = left + right;
2390 break;
2391 case '~':
2392 ret = arg_num_eval(arg->op.right, &right);
2393 if (!ret)
2394 break;
2395 *val = ~right;
2396 break;
2397 default:
2398 do_warning("unknown op '%s'", arg->op.op);
2399 ret = 0;
2401 break;
2403 case PRINT_NULL:
2404 case PRINT_FIELD ... PRINT_SYMBOL:
2405 case PRINT_STRING:
2406 case PRINT_BSTRING:
2407 case PRINT_BITMASK:
2408 default:
2409 do_warning("invalid eval type %d", arg->type);
2410 ret = 0;
2413 return ret;
2416 static char *arg_eval (struct print_arg *arg)
2418 long long val;
2419 static char buf[20];
2421 switch (arg->type) {
2422 case PRINT_ATOM:
2423 return arg->atom.atom;
2424 case PRINT_TYPE:
2425 return arg_eval(arg->typecast.item);
2426 case PRINT_OP:
2427 if (!arg_num_eval(arg, &val))
2428 break;
2429 sprintf(buf, "%lld", val);
2430 return buf;
2432 case PRINT_NULL:
2433 case PRINT_FIELD ... PRINT_SYMBOL:
2434 case PRINT_STRING:
2435 case PRINT_BSTRING:
2436 case PRINT_BITMASK:
2437 default:
2438 do_warning("invalid eval type %d", arg->type);
2439 break;
2442 return NULL;
2445 static enum event_type
2446 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2448 enum event_type type;
2449 struct print_arg *arg = NULL;
2450 struct print_flag_sym *field;
2451 char *token = *tok;
2452 char *value;
2454 do {
2455 free_token(token);
2456 type = read_token_item(&token);
2457 if (test_type_token(type, token, EVENT_OP, "{"))
2458 break;
2460 arg = alloc_arg();
2461 if (!arg)
2462 goto out_free;
2464 free_token(token);
2465 type = process_arg(event, arg, &token);
2467 if (type == EVENT_OP)
2468 type = process_op(event, arg, &token);
2470 if (type == EVENT_ERROR)
2471 goto out_free;
2473 if (test_type_token(type, token, EVENT_DELIM, ","))
2474 goto out_free;
2476 field = calloc(1, sizeof(*field));
2477 if (!field)
2478 goto out_free;
2480 value = arg_eval(arg);
2481 if (value == NULL)
2482 goto out_free_field;
2483 field->value = strdup(value);
2484 if (field->value == NULL)
2485 goto out_free_field;
2487 free_arg(arg);
2488 arg = alloc_arg();
2489 if (!arg)
2490 goto out_free;
2492 free_token(token);
2493 type = process_arg(event, arg, &token);
2494 if (test_type_token(type, token, EVENT_OP, "}"))
2495 goto out_free_field;
2497 value = arg_eval(arg);
2498 if (value == NULL)
2499 goto out_free_field;
2500 field->str = strdup(value);
2501 if (field->str == NULL)
2502 goto out_free_field;
2503 free_arg(arg);
2504 arg = NULL;
2506 *list = field;
2507 list = &field->next;
2509 free_token(token);
2510 type = read_token_item(&token);
2511 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2513 *tok = token;
2514 return type;
2516 out_free_field:
2517 free_flag_sym(field);
2518 out_free:
2519 free_arg(arg);
2520 free_token(token);
2521 *tok = NULL;
2523 return EVENT_ERROR;
2526 static enum event_type
2527 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2529 struct print_arg *field;
2530 enum event_type type;
2531 char *token = NULL;
2533 memset(arg, 0, sizeof(*arg));
2534 arg->type = PRINT_FLAGS;
2536 field = alloc_arg();
2537 if (!field) {
2538 do_warning_event(event, "%s: not enough memory!", __func__);
2539 goto out_free;
2542 type = process_field_arg(event, field, &token);
2544 /* Handle operations in the first argument */
2545 while (type == EVENT_OP)
2546 type = process_op(event, field, &token);
2548 if (test_type_token(type, token, EVENT_DELIM, ","))
2549 goto out_free_field;
2550 free_token(token);
2552 arg->flags.field = field;
2554 type = read_token_item(&token);
2555 if (event_item_type(type)) {
2556 arg->flags.delim = token;
2557 type = read_token_item(&token);
2560 if (test_type_token(type, token, EVENT_DELIM, ","))
2561 goto out_free;
2563 type = process_fields(event, &arg->flags.flags, &token);
2564 if (test_type_token(type, token, EVENT_DELIM, ")"))
2565 goto out_free;
2567 free_token(token);
2568 type = read_token_item(tok);
2569 return type;
2571 out_free_field:
2572 free_arg(field);
2573 out_free:
2574 free_token(token);
2575 *tok = NULL;
2576 return EVENT_ERROR;
2579 static enum event_type
2580 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2582 struct print_arg *field;
2583 enum event_type type;
2584 char *token = NULL;
2586 memset(arg, 0, sizeof(*arg));
2587 arg->type = PRINT_SYMBOL;
2589 field = alloc_arg();
2590 if (!field) {
2591 do_warning_event(event, "%s: not enough memory!", __func__);
2592 goto out_free;
2595 type = process_field_arg(event, field, &token);
2597 if (test_type_token(type, token, EVENT_DELIM, ","))
2598 goto out_free_field;
2600 arg->symbol.field = field;
2602 type = process_fields(event, &arg->symbol.symbols, &token);
2603 if (test_type_token(type, token, EVENT_DELIM, ")"))
2604 goto out_free;
2606 free_token(token);
2607 type = read_token_item(tok);
2608 return type;
2610 out_free_field:
2611 free_arg(field);
2612 out_free:
2613 free_token(token);
2614 *tok = NULL;
2615 return EVENT_ERROR;
2618 static enum event_type
2619 process_hex_common(struct event_format *event, struct print_arg *arg,
2620 char **tok, enum print_arg_type type)
2622 memset(arg, 0, sizeof(*arg));
2623 arg->type = type;
2625 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2626 goto out;
2628 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2629 goto free_field;
2631 return read_token_item(tok);
2633 free_field:
2634 free_arg(arg->hex.field);
2635 arg->hex.field = NULL;
2636 out:
2637 *tok = NULL;
2638 return EVENT_ERROR;
2641 static enum event_type
2642 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2644 return process_hex_common(event, arg, tok, PRINT_HEX);
2647 static enum event_type
2648 process_hex_str(struct event_format *event, struct print_arg *arg,
2649 char **tok)
2651 return process_hex_common(event, arg, tok, PRINT_HEX_STR);
2654 static enum event_type
2655 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2657 memset(arg, 0, sizeof(*arg));
2658 arg->type = PRINT_INT_ARRAY;
2660 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2661 goto out;
2663 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2664 goto free_field;
2666 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2667 goto free_size;
2669 return read_token_item(tok);
2671 free_size:
2672 free_arg(arg->int_array.count);
2673 arg->int_array.count = NULL;
2674 free_field:
2675 free_arg(arg->int_array.field);
2676 arg->int_array.field = NULL;
2677 out:
2678 *tok = NULL;
2679 return EVENT_ERROR;
2682 static enum event_type
2683 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2685 struct format_field *field;
2686 enum event_type type;
2687 char *token;
2689 memset(arg, 0, sizeof(*arg));
2690 arg->type = PRINT_DYNAMIC_ARRAY;
2693 * The item within the parenthesis is another field that holds
2694 * the index into where the array starts.
2696 type = read_token(&token);
2697 *tok = token;
2698 if (type != EVENT_ITEM)
2699 goto out_free;
2701 /* Find the field */
2703 field = tep_find_field(event, token);
2704 if (!field)
2705 goto out_free;
2707 arg->dynarray.field = field;
2708 arg->dynarray.index = 0;
2710 if (read_expected(EVENT_DELIM, ")") < 0)
2711 goto out_free;
2713 free_token(token);
2714 type = read_token_item(&token);
2715 *tok = token;
2716 if (type != EVENT_OP || strcmp(token, "[") != 0)
2717 return type;
2719 free_token(token);
2720 arg = alloc_arg();
2721 if (!arg) {
2722 do_warning_event(event, "%s: not enough memory!", __func__);
2723 *tok = NULL;
2724 return EVENT_ERROR;
2727 type = process_arg(event, arg, &token);
2728 if (type == EVENT_ERROR)
2729 goto out_free_arg;
2731 if (!test_type_token(type, token, EVENT_OP, "]"))
2732 goto out_free_arg;
2734 free_token(token);
2735 type = read_token_item(tok);
2736 return type;
2738 out_free_arg:
2739 free_arg(arg);
2740 out_free:
2741 free_token(token);
2742 *tok = NULL;
2743 return EVENT_ERROR;
2746 static enum event_type
2747 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2748 char **tok)
2750 struct format_field *field;
2751 enum event_type type;
2752 char *token;
2754 if (read_expect_type(EVENT_ITEM, &token) < 0)
2755 goto out_free;
2757 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2759 /* Find the field */
2760 field = tep_find_field(event, token);
2761 if (!field)
2762 goto out_free;
2764 arg->dynarray.field = field;
2765 arg->dynarray.index = 0;
2767 if (read_expected(EVENT_DELIM, ")") < 0)
2768 goto out_err;
2770 type = read_token(&token);
2771 *tok = token;
2773 return type;
2775 out_free:
2776 free_token(token);
2777 out_err:
2778 *tok = NULL;
2779 return EVENT_ERROR;
2782 static enum event_type
2783 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2785 struct print_arg *item_arg;
2786 enum event_type type;
2787 char *token;
2789 type = process_arg(event, arg, &token);
2791 if (type == EVENT_ERROR)
2792 goto out_free;
2794 if (type == EVENT_OP)
2795 type = process_op(event, arg, &token);
2797 if (type == EVENT_ERROR)
2798 goto out_free;
2800 if (test_type_token(type, token, EVENT_DELIM, ")"))
2801 goto out_free;
2803 free_token(token);
2804 type = read_token_item(&token);
2807 * If the next token is an item or another open paren, then
2808 * this was a typecast.
2810 if (event_item_type(type) ||
2811 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2813 /* make this a typecast and contine */
2815 /* prevous must be an atom */
2816 if (arg->type != PRINT_ATOM) {
2817 do_warning_event(event, "previous needed to be PRINT_ATOM");
2818 goto out_free;
2821 item_arg = alloc_arg();
2822 if (!item_arg) {
2823 do_warning_event(event, "%s: not enough memory!",
2824 __func__);
2825 goto out_free;
2828 arg->type = PRINT_TYPE;
2829 arg->typecast.type = arg->atom.atom;
2830 arg->typecast.item = item_arg;
2831 type = process_arg_token(event, item_arg, &token, type);
2835 *tok = token;
2836 return type;
2838 out_free:
2839 free_token(token);
2840 *tok = NULL;
2841 return EVENT_ERROR;
2845 static enum event_type
2846 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2847 char **tok)
2849 enum event_type type;
2850 char *token;
2852 if (read_expect_type(EVENT_ITEM, &token) < 0)
2853 goto out_free;
2855 arg->type = PRINT_STRING;
2856 arg->string.string = token;
2857 arg->string.offset = -1;
2859 if (read_expected(EVENT_DELIM, ")") < 0)
2860 goto out_err;
2862 type = read_token(&token);
2863 *tok = token;
2865 return type;
2867 out_free:
2868 free_token(token);
2869 out_err:
2870 *tok = NULL;
2871 return EVENT_ERROR;
2874 static enum event_type
2875 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2876 char **tok)
2878 enum event_type type;
2879 char *token;
2881 if (read_expect_type(EVENT_ITEM, &token) < 0)
2882 goto out_free;
2884 arg->type = PRINT_BITMASK;
2885 arg->bitmask.bitmask = token;
2886 arg->bitmask.offset = -1;
2888 if (read_expected(EVENT_DELIM, ")") < 0)
2889 goto out_err;
2891 type = read_token(&token);
2892 *tok = token;
2894 return type;
2896 out_free:
2897 free_token(token);
2898 out_err:
2899 *tok = NULL;
2900 return EVENT_ERROR;
2903 static struct tep_function_handler *
2904 find_func_handler(struct tep_handle *pevent, char *func_name)
2906 struct tep_function_handler *func;
2908 if (!pevent)
2909 return NULL;
2911 for (func = pevent->func_handlers; func; func = func->next) {
2912 if (strcmp(func->name, func_name) == 0)
2913 break;
2916 return func;
2919 static void remove_func_handler(struct tep_handle *pevent, char *func_name)
2921 struct tep_function_handler *func;
2922 struct tep_function_handler **next;
2924 next = &pevent->func_handlers;
2925 while ((func = *next)) {
2926 if (strcmp(func->name, func_name) == 0) {
2927 *next = func->next;
2928 free_func_handle(func);
2929 break;
2931 next = &func->next;
2935 static enum event_type
2936 process_func_handler(struct event_format *event, struct tep_function_handler *func,
2937 struct print_arg *arg, char **tok)
2939 struct print_arg **next_arg;
2940 struct print_arg *farg;
2941 enum event_type type;
2942 char *token;
2943 int i;
2945 arg->type = PRINT_FUNC;
2946 arg->func.func = func;
2948 *tok = NULL;
2950 next_arg = &(arg->func.args);
2951 for (i = 0; i < func->nr_args; i++) {
2952 farg = alloc_arg();
2953 if (!farg) {
2954 do_warning_event(event, "%s: not enough memory!",
2955 __func__);
2956 return EVENT_ERROR;
2959 type = process_arg(event, farg, &token);
2960 if (i < (func->nr_args - 1)) {
2961 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2962 do_warning_event(event,
2963 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2964 func->name, func->nr_args,
2965 event->name, i + 1);
2966 goto err;
2968 } else {
2969 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2970 do_warning_event(event,
2971 "Error: function '%s()' only expects %d arguments but event %s has more",
2972 func->name, func->nr_args, event->name);
2973 goto err;
2977 *next_arg = farg;
2978 next_arg = &(farg->next);
2979 free_token(token);
2982 type = read_token(&token);
2983 *tok = token;
2985 return type;
2987 err:
2988 free_arg(farg);
2989 free_token(token);
2990 return EVENT_ERROR;
2993 static enum event_type
2994 process_function(struct event_format *event, struct print_arg *arg,
2995 char *token, char **tok)
2997 struct tep_function_handler *func;
2999 if (strcmp(token, "__print_flags") == 0) {
3000 free_token(token);
3001 is_flag_field = 1;
3002 return process_flags(event, arg, tok);
3004 if (strcmp(token, "__print_symbolic") == 0) {
3005 free_token(token);
3006 is_symbolic_field = 1;
3007 return process_symbols(event, arg, tok);
3009 if (strcmp(token, "__print_hex") == 0) {
3010 free_token(token);
3011 return process_hex(event, arg, tok);
3013 if (strcmp(token, "__print_hex_str") == 0) {
3014 free_token(token);
3015 return process_hex_str(event, arg, tok);
3017 if (strcmp(token, "__print_array") == 0) {
3018 free_token(token);
3019 return process_int_array(event, arg, tok);
3021 if (strcmp(token, "__get_str") == 0) {
3022 free_token(token);
3023 return process_str(event, arg, tok);
3025 if (strcmp(token, "__get_bitmask") == 0) {
3026 free_token(token);
3027 return process_bitmask(event, arg, tok);
3029 if (strcmp(token, "__get_dynamic_array") == 0) {
3030 free_token(token);
3031 return process_dynamic_array(event, arg, tok);
3033 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3034 free_token(token);
3035 return process_dynamic_array_len(event, arg, tok);
3038 func = find_func_handler(event->pevent, token);
3039 if (func) {
3040 free_token(token);
3041 return process_func_handler(event, func, arg, tok);
3044 do_warning_event(event, "function %s not defined", token);
3045 free_token(token);
3046 return EVENT_ERROR;
3049 static enum event_type
3050 process_arg_token(struct event_format *event, struct print_arg *arg,
3051 char **tok, enum event_type type)
3053 char *token;
3054 char *atom;
3056 token = *tok;
3058 switch (type) {
3059 case EVENT_ITEM:
3060 if (strcmp(token, "REC") == 0) {
3061 free_token(token);
3062 type = process_entry(event, arg, &token);
3063 break;
3065 atom = token;
3066 /* test the next token */
3067 type = read_token_item(&token);
3070 * If the next token is a parenthesis, then this
3071 * is a function.
3073 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3074 free_token(token);
3075 token = NULL;
3076 /* this will free atom. */
3077 type = process_function(event, arg, atom, &token);
3078 break;
3080 /* atoms can be more than one token long */
3081 while (type == EVENT_ITEM) {
3082 char *new_atom;
3083 new_atom = realloc(atom,
3084 strlen(atom) + strlen(token) + 2);
3085 if (!new_atom) {
3086 free(atom);
3087 *tok = NULL;
3088 free_token(token);
3089 return EVENT_ERROR;
3091 atom = new_atom;
3092 strcat(atom, " ");
3093 strcat(atom, token);
3094 free_token(token);
3095 type = read_token_item(&token);
3098 arg->type = PRINT_ATOM;
3099 arg->atom.atom = atom;
3100 break;
3102 case EVENT_DQUOTE:
3103 case EVENT_SQUOTE:
3104 arg->type = PRINT_ATOM;
3105 arg->atom.atom = token;
3106 type = read_token_item(&token);
3107 break;
3108 case EVENT_DELIM:
3109 if (strcmp(token, "(") == 0) {
3110 free_token(token);
3111 type = process_paren(event, arg, &token);
3112 break;
3114 case EVENT_OP:
3115 /* handle single ops */
3116 arg->type = PRINT_OP;
3117 arg->op.op = token;
3118 arg->op.left = NULL;
3119 type = process_op(event, arg, &token);
3121 /* On error, the op is freed */
3122 if (type == EVENT_ERROR)
3123 arg->op.op = NULL;
3125 /* return error type if errored */
3126 break;
3128 case EVENT_ERROR ... EVENT_NEWLINE:
3129 default:
3130 do_warning_event(event, "unexpected type %d", type);
3131 return EVENT_ERROR;
3133 *tok = token;
3135 return type;
3138 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3140 enum event_type type = EVENT_ERROR;
3141 struct print_arg *arg;
3142 char *token;
3143 int args = 0;
3145 do {
3146 if (type == EVENT_NEWLINE) {
3147 type = read_token_item(&token);
3148 continue;
3151 arg = alloc_arg();
3152 if (!arg) {
3153 do_warning_event(event, "%s: not enough memory!",
3154 __func__);
3155 return -1;
3158 type = process_arg(event, arg, &token);
3160 if (type == EVENT_ERROR) {
3161 free_token(token);
3162 free_arg(arg);
3163 return -1;
3166 *list = arg;
3167 args++;
3169 if (type == EVENT_OP) {
3170 type = process_op(event, arg, &token);
3171 free_token(token);
3172 if (type == EVENT_ERROR) {
3173 *list = NULL;
3174 free_arg(arg);
3175 return -1;
3177 list = &arg->next;
3178 continue;
3181 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3182 free_token(token);
3183 *list = arg;
3184 list = &arg->next;
3185 continue;
3187 break;
3188 } while (type != EVENT_NONE);
3190 if (type != EVENT_NONE && type != EVENT_ERROR)
3191 free_token(token);
3193 return args;
3196 static int event_read_print(struct event_format *event)
3198 enum event_type type;
3199 char *token;
3200 int ret;
3202 if (read_expected_item(EVENT_ITEM, "print") < 0)
3203 return -1;
3205 if (read_expected(EVENT_ITEM, "fmt") < 0)
3206 return -1;
3208 if (read_expected(EVENT_OP, ":") < 0)
3209 return -1;
3211 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3212 goto fail;
3214 concat:
3215 event->print_fmt.format = token;
3216 event->print_fmt.args = NULL;
3218 /* ok to have no arg */
3219 type = read_token_item(&token);
3221 if (type == EVENT_NONE)
3222 return 0;
3224 /* Handle concatenation of print lines */
3225 if (type == EVENT_DQUOTE) {
3226 char *cat;
3228 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3229 goto fail;
3230 free_token(token);
3231 free_token(event->print_fmt.format);
3232 event->print_fmt.format = NULL;
3233 token = cat;
3234 goto concat;
3237 if (test_type_token(type, token, EVENT_DELIM, ","))
3238 goto fail;
3240 free_token(token);
3242 ret = event_read_print_args(event, &event->print_fmt.args);
3243 if (ret < 0)
3244 return -1;
3246 return ret;
3248 fail:
3249 free_token(token);
3250 return -1;
3254 * tep_find_common_field - return a common field by event
3255 * @event: handle for the event
3256 * @name: the name of the common field to return
3258 * Returns a common field from the event by the given @name.
3259 * This only searchs the common fields and not all field.
3261 struct format_field *
3262 tep_find_common_field(struct event_format *event, const char *name)
3264 struct format_field *format;
3266 for (format = event->format.common_fields;
3267 format; format = format->next) {
3268 if (strcmp(format->name, name) == 0)
3269 break;
3272 return format;
3276 * tep_find_field - find a non-common field
3277 * @event: handle for the event
3278 * @name: the name of the non-common field
3280 * Returns a non-common field by the given @name.
3281 * This does not search common fields.
3283 struct format_field *
3284 tep_find_field(struct event_format *event, const char *name)
3286 struct format_field *format;
3288 for (format = event->format.fields;
3289 format; format = format->next) {
3290 if (strcmp(format->name, name) == 0)
3291 break;
3294 return format;
3298 * tep_find_any_field - find any field by name
3299 * @event: handle for the event
3300 * @name: the name of the field
3302 * Returns a field by the given @name.
3303 * This searchs the common field names first, then
3304 * the non-common ones if a common one was not found.
3306 struct format_field *
3307 tep_find_any_field(struct event_format *event, const char *name)
3309 struct format_field *format;
3311 format = tep_find_common_field(event, name);
3312 if (format)
3313 return format;
3314 return tep_find_field(event, name);
3318 * tep_read_number - read a number from data
3319 * @pevent: handle for the pevent
3320 * @ptr: the raw data
3321 * @size: the size of the data that holds the number
3323 * Returns the number (converted to host) from the
3324 * raw data.
3326 unsigned long long tep_read_number(struct tep_handle *pevent,
3327 const void *ptr, int size)
3329 switch (size) {
3330 case 1:
3331 return *(unsigned char *)ptr;
3332 case 2:
3333 return data2host2(pevent, ptr);
3334 case 4:
3335 return data2host4(pevent, ptr);
3336 case 8:
3337 return data2host8(pevent, ptr);
3338 default:
3339 /* BUG! */
3340 return 0;
3345 * tep_read_number_field - read a number from data
3346 * @field: a handle to the field
3347 * @data: the raw data to read
3348 * @value: the value to place the number in
3350 * Reads raw data according to a field offset and size,
3351 * and translates it into @value.
3353 * Returns 0 on success, -1 otherwise.
3355 int tep_read_number_field(struct format_field *field, const void *data,
3356 unsigned long long *value)
3358 if (!field)
3359 return -1;
3360 switch (field->size) {
3361 case 1:
3362 case 2:
3363 case 4:
3364 case 8:
3365 *value = tep_read_number(field->event->pevent,
3366 data + field->offset, field->size);
3367 return 0;
3368 default:
3369 return -1;
3373 static int get_common_info(struct tep_handle *pevent,
3374 const char *type, int *offset, int *size)
3376 struct event_format *event;
3377 struct format_field *field;
3380 * All events should have the same common elements.
3381 * Pick any event to find where the type is;
3383 if (!pevent->events) {
3384 do_warning("no event_list!");
3385 return -1;
3388 event = pevent->events[0];
3389 field = tep_find_common_field(event, type);
3390 if (!field)
3391 return -1;
3393 *offset = field->offset;
3394 *size = field->size;
3396 return 0;
3399 static int __parse_common(struct tep_handle *pevent, void *data,
3400 int *size, int *offset, const char *name)
3402 int ret;
3404 if (!*size) {
3405 ret = get_common_info(pevent, name, offset, size);
3406 if (ret < 0)
3407 return ret;
3409 return tep_read_number(pevent, data + *offset, *size);
3412 static int trace_parse_common_type(struct tep_handle *pevent, void *data)
3414 return __parse_common(pevent, data,
3415 &pevent->type_size, &pevent->type_offset,
3416 "common_type");
3419 static int parse_common_pid(struct tep_handle *pevent, void *data)
3421 return __parse_common(pevent, data,
3422 &pevent->pid_size, &pevent->pid_offset,
3423 "common_pid");
3426 static int parse_common_pc(struct tep_handle *pevent, void *data)
3428 return __parse_common(pevent, data,
3429 &pevent->pc_size, &pevent->pc_offset,
3430 "common_preempt_count");
3433 static int parse_common_flags(struct tep_handle *pevent, void *data)
3435 return __parse_common(pevent, data,
3436 &pevent->flags_size, &pevent->flags_offset,
3437 "common_flags");
3440 static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
3442 return __parse_common(pevent, data,
3443 &pevent->ld_size, &pevent->ld_offset,
3444 "common_lock_depth");
3447 static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
3449 return __parse_common(pevent, data,
3450 &pevent->ld_size, &pevent->ld_offset,
3451 "common_migrate_disable");
3454 static int events_id_cmp(const void *a, const void *b);
3457 * tep_find_event - find an event by given id
3458 * @pevent: a handle to the pevent
3459 * @id: the id of the event
3461 * Returns an event that has a given @id.
3463 struct event_format *tep_find_event(struct tep_handle *pevent, int id)
3465 struct event_format **eventptr;
3466 struct event_format key;
3467 struct event_format *pkey = &key;
3469 /* Check cache first */
3470 if (pevent->last_event && pevent->last_event->id == id)
3471 return pevent->last_event;
3473 key.id = id;
3475 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3476 sizeof(*pevent->events), events_id_cmp);
3478 if (eventptr) {
3479 pevent->last_event = *eventptr;
3480 return *eventptr;
3483 return NULL;
3487 * tep_find_event_by_name - find an event by given name
3488 * @pevent: a handle to the pevent
3489 * @sys: the system name to search for
3490 * @name: the name of the event to search for
3492 * This returns an event with a given @name and under the system
3493 * @sys. If @sys is NULL the first event with @name is returned.
3495 struct event_format *
3496 tep_find_event_by_name(struct tep_handle *pevent,
3497 const char *sys, const char *name)
3499 struct event_format *event;
3500 int i;
3502 if (pevent->last_event &&
3503 strcmp(pevent->last_event->name, name) == 0 &&
3504 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3505 return pevent->last_event;
3507 for (i = 0; i < pevent->nr_events; i++) {
3508 event = pevent->events[i];
3509 if (strcmp(event->name, name) == 0) {
3510 if (!sys)
3511 break;
3512 if (strcmp(event->system, sys) == 0)
3513 break;
3516 if (i == pevent->nr_events)
3517 event = NULL;
3519 pevent->last_event = event;
3520 return event;
3523 static unsigned long long
3524 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3526 struct tep_handle *pevent = event->pevent;
3527 unsigned long long val = 0;
3528 unsigned long long left, right;
3529 struct print_arg *typearg = NULL;
3530 struct print_arg *larg;
3531 unsigned long offset;
3532 unsigned int field_size;
3534 switch (arg->type) {
3535 case PRINT_NULL:
3536 /* ?? */
3537 return 0;
3538 case PRINT_ATOM:
3539 return strtoull(arg->atom.atom, NULL, 0);
3540 case PRINT_FIELD:
3541 if (!arg->field.field) {
3542 arg->field.field = tep_find_any_field(event, arg->field.name);
3543 if (!arg->field.field)
3544 goto out_warning_field;
3547 /* must be a number */
3548 val = tep_read_number(pevent, data + arg->field.field->offset,
3549 arg->field.field->size);
3550 break;
3551 case PRINT_FLAGS:
3552 case PRINT_SYMBOL:
3553 case PRINT_INT_ARRAY:
3554 case PRINT_HEX:
3555 case PRINT_HEX_STR:
3556 break;
3557 case PRINT_TYPE:
3558 val = eval_num_arg(data, size, event, arg->typecast.item);
3559 return eval_type(val, arg, 0);
3560 case PRINT_STRING:
3561 case PRINT_BSTRING:
3562 case PRINT_BITMASK:
3563 return 0;
3564 case PRINT_FUNC: {
3565 struct trace_seq s;
3566 trace_seq_init(&s);
3567 val = process_defined_func(&s, data, size, event, arg);
3568 trace_seq_destroy(&s);
3569 return val;
3571 case PRINT_OP:
3572 if (strcmp(arg->op.op, "[") == 0) {
3574 * Arrays are special, since we don't want
3575 * to read the arg as is.
3577 right = eval_num_arg(data, size, event, arg->op.right);
3579 /* handle typecasts */
3580 larg = arg->op.left;
3581 while (larg->type == PRINT_TYPE) {
3582 if (!typearg)
3583 typearg = larg;
3584 larg = larg->typecast.item;
3587 /* Default to long size */
3588 field_size = pevent->long_size;
3590 switch (larg->type) {
3591 case PRINT_DYNAMIC_ARRAY:
3592 offset = tep_read_number(pevent,
3593 data + larg->dynarray.field->offset,
3594 larg->dynarray.field->size);
3595 if (larg->dynarray.field->elementsize)
3596 field_size = larg->dynarray.field->elementsize;
3598 * The actual length of the dynamic array is stored
3599 * in the top half of the field, and the offset
3600 * is in the bottom half of the 32 bit field.
3602 offset &= 0xffff;
3603 offset += right;
3604 break;
3605 case PRINT_FIELD:
3606 if (!larg->field.field) {
3607 larg->field.field =
3608 tep_find_any_field(event, larg->field.name);
3609 if (!larg->field.field) {
3610 arg = larg;
3611 goto out_warning_field;
3614 field_size = larg->field.field->elementsize;
3615 offset = larg->field.field->offset +
3616 right * larg->field.field->elementsize;
3617 break;
3618 default:
3619 goto default_op; /* oops, all bets off */
3621 val = tep_read_number(pevent,
3622 data + offset, field_size);
3623 if (typearg)
3624 val = eval_type(val, typearg, 1);
3625 break;
3626 } else if (strcmp(arg->op.op, "?") == 0) {
3627 left = eval_num_arg(data, size, event, arg->op.left);
3628 arg = arg->op.right;
3629 if (left)
3630 val = eval_num_arg(data, size, event, arg->op.left);
3631 else
3632 val = eval_num_arg(data, size, event, arg->op.right);
3633 break;
3635 default_op:
3636 left = eval_num_arg(data, size, event, arg->op.left);
3637 right = eval_num_arg(data, size, event, arg->op.right);
3638 switch (arg->op.op[0]) {
3639 case '!':
3640 switch (arg->op.op[1]) {
3641 case 0:
3642 val = !right;
3643 break;
3644 case '=':
3645 val = left != right;
3646 break;
3647 default:
3648 goto out_warning_op;
3650 break;
3651 case '~':
3652 val = ~right;
3653 break;
3654 case '|':
3655 if (arg->op.op[1])
3656 val = left || right;
3657 else
3658 val = left | right;
3659 break;
3660 case '&':
3661 if (arg->op.op[1])
3662 val = left && right;
3663 else
3664 val = left & right;
3665 break;
3666 case '<':
3667 switch (arg->op.op[1]) {
3668 case 0:
3669 val = left < right;
3670 break;
3671 case '<':
3672 val = left << right;
3673 break;
3674 case '=':
3675 val = left <= right;
3676 break;
3677 default:
3678 goto out_warning_op;
3680 break;
3681 case '>':
3682 switch (arg->op.op[1]) {
3683 case 0:
3684 val = left > right;
3685 break;
3686 case '>':
3687 val = left >> right;
3688 break;
3689 case '=':
3690 val = left >= right;
3691 break;
3692 default:
3693 goto out_warning_op;
3695 break;
3696 case '=':
3697 if (arg->op.op[1] != '=')
3698 goto out_warning_op;
3700 val = left == right;
3701 break;
3702 case '-':
3703 val = left - right;
3704 break;
3705 case '+':
3706 val = left + right;
3707 break;
3708 case '/':
3709 val = left / right;
3710 break;
3711 case '%':
3712 val = left % right;
3713 break;
3714 case '*':
3715 val = left * right;
3716 break;
3717 default:
3718 goto out_warning_op;
3720 break;
3721 case PRINT_DYNAMIC_ARRAY_LEN:
3722 offset = tep_read_number(pevent,
3723 data + arg->dynarray.field->offset,
3724 arg->dynarray.field->size);
3726 * The total allocated length of the dynamic array is
3727 * stored in the top half of the field, and the offset
3728 * is in the bottom half of the 32 bit field.
3730 val = (unsigned long long)(offset >> 16);
3731 break;
3732 case PRINT_DYNAMIC_ARRAY:
3733 /* Without [], we pass the address to the dynamic data */
3734 offset = tep_read_number(pevent,
3735 data + arg->dynarray.field->offset,
3736 arg->dynarray.field->size);
3738 * The total allocated length of the dynamic array is
3739 * stored in the top half of the field, and the offset
3740 * is in the bottom half of the 32 bit field.
3742 offset &= 0xffff;
3743 val = (unsigned long long)((unsigned long)data + offset);
3744 break;
3745 default: /* not sure what to do there */
3746 return 0;
3748 return val;
3750 out_warning_op:
3751 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3752 return 0;
3754 out_warning_field:
3755 do_warning_event(event, "%s: field %s not found",
3756 __func__, arg->field.name);
3757 return 0;
3760 struct flag {
3761 const char *name;
3762 unsigned long long value;
3765 static const struct flag flags[] = {
3766 { "HI_SOFTIRQ", 0 },
3767 { "TIMER_SOFTIRQ", 1 },
3768 { "NET_TX_SOFTIRQ", 2 },
3769 { "NET_RX_SOFTIRQ", 3 },
3770 { "BLOCK_SOFTIRQ", 4 },
3771 { "IRQ_POLL_SOFTIRQ", 5 },
3772 { "TASKLET_SOFTIRQ", 6 },
3773 { "SCHED_SOFTIRQ", 7 },
3774 { "HRTIMER_SOFTIRQ", 8 },
3775 { "RCU_SOFTIRQ", 9 },
3777 { "HRTIMER_NORESTART", 0 },
3778 { "HRTIMER_RESTART", 1 },
3781 static long long eval_flag(const char *flag)
3783 int i;
3786 * Some flags in the format files do not get converted.
3787 * If the flag is not numeric, see if it is something that
3788 * we already know about.
3790 if (isdigit(flag[0]))
3791 return strtoull(flag, NULL, 0);
3793 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3794 if (strcmp(flags[i].name, flag) == 0)
3795 return flags[i].value;
3797 return -1LL;
3800 static void print_str_to_seq(struct trace_seq *s, const char *format,
3801 int len_arg, const char *str)
3803 if (len_arg >= 0)
3804 trace_seq_printf(s, format, len_arg, str);
3805 else
3806 trace_seq_printf(s, format, str);
3809 static void print_bitmask_to_seq(struct tep_handle *pevent,
3810 struct trace_seq *s, const char *format,
3811 int len_arg, const void *data, int size)
3813 int nr_bits = size * 8;
3814 int str_size = (nr_bits + 3) / 4;
3815 int len = 0;
3816 char buf[3];
3817 char *str;
3818 int index;
3819 int i;
3822 * The kernel likes to put in commas every 32 bits, we
3823 * can do the same.
3825 str_size += (nr_bits - 1) / 32;
3827 str = malloc(str_size + 1);
3828 if (!str) {
3829 do_warning("%s: not enough memory!", __func__);
3830 return;
3832 str[str_size] = 0;
3834 /* Start out with -2 for the two chars per byte */
3835 for (i = str_size - 2; i >= 0; i -= 2) {
3837 * data points to a bit mask of size bytes.
3838 * In the kernel, this is an array of long words, thus
3839 * endianess is very important.
3841 if (pevent->file_bigendian)
3842 index = size - (len + 1);
3843 else
3844 index = len;
3846 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3847 memcpy(str + i, buf, 2);
3848 len++;
3849 if (!(len & 3) && i > 0) {
3850 i--;
3851 str[i] = ',';
3855 if (len_arg >= 0)
3856 trace_seq_printf(s, format, len_arg, str);
3857 else
3858 trace_seq_printf(s, format, str);
3860 free(str);
3863 static void print_str_arg(struct trace_seq *s, void *data, int size,
3864 struct event_format *event, const char *format,
3865 int len_arg, struct print_arg *arg)
3867 struct tep_handle *pevent = event->pevent;
3868 struct print_flag_sym *flag;
3869 struct format_field *field;
3870 struct printk_map *printk;
3871 long long val, fval;
3872 unsigned long long addr;
3873 char *str;
3874 unsigned char *hex;
3875 int print;
3876 int i, len;
3878 switch (arg->type) {
3879 case PRINT_NULL:
3880 /* ?? */
3881 return;
3882 case PRINT_ATOM:
3883 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3884 return;
3885 case PRINT_FIELD:
3886 field = arg->field.field;
3887 if (!field) {
3888 field = tep_find_any_field(event, arg->field.name);
3889 if (!field) {
3890 str = arg->field.name;
3891 goto out_warning_field;
3893 arg->field.field = field;
3895 /* Zero sized fields, mean the rest of the data */
3896 len = field->size ? : size - field->offset;
3899 * Some events pass in pointers. If this is not an array
3900 * and the size is the same as long_size, assume that it
3901 * is a pointer.
3903 if (!(field->flags & FIELD_IS_ARRAY) &&
3904 field->size == pevent->long_size) {
3906 /* Handle heterogeneous recording and processing
3907 * architectures
3909 * CASE I:
3910 * Traces recorded on 32-bit devices (32-bit
3911 * addressing) and processed on 64-bit devices:
3912 * In this case, only 32 bits should be read.
3914 * CASE II:
3915 * Traces recorded on 64 bit devices and processed
3916 * on 32-bit devices:
3917 * In this case, 64 bits must be read.
3919 addr = (pevent->long_size == 8) ?
3920 *(unsigned long long *)(data + field->offset) :
3921 (unsigned long long)*(unsigned int *)(data + field->offset);
3923 /* Check if it matches a print format */
3924 printk = find_printk(pevent, addr);
3925 if (printk)
3926 trace_seq_puts(s, printk->printk);
3927 else
3928 trace_seq_printf(s, "%llx", addr);
3929 break;
3931 str = malloc(len + 1);
3932 if (!str) {
3933 do_warning_event(event, "%s: not enough memory!",
3934 __func__);
3935 return;
3937 memcpy(str, data + field->offset, len);
3938 str[len] = 0;
3939 print_str_to_seq(s, format, len_arg, str);
3940 free(str);
3941 break;
3942 case PRINT_FLAGS:
3943 val = eval_num_arg(data, size, event, arg->flags.field);
3944 print = 0;
3945 for (flag = arg->flags.flags; flag; flag = flag->next) {
3946 fval = eval_flag(flag->value);
3947 if (!val && fval < 0) {
3948 print_str_to_seq(s, format, len_arg, flag->str);
3949 break;
3951 if (fval > 0 && (val & fval) == fval) {
3952 if (print && arg->flags.delim)
3953 trace_seq_puts(s, arg->flags.delim);
3954 print_str_to_seq(s, format, len_arg, flag->str);
3955 print = 1;
3956 val &= ~fval;
3959 if (val) {
3960 if (print && arg->flags.delim)
3961 trace_seq_puts(s, arg->flags.delim);
3962 trace_seq_printf(s, "0x%llx", val);
3964 break;
3965 case PRINT_SYMBOL:
3966 val = eval_num_arg(data, size, event, arg->symbol.field);
3967 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3968 fval = eval_flag(flag->value);
3969 if (val == fval) {
3970 print_str_to_seq(s, format, len_arg, flag->str);
3971 break;
3974 if (!flag)
3975 trace_seq_printf(s, "0x%llx", val);
3976 break;
3977 case PRINT_HEX:
3978 case PRINT_HEX_STR:
3979 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3980 unsigned long offset;
3981 offset = tep_read_number(pevent,
3982 data + arg->hex.field->dynarray.field->offset,
3983 arg->hex.field->dynarray.field->size);
3984 hex = data + (offset & 0xffff);
3985 } else {
3986 field = arg->hex.field->field.field;
3987 if (!field) {
3988 str = arg->hex.field->field.name;
3989 field = tep_find_any_field(event, str);
3990 if (!field)
3991 goto out_warning_field;
3992 arg->hex.field->field.field = field;
3994 hex = data + field->offset;
3996 len = eval_num_arg(data, size, event, arg->hex.size);
3997 for (i = 0; i < len; i++) {
3998 if (i && arg->type == PRINT_HEX)
3999 trace_seq_putc(s, ' ');
4000 trace_seq_printf(s, "%02x", hex[i]);
4002 break;
4004 case PRINT_INT_ARRAY: {
4005 void *num;
4006 int el_size;
4008 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
4009 unsigned long offset;
4010 struct format_field *field =
4011 arg->int_array.field->dynarray.field;
4012 offset = tep_read_number(pevent,
4013 data + field->offset,
4014 field->size);
4015 num = data + (offset & 0xffff);
4016 } else {
4017 field = arg->int_array.field->field.field;
4018 if (!field) {
4019 str = arg->int_array.field->field.name;
4020 field = tep_find_any_field(event, str);
4021 if (!field)
4022 goto out_warning_field;
4023 arg->int_array.field->field.field = field;
4025 num = data + field->offset;
4027 len = eval_num_arg(data, size, event, arg->int_array.count);
4028 el_size = eval_num_arg(data, size, event,
4029 arg->int_array.el_size);
4030 for (i = 0; i < len; i++) {
4031 if (i)
4032 trace_seq_putc(s, ' ');
4034 if (el_size == 1) {
4035 trace_seq_printf(s, "%u", *(uint8_t *)num);
4036 } else if (el_size == 2) {
4037 trace_seq_printf(s, "%u", *(uint16_t *)num);
4038 } else if (el_size == 4) {
4039 trace_seq_printf(s, "%u", *(uint32_t *)num);
4040 } else if (el_size == 8) {
4041 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4042 } else {
4043 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4044 el_size, *(uint8_t *)num);
4045 el_size = 1;
4048 num += el_size;
4050 break;
4052 case PRINT_TYPE:
4053 break;
4054 case PRINT_STRING: {
4055 int str_offset;
4057 if (arg->string.offset == -1) {
4058 struct format_field *f;
4060 f = tep_find_any_field(event, arg->string.string);
4061 arg->string.offset = f->offset;
4063 str_offset = data2host4(pevent, data + arg->string.offset);
4064 str_offset &= 0xffff;
4065 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4066 break;
4068 case PRINT_BSTRING:
4069 print_str_to_seq(s, format, len_arg, arg->string.string);
4070 break;
4071 case PRINT_BITMASK: {
4072 int bitmask_offset;
4073 int bitmask_size;
4075 if (arg->bitmask.offset == -1) {
4076 struct format_field *f;
4078 f = tep_find_any_field(event, arg->bitmask.bitmask);
4079 arg->bitmask.offset = f->offset;
4081 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4082 bitmask_size = bitmask_offset >> 16;
4083 bitmask_offset &= 0xffff;
4084 print_bitmask_to_seq(pevent, s, format, len_arg,
4085 data + bitmask_offset, bitmask_size);
4086 break;
4088 case PRINT_OP:
4090 * The only op for string should be ? :
4092 if (arg->op.op[0] != '?')
4093 return;
4094 val = eval_num_arg(data, size, event, arg->op.left);
4095 if (val)
4096 print_str_arg(s, data, size, event,
4097 format, len_arg, arg->op.right->op.left);
4098 else
4099 print_str_arg(s, data, size, event,
4100 format, len_arg, arg->op.right->op.right);
4101 break;
4102 case PRINT_FUNC:
4103 process_defined_func(s, data, size, event, arg);
4104 break;
4105 default:
4106 /* well... */
4107 break;
4110 return;
4112 out_warning_field:
4113 do_warning_event(event, "%s: field %s not found",
4114 __func__, arg->field.name);
4117 static unsigned long long
4118 process_defined_func(struct trace_seq *s, void *data, int size,
4119 struct event_format *event, struct print_arg *arg)
4121 struct tep_function_handler *func_handle = arg->func.func;
4122 struct func_params *param;
4123 unsigned long long *args;
4124 unsigned long long ret;
4125 struct print_arg *farg;
4126 struct trace_seq str;
4127 struct save_str {
4128 struct save_str *next;
4129 char *str;
4130 } *strings = NULL, *string;
4131 int i;
4133 if (!func_handle->nr_args) {
4134 ret = (*func_handle->func)(s, NULL);
4135 goto out;
4138 farg = arg->func.args;
4139 param = func_handle->params;
4141 ret = ULLONG_MAX;
4142 args = malloc(sizeof(*args) * func_handle->nr_args);
4143 if (!args)
4144 goto out;
4146 for (i = 0; i < func_handle->nr_args; i++) {
4147 switch (param->type) {
4148 case TEP_FUNC_ARG_INT:
4149 case TEP_FUNC_ARG_LONG:
4150 case TEP_FUNC_ARG_PTR:
4151 args[i] = eval_num_arg(data, size, event, farg);
4152 break;
4153 case TEP_FUNC_ARG_STRING:
4154 trace_seq_init(&str);
4155 print_str_arg(&str, data, size, event, "%s", -1, farg);
4156 trace_seq_terminate(&str);
4157 string = malloc(sizeof(*string));
4158 if (!string) {
4159 do_warning_event(event, "%s(%d): malloc str",
4160 __func__, __LINE__);
4161 goto out_free;
4163 string->next = strings;
4164 string->str = strdup(str.buffer);
4165 if (!string->str) {
4166 free(string);
4167 do_warning_event(event, "%s(%d): malloc str",
4168 __func__, __LINE__);
4169 goto out_free;
4171 args[i] = (uintptr_t)string->str;
4172 strings = string;
4173 trace_seq_destroy(&str);
4174 break;
4175 default:
4177 * Something went totally wrong, this is not
4178 * an input error, something in this code broke.
4180 do_warning_event(event, "Unexpected end of arguments\n");
4181 goto out_free;
4183 farg = farg->next;
4184 param = param->next;
4187 ret = (*func_handle->func)(s, args);
4188 out_free:
4189 free(args);
4190 while (strings) {
4191 string = strings;
4192 strings = string->next;
4193 free(string->str);
4194 free(string);
4197 out:
4198 /* TBD : handle return type here */
4199 return ret;
4202 static void free_args(struct print_arg *args)
4204 struct print_arg *next;
4206 while (args) {
4207 next = args->next;
4209 free_arg(args);
4210 args = next;
4214 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4216 struct tep_handle *pevent = event->pevent;
4217 struct format_field *field, *ip_field;
4218 struct print_arg *args, *arg, **next;
4219 unsigned long long ip, val;
4220 char *ptr;
4221 void *bptr;
4222 int vsize;
4224 field = pevent->bprint_buf_field;
4225 ip_field = pevent->bprint_ip_field;
4227 if (!field) {
4228 field = tep_find_field(event, "buf");
4229 if (!field) {
4230 do_warning_event(event, "can't find buffer field for binary printk");
4231 return NULL;
4233 ip_field = tep_find_field(event, "ip");
4234 if (!ip_field) {
4235 do_warning_event(event, "can't find ip field for binary printk");
4236 return NULL;
4238 pevent->bprint_buf_field = field;
4239 pevent->bprint_ip_field = ip_field;
4242 ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
4245 * The first arg is the IP pointer.
4247 args = alloc_arg();
4248 if (!args) {
4249 do_warning_event(event, "%s(%d): not enough memory!",
4250 __func__, __LINE__);
4251 return NULL;
4253 arg = args;
4254 arg->next = NULL;
4255 next = &arg->next;
4257 arg->type = PRINT_ATOM;
4259 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4260 goto out_free;
4262 /* skip the first "%ps: " */
4263 for (ptr = fmt + 5, bptr = data + field->offset;
4264 bptr < data + size && *ptr; ptr++) {
4265 int ls = 0;
4267 if (*ptr == '%') {
4268 process_again:
4269 ptr++;
4270 switch (*ptr) {
4271 case '%':
4272 break;
4273 case 'l':
4274 ls++;
4275 goto process_again;
4276 case 'L':
4277 ls = 2;
4278 goto process_again;
4279 case '0' ... '9':
4280 goto process_again;
4281 case '.':
4282 goto process_again;
4283 case 'z':
4284 case 'Z':
4285 ls = 1;
4286 goto process_again;
4287 case 'p':
4288 ls = 1;
4289 if (isalnum(ptr[1])) {
4290 ptr++;
4291 /* Check for special pointers */
4292 switch (*ptr) {
4293 case 's':
4294 case 'S':
4295 case 'f':
4296 case 'F':
4297 break;
4298 default:
4300 * Older kernels do not process
4301 * dereferenced pointers.
4302 * Only process if the pointer
4303 * value is a printable.
4305 if (isprint(*(char *)bptr))
4306 goto process_string;
4309 /* fall through */
4310 case 'd':
4311 case 'u':
4312 case 'x':
4313 case 'i':
4314 switch (ls) {
4315 case 0:
4316 vsize = 4;
4317 break;
4318 case 1:
4319 vsize = pevent->long_size;
4320 break;
4321 case 2:
4322 vsize = 8;
4323 break;
4324 default:
4325 vsize = ls; /* ? */
4326 break;
4328 /* fall through */
4329 case '*':
4330 if (*ptr == '*')
4331 vsize = 4;
4333 /* the pointers are always 4 bytes aligned */
4334 bptr = (void *)(((unsigned long)bptr + 3) &
4335 ~3);
4336 val = tep_read_number(pevent, bptr, vsize);
4337 bptr += vsize;
4338 arg = alloc_arg();
4339 if (!arg) {
4340 do_warning_event(event, "%s(%d): not enough memory!",
4341 __func__, __LINE__);
4342 goto out_free;
4344 arg->next = NULL;
4345 arg->type = PRINT_ATOM;
4346 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4347 free(arg);
4348 goto out_free;
4350 *next = arg;
4351 next = &arg->next;
4353 * The '*' case means that an arg is used as the length.
4354 * We need to continue to figure out for what.
4356 if (*ptr == '*')
4357 goto process_again;
4359 break;
4360 case 's':
4361 process_string:
4362 arg = alloc_arg();
4363 if (!arg) {
4364 do_warning_event(event, "%s(%d): not enough memory!",
4365 __func__, __LINE__);
4366 goto out_free;
4368 arg->next = NULL;
4369 arg->type = PRINT_BSTRING;
4370 arg->string.string = strdup(bptr);
4371 if (!arg->string.string)
4372 goto out_free;
4373 bptr += strlen(bptr) + 1;
4374 *next = arg;
4375 next = &arg->next;
4376 default:
4377 break;
4382 return args;
4384 out_free:
4385 free_args(args);
4386 return NULL;
4389 static char *
4390 get_bprint_format(void *data, int size __maybe_unused,
4391 struct event_format *event)
4393 struct tep_handle *pevent = event->pevent;
4394 unsigned long long addr;
4395 struct format_field *field;
4396 struct printk_map *printk;
4397 char *format;
4399 field = pevent->bprint_fmt_field;
4401 if (!field) {
4402 field = tep_find_field(event, "fmt");
4403 if (!field) {
4404 do_warning_event(event, "can't find format field for binary printk");
4405 return NULL;
4407 pevent->bprint_fmt_field = field;
4410 addr = tep_read_number(pevent, data + field->offset, field->size);
4412 printk = find_printk(pevent, addr);
4413 if (!printk) {
4414 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4415 return NULL;
4416 return format;
4419 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4420 return NULL;
4422 return format;
4425 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4426 struct event_format *event, struct print_arg *arg)
4428 unsigned char *buf;
4429 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4431 if (arg->type == PRINT_FUNC) {
4432 process_defined_func(s, data, size, event, arg);
4433 return;
4436 if (arg->type != PRINT_FIELD) {
4437 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4438 arg->type);
4439 return;
4442 if (mac == 'm')
4443 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4444 if (!arg->field.field) {
4445 arg->field.field =
4446 tep_find_any_field(event, arg->field.name);
4447 if (!arg->field.field) {
4448 do_warning_event(event, "%s: field %s not found",
4449 __func__, arg->field.name);
4450 return;
4453 if (arg->field.field->size != 6) {
4454 trace_seq_printf(s, "INVALIDMAC");
4455 return;
4457 buf = data + arg->field.field->offset;
4458 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4461 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4463 const char *fmt;
4465 if (i == 'i')
4466 fmt = "%03d.%03d.%03d.%03d";
4467 else
4468 fmt = "%d.%d.%d.%d";
4470 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4473 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4475 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4476 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4479 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4481 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4484 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4486 int i, j, range;
4487 unsigned char zerolength[8];
4488 int longest = 1;
4489 int colonpos = -1;
4490 uint16_t word;
4491 uint8_t hi, lo;
4492 bool needcolon = false;
4493 bool useIPv4;
4494 struct in6_addr in6;
4496 memcpy(&in6, addr, sizeof(struct in6_addr));
4498 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4500 memset(zerolength, 0, sizeof(zerolength));
4502 if (useIPv4)
4503 range = 6;
4504 else
4505 range = 8;
4507 /* find position of longest 0 run */
4508 for (i = 0; i < range; i++) {
4509 for (j = i; j < range; j++) {
4510 if (in6.s6_addr16[j] != 0)
4511 break;
4512 zerolength[i]++;
4515 for (i = 0; i < range; i++) {
4516 if (zerolength[i] > longest) {
4517 longest = zerolength[i];
4518 colonpos = i;
4521 if (longest == 1) /* don't compress a single 0 */
4522 colonpos = -1;
4524 /* emit address */
4525 for (i = 0; i < range; i++) {
4526 if (i == colonpos) {
4527 if (needcolon || i == 0)
4528 trace_seq_printf(s, ":");
4529 trace_seq_printf(s, ":");
4530 needcolon = false;
4531 i += longest - 1;
4532 continue;
4534 if (needcolon) {
4535 trace_seq_printf(s, ":");
4536 needcolon = false;
4538 /* hex u16 without leading 0s */
4539 word = ntohs(in6.s6_addr16[i]);
4540 hi = word >> 8;
4541 lo = word & 0xff;
4542 if (hi)
4543 trace_seq_printf(s, "%x%02x", hi, lo);
4544 else
4545 trace_seq_printf(s, "%x", lo);
4547 needcolon = true;
4550 if (useIPv4) {
4551 if (needcolon)
4552 trace_seq_printf(s, ":");
4553 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4556 return;
4559 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4561 int j;
4563 for (j = 0; j < 16; j += 2) {
4564 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4565 if (i == 'I' && j < 14)
4566 trace_seq_printf(s, ":");
4571 * %pi4 print an IPv4 address with leading zeros
4572 * %pI4 print an IPv4 address without leading zeros
4573 * %pi6 print an IPv6 address without colons
4574 * %pI6 print an IPv6 address with colons
4575 * %pI6c print an IPv6 address in compressed form with colons
4576 * %pISpc print an IP address based on sockaddr; p adds port.
4578 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4579 void *data, int size, struct event_format *event,
4580 struct print_arg *arg)
4582 unsigned char *buf;
4584 if (arg->type == PRINT_FUNC) {
4585 process_defined_func(s, data, size, event, arg);
4586 return 0;
4589 if (arg->type != PRINT_FIELD) {
4590 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4591 return 0;
4594 if (!arg->field.field) {
4595 arg->field.field =
4596 tep_find_any_field(event, arg->field.name);
4597 if (!arg->field.field) {
4598 do_warning("%s: field %s not found",
4599 __func__, arg->field.name);
4600 return 0;
4604 buf = data + arg->field.field->offset;
4606 if (arg->field.field->size != 4) {
4607 trace_seq_printf(s, "INVALIDIPv4");
4608 return 0;
4610 print_ip4_addr(s, i, buf);
4612 return 0;
4615 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4616 void *data, int size, struct event_format *event,
4617 struct print_arg *arg)
4619 char have_c = 0;
4620 unsigned char *buf;
4621 int rc = 0;
4623 /* pI6c */
4624 if (i == 'I' && *ptr == 'c') {
4625 have_c = 1;
4626 ptr++;
4627 rc++;
4630 if (arg->type == PRINT_FUNC) {
4631 process_defined_func(s, data, size, event, arg);
4632 return rc;
4635 if (arg->type != PRINT_FIELD) {
4636 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4637 return rc;
4640 if (!arg->field.field) {
4641 arg->field.field =
4642 tep_find_any_field(event, arg->field.name);
4643 if (!arg->field.field) {
4644 do_warning("%s: field %s not found",
4645 __func__, arg->field.name);
4646 return rc;
4650 buf = data + arg->field.field->offset;
4652 if (arg->field.field->size != 16) {
4653 trace_seq_printf(s, "INVALIDIPv6");
4654 return rc;
4657 if (have_c)
4658 print_ip6c_addr(s, buf);
4659 else
4660 print_ip6_addr(s, i, buf);
4662 return rc;
4665 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4666 void *data, int size, struct event_format *event,
4667 struct print_arg *arg)
4669 char have_c = 0, have_p = 0;
4670 unsigned char *buf;
4671 struct sockaddr_storage *sa;
4672 int rc = 0;
4674 /* pISpc */
4675 if (i == 'I') {
4676 if (*ptr == 'p') {
4677 have_p = 1;
4678 ptr++;
4679 rc++;
4681 if (*ptr == 'c') {
4682 have_c = 1;
4683 ptr++;
4684 rc++;
4688 if (arg->type == PRINT_FUNC) {
4689 process_defined_func(s, data, size, event, arg);
4690 return rc;
4693 if (arg->type != PRINT_FIELD) {
4694 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4695 return rc;
4698 if (!arg->field.field) {
4699 arg->field.field =
4700 tep_find_any_field(event, arg->field.name);
4701 if (!arg->field.field) {
4702 do_warning("%s: field %s not found",
4703 __func__, arg->field.name);
4704 return rc;
4708 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4710 if (sa->ss_family == AF_INET) {
4711 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4713 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4714 trace_seq_printf(s, "INVALIDIPv4");
4715 return rc;
4718 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4719 if (have_p)
4720 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4723 } else if (sa->ss_family == AF_INET6) {
4724 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4726 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4727 trace_seq_printf(s, "INVALIDIPv6");
4728 return rc;
4731 if (have_p)
4732 trace_seq_printf(s, "[");
4734 buf = (unsigned char *) &sa6->sin6_addr;
4735 if (have_c)
4736 print_ip6c_addr(s, buf);
4737 else
4738 print_ip6_addr(s, i, buf);
4740 if (have_p)
4741 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4744 return rc;
4747 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4748 void *data, int size, struct event_format *event,
4749 struct print_arg *arg)
4751 char i = *ptr; /* 'i' or 'I' */
4752 char ver;
4753 int rc = 0;
4755 ptr++;
4756 rc++;
4758 ver = *ptr;
4759 ptr++;
4760 rc++;
4762 switch (ver) {
4763 case '4':
4764 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4765 break;
4766 case '6':
4767 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4768 break;
4769 case 'S':
4770 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4771 break;
4772 default:
4773 return 0;
4776 return rc;
4779 static int is_printable_array(char *p, unsigned int len)
4781 unsigned int i;
4783 for (i = 0; i < len && p[i]; i++)
4784 if (!isprint(p[i]) && !isspace(p[i]))
4785 return 0;
4786 return 1;
4789 void tep_print_field(struct trace_seq *s, void *data,
4790 struct format_field *field)
4792 unsigned long long val;
4793 unsigned int offset, len, i;
4794 struct tep_handle *pevent = field->event->pevent;
4796 if (field->flags & FIELD_IS_ARRAY) {
4797 offset = field->offset;
4798 len = field->size;
4799 if (field->flags & FIELD_IS_DYNAMIC) {
4800 val = tep_read_number(pevent, data + offset, len);
4801 offset = val;
4802 len = offset >> 16;
4803 offset &= 0xffff;
4805 if (field->flags & FIELD_IS_STRING &&
4806 is_printable_array(data + offset, len)) {
4807 trace_seq_printf(s, "%s", (char *)data + offset);
4808 } else {
4809 trace_seq_puts(s, "ARRAY[");
4810 for (i = 0; i < len; i++) {
4811 if (i)
4812 trace_seq_puts(s, ", ");
4813 trace_seq_printf(s, "%02x",
4814 *((unsigned char *)data + offset + i));
4816 trace_seq_putc(s, ']');
4817 field->flags &= ~FIELD_IS_STRING;
4819 } else {
4820 val = tep_read_number(pevent, data + field->offset,
4821 field->size);
4822 if (field->flags & FIELD_IS_POINTER) {
4823 trace_seq_printf(s, "0x%llx", val);
4824 } else if (field->flags & FIELD_IS_SIGNED) {
4825 switch (field->size) {
4826 case 4:
4828 * If field is long then print it in hex.
4829 * A long usually stores pointers.
4831 if (field->flags & FIELD_IS_LONG)
4832 trace_seq_printf(s, "0x%x", (int)val);
4833 else
4834 trace_seq_printf(s, "%d", (int)val);
4835 break;
4836 case 2:
4837 trace_seq_printf(s, "%2d", (short)val);
4838 break;
4839 case 1:
4840 trace_seq_printf(s, "%1d", (char)val);
4841 break;
4842 default:
4843 trace_seq_printf(s, "%lld", val);
4845 } else {
4846 if (field->flags & FIELD_IS_LONG)
4847 trace_seq_printf(s, "0x%llx", val);
4848 else
4849 trace_seq_printf(s, "%llu", val);
4854 void tep_print_fields(struct trace_seq *s, void *data,
4855 int size __maybe_unused, struct event_format *event)
4857 struct format_field *field;
4859 field = event->format.fields;
4860 while (field) {
4861 trace_seq_printf(s, " %s=", field->name);
4862 tep_print_field(s, data, field);
4863 field = field->next;
4867 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4869 struct tep_handle *pevent = event->pevent;
4870 struct print_fmt *print_fmt = &event->print_fmt;
4871 struct print_arg *arg = print_fmt->args;
4872 struct print_arg *args = NULL;
4873 const char *ptr = print_fmt->format;
4874 unsigned long long val;
4875 struct func_map *func;
4876 const char *saveptr;
4877 struct trace_seq p;
4878 char *bprint_fmt = NULL;
4879 char format[32];
4880 int show_func;
4881 int len_as_arg;
4882 int len_arg;
4883 int len;
4884 int ls;
4886 if (event->flags & EVENT_FL_FAILED) {
4887 trace_seq_printf(s, "[FAILED TO PARSE]");
4888 tep_print_fields(s, data, size, event);
4889 return;
4892 if (event->flags & EVENT_FL_ISBPRINT) {
4893 bprint_fmt = get_bprint_format(data, size, event);
4894 args = make_bprint_args(bprint_fmt, data, size, event);
4895 arg = args;
4896 ptr = bprint_fmt;
4899 for (; *ptr; ptr++) {
4900 ls = 0;
4901 if (*ptr == '\\') {
4902 ptr++;
4903 switch (*ptr) {
4904 case 'n':
4905 trace_seq_putc(s, '\n');
4906 break;
4907 case 't':
4908 trace_seq_putc(s, '\t');
4909 break;
4910 case 'r':
4911 trace_seq_putc(s, '\r');
4912 break;
4913 case '\\':
4914 trace_seq_putc(s, '\\');
4915 break;
4916 default:
4917 trace_seq_putc(s, *ptr);
4918 break;
4921 } else if (*ptr == '%') {
4922 saveptr = ptr;
4923 show_func = 0;
4924 len_as_arg = 0;
4925 cont_process:
4926 ptr++;
4927 switch (*ptr) {
4928 case '%':
4929 trace_seq_putc(s, '%');
4930 break;
4931 case '#':
4932 /* FIXME: need to handle properly */
4933 goto cont_process;
4934 case 'h':
4935 ls--;
4936 goto cont_process;
4937 case 'l':
4938 ls++;
4939 goto cont_process;
4940 case 'L':
4941 ls = 2;
4942 goto cont_process;
4943 case '*':
4944 /* The argument is the length. */
4945 if (!arg) {
4946 do_warning_event(event, "no argument match");
4947 event->flags |= EVENT_FL_FAILED;
4948 goto out_failed;
4950 len_arg = eval_num_arg(data, size, event, arg);
4951 len_as_arg = 1;
4952 arg = arg->next;
4953 goto cont_process;
4954 case '.':
4955 case 'z':
4956 case 'Z':
4957 case '0' ... '9':
4958 case '-':
4959 goto cont_process;
4960 case 'p':
4961 if (pevent->long_size == 4)
4962 ls = 1;
4963 else
4964 ls = 2;
4966 if (isalnum(ptr[1]))
4967 ptr++;
4969 if (arg->type == PRINT_BSTRING) {
4970 trace_seq_puts(s, arg->string.string);
4971 break;
4974 if (*ptr == 'F' || *ptr == 'f' ||
4975 *ptr == 'S' || *ptr == 's') {
4976 show_func = *ptr;
4977 } else if (*ptr == 'M' || *ptr == 'm') {
4978 print_mac_arg(s, *ptr, data, size, event, arg);
4979 arg = arg->next;
4980 break;
4981 } else if (*ptr == 'I' || *ptr == 'i') {
4982 int n;
4984 n = print_ip_arg(s, ptr, data, size, event, arg);
4985 if (n > 0) {
4986 ptr += n - 1;
4987 arg = arg->next;
4988 break;
4992 /* fall through */
4993 case 'd':
4994 case 'i':
4995 case 'x':
4996 case 'X':
4997 case 'u':
4998 if (!arg) {
4999 do_warning_event(event, "no argument match");
5000 event->flags |= EVENT_FL_FAILED;
5001 goto out_failed;
5004 len = ((unsigned long)ptr + 1) -
5005 (unsigned long)saveptr;
5007 /* should never happen */
5008 if (len > 31) {
5009 do_warning_event(event, "bad format!");
5010 event->flags |= EVENT_FL_FAILED;
5011 len = 31;
5014 memcpy(format, saveptr, len);
5015 format[len] = 0;
5017 val = eval_num_arg(data, size, event, arg);
5018 arg = arg->next;
5020 if (show_func) {
5021 func = find_func(pevent, val);
5022 if (func) {
5023 trace_seq_puts(s, func->func);
5024 if (show_func == 'F')
5025 trace_seq_printf(s,
5026 "+0x%llx",
5027 val - func->addr);
5028 break;
5031 if (pevent->long_size == 8 && ls == 1 &&
5032 sizeof(long) != 8) {
5033 char *p;
5035 /* make %l into %ll */
5036 if (ls == 1 && (p = strchr(format, 'l')))
5037 memmove(p+1, p, strlen(p)+1);
5038 else if (strcmp(format, "%p") == 0)
5039 strcpy(format, "0x%llx");
5040 ls = 2;
5042 switch (ls) {
5043 case -2:
5044 if (len_as_arg)
5045 trace_seq_printf(s, format, len_arg, (char)val);
5046 else
5047 trace_seq_printf(s, format, (char)val);
5048 break;
5049 case -1:
5050 if (len_as_arg)
5051 trace_seq_printf(s, format, len_arg, (short)val);
5052 else
5053 trace_seq_printf(s, format, (short)val);
5054 break;
5055 case 0:
5056 if (len_as_arg)
5057 trace_seq_printf(s, format, len_arg, (int)val);
5058 else
5059 trace_seq_printf(s, format, (int)val);
5060 break;
5061 case 1:
5062 if (len_as_arg)
5063 trace_seq_printf(s, format, len_arg, (long)val);
5064 else
5065 trace_seq_printf(s, format, (long)val);
5066 break;
5067 case 2:
5068 if (len_as_arg)
5069 trace_seq_printf(s, format, len_arg,
5070 (long long)val);
5071 else
5072 trace_seq_printf(s, format, (long long)val);
5073 break;
5074 default:
5075 do_warning_event(event, "bad count (%d)", ls);
5076 event->flags |= EVENT_FL_FAILED;
5078 break;
5079 case 's':
5080 if (!arg) {
5081 do_warning_event(event, "no matching argument");
5082 event->flags |= EVENT_FL_FAILED;
5083 goto out_failed;
5086 len = ((unsigned long)ptr + 1) -
5087 (unsigned long)saveptr;
5089 /* should never happen */
5090 if (len > 31) {
5091 do_warning_event(event, "bad format!");
5092 event->flags |= EVENT_FL_FAILED;
5093 len = 31;
5096 memcpy(format, saveptr, len);
5097 format[len] = 0;
5098 if (!len_as_arg)
5099 len_arg = -1;
5100 /* Use helper trace_seq */
5101 trace_seq_init(&p);
5102 print_str_arg(&p, data, size, event,
5103 format, len_arg, arg);
5104 trace_seq_terminate(&p);
5105 trace_seq_puts(s, p.buffer);
5106 trace_seq_destroy(&p);
5107 arg = arg->next;
5108 break;
5109 default:
5110 trace_seq_printf(s, ">%c<", *ptr);
5113 } else
5114 trace_seq_putc(s, *ptr);
5117 if (event->flags & EVENT_FL_FAILED) {
5118 out_failed:
5119 trace_seq_printf(s, "[FAILED TO PARSE]");
5122 if (args) {
5123 free_args(args);
5124 free(bprint_fmt);
5129 * tep_data_lat_fmt - parse the data for the latency format
5130 * @pevent: a handle to the pevent
5131 * @s: the trace_seq to write to
5132 * @record: the record to read from
5134 * This parses out the Latency format (interrupts disabled,
5135 * need rescheduling, in hard/soft interrupt, preempt count
5136 * and lock depth) and places it into the trace_seq.
5138 void tep_data_lat_fmt(struct tep_handle *pevent,
5139 struct trace_seq *s, struct tep_record *record)
5141 static int check_lock_depth = 1;
5142 static int check_migrate_disable = 1;
5143 static int lock_depth_exists;
5144 static int migrate_disable_exists;
5145 unsigned int lat_flags;
5146 unsigned int pc;
5147 int lock_depth;
5148 int migrate_disable;
5149 int hardirq;
5150 int softirq;
5151 void *data = record->data;
5153 lat_flags = parse_common_flags(pevent, data);
5154 pc = parse_common_pc(pevent, data);
5155 /* lock_depth may not always exist */
5156 if (lock_depth_exists)
5157 lock_depth = parse_common_lock_depth(pevent, data);
5158 else if (check_lock_depth) {
5159 lock_depth = parse_common_lock_depth(pevent, data);
5160 if (lock_depth < 0)
5161 check_lock_depth = 0;
5162 else
5163 lock_depth_exists = 1;
5166 /* migrate_disable may not always exist */
5167 if (migrate_disable_exists)
5168 migrate_disable = parse_common_migrate_disable(pevent, data);
5169 else if (check_migrate_disable) {
5170 migrate_disable = parse_common_migrate_disable(pevent, data);
5171 if (migrate_disable < 0)
5172 check_migrate_disable = 0;
5173 else
5174 migrate_disable_exists = 1;
5177 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5178 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5180 trace_seq_printf(s, "%c%c%c",
5181 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5182 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5183 'X' : '.',
5184 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5185 'N' : '.',
5186 (hardirq && softirq) ? 'H' :
5187 hardirq ? 'h' : softirq ? 's' : '.');
5189 if (pc)
5190 trace_seq_printf(s, "%x", pc);
5191 else
5192 trace_seq_putc(s, '.');
5194 if (migrate_disable_exists) {
5195 if (migrate_disable < 0)
5196 trace_seq_putc(s, '.');
5197 else
5198 trace_seq_printf(s, "%d", migrate_disable);
5201 if (lock_depth_exists) {
5202 if (lock_depth < 0)
5203 trace_seq_putc(s, '.');
5204 else
5205 trace_seq_printf(s, "%d", lock_depth);
5208 trace_seq_terminate(s);
5212 * tep_data_type - parse out the given event type
5213 * @pevent: a handle to the pevent
5214 * @rec: the record to read from
5216 * This returns the event id from the @rec.
5218 int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
5220 return trace_parse_common_type(pevent, rec->data);
5224 * tep_data_event_from_type - find the event by a given type
5225 * @pevent: a handle to the pevent
5226 * @type: the type of the event.
5228 * This returns the event form a given @type;
5230 struct event_format *tep_data_event_from_type(struct tep_handle *pevent, int type)
5232 return tep_find_event(pevent, type);
5236 * tep_data_pid - parse the PID from record
5237 * @pevent: a handle to the pevent
5238 * @rec: the record to parse
5240 * This returns the PID from a record.
5242 int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
5244 return parse_common_pid(pevent, rec->data);
5248 * tep_data_preempt_count - parse the preempt count from the record
5249 * @pevent: a handle to the pevent
5250 * @rec: the record to parse
5252 * This returns the preempt count from a record.
5254 int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
5256 return parse_common_pc(pevent, rec->data);
5260 * tep_data_flags - parse the latency flags from the record
5261 * @pevent: a handle to the pevent
5262 * @rec: the record to parse
5264 * This returns the latency flags from a record.
5266 * Use trace_flag_type enum for the flags (see event-parse.h).
5268 int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
5270 return parse_common_flags(pevent, rec->data);
5274 * tep_data_comm_from_pid - return the command line from PID
5275 * @pevent: a handle to the pevent
5276 * @pid: the PID of the task to search for
5278 * This returns a pointer to the command line that has the given
5279 * @pid.
5281 const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
5283 const char *comm;
5285 comm = find_cmdline(pevent, pid);
5286 return comm;
5289 static struct cmdline *
5290 pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct cmdline *next)
5292 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5294 if (cmdlist)
5295 cmdlist = cmdlist->next;
5296 else
5297 cmdlist = pevent->cmdlist;
5299 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5300 cmdlist = cmdlist->next;
5302 return (struct cmdline *)cmdlist;
5306 * tep_data_pid_from_comm - return the pid from a given comm
5307 * @pevent: a handle to the pevent
5308 * @comm: the cmdline to find the pid from
5309 * @next: the cmdline structure to find the next comm
5311 * This returns the cmdline structure that holds a pid for a given
5312 * comm, or NULL if none found. As there may be more than one pid for
5313 * a given comm, the result of this call can be passed back into
5314 * a recurring call in the @next paramater, and then it will find the
5315 * next pid.
5316 * Also, it does a linear seach, so it may be slow.
5318 struct cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
5319 struct cmdline *next)
5321 struct cmdline *cmdline;
5324 * If the cmdlines have not been converted yet, then use
5325 * the list.
5327 if (!pevent->cmdlines)
5328 return pid_from_cmdlist(pevent, comm, next);
5330 if (next) {
5332 * The next pointer could have been still from
5333 * a previous call before cmdlines were created
5335 if (next < pevent->cmdlines ||
5336 next >= pevent->cmdlines + pevent->cmdline_count)
5337 next = NULL;
5338 else
5339 cmdline = next++;
5342 if (!next)
5343 cmdline = pevent->cmdlines;
5345 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5346 if (strcmp(cmdline->comm, comm) == 0)
5347 return cmdline;
5348 cmdline++;
5350 return NULL;
5354 * tep_cmdline_pid - return the pid associated to a given cmdline
5355 * @cmdline: The cmdline structure to get the pid from
5357 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5358 * -1 is returned.
5360 int tep_cmdline_pid(struct tep_handle *pevent, struct cmdline *cmdline)
5362 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5364 if (!cmdline)
5365 return -1;
5368 * If cmdlines have not been created yet, or cmdline is
5369 * not part of the array, then treat it as a cmdlist instead.
5371 if (!pevent->cmdlines ||
5372 cmdline < pevent->cmdlines ||
5373 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5374 return cmdlist->pid;
5376 return cmdline->pid;
5380 * tep_event_info - parse the data into the print format
5381 * @s: the trace_seq to write to
5382 * @event: the handle to the event
5383 * @record: the record to read from
5385 * This parses the raw @data using the given @event information and
5386 * writes the print format into the trace_seq.
5388 void tep_event_info(struct trace_seq *s, struct event_format *event,
5389 struct tep_record *record)
5391 int print_pretty = 1;
5393 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5394 tep_print_fields(s, record->data, record->size, event);
5395 else {
5397 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5398 print_pretty = event->handler(s, record, event,
5399 event->context);
5401 if (print_pretty)
5402 pretty_print(s, record->data, record->size, event);
5405 trace_seq_terminate(s);
5408 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5410 if (!use_trace_clock)
5411 return true;
5413 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5414 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5415 return true;
5417 /* trace_clock is setting in tsc or counter mode */
5418 return false;
5422 * tep_find_event_by_record - return the event from a given record
5423 * @pevent: a handle to the pevent
5424 * @record: The record to get the event from
5426 * Returns the associated event for a given record, or NULL if non is
5427 * is found.
5429 struct event_format *
5430 tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
5432 int type;
5434 if (record->size < 0) {
5435 do_warning("ug! negative record size %d", record->size);
5436 return NULL;
5439 type = trace_parse_common_type(pevent, record->data);
5441 return tep_find_event(pevent, type);
5445 * tep_print_event_task - Write the event task comm, pid and CPU
5446 * @pevent: a handle to the pevent
5447 * @s: the trace_seq to write to
5448 * @event: the handle to the record's event
5449 * @record: The record to get the event from
5451 * Writes the tasks comm, pid and CPU to @s.
5453 void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
5454 struct event_format *event,
5455 struct tep_record *record)
5457 void *data = record->data;
5458 const char *comm;
5459 int pid;
5461 pid = parse_common_pid(pevent, data);
5462 comm = find_cmdline(pevent, pid);
5464 if (pevent->latency_format) {
5465 trace_seq_printf(s, "%8.8s-%-5d %3d",
5466 comm, pid, record->cpu);
5467 } else
5468 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5472 * tep_print_event_time - Write the event timestamp
5473 * @pevent: a handle to the pevent
5474 * @s: the trace_seq to write to
5475 * @event: the handle to the record's event
5476 * @record: The record to get the event from
5477 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5479 * Writes the timestamp of the record into @s.
5481 void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
5482 struct event_format *event,
5483 struct tep_record *record,
5484 bool use_trace_clock)
5486 unsigned long secs;
5487 unsigned long usecs;
5488 unsigned long nsecs;
5489 int p;
5490 bool use_usec_format;
5492 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5493 use_trace_clock);
5494 if (use_usec_format) {
5495 secs = record->ts / NSEC_PER_SEC;
5496 nsecs = record->ts - secs * NSEC_PER_SEC;
5499 if (pevent->latency_format) {
5500 tep_data_lat_fmt(pevent, s, record);
5503 if (use_usec_format) {
5504 if (pevent->flags & TEP_NSEC_OUTPUT) {
5505 usecs = nsecs;
5506 p = 9;
5507 } else {
5508 usecs = (nsecs + 500) / NSEC_PER_USEC;
5509 /* To avoid usecs larger than 1 sec */
5510 if (usecs >= USEC_PER_SEC) {
5511 usecs -= USEC_PER_SEC;
5512 secs++;
5514 p = 6;
5517 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5518 } else
5519 trace_seq_printf(s, " %12llu:", record->ts);
5523 * tep_print_event_data - Write the event data section
5524 * @pevent: a handle to the pevent
5525 * @s: the trace_seq to write to
5526 * @event: the handle to the record's event
5527 * @record: The record to get the event from
5529 * Writes the parsing of the record's data to @s.
5531 void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
5532 struct event_format *event,
5533 struct tep_record *record)
5535 static const char *spaces = " "; /* 20 spaces */
5536 int len;
5538 trace_seq_printf(s, " %s: ", event->name);
5540 /* Space out the event names evenly. */
5541 len = strlen(event->name);
5542 if (len < 20)
5543 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5545 tep_event_info(s, event, record);
5548 void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
5549 struct tep_record *record, bool use_trace_clock)
5551 struct event_format *event;
5553 event = tep_find_event_by_record(pevent, record);
5554 if (!event) {
5555 int i;
5556 int type = trace_parse_common_type(pevent, record->data);
5558 do_warning("ug! no event found for type %d", type);
5559 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5560 for (i = 0; i < record->size; i++)
5561 trace_seq_printf(s, " %02x",
5562 ((unsigned char *)record->data)[i]);
5563 return;
5566 tep_print_event_task(pevent, s, event, record);
5567 tep_print_event_time(pevent, s, event, record, use_trace_clock);
5568 tep_print_event_data(pevent, s, event, record);
5571 static int events_id_cmp(const void *a, const void *b)
5573 struct event_format * const * ea = a;
5574 struct event_format * const * eb = b;
5576 if ((*ea)->id < (*eb)->id)
5577 return -1;
5579 if ((*ea)->id > (*eb)->id)
5580 return 1;
5582 return 0;
5585 static int events_name_cmp(const void *a, const void *b)
5587 struct event_format * const * ea = a;
5588 struct event_format * const * eb = b;
5589 int res;
5591 res = strcmp((*ea)->name, (*eb)->name);
5592 if (res)
5593 return res;
5595 res = strcmp((*ea)->system, (*eb)->system);
5596 if (res)
5597 return res;
5599 return events_id_cmp(a, b);
5602 static int events_system_cmp(const void *a, const void *b)
5604 struct event_format * const * ea = a;
5605 struct event_format * const * eb = b;
5606 int res;
5608 res = strcmp((*ea)->system, (*eb)->system);
5609 if (res)
5610 return res;
5612 res = strcmp((*ea)->name, (*eb)->name);
5613 if (res)
5614 return res;
5616 return events_id_cmp(a, b);
5619 struct event_format **tep_list_events(struct tep_handle *pevent, enum event_sort_type sort_type)
5621 struct event_format **events;
5622 int (*sort)(const void *a, const void *b);
5624 events = pevent->sort_events;
5626 if (events && pevent->last_type == sort_type)
5627 return events;
5629 if (!events) {
5630 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5631 if (!events)
5632 return NULL;
5634 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5635 events[pevent->nr_events] = NULL;
5637 pevent->sort_events = events;
5639 /* the internal events are sorted by id */
5640 if (sort_type == EVENT_SORT_ID) {
5641 pevent->last_type = sort_type;
5642 return events;
5646 switch (sort_type) {
5647 case EVENT_SORT_ID:
5648 sort = events_id_cmp;
5649 break;
5650 case EVENT_SORT_NAME:
5651 sort = events_name_cmp;
5652 break;
5653 case EVENT_SORT_SYSTEM:
5654 sort = events_system_cmp;
5655 break;
5656 default:
5657 return events;
5660 qsort(events, pevent->nr_events, sizeof(*events), sort);
5661 pevent->last_type = sort_type;
5663 return events;
5666 static struct format_field **
5667 get_event_fields(const char *type, const char *name,
5668 int count, struct format_field *list)
5670 struct format_field **fields;
5671 struct format_field *field;
5672 int i = 0;
5674 fields = malloc(sizeof(*fields) * (count + 1));
5675 if (!fields)
5676 return NULL;
5678 for (field = list; field; field = field->next) {
5679 fields[i++] = field;
5680 if (i == count + 1) {
5681 do_warning("event %s has more %s fields than specified",
5682 name, type);
5683 i--;
5684 break;
5688 if (i != count)
5689 do_warning("event %s has less %s fields than specified",
5690 name, type);
5692 fields[i] = NULL;
5694 return fields;
5698 * tep_event_common_fields - return a list of common fields for an event
5699 * @event: the event to return the common fields of.
5701 * Returns an allocated array of fields. The last item in the array is NULL.
5702 * The array must be freed with free().
5704 struct format_field **tep_event_common_fields(struct event_format *event)
5706 return get_event_fields("common", event->name,
5707 event->format.nr_common,
5708 event->format.common_fields);
5712 * tep_event_fields - return a list of event specific fields for an event
5713 * @event: the event to return the fields of.
5715 * Returns an allocated array of fields. The last item in the array is NULL.
5716 * The array must be freed with free().
5718 struct format_field **tep_event_fields(struct event_format *event)
5720 return get_event_fields("event", event->name,
5721 event->format.nr_fields,
5722 event->format.fields);
5725 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5727 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5728 if (field->next) {
5729 trace_seq_puts(s, ", ");
5730 print_fields(s, field->next);
5734 /* for debugging */
5735 static void print_args(struct print_arg *args)
5737 int print_paren = 1;
5738 struct trace_seq s;
5740 switch (args->type) {
5741 case PRINT_NULL:
5742 printf("null");
5743 break;
5744 case PRINT_ATOM:
5745 printf("%s", args->atom.atom);
5746 break;
5747 case PRINT_FIELD:
5748 printf("REC->%s", args->field.name);
5749 break;
5750 case PRINT_FLAGS:
5751 printf("__print_flags(");
5752 print_args(args->flags.field);
5753 printf(", %s, ", args->flags.delim);
5754 trace_seq_init(&s);
5755 print_fields(&s, args->flags.flags);
5756 trace_seq_do_printf(&s);
5757 trace_seq_destroy(&s);
5758 printf(")");
5759 break;
5760 case PRINT_SYMBOL:
5761 printf("__print_symbolic(");
5762 print_args(args->symbol.field);
5763 printf(", ");
5764 trace_seq_init(&s);
5765 print_fields(&s, args->symbol.symbols);
5766 trace_seq_do_printf(&s);
5767 trace_seq_destroy(&s);
5768 printf(")");
5769 break;
5770 case PRINT_HEX:
5771 printf("__print_hex(");
5772 print_args(args->hex.field);
5773 printf(", ");
5774 print_args(args->hex.size);
5775 printf(")");
5776 break;
5777 case PRINT_HEX_STR:
5778 printf("__print_hex_str(");
5779 print_args(args->hex.field);
5780 printf(", ");
5781 print_args(args->hex.size);
5782 printf(")");
5783 break;
5784 case PRINT_INT_ARRAY:
5785 printf("__print_array(");
5786 print_args(args->int_array.field);
5787 printf(", ");
5788 print_args(args->int_array.count);
5789 printf(", ");
5790 print_args(args->int_array.el_size);
5791 printf(")");
5792 break;
5793 case PRINT_STRING:
5794 case PRINT_BSTRING:
5795 printf("__get_str(%s)", args->string.string);
5796 break;
5797 case PRINT_BITMASK:
5798 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5799 break;
5800 case PRINT_TYPE:
5801 printf("(%s)", args->typecast.type);
5802 print_args(args->typecast.item);
5803 break;
5804 case PRINT_OP:
5805 if (strcmp(args->op.op, ":") == 0)
5806 print_paren = 0;
5807 if (print_paren)
5808 printf("(");
5809 print_args(args->op.left);
5810 printf(" %s ", args->op.op);
5811 print_args(args->op.right);
5812 if (print_paren)
5813 printf(")");
5814 break;
5815 default:
5816 /* we should warn... */
5817 return;
5819 if (args->next) {
5820 printf("\n");
5821 print_args(args->next);
5825 static void parse_header_field(const char *field,
5826 int *offset, int *size, int mandatory)
5828 unsigned long long save_input_buf_ptr;
5829 unsigned long long save_input_buf_siz;
5830 char *token;
5831 int type;
5833 save_input_buf_ptr = input_buf_ptr;
5834 save_input_buf_siz = input_buf_siz;
5836 if (read_expected(EVENT_ITEM, "field") < 0)
5837 return;
5838 if (read_expected(EVENT_OP, ":") < 0)
5839 return;
5841 /* type */
5842 if (read_expect_type(EVENT_ITEM, &token) < 0)
5843 goto fail;
5844 free_token(token);
5847 * If this is not a mandatory field, then test it first.
5849 if (mandatory) {
5850 if (read_expected(EVENT_ITEM, field) < 0)
5851 return;
5852 } else {
5853 if (read_expect_type(EVENT_ITEM, &token) < 0)
5854 goto fail;
5855 if (strcmp(token, field) != 0)
5856 goto discard;
5857 free_token(token);
5860 if (read_expected(EVENT_OP, ";") < 0)
5861 return;
5862 if (read_expected(EVENT_ITEM, "offset") < 0)
5863 return;
5864 if (read_expected(EVENT_OP, ":") < 0)
5865 return;
5866 if (read_expect_type(EVENT_ITEM, &token) < 0)
5867 goto fail;
5868 *offset = atoi(token);
5869 free_token(token);
5870 if (read_expected(EVENT_OP, ";") < 0)
5871 return;
5872 if (read_expected(EVENT_ITEM, "size") < 0)
5873 return;
5874 if (read_expected(EVENT_OP, ":") < 0)
5875 return;
5876 if (read_expect_type(EVENT_ITEM, &token) < 0)
5877 goto fail;
5878 *size = atoi(token);
5879 free_token(token);
5880 if (read_expected(EVENT_OP, ";") < 0)
5881 return;
5882 type = read_token(&token);
5883 if (type != EVENT_NEWLINE) {
5884 /* newer versions of the kernel have a "signed" type */
5885 if (type != EVENT_ITEM)
5886 goto fail;
5888 if (strcmp(token, "signed") != 0)
5889 goto fail;
5891 free_token(token);
5893 if (read_expected(EVENT_OP, ":") < 0)
5894 return;
5896 if (read_expect_type(EVENT_ITEM, &token))
5897 goto fail;
5899 free_token(token);
5900 if (read_expected(EVENT_OP, ";") < 0)
5901 return;
5903 if (read_expect_type(EVENT_NEWLINE, &token))
5904 goto fail;
5906 fail:
5907 free_token(token);
5908 return;
5910 discard:
5911 input_buf_ptr = save_input_buf_ptr;
5912 input_buf_siz = save_input_buf_siz;
5913 *offset = 0;
5914 *size = 0;
5915 free_token(token);
5919 * tep_parse_header_page - parse the data stored in the header page
5920 * @pevent: the handle to the pevent
5921 * @buf: the buffer storing the header page format string
5922 * @size: the size of @buf
5923 * @long_size: the long size to use if there is no header
5925 * This parses the header page format for information on the
5926 * ring buffer used. The @buf should be copied from
5928 * /sys/kernel/debug/tracing/events/header_page
5930 int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
5931 int long_size)
5933 int ignore;
5935 if (!size) {
5937 * Old kernels did not have header page info.
5938 * Sorry but we just use what we find here in user space.
5940 pevent->header_page_ts_size = sizeof(long long);
5941 pevent->header_page_size_size = long_size;
5942 pevent->header_page_data_offset = sizeof(long long) + long_size;
5943 pevent->old_format = 1;
5944 return -1;
5946 init_input_buf(buf, size);
5948 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5949 &pevent->header_page_ts_size, 1);
5950 parse_header_field("commit", &pevent->header_page_size_offset,
5951 &pevent->header_page_size_size, 1);
5952 parse_header_field("overwrite", &pevent->header_page_overwrite,
5953 &ignore, 0);
5954 parse_header_field("data", &pevent->header_page_data_offset,
5955 &pevent->header_page_data_size, 1);
5957 return 0;
5960 static int event_matches(struct event_format *event,
5961 int id, const char *sys_name,
5962 const char *event_name)
5964 if (id >= 0 && id != event->id)
5965 return 0;
5967 if (event_name && (strcmp(event_name, event->name) != 0))
5968 return 0;
5970 if (sys_name && (strcmp(sys_name, event->system) != 0))
5971 return 0;
5973 return 1;
5976 static void free_handler(struct event_handler *handle)
5978 free((void *)handle->sys_name);
5979 free((void *)handle->event_name);
5980 free(handle);
5983 static int find_event_handle(struct tep_handle *pevent, struct event_format *event)
5985 struct event_handler *handle, **next;
5987 for (next = &pevent->handlers; *next;
5988 next = &(*next)->next) {
5989 handle = *next;
5990 if (event_matches(event, handle->id,
5991 handle->sys_name,
5992 handle->event_name))
5993 break;
5996 if (!(*next))
5997 return 0;
5999 pr_stat("overriding event (%d) %s:%s with new print handler",
6000 event->id, event->system, event->name);
6002 event->handler = handle->func;
6003 event->context = handle->context;
6005 *next = handle->next;
6006 free_handler(handle);
6008 return 1;
6012 * __tep_parse_format - parse the event format
6013 * @buf: the buffer storing the event format string
6014 * @size: the size of @buf
6015 * @sys: the system the event belongs to
6017 * This parses the event format and creates an event structure
6018 * to quickly parse raw data for a given event.
6020 * These files currently come from:
6022 * /sys/kernel/debug/tracing/events/.../.../format
6024 enum tep_errno __tep_parse_format(struct event_format **eventp,
6025 struct tep_handle *pevent, const char *buf,
6026 unsigned long size, const char *sys)
6028 struct event_format *event;
6029 int ret;
6031 init_input_buf(buf, size);
6033 *eventp = event = alloc_event();
6034 if (!event)
6035 return TEP_ERRNO__MEM_ALLOC_FAILED;
6037 event->name = event_read_name();
6038 if (!event->name) {
6039 /* Bad event? */
6040 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6041 goto event_alloc_failed;
6044 if (strcmp(sys, "ftrace") == 0) {
6045 event->flags |= EVENT_FL_ISFTRACE;
6047 if (strcmp(event->name, "bprint") == 0)
6048 event->flags |= EVENT_FL_ISBPRINT;
6051 event->id = event_read_id();
6052 if (event->id < 0) {
6053 ret = TEP_ERRNO__READ_ID_FAILED;
6055 * This isn't an allocation error actually.
6056 * But as the ID is critical, just bail out.
6058 goto event_alloc_failed;
6061 event->system = strdup(sys);
6062 if (!event->system) {
6063 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6064 goto event_alloc_failed;
6067 /* Add pevent to event so that it can be referenced */
6068 event->pevent = pevent;
6070 ret = event_read_format(event);
6071 if (ret < 0) {
6072 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6073 goto event_parse_failed;
6077 * If the event has an override, don't print warnings if the event
6078 * print format fails to parse.
6080 if (pevent && find_event_handle(pevent, event))
6081 show_warning = 0;
6083 ret = event_read_print(event);
6084 show_warning = 1;
6086 if (ret < 0) {
6087 ret = TEP_ERRNO__READ_PRINT_FAILED;
6088 goto event_parse_failed;
6091 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6092 struct format_field *field;
6093 struct print_arg *arg, **list;
6095 /* old ftrace had no args */
6096 list = &event->print_fmt.args;
6097 for (field = event->format.fields; field; field = field->next) {
6098 arg = alloc_arg();
6099 if (!arg) {
6100 event->flags |= EVENT_FL_FAILED;
6101 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6103 arg->type = PRINT_FIELD;
6104 arg->field.name = strdup(field->name);
6105 if (!arg->field.name) {
6106 event->flags |= EVENT_FL_FAILED;
6107 free_arg(arg);
6108 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6110 arg->field.field = field;
6111 *list = arg;
6112 list = &arg->next;
6114 return 0;
6117 return 0;
6119 event_parse_failed:
6120 event->flags |= EVENT_FL_FAILED;
6121 return ret;
6123 event_alloc_failed:
6124 free(event->system);
6125 free(event->name);
6126 free(event);
6127 *eventp = NULL;
6128 return ret;
6131 static enum tep_errno
6132 __parse_event(struct tep_handle *pevent,
6133 struct event_format **eventp,
6134 const char *buf, unsigned long size,
6135 const char *sys)
6137 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
6138 struct event_format *event = *eventp;
6140 if (event == NULL)
6141 return ret;
6143 if (pevent && add_event(pevent, event)) {
6144 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6145 goto event_add_failed;
6148 #define PRINT_ARGS 0
6149 if (PRINT_ARGS && event->print_fmt.args)
6150 print_args(event->print_fmt.args);
6152 return 0;
6154 event_add_failed:
6155 tep_free_format(event);
6156 return ret;
6160 * tep_parse_format - parse the event format
6161 * @pevent: the handle to the pevent
6162 * @eventp: returned format
6163 * @buf: the buffer storing the event format string
6164 * @size: the size of @buf
6165 * @sys: the system the event belongs to
6167 * This parses the event format and creates an event structure
6168 * to quickly parse raw data for a given event.
6170 * These files currently come from:
6172 * /sys/kernel/debug/tracing/events/.../.../format
6174 enum tep_errno tep_parse_format(struct tep_handle *pevent,
6175 struct event_format **eventp,
6176 const char *buf,
6177 unsigned long size, const char *sys)
6179 return __parse_event(pevent, eventp, buf, size, sys);
6183 * tep_parse_event - parse the event format
6184 * @pevent: the handle to the pevent
6185 * @buf: the buffer storing the event format string
6186 * @size: the size of @buf
6187 * @sys: the system the event belongs to
6189 * This parses the event format and creates an event structure
6190 * to quickly parse raw data for a given event.
6192 * These files currently come from:
6194 * /sys/kernel/debug/tracing/events/.../.../format
6196 enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
6197 unsigned long size, const char *sys)
6199 struct event_format *event = NULL;
6200 return __parse_event(pevent, &event, buf, size, sys);
6203 #undef _PE
6204 #define _PE(code, str) str
6205 static const char * const tep_error_str[] = {
6206 TEP_ERRORS
6208 #undef _PE
6210 int tep_strerror(struct tep_handle *pevent __maybe_unused,
6211 enum tep_errno errnum, char *buf, size_t buflen)
6213 int idx;
6214 const char *msg;
6216 if (errnum >= 0) {
6217 str_error_r(errnum, buf, buflen);
6218 return 0;
6221 if (errnum <= __TEP_ERRNO__START ||
6222 errnum >= __TEP_ERRNO__END)
6223 return -1;
6225 idx = errnum - __TEP_ERRNO__START - 1;
6226 msg = tep_error_str[idx];
6227 snprintf(buf, buflen, "%s", msg);
6229 return 0;
6232 int get_field_val(struct trace_seq *s, struct format_field *field,
6233 const char *name, struct tep_record *record,
6234 unsigned long long *val, int err)
6236 if (!field) {
6237 if (err)
6238 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6239 return -1;
6242 if (tep_read_number_field(field, record->data, val)) {
6243 if (err)
6244 trace_seq_printf(s, " %s=INVALID", name);
6245 return -1;
6248 return 0;
6252 * tep_get_field_raw - return the raw pointer into the data field
6253 * @s: The seq to print to on error
6254 * @event: the event that the field is for
6255 * @name: The name of the field
6256 * @record: The record with the field name.
6257 * @len: place to store the field length.
6258 * @err: print default error if failed.
6260 * Returns a pointer into record->data of the field and places
6261 * the length of the field in @len.
6263 * On failure, it returns NULL.
6265 void *tep_get_field_raw(struct trace_seq *s, struct event_format *event,
6266 const char *name, struct tep_record *record,
6267 int *len, int err)
6269 struct format_field *field;
6270 void *data = record->data;
6271 unsigned offset;
6272 int dummy;
6274 if (!event)
6275 return NULL;
6277 field = tep_find_field(event, name);
6279 if (!field) {
6280 if (err)
6281 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6282 return NULL;
6285 /* Allow @len to be NULL */
6286 if (!len)
6287 len = &dummy;
6289 offset = field->offset;
6290 if (field->flags & FIELD_IS_DYNAMIC) {
6291 offset = tep_read_number(event->pevent,
6292 data + offset, field->size);
6293 *len = offset >> 16;
6294 offset &= 0xffff;
6295 } else
6296 *len = field->size;
6298 return data + offset;
6302 * tep_get_field_val - find a field and return its value
6303 * @s: The seq to print to on error
6304 * @event: the event that the field is for
6305 * @name: The name of the field
6306 * @record: The record with the field name.
6307 * @val: place to store the value of the field.
6308 * @err: print default error if failed.
6310 * Returns 0 on success -1 on field not found.
6312 int tep_get_field_val(struct trace_seq *s, struct event_format *event,
6313 const char *name, struct tep_record *record,
6314 unsigned long long *val, int err)
6316 struct format_field *field;
6318 if (!event)
6319 return -1;
6321 field = tep_find_field(event, name);
6323 return get_field_val(s, field, name, record, val, err);
6327 * tep_get_common_field_val - find a common field and return its value
6328 * @s: The seq to print to on error
6329 * @event: the event that the field is for
6330 * @name: The name of the field
6331 * @record: The record with the field name.
6332 * @val: place to store the value of the field.
6333 * @err: print default error if failed.
6335 * Returns 0 on success -1 on field not found.
6337 int tep_get_common_field_val(struct trace_seq *s, struct event_format *event,
6338 const char *name, struct tep_record *record,
6339 unsigned long long *val, int err)
6341 struct format_field *field;
6343 if (!event)
6344 return -1;
6346 field = tep_find_common_field(event, name);
6348 return get_field_val(s, field, name, record, val, err);
6352 * tep_get_any_field_val - find a any field and return its value
6353 * @s: The seq to print to on error
6354 * @event: the event that the field is for
6355 * @name: The name of the field
6356 * @record: The record with the field name.
6357 * @val: place to store the value of the field.
6358 * @err: print default error if failed.
6360 * Returns 0 on success -1 on field not found.
6362 int tep_get_any_field_val(struct trace_seq *s, struct event_format *event,
6363 const char *name, struct tep_record *record,
6364 unsigned long long *val, int err)
6366 struct format_field *field;
6368 if (!event)
6369 return -1;
6371 field = tep_find_any_field(event, name);
6373 return get_field_val(s, field, name, record, val, err);
6377 * tep_print_num_field - print a field and a format
6378 * @s: The seq to print to
6379 * @fmt: The printf format to print the field with.
6380 * @event: the event that the field is for
6381 * @name: The name of the field
6382 * @record: The record with the field name.
6383 * @err: print default error if failed.
6385 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6387 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6388 struct event_format *event, const char *name,
6389 struct tep_record *record, int err)
6391 struct format_field *field = tep_find_field(event, name);
6392 unsigned long long val;
6394 if (!field)
6395 goto failed;
6397 if (tep_read_number_field(field, record->data, &val))
6398 goto failed;
6400 return trace_seq_printf(s, fmt, val);
6402 failed:
6403 if (err)
6404 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6405 return -1;
6409 * tep_print_func_field - print a field and a format for function pointers
6410 * @s: The seq to print to
6411 * @fmt: The printf format to print the field with.
6412 * @event: the event that the field is for
6413 * @name: The name of the field
6414 * @record: The record with the field name.
6415 * @err: print default error if failed.
6417 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6419 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6420 struct event_format *event, const char *name,
6421 struct tep_record *record, int err)
6423 struct format_field *field = tep_find_field(event, name);
6424 struct tep_handle *pevent = event->pevent;
6425 unsigned long long val;
6426 struct func_map *func;
6427 char tmp[128];
6429 if (!field)
6430 goto failed;
6432 if (tep_read_number_field(field, record->data, &val))
6433 goto failed;
6435 func = find_func(pevent, val);
6437 if (func)
6438 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6439 else
6440 sprintf(tmp, "0x%08llx", val);
6442 return trace_seq_printf(s, fmt, tmp);
6444 failed:
6445 if (err)
6446 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6447 return -1;
6450 static void free_func_handle(struct tep_function_handler *func)
6452 struct func_params *params;
6454 free(func->name);
6456 while (func->params) {
6457 params = func->params;
6458 func->params = params->next;
6459 free(params);
6462 free(func);
6466 * tep_register_print_function - register a helper function
6467 * @pevent: the handle to the pevent
6468 * @func: the function to process the helper function
6469 * @ret_type: the return type of the helper function
6470 * @name: the name of the helper function
6471 * @parameters: A list of enum tep_func_arg_type
6473 * Some events may have helper functions in the print format arguments.
6474 * This allows a plugin to dynamically create a way to process one
6475 * of these functions.
6477 * The @parameters is a variable list of tep_func_arg_type enums that
6478 * must end with TEP_FUNC_ARG_VOID.
6480 int tep_register_print_function(struct tep_handle *pevent,
6481 tep_func_handler func,
6482 enum tep_func_arg_type ret_type,
6483 char *name, ...)
6485 struct tep_function_handler *func_handle;
6486 struct func_params **next_param;
6487 struct func_params *param;
6488 enum tep_func_arg_type type;
6489 va_list ap;
6490 int ret;
6492 func_handle = find_func_handler(pevent, name);
6493 if (func_handle) {
6495 * This is most like caused by the users own
6496 * plugins updating the function. This overrides the
6497 * system defaults.
6499 pr_stat("override of function helper '%s'", name);
6500 remove_func_handler(pevent, name);
6503 func_handle = calloc(1, sizeof(*func_handle));
6504 if (!func_handle) {
6505 do_warning("Failed to allocate function handler");
6506 return TEP_ERRNO__MEM_ALLOC_FAILED;
6509 func_handle->ret_type = ret_type;
6510 func_handle->name = strdup(name);
6511 func_handle->func = func;
6512 if (!func_handle->name) {
6513 do_warning("Failed to allocate function name");
6514 free(func_handle);
6515 return TEP_ERRNO__MEM_ALLOC_FAILED;
6518 next_param = &(func_handle->params);
6519 va_start(ap, name);
6520 for (;;) {
6521 type = va_arg(ap, enum tep_func_arg_type);
6522 if (type == TEP_FUNC_ARG_VOID)
6523 break;
6525 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6526 do_warning("Invalid argument type %d", type);
6527 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6528 goto out_free;
6531 param = malloc(sizeof(*param));
6532 if (!param) {
6533 do_warning("Failed to allocate function param");
6534 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6535 goto out_free;
6537 param->type = type;
6538 param->next = NULL;
6540 *next_param = param;
6541 next_param = &(param->next);
6543 func_handle->nr_args++;
6545 va_end(ap);
6547 func_handle->next = pevent->func_handlers;
6548 pevent->func_handlers = func_handle;
6550 return 0;
6551 out_free:
6552 va_end(ap);
6553 free_func_handle(func_handle);
6554 return ret;
6558 * tep_unregister_print_function - unregister a helper function
6559 * @pevent: the handle to the pevent
6560 * @func: the function to process the helper function
6561 * @name: the name of the helper function
6563 * This function removes existing print handler for function @name.
6565 * Returns 0 if the handler was removed successully, -1 otherwise.
6567 int tep_unregister_print_function(struct tep_handle *pevent,
6568 tep_func_handler func, char *name)
6570 struct tep_function_handler *func_handle;
6572 func_handle = find_func_handler(pevent, name);
6573 if (func_handle && func_handle->func == func) {
6574 remove_func_handler(pevent, name);
6575 return 0;
6577 return -1;
6580 static struct event_format *search_event(struct tep_handle *pevent, int id,
6581 const char *sys_name,
6582 const char *event_name)
6584 struct event_format *event;
6586 if (id >= 0) {
6587 /* search by id */
6588 event = tep_find_event(pevent, id);
6589 if (!event)
6590 return NULL;
6591 if (event_name && (strcmp(event_name, event->name) != 0))
6592 return NULL;
6593 if (sys_name && (strcmp(sys_name, event->system) != 0))
6594 return NULL;
6595 } else {
6596 event = tep_find_event_by_name(pevent, sys_name, event_name);
6597 if (!event)
6598 return NULL;
6600 return event;
6604 * tep_register_event_handler - register a way to parse an event
6605 * @pevent: the handle to the pevent
6606 * @id: the id of the event to register
6607 * @sys_name: the system name the event belongs to
6608 * @event_name: the name of the event
6609 * @func: the function to call to parse the event information
6610 * @context: the data to be passed to @func
6612 * This function allows a developer to override the parsing of
6613 * a given event. If for some reason the default print format
6614 * is not sufficient, this function will register a function
6615 * for an event to be used to parse the data instead.
6617 * If @id is >= 0, then it is used to find the event.
6618 * else @sys_name and @event_name are used.
6620 int tep_register_event_handler(struct tep_handle *pevent, int id,
6621 const char *sys_name, const char *event_name,
6622 tep_event_handler_func func, void *context)
6624 struct event_format *event;
6625 struct event_handler *handle;
6627 event = search_event(pevent, id, sys_name, event_name);
6628 if (event == NULL)
6629 goto not_found;
6631 pr_stat("overriding event (%d) %s:%s with new print handler",
6632 event->id, event->system, event->name);
6634 event->handler = func;
6635 event->context = context;
6636 return 0;
6638 not_found:
6639 /* Save for later use. */
6640 handle = calloc(1, sizeof(*handle));
6641 if (!handle) {
6642 do_warning("Failed to allocate event handler");
6643 return TEP_ERRNO__MEM_ALLOC_FAILED;
6646 handle->id = id;
6647 if (event_name)
6648 handle->event_name = strdup(event_name);
6649 if (sys_name)
6650 handle->sys_name = strdup(sys_name);
6652 if ((event_name && !handle->event_name) ||
6653 (sys_name && !handle->sys_name)) {
6654 do_warning("Failed to allocate event/sys name");
6655 free((void *)handle->event_name);
6656 free((void *)handle->sys_name);
6657 free(handle);
6658 return TEP_ERRNO__MEM_ALLOC_FAILED;
6661 handle->func = func;
6662 handle->next = pevent->handlers;
6663 pevent->handlers = handle;
6664 handle->context = context;
6666 return -1;
6669 static int handle_matches(struct event_handler *handler, int id,
6670 const char *sys_name, const char *event_name,
6671 tep_event_handler_func func, void *context)
6673 if (id >= 0 && id != handler->id)
6674 return 0;
6676 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6677 return 0;
6679 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6680 return 0;
6682 if (func != handler->func || context != handler->context)
6683 return 0;
6685 return 1;
6689 * tep_unregister_event_handler - unregister an existing event handler
6690 * @pevent: the handle to the pevent
6691 * @id: the id of the event to unregister
6692 * @sys_name: the system name the handler belongs to
6693 * @event_name: the name of the event handler
6694 * @func: the function to call to parse the event information
6695 * @context: the data to be passed to @func
6697 * This function removes existing event handler (parser).
6699 * If @id is >= 0, then it is used to find the event.
6700 * else @sys_name and @event_name are used.
6702 * Returns 0 if handler was removed successfully, -1 if event was not found.
6704 int tep_unregister_event_handler(struct tep_handle *pevent, int id,
6705 const char *sys_name, const char *event_name,
6706 tep_event_handler_func func, void *context)
6708 struct event_format *event;
6709 struct event_handler *handle;
6710 struct event_handler **next;
6712 event = search_event(pevent, id, sys_name, event_name);
6713 if (event == NULL)
6714 goto not_found;
6716 if (event->handler == func && event->context == context) {
6717 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6718 event->id, event->system, event->name);
6720 event->handler = NULL;
6721 event->context = NULL;
6722 return 0;
6725 not_found:
6726 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6727 handle = *next;
6728 if (handle_matches(handle, id, sys_name, event_name,
6729 func, context))
6730 break;
6733 if (!(*next))
6734 return -1;
6736 *next = handle->next;
6737 free_handler(handle);
6739 return 0;
6743 * tep_alloc - create a pevent handle
6745 struct tep_handle *tep_alloc(void)
6747 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
6749 if (pevent)
6750 pevent->ref_count = 1;
6752 return pevent;
6755 void tep_ref(struct tep_handle *pevent)
6757 pevent->ref_count++;
6760 void tep_free_format_field(struct format_field *field)
6762 free(field->type);
6763 if (field->alias != field->name)
6764 free(field->alias);
6765 free(field->name);
6766 free(field);
6769 static void free_format_fields(struct format_field *field)
6771 struct format_field *next;
6773 while (field) {
6774 next = field->next;
6775 tep_free_format_field(field);
6776 field = next;
6780 static void free_formats(struct format *format)
6782 free_format_fields(format->common_fields);
6783 free_format_fields(format->fields);
6786 void tep_free_format(struct event_format *event)
6788 free(event->name);
6789 free(event->system);
6791 free_formats(&event->format);
6793 free(event->print_fmt.format);
6794 free_args(event->print_fmt.args);
6796 free(event);
6800 * tep_free - free a pevent handle
6801 * @pevent: the pevent handle to free
6803 void tep_free(struct tep_handle *pevent)
6805 struct cmdline_list *cmdlist, *cmdnext;
6806 struct func_list *funclist, *funcnext;
6807 struct printk_list *printklist, *printknext;
6808 struct tep_function_handler *func_handler;
6809 struct event_handler *handle;
6810 int i;
6812 if (!pevent)
6813 return;
6815 cmdlist = pevent->cmdlist;
6816 funclist = pevent->funclist;
6817 printklist = pevent->printklist;
6819 pevent->ref_count--;
6820 if (pevent->ref_count)
6821 return;
6823 if (pevent->cmdlines) {
6824 for (i = 0; i < pevent->cmdline_count; i++)
6825 free(pevent->cmdlines[i].comm);
6826 free(pevent->cmdlines);
6829 while (cmdlist) {
6830 cmdnext = cmdlist->next;
6831 free(cmdlist->comm);
6832 free(cmdlist);
6833 cmdlist = cmdnext;
6836 if (pevent->func_map) {
6837 for (i = 0; i < (int)pevent->func_count; i++) {
6838 free(pevent->func_map[i].func);
6839 free(pevent->func_map[i].mod);
6841 free(pevent->func_map);
6844 while (funclist) {
6845 funcnext = funclist->next;
6846 free(funclist->func);
6847 free(funclist->mod);
6848 free(funclist);
6849 funclist = funcnext;
6852 while (pevent->func_handlers) {
6853 func_handler = pevent->func_handlers;
6854 pevent->func_handlers = func_handler->next;
6855 free_func_handle(func_handler);
6858 if (pevent->printk_map) {
6859 for (i = 0; i < (int)pevent->printk_count; i++)
6860 free(pevent->printk_map[i].printk);
6861 free(pevent->printk_map);
6864 while (printklist) {
6865 printknext = printklist->next;
6866 free(printklist->printk);
6867 free(printklist);
6868 printklist = printknext;
6871 for (i = 0; i < pevent->nr_events; i++)
6872 tep_free_format(pevent->events[i]);
6874 while (pevent->handlers) {
6875 handle = pevent->handlers;
6876 pevent->handlers = handle->next;
6877 free_handler(handle);
6880 free(pevent->trace_clock);
6881 free(pevent->events);
6882 free(pevent->sort_events);
6883 free(pevent->func_resolver);
6885 free(pevent);
6888 void tep_unref(struct tep_handle *pevent)
6890 tep_free(pevent);