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