trace-event-parse.c 65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License (not later!)
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  The parts for function graph printing was taken and modified from the
 *  Linux Kernel that were written by Frederic Weisbecker.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#undef _GNU_SOURCE
32
#include "../perf.h"
33
34
35
36
37
38
39
40
41
42
#include "util.h"
#include "trace-event.h"

int header_page_ts_offset;
int header_page_ts_size;
int header_page_size_offset;
int header_page_size_size;
int header_page_data_offset;
int header_page_data_size;

43
44
int latency_format;

45
46
47
48
49
50
static char *input_buf;
static unsigned long long input_buf_ptr;
static unsigned long long input_buf_siz;

static int cpus;
static int long_size;
51
52
53
54
55
static int is_flag_field;
static int is_symbolic_field;

static struct format_field *
find_any_field(struct event *event, const char *name);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

static void init_input_buf(char *buf, unsigned long long size)
{
	input_buf = buf;
	input_buf_siz = size;
	input_buf_ptr = 0;
}

struct cmdline {
	char *comm;
	int pid;
};

static struct cmdline *cmdlines;
static int cmdline_count;

static int cmdline_cmp(const void *a, const void *b)
{
	const struct cmdline *ca = a;
	const struct cmdline *cb = b;

	if (ca->pid < cb->pid)
		return -1;
	if (ca->pid > cb->pid)
		return 1;

	return 0;
}

void parse_cmdlines(char *file, int size __unused)
{
	struct cmdline_list {
		struct cmdline_list	*next;
		char			*comm;
		int			pid;
	} *list = NULL, *item;
	char *line;
	char *next = NULL;
	int i;

	line = strtok_r(file, "\n", &next);
	while (line) {
		item = malloc_or_die(sizeof(*item));
		sscanf(line, "%d %as", &item->pid,
100
		       (float *)(void *)&item->comm); /* workaround gcc warning */
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
		item->next = list;
		list = item;
		line = strtok_r(NULL, "\n", &next);
		cmdline_count++;
	}

	cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);

	i = 0;
	while (list) {
		cmdlines[i].pid = list->pid;
		cmdlines[i].comm = list->comm;
		i++;
		item = list;
		list = list->next;
		free(item);
	}

	qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
}

static struct func_map {
	unsigned long long		addr;
	char				*func;
	char				*mod;
} *func_list;
static unsigned int func_count;

static int func_cmp(const void *a, const void *b)
{
	const struct func_map *fa = a;
	const struct func_map *fb = b;

	if (fa->addr < fb->addr)
		return -1;
	if (fa->addr > fb->addr)
		return 1;

	return 0;
}

void parse_proc_kallsyms(char *file, unsigned int size __unused)
{
	struct func_list {
		struct func_list	*next;
		unsigned long long	addr;
		char			*func;
		char			*mod;
	} *list = NULL, *item;
	char *line;
	char *next = NULL;
	char *addr_str;
	char ch;
	int ret;
	int i;

	line = strtok_r(file, "\n", &next);
	while (line) {
		item = malloc_or_die(sizeof(*item));
		item->mod = NULL;
		ret = sscanf(line, "%as %c %as\t[%as",
162
			     (float *)(void *)&addr_str, /* workaround gcc warning */
163
			     &ch,
164
165
			     (float *)(void *)&item->func,
			     (float *)(void *)&item->mod);
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		item->addr = strtoull(addr_str, NULL, 16);
		free(addr_str);

		/* truncate the extra ']' */
		if (item->mod)
			item->mod[strlen(item->mod) - 1] = 0;


		item->next = list;
		list = item;
		line = strtok_r(NULL, "\n", &next);
		func_count++;
	}

OGAWA Hirofumi's avatar
OGAWA Hirofumi committed
180
	func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

	i = 0;
	while (list) {
		func_list[i].func = list->func;
		func_list[i].addr = list->addr;
		func_list[i].mod = list->mod;
		i++;
		item = list;
		list = list->next;
		free(item);
	}

	qsort(func_list, func_count, sizeof(*func_list), func_cmp);

	/*
	 * Add a special record at the end.
	 */
	func_list[func_count].func = NULL;
	func_list[func_count].addr = 0;
	func_list[func_count].mod = NULL;
}

/*
 * We are searching for a record in between, not an exact
 * match.
 */
static int func_bcmp(const void *a, const void *b)
{
	const struct func_map *fa = a;
	const struct func_map *fb = b;

	if ((fa->addr == fb->addr) ||

	    (fa->addr > fb->addr &&
	     fa->addr < (fb+1)->addr))
		return 0;

	if (fa->addr < fb->addr)
		return -1;

	return 1;
}

static struct func_map *find_func(unsigned long long addr)
{
	struct func_map *func;
	struct func_map key;

	key.addr = addr;

	func = bsearch(&key, func_list, func_count, sizeof(*func_list),
		       func_bcmp);

	return func;
}

void print_funcs(void)
{
	int i;

	for (i = 0; i < (int)func_count; i++) {
		printf("%016llx %s",
		       func_list[i].addr,
		       func_list[i].func);
		if (func_list[i].mod)
			printf(" [%s]\n", func_list[i].mod);
		else
			printf("\n");
	}
}

static struct printk_map {
	unsigned long long		addr;
	char				*printk;
} *printk_list;
static unsigned int printk_count;

static int printk_cmp(const void *a, const void *b)
{
	const struct func_map *fa = a;
	const struct func_map *fb = b;

	if (fa->addr < fb->addr)
		return -1;
	if (fa->addr > fb->addr)
		return 1;

	return 0;
}

static struct printk_map *find_printk(unsigned long long addr)
{
	struct printk_map *printk;
	struct printk_map key;

	key.addr = addr;

	printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
			 printk_cmp);

	return printk;
}

