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