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