void parse_ftrace_printk(char *file, unsigned int size __unused)
{
	struct printk_list {
		struct printk_list	*next;
		unsigned long long	addr;
		char			*printk;
	} *list = NULL, *item;
	char *line;
	char *next = NULL;
	char *addr_str;
	int i;

	line = strtok_r(file, "\n", &next);
	while (line) {
298
299
300
301
302
		addr_str = strsep(&line, ":");
		if (!line) {
			warning("error parsing print strings");
			break;
		}
303
304
		item = malloc_or_die(sizeof(*item));
		item->addr = strtoull(addr_str, NULL, 16);
305
		/* fmt still has a space, skip it */
306
		item->printk = strdup(line+1);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
		item->next = list;
		list = item;
		line = strtok_r(NULL, "\n", &next);
		printk_count++;
	}

	printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);

	i = 0;
	while (list) {
		printk_list[i].printk = list->printk;
		printk_list[i].addr = list->addr;
		i++;
		item = list;
		list = list->next;
		free(item);
	}

	qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
}

void print_printk(void)
{
	int i;

	for (i = 0; i < (int)printk_count; i++) {
		printf("%016llx %s\n",
		       printk_list[i].addr,
		       printk_list[i].printk);
	}
}

static struct event *alloc_event(void)
{
	struct event *event;

	event = malloc_or_die(sizeof(*event));
	memset(event, 0, sizeof(*event));

	return event;
}

enum event_type {
	EVENT_ERROR,
	EVENT_NONE,
	EVENT_SPACE,
	EVENT_NEWLINE,
	EVENT_OP,
	EVENT_DELIM,
	EVENT_ITEM,
	EVENT_DQUOTE,
	EVENT_SQUOTE,
};

static struct event *event_list;

static void add_event(struct event *event)
{
	event->next = event_list;
	event_list = event;
}

static int event_item_type(enum event_type type)
{
	switch (type) {
	case EVENT_ITEM ... EVENT_SQUOTE:
		return 1;
	case EVENT_ERROR ... EVENT_DELIM:
	default:
		return 0;
	}
}

static void free_arg(struct print_arg *arg)
{
	if (!arg)
		return;

	switch (arg->type) {
	case PRINT_ATOM:
		if (arg->atom.atom)
			free(arg->atom.atom);
		break;
	case PRINT_NULL:
	case PRINT_FIELD ... PRINT_OP:
	default:
		/* todo */
		break;
	}

	free(arg);
}

static enum event_type get_type(int ch)
{
	if (ch == '\n')
		return EVENT_NEWLINE;
	if (isspace(ch))
		return EVENT_SPACE;
	if (isalnum(ch) || ch == '_')
		return EVENT_ITEM;
	if (ch == '\'')
		return EVENT_SQUOTE;
	if (ch == '"')
		return EVENT_DQUOTE;
	if (!isprint(ch))
		return EVENT_NONE;
	if (ch == '(' || ch == ')' || ch == ',')
		return EVENT_DELIM;

	return EVENT_OP;
}

static int __read_char(void)
{
	if (input_buf_ptr >= input_buf_siz)
		return -1;

	return input_buf[input_buf_ptr++];
}

static int __peek_char(void)
{
	if (input_buf_ptr >= input_buf_siz)
		return -1;

	return input_buf[input_buf_ptr];
}

static enum event_type __read_token(char **tok)
{
	char buf[BUFSIZ];
	int ch, last_ch, quote_ch, next_ch;
	int i = 0;
	int tok_size = 0;
	enum event_type type;

	*tok = NULL;


	ch = __read_char();
	if (ch < 0)
		return EVENT_NONE;

	type = get_type(ch);
	if (type == EVENT_NONE)
		return type;

	buf[i++] = ch;

	switch (type) {
	case EVENT_NEWLINE:
	case EVENT_DELIM:
		*tok = malloc_or_die(2);
		(*tok)[0] = ch;
		(*tok)[1] = 0;
		return type;

	case EVENT_OP:
		switch (ch) {
		case '-':
			next_ch = __peek_char();
			if (next_ch == '>') {
				buf[i++] = __read_char();
				break;
			}
			/* fall through */
		case '+':
		case '|':
		case '&':
		case '>':
		case '<':
			last_ch = ch;
			ch = __peek_char();
			if (ch != last_ch)
				goto test_equal;
			buf[i++] = __read_char();
			switch (last_ch) {
			case '>':
			case '<':
				goto test_equal;
			default:
				break;
			}
			break;
		case '!':
		case '=':
			goto test_equal;
		default: /* what should we do instead? */
			break;
		}
		buf[i] = 0;
		*tok = strdup(buf);
		return type;

 test_equal:
		ch = __peek_char();
		if (ch == '=')
			buf[i++] = __read_char();
		break;

	case EVENT_DQUOTE:
	case EVENT_SQUOTE:
		/* don't keep quotes */
		i--;
		quote_ch = ch;
		last_ch = 0;
		do {
			if (i == (BUFSIZ - 1)) {
				buf[i] = 0;
				if (*tok) {
					*tok = realloc(*tok, tok_size + BUFSIZ);
					if (!*tok)
						return EVENT_NONE;
					strcat(*tok, buf);
				} else
					*tok = strdup(buf);

				if (!*tok)
					return EVENT_NONE;
				tok_size += BUFSIZ;
				i = 0;
			}
			last_ch = ch;
			ch = __read_char();
			buf[i++] = ch;
533
534
535
536
			/* the '\' '\' will cancel itself */
			if (ch == '\\' && last_ch == '\\')
				last_ch = 0;
		} while (ch != quote_ch || last_ch == '\\');
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
		/* remove the last quote */
		i--;
		goto out;

	case EVENT_ERROR ... EVENT_SPACE:
	case EVENT_ITEM:
	default:
		break;
	}

	while (get_type(__peek_char()) == type) {
		if (i == (BUFSIZ - 1)) {
			buf[i] = 0;
			if (*tok) {
				*tok = realloc(*tok, tok_size + BUFSIZ);
				if (!*tok)
					return EVENT_NONE;
				strcat(*tok, buf);
			} else
				*tok = strdup(buf);

			if (!*tok)
				return EVENT_NONE;
			tok_size += BUFSIZ;
			i = 0;
		}
		ch = __read_char();
		buf[i++] = ch;
	}

 out:
	buf[i] = 0;
	if (*tok) {
		*tok = realloc(*tok, tok_size + i);
		if (!*tok)
			return EVENT_NONE;
		strcat(*tok, buf);
	} else
		*tok = strdup(buf);
	if (!*tok)
		return EVENT_NONE;

