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