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