	return type;
}

static void free_token(char *tok)
{
	if (tok)
		free(tok);
}

static enum event_type read_token(char **tok)
{
	enum event_type type;

	for (;;) {
		type = __read_token(tok);
		if (type != EVENT_SPACE)
			return type;

		free_token(*tok);
	}

	/* not reached */
	return EVENT_NONE;
}

/* no newline */
static enum event_type read_token_item(char **tok)
{
	enum event_type type;

	for (;;) {
		type = __read_token(tok);
		if (type != EVENT_SPACE && type != EVENT_NEWLINE)
			return type;

		free_token(*tok);
	}

	/* not reached */
	return EVENT_NONE;
}

static int test_type(enum event_type type, enum event_type expect)
{
	if (type != expect) {
624
		warning("Error: expected type %d but read %d",
625
626
627
628
629
630
631
		    expect, type);
		return -1;
	}
	return 0;
}

static int test_type_token(enum event_type type, char *token,
632
		    enum event_type expect, const char *expect_tok)
633
634
{
	if (type != expect) {
635
		warning("Error: expected type %d but read %d",
636
637
638
639
640
		    expect, type);
		return -1;
	}

	if (strcmp(token, expect_tok) != 0) {
641
		warning("Error: expected '%s' but read '%s'",
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
		    expect_tok, token);
		return -1;
	}
	return 0;
}

static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
{
	enum event_type type;

	if (newline_ok)
		type = read_token(tok);
	else
		type = read_token_item(tok);
	return test_type(type, expect);
}

static int read_expect_type(enum event_type expect, char **tok)
{
	return __read_expect_type(expect, tok, 1);
}

664
static int __read_expected(enum event_type expect, const char *str, int newline_ok)
665
666
667
668
669
670
671
672
673
674
675
676
677
678
{
	enum event_type type;
	char *token;
	int ret;

	if (newline_ok)
		type = read_token(&token);
	else
		type = read_token_item(&token);

	ret = test_type_token(type, token, expect, str);

	free_token(token);

679
	return ret;
680
681
}

682
static int read_expected(enum event_type expect, const char *str)
683
684
685
686
{
	return __read_expected(expect, str, 1);
}

687
static int read_expected_item(enum event_type expect, const char *str)
688
689
690
691
692
693
694
695
{
	return __read_expected(expect, str, 0);
}

static char *event_read_name(void)
{
	char *token;

696
	if (read_expected(EVENT_ITEM, "name") < 0)
697
698
		return NULL;

699
	if (read_expected(EVENT_OP, ":") < 0)
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
		return NULL;

	if (read_expect_type(EVENT_ITEM, &token) < 0)
		goto fail;

	return token;

 fail:
	free_token(token);
	return NULL;
}

static int event_read_id(void)
{
	char *token;
	int id;

717
	if (read_expected_item(EVENT_ITEM, "ID") < 0)
718
719
		return -1;

720
	if (read_expected(EVENT_OP, ":") < 0)
721
722
723
724
725
726
727
728
729
730
731
732
733
734
		return -1;

	if (read_expect_type(EVENT_ITEM, &token) < 0)
		goto fail;

	id = strtoul(token, NULL, 0);
	free_token(token);
	return id;

 fail:
	free_token(token);
	return -1;
}

735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
static int field_is_string(struct format_field *field)
{
	if ((field->flags & FIELD_IS_ARRAY) &&
	    (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
	     !strstr(field->type, "s8")))
		return 1;

	return 0;
}

static int field_is_dynamic(struct format_field *field)
{
	if (!strcmp(field->type, "__data_loc"))
		return 1;

	return 0;
}

