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