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