753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
static int event_read_fields(struct event *event, struct format_field **fields)
{
	struct format_field *field = NULL;
	enum event_type type;
	char *token;
	char *last_token;
	int count = 0;

	do {
		type = read_token(&token);
		if (type == EVENT_NEWLINE) {
			free_token(token);
			return count;
		}

		count++;

770
		if (test_type_token(type, token, EVENT_ITEM, "field"))
771
772
773
774
775
776
777
778
779
780
781
782
783
784
			goto fail;
		free_token(token);

		type = read_token(&token);
		/*
		 * The ftrace fields may still use the "special" name.
		 * Just ignore it.
		 */
		if (event->flags & EVENT_FL_ISFTRACE &&
		    type == EVENT_ITEM && strcmp(token, "special") == 0) {
			free_token(token);
			type = read_token(&token);
		}

785
		if (test_type_token(type, token, EVENT_OP, ":") < 0)
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
			return -1;

		if (read_expect_type(EVENT_ITEM, &token) < 0)
			goto fail;

		last_token = token;

		field = malloc_or_die(sizeof(*field));
		memset(field, 0, sizeof(*field));

		/* read the rest of the type */
		for (;;) {
			type = read_token(&token);
			if (type == EVENT_ITEM ||
			    (type == EVENT_OP && strcmp(token, "*") == 0) ||
			    /*
			     * Some of the ftrace fields are broken and have
			     * an illegal "." in them.
			     */
			    (event->flags & EVENT_FL_ISFTRACE &&
			     type == EVENT_OP && strcmp(token, ".") == 0)) {

				if (strcmp(token, "*") == 0)
					field->flags |= FIELD_IS_POINTER;

				if (field->type) {
					field->type = realloc(field->type,
							      strlen(field->type) +
							      strlen(last_token) + 2);
					strcat(field->type, " ");
					strcat(field->type, last_token);
				} else
					field->type = last_token;
				last_token = token;
				continue;
			}

			break;
		}

		if (!field->type) {
			die("no type found");
			goto fail;
		}
		field->name = last_token;

		if (test_type(type, EVENT_OP))
			goto fail;

		if (strcmp(token, "[") == 0) {
			enum event_type last_type = type;
			char *brackets = token;
			int len;

			field->flags |= FIELD_IS_ARRAY;

			type = read_token(&token);
		        while (strcmp(token, "]") != 0) {
				if (last_type == EVENT_ITEM &&
				    type == EVENT_ITEM)
					len = 2;
				else
					len = 1;
				last_type = type;

				brackets = realloc(brackets,
						   strlen(brackets) +
						   strlen(token) + len);
				if (len == 2)
					strcat(brackets, " ");
				strcat(brackets, token);
				free_token(token);
				type = read_token(&token);
				if (type == EVENT_NONE) {
					die("failed to find token");
					goto fail;
				}
			}

			free_token(token);

			brackets = realloc(brackets, strlen(brackets) + 2);
			strcat(brackets, "]");

			/* add brackets to type */

			type = read_token(&token);
			/*
			 * If the next token is not an OP, then it is of
			 * the format: type [] item;
			 */
			if (type == EVENT_ITEM) {
				field->type = realloc(field->type,
						      strlen(field->type) +
						      strlen(field->name) +
						      strlen(brackets) + 2);
				strcat(field->type, " ");
				strcat(field->type, field->name);
				free_token(field->name);
				strcat(field->type, brackets);
				field->name = token;
				type = read_token(&token);
			} else {
				field->type = realloc(field->type,
						      strlen(field->type) +
						      strlen(brackets) + 1);
				strcat(field->type, brackets);
			}
			free(brackets);
		}

897
898
899
900
901
902
		if (field_is_string(field)) {
			field->flags |= FIELD_IS_STRING;
			if (field_is_dynamic(field))
				field->flags |= FIELD_IS_DYNAMIC;
		}

903
		if (test_type_token(type, token,  EVENT_OP, ";"))
904
905
906
			goto fail;
		free_token(token);

907
		if (read_expected(EVENT_ITEM, "offset") < 0)
908
909
			goto fail_expect;

910
		if (read_expected(EVENT_OP, ":") < 0)
911
912
913
914
915
916
917
			goto fail_expect;

		if (read_expect_type(EVENT_ITEM, &token))
			goto fail;
		field->offset = strtoul(token, NULL, 0);
		free_token(token);

918
		if (read_expected(EVENT_OP, ";") < 0)
919
920
			goto fail_expect;

921
		if (read_expected(EVENT_ITEM, "size") < 0)
922
923
			goto fail_expect;

924
		if (read_expected(EVENT_OP, ":") < 0)
925
926
927
928
929
930
931
			goto fail_expect;

		if (read_expect_type(EVENT_ITEM, &token))
			goto fail;
		field->size = strtoul(token, NULL, 0);
		free_token(token);

932
		if (read_expected(EVENT_OP, ";") < 0)
933
934
			goto fail_expect;

935
936
937
		type = read_token(&token);
		if (type != EVENT_NEWLINE) {
			/* newer versions of the kernel have a "signed" type */
938
			if (test_type_token(type, token, EVENT_ITEM, "signed"))
939
				goto fail;
940

941
			free_token(token);
942

943
			if (read_expected(EVENT_OP, ":") < 0)
944
				goto fail_expect;
945

946
947
948
			if (read_expect_type(EVENT_ITEM, &token))
				goto fail;

949
950
			if (strtoul(token, NULL, 0))
				field->flags |= FIELD_IS_SIGNED;
951
952

			free_token(token);
953
			if (read_expected(EVENT_OP, ";") < 0)
954
955
956
957
958
				goto fail_expect;

			if (read_expect_type(EVENT_NEWLINE, &token))
				goto fail;
		}
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981

		free_token(token);

		*fields = field;
		fields = &field->next;

	} while (1);

	return 0;

fail:
	free_token(token);
fail_expect:
	if (field)
		free(field);
	return -1;
}

static int event_read_format(struct event *event)
{
	char *token;
	int ret;

982
	if (read_expected_item(EVENT_ITEM, "format") < 0)
983
984
		return -1;

985
	if (read_expected(EVENT_OP, ":") < 0)
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
		return -1;

	if (read_expect_type(EVENT_NEWLINE, &token))
		goto fail;
	free_token(token);

	ret = event_read_fields(event, &event->format.common_fields);
	if (ret < 0)
		return ret;
	event->format.nr_common = ret;

	ret = event_read_fields(event, &event->format.fields);
	if (ret < 0)
		return ret;
	event->format.nr_fields = ret;

	return 0;

 fail:
	free_token(token);
	return -1;
}

enum event_type
process_arg_token(struct event *event, struct print_arg *arg,
		  char **tok, enum event_type type);

static enum event_type
process_arg(struct event *event, struct print_arg *arg, char **tok)
{
	enum event_type type;
	char *token;

	type = read_token(&token);
	*tok = token;

	return process_arg_token(event, arg, tok, type);
}

static enum event_type
process_cond(struct event *event, struct print_arg *top, char **tok)
{
	struct print_arg *arg, *left, *right;
	enum event_type type;
	char *token = NULL;

	arg = malloc_or_die(sizeof(*arg));
	memset(arg, 0, sizeof(*arg));

	left = malloc_or_die(sizeof(*left));

	right = malloc_or_die(sizeof(*right));

	arg->type = PRINT_OP;
	arg->op.left = left;
	arg->op.right = right;

	*tok = NULL;
	type = process_arg(event, left, &token);
1045
	if (test_type_token(type, token, EVENT_OP, ":"))
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
		goto out_free;

	arg->op.op = token;

	type = process_arg(event, right, &token);

	top->op.right = arg;

	*tok = token;
	return type;

out_free:
	free_token(*tok);
	free(right);
	free(left);
	free_arg(arg);
	return EVENT_ERROR;
}

1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
static enum event_type
process_array(struct event *event, struct print_arg *top, char **tok)
{
	struct print_arg *arg;
	enum event_type type;
	char *token = NULL;

	arg = malloc_or_die(sizeof(*arg));
	memset(arg, 0, sizeof(*arg));

	*tok = NULL;
	type = process_arg(event, arg, &token);
1077
	if (test_type_token(type, token, EVENT_OP, "]"))
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
		goto out_free;

	top->op.right = arg;

	free_token(token);
	type = read_token_item(&token);
	*tok = token;

	return type;

out_free:
	free_token(*tok);
	free_arg(arg);
	return EVENT_ERROR;
}

