blob: 2e05d567353ea0c6fa580637dced6b619c2da856 [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:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300830 if (asprintf(tok, "%c", ch) < 0)
831 return EVENT_ERROR;
832
Steven Rostedtf7d82352012-04-06 00:47:53 +0200833 return type;
834
835 case EVENT_OP:
836 switch (ch) {
837 case '-':
838 next_ch = __peek_char();
839 if (next_ch == '>') {
840 buf[i++] = __read_char();
841 break;
842 }
843 /* fall through */
844 case '+':
845 case '|':
846 case '&':
847 case '>':
848 case '<':
849 last_ch = ch;
850 ch = __peek_char();
851 if (ch != last_ch)
852 goto test_equal;
853 buf[i++] = __read_char();
854 switch (last_ch) {
855 case '>':
856 case '<':
857 goto test_equal;
858 default:
859 break;
860 }
861 break;
862 case '!':
863 case '=':
864 goto test_equal;
865 default: /* what should we do instead? */
866 break;
867 }
868 buf[i] = 0;
869 *tok = strdup(buf);
870 return type;
871
872 test_equal:
873 ch = __peek_char();
874 if (ch == '=')
875 buf[i++] = __read_char();
876 goto out;
877
878 case EVENT_DQUOTE:
879 case EVENT_SQUOTE:
880 /* don't keep quotes */
881 i--;
882 quote_ch = ch;
883 last_ch = 0;
884 concat:
885 do {
886 if (i == (BUFSIZ - 1)) {
887 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200888 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900889
890 if (extend_token(tok, buf, tok_size) < 0)
891 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200892 i = 0;
893 }
894 last_ch = ch;
895 ch = __read_char();
896 buf[i++] = ch;
897 /* the '\' '\' will cancel itself */
898 if (ch == '\\' && last_ch == '\\')
899 last_ch = 0;
900 } while (ch != quote_ch || last_ch == '\\');
901 /* remove the last quote */
902 i--;
903
904 /*
905 * For strings (double quotes) check the next token.
906 * If it is another string, concatinate the two.
907 */
908 if (type == EVENT_DQUOTE) {
909 unsigned long long save_input_buf_ptr = input_buf_ptr;
910
911 do {
912 ch = __read_char();
913 } while (isspace(ch));
914 if (ch == '"')
915 goto concat;
916 input_buf_ptr = save_input_buf_ptr;
917 }
918
919 goto out;
920
921 case EVENT_ERROR ... EVENT_SPACE:
922 case EVENT_ITEM:
923 default:
924 break;
925 }
926
927 while (get_type(__peek_char()) == type) {
928 if (i == (BUFSIZ - 1)) {
929 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200930 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900931
932 if (extend_token(tok, buf, tok_size) < 0)
933 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200934 i = 0;
935 }
936 ch = __read_char();
937 buf[i++] = ch;
938 }
939
940 out:
941 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900942 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200943 return EVENT_NONE;
944
945 if (type == EVENT_ITEM) {
946 /*
947 * Older versions of the kernel has a bug that
948 * creates invalid symbols and will break the mac80211
949 * parsing. This is a work around to that bug.
950 *
951 * See Linux kernel commit:
952 * 811cb50baf63461ce0bdb234927046131fc7fa8b
953 */
954 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
955 free(*tok);
956 *tok = NULL;
957 return force_token("\"\%s\" ", tok);
958 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
959 free(*tok);
960 *tok = NULL;
961 return force_token("\" sta:%pM\" ", tok);
962 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
963 free(*tok);
964 *tok = NULL;
965 return force_token("\" vif:%p(%d)\" ", tok);
966 }
967 }
968
969 return type;
970}
971
972static enum event_type force_token(const char *str, char **tok)
973{
974 const char *save_input_buf;
975 unsigned long long save_input_buf_ptr;
976 unsigned long long save_input_buf_siz;
977 enum event_type type;
978
979 /* save off the current input pointers */
980 save_input_buf = input_buf;
981 save_input_buf_ptr = input_buf_ptr;
982 save_input_buf_siz = input_buf_siz;
983
984 init_input_buf(str, strlen(str));
985
986 type = __read_token(tok);
987
988 /* reset back to original token */
989 input_buf = save_input_buf;
990 input_buf_ptr = save_input_buf_ptr;
991 input_buf_siz = save_input_buf_siz;
992
993 return type;
994}
995
996static void free_token(char *tok)
997{
998 if (tok)
999 free(tok);
1000}
1001
1002static enum event_type read_token(char **tok)
1003{
1004 enum event_type type;
1005
1006 for (;;) {
1007 type = __read_token(tok);
1008 if (type != EVENT_SPACE)
1009 return type;
1010
1011 free_token(*tok);
1012 }
1013
1014 /* not reached */
1015 *tok = NULL;
1016 return EVENT_NONE;
1017}
1018
1019/**
1020 * pevent_read_token - access to utilites to use the pevent parser
1021 * @tok: The token to return
1022 *
1023 * This will parse tokens from the string given by
1024 * pevent_init_data().
1025 *
1026 * Returns the token type.
1027 */
1028enum event_type pevent_read_token(char **tok)
1029{
1030 return read_token(tok);
1031}
1032
1033/**
1034 * pevent_free_token - free a token returned by pevent_read_token
1035 * @token: the token to free
1036 */
1037void pevent_free_token(char *token)
1038{
1039 free_token(token);
1040}
1041
1042/* no newline */
1043static enum event_type read_token_item(char **tok)
1044{
1045 enum event_type type;
1046
1047 for (;;) {
1048 type = __read_token(tok);
1049 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1050 return type;
1051 free_token(*tok);
1052 *tok = NULL;
1053 }
1054
1055 /* not reached */
1056 *tok = NULL;
1057 return EVENT_NONE;
1058}
1059
1060static int test_type(enum event_type type, enum event_type expect)
1061{
1062 if (type != expect) {
1063 do_warning("Error: expected type %d but read %d",
1064 expect, type);
1065 return -1;
1066 }
1067 return 0;
1068}
1069
1070static int test_type_token(enum event_type type, const char *token,
1071 enum event_type expect, const char *expect_tok)
1072{
1073 if (type != expect) {
1074 do_warning("Error: expected type %d but read %d",
1075 expect, type);
1076 return -1;
1077 }
1078
1079 if (strcmp(token, expect_tok) != 0) {
1080 do_warning("Error: expected '%s' but read '%s'",
1081 expect_tok, token);
1082 return -1;
1083 }
1084 return 0;
1085}
1086
1087static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1088{
1089 enum event_type type;
1090
1091 if (newline_ok)
1092 type = read_token(tok);
1093 else
1094 type = read_token_item(tok);
1095 return test_type(type, expect);
1096}
1097
1098static int read_expect_type(enum event_type expect, char **tok)
1099{
1100 return __read_expect_type(expect, tok, 1);
1101}
1102
1103static int __read_expected(enum event_type expect, const char *str,
1104 int newline_ok)
1105{
1106 enum event_type type;
1107 char *token;
1108 int ret;
1109
1110 if (newline_ok)
1111 type = read_token(&token);
1112 else
1113 type = read_token_item(&token);
1114
1115 ret = test_type_token(type, token, expect, str);
1116
1117 free_token(token);
1118
1119 return ret;
1120}
1121
1122static int read_expected(enum event_type expect, const char *str)
1123{
1124 return __read_expected(expect, str, 1);
1125}
1126
1127static int read_expected_item(enum event_type expect, const char *str)
1128{
1129 return __read_expected(expect, str, 0);
1130}
1131
1132static char *event_read_name(void)
1133{
1134 char *token;
1135
1136 if (read_expected(EVENT_ITEM, "name") < 0)
1137 return NULL;
1138
1139 if (read_expected(EVENT_OP, ":") < 0)
1140 return NULL;
1141
1142 if (read_expect_type(EVENT_ITEM, &token) < 0)
1143 goto fail;
1144
1145 return token;
1146
1147 fail:
1148 free_token(token);
1149 return NULL;
1150}
1151
1152static int event_read_id(void)
1153{
1154 char *token;
1155 int id;
1156
1157 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1158 return -1;
1159
1160 if (read_expected(EVENT_OP, ":") < 0)
1161 return -1;
1162
1163 if (read_expect_type(EVENT_ITEM, &token) < 0)
1164 goto fail;
1165
1166 id = strtoul(token, NULL, 0);
1167 free_token(token);
1168 return id;
1169
1170 fail:
1171 free_token(token);
1172 return -1;
1173}
1174
1175static int field_is_string(struct format_field *field)
1176{
1177 if ((field->flags & FIELD_IS_ARRAY) &&
1178 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1179 strstr(field->type, "s8")))
1180 return 1;
1181
1182 return 0;
1183}
1184
1185static int field_is_dynamic(struct format_field *field)
1186{
1187 if (strncmp(field->type, "__data_loc", 10) == 0)
1188 return 1;
1189
1190 return 0;
1191}
1192
1193static int field_is_long(struct format_field *field)
1194{
1195 /* includes long long */
1196 if (strstr(field->type, "long"))
1197 return 1;
1198
1199 return 0;
1200}
1201
1202static int event_read_fields(struct event_format *event, struct format_field **fields)
1203{
1204 struct format_field *field = NULL;
1205 enum event_type type;
1206 char *token;
1207 char *last_token;
1208 int count = 0;
1209
1210 do {
1211 type = read_token(&token);
1212 if (type == EVENT_NEWLINE) {
1213 free_token(token);
1214 return count;
1215 }
1216
1217 count++;
1218
1219 if (test_type_token(type, token, EVENT_ITEM, "field"))
1220 goto fail;
1221 free_token(token);
1222
1223 type = read_token(&token);
1224 /*
1225 * The ftrace fields may still use the "special" name.
1226 * Just ignore it.
1227 */
1228 if (event->flags & EVENT_FL_ISFTRACE &&
1229 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1230 free_token(token);
1231 type = read_token(&token);
1232 }
1233
1234 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1235 goto fail;
1236
1237 free_token(token);
1238 if (read_expect_type(EVENT_ITEM, &token) < 0)
1239 goto fail;
1240
1241 last_token = token;
1242
1243 field = malloc_or_die(sizeof(*field));
1244 memset(field, 0, sizeof(*field));
1245 field->event = event;
1246
1247 /* read the rest of the type */
1248 for (;;) {
1249 type = read_token(&token);
1250 if (type == EVENT_ITEM ||
1251 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1252 /*
1253 * Some of the ftrace fields are broken and have
1254 * an illegal "." in them.
1255 */
1256 (event->flags & EVENT_FL_ISFTRACE &&
1257 type == EVENT_OP && strcmp(token, ".") == 0)) {
1258
1259 if (strcmp(token, "*") == 0)
1260 field->flags |= FIELD_IS_POINTER;
1261
1262 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001263 char *new_type;
1264 new_type = realloc(field->type,
1265 strlen(field->type) +
1266 strlen(last_token) + 2);
1267 if (!new_type) {
1268 free(last_token);
1269 goto fail;
1270 }
1271 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001272 strcat(field->type, " ");
1273 strcat(field->type, last_token);
1274 free(last_token);
1275 } else
1276 field->type = last_token;
1277 last_token = token;
1278 continue;
1279 }
1280
1281 break;
1282 }
1283
1284 if (!field->type) {
1285 die("no type found");
1286 goto fail;
1287 }
1288 field->name = last_token;
1289
1290 if (test_type(type, EVENT_OP))
1291 goto fail;
1292
1293 if (strcmp(token, "[") == 0) {
1294 enum event_type last_type = type;
1295 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001296 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001297 int len;
1298
1299 field->flags |= FIELD_IS_ARRAY;
1300
1301 type = read_token(&token);
1302
1303 if (type == EVENT_ITEM)
1304 field->arraylen = strtoul(token, NULL, 0);
1305 else
1306 field->arraylen = 0;
1307
1308 while (strcmp(token, "]") != 0) {
1309 if (last_type == EVENT_ITEM &&
1310 type == EVENT_ITEM)
1311 len = 2;
1312 else
1313 len = 1;
1314 last_type = type;
1315
Namhyung Kimd2864472012-04-09 11:54:33 +09001316 new_brackets = realloc(brackets,
1317 strlen(brackets) +
1318 strlen(token) + len);
1319 if (!new_brackets) {
1320 free(brackets);
1321 goto fail;
1322 }
1323 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001324 if (len == 2)
1325 strcat(brackets, " ");
1326 strcat(brackets, token);
1327 /* We only care about the last token */
1328 field->arraylen = strtoul(token, NULL, 0);
1329 free_token(token);
1330 type = read_token(&token);
1331 if (type == EVENT_NONE) {
1332 die("failed to find token");
1333 goto fail;
1334 }
1335 }
1336
1337 free_token(token);
1338
Namhyung Kimd2864472012-04-09 11:54:33 +09001339 new_brackets = realloc(brackets, strlen(brackets) + 2);
1340 if (!new_brackets) {
1341 free(brackets);
1342 goto fail;
1343 }
1344 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001345 strcat(brackets, "]");
1346
1347 /* add brackets to type */
1348
1349 type = read_token(&token);
1350 /*
1351 * If the next token is not an OP, then it is of
1352 * the format: type [] item;
1353 */
1354 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001355 char *new_type;
1356 new_type = realloc(field->type,
1357 strlen(field->type) +
1358 strlen(field->name) +
1359 strlen(brackets) + 2);
1360 if (!new_type) {
1361 free(brackets);
1362 goto fail;
1363 }
1364 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001365 strcat(field->type, " ");
1366 strcat(field->type, field->name);
1367 free_token(field->name);
1368 strcat(field->type, brackets);
1369 field->name = token;
1370 type = read_token(&token);
1371 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001372 char *new_type;
1373 new_type = realloc(field->type,
1374 strlen(field->type) +
1375 strlen(brackets) + 1);
1376 if (!new_type) {
1377 free(brackets);
1378 goto fail;
1379 }
1380 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001381 strcat(field->type, brackets);
1382 }
1383 free(brackets);
1384 }
1385
1386 if (field_is_string(field))
1387 field->flags |= FIELD_IS_STRING;
1388 if (field_is_dynamic(field))
1389 field->flags |= FIELD_IS_DYNAMIC;
1390 if (field_is_long(field))
1391 field->flags |= FIELD_IS_LONG;
1392
1393 if (test_type_token(type, token, EVENT_OP, ";"))
1394 goto fail;
1395 free_token(token);
1396
1397 if (read_expected(EVENT_ITEM, "offset") < 0)
1398 goto fail_expect;
1399
1400 if (read_expected(EVENT_OP, ":") < 0)
1401 goto fail_expect;
1402
1403 if (read_expect_type(EVENT_ITEM, &token))
1404 goto fail;
1405 field->offset = strtoul(token, NULL, 0);
1406 free_token(token);
1407
1408 if (read_expected(EVENT_OP, ";") < 0)
1409 goto fail_expect;
1410
1411 if (read_expected(EVENT_ITEM, "size") < 0)
1412 goto fail_expect;
1413
1414 if (read_expected(EVENT_OP, ":") < 0)
1415 goto fail_expect;
1416
1417 if (read_expect_type(EVENT_ITEM, &token))
1418 goto fail;
1419 field->size = strtoul(token, NULL, 0);
1420 free_token(token);
1421
1422 if (read_expected(EVENT_OP, ";") < 0)
1423 goto fail_expect;
1424
1425 type = read_token(&token);
1426 if (type != EVENT_NEWLINE) {
1427 /* newer versions of the kernel have a "signed" type */
1428 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1429 goto fail;
1430
1431 free_token(token);
1432
1433 if (read_expected(EVENT_OP, ":") < 0)
1434 goto fail_expect;
1435
1436 if (read_expect_type(EVENT_ITEM, &token))
1437 goto fail;
1438
1439 /* add signed type */
1440
1441 free_token(token);
1442 if (read_expected(EVENT_OP, ";") < 0)
1443 goto fail_expect;
1444
1445 if (read_expect_type(EVENT_NEWLINE, &token))
1446 goto fail;
1447 }
1448
1449 free_token(token);
1450
1451 if (field->flags & FIELD_IS_ARRAY) {
1452 if (field->arraylen)
1453 field->elementsize = field->size / field->arraylen;
1454 else if (field->flags & FIELD_IS_STRING)
1455 field->elementsize = 1;
1456 else
1457 field->elementsize = event->pevent->long_size;
1458 } else
1459 field->elementsize = field->size;
1460
1461 *fields = field;
1462 fields = &field->next;
1463
1464 } while (1);
1465
1466 return 0;
1467
1468fail:
1469 free_token(token);
1470fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001471 if (field) {
1472 free(field->type);
1473 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001474 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001475 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001476 return -1;
1477}
1478
1479static int event_read_format(struct event_format *event)
1480{
1481 char *token;
1482 int ret;
1483
1484 if (read_expected_item(EVENT_ITEM, "format") < 0)
1485 return -1;
1486
1487 if (read_expected(EVENT_OP, ":") < 0)
1488 return -1;
1489
1490 if (read_expect_type(EVENT_NEWLINE, &token))
1491 goto fail;
1492 free_token(token);
1493
1494 ret = event_read_fields(event, &event->format.common_fields);
1495 if (ret < 0)
1496 return ret;
1497 event->format.nr_common = ret;
1498
1499 ret = event_read_fields(event, &event->format.fields);
1500 if (ret < 0)
1501 return ret;
1502 event->format.nr_fields = ret;
1503
1504 return 0;
1505
1506 fail:
1507 free_token(token);
1508 return -1;
1509}
1510
1511static enum event_type
1512process_arg_token(struct event_format *event, struct print_arg *arg,
1513 char **tok, enum event_type type);
1514
1515static enum event_type
1516process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1517{
1518 enum event_type type;
1519 char *token;
1520
1521 type = read_token(&token);
1522 *tok = token;
1523
1524 return process_arg_token(event, arg, tok, type);
1525}
1526
1527static enum event_type
1528process_op(struct event_format *event, struct print_arg *arg, char **tok);
1529
1530static enum event_type
1531process_cond(struct event_format *event, struct print_arg *top, char **tok)
1532{
1533 struct print_arg *arg, *left, *right;
1534 enum event_type type;
1535 char *token = NULL;
1536
1537 arg = alloc_arg();
1538 left = alloc_arg();
1539 right = alloc_arg();
1540
1541 arg->type = PRINT_OP;
1542 arg->op.left = left;
1543 arg->op.right = right;
1544
1545 *tok = NULL;
1546 type = process_arg(event, left, &token);
1547
1548 again:
1549 /* Handle other operations in the arguments */
1550 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1551 type = process_op(event, left, &token);
1552 goto again;
1553 }
1554
1555 if (test_type_token(type, token, EVENT_OP, ":"))
1556 goto out_free;
1557
1558 arg->op.op = token;
1559
1560 type = process_arg(event, right, &token);
1561
1562 top->op.right = arg;
1563
1564 *tok = token;
1565 return type;
1566
1567out_free:
1568 /* Top may point to itself */
1569 top->op.right = NULL;
1570 free_token(token);
1571 free_arg(arg);
1572 return EVENT_ERROR;
1573}
1574
1575static enum event_type
1576process_array(struct event_format *event, struct print_arg *top, char **tok)
1577{
1578 struct print_arg *arg;
1579 enum event_type type;
1580 char *token = NULL;
1581
1582 arg = alloc_arg();
1583
1584 *tok = NULL;
1585 type = process_arg(event, arg, &token);
1586 if (test_type_token(type, token, EVENT_OP, "]"))
1587 goto out_free;
1588
1589 top->op.right = arg;
1590
1591 free_token(token);
1592 type = read_token_item(&token);
1593 *tok = token;
1594
1595 return type;
1596
1597out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001598 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001599 free_arg(arg);
1600 return EVENT_ERROR;
1601}
1602
1603static int get_op_prio(char *op)
1604{
1605 if (!op[1]) {
1606 switch (op[0]) {
1607 case '~':
1608 case '!':
1609 return 4;
1610 case '*':
1611 case '/':
1612 case '%':
1613 return 6;
1614 case '+':
1615 case '-':
1616 return 7;
1617 /* '>>' and '<<' are 8 */
1618 case '<':
1619 case '>':
1620 return 9;
1621 /* '==' and '!=' are 10 */
1622 case '&':
1623 return 11;
1624 case '^':
1625 return 12;
1626 case '|':
1627 return 13;
1628 case '?':
1629 return 16;
1630 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001631 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001632 return -1;
1633 }
1634 } else {
1635 if (strcmp(op, "++") == 0 ||
1636 strcmp(op, "--") == 0) {
1637 return 3;
1638 } else if (strcmp(op, ">>") == 0 ||
1639 strcmp(op, "<<") == 0) {
1640 return 8;
1641 } else if (strcmp(op, ">=") == 0 ||
1642 strcmp(op, "<=") == 0) {
1643 return 9;
1644 } else if (strcmp(op, "==") == 0 ||
1645 strcmp(op, "!=") == 0) {
1646 return 10;
1647 } else if (strcmp(op, "&&") == 0) {
1648 return 14;
1649 } else if (strcmp(op, "||") == 0) {
1650 return 15;
1651 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001652 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001653 return -1;
1654 }
1655 }
1656}
1657
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001658static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001659{
1660
1661 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001662 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001663 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001664 else
1665 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001666
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001667 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001668}
1669
1670/* Note, *tok does not get freed, but will most likely be saved */
1671static enum event_type
1672process_op(struct event_format *event, struct print_arg *arg, char **tok)
1673{
1674 struct print_arg *left, *right = NULL;
1675 enum event_type type;
1676 char *token;
1677
1678 /* the op is passed in via tok */
1679 token = *tok;
1680
1681 if (arg->type == PRINT_OP && !arg->op.left) {
1682 /* handle single op */
1683 if (token[1]) {
1684 die("bad op token %s", token);
1685 goto out_free;
1686 }
1687 switch (token[0]) {
1688 case '~':
1689 case '!':
1690 case '+':
1691 case '-':
1692 break;
1693 default:
1694 do_warning("bad op token %s", token);
1695 goto out_free;
1696
1697 }
1698
1699 /* make an empty left */
1700 left = alloc_arg();
1701 left->type = PRINT_NULL;
1702 arg->op.left = left;
1703
1704 right = alloc_arg();
1705 arg->op.right = right;
1706
1707 /* do not free the token, it belongs to an op */
1708 *tok = NULL;
1709 type = process_arg(event, right, tok);
1710
1711 } else if (strcmp(token, "?") == 0) {
1712
1713 left = alloc_arg();
1714 /* copy the top arg to the left */
1715 *left = *arg;
1716
1717 arg->type = PRINT_OP;
1718 arg->op.op = token;
1719 arg->op.left = left;
1720 arg->op.prio = 0;
1721
Namhyung Kim41e51a22012-09-19 15:58:42 +09001722 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001723 type = process_cond(event, arg, tok);
1724
1725 } else if (strcmp(token, ">>") == 0 ||
1726 strcmp(token, "<<") == 0 ||
1727 strcmp(token, "&") == 0 ||
1728 strcmp(token, "|") == 0 ||
1729 strcmp(token, "&&") == 0 ||
1730 strcmp(token, "||") == 0 ||
1731 strcmp(token, "-") == 0 ||
1732 strcmp(token, "+") == 0 ||
1733 strcmp(token, "*") == 0 ||
1734 strcmp(token, "^") == 0 ||
1735 strcmp(token, "/") == 0 ||
1736 strcmp(token, "<") == 0 ||
1737 strcmp(token, ">") == 0 ||
1738 strcmp(token, "==") == 0 ||
1739 strcmp(token, "!=") == 0) {
1740
1741 left = alloc_arg();
1742
1743 /* copy the top arg to the left */
1744 *left = *arg;
1745
1746 arg->type = PRINT_OP;
1747 arg->op.op = token;
1748 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001749 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001750
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001751 if (set_op_prio(arg) == -1) {
1752 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001753 /* arg->op.op (= token) will be freed at out_free */
1754 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001755 goto out_free;
1756 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001757
1758 type = read_token_item(&token);
1759 *tok = token;
1760
1761 /* could just be a type pointer */
1762 if ((strcmp(arg->op.op, "*") == 0) &&
1763 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001764 char *new_atom;
1765
Steven Rostedtf7d82352012-04-06 00:47:53 +02001766 if (left->type != PRINT_ATOM)
1767 die("bad pointer type");
Namhyung Kimd2864472012-04-09 11:54:33 +09001768 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001769 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001770 if (!new_atom)
1771 goto out_free;
1772
1773 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001774 strcat(left->atom.atom, " *");
1775 free(arg->op.op);
1776 *arg = *left;
1777 free(left);
1778
1779 return type;
1780 }
1781
1782 right = alloc_arg();
1783 type = process_arg_token(event, right, tok, type);
1784 arg->op.right = right;
1785
1786 } else if (strcmp(token, "[") == 0) {
1787
1788 left = alloc_arg();
1789 *left = *arg;
1790
1791 arg->type = PRINT_OP;
1792 arg->op.op = token;
1793 arg->op.left = left;
1794
1795 arg->op.prio = 0;
1796
Namhyung Kim41e51a22012-09-19 15:58:42 +09001797 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001798 type = process_array(event, arg, tok);
1799
1800 } else {
1801 do_warning("unknown op '%s'", token);
1802 event->flags |= EVENT_FL_FAILED;
1803 /* the arg is now the left side */
1804 goto out_free;
1805 }
1806
1807 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1808 int prio;
1809
1810 /* higher prios need to be closer to the root */
1811 prio = get_op_prio(*tok);
1812
1813 if (prio > arg->op.prio)
1814 return process_op(event, arg, tok);
1815
1816 return process_op(event, right, tok);
1817 }
1818
1819 return type;
1820
1821 out_free:
1822 free_token(token);
1823 *tok = NULL;
1824 return EVENT_ERROR;
1825}
1826
1827static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001828process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001829 char **tok)
1830{
1831 enum event_type type;
1832 char *field;
1833 char *token;
1834
1835 if (read_expected(EVENT_OP, "->") < 0)
1836 goto out_err;
1837
1838 if (read_expect_type(EVENT_ITEM, &token) < 0)
1839 goto out_free;
1840 field = token;
1841
1842 arg->type = PRINT_FIELD;
1843 arg->field.name = field;
1844
Tom Zanussi5205aec2012-04-06 00:47:58 +02001845 if (is_flag_field) {
1846 arg->field.field = pevent_find_any_field(event, arg->field.name);
1847 arg->field.field->flags |= FIELD_IS_FLAG;
1848 is_flag_field = 0;
1849 } else if (is_symbolic_field) {
1850 arg->field.field = pevent_find_any_field(event, arg->field.name);
1851 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1852 is_symbolic_field = 0;
1853 }
1854
Steven Rostedtf7d82352012-04-06 00:47:53 +02001855 type = read_token(&token);
1856 *tok = token;
1857
1858 return type;
1859
1860 out_free:
1861 free_token(token);
1862 out_err:
1863 *tok = NULL;
1864 return EVENT_ERROR;
1865}
1866
1867static char *arg_eval (struct print_arg *arg);
1868
1869static unsigned long long
1870eval_type_str(unsigned long long val, const char *type, int pointer)
1871{
1872 int sign = 0;
1873 char *ref;
1874 int len;
1875
1876 len = strlen(type);
1877
1878 if (pointer) {
1879
1880 if (type[len-1] != '*') {
1881 do_warning("pointer expected with non pointer type");
1882 return val;
1883 }
1884
1885 ref = malloc_or_die(len);
1886 memcpy(ref, type, len);
1887
1888 /* chop off the " *" */
1889 ref[len - 2] = 0;
1890
1891 val = eval_type_str(val, ref, 0);
1892 free(ref);
1893 return val;
1894 }
1895
1896 /* check if this is a pointer */
1897 if (type[len - 1] == '*')
1898 return val;
1899
1900 /* Try to figure out the arg size*/
1901 if (strncmp(type, "struct", 6) == 0)
1902 /* all bets off */
1903 return val;
1904
1905 if (strcmp(type, "u8") == 0)
1906 return val & 0xff;
1907
1908 if (strcmp(type, "u16") == 0)
1909 return val & 0xffff;
1910
1911 if (strcmp(type, "u32") == 0)
1912 return val & 0xffffffff;
1913
1914 if (strcmp(type, "u64") == 0 ||
1915 strcmp(type, "s64"))
1916 return val;
1917
1918 if (strcmp(type, "s8") == 0)
1919 return (unsigned long long)(char)val & 0xff;
1920
1921 if (strcmp(type, "s16") == 0)
1922 return (unsigned long long)(short)val & 0xffff;
1923
1924 if (strcmp(type, "s32") == 0)
1925 return (unsigned long long)(int)val & 0xffffffff;
1926
1927 if (strncmp(type, "unsigned ", 9) == 0) {
1928 sign = 0;
1929 type += 9;
1930 }
1931
1932 if (strcmp(type, "char") == 0) {
1933 if (sign)
1934 return (unsigned long long)(char)val & 0xff;
1935 else
1936 return val & 0xff;
1937 }
1938
1939 if (strcmp(type, "short") == 0) {
1940 if (sign)
1941 return (unsigned long long)(short)val & 0xffff;
1942 else
1943 return val & 0xffff;
1944 }
1945
1946 if (strcmp(type, "int") == 0) {
1947 if (sign)
1948 return (unsigned long long)(int)val & 0xffffffff;
1949 else
1950 return val & 0xffffffff;
1951 }
1952
1953 return val;
1954}
1955
1956/*
1957 * Try to figure out the type.
1958 */
1959static unsigned long long
1960eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1961{
1962 if (arg->type != PRINT_TYPE)
1963 die("expected type argument");
1964
1965 return eval_type_str(val, arg->typecast.type, pointer);
1966}
1967
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001968static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001969{
1970 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001971 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001972
1973 switch (arg->type) {
1974 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001975 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001976 break;
1977 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001978 ret = arg_num_eval(arg->typecast.item, val);
1979 if (!ret)
1980 break;
1981 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001982 break;
1983 case PRINT_OP:
1984 switch (arg->op.op[0]) {
1985 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001986 ret = arg_num_eval(arg->op.left, &left);
1987 if (!ret)
1988 break;
1989 ret = arg_num_eval(arg->op.right, &right);
1990 if (!ret)
1991 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001992 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001993 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001994 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001995 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001996 break;
1997 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02001998 ret = arg_num_eval(arg->op.left, &left);
1999 if (!ret)
2000 break;
2001 ret = arg_num_eval(arg->op.right, &right);
2002 if (!ret)
2003 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002004 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002005 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002006 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002007 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002008 break;
2009 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002010 ret = arg_num_eval(arg->op.left, &left);
2011 if (!ret)
2012 break;
2013 ret = arg_num_eval(arg->op.right, &right);
2014 if (!ret)
2015 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002016 switch (arg->op.op[1]) {
2017 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002018 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002019 break;
2020 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002021 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002022 break;
2023 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002024 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002025 break;
2026 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002027 do_warning("unknown op '%s'", arg->op.op);
2028 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002029 }
2030 break;
2031 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002032 ret = arg_num_eval(arg->op.left, &left);
2033 if (!ret)
2034 break;
2035 ret = arg_num_eval(arg->op.right, &right);
2036 if (!ret)
2037 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002038 switch (arg->op.op[1]) {
2039 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002040 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002041 break;
2042 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002043 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002044 break;
2045 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002046 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002047 break;
2048 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002049 do_warning("unknown op '%s'", arg->op.op);
2050 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002051 }
2052 break;
2053 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002054 ret = arg_num_eval(arg->op.left, &left);
2055 if (!ret)
2056 break;
2057 ret = arg_num_eval(arg->op.right, &right);
2058 if (!ret)
2059 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002060
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002061 if (arg->op.op[1] != '=') {
2062 do_warning("unknown op '%s'", arg->op.op);
2063 ret = 0;
2064 } else
2065 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002066 break;
2067 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002068 ret = arg_num_eval(arg->op.left, &left);
2069 if (!ret)
2070 break;
2071 ret = arg_num_eval(arg->op.right, &right);
2072 if (!ret)
2073 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002074
2075 switch (arg->op.op[1]) {
2076 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002077 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002078 break;
2079 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002080 do_warning("unknown op '%s'", arg->op.op);
2081 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002082 }
2083 break;
2084 case '-':
2085 /* check for negative */
2086 if (arg->op.left->type == PRINT_NULL)
2087 left = 0;
2088 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002089 ret = arg_num_eval(arg->op.left, &left);
2090 if (!ret)
2091 break;
2092 ret = arg_num_eval(arg->op.right, &right);
2093 if (!ret)
2094 break;
2095 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002096 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002097 case '+':
2098 if (arg->op.left->type == PRINT_NULL)
2099 left = 0;
2100 else
2101 ret = arg_num_eval(arg->op.left, &left);
2102 if (!ret)
2103 break;
2104 ret = arg_num_eval(arg->op.right, &right);
2105 if (!ret)
2106 break;
2107 *val = left + right;
2108 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002109 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002110 do_warning("unknown op '%s'", arg->op.op);
2111 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002112 }
2113 break;
2114
2115 case PRINT_NULL:
2116 case PRINT_FIELD ... PRINT_SYMBOL:
2117 case PRINT_STRING:
2118 case PRINT_BSTRING:
2119 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002120 do_warning("invalid eval type %d", arg->type);
2121 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002122
2123 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002124 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002125}
2126
2127static char *arg_eval (struct print_arg *arg)
2128{
2129 long long val;
2130 static char buf[20];
2131
2132 switch (arg->type) {
2133 case PRINT_ATOM:
2134 return arg->atom.atom;
2135 case PRINT_TYPE:
2136 return arg_eval(arg->typecast.item);
2137 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002138 if (!arg_num_eval(arg, &val))
2139 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002140 sprintf(buf, "%lld", val);
2141 return buf;
2142
2143 case PRINT_NULL:
2144 case PRINT_FIELD ... PRINT_SYMBOL:
2145 case PRINT_STRING:
2146 case PRINT_BSTRING:
2147 default:
2148 die("invalid eval type %d", arg->type);
2149 break;
2150 }
2151
2152 return NULL;
2153}
2154
2155static enum event_type
2156process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2157{
2158 enum event_type type;
2159 struct print_arg *arg = NULL;
2160 struct print_flag_sym *field;
2161 char *token = *tok;
2162 char *value;
2163
2164 do {
2165 free_token(token);
2166 type = read_token_item(&token);
2167 if (test_type_token(type, token, EVENT_OP, "{"))
2168 break;
2169
2170 arg = alloc_arg();
2171
2172 free_token(token);
2173 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002174
2175 if (type == EVENT_OP)
2176 type = process_op(event, arg, &token);
2177
2178 if (type == EVENT_ERROR)
2179 goto out_free;
2180
Steven Rostedtf7d82352012-04-06 00:47:53 +02002181 if (test_type_token(type, token, EVENT_DELIM, ","))
2182 goto out_free;
2183
2184 field = malloc_or_die(sizeof(*field));
Julia Lawall54a36252012-04-06 00:47:59 +02002185 memset(field, 0, sizeof(*field));
Steven Rostedtf7d82352012-04-06 00:47:53 +02002186
2187 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002188 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002189 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002190 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002191 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002192 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002193
2194 free_arg(arg);
2195 arg = alloc_arg();
2196
2197 free_token(token);
2198 type = process_arg(event, arg, &token);
2199 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002200 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002201
2202 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002203 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002204 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002205 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002206 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002207 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002208 free_arg(arg);
2209 arg = NULL;
2210
2211 *list = field;
2212 list = &field->next;
2213
2214 free_token(token);
2215 type = read_token_item(&token);
2216 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2217
2218 *tok = token;
2219 return type;
2220
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002221out_free_field:
2222 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002223out_free:
2224 free_arg(arg);
2225 free_token(token);
2226 *tok = NULL;
2227
2228 return EVENT_ERROR;
2229}
2230
2231static enum event_type
2232process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2233{
2234 struct print_arg *field;
2235 enum event_type type;
2236 char *token;
2237
2238 memset(arg, 0, sizeof(*arg));
2239 arg->type = PRINT_FLAGS;
2240
2241 field = alloc_arg();
2242
2243 type = process_arg(event, field, &token);
2244
2245 /* Handle operations in the first argument */
2246 while (type == EVENT_OP)
2247 type = process_op(event, field, &token);
2248
2249 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002250 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002251 free_token(token);
2252
2253 arg->flags.field = field;
2254
2255 type = read_token_item(&token);
2256 if (event_item_type(type)) {
2257 arg->flags.delim = token;
2258 type = read_token_item(&token);
2259 }
2260
2261 if (test_type_token(type, token, EVENT_DELIM, ","))
2262 goto out_free;
2263
2264 type = process_fields(event, &arg->flags.flags, &token);
2265 if (test_type_token(type, token, EVENT_DELIM, ")"))
2266 goto out_free;
2267
2268 free_token(token);
2269 type = read_token_item(tok);
2270 return type;
2271
Namhyung Kim70d93042012-09-19 15:58:44 +09002272out_free_field:
2273 free_arg(field);
2274out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002275 free_token(token);
2276 *tok = NULL;
2277 return EVENT_ERROR;
2278}
2279
2280static enum event_type
2281process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2282{
2283 struct print_arg *field;
2284 enum event_type type;
2285 char *token;
2286
2287 memset(arg, 0, sizeof(*arg));
2288 arg->type = PRINT_SYMBOL;
2289
2290 field = alloc_arg();
2291
2292 type = process_arg(event, field, &token);
2293 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002294 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002295
2296 arg->symbol.field = field;
2297
2298 type = process_fields(event, &arg->symbol.symbols, &token);
2299 if (test_type_token(type, token, EVENT_DELIM, ")"))
2300 goto out_free;
2301
2302 free_token(token);
2303 type = read_token_item(tok);
2304 return type;
2305
Namhyung Kim70d93042012-09-19 15:58:44 +09002306out_free_field:
2307 free_arg(field);
2308out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002309 free_token(token);
2310 *tok = NULL;
2311 return EVENT_ERROR;
2312}
2313
2314static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002315process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2316{
2317 struct print_arg *field;
2318 enum event_type type;
2319 char *token;
2320
2321 memset(arg, 0, sizeof(*arg));
2322 arg->type = PRINT_HEX;
2323
2324 field = alloc_arg();
2325 type = process_arg(event, field, &token);
2326
2327 if (test_type_token(type, token, EVENT_DELIM, ","))
2328 goto out_free;
2329
2330 arg->hex.field = field;
2331
2332 free_token(token);
2333
2334 field = alloc_arg();
2335 type = process_arg(event, field, &token);
2336
2337 if (test_type_token(type, token, EVENT_DELIM, ")"))
2338 goto out_free;
2339
2340 arg->hex.size = field;
2341
2342 free_token(token);
2343 type = read_token_item(tok);
2344 return type;
2345
2346 out_free:
2347 free_arg(field);
2348 free_token(token);
2349 *tok = NULL;
2350 return EVENT_ERROR;
2351}
2352
2353static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002354process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2355{
2356 struct format_field *field;
2357 enum event_type type;
2358 char *token;
2359
2360 memset(arg, 0, sizeof(*arg));
2361 arg->type = PRINT_DYNAMIC_ARRAY;
2362
2363 /*
2364 * The item within the parenthesis is another field that holds
2365 * the index into where the array starts.
2366 */
2367 type = read_token(&token);
2368 *tok = token;
2369 if (type != EVENT_ITEM)
2370 goto out_free;
2371
2372 /* Find the field */
2373
2374 field = pevent_find_field(event, token);
2375 if (!field)
2376 goto out_free;
2377
2378 arg->dynarray.field = field;
2379 arg->dynarray.index = 0;
2380
2381 if (read_expected(EVENT_DELIM, ")") < 0)
2382 goto out_free;
2383
2384 free_token(token);
2385 type = read_token_item(&token);
2386 *tok = token;
2387 if (type != EVENT_OP || strcmp(token, "[") != 0)
2388 return type;
2389
2390 free_token(token);
2391 arg = alloc_arg();
2392 type = process_arg(event, arg, &token);
2393 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002394 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002395
2396 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002397 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002398
2399 free_token(token);
2400 type = read_token_item(tok);
2401 return type;
2402
Namhyung Kimb3511d02012-05-23 11:36:50 +09002403 out_free_arg:
2404 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002405 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002406 free_token(token);
2407 *tok = NULL;
2408 return EVENT_ERROR;
2409}
2410
2411static enum event_type
2412process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2413{
2414 struct print_arg *item_arg;
2415 enum event_type type;
2416 char *token;
2417
2418 type = process_arg(event, arg, &token);
2419
2420 if (type == EVENT_ERROR)
2421 goto out_free;
2422
2423 if (type == EVENT_OP)
2424 type = process_op(event, arg, &token);
2425
2426 if (type == EVENT_ERROR)
2427 goto out_free;
2428
2429 if (test_type_token(type, token, EVENT_DELIM, ")"))
2430 goto out_free;
2431
2432 free_token(token);
2433 type = read_token_item(&token);
2434
2435 /*
2436 * If the next token is an item or another open paren, then
2437 * this was a typecast.
2438 */
2439 if (event_item_type(type) ||
2440 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2441
2442 /* make this a typecast and contine */
2443
2444 /* prevous must be an atom */
2445 if (arg->type != PRINT_ATOM)
2446 die("previous needed to be PRINT_ATOM");
2447
2448 item_arg = alloc_arg();
2449
2450 arg->type = PRINT_TYPE;
2451 arg->typecast.type = arg->atom.atom;
2452 arg->typecast.item = item_arg;
2453 type = process_arg_token(event, item_arg, &token, type);
2454
2455 }
2456
2457 *tok = token;
2458 return type;
2459
2460 out_free:
2461 free_token(token);
2462 *tok = NULL;
2463 return EVENT_ERROR;
2464}
2465
2466
2467static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002468process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2469 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002470{
2471 enum event_type type;
2472 char *token;
2473
2474 if (read_expect_type(EVENT_ITEM, &token) < 0)
2475 goto out_free;
2476
2477 arg->type = PRINT_STRING;
2478 arg->string.string = token;
2479 arg->string.offset = -1;
2480
2481 if (read_expected(EVENT_DELIM, ")") < 0)
2482 goto out_err;
2483
2484 type = read_token(&token);
2485 *tok = token;
2486
2487 return type;
2488
2489 out_free:
2490 free_token(token);
2491 out_err:
2492 *tok = NULL;
2493 return EVENT_ERROR;
2494}
2495
2496static struct pevent_function_handler *
2497find_func_handler(struct pevent *pevent, char *func_name)
2498{
2499 struct pevent_function_handler *func;
2500
2501 for (func = pevent->func_handlers; func; func = func->next) {
2502 if (strcmp(func->name, func_name) == 0)
2503 break;
2504 }
2505
2506 return func;
2507}
2508
2509static void remove_func_handler(struct pevent *pevent, char *func_name)
2510{
2511 struct pevent_function_handler *func;
2512 struct pevent_function_handler **next;
2513
2514 next = &pevent->func_handlers;
2515 while ((func = *next)) {
2516 if (strcmp(func->name, func_name) == 0) {
2517 *next = func->next;
2518 free_func_handle(func);
2519 break;
2520 }
2521 next = &func->next;
2522 }
2523}
2524
2525static enum event_type
2526process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2527 struct print_arg *arg, char **tok)
2528{
2529 struct print_arg **next_arg;
2530 struct print_arg *farg;
2531 enum event_type type;
2532 char *token;
2533 char *test;
2534 int i;
2535
2536 arg->type = PRINT_FUNC;
2537 arg->func.func = func;
2538
2539 *tok = NULL;
2540
2541 next_arg = &(arg->func.args);
2542 for (i = 0; i < func->nr_args; i++) {
2543 farg = alloc_arg();
2544 type = process_arg(event, farg, &token);
2545 if (i < (func->nr_args - 1))
2546 test = ",";
2547 else
2548 test = ")";
2549
2550 if (test_type_token(type, token, EVENT_DELIM, test)) {
2551 free_arg(farg);
2552 free_token(token);
2553 return EVENT_ERROR;
2554 }
2555
2556 *next_arg = farg;
2557 next_arg = &(farg->next);
2558 free_token(token);
2559 }
2560
2561 type = read_token(&token);
2562 *tok = token;
2563
2564 return type;
2565}
2566
2567static enum event_type
2568process_function(struct event_format *event, struct print_arg *arg,
2569 char *token, char **tok)
2570{
2571 struct pevent_function_handler *func;
2572
2573 if (strcmp(token, "__print_flags") == 0) {
2574 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002575 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002576 return process_flags(event, arg, tok);
2577 }
2578 if (strcmp(token, "__print_symbolic") == 0) {
2579 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002580 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002581 return process_symbols(event, arg, tok);
2582 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002583 if (strcmp(token, "__print_hex") == 0) {
2584 free_token(token);
2585 return process_hex(event, arg, tok);
2586 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002587 if (strcmp(token, "__get_str") == 0) {
2588 free_token(token);
2589 return process_str(event, arg, tok);
2590 }
2591 if (strcmp(token, "__get_dynamic_array") == 0) {
2592 free_token(token);
2593 return process_dynamic_array(event, arg, tok);
2594 }
2595
2596 func = find_func_handler(event->pevent, token);
2597 if (func) {
2598 free_token(token);
2599 return process_func_handler(event, func, arg, tok);
2600 }
2601
2602 do_warning("function %s not defined", token);
2603 free_token(token);
2604 return EVENT_ERROR;
2605}
2606
2607static enum event_type
2608process_arg_token(struct event_format *event, struct print_arg *arg,
2609 char **tok, enum event_type type)
2610{
2611 char *token;
2612 char *atom;
2613
2614 token = *tok;
2615
2616 switch (type) {
2617 case EVENT_ITEM:
2618 if (strcmp(token, "REC") == 0) {
2619 free_token(token);
2620 type = process_entry(event, arg, &token);
2621 break;
2622 }
2623 atom = token;
2624 /* test the next token */
2625 type = read_token_item(&token);
2626
2627 /*
2628 * If the next token is a parenthesis, then this
2629 * is a function.
2630 */
2631 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2632 free_token(token);
2633 token = NULL;
2634 /* this will free atom. */
2635 type = process_function(event, arg, atom, &token);
2636 break;
2637 }
2638 /* atoms can be more than one token long */
2639 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002640 char *new_atom;
2641 new_atom = realloc(atom,
2642 strlen(atom) + strlen(token) + 2);
2643 if (!new_atom) {
2644 free(atom);
2645 *tok = NULL;
2646 free_token(token);
2647 return EVENT_ERROR;
2648 }
2649 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002650 strcat(atom, " ");
2651 strcat(atom, token);
2652 free_token(token);
2653 type = read_token_item(&token);
2654 }
2655
2656 arg->type = PRINT_ATOM;
2657 arg->atom.atom = atom;
2658 break;
2659
2660 case EVENT_DQUOTE:
2661 case EVENT_SQUOTE:
2662 arg->type = PRINT_ATOM;
2663 arg->atom.atom = token;
2664 type = read_token_item(&token);
2665 break;
2666 case EVENT_DELIM:
2667 if (strcmp(token, "(") == 0) {
2668 free_token(token);
2669 type = process_paren(event, arg, &token);
2670 break;
2671 }
2672 case EVENT_OP:
2673 /* handle single ops */
2674 arg->type = PRINT_OP;
2675 arg->op.op = token;
2676 arg->op.left = NULL;
2677 type = process_op(event, arg, &token);
2678
2679 /* On error, the op is freed */
2680 if (type == EVENT_ERROR)
2681 arg->op.op = NULL;
2682
2683 /* return error type if errored */
2684 break;
2685
2686 case EVENT_ERROR ... EVENT_NEWLINE:
2687 default:
2688 die("unexpected type %d", type);
2689 }
2690 *tok = token;
2691
2692 return type;
2693}
2694
2695static int event_read_print_args(struct event_format *event, struct print_arg **list)
2696{
2697 enum event_type type = EVENT_ERROR;
2698 struct print_arg *arg;
2699 char *token;
2700 int args = 0;
2701
2702 do {
2703 if (type == EVENT_NEWLINE) {
2704 type = read_token_item(&token);
2705 continue;
2706 }
2707
2708 arg = alloc_arg();
2709
2710 type = process_arg(event, arg, &token);
2711
2712 if (type == EVENT_ERROR) {
2713 free_token(token);
2714 free_arg(arg);
2715 return -1;
2716 }
2717
2718 *list = arg;
2719 args++;
2720
2721 if (type == EVENT_OP) {
2722 type = process_op(event, arg, &token);
2723 free_token(token);
2724 if (type == EVENT_ERROR) {
2725 *list = NULL;
2726 free_arg(arg);
2727 return -1;
2728 }
2729 list = &arg->next;
2730 continue;
2731 }
2732
2733 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2734 free_token(token);
2735 *list = arg;
2736 list = &arg->next;
2737 continue;
2738 }
2739 break;
2740 } while (type != EVENT_NONE);
2741
2742 if (type != EVENT_NONE && type != EVENT_ERROR)
2743 free_token(token);
2744
2745 return args;
2746}
2747
2748static int event_read_print(struct event_format *event)
2749{
2750 enum event_type type;
2751 char *token;
2752 int ret;
2753
2754 if (read_expected_item(EVENT_ITEM, "print") < 0)
2755 return -1;
2756
2757 if (read_expected(EVENT_ITEM, "fmt") < 0)
2758 return -1;
2759
2760 if (read_expected(EVENT_OP, ":") < 0)
2761 return -1;
2762
2763 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2764 goto fail;
2765
2766 concat:
2767 event->print_fmt.format = token;
2768 event->print_fmt.args = NULL;
2769
2770 /* ok to have no arg */
2771 type = read_token_item(&token);
2772
2773 if (type == EVENT_NONE)
2774 return 0;
2775
2776 /* Handle concatenation of print lines */
2777 if (type == EVENT_DQUOTE) {
2778 char *cat;
2779
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03002780 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2781 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002782 free_token(token);
2783 free_token(event->print_fmt.format);
2784 event->print_fmt.format = NULL;
2785 token = cat;
2786 goto concat;
2787 }
2788
2789 if (test_type_token(type, token, EVENT_DELIM, ","))
2790 goto fail;
2791
2792 free_token(token);
2793
2794 ret = event_read_print_args(event, &event->print_fmt.args);
2795 if (ret < 0)
2796 return -1;
2797
2798 return ret;
2799
2800 fail:
2801 free_token(token);
2802 return -1;
2803}
2804
2805/**
2806 * pevent_find_common_field - return a common field by event
2807 * @event: handle for the event
2808 * @name: the name of the common field to return
2809 *
2810 * Returns a common field from the event by the given @name.
2811 * This only searchs the common fields and not all field.
2812 */
2813struct format_field *
2814pevent_find_common_field(struct event_format *event, const char *name)
2815{
2816 struct format_field *format;
2817
2818 for (format = event->format.common_fields;
2819 format; format = format->next) {
2820 if (strcmp(format->name, name) == 0)
2821 break;
2822 }
2823
2824 return format;
2825}
2826
2827/**
2828 * pevent_find_field - find a non-common field
2829 * @event: handle for the event
2830 * @name: the name of the non-common field
2831 *
2832 * Returns a non-common field by the given @name.
2833 * This does not search common fields.
2834 */
2835struct format_field *
2836pevent_find_field(struct event_format *event, const char *name)
2837{
2838 struct format_field *format;
2839
2840 for (format = event->format.fields;
2841 format; format = format->next) {
2842 if (strcmp(format->name, name) == 0)
2843 break;
2844 }
2845
2846 return format;
2847}
2848
2849/**
2850 * pevent_find_any_field - find any field by name
2851 * @event: handle for the event
2852 * @name: the name of the field
2853 *
2854 * Returns a field by the given @name.
2855 * This searchs the common field names first, then
2856 * the non-common ones if a common one was not found.
2857 */
2858struct format_field *
2859pevent_find_any_field(struct event_format *event, const char *name)
2860{
2861 struct format_field *format;
2862
2863 format = pevent_find_common_field(event, name);
2864 if (format)
2865 return format;
2866 return pevent_find_field(event, name);
2867}
2868
2869/**
2870 * pevent_read_number - read a number from data
2871 * @pevent: handle for the pevent
2872 * @ptr: the raw data
2873 * @size: the size of the data that holds the number
2874 *
2875 * Returns the number (converted to host) from the
2876 * raw data.
2877 */
2878unsigned long long pevent_read_number(struct pevent *pevent,
2879 const void *ptr, int size)
2880{
2881 switch (size) {
2882 case 1:
2883 return *(unsigned char *)ptr;
2884 case 2:
2885 return data2host2(pevent, ptr);
2886 case 4:
2887 return data2host4(pevent, ptr);
2888 case 8:
2889 return data2host8(pevent, ptr);
2890 default:
2891 /* BUG! */
2892 return 0;
2893 }
2894}
2895
2896/**
2897 * pevent_read_number_field - read a number from data
2898 * @field: a handle to the field
2899 * @data: the raw data to read
2900 * @value: the value to place the number in
2901 *
2902 * Reads raw data according to a field offset and size,
2903 * and translates it into @value.
2904 *
2905 * Returns 0 on success, -1 otherwise.
2906 */
2907int pevent_read_number_field(struct format_field *field, const void *data,
2908 unsigned long long *value)
2909{
2910 if (!field)
2911 return -1;
2912 switch (field->size) {
2913 case 1:
2914 case 2:
2915 case 4:
2916 case 8:
2917 *value = pevent_read_number(field->event->pevent,
2918 data + field->offset, field->size);
2919 return 0;
2920 default:
2921 return -1;
2922 }
2923}
2924
2925static int get_common_info(struct pevent *pevent,
2926 const char *type, int *offset, int *size)
2927{
2928 struct event_format *event;
2929 struct format_field *field;
2930
2931 /*
2932 * All events should have the same common elements.
2933 * Pick any event to find where the type is;
2934 */
2935 if (!pevent->events)
2936 die("no event_list!");
2937
2938 event = pevent->events[0];
2939 field = pevent_find_common_field(event, type);
2940 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09002941 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002942
2943 *offset = field->offset;
2944 *size = field->size;
2945
2946 return 0;
2947}
2948
2949static int __parse_common(struct pevent *pevent, void *data,
2950 int *size, int *offset, const char *name)
2951{
2952 int ret;
2953
2954 if (!*size) {
2955 ret = get_common_info(pevent, name, offset, size);
2956 if (ret < 0)
2957 return ret;
2958 }
2959 return pevent_read_number(pevent, data + *offset, *size);
2960}
2961
2962static int trace_parse_common_type(struct pevent *pevent, void *data)
2963{
2964 return __parse_common(pevent, data,
2965 &pevent->type_size, &pevent->type_offset,
2966 "common_type");
2967}
2968
2969static int parse_common_pid(struct pevent *pevent, void *data)
2970{
2971 return __parse_common(pevent, data,
2972 &pevent->pid_size, &pevent->pid_offset,
2973 "common_pid");
2974}
2975
2976static int parse_common_pc(struct pevent *pevent, void *data)
2977{
2978 return __parse_common(pevent, data,
2979 &pevent->pc_size, &pevent->pc_offset,
2980 "common_preempt_count");
2981}
2982
2983static int parse_common_flags(struct pevent *pevent, void *data)
2984{
2985 return __parse_common(pevent, data,
2986 &pevent->flags_size, &pevent->flags_offset,
2987 "common_flags");
2988}
2989
2990static int parse_common_lock_depth(struct pevent *pevent, void *data)
2991{
Steven Rostedt0866a972012-05-22 14:52:40 +09002992 return __parse_common(pevent, data,
2993 &pevent->ld_size, &pevent->ld_offset,
2994 "common_lock_depth");
2995}
Steven Rostedtf7d82352012-04-06 00:47:53 +02002996
Steven Rostedt0866a972012-05-22 14:52:40 +09002997static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2998{
2999 return __parse_common(pevent, data,
3000 &pevent->ld_size, &pevent->ld_offset,
3001 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003002}
3003
3004static int events_id_cmp(const void *a, const void *b);
3005
3006/**
3007 * pevent_find_event - find an event by given id
3008 * @pevent: a handle to the pevent
3009 * @id: the id of the event
3010 *
3011 * Returns an event that has a given @id.
3012 */
3013struct event_format *pevent_find_event(struct pevent *pevent, int id)
3014{
3015 struct event_format **eventptr;
3016 struct event_format key;
3017 struct event_format *pkey = &key;
3018
3019 /* Check cache first */
3020 if (pevent->last_event && pevent->last_event->id == id)
3021 return pevent->last_event;
3022
3023 key.id = id;
3024
3025 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3026 sizeof(*pevent->events), events_id_cmp);
3027
3028 if (eventptr) {
3029 pevent->last_event = *eventptr;
3030 return *eventptr;
3031 }
3032
3033 return NULL;
3034}
3035
3036/**
3037 * pevent_find_event_by_name - find an event by given name
3038 * @pevent: a handle to the pevent
3039 * @sys: the system name to search for
3040 * @name: the name of the event to search for
3041 *
3042 * This returns an event with a given @name and under the system
3043 * @sys. If @sys is NULL the first event with @name is returned.
3044 */
3045struct event_format *
3046pevent_find_event_by_name(struct pevent *pevent,
3047 const char *sys, const char *name)
3048{
3049 struct event_format *event;
3050 int i;
3051
3052 if (pevent->last_event &&
3053 strcmp(pevent->last_event->name, name) == 0 &&
3054 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3055 return pevent->last_event;
3056
3057 for (i = 0; i < pevent->nr_events; i++) {
3058 event = pevent->events[i];
3059 if (strcmp(event->name, name) == 0) {
3060 if (!sys)
3061 break;
3062 if (strcmp(event->system, sys) == 0)
3063 break;
3064 }
3065 }
3066 if (i == pevent->nr_events)
3067 event = NULL;
3068
3069 pevent->last_event = event;
3070 return event;
3071}
3072
3073static unsigned long long
3074eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3075{
3076 struct pevent *pevent = event->pevent;
3077 unsigned long long val = 0;
3078 unsigned long long left, right;
3079 struct print_arg *typearg = NULL;
3080 struct print_arg *larg;
3081 unsigned long offset;
3082 unsigned int field_size;
3083
3084 switch (arg->type) {
3085 case PRINT_NULL:
3086 /* ?? */
3087 return 0;
3088 case PRINT_ATOM:
3089 return strtoull(arg->atom.atom, NULL, 0);
3090 case PRINT_FIELD:
3091 if (!arg->field.field) {
3092 arg->field.field = pevent_find_any_field(event, arg->field.name);
3093 if (!arg->field.field)
3094 die("field %s not found", arg->field.name);
3095 }
3096 /* must be a number */
3097 val = pevent_read_number(pevent, data + arg->field.field->offset,
3098 arg->field.field->size);
3099 break;
3100 case PRINT_FLAGS:
3101 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003102 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003103 break;
3104 case PRINT_TYPE:
3105 val = eval_num_arg(data, size, event, arg->typecast.item);
3106 return eval_type(val, arg, 0);
3107 case PRINT_STRING:
3108 case PRINT_BSTRING:
3109 return 0;
3110 case PRINT_FUNC: {
3111 struct trace_seq s;
3112 trace_seq_init(&s);
3113 val = process_defined_func(&s, data, size, event, arg);
3114 trace_seq_destroy(&s);
3115 return val;
3116 }
3117 case PRINT_OP:
3118 if (strcmp(arg->op.op, "[") == 0) {
3119 /*
3120 * Arrays are special, since we don't want
3121 * to read the arg as is.
3122 */
3123 right = eval_num_arg(data, size, event, arg->op.right);
3124
3125 /* handle typecasts */
3126 larg = arg->op.left;
3127 while (larg->type == PRINT_TYPE) {
3128 if (!typearg)
3129 typearg = larg;
3130 larg = larg->typecast.item;
3131 }
3132
3133 /* Default to long size */
3134 field_size = pevent->long_size;
3135
3136 switch (larg->type) {
3137 case PRINT_DYNAMIC_ARRAY:
3138 offset = pevent_read_number(pevent,
3139 data + larg->dynarray.field->offset,
3140 larg->dynarray.field->size);
3141 if (larg->dynarray.field->elementsize)
3142 field_size = larg->dynarray.field->elementsize;
3143 /*
3144 * The actual length of the dynamic array is stored
3145 * in the top half of the field, and the offset
3146 * is in the bottom half of the 32 bit field.
3147 */
3148 offset &= 0xffff;
3149 offset += right;
3150 break;
3151 case PRINT_FIELD:
3152 if (!larg->field.field) {
3153 larg->field.field =
3154 pevent_find_any_field(event, larg->field.name);
3155 if (!larg->field.field)
3156 die("field %s not found", larg->field.name);
3157 }
3158 field_size = larg->field.field->elementsize;
3159 offset = larg->field.field->offset +
3160 right * larg->field.field->elementsize;
3161 break;
3162 default:
3163 goto default_op; /* oops, all bets off */
3164 }
3165 val = pevent_read_number(pevent,
3166 data + offset, field_size);
3167 if (typearg)
3168 val = eval_type(val, typearg, 1);
3169 break;
3170 } else if (strcmp(arg->op.op, "?") == 0) {
3171 left = eval_num_arg(data, size, event, arg->op.left);
3172 arg = arg->op.right;
3173 if (left)
3174 val = eval_num_arg(data, size, event, arg->op.left);
3175 else
3176 val = eval_num_arg(data, size, event, arg->op.right);
3177 break;
3178 }
3179 default_op:
3180 left = eval_num_arg(data, size, event, arg->op.left);
3181 right = eval_num_arg(data, size, event, arg->op.right);
3182 switch (arg->op.op[0]) {
3183 case '!':
3184 switch (arg->op.op[1]) {
3185 case 0:
3186 val = !right;
3187 break;
3188 case '=':
3189 val = left != right;
3190 break;
3191 default:
3192 die("unknown op '%s'", arg->op.op);
3193 }
3194 break;
3195 case '~':
3196 val = ~right;
3197 break;
3198 case '|':
3199 if (arg->op.op[1])
3200 val = left || right;
3201 else
3202 val = left | right;
3203 break;
3204 case '&':
3205 if (arg->op.op[1])
3206 val = left && right;
3207 else
3208 val = left & right;
3209 break;
3210 case '<':
3211 switch (arg->op.op[1]) {
3212 case 0:
3213 val = left < right;
3214 break;
3215 case '<':
3216 val = left << right;
3217 break;
3218 case '=':
3219 val = left <= right;
3220 break;
3221 default:
3222 die("unknown op '%s'", arg->op.op);
3223 }
3224 break;
3225 case '>':
3226 switch (arg->op.op[1]) {
3227 case 0:
3228 val = left > right;
3229 break;
3230 case '>':
3231 val = left >> right;
3232 break;
3233 case '=':
3234 val = left >= right;
3235 break;
3236 default:
3237 die("unknown op '%s'", arg->op.op);
3238 }
3239 break;
3240 case '=':
3241 if (arg->op.op[1] != '=')
3242 die("unknown op '%s'", arg->op.op);
3243 val = left == right;
3244 break;
3245 case '-':
3246 val = left - right;
3247 break;
3248 case '+':
3249 val = left + right;
3250 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003251 case '/':
3252 val = left / right;
3253 break;
3254 case '*':
3255 val = left * right;
3256 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003257 default:
3258 die("unknown op '%s'", arg->op.op);
3259 }
3260 break;
3261 default: /* not sure what to do there */
3262 return 0;
3263 }
3264 return val;
3265}
3266
3267struct flag {
3268 const char *name;
3269 unsigned long long value;
3270};
3271
3272static const struct flag flags[] = {
3273 { "HI_SOFTIRQ", 0 },
3274 { "TIMER_SOFTIRQ", 1 },
3275 { "NET_TX_SOFTIRQ", 2 },
3276 { "NET_RX_SOFTIRQ", 3 },
3277 { "BLOCK_SOFTIRQ", 4 },
3278 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3279 { "TASKLET_SOFTIRQ", 6 },
3280 { "SCHED_SOFTIRQ", 7 },
3281 { "HRTIMER_SOFTIRQ", 8 },
3282 { "RCU_SOFTIRQ", 9 },
3283
3284 { "HRTIMER_NORESTART", 0 },
3285 { "HRTIMER_RESTART", 1 },
3286};
3287
3288static unsigned long long eval_flag(const char *flag)
3289{
3290 int i;
3291
3292 /*
3293 * Some flags in the format files do not get converted.
3294 * If the flag is not numeric, see if it is something that
3295 * we already know about.
3296 */
3297 if (isdigit(flag[0]))
3298 return strtoull(flag, NULL, 0);
3299
3300 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3301 if (strcmp(flags[i].name, flag) == 0)
3302 return flags[i].value;
3303
3304 return 0;
3305}
3306
3307static void print_str_to_seq(struct trace_seq *s, const char *format,
3308 int len_arg, const char *str)
3309{
3310 if (len_arg >= 0)
3311 trace_seq_printf(s, format, len_arg, str);
3312 else
3313 trace_seq_printf(s, format, str);
3314}
3315
3316static void print_str_arg(struct trace_seq *s, void *data, int size,
3317 struct event_format *event, const char *format,
3318 int len_arg, struct print_arg *arg)
3319{
3320 struct pevent *pevent = event->pevent;
3321 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003322 struct format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003323 unsigned long long val, fval;
3324 unsigned long addr;
3325 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003326 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003327 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003328 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003329
3330 switch (arg->type) {
3331 case PRINT_NULL:
3332 /* ?? */
3333 return;
3334 case PRINT_ATOM:
3335 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3336 return;
3337 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003338 field = arg->field.field;
3339 if (!field) {
3340 field = pevent_find_any_field(event, arg->field.name);
3341 if (!field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003342 die("field %s not found", arg->field.name);
Namhyung Kimb7008072012-06-27 09:41:40 +09003343 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003344 }
3345 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003346 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003347
3348 /*
3349 * Some events pass in pointers. If this is not an array
3350 * and the size is the same as long_size, assume that it
3351 * is a pointer.
3352 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003353 if (!(field->flags & FIELD_IS_ARRAY) &&
3354 field->size == pevent->long_size) {
3355 addr = *(unsigned long *)(data + field->offset);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003356 trace_seq_printf(s, "%lx", addr);
3357 break;
3358 }
3359 str = malloc_or_die(len + 1);
Namhyung Kimb7008072012-06-27 09:41:40 +09003360 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003361 str[len] = 0;
3362 print_str_to_seq(s, format, len_arg, str);
3363 free(str);
3364 break;
3365 case PRINT_FLAGS:
3366 val = eval_num_arg(data, size, event, arg->flags.field);
3367 print = 0;
3368 for (flag = arg->flags.flags; flag; flag = flag->next) {
3369 fval = eval_flag(flag->value);
3370 if (!val && !fval) {
3371 print_str_to_seq(s, format, len_arg, flag->str);
3372 break;
3373 }
3374 if (fval && (val & fval) == fval) {
3375 if (print && arg->flags.delim)
3376 trace_seq_puts(s, arg->flags.delim);
3377 print_str_to_seq(s, format, len_arg, flag->str);
3378 print = 1;
3379 val &= ~fval;
3380 }
3381 }
3382 break;
3383 case PRINT_SYMBOL:
3384 val = eval_num_arg(data, size, event, arg->symbol.field);
3385 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3386 fval = eval_flag(flag->value);
3387 if (val == fval) {
3388 print_str_to_seq(s, format, len_arg, flag->str);
3389 break;
3390 }
3391 }
3392 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003393 case PRINT_HEX:
3394 field = arg->hex.field->field.field;
3395 if (!field) {
3396 str = arg->hex.field->field.name;
3397 field = pevent_find_any_field(event, str);
3398 if (!field)
3399 die("field %s not found", str);
3400 arg->hex.field->field.field = field;
3401 }
3402 hex = data + field->offset;
3403 len = eval_num_arg(data, size, event, arg->hex.size);
3404 for (i = 0; i < len; i++) {
3405 if (i)
3406 trace_seq_putc(s, ' ');
3407 trace_seq_printf(s, "%02x", hex[i]);
3408 }
3409 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003410
3411 case PRINT_TYPE:
3412 break;
3413 case PRINT_STRING: {
3414 int str_offset;
3415
3416 if (arg->string.offset == -1) {
3417 struct format_field *f;
3418
3419 f = pevent_find_any_field(event, arg->string.string);
3420 arg->string.offset = f->offset;
3421 }
3422 str_offset = data2host4(pevent, data + arg->string.offset);
3423 str_offset &= 0xffff;
3424 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3425 break;
3426 }
3427 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003428 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003429 break;
3430 case PRINT_OP:
3431 /*
3432 * The only op for string should be ? :
3433 */
3434 if (arg->op.op[0] != '?')
3435 return;
3436 val = eval_num_arg(data, size, event, arg->op.left);
3437 if (val)
3438 print_str_arg(s, data, size, event,
3439 format, len_arg, arg->op.right->op.left);
3440 else
3441 print_str_arg(s, data, size, event,
3442 format, len_arg, arg->op.right->op.right);
3443 break;
3444 case PRINT_FUNC:
3445 process_defined_func(s, data, size, event, arg);
3446 break;
3447 default:
3448 /* well... */
3449 break;
3450 }
3451}
3452
3453static unsigned long long
3454process_defined_func(struct trace_seq *s, void *data, int size,
3455 struct event_format *event, struct print_arg *arg)
3456{
3457 struct pevent_function_handler *func_handle = arg->func.func;
3458 struct pevent_func_params *param;
3459 unsigned long long *args;
3460 unsigned long long ret;
3461 struct print_arg *farg;
3462 struct trace_seq str;
3463 struct save_str {
3464 struct save_str *next;
3465 char *str;
3466 } *strings = NULL, *string;
3467 int i;
3468
3469 if (!func_handle->nr_args) {
3470 ret = (*func_handle->func)(s, NULL);
3471 goto out;
3472 }
3473
3474 farg = arg->func.args;
3475 param = func_handle->params;
3476
3477 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3478 for (i = 0; i < func_handle->nr_args; i++) {
3479 switch (param->type) {
3480 case PEVENT_FUNC_ARG_INT:
3481 case PEVENT_FUNC_ARG_LONG:
3482 case PEVENT_FUNC_ARG_PTR:
3483 args[i] = eval_num_arg(data, size, event, farg);
3484 break;
3485 case PEVENT_FUNC_ARG_STRING:
3486 trace_seq_init(&str);
3487 print_str_arg(&str, data, size, event, "%s", -1, farg);
3488 trace_seq_terminate(&str);
3489 string = malloc_or_die(sizeof(*string));
3490 string->next = strings;
3491 string->str = strdup(str.buffer);
Namhyung Kimca638582012-04-09 11:54:31 +09003492 if (!string->str)
3493 die("malloc str");
3494
Robert Richter0cf26012012-08-07 19:43:14 +02003495 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003496 strings = string;
3497 trace_seq_destroy(&str);
3498 break;
3499 default:
3500 /*
3501 * Something went totally wrong, this is not
3502 * an input error, something in this code broke.
3503 */
3504 die("Unexpected end of arguments\n");
3505 break;
3506 }
3507 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003508 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003509 }
3510
3511 ret = (*func_handle->func)(s, args);
3512 free(args);
3513 while (strings) {
3514 string = strings;
3515 strings = string->next;
3516 free(string->str);
3517 free(string);
3518 }
3519
3520 out:
3521 /* TBD : handle return type here */
3522 return ret;
3523}
3524
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003525static void free_args(struct print_arg *args)
3526{
3527 struct print_arg *next;
3528
3529 while (args) {
3530 next = args->next;
3531
3532 free_arg(args);
3533 args = next;
3534 }
3535}
3536
Steven Rostedtf7d82352012-04-06 00:47:53 +02003537static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3538{
3539 struct pevent *pevent = event->pevent;
3540 struct format_field *field, *ip_field;
3541 struct print_arg *args, *arg, **next;
3542 unsigned long long ip, val;
3543 char *ptr;
3544 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003545 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003546
3547 field = pevent->bprint_buf_field;
3548 ip_field = pevent->bprint_ip_field;
3549
3550 if (!field) {
3551 field = pevent_find_field(event, "buf");
3552 if (!field)
3553 die("can't find buffer field for binary printk");
3554 ip_field = pevent_find_field(event, "ip");
3555 if (!ip_field)
3556 die("can't find ip field for binary printk");
3557 pevent->bprint_buf_field = field;
3558 pevent->bprint_ip_field = ip_field;
3559 }
3560
3561 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3562
3563 /*
3564 * The first arg is the IP pointer.
3565 */
3566 args = alloc_arg();
3567 arg = args;
3568 arg->next = NULL;
3569 next = &arg->next;
3570
3571 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003572
3573 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3574 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003575
3576 /* skip the first "%pf : " */
3577 for (ptr = fmt + 6, bptr = data + field->offset;
3578 bptr < data + size && *ptr; ptr++) {
3579 int ls = 0;
3580
3581 if (*ptr == '%') {
3582 process_again:
3583 ptr++;
3584 switch (*ptr) {
3585 case '%':
3586 break;
3587 case 'l':
3588 ls++;
3589 goto process_again;
3590 case 'L':
3591 ls = 2;
3592 goto process_again;
3593 case '0' ... '9':
3594 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003595 case '.':
3596 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003597 case 'p':
3598 ls = 1;
3599 /* fall through */
3600 case 'd':
3601 case 'u':
3602 case 'x':
3603 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003604 switch (ls) {
3605 case 0:
3606 vsize = 4;
3607 break;
3608 case 1:
3609 vsize = pevent->long_size;
3610 break;
3611 case 2:
3612 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003613 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003614 default:
3615 vsize = ls; /* ? */
3616 break;
3617 }
3618 /* fall through */
3619 case '*':
3620 if (*ptr == '*')
3621 vsize = 4;
3622
Steven Rostedtf7d82352012-04-06 00:47:53 +02003623 /* the pointers are always 4 bytes aligned */
3624 bptr = (void *)(((unsigned long)bptr + 3) &
3625 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003626 val = pevent_read_number(pevent, bptr, vsize);
3627 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003628 arg = alloc_arg();
3629 arg->next = NULL;
3630 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003631 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3632 free(arg);
3633 goto out_free;
3634 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003635 *next = arg;
3636 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003637 /*
3638 * The '*' case means that an arg is used as the length.
3639 * We need to continue to figure out for what.
3640 */
3641 if (*ptr == '*')
3642 goto process_again;
3643
Steven Rostedtf7d82352012-04-06 00:47:53 +02003644 break;
3645 case 's':
3646 arg = alloc_arg();
3647 arg->next = NULL;
3648 arg->type = PRINT_BSTRING;
3649 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003650 if (!arg->string.string)
3651 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003652 bptr += strlen(bptr) + 1;
3653 *next = arg;
3654 next = &arg->next;
3655 default:
3656 break;
3657 }
3658 }
3659 }
3660
3661 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003662
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003663out_free:
3664 free_args(args);
3665 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003666}
3667
3668static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003669get_bprint_format(void *data, int size __maybe_unused,
3670 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003671{
3672 struct pevent *pevent = event->pevent;
3673 unsigned long long addr;
3674 struct format_field *field;
3675 struct printk_map *printk;
3676 char *format;
3677 char *p;
3678
3679 field = pevent->bprint_fmt_field;
3680
3681 if (!field) {
3682 field = pevent_find_field(event, "fmt");
3683 if (!field)
3684 die("can't find format field for binary printk");
3685 pevent->bprint_fmt_field = field;
3686 }
3687
3688 addr = pevent_read_number(pevent, data + field->offset, field->size);
3689
3690 printk = find_printk(pevent, addr);
3691 if (!printk) {
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003692 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3693 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003694 return format;
3695 }
3696
3697 p = printk->printk;
3698 /* Remove any quotes. */
3699 if (*p == '"')
3700 p++;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003701 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3702 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003703 /* remove ending quotes and new line since we will add one too */
3704 p = format + strlen(format) - 1;
3705 if (*p == '"')
3706 *p = 0;
3707
3708 p -= 2;
3709 if (strcmp(p, "\\n") == 0)
3710 *p = 0;
3711
3712 return format;
3713}
3714
3715static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3716 struct event_format *event, struct print_arg *arg)
3717{
3718 unsigned char *buf;
3719 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3720
3721 if (arg->type == PRINT_FUNC) {
3722 process_defined_func(s, data, size, event, arg);
3723 return;
3724 }
3725
3726 if (arg->type != PRINT_FIELD) {
3727 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3728 arg->type);
3729 return;
3730 }
3731
3732 if (mac == 'm')
3733 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3734 if (!arg->field.field) {
3735 arg->field.field =
3736 pevent_find_any_field(event, arg->field.name);
3737 if (!arg->field.field)
3738 die("field %s not found", arg->field.name);
3739 }
3740 if (arg->field.field->size != 6) {
3741 trace_seq_printf(s, "INVALIDMAC");
3742 return;
3743 }
3744 buf = data + arg->field.field->offset;
3745 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3746}
3747
Namhyung Kim600da3c2012-06-22 17:10:15 +09003748static int is_printable_array(char *p, unsigned int len)
3749{
3750 unsigned int i;
3751
3752 for (i = 0; i < len && p[i]; i++)
3753 if (!isprint(p[i]))
3754 return 0;
3755 return 1;
3756}
3757
Steven Rostedtf7d82352012-04-06 00:47:53 +02003758static void print_event_fields(struct trace_seq *s, void *data, int size,
3759 struct event_format *event)
3760{
3761 struct format_field *field;
3762 unsigned long long val;
3763 unsigned int offset, len, i;
3764
3765 field = event->format.fields;
3766 while (field) {
3767 trace_seq_printf(s, " %s=", field->name);
3768 if (field->flags & FIELD_IS_ARRAY) {
3769 offset = field->offset;
3770 len = field->size;
3771 if (field->flags & FIELD_IS_DYNAMIC) {
3772 val = pevent_read_number(event->pevent, data + offset, len);
3773 offset = val;
3774 len = offset >> 16;
3775 offset &= 0xffff;
3776 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09003777 if (field->flags & FIELD_IS_STRING &&
3778 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003779 trace_seq_printf(s, "%s", (char *)data + offset);
3780 } else {
3781 trace_seq_puts(s, "ARRAY[");
3782 for (i = 0; i < len; i++) {
3783 if (i)
3784 trace_seq_puts(s, ", ");
3785 trace_seq_printf(s, "%02x",
3786 *((unsigned char *)data + offset + i));
3787 }
3788 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09003789 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003790 }
3791 } else {
3792 val = pevent_read_number(event->pevent, data + field->offset,
3793 field->size);
3794 if (field->flags & FIELD_IS_POINTER) {
3795 trace_seq_printf(s, "0x%llx", val);
3796 } else if (field->flags & FIELD_IS_SIGNED) {
3797 switch (field->size) {
3798 case 4:
3799 /*
3800 * If field is long then print it in hex.
3801 * A long usually stores pointers.
3802 */
3803 if (field->flags & FIELD_IS_LONG)
3804 trace_seq_printf(s, "0x%x", (int)val);
3805 else
3806 trace_seq_printf(s, "%d", (int)val);
3807 break;
3808 case 2:
3809 trace_seq_printf(s, "%2d", (short)val);
3810 break;
3811 case 1:
3812 trace_seq_printf(s, "%1d", (char)val);
3813 break;
3814 default:
3815 trace_seq_printf(s, "%lld", val);
3816 }
3817 } else {
3818 if (field->flags & FIELD_IS_LONG)
3819 trace_seq_printf(s, "0x%llx", val);
3820 else
3821 trace_seq_printf(s, "%llu", val);
3822 }
3823 }
3824 field = field->next;
3825 }
3826}
3827
3828static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3829{
3830 struct pevent *pevent = event->pevent;
3831 struct print_fmt *print_fmt = &event->print_fmt;
3832 struct print_arg *arg = print_fmt->args;
3833 struct print_arg *args = NULL;
3834 const char *ptr = print_fmt->format;
3835 unsigned long long val;
3836 struct func_map *func;
3837 const char *saveptr;
3838 char *bprint_fmt = NULL;
3839 char format[32];
3840 int show_func;
3841 int len_as_arg;
3842 int len_arg;
3843 int len;
3844 int ls;
3845
3846 if (event->flags & EVENT_FL_FAILED) {
3847 trace_seq_printf(s, "[FAILED TO PARSE]");
3848 print_event_fields(s, data, size, event);
3849 return;
3850 }
3851
3852 if (event->flags & EVENT_FL_ISBPRINT) {
3853 bprint_fmt = get_bprint_format(data, size, event);
3854 args = make_bprint_args(bprint_fmt, data, size, event);
3855 arg = args;
3856 ptr = bprint_fmt;
3857 }
3858
3859 for (; *ptr; ptr++) {
3860 ls = 0;
3861 if (*ptr == '\\') {
3862 ptr++;
3863 switch (*ptr) {
3864 case 'n':
3865 trace_seq_putc(s, '\n');
3866 break;
3867 case 't':
3868 trace_seq_putc(s, '\t');
3869 break;
3870 case 'r':
3871 trace_seq_putc(s, '\r');
3872 break;
3873 case '\\':
3874 trace_seq_putc(s, '\\');
3875 break;
3876 default:
3877 trace_seq_putc(s, *ptr);
3878 break;
3879 }
3880
3881 } else if (*ptr == '%') {
3882 saveptr = ptr;
3883 show_func = 0;
3884 len_as_arg = 0;
3885 cont_process:
3886 ptr++;
3887 switch (*ptr) {
3888 case '%':
3889 trace_seq_putc(s, '%');
3890 break;
3891 case '#':
3892 /* FIXME: need to handle properly */
3893 goto cont_process;
3894 case 'h':
3895 ls--;
3896 goto cont_process;
3897 case 'l':
3898 ls++;
3899 goto cont_process;
3900 case 'L':
3901 ls = 2;
3902 goto cont_process;
3903 case '*':
3904 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003905 if (!arg) {
3906 do_warning("no argument match");
3907 event->flags |= EVENT_FL_FAILED;
3908 goto out_failed;
3909 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003910 len_arg = eval_num_arg(data, size, event, arg);
3911 len_as_arg = 1;
3912 arg = arg->next;
3913 goto cont_process;
3914 case '.':
3915 case 'z':
3916 case 'Z':
3917 case '0' ... '9':
3918 goto cont_process;
3919 case 'p':
3920 if (pevent->long_size == 4)
3921 ls = 1;
3922 else
3923 ls = 2;
3924
3925 if (*(ptr+1) == 'F' ||
3926 *(ptr+1) == 'f') {
3927 ptr++;
3928 show_func = *ptr;
3929 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3930 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3931 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05003932 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003933 break;
3934 }
3935
3936 /* fall through */
3937 case 'd':
3938 case 'i':
3939 case 'x':
3940 case 'X':
3941 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09003942 if (!arg) {
3943 do_warning("no argument match");
3944 event->flags |= EVENT_FL_FAILED;
3945 goto out_failed;
3946 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003947
3948 len = ((unsigned long)ptr + 1) -
3949 (unsigned long)saveptr;
3950
3951 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09003952 if (len > 31) {
3953 do_warning("bad format!");
3954 event->flags |= EVENT_FL_FAILED;
3955 len = 31;
3956 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003957
3958 memcpy(format, saveptr, len);
3959 format[len] = 0;
3960
3961 val = eval_num_arg(data, size, event, arg);
3962 arg = arg->next;
3963
3964 if (show_func) {
3965 func = find_func(pevent, val);
3966 if (func) {
3967 trace_seq_puts(s, func->func);
3968 if (show_func == 'F')
3969 trace_seq_printf(s,
3970 "+0x%llx",
3971 val - func->addr);
3972 break;
3973 }
3974 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003975 if (pevent->long_size == 8 && ls &&
3976 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003977 char *p;
3978
3979 ls = 2;
3980 /* make %l into %ll */
3981 p = strchr(format, 'l');
3982 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01003983 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003984 else if (strcmp(format, "%p") == 0)
3985 strcpy(format, "0x%llx");
3986 }
3987 switch (ls) {
3988 case -2:
3989 if (len_as_arg)
3990 trace_seq_printf(s, format, len_arg, (char)val);
3991 else
3992 trace_seq_printf(s, format, (char)val);
3993 break;
3994 case -1:
3995 if (len_as_arg)
3996 trace_seq_printf(s, format, len_arg, (short)val);
3997 else
3998 trace_seq_printf(s, format, (short)val);
3999 break;
4000 case 0:
4001 if (len_as_arg)
4002 trace_seq_printf(s, format, len_arg, (int)val);
4003 else
4004 trace_seq_printf(s, format, (int)val);
4005 break;
4006 case 1:
4007 if (len_as_arg)
4008 trace_seq_printf(s, format, len_arg, (long)val);
4009 else
4010 trace_seq_printf(s, format, (long)val);
4011 break;
4012 case 2:
4013 if (len_as_arg)
4014 trace_seq_printf(s, format, len_arg,
4015 (long long)val);
4016 else
4017 trace_seq_printf(s, format, (long long)val);
4018 break;
4019 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004020 do_warning("bad count (%d)", ls);
4021 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004022 }
4023 break;
4024 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004025 if (!arg) {
4026 do_warning("no matching argument");
4027 event->flags |= EVENT_FL_FAILED;
4028 goto out_failed;
4029 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004030
4031 len = ((unsigned long)ptr + 1) -
4032 (unsigned long)saveptr;
4033
4034 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004035 if (len > 31) {
4036 do_warning("bad format!");
4037 event->flags |= EVENT_FL_FAILED;
4038 len = 31;
4039 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004040
4041 memcpy(format, saveptr, len);
4042 format[len] = 0;
4043 if (!len_as_arg)
4044 len_arg = -1;
4045 print_str_arg(s, data, size, event,
4046 format, len_arg, arg);
4047 arg = arg->next;
4048 break;
4049 default:
4050 trace_seq_printf(s, ">%c<", *ptr);
4051
4052 }
4053 } else
4054 trace_seq_putc(s, *ptr);
4055 }
4056
Namhyung Kim245c5a12012-09-07 11:49:45 +09004057 if (event->flags & EVENT_FL_FAILED) {
4058out_failed:
4059 trace_seq_printf(s, "[FAILED TO PARSE]");
4060 }
4061
Steven Rostedtf7d82352012-04-06 00:47:53 +02004062 if (args) {
4063 free_args(args);
4064 free(bprint_fmt);
4065 }
4066}
4067
4068/**
4069 * pevent_data_lat_fmt - parse the data for the latency format
4070 * @pevent: a handle to the pevent
4071 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004072 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004073 *
4074 * This parses out the Latency format (interrupts disabled,
4075 * need rescheduling, in hard/soft interrupt, preempt count
4076 * and lock depth) and places it into the trace_seq.
4077 */
4078void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004079 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004080{
4081 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004082 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004083 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004084 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004085 unsigned int lat_flags;
4086 unsigned int pc;
4087 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004088 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004089 int hardirq;
4090 int softirq;
4091 void *data = record->data;
4092
4093 lat_flags = parse_common_flags(pevent, data);
4094 pc = parse_common_pc(pevent, data);
4095 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004096 if (lock_depth_exists)
4097 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004098 else if (check_lock_depth) {
4099 lock_depth = parse_common_lock_depth(pevent, data);
4100 if (lock_depth < 0)
4101 check_lock_depth = 0;
4102 else
4103 lock_depth_exists = 1;
4104 }
4105
4106 /* migrate_disable may not always exist */
4107 if (migrate_disable_exists)
4108 migrate_disable = parse_common_migrate_disable(pevent, data);
4109 else if (check_migrate_disable) {
4110 migrate_disable = parse_common_migrate_disable(pevent, data);
4111 if (migrate_disable < 0)
4112 check_migrate_disable = 0;
4113 else
4114 migrate_disable_exists = 1;
4115 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004116
4117 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4118 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4119
4120 trace_seq_printf(s, "%c%c%c",
4121 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4122 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4123 'X' : '.',
4124 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4125 'N' : '.',
4126 (hardirq && softirq) ? 'H' :
4127 hardirq ? 'h' : softirq ? 's' : '.');
4128
4129 if (pc)
4130 trace_seq_printf(s, "%x", pc);
4131 else
4132 trace_seq_putc(s, '.');
4133
Steven Rostedt0866a972012-05-22 14:52:40 +09004134 if (migrate_disable_exists) {
4135 if (migrate_disable < 0)
4136 trace_seq_putc(s, '.');
4137 else
4138 trace_seq_printf(s, "%d", migrate_disable);
4139 }
4140
Steven Rostedtf7d82352012-04-06 00:47:53 +02004141 if (lock_depth_exists) {
4142 if (lock_depth < 0)
4143 trace_seq_putc(s, '.');
4144 else
4145 trace_seq_printf(s, "%d", lock_depth);
4146 }
4147
4148 trace_seq_terminate(s);
4149}
4150
4151/**
4152 * pevent_data_type - parse out the given event type
4153 * @pevent: a handle to the pevent
4154 * @rec: the record to read from
4155 *
4156 * This returns the event id from the @rec.
4157 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004158int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004159{
4160 return trace_parse_common_type(pevent, rec->data);
4161}
4162
4163/**
4164 * pevent_data_event_from_type - find the event by a given type
4165 * @pevent: a handle to the pevent
4166 * @type: the type of the event.
4167 *
4168 * This returns the event form a given @type;
4169 */
4170struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4171{
4172 return pevent_find_event(pevent, type);
4173}
4174
4175/**
4176 * pevent_data_pid - parse the PID from raw data
4177 * @pevent: a handle to the pevent
4178 * @rec: the record to parse
4179 *
4180 * This returns the PID from a raw data.
4181 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004182int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004183{
4184 return parse_common_pid(pevent, rec->data);
4185}
4186
4187/**
4188 * pevent_data_comm_from_pid - return the command line from PID
4189 * @pevent: a handle to the pevent
4190 * @pid: the PID of the task to search for
4191 *
4192 * This returns a pointer to the command line that has the given
4193 * @pid.
4194 */
4195const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4196{
4197 const char *comm;
4198
4199 comm = find_cmdline(pevent, pid);
4200 return comm;
4201}
4202
4203/**
4204 * pevent_data_comm_from_pid - parse the data into the print format
4205 * @s: the trace_seq to write to
4206 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004207 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004208 *
4209 * This parses the raw @data using the given @event information and
4210 * writes the print format into the trace_seq.
4211 */
4212void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004213 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004214{
4215 int print_pretty = 1;
4216
4217 if (event->pevent->print_raw)
4218 print_event_fields(s, record->data, record->size, event);
4219 else {
4220
4221 if (event->handler)
4222 print_pretty = event->handler(s, record, event,
4223 event->context);
4224
4225 if (print_pretty)
4226 pretty_print(s, record->data, record->size, event);
4227 }
4228
4229 trace_seq_terminate(s);
4230}
4231
4232void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Steven Rostedt1c698182012-04-06 00:48:06 +02004233 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004234{
4235 static char *spaces = " "; /* 20 spaces */
4236 struct event_format *event;
4237 unsigned long secs;
4238 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004239 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004240 const char *comm;
4241 void *data = record->data;
4242 int type;
4243 int pid;
4244 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004245 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004246
4247 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004248 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004249
4250 if (record->size < 0) {
4251 do_warning("ug! negative record size %d", record->size);
4252 return;
4253 }
4254
4255 type = trace_parse_common_type(pevent, data);
4256
4257 event = pevent_find_event(pevent, type);
4258 if (!event) {
4259 do_warning("ug! no event found for type %d", type);
4260 return;
4261 }
4262
4263 pid = parse_common_pid(pevent, data);
4264 comm = find_cmdline(pevent, pid);
4265
4266 if (pevent->latency_format) {
4267 trace_seq_printf(s, "%8.8s-%-5d %3d",
4268 comm, pid, record->cpu);
4269 pevent_data_lat_fmt(pevent, s, record);
4270 } else
4271 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4272
Steven Rostedt4dc10242012-04-06 00:47:57 +02004273 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4274 usecs = nsecs;
4275 p = 9;
4276 } else {
4277 usecs = (nsecs + 500) / NSECS_PER_USEC;
4278 p = 6;
4279 }
4280
4281 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004282
4283 /* Space out the event names evenly. */
4284 len = strlen(event->name);
4285 if (len < 20)
4286 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4287
4288 pevent_event_info(s, event, record);
4289}
4290
4291static int events_id_cmp(const void *a, const void *b)
4292{
4293 struct event_format * const * ea = a;
4294 struct event_format * const * eb = b;
4295
4296 if ((*ea)->id < (*eb)->id)
4297 return -1;
4298
4299 if ((*ea)->id > (*eb)->id)
4300 return 1;
4301
4302 return 0;
4303}
4304
4305static int events_name_cmp(const void *a, const void *b)
4306{
4307 struct event_format * const * ea = a;
4308 struct event_format * const * eb = b;
4309 int res;
4310
4311 res = strcmp((*ea)->name, (*eb)->name);
4312 if (res)
4313 return res;
4314
4315 res = strcmp((*ea)->system, (*eb)->system);
4316 if (res)
4317 return res;
4318
4319 return events_id_cmp(a, b);
4320}
4321
4322static int events_system_cmp(const void *a, const void *b)
4323{
4324 struct event_format * const * ea = a;
4325 struct event_format * const * eb = b;
4326 int res;
4327
4328 res = strcmp((*ea)->system, (*eb)->system);
4329 if (res)
4330 return res;
4331
4332 res = strcmp((*ea)->name, (*eb)->name);
4333 if (res)
4334 return res;
4335
4336 return events_id_cmp(a, b);
4337}
4338
4339struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4340{
4341 struct event_format **events;
4342 int (*sort)(const void *a, const void *b);
4343
4344 events = pevent->sort_events;
4345
4346 if (events && pevent->last_type == sort_type)
4347 return events;
4348
4349 if (!events) {
4350 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4351 if (!events)
4352 return NULL;
4353
4354 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4355 events[pevent->nr_events] = NULL;
4356
4357 pevent->sort_events = events;
4358
4359 /* the internal events are sorted by id */
4360 if (sort_type == EVENT_SORT_ID) {
4361 pevent->last_type = sort_type;
4362 return events;
4363 }
4364 }
4365
4366 switch (sort_type) {
4367 case EVENT_SORT_ID:
4368 sort = events_id_cmp;
4369 break;
4370 case EVENT_SORT_NAME:
4371 sort = events_name_cmp;
4372 break;
4373 case EVENT_SORT_SYSTEM:
4374 sort = events_system_cmp;
4375 break;
4376 default:
4377 return events;
4378 }
4379
4380 qsort(events, pevent->nr_events, sizeof(*events), sort);
4381 pevent->last_type = sort_type;
4382
4383 return events;
4384}
4385
4386static struct format_field **
4387get_event_fields(const char *type, const char *name,
4388 int count, struct format_field *list)
4389{
4390 struct format_field **fields;
4391 struct format_field *field;
4392 int i = 0;
4393
4394 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4395 for (field = list; field; field = field->next) {
4396 fields[i++] = field;
4397 if (i == count + 1) {
4398 do_warning("event %s has more %s fields than specified",
4399 name, type);
4400 i--;
4401 break;
4402 }
4403 }
4404
4405 if (i != count)
4406 do_warning("event %s has less %s fields than specified",
4407 name, type);
4408
4409 fields[i] = NULL;
4410
4411 return fields;
4412}
4413
4414/**
4415 * pevent_event_common_fields - return a list of common fields for an event
4416 * @event: the event to return the common fields of.
4417 *
4418 * Returns an allocated array of fields. The last item in the array is NULL.
4419 * The array must be freed with free().
4420 */
4421struct format_field **pevent_event_common_fields(struct event_format *event)
4422{
4423 return get_event_fields("common", event->name,
4424 event->format.nr_common,
4425 event->format.common_fields);
4426}
4427
4428/**
4429 * pevent_event_fields - return a list of event specific fields for an event
4430 * @event: the event to return the fields of.
4431 *
4432 * Returns an allocated array of fields. The last item in the array is NULL.
4433 * The array must be freed with free().
4434 */
4435struct format_field **pevent_event_fields(struct event_format *event)
4436{
4437 return get_event_fields("event", event->name,
4438 event->format.nr_fields,
4439 event->format.fields);
4440}
4441
4442static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4443{
4444 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4445 if (field->next) {
4446 trace_seq_puts(s, ", ");
4447 print_fields(s, field->next);
4448 }
4449}
4450
4451/* for debugging */
4452static void print_args(struct print_arg *args)
4453{
4454 int print_paren = 1;
4455 struct trace_seq s;
4456
4457 switch (args->type) {
4458 case PRINT_NULL:
4459 printf("null");
4460 break;
4461 case PRINT_ATOM:
4462 printf("%s", args->atom.atom);
4463 break;
4464 case PRINT_FIELD:
4465 printf("REC->%s", args->field.name);
4466 break;
4467 case PRINT_FLAGS:
4468 printf("__print_flags(");
4469 print_args(args->flags.field);
4470 printf(", %s, ", args->flags.delim);
4471 trace_seq_init(&s);
4472 print_fields(&s, args->flags.flags);
4473 trace_seq_do_printf(&s);
4474 trace_seq_destroy(&s);
4475 printf(")");
4476 break;
4477 case PRINT_SYMBOL:
4478 printf("__print_symbolic(");
4479 print_args(args->symbol.field);
4480 printf(", ");
4481 trace_seq_init(&s);
4482 print_fields(&s, args->symbol.symbols);
4483 trace_seq_do_printf(&s);
4484 trace_seq_destroy(&s);
4485 printf(")");
4486 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004487 case PRINT_HEX:
4488 printf("__print_hex(");
4489 print_args(args->hex.field);
4490 printf(", ");
4491 print_args(args->hex.size);
4492 printf(")");
4493 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004494 case PRINT_STRING:
4495 case PRINT_BSTRING:
4496 printf("__get_str(%s)", args->string.string);
4497 break;
4498 case PRINT_TYPE:
4499 printf("(%s)", args->typecast.type);
4500 print_args(args->typecast.item);
4501 break;
4502 case PRINT_OP:
4503 if (strcmp(args->op.op, ":") == 0)
4504 print_paren = 0;
4505 if (print_paren)
4506 printf("(");
4507 print_args(args->op.left);
4508 printf(" %s ", args->op.op);
4509 print_args(args->op.right);
4510 if (print_paren)
4511 printf(")");
4512 break;
4513 default:
4514 /* we should warn... */
4515 return;
4516 }
4517 if (args->next) {
4518 printf("\n");
4519 print_args(args->next);
4520 }
4521}
4522
4523static void parse_header_field(const char *field,
4524 int *offset, int *size, int mandatory)
4525{
4526 unsigned long long save_input_buf_ptr;
4527 unsigned long long save_input_buf_siz;
4528 char *token;
4529 int type;
4530
4531 save_input_buf_ptr = input_buf_ptr;
4532 save_input_buf_siz = input_buf_siz;
4533
4534 if (read_expected(EVENT_ITEM, "field") < 0)
4535 return;
4536 if (read_expected(EVENT_OP, ":") < 0)
4537 return;
4538
4539 /* type */
4540 if (read_expect_type(EVENT_ITEM, &token) < 0)
4541 goto fail;
4542 free_token(token);
4543
4544 /*
4545 * If this is not a mandatory field, then test it first.
4546 */
4547 if (mandatory) {
4548 if (read_expected(EVENT_ITEM, field) < 0)
4549 return;
4550 } else {
4551 if (read_expect_type(EVENT_ITEM, &token) < 0)
4552 goto fail;
4553 if (strcmp(token, field) != 0)
4554 goto discard;
4555 free_token(token);
4556 }
4557
4558 if (read_expected(EVENT_OP, ";") < 0)
4559 return;
4560 if (read_expected(EVENT_ITEM, "offset") < 0)
4561 return;
4562 if (read_expected(EVENT_OP, ":") < 0)
4563 return;
4564 if (read_expect_type(EVENT_ITEM, &token) < 0)
4565 goto fail;
4566 *offset = atoi(token);
4567 free_token(token);
4568 if (read_expected(EVENT_OP, ";") < 0)
4569 return;
4570 if (read_expected(EVENT_ITEM, "size") < 0)
4571 return;
4572 if (read_expected(EVENT_OP, ":") < 0)
4573 return;
4574 if (read_expect_type(EVENT_ITEM, &token) < 0)
4575 goto fail;
4576 *size = atoi(token);
4577 free_token(token);
4578 if (read_expected(EVENT_OP, ";") < 0)
4579 return;
4580 type = read_token(&token);
4581 if (type != EVENT_NEWLINE) {
4582 /* newer versions of the kernel have a "signed" type */
4583 if (type != EVENT_ITEM)
4584 goto fail;
4585
4586 if (strcmp(token, "signed") != 0)
4587 goto fail;
4588
4589 free_token(token);
4590
4591 if (read_expected(EVENT_OP, ":") < 0)
4592 return;
4593
4594 if (read_expect_type(EVENT_ITEM, &token))
4595 goto fail;
4596
4597 free_token(token);
4598 if (read_expected(EVENT_OP, ";") < 0)
4599 return;
4600
4601 if (read_expect_type(EVENT_NEWLINE, &token))
4602 goto fail;
4603 }
4604 fail:
4605 free_token(token);
4606 return;
4607
4608 discard:
4609 input_buf_ptr = save_input_buf_ptr;
4610 input_buf_siz = save_input_buf_siz;
4611 *offset = 0;
4612 *size = 0;
4613 free_token(token);
4614}
4615
4616/**
4617 * pevent_parse_header_page - parse the data stored in the header page
4618 * @pevent: the handle to the pevent
4619 * @buf: the buffer storing the header page format string
4620 * @size: the size of @buf
4621 * @long_size: the long size to use if there is no header
4622 *
4623 * This parses the header page format for information on the
4624 * ring buffer used. The @buf should be copied from
4625 *
4626 * /sys/kernel/debug/tracing/events/header_page
4627 */
4628int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4629 int long_size)
4630{
4631 int ignore;
4632
4633 if (!size) {
4634 /*
4635 * Old kernels did not have header page info.
4636 * Sorry but we just use what we find here in user space.
4637 */
4638 pevent->header_page_ts_size = sizeof(long long);
4639 pevent->header_page_size_size = long_size;
4640 pevent->header_page_data_offset = sizeof(long long) + long_size;
4641 pevent->old_format = 1;
4642 return -1;
4643 }
4644 init_input_buf(buf, size);
4645
4646 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4647 &pevent->header_page_ts_size, 1);
4648 parse_header_field("commit", &pevent->header_page_size_offset,
4649 &pevent->header_page_size_size, 1);
4650 parse_header_field("overwrite", &pevent->header_page_overwrite,
4651 &ignore, 0);
4652 parse_header_field("data", &pevent->header_page_data_offset,
4653 &pevent->header_page_data_size, 1);
4654
4655 return 0;
4656}
4657
4658static int event_matches(struct event_format *event,
4659 int id, const char *sys_name,
4660 const char *event_name)
4661{
4662 if (id >= 0 && id != event->id)
4663 return 0;
4664
4665 if (event_name && (strcmp(event_name, event->name) != 0))
4666 return 0;
4667
4668 if (sys_name && (strcmp(sys_name, event->system) != 0))
4669 return 0;
4670
4671 return 1;
4672}
4673
4674static void free_handler(struct event_handler *handle)
4675{
4676 free((void *)handle->sys_name);
4677 free((void *)handle->event_name);
4678 free(handle);
4679}
4680
4681static int find_event_handle(struct pevent *pevent, struct event_format *event)
4682{
4683 struct event_handler *handle, **next;
4684
4685 for (next = &pevent->handlers; *next;
4686 next = &(*next)->next) {
4687 handle = *next;
4688 if (event_matches(event, handle->id,
4689 handle->sys_name,
4690 handle->event_name))
4691 break;
4692 }
4693
4694 if (!(*next))
4695 return 0;
4696
4697 pr_stat("overriding event (%d) %s:%s with new print handler",
4698 event->id, event->system, event->name);
4699
4700 event->handler = handle->func;
4701 event->context = handle->context;
4702
4703 *next = handle->next;
4704 free_handler(handle);
4705
4706 return 1;
4707}
4708
4709/**
4710 * pevent_parse_event - parse the event format
4711 * @pevent: the handle to the pevent
4712 * @buf: the buffer storing the event format string
4713 * @size: the size of @buf
4714 * @sys: the system the event belongs to
4715 *
4716 * This parses the event format and creates an event structure
4717 * to quickly parse raw data for a given event.
4718 *
4719 * These files currently come from:
4720 *
4721 * /sys/kernel/debug/tracing/events/.../.../format
4722 */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004723enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4724 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004725{
4726 struct event_format *event;
4727 int ret;
4728
4729 init_input_buf(buf, size);
4730
4731 event = alloc_event();
4732 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09004733 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004734
4735 event->name = event_read_name();
4736 if (!event->name) {
4737 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004738 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4739 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004740 }
4741
4742 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004743 event->flags |= EVENT_FL_ISFTRACE;
4744
4745 if (strcmp(event->name, "bprint") == 0)
4746 event->flags |= EVENT_FL_ISBPRINT;
4747 }
4748
4749 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09004750 if (event->id < 0) {
4751 ret = PEVENT_ERRNO__READ_ID_FAILED;
4752 /*
4753 * This isn't an allocation error actually.
4754 * But as the ID is critical, just bail out.
4755 */
4756 goto event_alloc_failed;
4757 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004758
4759 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004760 if (!event->system) {
4761 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4762 goto event_alloc_failed;
4763 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004764
4765 /* Add pevent to event so that it can be referenced */
4766 event->pevent = pevent;
4767
4768 ret = event_read_format(event);
4769 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09004770 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4771 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004772 }
4773
4774 /*
4775 * If the event has an override, don't print warnings if the event
4776 * print format fails to parse.
4777 */
4778 if (find_event_handle(pevent, event))
4779 show_warning = 0;
4780
4781 ret = event_read_print(event);
4782 if (ret < 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004783 show_warning = 1;
Namhyung Kimbffddff2012-08-22 16:00:29 +09004784 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4785 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004786 }
4787 show_warning = 1;
4788
4789 add_event(pevent, event);
4790
4791 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4792 struct format_field *field;
4793 struct print_arg *arg, **list;
4794
4795 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004796 list = &event->print_fmt.args;
4797 for (field = event->format.fields; field; field = field->next) {
4798 arg = alloc_arg();
Steven Rostedtf7d82352012-04-06 00:47:53 +02004799 arg->type = PRINT_FIELD;
4800 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09004801 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09004802 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004803 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004804 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09004805 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004806 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004807 *list = arg;
4808 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004809 }
4810 return 0;
4811 }
4812
4813#define PRINT_ARGS 0
4814 if (PRINT_ARGS && event->print_fmt.args)
4815 print_args(event->print_fmt.args);
4816
4817 return 0;
4818
Namhyung Kimbffddff2012-08-22 16:00:29 +09004819 event_parse_failed:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004820 event->flags |= EVENT_FL_FAILED;
4821 /* still add it even if it failed */
4822 add_event(pevent, event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004823 return ret;
4824
4825 event_alloc_failed:
4826 free(event->system);
4827 free(event->name);
4828 free(event);
4829 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004830}
4831
Namhyung Kim2f197b92012-08-22 16:00:30 +09004832#undef _PE
4833#define _PE(code, str) str
4834static const char * const pevent_error_str[] = {
4835 PEVENT_ERRORS
4836};
4837#undef _PE
4838
4839int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4840 char *buf, size_t buflen)
4841{
4842 int idx;
4843 const char *msg;
4844
4845 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004846 msg = strerror_r(errnum, buf, buflen);
4847 if (msg != buf) {
4848 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03004849 memcpy(buf, msg, min(buflen - 1, len));
4850 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09004851 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09004852 return 0;
4853 }
4854
4855 if (errnum <= __PEVENT_ERRNO__START ||
4856 errnum >= __PEVENT_ERRNO__END)
4857 return -1;
4858
Namhyung Kimf63fe792012-08-23 16:37:00 +09004859 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09004860 msg = pevent_error_str[idx];
4861
4862 switch (errnum) {
4863 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4864 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4865 case PEVENT_ERRNO__READ_ID_FAILED:
4866 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4867 case PEVENT_ERRNO__READ_PRINT_FAILED:
4868 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4869 snprintf(buf, buflen, "%s", msg);
4870 break;
4871
4872 default:
4873 /* cannot reach here */
4874 break;
4875 }
4876
4877 return 0;
4878}
4879
Steven Rostedtf7d82352012-04-06 00:47:53 +02004880int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02004881 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004882 unsigned long long *val, int err)
4883{
4884 if (!field) {
4885 if (err)
4886 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4887 return -1;
4888 }
4889
4890 if (pevent_read_number_field(field, record->data, val)) {
4891 if (err)
4892 trace_seq_printf(s, " %s=INVALID", name);
4893 return -1;
4894 }
4895
4896 return 0;
4897}
4898
4899/**
4900 * pevent_get_field_raw - return the raw pointer into the data field
4901 * @s: The seq to print to on error
4902 * @event: the event that the field is for
4903 * @name: The name of the field
4904 * @record: The record with the field name.
4905 * @len: place to store the field length.
4906 * @err: print default error if failed.
4907 *
4908 * Returns a pointer into record->data of the field and places
4909 * the length of the field in @len.
4910 *
4911 * On failure, it returns NULL.
4912 */
4913void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004914 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004915 int *len, int err)
4916{
4917 struct format_field *field;
4918 void *data = record->data;
4919 unsigned offset;
4920 int dummy;
4921
4922 if (!event)
4923 return NULL;
4924
4925 field = pevent_find_field(event, name);
4926
4927 if (!field) {
4928 if (err)
4929 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4930 return NULL;
4931 }
4932
4933 /* Allow @len to be NULL */
4934 if (!len)
4935 len = &dummy;
4936
4937 offset = field->offset;
4938 if (field->flags & FIELD_IS_DYNAMIC) {
4939 offset = pevent_read_number(event->pevent,
4940 data + offset, field->size);
4941 *len = offset >> 16;
4942 offset &= 0xffff;
4943 } else
4944 *len = field->size;
4945
4946 return data + offset;
4947}
4948
4949/**
4950 * pevent_get_field_val - find a field and return its value
4951 * @s: The seq to print to on error
4952 * @event: the event that the field is for
4953 * @name: The name of the field
4954 * @record: The record with the field name.
4955 * @val: place to store the value of the field.
4956 * @err: print default error if failed.
4957 *
4958 * Returns 0 on success -1 on field not found.
4959 */
4960int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004961 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004962 unsigned long long *val, int err)
4963{
4964 struct format_field *field;
4965
4966 if (!event)
4967 return -1;
4968
4969 field = pevent_find_field(event, name);
4970
4971 return get_field_val(s, field, name, record, val, err);
4972}
4973
4974/**
4975 * pevent_get_common_field_val - find a common field and return its value
4976 * @s: The seq to print to on error
4977 * @event: the event that the field is for
4978 * @name: The name of the field
4979 * @record: The record with the field name.
4980 * @val: place to store the value of the field.
4981 * @err: print default error if failed.
4982 *
4983 * Returns 0 on success -1 on field not found.
4984 */
4985int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004986 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004987 unsigned long long *val, int err)
4988{
4989 struct format_field *field;
4990
4991 if (!event)
4992 return -1;
4993
4994 field = pevent_find_common_field(event, name);
4995
4996 return get_field_val(s, field, name, record, val, err);
4997}
4998
4999/**
5000 * pevent_get_any_field_val - find a any field and return its value
5001 * @s: The seq to print to on error
5002 * @event: the event that the field is for
5003 * @name: The name of the field
5004 * @record: The record with the field name.
5005 * @val: place to store the value of the field.
5006 * @err: print default error if failed.
5007 *
5008 * Returns 0 on success -1 on field not found.
5009 */
5010int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005011 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005012 unsigned long long *val, int err)
5013{
5014 struct format_field *field;
5015
5016 if (!event)
5017 return -1;
5018
5019 field = pevent_find_any_field(event, name);
5020
5021 return get_field_val(s, field, name, record, val, err);
5022}
5023
5024/**
5025 * pevent_print_num_field - print a field and a format
5026 * @s: The seq to print to
5027 * @fmt: The printf format to print the field with.
5028 * @event: the event that the field is for
5029 * @name: The name of the field
5030 * @record: The record with the field name.
5031 * @err: print default error if failed.
5032 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005033 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005034 */
5035int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5036 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005037 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005038{
5039 struct format_field *field = pevent_find_field(event, name);
5040 unsigned long long val;
5041
5042 if (!field)
5043 goto failed;
5044
5045 if (pevent_read_number_field(field, record->data, &val))
5046 goto failed;
5047
5048 return trace_seq_printf(s, fmt, val);
5049
5050 failed:
5051 if (err)
5052 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5053 return -1;
5054}
5055
5056static void free_func_handle(struct pevent_function_handler *func)
5057{
5058 struct pevent_func_params *params;
5059
5060 free(func->name);
5061
5062 while (func->params) {
5063 params = func->params;
5064 func->params = params->next;
5065 free(params);
5066 }
5067
5068 free(func);
5069}
5070
5071/**
5072 * pevent_register_print_function - register a helper function
5073 * @pevent: the handle to the pevent
5074 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005075 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005076 * @name: the name of the helper function
5077 * @parameters: A list of enum pevent_func_arg_type
5078 *
5079 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005080 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005081 * of these functions.
5082 *
5083 * The @parameters is a variable list of pevent_func_arg_type enums that
5084 * must end with PEVENT_FUNC_ARG_VOID.
5085 */
5086int pevent_register_print_function(struct pevent *pevent,
5087 pevent_func_handler func,
5088 enum pevent_func_arg_type ret_type,
5089 char *name, ...)
5090{
5091 struct pevent_function_handler *func_handle;
5092 struct pevent_func_params **next_param;
5093 struct pevent_func_params *param;
5094 enum pevent_func_arg_type type;
5095 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005096 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005097
5098 func_handle = find_func_handler(pevent, name);
5099 if (func_handle) {
5100 /*
5101 * This is most like caused by the users own
5102 * plugins updating the function. This overrides the
5103 * system defaults.
5104 */
5105 pr_stat("override of function helper '%s'", name);
5106 remove_func_handler(pevent, name);
5107 }
5108
Namhyung Kim67ed9392012-09-07 11:49:47 +09005109 func_handle = malloc(sizeof(*func_handle));
5110 if (!func_handle) {
5111 do_warning("Failed to allocate function handler");
5112 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5113 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005114 memset(func_handle, 0, sizeof(*func_handle));
5115
5116 func_handle->ret_type = ret_type;
5117 func_handle->name = strdup(name);
5118 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005119 if (!func_handle->name) {
5120 do_warning("Failed to allocate function name");
5121 free(func_handle);
5122 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5123 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005124
5125 next_param = &(func_handle->params);
5126 va_start(ap, name);
5127 for (;;) {
5128 type = va_arg(ap, enum pevent_func_arg_type);
5129 if (type == PEVENT_FUNC_ARG_VOID)
5130 break;
5131
5132 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005133 do_warning("Invalid argument type %d", type);
5134 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005135 goto out_free;
5136 }
5137
Namhyung Kim67ed9392012-09-07 11:49:47 +09005138 param = malloc(sizeof(*param));
5139 if (!param) {
5140 do_warning("Failed to allocate function param");
5141 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5142 goto out_free;
5143 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005144 param->type = type;
5145 param->next = NULL;
5146
5147 *next_param = param;
5148 next_param = &(param->next);
5149
5150 func_handle->nr_args++;
5151 }
5152 va_end(ap);
5153
5154 func_handle->next = pevent->func_handlers;
5155 pevent->func_handlers = func_handle;
5156
5157 return 0;
5158 out_free:
5159 va_end(ap);
5160 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005161 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005162}
5163
5164/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005165 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005166 * @pevent: the handle to the pevent
5167 * @id: the id of the event to register
5168 * @sys_name: the system name the event belongs to
5169 * @event_name: the name of the event
5170 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005171 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005172 *
5173 * This function allows a developer to override the parsing of
5174 * a given event. If for some reason the default print format
5175 * is not sufficient, this function will register a function
5176 * for an event to be used to parse the data instead.
5177 *
5178 * If @id is >= 0, then it is used to find the event.
5179 * else @sys_name and @event_name are used.
5180 */
5181int pevent_register_event_handler(struct pevent *pevent,
5182 int id, char *sys_name, char *event_name,
5183 pevent_event_handler_func func,
5184 void *context)
5185{
5186 struct event_format *event;
5187 struct event_handler *handle;
5188
5189 if (id >= 0) {
5190 /* search by id */
5191 event = pevent_find_event(pevent, id);
5192 if (!event)
5193 goto not_found;
5194 if (event_name && (strcmp(event_name, event->name) != 0))
5195 goto not_found;
5196 if (sys_name && (strcmp(sys_name, event->system) != 0))
5197 goto not_found;
5198 } else {
5199 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5200 if (!event)
5201 goto not_found;
5202 }
5203
5204 pr_stat("overriding event (%d) %s:%s with new print handler",
5205 event->id, event->system, event->name);
5206
5207 event->handler = func;
5208 event->context = context;
5209 return 0;
5210
5211 not_found:
5212 /* Save for later use. */
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005213 handle = malloc(sizeof(*handle));
5214 if (!handle) {
5215 do_warning("Failed to allocate event handler");
5216 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5217 }
5218
Steven Rostedt42c80132012-04-06 00:48:05 +02005219 memset(handle, 0, sizeof(*handle));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005220 handle->id = id;
5221 if (event_name)
5222 handle->event_name = strdup(event_name);
5223 if (sys_name)
5224 handle->sys_name = strdup(sys_name);
5225
Namhyung Kimca638582012-04-09 11:54:31 +09005226 if ((event_name && !handle->event_name) ||
5227 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005228 do_warning("Failed to allocate event/sys name");
5229 free((void *)handle->event_name);
5230 free((void *)handle->sys_name);
5231 free(handle);
5232 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005233 }
5234
Steven Rostedtf7d82352012-04-06 00:47:53 +02005235 handle->func = func;
5236 handle->next = pevent->handlers;
5237 pevent->handlers = handle;
5238 handle->context = context;
5239
5240 return -1;
5241}
5242
5243/**
5244 * pevent_alloc - create a pevent handle
5245 */
5246struct pevent *pevent_alloc(void)
5247{
5248 struct pevent *pevent;
5249
5250 pevent = malloc(sizeof(*pevent));
5251 if (!pevent)
5252 return NULL;
5253 memset(pevent, 0, sizeof(*pevent));
5254 pevent->ref_count = 1;
5255
5256 return pevent;
5257}
5258
5259void pevent_ref(struct pevent *pevent)
5260{
5261 pevent->ref_count++;
5262}
5263
5264static void free_format_fields(struct format_field *field)
5265{
5266 struct format_field *next;
5267
5268 while (field) {
5269 next = field->next;
5270 free(field->type);
5271 free(field->name);
5272 free(field);
5273 field = next;
5274 }
5275}
5276
5277static void free_formats(struct format *format)
5278{
5279 free_format_fields(format->common_fields);
5280 free_format_fields(format->fields);
5281}
5282
5283static void free_event(struct event_format *event)
5284{
5285 free(event->name);
5286 free(event->system);
5287
5288 free_formats(&event->format);
5289
5290 free(event->print_fmt.format);
5291 free_args(event->print_fmt.args);
5292
5293 free(event);
5294}
5295
5296/**
5297 * pevent_free - free a pevent handle
5298 * @pevent: the pevent handle to free
5299 */
5300void pevent_free(struct pevent *pevent)
5301{
Steven Rostedta2525a02012-04-06 00:48:02 +02005302 struct cmdline_list *cmdlist, *cmdnext;
5303 struct func_list *funclist, *funcnext;
5304 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005305 struct pevent_function_handler *func_handler;
5306 struct event_handler *handle;
5307 int i;
5308
Steven Rostedta2525a02012-04-06 00:48:02 +02005309 if (!pevent)
5310 return;
5311
5312 cmdlist = pevent->cmdlist;
5313 funclist = pevent->funclist;
5314 printklist = pevent->printklist;
5315
Steven Rostedtf7d82352012-04-06 00:47:53 +02005316 pevent->ref_count--;
5317 if (pevent->ref_count)
5318 return;
5319
5320 if (pevent->cmdlines) {
5321 for (i = 0; i < pevent->cmdline_count; i++)
5322 free(pevent->cmdlines[i].comm);
5323 free(pevent->cmdlines);
5324 }
5325
5326 while (cmdlist) {
5327 cmdnext = cmdlist->next;
5328 free(cmdlist->comm);
5329 free(cmdlist);
5330 cmdlist = cmdnext;
5331 }
5332
5333 if (pevent->func_map) {
5334 for (i = 0; i < pevent->func_count; i++) {
5335 free(pevent->func_map[i].func);
5336 free(pevent->func_map[i].mod);
5337 }
5338 free(pevent->func_map);
5339 }
5340
5341 while (funclist) {
5342 funcnext = funclist->next;
5343 free(funclist->func);
5344 free(funclist->mod);
5345 free(funclist);
5346 funclist = funcnext;
5347 }
5348
5349 while (pevent->func_handlers) {
5350 func_handler = pevent->func_handlers;
5351 pevent->func_handlers = func_handler->next;
5352 free_func_handle(func_handler);
5353 }
5354
5355 if (pevent->printk_map) {
5356 for (i = 0; i < pevent->printk_count; i++)
5357 free(pevent->printk_map[i].printk);
5358 free(pevent->printk_map);
5359 }
5360
5361 while (printklist) {
5362 printknext = printklist->next;
5363 free(printklist->printk);
5364 free(printklist);
5365 printklist = printknext;
5366 }
5367
5368 for (i = 0; i < pevent->nr_events; i++)
5369 free_event(pevent->events[i]);
5370
5371 while (pevent->handlers) {
5372 handle = pevent->handlers;
5373 pevent->handlers = handle->next;
5374 free_handler(handle);
5375 }
5376
5377 free(pevent->events);
5378 free(pevent->sort_events);
5379
5380 free(pevent);
5381}
5382
5383void pevent_unref(struct pevent *pevent)
5384{
5385 pevent_free(pevent);
5386}