• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
3  *
4  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation;
8  * version 2.1 of the License (not later!)
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not,  see <http://www.gnu.org/licenses>
17  *
18  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19  *
20  *  The parts for function graph printing was taken and modified from the
21  *  Linux Kernel that were written by
22  *    - Copyright (C) 2009  Frederic Weisbecker,
23  *  Frederic Weisbecker gave his permission to relicense the code to
24  *  the Lesser General Public License.
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <limits.h>
34 
35 #include "event-parse.h"
36 #include "event-utils.h"
37 
38 static const char *input_buf;
39 static unsigned long long input_buf_ptr;
40 static unsigned long long input_buf_siz;
41 
42 static int is_flag_field;
43 static int is_symbolic_field;
44 
45 static int show_warning = 1;
46 
47 #define do_warning(fmt, ...)				\
48 	do {						\
49 		if (show_warning)			\
50 			warning(fmt, ##__VA_ARGS__);	\
51 	} while (0)
52 
init_input_buf(const char * buf,unsigned long long size)53 static void init_input_buf(const char *buf, unsigned long long size)
54 {
55 	input_buf = buf;
56 	input_buf_siz = size;
57 	input_buf_ptr = 0;
58 }
59 
pevent_get_input_buf(void)60 const char *pevent_get_input_buf(void)
61 {
62 	return input_buf;
63 }
64 
pevent_get_input_buf_ptr(void)65 unsigned long long pevent_get_input_buf_ptr(void)
66 {
67 	return input_buf_ptr;
68 }
69 
70 struct event_handler {
71 	struct event_handler		*next;
72 	int				id;
73 	const char			*sys_name;
74 	const char			*event_name;
75 	pevent_event_handler_func	func;
76 	void				*context;
77 };
78 
79 struct pevent_func_params {
80 	struct pevent_func_params	*next;
81 	enum pevent_func_arg_type	type;
82 };
83 
84 struct pevent_function_handler {
85 	struct pevent_function_handler	*next;
86 	enum pevent_func_arg_type	ret_type;
87 	char				*name;
88 	pevent_func_handler		func;
89 	struct pevent_func_params	*params;
90 	int				nr_args;
91 };
92 
93 static unsigned long long
94 process_defined_func(struct trace_seq *s, void *data, int size,
95 		     struct event_format *event, struct print_arg *arg);
96 
97 static void free_func_handle(struct pevent_function_handler *func);
98 
99 /**
100  * pevent_buffer_init - init buffer for parsing
101  * @buf: buffer to parse
102  * @size: the size of the buffer
103  *
104  * For use with pevent_read_token(), this initializes the internal
105  * buffer that pevent_read_token() will parse.
106  */
pevent_buffer_init(const char * buf,unsigned long long size)107 void pevent_buffer_init(const char *buf, unsigned long long size)
108 {
109 	init_input_buf(buf, size);
110 }
111 
breakpoint(void)112 void breakpoint(void)
113 {
114 	static int x;
115 	x++;
116 }
117 
alloc_arg(void)118 struct print_arg *alloc_arg(void)
119 {
120 	return calloc(1, sizeof(struct print_arg));
121 }
122 
123 struct cmdline {
124 	char *comm;
125 	int pid;
126 };
127 
cmdline_cmp(const void * a,const void * b)128 static int cmdline_cmp(const void *a, const void *b)
129 {
130 	const struct cmdline *ca = a;
131 	const struct cmdline *cb = b;
132 
133 	if (ca->pid < cb->pid)
134 		return -1;
135 	if (ca->pid > cb->pid)
136 		return 1;
137 
138 	return 0;
139 }
140 
141 struct cmdline_list {
142 	struct cmdline_list	*next;
143 	char			*comm;
144 	int			pid;
145 };
146 
cmdline_init(struct pevent * pevent)147 static int cmdline_init(struct pevent *pevent)
148 {
149 	struct cmdline_list *cmdlist = pevent->cmdlist;
150 	struct cmdline_list *item;
151 	struct cmdline *cmdlines;
152 	int i;
153 
154 	cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
155 	if (!cmdlines)
156 		return -1;
157 
158 	i = 0;
159 	while (cmdlist) {
160 		cmdlines[i].pid = cmdlist->pid;
161 		cmdlines[i].comm = cmdlist->comm;
162 		i++;
163 		item = cmdlist;
164 		cmdlist = cmdlist->next;
165 		free(item);
166 	}
167 
168 	qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
169 
170 	pevent->cmdlines = cmdlines;
171 	pevent->cmdlist = NULL;
172 
173 	return 0;
174 }
175 
find_cmdline(struct pevent * pevent,int pid)176 static const char *find_cmdline(struct pevent *pevent, int pid)
177 {
178 	const struct cmdline *comm;
179 	struct cmdline key;
180 
181 	if (!pid)
182 		return "<idle>";
183 
184 	if (!pevent->cmdlines && cmdline_init(pevent))
185 		return "<not enough memory for cmdlines!>";
186 
187 	key.pid = pid;
188 
189 	comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
190 		       sizeof(*pevent->cmdlines), cmdline_cmp);
191 
192 	if (comm)
193 		return comm->comm;
194 	return "<...>";
195 }
196 
197 /**
198  * pevent_pid_is_registered - return if a pid has a cmdline registered
199  * @pevent: handle for the pevent
200  * @pid: The pid to check if it has a cmdline registered with.
201  *
202  * Returns 1 if the pid has a cmdline mapped to it
203  * 0 otherwise.
204  */
pevent_pid_is_registered(struct pevent * pevent,int pid)205 int pevent_pid_is_registered(struct pevent *pevent, int pid)
206 {
207 	const struct cmdline *comm;
208 	struct cmdline key;
209 
210 	if (!pid)
211 		return 1;
212 
213 	if (!pevent->cmdlines && cmdline_init(pevent))
214 		return 0;
215 
216 	key.pid = pid;
217 
218 	comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
219 		       sizeof(*pevent->cmdlines), cmdline_cmp);
220 
221 	if (comm)
222 		return 1;
223 	return 0;
224 }
225 
226 /*
227  * If the command lines have been converted to an array, then
228  * we must add this pid. This is much slower than when cmdlines
229  * are added before the array is initialized.
230  */
add_new_comm(struct pevent * pevent,const char * comm,int pid)231 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
232 {
233 	struct cmdline *cmdlines = pevent->cmdlines;
234 	const struct cmdline *cmdline;
235 	struct cmdline key;
236 
237 	if (!pid)
238 		return 0;
239 
240 	/* avoid duplicates */
241 	key.pid = pid;
242 
243 	cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
244 		       sizeof(*pevent->cmdlines), cmdline_cmp);
245 	if (cmdline) {
246 		errno = EEXIST;
247 		return -1;
248 	}
249 
250 	cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
251 	if (!cmdlines) {
252 		errno = ENOMEM;
253 		return -1;
254 	}
255 
256 	cmdlines[pevent->cmdline_count].comm = strdup(comm);
257 	if (!cmdlines[pevent->cmdline_count].comm) {
258 		free(cmdlines);
259 		errno = ENOMEM;
260 		return -1;
261 	}
262 
263 	cmdlines[pevent->cmdline_count].pid = pid;
264 
265 	if (cmdlines[pevent->cmdline_count].comm)
266 		pevent->cmdline_count++;
267 
268 	qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
269 	pevent->cmdlines = cmdlines;
270 
271 	return 0;
272 }
273 
274 /**
275  * pevent_register_comm - register a pid / comm mapping
276  * @pevent: handle for the pevent
277  * @comm: the command line to register
278  * @pid: the pid to map the command line to
279  *
280  * This adds a mapping to search for command line names with
281  * a given pid. The comm is duplicated.
282  */
pevent_register_comm(struct pevent * pevent,const char * comm,int pid)283 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
284 {
285 	struct cmdline_list *item;
286 
287 	if (pevent->cmdlines)
288 		return add_new_comm(pevent, comm, pid);
289 
290 	item = malloc(sizeof(*item));
291 	if (!item)
292 		return -1;
293 
294 	item->comm = strdup(comm);
295 	if (!item->comm) {
296 		free(item);
297 		return -1;
298 	}
299 	item->pid = pid;
300 	item->next = pevent->cmdlist;
301 
302 	pevent->cmdlist = item;
303 	pevent->cmdline_count++;
304 
305 	return 0;
306 }
307 
308 struct func_map {
309 	unsigned long long		addr;
310 	char				*func;
311 	char				*mod;
312 };
313 
314 struct func_list {
315 	struct func_list	*next;
316 	unsigned long long	addr;
317 	char			*func;
318 	char			*mod;
319 };
320 
func_cmp(const void * a,const void * b)321 static int func_cmp(const void *a, const void *b)
322 {
323 	const struct func_map *fa = a;
324 	const struct func_map *fb = b;
325 
326 	if (fa->addr < fb->addr)
327 		return -1;
328 	if (fa->addr > fb->addr)
329 		return 1;
330 
331 	return 0;
332 }
333 
334 /*
335  * We are searching for a record in between, not an exact
336  * match.
337  */
func_bcmp(const void * a,const void * b)338 static int func_bcmp(const void *a, const void *b)
339 {
340 	const struct func_map *fa = a;
341 	const struct func_map *fb = b;
342 
343 	if ((fa->addr == fb->addr) ||
344 
345 	    (fa->addr > fb->addr &&
346 	     fa->addr < (fb+1)->addr))
347 		return 0;
348 
349 	if (fa->addr < fb->addr)
350 		return -1;
351 
352 	return 1;
353 }
354 
func_map_init(struct pevent * pevent)355 static int func_map_init(struct pevent *pevent)
356 {
357 	struct func_list *funclist;
358 	struct func_list *item;
359 	struct func_map *func_map;
360 	int i;
361 
362 	func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
363 	if (!func_map)
364 		return -1;
365 
366 	funclist = pevent->funclist;
367 
368 	i = 0;
369 	while (funclist) {
370 		func_map[i].func = funclist->func;
371 		func_map[i].addr = funclist->addr;
372 		func_map[i].mod = funclist->mod;
373 		i++;
374 		item = funclist;
375 		funclist = funclist->next;
376 		free(item);
377 	}
378 
379 	qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
380 
381 	/*
382 	 * Add a special record at the end.
383 	 */
384 	func_map[pevent->func_count].func = NULL;
385 	func_map[pevent->func_count].addr = 0;
386 	func_map[pevent->func_count].mod = NULL;
387 
388 	pevent->func_map = func_map;
389 	pevent->funclist = NULL;
390 
391 	return 0;
392 }
393 
394 static struct func_map *
find_func(struct pevent * pevent,unsigned long long addr)395 find_func(struct pevent *pevent, unsigned long long addr)
396 {
397 	struct func_map *func;
398 	struct func_map key;
399 
400 	if (!pevent->func_map)
401 		func_map_init(pevent);
402 
403 	key.addr = addr;
404 
405 	func = bsearch(&key, pevent->func_map, pevent->func_count,
406 		       sizeof(*pevent->func_map), func_bcmp);
407 
408 	return func;
409 }
410 
411 /**
412  * pevent_find_function - find a function by a given address
413  * @pevent: handle for the pevent
414  * @addr: the address to find the function with
415  *
416  * Returns a pointer to the function stored that has the given
417  * address. Note, the address does not have to be exact, it
418  * will select the function that would contain the address.
419  */
pevent_find_function(struct pevent * pevent,unsigned long long addr)420 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
421 {
422 	struct func_map *map;
423 
424 	map = find_func(pevent, addr);
425 	if (!map)
426 		return NULL;
427 
428 	return map->func;
429 }
430 
431 /**
432  * pevent_find_function_address - find a function address by a given address
433  * @pevent: handle for the pevent
434  * @addr: the address to find the function with
435  *
436  * Returns the address the function starts at. This can be used in
437  * conjunction with pevent_find_function to print both the function
438  * name and the function offset.
439  */
440 unsigned long long
pevent_find_function_address(struct pevent * pevent,unsigned long long addr)441 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
442 {
443 	struct func_map *map;
444 
445 	map = find_func(pevent, addr);
446 	if (!map)
447 		return 0;
448 
449 	return map->addr;
450 }
451 
452 /**
453  * pevent_register_function - register a function with a given address
454  * @pevent: handle for the pevent
455  * @function: the function name to register
456  * @addr: the address the function starts at
457  * @mod: the kernel module the function may be in (NULL for none)
458  *
459  * This registers a function name with an address and module.
460  * The @func passed in is duplicated.
461  */
pevent_register_function(struct pevent * pevent,char * func,unsigned long long addr,char * mod)462 int pevent_register_function(struct pevent *pevent, char *func,
463 			     unsigned long long addr, char *mod)
464 {
465 	struct func_list *item = malloc(sizeof(*item));
466 
467 	if (!item)
468 		return -1;
469 
470 	item->next = pevent->funclist;
471 	item->func = strdup(func);
472 	if (!item->func)
473 		goto out_free;
474 
475 	if (mod) {
476 		item->mod = strdup(mod);
477 		if (!item->mod)
478 			goto out_free_func;
479 	} else
480 		item->mod = NULL;
481 	item->addr = addr;
482 
483 	pevent->funclist = item;
484 	pevent->func_count++;
485 
486 	return 0;
487 
488 out_free_func:
489 	free(item->func);
490 	item->func = NULL;
491 out_free:
492 	free(item);
493 	errno = ENOMEM;
494 	return -1;
495 }
496 
497 /**
498  * pevent_print_funcs - print out the stored functions
499  * @pevent: handle for the pevent
500  *
501  * This prints out the stored functions.
502  */
pevent_print_funcs(struct pevent * pevent)503 void pevent_print_funcs(struct pevent *pevent)
504 {
505 	int i;
506 
507 	if (!pevent->func_map)
508 		func_map_init(pevent);
509 
510 	for (i = 0; i < (int)pevent->func_count; i++) {
511 		printf("%016llx %s",
512 		       pevent->func_map[i].addr,
513 		       pevent->func_map[i].func);
514 		if (pevent->func_map[i].mod)
515 			printf(" [%s]\n", pevent->func_map[i].mod);
516 		else
517 			printf("\n");
518 	}
519 }
520 
521 struct printk_map {
522 	unsigned long long		addr;
523 	char				*printk;
524 };
525 
526 struct printk_list {
527 	struct printk_list	*next;
528 	unsigned long long	addr;
529 	char			*printk;
530 };
531 
printk_cmp(const void * a,const void * b)532 static int printk_cmp(const void *a, const void *b)
533 {
534 	const struct printk_map *pa = a;
535 	const struct printk_map *pb = b;
536 
537 	if (pa->addr < pb->addr)
538 		return -1;
539 	if (pa->addr > pb->addr)
540 		return 1;
541 
542 	return 0;
543 }
544 
printk_map_init(struct pevent * pevent)545 static int printk_map_init(struct pevent *pevent)
546 {
547 	struct printk_list *printklist;
548 	struct printk_list *item;
549 	struct printk_map *printk_map;
550 	int i;
551 
552 	printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
553 	if (!printk_map)
554 		return -1;
555 
556 	printklist = pevent->printklist;
557 
558 	i = 0;
559 	while (printklist) {
560 		printk_map[i].printk = printklist->printk;
561 		printk_map[i].addr = printklist->addr;
562 		i++;
563 		item = printklist;
564 		printklist = printklist->next;
565 		free(item);
566 	}
567 
568 	qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
569 
570 	pevent->printk_map = printk_map;
571 	pevent->printklist = NULL;
572 
573 	return 0;
574 }
575 
576 static struct printk_map *
find_printk(struct pevent * pevent,unsigned long long addr)577 find_printk(struct pevent *pevent, unsigned long long addr)
578 {
579 	struct printk_map *printk;
580 	struct printk_map key;
581 
582 	if (!pevent->printk_map && printk_map_init(pevent))
583 		return NULL;
584 
585 	key.addr = addr;
586 
587 	printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
588 			 sizeof(*pevent->printk_map), printk_cmp);
589 
590 	return printk;
591 }
592 
593 /**
594  * pevent_register_print_string - register a string by its address
595  * @pevent: handle for the pevent
596  * @fmt: the string format to register
597  * @addr: the address the string was located at
598  *
599  * This registers a string by the address it was stored in the kernel.
600  * The @fmt passed in is duplicated.
601  */
pevent_register_print_string(struct pevent * pevent,char * fmt,unsigned long long addr)602 int pevent_register_print_string(struct pevent *pevent, char *fmt,
603 				 unsigned long long addr)
604 {
605 	struct printk_list *item = malloc(sizeof(*item));
606 
607 	if (!item)
608 		return -1;
609 
610 	item->next = pevent->printklist;
611 	item->addr = addr;
612 
613 	item->printk = strdup(fmt);
614 	if (!item->printk)
615 		goto out_free;
616 
617 	pevent->printklist = item;
618 	pevent->printk_count++;
619 
620 	return 0;
621 
622 out_free:
623 	free(item);
624 	errno = ENOMEM;
625 	return -1;
626 }
627 
628 /**
629  * pevent_print_printk - print out the stored strings
630  * @pevent: handle for the pevent
631  *
632  * This prints the string formats that were stored.
633  */
pevent_print_printk(struct pevent * pevent)634 void pevent_print_printk(struct pevent *pevent)
635 {
636 	int i;
637 
638 	if (!pevent->printk_map)
639 		printk_map_init(pevent);
640 
641 	for (i = 0; i < (int)pevent->printk_count; i++) {
642 		printf("%016llx %s\n",
643 		       pevent->printk_map[i].addr,
644 		       pevent->printk_map[i].printk);
645 	}
646 }
647 
alloc_event(void)648 static struct event_format *alloc_event(void)
649 {
650 	return calloc(1, sizeof(struct event_format));
651 }
652 
add_event(struct pevent * pevent,struct event_format * event)653 static int add_event(struct pevent *pevent, struct event_format *event)
654 {
655 	int i;
656 	struct event_format **events = realloc(pevent->events, sizeof(event) *
657 					       (pevent->nr_events + 1));
658 	if (!events)
659 		return -1;
660 
661 	pevent->events = events;
662 
663 	for (i = 0; i < pevent->nr_events; i++) {
664 		if (pevent->events[i]->id > event->id)
665 			break;
666 	}
667 	if (i < pevent->nr_events)
668 		memmove(&pevent->events[i + 1],
669 			&pevent->events[i],
670 			sizeof(event) * (pevent->nr_events - i));
671 
672 	pevent->events[i] = event;
673 	pevent->nr_events++;
674 
675 	event->pevent = pevent;
676 
677 	return 0;
678 }
679 
event_item_type(enum event_type type)680 static int event_item_type(enum event_type type)
681 {
682 	switch (type) {
683 	case EVENT_ITEM ... EVENT_SQUOTE:
684 		return 1;
685 	case EVENT_ERROR ... EVENT_DELIM:
686 	default:
687 		return 0;
688 	}
689 }
690 
free_flag_sym(struct print_flag_sym * fsym)691 static void free_flag_sym(struct print_flag_sym *fsym)
692 {
693 	struct print_flag_sym *next;
694 
695 	while (fsym) {
696 		next = fsym->next;
697 		free(fsym->value);
698 		free(fsym->str);
699 		free(fsym);
700 		fsym = next;
701 	}
702 }
703 
free_arg(struct print_arg * arg)704 static void free_arg(struct print_arg *arg)
705 {
706 	struct print_arg *farg;
707 
708 	if (!arg)
709 		return;
710 
711 	switch (arg->type) {
712 	case PRINT_ATOM:
713 		free(arg->atom.atom);
714 		break;
715 	case PRINT_FIELD:
716 		free(arg->field.name);
717 		break;
718 	case PRINT_FLAGS:
719 		free_arg(arg->flags.field);
720 		free(arg->flags.delim);
721 		free_flag_sym(arg->flags.flags);
722 		break;
723 	case PRINT_SYMBOL:
724 		free_arg(arg->symbol.field);
725 		free_flag_sym(arg->symbol.symbols);
726 		break;
727 	case PRINT_HEX:
728 		free_arg(arg->hex.field);
729 		free_arg(arg->hex.size);
730 		break;
731 	case PRINT_TYPE:
732 		free(arg->typecast.type);
733 		free_arg(arg->typecast.item);
734 		break;
735 	case PRINT_STRING:
736 	case PRINT_BSTRING:
737 		free(arg->string.string);
738 		break;
739 	case PRINT_DYNAMIC_ARRAY:
740 		free(arg->dynarray.index);
741 		break;
742 	case PRINT_OP:
743 		free(arg->op.op);
744 		free_arg(arg->op.left);
745 		free_arg(arg->op.right);
746 		break;
747 	case PRINT_FUNC:
748 		while (arg->func.args) {
749 			farg = arg->func.args;
750 			arg->func.args = farg->next;
751 			free_arg(farg);
752 		}
753 		break;
754 
755 	case PRINT_NULL:
756 	default:
757 		break;
758 	}
759 
760 	free(arg);
761 }
762 
get_type(int ch)763 static enum event_type get_type(int ch)
764 {
765 	if (ch == '\n')
766 		return EVENT_NEWLINE;
767 	if (isspace(ch))
768 		return EVENT_SPACE;
769 	if (isalnum(ch) || ch == '_')
770 		return EVENT_ITEM;
771 	if (ch == '\'')
772 		return EVENT_SQUOTE;
773 	if (ch == '"')
774 		return EVENT_DQUOTE;
775 	if (!isprint(ch))
776 		return EVENT_NONE;
777 	if (ch == '(' || ch == ')' || ch == ',')
778 		return EVENT_DELIM;
779 
780 	return EVENT_OP;
781 }
782 
__read_char(void)783 static int __read_char(void)
784 {
785 	if (input_buf_ptr >= input_buf_siz)
786 		return -1;
787 
788 	return input_buf[input_buf_ptr++];
789 }
790 
__peek_char(void)791 static int __peek_char(void)
792 {
793 	if (input_buf_ptr >= input_buf_siz)
794 		return -1;
795 
796 	return input_buf[input_buf_ptr];
797 }
798 
799 /**
800  * pevent_peek_char - peek at the next character that will be read
801  *
802  * Returns the next character read, or -1 if end of buffer.
803  */
pevent_peek_char(void)804 int pevent_peek_char(void)
805 {
806 	return __peek_char();
807 }
808 
extend_token(char ** tok,char * buf,int size)809 static int extend_token(char **tok, char *buf, int size)
810 {
811 	char *newtok = realloc(*tok, size);
812 
813 	if (!newtok) {
814 		free(*tok);
815 		*tok = NULL;
816 		return -1;
817 	}
818 
819 	if (!*tok)
820 		strcpy(newtok, buf);
821 	else
822 		strcat(newtok, buf);
823 	*tok = newtok;
824 
825 	return 0;
826 }
827 
828 static enum event_type force_token(const char *str, char **tok);
829 
__read_token(char ** tok)830 static enum event_type __read_token(char **tok)
831 {
832 	char buf[BUFSIZ];
833 	int ch, last_ch, quote_ch, next_ch;
834 	int i = 0;
835 	int tok_size = 0;
836 	enum event_type type;
837 
838 	*tok = NULL;
839 
840 
841 	ch = __read_char();
842 	if (ch < 0)
843 		return EVENT_NONE;
844 
845 	type = get_type(ch);
846 	if (type == EVENT_NONE)
847 		return type;
848 
849 	buf[i++] = ch;
850 
851 	switch (type) {
852 	case EVENT_NEWLINE:
853 	case EVENT_DELIM:
854 		if (asprintf(tok, "%c", ch) < 0)
855 			return EVENT_ERROR;
856 
857 		return type;
858 
859 	case EVENT_OP:
860 		switch (ch) {
861 		case '-':
862 			next_ch = __peek_char();
863 			if (next_ch == '>') {
864 				buf[i++] = __read_char();
865 				break;
866 			}
867 			/* fall through */
868 		case '+':
869 		case '|':
870 		case '&':
871 		case '>':
872 		case '<':
873 			last_ch = ch;
874 			ch = __peek_char();
875 			if (ch != last_ch)
876 				goto test_equal;
877 			buf[i++] = __read_char();
878 			switch (last_ch) {
879 			case '>':
880 			case '<':
881 				goto test_equal;
882 			default:
883 				break;
884 			}
885 			break;
886 		case '!':
887 		case '=':
888 			goto test_equal;
889 		default: /* what should we do instead? */
890 			break;
891 		}
892 		buf[i] = 0;
893 		*tok = strdup(buf);
894 		return type;
895 
896  test_equal:
897 		ch = __peek_char();
898 		if (ch == '=')
899 			buf[i++] = __read_char();
900 		goto out;
901 
902 	case EVENT_DQUOTE:
903 	case EVENT_SQUOTE:
904 		/* don't keep quotes */
905 		i--;
906 		quote_ch = ch;
907 		last_ch = 0;
908  concat:
909 		do {
910 			if (i == (BUFSIZ - 1)) {
911 				buf[i] = 0;
912 				tok_size += BUFSIZ;
913 
914 				if (extend_token(tok, buf, tok_size) < 0)
915 					return EVENT_NONE;
916 				i = 0;
917 			}
918 			last_ch = ch;
919 			ch = __read_char();
920 			buf[i++] = ch;
921 			/* the '\' '\' will cancel itself */
922 			if (ch == '\\' && last_ch == '\\')
923 				last_ch = 0;
924 		} while (ch != quote_ch || last_ch == '\\');
925 		/* remove the last quote */
926 		i--;
927 
928 		/*
929 		 * For strings (double quotes) check the next token.
930 		 * If it is another string, concatinate the two.
931 		 */
932 		if (type == EVENT_DQUOTE) {
933 			unsigned long long save_input_buf_ptr = input_buf_ptr;
934 
935 			do {
936 				ch = __read_char();
937 			} while (isspace(ch));
938 			if (ch == '"')
939 				goto concat;
940 			input_buf_ptr = save_input_buf_ptr;
941 		}
942 
943 		goto out;
944 
945 	case EVENT_ERROR ... EVENT_SPACE:
946 	case EVENT_ITEM:
947 	default:
948 		break;
949 	}
950 
951 	while (get_type(__peek_char()) == type) {
952 		if (i == (BUFSIZ - 1)) {
953 			buf[i] = 0;
954 			tok_size += BUFSIZ;
955 
956 			if (extend_token(tok, buf, tok_size) < 0)
957 				return EVENT_NONE;
958 			i = 0;
959 		}
960 		ch = __read_char();
961 		buf[i++] = ch;
962 	}
963 
964  out:
965 	buf[i] = 0;
966 	if (extend_token(tok, buf, tok_size + i + 1) < 0)
967 		return EVENT_NONE;
968 
969 	if (type == EVENT_ITEM) {
970 		/*
971 		 * Older versions of the kernel has a bug that
972 		 * creates invalid symbols and will break the mac80211
973 		 * parsing. This is a work around to that bug.
974 		 *
975 		 * See Linux kernel commit:
976 		 *  811cb50baf63461ce0bdb234927046131fc7fa8b
977 		 */
978 		if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
979 			free(*tok);
980 			*tok = NULL;
981 			return force_token("\"\%s\" ", tok);
982 		} else if (strcmp(*tok, "STA_PR_FMT") == 0) {
983 			free(*tok);
984 			*tok = NULL;
985 			return force_token("\" sta:%pM\" ", tok);
986 		} else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
987 			free(*tok);
988 			*tok = NULL;
989 			return force_token("\" vif:%p(%d)\" ", tok);
990 		}
991 	}
992 
993 	return type;
994 }
995 
force_token(const char * str,char ** tok)996 static enum event_type force_token(const char *str, char **tok)
997 {
998 	const char *save_input_buf;
999 	unsigned long long save_input_buf_ptr;
1000 	unsigned long long save_input_buf_siz;
1001 	enum event_type type;
1002 
1003 	/* save off the current input pointers */
1004 	save_input_buf = input_buf;
1005 	save_input_buf_ptr = input_buf_ptr;
1006 	save_input_buf_siz = input_buf_siz;
1007 
1008 	init_input_buf(str, strlen(str));
1009 
1010 	type = __read_token(tok);
1011 
1012 	/* reset back to original token */
1013 	input_buf = save_input_buf;
1014 	input_buf_ptr = save_input_buf_ptr;
1015 	input_buf_siz = save_input_buf_siz;
1016 
1017 	return type;
1018 }
1019 
free_token(char * tok)1020 static void free_token(char *tok)
1021 {
1022 	if (tok)
1023 		free(tok);
1024 }
1025 
read_token(char ** tok)1026 static enum event_type read_token(char **tok)
1027 {
1028 	enum event_type type;
1029 
1030 	for (;;) {
1031 		type = __read_token(tok);
1032 		if (type != EVENT_SPACE)
1033 			return type;
1034 
1035 		free_token(*tok);
1036 	}
1037 
1038 	/* not reached */
1039 	*tok = NULL;
1040 	return EVENT_NONE;
1041 }
1042 
1043 /**
1044  * pevent_read_token - access to utilites to use the pevent parser
1045  * @tok: The token to return
1046  *
1047  * This will parse tokens from the string given by
1048  * pevent_init_data().
1049  *
1050  * Returns the token type.
1051  */
pevent_read_token(char ** tok)1052 enum event_type pevent_read_token(char **tok)
1053 {
1054 	return read_token(tok);
1055 }
1056 
1057 /**
1058  * pevent_free_token - free a token returned by pevent_read_token
1059  * @token: the token to free
1060  */
pevent_free_token(char * token)1061 void pevent_free_token(char *token)
1062 {
1063 	free_token(token);
1064 }
1065 
1066 /* no newline */
read_token_item(char ** tok)1067 static enum event_type read_token_item(char **tok)
1068 {
1069 	enum event_type type;
1070 
1071 	for (;;) {
1072 		type = __read_token(tok);
1073 		if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1074 			return type;
1075 		free_token(*tok);
1076 		*tok = NULL;
1077 	}
1078 
1079 	/* not reached */
1080 	*tok = NULL;
1081 	return EVENT_NONE;
1082 }
1083 
test_type(enum event_type type,enum event_type expect)1084 static int test_type(enum event_type type, enum event_type expect)
1085 {
1086 	if (type != expect) {
1087 		do_warning("Error: expected type %d but read %d",
1088 		    expect, type);
1089 		return -1;
1090 	}
1091 	return 0;
1092 }
1093 
test_type_token(enum event_type type,const char * token,enum event_type expect,const char * expect_tok)1094 static int test_type_token(enum event_type type, const char *token,
1095 		    enum event_type expect, const char *expect_tok)
1096 {
1097 	if (type != expect) {
1098 		do_warning("Error: expected type %d but read %d",
1099 		    expect, type);
1100 		return -1;
1101 	}
1102 
1103 	if (strcmp(token, expect_tok) != 0) {
1104 		do_warning("Error: expected '%s' but read '%s'",
1105 		    expect_tok, token);
1106 		return -1;
1107 	}
1108 	return 0;
1109 }
1110 
__read_expect_type(enum event_type expect,char ** tok,int newline_ok)1111 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1112 {
1113 	enum event_type type;
1114 
1115 	if (newline_ok)
1116 		type = read_token(tok);
1117 	else
1118 		type = read_token_item(tok);
1119 	return test_type(type, expect);
1120 }
1121 
read_expect_type(enum event_type expect,char ** tok)1122 static int read_expect_type(enum event_type expect, char **tok)
1123 {
1124 	return __read_expect_type(expect, tok, 1);
1125 }
1126 
__read_expected(enum event_type expect,const char * str,int newline_ok)1127 static int __read_expected(enum event_type expect, const char *str,
1128 			   int newline_ok)
1129 {
1130 	enum event_type type;
1131 	char *token;
1132 	int ret;
1133 
1134 	if (newline_ok)
1135 		type = read_token(&token);
1136 	else
1137 		type = read_token_item(&token);
1138 
1139 	ret = test_type_token(type, token, expect, str);
1140 
1141 	free_token(token);
1142 
1143 	return ret;
1144 }
1145 
read_expected(enum event_type expect,const char * str)1146 static int read_expected(enum event_type expect, const char *str)
1147 {
1148 	return __read_expected(expect, str, 1);
1149 }
1150 
read_expected_item(enum event_type expect,const char * str)1151 static int read_expected_item(enum event_type expect, const char *str)
1152 {
1153 	return __read_expected(expect, str, 0);
1154 }
1155 
event_read_name(void)1156 static char *event_read_name(void)
1157 {
1158 	char *token;
1159 
1160 	if (read_expected(EVENT_ITEM, "name") < 0)
1161 		return NULL;
1162 
1163 	if (read_expected(EVENT_OP, ":") < 0)
1164 		return NULL;
1165 
1166 	if (read_expect_type(EVENT_ITEM, &token) < 0)
1167 		goto fail;
1168 
1169 	return token;
1170 
1171  fail:
1172 	free_token(token);
1173 	return NULL;
1174 }
1175 
event_read_id(void)1176 static int event_read_id(void)
1177 {
1178 	char *token;
1179 	int id;
1180 
1181 	if (read_expected_item(EVENT_ITEM, "ID") < 0)
1182 		return -1;
1183 
1184 	if (read_expected(EVENT_OP, ":") < 0)
1185 		return -1;
1186 
1187 	if (read_expect_type(EVENT_ITEM, &token) < 0)
1188 		goto fail;
1189 
1190 	id = strtoul(token, NULL, 0);
1191 	free_token(token);
1192 	return id;
1193 
1194  fail:
1195 	free_token(token);
1196 	return -1;
1197 }
1198 
field_is_string(struct format_field * field)1199 static int field_is_string(struct format_field *field)
1200 {
1201 	if ((field->flags & FIELD_IS_ARRAY) &&
1202 	    (strstr(field->type, "char") || strstr(field->type, "u8") ||
1203 	     strstr(field->type, "s8")))
1204 		return 1;
1205 
1206 	return 0;
1207 }
1208 
field_is_dynamic(struct format_field * field)1209 static int field_is_dynamic(struct format_field *field)
1210 {
1211 	if (strncmp(field->type, "__data_loc", 10) == 0)
1212 		return 1;
1213 
1214 	return 0;
1215 }
1216 
field_is_long(struct format_field * field)1217 static int field_is_long(struct format_field *field)
1218 {
1219 	/* includes long long */
1220 	if (strstr(field->type, "long"))
1221 		return 1;
1222 
1223 	return 0;
1224 }
1225 
type_size(const char * name)1226 static unsigned int type_size(const char *name)
1227 {
1228 	/* This covers all FIELD_IS_STRING types. */
1229 	static struct {
1230 		const char *type;
1231 		unsigned int size;
1232 	} table[] = {
1233 		{ "u8",   1 },
1234 		{ "u16",  2 },
1235 		{ "u32",  4 },
1236 		{ "u64",  8 },
1237 		{ "s8",   1 },
1238 		{ "s16",  2 },
1239 		{ "s32",  4 },
1240 		{ "s64",  8 },
1241 		{ "char", 1 },
1242 		{ },
1243 	};
1244 	int i;
1245 
1246 	for (i = 0; table[i].type; i++) {
1247 		if (!strcmp(table[i].type, name))
1248 			return table[i].size;
1249 	}
1250 
1251 	return 0;
1252 }
1253 
event_read_fields(struct event_format * event,struct format_field ** fields)1254 static int event_read_fields(struct event_format *event, struct format_field **fields)
1255 {
1256 	struct format_field *field = NULL;
1257 	enum event_type type;
1258 	char *token;
1259 	char *last_token;
1260 	int count = 0;
1261 
1262 	do {
1263 		unsigned int size_dynamic = 0;
1264 
1265 		type = read_token(&token);
1266 		if (type == EVENT_NEWLINE) {
1267 			free_token(token);
1268 			return count;
1269 		}
1270 
1271 		count++;
1272 
1273 		if (test_type_token(type, token, EVENT_ITEM, "field"))
1274 			goto fail;
1275 		free_token(token);
1276 
1277 		type = read_token(&token);
1278 		/*
1279 		 * The ftrace fields may still use the "special" name.
1280 		 * Just ignore it.
1281 		 */
1282 		if (event->flags & EVENT_FL_ISFTRACE &&
1283 		    type == EVENT_ITEM && strcmp(token, "special") == 0) {
1284 			free_token(token);
1285 			type = read_token(&token);
1286 		}
1287 
1288 		if (test_type_token(type, token, EVENT_OP, ":") < 0)
1289 			goto fail;
1290 
1291 		free_token(token);
1292 		if (read_expect_type(EVENT_ITEM, &token) < 0)
1293 			goto fail;
1294 
1295 		last_token = token;
1296 
1297 		field = calloc(1, sizeof(*field));
1298 		if (!field)
1299 			goto fail;
1300 
1301 		field->event = event;
1302 
1303 		/* read the rest of the type */
1304 		for (;;) {
1305 			type = read_token(&token);
1306 			if (type == EVENT_ITEM ||
1307 			    (type == EVENT_OP && strcmp(token, "*") == 0) ||
1308 			    /*
1309 			     * Some of the ftrace fields are broken and have
1310 			     * an illegal "." in them.
1311 			     */
1312 			    (event->flags & EVENT_FL_ISFTRACE &&
1313 			     type == EVENT_OP && strcmp(token, ".") == 0)) {
1314 
1315 				if (strcmp(token, "*") == 0)
1316 					field->flags |= FIELD_IS_POINTER;
1317 
1318 				if (field->type) {
1319 					char *new_type;
1320 					new_type = realloc(field->type,
1321 							   strlen(field->type) +
1322 							   strlen(last_token) + 2);
1323 					if (!new_type) {
1324 						free(last_token);
1325 						goto fail;
1326 					}
1327 					field->type = new_type;
1328 					strcat(field->type, " ");
1329 					strcat(field->type, last_token);
1330 					free(last_token);
1331 				} else
1332 					field->type = last_token;
1333 				last_token = token;
1334 				continue;
1335 			}
1336 
1337 			break;
1338 		}
1339 
1340 		if (!field->type) {
1341 			do_warning("%s: no type found", __func__);
1342 			goto fail;
1343 		}
1344 		field->name = last_token;
1345 
1346 		if (test_type(type, EVENT_OP))
1347 			goto fail;
1348 
1349 		if (strcmp(token, "[") == 0) {
1350 			enum event_type last_type = type;
1351 			char *brackets = token;
1352 			char *new_brackets;
1353 			int len;
1354 
1355 			field->flags |= FIELD_IS_ARRAY;
1356 
1357 			type = read_token(&token);
1358 
1359 			if (type == EVENT_ITEM)
1360 				field->arraylen = strtoul(token, NULL, 0);
1361 			else
1362 				field->arraylen = 0;
1363 
1364 		        while (strcmp(token, "]") != 0) {
1365 				if (last_type == EVENT_ITEM &&
1366 				    type == EVENT_ITEM)
1367 					len = 2;
1368 				else
1369 					len = 1;
1370 				last_type = type;
1371 
1372 				new_brackets = realloc(brackets,
1373 						       strlen(brackets) +
1374 						       strlen(token) + len);
1375 				if (!new_brackets) {
1376 					free(brackets);
1377 					goto fail;
1378 				}
1379 				brackets = new_brackets;
1380 				if (len == 2)
1381 					strcat(brackets, " ");
1382 				strcat(brackets, token);
1383 				/* We only care about the last token */
1384 				field->arraylen = strtoul(token, NULL, 0);
1385 				free_token(token);
1386 				type = read_token(&token);
1387 				if (type == EVENT_NONE) {
1388 					do_warning("failed to find token");
1389 					goto fail;
1390 				}
1391 			}
1392 
1393 			free_token(token);
1394 
1395 			new_brackets = realloc(brackets, strlen(brackets) + 2);
1396 			if (!new_brackets) {
1397 				free(brackets);
1398 				goto fail;
1399 			}
1400 			brackets = new_brackets;
1401 			strcat(brackets, "]");
1402 
1403 			/* add brackets to type */
1404 
1405 			type = read_token(&token);
1406 			/*
1407 			 * If the next token is not an OP, then it is of
1408 			 * the format: type [] item;
1409 			 */
1410 			if (type == EVENT_ITEM) {
1411 				char *new_type;
1412 				new_type = realloc(field->type,
1413 						   strlen(field->type) +
1414 						   strlen(field->name) +
1415 						   strlen(brackets) + 2);
1416 				if (!new_type) {
1417 					free(brackets);
1418 					goto fail;
1419 				}
1420 				field->type = new_type;
1421 				strcat(field->type, " ");
1422 				strcat(field->type, field->name);
1423 				size_dynamic = type_size(field->name);
1424 				free_token(field->name);
1425 				strcat(field->type, brackets);
1426 				field->name = token;
1427 				type = read_token(&token);
1428 			} else {
1429 				char *new_type;
1430 				new_type = realloc(field->type,
1431 						   strlen(field->type) +
1432 						   strlen(brackets) + 1);
1433 				if (!new_type) {
1434 					free(brackets);
1435 					goto fail;
1436 				}
1437 				field->type = new_type;
1438 				strcat(field->type, brackets);
1439 			}
1440 			free(brackets);
1441 		}
1442 
1443 		if (field_is_string(field))
1444 			field->flags |= FIELD_IS_STRING;
1445 		if (field_is_dynamic(field))
1446 			field->flags |= FIELD_IS_DYNAMIC;
1447 		if (field_is_long(field))
1448 			field->flags |= FIELD_IS_LONG;
1449 
1450 		if (test_type_token(type, token,  EVENT_OP, ";"))
1451 			goto fail;
1452 		free_token(token);
1453 
1454 		if (read_expected(EVENT_ITEM, "offset") < 0)
1455 			goto fail_expect;
1456 
1457 		if (read_expected(EVENT_OP, ":") < 0)
1458 			goto fail_expect;
1459 
1460 		if (read_expect_type(EVENT_ITEM, &token))
1461 			goto fail;
1462 		field->offset = strtoul(token, NULL, 0);
1463 		free_token(token);
1464 
1465 		if (read_expected(EVENT_OP, ";") < 0)
1466 			goto fail_expect;
1467 
1468 		if (read_expected(EVENT_ITEM, "size") < 0)
1469 			goto fail_expect;
1470 
1471 		if (read_expected(EVENT_OP, ":") < 0)
1472 			goto fail_expect;
1473 
1474 		if (read_expect_type(EVENT_ITEM, &token))
1475 			goto fail;
1476 		field->size = strtoul(token, NULL, 0);
1477 		free_token(token);
1478 
1479 		if (read_expected(EVENT_OP, ";") < 0)
1480 			goto fail_expect;
1481 
1482 		type = read_token(&token);
1483 		if (type != EVENT_NEWLINE) {
1484 			/* newer versions of the kernel have a "signed" type */
1485 			if (test_type_token(type, token, EVENT_ITEM, "signed"))
1486 				goto fail;
1487 
1488 			free_token(token);
1489 
1490 			if (read_expected(EVENT_OP, ":") < 0)
1491 				goto fail_expect;
1492 
1493 			if (read_expect_type(EVENT_ITEM, &token))
1494 				goto fail;
1495 
1496 			if (strtoul(token, NULL, 0))
1497 				field->flags |= FIELD_IS_SIGNED;
1498 
1499 			free_token(token);
1500 			if (read_expected(EVENT_OP, ";") < 0)
1501 				goto fail_expect;
1502 
1503 			if (read_expect_type(EVENT_NEWLINE, &token))
1504 				goto fail;
1505 		}
1506 
1507 		free_token(token);
1508 
1509 		if (field->flags & FIELD_IS_ARRAY) {
1510 			if (field->arraylen)
1511 				field->elementsize = field->size / field->arraylen;
1512 			else if (field->flags & FIELD_IS_DYNAMIC)
1513 				field->elementsize = size_dynamic;
1514 			else if (field->flags & FIELD_IS_STRING)
1515 				field->elementsize = 1;
1516 			else if (field->flags & FIELD_IS_LONG)
1517 				field->elementsize = event->pevent ?
1518 						     event->pevent->long_size :
1519 						     sizeof(long);
1520 		} else
1521 			field->elementsize = field->size;
1522 
1523 		*fields = field;
1524 		fields = &field->next;
1525 
1526 	} while (1);
1527 
1528 	return 0;
1529 
1530 fail:
1531 	free_token(token);
1532 fail_expect:
1533 	if (field) {
1534 		free(field->type);
1535 		free(field->name);
1536 		free(field);
1537 	}
1538 	return -1;
1539 }
1540 
event_read_format(struct event_format * event)1541 static int event_read_format(struct event_format *event)
1542 {
1543 	char *token;
1544 	int ret;
1545 
1546 	if (read_expected_item(EVENT_ITEM, "format") < 0)
1547 		return -1;
1548 
1549 	if (read_expected(EVENT_OP, ":") < 0)
1550 		return -1;
1551 
1552 	if (read_expect_type(EVENT_NEWLINE, &token))
1553 		goto fail;
1554 	free_token(token);
1555 
1556 	ret = event_read_fields(event, &event->format.common_fields);
1557 	if (ret < 0)
1558 		return ret;
1559 	event->format.nr_common = ret;
1560 
1561 	ret = event_read_fields(event, &event->format.fields);
1562 	if (ret < 0)
1563 		return ret;
1564 	event->format.nr_fields = ret;
1565 
1566 	return 0;
1567 
1568  fail:
1569 	free_token(token);
1570 	return -1;
1571 }
1572 
1573 static enum event_type
1574 process_arg_token(struct event_format *event, struct print_arg *arg,
1575 		  char **tok, enum event_type type);
1576 
1577 static enum event_type
process_arg(struct event_format * event,struct print_arg * arg,char ** tok)1578 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1579 {
1580 	enum event_type type;
1581 	char *token;
1582 
1583 	type = read_token(&token);
1584 	*tok = token;
1585 
1586 	return process_arg_token(event, arg, tok, type);
1587 }
1588 
1589 static enum event_type
1590 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1591 
1592 static enum event_type
process_cond(struct event_format * event,struct print_arg * top,char ** tok)1593 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1594 {
1595 	struct print_arg *arg, *left, *right;
1596 	enum event_type type;
1597 	char *token = NULL;
1598 
1599 	arg = alloc_arg();
1600 	left = alloc_arg();
1601 	right = alloc_arg();
1602 
1603 	if (!arg || !left || !right) {
1604 		do_warning("%s: not enough memory!", __func__);
1605 		/* arg will be freed at out_free */
1606 		free_arg(left);
1607 		free_arg(right);
1608 		goto out_free;
1609 	}
1610 
1611 	arg->type = PRINT_OP;
1612 	arg->op.left = left;
1613 	arg->op.right = right;
1614 
1615 	*tok = NULL;
1616 	type = process_arg(event, left, &token);
1617 
1618  again:
1619 	/* Handle other operations in the arguments */
1620 	if (type == EVENT_OP && strcmp(token, ":") != 0) {
1621 		type = process_op(event, left, &token);
1622 		goto again;
1623 	}
1624 
1625 	if (test_type_token(type, token, EVENT_OP, ":"))
1626 		goto out_free;
1627 
1628 	arg->op.op = token;
1629 
1630 	type = process_arg(event, right, &token);
1631 
1632 	top->op.right = arg;
1633 
1634 	*tok = token;
1635 	return type;
1636 
1637 out_free:
1638 	/* Top may point to itself */
1639 	top->op.right = NULL;
1640 	free_token(token);
1641 	free_arg(arg);
1642 	return EVENT_ERROR;
1643 }
1644 
1645 static enum event_type
process_array(struct event_format * event,struct print_arg * top,char ** tok)1646 process_array(struct event_format *event, struct print_arg *top, char **tok)
1647 {
1648 	struct print_arg *arg;
1649 	enum event_type type;
1650 	char *token = NULL;
1651 
1652 	arg = alloc_arg();
1653 	if (!arg) {
1654 		do_warning("%s: not enough memory!", __func__);
1655 		/* '*tok' is set to top->op.op.  No need to free. */
1656 		*tok = NULL;
1657 		return EVENT_ERROR;
1658 	}
1659 
1660 	*tok = NULL;
1661 	type = process_arg(event, arg, &token);
1662 	if (test_type_token(type, token, EVENT_OP, "]"))
1663 		goto out_free;
1664 
1665 	top->op.right = arg;
1666 
1667 	free_token(token);
1668 	type = read_token_item(&token);
1669 	*tok = token;
1670 
1671 	return type;
1672 
1673 out_free:
1674 	free_token(token);
1675 	free_arg(arg);
1676 	return EVENT_ERROR;
1677 }
1678 
get_op_prio(char * op)1679 static int get_op_prio(char *op)
1680 {
1681 	if (!op[1]) {
1682 		switch (op[0]) {
1683 		case '~':
1684 		case '!':
1685 			return 4;
1686 		case '*':
1687 		case '/':
1688 		case '%':
1689 			return 6;
1690 		case '+':
1691 		case '-':
1692 			return 7;
1693 			/* '>>' and '<<' are 8 */
1694 		case '<':
1695 		case '>':
1696 			return 9;
1697 			/* '==' and '!=' are 10 */
1698 		case '&':
1699 			return 11;
1700 		case '^':
1701 			return 12;
1702 		case '|':
1703 			return 13;
1704 		case '?':
1705 			return 16;
1706 		default:
1707 			do_warning("unknown op '%c'", op[0]);
1708 			return -1;
1709 		}
1710 	} else {
1711 		if (strcmp(op, "++") == 0 ||
1712 		    strcmp(op, "--") == 0) {
1713 			return 3;
1714 		} else if (strcmp(op, ">>") == 0 ||
1715 			   strcmp(op, "<<") == 0) {
1716 			return 8;
1717 		} else if (strcmp(op, ">=") == 0 ||
1718 			   strcmp(op, "<=") == 0) {
1719 			return 9;
1720 		} else if (strcmp(op, "==") == 0 ||
1721 			   strcmp(op, "!=") == 0) {
1722 			return 10;
1723 		} else if (strcmp(op, "&&") == 0) {
1724 			return 14;
1725 		} else if (strcmp(op, "||") == 0) {
1726 			return 15;
1727 		} else {
1728 			do_warning("unknown op '%s'", op);
1729 			return -1;
1730 		}
1731 	}
1732 }
1733 
set_op_prio(struct print_arg * arg)1734 static int set_op_prio(struct print_arg *arg)
1735 {
1736 
1737 	/* single ops are the greatest */
1738 	if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1739 		arg->op.prio = 0;
1740 	else
1741 		arg->op.prio = get_op_prio(arg->op.op);
1742 
1743 	return arg->op.prio;
1744 }
1745 
1746 /* Note, *tok does not get freed, but will most likely be saved */
1747 static enum event_type
process_op(struct event_format * event,struct print_arg * arg,char ** tok)1748 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1749 {
1750 	struct print_arg *left, *right = NULL;
1751 	enum event_type type;
1752 	char *token;
1753 
1754 	/* the op is passed in via tok */
1755 	token = *tok;
1756 
1757 	if (arg->type == PRINT_OP && !arg->op.left) {
1758 		/* handle single op */
1759 		if (token[1]) {
1760 			do_warning("bad op token %s", token);
1761 			goto out_free;
1762 		}
1763 		switch (token[0]) {
1764 		case '~':
1765 		case '!':
1766 		case '+':
1767 		case '-':
1768 			break;
1769 		default:
1770 			do_warning("bad op token %s", token);
1771 			goto out_free;
1772 
1773 		}
1774 
1775 		/* make an empty left */
1776 		left = alloc_arg();
1777 		if (!left)
1778 			goto out_warn_free;
1779 
1780 		left->type = PRINT_NULL;
1781 		arg->op.left = left;
1782 
1783 		right = alloc_arg();
1784 		if (!right)
1785 			goto out_warn_free;
1786 
1787 		arg->op.right = right;
1788 
1789 		/* do not free the token, it belongs to an op */
1790 		*tok = NULL;
1791 		type = process_arg(event, right, tok);
1792 
1793 	} else if (strcmp(token, "?") == 0) {
1794 
1795 		left = alloc_arg();
1796 		if (!left)
1797 			goto out_warn_free;
1798 
1799 		/* copy the top arg to the left */
1800 		*left = *arg;
1801 
1802 		arg->type = PRINT_OP;
1803 		arg->op.op = token;
1804 		arg->op.left = left;
1805 		arg->op.prio = 0;
1806 
1807 		/* it will set arg->op.right */
1808 		type = process_cond(event, arg, tok);
1809 
1810 	} else if (strcmp(token, ">>") == 0 ||
1811 		   strcmp(token, "<<") == 0 ||
1812 		   strcmp(token, "&") == 0 ||
1813 		   strcmp(token, "|") == 0 ||
1814 		   strcmp(token, "&&") == 0 ||
1815 		   strcmp(token, "||") == 0 ||
1816 		   strcmp(token, "-") == 0 ||
1817 		   strcmp(token, "+") == 0 ||
1818 		   strcmp(token, "*") == 0 ||
1819 		   strcmp(token, "^") == 0 ||
1820 		   strcmp(token, "/") == 0 ||
1821 		   strcmp(token, "<") == 0 ||
1822 		   strcmp(token, ">") == 0 ||
1823 		   strcmp(token, "<=") == 0 ||
1824 		   strcmp(token, ">=") == 0 ||
1825 		   strcmp(token, "==") == 0 ||
1826 		   strcmp(token, "!=") == 0) {
1827 
1828 		left = alloc_arg();
1829 		if (!left)
1830 			goto out_warn_free;
1831 
1832 		/* copy the top arg to the left */
1833 		*left = *arg;
1834 
1835 		arg->type = PRINT_OP;
1836 		arg->op.op = token;
1837 		arg->op.left = left;
1838 		arg->op.right = NULL;
1839 
1840 		if (set_op_prio(arg) == -1) {
1841 			event->flags |= EVENT_FL_FAILED;
1842 			/* arg->op.op (= token) will be freed at out_free */
1843 			arg->op.op = NULL;
1844 			goto out_free;
1845 		}
1846 
1847 		type = read_token_item(&token);
1848 		*tok = token;
1849 
1850 		/* could just be a type pointer */
1851 		if ((strcmp(arg->op.op, "*") == 0) &&
1852 		    type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1853 			char *new_atom;
1854 
1855 			if (left->type != PRINT_ATOM) {
1856 				do_warning("bad pointer type");
1857 				goto out_free;
1858 			}
1859 			new_atom = realloc(left->atom.atom,
1860 					    strlen(left->atom.atom) + 3);
1861 			if (!new_atom)
1862 				goto out_warn_free;
1863 
1864 			left->atom.atom = new_atom;
1865 			strcat(left->atom.atom, " *");
1866 			free(arg->op.op);
1867 			*arg = *left;
1868 			free(left);
1869 
1870 			return type;
1871 		}
1872 
1873 		right = alloc_arg();
1874 		if (!right)
1875 			goto out_warn_free;
1876 
1877 		type = process_arg_token(event, right, tok, type);
1878 		arg->op.right = right;
1879 
1880 	} else if (strcmp(token, "[") == 0) {
1881 
1882 		left = alloc_arg();
1883 		if (!left)
1884 			goto out_warn_free;
1885 
1886 		*left = *arg;
1887 
1888 		arg->type = PRINT_OP;
1889 		arg->op.op = token;
1890 		arg->op.left = left;
1891 
1892 		arg->op.prio = 0;
1893 
1894 		/* it will set arg->op.right */
1895 		type = process_array(event, arg, tok);
1896 
1897 	} else {
1898 		do_warning("unknown op '%s'", token);
1899 		event->flags |= EVENT_FL_FAILED;
1900 		/* the arg is now the left side */
1901 		goto out_free;
1902 	}
1903 
1904 	if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1905 		int prio;
1906 
1907 		/* higher prios need to be closer to the root */
1908 		prio = get_op_prio(*tok);
1909 
1910 		if (prio > arg->op.prio)
1911 			return process_op(event, arg, tok);
1912 
1913 		return process_op(event, right, tok);
1914 	}
1915 
1916 	return type;
1917 
1918 out_warn_free:
1919 	do_warning("%s: not enough memory!", __func__);
1920 out_free:
1921 	free_token(token);
1922 	*tok = NULL;
1923 	return EVENT_ERROR;
1924 }
1925 
1926 static enum event_type
process_entry(struct event_format * event __maybe_unused,struct print_arg * arg,char ** tok)1927 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1928 	      char **tok)
1929 {
1930 	enum event_type type;
1931 	char *field;
1932 	char *token;
1933 
1934 	if (read_expected(EVENT_OP, "->") < 0)
1935 		goto out_err;
1936 
1937 	if (read_expect_type(EVENT_ITEM, &token) < 0)
1938 		goto out_free;
1939 	field = token;
1940 
1941 	arg->type = PRINT_FIELD;
1942 	arg->field.name = field;
1943 
1944 	if (is_flag_field) {
1945 		arg->field.field = pevent_find_any_field(event, arg->field.name);
1946 		arg->field.field->flags |= FIELD_IS_FLAG;
1947 		is_flag_field = 0;
1948 	} else if (is_symbolic_field) {
1949 		arg->field.field = pevent_find_any_field(event, arg->field.name);
1950 		arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1951 		is_symbolic_field = 0;
1952 	}
1953 
1954 	type = read_token(&token);
1955 	*tok = token;
1956 
1957 	return type;
1958 
1959  out_free:
1960 	free_token(token);
1961  out_err:
1962 	*tok = NULL;
1963 	return EVENT_ERROR;
1964 }
1965 
1966 static char *arg_eval (struct print_arg *arg);
1967 
1968 static unsigned long long
eval_type_str(unsigned long long val,const char * type,int pointer)1969 eval_type_str(unsigned long long val, const char *type, int pointer)
1970 {
1971 	int sign = 0;
1972 	char *ref;
1973 	int len;
1974 
1975 	len = strlen(type);
1976 
1977 	if (pointer) {
1978 
1979 		if (type[len-1] != '*') {
1980 			do_warning("pointer expected with non pointer type");
1981 			return val;
1982 		}
1983 
1984 		ref = malloc(len);
1985 		if (!ref) {
1986 			do_warning("%s: not enough memory!", __func__);
1987 			return val;
1988 		}
1989 		memcpy(ref, type, len);
1990 
1991 		/* chop off the " *" */
1992 		ref[len - 2] = 0;
1993 
1994 		val = eval_type_str(val, ref, 0);
1995 		free(ref);
1996 		return val;
1997 	}
1998 
1999 	/* check if this is a pointer */
2000 	if (type[len - 1] == '*')
2001 		return val;
2002 
2003 	/* Try to figure out the arg size*/
2004 	if (strncmp(type, "struct", 6) == 0)
2005 		/* all bets off */
2006 		return val;
2007 
2008 	if (strcmp(type, "u8") == 0)
2009 		return val & 0xff;
2010 
2011 	if (strcmp(type, "u16") == 0)
2012 		return val & 0xffff;
2013 
2014 	if (strcmp(type, "u32") == 0)
2015 		return val & 0xffffffff;
2016 
2017 	if (strcmp(type, "u64") == 0 ||
2018 	    strcmp(type, "s64"))
2019 		return val;
2020 
2021 	if (strcmp(type, "s8") == 0)
2022 		return (unsigned long long)(char)val & 0xff;
2023 
2024 	if (strcmp(type, "s16") == 0)
2025 		return (unsigned long long)(short)val & 0xffff;
2026 
2027 	if (strcmp(type, "s32") == 0)
2028 		return (unsigned long long)(int)val & 0xffffffff;
2029 
2030 	if (strncmp(type, "unsigned ", 9) == 0) {
2031 		sign = 0;
2032 		type += 9;
2033 	}
2034 
2035 	if (strcmp(type, "char") == 0) {
2036 		if (sign)
2037 			return (unsigned long long)(char)val & 0xff;
2038 		else
2039 			return val & 0xff;
2040 	}
2041 
2042 	if (strcmp(type, "short") == 0) {
2043 		if (sign)
2044 			return (unsigned long long)(short)val & 0xffff;
2045 		else
2046 			return val & 0xffff;
2047 	}
2048 
2049 	if (strcmp(type, "int") == 0) {
2050 		if (sign)
2051 			return (unsigned long long)(int)val & 0xffffffff;
2052 		else
2053 			return val & 0xffffffff;
2054 	}
2055 
2056 	return val;
2057 }
2058 
2059 /*
2060  * Try to figure out the type.
2061  */
2062 static unsigned long long
eval_type(unsigned long long val,struct print_arg * arg,int pointer)2063 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2064 {
2065 	if (arg->type != PRINT_TYPE) {
2066 		do_warning("expected type argument");
2067 		return 0;
2068 	}
2069 
2070 	return eval_type_str(val, arg->typecast.type, pointer);
2071 }
2072 
arg_num_eval(struct print_arg * arg,long long * val)2073 static int arg_num_eval(struct print_arg *arg, long long *val)
2074 {
2075 	long long left, right;
2076 	int ret = 1;
2077 
2078 	switch (arg->type) {
2079 	case PRINT_ATOM:
2080 		*val = strtoll(arg->atom.atom, NULL, 0);
2081 		break;
2082 	case PRINT_TYPE:
2083 		ret = arg_num_eval(arg->typecast.item, val);
2084 		if (!ret)
2085 			break;
2086 		*val = eval_type(*val, arg, 0);
2087 		break;
2088 	case PRINT_OP:
2089 		switch (arg->op.op[0]) {
2090 		case '|':
2091 			ret = arg_num_eval(arg->op.left, &left);
2092 			if (!ret)
2093 				break;
2094 			ret = arg_num_eval(arg->op.right, &right);
2095 			if (!ret)
2096 				break;
2097 			if (arg->op.op[1])
2098 				*val = left || right;
2099 			else
2100 				*val = left | right;
2101 			break;
2102 		case '&':
2103 			ret = arg_num_eval(arg->op.left, &left);
2104 			if (!ret)
2105 				break;
2106 			ret = arg_num_eval(arg->op.right, &right);
2107 			if (!ret)
2108 				break;
2109 			if (arg->op.op[1])
2110 				*val = left && right;
2111 			else
2112 				*val = left & right;
2113 			break;
2114 		case '<':
2115 			ret = arg_num_eval(arg->op.left, &left);
2116 			if (!ret)
2117 				break;
2118 			ret = arg_num_eval(arg->op.right, &right);
2119 			if (!ret)
2120 				break;
2121 			switch (arg->op.op[1]) {
2122 			case 0:
2123 				*val = left < right;
2124 				break;
2125 			case '<':
2126 				*val = left << right;
2127 				break;
2128 			case '=':
2129 				*val = left <= right;
2130 				break;
2131 			default:
2132 				do_warning("unknown op '%s'", arg->op.op);
2133 				ret = 0;
2134 			}
2135 			break;
2136 		case '>':
2137 			ret = arg_num_eval(arg->op.left, &left);
2138 			if (!ret)
2139 				break;
2140 			ret = arg_num_eval(arg->op.right, &right);
2141 			if (!ret)
2142 				break;
2143 			switch (arg->op.op[1]) {
2144 			case 0:
2145 				*val = left > right;
2146 				break;
2147 			case '>':
2148 				*val = left >> right;
2149 				break;
2150 			case '=':
2151 				*val = left >= right;
2152 				break;
2153 			default:
2154 				do_warning("unknown op '%s'", arg->op.op);
2155 				ret = 0;
2156 			}
2157 			break;
2158 		case '=':
2159 			ret = arg_num_eval(arg->op.left, &left);
2160 			if (!ret)
2161 				break;
2162 			ret = arg_num_eval(arg->op.right, &right);
2163 			if (!ret)
2164 				break;
2165 
2166 			if (arg->op.op[1] != '=') {
2167 				do_warning("unknown op '%s'", arg->op.op);
2168 				ret = 0;
2169 			} else
2170 				*val = left == right;
2171 			break;
2172 		case '!':
2173 			ret = arg_num_eval(arg->op.left, &left);
2174 			if (!ret)
2175 				break;
2176 			ret = arg_num_eval(arg->op.right, &right);
2177 			if (!ret)
2178 				break;
2179 
2180 			switch (arg->op.op[1]) {
2181 			case '=':
2182 				*val = left != right;
2183 				break;
2184 			default:
2185 				do_warning("unknown op '%s'", arg->op.op);
2186 				ret = 0;
2187 			}
2188 			break;
2189 		case '-':
2190 			/* check for negative */
2191 			if (arg->op.left->type == PRINT_NULL)
2192 				left = 0;
2193 			else
2194 				ret = arg_num_eval(arg->op.left, &left);
2195 			if (!ret)
2196 				break;
2197 			ret = arg_num_eval(arg->op.right, &right);
2198 			if (!ret)
2199 				break;
2200 			*val = left - right;
2201 			break;
2202 		case '+':
2203 			if (arg->op.left->type == PRINT_NULL)
2204 				left = 0;
2205 			else
2206 				ret = arg_num_eval(arg->op.left, &left);
2207 			if (!ret)
2208 				break;
2209 			ret = arg_num_eval(arg->op.right, &right);
2210 			if (!ret)
2211 				break;
2212 			*val = left + right;
2213 			break;
2214 		default:
2215 			do_warning("unknown op '%s'", arg->op.op);
2216 			ret = 0;
2217 		}
2218 		break;
2219 
2220 	case PRINT_NULL:
2221 	case PRINT_FIELD ... PRINT_SYMBOL:
2222 	case PRINT_STRING:
2223 	case PRINT_BSTRING:
2224 	default:
2225 		do_warning("invalid eval type %d", arg->type);
2226 		ret = 0;
2227 
2228 	}
2229 	return ret;
2230 }
2231 
arg_eval(struct print_arg * arg)2232 static char *arg_eval (struct print_arg *arg)
2233 {
2234 	long long val;
2235 	static char buf[20];
2236 
2237 	switch (arg->type) {
2238 	case PRINT_ATOM:
2239 		return arg->atom.atom;
2240 	case PRINT_TYPE:
2241 		return arg_eval(arg->typecast.item);
2242 	case PRINT_OP:
2243 		if (!arg_num_eval(arg, &val))
2244 			break;
2245 		sprintf(buf, "%lld", val);
2246 		return buf;
2247 
2248 	case PRINT_NULL:
2249 	case PRINT_FIELD ... PRINT_SYMBOL:
2250 	case PRINT_STRING:
2251 	case PRINT_BSTRING:
2252 	default:
2253 		do_warning("invalid eval type %d", arg->type);
2254 		break;
2255 	}
2256 
2257 	return NULL;
2258 }
2259 
2260 static enum event_type
process_fields(struct event_format * event,struct print_flag_sym ** list,char ** tok)2261 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2262 {
2263 	enum event_type type;
2264 	struct print_arg *arg = NULL;
2265 	struct print_flag_sym *field;
2266 	char *token = *tok;
2267 	char *value;
2268 
2269 	do {
2270 		free_token(token);
2271 		type = read_token_item(&token);
2272 		if (test_type_token(type, token, EVENT_OP, "{"))
2273 			break;
2274 
2275 		arg = alloc_arg();
2276 		if (!arg)
2277 			goto out_free;
2278 
2279 		free_token(token);
2280 		type = process_arg(event, arg, &token);
2281 
2282 		if (type == EVENT_OP)
2283 			type = process_op(event, arg, &token);
2284 
2285 		if (type == EVENT_ERROR)
2286 			goto out_free;
2287 
2288 		if (test_type_token(type, token, EVENT_DELIM, ","))
2289 			goto out_free;
2290 
2291 		field = calloc(1, sizeof(*field));
2292 		if (!field)
2293 			goto out_free;
2294 
2295 		value = arg_eval(arg);
2296 		if (value == NULL)
2297 			goto out_free_field;
2298 		field->value = strdup(value);
2299 		if (field->value == NULL)
2300 			goto out_free_field;
2301 
2302 		free_arg(arg);
2303 		arg = alloc_arg();
2304 		if (!arg)
2305 			goto out_free;
2306 
2307 		free_token(token);
2308 		type = process_arg(event, arg, &token);
2309 		if (test_type_token(type, token, EVENT_OP, "}"))
2310 			goto out_free_field;
2311 
2312 		value = arg_eval(arg);
2313 		if (value == NULL)
2314 			goto out_free_field;
2315 		field->str = strdup(value);
2316 		if (field->str == NULL)
2317 			goto out_free_field;
2318 		free_arg(arg);
2319 		arg = NULL;
2320 
2321 		*list = field;
2322 		list = &field->next;
2323 
2324 		free_token(token);
2325 		type = read_token_item(&token);
2326 	} while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2327 
2328 	*tok = token;
2329 	return type;
2330 
2331 out_free_field:
2332 	free_flag_sym(field);
2333 out_free:
2334 	free_arg(arg);
2335 	free_token(token);
2336 	*tok = NULL;
2337 
2338 	return EVENT_ERROR;
2339 }
2340 
2341 static enum event_type
process_flags(struct event_format * event,struct print_arg * arg,char ** tok)2342 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2343 {
2344 	struct print_arg *field;
2345 	enum event_type type;
2346 	char *token;
2347 
2348 	memset(arg, 0, sizeof(*arg));
2349 	arg->type = PRINT_FLAGS;
2350 
2351 	field = alloc_arg();
2352 	if (!field) {
2353 		do_warning("%s: not enough memory!", __func__);
2354 		goto out_free;
2355 	}
2356 
2357 	type = process_arg(event, field, &token);
2358 
2359 	/* Handle operations in the first argument */
2360 	while (type == EVENT_OP)
2361 		type = process_op(event, field, &token);
2362 
2363 	if (test_type_token(type, token, EVENT_DELIM, ","))
2364 		goto out_free_field;
2365 	free_token(token);
2366 
2367 	arg->flags.field = field;
2368 
2369 	type = read_token_item(&token);
2370 	if (event_item_type(type)) {
2371 		arg->flags.delim = token;
2372 		type = read_token_item(&token);
2373 	}
2374 
2375 	if (test_type_token(type, token, EVENT_DELIM, ","))
2376 		goto out_free;
2377 
2378 	type = process_fields(event, &arg->flags.flags, &token);
2379 	if (test_type_token(type, token, EVENT_DELIM, ")"))
2380 		goto out_free;
2381 
2382 	free_token(token);
2383 	type = read_token_item(tok);
2384 	return type;
2385 
2386 out_free_field:
2387 	free_arg(field);
2388 out_free:
2389 	free_token(token);
2390 	*tok = NULL;
2391 	return EVENT_ERROR;
2392 }
2393 
2394 static enum event_type
process_symbols(struct event_format * event,struct print_arg * arg,char ** tok)2395 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2396 {
2397 	struct print_arg *field;
2398 	enum event_type type;
2399 	char *token;
2400 
2401 	memset(arg, 0, sizeof(*arg));
2402 	arg->type = PRINT_SYMBOL;
2403 
2404 	field = alloc_arg();
2405 	if (!field) {
2406 		do_warning("%s: not enough memory!", __func__);
2407 		goto out_free;
2408 	}
2409 
2410 	type = process_arg(event, field, &token);
2411 	if (test_type_token(type, token, EVENT_DELIM, ","))
2412 		goto out_free_field;
2413 
2414 	arg->symbol.field = field;
2415 
2416 	type = process_fields(event, &arg->symbol.symbols, &token);
2417 	if (test_type_token(type, token, EVENT_DELIM, ")"))
2418 		goto out_free;
2419 
2420 	free_token(token);
2421 	type = read_token_item(tok);
2422 	return type;
2423 
2424 out_free_field:
2425 	free_arg(field);
2426 out_free:
2427 	free_token(token);
2428 	*tok = NULL;
2429 	return EVENT_ERROR;
2430 }
2431 
2432 static enum event_type
process_hex(struct event_format * event,struct print_arg * arg,char ** tok)2433 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2434 {
2435 	struct print_arg *field;
2436 	enum event_type type;
2437 	char *token;
2438 
2439 	memset(arg, 0, sizeof(*arg));
2440 	arg->type = PRINT_HEX;
2441 
2442 	field = alloc_arg();
2443 	if (!field) {
2444 		do_warning("%s: not enough memory!", __func__);
2445 		goto out_free;
2446 	}
2447 
2448 	type = process_arg(event, field, &token);
2449 
2450 	if (test_type_token(type, token, EVENT_DELIM, ","))
2451 		goto out_free;
2452 
2453 	arg->hex.field = field;
2454 
2455 	free_token(token);
2456 
2457 	field = alloc_arg();
2458 	if (!field) {
2459 		do_warning("%s: not enough memory!", __func__);
2460 		*tok = NULL;
2461 		return EVENT_ERROR;
2462 	}
2463 
2464 	type = process_arg(event, field, &token);
2465 
2466 	if (test_type_token(type, token, EVENT_DELIM, ")"))
2467 		goto out_free;
2468 
2469 	arg->hex.size = field;
2470 
2471 	free_token(token);
2472 	type = read_token_item(tok);
2473 	return type;
2474 
2475  out_free:
2476 	free_arg(field);
2477 	free_token(token);
2478 	*tok = NULL;
2479 	return EVENT_ERROR;
2480 }
2481 
2482 static enum event_type
process_dynamic_array(struct event_format * event,struct print_arg * arg,char ** tok)2483 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2484 {
2485 	struct format_field *field;
2486 	enum event_type type;
2487 	char *token;
2488 
2489 	memset(arg, 0, sizeof(*arg));
2490 	arg->type = PRINT_DYNAMIC_ARRAY;
2491 
2492 	/*
2493 	 * The item within the parenthesis is another field that holds
2494 	 * the index into where the array starts.
2495 	 */
2496 	type = read_token(&token);
2497 	*tok = token;
2498 	if (type != EVENT_ITEM)
2499 		goto out_free;
2500 
2501 	/* Find the field */
2502 
2503 	field = pevent_find_field(event, token);
2504 	if (!field)
2505 		goto out_free;
2506 
2507 	arg->dynarray.field = field;
2508 	arg->dynarray.index = 0;
2509 
2510 	if (read_expected(EVENT_DELIM, ")") < 0)
2511 		goto out_free;
2512 
2513 	free_token(token);
2514 	type = read_token_item(&token);
2515 	*tok = token;
2516 	if (type != EVENT_OP || strcmp(token, "[") != 0)
2517 		return type;
2518 
2519 	free_token(token);
2520 	arg = alloc_arg();
2521 	if (!arg) {
2522 		do_warning("%s: not enough memory!", __func__);
2523 		*tok = NULL;
2524 		return EVENT_ERROR;
2525 	}
2526 
2527 	type = process_arg(event, arg, &token);
2528 	if (type == EVENT_ERROR)
2529 		goto out_free_arg;
2530 
2531 	if (!test_type_token(type, token, EVENT_OP, "]"))
2532 		goto out_free_arg;
2533 
2534 	free_token(token);
2535 	type = read_token_item(tok);
2536 	return type;
2537 
2538  out_free_arg:
2539 	free_arg(arg);
2540  out_free:
2541 	free_token(token);
2542 	*tok = NULL;
2543 	return EVENT_ERROR;
2544 }
2545 
2546 static enum event_type
process_paren(struct event_format * event,struct print_arg * arg,char ** tok)2547 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2548 {
2549 	struct print_arg *item_arg;
2550 	enum event_type type;
2551 	char *token;
2552 
2553 	type = process_arg(event, arg, &token);
2554 
2555 	if (type == EVENT_ERROR)
2556 		goto out_free;
2557 
2558 	if (type == EVENT_OP)
2559 		type = process_op(event, arg, &token);
2560 
2561 	if (type == EVENT_ERROR)
2562 		goto out_free;
2563 
2564 	if (test_type_token(type, token, EVENT_DELIM, ")"))
2565 		goto out_free;
2566 
2567 	free_token(token);
2568 	type = read_token_item(&token);
2569 
2570 	/*
2571 	 * If the next token is an item or another open paren, then
2572 	 * this was a typecast.
2573 	 */
2574 	if (event_item_type(type) ||
2575 	    (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2576 
2577 		/* make this a typecast and contine */
2578 
2579 		/* prevous must be an atom */
2580 		if (arg->type != PRINT_ATOM) {
2581 			do_warning("previous needed to be PRINT_ATOM");
2582 			goto out_free;
2583 		}
2584 
2585 		item_arg = alloc_arg();
2586 		if (!item_arg) {
2587 			do_warning("%s: not enough memory!", __func__);
2588 			goto out_free;
2589 		}
2590 
2591 		arg->type = PRINT_TYPE;
2592 		arg->typecast.type = arg->atom.atom;
2593 		arg->typecast.item = item_arg;
2594 		type = process_arg_token(event, item_arg, &token, type);
2595 
2596 	}
2597 
2598 	*tok = token;
2599 	return type;
2600 
2601  out_free:
2602 	free_token(token);
2603 	*tok = NULL;
2604 	return EVENT_ERROR;
2605 }
2606 
2607 
2608 static enum event_type
process_str(struct event_format * event __maybe_unused,struct print_arg * arg,char ** tok)2609 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2610 	    char **tok)
2611 {
2612 	enum event_type type;
2613 	char *token;
2614 
2615 	if (read_expect_type(EVENT_ITEM, &token) < 0)
2616 		goto out_free;
2617 
2618 	arg->type = PRINT_STRING;
2619 	arg->string.string = token;
2620 	arg->string.offset = -1;
2621 
2622 	if (read_expected(EVENT_DELIM, ")") < 0)
2623 		goto out_err;
2624 
2625 	type = read_token(&token);
2626 	*tok = token;
2627 
2628 	return type;
2629 
2630  out_free:
2631 	free_token(token);
2632  out_err:
2633 	*tok = NULL;
2634 	return EVENT_ERROR;
2635 }
2636 
2637 static struct pevent_function_handler *
find_func_handler(struct pevent * pevent,char * func_name)2638 find_func_handler(struct pevent *pevent, char *func_name)
2639 {
2640 	struct pevent_function_handler *func;
2641 
2642 	if (!pevent)
2643 		return NULL;
2644 
2645 	for (func = pevent->func_handlers; func; func = func->next) {
2646 		if (strcmp(func->name, func_name) == 0)
2647 			break;
2648 	}
2649 
2650 	return func;
2651 }
2652 
remove_func_handler(struct pevent * pevent,char * func_name)2653 static void remove_func_handler(struct pevent *pevent, char *func_name)
2654 {
2655 	struct pevent_function_handler *func;
2656 	struct pevent_function_handler **next;
2657 
2658 	next = &pevent->func_handlers;
2659 	while ((func = *next)) {
2660 		if (strcmp(func->name, func_name) == 0) {
2661 			*next = func->next;
2662 			free_func_handle(func);
2663 			break;
2664 		}
2665 		next = &func->next;
2666 	}
2667 }
2668 
2669 static enum event_type
process_func_handler(struct event_format * event,struct pevent_function_handler * func,struct print_arg * arg,char ** tok)2670 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2671 		     struct print_arg *arg, char **tok)
2672 {
2673 	struct print_arg **next_arg;
2674 	struct print_arg *farg;
2675 	enum event_type type;
2676 	char *token;
2677 	const char *test;
2678 	int i;
2679 
2680 	arg->type = PRINT_FUNC;
2681 	arg->func.func = func;
2682 
2683 	*tok = NULL;
2684 
2685 	next_arg = &(arg->func.args);
2686 	for (i = 0; i < func->nr_args; i++) {
2687 		farg = alloc_arg();
2688 		if (!farg) {
2689 			do_warning("%s: not enough memory!", __func__);
2690 			return EVENT_ERROR;
2691 		}
2692 
2693 		type = process_arg(event, farg, &token);
2694 		if (i < (func->nr_args - 1))
2695 			test = ",";
2696 		else
2697 			test = ")";
2698 
2699 		if (test_type_token(type, token, EVENT_DELIM, test)) {
2700 			free_arg(farg);
2701 			free_token(token);
2702 			return EVENT_ERROR;
2703 		}
2704 
2705 		*next_arg = farg;
2706 		next_arg = &(farg->next);
2707 		free_token(token);
2708 	}
2709 
2710 	type = read_token(&token);
2711 	*tok = token;
2712 
2713 	return type;
2714 }
2715 
2716 static enum event_type
process_function(struct event_format * event,struct print_arg * arg,char * token,char ** tok)2717 process_function(struct event_format *event, struct print_arg *arg,
2718 		 char *token, char **tok)
2719 {
2720 	struct pevent_function_handler *func;
2721 
2722 	if (strcmp(token, "__print_flags") == 0) {
2723 		free_token(token);
2724 		is_flag_field = 1;
2725 		return process_flags(event, arg, tok);
2726 	}
2727 	if (strcmp(token, "__print_symbolic") == 0) {
2728 		free_token(token);
2729 		is_symbolic_field = 1;
2730 		return process_symbols(event, arg, tok);
2731 	}
2732 	if (strcmp(token, "__print_hex") == 0) {
2733 		free_token(token);
2734 		return process_hex(event, arg, tok);
2735 	}
2736 	if (strcmp(token, "__get_str") == 0) {
2737 		free_token(token);
2738 		return process_str(event, arg, tok);
2739 	}
2740 	if (strcmp(token, "__get_dynamic_array") == 0) {
2741 		free_token(token);
2742 		return process_dynamic_array(event, arg, tok);
2743 	}
2744 
2745 	func = find_func_handler(event->pevent, token);
2746 	if (func) {
2747 		free_token(token);
2748 		return process_func_handler(event, func, arg, tok);
2749 	}
2750 
2751 	do_warning("function %s not defined", token);
2752 	free_token(token);
2753 	return EVENT_ERROR;
2754 }
2755 
2756 static enum event_type
process_arg_token(struct event_format * event,struct print_arg * arg,char ** tok,enum event_type type)2757 process_arg_token(struct event_format *event, struct print_arg *arg,
2758 		  char **tok, enum event_type type)
2759 {
2760 	char *token;
2761 	char *atom;
2762 
2763 	token = *tok;
2764 
2765 	switch (type) {
2766 	case EVENT_ITEM:
2767 		if (strcmp(token, "REC") == 0) {
2768 			free_token(token);
2769 			type = process_entry(event, arg, &token);
2770 			break;
2771 		}
2772 		atom = token;
2773 		/* test the next token */
2774 		type = read_token_item(&token);
2775 
2776 		/*
2777 		 * If the next token is a parenthesis, then this
2778 		 * is a function.
2779 		 */
2780 		if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2781 			free_token(token);
2782 			token = NULL;
2783 			/* this will free atom. */
2784 			type = process_function(event, arg, atom, &token);
2785 			break;
2786 		}
2787 		/* atoms can be more than one token long */
2788 		while (type == EVENT_ITEM) {
2789 			char *new_atom;
2790 			new_atom = realloc(atom,
2791 					   strlen(atom) + strlen(token) + 2);
2792 			if (!new_atom) {
2793 				free(atom);
2794 				*tok = NULL;
2795 				free_token(token);
2796 				return EVENT_ERROR;
2797 			}
2798 			atom = new_atom;
2799 			strcat(atom, " ");
2800 			strcat(atom, token);
2801 			free_token(token);
2802 			type = read_token_item(&token);
2803 		}
2804 
2805 		arg->type = PRINT_ATOM;
2806 		arg->atom.atom = atom;
2807 		break;
2808 
2809 	case EVENT_DQUOTE:
2810 	case EVENT_SQUOTE:
2811 		arg->type = PRINT_ATOM;
2812 		arg->atom.atom = token;
2813 		type = read_token_item(&token);
2814 		break;
2815 	case EVENT_DELIM:
2816 		if (strcmp(token, "(") == 0) {
2817 			free_token(token);
2818 			type = process_paren(event, arg, &token);
2819 			break;
2820 		}
2821 	case EVENT_OP:
2822 		/* handle single ops */
2823 		arg->type = PRINT_OP;
2824 		arg->op.op = token;
2825 		arg->op.left = NULL;
2826 		type = process_op(event, arg, &token);
2827 
2828 		/* On error, the op is freed */
2829 		if (type == EVENT_ERROR)
2830 			arg->op.op = NULL;
2831 
2832 		/* return error type if errored */
2833 		break;
2834 
2835 	case EVENT_ERROR ... EVENT_NEWLINE:
2836 	default:
2837 		do_warning("unexpected type %d", type);
2838 		return EVENT_ERROR;
2839 	}
2840 	*tok = token;
2841 
2842 	return type;
2843 }
2844 
event_read_print_args(struct event_format * event,struct print_arg ** list)2845 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2846 {
2847 	enum event_type type = EVENT_ERROR;
2848 	struct print_arg *arg;
2849 	char *token;
2850 	int args = 0;
2851 
2852 	do {
2853 		if (type == EVENT_NEWLINE) {
2854 			type = read_token_item(&token);
2855 			continue;
2856 		}
2857 
2858 		arg = alloc_arg();
2859 		if (!arg) {
2860 			do_warning("%s: not enough memory!", __func__);
2861 			return -1;
2862 		}
2863 
2864 		type = process_arg(event, arg, &token);
2865 
2866 		if (type == EVENT_ERROR) {
2867 			free_token(token);
2868 			free_arg(arg);
2869 			return -1;
2870 		}
2871 
2872 		*list = arg;
2873 		args++;
2874 
2875 		if (type == EVENT_OP) {
2876 			type = process_op(event, arg, &token);
2877 			free_token(token);
2878 			if (type == EVENT_ERROR) {
2879 				*list = NULL;
2880 				free_arg(arg);
2881 				return -1;
2882 			}
2883 			list = &arg->next;
2884 			continue;
2885 		}
2886 
2887 		if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2888 			free_token(token);
2889 			*list = arg;
2890 			list = &arg->next;
2891 			continue;
2892 		}
2893 		break;
2894 	} while (type != EVENT_NONE);
2895 
2896 	if (type != EVENT_NONE && type != EVENT_ERROR)
2897 		free_token(token);
2898 
2899 	return args;
2900 }
2901 
event_read_print(struct event_format * event)2902 static int event_read_print(struct event_format *event)
2903 {
2904 	enum event_type type;
2905 	char *token;
2906 	int ret;
2907 
2908 	if (read_expected_item(EVENT_ITEM, "print") < 0)
2909 		return -1;
2910 
2911 	if (read_expected(EVENT_ITEM, "fmt") < 0)
2912 		return -1;
2913 
2914 	if (read_expected(EVENT_OP, ":") < 0)
2915 		return -1;
2916 
2917 	if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2918 		goto fail;
2919 
2920  concat:
2921 	event->print_fmt.format = token;
2922 	event->print_fmt.args = NULL;
2923 
2924 	/* ok to have no arg */
2925 	type = read_token_item(&token);
2926 
2927 	if (type == EVENT_NONE)
2928 		return 0;
2929 
2930 	/* Handle concatenation of print lines */
2931 	if (type == EVENT_DQUOTE) {
2932 		char *cat;
2933 
2934 		if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2935 			goto fail;
2936 		free_token(token);
2937 		free_token(event->print_fmt.format);
2938 		event->print_fmt.format = NULL;
2939 		token = cat;
2940 		goto concat;
2941 	}
2942 
2943 	if (test_type_token(type, token, EVENT_DELIM, ","))
2944 		goto fail;
2945 
2946 	free_token(token);
2947 
2948 	ret = event_read_print_args(event, &event->print_fmt.args);
2949 	if (ret < 0)
2950 		return -1;
2951 
2952 	return ret;
2953 
2954  fail:
2955 	free_token(token);
2956 	return -1;
2957 }
2958 
2959 /**
2960  * pevent_find_common_field - return a common field by event
2961  * @event: handle for the event
2962  * @name: the name of the common field to return
2963  *
2964  * Returns a common field from the event by the given @name.
2965  * This only searchs the common fields and not all field.
2966  */
2967 struct format_field *
pevent_find_common_field(struct event_format * event,const char * name)2968 pevent_find_common_field(struct event_format *event, const char *name)
2969 {
2970 	struct format_field *format;
2971 
2972 	for (format = event->format.common_fields;
2973 	     format; format = format->next) {
2974 		if (strcmp(format->name, name) == 0)
2975 			break;
2976 	}
2977 
2978 	return format;
2979 }
2980 
2981 /**
2982  * pevent_find_field - find a non-common field
2983  * @event: handle for the event
2984  * @name: the name of the non-common field
2985  *
2986  * Returns a non-common field by the given @name.
2987  * This does not search common fields.
2988  */
2989 struct format_field *
pevent_find_field(struct event_format * event,const char * name)2990 pevent_find_field(struct event_format *event, const char *name)
2991 {
2992 	struct format_field *format;
2993 
2994 	for (format = event->format.fields;
2995 	     format; format = format->next) {
2996 		if (strcmp(format->name, name) == 0)
2997 			break;
2998 	}
2999 
3000 	return format;
3001 }
3002 
3003 /**
3004  * pevent_find_any_field - find any field by name
3005  * @event: handle for the event
3006  * @name: the name of the field
3007  *
3008  * Returns a field by the given @name.
3009  * This searchs the common field names first, then
3010  * the non-common ones if a common one was not found.
3011  */
3012 struct format_field *
pevent_find_any_field(struct event_format * event,const char * name)3013 pevent_find_any_field(struct event_format *event, const char *name)
3014 {
3015 	struct format_field *format;
3016 
3017 	format = pevent_find_common_field(event, name);
3018 	if (format)
3019 		return format;
3020 	return pevent_find_field(event, name);
3021 }
3022 
3023 /**
3024  * pevent_read_number - read a number from data
3025  * @pevent: handle for the pevent
3026  * @ptr: the raw data
3027  * @size: the size of the data that holds the number
3028  *
3029  * Returns the number (converted to host) from the
3030  * raw data.
3031  */
pevent_read_number(struct pevent * pevent,const void * ptr,int size)3032 unsigned long long pevent_read_number(struct pevent *pevent,
3033 				      const void *ptr, int size)
3034 {
3035 	switch (size) {
3036 	case 1:
3037 		return *(unsigned char *)ptr;
3038 	case 2:
3039 		return data2host2(pevent, ptr);
3040 	case 4:
3041 		return data2host4(pevent, ptr);
3042 	case 8:
3043 		return data2host8(pevent, ptr);
3044 	default:
3045 		/* BUG! */
3046 		return 0;
3047 	}
3048 }
3049 
3050 /**
3051  * pevent_read_number_field - read a number from data
3052  * @field: a handle to the field
3053  * @data: the raw data to read
3054  * @value: the value to place the number in
3055  *
3056  * Reads raw data according to a field offset and size,
3057  * and translates it into @value.
3058  *
3059  * Returns 0 on success, -1 otherwise.
3060  */
pevent_read_number_field(struct format_field * field,const void * data,unsigned long long * value)3061 int pevent_read_number_field(struct format_field *field, const void *data,
3062 			     unsigned long long *value)
3063 {
3064 	if (!field)
3065 		return -1;
3066 	switch (field->size) {
3067 	case 1:
3068 	case 2:
3069 	case 4:
3070 	case 8:
3071 		*value = pevent_read_number(field->event->pevent,
3072 					    data + field->offset, field->size);
3073 		return 0;
3074 	default:
3075 		return -1;
3076 	}
3077 }
3078 
get_common_info(struct pevent * pevent,const char * type,int * offset,int * size)3079 static int get_common_info(struct pevent *pevent,
3080 			   const char *type, int *offset, int *size)
3081 {
3082 	struct event_format *event;
3083 	struct format_field *field;
3084 
3085 	/*
3086 	 * All events should have the same common elements.
3087 	 * Pick any event to find where the type is;
3088 	 */
3089 	if (!pevent->events) {
3090 		do_warning("no event_list!");
3091 		return -1;
3092 	}
3093 
3094 	event = pevent->events[0];
3095 	field = pevent_find_common_field(event, type);
3096 	if (!field)
3097 		return -1;
3098 
3099 	*offset = field->offset;
3100 	*size = field->size;
3101 
3102 	return 0;
3103 }
3104 
__parse_common(struct pevent * pevent,void * data,int * size,int * offset,const char * name)3105 static int __parse_common(struct pevent *pevent, void *data,
3106 			  int *size, int *offset, const char *name)
3107 {
3108 	int ret;
3109 
3110 	if (!*size) {
3111 		ret = get_common_info(pevent, name, offset, size);
3112 		if (ret < 0)
3113 			return ret;
3114 	}
3115 	return pevent_read_number(pevent, data + *offset, *size);
3116 }
3117 
trace_parse_common_type(struct pevent * pevent,void * data)3118 static int trace_parse_common_type(struct pevent *pevent, void *data)
3119 {
3120 	return __parse_common(pevent, data,
3121 			      &pevent->type_size, &pevent->type_offset,
3122 			      "common_type");
3123 }
3124 
parse_common_pid(struct pevent * pevent,void * data)3125 static int parse_common_pid(struct pevent *pevent, void *data)
3126 {
3127 	return __parse_common(pevent, data,
3128 			      &pevent->pid_size, &pevent->pid_offset,
3129 			      "common_pid");
3130 }
3131 
parse_common_pc(struct pevent * pevent,void * data)3132 static int parse_common_pc(struct pevent *pevent, void *data)
3133 {
3134 	return __parse_common(pevent, data,
3135 			      &pevent->pc_size, &pevent->pc_offset,
3136 			      "common_preempt_count");
3137 }
3138 
parse_common_flags(struct pevent * pevent,void * data)3139 static int parse_common_flags(struct pevent *pevent, void *data)
3140 {
3141 	return __parse_common(pevent, data,
3142 			      &pevent->flags_size, &pevent->flags_offset,
3143 			      "common_flags");
3144 }
3145 
parse_common_lock_depth(struct pevent * pevent,void * data)3146 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3147 {
3148 	return __parse_common(pevent, data,
3149 			      &pevent->ld_size, &pevent->ld_offset,
3150 			      "common_lock_depth");
3151 }
3152 
parse_common_migrate_disable(struct pevent * pevent,void * data)3153 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3154 {
3155 	return __parse_common(pevent, data,
3156 			      &pevent->ld_size, &pevent->ld_offset,
3157 			      "common_migrate_disable");
3158 }
3159 
3160 static int events_id_cmp(const void *a, const void *b);
3161 
3162 /**
3163  * pevent_find_event - find an event by given id
3164  * @pevent: a handle to the pevent
3165  * @id: the id of the event
3166  *
3167  * Returns an event that has a given @id.
3168  */
pevent_find_event(struct pevent * pevent,int id)3169 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3170 {
3171 	struct event_format **eventptr;
3172 	struct event_format key;
3173 	struct event_format *pkey = &key;
3174 
3175 	/* Check cache first */
3176 	if (pevent->last_event && pevent->last_event->id == id)
3177 		return pevent->last_event;
3178 
3179 	key.id = id;
3180 
3181 	eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3182 			   sizeof(*pevent->events), events_id_cmp);
3183 
3184 	if (eventptr) {
3185 		pevent->last_event = *eventptr;
3186 		return *eventptr;
3187 	}
3188 
3189 	return NULL;
3190 }
3191 
3192 /**
3193  * pevent_find_event_by_name - find an event by given name
3194  * @pevent: a handle to the pevent
3195  * @sys: the system name to search for
3196  * @name: the name of the event to search for
3197  *
3198  * This returns an event with a given @name and under the system
3199  * @sys. If @sys is NULL the first event with @name is returned.
3200  */
3201 struct event_format *
pevent_find_event_by_name(struct pevent * pevent,const char * sys,const char * name)3202 pevent_find_event_by_name(struct pevent *pevent,
3203 			  const char *sys, const char *name)
3204 {
3205 	struct event_format *event;
3206 	int i;
3207 
3208 	if (pevent->last_event &&
3209 	    strcmp(pevent->last_event->name, name) == 0 &&
3210 	    (!sys || strcmp(pevent->last_event->system, sys) == 0))
3211 		return pevent->last_event;
3212 
3213 	for (i = 0; i < pevent->nr_events; i++) {
3214 		event = pevent->events[i];
3215 		if (strcmp(event->name, name) == 0) {
3216 			if (!sys)
3217 				break;
3218 			if (strcmp(event->system, sys) == 0)
3219 				break;
3220 		}
3221 	}
3222 	if (i == pevent->nr_events)
3223 		event = NULL;
3224 
3225 	pevent->last_event = event;
3226 	return event;
3227 }
3228 
3229 static unsigned long long
eval_num_arg(void * data,int size,struct event_format * event,struct print_arg * arg)3230 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3231 {
3232 	struct pevent *pevent = event->pevent;
3233 	unsigned long long val = 0;
3234 	unsigned long long left, right;
3235 	struct print_arg *typearg = NULL;
3236 	struct print_arg *larg;
3237 	unsigned long offset;
3238 	unsigned int field_size;
3239 
3240 	switch (arg->type) {
3241 	case PRINT_NULL:
3242 		/* ?? */
3243 		return 0;
3244 	case PRINT_ATOM:
3245 		return strtoull(arg->atom.atom, NULL, 0);
3246 	case PRINT_FIELD:
3247 		if (!arg->field.field) {
3248 			arg->field.field = pevent_find_any_field(event, arg->field.name);
3249 			if (!arg->field.field)
3250 				goto out_warning_field;
3251 
3252 		}
3253 		/* must be a number */
3254 		val = pevent_read_number(pevent, data + arg->field.field->offset,
3255 				arg->field.field->size);
3256 		break;
3257 	case PRINT_FLAGS:
3258 	case PRINT_SYMBOL:
3259 	case PRINT_HEX:
3260 		break;
3261 	case PRINT_TYPE:
3262 		val = eval_num_arg(data, size, event, arg->typecast.item);
3263 		return eval_type(val, arg, 0);
3264 	case PRINT_STRING:
3265 	case PRINT_BSTRING:
3266 		return 0;
3267 	case PRINT_FUNC: {
3268 		struct trace_seq s;
3269 		trace_seq_init(&s);
3270 		val = process_defined_func(&s, data, size, event, arg);
3271 		trace_seq_destroy(&s);
3272 		return val;
3273 	}
3274 	case PRINT_OP:
3275 		if (strcmp(arg->op.op, "[") == 0) {
3276 			/*
3277 			 * Arrays are special, since we don't want
3278 			 * to read the arg as is.
3279 			 */
3280 			right = eval_num_arg(data, size, event, arg->op.right);
3281 
3282 			/* handle typecasts */
3283 			larg = arg->op.left;
3284 			while (larg->type == PRINT_TYPE) {
3285 				if (!typearg)
3286 					typearg = larg;
3287 				larg = larg->typecast.item;
3288 			}
3289 
3290 			/* Default to long size */
3291 			field_size = pevent->long_size;
3292 
3293 			switch (larg->type) {
3294 			case PRINT_DYNAMIC_ARRAY:
3295 				offset = pevent_read_number(pevent,
3296 						   data + larg->dynarray.field->offset,
3297 						   larg->dynarray.field->size);
3298 				if (larg->dynarray.field->elementsize)
3299 					field_size = larg->dynarray.field->elementsize;
3300 				/*
3301 				 * The actual length of the dynamic array is stored
3302 				 * in the top half of the field, and the offset
3303 				 * is in the bottom half of the 32 bit field.
3304 				 */
3305 				offset &= 0xffff;
3306 				offset += right;
3307 				break;
3308 			case PRINT_FIELD:
3309 				if (!larg->field.field) {
3310 					larg->field.field =
3311 						pevent_find_any_field(event, larg->field.name);
3312 					if (!larg->field.field) {
3313 						arg = larg;
3314 						goto out_warning_field;
3315 					}
3316 				}
3317 				field_size = larg->field.field->elementsize;
3318 				offset = larg->field.field->offset +
3319 					right * larg->field.field->elementsize;
3320 				break;
3321 			default:
3322 				goto default_op; /* oops, all bets off */
3323 			}
3324 			val = pevent_read_number(pevent,
3325 						 data + offset, field_size);
3326 			if (typearg)
3327 				val = eval_type(val, typearg, 1);
3328 			break;
3329 		} else if (strcmp(arg->op.op, "?") == 0) {
3330 			left = eval_num_arg(data, size, event, arg->op.left);
3331 			arg = arg->op.right;
3332 			if (left)
3333 				val = eval_num_arg(data, size, event, arg->op.left);
3334 			else
3335 				val = eval_num_arg(data, size, event, arg->op.right);
3336 			break;
3337 		}
3338  default_op:
3339 		left = eval_num_arg(data, size, event, arg->op.left);
3340 		right = eval_num_arg(data, size, event, arg->op.right);
3341 		switch (arg->op.op[0]) {
3342 		case '!':
3343 			switch (arg->op.op[1]) {
3344 			case 0:
3345 				val = !right;
3346 				break;
3347 			case '=':
3348 				val = left != right;
3349 				break;
3350 			default:
3351 				goto out_warning_op;
3352 			}
3353 			break;
3354 		case '~':
3355 			val = ~right;
3356 			break;
3357 		case '|':
3358 			if (arg->op.op[1])
3359 				val = left || right;
3360 			else
3361 				val = left | right;
3362 			break;
3363 		case '&':
3364 			if (arg->op.op[1])
3365 				val = left && right;
3366 			else
3367 				val = left & right;
3368 			break;
3369 		case '<':
3370 			switch (arg->op.op[1]) {
3371 			case 0:
3372 				val = left < right;
3373 				break;
3374 			case '<':
3375 				val = left << right;
3376 				break;
3377 			case '=':
3378 				val = left <= right;
3379 				break;
3380 			default:
3381 				goto out_warning_op;
3382 			}
3383 			break;
3384 		case '>':
3385 			switch (arg->op.op[1]) {
3386 			case 0:
3387 				val = left > right;
3388 				break;
3389 			case '>':
3390 				val = left >> right;
3391 				break;
3392 			case '=':
3393 				val = left >= right;
3394 				break;
3395 			default:
3396 				goto out_warning_op;
3397 			}
3398 			break;
3399 		case '=':
3400 			if (arg->op.op[1] != '=')
3401 				goto out_warning_op;
3402 
3403 			val = left == right;
3404 			break;
3405 		case '-':
3406 			val = left - right;
3407 			break;
3408 		case '+':
3409 			val = left + right;
3410 			break;
3411 		case '/':
3412 			val = left / right;
3413 			break;
3414 		case '*':
3415 			val = left * right;
3416 			break;
3417 		default:
3418 			goto out_warning_op;
3419 		}
3420 		break;
3421 	default: /* not sure what to do there */
3422 		return 0;
3423 	}
3424 	return val;
3425 
3426 out_warning_op:
3427 	do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3428 	return 0;
3429 
3430 out_warning_field:
3431 	do_warning("%s: field %s not found", __func__, arg->field.name);
3432 	return 0;
3433 }
3434 
3435 struct flag {
3436 	const char *name;
3437 	unsigned long long value;
3438 };
3439 
3440 static const struct flag flags[] = {
3441 	{ "HI_SOFTIRQ", 0 },
3442 	{ "TIMER_SOFTIRQ", 1 },
3443 	{ "NET_TX_SOFTIRQ", 2 },
3444 	{ "NET_RX_SOFTIRQ", 3 },
3445 	{ "BLOCK_SOFTIRQ", 4 },
3446 	{ "BLOCK_IOPOLL_SOFTIRQ", 5 },
3447 	{ "TASKLET_SOFTIRQ", 6 },
3448 	{ "SCHED_SOFTIRQ", 7 },
3449 	{ "HRTIMER_SOFTIRQ", 8 },
3450 	{ "RCU_SOFTIRQ", 9 },
3451 
3452 	{ "HRTIMER_NORESTART", 0 },
3453 	{ "HRTIMER_RESTART", 1 },
3454 };
3455 
eval_flag(const char * flag)3456 static unsigned long long eval_flag(const char *flag)
3457 {
3458 	int i;
3459 
3460 	/*
3461 	 * Some flags in the format files do not get converted.
3462 	 * If the flag is not numeric, see if it is something that
3463 	 * we already know about.
3464 	 */
3465 	if (isdigit(flag[0]))
3466 		return strtoull(flag, NULL, 0);
3467 
3468 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3469 		if (strcmp(flags[i].name, flag) == 0)
3470 			return flags[i].value;
3471 
3472 	return 0;
3473 }
3474 
print_str_to_seq(struct trace_seq * s,const char * format,int len_arg,const char * str)3475 static void print_str_to_seq(struct trace_seq *s, const char *format,
3476 			     int len_arg, const char *str)
3477 {
3478 	if (len_arg >= 0)
3479 		trace_seq_printf(s, format, len_arg, str);
3480 	else
3481 		trace_seq_printf(s, format, str);
3482 }
3483 
print_str_arg(struct trace_seq * s,void * data,int size,struct event_format * event,const char * format,int len_arg,struct print_arg * arg)3484 static void print_str_arg(struct trace_seq *s, void *data, int size,
3485 			  struct event_format *event, const char *format,
3486 			  int len_arg, struct print_arg *arg)
3487 {
3488 	struct pevent *pevent = event->pevent;
3489 	struct print_flag_sym *flag;
3490 	struct format_field *field;
3491 	unsigned long long val, fval;
3492 	unsigned long addr;
3493 	char *str;
3494 	unsigned char *hex;
3495 	int print;
3496 	int i, len;
3497 
3498 	switch (arg->type) {
3499 	case PRINT_NULL:
3500 		/* ?? */
3501 		return;
3502 	case PRINT_ATOM:
3503 		print_str_to_seq(s, format, len_arg, arg->atom.atom);
3504 		return;
3505 	case PRINT_FIELD:
3506 		field = arg->field.field;
3507 		if (!field) {
3508 			field = pevent_find_any_field(event, arg->field.name);
3509 			if (!field) {
3510 				str = arg->field.name;
3511 				goto out_warning_field;
3512 			}
3513 			arg->field.field = field;
3514 		}
3515 		/* Zero sized fields, mean the rest of the data */
3516 		len = field->size ? : size - field->offset;
3517 
3518 		/*
3519 		 * Some events pass in pointers. If this is not an array
3520 		 * and the size is the same as long_size, assume that it
3521 		 * is a pointer.
3522 		 */
3523 		if (!(field->flags & FIELD_IS_ARRAY) &&
3524 		    field->size == pevent->long_size) {
3525 			addr = *(unsigned long *)(data + field->offset);
3526 			trace_seq_printf(s, "%lx", addr);
3527 			break;
3528 		}
3529 		str = malloc(len + 1);
3530 		if (!str) {
3531 			do_warning("%s: not enough memory!", __func__);
3532 			return;
3533 		}
3534 		memcpy(str, data + field->offset, len);
3535 		str[len] = 0;
3536 		print_str_to_seq(s, format, len_arg, str);
3537 		free(str);
3538 		break;
3539 	case PRINT_FLAGS:
3540 		val = eval_num_arg(data, size, event, arg->flags.field);
3541 		print = 0;
3542 		for (flag = arg->flags.flags; flag; flag = flag->next) {
3543 			fval = eval_flag(flag->value);
3544 			if (!val && !fval) {
3545 				print_str_to_seq(s, format, len_arg, flag->str);
3546 				break;
3547 			}
3548 			if (fval && (val & fval) == fval) {
3549 				if (print && arg->flags.delim)
3550 					trace_seq_puts(s, arg->flags.delim);
3551 				print_str_to_seq(s, format, len_arg, flag->str);
3552 				print = 1;
3553 				val &= ~fval;
3554 			}
3555 		}
3556 		break;
3557 	case PRINT_SYMBOL:
3558 		val = eval_num_arg(data, size, event, arg->symbol.field);
3559 		for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3560 			fval = eval_flag(flag->value);
3561 			if (val == fval) {
3562 				print_str_to_seq(s, format, len_arg, flag->str);
3563 				break;
3564 			}
3565 		}
3566 		break;
3567 	case PRINT_HEX:
3568 		field = arg->hex.field->field.field;
3569 		if (!field) {
3570 			str = arg->hex.field->field.name;
3571 			field = pevent_find_any_field(event, str);
3572 			if (!field)
3573 				goto out_warning_field;
3574 			arg->hex.field->field.field = field;
3575 		}
3576 		hex = data + field->offset;
3577 		len = eval_num_arg(data, size, event, arg->hex.size);
3578 		for (i = 0; i < len; i++) {
3579 			if (i)
3580 				trace_seq_putc(s, ' ');
3581 			trace_seq_printf(s, "%02x", hex[i]);
3582 		}
3583 		break;
3584 
3585 	case PRINT_TYPE:
3586 		break;
3587 	case PRINT_STRING: {
3588 		int str_offset;
3589 
3590 		if (arg->string.offset == -1) {
3591 			struct format_field *f;
3592 
3593 			f = pevent_find_any_field(event, arg->string.string);
3594 			arg->string.offset = f->offset;
3595 		}
3596 		str_offset = data2host4(pevent, data + arg->string.offset);
3597 		str_offset &= 0xffff;
3598 		print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3599 		break;
3600 	}
3601 	case PRINT_BSTRING:
3602 		print_str_to_seq(s, format, len_arg, arg->string.string);
3603 		break;
3604 	case PRINT_OP:
3605 		/*
3606 		 * The only op for string should be ? :
3607 		 */
3608 		if (arg->op.op[0] != '?')
3609 			return;
3610 		val = eval_num_arg(data, size, event, arg->op.left);
3611 		if (val)
3612 			print_str_arg(s, data, size, event,
3613 				      format, len_arg, arg->op.right->op.left);
3614 		else
3615 			print_str_arg(s, data, size, event,
3616 				      format, len_arg, arg->op.right->op.right);
3617 		break;
3618 	case PRINT_FUNC:
3619 		process_defined_func(s, data, size, event, arg);
3620 		break;
3621 	default:
3622 		/* well... */
3623 		break;
3624 	}
3625 
3626 	return;
3627 
3628 out_warning_field:
3629 	do_warning("%s: field %s not found", __func__, arg->field.name);
3630 }
3631 
3632 static unsigned long long
process_defined_func(struct trace_seq * s,void * data,int size,struct event_format * event,struct print_arg * arg)3633 process_defined_func(struct trace_seq *s, void *data, int size,
3634 		     struct event_format *event, struct print_arg *arg)
3635 {
3636 	struct pevent_function_handler *func_handle = arg->func.func;
3637 	struct pevent_func_params *param;
3638 	unsigned long long *args;
3639 	unsigned long long ret;
3640 	struct print_arg *farg;
3641 	struct trace_seq str;
3642 	struct save_str {
3643 		struct save_str *next;
3644 		char *str;
3645 	} *strings = NULL, *string;
3646 	int i;
3647 
3648 	if (!func_handle->nr_args) {
3649 		ret = (*func_handle->func)(s, NULL);
3650 		goto out;
3651 	}
3652 
3653 	farg = arg->func.args;
3654 	param = func_handle->params;
3655 
3656 	ret = ULLONG_MAX;
3657 	args = malloc(sizeof(*args) * func_handle->nr_args);
3658 	if (!args)
3659 		goto out;
3660 
3661 	for (i = 0; i < func_handle->nr_args; i++) {
3662 		switch (param->type) {
3663 		case PEVENT_FUNC_ARG_INT:
3664 		case PEVENT_FUNC_ARG_LONG:
3665 		case PEVENT_FUNC_ARG_PTR:
3666 			args[i] = eval_num_arg(data, size, event, farg);
3667 			break;
3668 		case PEVENT_FUNC_ARG_STRING:
3669 			trace_seq_init(&str);
3670 			print_str_arg(&str, data, size, event, "%s", -1, farg);
3671 			trace_seq_terminate(&str);
3672 			string = malloc(sizeof(*string));
3673 			if (!string) {
3674 				do_warning("%s(%d): malloc str", __func__, __LINE__);
3675 				goto out_free;
3676 			}
3677 			string->next = strings;
3678 			string->str = strdup(str.buffer);
3679 			if (!string->str) {
3680 				free(string);
3681 				do_warning("%s(%d): malloc str", __func__, __LINE__);
3682 				goto out_free;
3683 			}
3684 			args[i] = (uintptr_t)string->str;
3685 			strings = string;
3686 			trace_seq_destroy(&str);
3687 			break;
3688 		default:
3689 			/*
3690 			 * Something went totally wrong, this is not
3691 			 * an input error, something in this code broke.
3692 			 */
3693 			do_warning("Unexpected end of arguments\n");
3694 			goto out_free;
3695 		}
3696 		farg = farg->next;
3697 		param = param->next;
3698 	}
3699 
3700 	ret = (*func_handle->func)(s, args);
3701 out_free:
3702 	free(args);
3703 	while (strings) {
3704 		string = strings;
3705 		strings = string->next;
3706 		free(string->str);
3707 		free(string);
3708 	}
3709 
3710  out:
3711 	/* TBD : handle return type here */
3712 	return ret;
3713 }
3714 
free_args(struct print_arg * args)3715 static void free_args(struct print_arg *args)
3716 {
3717 	struct print_arg *next;
3718 
3719 	while (args) {
3720 		next = args->next;
3721 
3722 		free_arg(args);
3723 		args = next;
3724 	}
3725 }
3726 
make_bprint_args(char * fmt,void * data,int size,struct event_format * event)3727 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3728 {
3729 	struct pevent *pevent = event->pevent;
3730 	struct format_field *field, *ip_field;
3731 	struct print_arg *args, *arg, **next;
3732 	unsigned long long ip, val;
3733 	char *ptr;
3734 	void *bptr;
3735 	int vsize;
3736 
3737 	field = pevent->bprint_buf_field;
3738 	ip_field = pevent->bprint_ip_field;
3739 
3740 	if (!field) {
3741 		field = pevent_find_field(event, "buf");
3742 		if (!field) {
3743 			do_warning("can't find buffer field for binary printk");
3744 			return NULL;
3745 		}
3746 		ip_field = pevent_find_field(event, "ip");
3747 		if (!ip_field) {
3748 			do_warning("can't find ip field for binary printk");
3749 			return NULL;
3750 		}
3751 		pevent->bprint_buf_field = field;
3752 		pevent->bprint_ip_field = ip_field;
3753 	}
3754 
3755 	ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3756 
3757 	/*
3758 	 * The first arg is the IP pointer.
3759 	 */
3760 	args = alloc_arg();
3761 	if (!args) {
3762 		do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3763 		return NULL;
3764 	}
3765 	arg = args;
3766 	arg->next = NULL;
3767 	next = &arg->next;
3768 
3769 	arg->type = PRINT_ATOM;
3770 
3771 	if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3772 		goto out_free;
3773 
3774 	/* skip the first "%pf : " */
3775 	for (ptr = fmt + 6, bptr = data + field->offset;
3776 	     bptr < data + size && *ptr; ptr++) {
3777 		int ls = 0;
3778 
3779 		if (*ptr == '%') {
3780  process_again:
3781 			ptr++;
3782 			switch (*ptr) {
3783 			case '%':
3784 				break;
3785 			case 'l':
3786 				ls++;
3787 				goto process_again;
3788 			case 'L':
3789 				ls = 2;
3790 				goto process_again;
3791 			case '0' ... '9':
3792 				goto process_again;
3793 			case '.':
3794 				goto process_again;
3795 			case 'p':
3796 				ls = 1;
3797 				/* fall through */
3798 			case 'd':
3799 			case 'u':
3800 			case 'x':
3801 			case 'i':
3802 				switch (ls) {
3803 				case 0:
3804 					vsize = 4;
3805 					break;
3806 				case 1:
3807 					vsize = pevent->long_size;
3808 					break;
3809 				case 2:
3810 					vsize = 8;
3811 					break;
3812 				default:
3813 					vsize = ls; /* ? */
3814 					break;
3815 				}
3816 			/* fall through */
3817 			case '*':
3818 				if (*ptr == '*')
3819 					vsize = 4;
3820 
3821 				/* the pointers are always 4 bytes aligned */
3822 				bptr = (void *)(((unsigned long)bptr + 3) &
3823 						~3);
3824 				val = pevent_read_number(pevent, bptr, vsize);
3825 				bptr += vsize;
3826 				arg = alloc_arg();
3827 				if (!arg) {
3828 					do_warning("%s(%d): not enough memory!",
3829 						   __func__, __LINE__);
3830 					goto out_free;
3831 				}
3832 				arg->next = NULL;
3833 				arg->type = PRINT_ATOM;
3834 				if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3835 					free(arg);
3836 					goto out_free;
3837 				}
3838 				*next = arg;
3839 				next = &arg->next;
3840 				/*
3841 				 * The '*' case means that an arg is used as the length.
3842 				 * We need to continue to figure out for what.
3843 				 */
3844 				if (*ptr == '*')
3845 					goto process_again;
3846 
3847 				break;
3848 			case 's':
3849 				arg = alloc_arg();
3850 				if (!arg) {
3851 					do_warning("%s(%d): not enough memory!",
3852 						   __func__, __LINE__);
3853 					goto out_free;
3854 				}
3855 				arg->next = NULL;
3856 				arg->type = PRINT_BSTRING;
3857 				arg->string.string = strdup(bptr);
3858 				if (!arg->string.string)
3859 					goto out_free;
3860 				bptr += strlen(bptr) + 1;
3861 				*next = arg;
3862 				next = &arg->next;
3863 			default:
3864 				break;
3865 			}
3866 		}
3867 	}
3868 
3869 	return args;
3870 
3871 out_free:
3872 	free_args(args);
3873 	return NULL;
3874 }
3875 
3876 static char *
get_bprint_format(void * data,int size __maybe_unused,struct event_format * event)3877 get_bprint_format(void *data, int size __maybe_unused,
3878 		  struct event_format *event)
3879 {
3880 	struct pevent *pevent = event->pevent;
3881 	unsigned long long addr;
3882 	struct format_field *field;
3883 	struct printk_map *printk;
3884 	char *format;
3885 	char *p;
3886 
3887 	field = pevent->bprint_fmt_field;
3888 
3889 	if (!field) {
3890 		field = pevent_find_field(event, "fmt");
3891 		if (!field) {
3892 			do_warning("can't find format field for binary printk");
3893 			return NULL;
3894 		}
3895 		pevent->bprint_fmt_field = field;
3896 	}
3897 
3898 	addr = pevent_read_number(pevent, data + field->offset, field->size);
3899 
3900 	printk = find_printk(pevent, addr);
3901 	if (!printk) {
3902 		if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3903 			return NULL;
3904 		return format;
3905 	}
3906 
3907 	p = printk->printk;
3908 	/* Remove any quotes. */
3909 	if (*p == '"')
3910 		p++;
3911 	if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3912 		return NULL;
3913 	/* remove ending quotes and new line since we will add one too */
3914 	p = format + strlen(format) - 1;
3915 	if (*p == '"')
3916 		*p = 0;
3917 
3918 	p -= 2;
3919 	if (strcmp(p, "\\n") == 0)
3920 		*p = 0;
3921 
3922 	return format;
3923 }
3924 
print_mac_arg(struct trace_seq * s,int mac,void * data,int size,struct event_format * event,struct print_arg * arg)3925 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3926 			  struct event_format *event, struct print_arg *arg)
3927 {
3928 	unsigned char *buf;
3929 	const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3930 
3931 	if (arg->type == PRINT_FUNC) {
3932 		process_defined_func(s, data, size, event, arg);
3933 		return;
3934 	}
3935 
3936 	if (arg->type != PRINT_FIELD) {
3937 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3938 				 arg->type);
3939 		return;
3940 	}
3941 
3942 	if (mac == 'm')
3943 		fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3944 	if (!arg->field.field) {
3945 		arg->field.field =
3946 			pevent_find_any_field(event, arg->field.name);
3947 		if (!arg->field.field) {
3948 			do_warning("%s: field %s not found",
3949 				   __func__, arg->field.name);
3950 			return;
3951 		}
3952 	}
3953 	if (arg->field.field->size != 6) {
3954 		trace_seq_printf(s, "INVALIDMAC");
3955 		return;
3956 	}
3957 	buf = data + arg->field.field->offset;
3958 	trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3959 }
3960 
is_printable_array(char * p,unsigned int len)3961 static int is_printable_array(char *p, unsigned int len)
3962 {
3963 	unsigned int i;
3964 
3965 	for (i = 0; i < len && p[i]; i++)
3966 		if (!isprint(p[i]))
3967 		    return 0;
3968 	return 1;
3969 }
3970 
print_event_fields(struct trace_seq * s,void * data,int size __maybe_unused,struct event_format * event)3971 static void print_event_fields(struct trace_seq *s, void *data,
3972 			       int size __maybe_unused,
3973 			       struct event_format *event)
3974 {
3975 	struct format_field *field;
3976 	unsigned long long val;
3977 	unsigned int offset, len, i;
3978 
3979 	field = event->format.fields;
3980 	while (field) {
3981 		trace_seq_printf(s, " %s=", field->name);
3982 		if (field->flags & FIELD_IS_ARRAY) {
3983 			offset = field->offset;
3984 			len = field->size;
3985 			if (field->flags & FIELD_IS_DYNAMIC) {
3986 				val = pevent_read_number(event->pevent, data + offset, len);
3987 				offset = val;
3988 				len = offset >> 16;
3989 				offset &= 0xffff;
3990 			}
3991 			if (field->flags & FIELD_IS_STRING &&
3992 			    is_printable_array(data + offset, len)) {
3993 				trace_seq_printf(s, "%s", (char *)data + offset);
3994 			} else {
3995 				trace_seq_puts(s, "ARRAY[");
3996 				for (i = 0; i < len; i++) {
3997 					if (i)
3998 						trace_seq_puts(s, ", ");
3999 					trace_seq_printf(s, "%02x",
4000 							 *((unsigned char *)data + offset + i));
4001 				}
4002 				trace_seq_putc(s, ']');
4003 				field->flags &= ~FIELD_IS_STRING;
4004 			}
4005 		} else {
4006 			val = pevent_read_number(event->pevent, data + field->offset,
4007 						 field->size);
4008 			if (field->flags & FIELD_IS_POINTER) {
4009 				trace_seq_printf(s, "0x%llx", val);
4010 			} else if (field->flags & FIELD_IS_SIGNED) {
4011 				switch (field->size) {
4012 				case 4:
4013 					/*
4014 					 * If field is long then print it in hex.
4015 					 * A long usually stores pointers.
4016 					 */
4017 					if (field->flags & FIELD_IS_LONG)
4018 						trace_seq_printf(s, "0x%x", (int)val);
4019 					else
4020 						trace_seq_printf(s, "%d", (int)val);
4021 					break;
4022 				case 2:
4023 					trace_seq_printf(s, "%2d", (short)val);
4024 					break;
4025 				case 1:
4026 					trace_seq_printf(s, "%1d", (char)val);
4027 					break;
4028 				default:
4029 					trace_seq_printf(s, "%lld", val);
4030 				}
4031 			} else {
4032 				if (field->flags & FIELD_IS_LONG)
4033 					trace_seq_printf(s, "0x%llx", val);
4034 				else
4035 					trace_seq_printf(s, "%llu", val);
4036 			}
4037 		}
4038 		field = field->next;
4039 	}
4040 }
4041 
pretty_print(struct trace_seq * s,void * data,int size,struct event_format * event)4042 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4043 {
4044 	struct pevent *pevent = event->pevent;
4045 	struct print_fmt *print_fmt = &event->print_fmt;
4046 	struct print_arg *arg = print_fmt->args;
4047 	struct print_arg *args = NULL;
4048 	const char *ptr = print_fmt->format;
4049 	unsigned long long val;
4050 	struct func_map *func;
4051 	const char *saveptr;
4052 	char *bprint_fmt = NULL;
4053 	char format[32];
4054 	int show_func;
4055 	int len_as_arg;
4056 	int len_arg;
4057 	int len;
4058 	int ls;
4059 
4060 	if (event->flags & EVENT_FL_FAILED) {
4061 		trace_seq_printf(s, "[FAILED TO PARSE]");
4062 		print_event_fields(s, data, size, event);
4063 		return;
4064 	}
4065 
4066 	if (event->flags & EVENT_FL_ISBPRINT) {
4067 		bprint_fmt = get_bprint_format(data, size, event);
4068 		args = make_bprint_args(bprint_fmt, data, size, event);
4069 		arg = args;
4070 		ptr = bprint_fmt;
4071 	}
4072 
4073 	for (; *ptr; ptr++) {
4074 		ls = 0;
4075 		if (*ptr == '\\') {
4076 			ptr++;
4077 			switch (*ptr) {
4078 			case 'n':
4079 				trace_seq_putc(s, '\n');
4080 				break;
4081 			case 't':
4082 				trace_seq_putc(s, '\t');
4083 				break;
4084 			case 'r':
4085 				trace_seq_putc(s, '\r');
4086 				break;
4087 			case '\\':
4088 				trace_seq_putc(s, '\\');
4089 				break;
4090 			default:
4091 				trace_seq_putc(s, *ptr);
4092 				break;
4093 			}
4094 
4095 		} else if (*ptr == '%') {
4096 			saveptr = ptr;
4097 			show_func = 0;
4098 			len_as_arg = 0;
4099  cont_process:
4100 			ptr++;
4101 			switch (*ptr) {
4102 			case '%':
4103 				trace_seq_putc(s, '%');
4104 				break;
4105 			case '#':
4106 				/* FIXME: need to handle properly */
4107 				goto cont_process;
4108 			case 'h':
4109 				ls--;
4110 				goto cont_process;
4111 			case 'l':
4112 				ls++;
4113 				goto cont_process;
4114 			case 'L':
4115 				ls = 2;
4116 				goto cont_process;
4117 			case '*':
4118 				/* The argument is the length. */
4119 				if (!arg) {
4120 					do_warning("no argument match");
4121 					event->flags |= EVENT_FL_FAILED;
4122 					goto out_failed;
4123 				}
4124 				len_arg = eval_num_arg(data, size, event, arg);
4125 				len_as_arg = 1;
4126 				arg = arg->next;
4127 				goto cont_process;
4128 			case '.':
4129 			case 'z':
4130 			case 'Z':
4131 			case '0' ... '9':
4132 				goto cont_process;
4133 			case 'p':
4134 				if (pevent->long_size == 4)
4135 					ls = 1;
4136 				else
4137 					ls = 2;
4138 
4139 				if (*(ptr+1) == 'F' ||
4140 				    *(ptr+1) == 'f') {
4141 					ptr++;
4142 					show_func = *ptr;
4143 				} else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4144 					print_mac_arg(s, *(ptr+1), data, size, event, arg);
4145 					ptr++;
4146 					arg = arg->next;
4147 					break;
4148 				}
4149 
4150 				/* fall through */
4151 			case 'd':
4152 			case 'i':
4153 			case 'x':
4154 			case 'X':
4155 			case 'u':
4156 				if (!arg) {
4157 					do_warning("no argument match");
4158 					event->flags |= EVENT_FL_FAILED;
4159 					goto out_failed;
4160 				}
4161 
4162 				len = ((unsigned long)ptr + 1) -
4163 					(unsigned long)saveptr;
4164 
4165 				/* should never happen */
4166 				if (len > 31) {
4167 					do_warning("bad format!");
4168 					event->flags |= EVENT_FL_FAILED;
4169 					len = 31;
4170 				}
4171 
4172 				memcpy(format, saveptr, len);
4173 				format[len] = 0;
4174 
4175 				val = eval_num_arg(data, size, event, arg);
4176 				arg = arg->next;
4177 
4178 				if (show_func) {
4179 					func = find_func(pevent, val);
4180 					if (func) {
4181 						trace_seq_puts(s, func->func);
4182 						if (show_func == 'F')
4183 							trace_seq_printf(s,
4184 							       "+0x%llx",
4185 							       val - func->addr);
4186 						break;
4187 					}
4188 				}
4189 				if (pevent->long_size == 8 && ls &&
4190 				    sizeof(long) != 8) {
4191 					char *p;
4192 
4193 					ls = 2;
4194 					/* make %l into %ll */
4195 					p = strchr(format, 'l');
4196 					if (p)
4197 						memmove(p+1, p, strlen(p)+1);
4198 					else if (strcmp(format, "%p") == 0)
4199 						strcpy(format, "0x%llx");
4200 				}
4201 				switch (ls) {
4202 				case -2:
4203 					if (len_as_arg)
4204 						trace_seq_printf(s, format, len_arg, (char)val);
4205 					else
4206 						trace_seq_printf(s, format, (char)val);
4207 					break;
4208 				case -1:
4209 					if (len_as_arg)
4210 						trace_seq_printf(s, format, len_arg, (short)val);
4211 					else
4212 						trace_seq_printf(s, format, (short)val);
4213 					break;
4214 				case 0:
4215 					if (len_as_arg)
4216 						trace_seq_printf(s, format, len_arg, (int)val);
4217 					else
4218 						trace_seq_printf(s, format, (int)val);
4219 					break;
4220 				case 1:
4221 					if (len_as_arg)
4222 						trace_seq_printf(s, format, len_arg, (long)val);
4223 					else
4224 						trace_seq_printf(s, format, (long)val);
4225 					break;
4226 				case 2:
4227 					if (len_as_arg)
4228 						trace_seq_printf(s, format, len_arg,
4229 								 (long long)val);
4230 					else
4231 						trace_seq_printf(s, format, (long long)val);
4232 					break;
4233 				default:
4234 					do_warning("bad count (%d)", ls);
4235 					event->flags |= EVENT_FL_FAILED;
4236 				}
4237 				break;
4238 			case 's':
4239 				if (!arg) {
4240 					do_warning("no matching argument");
4241 					event->flags |= EVENT_FL_FAILED;
4242 					goto out_failed;
4243 				}
4244 
4245 				len = ((unsigned long)ptr + 1) -
4246 					(unsigned long)saveptr;
4247 
4248 				/* should never happen */
4249 				if (len > 31) {
4250 					do_warning("bad format!");
4251 					event->flags |= EVENT_FL_FAILED;
4252 					len = 31;
4253 				}
4254 
4255 				memcpy(format, saveptr, len);
4256 				format[len] = 0;
4257 				if (!len_as_arg)
4258 					len_arg = -1;
4259 				print_str_arg(s, data, size, event,
4260 					      format, len_arg, arg);
4261 				arg = arg->next;
4262 				break;
4263 			default:
4264 				trace_seq_printf(s, ">%c<", *ptr);
4265 
4266 			}
4267 		} else
4268 			trace_seq_putc(s, *ptr);
4269 	}
4270 
4271 	if (event->flags & EVENT_FL_FAILED) {
4272 out_failed:
4273 		trace_seq_printf(s, "[FAILED TO PARSE]");
4274 	}
4275 
4276 	if (args) {
4277 		free_args(args);
4278 		free(bprint_fmt);
4279 	}
4280 }
4281 
4282 /**
4283  * pevent_data_lat_fmt - parse the data for the latency format
4284  * @pevent: a handle to the pevent
4285  * @s: the trace_seq to write to
4286  * @record: the record to read from
4287  *
4288  * This parses out the Latency format (interrupts disabled,
4289  * need rescheduling, in hard/soft interrupt, preempt count
4290  * and lock depth) and places it into the trace_seq.
4291  */
pevent_data_lat_fmt(struct pevent * pevent,struct trace_seq * s,struct pevent_record * record)4292 void pevent_data_lat_fmt(struct pevent *pevent,
4293 			 struct trace_seq *s, struct pevent_record *record)
4294 {
4295 	static int check_lock_depth = 1;
4296 	static int check_migrate_disable = 1;
4297 	static int lock_depth_exists;
4298 	static int migrate_disable_exists;
4299 	unsigned int lat_flags;
4300 	unsigned int pc;
4301 	int lock_depth;
4302 	int migrate_disable;
4303 	int hardirq;
4304 	int softirq;
4305 	void *data = record->data;
4306 
4307 	lat_flags = parse_common_flags(pevent, data);
4308 	pc = parse_common_pc(pevent, data);
4309 	/* lock_depth may not always exist */
4310 	if (lock_depth_exists)
4311 		lock_depth = parse_common_lock_depth(pevent, data);
4312 	else if (check_lock_depth) {
4313 		lock_depth = parse_common_lock_depth(pevent, data);
4314 		if (lock_depth < 0)
4315 			check_lock_depth = 0;
4316 		else
4317 			lock_depth_exists = 1;
4318 	}
4319 
4320 	/* migrate_disable may not always exist */
4321 	if (migrate_disable_exists)
4322 		migrate_disable = parse_common_migrate_disable(pevent, data);
4323 	else if (check_migrate_disable) {
4324 		migrate_disable = parse_common_migrate_disable(pevent, data);
4325 		if (migrate_disable < 0)
4326 			check_migrate_disable = 0;
4327 		else
4328 			migrate_disable_exists = 1;
4329 	}
4330 
4331 	hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4332 	softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4333 
4334 	trace_seq_printf(s, "%c%c%c",
4335 	       (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4336 	       (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4337 	       'X' : '.',
4338 	       (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4339 	       'N' : '.',
4340 	       (hardirq && softirq) ? 'H' :
4341 	       hardirq ? 'h' : softirq ? 's' : '.');
4342 
4343 	if (pc)
4344 		trace_seq_printf(s, "%x", pc);
4345 	else
4346 		trace_seq_putc(s, '.');
4347 
4348 	if (migrate_disable_exists) {
4349 		if (migrate_disable < 0)
4350 			trace_seq_putc(s, '.');
4351 		else
4352 			trace_seq_printf(s, "%d", migrate_disable);
4353 	}
4354 
4355 	if (lock_depth_exists) {
4356 		if (lock_depth < 0)
4357 			trace_seq_putc(s, '.');
4358 		else
4359 			trace_seq_printf(s, "%d", lock_depth);
4360 	}
4361 
4362 	trace_seq_terminate(s);
4363 }
4364 
4365 /**
4366  * pevent_data_type - parse out the given event type
4367  * @pevent: a handle to the pevent
4368  * @rec: the record to read from
4369  *
4370  * This returns the event id from the @rec.
4371  */
pevent_data_type(struct pevent * pevent,struct pevent_record * rec)4372 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4373 {
4374 	return trace_parse_common_type(pevent, rec->data);
4375 }
4376 
4377 /**
4378  * pevent_data_event_from_type - find the event by a given type
4379  * @pevent: a handle to the pevent
4380  * @type: the type of the event.
4381  *
4382  * This returns the event form a given @type;
4383  */
pevent_data_event_from_type(struct pevent * pevent,int type)4384 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4385 {
4386 	return pevent_find_event(pevent, type);
4387 }
4388 
4389 /**
4390  * pevent_data_pid - parse the PID from raw data
4391  * @pevent: a handle to the pevent
4392  * @rec: the record to parse
4393  *
4394  * This returns the PID from a raw data.
4395  */
pevent_data_pid(struct pevent * pevent,struct pevent_record * rec)4396 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4397 {
4398 	return parse_common_pid(pevent, rec->data);
4399 }
4400 
4401 /**
4402  * pevent_data_comm_from_pid - return the command line from PID
4403  * @pevent: a handle to the pevent
4404  * @pid: the PID of the task to search for
4405  *
4406  * This returns a pointer to the command line that has the given
4407  * @pid.
4408  */
pevent_data_comm_from_pid(struct pevent * pevent,int pid)4409 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4410 {
4411 	const char *comm;
4412 
4413 	comm = find_cmdline(pevent, pid);
4414 	return comm;
4415 }
4416 
4417 /**
4418  * pevent_data_comm_from_pid - parse the data into the print format
4419  * @s: the trace_seq to write to
4420  * @event: the handle to the event
4421  * @record: the record to read from
4422  *
4423  * This parses the raw @data using the given @event information and
4424  * writes the print format into the trace_seq.
4425  */
pevent_event_info(struct trace_seq * s,struct event_format * event,struct pevent_record * record)4426 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4427 		       struct pevent_record *record)
4428 {
4429 	int print_pretty = 1;
4430 
4431 	if (event->pevent->print_raw)
4432 		print_event_fields(s, record->data, record->size, event);
4433 	else {
4434 
4435 		if (event->handler)
4436 			print_pretty = event->handler(s, record, event,
4437 						      event->context);
4438 
4439 		if (print_pretty)
4440 			pretty_print(s, record->data, record->size, event);
4441 	}
4442 
4443 	trace_seq_terminate(s);
4444 }
4445 
pevent_print_event(struct pevent * pevent,struct trace_seq * s,struct pevent_record * record)4446 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4447 			struct pevent_record *record)
4448 {
4449 	static const char *spaces = "                    "; /* 20 spaces */
4450 	struct event_format *event;
4451 	unsigned long secs;
4452 	unsigned long usecs;
4453 	unsigned long nsecs;
4454 	const char *comm;
4455 	void *data = record->data;
4456 	int type;
4457 	int pid;
4458 	int len;
4459 	int p;
4460 
4461 	secs = record->ts / NSECS_PER_SEC;
4462 	nsecs = record->ts - secs * NSECS_PER_SEC;
4463 
4464 	if (record->size < 0) {
4465 		do_warning("ug! negative record size %d", record->size);
4466 		return;
4467 	}
4468 
4469 	type = trace_parse_common_type(pevent, data);
4470 
4471 	event = pevent_find_event(pevent, type);
4472 	if (!event) {
4473 		do_warning("ug! no event found for type %d", type);
4474 		return;
4475 	}
4476 
4477 	pid = parse_common_pid(pevent, data);
4478 	comm = find_cmdline(pevent, pid);
4479 
4480 	if (pevent->latency_format) {
4481 		trace_seq_printf(s, "%8.8s-%-5d %3d",
4482 		       comm, pid, record->cpu);
4483 		pevent_data_lat_fmt(pevent, s, record);
4484 	} else
4485 		trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4486 
4487 	if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4488 		usecs = nsecs;
4489 		p = 9;
4490 	} else {
4491 		usecs = (nsecs + 500) / NSECS_PER_USEC;
4492 		p = 6;
4493 	}
4494 
4495 	trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4496 
4497 	/* Space out the event names evenly. */
4498 	len = strlen(event->name);
4499 	if (len < 20)
4500 		trace_seq_printf(s, "%.*s", 20 - len, spaces);
4501 
4502 	pevent_event_info(s, event, record);
4503 }
4504 
events_id_cmp(const void * a,const void * b)4505 static int events_id_cmp(const void *a, const void *b)
4506 {
4507 	struct event_format * const * ea = a;
4508 	struct event_format * const * eb = b;
4509 
4510 	if ((*ea)->id < (*eb)->id)
4511 		return -1;
4512 
4513 	if ((*ea)->id > (*eb)->id)
4514 		return 1;
4515 
4516 	return 0;
4517 }
4518 
events_name_cmp(const void * a,const void * b)4519 static int events_name_cmp(const void *a, const void *b)
4520 {
4521 	struct event_format * const * ea = a;
4522 	struct event_format * const * eb = b;
4523 	int res;
4524 
4525 	res = strcmp((*ea)->name, (*eb)->name);
4526 	if (res)
4527 		return res;
4528 
4529 	res = strcmp((*ea)->system, (*eb)->system);
4530 	if (res)
4531 		return res;
4532 
4533 	return events_id_cmp(a, b);
4534 }
4535 
events_system_cmp(const void * a,const void * b)4536 static int events_system_cmp(const void *a, const void *b)
4537 {
4538 	struct event_format * const * ea = a;
4539 	struct event_format * const * eb = b;
4540 	int res;
4541 
4542 	res = strcmp((*ea)->system, (*eb)->system);
4543 	if (res)
4544 		return res;
4545 
4546 	res = strcmp((*ea)->name, (*eb)->name);
4547 	if (res)
4548 		return res;
4549 
4550 	return events_id_cmp(a, b);
4551 }
4552 
pevent_list_events(struct pevent * pevent,enum event_sort_type sort_type)4553 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4554 {
4555 	struct event_format **events;
4556 	int (*sort)(const void *a, const void *b);
4557 
4558 	events = pevent->sort_events;
4559 
4560 	if (events && pevent->last_type == sort_type)
4561 		return events;
4562 
4563 	if (!events) {
4564 		events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4565 		if (!events)
4566 			return NULL;
4567 
4568 		memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4569 		events[pevent->nr_events] = NULL;
4570 
4571 		pevent->sort_events = events;
4572 
4573 		/* the internal events are sorted by id */
4574 		if (sort_type == EVENT_SORT_ID) {
4575 			pevent->last_type = sort_type;
4576 			return events;
4577 		}
4578 	}
4579 
4580 	switch (sort_type) {
4581 	case EVENT_SORT_ID:
4582 		sort = events_id_cmp;
4583 		break;
4584 	case EVENT_SORT_NAME:
4585 		sort = events_name_cmp;
4586 		break;
4587 	case EVENT_SORT_SYSTEM:
4588 		sort = events_system_cmp;
4589 		break;
4590 	default:
4591 		return events;
4592 	}
4593 
4594 	qsort(events, pevent->nr_events, sizeof(*events), sort);
4595 	pevent->last_type = sort_type;
4596 
4597 	return events;
4598 }
4599 
4600 static struct format_field **
get_event_fields(const char * type,const char * name,int count,struct format_field * list)4601 get_event_fields(const char *type, const char *name,
4602 		 int count, struct format_field *list)
4603 {
4604 	struct format_field **fields;
4605 	struct format_field *field;
4606 	int i = 0;
4607 
4608 	fields = malloc(sizeof(*fields) * (count + 1));
4609 	if (!fields)
4610 		return NULL;
4611 
4612 	for (field = list; field; field = field->next) {
4613 		fields[i++] = field;
4614 		if (i == count + 1) {
4615 			do_warning("event %s has more %s fields than specified",
4616 				name, type);
4617 			i--;
4618 			break;
4619 		}
4620 	}
4621 
4622 	if (i != count)
4623 		do_warning("event %s has less %s fields than specified",
4624 			name, type);
4625 
4626 	fields[i] = NULL;
4627 
4628 	return fields;
4629 }
4630 
4631 /**
4632  * pevent_event_common_fields - return a list of common fields for an event
4633  * @event: the event to return the common fields of.
4634  *
4635  * Returns an allocated array of fields. The last item in the array is NULL.
4636  * The array must be freed with free().
4637  */
pevent_event_common_fields(struct event_format * event)4638 struct format_field **pevent_event_common_fields(struct event_format *event)
4639 {
4640 	return get_event_fields("common", event->name,
4641 				event->format.nr_common,
4642 				event->format.common_fields);
4643 }
4644 
4645 /**
4646  * pevent_event_fields - return a list of event specific fields for an event
4647  * @event: the event to return the fields of.
4648  *
4649  * Returns an allocated array of fields. The last item in the array is NULL.
4650  * The array must be freed with free().
4651  */
pevent_event_fields(struct event_format * event)4652 struct format_field **pevent_event_fields(struct event_format *event)
4653 {
4654 	return get_event_fields("event", event->name,
4655 				event->format.nr_fields,
4656 				event->format.fields);
4657 }
4658 
print_fields(struct trace_seq * s,struct print_flag_sym * field)4659 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4660 {
4661 	trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4662 	if (field->next) {
4663 		trace_seq_puts(s, ", ");
4664 		print_fields(s, field->next);
4665 	}
4666 }
4667 
4668 /* for debugging */
print_args(struct print_arg * args)4669 static void print_args(struct print_arg *args)
4670 {
4671 	int print_paren = 1;
4672 	struct trace_seq s;
4673 
4674 	switch (args->type) {
4675 	case PRINT_NULL:
4676 		printf("null");
4677 		break;
4678 	case PRINT_ATOM:
4679 		printf("%s", args->atom.atom);
4680 		break;
4681 	case PRINT_FIELD:
4682 		printf("REC->%s", args->field.name);
4683 		break;
4684 	case PRINT_FLAGS:
4685 		printf("__print_flags(");
4686 		print_args(args->flags.field);
4687 		printf(", %s, ", args->flags.delim);
4688 		trace_seq_init(&s);
4689 		print_fields(&s, args->flags.flags);
4690 		trace_seq_do_printf(&s);
4691 		trace_seq_destroy(&s);
4692 		printf(")");
4693 		break;
4694 	case PRINT_SYMBOL:
4695 		printf("__print_symbolic(");
4696 		print_args(args->symbol.field);
4697 		printf(", ");
4698 		trace_seq_init(&s);
4699 		print_fields(&s, args->symbol.symbols);
4700 		trace_seq_do_printf(&s);
4701 		trace_seq_destroy(&s);
4702 		printf(")");
4703 		break;
4704 	case PRINT_HEX:
4705 		printf("__print_hex(");
4706 		print_args(args->hex.field);
4707 		printf(", ");
4708 		print_args(args->hex.size);
4709 		printf(")");
4710 		break;
4711 	case PRINT_STRING:
4712 	case PRINT_BSTRING:
4713 		printf("__get_str(%s)", args->string.string);
4714 		break;
4715 	case PRINT_TYPE:
4716 		printf("(%s)", args->typecast.type);
4717 		print_args(args->typecast.item);
4718 		break;
4719 	case PRINT_OP:
4720 		if (strcmp(args->op.op, ":") == 0)
4721 			print_paren = 0;
4722 		if (print_paren)
4723 			printf("(");
4724 		print_args(args->op.left);
4725 		printf(" %s ", args->op.op);
4726 		print_args(args->op.right);
4727 		if (print_paren)
4728 			printf(")");
4729 		break;
4730 	default:
4731 		/* we should warn... */
4732 		return;
4733 	}
4734 	if (args->next) {
4735 		printf("\n");
4736 		print_args(args->next);
4737 	}
4738 }
4739 
parse_header_field(const char * field,int * offset,int * size,int mandatory)4740 static void parse_header_field(const char *field,
4741 			       int *offset, int *size, int mandatory)
4742 {
4743 	unsigned long long save_input_buf_ptr;
4744 	unsigned long long save_input_buf_siz;
4745 	char *token;
4746 	int type;
4747 
4748 	save_input_buf_ptr = input_buf_ptr;
4749 	save_input_buf_siz = input_buf_siz;
4750 
4751 	if (read_expected(EVENT_ITEM, "field") < 0)
4752 		return;
4753 	if (read_expected(EVENT_OP, ":") < 0)
4754 		return;
4755 
4756 	/* type */
4757 	if (read_expect_type(EVENT_ITEM, &token) < 0)
4758 		goto fail;
4759 	free_token(token);
4760 
4761 	/*
4762 	 * If this is not a mandatory field, then test it first.
4763 	 */
4764 	if (mandatory) {
4765 		if (read_expected(EVENT_ITEM, field) < 0)
4766 			return;
4767 	} else {
4768 		if (read_expect_type(EVENT_ITEM, &token) < 0)
4769 			goto fail;
4770 		if (strcmp(token, field) != 0)
4771 			goto discard;
4772 		free_token(token);
4773 	}
4774 
4775 	if (read_expected(EVENT_OP, ";") < 0)
4776 		return;
4777 	if (read_expected(EVENT_ITEM, "offset") < 0)
4778 		return;
4779 	if (read_expected(EVENT_OP, ":") < 0)
4780 		return;
4781 	if (read_expect_type(EVENT_ITEM, &token) < 0)
4782 		goto fail;
4783 	*offset = atoi(token);
4784 	free_token(token);
4785 	if (read_expected(EVENT_OP, ";") < 0)
4786 		return;
4787 	if (read_expected(EVENT_ITEM, "size") < 0)
4788 		return;
4789 	if (read_expected(EVENT_OP, ":") < 0)
4790 		return;
4791 	if (read_expect_type(EVENT_ITEM, &token) < 0)
4792 		goto fail;
4793 	*size = atoi(token);
4794 	free_token(token);
4795 	if (read_expected(EVENT_OP, ";") < 0)
4796 		return;
4797 	type = read_token(&token);
4798 	if (type != EVENT_NEWLINE) {
4799 		/* newer versions of the kernel have a "signed" type */
4800 		if (type != EVENT_ITEM)
4801 			goto fail;
4802 
4803 		if (strcmp(token, "signed") != 0)
4804 			goto fail;
4805 
4806 		free_token(token);
4807 
4808 		if (read_expected(EVENT_OP, ":") < 0)
4809 			return;
4810 
4811 		if (read_expect_type(EVENT_ITEM, &token))
4812 			goto fail;
4813 
4814 		free_token(token);
4815 		if (read_expected(EVENT_OP, ";") < 0)
4816 			return;
4817 
4818 		if (read_expect_type(EVENT_NEWLINE, &token))
4819 			goto fail;
4820 	}
4821  fail:
4822 	free_token(token);
4823 	return;
4824 
4825  discard:
4826 	input_buf_ptr = save_input_buf_ptr;
4827 	input_buf_siz = save_input_buf_siz;
4828 	*offset = 0;
4829 	*size = 0;
4830 	free_token(token);
4831 }
4832 
4833 /**
4834  * pevent_parse_header_page - parse the data stored in the header page
4835  * @pevent: the handle to the pevent
4836  * @buf: the buffer storing the header page format string
4837  * @size: the size of @buf
4838  * @long_size: the long size to use if there is no header
4839  *
4840  * This parses the header page format for information on the
4841  * ring buffer used. The @buf should be copied from
4842  *
4843  * /sys/kernel/debug/tracing/events/header_page
4844  */
pevent_parse_header_page(struct pevent * pevent,char * buf,unsigned long size,int long_size)4845 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4846 			     int long_size)
4847 {
4848 	int ignore;
4849 
4850 	if (!size) {
4851 		/*
4852 		 * Old kernels did not have header page info.
4853 		 * Sorry but we just use what we find here in user space.
4854 		 */
4855 		pevent->header_page_ts_size = sizeof(long long);
4856 		pevent->header_page_size_size = long_size;
4857 		pevent->header_page_data_offset = sizeof(long long) + long_size;
4858 		pevent->old_format = 1;
4859 		return -1;
4860 	}
4861 	init_input_buf(buf, size);
4862 
4863 	parse_header_field("timestamp", &pevent->header_page_ts_offset,
4864 			   &pevent->header_page_ts_size, 1);
4865 	parse_header_field("commit", &pevent->header_page_size_offset,
4866 			   &pevent->header_page_size_size, 1);
4867 	parse_header_field("overwrite", &pevent->header_page_overwrite,
4868 			   &ignore, 0);
4869 	parse_header_field("data", &pevent->header_page_data_offset,
4870 			   &pevent->header_page_data_size, 1);
4871 
4872 	return 0;
4873 }
4874 
event_matches(struct event_format * event,int id,const char * sys_name,const char * event_name)4875 static int event_matches(struct event_format *event,
4876 			 int id, const char *sys_name,
4877 			 const char *event_name)
4878 {
4879 	if (id >= 0 && id != event->id)
4880 		return 0;
4881 
4882 	if (event_name && (strcmp(event_name, event->name) != 0))
4883 		return 0;
4884 
4885 	if (sys_name && (strcmp(sys_name, event->system) != 0))
4886 		return 0;
4887 
4888 	return 1;
4889 }
4890 
free_handler(struct event_handler * handle)4891 static void free_handler(struct event_handler *handle)
4892 {
4893 	free((void *)handle->sys_name);
4894 	free((void *)handle->event_name);
4895 	free(handle);
4896 }
4897 
find_event_handle(struct pevent * pevent,struct event_format * event)4898 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4899 {
4900 	struct event_handler *handle, **next;
4901 
4902 	for (next = &pevent->handlers; *next;
4903 	     next = &(*next)->next) {
4904 		handle = *next;
4905 		if (event_matches(event, handle->id,
4906 				  handle->sys_name,
4907 				  handle->event_name))
4908 			break;
4909 	}
4910 
4911 	if (!(*next))
4912 		return 0;
4913 
4914 	pr_stat("overriding event (%d) %s:%s with new print handler",
4915 		event->id, event->system, event->name);
4916 
4917 	event->handler = handle->func;
4918 	event->context = handle->context;
4919 
4920 	*next = handle->next;
4921 	free_handler(handle);
4922 
4923 	return 1;
4924 }
4925 
4926 /**
4927  * __pevent_parse_format - parse the event format
4928  * @buf: the buffer storing the event format string
4929  * @size: the size of @buf
4930  * @sys: the system the event belongs to
4931  *
4932  * This parses the event format and creates an event structure
4933  * to quickly parse raw data for a given event.
4934  *
4935  * These files currently come from:
4936  *
4937  * /sys/kernel/debug/tracing/events/.../.../format
4938  */
__pevent_parse_format(struct event_format ** eventp,struct pevent * pevent,const char * buf,unsigned long size,const char * sys)4939 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4940 					struct pevent *pevent, const char *buf,
4941 					unsigned long size, const char *sys)
4942 {
4943 	struct event_format *event;
4944 	int ret;
4945 
4946 	init_input_buf(buf, size);
4947 
4948 	*eventp = event = alloc_event();
4949 	if (!event)
4950 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4951 
4952 	event->name = event_read_name();
4953 	if (!event->name) {
4954 		/* Bad event? */
4955 		ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4956 		goto event_alloc_failed;
4957 	}
4958 
4959 	if (strcmp(sys, "ftrace") == 0) {
4960 		event->flags |= EVENT_FL_ISFTRACE;
4961 
4962 		if (strcmp(event->name, "bprint") == 0)
4963 			event->flags |= EVENT_FL_ISBPRINT;
4964 	}
4965 
4966 	event->id = event_read_id();
4967 	if (event->id < 0) {
4968 		ret = PEVENT_ERRNO__READ_ID_FAILED;
4969 		/*
4970 		 * This isn't an allocation error actually.
4971 		 * But as the ID is critical, just bail out.
4972 		 */
4973 		goto event_alloc_failed;
4974 	}
4975 
4976 	event->system = strdup(sys);
4977 	if (!event->system) {
4978 		ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4979 		goto event_alloc_failed;
4980 	}
4981 
4982 	/* Add pevent to event so that it can be referenced */
4983 	event->pevent = pevent;
4984 
4985 	ret = event_read_format(event);
4986 	if (ret < 0) {
4987 		ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4988 		goto event_parse_failed;
4989 	}
4990 
4991 	/*
4992 	 * If the event has an override, don't print warnings if the event
4993 	 * print format fails to parse.
4994 	 */
4995 	if (pevent && find_event_handle(pevent, event))
4996 		show_warning = 0;
4997 
4998 	ret = event_read_print(event);
4999 	show_warning = 1;
5000 
5001 	if (ret < 0) {
5002 		ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5003 		goto event_parse_failed;
5004 	}
5005 
5006 	if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5007 		struct format_field *field;
5008 		struct print_arg *arg, **list;
5009 
5010 		/* old ftrace had no args */
5011 		list = &event->print_fmt.args;
5012 		for (field = event->format.fields; field; field = field->next) {
5013 			arg = alloc_arg();
5014 			if (!arg) {
5015 				event->flags |= EVENT_FL_FAILED;
5016 				return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5017 			}
5018 			arg->type = PRINT_FIELD;
5019 			arg->field.name = strdup(field->name);
5020 			if (!arg->field.name) {
5021 				event->flags |= EVENT_FL_FAILED;
5022 				free_arg(arg);
5023 				return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5024 			}
5025 			arg->field.field = field;
5026 			*list = arg;
5027 			list = &arg->next;
5028 		}
5029 		return 0;
5030 	}
5031 
5032 	return 0;
5033 
5034  event_parse_failed:
5035 	event->flags |= EVENT_FL_FAILED;
5036 	return ret;
5037 
5038  event_alloc_failed:
5039 	free(event->system);
5040 	free(event->name);
5041 	free(event);
5042 	*eventp = NULL;
5043 	return ret;
5044 }
5045 
5046 /**
5047  * pevent_parse_format - parse the event format
5048  * @buf: the buffer storing the event format string
5049  * @size: the size of @buf
5050  * @sys: the system the event belongs to
5051  *
5052  * This parses the event format and creates an event structure
5053  * to quickly parse raw data for a given event.
5054  *
5055  * These files currently come from:
5056  *
5057  * /sys/kernel/debug/tracing/events/.../.../format
5058  */
pevent_parse_format(struct event_format ** eventp,const char * buf,unsigned long size,const char * sys)5059 enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5060 				      unsigned long size, const char *sys)
5061 {
5062 	return __pevent_parse_format(eventp, NULL, buf, size, sys);
5063 }
5064 
5065 /**
5066  * pevent_parse_event - parse the event format
5067  * @pevent: the handle to the pevent
5068  * @buf: the buffer storing the event format string
5069  * @size: the size of @buf
5070  * @sys: the system the event belongs to
5071  *
5072  * This parses the event format and creates an event structure
5073  * to quickly parse raw data for a given event.
5074  *
5075  * These files currently come from:
5076  *
5077  * /sys/kernel/debug/tracing/events/.../.../format
5078  */
pevent_parse_event(struct pevent * pevent,const char * buf,unsigned long size,const char * sys)5079 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5080 				     unsigned long size, const char *sys)
5081 {
5082 	struct event_format *event = NULL;
5083 	int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5084 
5085 	if (event == NULL)
5086 		return ret;
5087 
5088 	if (add_event(pevent, event)) {
5089 		ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5090 		goto event_add_failed;
5091 	}
5092 
5093 #define PRINT_ARGS 0
5094 	if (PRINT_ARGS && event->print_fmt.args)
5095 		print_args(event->print_fmt.args);
5096 
5097 	return 0;
5098 
5099 event_add_failed:
5100 	pevent_free_format(event);
5101 	return ret;
5102 }
5103 
5104 #undef _PE
5105 #define _PE(code, str) str
5106 static const char * const pevent_error_str[] = {
5107 	PEVENT_ERRORS
5108 };
5109 #undef _PE
5110 
pevent_strerror(struct pevent * pevent __maybe_unused,enum pevent_errno errnum,char * buf,size_t buflen)5111 int pevent_strerror(struct pevent *pevent __maybe_unused,
5112 		    enum pevent_errno errnum, char *buf, size_t buflen)
5113 {
5114 	int idx;
5115 	const char *msg;
5116 
5117 	if (errnum >= 0) {
5118 		msg = strerror_r(errnum, buf, buflen);
5119 		if (msg != buf) {
5120 			size_t len = strlen(msg);
5121 			memcpy(buf, msg, min(buflen - 1, len));
5122 			*(buf + min(buflen - 1, len)) = '\0';
5123 		}
5124 		return 0;
5125 	}
5126 
5127 	if (errnum <= __PEVENT_ERRNO__START ||
5128 	    errnum >= __PEVENT_ERRNO__END)
5129 		return -1;
5130 
5131 	idx = errnum - __PEVENT_ERRNO__START - 1;
5132 	msg = pevent_error_str[idx];
5133 
5134 	switch (errnum) {
5135 	case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5136 	case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5137 	case PEVENT_ERRNO__READ_ID_FAILED:
5138 	case PEVENT_ERRNO__READ_FORMAT_FAILED:
5139 	case PEVENT_ERRNO__READ_PRINT_FAILED:
5140 	case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
5141 	case PEVENT_ERRNO__INVALID_ARG_TYPE:
5142 		snprintf(buf, buflen, "%s", msg);
5143 		break;
5144 
5145 	default:
5146 		/* cannot reach here */
5147 		break;
5148 	}
5149 
5150 	return 0;
5151 }
5152 
get_field_val(struct trace_seq * s,struct format_field * field,const char * name,struct pevent_record * record,unsigned long long * val,int err)5153 int get_field_val(struct trace_seq *s, struct format_field *field,
5154 		  const char *name, struct pevent_record *record,
5155 		  unsigned long long *val, int err)
5156 {
5157 	if (!field) {
5158 		if (err)
5159 			trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5160 		return -1;
5161 	}
5162 
5163 	if (pevent_read_number_field(field, record->data, val)) {
5164 		if (err)
5165 			trace_seq_printf(s, " %s=INVALID", name);
5166 		return -1;
5167 	}
5168 
5169 	return 0;
5170 }
5171 
5172 /**
5173  * pevent_get_field_raw - return the raw pointer into the data field
5174  * @s: The seq to print to on error
5175  * @event: the event that the field is for
5176  * @name: The name of the field
5177  * @record: The record with the field name.
5178  * @len: place to store the field length.
5179  * @err: print default error if failed.
5180  *
5181  * Returns a pointer into record->data of the field and places
5182  * the length of the field in @len.
5183  *
5184  * On failure, it returns NULL.
5185  */
pevent_get_field_raw(struct trace_seq * s,struct event_format * event,const char * name,struct pevent_record * record,int * len,int err)5186 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5187 			   const char *name, struct pevent_record *record,
5188 			   int *len, int err)
5189 {
5190 	struct format_field *field;
5191 	void *data = record->data;
5192 	unsigned offset;
5193 	int dummy;
5194 
5195 	if (!event)
5196 		return NULL;
5197 
5198 	field = pevent_find_field(event, name);
5199 
5200 	if (!field) {
5201 		if (err)
5202 			trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5203 		return NULL;
5204 	}
5205 
5206 	/* Allow @len to be NULL */
5207 	if (!len)
5208 		len = &dummy;
5209 
5210 	offset = field->offset;
5211 	if (field->flags & FIELD_IS_DYNAMIC) {
5212 		offset = pevent_read_number(event->pevent,
5213 					    data + offset, field->size);
5214 		*len = offset >> 16;
5215 		offset &= 0xffff;
5216 	} else
5217 		*len = field->size;
5218 
5219 	return data + offset;
5220 }
5221 
5222 /**
5223  * pevent_get_field_val - find a field and return its value
5224  * @s: The seq to print to on error
5225  * @event: the event that the field is for
5226  * @name: The name of the field
5227  * @record: The record with the field name.
5228  * @val: place to store the value of the field.
5229  * @err: print default error if failed.
5230  *
5231  * Returns 0 on success -1 on field not found.
5232  */
pevent_get_field_val(struct trace_seq * s,struct event_format * event,const char * name,struct pevent_record * record,unsigned long long * val,int err)5233 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5234 			 const char *name, struct pevent_record *record,
5235 			 unsigned long long *val, int err)
5236 {
5237 	struct format_field *field;
5238 
5239 	if (!event)
5240 		return -1;
5241 
5242 	field = pevent_find_field(event, name);
5243 
5244 	return get_field_val(s, field, name, record, val, err);
5245 }
5246 
5247 /**
5248  * pevent_get_common_field_val - find a common field and return its value
5249  * @s: The seq to print to on error
5250  * @event: the event that the field is for
5251  * @name: The name of the field
5252  * @record: The record with the field name.
5253  * @val: place to store the value of the field.
5254  * @err: print default error if failed.
5255  *
5256  * Returns 0 on success -1 on field not found.
5257  */
pevent_get_common_field_val(struct trace_seq * s,struct event_format * event,const char * name,struct pevent_record * record,unsigned long long * val,int err)5258 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5259 				const char *name, struct pevent_record *record,
5260 				unsigned long long *val, int err)
5261 {
5262 	struct format_field *field;
5263 
5264 	if (!event)
5265 		return -1;
5266 
5267 	field = pevent_find_common_field(event, name);
5268 
5269 	return get_field_val(s, field, name, record, val, err);
5270 }
5271 
5272 /**
5273  * pevent_get_any_field_val - find a any field and return its value
5274  * @s: The seq to print to on error
5275  * @event: the event that the field is for
5276  * @name: The name of the field
5277  * @record: The record with the field name.
5278  * @val: place to store the value of the field.
5279  * @err: print default error if failed.
5280  *
5281  * Returns 0 on success -1 on field not found.
5282  */
pevent_get_any_field_val(struct trace_seq * s,struct event_format * event,const char * name,struct pevent_record * record,unsigned long long * val,int err)5283 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5284 			     const char *name, struct pevent_record *record,
5285 			     unsigned long long *val, int err)
5286 {
5287 	struct format_field *field;
5288 
5289 	if (!event)
5290 		return -1;
5291 
5292 	field = pevent_find_any_field(event, name);
5293 
5294 	return get_field_val(s, field, name, record, val, err);
5295 }
5296 
5297 /**
5298  * pevent_print_num_field - print a field and a format
5299  * @s: The seq to print to
5300  * @fmt: The printf format to print the field with.
5301  * @event: the event that the field is for
5302  * @name: The name of the field
5303  * @record: The record with the field name.
5304  * @err: print default error if failed.
5305  *
5306  * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5307  */
pevent_print_num_field(struct trace_seq * s,const char * fmt,struct event_format * event,const char * name,struct pevent_record * record,int err)5308 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5309 			   struct event_format *event, const char *name,
5310 			   struct pevent_record *record, int err)
5311 {
5312 	struct format_field *field = pevent_find_field(event, name);
5313 	unsigned long long val;
5314 
5315 	if (!field)
5316 		goto failed;
5317 
5318 	if (pevent_read_number_field(field, record->data, &val))
5319 		goto failed;
5320 
5321 	return trace_seq_printf(s, fmt, val);
5322 
5323  failed:
5324 	if (err)
5325 		trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5326 	return -1;
5327 }
5328 
free_func_handle(struct pevent_function_handler * func)5329 static void free_func_handle(struct pevent_function_handler *func)
5330 {
5331 	struct pevent_func_params *params;
5332 
5333 	free(func->name);
5334 
5335 	while (func->params) {
5336 		params = func->params;
5337 		func->params = params->next;
5338 		free(params);
5339 	}
5340 
5341 	free(func);
5342 }
5343 
5344 /**
5345  * pevent_register_print_function - register a helper function
5346  * @pevent: the handle to the pevent
5347  * @func: the function to process the helper function
5348  * @ret_type: the return type of the helper function
5349  * @name: the name of the helper function
5350  * @parameters: A list of enum pevent_func_arg_type
5351  *
5352  * Some events may have helper functions in the print format arguments.
5353  * This allows a plugin to dynamically create a way to process one
5354  * of these functions.
5355  *
5356  * The @parameters is a variable list of pevent_func_arg_type enums that
5357  * must end with PEVENT_FUNC_ARG_VOID.
5358  */
pevent_register_print_function(struct pevent * pevent,pevent_func_handler func,enum pevent_func_arg_type ret_type,char * name,...)5359 int pevent_register_print_function(struct pevent *pevent,
5360 				   pevent_func_handler func,
5361 				   enum pevent_func_arg_type ret_type,
5362 				   char *name, ...)
5363 {
5364 	struct pevent_function_handler *func_handle;
5365 	struct pevent_func_params **next_param;
5366 	struct pevent_func_params *param;
5367 	enum pevent_func_arg_type type;
5368 	va_list ap;
5369 	int ret;
5370 
5371 	func_handle = find_func_handler(pevent, name);
5372 	if (func_handle) {
5373 		/*
5374 		 * This is most like caused by the users own
5375 		 * plugins updating the function. This overrides the
5376 		 * system defaults.
5377 		 */
5378 		pr_stat("override of function helper '%s'", name);
5379 		remove_func_handler(pevent, name);
5380 	}
5381 
5382 	func_handle = calloc(1, sizeof(*func_handle));
5383 	if (!func_handle) {
5384 		do_warning("Failed to allocate function handler");
5385 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5386 	}
5387 
5388 	func_handle->ret_type = ret_type;
5389 	func_handle->name = strdup(name);
5390 	func_handle->func = func;
5391 	if (!func_handle->name) {
5392 		do_warning("Failed to allocate function name");
5393 		free(func_handle);
5394 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5395 	}
5396 
5397 	next_param = &(func_handle->params);
5398 	va_start(ap, name);
5399 	for (;;) {
5400 		type = va_arg(ap, enum pevent_func_arg_type);
5401 		if (type == PEVENT_FUNC_ARG_VOID)
5402 			break;
5403 
5404 		if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5405 			do_warning("Invalid argument type %d", type);
5406 			ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5407 			goto out_free;
5408 		}
5409 
5410 		param = malloc(sizeof(*param));
5411 		if (!param) {
5412 			do_warning("Failed to allocate function param");
5413 			ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5414 			goto out_free;
5415 		}
5416 		param->type = type;
5417 		param->next = NULL;
5418 
5419 		*next_param = param;
5420 		next_param = &(param->next);
5421 
5422 		func_handle->nr_args++;
5423 	}
5424 	va_end(ap);
5425 
5426 	func_handle->next = pevent->func_handlers;
5427 	pevent->func_handlers = func_handle;
5428 
5429 	return 0;
5430  out_free:
5431 	va_end(ap);
5432 	free_func_handle(func_handle);
5433 	return ret;
5434 }
5435 
5436 /**
5437  * pevent_register_event_handler - register a way to parse an event
5438  * @pevent: the handle to the pevent
5439  * @id: the id of the event to register
5440  * @sys_name: the system name the event belongs to
5441  * @event_name: the name of the event
5442  * @func: the function to call to parse the event information
5443  * @context: the data to be passed to @func
5444  *
5445  * This function allows a developer to override the parsing of
5446  * a given event. If for some reason the default print format
5447  * is not sufficient, this function will register a function
5448  * for an event to be used to parse the data instead.
5449  *
5450  * If @id is >= 0, then it is used to find the event.
5451  * else @sys_name and @event_name are used.
5452  */
pevent_register_event_handler(struct pevent * pevent,int id,const char * sys_name,const char * event_name,pevent_event_handler_func func,void * context)5453 int pevent_register_event_handler(struct pevent *pevent, int id,
5454 				  const char *sys_name, const char *event_name,
5455 				  pevent_event_handler_func func, void *context)
5456 {
5457 	struct event_format *event;
5458 	struct event_handler *handle;
5459 
5460 	if (id >= 0) {
5461 		/* search by id */
5462 		event = pevent_find_event(pevent, id);
5463 		if (!event)
5464 			goto not_found;
5465 		if (event_name && (strcmp(event_name, event->name) != 0))
5466 			goto not_found;
5467 		if (sys_name && (strcmp(sys_name, event->system) != 0))
5468 			goto not_found;
5469 	} else {
5470 		event = pevent_find_event_by_name(pevent, sys_name, event_name);
5471 		if (!event)
5472 			goto not_found;
5473 	}
5474 
5475 	pr_stat("overriding event (%d) %s:%s with new print handler",
5476 		event->id, event->system, event->name);
5477 
5478 	event->handler = func;
5479 	event->context = context;
5480 	return 0;
5481 
5482  not_found:
5483 	/* Save for later use. */
5484 	handle = calloc(1, sizeof(*handle));
5485 	if (!handle) {
5486 		do_warning("Failed to allocate event handler");
5487 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5488 	}
5489 
5490 	handle->id = id;
5491 	if (event_name)
5492 		handle->event_name = strdup(event_name);
5493 	if (sys_name)
5494 		handle->sys_name = strdup(sys_name);
5495 
5496 	if ((event_name && !handle->event_name) ||
5497 	    (sys_name && !handle->sys_name)) {
5498 		do_warning("Failed to allocate event/sys name");
5499 		free((void *)handle->event_name);
5500 		free((void *)handle->sys_name);
5501 		free(handle);
5502 		return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5503 	}
5504 
5505 	handle->func = func;
5506 	handle->next = pevent->handlers;
5507 	pevent->handlers = handle;
5508 	handle->context = context;
5509 
5510 	return -1;
5511 }
5512 
5513 /**
5514  * pevent_alloc - create a pevent handle
5515  */
pevent_alloc(void)5516 struct pevent *pevent_alloc(void)
5517 {
5518 	struct pevent *pevent = calloc(1, sizeof(*pevent));
5519 
5520 	if (pevent)
5521 		pevent->ref_count = 1;
5522 
5523 	return pevent;
5524 }
5525 
pevent_ref(struct pevent * pevent)5526 void pevent_ref(struct pevent *pevent)
5527 {
5528 	pevent->ref_count++;
5529 }
5530 
free_format_fields(struct format_field * field)5531 static void free_format_fields(struct format_field *field)
5532 {
5533 	struct format_field *next;
5534 
5535 	while (field) {
5536 		next = field->next;
5537 		free(field->type);
5538 		free(field->name);
5539 		free(field);
5540 		field = next;
5541 	}
5542 }
5543 
free_formats(struct format * format)5544 static void free_formats(struct format *format)
5545 {
5546 	free_format_fields(format->common_fields);
5547 	free_format_fields(format->fields);
5548 }
5549 
pevent_free_format(struct event_format * event)5550 void pevent_free_format(struct event_format *event)
5551 {
5552 	free(event->name);
5553 	free(event->system);
5554 
5555 	free_formats(&event->format);
5556 
5557 	free(event->print_fmt.format);
5558 	free_args(event->print_fmt.args);
5559 
5560 	free(event);
5561 }
5562 
5563 /**
5564  * pevent_free - free a pevent handle
5565  * @pevent: the pevent handle to free
5566  */
pevent_free(struct pevent * pevent)5567 void pevent_free(struct pevent *pevent)
5568 {
5569 	struct cmdline_list *cmdlist, *cmdnext;
5570 	struct func_list *funclist, *funcnext;
5571 	struct printk_list *printklist, *printknext;
5572 	struct pevent_function_handler *func_handler;
5573 	struct event_handler *handle;
5574 	int i;
5575 
5576 	if (!pevent)
5577 		return;
5578 
5579 	cmdlist = pevent->cmdlist;
5580 	funclist = pevent->funclist;
5581 	printklist = pevent->printklist;
5582 
5583 	pevent->ref_count--;
5584 	if (pevent->ref_count)
5585 		return;
5586 
5587 	if (pevent->cmdlines) {
5588 		for (i = 0; i < pevent->cmdline_count; i++)
5589 			free(pevent->cmdlines[i].comm);
5590 		free(pevent->cmdlines);
5591 	}
5592 
5593 	while (cmdlist) {
5594 		cmdnext = cmdlist->next;
5595 		free(cmdlist->comm);
5596 		free(cmdlist);
5597 		cmdlist = cmdnext;
5598 	}
5599 
5600 	if (pevent->func_map) {
5601 		for (i = 0; i < (int)pevent->func_count; i++) {
5602 			free(pevent->func_map[i].func);
5603 			free(pevent->func_map[i].mod);
5604 		}
5605 		free(pevent->func_map);
5606 	}
5607 
5608 	while (funclist) {
5609 		funcnext = funclist->next;
5610 		free(funclist->func);
5611 		free(funclist->mod);
5612 		free(funclist);
5613 		funclist = funcnext;
5614 	}
5615 
5616 	while (pevent->func_handlers) {
5617 		func_handler = pevent->func_handlers;
5618 		pevent->func_handlers = func_handler->next;
5619 		free_func_handle(func_handler);
5620 	}
5621 
5622 	if (pevent->printk_map) {
5623 		for (i = 0; i < (int)pevent->printk_count; i++)
5624 			free(pevent->printk_map[i].printk);
5625 		free(pevent->printk_map);
5626 	}
5627 
5628 	while (printklist) {
5629 		printknext = printklist->next;
5630 		free(printklist->printk);
5631 		free(printklist);
5632 		printklist = printknext;
5633 	}
5634 
5635 	for (i = 0; i < pevent->nr_events; i++)
5636 		pevent_free_format(pevent->events[i]);
5637 
5638 	while (pevent->handlers) {
5639 		handle = pevent->handlers;
5640 		pevent->handlers = handle->next;
5641 		free_handler(handle);
5642 	}
5643 
5644 	free(pevent->events);
5645 	free(pevent->sort_events);
5646 
5647 	free(pevent);
5648 }
5649 
pevent_unref(struct pevent * pevent)5650 void pevent_unref(struct pevent *pevent)
5651 {
5652 	pevent_free(pevent);
5653 }
5654