1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
static int get_op_prio(char *op)
{
	if (!op[1]) {
		switch (op[0]) {
		case '*':
		case '/':
		case '%':
			return 6;
		case '+':
		case '-':
			return 7;
			/* '>>' and '<<' are 8 */
		case '<':
		case '>':
			return 9;
			/* '==' and '!=' are 10 */
		case '&':
			return 11;
		case '^':
			return 12;
		case '|':
			return 13;
		case '?':
			return 16;
		default:
			die("unknown op '%c'", op[0]);
			return -1;
		}
	} else {
		if (strcmp(op, "++") == 0 ||
		    strcmp(op, "--") == 0) {
			return 3;
		} else if (strcmp(op, ">>") == 0 ||
			   strcmp(op, "<<") == 0) {
			return 8;
		} else if (strcmp(op, ">=") == 0 ||
			   strcmp(op, "<=") == 0) {
			return 9;
		} else if (strcmp(op, "==") == 0 ||
			   strcmp(op, "!=") == 0) {
			return 10;
		} else if (strcmp(op, "&&") == 0) {
			return 14;
		} else if (strcmp(op, "||") == 0) {
			return 15;
		} else {
			die("unknown op '%s'", op);
			return -1;
		}
	}
}

static void set_op_prio(struct print_arg *arg)
{

	/* single ops are the greatest */
	if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
		arg->op.prio = 0;
		return;
	}

	arg->op.prio = get_op_prio(arg->op.op);
}

static enum event_type
process_op(struct event *event, struct print_arg *arg, char **tok)
{
	struct print_arg *left, *right = NULL;
	enum event_type type;
	char *token;

	/* the op is passed in via tok */
	token = *tok;

	if (arg->type == PRINT_OP && !arg->op.left) {
		/* handle single op */
		if (token[1]) {
			die("bad op token %s", token);
			return EVENT_ERROR;
		}
		switch (token[0]) {
		case '!':
		case '+':
		case '-':
			break;
		default:
			die("bad op token %s", token);
			return EVENT_ERROR;
		}

		/* make an empty left */
		left = malloc_or_die(sizeof(*left));
		left->type = PRINT_NULL;
		arg->op.left = left;

		right = malloc_or_die(sizeof(*right));
		arg->op.right = right;

		type = process_arg(event, right, tok);

	} else if (strcmp(token, "?") == 0) {

		left = malloc_or_die(sizeof(*left));
		/* copy the top arg to the left */
		*left = *arg;

		arg->type = PRINT_OP;
		arg->op.op = token;
		arg->op.left = left;
		arg->op.prio = 0;

		type = process_cond(event, arg, tok);

	} else if (strcmp(token, ">>") == 0 ||
		   strcmp(token, "<<") == 0 ||
		   strcmp(token, "&") == 0 ||
		   strcmp(token, "|") == 0 ||
		   strcmp(token, "&&") == 0 ||
		   strcmp(token, "||") == 0 ||
		   strcmp(token, "-") == 0 ||
		   strcmp(token, "+") == 0 ||
		   strcmp(token, "*") == 0 ||
		   strcmp(token, "^") == 0 ||
		   strcmp(token, "/") == 0 ||
1218
1219
		   strcmp(token, "<") == 0 ||
		   strcmp(token, ">") == 0 ||
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
		   strcmp(token, "==") == 0 ||
		   strcmp(token, "!=") == 0) {

		left = malloc_or_die(sizeof(*left));

		/* copy the top arg to the left */
		*left = *arg;

		arg->type = PRINT_OP;
		arg->op.op = token;
		arg->op.left = left;

		set_op_prio(arg);

		right = malloc_or_die(sizeof(*right));

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
		type = read_token_item(&token);
		*tok = token;

		/* could just be a type pointer */
		if ((strcmp(arg->op.op, "*") == 0) &&
		    type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
			if (left->type != PRINT_ATOM)
				die("bad pointer type");
			left->atom.atom = realloc(left->atom.atom,
					    sizeof(left->atom.atom) + 3);
			strcat(left->atom.atom, " *");
			*arg = *left;
			free(arg);

			return type;
		}

		type = process_arg_token(event, right, tok, type);
1254
1255
1256

		arg->op.right = right;

1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
	} else if (strcmp(token, "[") == 0) {

		left = malloc_or_die(sizeof(*left));
		*left = *arg;

		arg->type = PRINT_OP;
		arg->op.op = token;
		arg->op.left = left;

		arg->op.prio = 0;
		type = process_array(event, arg, tok);

1269
	} else {
1270
1271
		warning("unknown op '%s'", token);
		event->flags |= EVENT_FL_FAILED;
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
		/* the arg is now the left side */
		return EVENT_NONE;
	}

	if (type == EVENT_OP) {
		int prio;

		/* higher prios need to be closer to the root */
		prio = get_op_prio(*tok);

		if (prio > arg->op.prio)
			return process_op(event, arg, tok);

		return process_op(event, right, tok);
	}

	return type;
}

static enum event_type
process_entry(struct event *event __unused, struct print_arg *arg,
	      char **tok)
{
	enum event_type type;
	char *field;
	char *token;

1299
	if (read_expected(EVENT_OP, "->") < 0)
1300
1301
1302
1303
1304
1305
1306
1307
1308
		return EVENT_ERROR;

	if (read_expect_type(EVENT_ITEM, &token) < 0)
		goto fail;
	field = token;

	arg->type = PRINT_FIELD;
	arg->field.name = field;

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
	if (is_flag_field) {
		arg->field.field = find_any_field(event, arg->field.name);
		arg->field.field->flags |= FIELD_IS_FLAG;
		is_flag_field = 0;
	} else if (is_symbolic_field) {
		arg->field.field = find_any_field(event, arg->field.name);
		arg->field.field->flags |= FIELD_IS_SYMBOLIC;
		is_symbolic_field = 0;
	}

1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
	type = read_token(&token);
	*tok = token;

	return type;

fail:
	free_token(token);
	return EVENT_ERROR;
}

static char *arg_eval (struct print_arg *arg);

