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