tools lib traceevent: Add support for __print_array()
[deliverable/linux.git] / tools / lib / traceevent / event-parse.c
1 /*
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, see <http://www.gnu.org/licenses>
17 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 *
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
25 */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <limits.h>
34
35 #include <netinet/ip6.h>
36 #include "event-parse.h"
37 #include "event-utils.h"
38
39 static const char *input_buf;
40 static unsigned long long input_buf_ptr;
41 static unsigned long long input_buf_siz;
42
43 static int is_flag_field;
44 static int is_symbolic_field;
45
46 static int show_warning = 1;
47
48 #define do_warning(fmt, ...) \
49 do { \
50 if (show_warning) \
51 warning(fmt, ##__VA_ARGS__); \
52 } while (0)
53
54 #define do_warning_event(event, fmt, ...) \
55 do { \
56 if (!show_warning) \
57 continue; \
58 \
59 if (event) \
60 warning("[%s:%s] " fmt, event->system, \
61 event->name, ##__VA_ARGS__); \
62 else \
63 warning(fmt, ##__VA_ARGS__); \
64 } while (0)
65
66 static void init_input_buf(const char *buf, unsigned long long size)
67 {
68 input_buf = buf;
69 input_buf_siz = size;
70 input_buf_ptr = 0;
71 }
72
73 const char *pevent_get_input_buf(void)
74 {
75 return input_buf;
76 }
77
78 unsigned long long pevent_get_input_buf_ptr(void)
79 {
80 return input_buf_ptr;
81 }
82
83 struct event_handler {
84 struct event_handler *next;
85 int id;
86 const char *sys_name;
87 const char *event_name;
88 pevent_event_handler_func func;
89 void *context;
90 };
91
92 struct pevent_func_params {
93 struct pevent_func_params *next;
94 enum pevent_func_arg_type type;
95 };
96
97 struct pevent_function_handler {
98 struct pevent_function_handler *next;
99 enum pevent_func_arg_type ret_type;
100 char *name;
101 pevent_func_handler func;
102 struct pevent_func_params *params;
103 int nr_args;
104 };
105
106 static unsigned long long
107 process_defined_func(struct trace_seq *s, void *data, int size,
108 struct event_format *event, struct print_arg *arg);
109
110 static void free_func_handle(struct pevent_function_handler *func);
111
112 /**
113 * pevent_buffer_init - init buffer for parsing
114 * @buf: buffer to parse
115 * @size: the size of the buffer
116 *
117 * For use with pevent_read_token(), this initializes the internal
118 * buffer that pevent_read_token() will parse.
119 */
120 void pevent_buffer_init(const char *buf, unsigned long long size)
121 {
122 init_input_buf(buf, size);
123 }
124
125 void breakpoint(void)
126 {
127 static int x;
128 x++;
129 }
130
131 struct print_arg *alloc_arg(void)
132 {
133 return calloc(1, sizeof(struct print_arg));
134 }
135
136 struct cmdline {
137 char *comm;
138 int pid;
139 };
140
141 static int cmdline_cmp(const void *a, const void *b)
142 {
143 const struct cmdline *ca = a;
144 const struct cmdline *cb = b;
145
146 if (ca->pid < cb->pid)
147 return -1;
148 if (ca->pid > cb->pid)
149 return 1;
150
151 return 0;
152 }
153
154 struct cmdline_list {
155 struct cmdline_list *next;
156 char *comm;
157 int pid;
158 };
159
160 static int cmdline_init(struct pevent *pevent)
161 {
162 struct cmdline_list *cmdlist = pevent->cmdlist;
163 struct cmdline_list *item;
164 struct cmdline *cmdlines;
165 int i;
166
167 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
168 if (!cmdlines)
169 return -1;
170
171 i = 0;
172 while (cmdlist) {
173 cmdlines[i].pid = cmdlist->pid;
174 cmdlines[i].comm = cmdlist->comm;
175 i++;
176 item = cmdlist;
177 cmdlist = cmdlist->next;
178 free(item);
179 }
180
181 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
182
183 pevent->cmdlines = cmdlines;
184 pevent->cmdlist = NULL;
185
186 return 0;
187 }
188
189 static const char *find_cmdline(struct pevent *pevent, int pid)
190 {
191 const struct cmdline *comm;
192 struct cmdline key;
193
194 if (!pid)
195 return "<idle>";
196
197 if (!pevent->cmdlines && cmdline_init(pevent))
198 return "<not enough memory for cmdlines!>";
199
200 key.pid = pid;
201
202 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
203 sizeof(*pevent->cmdlines), cmdline_cmp);
204
205 if (comm)
206 return comm->comm;
207 return "<...>";
208 }
209
210 /**
211 * pevent_pid_is_registered - return if a pid has a cmdline registered
212 * @pevent: handle for the pevent
213 * @pid: The pid to check if it has a cmdline registered with.
214 *
215 * Returns 1 if the pid has a cmdline mapped to it
216 * 0 otherwise.
217 */
218 int pevent_pid_is_registered(struct pevent *pevent, int pid)
219 {
220 const struct cmdline *comm;
221 struct cmdline key;
222
223 if (!pid)
224 return 1;
225
226 if (!pevent->cmdlines && cmdline_init(pevent))
227 return 0;
228
229 key.pid = pid;
230
231 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
232 sizeof(*pevent->cmdlines), cmdline_cmp);
233
234 if (comm)
235 return 1;
236 return 0;
237 }
238
239 /*
240 * If the command lines have been converted to an array, then
241 * we must add this pid. This is much slower than when cmdlines
242 * are added before the array is initialized.
243 */
244 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
245 {
246 struct cmdline *cmdlines = pevent->cmdlines;
247 const struct cmdline *cmdline;
248 struct cmdline key;
249
250 if (!pid)
251 return 0;
252
253 /* avoid duplicates */
254 key.pid = pid;
255
256 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
257 sizeof(*pevent->cmdlines), cmdline_cmp);
258 if (cmdline) {
259 errno = EEXIST;
260 return -1;
261 }
262
263 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
264 if (!cmdlines) {
265 errno = ENOMEM;
266 return -1;
267 }
268
269 cmdlines[pevent->cmdline_count].comm = strdup(comm);
270 if (!cmdlines[pevent->cmdline_count].comm) {
271 free(cmdlines);
272 errno = ENOMEM;
273 return -1;
274 }
275
276 cmdlines[pevent->cmdline_count].pid = pid;
277
278 if (cmdlines[pevent->cmdline_count].comm)
279 pevent->cmdline_count++;
280
281 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
282 pevent->cmdlines = cmdlines;
283
284 return 0;
285 }
286
287 /**
288 * pevent_register_comm - register a pid / comm mapping
289 * @pevent: handle for the pevent
290 * @comm: the command line to register
291 * @pid: the pid to map the command line to
292 *
293 * This adds a mapping to search for command line names with
294 * a given pid. The comm is duplicated.
295 */
296 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
297 {
298 struct cmdline_list *item;
299
300 if (pevent->cmdlines)
301 return add_new_comm(pevent, comm, pid);
302
303 item = malloc(sizeof(*item));
304 if (!item)
305 return -1;
306
307 if (comm)
308 item->comm = strdup(comm);
309 else
310 item->comm = strdup("<...>");
311 if (!item->comm) {
312 free(item);
313 return -1;
314 }
315 item->pid = pid;
316 item->next = pevent->cmdlist;
317
318 pevent->cmdlist = item;
319 pevent->cmdline_count++;
320
321 return 0;
322 }
323
324 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
325 {
326 pevent->trace_clock = strdup(trace_clock);
327 if (!pevent->trace_clock) {
328 errno = ENOMEM;
329 return -1;
330 }
331 return 0;
332 }
333
334 struct func_map {
335 unsigned long long addr;
336 char *func;
337 char *mod;
338 };
339
340 struct func_list {
341 struct func_list *next;
342 unsigned long long addr;
343 char *func;
344 char *mod;
345 };
346
347 static int func_cmp(const void *a, const void *b)
348 {
349 const struct func_map *fa = a;
350 const struct func_map *fb = b;
351
352 if (fa->addr < fb->addr)
353 return -1;
354 if (fa->addr > fb->addr)
355 return 1;
356
357 return 0;
358 }
359
360 /*
361 * We are searching for a record in between, not an exact
362 * match.
363 */
364 static int func_bcmp(const void *a, const void *b)
365 {
366 const struct func_map *fa = a;
367 const struct func_map *fb = b;
368
369 if ((fa->addr == fb->addr) ||
370
371 (fa->addr > fb->addr &&
372 fa->addr < (fb+1)->addr))
373 return 0;
374
375 if (fa->addr < fb->addr)
376 return -1;
377
378 return 1;
379 }
380
381 static int func_map_init(struct pevent *pevent)
382 {
383 struct func_list *funclist;
384 struct func_list *item;
385 struct func_map *func_map;
386 int i;
387
388 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
389 if (!func_map)
390 return -1;
391
392 funclist = pevent->funclist;
393
394 i = 0;
395 while (funclist) {
396 func_map[i].func = funclist->func;
397 func_map[i].addr = funclist->addr;
398 func_map[i].mod = funclist->mod;
399 i++;
400 item = funclist;
401 funclist = funclist->next;
402 free(item);
403 }
404
405 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
406
407 /*
408 * Add a special record at the end.
409 */
410 func_map[pevent->func_count].func = NULL;
411 func_map[pevent->func_count].addr = 0;
412 func_map[pevent->func_count].mod = NULL;
413
414 pevent->func_map = func_map;
415 pevent->funclist = NULL;
416
417 return 0;
418 }
419
420 static struct func_map *
421 find_func(struct pevent *pevent, unsigned long long addr)
422 {
423 struct func_map *func;
424 struct func_map key;
425
426 if (!pevent->func_map)
427 func_map_init(pevent);
428
429 key.addr = addr;
430
431 func = bsearch(&key, pevent->func_map, pevent->func_count,
432 sizeof(*pevent->func_map), func_bcmp);
433
434 return func;
435 }
436
437 /**
438 * pevent_find_function - find a function by a given address
439 * @pevent: handle for the pevent
440 * @addr: the address to find the function with
441 *
442 * Returns a pointer to the function stored that has the given
443 * address. Note, the address does not have to be exact, it
444 * will select the function that would contain the address.
445 */
446 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
447 {
448 struct func_map *map;
449
450 map = find_func(pevent, addr);
451 if (!map)
452 return NULL;
453
454 return map->func;
455 }
456
457 /**
458 * pevent_find_function_address - find a function address by a given address
459 * @pevent: handle for the pevent
460 * @addr: the address to find the function with
461 *
462 * Returns the address the function starts at. This can be used in
463 * conjunction with pevent_find_function to print both the function
464 * name and the function offset.
465 */
466 unsigned long long
467 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
468 {
469 struct func_map *map;
470
471 map = find_func(pevent, addr);
472 if (!map)
473 return 0;
474
475 return map->addr;
476 }
477
478 /**
479 * pevent_register_function - register a function with a given address
480 * @pevent: handle for the pevent
481 * @function: the function name to register
482 * @addr: the address the function starts at
483 * @mod: the kernel module the function may be in (NULL for none)
484 *
485 * This registers a function name with an address and module.
486 * The @func passed in is duplicated.
487 */
488 int pevent_register_function(struct pevent *pevent, char *func,
489 unsigned long long addr, char *mod)
490 {
491 struct func_list *item = malloc(sizeof(*item));
492
493 if (!item)
494 return -1;
495
496 item->next = pevent->funclist;
497 item->func = strdup(func);
498 if (!item->func)
499 goto out_free;
500
501 if (mod) {
502 item->mod = strdup(mod);
503 if (!item->mod)
504 goto out_free_func;
505 } else
506 item->mod = NULL;
507 item->addr = addr;
508
509 pevent->funclist = item;
510 pevent->func_count++;
511
512 return 0;
513
514 out_free_func:
515 free(item->func);
516 item->func = NULL;
517 out_free:
518 free(item);
519 errno = ENOMEM;
520 return -1;
521 }
522
523 /**
524 * pevent_print_funcs - print out the stored functions
525 * @pevent: handle for the pevent
526 *
527 * This prints out the stored functions.
528 */
529 void pevent_print_funcs(struct pevent *pevent)
530 {
531 int i;
532
533 if (!pevent->func_map)
534 func_map_init(pevent);
535
536 for (i = 0; i < (int)pevent->func_count; i++) {
537 printf("%016llx %s",
538 pevent->func_map[i].addr,
539 pevent->func_map[i].func);
540 if (pevent->func_map[i].mod)
541 printf(" [%s]\n", pevent->func_map[i].mod);
542 else
543 printf("\n");
544 }
545 }
546
547 struct printk_map {
548 unsigned long long addr;
549 char *printk;
550 };
551
552 struct printk_list {
553 struct printk_list *next;
554 unsigned long long addr;
555 char *printk;
556 };
557
558 static int printk_cmp(const void *a, const void *b)
559 {
560 const struct printk_map *pa = a;
561 const struct printk_map *pb = b;
562
563 if (pa->addr < pb->addr)
564 return -1;
565 if (pa->addr > pb->addr)
566 return 1;
567
568 return 0;
569 }
570
571 static int printk_map_init(struct pevent *pevent)
572 {
573 struct printk_list *printklist;
574 struct printk_list *item;
575 struct printk_map *printk_map;
576 int i;
577
578 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
579 if (!printk_map)
580 return -1;
581
582 printklist = pevent->printklist;
583
584 i = 0;
585 while (printklist) {
586 printk_map[i].printk = printklist->printk;
587 printk_map[i].addr = printklist->addr;
588 i++;
589 item = printklist;
590 printklist = printklist->next;
591 free(item);
592 }
593
594 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
595
596 pevent->printk_map = printk_map;
597 pevent->printklist = NULL;
598
599 return 0;
600 }
601
602 static struct printk_map *
603 find_printk(struct pevent *pevent, unsigned long long addr)
604 {
605 struct printk_map *printk;
606 struct printk_map key;
607
608 if (!pevent->printk_map && printk_map_init(pevent))
609 return NULL;
610
611 key.addr = addr;
612
613 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
614 sizeof(*pevent->printk_map), printk_cmp);
615
616 return printk;
617 }
618
619 /**
620 * pevent_register_print_string - register a string by its address
621 * @pevent: handle for the pevent
622 * @fmt: the string format to register
623 * @addr: the address the string was located at
624 *
625 * This registers a string by the address it was stored in the kernel.
626 * The @fmt passed in is duplicated.
627 */
628 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
629 unsigned long long addr)
630 {
631 struct printk_list *item = malloc(sizeof(*item));
632 char *p;
633
634 if (!item)
635 return -1;
636
637 item->next = pevent->printklist;
638 item->addr = addr;
639
640 /* Strip off quotes and '\n' from the end */
641 if (fmt[0] == '"')
642 fmt++;
643 item->printk = strdup(fmt);
644 if (!item->printk)
645 goto out_free;
646
647 p = item->printk + strlen(item->printk) - 1;
648 if (*p == '"')
649 *p = 0;
650
651 p -= 2;
652 if (strcmp(p, "\\n") == 0)
653 *p = 0;
654
655 pevent->printklist = item;
656 pevent->printk_count++;
657
658 return 0;
659
660 out_free:
661 free(item);
662 errno = ENOMEM;
663 return -1;
664 }
665
666 /**
667 * pevent_print_printk - print out the stored strings
668 * @pevent: handle for the pevent
669 *
670 * This prints the string formats that were stored.
671 */
672 void pevent_print_printk(struct pevent *pevent)
673 {
674 int i;
675
676 if (!pevent->printk_map)
677 printk_map_init(pevent);
678
679 for (i = 0; i < (int)pevent->printk_count; i++) {
680 printf("%016llx %s\n",
681 pevent->printk_map[i].addr,
682 pevent->printk_map[i].printk);
683 }
684 }
685
686 static struct event_format *alloc_event(void)
687 {
688 return calloc(1, sizeof(struct event_format));
689 }
690
691 static int add_event(struct pevent *pevent, struct event_format *event)
692 {
693 int i;
694 struct event_format **events = realloc(pevent->events, sizeof(event) *
695 (pevent->nr_events + 1));
696 if (!events)
697 return -1;
698
699 pevent->events = events;
700
701 for (i = 0; i < pevent->nr_events; i++) {
702 if (pevent->events[i]->id > event->id)
703 break;
704 }
705 if (i < pevent->nr_events)
706 memmove(&pevent->events[i + 1],
707 &pevent->events[i],
708 sizeof(event) * (pevent->nr_events - i));
709
710 pevent->events[i] = event;
711 pevent->nr_events++;
712
713 event->pevent = pevent;
714
715 return 0;
716 }
717
718 static int event_item_type(enum event_type type)
719 {
720 switch (type) {
721 case EVENT_ITEM ... EVENT_SQUOTE:
722 return 1;
723 case EVENT_ERROR ... EVENT_DELIM:
724 default:
725 return 0;
726 }
727 }
728
729 static void free_flag_sym(struct print_flag_sym *fsym)
730 {
731 struct print_flag_sym *next;
732
733 while (fsym) {
734 next = fsym->next;
735 free(fsym->value);
736 free(fsym->str);
737 free(fsym);
738 fsym = next;
739 }
740 }
741
742 static void free_arg(struct print_arg *arg)
743 {
744 struct print_arg *farg;
745
746 if (!arg)
747 return;
748
749 switch (arg->type) {
750 case PRINT_ATOM:
751 free(arg->atom.atom);
752 break;
753 case PRINT_FIELD:
754 free(arg->field.name);
755 break;
756 case PRINT_FLAGS:
757 free_arg(arg->flags.field);
758 free(arg->flags.delim);
759 free_flag_sym(arg->flags.flags);
760 break;
761 case PRINT_SYMBOL:
762 free_arg(arg->symbol.field);
763 free_flag_sym(arg->symbol.symbols);
764 break;
765 case PRINT_HEX:
766 free_arg(arg->hex.field);
767 free_arg(arg->hex.size);
768 break;
769 case PRINT_INT_ARRAY:
770 free_arg(arg->int_array.field);
771 free_arg(arg->int_array.count);
772 free_arg(arg->int_array.el_size);
773 break;
774 case PRINT_TYPE:
775 free(arg->typecast.type);
776 free_arg(arg->typecast.item);
777 break;
778 case PRINT_STRING:
779 case PRINT_BSTRING:
780 free(arg->string.string);
781 break;
782 case PRINT_BITMASK:
783 free(arg->bitmask.bitmask);
784 break;
785 case PRINT_DYNAMIC_ARRAY:
786 free(arg->dynarray.index);
787 break;
788 case PRINT_OP:
789 free(arg->op.op);
790 free_arg(arg->op.left);
791 free_arg(arg->op.right);
792 break;
793 case PRINT_FUNC:
794 while (arg->func.args) {
795 farg = arg->func.args;
796 arg->func.args = farg->next;
797 free_arg(farg);
798 }
799 break;
800
801 case PRINT_NULL:
802 default:
803 break;
804 }
805
806 free(arg);
807 }
808
809 static enum event_type get_type(int ch)
810 {
811 if (ch == '\n')
812 return EVENT_NEWLINE;
813 if (isspace(ch))
814 return EVENT_SPACE;
815 if (isalnum(ch) || ch == '_')
816 return EVENT_ITEM;
817 if (ch == '\'')
818 return EVENT_SQUOTE;
819 if (ch == '"')
820 return EVENT_DQUOTE;
821 if (!isprint(ch))
822 return EVENT_NONE;
823 if (ch == '(' || ch == ')' || ch == ',')
824 return EVENT_DELIM;
825
826 return EVENT_OP;
827 }
828
829 static int __read_char(void)
830 {
831 if (input_buf_ptr >= input_buf_siz)
832 return -1;
833
834 return input_buf[input_buf_ptr++];
835 }
836
837 static int __peek_char(void)
838 {
839 if (input_buf_ptr >= input_buf_siz)
840 return -1;
841
842 return input_buf[input_buf_ptr];
843 }
844
845 /**
846 * pevent_peek_char - peek at the next character that will be read
847 *
848 * Returns the next character read, or -1 if end of buffer.
849 */
850 int pevent_peek_char(void)
851 {
852 return __peek_char();
853 }
854
855 static int extend_token(char **tok, char *buf, int size)
856 {
857 char *newtok = realloc(*tok, size);
858
859 if (!newtok) {
860 free(*tok);
861 *tok = NULL;
862 return -1;
863 }
864
865 if (!*tok)
866 strcpy(newtok, buf);
867 else
868 strcat(newtok, buf);
869 *tok = newtok;
870
871 return 0;
872 }
873
874 static enum event_type force_token(const char *str, char **tok);
875
876 static enum event_type __read_token(char **tok)
877 {
878 char buf[BUFSIZ];
879 int ch, last_ch, quote_ch, next_ch;
880 int i = 0;
881 int tok_size = 0;
882 enum event_type type;
883
884 *tok = NULL;
885
886
887 ch = __read_char();
888 if (ch < 0)
889 return EVENT_NONE;
890
891 type = get_type(ch);
892 if (type == EVENT_NONE)
893 return type;
894
895 buf[i++] = ch;
896
897 switch (type) {
898 case EVENT_NEWLINE:
899 case EVENT_DELIM:
900 if (asprintf(tok, "%c", ch) < 0)
901 return EVENT_ERROR;
902
903 return type;
904
905 case EVENT_OP:
906 switch (ch) {
907 case '-':
908 next_ch = __peek_char();
909 if (next_ch == '>') {
910 buf[i++] = __read_char();
911 break;
912 }
913 /* fall through */
914 case '+':
915 case '|':
916 case '&':
917 case '>':
918 case '<':
919 last_ch = ch;
920 ch = __peek_char();
921 if (ch != last_ch)
922 goto test_equal;
923 buf[i++] = __read_char();
924 switch (last_ch) {
925 case '>':
926 case '<':
927 goto test_equal;
928 default:
929 break;
930 }
931 break;
932 case '!':
933 case '=':
934 goto test_equal;
935 default: /* what should we do instead? */
936 break;
937 }
938 buf[i] = 0;
939 *tok = strdup(buf);
940 return type;
941
942 test_equal:
943 ch = __peek_char();
944 if (ch == '=')
945 buf[i++] = __read_char();
946 goto out;
947
948 case EVENT_DQUOTE:
949 case EVENT_SQUOTE:
950 /* don't keep quotes */
951 i--;
952 quote_ch = ch;
953 last_ch = 0;
954 concat:
955 do {
956 if (i == (BUFSIZ - 1)) {
957 buf[i] = 0;
958 tok_size += BUFSIZ;
959
960 if (extend_token(tok, buf, tok_size) < 0)
961 return EVENT_NONE;
962 i = 0;
963 }
964 last_ch = ch;
965 ch = __read_char();
966 buf[i++] = ch;
967 /* the '\' '\' will cancel itself */
968 if (ch == '\\' && last_ch == '\\')
969 last_ch = 0;
970 } while (ch != quote_ch || last_ch == '\\');
971 /* remove the last quote */
972 i--;
973
974 /*
975 * For strings (double quotes) check the next token.
976 * If it is another string, concatinate the two.
977 */
978 if (type == EVENT_DQUOTE) {
979 unsigned long long save_input_buf_ptr = input_buf_ptr;
980
981 do {
982 ch = __read_char();
983 } while (isspace(ch));
984 if (ch == '"')
985 goto concat;
986 input_buf_ptr = save_input_buf_ptr;
987 }
988
989 goto out;
990
991 case EVENT_ERROR ... EVENT_SPACE:
992 case EVENT_ITEM:
993 default:
994 break;
995 }
996
997 while (get_type(__peek_char()) == type) {
998 if (i == (BUFSIZ - 1)) {
999 buf[i] = 0;
1000 tok_size += BUFSIZ;
1001
1002 if (extend_token(tok, buf, tok_size) < 0)
1003 return EVENT_NONE;
1004 i = 0;
1005 }
1006 ch = __read_char();
1007 buf[i++] = ch;
1008 }
1009
1010 out:
1011 buf[i] = 0;
1012 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1013 return EVENT_NONE;
1014
1015 if (type == EVENT_ITEM) {
1016 /*
1017 * Older versions of the kernel has a bug that
1018 * creates invalid symbols and will break the mac80211
1019 * parsing. This is a work around to that bug.
1020 *
1021 * See Linux kernel commit:
1022 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1023 */
1024 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1025 free(*tok);
1026 *tok = NULL;
1027 return force_token("\"\%s\" ", tok);
1028 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1029 free(*tok);
1030 *tok = NULL;
1031 return force_token("\" sta:%pM\" ", tok);
1032 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1033 free(*tok);
1034 *tok = NULL;
1035 return force_token("\" vif:%p(%d)\" ", tok);
1036 }
1037 }
1038
1039 return type;
1040 }
1041
1042 static enum event_type force_token(const char *str, char **tok)
1043 {
1044 const char *save_input_buf;
1045 unsigned long long save_input_buf_ptr;
1046 unsigned long long save_input_buf_siz;
1047 enum event_type type;
1048
1049 /* save off the current input pointers */
1050 save_input_buf = input_buf;
1051 save_input_buf_ptr = input_buf_ptr;
1052 save_input_buf_siz = input_buf_siz;
1053
1054 init_input_buf(str, strlen(str));
1055
1056 type = __read_token(tok);
1057
1058 /* reset back to original token */
1059 input_buf = save_input_buf;
1060 input_buf_ptr = save_input_buf_ptr;
1061 input_buf_siz = save_input_buf_siz;
1062
1063 return type;
1064 }
1065
1066 static void free_token(char *tok)
1067 {
1068 if (tok)
1069 free(tok);
1070 }
1071
1072 static enum event_type read_token(char **tok)
1073 {
1074 enum event_type type;
1075
1076 for (;;) {
1077 type = __read_token(tok);
1078 if (type != EVENT_SPACE)
1079 return type;
1080
1081 free_token(*tok);
1082 }
1083
1084 /* not reached */
1085 *tok = NULL;
1086 return EVENT_NONE;
1087 }
1088
1089 /**
1090 * pevent_read_token - access to utilites to use the pevent parser
1091 * @tok: The token to return
1092 *
1093 * This will parse tokens from the string given by
1094 * pevent_init_data().
1095 *
1096 * Returns the token type.
1097 */
1098 enum event_type pevent_read_token(char **tok)
1099 {
1100 return read_token(tok);
1101 }
1102
1103 /**
1104 * pevent_free_token - free a token returned by pevent_read_token
1105 * @token: the token to free
1106 */
1107 void pevent_free_token(char *token)
1108 {
1109 free_token(token);
1110 }
1111
1112 /* no newline */
1113 static enum event_type read_token_item(char **tok)
1114 {
1115 enum event_type type;
1116
1117 for (;;) {
1118 type = __read_token(tok);
1119 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1120 return type;
1121 free_token(*tok);
1122 *tok = NULL;
1123 }
1124
1125 /* not reached */
1126 *tok = NULL;
1127 return EVENT_NONE;
1128 }
1129
1130 static int test_type(enum event_type type, enum event_type expect)
1131 {
1132 if (type != expect) {
1133 do_warning("Error: expected type %d but read %d",
1134 expect, type);
1135 return -1;
1136 }
1137 return 0;
1138 }
1139
1140 static int test_type_token(enum event_type type, const char *token,
1141 enum event_type expect, const char *expect_tok)
1142 {
1143 if (type != expect) {
1144 do_warning("Error: expected type %d but read %d",
1145 expect, type);
1146 return -1;
1147 }
1148
1149 if (strcmp(token, expect_tok) != 0) {
1150 do_warning("Error: expected '%s' but read '%s'",
1151 expect_tok, token);
1152 return -1;
1153 }
1154 return 0;
1155 }
1156
1157 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1158 {
1159 enum event_type type;
1160
1161 if (newline_ok)
1162 type = read_token(tok);
1163 else
1164 type = read_token_item(tok);
1165 return test_type(type, expect);
1166 }
1167
1168 static int read_expect_type(enum event_type expect, char **tok)
1169 {
1170 return __read_expect_type(expect, tok, 1);
1171 }
1172
1173 static int __read_expected(enum event_type expect, const char *str,
1174 int newline_ok)
1175 {
1176 enum event_type type;
1177 char *token;
1178 int ret;
1179
1180 if (newline_ok)
1181 type = read_token(&token);
1182 else
1183 type = read_token_item(&token);
1184
1185 ret = test_type_token(type, token, expect, str);
1186
1187 free_token(token);
1188
1189 return ret;
1190 }
1191
1192 static int read_expected(enum event_type expect, const char *str)
1193 {
1194 return __read_expected(expect, str, 1);
1195 }
1196
1197 static int read_expected_item(enum event_type expect, const char *str)
1198 {
1199 return __read_expected(expect, str, 0);
1200 }
1201
1202 static char *event_read_name(void)
1203 {
1204 char *token;
1205
1206 if (read_expected(EVENT_ITEM, "name") < 0)
1207 return NULL;
1208
1209 if (read_expected(EVENT_OP, ":") < 0)
1210 return NULL;
1211
1212 if (read_expect_type(EVENT_ITEM, &token) < 0)
1213 goto fail;
1214
1215 return token;
1216
1217 fail:
1218 free_token(token);
1219 return NULL;
1220 }
1221
1222 static int event_read_id(void)
1223 {
1224 char *token;
1225 int id;
1226
1227 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1228 return -1;
1229
1230 if (read_expected(EVENT_OP, ":") < 0)
1231 return -1;
1232
1233 if (read_expect_type(EVENT_ITEM, &token) < 0)
1234 goto fail;
1235
1236 id = strtoul(token, NULL, 0);
1237 free_token(token);
1238 return id;
1239
1240 fail:
1241 free_token(token);
1242 return -1;
1243 }
1244
1245 static int field_is_string(struct format_field *field)
1246 {
1247 if ((field->flags & FIELD_IS_ARRAY) &&
1248 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1249 strstr(field->type, "s8")))
1250 return 1;
1251
1252 return 0;
1253 }
1254
1255 static int field_is_dynamic(struct format_field *field)
1256 {
1257 if (strncmp(field->type, "__data_loc", 10) == 0)
1258 return 1;
1259
1260 return 0;
1261 }
1262
1263 static int field_is_long(struct format_field *field)
1264 {
1265 /* includes long long */
1266 if (strstr(field->type, "long"))
1267 return 1;
1268
1269 return 0;
1270 }
1271
1272 static unsigned int type_size(const char *name)
1273 {
1274 /* This covers all FIELD_IS_STRING types. */
1275 static struct {
1276 const char *type;
1277 unsigned int size;
1278 } table[] = {
1279 { "u8", 1 },
1280 { "u16", 2 },
1281 { "u32", 4 },
1282 { "u64", 8 },
1283 { "s8", 1 },
1284 { "s16", 2 },
1285 { "s32", 4 },
1286 { "s64", 8 },
1287 { "char", 1 },
1288 { },
1289 };
1290 int i;
1291
1292 for (i = 0; table[i].type; i++) {
1293 if (!strcmp(table[i].type, name))
1294 return table[i].size;
1295 }
1296
1297 return 0;
1298 }
1299
1300 static int event_read_fields(struct event_format *event, struct format_field **fields)
1301 {
1302 struct format_field *field = NULL;
1303 enum event_type type;
1304 char *token;
1305 char *last_token;
1306 int count = 0;
1307
1308 do {
1309 unsigned int size_dynamic = 0;
1310
1311 type = read_token(&token);
1312 if (type == EVENT_NEWLINE) {
1313 free_token(token);
1314 return count;
1315 }
1316
1317 count++;
1318
1319 if (test_type_token(type, token, EVENT_ITEM, "field"))
1320 goto fail;
1321 free_token(token);
1322
1323 type = read_token(&token);
1324 /*
1325 * The ftrace fields may still use the "special" name.
1326 * Just ignore it.
1327 */
1328 if (event->flags & EVENT_FL_ISFTRACE &&
1329 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1330 free_token(token);
1331 type = read_token(&token);
1332 }
1333
1334 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1335 goto fail;
1336
1337 free_token(token);
1338 if (read_expect_type(EVENT_ITEM, &token) < 0)
1339 goto fail;
1340
1341 last_token = token;
1342
1343 field = calloc(1, sizeof(*field));
1344 if (!field)
1345 goto fail;
1346
1347 field->event = event;
1348
1349 /* read the rest of the type */
1350 for (;;) {
1351 type = read_token(&token);
1352 if (type == EVENT_ITEM ||
1353 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1354 /*
1355 * Some of the ftrace fields are broken and have
1356 * an illegal "." in them.
1357 */
1358 (event->flags & EVENT_FL_ISFTRACE &&
1359 type == EVENT_OP && strcmp(token, ".") == 0)) {
1360
1361 if (strcmp(token, "*") == 0)
1362 field->flags |= FIELD_IS_POINTER;
1363
1364 if (field->type) {
1365 char *new_type;
1366 new_type = realloc(field->type,
1367 strlen(field->type) +
1368 strlen(last_token) + 2);
1369 if (!new_type) {
1370 free(last_token);
1371 goto fail;
1372 }
1373 field->type = new_type;
1374 strcat(field->type, " ");
1375 strcat(field->type, last_token);
1376 free(last_token);
1377 } else
1378 field->type = last_token;
1379 last_token = token;
1380 continue;
1381 }
1382
1383 break;
1384 }
1385
1386 if (!field->type) {
1387 do_warning_event(event, "%s: no type found", __func__);
1388 goto fail;
1389 }
1390 field->name = last_token;
1391
1392 if (test_type(type, EVENT_OP))
1393 goto fail;
1394
1395 if (strcmp(token, "[") == 0) {
1396 enum event_type last_type = type;
1397 char *brackets = token;
1398 char *new_brackets;
1399 int len;
1400
1401 field->flags |= FIELD_IS_ARRAY;
1402
1403 type = read_token(&token);
1404
1405 if (type == EVENT_ITEM)
1406 field->arraylen = strtoul(token, NULL, 0);
1407 else
1408 field->arraylen = 0;
1409
1410 while (strcmp(token, "]") != 0) {
1411 if (last_type == EVENT_ITEM &&
1412 type == EVENT_ITEM)
1413 len = 2;
1414 else
1415 len = 1;
1416 last_type = type;
1417
1418 new_brackets = realloc(brackets,
1419 strlen(brackets) +
1420 strlen(token) + len);
1421 if (!new_brackets) {
1422 free(brackets);
1423 goto fail;
1424 }
1425 brackets = new_brackets;
1426 if (len == 2)
1427 strcat(brackets, " ");
1428 strcat(brackets, token);
1429 /* We only care about the last token */
1430 field->arraylen = strtoul(token, NULL, 0);
1431 free_token(token);
1432 type = read_token(&token);
1433 if (type == EVENT_NONE) {
1434 do_warning_event(event, "failed to find token");
1435 goto fail;
1436 }
1437 }
1438
1439 free_token(token);
1440
1441 new_brackets = realloc(brackets, strlen(brackets) + 2);
1442 if (!new_brackets) {
1443 free(brackets);
1444 goto fail;
1445 }
1446 brackets = new_brackets;
1447 strcat(brackets, "]");
1448
1449 /* add brackets to type */
1450
1451 type = read_token(&token);
1452 /*
1453 * If the next token is not an OP, then it is of
1454 * the format: type [] item;
1455 */
1456 if (type == EVENT_ITEM) {
1457 char *new_type;
1458 new_type = realloc(field->type,
1459 strlen(field->type) +
1460 strlen(field->name) +
1461 strlen(brackets) + 2);
1462 if (!new_type) {
1463 free(brackets);
1464 goto fail;
1465 }
1466 field->type = new_type;
1467 strcat(field->type, " ");
1468 strcat(field->type, field->name);
1469 size_dynamic = type_size(field->name);
1470 free_token(field->name);
1471 strcat(field->type, brackets);
1472 field->name = token;
1473 type = read_token(&token);
1474 } else {
1475 char *new_type;
1476 new_type = realloc(field->type,
1477 strlen(field->type) +
1478 strlen(brackets) + 1);
1479 if (!new_type) {
1480 free(brackets);
1481 goto fail;
1482 }
1483 field->type = new_type;
1484 strcat(field->type, brackets);
1485 }
1486 free(brackets);
1487 }
1488
1489 if (field_is_string(field))
1490 field->flags |= FIELD_IS_STRING;
1491 if (field_is_dynamic(field))
1492 field->flags |= FIELD_IS_DYNAMIC;
1493 if (field_is_long(field))
1494 field->flags |= FIELD_IS_LONG;
1495
1496 if (test_type_token(type, token, EVENT_OP, ";"))
1497 goto fail;
1498 free_token(token);
1499
1500 if (read_expected(EVENT_ITEM, "offset") < 0)
1501 goto fail_expect;
1502
1503 if (read_expected(EVENT_OP, ":") < 0)
1504 goto fail_expect;
1505
1506 if (read_expect_type(EVENT_ITEM, &token))
1507 goto fail;
1508 field->offset = strtoul(token, NULL, 0);
1509 free_token(token);
1510
1511 if (read_expected(EVENT_OP, ";") < 0)
1512 goto fail_expect;
1513
1514 if (read_expected(EVENT_ITEM, "size") < 0)
1515 goto fail_expect;
1516
1517 if (read_expected(EVENT_OP, ":") < 0)
1518 goto fail_expect;
1519
1520 if (read_expect_type(EVENT_ITEM, &token))
1521 goto fail;
1522 field->size = strtoul(token, NULL, 0);
1523 free_token(token);
1524
1525 if (read_expected(EVENT_OP, ";") < 0)
1526 goto fail_expect;
1527
1528 type = read_token(&token);
1529 if (type != EVENT_NEWLINE) {
1530 /* newer versions of the kernel have a "signed" type */
1531 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1532 goto fail;
1533
1534 free_token(token);
1535
1536 if (read_expected(EVENT_OP, ":") < 0)
1537 goto fail_expect;
1538
1539 if (read_expect_type(EVENT_ITEM, &token))
1540 goto fail;
1541
1542 if (strtoul(token, NULL, 0))
1543 field->flags |= FIELD_IS_SIGNED;
1544
1545 free_token(token);
1546 if (read_expected(EVENT_OP, ";") < 0)
1547 goto fail_expect;
1548
1549 if (read_expect_type(EVENT_NEWLINE, &token))
1550 goto fail;
1551 }
1552
1553 free_token(token);
1554
1555 if (field->flags & FIELD_IS_ARRAY) {
1556 if (field->arraylen)
1557 field->elementsize = field->size / field->arraylen;
1558 else if (field->flags & FIELD_IS_DYNAMIC)
1559 field->elementsize = size_dynamic;
1560 else if (field->flags & FIELD_IS_STRING)
1561 field->elementsize = 1;
1562 else if (field->flags & FIELD_IS_LONG)
1563 field->elementsize = event->pevent ?
1564 event->pevent->long_size :
1565 sizeof(long);
1566 } else
1567 field->elementsize = field->size;
1568
1569 *fields = field;
1570 fields = &field->next;
1571
1572 } while (1);
1573
1574 return 0;
1575
1576 fail:
1577 free_token(token);
1578 fail_expect:
1579 if (field) {
1580 free(field->type);
1581 free(field->name);
1582 free(field);
1583 }
1584 return -1;
1585 }
1586
1587 static int event_read_format(struct event_format *event)
1588 {
1589 char *token;
1590 int ret;
1591
1592 if (read_expected_item(EVENT_ITEM, "format") < 0)
1593 return -1;
1594
1595 if (read_expected(EVENT_OP, ":") < 0)
1596 return -1;
1597
1598 if (read_expect_type(EVENT_NEWLINE, &token))
1599 goto fail;
1600 free_token(token);
1601
1602 ret = event_read_fields(event, &event->format.common_fields);
1603 if (ret < 0)
1604 return ret;
1605 event->format.nr_common = ret;
1606
1607 ret = event_read_fields(event, &event->format.fields);
1608 if (ret < 0)
1609 return ret;
1610 event->format.nr_fields = ret;
1611
1612 return 0;
1613
1614 fail:
1615 free_token(token);
1616 return -1;
1617 }
1618
1619 static enum event_type
1620 process_arg_token(struct event_format *event, struct print_arg *arg,
1621 char **tok, enum event_type type);
1622
1623 static enum event_type
1624 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1625 {
1626 enum event_type type;
1627 char *token;
1628
1629 type = read_token(&token);
1630 *tok = token;
1631
1632 return process_arg_token(event, arg, tok, type);
1633 }
1634
1635 static enum event_type
1636 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1637
1638 /*
1639 * For __print_symbolic() and __print_flags, we need to completely
1640 * evaluate the first argument, which defines what to print next.
1641 */
1642 static enum event_type
1643 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1644 {
1645 enum event_type type;
1646
1647 type = process_arg(event, arg, tok);
1648
1649 while (type == EVENT_OP) {
1650 type = process_op(event, arg, tok);
1651 }
1652
1653 return type;
1654 }
1655
1656 static enum event_type
1657 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1658 {
1659 struct print_arg *arg, *left, *right;
1660 enum event_type type;
1661 char *token = NULL;
1662
1663 arg = alloc_arg();
1664 left = alloc_arg();
1665 right = alloc_arg();
1666
1667 if (!arg || !left || !right) {
1668 do_warning_event(event, "%s: not enough memory!", __func__);
1669 /* arg will be freed at out_free */
1670 free_arg(left);
1671 free_arg(right);
1672 goto out_free;
1673 }
1674
1675 arg->type = PRINT_OP;
1676 arg->op.left = left;
1677 arg->op.right = right;
1678
1679 *tok = NULL;
1680 type = process_arg(event, left, &token);
1681
1682 again:
1683 /* Handle other operations in the arguments */
1684 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1685 type = process_op(event, left, &token);
1686 goto again;
1687 }
1688
1689 if (test_type_token(type, token, EVENT_OP, ":"))
1690 goto out_free;
1691
1692 arg->op.op = token;
1693
1694 type = process_arg(event, right, &token);
1695
1696 top->op.right = arg;
1697
1698 *tok = token;
1699 return type;
1700
1701 out_free:
1702 /* Top may point to itself */
1703 top->op.right = NULL;
1704 free_token(token);
1705 free_arg(arg);
1706 return EVENT_ERROR;
1707 }
1708
1709 static enum event_type
1710 process_array(struct event_format *event, struct print_arg *top, char **tok)
1711 {
1712 struct print_arg *arg;
1713 enum event_type type;
1714 char *token = NULL;
1715
1716 arg = alloc_arg();
1717 if (!arg) {
1718 do_warning_event(event, "%s: not enough memory!", __func__);
1719 /* '*tok' is set to top->op.op. No need to free. */
1720 *tok = NULL;
1721 return EVENT_ERROR;
1722 }
1723
1724 *tok = NULL;
1725 type = process_arg(event, arg, &token);
1726 if (test_type_token(type, token, EVENT_OP, "]"))
1727 goto out_free;
1728
1729 top->op.right = arg;
1730
1731 free_token(token);
1732 type = read_token_item(&token);
1733 *tok = token;
1734
1735 return type;
1736
1737 out_free:
1738 free_token(token);
1739 free_arg(arg);
1740 return EVENT_ERROR;
1741 }
1742
1743 static int get_op_prio(char *op)
1744 {
1745 if (!op[1]) {
1746 switch (op[0]) {
1747 case '~':
1748 case '!':
1749 return 4;
1750 case '*':
1751 case '/':
1752 case '%':
1753 return 6;
1754 case '+':
1755 case '-':
1756 return 7;
1757 /* '>>' and '<<' are 8 */
1758 case '<':
1759 case '>':
1760 return 9;
1761 /* '==' and '!=' are 10 */
1762 case '&':
1763 return 11;
1764 case '^':
1765 return 12;
1766 case '|':
1767 return 13;
1768 case '?':
1769 return 16;
1770 default:
1771 do_warning("unknown op '%c'", op[0]);
1772 return -1;
1773 }
1774 } else {
1775 if (strcmp(op, "++") == 0 ||
1776 strcmp(op, "--") == 0) {
1777 return 3;
1778 } else if (strcmp(op, ">>") == 0 ||
1779 strcmp(op, "<<") == 0) {
1780 return 8;
1781 } else if (strcmp(op, ">=") == 0 ||
1782 strcmp(op, "<=") == 0) {
1783 return 9;
1784 } else if (strcmp(op, "==") == 0 ||
1785 strcmp(op, "!=") == 0) {
1786 return 10;
1787 } else if (strcmp(op, "&&") == 0) {
1788 return 14;
1789 } else if (strcmp(op, "||") == 0) {
1790 return 15;
1791 } else {
1792 do_warning("unknown op '%s'", op);
1793 return -1;
1794 }
1795 }
1796 }
1797
1798 static int set_op_prio(struct print_arg *arg)
1799 {
1800
1801 /* single ops are the greatest */
1802 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1803 arg->op.prio = 0;
1804 else
1805 arg->op.prio = get_op_prio(arg->op.op);
1806
1807 return arg->op.prio;
1808 }
1809
1810 /* Note, *tok does not get freed, but will most likely be saved */
1811 static enum event_type
1812 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1813 {
1814 struct print_arg *left, *right = NULL;
1815 enum event_type type;
1816 char *token;
1817
1818 /* the op is passed in via tok */
1819 token = *tok;
1820
1821 if (arg->type == PRINT_OP && !arg->op.left) {
1822 /* handle single op */
1823 if (token[1]) {
1824 do_warning_event(event, "bad op token %s", token);
1825 goto out_free;
1826 }
1827 switch (token[0]) {
1828 case '~':
1829 case '!':
1830 case '+':
1831 case '-':
1832 break;
1833 default:
1834 do_warning_event(event, "bad op token %s", token);
1835 goto out_free;
1836
1837 }
1838
1839 /* make an empty left */
1840 left = alloc_arg();
1841 if (!left)
1842 goto out_warn_free;
1843
1844 left->type = PRINT_NULL;
1845 arg->op.left = left;
1846
1847 right = alloc_arg();
1848 if (!right)
1849 goto out_warn_free;
1850
1851 arg->op.right = right;
1852
1853 /* do not free the token, it belongs to an op */
1854 *tok = NULL;
1855 type = process_arg(event, right, tok);
1856
1857 } else if (strcmp(token, "?") == 0) {
1858
1859 left = alloc_arg();
1860 if (!left)
1861 goto out_warn_free;
1862
1863 /* copy the top arg to the left */
1864 *left = *arg;
1865
1866 arg->type = PRINT_OP;
1867 arg->op.op = token;
1868 arg->op.left = left;
1869 arg->op.prio = 0;
1870
1871 /* it will set arg->op.right */
1872 type = process_cond(event, arg, tok);
1873
1874 } else if (strcmp(token, ">>") == 0 ||
1875 strcmp(token, "<<") == 0 ||
1876 strcmp(token, "&") == 0 ||
1877 strcmp(token, "|") == 0 ||
1878 strcmp(token, "&&") == 0 ||
1879 strcmp(token, "||") == 0 ||
1880 strcmp(token, "-") == 0 ||
1881 strcmp(token, "+") == 0 ||
1882 strcmp(token, "*") == 0 ||
1883 strcmp(token, "^") == 0 ||
1884 strcmp(token, "/") == 0 ||
1885 strcmp(token, "<") == 0 ||
1886 strcmp(token, ">") == 0 ||
1887 strcmp(token, "<=") == 0 ||
1888 strcmp(token, ">=") == 0 ||
1889 strcmp(token, "==") == 0 ||
1890 strcmp(token, "!=") == 0) {
1891
1892 left = alloc_arg();
1893 if (!left)
1894 goto out_warn_free;
1895
1896 /* copy the top arg to the left */
1897 *left = *arg;
1898
1899 arg->type = PRINT_OP;
1900 arg->op.op = token;
1901 arg->op.left = left;
1902 arg->op.right = NULL;
1903
1904 if (set_op_prio(arg) == -1) {
1905 event->flags |= EVENT_FL_FAILED;
1906 /* arg->op.op (= token) will be freed at out_free */
1907 arg->op.op = NULL;
1908 goto out_free;
1909 }
1910
1911 type = read_token_item(&token);
1912 *tok = token;
1913
1914 /* could just be a type pointer */
1915 if ((strcmp(arg->op.op, "*") == 0) &&
1916 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1917 char *new_atom;
1918
1919 if (left->type != PRINT_ATOM) {
1920 do_warning_event(event, "bad pointer type");
1921 goto out_free;
1922 }
1923 new_atom = realloc(left->atom.atom,
1924 strlen(left->atom.atom) + 3);
1925 if (!new_atom)
1926 goto out_warn_free;
1927
1928 left->atom.atom = new_atom;
1929 strcat(left->atom.atom, " *");
1930 free(arg->op.op);
1931 *arg = *left;
1932 free(left);
1933
1934 return type;
1935 }
1936
1937 right = alloc_arg();
1938 if (!right)
1939 goto out_warn_free;
1940
1941 type = process_arg_token(event, right, tok, type);
1942 arg->op.right = right;
1943
1944 } else if (strcmp(token, "[") == 0) {
1945
1946 left = alloc_arg();
1947 if (!left)
1948 goto out_warn_free;
1949
1950 *left = *arg;
1951
1952 arg->type = PRINT_OP;
1953 arg->op.op = token;
1954 arg->op.left = left;
1955
1956 arg->op.prio = 0;
1957
1958 /* it will set arg->op.right */
1959 type = process_array(event, arg, tok);
1960
1961 } else {
1962 do_warning_event(event, "unknown op '%s'", token);
1963 event->flags |= EVENT_FL_FAILED;
1964 /* the arg is now the left side */
1965 goto out_free;
1966 }
1967
1968 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1969 int prio;
1970
1971 /* higher prios need to be closer to the root */
1972 prio = get_op_prio(*tok);
1973
1974 if (prio > arg->op.prio)
1975 return process_op(event, arg, tok);
1976
1977 return process_op(event, right, tok);
1978 }
1979
1980 return type;
1981
1982 out_warn_free:
1983 do_warning_event(event, "%s: not enough memory!", __func__);
1984 out_free:
1985 free_token(token);
1986 *tok = NULL;
1987 return EVENT_ERROR;
1988 }
1989
1990 static enum event_type
1991 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1992 char **tok)
1993 {
1994 enum event_type type;
1995 char *field;
1996 char *token;
1997
1998 if (read_expected(EVENT_OP, "->") < 0)
1999 goto out_err;
2000
2001 if (read_expect_type(EVENT_ITEM, &token) < 0)
2002 goto out_free;
2003 field = token;
2004
2005 arg->type = PRINT_FIELD;
2006 arg->field.name = field;
2007
2008 if (is_flag_field) {
2009 arg->field.field = pevent_find_any_field(event, arg->field.name);
2010 arg->field.field->flags |= FIELD_IS_FLAG;
2011 is_flag_field = 0;
2012 } else if (is_symbolic_field) {
2013 arg->field.field = pevent_find_any_field(event, arg->field.name);
2014 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2015 is_symbolic_field = 0;
2016 }
2017
2018 type = read_token(&token);
2019 *tok = token;
2020
2021 return type;
2022
2023 out_free:
2024 free_token(token);
2025 out_err:
2026 *tok = NULL;
2027 return EVENT_ERROR;
2028 }
2029
2030 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2031 struct print_arg **print_arg)
2032 {
2033 struct print_arg *field;
2034 enum event_type type;
2035 char *token;
2036 int ret = 0;
2037
2038 field = alloc_arg();
2039 if (!field) {
2040 do_warning_event(event, "%s: not enough memory!", __func__);
2041 errno = ENOMEM;
2042 return -1;
2043 }
2044
2045 type = process_arg(event, field, &token);
2046
2047 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2048 errno = EINVAL;
2049 ret = -1;
2050 free_arg(field);
2051 goto out_free_token;
2052 }
2053
2054 *print_arg = field;
2055
2056 out_free_token:
2057 free_token(token);
2058
2059 return ret;
2060 }
2061
2062 static char *arg_eval (struct print_arg *arg);
2063
2064 static unsigned long long
2065 eval_type_str(unsigned long long val, const char *type, int pointer)
2066 {
2067 int sign = 0;
2068 char *ref;
2069 int len;
2070
2071 len = strlen(type);
2072
2073 if (pointer) {
2074
2075 if (type[len-1] != '*') {
2076 do_warning("pointer expected with non pointer type");
2077 return val;
2078 }
2079
2080 ref = malloc(len);
2081 if (!ref) {
2082 do_warning("%s: not enough memory!", __func__);
2083 return val;
2084 }
2085 memcpy(ref, type, len);
2086
2087 /* chop off the " *" */
2088 ref[len - 2] = 0;
2089
2090 val = eval_type_str(val, ref, 0);
2091 free(ref);
2092 return val;
2093 }
2094
2095 /* check if this is a pointer */
2096 if (type[len - 1] == '*')
2097 return val;
2098
2099 /* Try to figure out the arg size*/
2100 if (strncmp(type, "struct", 6) == 0)
2101 /* all bets off */
2102 return val;
2103
2104 if (strcmp(type, "u8") == 0)
2105 return val & 0xff;
2106
2107 if (strcmp(type, "u16") == 0)
2108 return val & 0xffff;
2109
2110 if (strcmp(type, "u32") == 0)
2111 return val & 0xffffffff;
2112
2113 if (strcmp(type, "u64") == 0 ||
2114 strcmp(type, "s64"))
2115 return val;
2116
2117 if (strcmp(type, "s8") == 0)
2118 return (unsigned long long)(char)val & 0xff;
2119
2120 if (strcmp(type, "s16") == 0)
2121 return (unsigned long long)(short)val & 0xffff;
2122
2123 if (strcmp(type, "s32") == 0)
2124 return (unsigned long long)(int)val & 0xffffffff;
2125
2126 if (strncmp(type, "unsigned ", 9) == 0) {
2127 sign = 0;
2128 type += 9;
2129 }
2130
2131 if (strcmp(type, "char") == 0) {
2132 if (sign)
2133 return (unsigned long long)(char)val & 0xff;
2134 else
2135 return val & 0xff;
2136 }
2137
2138 if (strcmp(type, "short") == 0) {
2139 if (sign)
2140 return (unsigned long long)(short)val & 0xffff;
2141 else
2142 return val & 0xffff;
2143 }
2144
2145 if (strcmp(type, "int") == 0) {
2146 if (sign)
2147 return (unsigned long long)(int)val & 0xffffffff;
2148 else
2149 return val & 0xffffffff;
2150 }
2151
2152 return val;
2153 }
2154
2155 /*
2156 * Try to figure out the type.
2157 */
2158 static unsigned long long
2159 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2160 {
2161 if (arg->type != PRINT_TYPE) {
2162 do_warning("expected type argument");
2163 return 0;
2164 }
2165
2166 return eval_type_str(val, arg->typecast.type, pointer);
2167 }
2168
2169 static int arg_num_eval(struct print_arg *arg, long long *val)
2170 {
2171 long long left, right;
2172 int ret = 1;
2173
2174 switch (arg->type) {
2175 case PRINT_ATOM:
2176 *val = strtoll(arg->atom.atom, NULL, 0);
2177 break;
2178 case PRINT_TYPE:
2179 ret = arg_num_eval(arg->typecast.item, val);
2180 if (!ret)
2181 break;
2182 *val = eval_type(*val, arg, 0);
2183 break;
2184 case PRINT_OP:
2185 switch (arg->op.op[0]) {
2186 case '|':
2187 ret = arg_num_eval(arg->op.left, &left);
2188 if (!ret)
2189 break;
2190 ret = arg_num_eval(arg->op.right, &right);
2191 if (!ret)
2192 break;
2193 if (arg->op.op[1])
2194 *val = left || right;
2195 else
2196 *val = left | right;
2197 break;
2198 case '&':
2199 ret = arg_num_eval(arg->op.left, &left);
2200 if (!ret)
2201 break;
2202 ret = arg_num_eval(arg->op.right, &right);
2203 if (!ret)
2204 break;
2205 if (arg->op.op[1])
2206 *val = left && right;
2207 else
2208 *val = left & right;
2209 break;
2210 case '<':
2211 ret = arg_num_eval(arg->op.left, &left);
2212 if (!ret)
2213 break;
2214 ret = arg_num_eval(arg->op.right, &right);
2215 if (!ret)
2216 break;
2217 switch (arg->op.op[1]) {
2218 case 0:
2219 *val = left < right;
2220 break;
2221 case '<':
2222 *val = left << right;
2223 break;
2224 case '=':
2225 *val = left <= right;
2226 break;
2227 default:
2228 do_warning("unknown op '%s'", arg->op.op);
2229 ret = 0;
2230 }
2231 break;
2232 case '>':
2233 ret = arg_num_eval(arg->op.left, &left);
2234 if (!ret)
2235 break;
2236 ret = arg_num_eval(arg->op.right, &right);
2237 if (!ret)
2238 break;
2239 switch (arg->op.op[1]) {
2240 case 0:
2241 *val = left > right;
2242 break;
2243 case '>':
2244 *val = left >> right;
2245 break;
2246 case '=':
2247 *val = left >= right;
2248 break;
2249 default:
2250 do_warning("unknown op '%s'", arg->op.op);
2251 ret = 0;
2252 }
2253 break;
2254 case '=':
2255 ret = arg_num_eval(arg->op.left, &left);
2256 if (!ret)
2257 break;
2258 ret = arg_num_eval(arg->op.right, &right);
2259 if (!ret)
2260 break;
2261
2262 if (arg->op.op[1] != '=') {
2263 do_warning("unknown op '%s'", arg->op.op);
2264 ret = 0;
2265 } else
2266 *val = left == right;
2267 break;
2268 case '!':
2269 ret = arg_num_eval(arg->op.left, &left);
2270 if (!ret)
2271 break;
2272 ret = arg_num_eval(arg->op.right, &right);
2273 if (!ret)
2274 break;
2275
2276 switch (arg->op.op[1]) {
2277 case '=':
2278 *val = left != right;
2279 break;
2280 default:
2281 do_warning("unknown op '%s'", arg->op.op);
2282 ret = 0;
2283 }
2284 break;
2285 case '-':
2286 /* check for negative */
2287 if (arg->op.left->type == PRINT_NULL)
2288 left = 0;
2289 else
2290 ret = arg_num_eval(arg->op.left, &left);
2291 if (!ret)
2292 break;
2293 ret = arg_num_eval(arg->op.right, &right);
2294 if (!ret)
2295 break;
2296 *val = left - right;
2297 break;
2298 case '+':
2299 if (arg->op.left->type == PRINT_NULL)
2300 left = 0;
2301 else
2302 ret = arg_num_eval(arg->op.left, &left);
2303 if (!ret)
2304 break;
2305 ret = arg_num_eval(arg->op.right, &right);
2306 if (!ret)
2307 break;
2308 *val = left + right;
2309 break;
2310 default:
2311 do_warning("unknown op '%s'", arg->op.op);
2312 ret = 0;
2313 }
2314 break;
2315
2316 case PRINT_NULL:
2317 case PRINT_FIELD ... PRINT_SYMBOL:
2318 case PRINT_STRING:
2319 case PRINT_BSTRING:
2320 case PRINT_BITMASK:
2321 default:
2322 do_warning("invalid eval type %d", arg->type);
2323 ret = 0;
2324
2325 }
2326 return ret;
2327 }
2328
2329 static char *arg_eval (struct print_arg *arg)
2330 {
2331 long long val;
2332 static char buf[20];
2333
2334 switch (arg->type) {
2335 case PRINT_ATOM:
2336 return arg->atom.atom;
2337 case PRINT_TYPE:
2338 return arg_eval(arg->typecast.item);
2339 case PRINT_OP:
2340 if (!arg_num_eval(arg, &val))
2341 break;
2342 sprintf(buf, "%lld", val);
2343 return buf;
2344
2345 case PRINT_NULL:
2346 case PRINT_FIELD ... PRINT_SYMBOL:
2347 case PRINT_STRING:
2348 case PRINT_BSTRING:
2349 case PRINT_BITMASK:
2350 default:
2351 do_warning("invalid eval type %d", arg->type);
2352 break;
2353 }
2354
2355 return NULL;
2356 }
2357
2358 static enum event_type
2359 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2360 {
2361 enum event_type type;
2362 struct print_arg *arg = NULL;
2363 struct print_flag_sym *field;
2364 char *token = *tok;
2365 char *value;
2366
2367 do {
2368 free_token(token);
2369 type = read_token_item(&token);
2370 if (test_type_token(type, token, EVENT_OP, "{"))
2371 break;
2372
2373 arg = alloc_arg();
2374 if (!arg)
2375 goto out_free;
2376
2377 free_token(token);
2378 type = process_arg(event, arg, &token);
2379
2380 if (type == EVENT_OP)
2381 type = process_op(event, arg, &token);
2382
2383 if (type == EVENT_ERROR)
2384 goto out_free;
2385
2386 if (test_type_token(type, token, EVENT_DELIM, ","))
2387 goto out_free;
2388
2389 field = calloc(1, sizeof(*field));
2390 if (!field)
2391 goto out_free;
2392
2393 value = arg_eval(arg);
2394 if (value == NULL)
2395 goto out_free_field;
2396 field->value = strdup(value);
2397 if (field->value == NULL)
2398 goto out_free_field;
2399
2400 free_arg(arg);
2401 arg = alloc_arg();
2402 if (!arg)
2403 goto out_free;
2404
2405 free_token(token);
2406 type = process_arg(event, arg, &token);
2407 if (test_type_token(type, token, EVENT_OP, "}"))
2408 goto out_free_field;
2409
2410 value = arg_eval(arg);
2411 if (value == NULL)
2412 goto out_free_field;
2413 field->str = strdup(value);
2414 if (field->str == NULL)
2415 goto out_free_field;
2416 free_arg(arg);
2417 arg = NULL;
2418
2419 *list = field;
2420 list = &field->next;
2421
2422 free_token(token);
2423 type = read_token_item(&token);
2424 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2425
2426 *tok = token;
2427 return type;
2428
2429 out_free_field:
2430 free_flag_sym(field);
2431 out_free:
2432 free_arg(arg);
2433 free_token(token);
2434 *tok = NULL;
2435
2436 return EVENT_ERROR;
2437 }
2438
2439 static enum event_type
2440 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2441 {
2442 struct print_arg *field;
2443 enum event_type type;
2444 char *token = NULL;
2445
2446 memset(arg, 0, sizeof(*arg));
2447 arg->type = PRINT_FLAGS;
2448
2449 field = alloc_arg();
2450 if (!field) {
2451 do_warning_event(event, "%s: not enough memory!", __func__);
2452 goto out_free;
2453 }
2454
2455 type = process_field_arg(event, field, &token);
2456
2457 /* Handle operations in the first argument */
2458 while (type == EVENT_OP)
2459 type = process_op(event, field, &token);
2460
2461 if (test_type_token(type, token, EVENT_DELIM, ","))
2462 goto out_free_field;
2463 free_token(token);
2464
2465 arg->flags.field = field;
2466
2467 type = read_token_item(&token);
2468 if (event_item_type(type)) {
2469 arg->flags.delim = token;
2470 type = read_token_item(&token);
2471 }
2472
2473 if (test_type_token(type, token, EVENT_DELIM, ","))
2474 goto out_free;
2475
2476 type = process_fields(event, &arg->flags.flags, &token);
2477 if (test_type_token(type, token, EVENT_DELIM, ")"))
2478 goto out_free;
2479
2480 free_token(token);
2481 type = read_token_item(tok);
2482 return type;
2483
2484 out_free_field:
2485 free_arg(field);
2486 out_free:
2487 free_token(token);
2488 *tok = NULL;
2489 return EVENT_ERROR;
2490 }
2491
2492 static enum event_type
2493 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2494 {
2495 struct print_arg *field;
2496 enum event_type type;
2497 char *token = NULL;
2498
2499 memset(arg, 0, sizeof(*arg));
2500 arg->type = PRINT_SYMBOL;
2501
2502 field = alloc_arg();
2503 if (!field) {
2504 do_warning_event(event, "%s: not enough memory!", __func__);
2505 goto out_free;
2506 }
2507
2508 type = process_field_arg(event, field, &token);
2509
2510 if (test_type_token(type, token, EVENT_DELIM, ","))
2511 goto out_free_field;
2512
2513 arg->symbol.field = field;
2514
2515 type = process_fields(event, &arg->symbol.symbols, &token);
2516 if (test_type_token(type, token, EVENT_DELIM, ")"))
2517 goto out_free;
2518
2519 free_token(token);
2520 type = read_token_item(tok);
2521 return type;
2522
2523 out_free_field:
2524 free_arg(field);
2525 out_free:
2526 free_token(token);
2527 *tok = NULL;
2528 return EVENT_ERROR;
2529 }
2530
2531 static enum event_type
2532 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2533 {
2534 memset(arg, 0, sizeof(*arg));
2535 arg->type = PRINT_HEX;
2536
2537 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2538 goto out;
2539
2540 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2541 goto free_field;
2542
2543 return read_token_item(tok);
2544
2545 free_field:
2546 free_arg(arg->hex.field);
2547 out:
2548 *tok = NULL;
2549 return EVENT_ERROR;
2550 }
2551
2552 static enum event_type
2553 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2554 {
2555 memset(arg, 0, sizeof(*arg));
2556 arg->type = PRINT_INT_ARRAY;
2557
2558 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2559 goto out;
2560
2561 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2562 goto free_field;
2563
2564 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2565 goto free_size;
2566
2567 return read_token_item(tok);
2568
2569 free_size:
2570 free_arg(arg->int_array.count);
2571 free_field:
2572 free_arg(arg->int_array.field);
2573 out:
2574 *tok = NULL;
2575 return EVENT_ERROR;
2576 }
2577
2578 static enum event_type
2579 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2580 {
2581 struct format_field *field;
2582 enum event_type type;
2583 char *token;
2584
2585 memset(arg, 0, sizeof(*arg));
2586 arg->type = PRINT_DYNAMIC_ARRAY;
2587
2588 /*
2589 * The item within the parenthesis is another field that holds
2590 * the index into where the array starts.
2591 */
2592 type = read_token(&token);
2593 *tok = token;
2594 if (type != EVENT_ITEM)
2595 goto out_free;
2596
2597 /* Find the field */
2598
2599 field = pevent_find_field(event, token);
2600 if (!field)
2601 goto out_free;
2602
2603 arg->dynarray.field = field;
2604 arg->dynarray.index = 0;
2605
2606 if (read_expected(EVENT_DELIM, ")") < 0)
2607 goto out_free;
2608
2609 free_token(token);
2610 type = read_token_item(&token);
2611 *tok = token;
2612 if (type != EVENT_OP || strcmp(token, "[") != 0)
2613 return type;
2614
2615 free_token(token);
2616 arg = alloc_arg();
2617 if (!arg) {
2618 do_warning_event(event, "%s: not enough memory!", __func__);
2619 *tok = NULL;
2620 return EVENT_ERROR;
2621 }
2622
2623 type = process_arg(event, arg, &token);
2624 if (type == EVENT_ERROR)
2625 goto out_free_arg;
2626
2627 if (!test_type_token(type, token, EVENT_OP, "]"))
2628 goto out_free_arg;
2629
2630 free_token(token);
2631 type = read_token_item(tok);
2632 return type;
2633
2634 out_free_arg:
2635 free_arg(arg);
2636 out_free:
2637 free_token(token);
2638 *tok = NULL;
2639 return EVENT_ERROR;
2640 }
2641
2642 static enum event_type
2643 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2644 {
2645 struct print_arg *item_arg;
2646 enum event_type type;
2647 char *token;
2648
2649 type = process_arg(event, arg, &token);
2650
2651 if (type == EVENT_ERROR)
2652 goto out_free;
2653
2654 if (type == EVENT_OP)
2655 type = process_op(event, arg, &token);
2656
2657 if (type == EVENT_ERROR)
2658 goto out_free;
2659
2660 if (test_type_token(type, token, EVENT_DELIM, ")"))
2661 goto out_free;
2662
2663 free_token(token);
2664 type = read_token_item(&token);
2665
2666 /*
2667 * If the next token is an item or another open paren, then
2668 * this was a typecast.
2669 */
2670 if (event_item_type(type) ||
2671 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2672
2673 /* make this a typecast and contine */
2674
2675 /* prevous must be an atom */
2676 if (arg->type != PRINT_ATOM) {
2677 do_warning_event(event, "previous needed to be PRINT_ATOM");
2678 goto out_free;
2679 }
2680
2681 item_arg = alloc_arg();
2682 if (!item_arg) {
2683 do_warning_event(event, "%s: not enough memory!",
2684 __func__);
2685 goto out_free;
2686 }
2687
2688 arg->type = PRINT_TYPE;
2689 arg->typecast.type = arg->atom.atom;
2690 arg->typecast.item = item_arg;
2691 type = process_arg_token(event, item_arg, &token, type);
2692
2693 }
2694
2695 *tok = token;
2696 return type;
2697
2698 out_free:
2699 free_token(token);
2700 *tok = NULL;
2701 return EVENT_ERROR;
2702 }
2703
2704
2705 static enum event_type
2706 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2707 char **tok)
2708 {
2709 enum event_type type;
2710 char *token;
2711
2712 if (read_expect_type(EVENT_ITEM, &token) < 0)
2713 goto out_free;
2714
2715 arg->type = PRINT_STRING;
2716 arg->string.string = token;
2717 arg->string.offset = -1;
2718
2719 if (read_expected(EVENT_DELIM, ")") < 0)
2720 goto out_err;
2721
2722 type = read_token(&token);
2723 *tok = token;
2724
2725 return type;
2726
2727 out_free:
2728 free_token(token);
2729 out_err:
2730 *tok = NULL;
2731 return EVENT_ERROR;
2732 }
2733
2734 static enum event_type
2735 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2736 char **tok)
2737 {
2738 enum event_type type;
2739 char *token;
2740
2741 if (read_expect_type(EVENT_ITEM, &token) < 0)
2742 goto out_free;
2743
2744 arg->type = PRINT_BITMASK;
2745 arg->bitmask.bitmask = token;
2746 arg->bitmask.offset = -1;
2747
2748 if (read_expected(EVENT_DELIM, ")") < 0)
2749 goto out_err;
2750
2751 type = read_token(&token);
2752 *tok = token;
2753
2754 return type;
2755
2756 out_free:
2757 free_token(token);
2758 out_err:
2759 *tok = NULL;
2760 return EVENT_ERROR;
2761 }
2762
2763 static struct pevent_function_handler *
2764 find_func_handler(struct pevent *pevent, char *func_name)
2765 {
2766 struct pevent_function_handler *func;
2767
2768 if (!pevent)
2769 return NULL;
2770
2771 for (func = pevent->func_handlers; func; func = func->next) {
2772 if (strcmp(func->name, func_name) == 0)
2773 break;
2774 }
2775
2776 return func;
2777 }
2778
2779 static void remove_func_handler(struct pevent *pevent, char *func_name)
2780 {
2781 struct pevent_function_handler *func;
2782 struct pevent_function_handler **next;
2783
2784 next = &pevent->func_handlers;
2785 while ((func = *next)) {
2786 if (strcmp(func->name, func_name) == 0) {
2787 *next = func->next;
2788 free_func_handle(func);
2789 break;
2790 }
2791 next = &func->next;
2792 }
2793 }
2794
2795 static enum event_type
2796 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2797 struct print_arg *arg, char **tok)
2798 {
2799 struct print_arg **next_arg;
2800 struct print_arg *farg;
2801 enum event_type type;
2802 char *token;
2803 int i;
2804
2805 arg->type = PRINT_FUNC;
2806 arg->func.func = func;
2807
2808 *tok = NULL;
2809
2810 next_arg = &(arg->func.args);
2811 for (i = 0; i < func->nr_args; i++) {
2812 farg = alloc_arg();
2813 if (!farg) {
2814 do_warning_event(event, "%s: not enough memory!",
2815 __func__);
2816 return EVENT_ERROR;
2817 }
2818
2819 type = process_arg(event, farg, &token);
2820 if (i < (func->nr_args - 1)) {
2821 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2822 do_warning_event(event,
2823 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2824 func->name, func->nr_args,
2825 event->name, i + 1);
2826 goto err;
2827 }
2828 } else {
2829 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2830 do_warning_event(event,
2831 "Error: function '%s()' only expects %d arguments but event %s has more",
2832 func->name, func->nr_args, event->name);
2833 goto err;
2834 }
2835 }
2836
2837 *next_arg = farg;
2838 next_arg = &(farg->next);
2839 free_token(token);
2840 }
2841
2842 type = read_token(&token);
2843 *tok = token;
2844
2845 return type;
2846
2847 err:
2848 free_arg(farg);
2849 free_token(token);
2850 return EVENT_ERROR;
2851 }
2852
2853 static enum event_type
2854 process_function(struct event_format *event, struct print_arg *arg,
2855 char *token, char **tok)
2856 {
2857 struct pevent_function_handler *func;
2858
2859 if (strcmp(token, "__print_flags") == 0) {
2860 free_token(token);
2861 is_flag_field = 1;
2862 return process_flags(event, arg, tok);
2863 }
2864 if (strcmp(token, "__print_symbolic") == 0) {
2865 free_token(token);
2866 is_symbolic_field = 1;
2867 return process_symbols(event, arg, tok);
2868 }
2869 if (strcmp(token, "__print_hex") == 0) {
2870 free_token(token);
2871 return process_hex(event, arg, tok);
2872 }
2873 if (strcmp(token, "__print_array") == 0) {
2874 free_token(token);
2875 return process_int_array(event, arg, tok);
2876 }
2877 if (strcmp(token, "__get_str") == 0) {
2878 free_token(token);
2879 return process_str(event, arg, tok);
2880 }
2881 if (strcmp(token, "__get_bitmask") == 0) {
2882 free_token(token);
2883 return process_bitmask(event, arg, tok);
2884 }
2885 if (strcmp(token, "__get_dynamic_array") == 0) {
2886 free_token(token);
2887 return process_dynamic_array(event, arg, tok);
2888 }
2889
2890 func = find_func_handler(event->pevent, token);
2891 if (func) {
2892 free_token(token);
2893 return process_func_handler(event, func, arg, tok);
2894 }
2895
2896 do_warning_event(event, "function %s not defined", token);
2897 free_token(token);
2898 return EVENT_ERROR;
2899 }
2900
2901 static enum event_type
2902 process_arg_token(struct event_format *event, struct print_arg *arg,
2903 char **tok, enum event_type type)
2904 {
2905 char *token;
2906 char *atom;
2907
2908 token = *tok;
2909
2910 switch (type) {
2911 case EVENT_ITEM:
2912 if (strcmp(token, "REC") == 0) {
2913 free_token(token);
2914 type = process_entry(event, arg, &token);
2915 break;
2916 }
2917 atom = token;
2918 /* test the next token */
2919 type = read_token_item(&token);
2920
2921 /*
2922 * If the next token is a parenthesis, then this
2923 * is a function.
2924 */
2925 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2926 free_token(token);
2927 token = NULL;
2928 /* this will free atom. */
2929 type = process_function(event, arg, atom, &token);
2930 break;
2931 }
2932 /* atoms can be more than one token long */
2933 while (type == EVENT_ITEM) {
2934 char *new_atom;
2935 new_atom = realloc(atom,
2936 strlen(atom) + strlen(token) + 2);
2937 if (!new_atom) {
2938 free(atom);
2939 *tok = NULL;
2940 free_token(token);
2941 return EVENT_ERROR;
2942 }
2943 atom = new_atom;
2944 strcat(atom, " ");
2945 strcat(atom, token);
2946 free_token(token);
2947 type = read_token_item(&token);
2948 }
2949
2950 arg->type = PRINT_ATOM;
2951 arg->atom.atom = atom;
2952 break;
2953
2954 case EVENT_DQUOTE:
2955 case EVENT_SQUOTE:
2956 arg->type = PRINT_ATOM;
2957 arg->atom.atom = token;
2958 type = read_token_item(&token);
2959 break;
2960 case EVENT_DELIM:
2961 if (strcmp(token, "(") == 0) {
2962 free_token(token);
2963 type = process_paren(event, arg, &token);
2964 break;
2965 }
2966 case EVENT_OP:
2967 /* handle single ops */
2968 arg->type = PRINT_OP;
2969 arg->op.op = token;
2970 arg->op.left = NULL;
2971 type = process_op(event, arg, &token);
2972
2973 /* On error, the op is freed */
2974 if (type == EVENT_ERROR)
2975 arg->op.op = NULL;
2976
2977 /* return error type if errored */
2978 break;
2979
2980 case EVENT_ERROR ... EVENT_NEWLINE:
2981 default:
2982 do_warning_event(event, "unexpected type %d", type);
2983 return EVENT_ERROR;
2984 }
2985 *tok = token;
2986
2987 return type;
2988 }
2989
2990 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2991 {
2992 enum event_type type = EVENT_ERROR;
2993 struct print_arg *arg;
2994 char *token;
2995 int args = 0;
2996
2997 do {
2998 if (type == EVENT_NEWLINE) {
2999 type = read_token_item(&token);
3000 continue;
3001 }
3002
3003 arg = alloc_arg();
3004 if (!arg) {
3005 do_warning_event(event, "%s: not enough memory!",
3006 __func__);
3007 return -1;
3008 }
3009
3010 type = process_arg(event, arg, &token);
3011
3012 if (type == EVENT_ERROR) {
3013 free_token(token);
3014 free_arg(arg);
3015 return -1;
3016 }
3017
3018 *list = arg;
3019 args++;
3020
3021 if (type == EVENT_OP) {
3022 type = process_op(event, arg, &token);
3023 free_token(token);
3024 if (type == EVENT_ERROR) {
3025 *list = NULL;
3026 free_arg(arg);
3027 return -1;
3028 }
3029 list = &arg->next;
3030 continue;
3031 }
3032
3033 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3034 free_token(token);
3035 *list = arg;
3036 list = &arg->next;
3037 continue;
3038 }
3039 break;
3040 } while (type != EVENT_NONE);
3041
3042 if (type != EVENT_NONE && type != EVENT_ERROR)
3043 free_token(token);
3044
3045 return args;
3046 }
3047
3048 static int event_read_print(struct event_format *event)
3049 {
3050 enum event_type type;
3051 char *token;
3052 int ret;
3053
3054 if (read_expected_item(EVENT_ITEM, "print") < 0)
3055 return -1;
3056
3057 if (read_expected(EVENT_ITEM, "fmt") < 0)
3058 return -1;
3059
3060 if (read_expected(EVENT_OP, ":") < 0)
3061 return -1;
3062
3063 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3064 goto fail;
3065
3066 concat:
3067 event->print_fmt.format = token;
3068 event->print_fmt.args = NULL;
3069
3070 /* ok to have no arg */
3071 type = read_token_item(&token);
3072
3073 if (type == EVENT_NONE)
3074 return 0;
3075
3076 /* Handle concatenation of print lines */
3077 if (type == EVENT_DQUOTE) {
3078 char *cat;
3079
3080 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3081 goto fail;
3082 free_token(token);
3083 free_token(event->print_fmt.format);
3084 event->print_fmt.format = NULL;
3085 token = cat;
3086 goto concat;
3087 }
3088
3089 if (test_type_token(type, token, EVENT_DELIM, ","))
3090 goto fail;
3091
3092 free_token(token);
3093
3094 ret = event_read_print_args(event, &event->print_fmt.args);
3095 if (ret < 0)
3096 return -1;
3097
3098 return ret;
3099
3100 fail:
3101 free_token(token);
3102 return -1;
3103 }
3104
3105 /**
3106 * pevent_find_common_field - return a common field by event
3107 * @event: handle for the event
3108 * @name: the name of the common field to return
3109 *
3110 * Returns a common field from the event by the given @name.
3111 * This only searchs the common fields and not all field.
3112 */
3113 struct format_field *
3114 pevent_find_common_field(struct event_format *event, const char *name)
3115 {
3116 struct format_field *format;
3117
3118 for (format = event->format.common_fields;
3119 format; format = format->next) {
3120 if (strcmp(format->name, name) == 0)
3121 break;
3122 }
3123
3124 return format;
3125 }
3126
3127 /**
3128 * pevent_find_field - find a non-common field
3129 * @event: handle for the event
3130 * @name: the name of the non-common field
3131 *
3132 * Returns a non-common field by the given @name.
3133 * This does not search common fields.
3134 */
3135 struct format_field *
3136 pevent_find_field(struct event_format *event, const char *name)
3137 {
3138 struct format_field *format;
3139
3140 for (format = event->format.fields;
3141 format; format = format->next) {
3142 if (strcmp(format->name, name) == 0)
3143 break;
3144 }
3145
3146 return format;
3147 }
3148
3149 /**
3150 * pevent_find_any_field - find any field by name
3151 * @event: handle for the event
3152 * @name: the name of the field
3153 *
3154 * Returns a field by the given @name.
3155 * This searchs the common field names first, then
3156 * the non-common ones if a common one was not found.
3157 */
3158 struct format_field *
3159 pevent_find_any_field(struct event_format *event, const char *name)
3160 {
3161 struct format_field *format;
3162
3163 format = pevent_find_common_field(event, name);
3164 if (format)
3165 return format;
3166 return pevent_find_field(event, name);
3167 }
3168
3169 /**
3170 * pevent_read_number - read a number from data
3171 * @pevent: handle for the pevent
3172 * @ptr: the raw data
3173 * @size: the size of the data that holds the number
3174 *
3175 * Returns the number (converted to host) from the
3176 * raw data.
3177 */
3178 unsigned long long pevent_read_number(struct pevent *pevent,
3179 const void *ptr, int size)
3180 {
3181 switch (size) {
3182 case 1:
3183 return *(unsigned char *)ptr;
3184 case 2:
3185 return data2host2(pevent, ptr);
3186 case 4:
3187 return data2host4(pevent, ptr);
3188 case 8:
3189 return data2host8(pevent, ptr);
3190 default:
3191 /* BUG! */
3192 return 0;
3193 }
3194 }
3195
3196 /**
3197 * pevent_read_number_field - read a number from data
3198 * @field: a handle to the field
3199 * @data: the raw data to read
3200 * @value: the value to place the number in
3201 *
3202 * Reads raw data according to a field offset and size,
3203 * and translates it into @value.
3204 *
3205 * Returns 0 on success, -1 otherwise.
3206 */
3207 int pevent_read_number_field(struct format_field *field, const void *data,
3208 unsigned long long *value)
3209 {
3210 if (!field)
3211 return -1;
3212 switch (field->size) {
3213 case 1:
3214 case 2:
3215 case 4:
3216 case 8:
3217 *value = pevent_read_number(field->event->pevent,
3218 data + field->offset, field->size);
3219 return 0;
3220 default:
3221 return -1;
3222 }
3223 }
3224
3225 static int get_common_info(struct pevent *pevent,
3226 const char *type, int *offset, int *size)
3227 {
3228 struct event_format *event;
3229 struct format_field *field;
3230
3231 /*
3232 * All events should have the same common elements.
3233 * Pick any event to find where the type is;
3234 */
3235 if (!pevent->events) {
3236 do_warning("no event_list!");
3237 return -1;
3238 }
3239
3240 event = pevent->events[0];
3241 field = pevent_find_common_field(event, type);
3242 if (!field)
3243 return -1;
3244
3245 *offset = field->offset;
3246 *size = field->size;
3247
3248 return 0;
3249 }
3250
3251 static int __parse_common(struct pevent *pevent, void *data,
3252 int *size, int *offset, const char *name)
3253 {
3254 int ret;
3255
3256 if (!*size) {
3257 ret = get_common_info(pevent, name, offset, size);
3258 if (ret < 0)
3259 return ret;
3260 }
3261 return pevent_read_number(pevent, data + *offset, *size);
3262 }
3263
3264 static int trace_parse_common_type(struct pevent *pevent, void *data)
3265 {
3266 return __parse_common(pevent, data,
3267 &pevent->type_size, &pevent->type_offset,
3268 "common_type");
3269 }
3270
3271 static int parse_common_pid(struct pevent *pevent, void *data)
3272 {
3273 return __parse_common(pevent, data,
3274 &pevent->pid_size, &pevent->pid_offset,
3275 "common_pid");
3276 }
3277
3278 static int parse_common_pc(struct pevent *pevent, void *data)
3279 {
3280 return __parse_common(pevent, data,
3281 &pevent->pc_size, &pevent->pc_offset,
3282 "common_preempt_count");
3283 }
3284
3285 static int parse_common_flags(struct pevent *pevent, void *data)
3286 {
3287 return __parse_common(pevent, data,
3288 &pevent->flags_size, &pevent->flags_offset,
3289 "common_flags");
3290 }
3291
3292 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3293 {
3294 return __parse_common(pevent, data,
3295 &pevent->ld_size, &pevent->ld_offset,
3296 "common_lock_depth");
3297 }
3298
3299 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3300 {
3301 return __parse_common(pevent, data,
3302 &pevent->ld_size, &pevent->ld_offset,
3303 "common_migrate_disable");
3304 }
3305
3306 static int events_id_cmp(const void *a, const void *b);
3307
3308 /**
3309 * pevent_find_event - find an event by given id
3310 * @pevent: a handle to the pevent
3311 * @id: the id of the event
3312 *
3313 * Returns an event that has a given @id.
3314 */
3315 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3316 {
3317 struct event_format **eventptr;
3318 struct event_format key;
3319 struct event_format *pkey = &key;
3320
3321 /* Check cache first */
3322 if (pevent->last_event && pevent->last_event->id == id)
3323 return pevent->last_event;
3324
3325 key.id = id;
3326
3327 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3328 sizeof(*pevent->events), events_id_cmp);
3329
3330 if (eventptr) {
3331 pevent->last_event = *eventptr;
3332 return *eventptr;
3333 }
3334
3335 return NULL;
3336 }
3337
3338 /**
3339 * pevent_find_event_by_name - find an event by given name
3340 * @pevent: a handle to the pevent
3341 * @sys: the system name to search for
3342 * @name: the name of the event to search for
3343 *
3344 * This returns an event with a given @name and under the system
3345 * @sys. If @sys is NULL the first event with @name is returned.
3346 */
3347 struct event_format *
3348 pevent_find_event_by_name(struct pevent *pevent,
3349 const char *sys, const char *name)
3350 {
3351 struct event_format *event;
3352 int i;
3353
3354 if (pevent->last_event &&
3355 strcmp(pevent->last_event->name, name) == 0 &&
3356 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3357 return pevent->last_event;
3358
3359 for (i = 0; i < pevent->nr_events; i++) {
3360 event = pevent->events[i];
3361 if (strcmp(event->name, name) == 0) {
3362 if (!sys)
3363 break;
3364 if (strcmp(event->system, sys) == 0)
3365 break;
3366 }
3367 }
3368 if (i == pevent->nr_events)
3369 event = NULL;
3370
3371 pevent->last_event = event;
3372 return event;
3373 }
3374
3375 static unsigned long long
3376 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3377 {
3378 struct pevent *pevent = event->pevent;
3379 unsigned long long val = 0;
3380 unsigned long long left, right;
3381 struct print_arg *typearg = NULL;
3382 struct print_arg *larg;
3383 unsigned long offset;
3384 unsigned int field_size;
3385
3386 switch (arg->type) {
3387 case PRINT_NULL:
3388 /* ?? */
3389 return 0;
3390 case PRINT_ATOM:
3391 return strtoull(arg->atom.atom, NULL, 0);
3392 case PRINT_FIELD:
3393 if (!arg->field.field) {
3394 arg->field.field = pevent_find_any_field(event, arg->field.name);
3395 if (!arg->field.field)
3396 goto out_warning_field;
3397
3398 }
3399 /* must be a number */
3400 val = pevent_read_number(pevent, data + arg->field.field->offset,
3401 arg->field.field->size);
3402 break;
3403 case PRINT_FLAGS:
3404 case PRINT_SYMBOL:
3405 case PRINT_INT_ARRAY:
3406 case PRINT_HEX:
3407 break;
3408 case PRINT_TYPE:
3409 val = eval_num_arg(data, size, event, arg->typecast.item);
3410 return eval_type(val, arg, 0);
3411 case PRINT_STRING:
3412 case PRINT_BSTRING:
3413 case PRINT_BITMASK:
3414 return 0;
3415 case PRINT_FUNC: {
3416 struct trace_seq s;
3417 trace_seq_init(&s);
3418 val = process_defined_func(&s, data, size, event, arg);
3419 trace_seq_destroy(&s);
3420 return val;
3421 }
3422 case PRINT_OP:
3423 if (strcmp(arg->op.op, "[") == 0) {
3424 /*
3425 * Arrays are special, since we don't want
3426 * to read the arg as is.
3427 */
3428 right = eval_num_arg(data, size, event, arg->op.right);
3429
3430 /* handle typecasts */
3431 larg = arg->op.left;
3432 while (larg->type == PRINT_TYPE) {
3433 if (!typearg)
3434 typearg = larg;
3435 larg = larg->typecast.item;
3436 }
3437
3438 /* Default to long size */
3439 field_size = pevent->long_size;
3440
3441 switch (larg->type) {
3442 case PRINT_DYNAMIC_ARRAY:
3443 offset = pevent_read_number(pevent,
3444 data + larg->dynarray.field->offset,
3445 larg->dynarray.field->size);
3446 if (larg->dynarray.field->elementsize)
3447 field_size = larg->dynarray.field->elementsize;
3448 /*
3449 * The actual length of the dynamic array is stored
3450 * in the top half of the field, and the offset
3451 * is in the bottom half of the 32 bit field.
3452 */
3453 offset &= 0xffff;
3454 offset += right;
3455 break;
3456 case PRINT_FIELD:
3457 if (!larg->field.field) {
3458 larg->field.field =
3459 pevent_find_any_field(event, larg->field.name);
3460 if (!larg->field.field) {
3461 arg = larg;
3462 goto out_warning_field;
3463 }
3464 }
3465 field_size = larg->field.field->elementsize;
3466 offset = larg->field.field->offset +
3467 right * larg->field.field->elementsize;
3468 break;
3469 default:
3470 goto default_op; /* oops, all bets off */
3471 }
3472 val = pevent_read_number(pevent,
3473 data + offset, field_size);
3474 if (typearg)
3475 val = eval_type(val, typearg, 1);
3476 break;
3477 } else if (strcmp(arg->op.op, "?") == 0) {
3478 left = eval_num_arg(data, size, event, arg->op.left);
3479 arg = arg->op.right;
3480 if (left)
3481 val = eval_num_arg(data, size, event, arg->op.left);
3482 else
3483 val = eval_num_arg(data, size, event, arg->op.right);
3484 break;
3485 }
3486 default_op:
3487 left = eval_num_arg(data, size, event, arg->op.left);
3488 right = eval_num_arg(data, size, event, arg->op.right);
3489 switch (arg->op.op[0]) {
3490 case '!':
3491 switch (arg->op.op[1]) {
3492 case 0:
3493 val = !right;
3494 break;
3495 case '=':
3496 val = left != right;
3497 break;
3498 default:
3499 goto out_warning_op;
3500 }
3501 break;
3502 case '~':
3503 val = ~right;
3504 break;
3505 case '|':
3506 if (arg->op.op[1])
3507 val = left || right;
3508 else
3509 val = left | right;
3510 break;
3511 case '&':
3512 if (arg->op.op[1])
3513 val = left && right;
3514 else
3515 val = left & right;
3516 break;
3517 case '<':
3518 switch (arg->op.op[1]) {
3519 case 0:
3520 val = left < right;
3521 break;
3522 case '<':
3523 val = left << right;
3524 break;
3525 case '=':
3526 val = left <= right;
3527 break;
3528 default:
3529 goto out_warning_op;
3530 }
3531 break;
3532 case '>':
3533 switch (arg->op.op[1]) {
3534 case 0:
3535 val = left > right;
3536 break;
3537 case '>':
3538 val = left >> right;
3539 break;
3540 case '=':
3541 val = left >= right;
3542 break;
3543 default:
3544 goto out_warning_op;
3545 }
3546 break;
3547 case '=':
3548 if (arg->op.op[1] != '=')
3549 goto out_warning_op;
3550
3551 val = left == right;
3552 break;
3553 case '-':
3554 val = left - right;
3555 break;
3556 case '+':
3557 val = left + right;
3558 break;
3559 case '/':
3560 val = left / right;
3561 break;
3562 case '*':
3563 val = left * right;
3564 break;
3565 default:
3566 goto out_warning_op;
3567 }
3568 break;
3569 case PRINT_DYNAMIC_ARRAY:
3570 /* Without [], we pass the address to the dynamic data */
3571 offset = pevent_read_number(pevent,
3572 data + arg->dynarray.field->offset,
3573 arg->dynarray.field->size);
3574 /*
3575 * The actual length of the dynamic array is stored
3576 * in the top half of the field, and the offset
3577 * is in the bottom half of the 32 bit field.
3578 */
3579 offset &= 0xffff;
3580 val = (unsigned long long)((unsigned long)data + offset);
3581 break;
3582 default: /* not sure what to do there */
3583 return 0;
3584 }
3585 return val;
3586
3587 out_warning_op:
3588 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3589 return 0;
3590
3591 out_warning_field:
3592 do_warning_event(event, "%s: field %s not found",
3593 __func__, arg->field.name);
3594 return 0;
3595 }
3596
3597 struct flag {
3598 const char *name;
3599 unsigned long long value;
3600 };
3601
3602 static const struct flag flags[] = {
3603 { "HI_SOFTIRQ", 0 },
3604 { "TIMER_SOFTIRQ", 1 },
3605 { "NET_TX_SOFTIRQ", 2 },
3606 { "NET_RX_SOFTIRQ", 3 },
3607 { "BLOCK_SOFTIRQ", 4 },
3608 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3609 { "TASKLET_SOFTIRQ", 6 },
3610 { "SCHED_SOFTIRQ", 7 },
3611 { "HRTIMER_SOFTIRQ", 8 },
3612 { "RCU_SOFTIRQ", 9 },
3613
3614 { "HRTIMER_NORESTART", 0 },
3615 { "HRTIMER_RESTART", 1 },
3616 };
3617
3618 static unsigned long long eval_flag(const char *flag)
3619 {
3620 int i;
3621
3622 /*
3623 * Some flags in the format files do not get converted.
3624 * If the flag is not numeric, see if it is something that
3625 * we already know about.
3626 */
3627 if (isdigit(flag[0]))
3628 return strtoull(flag, NULL, 0);
3629
3630 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3631 if (strcmp(flags[i].name, flag) == 0)
3632 return flags[i].value;
3633
3634 return 0;
3635 }
3636
3637 static void print_str_to_seq(struct trace_seq *s, const char *format,
3638 int len_arg, const char *str)
3639 {
3640 if (len_arg >= 0)
3641 trace_seq_printf(s, format, len_arg, str);
3642 else
3643 trace_seq_printf(s, format, str);
3644 }
3645
3646 static void print_bitmask_to_seq(struct pevent *pevent,
3647 struct trace_seq *s, const char *format,
3648 int len_arg, const void *data, int size)
3649 {
3650 int nr_bits = size * 8;
3651 int str_size = (nr_bits + 3) / 4;
3652 int len = 0;
3653 char buf[3];
3654 char *str;
3655 int index;
3656 int i;
3657
3658 /*
3659 * The kernel likes to put in commas every 32 bits, we
3660 * can do the same.
3661 */
3662 str_size += (nr_bits - 1) / 32;
3663
3664 str = malloc(str_size + 1);
3665 if (!str) {
3666 do_warning("%s: not enough memory!", __func__);
3667 return;
3668 }
3669 str[str_size] = 0;
3670
3671 /* Start out with -2 for the two chars per byte */
3672 for (i = str_size - 2; i >= 0; i -= 2) {
3673 /*
3674 * data points to a bit mask of size bytes.
3675 * In the kernel, this is an array of long words, thus
3676 * endianess is very important.
3677 */
3678 if (pevent->file_bigendian)
3679 index = size - (len + 1);
3680 else
3681 index = len;
3682
3683 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3684 memcpy(str + i, buf, 2);
3685 len++;
3686 if (!(len & 3) && i > 0) {
3687 i--;
3688 str[i] = ',';
3689 }
3690 }
3691
3692 if (len_arg >= 0)
3693 trace_seq_printf(s, format, len_arg, str);
3694 else
3695 trace_seq_printf(s, format, str);
3696
3697 free(str);
3698 }
3699
3700 static void print_str_arg(struct trace_seq *s, void *data, int size,
3701 struct event_format *event, const char *format,
3702 int len_arg, struct print_arg *arg)
3703 {
3704 struct pevent *pevent = event->pevent;
3705 struct print_flag_sym *flag;
3706 struct format_field *field;
3707 struct printk_map *printk;
3708 unsigned long long val, fval;
3709 unsigned long addr;
3710 char *str;
3711 unsigned char *hex;
3712 int print;
3713 int i, len;
3714
3715 switch (arg->type) {
3716 case PRINT_NULL:
3717 /* ?? */
3718 return;
3719 case PRINT_ATOM:
3720 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3721 return;
3722 case PRINT_FIELD:
3723 field = arg->field.field;
3724 if (!field) {
3725 field = pevent_find_any_field(event, arg->field.name);
3726 if (!field) {
3727 str = arg->field.name;
3728 goto out_warning_field;
3729 }
3730 arg->field.field = field;
3731 }
3732 /* Zero sized fields, mean the rest of the data */
3733 len = field->size ? : size - field->offset;
3734
3735 /*
3736 * Some events pass in pointers. If this is not an array
3737 * and the size is the same as long_size, assume that it
3738 * is a pointer.
3739 */
3740 if (!(field->flags & FIELD_IS_ARRAY) &&
3741 field->size == pevent->long_size) {
3742 addr = *(unsigned long *)(data + field->offset);
3743 /* Check if it matches a print format */
3744 printk = find_printk(pevent, addr);
3745 if (printk)
3746 trace_seq_puts(s, printk->printk);
3747 else
3748 trace_seq_printf(s, "%lx", addr);
3749 break;
3750 }
3751 str = malloc(len + 1);
3752 if (!str) {
3753 do_warning_event(event, "%s: not enough memory!",
3754 __func__);
3755 return;
3756 }
3757 memcpy(str, data + field->offset, len);
3758 str[len] = 0;
3759 print_str_to_seq(s, format, len_arg, str);
3760 free(str);
3761 break;
3762 case PRINT_FLAGS:
3763 val = eval_num_arg(data, size, event, arg->flags.field);
3764 print = 0;
3765 for (flag = arg->flags.flags; flag; flag = flag->next) {
3766 fval = eval_flag(flag->value);
3767 if (!val && !fval) {
3768 print_str_to_seq(s, format, len_arg, flag->str);
3769 break;
3770 }
3771 if (fval && (val & fval) == fval) {
3772 if (print && arg->flags.delim)
3773 trace_seq_puts(s, arg->flags.delim);
3774 print_str_to_seq(s, format, len_arg, flag->str);
3775 print = 1;
3776 val &= ~fval;
3777 }
3778 }
3779 break;
3780 case PRINT_SYMBOL:
3781 val = eval_num_arg(data, size, event, arg->symbol.field);
3782 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3783 fval = eval_flag(flag->value);
3784 if (val == fval) {
3785 print_str_to_seq(s, format, len_arg, flag->str);
3786 break;
3787 }
3788 }
3789 break;
3790 case PRINT_HEX:
3791 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3792 unsigned long offset;
3793 offset = pevent_read_number(pevent,
3794 data + arg->hex.field->dynarray.field->offset,
3795 arg->hex.field->dynarray.field->size);
3796 hex = data + (offset & 0xffff);
3797 } else {
3798 field = arg->hex.field->field.field;
3799 if (!field) {
3800 str = arg->hex.field->field.name;
3801 field = pevent_find_any_field(event, str);
3802 if (!field)
3803 goto out_warning_field;
3804 arg->hex.field->field.field = field;
3805 }
3806 hex = data + field->offset;
3807 }
3808 len = eval_num_arg(data, size, event, arg->hex.size);
3809 for (i = 0; i < len; i++) {
3810 if (i)
3811 trace_seq_putc(s, ' ');
3812 trace_seq_printf(s, "%02x", hex[i]);
3813 }
3814 break;
3815
3816 case PRINT_INT_ARRAY: {
3817 void *num;
3818 int el_size;
3819
3820 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3821 unsigned long offset;
3822 struct format_field *field =
3823 arg->int_array.field->dynarray.field;
3824 offset = pevent_read_number(pevent,
3825 data + field->offset,
3826 field->size);
3827 num = data + (offset & 0xffff);
3828 } else {
3829 field = arg->int_array.field->field.field;
3830 if (!field) {
3831 str = arg->int_array.field->field.name;
3832 field = pevent_find_any_field(event, str);
3833 if (!field)
3834 goto out_warning_field;
3835 arg->int_array.field->field.field = field;
3836 }
3837 num = data + field->offset;
3838 }
3839 len = eval_num_arg(data, size, event, arg->int_array.count);
3840 el_size = eval_num_arg(data, size, event,
3841 arg->int_array.el_size);
3842 for (i = 0; i < len; i++) {
3843 if (i)
3844 trace_seq_putc(s, ' ');
3845
3846 if (el_size == 1) {
3847 trace_seq_printf(s, "%u", *(uint8_t *)num);
3848 } else if (el_size == 2) {
3849 trace_seq_printf(s, "%u", *(uint16_t *)num);
3850 } else if (el_size == 4) {
3851 trace_seq_printf(s, "%u", *(uint32_t *)num);
3852 } else if (el_size == 8) {
3853 trace_seq_printf(s, "%lu", *(uint64_t *)num);
3854 } else {
3855 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3856 el_size, *(uint8_t *)num);
3857 el_size = 1;
3858 }
3859
3860 num += el_size;
3861 }
3862 break;
3863 }
3864 case PRINT_TYPE:
3865 break;
3866 case PRINT_STRING: {
3867 int str_offset;
3868
3869 if (arg->string.offset == -1) {
3870 struct format_field *f;
3871
3872 f = pevent_find_any_field(event, arg->string.string);
3873 arg->string.offset = f->offset;
3874 }
3875 str_offset = data2host4(pevent, data + arg->string.offset);
3876 str_offset &= 0xffff;
3877 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3878 break;
3879 }
3880 case PRINT_BSTRING:
3881 print_str_to_seq(s, format, len_arg, arg->string.string);
3882 break;
3883 case PRINT_BITMASK: {
3884 int bitmask_offset;
3885 int bitmask_size;
3886
3887 if (arg->bitmask.offset == -1) {
3888 struct format_field *f;
3889
3890 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3891 arg->bitmask.offset = f->offset;
3892 }
3893 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3894 bitmask_size = bitmask_offset >> 16;
3895 bitmask_offset &= 0xffff;
3896 print_bitmask_to_seq(pevent, s, format, len_arg,
3897 data + bitmask_offset, bitmask_size);
3898 break;
3899 }
3900 case PRINT_OP:
3901 /*
3902 * The only op for string should be ? :
3903 */
3904 if (arg->op.op[0] != '?')
3905 return;
3906 val = eval_num_arg(data, size, event, arg->op.left);
3907 if (val)
3908 print_str_arg(s, data, size, event,
3909 format, len_arg, arg->op.right->op.left);
3910 else
3911 print_str_arg(s, data, size, event,
3912 format, len_arg, arg->op.right->op.right);
3913 break;
3914 case PRINT_FUNC:
3915 process_defined_func(s, data, size, event, arg);
3916 break;
3917 default:
3918 /* well... */
3919 break;
3920 }
3921
3922 return;
3923
3924 out_warning_field:
3925 do_warning_event(event, "%s: field %s not found",
3926 __func__, arg->field.name);
3927 }
3928
3929 static unsigned long long
3930 process_defined_func(struct trace_seq *s, void *data, int size,
3931 struct event_format *event, struct print_arg *arg)
3932 {
3933 struct pevent_function_handler *func_handle = arg->func.func;
3934 struct pevent_func_params *param;
3935 unsigned long long *args;
3936 unsigned long long ret;
3937 struct print_arg *farg;
3938 struct trace_seq str;
3939 struct save_str {
3940 struct save_str *next;
3941 char *str;
3942 } *strings = NULL, *string;
3943 int i;
3944
3945 if (!func_handle->nr_args) {
3946 ret = (*func_handle->func)(s, NULL);
3947 goto out;
3948 }
3949
3950 farg = arg->func.args;
3951 param = func_handle->params;
3952
3953 ret = ULLONG_MAX;
3954 args = malloc(sizeof(*args) * func_handle->nr_args);
3955 if (!args)
3956 goto out;
3957
3958 for (i = 0; i < func_handle->nr_args; i++) {
3959 switch (param->type) {
3960 case PEVENT_FUNC_ARG_INT:
3961 case PEVENT_FUNC_ARG_LONG:
3962 case PEVENT_FUNC_ARG_PTR:
3963 args[i] = eval_num_arg(data, size, event, farg);
3964 break;
3965 case PEVENT_FUNC_ARG_STRING:
3966 trace_seq_init(&str);
3967 print_str_arg(&str, data, size, event, "%s", -1, farg);
3968 trace_seq_terminate(&str);
3969 string = malloc(sizeof(*string));
3970 if (!string) {
3971 do_warning_event(event, "%s(%d): malloc str",
3972 __func__, __LINE__);
3973 goto out_free;
3974 }
3975 string->next = strings;
3976 string->str = strdup(str.buffer);
3977 if (!string->str) {
3978 free(string);
3979 do_warning_event(event, "%s(%d): malloc str",
3980 __func__, __LINE__);
3981 goto out_free;
3982 }
3983 args[i] = (uintptr_t)string->str;
3984 strings = string;
3985 trace_seq_destroy(&str);
3986 break;
3987 default:
3988 /*
3989 * Something went totally wrong, this is not
3990 * an input error, something in this code broke.
3991 */
3992 do_warning_event(event, "Unexpected end of arguments\n");
3993 goto out_free;
3994 }
3995 farg = farg->next;
3996 param = param->next;
3997 }
3998
3999 ret = (*func_handle->func)(s, args);
4000 out_free:
4001 free(args);
4002 while (strings) {
4003 string = strings;
4004 strings = string->next;
4005 free(string->str);
4006 free(string);
4007 }
4008
4009 out:
4010 /* TBD : handle return type here */
4011 return ret;
4012 }
4013
4014 static void free_args(struct print_arg *args)
4015 {
4016 struct print_arg *next;
4017
4018 while (args) {
4019 next = args->next;
4020
4021 free_arg(args);
4022 args = next;
4023 }
4024 }
4025
4026 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4027 {
4028 struct pevent *pevent = event->pevent;
4029 struct format_field *field, *ip_field;
4030 struct print_arg *args, *arg, **next;
4031 unsigned long long ip, val;
4032 char *ptr;
4033 void *bptr;
4034 int vsize;
4035
4036 field = pevent->bprint_buf_field;
4037 ip_field = pevent->bprint_ip_field;
4038
4039 if (!field) {
4040 field = pevent_find_field(event, "buf");
4041 if (!field) {
4042 do_warning_event(event, "can't find buffer field for binary printk");
4043 return NULL;
4044 }
4045 ip_field = pevent_find_field(event, "ip");
4046 if (!ip_field) {
4047 do_warning_event(event, "can't find ip field for binary printk");
4048 return NULL;
4049 }
4050 pevent->bprint_buf_field = field;
4051 pevent->bprint_ip_field = ip_field;
4052 }
4053
4054 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4055
4056 /*
4057 * The first arg is the IP pointer.
4058 */
4059 args = alloc_arg();
4060 if (!args) {
4061 do_warning_event(event, "%s(%d): not enough memory!",
4062 __func__, __LINE__);
4063 return NULL;
4064 }
4065 arg = args;
4066 arg->next = NULL;
4067 next = &arg->next;
4068
4069 arg->type = PRINT_ATOM;
4070
4071 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4072 goto out_free;
4073
4074 /* skip the first "%pf: " */
4075 for (ptr = fmt + 5, bptr = data + field->offset;
4076 bptr < data + size && *ptr; ptr++) {
4077 int ls = 0;
4078
4079 if (*ptr == '%') {
4080 process_again:
4081 ptr++;
4082 switch (*ptr) {
4083 case '%':
4084 break;
4085 case 'l':
4086 ls++;
4087 goto process_again;
4088 case 'L':
4089 ls = 2;
4090 goto process_again;
4091 case '0' ... '9':
4092 goto process_again;
4093 case '.':
4094 goto process_again;
4095 case 'z':
4096 case 'Z':
4097 ls = 1;
4098 goto process_again;
4099 case 'p':
4100 ls = 1;
4101 /* fall through */
4102 case 'd':
4103 case 'u':
4104 case 'x':
4105 case 'i':
4106 switch (ls) {
4107 case 0:
4108 vsize = 4;
4109 break;
4110 case 1:
4111 vsize = pevent->long_size;
4112 break;
4113 case 2:
4114 vsize = 8;
4115 break;
4116 default:
4117 vsize = ls; /* ? */
4118 break;
4119 }
4120 /* fall through */
4121 case '*':
4122 if (*ptr == '*')
4123 vsize = 4;
4124
4125 /* the pointers are always 4 bytes aligned */
4126 bptr = (void *)(((unsigned long)bptr + 3) &
4127 ~3);
4128 val = pevent_read_number(pevent, bptr, vsize);
4129 bptr += vsize;
4130 arg = alloc_arg();
4131 if (!arg) {
4132 do_warning_event(event, "%s(%d): not enough memory!",
4133 __func__, __LINE__);
4134 goto out_free;
4135 }
4136 arg->next = NULL;
4137 arg->type = PRINT_ATOM;
4138 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4139 free(arg);
4140 goto out_free;
4141 }
4142 *next = arg;
4143 next = &arg->next;
4144 /*
4145 * The '*' case means that an arg is used as the length.
4146 * We need to continue to figure out for what.
4147 */
4148 if (*ptr == '*')
4149 goto process_again;
4150
4151 break;
4152 case 's':
4153 arg = alloc_arg();
4154 if (!arg) {
4155 do_warning_event(event, "%s(%d): not enough memory!",
4156 __func__, __LINE__);
4157 goto out_free;
4158 }
4159 arg->next = NULL;
4160 arg->type = PRINT_BSTRING;
4161 arg->string.string = strdup(bptr);
4162 if (!arg->string.string)
4163 goto out_free;
4164 bptr += strlen(bptr) + 1;
4165 *next = arg;
4166 next = &arg->next;
4167 default:
4168 break;
4169 }
4170 }
4171 }
4172
4173 return args;
4174
4175 out_free:
4176 free_args(args);
4177 return NULL;
4178 }
4179
4180 static char *
4181 get_bprint_format(void *data, int size __maybe_unused,
4182 struct event_format *event)
4183 {
4184 struct pevent *pevent = event->pevent;
4185 unsigned long long addr;
4186 struct format_field *field;
4187 struct printk_map *printk;
4188 char *format;
4189
4190 field = pevent->bprint_fmt_field;
4191
4192 if (!field) {
4193 field = pevent_find_field(event, "fmt");
4194 if (!field) {
4195 do_warning_event(event, "can't find format field for binary printk");
4196 return NULL;
4197 }
4198 pevent->bprint_fmt_field = field;
4199 }
4200
4201 addr = pevent_read_number(pevent, data + field->offset, field->size);
4202
4203 printk = find_printk(pevent, addr);
4204 if (!printk) {
4205 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4206 return NULL;
4207 return format;
4208 }
4209
4210 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4211 return NULL;
4212
4213 return format;
4214 }
4215
4216 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4217 struct event_format *event, struct print_arg *arg)
4218 {
4219 unsigned char *buf;
4220 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4221
4222 if (arg->type == PRINT_FUNC) {
4223 process_defined_func(s, data, size, event, arg);
4224 return;
4225 }
4226
4227 if (arg->type != PRINT_FIELD) {
4228 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4229 arg->type);
4230 return;
4231 }
4232
4233 if (mac == 'm')
4234 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4235 if (!arg->field.field) {
4236 arg->field.field =
4237 pevent_find_any_field(event, arg->field.name);
4238 if (!arg->field.field) {
4239 do_warning_event(event, "%s: field %s not found",
4240 __func__, arg->field.name);
4241 return;
4242 }
4243 }
4244 if (arg->field.field->size != 6) {
4245 trace_seq_printf(s, "INVALIDMAC");
4246 return;
4247 }
4248 buf = data + arg->field.field->offset;
4249 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4250 }
4251
4252 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4253 {
4254 const char *fmt;
4255
4256 if (i == 'i')
4257 fmt = "%03d.%03d.%03d.%03d";
4258 else
4259 fmt = "%d.%d.%d.%d";
4260
4261 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4262 }
4263
4264 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4265 {
4266 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4267 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4268 }
4269
4270 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4271 {
4272 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4273 }
4274
4275 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4276 {
4277 int i, j, range;
4278 unsigned char zerolength[8];
4279 int longest = 1;
4280 int colonpos = -1;
4281 uint16_t word;
4282 uint8_t hi, lo;
4283 bool needcolon = false;
4284 bool useIPv4;
4285 struct in6_addr in6;
4286
4287 memcpy(&in6, addr, sizeof(struct in6_addr));
4288
4289 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4290
4291 memset(zerolength, 0, sizeof(zerolength));
4292
4293 if (useIPv4)
4294 range = 6;
4295 else
4296 range = 8;
4297
4298 /* find position of longest 0 run */
4299 for (i = 0; i < range; i++) {
4300 for (j = i; j < range; j++) {
4301 if (in6.s6_addr16[j] != 0)
4302 break;
4303 zerolength[i]++;
4304 }
4305 }
4306 for (i = 0; i < range; i++) {
4307 if (zerolength[i] > longest) {
4308 longest = zerolength[i];
4309 colonpos = i;
4310 }
4311 }
4312 if (longest == 1) /* don't compress a single 0 */
4313 colonpos = -1;
4314
4315 /* emit address */
4316 for (i = 0; i < range; i++) {
4317 if (i == colonpos) {
4318 if (needcolon || i == 0)
4319 trace_seq_printf(s, ":");
4320 trace_seq_printf(s, ":");
4321 needcolon = false;
4322 i += longest - 1;
4323 continue;
4324 }
4325 if (needcolon) {
4326 trace_seq_printf(s, ":");
4327 needcolon = false;
4328 }
4329 /* hex u16 without leading 0s */
4330 word = ntohs(in6.s6_addr16[i]);
4331 hi = word >> 8;
4332 lo = word & 0xff;
4333 if (hi)
4334 trace_seq_printf(s, "%x%02x", hi, lo);
4335 else
4336 trace_seq_printf(s, "%x", lo);
4337
4338 needcolon = true;
4339 }
4340
4341 if (useIPv4) {
4342 if (needcolon)
4343 trace_seq_printf(s, ":");
4344 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4345 }
4346
4347 return;
4348 }
4349
4350 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4351 {
4352 int j;
4353
4354 for (j = 0; j < 16; j += 2) {
4355 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4356 if (i == 'I' && j < 14)
4357 trace_seq_printf(s, ":");
4358 }
4359 }
4360
4361 /*
4362 * %pi4 print an IPv4 address with leading zeros
4363 * %pI4 print an IPv4 address without leading zeros
4364 * %pi6 print an IPv6 address without colons
4365 * %pI6 print an IPv6 address with colons
4366 * %pI6c print an IPv6 address in compressed form with colons
4367 * %pISpc print an IP address based on sockaddr; p adds port.
4368 */
4369 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4370 void *data, int size, struct event_format *event,
4371 struct print_arg *arg)
4372 {
4373 unsigned char *buf;
4374
4375 if (arg->type == PRINT_FUNC) {
4376 process_defined_func(s, data, size, event, arg);
4377 return 0;
4378 }
4379
4380 if (arg->type != PRINT_FIELD) {
4381 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4382 return 0;
4383 }
4384
4385 if (!arg->field.field) {
4386 arg->field.field =
4387 pevent_find_any_field(event, arg->field.name);
4388 if (!arg->field.field) {
4389 do_warning("%s: field %s not found",
4390 __func__, arg->field.name);
4391 return 0;
4392 }
4393 }
4394
4395 buf = data + arg->field.field->offset;
4396
4397 if (arg->field.field->size != 4) {
4398 trace_seq_printf(s, "INVALIDIPv4");
4399 return 0;
4400 }
4401 print_ip4_addr(s, i, buf);
4402
4403 return 0;
4404 }
4405
4406 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4407 void *data, int size, struct event_format *event,
4408 struct print_arg *arg)
4409 {
4410 char have_c = 0;
4411 unsigned char *buf;
4412 int rc = 0;
4413
4414 /* pI6c */
4415 if (i == 'I' && *ptr == 'c') {
4416 have_c = 1;
4417 ptr++;
4418 rc++;
4419 }
4420
4421 if (arg->type == PRINT_FUNC) {
4422 process_defined_func(s, data, size, event, arg);
4423 return rc;
4424 }
4425
4426 if (arg->type != PRINT_FIELD) {
4427 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4428 return rc;
4429 }
4430
4431 if (!arg->field.field) {
4432 arg->field.field =
4433 pevent_find_any_field(event, arg->field.name);
4434 if (!arg->field.field) {
4435 do_warning("%s: field %s not found",
4436 __func__, arg->field.name);
4437 return rc;
4438 }
4439 }
4440
4441 buf = data + arg->field.field->offset;
4442
4443 if (arg->field.field->size != 16) {
4444 trace_seq_printf(s, "INVALIDIPv6");
4445 return rc;
4446 }
4447
4448 if (have_c)
4449 print_ip6c_addr(s, buf);
4450 else
4451 print_ip6_addr(s, i, buf);
4452
4453 return rc;
4454 }
4455
4456 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4457 void *data, int size, struct event_format *event,
4458 struct print_arg *arg)
4459 {
4460 char have_c = 0, have_p = 0;
4461 unsigned char *buf;
4462 struct sockaddr_storage *sa;
4463 int rc = 0;
4464
4465 /* pISpc */
4466 if (i == 'I') {
4467 if (*ptr == 'p') {
4468 have_p = 1;
4469 ptr++;
4470 rc++;
4471 }
4472 if (*ptr == 'c') {
4473 have_c = 1;
4474 ptr++;
4475 rc++;
4476 }
4477 }
4478
4479 if (arg->type == PRINT_FUNC) {
4480 process_defined_func(s, data, size, event, arg);
4481 return rc;
4482 }
4483
4484 if (arg->type != PRINT_FIELD) {
4485 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4486 return rc;
4487 }
4488
4489 if (!arg->field.field) {
4490 arg->field.field =
4491 pevent_find_any_field(event, arg->field.name);
4492 if (!arg->field.field) {
4493 do_warning("%s: field %s not found",
4494 __func__, arg->field.name);
4495 return rc;
4496 }
4497 }
4498
4499 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4500
4501 if (sa->ss_family == AF_INET) {
4502 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4503
4504 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4505 trace_seq_printf(s, "INVALIDIPv4");
4506 return rc;
4507 }
4508
4509 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4510 if (have_p)
4511 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4512
4513
4514 } else if (sa->ss_family == AF_INET6) {
4515 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4516
4517 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4518 trace_seq_printf(s, "INVALIDIPv6");
4519 return rc;
4520 }
4521
4522 if (have_p)
4523 trace_seq_printf(s, "[");
4524
4525 buf = (unsigned char *) &sa6->sin6_addr;
4526 if (have_c)
4527 print_ip6c_addr(s, buf);
4528 else
4529 print_ip6_addr(s, i, buf);
4530
4531 if (have_p)
4532 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4533 }
4534
4535 return rc;
4536 }
4537
4538 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4539 void *data, int size, struct event_format *event,
4540 struct print_arg *arg)
4541 {
4542 char i = *ptr; /* 'i' or 'I' */
4543 char ver;
4544 int rc = 0;
4545
4546 ptr++;
4547 rc++;
4548
4549 ver = *ptr;
4550 ptr++;
4551 rc++;
4552
4553 switch (ver) {
4554 case '4':
4555 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4556 break;
4557 case '6':
4558 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4559 break;
4560 case 'S':
4561 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4562 break;
4563 default:
4564 return 0;
4565 }
4566
4567 return rc;
4568 }
4569
4570 static int is_printable_array(char *p, unsigned int len)
4571 {
4572 unsigned int i;
4573
4574 for (i = 0; i < len && p[i]; i++)
4575 if (!isprint(p[i]) && !isspace(p[i]))
4576 return 0;
4577 return 1;
4578 }
4579
4580 static void print_event_fields(struct trace_seq *s, void *data,
4581 int size __maybe_unused,
4582 struct event_format *event)
4583 {
4584 struct format_field *field;
4585 unsigned long long val;
4586 unsigned int offset, len, i;
4587
4588 field = event->format.fields;
4589 while (field) {
4590 trace_seq_printf(s, " %s=", field->name);
4591 if (field->flags & FIELD_IS_ARRAY) {
4592 offset = field->offset;
4593 len = field->size;
4594 if (field->flags & FIELD_IS_DYNAMIC) {
4595 val = pevent_read_number(event->pevent, data + offset, len);
4596 offset = val;
4597 len = offset >> 16;
4598 offset &= 0xffff;
4599 }
4600 if (field->flags & FIELD_IS_STRING &&
4601 is_printable_array(data + offset, len)) {
4602 trace_seq_printf(s, "%s", (char *)data + offset);
4603 } else {
4604 trace_seq_puts(s, "ARRAY[");
4605 for (i = 0; i < len; i++) {
4606 if (i)
4607 trace_seq_puts(s, ", ");
4608 trace_seq_printf(s, "%02x",
4609 *((unsigned char *)data + offset + i));
4610 }
4611 trace_seq_putc(s, ']');
4612 field->flags &= ~FIELD_IS_STRING;
4613 }
4614 } else {
4615 val = pevent_read_number(event->pevent, data + field->offset,
4616 field->size);
4617 if (field->flags & FIELD_IS_POINTER) {
4618 trace_seq_printf(s, "0x%llx", val);
4619 } else if (field->flags & FIELD_IS_SIGNED) {
4620 switch (field->size) {
4621 case 4:
4622 /*
4623 * If field is long then print it in hex.
4624 * A long usually stores pointers.
4625 */
4626 if (field->flags & FIELD_IS_LONG)
4627 trace_seq_printf(s, "0x%x", (int)val);
4628 else
4629 trace_seq_printf(s, "%d", (int)val);
4630 break;
4631 case 2:
4632 trace_seq_printf(s, "%2d", (short)val);
4633 break;
4634 case 1:
4635 trace_seq_printf(s, "%1d", (char)val);
4636 break;
4637 default:
4638 trace_seq_printf(s, "%lld", val);
4639 }
4640 } else {
4641 if (field->flags & FIELD_IS_LONG)
4642 trace_seq_printf(s, "0x%llx", val);
4643 else
4644 trace_seq_printf(s, "%llu", val);
4645 }
4646 }
4647 field = field->next;
4648 }
4649 }
4650
4651 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4652 {
4653 struct pevent *pevent = event->pevent;
4654 struct print_fmt *print_fmt = &event->print_fmt;
4655 struct print_arg *arg = print_fmt->args;
4656 struct print_arg *args = NULL;
4657 const char *ptr = print_fmt->format;
4658 unsigned long long val;
4659 struct func_map *func;
4660 const char *saveptr;
4661 struct trace_seq p;
4662 char *bprint_fmt = NULL;
4663 char format[32];
4664 int show_func;
4665 int len_as_arg;
4666 int len_arg;
4667 int len;
4668 int ls;
4669
4670 if (event->flags & EVENT_FL_FAILED) {
4671 trace_seq_printf(s, "[FAILED TO PARSE]");
4672 print_event_fields(s, data, size, event);
4673 return;
4674 }
4675
4676 if (event->flags & EVENT_FL_ISBPRINT) {
4677 bprint_fmt = get_bprint_format(data, size, event);
4678 args = make_bprint_args(bprint_fmt, data, size, event);
4679 arg = args;
4680 ptr = bprint_fmt;
4681 }
4682
4683 for (; *ptr; ptr++) {
4684 ls = 0;
4685 if (*ptr == '\\') {
4686 ptr++;
4687 switch (*ptr) {
4688 case 'n':
4689 trace_seq_putc(s, '\n');
4690 break;
4691 case 't':
4692 trace_seq_putc(s, '\t');
4693 break;
4694 case 'r':
4695 trace_seq_putc(s, '\r');
4696 break;
4697 case '\\':
4698 trace_seq_putc(s, '\\');
4699 break;
4700 default:
4701 trace_seq_putc(s, *ptr);
4702 break;
4703 }
4704
4705 } else if (*ptr == '%') {
4706 saveptr = ptr;
4707 show_func = 0;
4708 len_as_arg = 0;
4709 cont_process:
4710 ptr++;
4711 switch (*ptr) {
4712 case '%':
4713 trace_seq_putc(s, '%');
4714 break;
4715 case '#':
4716 /* FIXME: need to handle properly */
4717 goto cont_process;
4718 case 'h':
4719 ls--;
4720 goto cont_process;
4721 case 'l':
4722 ls++;
4723 goto cont_process;
4724 case 'L':
4725 ls = 2;
4726 goto cont_process;
4727 case '*':
4728 /* The argument is the length. */
4729 if (!arg) {
4730 do_warning_event(event, "no argument match");
4731 event->flags |= EVENT_FL_FAILED;
4732 goto out_failed;
4733 }
4734 len_arg = eval_num_arg(data, size, event, arg);
4735 len_as_arg = 1;
4736 arg = arg->next;
4737 goto cont_process;
4738 case '.':
4739 case 'z':
4740 case 'Z':
4741 case '0' ... '9':
4742 goto cont_process;
4743 case 'p':
4744 if (pevent->long_size == 4)
4745 ls = 1;
4746 else
4747 ls = 2;
4748
4749 if (*(ptr+1) == 'F' ||
4750 *(ptr+1) == 'f') {
4751 ptr++;
4752 show_func = *ptr;
4753 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4754 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4755 ptr++;
4756 arg = arg->next;
4757 break;
4758 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4759 int n;
4760
4761 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4762 if (n > 0) {
4763 ptr += n;
4764 arg = arg->next;
4765 break;
4766 }
4767 }
4768
4769 /* fall through */
4770 case 'd':
4771 case 'i':
4772 case 'x':
4773 case 'X':
4774 case 'u':
4775 if (!arg) {
4776 do_warning_event(event, "no argument match");
4777 event->flags |= EVENT_FL_FAILED;
4778 goto out_failed;
4779 }
4780
4781 len = ((unsigned long)ptr + 1) -
4782 (unsigned long)saveptr;
4783
4784 /* should never happen */
4785 if (len > 31) {
4786 do_warning_event(event, "bad format!");
4787 event->flags |= EVENT_FL_FAILED;
4788 len = 31;
4789 }
4790
4791 memcpy(format, saveptr, len);
4792 format[len] = 0;
4793
4794 val = eval_num_arg(data, size, event, arg);
4795 arg = arg->next;
4796
4797 if (show_func) {
4798 func = find_func(pevent, val);
4799 if (func) {
4800 trace_seq_puts(s, func->func);
4801 if (show_func == 'F')
4802 trace_seq_printf(s,
4803 "+0x%llx",
4804 val - func->addr);
4805 break;
4806 }
4807 }
4808 if (pevent->long_size == 8 && ls &&
4809 sizeof(long) != 8) {
4810 char *p;
4811
4812 ls = 2;
4813 /* make %l into %ll */
4814 p = strchr(format, 'l');
4815 if (p)
4816 memmove(p+1, p, strlen(p)+1);
4817 else if (strcmp(format, "%p") == 0)
4818 strcpy(format, "0x%llx");
4819 }
4820 switch (ls) {
4821 case -2:
4822 if (len_as_arg)
4823 trace_seq_printf(s, format, len_arg, (char)val);
4824 else
4825 trace_seq_printf(s, format, (char)val);
4826 break;
4827 case -1:
4828 if (len_as_arg)
4829 trace_seq_printf(s, format, len_arg, (short)val);
4830 else
4831 trace_seq_printf(s, format, (short)val);
4832 break;
4833 case 0:
4834 if (len_as_arg)
4835 trace_seq_printf(s, format, len_arg, (int)val);
4836 else
4837 trace_seq_printf(s, format, (int)val);
4838 break;
4839 case 1:
4840 if (len_as_arg)
4841 trace_seq_printf(s, format, len_arg, (long)val);
4842 else
4843 trace_seq_printf(s, format, (long)val);
4844 break;
4845 case 2:
4846 if (len_as_arg)
4847 trace_seq_printf(s, format, len_arg,
4848 (long long)val);
4849 else
4850 trace_seq_printf(s, format, (long long)val);
4851 break;
4852 default:
4853 do_warning_event(event, "bad count (%d)", ls);
4854 event->flags |= EVENT_FL_FAILED;
4855 }
4856 break;
4857 case 's':
4858 if (!arg) {
4859 do_warning_event(event, "no matching argument");
4860 event->flags |= EVENT_FL_FAILED;
4861 goto out_failed;
4862 }
4863
4864 len = ((unsigned long)ptr + 1) -
4865 (unsigned long)saveptr;
4866
4867 /* should never happen */
4868 if (len > 31) {
4869 do_warning_event(event, "bad format!");
4870 event->flags |= EVENT_FL_FAILED;
4871 len = 31;
4872 }
4873
4874 memcpy(format, saveptr, len);
4875 format[len] = 0;
4876 if (!len_as_arg)
4877 len_arg = -1;
4878 /* Use helper trace_seq */
4879 trace_seq_init(&p);
4880 print_str_arg(&p, data, size, event,
4881 format, len_arg, arg);
4882 trace_seq_terminate(&p);
4883 trace_seq_puts(s, p.buffer);
4884 trace_seq_destroy(&p);
4885 arg = arg->next;
4886 break;
4887 default:
4888 trace_seq_printf(s, ">%c<", *ptr);
4889
4890 }
4891 } else
4892 trace_seq_putc(s, *ptr);
4893 }
4894
4895 if (event->flags & EVENT_FL_FAILED) {
4896 out_failed:
4897 trace_seq_printf(s, "[FAILED TO PARSE]");
4898 }
4899
4900 if (args) {
4901 free_args(args);
4902 free(bprint_fmt);
4903 }
4904 }
4905
4906 /**
4907 * pevent_data_lat_fmt - parse the data for the latency format
4908 * @pevent: a handle to the pevent
4909 * @s: the trace_seq to write to
4910 * @record: the record to read from
4911 *
4912 * This parses out the Latency format (interrupts disabled,
4913 * need rescheduling, in hard/soft interrupt, preempt count
4914 * and lock depth) and places it into the trace_seq.
4915 */
4916 void pevent_data_lat_fmt(struct pevent *pevent,
4917 struct trace_seq *s, struct pevent_record *record)
4918 {
4919 static int check_lock_depth = 1;
4920 static int check_migrate_disable = 1;
4921 static int lock_depth_exists;
4922 static int migrate_disable_exists;
4923 unsigned int lat_flags;
4924 unsigned int pc;
4925 int lock_depth;
4926 int migrate_disable;
4927 int hardirq;
4928 int softirq;
4929 void *data = record->data;
4930
4931 lat_flags = parse_common_flags(pevent, data);
4932 pc = parse_common_pc(pevent, data);
4933 /* lock_depth may not always exist */
4934 if (lock_depth_exists)
4935 lock_depth = parse_common_lock_depth(pevent, data);
4936 else if (check_lock_depth) {
4937 lock_depth = parse_common_lock_depth(pevent, data);
4938 if (lock_depth < 0)
4939 check_lock_depth = 0;
4940 else
4941 lock_depth_exists = 1;
4942 }
4943
4944 /* migrate_disable may not always exist */
4945 if (migrate_disable_exists)
4946 migrate_disable = parse_common_migrate_disable(pevent, data);
4947 else if (check_migrate_disable) {
4948 migrate_disable = parse_common_migrate_disable(pevent, data);
4949 if (migrate_disable < 0)
4950 check_migrate_disable = 0;
4951 else
4952 migrate_disable_exists = 1;
4953 }
4954
4955 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4956 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4957
4958 trace_seq_printf(s, "%c%c%c",
4959 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4960 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4961 'X' : '.',
4962 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4963 'N' : '.',
4964 (hardirq && softirq) ? 'H' :
4965 hardirq ? 'h' : softirq ? 's' : '.');
4966
4967 if (pc)
4968 trace_seq_printf(s, "%x", pc);
4969 else
4970 trace_seq_putc(s, '.');
4971
4972 if (migrate_disable_exists) {
4973 if (migrate_disable < 0)
4974 trace_seq_putc(s, '.');
4975 else
4976 trace_seq_printf(s, "%d", migrate_disable);
4977 }
4978
4979 if (lock_depth_exists) {
4980 if (lock_depth < 0)
4981 trace_seq_putc(s, '.');
4982 else
4983 trace_seq_printf(s, "%d", lock_depth);
4984 }
4985
4986 trace_seq_terminate(s);
4987 }
4988
4989 /**
4990 * pevent_data_type - parse out the given event type
4991 * @pevent: a handle to the pevent
4992 * @rec: the record to read from
4993 *
4994 * This returns the event id from the @rec.
4995 */
4996 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4997 {
4998 return trace_parse_common_type(pevent, rec->data);
4999 }
5000
5001 /**
5002 * pevent_data_event_from_type - find the event by a given type
5003 * @pevent: a handle to the pevent
5004 * @type: the type of the event.
5005 *
5006 * This returns the event form a given @type;
5007 */
5008 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5009 {
5010 return pevent_find_event(pevent, type);
5011 }
5012
5013 /**
5014 * pevent_data_pid - parse the PID from raw data
5015 * @pevent: a handle to the pevent
5016 * @rec: the record to parse
5017 *
5018 * This returns the PID from a raw data.
5019 */
5020 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5021 {
5022 return parse_common_pid(pevent, rec->data);
5023 }
5024
5025 /**
5026 * pevent_data_comm_from_pid - return the command line from PID
5027 * @pevent: a handle to the pevent
5028 * @pid: the PID of the task to search for
5029 *
5030 * This returns a pointer to the command line that has the given
5031 * @pid.
5032 */
5033 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5034 {
5035 const char *comm;
5036
5037 comm = find_cmdline(pevent, pid);
5038 return comm;
5039 }
5040
5041 static struct cmdline *
5042 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5043 {
5044 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5045
5046 if (cmdlist)
5047 cmdlist = cmdlist->next;
5048 else
5049 cmdlist = pevent->cmdlist;
5050
5051 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5052 cmdlist = cmdlist->next;
5053
5054 return (struct cmdline *)cmdlist;
5055 }
5056
5057 /**
5058 * pevent_data_pid_from_comm - return the pid from a given comm
5059 * @pevent: a handle to the pevent
5060 * @comm: the cmdline to find the pid from
5061 * @next: the cmdline structure to find the next comm
5062 *
5063 * This returns the cmdline structure that holds a pid for a given
5064 * comm, or NULL if none found. As there may be more than one pid for
5065 * a given comm, the result of this call can be passed back into
5066 * a recurring call in the @next paramater, and then it will find the
5067 * next pid.
5068 * Also, it does a linear seach, so it may be slow.
5069 */
5070 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5071 struct cmdline *next)
5072 {
5073 struct cmdline *cmdline;
5074
5075 /*
5076 * If the cmdlines have not been converted yet, then use
5077 * the list.
5078 */
5079 if (!pevent->cmdlines)
5080 return pid_from_cmdlist(pevent, comm, next);
5081
5082 if (next) {
5083 /*
5084 * The next pointer could have been still from
5085 * a previous call before cmdlines were created
5086 */
5087 if (next < pevent->cmdlines ||
5088 next >= pevent->cmdlines + pevent->cmdline_count)
5089 next = NULL;
5090 else
5091 cmdline = next++;
5092 }
5093
5094 if (!next)
5095 cmdline = pevent->cmdlines;
5096
5097 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5098 if (strcmp(cmdline->comm, comm) == 0)
5099 return cmdline;
5100 cmdline++;
5101 }
5102 return NULL;
5103 }
5104
5105 /**
5106 * pevent_cmdline_pid - return the pid associated to a given cmdline
5107 * @cmdline: The cmdline structure to get the pid from
5108 *
5109 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5110 * -1 is returned.
5111 */
5112 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5113 {
5114 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5115
5116 if (!cmdline)
5117 return -1;
5118
5119 /*
5120 * If cmdlines have not been created yet, or cmdline is
5121 * not part of the array, then treat it as a cmdlist instead.
5122 */
5123 if (!pevent->cmdlines ||
5124 cmdline < pevent->cmdlines ||
5125 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5126 return cmdlist->pid;
5127
5128 return cmdline->pid;
5129 }
5130
5131 /**
5132 * pevent_data_comm_from_pid - parse the data into the print format
5133 * @s: the trace_seq to write to
5134 * @event: the handle to the event
5135 * @record: the record to read from
5136 *
5137 * This parses the raw @data using the given @event information and
5138 * writes the print format into the trace_seq.
5139 */
5140 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5141 struct pevent_record *record)
5142 {
5143 int print_pretty = 1;
5144
5145 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5146 print_event_fields(s, record->data, record->size, event);
5147 else {
5148
5149 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5150 print_pretty = event->handler(s, record, event,
5151 event->context);
5152
5153 if (print_pretty)
5154 pretty_print(s, record->data, record->size, event);
5155 }
5156
5157 trace_seq_terminate(s);
5158 }
5159
5160 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5161 {
5162 if (!use_trace_clock)
5163 return true;
5164
5165 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5166 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5167 return true;
5168
5169 /* trace_clock is setting in tsc or counter mode */
5170 return false;
5171 }
5172
5173 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5174 struct pevent_record *record, bool use_trace_clock)
5175 {
5176 static const char *spaces = " "; /* 20 spaces */
5177 struct event_format *event;
5178 unsigned long secs;
5179 unsigned long usecs;
5180 unsigned long nsecs;
5181 const char *comm;
5182 void *data = record->data;
5183 int type;
5184 int pid;
5185 int len;
5186 int p;
5187 bool use_usec_format;
5188
5189 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5190 use_trace_clock);
5191 if (use_usec_format) {
5192 secs = record->ts / NSECS_PER_SEC;
5193 nsecs = record->ts - secs * NSECS_PER_SEC;
5194 }
5195
5196 if (record->size < 0) {
5197 do_warning("ug! negative record size %d", record->size);
5198 return;
5199 }
5200
5201 type = trace_parse_common_type(pevent, data);
5202
5203 event = pevent_find_event(pevent, type);
5204 if (!event) {
5205 do_warning("ug! no event found for type %d", type);
5206 return;
5207 }
5208
5209 pid = parse_common_pid(pevent, data);
5210 comm = find_cmdline(pevent, pid);
5211
5212 if (pevent->latency_format) {
5213 trace_seq_printf(s, "%8.8s-%-5d %3d",
5214 comm, pid, record->cpu);
5215 pevent_data_lat_fmt(pevent, s, record);
5216 } else
5217 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5218
5219 if (use_usec_format) {
5220 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5221 usecs = nsecs;
5222 p = 9;
5223 } else {
5224 usecs = (nsecs + 500) / NSECS_PER_USEC;
5225 p = 6;
5226 }
5227
5228 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5229 secs, p, usecs, event->name);
5230 } else
5231 trace_seq_printf(s, " %12llu: %s: ",
5232 record->ts, event->name);
5233
5234 /* Space out the event names evenly. */
5235 len = strlen(event->name);
5236 if (len < 20)
5237 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5238
5239 pevent_event_info(s, event, record);
5240 }
5241
5242 static int events_id_cmp(const void *a, const void *b)
5243 {
5244 struct event_format * const * ea = a;
5245 struct event_format * const * eb = b;
5246
5247 if ((*ea)->id < (*eb)->id)
5248 return -1;
5249
5250 if ((*ea)->id > (*eb)->id)
5251 return 1;
5252
5253 return 0;
5254 }
5255
5256 static int events_name_cmp(const void *a, const void *b)
5257 {
5258 struct event_format * const * ea = a;
5259 struct event_format * const * eb = b;
5260 int res;
5261
5262 res = strcmp((*ea)->name, (*eb)->name);
5263 if (res)
5264 return res;
5265
5266 res = strcmp((*ea)->system, (*eb)->system);
5267 if (res)
5268 return res;
5269
5270 return events_id_cmp(a, b);
5271 }
5272
5273 static int events_system_cmp(const void *a, const void *b)
5274 {
5275 struct event_format * const * ea = a;
5276 struct event_format * const * eb = b;
5277 int res;
5278
5279 res = strcmp((*ea)->system, (*eb)->system);
5280 if (res)
5281 return res;
5282
5283 res = strcmp((*ea)->name, (*eb)->name);
5284 if (res)
5285 return res;
5286
5287 return events_id_cmp(a, b);
5288 }
5289
5290 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5291 {
5292 struct event_format **events;
5293 int (*sort)(const void *a, const void *b);
5294
5295 events = pevent->sort_events;
5296
5297 if (events && pevent->last_type == sort_type)
5298 return events;
5299
5300 if (!events) {
5301 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5302 if (!events)
5303 return NULL;
5304
5305 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5306 events[pevent->nr_events] = NULL;
5307
5308 pevent->sort_events = events;
5309
5310 /* the internal events are sorted by id */
5311 if (sort_type == EVENT_SORT_ID) {
5312 pevent->last_type = sort_type;
5313 return events;
5314 }
5315 }
5316
5317 switch (sort_type) {
5318 case EVENT_SORT_ID:
5319 sort = events_id_cmp;
5320 break;
5321 case EVENT_SORT_NAME:
5322 sort = events_name_cmp;
5323 break;
5324 case EVENT_SORT_SYSTEM:
5325 sort = events_system_cmp;
5326 break;
5327 default:
5328 return events;
5329 }
5330
5331 qsort(events, pevent->nr_events, sizeof(*events), sort);
5332 pevent->last_type = sort_type;
5333
5334 return events;
5335 }
5336
5337 static struct format_field **
5338 get_event_fields(const char *type, const char *name,
5339 int count, struct format_field *list)
5340 {
5341 struct format_field **fields;
5342 struct format_field *field;
5343 int i = 0;
5344
5345 fields = malloc(sizeof(*fields) * (count + 1));
5346 if (!fields)
5347 return NULL;
5348
5349 for (field = list; field; field = field->next) {
5350 fields[i++] = field;
5351 if (i == count + 1) {
5352 do_warning("event %s has more %s fields than specified",
5353 name, type);
5354 i--;
5355 break;
5356 }
5357 }
5358
5359 if (i != count)
5360 do_warning("event %s has less %s fields than specified",
5361 name, type);
5362
5363 fields[i] = NULL;
5364
5365 return fields;
5366 }
5367
5368 /**
5369 * pevent_event_common_fields - return a list of common fields for an event
5370 * @event: the event to return the common fields of.
5371 *
5372 * Returns an allocated array of fields. The last item in the array is NULL.
5373 * The array must be freed with free().
5374 */
5375 struct format_field **pevent_event_common_fields(struct event_format *event)
5376 {
5377 return get_event_fields("common", event->name,
5378 event->format.nr_common,
5379 event->format.common_fields);
5380 }
5381
5382 /**
5383 * pevent_event_fields - return a list of event specific fields for an event
5384 * @event: the event to return the fields of.
5385 *
5386 * Returns an allocated array of fields. The last item in the array is NULL.
5387 * The array must be freed with free().
5388 */
5389 struct format_field **pevent_event_fields(struct event_format *event)
5390 {
5391 return get_event_fields("event", event->name,
5392 event->format.nr_fields,
5393 event->format.fields);
5394 }
5395
5396 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5397 {
5398 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5399 if (field->next) {
5400 trace_seq_puts(s, ", ");
5401 print_fields(s, field->next);
5402 }
5403 }
5404
5405 /* for debugging */
5406 static void print_args(struct print_arg *args)
5407 {
5408 int print_paren = 1;
5409 struct trace_seq s;
5410
5411 switch (args->type) {
5412 case PRINT_NULL:
5413 printf("null");
5414 break;
5415 case PRINT_ATOM:
5416 printf("%s", args->atom.atom);
5417 break;
5418 case PRINT_FIELD:
5419 printf("REC->%s", args->field.name);
5420 break;
5421 case PRINT_FLAGS:
5422 printf("__print_flags(");
5423 print_args(args->flags.field);
5424 printf(", %s, ", args->flags.delim);
5425 trace_seq_init(&s);
5426 print_fields(&s, args->flags.flags);
5427 trace_seq_do_printf(&s);
5428 trace_seq_destroy(&s);
5429 printf(")");
5430 break;
5431 case PRINT_SYMBOL:
5432 printf("__print_symbolic(");
5433 print_args(args->symbol.field);
5434 printf(", ");
5435 trace_seq_init(&s);
5436 print_fields(&s, args->symbol.symbols);
5437 trace_seq_do_printf(&s);
5438 trace_seq_destroy(&s);
5439 printf(")");
5440 break;
5441 case PRINT_HEX:
5442 printf("__print_hex(");
5443 print_args(args->hex.field);
5444 printf(", ");
5445 print_args(args->hex.size);
5446 printf(")");
5447 break;
5448 case PRINT_INT_ARRAY:
5449 printf("__print_array(");
5450 print_args(args->int_array.field);
5451 printf(", ");
5452 print_args(args->int_array.count);
5453 printf(", ");
5454 print_args(args->int_array.el_size);
5455 printf(")");
5456 break;
5457 case PRINT_STRING:
5458 case PRINT_BSTRING:
5459 printf("__get_str(%s)", args->string.string);
5460 break;
5461 case PRINT_BITMASK:
5462 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5463 break;
5464 case PRINT_TYPE:
5465 printf("(%s)", args->typecast.type);
5466 print_args(args->typecast.item);
5467 break;
5468 case PRINT_OP:
5469 if (strcmp(args->op.op, ":") == 0)
5470 print_paren = 0;
5471 if (print_paren)
5472 printf("(");
5473 print_args(args->op.left);
5474 printf(" %s ", args->op.op);
5475 print_args(args->op.right);
5476 if (print_paren)
5477 printf(")");
5478 break;
5479 default:
5480 /* we should warn... */
5481 return;
5482 }
5483 if (args->next) {
5484 printf("\n");
5485 print_args(args->next);
5486 }
5487 }
5488
5489 static void parse_header_field(const char *field,
5490 int *offset, int *size, int mandatory)
5491 {
5492 unsigned long long save_input_buf_ptr;
5493 unsigned long long save_input_buf_siz;
5494 char *token;
5495 int type;
5496
5497 save_input_buf_ptr = input_buf_ptr;
5498 save_input_buf_siz = input_buf_siz;
5499
5500 if (read_expected(EVENT_ITEM, "field") < 0)
5501 return;
5502 if (read_expected(EVENT_OP, ":") < 0)
5503 return;
5504
5505 /* type */
5506 if (read_expect_type(EVENT_ITEM, &token) < 0)
5507 goto fail;
5508 free_token(token);
5509
5510 /*
5511 * If this is not a mandatory field, then test it first.
5512 */
5513 if (mandatory) {
5514 if (read_expected(EVENT_ITEM, field) < 0)
5515 return;
5516 } else {
5517 if (read_expect_type(EVENT_ITEM, &token) < 0)
5518 goto fail;
5519 if (strcmp(token, field) != 0)
5520 goto discard;
5521 free_token(token);
5522 }
5523
5524 if (read_expected(EVENT_OP, ";") < 0)
5525 return;
5526 if (read_expected(EVENT_ITEM, "offset") < 0)
5527 return;
5528 if (read_expected(EVENT_OP, ":") < 0)
5529 return;
5530 if (read_expect_type(EVENT_ITEM, &token) < 0)
5531 goto fail;
5532 *offset = atoi(token);
5533 free_token(token);
5534 if (read_expected(EVENT_OP, ";") < 0)
5535 return;
5536 if (read_expected(EVENT_ITEM, "size") < 0)
5537 return;
5538 if (read_expected(EVENT_OP, ":") < 0)
5539 return;
5540 if (read_expect_type(EVENT_ITEM, &token) < 0)
5541 goto fail;
5542 *size = atoi(token);
5543 free_token(token);
5544 if (read_expected(EVENT_OP, ";") < 0)
5545 return;
5546 type = read_token(&token);
5547 if (type != EVENT_NEWLINE) {
5548 /* newer versions of the kernel have a "signed" type */
5549 if (type != EVENT_ITEM)
5550 goto fail;
5551
5552 if (strcmp(token, "signed") != 0)
5553 goto fail;
5554
5555 free_token(token);
5556
5557 if (read_expected(EVENT_OP, ":") < 0)
5558 return;
5559
5560 if (read_expect_type(EVENT_ITEM, &token))
5561 goto fail;
5562
5563 free_token(token);
5564 if (read_expected(EVENT_OP, ";") < 0)
5565 return;
5566
5567 if (read_expect_type(EVENT_NEWLINE, &token))
5568 goto fail;
5569 }
5570 fail:
5571 free_token(token);
5572 return;
5573
5574 discard:
5575 input_buf_ptr = save_input_buf_ptr;
5576 input_buf_siz = save_input_buf_siz;
5577 *offset = 0;
5578 *size = 0;
5579 free_token(token);
5580 }
5581
5582 /**
5583 * pevent_parse_header_page - parse the data stored in the header page
5584 * @pevent: the handle to the pevent
5585 * @buf: the buffer storing the header page format string
5586 * @size: the size of @buf
5587 * @long_size: the long size to use if there is no header
5588 *
5589 * This parses the header page format for information on the
5590 * ring buffer used. The @buf should be copied from
5591 *
5592 * /sys/kernel/debug/tracing/events/header_page
5593 */
5594 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5595 int long_size)
5596 {
5597 int ignore;
5598
5599 if (!size) {
5600 /*
5601 * Old kernels did not have header page info.
5602 * Sorry but we just use what we find here in user space.
5603 */
5604 pevent->header_page_ts_size = sizeof(long long);
5605 pevent->header_page_size_size = long_size;
5606 pevent->header_page_data_offset = sizeof(long long) + long_size;
5607 pevent->old_format = 1;
5608 return -1;
5609 }
5610 init_input_buf(buf, size);
5611
5612 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5613 &pevent->header_page_ts_size, 1);
5614 parse_header_field("commit", &pevent->header_page_size_offset,
5615 &pevent->header_page_size_size, 1);
5616 parse_header_field("overwrite", &pevent->header_page_overwrite,
5617 &ignore, 0);
5618 parse_header_field("data", &pevent->header_page_data_offset,
5619 &pevent->header_page_data_size, 1);
5620
5621 return 0;
5622 }
5623
5624 static int event_matches(struct event_format *event,
5625 int id, const char *sys_name,
5626 const char *event_name)
5627 {
5628 if (id >= 0 && id != event->id)
5629 return 0;
5630
5631 if (event_name && (strcmp(event_name, event->name) != 0))
5632 return 0;
5633
5634 if (sys_name && (strcmp(sys_name, event->system) != 0))
5635 return 0;
5636
5637 return 1;
5638 }
5639
5640 static void free_handler(struct event_handler *handle)
5641 {
5642 free((void *)handle->sys_name);
5643 free((void *)handle->event_name);
5644 free(handle);
5645 }
5646
5647 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5648 {
5649 struct event_handler *handle, **next;
5650
5651 for (next = &pevent->handlers; *next;
5652 next = &(*next)->next) {
5653 handle = *next;
5654 if (event_matches(event, handle->id,
5655 handle->sys_name,
5656 handle->event_name))
5657 break;
5658 }
5659
5660 if (!(*next))
5661 return 0;
5662
5663 pr_stat("overriding event (%d) %s:%s with new print handler",
5664 event->id, event->system, event->name);
5665
5666 event->handler = handle->func;
5667 event->context = handle->context;
5668
5669 *next = handle->next;
5670 free_handler(handle);
5671
5672 return 1;
5673 }
5674
5675 /**
5676 * __pevent_parse_format - parse the event format
5677 * @buf: the buffer storing the event format string
5678 * @size: the size of @buf
5679 * @sys: the system the event belongs to
5680 *
5681 * This parses the event format and creates an event structure
5682 * to quickly parse raw data for a given event.
5683 *
5684 * These files currently come from:
5685 *
5686 * /sys/kernel/debug/tracing/events/.../.../format
5687 */
5688 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5689 struct pevent *pevent, const char *buf,
5690 unsigned long size, const char *sys)
5691 {
5692 struct event_format *event;
5693 int ret;
5694
5695 init_input_buf(buf, size);
5696
5697 *eventp = event = alloc_event();
5698 if (!event)
5699 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5700
5701 event->name = event_read_name();
5702 if (!event->name) {
5703 /* Bad event? */
5704 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5705 goto event_alloc_failed;
5706 }
5707
5708 if (strcmp(sys, "ftrace") == 0) {
5709 event->flags |= EVENT_FL_ISFTRACE;
5710
5711 if (strcmp(event->name, "bprint") == 0)
5712 event->flags |= EVENT_FL_ISBPRINT;
5713 }
5714
5715 event->id = event_read_id();
5716 if (event->id < 0) {
5717 ret = PEVENT_ERRNO__READ_ID_FAILED;
5718 /*
5719 * This isn't an allocation error actually.
5720 * But as the ID is critical, just bail out.
5721 */
5722 goto event_alloc_failed;
5723 }
5724
5725 event->system = strdup(sys);
5726 if (!event->system) {
5727 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5728 goto event_alloc_failed;
5729 }
5730
5731 /* Add pevent to event so that it can be referenced */
5732 event->pevent = pevent;
5733
5734 ret = event_read_format(event);
5735 if (ret < 0) {
5736 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5737 goto event_parse_failed;
5738 }
5739
5740 /*
5741 * If the event has an override, don't print warnings if the event
5742 * print format fails to parse.
5743 */
5744 if (pevent && find_event_handle(pevent, event))
5745 show_warning = 0;
5746
5747 ret = event_read_print(event);
5748 show_warning = 1;
5749
5750 if (ret < 0) {
5751 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5752 goto event_parse_failed;
5753 }
5754
5755 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5756 struct format_field *field;
5757 struct print_arg *arg, **list;
5758
5759 /* old ftrace had no args */
5760 list = &event->print_fmt.args;
5761 for (field = event->format.fields; field; field = field->next) {
5762 arg = alloc_arg();
5763 if (!arg) {
5764 event->flags |= EVENT_FL_FAILED;
5765 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5766 }
5767 arg->type = PRINT_FIELD;
5768 arg->field.name = strdup(field->name);
5769 if (!arg->field.name) {
5770 event->flags |= EVENT_FL_FAILED;
5771 free_arg(arg);
5772 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5773 }
5774 arg->field.field = field;
5775 *list = arg;
5776 list = &arg->next;
5777 }
5778 return 0;
5779 }
5780
5781 return 0;
5782
5783 event_parse_failed:
5784 event->flags |= EVENT_FL_FAILED;
5785 return ret;
5786
5787 event_alloc_failed:
5788 free(event->system);
5789 free(event->name);
5790 free(event);
5791 *eventp = NULL;
5792 return ret;
5793 }
5794
5795 static enum pevent_errno
5796 __pevent_parse_event(struct pevent *pevent,
5797 struct event_format **eventp,
5798 const char *buf, unsigned long size,
5799 const char *sys)
5800 {
5801 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5802 struct event_format *event = *eventp;
5803
5804 if (event == NULL)
5805 return ret;
5806
5807 if (pevent && add_event(pevent, event)) {
5808 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5809 goto event_add_failed;
5810 }
5811
5812 #define PRINT_ARGS 0
5813 if (PRINT_ARGS && event->print_fmt.args)
5814 print_args(event->print_fmt.args);
5815
5816 return 0;
5817
5818 event_add_failed:
5819 pevent_free_format(event);
5820 return ret;
5821 }
5822
5823 /**
5824 * pevent_parse_format - parse the event format
5825 * @pevent: the handle to the pevent
5826 * @eventp: returned format
5827 * @buf: the buffer storing the event format string
5828 * @size: the size of @buf
5829 * @sys: the system the event belongs to
5830 *
5831 * This parses the event format and creates an event structure
5832 * to quickly parse raw data for a given event.
5833 *
5834 * These files currently come from:
5835 *
5836 * /sys/kernel/debug/tracing/events/.../.../format
5837 */
5838 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5839 struct event_format **eventp,
5840 const char *buf,
5841 unsigned long size, const char *sys)
5842 {
5843 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5844 }
5845
5846 /**
5847 * pevent_parse_event - parse the event format
5848 * @pevent: the handle to the pevent
5849 * @buf: the buffer storing the event format string
5850 * @size: the size of @buf
5851 * @sys: the system the event belongs to
5852 *
5853 * This parses the event format and creates an event structure
5854 * to quickly parse raw data for a given event.
5855 *
5856 * These files currently come from:
5857 *
5858 * /sys/kernel/debug/tracing/events/.../.../format
5859 */
5860 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5861 unsigned long size, const char *sys)
5862 {
5863 struct event_format *event = NULL;
5864 return __pevent_parse_event(pevent, &event, buf, size, sys);
5865 }
5866
5867 #undef _PE
5868 #define _PE(code, str) str
5869 static const char * const pevent_error_str[] = {
5870 PEVENT_ERRORS
5871 };
5872 #undef _PE
5873
5874 int pevent_strerror(struct pevent *pevent __maybe_unused,
5875 enum pevent_errno errnum, char *buf, size_t buflen)
5876 {
5877 int idx;
5878 const char *msg;
5879
5880 if (errnum >= 0) {
5881 msg = strerror_r(errnum, buf, buflen);
5882 if (msg != buf) {
5883 size_t len = strlen(msg);
5884 memcpy(buf, msg, min(buflen - 1, len));
5885 *(buf + min(buflen - 1, len)) = '\0';
5886 }
5887 return 0;
5888 }
5889
5890 if (errnum <= __PEVENT_ERRNO__START ||
5891 errnum >= __PEVENT_ERRNO__END)
5892 return -1;
5893
5894 idx = errnum - __PEVENT_ERRNO__START - 1;
5895 msg = pevent_error_str[idx];
5896 snprintf(buf, buflen, "%s", msg);
5897
5898 return 0;
5899 }
5900
5901 int get_field_val(struct trace_seq *s, struct format_field *field,
5902 const char *name, struct pevent_record *record,
5903 unsigned long long *val, int err)
5904 {
5905 if (!field) {
5906 if (err)
5907 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5908 return -1;
5909 }
5910
5911 if (pevent_read_number_field(field, record->data, val)) {
5912 if (err)
5913 trace_seq_printf(s, " %s=INVALID", name);
5914 return -1;
5915 }
5916
5917 return 0;
5918 }
5919
5920 /**
5921 * pevent_get_field_raw - return the raw pointer into the data field
5922 * @s: The seq to print to on error
5923 * @event: the event that the field is for
5924 * @name: The name of the field
5925 * @record: The record with the field name.
5926 * @len: place to store the field length.
5927 * @err: print default error if failed.
5928 *
5929 * Returns a pointer into record->data of the field and places
5930 * the length of the field in @len.
5931 *
5932 * On failure, it returns NULL.
5933 */
5934 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5935 const char *name, struct pevent_record *record,
5936 int *len, int err)
5937 {
5938 struct format_field *field;
5939 void *data = record->data;
5940 unsigned offset;
5941 int dummy;
5942
5943 if (!event)
5944 return NULL;
5945
5946 field = pevent_find_field(event, name);
5947
5948 if (!field) {
5949 if (err)
5950 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5951 return NULL;
5952 }
5953
5954 /* Allow @len to be NULL */
5955 if (!len)
5956 len = &dummy;
5957
5958 offset = field->offset;
5959 if (field->flags & FIELD_IS_DYNAMIC) {
5960 offset = pevent_read_number(event->pevent,
5961 data + offset, field->size);
5962 *len = offset >> 16;
5963 offset &= 0xffff;
5964 } else
5965 *len = field->size;
5966
5967 return data + offset;
5968 }
5969
5970 /**
5971 * pevent_get_field_val - find a field and return its value
5972 * @s: The seq to print to on error
5973 * @event: the event that the field is for
5974 * @name: The name of the field
5975 * @record: The record with the field name.
5976 * @val: place to store the value of the field.
5977 * @err: print default error if failed.
5978 *
5979 * Returns 0 on success -1 on field not found.
5980 */
5981 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5982 const char *name, struct pevent_record *record,
5983 unsigned long long *val, int err)
5984 {
5985 struct format_field *field;
5986
5987 if (!event)
5988 return -1;
5989
5990 field = pevent_find_field(event, name);
5991
5992 return get_field_val(s, field, name, record, val, err);
5993 }
5994
5995 /**
5996 * pevent_get_common_field_val - find a common field and return its value
5997 * @s: The seq to print to on error
5998 * @event: the event that the field is for
5999 * @name: The name of the field
6000 * @record: The record with the field name.
6001 * @val: place to store the value of the field.
6002 * @err: print default error if failed.
6003 *
6004 * Returns 0 on success -1 on field not found.
6005 */
6006 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6007 const char *name, struct pevent_record *record,
6008 unsigned long long *val, int err)
6009 {
6010 struct format_field *field;
6011
6012 if (!event)
6013 return -1;
6014
6015 field = pevent_find_common_field(event, name);
6016
6017 return get_field_val(s, field, name, record, val, err);
6018 }
6019
6020 /**
6021 * pevent_get_any_field_val - find a any field and return its value
6022 * @s: The seq to print to on error
6023 * @event: the event that the field is for
6024 * @name: The name of the field
6025 * @record: The record with the field name.
6026 * @val: place to store the value of the field.
6027 * @err: print default error if failed.
6028 *
6029 * Returns 0 on success -1 on field not found.
6030 */
6031 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6032 const char *name, struct pevent_record *record,
6033 unsigned long long *val, int err)
6034 {
6035 struct format_field *field;
6036
6037 if (!event)
6038 return -1;
6039
6040 field = pevent_find_any_field(event, name);
6041
6042 return get_field_val(s, field, name, record, val, err);
6043 }
6044
6045 /**
6046 * pevent_print_num_field - print a field and a format
6047 * @s: The seq to print to
6048 * @fmt: The printf format to print the field with.
6049 * @event: the event that the field is for
6050 * @name: The name of the field
6051 * @record: The record with the field name.
6052 * @err: print default error if failed.
6053 *
6054 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6055 */
6056 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6057 struct event_format *event, const char *name,
6058 struct pevent_record *record, int err)
6059 {
6060 struct format_field *field = pevent_find_field(event, name);
6061 unsigned long long val;
6062
6063 if (!field)
6064 goto failed;
6065
6066 if (pevent_read_number_field(field, record->data, &val))
6067 goto failed;
6068
6069 return trace_seq_printf(s, fmt, val);
6070
6071 failed:
6072 if (err)
6073 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6074 return -1;
6075 }
6076
6077 /**
6078 * pevent_print_func_field - print a field and a format for function pointers
6079 * @s: The seq to print to
6080 * @fmt: The printf format to print the field with.
6081 * @event: the event that the field is for
6082 * @name: The name of the field
6083 * @record: The record with the field name.
6084 * @err: print default error if failed.
6085 *
6086 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6087 */
6088 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6089 struct event_format *event, const char *name,
6090 struct pevent_record *record, int err)
6091 {
6092 struct format_field *field = pevent_find_field(event, name);
6093 struct pevent *pevent = event->pevent;
6094 unsigned long long val;
6095 struct func_map *func;
6096 char tmp[128];
6097
6098 if (!field)
6099 goto failed;
6100
6101 if (pevent_read_number_field(field, record->data, &val))
6102 goto failed;
6103
6104 func = find_func(pevent, val);
6105
6106 if (func)
6107 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6108 else
6109 sprintf(tmp, "0x%08llx", val);
6110
6111 return trace_seq_printf(s, fmt, tmp);
6112
6113 failed:
6114 if (err)
6115 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6116 return -1;
6117 }
6118
6119 static void free_func_handle(struct pevent_function_handler *func)
6120 {
6121 struct pevent_func_params *params;
6122
6123 free(func->name);
6124
6125 while (func->params) {
6126 params = func->params;
6127 func->params = params->next;
6128 free(params);
6129 }
6130
6131 free(func);
6132 }
6133
6134 /**
6135 * pevent_register_print_function - register a helper function
6136 * @pevent: the handle to the pevent
6137 * @func: the function to process the helper function
6138 * @ret_type: the return type of the helper function
6139 * @name: the name of the helper function
6140 * @parameters: A list of enum pevent_func_arg_type
6141 *
6142 * Some events may have helper functions in the print format arguments.
6143 * This allows a plugin to dynamically create a way to process one
6144 * of these functions.
6145 *
6146 * The @parameters is a variable list of pevent_func_arg_type enums that
6147 * must end with PEVENT_FUNC_ARG_VOID.
6148 */
6149 int pevent_register_print_function(struct pevent *pevent,
6150 pevent_func_handler func,
6151 enum pevent_func_arg_type ret_type,
6152 char *name, ...)
6153 {
6154 struct pevent_function_handler *func_handle;
6155 struct pevent_func_params **next_param;
6156 struct pevent_func_params *param;
6157 enum pevent_func_arg_type type;
6158 va_list ap;
6159 int ret;
6160
6161 func_handle = find_func_handler(pevent, name);
6162 if (func_handle) {
6163 /*
6164 * This is most like caused by the users own
6165 * plugins updating the function. This overrides the
6166 * system defaults.
6167 */
6168 pr_stat("override of function helper '%s'", name);
6169 remove_func_handler(pevent, name);
6170 }
6171
6172 func_handle = calloc(1, sizeof(*func_handle));
6173 if (!func_handle) {
6174 do_warning("Failed to allocate function handler");
6175 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6176 }
6177
6178 func_handle->ret_type = ret_type;
6179 func_handle->name = strdup(name);
6180 func_handle->func = func;
6181 if (!func_handle->name) {
6182 do_warning("Failed to allocate function name");
6183 free(func_handle);
6184 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6185 }
6186
6187 next_param = &(func_handle->params);
6188 va_start(ap, name);
6189 for (;;) {
6190 type = va_arg(ap, enum pevent_func_arg_type);
6191 if (type == PEVENT_FUNC_ARG_VOID)
6192 break;
6193
6194 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6195 do_warning("Invalid argument type %d", type);
6196 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6197 goto out_free;
6198 }
6199
6200 param = malloc(sizeof(*param));
6201 if (!param) {
6202 do_warning("Failed to allocate function param");
6203 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6204 goto out_free;
6205 }
6206 param->type = type;
6207 param->next = NULL;
6208
6209 *next_param = param;
6210 next_param = &(param->next);
6211
6212 func_handle->nr_args++;
6213 }
6214 va_end(ap);
6215
6216 func_handle->next = pevent->func_handlers;
6217 pevent->func_handlers = func_handle;
6218
6219 return 0;
6220 out_free:
6221 va_end(ap);
6222 free_func_handle(func_handle);
6223 return ret;
6224 }
6225
6226 /**
6227 * pevent_unregister_print_function - unregister a helper function
6228 * @pevent: the handle to the pevent
6229 * @func: the function to process the helper function
6230 * @name: the name of the helper function
6231 *
6232 * This function removes existing print handler for function @name.
6233 *
6234 * Returns 0 if the handler was removed successully, -1 otherwise.
6235 */
6236 int pevent_unregister_print_function(struct pevent *pevent,
6237 pevent_func_handler func, char *name)
6238 {
6239 struct pevent_function_handler *func_handle;
6240
6241 func_handle = find_func_handler(pevent, name);
6242 if (func_handle && func_handle->func == func) {
6243 remove_func_handler(pevent, name);
6244 return 0;
6245 }
6246 return -1;
6247 }
6248
6249 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6250 const char *sys_name,
6251 const char *event_name)
6252 {
6253 struct event_format *event;
6254
6255 if (id >= 0) {
6256 /* search by id */
6257 event = pevent_find_event(pevent, id);
6258 if (!event)
6259 return NULL;
6260 if (event_name && (strcmp(event_name, event->name) != 0))
6261 return NULL;
6262 if (sys_name && (strcmp(sys_name, event->system) != 0))
6263 return NULL;
6264 } else {
6265 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6266 if (!event)
6267 return NULL;
6268 }
6269 return event;
6270 }
6271
6272 /**
6273 * pevent_register_event_handler - register a way to parse an event
6274 * @pevent: the handle to the pevent
6275 * @id: the id of the event to register
6276 * @sys_name: the system name the event belongs to
6277 * @event_name: the name of the event
6278 * @func: the function to call to parse the event information
6279 * @context: the data to be passed to @func
6280 *
6281 * This function allows a developer to override the parsing of
6282 * a given event. If for some reason the default print format
6283 * is not sufficient, this function will register a function
6284 * for an event to be used to parse the data instead.
6285 *
6286 * If @id is >= 0, then it is used to find the event.
6287 * else @sys_name and @event_name are used.
6288 */
6289 int pevent_register_event_handler(struct pevent *pevent, int id,
6290 const char *sys_name, const char *event_name,
6291 pevent_event_handler_func func, void *context)
6292 {
6293 struct event_format *event;
6294 struct event_handler *handle;
6295
6296 event = pevent_search_event(pevent, id, sys_name, event_name);
6297 if (event == NULL)
6298 goto not_found;
6299
6300 pr_stat("overriding event (%d) %s:%s with new print handler",
6301 event->id, event->system, event->name);
6302
6303 event->handler = func;
6304 event->context = context;
6305 return 0;
6306
6307 not_found:
6308 /* Save for later use. */
6309 handle = calloc(1, sizeof(*handle));
6310 if (!handle) {
6311 do_warning("Failed to allocate event handler");
6312 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6313 }
6314
6315 handle->id = id;
6316 if (event_name)
6317 handle->event_name = strdup(event_name);
6318 if (sys_name)
6319 handle->sys_name = strdup(sys_name);
6320
6321 if ((event_name && !handle->event_name) ||
6322 (sys_name && !handle->sys_name)) {
6323 do_warning("Failed to allocate event/sys name");
6324 free((void *)handle->event_name);
6325 free((void *)handle->sys_name);
6326 free(handle);
6327 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6328 }
6329
6330 handle->func = func;
6331 handle->next = pevent->handlers;
6332 pevent->handlers = handle;
6333 handle->context = context;
6334
6335 return -1;
6336 }
6337
6338 static int handle_matches(struct event_handler *handler, int id,
6339 const char *sys_name, const char *event_name,
6340 pevent_event_handler_func func, void *context)
6341 {
6342 if (id >= 0 && id != handler->id)
6343 return 0;
6344
6345 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6346 return 0;
6347
6348 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6349 return 0;
6350
6351 if (func != handler->func || context != handler->context)
6352 return 0;
6353
6354 return 1;
6355 }
6356
6357 /**
6358 * pevent_unregister_event_handler - unregister an existing event handler
6359 * @pevent: the handle to the pevent
6360 * @id: the id of the event to unregister
6361 * @sys_name: the system name the handler belongs to
6362 * @event_name: the name of the event handler
6363 * @func: the function to call to parse the event information
6364 * @context: the data to be passed to @func
6365 *
6366 * This function removes existing event handler (parser).
6367 *
6368 * If @id is >= 0, then it is used to find the event.
6369 * else @sys_name and @event_name are used.
6370 *
6371 * Returns 0 if handler was removed successfully, -1 if event was not found.
6372 */
6373 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6374 const char *sys_name, const char *event_name,
6375 pevent_event_handler_func func, void *context)
6376 {
6377 struct event_format *event;
6378 struct event_handler *handle;
6379 struct event_handler **next;
6380
6381 event = pevent_search_event(pevent, id, sys_name, event_name);
6382 if (event == NULL)
6383 goto not_found;
6384
6385 if (event->handler == func && event->context == context) {
6386 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6387 event->id, event->system, event->name);
6388
6389 event->handler = NULL;
6390 event->context = NULL;
6391 return 0;
6392 }
6393
6394 not_found:
6395 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6396 handle = *next;
6397 if (handle_matches(handle, id, sys_name, event_name,
6398 func, context))
6399 break;
6400 }
6401
6402 if (!(*next))
6403 return -1;
6404
6405 *next = handle->next;
6406 free_handler(handle);
6407
6408 return 0;
6409 }
6410
6411 /**
6412 * pevent_alloc - create a pevent handle
6413 */
6414 struct pevent *pevent_alloc(void)
6415 {
6416 struct pevent *pevent = calloc(1, sizeof(*pevent));
6417
6418 if (pevent)
6419 pevent->ref_count = 1;
6420
6421 return pevent;
6422 }
6423
6424 void pevent_ref(struct pevent *pevent)
6425 {
6426 pevent->ref_count++;
6427 }
6428
6429 void pevent_free_format_field(struct format_field *field)
6430 {
6431 free(field->type);
6432 free(field->name);
6433 free(field);
6434 }
6435
6436 static void free_format_fields(struct format_field *field)
6437 {
6438 struct format_field *next;
6439
6440 while (field) {
6441 next = field->next;
6442 pevent_free_format_field(field);
6443 field = next;
6444 }
6445 }
6446
6447 static void free_formats(struct format *format)
6448 {
6449 free_format_fields(format->common_fields);
6450 free_format_fields(format->fields);
6451 }
6452
6453 void pevent_free_format(struct event_format *event)
6454 {
6455 free(event->name);
6456 free(event->system);
6457
6458 free_formats(&event->format);
6459
6460 free(event->print_fmt.format);
6461 free_args(event->print_fmt.args);
6462
6463 free(event);
6464 }
6465
6466 /**
6467 * pevent_free - free a pevent handle
6468 * @pevent: the pevent handle to free
6469 */
6470 void pevent_free(struct pevent *pevent)
6471 {
6472 struct cmdline_list *cmdlist, *cmdnext;
6473 struct func_list *funclist, *funcnext;
6474 struct printk_list *printklist, *printknext;
6475 struct pevent_function_handler *func_handler;
6476 struct event_handler *handle;
6477 int i;
6478
6479 if (!pevent)
6480 return;
6481
6482 cmdlist = pevent->cmdlist;
6483 funclist = pevent->funclist;
6484 printklist = pevent->printklist;
6485
6486 pevent->ref_count--;
6487 if (pevent->ref_count)
6488 return;
6489
6490 if (pevent->cmdlines) {
6491 for (i = 0; i < pevent->cmdline_count; i++)
6492 free(pevent->cmdlines[i].comm);
6493 free(pevent->cmdlines);
6494 }
6495
6496 while (cmdlist) {
6497 cmdnext = cmdlist->next;
6498 free(cmdlist->comm);
6499 free(cmdlist);
6500 cmdlist = cmdnext;
6501 }
6502
6503 if (pevent->func_map) {
6504 for (i = 0; i < (int)pevent->func_count; i++) {
6505 free(pevent->func_map[i].func);
6506 free(pevent->func_map[i].mod);
6507 }
6508 free(pevent->func_map);
6509 }
6510
6511 while (funclist) {
6512 funcnext = funclist->next;
6513 free(funclist->func);
6514 free(funclist->mod);
6515 free(funclist);
6516 funclist = funcnext;
6517 }
6518
6519 while (pevent->func_handlers) {
6520 func_handler = pevent->func_handlers;
6521 pevent->func_handlers = func_handler->next;
6522 free_func_handle(func_handler);
6523 }
6524
6525 if (pevent->printk_map) {
6526 for (i = 0; i < (int)pevent->printk_count; i++)
6527 free(pevent->printk_map[i].printk);
6528 free(pevent->printk_map);
6529 }
6530
6531 while (printklist) {
6532 printknext = printklist->next;
6533 free(printklist->printk);
6534 free(printklist);
6535 printklist = printknext;
6536 }
6537
6538 for (i = 0; i < pevent->nr_events; i++)
6539 pevent_free_format(pevent->events[i]);
6540
6541 while (pevent->handlers) {
6542 handle = pevent->handlers;
6543 pevent->handlers = handle->next;
6544 free_handler(handle);
6545 }
6546
6547 free(pevent->trace_clock);
6548 free(pevent->events);
6549 free(pevent->sort_events);
6550
6551 free(pevent);
6552 }
6553
6554 void pevent_unref(struct pevent *pevent)
6555 {
6556 pevent_free(pevent);
6557 }
This page took 0.169394 seconds and 5 git commands to generate.