static long long arg_num_eval(struct print_arg *arg)
{
	long long left, right;
	long long val = 0;

	switch (arg->type) {
	case PRINT_ATOM:
		val = strtoll(arg->atom.atom, NULL, 0);
		break;
	case PRINT_TYPE:
		val = arg_num_eval(arg->typecast.item);
		break;
	case PRINT_OP:
		switch (arg->op.op[0]) {
		case '|':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);
			if (arg->op.op[1])
				val = left || right;
			else
				val = left | right;
			break;
		case '&':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);
			if (arg->op.op[1])
				val = left && right;
			else
				val = left & right;
			break;
		case '<':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);
			switch (arg->op.op[1]) {
			case 0:
				val = left < right;
				break;
			case '<':
				val = left << right;
				break;
			case '=':
				val = left <= right;
				break;
			default:
				die("unknown op '%s'", arg->op.op);
			}
			break;
		case '>':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);
			switch (arg->op.op[1]) {
			case 0:
				val = left > right;
				break;
			case '>':
				val = left >> right;
				break;
			case '=':
				val = left >= right;
				break;
			default:
				die("unknown op '%s'", arg->op.op);
			}
			break;
		case '=':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);

			if (arg->op.op[1] != '=')
				die("unknown op '%s'", arg->op.op);

			val = left == right;
			break;
		case '!':
			left = arg_num_eval(arg->op.left);
			right = arg_num_eval(arg->op.right);

			switch (arg->op.op[1]) {
			case '=':
				val = left != right;
				break;
			default:
				die("unknown op '%s'", arg->op.op);
			}
			break;
		default:
			die("unknown op '%s'", arg->op.op);
		}
		break;

	case PRINT_NULL:
	case PRINT_FIELD ... PRINT_SYMBOL:
	case PRINT_STRING:
	default:
		die("invalid eval type %d", arg->type);

	}
	return val;
}

static char *arg_eval (struct print_arg *arg)
{
	long long val;
	static char buf[20];

	switch (arg->type) {
	case PRINT_ATOM:
		return arg->atom.atom;
	case PRINT_TYPE:
		return arg_eval(arg->typecast.item);
	case PRINT_OP:
		val = arg_num_eval(arg);
		sprintf(buf, "%lld", val);
		return buf;

	case PRINT_NULL:
	case PRINT_FIELD ... PRINT_SYMBOL:
	case PRINT_STRING:
	default:
		die("invalid eval type %d", arg->type);
		break;
	}

	return NULL;
}

static enum event_type
process_fields(struct event *event, struct print_flag_sym **list, char **tok)
{
	enum event_type type;
	struct print_arg *arg = NULL;
	struct print_flag_sym *field;
	char *token = NULL;
	char *value;

	do {
		free_token(token);
		type = read_token_item(&token);
1469
		if (test_type_token(type, token, EVENT_OP, "{"))
1470
1471
1472
1473
1474
1475
			break;

		arg = malloc_or_die(sizeof(*arg));

		free_token(token);
		type = process_arg(event, arg, &token);
1476
		if (test_type_token(type, token, EVENT_DELIM, ","))
1477
1478
1479
			goto out_free;

		field = malloc_or_die(sizeof(*field));
1480
		memset(field, 0, sizeof(*field));
1481
1482
1483
1484
1485
1486

		value = arg_eval(arg);
		field->value = strdup(value);

		free_token(token);
		type = process_arg(event, arg, &token);
1487
		if (test_type_token(type, token, EVENT_OP, "}"))
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
			goto out_free;

		value = arg_eval(arg);
		field->str = strdup(value);
		free_arg(arg);
		arg = NULL;

		*list = field;
		list = &field->next;

		free_token(token);
		type = read_token_item(&token);
	} while (type == EVENT_DELIM && strcmp(token, ",") == 0);

	*tok = token;
	return type;

out_free:
	free_arg(arg);
	free_token(token);

	return EVENT_ERROR;
}

static enum event_type
process_flags(struct event *event, struct print_arg *arg, char **tok)
{
	struct print_arg *field;
	enum event_type type;
	char *token;

	memset(arg, 0, sizeof(*arg));
	arg->type = PRINT_FLAGS;

1522
	if (read_expected_item(EVENT_DELIM, "(") < 0)
1523
1524
1525
1526
1527
		return EVENT_ERROR;

	field = malloc_or_die(sizeof(*field));

	type = process_arg(event, field, &token);
1528
	if (test_type_token(type, token, EVENT_DELIM, ","))
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
		goto out_free;

	arg->flags.field = field;

	type = read_token_item(&token);
	if (event_item_type(type)) {
		arg->flags.delim = token;
		type = read_token_item(&token);
	}

1539
	if (test_type_token(type, token, EVENT_DELIM, ","))
1540
1541
1542
		goto out_free;

	type = process_fields(event, &arg->flags.flags, &token);
1543
	if (test_type_token(type, token, EVENT_DELIM, ")"))
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
		goto out_free;

	free_token(token);
	type = read_token_item(tok);
	return type;

out_free:
	free_token(token);
	return EVENT_ERROR;
}

static enum event_type
process_symbols(struct event *event, struct print_arg *arg, char **tok)
{
	struct print_arg *field;
	enum event_type type;
	char *token;

	memset(arg, 0, sizeof(*arg));
	arg->type = PRINT_SYMBOL;

1565
	if (read_expected_item(EVENT_DELIM, "(") < 0)
1566
1567
1568
1569
1570
		return EVENT_ERROR;

	field = malloc_or_die(sizeof(*field));

	type = process_arg(event, field, &token);
1571
	if (test_type_token(type, token, EVENT_DELIM, ","))
1572
1573
1574
1575
1576
		goto out_free;

	arg->symbol.field = field;

	type = process_fields(event, &arg->symbol.symbols, &token);
1577
	if (test_type_token(type, token, EVENT_DELIM, ")"))
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
		goto out_free;

	free_token(token);
	type = read_token_item(tok);
	return type;

out_free:
	free_token(token);
	return EVENT_ERROR;
}

