tools lib traceevent: Fix a possible memory leak
[deliverable/linux.git] / tools / lib / traceevent / event-parse.c
CommitLineData
f7d82350
SR
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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 *
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by
23 * - Copyright (C) 2009 Frederic Weisbecker,
24 * Frederic Weisbecker gave his permission to relicense the code to
25 * the Lesser General Public License.
26 */
27#define _GNU_SOURCE
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <ctype.h>
33#include <errno.h>
34
35#include "event-parse.h"
668fe01f 36#include "event-utils.h"
f7d82350
SR
37
38static const char *input_buf;
39static unsigned long long input_buf_ptr;
40static unsigned long long input_buf_siz;
41
5205aec9
TZ
42static int is_flag_field;
43static int is_symbolic_field;
44
f7d82350
SR
45static int show_warning = 1;
46
47#define do_warning(fmt, ...) \
48 do { \
49 if (show_warning) \
50 warning(fmt, ##__VA_ARGS__); \
51 } while (0)
52
53static void init_input_buf(const char *buf, unsigned long long size)
54{
55 input_buf = buf;
56 input_buf_siz = size;
57 input_buf_ptr = 0;
58}
59
60const char *pevent_get_input_buf(void)
61{
62 return input_buf;
63}
64
65unsigned long long pevent_get_input_buf_ptr(void)
66{
67 return input_buf_ptr;
68}
69
70struct event_handler {
71 struct event_handler *next;
72 int id;
73 const char *sys_name;
74 const char *event_name;
75 pevent_event_handler_func func;
76 void *context;
77};
78
79struct pevent_func_params {
80 struct pevent_func_params *next;
81 enum pevent_func_arg_type type;
82};
83
84struct pevent_function_handler {
85 struct pevent_function_handler *next;
86 enum pevent_func_arg_type ret_type;
87 char *name;
88 pevent_func_handler func;
89 struct pevent_func_params *params;
90 int nr_args;
91};
92
93static unsigned long long
94process_defined_func(struct trace_seq *s, void *data, int size,
95 struct event_format *event, struct print_arg *arg);
96
97static void free_func_handle(struct pevent_function_handler *func);
98
99/**
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
103 *
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
106 */
107void pevent_buffer_init(const char *buf, unsigned long long size)
108{
109 init_input_buf(buf, size);
110}
111
112void breakpoint(void)
113{
114 static int x;
115 x++;
116}
117
118struct print_arg *alloc_arg(void)
119{
120 struct print_arg *arg;
121
122 arg = malloc_or_die(sizeof(*arg));
123 if (!arg)
124 return NULL;
125 memset(arg, 0, sizeof(*arg));
126
127 return arg;
128}
129
130struct cmdline {
131 char *comm;
132 int pid;
133};
134
135static int cmdline_cmp(const void *a, const void *b)
136{
137 const struct cmdline *ca = a;
138 const struct cmdline *cb = b;
139
140 if (ca->pid < cb->pid)
141 return -1;
142 if (ca->pid > cb->pid)
143 return 1;
144
145 return 0;
146}
147
148struct cmdline_list {
149 struct cmdline_list *next;
150 char *comm;
151 int pid;
152};
153
154static int cmdline_init(struct pevent *pevent)
155{
156 struct cmdline_list *cmdlist = pevent->cmdlist;
157 struct cmdline_list *item;
158 struct cmdline *cmdlines;
159 int i;
160
161 cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
162
163 i = 0;
164 while (cmdlist) {
165 cmdlines[i].pid = cmdlist->pid;
166 cmdlines[i].comm = cmdlist->comm;
167 i++;
168 item = cmdlist;
169 cmdlist = cmdlist->next;
170 free(item);
171 }
172
173 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
174
175 pevent->cmdlines = cmdlines;
176 pevent->cmdlist = NULL;
177
178 return 0;
179}
180
181static char *find_cmdline(struct pevent *pevent, int pid)
182{
183 const struct cmdline *comm;
184 struct cmdline key;
185
186 if (!pid)
187 return "<idle>";
188
189 if (!pevent->cmdlines)
190 cmdline_init(pevent);
191
192 key.pid = pid;
193
194 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
195 sizeof(*pevent->cmdlines), cmdline_cmp);
196
197 if (comm)
198 return comm->comm;
199 return "<...>";
200}
201
202/**
203 * pevent_pid_is_registered - return if a pid has a cmdline registered
204 * @pevent: handle for the pevent
205 * @pid: The pid to check if it has a cmdline registered with.
206 *
207 * Returns 1 if the pid has a cmdline mapped to it
208 * 0 otherwise.
209 */
210int pevent_pid_is_registered(struct pevent *pevent, int pid)
211{
212 const struct cmdline *comm;
213 struct cmdline key;
214
215 if (!pid)
216 return 1;
217
218 if (!pevent->cmdlines)
219 cmdline_init(pevent);
220
221 key.pid = pid;
222
223 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
224 sizeof(*pevent->cmdlines), cmdline_cmp);
225
226 if (comm)
227 return 1;
228 return 0;
229}
230
231/*
232 * If the command lines have been converted to an array, then
233 * we must add this pid. This is much slower than when cmdlines
234 * are added before the array is initialized.
235 */
236static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
237{
238 struct cmdline *cmdlines = pevent->cmdlines;
239 const struct cmdline *cmdline;
240 struct cmdline key;
241
242 if (!pid)
243 return 0;
244
245 /* avoid duplicates */
246 key.pid = pid;
247
248 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
249 sizeof(*pevent->cmdlines), cmdline_cmp);
250 if (cmdline) {
251 errno = EEXIST;
252 return -1;
253 }
254
255 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
256 if (!cmdlines) {
257 errno = ENOMEM;
258 return -1;
259 }
260
261 cmdlines[pevent->cmdline_count].pid = pid;
262 cmdlines[pevent->cmdline_count].comm = strdup(comm);
263 if (!cmdlines[pevent->cmdline_count].comm)
264 die("malloc comm");
265
266 if (cmdlines[pevent->cmdline_count].comm)
267 pevent->cmdline_count++;
268
269 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
270 pevent->cmdlines = cmdlines;
271
272 return 0;
273}
274
275/**
276 * pevent_register_comm - register a pid / comm mapping
277 * @pevent: handle for the pevent
278 * @comm: the command line to register
279 * @pid: the pid to map the command line to
280 *
281 * This adds a mapping to search for command line names with
282 * a given pid. The comm is duplicated.
283 */
284int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
285{
286 struct cmdline_list *item;
287
288 if (pevent->cmdlines)
289 return add_new_comm(pevent, comm, pid);
290
291 item = malloc_or_die(sizeof(*item));
292 item->comm = strdup(comm);
293 if (!item->comm)
294 die("malloc comm");
295 item->pid = pid;
296 item->next = pevent->cmdlist;
297
298 pevent->cmdlist = item;
299 pevent->cmdline_count++;
300
301 return 0;
302}
303
304struct func_map {
305 unsigned long long addr;
306 char *func;
307 char *mod;
308};
309
310struct func_list {
311 struct func_list *next;
312 unsigned long long addr;
313 char *func;
314 char *mod;
315};
316
317static int func_cmp(const void *a, const void *b)
318{
319 const struct func_map *fa = a;
320 const struct func_map *fb = b;
321
322 if (fa->addr < fb->addr)
323 return -1;
324 if (fa->addr > fb->addr)
325 return 1;
326
327 return 0;
328}
329
330/*
331 * We are searching for a record in between, not an exact
332 * match.
333 */
334static int func_bcmp(const void *a, const void *b)
335{
336 const struct func_map *fa = a;
337 const struct func_map *fb = b;
338
339 if ((fa->addr == fb->addr) ||
340
341 (fa->addr > fb->addr &&
342 fa->addr < (fb+1)->addr))
343 return 0;
344
345 if (fa->addr < fb->addr)
346 return -1;
347
348 return 1;
349}
350
351static int func_map_init(struct pevent *pevent)
352{
353 struct func_list *funclist;
354 struct func_list *item;
355 struct func_map *func_map;
356 int i;
357
358 func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
359 funclist = pevent->funclist;
360
361 i = 0;
362 while (funclist) {
363 func_map[i].func = funclist->func;
364 func_map[i].addr = funclist->addr;
365 func_map[i].mod = funclist->mod;
366 i++;
367 item = funclist;
368 funclist = funclist->next;
369 free(item);
370 }
371
372 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
373
374 /*
375 * Add a special record at the end.
376 */
377 func_map[pevent->func_count].func = NULL;
378 func_map[pevent->func_count].addr = 0;
379 func_map[pevent->func_count].mod = NULL;
380
381 pevent->func_map = func_map;
382 pevent->funclist = NULL;
383
384 return 0;
385}
386
387static struct func_map *
388find_func(struct pevent *pevent, unsigned long long addr)
389{
390 struct func_map *func;
391 struct func_map key;
392
393 if (!pevent->func_map)
394 func_map_init(pevent);
395
396 key.addr = addr;
397
398 func = bsearch(&key, pevent->func_map, pevent->func_count,
399 sizeof(*pevent->func_map), func_bcmp);
400
401 return func;
402}
403
404/**
405 * pevent_find_function - find a function by a given address
406 * @pevent: handle for the pevent
407 * @addr: the address to find the function with
408 *
409 * Returns a pointer to the function stored that has the given
410 * address. Note, the address does not have to be exact, it
411 * will select the function that would contain the address.
412 */
413const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
414{
415 struct func_map *map;
416
417 map = find_func(pevent, addr);
418 if (!map)
419 return NULL;
420
421 return map->func;
422}
423
424/**
425 * pevent_find_function_address - find a function address by a given address
426 * @pevent: handle for the pevent
427 * @addr: the address to find the function with
428 *
429 * Returns the address the function starts at. This can be used in
430 * conjunction with pevent_find_function to print both the function
431 * name and the function offset.
432 */
433unsigned long long
434pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
435{
436 struct func_map *map;
437
438 map = find_func(pevent, addr);
439 if (!map)
440 return 0;
441
442 return map->addr;
443}
444
445/**
446 * pevent_register_function - register a function with a given address
447 * @pevent: handle for the pevent
448 * @function: the function name to register
449 * @addr: the address the function starts at
450 * @mod: the kernel module the function may be in (NULL for none)
451 *
452 * This registers a function name with an address and module.
453 * The @func passed in is duplicated.
454 */
455int pevent_register_function(struct pevent *pevent, char *func,
456 unsigned long long addr, char *mod)
457{
458 struct func_list *item;
459
460 item = malloc_or_die(sizeof(*item));
461
462 item->next = pevent->funclist;
463 item->func = strdup(func);
464 if (mod)
465 item->mod = strdup(mod);
466 else
467 item->mod = NULL;
468 item->addr = addr;
469
470 pevent->funclist = item;
471
472 pevent->func_count++;
473
474 return 0;
475}
476
477/**
478 * pevent_print_funcs - print out the stored functions
479 * @pevent: handle for the pevent
480 *
481 * This prints out the stored functions.
482 */
483void pevent_print_funcs(struct pevent *pevent)
484{
485 int i;
486
487 if (!pevent->func_map)
488 func_map_init(pevent);
489
490 for (i = 0; i < (int)pevent->func_count; i++) {
491 printf("%016llx %s",
492 pevent->func_map[i].addr,
493 pevent->func_map[i].func);
494 if (pevent->func_map[i].mod)
495 printf(" [%s]\n", pevent->func_map[i].mod);
496 else
497 printf("\n");
498 }
499}
500
501struct printk_map {
502 unsigned long long addr;
503 char *printk;
504};
505
506struct printk_list {
507 struct printk_list *next;
508 unsigned long long addr;
509 char *printk;
510};
511
512static int printk_cmp(const void *a, const void *b)
513{
514 const struct func_map *fa = a;
515 const struct func_map *fb = b;
516
517 if (fa->addr < fb->addr)
518 return -1;
519 if (fa->addr > fb->addr)
520 return 1;
521
522 return 0;
523}
524
525static void printk_map_init(struct pevent *pevent)
526{
527 struct printk_list *printklist;
528 struct printk_list *item;
529 struct printk_map *printk_map;
530 int i;
531
532 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
533
534 printklist = pevent->printklist;
535
536 i = 0;
537 while (printklist) {
538 printk_map[i].printk = printklist->printk;
539 printk_map[i].addr = printklist->addr;
540 i++;
541 item = printklist;
542 printklist = printklist->next;
543 free(item);
544 }
545
546 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
547
548 pevent->printk_map = printk_map;
549 pevent->printklist = NULL;
550}
551
552static struct printk_map *
553find_printk(struct pevent *pevent, unsigned long long addr)
554{
555 struct printk_map *printk;
556 struct printk_map key;
557
558 if (!pevent->printk_map)
559 printk_map_init(pevent);
560
561 key.addr = addr;
562
563 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
564 sizeof(*pevent->printk_map), printk_cmp);
565
566 return printk;
567}
568
569/**
570 * pevent_register_print_string - register a string by its address
571 * @pevent: handle for the pevent
572 * @fmt: the string format to register
573 * @addr: the address the string was located at
574 *
575 * This registers a string by the address it was stored in the kernel.
576 * The @fmt passed in is duplicated.
577 */
578int pevent_register_print_string(struct pevent *pevent, char *fmt,
579 unsigned long long addr)
580{
581 struct printk_list *item;
582
583 item = malloc_or_die(sizeof(*item));
584
585 item->next = pevent->printklist;
586 pevent->printklist = item;
587 item->printk = strdup(fmt);
588 item->addr = addr;
589
590 pevent->printk_count++;
591
592 return 0;
593}
594
595/**
596 * pevent_print_printk - print out the stored strings
597 * @pevent: handle for the pevent
598 *
599 * This prints the string formats that were stored.
600 */
601void pevent_print_printk(struct pevent *pevent)
602{
603 int i;
604
605 if (!pevent->printk_map)
606 printk_map_init(pevent);
607
608 for (i = 0; i < (int)pevent->printk_count; i++) {
609 printf("%016llx %s\n",
610 pevent->printk_map[i].addr,
611 pevent->printk_map[i].printk);
612 }
613}
614
615static struct event_format *alloc_event(void)
616{
617 struct event_format *event;
618
619 event = malloc_or_die(sizeof(*event));
620 memset(event, 0, sizeof(*event));
621
622 return event;
623}
624
625static void add_event(struct pevent *pevent, struct event_format *event)
626{
627 int i;
628
629 if (!pevent->events)
630 pevent->events = malloc_or_die(sizeof(event));
631 else
632 pevent->events =
633 realloc(pevent->events, sizeof(event) *
634 (pevent->nr_events + 1));
635 if (!pevent->events)
636 die("Can not allocate events");
637
638 for (i = 0; i < pevent->nr_events; i++) {
639 if (pevent->events[i]->id > event->id)
640 break;
641 }
642 if (i < pevent->nr_events)
643 memmove(&pevent->events[i + 1],
644 &pevent->events[i],
645 sizeof(event) * (pevent->nr_events - i));
646
647 pevent->events[i] = event;
648 pevent->nr_events++;
649
650 event->pevent = pevent;
651}
652
653static int event_item_type(enum event_type type)
654{
655 switch (type) {
656 case EVENT_ITEM ... EVENT_SQUOTE:
657 return 1;
658 case EVENT_ERROR ... EVENT_DELIM:
659 default:
660 return 0;
661 }
662}
663
664static void free_flag_sym(struct print_flag_sym *fsym)
665{
666 struct print_flag_sym *next;
667
668 while (fsym) {
669 next = fsym->next;
670 free(fsym->value);
671 free(fsym->str);
672 free(fsym);
673 fsym = next;
674 }
675}
676
677static void free_arg(struct print_arg *arg)
678{
679 struct print_arg *farg;
680
681 if (!arg)
682 return;
683
684 switch (arg->type) {
685 case PRINT_ATOM:
686 free(arg->atom.atom);
687 break;
688 case PRINT_FIELD:
689 free(arg->field.name);
690 break;
691 case PRINT_FLAGS:
692 free_arg(arg->flags.field);
693 free(arg->flags.delim);
694 free_flag_sym(arg->flags.flags);
695 break;
696 case PRINT_SYMBOL:
697 free_arg(arg->symbol.field);
698 free_flag_sym(arg->symbol.symbols);
699 break;
700 case PRINT_TYPE:
701 free(arg->typecast.type);
702 free_arg(arg->typecast.item);
703 break;
704 case PRINT_STRING:
705 case PRINT_BSTRING:
706 free(arg->string.string);
707 break;
708 case PRINT_DYNAMIC_ARRAY:
709 free(arg->dynarray.index);
710 break;
711 case PRINT_OP:
712 free(arg->op.op);
713 free_arg(arg->op.left);
714 free_arg(arg->op.right);
715 break;
716 case PRINT_FUNC:
717 while (arg->func.args) {
718 farg = arg->func.args;
719 arg->func.args = farg->next;
720 free_arg(farg);
721 }
722 break;
723
724 case PRINT_NULL:
725 default:
726 break;
727 }
728
729 free(arg);
730}
731
732static enum event_type get_type(int ch)
733{
734 if (ch == '\n')
735 return EVENT_NEWLINE;
736 if (isspace(ch))
737 return EVENT_SPACE;
738 if (isalnum(ch) || ch == '_')
739 return EVENT_ITEM;
740 if (ch == '\'')
741 return EVENT_SQUOTE;
742 if (ch == '"')
743 return EVENT_DQUOTE;
744 if (!isprint(ch))
745 return EVENT_NONE;
746 if (ch == '(' || ch == ')' || ch == ',')
747 return EVENT_DELIM;
748
749 return EVENT_OP;
750}
751
752static int __read_char(void)
753{
754 if (input_buf_ptr >= input_buf_siz)
755 return -1;
756
757 return input_buf[input_buf_ptr++];
758}
759
760static int __peek_char(void)
761{
762 if (input_buf_ptr >= input_buf_siz)
763 return -1;
764
765 return input_buf[input_buf_ptr];
766}
767
768/**
769 * pevent_peek_char - peek at the next character that will be read
770 *
771 * Returns the next character read, or -1 if end of buffer.
772 */
773int pevent_peek_char(void)
774{
775 return __peek_char();
776}
777
778static enum event_type force_token(const char *str, char **tok);
779
780static enum event_type __read_token(char **tok)
781{
782 char buf[BUFSIZ];
783 int ch, last_ch, quote_ch, next_ch;
784 int i = 0;
785 int tok_size = 0;
786 enum event_type type;
787
788 *tok = NULL;
789
790
791 ch = __read_char();
792 if (ch < 0)
793 return EVENT_NONE;
794
795 type = get_type(ch);
796 if (type == EVENT_NONE)
797 return type;
798
799 buf[i++] = ch;
800
801 switch (type) {
802 case EVENT_NEWLINE:
803 case EVENT_DELIM:
804 *tok = malloc_or_die(2);
805 (*tok)[0] = ch;
806 (*tok)[1] = 0;
807 return type;
808
809 case EVENT_OP:
810 switch (ch) {
811 case '-':
812 next_ch = __peek_char();
813 if (next_ch == '>') {
814 buf[i++] = __read_char();
815 break;
816 }
817 /* fall through */
818 case '+':
819 case '|':
820 case '&':
821 case '>':
822 case '<':
823 last_ch = ch;
824 ch = __peek_char();
825 if (ch != last_ch)
826 goto test_equal;
827 buf[i++] = __read_char();
828 switch (last_ch) {
829 case '>':
830 case '<':
831 goto test_equal;
832 default:
833 break;
834 }
835 break;
836 case '!':
837 case '=':
838 goto test_equal;
839 default: /* what should we do instead? */
840 break;
841 }
842 buf[i] = 0;
843 *tok = strdup(buf);
844 return type;
845
846 test_equal:
847 ch = __peek_char();
848 if (ch == '=')
849 buf[i++] = __read_char();
850 goto out;
851
852 case EVENT_DQUOTE:
853 case EVENT_SQUOTE:
854 /* don't keep quotes */
855 i--;
856 quote_ch = ch;
857 last_ch = 0;
858 concat:
859 do {
860 if (i == (BUFSIZ - 1)) {
861 buf[i] = 0;
862 if (*tok) {
863 *tok = realloc(*tok, tok_size + BUFSIZ);
864 if (!*tok)
865 return EVENT_NONE;
866 strcat(*tok, buf);
867 } else
868 *tok = strdup(buf);
869
870 if (!*tok)
871 return EVENT_NONE;
872 tok_size += BUFSIZ;
873 i = 0;
874 }
875 last_ch = ch;
876 ch = __read_char();
877 buf[i++] = ch;
878 /* the '\' '\' will cancel itself */
879 if (ch == '\\' && last_ch == '\\')
880 last_ch = 0;
881 } while (ch != quote_ch || last_ch == '\\');
882 /* remove the last quote */
883 i--;
884
885 /*
886 * For strings (double quotes) check the next token.
887 * If it is another string, concatinate the two.
888 */
889 if (type == EVENT_DQUOTE) {
890 unsigned long long save_input_buf_ptr = input_buf_ptr;
891
892 do {
893 ch = __read_char();
894 } while (isspace(ch));
895 if (ch == '"')
896 goto concat;
897 input_buf_ptr = save_input_buf_ptr;
898 }
899
900 goto out;
901
902 case EVENT_ERROR ... EVENT_SPACE:
903 case EVENT_ITEM:
904 default:
905 break;
906 }
907
908 while (get_type(__peek_char()) == type) {
909 if (i == (BUFSIZ - 1)) {
910 buf[i] = 0;
911 if (*tok) {
912 *tok = realloc(*tok, tok_size + BUFSIZ);
913 if (!*tok)
914 return EVENT_NONE;
915 strcat(*tok, buf);
916 } else
917 *tok = strdup(buf);
918
919 if (!*tok)
920 return EVENT_NONE;
921 tok_size += BUFSIZ;
922 i = 0;
923 }
924 ch = __read_char();
925 buf[i++] = ch;
926 }
927
928 out:
929 buf[i] = 0;
930 if (*tok) {
931 *tok = realloc(*tok, tok_size + i);
932 if (!*tok)
933 return EVENT_NONE;
934 strcat(*tok, buf);
935 } else
936 *tok = strdup(buf);
937 if (!*tok)
938 return EVENT_NONE;
939
940 if (type == EVENT_ITEM) {
941 /*
942 * Older versions of the kernel has a bug that
943 * creates invalid symbols and will break the mac80211
944 * parsing. This is a work around to that bug.
945 *
946 * See Linux kernel commit:
947 * 811cb50baf63461ce0bdb234927046131fc7fa8b
948 */
949 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
950 free(*tok);
951 *tok = NULL;
952 return force_token("\"\%s\" ", tok);
953 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
954 free(*tok);
955 *tok = NULL;
956 return force_token("\" sta:%pM\" ", tok);
957 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
958 free(*tok);
959 *tok = NULL;
960 return force_token("\" vif:%p(%d)\" ", tok);
961 }
962 }
963
964 return type;
965}
966
967static enum event_type force_token(const char *str, char **tok)
968{
969 const char *save_input_buf;
970 unsigned long long save_input_buf_ptr;
971 unsigned long long save_input_buf_siz;
972 enum event_type type;
973
974 /* save off the current input pointers */
975 save_input_buf = input_buf;
976 save_input_buf_ptr = input_buf_ptr;
977 save_input_buf_siz = input_buf_siz;
978
979 init_input_buf(str, strlen(str));
980
981 type = __read_token(tok);
982
983 /* reset back to original token */
984 input_buf = save_input_buf;
985 input_buf_ptr = save_input_buf_ptr;
986 input_buf_siz = save_input_buf_siz;
987
988 return type;
989}
990
991static void free_token(char *tok)
992{
993 if (tok)
994 free(tok);
995}
996
997static enum event_type read_token(char **tok)
998{
999 enum event_type type;
1000
1001 for (;;) {
1002 type = __read_token(tok);
1003 if (type != EVENT_SPACE)
1004 return type;
1005
1006 free_token(*tok);
1007 }
1008
1009 /* not reached */
1010 *tok = NULL;
1011 return EVENT_NONE;
1012}
1013
1014/**
1015 * pevent_read_token - access to utilites to use the pevent parser
1016 * @tok: The token to return
1017 *
1018 * This will parse tokens from the string given by
1019 * pevent_init_data().
1020 *
1021 * Returns the token type.
1022 */
1023enum event_type pevent_read_token(char **tok)
1024{
1025 return read_token(tok);
1026}
1027
1028/**
1029 * pevent_free_token - free a token returned by pevent_read_token
1030 * @token: the token to free
1031 */
1032void pevent_free_token(char *token)
1033{
1034 free_token(token);
1035}
1036
1037/* no newline */
1038static enum event_type read_token_item(char **tok)
1039{
1040 enum event_type type;
1041
1042 for (;;) {
1043 type = __read_token(tok);
1044 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1045 return type;
1046 free_token(*tok);
1047 *tok = NULL;
1048 }
1049
1050 /* not reached */
1051 *tok = NULL;
1052 return EVENT_NONE;
1053}
1054
1055static int test_type(enum event_type type, enum event_type expect)
1056{
1057 if (type != expect) {
1058 do_warning("Error: expected type %d but read %d",
1059 expect, type);
1060 return -1;
1061 }
1062 return 0;
1063}
1064
1065static int test_type_token(enum event_type type, const char *token,
1066 enum event_type expect, const char *expect_tok)
1067{
1068 if (type != expect) {
1069 do_warning("Error: expected type %d but read %d",
1070 expect, type);
1071 return -1;
1072 }
1073
1074 if (strcmp(token, expect_tok) != 0) {
1075 do_warning("Error: expected '%s' but read '%s'",
1076 expect_tok, token);
1077 return -1;
1078 }
1079 return 0;
1080}
1081
1082static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1083{
1084 enum event_type type;
1085
1086 if (newline_ok)
1087 type = read_token(tok);
1088 else
1089 type = read_token_item(tok);
1090 return test_type(type, expect);
1091}
1092
1093static int read_expect_type(enum event_type expect, char **tok)
1094{
1095 return __read_expect_type(expect, tok, 1);
1096}
1097
1098static int __read_expected(enum event_type expect, const char *str,
1099 int newline_ok)
1100{
1101 enum event_type type;
1102 char *token;
1103 int ret;
1104
1105 if (newline_ok)
1106 type = read_token(&token);
1107 else
1108 type = read_token_item(&token);
1109
1110 ret = test_type_token(type, token, expect, str);
1111
1112 free_token(token);
1113
1114 return ret;
1115}
1116
1117static int read_expected(enum event_type expect, const char *str)
1118{
1119 return __read_expected(expect, str, 1);
1120}
1121
1122static int read_expected_item(enum event_type expect, const char *str)
1123{
1124 return __read_expected(expect, str, 0);
1125}
1126
1127static char *event_read_name(void)
1128{
1129 char *token;
1130
1131 if (read_expected(EVENT_ITEM, "name") < 0)
1132 return NULL;
1133
1134 if (read_expected(EVENT_OP, ":") < 0)
1135 return NULL;
1136
1137 if (read_expect_type(EVENT_ITEM, &token) < 0)
1138 goto fail;
1139
1140 return token;
1141
1142 fail:
1143 free_token(token);
1144 return NULL;
1145}
1146
1147static int event_read_id(void)
1148{
1149 char *token;
1150 int id;
1151
1152 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1153 return -1;
1154
1155 if (read_expected(EVENT_OP, ":") < 0)
1156 return -1;
1157
1158 if (read_expect_type(EVENT_ITEM, &token) < 0)
1159 goto fail;
1160
1161 id = strtoul(token, NULL, 0);
1162 free_token(token);
1163 return id;
1164
1165 fail:
1166 free_token(token);
1167 return -1;
1168}
1169
1170static int field_is_string(struct format_field *field)
1171{
1172 if ((field->flags & FIELD_IS_ARRAY) &&
1173 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1174 strstr(field->type, "s8")))
1175 return 1;
1176
1177 return 0;
1178}
1179
1180static int field_is_dynamic(struct format_field *field)
1181{
1182 if (strncmp(field->type, "__data_loc", 10) == 0)
1183 return 1;
1184
1185 return 0;
1186}
1187
1188static int field_is_long(struct format_field *field)
1189{
1190 /* includes long long */
1191 if (strstr(field->type, "long"))
1192 return 1;
1193
1194 return 0;
1195}
1196
1197static int event_read_fields(struct event_format *event, struct format_field **fields)
1198{
1199 struct format_field *field = NULL;
1200 enum event_type type;
1201 char *token;
1202 char *last_token;
1203 int count = 0;
1204
1205 do {
1206 type = read_token(&token);
1207 if (type == EVENT_NEWLINE) {
1208 free_token(token);
1209 return count;
1210 }
1211
1212 count++;
1213
1214 if (test_type_token(type, token, EVENT_ITEM, "field"))
1215 goto fail;
1216 free_token(token);
1217
1218 type = read_token(&token);
1219 /*
1220 * The ftrace fields may still use the "special" name.
1221 * Just ignore it.
1222 */
1223 if (event->flags & EVENT_FL_ISFTRACE &&
1224 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1225 free_token(token);
1226 type = read_token(&token);
1227 }
1228
1229 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1230 goto fail;
1231
1232 free_token(token);
1233 if (read_expect_type(EVENT_ITEM, &token) < 0)
1234 goto fail;
1235
1236 last_token = token;
1237
1238 field = malloc_or_die(sizeof(*field));
1239 memset(field, 0, sizeof(*field));
1240 field->event = event;
1241
1242 /* read the rest of the type */
1243 for (;;) {
1244 type = read_token(&token);
1245 if (type == EVENT_ITEM ||
1246 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1247 /*
1248 * Some of the ftrace fields are broken and have
1249 * an illegal "." in them.
1250 */
1251 (event->flags & EVENT_FL_ISFTRACE &&
1252 type == EVENT_OP && strcmp(token, ".") == 0)) {
1253
1254 if (strcmp(token, "*") == 0)
1255 field->flags |= FIELD_IS_POINTER;
1256
1257 if (field->type) {
1258 field->type = realloc(field->type,
1259 strlen(field->type) +
1260 strlen(last_token) + 2);
1261 strcat(field->type, " ");
1262 strcat(field->type, last_token);
1263 free(last_token);
1264 } else
1265 field->type = last_token;
1266 last_token = token;
1267 continue;
1268 }
1269
1270 break;
1271 }
1272
1273 if (!field->type) {
1274 die("no type found");
1275 goto fail;
1276 }
1277 field->name = last_token;
1278
1279 if (test_type(type, EVENT_OP))
1280 goto fail;
1281
1282 if (strcmp(token, "[") == 0) {
1283 enum event_type last_type = type;
1284 char *brackets = token;
1285 int len;
1286
1287 field->flags |= FIELD_IS_ARRAY;
1288
1289 type = read_token(&token);
1290
1291 if (type == EVENT_ITEM)
1292 field->arraylen = strtoul(token, NULL, 0);
1293 else
1294 field->arraylen = 0;
1295
1296 while (strcmp(token, "]") != 0) {
1297 if (last_type == EVENT_ITEM &&
1298 type == EVENT_ITEM)
1299 len = 2;
1300 else
1301 len = 1;
1302 last_type = type;
1303
1304 brackets = realloc(brackets,
1305 strlen(brackets) +
1306 strlen(token) + len);
1307 if (len == 2)
1308 strcat(brackets, " ");
1309 strcat(brackets, token);
1310 /* We only care about the last token */
1311 field->arraylen = strtoul(token, NULL, 0);
1312 free_token(token);
1313 type = read_token(&token);
1314 if (type == EVENT_NONE) {
1315 die("failed to find token");
1316 goto fail;
1317 }
1318 }
1319
1320 free_token(token);
1321
1322 brackets = realloc(brackets, strlen(brackets) + 2);
1323 strcat(brackets, "]");
1324
1325 /* add brackets to type */
1326
1327 type = read_token(&token);
1328 /*
1329 * If the next token is not an OP, then it is of
1330 * the format: type [] item;
1331 */
1332 if (type == EVENT_ITEM) {
1333 field->type = realloc(field->type,
1334 strlen(field->type) +
1335 strlen(field->name) +
1336 strlen(brackets) + 2);
1337 strcat(field->type, " ");
1338 strcat(field->type, field->name);
1339 free_token(field->name);
1340 strcat(field->type, brackets);
1341 field->name = token;
1342 type = read_token(&token);
1343 } else {
1344 field->type = realloc(field->type,
1345 strlen(field->type) +
1346 strlen(brackets) + 1);
1347 strcat(field->type, brackets);
1348 }
1349 free(brackets);
1350 }
1351
1352 if (field_is_string(field))
1353 field->flags |= FIELD_IS_STRING;
1354 if (field_is_dynamic(field))
1355 field->flags |= FIELD_IS_DYNAMIC;
1356 if (field_is_long(field))
1357 field->flags |= FIELD_IS_LONG;
1358
1359 if (test_type_token(type, token, EVENT_OP, ";"))
1360 goto fail;
1361 free_token(token);
1362
1363 if (read_expected(EVENT_ITEM, "offset") < 0)
1364 goto fail_expect;
1365
1366 if (read_expected(EVENT_OP, ":") < 0)
1367 goto fail_expect;
1368
1369 if (read_expect_type(EVENT_ITEM, &token))
1370 goto fail;
1371 field->offset = strtoul(token, NULL, 0);
1372 free_token(token);
1373
1374 if (read_expected(EVENT_OP, ";") < 0)
1375 goto fail_expect;
1376
1377 if (read_expected(EVENT_ITEM, "size") < 0)
1378 goto fail_expect;
1379
1380 if (read_expected(EVENT_OP, ":") < 0)
1381 goto fail_expect;
1382
1383 if (read_expect_type(EVENT_ITEM, &token))
1384 goto fail;
1385 field->size = strtoul(token, NULL, 0);
1386 free_token(token);
1387
1388 if (read_expected(EVENT_OP, ";") < 0)
1389 goto fail_expect;
1390
1391 type = read_token(&token);
1392 if (type != EVENT_NEWLINE) {
1393 /* newer versions of the kernel have a "signed" type */
1394 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1395 goto fail;
1396
1397 free_token(token);
1398
1399 if (read_expected(EVENT_OP, ":") < 0)
1400 goto fail_expect;
1401
1402 if (read_expect_type(EVENT_ITEM, &token))
1403 goto fail;
1404
1405 /* add signed type */
1406
1407 free_token(token);
1408 if (read_expected(EVENT_OP, ";") < 0)
1409 goto fail_expect;
1410
1411 if (read_expect_type(EVENT_NEWLINE, &token))
1412 goto fail;
1413 }
1414
1415 free_token(token);
1416
1417 if (field->flags & FIELD_IS_ARRAY) {
1418 if (field->arraylen)
1419 field->elementsize = field->size / field->arraylen;
1420 else if (field->flags & FIELD_IS_STRING)
1421 field->elementsize = 1;
1422 else
1423 field->elementsize = event->pevent->long_size;
1424 } else
1425 field->elementsize = field->size;
1426
1427 *fields = field;
1428 fields = &field->next;
1429
1430 } while (1);
1431
1432 return 0;
1433
1434fail:
1435 free_token(token);
1436fail_expect:
57d34dc5
NK
1437 if (field) {
1438 free(field->type);
1439 free(field->name);
f7d82350 1440 free(field);
57d34dc5 1441 }
f7d82350
SR
1442 return -1;
1443}
1444
1445static int event_read_format(struct event_format *event)
1446{
1447 char *token;
1448 int ret;
1449
1450 if (read_expected_item(EVENT_ITEM, "format") < 0)
1451 return -1;
1452
1453 if (read_expected(EVENT_OP, ":") < 0)
1454 return -1;
1455
1456 if (read_expect_type(EVENT_NEWLINE, &token))
1457 goto fail;
1458 free_token(token);
1459
1460 ret = event_read_fields(event, &event->format.common_fields);
1461 if (ret < 0)
1462 return ret;
1463 event->format.nr_common = ret;
1464
1465 ret = event_read_fields(event, &event->format.fields);
1466 if (ret < 0)
1467 return ret;
1468 event->format.nr_fields = ret;
1469
1470 return 0;
1471
1472 fail:
1473 free_token(token);
1474 return -1;
1475}
1476
1477static enum event_type
1478process_arg_token(struct event_format *event, struct print_arg *arg,
1479 char **tok, enum event_type type);
1480
1481static enum event_type
1482process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1483{
1484 enum event_type type;
1485 char *token;
1486
1487 type = read_token(&token);
1488 *tok = token;
1489
1490 return process_arg_token(event, arg, tok, type);
1491}
1492
1493static enum event_type
1494process_op(struct event_format *event, struct print_arg *arg, char **tok);
1495
1496static enum event_type
1497process_cond(struct event_format *event, struct print_arg *top, char **tok)
1498{
1499 struct print_arg *arg, *left, *right;
1500 enum event_type type;
1501 char *token = NULL;
1502
1503 arg = alloc_arg();
1504 left = alloc_arg();
1505 right = alloc_arg();
1506
1507 arg->type = PRINT_OP;
1508 arg->op.left = left;
1509 arg->op.right = right;
1510
1511 *tok = NULL;
1512 type = process_arg(event, left, &token);
1513
1514 again:
1515 /* Handle other operations in the arguments */
1516 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1517 type = process_op(event, left, &token);
1518 goto again;
1519 }
1520
1521 if (test_type_token(type, token, EVENT_OP, ":"))
1522 goto out_free;
1523
1524 arg->op.op = token;
1525
1526 type = process_arg(event, right, &token);
1527
1528 top->op.right = arg;
1529
1530 *tok = token;
1531 return type;
1532
1533out_free:
1534 /* Top may point to itself */
1535 top->op.right = NULL;
1536 free_token(token);
1537 free_arg(arg);
1538 return EVENT_ERROR;
1539}
1540
1541static enum event_type
1542process_array(struct event_format *event, struct print_arg *top, char **tok)
1543{
1544 struct print_arg *arg;
1545 enum event_type type;
1546 char *token = NULL;
1547
1548 arg = alloc_arg();
1549
1550 *tok = NULL;
1551 type = process_arg(event, arg, &token);
1552 if (test_type_token(type, token, EVENT_OP, "]"))
1553 goto out_free;
1554
1555 top->op.right = arg;
1556
1557 free_token(token);
1558 type = read_token_item(&token);
1559 *tok = token;
1560
1561 return type;
1562
1563out_free:
1564 free_token(*tok);
1565 *tok = NULL;
1566 free_arg(arg);
1567 return EVENT_ERROR;
1568}
1569
1570static int get_op_prio(char *op)
1571{
1572 if (!op[1]) {
1573 switch (op[0]) {
1574 case '~':
1575 case '!':
1576 return 4;
1577 case '*':
1578 case '/':
1579 case '%':
1580 return 6;
1581 case '+':
1582 case '-':
1583 return 7;
1584 /* '>>' and '<<' are 8 */
1585 case '<':
1586 case '>':
1587 return 9;
1588 /* '==' and '!=' are 10 */
1589 case '&':
1590 return 11;
1591 case '^':
1592 return 12;
1593 case '|':
1594 return 13;
1595 case '?':
1596 return 16;
1597 default:
14ffde0e 1598 do_warning("unknown op '%c'", op[0]);
f7d82350
SR
1599 return -1;
1600 }
1601 } else {
1602 if (strcmp(op, "++") == 0 ||
1603 strcmp(op, "--") == 0) {
1604 return 3;
1605 } else if (strcmp(op, ">>") == 0 ||
1606 strcmp(op, "<<") == 0) {
1607 return 8;
1608 } else if (strcmp(op, ">=") == 0 ||
1609 strcmp(op, "<=") == 0) {
1610 return 9;
1611 } else if (strcmp(op, "==") == 0 ||
1612 strcmp(op, "!=") == 0) {
1613 return 10;
1614 } else if (strcmp(op, "&&") == 0) {
1615 return 14;
1616 } else if (strcmp(op, "||") == 0) {
1617 return 15;
1618 } else {
14ffde0e 1619 do_warning("unknown op '%s'", op);
f7d82350
SR
1620 return -1;
1621 }
1622 }
1623}
1624
14ffde0e 1625static int set_op_prio(struct print_arg *arg)
f7d82350
SR
1626{
1627
1628 /* single ops are the greatest */
14ffde0e 1629 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
f7d82350 1630 arg->op.prio = 0;
14ffde0e
VN
1631 else
1632 arg->op.prio = get_op_prio(arg->op.op);
f7d82350 1633
14ffde0e 1634 return arg->op.prio;
f7d82350
SR
1635}
1636
1637/* Note, *tok does not get freed, but will most likely be saved */
1638static enum event_type
1639process_op(struct event_format *event, struct print_arg *arg, char **tok)
1640{
1641 struct print_arg *left, *right = NULL;
1642 enum event_type type;
1643 char *token;
1644
1645 /* the op is passed in via tok */
1646 token = *tok;
1647
1648 if (arg->type == PRINT_OP && !arg->op.left) {
1649 /* handle single op */
1650 if (token[1]) {
1651 die("bad op token %s", token);
1652 goto out_free;
1653 }
1654 switch (token[0]) {
1655 case '~':
1656 case '!':
1657 case '+':
1658 case '-':
1659 break;
1660 default:
1661 do_warning("bad op token %s", token);
1662 goto out_free;
1663
1664 }
1665
1666 /* make an empty left */
1667 left = alloc_arg();
1668 left->type = PRINT_NULL;
1669 arg->op.left = left;
1670
1671 right = alloc_arg();
1672 arg->op.right = right;
1673
1674 /* do not free the token, it belongs to an op */
1675 *tok = NULL;
1676 type = process_arg(event, right, tok);
1677
1678 } else if (strcmp(token, "?") == 0) {
1679
1680 left = alloc_arg();
1681 /* copy the top arg to the left */
1682 *left = *arg;
1683
1684 arg->type = PRINT_OP;
1685 arg->op.op = token;
1686 arg->op.left = left;
1687 arg->op.prio = 0;
1688
1689 type = process_cond(event, arg, tok);
1690
1691 } else if (strcmp(token, ">>") == 0 ||
1692 strcmp(token, "<<") == 0 ||
1693 strcmp(token, "&") == 0 ||
1694 strcmp(token, "|") == 0 ||
1695 strcmp(token, "&&") == 0 ||
1696 strcmp(token, "||") == 0 ||
1697 strcmp(token, "-") == 0 ||
1698 strcmp(token, "+") == 0 ||
1699 strcmp(token, "*") == 0 ||
1700 strcmp(token, "^") == 0 ||
1701 strcmp(token, "/") == 0 ||
1702 strcmp(token, "<") == 0 ||
1703 strcmp(token, ">") == 0 ||
1704 strcmp(token, "==") == 0 ||
1705 strcmp(token, "!=") == 0) {
1706
1707 left = alloc_arg();
1708
1709 /* copy the top arg to the left */
1710 *left = *arg;
1711
1712 arg->type = PRINT_OP;
1713 arg->op.op = token;
1714 arg->op.left = left;
1715
14ffde0e
VN
1716 if (set_op_prio(arg) == -1) {
1717 event->flags |= EVENT_FL_FAILED;
1718 goto out_free;
1719 }
f7d82350
SR
1720
1721 type = read_token_item(&token);
1722 *tok = token;
1723
1724 /* could just be a type pointer */
1725 if ((strcmp(arg->op.op, "*") == 0) &&
1726 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1727 if (left->type != PRINT_ATOM)
1728 die("bad pointer type");
1729 left->atom.atom = realloc(left->atom.atom,
1730 strlen(left->atom.atom) + 3);
1731 strcat(left->atom.atom, " *");
1732 free(arg->op.op);
1733 *arg = *left;
1734 free(left);
1735
1736 return type;
1737 }
1738
1739 right = alloc_arg();
1740 type = process_arg_token(event, right, tok, type);
1741 arg->op.right = right;
1742
1743 } else if (strcmp(token, "[") == 0) {
1744
1745 left = alloc_arg();
1746 *left = *arg;
1747
1748 arg->type = PRINT_OP;
1749 arg->op.op = token;
1750 arg->op.left = left;
1751
1752 arg->op.prio = 0;
1753
1754 type = process_array(event, arg, tok);
1755
1756 } else {
1757 do_warning("unknown op '%s'", token);
1758 event->flags |= EVENT_FL_FAILED;
1759 /* the arg is now the left side */
1760 goto out_free;
1761 }
1762
1763 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1764 int prio;
1765
1766 /* higher prios need to be closer to the root */
1767 prio = get_op_prio(*tok);
1768
1769 if (prio > arg->op.prio)
1770 return process_op(event, arg, tok);
1771
1772 return process_op(event, right, tok);
1773 }
1774
1775 return type;
1776
1777 out_free:
1778 free_token(token);
1779 *tok = NULL;
1780 return EVENT_ERROR;
1781}
1782
1783static enum event_type
1784process_entry(struct event_format *event __unused, struct print_arg *arg,
1785 char **tok)
1786{
1787 enum event_type type;
1788 char *field;
1789 char *token;
1790
1791 if (read_expected(EVENT_OP, "->") < 0)
1792 goto out_err;
1793
1794 if (read_expect_type(EVENT_ITEM, &token) < 0)
1795 goto out_free;
1796 field = token;
1797
1798 arg->type = PRINT_FIELD;
1799 arg->field.name = field;
1800
5205aec9
TZ
1801 if (is_flag_field) {
1802 arg->field.field = pevent_find_any_field(event, arg->field.name);
1803 arg->field.field->flags |= FIELD_IS_FLAG;
1804 is_flag_field = 0;
1805 } else if (is_symbolic_field) {
1806 arg->field.field = pevent_find_any_field(event, arg->field.name);
1807 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1808 is_symbolic_field = 0;
1809 }
1810
f7d82350
SR
1811 type = read_token(&token);
1812 *tok = token;
1813
1814 return type;
1815
1816 out_free:
1817 free_token(token);
1818 out_err:
1819 *tok = NULL;
1820 return EVENT_ERROR;
1821}
1822
1823static char *arg_eval (struct print_arg *arg);
1824
1825static unsigned long long
1826eval_type_str(unsigned long long val, const char *type, int pointer)
1827{
1828 int sign = 0;
1829 char *ref;
1830 int len;
1831
1832 len = strlen(type);
1833
1834 if (pointer) {
1835
1836 if (type[len-1] != '*') {
1837 do_warning("pointer expected with non pointer type");
1838 return val;
1839 }
1840
1841 ref = malloc_or_die(len);
1842 memcpy(ref, type, len);
1843
1844 /* chop off the " *" */
1845 ref[len - 2] = 0;
1846
1847 val = eval_type_str(val, ref, 0);
1848 free(ref);
1849 return val;
1850 }
1851
1852 /* check if this is a pointer */
1853 if (type[len - 1] == '*')
1854 return val;
1855
1856 /* Try to figure out the arg size*/
1857 if (strncmp(type, "struct", 6) == 0)
1858 /* all bets off */
1859 return val;
1860
1861 if (strcmp(type, "u8") == 0)
1862 return val & 0xff;
1863
1864 if (strcmp(type, "u16") == 0)
1865 return val & 0xffff;
1866
1867 if (strcmp(type, "u32") == 0)
1868 return val & 0xffffffff;
1869
1870 if (strcmp(type, "u64") == 0 ||
1871 strcmp(type, "s64"))
1872 return val;
1873
1874 if (strcmp(type, "s8") == 0)
1875 return (unsigned long long)(char)val & 0xff;
1876
1877 if (strcmp(type, "s16") == 0)
1878 return (unsigned long long)(short)val & 0xffff;
1879
1880 if (strcmp(type, "s32") == 0)
1881 return (unsigned long long)(int)val & 0xffffffff;
1882
1883 if (strncmp(type, "unsigned ", 9) == 0) {
1884 sign = 0;
1885 type += 9;
1886 }
1887
1888 if (strcmp(type, "char") == 0) {
1889 if (sign)
1890 return (unsigned long long)(char)val & 0xff;
1891 else
1892 return val & 0xff;
1893 }
1894
1895 if (strcmp(type, "short") == 0) {
1896 if (sign)
1897 return (unsigned long long)(short)val & 0xffff;
1898 else
1899 return val & 0xffff;
1900 }
1901
1902 if (strcmp(type, "int") == 0) {
1903 if (sign)
1904 return (unsigned long long)(int)val & 0xffffffff;
1905 else
1906 return val & 0xffffffff;
1907 }
1908
1909 return val;
1910}
1911
1912/*
1913 * Try to figure out the type.
1914 */
1915static unsigned long long
1916eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1917{
1918 if (arg->type != PRINT_TYPE)
1919 die("expected type argument");
1920
1921 return eval_type_str(val, arg->typecast.type, pointer);
1922}
1923
d69afed5 1924static int arg_num_eval(struct print_arg *arg, long long *val)
f7d82350
SR
1925{
1926 long long left, right;
d69afed5 1927 int ret = 1;
f7d82350
SR
1928
1929 switch (arg->type) {
1930 case PRINT_ATOM:
d69afed5 1931 *val = strtoll(arg->atom.atom, NULL, 0);
f7d82350
SR
1932 break;
1933 case PRINT_TYPE:
d69afed5
VN
1934 ret = arg_num_eval(arg->typecast.item, val);
1935 if (!ret)
1936 break;
1937 *val = eval_type(*val, arg, 0);
f7d82350
SR
1938 break;
1939 case PRINT_OP:
1940 switch (arg->op.op[0]) {
1941 case '|':
d69afed5
VN
1942 ret = arg_num_eval(arg->op.left, &left);
1943 if (!ret)
1944 break;
1945 ret = arg_num_eval(arg->op.right, &right);
1946 if (!ret)
1947 break;
f7d82350 1948 if (arg->op.op[1])
d69afed5 1949 *val = left || right;
f7d82350 1950 else
d69afed5 1951 *val = left | right;
f7d82350
SR
1952 break;
1953 case '&':
d69afed5
VN
1954 ret = arg_num_eval(arg->op.left, &left);
1955 if (!ret)
1956 break;
1957 ret = arg_num_eval(arg->op.right, &right);
1958 if (!ret)
1959 break;
f7d82350 1960 if (arg->op.op[1])
d69afed5 1961 *val = left && right;
f7d82350 1962 else
d69afed5 1963 *val = left & right;
f7d82350
SR
1964 break;
1965 case '<':
d69afed5
VN
1966 ret = arg_num_eval(arg->op.left, &left);
1967 if (!ret)
1968 break;
1969 ret = arg_num_eval(arg->op.right, &right);
1970 if (!ret)
1971 break;
f7d82350
SR
1972 switch (arg->op.op[1]) {
1973 case 0:
d69afed5 1974 *val = left < right;
f7d82350
SR
1975 break;
1976 case '<':
d69afed5 1977 *val = left << right;
f7d82350
SR
1978 break;
1979 case '=':
d69afed5 1980 *val = left <= right;
f7d82350
SR
1981 break;
1982 default:
d69afed5
VN
1983 do_warning("unknown op '%s'", arg->op.op);
1984 ret = 0;
f7d82350
SR
1985 }
1986 break;
1987 case '>':
d69afed5
VN
1988 ret = arg_num_eval(arg->op.left, &left);
1989 if (!ret)
1990 break;
1991 ret = arg_num_eval(arg->op.right, &right);
1992 if (!ret)
1993 break;
f7d82350
SR
1994 switch (arg->op.op[1]) {
1995 case 0:
d69afed5 1996 *val = left > right;
f7d82350
SR
1997 break;
1998 case '>':
d69afed5 1999 *val = left >> right;
f7d82350
SR
2000 break;
2001 case '=':
d69afed5 2002 *val = left >= right;
f7d82350
SR
2003 break;
2004 default:
d69afed5
VN
2005 do_warning("unknown op '%s'", arg->op.op);
2006 ret = 0;
f7d82350
SR
2007 }
2008 break;
2009 case '=':
d69afed5
VN
2010 ret = arg_num_eval(arg->op.left, &left);
2011 if (!ret)
2012 break;
2013 ret = arg_num_eval(arg->op.right, &right);
2014 if (!ret)
2015 break;
f7d82350 2016
d69afed5
VN
2017 if (arg->op.op[1] != '=') {
2018 do_warning("unknown op '%s'", arg->op.op);
2019 ret = 0;
2020 } else
2021 *val = left == right;
f7d82350
SR
2022 break;
2023 case '!':
d69afed5
VN
2024 ret = arg_num_eval(arg->op.left, &left);
2025 if (!ret)
2026 break;
2027 ret = arg_num_eval(arg->op.right, &right);
2028 if (!ret)
2029 break;
f7d82350
SR
2030
2031 switch (arg->op.op[1]) {
2032 case '=':
d69afed5 2033 *val = left != right;
f7d82350
SR
2034 break;
2035 default:
d69afed5
VN
2036 do_warning("unknown op '%s'", arg->op.op);
2037 ret = 0;
f7d82350
SR
2038 }
2039 break;
2040 case '-':
2041 /* check for negative */
2042 if (arg->op.left->type == PRINT_NULL)
2043 left = 0;
2044 else
d69afed5
VN
2045 ret = arg_num_eval(arg->op.left, &left);
2046 if (!ret)
2047 break;
2048 ret = arg_num_eval(arg->op.right, &right);
2049 if (!ret)
2050 break;
2051 *val = left - right;
f7d82350 2052 break;
b4828598
VN
2053 case '+':
2054 if (arg->op.left->type == PRINT_NULL)
2055 left = 0;
2056 else
2057 ret = arg_num_eval(arg->op.left, &left);
2058 if (!ret)
2059 break;
2060 ret = arg_num_eval(arg->op.right, &right);
2061 if (!ret)
2062 break;
2063 *val = left + right;
2064 break;
f7d82350 2065 default:
d69afed5
VN
2066 do_warning("unknown op '%s'", arg->op.op);
2067 ret = 0;
f7d82350
SR
2068 }
2069 break;
2070
2071 case PRINT_NULL:
2072 case PRINT_FIELD ... PRINT_SYMBOL:
2073 case PRINT_STRING:
2074 case PRINT_BSTRING:
2075 default:
d69afed5
VN
2076 do_warning("invalid eval type %d", arg->type);
2077 ret = 0;
f7d82350
SR
2078
2079 }
d69afed5 2080 return ret;
f7d82350
SR
2081}
2082
2083static char *arg_eval (struct print_arg *arg)
2084{
2085 long long val;
2086 static char buf[20];
2087
2088 switch (arg->type) {
2089 case PRINT_ATOM:
2090 return arg->atom.atom;
2091 case PRINT_TYPE:
2092 return arg_eval(arg->typecast.item);
2093 case PRINT_OP:
d69afed5
VN
2094 if (!arg_num_eval(arg, &val))
2095 break;
f7d82350
SR
2096 sprintf(buf, "%lld", val);
2097 return buf;
2098
2099 case PRINT_NULL:
2100 case PRINT_FIELD ... PRINT_SYMBOL:
2101 case PRINT_STRING:
2102 case PRINT_BSTRING:
2103 default:
2104 die("invalid eval type %d", arg->type);
2105 break;
2106 }
2107
2108 return NULL;
2109}
2110
2111static enum event_type
2112process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2113{
2114 enum event_type type;
2115 struct print_arg *arg = NULL;
2116 struct print_flag_sym *field;
2117 char *token = *tok;
2118 char *value;
2119
2120 do {
2121 free_token(token);
2122 type = read_token_item(&token);
2123 if (test_type_token(type, token, EVENT_OP, "{"))
2124 break;
2125
2126 arg = alloc_arg();
2127
2128 free_token(token);
2129 type = process_arg(event, arg, &token);
00b9da72
SH
2130
2131 if (type == EVENT_OP)
2132 type = process_op(event, arg, &token);
2133
2134 if (type == EVENT_ERROR)
2135 goto out_free;
2136
f7d82350
SR
2137 if (test_type_token(type, token, EVENT_DELIM, ","))
2138 goto out_free;
2139
2140 field = malloc_or_die(sizeof(*field));
54a36258 2141 memset(field, 0, sizeof(*field));
f7d82350
SR
2142
2143 value = arg_eval(arg);
d69afed5
VN
2144 if (value == NULL)
2145 goto out_free;
f7d82350
SR
2146 field->value = strdup(value);
2147
2148 free_arg(arg);
2149 arg = alloc_arg();
2150
2151 free_token(token);
2152 type = process_arg(event, arg, &token);
2153 if (test_type_token(type, token, EVENT_OP, "}"))
2154 goto out_free;
2155
2156 value = arg_eval(arg);
d69afed5
VN
2157 if (value == NULL)
2158 goto out_free;
f7d82350
SR
2159 field->str = strdup(value);
2160 free_arg(arg);
2161 arg = NULL;
2162
2163 *list = field;
2164 list = &field->next;
2165
2166 free_token(token);
2167 type = read_token_item(&token);
2168 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2169
2170 *tok = token;
2171 return type;
2172
2173out_free:
2174 free_arg(arg);
2175 free_token(token);
2176 *tok = NULL;
2177
2178 return EVENT_ERROR;
2179}
2180
2181static enum event_type
2182process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2183{
2184 struct print_arg *field;
2185 enum event_type type;
2186 char *token;
2187
2188 memset(arg, 0, sizeof(*arg));
2189 arg->type = PRINT_FLAGS;
2190
2191 field = alloc_arg();
2192
2193 type = process_arg(event, field, &token);
2194
2195 /* Handle operations in the first argument */
2196 while (type == EVENT_OP)
2197 type = process_op(event, field, &token);
2198
2199 if (test_type_token(type, token, EVENT_DELIM, ","))
2200 goto out_free;
2201 free_token(token);
2202
2203 arg->flags.field = field;
2204
2205 type = read_token_item(&token);
2206 if (event_item_type(type)) {
2207 arg->flags.delim = token;
2208 type = read_token_item(&token);
2209 }
2210
2211 if (test_type_token(type, token, EVENT_DELIM, ","))
2212 goto out_free;
2213
2214 type = process_fields(event, &arg->flags.flags, &token);
2215 if (test_type_token(type, token, EVENT_DELIM, ")"))
2216 goto out_free;
2217
2218 free_token(token);
2219 type = read_token_item(tok);
2220 return type;
2221
2222 out_free:
2223 free_token(token);
2224 *tok = NULL;
2225 return EVENT_ERROR;
2226}
2227
2228static enum event_type
2229process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2230{
2231 struct print_arg *field;
2232 enum event_type type;
2233 char *token;
2234
2235 memset(arg, 0, sizeof(*arg));
2236 arg->type = PRINT_SYMBOL;
2237
2238 field = alloc_arg();
2239
2240 type = process_arg(event, field, &token);
2241 if (test_type_token(type, token, EVENT_DELIM, ","))
2242 goto out_free;
2243
2244 arg->symbol.field = field;
2245
2246 type = process_fields(event, &arg->symbol.symbols, &token);
2247 if (test_type_token(type, token, EVENT_DELIM, ")"))
2248 goto out_free;
2249
2250 free_token(token);
2251 type = read_token_item(tok);
2252 return type;
2253
2254 out_free:
2255 free_token(token);
2256 *tok = NULL;
2257 return EVENT_ERROR;
2258}
2259
2260static enum event_type
2261process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2262{
2263 struct format_field *field;
2264 enum event_type type;
2265 char *token;
2266
2267 memset(arg, 0, sizeof(*arg));
2268 arg->type = PRINT_DYNAMIC_ARRAY;
2269
2270 /*
2271 * The item within the parenthesis is another field that holds
2272 * the index into where the array starts.
2273 */
2274 type = read_token(&token);
2275 *tok = token;
2276 if (type != EVENT_ITEM)
2277 goto out_free;
2278
2279 /* Find the field */
2280
2281 field = pevent_find_field(event, token);
2282 if (!field)
2283 goto out_free;
2284
2285 arg->dynarray.field = field;
2286 arg->dynarray.index = 0;
2287
2288 if (read_expected(EVENT_DELIM, ")") < 0)
2289 goto out_free;
2290
2291 free_token(token);
2292 type = read_token_item(&token);
2293 *tok = token;
2294 if (type != EVENT_OP || strcmp(token, "[") != 0)
2295 return type;
2296
2297 free_token(token);
2298 arg = alloc_arg();
2299 type = process_arg(event, arg, &token);
2300 if (type == EVENT_ERROR)
2301 goto out_free;
2302
2303 if (!test_type_token(type, token, EVENT_OP, "]"))
2304 goto out_free;
2305
2306 free_token(token);
2307 type = read_token_item(tok);
2308 return type;
2309
2310 out_free:
2311 free(arg);
2312 free_token(token);
2313 *tok = NULL;
2314 return EVENT_ERROR;
2315}
2316
2317static enum event_type
2318process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2319{
2320 struct print_arg *item_arg;
2321 enum event_type type;
2322 char *token;
2323
2324 type = process_arg(event, arg, &token);
2325
2326 if (type == EVENT_ERROR)
2327 goto out_free;
2328
2329 if (type == EVENT_OP)
2330 type = process_op(event, arg, &token);
2331
2332 if (type == EVENT_ERROR)
2333 goto out_free;
2334
2335 if (test_type_token(type, token, EVENT_DELIM, ")"))
2336 goto out_free;
2337
2338 free_token(token);
2339 type = read_token_item(&token);
2340
2341 /*
2342 * If the next token is an item or another open paren, then
2343 * this was a typecast.
2344 */
2345 if (event_item_type(type) ||
2346 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2347
2348 /* make this a typecast and contine */
2349
2350 /* prevous must be an atom */
2351 if (arg->type != PRINT_ATOM)
2352 die("previous needed to be PRINT_ATOM");
2353
2354 item_arg = alloc_arg();
2355
2356 arg->type = PRINT_TYPE;
2357 arg->typecast.type = arg->atom.atom;
2358 arg->typecast.item = item_arg;
2359 type = process_arg_token(event, item_arg, &token, type);
2360
2361 }
2362
2363 *tok = token;
2364 return type;
2365
2366 out_free:
2367 free_token(token);
2368 *tok = NULL;
2369 return EVENT_ERROR;
2370}
2371
2372
2373static enum event_type
2374process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2375{
2376 enum event_type type;
2377 char *token;
2378
2379 if (read_expect_type(EVENT_ITEM, &token) < 0)
2380 goto out_free;
2381
2382 arg->type = PRINT_STRING;
2383 arg->string.string = token;
2384 arg->string.offset = -1;
2385
2386 if (read_expected(EVENT_DELIM, ")") < 0)
2387 goto out_err;
2388
2389 type = read_token(&token);
2390 *tok = token;
2391
2392 return type;
2393
2394 out_free:
2395 free_token(token);
2396 out_err:
2397 *tok = NULL;
2398 return EVENT_ERROR;
2399}
2400
2401static struct pevent_function_handler *
2402find_func_handler(struct pevent *pevent, char *func_name)
2403{
2404 struct pevent_function_handler *func;
2405
2406 for (func = pevent->func_handlers; func; func = func->next) {
2407 if (strcmp(func->name, func_name) == 0)
2408 break;
2409 }
2410
2411 return func;
2412}
2413
2414static void remove_func_handler(struct pevent *pevent, char *func_name)
2415{
2416 struct pevent_function_handler *func;
2417 struct pevent_function_handler **next;
2418
2419 next = &pevent->func_handlers;
2420 while ((func = *next)) {
2421 if (strcmp(func->name, func_name) == 0) {
2422 *next = func->next;
2423 free_func_handle(func);
2424 break;
2425 }
2426 next = &func->next;
2427 }
2428}
2429
2430static enum event_type
2431process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2432 struct print_arg *arg, char **tok)
2433{
2434 struct print_arg **next_arg;
2435 struct print_arg *farg;
2436 enum event_type type;
2437 char *token;
2438 char *test;
2439 int i;
2440
2441 arg->type = PRINT_FUNC;
2442 arg->func.func = func;
2443
2444 *tok = NULL;
2445
2446 next_arg = &(arg->func.args);
2447 for (i = 0; i < func->nr_args; i++) {
2448 farg = alloc_arg();
2449 type = process_arg(event, farg, &token);
2450 if (i < (func->nr_args - 1))
2451 test = ",";
2452 else
2453 test = ")";
2454
2455 if (test_type_token(type, token, EVENT_DELIM, test)) {
2456 free_arg(farg);
2457 free_token(token);
2458 return EVENT_ERROR;
2459 }
2460
2461 *next_arg = farg;
2462 next_arg = &(farg->next);
2463 free_token(token);
2464 }
2465
2466 type = read_token(&token);
2467 *tok = token;
2468
2469 return type;
2470}
2471
2472static enum event_type
2473process_function(struct event_format *event, struct print_arg *arg,
2474 char *token, char **tok)
2475{
2476 struct pevent_function_handler *func;
2477
2478 if (strcmp(token, "__print_flags") == 0) {
2479 free_token(token);
5205aec9 2480 is_flag_field = 1;
f7d82350
SR
2481 return process_flags(event, arg, tok);
2482 }
2483 if (strcmp(token, "__print_symbolic") == 0) {
2484 free_token(token);
5205aec9 2485 is_symbolic_field = 1;
f7d82350
SR
2486 return process_symbols(event, arg, tok);
2487 }
2488 if (strcmp(token, "__get_str") == 0) {
2489 free_token(token);
2490 return process_str(event, arg, tok);
2491 }
2492 if (strcmp(token, "__get_dynamic_array") == 0) {
2493 free_token(token);
2494 return process_dynamic_array(event, arg, tok);
2495 }
2496
2497 func = find_func_handler(event->pevent, token);
2498 if (func) {
2499 free_token(token);
2500 return process_func_handler(event, func, arg, tok);
2501 }
2502
2503 do_warning("function %s not defined", token);
2504 free_token(token);
2505 return EVENT_ERROR;
2506}
2507
2508static enum event_type
2509process_arg_token(struct event_format *event, struct print_arg *arg,
2510 char **tok, enum event_type type)
2511{
2512 char *token;
2513 char *atom;
2514
2515 token = *tok;
2516
2517 switch (type) {
2518 case EVENT_ITEM:
2519 if (strcmp(token, "REC") == 0) {
2520 free_token(token);
2521 type = process_entry(event, arg, &token);
2522 break;
2523 }
2524 atom = token;
2525 /* test the next token */
2526 type = read_token_item(&token);
2527
2528 /*
2529 * If the next token is a parenthesis, then this
2530 * is a function.
2531 */
2532 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2533 free_token(token);
2534 token = NULL;
2535 /* this will free atom. */
2536 type = process_function(event, arg, atom, &token);
2537 break;
2538 }
2539 /* atoms can be more than one token long */
2540 while (type == EVENT_ITEM) {
2541 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
2542 strcat(atom, " ");
2543 strcat(atom, token);
2544 free_token(token);
2545 type = read_token_item(&token);
2546 }
2547
2548 arg->type = PRINT_ATOM;
2549 arg->atom.atom = atom;
2550 break;
2551
2552 case EVENT_DQUOTE:
2553 case EVENT_SQUOTE:
2554 arg->type = PRINT_ATOM;
2555 arg->atom.atom = token;
2556 type = read_token_item(&token);
2557 break;
2558 case EVENT_DELIM:
2559 if (strcmp(token, "(") == 0) {
2560 free_token(token);
2561 type = process_paren(event, arg, &token);
2562 break;
2563 }
2564 case EVENT_OP:
2565 /* handle single ops */
2566 arg->type = PRINT_OP;
2567 arg->op.op = token;
2568 arg->op.left = NULL;
2569 type = process_op(event, arg, &token);
2570
2571 /* On error, the op is freed */
2572 if (type == EVENT_ERROR)
2573 arg->op.op = NULL;
2574
2575 /* return error type if errored */
2576 break;
2577
2578 case EVENT_ERROR ... EVENT_NEWLINE:
2579 default:
2580 die("unexpected type %d", type);
2581 }
2582 *tok = token;
2583
2584 return type;
2585}
2586
2587static int event_read_print_args(struct event_format *event, struct print_arg **list)
2588{
2589 enum event_type type = EVENT_ERROR;
2590 struct print_arg *arg;
2591 char *token;
2592 int args = 0;
2593
2594 do {
2595 if (type == EVENT_NEWLINE) {
2596 type = read_token_item(&token);
2597 continue;
2598 }
2599
2600 arg = alloc_arg();
2601
2602 type = process_arg(event, arg, &token);
2603
2604 if (type == EVENT_ERROR) {
2605 free_token(token);
2606 free_arg(arg);
2607 return -1;
2608 }
2609
2610 *list = arg;
2611 args++;
2612
2613 if (type == EVENT_OP) {
2614 type = process_op(event, arg, &token);
2615 free_token(token);
2616 if (type == EVENT_ERROR) {
2617 *list = NULL;
2618 free_arg(arg);
2619 return -1;
2620 }
2621 list = &arg->next;
2622 continue;
2623 }
2624
2625 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2626 free_token(token);
2627 *list = arg;
2628 list = &arg->next;
2629 continue;
2630 }
2631 break;
2632 } while (type != EVENT_NONE);
2633
2634 if (type != EVENT_NONE && type != EVENT_ERROR)
2635 free_token(token);
2636
2637 return args;
2638}
2639
2640static int event_read_print(struct event_format *event)
2641{
2642 enum event_type type;
2643 char *token;
2644 int ret;
2645
2646 if (read_expected_item(EVENT_ITEM, "print") < 0)
2647 return -1;
2648
2649 if (read_expected(EVENT_ITEM, "fmt") < 0)
2650 return -1;
2651
2652 if (read_expected(EVENT_OP, ":") < 0)
2653 return -1;
2654
2655 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2656 goto fail;
2657
2658 concat:
2659 event->print_fmt.format = token;
2660 event->print_fmt.args = NULL;
2661
2662 /* ok to have no arg */
2663 type = read_token_item(&token);
2664
2665 if (type == EVENT_NONE)
2666 return 0;
2667
2668 /* Handle concatenation of print lines */
2669 if (type == EVENT_DQUOTE) {
2670 char *cat;
2671
2672 cat = malloc_or_die(strlen(event->print_fmt.format) +
2673 strlen(token) + 1);
2674 strcpy(cat, event->print_fmt.format);
2675 strcat(cat, token);
2676 free_token(token);
2677 free_token(event->print_fmt.format);
2678 event->print_fmt.format = NULL;
2679 token = cat;
2680 goto concat;
2681 }
2682
2683 if (test_type_token(type, token, EVENT_DELIM, ","))
2684 goto fail;
2685
2686 free_token(token);
2687
2688 ret = event_read_print_args(event, &event->print_fmt.args);
2689 if (ret < 0)
2690 return -1;
2691
2692 return ret;
2693
2694 fail:
2695 free_token(token);
2696 return -1;
2697}
2698
2699/**
2700 * pevent_find_common_field - return a common field by event
2701 * @event: handle for the event
2702 * @name: the name of the common field to return
2703 *
2704 * Returns a common field from the event by the given @name.
2705 * This only searchs the common fields and not all field.
2706 */
2707struct format_field *
2708pevent_find_common_field(struct event_format *event, const char *name)
2709{
2710 struct format_field *format;
2711
2712 for (format = event->format.common_fields;
2713 format; format = format->next) {
2714 if (strcmp(format->name, name) == 0)
2715 break;
2716 }
2717
2718 return format;
2719}
2720
2721/**
2722 * pevent_find_field - find a non-common field
2723 * @event: handle for the event
2724 * @name: the name of the non-common field
2725 *
2726 * Returns a non-common field by the given @name.
2727 * This does not search common fields.
2728 */
2729struct format_field *
2730pevent_find_field(struct event_format *event, const char *name)
2731{
2732 struct format_field *format;
2733
2734 for (format = event->format.fields;
2735 format; format = format->next) {
2736 if (strcmp(format->name, name) == 0)
2737 break;
2738 }
2739
2740 return format;
2741}
2742
2743/**
2744 * pevent_find_any_field - find any field by name
2745 * @event: handle for the event
2746 * @name: the name of the field
2747 *
2748 * Returns a field by the given @name.
2749 * This searchs the common field names first, then
2750 * the non-common ones if a common one was not found.
2751 */
2752struct format_field *
2753pevent_find_any_field(struct event_format *event, const char *name)
2754{
2755 struct format_field *format;
2756
2757 format = pevent_find_common_field(event, name);
2758 if (format)
2759 return format;
2760 return pevent_find_field(event, name);
2761}
2762
2763/**
2764 * pevent_read_number - read a number from data
2765 * @pevent: handle for the pevent
2766 * @ptr: the raw data
2767 * @size: the size of the data that holds the number
2768 *
2769 * Returns the number (converted to host) from the
2770 * raw data.
2771 */
2772unsigned long long pevent_read_number(struct pevent *pevent,
2773 const void *ptr, int size)
2774{
2775 switch (size) {
2776 case 1:
2777 return *(unsigned char *)ptr;
2778 case 2:
2779 return data2host2(pevent, ptr);
2780 case 4:
2781 return data2host4(pevent, ptr);
2782 case 8:
2783 return data2host8(pevent, ptr);
2784 default:
2785 /* BUG! */
2786 return 0;
2787 }
2788}
2789
2790/**
2791 * pevent_read_number_field - read a number from data
2792 * @field: a handle to the field
2793 * @data: the raw data to read
2794 * @value: the value to place the number in
2795 *
2796 * Reads raw data according to a field offset and size,
2797 * and translates it into @value.
2798 *
2799 * Returns 0 on success, -1 otherwise.
2800 */
2801int pevent_read_number_field(struct format_field *field, const void *data,
2802 unsigned long long *value)
2803{
2804 if (!field)
2805 return -1;
2806 switch (field->size) {
2807 case 1:
2808 case 2:
2809 case 4:
2810 case 8:
2811 *value = pevent_read_number(field->event->pevent,
2812 data + field->offset, field->size);
2813 return 0;
2814 default:
2815 return -1;
2816 }
2817}
2818
2819static int get_common_info(struct pevent *pevent,
2820 const char *type, int *offset, int *size)
2821{
2822 struct event_format *event;
2823 struct format_field *field;
2824
2825 /*
2826 * All events should have the same common elements.
2827 * Pick any event to find where the type is;
2828 */
2829 if (!pevent->events)
2830 die("no event_list!");
2831
2832 event = pevent->events[0];
2833 field = pevent_find_common_field(event, type);
2834 if (!field)
2835 die("field '%s' not found", type);
2836
2837 *offset = field->offset;
2838 *size = field->size;
2839
2840 return 0;
2841}
2842
2843static int __parse_common(struct pevent *pevent, void *data,
2844 int *size, int *offset, const char *name)
2845{
2846 int ret;
2847
2848 if (!*size) {
2849 ret = get_common_info(pevent, name, offset, size);
2850 if (ret < 0)
2851 return ret;
2852 }
2853 return pevent_read_number(pevent, data + *offset, *size);
2854}
2855
2856static int trace_parse_common_type(struct pevent *pevent, void *data)
2857{
2858 return __parse_common(pevent, data,
2859 &pevent->type_size, &pevent->type_offset,
2860 "common_type");
2861}
2862
2863static int parse_common_pid(struct pevent *pevent, void *data)
2864{
2865 return __parse_common(pevent, data,
2866 &pevent->pid_size, &pevent->pid_offset,
2867 "common_pid");
2868}
2869
2870static int parse_common_pc(struct pevent *pevent, void *data)
2871{
2872 return __parse_common(pevent, data,
2873 &pevent->pc_size, &pevent->pc_offset,
2874 "common_preempt_count");
2875}
2876
2877static int parse_common_flags(struct pevent *pevent, void *data)
2878{
2879 return __parse_common(pevent, data,
2880 &pevent->flags_size, &pevent->flags_offset,
2881 "common_flags");
2882}
2883
2884static int parse_common_lock_depth(struct pevent *pevent, void *data)
2885{
2886 int ret;
2887
2888 ret = __parse_common(pevent, data,
2889 &pevent->ld_size, &pevent->ld_offset,
2890 "common_lock_depth");
2891 if (ret < 0)
2892 return -1;
2893
2894 return ret;
2895}
2896
2897static int events_id_cmp(const void *a, const void *b);
2898
2899/**
2900 * pevent_find_event - find an event by given id
2901 * @pevent: a handle to the pevent
2902 * @id: the id of the event
2903 *
2904 * Returns an event that has a given @id.
2905 */
2906struct event_format *pevent_find_event(struct pevent *pevent, int id)
2907{
2908 struct event_format **eventptr;
2909 struct event_format key;
2910 struct event_format *pkey = &key;
2911
2912 /* Check cache first */
2913 if (pevent->last_event && pevent->last_event->id == id)
2914 return pevent->last_event;
2915
2916 key.id = id;
2917
2918 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
2919 sizeof(*pevent->events), events_id_cmp);
2920
2921 if (eventptr) {
2922 pevent->last_event = *eventptr;
2923 return *eventptr;
2924 }
2925
2926 return NULL;
2927}
2928
2929/**
2930 * pevent_find_event_by_name - find an event by given name
2931 * @pevent: a handle to the pevent
2932 * @sys: the system name to search for
2933 * @name: the name of the event to search for
2934 *
2935 * This returns an event with a given @name and under the system
2936 * @sys. If @sys is NULL the first event with @name is returned.
2937 */
2938struct event_format *
2939pevent_find_event_by_name(struct pevent *pevent,
2940 const char *sys, const char *name)
2941{
2942 struct event_format *event;
2943 int i;
2944
2945 if (pevent->last_event &&
2946 strcmp(pevent->last_event->name, name) == 0 &&
2947 (!sys || strcmp(pevent->last_event->system, sys) == 0))
2948 return pevent->last_event;
2949
2950 for (i = 0; i < pevent->nr_events; i++) {
2951 event = pevent->events[i];
2952 if (strcmp(event->name, name) == 0) {
2953 if (!sys)
2954 break;
2955 if (strcmp(event->system, sys) == 0)
2956 break;
2957 }
2958 }
2959 if (i == pevent->nr_events)
2960 event = NULL;
2961
2962 pevent->last_event = event;
2963 return event;
2964}
2965
2966static unsigned long long
2967eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
2968{
2969 struct pevent *pevent = event->pevent;
2970 unsigned long long val = 0;
2971 unsigned long long left, right;
2972 struct print_arg *typearg = NULL;
2973 struct print_arg *larg;
2974 unsigned long offset;
2975 unsigned int field_size;
2976
2977 switch (arg->type) {
2978 case PRINT_NULL:
2979 /* ?? */
2980 return 0;
2981 case PRINT_ATOM:
2982 return strtoull(arg->atom.atom, NULL, 0);
2983 case PRINT_FIELD:
2984 if (!arg->field.field) {
2985 arg->field.field = pevent_find_any_field(event, arg->field.name);
2986 if (!arg->field.field)
2987 die("field %s not found", arg->field.name);
2988 }
2989 /* must be a number */
2990 val = pevent_read_number(pevent, data + arg->field.field->offset,
2991 arg->field.field->size);
2992 break;
2993 case PRINT_FLAGS:
2994 case PRINT_SYMBOL:
2995 break;
2996 case PRINT_TYPE:
2997 val = eval_num_arg(data, size, event, arg->typecast.item);
2998 return eval_type(val, arg, 0);
2999 case PRINT_STRING:
3000 case PRINT_BSTRING:
3001 return 0;
3002 case PRINT_FUNC: {
3003 struct trace_seq s;
3004 trace_seq_init(&s);
3005 val = process_defined_func(&s, data, size, event, arg);
3006 trace_seq_destroy(&s);
3007 return val;
3008 }
3009 case PRINT_OP:
3010 if (strcmp(arg->op.op, "[") == 0) {
3011 /*
3012 * Arrays are special, since we don't want
3013 * to read the arg as is.
3014 */
3015 right = eval_num_arg(data, size, event, arg->op.right);
3016
3017 /* handle typecasts */
3018 larg = arg->op.left;
3019 while (larg->type == PRINT_TYPE) {
3020 if (!typearg)
3021 typearg = larg;
3022 larg = larg->typecast.item;
3023 }
3024
3025 /* Default to long size */
3026 field_size = pevent->long_size;
3027
3028 switch (larg->type) {
3029 case PRINT_DYNAMIC_ARRAY:
3030 offset = pevent_read_number(pevent,
3031 data + larg->dynarray.field->offset,
3032 larg->dynarray.field->size);
3033 if (larg->dynarray.field->elementsize)
3034 field_size = larg->dynarray.field->elementsize;
3035 /*
3036 * The actual length of the dynamic array is stored
3037 * in the top half of the field, and the offset
3038 * is in the bottom half of the 32 bit field.
3039 */
3040 offset &= 0xffff;
3041 offset += right;
3042 break;
3043 case PRINT_FIELD:
3044 if (!larg->field.field) {
3045 larg->field.field =
3046 pevent_find_any_field(event, larg->field.name);
3047 if (!larg->field.field)
3048 die("field %s not found", larg->field.name);
3049 }
3050 field_size = larg->field.field->elementsize;
3051 offset = larg->field.field->offset +
3052 right * larg->field.field->elementsize;
3053 break;
3054 default:
3055 goto default_op; /* oops, all bets off */
3056 }
3057 val = pevent_read_number(pevent,
3058 data + offset, field_size);
3059 if (typearg)
3060 val = eval_type(val, typearg, 1);
3061 break;
3062 } else if (strcmp(arg->op.op, "?") == 0) {
3063 left = eval_num_arg(data, size, event, arg->op.left);
3064 arg = arg->op.right;
3065 if (left)
3066 val = eval_num_arg(data, size, event, arg->op.left);
3067 else
3068 val = eval_num_arg(data, size, event, arg->op.right);
3069 break;
3070 }
3071 default_op:
3072 left = eval_num_arg(data, size, event, arg->op.left);
3073 right = eval_num_arg(data, size, event, arg->op.right);
3074 switch (arg->op.op[0]) {
3075 case '!':
3076 switch (arg->op.op[1]) {
3077 case 0:
3078 val = !right;
3079 break;
3080 case '=':
3081 val = left != right;
3082 break;
3083 default:
3084 die("unknown op '%s'", arg->op.op);
3085 }
3086 break;
3087 case '~':
3088 val = ~right;
3089 break;
3090 case '|':
3091 if (arg->op.op[1])
3092 val = left || right;
3093 else
3094 val = left | right;
3095 break;
3096 case '&':
3097 if (arg->op.op[1])
3098 val = left && right;
3099 else
3100 val = left & right;
3101 break;
3102 case '<':
3103 switch (arg->op.op[1]) {
3104 case 0:
3105 val = left < right;
3106 break;
3107 case '<':
3108 val = left << right;
3109 break;
3110 case '=':
3111 val = left <= right;
3112 break;
3113 default:
3114 die("unknown op '%s'", arg->op.op);
3115 }
3116 break;
3117 case '>':
3118 switch (arg->op.op[1]) {
3119 case 0:
3120 val = left > right;
3121 break;
3122 case '>':
3123 val = left >> right;
3124 break;
3125 case '=':
3126 val = left >= right;
3127 break;
3128 default:
3129 die("unknown op '%s'", arg->op.op);
3130 }
3131 break;
3132 case '=':
3133 if (arg->op.op[1] != '=')
3134 die("unknown op '%s'", arg->op.op);
3135 val = left == right;
3136 break;
3137 case '-':
3138 val = left - right;
3139 break;
3140 case '+':
3141 val = left + right;
3142 break;
2e7a5fc8
SR
3143 case '/':
3144 val = left / right;
3145 break;
3146 case '*':
3147 val = left * right;
3148 break;
f7d82350
SR
3149 default:
3150 die("unknown op '%s'", arg->op.op);
3151 }
3152 break;
3153 default: /* not sure what to do there */
3154 return 0;
3155 }
3156 return val;
3157}
3158
3159struct flag {
3160 const char *name;
3161 unsigned long long value;
3162};
3163
3164static const struct flag flags[] = {
3165 { "HI_SOFTIRQ", 0 },
3166 { "TIMER_SOFTIRQ", 1 },
3167 { "NET_TX_SOFTIRQ", 2 },
3168 { "NET_RX_SOFTIRQ", 3 },
3169 { "BLOCK_SOFTIRQ", 4 },
3170 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3171 { "TASKLET_SOFTIRQ", 6 },
3172 { "SCHED_SOFTIRQ", 7 },
3173 { "HRTIMER_SOFTIRQ", 8 },
3174 { "RCU_SOFTIRQ", 9 },
3175
3176 { "HRTIMER_NORESTART", 0 },
3177 { "HRTIMER_RESTART", 1 },
3178};
3179
3180static unsigned long long eval_flag(const char *flag)
3181{
3182 int i;
3183
3184 /*
3185 * Some flags in the format files do not get converted.
3186 * If the flag is not numeric, see if it is something that
3187 * we already know about.
3188 */
3189 if (isdigit(flag[0]))
3190 return strtoull(flag, NULL, 0);
3191
3192 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3193 if (strcmp(flags[i].name, flag) == 0)
3194 return flags[i].value;
3195
3196 return 0;
3197}
3198
3199static void print_str_to_seq(struct trace_seq *s, const char *format,
3200 int len_arg, const char *str)
3201{
3202 if (len_arg >= 0)
3203 trace_seq_printf(s, format, len_arg, str);
3204 else
3205 trace_seq_printf(s, format, str);
3206}
3207
3208static void print_str_arg(struct trace_seq *s, void *data, int size,
3209 struct event_format *event, const char *format,
3210 int len_arg, struct print_arg *arg)
3211{
3212 struct pevent *pevent = event->pevent;
3213 struct print_flag_sym *flag;
3214 unsigned long long val, fval;
3215 unsigned long addr;
3216 char *str;
3217 int print;
3218 int len;
3219
3220 switch (arg->type) {
3221 case PRINT_NULL:
3222 /* ?? */
3223 return;
3224 case PRINT_ATOM:
3225 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3226 return;
3227 case PRINT_FIELD:
3228 if (!arg->field.field) {
3229 arg->field.field = pevent_find_any_field(event, arg->field.name);
3230 if (!arg->field.field)
3231 die("field %s not found", arg->field.name);
3232 }
3233 /* Zero sized fields, mean the rest of the data */
3234 len = arg->field.field->size ? : size - arg->field.field->offset;
3235
3236 /*
3237 * Some events pass in pointers. If this is not an array
3238 * and the size is the same as long_size, assume that it
3239 * is a pointer.
3240 */
3241 if (!(arg->field.field->flags & FIELD_IS_ARRAY) &&
3242 arg->field.field->size == pevent->long_size) {
3243 addr = *(unsigned long *)(data + arg->field.field->offset);
3244 trace_seq_printf(s, "%lx", addr);
3245 break;
3246 }
3247 str = malloc_or_die(len + 1);
3248 memcpy(str, data + arg->field.field->offset, len);
3249 str[len] = 0;
3250 print_str_to_seq(s, format, len_arg, str);
3251 free(str);
3252 break;
3253 case PRINT_FLAGS:
3254 val = eval_num_arg(data, size, event, arg->flags.field);
3255 print = 0;
3256 for (flag = arg->flags.flags; flag; flag = flag->next) {
3257 fval = eval_flag(flag->value);
3258 if (!val && !fval) {
3259 print_str_to_seq(s, format, len_arg, flag->str);
3260 break;
3261 }
3262 if (fval && (val & fval) == fval) {
3263 if (print && arg->flags.delim)
3264 trace_seq_puts(s, arg->flags.delim);
3265 print_str_to_seq(s, format, len_arg, flag->str);
3266 print = 1;
3267 val &= ~fval;
3268 }
3269 }
3270 break;
3271 case PRINT_SYMBOL:
3272 val = eval_num_arg(data, size, event, arg->symbol.field);
3273 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3274 fval = eval_flag(flag->value);
3275 if (val == fval) {
3276 print_str_to_seq(s, format, len_arg, flag->str);
3277 break;
3278 }
3279 }
3280 break;
3281
3282 case PRINT_TYPE:
3283 break;
3284 case PRINT_STRING: {
3285 int str_offset;
3286
3287 if (arg->string.offset == -1) {
3288 struct format_field *f;
3289
3290 f = pevent_find_any_field(event, arg->string.string);
3291 arg->string.offset = f->offset;
3292 }
3293 str_offset = data2host4(pevent, data + arg->string.offset);
3294 str_offset &= 0xffff;
3295 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3296 break;
3297 }
3298 case PRINT_BSTRING:
3299 trace_seq_printf(s, format, arg->string.string);
3300 break;
3301 case PRINT_OP:
3302 /*
3303 * The only op for string should be ? :
3304 */
3305 if (arg->op.op[0] != '?')
3306 return;
3307 val = eval_num_arg(data, size, event, arg->op.left);
3308 if (val)
3309 print_str_arg(s, data, size, event,
3310 format, len_arg, arg->op.right->op.left);
3311 else
3312 print_str_arg(s, data, size, event,
3313 format, len_arg, arg->op.right->op.right);
3314 break;
3315 case PRINT_FUNC:
3316 process_defined_func(s, data, size, event, arg);
3317 break;
3318 default:
3319 /* well... */
3320 break;
3321 }
3322}
3323
3324static unsigned long long
3325process_defined_func(struct trace_seq *s, void *data, int size,
3326 struct event_format *event, struct print_arg *arg)
3327{
3328 struct pevent_function_handler *func_handle = arg->func.func;
3329 struct pevent_func_params *param;
3330 unsigned long long *args;
3331 unsigned long long ret;
3332 struct print_arg *farg;
3333 struct trace_seq str;
3334 struct save_str {
3335 struct save_str *next;
3336 char *str;
3337 } *strings = NULL, *string;
3338 int i;
3339
3340 if (!func_handle->nr_args) {
3341 ret = (*func_handle->func)(s, NULL);
3342 goto out;
3343 }
3344
3345 farg = arg->func.args;
3346 param = func_handle->params;
3347
3348 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3349 for (i = 0; i < func_handle->nr_args; i++) {
3350 switch (param->type) {
3351 case PEVENT_FUNC_ARG_INT:
3352 case PEVENT_FUNC_ARG_LONG:
3353 case PEVENT_FUNC_ARG_PTR:
3354 args[i] = eval_num_arg(data, size, event, farg);
3355 break;
3356 case PEVENT_FUNC_ARG_STRING:
3357 trace_seq_init(&str);
3358 print_str_arg(&str, data, size, event, "%s", -1, farg);
3359 trace_seq_terminate(&str);
3360 string = malloc_or_die(sizeof(*string));
3361 string->next = strings;
3362 string->str = strdup(str.buffer);
3363 strings = string;
3364 trace_seq_destroy(&str);
3365 break;
3366 default:
3367 /*
3368 * Something went totally wrong, this is not
3369 * an input error, something in this code broke.
3370 */
3371 die("Unexpected end of arguments\n");
3372 break;
3373 }
3374 farg = farg->next;
3375 }
3376
3377 ret = (*func_handle->func)(s, args);
3378 free(args);
3379 while (strings) {
3380 string = strings;
3381 strings = string->next;
3382 free(string->str);
3383 free(string);
3384 }
3385
3386 out:
3387 /* TBD : handle return type here */
3388 return ret;
3389}
3390
3391static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3392{
3393 struct pevent *pevent = event->pevent;
3394 struct format_field *field, *ip_field;
3395 struct print_arg *args, *arg, **next;
3396 unsigned long long ip, val;
3397 char *ptr;
3398 void *bptr;
3399
3400 field = pevent->bprint_buf_field;
3401 ip_field = pevent->bprint_ip_field;
3402
3403 if (!field) {
3404 field = pevent_find_field(event, "buf");
3405 if (!field)
3406 die("can't find buffer field for binary printk");
3407 ip_field = pevent_find_field(event, "ip");
3408 if (!ip_field)
3409 die("can't find ip field for binary printk");
3410 pevent->bprint_buf_field = field;
3411 pevent->bprint_ip_field = ip_field;
3412 }
3413
3414 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3415
3416 /*
3417 * The first arg is the IP pointer.
3418 */
3419 args = alloc_arg();
3420 arg = args;
3421 arg->next = NULL;
3422 next = &arg->next;
3423
3424 arg->type = PRINT_ATOM;
3425 arg->atom.atom = malloc_or_die(32);
3426 sprintf(arg->atom.atom, "%lld", ip);
3427
3428 /* skip the first "%pf : " */
3429 for (ptr = fmt + 6, bptr = data + field->offset;
3430 bptr < data + size && *ptr; ptr++) {
3431 int ls = 0;
3432
3433 if (*ptr == '%') {
3434 process_again:
3435 ptr++;
3436 switch (*ptr) {
3437 case '%':
3438 break;
3439 case 'l':
3440 ls++;
3441 goto process_again;
3442 case 'L':
3443 ls = 2;
3444 goto process_again;
3445 case '0' ... '9':
3446 goto process_again;
3447 case 'p':
3448 ls = 1;
3449 /* fall through */
3450 case 'd':
3451 case 'u':
3452 case 'x':
3453 case 'i':
3454 /* the pointers are always 4 bytes aligned */
3455 bptr = (void *)(((unsigned long)bptr + 3) &
3456 ~3);
3457 switch (ls) {
3458 case 0:
3459 ls = 4;
3460 break;
3461 case 1:
3462 ls = pevent->long_size;
3463 break;
3464 case 2:
3465 ls = 8;
3466 default:
3467 break;
3468 }
3469 val = pevent_read_number(pevent, bptr, ls);
3470 bptr += ls;
3471 arg = alloc_arg();
3472 arg->next = NULL;
3473 arg->type = PRINT_ATOM;
3474 arg->atom.atom = malloc_or_die(32);
3475 sprintf(arg->atom.atom, "%lld", val);
3476 *next = arg;
3477 next = &arg->next;
3478 break;
3479 case 's':
3480 arg = alloc_arg();
3481 arg->next = NULL;
3482 arg->type = PRINT_BSTRING;
3483 arg->string.string = strdup(bptr);
3484 bptr += strlen(bptr) + 1;
3485 *next = arg;
3486 next = &arg->next;
3487 default:
3488 break;
3489 }
3490 }
3491 }
3492
3493 return args;
3494}
3495
3496static void free_args(struct print_arg *args)
3497{
3498 struct print_arg *next;
3499
3500 while (args) {
3501 next = args->next;
3502
3503 free_arg(args);
3504 args = next;
3505 }
3506}
3507
3508static char *
3509get_bprint_format(void *data, int size __unused, struct event_format *event)
3510{
3511 struct pevent *pevent = event->pevent;
3512 unsigned long long addr;
3513 struct format_field *field;
3514 struct printk_map *printk;
3515 char *format;
3516 char *p;
3517
3518 field = pevent->bprint_fmt_field;
3519
3520 if (!field) {
3521 field = pevent_find_field(event, "fmt");
3522 if (!field)
3523 die("can't find format field for binary printk");
3524 pevent->bprint_fmt_field = field;
3525 }
3526
3527 addr = pevent_read_number(pevent, data + field->offset, field->size);
3528
3529 printk = find_printk(pevent, addr);
3530 if (!printk) {
3531 format = malloc_or_die(45);
3532 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3533 addr);
3534 return format;
3535 }
3536
3537 p = printk->printk;
3538 /* Remove any quotes. */
3539 if (*p == '"')
3540 p++;
3541 format = malloc_or_die(strlen(p) + 10);
3542 sprintf(format, "%s : %s", "%pf", p);
3543 /* remove ending quotes and new line since we will add one too */
3544 p = format + strlen(format) - 1;
3545 if (*p == '"')
3546 *p = 0;
3547
3548 p -= 2;
3549 if (strcmp(p, "\\n") == 0)
3550 *p = 0;
3551
3552 return format;
3553}
3554
3555static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3556 struct event_format *event, struct print_arg *arg)
3557{
3558 unsigned char *buf;
3559 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3560
3561 if (arg->type == PRINT_FUNC) {
3562 process_defined_func(s, data, size, event, arg);
3563 return;
3564 }
3565
3566 if (arg->type != PRINT_FIELD) {
3567 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3568 arg->type);
3569 return;
3570 }
3571
3572 if (mac == 'm')
3573 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3574 if (!arg->field.field) {
3575 arg->field.field =
3576 pevent_find_any_field(event, arg->field.name);
3577 if (!arg->field.field)
3578 die("field %s not found", arg->field.name);
3579 }
3580 if (arg->field.field->size != 6) {
3581 trace_seq_printf(s, "INVALIDMAC");
3582 return;
3583 }
3584 buf = data + arg->field.field->offset;
3585 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3586}
3587
3588static void print_event_fields(struct trace_seq *s, void *data, int size,
3589 struct event_format *event)
3590{
3591 struct format_field *field;
3592 unsigned long long val;
3593 unsigned int offset, len, i;
3594
3595 field = event->format.fields;
3596 while (field) {
3597 trace_seq_printf(s, " %s=", field->name);
3598 if (field->flags & FIELD_IS_ARRAY) {
3599 offset = field->offset;
3600 len = field->size;
3601 if (field->flags & FIELD_IS_DYNAMIC) {
3602 val = pevent_read_number(event->pevent, data + offset, len);
3603 offset = val;
3604 len = offset >> 16;
3605 offset &= 0xffff;
3606 }
3607 if (field->flags & FIELD_IS_STRING) {
3608 trace_seq_printf(s, "%s", (char *)data + offset);
3609 } else {
3610 trace_seq_puts(s, "ARRAY[");
3611 for (i = 0; i < len; i++) {
3612 if (i)
3613 trace_seq_puts(s, ", ");
3614 trace_seq_printf(s, "%02x",
3615 *((unsigned char *)data + offset + i));
3616 }
3617 trace_seq_putc(s, ']');
3618 }
3619 } else {
3620 val = pevent_read_number(event->pevent, data + field->offset,
3621 field->size);
3622 if (field->flags & FIELD_IS_POINTER) {
3623 trace_seq_printf(s, "0x%llx", val);
3624 } else if (field->flags & FIELD_IS_SIGNED) {
3625 switch (field->size) {
3626 case 4:
3627 /*
3628 * If field is long then print it in hex.
3629 * A long usually stores pointers.
3630 */
3631 if (field->flags & FIELD_IS_LONG)
3632 trace_seq_printf(s, "0x%x", (int)val);
3633 else
3634 trace_seq_printf(s, "%d", (int)val);
3635 break;
3636 case 2:
3637 trace_seq_printf(s, "%2d", (short)val);
3638 break;
3639 case 1:
3640 trace_seq_printf(s, "%1d", (char)val);
3641 break;
3642 default:
3643 trace_seq_printf(s, "%lld", val);
3644 }
3645 } else {
3646 if (field->flags & FIELD_IS_LONG)
3647 trace_seq_printf(s, "0x%llx", val);
3648 else
3649 trace_seq_printf(s, "%llu", val);
3650 }
3651 }
3652 field = field->next;
3653 }
3654}
3655
3656static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3657{
3658 struct pevent *pevent = event->pevent;
3659 struct print_fmt *print_fmt = &event->print_fmt;
3660 struct print_arg *arg = print_fmt->args;
3661 struct print_arg *args = NULL;
3662 const char *ptr = print_fmt->format;
3663 unsigned long long val;
3664 struct func_map *func;
3665 const char *saveptr;
3666 char *bprint_fmt = NULL;
3667 char format[32];
3668 int show_func;
3669 int len_as_arg;
3670 int len_arg;
3671 int len;
3672 int ls;
3673
3674 if (event->flags & EVENT_FL_FAILED) {
3675 trace_seq_printf(s, "[FAILED TO PARSE]");
3676 print_event_fields(s, data, size, event);
3677 return;
3678 }
3679
3680 if (event->flags & EVENT_FL_ISBPRINT) {
3681 bprint_fmt = get_bprint_format(data, size, event);
3682 args = make_bprint_args(bprint_fmt, data, size, event);
3683 arg = args;
3684 ptr = bprint_fmt;
3685 }
3686
3687 for (; *ptr; ptr++) {
3688 ls = 0;
3689 if (*ptr == '\\') {
3690 ptr++;
3691 switch (*ptr) {
3692 case 'n':
3693 trace_seq_putc(s, '\n');
3694 break;
3695 case 't':
3696 trace_seq_putc(s, '\t');
3697 break;
3698 case 'r':
3699 trace_seq_putc(s, '\r');
3700 break;
3701 case '\\':
3702 trace_seq_putc(s, '\\');
3703 break;
3704 default:
3705 trace_seq_putc(s, *ptr);
3706 break;
3707 }
3708
3709 } else if (*ptr == '%') {
3710 saveptr = ptr;
3711 show_func = 0;
3712 len_as_arg = 0;
3713 cont_process:
3714 ptr++;
3715 switch (*ptr) {
3716 case '%':
3717 trace_seq_putc(s, '%');
3718 break;
3719 case '#':
3720 /* FIXME: need to handle properly */
3721 goto cont_process;
3722 case 'h':
3723 ls--;
3724 goto cont_process;
3725 case 'l':
3726 ls++;
3727 goto cont_process;
3728 case 'L':
3729 ls = 2;
3730 goto cont_process;
3731 case '*':
3732 /* The argument is the length. */
3733 if (!arg)
3734 die("no argument match");
3735 len_arg = eval_num_arg(data, size, event, arg);
3736 len_as_arg = 1;
3737 arg = arg->next;
3738 goto cont_process;
3739 case '.':
3740 case 'z':
3741 case 'Z':
3742 case '0' ... '9':
3743 goto cont_process;
3744 case 'p':
3745 if (pevent->long_size == 4)
3746 ls = 1;
3747 else
3748 ls = 2;
3749
3750 if (*(ptr+1) == 'F' ||
3751 *(ptr+1) == 'f') {
3752 ptr++;
3753 show_func = *ptr;
3754 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3755 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3756 ptr++;
3757 break;
3758 }
3759
3760 /* fall through */
3761 case 'd':
3762 case 'i':
3763 case 'x':
3764 case 'X':
3765 case 'u':
3766 if (!arg)
3767 die("no argument match");
3768
3769 len = ((unsigned long)ptr + 1) -
3770 (unsigned long)saveptr;
3771
3772 /* should never happen */
3773 if (len > 31)
3774 die("bad format!");
3775
3776 memcpy(format, saveptr, len);
3777 format[len] = 0;
3778
3779 val = eval_num_arg(data, size, event, arg);
3780 arg = arg->next;
3781
3782 if (show_func) {
3783 func = find_func(pevent, val);
3784 if (func) {
3785 trace_seq_puts(s, func->func);
3786 if (show_func == 'F')
3787 trace_seq_printf(s,
3788 "+0x%llx",
3789 val - func->addr);
3790 break;
3791 }
3792 }
3793 if (pevent->long_size == 8 && ls) {
3794 char *p;
3795
3796 ls = 2;
3797 /* make %l into %ll */
3798 p = strchr(format, 'l');
3799 if (p)
3800 memmove(p, p+1, strlen(p)+1);
3801 else if (strcmp(format, "%p") == 0)
3802 strcpy(format, "0x%llx");
3803 }
3804 switch (ls) {
3805 case -2:
3806 if (len_as_arg)
3807 trace_seq_printf(s, format, len_arg, (char)val);
3808 else
3809 trace_seq_printf(s, format, (char)val);
3810 break;
3811 case -1:
3812 if (len_as_arg)
3813 trace_seq_printf(s, format, len_arg, (short)val);
3814 else
3815 trace_seq_printf(s, format, (short)val);
3816 break;
3817 case 0:
3818 if (len_as_arg)
3819 trace_seq_printf(s, format, len_arg, (int)val);
3820 else
3821 trace_seq_printf(s, format, (int)val);
3822 break;
3823 case 1:
3824 if (len_as_arg)
3825 trace_seq_printf(s, format, len_arg, (long)val);
3826 else
3827 trace_seq_printf(s, format, (long)val);
3828 break;
3829 case 2:
3830 if (len_as_arg)
3831 trace_seq_printf(s, format, len_arg,
3832 (long long)val);
3833 else
3834 trace_seq_printf(s, format, (long long)val);
3835 break;
3836 default:
3837 die("bad count (%d)", ls);
3838 }
3839 break;
3840 case 's':
3841 if (!arg)
3842 die("no matching argument");
3843
3844 len = ((unsigned long)ptr + 1) -
3845 (unsigned long)saveptr;
3846
3847 /* should never happen */
3848 if (len > 31)
3849 die("bad format!");
3850
3851 memcpy(format, saveptr, len);
3852 format[len] = 0;
3853 if (!len_as_arg)
3854 len_arg = -1;
3855 print_str_arg(s, data, size, event,
3856 format, len_arg, arg);
3857 arg = arg->next;
3858 break;
3859 default:
3860 trace_seq_printf(s, ">%c<", *ptr);
3861
3862 }
3863 } else
3864 trace_seq_putc(s, *ptr);
3865 }
3866
3867 if (args) {
3868 free_args(args);
3869 free(bprint_fmt);
3870 }
3871}
3872
3873/**
3874 * pevent_data_lat_fmt - parse the data for the latency format
3875 * @pevent: a handle to the pevent
3876 * @s: the trace_seq to write to
3877 * @data: the raw data to read from
3878 * @size: currently unused.
3879 *
3880 * This parses out the Latency format (interrupts disabled,
3881 * need rescheduling, in hard/soft interrupt, preempt count
3882 * and lock depth) and places it into the trace_seq.
3883 */
3884void pevent_data_lat_fmt(struct pevent *pevent,
1c698186 3885 struct trace_seq *s, struct pevent_record *record)
f7d82350
SR
3886{
3887 static int check_lock_depth = 1;
3888 static int lock_depth_exists;
3889 unsigned int lat_flags;
3890 unsigned int pc;
3891 int lock_depth;
3892 int hardirq;
3893 int softirq;
3894 void *data = record->data;
3895
3896 lat_flags = parse_common_flags(pevent, data);
3897 pc = parse_common_pc(pevent, data);
3898 /* lock_depth may not always exist */
3899 if (check_lock_depth) {
3900 struct format_field *field;
3901 struct event_format *event;
3902
3903 check_lock_depth = 0;
3904 event = pevent->events[0];
3905 field = pevent_find_common_field(event, "common_lock_depth");
3906 if (field)
3907 lock_depth_exists = 1;
3908 }
3909 if (lock_depth_exists)
3910 lock_depth = parse_common_lock_depth(pevent, data);
3911
3912 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
3913 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
3914
3915 trace_seq_printf(s, "%c%c%c",
3916 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
3917 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
3918 'X' : '.',
3919 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
3920 'N' : '.',
3921 (hardirq && softirq) ? 'H' :
3922 hardirq ? 'h' : softirq ? 's' : '.');
3923
3924 if (pc)
3925 trace_seq_printf(s, "%x", pc);
3926 else
3927 trace_seq_putc(s, '.');
3928
3929 if (lock_depth_exists) {
3930 if (lock_depth < 0)
3931 trace_seq_putc(s, '.');
3932 else
3933 trace_seq_printf(s, "%d", lock_depth);
3934 }
3935
3936 trace_seq_terminate(s);
3937}
3938
3939/**
3940 * pevent_data_type - parse out the given event type
3941 * @pevent: a handle to the pevent
3942 * @rec: the record to read from
3943 *
3944 * This returns the event id from the @rec.
3945 */
1c698186 3946int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
f7d82350
SR
3947{
3948 return trace_parse_common_type(pevent, rec->data);
3949}
3950
3951/**
3952 * pevent_data_event_from_type - find the event by a given type
3953 * @pevent: a handle to the pevent
3954 * @type: the type of the event.
3955 *
3956 * This returns the event form a given @type;
3957 */
3958struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
3959{
3960 return pevent_find_event(pevent, type);
3961}
3962
3963/**
3964 * pevent_data_pid - parse the PID from raw data
3965 * @pevent: a handle to the pevent
3966 * @rec: the record to parse
3967 *
3968 * This returns the PID from a raw data.
3969 */
1c698186 3970int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
f7d82350
SR
3971{
3972 return parse_common_pid(pevent, rec->data);
3973}
3974
3975/**
3976 * pevent_data_comm_from_pid - return the command line from PID
3977 * @pevent: a handle to the pevent
3978 * @pid: the PID of the task to search for
3979 *
3980 * This returns a pointer to the command line that has the given
3981 * @pid.
3982 */
3983const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
3984{
3985 const char *comm;
3986
3987 comm = find_cmdline(pevent, pid);
3988 return comm;
3989}
3990
3991/**
3992 * pevent_data_comm_from_pid - parse the data into the print format
3993 * @s: the trace_seq to write to
3994 * @event: the handle to the event
3995 * @cpu: the cpu the event was recorded on
3996 * @data: the raw data
3997 * @size: the size of the raw data
3998 * @nsecs: the timestamp of the event
3999 *
4000 * This parses the raw @data using the given @event information and
4001 * writes the print format into the trace_seq.
4002 */
4003void pevent_event_info(struct trace_seq *s, struct event_format *event,
1c698186 4004 struct pevent_record *record)
f7d82350
SR
4005{
4006 int print_pretty = 1;
4007
4008 if (event->pevent->print_raw)
4009 print_event_fields(s, record->data, record->size, event);
4010 else {
4011
4012 if (event->handler)
4013 print_pretty = event->handler(s, record, event,
4014 event->context);
4015
4016 if (print_pretty)
4017 pretty_print(s, record->data, record->size, event);
4018 }
4019
4020 trace_seq_terminate(s);
4021}
4022
4023void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
1c698186 4024 struct pevent_record *record)
f7d82350
SR
4025{
4026 static char *spaces = " "; /* 20 spaces */
4027 struct event_format *event;
4028 unsigned long secs;
4029 unsigned long usecs;
4dc1024a 4030 unsigned long nsecs;
f7d82350
SR
4031 const char *comm;
4032 void *data = record->data;
4033 int type;
4034 int pid;
4035 int len;
4dc1024a 4036 int p;
f7d82350
SR
4037
4038 secs = record->ts / NSECS_PER_SEC;
4dc1024a 4039 nsecs = record->ts - secs * NSECS_PER_SEC;
f7d82350
SR
4040
4041 if (record->size < 0) {
4042 do_warning("ug! negative record size %d", record->size);
4043 return;
4044 }
4045
4046 type = trace_parse_common_type(pevent, data);
4047
4048 event = pevent_find_event(pevent, type);
4049 if (!event) {
4050 do_warning("ug! no event found for type %d", type);
4051 return;
4052 }
4053
4054 pid = parse_common_pid(pevent, data);
4055 comm = find_cmdline(pevent, pid);
4056
4057 if (pevent->latency_format) {
4058 trace_seq_printf(s, "%8.8s-%-5d %3d",
4059 comm, pid, record->cpu);
4060 pevent_data_lat_fmt(pevent, s, record);
4061 } else
4062 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4063
4dc1024a
SR
4064 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4065 usecs = nsecs;
4066 p = 9;
4067 } else {
4068 usecs = (nsecs + 500) / NSECS_PER_USEC;
4069 p = 6;
4070 }
4071
4072 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
f7d82350
SR
4073
4074 /* Space out the event names evenly. */
4075 len = strlen(event->name);
4076 if (len < 20)
4077 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4078
4079 pevent_event_info(s, event, record);
4080}
4081
4082static int events_id_cmp(const void *a, const void *b)
4083{
4084 struct event_format * const * ea = a;
4085 struct event_format * const * eb = b;
4086
4087 if ((*ea)->id < (*eb)->id)
4088 return -1;
4089
4090 if ((*ea)->id > (*eb)->id)
4091 return 1;
4092
4093 return 0;
4094}
4095
4096static int events_name_cmp(const void *a, const void *b)
4097{
4098 struct event_format * const * ea = a;
4099 struct event_format * const * eb = b;
4100 int res;
4101
4102 res = strcmp((*ea)->name, (*eb)->name);
4103 if (res)
4104 return res;
4105
4106 res = strcmp((*ea)->system, (*eb)->system);
4107 if (res)
4108 return res;
4109
4110 return events_id_cmp(a, b);
4111}
4112
4113static int events_system_cmp(const void *a, const void *b)
4114{
4115 struct event_format * const * ea = a;
4116 struct event_format * const * eb = b;
4117 int res;
4118
4119 res = strcmp((*ea)->system, (*eb)->system);
4120 if (res)
4121 return res;
4122
4123 res = strcmp((*ea)->name, (*eb)->name);
4124 if (res)
4125 return res;
4126
4127 return events_id_cmp(a, b);
4128}
4129
4130struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4131{
4132 struct event_format **events;
4133 int (*sort)(const void *a, const void *b);
4134
4135 events = pevent->sort_events;
4136
4137 if (events && pevent->last_type == sort_type)
4138 return events;
4139
4140 if (!events) {
4141 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4142 if (!events)
4143 return NULL;
4144
4145 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4146 events[pevent->nr_events] = NULL;
4147
4148 pevent->sort_events = events;
4149
4150 /* the internal events are sorted by id */
4151 if (sort_type == EVENT_SORT_ID) {
4152 pevent->last_type = sort_type;
4153 return events;
4154 }
4155 }
4156
4157 switch (sort_type) {
4158 case EVENT_SORT_ID:
4159 sort = events_id_cmp;
4160 break;
4161 case EVENT_SORT_NAME:
4162 sort = events_name_cmp;
4163 break;
4164 case EVENT_SORT_SYSTEM:
4165 sort = events_system_cmp;
4166 break;
4167 default:
4168 return events;
4169 }
4170
4171 qsort(events, pevent->nr_events, sizeof(*events), sort);
4172 pevent->last_type = sort_type;
4173
4174 return events;
4175}
4176
4177static struct format_field **
4178get_event_fields(const char *type, const char *name,
4179 int count, struct format_field *list)
4180{
4181 struct format_field **fields;
4182 struct format_field *field;
4183 int i = 0;
4184
4185 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4186 for (field = list; field; field = field->next) {
4187 fields[i++] = field;
4188 if (i == count + 1) {
4189 do_warning("event %s has more %s fields than specified",
4190 name, type);
4191 i--;
4192 break;
4193 }
4194 }
4195
4196 if (i != count)
4197 do_warning("event %s has less %s fields than specified",
4198 name, type);
4199
4200 fields[i] = NULL;
4201
4202 return fields;
4203}
4204
4205/**
4206 * pevent_event_common_fields - return a list of common fields for an event
4207 * @event: the event to return the common fields of.
4208 *
4209 * Returns an allocated array of fields. The last item in the array is NULL.
4210 * The array must be freed with free().
4211 */
4212struct format_field **pevent_event_common_fields(struct event_format *event)
4213{
4214 return get_event_fields("common", event->name,
4215 event->format.nr_common,
4216 event->format.common_fields);
4217}
4218
4219/**
4220 * pevent_event_fields - return a list of event specific fields for an event
4221 * @event: the event to return the fields of.
4222 *
4223 * Returns an allocated array of fields. The last item in the array is NULL.
4224 * The array must be freed with free().
4225 */
4226struct format_field **pevent_event_fields(struct event_format *event)
4227{
4228 return get_event_fields("event", event->name,
4229 event->format.nr_fields,
4230 event->format.fields);
4231}
4232
4233static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4234{
4235 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4236 if (field->next) {
4237 trace_seq_puts(s, ", ");
4238 print_fields(s, field->next);
4239 }
4240}
4241
4242/* for debugging */
4243static void print_args(struct print_arg *args)
4244{
4245 int print_paren = 1;
4246 struct trace_seq s;
4247
4248 switch (args->type) {
4249 case PRINT_NULL:
4250 printf("null");
4251 break;
4252 case PRINT_ATOM:
4253 printf("%s", args->atom.atom);
4254 break;
4255 case PRINT_FIELD:
4256 printf("REC->%s", args->field.name);
4257 break;
4258 case PRINT_FLAGS:
4259 printf("__print_flags(");
4260 print_args(args->flags.field);
4261 printf(", %s, ", args->flags.delim);
4262 trace_seq_init(&s);
4263 print_fields(&s, args->flags.flags);
4264 trace_seq_do_printf(&s);
4265 trace_seq_destroy(&s);
4266 printf(")");
4267 break;
4268 case PRINT_SYMBOL:
4269 printf("__print_symbolic(");
4270 print_args(args->symbol.field);
4271 printf(", ");
4272 trace_seq_init(&s);
4273 print_fields(&s, args->symbol.symbols);
4274 trace_seq_do_printf(&s);
4275 trace_seq_destroy(&s);
4276 printf(")");
4277 break;
4278 case PRINT_STRING:
4279 case PRINT_BSTRING:
4280 printf("__get_str(%s)", args->string.string);
4281 break;
4282 case PRINT_TYPE:
4283 printf("(%s)", args->typecast.type);
4284 print_args(args->typecast.item);
4285 break;
4286 case PRINT_OP:
4287 if (strcmp(args->op.op, ":") == 0)
4288 print_paren = 0;
4289 if (print_paren)
4290 printf("(");
4291 print_args(args->op.left);
4292 printf(" %s ", args->op.op);
4293 print_args(args->op.right);
4294 if (print_paren)
4295 printf(")");
4296 break;
4297 default:
4298 /* we should warn... */
4299 return;
4300 }
4301 if (args->next) {
4302 printf("\n");
4303 print_args(args->next);
4304 }
4305}
4306
4307static void parse_header_field(const char *field,
4308 int *offset, int *size, int mandatory)
4309{
4310 unsigned long long save_input_buf_ptr;
4311 unsigned long long save_input_buf_siz;
4312 char *token;
4313 int type;
4314
4315 save_input_buf_ptr = input_buf_ptr;
4316 save_input_buf_siz = input_buf_siz;
4317
4318 if (read_expected(EVENT_ITEM, "field") < 0)
4319 return;
4320 if (read_expected(EVENT_OP, ":") < 0)
4321 return;
4322
4323 /* type */
4324 if (read_expect_type(EVENT_ITEM, &token) < 0)
4325 goto fail;
4326 free_token(token);
4327
4328 /*
4329 * If this is not a mandatory field, then test it first.
4330 */
4331 if (mandatory) {
4332 if (read_expected(EVENT_ITEM, field) < 0)
4333 return;
4334 } else {
4335 if (read_expect_type(EVENT_ITEM, &token) < 0)
4336 goto fail;
4337 if (strcmp(token, field) != 0)
4338 goto discard;
4339 free_token(token);
4340 }
4341
4342 if (read_expected(EVENT_OP, ";") < 0)
4343 return;
4344 if (read_expected(EVENT_ITEM, "offset") < 0)
4345 return;
4346 if (read_expected(EVENT_OP, ":") < 0)
4347 return;
4348 if (read_expect_type(EVENT_ITEM, &token) < 0)
4349 goto fail;
4350 *offset = atoi(token);
4351 free_token(token);
4352 if (read_expected(EVENT_OP, ";") < 0)
4353 return;
4354 if (read_expected(EVENT_ITEM, "size") < 0)
4355 return;
4356 if (read_expected(EVENT_OP, ":") < 0)
4357 return;
4358 if (read_expect_type(EVENT_ITEM, &token) < 0)
4359 goto fail;
4360 *size = atoi(token);
4361 free_token(token);
4362 if (read_expected(EVENT_OP, ";") < 0)
4363 return;
4364 type = read_token(&token);
4365 if (type != EVENT_NEWLINE) {
4366 /* newer versions of the kernel have a "signed" type */
4367 if (type != EVENT_ITEM)
4368 goto fail;
4369
4370 if (strcmp(token, "signed") != 0)
4371 goto fail;
4372
4373 free_token(token);
4374
4375 if (read_expected(EVENT_OP, ":") < 0)
4376 return;
4377
4378 if (read_expect_type(EVENT_ITEM, &token))
4379 goto fail;
4380
4381 free_token(token);
4382 if (read_expected(EVENT_OP, ";") < 0)
4383 return;
4384
4385 if (read_expect_type(EVENT_NEWLINE, &token))
4386 goto fail;
4387 }
4388 fail:
4389 free_token(token);
4390 return;
4391
4392 discard:
4393 input_buf_ptr = save_input_buf_ptr;
4394 input_buf_siz = save_input_buf_siz;
4395 *offset = 0;
4396 *size = 0;
4397 free_token(token);
4398}
4399
4400/**
4401 * pevent_parse_header_page - parse the data stored in the header page
4402 * @pevent: the handle to the pevent
4403 * @buf: the buffer storing the header page format string
4404 * @size: the size of @buf
4405 * @long_size: the long size to use if there is no header
4406 *
4407 * This parses the header page format for information on the
4408 * ring buffer used. The @buf should be copied from
4409 *
4410 * /sys/kernel/debug/tracing/events/header_page
4411 */
4412int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4413 int long_size)
4414{
4415 int ignore;
4416
4417 if (!size) {
4418 /*
4419 * Old kernels did not have header page info.
4420 * Sorry but we just use what we find here in user space.
4421 */
4422 pevent->header_page_ts_size = sizeof(long long);
4423 pevent->header_page_size_size = long_size;
4424 pevent->header_page_data_offset = sizeof(long long) + long_size;
4425 pevent->old_format = 1;
4426 return -1;
4427 }
4428 init_input_buf(buf, size);
4429
4430 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4431 &pevent->header_page_ts_size, 1);
4432 parse_header_field("commit", &pevent->header_page_size_offset,
4433 &pevent->header_page_size_size, 1);
4434 parse_header_field("overwrite", &pevent->header_page_overwrite,
4435 &ignore, 0);
4436 parse_header_field("data", &pevent->header_page_data_offset,
4437 &pevent->header_page_data_size, 1);
4438
4439 return 0;
4440}
4441
4442static int event_matches(struct event_format *event,
4443 int id, const char *sys_name,
4444 const char *event_name)
4445{
4446 if (id >= 0 && id != event->id)
4447 return 0;
4448
4449 if (event_name && (strcmp(event_name, event->name) != 0))
4450 return 0;
4451
4452 if (sys_name && (strcmp(sys_name, event->system) != 0))
4453 return 0;
4454
4455 return 1;
4456}
4457
4458static void free_handler(struct event_handler *handle)
4459{
4460 free((void *)handle->sys_name);
4461 free((void *)handle->event_name);
4462 free(handle);
4463}
4464
4465static int find_event_handle(struct pevent *pevent, struct event_format *event)
4466{
4467 struct event_handler *handle, **next;
4468
4469 for (next = &pevent->handlers; *next;
4470 next = &(*next)->next) {
4471 handle = *next;
4472 if (event_matches(event, handle->id,
4473 handle->sys_name,
4474 handle->event_name))
4475 break;
4476 }
4477
4478 if (!(*next))
4479 return 0;
4480
4481 pr_stat("overriding event (%d) %s:%s with new print handler",
4482 event->id, event->system, event->name);
4483
4484 event->handler = handle->func;
4485 event->context = handle->context;
4486
4487 *next = handle->next;
4488 free_handler(handle);
4489
4490 return 1;
4491}
4492
4493/**
4494 * pevent_parse_event - parse the event format
4495 * @pevent: the handle to the pevent
4496 * @buf: the buffer storing the event format string
4497 * @size: the size of @buf
4498 * @sys: the system the event belongs to
4499 *
4500 * This parses the event format and creates an event structure
4501 * to quickly parse raw data for a given event.
4502 *
4503 * These files currently come from:
4504 *
4505 * /sys/kernel/debug/tracing/events/.../.../format
4506 */
4507int pevent_parse_event(struct pevent *pevent,
4508 const char *buf, unsigned long size,
4509 const char *sys)
4510{
4511 struct event_format *event;
4512 int ret;
4513
4514 init_input_buf(buf, size);
4515
4516 event = alloc_event();
4517 if (!event)
4518 return -ENOMEM;
4519
4520 event->name = event_read_name();
4521 if (!event->name) {
4522 /* Bad event? */
4523 free(event);
4524 return -1;
4525 }
4526
4527 if (strcmp(sys, "ftrace") == 0) {
4528
4529 event->flags |= EVENT_FL_ISFTRACE;
4530
4531 if (strcmp(event->name, "bprint") == 0)
4532 event->flags |= EVENT_FL_ISBPRINT;
4533 }
4534
4535 event->id = event_read_id();
4536 if (event->id < 0)
4537 die("failed to read event id");
4538
4539 event->system = strdup(sys);
4540
4541 /* Add pevent to event so that it can be referenced */
4542 event->pevent = pevent;
4543
4544 ret = event_read_format(event);
4545 if (ret < 0) {
4546 do_warning("failed to read event format for %s", event->name);
4547 goto event_failed;
4548 }
4549
4550 /*
4551 * If the event has an override, don't print warnings if the event
4552 * print format fails to parse.
4553 */
4554 if (find_event_handle(pevent, event))
4555 show_warning = 0;
4556
4557 ret = event_read_print(event);
4558 if (ret < 0) {
4559 do_warning("failed to read event print fmt for %s",
4560 event->name);
4561 show_warning = 1;
4562 goto event_failed;
4563 }
4564 show_warning = 1;
4565
4566 add_event(pevent, event);
4567
4568 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4569 struct format_field *field;
4570 struct print_arg *arg, **list;
4571
4572 /* old ftrace had no args */
4573
4574 list = &event->print_fmt.args;
4575 for (field = event->format.fields; field; field = field->next) {
4576 arg = alloc_arg();
4577 *list = arg;
4578 list = &arg->next;
4579 arg->type = PRINT_FIELD;
4580 arg->field.name = strdup(field->name);
4581 arg->field.field = field;
4582 }
4583 return 0;
4584 }
4585
4586#define PRINT_ARGS 0
4587 if (PRINT_ARGS && event->print_fmt.args)
4588 print_args(event->print_fmt.args);
4589
4590 return 0;
4591
4592 event_failed:
4593 event->flags |= EVENT_FL_FAILED;
4594 /* still add it even if it failed */
4595 add_event(pevent, event);
4596 return -1;
4597}
4598
4599int get_field_val(struct trace_seq *s, struct format_field *field,
1c698186 4600 const char *name, struct pevent_record *record,
f7d82350
SR
4601 unsigned long long *val, int err)
4602{
4603 if (!field) {
4604 if (err)
4605 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4606 return -1;
4607 }
4608
4609 if (pevent_read_number_field(field, record->data, val)) {
4610 if (err)
4611 trace_seq_printf(s, " %s=INVALID", name);
4612 return -1;
4613 }
4614
4615 return 0;
4616}
4617
4618/**
4619 * pevent_get_field_raw - return the raw pointer into the data field
4620 * @s: The seq to print to on error
4621 * @event: the event that the field is for
4622 * @name: The name of the field
4623 * @record: The record with the field name.
4624 * @len: place to store the field length.
4625 * @err: print default error if failed.
4626 *
4627 * Returns a pointer into record->data of the field and places
4628 * the length of the field in @len.
4629 *
4630 * On failure, it returns NULL.
4631 */
4632void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
1c698186 4633 const char *name, struct pevent_record *record,
f7d82350
SR
4634 int *len, int err)
4635{
4636 struct format_field *field;
4637 void *data = record->data;
4638 unsigned offset;
4639 int dummy;
4640
4641 if (!event)
4642 return NULL;
4643
4644 field = pevent_find_field(event, name);
4645
4646 if (!field) {
4647 if (err)
4648 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4649 return NULL;
4650 }
4651
4652 /* Allow @len to be NULL */
4653 if (!len)
4654 len = &dummy;
4655
4656 offset = field->offset;
4657 if (field->flags & FIELD_IS_DYNAMIC) {
4658 offset = pevent_read_number(event->pevent,
4659 data + offset, field->size);
4660 *len = offset >> 16;
4661 offset &= 0xffff;
4662 } else
4663 *len = field->size;
4664
4665 return data + offset;
4666}
4667
4668/**
4669 * pevent_get_field_val - find a field and return its value
4670 * @s: The seq to print to on error
4671 * @event: the event that the field is for
4672 * @name: The name of the field
4673 * @record: The record with the field name.
4674 * @val: place to store the value of the field.
4675 * @err: print default error if failed.
4676 *
4677 * Returns 0 on success -1 on field not found.
4678 */
4679int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
1c698186 4680 const char *name, struct pevent_record *record,
f7d82350
SR
4681 unsigned long long *val, int err)
4682{
4683 struct format_field *field;
4684
4685 if (!event)
4686 return -1;
4687
4688 field = pevent_find_field(event, name);
4689
4690 return get_field_val(s, field, name, record, val, err);
4691}
4692
4693/**
4694 * pevent_get_common_field_val - find a common field and return its value
4695 * @s: The seq to print to on error
4696 * @event: the event that the field is for
4697 * @name: The name of the field
4698 * @record: The record with the field name.
4699 * @val: place to store the value of the field.
4700 * @err: print default error if failed.
4701 *
4702 * Returns 0 on success -1 on field not found.
4703 */
4704int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
1c698186 4705 const char *name, struct pevent_record *record,
f7d82350
SR
4706 unsigned long long *val, int err)
4707{
4708 struct format_field *field;
4709
4710 if (!event)
4711 return -1;
4712
4713 field = pevent_find_common_field(event, name);
4714
4715 return get_field_val(s, field, name, record, val, err);
4716}
4717
4718/**
4719 * pevent_get_any_field_val - find a any field and return its value
4720 * @s: The seq to print to on error
4721 * @event: the event that the field is for
4722 * @name: The name of the field
4723 * @record: The record with the field name.
4724 * @val: place to store the value of the field.
4725 * @err: print default error if failed.
4726 *
4727 * Returns 0 on success -1 on field not found.
4728 */
4729int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
1c698186 4730 const char *name, struct pevent_record *record,
f7d82350
SR
4731 unsigned long long *val, int err)
4732{
4733 struct format_field *field;
4734
4735 if (!event)
4736 return -1;
4737
4738 field = pevent_find_any_field(event, name);
4739
4740 return get_field_val(s, field, name, record, val, err);
4741}
4742
4743/**
4744 * pevent_print_num_field - print a field and a format
4745 * @s: The seq to print to
4746 * @fmt: The printf format to print the field with.
4747 * @event: the event that the field is for
4748 * @name: The name of the field
4749 * @record: The record with the field name.
4750 * @err: print default error if failed.
4751 *
4752 * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
4753 */
4754int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4755 struct event_format *event, const char *name,
1c698186 4756 struct pevent_record *record, int err)
f7d82350
SR
4757{
4758 struct format_field *field = pevent_find_field(event, name);
4759 unsigned long long val;
4760
4761 if (!field)
4762 goto failed;
4763
4764 if (pevent_read_number_field(field, record->data, &val))
4765 goto failed;
4766
4767 return trace_seq_printf(s, fmt, val);
4768
4769 failed:
4770 if (err)
4771 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
4772 return -1;
4773}
4774
4775static void free_func_handle(struct pevent_function_handler *func)
4776{
4777 struct pevent_func_params *params;
4778
4779 free(func->name);
4780
4781 while (func->params) {
4782 params = func->params;
4783 func->params = params->next;
4784 free(params);
4785 }
4786
4787 free(func);
4788}
4789
4790/**
4791 * pevent_register_print_function - register a helper function
4792 * @pevent: the handle to the pevent
4793 * @func: the function to process the helper function
4794 * @name: the name of the helper function
4795 * @parameters: A list of enum pevent_func_arg_type
4796 *
4797 * Some events may have helper functions in the print format arguments.
4798 * This allows a plugin to dynmically create a way to process one
4799 * of these functions.
4800 *
4801 * The @parameters is a variable list of pevent_func_arg_type enums that
4802 * must end with PEVENT_FUNC_ARG_VOID.
4803 */
4804int pevent_register_print_function(struct pevent *pevent,
4805 pevent_func_handler func,
4806 enum pevent_func_arg_type ret_type,
4807 char *name, ...)
4808{
4809 struct pevent_function_handler *func_handle;
4810 struct pevent_func_params **next_param;
4811 struct pevent_func_params *param;
4812 enum pevent_func_arg_type type;
4813 va_list ap;
4814
4815 func_handle = find_func_handler(pevent, name);
4816 if (func_handle) {
4817 /*
4818 * This is most like caused by the users own
4819 * plugins updating the function. This overrides the
4820 * system defaults.
4821 */
4822 pr_stat("override of function helper '%s'", name);
4823 remove_func_handler(pevent, name);
4824 }
4825
4826 func_handle = malloc_or_die(sizeof(*func_handle));
4827 memset(func_handle, 0, sizeof(*func_handle));
4828
4829 func_handle->ret_type = ret_type;
4830 func_handle->name = strdup(name);
4831 func_handle->func = func;
4832 if (!func_handle->name)
4833 die("Failed to allocate function name");
4834
4835 next_param = &(func_handle->params);
4836 va_start(ap, name);
4837 for (;;) {
4838 type = va_arg(ap, enum pevent_func_arg_type);
4839 if (type == PEVENT_FUNC_ARG_VOID)
4840 break;
4841
4842 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
4843 warning("Invalid argument type %d", type);
4844 goto out_free;
4845 }
4846
4847 param = malloc_or_die(sizeof(*param));
4848 param->type = type;
4849 param->next = NULL;
4850
4851 *next_param = param;
4852 next_param = &(param->next);
4853
4854 func_handle->nr_args++;
4855 }
4856 va_end(ap);
4857
4858 func_handle->next = pevent->func_handlers;
4859 pevent->func_handlers = func_handle;
4860
4861 return 0;
4862 out_free:
4863 va_end(ap);
4864 free_func_handle(func_handle);
4865 return -1;
4866}
4867
4868/**
4869 * pevent_register_event_handle - register a way to parse an event
4870 * @pevent: the handle to the pevent
4871 * @id: the id of the event to register
4872 * @sys_name: the system name the event belongs to
4873 * @event_name: the name of the event
4874 * @func: the function to call to parse the event information
4875 *
4876 * This function allows a developer to override the parsing of
4877 * a given event. If for some reason the default print format
4878 * is not sufficient, this function will register a function
4879 * for an event to be used to parse the data instead.
4880 *
4881 * If @id is >= 0, then it is used to find the event.
4882 * else @sys_name and @event_name are used.
4883 */
4884int pevent_register_event_handler(struct pevent *pevent,
4885 int id, char *sys_name, char *event_name,
4886 pevent_event_handler_func func,
4887 void *context)
4888{
4889 struct event_format *event;
4890 struct event_handler *handle;
4891
4892 if (id >= 0) {
4893 /* search by id */
4894 event = pevent_find_event(pevent, id);
4895 if (!event)
4896 goto not_found;
4897 if (event_name && (strcmp(event_name, event->name) != 0))
4898 goto not_found;
4899 if (sys_name && (strcmp(sys_name, event->system) != 0))
4900 goto not_found;
4901 } else {
4902 event = pevent_find_event_by_name(pevent, sys_name, event_name);
4903 if (!event)
4904 goto not_found;
4905 }
4906
4907 pr_stat("overriding event (%d) %s:%s with new print handler",
4908 event->id, event->system, event->name);
4909
4910 event->handler = func;
4911 event->context = context;
4912 return 0;
4913
4914 not_found:
4915 /* Save for later use. */
4916 handle = malloc_or_die(sizeof(*handle));
42c80139 4917 memset(handle, 0, sizeof(*handle));
f7d82350
SR
4918 handle->id = id;
4919 if (event_name)
4920 handle->event_name = strdup(event_name);
4921 if (sys_name)
4922 handle->sys_name = strdup(sys_name);
4923
4924 handle->func = func;
4925 handle->next = pevent->handlers;
4926 pevent->handlers = handle;
4927 handle->context = context;
4928
4929 return -1;
4930}
4931
4932/**
4933 * pevent_alloc - create a pevent handle
4934 */
4935struct pevent *pevent_alloc(void)
4936{
4937 struct pevent *pevent;
4938
4939 pevent = malloc(sizeof(*pevent));
4940 if (!pevent)
4941 return NULL;
4942 memset(pevent, 0, sizeof(*pevent));
4943 pevent->ref_count = 1;
4944
4945 return pevent;
4946}
4947
4948void pevent_ref(struct pevent *pevent)
4949{
4950 pevent->ref_count++;
4951}
4952
4953static void free_format_fields(struct format_field *field)
4954{
4955 struct format_field *next;
4956
4957 while (field) {
4958 next = field->next;
4959 free(field->type);
4960 free(field->name);
4961 free(field);
4962 field = next;
4963 }
4964}
4965
4966static void free_formats(struct format *format)
4967{
4968 free_format_fields(format->common_fields);
4969 free_format_fields(format->fields);
4970}
4971
4972static void free_event(struct event_format *event)
4973{
4974 free(event->name);
4975 free(event->system);
4976
4977 free_formats(&event->format);
4978
4979 free(event->print_fmt.format);
4980 free_args(event->print_fmt.args);
4981
4982 free(event);
4983}
4984
4985/**
4986 * pevent_free - free a pevent handle
4987 * @pevent: the pevent handle to free
4988 */
4989void pevent_free(struct pevent *pevent)
4990{
a2525a08
SR
4991 struct cmdline_list *cmdlist, *cmdnext;
4992 struct func_list *funclist, *funcnext;
4993 struct printk_list *printklist, *printknext;
f7d82350
SR
4994 struct pevent_function_handler *func_handler;
4995 struct event_handler *handle;
4996 int i;
4997
a2525a08
SR
4998 if (!pevent)
4999 return;
5000
5001 cmdlist = pevent->cmdlist;
5002 funclist = pevent->funclist;
5003 printklist = pevent->printklist;
5004
f7d82350
SR
5005 pevent->ref_count--;
5006 if (pevent->ref_count)
5007 return;
5008
5009 if (pevent->cmdlines) {
5010 for (i = 0; i < pevent->cmdline_count; i++)
5011 free(pevent->cmdlines[i].comm);
5012 free(pevent->cmdlines);
5013 }
5014
5015 while (cmdlist) {
5016 cmdnext = cmdlist->next;
5017 free(cmdlist->comm);
5018 free(cmdlist);
5019 cmdlist = cmdnext;
5020 }
5021
5022 if (pevent->func_map) {
5023 for (i = 0; i < pevent->func_count; i++) {
5024 free(pevent->func_map[i].func);
5025 free(pevent->func_map[i].mod);
5026 }
5027 free(pevent->func_map);
5028 }
5029
5030 while (funclist) {
5031 funcnext = funclist->next;
5032 free(funclist->func);
5033 free(funclist->mod);
5034 free(funclist);
5035 funclist = funcnext;
5036 }
5037
5038 while (pevent->func_handlers) {
5039 func_handler = pevent->func_handlers;
5040 pevent->func_handlers = func_handler->next;
5041 free_func_handle(func_handler);
5042 }
5043
5044 if (pevent->printk_map) {
5045 for (i = 0; i < pevent->printk_count; i++)
5046 free(pevent->printk_map[i].printk);
5047 free(pevent->printk_map);
5048 }
5049
5050 while (printklist) {
5051 printknext = printklist->next;
5052 free(printklist->printk);
5053 free(printklist);
5054 printklist = printknext;
5055 }
5056
5057 for (i = 0; i < pevent->nr_events; i++)
5058 free_event(pevent->events[i]);
5059
5060 while (pevent->handlers) {
5061 handle = pevent->handlers;
5062 pevent->handlers = handle->next;
5063 free_handler(handle);
5064 }
5065
5066 free(pevent->events);
5067 free(pevent->sort_events);
5068
5069 free(pevent);
5070}
5071
5072void pevent_unref(struct pevent *pevent)
5073{
5074 pevent_free(pevent);
5075}
This page took 0.33423 seconds and 5 git commands to generate.