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