static enum event_type
process_paren(struct event *event, struct print_arg *arg, char **tok)
{
	struct print_arg *item_arg;
	enum event_type type;
	char *token;

	type = process_arg(event, arg, &token);

	if (type == EVENT_ERROR)
		return EVENT_ERROR;

1601
1602
	if (type == EVENT_OP)
		type = process_op(event, arg, &token);
1603

1604
1605
	if (type == EVENT_ERROR)
		return EVENT_ERROR;
1606

1607
	if (test_type_token(type, token, EVENT_DELIM, ")")) {
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
		free_token(token);
		return EVENT_ERROR;
	}

	free_token(token);
	type = read_token_item(&token);

	/*
	 * If the next token is an item or another open paren, then
	 * this was a typecast.
	 */
	if (event_item_type(type) ||
	    (type == EVENT_DELIM && strcmp(token, "(") == 0)) {

		/* make this a typecast and contine */

		/* prevous must be an atom */
		if (arg->type != PRINT_ATOM)
			die("previous needed to be PRINT_ATOM");

		item_arg = malloc_or_die(sizeof(*item_arg));

		arg->type = PRINT_TYPE;
		arg->typecast.type = arg->atom.atom;
		arg->typecast.item = item_arg;
		type = process_arg_token(event, item_arg, &token, type);

	}

	*tok = token;
	return type;
}


static enum event_type
process_str(struct event *event __unused, struct print_arg *arg, char **tok)
{
	enum event_type type;
	char *token;

1648
	if (read_expected(EVENT_DELIM, "(") < 0)
1649
1650
1651
1652
1653
1654
1655
		return EVENT_ERROR;

	if (read_expect_type(EVENT_ITEM, &token) < 0)
		goto fail;

	arg->type = PRINT_STRING;
	arg->string.string = token;
1656
	arg->string.offset = -1;
1657

1658
	if (read_expected(EVENT_DELIM, ")") < 0)
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
		return EVENT_ERROR;

	type = read_token(&token);
	*tok = token;

	return type;
fail:
	free_token(token);
	return EVENT_ERROR;
}

enum event_type
process_arg_token(struct event *event, struct print_arg *arg,
		  char **tok, enum event_type type)
{
	char *token;
	char *atom;

	token = *tok;

	switch (type) {
	case EVENT_ITEM:
		if (strcmp(token, "REC") == 0) {
			free_token(token);
			type = process_entry(event, arg, &token);
		} else if (strcmp(token, "__print_flags") == 0) {
			free_token(token);
1686
			is_flag_field = 1;
1687
1688
1689
			type = process_flags(event, arg, &token);
		} else if (strcmp(token, "__print_symbolic") == 0) {
			free_token(token);
1690
			is_symbolic_field = 1;
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
			type = process_symbols(event, arg, &token);
		} else if (strcmp(token, "__get_str") == 0) {
			free_token(token);
			type = process_str(event, arg, &token);
		} else {
			atom = token;
			/* test the next token */
			type = read_token_item(&token);

			/* atoms can be more than one token long */
			while (type == EVENT_ITEM) {
				atom = realloc(atom, strlen(atom) + strlen(token) + 2);
				strcat(atom, " ");
				strcat(atom, token);
				free_token(token);
				type = read_token_item(&token);
			}

			/* todo, test for function */

			arg->type = PRINT_ATOM;
			arg->atom.atom = atom;
		}
		break;
	case EVENT_DQUOTE:
	case EVENT_SQUOTE:
		arg->type = PRINT_ATOM;
		arg->atom.atom = token;
		type = read_token_item(&token);
		break;
	case EVENT_DELIM:
		if (strcmp(token, "(") == 0) {
			free_token(token);
			type = process_paren(event, arg, &token);
			break;
		}
	case EVENT_OP:
		/* handle single ops */
		arg->type = PRINT_OP;
		arg->op.op = token;
		arg->op.left = NULL;
		type = process_op(event, arg, &token);

		break;

	case EVENT_ERROR ... EVENT_NEWLINE:
	default:
		die("unexpected type %d", type);
	}
	*tok = token;

	return type;
}

static int event_read_print_args(struct event *event, struct print_arg **list)
{
1747
	enum event_type type = EVENT_ERROR;
1748
1749
1750
1751
1752
	struct print_arg *arg;
	char *token;
	int args = 0;

	do {
1753
1754
1755
1756
1757
1758
		if (type == EVENT_NEWLINE) {
			free_token(token);
			type = read_token_item(&token);
			continue;
		}

1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
		arg = malloc_or_die(sizeof(*arg));
		memset(arg, 0, sizeof(*arg));

		type = process_arg(event, arg, &token);

		if (type == EVENT_ERROR) {
			free_arg(arg);
			return -1;
		}

		*list = arg;
		args++;

		if (type == EVENT_OP) {
			type = process_op(event, arg, &token);
			list = &arg->next;
			continue;
		}

		if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
			free_token(token);
			*list = arg;
			list = &arg->next;
			continue;
		}
		break;
	} while (type != EVENT_NONE);

	if (type != EVENT_NONE)
		free_token(token);

	return args;
}

static int event_read_print(struct event *event)
{
	enum event_type type;
	char *token;
	int ret;

1799
	if (read_expected_item(EVENT_ITEM, "print") < 0)
1800
1801
		return -1;

1802
	if (read_expected(EVENT_ITEM, "fmt") < 0)
1803
1804
		return -1;

1805
	if (read_expected(EVENT_OP, ":") < 0)
1806
1807
1808
1809
1810
		return -1;

	if (read_expect_type(EVENT_DQUOTE, &token) < 0)
		goto fail;

1811
 concat:
1812
1813
1814
1815
1816
1817
1818
1819
1820
	event->print_fmt.format = token;
	event->print_fmt.args = NULL;

	/* ok to have no arg */
	type = read_token_item(&token);

	if (type == EVENT_NONE)
		return 0;

1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
	/* Handle concatination of print lines */
	if (type == EVENT_DQUOTE) {
		char *cat;

		cat = malloc_or_die(strlen(event->print_fmt.format) +
				    strlen(token) + 1);
		strcpy(cat, event->print_fmt.format);
		strcat(cat, token);
		free_token(token);
		free_token(event->print_fmt.format);
		event->print_fmt.format = NULL;
		token = cat;
		goto concat;
	}
1835
1836

	if (test_type_token(type, token, EVENT_DELIM, ","))
1837
1838
1839
1840
1841
1842
1843
1844
		goto fail;

	free_token(token);

	ret = event_read_print_args(event, &event->print_fmt.args);
	if (ret < 0)
		return -1;

1845
	return ret;
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890

 fail:
	free_token(token);
	return -1;
}

