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