xref: /aosp_15_r20/external/libtraceevent/src/event-parse.c (revision 436bf2bcd5202612ffffe471bbcc1f277cc8d28e)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
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 <unistd.h>
21 #include <limits.h>
22 #include <linux/time64.h>
23 
24 #include <netinet/in.h>
25 #include "event-parse.h"
26 
27 #include "event-parse-local.h"
28 #include "event-utils.h"
29 #include "trace-seq.h"
30 
31 static int is_flag_field;
32 static int is_symbolic_field;
33 
34 static int show_warning = 1;
35 
36 #define do_warning(fmt, ...)				\
37 	do {						\
38 		if (show_warning)			\
39 			tep_warning(fmt, ##__VA_ARGS__);\
40 	} while (0)
41 
42 #define do_warning_event(event, fmt, ...)				\
43 	do {								\
44 		if (!show_warning)					\
45 			continue;					\
46 									\
47 		if (event)						\
48 			tep_warning("[%s:%s] " fmt, event->system,	\
49 				    event->name, ##__VA_ARGS__);	\
50 		else							\
51 			tep_warning(fmt, ##__VA_ARGS__);		\
52 	} while (0)
53 
54 /**
55  * init_input_buf - init buffer for parsing
56  * @buf: buffer to parse
57  * @size: the size of the buffer
58  *
59  * Initializes the internal buffer that tep_read_token() will parse.
60  */
init_input_buf(struct tep_handle * tep,const char * buf,unsigned long long size)61 __hidden void init_input_buf(struct tep_handle *tep, const char *buf,
62 		unsigned long long size)
63 {
64 	tep->input_buf = buf;
65 	tep->input_buf_siz = size;
66 	tep->input_buf_ptr = 0;
67 }
68 
get_input_buf(struct tep_handle * tep)69 __hidden const char *get_input_buf(struct tep_handle *tep)
70 {
71 	return tep->input_buf;
72 }
73 
get_input_buf_ptr(struct tep_handle * tep)74 __hidden unsigned long long get_input_buf_ptr(struct tep_handle *tep)
75 {
76 	return tep->input_buf_ptr;
77 }
78 
79 struct event_handler {
80 	struct event_handler		*next;
81 	int				id;
82 	const char			*sys_name;
83 	const char			*event_name;
84 	tep_event_handler_func		func;
85 	void				*context;
86 };
87 
88 struct func_params {
89 	struct func_params	*next;
90 	enum tep_func_arg_type	type;
91 };
92 
93 struct tep_function_handler {
94 	struct tep_function_handler	*next;
95 	enum tep_func_arg_type		ret_type;
96 	char				*name;
97 	tep_func_handler		func;
98 	struct func_params		*params;
99 	int				nr_args;
100 };
101 
102 static unsigned long long
103 process_defined_func(struct trace_seq *s, void *data, int size,
104 		     struct tep_event *event, struct tep_print_arg *arg);
105 
106 static void free_func_handle(struct tep_function_handler *func);
107 
breakpoint(void)108 void breakpoint(void)
109 {
110 	static int x;
111 	x++;
112 }
113 
get_event_type(enum tep_event_type type)114 static const char *get_event_type(enum tep_event_type type)
115 {
116 	switch (type) {
117 	case TEP_EVENT_ERROR: return "ERROR";
118 	case TEP_EVENT_NONE: return "NONE";
119 	case TEP_EVENT_SPACE: return "SPACE";
120 	case TEP_EVENT_NEWLINE: return "NEWLINE";
121 	case TEP_EVENT_OP: return "OP";
122 	case TEP_EVENT_DELIM: return "DELIM";
123 	case TEP_EVENT_ITEM: return "ITEM";
124 	case TEP_EVENT_DQUOTE: return "DQUOTE";
125 	case TEP_EVENT_SQUOTE: return "SQUOTE";
126 	}
127 	return "(UNKNOWN)";
128 }
129 
alloc_arg(void)130 static struct tep_print_arg *alloc_arg(void)
131 {
132 	return calloc(1, sizeof(struct tep_print_arg));
133 }
134 
135 struct tep_cmdline {
136 	char *comm;
137 	int pid;
138 };
139 
cmdline_cmp(const void * a,const void * b)140 static int cmdline_cmp(const void *a, const void *b)
141 {
142 	const struct tep_cmdline *ca = a;
143 	const struct tep_cmdline *cb = b;
144 
145 	if (ca->pid < cb->pid)
146 		return -1;
147 	if (ca->pid > cb->pid)
148 		return 1;
149 
150 	return 0;
151 }
152 
153 /* Looking for where to place the key */
cmdline_slot_cmp(const void * a,const void * b)154 static int cmdline_slot_cmp(const void *a, const void *b)
155 {
156 	const struct tep_cmdline *ca = a;
157 	const struct tep_cmdline *cb = b;
158 	const struct tep_cmdline *cb1 = cb + 1;
159 
160 	if (ca->pid < cb->pid)
161 		return -1;
162 
163 	if (ca->pid > cb->pid) {
164 		if (ca->pid <= cb1->pid)
165 			return 0;
166 		return 1;
167 	}
168 
169 	return 0;
170 }
171 
172 struct cmdline_list {
173 	struct cmdline_list	*next;
174 	char			*comm;
175 	int			pid;
176 };
177 
cmdline_init(struct tep_handle * tep)178 static int cmdline_init(struct tep_handle *tep)
179 {
180 	struct cmdline_list *cmdlist = tep->cmdlist;
181 	struct cmdline_list *item;
182 	struct tep_cmdline *cmdlines;
183 	int i;
184 
185 	cmdlines = malloc(sizeof(*cmdlines) * tep->cmdline_count);
186 	if (!cmdlines)
187 		return -1;
188 
189 	i = 0;
190 	while (cmdlist) {
191 		cmdlines[i].pid = cmdlist->pid;
192 		cmdlines[i].comm = cmdlist->comm;
193 		i++;
194 		item = cmdlist;
195 		cmdlist = cmdlist->next;
196 		free(item);
197 	}
198 
199 	qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
200 
201 	tep->cmdlines = cmdlines;
202 	tep->cmdlist = NULL;
203 
204 	return 0;
205 }
206 
find_cmdline(struct tep_handle * tep,int pid)207 static const char *find_cmdline(struct tep_handle *tep, int pid)
208 {
209 	const struct tep_cmdline *comm;
210 	struct tep_cmdline key;
211 
212 	if (!pid)
213 		return "<idle>";
214 
215 	if (!tep->cmdlines && cmdline_init(tep))
216 		return "<not enough memory for cmdlines!>";
217 
218 	key.pid = pid;
219 
220 	comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
221 		       sizeof(*tep->cmdlines), cmdline_cmp);
222 
223 	if (comm)
224 		return comm->comm;
225 	return "<...>";
226 }
227 
228 /**
229  * tep_is_pid_registered - return if a pid has a cmdline registered
230  * @tep: a handle to the trace event parser context
231  * @pid: The pid to check if it has a cmdline registered with.
232  *
233  * Returns true if the pid has a cmdline mapped to it
234  * false otherwise.
235  */
tep_is_pid_registered(struct tep_handle * tep,int pid)236 bool tep_is_pid_registered(struct tep_handle *tep, int pid)
237 {
238 	const struct tep_cmdline *comm;
239 	struct tep_cmdline key;
240 
241 	if (!pid)
242 		return true;
243 
244 	if (!tep->cmdlines && cmdline_init(tep))
245 		return false;
246 
247 	key.pid = pid;
248 
249 	comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
250 		       sizeof(*tep->cmdlines), cmdline_cmp);
251 
252 	if (comm)
253 		return true;
254 	return false;
255 }
256 
257 /*
258  * If the command lines have been converted to an array, then
259  * we must add this pid. This is much slower than when cmdlines
260  * are added before the array is initialized.
261  */
add_new_comm(struct tep_handle * tep,const char * comm,int pid,bool override)262 static int add_new_comm(struct tep_handle *tep,
263 			const char *comm, int pid, bool override)
264 {
265 	struct tep_cmdline *cmdlines = tep->cmdlines;
266 	struct tep_cmdline *cmdline;
267 	struct tep_cmdline key;
268 	char *new_comm;
269 	int cnt;
270 
271 	if (!pid)
272 		return 0;
273 
274 	/* avoid duplicates */
275 	key.pid = pid;
276 
277 	cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count,
278 			  sizeof(*tep->cmdlines), cmdline_cmp);
279 	if (cmdline) {
280 		if (!override) {
281 			errno = EEXIST;
282 			return -1;
283 		}
284 		new_comm = strdup(comm);
285 		if (!new_comm) {
286 			errno = ENOMEM;
287 			return -1;
288 		}
289 		free(cmdline->comm);
290 		cmdline->comm = new_comm;
291 
292 		return 0;
293 	}
294 
295 	cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1));
296 	if (!cmdlines) {
297 		errno = ENOMEM;
298 		return -1;
299 	}
300 	tep->cmdlines = cmdlines;
301 
302 	key.comm = strdup(comm);
303 	if (!key.comm) {
304 		errno = ENOMEM;
305 		return -1;
306 	}
307 
308 	if (!tep->cmdline_count) {
309 		/* no entries yet */
310 		tep->cmdlines[0] = key;
311 		tep->cmdline_count++;
312 		return 0;
313 	}
314 
315 	/* Now find where we want to store the new cmdline */
316 	cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count - 1,
317 			  sizeof(*tep->cmdlines), cmdline_slot_cmp);
318 
319 	cnt = tep->cmdline_count;
320 	if (cmdline) {
321 		/* cmdline points to the one before the spot we want */
322 		cmdline++;
323 		cnt -= cmdline - tep->cmdlines;
324 
325 	} else {
326 		/* The new entry is either before or after the list */
327 		if (key.pid > tep->cmdlines[tep->cmdline_count - 1].pid) {
328 			tep->cmdlines[tep->cmdline_count++] = key;
329 			return 0;
330 		}
331 		cmdline = &tep->cmdlines[0];
332 	}
333 	memmove(cmdline + 1, cmdline, (cnt * sizeof(*cmdline)));
334 	*cmdline = key;
335 
336 	tep->cmdline_count++;
337 
338 	return 0;
339 }
340 
_tep_register_comm(struct tep_handle * tep,const char * comm,int pid,bool override)341 static int _tep_register_comm(struct tep_handle *tep,
342 			      const char *comm, int pid, bool override)
343 {
344 	struct cmdline_list *item;
345 
346 	if (tep->cmdlines)
347 		return add_new_comm(tep, comm, pid, override);
348 
349 	item = malloc(sizeof(*item));
350 	if (!item)
351 		return -1;
352 
353 	if (comm)
354 		item->comm = strdup(comm);
355 	else
356 		item->comm = strdup("<...>");
357 	if (!item->comm) {
358 		free(item);
359 		return -1;
360 	}
361 	item->pid = pid;
362 	item->next = tep->cmdlist;
363 
364 	tep->cmdlist = item;
365 	tep->cmdline_count++;
366 
367 	return 0;
368 }
369 
370 /**
371  * tep_register_comm - register a pid / comm mapping
372  * @tep: a handle to the trace event parser context
373  * @comm: the command line to register
374  * @pid: the pid to map the command line to
375  *
376  * This adds a mapping to search for command line names with
377  * a given pid. The comm is duplicated. If a command with the same pid
378  * already exist, -1 is returned and errno is set to EEXIST
379  */
tep_register_comm(struct tep_handle * tep,const char * comm,int pid)380 int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
381 {
382 	return _tep_register_comm(tep, comm, pid, false);
383 }
384 
385 /**
386  * tep_override_comm - register a pid / comm mapping
387  * @tep: a handle to the trace event parser context
388  * @comm: the command line to register
389  * @pid: the pid to map the command line to
390  *
391  * This adds a mapping to search for command line names with
392  * a given pid. The comm is duplicated. If a command with the same pid
393  * already exist, the command string is udapted with the new one
394  */
tep_override_comm(struct tep_handle * tep,const char * comm,int pid)395 int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
396 {
397 	if (!tep->cmdlines && cmdline_init(tep)) {
398 		errno = ENOMEM;
399 		return -1;
400 	}
401 	return _tep_register_comm(tep, comm, pid, true);
402 }
403 
404 /**
405  * tep_parse_saved_cmdlines - parse the comms from the saved_cmdlines file
406  * @tep: a handle to the trace event parser
407  * @buf: A string buffer that holds the content of saved_cmdlines and ends with '\0'
408  *
409  * This is a helper function to parse the comms in the tracefs saved_cmdlines
410  * file (stored in a string buffer) and load the comms into the @tep handler
411  * such that comm name matches an process ID (pid). This is used to show
412  * the names of the processes as the events only hold the pid.
413  *
414  * Returns 0 on success, and -1 on error.
415  */
tep_parse_saved_cmdlines(struct tep_handle * tep,const char * buf)416 int tep_parse_saved_cmdlines(struct tep_handle *tep, const char *buf)
417 {
418 	char *copy;
419 	char *comm;
420 	char *line;
421 	char *next = NULL;
422 	int pid;
423 	int ret = -1;
424 	int n;
425 
426 	copy = strdup(buf);
427 	if (!copy)
428 		return -1;
429 
430 	line = strtok_r(copy, "\n", &next);
431 	while (line) {
432 		errno = 0;
433 		n = sscanf(line, "%d %m[^\n]s", &pid, &comm);
434 		if (errno || n != 2 || !comm)
435 			goto out;
436 		tep_register_comm(tep, comm, pid);
437 		free(comm);
438 		line = strtok_r(NULL, "\n", &next);
439 	}
440 	ret = 0;
441  out:
442 	free(copy);
443 	return ret;
444 }
445 
446 struct func_map {
447 	unsigned long long		addr;
448 	char				*func;
449 	char				*mod;
450 };
451 
452 struct func_list {
453 	struct func_list	*next;
454 	unsigned long long	addr;
455 	char			*func;
456 	char			*mod;
457 };
458 
func_cmp(const void * a,const void * b)459 static int func_cmp(const void *a, const void *b)
460 {
461 	const struct func_map *fa = a;
462 	const struct func_map *fb = b;
463 
464 	if (fa->addr < fb->addr)
465 		return -1;
466 	if (fa->addr > fb->addr)
467 		return 1;
468 
469 	return 0;
470 }
471 
472 /*
473  * We are searching for a record in between, not an exact
474  * match.
475  */
func_bcmp(const void * a,const void * b)476 static int func_bcmp(const void *a, const void *b)
477 {
478 	const struct func_map *fa = a;
479 	const struct func_map *fb = b;
480 
481 	if ((fa->addr == fb->addr) ||
482 
483 	    (fa->addr > fb->addr &&
484 	     fa->addr < (fb+1)->addr))
485 		return 0;
486 
487 	if (fa->addr < fb->addr)
488 		return -1;
489 
490 	return 1;
491 }
492 
func_map_init(struct tep_handle * tep)493 static int func_map_init(struct tep_handle *tep)
494 {
495 	struct func_list *funclist;
496 	struct func_list *item;
497 	struct func_map *func_map;
498 	int i;
499 
500 	func_map = malloc(sizeof(*func_map) * (tep->func_count + 1));
501 	if (!func_map)
502 		return -1;
503 
504 	funclist = tep->funclist;
505 
506 	i = 0;
507 	while (funclist) {
508 		func_map[i].func = funclist->func;
509 		func_map[i].addr = funclist->addr;
510 		func_map[i].mod = funclist->mod;
511 		i++;
512 		item = funclist;
513 		funclist = funclist->next;
514 		free(item);
515 	}
516 
517 	qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp);
518 
519 	/*
520 	 * Add a special record at the end.
521 	 */
522 	func_map[tep->func_count].func = NULL;
523 	func_map[tep->func_count].addr = 0;
524 	func_map[tep->func_count].mod = NULL;
525 
526 	tep->func_map = func_map;
527 	tep->funclist = NULL;
528 
529 	return 0;
530 }
531 
532 static struct func_map *
__find_func(struct tep_handle * tep,unsigned long long addr)533 __find_func(struct tep_handle *tep, unsigned long long addr)
534 {
535 	struct func_map *func;
536 	struct func_map key;
537 
538 	if (!tep->func_map)
539 		func_map_init(tep);
540 
541 	key.addr = addr;
542 
543 	func = bsearch(&key, tep->func_map, tep->func_count,
544 		       sizeof(*tep->func_map), func_bcmp);
545 
546 	return func;
547 }
548 
549 struct func_resolver {
550 	tep_func_resolver_t	*func;
551 	void			*priv;
552 	struct func_map		map;
553 };
554 
555 /**
556  * tep_set_function_resolver - set an alternative function resolver
557  * @tep: a handle to the trace event parser context
558  * @resolver: function to be used
559  * @priv: resolver function private state.
560  *
561  * Some tools may have already a way to resolve kernel functions, allow them to
562  * keep using it instead of duplicating all the entries inside tep->funclist.
563  */
tep_set_function_resolver(struct tep_handle * tep,tep_func_resolver_t * func,void * priv)564 int tep_set_function_resolver(struct tep_handle *tep,
565 			      tep_func_resolver_t *func, void *priv)
566 {
567 	struct func_resolver *resolver = malloc(sizeof(*resolver));
568 
569 	if (resolver == NULL)
570 		return -1;
571 
572 	resolver->func = func;
573 	resolver->priv = priv;
574 
575 	free(tep->func_resolver);
576 	tep->func_resolver = resolver;
577 
578 	return 0;
579 }
580 
581 /**
582  * tep_reset_function_resolver - reset alternative function resolver
583  * @tep: a handle to the trace event parser context
584  *
585  * Stop using whatever alternative resolver was set, use the default
586  * one instead.
587  */
tep_reset_function_resolver(struct tep_handle * tep)588 void tep_reset_function_resolver(struct tep_handle *tep)
589 {
590 	free(tep->func_resolver);
591 	tep->func_resolver = NULL;
592 }
593 
594 static struct func_map *
find_func(struct tep_handle * tep,unsigned long long addr)595 find_func(struct tep_handle *tep, unsigned long long addr)
596 {
597 	struct func_map *map;
598 
599 	if (!tep->func_resolver)
600 		return __find_func(tep, addr);
601 
602 	map = &tep->func_resolver->map;
603 	map->mod  = NULL;
604 	map->addr = addr;
605 	map->func = tep->func_resolver->func(tep->func_resolver->priv,
606 					     &map->addr, &map->mod);
607 	if (map->func == NULL)
608 		return NULL;
609 
610 	return map;
611 }
612 
613 /**
614  * tep_find_function_info - find a function by a given address
615  * @tep: a handle to the trace event parser context
616  * @addr: the address to find the function with
617  * @name: Return the name of the function (if found)
618  * @start: Return the start of the function (if found)
619  * @size: Return the size of the function (if found)
620  *
621  * Returns 1 if found, and 0 if it is not.
622  *  If found then @name will point to the name of the function.
623  *                @start: will contain the starting address of the function.
624  *                @size: will contain the size of the function.
625  */
tep_find_function_info(struct tep_handle * tep,unsigned long long addr,const char ** name,unsigned long long * start,unsigned long * size)626 int tep_find_function_info(struct tep_handle *tep, unsigned long long addr,
627 			   const char **name, unsigned long long *start,
628 			   unsigned long *size)
629 {
630 	struct func_map *map;
631 
632 	map = find_func(tep, addr);
633 	if (!map)
634 		return 0;
635 
636 	if (name)
637 		*name = map->func;
638 	if (start)
639 		*start = map->addr;
640 	if (size) {
641 		if (!tep->func_resolver)
642 			*size = map[1].addr - map->addr;
643 		else
644 			*size = 0;
645 	}
646 
647 	return 1;
648 }
649 
650 /**
651  * tep_find_function - find a function by a given address
652  * @tep: a handle to the trace event parser context
653  * @addr: the address to find the function with
654  *
655  * Returns a pointer to the function stored that has the given
656  * address. Note, the address does not have to be exact, it
657  * will select the function that would contain the address.
658  */
tep_find_function(struct tep_handle * tep,unsigned long long addr)659 const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
660 {
661 	struct func_map *map;
662 
663 	map = find_func(tep, addr);
664 	if (!map)
665 		return NULL;
666 
667 	return map->func;
668 }
669 
670 /**
671  * tep_find_function_address - find a function address by a given address
672  * @tep: a handle to the trace event parser context
673  * @addr: the address to find the function with
674  *
675  * Returns the address the function starts at. This can be used in
676  * conjunction with tep_find_function to print both the function
677  * name and the function offset.
678  */
679 unsigned long long
tep_find_function_address(struct tep_handle * tep,unsigned long long addr)680 tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
681 {
682 	struct func_map *map;
683 
684 	map = find_func(tep, addr);
685 	if (!map)
686 		return 0;
687 
688 	return map->addr;
689 }
690 
691 /**
692  * tep_register_function - register a function with a given address
693  * @tep: a handle to the trace event parser context
694  * @function: the function name to register
695  * @addr: the address the function starts at
696  * @mod: the kernel module the function may be in (NULL for none)
697  *
698  * This registers a function name with an address and module.
699  * The @func passed in is duplicated.
700  */
tep_register_function(struct tep_handle * tep,char * func,unsigned long long addr,char * mod)701 int tep_register_function(struct tep_handle *tep, char *func,
702 			  unsigned long long addr, char *mod)
703 {
704 	struct func_list *item = malloc(sizeof(*item));
705 
706 	if (!item)
707 		return -1;
708 
709 	item->next = tep->funclist;
710 	item->func = strdup(func);
711 	if (!item->func)
712 		goto out_free;
713 
714 	if (mod) {
715 		item->mod = strdup(mod);
716 		if (!item->mod)
717 			goto out_free_func;
718 	} else
719 		item->mod = NULL;
720 	item->addr = addr;
721 
722 	tep->funclist = item;
723 	tep->func_count++;
724 
725 	return 0;
726 
727 out_free_func:
728 	free(item->func);
729 	item->func = NULL;
730 out_free:
731 	free(item);
732 	errno = ENOMEM;
733 	return -1;
734 }
735 
736 /**
737  * tep_parse_kallsyms - load functions from a read of /proc/kallsyms
738  * @tep: a handle to the trace event parser
739  * @kallsyms: A string buffer that holds the content of /proc/kallsyms and ends with '\0'
740  *
741  * This is a helper function to parse the Linux kernel /proc/kallsyms
742  * format (stored in a string buffer) and load the functions into
743  * the @tep handler such that function IP addresses can be mapped to
744  * their name when parsing events with %pS in the print format field.
745  *
746  * Returns 0 on success, and -1 on error.
747  */
tep_parse_kallsyms(struct tep_handle * tep,const char * kallsyms)748 int tep_parse_kallsyms(struct tep_handle *tep, const char *kallsyms)
749 {
750 	unsigned long long addr;
751 	char *copy;
752 	char *func;
753 	char *line;
754 	char *next = NULL;
755 	char *mod;
756 	char ch;
757 	int ret = -1;
758 
759 	copy = strdup(kallsyms);
760 	if (!copy)
761 		return -1;
762 
763 	line = strtok_r(copy, "\n", &next);
764 	while (line) {
765 		int func_start, func_end = 0;
766 		int mod_start, mod_end = 0;
767 		int n;
768 
769 		mod = NULL;
770 		errno = 0;
771 		n = sscanf(line, "%16llx %c %n%*s%n%*1[\t][%n%*s%n",
772 			   &addr, &ch, &func_start, &func_end, &mod_start, &mod_end);
773 		if (errno)
774 			goto out;
775 
776 		if (n != 2 || !func_end) {
777 			tep_warning("Failed to parse kallsyms n=%d func_end=%d",
778 				    n, func_end);
779 			goto out;
780 		}
781 
782 		func = line + func_start;
783 		/*
784 		 * Hacks for
785 		 *  - arm arch that adds a lot of bogus '$a' functions
786 		 *  - x86-64 that reports per-cpu variable offsets as absolute
787 		 */
788 		if (func[0] != '$' && ch != 'A' && ch != 'a') {
789 			line[func_end] = 0;
790 			if (mod_end) {
791 				mod = line + mod_start;
792 				/* truncate the extra ']' */
793 				line[mod_end - 1] = 0;
794 			}
795 			tep_register_function(tep, func, addr, mod);
796 		}
797 
798 		line = strtok_r(NULL, "\n", &next);
799 	}
800 	free(line);
801 	ret = 0;
802  out:
803 	free(copy);
804 
805 	return ret;
806 }
807 
808 /**
809  * tep_print_funcs - print out the stored functions
810  * @tep: a handle to the trace event parser context
811  *
812  * This prints out the stored functions.
813  */
tep_print_funcs(struct tep_handle * tep)814 void tep_print_funcs(struct tep_handle *tep)
815 {
816 	int i;
817 
818 	if (!tep->func_map)
819 		func_map_init(tep);
820 
821 	for (i = 0; i < (int)tep->func_count; i++) {
822 		printf("%016llx %s",
823 		       tep->func_map[i].addr,
824 		       tep->func_map[i].func);
825 		if (tep->func_map[i].mod)
826 			printf(" [%s]\n", tep->func_map[i].mod);
827 		else
828 			printf("\n");
829 	}
830 }
831 
832 struct printk_map {
833 	unsigned long long		addr;
834 	char				*printk;
835 };
836 
837 struct printk_list {
838 	struct printk_list	*next;
839 	unsigned long long	addr;
840 	char			*printk;
841 };
842 
printk_cmp(const void * a,const void * b)843 static int printk_cmp(const void *a, const void *b)
844 {
845 	const struct printk_map *pa = a;
846 	const struct printk_map *pb = b;
847 
848 	if (pa->addr < pb->addr)
849 		return -1;
850 	if (pa->addr > pb->addr)
851 		return 1;
852 
853 	return 0;
854 }
855 
printk_map_init(struct tep_handle * tep)856 static int printk_map_init(struct tep_handle *tep)
857 {
858 	struct printk_list *printklist;
859 	struct printk_list *item;
860 	struct printk_map *printk_map;
861 	int i;
862 
863 	printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1));
864 	if (!printk_map)
865 		return -1;
866 
867 	printklist = tep->printklist;
868 
869 	i = 0;
870 	while (printklist) {
871 		printk_map[i].printk = printklist->printk;
872 		printk_map[i].addr = printklist->addr;
873 		i++;
874 		item = printklist;
875 		printklist = printklist->next;
876 		free(item);
877 	}
878 
879 	qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp);
880 
881 	tep->printk_map = printk_map;
882 	tep->printklist = NULL;
883 
884 	return 0;
885 }
886 
887 static struct printk_map *
find_printk(struct tep_handle * tep,unsigned long long addr)888 find_printk(struct tep_handle *tep, unsigned long long addr)
889 {
890 	struct printk_map *printk;
891 	struct printk_map key;
892 
893 	if (!tep->printk_map && printk_map_init(tep))
894 		return NULL;
895 
896 	key.addr = addr;
897 
898 	printk = bsearch(&key, tep->printk_map, tep->printk_count,
899 			 sizeof(*tep->printk_map), printk_cmp);
900 
901 	return printk;
902 }
903 
904 /**
905  * tep_register_print_string - register a string by its address
906  * @tep: a handle to the trace event parser context
907  * @fmt: the string format to register
908  * @addr: the address the string was located at
909  *
910  * This registers a string by the address it was stored in the kernel.
911  * The @fmt passed in is duplicated.
912  */
tep_register_print_string(struct tep_handle * tep,const char * fmt,unsigned long long addr)913 int tep_register_print_string(struct tep_handle *tep, const char *fmt,
914 			      unsigned long long addr)
915 {
916 	struct printk_list *item = malloc(sizeof(*item));
917 	char *p;
918 
919 	if (!item)
920 		return -1;
921 
922 	item->next = tep->printklist;
923 	item->addr = addr;
924 
925 	/* Strip off quotes and '\n' from the end */
926 	if (fmt[0] == '"')
927 		fmt++;
928 	item->printk = strdup(fmt);
929 	if (!item->printk)
930 		goto out_free;
931 
932 	p = item->printk + strlen(item->printk) - 1;
933 	if (*p == '"')
934 		*p = 0;
935 
936 	p -= 2;
937 	if (strcmp(p, "\\n") == 0)
938 		*p = 0;
939 
940 	tep->printklist = item;
941 	tep->printk_count++;
942 
943 	return 0;
944 
945 out_free:
946 	free(item);
947 	errno = ENOMEM;
948 	return -1;
949 }
950 
951 /**
952  * tep_print_printk - print out the stored strings
953  * @tep: a handle to the trace event parser context
954  *
955  * This prints the string formats that were stored.
956  */
tep_print_printk(struct tep_handle * tep)957 void tep_print_printk(struct tep_handle *tep)
958 {
959 	int i;
960 
961 	if (!tep->printk_map)
962 		printk_map_init(tep);
963 
964 	for (i = 0; i < (int)tep->printk_count; i++) {
965 		printf("%016llx %s\n",
966 		       tep->printk_map[i].addr,
967 		       tep->printk_map[i].printk);
968 	}
969 }
970 
971 /**
972  * tep_parse_printk_formats - Parse the address to strings
973  * @tep: a handle to the trace event parser
974  * @buf: A string buffer that holds the content of printk_formats and ends with '\0'
975  *
976  * This is a helper function to parse the address to printk formats in
977  * the kernel. Some events use %s to a kernel address that holds a constant
978  * string. The printk_formats file has a mapping of these addresses to the
979  * strings that are in the kernel. This parses the content of that file
980  * and registers those strings and their addresses so that the parsing of
981  * events can display the string as the event only has the address of the string.
982  *
983  * Returns 0 on success, and -1 on error.
984  */
tep_parse_printk_formats(struct tep_handle * tep,const char * buf)985 int tep_parse_printk_formats(struct tep_handle *tep, const char *buf)
986 {
987 	unsigned long long addr;
988 	char *addr_str;
989 	char *printk;
990 	char *copy;
991 	char *line;
992 	char *next;
993 	char *fmt;
994 	int ret = -1;
995 
996 	copy = strdup(buf);
997 	if (!copy)
998 		return -1;
999 
1000 	line = strtok_r(copy, "\n", &next);
1001 	while (line) {
1002 		addr_str = strtok_r(line, ":", &fmt);
1003 		if (!addr_str) {
1004 			tep_warning("printk format with empty entry");
1005 			break;
1006 		}
1007 		addr = strtoull(addr_str, NULL, 16);
1008 		/* fmt still has a space, skip it */
1009 		printk = strdup(fmt+1);
1010 		if (!printk)
1011 			goto out;
1012 		line = strtok_r(NULL, "\n", &next);
1013 		tep_register_print_string(tep, printk, addr);
1014 		free(printk);
1015 	}
1016 	ret = 0;
1017  out:
1018 	free(copy);
1019 	return ret;
1020 }
1021 
alloc_event(void)1022 static struct tep_event *alloc_event(void)
1023 {
1024 	return calloc(1, sizeof(struct tep_event));
1025 }
1026 
add_event(struct tep_handle * tep,struct tep_event * event)1027 static int add_event(struct tep_handle *tep, struct tep_event *event)
1028 {
1029 	int i;
1030 	struct tep_event **events = realloc(tep->events, sizeof(event) *
1031 					    (tep->nr_events + 1));
1032 	if (!events)
1033 		return -1;
1034 
1035 	tep->events = events;
1036 
1037 	for (i = 0; i < tep->nr_events; i++) {
1038 		if (tep->events[i]->id > event->id)
1039 			break;
1040 	}
1041 	if (i < tep->nr_events)
1042 		memmove(&tep->events[i + 1],
1043 			&tep->events[i],
1044 			sizeof(event) * (tep->nr_events - i));
1045 
1046 	tep->events[i] = event;
1047 	tep->nr_events++;
1048 
1049 	event->tep = tep;
1050 
1051 	return 0;
1052 }
1053 
event_item_type(enum tep_event_type type)1054 static int event_item_type(enum tep_event_type type)
1055 {
1056 	switch (type) {
1057 	case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
1058 		return 1;
1059 	case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
1060 	default:
1061 		return 0;
1062 	}
1063 }
1064 
free_flag_sym(struct tep_print_flag_sym * fsym)1065 static void free_flag_sym(struct tep_print_flag_sym *fsym)
1066 {
1067 	struct tep_print_flag_sym *next;
1068 
1069 	while (fsym) {
1070 		next = fsym->next;
1071 		free(fsym->value);
1072 		free(fsym->str);
1073 		free(fsym);
1074 		fsym = next;
1075 	}
1076 }
1077 
free_arg(struct tep_print_arg * arg)1078 static void free_arg(struct tep_print_arg *arg)
1079 {
1080 	struct tep_print_arg *farg;
1081 
1082 	if (!arg)
1083 		return;
1084 
1085 	switch (arg->type) {
1086 	case TEP_PRINT_ATOM:
1087 		free(arg->atom.atom);
1088 		break;
1089 	case TEP_PRINT_FIELD:
1090 		free(arg->field.name);
1091 		break;
1092 	case TEP_PRINT_FLAGS:
1093 		free_arg(arg->flags.field);
1094 		free(arg->flags.delim);
1095 		free_flag_sym(arg->flags.flags);
1096 		break;
1097 	case TEP_PRINT_SYMBOL:
1098 		free_arg(arg->symbol.field);
1099 		free_flag_sym(arg->symbol.symbols);
1100 		break;
1101 	case TEP_PRINT_HEX:
1102 	case TEP_PRINT_HEX_STR:
1103 		free_arg(arg->hex.field);
1104 		free_arg(arg->hex.size);
1105 		break;
1106 	case TEP_PRINT_INT_ARRAY:
1107 		free_arg(arg->int_array.field);
1108 		free_arg(arg->int_array.count);
1109 		free_arg(arg->int_array.el_size);
1110 		break;
1111 	case TEP_PRINT_TYPE:
1112 		free(arg->typecast.type);
1113 		free_arg(arg->typecast.item);
1114 		break;
1115 	case TEP_PRINT_STRING:
1116 	case TEP_PRINT_BSTRING:
1117 		free(arg->string.string);
1118 		break;
1119 	case TEP_PRINT_BITMASK:
1120 	case TEP_PRINT_CPUMASK:
1121 		free(arg->bitmask.bitmask);
1122 		break;
1123 	case TEP_PRINT_DYNAMIC_ARRAY:
1124 	case TEP_PRINT_DYNAMIC_ARRAY_LEN:
1125 		free(arg->dynarray.index);
1126 		break;
1127 	case TEP_PRINT_OP:
1128 		free(arg->op.op);
1129 		free_arg(arg->op.left);
1130 		free_arg(arg->op.right);
1131 		break;
1132 	case TEP_PRINT_FUNC:
1133 		while (arg->func.args) {
1134 			farg = arg->func.args;
1135 			arg->func.args = farg->next;
1136 			free_arg(farg);
1137 		}
1138 		break;
1139 
1140 	case TEP_PRINT_NULL:
1141 	default:
1142 		break;
1143 	}
1144 
1145 	free(arg);
1146 }
1147 
get_type(int ch)1148 static enum tep_event_type get_type(int ch)
1149 {
1150 	if (ch == '\n')
1151 		return TEP_EVENT_NEWLINE;
1152 	if (isspace(ch))
1153 		return TEP_EVENT_SPACE;
1154 	if (isalnum(ch) || ch == '_')
1155 		return TEP_EVENT_ITEM;
1156 	if (ch == '\'')
1157 		return TEP_EVENT_SQUOTE;
1158 	if (ch == '"')
1159 		return TEP_EVENT_DQUOTE;
1160 	if (!isprint(ch))
1161 		return TEP_EVENT_NONE;
1162 	if (ch == '(' || ch == ')' || ch == ',')
1163 		return TEP_EVENT_DELIM;
1164 
1165 	return TEP_EVENT_OP;
1166 }
1167 
__read_char(struct tep_handle * tep)1168 static int __read_char(struct tep_handle *tep)
1169 {
1170 	if (tep->input_buf_ptr >= tep->input_buf_siz)
1171 		return -1;
1172 
1173 	return tep->input_buf[tep->input_buf_ptr++];
1174 }
1175 
1176 /**
1177  * peek_char - peek at the next character that will be read
1178  *
1179  * Returns the next character read, or -1 if end of buffer.
1180  */
peek_char(struct tep_handle * tep)1181 __hidden int peek_char(struct tep_handle *tep)
1182 {
1183 	if (tep->input_buf_ptr >= tep->input_buf_siz)
1184 		return -1;
1185 
1186 	return tep->input_buf[tep->input_buf_ptr];
1187 }
1188 
extend_token(char ** tok,char * buf,int size)1189 static int extend_token(char **tok, char *buf, int size)
1190 {
1191 	char *newtok = realloc(*tok, size);
1192 
1193 	if (!newtok) {
1194 		free(*tok);
1195 		*tok = NULL;
1196 		return -1;
1197 	}
1198 
1199 	if (!*tok)
1200 		strcpy(newtok, buf);
1201 	else
1202 		strcat(newtok, buf);
1203 	*tok = newtok;
1204 
1205 	return 0;
1206 }
1207 
1208 static enum tep_event_type force_token(struct tep_handle *tep, const char *str,
1209 		char **tok);
1210 
__read_token(struct tep_handle * tep,char ** tok)1211 static enum tep_event_type __read_token(struct tep_handle *tep, char **tok)
1212 {
1213 	char buf[BUFSIZ];
1214 	int ch, last_ch, quote_ch, next_ch;
1215 	int i = 0;
1216 	int tok_size = 0;
1217 	enum tep_event_type type;
1218 
1219 	*tok = NULL;
1220 
1221 
1222 	ch = __read_char(tep);
1223 	if (ch < 0)
1224 		return TEP_EVENT_NONE;
1225 
1226 	type = get_type(ch);
1227 	if (type == TEP_EVENT_NONE)
1228 		return type;
1229 
1230 	buf[i++] = ch;
1231 
1232 	switch (type) {
1233 	case TEP_EVENT_NEWLINE:
1234 	case TEP_EVENT_DELIM:
1235 		*tok = malloc(2);
1236 		if (!*tok)
1237 			return TEP_EVENT_ERROR;
1238 		(*tok)[0] = ch;
1239 		(*tok)[1] = '\0';
1240 		return type;
1241 
1242 	case TEP_EVENT_OP:
1243 		switch (ch) {
1244 		case '-':
1245 			next_ch = peek_char(tep);
1246 			if (next_ch == '>') {
1247 				buf[i++] = __read_char(tep);
1248 				break;
1249 			}
1250 			/* fall through */
1251 		case '+':
1252 		case '|':
1253 		case '&':
1254 		case '>':
1255 		case '<':
1256 			last_ch = ch;
1257 			ch = peek_char(tep);
1258 			if (ch != last_ch)
1259 				goto test_equal;
1260 			buf[i++] = __read_char(tep);
1261 			switch (last_ch) {
1262 			case '>':
1263 			case '<':
1264 				goto test_equal;
1265 			default:
1266 				break;
1267 			}
1268 			break;
1269 		case '!':
1270 		case '=':
1271 			goto test_equal;
1272 		default: /* what should we do instead? */
1273 			break;
1274 		}
1275 		buf[i] = 0;
1276 		*tok = strdup(buf);
1277 		return type;
1278 
1279  test_equal:
1280 		ch = peek_char(tep);
1281 		if (ch == '=')
1282 			buf[i++] = __read_char(tep);
1283 		goto out;
1284 
1285 	case TEP_EVENT_DQUOTE:
1286 	case TEP_EVENT_SQUOTE:
1287 		/* don't keep quotes */
1288 		i--;
1289 		quote_ch = ch;
1290 		last_ch = 0;
1291  concat:
1292 		do {
1293 			if (i == (BUFSIZ - 1)) {
1294 				buf[i] = 0;
1295 				tok_size += BUFSIZ;
1296 
1297 				if (extend_token(tok, buf, tok_size) < 0)
1298 					return TEP_EVENT_NONE;
1299 				i = 0;
1300 			}
1301 			last_ch = ch;
1302 			ch = __read_char(tep);
1303 			buf[i++] = ch;
1304 			/* the '\' '\' will cancel itself */
1305 			if (ch == '\\' && last_ch == '\\')
1306 				last_ch = 0;
1307 			/* Break out if the file is corrupted and giving non print chars */
1308 			if (ch <= 0)
1309 				break;
1310 		} while ((ch != quote_ch && isprint(ch)) || last_ch == '\\' || ch == '\n');
1311 		/* remove the last quote */
1312 		i--;
1313 
1314 		if (ch <= 0)
1315 			type = TEP_EVENT_NONE;
1316 
1317 		/*
1318 		 * For strings (double quotes) check the next token.
1319 		 * If it is another string, concatinate the two.
1320 		 */
1321 		if (type == TEP_EVENT_DQUOTE) {
1322 			unsigned long long save_input_buf_ptr = tep->input_buf_ptr;
1323 
1324 			do {
1325 				ch = __read_char(tep);
1326 			} while (isspace(ch));
1327 			if (ch == '"')
1328 				goto concat;
1329 			tep->input_buf_ptr = save_input_buf_ptr;
1330 		}
1331 
1332 		goto out;
1333 
1334 	case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1335 	case TEP_EVENT_ITEM:
1336 	default:
1337 		break;
1338 	}
1339 
1340 	while (get_type(peek_char(tep)) == type) {
1341 		if (i == (BUFSIZ - 1)) {
1342 			buf[i] = 0;
1343 			tok_size += BUFSIZ;
1344 
1345 			if (extend_token(tok, buf, tok_size) < 0)
1346 				return TEP_EVENT_NONE;
1347 			i = 0;
1348 		}
1349 		ch = __read_char(tep);
1350 		buf[i++] = ch;
1351 	}
1352 
1353  out:
1354 	buf[i] = 0;
1355 	if (extend_token(tok, buf, tok_size + i + 1) < 0)
1356 		return TEP_EVENT_NONE;
1357 
1358 	if (type == TEP_EVENT_ITEM) {
1359 		/*
1360 		 * Older versions of the kernel has a bug that
1361 		 * creates invalid symbols and will break the mac80211
1362 		 * parsing. This is a work around to that bug.
1363 		 *
1364 		 * See Linux kernel commit:
1365 		 *  811cb50baf63461ce0bdb234927046131fc7fa8b
1366 		 */
1367 		if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1368 			free(*tok);
1369 			*tok = NULL;
1370 			return force_token(tep, "\"%s\" ", tok);
1371 		} else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1372 			free(*tok);
1373 			*tok = NULL;
1374 			return force_token(tep, "\" sta:%pM\" ", tok);
1375 		} else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1376 			free(*tok);
1377 			*tok = NULL;
1378 			return force_token(tep, "\" vif:%p(%d)\" ", tok);
1379 		}
1380 	}
1381 
1382 	return type;
1383 }
1384 
force_token(struct tep_handle * tep,const char * str,char ** tok)1385 static enum tep_event_type force_token(struct tep_handle *tep, const char *str,
1386 		char **tok)
1387 {
1388 	const char *save_input_buf;
1389 	unsigned long long save_input_buf_ptr;
1390 	unsigned long long save_input_buf_siz;
1391 	enum tep_event_type type;
1392 
1393 	/* save off the current input pointers */
1394 	save_input_buf = tep->input_buf;
1395 	save_input_buf_ptr = tep->input_buf_ptr;
1396 	save_input_buf_siz = tep->input_buf_siz;
1397 
1398 	init_input_buf(tep, str, strlen(str));
1399 
1400 	type = __read_token(tep, tok);
1401 
1402 	/* reset back to original token */
1403 	tep->input_buf = save_input_buf;
1404 	tep->input_buf_ptr = save_input_buf_ptr;
1405 	tep->input_buf_siz = save_input_buf_siz;
1406 
1407 	return type;
1408 }
1409 
1410 /**
1411  * free_token - free a token returned by tep_read_token
1412  * @token: the token to free
1413  */
free_token(char * tok)1414 __hidden void free_token(char *tok)
1415 {
1416 	if (tok)
1417 		free(tok);
1418 }
1419 
1420 /**
1421  * read_token - access to utilities to use the tep parser
1422  * @tok: The token to return
1423  *
1424  * This will parse tokens from the string given by
1425  * tep_init_data().
1426  *
1427  * Returns the token type.
1428  */
read_token(struct tep_handle * tep,char ** tok)1429 __hidden enum tep_event_type read_token(struct tep_handle *tep, char **tok)
1430 {
1431 	enum tep_event_type type;
1432 
1433 	for (;;) {
1434 		type = __read_token(tep, tok);
1435 		if (type != TEP_EVENT_SPACE)
1436 			return type;
1437 
1438 		free_token(*tok);
1439 	}
1440 
1441 	/* not reached */
1442 	*tok = NULL;
1443 	return TEP_EVENT_NONE;
1444 }
1445 
1446 /* no newline */
read_token_item(struct tep_handle * tep,char ** tok)1447 static enum tep_event_type read_token_item(struct tep_handle *tep, char **tok)
1448 {
1449 	enum tep_event_type type;
1450 
1451 	for (;;) {
1452 		type = __read_token(tep, tok);
1453 		if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1454 			return type;
1455 		free_token(*tok);
1456 		*tok = NULL;
1457 	}
1458 
1459 	/* not reached */
1460 	*tok = NULL;
1461 	return TEP_EVENT_NONE;
1462 }
1463 
test_type(enum tep_event_type type,enum tep_event_type expect)1464 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1465 {
1466 	if (type != expect) {
1467 		do_warning("Error: expected type %d (%s) but read %d (%s)",
1468 			   expect, get_event_type(expect),
1469 			   type, get_event_type(type));
1470 		return -1;
1471 	}
1472 	return 0;
1473 }
1474 
test_type_token(enum tep_event_type type,const char * token,enum tep_event_type expect,const char * expect_tok)1475 static int test_type_token(enum tep_event_type type, const char *token,
1476 		    enum tep_event_type expect, const char *expect_tok)
1477 {
1478 	if (type != expect) {
1479 		do_warning("Error: expected type %d (%s) but read %d (%s)",
1480 			   expect, get_event_type(expect),
1481 			   type, get_event_type(type));
1482 		return -1;
1483 	}
1484 
1485 	if (strcmp(token, expect_tok) != 0) {
1486 		do_warning("Error: expected '%s' but read '%s'",
1487 		    expect_tok, token);
1488 		return -1;
1489 	}
1490 	return 0;
1491 }
1492 
__read_expect_type(struct tep_handle * tep,enum tep_event_type expect,char ** tok,int newline_ok)1493 static int __read_expect_type(struct tep_handle *tep, enum tep_event_type expect,
1494 		char **tok, int newline_ok)
1495 {
1496 	enum tep_event_type type;
1497 
1498 	if (newline_ok)
1499 		type = read_token(tep, tok);
1500 	else
1501 		type = read_token_item(tep, tok);
1502 	return test_type(type, expect);
1503 }
1504 
read_expect_type(struct tep_handle * tep,enum tep_event_type expect,char ** tok)1505 static int read_expect_type(struct tep_handle *tep, enum tep_event_type expect,
1506 		char **tok)
1507 {
1508 	return __read_expect_type(tep, expect, tok, 1);
1509 }
1510 
__read_expected(struct tep_handle * tep,enum tep_event_type expect,const char * str,int newline_ok)1511 static int __read_expected(struct tep_handle *tep, enum tep_event_type expect,
1512 		const char *str, int newline_ok)
1513 {
1514 	enum tep_event_type type;
1515 	char *token;
1516 	int ret;
1517 
1518 	if (newline_ok)
1519 		type = read_token(tep, &token);
1520 	else
1521 		type = read_token_item(tep, &token);
1522 
1523 	ret = test_type_token(type, token, expect, str);
1524 
1525 	free_token(token);
1526 
1527 	return ret;
1528 }
1529 
read_expected(struct tep_handle * tep,enum tep_event_type expect,const char * str)1530 static int read_expected(struct tep_handle *tep, enum tep_event_type expect,
1531 		const char *str)
1532 {
1533 	return __read_expected(tep, expect, str, 1);
1534 }
1535 
read_expected_item(struct tep_handle * tep,enum tep_event_type expect,const char * str)1536 static int read_expected_item(struct tep_handle *tep, enum tep_event_type expect,
1537 		const char *str)
1538 {
1539 	return __read_expected(tep, expect, str, 0);
1540 }
1541 
event_read_name(struct tep_handle * tep)1542 static char *event_read_name(struct tep_handle *tep)
1543 {
1544 	char *token;
1545 
1546 	if (read_expected(tep, TEP_EVENT_ITEM, "name") < 0)
1547 		return NULL;
1548 
1549 	if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1550 		return NULL;
1551 
1552 	if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1553 		goto fail;
1554 
1555 	return token;
1556 
1557  fail:
1558 	free_token(token);
1559 	return NULL;
1560 }
1561 
event_read_id(struct tep_handle * tep)1562 static int event_read_id(struct tep_handle *tep)
1563 {
1564 	char *token;
1565 	int id;
1566 
1567 	if (read_expected_item(tep, TEP_EVENT_ITEM, "ID") < 0)
1568 		return -1;
1569 
1570 	if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1571 		return -1;
1572 
1573 	if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1574 		goto fail;
1575 
1576 	id = strtoul(token, NULL, 0);
1577 	free_token(token);
1578 	return id;
1579 
1580  fail:
1581 	free_token(token);
1582 	return -1;
1583 }
1584 
field_is_string(struct tep_format_field * field)1585 static int field_is_string(struct tep_format_field *field)
1586 {
1587 	if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1588 	    (strstr(field->type, "char") || strstr(field->type, "u8") ||
1589 	     strstr(field->type, "s8")))
1590 		return 1;
1591 
1592 	return 0;
1593 }
1594 
field_is_dynamic(struct tep_format_field * field)1595 static int field_is_dynamic(struct tep_format_field *field)
1596 {
1597 	if (strncmp(field->type, "__data_loc", 10) == 0)
1598 		return 1;
1599 
1600 	return 0;
1601 }
1602 
field_is_relative_dynamic(struct tep_format_field * field)1603 static int field_is_relative_dynamic(struct tep_format_field *field)
1604 {
1605 	if (strncmp(field->type, "__rel_loc", 9) == 0)
1606 		return 1;
1607 
1608 	return 0;
1609 }
1610 
field_is_long(struct tep_format_field * field)1611 static int field_is_long(struct tep_format_field *field)
1612 {
1613 	/* includes long long */
1614 	if (strstr(field->type, "long"))
1615 		return 1;
1616 
1617 	return 0;
1618 }
1619 
type_size(const char * name)1620 static unsigned int type_size(const char *name)
1621 {
1622 	/* This covers all TEP_FIELD_IS_STRING types. */
1623 	static struct {
1624 		const char *type;
1625 		unsigned int size;
1626 	} table[] = {
1627 		{ "u8",   1 },
1628 		{ "u16",  2 },
1629 		{ "u32",  4 },
1630 		{ "u64",  8 },
1631 		{ "s8",   1 },
1632 		{ "s16",  2 },
1633 		{ "s32",  4 },
1634 		{ "s64",  8 },
1635 		{ "char", 1 },
1636 		{ },
1637 	};
1638 	int i;
1639 
1640 	for (i = 0; table[i].type; i++) {
1641 		if (!strcmp(table[i].type, name))
1642 			return table[i].size;
1643 	}
1644 
1645 	return 0;
1646 }
1647 
append(char ** buf,const char * delim,const char * str)1648 static int append(char **buf, const char *delim, const char *str)
1649 {
1650 	char *new_buf;
1651 
1652 	new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1);
1653 	if (!new_buf)
1654 		return -1;
1655 	strcat(new_buf, delim);
1656 	strcat(new_buf, str);
1657 	*buf = new_buf;
1658 	return 0;
1659 }
1660 
event_read_fields(struct tep_handle * tep,struct tep_event * event,struct tep_format_field ** fields)1661 static int event_read_fields(struct tep_handle *tep, struct tep_event *event,
1662 		struct tep_format_field **fields)
1663 {
1664 	struct tep_format_field *field = NULL;
1665 	enum tep_event_type type;
1666 	char *token;
1667 	char *last_token;
1668 	char *delim = " ";
1669 	int count = 0;
1670 	int ret;
1671 
1672 	do {
1673 		unsigned int size_dynamic = 0;
1674 
1675 		type = read_token(tep, &token);
1676 		if (type == TEP_EVENT_NEWLINE) {
1677 			free_token(token);
1678 			return count;
1679 		}
1680 
1681 		count++;
1682 
1683 		if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1684 			goto fail;
1685 		free_token(token);
1686 
1687 		type = read_token(tep, &token);
1688 		/*
1689 		 * The ftrace fields may still use the "special" name.
1690 		 * Just ignore it.
1691 		 */
1692 		if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1693 		    type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1694 			free_token(token);
1695 			type = read_token(tep, &token);
1696 		}
1697 
1698 		if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1699 			goto fail;
1700 
1701 		free_token(token);
1702 		if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1703 			goto fail;
1704 
1705 		last_token = token;
1706 
1707 		field = calloc(1, sizeof(*field));
1708 		if (!field)
1709 			goto fail;
1710 
1711 		field->event = event;
1712 
1713 		/* read the rest of the type */
1714 		for (;;) {
1715 			type = read_token(tep, &token);
1716 			if (type == TEP_EVENT_ITEM ||
1717 			    (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1718 			    /*
1719 			     * Some of the ftrace fields are broken and have
1720 			     * an illegal "." in them.
1721 			     */
1722 			    (event->flags & TEP_EVENT_FL_ISFTRACE &&
1723 			     type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1724 
1725 				if (strcmp(token, "*") == 0)
1726 					field->flags |= TEP_FIELD_IS_POINTER;
1727 
1728 				if (field->type) {
1729 					ret = append(&field->type, delim, last_token);
1730 					free(last_token);
1731 					if (ret < 0)
1732 						goto fail;
1733 				} else
1734 					field->type = last_token;
1735 				last_token = token;
1736 				delim = " ";
1737 				continue;
1738 			}
1739 
1740 			/* Handle __attribute__((user)) */
1741 			if ((type == TEP_EVENT_DELIM) &&
1742 			    strcmp("__attribute__", last_token) == 0 &&
1743 			    token[0] == '(') {
1744 				int depth = 1;
1745 				int ret;
1746 
1747 				ret = append(&field->type, " ", last_token);
1748 				ret |= append(&field->type, "", "(");
1749 				if (ret < 0)
1750 					goto fail;
1751 
1752 				delim = " ";
1753 				while ((type = read_token(tep, &token)) != TEP_EVENT_NONE) {
1754 					if (type == TEP_EVENT_DELIM) {
1755 						if (token[0] == '(')
1756 							depth++;
1757 						else if (token[0] == ')')
1758 							depth--;
1759 						if (!depth)
1760 							break;
1761 						ret = append(&field->type, "", token);
1762 						delim = "";
1763 					} else {
1764 						ret = append(&field->type, delim, token);
1765 						delim = " ";
1766 					}
1767 					if (ret < 0)
1768 						goto fail;
1769 					free(last_token);
1770 					last_token = token;
1771 				}
1772 				continue;
1773 			}
1774 			break;
1775 		}
1776 
1777 		if (!field->type) {
1778 			do_warning_event(event, "%s: no type found", __func__);
1779 			goto fail;
1780 		}
1781 		field->name = field->alias = last_token;
1782 
1783 		if (test_type(type, TEP_EVENT_OP))
1784 			goto fail;
1785 
1786 		if (strcmp(token, "[") == 0) {
1787 			enum tep_event_type last_type = type;
1788 			char *brackets = token;
1789 
1790 			field->flags |= TEP_FIELD_IS_ARRAY;
1791 
1792 			type = read_token(tep, &token);
1793 
1794 			if (type == TEP_EVENT_ITEM)
1795 				field->arraylen = strtoul(token, NULL, 0);
1796 			else
1797 				field->arraylen = 0;
1798 
1799 		        while (strcmp(token, "]") != 0) {
1800 				const char *delim;
1801 
1802 				if (last_type == TEP_EVENT_ITEM &&
1803 				    type == TEP_EVENT_ITEM)
1804 					delim = " ";
1805 				else
1806 					delim = "";
1807 
1808 				last_type = type;
1809 
1810 				ret = append(&brackets, delim, token);
1811 				if (ret < 0) {
1812 					free(brackets);
1813 					goto fail;
1814 				}
1815 				/* We only care about the last token */
1816 				field->arraylen = strtoul(token, NULL, 0);
1817 				free_token(token);
1818 				type = read_token(tep, &token);
1819 				if (type == TEP_EVENT_NONE) {
1820 					free(brackets);
1821 					do_warning_event(event, "failed to find token");
1822 					goto fail;
1823 				}
1824 			}
1825 
1826 			free_token(token);
1827 
1828 			ret = append(&brackets, "", "]");
1829 			if (ret < 0) {
1830 				free(brackets);
1831 				goto fail_expect;
1832 			}
1833 
1834 			/* add brackets to type */
1835 
1836 			type = read_token(tep, &token);
1837 			/*
1838 			 * If the next token is not an OP, then it is of
1839 			 * the format: type [] item;
1840 			 */
1841 			if (type == TEP_EVENT_ITEM) {
1842 				ret = append(&field->type, " ", field->name);
1843 				if (ret < 0) {
1844 					free(brackets);
1845 					goto fail;
1846 				}
1847 				ret = append(&field->type, "", brackets);
1848 
1849 				size_dynamic = type_size(field->name);
1850 				free_token(field->name);
1851 				field->name = field->alias = token;
1852 				type = read_token(tep, &token);
1853 			} else {
1854 				ret = append(&field->type, "", brackets);
1855 				if (ret < 0) {
1856 					free(brackets);
1857 					goto fail;
1858 				}
1859 			}
1860 			free(brackets);
1861 		}
1862 
1863 		if (field_is_string(field))
1864 			field->flags |= TEP_FIELD_IS_STRING;
1865 		if (field_is_dynamic(field))
1866 			field->flags |= TEP_FIELD_IS_DYNAMIC;
1867 		if (field_is_relative_dynamic(field))
1868 			field->flags |= TEP_FIELD_IS_DYNAMIC | TEP_FIELD_IS_RELATIVE;
1869 		if (field_is_long(field))
1870 			field->flags |= TEP_FIELD_IS_LONG;
1871 
1872 		if (test_type_token(type, token,  TEP_EVENT_OP, ";"))
1873 			goto fail;
1874 		free_token(token);
1875 
1876 		if (read_expected(tep, TEP_EVENT_ITEM, "offset") < 0)
1877 			goto fail_expect;
1878 
1879 		if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1880 			goto fail_expect;
1881 
1882 		if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1883 			goto fail;
1884 		field->offset = strtoul(token, NULL, 0);
1885 		free_token(token);
1886 
1887 		if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1888 			goto fail_expect;
1889 
1890 		if (read_expected(tep, TEP_EVENT_ITEM, "size") < 0)
1891 			goto fail_expect;
1892 
1893 		if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1894 			goto fail_expect;
1895 
1896 		if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1897 			goto fail;
1898 		field->size = strtoul(token, NULL, 0);
1899 		free_token(token);
1900 
1901 		/*
1902 		 * The old data format before dynamic arrays had dynamic
1903 		 * strings defined with just a 2 byte offset (the length
1904 		 * is defined by the strlen() of the string. To process them
1905 		 * correctly, check if the field is dynamic and has a size of
1906 		 * 2 bytes. All current dynamic events have a size of 4.
1907 		 */
1908 		if ((field->flags & TEP_FIELD_IS_DYNAMIC) && field->size == 2)
1909 			field->flags |= TEP_FIELD_IS_STRING | TEP_FIELD_IS_ARRAY;
1910 
1911 		if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1912 			goto fail_expect;
1913 
1914 		type = read_token(tep, &token);
1915 		if (type != TEP_EVENT_NEWLINE) {
1916 			/* newer versions of the kernel have a "signed" type */
1917 			if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1918 				goto fail;
1919 
1920 			free_token(token);
1921 
1922 			if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1923 				goto fail_expect;
1924 
1925 			if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1926 				goto fail;
1927 
1928 			if (strtoul(token, NULL, 0))
1929 				field->flags |= TEP_FIELD_IS_SIGNED;
1930 
1931 			free_token(token);
1932 			if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1933 				goto fail_expect;
1934 
1935 			if (read_expect_type(tep, TEP_EVENT_NEWLINE, &token))
1936 				goto fail;
1937 		}
1938 
1939 		free_token(token);
1940 
1941 		if (field->flags & (TEP_FIELD_IS_ARRAY | TEP_FIELD_IS_DYNAMIC)) {
1942 			if (field->arraylen)
1943 				field->elementsize = field->size / field->arraylen;
1944 			else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1945 				field->elementsize = size_dynamic;
1946 			else if (field->flags & TEP_FIELD_IS_STRING)
1947 				field->elementsize = 1;
1948 			else if (field->flags & TEP_FIELD_IS_LONG)
1949 				field->elementsize = event->tep ?
1950 						     event->tep->long_size :
1951 						     sizeof(long);
1952 		} else
1953 			field->elementsize = field->size;
1954 
1955 		*fields = field;
1956 		fields = &field->next;
1957 		field = NULL;
1958 
1959 	} while (1);
1960 
1961 	return 0;
1962 
1963 fail:
1964 	free_token(token);
1965 fail_expect:
1966 	if (field) {
1967 		free(field->type);
1968 		free(field->name);
1969 		free(field);
1970 	}
1971 	return -1;
1972 }
1973 
event_read_format(struct tep_event * event)1974 static int event_read_format(struct tep_event *event)
1975 {
1976 	char *token;
1977 	int ret;
1978 
1979 	if (read_expected_item(event->tep, TEP_EVENT_ITEM, "format") < 0)
1980 		return -1;
1981 
1982 	if (read_expected(event->tep, TEP_EVENT_OP, ":") < 0)
1983 		return -1;
1984 
1985 	if (read_expect_type(event->tep, TEP_EVENT_NEWLINE, &token))
1986 		goto fail;
1987 	free_token(token);
1988 
1989 	ret = event_read_fields(event->tep, event, &event->format.common_fields);
1990 	if (ret < 0)
1991 		return ret;
1992 	event->format.nr_common = ret;
1993 
1994 	ret = event_read_fields(event->tep, event, &event->format.fields);
1995 	if (ret < 0)
1996 		return ret;
1997 	event->format.nr_fields = ret;
1998 
1999 	return 0;
2000 
2001  fail:
2002 	free_token(token);
2003 	return -1;
2004 }
2005 
2006 static enum tep_event_type
2007 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
2008 		  char **tok, enum tep_event_type type);
2009 
2010 static enum tep_event_type
process_arg(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2011 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2012 {
2013 	enum tep_event_type type;
2014 	char *token;
2015 
2016 	type = read_token(event->tep, &token);
2017 	*tok = token;
2018 
2019 	return process_arg_token(event, arg, tok, type);
2020 }
2021 
2022 static enum tep_event_type
2023 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
2024 
2025 /*
2026  * For __print_symbolic() and __print_flags, we need to completely
2027  * evaluate the first argument, which defines what to print next.
2028  */
2029 static enum tep_event_type
process_field_arg(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2030 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2031 {
2032 	enum tep_event_type type;
2033 
2034 	type = process_arg(event, arg, tok);
2035 
2036 	while (type == TEP_EVENT_OP) {
2037 		type = process_op(event, arg, tok);
2038 	}
2039 
2040 	return type;
2041 }
2042 
2043 static enum tep_event_type
process_cond(struct tep_event * event,struct tep_print_arg * top,char ** tok)2044 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
2045 {
2046 	struct tep_print_arg *arg, *left, *right;
2047 	enum tep_event_type type;
2048 	char *token = NULL;
2049 
2050 	arg = alloc_arg();
2051 	left = alloc_arg();
2052 	right = alloc_arg();
2053 
2054 	if (!arg || !left || !right) {
2055 		do_warning_event(event, "%s: not enough memory!", __func__);
2056 		/* arg will be freed at out_free */
2057 		free_arg(left);
2058 		free_arg(right);
2059 		goto out_free;
2060 	}
2061 
2062 	arg->type = TEP_PRINT_OP;
2063 	arg->op.left = left;
2064 	arg->op.right = right;
2065 
2066 	*tok = NULL;
2067 	type = process_arg(event, left, &token);
2068 
2069  again:
2070 	if (type == TEP_EVENT_ERROR)
2071 		goto out_free;
2072 
2073 	/* Handle other operations in the arguments */
2074 	if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
2075 		type = process_op(event, left, &token);
2076 		goto again;
2077 	}
2078 
2079 	if (test_type_token(type, token, TEP_EVENT_OP, ":"))
2080 		goto out_free;
2081 
2082 	arg->op.op = token;
2083 
2084 	type = process_arg(event, right, &token);
2085 
2086 	top->op.right = arg;
2087 
2088 	*tok = token;
2089 	return type;
2090 
2091 out_free:
2092 	/* Top may point to itself */
2093 	top->op.right = NULL;
2094 	free_token(token);
2095 	free_arg(arg);
2096 	return TEP_EVENT_ERROR;
2097 }
2098 
2099 static enum tep_event_type
process_array(struct tep_event * event,struct tep_print_arg * top,char ** tok)2100 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
2101 {
2102 	struct tep_print_arg *arg;
2103 	enum tep_event_type type;
2104 	char *token = NULL;
2105 
2106 	arg = alloc_arg();
2107 	if (!arg) {
2108 		do_warning_event(event, "%s: not enough memory!", __func__);
2109 		/* '*tok' is set to top->op.op.  No need to free. */
2110 		*tok = NULL;
2111 		return TEP_EVENT_ERROR;
2112 	}
2113 
2114 	*tok = NULL;
2115 	type = process_arg(event, arg, &token);
2116 	if (test_type_token(type, token, TEP_EVENT_OP, "]"))
2117 		goto out_free;
2118 
2119 	top->op.right = arg;
2120 
2121 	free_token(token);
2122 	type = read_token_item(event->tep, &token);
2123 	*tok = token;
2124 
2125 	return type;
2126 
2127 out_free:
2128 	free_token(token);
2129 	free_arg(arg);
2130 	return TEP_EVENT_ERROR;
2131 }
2132 
get_op_prio(char * op)2133 static int get_op_prio(char *op)
2134 {
2135 	if (strlen(op) == 1) {
2136 		switch (op[0]) {
2137 		case '~':
2138 		case '!':
2139 			return 4;
2140 		case '*':
2141 		case '/':
2142 		case '%':
2143 			return 6;
2144 		case '+':
2145 		case '-':
2146 			return 7;
2147 			/* '>>' and '<<' are 8 */
2148 		case '<':
2149 		case '>':
2150 			return 9;
2151 			/* '==' and '!=' are 10 */
2152 		case '&':
2153 			return 11;
2154 		case '^':
2155 			return 12;
2156 		case '|':
2157 			return 13;
2158 		case '?':
2159 			return 16;
2160 		default:
2161 			do_warning("unknown op '%c'", op[0]);
2162 			return -1;
2163 		}
2164 	} else {
2165 		if (strcmp(op, "++") == 0 ||
2166 		    strcmp(op, "--") == 0) {
2167 			return 3;
2168 		} else if (strcmp(op, ">>") == 0 ||
2169 			   strcmp(op, "<<") == 0) {
2170 			return 8;
2171 		} else if (strcmp(op, ">=") == 0 ||
2172 			   strcmp(op, "<=") == 0) {
2173 			return 9;
2174 		} else if (strcmp(op, "==") == 0 ||
2175 			   strcmp(op, "!=") == 0) {
2176 			return 10;
2177 		} else if (strcmp(op, "&&") == 0) {
2178 			return 14;
2179 		} else if (strcmp(op, "||") == 0) {
2180 			return 15;
2181 		} else {
2182 			do_warning("unknown op '%s'", op);
2183 			return -1;
2184 		}
2185 	}
2186 }
2187 
set_op_prio(struct tep_print_arg * arg)2188 static int set_op_prio(struct tep_print_arg *arg)
2189 {
2190 
2191 	/* single ops are the greatest */
2192 	if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
2193 		arg->op.prio = 0;
2194 	else
2195 		arg->op.prio = get_op_prio(arg->op.op);
2196 
2197 	return arg->op.prio;
2198 }
2199 
consolidate_op_arg(enum tep_event_type type,struct tep_print_arg * arg)2200 static int consolidate_op_arg(enum tep_event_type type, struct tep_print_arg *arg)
2201 {
2202 	unsigned long long val, left, right;
2203 	int ret = 0;
2204 
2205 	if (type == TEP_EVENT_ERROR)
2206 		return -1;
2207 
2208 	if (arg->type != TEP_PRINT_OP)
2209 		return 0;
2210 
2211 	if (arg->op.left)
2212 		ret = consolidate_op_arg(type, arg->op.left);
2213 	if (ret < 0)
2214 		return ret;
2215 
2216 	if (arg->op.right)
2217 		ret = consolidate_op_arg(type, arg->op.right);
2218 	if (ret < 0)
2219 		return ret;
2220 
2221 	if (!arg->op.left || !arg->op.right)
2222 		return 0;
2223 
2224 	if (arg->op.left->type != TEP_PRINT_ATOM ||
2225 	    arg->op.right->type != TEP_PRINT_ATOM)
2226 		return 0;
2227 
2228 	/* Two atoms, we can do the operation now. */
2229 	left = strtoull(arg->op.left->atom.atom, NULL, 0);
2230 	right = strtoull(arg->op.right->atom.atom, NULL, 0);
2231 
2232 	switch (arg->op.op[0]) {
2233 	case '>':
2234 		switch (arg->op.op[1]) {
2235 		case '>':
2236 			val = left >> right;
2237 			break;
2238 		case '=':
2239 			val = left >= right;
2240 			break;
2241 		default:
2242 			val = left > right;
2243 			break;
2244 		}
2245 		break;
2246 	case '<':
2247 		switch (arg->op.op[1]) {
2248 		case '<':
2249 			val = left << right;
2250 			break;
2251 		case '=':
2252 			val = left <= right;
2253 			break;
2254 		default:
2255 			val = left < right;
2256 			break;
2257 		}
2258 		break;
2259 	case '&':
2260 		switch (arg->op.op[1]) {
2261 		case '&':
2262 			val = left && right;
2263 			break;
2264 		default:
2265 			val = left & right;
2266 			break;
2267 		}
2268 		break;
2269 	case '|':
2270 		switch (arg->op.op[1]) {
2271 		case '|':
2272 			val = left || right;
2273 			break;
2274 		default:
2275 			val = left | right;
2276 			break;
2277 		}
2278 		break;
2279 	case '-':
2280 		val = left - right;
2281 		break;
2282 	case '+':
2283 		val = left + right;
2284 		break;
2285 	case '*':
2286 		val = left * right;
2287 		break;
2288 	case '^':
2289 		val = left ^ right;
2290 		break;
2291 	case '/':
2292 		val = left / right;
2293 		break;
2294 	case '%':
2295 		val = left % right;
2296 		break;
2297 	case '=':
2298 		/* Only '==' is called here */
2299 		val = left == right;
2300 		break;
2301 	case '!':
2302 		/* Only '!=' is called here. */
2303 		val = left != right;
2304 		break;
2305 	default:
2306 		return 0;
2307 	}
2308 
2309 	free_arg(arg->op.left);
2310 	free_arg(arg->op.right);
2311 
2312 	arg->type = TEP_PRINT_ATOM;
2313 	free(arg->op.op);
2314 	return asprintf(&arg->atom.atom, "%lld", val) < 0 ? -1 : 0;
2315 }
2316 
2317 /* Note, *tok does not get freed, but will most likely be saved */
2318 static enum tep_event_type
process_op(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2319 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2320 {
2321 	struct tep_print_arg *left, *right = NULL;
2322 	enum tep_event_type type;
2323 	char *token;
2324 
2325 	/* the op is passed in via tok */
2326 	token = *tok;
2327 
2328 	if (arg->type == TEP_PRINT_OP && !arg->op.left) {
2329 		/* handle single op */
2330 		switch (token[0]) {
2331 		case '~':
2332 		case '!':
2333 		case '+':
2334 		case '-':
2335 			break;
2336 		default:
2337 			do_warning_event(event, "bad op token %s", token);
2338 			goto out_free;
2339 
2340 		}
2341 		if (token[1]) {
2342 			do_warning_event(event, "bad op token %s", token);
2343 			goto out_free;
2344 		}
2345 
2346 		/* make an empty left */
2347 		left = alloc_arg();
2348 		if (!left)
2349 			goto out_warn_free;
2350 
2351 		left->type = TEP_PRINT_NULL;
2352 		arg->op.left = left;
2353 
2354 		right = alloc_arg();
2355 		if (!right)
2356 			goto out_warn_free;
2357 
2358 		arg->op.right = right;
2359 
2360 		/* do not free the token, it belongs to an op */
2361 		*tok = NULL;
2362 		type = process_arg(event, right, tok);
2363 
2364 	} else if (strcmp(token, "?") == 0) {
2365 
2366 		left = alloc_arg();
2367 		if (!left)
2368 			goto out_warn_free;
2369 
2370 		/* copy the top arg to the left */
2371 		*left = *arg;
2372 
2373 		arg->type = TEP_PRINT_OP;
2374 		arg->op.op = token;
2375 		arg->op.left = left;
2376 		arg->op.right = NULL;
2377 		arg->op.prio = 0;
2378 
2379 		/* it will set arg->op.right */
2380 		type = process_cond(event, arg, tok);
2381 
2382 	} else if (strcmp(token, ">>") == 0 ||
2383 		   strcmp(token, "<<") == 0 ||
2384 		   strcmp(token, "&") == 0 ||
2385 		   strcmp(token, "|") == 0 ||
2386 		   strcmp(token, "&&") == 0 ||
2387 		   strcmp(token, "||") == 0 ||
2388 		   strcmp(token, "-") == 0 ||
2389 		   strcmp(token, "+") == 0 ||
2390 		   strcmp(token, "*") == 0 ||
2391 		   strcmp(token, "^") == 0 ||
2392 		   strcmp(token, "/") == 0 ||
2393 		   strcmp(token, "%") == 0 ||
2394 		   strcmp(token, "<") == 0 ||
2395 		   strcmp(token, ">") == 0 ||
2396 		   strcmp(token, "<=") == 0 ||
2397 		   strcmp(token, ">=") == 0 ||
2398 		   strcmp(token, "==") == 0 ||
2399 		   strcmp(token, "!=") == 0) {
2400 
2401 		left = alloc_arg();
2402 		if (!left)
2403 			goto out_warn_free;
2404 
2405 		/* copy the top arg to the left */
2406 		*left = *arg;
2407 
2408 		arg->type = TEP_PRINT_OP;
2409 		arg->op.op = token;
2410 		arg->op.left = left;
2411 		arg->op.right = NULL;
2412 
2413 		if (set_op_prio(arg) == -1) {
2414 			event->flags |= TEP_EVENT_FL_FAILED;
2415 			/* arg->op.op (= token) will be freed at out_free */
2416 			arg->op.op = NULL;
2417 			goto out_free;
2418 		}
2419 
2420 		type = read_token_item(event->tep, &token);
2421 		*tok = token;
2422 
2423 		/* could just be a type pointer */
2424 		if ((strcmp(arg->op.op, "*") == 0) &&
2425 		    type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2426 			int ret;
2427 
2428 			if (left->type != TEP_PRINT_ATOM) {
2429 				do_warning_event(event, "bad pointer type");
2430 				goto out_free;
2431 			}
2432 			ret = append(&left->atom.atom, " ", "*");
2433 			if (ret < 0)
2434 				goto out_warn_free;
2435 
2436 			free(arg->op.op);
2437 			*arg = *left;
2438 			free(left);
2439 
2440 			return type;
2441 		}
2442 
2443 		right = alloc_arg();
2444 		if (!right)
2445 			goto out_warn_free;
2446 
2447 		type = process_arg_token(event, right, tok, type);
2448 		if (type == TEP_EVENT_ERROR) {
2449 			free_arg(right);
2450 			/* token was freed in process_arg_token() via *tok */
2451 			token = NULL;
2452 			goto out_free;
2453 		}
2454 
2455 		if (right->type == TEP_PRINT_OP &&
2456 		    get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2457 			struct tep_print_arg tmp;
2458 
2459 			/* rotate ops according to the priority */
2460 			arg->op.right = right->op.left;
2461 
2462 			tmp = *arg;
2463 			*arg = *right;
2464 			*right = tmp;
2465 
2466 			arg->op.left = right;
2467 		} else {
2468 			arg->op.right = right;
2469 		}
2470 
2471 	} else if (strcmp(token, "[") == 0) {
2472 
2473 		left = alloc_arg();
2474 		if (!left)
2475 			goto out_warn_free;
2476 
2477 		*left = *arg;
2478 
2479 		arg->type = TEP_PRINT_OP;
2480 		arg->op.op = token;
2481 		arg->op.left = left;
2482 		arg->op.right = NULL;
2483 
2484 		arg->op.prio = 0;
2485 
2486 		/* it will set arg->op.right */
2487 		type = process_array(event, arg, tok);
2488 
2489 	} else {
2490 		do_warning_event(event, "unknown op '%s'", token);
2491 		event->flags |= TEP_EVENT_FL_FAILED;
2492 		/* the arg is now the left side */
2493 		goto out_free;
2494 	}
2495 
2496 	if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2497 		int prio;
2498 
2499 		/* higher prios need to be closer to the root */
2500 		prio = get_op_prio(*tok);
2501 
2502 		if (prio > arg->op.prio)
2503 			return process_op(event, arg, tok);
2504 
2505 		return process_op(event, right, tok);
2506 	}
2507 
2508 	return type;
2509 
2510 out_warn_free:
2511 	do_warning_event(event, "%s: not enough memory!", __func__);
2512 out_free:
2513 	free_token(token);
2514 	*tok = NULL;
2515 	return TEP_EVENT_ERROR;
2516 }
2517 
2518 static enum tep_event_type
process_entry(struct tep_event * event __maybe_unused,struct tep_print_arg * arg,char ** tok)2519 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2520 	      char **tok)
2521 {
2522 	enum tep_event_type type;
2523 	char *field;
2524 	char *token;
2525 
2526 	type = read_token_item(event->tep, &token);
2527 	/*
2528 	 * Check if REC happens to be surrounded by parenthesis, and
2529 	 * return if that's the case, as "(REC)->" is valid.
2530 	 * but return TEP_EVENT_ITEM.
2531 	 */
2532 	if (type == TEP_EVENT_DELIM && strcmp(token, ")") == 0) {
2533 		*tok = token;
2534 		return TEP_EVENT_ITEM;
2535 	}
2536 
2537 	if (test_type_token(type, token,  TEP_EVENT_OP, "->"))
2538 		goto out_free;
2539 
2540 	free_token(token);
2541 
2542 	if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
2543 		goto out_free;
2544 	field = token;
2545 
2546 	arg->type = TEP_PRINT_FIELD;
2547 	arg->field.name = field;
2548 
2549 	arg->field.field = tep_find_any_field(event, arg->field.name);
2550 
2551 	if (is_flag_field) {
2552 		arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2553 		is_flag_field = 0;
2554 	} else if (is_symbolic_field) {
2555 		arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2556 		is_symbolic_field = 0;
2557 	}
2558 
2559 	type = read_token(event->tep, &token);
2560 	*tok = token;
2561 
2562 	return type;
2563 
2564  out_free:
2565 	free_token(token);
2566 	*tok = NULL;
2567 	return TEP_EVENT_ERROR;
2568 }
2569 
alloc_and_process_delim(struct tep_event * event,char * next_token,struct tep_print_arg ** print_arg)2570 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2571 				   struct tep_print_arg **print_arg)
2572 {
2573 	struct tep_print_arg *field;
2574 	enum tep_event_type type;
2575 	char *token;
2576 	int ret = 0;
2577 
2578 	field = alloc_arg();
2579 	if (!field) {
2580 		do_warning_event(event, "%s: not enough memory!", __func__);
2581 		errno = ENOMEM;
2582 		return -1;
2583 	}
2584 
2585 	type = process_arg(event, field, &token);
2586 
2587 	/* We do allow operators */
2588 	if (type == TEP_EVENT_OP) {
2589 		type = process_op(event, field, &token);
2590 
2591 		if (consolidate_op_arg(type, field) < 0)
2592 			type = TEP_EVENT_ERROR;
2593 
2594 		if (type == TEP_EVENT_ERROR)
2595 			goto out_error;
2596 	}
2597 
2598 	if (test_type_token(type, token, TEP_EVENT_DELIM, next_token))
2599 		goto out_error;
2600 
2601 	*print_arg = field;
2602 
2603 out_free_token:
2604 	free_token(token);
2605 
2606 	return ret;
2607 out_error:
2608 	errno = EINVAL;
2609 	ret = -1;
2610 	free_arg(field);
2611 	goto out_free_token;
2612 }
2613 
2614 static char *arg_eval (struct tep_print_arg *arg);
2615 
2616 static unsigned long long
eval_type_str(unsigned long long val,const char * type,int pointer)2617 eval_type_str(unsigned long long val, const char *type, int pointer)
2618 {
2619 	int sign = 0;
2620 	char *ref;
2621 	int len;
2622 
2623 	len = strlen(type);
2624 	if (len < 2) {
2625 		do_warning("invalid type: %s", type);
2626 		return val;
2627 	}
2628 
2629 	if (pointer) {
2630 
2631 		if (type[len-1] != '*') {
2632 			do_warning("pointer expected with non pointer type");
2633 			return val;
2634 		}
2635 
2636 		ref = malloc(len);
2637 		if (!ref) {
2638 			do_warning("%s: not enough memory!", __func__);
2639 			return val;
2640 		}
2641 		memcpy(ref, type, len);
2642 
2643 		/* chop off the " *" */
2644 		ref[len - 2] = 0;
2645 
2646 		val = eval_type_str(val, ref, 0);
2647 		free(ref);
2648 		return val;
2649 	}
2650 
2651 	/* check if this is a pointer */
2652 	if (type[len - 1] == '*')
2653 		return val;
2654 
2655 	/* Try to figure out the arg size*/
2656 	if (strncmp(type, "struct", 6) == 0)
2657 		/* all bets off */
2658 		return val;
2659 
2660 	if (strcmp(type, "u8") == 0)
2661 		return val & 0xff;
2662 
2663 	if (strcmp(type, "u16") == 0)
2664 		return val & 0xffff;
2665 
2666 	if (strcmp(type, "u32") == 0)
2667 		return val & 0xffffffff;
2668 
2669 	if (strcmp(type, "u64") == 0 ||
2670 	    strcmp(type, "s64") == 0)
2671 		return val;
2672 
2673 	if (strcmp(type, "s8") == 0)
2674 		return (unsigned long long)(char)val & 0xff;
2675 
2676 	if (strcmp(type, "s16") == 0)
2677 		return (unsigned long long)(short)val & 0xffff;
2678 
2679 	if (strcmp(type, "s32") == 0)
2680 		return (unsigned long long)(int)val & 0xffffffff;
2681 
2682 	if (strncmp(type, "unsigned ", 9) == 0) {
2683 		sign = 0;
2684 		type += 9;
2685 	}
2686 
2687 	if (strcmp(type, "char") == 0) {
2688 		if (sign)
2689 			return (unsigned long long)(char)val & 0xff;
2690 		else
2691 			return val & 0xff;
2692 	}
2693 
2694 	if (strcmp(type, "short") == 0) {
2695 		if (sign)
2696 			return (unsigned long long)(short)val & 0xffff;
2697 		else
2698 			return val & 0xffff;
2699 	}
2700 
2701 	if (strcmp(type, "int") == 0) {
2702 		if (sign)
2703 			return (unsigned long long)(int)val & 0xffffffff;
2704 		else
2705 			return val & 0xffffffff;
2706 	}
2707 
2708 	return val;
2709 }
2710 
2711 /*
2712  * Try to figure out the type.
2713  */
2714 static unsigned long long
eval_type(unsigned long long val,struct tep_print_arg * arg,int pointer)2715 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2716 {
2717 	if (arg->type != TEP_PRINT_TYPE) {
2718 		do_warning("expected type argument");
2719 		return 0;
2720 	}
2721 
2722 	return eval_type_str(val, arg->typecast.type, pointer);
2723 }
2724 
arg_num_eval(struct tep_print_arg * arg,long long * val)2725 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2726 {
2727 	long long left, right;
2728 	int ret = 1;
2729 
2730 	switch (arg->type) {
2731 	case TEP_PRINT_ATOM:
2732 		*val = strtoll(arg->atom.atom, NULL, 0);
2733 		break;
2734 	case TEP_PRINT_TYPE:
2735 		ret = arg_num_eval(arg->typecast.item, val);
2736 		if (!ret)
2737 			break;
2738 		*val = eval_type(*val, arg, 0);
2739 		break;
2740 	case TEP_PRINT_OP:
2741 		switch (arg->op.op[0]) {
2742 		case '|':
2743 			ret = arg_num_eval(arg->op.left, &left);
2744 			if (!ret)
2745 				break;
2746 			ret = arg_num_eval(arg->op.right, &right);
2747 			if (!ret)
2748 				break;
2749 			if (arg->op.op[1])
2750 				*val = left || right;
2751 			else
2752 				*val = left | right;
2753 			break;
2754 		case '&':
2755 			ret = arg_num_eval(arg->op.left, &left);
2756 			if (!ret)
2757 				break;
2758 			ret = arg_num_eval(arg->op.right, &right);
2759 			if (!ret)
2760 				break;
2761 			if (arg->op.op[1])
2762 				*val = left && right;
2763 			else
2764 				*val = left & right;
2765 			break;
2766 		case '<':
2767 			ret = arg_num_eval(arg->op.left, &left);
2768 			if (!ret)
2769 				break;
2770 			ret = arg_num_eval(arg->op.right, &right);
2771 			if (!ret)
2772 				break;
2773 			switch (arg->op.op[1]) {
2774 			case 0:
2775 				*val = left < right;
2776 				break;
2777 			case '<':
2778 				*val = left << right;
2779 				break;
2780 			case '=':
2781 				*val = left <= right;
2782 				break;
2783 			default:
2784 				do_warning("unknown op '%s'", arg->op.op);
2785 				ret = 0;
2786 			}
2787 			break;
2788 		case '>':
2789 			ret = arg_num_eval(arg->op.left, &left);
2790 			if (!ret)
2791 				break;
2792 			ret = arg_num_eval(arg->op.right, &right);
2793 			if (!ret)
2794 				break;
2795 			switch (arg->op.op[1]) {
2796 			case 0:
2797 				*val = left > right;
2798 				break;
2799 			case '>':
2800 				*val = left >> right;
2801 				break;
2802 			case '=':
2803 				*val = left >= right;
2804 				break;
2805 			default:
2806 				do_warning("unknown op '%s'", arg->op.op);
2807 				ret = 0;
2808 			}
2809 			break;
2810 		case '=':
2811 			ret = arg_num_eval(arg->op.left, &left);
2812 			if (!ret)
2813 				break;
2814 			ret = arg_num_eval(arg->op.right, &right);
2815 			if (!ret)
2816 				break;
2817 
2818 			if (arg->op.op[1] != '=') {
2819 				do_warning("unknown op '%s'", arg->op.op);
2820 				ret = 0;
2821 			} else
2822 				*val = left == right;
2823 			break;
2824 		case '!':
2825 			ret = arg_num_eval(arg->op.left, &left);
2826 			if (!ret)
2827 				break;
2828 			ret = arg_num_eval(arg->op.right, &right);
2829 			if (!ret)
2830 				break;
2831 
2832 			switch (arg->op.op[1]) {
2833 			case '=':
2834 				*val = left != right;
2835 				break;
2836 			default:
2837 				do_warning("unknown op '%s'", arg->op.op);
2838 				ret = 0;
2839 			}
2840 			break;
2841 		case '-':
2842 			/* check for negative */
2843 			if (arg->op.left->type == TEP_PRINT_NULL)
2844 				left = 0;
2845 			else
2846 				ret = arg_num_eval(arg->op.left, &left);
2847 			if (!ret)
2848 				break;
2849 			ret = arg_num_eval(arg->op.right, &right);
2850 			if (!ret)
2851 				break;
2852 			*val = left - right;
2853 			break;
2854 		case '+':
2855 			if (arg->op.left->type == TEP_PRINT_NULL)
2856 				left = 0;
2857 			else
2858 				ret = arg_num_eval(arg->op.left, &left);
2859 			if (!ret)
2860 				break;
2861 			ret = arg_num_eval(arg->op.right, &right);
2862 			if (!ret)
2863 				break;
2864 			*val = left + right;
2865 			break;
2866 		case '~':
2867 			ret = arg_num_eval(arg->op.right, &right);
2868 			if (!ret)
2869 				break;
2870 			*val = ~right;
2871 			break;
2872 		default:
2873 			do_warning("unknown op '%s'", arg->op.op);
2874 			ret = 0;
2875 		}
2876 		break;
2877 
2878 	case TEP_PRINT_NULL:
2879 	case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2880 	case TEP_PRINT_STRING:
2881 	case TEP_PRINT_BSTRING:
2882 	case TEP_PRINT_BITMASK:
2883 	case TEP_PRINT_CPUMASK:
2884 	default:
2885 		do_warning("invalid eval type %d", arg->type);
2886 		ret = 0;
2887 
2888 	}
2889 	return ret;
2890 }
2891 
arg_eval(struct tep_print_arg * arg)2892 static char *arg_eval (struct tep_print_arg *arg)
2893 {
2894 	long long val;
2895 	static char buf[24];
2896 
2897 	switch (arg->type) {
2898 	case TEP_PRINT_ATOM:
2899 		return arg->atom.atom;
2900 	case TEP_PRINT_TYPE:
2901 		return arg_eval(arg->typecast.item);
2902 	case TEP_PRINT_OP:
2903 		if (!arg_num_eval(arg, &val))
2904 			break;
2905 		sprintf(buf, "%lld", val);
2906 		return buf;
2907 
2908 	case TEP_PRINT_NULL:
2909 	case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2910 	case TEP_PRINT_STRING:
2911 	case TEP_PRINT_BSTRING:
2912 	case TEP_PRINT_BITMASK:
2913 	case TEP_PRINT_CPUMASK:
2914 	default:
2915 		do_warning("invalid eval type %d", arg->type);
2916 		break;
2917 	}
2918 
2919 	return NULL;
2920 }
2921 
2922 static enum tep_event_type
process_fields(struct tep_event * event,struct tep_print_flag_sym ** list,char ** tok)2923 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2924 {
2925 	enum tep_event_type type;
2926 	struct tep_print_arg *arg = NULL;
2927 	struct tep_print_flag_sym *field;
2928 	char *token = *tok;
2929 	char *value;
2930 
2931 	do {
2932 		free_token(token);
2933 		type = read_token_item(event->tep, &token);
2934 		if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2935 			break;
2936 
2937 		arg = alloc_arg();
2938 		if (!arg)
2939 			goto out_free;
2940 
2941 		free_token(token);
2942 		type = process_arg(event, arg, &token);
2943 
2944 		if (type == TEP_EVENT_OP)
2945 			type = process_op(event, arg, &token);
2946 
2947 		if (type == TEP_EVENT_ERROR)
2948 			goto out_free;
2949 
2950 		if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2951 			goto out_free;
2952 
2953 		field = calloc(1, sizeof(*field));
2954 		if (!field)
2955 			goto out_free;
2956 
2957 		value = arg_eval(arg);
2958 		if (value == NULL)
2959 			goto out_free_field;
2960 		field->value = strdup(value);
2961 		if (field->value == NULL)
2962 			goto out_free_field;
2963 
2964 		free_arg(arg);
2965 		arg = alloc_arg();
2966 		if (!arg)
2967 			goto out_free_field;
2968 
2969 		free_token(token);
2970 		type = process_arg(event, arg, &token);
2971 		if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2972 			goto out_free_field;
2973 
2974 		value = arg_eval(arg);
2975 		if (value == NULL)
2976 			goto out_free_field;
2977 		field->str = strdup(value);
2978 		if (field->str == NULL)
2979 			goto out_free_field;
2980 		free_arg(arg);
2981 		arg = NULL;
2982 
2983 		*list = field;
2984 		list = &field->next;
2985 
2986 		free_token(token);
2987 		type = read_token_item(event->tep, &token);
2988 	} while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2989 
2990 	*tok = token;
2991 	return type;
2992 
2993 out_free_field:
2994 	free_flag_sym(field);
2995 out_free:
2996 	free_arg(arg);
2997 	free_token(token);
2998 	*tok = NULL;
2999 
3000 	return TEP_EVENT_ERROR;
3001 }
3002 
3003 static enum tep_event_type
process_flags(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3004 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3005 {
3006 	struct tep_print_arg *field;
3007 	enum tep_event_type type;
3008 	char *token = NULL;
3009 
3010 	memset(arg, 0, sizeof(*arg));
3011 	arg->type = TEP_PRINT_FLAGS;
3012 
3013 	field = alloc_arg();
3014 	if (!field) {
3015 		do_warning_event(event, "%s: not enough memory!", __func__);
3016 		goto out_free;
3017 	}
3018 
3019 	type = process_field_arg(event, field, &token);
3020 
3021 	/* Handle operations in the first argument */
3022 	while (type == TEP_EVENT_OP)
3023 		type = process_op(event, field, &token);
3024 
3025 	if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3026 		goto out_free_field;
3027 	free_token(token);
3028 
3029 	arg->flags.field = field;
3030 
3031 	type = read_token_item(event->tep, &token);
3032 	if (event_item_type(type)) {
3033 		arg->flags.delim = token;
3034 		type = read_token_item(event->tep, &token);
3035 	}
3036 
3037 	if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3038 		goto out_free;
3039 
3040 	type = process_fields(event, &arg->flags.flags, &token);
3041 	if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3042 		goto out_free;
3043 
3044 	free_token(token);
3045 	type = read_token_item(event->tep, tok);
3046 	return type;
3047 
3048 out_free_field:
3049 	free_arg(field);
3050 out_free:
3051 	free_token(token);
3052 	*tok = NULL;
3053 	return TEP_EVENT_ERROR;
3054 }
3055 
3056 static enum tep_event_type
process_symbols(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3057 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3058 {
3059 	struct tep_print_arg *field;
3060 	enum tep_event_type type;
3061 	char *token = NULL;
3062 
3063 	memset(arg, 0, sizeof(*arg));
3064 	arg->type = TEP_PRINT_SYMBOL;
3065 
3066 	field = alloc_arg();
3067 	if (!field) {
3068 		do_warning_event(event, "%s: not enough memory!", __func__);
3069 		goto out_free;
3070 	}
3071 
3072 	type = process_field_arg(event, field, &token);
3073 
3074 	if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3075 		goto out_free_field;
3076 
3077 	arg->symbol.field = field;
3078 
3079 	type = process_fields(event, &arg->symbol.symbols, &token);
3080 	if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3081 		goto out_free;
3082 
3083 	free_token(token);
3084 	type = read_token_item(event->tep, tok);
3085 	return type;
3086 
3087 out_free_field:
3088 	free_arg(field);
3089 out_free:
3090 	free_token(token);
3091 	*tok = NULL;
3092 	return TEP_EVENT_ERROR;
3093 }
3094 
3095 static enum tep_event_type
process_hex_common(struct tep_event * event,struct tep_print_arg * arg,char ** tok,enum tep_print_arg_type type)3096 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
3097 		   char **tok, enum tep_print_arg_type type)
3098 {
3099 	memset(arg, 0, sizeof(*arg));
3100 	arg->type = type;
3101 
3102 	if (alloc_and_process_delim(event, ",", &arg->hex.field))
3103 		goto out;
3104 
3105 	if (alloc_and_process_delim(event, ")", &arg->hex.size))
3106 		goto free_field;
3107 
3108 	return read_token_item(event->tep, tok);
3109 
3110 free_field:
3111 	free_arg(arg->hex.field);
3112 	arg->hex.field = NULL;
3113 out:
3114 	*tok = NULL;
3115 	return TEP_EVENT_ERROR;
3116 }
3117 
3118 static enum tep_event_type
process_hex(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3119 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3120 {
3121 	return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
3122 }
3123 
3124 static enum tep_event_type
process_hex_str(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3125 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
3126 		char **tok)
3127 {
3128 	return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
3129 }
3130 
3131 static enum tep_event_type
process_int_array(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3132 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3133 {
3134 	memset(arg, 0, sizeof(*arg));
3135 	arg->type = TEP_PRINT_INT_ARRAY;
3136 
3137 	if (alloc_and_process_delim(event, ",", &arg->int_array.field))
3138 		goto out;
3139 
3140 	if (alloc_and_process_delim(event, ",", &arg->int_array.count))
3141 		goto free_field;
3142 
3143 	if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
3144 		goto free_size;
3145 
3146 	return read_token_item(event->tep, tok);
3147 
3148 free_size:
3149 	free_arg(arg->int_array.count);
3150 	arg->int_array.count = NULL;
3151 free_field:
3152 	free_arg(arg->int_array.field);
3153 	arg->int_array.field = NULL;
3154 out:
3155 	*tok = NULL;
3156 	return TEP_EVENT_ERROR;
3157 }
3158 
3159 static enum tep_event_type
process_dynamic_array(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3160 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3161 {
3162 	struct tep_format_field *field;
3163 	enum tep_event_type type;
3164 	char *token;
3165 
3166 	memset(arg, 0, sizeof(*arg));
3167 	arg->type = TEP_PRINT_DYNAMIC_ARRAY;
3168 
3169 	/*
3170 	 * The item within the parenthesis is another field that holds
3171 	 * the index into where the array starts.
3172 	 */
3173 	type = read_token(event->tep, &token);
3174 	*tok = token;
3175 	if (type != TEP_EVENT_ITEM)
3176 		goto out_free;
3177 
3178 	/* Find the field */
3179 
3180 	field = tep_find_field(event, token);
3181 	if (!field)
3182 		goto out_free;
3183 
3184 	arg->dynarray.field = field;
3185 	arg->dynarray.index = 0;
3186 
3187 	if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3188 		goto out_free;
3189 
3190 	free_token(token);
3191 	type = read_token_item(event->tep, &token);
3192 	*tok = token;
3193 	if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
3194 		return type;
3195 
3196 	free_token(token);
3197 	arg = alloc_arg();
3198 	if (!arg) {
3199 		do_warning_event(event, "%s: not enough memory!", __func__);
3200 		*tok = NULL;
3201 		return TEP_EVENT_ERROR;
3202 	}
3203 
3204 	type = process_arg(event, arg, &token);
3205 	if (type == TEP_EVENT_ERROR)
3206 		goto out_free_arg;
3207 
3208 	if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
3209 		goto out_free_arg;
3210 
3211 	free_token(token);
3212 	type = read_token_item(event->tep, tok);
3213 	return type;
3214 
3215  out_free_arg:
3216 	free_arg(arg);
3217  out_free:
3218 	free_token(token);
3219 	*tok = NULL;
3220 	return TEP_EVENT_ERROR;
3221 }
3222 
3223 static enum tep_event_type
process_dynamic_array_len(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3224 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
3225 			  char **tok)
3226 {
3227 	struct tep_format_field *field;
3228 	enum tep_event_type type;
3229 	char *token;
3230 
3231 	if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3232 		goto out_free;
3233 
3234 	arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
3235 
3236 	/* Find the field */
3237 	field = tep_find_field(event, token);
3238 	if (!field)
3239 		goto out_free;
3240 
3241 	arg->dynarray.field = field;
3242 	arg->dynarray.index = 0;
3243 
3244 	if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3245 		goto out_err;
3246 
3247 	free_token(token);
3248 	type = read_token(event->tep, &token);
3249 	*tok = token;
3250 
3251 	return type;
3252 
3253  out_free:
3254 	free_token(token);
3255  out_err:
3256 	*tok = NULL;
3257 	return TEP_EVENT_ERROR;
3258 }
3259 
3260 static enum tep_event_type
process_paren(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3261 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3262 {
3263 	struct tep_print_arg *item_arg;
3264 	enum tep_event_type type;
3265 	char *token;
3266 
3267 	type = process_arg(event, arg, &token);
3268 
3269 	if (type == TEP_EVENT_ERROR)
3270 		goto out_free;
3271 
3272 	if (type == TEP_EVENT_OP)
3273 		type = process_op(event, arg, &token);
3274 
3275 	if (type == TEP_EVENT_ERROR)
3276 		goto out_free;
3277 
3278 	/*
3279 	 * If REC is surrounded by parenthesis, the process_arg()
3280 	 * will return TEP_EVENT_ITEM with token == ")". In
3281 	 * this case, we need to continue processing the item
3282 	 * and return.
3283 	 */
3284 	if (type == TEP_EVENT_ITEM && strcmp(token, ")") == 0) {
3285 		free_token(token);
3286 		return process_entry(event, arg, tok);
3287 	}
3288 
3289 	if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3290 		goto out_free;
3291 
3292 	free_token(token);
3293 	type = read_token_item(event->tep, &token);
3294 
3295 	/*
3296 	 * If the next token is an item or another open paren, then
3297 	 * this was a typecast.
3298 	 */
3299 	if (event_item_type(type) ||
3300 	    (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
3301 
3302 		/* make this a typecast and contine */
3303 
3304 		/* prevous must be an atom */
3305 		if (arg->type != TEP_PRINT_ATOM) {
3306 			do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
3307 			goto out_free;
3308 		}
3309 
3310 		item_arg = alloc_arg();
3311 		if (!item_arg) {
3312 			do_warning_event(event, "%s: not enough memory!",
3313 					 __func__);
3314 			goto out_free;
3315 		}
3316 
3317 		arg->type = TEP_PRINT_TYPE;
3318 		arg->typecast.type = arg->atom.atom;
3319 		arg->typecast.item = item_arg;
3320 		type = process_arg_token(event, item_arg, &token, type);
3321 
3322 	}
3323 
3324 	*tok = token;
3325 	return type;
3326 
3327  out_free:
3328 	free_token(token);
3329 	*tok = NULL;
3330 	return TEP_EVENT_ERROR;
3331 }
3332 
3333 
3334 static enum tep_event_type
process_str(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3335 process_str(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3336 {
3337 	enum tep_event_type type;
3338 	char *token;
3339 
3340 	if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3341 		goto out_free;
3342 
3343 	arg->type = TEP_PRINT_STRING;
3344 	arg->string.string = token;
3345 	arg->string.offset = -1;
3346 	arg->string.field = NULL;
3347 
3348 	if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3349 		goto out_err;
3350 
3351 	type = read_token(event->tep, &token);
3352 	*tok = token;
3353 
3354 	return type;
3355 
3356  out_free:
3357 	free_token(token);
3358  out_err:
3359 	*tok = NULL;
3360 	return TEP_EVENT_ERROR;
3361 }
3362 
3363 static enum tep_event_type
process_bitmask(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3364 process_bitmask(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3365 {
3366 	enum tep_event_type type;
3367 	char *token;
3368 
3369 	if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3370 		goto out_free;
3371 
3372 	arg->type = TEP_PRINT_BITMASK;
3373 	arg->bitmask.bitmask = token;
3374 	arg->bitmask.offset = -1;
3375 	arg->bitmask.field = NULL;
3376 
3377 	if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3378 		goto out_err;
3379 
3380 	type = read_token(event->tep, &token);
3381 	*tok = token;
3382 
3383 	return type;
3384 
3385  out_free:
3386 	free_token(token);
3387  out_err:
3388 	*tok = NULL;
3389 	return TEP_EVENT_ERROR;
3390 }
3391 
3392 static enum tep_event_type
process_cpumask(struct tep_event * event __maybe_unused,struct tep_print_arg * arg,char ** tok)3393 process_cpumask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
3394 		char **tok)
3395 {
3396 	enum tep_event_type type = process_bitmask(event, arg, tok);
3397 	if (type != TEP_EVENT_ERROR)
3398 		arg->type = TEP_PRINT_CPUMASK;
3399 
3400 	return type;
3401 }
3402 
3403 static struct tep_function_handler *
find_func_handler(struct tep_handle * tep,char * func_name)3404 find_func_handler(struct tep_handle *tep, char *func_name)
3405 {
3406 	struct tep_function_handler *func;
3407 
3408 	if (!tep)
3409 		return NULL;
3410 
3411 	for (func = tep->func_handlers; func; func = func->next) {
3412 		if (strcmp(func->name, func_name) == 0)
3413 			break;
3414 	}
3415 
3416 	return func;
3417 }
3418 
remove_func_handler(struct tep_handle * tep,char * func_name)3419 static void remove_func_handler(struct tep_handle *tep, char *func_name)
3420 {
3421 	struct tep_function_handler *func;
3422 	struct tep_function_handler **next;
3423 
3424 	next = &tep->func_handlers;
3425 	while ((func = *next)) {
3426 		if (strcmp(func->name, func_name) == 0) {
3427 			*next = func->next;
3428 			free_func_handle(func);
3429 			break;
3430 		}
3431 		next = &func->next;
3432 	}
3433 }
3434 
3435 static enum tep_event_type
process_func_handler(struct tep_event * event,struct tep_function_handler * func,struct tep_print_arg * arg,char ** tok)3436 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
3437 		     struct tep_print_arg *arg, char **tok)
3438 {
3439 	struct tep_print_arg **next_arg;
3440 	struct tep_print_arg *farg;
3441 	enum tep_event_type type;
3442 	char *token;
3443 	int i;
3444 
3445 	arg->type = TEP_PRINT_FUNC;
3446 	arg->func.func = func;
3447 
3448 	*tok = NULL;
3449 
3450 	next_arg = &(arg->func.args);
3451 	for (i = 0; i < func->nr_args; i++) {
3452 		farg = alloc_arg();
3453 		if (!farg) {
3454 			do_warning_event(event, "%s: not enough memory!",
3455 					 __func__);
3456 			return TEP_EVENT_ERROR;
3457 		}
3458 
3459 		type = process_arg(event, farg, &token);
3460 		if (i < (func->nr_args - 1)) {
3461 			if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3462 				do_warning_event(event,
3463 					"Error: function '%s()' expects %d arguments but event %s only uses %d",
3464 					func->name, func->nr_args,
3465 					event->name, i + 1);
3466 				goto err;
3467 			}
3468 		} else {
3469 			if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3470 				do_warning_event(event,
3471 					"Error: function '%s()' only expects %d arguments but event %s has more",
3472 					func->name, func->nr_args, event->name);
3473 				goto err;
3474 			}
3475 		}
3476 
3477 		*next_arg = farg;
3478 		next_arg = &(farg->next);
3479 		free_token(token);
3480 	}
3481 
3482 	type = read_token(event->tep, &token);
3483 	*tok = token;
3484 
3485 	return type;
3486 
3487 err:
3488 	free_arg(farg);
3489 	free_token(token);
3490 	return TEP_EVENT_ERROR;
3491 }
3492 
3493 static enum tep_event_type
process_builtin_expect(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3494 process_builtin_expect(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3495 {
3496 	enum tep_event_type type;
3497 	char *token = NULL;
3498 
3499 	/* Handle __builtin_expect( cond, #) */
3500 	type = process_arg(event, arg, &token);
3501 
3502 	if (type != TEP_EVENT_DELIM || token[0] != ',')
3503 		goto out_free;
3504 
3505 	free_token(token);
3506 
3507 	/* We don't care what the second parameter is of the __builtin_expect() */
3508 	if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3509 		goto out_free;
3510 
3511 	if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3512 		goto out_free;
3513 
3514 	free_token(token);
3515 	type = read_token_item(event->tep, tok);
3516 	return type;
3517 
3518 out_free:
3519 	free_token(token);
3520 	*tok = NULL;
3521 	return TEP_EVENT_ERROR;
3522 }
3523 
3524 static enum tep_event_type
process_sizeof(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3525 process_sizeof(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3526 {
3527 	struct tep_format_field *field;
3528 	enum tep_event_type type;
3529 	char *token = NULL;
3530 	bool token_has_paren = false;
3531 	int ret;
3532 
3533 	type = read_token_item(event->tep, &token);
3534 
3535 	arg->type = TEP_PRINT_ATOM;
3536 
3537 	/* We handle some sizeof types */
3538 	if (strcmp(token, "unsigned") == 0) {
3539 		free_token(token);
3540 		type = read_token_item(event->tep, &token);
3541 
3542 		if (type == TEP_EVENT_ERROR)
3543 			goto error;
3544 
3545 		/* If it's not an item (like "long") then do not process more */
3546 		if (type != TEP_EVENT_ITEM)
3547 			token_has_paren = true;
3548 	}
3549 
3550 	if (token_has_paren || strcmp(token, "int") == 0) {
3551 		arg->atom.atom = strdup("4");
3552 
3553 	} else if (strcmp(token, "long") == 0) {
3554 		free_token(token);
3555 		type = read_token_item(event->tep, &token);
3556 
3557 		if (token && strcmp(token, "long") == 0) {
3558 			arg->atom.atom = strdup("8");
3559 		} else {
3560 			switch (event->tep->long_size) {
3561 			case 4:
3562 				arg->atom.atom = strdup("4");
3563 				break;
3564 			case 8:
3565 				arg->atom.atom = strdup("8");
3566 				break;
3567 			default:
3568 				/* long size not defined yet, fail to parse it */
3569 				goto error;
3570 			}
3571 			/* The token is the next token */
3572 			token_has_paren = true;
3573 		}
3574 
3575 	} else if (strcmp(token, "__u64") == 0 || strcmp(token, "u64") == 0 ||
3576 		   strcmp(token, "__s64") == 0 || strcmp(token, "s64") == 0) {
3577 		arg->atom.atom = strdup("8");
3578 
3579 	} else if (strcmp(token, "__u32") == 0 || strcmp(token, "u32") == 0 ||
3580 		   strcmp(token, "__s32") == 0 || strcmp(token, "s32") == 0) {
3581 		arg->atom.atom = strdup("4");
3582 
3583 	} else if (strcmp(token, "__u16") == 0 || strcmp(token, "u16") == 0 ||
3584 		   strcmp(token, "__s16") == 0 || strcmp(token, "s16") == 0) {
3585 		arg->atom.atom = strdup("2");
3586 
3587 	} else if (strcmp(token, "__u8") == 0 || strcmp(token, "u8") == 0 ||
3588 		   strcmp(token, "__8") == 0 || strcmp(token, "s8") == 0) {
3589 		arg->atom.atom = strdup("1");
3590 
3591 	} else if (strcmp(token, "REC") == 0) {
3592 
3593 		free_token(token);
3594 		type = read_token_item(event->tep, &token);
3595 
3596 		if (test_type_token(type, token,  TEP_EVENT_OP, "->"))
3597 			goto error;
3598 		free_token(token);
3599 
3600 		if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3601 			goto error;
3602 
3603 		field = tep_find_any_field(event, token);
3604 		/* Can't handle arrays (yet) */
3605 		if (!field || field->flags & TEP_FIELD_IS_ARRAY)
3606 			goto error;
3607 
3608 		ret = asprintf(&arg->atom.atom, "%d", field->size);
3609 		if (ret < 0)
3610 			goto error;
3611 
3612 	} else {
3613 		goto error;
3614 	}
3615 
3616 	if (!token_has_paren) {
3617 		/* The token contains the last item before the parenthesis */
3618 		free_token(token);
3619 		type = read_token_item(event->tep, &token);
3620 	}
3621 	if (test_type_token(type, token,  TEP_EVENT_DELIM, ")"))
3622 		goto error;
3623 
3624 	free_token(token);
3625 	return read_token_item(event->tep, tok);
3626 error:
3627 	free_token(token);
3628 	*tok = NULL;
3629 	return TEP_EVENT_ERROR;
3630 }
3631 
3632 static enum tep_event_type
process_function(struct tep_event * event,struct tep_print_arg * arg,char * token,char ** tok)3633 process_function(struct tep_event *event, struct tep_print_arg *arg,
3634 		 char *token, char **tok)
3635 {
3636 	struct tep_function_handler *func;
3637 
3638 	if (strcmp(token, "__print_flags") == 0) {
3639 		free_token(token);
3640 		is_flag_field = 1;
3641 		return process_flags(event, arg, tok);
3642 	}
3643 	if (strcmp(token, "__print_symbolic") == 0) {
3644 		free_token(token);
3645 		is_symbolic_field = 1;
3646 		return process_symbols(event, arg, tok);
3647 	}
3648 	if (strcmp(token, "__print_hex") == 0) {
3649 		free_token(token);
3650 		return process_hex(event, arg, tok);
3651 	}
3652 	if (strcmp(token, "__print_hex_str") == 0) {
3653 		free_token(token);
3654 		return process_hex_str(event, arg, tok);
3655 	}
3656 	if (strcmp(token, "__print_array") == 0) {
3657 		free_token(token);
3658 		return process_int_array(event, arg, tok);
3659 	}
3660 	if (strcmp(token, "__get_str") == 0 ||
3661 	    strcmp(token, "__get_rel_str") == 0) {
3662 		free_token(token);
3663 		return process_str(event, arg, tok);
3664 	}
3665 	if (strcmp(token, "__get_bitmask") == 0 ||
3666 	    strcmp(token, "__get_rel_bitmask") == 0) {
3667 		free_token(token);
3668 		return process_bitmask(event, arg, tok);
3669 	}
3670 	if (strcmp(token, "__get_cpumask") == 0 ||
3671 	    strcmp(token, "__get_rel_cpumask") == 0) {
3672 		free_token(token);
3673 		return process_cpumask(event, arg, tok);
3674 	}
3675 	if (strcmp(token, "__get_dynamic_array") == 0 ||
3676 	    strcmp(token, "__get_rel_dynamic_array") == 0 ||
3677 	    strcmp(token, "__get_sockaddr") == 0 ||
3678 	    strcmp(token, "__get_sockaddr_rel") == 0) {
3679 		free_token(token);
3680 		return process_dynamic_array(event, arg, tok);
3681 	}
3682 	if (strcmp(token, "__get_dynamic_array_len") == 0 ||
3683 	    strcmp(token, "__get_rel_dynamic_array_len") == 0) {
3684 		free_token(token);
3685 		return process_dynamic_array_len(event, arg, tok);
3686 	}
3687 	if (strcmp(token, "__builtin_expect") == 0) {
3688 		free_token(token);
3689 		return process_builtin_expect(event, arg, tok);
3690 	}
3691 	if (strcmp(token, "sizeof") == 0) {
3692 		free_token(token);
3693 		return process_sizeof(event, arg, tok);
3694 	}
3695 
3696 	func = find_func_handler(event->tep, token);
3697 	if (func) {
3698 		free_token(token);
3699 		return process_func_handler(event, func, arg, tok);
3700 	}
3701 
3702 	do_warning_event(event, "function %s not defined", token);
3703 	free_token(token);
3704 	return TEP_EVENT_ERROR;
3705 }
3706 
3707 static enum tep_event_type
process_arg_token(struct tep_event * event,struct tep_print_arg * arg,char ** tok,enum tep_event_type type)3708 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3709 		  char **tok, enum tep_event_type type)
3710 {
3711 	char *token;
3712 	char *atom;
3713 
3714 	token = *tok;
3715 
3716 	switch (type) {
3717 	case TEP_EVENT_ITEM:
3718 		if (strcmp(token, "REC") == 0) {
3719 			free_token(token);
3720 			type = process_entry(event, arg, &token);
3721 			break;
3722 		}
3723 		atom = token;
3724 		/* test the next token */
3725 		type = read_token_item(event->tep, &token);
3726 
3727 		/*
3728 		 * If the next token is a parenthesis, then this
3729 		 * is a function.
3730 		 */
3731 		if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3732 			free_token(token);
3733 			token = NULL;
3734 			/* this will free atom. */
3735 			type = process_function(event, arg, atom, &token);
3736 			break;
3737 		}
3738 		/* atoms can be more than one token long */
3739 		while (type == TEP_EVENT_ITEM) {
3740 			int ret;
3741 
3742 			ret = append(&atom, " ", token);
3743 			if (ret < 0) {
3744 				free(atom);
3745 				*tok = NULL;
3746 				free_token(token);
3747 				return TEP_EVENT_ERROR;
3748 			}
3749 			free_token(token);
3750 			type = read_token_item(event->tep, &token);
3751 		}
3752 
3753 		arg->type = TEP_PRINT_ATOM;
3754 		arg->atom.atom = atom;
3755 		break;
3756 
3757 	case TEP_EVENT_SQUOTE:
3758 		arg->type = TEP_PRINT_ATOM;
3759 		/* Make characters into numbers */
3760 		if (asprintf(&arg->atom.atom, "%d", token[0]) < 0) {
3761 			free_token(token);
3762 			*tok = NULL;
3763 			arg->atom.atom = NULL;
3764 			return TEP_EVENT_ERROR;
3765 		}
3766 		free_token(token);
3767 		type = read_token_item(event->tep, &token);
3768 		break;
3769 	case TEP_EVENT_DQUOTE:
3770 		arg->type = TEP_PRINT_ATOM;
3771 		arg->atom.atom = token;
3772 		type = read_token_item(event->tep, &token);
3773 		break;
3774 	case TEP_EVENT_DELIM:
3775 		if (strcmp(token, "(") == 0) {
3776 			free_token(token);
3777 			type = process_paren(event, arg, &token);
3778 			break;
3779 		}
3780 	case TEP_EVENT_OP:
3781 		/* handle single ops */
3782 		arg->type = TEP_PRINT_OP;
3783 		arg->op.op = token;
3784 		arg->op.left = NULL;
3785 		type = process_op(event, arg, &token);
3786 
3787 		/* On error, the op is freed */
3788 		if (type == TEP_EVENT_ERROR)
3789 			arg->op.op = NULL;
3790 
3791 		/* return error type if errored */
3792 		break;
3793 
3794 	case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3795 	default:
3796 		do_warning_event(event, "unexpected type %d", type);
3797 		return TEP_EVENT_ERROR;
3798 	}
3799 	*tok = token;
3800 
3801 	return type;
3802 }
3803 
event_read_print_args(struct tep_event * event,struct tep_print_arg ** list)3804 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3805 {
3806 	enum tep_event_type type = TEP_EVENT_ERROR;
3807 	struct tep_print_arg *arg;
3808 	char *token;
3809 	int args = 0;
3810 
3811 	do {
3812 		if (type == TEP_EVENT_NEWLINE) {
3813 			type = read_token_item(event->tep, &token);
3814 			continue;
3815 		}
3816 
3817 		arg = alloc_arg();
3818 		if (!arg) {
3819 			do_warning_event(event, "%s: not enough memory!",
3820 					 __func__);
3821 			return -1;
3822 		}
3823 
3824 		type = process_arg(event, arg, &token);
3825 
3826 		if (type == TEP_EVENT_ERROR) {
3827 			free_token(token);
3828 			free_arg(arg);
3829 			return -1;
3830 		}
3831 
3832 		*list = arg;
3833 		args++;
3834 
3835 		if (type == TEP_EVENT_OP) {
3836 			type = process_op(event, arg, &token);
3837 			free_token(token);
3838 
3839 			if (consolidate_op_arg(type, arg) < 0)
3840 				type = TEP_EVENT_ERROR;
3841 
3842 			if (type == TEP_EVENT_ERROR) {
3843 				*list = NULL;
3844 				free_arg(arg);
3845 				return -1;
3846 			}
3847 			list = &arg->next;
3848 			continue;
3849 		}
3850 
3851 		if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3852 			free_token(token);
3853 			*list = arg;
3854 			list = &arg->next;
3855 			continue;
3856 		}
3857 		break;
3858 	} while (type != TEP_EVENT_NONE);
3859 
3860 	if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3861 		free_token(token);
3862 
3863 	return args;
3864 }
3865 
event_read_print(struct tep_event * event)3866 static int event_read_print(struct tep_event *event)
3867 {
3868 	enum tep_event_type type;
3869 	char *token;
3870 	int ret;
3871 
3872 	if (read_expected_item(event->tep, TEP_EVENT_ITEM, "print") < 0)
3873 		return -1;
3874 
3875 	if (read_expected(event->tep, TEP_EVENT_ITEM, "fmt") < 0)
3876 		return -1;
3877 
3878 	if (read_expected(event->tep, TEP_EVENT_OP, ":") < 0)
3879 		return -1;
3880 
3881 	if (read_expect_type(event->tep, TEP_EVENT_DQUOTE, &token) < 0)
3882 		goto fail;
3883 
3884  concat:
3885 	event->print_fmt.format = token;
3886 	event->print_fmt.args = NULL;
3887 
3888 	/* ok to have no arg */
3889 	type = read_token_item(event->tep, &token);
3890 
3891 	if (type == TEP_EVENT_NONE)
3892 		return 0;
3893 
3894 	/* Handle concatenation of print lines */
3895 	if (type == TEP_EVENT_DQUOTE) {
3896 		char *cat;
3897 
3898 		if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3899 			goto fail;
3900 		free_token(token);
3901 		free_token(event->print_fmt.format);
3902 		event->print_fmt.format = NULL;
3903 		token = cat;
3904 		goto concat;
3905 	}
3906 
3907 	if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3908 		goto fail;
3909 
3910 	free_token(token);
3911 
3912 	ret = event_read_print_args(event, &event->print_fmt.args);
3913 	if (ret < 0)
3914 		return -1;
3915 
3916 	return ret;
3917 
3918  fail:
3919 	free_token(token);
3920 	return -1;
3921 }
3922 
3923 /**
3924  * tep_find_common_field - return a common field by event
3925  * @event: handle for the event
3926  * @name: the name of the common field to return
3927  *
3928  * Returns a common field from the event by the given @name.
3929  * This only searches the common fields and not all field.
3930  */
3931 struct tep_format_field *
tep_find_common_field(struct tep_event * event,const char * name)3932 tep_find_common_field(struct tep_event *event, const char *name)
3933 {
3934 	struct tep_format_field *format;
3935 
3936 	for (format = event->format.common_fields;
3937 	     format; format = format->next) {
3938 		if (strcmp(format->name, name) == 0)
3939 			break;
3940 	}
3941 
3942 	return format;
3943 }
3944 
3945 /**
3946  * tep_find_field - find a non-common field
3947  * @event: handle for the event
3948  * @name: the name of the non-common field
3949  *
3950  * Returns a non-common field by the given @name.
3951  * This does not search common fields.
3952  */
3953 struct tep_format_field *
tep_find_field(struct tep_event * event,const char * name)3954 tep_find_field(struct tep_event *event, const char *name)
3955 {
3956 	struct tep_format_field *format;
3957 
3958 	for (format = event->format.fields;
3959 	     format; format = format->next) {
3960 		if (strcmp(format->name, name) == 0)
3961 			break;
3962 	}
3963 
3964 	return format;
3965 }
3966 
3967 /**
3968  * tep_find_any_field - find any field by name
3969  * @event: handle for the event
3970  * @name: the name of the field
3971  *
3972  * Returns a field by the given @name.
3973  * This searches the common field names first, then
3974  * the non-common ones if a common one was not found.
3975  */
3976 struct tep_format_field *
tep_find_any_field(struct tep_event * event,const char * name)3977 tep_find_any_field(struct tep_event *event, const char *name)
3978 {
3979 	struct tep_format_field *format;
3980 
3981 	format = tep_find_common_field(event, name);
3982 	if (format)
3983 		return format;
3984 	return tep_find_field(event, name);
3985 }
3986 
3987 /**
3988  * tep_read_number - read a number from data
3989  * @tep: a handle to the trace event parser context
3990  * @ptr: the raw data
3991  * @size: the size of the data that holds the number
3992  *
3993  * Returns the number (converted to host) from the
3994  * raw data.
3995  */
tep_read_number(struct tep_handle * tep,const void * ptr,int size)3996 unsigned long long tep_read_number(struct tep_handle *tep,
3997 				   const void *ptr, int size)
3998 {
3999 	unsigned long long val;
4000 
4001 	switch (size) {
4002 	case 1:
4003 		return *(unsigned char *)ptr;
4004 	case 2:
4005 		return data2host2(tep, *(unsigned short *)ptr);
4006 	case 4:
4007 		return data2host4(tep, *(unsigned int *)ptr);
4008 	case 8:
4009 		memcpy(&val, (ptr), sizeof(unsigned long long));
4010 		return data2host8(tep, val);
4011 	default:
4012 		/* BUG! */
4013 		return 0;
4014 	}
4015 }
4016 
4017 /**
4018  * tep_read_number_field - read a number from data
4019  * @field: a handle to the field
4020  * @data: the raw data to read
4021  * @value: the value to place the number in
4022  *
4023  * Reads raw data according to a field offset and size,
4024  * and translates it into @value.
4025  *
4026  * Returns 0 on success, -1 otherwise.
4027  */
tep_read_number_field(struct tep_format_field * field,const void * data,unsigned long long * value)4028 int tep_read_number_field(struct tep_format_field *field, const void *data,
4029 			  unsigned long long *value)
4030 {
4031 	if (!field)
4032 		return -1;
4033 	switch (field->size) {
4034 	case 1:
4035 	case 2:
4036 	case 4:
4037 	case 8:
4038 		*value = tep_read_number(field->event->tep,
4039 					 data + field->offset, field->size);
4040 		return 0;
4041 	default:
4042 		return -1;
4043 	}
4044 }
4045 
get_common_info(struct tep_handle * tep,const char * type,int * offset,int * size)4046 static int get_common_info(struct tep_handle *tep,
4047 			   const char *type, int *offset, int *size)
4048 {
4049 	struct tep_event *event;
4050 	struct tep_format_field *field;
4051 
4052 	/*
4053 	 * All events should have the same common elements.
4054 	 * Pick any event to find where the type is;
4055 	 */
4056 	if (!tep->events) {
4057 		do_warning("no event_list!");
4058 		return -1;
4059 	}
4060 
4061 	event = tep->events[0];
4062 	field = tep_find_common_field(event, type);
4063 	if (!field)
4064 		return -1;
4065 
4066 	*offset = field->offset;
4067 	*size = field->size;
4068 
4069 	return 0;
4070 }
4071 
__parse_common(struct tep_handle * tep,void * data,int * size,int * offset,const char * name)4072 static int __parse_common(struct tep_handle *tep, void *data,
4073 			  int *size, int *offset, const char *name)
4074 {
4075 	int ret;
4076 
4077 	if (!*size) {
4078 		ret = get_common_info(tep, name, offset, size);
4079 		if (ret < 0)
4080 			return ret;
4081 	}
4082 	return tep_read_number(tep, data + *offset, *size);
4083 }
4084 
trace_parse_common_type(struct tep_handle * tep,void * data)4085 static int trace_parse_common_type(struct tep_handle *tep, void *data)
4086 {
4087 	return __parse_common(tep, data,
4088 			      &tep->type_size, &tep->type_offset,
4089 			      "common_type");
4090 }
4091 
parse_common_pid(struct tep_handle * tep,void * data)4092 static int parse_common_pid(struct tep_handle *tep, void *data)
4093 {
4094 	return __parse_common(tep, data,
4095 			      &tep->pid_size, &tep->pid_offset,
4096 			      "common_pid");
4097 }
4098 
parse_common_pc(struct tep_handle * tep,void * data)4099 static int parse_common_pc(struct tep_handle *tep, void *data)
4100 {
4101 	return __parse_common(tep, data,
4102 			      &tep->pc_size, &tep->pc_offset,
4103 			      "common_preempt_count");
4104 }
4105 
parse_common_flags(struct tep_handle * tep,void * data)4106 static int parse_common_flags(struct tep_handle *tep, void *data)
4107 {
4108 	return __parse_common(tep, data,
4109 			      &tep->flags_size, &tep->flags_offset,
4110 			      "common_flags");
4111 }
4112 
parse_common_lock_depth(struct tep_handle * tep,void * data)4113 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
4114 {
4115 	return __parse_common(tep, data,
4116 			      &tep->ld_size, &tep->ld_offset,
4117 			      "common_lock_depth");
4118 }
4119 
parse_common_migrate_disable(struct tep_handle * tep,void * data)4120 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
4121 {
4122 	return __parse_common(tep, data,
4123 			      &tep->ld_size, &tep->ld_offset,
4124 			      "common_migrate_disable");
4125 }
4126 
4127 static int events_id_cmp(const void *a, const void *b);
4128 
4129 /**
4130  * tep_find_event - find an event by given id
4131  * @tep: a handle to the trace event parser context
4132  * @id: the id of the event
4133  *
4134  * Returns an event that has a given @id.
4135  */
tep_find_event(struct tep_handle * tep,int id)4136 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
4137 {
4138 	struct tep_event **eventptr;
4139 	struct tep_event key;
4140 	struct tep_event *pkey = &key;
4141 
4142 	/* Check cache first */
4143 	if (tep->last_event && tep->last_event->id == id)
4144 		return tep->last_event;
4145 
4146 	key.id = id;
4147 
4148 	eventptr = bsearch(&pkey, tep->events, tep->nr_events,
4149 			   sizeof(*tep->events), events_id_cmp);
4150 
4151 	if (eventptr) {
4152 		tep->last_event = *eventptr;
4153 		return *eventptr;
4154 	}
4155 
4156 	return NULL;
4157 }
4158 
4159 /**
4160  * tep_find_event_by_name - find an event by given name
4161  * @tep: a handle to the trace event parser context
4162  * @sys: the system name to search for
4163  * @name: the name of the event to search for
4164  *
4165  * This returns an event with a given @name and under the system
4166  * @sys. If @sys is NULL the first event with @name is returned.
4167  */
4168 struct tep_event *
tep_find_event_by_name(struct tep_handle * tep,const char * sys,const char * name)4169 tep_find_event_by_name(struct tep_handle *tep,
4170 		       const char *sys, const char *name)
4171 {
4172 	struct tep_event *event = NULL;
4173 	int i;
4174 
4175 	if (tep->last_event &&
4176 	    strcmp(tep->last_event->name, name) == 0 &&
4177 	    (!sys || strcmp(tep->last_event->system, sys) == 0))
4178 		return tep->last_event;
4179 
4180 	for (i = 0; i < tep->nr_events; i++) {
4181 		event = tep->events[i];
4182 		if (strcmp(event->name, name) == 0) {
4183 			if (!sys)
4184 				break;
4185 			if (strcmp(event->system, sys) == 0)
4186 				break;
4187 		}
4188 	}
4189 	if (i == tep->nr_events)
4190 		event = NULL;
4191 
4192 	tep->last_event = event;
4193 	return event;
4194 }
4195 
test_for_symbol(struct tep_handle * tep,struct tep_print_arg * arg)4196 static unsigned long long test_for_symbol(struct tep_handle *tep,
4197 					  struct tep_print_arg *arg)
4198 {
4199 	unsigned long long val = 0;
4200 	struct func_list *item = tep->funclist;
4201 	char *func;
4202 	int i;
4203 
4204 	if (isdigit(arg->atom.atom[0]))
4205 		return 0;
4206 
4207 	/* Linear search but only happens once (see after the loop) */
4208 	for (i = 0; i < (int)tep->func_count; i++) {
4209 		unsigned long long addr;
4210 		const char *name;
4211 
4212 		if (tep->func_map) {
4213 			addr = tep->func_map[i].addr;
4214 			name = tep->func_map[i].func;
4215 		} else if (item) {
4216 			addr = item->addr;
4217 			name = item->func;
4218 			item = item->next;
4219 		} else
4220 			break;
4221 
4222 		if (strcmp(arg->atom.atom, name) == 0) {
4223 			val = addr;
4224 			break;
4225 		}
4226 	}
4227 
4228 	/*
4229 	 * This modifies the arg to hardcode the value
4230 	 * and will not loop again.
4231 	 */
4232 	func = realloc(arg->atom.atom, 32);
4233 	if (func) {
4234 		snprintf(func, 32, "%lld", val);
4235 		arg->atom.atom = func;
4236 	}
4237 	return val;
4238 }
4239 
4240 #define TEP_OFFSET_LEN_MASK		0xffff
4241 #define TEP_LEN_SHIFT			16
4242 
dynamic_offset(struct tep_handle * tep,int size,void * data,int data_size,unsigned int * offset,unsigned int * len)4243 static void dynamic_offset(struct tep_handle *tep, int size, void *data,
4244 			   int data_size, unsigned int *offset, unsigned int *len)
4245 {
4246 	unsigned long long val;
4247 	unsigned int o, l;
4248 
4249 	/*
4250 	 * The total allocated length of the dynamic array is
4251 	 * stored in the top half of the field and the offset
4252 	 * is in the bottom half of the 32 bit field.
4253 	 */
4254 	val = tep_read_number(tep, data, size);
4255 
4256 	/* Check for overflows */
4257 	o = (unsigned int)(val & TEP_OFFSET_LEN_MASK);
4258 
4259 	/* If there's no length, then just make the length the size of the data */
4260 	if (size == 2)
4261 		l = data_size - o;
4262 	else
4263 		l = (unsigned int)((val >> TEP_LEN_SHIFT) & TEP_OFFSET_LEN_MASK);
4264 
4265 	if (offset)
4266 		*offset = o > data_size ? 0 : o;
4267 	if (len)
4268 		*len = o + l > data_size ? 0 : l;
4269 }
4270 
dynamic_offset_field(struct tep_handle * tep,struct tep_format_field * field,void * data,int size,unsigned int * offset,unsigned int * len)4271 static inline void dynamic_offset_field(struct tep_handle *tep,
4272 					struct tep_format_field *field,
4273 					void *data, int size,
4274 					unsigned int *offset,
4275 					unsigned int *len)
4276 {
4277 	/* Test for overflow */
4278 	if (field->offset + field->size > size) {
4279 		if (offset)
4280 			*offset = 0;
4281 		if (len)
4282 			*len = 0;
4283 		return;
4284 	}
4285 	dynamic_offset(tep, field->size, data + field->offset, size, offset, len);
4286 	if (field->flags & TEP_FIELD_IS_RELATIVE)
4287 		*offset += field->offset + field->size;
4288 }
4289 
check_data_offset_size(struct tep_event * event,const char * field_name,int data_size,int field_offset,int field_size)4290 static bool check_data_offset_size(struct tep_event *event, const char *field_name,
4291 				    int data_size, int field_offset, int field_size)
4292 {
4293 	/* Check to make sure the field is within the data */
4294 	if (field_offset + field_size <= data_size)
4295 		return false;
4296 
4297 	tep_warning("Event '%s' field '%s' goes beyond the size of the event (%d > %d)",
4298 		    event->name, field_name, field_offset + field_size, data_size);
4299 	return true;
4300 }
4301 
4302 static unsigned long long
eval_num_arg(void * data,int size,struct tep_event * event,struct tep_print_arg * arg)4303 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
4304 {
4305 	struct tep_handle *tep = event->tep;
4306 	unsigned long long val = 0;
4307 	unsigned long long left, right;
4308 	struct tep_print_arg *typearg = NULL;
4309 	struct tep_print_arg *larg;
4310 	unsigned int offset;
4311 	unsigned int field_size;
4312 
4313 	switch (arg->type) {
4314 	case TEP_PRINT_NULL:
4315 		/* ?? */
4316 		return 0;
4317 	case TEP_PRINT_ATOM:
4318 		val = strtoull(arg->atom.atom, NULL, 0);
4319 		if (!val)
4320 			val = test_for_symbol(tep, arg);
4321 		return val;
4322 	case TEP_PRINT_FIELD:
4323 		if (!arg->field.field) {
4324 			arg->field.field = tep_find_any_field(event, arg->field.name);
4325 			if (!arg->field.field)
4326 				goto out_warning_field;
4327 		}
4328 		if (check_data_offset_size(event, arg->field.name, size,
4329 					   arg->field.field->offset,
4330 					   arg->field.field->size)) {
4331 			val = 0;
4332 			break;
4333 		}
4334 		/* must be a number */
4335 		val = tep_read_number(tep, data + arg->field.field->offset,
4336 				      arg->field.field->size);
4337 		break;
4338 	case TEP_PRINT_FLAGS:
4339 	case TEP_PRINT_SYMBOL:
4340 	case TEP_PRINT_INT_ARRAY:
4341 	case TEP_PRINT_HEX:
4342 	case TEP_PRINT_HEX_STR:
4343 		break;
4344 	case TEP_PRINT_TYPE:
4345 		val = eval_num_arg(data, size, event, arg->typecast.item);
4346 		return eval_type(val, arg, 0);
4347 	case TEP_PRINT_STRING:
4348 	case TEP_PRINT_BSTRING:
4349 	case TEP_PRINT_BITMASK:
4350 	case TEP_PRINT_CPUMASK:
4351 		return 0;
4352 	case TEP_PRINT_FUNC: {
4353 		struct trace_seq s;
4354 		trace_seq_init(&s);
4355 		val = process_defined_func(&s, data, size, event, arg);
4356 		trace_seq_destroy(&s);
4357 		return val;
4358 	}
4359 	case TEP_PRINT_OP:
4360 		if (strcmp(arg->op.op, "[") == 0) {
4361 			/*
4362 			 * Arrays are special, since we don't want
4363 			 * to read the arg as is.
4364 			 */
4365 			right = eval_num_arg(data, size, event, arg->op.right);
4366 
4367 			/* handle typecasts */
4368 			larg = arg->op.left;
4369 			while (larg->type == TEP_PRINT_TYPE) {
4370 				if (!typearg)
4371 					typearg = larg;
4372 				larg = larg->typecast.item;
4373 			}
4374 
4375 			/* Default to long size */
4376 			field_size = tep->long_size;
4377 
4378 			switch (larg->type) {
4379 			case TEP_PRINT_DYNAMIC_ARRAY:
4380 				dynamic_offset_field(tep, larg->dynarray.field, data,
4381 						     size, &offset, NULL);
4382 				offset += right;
4383 				if (larg->dynarray.field->elementsize)
4384 					field_size = larg->dynarray.field->elementsize;
4385 				break;
4386 			case TEP_PRINT_FIELD:
4387 				if (!larg->field.field) {
4388 					larg->field.field =
4389 						tep_find_any_field(event, larg->field.name);
4390 					if (!larg->field.field) {
4391 						arg = larg;
4392 						goto out_warning_field;
4393 					}
4394 				}
4395 				field_size = larg->field.field->elementsize;
4396 				offset = larg->field.field->offset +
4397 					right * larg->field.field->elementsize;
4398 				break;
4399 			default:
4400 				goto default_op; /* oops, all bets off */
4401 			}
4402 			if (check_data_offset_size(event, arg->field.name, size,
4403 						   offset, field_size)) {
4404 				val = 0;
4405 				break;
4406 			}
4407 			val = tep_read_number(tep,
4408 					      data + offset, field_size);
4409 			if (typearg)
4410 				val = eval_type(val, typearg, 1);
4411 			break;
4412 		} else if (strcmp(arg->op.op, "?") == 0) {
4413 			left = eval_num_arg(data, size, event, arg->op.left);
4414 			arg = arg->op.right;
4415 			if (left)
4416 				val = eval_num_arg(data, size, event, arg->op.left);
4417 			else
4418 				val = eval_num_arg(data, size, event, arg->op.right);
4419 			break;
4420 		}
4421  default_op:
4422 		left = eval_num_arg(data, size, event, arg->op.left);
4423 		right = eval_num_arg(data, size, event, arg->op.right);
4424 		switch (arg->op.op[0]) {
4425 		case '!':
4426 			switch (arg->op.op[1]) {
4427 			case 0:
4428 				val = !right;
4429 				break;
4430 			case '=':
4431 				val = left != right;
4432 				break;
4433 			default:
4434 				goto out_warning_op;
4435 			}
4436 			break;
4437 		case '~':
4438 			val = ~right;
4439 			break;
4440 		case '|':
4441 			if (arg->op.op[1])
4442 				val = left || right;
4443 			else
4444 				val = left | right;
4445 			break;
4446 		case '&':
4447 			if (arg->op.op[1])
4448 				val = left && right;
4449 			else
4450 				val = left & right;
4451 			break;
4452 		case '<':
4453 			switch (arg->op.op[1]) {
4454 			case 0:
4455 				val = left < right;
4456 				break;
4457 			case '<':
4458 				val = left << right;
4459 				break;
4460 			case '=':
4461 				val = left <= right;
4462 				break;
4463 			default:
4464 				goto out_warning_op;
4465 			}
4466 			break;
4467 		case '>':
4468 			switch (arg->op.op[1]) {
4469 			case 0:
4470 				val = left > right;
4471 				break;
4472 			case '>':
4473 				val = left >> right;
4474 				break;
4475 			case '=':
4476 				val = left >= right;
4477 				break;
4478 			default:
4479 				goto out_warning_op;
4480 			}
4481 			break;
4482 		case '=':
4483 			if (arg->op.op[1] != '=')
4484 				goto out_warning_op;
4485 
4486 			val = left == right;
4487 			break;
4488 		case '-':
4489 			val = left - right;
4490 			break;
4491 		case '+':
4492 			val = left + right;
4493 			break;
4494 		case '/':
4495 			val = left / right;
4496 			break;
4497 		case '%':
4498 			val = left % right;
4499 			break;
4500 		case '*':
4501 			val = left * right;
4502 			break;
4503 		default:
4504 			goto out_warning_op;
4505 		}
4506 		break;
4507 	case TEP_PRINT_DYNAMIC_ARRAY_LEN:
4508 		dynamic_offset_field(tep, arg->dynarray.field, data, size,
4509 				     NULL, &field_size);
4510 		val = field_size;
4511 		break;
4512 	case TEP_PRINT_DYNAMIC_ARRAY:
4513 		/* Without [], we pass the address to the dynamic data */
4514 		dynamic_offset_field(tep, arg->dynarray.field, data, size,
4515 				     &offset, NULL);
4516 		if (check_data_offset_size(event, arg->field.name, size,
4517 					   offset, 0)) {
4518 			val = (unsigned long)data;
4519 			break;
4520 		}
4521 		val = (unsigned long)data + offset;
4522 		break;
4523 	default: /* not sure what to do there */
4524 		return 0;
4525 	}
4526 	return val;
4527 
4528 out_warning_op:
4529 	do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
4530 	return 0;
4531 
4532 out_warning_field:
4533 	do_warning_event(event, "%s: field %s not found",
4534 			 __func__, arg->field.name);
4535 	return 0;
4536 }
4537 
4538 struct flag {
4539 	const char *name;
4540 	unsigned long long value;
4541 };
4542 
4543 static const struct flag flags[] = {
4544 	{ "HI_SOFTIRQ", 0 },
4545 	{ "TIMER_SOFTIRQ", 1 },
4546 	{ "NET_TX_SOFTIRQ", 2 },
4547 	{ "NET_RX_SOFTIRQ", 3 },
4548 	{ "BLOCK_SOFTIRQ", 4 },
4549 	{ "IRQ_POLL_SOFTIRQ", 5 },
4550 	{ "TASKLET_SOFTIRQ", 6 },
4551 	{ "SCHED_SOFTIRQ", 7 },
4552 	{ "HRTIMER_SOFTIRQ", 8 },
4553 	{ "RCU_SOFTIRQ", 9 },
4554 
4555 	{ "HRTIMER_NORESTART", 0 },
4556 	{ "HRTIMER_RESTART", 1 },
4557 };
4558 
eval_flag(const char * flag)4559 static long long eval_flag(const char *flag)
4560 {
4561 	int i;
4562 
4563 	/*
4564 	 * Some flags in the format files do not get converted.
4565 	 * If the flag is not numeric, see if it is something that
4566 	 * we already know about.
4567 	 */
4568 	if (isdigit(flag[0]))
4569 		return strtoull(flag, NULL, 0);
4570 
4571 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
4572 		if (strcmp(flags[i].name, flag) == 0)
4573 			return flags[i].value;
4574 
4575 	return -1LL;
4576 }
4577 
print_str_to_seq(struct trace_seq * s,const char * format,int len_arg,const char * str)4578 static void print_str_to_seq(struct trace_seq *s, const char *format,
4579 			     int len_arg, const char *str)
4580 {
4581 	if (len_arg >= 0)
4582 		trace_seq_printf(s, format, len_arg, str);
4583 	else
4584 		trace_seq_printf(s, format, str);
4585 }
4586 
print_bitmask_to_seq(struct tep_handle * tep,struct trace_seq * s,const char * format,int len_arg,const void * data,int size)4587 static void print_bitmask_to_seq(struct tep_handle *tep,
4588 				 struct trace_seq *s, const char *format,
4589 				 int len_arg, const void *data, int size)
4590 {
4591 	int nr_bits = size * 8;
4592 	int str_size = (nr_bits + 3) / 4;
4593 	int len = 0;
4594 	char buf[3];
4595 	char *str;
4596 	int index;
4597 	int i;
4598 
4599 	/*
4600 	 * The kernel likes to put in commas every 32 bits, we
4601 	 * can do the same.
4602 	 */
4603 	str_size += (nr_bits - 1) / 32;
4604 
4605 	str = malloc(str_size + 1);
4606 	if (!str) {
4607 		do_warning("%s: not enough memory!", __func__);
4608 		return;
4609 	}
4610 	str[str_size] = 0;
4611 
4612 	/* Start out with -2 for the two chars per byte */
4613 	for (i = str_size - 2; i >= 0; i -= 2) {
4614 		/*
4615 		 * data points to a bit mask of size bytes.
4616 		 * In the kernel, this is an array of long words, thus
4617 		 * endianness is very important.
4618 		 */
4619 		if (tep->file_bigendian)
4620 			index = size - (len + 1);
4621 		else
4622 			index = len;
4623 
4624 		snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
4625 		memcpy(str + i, buf, 2);
4626 		len++;
4627 		if (!(len & 3) && i > 0) {
4628 			i--;
4629 			str[i] = ',';
4630 		}
4631 	}
4632 
4633 	if (len_arg >= 0)
4634 		trace_seq_printf(s, format, len_arg, str);
4635 	else
4636 		trace_seq_printf(s, format, str);
4637 
4638 	free(str);
4639 }
4640 
4641 #define log10(n)               \
4642 (			       \
4643 	n < 10UL ? 0 :	       \
4644 	n < 100UL ? 1 :	       \
4645 	n < 1000UL ? 2 :       \
4646 	n < 10000UL ? 3 :      \
4647 	n < 100000UL ? 4 :     \
4648 	n < 1000000UL ? 5 :    \
4649 	n < 10000000UL ? 6 :   \
4650 	n < 100000000UL ? 7 :  \
4651 	n < 1000000000UL ? 8 : \
4652 	9		       \
4653 )
4654 
4655 /* ilog10(0) should be 1 but the 0 simplifies below math */
4656 #define ilog10(n)    \
4657 (                     \
4658 	n == 0 ? 0UL : \
4659 	n == 1 ? 10UL : \
4660 	n == 2 ? 100UL : \
4661 	n == 3 ? 1000UL : \
4662 	n == 4 ? 10000UL : \
4663 	n == 5 ? 100000UL : \
4664 	n == 6 ? 1000000UL : \
4665 	n == 7 ? 10000000UL : \
4666 	n == 8 ? 100000000UL : \
4667 		 1000000000UL   \
4668 )
4669 
cpumask_worst_size(unsigned int nr_bits)4670 static unsigned int cpumask_worst_size(unsigned int nr_bits)
4671 {
4672 	/*
4673 	 * Printing all the CPUs separated by a comma is a decent bound for the
4674 	 * maximum memory required to print a cpumask (a slightly better bound
4675 	 * is chunks of 2 bits set, i.e. 0-1,3-4,6-7...).
4676 	 *
4677 	 * e.g. for nr_bits=132:
4678 	 * - 131 commas
4679 	 * - 10 * 1 chars for CPUS [0, 9]
4680 	 * - 90 * 2 chars for CPUS [10-99]
4681 	 * - 32 * 3 chars for CPUS [100-131]
4682 	 */
4683 	unsigned int last_cpu = nr_bits - 1;
4684 	unsigned int nr_chars = nr_bits - 1;
4685 	int last_lvl = log10(last_cpu);
4686 
4687 	/* All log10 levels before the last one have all values used */
4688 	for (int lvl = 0; lvl < last_lvl; lvl++) {
4689 		int nr_values = ilog10(lvl + 1) - ilog10(lvl);
4690 
4691 		nr_chars += nr_values * (lvl + 1);
4692 	}
4693 	/* Last level is incomplete */
4694 	nr_chars += (nr_bits - ilog10(last_lvl)) * (last_lvl + 1);
4695 
4696 	return nr_chars;
4697 }
4698 
print_cpumask_to_seq(struct tep_handle * tep,struct trace_seq * s,const char * format,int len_arg,const void * data,int size)4699 static void print_cpumask_to_seq(struct tep_handle *tep,
4700 				 struct trace_seq *s, const char *format,
4701 				 int len_arg, const void *data, int size)
4702 {
4703 	int firstone = -1, firstzero = -1;
4704 	int nr_bits = size * 8;
4705 	bool first = true;
4706 	int str_size = 0;
4707 	char buf[12]; /* '-' + log10(2^32) + 1 digits + '\0' */
4708 	char *str;
4709 	int index;
4710 	int i;
4711 
4712 	str = malloc(cpumask_worst_size(nr_bits) + 1);
4713 	if (!str) {
4714 		do_warning("%s: not enough memory!", __func__);
4715 		return;
4716 	}
4717 
4718 	for (i = 0; i < size; i++) {
4719 		unsigned char byte;
4720 		int fmtsize;
4721 
4722 		if (tep->file_bigendian)
4723 			index = size - (i + 1);
4724 		else
4725 			index = i;
4726 
4727 		/* Byte by byte scan, not the best... */
4728 		byte = *(((unsigned char *)data) + index);
4729 more:
4730 		/* First find a bit set to one...*/
4731 		if (firstone < 0 && byte) {
4732 			/*
4733 			 * Set all lower bits, so a later ffz on this same byte
4734 			 * is guaranteed to find a later bit.
4735 			 */
4736 			firstone = ffs(byte) - 1;
4737 			byte |= (1 << firstone) - 1;
4738 			firstone += i * 8;
4739 		}
4740 
4741 		if (firstone < 0)
4742 			continue;
4743 
4744 		/* ...Then find a bit set to zero */
4745 		if ((~byte) & 0xFF) {
4746 			/*
4747 			 * Clear all lower bits, so a later ffs on this same
4748 			 * byte is guaranteed to find a later bit.
4749 			 */
4750 			firstzero = ffs(~byte) - 1;
4751 			byte &= ~((1 << (firstzero)) - 1);
4752 			firstzero += i * 8;
4753 		} else if (i == size - 1) { /* ...Or reach the end of the mask */
4754 			firstzero = nr_bits;
4755 			byte = 0;
4756 		} else {
4757 			continue;
4758 		}
4759 
4760 		/* We've found a bit set to one, and a later bit set to zero. */
4761 		if (!first) {
4762 			str[str_size] = ',';
4763 			str_size++;
4764 		}
4765 		first = false;
4766 
4767 		/* It takes {log10(number) + 1} chars to format a number */
4768 		fmtsize = log10(firstone) + 1;
4769 		snprintf(buf, fmtsize + 1, "%d", firstone);
4770 		memcpy(str + str_size, buf, fmtsize);
4771 		str_size += fmtsize;
4772 
4773 		if (firstzero > firstone + 1) {
4774 			fmtsize = log10(firstzero - 1) + 2;
4775 			snprintf(buf, fmtsize + 1, "-%d", firstzero - 1);
4776 			memcpy(str + str_size, buf, fmtsize);
4777 			str_size += fmtsize;
4778 		}
4779 
4780 		firstzero = firstone = -1;
4781 		if (byte)
4782 			goto more;
4783 	}
4784 
4785 	str[str_size] = 0;
4786 	str_size++;
4787 
4788 	if (len_arg >= 0)
4789 		trace_seq_printf(s, format, len_arg, str);
4790 	else
4791 		trace_seq_printf(s, format, str);
4792 
4793 	free(str);
4794 }
4795 
print_str_arg(struct trace_seq * s,void * data,int size,struct tep_event * event,const char * format,int len_arg,struct tep_print_arg * arg)4796 static void print_str_arg(struct trace_seq *s, void *data, int size,
4797 			  struct tep_event *event, const char *format,
4798 			  int len_arg, struct tep_print_arg *arg)
4799 {
4800 	struct tep_handle *tep = event->tep;
4801 	struct tep_print_flag_sym *flag;
4802 	struct tep_format_field *field;
4803 	struct printk_map *printk;
4804 	unsigned int offset, len;
4805 	long long val, fval;
4806 	unsigned long long addr;
4807 	char *str;
4808 	unsigned char *hex;
4809 	int print;
4810 	int i;
4811 
4812 	switch (arg->type) {
4813 	case TEP_PRINT_NULL:
4814 		/* ?? */
4815 		return;
4816 	case TEP_PRINT_ATOM:
4817 		print_str_to_seq(s, format, len_arg, arg->atom.atom);
4818 		return;
4819 	case TEP_PRINT_FIELD:
4820 		field = arg->field.field;
4821 		if (!field) {
4822 			field = tep_find_any_field(event, arg->field.name);
4823 			if (!field) {
4824 				str = arg->field.name;
4825 				goto out_warning_field;
4826 			}
4827 			arg->field.field = field;
4828 		}
4829 		/* Zero sized fields, mean the rest of the data */
4830 		len = field->size ? : size - field->offset;
4831 
4832 		/*
4833 		 * Some events pass in pointers. If this is not an array
4834 		 * and the size is the same as long_size, assume that it
4835 		 * is a pointer.
4836 		 */
4837 		if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
4838 		    field->size == tep->long_size) {
4839 
4840 			/* Handle heterogeneous recording and processing
4841 			 * architectures
4842 			 *
4843 			 * CASE I:
4844 			 * Traces recorded on 32-bit devices (32-bit
4845 			 * addressing) and processed on 64-bit devices:
4846 			 * In this case, only 32 bits should be read.
4847 			 *
4848 			 * CASE II:
4849 			 * Traces recorded on 64 bit devices and processed
4850 			 * on 32-bit devices:
4851 			 * In this case, 64 bits must be read.
4852 			 */
4853 			addr = (tep->long_size == 8) ?
4854 				*(unsigned long long *)(data + field->offset) :
4855 				(unsigned long long)*(unsigned int *)(data + field->offset);
4856 
4857 			/* Check if it matches a print format */
4858 			printk = find_printk(tep, addr);
4859 			if (printk)
4860 				trace_seq_puts(s, printk->printk);
4861 			else
4862 				trace_seq_printf(s, "%llx", addr);
4863 			break;
4864 		}
4865 		str = malloc(len + 1);
4866 		if (!str) {
4867 			do_warning_event(event, "%s: not enough memory!",
4868 					 __func__);
4869 			return;
4870 		}
4871 		memcpy(str, data + field->offset, len);
4872 		str[len] = 0;
4873 		print_str_to_seq(s, format, len_arg, str);
4874 		free(str);
4875 		break;
4876 	case TEP_PRINT_FLAGS:
4877 		val = eval_num_arg(data, size, event, arg->flags.field);
4878 		print = 0;
4879 		for (flag = arg->flags.flags; flag; flag = flag->next) {
4880 			fval = eval_flag(flag->value);
4881 			if (!val && fval < 0) {
4882 				print_str_to_seq(s, format, len_arg, flag->str);
4883 				break;
4884 			}
4885 			if (fval > 0 && (val & fval) == fval) {
4886 				if (print && arg->flags.delim)
4887 					trace_seq_puts(s, arg->flags.delim);
4888 				print_str_to_seq(s, format, len_arg, flag->str);
4889 				print = 1;
4890 				val &= ~fval;
4891 			}
4892 		}
4893 		if (val) {
4894 			if (print && arg->flags.delim)
4895 				trace_seq_puts(s, arg->flags.delim);
4896 			trace_seq_printf(s, "0x%llx", val);
4897 		}
4898 		break;
4899 	case TEP_PRINT_SYMBOL:
4900 		val = eval_num_arg(data, size, event, arg->symbol.field);
4901 		for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4902 			fval = eval_flag(flag->value);
4903 			if (val == fval) {
4904 				print_str_to_seq(s, format, len_arg, flag->str);
4905 				break;
4906 			}
4907 		}
4908 		if (!flag)
4909 			trace_seq_printf(s, "0x%llx", val);
4910 		break;
4911 	case TEP_PRINT_HEX:
4912 	case TEP_PRINT_HEX_STR:
4913 		if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4914 			dynamic_offset_field(tep, arg->hex.field->dynarray.field, data,
4915 				             size, &offset, NULL);
4916 			hex = data + offset;
4917 		} else {
4918 			field = arg->hex.field->field.field;
4919 			if (!field) {
4920 				str = arg->hex.field->field.name;
4921 				field = tep_find_any_field(event, str);
4922 				if (!field)
4923 					goto out_warning_field;
4924 				arg->hex.field->field.field = field;
4925 			}
4926 			hex = data + field->offset;
4927 		}
4928 		len = eval_num_arg(data, size, event, arg->hex.size);
4929 		for (i = 0; i < len; i++) {
4930 			if (i && arg->type == TEP_PRINT_HEX)
4931 				trace_seq_putc(s, ' ');
4932 			trace_seq_printf(s, "%02x", hex[i]);
4933 		}
4934 		break;
4935 
4936 	case TEP_PRINT_INT_ARRAY: {
4937 		void *num;
4938 		int el_size;
4939 
4940 		if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4941 			dynamic_offset_field(tep, arg->int_array.field->dynarray.field, data,
4942 					     size, &offset, NULL);
4943 			num = data + offset;
4944 		} else {
4945 			field = arg->int_array.field->field.field;
4946 			if (!field) {
4947 				str = arg->int_array.field->field.name;
4948 				field = tep_find_any_field(event, str);
4949 				if (!field)
4950 					goto out_warning_field;
4951 				arg->int_array.field->field.field = field;
4952 			}
4953 			num = data + field->offset;
4954 		}
4955 		len = eval_num_arg(data, size, event, arg->int_array.count);
4956 		el_size = eval_num_arg(data, size, event,
4957 				       arg->int_array.el_size);
4958 		trace_seq_putc(s, '{');
4959 		for (i = 0; i < len; i++) {
4960 			if (i)
4961 				trace_seq_putc(s, ',');
4962 
4963 			if (el_size == 1) {
4964 				trace_seq_printf(s, "0x%x", *(uint8_t *)num);
4965 			} else if (el_size == 2) {
4966 				trace_seq_printf(s, "0x%x", *(uint16_t *)num);
4967 			} else if (el_size == 4) {
4968 				trace_seq_printf(s, "0x%x", *(uint32_t *)num);
4969 			} else if (el_size == 8) {
4970 				trace_seq_printf(s, "0x%"PRIx64, *(uint64_t *)num);
4971 			} else {
4972 				trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4973 						 el_size, *(uint8_t *)num);
4974 				el_size = 1;
4975 			}
4976 
4977 			num += el_size;
4978 		}
4979 		trace_seq_putc(s, '}');
4980 		break;
4981 	}
4982 	case TEP_PRINT_TYPE:
4983 		break;
4984 	case TEP_PRINT_STRING: {
4985 		if (!arg->string.field) {
4986 			arg->string.field = tep_find_any_field(event, arg->string.string);
4987 			if (!arg->string.field)
4988 				break;
4989 			arg->string.offset = arg->string.field->offset;
4990 		}
4991 		dynamic_offset_field(tep, arg->string.field, data, size, &offset, &len);
4992 		/* Do not attempt to save zero length dynamic strings */
4993 		if (!len)
4994 			break;
4995 		print_str_to_seq(s, format, len_arg, ((char *)data) + offset);
4996 		break;
4997 	}
4998 	case TEP_PRINT_BSTRING:
4999 		print_str_to_seq(s, format, len_arg, arg->string.string);
5000 		break;
5001 	case TEP_PRINT_BITMASK: {
5002 		if (!arg->bitmask.field) {
5003 			arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
5004 			if (!arg->bitmask.field)
5005 				break;
5006 			arg->bitmask.offset = arg->bitmask.field->offset;
5007 		}
5008 		dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
5009 		print_bitmask_to_seq(tep, s, format, len_arg,
5010 				     data + offset, len);
5011 		break;
5012 	}
5013 	case TEP_PRINT_CPUMASK: {
5014 		if (!arg->bitmask.field) {
5015 			arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
5016 			arg->bitmask.offset = arg->bitmask.field->offset;
5017 		}
5018 		if (!arg->bitmask.field)
5019 			break;
5020 		dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
5021 		print_cpumask_to_seq(tep, s, format, len_arg,
5022 				     data + offset, len);
5023 		break;
5024 	}
5025 	case TEP_PRINT_OP:
5026 		/*
5027 		 * The only op for string should be ? :
5028 		 */
5029 		if (arg->op.op[0] != '?')
5030 			return;
5031 		val = eval_num_arg(data, size, event, arg->op.left);
5032 		if (val)
5033 			print_str_arg(s, data, size, event,
5034 				      format, len_arg, arg->op.right->op.left);
5035 		else
5036 			print_str_arg(s, data, size, event,
5037 				      format, len_arg, arg->op.right->op.right);
5038 		break;
5039 	case TEP_PRINT_FUNC:
5040 		process_defined_func(s, data, size, event, arg);
5041 		break;
5042 	default:
5043 		/* well... */
5044 		break;
5045 	}
5046 
5047 	return;
5048 
5049 out_warning_field:
5050 	do_warning_event(event, "%s: field %s not found",
5051 			 __func__, arg->field.name);
5052 }
5053 
5054 static unsigned long long
process_defined_func(struct trace_seq * s,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5055 process_defined_func(struct trace_seq *s, void *data, int size,
5056 		     struct tep_event *event, struct tep_print_arg *arg)
5057 {
5058 	struct tep_function_handler *func_handle = arg->func.func;
5059 	struct func_params *param;
5060 	unsigned long long *args;
5061 	unsigned long long ret;
5062 	struct tep_print_arg *farg;
5063 	struct trace_seq str;
5064 	struct save_str {
5065 		struct save_str *next;
5066 		char *str;
5067 	} *strings = NULL, *string;
5068 	int i;
5069 
5070 	if (!func_handle->nr_args) {
5071 		ret = (*func_handle->func)(s, NULL);
5072 		goto out;
5073 	}
5074 
5075 	farg = arg->func.args;
5076 	param = func_handle->params;
5077 
5078 	ret = ULLONG_MAX;
5079 	args = malloc(sizeof(*args) * func_handle->nr_args);
5080 	if (!args)
5081 		goto out;
5082 
5083 	for (i = 0; i < func_handle->nr_args; i++) {
5084 		switch (param->type) {
5085 		case TEP_FUNC_ARG_INT:
5086 		case TEP_FUNC_ARG_LONG:
5087 		case TEP_FUNC_ARG_PTR:
5088 			args[i] = eval_num_arg(data, size, event, farg);
5089 			break;
5090 		case TEP_FUNC_ARG_STRING:
5091 			trace_seq_init(&str);
5092 			print_str_arg(&str, data, size, event, "%s", -1, farg);
5093 			trace_seq_terminate(&str);
5094 			string = malloc(sizeof(*string));
5095 			if (!string) {
5096 				do_warning_event(event, "%s(%d): malloc str",
5097 						 __func__, __LINE__);
5098 				goto out_free;
5099 			}
5100 			string->next = strings;
5101 			string->str = strdup(str.buffer);
5102 			if (!string->str) {
5103 				free(string);
5104 				do_warning_event(event, "%s(%d): malloc str",
5105 						 __func__, __LINE__);
5106 				goto out_free;
5107 			}
5108 			args[i] = (uintptr_t)string->str;
5109 			strings = string;
5110 			trace_seq_destroy(&str);
5111 			break;
5112 		default:
5113 			/*
5114 			 * Something went totally wrong, this is not
5115 			 * an input error, something in this code broke.
5116 			 */
5117 			do_warning_event(event, "Unexpected end of arguments\n");
5118 			goto out_free;
5119 		}
5120 		farg = farg->next;
5121 		param = param->next;
5122 	}
5123 
5124 	ret = (*func_handle->func)(s, args);
5125 out_free:
5126 	free(args);
5127 	while (strings) {
5128 		string = strings;
5129 		strings = string->next;
5130 		free(string->str);
5131 		free(string);
5132 	}
5133 
5134  out:
5135 	/* TBD : handle return type here */
5136 	return ret;
5137 }
5138 
free_args(struct tep_print_arg * args)5139 static void free_args(struct tep_print_arg *args)
5140 {
5141 	struct tep_print_arg *next;
5142 
5143 	while (args) {
5144 		next = args->next;
5145 
5146 		free_arg(args);
5147 		args = next;
5148 	}
5149 }
5150 
make_bprint_args(char * fmt,void * data,int size,struct tep_event * event)5151 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
5152 {
5153 	struct tep_handle *tep = event->tep;
5154 	struct tep_format_field *field, *ip_field;
5155 	struct tep_print_arg *args, *arg, **next;
5156 	unsigned long long ip, val;
5157 	char *ptr;
5158 	void *bptr;
5159 	int vsize = 0;
5160 
5161 	field = tep->bprint_buf_field;
5162 	ip_field = tep->bprint_ip_field;
5163 
5164 	if (!field) {
5165 		field = tep_find_field(event, "buf");
5166 		if (!field) {
5167 			do_warning_event(event, "can't find buffer field for binary printk");
5168 			return NULL;
5169 		}
5170 		ip_field = tep_find_field(event, "ip");
5171 		if (!ip_field) {
5172 			do_warning_event(event, "can't find ip field for binary printk");
5173 			return NULL;
5174 		}
5175 		tep->bprint_buf_field = field;
5176 		tep->bprint_ip_field = ip_field;
5177 	}
5178 
5179 	ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
5180 
5181 	/*
5182 	 * The first arg is the IP pointer.
5183 	 */
5184 	args = alloc_arg();
5185 	if (!args) {
5186 		do_warning_event(event, "%s(%d): not enough memory!",
5187 				 __func__, __LINE__);
5188 		return NULL;
5189 	}
5190 	arg = args;
5191 	arg->next = NULL;
5192 	next = &arg->next;
5193 
5194 	arg->type = TEP_PRINT_ATOM;
5195 
5196 	if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
5197 		goto out_free;
5198 
5199 	/* skip the first "%ps: " */
5200 	for (ptr = fmt + 5, bptr = data + field->offset;
5201 	     bptr < data + size && *ptr; ptr++) {
5202 		int ls = 0;
5203 
5204 		if (*ptr == '%') {
5205  process_again:
5206 			ptr++;
5207 			switch (*ptr) {
5208 			case '%':
5209 				break;
5210 			case 'l':
5211 				ls++;
5212 				goto process_again;
5213 			case 'L':
5214 				ls = 2;
5215 				goto process_again;
5216 			case '0' ... '9':
5217 			case '.':
5218 			case '#':
5219 			case '+':
5220 				goto process_again;
5221 			case 'z':
5222 			case 'Z':
5223 				ls = 1;
5224 				goto process_again;
5225 			case 'p':
5226 				ls = 1;
5227 				if (isalnum(ptr[1])) {
5228 					ptr++;
5229 					/* Check for special pointers */
5230 					switch (*ptr) {
5231 					case 's':
5232 					case 'S':
5233 					case 'x':
5234 						break;
5235 					case 'f':
5236 					case 'F':
5237 						/*
5238 						 * Pre-5.5 kernels use %pf and
5239 						 * %pF for printing symbols
5240 						 * while kernels since 5.5 use
5241 						 * %pfw for fwnodes. So check
5242 						 * %p[fF] isn't followed by 'w'.
5243 						 */
5244 						if (ptr[1] != 'w')
5245 							break;
5246 						/* fall through */
5247 					default:
5248 						/*
5249 						 * Older kernels do not process
5250 						 * dereferenced pointers.
5251 						 * Only process if the pointer
5252 						 * value is a printable.
5253 						 */
5254 						if (isprint(*(char *)bptr))
5255 							goto process_string;
5256 					}
5257 				}
5258 				/* fall through */
5259 			case 'd':
5260 			case 'u':
5261 			case 'i':
5262 			case 'x':
5263 			case 'X':
5264 			case 'o':
5265 				switch (ls) {
5266 				case 0:
5267 					vsize = 4;
5268 					break;
5269 				case 1:
5270 					vsize = tep->long_size;
5271 					break;
5272 				case 2:
5273 					vsize = 8;
5274 					break;
5275 				default:
5276 					vsize = ls; /* ? */
5277 					break;
5278 				}
5279 			/* fall through */
5280 			case '*':
5281 				if (*ptr == '*')
5282 					vsize = 4;
5283 
5284 				/* the pointers are always 4 bytes aligned */
5285 				bptr = (void *)(((unsigned long)bptr + 3) &
5286 						~3);
5287 				val = tep_read_number(tep, bptr, vsize);
5288 				bptr += vsize;
5289 				arg = alloc_arg();
5290 				if (!arg) {
5291 					do_warning_event(event, "%s(%d): not enough memory!",
5292 						   __func__, __LINE__);
5293 					goto out_free;
5294 				}
5295 				arg->next = NULL;
5296 				arg->type = TEP_PRINT_ATOM;
5297 				if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
5298 					free(arg);
5299 					goto out_free;
5300 				}
5301 				*next = arg;
5302 				next = &arg->next;
5303 				/*
5304 				 * The '*' case means that an arg is used as the length.
5305 				 * We need to continue to figure out for what.
5306 				 */
5307 				if (*ptr == '*')
5308 					goto process_again;
5309 
5310 				break;
5311 			case 's':
5312  process_string:
5313 				arg = alloc_arg();
5314 				if (!arg) {
5315 					do_warning_event(event, "%s(%d): not enough memory!",
5316 						   __func__, __LINE__);
5317 					goto out_free;
5318 				}
5319 				arg->next = NULL;
5320 				arg->type = TEP_PRINT_BSTRING;
5321 				arg->string.string = strdup(bptr);
5322 				if (!arg->string.string) {
5323 					free(arg);
5324 					goto out_free;
5325 				}
5326 				bptr += strlen(bptr) + 1;
5327 				*next = arg;
5328 				next = &arg->next;
5329 			default:
5330 				break;
5331 			}
5332 		}
5333 	}
5334 
5335 	return args;
5336 
5337 out_free:
5338 	free_args(args);
5339 	return NULL;
5340 }
5341 
5342 static char *
get_bprint_format(void * data,int size __maybe_unused,struct tep_event * event)5343 get_bprint_format(void *data, int size __maybe_unused,
5344 		  struct tep_event *event)
5345 {
5346 	struct tep_handle *tep = event->tep;
5347 	unsigned long long addr;
5348 	struct tep_format_field *field;
5349 	struct printk_map *printk;
5350 	char *format;
5351 
5352 	field = tep->bprint_fmt_field;
5353 
5354 	if (!field) {
5355 		field = tep_find_field(event, "fmt");
5356 		if (!field) {
5357 			do_warning_event(event, "can't find format field for binary printk");
5358 			return NULL;
5359 		}
5360 		tep->bprint_fmt_field = field;
5361 	}
5362 
5363 	addr = tep_read_number(tep, data + field->offset, field->size);
5364 
5365 	printk = find_printk(tep, addr);
5366 	if (!printk) {
5367 		if (asprintf(&format, "%%ps: (NO FORMAT FOUND at %llx)\n", addr) < 0)
5368 			return NULL;
5369 		return format;
5370 	}
5371 
5372 	if (asprintf(&format, "%s: %s", "%ps", printk->printk) < 0)
5373 		return NULL;
5374 
5375 	return format;
5376 }
5377 
print_mac_arg(struct trace_seq * s,const char * format,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5378 static int print_mac_arg(struct trace_seq *s, const char *format,
5379 			 void *data, int size, struct tep_event *event,
5380 			 struct tep_print_arg *arg)
5381 {
5382 	const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
5383 	bool reverse = false;
5384 	unsigned char *buf;
5385 	int ret = 0;
5386 
5387 	if (arg->type == TEP_PRINT_FUNC) {
5388 		process_defined_func(s, data, size, event, arg);
5389 		return 0;
5390 	}
5391 
5392 	/* evaluate if the arg has a type cast */
5393 	while (arg->type == TEP_PRINT_TYPE)
5394 		arg = arg->typecast.item;
5395 
5396 	if (arg->type != TEP_PRINT_FIELD) {
5397 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
5398 				 arg->type);
5399 		return 0;
5400 	}
5401 
5402 	if (format[0] == 'm') {
5403 		fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
5404 	} else if (format[0] == 'M' && format[1] == 'F') {
5405 		fmt = "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x";
5406 		ret++;
5407 	}
5408 	if (format[1] == 'R') {
5409 		reverse = true;
5410 		ret++;
5411 	}
5412 
5413 	if (!arg->field.field) {
5414 		arg->field.field =
5415 			tep_find_any_field(event, arg->field.name);
5416 		if (!arg->field.field) {
5417 			do_warning_event(event, "%s: field %s not found",
5418 					 __func__, arg->field.name);
5419 			return ret;
5420 		}
5421 	}
5422 	if (arg->field.field->size != 6) {
5423 		trace_seq_printf(s, "INVALIDMAC");
5424 		return ret;
5425 	}
5426 
5427 	buf = data + arg->field.field->offset;
5428 	if (reverse)
5429 		trace_seq_printf(s, fmt, buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
5430 	else
5431 		trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
5432 
5433 	return ret;
5434 }
5435 
parse_ip4_print_args(struct tep_handle * tep,const char * ptr,bool * reverse)5436 static int parse_ip4_print_args(struct tep_handle *tep,
5437 				const char *ptr, bool *reverse)
5438 {
5439 	int ret = 0;
5440 
5441 	*reverse = false;
5442 
5443 	/* hnbl */
5444 	switch (*ptr) {
5445 	case 'h':
5446 		if (tep->file_bigendian)
5447 			*reverse = false;
5448 		else
5449 			*reverse = true;
5450 		ret++;
5451 		break;
5452 	case 'l':
5453 		*reverse = true;
5454 		ret++;
5455 		break;
5456 	case 'n':
5457 	case 'b':
5458 		ret++;
5459 		/* fall through */
5460 	default:
5461 		*reverse = false;
5462 		break;
5463 	}
5464 
5465 	return ret;
5466 }
5467 
print_ip4_addr(struct trace_seq * s,char i,bool reverse,unsigned char * buf)5468 static void print_ip4_addr(struct trace_seq *s, char i, bool reverse, unsigned char *buf)
5469 {
5470 	const char *fmt;
5471 
5472 	if (i == 'i')
5473 		fmt = "%03d.%03d.%03d.%03d";
5474 	else
5475 		fmt = "%d.%d.%d.%d";
5476 
5477 	if (reverse)
5478 		trace_seq_printf(s, fmt, buf[3], buf[2], buf[1], buf[0]);
5479 	else
5480 		trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
5481 
5482 }
5483 
ipv6_addr_v4mapped(const struct in6_addr * a)5484 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
5485 {
5486 	return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
5487 		(unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
5488 }
5489 
ipv6_addr_is_isatap(const struct in6_addr * addr)5490 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
5491 {
5492 	return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
5493 }
5494 
print_ip6c_addr(struct trace_seq * s,unsigned char * addr)5495 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
5496 {
5497 	int i, j, range;
5498 	unsigned char zerolength[8];
5499 	int longest = 1;
5500 	int colonpos = -1;
5501 	uint16_t word;
5502 	uint8_t hi, lo;
5503 	bool needcolon = false;
5504 	bool useIPv4;
5505 	struct in6_addr in6;
5506 
5507 	memcpy(&in6, addr, sizeof(struct in6_addr));
5508 
5509 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
5510 
5511 	memset(zerolength, 0, sizeof(zerolength));
5512 
5513 	if (useIPv4)
5514 		range = 6;
5515 	else
5516 		range = 8;
5517 
5518 	/* find position of longest 0 run */
5519 	for (i = 0; i < range; i++) {
5520 		for (j = i; j < range; j++) {
5521 			if (in6.s6_addr16[j] != 0)
5522 				break;
5523 			zerolength[i]++;
5524 		}
5525 	}
5526 	for (i = 0; i < range; i++) {
5527 		if (zerolength[i] > longest) {
5528 			longest = zerolength[i];
5529 			colonpos = i;
5530 		}
5531 	}
5532 	if (longest == 1)		/* don't compress a single 0 */
5533 		colonpos = -1;
5534 
5535 	/* emit address */
5536 	for (i = 0; i < range; i++) {
5537 		if (i == colonpos) {
5538 			if (needcolon || i == 0)
5539 				trace_seq_printf(s, ":");
5540 			trace_seq_printf(s, ":");
5541 			needcolon = false;
5542 			i += longest - 1;
5543 			continue;
5544 		}
5545 		if (needcolon) {
5546 			trace_seq_printf(s, ":");
5547 			needcolon = false;
5548 		}
5549 		/* hex u16 without leading 0s */
5550 		word = ntohs(in6.s6_addr16[i]);
5551 		hi = word >> 8;
5552 		lo = word & 0xff;
5553 		if (hi)
5554 			trace_seq_printf(s, "%x%02x", hi, lo);
5555 		else
5556 			trace_seq_printf(s, "%x", lo);
5557 
5558 		needcolon = true;
5559 	}
5560 
5561 	if (useIPv4) {
5562 		if (needcolon)
5563 			trace_seq_printf(s, ":");
5564 		print_ip4_addr(s, 'I', false, &in6.s6_addr[12]);
5565 	}
5566 
5567 	return;
5568 }
5569 
print_ip6_addr(struct trace_seq * s,char i,unsigned char * buf)5570 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
5571 {
5572 	int j;
5573 
5574 	for (j = 0; j < 16; j += 2) {
5575 		trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
5576 		if (i == 'I' && j < 14)
5577 			trace_seq_printf(s, ":");
5578 	}
5579 }
5580 
5581 /*
5582  * %pi4   print an IPv4 address with leading zeros
5583  * %pI4   print an IPv4 address without leading zeros
5584  * %pi6   print an IPv6 address without colons
5585  * %pI6   print an IPv6 address with colons
5586  * %pI6c  print an IPv6 address in compressed form with colons
5587  * %pISpc print an IP address based on sockaddr; p adds port.
5588  */
print_ipv4_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5589 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
5590 			  void *data, int size, struct tep_event *event,
5591 			  struct tep_print_arg *arg)
5592 {
5593 	bool reverse = false;
5594 	unsigned char *buf;
5595 	int ret;
5596 
5597 	ret = parse_ip4_print_args(event->tep, ptr, &reverse);
5598 
5599 	if (arg->type == TEP_PRINT_FUNC) {
5600 		process_defined_func(s, data, size, event, arg);
5601 		return ret;
5602 	}
5603 
5604 	/* evaluate if the arg has a type cast */
5605 	while (arg->type == TEP_PRINT_TYPE)
5606 		arg = arg->typecast.item;
5607 
5608 	if (arg->type != TEP_PRINT_FIELD) {
5609 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5610 		return ret;
5611 	}
5612 
5613 	if (!arg->field.field) {
5614 		arg->field.field =
5615 			tep_find_any_field(event, arg->field.name);
5616 		if (!arg->field.field) {
5617 			do_warning("%s: field %s not found",
5618 				   __func__, arg->field.name);
5619 			return ret;
5620 		}
5621 	}
5622 
5623 	buf = data + arg->field.field->offset;
5624 
5625 	if (arg->field.field->size != 4) {
5626 		trace_seq_printf(s, "INVALIDIPv4");
5627 		return ret;
5628 	}
5629 
5630 	print_ip4_addr(s, i, reverse, buf);
5631 	return ret;
5632 
5633 }
5634 
print_ipv6_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5635 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
5636 			  void *data, int size, struct tep_event *event,
5637 			  struct tep_print_arg *arg)
5638 {
5639 	char have_c = 0;
5640 	unsigned char *buf;
5641 	int rc = 0;
5642 
5643 	/* pI6c */
5644 	if (i == 'I' && *ptr == 'c') {
5645 		have_c = 1;
5646 		ptr++;
5647 		rc++;
5648 	}
5649 
5650 	if (arg->type == TEP_PRINT_FUNC) {
5651 		process_defined_func(s, data, size, event, arg);
5652 		return rc;
5653 	}
5654 
5655 	/* evaluate if the arg has a type cast */
5656 	while (arg->type == TEP_PRINT_TYPE)
5657 		arg = arg->typecast.item;
5658 
5659 	if (arg->type != TEP_PRINT_FIELD) {
5660 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5661 		return rc;
5662 	}
5663 
5664 	if (!arg->field.field) {
5665 		arg->field.field =
5666 			tep_find_any_field(event, arg->field.name);
5667 		if (!arg->field.field) {
5668 			do_warning("%s: field %s not found",
5669 				   __func__, arg->field.name);
5670 			return rc;
5671 		}
5672 	}
5673 
5674 	buf = data + arg->field.field->offset;
5675 
5676 	if (arg->field.field->size != 16) {
5677 		trace_seq_printf(s, "INVALIDIPv6");
5678 		return rc;
5679 	}
5680 
5681 	if (have_c)
5682 		print_ip6c_addr(s, buf);
5683 	else
5684 		print_ip6_addr(s, i, buf);
5685 
5686 	return rc;
5687 }
5688 
print_ipsa_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5689 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
5690 			  void *data, int size, struct tep_event *event,
5691 			  struct tep_print_arg *arg)
5692 {
5693 	char have_c = 0, have_p = 0;
5694 	unsigned char *buf;
5695 	struct sockaddr_storage *sa;
5696 	bool reverse = false;
5697 	unsigned int offset;
5698 	unsigned int len;
5699 	int rc = 0;
5700 	int ret;
5701 
5702 	/* pISpc */
5703 	if (i == 'I') {
5704 		if (*ptr == 'p') {
5705 			have_p = 1;
5706 			ptr++;
5707 			rc++;
5708 		}
5709 		if (*ptr == 'c') {
5710 			have_c = 1;
5711 			ptr++;
5712 			rc++;
5713 		}
5714 	}
5715 	ret = parse_ip4_print_args(event->tep, ptr, &reverse);
5716 	ptr += ret;
5717 	rc += ret;
5718 
5719 	if (arg->type == TEP_PRINT_FUNC) {
5720 		process_defined_func(s, data, size, event, arg);
5721 		return rc;
5722 	}
5723 
5724 	/* evaluate if the arg has a type cast */
5725 	while (arg->type == TEP_PRINT_TYPE)
5726 		arg = arg->typecast.item;
5727 
5728 	if (arg->type == TEP_PRINT_FIELD) {
5729 
5730 		if (!arg->field.field) {
5731 			arg->field.field =
5732 				tep_find_any_field(event, arg->field.name);
5733 			if (!arg->field.field) {
5734 				do_warning("%s: field %s not found",
5735 					   __func__, arg->field.name);
5736 				return rc;
5737 			}
5738 		}
5739 
5740 		offset = arg->field.field->offset;
5741 		len = arg->field.field->size;
5742 
5743 	} else if (arg->type == TEP_PRINT_DYNAMIC_ARRAY) {
5744 
5745 		dynamic_offset_field(event->tep, arg->dynarray.field, data,
5746 				     size, &offset, &len);
5747 
5748 	} else {
5749 		trace_seq_printf(s, "ARG NOT FIELD NOR DYNAMIC ARRAY BUT TYPE %d",
5750 				 arg->type);
5751 		return rc;
5752 	}
5753 
5754 	sa = (struct sockaddr_storage *)(data + offset);
5755 
5756 	if (sa->ss_family == AF_INET) {
5757 		struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
5758 
5759 		if (len < sizeof(struct sockaddr_in)) {
5760 			trace_seq_printf(s, "INVALIDIPv4");
5761 			return rc;
5762 		}
5763 
5764 		print_ip4_addr(s, i, reverse, (unsigned char *) &sa4->sin_addr);
5765 		if (have_p)
5766 			trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
5767 
5768 
5769 	} else if (sa->ss_family == AF_INET6) {
5770 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
5771 
5772 		if (len < sizeof(struct sockaddr_in6)) {
5773 			trace_seq_printf(s, "INVALIDIPv6");
5774 			return rc;
5775 		}
5776 
5777 		if (have_p)
5778 			trace_seq_printf(s, "[");
5779 
5780 		buf = (unsigned char *) &sa6->sin6_addr;
5781 		if (have_c)
5782 			print_ip6c_addr(s, buf);
5783 		else
5784 			print_ip6_addr(s, i, buf);
5785 
5786 		if (have_p)
5787 			trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
5788 	}
5789 
5790 	return rc;
5791 }
5792 
print_ip_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5793 static int print_ip_arg(struct trace_seq *s, const char *ptr,
5794 			void *data, int size, struct tep_event *event,
5795 			struct tep_print_arg *arg)
5796 {
5797 	char i = *ptr;  /* 'i' or 'I' */
5798 	int rc = 1;
5799 
5800 	/* IP version */
5801 	ptr++;
5802 
5803 	switch (*ptr) {
5804 	case '4':
5805 		rc += print_ipv4_arg(s, ptr + 1, i, data, size, event, arg);
5806 		break;
5807 	case '6':
5808 		rc += print_ipv6_arg(s, ptr + 1, i, data, size, event, arg);
5809 		break;
5810 	case 'S':
5811 		rc += print_ipsa_arg(s, ptr + 1, i, data, size, event, arg);
5812 		break;
5813 	default:
5814 		return 0;
5815 	}
5816 
5817 	return rc;
5818 }
5819 
5820 static const int guid_index[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
5821 static const int uuid_index[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
5822 
print_uuid_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5823 static int print_uuid_arg(struct trace_seq *s, const char *ptr,
5824 			void *data, int size, struct tep_event *event,
5825 			struct tep_print_arg *arg)
5826 {
5827 	const int *index = uuid_index;
5828 	char *format = "%02x";
5829 	int ret = 0;
5830 	char *buf;
5831 	int i;
5832 
5833 	switch (*(ptr + 1)) {
5834 	case 'L':
5835 		format = "%02X";
5836 		/* fall through */
5837 	case 'l':
5838 		index = guid_index;
5839 		ret++;
5840 		break;
5841 	case 'B':
5842 		format = "%02X";
5843 		/* fall through */
5844 	case 'b':
5845 		ret++;
5846 		break;
5847 	}
5848 
5849 	if (arg->type == TEP_PRINT_FUNC) {
5850 		process_defined_func(s, data, size, event, arg);
5851 		return ret;
5852 	}
5853 
5854 	/* evaluate if the arg has a type cast */
5855 	while (arg->type == TEP_PRINT_TYPE)
5856 		arg = arg->typecast.item;
5857 
5858 	if (arg->type != TEP_PRINT_FIELD) {
5859 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5860 		return ret;
5861 	}
5862 
5863 	if (!arg->field.field) {
5864 		arg->field.field =
5865 			tep_find_any_field(event, arg->field.name);
5866 		if (!arg->field.field) {
5867 			do_warning("%s: field %s not found",
5868 				   __func__, arg->field.name);
5869 			return ret;
5870 		}
5871 	}
5872 
5873 	if (arg->field.field->size != 16) {
5874 		trace_seq_printf(s, "INVALIDUUID");
5875 		return ret;
5876 	}
5877 
5878 	buf = data + arg->field.field->offset;
5879 
5880 	for (i = 0; i < 16; i++) {
5881 		trace_seq_printf(s, format, buf[index[i]] & 0xff);
5882 		switch (i) {
5883 		case 3:
5884 		case 5:
5885 		case 7:
5886 		case 9:
5887 			trace_seq_printf(s, "-");
5888 			break;
5889 		}
5890 	}
5891 
5892 	return ret;
5893 }
5894 
print_raw_buff_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg,int print_len)5895 static int print_raw_buff_arg(struct trace_seq *s, const char *ptr,
5896 			      void *data, int size, struct tep_event *event,
5897 			      struct tep_print_arg *arg, int print_len)
5898 {
5899 	unsigned int offset, arr_len;
5900 	int plen = print_len;
5901 	char *delim = " ";
5902 	int ret = 0;
5903 	char *buf;
5904 	int i;
5905 
5906 	switch (*(ptr + 1)) {
5907 	case 'C':
5908 		delim = ":";
5909 		ret++;
5910 		break;
5911 	case 'D':
5912 		delim = "-";
5913 		ret++;
5914 		break;
5915 	case 'N':
5916 		delim = "";
5917 		ret++;
5918 		break;
5919 	}
5920 
5921 	if (arg->type == TEP_PRINT_FUNC) {
5922 		process_defined_func(s, data, size, event, arg);
5923 		return ret;
5924 	}
5925 
5926 	if (arg->type != TEP_PRINT_DYNAMIC_ARRAY) {
5927 		trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5928 		return ret;
5929 	}
5930 
5931 	dynamic_offset_field(event->tep, arg->dynarray.field, data, size,
5932 			     &offset, &arr_len);
5933 	buf = data + offset;
5934 
5935 	if (arr_len < plen)
5936 		plen = arr_len;
5937 
5938 	if (plen < 1)
5939 		return ret;
5940 
5941 	trace_seq_printf(s, "%02x", buf[0] & 0xff);
5942 	for (i = 1; i < plen; i++)
5943 		trace_seq_printf(s, "%s%02x", delim, buf[i] & 0xff);
5944 
5945 	return ret;
5946 }
5947 
is_printable_array(char * p,unsigned int len)5948 static int is_printable_array(char *p, unsigned int len)
5949 {
5950 	unsigned int i;
5951 
5952 	for (i = 0; i < len && p[i]; i++)
5953 		if (!isprint(p[i]) && !isspace(p[i]))
5954 		    return 0;
5955 	return 1;
5956 }
5957 
print_field_raw(struct trace_seq * s,void * data,int size,struct tep_format_field * field)5958 static void print_field_raw(struct trace_seq *s, void *data, int size,
5959 			     struct tep_format_field *field)
5960 {
5961 	struct tep_handle *tep = field->event->tep;
5962 	unsigned int offset, len, i;
5963 	unsigned long long val;
5964 
5965 	if (field->flags & TEP_FIELD_IS_ARRAY) {
5966 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
5967 			dynamic_offset_field(tep, field, data, size, &offset, &len);
5968 		} else {
5969 			offset = field->offset;
5970 			len = field->size;
5971 		}
5972 		if (field->flags & TEP_FIELD_IS_STRING &&
5973 		    is_printable_array(data + offset, len)) {
5974 			trace_seq_printf(s, "%s", (char *)data + offset);
5975 		} else {
5976 			trace_seq_puts(s, "ARRAY[");
5977 			for (i = 0; i < len; i++) {
5978 				if (i)
5979 					trace_seq_puts(s, ", ");
5980 				trace_seq_printf(s, "%02x",
5981 						 *((unsigned char *)data + offset + i));
5982 			}
5983 			trace_seq_putc(s, ']');
5984 			field->flags &= ~TEP_FIELD_IS_STRING;
5985 		}
5986 	} else {
5987 		val = tep_read_number(tep, data + field->offset,
5988 				      field->size);
5989 		if (field->flags & TEP_FIELD_IS_POINTER) {
5990 			trace_seq_printf(s, "0x%llx", val);
5991 		} else if (field->flags & TEP_FIELD_IS_SIGNED) {
5992 			switch (field->size) {
5993 			case 4:
5994 				/*
5995 				 * If field is long then print it in hex.
5996 				 * A long usually stores pointers.
5997 				 */
5998 				if (field->flags & TEP_FIELD_IS_LONG)
5999 					trace_seq_printf(s, "0x%x", (int)val);
6000 				else
6001 					trace_seq_printf(s, "%d", (int)val);
6002 				break;
6003 			case 2:
6004 				trace_seq_printf(s, "%2d", (short)val);
6005 				break;
6006 			case 1:
6007 				trace_seq_printf(s, "%1d", (char)val);
6008 				break;
6009 			default:
6010 				trace_seq_printf(s, "%lld", val);
6011 			}
6012 		} else {
6013 			if (field->flags & TEP_FIELD_IS_LONG)
6014 				trace_seq_printf(s, "0x%llx", val);
6015 			else
6016 				trace_seq_printf(s, "%llu", val);
6017 		}
6018 	}
6019 	trace_seq_terminate(s);
6020 }
6021 
6022 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
6023 			    void *data, int size, struct tep_event *event, bool raw);
6024 
print_field(struct trace_seq * s,void * data,int size,struct tep_format_field * field,struct tep_print_parse ** parse_ptr,bool raw)6025 static inline void print_field(struct trace_seq *s, void *data, int size,
6026 				    struct tep_format_field *field,
6027 				    struct tep_print_parse **parse_ptr, bool raw)
6028 {
6029 	struct tep_event *event = field->event;
6030 	struct tep_print_parse *start_parse;
6031 	struct tep_print_parse *parse;
6032 	struct tep_print_arg *arg;
6033 	bool has_0x = false;
6034 
6035 	parse = parse_ptr ? *parse_ptr : event->print_fmt.print_cache;
6036 
6037 	if (!parse || event->flags & TEP_EVENT_FL_FAILED)
6038 		goto out;
6039 
6040 	if (field->flags & (TEP_FIELD_IS_ARRAY | TEP_FIELD_IS_STRING))
6041 		goto out;
6042 
6043 	start_parse = parse;
6044 	do {
6045 		if (parse->type == PRINT_FMT_STRING) {
6046 			int len = strlen(parse->format);
6047 
6048 			if (len > 1 &&
6049 			    strcmp(parse->format + (len -2), "0x") == 0)
6050 				has_0x = true;
6051 			else
6052 				has_0x = false;
6053 
6054 			goto next;
6055 		}
6056 
6057 		arg = parse->arg;
6058 
6059 		while (arg && arg->type == TEP_PRINT_TYPE)
6060 			arg = arg->typecast.item;
6061 
6062 		if (!arg || arg->type != TEP_PRINT_FIELD ||
6063 		    arg->field.field != field) {
6064 			has_0x = false;
6065 			goto next;
6066 		}
6067 
6068 		if (has_0x)
6069 			trace_seq_puts(s, "0x");
6070 
6071 		print_parse_data(parse, s, data, size, event, raw);
6072 
6073 		if (parse_ptr)
6074 			*parse_ptr = parse->next;
6075 
6076 		return;
6077 
6078  next:
6079 		parse = parse->next ? parse->next :
6080 				      event->print_fmt.print_cache;
6081 	} while (parse != start_parse);
6082 
6083  out:
6084 	/* Not found. */
6085 	print_field_raw(s, data, size, field);
6086 }
6087 
6088 /**
6089  * tep_print_field_content - write out the raw content of a field
6090  * @s:    The trace_seq to write the content into
6091  * @data: The payload to extract the field from.
6092  * @size: The size of the payload.
6093  * @field: The field to extract
6094  *
6095  * Use @field to find the field content from within @data and write it
6096  * in human readable format into @s.
6097  *
6098  * It will not write anything on error (s->len will not move)
6099  */
tep_print_field_content(struct trace_seq * s,void * data,int size,struct tep_format_field * field)6100 void tep_print_field_content(struct trace_seq *s, void *data, int size,
6101 			     struct tep_format_field *field)
6102 {
6103 	print_field(s, data, size, field, NULL, false);
6104 }
6105 
6106 /** DEPRECATED **/
tep_print_field(struct trace_seq * s,void * data,struct tep_format_field * field)6107 void tep_print_field(struct trace_seq *s, void *data,
6108 		     struct tep_format_field *field)
6109 {
6110 	/* unsafe to use, should pass in size */
6111 	print_field(s, data, 4096, field, NULL, false);
6112 }
6113 
6114 static inline void
print_selected_fields(struct trace_seq * s,void * data,int size,struct tep_event * event,unsigned long long ignore_mask,bool raw)6115 print_selected_fields(struct trace_seq *s, void *data, int size,
6116 		      struct tep_event *event,
6117 		      unsigned long long ignore_mask, bool raw)
6118 {
6119 	struct tep_print_parse *parse = event->print_fmt.print_cache;
6120 	struct tep_format_field *field;
6121 	unsigned long long field_mask = 1;
6122 
6123 	field = event->format.fields;
6124 	for(; field; field = field->next, field_mask <<= 1) {
6125 		if (field_mask & ignore_mask)
6126 			continue;
6127 
6128 		trace_seq_printf(s, " %s=", field->name);
6129 		print_field(s, data, size, field, &parse, raw);
6130 	}
6131 }
6132 
tep_print_fields(struct trace_seq * s,void * data,int size,struct tep_event * event)6133 void tep_print_fields(struct trace_seq *s, void *data,
6134 		      int size, struct tep_event *event)
6135 {
6136 	print_selected_fields(s, data, size, event, 0, false);
6137 }
6138 
6139 /**
6140  * tep_record_print_fields - print the field name followed by the
6141  * record's field value.
6142  * @s: The seq to print to
6143  * @record: The record to get the event from
6144  * @event: The event that the field is for
6145  */
tep_record_print_fields(struct trace_seq * s,struct tep_record * record,struct tep_event * event)6146 void tep_record_print_fields(struct trace_seq *s,
6147 			     struct tep_record *record,
6148 			     struct tep_event *event)
6149 {
6150 	print_selected_fields(s, record->data, record->size, event, 0, false);
6151 }
6152 
6153 /**
6154  * tep_record_print_selected_fields - print the field name followed by the
6155  * record's field value for a selected subset of record fields.
6156  * @s: The seq to print to
6157  * @record: The record to get the event from
6158  * @event: The event that the field is for
6159  * @select_mask: Bit mask defining the fields to print
6160  */
tep_record_print_selected_fields(struct trace_seq * s,struct tep_record * record,struct tep_event * event,unsigned long long select_mask)6161 void tep_record_print_selected_fields(struct trace_seq *s,
6162 				      struct tep_record *record,
6163 				      struct tep_event *event,
6164 				      unsigned long long select_mask)
6165 {
6166 	unsigned long long ignore_mask = ~select_mask;
6167 
6168 	print_selected_fields(s, record->data, record->size, event, ignore_mask, false);
6169 }
6170 
print_function(struct trace_seq * s,const char * format,void * data,int size,struct tep_event * event,struct tep_print_arg * arg,bool raw)6171 static int print_function(struct trace_seq *s, const char *format,
6172 			  void *data, int size, struct tep_event *event,
6173 			  struct tep_print_arg *arg, bool raw)
6174 {
6175 	struct func_map *func;
6176 	unsigned long long val;
6177 
6178 	val = eval_num_arg(data, size, event, arg);
6179 	func = find_func(event->tep, val);
6180 	if (func) {
6181 		trace_seq_puts(s, func->func);
6182 		if (*format == 'F' || *format == 'S')
6183 			trace_seq_printf(s, "+0x%llx", val - func->addr);
6184 	}
6185 
6186 	if (!func || raw) {
6187 		if (raw)
6188 			trace_seq_puts(s, " (");
6189 		if (event->tep->long_size == 4)
6190 			trace_seq_printf(s, "0x%lx", (long)val);
6191 		else
6192 			trace_seq_printf(s, "0x%llx", (long long)val);
6193 		if (raw)
6194 			trace_seq_puts(s, ")");
6195 	}
6196 
6197 	return 0;
6198 }
6199 
print_arg_pointer(struct trace_seq * s,const char * format,int plen,void * data,int size,struct tep_event * event,struct tep_print_arg * arg,bool raw)6200 static int print_arg_pointer(struct trace_seq *s, const char *format, int plen,
6201 			     void *data, int size,
6202 			     struct tep_event *event, struct tep_print_arg *arg,
6203 			     bool raw)
6204 {
6205 	unsigned long long val;
6206 	int ret = 1;
6207 
6208 	if (arg->type == TEP_PRINT_BSTRING) {
6209 		trace_seq_puts(s, arg->string.string);
6210 		return 0;
6211 	}
6212 	while (*format) {
6213 		if (*format == 'p') {
6214 			format++;
6215 			break;
6216 		}
6217 		format++;
6218 	}
6219 
6220 	switch (*format) {
6221 	case 'F':
6222 	case 'f':
6223 	case 'S':
6224 	case 's':
6225 		ret += print_function(s, format, data, size, event, arg, raw);
6226 		break;
6227 	case 'M':
6228 	case 'm':
6229 		ret += print_mac_arg(s, format, data, size, event, arg);
6230 		break;
6231 	case 'I':
6232 	case 'i':
6233 		ret += print_ip_arg(s, format, data, size, event, arg);
6234 		break;
6235 	case 'U':
6236 		ret += print_uuid_arg(s, format, data, size, event, arg);
6237 		break;
6238 	case 'h':
6239 		ret += print_raw_buff_arg(s, format, data, size, event, arg, plen);
6240 		break;
6241 	default:
6242 		ret = 0;
6243 		val = eval_num_arg(data, size, event, arg);
6244 		trace_seq_printf(s, "%p", (void *)(intptr_t)val);
6245 		break;
6246 	}
6247 
6248 	return ret;
6249 
6250 }
6251 
print_arg_number(struct trace_seq * s,const char * format,int plen,void * data,int size,int ls,struct tep_event * event,struct tep_print_arg * arg)6252 static int print_arg_number(struct trace_seq *s, const char *format, int plen,
6253 			    void *data, int size, int ls,
6254 			    struct tep_event *event, struct tep_print_arg *arg)
6255 {
6256 	unsigned long long val;
6257 
6258 	val = eval_num_arg(data, size, event, arg);
6259 
6260 	switch (ls) {
6261 	case -2:
6262 		if (plen >= 0)
6263 			trace_seq_printf(s, format, plen, (char)val);
6264 		else
6265 			trace_seq_printf(s, format, (char)val);
6266 		break;
6267 	case -1:
6268 		if (plen >= 0)
6269 			trace_seq_printf(s, format, plen, (short)val);
6270 		else
6271 			trace_seq_printf(s, format, (short)val);
6272 		break;
6273 	case 0:
6274 		if (plen >= 0)
6275 			trace_seq_printf(s, format, plen, (int)val);
6276 		else
6277 			trace_seq_printf(s, format, (int)val);
6278 		break;
6279 	case 1:
6280 		if (plen >= 0)
6281 			trace_seq_printf(s, format, plen, (long)val);
6282 		else
6283 			trace_seq_printf(s, format, (long)val);
6284 		break;
6285 	case 2:
6286 		if (plen >= 0)
6287 			trace_seq_printf(s, format, plen, (long long)val);
6288 		else
6289 			trace_seq_printf(s, format, (long long)val);
6290 		break;
6291 	default:
6292 		do_warning_event(event, "bad count (%d)", ls);
6293 		event->flags |= TEP_EVENT_FL_FAILED;
6294 	}
6295 	return 0;
6296 }
6297 
6298 
print_arg_string(struct trace_seq * s,const char * format,int plen,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)6299 static void print_arg_string(struct trace_seq *s, const char *format, int plen,
6300 			     void *data, int size,
6301 			     struct tep_event *event, struct tep_print_arg *arg)
6302 {
6303 	struct trace_seq p;
6304 
6305 	/* Use helper trace_seq */
6306 	trace_seq_init(&p);
6307 	print_str_arg(&p, data, size, event,
6308 		      format, plen, arg);
6309 	trace_seq_terminate(&p);
6310 	trace_seq_puts(s, p.buffer);
6311 	trace_seq_destroy(&p);
6312 }
6313 
parse_arg_format_pointer(const char * format)6314 static int parse_arg_format_pointer(const char *format)
6315 {
6316 	int ret = 0;
6317 	int index;
6318 	int loop;
6319 
6320 	switch (*format) {
6321 	case 'F':
6322 	case 'S':
6323 	case 'f':
6324 	case 's':
6325 		ret++;
6326 		break;
6327 	case 'M':
6328 	case 'm':
6329 		/* [mM]R , [mM]F */
6330 		switch (format[1]) {
6331 		case 'R':
6332 		case 'F':
6333 			ret++;
6334 			break;
6335 		}
6336 		ret++;
6337 		break;
6338 	case 'I':
6339 	case 'i':
6340 		index = 2;
6341 		loop = 1;
6342 		switch (format[1]) {
6343 		case 'S':
6344 			/*[S][pfs]*/
6345 			while (loop) {
6346 				switch (format[index]) {
6347 				case 'p':
6348 				case 'f':
6349 				case 's':
6350 					ret++;
6351 					index++;
6352 					break;
6353 				default:
6354 					loop = 0;
6355 					break;
6356 				}
6357 			}
6358 			/* fall through */
6359 		case '4':
6360 			/* [4S][hnbl] */
6361 			switch (format[index]) {
6362 			case 'h':
6363 			case 'n':
6364 			case 'l':
6365 			case 'b':
6366 				ret++;
6367 				index++;
6368 				break;
6369 			}
6370 			if (format[1] == '4') {
6371 				ret++;
6372 				break;
6373 			}
6374 			/* fall through */
6375 		case '6':
6376 			/* [6S]c */
6377 			if (format[index] == 'c')
6378 				ret++;
6379 			ret++;
6380 			break;
6381 		}
6382 		ret++;
6383 		break;
6384 	case 'U':
6385 		switch (format[1]) {
6386 		case 'L':
6387 		case 'l':
6388 		case 'B':
6389 		case 'b':
6390 			ret++;
6391 			break;
6392 		}
6393 		ret++;
6394 		break;
6395 	case 'h':
6396 		switch (format[1]) {
6397 		case 'C':
6398 		case 'D':
6399 		case 'N':
6400 			ret++;
6401 			break;
6402 		}
6403 		ret++;
6404 		break;
6405 	default:
6406 		break;
6407 	}
6408 
6409 	return ret;
6410 }
6411 
free_parse_args(struct tep_print_parse * arg)6412 static void free_parse_args(struct tep_print_parse *arg)
6413 {
6414 	struct tep_print_parse *del;
6415 
6416 	while (arg) {
6417 		del = arg;
6418 		arg = del->next;
6419 		free(del->format);
6420 		free(del);
6421 	}
6422 }
6423 
parse_arg_add(struct tep_print_parse ** parse,char * format,enum tep_print_parse_type type,struct tep_print_arg * arg,struct tep_print_arg * len_as_arg,int ls)6424 static int parse_arg_add(struct tep_print_parse **parse, char *format,
6425 			 enum tep_print_parse_type type,
6426 			 struct tep_print_arg *arg,
6427 			 struct tep_print_arg *len_as_arg,
6428 			 int ls)
6429 {
6430 	struct tep_print_parse *parg = NULL;
6431 
6432 	parg = calloc(1, sizeof(*parg));
6433 	if (!parg)
6434 		goto error;
6435 	parg->format = strdup(format);
6436 	if (!parg->format)
6437 		goto error;
6438 	parg->type = type;
6439 	parg->arg = arg;
6440 	parg->len_as_arg = len_as_arg;
6441 	parg->ls = ls;
6442 	*parse = parg;
6443 	return 0;
6444 error:
6445 	if (parg) {
6446 		free(parg->format);
6447 		free(parg);
6448 	}
6449 	return -1;
6450 }
6451 
parse_arg_format(struct tep_print_parse ** parse,struct tep_event * event,const char * format,struct tep_print_arg ** arg)6452 static int parse_arg_format(struct tep_print_parse **parse,
6453 			    struct tep_event *event,
6454 			    const char *format, struct tep_print_arg **arg)
6455 {
6456 	struct tep_print_arg *len_arg = NULL;
6457 	char print_format[32];
6458 	const char *start = format;
6459 	int ret = 0;
6460 	int ls = 0;
6461 	int res;
6462 	int len;
6463 
6464 	format++;
6465 	ret++;
6466 	for (; *format; format++) {
6467 		switch (*format) {
6468 		case '#':
6469 			/* FIXME: need to handle properly */
6470 			break;
6471 		case 'h':
6472 			ls--;
6473 			break;
6474 		case 'l':
6475 			ls++;
6476 			break;
6477 		case 'L':
6478 			ls = 2;
6479 			break;
6480 		case 'z':
6481 		case 'Z':
6482 			ls = 1;
6483 			break;
6484 		case '.':
6485 		case '0' ... '9':
6486 		case '-':
6487 		case '+':
6488 			break;
6489 		case '*':
6490 			/* The argument is the length. */
6491 			if (!*arg) {
6492 				do_warning_event(event, "no argument match");
6493 				event->flags |= TEP_EVENT_FL_FAILED;
6494 				goto out_failed;
6495 			}
6496 			if (len_arg) {
6497 				do_warning_event(event, "argument already matched");
6498 				event->flags |= TEP_EVENT_FL_FAILED;
6499 				goto out_failed;
6500 			}
6501 			len_arg = *arg;
6502 			*arg = (*arg)->next;
6503 			break;
6504 		case 'p':
6505 			if (!*arg) {
6506 				do_warning_event(event, "no argument match");
6507 				event->flags |= TEP_EVENT_FL_FAILED;
6508 				goto out_failed;
6509 			}
6510 			res = parse_arg_format_pointer(format + 1);
6511 			if (res > 0) {
6512 				format += res;
6513 				ret += res;
6514 			}
6515 			len = ((unsigned long)format + 1) -
6516 				(unsigned long)start;
6517 			/* should never happen */
6518 			if (len > 31) {
6519 				do_warning_event(event, "bad format!");
6520 				event->flags |= TEP_EVENT_FL_FAILED;
6521 				len = 31;
6522 			}
6523 			memcpy(print_format, start, len);
6524 			print_format[len] = 0;
6525 
6526 			parse_arg_add(parse, print_format,
6527 				      PRINT_FMT_ARG_POINTER, *arg, len_arg, ls);
6528 			*arg = (*arg)->next;
6529 			ret++;
6530 			return ret;
6531 		case 'c':
6532 		case 'd':
6533 		case 'u':
6534 		case 'i':
6535 		case 'x':
6536 		case 'X':
6537 		case 'o':
6538 			if (!*arg) {
6539 				do_warning_event(event, "no argument match");
6540 				event->flags |= TEP_EVENT_FL_FAILED;
6541 				goto out_failed;
6542 			}
6543 
6544 			len = ((unsigned long)format + 1) -
6545 				(unsigned long)start;
6546 
6547 			/* should never happen */
6548 			if (len > 30) {
6549 				do_warning_event(event, "bad format!");
6550 				event->flags |= TEP_EVENT_FL_FAILED;
6551 				len = 31;
6552 			}
6553 			memcpy(print_format, start, len);
6554 			print_format[len] = 0;
6555 
6556 			if (event->tep->long_size == 8 && ls == 1 &&
6557 			    sizeof(long) != 8) {
6558 				char *p;
6559 
6560 				/* make %l into %ll */
6561 				if (ls == 1 && (p = strchr(print_format, 'l')))
6562 					memmove(p+1, p, strlen(p)+1);
6563 				ls = 2;
6564 			}
6565 			if (ls < -2 || ls > 2) {
6566 				do_warning_event(event, "bad count (%d)", ls);
6567 				event->flags |= TEP_EVENT_FL_FAILED;
6568 			}
6569 			parse_arg_add(parse, print_format,
6570 				      PRINT_FMT_ARG_DIGIT, *arg, len_arg, ls);
6571 			*arg = (*arg)->next;
6572 			ret++;
6573 			return ret;
6574 		case 's':
6575 			if (!*arg) {
6576 				do_warning_event(event, "no matching argument");
6577 				event->flags |= TEP_EVENT_FL_FAILED;
6578 				goto out_failed;
6579 			}
6580 
6581 			len = ((unsigned long)format + 1) -
6582 				(unsigned long)start;
6583 
6584 			/* should never happen */
6585 			if (len > 31) {
6586 				do_warning_event(event, "bad format!");
6587 				event->flags |= TEP_EVENT_FL_FAILED;
6588 				len = 31;
6589 			}
6590 
6591 			memcpy(print_format, start, len);
6592 			print_format[len] = 0;
6593 
6594 			parse_arg_add(parse, print_format,
6595 					PRINT_FMT_ARG_STRING, *arg, len_arg, 0);
6596 			*arg = (*arg)->next;
6597 			ret++;
6598 			return ret;
6599 		default:
6600 			snprintf(print_format, 32, ">%c<", *format);
6601 			parse_arg_add(parse, print_format,
6602 					PRINT_FMT_STRING, NULL, NULL, 0);
6603 			ret++;
6604 			return ret;
6605 		}
6606 		ret++;
6607 	}
6608 
6609 out_failed:
6610 	return ret;
6611 
6612 }
6613 
parse_arg_string(struct tep_print_parse ** parse,const char * format)6614 static int parse_arg_string(struct tep_print_parse **parse, const char *format)
6615 {
6616 	struct trace_seq s;
6617 	int ret = 0;
6618 
6619 	trace_seq_init(&s);
6620 	for (; *format; format++) {
6621 		if (*format == '\\') {
6622 			format++;
6623 			ret++;
6624 			switch (*format) {
6625 			case 'n':
6626 				trace_seq_putc(&s, '\n');
6627 				break;
6628 			case 't':
6629 				trace_seq_putc(&s, '\t');
6630 				break;
6631 			case 'r':
6632 				trace_seq_putc(&s, '\r');
6633 				break;
6634 			case '\\':
6635 				trace_seq_putc(&s, '\\');
6636 				break;
6637 			default:
6638 				trace_seq_putc(&s, *format);
6639 				break;
6640 			}
6641 		} else if (*format == '%') {
6642 			if (*(format + 1) == '%') {
6643 				trace_seq_putc(&s, '%');
6644 				format++;
6645 				ret++;
6646 			} else
6647 				break;
6648 		} else
6649 			trace_seq_putc(&s, *format);
6650 
6651 		ret++;
6652 	}
6653 	trace_seq_terminate(&s);
6654 	parse_arg_add(parse, s.buffer, PRINT_FMT_STRING, NULL, NULL, 0);
6655 	trace_seq_destroy(&s);
6656 
6657 	return ret;
6658 }
6659 
6660 static struct tep_print_parse *
parse_args(struct tep_event * event,const char * format,struct tep_print_arg * arg)6661 parse_args(struct tep_event *event, const char *format, struct tep_print_arg *arg)
6662 {
6663 	struct tep_print_parse *parse_ret = NULL;
6664 	struct tep_print_parse **parse = NULL;
6665 	int ret;
6666 	int len;
6667 
6668 	len = strlen(format);
6669 	while (*format) {
6670 		if (!parse_ret)
6671 			parse = &parse_ret;
6672 		if (*format == '%' && *(format + 1) != '%')
6673 			ret = parse_arg_format(parse, event, format, &arg);
6674 		else
6675 			ret = parse_arg_string(parse, format);
6676 		if (*parse)
6677 			parse = &((*parse)->next);
6678 
6679 		len -= ret;
6680 		if (len > 0)
6681 			format += ret;
6682 		else
6683 			break;
6684 	}
6685 	return parse_ret;
6686 }
6687 
print_parse_data(struct tep_print_parse * parse,struct trace_seq * s,void * data,int size,struct tep_event * event,bool raw)6688 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
6689 			    void *data, int size, struct tep_event *event, bool raw)
6690 {
6691 	int len_arg;
6692 
6693 	if (parse->len_as_arg)
6694 		len_arg = eval_num_arg(data, size, event, parse->len_as_arg);
6695 
6696 	switch (parse->type) {
6697 	case PRINT_FMT_ARG_DIGIT:
6698 		print_arg_number(s, parse->format,
6699 				 parse->len_as_arg ? len_arg : -1, data,
6700 				 size, parse->ls, event, parse->arg);
6701 		break;
6702 	case PRINT_FMT_ARG_POINTER:
6703 		print_arg_pointer(s, parse->format,
6704 				  parse->len_as_arg ? len_arg : 1,
6705 				  data, size, event, parse->arg, raw);
6706 		break;
6707 	case PRINT_FMT_ARG_STRING:
6708 		print_arg_string(s, parse->format,
6709 				 parse->len_as_arg ? len_arg : -1,
6710 				 data, size, event, parse->arg);
6711 		break;
6712 	case PRINT_FMT_STRING:
6713 	default:
6714 		trace_seq_printf(s, "%s", parse->format);
6715 		/* Return 1 on non field. */
6716 		return 1;
6717 	}
6718 	/* Return 0 on field being processed. */
6719 	return 0;
6720 }
6721 
print_event_cache(struct tep_print_parse * parse,struct trace_seq * s,void * data,int size,struct tep_event * event)6722 static void print_event_cache(struct tep_print_parse *parse, struct trace_seq *s,
6723 			      void *data, int size, struct tep_event *event)
6724 {
6725 	while (parse) {
6726 		print_parse_data(parse, s, data, size, event, false);
6727 		parse = parse->next;
6728 	}
6729 }
6730 
pretty_print(struct trace_seq * s,void * data,int size,struct tep_event * event)6731 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
6732 {
6733 	struct tep_print_parse *parse = event->print_fmt.print_cache;
6734 	struct tep_print_arg *args = NULL;
6735 	char *bprint_fmt = NULL;
6736 
6737 	if (event->flags & TEP_EVENT_FL_FAILED) {
6738 		trace_seq_printf(s, "[FAILED TO PARSE]");
6739 		tep_print_fields(s, data, size, event);
6740 		return;
6741 	}
6742 
6743 	if (event->flags & TEP_EVENT_FL_ISBPRINT) {
6744 		bprint_fmt = get_bprint_format(data, size, event);
6745 		args = make_bprint_args(bprint_fmt, data, size, event);
6746 		parse = parse_args(event, bprint_fmt, args);
6747 	}
6748 
6749 	print_event_cache(parse, s, data, size, event);
6750 
6751 	if (event->flags & TEP_EVENT_FL_ISBPRINT) {
6752 		free_parse_args(parse);
6753 		free_args(args);
6754 		free(bprint_fmt);
6755 	}
6756 }
6757 
6758 /*
6759  * This parses out the Latency format (interrupts disabled,
6760  * need rescheduling, in hard/soft interrupt, preempt count
6761  * and lock depth) and places it into the trace_seq.
6762  */
data_latency_format(struct tep_handle * tep,struct trace_seq * s,char * format,struct tep_record * record)6763 static void data_latency_format(struct tep_handle *tep, struct trace_seq *s,
6764 				char *format, struct tep_record *record)
6765 {
6766 	static int check_lock_depth = 1;
6767 	static int check_migrate_disable = 1;
6768 	static int lock_depth_exists;
6769 	static int migrate_disable_exists;
6770 	unsigned int lat_flags;
6771 	struct trace_seq sq;
6772 	unsigned int pc;
6773 	int lock_depth = 0;
6774 	int migrate_disable = 0;
6775 	int hardirq;
6776 	int softirq;
6777 	void *data = record->data;
6778 
6779 	trace_seq_init(&sq);
6780 	lat_flags = parse_common_flags(tep, data);
6781 	pc = parse_common_pc(tep, data);
6782 	/* lock_depth may not always exist */
6783 	if (lock_depth_exists)
6784 		lock_depth = parse_common_lock_depth(tep, data);
6785 	else if (check_lock_depth) {
6786 		lock_depth = parse_common_lock_depth(tep, data);
6787 		if (lock_depth < 0)
6788 			check_lock_depth = 0;
6789 		else
6790 			lock_depth_exists = 1;
6791 	}
6792 
6793 	/* migrate_disable may not always exist */
6794 	if (migrate_disable_exists)
6795 		migrate_disable = parse_common_migrate_disable(tep, data);
6796 	else if (check_migrate_disable) {
6797 		migrate_disable = parse_common_migrate_disable(tep, data);
6798 		if (migrate_disable < 0)
6799 			check_migrate_disable = 0;
6800 		else
6801 			migrate_disable_exists = 1;
6802 	}
6803 
6804 	hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
6805 	softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
6806 
6807 	trace_seq_printf(&sq, "%c%c%c",
6808 	       (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
6809 	       (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
6810 	       'X' : '.',
6811 	       (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
6812 	       'N' : '.',
6813 	       (hardirq && softirq) ? 'H' :
6814 	       hardirq ? 'h' : softirq ? 's' : '.');
6815 
6816 	if (pc & 0xf)
6817 		trace_seq_printf(&sq, "%x", pc & 0xf);
6818 	else
6819 		trace_seq_printf(&sq, ".");
6820 
6821 	if (pc & 0xf0)
6822 		trace_seq_printf(&sq, "%x", pc >> 4);
6823 	else
6824 		trace_seq_printf(&sq, ".");
6825 
6826 	if (migrate_disable_exists) {
6827 		if (migrate_disable < 0)
6828 			trace_seq_printf(&sq, ".");
6829 		else
6830 			trace_seq_printf(&sq, "%d", migrate_disable);
6831 	}
6832 
6833 	if (lock_depth_exists) {
6834 		if (lock_depth < 0)
6835 			trace_seq_printf(&sq, ".");
6836 		else
6837 			trace_seq_printf(&sq, "%d", lock_depth);
6838 	}
6839 
6840 	if (sq.state == TRACE_SEQ__MEM_ALLOC_FAILED) {
6841 		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
6842 		return;
6843 	}
6844 
6845 	trace_seq_terminate(&sq);
6846 	trace_seq_puts(s, sq.buffer);
6847 	trace_seq_destroy(&sq);
6848 	trace_seq_terminate(s);
6849 }
6850 
6851 /**
6852  * tep_data_type - parse out the given event type
6853  * @tep: a handle to the trace event parser context
6854  * @rec: the record to read from
6855  *
6856  * This returns the event id from the @rec.
6857  */
tep_data_type(struct tep_handle * tep,struct tep_record * rec)6858 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
6859 {
6860 	return trace_parse_common_type(tep, rec->data);
6861 }
6862 
6863 /**
6864  * tep_data_pid - parse the PID from record
6865  * @tep: a handle to the trace event parser context
6866  * @rec: the record to parse
6867  *
6868  * This returns the PID from a record.
6869  */
tep_data_pid(struct tep_handle * tep,struct tep_record * rec)6870 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
6871 {
6872 	return parse_common_pid(tep, rec->data);
6873 }
6874 
6875 /**
6876  * tep_data_preempt_count - parse the preempt count from the record
6877  * @tep: a handle to the trace event parser context
6878  * @rec: the record to parse
6879  *
6880  * This returns the preempt count from a record.
6881  */
tep_data_preempt_count(struct tep_handle * tep,struct tep_record * rec)6882 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
6883 {
6884 	return parse_common_pc(tep, rec->data);
6885 }
6886 
6887 /**
6888  * tep_data_flags - parse the latency flags from the record
6889  * @tep: a handle to the trace event parser context
6890  * @rec: the record to parse
6891  *
6892  * This returns the latency flags from a record.
6893  *
6894  *  Use trace_flag_type enum for the flags (see event-parse.h).
6895  */
tep_data_flags(struct tep_handle * tep,struct tep_record * rec)6896 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
6897 {
6898 	return parse_common_flags(tep, rec->data);
6899 }
6900 
6901 /**
6902  * tep_data_comm_from_pid - return the command line from PID
6903  * @tep: a handle to the trace event parser context
6904  * @pid: the PID of the task to search for
6905  *
6906  * This returns a pointer to the command line that has the given
6907  * @pid.
6908  */
tep_data_comm_from_pid(struct tep_handle * tep,int pid)6909 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
6910 {
6911 	const char *comm;
6912 
6913 	comm = find_cmdline(tep, pid);
6914 	return comm;
6915 }
6916 
6917 /**
6918  * tep_record_is_event - return true if the given record is the given event
6919  * @record: The record to see is the @event
6920  * @event: The event to test against @record
6921  *
6922  * Returns true if the record is of the given event, false otherwise
6923  */
tep_record_is_event(struct tep_record * record,struct tep_event * event)6924 bool tep_record_is_event(struct tep_record *record, struct tep_event *event)
6925 {
6926 	int type;
6927 
6928 	type = tep_data_type(event->tep, record);
6929 	return event->id == type;
6930 }
6931 
6932 static struct tep_cmdline *
pid_from_cmdlist(struct tep_handle * tep,const char * comm,struct tep_cmdline * next)6933 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
6934 {
6935 	struct cmdline_list *cmdlist = (struct cmdline_list *)next;
6936 
6937 	if (cmdlist)
6938 		cmdlist = cmdlist->next;
6939 	else
6940 		cmdlist = tep->cmdlist;
6941 
6942 	while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
6943 		cmdlist = cmdlist->next;
6944 
6945 	return (struct tep_cmdline *)cmdlist;
6946 }
6947 
6948 /**
6949  * tep_data_pid_from_comm - return the pid from a given comm
6950  * @tep: a handle to the trace event parser context
6951  * @comm: the cmdline to find the pid from
6952  * @next: the cmdline structure to find the next comm
6953  *
6954  * This returns the cmdline structure that holds a pid for a given
6955  * comm, or NULL if none found. As there may be more than one pid for
6956  * a given comm, the result of this call can be passed back into
6957  * a recurring call in the @next parameter, and then it will find the
6958  * next pid.
6959  * Also, it does a linear search, so it may be slow.
6960  */
tep_data_pid_from_comm(struct tep_handle * tep,const char * comm,struct tep_cmdline * next)6961 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
6962 					   struct tep_cmdline *next)
6963 {
6964 	struct tep_cmdline *cmdline;
6965 
6966 	/*
6967 	 * If the cmdlines have not been converted yet, then use
6968 	 * the list.
6969 	 */
6970 	if (!tep->cmdlines)
6971 		return pid_from_cmdlist(tep, comm, next);
6972 
6973 	if (next) {
6974 		/*
6975 		 * The next pointer could have been still from
6976 		 * a previous call before cmdlines were created
6977 		 */
6978 		if (next < tep->cmdlines ||
6979 		    next >= tep->cmdlines + tep->cmdline_count)
6980 			next = NULL;
6981 		else
6982 			cmdline  = next++;
6983 	}
6984 
6985 	if (!next)
6986 		cmdline = tep->cmdlines;
6987 
6988 	while (cmdline < tep->cmdlines + tep->cmdline_count) {
6989 		if (strcmp(cmdline->comm, comm) == 0)
6990 			return cmdline;
6991 		cmdline++;
6992 	}
6993 	return NULL;
6994 }
6995 
6996 /**
6997  * tep_cmdline_pid - return the pid associated to a given cmdline
6998  * @tep: a handle to the trace event parser context
6999  * @cmdline: The cmdline structure to get the pid from
7000  *
7001  * Returns the pid for a give cmdline. If @cmdline is NULL, then
7002  * -1 is returned.
7003  */
tep_cmdline_pid(struct tep_handle * tep,struct tep_cmdline * cmdline)7004 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
7005 {
7006 	struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
7007 
7008 	if (!cmdline)
7009 		return -1;
7010 
7011 	/*
7012 	 * If cmdlines have not been created yet, or cmdline is
7013 	 * not part of the array, then treat it as a cmdlist instead.
7014 	 */
7015 	if (!tep->cmdlines ||
7016 	    cmdline < tep->cmdlines ||
7017 	    cmdline >= tep->cmdlines + tep->cmdline_count)
7018 		return cmdlist->pid;
7019 
7020 	return cmdline->pid;
7021 }
7022 
7023 /*
7024  * This parses the raw @data using the given @event information and
7025  * writes the print format into the trace_seq.
7026  */
print_event_info(struct trace_seq * s,char * format,bool raw,struct tep_event * event,struct tep_record * record)7027 static void print_event_info(struct trace_seq *s, char *format, bool raw,
7028 			     struct tep_event *event, struct tep_record *record)
7029 {
7030 	int print_pretty = 1;
7031 
7032 	if (raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
7033 		print_selected_fields(s, record->data, record->size, event, 0, true);
7034 	else {
7035 
7036 		if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
7037 			print_pretty = event->handler(s, record, event,
7038 						      event->context);
7039 
7040 		if (print_pretty)
7041 			pretty_print(s, record->data, record->size, event);
7042 	}
7043 
7044 	trace_seq_terminate(s);
7045 }
7046 
7047 /**
7048  * tep_find_event_by_record - return the event from a given record
7049  * @tep: a handle to the trace event parser context
7050  * @record: The record to get the event from
7051  *
7052  * Returns the associated event for a given record, or NULL if non is
7053  * is found.
7054  */
7055 struct tep_event *
tep_find_event_by_record(struct tep_handle * tep,struct tep_record * record)7056 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
7057 {
7058 	int type;
7059 
7060 	if (record->size < 0) {
7061 		do_warning("ug! negative record size %d", record->size);
7062 		return NULL;
7063 	}
7064 
7065 	type = trace_parse_common_type(tep, record->data);
7066 
7067 	return tep_find_event(tep, type);
7068 }
7069 
7070 /*
7071  * Writes the timestamp of the record into @s. Time divisor and precision can be
7072  * specified as part of printf @format string. Example:
7073  *	"%3.1000d" - divide the time by 1000 and print the first 3 digits
7074  *	before the dot. Thus, the timestamp "123456000" will be printed as
7075  *	"123.456"
7076  */
print_event_time(struct tep_handle * tep,struct trace_seq * s,char * format,struct tep_event * event,struct tep_record * record)7077 static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
7078 				 char *format, struct tep_event *event,
7079 				 struct tep_record *record)
7080 {
7081 	unsigned long long time;
7082 	char *divstr;
7083 	int prec = 0, pr;
7084 	int div = 0;
7085 	int p10 = 1;
7086 
7087 	if (isdigit(*(format + 1)))
7088 		prec = atoi(format + 1);
7089 	divstr = strchr(format, '.');
7090 	if (divstr && isdigit(*(divstr + 1)))
7091 		div = atoi(divstr + 1);
7092 	time = record->ts;
7093 	if (div) {
7094 		time += div / 2;
7095 		time /= div;
7096 	}
7097 	pr = prec;
7098 	while (pr--)
7099 		p10 *= 10;
7100 
7101 	if (p10 > 1)
7102 		trace_seq_printf(s, "%5llu.%0*llu", time / p10, prec, time % p10);
7103 	else
7104 		trace_seq_printf(s, "%12llu", time);
7105 }
7106 
7107 struct print_event_type {
7108 	enum {
7109 		EVENT_TYPE_INT = 1,
7110 		EVENT_TYPE_STRING,
7111 		EVENT_TYPE_UNKNOWN,
7112 	} type;
7113 	char format[32];
7114 };
7115 
print_string(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,struct tep_event * event,const char * arg,struct print_event_type * type)7116 static void print_string(struct tep_handle *tep, struct trace_seq *s,
7117 			 struct tep_record *record, struct tep_event *event,
7118 			 const char *arg, struct print_event_type *type)
7119 {
7120 	const char *comm;
7121 	int pid;
7122 
7123 	if (strncmp(arg, TEP_PRINT_LATENCY, strlen(TEP_PRINT_LATENCY)) == 0) {
7124 		data_latency_format(tep, s, type->format, record);
7125 	} else if (strncmp(arg, TEP_PRINT_COMM, strlen(TEP_PRINT_COMM)) == 0) {
7126 		pid = parse_common_pid(tep, record->data);
7127 		comm = find_cmdline(tep, pid);
7128 		trace_seq_printf(s, type->format, comm);
7129 	} else if (strncmp(arg, TEP_PRINT_INFO_RAW, strlen(TEP_PRINT_INFO_RAW)) == 0) {
7130 		print_event_info(s, type->format, true, event, record);
7131 	} else if (strncmp(arg, TEP_PRINT_INFO, strlen(TEP_PRINT_INFO)) == 0) {
7132 		print_event_info(s, type->format, false, event, record);
7133 	} else if  (strncmp(arg, TEP_PRINT_NAME, strlen(TEP_PRINT_NAME)) == 0) {
7134 		trace_seq_printf(s, type->format, event->name);
7135 	} else {
7136 		trace_seq_printf(s, "[UNKNOWN TEP TYPE %s]", arg);
7137 	}
7138 
7139 }
7140 
print_int(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,struct tep_event * event,int arg,struct print_event_type * type)7141 static void print_int(struct tep_handle *tep, struct trace_seq *s,
7142 		      struct tep_record *record, struct tep_event *event,
7143 		      int arg, struct print_event_type *type)
7144 {
7145 	int param;
7146 
7147 	switch (arg) {
7148 	case TEP_PRINT_CPU:
7149 		param = record->cpu;
7150 		break;
7151 	case TEP_PRINT_PID:
7152 		param = parse_common_pid(tep, record->data);
7153 		break;
7154 	case TEP_PRINT_TIME:
7155 		return print_event_time(tep, s, type->format, event, record);
7156 	default:
7157 		return;
7158 	}
7159 	trace_seq_printf(s, type->format, param);
7160 }
7161 
tep_print_event_param_type(char * format,struct print_event_type * type)7162 static int tep_print_event_param_type(char *format,
7163 				      struct print_event_type *type)
7164 {
7165 	char *str = format + 1;
7166 	int i = 1;
7167 
7168 	type->type = EVENT_TYPE_UNKNOWN;
7169 	while (*str) {
7170 		switch (*str) {
7171 		case 'd':
7172 		case 'u':
7173 		case 'i':
7174 		case 'x':
7175 		case 'X':
7176 		case 'o':
7177 			type->type = EVENT_TYPE_INT;
7178 			break;
7179 		case 's':
7180 			type->type = EVENT_TYPE_STRING;
7181 			break;
7182 		}
7183 		str++;
7184 		i++;
7185 		if (type->type != EVENT_TYPE_UNKNOWN)
7186 			break;
7187 	}
7188 	memset(type->format, 0, 32);
7189 	memcpy(type->format, format, i < 32 ? i : 31);
7190 	return i;
7191 }
7192 
7193 /**
7194  * tep_print_event - Write various event information
7195  * @tep: a handle to the trace event parser context
7196  * @s: the trace_seq to write to
7197  * @record: The record to get the event from
7198  * @format: a printf format string. Supported event fileds:
7199  *	TEP_PRINT_PID, "%d" - event PID
7200  *	TEP_PRINT_CPU, "%d" - event CPU
7201  *	TEP_PRINT_COMM, "%s" - event command string
7202  *	TEP_PRINT_NAME, "%s" - event name
7203  *	TEP_PRINT_LATENCY, "%s" - event latency
7204  *	TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
7205  *			can be specified as part of this format string:
7206  *			"%precision.divisord". Example:
7207  *			"%3.1000d" - divide the time by 1000 and print the first
7208  *			3 digits before the dot. Thus, the time stamp
7209  *			"123456000" will be printed as "123.456"
7210  *	TEP_PRINT_INFO, "%s" - event information. If any width is specified in
7211  *			the format string, the event information will be printed
7212  *			in raw format.
7213  * Writes the specified event information into @s.
7214  */
tep_print_event(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,const char * fmt,...)7215 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
7216 		     struct tep_record *record, const char *fmt, ...)
7217 {
7218 	struct print_event_type type;
7219 	struct tep_event *event;
7220 	char *current;
7221 	char *format;
7222 	char *str;
7223 	int offset;
7224 	va_list args;
7225 
7226 	event = tep_find_event_by_record(tep, record);
7227 	if (!event) {
7228 		trace_seq_printf(s, "[UNKNOWN EVENT]");
7229 		return;
7230 	}
7231 
7232 	str = current = format = strdup(fmt);
7233 	if (!format)
7234 		return;
7235 
7236 	va_start(args, fmt);
7237 	while (*current) {
7238 		current = strchr(str, '%');
7239 		if (!current) {
7240 			trace_seq_puts(s, str);
7241 			break;
7242 		}
7243 		memset(&type, 0, sizeof(type));
7244 		offset = tep_print_event_param_type(current, &type);
7245 		*current = '\0';
7246 		trace_seq_puts(s, str);
7247 		current += offset;
7248 		switch (type.type) {
7249 		case EVENT_TYPE_STRING:
7250 			print_string(tep, s, record, event,
7251 				     va_arg(args, char*), &type);
7252 			break;
7253 		case EVENT_TYPE_INT:
7254 			print_int(tep, s, record, event,
7255 				  va_arg(args, int), &type);
7256 			break;
7257 		case EVENT_TYPE_UNKNOWN:
7258 		default:
7259 			trace_seq_printf(s, "[UNKNOWN TYPE]");
7260 			break;
7261 		}
7262 		str = current;
7263 
7264 	}
7265 	va_end(args);
7266 	free(format);
7267 }
7268 
events_id_cmp(const void * a,const void * b)7269 static int events_id_cmp(const void *a, const void *b)
7270 {
7271 	struct tep_event * const * ea = a;
7272 	struct tep_event * const * eb = b;
7273 
7274 	if ((*ea)->id < (*eb)->id)
7275 		return -1;
7276 
7277 	if ((*ea)->id > (*eb)->id)
7278 		return 1;
7279 
7280 	return 0;
7281 }
7282 
events_name_cmp(const void * a,const void * b)7283 static int events_name_cmp(const void *a, const void *b)
7284 {
7285 	struct tep_event * const * ea = a;
7286 	struct tep_event * const * eb = b;
7287 	int res;
7288 
7289 	res = strcmp((*ea)->name, (*eb)->name);
7290 	if (res)
7291 		return res;
7292 
7293 	res = strcmp((*ea)->system, (*eb)->system);
7294 	if (res)
7295 		return res;
7296 
7297 	return events_id_cmp(a, b);
7298 }
7299 
events_system_cmp(const void * a,const void * b)7300 static int events_system_cmp(const void *a, const void *b)
7301 {
7302 	struct tep_event * const * ea = a;
7303 	struct tep_event * const * eb = b;
7304 	int res;
7305 
7306 	res = strcmp((*ea)->system, (*eb)->system);
7307 	if (res)
7308 		return res;
7309 
7310 	res = strcmp((*ea)->name, (*eb)->name);
7311 	if (res)
7312 		return res;
7313 
7314 	return events_id_cmp(a, b);
7315 }
7316 
list_events_copy(struct tep_handle * tep)7317 static struct tep_event **list_events_copy(struct tep_handle *tep)
7318 {
7319 	struct tep_event **events;
7320 
7321 	if (!tep)
7322 		return NULL;
7323 
7324 	events = malloc(sizeof(*events) * (tep->nr_events + 1));
7325 	if (!events)
7326 		return NULL;
7327 
7328 	memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
7329 	events[tep->nr_events] = NULL;
7330 	return events;
7331 }
7332 
list_events_sort(struct tep_event ** events,int nr_events,enum tep_event_sort_type sort_type)7333 static void list_events_sort(struct tep_event **events, int nr_events,
7334 			     enum tep_event_sort_type sort_type)
7335 {
7336 	int (*sort)(const void *a, const void *b);
7337 
7338 	switch (sort_type) {
7339 	case TEP_EVENT_SORT_ID:
7340 		sort = events_id_cmp;
7341 		break;
7342 	case TEP_EVENT_SORT_NAME:
7343 		sort = events_name_cmp;
7344 		break;
7345 	case TEP_EVENT_SORT_SYSTEM:
7346 		sort = events_system_cmp;
7347 		break;
7348 	default:
7349 		sort = NULL;
7350 	}
7351 
7352 	if (sort)
7353 		qsort(events, nr_events, sizeof(*events), sort);
7354 }
7355 
7356 /**
7357  * tep_list_events - Get events, sorted by given criteria.
7358  * @tep: a handle to the tep context
7359  * @sort_type: desired sort order of the events in the array
7360  *
7361  * Returns an array of pointers to all events, sorted by the given
7362  * @sort_type criteria. The last element of the array is NULL. The returned
7363  * memory must not be freed, it is managed by the library.
7364  * The function is not thread safe.
7365  */
tep_list_events(struct tep_handle * tep,enum tep_event_sort_type sort_type)7366 struct tep_event **tep_list_events(struct tep_handle *tep,
7367 				   enum tep_event_sort_type sort_type)
7368 {
7369 	struct tep_event **events;
7370 
7371 	if (!tep)
7372 		return NULL;
7373 
7374 	events = tep->sort_events;
7375 	if (events && tep->last_type == sort_type)
7376 		return events;
7377 
7378 	if (!events) {
7379 		events = list_events_copy(tep);
7380 		if (!events)
7381 			return NULL;
7382 
7383 		tep->sort_events = events;
7384 
7385 		/* the internal events are sorted by id */
7386 		if (sort_type == TEP_EVENT_SORT_ID) {
7387 			tep->last_type = sort_type;
7388 			return events;
7389 		}
7390 	}
7391 
7392 	list_events_sort(events, tep->nr_events, sort_type);
7393 	tep->last_type = sort_type;
7394 
7395 	return events;
7396 }
7397 
7398 
7399 /**
7400  * tep_list_events_copy - Thread safe version of tep_list_events()
7401  * @tep: a handle to the tep context
7402  * @sort_type: desired sort order of the events in the array
7403  *
7404  * Returns an array of pointers to all events, sorted by the given
7405  * @sort_type criteria. The last element of the array is NULL. The returned
7406  * array is newly allocated inside the function and must be freed by the caller
7407  */
tep_list_events_copy(struct tep_handle * tep,enum tep_event_sort_type sort_type)7408 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
7409 					enum tep_event_sort_type sort_type)
7410 {
7411 	struct tep_event **events;
7412 
7413 	if (!tep)
7414 		return NULL;
7415 
7416 	events = list_events_copy(tep);
7417 	if (!events)
7418 		return NULL;
7419 
7420 	/* the internal events are sorted by id */
7421 	if (sort_type == TEP_EVENT_SORT_ID)
7422 		return events;
7423 
7424 	list_events_sort(events, tep->nr_events, sort_type);
7425 
7426 	return events;
7427 }
7428 
7429 static struct tep_format_field **
get_event_fields(const char * type,const char * name,int count,struct tep_format_field * list)7430 get_event_fields(const char *type, const char *name,
7431 		 int count, struct tep_format_field *list)
7432 {
7433 	struct tep_format_field **fields;
7434 	struct tep_format_field *field;
7435 	int i = 0;
7436 
7437 	fields = malloc(sizeof(*fields) * (count + 1));
7438 	if (!fields)
7439 		return NULL;
7440 
7441 	for (field = list; field; field = field->next) {
7442 		fields[i++] = field;
7443 		if (i == count + 1) {
7444 			do_warning("event %s has more %s fields than specified",
7445 				name, type);
7446 			i--;
7447 			break;
7448 		}
7449 	}
7450 
7451 	if (i != count)
7452 		do_warning("event %s has less %s fields than specified",
7453 			name, type);
7454 
7455 	fields[i] = NULL;
7456 
7457 	return fields;
7458 }
7459 
7460 /**
7461  * tep_event_common_fields - return a list of common fields for an event
7462  * @event: the event to return the common fields of.
7463  *
7464  * Returns an allocated array of fields. The last item in the array is NULL.
7465  * The array must be freed with free().
7466  */
tep_event_common_fields(struct tep_event * event)7467 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
7468 {
7469 	return get_event_fields("common", event->name,
7470 				event->format.nr_common,
7471 				event->format.common_fields);
7472 }
7473 
7474 /**
7475  * tep_event_fields - return a list of event specific fields for an event
7476  * @event: the event to return the fields of.
7477  *
7478  * Returns an allocated array of fields. The last item in the array is NULL.
7479  * The array must be freed with free().
7480  */
tep_event_fields(struct tep_event * event)7481 struct tep_format_field **tep_event_fields(struct tep_event *event)
7482 {
7483 	return get_event_fields("event", event->name,
7484 				event->format.nr_fields,
7485 				event->format.fields);
7486 }
7487 
print_fields(struct trace_seq * s,struct tep_print_flag_sym * field)7488 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
7489 {
7490 	trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
7491 	if (field->next) {
7492 		trace_seq_puts(s, ", ");
7493 		print_fields(s, field->next);
7494 	}
7495 }
7496 
7497 /* for debugging */
print_args(struct tep_print_arg * args)7498 static void print_args(struct tep_print_arg *args)
7499 {
7500 	int print_paren = 1;
7501 	struct trace_seq s;
7502 
7503 	switch (args->type) {
7504 	case TEP_PRINT_NULL:
7505 		printf("null");
7506 		break;
7507 	case TEP_PRINT_ATOM:
7508 		printf("%s", args->atom.atom);
7509 		break;
7510 	case TEP_PRINT_FIELD:
7511 		printf("REC->%s", args->field.name);
7512 		break;
7513 	case TEP_PRINT_FLAGS:
7514 		printf("__print_flags(");
7515 		print_args(args->flags.field);
7516 		printf(", %s, ", args->flags.delim);
7517 		trace_seq_init(&s);
7518 		print_fields(&s, args->flags.flags);
7519 		trace_seq_do_printf(&s);
7520 		trace_seq_destroy(&s);
7521 		printf(")");
7522 		break;
7523 	case TEP_PRINT_SYMBOL:
7524 		printf("__print_symbolic(");
7525 		print_args(args->symbol.field);
7526 		printf(", ");
7527 		trace_seq_init(&s);
7528 		print_fields(&s, args->symbol.symbols);
7529 		trace_seq_do_printf(&s);
7530 		trace_seq_destroy(&s);
7531 		printf(")");
7532 		break;
7533 	case TEP_PRINT_HEX:
7534 		printf("__print_hex(");
7535 		print_args(args->hex.field);
7536 		printf(", ");
7537 		print_args(args->hex.size);
7538 		printf(")");
7539 		break;
7540 	case TEP_PRINT_HEX_STR:
7541 		printf("__print_hex_str(");
7542 		print_args(args->hex.field);
7543 		printf(", ");
7544 		print_args(args->hex.size);
7545 		printf(")");
7546 		break;
7547 	case TEP_PRINT_INT_ARRAY:
7548 		printf("__print_array(");
7549 		print_args(args->int_array.field);
7550 		printf(", ");
7551 		print_args(args->int_array.count);
7552 		printf(", ");
7553 		print_args(args->int_array.el_size);
7554 		printf(")");
7555 		break;
7556 	case TEP_PRINT_STRING:
7557 	case TEP_PRINT_BSTRING:
7558 		printf("__get_str(%s)", args->string.string);
7559 		break;
7560 	case TEP_PRINT_BITMASK:
7561 		printf("__get_bitmask(%s)", args->bitmask.bitmask);
7562 		break;
7563 	case TEP_PRINT_CPUMASK:
7564 		printf("__get_cpumask(%s)", args->bitmask.bitmask);
7565 		break;
7566 	case TEP_PRINT_TYPE:
7567 		printf("(%s)", args->typecast.type);
7568 		print_args(args->typecast.item);
7569 		break;
7570 	case TEP_PRINT_OP:
7571 		if (strcmp(args->op.op, ":") == 0)
7572 			print_paren = 0;
7573 		if (print_paren)
7574 			printf("(");
7575 		print_args(args->op.left);
7576 		printf(" %s ", args->op.op);
7577 		print_args(args->op.right);
7578 		if (print_paren)
7579 			printf(")");
7580 		break;
7581 	default:
7582 		/* we should warn... */
7583 		return;
7584 	}
7585 	if (args->next) {
7586 		printf("\n");
7587 		print_args(args->next);
7588 	}
7589 }
7590 
parse_header_field(struct tep_handle * tep,const char * field,int * offset,int * size,int mandatory)7591 static void parse_header_field(struct tep_handle *tep, const char *field,
7592 			       int *offset, int *size, int mandatory)
7593 {
7594 	unsigned long long save_input_buf_ptr;
7595 	unsigned long long save_input_buf_siz;
7596 	char *token;
7597 	int type;
7598 
7599 	save_input_buf_ptr = tep->input_buf_ptr;
7600 	save_input_buf_siz = tep->input_buf_siz;
7601 
7602 	if (read_expected(tep, TEP_EVENT_ITEM, "field") < 0)
7603 		return;
7604 	if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7605 		return;
7606 
7607 	/* type */
7608 	if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7609 		goto fail;
7610 	free_token(token);
7611 
7612 	/*
7613 	 * If this is not a mandatory field, then test it first.
7614 	 */
7615 	if (mandatory) {
7616 		if (read_expected(tep, TEP_EVENT_ITEM, field) < 0)
7617 			return;
7618 	} else {
7619 		if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7620 			goto fail;
7621 		if (strcmp(token, field) != 0)
7622 			goto discard;
7623 		free_token(token);
7624 	}
7625 
7626 	if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7627 		return;
7628 	if (read_expected(tep, TEP_EVENT_ITEM, "offset") < 0)
7629 		return;
7630 	if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7631 		return;
7632 	if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7633 		goto fail;
7634 	*offset = atoi(token);
7635 	free_token(token);
7636 	if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7637 		return;
7638 	if (read_expected(tep, TEP_EVENT_ITEM, "size") < 0)
7639 		return;
7640 	if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7641 		return;
7642 	if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7643 		goto fail;
7644 	*size = atoi(token);
7645 	free_token(token);
7646 	if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7647 		return;
7648 	type = read_token(tep, &token);
7649 	if (type != TEP_EVENT_NEWLINE) {
7650 		/* newer versions of the kernel have a "signed" type */
7651 		if (type != TEP_EVENT_ITEM)
7652 			goto fail;
7653 
7654 		if (strcmp(token, "signed") != 0)
7655 			goto fail;
7656 
7657 		free_token(token);
7658 
7659 		if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7660 			return;
7661 
7662 		if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
7663 			goto fail;
7664 
7665 		free_token(token);
7666 		if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7667 			return;
7668 
7669 		if (read_expect_type(tep, TEP_EVENT_NEWLINE, &token))
7670 			goto fail;
7671 	}
7672  fail:
7673 	free_token(token);
7674 	return;
7675 
7676  discard:
7677 	tep->input_buf_ptr = save_input_buf_ptr;
7678 	tep->input_buf_siz = save_input_buf_siz;
7679 	*offset = 0;
7680 	*size = 0;
7681 	free_token(token);
7682 }
7683 
7684 /**
7685  * tep_parse_header_page - parse the data stored in the header page
7686  * @tep: a handle to the trace event parser context
7687  * @buf: the buffer storing the header page format string
7688  * @size: the size of @buf
7689  * @long_size: the long size to use if there is no header
7690  *
7691  * This parses the header page format for information on the
7692  * ring buffer used. The @buf should be copied from
7693  *
7694  * /sys/kernel/debug/tracing/events/header_page
7695  */
tep_parse_header_page(struct tep_handle * tep,char * buf,unsigned long size,int long_size)7696 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
7697 			  int long_size)
7698 {
7699 	int ignore;
7700 
7701 	if (!size) {
7702 		/*
7703 		 * Old kernels did not have header page info.
7704 		 * Sorry but we just use what we find here in user space.
7705 		 */
7706 		tep->header_page_ts_size = sizeof(long long);
7707 		tep->header_page_size_size = long_size;
7708 		tep->header_page_data_offset = sizeof(long long) + long_size;
7709 		tep->header_page_data_size = getpagesize() - tep->header_page_data_offset;
7710 		tep->old_format = 1;
7711 		return -1;
7712 	}
7713 	init_input_buf(tep, buf, size);
7714 
7715 	parse_header_field(tep, "timestamp", &tep->header_page_ts_offset,
7716 			   &tep->header_page_ts_size, 1);
7717 	parse_header_field(tep, "commit", &tep->header_page_size_offset,
7718 			   &tep->header_page_size_size, 1);
7719 	parse_header_field(tep, "overwrite", &tep->header_page_overwrite,
7720 			   &ignore, 0);
7721 	parse_header_field(tep, "data", &tep->header_page_data_offset,
7722 			   &tep->header_page_data_size, 1);
7723 
7724 	return 0;
7725 }
7726 
event_matches(struct tep_event * event,int id,const char * sys_name,const char * event_name)7727 static int event_matches(struct tep_event *event,
7728 			 int id, const char *sys_name,
7729 			 const char *event_name)
7730 {
7731 	if (id >= 0 && id != event->id)
7732 		return 0;
7733 
7734 	if (event_name && (strcmp(event_name, event->name) != 0))
7735 		return 0;
7736 
7737 	if (sys_name && (strcmp(sys_name, event->system) != 0))
7738 		return 0;
7739 
7740 	return 1;
7741 }
7742 
free_handler(struct event_handler * handle)7743 static void free_handler(struct event_handler *handle)
7744 {
7745 	free((void *)handle->sys_name);
7746 	free((void *)handle->event_name);
7747 	free(handle);
7748 }
7749 
find_event_handle(struct tep_handle * tep,struct tep_event * event)7750 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
7751 {
7752 	struct event_handler *handle, **next;
7753 
7754 	for (next = &tep->handlers; *next;
7755 	     next = &(*next)->next) {
7756 		handle = *next;
7757 		if (event_matches(event, handle->id,
7758 				  handle->sys_name,
7759 				  handle->event_name))
7760 			break;
7761 	}
7762 
7763 	if (!(*next))
7764 		return 0;
7765 
7766 	tep_info("overriding event (%d) %s:%s with new print handler",
7767 		 event->id, event->system, event->name);
7768 
7769 	event->handler = handle->func;
7770 	event->context = handle->context;
7771 
7772 	*next = handle->next;
7773 	free_handler(handle);
7774 
7775 	return 1;
7776 }
7777 
7778 /**
7779  * parse_format - parse the event format
7780  * @buf: the buffer storing the event format string
7781  * @size: the size of @buf
7782  * @sys: the system the event belongs to
7783  *
7784  * This parses the event format and creates an event structure
7785  * to quickly parse raw data for a given event.
7786  *
7787  * These files currently come from:
7788  *
7789  * /sys/kernel/debug/tracing/events/.../.../format
7790  */
parse_format(struct tep_event ** eventp,struct tep_handle * tep,const char * buf,unsigned long size,const char * sys)7791 static enum tep_errno parse_format(struct tep_event **eventp,
7792 				   struct tep_handle *tep, const char *buf,
7793 				   unsigned long size, const char *sys)
7794 {
7795 	struct tep_event *event;
7796 	int ret;
7797 
7798 	init_input_buf(tep, buf, size);
7799 
7800 	*eventp = event = alloc_event();
7801 	if (!event)
7802 		return TEP_ERRNO__MEM_ALLOC_FAILED;
7803 
7804 	event->name = event_read_name(tep);
7805 	if (!event->name) {
7806 		/* Bad event? */
7807 		ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7808 		goto event_alloc_failed;
7809 	}
7810 
7811 	if (strcmp(sys, "ftrace") == 0) {
7812 		event->flags |= TEP_EVENT_FL_ISFTRACE;
7813 
7814 		if (strcmp(event->name, "bprint") == 0)
7815 			event->flags |= TEP_EVENT_FL_ISBPRINT;
7816 	}
7817 
7818 	event->id = event_read_id(tep);
7819 	if (event->id < 0) {
7820 		ret = TEP_ERRNO__READ_ID_FAILED;
7821 		/*
7822 		 * This isn't an allocation error actually.
7823 		 * But as the ID is critical, just bail out.
7824 		 */
7825 		goto event_alloc_failed;
7826 	}
7827 
7828 	event->system = strdup(sys);
7829 	if (!event->system) {
7830 		ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7831 		goto event_alloc_failed;
7832 	}
7833 
7834 	/* Add tep to event so that it can be referenced */
7835 	event->tep = tep;
7836 
7837 	ret = event_read_format(event);
7838 	if (ret < 0) {
7839 		ret = TEP_ERRNO__READ_FORMAT_FAILED;
7840 		goto event_parse_failed;
7841 	}
7842 
7843 	/*
7844 	 * If the event has an override, don't print warnings if the event
7845 	 * print format fails to parse.
7846 	 */
7847 	if (tep && find_event_handle(tep, event))
7848 		show_warning = 0;
7849 
7850 	ret = event_read_print(event);
7851 	show_warning = 1;
7852 
7853 	if (ret < 0) {
7854 		ret = TEP_ERRNO__READ_PRINT_FAILED;
7855 		goto event_parse_failed;
7856 	}
7857 
7858 	if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
7859 		struct tep_format_field *field;
7860 		struct tep_print_arg *arg, **list;
7861 
7862 		/* old ftrace had no args */
7863 		list = &event->print_fmt.args;
7864 		for (field = event->format.fields; field; field = field->next) {
7865 			arg = alloc_arg();
7866 			if (!arg) {
7867 				event->flags |= TEP_EVENT_FL_FAILED;
7868 				return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
7869 			}
7870 			arg->type = TEP_PRINT_FIELD;
7871 			arg->field.name = strdup(field->name);
7872 			if (!arg->field.name) {
7873 				event->flags |= TEP_EVENT_FL_FAILED;
7874 				free_arg(arg);
7875 				return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
7876 			}
7877 			arg->field.field = field;
7878 			*list = arg;
7879 			list = &arg->next;
7880 		}
7881 	}
7882 
7883 	if (!(event->flags & TEP_EVENT_FL_ISBPRINT))
7884 		event->print_fmt.print_cache = parse_args(event,
7885 							  event->print_fmt.format,
7886 							  event->print_fmt.args);
7887 
7888 	return 0;
7889 
7890  event_parse_failed:
7891 	event->flags |= TEP_EVENT_FL_FAILED;
7892 	return ret;
7893 
7894  event_alloc_failed:
7895 	free(event->system);
7896 	free(event->name);
7897 	free(event);
7898 	*eventp = NULL;
7899 	return ret;
7900 }
7901 
7902 static enum tep_errno
__parse_event(struct tep_handle * tep,struct tep_event ** eventp,const char * buf,unsigned long size,const char * sys)7903 __parse_event(struct tep_handle *tep,
7904 	      struct tep_event **eventp,
7905 	      const char *buf, unsigned long size,
7906 	      const char *sys)
7907 {
7908 	int ret = parse_format(eventp, tep, buf, size, sys);
7909 	struct tep_event *event = *eventp;
7910 
7911 	if (event == NULL)
7912 		return ret;
7913 
7914 	if (tep && add_event(tep, event)) {
7915 		ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7916 		goto event_add_failed;
7917 	}
7918 
7919 #define PRINT_ARGS 0
7920 	if (PRINT_ARGS && event->print_fmt.args)
7921 		print_args(event->print_fmt.args);
7922 
7923 	return 0;
7924 
7925 event_add_failed:
7926 	free_tep_event(event);
7927 	return ret;
7928 }
7929 
7930 /**
7931  * tep_parse_format - parse the event format
7932  * @tep: a handle to the trace event parser context
7933  * @eventp: returned format
7934  * @buf: the buffer storing the event format string
7935  * @size: the size of @buf
7936  * @sys: the system the event belongs to
7937  *
7938  * This parses the event format and creates an event structure
7939  * to quickly parse raw data for a given event.
7940  *
7941  * These files currently come from:
7942  *
7943  * /sys/kernel/debug/tracing/events/.../.../format
7944  */
tep_parse_format(struct tep_handle * tep,struct tep_event ** eventp,const char * buf,unsigned long size,const char * sys)7945 enum tep_errno tep_parse_format(struct tep_handle *tep,
7946 				struct tep_event **eventp,
7947 				const char *buf,
7948 				unsigned long size, const char *sys)
7949 {
7950 	return __parse_event(tep, eventp, buf, size, sys);
7951 }
7952 
7953 /**
7954  * tep_parse_event - parse the event format
7955  * @tep: a handle to the trace event parser context
7956  * @buf: the buffer storing the event format string
7957  * @size: the size of @buf
7958  * @sys: the system the event belongs to
7959  *
7960  * This parses the event format and creates an event structure
7961  * to quickly parse raw data for a given event.
7962  *
7963  * These files currently come from:
7964  *
7965  * /sys/kernel/debug/tracing/events/.../.../format
7966  */
tep_parse_event(struct tep_handle * tep,const char * buf,unsigned long size,const char * sys)7967 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
7968 			       unsigned long size, const char *sys)
7969 {
7970 	struct tep_event *event = NULL;
7971 	return __parse_event(tep, &event, buf, size, sys);
7972 }
7973 
get_field_val(struct trace_seq * s,struct tep_format_field * field,const char * name,struct tep_record * record,unsigned long long * val,int err)7974 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
7975 		  const char *name, struct tep_record *record,
7976 		  unsigned long long *val, int err)
7977 {
7978 	if (!field) {
7979 		if (err)
7980 			trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
7981 		return -1;
7982 	}
7983 
7984 	if (tep_read_number_field(field, record->data, val)) {
7985 		if (err)
7986 			trace_seq_printf(s, " %s=INVALID", name);
7987 		return -1;
7988 	}
7989 
7990 	return 0;
7991 }
7992 
7993 /**
7994  * tep_get_field_raw - return the raw pointer into the data field
7995  * @s: The seq to print to on error
7996  * @event: the event that the field is for
7997  * @name: The name of the field
7998  * @record: The record with the field name.
7999  * @len: place to store the field length.
8000  * @err: print default error if failed.
8001  *
8002  * Returns a pointer into record->data of the field and places
8003  * the length of the field in @len.
8004  *
8005  * On failure, it returns NULL.
8006  */
tep_get_field_raw(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,int * len,int err)8007 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
8008 			const char *name, struct tep_record *record,
8009 			int *len, int err)
8010 {
8011 	struct tep_format_field *field;
8012 	void *data = record->data;
8013 	unsigned offset;
8014 	int dummy;
8015 
8016 	if (!event)
8017 		return NULL;
8018 
8019 	field = tep_find_field(event, name);
8020 
8021 	if (!field) {
8022 		if (err)
8023 			trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
8024 		return NULL;
8025 	}
8026 
8027 	/* Allow @len to be NULL */
8028 	if (!len)
8029 		len = &dummy;
8030 
8031 	offset = field->offset;
8032 	if (field->flags & TEP_FIELD_IS_DYNAMIC) {
8033 		offset = tep_read_number(event->tep,
8034 					 data + offset, field->size);
8035 		*len = offset >> 16;
8036 		offset &= 0xffff;
8037 		if (field->flags & TEP_FIELD_IS_RELATIVE)
8038 			offset += field->offset + field->size;
8039 	} else
8040 		*len = field->size;
8041 
8042 	return data + offset;
8043 }
8044 
8045 /**
8046  * tep_get_field_val - find a field and return its value
8047  * @s: The seq to print to on error
8048  * @event: the event that the field is for
8049  * @name: The name of the field
8050  * @record: The record with the field name.
8051  * @val: place to store the value of the field.
8052  * @err: print default error if failed.
8053  *
8054  * Returns 0 on success -1 on field not found.
8055  */
tep_get_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)8056 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
8057 		      const char *name, struct tep_record *record,
8058 		      unsigned long long *val, int err)
8059 {
8060 	struct tep_format_field *field;
8061 
8062 	if (!event)
8063 		return -1;
8064 
8065 	field = tep_find_field(event, name);
8066 
8067 	return get_field_val(s, field, name, record, val, err);
8068 }
8069 
8070 /**
8071  * tep_get_common_field_val - find a common field and return its value
8072  * @s: The seq to print to on error
8073  * @event: the event that the field is for
8074  * @name: The name of the field
8075  * @record: The record with the field name.
8076  * @val: place to store the value of the field.
8077  * @err: print default error if failed.
8078  *
8079  * Returns 0 on success -1 on field not found.
8080  */
tep_get_common_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)8081 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
8082 			     const char *name, struct tep_record *record,
8083 			     unsigned long long *val, int err)
8084 {
8085 	struct tep_format_field *field;
8086 
8087 	if (!event)
8088 		return -1;
8089 
8090 	field = tep_find_common_field(event, name);
8091 
8092 	return get_field_val(s, field, name, record, val, err);
8093 }
8094 
8095 /**
8096  * tep_get_any_field_val - find a any field and return its value
8097  * @s: The seq to print to on error
8098  * @event: the event that the field is for
8099  * @name: The name of the field
8100  * @record: The record with the field name.
8101  * @val: place to store the value of the field.
8102  * @err: print default error if failed.
8103  *
8104  * Returns 0 on success -1 on field not found.
8105  */
tep_get_any_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)8106 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
8107 			  const char *name, struct tep_record *record,
8108 			  unsigned long long *val, int err)
8109 {
8110 	struct tep_format_field *field;
8111 
8112 	if (!event)
8113 		return -1;
8114 
8115 	field = tep_find_any_field(event, name);
8116 
8117 	return get_field_val(s, field, name, record, val, err);
8118 }
8119 
8120 /**
8121  * tep_print_num_field - print a field and a format
8122  * @s: The seq to print to
8123  * @fmt: The printf format to print the field with.
8124  * @event: the event that the field is for
8125  * @name: The name of the field
8126  * @record: The record with the field name.
8127  * @err: print default error if failed.
8128  *
8129  * Returns positive value on success, negative in case of an error,
8130  * or 0 if buffer is full.
8131  */
tep_print_num_field(struct trace_seq * s,const char * fmt,struct tep_event * event,const char * name,struct tep_record * record,int err)8132 int tep_print_num_field(struct trace_seq *s, const char *fmt,
8133 			struct tep_event *event, const char *name,
8134 			struct tep_record *record, int err)
8135 {
8136 	struct tep_format_field *field = tep_find_field(event, name);
8137 	unsigned long long val;
8138 
8139 	if (!field)
8140 		goto failed;
8141 
8142 	if (tep_read_number_field(field, record->data, &val))
8143 		goto failed;
8144 
8145 	return trace_seq_printf(s, fmt, val);
8146 
8147  failed:
8148 	if (err)
8149 		trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
8150 	return -1;
8151 }
8152 
8153 /**
8154  * tep_print_func_field - print a field and a format for function pointers
8155  * @s: The seq to print to
8156  * @fmt: The printf format to print the field with.
8157  * @event: the event that the field is for
8158  * @name: The name of the field
8159  * @record: The record with the field name.
8160  * @err: print default error if failed.
8161  *
8162  * Returns positive value on success, negative in case of an error,
8163  * or 0 if buffer is full.
8164  */
tep_print_func_field(struct trace_seq * s,const char * fmt,struct tep_event * event,const char * name,struct tep_record * record,int err)8165 int tep_print_func_field(struct trace_seq *s, const char *fmt,
8166 			 struct tep_event *event, const char *name,
8167 			 struct tep_record *record, int err)
8168 {
8169 	struct tep_format_field *field = tep_find_field(event, name);
8170 	struct tep_handle *tep = event->tep;
8171 	unsigned long long val;
8172 	struct func_map *func;
8173 	char tmp[128];
8174 
8175 	if (!field)
8176 		goto failed;
8177 
8178 	if (tep_read_number_field(field, record->data, &val))
8179 		goto failed;
8180 
8181 	func = find_func(tep, val);
8182 
8183 	if (func)
8184 		snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
8185 	else
8186 		sprintf(tmp, "0x%08llx", val);
8187 
8188 	return trace_seq_printf(s, fmt, tmp);
8189 
8190  failed:
8191 	if (err)
8192 		trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
8193 	return -1;
8194 }
8195 
free_func_handle(struct tep_function_handler * func)8196 static void free_func_handle(struct tep_function_handler *func)
8197 {
8198 	struct func_params *params;
8199 
8200 	free(func->name);
8201 
8202 	while (func->params) {
8203 		params = func->params;
8204 		func->params = params->next;
8205 		free(params);
8206 	}
8207 
8208 	free(func);
8209 }
8210 
8211 /**
8212  * tep_register_print_function - register a helper function
8213  * @tep: a handle to the trace event parser context
8214  * @func: the function to process the helper function
8215  * @ret_type: the return type of the helper function
8216  * @name: the name of the helper function
8217  * @parameters: A list of enum tep_func_arg_type
8218  *
8219  * Some events may have helper functions in the print format arguments.
8220  * This allows a plugin to dynamically create a way to process one
8221  * of these functions.
8222  *
8223  * The @parameters is a variable list of tep_func_arg_type enums that
8224  * must end with TEP_FUNC_ARG_VOID.
8225  */
tep_register_print_function(struct tep_handle * tep,tep_func_handler func,enum tep_func_arg_type ret_type,char * name,...)8226 int tep_register_print_function(struct tep_handle *tep,
8227 				tep_func_handler func,
8228 				enum tep_func_arg_type ret_type,
8229 				char *name, ...)
8230 {
8231 	struct tep_function_handler *func_handle;
8232 	struct func_params **next_param;
8233 	struct func_params *param;
8234 	enum tep_func_arg_type type;
8235 	va_list ap;
8236 	int ret;
8237 
8238 	func_handle = find_func_handler(tep, name);
8239 	if (func_handle) {
8240 		/*
8241 		 * This is most like caused by the users own
8242 		 * plugins updating the function. This overrides the
8243 		 * system defaults.
8244 		 */
8245 		tep_info("override of function helper '%s'", name);
8246 		remove_func_handler(tep, name);
8247 	}
8248 
8249 	func_handle = calloc(1, sizeof(*func_handle));
8250 	if (!func_handle) {
8251 		do_warning("Failed to allocate function handler");
8252 		return TEP_ERRNO__MEM_ALLOC_FAILED;
8253 	}
8254 
8255 	func_handle->ret_type = ret_type;
8256 	func_handle->name = strdup(name);
8257 	func_handle->func = func;
8258 	if (!func_handle->name) {
8259 		do_warning("Failed to allocate function name");
8260 		free(func_handle);
8261 		return TEP_ERRNO__MEM_ALLOC_FAILED;
8262 	}
8263 
8264 	next_param = &(func_handle->params);
8265 	va_start(ap, name);
8266 	for (;;) {
8267 		type = va_arg(ap, enum tep_func_arg_type);
8268 		if (type == TEP_FUNC_ARG_VOID)
8269 			break;
8270 
8271 		if (type >= TEP_FUNC_ARG_MAX_TYPES) {
8272 			do_warning("Invalid argument type %d", type);
8273 			ret = TEP_ERRNO__INVALID_ARG_TYPE;
8274 			goto out_free;
8275 		}
8276 
8277 		param = malloc(sizeof(*param));
8278 		if (!param) {
8279 			do_warning("Failed to allocate function param");
8280 			ret = TEP_ERRNO__MEM_ALLOC_FAILED;
8281 			goto out_free;
8282 		}
8283 		param->type = type;
8284 		param->next = NULL;
8285 
8286 		*next_param = param;
8287 		next_param = &(param->next);
8288 
8289 		func_handle->nr_args++;
8290 	}
8291 	va_end(ap);
8292 
8293 	func_handle->next = tep->func_handlers;
8294 	tep->func_handlers = func_handle;
8295 
8296 	return 0;
8297  out_free:
8298 	va_end(ap);
8299 	free_func_handle(func_handle);
8300 	return ret;
8301 }
8302 
8303 /**
8304  * tep_unregister_print_function - unregister a helper function
8305  * @tep: a handle to the trace event parser context
8306  * @func: the function to process the helper function
8307  * @name: the name of the helper function
8308  *
8309  * This function removes existing print handler for function @name.
8310  *
8311  * Returns 0 if the handler was removed successully, -1 otherwise.
8312  */
tep_unregister_print_function(struct tep_handle * tep,tep_func_handler func,char * name)8313 int tep_unregister_print_function(struct tep_handle *tep,
8314 				  tep_func_handler func, char *name)
8315 {
8316 	struct tep_function_handler *func_handle;
8317 
8318 	func_handle = find_func_handler(tep, name);
8319 	if (func_handle && func_handle->func == func) {
8320 		remove_func_handler(tep, name);
8321 		return 0;
8322 	}
8323 	return -1;
8324 }
8325 
search_event(struct tep_handle * tep,int id,const char * sys_name,const char * event_name)8326 static struct tep_event *search_event(struct tep_handle *tep, int id,
8327 				      const char *sys_name,
8328 				      const char *event_name)
8329 {
8330 	struct tep_event *event;
8331 
8332 	if (id >= 0) {
8333 		/* search by id */
8334 		event = tep_find_event(tep, id);
8335 		if (!event)
8336 			return NULL;
8337 		if (event_name && (strcmp(event_name, event->name) != 0))
8338 			return NULL;
8339 		if (sys_name && (strcmp(sys_name, event->system) != 0))
8340 			return NULL;
8341 	} else {
8342 		event = tep_find_event_by_name(tep, sys_name, event_name);
8343 		if (!event)
8344 			return NULL;
8345 	}
8346 	return event;
8347 }
8348 
8349 /**
8350  * tep_register_event_handler - register a way to parse an event
8351  * @tep: a handle to the trace event parser context
8352  * @id: the id of the event to register
8353  * @sys_name: the system name the event belongs to
8354  * @event_name: the name of the event
8355  * @func: the function to call to parse the event information
8356  * @context: the data to be passed to @func
8357  *
8358  * This function allows a developer to override the parsing of
8359  * a given event. If for some reason the default print format
8360  * is not sufficient, this function will register a function
8361  * for an event to be used to parse the data instead.
8362  *
8363  * If @id is >= 0, then it is used to find the event.
8364  * else @sys_name and @event_name are used.
8365  *
8366  * Returns:
8367  *  TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
8368  *  TEP_REGISTER_SUCCESS if a new handler is registered successfully
8369  *  negative TEP_ERRNO_... in case of an error
8370  *
8371  */
tep_register_event_handler(struct tep_handle * tep,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8372 int tep_register_event_handler(struct tep_handle *tep, int id,
8373 			       const char *sys_name, const char *event_name,
8374 			       tep_event_handler_func func, void *context)
8375 {
8376 	struct tep_event *event;
8377 	struct event_handler *handle;
8378 
8379 	event = search_event(tep, id, sys_name, event_name);
8380 	if (event == NULL)
8381 		goto not_found;
8382 
8383 	tep_info("overriding event (%d) %s:%s with new print handler",
8384 		 event->id, event->system, event->name);
8385 
8386 	event->handler = func;
8387 	event->context = context;
8388 	return TEP_REGISTER_SUCCESS_OVERWRITE;
8389 
8390  not_found:
8391 	/* Save for later use. */
8392 	handle = calloc(1, sizeof(*handle));
8393 	if (!handle) {
8394 		do_warning("Failed to allocate event handler");
8395 		return TEP_ERRNO__MEM_ALLOC_FAILED;
8396 	}
8397 
8398 	handle->id = id;
8399 	if (event_name)
8400 		handle->event_name = strdup(event_name);
8401 	if (sys_name)
8402 		handle->sys_name = strdup(sys_name);
8403 
8404 	if ((event_name && !handle->event_name) ||
8405 	    (sys_name && !handle->sys_name)) {
8406 		do_warning("Failed to allocate event/sys name");
8407 		free((void *)handle->event_name);
8408 		free((void *)handle->sys_name);
8409 		free(handle);
8410 		return TEP_ERRNO__MEM_ALLOC_FAILED;
8411 	}
8412 
8413 	handle->func = func;
8414 	handle->next = tep->handlers;
8415 	tep->handlers = handle;
8416 	handle->context = context;
8417 
8418 	return TEP_REGISTER_SUCCESS;
8419 }
8420 
handle_matches(struct event_handler * handler,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8421 static int handle_matches(struct event_handler *handler, int id,
8422 			  const char *sys_name, const char *event_name,
8423 			  tep_event_handler_func func, void *context)
8424 {
8425 	if (id >= 0 && id != handler->id)
8426 		return 0;
8427 
8428 	if (event_name && (strcmp(event_name, handler->event_name) != 0))
8429 		return 0;
8430 
8431 	if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
8432 		return 0;
8433 
8434 	if (func != handler->func || context != handler->context)
8435 		return 0;
8436 
8437 	return 1;
8438 }
8439 
8440 /**
8441  * tep_unregister_event_handler - unregister an existing event handler
8442  * @tep: a handle to the trace event parser context
8443  * @id: the id of the event to unregister
8444  * @sys_name: the system name the handler belongs to
8445  * @event_name: the name of the event handler
8446  * @func: the function to call to parse the event information
8447  * @context: the data to be passed to @func
8448  *
8449  * This function removes existing event handler (parser).
8450  *
8451  * If @id is >= 0, then it is used to find the event.
8452  * else @sys_name and @event_name are used.
8453  *
8454  * Returns 0 if handler was removed successfully, -1 if event was not found.
8455  */
tep_unregister_event_handler(struct tep_handle * tep,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8456 int tep_unregister_event_handler(struct tep_handle *tep, int id,
8457 				 const char *sys_name, const char *event_name,
8458 				 tep_event_handler_func func, void *context)
8459 {
8460 	struct tep_event *event;
8461 	struct event_handler *handle;
8462 	struct event_handler **next;
8463 
8464 	event = search_event(tep, id, sys_name, event_name);
8465 	if (event == NULL)
8466 		goto not_found;
8467 
8468 	if (event->handler == func && event->context == context) {
8469 		tep_info("removing override handler for event (%d) %s:%s. Going back to default handler.",
8470 			 event->id, event->system, event->name);
8471 
8472 		event->handler = NULL;
8473 		event->context = NULL;
8474 		return 0;
8475 	}
8476 
8477 not_found:
8478 	for (next = &tep->handlers; *next; next = &(*next)->next) {
8479 		handle = *next;
8480 		if (handle_matches(handle, id, sys_name, event_name,
8481 				   func, context))
8482 			break;
8483 	}
8484 
8485 	if (!(*next))
8486 		return -1;
8487 
8488 	*next = handle->next;
8489 	free_handler(handle);
8490 
8491 	return 0;
8492 }
8493 
8494 /**
8495  * tep_alloc - create a tep handle
8496  */
tep_alloc(void)8497 struct tep_handle *tep_alloc(void)
8498 {
8499 	struct tep_handle *tep = calloc(1, sizeof(*tep));
8500 
8501 	if (tep) {
8502 		tep->ref_count = 1;
8503 		tep->host_bigendian = tep_is_bigendian();
8504 	}
8505 
8506 	return tep;
8507 }
8508 
tep_ref(struct tep_handle * tep)8509 void tep_ref(struct tep_handle *tep)
8510 {
8511 	tep->ref_count++;
8512 }
8513 
tep_get_ref(struct tep_handle * tep)8514 int tep_get_ref(struct tep_handle *tep)
8515 {
8516 	if (tep)
8517 		return tep->ref_count;
8518 	return 0;
8519 }
8520 
free_tep_format_field(struct tep_format_field * field)8521 __hidden void free_tep_format_field(struct tep_format_field *field)
8522 {
8523 	free(field->type);
8524 	if (field->alias != field->name)
8525 		free(field->alias);
8526 	free(field->name);
8527 	free(field);
8528 }
8529 
free_format_fields(struct tep_format_field * field)8530 static void free_format_fields(struct tep_format_field *field)
8531 {
8532 	struct tep_format_field *next;
8533 
8534 	while (field) {
8535 		next = field->next;
8536 		free_tep_format_field(field);
8537 		field = next;
8538 	}
8539 }
8540 
free_formats(struct tep_format * format)8541 static void free_formats(struct tep_format *format)
8542 {
8543 	free_format_fields(format->common_fields);
8544 	free_format_fields(format->fields);
8545 }
8546 
free_tep_event(struct tep_event * event)8547 __hidden void free_tep_event(struct tep_event *event)
8548 {
8549 	free(event->name);
8550 	free(event->system);
8551 
8552 	free_formats(&event->format);
8553 
8554 	free(event->print_fmt.format);
8555 	free_args(event->print_fmt.args);
8556 	free_parse_args(event->print_fmt.print_cache);
8557 	free(event);
8558 }
8559 
8560 /**
8561  * tep_free - free a tep handle
8562  * @tep: the tep handle to free
8563  */
tep_free(struct tep_handle * tep)8564 void tep_free(struct tep_handle *tep)
8565 {
8566 	struct cmdline_list *cmdlist, *cmdnext;
8567 	struct func_list *funclist, *funcnext;
8568 	struct printk_list *printklist, *printknext;
8569 	struct tep_function_handler *func_handler;
8570 	struct event_handler *handle;
8571 	int i;
8572 
8573 	if (!tep)
8574 		return;
8575 
8576 	cmdlist = tep->cmdlist;
8577 	funclist = tep->funclist;
8578 	printklist = tep->printklist;
8579 
8580 	tep->ref_count--;
8581 	if (tep->ref_count)
8582 		return;
8583 
8584 	if (tep->cmdlines) {
8585 		for (i = 0; i < tep->cmdline_count; i++)
8586 			free(tep->cmdlines[i].comm);
8587 		free(tep->cmdlines);
8588 	}
8589 
8590 	while (cmdlist) {
8591 		cmdnext = cmdlist->next;
8592 		free(cmdlist->comm);
8593 		free(cmdlist);
8594 		cmdlist = cmdnext;
8595 	}
8596 
8597 	if (tep->func_map) {
8598 		for (i = 0; i < (int)tep->func_count; i++) {
8599 			free(tep->func_map[i].func);
8600 			free(tep->func_map[i].mod);
8601 		}
8602 		free(tep->func_map);
8603 	}
8604 
8605 	while (funclist) {
8606 		funcnext = funclist->next;
8607 		free(funclist->func);
8608 		free(funclist->mod);
8609 		free(funclist);
8610 		funclist = funcnext;
8611 	}
8612 
8613 	while (tep->func_handlers) {
8614 		func_handler = tep->func_handlers;
8615 		tep->func_handlers = func_handler->next;
8616 		free_func_handle(func_handler);
8617 	}
8618 
8619 	if (tep->printk_map) {
8620 		for (i = 0; i < (int)tep->printk_count; i++)
8621 			free(tep->printk_map[i].printk);
8622 		free(tep->printk_map);
8623 	}
8624 
8625 	while (printklist) {
8626 		printknext = printklist->next;
8627 		free(printklist->printk);
8628 		free(printklist);
8629 		printklist = printknext;
8630 	}
8631 
8632 	for (i = 0; i < tep->nr_events; i++)
8633 		free_tep_event(tep->events[i]);
8634 
8635 	while (tep->handlers) {
8636 		handle = tep->handlers;
8637 		tep->handlers = handle->next;
8638 		free_handler(handle);
8639 	}
8640 
8641 	free(tep->events);
8642 	free(tep->sort_events);
8643 	free(tep->func_resolver);
8644 	free_tep_plugin_paths(tep);
8645 
8646 	free(tep);
8647 }
8648 
tep_unref(struct tep_handle * tep)8649 void tep_unref(struct tep_handle *tep)
8650 {
8651 	tep_free(tep);
8652 }
8653