blob: 0644c2a23ad606483a03c14b4506c81d68866c97 [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:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003373 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003374 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;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003474 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003475
3476 field = pevent->bprint_buf_field;
3477 ip_field = pevent->bprint_ip_field;
3478
3479 if (!field) {
3480 field = pevent_find_field(event, "buf");
3481 if (!field)
3482 die("can't find buffer field for binary printk");
3483 ip_field = pevent_find_field(event, "ip");
3484 if (!ip_field)
3485 die("can't find ip field for binary printk");
3486 pevent->bprint_buf_field = field;
3487 pevent->bprint_ip_field = ip_field;
3488 }
3489
3490 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3491
3492 /*
3493 * The first arg is the IP pointer.
3494 */
3495 args = alloc_arg();
3496 arg = args;
3497 arg->next = NULL;
3498 next = &arg->next;
3499
3500 arg->type = PRINT_ATOM;
3501 arg->atom.atom = malloc_or_die(32);
3502 sprintf(arg->atom.atom, "%lld", ip);
3503
3504 /* skip the first "%pf : " */
3505 for (ptr = fmt + 6, bptr = data + field->offset;
3506 bptr < data + size && *ptr; ptr++) {
3507 int ls = 0;
3508
3509 if (*ptr == '%') {
3510 process_again:
3511 ptr++;
3512 switch (*ptr) {
3513 case '%':
3514 break;
3515 case 'l':
3516 ls++;
3517 goto process_again;
3518 case 'L':
3519 ls = 2;
3520 goto process_again;
3521 case '0' ... '9':
3522 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003523 case '.':
3524 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003525 case 'p':
3526 ls = 1;
3527 /* fall through */
3528 case 'd':
3529 case 'u':
3530 case 'x':
3531 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003532 switch (ls) {
3533 case 0:
3534 vsize = 4;
3535 break;
3536 case 1:
3537 vsize = pevent->long_size;
3538 break;
3539 case 2:
3540 vsize = 8;
3541 default:
3542 vsize = ls; /* ? */
3543 break;
3544 }
3545 /* fall through */
3546 case '*':
3547 if (*ptr == '*')
3548 vsize = 4;
3549
Steven Rostedtf7d82352012-04-06 00:47:53 +02003550 /* the pointers are always 4 bytes aligned */
3551 bptr = (void *)(((unsigned long)bptr + 3) &
3552 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003553 val = pevent_read_number(pevent, bptr, vsize);
3554 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003555 arg = alloc_arg();
3556 arg->next = NULL;
3557 arg->type = PRINT_ATOM;
3558 arg->atom.atom = malloc_or_die(32);
3559 sprintf(arg->atom.atom, "%lld", val);
3560 *next = arg;
3561 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003562 /*
3563 * The '*' case means that an arg is used as the length.
3564 * We need to continue to figure out for what.
3565 */
3566 if (*ptr == '*')
3567 goto process_again;
3568
Steven Rostedtf7d82352012-04-06 00:47:53 +02003569 break;
3570 case 's':
3571 arg = alloc_arg();
3572 arg->next = NULL;
3573 arg->type = PRINT_BSTRING;
3574 arg->string.string = strdup(bptr);
3575 bptr += strlen(bptr) + 1;
3576 *next = arg;
3577 next = &arg->next;
3578 default:
3579 break;
3580 }
3581 }
3582 }
3583
3584 return args;
3585}
3586
3587static void free_args(struct print_arg *args)
3588{
3589 struct print_arg *next;
3590
3591 while (args) {
3592 next = args->next;
3593
3594 free_arg(args);
3595 args = next;
3596 }
3597}
3598
3599static char *
3600get_bprint_format(void *data, int size __unused, struct event_format *event)
3601{
3602 struct pevent *pevent = event->pevent;
3603 unsigned long long addr;
3604 struct format_field *field;
3605 struct printk_map *printk;
3606 char *format;
3607 char *p;
3608
3609 field = pevent->bprint_fmt_field;
3610
3611 if (!field) {
3612 field = pevent_find_field(event, "fmt");
3613 if (!field)
3614 die("can't find format field for binary printk");
3615 pevent->bprint_fmt_field = field;
3616 }
3617
3618 addr = pevent_read_number(pevent, data + field->offset, field->size);
3619
3620 printk = find_printk(pevent, addr);
3621 if (!printk) {
3622 format = malloc_or_die(45);
3623 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3624 addr);
3625 return format;
3626 }
3627
3628 p = printk->printk;
3629 /* Remove any quotes. */
3630 if (*p == '"')
3631 p++;
3632 format = malloc_or_die(strlen(p) + 10);
3633 sprintf(format, "%s : %s", "%pf", p);
3634 /* remove ending quotes and new line since we will add one too */
3635 p = format + strlen(format) - 1;
3636 if (*p == '"')
3637 *p = 0;
3638
3639 p -= 2;
3640 if (strcmp(p, "\\n") == 0)
3641 *p = 0;
3642
3643 return format;
3644}
3645
3646static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3647 struct event_format *event, struct print_arg *arg)
3648{
3649 unsigned char *buf;
3650 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3651
3652 if (arg->type == PRINT_FUNC) {
3653 process_defined_func(s, data, size, event, arg);
3654 return;
3655 }
3656
3657 if (arg->type != PRINT_FIELD) {
3658 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3659 arg->type);
3660 return;
3661 }
3662
3663 if (mac == 'm')
3664 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3665 if (!arg->field.field) {
3666 arg->field.field =
3667 pevent_find_any_field(event, arg->field.name);
3668 if (!arg->field.field)
3669 die("field %s not found", arg->field.name);
3670 }
3671 if (arg->field.field->size != 6) {
3672 trace_seq_printf(s, "INVALIDMAC");
3673 return;
3674 }
3675 buf = data + arg->field.field->offset;
3676 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3677}
3678
Namhyung Kim600da3c2012-06-22 17:10:15 +09003679static int is_printable_array(char *p, unsigned int len)
3680{
3681 unsigned int i;
3682
3683 for (i = 0; i < len && p[i]; i++)
3684 if (!isprint(p[i]))
3685 return 0;
3686 return 1;
3687}
3688
Steven Rostedtf7d82352012-04-06 00:47:53 +02003689static void print_event_fields(struct trace_seq *s, void *data, int size,
3690 struct event_format *event)
3691{
3692 struct format_field *field;
3693 unsigned long long val;
3694 unsigned int offset, len, i;
3695
3696 field = event->format.fields;
3697 while (field) {
3698 trace_seq_printf(s, " %s=", field->name);
3699 if (field->flags & FIELD_IS_ARRAY) {
3700 offset = field->offset;
3701 len = field->size;
3702 if (field->flags & FIELD_IS_DYNAMIC) {
3703 val = pevent_read_number(event->pevent, data + offset, len);
3704 offset = val;
3705 len = offset >> 16;
3706 offset &= 0xffff;
3707 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09003708 if (field->flags & FIELD_IS_STRING &&
3709 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003710 trace_seq_printf(s, "%s", (char *)data + offset);
3711 } else {
3712 trace_seq_puts(s, "ARRAY[");
3713 for (i = 0; i < len; i++) {
3714 if (i)
3715 trace_seq_puts(s, ", ");
3716 trace_seq_printf(s, "%02x",
3717 *((unsigned char *)data + offset + i));
3718 }
3719 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09003720 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003721 }
3722 } else {
3723 val = pevent_read_number(event->pevent, data + field->offset,
3724 field->size);
3725 if (field->flags & FIELD_IS_POINTER) {
3726 trace_seq_printf(s, "0x%llx", val);
3727 } else if (field->flags & FIELD_IS_SIGNED) {
3728 switch (field->size) {
3729 case 4:
3730 /*
3731 * If field is long then print it in hex.
3732 * A long usually stores pointers.
3733 */
3734 if (field->flags & FIELD_IS_LONG)
3735 trace_seq_printf(s, "0x%x", (int)val);
3736 else
3737 trace_seq_printf(s, "%d", (int)val);
3738 break;
3739 case 2:
3740 trace_seq_printf(s, "%2d", (short)val);
3741 break;
3742 case 1:
3743 trace_seq_printf(s, "%1d", (char)val);
3744 break;
3745 default:
3746 trace_seq_printf(s, "%lld", val);
3747 }
3748 } else {
3749 if (field->flags & FIELD_IS_LONG)
3750 trace_seq_printf(s, "0x%llx", val);
3751 else
3752 trace_seq_printf(s, "%llu", val);
3753 }
3754 }
3755 field = field->next;
3756 }
3757}
3758
3759static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3760{
3761 struct pevent *pevent = event->pevent;
3762 struct print_fmt *print_fmt = &event->print_fmt;
3763 struct print_arg *arg = print_fmt->args;
3764 struct print_arg *args = NULL;
3765 const char *ptr = print_fmt->format;
3766 unsigned long long val;
3767 struct func_map *func;
3768 const char *saveptr;
3769 char *bprint_fmt = NULL;
3770 char format[32];
3771 int show_func;
3772 int len_as_arg;
3773 int len_arg;
3774 int len;
3775 int ls;
3776
3777 if (event->flags & EVENT_FL_FAILED) {
3778 trace_seq_printf(s, "[FAILED TO PARSE]");
3779 print_event_fields(s, data, size, event);
3780 return;
3781 }
3782
3783 if (event->flags & EVENT_FL_ISBPRINT) {
3784 bprint_fmt = get_bprint_format(data, size, event);
3785 args = make_bprint_args(bprint_fmt, data, size, event);
3786 arg = args;
3787 ptr = bprint_fmt;
3788 }
3789
3790 for (; *ptr; ptr++) {
3791 ls = 0;
3792 if (*ptr == '\\') {
3793 ptr++;
3794 switch (*ptr) {
3795 case 'n':
3796 trace_seq_putc(s, '\n');
3797 break;
3798 case 't':
3799 trace_seq_putc(s, '\t');
3800 break;
3801 case 'r':
3802 trace_seq_putc(s, '\r');
3803 break;
3804 case '\\':
3805 trace_seq_putc(s, '\\');
3806 break;
3807 default:
3808 trace_seq_putc(s, *ptr);
3809 break;
3810 }
3811
3812 } else if (*ptr == '%') {
3813 saveptr = ptr;
3814 show_func = 0;
3815 len_as_arg = 0;
3816 cont_process:
3817 ptr++;
3818 switch (*ptr) {
3819 case '%':
3820 trace_seq_putc(s, '%');
3821 break;
3822 case '#':
3823 /* FIXME: need to handle properly */
3824 goto cont_process;
3825 case 'h':
3826 ls--;
3827 goto cont_process;
3828 case 'l':
3829 ls++;
3830 goto cont_process;
3831 case 'L':
3832 ls = 2;
3833 goto cont_process;
3834 case '*':
3835 /* The argument is the length. */
3836 if (!arg)
3837 die("no argument match");
3838 len_arg = eval_num_arg(data, size, event, arg);
3839 len_as_arg = 1;
3840 arg = arg->next;
3841 goto cont_process;
3842 case '.':
3843 case 'z':
3844 case 'Z':
3845 case '0' ... '9':
3846 goto cont_process;
3847 case 'p':
3848 if (pevent->long_size == 4)
3849 ls = 1;
3850 else
3851 ls = 2;
3852
3853 if (*(ptr+1) == 'F' ||
3854 *(ptr+1) == 'f') {
3855 ptr++;
3856 show_func = *ptr;
3857 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3858 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3859 ptr++;
3860 break;
3861 }
3862
3863 /* fall through */
3864 case 'd':
3865 case 'i':
3866 case 'x':
3867 case 'X':
3868 case 'u':
3869 if (!arg)
3870 die("no argument match");
3871
3872 len = ((unsigned long)ptr + 1) -
3873 (unsigned long)saveptr;
3874
3875 /* should never happen */
3876 if (len > 31)
3877 die("bad format!");
3878
3879 memcpy(format, saveptr, len);
3880 format[len] = 0;
3881
3882 val = eval_num_arg(data, size, event, arg);
3883 arg = arg->next;
3884
3885 if (show_func) {
3886 func = find_func(pevent, val);
3887 if (func) {
3888 trace_seq_puts(s, func->func);
3889 if (show_func == 'F')
3890 trace_seq_printf(s,
3891 "+0x%llx",
3892 val - func->addr);
3893 break;
3894 }
3895 }
3896 if (pevent->long_size == 8 && ls) {
3897 char *p;
3898
3899 ls = 2;
3900 /* make %l into %ll */
3901 p = strchr(format, 'l');
3902 if (p)
3903 memmove(p, p+1, strlen(p)+1);
3904 else if (strcmp(format, "%p") == 0)
3905 strcpy(format, "0x%llx");
3906 }
3907 switch (ls) {
3908 case -2:
3909 if (len_as_arg)
3910 trace_seq_printf(s, format, len_arg, (char)val);
3911 else
3912 trace_seq_printf(s, format, (char)val);
3913 break;
3914 case -1:
3915 if (len_as_arg)
3916 trace_seq_printf(s, format, len_arg, (short)val);
3917 else
3918 trace_seq_printf(s, format, (short)val);
3919 break;
3920 case 0:
3921 if (len_as_arg)
3922 trace_seq_printf(s, format, len_arg, (int)val);
3923 else
3924 trace_seq_printf(s, format, (int)val);
3925 break;
3926 case 1:
3927 if (len_as_arg)
3928 trace_seq_printf(s, format, len_arg, (long)val);
3929 else
3930 trace_seq_printf(s, format, (long)val);
3931 break;
3932 case 2:
3933 if (len_as_arg)
3934 trace_seq_printf(s, format, len_arg,
3935 (long long)val);
3936 else
3937 trace_seq_printf(s, format, (long long)val);
3938 break;
3939 default:
3940 die("bad count (%d)", ls);
3941 }
3942 break;
3943 case 's':
3944 if (!arg)
3945 die("no matching argument");
3946
3947 len = ((unsigned long)ptr + 1) -
3948 (unsigned long)saveptr;
3949
3950 /* should never happen */
3951 if (len > 31)
3952 die("bad format!");
3953
3954 memcpy(format, saveptr, len);
3955 format[len] = 0;
3956 if (!len_as_arg)
3957 len_arg = -1;
3958 print_str_arg(s, data, size, event,
3959 format, len_arg, arg);
3960 arg = arg->next;
3961 break;
3962 default:
3963 trace_seq_printf(s, ">%c<", *ptr);
3964
3965 }
3966 } else
3967 trace_seq_putc(s, *ptr);
3968 }
3969
3970 if (args) {
3971 free_args(args);
3972 free(bprint_fmt);
3973 }
3974}
3975
3976/**
3977 * pevent_data_lat_fmt - parse the data for the latency format
3978 * @pevent: a handle to the pevent
3979 * @s: the trace_seq to write to
3980 * @data: the raw data to read from
3981 * @size: currently unused.
3982 *
3983 * This parses out the Latency format (interrupts disabled,
3984 * need rescheduling, in hard/soft interrupt, preempt count
3985 * and lock depth) and places it into the trace_seq.
3986 */
3987void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02003988 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003989{
3990 static int check_lock_depth = 1;
3991 static int lock_depth_exists;
3992 unsigned int lat_flags;
3993 unsigned int pc;
3994 int lock_depth;
3995 int hardirq;
3996 int softirq;
3997 void *data = record->data;
3998
3999 lat_flags = parse_common_flags(pevent, data);
4000 pc = parse_common_pc(pevent, data);
4001 /* lock_depth may not always exist */
4002 if (check_lock_depth) {
4003 struct format_field *field;
4004 struct event_format *event;
4005
4006 check_lock_depth = 0;
4007 event = pevent->events[0];
4008 field = pevent_find_common_field(event, "common_lock_depth");
4009 if (field)
4010 lock_depth_exists = 1;
4011 }
4012 if (lock_depth_exists)
4013 lock_depth = parse_common_lock_depth(pevent, data);
4014
4015 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4016 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4017
4018 trace_seq_printf(s, "%c%c%c",
4019 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4020 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4021 'X' : '.',
4022 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4023 'N' : '.',
4024 (hardirq && softirq) ? 'H' :
4025 hardirq ? 'h' : softirq ? 's' : '.');
4026
4027 if (pc)
4028 trace_seq_printf(s, "%x", pc);
4029 else
4030 trace_seq_putc(s, '.');
4031
4032 if (lock_depth_exists) {
4033 if (lock_depth < 0)
4034 trace_seq_putc(s, '.');
4035 else
4036 trace_seq_printf(s, "%d", lock_depth);
4037 }
4038
4039 trace_seq_terminate(s);
4040}
4041
4042/**
4043 * pevent_data_type - parse out the given event type
4044 * @pevent: a handle to the pevent
4045 * @rec: the record to read from
4046 *
4047 * This returns the event id from the @rec.
4048 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004049int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004050{
4051 return trace_parse_common_type(pevent, rec->data);
4052}
4053
4054/**
4055 * pevent_data_event_from_type - find the event by a given type
4056 * @pevent: a handle to the pevent
4057 * @type: the type of the event.
4058 *
4059 * This returns the event form a given @type;
4060 */
4061struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4062{
4063 return pevent_find_event(pevent, type);
4064}
4065
4066/**
4067 * pevent_data_pid - parse the PID from raw data
4068 * @pevent: a handle to the pevent
4069 * @rec: the record to parse
4070 *
4071 * This returns the PID from a raw data.
4072 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004073int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004074{
4075 return parse_common_pid(pevent, rec->data);
4076}
4077
4078/**
4079 * pevent_data_comm_from_pid - return the command line from PID
4080 * @pevent: a handle to the pevent
4081 * @pid: the PID of the task to search for
4082 *
4083 * This returns a pointer to the command line that has the given
4084 * @pid.
4085 */
4086const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4087{
4088 const char *comm;
4089
4090 comm = find_cmdline(pevent, pid);
4091 return comm;
4092}
4093
4094/**
4095 * pevent_data_comm_from_pid - parse the data into the print format
4096 * @s: the trace_seq to write to
4097 * @event: the handle to the event
4098 * @cpu: the cpu the event was recorded on
4099 * @data: the raw data
4100 * @size: the size of the raw data
4101 * @nsecs: the timestamp of the event
4102 *
4103 * This parses the raw @data using the given @event information and
4104 * writes the print format into the trace_seq.
4105 */
4106void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004107 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004108{
4109 int print_pretty = 1;
4110
4111 if (event->pevent->print_raw)
4112 print_event_fields(s, record->data, record->size, event);
4113 else {
4114
4115 if (event->handler)
4116 print_pretty = event->handler(s, record, event,
4117 event->context);
4118
4119 if (print_pretty)
4120 pretty_print(s, record->data, record->size, event);
4121 }
4122
4123 trace_seq_terminate(s);
4124}
4125
4126void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Steven Rostedt1c698182012-04-06 00:48:06 +02004127 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004128{
4129 static char *spaces = " "; /* 20 spaces */
4130 struct event_format *event;
4131 unsigned long secs;
4132 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004133 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004134 const char *comm;
4135 void *data = record->data;
4136 int type;
4137 int pid;
4138 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004139 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004140
4141 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004142 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004143
4144 if (record->size < 0) {
4145 do_warning("ug! negative record size %d", record->size);
4146 return;
4147 }
4148
4149 type = trace_parse_common_type(pevent, data);
4150
4151 event = pevent_find_event(pevent, type);
4152 if (!event) {
4153 do_warning("ug! no event found for type %d", type);
4154 return;
4155 }
4156
4157 pid = parse_common_pid(pevent, data);
4158 comm = find_cmdline(pevent, pid);
4159
4160 if (pevent->latency_format) {
4161 trace_seq_printf(s, "%8.8s-%-5d %3d",
4162 comm, pid, record->cpu);
4163 pevent_data_lat_fmt(pevent, s, record);
4164 } else
4165 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4166
Steven Rostedt4dc10242012-04-06 00:47:57 +02004167 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4168 usecs = nsecs;
4169 p = 9;
4170 } else {
4171 usecs = (nsecs + 500) / NSECS_PER_USEC;
4172 p = 6;
4173 }
4174
4175 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004176
4177 /* Space out the event names evenly. */
4178 len = strlen(event->name);
4179 if (len < 20)
4180 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4181
4182 pevent_event_info(s, event, record);
4183}
4184
4185static int events_id_cmp(const void *a, const void *b)
4186{
4187 struct event_format * const * ea = a;
4188 struct event_format * const * eb = b;
4189
4190 if ((*ea)->id < (*eb)->id)
4191 return -1;
4192
4193 if ((*ea)->id > (*eb)->id)
4194 return 1;
4195
4196 return 0;
4197}
4198
4199static int events_name_cmp(const void *a, const void *b)
4200{
4201 struct event_format * const * ea = a;
4202 struct event_format * const * eb = b;
4203 int res;
4204
4205 res = strcmp((*ea)->name, (*eb)->name);
4206 if (res)
4207 return res;
4208
4209 res = strcmp((*ea)->system, (*eb)->system);
4210 if (res)
4211 return res;
4212
4213 return events_id_cmp(a, b);
4214}
4215
4216static int events_system_cmp(const void *a, const void *b)
4217{
4218 struct event_format * const * ea = a;
4219 struct event_format * const * eb = b;
4220 int res;
4221
4222 res = strcmp((*ea)->system, (*eb)->system);
4223 if (res)
4224 return res;
4225
4226 res = strcmp((*ea)->name, (*eb)->name);
4227 if (res)
4228 return res;
4229
4230 return events_id_cmp(a, b);
4231}
4232
4233struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4234{
4235 struct event_format **events;
4236 int (*sort)(const void *a, const void *b);
4237
4238 events = pevent->sort_events;
4239
4240 if (events && pevent->last_type == sort_type)
4241 return events;
4242
4243 if (!events) {
4244 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4245 if (!events)
4246 return NULL;
4247
4248 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4249 events[pevent->nr_events] = NULL;
4250
4251 pevent->sort_events = events;
4252
4253 /* the internal events are sorted by id */
4254 if (sort_type == EVENT_SORT_ID) {
4255 pevent->last_type = sort_type;
4256 return events;
4257 }
4258 }
4259
4260 switch (sort_type) {
4261 case EVENT_SORT_ID:
4262 sort = events_id_cmp;
4263 break;
4264 case EVENT_SORT_NAME:
4265 sort = events_name_cmp;
4266 break;
4267 case EVENT_SORT_SYSTEM:
4268 sort = events_system_cmp;
4269 break;
4270 default:
4271 return events;
4272 }
4273
4274 qsort(events, pevent->nr_events, sizeof(*events), sort);
4275 pevent->last_type = sort_type;
4276
4277 return events;
4278}
4279
4280static struct format_field **
4281get_event_fields(const char *type, const char *name,
4282 int count, struct format_field *list)
4283{
4284 struct format_field **fields;
4285 struct format_field *field;
4286 int i = 0;
4287
4288 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4289 for (field = list; field; field = field->next) {
4290 fields[i++] = field;
4291 if (i == count + 1) {
4292 do_warning("event %s has more %s fields than specified",
4293 name, type);
4294 i--;
4295 break;
4296 }
4297 }
4298
4299 if (i != count)
4300 do_warning("event %s has less %s fields than specified",
4301 name, type);
4302
4303 fields[i] = NULL;
4304
4305 return fields;
4306}
4307
4308/**
4309 * pevent_event_common_fields - return a list of common fields for an event
4310 * @event: the event to return the common fields of.
4311 *
4312 * Returns an allocated array of fields. The last item in the array is NULL.
4313 * The array must be freed with free().
4314 */
4315struct format_field **pevent_event_common_fields(struct event_format *event)
4316{
4317 return get_event_fields("common", event->name,
4318 event->format.nr_common,
4319 event->format.common_fields);
4320}
4321
4322/**
4323 * pevent_event_fields - return a list of event specific fields for an event
4324 * @event: the event to return the fields of.
4325 *
4326 * Returns an allocated array of fields. The last item in the array is NULL.
4327 * The array must be freed with free().
4328 */
4329struct format_field **pevent_event_fields(struct event_format *event)
4330{
4331 return get_event_fields("event", event->name,
4332 event->format.nr_fields,
4333 event->format.fields);
4334}
4335
4336static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4337{
4338 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4339 if (field->next) {
4340 trace_seq_puts(s, ", ");
4341 print_fields(s, field->next);
4342 }
4343}
4344
4345/* for debugging */
4346static void print_args(struct print_arg *args)
4347{
4348 int print_paren = 1;
4349 struct trace_seq s;
4350
4351 switch (args->type) {
4352 case PRINT_NULL:
4353 printf("null");
4354 break;
4355 case PRINT_ATOM:
4356 printf("%s", args->atom.atom);
4357 break;
4358 case PRINT_FIELD:
4359 printf("REC->%s", args->field.name);
4360 break;
4361 case PRINT_FLAGS:
4362 printf("__print_flags(");
4363 print_args(args->flags.field);
4364 printf(", %s, ", args->flags.delim);
4365 trace_seq_init(&s);
4366 print_fields(&s, args->flags.flags);
4367 trace_seq_do_printf(&s);
4368 trace_seq_destroy(&s);
4369 printf(")");
4370 break;
4371 case PRINT_SYMBOL:
4372 printf("__print_symbolic(");
4373 print_args(args->symbol.field);
4374 printf(", ");
4375 trace_seq_init(&s);
4376 print_fields(&s, args->symbol.symbols);
4377 trace_seq_do_printf(&s);
4378 trace_seq_destroy(&s);
4379 printf(")");
4380 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004381 case PRINT_HEX:
4382 printf("__print_hex(");
4383 print_args(args->hex.field);
4384 printf(", ");
4385 print_args(args->hex.size);
4386 printf(")");
4387 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004388 case PRINT_STRING:
4389 case PRINT_BSTRING:
4390 printf("__get_str(%s)", args->string.string);
4391 break;
4392 case PRINT_TYPE:
4393 printf("(%s)", args->typecast.type);
4394 print_args(args->typecast.item);
4395 break;
4396 case PRINT_OP:
4397 if (strcmp(args->op.op, ":") == 0)
4398 print_paren = 0;
4399 if (print_paren)
4400 printf("(");
4401 print_args(args->op.left);
4402 printf(" %s ", args->op.op);
4403 print_args(args->op.right);
4404 if (print_paren)
4405 printf(")");
4406 break;
4407 default:
4408 /* we should warn... */
4409 return;
4410 }
4411 if (args->next) {
4412 printf("\n");
4413 print_args(args->next);
4414 }
4415}
4416
4417static void parse_header_field(const char *field,
4418 int *offset, int *size, int mandatory)
4419{
4420 unsigned long long save_input_buf_ptr;
4421 unsigned long long save_input_buf_siz;
4422 char *token;
4423 int type;
4424
4425 save_input_buf_ptr = input_buf_ptr;
4426 save_input_buf_siz = input_buf_siz;
4427
4428 if (read_expected(EVENT_ITEM, "field") < 0)
4429 return;
4430 if (read_expected(EVENT_OP, ":") < 0)
4431 return;
4432
4433 /* type */
4434 if (read_expect_type(EVENT_ITEM, &token) < 0)
4435 goto fail;
4436 free_token(token);
4437
4438 /*
4439 * If this is not a mandatory field, then test it first.
4440 */
4441 if (mandatory) {
4442 if (read_expected(EVENT_ITEM, field) < 0)
4443 return;
4444 } else {
4445 if (read_expect_type(EVENT_ITEM, &token) < 0)
4446 goto fail;
4447 if (strcmp(token, field) != 0)
4448 goto discard;
4449 free_token(token);
4450 }
4451
4452 if (read_expected(EVENT_OP, ";") < 0)
4453 return;
4454 if (read_expected(EVENT_ITEM, "offset") < 0)
4455 return;
4456 if (read_expected(EVENT_OP, ":") < 0)
4457 return;
4458 if (read_expect_type(EVENT_ITEM, &token) < 0)
4459 goto fail;
4460 *offset = atoi(token);
4461 free_token(token);
4462 if (read_expected(EVENT_OP, ";") < 0)
4463 return;
4464 if (read_expected(EVENT_ITEM, "size") < 0)
4465 return;
4466 if (read_expected(EVENT_OP, ":") < 0)
4467 return;
4468 if (read_expect_type(EVENT_ITEM, &token) < 0)
4469 goto fail;
4470 *size = atoi(token);
4471 free_token(token);
4472 if (read_expected(EVENT_OP, ";") < 0)
4473 return;
4474 type = read_token(&token);
4475 if (type != EVENT_NEWLINE) {
4476 /* newer versions of the kernel have a "signed" type */
4477 if (type != EVENT_ITEM)
4478 goto fail;
4479
4480 if (strcmp(token, "signed") != 0)
4481 goto fail;
4482
4483 free_token(token);
4484
4485 if (read_expected(EVENT_OP, ":") < 0)
4486 return;
4487
4488 if (read_expect_type(EVENT_ITEM, &token))
4489 goto fail;
4490
4491 free_token(token);
4492 if (read_expected(EVENT_OP, ";") < 0)
4493 return;
4494
4495 if (read_expect_type(EVENT_NEWLINE, &token))
4496 goto fail;
4497 }
4498 fail:
4499 free_token(token);
4500 return;
4501
4502 discard:
4503 input_buf_ptr = save_input_buf_ptr;
4504 input_buf_siz = save_input_buf_siz;
4505 *offset = 0;
4506 *size = 0;
4507 free_token(token);
4508}
4509
4510/**
4511 * pevent_parse_header_page - parse the data stored in the header page
4512 * @pevent: the handle to the pevent
4513 * @buf: the buffer storing the header page format string
4514 * @size: the size of @buf
4515 * @long_size: the long size to use if there is no header
4516 *
4517 * This parses the header page format for information on the
4518 * ring buffer used. The @buf should be copied from
4519 *
4520 * /sys/kernel/debug/tracing/events/header_page
4521 */
4522int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4523 int long_size)
4524{
4525 int ignore;
4526
4527 if (!size) {
4528 /*
4529 * Old kernels did not have header page info.
4530 * Sorry but we just use what we find here in user space.
4531 */
4532 pevent->header_page_ts_size = sizeof(long long);
4533 pevent->header_page_size_size = long_size;
4534 pevent->header_page_data_offset = sizeof(long long) + long_size;
4535 pevent->old_format = 1;
4536 return -1;
4537 }
4538 init_input_buf(buf, size);
4539
4540 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4541 &pevent->header_page_ts_size, 1);
4542 parse_header_field("commit", &pevent->header_page_size_offset,
4543 &pevent->header_page_size_size, 1);
4544 parse_header_field("overwrite", &pevent->header_page_overwrite,
4545 &ignore, 0);
4546 parse_header_field("data", &pevent->header_page_data_offset,
4547 &pevent->header_page_data_size, 1);
4548
4549 return 0;
4550}
4551
4552static int event_matches(struct event_format *event,
4553 int id, const char *sys_name,
4554 const char *event_name)
4555{
4556 if (id >= 0 && id != event->id)
4557 return 0;
4558
4559 if (event_name && (strcmp(event_name, event->name) != 0))
4560 return 0;
4561
4562 if (sys_name && (strcmp(sys_name, event->system) != 0))
4563 return 0;
4564
4565 return 1;
4566}
4567
4568static void free_handler(struct event_handler *handle)
4569{
4570 free((void *)handle->sys_name);
4571 free((void *)handle->event_name);
4572 free(handle);
4573}
4574
4575static int find_event_handle(struct pevent *pevent, struct event_format *event)
4576{
4577 struct event_handler *handle, **next;
4578
4579 for (next = &pevent->handlers; *next;
4580 next = &(*next)->next) {
4581 handle = *next;
4582 if (event_matches(event, handle->id,
4583 handle->sys_name,
4584 handle->event_name))
4585 break;
4586 }
4587
4588 if (!(*next))
4589 return 0;
4590
4591 pr_stat("overriding event (%d) %s:%s with new print handler",
4592 event->id, event->system, event->name);
4593
4594 event->handler = handle->func;
4595 event->context = handle->context;
4596
4597 *next = handle->next;
4598 free_handler(handle);
4599
4600 return 1;
4601}
4602
4603/**
4604 * pevent_parse_event - parse the event format
4605 * @pevent: the handle to the pevent
4606 * @buf: the buffer storing the event format string
4607 * @size: the size of @buf
4608 * @sys: the system the event belongs to
4609 *
4610 * This parses the event format and creates an event structure
4611 * to quickly parse raw data for a given event.
4612 *
4613 * These files currently come from:
4614 *
4615 * /sys/kernel/debug/tracing/events/.../.../format
4616 */
4617int pevent_parse_event(struct pevent *pevent,
4618 const char *buf, unsigned long size,
4619 const char *sys)
4620{
4621 struct event_format *event;
4622 int ret;
4623
4624 init_input_buf(buf, size);
4625
4626 event = alloc_event();
4627 if (!event)
4628 return -ENOMEM;
4629
4630 event->name = event_read_name();
4631 if (!event->name) {
4632 /* Bad event? */
4633 free(event);
4634 return -1;
4635 }
4636
4637 if (strcmp(sys, "ftrace") == 0) {
4638
4639 event->flags |= EVENT_FL_ISFTRACE;
4640
4641 if (strcmp(event->name, "bprint") == 0)
4642 event->flags |= EVENT_FL_ISBPRINT;
4643 }
4644
4645 event->id = event_read_id();
4646 if (event->id < 0)
4647 die("failed to read event id");
4648
4649 event->system = strdup(sys);
4650
4651 /* Add pevent to event so that it can be referenced */
4652 event->pevent = pevent;
4653
4654 ret = event_read_format(event);
4655 if (ret < 0) {
4656 do_warning("failed to read event format for %s", event->name);
4657 goto event_failed;
4658 }
4659
4660 /*
4661 * If the event has an override, don't print warnings if the event
4662 * print format fails to parse.
4663 */
4664 if (find_event_handle(pevent, event))
4665 show_warning = 0;
4666
4667 ret = event_read_print(event);
4668 if (ret < 0) {
4669 do_warning("failed to read event print fmt for %s",
4670 event->name);
4671 show_warning = 1;
4672 goto event_failed;
4673 }
4674 show_warning = 1;
4675
4676 add_event(pevent, event);
4677
4678 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4679 struct format_field *field;
4680 struct print_arg *arg, **list;
4681
4682 /* old ftrace had no args */
4683
4684 list = &event->print_fmt.args;
4685 for (field = event->format.fields; field; field = field->next) {
4686 arg = alloc_arg();
4687 *list = arg;
4688 list = &arg->next;
4689 arg->type = PRINT_FIELD;
4690 arg->field.name = strdup(field->name);
4691 arg->field.field = field;
4692 }
4693 return 0;
4694 }
4695
4696#define PRINT_ARGS 0
4697 if (PRINT_ARGS && event->print_fmt.args)
4698 print_args(event->print_fmt.args);
4699
4700 return 0;
4701
4702 event_failed:
4703 event->flags |= EVENT_FL_FAILED;
4704 /* still add it even if it failed */
4705 add_event(pevent, event);
4706 return -1;
4707}
4708
4709int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02004710 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004711 unsigned long long *val, int err)
4712{
4713 if (!field) {
4714 if (err)
4715 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4716 return -1;
4717 }
4718
4719 if (pevent_read_number_field(field, record->data, val)) {
4720 if (err)
4721 trace_seq_printf(s, " %s=INVALID", name);
4722 return -1;
4723 }
4724
4725 return 0;
4726}
4727
4728/**
4729 * pevent_get_field_raw - return the raw pointer into the data field
4730 * @s: The seq to print to on error
4731 * @event: the event that the field is for
4732 * @name: The name of the field
4733 * @record: The record with the field name.
4734 * @len: place to store the field length.
4735 * @err: print default error if failed.
4736 *
4737 * Returns a pointer into record->data of the field and places
4738 * the length of the field in @len.
4739 *
4740 * On failure, it returns NULL.
4741 */
4742void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004743 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004744 int *len, int err)
4745{
4746 struct format_field *field;
4747 void *data = record->data;
4748 unsigned offset;
4749 int dummy;
4750
4751 if (!event)
4752 return NULL;
4753
4754 field = pevent_find_field(event, name);
4755
4756 if (!field) {
4757 if (err)
4758 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4759 return NULL;
4760 }
4761
4762 /* Allow @len to be NULL */
4763 if (!len)
4764 len = &dummy;
4765
4766 offset = field->offset;
4767 if (field->flags & FIELD_IS_DYNAMIC) {
4768 offset = pevent_read_number(event->pevent,
4769 data + offset, field->size);
4770 *len = offset >> 16;
4771 offset &= 0xffff;
4772 } else
4773 *len = field->size;
4774
4775 return data + offset;
4776}
4777
4778/**
4779 * pevent_get_field_val - find a field and return its value
4780 * @s: The seq to print to on error
4781 * @event: the event that the field is for
4782 * @name: The name of the field
4783 * @record: The record with the field name.
4784 * @val: place to store the value of the field.
4785 * @err: print default error if failed.
4786 *
4787 * Returns 0 on success -1 on field not found.
4788 */
4789int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004790 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004791 unsigned long long *val, int err)
4792{
4793 struct format_field *field;
4794
4795 if (!event)
4796 return -1;
4797
4798 field = pevent_find_field(event, name);
4799
4800 return get_field_val(s, field, name, record, val, err);
4801}
4802
4803/**
4804 * pevent_get_common_field_val - find a common field and return its value
4805 * @s: The seq to print to on error
4806 * @event: the event that the field is for
4807 * @name: The name of the field
4808 * @record: The record with the field name.
4809 * @val: place to store the value of the field.
4810 * @err: print default error if failed.
4811 *
4812 * Returns 0 on success -1 on field not found.
4813 */
4814int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004815 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004816 unsigned long long *val, int err)
4817{
4818 struct format_field *field;
4819
4820 if (!event)
4821 return -1;
4822
4823 field = pevent_find_common_field(event, name);
4824
4825 return get_field_val(s, field, name, record, val, err);
4826}
4827
4828/**
4829 * pevent_get_any_field_val - find a any field and return its value
4830 * @s: The seq to print to on error
4831 * @event: the event that the field is for
4832 * @name: The name of the field
4833 * @record: The record with the field name.
4834 * @val: place to store the value of the field.
4835 * @err: print default error if failed.
4836 *
4837 * Returns 0 on success -1 on field not found.
4838 */
4839int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004840 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004841 unsigned long long *val, int err)
4842{
4843 struct format_field *field;
4844
4845 if (!event)
4846 return -1;
4847
4848 field = pevent_find_any_field(event, name);
4849
4850 return get_field_val(s, field, name, record, val, err);
4851}
4852
4853/**
4854 * pevent_print_num_field - print a field and a format
4855 * @s: The seq to print to
4856 * @fmt: The printf format to print the field with.
4857 * @event: the event that the field is for
4858 * @name: The name of the field
4859 * @record: The record with the field name.
4860 * @err: print default error if failed.
4861 *
4862 * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
4863 */
4864int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4865 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02004866 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004867{
4868 struct format_field *field = pevent_find_field(event, name);
4869 unsigned long long val;
4870
4871 if (!field)
4872 goto failed;
4873
4874 if (pevent_read_number_field(field, record->data, &val))
4875 goto failed;
4876
4877 return trace_seq_printf(s, fmt, val);
4878
4879 failed:
4880 if (err)
4881 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
4882 return -1;
4883}
4884
4885static void free_func_handle(struct pevent_function_handler *func)
4886{
4887 struct pevent_func_params *params;
4888
4889 free(func->name);
4890
4891 while (func->params) {
4892 params = func->params;
4893 func->params = params->next;
4894 free(params);
4895 }
4896
4897 free(func);
4898}
4899
4900/**
4901 * pevent_register_print_function - register a helper function
4902 * @pevent: the handle to the pevent
4903 * @func: the function to process the helper function
4904 * @name: the name of the helper function
4905 * @parameters: A list of enum pevent_func_arg_type
4906 *
4907 * Some events may have helper functions in the print format arguments.
4908 * This allows a plugin to dynmically create a way to process one
4909 * of these functions.
4910 *
4911 * The @parameters is a variable list of pevent_func_arg_type enums that
4912 * must end with PEVENT_FUNC_ARG_VOID.
4913 */
4914int pevent_register_print_function(struct pevent *pevent,
4915 pevent_func_handler func,
4916 enum pevent_func_arg_type ret_type,
4917 char *name, ...)
4918{
4919 struct pevent_function_handler *func_handle;
4920 struct pevent_func_params **next_param;
4921 struct pevent_func_params *param;
4922 enum pevent_func_arg_type type;
4923 va_list ap;
4924
4925 func_handle = find_func_handler(pevent, name);
4926 if (func_handle) {
4927 /*
4928 * This is most like caused by the users own
4929 * plugins updating the function. This overrides the
4930 * system defaults.
4931 */
4932 pr_stat("override of function helper '%s'", name);
4933 remove_func_handler(pevent, name);
4934 }
4935
4936 func_handle = malloc_or_die(sizeof(*func_handle));
4937 memset(func_handle, 0, sizeof(*func_handle));
4938
4939 func_handle->ret_type = ret_type;
4940 func_handle->name = strdup(name);
4941 func_handle->func = func;
4942 if (!func_handle->name)
4943 die("Failed to allocate function name");
4944
4945 next_param = &(func_handle->params);
4946 va_start(ap, name);
4947 for (;;) {
4948 type = va_arg(ap, enum pevent_func_arg_type);
4949 if (type == PEVENT_FUNC_ARG_VOID)
4950 break;
4951
4952 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
4953 warning("Invalid argument type %d", type);
4954 goto out_free;
4955 }
4956
4957 param = malloc_or_die(sizeof(*param));
4958 param->type = type;
4959 param->next = NULL;
4960
4961 *next_param = param;
4962 next_param = &(param->next);
4963
4964 func_handle->nr_args++;
4965 }
4966 va_end(ap);
4967
4968 func_handle->next = pevent->func_handlers;
4969 pevent->func_handlers = func_handle;
4970
4971 return 0;
4972 out_free:
4973 va_end(ap);
4974 free_func_handle(func_handle);
4975 return -1;
4976}
4977
4978/**
4979 * pevent_register_event_handle - register a way to parse an event
4980 * @pevent: the handle to the pevent
4981 * @id: the id of the event to register
4982 * @sys_name: the system name the event belongs to
4983 * @event_name: the name of the event
4984 * @func: the function to call to parse the event information
4985 *
4986 * This function allows a developer to override the parsing of
4987 * a given event. If for some reason the default print format
4988 * is not sufficient, this function will register a function
4989 * for an event to be used to parse the data instead.
4990 *
4991 * If @id is >= 0, then it is used to find the event.
4992 * else @sys_name and @event_name are used.
4993 */
4994int pevent_register_event_handler(struct pevent *pevent,
4995 int id, char *sys_name, char *event_name,
4996 pevent_event_handler_func func,
4997 void *context)
4998{
4999 struct event_format *event;
5000 struct event_handler *handle;
5001
5002 if (id >= 0) {
5003 /* search by id */
5004 event = pevent_find_event(pevent, id);
5005 if (!event)
5006 goto not_found;
5007 if (event_name && (strcmp(event_name, event->name) != 0))
5008 goto not_found;
5009 if (sys_name && (strcmp(sys_name, event->system) != 0))
5010 goto not_found;
5011 } else {
5012 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5013 if (!event)
5014 goto not_found;
5015 }
5016
5017 pr_stat("overriding event (%d) %s:%s with new print handler",
5018 event->id, event->system, event->name);
5019
5020 event->handler = func;
5021 event->context = context;
5022 return 0;
5023
5024 not_found:
5025 /* Save for later use. */
5026 handle = malloc_or_die(sizeof(*handle));
Steven Rostedt42c80132012-04-06 00:48:05 +02005027 memset(handle, 0, sizeof(*handle));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005028 handle->id = id;
5029 if (event_name)
5030 handle->event_name = strdup(event_name);
5031 if (sys_name)
5032 handle->sys_name = strdup(sys_name);
5033
5034 handle->func = func;
5035 handle->next = pevent->handlers;
5036 pevent->handlers = handle;
5037 handle->context = context;
5038
5039 return -1;
5040}
5041
5042/**
5043 * pevent_alloc - create a pevent handle
5044 */
5045struct pevent *pevent_alloc(void)
5046{
5047 struct pevent *pevent;
5048
5049 pevent = malloc(sizeof(*pevent));
5050 if (!pevent)
5051 return NULL;
5052 memset(pevent, 0, sizeof(*pevent));
5053 pevent->ref_count = 1;
5054
5055 return pevent;
5056}
5057
5058void pevent_ref(struct pevent *pevent)
5059{
5060 pevent->ref_count++;
5061}
5062
5063static void free_format_fields(struct format_field *field)
5064{
5065 struct format_field *next;
5066
5067 while (field) {
5068 next = field->next;
5069 free(field->type);
5070 free(field->name);
5071 free(field);
5072 field = next;
5073 }
5074}
5075
5076static void free_formats(struct format *format)
5077{
5078 free_format_fields(format->common_fields);
5079 free_format_fields(format->fields);
5080}
5081
5082static void free_event(struct event_format *event)
5083{
5084 free(event->name);
5085 free(event->system);
5086
5087 free_formats(&event->format);
5088
5089 free(event->print_fmt.format);
5090 free_args(event->print_fmt.args);
5091
5092 free(event);
5093}
5094
5095/**
5096 * pevent_free - free a pevent handle
5097 * @pevent: the pevent handle to free
5098 */
5099void pevent_free(struct pevent *pevent)
5100{
Steven Rostedta2525a02012-04-06 00:48:02 +02005101 struct cmdline_list *cmdlist, *cmdnext;
5102 struct func_list *funclist, *funcnext;
5103 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005104 struct pevent_function_handler *func_handler;
5105 struct event_handler *handle;
5106 int i;
5107
Steven Rostedta2525a02012-04-06 00:48:02 +02005108 if (!pevent)
5109 return;
5110
5111 cmdlist = pevent->cmdlist;
5112 funclist = pevent->funclist;
5113 printklist = pevent->printklist;
5114
Steven Rostedtf7d82352012-04-06 00:47:53 +02005115 pevent->ref_count--;
5116 if (pevent->ref_count)
5117 return;
5118
5119 if (pevent->cmdlines) {
5120 for (i = 0; i < pevent->cmdline_count; i++)
5121 free(pevent->cmdlines[i].comm);
5122 free(pevent->cmdlines);
5123 }
5124
5125 while (cmdlist) {
5126 cmdnext = cmdlist->next;
5127 free(cmdlist->comm);
5128 free(cmdlist);
5129 cmdlist = cmdnext;
5130 }
5131
5132 if (pevent->func_map) {
5133 for (i = 0; i < pevent->func_count; i++) {
5134 free(pevent->func_map[i].func);
5135 free(pevent->func_map[i].mod);
5136 }
5137 free(pevent->func_map);
5138 }
5139
5140 while (funclist) {
5141 funcnext = funclist->next;
5142 free(funclist->func);
5143 free(funclist->mod);
5144 free(funclist);
5145 funclist = funcnext;
5146 }
5147
5148 while (pevent->func_handlers) {
5149 func_handler = pevent->func_handlers;
5150 pevent->func_handlers = func_handler->next;
5151 free_func_handle(func_handler);
5152 }
5153
5154 if (pevent->printk_map) {
5155 for (i = 0; i < pevent->printk_count; i++)
5156 free(pevent->printk_map[i].printk);
5157 free(pevent->printk_map);
5158 }
5159
5160 while (printklist) {
5161 printknext = printklist->next;
5162 free(printklist->printk);
5163 free(printklist);
5164 printklist = printknext;
5165 }
5166
5167 for (i = 0; i < pevent->nr_events; i++)
5168 free_event(pevent->events[i]);
5169
5170 while (pevent->handlers) {
5171 handle = pevent->handlers;
5172 pevent->handlers = handle->next;
5173 free_handler(handle);
5174 }
5175
5176 free(pevent->events);
5177 free(pevent->sort_events);
5178
5179 free(pevent);
5180}
5181
5182void pevent_unref(struct pevent *pevent)
5183{
5184 pevent_free(pevent);
5185}