blob: 2091991691a63cb738e210cd25e9ff03092f0ab0 [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 */
Steven Rostedtf7d82352012-04-06 00:47:53 +020027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdarg.h>
31#include <ctype.h>
32#include <errno.h>
Robert Richter0cf26012012-08-07 19:43:14 +020033#include <stdint.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020034
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{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300120 return calloc(1, sizeof(struct print_arg));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200121}
122
123struct cmdline {
124 char *comm;
125 int pid;
126};
127
128static int cmdline_cmp(const void *a, const void *b)
129{
130 const struct cmdline *ca = a;
131 const struct cmdline *cb = b;
132
133 if (ca->pid < cb->pid)
134 return -1;
135 if (ca->pid > cb->pid)
136 return 1;
137
138 return 0;
139}
140
141struct cmdline_list {
142 struct cmdline_list *next;
143 char *comm;
144 int pid;
145};
146
147static int cmdline_init(struct pevent *pevent)
148{
149 struct cmdline_list *cmdlist = pevent->cmdlist;
150 struct cmdline_list *item;
151 struct cmdline *cmdlines;
152 int i;
153
154 cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
155
156 i = 0;
157 while (cmdlist) {
158 cmdlines[i].pid = cmdlist->pid;
159 cmdlines[i].comm = cmdlist->comm;
160 i++;
161 item = cmdlist;
162 cmdlist = cmdlist->next;
163 free(item);
164 }
165
166 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
167
168 pevent->cmdlines = cmdlines;
169 pevent->cmdlist = NULL;
170
171 return 0;
172}
173
174static char *find_cmdline(struct pevent *pevent, int pid)
175{
176 const struct cmdline *comm;
177 struct cmdline key;
178
179 if (!pid)
180 return "<idle>";
181
182 if (!pevent->cmdlines)
183 cmdline_init(pevent);
184
185 key.pid = pid;
186
187 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
188 sizeof(*pevent->cmdlines), cmdline_cmp);
189
190 if (comm)
191 return comm->comm;
192 return "<...>";
193}
194
195/**
196 * pevent_pid_is_registered - return if a pid has a cmdline registered
197 * @pevent: handle for the pevent
198 * @pid: The pid to check if it has a cmdline registered with.
199 *
200 * Returns 1 if the pid has a cmdline mapped to it
201 * 0 otherwise.
202 */
203int pevent_pid_is_registered(struct pevent *pevent, int pid)
204{
205 const struct cmdline *comm;
206 struct cmdline key;
207
208 if (!pid)
209 return 1;
210
211 if (!pevent->cmdlines)
212 cmdline_init(pevent);
213
214 key.pid = pid;
215
216 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
217 sizeof(*pevent->cmdlines), cmdline_cmp);
218
219 if (comm)
220 return 1;
221 return 0;
222}
223
224/*
225 * If the command lines have been converted to an array, then
226 * we must add this pid. This is much slower than when cmdlines
227 * are added before the array is initialized.
228 */
229static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
230{
231 struct cmdline *cmdlines = pevent->cmdlines;
232 const struct cmdline *cmdline;
233 struct cmdline key;
234
235 if (!pid)
236 return 0;
237
238 /* avoid duplicates */
239 key.pid = pid;
240
241 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
242 sizeof(*pevent->cmdlines), cmdline_cmp);
243 if (cmdline) {
244 errno = EEXIST;
245 return -1;
246 }
247
248 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
249 if (!cmdlines) {
250 errno = ENOMEM;
251 return -1;
252 }
253
254 cmdlines[pevent->cmdline_count].pid = pid;
255 cmdlines[pevent->cmdline_count].comm = strdup(comm);
256 if (!cmdlines[pevent->cmdline_count].comm)
257 die("malloc comm");
258
259 if (cmdlines[pevent->cmdline_count].comm)
260 pevent->cmdline_count++;
261
262 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
263 pevent->cmdlines = cmdlines;
264
265 return 0;
266}
267
268/**
269 * pevent_register_comm - register a pid / comm mapping
270 * @pevent: handle for the pevent
271 * @comm: the command line to register
272 * @pid: the pid to map the command line to
273 *
274 * This adds a mapping to search for command line names with
275 * a given pid. The comm is duplicated.
276 */
277int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
278{
279 struct cmdline_list *item;
280
281 if (pevent->cmdlines)
282 return add_new_comm(pevent, comm, pid);
283
284 item = malloc_or_die(sizeof(*item));
285 item->comm = strdup(comm);
286 if (!item->comm)
287 die("malloc comm");
288 item->pid = pid;
289 item->next = pevent->cmdlist;
290
291 pevent->cmdlist = item;
292 pevent->cmdline_count++;
293
294 return 0;
295}
296
297struct func_map {
298 unsigned long long addr;
299 char *func;
300 char *mod;
301};
302
303struct func_list {
304 struct func_list *next;
305 unsigned long long addr;
306 char *func;
307 char *mod;
308};
309
310static int func_cmp(const void *a, const void *b)
311{
312 const struct func_map *fa = a;
313 const struct func_map *fb = b;
314
315 if (fa->addr < fb->addr)
316 return -1;
317 if (fa->addr > fb->addr)
318 return 1;
319
320 return 0;
321}
322
323/*
324 * We are searching for a record in between, not an exact
325 * match.
326 */
327static int func_bcmp(const void *a, const void *b)
328{
329 const struct func_map *fa = a;
330 const struct func_map *fb = b;
331
332 if ((fa->addr == fb->addr) ||
333
334 (fa->addr > fb->addr &&
335 fa->addr < (fb+1)->addr))
336 return 0;
337
338 if (fa->addr < fb->addr)
339 return -1;
340
341 return 1;
342}
343
344static int func_map_init(struct pevent *pevent)
345{
346 struct func_list *funclist;
347 struct func_list *item;
348 struct func_map *func_map;
349 int i;
350
351 func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
352 funclist = pevent->funclist;
353
354 i = 0;
355 while (funclist) {
356 func_map[i].func = funclist->func;
357 func_map[i].addr = funclist->addr;
358 func_map[i].mod = funclist->mod;
359 i++;
360 item = funclist;
361 funclist = funclist->next;
362 free(item);
363 }
364
365 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
366
367 /*
368 * Add a special record at the end.
369 */
370 func_map[pevent->func_count].func = NULL;
371 func_map[pevent->func_count].addr = 0;
372 func_map[pevent->func_count].mod = NULL;
373
374 pevent->func_map = func_map;
375 pevent->funclist = NULL;
376
377 return 0;
378}
379
380static struct func_map *
381find_func(struct pevent *pevent, unsigned long long addr)
382{
383 struct func_map *func;
384 struct func_map key;
385
386 if (!pevent->func_map)
387 func_map_init(pevent);
388
389 key.addr = addr;
390
391 func = bsearch(&key, pevent->func_map, pevent->func_count,
392 sizeof(*pevent->func_map), func_bcmp);
393
394 return func;
395}
396
397/**
398 * pevent_find_function - find a function by a given address
399 * @pevent: handle for the pevent
400 * @addr: the address to find the function with
401 *
402 * Returns a pointer to the function stored that has the given
403 * address. Note, the address does not have to be exact, it
404 * will select the function that would contain the address.
405 */
406const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
407{
408 struct func_map *map;
409
410 map = find_func(pevent, addr);
411 if (!map)
412 return NULL;
413
414 return map->func;
415}
416
417/**
418 * pevent_find_function_address - find a function address by a given address
419 * @pevent: handle for the pevent
420 * @addr: the address to find the function with
421 *
422 * Returns the address the function starts at. This can be used in
423 * conjunction with pevent_find_function to print both the function
424 * name and the function offset.
425 */
426unsigned long long
427pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
428{
429 struct func_map *map;
430
431 map = find_func(pevent, addr);
432 if (!map)
433 return 0;
434
435 return map->addr;
436}
437
438/**
439 * pevent_register_function - register a function with a given address
440 * @pevent: handle for the pevent
441 * @function: the function name to register
442 * @addr: the address the function starts at
443 * @mod: the kernel module the function may be in (NULL for none)
444 *
445 * This registers a function name with an address and module.
446 * The @func passed in is duplicated.
447 */
448int pevent_register_function(struct pevent *pevent, char *func,
449 unsigned long long addr, char *mod)
450{
451 struct func_list *item;
452
453 item = malloc_or_die(sizeof(*item));
454
455 item->next = pevent->funclist;
456 item->func = strdup(func);
457 if (mod)
458 item->mod = strdup(mod);
459 else
460 item->mod = NULL;
461 item->addr = addr;
462
Namhyung Kimca638582012-04-09 11:54:31 +0900463 if (!item->func || (mod && !item->mod))
464 die("malloc func");
Steven Rostedtf7d82352012-04-06 00:47:53 +0200465
Namhyung Kimca638582012-04-09 11:54:31 +0900466 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200467 pevent->func_count++;
468
469 return 0;
470}
471
472/**
473 * pevent_print_funcs - print out the stored functions
474 * @pevent: handle for the pevent
475 *
476 * This prints out the stored functions.
477 */
478void pevent_print_funcs(struct pevent *pevent)
479{
480 int i;
481
482 if (!pevent->func_map)
483 func_map_init(pevent);
484
485 for (i = 0; i < (int)pevent->func_count; i++) {
486 printf("%016llx %s",
487 pevent->func_map[i].addr,
488 pevent->func_map[i].func);
489 if (pevent->func_map[i].mod)
490 printf(" [%s]\n", pevent->func_map[i].mod);
491 else
492 printf("\n");
493 }
494}
495
496struct printk_map {
497 unsigned long long addr;
498 char *printk;
499};
500
501struct printk_list {
502 struct printk_list *next;
503 unsigned long long addr;
504 char *printk;
505};
506
507static int printk_cmp(const void *a, const void *b)
508{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900509 const struct printk_map *pa = a;
510 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200511
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900512 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200513 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900514 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200515 return 1;
516
517 return 0;
518}
519
520static void printk_map_init(struct pevent *pevent)
521{
522 struct printk_list *printklist;
523 struct printk_list *item;
524 struct printk_map *printk_map;
525 int i;
526
527 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
528
529 printklist = pevent->printklist;
530
531 i = 0;
532 while (printklist) {
533 printk_map[i].printk = printklist->printk;
534 printk_map[i].addr = printklist->addr;
535 i++;
536 item = printklist;
537 printklist = printklist->next;
538 free(item);
539 }
540
541 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
542
543 pevent->printk_map = printk_map;
544 pevent->printklist = NULL;
545}
546
547static struct printk_map *
548find_printk(struct pevent *pevent, unsigned long long addr)
549{
550 struct printk_map *printk;
551 struct printk_map key;
552
553 if (!pevent->printk_map)
554 printk_map_init(pevent);
555
556 key.addr = addr;
557
558 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
559 sizeof(*pevent->printk_map), printk_cmp);
560
561 return printk;
562}
563
564/**
565 * pevent_register_print_string - register a string by its address
566 * @pevent: handle for the pevent
567 * @fmt: the string format to register
568 * @addr: the address the string was located at
569 *
570 * This registers a string by the address it was stored in the kernel.
571 * The @fmt passed in is duplicated.
572 */
573int pevent_register_print_string(struct pevent *pevent, char *fmt,
574 unsigned long long addr)
575{
576 struct printk_list *item;
577
578 item = malloc_or_die(sizeof(*item));
579
580 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200581 item->printk = strdup(fmt);
582 item->addr = addr;
583
Namhyung Kimca638582012-04-09 11:54:31 +0900584 if (!item->printk)
585 die("malloc fmt");
586
587 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200588 pevent->printk_count++;
589
590 return 0;
591}
592
593/**
594 * pevent_print_printk - print out the stored strings
595 * @pevent: handle for the pevent
596 *
597 * This prints the string formats that were stored.
598 */
599void pevent_print_printk(struct pevent *pevent)
600{
601 int i;
602
603 if (!pevent->printk_map)
604 printk_map_init(pevent);
605
606 for (i = 0; i < (int)pevent->printk_count; i++) {
607 printf("%016llx %s\n",
608 pevent->printk_map[i].addr,
609 pevent->printk_map[i].printk);
610 }
611}
612
613static struct event_format *alloc_event(void)
614{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300615 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200616}
617
618static void add_event(struct pevent *pevent, struct event_format *event)
619{
620 int i;
621
Namhyung Kimf6ced602012-04-24 10:29:44 +0900622 pevent->events = realloc(pevent->events, sizeof(event) *
623 (pevent->nr_events + 1));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200624 if (!pevent->events)
625 die("Can not allocate events");
626
627 for (i = 0; i < pevent->nr_events; i++) {
628 if (pevent->events[i]->id > event->id)
629 break;
630 }
631 if (i < pevent->nr_events)
632 memmove(&pevent->events[i + 1],
633 &pevent->events[i],
634 sizeof(event) * (pevent->nr_events - i));
635
636 pevent->events[i] = event;
637 pevent->nr_events++;
638
639 event->pevent = pevent;
640}
641
642static int event_item_type(enum event_type type)
643{
644 switch (type) {
645 case EVENT_ITEM ... EVENT_SQUOTE:
646 return 1;
647 case EVENT_ERROR ... EVENT_DELIM:
648 default:
649 return 0;
650 }
651}
652
653static void free_flag_sym(struct print_flag_sym *fsym)
654{
655 struct print_flag_sym *next;
656
657 while (fsym) {
658 next = fsym->next;
659 free(fsym->value);
660 free(fsym->str);
661 free(fsym);
662 fsym = next;
663 }
664}
665
666static void free_arg(struct print_arg *arg)
667{
668 struct print_arg *farg;
669
670 if (!arg)
671 return;
672
673 switch (arg->type) {
674 case PRINT_ATOM:
675 free(arg->atom.atom);
676 break;
677 case PRINT_FIELD:
678 free(arg->field.name);
679 break;
680 case PRINT_FLAGS:
681 free_arg(arg->flags.field);
682 free(arg->flags.delim);
683 free_flag_sym(arg->flags.flags);
684 break;
685 case PRINT_SYMBOL:
686 free_arg(arg->symbol.field);
687 free_flag_sym(arg->symbol.symbols);
688 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900689 case PRINT_HEX:
690 free_arg(arg->hex.field);
691 free_arg(arg->hex.size);
692 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200693 case PRINT_TYPE:
694 free(arg->typecast.type);
695 free_arg(arg->typecast.item);
696 break;
697 case PRINT_STRING:
698 case PRINT_BSTRING:
699 free(arg->string.string);
700 break;
701 case PRINT_DYNAMIC_ARRAY:
702 free(arg->dynarray.index);
703 break;
704 case PRINT_OP:
705 free(arg->op.op);
706 free_arg(arg->op.left);
707 free_arg(arg->op.right);
708 break;
709 case PRINT_FUNC:
710 while (arg->func.args) {
711 farg = arg->func.args;
712 arg->func.args = farg->next;
713 free_arg(farg);
714 }
715 break;
716
717 case PRINT_NULL:
718 default:
719 break;
720 }
721
722 free(arg);
723}
724
725static enum event_type get_type(int ch)
726{
727 if (ch == '\n')
728 return EVENT_NEWLINE;
729 if (isspace(ch))
730 return EVENT_SPACE;
731 if (isalnum(ch) || ch == '_')
732 return EVENT_ITEM;
733 if (ch == '\'')
734 return EVENT_SQUOTE;
735 if (ch == '"')
736 return EVENT_DQUOTE;
737 if (!isprint(ch))
738 return EVENT_NONE;
739 if (ch == '(' || ch == ')' || ch == ',')
740 return EVENT_DELIM;
741
742 return EVENT_OP;
743}
744
745static int __read_char(void)
746{
747 if (input_buf_ptr >= input_buf_siz)
748 return -1;
749
750 return input_buf[input_buf_ptr++];
751}
752
753static int __peek_char(void)
754{
755 if (input_buf_ptr >= input_buf_siz)
756 return -1;
757
758 return input_buf[input_buf_ptr];
759}
760
761/**
762 * pevent_peek_char - peek at the next character that will be read
763 *
764 * Returns the next character read, or -1 if end of buffer.
765 */
766int pevent_peek_char(void)
767{
768 return __peek_char();
769}
770
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900771static int extend_token(char **tok, char *buf, int size)
772{
773 char *newtok = realloc(*tok, size);
774
775 if (!newtok) {
776 free(*tok);
777 *tok = NULL;
778 return -1;
779 }
780
781 if (!*tok)
782 strcpy(newtok, buf);
783 else
784 strcat(newtok, buf);
785 *tok = newtok;
786
787 return 0;
788}
789
Steven Rostedtf7d82352012-04-06 00:47:53 +0200790static enum event_type force_token(const char *str, char **tok);
791
792static enum event_type __read_token(char **tok)
793{
794 char buf[BUFSIZ];
795 int ch, last_ch, quote_ch, next_ch;
796 int i = 0;
797 int tok_size = 0;
798 enum event_type type;
799
800 *tok = NULL;
801
802
803 ch = __read_char();
804 if (ch < 0)
805 return EVENT_NONE;
806
807 type = get_type(ch);
808 if (type == EVENT_NONE)
809 return type;
810
811 buf[i++] = ch;
812
813 switch (type) {
814 case EVENT_NEWLINE:
815 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300816 if (asprintf(tok, "%c", ch) < 0)
817 return EVENT_ERROR;
818
Steven Rostedtf7d82352012-04-06 00:47:53 +0200819 return type;
820
821 case EVENT_OP:
822 switch (ch) {
823 case '-':
824 next_ch = __peek_char();
825 if (next_ch == '>') {
826 buf[i++] = __read_char();
827 break;
828 }
829 /* fall through */
830 case '+':
831 case '|':
832 case '&':
833 case '>':
834 case '<':
835 last_ch = ch;
836 ch = __peek_char();
837 if (ch != last_ch)
838 goto test_equal;
839 buf[i++] = __read_char();
840 switch (last_ch) {
841 case '>':
842 case '<':
843 goto test_equal;
844 default:
845 break;
846 }
847 break;
848 case '!':
849 case '=':
850 goto test_equal;
851 default: /* what should we do instead? */
852 break;
853 }
854 buf[i] = 0;
855 *tok = strdup(buf);
856 return type;
857
858 test_equal:
859 ch = __peek_char();
860 if (ch == '=')
861 buf[i++] = __read_char();
862 goto out;
863
864 case EVENT_DQUOTE:
865 case EVENT_SQUOTE:
866 /* don't keep quotes */
867 i--;
868 quote_ch = ch;
869 last_ch = 0;
870 concat:
871 do {
872 if (i == (BUFSIZ - 1)) {
873 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200874 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900875
876 if (extend_token(tok, buf, tok_size) < 0)
877 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200878 i = 0;
879 }
880 last_ch = ch;
881 ch = __read_char();
882 buf[i++] = ch;
883 /* the '\' '\' will cancel itself */
884 if (ch == '\\' && last_ch == '\\')
885 last_ch = 0;
886 } while (ch != quote_ch || last_ch == '\\');
887 /* remove the last quote */
888 i--;
889
890 /*
891 * For strings (double quotes) check the next token.
892 * If it is another string, concatinate the two.
893 */
894 if (type == EVENT_DQUOTE) {
895 unsigned long long save_input_buf_ptr = input_buf_ptr;
896
897 do {
898 ch = __read_char();
899 } while (isspace(ch));
900 if (ch == '"')
901 goto concat;
902 input_buf_ptr = save_input_buf_ptr;
903 }
904
905 goto out;
906
907 case EVENT_ERROR ... EVENT_SPACE:
908 case EVENT_ITEM:
909 default:
910 break;
911 }
912
913 while (get_type(__peek_char()) == type) {
914 if (i == (BUFSIZ - 1)) {
915 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200916 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900917
918 if (extend_token(tok, buf, tok_size) < 0)
919 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200920 i = 0;
921 }
922 ch = __read_char();
923 buf[i++] = ch;
924 }
925
926 out:
927 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900928 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200929 return EVENT_NONE;
930
931 if (type == EVENT_ITEM) {
932 /*
933 * Older versions of the kernel has a bug that
934 * creates invalid symbols and will break the mac80211
935 * parsing. This is a work around to that bug.
936 *
937 * See Linux kernel commit:
938 * 811cb50baf63461ce0bdb234927046131fc7fa8b
939 */
940 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
941 free(*tok);
942 *tok = NULL;
943 return force_token("\"\%s\" ", tok);
944 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
945 free(*tok);
946 *tok = NULL;
947 return force_token("\" sta:%pM\" ", tok);
948 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
949 free(*tok);
950 *tok = NULL;
951 return force_token("\" vif:%p(%d)\" ", tok);
952 }
953 }
954
955 return type;
956}
957
958static enum event_type force_token(const char *str, char **tok)
959{
960 const char *save_input_buf;
961 unsigned long long save_input_buf_ptr;
962 unsigned long long save_input_buf_siz;
963 enum event_type type;
964
965 /* save off the current input pointers */
966 save_input_buf = input_buf;
967 save_input_buf_ptr = input_buf_ptr;
968 save_input_buf_siz = input_buf_siz;
969
970 init_input_buf(str, strlen(str));
971
972 type = __read_token(tok);
973
974 /* reset back to original token */
975 input_buf = save_input_buf;
976 input_buf_ptr = save_input_buf_ptr;
977 input_buf_siz = save_input_buf_siz;
978
979 return type;
980}
981
982static void free_token(char *tok)
983{
984 if (tok)
985 free(tok);
986}
987
988static enum event_type read_token(char **tok)
989{
990 enum event_type type;
991
992 for (;;) {
993 type = __read_token(tok);
994 if (type != EVENT_SPACE)
995 return type;
996
997 free_token(*tok);
998 }
999
1000 /* not reached */
1001 *tok = NULL;
1002 return EVENT_NONE;
1003}
1004
1005/**
1006 * pevent_read_token - access to utilites to use the pevent parser
1007 * @tok: The token to return
1008 *
1009 * This will parse tokens from the string given by
1010 * pevent_init_data().
1011 *
1012 * Returns the token type.
1013 */
1014enum event_type pevent_read_token(char **tok)
1015{
1016 return read_token(tok);
1017}
1018
1019/**
1020 * pevent_free_token - free a token returned by pevent_read_token
1021 * @token: the token to free
1022 */
1023void pevent_free_token(char *token)
1024{
1025 free_token(token);
1026}
1027
1028/* no newline */
1029static enum event_type read_token_item(char **tok)
1030{
1031 enum event_type type;
1032
1033 for (;;) {
1034 type = __read_token(tok);
1035 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1036 return type;
1037 free_token(*tok);
1038 *tok = NULL;
1039 }
1040
1041 /* not reached */
1042 *tok = NULL;
1043 return EVENT_NONE;
1044}
1045
1046static int test_type(enum event_type type, enum event_type expect)
1047{
1048 if (type != expect) {
1049 do_warning("Error: expected type %d but read %d",
1050 expect, type);
1051 return -1;
1052 }
1053 return 0;
1054}
1055
1056static int test_type_token(enum event_type type, const char *token,
1057 enum event_type expect, const char *expect_tok)
1058{
1059 if (type != expect) {
1060 do_warning("Error: expected type %d but read %d",
1061 expect, type);
1062 return -1;
1063 }
1064
1065 if (strcmp(token, expect_tok) != 0) {
1066 do_warning("Error: expected '%s' but read '%s'",
1067 expect_tok, token);
1068 return -1;
1069 }
1070 return 0;
1071}
1072
1073static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1074{
1075 enum event_type type;
1076
1077 if (newline_ok)
1078 type = read_token(tok);
1079 else
1080 type = read_token_item(tok);
1081 return test_type(type, expect);
1082}
1083
1084static int read_expect_type(enum event_type expect, char **tok)
1085{
1086 return __read_expect_type(expect, tok, 1);
1087}
1088
1089static int __read_expected(enum event_type expect, const char *str,
1090 int newline_ok)
1091{
1092 enum event_type type;
1093 char *token;
1094 int ret;
1095
1096 if (newline_ok)
1097 type = read_token(&token);
1098 else
1099 type = read_token_item(&token);
1100
1101 ret = test_type_token(type, token, expect, str);
1102
1103 free_token(token);
1104
1105 return ret;
1106}
1107
1108static int read_expected(enum event_type expect, const char *str)
1109{
1110 return __read_expected(expect, str, 1);
1111}
1112
1113static int read_expected_item(enum event_type expect, const char *str)
1114{
1115 return __read_expected(expect, str, 0);
1116}
1117
1118static char *event_read_name(void)
1119{
1120 char *token;
1121
1122 if (read_expected(EVENT_ITEM, "name") < 0)
1123 return NULL;
1124
1125 if (read_expected(EVENT_OP, ":") < 0)
1126 return NULL;
1127
1128 if (read_expect_type(EVENT_ITEM, &token) < 0)
1129 goto fail;
1130
1131 return token;
1132
1133 fail:
1134 free_token(token);
1135 return NULL;
1136}
1137
1138static int event_read_id(void)
1139{
1140 char *token;
1141 int id;
1142
1143 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1144 return -1;
1145
1146 if (read_expected(EVENT_OP, ":") < 0)
1147 return -1;
1148
1149 if (read_expect_type(EVENT_ITEM, &token) < 0)
1150 goto fail;
1151
1152 id = strtoul(token, NULL, 0);
1153 free_token(token);
1154 return id;
1155
1156 fail:
1157 free_token(token);
1158 return -1;
1159}
1160
1161static int field_is_string(struct format_field *field)
1162{
1163 if ((field->flags & FIELD_IS_ARRAY) &&
1164 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1165 strstr(field->type, "s8")))
1166 return 1;
1167
1168 return 0;
1169}
1170
1171static int field_is_dynamic(struct format_field *field)
1172{
1173 if (strncmp(field->type, "__data_loc", 10) == 0)
1174 return 1;
1175
1176 return 0;
1177}
1178
1179static int field_is_long(struct format_field *field)
1180{
1181 /* includes long long */
1182 if (strstr(field->type, "long"))
1183 return 1;
1184
1185 return 0;
1186}
1187
1188static int event_read_fields(struct event_format *event, struct format_field **fields)
1189{
1190 struct format_field *field = NULL;
1191 enum event_type type;
1192 char *token;
1193 char *last_token;
1194 int count = 0;
1195
1196 do {
1197 type = read_token(&token);
1198 if (type == EVENT_NEWLINE) {
1199 free_token(token);
1200 return count;
1201 }
1202
1203 count++;
1204
1205 if (test_type_token(type, token, EVENT_ITEM, "field"))
1206 goto fail;
1207 free_token(token);
1208
1209 type = read_token(&token);
1210 /*
1211 * The ftrace fields may still use the "special" name.
1212 * Just ignore it.
1213 */
1214 if (event->flags & EVENT_FL_ISFTRACE &&
1215 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1216 free_token(token);
1217 type = read_token(&token);
1218 }
1219
1220 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1221 goto fail;
1222
1223 free_token(token);
1224 if (read_expect_type(EVENT_ITEM, &token) < 0)
1225 goto fail;
1226
1227 last_token = token;
1228
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001229 field = calloc(1, sizeof(*field));
1230 if (!field)
1231 goto fail;
1232
Steven Rostedtf7d82352012-04-06 00:47:53 +02001233 field->event = event;
1234
1235 /* read the rest of the type */
1236 for (;;) {
1237 type = read_token(&token);
1238 if (type == EVENT_ITEM ||
1239 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1240 /*
1241 * Some of the ftrace fields are broken and have
1242 * an illegal "." in them.
1243 */
1244 (event->flags & EVENT_FL_ISFTRACE &&
1245 type == EVENT_OP && strcmp(token, ".") == 0)) {
1246
1247 if (strcmp(token, "*") == 0)
1248 field->flags |= FIELD_IS_POINTER;
1249
1250 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001251 char *new_type;
1252 new_type = realloc(field->type,
1253 strlen(field->type) +
1254 strlen(last_token) + 2);
1255 if (!new_type) {
1256 free(last_token);
1257 goto fail;
1258 }
1259 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001260 strcat(field->type, " ");
1261 strcat(field->type, last_token);
1262 free(last_token);
1263 } else
1264 field->type = last_token;
1265 last_token = token;
1266 continue;
1267 }
1268
1269 break;
1270 }
1271
1272 if (!field->type) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001273 do_warning("%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001274 goto fail;
1275 }
1276 field->name = last_token;
1277
1278 if (test_type(type, EVENT_OP))
1279 goto fail;
1280
1281 if (strcmp(token, "[") == 0) {
1282 enum event_type last_type = type;
1283 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001284 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001285 int len;
1286
1287 field->flags |= FIELD_IS_ARRAY;
1288
1289 type = read_token(&token);
1290
1291 if (type == EVENT_ITEM)
1292 field->arraylen = strtoul(token, NULL, 0);
1293 else
1294 field->arraylen = 0;
1295
1296 while (strcmp(token, "]") != 0) {
1297 if (last_type == EVENT_ITEM &&
1298 type == EVENT_ITEM)
1299 len = 2;
1300 else
1301 len = 1;
1302 last_type = type;
1303
Namhyung Kimd2864472012-04-09 11:54:33 +09001304 new_brackets = realloc(brackets,
1305 strlen(brackets) +
1306 strlen(token) + len);
1307 if (!new_brackets) {
1308 free(brackets);
1309 goto fail;
1310 }
1311 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001312 if (len == 2)
1313 strcat(brackets, " ");
1314 strcat(brackets, token);
1315 /* We only care about the last token */
1316 field->arraylen = strtoul(token, NULL, 0);
1317 free_token(token);
1318 type = read_token(&token);
1319 if (type == EVENT_NONE) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001320 do_warning("failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001321 goto fail;
1322 }
1323 }
1324
1325 free_token(token);
1326
Namhyung Kimd2864472012-04-09 11:54:33 +09001327 new_brackets = realloc(brackets, strlen(brackets) + 2);
1328 if (!new_brackets) {
1329 free(brackets);
1330 goto fail;
1331 }
1332 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001333 strcat(brackets, "]");
1334
1335 /* add brackets to type */
1336
1337 type = read_token(&token);
1338 /*
1339 * If the next token is not an OP, then it is of
1340 * the format: type [] item;
1341 */
1342 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001343 char *new_type;
1344 new_type = realloc(field->type,
1345 strlen(field->type) +
1346 strlen(field->name) +
1347 strlen(brackets) + 2);
1348 if (!new_type) {
1349 free(brackets);
1350 goto fail;
1351 }
1352 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001353 strcat(field->type, " ");
1354 strcat(field->type, field->name);
1355 free_token(field->name);
1356 strcat(field->type, brackets);
1357 field->name = token;
1358 type = read_token(&token);
1359 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001360 char *new_type;
1361 new_type = realloc(field->type,
1362 strlen(field->type) +
1363 strlen(brackets) + 1);
1364 if (!new_type) {
1365 free(brackets);
1366 goto fail;
1367 }
1368 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001369 strcat(field->type, brackets);
1370 }
1371 free(brackets);
1372 }
1373
1374 if (field_is_string(field))
1375 field->flags |= FIELD_IS_STRING;
1376 if (field_is_dynamic(field))
1377 field->flags |= FIELD_IS_DYNAMIC;
1378 if (field_is_long(field))
1379 field->flags |= FIELD_IS_LONG;
1380
1381 if (test_type_token(type, token, EVENT_OP, ";"))
1382 goto fail;
1383 free_token(token);
1384
1385 if (read_expected(EVENT_ITEM, "offset") < 0)
1386 goto fail_expect;
1387
1388 if (read_expected(EVENT_OP, ":") < 0)
1389 goto fail_expect;
1390
1391 if (read_expect_type(EVENT_ITEM, &token))
1392 goto fail;
1393 field->offset = strtoul(token, NULL, 0);
1394 free_token(token);
1395
1396 if (read_expected(EVENT_OP, ";") < 0)
1397 goto fail_expect;
1398
1399 if (read_expected(EVENT_ITEM, "size") < 0)
1400 goto fail_expect;
1401
1402 if (read_expected(EVENT_OP, ":") < 0)
1403 goto fail_expect;
1404
1405 if (read_expect_type(EVENT_ITEM, &token))
1406 goto fail;
1407 field->size = strtoul(token, NULL, 0);
1408 free_token(token);
1409
1410 if (read_expected(EVENT_OP, ";") < 0)
1411 goto fail_expect;
1412
1413 type = read_token(&token);
1414 if (type != EVENT_NEWLINE) {
1415 /* newer versions of the kernel have a "signed" type */
1416 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1417 goto fail;
1418
1419 free_token(token);
1420
1421 if (read_expected(EVENT_OP, ":") < 0)
1422 goto fail_expect;
1423
1424 if (read_expect_type(EVENT_ITEM, &token))
1425 goto fail;
1426
1427 /* add signed type */
1428
1429 free_token(token);
1430 if (read_expected(EVENT_OP, ";") < 0)
1431 goto fail_expect;
1432
1433 if (read_expect_type(EVENT_NEWLINE, &token))
1434 goto fail;
1435 }
1436
1437 free_token(token);
1438
1439 if (field->flags & FIELD_IS_ARRAY) {
1440 if (field->arraylen)
1441 field->elementsize = field->size / field->arraylen;
1442 else if (field->flags & FIELD_IS_STRING)
1443 field->elementsize = 1;
1444 else
1445 field->elementsize = event->pevent->long_size;
1446 } else
1447 field->elementsize = field->size;
1448
1449 *fields = field;
1450 fields = &field->next;
1451
1452 } while (1);
1453
1454 return 0;
1455
1456fail:
1457 free_token(token);
1458fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001459 if (field) {
1460 free(field->type);
1461 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001462 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001463 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001464 return -1;
1465}
1466
1467static int event_read_format(struct event_format *event)
1468{
1469 char *token;
1470 int ret;
1471
1472 if (read_expected_item(EVENT_ITEM, "format") < 0)
1473 return -1;
1474
1475 if (read_expected(EVENT_OP, ":") < 0)
1476 return -1;
1477
1478 if (read_expect_type(EVENT_NEWLINE, &token))
1479 goto fail;
1480 free_token(token);
1481
1482 ret = event_read_fields(event, &event->format.common_fields);
1483 if (ret < 0)
1484 return ret;
1485 event->format.nr_common = ret;
1486
1487 ret = event_read_fields(event, &event->format.fields);
1488 if (ret < 0)
1489 return ret;
1490 event->format.nr_fields = ret;
1491
1492 return 0;
1493
1494 fail:
1495 free_token(token);
1496 return -1;
1497}
1498
1499static enum event_type
1500process_arg_token(struct event_format *event, struct print_arg *arg,
1501 char **tok, enum event_type type);
1502
1503static enum event_type
1504process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1505{
1506 enum event_type type;
1507 char *token;
1508
1509 type = read_token(&token);
1510 *tok = token;
1511
1512 return process_arg_token(event, arg, tok, type);
1513}
1514
1515static enum event_type
1516process_op(struct event_format *event, struct print_arg *arg, char **tok);
1517
1518static enum event_type
1519process_cond(struct event_format *event, struct print_arg *top, char **tok)
1520{
1521 struct print_arg *arg, *left, *right;
1522 enum event_type type;
1523 char *token = NULL;
1524
1525 arg = alloc_arg();
1526 left = alloc_arg();
1527 right = alloc_arg();
1528
1529 arg->type = PRINT_OP;
1530 arg->op.left = left;
1531 arg->op.right = right;
1532
1533 *tok = NULL;
1534 type = process_arg(event, left, &token);
1535
1536 again:
1537 /* Handle other operations in the arguments */
1538 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1539 type = process_op(event, left, &token);
1540 goto again;
1541 }
1542
1543 if (test_type_token(type, token, EVENT_OP, ":"))
1544 goto out_free;
1545
1546 arg->op.op = token;
1547
1548 type = process_arg(event, right, &token);
1549
1550 top->op.right = arg;
1551
1552 *tok = token;
1553 return type;
1554
1555out_free:
1556 /* Top may point to itself */
1557 top->op.right = NULL;
1558 free_token(token);
1559 free_arg(arg);
1560 return EVENT_ERROR;
1561}
1562
1563static enum event_type
1564process_array(struct event_format *event, struct print_arg *top, char **tok)
1565{
1566 struct print_arg *arg;
1567 enum event_type type;
1568 char *token = NULL;
1569
1570 arg = alloc_arg();
1571
1572 *tok = NULL;
1573 type = process_arg(event, arg, &token);
1574 if (test_type_token(type, token, EVENT_OP, "]"))
1575 goto out_free;
1576
1577 top->op.right = arg;
1578
1579 free_token(token);
1580 type = read_token_item(&token);
1581 *tok = token;
1582
1583 return type;
1584
1585out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001586 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001587 free_arg(arg);
1588 return EVENT_ERROR;
1589}
1590
1591static int get_op_prio(char *op)
1592{
1593 if (!op[1]) {
1594 switch (op[0]) {
1595 case '~':
1596 case '!':
1597 return 4;
1598 case '*':
1599 case '/':
1600 case '%':
1601 return 6;
1602 case '+':
1603 case '-':
1604 return 7;
1605 /* '>>' and '<<' are 8 */
1606 case '<':
1607 case '>':
1608 return 9;
1609 /* '==' and '!=' are 10 */
1610 case '&':
1611 return 11;
1612 case '^':
1613 return 12;
1614 case '|':
1615 return 13;
1616 case '?':
1617 return 16;
1618 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001619 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001620 return -1;
1621 }
1622 } else {
1623 if (strcmp(op, "++") == 0 ||
1624 strcmp(op, "--") == 0) {
1625 return 3;
1626 } else if (strcmp(op, ">>") == 0 ||
1627 strcmp(op, "<<") == 0) {
1628 return 8;
1629 } else if (strcmp(op, ">=") == 0 ||
1630 strcmp(op, "<=") == 0) {
1631 return 9;
1632 } else if (strcmp(op, "==") == 0 ||
1633 strcmp(op, "!=") == 0) {
1634 return 10;
1635 } else if (strcmp(op, "&&") == 0) {
1636 return 14;
1637 } else if (strcmp(op, "||") == 0) {
1638 return 15;
1639 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001640 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001641 return -1;
1642 }
1643 }
1644}
1645
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001646static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001647{
1648
1649 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001650 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001651 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001652 else
1653 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001654
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001655 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001656}
1657
1658/* Note, *tok does not get freed, but will most likely be saved */
1659static enum event_type
1660process_op(struct event_format *event, struct print_arg *arg, char **tok)
1661{
1662 struct print_arg *left, *right = NULL;
1663 enum event_type type;
1664 char *token;
1665
1666 /* the op is passed in via tok */
1667 token = *tok;
1668
1669 if (arg->type == PRINT_OP && !arg->op.left) {
1670 /* handle single op */
1671 if (token[1]) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001672 do_warning("bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001673 goto out_free;
1674 }
1675 switch (token[0]) {
1676 case '~':
1677 case '!':
1678 case '+':
1679 case '-':
1680 break;
1681 default:
1682 do_warning("bad op token %s", token);
1683 goto out_free;
1684
1685 }
1686
1687 /* make an empty left */
1688 left = alloc_arg();
1689 left->type = PRINT_NULL;
1690 arg->op.left = left;
1691
1692 right = alloc_arg();
1693 arg->op.right = right;
1694
1695 /* do not free the token, it belongs to an op */
1696 *tok = NULL;
1697 type = process_arg(event, right, tok);
1698
1699 } else if (strcmp(token, "?") == 0) {
1700
1701 left = alloc_arg();
1702 /* copy the top arg to the left */
1703 *left = *arg;
1704
1705 arg->type = PRINT_OP;
1706 arg->op.op = token;
1707 arg->op.left = left;
1708 arg->op.prio = 0;
1709
Namhyung Kim41e51a22012-09-19 15:58:42 +09001710 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001711 type = process_cond(event, arg, tok);
1712
1713 } else if (strcmp(token, ">>") == 0 ||
1714 strcmp(token, "<<") == 0 ||
1715 strcmp(token, "&") == 0 ||
1716 strcmp(token, "|") == 0 ||
1717 strcmp(token, "&&") == 0 ||
1718 strcmp(token, "||") == 0 ||
1719 strcmp(token, "-") == 0 ||
1720 strcmp(token, "+") == 0 ||
1721 strcmp(token, "*") == 0 ||
1722 strcmp(token, "^") == 0 ||
1723 strcmp(token, "/") == 0 ||
1724 strcmp(token, "<") == 0 ||
1725 strcmp(token, ">") == 0 ||
1726 strcmp(token, "==") == 0 ||
1727 strcmp(token, "!=") == 0) {
1728
1729 left = alloc_arg();
1730
1731 /* copy the top arg to the left */
1732 *left = *arg;
1733
1734 arg->type = PRINT_OP;
1735 arg->op.op = token;
1736 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001737 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001738
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001739 if (set_op_prio(arg) == -1) {
1740 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001741 /* arg->op.op (= token) will be freed at out_free */
1742 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001743 goto out_free;
1744 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001745
1746 type = read_token_item(&token);
1747 *tok = token;
1748
1749 /* could just be a type pointer */
1750 if ((strcmp(arg->op.op, "*") == 0) &&
1751 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001752 char *new_atom;
1753
Steven Rostedtf7d82352012-04-06 00:47:53 +02001754 if (left->type != PRINT_ATOM)
1755 die("bad pointer type");
Namhyung Kimd2864472012-04-09 11:54:33 +09001756 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001757 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001758 if (!new_atom)
1759 goto out_free;
1760
1761 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001762 strcat(left->atom.atom, " *");
1763 free(arg->op.op);
1764 *arg = *left;
1765 free(left);
1766
1767 return type;
1768 }
1769
1770 right = alloc_arg();
1771 type = process_arg_token(event, right, tok, type);
1772 arg->op.right = right;
1773
1774 } else if (strcmp(token, "[") == 0) {
1775
1776 left = alloc_arg();
1777 *left = *arg;
1778
1779 arg->type = PRINT_OP;
1780 arg->op.op = token;
1781 arg->op.left = left;
1782
1783 arg->op.prio = 0;
1784
Namhyung Kim41e51a22012-09-19 15:58:42 +09001785 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001786 type = process_array(event, arg, tok);
1787
1788 } else {
1789 do_warning("unknown op '%s'", token);
1790 event->flags |= EVENT_FL_FAILED;
1791 /* the arg is now the left side */
1792 goto out_free;
1793 }
1794
1795 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1796 int prio;
1797
1798 /* higher prios need to be closer to the root */
1799 prio = get_op_prio(*tok);
1800
1801 if (prio > arg->op.prio)
1802 return process_op(event, arg, tok);
1803
1804 return process_op(event, right, tok);
1805 }
1806
1807 return type;
1808
1809 out_free:
1810 free_token(token);
1811 *tok = NULL;
1812 return EVENT_ERROR;
1813}
1814
1815static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001816process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001817 char **tok)
1818{
1819 enum event_type type;
1820 char *field;
1821 char *token;
1822
1823 if (read_expected(EVENT_OP, "->") < 0)
1824 goto out_err;
1825
1826 if (read_expect_type(EVENT_ITEM, &token) < 0)
1827 goto out_free;
1828 field = token;
1829
1830 arg->type = PRINT_FIELD;
1831 arg->field.name = field;
1832
Tom Zanussi5205aec2012-04-06 00:47:58 +02001833 if (is_flag_field) {
1834 arg->field.field = pevent_find_any_field(event, arg->field.name);
1835 arg->field.field->flags |= FIELD_IS_FLAG;
1836 is_flag_field = 0;
1837 } else if (is_symbolic_field) {
1838 arg->field.field = pevent_find_any_field(event, arg->field.name);
1839 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1840 is_symbolic_field = 0;
1841 }
1842
Steven Rostedtf7d82352012-04-06 00:47:53 +02001843 type = read_token(&token);
1844 *tok = token;
1845
1846 return type;
1847
1848 out_free:
1849 free_token(token);
1850 out_err:
1851 *tok = NULL;
1852 return EVENT_ERROR;
1853}
1854
1855static char *arg_eval (struct print_arg *arg);
1856
1857static unsigned long long
1858eval_type_str(unsigned long long val, const char *type, int pointer)
1859{
1860 int sign = 0;
1861 char *ref;
1862 int len;
1863
1864 len = strlen(type);
1865
1866 if (pointer) {
1867
1868 if (type[len-1] != '*') {
1869 do_warning("pointer expected with non pointer type");
1870 return val;
1871 }
1872
1873 ref = malloc_or_die(len);
1874 memcpy(ref, type, len);
1875
1876 /* chop off the " *" */
1877 ref[len - 2] = 0;
1878
1879 val = eval_type_str(val, ref, 0);
1880 free(ref);
1881 return val;
1882 }
1883
1884 /* check if this is a pointer */
1885 if (type[len - 1] == '*')
1886 return val;
1887
1888 /* Try to figure out the arg size*/
1889 if (strncmp(type, "struct", 6) == 0)
1890 /* all bets off */
1891 return val;
1892
1893 if (strcmp(type, "u8") == 0)
1894 return val & 0xff;
1895
1896 if (strcmp(type, "u16") == 0)
1897 return val & 0xffff;
1898
1899 if (strcmp(type, "u32") == 0)
1900 return val & 0xffffffff;
1901
1902 if (strcmp(type, "u64") == 0 ||
1903 strcmp(type, "s64"))
1904 return val;
1905
1906 if (strcmp(type, "s8") == 0)
1907 return (unsigned long long)(char)val & 0xff;
1908
1909 if (strcmp(type, "s16") == 0)
1910 return (unsigned long long)(short)val & 0xffff;
1911
1912 if (strcmp(type, "s32") == 0)
1913 return (unsigned long long)(int)val & 0xffffffff;
1914
1915 if (strncmp(type, "unsigned ", 9) == 0) {
1916 sign = 0;
1917 type += 9;
1918 }
1919
1920 if (strcmp(type, "char") == 0) {
1921 if (sign)
1922 return (unsigned long long)(char)val & 0xff;
1923 else
1924 return val & 0xff;
1925 }
1926
1927 if (strcmp(type, "short") == 0) {
1928 if (sign)
1929 return (unsigned long long)(short)val & 0xffff;
1930 else
1931 return val & 0xffff;
1932 }
1933
1934 if (strcmp(type, "int") == 0) {
1935 if (sign)
1936 return (unsigned long long)(int)val & 0xffffffff;
1937 else
1938 return val & 0xffffffff;
1939 }
1940
1941 return val;
1942}
1943
1944/*
1945 * Try to figure out the type.
1946 */
1947static unsigned long long
1948eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1949{
1950 if (arg->type != PRINT_TYPE)
1951 die("expected type argument");
1952
1953 return eval_type_str(val, arg->typecast.type, pointer);
1954}
1955
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001956static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001957{
1958 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001959 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001960
1961 switch (arg->type) {
1962 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001963 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001964 break;
1965 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001966 ret = arg_num_eval(arg->typecast.item, val);
1967 if (!ret)
1968 break;
1969 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001970 break;
1971 case PRINT_OP:
1972 switch (arg->op.op[0]) {
1973 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001974 ret = arg_num_eval(arg->op.left, &left);
1975 if (!ret)
1976 break;
1977 ret = arg_num_eval(arg->op.right, &right);
1978 if (!ret)
1979 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001980 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001981 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001982 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001983 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001984 break;
1985 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001986 ret = arg_num_eval(arg->op.left, &left);
1987 if (!ret)
1988 break;
1989 ret = arg_num_eval(arg->op.right, &right);
1990 if (!ret)
1991 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001992 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001993 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001994 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001995 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001996 break;
1997 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001998 ret = arg_num_eval(arg->op.left, &left);
1999 if (!ret)
2000 break;
2001 ret = arg_num_eval(arg->op.right, &right);
2002 if (!ret)
2003 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002004 switch (arg->op.op[1]) {
2005 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002006 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002007 break;
2008 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002009 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002010 break;
2011 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002012 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002013 break;
2014 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002015 do_warning("unknown op '%s'", arg->op.op);
2016 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002017 }
2018 break;
2019 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002020 ret = arg_num_eval(arg->op.left, &left);
2021 if (!ret)
2022 break;
2023 ret = arg_num_eval(arg->op.right, &right);
2024 if (!ret)
2025 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002026 switch (arg->op.op[1]) {
2027 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002028 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002029 break;
2030 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002031 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002032 break;
2033 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002034 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002035 break;
2036 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002037 do_warning("unknown op '%s'", arg->op.op);
2038 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002039 }
2040 break;
2041 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002042 ret = arg_num_eval(arg->op.left, &left);
2043 if (!ret)
2044 break;
2045 ret = arg_num_eval(arg->op.right, &right);
2046 if (!ret)
2047 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002048
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002049 if (arg->op.op[1] != '=') {
2050 do_warning("unknown op '%s'", arg->op.op);
2051 ret = 0;
2052 } else
2053 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002054 break;
2055 case '!':
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;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002062
2063 switch (arg->op.op[1]) {
2064 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002065 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002066 break;
2067 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002068 do_warning("unknown op '%s'", arg->op.op);
2069 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002070 }
2071 break;
2072 case '-':
2073 /* check for negative */
2074 if (arg->op.left->type == PRINT_NULL)
2075 left = 0;
2076 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002077 ret = arg_num_eval(arg->op.left, &left);
2078 if (!ret)
2079 break;
2080 ret = arg_num_eval(arg->op.right, &right);
2081 if (!ret)
2082 break;
2083 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002084 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002085 case '+':
2086 if (arg->op.left->type == PRINT_NULL)
2087 left = 0;
2088 else
2089 ret = arg_num_eval(arg->op.left, &left);
2090 if (!ret)
2091 break;
2092 ret = arg_num_eval(arg->op.right, &right);
2093 if (!ret)
2094 break;
2095 *val = left + right;
2096 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002097 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002098 do_warning("unknown op '%s'", arg->op.op);
2099 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002100 }
2101 break;
2102
2103 case PRINT_NULL:
2104 case PRINT_FIELD ... PRINT_SYMBOL:
2105 case PRINT_STRING:
2106 case PRINT_BSTRING:
2107 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002108 do_warning("invalid eval type %d", arg->type);
2109 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002110
2111 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002112 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002113}
2114
2115static char *arg_eval (struct print_arg *arg)
2116{
2117 long long val;
2118 static char buf[20];
2119
2120 switch (arg->type) {
2121 case PRINT_ATOM:
2122 return arg->atom.atom;
2123 case PRINT_TYPE:
2124 return arg_eval(arg->typecast.item);
2125 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002126 if (!arg_num_eval(arg, &val))
2127 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002128 sprintf(buf, "%lld", val);
2129 return buf;
2130
2131 case PRINT_NULL:
2132 case PRINT_FIELD ... PRINT_SYMBOL:
2133 case PRINT_STRING:
2134 case PRINT_BSTRING:
2135 default:
2136 die("invalid eval type %d", arg->type);
2137 break;
2138 }
2139
2140 return NULL;
2141}
2142
2143static enum event_type
2144process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2145{
2146 enum event_type type;
2147 struct print_arg *arg = NULL;
2148 struct print_flag_sym *field;
2149 char *token = *tok;
2150 char *value;
2151
2152 do {
2153 free_token(token);
2154 type = read_token_item(&token);
2155 if (test_type_token(type, token, EVENT_OP, "{"))
2156 break;
2157
2158 arg = alloc_arg();
2159
2160 free_token(token);
2161 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002162
2163 if (type == EVENT_OP)
2164 type = process_op(event, arg, &token);
2165
2166 if (type == EVENT_ERROR)
2167 goto out_free;
2168
Steven Rostedtf7d82352012-04-06 00:47:53 +02002169 if (test_type_token(type, token, EVENT_DELIM, ","))
2170 goto out_free;
2171
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002172 field = calloc(1, sizeof(*field));
2173 if (!field)
2174 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002175
2176 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002177 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002178 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002179 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002180 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002181 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002182
2183 free_arg(arg);
2184 arg = alloc_arg();
2185
2186 free_token(token);
2187 type = process_arg(event, arg, &token);
2188 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002189 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002190
2191 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002192 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002193 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002194 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002195 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002196 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002197 free_arg(arg);
2198 arg = NULL;
2199
2200 *list = field;
2201 list = &field->next;
2202
2203 free_token(token);
2204 type = read_token_item(&token);
2205 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2206
2207 *tok = token;
2208 return type;
2209
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002210out_free_field:
2211 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002212out_free:
2213 free_arg(arg);
2214 free_token(token);
2215 *tok = NULL;
2216
2217 return EVENT_ERROR;
2218}
2219
2220static enum event_type
2221process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2222{
2223 struct print_arg *field;
2224 enum event_type type;
2225 char *token;
2226
2227 memset(arg, 0, sizeof(*arg));
2228 arg->type = PRINT_FLAGS;
2229
2230 field = alloc_arg();
2231
2232 type = process_arg(event, field, &token);
2233
2234 /* Handle operations in the first argument */
2235 while (type == EVENT_OP)
2236 type = process_op(event, field, &token);
2237
2238 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002239 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002240 free_token(token);
2241
2242 arg->flags.field = field;
2243
2244 type = read_token_item(&token);
2245 if (event_item_type(type)) {
2246 arg->flags.delim = token;
2247 type = read_token_item(&token);
2248 }
2249
2250 if (test_type_token(type, token, EVENT_DELIM, ","))
2251 goto out_free;
2252
2253 type = process_fields(event, &arg->flags.flags, &token);
2254 if (test_type_token(type, token, EVENT_DELIM, ")"))
2255 goto out_free;
2256
2257 free_token(token);
2258 type = read_token_item(tok);
2259 return type;
2260
Namhyung Kim70d93042012-09-19 15:58:44 +09002261out_free_field:
2262 free_arg(field);
2263out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002264 free_token(token);
2265 *tok = NULL;
2266 return EVENT_ERROR;
2267}
2268
2269static enum event_type
2270process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2271{
2272 struct print_arg *field;
2273 enum event_type type;
2274 char *token;
2275
2276 memset(arg, 0, sizeof(*arg));
2277 arg->type = PRINT_SYMBOL;
2278
2279 field = alloc_arg();
2280
2281 type = process_arg(event, field, &token);
2282 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002283 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002284
2285 arg->symbol.field = field;
2286
2287 type = process_fields(event, &arg->symbol.symbols, &token);
2288 if (test_type_token(type, token, EVENT_DELIM, ")"))
2289 goto out_free;
2290
2291 free_token(token);
2292 type = read_token_item(tok);
2293 return type;
2294
Namhyung Kim70d93042012-09-19 15:58:44 +09002295out_free_field:
2296 free_arg(field);
2297out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002298 free_token(token);
2299 *tok = NULL;
2300 return EVENT_ERROR;
2301}
2302
2303static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002304process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2305{
2306 struct print_arg *field;
2307 enum event_type type;
2308 char *token;
2309
2310 memset(arg, 0, sizeof(*arg));
2311 arg->type = PRINT_HEX;
2312
2313 field = alloc_arg();
2314 type = process_arg(event, field, &token);
2315
2316 if (test_type_token(type, token, EVENT_DELIM, ","))
2317 goto out_free;
2318
2319 arg->hex.field = field;
2320
2321 free_token(token);
2322
2323 field = alloc_arg();
2324 type = process_arg(event, field, &token);
2325
2326 if (test_type_token(type, token, EVENT_DELIM, ")"))
2327 goto out_free;
2328
2329 arg->hex.size = field;
2330
2331 free_token(token);
2332 type = read_token_item(tok);
2333 return type;
2334
2335 out_free:
2336 free_arg(field);
2337 free_token(token);
2338 *tok = NULL;
2339 return EVENT_ERROR;
2340}
2341
2342static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002343process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2344{
2345 struct format_field *field;
2346 enum event_type type;
2347 char *token;
2348
2349 memset(arg, 0, sizeof(*arg));
2350 arg->type = PRINT_DYNAMIC_ARRAY;
2351
2352 /*
2353 * The item within the parenthesis is another field that holds
2354 * the index into where the array starts.
2355 */
2356 type = read_token(&token);
2357 *tok = token;
2358 if (type != EVENT_ITEM)
2359 goto out_free;
2360
2361 /* Find the field */
2362
2363 field = pevent_find_field(event, token);
2364 if (!field)
2365 goto out_free;
2366
2367 arg->dynarray.field = field;
2368 arg->dynarray.index = 0;
2369
2370 if (read_expected(EVENT_DELIM, ")") < 0)
2371 goto out_free;
2372
2373 free_token(token);
2374 type = read_token_item(&token);
2375 *tok = token;
2376 if (type != EVENT_OP || strcmp(token, "[") != 0)
2377 return type;
2378
2379 free_token(token);
2380 arg = alloc_arg();
2381 type = process_arg(event, arg, &token);
2382 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002383 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002384
2385 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002386 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002387
2388 free_token(token);
2389 type = read_token_item(tok);
2390 return type;
2391
Namhyung Kimb3511d02012-05-23 11:36:50 +09002392 out_free_arg:
2393 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002394 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002395 free_token(token);
2396 *tok = NULL;
2397 return EVENT_ERROR;
2398}
2399
2400static enum event_type
2401process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2402{
2403 struct print_arg *item_arg;
2404 enum event_type type;
2405 char *token;
2406
2407 type = process_arg(event, arg, &token);
2408
2409 if (type == EVENT_ERROR)
2410 goto out_free;
2411
2412 if (type == EVENT_OP)
2413 type = process_op(event, arg, &token);
2414
2415 if (type == EVENT_ERROR)
2416 goto out_free;
2417
2418 if (test_type_token(type, token, EVENT_DELIM, ")"))
2419 goto out_free;
2420
2421 free_token(token);
2422 type = read_token_item(&token);
2423
2424 /*
2425 * If the next token is an item or another open paren, then
2426 * this was a typecast.
2427 */
2428 if (event_item_type(type) ||
2429 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2430
2431 /* make this a typecast and contine */
2432
2433 /* prevous must be an atom */
2434 if (arg->type != PRINT_ATOM)
2435 die("previous needed to be PRINT_ATOM");
2436
2437 item_arg = alloc_arg();
2438
2439 arg->type = PRINT_TYPE;
2440 arg->typecast.type = arg->atom.atom;
2441 arg->typecast.item = item_arg;
2442 type = process_arg_token(event, item_arg, &token, type);
2443
2444 }
2445
2446 *tok = token;
2447 return type;
2448
2449 out_free:
2450 free_token(token);
2451 *tok = NULL;
2452 return EVENT_ERROR;
2453}
2454
2455
2456static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002457process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2458 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002459{
2460 enum event_type type;
2461 char *token;
2462
2463 if (read_expect_type(EVENT_ITEM, &token) < 0)
2464 goto out_free;
2465
2466 arg->type = PRINT_STRING;
2467 arg->string.string = token;
2468 arg->string.offset = -1;
2469
2470 if (read_expected(EVENT_DELIM, ")") < 0)
2471 goto out_err;
2472
2473 type = read_token(&token);
2474 *tok = token;
2475
2476 return type;
2477
2478 out_free:
2479 free_token(token);
2480 out_err:
2481 *tok = NULL;
2482 return EVENT_ERROR;
2483}
2484
2485static struct pevent_function_handler *
2486find_func_handler(struct pevent *pevent, char *func_name)
2487{
2488 struct pevent_function_handler *func;
2489
2490 for (func = pevent->func_handlers; func; func = func->next) {
2491 if (strcmp(func->name, func_name) == 0)
2492 break;
2493 }
2494
2495 return func;
2496}
2497
2498static void remove_func_handler(struct pevent *pevent, char *func_name)
2499{
2500 struct pevent_function_handler *func;
2501 struct pevent_function_handler **next;
2502
2503 next = &pevent->func_handlers;
2504 while ((func = *next)) {
2505 if (strcmp(func->name, func_name) == 0) {
2506 *next = func->next;
2507 free_func_handle(func);
2508 break;
2509 }
2510 next = &func->next;
2511 }
2512}
2513
2514static enum event_type
2515process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2516 struct print_arg *arg, char **tok)
2517{
2518 struct print_arg **next_arg;
2519 struct print_arg *farg;
2520 enum event_type type;
2521 char *token;
2522 char *test;
2523 int i;
2524
2525 arg->type = PRINT_FUNC;
2526 arg->func.func = func;
2527
2528 *tok = NULL;
2529
2530 next_arg = &(arg->func.args);
2531 for (i = 0; i < func->nr_args; i++) {
2532 farg = alloc_arg();
2533 type = process_arg(event, farg, &token);
2534 if (i < (func->nr_args - 1))
2535 test = ",";
2536 else
2537 test = ")";
2538
2539 if (test_type_token(type, token, EVENT_DELIM, test)) {
2540 free_arg(farg);
2541 free_token(token);
2542 return EVENT_ERROR;
2543 }
2544
2545 *next_arg = farg;
2546 next_arg = &(farg->next);
2547 free_token(token);
2548 }
2549
2550 type = read_token(&token);
2551 *tok = token;
2552
2553 return type;
2554}
2555
2556static enum event_type
2557process_function(struct event_format *event, struct print_arg *arg,
2558 char *token, char **tok)
2559{
2560 struct pevent_function_handler *func;
2561
2562 if (strcmp(token, "__print_flags") == 0) {
2563 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002564 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002565 return process_flags(event, arg, tok);
2566 }
2567 if (strcmp(token, "__print_symbolic") == 0) {
2568 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002569 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002570 return process_symbols(event, arg, tok);
2571 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002572 if (strcmp(token, "__print_hex") == 0) {
2573 free_token(token);
2574 return process_hex(event, arg, tok);
2575 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002576 if (strcmp(token, "__get_str") == 0) {
2577 free_token(token);
2578 return process_str(event, arg, tok);
2579 }
2580 if (strcmp(token, "__get_dynamic_array") == 0) {
2581 free_token(token);
2582 return process_dynamic_array(event, arg, tok);
2583 }
2584
2585 func = find_func_handler(event->pevent, token);
2586 if (func) {
2587 free_token(token);
2588 return process_func_handler(event, func, arg, tok);
2589 }
2590
2591 do_warning("function %s not defined", token);
2592 free_token(token);
2593 return EVENT_ERROR;
2594}
2595
2596static enum event_type
2597process_arg_token(struct event_format *event, struct print_arg *arg,
2598 char **tok, enum event_type type)
2599{
2600 char *token;
2601 char *atom;
2602
2603 token = *tok;
2604
2605 switch (type) {
2606 case EVENT_ITEM:
2607 if (strcmp(token, "REC") == 0) {
2608 free_token(token);
2609 type = process_entry(event, arg, &token);
2610 break;
2611 }
2612 atom = token;
2613 /* test the next token */
2614 type = read_token_item(&token);
2615
2616 /*
2617 * If the next token is a parenthesis, then this
2618 * is a function.
2619 */
2620 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2621 free_token(token);
2622 token = NULL;
2623 /* this will free atom. */
2624 type = process_function(event, arg, atom, &token);
2625 break;
2626 }
2627 /* atoms can be more than one token long */
2628 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002629 char *new_atom;
2630 new_atom = realloc(atom,
2631 strlen(atom) + strlen(token) + 2);
2632 if (!new_atom) {
2633 free(atom);
2634 *tok = NULL;
2635 free_token(token);
2636 return EVENT_ERROR;
2637 }
2638 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002639 strcat(atom, " ");
2640 strcat(atom, token);
2641 free_token(token);
2642 type = read_token_item(&token);
2643 }
2644
2645 arg->type = PRINT_ATOM;
2646 arg->atom.atom = atom;
2647 break;
2648
2649 case EVENT_DQUOTE:
2650 case EVENT_SQUOTE:
2651 arg->type = PRINT_ATOM;
2652 arg->atom.atom = token;
2653 type = read_token_item(&token);
2654 break;
2655 case EVENT_DELIM:
2656 if (strcmp(token, "(") == 0) {
2657 free_token(token);
2658 type = process_paren(event, arg, &token);
2659 break;
2660 }
2661 case EVENT_OP:
2662 /* handle single ops */
2663 arg->type = PRINT_OP;
2664 arg->op.op = token;
2665 arg->op.left = NULL;
2666 type = process_op(event, arg, &token);
2667
2668 /* On error, the op is freed */
2669 if (type == EVENT_ERROR)
2670 arg->op.op = NULL;
2671
2672 /* return error type if errored */
2673 break;
2674
2675 case EVENT_ERROR ... EVENT_NEWLINE:
2676 default:
2677 die("unexpected type %d", type);
2678 }
2679 *tok = token;
2680
2681 return type;
2682}
2683
2684static int event_read_print_args(struct event_format *event, struct print_arg **list)
2685{
2686 enum event_type type = EVENT_ERROR;
2687 struct print_arg *arg;
2688 char *token;
2689 int args = 0;
2690
2691 do {
2692 if (type == EVENT_NEWLINE) {
2693 type = read_token_item(&token);
2694 continue;
2695 }
2696
2697 arg = alloc_arg();
2698
2699 type = process_arg(event, arg, &token);
2700
2701 if (type == EVENT_ERROR) {
2702 free_token(token);
2703 free_arg(arg);
2704 return -1;
2705 }
2706
2707 *list = arg;
2708 args++;
2709
2710 if (type == EVENT_OP) {
2711 type = process_op(event, arg, &token);
2712 free_token(token);
2713 if (type == EVENT_ERROR) {
2714 *list = NULL;
2715 free_arg(arg);
2716 return -1;
2717 }
2718 list = &arg->next;
2719 continue;
2720 }
2721
2722 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2723 free_token(token);
2724 *list = arg;
2725 list = &arg->next;
2726 continue;
2727 }
2728 break;
2729 } while (type != EVENT_NONE);
2730
2731 if (type != EVENT_NONE && type != EVENT_ERROR)
2732 free_token(token);
2733
2734 return args;
2735}
2736
2737static int event_read_print(struct event_format *event)
2738{
2739 enum event_type type;
2740 char *token;
2741 int ret;
2742
2743 if (read_expected_item(EVENT_ITEM, "print") < 0)
2744 return -1;
2745
2746 if (read_expected(EVENT_ITEM, "fmt") < 0)
2747 return -1;
2748
2749 if (read_expected(EVENT_OP, ":") < 0)
2750 return -1;
2751
2752 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2753 goto fail;
2754
2755 concat:
2756 event->print_fmt.format = token;
2757 event->print_fmt.args = NULL;
2758
2759 /* ok to have no arg */
2760 type = read_token_item(&token);
2761
2762 if (type == EVENT_NONE)
2763 return 0;
2764
2765 /* Handle concatenation of print lines */
2766 if (type == EVENT_DQUOTE) {
2767 char *cat;
2768
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03002769 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2770 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002771 free_token(token);
2772 free_token(event->print_fmt.format);
2773 event->print_fmt.format = NULL;
2774 token = cat;
2775 goto concat;
2776 }
2777
2778 if (test_type_token(type, token, EVENT_DELIM, ","))
2779 goto fail;
2780
2781 free_token(token);
2782
2783 ret = event_read_print_args(event, &event->print_fmt.args);
2784 if (ret < 0)
2785 return -1;
2786
2787 return ret;
2788
2789 fail:
2790 free_token(token);
2791 return -1;
2792}
2793
2794/**
2795 * pevent_find_common_field - return a common field by event
2796 * @event: handle for the event
2797 * @name: the name of the common field to return
2798 *
2799 * Returns a common field from the event by the given @name.
2800 * This only searchs the common fields and not all field.
2801 */
2802struct format_field *
2803pevent_find_common_field(struct event_format *event, const char *name)
2804{
2805 struct format_field *format;
2806
2807 for (format = event->format.common_fields;
2808 format; format = format->next) {
2809 if (strcmp(format->name, name) == 0)
2810 break;
2811 }
2812
2813 return format;
2814}
2815
2816/**
2817 * pevent_find_field - find a non-common field
2818 * @event: handle for the event
2819 * @name: the name of the non-common field
2820 *
2821 * Returns a non-common field by the given @name.
2822 * This does not search common fields.
2823 */
2824struct format_field *
2825pevent_find_field(struct event_format *event, const char *name)
2826{
2827 struct format_field *format;
2828
2829 for (format = event->format.fields;
2830 format; format = format->next) {
2831 if (strcmp(format->name, name) == 0)
2832 break;
2833 }
2834
2835 return format;
2836}
2837
2838/**
2839 * pevent_find_any_field - find any field by name
2840 * @event: handle for the event
2841 * @name: the name of the field
2842 *
2843 * Returns a field by the given @name.
2844 * This searchs the common field names first, then
2845 * the non-common ones if a common one was not found.
2846 */
2847struct format_field *
2848pevent_find_any_field(struct event_format *event, const char *name)
2849{
2850 struct format_field *format;
2851
2852 format = pevent_find_common_field(event, name);
2853 if (format)
2854 return format;
2855 return pevent_find_field(event, name);
2856}
2857
2858/**
2859 * pevent_read_number - read a number from data
2860 * @pevent: handle for the pevent
2861 * @ptr: the raw data
2862 * @size: the size of the data that holds the number
2863 *
2864 * Returns the number (converted to host) from the
2865 * raw data.
2866 */
2867unsigned long long pevent_read_number(struct pevent *pevent,
2868 const void *ptr, int size)
2869{
2870 switch (size) {
2871 case 1:
2872 return *(unsigned char *)ptr;
2873 case 2:
2874 return data2host2(pevent, ptr);
2875 case 4:
2876 return data2host4(pevent, ptr);
2877 case 8:
2878 return data2host8(pevent, ptr);
2879 default:
2880 /* BUG! */
2881 return 0;
2882 }
2883}
2884
2885/**
2886 * pevent_read_number_field - read a number from data
2887 * @field: a handle to the field
2888 * @data: the raw data to read
2889 * @value: the value to place the number in
2890 *
2891 * Reads raw data according to a field offset and size,
2892 * and translates it into @value.
2893 *
2894 * Returns 0 on success, -1 otherwise.
2895 */
2896int pevent_read_number_field(struct format_field *field, const void *data,
2897 unsigned long long *value)
2898{
2899 if (!field)
2900 return -1;
2901 switch (field->size) {
2902 case 1:
2903 case 2:
2904 case 4:
2905 case 8:
2906 *value = pevent_read_number(field->event->pevent,
2907 data + field->offset, field->size);
2908 return 0;
2909 default:
2910 return -1;
2911 }
2912}
2913
2914static int get_common_info(struct pevent *pevent,
2915 const char *type, int *offset, int *size)
2916{
2917 struct event_format *event;
2918 struct format_field *field;
2919
2920 /*
2921 * All events should have the same common elements.
2922 * Pick any event to find where the type is;
2923 */
2924 if (!pevent->events)
2925 die("no event_list!");
2926
2927 event = pevent->events[0];
2928 field = pevent_find_common_field(event, type);
2929 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09002930 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002931
2932 *offset = field->offset;
2933 *size = field->size;
2934
2935 return 0;
2936}
2937
2938static int __parse_common(struct pevent *pevent, void *data,
2939 int *size, int *offset, const char *name)
2940{
2941 int ret;
2942
2943 if (!*size) {
2944 ret = get_common_info(pevent, name, offset, size);
2945 if (ret < 0)
2946 return ret;
2947 }
2948 return pevent_read_number(pevent, data + *offset, *size);
2949}
2950
2951static int trace_parse_common_type(struct pevent *pevent, void *data)
2952{
2953 return __parse_common(pevent, data,
2954 &pevent->type_size, &pevent->type_offset,
2955 "common_type");
2956}
2957
2958static int parse_common_pid(struct pevent *pevent, void *data)
2959{
2960 return __parse_common(pevent, data,
2961 &pevent->pid_size, &pevent->pid_offset,
2962 "common_pid");
2963}
2964
2965static int parse_common_pc(struct pevent *pevent, void *data)
2966{
2967 return __parse_common(pevent, data,
2968 &pevent->pc_size, &pevent->pc_offset,
2969 "common_preempt_count");
2970}
2971
2972static int parse_common_flags(struct pevent *pevent, void *data)
2973{
2974 return __parse_common(pevent, data,
2975 &pevent->flags_size, &pevent->flags_offset,
2976 "common_flags");
2977}
2978
2979static int parse_common_lock_depth(struct pevent *pevent, void *data)
2980{
Steven Rostedt0866a972012-05-22 14:52:40 +09002981 return __parse_common(pevent, data,
2982 &pevent->ld_size, &pevent->ld_offset,
2983 "common_lock_depth");
2984}
Steven Rostedtf7d82352012-04-06 00:47:53 +02002985
Steven Rostedt0866a972012-05-22 14:52:40 +09002986static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2987{
2988 return __parse_common(pevent, data,
2989 &pevent->ld_size, &pevent->ld_offset,
2990 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02002991}
2992
2993static int events_id_cmp(const void *a, const void *b);
2994
2995/**
2996 * pevent_find_event - find an event by given id
2997 * @pevent: a handle to the pevent
2998 * @id: the id of the event
2999 *
3000 * Returns an event that has a given @id.
3001 */
3002struct event_format *pevent_find_event(struct pevent *pevent, int id)
3003{
3004 struct event_format **eventptr;
3005 struct event_format key;
3006 struct event_format *pkey = &key;
3007
3008 /* Check cache first */
3009 if (pevent->last_event && pevent->last_event->id == id)
3010 return pevent->last_event;
3011
3012 key.id = id;
3013
3014 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3015 sizeof(*pevent->events), events_id_cmp);
3016
3017 if (eventptr) {
3018 pevent->last_event = *eventptr;
3019 return *eventptr;
3020 }
3021
3022 return NULL;
3023}
3024
3025/**
3026 * pevent_find_event_by_name - find an event by given name
3027 * @pevent: a handle to the pevent
3028 * @sys: the system name to search for
3029 * @name: the name of the event to search for
3030 *
3031 * This returns an event with a given @name and under the system
3032 * @sys. If @sys is NULL the first event with @name is returned.
3033 */
3034struct event_format *
3035pevent_find_event_by_name(struct pevent *pevent,
3036 const char *sys, const char *name)
3037{
3038 struct event_format *event;
3039 int i;
3040
3041 if (pevent->last_event &&
3042 strcmp(pevent->last_event->name, name) == 0 &&
3043 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3044 return pevent->last_event;
3045
3046 for (i = 0; i < pevent->nr_events; i++) {
3047 event = pevent->events[i];
3048 if (strcmp(event->name, name) == 0) {
3049 if (!sys)
3050 break;
3051 if (strcmp(event->system, sys) == 0)
3052 break;
3053 }
3054 }
3055 if (i == pevent->nr_events)
3056 event = NULL;
3057
3058 pevent->last_event = event;
3059 return event;
3060}
3061
3062static unsigned long long
3063eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3064{
3065 struct pevent *pevent = event->pevent;
3066 unsigned long long val = 0;
3067 unsigned long long left, right;
3068 struct print_arg *typearg = NULL;
3069 struct print_arg *larg;
3070 unsigned long offset;
3071 unsigned int field_size;
3072
3073 switch (arg->type) {
3074 case PRINT_NULL:
3075 /* ?? */
3076 return 0;
3077 case PRINT_ATOM:
3078 return strtoull(arg->atom.atom, NULL, 0);
3079 case PRINT_FIELD:
3080 if (!arg->field.field) {
3081 arg->field.field = pevent_find_any_field(event, arg->field.name);
3082 if (!arg->field.field)
3083 die("field %s not found", arg->field.name);
3084 }
3085 /* must be a number */
3086 val = pevent_read_number(pevent, data + arg->field.field->offset,
3087 arg->field.field->size);
3088 break;
3089 case PRINT_FLAGS:
3090 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003091 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003092 break;
3093 case PRINT_TYPE:
3094 val = eval_num_arg(data, size, event, arg->typecast.item);
3095 return eval_type(val, arg, 0);
3096 case PRINT_STRING:
3097 case PRINT_BSTRING:
3098 return 0;
3099 case PRINT_FUNC: {
3100 struct trace_seq s;
3101 trace_seq_init(&s);
3102 val = process_defined_func(&s, data, size, event, arg);
3103 trace_seq_destroy(&s);
3104 return val;
3105 }
3106 case PRINT_OP:
3107 if (strcmp(arg->op.op, "[") == 0) {
3108 /*
3109 * Arrays are special, since we don't want
3110 * to read the arg as is.
3111 */
3112 right = eval_num_arg(data, size, event, arg->op.right);
3113
3114 /* handle typecasts */
3115 larg = arg->op.left;
3116 while (larg->type == PRINT_TYPE) {
3117 if (!typearg)
3118 typearg = larg;
3119 larg = larg->typecast.item;
3120 }
3121
3122 /* Default to long size */
3123 field_size = pevent->long_size;
3124
3125 switch (larg->type) {
3126 case PRINT_DYNAMIC_ARRAY:
3127 offset = pevent_read_number(pevent,
3128 data + larg->dynarray.field->offset,
3129 larg->dynarray.field->size);
3130 if (larg->dynarray.field->elementsize)
3131 field_size = larg->dynarray.field->elementsize;
3132 /*
3133 * The actual length of the dynamic array is stored
3134 * in the top half of the field, and the offset
3135 * is in the bottom half of the 32 bit field.
3136 */
3137 offset &= 0xffff;
3138 offset += right;
3139 break;
3140 case PRINT_FIELD:
3141 if (!larg->field.field) {
3142 larg->field.field =
3143 pevent_find_any_field(event, larg->field.name);
3144 if (!larg->field.field)
3145 die("field %s not found", larg->field.name);
3146 }
3147 field_size = larg->field.field->elementsize;
3148 offset = larg->field.field->offset +
3149 right * larg->field.field->elementsize;
3150 break;
3151 default:
3152 goto default_op; /* oops, all bets off */
3153 }
3154 val = pevent_read_number(pevent,
3155 data + offset, field_size);
3156 if (typearg)
3157 val = eval_type(val, typearg, 1);
3158 break;
3159 } else if (strcmp(arg->op.op, "?") == 0) {
3160 left = eval_num_arg(data, size, event, arg->op.left);
3161 arg = arg->op.right;
3162 if (left)
3163 val = eval_num_arg(data, size, event, arg->op.left);
3164 else
3165 val = eval_num_arg(data, size, event, arg->op.right);
3166 break;
3167 }
3168 default_op:
3169 left = eval_num_arg(data, size, event, arg->op.left);
3170 right = eval_num_arg(data, size, event, arg->op.right);
3171 switch (arg->op.op[0]) {
3172 case '!':
3173 switch (arg->op.op[1]) {
3174 case 0:
3175 val = !right;
3176 break;
3177 case '=':
3178 val = left != right;
3179 break;
3180 default:
3181 die("unknown op '%s'", arg->op.op);
3182 }
3183 break;
3184 case '~':
3185 val = ~right;
3186 break;
3187 case '|':
3188 if (arg->op.op[1])
3189 val = left || right;
3190 else
3191 val = left | right;
3192 break;
3193 case '&':
3194 if (arg->op.op[1])
3195 val = left && right;
3196 else
3197 val = left & right;
3198 break;
3199 case '<':
3200 switch (arg->op.op[1]) {
3201 case 0:
3202 val = left < right;
3203 break;
3204 case '<':
3205 val = left << right;
3206 break;
3207 case '=':
3208 val = left <= right;
3209 break;
3210 default:
3211 die("unknown op '%s'", arg->op.op);
3212 }
3213 break;
3214 case '>':
3215 switch (arg->op.op[1]) {
3216 case 0:
3217 val = left > right;
3218 break;
3219 case '>':
3220 val = left >> right;
3221 break;
3222 case '=':
3223 val = left >= right;
3224 break;
3225 default:
3226 die("unknown op '%s'", arg->op.op);
3227 }
3228 break;
3229 case '=':
3230 if (arg->op.op[1] != '=')
3231 die("unknown op '%s'", arg->op.op);
3232 val = left == right;
3233 break;
3234 case '-':
3235 val = left - right;
3236 break;
3237 case '+':
3238 val = left + right;
3239 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003240 case '/':
3241 val = left / right;
3242 break;
3243 case '*':
3244 val = left * right;
3245 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003246 default:
3247 die("unknown op '%s'", arg->op.op);
3248 }
3249 break;
3250 default: /* not sure what to do there */
3251 return 0;
3252 }
3253 return val;
3254}
3255
3256struct flag {
3257 const char *name;
3258 unsigned long long value;
3259};
3260
3261static const struct flag flags[] = {
3262 { "HI_SOFTIRQ", 0 },
3263 { "TIMER_SOFTIRQ", 1 },
3264 { "NET_TX_SOFTIRQ", 2 },
3265 { "NET_RX_SOFTIRQ", 3 },
3266 { "BLOCK_SOFTIRQ", 4 },
3267 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3268 { "TASKLET_SOFTIRQ", 6 },
3269 { "SCHED_SOFTIRQ", 7 },
3270 { "HRTIMER_SOFTIRQ", 8 },
3271 { "RCU_SOFTIRQ", 9 },
3272
3273 { "HRTIMER_NORESTART", 0 },
3274 { "HRTIMER_RESTART", 1 },
3275};
3276
3277static unsigned long long eval_flag(const char *flag)
3278{
3279 int i;
3280
3281 /*
3282 * Some flags in the format files do not get converted.
3283 * If the flag is not numeric, see if it is something that
3284 * we already know about.
3285 */
3286 if (isdigit(flag[0]))
3287 return strtoull(flag, NULL, 0);
3288
3289 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3290 if (strcmp(flags[i].name, flag) == 0)
3291 return flags[i].value;
3292
3293 return 0;
3294}
3295
3296static void print_str_to_seq(struct trace_seq *s, const char *format,
3297 int len_arg, const char *str)
3298{
3299 if (len_arg >= 0)
3300 trace_seq_printf(s, format, len_arg, str);
3301 else
3302 trace_seq_printf(s, format, str);
3303}
3304
3305static void print_str_arg(struct trace_seq *s, void *data, int size,
3306 struct event_format *event, const char *format,
3307 int len_arg, struct print_arg *arg)
3308{
3309 struct pevent *pevent = event->pevent;
3310 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003311 struct format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003312 unsigned long long val, fval;
3313 unsigned long addr;
3314 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003315 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003316 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003317 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003318
3319 switch (arg->type) {
3320 case PRINT_NULL:
3321 /* ?? */
3322 return;
3323 case PRINT_ATOM:
3324 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3325 return;
3326 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003327 field = arg->field.field;
3328 if (!field) {
3329 field = pevent_find_any_field(event, arg->field.name);
3330 if (!field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003331 die("field %s not found", arg->field.name);
Namhyung Kimb7008072012-06-27 09:41:40 +09003332 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003333 }
3334 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003335 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003336
3337 /*
3338 * Some events pass in pointers. If this is not an array
3339 * and the size is the same as long_size, assume that it
3340 * is a pointer.
3341 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003342 if (!(field->flags & FIELD_IS_ARRAY) &&
3343 field->size == pevent->long_size) {
3344 addr = *(unsigned long *)(data + field->offset);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003345 trace_seq_printf(s, "%lx", addr);
3346 break;
3347 }
3348 str = malloc_or_die(len + 1);
Namhyung Kimb7008072012-06-27 09:41:40 +09003349 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003350 str[len] = 0;
3351 print_str_to_seq(s, format, len_arg, str);
3352 free(str);
3353 break;
3354 case PRINT_FLAGS:
3355 val = eval_num_arg(data, size, event, arg->flags.field);
3356 print = 0;
3357 for (flag = arg->flags.flags; flag; flag = flag->next) {
3358 fval = eval_flag(flag->value);
3359 if (!val && !fval) {
3360 print_str_to_seq(s, format, len_arg, flag->str);
3361 break;
3362 }
3363 if (fval && (val & fval) == fval) {
3364 if (print && arg->flags.delim)
3365 trace_seq_puts(s, arg->flags.delim);
3366 print_str_to_seq(s, format, len_arg, flag->str);
3367 print = 1;
3368 val &= ~fval;
3369 }
3370 }
3371 break;
3372 case PRINT_SYMBOL:
3373 val = eval_num_arg(data, size, event, arg->symbol.field);
3374 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3375 fval = eval_flag(flag->value);
3376 if (val == fval) {
3377 print_str_to_seq(s, format, len_arg, flag->str);
3378 break;
3379 }
3380 }
3381 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003382 case PRINT_HEX:
3383 field = arg->hex.field->field.field;
3384 if (!field) {
3385 str = arg->hex.field->field.name;
3386 field = pevent_find_any_field(event, str);
3387 if (!field)
3388 die("field %s not found", str);
3389 arg->hex.field->field.field = field;
3390 }
3391 hex = data + field->offset;
3392 len = eval_num_arg(data, size, event, arg->hex.size);
3393 for (i = 0; i < len; i++) {
3394 if (i)
3395 trace_seq_putc(s, ' ');
3396 trace_seq_printf(s, "%02x", hex[i]);
3397 }
3398 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003399
3400 case PRINT_TYPE:
3401 break;
3402 case PRINT_STRING: {
3403 int str_offset;
3404
3405 if (arg->string.offset == -1) {
3406 struct format_field *f;
3407
3408 f = pevent_find_any_field(event, arg->string.string);
3409 arg->string.offset = f->offset;
3410 }
3411 str_offset = data2host4(pevent, data + arg->string.offset);
3412 str_offset &= 0xffff;
3413 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3414 break;
3415 }
3416 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003417 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003418 break;
3419 case PRINT_OP:
3420 /*
3421 * The only op for string should be ? :
3422 */
3423 if (arg->op.op[0] != '?')
3424 return;
3425 val = eval_num_arg(data, size, event, arg->op.left);
3426 if (val)
3427 print_str_arg(s, data, size, event,
3428 format, len_arg, arg->op.right->op.left);
3429 else
3430 print_str_arg(s, data, size, event,
3431 format, len_arg, arg->op.right->op.right);
3432 break;
3433 case PRINT_FUNC:
3434 process_defined_func(s, data, size, event, arg);
3435 break;
3436 default:
3437 /* well... */
3438 break;
3439 }
3440}
3441
3442static unsigned long long
3443process_defined_func(struct trace_seq *s, void *data, int size,
3444 struct event_format *event, struct print_arg *arg)
3445{
3446 struct pevent_function_handler *func_handle = arg->func.func;
3447 struct pevent_func_params *param;
3448 unsigned long long *args;
3449 unsigned long long ret;
3450 struct print_arg *farg;
3451 struct trace_seq str;
3452 struct save_str {
3453 struct save_str *next;
3454 char *str;
3455 } *strings = NULL, *string;
3456 int i;
3457
3458 if (!func_handle->nr_args) {
3459 ret = (*func_handle->func)(s, NULL);
3460 goto out;
3461 }
3462
3463 farg = arg->func.args;
3464 param = func_handle->params;
3465
3466 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3467 for (i = 0; i < func_handle->nr_args; i++) {
3468 switch (param->type) {
3469 case PEVENT_FUNC_ARG_INT:
3470 case PEVENT_FUNC_ARG_LONG:
3471 case PEVENT_FUNC_ARG_PTR:
3472 args[i] = eval_num_arg(data, size, event, farg);
3473 break;
3474 case PEVENT_FUNC_ARG_STRING:
3475 trace_seq_init(&str);
3476 print_str_arg(&str, data, size, event, "%s", -1, farg);
3477 trace_seq_terminate(&str);
3478 string = malloc_or_die(sizeof(*string));
3479 string->next = strings;
3480 string->str = strdup(str.buffer);
Namhyung Kimca638582012-04-09 11:54:31 +09003481 if (!string->str)
3482 die("malloc str");
3483
Robert Richter0cf26012012-08-07 19:43:14 +02003484 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003485 strings = string;
3486 trace_seq_destroy(&str);
3487 break;
3488 default:
3489 /*
3490 * Something went totally wrong, this is not
3491 * an input error, something in this code broke.
3492 */
3493 die("Unexpected end of arguments\n");
3494 break;
3495 }
3496 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003497 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003498 }
3499
3500 ret = (*func_handle->func)(s, args);
3501 free(args);
3502 while (strings) {
3503 string = strings;
3504 strings = string->next;
3505 free(string->str);
3506 free(string);
3507 }
3508
3509 out:
3510 /* TBD : handle return type here */
3511 return ret;
3512}
3513
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003514static void free_args(struct print_arg *args)
3515{
3516 struct print_arg *next;
3517
3518 while (args) {
3519 next = args->next;
3520
3521 free_arg(args);
3522 args = next;
3523 }
3524}
3525
Steven Rostedtf7d82352012-04-06 00:47:53 +02003526static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3527{
3528 struct pevent *pevent = event->pevent;
3529 struct format_field *field, *ip_field;
3530 struct print_arg *args, *arg, **next;
3531 unsigned long long ip, val;
3532 char *ptr;
3533 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003534 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003535
3536 field = pevent->bprint_buf_field;
3537 ip_field = pevent->bprint_ip_field;
3538
3539 if (!field) {
3540 field = pevent_find_field(event, "buf");
3541 if (!field)
3542 die("can't find buffer field for binary printk");
3543 ip_field = pevent_find_field(event, "ip");
3544 if (!ip_field)
3545 die("can't find ip field for binary printk");
3546 pevent->bprint_buf_field = field;
3547 pevent->bprint_ip_field = ip_field;
3548 }
3549
3550 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3551
3552 /*
3553 * The first arg is the IP pointer.
3554 */
3555 args = alloc_arg();
3556 arg = args;
3557 arg->next = NULL;
3558 next = &arg->next;
3559
3560 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003561
3562 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3563 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003564
3565 /* skip the first "%pf : " */
3566 for (ptr = fmt + 6, bptr = data + field->offset;
3567 bptr < data + size && *ptr; ptr++) {
3568 int ls = 0;
3569
3570 if (*ptr == '%') {
3571 process_again:
3572 ptr++;
3573 switch (*ptr) {
3574 case '%':
3575 break;
3576 case 'l':
3577 ls++;
3578 goto process_again;
3579 case 'L':
3580 ls = 2;
3581 goto process_again;
3582 case '0' ... '9':
3583 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003584 case '.':
3585 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003586 case 'p':
3587 ls = 1;
3588 /* fall through */
3589 case 'd':
3590 case 'u':
3591 case 'x':
3592 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003593 switch (ls) {
3594 case 0:
3595 vsize = 4;
3596 break;
3597 case 1:
3598 vsize = pevent->long_size;
3599 break;
3600 case 2:
3601 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003602 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003603 default:
3604 vsize = ls; /* ? */
3605 break;
3606 }
3607 /* fall through */
3608 case '*':
3609 if (*ptr == '*')
3610 vsize = 4;
3611
Steven Rostedtf7d82352012-04-06 00:47:53 +02003612 /* the pointers are always 4 bytes aligned */
3613 bptr = (void *)(((unsigned long)bptr + 3) &
3614 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003615 val = pevent_read_number(pevent, bptr, vsize);
3616 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003617 arg = alloc_arg();
3618 arg->next = NULL;
3619 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003620 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3621 free(arg);
3622 goto out_free;
3623 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003624 *next = arg;
3625 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003626 /*
3627 * The '*' case means that an arg is used as the length.
3628 * We need to continue to figure out for what.
3629 */
3630 if (*ptr == '*')
3631 goto process_again;
3632
Steven Rostedtf7d82352012-04-06 00:47:53 +02003633 break;
3634 case 's':
3635 arg = alloc_arg();
3636 arg->next = NULL;
3637 arg->type = PRINT_BSTRING;
3638 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003639 if (!arg->string.string)
3640 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003641 bptr += strlen(bptr) + 1;
3642 *next = arg;
3643 next = &arg->next;
3644 default:
3645 break;
3646 }
3647 }
3648 }
3649
3650 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003651
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003652out_free:
3653 free_args(args);
3654 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003655}
3656
3657static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003658get_bprint_format(void *data, int size __maybe_unused,
3659 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003660{
3661 struct pevent *pevent = event->pevent;
3662 unsigned long long addr;
3663 struct format_field *field;
3664 struct printk_map *printk;
3665 char *format;
3666 char *p;
3667
3668 field = pevent->bprint_fmt_field;
3669
3670 if (!field) {
3671 field = pevent_find_field(event, "fmt");
3672 if (!field)
3673 die("can't find format field for binary printk");
3674 pevent->bprint_fmt_field = field;
3675 }
3676
3677 addr = pevent_read_number(pevent, data + field->offset, field->size);
3678
3679 printk = find_printk(pevent, addr);
3680 if (!printk) {
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003681 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3682 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003683 return format;
3684 }
3685
3686 p = printk->printk;
3687 /* Remove any quotes. */
3688 if (*p == '"')
3689 p++;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003690 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3691 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003692 /* remove ending quotes and new line since we will add one too */
3693 p = format + strlen(format) - 1;
3694 if (*p == '"')
3695 *p = 0;
3696
3697 p -= 2;
3698 if (strcmp(p, "\\n") == 0)
3699 *p = 0;
3700
3701 return format;
3702}
3703
3704static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3705 struct event_format *event, struct print_arg *arg)
3706{
3707 unsigned char *buf;
3708 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3709
3710 if (arg->type == PRINT_FUNC) {
3711 process_defined_func(s, data, size, event, arg);
3712 return;
3713 }
3714
3715 if (arg->type != PRINT_FIELD) {
3716 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3717 arg->type);
3718 return;
3719 }
3720
3721 if (mac == 'm')
3722 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3723 if (!arg->field.field) {
3724 arg->field.field =
3725 pevent_find_any_field(event, arg->field.name);
3726 if (!arg->field.field)
3727 die("field %s not found", arg->field.name);
3728 }
3729 if (arg->field.field->size != 6) {
3730 trace_seq_printf(s, "INVALIDMAC");
3731 return;
3732 }
3733 buf = data + arg->field.field->offset;
3734 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3735}
3736
Namhyung Kim600da3c2012-06-22 17:10:15 +09003737static int is_printable_array(char *p, unsigned int len)
3738{
3739 unsigned int i;
3740
3741 for (i = 0; i < len && p[i]; i++)
3742 if (!isprint(p[i]))
3743 return 0;
3744 return 1;
3745}
3746
Steven Rostedtf7d82352012-04-06 00:47:53 +02003747static void print_event_fields(struct trace_seq *s, void *data, int size,
3748 struct event_format *event)
3749{
3750 struct format_field *field;
3751 unsigned long long val;
3752 unsigned int offset, len, i;
3753
3754 field = event->format.fields;
3755 while (field) {
3756 trace_seq_printf(s, " %s=", field->name);
3757 if (field->flags & FIELD_IS_ARRAY) {
3758 offset = field->offset;
3759 len = field->size;
3760 if (field->flags & FIELD_IS_DYNAMIC) {
3761 val = pevent_read_number(event->pevent, data + offset, len);
3762 offset = val;
3763 len = offset >> 16;
3764 offset &= 0xffff;
3765 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09003766 if (field->flags & FIELD_IS_STRING &&
3767 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003768 trace_seq_printf(s, "%s", (char *)data + offset);
3769 } else {
3770 trace_seq_puts(s, "ARRAY[");
3771 for (i = 0; i < len; i++) {
3772 if (i)
3773 trace_seq_puts(s, ", ");
3774 trace_seq_printf(s, "%02x",
3775 *((unsigned char *)data + offset + i));
3776 }
3777 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09003778 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003779 }
3780 } else {
3781 val = pevent_read_number(event->pevent, data + field->offset,
3782 field->size);
3783 if (field->flags & FIELD_IS_POINTER) {
3784 trace_seq_printf(s, "0x%llx", val);
3785 } else if (field->flags & FIELD_IS_SIGNED) {
3786 switch (field->size) {
3787 case 4:
3788 /*
3789 * If field is long then print it in hex.
3790 * A long usually stores pointers.
3791 */
3792 if (field->flags & FIELD_IS_LONG)
3793 trace_seq_printf(s, "0x%x", (int)val);
3794 else
3795 trace_seq_printf(s, "%d", (int)val);
3796 break;
3797 case 2:
3798 trace_seq_printf(s, "%2d", (short)val);
3799 break;
3800 case 1:
3801 trace_seq_printf(s, "%1d", (char)val);
3802 break;
3803 default:
3804 trace_seq_printf(s, "%lld", val);
3805 }
3806 } else {
3807 if (field->flags & FIELD_IS_LONG)
3808 trace_seq_printf(s, "0x%llx", val);
3809 else
3810 trace_seq_printf(s, "%llu", val);
3811 }
3812 }
3813 field = field->next;
3814 }
3815}
3816
3817static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3818{
3819 struct pevent *pevent = event->pevent;
3820 struct print_fmt *print_fmt = &event->print_fmt;
3821 struct print_arg *arg = print_fmt->args;
3822 struct print_arg *args = NULL;
3823 const char *ptr = print_fmt->format;
3824 unsigned long long val;
3825 struct func_map *func;
3826 const char *saveptr;
3827 char *bprint_fmt = NULL;
3828 char format[32];
3829 int show_func;
3830 int len_as_arg;
3831 int len_arg;
3832 int len;
3833 int ls;
3834
3835 if (event->flags & EVENT_FL_FAILED) {
3836 trace_seq_printf(s, "[FAILED TO PARSE]");
3837 print_event_fields(s, data, size, event);
3838 return;
3839 }
3840
3841 if (event->flags & EVENT_FL_ISBPRINT) {
3842 bprint_fmt = get_bprint_format(data, size, event);
3843 args = make_bprint_args(bprint_fmt, data, size, event);
3844 arg = args;
3845 ptr = bprint_fmt;
3846 }
3847
3848 for (; *ptr; ptr++) {
3849 ls = 0;
3850 if (*ptr == '\\') {
3851 ptr++;
3852 switch (*ptr) {
3853 case 'n':
3854 trace_seq_putc(s, '\n');
3855 break;
3856 case 't':
3857 trace_seq_putc(s, '\t');
3858 break;
3859 case 'r':
3860 trace_seq_putc(s, '\r');
3861 break;
3862 case '\\':
3863 trace_seq_putc(s, '\\');
3864 break;
3865 default:
3866 trace_seq_putc(s, *ptr);
3867 break;
3868 }
3869
3870 } else if (*ptr == '%') {
3871 saveptr = ptr;
3872 show_func = 0;
3873 len_as_arg = 0;
3874 cont_process:
3875 ptr++;
3876 switch (*ptr) {
3877 case '%':
3878 trace_seq_putc(s, '%');
3879 break;
3880 case '#':
3881 /* FIXME: need to handle properly */
3882 goto cont_process;
3883 case 'h':
3884 ls--;
3885 goto cont_process;
3886 case 'l':
3887 ls++;
3888 goto cont_process;
3889 case 'L':
3890 ls = 2;
3891 goto cont_process;
3892 case '*':
3893 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003894 if (!arg) {
3895 do_warning("no argument match");
3896 event->flags |= EVENT_FL_FAILED;
3897 goto out_failed;
3898 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003899 len_arg = eval_num_arg(data, size, event, arg);
3900 len_as_arg = 1;
3901 arg = arg->next;
3902 goto cont_process;
3903 case '.':
3904 case 'z':
3905 case 'Z':
3906 case '0' ... '9':
3907 goto cont_process;
3908 case 'p':
3909 if (pevent->long_size == 4)
3910 ls = 1;
3911 else
3912 ls = 2;
3913
3914 if (*(ptr+1) == 'F' ||
3915 *(ptr+1) == 'f') {
3916 ptr++;
3917 show_func = *ptr;
3918 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3919 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3920 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05003921 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003922 break;
3923 }
3924
3925 /* fall through */
3926 case 'd':
3927 case 'i':
3928 case 'x':
3929 case 'X':
3930 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09003931 if (!arg) {
3932 do_warning("no argument match");
3933 event->flags |= EVENT_FL_FAILED;
3934 goto out_failed;
3935 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003936
3937 len = ((unsigned long)ptr + 1) -
3938 (unsigned long)saveptr;
3939
3940 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003941 if (len > 31) {
3942 do_warning("bad format!");
3943 event->flags |= EVENT_FL_FAILED;
3944 len = 31;
3945 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003946
3947 memcpy(format, saveptr, len);
3948 format[len] = 0;
3949
3950 val = eval_num_arg(data, size, event, arg);
3951 arg = arg->next;
3952
3953 if (show_func) {
3954 func = find_func(pevent, val);
3955 if (func) {
3956 trace_seq_puts(s, func->func);
3957 if (show_func == 'F')
3958 trace_seq_printf(s,
3959 "+0x%llx",
3960 val - func->addr);
3961 break;
3962 }
3963 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003964 if (pevent->long_size == 8 && ls &&
3965 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003966 char *p;
3967
3968 ls = 2;
3969 /* make %l into %ll */
3970 p = strchr(format, 'l');
3971 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003972 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003973 else if (strcmp(format, "%p") == 0)
3974 strcpy(format, "0x%llx");
3975 }
3976 switch (ls) {
3977 case -2:
3978 if (len_as_arg)
3979 trace_seq_printf(s, format, len_arg, (char)val);
3980 else
3981 trace_seq_printf(s, format, (char)val);
3982 break;
3983 case -1:
3984 if (len_as_arg)
3985 trace_seq_printf(s, format, len_arg, (short)val);
3986 else
3987 trace_seq_printf(s, format, (short)val);
3988 break;
3989 case 0:
3990 if (len_as_arg)
3991 trace_seq_printf(s, format, len_arg, (int)val);
3992 else
3993 trace_seq_printf(s, format, (int)val);
3994 break;
3995 case 1:
3996 if (len_as_arg)
3997 trace_seq_printf(s, format, len_arg, (long)val);
3998 else
3999 trace_seq_printf(s, format, (long)val);
4000 break;
4001 case 2:
4002 if (len_as_arg)
4003 trace_seq_printf(s, format, len_arg,
4004 (long long)val);
4005 else
4006 trace_seq_printf(s, format, (long long)val);
4007 break;
4008 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004009 do_warning("bad count (%d)", ls);
4010 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004011 }
4012 break;
4013 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004014 if (!arg) {
4015 do_warning("no matching argument");
4016 event->flags |= EVENT_FL_FAILED;
4017 goto out_failed;
4018 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004019
4020 len = ((unsigned long)ptr + 1) -
4021 (unsigned long)saveptr;
4022
4023 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004024 if (len > 31) {
4025 do_warning("bad format!");
4026 event->flags |= EVENT_FL_FAILED;
4027 len = 31;
4028 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004029
4030 memcpy(format, saveptr, len);
4031 format[len] = 0;
4032 if (!len_as_arg)
4033 len_arg = -1;
4034 print_str_arg(s, data, size, event,
4035 format, len_arg, arg);
4036 arg = arg->next;
4037 break;
4038 default:
4039 trace_seq_printf(s, ">%c<", *ptr);
4040
4041 }
4042 } else
4043 trace_seq_putc(s, *ptr);
4044 }
4045
Namhyung Kim245c5a12012-09-07 11:49:45 +09004046 if (event->flags & EVENT_FL_FAILED) {
4047out_failed:
4048 trace_seq_printf(s, "[FAILED TO PARSE]");
4049 }
4050
Steven Rostedtf7d82352012-04-06 00:47:53 +02004051 if (args) {
4052 free_args(args);
4053 free(bprint_fmt);
4054 }
4055}
4056
4057/**
4058 * pevent_data_lat_fmt - parse the data for the latency format
4059 * @pevent: a handle to the pevent
4060 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004061 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004062 *
4063 * This parses out the Latency format (interrupts disabled,
4064 * need rescheduling, in hard/soft interrupt, preempt count
4065 * and lock depth) and places it into the trace_seq.
4066 */
4067void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004068 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004069{
4070 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004071 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004072 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004073 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004074 unsigned int lat_flags;
4075 unsigned int pc;
4076 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004077 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004078 int hardirq;
4079 int softirq;
4080 void *data = record->data;
4081
4082 lat_flags = parse_common_flags(pevent, data);
4083 pc = parse_common_pc(pevent, data);
4084 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004085 if (lock_depth_exists)
4086 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004087 else if (check_lock_depth) {
4088 lock_depth = parse_common_lock_depth(pevent, data);
4089 if (lock_depth < 0)
4090 check_lock_depth = 0;
4091 else
4092 lock_depth_exists = 1;
4093 }
4094
4095 /* migrate_disable may not always exist */
4096 if (migrate_disable_exists)
4097 migrate_disable = parse_common_migrate_disable(pevent, data);
4098 else if (check_migrate_disable) {
4099 migrate_disable = parse_common_migrate_disable(pevent, data);
4100 if (migrate_disable < 0)
4101 check_migrate_disable = 0;
4102 else
4103 migrate_disable_exists = 1;
4104 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004105
4106 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4107 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4108
4109 trace_seq_printf(s, "%c%c%c",
4110 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4111 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4112 'X' : '.',
4113 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4114 'N' : '.',
4115 (hardirq && softirq) ? 'H' :
4116 hardirq ? 'h' : softirq ? 's' : '.');
4117
4118 if (pc)
4119 trace_seq_printf(s, "%x", pc);
4120 else
4121 trace_seq_putc(s, '.');
4122
Steven Rostedt0866a972012-05-22 14:52:40 +09004123 if (migrate_disable_exists) {
4124 if (migrate_disable < 0)
4125 trace_seq_putc(s, '.');
4126 else
4127 trace_seq_printf(s, "%d", migrate_disable);
4128 }
4129
Steven Rostedtf7d82352012-04-06 00:47:53 +02004130 if (lock_depth_exists) {
4131 if (lock_depth < 0)
4132 trace_seq_putc(s, '.');
4133 else
4134 trace_seq_printf(s, "%d", lock_depth);
4135 }
4136
4137 trace_seq_terminate(s);
4138}
4139
4140/**
4141 * pevent_data_type - parse out the given event type
4142 * @pevent: a handle to the pevent
4143 * @rec: the record to read from
4144 *
4145 * This returns the event id from the @rec.
4146 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004147int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004148{
4149 return trace_parse_common_type(pevent, rec->data);
4150}
4151
4152/**
4153 * pevent_data_event_from_type - find the event by a given type
4154 * @pevent: a handle to the pevent
4155 * @type: the type of the event.
4156 *
4157 * This returns the event form a given @type;
4158 */
4159struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4160{
4161 return pevent_find_event(pevent, type);
4162}
4163
4164/**
4165 * pevent_data_pid - parse the PID from raw data
4166 * @pevent: a handle to the pevent
4167 * @rec: the record to parse
4168 *
4169 * This returns the PID from a raw data.
4170 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004171int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004172{
4173 return parse_common_pid(pevent, rec->data);
4174}
4175
4176/**
4177 * pevent_data_comm_from_pid - return the command line from PID
4178 * @pevent: a handle to the pevent
4179 * @pid: the PID of the task to search for
4180 *
4181 * This returns a pointer to the command line that has the given
4182 * @pid.
4183 */
4184const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4185{
4186 const char *comm;
4187
4188 comm = find_cmdline(pevent, pid);
4189 return comm;
4190}
4191
4192/**
4193 * pevent_data_comm_from_pid - parse the data into the print format
4194 * @s: the trace_seq to write to
4195 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004196 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004197 *
4198 * This parses the raw @data using the given @event information and
4199 * writes the print format into the trace_seq.
4200 */
4201void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004202 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004203{
4204 int print_pretty = 1;
4205
4206 if (event->pevent->print_raw)
4207 print_event_fields(s, record->data, record->size, event);
4208 else {
4209
4210 if (event->handler)
4211 print_pretty = event->handler(s, record, event,
4212 event->context);
4213
4214 if (print_pretty)
4215 pretty_print(s, record->data, record->size, event);
4216 }
4217
4218 trace_seq_terminate(s);
4219}
4220
4221void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Steven Rostedt1c698182012-04-06 00:48:06 +02004222 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004223{
4224 static char *spaces = " "; /* 20 spaces */
4225 struct event_format *event;
4226 unsigned long secs;
4227 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004228 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004229 const char *comm;
4230 void *data = record->data;
4231 int type;
4232 int pid;
4233 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004234 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004235
4236 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004237 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004238
4239 if (record->size < 0) {
4240 do_warning("ug! negative record size %d", record->size);
4241 return;
4242 }
4243
4244 type = trace_parse_common_type(pevent, data);
4245
4246 event = pevent_find_event(pevent, type);
4247 if (!event) {
4248 do_warning("ug! no event found for type %d", type);
4249 return;
4250 }
4251
4252 pid = parse_common_pid(pevent, data);
4253 comm = find_cmdline(pevent, pid);
4254
4255 if (pevent->latency_format) {
4256 trace_seq_printf(s, "%8.8s-%-5d %3d",
4257 comm, pid, record->cpu);
4258 pevent_data_lat_fmt(pevent, s, record);
4259 } else
4260 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4261
Steven Rostedt4dc10242012-04-06 00:47:57 +02004262 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4263 usecs = nsecs;
4264 p = 9;
4265 } else {
4266 usecs = (nsecs + 500) / NSECS_PER_USEC;
4267 p = 6;
4268 }
4269
4270 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004271
4272 /* Space out the event names evenly. */
4273 len = strlen(event->name);
4274 if (len < 20)
4275 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4276
4277 pevent_event_info(s, event, record);
4278}
4279
4280static int events_id_cmp(const void *a, const void *b)
4281{
4282 struct event_format * const * ea = a;
4283 struct event_format * const * eb = b;
4284
4285 if ((*ea)->id < (*eb)->id)
4286 return -1;
4287
4288 if ((*ea)->id > (*eb)->id)
4289 return 1;
4290
4291 return 0;
4292}
4293
4294static int events_name_cmp(const void *a, const void *b)
4295{
4296 struct event_format * const * ea = a;
4297 struct event_format * const * eb = b;
4298 int res;
4299
4300 res = strcmp((*ea)->name, (*eb)->name);
4301 if (res)
4302 return res;
4303
4304 res = strcmp((*ea)->system, (*eb)->system);
4305 if (res)
4306 return res;
4307
4308 return events_id_cmp(a, b);
4309}
4310
4311static int events_system_cmp(const void *a, const void *b)
4312{
4313 struct event_format * const * ea = a;
4314 struct event_format * const * eb = b;
4315 int res;
4316
4317 res = strcmp((*ea)->system, (*eb)->system);
4318 if (res)
4319 return res;
4320
4321 res = strcmp((*ea)->name, (*eb)->name);
4322 if (res)
4323 return res;
4324
4325 return events_id_cmp(a, b);
4326}
4327
4328struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4329{
4330 struct event_format **events;
4331 int (*sort)(const void *a, const void *b);
4332
4333 events = pevent->sort_events;
4334
4335 if (events && pevent->last_type == sort_type)
4336 return events;
4337
4338 if (!events) {
4339 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4340 if (!events)
4341 return NULL;
4342
4343 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4344 events[pevent->nr_events] = NULL;
4345
4346 pevent->sort_events = events;
4347
4348 /* the internal events are sorted by id */
4349 if (sort_type == EVENT_SORT_ID) {
4350 pevent->last_type = sort_type;
4351 return events;
4352 }
4353 }
4354
4355 switch (sort_type) {
4356 case EVENT_SORT_ID:
4357 sort = events_id_cmp;
4358 break;
4359 case EVENT_SORT_NAME:
4360 sort = events_name_cmp;
4361 break;
4362 case EVENT_SORT_SYSTEM:
4363 sort = events_system_cmp;
4364 break;
4365 default:
4366 return events;
4367 }
4368
4369 qsort(events, pevent->nr_events, sizeof(*events), sort);
4370 pevent->last_type = sort_type;
4371
4372 return events;
4373}
4374
4375static struct format_field **
4376get_event_fields(const char *type, const char *name,
4377 int count, struct format_field *list)
4378{
4379 struct format_field **fields;
4380 struct format_field *field;
4381 int i = 0;
4382
4383 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4384 for (field = list; field; field = field->next) {
4385 fields[i++] = field;
4386 if (i == count + 1) {
4387 do_warning("event %s has more %s fields than specified",
4388 name, type);
4389 i--;
4390 break;
4391 }
4392 }
4393
4394 if (i != count)
4395 do_warning("event %s has less %s fields than specified",
4396 name, type);
4397
4398 fields[i] = NULL;
4399
4400 return fields;
4401}
4402
4403/**
4404 * pevent_event_common_fields - return a list of common fields for an event
4405 * @event: the event to return the common fields of.
4406 *
4407 * Returns an allocated array of fields. The last item in the array is NULL.
4408 * The array must be freed with free().
4409 */
4410struct format_field **pevent_event_common_fields(struct event_format *event)
4411{
4412 return get_event_fields("common", event->name,
4413 event->format.nr_common,
4414 event->format.common_fields);
4415}
4416
4417/**
4418 * pevent_event_fields - return a list of event specific fields for an event
4419 * @event: the event to return the fields of.
4420 *
4421 * Returns an allocated array of fields. The last item in the array is NULL.
4422 * The array must be freed with free().
4423 */
4424struct format_field **pevent_event_fields(struct event_format *event)
4425{
4426 return get_event_fields("event", event->name,
4427 event->format.nr_fields,
4428 event->format.fields);
4429}
4430
4431static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4432{
4433 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4434 if (field->next) {
4435 trace_seq_puts(s, ", ");
4436 print_fields(s, field->next);
4437 }
4438}
4439
4440/* for debugging */
4441static void print_args(struct print_arg *args)
4442{
4443 int print_paren = 1;
4444 struct trace_seq s;
4445
4446 switch (args->type) {
4447 case PRINT_NULL:
4448 printf("null");
4449 break;
4450 case PRINT_ATOM:
4451 printf("%s", args->atom.atom);
4452 break;
4453 case PRINT_FIELD:
4454 printf("REC->%s", args->field.name);
4455 break;
4456 case PRINT_FLAGS:
4457 printf("__print_flags(");
4458 print_args(args->flags.field);
4459 printf(", %s, ", args->flags.delim);
4460 trace_seq_init(&s);
4461 print_fields(&s, args->flags.flags);
4462 trace_seq_do_printf(&s);
4463 trace_seq_destroy(&s);
4464 printf(")");
4465 break;
4466 case PRINT_SYMBOL:
4467 printf("__print_symbolic(");
4468 print_args(args->symbol.field);
4469 printf(", ");
4470 trace_seq_init(&s);
4471 print_fields(&s, args->symbol.symbols);
4472 trace_seq_do_printf(&s);
4473 trace_seq_destroy(&s);
4474 printf(")");
4475 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004476 case PRINT_HEX:
4477 printf("__print_hex(");
4478 print_args(args->hex.field);
4479 printf(", ");
4480 print_args(args->hex.size);
4481 printf(")");
4482 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004483 case PRINT_STRING:
4484 case PRINT_BSTRING:
4485 printf("__get_str(%s)", args->string.string);
4486 break;
4487 case PRINT_TYPE:
4488 printf("(%s)", args->typecast.type);
4489 print_args(args->typecast.item);
4490 break;
4491 case PRINT_OP:
4492 if (strcmp(args->op.op, ":") == 0)
4493 print_paren = 0;
4494 if (print_paren)
4495 printf("(");
4496 print_args(args->op.left);
4497 printf(" %s ", args->op.op);
4498 print_args(args->op.right);
4499 if (print_paren)
4500 printf(")");
4501 break;
4502 default:
4503 /* we should warn... */
4504 return;
4505 }
4506 if (args->next) {
4507 printf("\n");
4508 print_args(args->next);
4509 }
4510}
4511
4512static void parse_header_field(const char *field,
4513 int *offset, int *size, int mandatory)
4514{
4515 unsigned long long save_input_buf_ptr;
4516 unsigned long long save_input_buf_siz;
4517 char *token;
4518 int type;
4519
4520 save_input_buf_ptr = input_buf_ptr;
4521 save_input_buf_siz = input_buf_siz;
4522
4523 if (read_expected(EVENT_ITEM, "field") < 0)
4524 return;
4525 if (read_expected(EVENT_OP, ":") < 0)
4526 return;
4527
4528 /* type */
4529 if (read_expect_type(EVENT_ITEM, &token) < 0)
4530 goto fail;
4531 free_token(token);
4532
4533 /*
4534 * If this is not a mandatory field, then test it first.
4535 */
4536 if (mandatory) {
4537 if (read_expected(EVENT_ITEM, field) < 0)
4538 return;
4539 } else {
4540 if (read_expect_type(EVENT_ITEM, &token) < 0)
4541 goto fail;
4542 if (strcmp(token, field) != 0)
4543 goto discard;
4544 free_token(token);
4545 }
4546
4547 if (read_expected(EVENT_OP, ";") < 0)
4548 return;
4549 if (read_expected(EVENT_ITEM, "offset") < 0)
4550 return;
4551 if (read_expected(EVENT_OP, ":") < 0)
4552 return;
4553 if (read_expect_type(EVENT_ITEM, &token) < 0)
4554 goto fail;
4555 *offset = atoi(token);
4556 free_token(token);
4557 if (read_expected(EVENT_OP, ";") < 0)
4558 return;
4559 if (read_expected(EVENT_ITEM, "size") < 0)
4560 return;
4561 if (read_expected(EVENT_OP, ":") < 0)
4562 return;
4563 if (read_expect_type(EVENT_ITEM, &token) < 0)
4564 goto fail;
4565 *size = atoi(token);
4566 free_token(token);
4567 if (read_expected(EVENT_OP, ";") < 0)
4568 return;
4569 type = read_token(&token);
4570 if (type != EVENT_NEWLINE) {
4571 /* newer versions of the kernel have a "signed" type */
4572 if (type != EVENT_ITEM)
4573 goto fail;
4574
4575 if (strcmp(token, "signed") != 0)
4576 goto fail;
4577
4578 free_token(token);
4579
4580 if (read_expected(EVENT_OP, ":") < 0)
4581 return;
4582
4583 if (read_expect_type(EVENT_ITEM, &token))
4584 goto fail;
4585
4586 free_token(token);
4587 if (read_expected(EVENT_OP, ";") < 0)
4588 return;
4589
4590 if (read_expect_type(EVENT_NEWLINE, &token))
4591 goto fail;
4592 }
4593 fail:
4594 free_token(token);
4595 return;
4596
4597 discard:
4598 input_buf_ptr = save_input_buf_ptr;
4599 input_buf_siz = save_input_buf_siz;
4600 *offset = 0;
4601 *size = 0;
4602 free_token(token);
4603}
4604
4605/**
4606 * pevent_parse_header_page - parse the data stored in the header page
4607 * @pevent: the handle to the pevent
4608 * @buf: the buffer storing the header page format string
4609 * @size: the size of @buf
4610 * @long_size: the long size to use if there is no header
4611 *
4612 * This parses the header page format for information on the
4613 * ring buffer used. The @buf should be copied from
4614 *
4615 * /sys/kernel/debug/tracing/events/header_page
4616 */
4617int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4618 int long_size)
4619{
4620 int ignore;
4621
4622 if (!size) {
4623 /*
4624 * Old kernels did not have header page info.
4625 * Sorry but we just use what we find here in user space.
4626 */
4627 pevent->header_page_ts_size = sizeof(long long);
4628 pevent->header_page_size_size = long_size;
4629 pevent->header_page_data_offset = sizeof(long long) + long_size;
4630 pevent->old_format = 1;
4631 return -1;
4632 }
4633 init_input_buf(buf, size);
4634
4635 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4636 &pevent->header_page_ts_size, 1);
4637 parse_header_field("commit", &pevent->header_page_size_offset,
4638 &pevent->header_page_size_size, 1);
4639 parse_header_field("overwrite", &pevent->header_page_overwrite,
4640 &ignore, 0);
4641 parse_header_field("data", &pevent->header_page_data_offset,
4642 &pevent->header_page_data_size, 1);
4643
4644 return 0;
4645}
4646
4647static int event_matches(struct event_format *event,
4648 int id, const char *sys_name,
4649 const char *event_name)
4650{
4651 if (id >= 0 && id != event->id)
4652 return 0;
4653
4654 if (event_name && (strcmp(event_name, event->name) != 0))
4655 return 0;
4656
4657 if (sys_name && (strcmp(sys_name, event->system) != 0))
4658 return 0;
4659
4660 return 1;
4661}
4662
4663static void free_handler(struct event_handler *handle)
4664{
4665 free((void *)handle->sys_name);
4666 free((void *)handle->event_name);
4667 free(handle);
4668}
4669
4670static int find_event_handle(struct pevent *pevent, struct event_format *event)
4671{
4672 struct event_handler *handle, **next;
4673
4674 for (next = &pevent->handlers; *next;
4675 next = &(*next)->next) {
4676 handle = *next;
4677 if (event_matches(event, handle->id,
4678 handle->sys_name,
4679 handle->event_name))
4680 break;
4681 }
4682
4683 if (!(*next))
4684 return 0;
4685
4686 pr_stat("overriding event (%d) %s:%s with new print handler",
4687 event->id, event->system, event->name);
4688
4689 event->handler = handle->func;
4690 event->context = handle->context;
4691
4692 *next = handle->next;
4693 free_handler(handle);
4694
4695 return 1;
4696}
4697
4698/**
4699 * pevent_parse_event - parse the event format
4700 * @pevent: the handle to the pevent
4701 * @buf: the buffer storing the event format string
4702 * @size: the size of @buf
4703 * @sys: the system the event belongs to
4704 *
4705 * This parses the event format and creates an event structure
4706 * to quickly parse raw data for a given event.
4707 *
4708 * These files currently come from:
4709 *
4710 * /sys/kernel/debug/tracing/events/.../.../format
4711 */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004712enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4713 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004714{
4715 struct event_format *event;
4716 int ret;
4717
4718 init_input_buf(buf, size);
4719
4720 event = alloc_event();
4721 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09004722 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004723
4724 event->name = event_read_name();
4725 if (!event->name) {
4726 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004727 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4728 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004729 }
4730
4731 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004732 event->flags |= EVENT_FL_ISFTRACE;
4733
4734 if (strcmp(event->name, "bprint") == 0)
4735 event->flags |= EVENT_FL_ISBPRINT;
4736 }
4737
4738 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09004739 if (event->id < 0) {
4740 ret = PEVENT_ERRNO__READ_ID_FAILED;
4741 /*
4742 * This isn't an allocation error actually.
4743 * But as the ID is critical, just bail out.
4744 */
4745 goto event_alloc_failed;
4746 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004747
4748 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004749 if (!event->system) {
4750 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4751 goto event_alloc_failed;
4752 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004753
4754 /* Add pevent to event so that it can be referenced */
4755 event->pevent = pevent;
4756
4757 ret = event_read_format(event);
4758 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09004759 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4760 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004761 }
4762
4763 /*
4764 * If the event has an override, don't print warnings if the event
4765 * print format fails to parse.
4766 */
4767 if (find_event_handle(pevent, event))
4768 show_warning = 0;
4769
4770 ret = event_read_print(event);
4771 if (ret < 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004772 show_warning = 1;
Namhyung Kimbffddff2012-08-22 16:00:29 +09004773 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4774 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004775 }
4776 show_warning = 1;
4777
4778 add_event(pevent, event);
4779
4780 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4781 struct format_field *field;
4782 struct print_arg *arg, **list;
4783
4784 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004785 list = &event->print_fmt.args;
4786 for (field = event->format.fields; field; field = field->next) {
4787 arg = alloc_arg();
Steven Rostedtf7d82352012-04-06 00:47:53 +02004788 arg->type = PRINT_FIELD;
4789 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09004790 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09004791 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004792 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004793 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09004794 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004795 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004796 *list = arg;
4797 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004798 }
4799 return 0;
4800 }
4801
4802#define PRINT_ARGS 0
4803 if (PRINT_ARGS && event->print_fmt.args)
4804 print_args(event->print_fmt.args);
4805
4806 return 0;
4807
Namhyung Kimbffddff2012-08-22 16:00:29 +09004808 event_parse_failed:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004809 event->flags |= EVENT_FL_FAILED;
4810 /* still add it even if it failed */
4811 add_event(pevent, event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004812 return ret;
4813
4814 event_alloc_failed:
4815 free(event->system);
4816 free(event->name);
4817 free(event);
4818 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004819}
4820
Namhyung Kim2f197b92012-08-22 16:00:30 +09004821#undef _PE
4822#define _PE(code, str) str
4823static const char * const pevent_error_str[] = {
4824 PEVENT_ERRORS
4825};
4826#undef _PE
4827
4828int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4829 char *buf, size_t buflen)
4830{
4831 int idx;
4832 const char *msg;
4833
4834 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004835 msg = strerror_r(errnum, buf, buflen);
4836 if (msg != buf) {
4837 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03004838 memcpy(buf, msg, min(buflen - 1, len));
4839 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004840 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09004841 return 0;
4842 }
4843
4844 if (errnum <= __PEVENT_ERRNO__START ||
4845 errnum >= __PEVENT_ERRNO__END)
4846 return -1;
4847
Namhyung Kimf63fe792012-08-23 16:37:00 +09004848 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09004849 msg = pevent_error_str[idx];
4850
4851 switch (errnum) {
4852 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4853 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4854 case PEVENT_ERRNO__READ_ID_FAILED:
4855 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4856 case PEVENT_ERRNO__READ_PRINT_FAILED:
4857 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4858 snprintf(buf, buflen, "%s", msg);
4859 break;
4860
4861 default:
4862 /* cannot reach here */
4863 break;
4864 }
4865
4866 return 0;
4867}
4868
Steven Rostedtf7d82352012-04-06 00:47:53 +02004869int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02004870 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004871 unsigned long long *val, int err)
4872{
4873 if (!field) {
4874 if (err)
4875 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4876 return -1;
4877 }
4878
4879 if (pevent_read_number_field(field, record->data, val)) {
4880 if (err)
4881 trace_seq_printf(s, " %s=INVALID", name);
4882 return -1;
4883 }
4884
4885 return 0;
4886}
4887
4888/**
4889 * pevent_get_field_raw - return the raw pointer into the data field
4890 * @s: The seq to print to on error
4891 * @event: the event that the field is for
4892 * @name: The name of the field
4893 * @record: The record with the field name.
4894 * @len: place to store the field length.
4895 * @err: print default error if failed.
4896 *
4897 * Returns a pointer into record->data of the field and places
4898 * the length of the field in @len.
4899 *
4900 * On failure, it returns NULL.
4901 */
4902void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004903 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004904 int *len, int err)
4905{
4906 struct format_field *field;
4907 void *data = record->data;
4908 unsigned offset;
4909 int dummy;
4910
4911 if (!event)
4912 return NULL;
4913
4914 field = pevent_find_field(event, name);
4915
4916 if (!field) {
4917 if (err)
4918 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4919 return NULL;
4920 }
4921
4922 /* Allow @len to be NULL */
4923 if (!len)
4924 len = &dummy;
4925
4926 offset = field->offset;
4927 if (field->flags & FIELD_IS_DYNAMIC) {
4928 offset = pevent_read_number(event->pevent,
4929 data + offset, field->size);
4930 *len = offset >> 16;
4931 offset &= 0xffff;
4932 } else
4933 *len = field->size;
4934
4935 return data + offset;
4936}
4937
4938/**
4939 * pevent_get_field_val - find a field and return its value
4940 * @s: The seq to print to on error
4941 * @event: the event that the field is for
4942 * @name: The name of the field
4943 * @record: The record with the field name.
4944 * @val: place to store the value of the field.
4945 * @err: print default error if failed.
4946 *
4947 * Returns 0 on success -1 on field not found.
4948 */
4949int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004950 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004951 unsigned long long *val, int err)
4952{
4953 struct format_field *field;
4954
4955 if (!event)
4956 return -1;
4957
4958 field = pevent_find_field(event, name);
4959
4960 return get_field_val(s, field, name, record, val, err);
4961}
4962
4963/**
4964 * pevent_get_common_field_val - find a common field and return its value
4965 * @s: The seq to print to on error
4966 * @event: the event that the field is for
4967 * @name: The name of the field
4968 * @record: The record with the field name.
4969 * @val: place to store the value of the field.
4970 * @err: print default error if failed.
4971 *
4972 * Returns 0 on success -1 on field not found.
4973 */
4974int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004975 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004976 unsigned long long *val, int err)
4977{
4978 struct format_field *field;
4979
4980 if (!event)
4981 return -1;
4982
4983 field = pevent_find_common_field(event, name);
4984
4985 return get_field_val(s, field, name, record, val, err);
4986}
4987
4988/**
4989 * pevent_get_any_field_val - find a any field and return its value
4990 * @s: The seq to print to on error
4991 * @event: the event that the field is for
4992 * @name: The name of the field
4993 * @record: The record with the field name.
4994 * @val: place to store the value of the field.
4995 * @err: print default error if failed.
4996 *
4997 * Returns 0 on success -1 on field not found.
4998 */
4999int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005000 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005001 unsigned long long *val, int err)
5002{
5003 struct format_field *field;
5004
5005 if (!event)
5006 return -1;
5007
5008 field = pevent_find_any_field(event, name);
5009
5010 return get_field_val(s, field, name, record, val, err);
5011}
5012
5013/**
5014 * pevent_print_num_field - print a field and a format
5015 * @s: The seq to print to
5016 * @fmt: The printf format to print the field with.
5017 * @event: the event that the field is for
5018 * @name: The name of the field
5019 * @record: The record with the field name.
5020 * @err: print default error if failed.
5021 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005022 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005023 */
5024int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5025 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005026 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005027{
5028 struct format_field *field = pevent_find_field(event, name);
5029 unsigned long long val;
5030
5031 if (!field)
5032 goto failed;
5033
5034 if (pevent_read_number_field(field, record->data, &val))
5035 goto failed;
5036
5037 return trace_seq_printf(s, fmt, val);
5038
5039 failed:
5040 if (err)
5041 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5042 return -1;
5043}
5044
5045static void free_func_handle(struct pevent_function_handler *func)
5046{
5047 struct pevent_func_params *params;
5048
5049 free(func->name);
5050
5051 while (func->params) {
5052 params = func->params;
5053 func->params = params->next;
5054 free(params);
5055 }
5056
5057 free(func);
5058}
5059
5060/**
5061 * pevent_register_print_function - register a helper function
5062 * @pevent: the handle to the pevent
5063 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005064 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005065 * @name: the name of the helper function
5066 * @parameters: A list of enum pevent_func_arg_type
5067 *
5068 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005069 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005070 * of these functions.
5071 *
5072 * The @parameters is a variable list of pevent_func_arg_type enums that
5073 * must end with PEVENT_FUNC_ARG_VOID.
5074 */
5075int pevent_register_print_function(struct pevent *pevent,
5076 pevent_func_handler func,
5077 enum pevent_func_arg_type ret_type,
5078 char *name, ...)
5079{
5080 struct pevent_function_handler *func_handle;
5081 struct pevent_func_params **next_param;
5082 struct pevent_func_params *param;
5083 enum pevent_func_arg_type type;
5084 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005085 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005086
5087 func_handle = find_func_handler(pevent, name);
5088 if (func_handle) {
5089 /*
5090 * This is most like caused by the users own
5091 * plugins updating the function. This overrides the
5092 * system defaults.
5093 */
5094 pr_stat("override of function helper '%s'", name);
5095 remove_func_handler(pevent, name);
5096 }
5097
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005098 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005099 if (!func_handle) {
5100 do_warning("Failed to allocate function handler");
5101 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5102 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005103
5104 func_handle->ret_type = ret_type;
5105 func_handle->name = strdup(name);
5106 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005107 if (!func_handle->name) {
5108 do_warning("Failed to allocate function name");
5109 free(func_handle);
5110 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5111 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005112
5113 next_param = &(func_handle->params);
5114 va_start(ap, name);
5115 for (;;) {
5116 type = va_arg(ap, enum pevent_func_arg_type);
5117 if (type == PEVENT_FUNC_ARG_VOID)
5118 break;
5119
5120 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005121 do_warning("Invalid argument type %d", type);
5122 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005123 goto out_free;
5124 }
5125
Namhyung Kim67ed9392012-09-07 11:49:47 +09005126 param = malloc(sizeof(*param));
5127 if (!param) {
5128 do_warning("Failed to allocate function param");
5129 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5130 goto out_free;
5131 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005132 param->type = type;
5133 param->next = NULL;
5134
5135 *next_param = param;
5136 next_param = &(param->next);
5137
5138 func_handle->nr_args++;
5139 }
5140 va_end(ap);
5141
5142 func_handle->next = pevent->func_handlers;
5143 pevent->func_handlers = func_handle;
5144
5145 return 0;
5146 out_free:
5147 va_end(ap);
5148 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005149 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005150}
5151
5152/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005153 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005154 * @pevent: the handle to the pevent
5155 * @id: the id of the event to register
5156 * @sys_name: the system name the event belongs to
5157 * @event_name: the name of the event
5158 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005159 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005160 *
5161 * This function allows a developer to override the parsing of
5162 * a given event. If for some reason the default print format
5163 * is not sufficient, this function will register a function
5164 * for an event to be used to parse the data instead.
5165 *
5166 * If @id is >= 0, then it is used to find the event.
5167 * else @sys_name and @event_name are used.
5168 */
5169int pevent_register_event_handler(struct pevent *pevent,
5170 int id, char *sys_name, char *event_name,
5171 pevent_event_handler_func func,
5172 void *context)
5173{
5174 struct event_format *event;
5175 struct event_handler *handle;
5176
5177 if (id >= 0) {
5178 /* search by id */
5179 event = pevent_find_event(pevent, id);
5180 if (!event)
5181 goto not_found;
5182 if (event_name && (strcmp(event_name, event->name) != 0))
5183 goto not_found;
5184 if (sys_name && (strcmp(sys_name, event->system) != 0))
5185 goto not_found;
5186 } else {
5187 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5188 if (!event)
5189 goto not_found;
5190 }
5191
5192 pr_stat("overriding event (%d) %s:%s with new print handler",
5193 event->id, event->system, event->name);
5194
5195 event->handler = func;
5196 event->context = context;
5197 return 0;
5198
5199 not_found:
5200 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005201 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005202 if (!handle) {
5203 do_warning("Failed to allocate event handler");
5204 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5205 }
5206
Steven Rostedtf7d82352012-04-06 00:47:53 +02005207 handle->id = id;
5208 if (event_name)
5209 handle->event_name = strdup(event_name);
5210 if (sys_name)
5211 handle->sys_name = strdup(sys_name);
5212
Namhyung Kimca638582012-04-09 11:54:31 +09005213 if ((event_name && !handle->event_name) ||
5214 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005215 do_warning("Failed to allocate event/sys name");
5216 free((void *)handle->event_name);
5217 free((void *)handle->sys_name);
5218 free(handle);
5219 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005220 }
5221
Steven Rostedtf7d82352012-04-06 00:47:53 +02005222 handle->func = func;
5223 handle->next = pevent->handlers;
5224 pevent->handlers = handle;
5225 handle->context = context;
5226
5227 return -1;
5228}
5229
5230/**
5231 * pevent_alloc - create a pevent handle
5232 */
5233struct pevent *pevent_alloc(void)
5234{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005235 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005236
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005237 if (pevent)
5238 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005239
5240 return pevent;
5241}
5242
5243void pevent_ref(struct pevent *pevent)
5244{
5245 pevent->ref_count++;
5246}
5247
5248static void free_format_fields(struct format_field *field)
5249{
5250 struct format_field *next;
5251
5252 while (field) {
5253 next = field->next;
5254 free(field->type);
5255 free(field->name);
5256 free(field);
5257 field = next;
5258 }
5259}
5260
5261static void free_formats(struct format *format)
5262{
5263 free_format_fields(format->common_fields);
5264 free_format_fields(format->fields);
5265}
5266
5267static void free_event(struct event_format *event)
5268{
5269 free(event->name);
5270 free(event->system);
5271
5272 free_formats(&event->format);
5273
5274 free(event->print_fmt.format);
5275 free_args(event->print_fmt.args);
5276
5277 free(event);
5278}
5279
5280/**
5281 * pevent_free - free a pevent handle
5282 * @pevent: the pevent handle to free
5283 */
5284void pevent_free(struct pevent *pevent)
5285{
Steven Rostedta2525a02012-04-06 00:48:02 +02005286 struct cmdline_list *cmdlist, *cmdnext;
5287 struct func_list *funclist, *funcnext;
5288 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005289 struct pevent_function_handler *func_handler;
5290 struct event_handler *handle;
5291 int i;
5292
Steven Rostedta2525a02012-04-06 00:48:02 +02005293 if (!pevent)
5294 return;
5295
5296 cmdlist = pevent->cmdlist;
5297 funclist = pevent->funclist;
5298 printklist = pevent->printklist;
5299
Steven Rostedtf7d82352012-04-06 00:47:53 +02005300 pevent->ref_count--;
5301 if (pevent->ref_count)
5302 return;
5303
5304 if (pevent->cmdlines) {
5305 for (i = 0; i < pevent->cmdline_count; i++)
5306 free(pevent->cmdlines[i].comm);
5307 free(pevent->cmdlines);
5308 }
5309
5310 while (cmdlist) {
5311 cmdnext = cmdlist->next;
5312 free(cmdlist->comm);
5313 free(cmdlist);
5314 cmdlist = cmdnext;
5315 }
5316
5317 if (pevent->func_map) {
5318 for (i = 0; i < pevent->func_count; i++) {
5319 free(pevent->func_map[i].func);
5320 free(pevent->func_map[i].mod);
5321 }
5322 free(pevent->func_map);
5323 }
5324
5325 while (funclist) {
5326 funcnext = funclist->next;
5327 free(funclist->func);
5328 free(funclist->mod);
5329 free(funclist);
5330 funclist = funcnext;
5331 }
5332
5333 while (pevent->func_handlers) {
5334 func_handler = pevent->func_handlers;
5335 pevent->func_handlers = func_handler->next;
5336 free_func_handle(func_handler);
5337 }
5338
5339 if (pevent->printk_map) {
5340 for (i = 0; i < pevent->printk_count; i++)
5341 free(pevent->printk_map[i].printk);
5342 free(pevent->printk_map);
5343 }
5344
5345 while (printklist) {
5346 printknext = printklist->next;
5347 free(printklist->printk);
5348 free(printklist);
5349 printklist = printknext;
5350 }
5351
5352 for (i = 0; i < pevent->nr_events; i++)
5353 free_event(pevent->events[i]);
5354
5355 while (pevent->handlers) {
5356 handle = pevent->handlers;
5357 pevent->handlers = handle->next;
5358 free_handler(handle);
5359 }
5360
5361 free(pevent->events);
5362 free(pevent->sort_events);
5363
5364 free(pevent);
5365}
5366
5367void pevent_unref(struct pevent *pevent)
5368{
5369 pevent_free(pevent);
5370}