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