static struct format_field *
find_common_field(struct event *event, const char *name)
{
	struct format_field *format;

	for (format = event->format.common_fields;
	     format; format = format->next) {
		if (strcmp(format->name, name) == 0)
			break;
	}

	return format;
}

static struct format_field *
find_field(struct event *event, const char *name)
{
	struct format_field *format;

	for (format = event->format.fields;
	     format; format = format->next) {
		if (strcmp(format->name, name) == 0)
			break;
	}

	return format;
}

static struct format_field *
find_any_field(struct event *event, const char *name)
{
	struct format_field *format;

	format = find_common_field(event, name);
	if (format)
		return format;
	return find_field(event, name);
}

1891
unsigned long long read_size(void *ptr, int size)
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
{
	switch (size) {
	case 1:
		return *(unsigned char *)ptr;
	case 2:
		return data2host2(ptr);
	case 4:
		return data2host4(ptr);
	case 8:
		return data2host8(ptr);
	default:
		/* BUG! */
		return 0;
	}
}

1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
unsigned long long
raw_field_value(struct event *event, const char *name, void *data)
{
	struct format_field *field;

	field = find_any_field(event, name);
	if (!field)
		return 0ULL;

	return read_size(data + field->offset, field->size);
}

void *raw_field_ptr(struct event *event, const char *name, void *data)
{
	struct format_field *field;

	field = find_any_field(event, name);
	if (!field)
		return NULL;

1928
	if (field->flags & FIELD_IS_DYNAMIC) {
1929
1930
1931
1932
1933
1934
1935
1936
		int offset;

		offset = *(int *)(data + field->offset);
		offset &= 0xffff;

		return data + offset;
	}

1937
1938
1939
	return data + field->offset;
}

1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
static int get_common_info(const char *type, int *offset, int *size)
{
	struct event *event;
	struct format_field *field;

	/*
	 * All events should have the same common elements.
	 * Pick any event to find where the type is;
	 */
	if (!event_list)
		die("no event_list!");

	event = event_list;
	field = find_common_field(event, type);
	if (!field)
		die("field '%s' not found", type);

	*offset = field->offset;
	*size = field->size;

	return 0;
}

1963
static int __parse_common(void *data, int *size, int *offset,
1964
			  const char *name)
1965
1966
1967
{
	int ret;

1968
1969
	if (!*size) {
		ret = get_common_info(name, offset, size);
1970
1971
1972
		if (ret < 0)
			return ret;
	}
1973
1974
1975
1976
1977
1978
1979
1980
1981
	return read_size(data + *offset, *size);
}

int trace_parse_common_type(void *data)
{
	static int type_offset;
	static int type_size;

	return __parse_common(data, &type_size, &type_offset,
1982
			      "common_type");
1983
1984
}

1985
int trace_parse_common_pid(void *data)
1986
1987
1988
{
	static int pid_offset;
	static int pid_size;
1989
1990

	return __parse_common(data, &pid_size, &pid_offset,
1991
			      "common_pid");
1992
1993
}

1994
int parse_common_pc(void *data)
1995
1996
1997
1998
1999
{
	static int pc_offset;
	static int pc_size;

	return __parse_common(data, &pc_size, &pc_offset,
2000
			      "common_preempt_count");
2001
2002
}

2003
int parse_common_flags(void *data)
2004
2005
2006
2007
2008
{
	static int flags_offset;
	static int flags_size;

	return __parse_common(data, &flags_size, &flags_offset,
2009
			      "common_flags");
2010
2011
}

2012
int parse_common_lock_depth(void *data)
2013
2014
2015
{
	static int ld_offset;
	static int ld_size;
2016
2017
	int ret;

2018
	ret = __parse_common(data, &ld_size, &ld_offset,
2019
			     "common_lock_depth");
2020
2021
	if (ret < 0)
		return -1;
2022

2023
	return ret;
2024
2025
}

Ingo Molnar's avatar
Ingo Molnar committed
2026
struct event *trace_find_event(int id)
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
{
	struct event *event;

	for (event = event_list; event; event = event->next) {
		if (event->id == id)
			break;
	}
	return event;
}

2037
2038
2039
2040
2041
2042
2043
2044
struct event *trace_find_next_event(struct event *event)
{
	if (!event)
		return event_list;

	return event->next;
}

2045
2046
2047
2048
2049
static unsigned long long eval_num_arg(void *data, int size,
				   struct event *event, struct print_arg *arg)
{
	unsigned long long val = 0;
	unsigned long long left, right;
2050
	struct print_arg *larg;
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076

	switch (arg->type) {
	case PRINT_NULL:
		/* ?? */
		return 0;
	case PRINT_ATOM:
		return strtoull(arg->atom.atom, NULL, 0);
	case PRINT_FIELD:
		if (!arg->field.field) {
			arg->field.field = find_any_field(event, arg->field.name);
			if (!arg->field.field)
				die("field %s not found", arg->field.name);
		}
		/* must be a number */
		val = read_size(data + arg->field.field->offset,
				arg->field.field->size);
		break;
	case PRINT_FLAGS:
	case PRINT_SYMBOL:
		break;
	case PRINT_TYPE:
		return eval_num_arg(data, size, event, arg->typecast.item);
	case PRINT_STRING:
		return 0;
		break;
	case PRINT_OP:
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
		if (strcmp(arg->op.op, "[") == 0) {
			/*
			 * Arrays are special, since we don't want
			 * to read the arg as is.
			 */
			if (arg->op.left->type != PRINT_FIELD)
				goto default_op; /* oops, all bets off */
			larg = arg->op.left;
			if (!larg->field.field) {
				larg->field.field =
					find_any_field(event, larg->field.name);
				if (!larg->field.field)
					die("field %s not found", larg->field.name);
			}
			right = eval_num_arg(data, size, event, arg->op.right);
			val = read_size(data + larg->field.field->offset +
					right * long_size, long_size);
			break;
		}
 default_op:
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
		left = eval_num_arg(data, size, event, arg->op.left);
		right = eval_num_arg(data, size, event, arg->op.right);
		switch (arg->op.op[0]) {
		case '|':
			if (arg->op.op[1])
				val = left || right;
			else
				val = left | right;
			break;
		case '&':