blob: 4d5092f671674a838830e22e7a3d63e620587f40 [file] [log] [blame]
Steven Rostedtf7d82352012-04-06 00:47:53 +02001/*
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"
Steven Rostedt668fe012012-04-06 00:47:55 +020036#include "event-utils.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020037
38static const char *input_buf;
39static unsigned long long input_buf_ptr;
40static unsigned long long input_buf_siz;
41
Tom Zanussi5205aec2012-04-06 00:47:58 +020042static int is_flag_field;
43static int is_symbolic_field;
44
Steven Rostedtf7d82352012-04-06 00:47:53 +020045static 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:
1437 if (field)
1438 free(field);
1439 return -1;
1440}
1441
1442static int event_read_format(struct event_format *event)
1443{
1444 char *token;
1445 int ret;
1446
1447 if (read_expected_item(EVENT_ITEM, "format") < 0)
1448 return -1;
1449
1450 if (read_expected(EVENT_OP, ":") < 0)
1451 return -1;
1452
1453 if (read_expect_type(EVENT_NEWLINE, &token))
1454 goto fail;
1455 free_token(token);
1456
1457 ret = event_read_fields(event, &event->format.common_fields);
1458 if (ret < 0)
1459 return ret;
1460 event->format.nr_common = ret;
1461
1462 ret = event_read_fields(event, &event->format.fields);
1463 if (ret < 0)
1464 return ret;
1465 event->format.nr_fields = ret;
1466
1467 return 0;
1468
1469 fail:
1470 free_token(token);
1471 return -1;
1472}
1473
1474static enum event_type
1475process_arg_token(struct event_format *event, struct print_arg *arg,
1476 char **tok, enum event_type type);
1477
1478static enum event_type
1479process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1480{
1481 enum event_type type;
1482 char *token;
1483
1484 type = read_token(&token);
1485 *tok = token;
1486
1487 return process_arg_token(event, arg, tok, type);
1488}
1489
1490static enum event_type
1491process_op(struct event_format *event, struct print_arg *arg, char **tok);
1492
1493static enum event_type
1494process_cond(struct event_format *event, struct print_arg *top, char **tok)
1495{
1496 struct print_arg *arg, *left, *right;
1497 enum event_type type;
1498 char *token = NULL;
1499
1500 arg = alloc_arg();
1501 left = alloc_arg();
1502 right = alloc_arg();
1503
1504 arg->type = PRINT_OP;
1505 arg->op.left = left;
1506 arg->op.right = right;
1507
1508 *tok = NULL;
1509 type = process_arg(event, left, &token);
1510
1511 again:
1512 /* Handle other operations in the arguments */
1513 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1514 type = process_op(event, left, &token);
1515 goto again;
1516 }
1517
1518 if (test_type_token(type, token, EVENT_OP, ":"))
1519 goto out_free;
1520
1521 arg->op.op = token;
1522
1523 type = process_arg(event, right, &token);
1524
1525 top->op.right = arg;
1526
1527 *tok = token;
1528 return type;
1529
1530out_free:
1531 /* Top may point to itself */
1532 top->op.right = NULL;
1533 free_token(token);
1534 free_arg(arg);
1535 return EVENT_ERROR;
1536}
1537
1538static enum event_type
1539process_array(struct event_format *event, struct print_arg *top, char **tok)
1540{
1541 struct print_arg *arg;
1542 enum event_type type;
1543 char *token = NULL;
1544
1545 arg = alloc_arg();
1546
1547 *tok = NULL;
1548 type = process_arg(event, arg, &token);
1549 if (test_type_token(type, token, EVENT_OP, "]"))
1550 goto out_free;
1551
1552 top->op.right = arg;
1553
1554 free_token(token);
1555 type = read_token_item(&token);
1556 *tok = token;
1557
1558 return type;
1559
1560out_free:
1561 free_token(*tok);
1562 *tok = NULL;
1563 free_arg(arg);
1564 return EVENT_ERROR;
1565}
1566
1567static int get_op_prio(char *op)
1568{
1569 if (!op[1]) {
1570 switch (op[0]) {
1571 case '~':
1572 case '!':
1573 return 4;
1574 case '*':
1575 case '/':
1576 case '%':
1577 return 6;
1578 case '+':
1579 case '-':
1580 return 7;
1581 /* '>>' and '<<' are 8 */
1582 case '<':
1583 case '>':
1584 return 9;
1585 /* '==' and '!=' are 10 */
1586 case '&':
1587 return 11;
1588 case '^':
1589 return 12;
1590 case '|':
1591 return 13;
1592 case '?':
1593 return 16;
1594 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001595 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001596 return -1;
1597 }
1598 } else {
1599 if (strcmp(op, "++") == 0 ||
1600 strcmp(op, "--") == 0) {
1601 return 3;
1602 } else if (strcmp(op, ">>") == 0 ||
1603 strcmp(op, "<<") == 0) {
1604 return 8;
1605 } else if (strcmp(op, ">=") == 0 ||
1606 strcmp(op, "<=") == 0) {
1607 return 9;
1608 } else if (strcmp(op, "==") == 0 ||
1609 strcmp(op, "!=") == 0) {
1610 return 10;
1611 } else if (strcmp(op, "&&") == 0) {
1612 return 14;
1613 } else if (strcmp(op, "||") == 0) {
1614 return 15;
1615 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001616 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001617 return -1;
1618 }
1619 }
1620}
1621
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001622static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001623{
1624
1625 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001626 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001627 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001628 else
1629 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001630
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001631 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001632}
1633
1634/* Note, *tok does not get freed, but will most likely be saved */
1635static enum event_type
1636process_op(struct event_format *event, struct print_arg *arg, char **tok)
1637{
1638 struct print_arg *left, *right = NULL;
1639 enum event_type type;
1640 char *token;
1641
1642 /* the op is passed in via tok */
1643 token = *tok;
1644
1645 if (arg->type == PRINT_OP && !arg->op.left) {
1646 /* handle single op */
1647 if (token[1]) {
1648 die("bad op token %s", token);
1649 goto out_free;
1650 }
1651 switch (token[0]) {
1652 case '~':
1653 case '!':
1654 case '+':
1655 case '-':
1656 break;
1657 default:
1658 do_warning("bad op token %s", token);
1659 goto out_free;
1660
1661 }
1662
1663 /* make an empty left */
1664 left = alloc_arg();
1665 left->type = PRINT_NULL;
1666 arg->op.left = left;
1667
1668 right = alloc_arg();
1669 arg->op.right = right;
1670
1671 /* do not free the token, it belongs to an op */
1672 *tok = NULL;
1673 type = process_arg(event, right, tok);
1674
1675 } else if (strcmp(token, "?") == 0) {
1676
1677 left = alloc_arg();
1678 /* copy the top arg to the left */
1679 *left = *arg;
1680
1681 arg->type = PRINT_OP;
1682 arg->op.op = token;
1683 arg->op.left = left;
1684 arg->op.prio = 0;
1685
1686 type = process_cond(event, arg, tok);
1687
1688 } else if (strcmp(token, ">>") == 0 ||
1689 strcmp(token, "<<") == 0 ||
1690 strcmp(token, "&") == 0 ||
1691 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
1704 left = alloc_arg();
1705
1706 /* copy the top arg to the left */
1707 *left = *arg;
1708
1709 arg->type = PRINT_OP;
1710 arg->op.op = token;
1711 arg->op.left = left;
1712
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001713 if (set_op_prio(arg) == -1) {
1714 event->flags |= EVENT_FL_FAILED;
1715 goto out_free;
1716 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001717
1718 type = read_token_item(&token);
1719 *tok = token;
1720
1721 /* could just be a type pointer */
1722 if ((strcmp(arg->op.op, "*") == 0) &&
1723 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1724 if (left->type != PRINT_ATOM)
1725 die("bad pointer type");
1726 left->atom.atom = realloc(left->atom.atom,
1727 strlen(left->atom.atom) + 3);
1728 strcat(left->atom.atom, " *");
1729 free(arg->op.op);
1730 *arg = *left;
1731 free(left);
1732
1733 return type;
1734 }
1735
1736 right = alloc_arg();
1737 type = process_arg_token(event, right, tok, type);
1738 arg->op.right = right;
1739
1740 } else if (strcmp(token, "[") == 0) {
1741
1742 left = alloc_arg();
1743 *left = *arg;
1744
1745 arg->type = PRINT_OP;
1746 arg->op.op = token;
1747 arg->op.left = left;
1748
1749 arg->op.prio = 0;
1750
1751 type = process_array(event, arg, tok);
1752
1753 } else {
1754 do_warning("unknown op '%s'", token);
1755 event->flags |= EVENT_FL_FAILED;
1756 /* the arg is now the left side */
1757 goto out_free;
1758 }
1759
1760 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1761 int prio;
1762
1763 /* higher prios need to be closer to the root */
1764 prio = get_op_prio(*tok);
1765
1766 if (prio > arg->op.prio)
1767 return process_op(event, arg, tok);
1768
1769 return process_op(event, right, tok);
1770 }
1771
1772 return type;
1773
1774 out_free:
1775 free_token(token);
1776 *tok = NULL;
1777 return EVENT_ERROR;
1778}
1779
1780static enum event_type
1781process_entry(struct event_format *event __unused, struct print_arg *arg,
1782 char **tok)
1783{
1784 enum event_type type;
1785 char *field;
1786 char *token;
1787
1788 if (read_expected(EVENT_OP, "->") < 0)
1789 goto out_err;
1790
1791 if (read_expect_type(EVENT_ITEM, &token) < 0)
1792 goto out_free;
1793 field = token;
1794
1795 arg->type = PRINT_FIELD;
1796 arg->field.name = field;
1797
Tom Zanussi5205aec2012-04-06 00:47:58 +02001798 if (is_flag_field) {
1799 arg->field.field = pevent_find_any_field(event, arg->field.name);
1800 arg->field.field->flags |= FIELD_IS_FLAG;
1801 is_flag_field = 0;
1802 } else if (is_symbolic_field) {
1803 arg->field.field = pevent_find_any_field(event, arg->field.name);
1804 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1805 is_symbolic_field = 0;
1806 }
1807
Steven Rostedtf7d82352012-04-06 00:47:53 +02001808 type = read_token(&token);
1809 *tok = token;
1810
1811 return type;
1812
1813 out_free:
1814 free_token(token);
1815 out_err:
1816 *tok = NULL;
1817 return EVENT_ERROR;
1818}
1819
1820static char *arg_eval (struct print_arg *arg);
1821
1822static unsigned long long
1823eval_type_str(unsigned long long val, const char *type, int pointer)
1824{
1825 int sign = 0;
1826 char *ref;
1827 int len;
1828
1829 len = strlen(type);
1830
1831 if (pointer) {
1832
1833 if (type[len-1] != '*') {
1834 do_warning("pointer expected with non pointer type");
1835 return val;
1836 }
1837
1838 ref = malloc_or_die(len);
1839 memcpy(ref, type, len);
1840
1841 /* chop off the " *" */
1842 ref[len - 2] = 0;
1843
1844 val = eval_type_str(val, ref, 0);
1845 free(ref);
1846 return val;
1847 }
1848
1849 /* check if this is a pointer */
1850 if (type[len - 1] == '*')
1851 return val;
1852
1853 /* Try to figure out the arg size*/
1854 if (strncmp(type, "struct", 6) == 0)
1855 /* all bets off */
1856 return val;
1857
1858 if (strcmp(type, "u8") == 0)
1859 return val & 0xff;
1860
1861 if (strcmp(type, "u16") == 0)
1862 return val & 0xffff;
1863
1864 if (strcmp(type, "u32") == 0)
1865 return val & 0xffffffff;
1866
1867 if (strcmp(type, "u64") == 0 ||
1868 strcmp(type, "s64"))
1869 return val;
1870
1871 if (strcmp(type, "s8") == 0)
1872 return (unsigned long long)(char)val & 0xff;
1873
1874 if (strcmp(type, "s16") == 0)
1875 return (unsigned long long)(short)val & 0xffff;
1876
1877 if (strcmp(type, "s32") == 0)
1878 return (unsigned long long)(int)val & 0xffffffff;
1879
1880 if (strncmp(type, "unsigned ", 9) == 0) {
1881 sign = 0;
1882 type += 9;
1883 }
1884
1885 if (strcmp(type, "char") == 0) {
1886 if (sign)
1887 return (unsigned long long)(char)val & 0xff;
1888 else
1889 return val & 0xff;
1890 }
1891
1892 if (strcmp(type, "short") == 0) {
1893 if (sign)
1894 return (unsigned long long)(short)val & 0xffff;
1895 else
1896 return val & 0xffff;
1897 }
1898
1899 if (strcmp(type, "int") == 0) {
1900 if (sign)
1901 return (unsigned long long)(int)val & 0xffffffff;
1902 else
1903 return val & 0xffffffff;
1904 }
1905
1906 return val;
1907}
1908
1909/*
1910 * Try to figure out the type.
1911 */
1912static unsigned long long
1913eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1914{
1915 if (arg->type != PRINT_TYPE)
1916 die("expected type argument");
1917
1918 return eval_type_str(val, arg->typecast.type, pointer);
1919}
1920
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001921static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001922{
1923 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001924 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001925
1926 switch (arg->type) {
1927 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001928 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001929 break;
1930 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001931 ret = arg_num_eval(arg->typecast.item, val);
1932 if (!ret)
1933 break;
1934 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001935 break;
1936 case PRINT_OP:
1937 switch (arg->op.op[0]) {
1938 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001939 ret = arg_num_eval(arg->op.left, &left);
1940 if (!ret)
1941 break;
1942 ret = arg_num_eval(arg->op.right, &right);
1943 if (!ret)
1944 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001945 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001946 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001947 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001948 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001949 break;
1950 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001951 ret = arg_num_eval(arg->op.left, &left);
1952 if (!ret)
1953 break;
1954 ret = arg_num_eval(arg->op.right, &right);
1955 if (!ret)
1956 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001957 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001958 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001959 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001960 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001961 break;
1962 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001963 ret = arg_num_eval(arg->op.left, &left);
1964 if (!ret)
1965 break;
1966 ret = arg_num_eval(arg->op.right, &right);
1967 if (!ret)
1968 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001969 switch (arg->op.op[1]) {
1970 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001971 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001972 break;
1973 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001974 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001975 break;
1976 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001977 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001978 break;
1979 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001980 do_warning("unknown op '%s'", arg->op.op);
1981 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001982 }
1983 break;
1984 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001985 ret = arg_num_eval(arg->op.left, &left);
1986 if (!ret)
1987 break;
1988 ret = arg_num_eval(arg->op.right, &right);
1989 if (!ret)
1990 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001991 switch (arg->op.op[1]) {
1992 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001993 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001994 break;
1995 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001996 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001997 break;
1998 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001999 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002000 break;
2001 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002002 do_warning("unknown op '%s'", arg->op.op);
2003 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002004 }
2005 break;
2006 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002007 ret = arg_num_eval(arg->op.left, &left);
2008 if (!ret)
2009 break;
2010 ret = arg_num_eval(arg->op.right, &right);
2011 if (!ret)
2012 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002013
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002014 if (arg->op.op[1] != '=') {
2015 do_warning("unknown op '%s'", arg->op.op);
2016 ret = 0;
2017 } else
2018 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002019 break;
2020 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002021 ret = arg_num_eval(arg->op.left, &left);
2022 if (!ret)
2023 break;
2024 ret = arg_num_eval(arg->op.right, &right);
2025 if (!ret)
2026 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002027
2028 switch (arg->op.op[1]) {
2029 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002030 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002031 break;
2032 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002033 do_warning("unknown op '%s'", arg->op.op);
2034 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002035 }
2036 break;
2037 case '-':
2038 /* check for negative */
2039 if (arg->op.left->type == PRINT_NULL)
2040 left = 0;
2041 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002042 ret = arg_num_eval(arg->op.left, &left);
2043 if (!ret)
2044 break;
2045 ret = arg_num_eval(arg->op.right, &right);
2046 if (!ret)
2047 break;
2048 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002049 break;
2050 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002051 do_warning("unknown op '%s'", arg->op.op);
2052 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002053 }
2054 break;
2055
2056 case PRINT_NULL:
2057 case PRINT_FIELD ... PRINT_SYMBOL:
2058 case PRINT_STRING:
2059 case PRINT_BSTRING:
2060 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002061 do_warning("invalid eval type %d", arg->type);
2062 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002063
2064 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002065 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002066}
2067
2068static char *arg_eval (struct print_arg *arg)
2069{
2070 long long val;
2071 static char buf[20];
2072
2073 switch (arg->type) {
2074 case PRINT_ATOM:
2075 return arg->atom.atom;
2076 case PRINT_TYPE:
2077 return arg_eval(arg->typecast.item);
2078 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002079 if (!arg_num_eval(arg, &val))
2080 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002081 sprintf(buf, "%lld", val);
2082 return buf;
2083
2084 case PRINT_NULL:
2085 case PRINT_FIELD ... PRINT_SYMBOL:
2086 case PRINT_STRING:
2087 case PRINT_BSTRING:
2088 default:
2089 die("invalid eval type %d", arg->type);
2090 break;
2091 }
2092
2093 return NULL;
2094}
2095
2096static enum event_type
2097process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2098{
2099 enum event_type type;
2100 struct print_arg *arg = NULL;
2101 struct print_flag_sym *field;
2102 char *token = *tok;
2103 char *value;
2104
2105 do {
2106 free_token(token);
2107 type = read_token_item(&token);
2108 if (test_type_token(type, token, EVENT_OP, "{"))
2109 break;
2110
2111 arg = alloc_arg();
2112
2113 free_token(token);
2114 type = process_arg(event, arg, &token);
2115 if (test_type_token(type, token, EVENT_DELIM, ","))
2116 goto out_free;
2117
2118 field = malloc_or_die(sizeof(*field));
Julia Lawall54a36252012-04-06 00:47:59 +02002119 memset(field, 0, sizeof(*field));
Steven Rostedtf7d82352012-04-06 00:47:53 +02002120
2121 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002122 if (value == NULL)
2123 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002124 field->value = strdup(value);
2125
2126 free_arg(arg);
2127 arg = alloc_arg();
2128
2129 free_token(token);
2130 type = process_arg(event, arg, &token);
2131 if (test_type_token(type, token, EVENT_OP, "}"))
2132 goto out_free;
2133
2134 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002135 if (value == NULL)
2136 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002137 field->str = strdup(value);
2138 free_arg(arg);
2139 arg = NULL;
2140
2141 *list = field;
2142 list = &field->next;
2143
2144 free_token(token);
2145 type = read_token_item(&token);
2146 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2147
2148 *tok = token;
2149 return type;
2150
2151out_free:
2152 free_arg(arg);
2153 free_token(token);
2154 *tok = NULL;
2155
2156 return EVENT_ERROR;
2157}
2158
2159static enum event_type
2160process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2161{
2162 struct print_arg *field;
2163 enum event_type type;
2164 char *token;
2165
2166 memset(arg, 0, sizeof(*arg));
2167 arg->type = PRINT_FLAGS;
2168
2169 field = alloc_arg();
2170
2171 type = process_arg(event, field, &token);
2172
2173 /* Handle operations in the first argument */
2174 while (type == EVENT_OP)
2175 type = process_op(event, field, &token);
2176
2177 if (test_type_token(type, token, EVENT_DELIM, ","))
2178 goto out_free;
2179 free_token(token);
2180
2181 arg->flags.field = field;
2182
2183 type = read_token_item(&token);
2184 if (event_item_type(type)) {
2185 arg->flags.delim = token;
2186 type = read_token_item(&token);
2187 }
2188
2189 if (test_type_token(type, token, EVENT_DELIM, ","))
2190 goto out_free;
2191
2192 type = process_fields(event, &arg->flags.flags, &token);
2193 if (test_type_token(type, token, EVENT_DELIM, ")"))
2194 goto out_free;
2195
2196 free_token(token);
2197 type = read_token_item(tok);
2198 return type;
2199
2200 out_free:
2201 free_token(token);
2202 *tok = NULL;
2203 return EVENT_ERROR;
2204}
2205
2206static enum event_type
2207process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2208{
2209 struct print_arg *field;
2210 enum event_type type;
2211 char *token;
2212
2213 memset(arg, 0, sizeof(*arg));
2214 arg->type = PRINT_SYMBOL;
2215
2216 field = alloc_arg();
2217
2218 type = process_arg(event, field, &token);
2219 if (test_type_token(type, token, EVENT_DELIM, ","))
2220 goto out_free;
2221
2222 arg->symbol.field = field;
2223
2224 type = process_fields(event, &arg->symbol.symbols, &token);
2225 if (test_type_token(type, token, EVENT_DELIM, ")"))
2226 goto out_free;
2227
2228 free_token(token);
2229 type = read_token_item(tok);
2230 return type;
2231
2232 out_free:
2233 free_token(token);
2234 *tok = NULL;
2235 return EVENT_ERROR;
2236}
2237
2238static enum event_type
2239process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2240{
2241 struct format_field *field;
2242 enum event_type type;
2243 char *token;
2244
2245 memset(arg, 0, sizeof(*arg));
2246 arg->type = PRINT_DYNAMIC_ARRAY;
2247
2248 /*
2249 * The item within the parenthesis is another field that holds
2250 * the index into where the array starts.
2251 */
2252 type = read_token(&token);
2253 *tok = token;
2254 if (type != EVENT_ITEM)
2255 goto out_free;
2256
2257 /* Find the field */
2258
2259 field = pevent_find_field(event, token);
2260 if (!field)
2261 goto out_free;
2262
2263 arg->dynarray.field = field;
2264 arg->dynarray.index = 0;
2265
2266 if (read_expected(EVENT_DELIM, ")") < 0)
2267 goto out_free;
2268
2269 free_token(token);
2270 type = read_token_item(&token);
2271 *tok = token;
2272 if (type != EVENT_OP || strcmp(token, "[") != 0)
2273 return type;
2274
2275 free_token(token);
2276 arg = alloc_arg();
2277 type = process_arg(event, arg, &token);
2278 if (type == EVENT_ERROR)
2279 goto out_free;
2280
2281 if (!test_type_token(type, token, EVENT_OP, "]"))
2282 goto out_free;
2283
2284 free_token(token);
2285 type = read_token_item(tok);
2286 return type;
2287
2288 out_free:
2289 free(arg);
2290 free_token(token);
2291 *tok = NULL;
2292 return EVENT_ERROR;
2293}
2294
2295static enum event_type
2296process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2297{
2298 struct print_arg *item_arg;
2299 enum event_type type;
2300 char *token;
2301
2302 type = process_arg(event, arg, &token);
2303
2304 if (type == EVENT_ERROR)
2305 goto out_free;
2306
2307 if (type == EVENT_OP)
2308 type = process_op(event, arg, &token);
2309
2310 if (type == EVENT_ERROR)
2311 goto out_free;
2312
2313 if (test_type_token(type, token, EVENT_DELIM, ")"))
2314 goto out_free;
2315
2316 free_token(token);
2317 type = read_token_item(&token);
2318
2319 /*
2320 * If the next token is an item or another open paren, then
2321 * this was a typecast.
2322 */
2323 if (event_item_type(type) ||
2324 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2325
2326 /* make this a typecast and contine */
2327
2328 /* prevous must be an atom */
2329 if (arg->type != PRINT_ATOM)
2330 die("previous needed to be PRINT_ATOM");
2331
2332 item_arg = alloc_arg();
2333
2334 arg->type = PRINT_TYPE;
2335 arg->typecast.type = arg->atom.atom;
2336 arg->typecast.item = item_arg;
2337 type = process_arg_token(event, item_arg, &token, type);
2338
2339 }
2340
2341 *tok = token;
2342 return type;
2343
2344 out_free:
2345 free_token(token);
2346 *tok = NULL;
2347 return EVENT_ERROR;
2348}
2349
2350
2351static enum event_type
2352process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2353{
2354 enum event_type type;
2355 char *token;
2356
2357 if (read_expect_type(EVENT_ITEM, &token) < 0)
2358 goto out_free;
2359
2360 arg->type = PRINT_STRING;
2361 arg->string.string = token;
2362 arg->string.offset = -1;
2363
2364 if (read_expected(EVENT_DELIM, ")") < 0)
2365 goto out_err;
2366
2367 type = read_token(&token);
2368 *tok = token;
2369
2370 return type;
2371
2372 out_free:
2373 free_token(token);
2374 out_err:
2375 *tok = NULL;
2376 return EVENT_ERROR;
2377}
2378
2379static struct pevent_function_handler *
2380find_func_handler(struct pevent *pevent, char *func_name)
2381{
2382 struct pevent_function_handler *func;
2383
2384 for (func = pevent->func_handlers; func; func = func->next) {
2385 if (strcmp(func->name, func_name) == 0)
2386 break;
2387 }
2388
2389 return func;
2390}
2391
2392static void remove_func_handler(struct pevent *pevent, char *func_name)
2393{
2394 struct pevent_function_handler *func;
2395 struct pevent_function_handler **next;
2396
2397 next = &pevent->func_handlers;
2398 while ((func = *next)) {
2399 if (strcmp(func->name, func_name) == 0) {
2400 *next = func->next;
2401 free_func_handle(func);
2402 break;
2403 }
2404 next = &func->next;
2405 }
2406}
2407
2408static enum event_type
2409process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2410 struct print_arg *arg, char **tok)
2411{
2412 struct print_arg **next_arg;
2413 struct print_arg *farg;
2414 enum event_type type;
2415 char *token;
2416 char *test;
2417 int i;
2418
2419 arg->type = PRINT_FUNC;
2420 arg->func.func = func;
2421
2422 *tok = NULL;
2423
2424 next_arg = &(arg->func.args);
2425 for (i = 0; i < func->nr_args; i++) {
2426 farg = alloc_arg();
2427 type = process_arg(event, farg, &token);
2428 if (i < (func->nr_args - 1))
2429 test = ",";
2430 else
2431 test = ")";
2432
2433 if (test_type_token(type, token, EVENT_DELIM, test)) {
2434 free_arg(farg);
2435 free_token(token);
2436 return EVENT_ERROR;
2437 }
2438
2439 *next_arg = farg;
2440 next_arg = &(farg->next);
2441 free_token(token);
2442 }
2443
2444 type = read_token(&token);
2445 *tok = token;
2446
2447 return type;
2448}
2449
2450static enum event_type
2451process_function(struct event_format *event, struct print_arg *arg,
2452 char *token, char **tok)
2453{
2454 struct pevent_function_handler *func;
2455
2456 if (strcmp(token, "__print_flags") == 0) {
2457 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002458 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002459 return process_flags(event, arg, tok);
2460 }
2461 if (strcmp(token, "__print_symbolic") == 0) {
2462 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002463 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002464 return process_symbols(event, arg, tok);
2465 }
2466 if (strcmp(token, "__get_str") == 0) {
2467 free_token(token);
2468 return process_str(event, arg, tok);
2469 }
2470 if (strcmp(token, "__get_dynamic_array") == 0) {
2471 free_token(token);
2472 return process_dynamic_array(event, arg, tok);
2473 }
2474
2475 func = find_func_handler(event->pevent, token);
2476 if (func) {
2477 free_token(token);
2478 return process_func_handler(event, func, arg, tok);
2479 }
2480
2481 do_warning("function %s not defined", token);
2482 free_token(token);
2483 return EVENT_ERROR;
2484}
2485
2486static enum event_type
2487process_arg_token(struct event_format *event, struct print_arg *arg,
2488 char **tok, enum event_type type)
2489{
2490 char *token;
2491 char *atom;
2492
2493 token = *tok;
2494
2495 switch (type) {
2496 case EVENT_ITEM:
2497 if (strcmp(token, "REC") == 0) {
2498 free_token(token);
2499 type = process_entry(event, arg, &token);
2500 break;
2501 }
2502 atom = token;
2503 /* test the next token */
2504 type = read_token_item(&token);
2505
2506 /*
2507 * If the next token is a parenthesis, then this
2508 * is a function.
2509 */
2510 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2511 free_token(token);
2512 token = NULL;
2513 /* this will free atom. */
2514 type = process_function(event, arg, atom, &token);
2515 break;
2516 }
2517 /* atoms can be more than one token long */
2518 while (type == EVENT_ITEM) {
2519 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
2520 strcat(atom, " ");
2521 strcat(atom, token);
2522 free_token(token);
2523 type = read_token_item(&token);
2524 }
2525
2526 arg->type = PRINT_ATOM;
2527 arg->atom.atom = atom;
2528 break;
2529
2530 case EVENT_DQUOTE:
2531 case EVENT_SQUOTE:
2532 arg->type = PRINT_ATOM;
2533 arg->atom.atom = token;
2534 type = read_token_item(&token);
2535 break;
2536 case EVENT_DELIM:
2537 if (strcmp(token, "(") == 0) {
2538 free_token(token);
2539 type = process_paren(event, arg, &token);
2540 break;
2541 }
2542 case EVENT_OP:
2543 /* handle single ops */
2544 arg->type = PRINT_OP;
2545 arg->op.op = token;
2546 arg->op.left = NULL;
2547 type = process_op(event, arg, &token);
2548
2549 /* On error, the op is freed */
2550 if (type == EVENT_ERROR)
2551 arg->op.op = NULL;
2552
2553 /* return error type if errored */
2554 break;
2555
2556 case EVENT_ERROR ... EVENT_NEWLINE:
2557 default:
2558 die("unexpected type %d", type);
2559 }
2560 *tok = token;
2561
2562 return type;
2563}
2564
2565static int event_read_print_args(struct event_format *event, struct print_arg **list)
2566{
2567 enum event_type type = EVENT_ERROR;
2568 struct print_arg *arg;
2569 char *token;
2570 int args = 0;
2571
2572 do {
2573 if (type == EVENT_NEWLINE) {
2574 type = read_token_item(&token);
2575 continue;
2576 }
2577
2578 arg = alloc_arg();
2579
2580 type = process_arg(event, arg, &token);
2581
2582 if (type == EVENT_ERROR) {
2583 free_token(token);
2584 free_arg(arg);
2585 return -1;
2586 }
2587
2588 *list = arg;
2589 args++;
2590
2591 if (type == EVENT_OP) {
2592 type = process_op(event, arg, &token);
2593 free_token(token);
2594 if (type == EVENT_ERROR) {
2595 *list = NULL;
2596 free_arg(arg);
2597 return -1;
2598 }
2599 list = &arg->next;
2600 continue;
2601 }
2602
2603 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2604 free_token(token);
2605 *list = arg;
2606 list = &arg->next;
2607 continue;
2608 }
2609 break;
2610 } while (type != EVENT_NONE);
2611
2612 if (type != EVENT_NONE && type != EVENT_ERROR)
2613 free_token(token);
2614
2615 return args;
2616}
2617
2618static int event_read_print(struct event_format *event)
2619{
2620 enum event_type type;
2621 char *token;
2622 int ret;
2623
2624 if (read_expected_item(EVENT_ITEM, "print") < 0)
2625 return -1;
2626
2627 if (read_expected(EVENT_ITEM, "fmt") < 0)
2628 return -1;
2629
2630 if (read_expected(EVENT_OP, ":") < 0)
2631 return -1;
2632
2633 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2634 goto fail;
2635
2636 concat:
2637 event->print_fmt.format = token;
2638 event->print_fmt.args = NULL;
2639
2640 /* ok to have no arg */
2641 type = read_token_item(&token);
2642
2643 if (type == EVENT_NONE)
2644 return 0;
2645
2646 /* Handle concatenation of print lines */
2647 if (type == EVENT_DQUOTE) {
2648 char *cat;
2649
2650 cat = malloc_or_die(strlen(event->print_fmt.format) +
2651 strlen(token) + 1);
2652 strcpy(cat, event->print_fmt.format);
2653 strcat(cat, token);
2654 free_token(token);
2655 free_token(event->print_fmt.format);
2656 event->print_fmt.format = NULL;
2657 token = cat;
2658 goto concat;
2659 }
2660
2661 if (test_type_token(type, token, EVENT_DELIM, ","))
2662 goto fail;
2663
2664 free_token(token);
2665
2666 ret = event_read_print_args(event, &event->print_fmt.args);
2667 if (ret < 0)
2668 return -1;
2669
2670 return ret;
2671
2672 fail:
2673 free_token(token);
2674 return -1;
2675}
2676
2677/**
2678 * pevent_find_common_field - return a common field by event
2679 * @event: handle for the event
2680 * @name: the name of the common field to return
2681 *
2682 * Returns a common field from the event by the given @name.
2683 * This only searchs the common fields and not all field.
2684 */
2685struct format_field *
2686pevent_find_common_field(struct event_format *event, const char *name)
2687{
2688 struct format_field *format;
2689
2690 for (format = event->format.common_fields;
2691 format; format = format->next) {
2692 if (strcmp(format->name, name) == 0)
2693 break;
2694 }
2695
2696 return format;
2697}
2698
2699/**
2700 * pevent_find_field - find a non-common field
2701 * @event: handle for the event
2702 * @name: the name of the non-common field
2703 *
2704 * Returns a non-common field by the given @name.
2705 * This does not search common fields.
2706 */
2707struct format_field *
2708pevent_find_field(struct event_format *event, const char *name)
2709{
2710 struct format_field *format;
2711
2712 for (format = event->format.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_any_field - find any field by name
2723 * @event: handle for the event
2724 * @name: the name of the field
2725 *
2726 * Returns a field by the given @name.
2727 * This searchs the common field names first, then
2728 * the non-common ones if a common one was not found.
2729 */
2730struct format_field *
2731pevent_find_any_field(struct event_format *event, const char *name)
2732{
2733 struct format_field *format;
2734
2735 format = pevent_find_common_field(event, name);
2736 if (format)
2737 return format;
2738 return pevent_find_field(event, name);
2739}
2740
2741/**
2742 * pevent_read_number - read a number from data
2743 * @pevent: handle for the pevent
2744 * @ptr: the raw data
2745 * @size: the size of the data that holds the number
2746 *
2747 * Returns the number (converted to host) from the
2748 * raw data.
2749 */
2750unsigned long long pevent_read_number(struct pevent *pevent,
2751 const void *ptr, int size)
2752{
2753 switch (size) {
2754 case 1:
2755 return *(unsigned char *)ptr;
2756 case 2:
2757 return data2host2(pevent, ptr);
2758 case 4:
2759 return data2host4(pevent, ptr);
2760 case 8:
2761 return data2host8(pevent, ptr);
2762 default:
2763 /* BUG! */
2764 return 0;
2765 }
2766}
2767
2768/**
2769 * pevent_read_number_field - read a number from data
2770 * @field: a handle to the field
2771 * @data: the raw data to read
2772 * @value: the value to place the number in
2773 *
2774 * Reads raw data according to a field offset and size,
2775 * and translates it into @value.
2776 *
2777 * Returns 0 on success, -1 otherwise.
2778 */
2779int pevent_read_number_field(struct format_field *field, const void *data,
2780 unsigned long long *value)
2781{
2782 if (!field)
2783 return -1;
2784 switch (field->size) {
2785 case 1:
2786 case 2:
2787 case 4:
2788 case 8:
2789 *value = pevent_read_number(field->event->pevent,
2790 data + field->offset, field->size);
2791 return 0;
2792 default:
2793 return -1;
2794 }
2795}
2796
2797static int get_common_info(struct pevent *pevent,
2798 const char *type, int *offset, int *size)
2799{
2800 struct event_format *event;
2801 struct format_field *field;
2802
2803 /*
2804 * All events should have the same common elements.
2805 * Pick any event to find where the type is;
2806 */
2807 if (!pevent->events)
2808 die("no event_list!");
2809
2810 event = pevent->events[0];
2811 field = pevent_find_common_field(event, type);
2812 if (!field)
2813 die("field '%s' not found", type);
2814
2815 *offset = field->offset;
2816 *size = field->size;
2817
2818 return 0;
2819}
2820
2821static int __parse_common(struct pevent *pevent, void *data,
2822 int *size, int *offset, const char *name)
2823{
2824 int ret;
2825
2826 if (!*size) {
2827 ret = get_common_info(pevent, name, offset, size);
2828 if (ret < 0)
2829 return ret;
2830 }
2831 return pevent_read_number(pevent, data + *offset, *size);
2832}
2833
2834static int trace_parse_common_type(struct pevent *pevent, void *data)
2835{
2836 return __parse_common(pevent, data,
2837 &pevent->type_size, &pevent->type_offset,
2838 "common_type");
2839}
2840
2841static int parse_common_pid(struct pevent *pevent, void *data)
2842{
2843 return __parse_common(pevent, data,
2844 &pevent->pid_size, &pevent->pid_offset,
2845 "common_pid");
2846}
2847
2848static int parse_common_pc(struct pevent *pevent, void *data)
2849{
2850 return __parse_common(pevent, data,
2851 &pevent->pc_size, &pevent->pc_offset,
2852 "common_preempt_count");
2853}
2854
2855static int parse_common_flags(struct pevent *pevent, void *data)
2856{
2857 return __parse_common(pevent, data,
2858 &pevent->flags_size, &pevent->flags_offset,
2859 "common_flags");
2860}
2861
2862static int parse_common_lock_depth(struct pevent *pevent, void *data)
2863{
2864 int ret;
2865
2866 ret = __parse_common(pevent, data,
2867 &pevent->ld_size, &pevent->ld_offset,
2868 "common_lock_depth");
2869 if (ret < 0)
2870 return -1;
2871
2872 return ret;
2873}
2874
2875static int events_id_cmp(const void *a, const void *b);
2876
2877/**
2878 * pevent_find_event - find an event by given id
2879 * @pevent: a handle to the pevent
2880 * @id: the id of the event
2881 *
2882 * Returns an event that has a given @id.
2883 */
2884struct event_format *pevent_find_event(struct pevent *pevent, int id)
2885{
2886 struct event_format **eventptr;
2887 struct event_format key;
2888 struct event_format *pkey = &key;
2889
2890 /* Check cache first */
2891 if (pevent->last_event && pevent->last_event->id == id)
2892 return pevent->last_event;
2893
2894 key.id = id;
2895
2896 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
2897 sizeof(*pevent->events), events_id_cmp);
2898
2899 if (eventptr) {
2900 pevent->last_event = *eventptr;
2901 return *eventptr;
2902 }
2903
2904 return NULL;
2905}
2906
2907/**
2908 * pevent_find_event_by_name - find an event by given name
2909 * @pevent: a handle to the pevent
2910 * @sys: the system name to search for
2911 * @name: the name of the event to search for
2912 *
2913 * This returns an event with a given @name and under the system
2914 * @sys. If @sys is NULL the first event with @name is returned.
2915 */
2916struct event_format *
2917pevent_find_event_by_name(struct pevent *pevent,
2918 const char *sys, const char *name)
2919{
2920 struct event_format *event;
2921 int i;
2922
2923 if (pevent->last_event &&
2924 strcmp(pevent->last_event->name, name) == 0 &&
2925 (!sys || strcmp(pevent->last_event->system, sys) == 0))
2926 return pevent->last_event;
2927
2928 for (i = 0; i < pevent->nr_events; i++) {
2929 event = pevent->events[i];
2930 if (strcmp(event->name, name) == 0) {
2931 if (!sys)
2932 break;
2933 if (strcmp(event->system, sys) == 0)
2934 break;
2935 }
2936 }
2937 if (i == pevent->nr_events)
2938 event = NULL;
2939
2940 pevent->last_event = event;
2941 return event;
2942}
2943
2944static unsigned long long
2945eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
2946{
2947 struct pevent *pevent = event->pevent;
2948 unsigned long long val = 0;
2949 unsigned long long left, right;
2950 struct print_arg *typearg = NULL;
2951 struct print_arg *larg;
2952 unsigned long offset;
2953 unsigned int field_size;
2954
2955 switch (arg->type) {
2956 case PRINT_NULL:
2957 /* ?? */
2958 return 0;
2959 case PRINT_ATOM:
2960 return strtoull(arg->atom.atom, NULL, 0);
2961 case PRINT_FIELD:
2962 if (!arg->field.field) {
2963 arg->field.field = pevent_find_any_field(event, arg->field.name);
2964 if (!arg->field.field)
2965 die("field %s not found", arg->field.name);
2966 }
2967 /* must be a number */
2968 val = pevent_read_number(pevent, data + arg->field.field->offset,
2969 arg->field.field->size);
2970 break;
2971 case PRINT_FLAGS:
2972 case PRINT_SYMBOL:
2973 break;
2974 case PRINT_TYPE:
2975 val = eval_num_arg(data, size, event, arg->typecast.item);
2976 return eval_type(val, arg, 0);
2977 case PRINT_STRING:
2978 case PRINT_BSTRING:
2979 return 0;
2980 case PRINT_FUNC: {
2981 struct trace_seq s;
2982 trace_seq_init(&s);
2983 val = process_defined_func(&s, data, size, event, arg);
2984 trace_seq_destroy(&s);
2985 return val;
2986 }
2987 case PRINT_OP:
2988 if (strcmp(arg->op.op, "[") == 0) {
2989 /*
2990 * Arrays are special, since we don't want
2991 * to read the arg as is.
2992 */
2993 right = eval_num_arg(data, size, event, arg->op.right);
2994
2995 /* handle typecasts */
2996 larg = arg->op.left;
2997 while (larg->type == PRINT_TYPE) {
2998 if (!typearg)
2999 typearg = larg;
3000 larg = larg->typecast.item;
3001 }
3002
3003 /* Default to long size */
3004 field_size = pevent->long_size;
3005
3006 switch (larg->type) {
3007 case PRINT_DYNAMIC_ARRAY:
3008 offset = pevent_read_number(pevent,
3009 data + larg->dynarray.field->offset,
3010 larg->dynarray.field->size);
3011 if (larg->dynarray.field->elementsize)
3012 field_size = larg->dynarray.field->elementsize;
3013 /*
3014 * The actual length of the dynamic array is stored
3015 * in the top half of the field, and the offset
3016 * is in the bottom half of the 32 bit field.
3017 */
3018 offset &= 0xffff;
3019 offset += right;
3020 break;
3021 case PRINT_FIELD:
3022 if (!larg->field.field) {
3023 larg->field.field =
3024 pevent_find_any_field(event, larg->field.name);
3025 if (!larg->field.field)
3026 die("field %s not found", larg->field.name);
3027 }
3028 field_size = larg->field.field->elementsize;
3029 offset = larg->field.field->offset +
3030 right * larg->field.field->elementsize;
3031 break;
3032 default:
3033 goto default_op; /* oops, all bets off */
3034 }
3035 val = pevent_read_number(pevent,
3036 data + offset, field_size);
3037 if (typearg)
3038 val = eval_type(val, typearg, 1);
3039 break;
3040 } else if (strcmp(arg->op.op, "?") == 0) {
3041 left = eval_num_arg(data, size, event, arg->op.left);
3042 arg = arg->op.right;
3043 if (left)
3044 val = eval_num_arg(data, size, event, arg->op.left);
3045 else
3046 val = eval_num_arg(data, size, event, arg->op.right);
3047 break;
3048 }
3049 default_op:
3050 left = eval_num_arg(data, size, event, arg->op.left);
3051 right = eval_num_arg(data, size, event, arg->op.right);
3052 switch (arg->op.op[0]) {
3053 case '!':
3054 switch (arg->op.op[1]) {
3055 case 0:
3056 val = !right;
3057 break;
3058 case '=':
3059 val = left != right;
3060 break;
3061 default:
3062 die("unknown op '%s'", arg->op.op);
3063 }
3064 break;
3065 case '~':
3066 val = ~right;
3067 break;
3068 case '|':
3069 if (arg->op.op[1])
3070 val = left || right;
3071 else
3072 val = left | right;
3073 break;
3074 case '&':
3075 if (arg->op.op[1])
3076 val = left && right;
3077 else
3078 val = left & right;
3079 break;
3080 case '<':
3081 switch (arg->op.op[1]) {
3082 case 0:
3083 val = left < right;
3084 break;
3085 case '<':
3086 val = left << right;
3087 break;
3088 case '=':
3089 val = left <= right;
3090 break;
3091 default:
3092 die("unknown op '%s'", arg->op.op);
3093 }
3094 break;
3095 case '>':
3096 switch (arg->op.op[1]) {
3097 case 0:
3098 val = left > right;
3099 break;
3100 case '>':
3101 val = left >> right;
3102 break;
3103 case '=':
3104 val = left >= right;
3105 break;
3106 default:
3107 die("unknown op '%s'", arg->op.op);
3108 }
3109 break;
3110 case '=':
3111 if (arg->op.op[1] != '=')
3112 die("unknown op '%s'", arg->op.op);
3113 val = left == right;
3114 break;
3115 case '-':
3116 val = left - right;
3117 break;
3118 case '+':
3119 val = left + right;
3120 break;
3121 default:
3122 die("unknown op '%s'", arg->op.op);
3123 }
3124 break;
3125 default: /* not sure what to do there */
3126 return 0;
3127 }
3128 return val;
3129}
3130
3131struct flag {
3132 const char *name;
3133 unsigned long long value;
3134};
3135
3136static const struct flag flags[] = {
3137 { "HI_SOFTIRQ", 0 },
3138 { "TIMER_SOFTIRQ", 1 },
3139 { "NET_TX_SOFTIRQ", 2 },
3140 { "NET_RX_SOFTIRQ", 3 },
3141 { "BLOCK_SOFTIRQ", 4 },
3142 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3143 { "TASKLET_SOFTIRQ", 6 },
3144 { "SCHED_SOFTIRQ", 7 },
3145 { "HRTIMER_SOFTIRQ", 8 },
3146 { "RCU_SOFTIRQ", 9 },
3147
3148 { "HRTIMER_NORESTART", 0 },
3149 { "HRTIMER_RESTART", 1 },
3150};
3151
3152static unsigned long long eval_flag(const char *flag)
3153{
3154 int i;
3155
3156 /*
3157 * Some flags in the format files do not get converted.
3158 * If the flag is not numeric, see if it is something that
3159 * we already know about.
3160 */
3161 if (isdigit(flag[0]))
3162 return strtoull(flag, NULL, 0);
3163
3164 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3165 if (strcmp(flags[i].name, flag) == 0)
3166 return flags[i].value;
3167
3168 return 0;
3169}
3170
3171static void print_str_to_seq(struct trace_seq *s, const char *format,
3172 int len_arg, const char *str)
3173{
3174 if (len_arg >= 0)
3175 trace_seq_printf(s, format, len_arg, str);
3176 else
3177 trace_seq_printf(s, format, str);
3178}
3179
3180static void print_str_arg(struct trace_seq *s, void *data, int size,
3181 struct event_format *event, const char *format,
3182 int len_arg, struct print_arg *arg)
3183{
3184 struct pevent *pevent = event->pevent;
3185 struct print_flag_sym *flag;
3186 unsigned long long val, fval;
3187 unsigned long addr;
3188 char *str;
3189 int print;
3190 int len;
3191
3192 switch (arg->type) {
3193 case PRINT_NULL:
3194 /* ?? */
3195 return;
3196 case PRINT_ATOM:
3197 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3198 return;
3199 case PRINT_FIELD:
3200 if (!arg->field.field) {
3201 arg->field.field = pevent_find_any_field(event, arg->field.name);
3202 if (!arg->field.field)
3203 die("field %s not found", arg->field.name);
3204 }
3205 /* Zero sized fields, mean the rest of the data */
3206 len = arg->field.field->size ? : size - arg->field.field->offset;
3207
3208 /*
3209 * Some events pass in pointers. If this is not an array
3210 * and the size is the same as long_size, assume that it
3211 * is a pointer.
3212 */
3213 if (!(arg->field.field->flags & FIELD_IS_ARRAY) &&
3214 arg->field.field->size == pevent->long_size) {
3215 addr = *(unsigned long *)(data + arg->field.field->offset);
3216 trace_seq_printf(s, "%lx", addr);
3217 break;
3218 }
3219 str = malloc_or_die(len + 1);
3220 memcpy(str, data + arg->field.field->offset, len);
3221 str[len] = 0;
3222 print_str_to_seq(s, format, len_arg, str);
3223 free(str);
3224 break;
3225 case PRINT_FLAGS:
3226 val = eval_num_arg(data, size, event, arg->flags.field);
3227 print = 0;
3228 for (flag = arg->flags.flags; flag; flag = flag->next) {
3229 fval = eval_flag(flag->value);
3230 if (!val && !fval) {
3231 print_str_to_seq(s, format, len_arg, flag->str);
3232 break;
3233 }
3234 if (fval && (val & fval) == fval) {
3235 if (print && arg->flags.delim)
3236 trace_seq_puts(s, arg->flags.delim);
3237 print_str_to_seq(s, format, len_arg, flag->str);
3238 print = 1;
3239 val &= ~fval;
3240 }
3241 }
3242 break;
3243 case PRINT_SYMBOL:
3244 val = eval_num_arg(data, size, event, arg->symbol.field);
3245 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3246 fval = eval_flag(flag->value);
3247 if (val == fval) {
3248 print_str_to_seq(s, format, len_arg, flag->str);
3249 break;
3250 }
3251 }
3252 break;
3253
3254 case PRINT_TYPE:
3255 break;
3256 case PRINT_STRING: {
3257 int str_offset;
3258
3259 if (arg->string.offset == -1) {
3260 struct format_field *f;
3261
3262 f = pevent_find_any_field(event, arg->string.string);
3263 arg->string.offset = f->offset;
3264 }
3265 str_offset = data2host4(pevent, data + arg->string.offset);
3266 str_offset &= 0xffff;
3267 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3268 break;
3269 }
3270 case PRINT_BSTRING:
3271 trace_seq_printf(s, format, arg->string.string);
3272 break;
3273 case PRINT_OP:
3274 /*
3275 * The only op for string should be ? :
3276 */
3277 if (arg->op.op[0] != '?')
3278 return;
3279 val = eval_num_arg(data, size, event, arg->op.left);
3280 if (val)
3281 print_str_arg(s, data, size, event,
3282 format, len_arg, arg->op.right->op.left);
3283 else
3284 print_str_arg(s, data, size, event,
3285 format, len_arg, arg->op.right->op.right);
3286 break;
3287 case PRINT_FUNC:
3288 process_defined_func(s, data, size, event, arg);
3289 break;
3290 default:
3291 /* well... */
3292 break;
3293 }
3294}
3295
3296static unsigned long long
3297process_defined_func(struct trace_seq *s, void *data, int size,
3298 struct event_format *event, struct print_arg *arg)
3299{
3300 struct pevent_function_handler *func_handle = arg->func.func;
3301 struct pevent_func_params *param;
3302 unsigned long long *args;
3303 unsigned long long ret;
3304 struct print_arg *farg;
3305 struct trace_seq str;
3306 struct save_str {
3307 struct save_str *next;
3308 char *str;
3309 } *strings = NULL, *string;
3310 int i;
3311
3312 if (!func_handle->nr_args) {
3313 ret = (*func_handle->func)(s, NULL);
3314 goto out;
3315 }
3316
3317 farg = arg->func.args;
3318 param = func_handle->params;
3319
3320 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3321 for (i = 0; i < func_handle->nr_args; i++) {
3322 switch (param->type) {
3323 case PEVENT_FUNC_ARG_INT:
3324 case PEVENT_FUNC_ARG_LONG:
3325 case PEVENT_FUNC_ARG_PTR:
3326 args[i] = eval_num_arg(data, size, event, farg);
3327 break;
3328 case PEVENT_FUNC_ARG_STRING:
3329 trace_seq_init(&str);
3330 print_str_arg(&str, data, size, event, "%s", -1, farg);
3331 trace_seq_terminate(&str);
3332 string = malloc_or_die(sizeof(*string));
3333 string->next = strings;
3334 string->str = strdup(str.buffer);
3335 strings = string;
3336 trace_seq_destroy(&str);
3337 break;
3338 default:
3339 /*
3340 * Something went totally wrong, this is not
3341 * an input error, something in this code broke.
3342 */
3343 die("Unexpected end of arguments\n");
3344 break;
3345 }
3346 farg = farg->next;
3347 }
3348
3349 ret = (*func_handle->func)(s, args);
3350 free(args);
3351 while (strings) {
3352 string = strings;
3353 strings = string->next;
3354 free(string->str);
3355 free(string);
3356 }
3357
3358 out:
3359 /* TBD : handle return type here */
3360 return ret;
3361}
3362
3363static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3364{
3365 struct pevent *pevent = event->pevent;
3366 struct format_field *field, *ip_field;
3367 struct print_arg *args, *arg, **next;
3368 unsigned long long ip, val;
3369 char *ptr;
3370 void *bptr;
3371
3372 field = pevent->bprint_buf_field;
3373 ip_field = pevent->bprint_ip_field;
3374
3375 if (!field) {
3376 field = pevent_find_field(event, "buf");
3377 if (!field)
3378 die("can't find buffer field for binary printk");
3379 ip_field = pevent_find_field(event, "ip");
3380 if (!ip_field)
3381 die("can't find ip field for binary printk");
3382 pevent->bprint_buf_field = field;
3383 pevent->bprint_ip_field = ip_field;
3384 }
3385
3386 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3387
3388 /*
3389 * The first arg is the IP pointer.
3390 */
3391 args = alloc_arg();
3392 arg = args;
3393 arg->next = NULL;
3394 next = &arg->next;
3395
3396 arg->type = PRINT_ATOM;
3397 arg->atom.atom = malloc_or_die(32);
3398 sprintf(arg->atom.atom, "%lld", ip);
3399
3400 /* skip the first "%pf : " */
3401 for (ptr = fmt + 6, bptr = data + field->offset;
3402 bptr < data + size && *ptr; ptr++) {
3403 int ls = 0;
3404
3405 if (*ptr == '%') {
3406 process_again:
3407 ptr++;
3408 switch (*ptr) {
3409 case '%':
3410 break;
3411 case 'l':
3412 ls++;
3413 goto process_again;
3414 case 'L':
3415 ls = 2;
3416 goto process_again;
3417 case '0' ... '9':
3418 goto process_again;
3419 case 'p':
3420 ls = 1;
3421 /* fall through */
3422 case 'd':
3423 case 'u':
3424 case 'x':
3425 case 'i':
3426 /* the pointers are always 4 bytes aligned */
3427 bptr = (void *)(((unsigned long)bptr + 3) &
3428 ~3);
3429 switch (ls) {
3430 case 0:
3431 ls = 4;
3432 break;
3433 case 1:
3434 ls = pevent->long_size;
3435 break;
3436 case 2:
3437 ls = 8;
3438 default:
3439 break;
3440 }
3441 val = pevent_read_number(pevent, bptr, ls);
3442 bptr += ls;
3443 arg = alloc_arg();
3444 arg->next = NULL;
3445 arg->type = PRINT_ATOM;
3446 arg->atom.atom = malloc_or_die(32);
3447 sprintf(arg->atom.atom, "%lld", val);
3448 *next = arg;
3449 next = &arg->next;
3450 break;
3451 case 's':
3452 arg = alloc_arg();
3453 arg->next = NULL;
3454 arg->type = PRINT_BSTRING;
3455 arg->string.string = strdup(bptr);
3456 bptr += strlen(bptr) + 1;
3457 *next = arg;
3458 next = &arg->next;
3459 default:
3460 break;
3461 }
3462 }
3463 }
3464
3465 return args;
3466}
3467
3468static void free_args(struct print_arg *args)
3469{
3470 struct print_arg *next;
3471
3472 while (args) {
3473 next = args->next;
3474
3475 free_arg(args);
3476 args = next;
3477 }
3478}
3479
3480static char *
3481get_bprint_format(void *data, int size __unused, struct event_format *event)
3482{
3483 struct pevent *pevent = event->pevent;
3484 unsigned long long addr;
3485 struct format_field *field;
3486 struct printk_map *printk;
3487 char *format;
3488 char *p;
3489
3490 field = pevent->bprint_fmt_field;
3491
3492 if (!field) {
3493 field = pevent_find_field(event, "fmt");
3494 if (!field)
3495 die("can't find format field for binary printk");
3496 pevent->bprint_fmt_field = field;
3497 }
3498
3499 addr = pevent_read_number(pevent, data + field->offset, field->size);
3500
3501 printk = find_printk(pevent, addr);
3502 if (!printk) {
3503 format = malloc_or_die(45);
3504 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3505 addr);
3506 return format;
3507 }
3508
3509 p = printk->printk;
3510 /* Remove any quotes. */
3511 if (*p == '"')
3512 p++;
3513 format = malloc_or_die(strlen(p) + 10);
3514 sprintf(format, "%s : %s", "%pf", p);
3515 /* remove ending quotes and new line since we will add one too */
3516 p = format + strlen(format) - 1;
3517 if (*p == '"')
3518 *p = 0;
3519
3520 p -= 2;
3521 if (strcmp(p, "\\n") == 0)
3522 *p = 0;
3523
3524 return format;
3525}
3526
3527static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3528 struct event_format *event, struct print_arg *arg)
3529{
3530 unsigned char *buf;
3531 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3532
3533 if (arg->type == PRINT_FUNC) {
3534 process_defined_func(s, data, size, event, arg);
3535 return;
3536 }
3537
3538 if (arg->type != PRINT_FIELD) {
3539 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3540 arg->type);
3541 return;
3542 }
3543
3544 if (mac == 'm')
3545 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3546 if (!arg->field.field) {
3547 arg->field.field =
3548 pevent_find_any_field(event, arg->field.name);
3549 if (!arg->field.field)
3550 die("field %s not found", arg->field.name);
3551 }
3552 if (arg->field.field->size != 6) {
3553 trace_seq_printf(s, "INVALIDMAC");
3554 return;
3555 }
3556 buf = data + arg->field.field->offset;
3557 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3558}
3559
3560static void print_event_fields(struct trace_seq *s, void *data, int size,
3561 struct event_format *event)
3562{
3563 struct format_field *field;
3564 unsigned long long val;
3565 unsigned int offset, len, i;
3566
3567 field = event->format.fields;
3568 while (field) {
3569 trace_seq_printf(s, " %s=", field->name);
3570 if (field->flags & FIELD_IS_ARRAY) {
3571 offset = field->offset;
3572 len = field->size;
3573 if (field->flags & FIELD_IS_DYNAMIC) {
3574 val = pevent_read_number(event->pevent, data + offset, len);
3575 offset = val;
3576 len = offset >> 16;
3577 offset &= 0xffff;
3578 }
3579 if (field->flags & FIELD_IS_STRING) {
3580 trace_seq_printf(s, "%s", (char *)data + offset);
3581 } else {
3582 trace_seq_puts(s, "ARRAY[");
3583 for (i = 0; i < len; i++) {
3584 if (i)
3585 trace_seq_puts(s, ", ");
3586 trace_seq_printf(s, "%02x",
3587 *((unsigned char *)data + offset + i));
3588 }
3589 trace_seq_putc(s, ']');
3590 }
3591 } else {
3592 val = pevent_read_number(event->pevent, data + field->offset,
3593 field->size);
3594 if (field->flags & FIELD_IS_POINTER) {
3595 trace_seq_printf(s, "0x%llx", val);
3596 } else if (field->flags & FIELD_IS_SIGNED) {
3597 switch (field->size) {
3598 case 4:
3599 /*
3600 * If field is long then print it in hex.
3601 * A long usually stores pointers.
3602 */
3603 if (field->flags & FIELD_IS_LONG)
3604 trace_seq_printf(s, "0x%x", (int)val);
3605 else
3606 trace_seq_printf(s, "%d", (int)val);
3607 break;
3608 case 2:
3609 trace_seq_printf(s, "%2d", (short)val);
3610 break;
3611 case 1:
3612 trace_seq_printf(s, "%1d", (char)val);
3613 break;
3614 default:
3615 trace_seq_printf(s, "%lld", val);
3616 }
3617 } else {
3618 if (field->flags & FIELD_IS_LONG)
3619 trace_seq_printf(s, "0x%llx", val);
3620 else
3621 trace_seq_printf(s, "%llu", val);
3622 }
3623 }
3624 field = field->next;
3625 }
3626}
3627
3628static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3629{
3630 struct pevent *pevent = event->pevent;
3631 struct print_fmt *print_fmt = &event->print_fmt;
3632 struct print_arg *arg = print_fmt->args;
3633 struct print_arg *args = NULL;
3634 const char *ptr = print_fmt->format;
3635 unsigned long long val;
3636 struct func_map *func;
3637 const char *saveptr;
3638 char *bprint_fmt = NULL;
3639 char format[32];
3640 int show_func;
3641 int len_as_arg;
3642 int len_arg;
3643 int len;
3644 int ls;
3645
3646 if (event->flags & EVENT_FL_FAILED) {
3647 trace_seq_printf(s, "[FAILED TO PARSE]");
3648 print_event_fields(s, data, size, event);
3649 return;
3650 }
3651
3652 if (event->flags & EVENT_FL_ISBPRINT) {
3653 bprint_fmt = get_bprint_format(data, size, event);
3654 args = make_bprint_args(bprint_fmt, data, size, event);
3655 arg = args;
3656 ptr = bprint_fmt;
3657 }
3658
3659 for (; *ptr; ptr++) {
3660 ls = 0;
3661 if (*ptr == '\\') {
3662 ptr++;
3663 switch (*ptr) {
3664 case 'n':
3665 trace_seq_putc(s, '\n');
3666 break;
3667 case 't':
3668 trace_seq_putc(s, '\t');
3669 break;
3670 case 'r':
3671 trace_seq_putc(s, '\r');
3672 break;
3673 case '\\':
3674 trace_seq_putc(s, '\\');
3675 break;
3676 default:
3677 trace_seq_putc(s, *ptr);
3678 break;
3679 }
3680
3681 } else if (*ptr == '%') {
3682 saveptr = ptr;
3683 show_func = 0;
3684 len_as_arg = 0;
3685 cont_process:
3686 ptr++;
3687 switch (*ptr) {
3688 case '%':
3689 trace_seq_putc(s, '%');
3690 break;
3691 case '#':
3692 /* FIXME: need to handle properly */
3693 goto cont_process;
3694 case 'h':
3695 ls--;
3696 goto cont_process;
3697 case 'l':
3698 ls++;
3699 goto cont_process;
3700 case 'L':
3701 ls = 2;
3702 goto cont_process;
3703 case '*':
3704 /* The argument is the length. */
3705 if (!arg)
3706 die("no argument match");
3707 len_arg = eval_num_arg(data, size, event, arg);
3708 len_as_arg = 1;
3709 arg = arg->next;
3710 goto cont_process;
3711 case '.':
3712 case 'z':
3713 case 'Z':
3714 case '0' ... '9':
3715 goto cont_process;
3716 case 'p':
3717 if (pevent->long_size == 4)
3718 ls = 1;
3719 else
3720 ls = 2;
3721
3722 if (*(ptr+1) == 'F' ||
3723 *(ptr+1) == 'f') {
3724 ptr++;
3725 show_func = *ptr;
3726 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3727 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3728 ptr++;
3729 break;
3730 }
3731
3732 /* fall through */
3733 case 'd':
3734 case 'i':
3735 case 'x':
3736 case 'X':
3737 case 'u':
3738 if (!arg)
3739 die("no argument match");
3740
3741 len = ((unsigned long)ptr + 1) -
3742 (unsigned long)saveptr;
3743
3744 /* should never happen */
3745 if (len > 31)
3746 die("bad format!");
3747
3748 memcpy(format, saveptr, len);
3749 format[len] = 0;
3750
3751 val = eval_num_arg(data, size, event, arg);
3752 arg = arg->next;
3753
3754 if (show_func) {
3755 func = find_func(pevent, val);
3756 if (func) {
3757 trace_seq_puts(s, func->func);
3758 if (show_func == 'F')
3759 trace_seq_printf(s,
3760 "+0x%llx",
3761 val - func->addr);
3762 break;
3763 }
3764 }
3765 if (pevent->long_size == 8 && ls) {
3766 char *p;
3767
3768 ls = 2;
3769 /* make %l into %ll */
3770 p = strchr(format, 'l');
3771 if (p)
3772 memmove(p, p+1, strlen(p)+1);
3773 else if (strcmp(format, "%p") == 0)
3774 strcpy(format, "0x%llx");
3775 }
3776 switch (ls) {
3777 case -2:
3778 if (len_as_arg)
3779 trace_seq_printf(s, format, len_arg, (char)val);
3780 else
3781 trace_seq_printf(s, format, (char)val);
3782 break;
3783 case -1:
3784 if (len_as_arg)
3785 trace_seq_printf(s, format, len_arg, (short)val);
3786 else
3787 trace_seq_printf(s, format, (short)val);
3788 break;
3789 case 0:
3790 if (len_as_arg)
3791 trace_seq_printf(s, format, len_arg, (int)val);
3792 else
3793 trace_seq_printf(s, format, (int)val);
3794 break;
3795 case 1:
3796 if (len_as_arg)
3797 trace_seq_printf(s, format, len_arg, (long)val);
3798 else
3799 trace_seq_printf(s, format, (long)val);
3800 break;
3801 case 2:
3802 if (len_as_arg)
3803 trace_seq_printf(s, format, len_arg,
3804 (long long)val);
3805 else
3806 trace_seq_printf(s, format, (long long)val);
3807 break;
3808 default:
3809 die("bad count (%d)", ls);
3810 }
3811 break;
3812 case 's':
3813 if (!arg)
3814 die("no matching argument");
3815
3816 len = ((unsigned long)ptr + 1) -
3817 (unsigned long)saveptr;
3818
3819 /* should never happen */
3820 if (len > 31)
3821 die("bad format!");
3822
3823 memcpy(format, saveptr, len);
3824 format[len] = 0;
3825 if (!len_as_arg)
3826 len_arg = -1;
3827 print_str_arg(s, data, size, event,
3828 format, len_arg, arg);
3829 arg = arg->next;
3830 break;
3831 default:
3832 trace_seq_printf(s, ">%c<", *ptr);
3833
3834 }
3835 } else
3836 trace_seq_putc(s, *ptr);
3837 }
3838
3839 if (args) {
3840 free_args(args);
3841 free(bprint_fmt);
3842 }
3843}
3844
3845/**
3846 * pevent_data_lat_fmt - parse the data for the latency format
3847 * @pevent: a handle to the pevent
3848 * @s: the trace_seq to write to
3849 * @data: the raw data to read from
3850 * @size: currently unused.
3851 *
3852 * This parses out the Latency format (interrupts disabled,
3853 * need rescheduling, in hard/soft interrupt, preempt count
3854 * and lock depth) and places it into the trace_seq.
3855 */
3856void pevent_data_lat_fmt(struct pevent *pevent,
3857 struct trace_seq *s, struct record *record)
3858{
3859 static int check_lock_depth = 1;
3860 static int lock_depth_exists;
3861 unsigned int lat_flags;
3862 unsigned int pc;
3863 int lock_depth;
3864 int hardirq;
3865 int softirq;
3866 void *data = record->data;
3867
3868 lat_flags = parse_common_flags(pevent, data);
3869 pc = parse_common_pc(pevent, data);
3870 /* lock_depth may not always exist */
3871 if (check_lock_depth) {
3872 struct format_field *field;
3873 struct event_format *event;
3874
3875 check_lock_depth = 0;
3876 event = pevent->events[0];
3877 field = pevent_find_common_field(event, "common_lock_depth");
3878 if (field)
3879 lock_depth_exists = 1;
3880 }
3881 if (lock_depth_exists)
3882 lock_depth = parse_common_lock_depth(pevent, data);
3883
3884 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
3885 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
3886
3887 trace_seq_printf(s, "%c%c%c",
3888 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
3889 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
3890 'X' : '.',
3891 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
3892 'N' : '.',
3893 (hardirq && softirq) ? 'H' :
3894 hardirq ? 'h' : softirq ? 's' : '.');
3895
3896 if (pc)
3897 trace_seq_printf(s, "%x", pc);
3898 else
3899 trace_seq_putc(s, '.');
3900
3901 if (lock_depth_exists) {
3902 if (lock_depth < 0)
3903 trace_seq_putc(s, '.');
3904 else
3905 trace_seq_printf(s, "%d", lock_depth);
3906 }
3907
3908 trace_seq_terminate(s);
3909}
3910
3911/**
3912 * pevent_data_type - parse out the given event type
3913 * @pevent: a handle to the pevent
3914 * @rec: the record to read from
3915 *
3916 * This returns the event id from the @rec.
3917 */
3918int pevent_data_type(struct pevent *pevent, struct record *rec)
3919{
3920 return trace_parse_common_type(pevent, rec->data);
3921}
3922
3923/**
3924 * pevent_data_event_from_type - find the event by a given type
3925 * @pevent: a handle to the pevent
3926 * @type: the type of the event.
3927 *
3928 * This returns the event form a given @type;
3929 */
3930struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
3931{
3932 return pevent_find_event(pevent, type);
3933}
3934
3935/**
3936 * pevent_data_pid - parse the PID from raw data
3937 * @pevent: a handle to the pevent
3938 * @rec: the record to parse
3939 *
3940 * This returns the PID from a raw data.
3941 */
3942int pevent_data_pid(struct pevent *pevent, struct record *rec)
3943{
3944 return parse_common_pid(pevent, rec->data);
3945}
3946
3947/**
3948 * pevent_data_comm_from_pid - return the command line from PID
3949 * @pevent: a handle to the pevent
3950 * @pid: the PID of the task to search for
3951 *
3952 * This returns a pointer to the command line that has the given
3953 * @pid.
3954 */
3955const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
3956{
3957 const char *comm;
3958
3959 comm = find_cmdline(pevent, pid);
3960 return comm;
3961}
3962
3963/**
3964 * pevent_data_comm_from_pid - parse the data into the print format
3965 * @s: the trace_seq to write to
3966 * @event: the handle to the event
3967 * @cpu: the cpu the event was recorded on
3968 * @data: the raw data
3969 * @size: the size of the raw data
3970 * @nsecs: the timestamp of the event
3971 *
3972 * This parses the raw @data using the given @event information and
3973 * writes the print format into the trace_seq.
3974 */
3975void pevent_event_info(struct trace_seq *s, struct event_format *event,
3976 struct record *record)
3977{
3978 int print_pretty = 1;
3979
3980 if (event->pevent->print_raw)
3981 print_event_fields(s, record->data, record->size, event);
3982 else {
3983
3984 if (event->handler)
3985 print_pretty = event->handler(s, record, event,
3986 event->context);
3987
3988 if (print_pretty)
3989 pretty_print(s, record->data, record->size, event);
3990 }
3991
3992 trace_seq_terminate(s);
3993}
3994
3995void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
3996 struct record *record)
3997{
3998 static char *spaces = " "; /* 20 spaces */
3999 struct event_format *event;
4000 unsigned long secs;
4001 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004002 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004003 const char *comm;
4004 void *data = record->data;
4005 int type;
4006 int pid;
4007 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004008 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004009
4010 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004011 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004012
4013 if (record->size < 0) {
4014 do_warning("ug! negative record size %d", record->size);
4015 return;
4016 }
4017
4018 type = trace_parse_common_type(pevent, data);
4019
4020 event = pevent_find_event(pevent, type);
4021 if (!event) {
4022 do_warning("ug! no event found for type %d", type);
4023 return;
4024 }
4025
4026 pid = parse_common_pid(pevent, data);
4027 comm = find_cmdline(pevent, pid);
4028
4029 if (pevent->latency_format) {
4030 trace_seq_printf(s, "%8.8s-%-5d %3d",
4031 comm, pid, record->cpu);
4032 pevent_data_lat_fmt(pevent, s, record);
4033 } else
4034 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4035
Steven Rostedt4dc10242012-04-06 00:47:57 +02004036 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4037 usecs = nsecs;
4038 p = 9;
4039 } else {
4040 usecs = (nsecs + 500) / NSECS_PER_USEC;
4041 p = 6;
4042 }
4043
4044 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004045
4046 /* Space out the event names evenly. */
4047 len = strlen(event->name);
4048 if (len < 20)
4049 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4050
4051 pevent_event_info(s, event, record);
4052}
4053
4054static int events_id_cmp(const void *a, const void *b)
4055{
4056 struct event_format * const * ea = a;
4057 struct event_format * const * eb = b;
4058
4059 if ((*ea)->id < (*eb)->id)
4060 return -1;
4061
4062 if ((*ea)->id > (*eb)->id)
4063 return 1;
4064
4065 return 0;
4066}
4067
4068static int events_name_cmp(const void *a, const void *b)
4069{
4070 struct event_format * const * ea = a;
4071 struct event_format * const * eb = b;
4072 int res;
4073
4074 res = strcmp((*ea)->name, (*eb)->name);
4075 if (res)
4076 return res;
4077
4078 res = strcmp((*ea)->system, (*eb)->system);
4079 if (res)
4080 return res;
4081
4082 return events_id_cmp(a, b);
4083}
4084
4085static int events_system_cmp(const void *a, const void *b)
4086{
4087 struct event_format * const * ea = a;
4088 struct event_format * const * eb = b;
4089 int res;
4090
4091 res = strcmp((*ea)->system, (*eb)->system);
4092 if (res)
4093 return res;
4094
4095 res = strcmp((*ea)->name, (*eb)->name);
4096 if (res)
4097 return res;
4098
4099 return events_id_cmp(a, b);
4100}
4101
4102struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4103{
4104 struct event_format **events;
4105 int (*sort)(const void *a, const void *b);
4106
4107 events = pevent->sort_events;
4108
4109 if (events && pevent->last_type == sort_type)
4110 return events;
4111
4112 if (!events) {
4113 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4114 if (!events)
4115 return NULL;
4116
4117 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4118 events[pevent->nr_events] = NULL;
4119
4120 pevent->sort_events = events;
4121
4122 /* the internal events are sorted by id */
4123 if (sort_type == EVENT_SORT_ID) {
4124 pevent->last_type = sort_type;
4125 return events;
4126 }
4127 }
4128
4129 switch (sort_type) {
4130 case EVENT_SORT_ID:
4131 sort = events_id_cmp;
4132 break;
4133 case EVENT_SORT_NAME:
4134 sort = events_name_cmp;
4135 break;
4136 case EVENT_SORT_SYSTEM:
4137 sort = events_system_cmp;
4138 break;
4139 default:
4140 return events;
4141 }
4142
4143 qsort(events, pevent->nr_events, sizeof(*events), sort);
4144 pevent->last_type = sort_type;
4145
4146 return events;
4147}
4148
4149static struct format_field **
4150get_event_fields(const char *type, const char *name,
4151 int count, struct format_field *list)
4152{
4153 struct format_field **fields;
4154 struct format_field *field;
4155 int i = 0;
4156
4157 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4158 for (field = list; field; field = field->next) {
4159 fields[i++] = field;
4160 if (i == count + 1) {
4161 do_warning("event %s has more %s fields than specified",
4162 name, type);
4163 i--;
4164 break;
4165 }
4166 }
4167
4168 if (i != count)
4169 do_warning("event %s has less %s fields than specified",
4170 name, type);
4171
4172 fields[i] = NULL;
4173
4174 return fields;
4175}
4176
4177/**
4178 * pevent_event_common_fields - return a list of common fields for an event
4179 * @event: the event to return the common fields of.
4180 *
4181 * Returns an allocated array of fields. The last item in the array is NULL.
4182 * The array must be freed with free().
4183 */
4184struct format_field **pevent_event_common_fields(struct event_format *event)
4185{
4186 return get_event_fields("common", event->name,
4187 event->format.nr_common,
4188 event->format.common_fields);
4189}
4190
4191/**
4192 * pevent_event_fields - return a list of event specific fields for an event
4193 * @event: the event to return the fields of.
4194 *
4195 * Returns an allocated array of fields. The last item in the array is NULL.
4196 * The array must be freed with free().
4197 */
4198struct format_field **pevent_event_fields(struct event_format *event)
4199{
4200 return get_event_fields("event", event->name,
4201 event->format.nr_fields,
4202 event->format.fields);
4203}
4204
4205static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4206{
4207 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4208 if (field->next) {
4209 trace_seq_puts(s, ", ");
4210 print_fields(s, field->next);
4211 }
4212}
4213
4214/* for debugging */
4215static void print_args(struct print_arg *args)
4216{
4217 int print_paren = 1;
4218 struct trace_seq s;
4219
4220 switch (args->type) {
4221 case PRINT_NULL:
4222 printf("null");
4223 break;
4224 case PRINT_ATOM:
4225 printf("%s", args->atom.atom);
4226 break;
4227 case PRINT_FIELD:
4228 printf("REC->%s", args->field.name);
4229 break;
4230 case PRINT_FLAGS:
4231 printf("__print_flags(");
4232 print_args(args->flags.field);
4233 printf(", %s, ", args->flags.delim);
4234 trace_seq_init(&s);
4235 print_fields(&s, args->flags.flags);
4236 trace_seq_do_printf(&s);
4237 trace_seq_destroy(&s);
4238 printf(")");
4239 break;
4240 case PRINT_SYMBOL:
4241 printf("__print_symbolic(");
4242 print_args(args->symbol.field);
4243 printf(", ");
4244 trace_seq_init(&s);
4245 print_fields(&s, args->symbol.symbols);
4246 trace_seq_do_printf(&s);
4247 trace_seq_destroy(&s);
4248 printf(")");
4249 break;
4250 case PRINT_STRING:
4251 case PRINT_BSTRING:
4252 printf("__get_str(%s)", args->string.string);
4253 break;
4254 case PRINT_TYPE:
4255 printf("(%s)", args->typecast.type);
4256 print_args(args->typecast.item);
4257 break;
4258 case PRINT_OP:
4259 if (strcmp(args->op.op, ":") == 0)
4260 print_paren = 0;
4261 if (print_paren)
4262 printf("(");
4263 print_args(args->op.left);
4264 printf(" %s ", args->op.op);
4265 print_args(args->op.right);
4266 if (print_paren)
4267 printf(")");
4268 break;
4269 default:
4270 /* we should warn... */
4271 return;
4272 }
4273 if (args->next) {
4274 printf("\n");
4275 print_args(args->next);
4276 }
4277}
4278
4279static void parse_header_field(const char *field,
4280 int *offset, int *size, int mandatory)
4281{
4282 unsigned long long save_input_buf_ptr;
4283 unsigned long long save_input_buf_siz;
4284 char *token;
4285 int type;
4286
4287 save_input_buf_ptr = input_buf_ptr;
4288 save_input_buf_siz = input_buf_siz;
4289
4290 if (read_expected(EVENT_ITEM, "field") < 0)
4291 return;
4292 if (read_expected(EVENT_OP, ":") < 0)
4293 return;
4294
4295 /* type */
4296 if (read_expect_type(EVENT_ITEM, &token) < 0)
4297 goto fail;
4298 free_token(token);
4299
4300 /*
4301 * If this is not a mandatory field, then test it first.
4302 */
4303 if (mandatory) {
4304 if (read_expected(EVENT_ITEM, field) < 0)
4305 return;
4306 } else {
4307 if (read_expect_type(EVENT_ITEM, &token) < 0)
4308 goto fail;
4309 if (strcmp(token, field) != 0)
4310 goto discard;
4311 free_token(token);
4312 }
4313
4314 if (read_expected(EVENT_OP, ";") < 0)
4315 return;
4316 if (read_expected(EVENT_ITEM, "offset") < 0)
4317 return;
4318 if (read_expected(EVENT_OP, ":") < 0)
4319 return;
4320 if (read_expect_type(EVENT_ITEM, &token) < 0)
4321 goto fail;
4322 *offset = atoi(token);
4323 free_token(token);
4324 if (read_expected(EVENT_OP, ";") < 0)
4325 return;
4326 if (read_expected(EVENT_ITEM, "size") < 0)
4327 return;
4328 if (read_expected(EVENT_OP, ":") < 0)
4329 return;
4330 if (read_expect_type(EVENT_ITEM, &token) < 0)
4331 goto fail;
4332 *size = atoi(token);
4333 free_token(token);
4334 if (read_expected(EVENT_OP, ";") < 0)
4335 return;
4336 type = read_token(&token);
4337 if (type != EVENT_NEWLINE) {
4338 /* newer versions of the kernel have a "signed" type */
4339 if (type != EVENT_ITEM)
4340 goto fail;
4341
4342 if (strcmp(token, "signed") != 0)
4343 goto fail;
4344
4345 free_token(token);
4346
4347 if (read_expected(EVENT_OP, ":") < 0)
4348 return;
4349
4350 if (read_expect_type(EVENT_ITEM, &token))
4351 goto fail;
4352
4353 free_token(token);
4354 if (read_expected(EVENT_OP, ";") < 0)
4355 return;
4356
4357 if (read_expect_type(EVENT_NEWLINE, &token))
4358 goto fail;
4359 }
4360 fail:
4361 free_token(token);
4362 return;
4363
4364 discard:
4365 input_buf_ptr = save_input_buf_ptr;
4366 input_buf_siz = save_input_buf_siz;
4367 *offset = 0;
4368 *size = 0;
4369 free_token(token);
4370}
4371
4372/**
4373 * pevent_parse_header_page - parse the data stored in the header page
4374 * @pevent: the handle to the pevent
4375 * @buf: the buffer storing the header page format string
4376 * @size: the size of @buf
4377 * @long_size: the long size to use if there is no header
4378 *
4379 * This parses the header page format for information on the
4380 * ring buffer used. The @buf should be copied from
4381 *
4382 * /sys/kernel/debug/tracing/events/header_page
4383 */
4384int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4385 int long_size)
4386{
4387 int ignore;
4388
4389 if (!size) {
4390 /*
4391 * Old kernels did not have header page info.
4392 * Sorry but we just use what we find here in user space.
4393 */
4394 pevent->header_page_ts_size = sizeof(long long);
4395 pevent->header_page_size_size = long_size;
4396 pevent->header_page_data_offset = sizeof(long long) + long_size;
4397 pevent->old_format = 1;
4398 return -1;
4399 }
4400 init_input_buf(buf, size);
4401
4402 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4403 &pevent->header_page_ts_size, 1);
4404 parse_header_field("commit", &pevent->header_page_size_offset,
4405 &pevent->header_page_size_size, 1);
4406 parse_header_field("overwrite", &pevent->header_page_overwrite,
4407 &ignore, 0);
4408 parse_header_field("data", &pevent->header_page_data_offset,
4409 &pevent->header_page_data_size, 1);
4410
4411 return 0;
4412}
4413
4414static int event_matches(struct event_format *event,
4415 int id, const char *sys_name,
4416 const char *event_name)
4417{
4418 if (id >= 0 && id != event->id)
4419 return 0;
4420
4421 if (event_name && (strcmp(event_name, event->name) != 0))
4422 return 0;
4423
4424 if (sys_name && (strcmp(sys_name, event->system) != 0))
4425 return 0;
4426
4427 return 1;
4428}
4429
4430static void free_handler(struct event_handler *handle)
4431{
4432 free((void *)handle->sys_name);
4433 free((void *)handle->event_name);
4434 free(handle);
4435}
4436
4437static int find_event_handle(struct pevent *pevent, struct event_format *event)
4438{
4439 struct event_handler *handle, **next;
4440
4441 for (next = &pevent->handlers; *next;
4442 next = &(*next)->next) {
4443 handle = *next;
4444 if (event_matches(event, handle->id,
4445 handle->sys_name,
4446 handle->event_name))
4447 break;
4448 }
4449
4450 if (!(*next))
4451 return 0;
4452
4453 pr_stat("overriding event (%d) %s:%s with new print handler",
4454 event->id, event->system, event->name);
4455
4456 event->handler = handle->func;
4457 event->context = handle->context;
4458
4459 *next = handle->next;
4460 free_handler(handle);
4461
4462 return 1;
4463}
4464
4465/**
4466 * pevent_parse_event - parse the event format
4467 * @pevent: the handle to the pevent
4468 * @buf: the buffer storing the event format string
4469 * @size: the size of @buf
4470 * @sys: the system the event belongs to
4471 *
4472 * This parses the event format and creates an event structure
4473 * to quickly parse raw data for a given event.
4474 *
4475 * These files currently come from:
4476 *
4477 * /sys/kernel/debug/tracing/events/.../.../format
4478 */
4479int pevent_parse_event(struct pevent *pevent,
4480 const char *buf, unsigned long size,
4481 const char *sys)
4482{
4483 struct event_format *event;
4484 int ret;
4485
4486 init_input_buf(buf, size);
4487
4488 event = alloc_event();
4489 if (!event)
4490 return -ENOMEM;
4491
4492 event->name = event_read_name();
4493 if (!event->name) {
4494 /* Bad event? */
4495 free(event);
4496 return -1;
4497 }
4498
4499 if (strcmp(sys, "ftrace") == 0) {
4500
4501 event->flags |= EVENT_FL_ISFTRACE;
4502
4503 if (strcmp(event->name, "bprint") == 0)
4504 event->flags |= EVENT_FL_ISBPRINT;
4505 }
4506
4507 event->id = event_read_id();
4508 if (event->id < 0)
4509 die("failed to read event id");
4510
4511 event->system = strdup(sys);
4512
4513 /* Add pevent to event so that it can be referenced */
4514 event->pevent = pevent;
4515
4516 ret = event_read_format(event);
4517 if (ret < 0) {
4518 do_warning("failed to read event format for %s", event->name);
4519 goto event_failed;
4520 }
4521
4522 /*
4523 * If the event has an override, don't print warnings if the event
4524 * print format fails to parse.
4525 */
4526 if (find_event_handle(pevent, event))
4527 show_warning = 0;
4528
4529 ret = event_read_print(event);
4530 if (ret < 0) {
4531 do_warning("failed to read event print fmt for %s",
4532 event->name);
4533 show_warning = 1;
4534 goto event_failed;
4535 }
4536 show_warning = 1;
4537
4538 add_event(pevent, event);
4539
4540 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4541 struct format_field *field;
4542 struct print_arg *arg, **list;
4543
4544 /* old ftrace had no args */
4545
4546 list = &event->print_fmt.args;
4547 for (field = event->format.fields; field; field = field->next) {
4548 arg = alloc_arg();
4549 *list = arg;
4550 list = &arg->next;
4551 arg->type = PRINT_FIELD;
4552 arg->field.name = strdup(field->name);
4553 arg->field.field = field;
4554 }
4555 return 0;
4556 }
4557
4558#define PRINT_ARGS 0
4559 if (PRINT_ARGS && event->print_fmt.args)
4560 print_args(event->print_fmt.args);
4561
4562 return 0;
4563
4564 event_failed:
4565 event->flags |= EVENT_FL_FAILED;
4566 /* still add it even if it failed */
4567 add_event(pevent, event);
4568 return -1;
4569}
4570
4571int get_field_val(struct trace_seq *s, struct format_field *field,
4572 const char *name, struct record *record,
4573 unsigned long long *val, int err)
4574{
4575 if (!field) {
4576 if (err)
4577 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4578 return -1;
4579 }
4580
4581 if (pevent_read_number_field(field, record->data, val)) {
4582 if (err)
4583 trace_seq_printf(s, " %s=INVALID", name);
4584 return -1;
4585 }
4586
4587 return 0;
4588}
4589
4590/**
4591 * pevent_get_field_raw - return the raw pointer into the data field
4592 * @s: The seq to print to on error
4593 * @event: the event that the field is for
4594 * @name: The name of the field
4595 * @record: The record with the field name.
4596 * @len: place to store the field length.
4597 * @err: print default error if failed.
4598 *
4599 * Returns a pointer into record->data of the field and places
4600 * the length of the field in @len.
4601 *
4602 * On failure, it returns NULL.
4603 */
4604void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4605 const char *name, struct record *record,
4606 int *len, int err)
4607{
4608 struct format_field *field;
4609 void *data = record->data;
4610 unsigned offset;
4611 int dummy;
4612
4613 if (!event)
4614 return NULL;
4615
4616 field = pevent_find_field(event, name);
4617
4618 if (!field) {
4619 if (err)
4620 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4621 return NULL;
4622 }
4623
4624 /* Allow @len to be NULL */
4625 if (!len)
4626 len = &dummy;
4627
4628 offset = field->offset;
4629 if (field->flags & FIELD_IS_DYNAMIC) {
4630 offset = pevent_read_number(event->pevent,
4631 data + offset, field->size);
4632 *len = offset >> 16;
4633 offset &= 0xffff;
4634 } else
4635 *len = field->size;
4636
4637 return data + offset;
4638}
4639
4640/**
4641 * pevent_get_field_val - find a field and return its value
4642 * @s: The seq to print to on error
4643 * @event: the event that the field is for
4644 * @name: The name of the field
4645 * @record: The record with the field name.
4646 * @val: place to store the value of the field.
4647 * @err: print default error if failed.
4648 *
4649 * Returns 0 on success -1 on field not found.
4650 */
4651int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4652 const char *name, struct record *record,
4653 unsigned long long *val, int err)
4654{
4655 struct format_field *field;
4656
4657 if (!event)
4658 return -1;
4659
4660 field = pevent_find_field(event, name);
4661
4662 return get_field_val(s, field, name, record, val, err);
4663}
4664
4665/**
4666 * pevent_get_common_field_val - find a common field and return its value
4667 * @s: The seq to print to on error
4668 * @event: the event that the field is for
4669 * @name: The name of the field
4670 * @record: The record with the field name.
4671 * @val: place to store the value of the field.
4672 * @err: print default error if failed.
4673 *
4674 * Returns 0 on success -1 on field not found.
4675 */
4676int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4677 const char *name, struct record *record,
4678 unsigned long long *val, int err)
4679{
4680 struct format_field *field;
4681
4682 if (!event)
4683 return -1;
4684
4685 field = pevent_find_common_field(event, name);
4686
4687 return get_field_val(s, field, name, record, val, err);
4688}
4689
4690/**
4691 * pevent_get_any_field_val - find a any field and return its value
4692 * @s: The seq to print to on error
4693 * @event: the event that the field is for
4694 * @name: The name of the field
4695 * @record: The record with the field name.
4696 * @val: place to store the value of the field.
4697 * @err: print default error if failed.
4698 *
4699 * Returns 0 on success -1 on field not found.
4700 */
4701int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
4702 const char *name, struct record *record,
4703 unsigned long long *val, int err)
4704{
4705 struct format_field *field;
4706
4707 if (!event)
4708 return -1;
4709
4710 field = pevent_find_any_field(event, name);
4711
4712 return get_field_val(s, field, name, record, val, err);
4713}
4714
4715/**
4716 * pevent_print_num_field - print a field and a format
4717 * @s: The seq to print to
4718 * @fmt: The printf format to print the field with.
4719 * @event: the event that the field is for
4720 * @name: The name of the field
4721 * @record: The record with the field name.
4722 * @err: print default error if failed.
4723 *
4724 * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
4725 */
4726int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4727 struct event_format *event, const char *name,
4728 struct record *record, int err)
4729{
4730 struct format_field *field = pevent_find_field(event, name);
4731 unsigned long long val;
4732
4733 if (!field)
4734 goto failed;
4735
4736 if (pevent_read_number_field(field, record->data, &val))
4737 goto failed;
4738
4739 return trace_seq_printf(s, fmt, val);
4740
4741 failed:
4742 if (err)
4743 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
4744 return -1;
4745}
4746
4747static void free_func_handle(struct pevent_function_handler *func)
4748{
4749 struct pevent_func_params *params;
4750
4751 free(func->name);
4752
4753 while (func->params) {
4754 params = func->params;
4755 func->params = params->next;
4756 free(params);
4757 }
4758
4759 free(func);
4760}
4761
4762/**
4763 * pevent_register_print_function - register a helper function
4764 * @pevent: the handle to the pevent
4765 * @func: the function to process the helper function
4766 * @name: the name of the helper function
4767 * @parameters: A list of enum pevent_func_arg_type
4768 *
4769 * Some events may have helper functions in the print format arguments.
4770 * This allows a plugin to dynmically create a way to process one
4771 * of these functions.
4772 *
4773 * The @parameters is a variable list of pevent_func_arg_type enums that
4774 * must end with PEVENT_FUNC_ARG_VOID.
4775 */
4776int pevent_register_print_function(struct pevent *pevent,
4777 pevent_func_handler func,
4778 enum pevent_func_arg_type ret_type,
4779 char *name, ...)
4780{
4781 struct pevent_function_handler *func_handle;
4782 struct pevent_func_params **next_param;
4783 struct pevent_func_params *param;
4784 enum pevent_func_arg_type type;
4785 va_list ap;
4786
4787 func_handle = find_func_handler(pevent, name);
4788 if (func_handle) {
4789 /*
4790 * This is most like caused by the users own
4791 * plugins updating the function. This overrides the
4792 * system defaults.
4793 */
4794 pr_stat("override of function helper '%s'", name);
4795 remove_func_handler(pevent, name);
4796 }
4797
4798 func_handle = malloc_or_die(sizeof(*func_handle));
4799 memset(func_handle, 0, sizeof(*func_handle));
4800
4801 func_handle->ret_type = ret_type;
4802 func_handle->name = strdup(name);
4803 func_handle->func = func;
4804 if (!func_handle->name)
4805 die("Failed to allocate function name");
4806
4807 next_param = &(func_handle->params);
4808 va_start(ap, name);
4809 for (;;) {
4810 type = va_arg(ap, enum pevent_func_arg_type);
4811 if (type == PEVENT_FUNC_ARG_VOID)
4812 break;
4813
4814 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
4815 warning("Invalid argument type %d", type);
4816 goto out_free;
4817 }
4818
4819 param = malloc_or_die(sizeof(*param));
4820 param->type = type;
4821 param->next = NULL;
4822
4823 *next_param = param;
4824 next_param = &(param->next);
4825
4826 func_handle->nr_args++;
4827 }
4828 va_end(ap);
4829
4830 func_handle->next = pevent->func_handlers;
4831 pevent->func_handlers = func_handle;
4832
4833 return 0;
4834 out_free:
4835 va_end(ap);
4836 free_func_handle(func_handle);
4837 return -1;
4838}
4839
4840/**
4841 * pevent_register_event_handle - register a way to parse an event
4842 * @pevent: the handle to the pevent
4843 * @id: the id of the event to register
4844 * @sys_name: the system name the event belongs to
4845 * @event_name: the name of the event
4846 * @func: the function to call to parse the event information
4847 *
4848 * This function allows a developer to override the parsing of
4849 * a given event. If for some reason the default print format
4850 * is not sufficient, this function will register a function
4851 * for an event to be used to parse the data instead.
4852 *
4853 * If @id is >= 0, then it is used to find the event.
4854 * else @sys_name and @event_name are used.
4855 */
4856int pevent_register_event_handler(struct pevent *pevent,
4857 int id, char *sys_name, char *event_name,
4858 pevent_event_handler_func func,
4859 void *context)
4860{
4861 struct event_format *event;
4862 struct event_handler *handle;
4863
4864 if (id >= 0) {
4865 /* search by id */
4866 event = pevent_find_event(pevent, id);
4867 if (!event)
4868 goto not_found;
4869 if (event_name && (strcmp(event_name, event->name) != 0))
4870 goto not_found;
4871 if (sys_name && (strcmp(sys_name, event->system) != 0))
4872 goto not_found;
4873 } else {
4874 event = pevent_find_event_by_name(pevent, sys_name, event_name);
4875 if (!event)
4876 goto not_found;
4877 }
4878
4879 pr_stat("overriding event (%d) %s:%s with new print handler",
4880 event->id, event->system, event->name);
4881
4882 event->handler = func;
4883 event->context = context;
4884 return 0;
4885
4886 not_found:
4887 /* Save for later use. */
4888 handle = malloc_or_die(sizeof(*handle));
4889 memset(handle, 0, sizeof(handle));
4890 handle->id = id;
4891 if (event_name)
4892 handle->event_name = strdup(event_name);
4893 if (sys_name)
4894 handle->sys_name = strdup(sys_name);
4895
4896 handle->func = func;
4897 handle->next = pevent->handlers;
4898 pevent->handlers = handle;
4899 handle->context = context;
4900
4901 return -1;
4902}
4903
4904/**
4905 * pevent_alloc - create a pevent handle
4906 */
4907struct pevent *pevent_alloc(void)
4908{
4909 struct pevent *pevent;
4910
4911 pevent = malloc(sizeof(*pevent));
4912 if (!pevent)
4913 return NULL;
4914 memset(pevent, 0, sizeof(*pevent));
4915 pevent->ref_count = 1;
4916
4917 return pevent;
4918}
4919
4920void pevent_ref(struct pevent *pevent)
4921{
4922 pevent->ref_count++;
4923}
4924
4925static void free_format_fields(struct format_field *field)
4926{
4927 struct format_field *next;
4928
4929 while (field) {
4930 next = field->next;
4931 free(field->type);
4932 free(field->name);
4933 free(field);
4934 field = next;
4935 }
4936}
4937
4938static void free_formats(struct format *format)
4939{
4940 free_format_fields(format->common_fields);
4941 free_format_fields(format->fields);
4942}
4943
4944static void free_event(struct event_format *event)
4945{
4946 free(event->name);
4947 free(event->system);
4948
4949 free_formats(&event->format);
4950
4951 free(event->print_fmt.format);
4952 free_args(event->print_fmt.args);
4953
4954 free(event);
4955}
4956
4957/**
4958 * pevent_free - free a pevent handle
4959 * @pevent: the pevent handle to free
4960 */
4961void pevent_free(struct pevent *pevent)
4962{
Steven Rostedta2525a02012-04-06 00:48:02 +02004963 struct cmdline_list *cmdlist, *cmdnext;
4964 struct func_list *funclist, *funcnext;
4965 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004966 struct pevent_function_handler *func_handler;
4967 struct event_handler *handle;
4968 int i;
4969
Steven Rostedta2525a02012-04-06 00:48:02 +02004970 if (!pevent)
4971 return;
4972
4973 cmdlist = pevent->cmdlist;
4974 funclist = pevent->funclist;
4975 printklist = pevent->printklist;
4976
Steven Rostedtf7d82352012-04-06 00:47:53 +02004977 pevent->ref_count--;
4978 if (pevent->ref_count)
4979 return;
4980
4981 if (pevent->cmdlines) {
4982 for (i = 0; i < pevent->cmdline_count; i++)
4983 free(pevent->cmdlines[i].comm);
4984 free(pevent->cmdlines);
4985 }
4986
4987 while (cmdlist) {
4988 cmdnext = cmdlist->next;
4989 free(cmdlist->comm);
4990 free(cmdlist);
4991 cmdlist = cmdnext;
4992 }
4993
4994 if (pevent->func_map) {
4995 for (i = 0; i < pevent->func_count; i++) {
4996 free(pevent->func_map[i].func);
4997 free(pevent->func_map[i].mod);
4998 }
4999 free(pevent->func_map);
5000 }
5001
5002 while (funclist) {
5003 funcnext = funclist->next;
5004 free(funclist->func);
5005 free(funclist->mod);
5006 free(funclist);
5007 funclist = funcnext;
5008 }
5009
5010 while (pevent->func_handlers) {
5011 func_handler = pevent->func_handlers;
5012 pevent->func_handlers = func_handler->next;
5013 free_func_handle(func_handler);
5014 }
5015
5016 if (pevent->printk_map) {
5017 for (i = 0; i < pevent->printk_count; i++)
5018 free(pevent->printk_map[i].printk);
5019 free(pevent->printk_map);
5020 }
5021
5022 while (printklist) {
5023 printknext = printklist->next;
5024 free(printklist->printk);
5025 free(printklist);
5026 printklist = printknext;
5027 }
5028
5029 for (i = 0; i < pevent->nr_events; i++)
5030 free_event(pevent->events[i]);
5031
5032 while (pevent->handlers) {
5033 handle = pevent->handlers;
5034 pevent->handlers = handle->next;
5035 free_handler(handle);
5036 }
5037
5038 free(pevent->events);
5039 free(pevent->sort_events);
5040
5041 free(pevent);
5042}
5043
5044void pevent_unref(struct pevent *pevent)
5045{
5046 pevent_free(pevent);
5047}