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