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