blob: 6270ee2e98308cf0f8e786bd75d8beb842445dc4 [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
1722 type = process_cond(event, arg, tok);
1723
1724 } else if (strcmp(token, ">>") == 0 ||
1725 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
1740 left = alloc_arg();
1741
1742 /* copy the top arg to the left */
1743 *left = *arg;
1744
1745 arg->type = PRINT_OP;
1746 arg->op.op = token;
1747 arg->op.left = left;
1748
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001749 if (set_op_prio(arg) == -1) {
1750 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001751 /* arg->op.op (= token) will be freed at out_free */
1752 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001753 goto out_free;
1754 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001755
1756 type = read_token_item(&token);
1757 *tok = token;
1758
1759 /* could just be a type pointer */
1760 if ((strcmp(arg->op.op, "*") == 0) &&
1761 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001762 char *new_atom;
1763
Steven Rostedtf7d82352012-04-06 00:47:53 +02001764 if (left->type != PRINT_ATOM)
1765 die("bad pointer type");
Namhyung Kimd2864472012-04-09 11:54:33 +09001766 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001767 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001768 if (!new_atom)
1769 goto out_free;
1770
1771 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001772 strcat(left->atom.atom, " *");
1773 free(arg->op.op);
1774 *arg = *left;
1775 free(left);
1776
1777 return type;
1778 }
1779
1780 right = alloc_arg();
1781 type = process_arg_token(event, right, tok, type);
1782 arg->op.right = right;
1783
1784 } else if (strcmp(token, "[") == 0) {
1785
1786 left = alloc_arg();
1787 *left = *arg;
1788
1789 arg->type = PRINT_OP;
1790 arg->op.op = token;
1791 arg->op.left = left;
1792
1793 arg->op.prio = 0;
1794
1795 type = process_array(event, arg, tok);
1796
1797 } else {
1798 do_warning("unknown op '%s'", token);
1799 event->flags |= EVENT_FL_FAILED;
1800 /* the arg is now the left side */
1801 goto out_free;
1802 }
1803
1804 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1805 int prio;
1806
1807 /* higher prios need to be closer to the root */
1808 prio = get_op_prio(*tok);
1809
1810 if (prio > arg->op.prio)
1811 return process_op(event, arg, tok);
1812
1813 return process_op(event, right, tok);
1814 }
1815
1816 return type;
1817
1818 out_free:
1819 free_token(token);
1820 *tok = NULL;
1821 return EVENT_ERROR;
1822}
1823
1824static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001825process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001826 char **tok)
1827{
1828 enum event_type type;
1829 char *field;
1830 char *token;
1831
1832 if (read_expected(EVENT_OP, "->") < 0)
1833 goto out_err;
1834
1835 if (read_expect_type(EVENT_ITEM, &token) < 0)
1836 goto out_free;
1837 field = token;
1838
1839 arg->type = PRINT_FIELD;
1840 arg->field.name = field;
1841
Tom Zanussi5205aec2012-04-06 00:47:58 +02001842 if (is_flag_field) {
1843 arg->field.field = pevent_find_any_field(event, arg->field.name);
1844 arg->field.field->flags |= FIELD_IS_FLAG;
1845 is_flag_field = 0;
1846 } else if (is_symbolic_field) {
1847 arg->field.field = pevent_find_any_field(event, arg->field.name);
1848 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1849 is_symbolic_field = 0;
1850 }
1851
Steven Rostedtf7d82352012-04-06 00:47:53 +02001852 type = read_token(&token);
1853 *tok = token;
1854
1855 return type;
1856
1857 out_free:
1858 free_token(token);
1859 out_err:
1860 *tok = NULL;
1861 return EVENT_ERROR;
1862}
1863
1864static char *arg_eval (struct print_arg *arg);
1865
1866static unsigned long long
1867eval_type_str(unsigned long long val, const char *type, int pointer)
1868{
1869 int sign = 0;
1870 char *ref;
1871 int len;
1872
1873 len = strlen(type);
1874
1875 if (pointer) {
1876
1877 if (type[len-1] != '*') {
1878 do_warning("pointer expected with non pointer type");
1879 return val;
1880 }
1881
1882 ref = malloc_or_die(len);
1883 memcpy(ref, type, len);
1884
1885 /* chop off the " *" */
1886 ref[len - 2] = 0;
1887
1888 val = eval_type_str(val, ref, 0);
1889 free(ref);
1890 return val;
1891 }
1892
1893 /* check if this is a pointer */
1894 if (type[len - 1] == '*')
1895 return val;
1896
1897 /* Try to figure out the arg size*/
1898 if (strncmp(type, "struct", 6) == 0)
1899 /* all bets off */
1900 return val;
1901
1902 if (strcmp(type, "u8") == 0)
1903 return val & 0xff;
1904
1905 if (strcmp(type, "u16") == 0)
1906 return val & 0xffff;
1907
1908 if (strcmp(type, "u32") == 0)
1909 return val & 0xffffffff;
1910
1911 if (strcmp(type, "u64") == 0 ||
1912 strcmp(type, "s64"))
1913 return val;
1914
1915 if (strcmp(type, "s8") == 0)
1916 return (unsigned long long)(char)val & 0xff;
1917
1918 if (strcmp(type, "s16") == 0)
1919 return (unsigned long long)(short)val & 0xffff;
1920
1921 if (strcmp(type, "s32") == 0)
1922 return (unsigned long long)(int)val & 0xffffffff;
1923
1924 if (strncmp(type, "unsigned ", 9) == 0) {
1925 sign = 0;
1926 type += 9;
1927 }
1928
1929 if (strcmp(type, "char") == 0) {
1930 if (sign)
1931 return (unsigned long long)(char)val & 0xff;
1932 else
1933 return val & 0xff;
1934 }
1935
1936 if (strcmp(type, "short") == 0) {
1937 if (sign)
1938 return (unsigned long long)(short)val & 0xffff;
1939 else
1940 return val & 0xffff;
1941 }
1942
1943 if (strcmp(type, "int") == 0) {
1944 if (sign)
1945 return (unsigned long long)(int)val & 0xffffffff;
1946 else
1947 return val & 0xffffffff;
1948 }
1949
1950 return val;
1951}
1952
1953/*
1954 * Try to figure out the type.
1955 */
1956static unsigned long long
1957eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1958{
1959 if (arg->type != PRINT_TYPE)
1960 die("expected type argument");
1961
1962 return eval_type_str(val, arg->typecast.type, pointer);
1963}
1964
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001965static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001966{
1967 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001968 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001969
1970 switch (arg->type) {
1971 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001972 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001973 break;
1974 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001975 ret = arg_num_eval(arg->typecast.item, val);
1976 if (!ret)
1977 break;
1978 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001979 break;
1980 case PRINT_OP:
1981 switch (arg->op.op[0]) {
1982 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001983 ret = arg_num_eval(arg->op.left, &left);
1984 if (!ret)
1985 break;
1986 ret = arg_num_eval(arg->op.right, &right);
1987 if (!ret)
1988 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001989 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001990 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001991 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001992 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001993 break;
1994 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001995 ret = arg_num_eval(arg->op.left, &left);
1996 if (!ret)
1997 break;
1998 ret = arg_num_eval(arg->op.right, &right);
1999 if (!ret)
2000 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002001 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002002 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002003 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002004 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002005 break;
2006 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002007 ret = arg_num_eval(arg->op.left, &left);
2008 if (!ret)
2009 break;
2010 ret = arg_num_eval(arg->op.right, &right);
2011 if (!ret)
2012 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002013 switch (arg->op.op[1]) {
2014 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002015 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002016 break;
2017 case '<':
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 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002024 do_warning("unknown op '%s'", arg->op.op);
2025 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002026 }
2027 break;
2028 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002029 ret = arg_num_eval(arg->op.left, &left);
2030 if (!ret)
2031 break;
2032 ret = arg_num_eval(arg->op.right, &right);
2033 if (!ret)
2034 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002035 switch (arg->op.op[1]) {
2036 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002037 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002038 break;
2039 case '>':
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 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002046 do_warning("unknown op '%s'", arg->op.op);
2047 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002048 }
2049 break;
2050 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002051 ret = arg_num_eval(arg->op.left, &left);
2052 if (!ret)
2053 break;
2054 ret = arg_num_eval(arg->op.right, &right);
2055 if (!ret)
2056 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002057
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002058 if (arg->op.op[1] != '=') {
2059 do_warning("unknown op '%s'", arg->op.op);
2060 ret = 0;
2061 } else
2062 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002063 break;
2064 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002065 ret = arg_num_eval(arg->op.left, &left);
2066 if (!ret)
2067 break;
2068 ret = arg_num_eval(arg->op.right, &right);
2069 if (!ret)
2070 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002071
2072 switch (arg->op.op[1]) {
2073 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002074 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002075 break;
2076 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002077 do_warning("unknown op '%s'", arg->op.op);
2078 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002079 }
2080 break;
2081 case '-':
2082 /* check for negative */
2083 if (arg->op.left->type == PRINT_NULL)
2084 left = 0;
2085 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002086 ret = arg_num_eval(arg->op.left, &left);
2087 if (!ret)
2088 break;
2089 ret = arg_num_eval(arg->op.right, &right);
2090 if (!ret)
2091 break;
2092 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002093 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002094 case '+':
2095 if (arg->op.left->type == PRINT_NULL)
2096 left = 0;
2097 else
2098 ret = arg_num_eval(arg->op.left, &left);
2099 if (!ret)
2100 break;
2101 ret = arg_num_eval(arg->op.right, &right);
2102 if (!ret)
2103 break;
2104 *val = left + right;
2105 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002106 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002107 do_warning("unknown op '%s'", arg->op.op);
2108 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002109 }
2110 break;
2111
2112 case PRINT_NULL:
2113 case PRINT_FIELD ... PRINT_SYMBOL:
2114 case PRINT_STRING:
2115 case PRINT_BSTRING:
2116 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002117 do_warning("invalid eval type %d", arg->type);
2118 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002119
2120 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002121 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002122}
2123
2124static char *arg_eval (struct print_arg *arg)
2125{
2126 long long val;
2127 static char buf[20];
2128
2129 switch (arg->type) {
2130 case PRINT_ATOM:
2131 return arg->atom.atom;
2132 case PRINT_TYPE:
2133 return arg_eval(arg->typecast.item);
2134 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002135 if (!arg_num_eval(arg, &val))
2136 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002137 sprintf(buf, "%lld", val);
2138 return buf;
2139
2140 case PRINT_NULL:
2141 case PRINT_FIELD ... PRINT_SYMBOL:
2142 case PRINT_STRING:
2143 case PRINT_BSTRING:
2144 default:
2145 die("invalid eval type %d", arg->type);
2146 break;
2147 }
2148
2149 return NULL;
2150}
2151
2152static enum event_type
2153process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2154{
2155 enum event_type type;
2156 struct print_arg *arg = NULL;
2157 struct print_flag_sym *field;
2158 char *token = *tok;
2159 char *value;
2160
2161 do {
2162 free_token(token);
2163 type = read_token_item(&token);
2164 if (test_type_token(type, token, EVENT_OP, "{"))
2165 break;
2166
2167 arg = alloc_arg();
2168
2169 free_token(token);
2170 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002171
2172 if (type == EVENT_OP)
2173 type = process_op(event, arg, &token);
2174
2175 if (type == EVENT_ERROR)
2176 goto out_free;
2177
Steven Rostedtf7d82352012-04-06 00:47:53 +02002178 if (test_type_token(type, token, EVENT_DELIM, ","))
2179 goto out_free;
2180
2181 field = malloc_or_die(sizeof(*field));
Julia Lawall54a36252012-04-06 00:47:59 +02002182 memset(field, 0, sizeof(*field));
Steven Rostedtf7d82352012-04-06 00:47:53 +02002183
2184 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002185 if (value == NULL)
2186 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002187 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002188 if (field->value == NULL)
2189 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002190
2191 free_arg(arg);
2192 arg = alloc_arg();
2193
2194 free_token(token);
2195 type = process_arg(event, arg, &token);
2196 if (test_type_token(type, token, EVENT_OP, "}"))
2197 goto out_free;
2198
2199 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002200 if (value == NULL)
2201 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002202 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002203 if (field->str == NULL)
2204 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002205 free_arg(arg);
2206 arg = NULL;
2207
2208 *list = field;
2209 list = &field->next;
2210
2211 free_token(token);
2212 type = read_token_item(&token);
2213 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2214
2215 *tok = token;
2216 return type;
2217
2218out_free:
2219 free_arg(arg);
2220 free_token(token);
2221 *tok = NULL;
2222
2223 return EVENT_ERROR;
2224}
2225
2226static enum event_type
2227process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2228{
2229 struct print_arg *field;
2230 enum event_type type;
2231 char *token;
2232
2233 memset(arg, 0, sizeof(*arg));
2234 arg->type = PRINT_FLAGS;
2235
2236 field = alloc_arg();
2237
2238 type = process_arg(event, field, &token);
2239
2240 /* Handle operations in the first argument */
2241 while (type == EVENT_OP)
2242 type = process_op(event, field, &token);
2243
2244 if (test_type_token(type, token, EVENT_DELIM, ","))
2245 goto out_free;
2246 free_token(token);
2247
2248 arg->flags.field = field;
2249
2250 type = read_token_item(&token);
2251 if (event_item_type(type)) {
2252 arg->flags.delim = token;
2253 type = read_token_item(&token);
2254 }
2255
2256 if (test_type_token(type, token, EVENT_DELIM, ","))
2257 goto out_free;
2258
2259 type = process_fields(event, &arg->flags.flags, &token);
2260 if (test_type_token(type, token, EVENT_DELIM, ")"))
2261 goto out_free;
2262
2263 free_token(token);
2264 type = read_token_item(tok);
2265 return type;
2266
2267 out_free:
2268 free_token(token);
2269 *tok = NULL;
2270 return EVENT_ERROR;
2271}
2272
2273static enum event_type
2274process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2275{
2276 struct print_arg *field;
2277 enum event_type type;
2278 char *token;
2279
2280 memset(arg, 0, sizeof(*arg));
2281 arg->type = PRINT_SYMBOL;
2282
2283 field = alloc_arg();
2284
2285 type = process_arg(event, field, &token);
2286 if (test_type_token(type, token, EVENT_DELIM, ","))
2287 goto out_free;
2288
2289 arg->symbol.field = field;
2290
2291 type = process_fields(event, &arg->symbol.symbols, &token);
2292 if (test_type_token(type, token, EVENT_DELIM, ")"))
2293 goto out_free;
2294
2295 free_token(token);
2296 type = read_token_item(tok);
2297 return type;
2298
2299 out_free:
2300 free_token(token);
2301 *tok = NULL;
2302 return EVENT_ERROR;
2303}
2304
2305static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002306process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2307{
2308 struct print_arg *field;
2309 enum event_type type;
2310 char *token;
2311
2312 memset(arg, 0, sizeof(*arg));
2313 arg->type = PRINT_HEX;
2314
2315 field = alloc_arg();
2316 type = process_arg(event, field, &token);
2317
2318 if (test_type_token(type, token, EVENT_DELIM, ","))
2319 goto out_free;
2320
2321 arg->hex.field = field;
2322
2323 free_token(token);
2324
2325 field = alloc_arg();
2326 type = process_arg(event, field, &token);
2327
2328 if (test_type_token(type, token, EVENT_DELIM, ")"))
2329 goto out_free;
2330
2331 arg->hex.size = field;
2332
2333 free_token(token);
2334 type = read_token_item(tok);
2335 return type;
2336
2337 out_free:
2338 free_arg(field);
2339 free_token(token);
2340 *tok = NULL;
2341 return EVENT_ERROR;
2342}
2343
2344static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002345process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2346{
2347 struct format_field *field;
2348 enum event_type type;
2349 char *token;
2350
2351 memset(arg, 0, sizeof(*arg));
2352 arg->type = PRINT_DYNAMIC_ARRAY;
2353
2354 /*
2355 * The item within the parenthesis is another field that holds
2356 * the index into where the array starts.
2357 */
2358 type = read_token(&token);
2359 *tok = token;
2360 if (type != EVENT_ITEM)
2361 goto out_free;
2362
2363 /* Find the field */
2364
2365 field = pevent_find_field(event, token);
2366 if (!field)
2367 goto out_free;
2368
2369 arg->dynarray.field = field;
2370 arg->dynarray.index = 0;
2371
2372 if (read_expected(EVENT_DELIM, ")") < 0)
2373 goto out_free;
2374
2375 free_token(token);
2376 type = read_token_item(&token);
2377 *tok = token;
2378 if (type != EVENT_OP || strcmp(token, "[") != 0)
2379 return type;
2380
2381 free_token(token);
2382 arg = alloc_arg();
2383 type = process_arg(event, arg, &token);
2384 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002385 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002386
2387 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002388 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002389
2390 free_token(token);
2391 type = read_token_item(tok);
2392 return type;
2393
Namhyung Kimb3511d02012-05-23 11:36:50 +09002394 out_free_arg:
2395 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002396 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002397 free_token(token);
2398 *tok = NULL;
2399 return EVENT_ERROR;
2400}
2401
2402static enum event_type
2403process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2404{
2405 struct print_arg *item_arg;
2406 enum event_type type;
2407 char *token;
2408
2409 type = process_arg(event, arg, &token);
2410
2411 if (type == EVENT_ERROR)
2412 goto out_free;
2413
2414 if (type == EVENT_OP)
2415 type = process_op(event, arg, &token);
2416
2417 if (type == EVENT_ERROR)
2418 goto out_free;
2419
2420 if (test_type_token(type, token, EVENT_DELIM, ")"))
2421 goto out_free;
2422
2423 free_token(token);
2424 type = read_token_item(&token);
2425
2426 /*
2427 * If the next token is an item or another open paren, then
2428 * this was a typecast.
2429 */
2430 if (event_item_type(type) ||
2431 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2432
2433 /* make this a typecast and contine */
2434
2435 /* prevous must be an atom */
2436 if (arg->type != PRINT_ATOM)
2437 die("previous needed to be PRINT_ATOM");
2438
2439 item_arg = alloc_arg();
2440
2441 arg->type = PRINT_TYPE;
2442 arg->typecast.type = arg->atom.atom;
2443 arg->typecast.item = item_arg;
2444 type = process_arg_token(event, item_arg, &token, type);
2445
2446 }
2447
2448 *tok = token;
2449 return type;
2450
2451 out_free:
2452 free_token(token);
2453 *tok = NULL;
2454 return EVENT_ERROR;
2455}
2456
2457
2458static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002459process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2460 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002461{
2462 enum event_type type;
2463 char *token;
2464
2465 if (read_expect_type(EVENT_ITEM, &token) < 0)
2466 goto out_free;
2467
2468 arg->type = PRINT_STRING;
2469 arg->string.string = token;
2470 arg->string.offset = -1;
2471
2472 if (read_expected(EVENT_DELIM, ")") < 0)
2473 goto out_err;
2474
2475 type = read_token(&token);
2476 *tok = token;
2477
2478 return type;
2479
2480 out_free:
2481 free_token(token);
2482 out_err:
2483 *tok = NULL;
2484 return EVENT_ERROR;
2485}
2486
2487static struct pevent_function_handler *
2488find_func_handler(struct pevent *pevent, char *func_name)
2489{
2490 struct pevent_function_handler *func;
2491
2492 for (func = pevent->func_handlers; func; func = func->next) {
2493 if (strcmp(func->name, func_name) == 0)
2494 break;
2495 }
2496
2497 return func;
2498}
2499
2500static void remove_func_handler(struct pevent *pevent, char *func_name)
2501{
2502 struct pevent_function_handler *func;
2503 struct pevent_function_handler **next;
2504
2505 next = &pevent->func_handlers;
2506 while ((func = *next)) {
2507 if (strcmp(func->name, func_name) == 0) {
2508 *next = func->next;
2509 free_func_handle(func);
2510 break;
2511 }
2512 next = &func->next;
2513 }
2514}
2515
2516static enum event_type
2517process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2518 struct print_arg *arg, char **tok)
2519{
2520 struct print_arg **next_arg;
2521 struct print_arg *farg;
2522 enum event_type type;
2523 char *token;
2524 char *test;
2525 int i;
2526
2527 arg->type = PRINT_FUNC;
2528 arg->func.func = func;
2529
2530 *tok = NULL;
2531
2532 next_arg = &(arg->func.args);
2533 for (i = 0; i < func->nr_args; i++) {
2534 farg = alloc_arg();
2535 type = process_arg(event, farg, &token);
2536 if (i < (func->nr_args - 1))
2537 test = ",";
2538 else
2539 test = ")";
2540
2541 if (test_type_token(type, token, EVENT_DELIM, test)) {
2542 free_arg(farg);
2543 free_token(token);
2544 return EVENT_ERROR;
2545 }
2546
2547 *next_arg = farg;
2548 next_arg = &(farg->next);
2549 free_token(token);
2550 }
2551
2552 type = read_token(&token);
2553 *tok = token;
2554
2555 return type;
2556}
2557
2558static enum event_type
2559process_function(struct event_format *event, struct print_arg *arg,
2560 char *token, char **tok)
2561{
2562 struct pevent_function_handler *func;
2563
2564 if (strcmp(token, "__print_flags") == 0) {
2565 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002566 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002567 return process_flags(event, arg, tok);
2568 }
2569 if (strcmp(token, "__print_symbolic") == 0) {
2570 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002571 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002572 return process_symbols(event, arg, tok);
2573 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002574 if (strcmp(token, "__print_hex") == 0) {
2575 free_token(token);
2576 return process_hex(event, arg, tok);
2577 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002578 if (strcmp(token, "__get_str") == 0) {
2579 free_token(token);
2580 return process_str(event, arg, tok);
2581 }
2582 if (strcmp(token, "__get_dynamic_array") == 0) {
2583 free_token(token);
2584 return process_dynamic_array(event, arg, tok);
2585 }
2586
2587 func = find_func_handler(event->pevent, token);
2588 if (func) {
2589 free_token(token);
2590 return process_func_handler(event, func, arg, tok);
2591 }
2592
2593 do_warning("function %s not defined", token);
2594 free_token(token);
2595 return EVENT_ERROR;
2596}
2597
2598static enum event_type
2599process_arg_token(struct event_format *event, struct print_arg *arg,
2600 char **tok, enum event_type type)
2601{
2602 char *token;
2603 char *atom;
2604
2605 token = *tok;
2606
2607 switch (type) {
2608 case EVENT_ITEM:
2609 if (strcmp(token, "REC") == 0) {
2610 free_token(token);
2611 type = process_entry(event, arg, &token);
2612 break;
2613 }
2614 atom = token;
2615 /* test the next token */
2616 type = read_token_item(&token);
2617
2618 /*
2619 * If the next token is a parenthesis, then this
2620 * is a function.
2621 */
2622 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2623 free_token(token);
2624 token = NULL;
2625 /* this will free atom. */
2626 type = process_function(event, arg, atom, &token);
2627 break;
2628 }
2629 /* atoms can be more than one token long */
2630 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002631 char *new_atom;
2632 new_atom = realloc(atom,
2633 strlen(atom) + strlen(token) + 2);
2634 if (!new_atom) {
2635 free(atom);
2636 *tok = NULL;
2637 free_token(token);
2638 return EVENT_ERROR;
2639 }
2640 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002641 strcat(atom, " ");
2642 strcat(atom, token);
2643 free_token(token);
2644 type = read_token_item(&token);
2645 }
2646
2647 arg->type = PRINT_ATOM;
2648 arg->atom.atom = atom;
2649 break;
2650
2651 case EVENT_DQUOTE:
2652 case EVENT_SQUOTE:
2653 arg->type = PRINT_ATOM;
2654 arg->atom.atom = token;
2655 type = read_token_item(&token);
2656 break;
2657 case EVENT_DELIM:
2658 if (strcmp(token, "(") == 0) {
2659 free_token(token);
2660 type = process_paren(event, arg, &token);
2661 break;
2662 }
2663 case EVENT_OP:
2664 /* handle single ops */
2665 arg->type = PRINT_OP;
2666 arg->op.op = token;
2667 arg->op.left = NULL;
2668 type = process_op(event, arg, &token);
2669
2670 /* On error, the op is freed */
2671 if (type == EVENT_ERROR)
2672 arg->op.op = NULL;
2673
2674 /* return error type if errored */
2675 break;
2676
2677 case EVENT_ERROR ... EVENT_NEWLINE:
2678 default:
2679 die("unexpected type %d", type);
2680 }
2681 *tok = token;
2682
2683 return type;
2684}
2685
2686static int event_read_print_args(struct event_format *event, struct print_arg **list)
2687{
2688 enum event_type type = EVENT_ERROR;
2689 struct print_arg *arg;
2690 char *token;
2691 int args = 0;
2692
2693 do {
2694 if (type == EVENT_NEWLINE) {
2695 type = read_token_item(&token);
2696 continue;
2697 }
2698
2699 arg = alloc_arg();
2700
2701 type = process_arg(event, arg, &token);
2702
2703 if (type == EVENT_ERROR) {
2704 free_token(token);
2705 free_arg(arg);
2706 return -1;
2707 }
2708
2709 *list = arg;
2710 args++;
2711
2712 if (type == EVENT_OP) {
2713 type = process_op(event, arg, &token);
2714 free_token(token);
2715 if (type == EVENT_ERROR) {
2716 *list = NULL;
2717 free_arg(arg);
2718 return -1;
2719 }
2720 list = &arg->next;
2721 continue;
2722 }
2723
2724 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2725 free_token(token);
2726 *list = arg;
2727 list = &arg->next;
2728 continue;
2729 }
2730 break;
2731 } while (type != EVENT_NONE);
2732
2733 if (type != EVENT_NONE && type != EVENT_ERROR)
2734 free_token(token);
2735
2736 return args;
2737}
2738
2739static int event_read_print(struct event_format *event)
2740{
2741 enum event_type type;
2742 char *token;
2743 int ret;
2744
2745 if (read_expected_item(EVENT_ITEM, "print") < 0)
2746 return -1;
2747
2748 if (read_expected(EVENT_ITEM, "fmt") < 0)
2749 return -1;
2750
2751 if (read_expected(EVENT_OP, ":") < 0)
2752 return -1;
2753
2754 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2755 goto fail;
2756
2757 concat:
2758 event->print_fmt.format = token;
2759 event->print_fmt.args = NULL;
2760
2761 /* ok to have no arg */
2762 type = read_token_item(&token);
2763
2764 if (type == EVENT_NONE)
2765 return 0;
2766
2767 /* Handle concatenation of print lines */
2768 if (type == EVENT_DQUOTE) {
2769 char *cat;
2770
2771 cat = malloc_or_die(strlen(event->print_fmt.format) +
2772 strlen(token) + 1);
2773 strcpy(cat, event->print_fmt.format);
2774 strcat(cat, token);
2775 free_token(token);
2776 free_token(event->print_fmt.format);
2777 event->print_fmt.format = NULL;
2778 token = cat;
2779 goto concat;
2780 }
2781
2782 if (test_type_token(type, token, EVENT_DELIM, ","))
2783 goto fail;
2784
2785 free_token(token);
2786
2787 ret = event_read_print_args(event, &event->print_fmt.args);
2788 if (ret < 0)
2789 return -1;
2790
2791 return ret;
2792
2793 fail:
2794 free_token(token);
2795 return -1;
2796}
2797
2798/**
2799 * pevent_find_common_field - return a common field by event
2800 * @event: handle for the event
2801 * @name: the name of the common field to return
2802 *
2803 * Returns a common field from the event by the given @name.
2804 * This only searchs the common fields and not all field.
2805 */
2806struct format_field *
2807pevent_find_common_field(struct event_format *event, const char *name)
2808{
2809 struct format_field *format;
2810
2811 for (format = event->format.common_fields;
2812 format; format = format->next) {
2813 if (strcmp(format->name, name) == 0)
2814 break;
2815 }
2816
2817 return format;
2818}
2819
2820/**
2821 * pevent_find_field - find a non-common field
2822 * @event: handle for the event
2823 * @name: the name of the non-common field
2824 *
2825 * Returns a non-common field by the given @name.
2826 * This does not search common fields.
2827 */
2828struct format_field *
2829pevent_find_field(struct event_format *event, const char *name)
2830{
2831 struct format_field *format;
2832
2833 for (format = event->format.fields;
2834 format; format = format->next) {
2835 if (strcmp(format->name, name) == 0)
2836 break;
2837 }
2838
2839 return format;
2840}
2841
2842/**
2843 * pevent_find_any_field - find any field by name
2844 * @event: handle for the event
2845 * @name: the name of the field
2846 *
2847 * Returns a field by the given @name.
2848 * This searchs the common field names first, then
2849 * the non-common ones if a common one was not found.
2850 */
2851struct format_field *
2852pevent_find_any_field(struct event_format *event, const char *name)
2853{
2854 struct format_field *format;
2855
2856 format = pevent_find_common_field(event, name);
2857 if (format)
2858 return format;
2859 return pevent_find_field(event, name);
2860}
2861
2862/**
2863 * pevent_read_number - read a number from data
2864 * @pevent: handle for the pevent
2865 * @ptr: the raw data
2866 * @size: the size of the data that holds the number
2867 *
2868 * Returns the number (converted to host) from the
2869 * raw data.
2870 */
2871unsigned long long pevent_read_number(struct pevent *pevent,
2872 const void *ptr, int size)
2873{
2874 switch (size) {
2875 case 1:
2876 return *(unsigned char *)ptr;
2877 case 2:
2878 return data2host2(pevent, ptr);
2879 case 4:
2880 return data2host4(pevent, ptr);
2881 case 8:
2882 return data2host8(pevent, ptr);
2883 default:
2884 /* BUG! */
2885 return 0;
2886 }
2887}
2888
2889/**
2890 * pevent_read_number_field - read a number from data
2891 * @field: a handle to the field
2892 * @data: the raw data to read
2893 * @value: the value to place the number in
2894 *
2895 * Reads raw data according to a field offset and size,
2896 * and translates it into @value.
2897 *
2898 * Returns 0 on success, -1 otherwise.
2899 */
2900int pevent_read_number_field(struct format_field *field, const void *data,
2901 unsigned long long *value)
2902{
2903 if (!field)
2904 return -1;
2905 switch (field->size) {
2906 case 1:
2907 case 2:
2908 case 4:
2909 case 8:
2910 *value = pevent_read_number(field->event->pevent,
2911 data + field->offset, field->size);
2912 return 0;
2913 default:
2914 return -1;
2915 }
2916}
2917
2918static int get_common_info(struct pevent *pevent,
2919 const char *type, int *offset, int *size)
2920{
2921 struct event_format *event;
2922 struct format_field *field;
2923
2924 /*
2925 * All events should have the same common elements.
2926 * Pick any event to find where the type is;
2927 */
2928 if (!pevent->events)
2929 die("no event_list!");
2930
2931 event = pevent->events[0];
2932 field = pevent_find_common_field(event, type);
2933 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09002934 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002935
2936 *offset = field->offset;
2937 *size = field->size;
2938
2939 return 0;
2940}
2941
2942static int __parse_common(struct pevent *pevent, void *data,
2943 int *size, int *offset, const char *name)
2944{
2945 int ret;
2946
2947 if (!*size) {
2948 ret = get_common_info(pevent, name, offset, size);
2949 if (ret < 0)
2950 return ret;
2951 }
2952 return pevent_read_number(pevent, data + *offset, *size);
2953}
2954
2955static int trace_parse_common_type(struct pevent *pevent, void *data)
2956{
2957 return __parse_common(pevent, data,
2958 &pevent->type_size, &pevent->type_offset,
2959 "common_type");
2960}
2961
2962static int parse_common_pid(struct pevent *pevent, void *data)
2963{
2964 return __parse_common(pevent, data,
2965 &pevent->pid_size, &pevent->pid_offset,
2966 "common_pid");
2967}
2968
2969static int parse_common_pc(struct pevent *pevent, void *data)
2970{
2971 return __parse_common(pevent, data,
2972 &pevent->pc_size, &pevent->pc_offset,
2973 "common_preempt_count");
2974}
2975
2976static int parse_common_flags(struct pevent *pevent, void *data)
2977{
2978 return __parse_common(pevent, data,
2979 &pevent->flags_size, &pevent->flags_offset,
2980 "common_flags");
2981}
2982
2983static int parse_common_lock_depth(struct pevent *pevent, void *data)
2984{
Steven Rostedt0866a972012-05-22 14:52:40 +09002985 return __parse_common(pevent, data,
2986 &pevent->ld_size, &pevent->ld_offset,
2987 "common_lock_depth");
2988}
Steven Rostedtf7d82352012-04-06 00:47:53 +02002989
Steven Rostedt0866a972012-05-22 14:52:40 +09002990static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2991{
2992 return __parse_common(pevent, data,
2993 &pevent->ld_size, &pevent->ld_offset,
2994 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02002995}
2996
2997static int events_id_cmp(const void *a, const void *b);
2998
2999/**
3000 * pevent_find_event - find an event by given id
3001 * @pevent: a handle to the pevent
3002 * @id: the id of the event
3003 *
3004 * Returns an event that has a given @id.
3005 */
3006struct event_format *pevent_find_event(struct pevent *pevent, int id)
3007{
3008 struct event_format **eventptr;
3009 struct event_format key;
3010 struct event_format *pkey = &key;
3011
3012 /* Check cache first */
3013 if (pevent->last_event && pevent->last_event->id == id)
3014 return pevent->last_event;
3015
3016 key.id = id;
3017
3018 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3019 sizeof(*pevent->events), events_id_cmp);
3020
3021 if (eventptr) {
3022 pevent->last_event = *eventptr;
3023 return *eventptr;
3024 }
3025
3026 return NULL;
3027}
3028
3029/**
3030 * pevent_find_event_by_name - find an event by given name
3031 * @pevent: a handle to the pevent
3032 * @sys: the system name to search for
3033 * @name: the name of the event to search for
3034 *
3035 * This returns an event with a given @name and under the system
3036 * @sys. If @sys is NULL the first event with @name is returned.
3037 */
3038struct event_format *
3039pevent_find_event_by_name(struct pevent *pevent,
3040 const char *sys, const char *name)
3041{
3042 struct event_format *event;
3043 int i;
3044
3045 if (pevent->last_event &&
3046 strcmp(pevent->last_event->name, name) == 0 &&
3047 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3048 return pevent->last_event;
3049
3050 for (i = 0; i < pevent->nr_events; i++) {
3051 event = pevent->events[i];
3052 if (strcmp(event->name, name) == 0) {
3053 if (!sys)
3054 break;
3055 if (strcmp(event->system, sys) == 0)
3056 break;
3057 }
3058 }
3059 if (i == pevent->nr_events)
3060 event = NULL;
3061
3062 pevent->last_event = event;
3063 return event;
3064}
3065
3066static unsigned long long
3067eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3068{
3069 struct pevent *pevent = event->pevent;
3070 unsigned long long val = 0;
3071 unsigned long long left, right;
3072 struct print_arg *typearg = NULL;
3073 struct print_arg *larg;
3074 unsigned long offset;
3075 unsigned int field_size;
3076
3077 switch (arg->type) {
3078 case PRINT_NULL:
3079 /* ?? */
3080 return 0;
3081 case PRINT_ATOM:
3082 return strtoull(arg->atom.atom, NULL, 0);
3083 case PRINT_FIELD:
3084 if (!arg->field.field) {
3085 arg->field.field = pevent_find_any_field(event, arg->field.name);
3086 if (!arg->field.field)
3087 die("field %s not found", arg->field.name);
3088 }
3089 /* must be a number */
3090 val = pevent_read_number(pevent, data + arg->field.field->offset,
3091 arg->field.field->size);
3092 break;
3093 case PRINT_FLAGS:
3094 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003095 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003096 break;
3097 case PRINT_TYPE:
3098 val = eval_num_arg(data, size, event, arg->typecast.item);
3099 return eval_type(val, arg, 0);
3100 case PRINT_STRING:
3101 case PRINT_BSTRING:
3102 return 0;
3103 case PRINT_FUNC: {
3104 struct trace_seq s;
3105 trace_seq_init(&s);
3106 val = process_defined_func(&s, data, size, event, arg);
3107 trace_seq_destroy(&s);
3108 return val;
3109 }
3110 case PRINT_OP:
3111 if (strcmp(arg->op.op, "[") == 0) {
3112 /*
3113 * Arrays are special, since we don't want
3114 * to read the arg as is.
3115 */
3116 right = eval_num_arg(data, size, event, arg->op.right);
3117
3118 /* handle typecasts */
3119 larg = arg->op.left;
3120 while (larg->type == PRINT_TYPE) {
3121 if (!typearg)
3122 typearg = larg;
3123 larg = larg->typecast.item;
3124 }
3125
3126 /* Default to long size */
3127 field_size = pevent->long_size;
3128
3129 switch (larg->type) {
3130 case PRINT_DYNAMIC_ARRAY:
3131 offset = pevent_read_number(pevent,
3132 data + larg->dynarray.field->offset,
3133 larg->dynarray.field->size);
3134 if (larg->dynarray.field->elementsize)
3135 field_size = larg->dynarray.field->elementsize;
3136 /*
3137 * The actual length of the dynamic array is stored
3138 * in the top half of the field, and the offset
3139 * is in the bottom half of the 32 bit field.
3140 */
3141 offset &= 0xffff;
3142 offset += right;
3143 break;
3144 case PRINT_FIELD:
3145 if (!larg->field.field) {
3146 larg->field.field =
3147 pevent_find_any_field(event, larg->field.name);
3148 if (!larg->field.field)
3149 die("field %s not found", larg->field.name);
3150 }
3151 field_size = larg->field.field->elementsize;
3152 offset = larg->field.field->offset +
3153 right * larg->field.field->elementsize;
3154 break;
3155 default:
3156 goto default_op; /* oops, all bets off */
3157 }
3158 val = pevent_read_number(pevent,
3159 data + offset, field_size);
3160 if (typearg)
3161 val = eval_type(val, typearg, 1);
3162 break;
3163 } else if (strcmp(arg->op.op, "?") == 0) {
3164 left = eval_num_arg(data, size, event, arg->op.left);
3165 arg = arg->op.right;
3166 if (left)
3167 val = eval_num_arg(data, size, event, arg->op.left);
3168 else
3169 val = eval_num_arg(data, size, event, arg->op.right);
3170 break;
3171 }
3172 default_op:
3173 left = eval_num_arg(data, size, event, arg->op.left);
3174 right = eval_num_arg(data, size, event, arg->op.right);
3175 switch (arg->op.op[0]) {
3176 case '!':
3177 switch (arg->op.op[1]) {
3178 case 0:
3179 val = !right;
3180 break;
3181 case '=':
3182 val = left != right;
3183 break;
3184 default:
3185 die("unknown op '%s'", arg->op.op);
3186 }
3187 break;
3188 case '~':
3189 val = ~right;
3190 break;
3191 case '|':
3192 if (arg->op.op[1])
3193 val = left || right;
3194 else
3195 val = left | right;
3196 break;
3197 case '&':
3198 if (arg->op.op[1])
3199 val = left && right;
3200 else
3201 val = left & right;
3202 break;
3203 case '<':
3204 switch (arg->op.op[1]) {
3205 case 0:
3206 val = left < right;
3207 break;
3208 case '<':
3209 val = left << right;
3210 break;
3211 case '=':
3212 val = left <= right;
3213 break;
3214 default:
3215 die("unknown op '%s'", arg->op.op);
3216 }
3217 break;
3218 case '>':
3219 switch (arg->op.op[1]) {
3220 case 0:
3221 val = left > right;
3222 break;
3223 case '>':
3224 val = left >> right;
3225 break;
3226 case '=':
3227 val = left >= right;
3228 break;
3229 default:
3230 die("unknown op '%s'", arg->op.op);
3231 }
3232 break;
3233 case '=':
3234 if (arg->op.op[1] != '=')
3235 die("unknown op '%s'", arg->op.op);
3236 val = left == right;
3237 break;
3238 case '-':
3239 val = left - right;
3240 break;
3241 case '+':
3242 val = left + right;
3243 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003244 case '/':
3245 val = left / right;
3246 break;
3247 case '*':
3248 val = left * right;
3249 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003250 default:
3251 die("unknown op '%s'", arg->op.op);
3252 }
3253 break;
3254 default: /* not sure what to do there */
3255 return 0;
3256 }
3257 return val;
3258}
3259
3260struct flag {
3261 const char *name;
3262 unsigned long long value;
3263};
3264
3265static const struct flag flags[] = {
3266 { "HI_SOFTIRQ", 0 },
3267 { "TIMER_SOFTIRQ", 1 },
3268 { "NET_TX_SOFTIRQ", 2 },
3269 { "NET_RX_SOFTIRQ", 3 },
3270 { "BLOCK_SOFTIRQ", 4 },
3271 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3272 { "TASKLET_SOFTIRQ", 6 },
3273 { "SCHED_SOFTIRQ", 7 },
3274 { "HRTIMER_SOFTIRQ", 8 },
3275 { "RCU_SOFTIRQ", 9 },
3276
3277 { "HRTIMER_NORESTART", 0 },
3278 { "HRTIMER_RESTART", 1 },
3279};
3280
3281static unsigned long long eval_flag(const char *flag)
3282{
3283 int i;
3284
3285 /*
3286 * Some flags in the format files do not get converted.
3287 * If the flag is not numeric, see if it is something that
3288 * we already know about.
3289 */
3290 if (isdigit(flag[0]))
3291 return strtoull(flag, NULL, 0);
3292
3293 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3294 if (strcmp(flags[i].name, flag) == 0)
3295 return flags[i].value;
3296
3297 return 0;
3298}
3299
3300static void print_str_to_seq(struct trace_seq *s, const char *format,
3301 int len_arg, const char *str)
3302{
3303 if (len_arg >= 0)
3304 trace_seq_printf(s, format, len_arg, str);
3305 else
3306 trace_seq_printf(s, format, str);
3307}
3308
3309static void print_str_arg(struct trace_seq *s, void *data, int size,
3310 struct event_format *event, const char *format,
3311 int len_arg, struct print_arg *arg)
3312{
3313 struct pevent *pevent = event->pevent;
3314 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003315 struct format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003316 unsigned long long val, fval;
3317 unsigned long addr;
3318 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003319 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003320 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003321 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003322
3323 switch (arg->type) {
3324 case PRINT_NULL:
3325 /* ?? */
3326 return;
3327 case PRINT_ATOM:
3328 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3329 return;
3330 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003331 field = arg->field.field;
3332 if (!field) {
3333 field = pevent_find_any_field(event, arg->field.name);
3334 if (!field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003335 die("field %s not found", arg->field.name);
Namhyung Kimb7008072012-06-27 09:41:40 +09003336 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003337 }
3338 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003339 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003340
3341 /*
3342 * Some events pass in pointers. If this is not an array
3343 * and the size is the same as long_size, assume that it
3344 * is a pointer.
3345 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003346 if (!(field->flags & FIELD_IS_ARRAY) &&
3347 field->size == pevent->long_size) {
3348 addr = *(unsigned long *)(data + field->offset);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003349 trace_seq_printf(s, "%lx", addr);
3350 break;
3351 }
3352 str = malloc_or_die(len + 1);
Namhyung Kimb7008072012-06-27 09:41:40 +09003353 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003354 str[len] = 0;
3355 print_str_to_seq(s, format, len_arg, str);
3356 free(str);
3357 break;
3358 case PRINT_FLAGS:
3359 val = eval_num_arg(data, size, event, arg->flags.field);
3360 print = 0;
3361 for (flag = arg->flags.flags; flag; flag = flag->next) {
3362 fval = eval_flag(flag->value);
3363 if (!val && !fval) {
3364 print_str_to_seq(s, format, len_arg, flag->str);
3365 break;
3366 }
3367 if (fval && (val & fval) == fval) {
3368 if (print && arg->flags.delim)
3369 trace_seq_puts(s, arg->flags.delim);
3370 print_str_to_seq(s, format, len_arg, flag->str);
3371 print = 1;
3372 val &= ~fval;
3373 }
3374 }
3375 break;
3376 case PRINT_SYMBOL:
3377 val = eval_num_arg(data, size, event, arg->symbol.field);
3378 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3379 fval = eval_flag(flag->value);
3380 if (val == fval) {
3381 print_str_to_seq(s, format, len_arg, flag->str);
3382 break;
3383 }
3384 }
3385 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003386 case PRINT_HEX:
3387 field = arg->hex.field->field.field;
3388 if (!field) {
3389 str = arg->hex.field->field.name;
3390 field = pevent_find_any_field(event, str);
3391 if (!field)
3392 die("field %s not found", str);
3393 arg->hex.field->field.field = field;
3394 }
3395 hex = data + field->offset;
3396 len = eval_num_arg(data, size, event, arg->hex.size);
3397 for (i = 0; i < len; i++) {
3398 if (i)
3399 trace_seq_putc(s, ' ');
3400 trace_seq_printf(s, "%02x", hex[i]);
3401 }
3402 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003403
3404 case PRINT_TYPE:
3405 break;
3406 case PRINT_STRING: {
3407 int str_offset;
3408
3409 if (arg->string.offset == -1) {
3410 struct format_field *f;
3411
3412 f = pevent_find_any_field(event, arg->string.string);
3413 arg->string.offset = f->offset;
3414 }
3415 str_offset = data2host4(pevent, data + arg->string.offset);
3416 str_offset &= 0xffff;
3417 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3418 break;
3419 }
3420 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003421 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003422 break;
3423 case PRINT_OP:
3424 /*
3425 * The only op for string should be ? :
3426 */
3427 if (arg->op.op[0] != '?')
3428 return;
3429 val = eval_num_arg(data, size, event, arg->op.left);
3430 if (val)
3431 print_str_arg(s, data, size, event,
3432 format, len_arg, arg->op.right->op.left);
3433 else
3434 print_str_arg(s, data, size, event,
3435 format, len_arg, arg->op.right->op.right);
3436 break;
3437 case PRINT_FUNC:
3438 process_defined_func(s, data, size, event, arg);
3439 break;
3440 default:
3441 /* well... */
3442 break;
3443 }
3444}
3445
3446static unsigned long long
3447process_defined_func(struct trace_seq *s, void *data, int size,
3448 struct event_format *event, struct print_arg *arg)
3449{
3450 struct pevent_function_handler *func_handle = arg->func.func;
3451 struct pevent_func_params *param;
3452 unsigned long long *args;
3453 unsigned long long ret;
3454 struct print_arg *farg;
3455 struct trace_seq str;
3456 struct save_str {
3457 struct save_str *next;
3458 char *str;
3459 } *strings = NULL, *string;
3460 int i;
3461
3462 if (!func_handle->nr_args) {
3463 ret = (*func_handle->func)(s, NULL);
3464 goto out;
3465 }
3466
3467 farg = arg->func.args;
3468 param = func_handle->params;
3469
3470 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3471 for (i = 0; i < func_handle->nr_args; i++) {
3472 switch (param->type) {
3473 case PEVENT_FUNC_ARG_INT:
3474 case PEVENT_FUNC_ARG_LONG:
3475 case PEVENT_FUNC_ARG_PTR:
3476 args[i] = eval_num_arg(data, size, event, farg);
3477 break;
3478 case PEVENT_FUNC_ARG_STRING:
3479 trace_seq_init(&str);
3480 print_str_arg(&str, data, size, event, "%s", -1, farg);
3481 trace_seq_terminate(&str);
3482 string = malloc_or_die(sizeof(*string));
3483 string->next = strings;
3484 string->str = strdup(str.buffer);
Namhyung Kimca638582012-04-09 11:54:31 +09003485 if (!string->str)
3486 die("malloc str");
3487
Robert Richter0cf26012012-08-07 19:43:14 +02003488 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003489 strings = string;
3490 trace_seq_destroy(&str);
3491 break;
3492 default:
3493 /*
3494 * Something went totally wrong, this is not
3495 * an input error, something in this code broke.
3496 */
3497 die("Unexpected end of arguments\n");
3498 break;
3499 }
3500 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003501 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003502 }
3503
3504 ret = (*func_handle->func)(s, args);
3505 free(args);
3506 while (strings) {
3507 string = strings;
3508 strings = string->next;
3509 free(string->str);
3510 free(string);
3511 }
3512
3513 out:
3514 /* TBD : handle return type here */
3515 return ret;
3516}
3517
3518static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3519{
3520 struct pevent *pevent = event->pevent;
3521 struct format_field *field, *ip_field;
3522 struct print_arg *args, *arg, **next;
3523 unsigned long long ip, val;
3524 char *ptr;
3525 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003526 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003527
3528 field = pevent->bprint_buf_field;
3529 ip_field = pevent->bprint_ip_field;
3530
3531 if (!field) {
3532 field = pevent_find_field(event, "buf");
3533 if (!field)
3534 die("can't find buffer field for binary printk");
3535 ip_field = pevent_find_field(event, "ip");
3536 if (!ip_field)
3537 die("can't find ip field for binary printk");
3538 pevent->bprint_buf_field = field;
3539 pevent->bprint_ip_field = ip_field;
3540 }
3541
3542 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3543
3544 /*
3545 * The first arg is the IP pointer.
3546 */
3547 args = alloc_arg();
3548 arg = args;
3549 arg->next = NULL;
3550 next = &arg->next;
3551
3552 arg->type = PRINT_ATOM;
3553 arg->atom.atom = malloc_or_die(32);
3554 sprintf(arg->atom.atom, "%lld", ip);
3555
3556 /* skip the first "%pf : " */
3557 for (ptr = fmt + 6, bptr = data + field->offset;
3558 bptr < data + size && *ptr; ptr++) {
3559 int ls = 0;
3560
3561 if (*ptr == '%') {
3562 process_again:
3563 ptr++;
3564 switch (*ptr) {
3565 case '%':
3566 break;
3567 case 'l':
3568 ls++;
3569 goto process_again;
3570 case 'L':
3571 ls = 2;
3572 goto process_again;
3573 case '0' ... '9':
3574 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003575 case '.':
3576 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003577 case 'p':
3578 ls = 1;
3579 /* fall through */
3580 case 'd':
3581 case 'u':
3582 case 'x':
3583 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003584 switch (ls) {
3585 case 0:
3586 vsize = 4;
3587 break;
3588 case 1:
3589 vsize = pevent->long_size;
3590 break;
3591 case 2:
3592 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003593 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003594 default:
3595 vsize = ls; /* ? */
3596 break;
3597 }
3598 /* fall through */
3599 case '*':
3600 if (*ptr == '*')
3601 vsize = 4;
3602
Steven Rostedtf7d82352012-04-06 00:47:53 +02003603 /* the pointers are always 4 bytes aligned */
3604 bptr = (void *)(((unsigned long)bptr + 3) &
3605 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003606 val = pevent_read_number(pevent, bptr, vsize);
3607 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003608 arg = alloc_arg();
3609 arg->next = NULL;
3610 arg->type = PRINT_ATOM;
3611 arg->atom.atom = malloc_or_die(32);
3612 sprintf(arg->atom.atom, "%lld", val);
3613 *next = arg;
3614 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003615 /*
3616 * The '*' case means that an arg is used as the length.
3617 * We need to continue to figure out for what.
3618 */
3619 if (*ptr == '*')
3620 goto process_again;
3621
Steven Rostedtf7d82352012-04-06 00:47:53 +02003622 break;
3623 case 's':
3624 arg = alloc_arg();
3625 arg->next = NULL;
3626 arg->type = PRINT_BSTRING;
3627 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003628 if (!arg->string.string)
3629 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003630 bptr += strlen(bptr) + 1;
3631 *next = arg;
3632 next = &arg->next;
3633 default:
3634 break;
3635 }
3636 }
3637 }
3638
3639 return args;
3640}
3641
3642static void free_args(struct print_arg *args)
3643{
3644 struct print_arg *next;
3645
3646 while (args) {
3647 next = args->next;
3648
3649 free_arg(args);
3650 args = next;
3651 }
3652}
3653
3654static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003655get_bprint_format(void *data, int size __maybe_unused,
3656 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003657{
3658 struct pevent *pevent = event->pevent;
3659 unsigned long long addr;
3660 struct format_field *field;
3661 struct printk_map *printk;
3662 char *format;
3663 char *p;
3664
3665 field = pevent->bprint_fmt_field;
3666
3667 if (!field) {
3668 field = pevent_find_field(event, "fmt");
3669 if (!field)
3670 die("can't find format field for binary printk");
3671 pevent->bprint_fmt_field = field;
3672 }
3673
3674 addr = pevent_read_number(pevent, data + field->offset, field->size);
3675
3676 printk = find_printk(pevent, addr);
3677 if (!printk) {
3678 format = malloc_or_die(45);
3679 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3680 addr);
3681 return format;
3682 }
3683
3684 p = printk->printk;
3685 /* Remove any quotes. */
3686 if (*p == '"')
3687 p++;
3688 format = malloc_or_die(strlen(p) + 10);
3689 sprintf(format, "%s : %s", "%pf", p);
3690 /* remove ending quotes and new line since we will add one too */
3691 p = format + strlen(format) - 1;
3692 if (*p == '"')
3693 *p = 0;
3694
3695 p -= 2;
3696 if (strcmp(p, "\\n") == 0)
3697 *p = 0;
3698
3699 return format;
3700}
3701
3702static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3703 struct event_format *event, struct print_arg *arg)
3704{
3705 unsigned char *buf;
3706 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3707
3708 if (arg->type == PRINT_FUNC) {
3709 process_defined_func(s, data, size, event, arg);
3710 return;
3711 }
3712
3713 if (arg->type != PRINT_FIELD) {
3714 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3715 arg->type);
3716 return;
3717 }
3718
3719 if (mac == 'm')
3720 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3721 if (!arg->field.field) {
3722 arg->field.field =
3723 pevent_find_any_field(event, arg->field.name);
3724 if (!arg->field.field)
3725 die("field %s not found", arg->field.name);
3726 }
3727 if (arg->field.field->size != 6) {
3728 trace_seq_printf(s, "INVALIDMAC");
3729 return;
3730 }
3731 buf = data + arg->field.field->offset;
3732 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3733}
3734
Namhyung Kim600da3c2012-06-22 17:10:15 +09003735static int is_printable_array(char *p, unsigned int len)
3736{
3737 unsigned int i;
3738
3739 for (i = 0; i < len && p[i]; i++)
3740 if (!isprint(p[i]))
3741 return 0;
3742 return 1;
3743}
3744
Steven Rostedtf7d82352012-04-06 00:47:53 +02003745static void print_event_fields(struct trace_seq *s, void *data, int size,
3746 struct event_format *event)
3747{
3748 struct format_field *field;
3749 unsigned long long val;
3750 unsigned int offset, len, i;
3751
3752 field = event->format.fields;
3753 while (field) {
3754 trace_seq_printf(s, " %s=", field->name);
3755 if (field->flags & FIELD_IS_ARRAY) {
3756 offset = field->offset;
3757 len = field->size;
3758 if (field->flags & FIELD_IS_DYNAMIC) {
3759 val = pevent_read_number(event->pevent, data + offset, len);
3760 offset = val;
3761 len = offset >> 16;
3762 offset &= 0xffff;
3763 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09003764 if (field->flags & FIELD_IS_STRING &&
3765 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003766 trace_seq_printf(s, "%s", (char *)data + offset);
3767 } else {
3768 trace_seq_puts(s, "ARRAY[");
3769 for (i = 0; i < len; i++) {
3770 if (i)
3771 trace_seq_puts(s, ", ");
3772 trace_seq_printf(s, "%02x",
3773 *((unsigned char *)data + offset + i));
3774 }
3775 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09003776 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003777 }
3778 } else {
3779 val = pevent_read_number(event->pevent, data + field->offset,
3780 field->size);
3781 if (field->flags & FIELD_IS_POINTER) {
3782 trace_seq_printf(s, "0x%llx", val);
3783 } else if (field->flags & FIELD_IS_SIGNED) {
3784 switch (field->size) {
3785 case 4:
3786 /*
3787 * If field is long then print it in hex.
3788 * A long usually stores pointers.
3789 */
3790 if (field->flags & FIELD_IS_LONG)
3791 trace_seq_printf(s, "0x%x", (int)val);
3792 else
3793 trace_seq_printf(s, "%d", (int)val);
3794 break;
3795 case 2:
3796 trace_seq_printf(s, "%2d", (short)val);
3797 break;
3798 case 1:
3799 trace_seq_printf(s, "%1d", (char)val);
3800 break;
3801 default:
3802 trace_seq_printf(s, "%lld", val);
3803 }
3804 } else {
3805 if (field->flags & FIELD_IS_LONG)
3806 trace_seq_printf(s, "0x%llx", val);
3807 else
3808 trace_seq_printf(s, "%llu", val);
3809 }
3810 }
3811 field = field->next;
3812 }
3813}
3814
3815static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3816{
3817 struct pevent *pevent = event->pevent;
3818 struct print_fmt *print_fmt = &event->print_fmt;
3819 struct print_arg *arg = print_fmt->args;
3820 struct print_arg *args = NULL;
3821 const char *ptr = print_fmt->format;
3822 unsigned long long val;
3823 struct func_map *func;
3824 const char *saveptr;
3825 char *bprint_fmt = NULL;
3826 char format[32];
3827 int show_func;
3828 int len_as_arg;
3829 int len_arg;
3830 int len;
3831 int ls;
3832
3833 if (event->flags & EVENT_FL_FAILED) {
3834 trace_seq_printf(s, "[FAILED TO PARSE]");
3835 print_event_fields(s, data, size, event);
3836 return;
3837 }
3838
3839 if (event->flags & EVENT_FL_ISBPRINT) {
3840 bprint_fmt = get_bprint_format(data, size, event);
3841 args = make_bprint_args(bprint_fmt, data, size, event);
3842 arg = args;
3843 ptr = bprint_fmt;
3844 }
3845
3846 for (; *ptr; ptr++) {
3847 ls = 0;
3848 if (*ptr == '\\') {
3849 ptr++;
3850 switch (*ptr) {
3851 case 'n':
3852 trace_seq_putc(s, '\n');
3853 break;
3854 case 't':
3855 trace_seq_putc(s, '\t');
3856 break;
3857 case 'r':
3858 trace_seq_putc(s, '\r');
3859 break;
3860 case '\\':
3861 trace_seq_putc(s, '\\');
3862 break;
3863 default:
3864 trace_seq_putc(s, *ptr);
3865 break;
3866 }
3867
3868 } else if (*ptr == '%') {
3869 saveptr = ptr;
3870 show_func = 0;
3871 len_as_arg = 0;
3872 cont_process:
3873 ptr++;
3874 switch (*ptr) {
3875 case '%':
3876 trace_seq_putc(s, '%');
3877 break;
3878 case '#':
3879 /* FIXME: need to handle properly */
3880 goto cont_process;
3881 case 'h':
3882 ls--;
3883 goto cont_process;
3884 case 'l':
3885 ls++;
3886 goto cont_process;
3887 case 'L':
3888 ls = 2;
3889 goto cont_process;
3890 case '*':
3891 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003892 if (!arg) {
3893 do_warning("no argument match");
3894 event->flags |= EVENT_FL_FAILED;
3895 goto out_failed;
3896 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003897 len_arg = eval_num_arg(data, size, event, arg);
3898 len_as_arg = 1;
3899 arg = arg->next;
3900 goto cont_process;
3901 case '.':
3902 case 'z':
3903 case 'Z':
3904 case '0' ... '9':
3905 goto cont_process;
3906 case 'p':
3907 if (pevent->long_size == 4)
3908 ls = 1;
3909 else
3910 ls = 2;
3911
3912 if (*(ptr+1) == 'F' ||
3913 *(ptr+1) == 'f') {
3914 ptr++;
3915 show_func = *ptr;
3916 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3917 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3918 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05003919 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003920 break;
3921 }
3922
3923 /* fall through */
3924 case 'd':
3925 case 'i':
3926 case 'x':
3927 case 'X':
3928 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09003929 if (!arg) {
3930 do_warning("no argument match");
3931 event->flags |= EVENT_FL_FAILED;
3932 goto out_failed;
3933 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003934
3935 len = ((unsigned long)ptr + 1) -
3936 (unsigned long)saveptr;
3937
3938 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003939 if (len > 31) {
3940 do_warning("bad format!");
3941 event->flags |= EVENT_FL_FAILED;
3942 len = 31;
3943 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003944
3945 memcpy(format, saveptr, len);
3946 format[len] = 0;
3947
3948 val = eval_num_arg(data, size, event, arg);
3949 arg = arg->next;
3950
3951 if (show_func) {
3952 func = find_func(pevent, val);
3953 if (func) {
3954 trace_seq_puts(s, func->func);
3955 if (show_func == 'F')
3956 trace_seq_printf(s,
3957 "+0x%llx",
3958 val - func->addr);
3959 break;
3960 }
3961 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003962 if (pevent->long_size == 8 && ls &&
3963 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003964 char *p;
3965
3966 ls = 2;
3967 /* make %l into %ll */
3968 p = strchr(format, 'l');
3969 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003970 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003971 else if (strcmp(format, "%p") == 0)
3972 strcpy(format, "0x%llx");
3973 }
3974 switch (ls) {
3975 case -2:
3976 if (len_as_arg)
3977 trace_seq_printf(s, format, len_arg, (char)val);
3978 else
3979 trace_seq_printf(s, format, (char)val);
3980 break;
3981 case -1:
3982 if (len_as_arg)
3983 trace_seq_printf(s, format, len_arg, (short)val);
3984 else
3985 trace_seq_printf(s, format, (short)val);
3986 break;
3987 case 0:
3988 if (len_as_arg)
3989 trace_seq_printf(s, format, len_arg, (int)val);
3990 else
3991 trace_seq_printf(s, format, (int)val);
3992 break;
3993 case 1:
3994 if (len_as_arg)
3995 trace_seq_printf(s, format, len_arg, (long)val);
3996 else
3997 trace_seq_printf(s, format, (long)val);
3998 break;
3999 case 2:
4000 if (len_as_arg)
4001 trace_seq_printf(s, format, len_arg,
4002 (long long)val);
4003 else
4004 trace_seq_printf(s, format, (long long)val);
4005 break;
4006 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004007 do_warning("bad count (%d)", ls);
4008 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004009 }
4010 break;
4011 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004012 if (!arg) {
4013 do_warning("no matching argument");
4014 event->flags |= EVENT_FL_FAILED;
4015 goto out_failed;
4016 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004017
4018 len = ((unsigned long)ptr + 1) -
4019 (unsigned long)saveptr;
4020
4021 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004022 if (len > 31) {
4023 do_warning("bad format!");
4024 event->flags |= EVENT_FL_FAILED;
4025 len = 31;
4026 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004027
4028 memcpy(format, saveptr, len);
4029 format[len] = 0;
4030 if (!len_as_arg)
4031 len_arg = -1;
4032 print_str_arg(s, data, size, event,
4033 format, len_arg, arg);
4034 arg = arg->next;
4035 break;
4036 default:
4037 trace_seq_printf(s, ">%c<", *ptr);
4038
4039 }
4040 } else
4041 trace_seq_putc(s, *ptr);
4042 }
4043
Namhyung Kim245c5a12012-09-07 11:49:45 +09004044 if (event->flags & EVENT_FL_FAILED) {
4045out_failed:
4046 trace_seq_printf(s, "[FAILED TO PARSE]");
4047 }
4048
Steven Rostedtf7d82352012-04-06 00:47:53 +02004049 if (args) {
4050 free_args(args);
4051 free(bprint_fmt);
4052 }
4053}
4054
4055/**
4056 * pevent_data_lat_fmt - parse the data for the latency format
4057 * @pevent: a handle to the pevent
4058 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004059 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004060 *
4061 * This parses out the Latency format (interrupts disabled,
4062 * need rescheduling, in hard/soft interrupt, preempt count
4063 * and lock depth) and places it into the trace_seq.
4064 */
4065void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004066 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004067{
4068 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004069 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004070 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004071 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004072 unsigned int lat_flags;
4073 unsigned int pc;
4074 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004075 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004076 int hardirq;
4077 int softirq;
4078 void *data = record->data;
4079
4080 lat_flags = parse_common_flags(pevent, data);
4081 pc = parse_common_pc(pevent, data);
4082 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004083 if (lock_depth_exists)
4084 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004085 else if (check_lock_depth) {
4086 lock_depth = parse_common_lock_depth(pevent, data);
4087 if (lock_depth < 0)
4088 check_lock_depth = 0;
4089 else
4090 lock_depth_exists = 1;
4091 }
4092
4093 /* migrate_disable may not always exist */
4094 if (migrate_disable_exists)
4095 migrate_disable = parse_common_migrate_disable(pevent, data);
4096 else if (check_migrate_disable) {
4097 migrate_disable = parse_common_migrate_disable(pevent, data);
4098 if (migrate_disable < 0)
4099 check_migrate_disable = 0;
4100 else
4101 migrate_disable_exists = 1;
4102 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004103
4104 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4105 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4106
4107 trace_seq_printf(s, "%c%c%c",
4108 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4109 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4110 'X' : '.',
4111 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4112 'N' : '.',
4113 (hardirq && softirq) ? 'H' :
4114 hardirq ? 'h' : softirq ? 's' : '.');
4115
4116 if (pc)
4117 trace_seq_printf(s, "%x", pc);
4118 else
4119 trace_seq_putc(s, '.');
4120
Steven Rostedt0866a972012-05-22 14:52:40 +09004121 if (migrate_disable_exists) {
4122 if (migrate_disable < 0)
4123 trace_seq_putc(s, '.');
4124 else
4125 trace_seq_printf(s, "%d", migrate_disable);
4126 }
4127
Steven Rostedtf7d82352012-04-06 00:47:53 +02004128 if (lock_depth_exists) {
4129 if (lock_depth < 0)
4130 trace_seq_putc(s, '.');
4131 else
4132 trace_seq_printf(s, "%d", lock_depth);
4133 }
4134
4135 trace_seq_terminate(s);
4136}
4137
4138/**
4139 * pevent_data_type - parse out the given event type
4140 * @pevent: a handle to the pevent
4141 * @rec: the record to read from
4142 *
4143 * This returns the event id from the @rec.
4144 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004145int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004146{
4147 return trace_parse_common_type(pevent, rec->data);
4148}
4149
4150/**
4151 * pevent_data_event_from_type - find the event by a given type
4152 * @pevent: a handle to the pevent
4153 * @type: the type of the event.
4154 *
4155 * This returns the event form a given @type;
4156 */
4157struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4158{
4159 return pevent_find_event(pevent, type);
4160}
4161
4162/**
4163 * pevent_data_pid - parse the PID from raw data
4164 * @pevent: a handle to the pevent
4165 * @rec: the record to parse
4166 *
4167 * This returns the PID from a raw data.
4168 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004169int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004170{
4171 return parse_common_pid(pevent, rec->data);
4172}
4173
4174/**
4175 * pevent_data_comm_from_pid - return the command line from PID
4176 * @pevent: a handle to the pevent
4177 * @pid: the PID of the task to search for
4178 *
4179 * This returns a pointer to the command line that has the given
4180 * @pid.
4181 */
4182const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4183{
4184 const char *comm;
4185
4186 comm = find_cmdline(pevent, pid);
4187 return comm;
4188}
4189
4190/**
4191 * pevent_data_comm_from_pid - parse the data into the print format
4192 * @s: the trace_seq to write to
4193 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004194 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004195 *
4196 * This parses the raw @data using the given @event information and
4197 * writes the print format into the trace_seq.
4198 */
4199void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004200 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004201{
4202 int print_pretty = 1;
4203
4204 if (event->pevent->print_raw)
4205 print_event_fields(s, record->data, record->size, event);
4206 else {
4207
4208 if (event->handler)
4209 print_pretty = event->handler(s, record, event,
4210 event->context);
4211
4212 if (print_pretty)
4213 pretty_print(s, record->data, record->size, event);
4214 }
4215
4216 trace_seq_terminate(s);
4217}
4218
4219void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Steven Rostedt1c698182012-04-06 00:48:06 +02004220 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004221{
4222 static char *spaces = " "; /* 20 spaces */
4223 struct event_format *event;
4224 unsigned long secs;
4225 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004226 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004227 const char *comm;
4228 void *data = record->data;
4229 int type;
4230 int pid;
4231 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004232 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004233
4234 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004235 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004236
4237 if (record->size < 0) {
4238 do_warning("ug! negative record size %d", record->size);
4239 return;
4240 }
4241
4242 type = trace_parse_common_type(pevent, data);
4243
4244 event = pevent_find_event(pevent, type);
4245 if (!event) {
4246 do_warning("ug! no event found for type %d", type);
4247 return;
4248 }
4249
4250 pid = parse_common_pid(pevent, data);
4251 comm = find_cmdline(pevent, pid);
4252
4253 if (pevent->latency_format) {
4254 trace_seq_printf(s, "%8.8s-%-5d %3d",
4255 comm, pid, record->cpu);
4256 pevent_data_lat_fmt(pevent, s, record);
4257 } else
4258 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4259
Steven Rostedt4dc10242012-04-06 00:47:57 +02004260 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4261 usecs = nsecs;
4262 p = 9;
4263 } else {
4264 usecs = (nsecs + 500) / NSECS_PER_USEC;
4265 p = 6;
4266 }
4267
4268 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004269
4270 /* Space out the event names evenly. */
4271 len = strlen(event->name);
4272 if (len < 20)
4273 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4274
4275 pevent_event_info(s, event, record);
4276}
4277
4278static int events_id_cmp(const void *a, const void *b)
4279{
4280 struct event_format * const * ea = a;
4281 struct event_format * const * eb = b;
4282
4283 if ((*ea)->id < (*eb)->id)
4284 return -1;
4285
4286 if ((*ea)->id > (*eb)->id)
4287 return 1;
4288
4289 return 0;
4290}
4291
4292static int events_name_cmp(const void *a, const void *b)
4293{
4294 struct event_format * const * ea = a;
4295 struct event_format * const * eb = b;
4296 int res;
4297
4298 res = strcmp((*ea)->name, (*eb)->name);
4299 if (res)
4300 return res;
4301
4302 res = strcmp((*ea)->system, (*eb)->system);
4303 if (res)
4304 return res;
4305
4306 return events_id_cmp(a, b);
4307}
4308
4309static int events_system_cmp(const void *a, const void *b)
4310{
4311 struct event_format * const * ea = a;
4312 struct event_format * const * eb = b;
4313 int res;
4314
4315 res = strcmp((*ea)->system, (*eb)->system);
4316 if (res)
4317 return res;
4318
4319 res = strcmp((*ea)->name, (*eb)->name);
4320 if (res)
4321 return res;
4322
4323 return events_id_cmp(a, b);
4324}
4325
4326struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4327{
4328 struct event_format **events;
4329 int (*sort)(const void *a, const void *b);
4330
4331 events = pevent->sort_events;
4332
4333 if (events && pevent->last_type == sort_type)
4334 return events;
4335
4336 if (!events) {
4337 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4338 if (!events)
4339 return NULL;
4340
4341 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4342 events[pevent->nr_events] = NULL;
4343
4344 pevent->sort_events = events;
4345
4346 /* the internal events are sorted by id */
4347 if (sort_type == EVENT_SORT_ID) {
4348 pevent->last_type = sort_type;
4349 return events;
4350 }
4351 }
4352
4353 switch (sort_type) {
4354 case EVENT_SORT_ID:
4355 sort = events_id_cmp;
4356 break;
4357 case EVENT_SORT_NAME:
4358 sort = events_name_cmp;
4359 break;
4360 case EVENT_SORT_SYSTEM:
4361 sort = events_system_cmp;
4362 break;
4363 default:
4364 return events;
4365 }
4366
4367 qsort(events, pevent->nr_events, sizeof(*events), sort);
4368 pevent->last_type = sort_type;
4369
4370 return events;
4371}
4372
4373static struct format_field **
4374get_event_fields(const char *type, const char *name,
4375 int count, struct format_field *list)
4376{
4377 struct format_field **fields;
4378 struct format_field *field;
4379 int i = 0;
4380
4381 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4382 for (field = list; field; field = field->next) {
4383 fields[i++] = field;
4384 if (i == count + 1) {
4385 do_warning("event %s has more %s fields than specified",
4386 name, type);
4387 i--;
4388 break;
4389 }
4390 }
4391
4392 if (i != count)
4393 do_warning("event %s has less %s fields than specified",
4394 name, type);
4395
4396 fields[i] = NULL;
4397
4398 return fields;
4399}
4400
4401/**
4402 * pevent_event_common_fields - return a list of common fields for an event
4403 * @event: the event to return the common fields of.
4404 *
4405 * Returns an allocated array of fields. The last item in the array is NULL.
4406 * The array must be freed with free().
4407 */
4408struct format_field **pevent_event_common_fields(struct event_format *event)
4409{
4410 return get_event_fields("common", event->name,
4411 event->format.nr_common,
4412 event->format.common_fields);
4413}
4414
4415/**
4416 * pevent_event_fields - return a list of event specific fields for an event
4417 * @event: the event to return the fields of.
4418 *
4419 * Returns an allocated array of fields. The last item in the array is NULL.
4420 * The array must be freed with free().
4421 */
4422struct format_field **pevent_event_fields(struct event_format *event)
4423{
4424 return get_event_fields("event", event->name,
4425 event->format.nr_fields,
4426 event->format.fields);
4427}
4428
4429static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4430{
4431 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4432 if (field->next) {
4433 trace_seq_puts(s, ", ");
4434 print_fields(s, field->next);
4435 }
4436}
4437
4438/* for debugging */
4439static void print_args(struct print_arg *args)
4440{
4441 int print_paren = 1;
4442 struct trace_seq s;
4443
4444 switch (args->type) {
4445 case PRINT_NULL:
4446 printf("null");
4447 break;
4448 case PRINT_ATOM:
4449 printf("%s", args->atom.atom);
4450 break;
4451 case PRINT_FIELD:
4452 printf("REC->%s", args->field.name);
4453 break;
4454 case PRINT_FLAGS:
4455 printf("__print_flags(");
4456 print_args(args->flags.field);
4457 printf(", %s, ", args->flags.delim);
4458 trace_seq_init(&s);
4459 print_fields(&s, args->flags.flags);
4460 trace_seq_do_printf(&s);
4461 trace_seq_destroy(&s);
4462 printf(")");
4463 break;
4464 case PRINT_SYMBOL:
4465 printf("__print_symbolic(");
4466 print_args(args->symbol.field);
4467 printf(", ");
4468 trace_seq_init(&s);
4469 print_fields(&s, args->symbol.symbols);
4470 trace_seq_do_printf(&s);
4471 trace_seq_destroy(&s);
4472 printf(")");
4473 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004474 case PRINT_HEX:
4475 printf("__print_hex(");
4476 print_args(args->hex.field);
4477 printf(", ");
4478 print_args(args->hex.size);
4479 printf(")");
4480 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004481 case PRINT_STRING:
4482 case PRINT_BSTRING:
4483 printf("__get_str(%s)", args->string.string);
4484 break;
4485 case PRINT_TYPE:
4486 printf("(%s)", args->typecast.type);
4487 print_args(args->typecast.item);
4488 break;
4489 case PRINT_OP:
4490 if (strcmp(args->op.op, ":") == 0)
4491 print_paren = 0;
4492 if (print_paren)
4493 printf("(");
4494 print_args(args->op.left);
4495 printf(" %s ", args->op.op);
4496 print_args(args->op.right);
4497 if (print_paren)
4498 printf(")");
4499 break;
4500 default:
4501 /* we should warn... */
4502 return;
4503 }
4504 if (args->next) {
4505 printf("\n");
4506 print_args(args->next);
4507 }
4508}
4509
4510static void parse_header_field(const char *field,
4511 int *offset, int *size, int mandatory)
4512{
4513 unsigned long long save_input_buf_ptr;
4514 unsigned long long save_input_buf_siz;
4515 char *token;
4516 int type;
4517
4518 save_input_buf_ptr = input_buf_ptr;
4519 save_input_buf_siz = input_buf_siz;
4520
4521 if (read_expected(EVENT_ITEM, "field") < 0)
4522 return;
4523 if (read_expected(EVENT_OP, ":") < 0)
4524 return;
4525
4526 /* type */
4527 if (read_expect_type(EVENT_ITEM, &token) < 0)
4528 goto fail;
4529 free_token(token);
4530
4531 /*
4532 * If this is not a mandatory field, then test it first.
4533 */
4534 if (mandatory) {
4535 if (read_expected(EVENT_ITEM, field) < 0)
4536 return;
4537 } else {
4538 if (read_expect_type(EVENT_ITEM, &token) < 0)
4539 goto fail;
4540 if (strcmp(token, field) != 0)
4541 goto discard;
4542 free_token(token);
4543 }
4544
4545 if (read_expected(EVENT_OP, ";") < 0)
4546 return;
4547 if (read_expected(EVENT_ITEM, "offset") < 0)
4548 return;
4549 if (read_expected(EVENT_OP, ":") < 0)
4550 return;
4551 if (read_expect_type(EVENT_ITEM, &token) < 0)
4552 goto fail;
4553 *offset = atoi(token);
4554 free_token(token);
4555 if (read_expected(EVENT_OP, ";") < 0)
4556 return;
4557 if (read_expected(EVENT_ITEM, "size") < 0)
4558 return;
4559 if (read_expected(EVENT_OP, ":") < 0)
4560 return;
4561 if (read_expect_type(EVENT_ITEM, &token) < 0)
4562 goto fail;
4563 *size = atoi(token);
4564 free_token(token);
4565 if (read_expected(EVENT_OP, ";") < 0)
4566 return;
4567 type = read_token(&token);
4568 if (type != EVENT_NEWLINE) {
4569 /* newer versions of the kernel have a "signed" type */
4570 if (type != EVENT_ITEM)
4571 goto fail;
4572
4573 if (strcmp(token, "signed") != 0)
4574 goto fail;
4575
4576 free_token(token);
4577
4578 if (read_expected(EVENT_OP, ":") < 0)
4579 return;
4580
4581 if (read_expect_type(EVENT_ITEM, &token))
4582 goto fail;
4583
4584 free_token(token);
4585 if (read_expected(EVENT_OP, ";") < 0)
4586 return;
4587
4588 if (read_expect_type(EVENT_NEWLINE, &token))
4589 goto fail;
4590 }
4591 fail:
4592 free_token(token);
4593 return;
4594
4595 discard:
4596 input_buf_ptr = save_input_buf_ptr;
4597 input_buf_siz = save_input_buf_siz;
4598 *offset = 0;
4599 *size = 0;
4600 free_token(token);
4601}
4602
4603/**
4604 * pevent_parse_header_page - parse the data stored in the header page
4605 * @pevent: the handle to the pevent
4606 * @buf: the buffer storing the header page format string
4607 * @size: the size of @buf
4608 * @long_size: the long size to use if there is no header
4609 *
4610 * This parses the header page format for information on the
4611 * ring buffer used. The @buf should be copied from
4612 *
4613 * /sys/kernel/debug/tracing/events/header_page
4614 */
4615int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4616 int long_size)
4617{
4618 int ignore;
4619
4620 if (!size) {
4621 /*
4622 * Old kernels did not have header page info.
4623 * Sorry but we just use what we find here in user space.
4624 */
4625 pevent->header_page_ts_size = sizeof(long long);
4626 pevent->header_page_size_size = long_size;
4627 pevent->header_page_data_offset = sizeof(long long) + long_size;
4628 pevent->old_format = 1;
4629 return -1;
4630 }
4631 init_input_buf(buf, size);
4632
4633 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4634 &pevent->header_page_ts_size, 1);
4635 parse_header_field("commit", &pevent->header_page_size_offset,
4636 &pevent->header_page_size_size, 1);
4637 parse_header_field("overwrite", &pevent->header_page_overwrite,
4638 &ignore, 0);
4639 parse_header_field("data", &pevent->header_page_data_offset,
4640 &pevent->header_page_data_size, 1);
4641
4642 return 0;
4643}
4644
4645static int event_matches(struct event_format *event,
4646 int id, const char *sys_name,
4647 const char *event_name)
4648{
4649 if (id >= 0 && id != event->id)
4650 return 0;
4651
4652 if (event_name && (strcmp(event_name, event->name) != 0))
4653 return 0;
4654
4655 if (sys_name && (strcmp(sys_name, event->system) != 0))
4656 return 0;
4657
4658 return 1;
4659}
4660
4661static void free_handler(struct event_handler *handle)
4662{
4663 free((void *)handle->sys_name);
4664 free((void *)handle->event_name);
4665 free(handle);
4666}
4667
4668static int find_event_handle(struct pevent *pevent, struct event_format *event)
4669{
4670 struct event_handler *handle, **next;
4671
4672 for (next = &pevent->handlers; *next;
4673 next = &(*next)->next) {
4674 handle = *next;
4675 if (event_matches(event, handle->id,
4676 handle->sys_name,
4677 handle->event_name))
4678 break;
4679 }
4680
4681 if (!(*next))
4682 return 0;
4683
4684 pr_stat("overriding event (%d) %s:%s with new print handler",
4685 event->id, event->system, event->name);
4686
4687 event->handler = handle->func;
4688 event->context = handle->context;
4689
4690 *next = handle->next;
4691 free_handler(handle);
4692
4693 return 1;
4694}
4695
4696/**
4697 * pevent_parse_event - parse the event format
4698 * @pevent: the handle to the pevent
4699 * @buf: the buffer storing the event format string
4700 * @size: the size of @buf
4701 * @sys: the system the event belongs to
4702 *
4703 * This parses the event format and creates an event structure
4704 * to quickly parse raw data for a given event.
4705 *
4706 * These files currently come from:
4707 *
4708 * /sys/kernel/debug/tracing/events/.../.../format
4709 */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004710enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4711 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004712{
4713 struct event_format *event;
4714 int ret;
4715
4716 init_input_buf(buf, size);
4717
4718 event = alloc_event();
4719 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09004720 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004721
4722 event->name = event_read_name();
4723 if (!event->name) {
4724 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004725 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4726 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004727 }
4728
4729 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004730 event->flags |= EVENT_FL_ISFTRACE;
4731
4732 if (strcmp(event->name, "bprint") == 0)
4733 event->flags |= EVENT_FL_ISBPRINT;
4734 }
4735
4736 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09004737 if (event->id < 0) {
4738 ret = PEVENT_ERRNO__READ_ID_FAILED;
4739 /*
4740 * This isn't an allocation error actually.
4741 * But as the ID is critical, just bail out.
4742 */
4743 goto event_alloc_failed;
4744 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004745
4746 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004747 if (!event->system) {
4748 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4749 goto event_alloc_failed;
4750 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004751
4752 /* Add pevent to event so that it can be referenced */
4753 event->pevent = pevent;
4754
4755 ret = event_read_format(event);
4756 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09004757 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4758 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004759 }
4760
4761 /*
4762 * If the event has an override, don't print warnings if the event
4763 * print format fails to parse.
4764 */
4765 if (find_event_handle(pevent, event))
4766 show_warning = 0;
4767
4768 ret = event_read_print(event);
4769 if (ret < 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004770 show_warning = 1;
Namhyung Kimbffddff2012-08-22 16:00:29 +09004771 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4772 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004773 }
4774 show_warning = 1;
4775
4776 add_event(pevent, event);
4777
4778 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4779 struct format_field *field;
4780 struct print_arg *arg, **list;
4781
4782 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004783 list = &event->print_fmt.args;
4784 for (field = event->format.fields; field; field = field->next) {
4785 arg = alloc_arg();
Steven Rostedtf7d82352012-04-06 00:47:53 +02004786 arg->type = PRINT_FIELD;
4787 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09004788 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09004789 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004790 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004791 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09004792 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004793 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004794 *list = arg;
4795 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004796 }
4797 return 0;
4798 }
4799
4800#define PRINT_ARGS 0
4801 if (PRINT_ARGS && event->print_fmt.args)
4802 print_args(event->print_fmt.args);
4803
4804 return 0;
4805
Namhyung Kimbffddff2012-08-22 16:00:29 +09004806 event_parse_failed:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004807 event->flags |= EVENT_FL_FAILED;
4808 /* still add it even if it failed */
4809 add_event(pevent, event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004810 return ret;
4811
4812 event_alloc_failed:
4813 free(event->system);
4814 free(event->name);
4815 free(event);
4816 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004817}
4818
Namhyung Kim2f197b92012-08-22 16:00:30 +09004819#undef _PE
4820#define _PE(code, str) str
4821static const char * const pevent_error_str[] = {
4822 PEVENT_ERRORS
4823};
4824#undef _PE
4825
4826int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4827 char *buf, size_t buflen)
4828{
4829 int idx;
4830 const char *msg;
4831
4832 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004833 msg = strerror_r(errnum, buf, buflen);
4834 if (msg != buf) {
4835 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03004836 memcpy(buf, msg, min(buflen - 1, len));
4837 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004838 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09004839 return 0;
4840 }
4841
4842 if (errnum <= __PEVENT_ERRNO__START ||
4843 errnum >= __PEVENT_ERRNO__END)
4844 return -1;
4845
Namhyung Kimf63fe792012-08-23 16:37:00 +09004846 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09004847 msg = pevent_error_str[idx];
4848
4849 switch (errnum) {
4850 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4851 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4852 case PEVENT_ERRNO__READ_ID_FAILED:
4853 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4854 case PEVENT_ERRNO__READ_PRINT_FAILED:
4855 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4856 snprintf(buf, buflen, "%s", msg);
4857 break;
4858
4859 default:
4860 /* cannot reach here */
4861 break;
4862 }
4863
4864 return 0;
4865}
4866
Steven Rostedtf7d82352012-04-06 00:47:53 +02004867int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02004868 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004869 unsigned long long *val, int err)
4870{
4871 if (!field) {
4872 if (err)
4873 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4874 return -1;
4875 }
4876
4877 if (pevent_read_number_field(field, record->data, val)) {
4878 if (err)
4879 trace_seq_printf(s, " %s=INVALID", name);
4880 return -1;
4881 }
4882
4883 return 0;
4884}
4885
4886/**
4887 * pevent_get_field_raw - return the raw pointer into the data field
4888 * @s: The seq to print to on error
4889 * @event: the event that the field is for
4890 * @name: The name of the field
4891 * @record: The record with the field name.
4892 * @len: place to store the field length.
4893 * @err: print default error if failed.
4894 *
4895 * Returns a pointer into record->data of the field and places
4896 * the length of the field in @len.
4897 *
4898 * On failure, it returns NULL.
4899 */
4900void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004901 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004902 int *len, int err)
4903{
4904 struct format_field *field;
4905 void *data = record->data;
4906 unsigned offset;
4907 int dummy;
4908
4909 if (!event)
4910 return NULL;
4911
4912 field = pevent_find_field(event, name);
4913
4914 if (!field) {
4915 if (err)
4916 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4917 return NULL;
4918 }
4919
4920 /* Allow @len to be NULL */
4921 if (!len)
4922 len = &dummy;
4923
4924 offset = field->offset;
4925 if (field->flags & FIELD_IS_DYNAMIC) {
4926 offset = pevent_read_number(event->pevent,
4927 data + offset, field->size);
4928 *len = offset >> 16;
4929 offset &= 0xffff;
4930 } else
4931 *len = field->size;
4932
4933 return data + offset;
4934}
4935
4936/**
4937 * pevent_get_field_val - find a field and return its value
4938 * @s: The seq to print to on error
4939 * @event: the event that the field is for
4940 * @name: The name of the field
4941 * @record: The record with the field name.
4942 * @val: place to store the value of the field.
4943 * @err: print default error if failed.
4944 *
4945 * Returns 0 on success -1 on field not found.
4946 */
4947int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004948 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004949 unsigned long long *val, int err)
4950{
4951 struct format_field *field;
4952
4953 if (!event)
4954 return -1;
4955
4956 field = pevent_find_field(event, name);
4957
4958 return get_field_val(s, field, name, record, val, err);
4959}
4960
4961/**
4962 * pevent_get_common_field_val - find a common field and return its value
4963 * @s: The seq to print to on error
4964 * @event: the event that the field is for
4965 * @name: The name of the field
4966 * @record: The record with the field name.
4967 * @val: place to store the value of the field.
4968 * @err: print default error if failed.
4969 *
4970 * Returns 0 on success -1 on field not found.
4971 */
4972int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004973 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004974 unsigned long long *val, int err)
4975{
4976 struct format_field *field;
4977
4978 if (!event)
4979 return -1;
4980
4981 field = pevent_find_common_field(event, name);
4982
4983 return get_field_val(s, field, name, record, val, err);
4984}
4985
4986/**
4987 * pevent_get_any_field_val - find a any field and return its value
4988 * @s: The seq to print to on error
4989 * @event: the event that the field is for
4990 * @name: The name of the field
4991 * @record: The record with the field name.
4992 * @val: place to store the value of the field.
4993 * @err: print default error if failed.
4994 *
4995 * Returns 0 on success -1 on field not found.
4996 */
4997int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004998 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004999 unsigned long long *val, int err)
5000{
5001 struct format_field *field;
5002
5003 if (!event)
5004 return -1;
5005
5006 field = pevent_find_any_field(event, name);
5007
5008 return get_field_val(s, field, name, record, val, err);
5009}
5010
5011/**
5012 * pevent_print_num_field - print a field and a format
5013 * @s: The seq to print to
5014 * @fmt: The printf format to print the field with.
5015 * @event: the event that the field is for
5016 * @name: The name of the field
5017 * @record: The record with the field name.
5018 * @err: print default error if failed.
5019 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005020 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005021 */
5022int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5023 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005024 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005025{
5026 struct format_field *field = pevent_find_field(event, name);
5027 unsigned long long val;
5028
5029 if (!field)
5030 goto failed;
5031
5032 if (pevent_read_number_field(field, record->data, &val))
5033 goto failed;
5034
5035 return trace_seq_printf(s, fmt, val);
5036
5037 failed:
5038 if (err)
5039 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5040 return -1;
5041}
5042
5043static void free_func_handle(struct pevent_function_handler *func)
5044{
5045 struct pevent_func_params *params;
5046
5047 free(func->name);
5048
5049 while (func->params) {
5050 params = func->params;
5051 func->params = params->next;
5052 free(params);
5053 }
5054
5055 free(func);
5056}
5057
5058/**
5059 * pevent_register_print_function - register a helper function
5060 * @pevent: the handle to the pevent
5061 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005062 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005063 * @name: the name of the helper function
5064 * @parameters: A list of enum pevent_func_arg_type
5065 *
5066 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005067 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005068 * of these functions.
5069 *
5070 * The @parameters is a variable list of pevent_func_arg_type enums that
5071 * must end with PEVENT_FUNC_ARG_VOID.
5072 */
5073int pevent_register_print_function(struct pevent *pevent,
5074 pevent_func_handler func,
5075 enum pevent_func_arg_type ret_type,
5076 char *name, ...)
5077{
5078 struct pevent_function_handler *func_handle;
5079 struct pevent_func_params **next_param;
5080 struct pevent_func_params *param;
5081 enum pevent_func_arg_type type;
5082 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005083 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005084
5085 func_handle = find_func_handler(pevent, name);
5086 if (func_handle) {
5087 /*
5088 * This is most like caused by the users own
5089 * plugins updating the function. This overrides the
5090 * system defaults.
5091 */
5092 pr_stat("override of function helper '%s'", name);
5093 remove_func_handler(pevent, name);
5094 }
5095
Namhyung Kim67ed9392012-09-07 11:49:47 +09005096 func_handle = malloc(sizeof(*func_handle));
5097 if (!func_handle) {
5098 do_warning("Failed to allocate function handler");
5099 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5100 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005101 memset(func_handle, 0, sizeof(*func_handle));
5102
5103 func_handle->ret_type = ret_type;
5104 func_handle->name = strdup(name);
5105 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005106 if (!func_handle->name) {
5107 do_warning("Failed to allocate function name");
5108 free(func_handle);
5109 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5110 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005111
5112 next_param = &(func_handle->params);
5113 va_start(ap, name);
5114 for (;;) {
5115 type = va_arg(ap, enum pevent_func_arg_type);
5116 if (type == PEVENT_FUNC_ARG_VOID)
5117 break;
5118
5119 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005120 do_warning("Invalid argument type %d", type);
5121 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005122 goto out_free;
5123 }
5124
Namhyung Kim67ed9392012-09-07 11:49:47 +09005125 param = malloc(sizeof(*param));
5126 if (!param) {
5127 do_warning("Failed to allocate function param");
5128 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5129 goto out_free;
5130 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005131 param->type = type;
5132 param->next = NULL;
5133
5134 *next_param = param;
5135 next_param = &(param->next);
5136
5137 func_handle->nr_args++;
5138 }
5139 va_end(ap);
5140
5141 func_handle->next = pevent->func_handlers;
5142 pevent->func_handlers = func_handle;
5143
5144 return 0;
5145 out_free:
5146 va_end(ap);
5147 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005148 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005149}
5150
5151/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005152 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005153 * @pevent: the handle to the pevent
5154 * @id: the id of the event to register
5155 * @sys_name: the system name the event belongs to
5156 * @event_name: the name of the event
5157 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005158 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005159 *
5160 * This function allows a developer to override the parsing of
5161 * a given event. If for some reason the default print format
5162 * is not sufficient, this function will register a function
5163 * for an event to be used to parse the data instead.
5164 *
5165 * If @id is >= 0, then it is used to find the event.
5166 * else @sys_name and @event_name are used.
5167 */
5168int pevent_register_event_handler(struct pevent *pevent,
5169 int id, char *sys_name, char *event_name,
5170 pevent_event_handler_func func,
5171 void *context)
5172{
5173 struct event_format *event;
5174 struct event_handler *handle;
5175
5176 if (id >= 0) {
5177 /* search by id */
5178 event = pevent_find_event(pevent, id);
5179 if (!event)
5180 goto not_found;
5181 if (event_name && (strcmp(event_name, event->name) != 0))
5182 goto not_found;
5183 if (sys_name && (strcmp(sys_name, event->system) != 0))
5184 goto not_found;
5185 } else {
5186 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5187 if (!event)
5188 goto not_found;
5189 }
5190
5191 pr_stat("overriding event (%d) %s:%s with new print handler",
5192 event->id, event->system, event->name);
5193
5194 event->handler = func;
5195 event->context = context;
5196 return 0;
5197
5198 not_found:
5199 /* Save for later use. */
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005200 handle = malloc(sizeof(*handle));
5201 if (!handle) {
5202 do_warning("Failed to allocate event handler");
5203 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5204 }
5205
Steven Rostedt42c80132012-04-06 00:48:05 +02005206 memset(handle, 0, sizeof(*handle));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005207 handle->id = id;
5208 if (event_name)
5209 handle->event_name = strdup(event_name);
5210 if (sys_name)
5211 handle->sys_name = strdup(sys_name);
5212
Namhyung Kimca638582012-04-09 11:54:31 +09005213 if ((event_name && !handle->event_name) ||
5214 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005215 do_warning("Failed to allocate event/sys name");
5216 free((void *)handle->event_name);
5217 free((void *)handle->sys_name);
5218 free(handle);
5219 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005220 }
5221
Steven Rostedtf7d82352012-04-06 00:47:53 +02005222 handle->func = func;
5223 handle->next = pevent->handlers;
5224 pevent->handlers = handle;
5225 handle->context = context;
5226
5227 return -1;
5228}
5229
5230/**
5231 * pevent_alloc - create a pevent handle
5232 */
5233struct pevent *pevent_alloc(void)
5234{
5235 struct pevent *pevent;
5236
5237 pevent = malloc(sizeof(*pevent));
5238 if (!pevent)
5239 return NULL;
5240 memset(pevent, 0, sizeof(*pevent));
5241 pevent->ref_count = 1;
5242
5243 return pevent;
5244}
5245
5246void pevent_ref(struct pevent *pevent)
5247{
5248 pevent->ref_count++;
5249}
5250
5251static void free_format_fields(struct format_field *field)
5252{
5253 struct format_field *next;
5254
5255 while (field) {
5256 next = field->next;
5257 free(field->type);
5258 free(field->name);
5259 free(field);
5260 field = next;
5261 }
5262}
5263
5264static void free_formats(struct format *format)
5265{
5266 free_format_fields(format->common_fields);
5267 free_format_fields(format->fields);
5268}
5269
5270static void free_event(struct event_format *event)
5271{
5272 free(event->name);
5273 free(event->system);
5274
5275 free_formats(&event->format);
5276
5277 free(event->print_fmt.format);
5278 free_args(event->print_fmt.args);
5279
5280 free(event);
5281}
5282
5283/**
5284 * pevent_free - free a pevent handle
5285 * @pevent: the pevent handle to free
5286 */
5287void pevent_free(struct pevent *pevent)
5288{
Steven Rostedta2525a02012-04-06 00:48:02 +02005289 struct cmdline_list *cmdlist, *cmdnext;
5290 struct func_list *funclist, *funcnext;
5291 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005292 struct pevent_function_handler *func_handler;
5293 struct event_handler *handle;
5294 int i;
5295
Steven Rostedta2525a02012-04-06 00:48:02 +02005296 if (!pevent)
5297 return;
5298
5299 cmdlist = pevent->cmdlist;
5300 funclist = pevent->funclist;
5301 printklist = pevent->printklist;
5302
Steven Rostedtf7d82352012-04-06 00:47:53 +02005303 pevent->ref_count--;
5304 if (pevent->ref_count)
5305 return;
5306
5307 if (pevent->cmdlines) {
5308 for (i = 0; i < pevent->cmdline_count; i++)
5309 free(pevent->cmdlines[i].comm);
5310 free(pevent->cmdlines);
5311 }
5312
5313 while (cmdlist) {
5314 cmdnext = cmdlist->next;
5315 free(cmdlist->comm);
5316 free(cmdlist);
5317 cmdlist = cmdnext;
5318 }
5319
5320 if (pevent->func_map) {
5321 for (i = 0; i < pevent->func_count; i++) {
5322 free(pevent->func_map[i].func);
5323 free(pevent->func_map[i].mod);
5324 }
5325 free(pevent->func_map);
5326 }
5327
5328 while (funclist) {
5329 funcnext = funclist->next;
5330 free(funclist->func);
5331 free(funclist->mod);
5332 free(funclist);
5333 funclist = funcnext;
5334 }
5335
5336 while (pevent->func_handlers) {
5337 func_handler = pevent->func_handlers;
5338 pevent->func_handlers = func_handler->next;
5339 free_func_handle(func_handler);
5340 }
5341
5342 if (pevent->printk_map) {
5343 for (i = 0; i < pevent->printk_count; i++)
5344 free(pevent->printk_map[i].printk);
5345 free(pevent->printk_map);
5346 }
5347
5348 while (printklist) {
5349 printknext = printklist->next;
5350 free(printklist->printk);
5351 free(printklist);
5352 printklist = printknext;
5353 }
5354
5355 for (i = 0; i < pevent->nr_events; i++)
5356 free_event(pevent->events[i]);
5357
5358 while (pevent->handlers) {
5359 handle = pevent->handlers;
5360 pevent->handlers = handle->next;
5361 free_handler(handle);
5362 }
5363
5364 free(pevent->events);
5365 free(pevent->sort_events);
5366
5367 free(pevent);
5368}
5369
5370void pevent_unref(struct pevent *pevent)
5371{
5372 pevent_free(pevent);
5373}