1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 *
5 *
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
11 */
12 #include <inttypes.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <unistd.h>
21 #include <limits.h>
22 #include <linux/time64.h>
23
24 #include <netinet/in.h>
25 #include "event-parse.h"
26
27 #include "event-parse-local.h"
28 #include "event-utils.h"
29 #include "trace-seq.h"
30
31 static int is_flag_field;
32 static int is_symbolic_field;
33
34 static int show_warning = 1;
35
36 #define do_warning(fmt, ...) \
37 do { \
38 if (show_warning) \
39 tep_warning(fmt, ##__VA_ARGS__);\
40 } while (0)
41
42 #define do_warning_event(event, fmt, ...) \
43 do { \
44 if (!show_warning) \
45 continue; \
46 \
47 if (event) \
48 tep_warning("[%s:%s] " fmt, event->system, \
49 event->name, ##__VA_ARGS__); \
50 else \
51 tep_warning(fmt, ##__VA_ARGS__); \
52 } while (0)
53
54 /**
55 * init_input_buf - init buffer for parsing
56 * @buf: buffer to parse
57 * @size: the size of the buffer
58 *
59 * Initializes the internal buffer that tep_read_token() will parse.
60 */
init_input_buf(struct tep_handle * tep,const char * buf,unsigned long long size)61 __hidden void init_input_buf(struct tep_handle *tep, const char *buf,
62 unsigned long long size)
63 {
64 tep->input_buf = buf;
65 tep->input_buf_siz = size;
66 tep->input_buf_ptr = 0;
67 }
68
get_input_buf(struct tep_handle * tep)69 __hidden const char *get_input_buf(struct tep_handle *tep)
70 {
71 return tep->input_buf;
72 }
73
get_input_buf_ptr(struct tep_handle * tep)74 __hidden unsigned long long get_input_buf_ptr(struct tep_handle *tep)
75 {
76 return tep->input_buf_ptr;
77 }
78
79 struct event_handler {
80 struct event_handler *next;
81 int id;
82 const char *sys_name;
83 const char *event_name;
84 tep_event_handler_func func;
85 void *context;
86 };
87
88 struct func_params {
89 struct func_params *next;
90 enum tep_func_arg_type type;
91 };
92
93 struct tep_function_handler {
94 struct tep_function_handler *next;
95 enum tep_func_arg_type ret_type;
96 char *name;
97 tep_func_handler func;
98 struct func_params *params;
99 int nr_args;
100 };
101
102 static unsigned long long
103 process_defined_func(struct trace_seq *s, void *data, int size,
104 struct tep_event *event, struct tep_print_arg *arg);
105
106 static void free_func_handle(struct tep_function_handler *func);
107
breakpoint(void)108 void breakpoint(void)
109 {
110 static int x;
111 x++;
112 }
113
get_event_type(enum tep_event_type type)114 static const char *get_event_type(enum tep_event_type type)
115 {
116 switch (type) {
117 case TEP_EVENT_ERROR: return "ERROR";
118 case TEP_EVENT_NONE: return "NONE";
119 case TEP_EVENT_SPACE: return "SPACE";
120 case TEP_EVENT_NEWLINE: return "NEWLINE";
121 case TEP_EVENT_OP: return "OP";
122 case TEP_EVENT_DELIM: return "DELIM";
123 case TEP_EVENT_ITEM: return "ITEM";
124 case TEP_EVENT_DQUOTE: return "DQUOTE";
125 case TEP_EVENT_SQUOTE: return "SQUOTE";
126 }
127 return "(UNKNOWN)";
128 }
129
alloc_arg(void)130 static struct tep_print_arg *alloc_arg(void)
131 {
132 return calloc(1, sizeof(struct tep_print_arg));
133 }
134
135 struct tep_cmdline {
136 char *comm;
137 int pid;
138 };
139
cmdline_cmp(const void * a,const void * b)140 static int cmdline_cmp(const void *a, const void *b)
141 {
142 const struct tep_cmdline *ca = a;
143 const struct tep_cmdline *cb = b;
144
145 if (ca->pid < cb->pid)
146 return -1;
147 if (ca->pid > cb->pid)
148 return 1;
149
150 return 0;
151 }
152
153 /* Looking for where to place the key */
cmdline_slot_cmp(const void * a,const void * b)154 static int cmdline_slot_cmp(const void *a, const void *b)
155 {
156 const struct tep_cmdline *ca = a;
157 const struct tep_cmdline *cb = b;
158 const struct tep_cmdline *cb1 = cb + 1;
159
160 if (ca->pid < cb->pid)
161 return -1;
162
163 if (ca->pid > cb->pid) {
164 if (ca->pid <= cb1->pid)
165 return 0;
166 return 1;
167 }
168
169 return 0;
170 }
171
172 struct cmdline_list {
173 struct cmdline_list *next;
174 char *comm;
175 int pid;
176 };
177
cmdline_init(struct tep_handle * tep)178 static int cmdline_init(struct tep_handle *tep)
179 {
180 struct cmdline_list *cmdlist = tep->cmdlist;
181 struct cmdline_list *item;
182 struct tep_cmdline *cmdlines;
183 int i;
184
185 cmdlines = malloc(sizeof(*cmdlines) * tep->cmdline_count);
186 if (!cmdlines)
187 return -1;
188
189 i = 0;
190 while (cmdlist) {
191 cmdlines[i].pid = cmdlist->pid;
192 cmdlines[i].comm = cmdlist->comm;
193 i++;
194 item = cmdlist;
195 cmdlist = cmdlist->next;
196 free(item);
197 }
198
199 qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
200
201 tep->cmdlines = cmdlines;
202 tep->cmdlist = NULL;
203
204 return 0;
205 }
206
find_cmdline(struct tep_handle * tep,int pid)207 static const char *find_cmdline(struct tep_handle *tep, int pid)
208 {
209 const struct tep_cmdline *comm;
210 struct tep_cmdline key;
211
212 if (!pid)
213 return "<idle>";
214
215 if (!tep->cmdlines && cmdline_init(tep))
216 return "<not enough memory for cmdlines!>";
217
218 key.pid = pid;
219
220 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
221 sizeof(*tep->cmdlines), cmdline_cmp);
222
223 if (comm)
224 return comm->comm;
225 return "<...>";
226 }
227
228 /**
229 * tep_is_pid_registered - return if a pid has a cmdline registered
230 * @tep: a handle to the trace event parser context
231 * @pid: The pid to check if it has a cmdline registered with.
232 *
233 * Returns true if the pid has a cmdline mapped to it
234 * false otherwise.
235 */
tep_is_pid_registered(struct tep_handle * tep,int pid)236 bool tep_is_pid_registered(struct tep_handle *tep, int pid)
237 {
238 const struct tep_cmdline *comm;
239 struct tep_cmdline key;
240
241 if (!pid)
242 return true;
243
244 if (!tep->cmdlines && cmdline_init(tep))
245 return false;
246
247 key.pid = pid;
248
249 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
250 sizeof(*tep->cmdlines), cmdline_cmp);
251
252 if (comm)
253 return true;
254 return false;
255 }
256
257 /*
258 * If the command lines have been converted to an array, then
259 * we must add this pid. This is much slower than when cmdlines
260 * are added before the array is initialized.
261 */
add_new_comm(struct tep_handle * tep,const char * comm,int pid,bool override)262 static int add_new_comm(struct tep_handle *tep,
263 const char *comm, int pid, bool override)
264 {
265 struct tep_cmdline *cmdlines = tep->cmdlines;
266 struct tep_cmdline *cmdline;
267 struct tep_cmdline key;
268 char *new_comm;
269 int cnt;
270
271 if (!pid)
272 return 0;
273
274 /* avoid duplicates */
275 key.pid = pid;
276
277 cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count,
278 sizeof(*tep->cmdlines), cmdline_cmp);
279 if (cmdline) {
280 if (!override) {
281 errno = EEXIST;
282 return -1;
283 }
284 new_comm = strdup(comm);
285 if (!new_comm) {
286 errno = ENOMEM;
287 return -1;
288 }
289 free(cmdline->comm);
290 cmdline->comm = new_comm;
291
292 return 0;
293 }
294
295 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1));
296 if (!cmdlines) {
297 errno = ENOMEM;
298 return -1;
299 }
300 tep->cmdlines = cmdlines;
301
302 key.comm = strdup(comm);
303 if (!key.comm) {
304 errno = ENOMEM;
305 return -1;
306 }
307
308 if (!tep->cmdline_count) {
309 /* no entries yet */
310 tep->cmdlines[0] = key;
311 tep->cmdline_count++;
312 return 0;
313 }
314
315 /* Now find where we want to store the new cmdline */
316 cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count - 1,
317 sizeof(*tep->cmdlines), cmdline_slot_cmp);
318
319 cnt = tep->cmdline_count;
320 if (cmdline) {
321 /* cmdline points to the one before the spot we want */
322 cmdline++;
323 cnt -= cmdline - tep->cmdlines;
324
325 } else {
326 /* The new entry is either before or after the list */
327 if (key.pid > tep->cmdlines[tep->cmdline_count - 1].pid) {
328 tep->cmdlines[tep->cmdline_count++] = key;
329 return 0;
330 }
331 cmdline = &tep->cmdlines[0];
332 }
333 memmove(cmdline + 1, cmdline, (cnt * sizeof(*cmdline)));
334 *cmdline = key;
335
336 tep->cmdline_count++;
337
338 return 0;
339 }
340
_tep_register_comm(struct tep_handle * tep,const char * comm,int pid,bool override)341 static int _tep_register_comm(struct tep_handle *tep,
342 const char *comm, int pid, bool override)
343 {
344 struct cmdline_list *item;
345
346 if (tep->cmdlines)
347 return add_new_comm(tep, comm, pid, override);
348
349 item = malloc(sizeof(*item));
350 if (!item)
351 return -1;
352
353 if (comm)
354 item->comm = strdup(comm);
355 else
356 item->comm = strdup("<...>");
357 if (!item->comm) {
358 free(item);
359 return -1;
360 }
361 item->pid = pid;
362 item->next = tep->cmdlist;
363
364 tep->cmdlist = item;
365 tep->cmdline_count++;
366
367 return 0;
368 }
369
370 /**
371 * tep_register_comm - register a pid / comm mapping
372 * @tep: a handle to the trace event parser context
373 * @comm: the command line to register
374 * @pid: the pid to map the command line to
375 *
376 * This adds a mapping to search for command line names with
377 * a given pid. The comm is duplicated. If a command with the same pid
378 * already exist, -1 is returned and errno is set to EEXIST
379 */
tep_register_comm(struct tep_handle * tep,const char * comm,int pid)380 int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
381 {
382 return _tep_register_comm(tep, comm, pid, false);
383 }
384
385 /**
386 * tep_override_comm - register a pid / comm mapping
387 * @tep: a handle to the trace event parser context
388 * @comm: the command line to register
389 * @pid: the pid to map the command line to
390 *
391 * This adds a mapping to search for command line names with
392 * a given pid. The comm is duplicated. If a command with the same pid
393 * already exist, the command string is udapted with the new one
394 */
tep_override_comm(struct tep_handle * tep,const char * comm,int pid)395 int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
396 {
397 if (!tep->cmdlines && cmdline_init(tep)) {
398 errno = ENOMEM;
399 return -1;
400 }
401 return _tep_register_comm(tep, comm, pid, true);
402 }
403
404 /**
405 * tep_parse_saved_cmdlines - parse the comms from the saved_cmdlines file
406 * @tep: a handle to the trace event parser
407 * @buf: A string buffer that holds the content of saved_cmdlines and ends with '\0'
408 *
409 * This is a helper function to parse the comms in the tracefs saved_cmdlines
410 * file (stored in a string buffer) and load the comms into the @tep handler
411 * such that comm name matches an process ID (pid). This is used to show
412 * the names of the processes as the events only hold the pid.
413 *
414 * Returns 0 on success, and -1 on error.
415 */
tep_parse_saved_cmdlines(struct tep_handle * tep,const char * buf)416 int tep_parse_saved_cmdlines(struct tep_handle *tep, const char *buf)
417 {
418 char *copy;
419 char *comm;
420 char *line;
421 char *next = NULL;
422 int pid;
423 int ret = -1;
424 int n;
425
426 copy = strdup(buf);
427 if (!copy)
428 return -1;
429
430 line = strtok_r(copy, "\n", &next);
431 while (line) {
432 errno = 0;
433 n = sscanf(line, "%d %m[^\n]s", &pid, &comm);
434 if (errno || n != 2 || !comm)
435 goto out;
436 tep_register_comm(tep, comm, pid);
437 free(comm);
438 line = strtok_r(NULL, "\n", &next);
439 }
440 ret = 0;
441 out:
442 free(copy);
443 return ret;
444 }
445
446 struct func_map {
447 unsigned long long addr;
448 char *func;
449 char *mod;
450 };
451
452 struct func_list {
453 struct func_list *next;
454 unsigned long long addr;
455 char *func;
456 char *mod;
457 };
458
func_cmp(const void * a,const void * b)459 static int func_cmp(const void *a, const void *b)
460 {
461 const struct func_map *fa = a;
462 const struct func_map *fb = b;
463
464 if (fa->addr < fb->addr)
465 return -1;
466 if (fa->addr > fb->addr)
467 return 1;
468
469 return 0;
470 }
471
472 /*
473 * We are searching for a record in between, not an exact
474 * match.
475 */
func_bcmp(const void * a,const void * b)476 static int func_bcmp(const void *a, const void *b)
477 {
478 const struct func_map *fa = a;
479 const struct func_map *fb = b;
480
481 if ((fa->addr == fb->addr) ||
482
483 (fa->addr > fb->addr &&
484 fa->addr < (fb+1)->addr))
485 return 0;
486
487 if (fa->addr < fb->addr)
488 return -1;
489
490 return 1;
491 }
492
func_map_init(struct tep_handle * tep)493 static int func_map_init(struct tep_handle *tep)
494 {
495 struct func_list *funclist;
496 struct func_list *item;
497 struct func_map *func_map;
498 int i;
499
500 func_map = malloc(sizeof(*func_map) * (tep->func_count + 1));
501 if (!func_map)
502 return -1;
503
504 funclist = tep->funclist;
505
506 i = 0;
507 while (funclist) {
508 func_map[i].func = funclist->func;
509 func_map[i].addr = funclist->addr;
510 func_map[i].mod = funclist->mod;
511 i++;
512 item = funclist;
513 funclist = funclist->next;
514 free(item);
515 }
516
517 qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp);
518
519 /*
520 * Add a special record at the end.
521 */
522 func_map[tep->func_count].func = NULL;
523 func_map[tep->func_count].addr = 0;
524 func_map[tep->func_count].mod = NULL;
525
526 tep->func_map = func_map;
527 tep->funclist = NULL;
528
529 return 0;
530 }
531
532 static struct func_map *
__find_func(struct tep_handle * tep,unsigned long long addr)533 __find_func(struct tep_handle *tep, unsigned long long addr)
534 {
535 struct func_map *func;
536 struct func_map key;
537
538 if (!tep->func_map)
539 func_map_init(tep);
540
541 key.addr = addr;
542
543 func = bsearch(&key, tep->func_map, tep->func_count,
544 sizeof(*tep->func_map), func_bcmp);
545
546 return func;
547 }
548
549 struct func_resolver {
550 tep_func_resolver_t *func;
551 void *priv;
552 struct func_map map;
553 };
554
555 /**
556 * tep_set_function_resolver - set an alternative function resolver
557 * @tep: a handle to the trace event parser context
558 * @resolver: function to be used
559 * @priv: resolver function private state.
560 *
561 * Some tools may have already a way to resolve kernel functions, allow them to
562 * keep using it instead of duplicating all the entries inside tep->funclist.
563 */
tep_set_function_resolver(struct tep_handle * tep,tep_func_resolver_t * func,void * priv)564 int tep_set_function_resolver(struct tep_handle *tep,
565 tep_func_resolver_t *func, void *priv)
566 {
567 struct func_resolver *resolver = malloc(sizeof(*resolver));
568
569 if (resolver == NULL)
570 return -1;
571
572 resolver->func = func;
573 resolver->priv = priv;
574
575 free(tep->func_resolver);
576 tep->func_resolver = resolver;
577
578 return 0;
579 }
580
581 /**
582 * tep_reset_function_resolver - reset alternative function resolver
583 * @tep: a handle to the trace event parser context
584 *
585 * Stop using whatever alternative resolver was set, use the default
586 * one instead.
587 */
tep_reset_function_resolver(struct tep_handle * tep)588 void tep_reset_function_resolver(struct tep_handle *tep)
589 {
590 free(tep->func_resolver);
591 tep->func_resolver = NULL;
592 }
593
594 static struct func_map *
find_func(struct tep_handle * tep,unsigned long long addr)595 find_func(struct tep_handle *tep, unsigned long long addr)
596 {
597 struct func_map *map;
598
599 if (!tep->func_resolver)
600 return __find_func(tep, addr);
601
602 map = &tep->func_resolver->map;
603 map->mod = NULL;
604 map->addr = addr;
605 map->func = tep->func_resolver->func(tep->func_resolver->priv,
606 &map->addr, &map->mod);
607 if (map->func == NULL)
608 return NULL;
609
610 return map;
611 }
612
613 /**
614 * tep_find_function_info - find a function by a given address
615 * @tep: a handle to the trace event parser context
616 * @addr: the address to find the function with
617 * @name: Return the name of the function (if found)
618 * @start: Return the start of the function (if found)
619 * @size: Return the size of the function (if found)
620 *
621 * Returns 1 if found, and 0 if it is not.
622 * If found then @name will point to the name of the function.
623 * @start: will contain the starting address of the function.
624 * @size: will contain the size of the function.
625 */
tep_find_function_info(struct tep_handle * tep,unsigned long long addr,const char ** name,unsigned long long * start,unsigned long * size)626 int tep_find_function_info(struct tep_handle *tep, unsigned long long addr,
627 const char **name, unsigned long long *start,
628 unsigned long *size)
629 {
630 struct func_map *map;
631
632 map = find_func(tep, addr);
633 if (!map)
634 return 0;
635
636 if (name)
637 *name = map->func;
638 if (start)
639 *start = map->addr;
640 if (size) {
641 if (!tep->func_resolver)
642 *size = map[1].addr - map->addr;
643 else
644 *size = 0;
645 }
646
647 return 1;
648 }
649
650 /**
651 * tep_find_function - find a function by a given address
652 * @tep: a handle to the trace event parser context
653 * @addr: the address to find the function with
654 *
655 * Returns a pointer to the function stored that has the given
656 * address. Note, the address does not have to be exact, it
657 * will select the function that would contain the address.
658 */
tep_find_function(struct tep_handle * tep,unsigned long long addr)659 const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
660 {
661 struct func_map *map;
662
663 map = find_func(tep, addr);
664 if (!map)
665 return NULL;
666
667 return map->func;
668 }
669
670 /**
671 * tep_find_function_address - find a function address by a given address
672 * @tep: a handle to the trace event parser context
673 * @addr: the address to find the function with
674 *
675 * Returns the address the function starts at. This can be used in
676 * conjunction with tep_find_function to print both the function
677 * name and the function offset.
678 */
679 unsigned long long
tep_find_function_address(struct tep_handle * tep,unsigned long long addr)680 tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
681 {
682 struct func_map *map;
683
684 map = find_func(tep, addr);
685 if (!map)
686 return 0;
687
688 return map->addr;
689 }
690
691 /**
692 * tep_register_function - register a function with a given address
693 * @tep: a handle to the trace event parser context
694 * @function: the function name to register
695 * @addr: the address the function starts at
696 * @mod: the kernel module the function may be in (NULL for none)
697 *
698 * This registers a function name with an address and module.
699 * The @func passed in is duplicated.
700 */
tep_register_function(struct tep_handle * tep,char * func,unsigned long long addr,char * mod)701 int tep_register_function(struct tep_handle *tep, char *func,
702 unsigned long long addr, char *mod)
703 {
704 struct func_list *item = malloc(sizeof(*item));
705
706 if (!item)
707 return -1;
708
709 item->next = tep->funclist;
710 item->func = strdup(func);
711 if (!item->func)
712 goto out_free;
713
714 if (mod) {
715 item->mod = strdup(mod);
716 if (!item->mod)
717 goto out_free_func;
718 } else
719 item->mod = NULL;
720 item->addr = addr;
721
722 tep->funclist = item;
723 tep->func_count++;
724
725 return 0;
726
727 out_free_func:
728 free(item->func);
729 item->func = NULL;
730 out_free:
731 free(item);
732 errno = ENOMEM;
733 return -1;
734 }
735
736 /**
737 * tep_parse_kallsyms - load functions from a read of /proc/kallsyms
738 * @tep: a handle to the trace event parser
739 * @kallsyms: A string buffer that holds the content of /proc/kallsyms and ends with '\0'
740 *
741 * This is a helper function to parse the Linux kernel /proc/kallsyms
742 * format (stored in a string buffer) and load the functions into
743 * the @tep handler such that function IP addresses can be mapped to
744 * their name when parsing events with %pS in the print format field.
745 *
746 * Returns 0 on success, and -1 on error.
747 */
tep_parse_kallsyms(struct tep_handle * tep,const char * kallsyms)748 int tep_parse_kallsyms(struct tep_handle *tep, const char *kallsyms)
749 {
750 unsigned long long addr;
751 char *copy;
752 char *func;
753 char *line;
754 char *next = NULL;
755 char *mod;
756 char ch;
757 int ret = -1;
758
759 copy = strdup(kallsyms);
760 if (!copy)
761 return -1;
762
763 line = strtok_r(copy, "\n", &next);
764 while (line) {
765 int func_start, func_end = 0;
766 int mod_start, mod_end = 0;
767 int n;
768
769 mod = NULL;
770 errno = 0;
771 n = sscanf(line, "%16llx %c %n%*s%n%*1[\t][%n%*s%n",
772 &addr, &ch, &func_start, &func_end, &mod_start, &mod_end);
773 if (errno)
774 goto out;
775
776 if (n != 2 || !func_end) {
777 tep_warning("Failed to parse kallsyms n=%d func_end=%d",
778 n, func_end);
779 goto out;
780 }
781
782 func = line + func_start;
783 /*
784 * Hacks for
785 * - arm arch that adds a lot of bogus '$a' functions
786 * - x86-64 that reports per-cpu variable offsets as absolute
787 */
788 if (func[0] != '$' && ch != 'A' && ch != 'a') {
789 line[func_end] = 0;
790 if (mod_end) {
791 mod = line + mod_start;
792 /* truncate the extra ']' */
793 line[mod_end - 1] = 0;
794 }
795 tep_register_function(tep, func, addr, mod);
796 }
797
798 line = strtok_r(NULL, "\n", &next);
799 }
800 free(line);
801 ret = 0;
802 out:
803 free(copy);
804
805 return ret;
806 }
807
808 /**
809 * tep_print_funcs - print out the stored functions
810 * @tep: a handle to the trace event parser context
811 *
812 * This prints out the stored functions.
813 */
tep_print_funcs(struct tep_handle * tep)814 void tep_print_funcs(struct tep_handle *tep)
815 {
816 int i;
817
818 if (!tep->func_map)
819 func_map_init(tep);
820
821 for (i = 0; i < (int)tep->func_count; i++) {
822 printf("%016llx %s",
823 tep->func_map[i].addr,
824 tep->func_map[i].func);
825 if (tep->func_map[i].mod)
826 printf(" [%s]\n", tep->func_map[i].mod);
827 else
828 printf("\n");
829 }
830 }
831
832 struct printk_map {
833 unsigned long long addr;
834 char *printk;
835 };
836
837 struct printk_list {
838 struct printk_list *next;
839 unsigned long long addr;
840 char *printk;
841 };
842
printk_cmp(const void * a,const void * b)843 static int printk_cmp(const void *a, const void *b)
844 {
845 const struct printk_map *pa = a;
846 const struct printk_map *pb = b;
847
848 if (pa->addr < pb->addr)
849 return -1;
850 if (pa->addr > pb->addr)
851 return 1;
852
853 return 0;
854 }
855
printk_map_init(struct tep_handle * tep)856 static int printk_map_init(struct tep_handle *tep)
857 {
858 struct printk_list *printklist;
859 struct printk_list *item;
860 struct printk_map *printk_map;
861 int i;
862
863 printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1));
864 if (!printk_map)
865 return -1;
866
867 printklist = tep->printklist;
868
869 i = 0;
870 while (printklist) {
871 printk_map[i].printk = printklist->printk;
872 printk_map[i].addr = printklist->addr;
873 i++;
874 item = printklist;
875 printklist = printklist->next;
876 free(item);
877 }
878
879 qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp);
880
881 tep->printk_map = printk_map;
882 tep->printklist = NULL;
883
884 return 0;
885 }
886
887 static struct printk_map *
find_printk(struct tep_handle * tep,unsigned long long addr)888 find_printk(struct tep_handle *tep, unsigned long long addr)
889 {
890 struct printk_map *printk;
891 struct printk_map key;
892
893 if (!tep->printk_map && printk_map_init(tep))
894 return NULL;
895
896 key.addr = addr;
897
898 printk = bsearch(&key, tep->printk_map, tep->printk_count,
899 sizeof(*tep->printk_map), printk_cmp);
900
901 return printk;
902 }
903
904 /**
905 * tep_register_print_string - register a string by its address
906 * @tep: a handle to the trace event parser context
907 * @fmt: the string format to register
908 * @addr: the address the string was located at
909 *
910 * This registers a string by the address it was stored in the kernel.
911 * The @fmt passed in is duplicated.
912 */
tep_register_print_string(struct tep_handle * tep,const char * fmt,unsigned long long addr)913 int tep_register_print_string(struct tep_handle *tep, const char *fmt,
914 unsigned long long addr)
915 {
916 struct printk_list *item = malloc(sizeof(*item));
917 char *p;
918
919 if (!item)
920 return -1;
921
922 item->next = tep->printklist;
923 item->addr = addr;
924
925 /* Strip off quotes and '\n' from the end */
926 if (fmt[0] == '"')
927 fmt++;
928 item->printk = strdup(fmt);
929 if (!item->printk)
930 goto out_free;
931
932 p = item->printk + strlen(item->printk) - 1;
933 if (*p == '"')
934 *p = 0;
935
936 p -= 2;
937 if (strcmp(p, "\\n") == 0)
938 *p = 0;
939
940 tep->printklist = item;
941 tep->printk_count++;
942
943 return 0;
944
945 out_free:
946 free(item);
947 errno = ENOMEM;
948 return -1;
949 }
950
951 /**
952 * tep_print_printk - print out the stored strings
953 * @tep: a handle to the trace event parser context
954 *
955 * This prints the string formats that were stored.
956 */
tep_print_printk(struct tep_handle * tep)957 void tep_print_printk(struct tep_handle *tep)
958 {
959 int i;
960
961 if (!tep->printk_map)
962 printk_map_init(tep);
963
964 for (i = 0; i < (int)tep->printk_count; i++) {
965 printf("%016llx %s\n",
966 tep->printk_map[i].addr,
967 tep->printk_map[i].printk);
968 }
969 }
970
971 /**
972 * tep_parse_printk_formats - Parse the address to strings
973 * @tep: a handle to the trace event parser
974 * @buf: A string buffer that holds the content of printk_formats and ends with '\0'
975 *
976 * This is a helper function to parse the address to printk formats in
977 * the kernel. Some events use %s to a kernel address that holds a constant
978 * string. The printk_formats file has a mapping of these addresses to the
979 * strings that are in the kernel. This parses the content of that file
980 * and registers those strings and their addresses so that the parsing of
981 * events can display the string as the event only has the address of the string.
982 *
983 * Returns 0 on success, and -1 on error.
984 */
tep_parse_printk_formats(struct tep_handle * tep,const char * buf)985 int tep_parse_printk_formats(struct tep_handle *tep, const char *buf)
986 {
987 unsigned long long addr;
988 char *addr_str;
989 char *printk;
990 char *copy;
991 char *line;
992 char *next;
993 char *fmt;
994 int ret = -1;
995
996 copy = strdup(buf);
997 if (!copy)
998 return -1;
999
1000 line = strtok_r(copy, "\n", &next);
1001 while (line) {
1002 addr_str = strtok_r(line, ":", &fmt);
1003 if (!addr_str) {
1004 tep_warning("printk format with empty entry");
1005 break;
1006 }
1007 addr = strtoull(addr_str, NULL, 16);
1008 /* fmt still has a space, skip it */
1009 printk = strdup(fmt+1);
1010 if (!printk)
1011 goto out;
1012 line = strtok_r(NULL, "\n", &next);
1013 tep_register_print_string(tep, printk, addr);
1014 free(printk);
1015 }
1016 ret = 0;
1017 out:
1018 free(copy);
1019 return ret;
1020 }
1021
alloc_event(void)1022 static struct tep_event *alloc_event(void)
1023 {
1024 return calloc(1, sizeof(struct tep_event));
1025 }
1026
add_event(struct tep_handle * tep,struct tep_event * event)1027 static int add_event(struct tep_handle *tep, struct tep_event *event)
1028 {
1029 int i;
1030 struct tep_event **events = realloc(tep->events, sizeof(event) *
1031 (tep->nr_events + 1));
1032 if (!events)
1033 return -1;
1034
1035 tep->events = events;
1036
1037 for (i = 0; i < tep->nr_events; i++) {
1038 if (tep->events[i]->id > event->id)
1039 break;
1040 }
1041 if (i < tep->nr_events)
1042 memmove(&tep->events[i + 1],
1043 &tep->events[i],
1044 sizeof(event) * (tep->nr_events - i));
1045
1046 tep->events[i] = event;
1047 tep->nr_events++;
1048
1049 event->tep = tep;
1050
1051 return 0;
1052 }
1053
event_item_type(enum tep_event_type type)1054 static int event_item_type(enum tep_event_type type)
1055 {
1056 switch (type) {
1057 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
1058 return 1;
1059 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
1060 default:
1061 return 0;
1062 }
1063 }
1064
free_flag_sym(struct tep_print_flag_sym * fsym)1065 static void free_flag_sym(struct tep_print_flag_sym *fsym)
1066 {
1067 struct tep_print_flag_sym *next;
1068
1069 while (fsym) {
1070 next = fsym->next;
1071 free(fsym->value);
1072 free(fsym->str);
1073 free(fsym);
1074 fsym = next;
1075 }
1076 }
1077
free_arg(struct tep_print_arg * arg)1078 static void free_arg(struct tep_print_arg *arg)
1079 {
1080 struct tep_print_arg *farg;
1081
1082 if (!arg)
1083 return;
1084
1085 switch (arg->type) {
1086 case TEP_PRINT_ATOM:
1087 free(arg->atom.atom);
1088 break;
1089 case TEP_PRINT_FIELD:
1090 free(arg->field.name);
1091 break;
1092 case TEP_PRINT_FLAGS:
1093 free_arg(arg->flags.field);
1094 free(arg->flags.delim);
1095 free_flag_sym(arg->flags.flags);
1096 break;
1097 case TEP_PRINT_SYMBOL:
1098 free_arg(arg->symbol.field);
1099 free_flag_sym(arg->symbol.symbols);
1100 break;
1101 case TEP_PRINT_HEX:
1102 case TEP_PRINT_HEX_STR:
1103 free_arg(arg->hex.field);
1104 free_arg(arg->hex.size);
1105 break;
1106 case TEP_PRINT_INT_ARRAY:
1107 free_arg(arg->int_array.field);
1108 free_arg(arg->int_array.count);
1109 free_arg(arg->int_array.el_size);
1110 break;
1111 case TEP_PRINT_TYPE:
1112 free(arg->typecast.type);
1113 free_arg(arg->typecast.item);
1114 break;
1115 case TEP_PRINT_STRING:
1116 case TEP_PRINT_BSTRING:
1117 free(arg->string.string);
1118 break;
1119 case TEP_PRINT_BITMASK:
1120 case TEP_PRINT_CPUMASK:
1121 free(arg->bitmask.bitmask);
1122 break;
1123 case TEP_PRINT_DYNAMIC_ARRAY:
1124 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
1125 free(arg->dynarray.index);
1126 break;
1127 case TEP_PRINT_OP:
1128 free(arg->op.op);
1129 free_arg(arg->op.left);
1130 free_arg(arg->op.right);
1131 break;
1132 case TEP_PRINT_FUNC:
1133 while (arg->func.args) {
1134 farg = arg->func.args;
1135 arg->func.args = farg->next;
1136 free_arg(farg);
1137 }
1138 break;
1139
1140 case TEP_PRINT_NULL:
1141 default:
1142 break;
1143 }
1144
1145 free(arg);
1146 }
1147
get_type(int ch)1148 static enum tep_event_type get_type(int ch)
1149 {
1150 if (ch == '\n')
1151 return TEP_EVENT_NEWLINE;
1152 if (isspace(ch))
1153 return TEP_EVENT_SPACE;
1154 if (isalnum(ch) || ch == '_')
1155 return TEP_EVENT_ITEM;
1156 if (ch == '\'')
1157 return TEP_EVENT_SQUOTE;
1158 if (ch == '"')
1159 return TEP_EVENT_DQUOTE;
1160 if (!isprint(ch))
1161 return TEP_EVENT_NONE;
1162 if (ch == '(' || ch == ')' || ch == ',')
1163 return TEP_EVENT_DELIM;
1164
1165 return TEP_EVENT_OP;
1166 }
1167
__read_char(struct tep_handle * tep)1168 static int __read_char(struct tep_handle *tep)
1169 {
1170 if (tep->input_buf_ptr >= tep->input_buf_siz)
1171 return -1;
1172
1173 return tep->input_buf[tep->input_buf_ptr++];
1174 }
1175
1176 /**
1177 * peek_char - peek at the next character that will be read
1178 *
1179 * Returns the next character read, or -1 if end of buffer.
1180 */
peek_char(struct tep_handle * tep)1181 __hidden int peek_char(struct tep_handle *tep)
1182 {
1183 if (tep->input_buf_ptr >= tep->input_buf_siz)
1184 return -1;
1185
1186 return tep->input_buf[tep->input_buf_ptr];
1187 }
1188
extend_token(char ** tok,char * buf,int size)1189 static int extend_token(char **tok, char *buf, int size)
1190 {
1191 char *newtok = realloc(*tok, size);
1192
1193 if (!newtok) {
1194 free(*tok);
1195 *tok = NULL;
1196 return -1;
1197 }
1198
1199 if (!*tok)
1200 strcpy(newtok, buf);
1201 else
1202 strcat(newtok, buf);
1203 *tok = newtok;
1204
1205 return 0;
1206 }
1207
1208 static enum tep_event_type force_token(struct tep_handle *tep, const char *str,
1209 char **tok);
1210
__read_token(struct tep_handle * tep,char ** tok)1211 static enum tep_event_type __read_token(struct tep_handle *tep, char **tok)
1212 {
1213 char buf[BUFSIZ];
1214 int ch, last_ch, quote_ch, next_ch;
1215 int i = 0;
1216 int tok_size = 0;
1217 enum tep_event_type type;
1218
1219 *tok = NULL;
1220
1221
1222 ch = __read_char(tep);
1223 if (ch < 0)
1224 return TEP_EVENT_NONE;
1225
1226 type = get_type(ch);
1227 if (type == TEP_EVENT_NONE)
1228 return type;
1229
1230 buf[i++] = ch;
1231
1232 switch (type) {
1233 case TEP_EVENT_NEWLINE:
1234 case TEP_EVENT_DELIM:
1235 if (asprintf(tok, "%c", ch) < 0)
1236 return TEP_EVENT_ERROR;
1237
1238 return type;
1239
1240 case TEP_EVENT_OP:
1241 switch (ch) {
1242 case '-':
1243 next_ch = peek_char(tep);
1244 if (next_ch == '>') {
1245 buf[i++] = __read_char(tep);
1246 break;
1247 }
1248 /* fall through */
1249 case '+':
1250 case '|':
1251 case '&':
1252 case '>':
1253 case '<':
1254 last_ch = ch;
1255 ch = peek_char(tep);
1256 if (ch != last_ch)
1257 goto test_equal;
1258 buf[i++] = __read_char(tep);
1259 switch (last_ch) {
1260 case '>':
1261 case '<':
1262 goto test_equal;
1263 default:
1264 break;
1265 }
1266 break;
1267 case '!':
1268 case '=':
1269 goto test_equal;
1270 default: /* what should we do instead? */
1271 break;
1272 }
1273 buf[i] = 0;
1274 *tok = strdup(buf);
1275 return type;
1276
1277 test_equal:
1278 ch = peek_char(tep);
1279 if (ch == '=')
1280 buf[i++] = __read_char(tep);
1281 goto out;
1282
1283 case TEP_EVENT_DQUOTE:
1284 case TEP_EVENT_SQUOTE:
1285 /* don't keep quotes */
1286 i--;
1287 quote_ch = ch;
1288 last_ch = 0;
1289 concat:
1290 do {
1291 if (i == (BUFSIZ - 1)) {
1292 buf[i] = 0;
1293 tok_size += BUFSIZ;
1294
1295 if (extend_token(tok, buf, tok_size) < 0)
1296 return TEP_EVENT_NONE;
1297 i = 0;
1298 }
1299 last_ch = ch;
1300 ch = __read_char(tep);
1301 buf[i++] = ch;
1302 /* the '\' '\' will cancel itself */
1303 if (ch == '\\' && last_ch == '\\')
1304 last_ch = 0;
1305 /* Break out if the file is corrupted and giving non print chars */
1306 if (ch <= 0)
1307 break;
1308 } while ((ch != quote_ch && isprint(ch)) || last_ch == '\\' || ch == '\n');
1309 /* remove the last quote */
1310 i--;
1311
1312 if (ch <= 0)
1313 type = TEP_EVENT_NONE;
1314
1315 /*
1316 * For strings (double quotes) check the next token.
1317 * If it is another string, concatinate the two.
1318 */
1319 if (type == TEP_EVENT_DQUOTE) {
1320 unsigned long long save_input_buf_ptr = tep->input_buf_ptr;
1321
1322 do {
1323 ch = __read_char(tep);
1324 } while (isspace(ch));
1325 if (ch == '"')
1326 goto concat;
1327 tep->input_buf_ptr = save_input_buf_ptr;
1328 }
1329
1330 goto out;
1331
1332 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1333 case TEP_EVENT_ITEM:
1334 default:
1335 break;
1336 }
1337
1338 while (get_type(peek_char(tep)) == type) {
1339 if (i == (BUFSIZ - 1)) {
1340 buf[i] = 0;
1341 tok_size += BUFSIZ;
1342
1343 if (extend_token(tok, buf, tok_size) < 0)
1344 return TEP_EVENT_NONE;
1345 i = 0;
1346 }
1347 ch = __read_char(tep);
1348 buf[i++] = ch;
1349 }
1350
1351 out:
1352 buf[i] = 0;
1353 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1354 return TEP_EVENT_NONE;
1355
1356 if (type == TEP_EVENT_ITEM) {
1357 /*
1358 * Older versions of the kernel has a bug that
1359 * creates invalid symbols and will break the mac80211
1360 * parsing. This is a work around to that bug.
1361 *
1362 * See Linux kernel commit:
1363 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1364 */
1365 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1366 free(*tok);
1367 *tok = NULL;
1368 return force_token(tep, "\"%s\" ", tok);
1369 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1370 free(*tok);
1371 *tok = NULL;
1372 return force_token(tep, "\" sta:%pM\" ", tok);
1373 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1374 free(*tok);
1375 *tok = NULL;
1376 return force_token(tep, "\" vif:%p(%d)\" ", tok);
1377 }
1378 }
1379
1380 return type;
1381 }
1382
force_token(struct tep_handle * tep,const char * str,char ** tok)1383 static enum tep_event_type force_token(struct tep_handle *tep, const char *str,
1384 char **tok)
1385 {
1386 const char *save_input_buf;
1387 unsigned long long save_input_buf_ptr;
1388 unsigned long long save_input_buf_siz;
1389 enum tep_event_type type;
1390
1391 /* save off the current input pointers */
1392 save_input_buf = tep->input_buf;
1393 save_input_buf_ptr = tep->input_buf_ptr;
1394 save_input_buf_siz = tep->input_buf_siz;
1395
1396 init_input_buf(tep, str, strlen(str));
1397
1398 type = __read_token(tep, tok);
1399
1400 /* reset back to original token */
1401 tep->input_buf = save_input_buf;
1402 tep->input_buf_ptr = save_input_buf_ptr;
1403 tep->input_buf_siz = save_input_buf_siz;
1404
1405 return type;
1406 }
1407
1408 /**
1409 * free_token - free a token returned by tep_read_token
1410 * @token: the token to free
1411 */
free_token(char * tok)1412 __hidden void free_token(char *tok)
1413 {
1414 if (tok)
1415 free(tok);
1416 }
1417
1418 /**
1419 * read_token - access to utilities to use the tep parser
1420 * @tok: The token to return
1421 *
1422 * This will parse tokens from the string given by
1423 * tep_init_data().
1424 *
1425 * Returns the token type.
1426 */
read_token(struct tep_handle * tep,char ** tok)1427 __hidden enum tep_event_type read_token(struct tep_handle *tep, char **tok)
1428 {
1429 enum tep_event_type type;
1430
1431 for (;;) {
1432 type = __read_token(tep, tok);
1433 if (type != TEP_EVENT_SPACE)
1434 return type;
1435
1436 free_token(*tok);
1437 }
1438
1439 /* not reached */
1440 *tok = NULL;
1441 return TEP_EVENT_NONE;
1442 }
1443
1444 /* no newline */
read_token_item(struct tep_handle * tep,char ** tok)1445 static enum tep_event_type read_token_item(struct tep_handle *tep, char **tok)
1446 {
1447 enum tep_event_type type;
1448
1449 for (;;) {
1450 type = __read_token(tep, tok);
1451 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1452 return type;
1453 free_token(*tok);
1454 *tok = NULL;
1455 }
1456
1457 /* not reached */
1458 *tok = NULL;
1459 return TEP_EVENT_NONE;
1460 }
1461
test_type(enum tep_event_type type,enum tep_event_type expect)1462 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1463 {
1464 if (type != expect) {
1465 do_warning("Error: expected type %d (%s) but read %d (%s)",
1466 expect, get_event_type(expect),
1467 type, get_event_type(type));
1468 return -1;
1469 }
1470 return 0;
1471 }
1472
test_type_token(enum tep_event_type type,const char * token,enum tep_event_type expect,const char * expect_tok)1473 static int test_type_token(enum tep_event_type type, const char *token,
1474 enum tep_event_type expect, const char *expect_tok)
1475 {
1476 if (type != expect) {
1477 do_warning("Error: expected type %d (%s) but read %d (%s)",
1478 expect, get_event_type(expect),
1479 type, get_event_type(type));
1480 return -1;
1481 }
1482
1483 if (strcmp(token, expect_tok) != 0) {
1484 do_warning("Error: expected '%s' but read '%s'",
1485 expect_tok, token);
1486 return -1;
1487 }
1488 return 0;
1489 }
1490
__read_expect_type(struct tep_handle * tep,enum tep_event_type expect,char ** tok,int newline_ok)1491 static int __read_expect_type(struct tep_handle *tep, enum tep_event_type expect,
1492 char **tok, int newline_ok)
1493 {
1494 enum tep_event_type type;
1495
1496 if (newline_ok)
1497 type = read_token(tep, tok);
1498 else
1499 type = read_token_item(tep, tok);
1500 return test_type(type, expect);
1501 }
1502
read_expect_type(struct tep_handle * tep,enum tep_event_type expect,char ** tok)1503 static int read_expect_type(struct tep_handle *tep, enum tep_event_type expect,
1504 char **tok)
1505 {
1506 return __read_expect_type(tep, expect, tok, 1);
1507 }
1508
__read_expected(struct tep_handle * tep,enum tep_event_type expect,const char * str,int newline_ok)1509 static int __read_expected(struct tep_handle *tep, enum tep_event_type expect,
1510 const char *str, int newline_ok)
1511 {
1512 enum tep_event_type type;
1513 char *token;
1514 int ret;
1515
1516 if (newline_ok)
1517 type = read_token(tep, &token);
1518 else
1519 type = read_token_item(tep, &token);
1520
1521 ret = test_type_token(type, token, expect, str);
1522
1523 free_token(token);
1524
1525 return ret;
1526 }
1527
read_expected(struct tep_handle * tep,enum tep_event_type expect,const char * str)1528 static int read_expected(struct tep_handle *tep, enum tep_event_type expect,
1529 const char *str)
1530 {
1531 return __read_expected(tep, expect, str, 1);
1532 }
1533
read_expected_item(struct tep_handle * tep,enum tep_event_type expect,const char * str)1534 static int read_expected_item(struct tep_handle *tep, enum tep_event_type expect,
1535 const char *str)
1536 {
1537 return __read_expected(tep, expect, str, 0);
1538 }
1539
event_read_name(struct tep_handle * tep)1540 static char *event_read_name(struct tep_handle *tep)
1541 {
1542 char *token;
1543
1544 if (read_expected(tep, TEP_EVENT_ITEM, "name") < 0)
1545 return NULL;
1546
1547 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1548 return NULL;
1549
1550 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1551 goto fail;
1552
1553 return token;
1554
1555 fail:
1556 free_token(token);
1557 return NULL;
1558 }
1559
event_read_id(struct tep_handle * tep)1560 static int event_read_id(struct tep_handle *tep)
1561 {
1562 char *token;
1563 int id;
1564
1565 if (read_expected_item(tep, TEP_EVENT_ITEM, "ID") < 0)
1566 return -1;
1567
1568 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1569 return -1;
1570
1571 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1572 goto fail;
1573
1574 id = strtoul(token, NULL, 0);
1575 free_token(token);
1576 return id;
1577
1578 fail:
1579 free_token(token);
1580 return -1;
1581 }
1582
field_is_string(struct tep_format_field * field)1583 static int field_is_string(struct tep_format_field *field)
1584 {
1585 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1586 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1587 strstr(field->type, "s8")))
1588 return 1;
1589
1590 return 0;
1591 }
1592
field_is_dynamic(struct tep_format_field * field)1593 static int field_is_dynamic(struct tep_format_field *field)
1594 {
1595 if (strncmp(field->type, "__data_loc", 10) == 0)
1596 return 1;
1597
1598 return 0;
1599 }
1600
field_is_relative_dynamic(struct tep_format_field * field)1601 static int field_is_relative_dynamic(struct tep_format_field *field)
1602 {
1603 if (strncmp(field->type, "__rel_loc", 9) == 0)
1604 return 1;
1605
1606 return 0;
1607 }
1608
field_is_long(struct tep_format_field * field)1609 static int field_is_long(struct tep_format_field *field)
1610 {
1611 /* includes long long */
1612 if (strstr(field->type, "long"))
1613 return 1;
1614
1615 return 0;
1616 }
1617
type_size(const char * name)1618 static unsigned int type_size(const char *name)
1619 {
1620 /* This covers all TEP_FIELD_IS_STRING types. */
1621 static struct {
1622 const char *type;
1623 unsigned int size;
1624 } table[] = {
1625 { "u8", 1 },
1626 { "u16", 2 },
1627 { "u32", 4 },
1628 { "u64", 8 },
1629 { "s8", 1 },
1630 { "s16", 2 },
1631 { "s32", 4 },
1632 { "s64", 8 },
1633 { "char", 1 },
1634 { },
1635 };
1636 int i;
1637
1638 for (i = 0; table[i].type; i++) {
1639 if (!strcmp(table[i].type, name))
1640 return table[i].size;
1641 }
1642
1643 return 0;
1644 }
1645
append(char ** buf,const char * delim,const char * str)1646 static int append(char **buf, const char *delim, const char *str)
1647 {
1648 char *new_buf;
1649
1650 new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1);
1651 if (!new_buf)
1652 return -1;
1653 strcat(new_buf, delim);
1654 strcat(new_buf, str);
1655 *buf = new_buf;
1656 return 0;
1657 }
1658
event_read_fields(struct tep_handle * tep,struct tep_event * event,struct tep_format_field ** fields)1659 static int event_read_fields(struct tep_handle *tep, struct tep_event *event,
1660 struct tep_format_field **fields)
1661 {
1662 struct tep_format_field *field = NULL;
1663 enum tep_event_type type;
1664 char *token;
1665 char *last_token;
1666 char *delim = " ";
1667 int count = 0;
1668 int ret;
1669
1670 do {
1671 unsigned int size_dynamic = 0;
1672
1673 type = read_token(tep, &token);
1674 if (type == TEP_EVENT_NEWLINE) {
1675 free_token(token);
1676 return count;
1677 }
1678
1679 count++;
1680
1681 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1682 goto fail;
1683 free_token(token);
1684
1685 type = read_token(tep, &token);
1686 /*
1687 * The ftrace fields may still use the "special" name.
1688 * Just ignore it.
1689 */
1690 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1691 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1692 free_token(token);
1693 type = read_token(tep, &token);
1694 }
1695
1696 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1697 goto fail;
1698
1699 free_token(token);
1700 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
1701 goto fail;
1702
1703 last_token = token;
1704
1705 field = calloc(1, sizeof(*field));
1706 if (!field)
1707 goto fail;
1708
1709 field->event = event;
1710
1711 /* read the rest of the type */
1712 for (;;) {
1713 type = read_token(tep, &token);
1714 if (type == TEP_EVENT_ITEM ||
1715 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1716 /*
1717 * Some of the ftrace fields are broken and have
1718 * an illegal "." in them.
1719 */
1720 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1721 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1722
1723 if (strcmp(token, "*") == 0)
1724 field->flags |= TEP_FIELD_IS_POINTER;
1725
1726 if (field->type) {
1727 ret = append(&field->type, delim, last_token);
1728 free(last_token);
1729 if (ret < 0)
1730 goto fail;
1731 } else
1732 field->type = last_token;
1733 last_token = token;
1734 delim = " ";
1735 continue;
1736 }
1737
1738 /* Handle __attribute__((user)) */
1739 if ((type == TEP_EVENT_DELIM) &&
1740 strcmp("__attribute__", last_token) == 0 &&
1741 token[0] == '(') {
1742 int depth = 1;
1743 int ret;
1744
1745 ret = append(&field->type, " ", last_token);
1746 ret |= append(&field->type, "", "(");
1747 if (ret < 0)
1748 goto fail;
1749
1750 delim = " ";
1751 while ((type = read_token(tep, &token)) != TEP_EVENT_NONE) {
1752 if (type == TEP_EVENT_DELIM) {
1753 if (token[0] == '(')
1754 depth++;
1755 else if (token[0] == ')')
1756 depth--;
1757 if (!depth)
1758 break;
1759 ret = append(&field->type, "", token);
1760 delim = "";
1761 } else {
1762 ret = append(&field->type, delim, token);
1763 delim = " ";
1764 }
1765 if (ret < 0)
1766 goto fail;
1767 free(last_token);
1768 last_token = token;
1769 }
1770 continue;
1771 }
1772 break;
1773 }
1774
1775 if (!field->type) {
1776 do_warning_event(event, "%s: no type found", __func__);
1777 goto fail;
1778 }
1779 field->name = field->alias = last_token;
1780
1781 if (test_type(type, TEP_EVENT_OP))
1782 goto fail;
1783
1784 if (strcmp(token, "[") == 0) {
1785 enum tep_event_type last_type = type;
1786 char *brackets = token;
1787
1788 field->flags |= TEP_FIELD_IS_ARRAY;
1789
1790 type = read_token(tep, &token);
1791
1792 if (type == TEP_EVENT_ITEM)
1793 field->arraylen = strtoul(token, NULL, 0);
1794 else
1795 field->arraylen = 0;
1796
1797 while (strcmp(token, "]") != 0) {
1798 const char *delim;
1799
1800 if (last_type == TEP_EVENT_ITEM &&
1801 type == TEP_EVENT_ITEM)
1802 delim = " ";
1803 else
1804 delim = "";
1805
1806 last_type = type;
1807
1808 ret = append(&brackets, delim, token);
1809 if (ret < 0) {
1810 free(brackets);
1811 goto fail;
1812 }
1813 /* We only care about the last token */
1814 field->arraylen = strtoul(token, NULL, 0);
1815 free_token(token);
1816 type = read_token(tep, &token);
1817 if (type == TEP_EVENT_NONE) {
1818 free(brackets);
1819 do_warning_event(event, "failed to find token");
1820 goto fail;
1821 }
1822 }
1823
1824 free_token(token);
1825
1826 ret = append(&brackets, "", "]");
1827 if (ret < 0) {
1828 free(brackets);
1829 goto fail_expect;
1830 }
1831
1832 /* add brackets to type */
1833
1834 type = read_token(tep, &token);
1835 /*
1836 * If the next token is not an OP, then it is of
1837 * the format: type [] item;
1838 */
1839 if (type == TEP_EVENT_ITEM) {
1840 ret = append(&field->type, " ", field->name);
1841 if (ret < 0) {
1842 free(brackets);
1843 goto fail;
1844 }
1845 ret = append(&field->type, "", brackets);
1846
1847 size_dynamic = type_size(field->name);
1848 free_token(field->name);
1849 field->name = field->alias = token;
1850 type = read_token(tep, &token);
1851 } else {
1852 ret = append(&field->type, "", brackets);
1853 if (ret < 0) {
1854 free(brackets);
1855 goto fail;
1856 }
1857 }
1858 free(brackets);
1859 }
1860
1861 if (field_is_string(field))
1862 field->flags |= TEP_FIELD_IS_STRING;
1863 if (field_is_dynamic(field))
1864 field->flags |= TEP_FIELD_IS_DYNAMIC;
1865 if (field_is_relative_dynamic(field))
1866 field->flags |= TEP_FIELD_IS_DYNAMIC | TEP_FIELD_IS_RELATIVE;
1867 if (field_is_long(field))
1868 field->flags |= TEP_FIELD_IS_LONG;
1869
1870 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1871 goto fail;
1872 free_token(token);
1873
1874 if (read_expected(tep, TEP_EVENT_ITEM, "offset") < 0)
1875 goto fail_expect;
1876
1877 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1878 goto fail_expect;
1879
1880 if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1881 goto fail;
1882 field->offset = strtoul(token, NULL, 0);
1883 free_token(token);
1884
1885 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1886 goto fail_expect;
1887
1888 if (read_expected(tep, TEP_EVENT_ITEM, "size") < 0)
1889 goto fail_expect;
1890
1891 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1892 goto fail_expect;
1893
1894 if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1895 goto fail;
1896 field->size = strtoul(token, NULL, 0);
1897 free_token(token);
1898
1899 /*
1900 * The old data format before dynamic arrays had dynamic
1901 * strings defined with just a 2 byte offset (the length
1902 * is defined by the strlen() of the string. To process them
1903 * correctly, check if the field is dynamic and has a size of
1904 * 2 bytes. All current dynamic events have a size of 4.
1905 */
1906 if ((field->flags & TEP_FIELD_IS_DYNAMIC) && field->size == 2)
1907 field->flags |= TEP_FIELD_IS_STRING | TEP_FIELD_IS_ARRAY;
1908
1909 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1910 goto fail_expect;
1911
1912 type = read_token(tep, &token);
1913 if (type != TEP_EVENT_NEWLINE) {
1914 /* newer versions of the kernel have a "signed" type */
1915 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1916 goto fail;
1917
1918 free_token(token);
1919
1920 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
1921 goto fail_expect;
1922
1923 if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
1924 goto fail;
1925
1926 if (strtoul(token, NULL, 0))
1927 field->flags |= TEP_FIELD_IS_SIGNED;
1928
1929 free_token(token);
1930 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
1931 goto fail_expect;
1932
1933 if (read_expect_type(tep, TEP_EVENT_NEWLINE, &token))
1934 goto fail;
1935 }
1936
1937 free_token(token);
1938
1939 if (field->flags & (TEP_FIELD_IS_ARRAY | TEP_FIELD_IS_DYNAMIC)) {
1940 if (field->arraylen)
1941 field->elementsize = field->size / field->arraylen;
1942 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1943 field->elementsize = size_dynamic;
1944 else if (field->flags & TEP_FIELD_IS_STRING)
1945 field->elementsize = 1;
1946 else if (field->flags & TEP_FIELD_IS_LONG)
1947 field->elementsize = event->tep ?
1948 event->tep->long_size :
1949 sizeof(long);
1950 } else
1951 field->elementsize = field->size;
1952
1953 *fields = field;
1954 fields = &field->next;
1955 field = NULL;
1956
1957 } while (1);
1958
1959 return 0;
1960
1961 fail:
1962 free_token(token);
1963 fail_expect:
1964 if (field) {
1965 free(field->type);
1966 free(field->name);
1967 free(field);
1968 }
1969 return -1;
1970 }
1971
event_read_format(struct tep_event * event)1972 static int event_read_format(struct tep_event *event)
1973 {
1974 char *token;
1975 int ret;
1976
1977 if (read_expected_item(event->tep, TEP_EVENT_ITEM, "format") < 0)
1978 return -1;
1979
1980 if (read_expected(event->tep, TEP_EVENT_OP, ":") < 0)
1981 return -1;
1982
1983 if (read_expect_type(event->tep, TEP_EVENT_NEWLINE, &token))
1984 goto fail;
1985 free_token(token);
1986
1987 ret = event_read_fields(event->tep, event, &event->format.common_fields);
1988 if (ret < 0)
1989 return ret;
1990 event->format.nr_common = ret;
1991
1992 ret = event_read_fields(event->tep, event, &event->format.fields);
1993 if (ret < 0)
1994 return ret;
1995 event->format.nr_fields = ret;
1996
1997 return 0;
1998
1999 fail:
2000 free_token(token);
2001 return -1;
2002 }
2003
2004 static enum tep_event_type
2005 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
2006 char **tok, enum tep_event_type type);
2007
2008 static enum tep_event_type
process_arg(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2009 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2010 {
2011 enum tep_event_type type;
2012 char *token;
2013
2014 type = read_token(event->tep, &token);
2015 *tok = token;
2016
2017 return process_arg_token(event, arg, tok, type);
2018 }
2019
2020 static enum tep_event_type
2021 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
2022
2023 /*
2024 * For __print_symbolic() and __print_flags, we need to completely
2025 * evaluate the first argument, which defines what to print next.
2026 */
2027 static enum tep_event_type
process_field_arg(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2028 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2029 {
2030 enum tep_event_type type;
2031
2032 type = process_arg(event, arg, tok);
2033
2034 while (type == TEP_EVENT_OP) {
2035 type = process_op(event, arg, tok);
2036 }
2037
2038 return type;
2039 }
2040
2041 static enum tep_event_type
process_cond(struct tep_event * event,struct tep_print_arg * top,char ** tok)2042 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
2043 {
2044 struct tep_print_arg *arg, *left, *right;
2045 enum tep_event_type type;
2046 char *token = NULL;
2047
2048 arg = alloc_arg();
2049 left = alloc_arg();
2050 right = alloc_arg();
2051
2052 if (!arg || !left || !right) {
2053 do_warning_event(event, "%s: not enough memory!", __func__);
2054 /* arg will be freed at out_free */
2055 free_arg(left);
2056 free_arg(right);
2057 goto out_free;
2058 }
2059
2060 arg->type = TEP_PRINT_OP;
2061 arg->op.left = left;
2062 arg->op.right = right;
2063
2064 *tok = NULL;
2065 type = process_arg(event, left, &token);
2066
2067 again:
2068 if (type == TEP_EVENT_ERROR)
2069 goto out_free;
2070
2071 /* Handle other operations in the arguments */
2072 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
2073 type = process_op(event, left, &token);
2074 goto again;
2075 }
2076
2077 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
2078 goto out_free;
2079
2080 arg->op.op = token;
2081
2082 type = process_arg(event, right, &token);
2083
2084 top->op.right = arg;
2085
2086 *tok = token;
2087 return type;
2088
2089 out_free:
2090 /* Top may point to itself */
2091 top->op.right = NULL;
2092 free_token(token);
2093 free_arg(arg);
2094 return TEP_EVENT_ERROR;
2095 }
2096
2097 static enum tep_event_type
process_array(struct tep_event * event,struct tep_print_arg * top,char ** tok)2098 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
2099 {
2100 struct tep_print_arg *arg;
2101 enum tep_event_type type;
2102 char *token = NULL;
2103
2104 arg = alloc_arg();
2105 if (!arg) {
2106 do_warning_event(event, "%s: not enough memory!", __func__);
2107 /* '*tok' is set to top->op.op. No need to free. */
2108 *tok = NULL;
2109 return TEP_EVENT_ERROR;
2110 }
2111
2112 *tok = NULL;
2113 type = process_arg(event, arg, &token);
2114 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
2115 goto out_free;
2116
2117 top->op.right = arg;
2118
2119 free_token(token);
2120 type = read_token_item(event->tep, &token);
2121 *tok = token;
2122
2123 return type;
2124
2125 out_free:
2126 free_token(token);
2127 free_arg(arg);
2128 return TEP_EVENT_ERROR;
2129 }
2130
get_op_prio(char * op)2131 static int get_op_prio(char *op)
2132 {
2133 if (strlen(op) == 1) {
2134 switch (op[0]) {
2135 case '~':
2136 case '!':
2137 return 4;
2138 case '*':
2139 case '/':
2140 case '%':
2141 return 6;
2142 case '+':
2143 case '-':
2144 return 7;
2145 /* '>>' and '<<' are 8 */
2146 case '<':
2147 case '>':
2148 return 9;
2149 /* '==' and '!=' are 10 */
2150 case '&':
2151 return 11;
2152 case '^':
2153 return 12;
2154 case '|':
2155 return 13;
2156 case '?':
2157 return 16;
2158 default:
2159 do_warning("unknown op '%c'", op[0]);
2160 return -1;
2161 }
2162 } else {
2163 if (strcmp(op, "++") == 0 ||
2164 strcmp(op, "--") == 0) {
2165 return 3;
2166 } else if (strcmp(op, ">>") == 0 ||
2167 strcmp(op, "<<") == 0) {
2168 return 8;
2169 } else if (strcmp(op, ">=") == 0 ||
2170 strcmp(op, "<=") == 0) {
2171 return 9;
2172 } else if (strcmp(op, "==") == 0 ||
2173 strcmp(op, "!=") == 0) {
2174 return 10;
2175 } else if (strcmp(op, "&&") == 0) {
2176 return 14;
2177 } else if (strcmp(op, "||") == 0) {
2178 return 15;
2179 } else {
2180 do_warning("unknown op '%s'", op);
2181 return -1;
2182 }
2183 }
2184 }
2185
set_op_prio(struct tep_print_arg * arg)2186 static int set_op_prio(struct tep_print_arg *arg)
2187 {
2188
2189 /* single ops are the greatest */
2190 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
2191 arg->op.prio = 0;
2192 else
2193 arg->op.prio = get_op_prio(arg->op.op);
2194
2195 return arg->op.prio;
2196 }
2197
consolidate_op_arg(struct tep_print_arg * arg)2198 static int consolidate_op_arg(struct tep_print_arg *arg)
2199 {
2200 unsigned long long val, left, right;
2201 int ret = 0;
2202
2203 if (arg->type != TEP_PRINT_OP)
2204 return 0;
2205
2206 if (arg->op.left)
2207 ret = consolidate_op_arg(arg->op.left);
2208 if (ret < 0)
2209 return ret;
2210
2211 if (arg->op.right)
2212 ret = consolidate_op_arg(arg->op.right);
2213 if (ret < 0)
2214 return ret;
2215
2216 if (!arg->op.left || !arg->op.right)
2217 return 0;
2218
2219 if (arg->op.left->type != TEP_PRINT_ATOM ||
2220 arg->op.right->type != TEP_PRINT_ATOM)
2221 return 0;
2222
2223 /* Two atoms, we can do the operation now. */
2224 left = strtoull(arg->op.left->atom.atom, NULL, 0);
2225 right = strtoull(arg->op.right->atom.atom, NULL, 0);
2226
2227 switch (arg->op.op[0]) {
2228 case '>':
2229 switch (arg->op.op[1]) {
2230 case '>':
2231 val = left >> right;
2232 break;
2233 case '=':
2234 val = left >= right;
2235 break;
2236 default:
2237 val = left > right;
2238 break;
2239 }
2240 break;
2241 case '<':
2242 switch (arg->op.op[1]) {
2243 case '<':
2244 val = left << right;
2245 break;
2246 case '=':
2247 val = left <= right;
2248 break;
2249 default:
2250 val = left < right;
2251 break;
2252 }
2253 break;
2254 case '&':
2255 switch (arg->op.op[1]) {
2256 case '&':
2257 val = left && right;
2258 break;
2259 default:
2260 val = left & right;
2261 break;
2262 }
2263 break;
2264 case '|':
2265 switch (arg->op.op[1]) {
2266 case '|':
2267 val = left || right;
2268 break;
2269 default:
2270 val = left | right;
2271 break;
2272 }
2273 break;
2274 case '-':
2275 val = left - right;
2276 break;
2277 case '+':
2278 val = left + right;
2279 break;
2280 case '*':
2281 val = left * right;
2282 break;
2283 case '^':
2284 val = left ^ right;
2285 break;
2286 case '/':
2287 val = left / right;
2288 break;
2289 case '%':
2290 val = left % right;
2291 break;
2292 case '=':
2293 /* Only '==' is called here */
2294 val = left == right;
2295 break;
2296 case '!':
2297 /* Only '!=' is called here. */
2298 val = left != right;
2299 break;
2300 default:
2301 return 0;
2302 }
2303
2304 free_arg(arg->op.left);
2305 free_arg(arg->op.right);
2306
2307 arg->type = TEP_PRINT_ATOM;
2308 free(arg->op.op);
2309 return asprintf(&arg->atom.atom, "%lld", val) < 0 ? -1 : 0;
2310 }
2311
2312 /* Note, *tok does not get freed, but will most likely be saved */
2313 static enum tep_event_type
process_op(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2314 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2315 {
2316 struct tep_print_arg *left, *right = NULL;
2317 enum tep_event_type type;
2318 char *token;
2319
2320 /* the op is passed in via tok */
2321 token = *tok;
2322
2323 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
2324 /* handle single op */
2325 switch (token[0]) {
2326 case '~':
2327 case '!':
2328 case '+':
2329 case '-':
2330 break;
2331 default:
2332 do_warning_event(event, "bad op token %s", token);
2333 goto out_free;
2334
2335 }
2336 if (token[1]) {
2337 do_warning_event(event, "bad op token %s", token);
2338 goto out_free;
2339 }
2340
2341 /* make an empty left */
2342 left = alloc_arg();
2343 if (!left)
2344 goto out_warn_free;
2345
2346 left->type = TEP_PRINT_NULL;
2347 arg->op.left = left;
2348
2349 right = alloc_arg();
2350 if (!right)
2351 goto out_warn_free;
2352
2353 arg->op.right = right;
2354
2355 /* do not free the token, it belongs to an op */
2356 *tok = NULL;
2357 type = process_arg(event, right, tok);
2358
2359 } else if (strcmp(token, "?") == 0) {
2360
2361 left = alloc_arg();
2362 if (!left)
2363 goto out_warn_free;
2364
2365 /* copy the top arg to the left */
2366 *left = *arg;
2367
2368 arg->type = TEP_PRINT_OP;
2369 arg->op.op = token;
2370 arg->op.left = left;
2371 arg->op.right = NULL;
2372 arg->op.prio = 0;
2373
2374 /* it will set arg->op.right */
2375 type = process_cond(event, arg, tok);
2376
2377 } else if (strcmp(token, ">>") == 0 ||
2378 strcmp(token, "<<") == 0 ||
2379 strcmp(token, "&") == 0 ||
2380 strcmp(token, "|") == 0 ||
2381 strcmp(token, "&&") == 0 ||
2382 strcmp(token, "||") == 0 ||
2383 strcmp(token, "-") == 0 ||
2384 strcmp(token, "+") == 0 ||
2385 strcmp(token, "*") == 0 ||
2386 strcmp(token, "^") == 0 ||
2387 strcmp(token, "/") == 0 ||
2388 strcmp(token, "%") == 0 ||
2389 strcmp(token, "<") == 0 ||
2390 strcmp(token, ">") == 0 ||
2391 strcmp(token, "<=") == 0 ||
2392 strcmp(token, ">=") == 0 ||
2393 strcmp(token, "==") == 0 ||
2394 strcmp(token, "!=") == 0) {
2395
2396 left = alloc_arg();
2397 if (!left)
2398 goto out_warn_free;
2399
2400 /* copy the top arg to the left */
2401 *left = *arg;
2402
2403 arg->type = TEP_PRINT_OP;
2404 arg->op.op = token;
2405 arg->op.left = left;
2406 arg->op.right = NULL;
2407
2408 if (set_op_prio(arg) == -1) {
2409 event->flags |= TEP_EVENT_FL_FAILED;
2410 /* arg->op.op (= token) will be freed at out_free */
2411 arg->op.op = NULL;
2412 goto out_free;
2413 }
2414
2415 type = read_token_item(event->tep, &token);
2416 *tok = token;
2417
2418 /* could just be a type pointer */
2419 if ((strcmp(arg->op.op, "*") == 0) &&
2420 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2421 int ret;
2422
2423 if (left->type != TEP_PRINT_ATOM) {
2424 do_warning_event(event, "bad pointer type");
2425 goto out_free;
2426 }
2427 ret = append(&left->atom.atom, " ", "*");
2428 if (ret < 0)
2429 goto out_warn_free;
2430
2431 free(arg->op.op);
2432 *arg = *left;
2433 free(left);
2434
2435 return type;
2436 }
2437
2438 right = alloc_arg();
2439 if (!right)
2440 goto out_warn_free;
2441
2442 type = process_arg_token(event, right, tok, type);
2443 if (type == TEP_EVENT_ERROR) {
2444 free_arg(right);
2445 /* token was freed in process_arg_token() via *tok */
2446 token = NULL;
2447 goto out_free;
2448 }
2449
2450 if (right->type == TEP_PRINT_OP &&
2451 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2452 struct tep_print_arg tmp;
2453
2454 /* rotate ops according to the priority */
2455 arg->op.right = right->op.left;
2456
2457 tmp = *arg;
2458 *arg = *right;
2459 *right = tmp;
2460
2461 arg->op.left = right;
2462 } else {
2463 arg->op.right = right;
2464 }
2465
2466 } else if (strcmp(token, "[") == 0) {
2467
2468 left = alloc_arg();
2469 if (!left)
2470 goto out_warn_free;
2471
2472 *left = *arg;
2473
2474 arg->type = TEP_PRINT_OP;
2475 arg->op.op = token;
2476 arg->op.left = left;
2477 arg->op.right = NULL;
2478
2479 arg->op.prio = 0;
2480
2481 /* it will set arg->op.right */
2482 type = process_array(event, arg, tok);
2483
2484 } else {
2485 do_warning_event(event, "unknown op '%s'", token);
2486 event->flags |= TEP_EVENT_FL_FAILED;
2487 /* the arg is now the left side */
2488 goto out_free;
2489 }
2490
2491 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2492 int prio;
2493
2494 /* higher prios need to be closer to the root */
2495 prio = get_op_prio(*tok);
2496
2497 if (prio > arg->op.prio)
2498 return process_op(event, arg, tok);
2499
2500 return process_op(event, right, tok);
2501 }
2502
2503 return type;
2504
2505 out_warn_free:
2506 do_warning_event(event, "%s: not enough memory!", __func__);
2507 out_free:
2508 free_token(token);
2509 *tok = NULL;
2510 return TEP_EVENT_ERROR;
2511 }
2512
2513 static enum tep_event_type
process_entry(struct tep_event * event __maybe_unused,struct tep_print_arg * arg,char ** tok)2514 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2515 char **tok)
2516 {
2517 enum tep_event_type type;
2518 char *field;
2519 char *token;
2520
2521 type = read_token_item(event->tep, &token);
2522 /*
2523 * Check if REC happens to be surrounded by parenthesis, and
2524 * return if that's the case, as "(REC)->" is valid.
2525 * but return TEP_EVENT_ITEM.
2526 */
2527 if (type == TEP_EVENT_DELIM && strcmp(token, ")") == 0) {
2528 *tok = token;
2529 return TEP_EVENT_ITEM;
2530 }
2531
2532 if (test_type_token(type, token, TEP_EVENT_OP, "->"))
2533 goto out_free;
2534
2535 free_token(token);
2536
2537 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
2538 goto out_free;
2539 field = token;
2540
2541 arg->type = TEP_PRINT_FIELD;
2542 arg->field.name = field;
2543
2544 arg->field.field = tep_find_any_field(event, arg->field.name);
2545
2546 if (is_flag_field) {
2547 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2548 is_flag_field = 0;
2549 } else if (is_symbolic_field) {
2550 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2551 is_symbolic_field = 0;
2552 }
2553
2554 type = read_token(event->tep, &token);
2555 *tok = token;
2556
2557 return type;
2558
2559 out_free:
2560 free_token(token);
2561 *tok = NULL;
2562 return TEP_EVENT_ERROR;
2563 }
2564
alloc_and_process_delim(struct tep_event * event,char * next_token,struct tep_print_arg ** print_arg)2565 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2566 struct tep_print_arg **print_arg)
2567 {
2568 struct tep_print_arg *field;
2569 enum tep_event_type type;
2570 char *token;
2571 int ret = 0;
2572
2573 field = alloc_arg();
2574 if (!field) {
2575 do_warning_event(event, "%s: not enough memory!", __func__);
2576 errno = ENOMEM;
2577 return -1;
2578 }
2579
2580 type = process_arg(event, field, &token);
2581
2582 /* We do allow operators */
2583 if (type == TEP_EVENT_OP) {
2584 type = process_op(event, field, &token);
2585
2586 if (consolidate_op_arg(field) < 0)
2587 type = TEP_EVENT_ERROR;
2588
2589 if (type == TEP_EVENT_ERROR)
2590 goto out_error;
2591 }
2592
2593 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token))
2594 goto out_error;
2595
2596 *print_arg = field;
2597
2598 out_free_token:
2599 free_token(token);
2600
2601 return ret;
2602 out_error:
2603 errno = EINVAL;
2604 ret = -1;
2605 free_arg(field);
2606 goto out_free_token;
2607 }
2608
2609 static char *arg_eval (struct tep_print_arg *arg);
2610
2611 static unsigned long long
eval_type_str(unsigned long long val,const char * type,int pointer)2612 eval_type_str(unsigned long long val, const char *type, int pointer)
2613 {
2614 int sign = 0;
2615 char *ref;
2616 int len;
2617
2618 len = strlen(type);
2619 if (len < 2) {
2620 do_warning("invalid type: %s", type);
2621 return val;
2622 }
2623
2624 if (pointer) {
2625
2626 if (type[len-1] != '*') {
2627 do_warning("pointer expected with non pointer type");
2628 return val;
2629 }
2630
2631 ref = malloc(len);
2632 if (!ref) {
2633 do_warning("%s: not enough memory!", __func__);
2634 return val;
2635 }
2636 memcpy(ref, type, len);
2637
2638 /* chop off the " *" */
2639 ref[len - 2] = 0;
2640
2641 val = eval_type_str(val, ref, 0);
2642 free(ref);
2643 return val;
2644 }
2645
2646 /* check if this is a pointer */
2647 if (type[len - 1] == '*')
2648 return val;
2649
2650 /* Try to figure out the arg size*/
2651 if (strncmp(type, "struct", 6) == 0)
2652 /* all bets off */
2653 return val;
2654
2655 if (strcmp(type, "u8") == 0)
2656 return val & 0xff;
2657
2658 if (strcmp(type, "u16") == 0)
2659 return val & 0xffff;
2660
2661 if (strcmp(type, "u32") == 0)
2662 return val & 0xffffffff;
2663
2664 if (strcmp(type, "u64") == 0 ||
2665 strcmp(type, "s64") == 0)
2666 return val;
2667
2668 if (strcmp(type, "s8") == 0)
2669 return (unsigned long long)(char)val & 0xff;
2670
2671 if (strcmp(type, "s16") == 0)
2672 return (unsigned long long)(short)val & 0xffff;
2673
2674 if (strcmp(type, "s32") == 0)
2675 return (unsigned long long)(int)val & 0xffffffff;
2676
2677 if (strncmp(type, "unsigned ", 9) == 0) {
2678 sign = 0;
2679 type += 9;
2680 }
2681
2682 if (strcmp(type, "char") == 0) {
2683 if (sign)
2684 return (unsigned long long)(char)val & 0xff;
2685 else
2686 return val & 0xff;
2687 }
2688
2689 if (strcmp(type, "short") == 0) {
2690 if (sign)
2691 return (unsigned long long)(short)val & 0xffff;
2692 else
2693 return val & 0xffff;
2694 }
2695
2696 if (strcmp(type, "int") == 0) {
2697 if (sign)
2698 return (unsigned long long)(int)val & 0xffffffff;
2699 else
2700 return val & 0xffffffff;
2701 }
2702
2703 return val;
2704 }
2705
2706 /*
2707 * Try to figure out the type.
2708 */
2709 static unsigned long long
eval_type(unsigned long long val,struct tep_print_arg * arg,int pointer)2710 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2711 {
2712 if (arg->type != TEP_PRINT_TYPE) {
2713 do_warning("expected type argument");
2714 return 0;
2715 }
2716
2717 return eval_type_str(val, arg->typecast.type, pointer);
2718 }
2719
arg_num_eval(struct tep_print_arg * arg,long long * val)2720 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2721 {
2722 long long left, right;
2723 int ret = 1;
2724
2725 switch (arg->type) {
2726 case TEP_PRINT_ATOM:
2727 *val = strtoll(arg->atom.atom, NULL, 0);
2728 break;
2729 case TEP_PRINT_TYPE:
2730 ret = arg_num_eval(arg->typecast.item, val);
2731 if (!ret)
2732 break;
2733 *val = eval_type(*val, arg, 0);
2734 break;
2735 case TEP_PRINT_OP:
2736 switch (arg->op.op[0]) {
2737 case '|':
2738 ret = arg_num_eval(arg->op.left, &left);
2739 if (!ret)
2740 break;
2741 ret = arg_num_eval(arg->op.right, &right);
2742 if (!ret)
2743 break;
2744 if (arg->op.op[1])
2745 *val = left || right;
2746 else
2747 *val = left | right;
2748 break;
2749 case '&':
2750 ret = arg_num_eval(arg->op.left, &left);
2751 if (!ret)
2752 break;
2753 ret = arg_num_eval(arg->op.right, &right);
2754 if (!ret)
2755 break;
2756 if (arg->op.op[1])
2757 *val = left && right;
2758 else
2759 *val = left & right;
2760 break;
2761 case '<':
2762 ret = arg_num_eval(arg->op.left, &left);
2763 if (!ret)
2764 break;
2765 ret = arg_num_eval(arg->op.right, &right);
2766 if (!ret)
2767 break;
2768 switch (arg->op.op[1]) {
2769 case 0:
2770 *val = left < right;
2771 break;
2772 case '<':
2773 *val = left << right;
2774 break;
2775 case '=':
2776 *val = left <= right;
2777 break;
2778 default:
2779 do_warning("unknown op '%s'", arg->op.op);
2780 ret = 0;
2781 }
2782 break;
2783 case '>':
2784 ret = arg_num_eval(arg->op.left, &left);
2785 if (!ret)
2786 break;
2787 ret = arg_num_eval(arg->op.right, &right);
2788 if (!ret)
2789 break;
2790 switch (arg->op.op[1]) {
2791 case 0:
2792 *val = left > right;
2793 break;
2794 case '>':
2795 *val = left >> right;
2796 break;
2797 case '=':
2798 *val = left >= right;
2799 break;
2800 default:
2801 do_warning("unknown op '%s'", arg->op.op);
2802 ret = 0;
2803 }
2804 break;
2805 case '=':
2806 ret = arg_num_eval(arg->op.left, &left);
2807 if (!ret)
2808 break;
2809 ret = arg_num_eval(arg->op.right, &right);
2810 if (!ret)
2811 break;
2812
2813 if (arg->op.op[1] != '=') {
2814 do_warning("unknown op '%s'", arg->op.op);
2815 ret = 0;
2816 } else
2817 *val = left == right;
2818 break;
2819 case '!':
2820 ret = arg_num_eval(arg->op.left, &left);
2821 if (!ret)
2822 break;
2823 ret = arg_num_eval(arg->op.right, &right);
2824 if (!ret)
2825 break;
2826
2827 switch (arg->op.op[1]) {
2828 case '=':
2829 *val = left != right;
2830 break;
2831 default:
2832 do_warning("unknown op '%s'", arg->op.op);
2833 ret = 0;
2834 }
2835 break;
2836 case '-':
2837 /* check for negative */
2838 if (arg->op.left->type == TEP_PRINT_NULL)
2839 left = 0;
2840 else
2841 ret = arg_num_eval(arg->op.left, &left);
2842 if (!ret)
2843 break;
2844 ret = arg_num_eval(arg->op.right, &right);
2845 if (!ret)
2846 break;
2847 *val = left - right;
2848 break;
2849 case '+':
2850 if (arg->op.left->type == TEP_PRINT_NULL)
2851 left = 0;
2852 else
2853 ret = arg_num_eval(arg->op.left, &left);
2854 if (!ret)
2855 break;
2856 ret = arg_num_eval(arg->op.right, &right);
2857 if (!ret)
2858 break;
2859 *val = left + right;
2860 break;
2861 case '~':
2862 ret = arg_num_eval(arg->op.right, &right);
2863 if (!ret)
2864 break;
2865 *val = ~right;
2866 break;
2867 default:
2868 do_warning("unknown op '%s'", arg->op.op);
2869 ret = 0;
2870 }
2871 break;
2872
2873 case TEP_PRINT_NULL:
2874 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2875 case TEP_PRINT_STRING:
2876 case TEP_PRINT_BSTRING:
2877 case TEP_PRINT_BITMASK:
2878 case TEP_PRINT_CPUMASK:
2879 default:
2880 do_warning("invalid eval type %d", arg->type);
2881 ret = 0;
2882
2883 }
2884 return ret;
2885 }
2886
arg_eval(struct tep_print_arg * arg)2887 static char *arg_eval (struct tep_print_arg *arg)
2888 {
2889 long long val;
2890 static char buf[24];
2891
2892 switch (arg->type) {
2893 case TEP_PRINT_ATOM:
2894 return arg->atom.atom;
2895 case TEP_PRINT_TYPE:
2896 return arg_eval(arg->typecast.item);
2897 case TEP_PRINT_OP:
2898 if (!arg_num_eval(arg, &val))
2899 break;
2900 sprintf(buf, "%lld", val);
2901 return buf;
2902
2903 case TEP_PRINT_NULL:
2904 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2905 case TEP_PRINT_STRING:
2906 case TEP_PRINT_BSTRING:
2907 case TEP_PRINT_BITMASK:
2908 case TEP_PRINT_CPUMASK:
2909 default:
2910 do_warning("invalid eval type %d", arg->type);
2911 break;
2912 }
2913
2914 return NULL;
2915 }
2916
2917 static enum tep_event_type
process_fields(struct tep_event * event,struct tep_print_flag_sym ** list,char ** tok)2918 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2919 {
2920 enum tep_event_type type;
2921 struct tep_print_arg *arg = NULL;
2922 struct tep_print_flag_sym *field;
2923 char *token = *tok;
2924 char *value;
2925
2926 do {
2927 free_token(token);
2928 type = read_token_item(event->tep, &token);
2929 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2930 break;
2931
2932 arg = alloc_arg();
2933 if (!arg)
2934 goto out_free;
2935
2936 free_token(token);
2937 type = process_arg(event, arg, &token);
2938
2939 if (type == TEP_EVENT_OP)
2940 type = process_op(event, arg, &token);
2941
2942 if (type == TEP_EVENT_ERROR)
2943 goto out_free;
2944
2945 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2946 goto out_free;
2947
2948 field = calloc(1, sizeof(*field));
2949 if (!field)
2950 goto out_free;
2951
2952 value = arg_eval(arg);
2953 if (value == NULL)
2954 goto out_free_field;
2955 field->value = strdup(value);
2956 if (field->value == NULL)
2957 goto out_free_field;
2958
2959 free_arg(arg);
2960 arg = alloc_arg();
2961 if (!arg)
2962 goto out_free;
2963
2964 free_token(token);
2965 type = process_arg(event, arg, &token);
2966 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2967 goto out_free_field;
2968
2969 value = arg_eval(arg);
2970 if (value == NULL)
2971 goto out_free_field;
2972 field->str = strdup(value);
2973 if (field->str == NULL)
2974 goto out_free_field;
2975 free_arg(arg);
2976 arg = NULL;
2977
2978 *list = field;
2979 list = &field->next;
2980
2981 free_token(token);
2982 type = read_token_item(event->tep, &token);
2983 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2984
2985 *tok = token;
2986 return type;
2987
2988 out_free_field:
2989 free_flag_sym(field);
2990 out_free:
2991 free_arg(arg);
2992 free_token(token);
2993 *tok = NULL;
2994
2995 return TEP_EVENT_ERROR;
2996 }
2997
2998 static enum tep_event_type
process_flags(struct tep_event * event,struct tep_print_arg * arg,char ** tok)2999 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3000 {
3001 struct tep_print_arg *field;
3002 enum tep_event_type type;
3003 char *token = NULL;
3004
3005 memset(arg, 0, sizeof(*arg));
3006 arg->type = TEP_PRINT_FLAGS;
3007
3008 field = alloc_arg();
3009 if (!field) {
3010 do_warning_event(event, "%s: not enough memory!", __func__);
3011 goto out_free;
3012 }
3013
3014 type = process_field_arg(event, field, &token);
3015
3016 /* Handle operations in the first argument */
3017 while (type == TEP_EVENT_OP)
3018 type = process_op(event, field, &token);
3019
3020 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3021 goto out_free_field;
3022 free_token(token);
3023
3024 arg->flags.field = field;
3025
3026 type = read_token_item(event->tep, &token);
3027 if (event_item_type(type)) {
3028 arg->flags.delim = token;
3029 type = read_token_item(event->tep, &token);
3030 }
3031
3032 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3033 goto out_free;
3034
3035 type = process_fields(event, &arg->flags.flags, &token);
3036 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3037 goto out_free;
3038
3039 free_token(token);
3040 type = read_token_item(event->tep, tok);
3041 return type;
3042
3043 out_free_field:
3044 free_arg(field);
3045 out_free:
3046 free_token(token);
3047 *tok = NULL;
3048 return TEP_EVENT_ERROR;
3049 }
3050
3051 static enum tep_event_type
process_symbols(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3052 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3053 {
3054 struct tep_print_arg *field;
3055 enum tep_event_type type;
3056 char *token = NULL;
3057
3058 memset(arg, 0, sizeof(*arg));
3059 arg->type = TEP_PRINT_SYMBOL;
3060
3061 field = alloc_arg();
3062 if (!field) {
3063 do_warning_event(event, "%s: not enough memory!", __func__);
3064 goto out_free;
3065 }
3066
3067 type = process_field_arg(event, field, &token);
3068
3069 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3070 goto out_free_field;
3071
3072 arg->symbol.field = field;
3073
3074 type = process_fields(event, &arg->symbol.symbols, &token);
3075 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3076 goto out_free;
3077
3078 free_token(token);
3079 type = read_token_item(event->tep, tok);
3080 return type;
3081
3082 out_free_field:
3083 free_arg(field);
3084 out_free:
3085 free_token(token);
3086 *tok = NULL;
3087 return TEP_EVENT_ERROR;
3088 }
3089
3090 static enum tep_event_type
process_hex_common(struct tep_event * event,struct tep_print_arg * arg,char ** tok,enum tep_print_arg_type type)3091 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
3092 char **tok, enum tep_print_arg_type type)
3093 {
3094 memset(arg, 0, sizeof(*arg));
3095 arg->type = type;
3096
3097 if (alloc_and_process_delim(event, ",", &arg->hex.field))
3098 goto out;
3099
3100 if (alloc_and_process_delim(event, ")", &arg->hex.size))
3101 goto free_field;
3102
3103 return read_token_item(event->tep, tok);
3104
3105 free_field:
3106 free_arg(arg->hex.field);
3107 arg->hex.field = NULL;
3108 out:
3109 *tok = NULL;
3110 return TEP_EVENT_ERROR;
3111 }
3112
3113 static enum tep_event_type
process_hex(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3114 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3115 {
3116 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
3117 }
3118
3119 static enum tep_event_type
process_hex_str(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3120 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
3121 char **tok)
3122 {
3123 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
3124 }
3125
3126 static enum tep_event_type
process_int_array(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3127 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3128 {
3129 memset(arg, 0, sizeof(*arg));
3130 arg->type = TEP_PRINT_INT_ARRAY;
3131
3132 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
3133 goto out;
3134
3135 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
3136 goto free_field;
3137
3138 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
3139 goto free_size;
3140
3141 return read_token_item(event->tep, tok);
3142
3143 free_size:
3144 free_arg(arg->int_array.count);
3145 arg->int_array.count = NULL;
3146 free_field:
3147 free_arg(arg->int_array.field);
3148 arg->int_array.field = NULL;
3149 out:
3150 *tok = NULL;
3151 return TEP_EVENT_ERROR;
3152 }
3153
3154 static enum tep_event_type
process_dynamic_array(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3155 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3156 {
3157 struct tep_format_field *field;
3158 enum tep_event_type type;
3159 char *token;
3160
3161 memset(arg, 0, sizeof(*arg));
3162 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
3163
3164 /*
3165 * The item within the parenthesis is another field that holds
3166 * the index into where the array starts.
3167 */
3168 type = read_token(event->tep, &token);
3169 *tok = token;
3170 if (type != TEP_EVENT_ITEM)
3171 goto out_free;
3172
3173 /* Find the field */
3174
3175 field = tep_find_field(event, token);
3176 if (!field)
3177 goto out_free;
3178
3179 arg->dynarray.field = field;
3180 arg->dynarray.index = 0;
3181
3182 if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3183 goto out_free;
3184
3185 free_token(token);
3186 type = read_token_item(event->tep, &token);
3187 *tok = token;
3188 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
3189 return type;
3190
3191 free_token(token);
3192 arg = alloc_arg();
3193 if (!arg) {
3194 do_warning_event(event, "%s: not enough memory!", __func__);
3195 *tok = NULL;
3196 return TEP_EVENT_ERROR;
3197 }
3198
3199 type = process_arg(event, arg, &token);
3200 if (type == TEP_EVENT_ERROR)
3201 goto out_free_arg;
3202
3203 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
3204 goto out_free_arg;
3205
3206 free_token(token);
3207 type = read_token_item(event->tep, tok);
3208 return type;
3209
3210 out_free_arg:
3211 free_arg(arg);
3212 out_free:
3213 free_token(token);
3214 *tok = NULL;
3215 return TEP_EVENT_ERROR;
3216 }
3217
3218 static enum tep_event_type
process_dynamic_array_len(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3219 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
3220 char **tok)
3221 {
3222 struct tep_format_field *field;
3223 enum tep_event_type type;
3224 char *token;
3225
3226 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3227 goto out_free;
3228
3229 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
3230
3231 /* Find the field */
3232 field = tep_find_field(event, token);
3233 if (!field)
3234 goto out_free;
3235
3236 arg->dynarray.field = field;
3237 arg->dynarray.index = 0;
3238
3239 if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3240 goto out_err;
3241
3242 free_token(token);
3243 type = read_token(event->tep, &token);
3244 *tok = token;
3245
3246 return type;
3247
3248 out_free:
3249 free_token(token);
3250 out_err:
3251 *tok = NULL;
3252 return TEP_EVENT_ERROR;
3253 }
3254
3255 static enum tep_event_type
process_paren(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3256 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3257 {
3258 struct tep_print_arg *item_arg;
3259 enum tep_event_type type;
3260 char *token;
3261
3262 type = process_arg(event, arg, &token);
3263
3264 if (type == TEP_EVENT_ERROR)
3265 goto out_free;
3266
3267 if (type == TEP_EVENT_OP)
3268 type = process_op(event, arg, &token);
3269
3270 if (type == TEP_EVENT_ERROR)
3271 goto out_free;
3272
3273 /*
3274 * If REC is surrounded by parenthesis, the process_arg()
3275 * will return TEP_EVENT_ITEM with token == ")". In
3276 * this case, we need to continue processing the item
3277 * and return.
3278 */
3279 if (type == TEP_EVENT_ITEM && strcmp(token, ")") == 0) {
3280 free_token(token);
3281 return process_entry(event, arg, tok);
3282 }
3283
3284 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3285 goto out_free;
3286
3287 free_token(token);
3288 type = read_token_item(event->tep, &token);
3289
3290 /*
3291 * If the next token is an item or another open paren, then
3292 * this was a typecast.
3293 */
3294 if (event_item_type(type) ||
3295 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
3296
3297 /* make this a typecast and contine */
3298
3299 /* prevous must be an atom */
3300 if (arg->type != TEP_PRINT_ATOM) {
3301 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
3302 goto out_free;
3303 }
3304
3305 item_arg = alloc_arg();
3306 if (!item_arg) {
3307 do_warning_event(event, "%s: not enough memory!",
3308 __func__);
3309 goto out_free;
3310 }
3311
3312 arg->type = TEP_PRINT_TYPE;
3313 arg->typecast.type = arg->atom.atom;
3314 arg->typecast.item = item_arg;
3315 type = process_arg_token(event, item_arg, &token, type);
3316
3317 }
3318
3319 *tok = token;
3320 return type;
3321
3322 out_free:
3323 free_token(token);
3324 *tok = NULL;
3325 return TEP_EVENT_ERROR;
3326 }
3327
3328
3329 static enum tep_event_type
process_str(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3330 process_str(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3331 {
3332 enum tep_event_type type;
3333 char *token;
3334
3335 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3336 goto out_free;
3337
3338 arg->type = TEP_PRINT_STRING;
3339 arg->string.string = token;
3340 arg->string.offset = -1;
3341 arg->string.field = NULL;
3342
3343 if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3344 goto out_err;
3345
3346 type = read_token(event->tep, &token);
3347 *tok = token;
3348
3349 return type;
3350
3351 out_free:
3352 free_token(token);
3353 out_err:
3354 *tok = NULL;
3355 return TEP_EVENT_ERROR;
3356 }
3357
3358 static enum tep_event_type
process_bitmask(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3359 process_bitmask(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3360 {
3361 enum tep_event_type type;
3362 char *token;
3363
3364 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3365 goto out_free;
3366
3367 arg->type = TEP_PRINT_BITMASK;
3368 arg->bitmask.bitmask = token;
3369 arg->bitmask.offset = -1;
3370 arg->bitmask.field = NULL;
3371
3372 if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3373 goto out_err;
3374
3375 type = read_token(event->tep, &token);
3376 *tok = token;
3377
3378 return type;
3379
3380 out_free:
3381 free_token(token);
3382 out_err:
3383 *tok = NULL;
3384 return TEP_EVENT_ERROR;
3385 }
3386
3387 static enum tep_event_type
process_cpumask(struct tep_event * event __maybe_unused,struct tep_print_arg * arg,char ** tok)3388 process_cpumask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
3389 char **tok)
3390 {
3391 enum tep_event_type type = process_bitmask(event, arg, tok);
3392 if (type != TEP_EVENT_ERROR)
3393 arg->type = TEP_PRINT_CPUMASK;
3394
3395 return type;
3396 }
3397
3398 static struct tep_function_handler *
find_func_handler(struct tep_handle * tep,char * func_name)3399 find_func_handler(struct tep_handle *tep, char *func_name)
3400 {
3401 struct tep_function_handler *func;
3402
3403 if (!tep)
3404 return NULL;
3405
3406 for (func = tep->func_handlers; func; func = func->next) {
3407 if (strcmp(func->name, func_name) == 0)
3408 break;
3409 }
3410
3411 return func;
3412 }
3413
remove_func_handler(struct tep_handle * tep,char * func_name)3414 static void remove_func_handler(struct tep_handle *tep, char *func_name)
3415 {
3416 struct tep_function_handler *func;
3417 struct tep_function_handler **next;
3418
3419 next = &tep->func_handlers;
3420 while ((func = *next)) {
3421 if (strcmp(func->name, func_name) == 0) {
3422 *next = func->next;
3423 free_func_handle(func);
3424 break;
3425 }
3426 next = &func->next;
3427 }
3428 }
3429
3430 static enum tep_event_type
process_func_handler(struct tep_event * event,struct tep_function_handler * func,struct tep_print_arg * arg,char ** tok)3431 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
3432 struct tep_print_arg *arg, char **tok)
3433 {
3434 struct tep_print_arg **next_arg;
3435 struct tep_print_arg *farg;
3436 enum tep_event_type type;
3437 char *token;
3438 int i;
3439
3440 arg->type = TEP_PRINT_FUNC;
3441 arg->func.func = func;
3442
3443 *tok = NULL;
3444
3445 next_arg = &(arg->func.args);
3446 for (i = 0; i < func->nr_args; i++) {
3447 farg = alloc_arg();
3448 if (!farg) {
3449 do_warning_event(event, "%s: not enough memory!",
3450 __func__);
3451 return TEP_EVENT_ERROR;
3452 }
3453
3454 type = process_arg(event, farg, &token);
3455 if (i < (func->nr_args - 1)) {
3456 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3457 do_warning_event(event,
3458 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3459 func->name, func->nr_args,
3460 event->name, i + 1);
3461 goto err;
3462 }
3463 } else {
3464 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3465 do_warning_event(event,
3466 "Error: function '%s()' only expects %d arguments but event %s has more",
3467 func->name, func->nr_args, event->name);
3468 goto err;
3469 }
3470 }
3471
3472 *next_arg = farg;
3473 next_arg = &(farg->next);
3474 free_token(token);
3475 }
3476
3477 type = read_token(event->tep, &token);
3478 *tok = token;
3479
3480 return type;
3481
3482 err:
3483 free_arg(farg);
3484 free_token(token);
3485 return TEP_EVENT_ERROR;
3486 }
3487
3488 static enum tep_event_type
process_builtin_expect(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3489 process_builtin_expect(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3490 {
3491 enum tep_event_type type;
3492 char *token = NULL;
3493
3494 /* Handle __builtin_expect( cond, #) */
3495 type = process_arg(event, arg, &token);
3496
3497 if (type != TEP_EVENT_DELIM || token[0] != ',')
3498 goto out_free;
3499
3500 free_token(token);
3501
3502 /* We don't care what the second parameter is of the __builtin_expect() */
3503 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3504 goto out_free;
3505
3506 if (read_expected(event->tep, TEP_EVENT_DELIM, ")") < 0)
3507 goto out_free;
3508
3509 free_token(token);
3510 type = read_token_item(event->tep, tok);
3511 return type;
3512
3513 out_free:
3514 free_token(token);
3515 *tok = NULL;
3516 return TEP_EVENT_ERROR;
3517 }
3518
3519 static enum tep_event_type
process_sizeof(struct tep_event * event,struct tep_print_arg * arg,char ** tok)3520 process_sizeof(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3521 {
3522 struct tep_format_field *field;
3523 enum tep_event_type type;
3524 char *token = NULL;
3525 bool ok = false;
3526 int ret;
3527
3528 type = read_token_item(event->tep, &token);
3529
3530 arg->type = TEP_PRINT_ATOM;
3531
3532 /* We handle some sizeof types */
3533 if (strcmp(token, "unsigned") == 0) {
3534 free_token(token);
3535 type = read_token_item(event->tep, &token);
3536
3537 if (type == TEP_EVENT_ERROR)
3538 goto error;
3539
3540 if (type != TEP_EVENT_ITEM)
3541 ok = true;
3542 }
3543
3544 if (ok || strcmp(token, "int") == 0) {
3545 arg->atom.atom = strdup("4");
3546
3547 } else if (strcmp(token, "long") == 0) {
3548 free_token(token);
3549 type = read_token_item(event->tep, &token);
3550
3551 if (token && strcmp(token, "long") == 0) {
3552 arg->atom.atom = strdup("8");
3553 } else {
3554 switch (event->tep->long_size) {
3555 case 4:
3556 arg->atom.atom = strdup("4");
3557 break;
3558 case 8:
3559 arg->atom.atom = strdup("8");
3560 break;
3561 default:
3562 /* long size not defined yet, fail to parse it */
3563 goto error;
3564 }
3565 /* The token is the next token */
3566 ok = true;
3567 }
3568 } else if (strcmp(token, "REC") == 0) {
3569
3570 free_token(token);
3571 type = read_token_item(event->tep, &token);
3572
3573 if (test_type_token(type, token, TEP_EVENT_OP, "->"))
3574 goto error;
3575 free_token(token);
3576
3577 if (read_expect_type(event->tep, TEP_EVENT_ITEM, &token) < 0)
3578 goto error;
3579
3580 field = tep_find_any_field(event, token);
3581 /* Can't handle arrays (yet) */
3582 if (!field || field->flags & TEP_FIELD_IS_ARRAY)
3583 goto error;
3584
3585 ret = asprintf(&arg->atom.atom, "%d", field->size);
3586 if (ret < 0)
3587 goto error;
3588
3589 } else if (!ok) {
3590 goto error;
3591 }
3592
3593 if (!ok) {
3594 free_token(token);
3595 type = read_token_item(event->tep, tok);
3596 }
3597 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
3598 goto error;
3599
3600 free_token(token);
3601 return read_token_item(event->tep, tok);
3602 error:
3603 free_token(token);
3604 *tok = NULL;
3605 return TEP_EVENT_ERROR;
3606 }
3607
3608 static enum tep_event_type
process_function(struct tep_event * event,struct tep_print_arg * arg,char * token,char ** tok)3609 process_function(struct tep_event *event, struct tep_print_arg *arg,
3610 char *token, char **tok)
3611 {
3612 struct tep_function_handler *func;
3613
3614 if (strcmp(token, "__print_flags") == 0) {
3615 free_token(token);
3616 is_flag_field = 1;
3617 return process_flags(event, arg, tok);
3618 }
3619 if (strcmp(token, "__print_symbolic") == 0) {
3620 free_token(token);
3621 is_symbolic_field = 1;
3622 return process_symbols(event, arg, tok);
3623 }
3624 if (strcmp(token, "__print_hex") == 0) {
3625 free_token(token);
3626 return process_hex(event, arg, tok);
3627 }
3628 if (strcmp(token, "__print_hex_str") == 0) {
3629 free_token(token);
3630 return process_hex_str(event, arg, tok);
3631 }
3632 if (strcmp(token, "__print_array") == 0) {
3633 free_token(token);
3634 return process_int_array(event, arg, tok);
3635 }
3636 if (strcmp(token, "__get_str") == 0 ||
3637 strcmp(token, "__get_rel_str") == 0) {
3638 free_token(token);
3639 return process_str(event, arg, tok);
3640 }
3641 if (strcmp(token, "__get_bitmask") == 0 ||
3642 strcmp(token, "__get_rel_bitmask") == 0) {
3643 free_token(token);
3644 return process_bitmask(event, arg, tok);
3645 }
3646 if (strcmp(token, "__get_cpumask") == 0 ||
3647 strcmp(token, "__get_rel_cpumask") == 0) {
3648 free_token(token);
3649 return process_cpumask(event, arg, tok);
3650 }
3651 if (strcmp(token, "__get_dynamic_array") == 0 ||
3652 strcmp(token, "__get_rel_dynamic_array") == 0 ||
3653 strcmp(token, "__get_sockaddr") == 0 ||
3654 strcmp(token, "__get_sockaddr_rel") == 0) {
3655 free_token(token);
3656 return process_dynamic_array(event, arg, tok);
3657 }
3658 if (strcmp(token, "__get_dynamic_array_len") == 0 ||
3659 strcmp(token, "__get_rel_dynamic_array_len") == 0) {
3660 free_token(token);
3661 return process_dynamic_array_len(event, arg, tok);
3662 }
3663 if (strcmp(token, "__builtin_expect") == 0) {
3664 free_token(token);
3665 return process_builtin_expect(event, arg, tok);
3666 }
3667 if (strcmp(token, "sizeof") == 0) {
3668 free_token(token);
3669 return process_sizeof(event, arg, tok);
3670 }
3671
3672 func = find_func_handler(event->tep, token);
3673 if (func) {
3674 free_token(token);
3675 return process_func_handler(event, func, arg, tok);
3676 }
3677
3678 do_warning_event(event, "function %s not defined", token);
3679 free_token(token);
3680 return TEP_EVENT_ERROR;
3681 }
3682
3683 static enum tep_event_type
process_arg_token(struct tep_event * event,struct tep_print_arg * arg,char ** tok,enum tep_event_type type)3684 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3685 char **tok, enum tep_event_type type)
3686 {
3687 char *token;
3688 char *atom;
3689
3690 token = *tok;
3691
3692 switch (type) {
3693 case TEP_EVENT_ITEM:
3694 if (strcmp(token, "REC") == 0) {
3695 free_token(token);
3696 type = process_entry(event, arg, &token);
3697 break;
3698 }
3699 atom = token;
3700 /* test the next token */
3701 type = read_token_item(event->tep, &token);
3702
3703 /*
3704 * If the next token is a parenthesis, then this
3705 * is a function.
3706 */
3707 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3708 free_token(token);
3709 token = NULL;
3710 /* this will free atom. */
3711 type = process_function(event, arg, atom, &token);
3712 break;
3713 }
3714 /* atoms can be more than one token long */
3715 while (type == TEP_EVENT_ITEM) {
3716 int ret;
3717
3718 ret = append(&atom, " ", token);
3719 if (ret < 0) {
3720 free(atom);
3721 *tok = NULL;
3722 free_token(token);
3723 return TEP_EVENT_ERROR;
3724 }
3725 free_token(token);
3726 type = read_token_item(event->tep, &token);
3727 }
3728
3729 arg->type = TEP_PRINT_ATOM;
3730 arg->atom.atom = atom;
3731 break;
3732
3733 case TEP_EVENT_DQUOTE:
3734 case TEP_EVENT_SQUOTE:
3735 arg->type = TEP_PRINT_ATOM;
3736 arg->atom.atom = token;
3737 type = read_token_item(event->tep, &token);
3738 break;
3739 case TEP_EVENT_DELIM:
3740 if (strcmp(token, "(") == 0) {
3741 free_token(token);
3742 type = process_paren(event, arg, &token);
3743 break;
3744 }
3745 case TEP_EVENT_OP:
3746 /* handle single ops */
3747 arg->type = TEP_PRINT_OP;
3748 arg->op.op = token;
3749 arg->op.left = NULL;
3750 type = process_op(event, arg, &token);
3751
3752 /* On error, the op is freed */
3753 if (type == TEP_EVENT_ERROR)
3754 arg->op.op = NULL;
3755
3756 /* return error type if errored */
3757 break;
3758
3759 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3760 default:
3761 do_warning_event(event, "unexpected type %d", type);
3762 return TEP_EVENT_ERROR;
3763 }
3764 *tok = token;
3765
3766 return type;
3767 }
3768
event_read_print_args(struct tep_event * event,struct tep_print_arg ** list)3769 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3770 {
3771 enum tep_event_type type = TEP_EVENT_ERROR;
3772 struct tep_print_arg *arg;
3773 char *token;
3774 int args = 0;
3775
3776 do {
3777 if (type == TEP_EVENT_NEWLINE) {
3778 type = read_token_item(event->tep, &token);
3779 continue;
3780 }
3781
3782 arg = alloc_arg();
3783 if (!arg) {
3784 do_warning_event(event, "%s: not enough memory!",
3785 __func__);
3786 return -1;
3787 }
3788
3789 type = process_arg(event, arg, &token);
3790
3791 if (type == TEP_EVENT_ERROR) {
3792 free_token(token);
3793 free_arg(arg);
3794 return -1;
3795 }
3796
3797 *list = arg;
3798 args++;
3799
3800 if (type == TEP_EVENT_OP) {
3801 type = process_op(event, arg, &token);
3802 free_token(token);
3803
3804 if (consolidate_op_arg(arg) < 0)
3805 type = TEP_EVENT_ERROR;
3806
3807 if (type == TEP_EVENT_ERROR) {
3808 *list = NULL;
3809 free_arg(arg);
3810 return -1;
3811 }
3812 list = &arg->next;
3813 continue;
3814 }
3815
3816 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3817 free_token(token);
3818 *list = arg;
3819 list = &arg->next;
3820 continue;
3821 }
3822 break;
3823 } while (type != TEP_EVENT_NONE);
3824
3825 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3826 free_token(token);
3827
3828 return args;
3829 }
3830
event_read_print(struct tep_event * event)3831 static int event_read_print(struct tep_event *event)
3832 {
3833 enum tep_event_type type;
3834 char *token;
3835 int ret;
3836
3837 if (read_expected_item(event->tep, TEP_EVENT_ITEM, "print") < 0)
3838 return -1;
3839
3840 if (read_expected(event->tep, TEP_EVENT_ITEM, "fmt") < 0)
3841 return -1;
3842
3843 if (read_expected(event->tep, TEP_EVENT_OP, ":") < 0)
3844 return -1;
3845
3846 if (read_expect_type(event->tep, TEP_EVENT_DQUOTE, &token) < 0)
3847 goto fail;
3848
3849 concat:
3850 event->print_fmt.format = token;
3851 event->print_fmt.args = NULL;
3852
3853 /* ok to have no arg */
3854 type = read_token_item(event->tep, &token);
3855
3856 if (type == TEP_EVENT_NONE)
3857 return 0;
3858
3859 /* Handle concatenation of print lines */
3860 if (type == TEP_EVENT_DQUOTE) {
3861 char *cat;
3862
3863 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3864 goto fail;
3865 free_token(token);
3866 free_token(event->print_fmt.format);
3867 event->print_fmt.format = NULL;
3868 token = cat;
3869 goto concat;
3870 }
3871
3872 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3873 goto fail;
3874
3875 free_token(token);
3876
3877 ret = event_read_print_args(event, &event->print_fmt.args);
3878 if (ret < 0)
3879 return -1;
3880
3881 return ret;
3882
3883 fail:
3884 free_token(token);
3885 return -1;
3886 }
3887
3888 /**
3889 * tep_find_common_field - return a common field by event
3890 * @event: handle for the event
3891 * @name: the name of the common field to return
3892 *
3893 * Returns a common field from the event by the given @name.
3894 * This only searches the common fields and not all field.
3895 */
3896 struct tep_format_field *
tep_find_common_field(struct tep_event * event,const char * name)3897 tep_find_common_field(struct tep_event *event, const char *name)
3898 {
3899 struct tep_format_field *format;
3900
3901 for (format = event->format.common_fields;
3902 format; format = format->next) {
3903 if (strcmp(format->name, name) == 0)
3904 break;
3905 }
3906
3907 return format;
3908 }
3909
3910 /**
3911 * tep_find_field - find a non-common field
3912 * @event: handle for the event
3913 * @name: the name of the non-common field
3914 *
3915 * Returns a non-common field by the given @name.
3916 * This does not search common fields.
3917 */
3918 struct tep_format_field *
tep_find_field(struct tep_event * event,const char * name)3919 tep_find_field(struct tep_event *event, const char *name)
3920 {
3921 struct tep_format_field *format;
3922
3923 for (format = event->format.fields;
3924 format; format = format->next) {
3925 if (strcmp(format->name, name) == 0)
3926 break;
3927 }
3928
3929 return format;
3930 }
3931
3932 /**
3933 * tep_find_any_field - find any field by name
3934 * @event: handle for the event
3935 * @name: the name of the field
3936 *
3937 * Returns a field by the given @name.
3938 * This searches the common field names first, then
3939 * the non-common ones if a common one was not found.
3940 */
3941 struct tep_format_field *
tep_find_any_field(struct tep_event * event,const char * name)3942 tep_find_any_field(struct tep_event *event, const char *name)
3943 {
3944 struct tep_format_field *format;
3945
3946 format = tep_find_common_field(event, name);
3947 if (format)
3948 return format;
3949 return tep_find_field(event, name);
3950 }
3951
3952 /**
3953 * tep_read_number - read a number from data
3954 * @tep: a handle to the trace event parser context
3955 * @ptr: the raw data
3956 * @size: the size of the data that holds the number
3957 *
3958 * Returns the number (converted to host) from the
3959 * raw data.
3960 */
tep_read_number(struct tep_handle * tep,const void * ptr,int size)3961 unsigned long long tep_read_number(struct tep_handle *tep,
3962 const void *ptr, int size)
3963 {
3964 unsigned long long val;
3965
3966 switch (size) {
3967 case 1:
3968 return *(unsigned char *)ptr;
3969 case 2:
3970 return data2host2(tep, *(unsigned short *)ptr);
3971 case 4:
3972 return data2host4(tep, *(unsigned int *)ptr);
3973 case 8:
3974 memcpy(&val, (ptr), sizeof(unsigned long long));
3975 return data2host8(tep, val);
3976 default:
3977 /* BUG! */
3978 return 0;
3979 }
3980 }
3981
3982 /**
3983 * tep_read_number_field - read a number from data
3984 * @field: a handle to the field
3985 * @data: the raw data to read
3986 * @value: the value to place the number in
3987 *
3988 * Reads raw data according to a field offset and size,
3989 * and translates it into @value.
3990 *
3991 * Returns 0 on success, -1 otherwise.
3992 */
tep_read_number_field(struct tep_format_field * field,const void * data,unsigned long long * value)3993 int tep_read_number_field(struct tep_format_field *field, const void *data,
3994 unsigned long long *value)
3995 {
3996 if (!field)
3997 return -1;
3998 switch (field->size) {
3999 case 1:
4000 case 2:
4001 case 4:
4002 case 8:
4003 *value = tep_read_number(field->event->tep,
4004 data + field->offset, field->size);
4005 return 0;
4006 default:
4007 return -1;
4008 }
4009 }
4010
get_common_info(struct tep_handle * tep,const char * type,int * offset,int * size)4011 static int get_common_info(struct tep_handle *tep,
4012 const char *type, int *offset, int *size)
4013 {
4014 struct tep_event *event;
4015 struct tep_format_field *field;
4016
4017 /*
4018 * All events should have the same common elements.
4019 * Pick any event to find where the type is;
4020 */
4021 if (!tep->events) {
4022 do_warning("no event_list!");
4023 return -1;
4024 }
4025
4026 event = tep->events[0];
4027 field = tep_find_common_field(event, type);
4028 if (!field)
4029 return -1;
4030
4031 *offset = field->offset;
4032 *size = field->size;
4033
4034 return 0;
4035 }
4036
__parse_common(struct tep_handle * tep,void * data,int * size,int * offset,const char * name)4037 static int __parse_common(struct tep_handle *tep, void *data,
4038 int *size, int *offset, const char *name)
4039 {
4040 int ret;
4041
4042 if (!*size) {
4043 ret = get_common_info(tep, name, offset, size);
4044 if (ret < 0)
4045 return ret;
4046 }
4047 return tep_read_number(tep, data + *offset, *size);
4048 }
4049
trace_parse_common_type(struct tep_handle * tep,void * data)4050 static int trace_parse_common_type(struct tep_handle *tep, void *data)
4051 {
4052 return __parse_common(tep, data,
4053 &tep->type_size, &tep->type_offset,
4054 "common_type");
4055 }
4056
parse_common_pid(struct tep_handle * tep,void * data)4057 static int parse_common_pid(struct tep_handle *tep, void *data)
4058 {
4059 return __parse_common(tep, data,
4060 &tep->pid_size, &tep->pid_offset,
4061 "common_pid");
4062 }
4063
parse_common_pc(struct tep_handle * tep,void * data)4064 static int parse_common_pc(struct tep_handle *tep, void *data)
4065 {
4066 return __parse_common(tep, data,
4067 &tep->pc_size, &tep->pc_offset,
4068 "common_preempt_count");
4069 }
4070
parse_common_flags(struct tep_handle * tep,void * data)4071 static int parse_common_flags(struct tep_handle *tep, void *data)
4072 {
4073 return __parse_common(tep, data,
4074 &tep->flags_size, &tep->flags_offset,
4075 "common_flags");
4076 }
4077
parse_common_lock_depth(struct tep_handle * tep,void * data)4078 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
4079 {
4080 return __parse_common(tep, data,
4081 &tep->ld_size, &tep->ld_offset,
4082 "common_lock_depth");
4083 }
4084
parse_common_migrate_disable(struct tep_handle * tep,void * data)4085 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
4086 {
4087 return __parse_common(tep, data,
4088 &tep->ld_size, &tep->ld_offset,
4089 "common_migrate_disable");
4090 }
4091
4092 static int events_id_cmp(const void *a, const void *b);
4093
4094 /**
4095 * tep_find_event - find an event by given id
4096 * @tep: a handle to the trace event parser context
4097 * @id: the id of the event
4098 *
4099 * Returns an event that has a given @id.
4100 */
tep_find_event(struct tep_handle * tep,int id)4101 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
4102 {
4103 struct tep_event **eventptr;
4104 struct tep_event key;
4105 struct tep_event *pkey = &key;
4106
4107 /* Check cache first */
4108 if (tep->last_event && tep->last_event->id == id)
4109 return tep->last_event;
4110
4111 key.id = id;
4112
4113 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
4114 sizeof(*tep->events), events_id_cmp);
4115
4116 if (eventptr) {
4117 tep->last_event = *eventptr;
4118 return *eventptr;
4119 }
4120
4121 return NULL;
4122 }
4123
4124 /**
4125 * tep_find_event_by_name - find an event by given name
4126 * @tep: a handle to the trace event parser context
4127 * @sys: the system name to search for
4128 * @name: the name of the event to search for
4129 *
4130 * This returns an event with a given @name and under the system
4131 * @sys. If @sys is NULL the first event with @name is returned.
4132 */
4133 struct tep_event *
tep_find_event_by_name(struct tep_handle * tep,const char * sys,const char * name)4134 tep_find_event_by_name(struct tep_handle *tep,
4135 const char *sys, const char *name)
4136 {
4137 struct tep_event *event = NULL;
4138 int i;
4139
4140 if (tep->last_event &&
4141 strcmp(tep->last_event->name, name) == 0 &&
4142 (!sys || strcmp(tep->last_event->system, sys) == 0))
4143 return tep->last_event;
4144
4145 for (i = 0; i < tep->nr_events; i++) {
4146 event = tep->events[i];
4147 if (strcmp(event->name, name) == 0) {
4148 if (!sys)
4149 break;
4150 if (strcmp(event->system, sys) == 0)
4151 break;
4152 }
4153 }
4154 if (i == tep->nr_events)
4155 event = NULL;
4156
4157 tep->last_event = event;
4158 return event;
4159 }
4160
test_for_symbol(struct tep_handle * tep,struct tep_print_arg * arg)4161 static unsigned long long test_for_symbol(struct tep_handle *tep,
4162 struct tep_print_arg *arg)
4163 {
4164 unsigned long long val = 0;
4165 struct func_list *item = tep->funclist;
4166 char *func;
4167 int i;
4168
4169 if (isdigit(arg->atom.atom[0]))
4170 return 0;
4171
4172 /* Linear search but only happens once (see after the loop) */
4173 for (i = 0; i < (int)tep->func_count; i++) {
4174 unsigned long long addr;
4175 const char *name;
4176
4177 if (tep->func_map) {
4178 addr = tep->func_map[i].addr;
4179 name = tep->func_map[i].func;
4180 } else if (item) {
4181 addr = item->addr;
4182 name = item->func;
4183 item = item->next;
4184 } else
4185 break;
4186
4187 if (strcmp(arg->atom.atom, name) == 0) {
4188 val = addr;
4189 break;
4190 }
4191 }
4192
4193 /*
4194 * This modifies the arg to hardcode the value
4195 * and will not loop again.
4196 */
4197 func = realloc(arg->atom.atom, 32);
4198 if (func) {
4199 snprintf(func, 32, "%lld", val);
4200 arg->atom.atom = func;
4201 }
4202 return val;
4203 }
4204
4205 #define TEP_OFFSET_LEN_MASK 0xffff
4206 #define TEP_LEN_SHIFT 16
4207
dynamic_offset(struct tep_handle * tep,int size,void * data,int data_size,unsigned int * offset,unsigned int * len)4208 static void dynamic_offset(struct tep_handle *tep, int size, void *data,
4209 int data_size, unsigned int *offset, unsigned int *len)
4210 {
4211 unsigned long long val;
4212 unsigned int o, l;
4213
4214 /*
4215 * The total allocated length of the dynamic array is
4216 * stored in the top half of the field and the offset
4217 * is in the bottom half of the 32 bit field.
4218 */
4219 val = tep_read_number(tep, data, size);
4220
4221 /* Check for overflows */
4222 o = (unsigned int)(val & TEP_OFFSET_LEN_MASK);
4223
4224 /* If there's no length, then just make the length the size of the data */
4225 if (size == 2)
4226 l = data_size - o;
4227 else
4228 l = (unsigned int)((val >> TEP_LEN_SHIFT) & TEP_OFFSET_LEN_MASK);
4229
4230 if (offset)
4231 *offset = o > data_size ? 0 : o;
4232 if (len)
4233 *len = o + l > data_size ? 0 : l;
4234 }
4235
dynamic_offset_field(struct tep_handle * tep,struct tep_format_field * field,void * data,int size,unsigned int * offset,unsigned int * len)4236 static inline void dynamic_offset_field(struct tep_handle *tep,
4237 struct tep_format_field *field,
4238 void *data, int size,
4239 unsigned int *offset,
4240 unsigned int *len)
4241 {
4242 /* Test for overflow */
4243 if (field->offset + field->size > size) {
4244 if (offset)
4245 *offset = 0;
4246 if (len)
4247 *len = 0;
4248 return;
4249 }
4250 dynamic_offset(tep, field->size, data + field->offset, size, offset, len);
4251 if (field->flags & TEP_FIELD_IS_RELATIVE)
4252 *offset += field->offset + field->size;
4253 }
4254
check_data_offset_size(struct tep_event * event,const char * field_name,int data_size,int field_offset,int field_size)4255 static bool check_data_offset_size(struct tep_event *event, const char *field_name,
4256 int data_size, int field_offset, int field_size)
4257 {
4258 /* Check to make sure the field is within the data */
4259 if (field_offset + field_size <= data_size)
4260 return false;
4261
4262 tep_warning("Event '%s' field '%s' goes beyond the size of the event (%d > %d)",
4263 event->name, field_name, field_offset + field_size, data_size);
4264 return true;
4265 }
4266
4267 static unsigned long long
eval_num_arg(void * data,int size,struct tep_event * event,struct tep_print_arg * arg)4268 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
4269 {
4270 struct tep_handle *tep = event->tep;
4271 unsigned long long val = 0;
4272 unsigned long long left, right;
4273 struct tep_print_arg *typearg = NULL;
4274 struct tep_print_arg *larg;
4275 unsigned int offset;
4276 unsigned int field_size;
4277
4278 switch (arg->type) {
4279 case TEP_PRINT_NULL:
4280 /* ?? */
4281 return 0;
4282 case TEP_PRINT_ATOM:
4283 val = strtoull(arg->atom.atom, NULL, 0);
4284 if (!val)
4285 val = test_for_symbol(tep, arg);
4286 return val;
4287 case TEP_PRINT_FIELD:
4288 if (!arg->field.field) {
4289 arg->field.field = tep_find_any_field(event, arg->field.name);
4290 if (!arg->field.field)
4291 goto out_warning_field;
4292 }
4293 if (check_data_offset_size(event, arg->field.name, size,
4294 arg->field.field->offset,
4295 arg->field.field->size)) {
4296 val = 0;
4297 break;
4298 }
4299 /* must be a number */
4300 val = tep_read_number(tep, data + arg->field.field->offset,
4301 arg->field.field->size);
4302 break;
4303 case TEP_PRINT_FLAGS:
4304 case TEP_PRINT_SYMBOL:
4305 case TEP_PRINT_INT_ARRAY:
4306 case TEP_PRINT_HEX:
4307 case TEP_PRINT_HEX_STR:
4308 break;
4309 case TEP_PRINT_TYPE:
4310 val = eval_num_arg(data, size, event, arg->typecast.item);
4311 return eval_type(val, arg, 0);
4312 case TEP_PRINT_STRING:
4313 case TEP_PRINT_BSTRING:
4314 case TEP_PRINT_BITMASK:
4315 case TEP_PRINT_CPUMASK:
4316 return 0;
4317 case TEP_PRINT_FUNC: {
4318 struct trace_seq s;
4319 trace_seq_init(&s);
4320 val = process_defined_func(&s, data, size, event, arg);
4321 trace_seq_destroy(&s);
4322 return val;
4323 }
4324 case TEP_PRINT_OP:
4325 if (strcmp(arg->op.op, "[") == 0) {
4326 /*
4327 * Arrays are special, since we don't want
4328 * to read the arg as is.
4329 */
4330 right = eval_num_arg(data, size, event, arg->op.right);
4331
4332 /* handle typecasts */
4333 larg = arg->op.left;
4334 while (larg->type == TEP_PRINT_TYPE) {
4335 if (!typearg)
4336 typearg = larg;
4337 larg = larg->typecast.item;
4338 }
4339
4340 /* Default to long size */
4341 field_size = tep->long_size;
4342
4343 switch (larg->type) {
4344 case TEP_PRINT_DYNAMIC_ARRAY:
4345 dynamic_offset_field(tep, larg->dynarray.field, data,
4346 size, &offset, NULL);
4347 offset += right;
4348 if (larg->dynarray.field->elementsize)
4349 field_size = larg->dynarray.field->elementsize;
4350 break;
4351 case TEP_PRINT_FIELD:
4352 if (!larg->field.field) {
4353 larg->field.field =
4354 tep_find_any_field(event, larg->field.name);
4355 if (!larg->field.field) {
4356 arg = larg;
4357 goto out_warning_field;
4358 }
4359 }
4360 field_size = larg->field.field->elementsize;
4361 offset = larg->field.field->offset +
4362 right * larg->field.field->elementsize;
4363 break;
4364 default:
4365 goto default_op; /* oops, all bets off */
4366 }
4367 if (check_data_offset_size(event, arg->field.name, size,
4368 offset, field_size)) {
4369 val = 0;
4370 break;
4371 }
4372 val = tep_read_number(tep,
4373 data + offset, field_size);
4374 if (typearg)
4375 val = eval_type(val, typearg, 1);
4376 break;
4377 } else if (strcmp(arg->op.op, "?") == 0) {
4378 left = eval_num_arg(data, size, event, arg->op.left);
4379 arg = arg->op.right;
4380 if (left)
4381 val = eval_num_arg(data, size, event, arg->op.left);
4382 else
4383 val = eval_num_arg(data, size, event, arg->op.right);
4384 break;
4385 }
4386 default_op:
4387 left = eval_num_arg(data, size, event, arg->op.left);
4388 right = eval_num_arg(data, size, event, arg->op.right);
4389 switch (arg->op.op[0]) {
4390 case '!':
4391 switch (arg->op.op[1]) {
4392 case 0:
4393 val = !right;
4394 break;
4395 case '=':
4396 val = left != right;
4397 break;
4398 default:
4399 goto out_warning_op;
4400 }
4401 break;
4402 case '~':
4403 val = ~right;
4404 break;
4405 case '|':
4406 if (arg->op.op[1])
4407 val = left || right;
4408 else
4409 val = left | right;
4410 break;
4411 case '&':
4412 if (arg->op.op[1])
4413 val = left && right;
4414 else
4415 val = left & right;
4416 break;
4417 case '<':
4418 switch (arg->op.op[1]) {
4419 case 0:
4420 val = left < right;
4421 break;
4422 case '<':
4423 val = left << right;
4424 break;
4425 case '=':
4426 val = left <= right;
4427 break;
4428 default:
4429 goto out_warning_op;
4430 }
4431 break;
4432 case '>':
4433 switch (arg->op.op[1]) {
4434 case 0:
4435 val = left > right;
4436 break;
4437 case '>':
4438 val = left >> right;
4439 break;
4440 case '=':
4441 val = left >= right;
4442 break;
4443 default:
4444 goto out_warning_op;
4445 }
4446 break;
4447 case '=':
4448 if (arg->op.op[1] != '=')
4449 goto out_warning_op;
4450
4451 val = left == right;
4452 break;
4453 case '-':
4454 val = left - right;
4455 break;
4456 case '+':
4457 val = left + right;
4458 break;
4459 case '/':
4460 val = left / right;
4461 break;
4462 case '%':
4463 val = left % right;
4464 break;
4465 case '*':
4466 val = left * right;
4467 break;
4468 default:
4469 goto out_warning_op;
4470 }
4471 break;
4472 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
4473 dynamic_offset_field(tep, arg->dynarray.field, data, size,
4474 NULL, &field_size);
4475 val = field_size;
4476 break;
4477 case TEP_PRINT_DYNAMIC_ARRAY:
4478 /* Without [], we pass the address to the dynamic data */
4479 dynamic_offset_field(tep, arg->dynarray.field, data, size,
4480 &offset, NULL);
4481 if (check_data_offset_size(event, arg->field.name, size,
4482 offset, 0)) {
4483 val = (unsigned long)data;
4484 break;
4485 }
4486 val = (unsigned long)data + offset;
4487 break;
4488 default: /* not sure what to do there */
4489 return 0;
4490 }
4491 return val;
4492
4493 out_warning_op:
4494 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
4495 return 0;
4496
4497 out_warning_field:
4498 do_warning_event(event, "%s: field %s not found",
4499 __func__, arg->field.name);
4500 return 0;
4501 }
4502
4503 struct flag {
4504 const char *name;
4505 unsigned long long value;
4506 };
4507
4508 static const struct flag flags[] = {
4509 { "HI_SOFTIRQ", 0 },
4510 { "TIMER_SOFTIRQ", 1 },
4511 { "NET_TX_SOFTIRQ", 2 },
4512 { "NET_RX_SOFTIRQ", 3 },
4513 { "BLOCK_SOFTIRQ", 4 },
4514 { "IRQ_POLL_SOFTIRQ", 5 },
4515 { "TASKLET_SOFTIRQ", 6 },
4516 { "SCHED_SOFTIRQ", 7 },
4517 { "HRTIMER_SOFTIRQ", 8 },
4518 { "RCU_SOFTIRQ", 9 },
4519
4520 { "HRTIMER_NORESTART", 0 },
4521 { "HRTIMER_RESTART", 1 },
4522 };
4523
eval_flag(const char * flag)4524 static long long eval_flag(const char *flag)
4525 {
4526 int i;
4527
4528 /*
4529 * Some flags in the format files do not get converted.
4530 * If the flag is not numeric, see if it is something that
4531 * we already know about.
4532 */
4533 if (isdigit(flag[0]))
4534 return strtoull(flag, NULL, 0);
4535
4536 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
4537 if (strcmp(flags[i].name, flag) == 0)
4538 return flags[i].value;
4539
4540 return -1LL;
4541 }
4542
print_str_to_seq(struct trace_seq * s,const char * format,int len_arg,const char * str)4543 static void print_str_to_seq(struct trace_seq *s, const char *format,
4544 int len_arg, const char *str)
4545 {
4546 if (len_arg >= 0)
4547 trace_seq_printf(s, format, len_arg, str);
4548 else
4549 trace_seq_printf(s, format, str);
4550 }
4551
print_bitmask_to_seq(struct tep_handle * tep,struct trace_seq * s,const char * format,int len_arg,const void * data,int size)4552 static void print_bitmask_to_seq(struct tep_handle *tep,
4553 struct trace_seq *s, const char *format,
4554 int len_arg, const void *data, int size)
4555 {
4556 int nr_bits = size * 8;
4557 int str_size = (nr_bits + 3) / 4;
4558 int len = 0;
4559 char buf[3];
4560 char *str;
4561 int index;
4562 int i;
4563
4564 /*
4565 * The kernel likes to put in commas every 32 bits, we
4566 * can do the same.
4567 */
4568 str_size += (nr_bits - 1) / 32;
4569
4570 str = malloc(str_size + 1);
4571 if (!str) {
4572 do_warning("%s: not enough memory!", __func__);
4573 return;
4574 }
4575 str[str_size] = 0;
4576
4577 /* Start out with -2 for the two chars per byte */
4578 for (i = str_size - 2; i >= 0; i -= 2) {
4579 /*
4580 * data points to a bit mask of size bytes.
4581 * In the kernel, this is an array of long words, thus
4582 * endianness is very important.
4583 */
4584 if (tep->file_bigendian)
4585 index = size - (len + 1);
4586 else
4587 index = len;
4588
4589 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
4590 memcpy(str + i, buf, 2);
4591 len++;
4592 if (!(len & 3) && i > 0) {
4593 i--;
4594 str[i] = ',';
4595 }
4596 }
4597
4598 if (len_arg >= 0)
4599 trace_seq_printf(s, format, len_arg, str);
4600 else
4601 trace_seq_printf(s, format, str);
4602
4603 free(str);
4604 }
4605
4606 #define log10(n) \
4607 ( \
4608 n < 10UL ? 0 : \
4609 n < 100UL ? 1 : \
4610 n < 1000UL ? 2 : \
4611 n < 10000UL ? 3 : \
4612 n < 100000UL ? 4 : \
4613 n < 1000000UL ? 5 : \
4614 n < 10000000UL ? 6 : \
4615 n < 100000000UL ? 7 : \
4616 n < 1000000000UL ? 8 : \
4617 9 \
4618 )
4619
4620 /* ilog10(0) should be 1 but the 0 simplifies below math */
4621 #define ilog10(n) \
4622 ( \
4623 n == 0 ? 0UL : \
4624 n == 1 ? 10UL : \
4625 n == 2 ? 100UL : \
4626 n == 3 ? 1000UL : \
4627 n == 4 ? 10000UL : \
4628 n == 5 ? 100000UL : \
4629 n == 6 ? 1000000UL : \
4630 n == 7 ? 10000000UL : \
4631 n == 8 ? 100000000UL : \
4632 1000000000UL \
4633 )
4634
cpumask_worst_size(unsigned int nr_bits)4635 static unsigned int cpumask_worst_size(unsigned int nr_bits)
4636 {
4637 /*
4638 * Printing all the CPUs separated by a comma is a decent bound for the
4639 * maximum memory required to print a cpumask (a slightly better bound
4640 * is chunks of 2 bits set, i.e. 0-1,3-4,6-7...).
4641 *
4642 * e.g. for nr_bits=132:
4643 * - 131 commas
4644 * - 10 * 1 chars for CPUS [0, 9]
4645 * - 90 * 2 chars for CPUS [10-99]
4646 * - 32 * 3 chars for CPUS [100-131]
4647 */
4648 unsigned int last_cpu = nr_bits - 1;
4649 unsigned int nr_chars = nr_bits - 1;
4650 int last_lvl = log10(last_cpu);
4651
4652 /* All log10 levels before the last one have all values used */
4653 for (int lvl = 0; lvl < last_lvl; lvl++) {
4654 int nr_values = ilog10(lvl + 1) - ilog10(lvl);
4655
4656 nr_chars += nr_values * (lvl + 1);
4657 }
4658 /* Last level is incomplete */
4659 nr_chars += (nr_bits - ilog10(last_lvl)) * (last_lvl + 1);
4660
4661 return nr_chars;
4662 }
4663
print_cpumask_to_seq(struct tep_handle * tep,struct trace_seq * s,const char * format,int len_arg,const void * data,int size)4664 static void print_cpumask_to_seq(struct tep_handle *tep,
4665 struct trace_seq *s, const char *format,
4666 int len_arg, const void *data, int size)
4667 {
4668 int firstone = -1, firstzero = -1;
4669 int nr_bits = size * 8;
4670 bool first = true;
4671 int str_size = 0;
4672 char buf[12]; /* '-' + log10(2^32) + 1 digits + '\0' */
4673 char *str;
4674 int index;
4675 int i;
4676
4677 str = malloc(cpumask_worst_size(nr_bits) + 1);
4678 if (!str) {
4679 do_warning("%s: not enough memory!", __func__);
4680 return;
4681 }
4682
4683 for (i = 0; i < size; i++) {
4684 unsigned char byte;
4685 int fmtsize;
4686
4687 if (tep->file_bigendian)
4688 index = size - (i + 1);
4689 else
4690 index = i;
4691
4692 /* Byte by byte scan, not the best... */
4693 byte = *(((unsigned char *)data) + index);
4694 more:
4695 /* First find a bit set to one...*/
4696 if (firstone < 0 && byte) {
4697 /*
4698 * Set all lower bits, so a later ffz on this same byte
4699 * is guaranteed to find a later bit.
4700 */
4701 firstone = ffs(byte) - 1;
4702 byte |= (1 << firstone) - 1;
4703 firstone += i * 8;
4704 }
4705
4706 if (firstone < 0)
4707 continue;
4708
4709 /* ...Then find a bit set to zero */
4710 if ((~byte) & 0xFF) {
4711 /*
4712 * Clear all lower bits, so a later ffs on this same
4713 * byte is guaranteed to find a later bit.
4714 */
4715 firstzero = ffs(~byte) - 1;
4716 byte &= ~((1 << (firstzero)) - 1);
4717 firstzero += i * 8;
4718 } else if (i == size - 1) { /* ...Or reach the end of the mask */
4719 firstzero = nr_bits;
4720 byte = 0;
4721 } else {
4722 continue;
4723 }
4724
4725 /* We've found a bit set to one, and a later bit set to zero. */
4726 if (!first) {
4727 str[str_size] = ',';
4728 str_size++;
4729 }
4730 first = false;
4731
4732 /* It takes {log10(number) + 1} chars to format a number */
4733 fmtsize = log10(firstone) + 1;
4734 snprintf(buf, fmtsize + 1, "%d", firstone);
4735 memcpy(str + str_size, buf, fmtsize);
4736 str_size += fmtsize;
4737
4738 if (firstzero > firstone + 1) {
4739 fmtsize = log10(firstzero - 1) + 2;
4740 snprintf(buf, fmtsize + 1, "-%d", firstzero - 1);
4741 memcpy(str + str_size, buf, fmtsize);
4742 str_size += fmtsize;
4743 }
4744
4745 firstzero = firstone = -1;
4746 if (byte)
4747 goto more;
4748 }
4749
4750 str[str_size] = 0;
4751 str_size++;
4752
4753 if (len_arg >= 0)
4754 trace_seq_printf(s, format, len_arg, str);
4755 else
4756 trace_seq_printf(s, format, str);
4757
4758 free(str);
4759 }
4760
print_str_arg(struct trace_seq * s,void * data,int size,struct tep_event * event,const char * format,int len_arg,struct tep_print_arg * arg)4761 static void print_str_arg(struct trace_seq *s, void *data, int size,
4762 struct tep_event *event, const char *format,
4763 int len_arg, struct tep_print_arg *arg)
4764 {
4765 struct tep_handle *tep = event->tep;
4766 struct tep_print_flag_sym *flag;
4767 struct tep_format_field *field;
4768 struct printk_map *printk;
4769 unsigned int offset, len;
4770 long long val, fval;
4771 unsigned long long addr;
4772 char *str;
4773 unsigned char *hex;
4774 int print;
4775 int i;
4776
4777 switch (arg->type) {
4778 case TEP_PRINT_NULL:
4779 /* ?? */
4780 return;
4781 case TEP_PRINT_ATOM:
4782 print_str_to_seq(s, format, len_arg, arg->atom.atom);
4783 return;
4784 case TEP_PRINT_FIELD:
4785 field = arg->field.field;
4786 if (!field) {
4787 field = tep_find_any_field(event, arg->field.name);
4788 if (!field) {
4789 str = arg->field.name;
4790 goto out_warning_field;
4791 }
4792 arg->field.field = field;
4793 }
4794 /* Zero sized fields, mean the rest of the data */
4795 len = field->size ? : size - field->offset;
4796
4797 /*
4798 * Some events pass in pointers. If this is not an array
4799 * and the size is the same as long_size, assume that it
4800 * is a pointer.
4801 */
4802 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
4803 field->size == tep->long_size) {
4804
4805 /* Handle heterogeneous recording and processing
4806 * architectures
4807 *
4808 * CASE I:
4809 * Traces recorded on 32-bit devices (32-bit
4810 * addressing) and processed on 64-bit devices:
4811 * In this case, only 32 bits should be read.
4812 *
4813 * CASE II:
4814 * Traces recorded on 64 bit devices and processed
4815 * on 32-bit devices:
4816 * In this case, 64 bits must be read.
4817 */
4818 addr = (tep->long_size == 8) ?
4819 *(unsigned long long *)(data + field->offset) :
4820 (unsigned long long)*(unsigned int *)(data + field->offset);
4821
4822 /* Check if it matches a print format */
4823 printk = find_printk(tep, addr);
4824 if (printk)
4825 trace_seq_puts(s, printk->printk);
4826 else
4827 trace_seq_printf(s, "%llx", addr);
4828 break;
4829 }
4830 str = malloc(len + 1);
4831 if (!str) {
4832 do_warning_event(event, "%s: not enough memory!",
4833 __func__);
4834 return;
4835 }
4836 memcpy(str, data + field->offset, len);
4837 str[len] = 0;
4838 print_str_to_seq(s, format, len_arg, str);
4839 free(str);
4840 break;
4841 case TEP_PRINT_FLAGS:
4842 val = eval_num_arg(data, size, event, arg->flags.field);
4843 print = 0;
4844 for (flag = arg->flags.flags; flag; flag = flag->next) {
4845 fval = eval_flag(flag->value);
4846 if (!val && fval < 0) {
4847 print_str_to_seq(s, format, len_arg, flag->str);
4848 break;
4849 }
4850 if (fval > 0 && (val & fval) == fval) {
4851 if (print && arg->flags.delim)
4852 trace_seq_puts(s, arg->flags.delim);
4853 print_str_to_seq(s, format, len_arg, flag->str);
4854 print = 1;
4855 val &= ~fval;
4856 }
4857 }
4858 if (val) {
4859 if (print && arg->flags.delim)
4860 trace_seq_puts(s, arg->flags.delim);
4861 trace_seq_printf(s, "0x%llx", val);
4862 }
4863 break;
4864 case TEP_PRINT_SYMBOL:
4865 val = eval_num_arg(data, size, event, arg->symbol.field);
4866 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4867 fval = eval_flag(flag->value);
4868 if (val == fval) {
4869 print_str_to_seq(s, format, len_arg, flag->str);
4870 break;
4871 }
4872 }
4873 if (!flag)
4874 trace_seq_printf(s, "0x%llx", val);
4875 break;
4876 case TEP_PRINT_HEX:
4877 case TEP_PRINT_HEX_STR:
4878 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4879 dynamic_offset_field(tep, arg->hex.field->dynarray.field, data,
4880 size, &offset, NULL);
4881 hex = data + offset;
4882 } else {
4883 field = arg->hex.field->field.field;
4884 if (!field) {
4885 str = arg->hex.field->field.name;
4886 field = tep_find_any_field(event, str);
4887 if (!field)
4888 goto out_warning_field;
4889 arg->hex.field->field.field = field;
4890 }
4891 hex = data + field->offset;
4892 }
4893 len = eval_num_arg(data, size, event, arg->hex.size);
4894 for (i = 0; i < len; i++) {
4895 if (i && arg->type == TEP_PRINT_HEX)
4896 trace_seq_putc(s, ' ');
4897 trace_seq_printf(s, "%02x", hex[i]);
4898 }
4899 break;
4900
4901 case TEP_PRINT_INT_ARRAY: {
4902 void *num;
4903 int el_size;
4904
4905 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4906 dynamic_offset_field(tep, arg->int_array.field->dynarray.field, data,
4907 size, &offset, NULL);
4908 num = data + offset;
4909 } else {
4910 field = arg->int_array.field->field.field;
4911 if (!field) {
4912 str = arg->int_array.field->field.name;
4913 field = tep_find_any_field(event, str);
4914 if (!field)
4915 goto out_warning_field;
4916 arg->int_array.field->field.field = field;
4917 }
4918 num = data + field->offset;
4919 }
4920 len = eval_num_arg(data, size, event, arg->int_array.count);
4921 el_size = eval_num_arg(data, size, event,
4922 arg->int_array.el_size);
4923 for (i = 0; i < len; i++) {
4924 if (i)
4925 trace_seq_putc(s, ' ');
4926
4927 if (el_size == 1) {
4928 trace_seq_printf(s, "%u", *(uint8_t *)num);
4929 } else if (el_size == 2) {
4930 trace_seq_printf(s, "%u", *(uint16_t *)num);
4931 } else if (el_size == 4) {
4932 trace_seq_printf(s, "%u", *(uint32_t *)num);
4933 } else if (el_size == 8) {
4934 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4935 } else {
4936 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4937 el_size, *(uint8_t *)num);
4938 el_size = 1;
4939 }
4940
4941 num += el_size;
4942 }
4943 break;
4944 }
4945 case TEP_PRINT_TYPE:
4946 break;
4947 case TEP_PRINT_STRING: {
4948 if (!arg->string.field) {
4949 arg->string.field = tep_find_any_field(event, arg->string.string);
4950 if (!arg->string.field)
4951 break;
4952 arg->string.offset = arg->string.field->offset;
4953 }
4954 dynamic_offset_field(tep, arg->string.field, data, size, &offset, &len);
4955 /* Do not attempt to save zero length dynamic strings */
4956 if (!len)
4957 break;
4958 print_str_to_seq(s, format, len_arg, ((char *)data) + offset);
4959 break;
4960 }
4961 case TEP_PRINT_BSTRING:
4962 print_str_to_seq(s, format, len_arg, arg->string.string);
4963 break;
4964 case TEP_PRINT_BITMASK: {
4965 if (!arg->bitmask.field) {
4966 arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
4967 if (!arg->bitmask.field)
4968 break;
4969 arg->bitmask.offset = arg->bitmask.field->offset;
4970 }
4971 dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
4972 print_bitmask_to_seq(tep, s, format, len_arg,
4973 data + offset, len);
4974 break;
4975 }
4976 case TEP_PRINT_CPUMASK: {
4977 if (!arg->bitmask.field) {
4978 arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
4979 arg->bitmask.offset = arg->bitmask.field->offset;
4980 }
4981 if (!arg->bitmask.field)
4982 break;
4983 dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
4984 print_cpumask_to_seq(tep, s, format, len_arg,
4985 data + offset, len);
4986 break;
4987 }
4988 case TEP_PRINT_OP:
4989 /*
4990 * The only op for string should be ? :
4991 */
4992 if (arg->op.op[0] != '?')
4993 return;
4994 val = eval_num_arg(data, size, event, arg->op.left);
4995 if (val)
4996 print_str_arg(s, data, size, event,
4997 format, len_arg, arg->op.right->op.left);
4998 else
4999 print_str_arg(s, data, size, event,
5000 format, len_arg, arg->op.right->op.right);
5001 break;
5002 case TEP_PRINT_FUNC:
5003 process_defined_func(s, data, size, event, arg);
5004 break;
5005 default:
5006 /* well... */
5007 break;
5008 }
5009
5010 return;
5011
5012 out_warning_field:
5013 do_warning_event(event, "%s: field %s not found",
5014 __func__, arg->field.name);
5015 }
5016
5017 static unsigned long long
process_defined_func(struct trace_seq * s,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5018 process_defined_func(struct trace_seq *s, void *data, int size,
5019 struct tep_event *event, struct tep_print_arg *arg)
5020 {
5021 struct tep_function_handler *func_handle = arg->func.func;
5022 struct func_params *param;
5023 unsigned long long *args;
5024 unsigned long long ret;
5025 struct tep_print_arg *farg;
5026 struct trace_seq str;
5027 struct save_str {
5028 struct save_str *next;
5029 char *str;
5030 } *strings = NULL, *string;
5031 int i;
5032
5033 if (!func_handle->nr_args) {
5034 ret = (*func_handle->func)(s, NULL);
5035 goto out;
5036 }
5037
5038 farg = arg->func.args;
5039 param = func_handle->params;
5040
5041 ret = ULLONG_MAX;
5042 args = malloc(sizeof(*args) * func_handle->nr_args);
5043 if (!args)
5044 goto out;
5045
5046 for (i = 0; i < func_handle->nr_args; i++) {
5047 switch (param->type) {
5048 case TEP_FUNC_ARG_INT:
5049 case TEP_FUNC_ARG_LONG:
5050 case TEP_FUNC_ARG_PTR:
5051 args[i] = eval_num_arg(data, size, event, farg);
5052 break;
5053 case TEP_FUNC_ARG_STRING:
5054 trace_seq_init(&str);
5055 print_str_arg(&str, data, size, event, "%s", -1, farg);
5056 trace_seq_terminate(&str);
5057 string = malloc(sizeof(*string));
5058 if (!string) {
5059 do_warning_event(event, "%s(%d): malloc str",
5060 __func__, __LINE__);
5061 goto out_free;
5062 }
5063 string->next = strings;
5064 string->str = strdup(str.buffer);
5065 if (!string->str) {
5066 free(string);
5067 do_warning_event(event, "%s(%d): malloc str",
5068 __func__, __LINE__);
5069 goto out_free;
5070 }
5071 args[i] = (uintptr_t)string->str;
5072 strings = string;
5073 trace_seq_destroy(&str);
5074 break;
5075 default:
5076 /*
5077 * Something went totally wrong, this is not
5078 * an input error, something in this code broke.
5079 */
5080 do_warning_event(event, "Unexpected end of arguments\n");
5081 goto out_free;
5082 }
5083 farg = farg->next;
5084 param = param->next;
5085 }
5086
5087 ret = (*func_handle->func)(s, args);
5088 out_free:
5089 free(args);
5090 while (strings) {
5091 string = strings;
5092 strings = string->next;
5093 free(string->str);
5094 free(string);
5095 }
5096
5097 out:
5098 /* TBD : handle return type here */
5099 return ret;
5100 }
5101
free_args(struct tep_print_arg * args)5102 static void free_args(struct tep_print_arg *args)
5103 {
5104 struct tep_print_arg *next;
5105
5106 while (args) {
5107 next = args->next;
5108
5109 free_arg(args);
5110 args = next;
5111 }
5112 }
5113
make_bprint_args(char * fmt,void * data,int size,struct tep_event * event)5114 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
5115 {
5116 struct tep_handle *tep = event->tep;
5117 struct tep_format_field *field, *ip_field;
5118 struct tep_print_arg *args, *arg, **next;
5119 unsigned long long ip, val;
5120 char *ptr;
5121 void *bptr;
5122 int vsize = 0;
5123
5124 field = tep->bprint_buf_field;
5125 ip_field = tep->bprint_ip_field;
5126
5127 if (!field) {
5128 field = tep_find_field(event, "buf");
5129 if (!field) {
5130 do_warning_event(event, "can't find buffer field for binary printk");
5131 return NULL;
5132 }
5133 ip_field = tep_find_field(event, "ip");
5134 if (!ip_field) {
5135 do_warning_event(event, "can't find ip field for binary printk");
5136 return NULL;
5137 }
5138 tep->bprint_buf_field = field;
5139 tep->bprint_ip_field = ip_field;
5140 }
5141
5142 ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
5143
5144 /*
5145 * The first arg is the IP pointer.
5146 */
5147 args = alloc_arg();
5148 if (!args) {
5149 do_warning_event(event, "%s(%d): not enough memory!",
5150 __func__, __LINE__);
5151 return NULL;
5152 }
5153 arg = args;
5154 arg->next = NULL;
5155 next = &arg->next;
5156
5157 arg->type = TEP_PRINT_ATOM;
5158
5159 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
5160 goto out_free;
5161
5162 /* skip the first "%ps: " */
5163 for (ptr = fmt + 5, bptr = data + field->offset;
5164 bptr < data + size && *ptr; ptr++) {
5165 int ls = 0;
5166
5167 if (*ptr == '%') {
5168 process_again:
5169 ptr++;
5170 switch (*ptr) {
5171 case '%':
5172 break;
5173 case 'l':
5174 ls++;
5175 goto process_again;
5176 case 'L':
5177 ls = 2;
5178 goto process_again;
5179 case '0' ... '9':
5180 goto process_again;
5181 case '.':
5182 goto process_again;
5183 case '#':
5184 goto process_again;
5185 case 'z':
5186 case 'Z':
5187 ls = 1;
5188 goto process_again;
5189 case 'p':
5190 ls = 1;
5191 if (isalnum(ptr[1])) {
5192 ptr++;
5193 /* Check for special pointers */
5194 switch (*ptr) {
5195 case 's':
5196 case 'S':
5197 case 'x':
5198 break;
5199 case 'f':
5200 case 'F':
5201 /*
5202 * Pre-5.5 kernels use %pf and
5203 * %pF for printing symbols
5204 * while kernels since 5.5 use
5205 * %pfw for fwnodes. So check
5206 * %p[fF] isn't followed by 'w'.
5207 */
5208 if (ptr[1] != 'w')
5209 break;
5210 /* fall through */
5211 default:
5212 /*
5213 * Older kernels do not process
5214 * dereferenced pointers.
5215 * Only process if the pointer
5216 * value is a printable.
5217 */
5218 if (isprint(*(char *)bptr))
5219 goto process_string;
5220 }
5221 }
5222 /* fall through */
5223 case 'd':
5224 case 'u':
5225 case 'i':
5226 case 'x':
5227 case 'X':
5228 case 'o':
5229 switch (ls) {
5230 case 0:
5231 vsize = 4;
5232 break;
5233 case 1:
5234 vsize = tep->long_size;
5235 break;
5236 case 2:
5237 vsize = 8;
5238 break;
5239 default:
5240 vsize = ls; /* ? */
5241 break;
5242 }
5243 /* fall through */
5244 case '*':
5245 if (*ptr == '*')
5246 vsize = 4;
5247
5248 /* the pointers are always 4 bytes aligned */
5249 bptr = (void *)(((unsigned long)bptr + 3) &
5250 ~3);
5251 val = tep_read_number(tep, bptr, vsize);
5252 bptr += vsize;
5253 arg = alloc_arg();
5254 if (!arg) {
5255 do_warning_event(event, "%s(%d): not enough memory!",
5256 __func__, __LINE__);
5257 goto out_free;
5258 }
5259 arg->next = NULL;
5260 arg->type = TEP_PRINT_ATOM;
5261 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
5262 free(arg);
5263 goto out_free;
5264 }
5265 *next = arg;
5266 next = &arg->next;
5267 /*
5268 * The '*' case means that an arg is used as the length.
5269 * We need to continue to figure out for what.
5270 */
5271 if (*ptr == '*')
5272 goto process_again;
5273
5274 break;
5275 case 's':
5276 process_string:
5277 arg = alloc_arg();
5278 if (!arg) {
5279 do_warning_event(event, "%s(%d): not enough memory!",
5280 __func__, __LINE__);
5281 goto out_free;
5282 }
5283 arg->next = NULL;
5284 arg->type = TEP_PRINT_BSTRING;
5285 arg->string.string = strdup(bptr);
5286 if (!arg->string.string) {
5287 free(arg);
5288 goto out_free;
5289 }
5290 bptr += strlen(bptr) + 1;
5291 *next = arg;
5292 next = &arg->next;
5293 default:
5294 break;
5295 }
5296 }
5297 }
5298
5299 return args;
5300
5301 out_free:
5302 free_args(args);
5303 return NULL;
5304 }
5305
5306 static char *
get_bprint_format(void * data,int size __maybe_unused,struct tep_event * event)5307 get_bprint_format(void *data, int size __maybe_unused,
5308 struct tep_event *event)
5309 {
5310 struct tep_handle *tep = event->tep;
5311 unsigned long long addr;
5312 struct tep_format_field *field;
5313 struct printk_map *printk;
5314 char *format;
5315
5316 field = tep->bprint_fmt_field;
5317
5318 if (!field) {
5319 field = tep_find_field(event, "fmt");
5320 if (!field) {
5321 do_warning_event(event, "can't find format field for binary printk");
5322 return NULL;
5323 }
5324 tep->bprint_fmt_field = field;
5325 }
5326
5327 addr = tep_read_number(tep, data + field->offset, field->size);
5328
5329 printk = find_printk(tep, addr);
5330 if (!printk) {
5331 if (asprintf(&format, "%%ps: (NO FORMAT FOUND at %llx)\n", addr) < 0)
5332 return NULL;
5333 return format;
5334 }
5335
5336 if (asprintf(&format, "%s: %s", "%ps", printk->printk) < 0)
5337 return NULL;
5338
5339 return format;
5340 }
5341
print_mac_arg(struct trace_seq * s,const char * format,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5342 static int print_mac_arg(struct trace_seq *s, const char *format,
5343 void *data, int size, struct tep_event *event,
5344 struct tep_print_arg *arg)
5345 {
5346 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
5347 bool reverse = false;
5348 unsigned char *buf;
5349 int ret = 0;
5350
5351 if (arg->type == TEP_PRINT_FUNC) {
5352 process_defined_func(s, data, size, event, arg);
5353 return 0;
5354 }
5355
5356 /* evaluate if the arg has a type cast */
5357 while (arg->type == TEP_PRINT_TYPE)
5358 arg = arg->typecast.item;
5359
5360 if (arg->type != TEP_PRINT_FIELD) {
5361 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
5362 arg->type);
5363 return 0;
5364 }
5365
5366 if (format[0] == 'm') {
5367 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
5368 } else if (format[0] == 'M' && format[1] == 'F') {
5369 fmt = "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x";
5370 ret++;
5371 }
5372 if (format[1] == 'R') {
5373 reverse = true;
5374 ret++;
5375 }
5376
5377 if (!arg->field.field) {
5378 arg->field.field =
5379 tep_find_any_field(event, arg->field.name);
5380 if (!arg->field.field) {
5381 do_warning_event(event, "%s: field %s not found",
5382 __func__, arg->field.name);
5383 return ret;
5384 }
5385 }
5386 if (arg->field.field->size != 6) {
5387 trace_seq_printf(s, "INVALIDMAC");
5388 return ret;
5389 }
5390
5391 buf = data + arg->field.field->offset;
5392 if (reverse)
5393 trace_seq_printf(s, fmt, buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
5394 else
5395 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
5396
5397 return ret;
5398 }
5399
parse_ip4_print_args(struct tep_handle * tep,const char * ptr,bool * reverse)5400 static int parse_ip4_print_args(struct tep_handle *tep,
5401 const char *ptr, bool *reverse)
5402 {
5403 int ret = 0;
5404
5405 *reverse = false;
5406
5407 /* hnbl */
5408 switch (*ptr) {
5409 case 'h':
5410 if (tep->file_bigendian)
5411 *reverse = false;
5412 else
5413 *reverse = true;
5414 ret++;
5415 break;
5416 case 'l':
5417 *reverse = true;
5418 ret++;
5419 break;
5420 case 'n':
5421 case 'b':
5422 ret++;
5423 /* fall through */
5424 default:
5425 *reverse = false;
5426 break;
5427 }
5428
5429 return ret;
5430 }
5431
print_ip4_addr(struct trace_seq * s,char i,bool reverse,unsigned char * buf)5432 static void print_ip4_addr(struct trace_seq *s, char i, bool reverse, unsigned char *buf)
5433 {
5434 const char *fmt;
5435
5436 if (i == 'i')
5437 fmt = "%03d.%03d.%03d.%03d";
5438 else
5439 fmt = "%d.%d.%d.%d";
5440
5441 if (reverse)
5442 trace_seq_printf(s, fmt, buf[3], buf[2], buf[1], buf[0]);
5443 else
5444 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
5445
5446 }
5447
ipv6_addr_v4mapped(const struct in6_addr * a)5448 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
5449 {
5450 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
5451 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
5452 }
5453
ipv6_addr_is_isatap(const struct in6_addr * addr)5454 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
5455 {
5456 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
5457 }
5458
print_ip6c_addr(struct trace_seq * s,unsigned char * addr)5459 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
5460 {
5461 int i, j, range;
5462 unsigned char zerolength[8];
5463 int longest = 1;
5464 int colonpos = -1;
5465 uint16_t word;
5466 uint8_t hi, lo;
5467 bool needcolon = false;
5468 bool useIPv4;
5469 struct in6_addr in6;
5470
5471 memcpy(&in6, addr, sizeof(struct in6_addr));
5472
5473 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
5474
5475 memset(zerolength, 0, sizeof(zerolength));
5476
5477 if (useIPv4)
5478 range = 6;
5479 else
5480 range = 8;
5481
5482 /* find position of longest 0 run */
5483 for (i = 0; i < range; i++) {
5484 for (j = i; j < range; j++) {
5485 if (in6.s6_addr16[j] != 0)
5486 break;
5487 zerolength[i]++;
5488 }
5489 }
5490 for (i = 0; i < range; i++) {
5491 if (zerolength[i] > longest) {
5492 longest = zerolength[i];
5493 colonpos = i;
5494 }
5495 }
5496 if (longest == 1) /* don't compress a single 0 */
5497 colonpos = -1;
5498
5499 /* emit address */
5500 for (i = 0; i < range; i++) {
5501 if (i == colonpos) {
5502 if (needcolon || i == 0)
5503 trace_seq_printf(s, ":");
5504 trace_seq_printf(s, ":");
5505 needcolon = false;
5506 i += longest - 1;
5507 continue;
5508 }
5509 if (needcolon) {
5510 trace_seq_printf(s, ":");
5511 needcolon = false;
5512 }
5513 /* hex u16 without leading 0s */
5514 word = ntohs(in6.s6_addr16[i]);
5515 hi = word >> 8;
5516 lo = word & 0xff;
5517 if (hi)
5518 trace_seq_printf(s, "%x%02x", hi, lo);
5519 else
5520 trace_seq_printf(s, "%x", lo);
5521
5522 needcolon = true;
5523 }
5524
5525 if (useIPv4) {
5526 if (needcolon)
5527 trace_seq_printf(s, ":");
5528 print_ip4_addr(s, 'I', false, &in6.s6_addr[12]);
5529 }
5530
5531 return;
5532 }
5533
print_ip6_addr(struct trace_seq * s,char i,unsigned char * buf)5534 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
5535 {
5536 int j;
5537
5538 for (j = 0; j < 16; j += 2) {
5539 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
5540 if (i == 'I' && j < 14)
5541 trace_seq_printf(s, ":");
5542 }
5543 }
5544
5545 /*
5546 * %pi4 print an IPv4 address with leading zeros
5547 * %pI4 print an IPv4 address without leading zeros
5548 * %pi6 print an IPv6 address without colons
5549 * %pI6 print an IPv6 address with colons
5550 * %pI6c print an IPv6 address in compressed form with colons
5551 * %pISpc print an IP address based on sockaddr; p adds port.
5552 */
print_ipv4_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5553 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
5554 void *data, int size, struct tep_event *event,
5555 struct tep_print_arg *arg)
5556 {
5557 bool reverse = false;
5558 unsigned char *buf;
5559 int ret;
5560
5561 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
5562
5563 if (arg->type == TEP_PRINT_FUNC) {
5564 process_defined_func(s, data, size, event, arg);
5565 return ret;
5566 }
5567
5568 /* evaluate if the arg has a type cast */
5569 while (arg->type == TEP_PRINT_TYPE)
5570 arg = arg->typecast.item;
5571
5572 if (arg->type != TEP_PRINT_FIELD) {
5573 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5574 return ret;
5575 }
5576
5577 if (!arg->field.field) {
5578 arg->field.field =
5579 tep_find_any_field(event, arg->field.name);
5580 if (!arg->field.field) {
5581 do_warning("%s: field %s not found",
5582 __func__, arg->field.name);
5583 return ret;
5584 }
5585 }
5586
5587 buf = data + arg->field.field->offset;
5588
5589 if (arg->field.field->size != 4) {
5590 trace_seq_printf(s, "INVALIDIPv4");
5591 return ret;
5592 }
5593
5594 print_ip4_addr(s, i, reverse, buf);
5595 return ret;
5596
5597 }
5598
print_ipv6_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5599 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
5600 void *data, int size, struct tep_event *event,
5601 struct tep_print_arg *arg)
5602 {
5603 char have_c = 0;
5604 unsigned char *buf;
5605 int rc = 0;
5606
5607 /* pI6c */
5608 if (i == 'I' && *ptr == 'c') {
5609 have_c = 1;
5610 ptr++;
5611 rc++;
5612 }
5613
5614 if (arg->type == TEP_PRINT_FUNC) {
5615 process_defined_func(s, data, size, event, arg);
5616 return rc;
5617 }
5618
5619 /* evaluate if the arg has a type cast */
5620 while (arg->type == TEP_PRINT_TYPE)
5621 arg = arg->typecast.item;
5622
5623 if (arg->type != TEP_PRINT_FIELD) {
5624 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5625 return rc;
5626 }
5627
5628 if (!arg->field.field) {
5629 arg->field.field =
5630 tep_find_any_field(event, arg->field.name);
5631 if (!arg->field.field) {
5632 do_warning("%s: field %s not found",
5633 __func__, arg->field.name);
5634 return rc;
5635 }
5636 }
5637
5638 buf = data + arg->field.field->offset;
5639
5640 if (arg->field.field->size != 16) {
5641 trace_seq_printf(s, "INVALIDIPv6");
5642 return rc;
5643 }
5644
5645 if (have_c)
5646 print_ip6c_addr(s, buf);
5647 else
5648 print_ip6_addr(s, i, buf);
5649
5650 return rc;
5651 }
5652
print_ipsa_arg(struct trace_seq * s,const char * ptr,char i,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5653 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
5654 void *data, int size, struct tep_event *event,
5655 struct tep_print_arg *arg)
5656 {
5657 char have_c = 0, have_p = 0;
5658 unsigned char *buf;
5659 struct sockaddr_storage *sa;
5660 bool reverse = false;
5661 unsigned int offset;
5662 unsigned int len;
5663 int rc = 0;
5664 int ret;
5665
5666 /* pISpc */
5667 if (i == 'I') {
5668 if (*ptr == 'p') {
5669 have_p = 1;
5670 ptr++;
5671 rc++;
5672 }
5673 if (*ptr == 'c') {
5674 have_c = 1;
5675 ptr++;
5676 rc++;
5677 }
5678 }
5679 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
5680 ptr += ret;
5681 rc += ret;
5682
5683 if (arg->type == TEP_PRINT_FUNC) {
5684 process_defined_func(s, data, size, event, arg);
5685 return rc;
5686 }
5687
5688 /* evaluate if the arg has a type cast */
5689 while (arg->type == TEP_PRINT_TYPE)
5690 arg = arg->typecast.item;
5691
5692 if (arg->type == TEP_PRINT_FIELD) {
5693
5694 if (!arg->field.field) {
5695 arg->field.field =
5696 tep_find_any_field(event, arg->field.name);
5697 if (!arg->field.field) {
5698 do_warning("%s: field %s not found",
5699 __func__, arg->field.name);
5700 return rc;
5701 }
5702 }
5703
5704 offset = arg->field.field->offset;
5705 len = arg->field.field->size;
5706
5707 } else if (arg->type == TEP_PRINT_DYNAMIC_ARRAY) {
5708
5709 dynamic_offset_field(event->tep, arg->dynarray.field, data,
5710 size, &offset, &len);
5711
5712 } else {
5713 trace_seq_printf(s, "ARG NOT FIELD NOR DYNAMIC ARRAY BUT TYPE %d",
5714 arg->type);
5715 return rc;
5716 }
5717
5718 sa = (struct sockaddr_storage *)(data + offset);
5719
5720 if (sa->ss_family == AF_INET) {
5721 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
5722
5723 if (len < sizeof(struct sockaddr_in)) {
5724 trace_seq_printf(s, "INVALIDIPv4");
5725 return rc;
5726 }
5727
5728 print_ip4_addr(s, i, reverse, (unsigned char *) &sa4->sin_addr);
5729 if (have_p)
5730 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
5731
5732
5733 } else if (sa->ss_family == AF_INET6) {
5734 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
5735
5736 if (len < sizeof(struct sockaddr_in6)) {
5737 trace_seq_printf(s, "INVALIDIPv6");
5738 return rc;
5739 }
5740
5741 if (have_p)
5742 trace_seq_printf(s, "[");
5743
5744 buf = (unsigned char *) &sa6->sin6_addr;
5745 if (have_c)
5746 print_ip6c_addr(s, buf);
5747 else
5748 print_ip6_addr(s, i, buf);
5749
5750 if (have_p)
5751 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
5752 }
5753
5754 return rc;
5755 }
5756
print_ip_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5757 static int print_ip_arg(struct trace_seq *s, const char *ptr,
5758 void *data, int size, struct tep_event *event,
5759 struct tep_print_arg *arg)
5760 {
5761 char i = *ptr; /* 'i' or 'I' */
5762 int rc = 1;
5763
5764 /* IP version */
5765 ptr++;
5766
5767 switch (*ptr) {
5768 case '4':
5769 rc += print_ipv4_arg(s, ptr + 1, i, data, size, event, arg);
5770 break;
5771 case '6':
5772 rc += print_ipv6_arg(s, ptr + 1, i, data, size, event, arg);
5773 break;
5774 case 'S':
5775 rc += print_ipsa_arg(s, ptr + 1, i, data, size, event, arg);
5776 break;
5777 default:
5778 return 0;
5779 }
5780
5781 return rc;
5782 }
5783
5784 static const int guid_index[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
5785 static const int uuid_index[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
5786
print_uuid_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)5787 static int print_uuid_arg(struct trace_seq *s, const char *ptr,
5788 void *data, int size, struct tep_event *event,
5789 struct tep_print_arg *arg)
5790 {
5791 const int *index = uuid_index;
5792 char *format = "%02x";
5793 int ret = 0;
5794 char *buf;
5795 int i;
5796
5797 switch (*(ptr + 1)) {
5798 case 'L':
5799 format = "%02X";
5800 /* fall through */
5801 case 'l':
5802 index = guid_index;
5803 ret++;
5804 break;
5805 case 'B':
5806 format = "%02X";
5807 /* fall through */
5808 case 'b':
5809 ret++;
5810 break;
5811 }
5812
5813 if (arg->type == TEP_PRINT_FUNC) {
5814 process_defined_func(s, data, size, event, arg);
5815 return ret;
5816 }
5817
5818 /* evaluate if the arg has a type cast */
5819 while (arg->type == TEP_PRINT_TYPE)
5820 arg = arg->typecast.item;
5821
5822 if (arg->type != TEP_PRINT_FIELD) {
5823 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5824 return ret;
5825 }
5826
5827 if (!arg->field.field) {
5828 arg->field.field =
5829 tep_find_any_field(event, arg->field.name);
5830 if (!arg->field.field) {
5831 do_warning("%s: field %s not found",
5832 __func__, arg->field.name);
5833 return ret;
5834 }
5835 }
5836
5837 if (arg->field.field->size != 16) {
5838 trace_seq_printf(s, "INVALIDUUID");
5839 return ret;
5840 }
5841
5842 buf = data + arg->field.field->offset;
5843
5844 for (i = 0; i < 16; i++) {
5845 trace_seq_printf(s, format, buf[index[i]] & 0xff);
5846 switch (i) {
5847 case 3:
5848 case 5:
5849 case 7:
5850 case 9:
5851 trace_seq_printf(s, "-");
5852 break;
5853 }
5854 }
5855
5856 return ret;
5857 }
5858
print_raw_buff_arg(struct trace_seq * s,const char * ptr,void * data,int size,struct tep_event * event,struct tep_print_arg * arg,int print_len)5859 static int print_raw_buff_arg(struct trace_seq *s, const char *ptr,
5860 void *data, int size, struct tep_event *event,
5861 struct tep_print_arg *arg, int print_len)
5862 {
5863 unsigned int offset, arr_len;
5864 int plen = print_len;
5865 char *delim = " ";
5866 int ret = 0;
5867 char *buf;
5868 int i;
5869
5870 switch (*(ptr + 1)) {
5871 case 'C':
5872 delim = ":";
5873 ret++;
5874 break;
5875 case 'D':
5876 delim = "-";
5877 ret++;
5878 break;
5879 case 'N':
5880 delim = "";
5881 ret++;
5882 break;
5883 }
5884
5885 if (arg->type == TEP_PRINT_FUNC) {
5886 process_defined_func(s, data, size, event, arg);
5887 return ret;
5888 }
5889
5890 if (arg->type != TEP_PRINT_DYNAMIC_ARRAY) {
5891 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5892 return ret;
5893 }
5894
5895 dynamic_offset_field(event->tep, arg->dynarray.field, data, size,
5896 &offset, &arr_len);
5897 buf = data + offset;
5898
5899 if (arr_len < plen)
5900 plen = arr_len;
5901
5902 if (plen < 1)
5903 return ret;
5904
5905 trace_seq_printf(s, "%02x", buf[0] & 0xff);
5906 for (i = 1; i < plen; i++)
5907 trace_seq_printf(s, "%s%02x", delim, buf[i] & 0xff);
5908
5909 return ret;
5910 }
5911
is_printable_array(char * p,unsigned int len)5912 static int is_printable_array(char *p, unsigned int len)
5913 {
5914 unsigned int i;
5915
5916 for (i = 0; i < len && p[i]; i++)
5917 if (!isprint(p[i]) && !isspace(p[i]))
5918 return 0;
5919 return 1;
5920 }
5921
print_field_raw(struct trace_seq * s,void * data,int size,struct tep_format_field * field)5922 static void print_field_raw(struct trace_seq *s, void *data, int size,
5923 struct tep_format_field *field)
5924 {
5925 struct tep_handle *tep = field->event->tep;
5926 unsigned int offset, len, i;
5927 unsigned long long val;
5928
5929 if (field->flags & TEP_FIELD_IS_ARRAY) {
5930 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
5931 dynamic_offset_field(tep, field, data, size, &offset, &len);
5932 } else {
5933 offset = field->offset;
5934 len = field->size;
5935 }
5936 if (field->flags & TEP_FIELD_IS_STRING &&
5937 is_printable_array(data + offset, len)) {
5938 trace_seq_printf(s, "%s", (char *)data + offset);
5939 } else {
5940 trace_seq_puts(s, "ARRAY[");
5941 for (i = 0; i < len; i++) {
5942 if (i)
5943 trace_seq_puts(s, ", ");
5944 trace_seq_printf(s, "%02x",
5945 *((unsigned char *)data + offset + i));
5946 }
5947 trace_seq_putc(s, ']');
5948 field->flags &= ~TEP_FIELD_IS_STRING;
5949 }
5950 } else {
5951 val = tep_read_number(tep, data + field->offset,
5952 field->size);
5953 if (field->flags & TEP_FIELD_IS_POINTER) {
5954 trace_seq_printf(s, "0x%llx", val);
5955 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
5956 switch (field->size) {
5957 case 4:
5958 /*
5959 * If field is long then print it in hex.
5960 * A long usually stores pointers.
5961 */
5962 if (field->flags & TEP_FIELD_IS_LONG)
5963 trace_seq_printf(s, "0x%x", (int)val);
5964 else
5965 trace_seq_printf(s, "%d", (int)val);
5966 break;
5967 case 2:
5968 trace_seq_printf(s, "%2d", (short)val);
5969 break;
5970 case 1:
5971 trace_seq_printf(s, "%1d", (char)val);
5972 break;
5973 default:
5974 trace_seq_printf(s, "%lld", val);
5975 }
5976 } else {
5977 if (field->flags & TEP_FIELD_IS_LONG)
5978 trace_seq_printf(s, "0x%llx", val);
5979 else
5980 trace_seq_printf(s, "%llu", val);
5981 }
5982 }
5983 trace_seq_terminate(s);
5984 }
5985
5986 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
5987 void *data, int size, struct tep_event *event);
5988
print_field(struct trace_seq * s,void * data,int size,struct tep_format_field * field,struct tep_print_parse ** parse_ptr)5989 static inline void print_field(struct trace_seq *s, void *data, int size,
5990 struct tep_format_field *field,
5991 struct tep_print_parse **parse_ptr)
5992 {
5993 struct tep_event *event = field->event;
5994 struct tep_print_parse *start_parse;
5995 struct tep_print_parse *parse;
5996 struct tep_print_arg *arg;
5997 bool has_0x = false;
5998
5999 parse = parse_ptr ? *parse_ptr : event->print_fmt.print_cache;
6000
6001 if (!parse || event->flags & TEP_EVENT_FL_FAILED)
6002 goto out;
6003
6004 if (field->flags & (TEP_FIELD_IS_ARRAY | TEP_FIELD_IS_STRING))
6005 goto out;
6006
6007 start_parse = parse;
6008 do {
6009 if (parse->type == PRINT_FMT_STRING) {
6010 int len = strlen(parse->format);
6011
6012 if (len > 1 &&
6013 strcmp(parse->format + (len -2), "0x") == 0)
6014 has_0x = true;
6015 else
6016 has_0x = false;
6017
6018 goto next;
6019 }
6020
6021 arg = parse->arg;
6022
6023 while (arg && arg->type == TEP_PRINT_TYPE)
6024 arg = arg->typecast.item;
6025
6026 if (!arg || arg->type != TEP_PRINT_FIELD ||
6027 arg->field.field != field) {
6028 has_0x = false;
6029 goto next;
6030 }
6031
6032 if (has_0x)
6033 trace_seq_puts(s, "0x");
6034
6035 print_parse_data(parse, s, data, size, event);
6036
6037 if (parse_ptr)
6038 *parse_ptr = parse->next;
6039
6040 return;
6041
6042 next:
6043 parse = parse->next ? parse->next :
6044 event->print_fmt.print_cache;
6045 } while (parse != start_parse);
6046
6047 out:
6048 /* Not found. */
6049 print_field_raw(s, data, size, field);
6050 }
6051
6052 /**
6053 * tep_print_field_content - write out the raw content of a field
6054 * @s: The trace_seq to write the content into
6055 * @data: The payload to extract the field from.
6056 * @size: The size of the payload.
6057 * @field: The field to extract
6058 *
6059 * Use @field to find the field content from within @data and write it
6060 * in human readable format into @s.
6061 *
6062 * It will not write anything on error (s->len will not move)
6063 */
tep_print_field_content(struct trace_seq * s,void * data,int size,struct tep_format_field * field)6064 void tep_print_field_content(struct trace_seq *s, void *data, int size,
6065 struct tep_format_field *field)
6066 {
6067 print_field(s, data, size, field, NULL);
6068 }
6069
6070 /** DEPRECATED **/
tep_print_field(struct trace_seq * s,void * data,struct tep_format_field * field)6071 void tep_print_field(struct trace_seq *s, void *data,
6072 struct tep_format_field *field)
6073 {
6074 /* unsafe to use, should pass in size */
6075 print_field(s, data, 4096, field, NULL);
6076 }
6077
6078 static inline void
print_selected_fields(struct trace_seq * s,void * data,int size,struct tep_event * event,unsigned long long ignore_mask)6079 print_selected_fields(struct trace_seq *s, void *data, int size,
6080 struct tep_event *event,
6081 unsigned long long ignore_mask)
6082 {
6083 struct tep_print_parse *parse = event->print_fmt.print_cache;
6084 struct tep_format_field *field;
6085 unsigned long long field_mask = 1;
6086
6087 field = event->format.fields;
6088 for(; field; field = field->next, field_mask <<= 1) {
6089 if (field_mask & ignore_mask)
6090 continue;
6091
6092 trace_seq_printf(s, " %s=", field->name);
6093 print_field(s, data, size, field, &parse);
6094 }
6095 }
6096
tep_print_fields(struct trace_seq * s,void * data,int size,struct tep_event * event)6097 void tep_print_fields(struct trace_seq *s, void *data,
6098 int size, struct tep_event *event)
6099 {
6100 print_selected_fields(s, data, size, event, 0);
6101 }
6102
6103 /**
6104 * tep_record_print_fields - print the field name followed by the
6105 * record's field value.
6106 * @s: The seq to print to
6107 * @record: The record to get the event from
6108 * @event: The event that the field is for
6109 */
tep_record_print_fields(struct trace_seq * s,struct tep_record * record,struct tep_event * event)6110 void tep_record_print_fields(struct trace_seq *s,
6111 struct tep_record *record,
6112 struct tep_event *event)
6113 {
6114 print_selected_fields(s, record->data, record->size, event, 0);
6115 }
6116
6117 /**
6118 * tep_record_print_selected_fields - print the field name followed by the
6119 * record's field value for a selected subset of record fields.
6120 * @s: The seq to print to
6121 * @record: The record to get the event from
6122 * @event: The event that the field is for
6123 * @select_mask: Bit mask defining the fields to print
6124 */
tep_record_print_selected_fields(struct trace_seq * s,struct tep_record * record,struct tep_event * event,unsigned long long select_mask)6125 void tep_record_print_selected_fields(struct trace_seq *s,
6126 struct tep_record *record,
6127 struct tep_event *event,
6128 unsigned long long select_mask)
6129 {
6130 unsigned long long ignore_mask = ~select_mask;
6131
6132 print_selected_fields(s, record->data, record->size, event, ignore_mask);
6133 }
6134
print_function(struct trace_seq * s,const char * format,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)6135 static int print_function(struct trace_seq *s, const char *format,
6136 void *data, int size, struct tep_event *event,
6137 struct tep_print_arg *arg)
6138 {
6139 struct func_map *func;
6140 unsigned long long val;
6141
6142 val = eval_num_arg(data, size, event, arg);
6143 func = find_func(event->tep, val);
6144 if (func) {
6145 trace_seq_puts(s, func->func);
6146 if (*format == 'F' || *format == 'S')
6147 trace_seq_printf(s, "+0x%llx", val - func->addr);
6148 } else {
6149 if (event->tep->long_size == 4)
6150 trace_seq_printf(s, "0x%lx", (long)val);
6151 else
6152 trace_seq_printf(s, "0x%llx", (long long)val);
6153 }
6154
6155 return 0;
6156 }
6157
print_arg_pointer(struct trace_seq * s,const char * format,int plen,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)6158 static int print_arg_pointer(struct trace_seq *s, const char *format, int plen,
6159 void *data, int size,
6160 struct tep_event *event, struct tep_print_arg *arg)
6161 {
6162 unsigned long long val;
6163 int ret = 1;
6164
6165 if (arg->type == TEP_PRINT_BSTRING) {
6166 trace_seq_puts(s, arg->string.string);
6167 return 0;
6168 }
6169 while (*format) {
6170 if (*format == 'p') {
6171 format++;
6172 break;
6173 }
6174 format++;
6175 }
6176
6177 switch (*format) {
6178 case 'F':
6179 case 'f':
6180 case 'S':
6181 case 's':
6182 ret += print_function(s, format, data, size, event, arg);
6183 break;
6184 case 'M':
6185 case 'm':
6186 ret += print_mac_arg(s, format, data, size, event, arg);
6187 break;
6188 case 'I':
6189 case 'i':
6190 ret += print_ip_arg(s, format, data, size, event, arg);
6191 break;
6192 case 'U':
6193 ret += print_uuid_arg(s, format, data, size, event, arg);
6194 break;
6195 case 'h':
6196 ret += print_raw_buff_arg(s, format, data, size, event, arg, plen);
6197 break;
6198 default:
6199 ret = 0;
6200 val = eval_num_arg(data, size, event, arg);
6201 trace_seq_printf(s, "%p", (void *)(intptr_t)val);
6202 break;
6203 }
6204
6205 return ret;
6206
6207 }
6208
print_arg_number(struct trace_seq * s,const char * format,int plen,void * data,int size,int ls,struct tep_event * event,struct tep_print_arg * arg)6209 static int print_arg_number(struct trace_seq *s, const char *format, int plen,
6210 void *data, int size, int ls,
6211 struct tep_event *event, struct tep_print_arg *arg)
6212 {
6213 unsigned long long val;
6214
6215 val = eval_num_arg(data, size, event, arg);
6216
6217 switch (ls) {
6218 case -2:
6219 if (plen >= 0)
6220 trace_seq_printf(s, format, plen, (char)val);
6221 else
6222 trace_seq_printf(s, format, (char)val);
6223 break;
6224 case -1:
6225 if (plen >= 0)
6226 trace_seq_printf(s, format, plen, (short)val);
6227 else
6228 trace_seq_printf(s, format, (short)val);
6229 break;
6230 case 0:
6231 if (plen >= 0)
6232 trace_seq_printf(s, format, plen, (int)val);
6233 else
6234 trace_seq_printf(s, format, (int)val);
6235 break;
6236 case 1:
6237 if (plen >= 0)
6238 trace_seq_printf(s, format, plen, (long)val);
6239 else
6240 trace_seq_printf(s, format, (long)val);
6241 break;
6242 case 2:
6243 if (plen >= 0)
6244 trace_seq_printf(s, format, plen, (long long)val);
6245 else
6246 trace_seq_printf(s, format, (long long)val);
6247 break;
6248 default:
6249 do_warning_event(event, "bad count (%d)", ls);
6250 event->flags |= TEP_EVENT_FL_FAILED;
6251 }
6252 return 0;
6253 }
6254
6255
print_arg_string(struct trace_seq * s,const char * format,int plen,void * data,int size,struct tep_event * event,struct tep_print_arg * arg)6256 static void print_arg_string(struct trace_seq *s, const char *format, int plen,
6257 void *data, int size,
6258 struct tep_event *event, struct tep_print_arg *arg)
6259 {
6260 struct trace_seq p;
6261
6262 /* Use helper trace_seq */
6263 trace_seq_init(&p);
6264 print_str_arg(&p, data, size, event,
6265 format, plen, arg);
6266 trace_seq_terminate(&p);
6267 trace_seq_puts(s, p.buffer);
6268 trace_seq_destroy(&p);
6269 }
6270
parse_arg_format_pointer(const char * format)6271 static int parse_arg_format_pointer(const char *format)
6272 {
6273 int ret = 0;
6274 int index;
6275 int loop;
6276
6277 switch (*format) {
6278 case 'F':
6279 case 'S':
6280 case 'f':
6281 case 's':
6282 ret++;
6283 break;
6284 case 'M':
6285 case 'm':
6286 /* [mM]R , [mM]F */
6287 switch (format[1]) {
6288 case 'R':
6289 case 'F':
6290 ret++;
6291 break;
6292 }
6293 ret++;
6294 break;
6295 case 'I':
6296 case 'i':
6297 index = 2;
6298 loop = 1;
6299 switch (format[1]) {
6300 case 'S':
6301 /*[S][pfs]*/
6302 while (loop) {
6303 switch (format[index]) {
6304 case 'p':
6305 case 'f':
6306 case 's':
6307 ret++;
6308 index++;
6309 break;
6310 default:
6311 loop = 0;
6312 break;
6313 }
6314 }
6315 /* fall through */
6316 case '4':
6317 /* [4S][hnbl] */
6318 switch (format[index]) {
6319 case 'h':
6320 case 'n':
6321 case 'l':
6322 case 'b':
6323 ret++;
6324 index++;
6325 break;
6326 }
6327 if (format[1] == '4') {
6328 ret++;
6329 break;
6330 }
6331 /* fall through */
6332 case '6':
6333 /* [6S]c */
6334 if (format[index] == 'c')
6335 ret++;
6336 ret++;
6337 break;
6338 }
6339 ret++;
6340 break;
6341 case 'U':
6342 switch (format[1]) {
6343 case 'L':
6344 case 'l':
6345 case 'B':
6346 case 'b':
6347 ret++;
6348 break;
6349 }
6350 ret++;
6351 break;
6352 case 'h':
6353 switch (format[1]) {
6354 case 'C':
6355 case 'D':
6356 case 'N':
6357 ret++;
6358 break;
6359 }
6360 ret++;
6361 break;
6362 default:
6363 break;
6364 }
6365
6366 return ret;
6367 }
6368
free_parse_args(struct tep_print_parse * arg)6369 static void free_parse_args(struct tep_print_parse *arg)
6370 {
6371 struct tep_print_parse *del;
6372
6373 while (arg) {
6374 del = arg;
6375 arg = del->next;
6376 free(del->format);
6377 free(del);
6378 }
6379 }
6380
parse_arg_add(struct tep_print_parse ** parse,char * format,enum tep_print_parse_type type,struct tep_print_arg * arg,struct tep_print_arg * len_as_arg,int ls)6381 static int parse_arg_add(struct tep_print_parse **parse, char *format,
6382 enum tep_print_parse_type type,
6383 struct tep_print_arg *arg,
6384 struct tep_print_arg *len_as_arg,
6385 int ls)
6386 {
6387 struct tep_print_parse *parg = NULL;
6388
6389 parg = calloc(1, sizeof(*parg));
6390 if (!parg)
6391 goto error;
6392 parg->format = strdup(format);
6393 if (!parg->format)
6394 goto error;
6395 parg->type = type;
6396 parg->arg = arg;
6397 parg->len_as_arg = len_as_arg;
6398 parg->ls = ls;
6399 *parse = parg;
6400 return 0;
6401 error:
6402 if (parg) {
6403 free(parg->format);
6404 free(parg);
6405 }
6406 return -1;
6407 }
6408
parse_arg_format(struct tep_print_parse ** parse,struct tep_event * event,const char * format,struct tep_print_arg ** arg)6409 static int parse_arg_format(struct tep_print_parse **parse,
6410 struct tep_event *event,
6411 const char *format, struct tep_print_arg **arg)
6412 {
6413 struct tep_print_arg *len_arg = NULL;
6414 char print_format[32];
6415 const char *start = format;
6416 int ret = 0;
6417 int ls = 0;
6418 int res;
6419 int len;
6420
6421 format++;
6422 ret++;
6423 for (; *format; format++) {
6424 switch (*format) {
6425 case '#':
6426 /* FIXME: need to handle properly */
6427 break;
6428 case 'h':
6429 ls--;
6430 break;
6431 case 'l':
6432 ls++;
6433 break;
6434 case 'L':
6435 ls = 2;
6436 break;
6437 case 'z':
6438 case 'Z':
6439 ls = 1;
6440 break;
6441 case '.':
6442 case '0' ... '9':
6443 case '-':
6444 break;
6445 case '*':
6446 /* The argument is the length. */
6447 if (!*arg) {
6448 do_warning_event(event, "no argument match");
6449 event->flags |= TEP_EVENT_FL_FAILED;
6450 goto out_failed;
6451 }
6452 if (len_arg) {
6453 do_warning_event(event, "argument already matched");
6454 event->flags |= TEP_EVENT_FL_FAILED;
6455 goto out_failed;
6456 }
6457 len_arg = *arg;
6458 *arg = (*arg)->next;
6459 break;
6460 case 'p':
6461 if (!*arg) {
6462 do_warning_event(event, "no argument match");
6463 event->flags |= TEP_EVENT_FL_FAILED;
6464 goto out_failed;
6465 }
6466 res = parse_arg_format_pointer(format + 1);
6467 if (res > 0) {
6468 format += res;
6469 ret += res;
6470 }
6471 len = ((unsigned long)format + 1) -
6472 (unsigned long)start;
6473 /* should never happen */
6474 if (len > 31) {
6475 do_warning_event(event, "bad format!");
6476 event->flags |= TEP_EVENT_FL_FAILED;
6477 len = 31;
6478 }
6479 memcpy(print_format, start, len);
6480 print_format[len] = 0;
6481
6482 parse_arg_add(parse, print_format,
6483 PRINT_FMT_ARG_POINTER, *arg, len_arg, ls);
6484 *arg = (*arg)->next;
6485 ret++;
6486 return ret;
6487 case 'd':
6488 case 'u':
6489 case 'i':
6490 case 'x':
6491 case 'X':
6492 case 'o':
6493 if (!*arg) {
6494 do_warning_event(event, "no argument match");
6495 event->flags |= TEP_EVENT_FL_FAILED;
6496 goto out_failed;
6497 }
6498
6499 len = ((unsigned long)format + 1) -
6500 (unsigned long)start;
6501
6502 /* should never happen */
6503 if (len > 30) {
6504 do_warning_event(event, "bad format!");
6505 event->flags |= TEP_EVENT_FL_FAILED;
6506 len = 31;
6507 }
6508 memcpy(print_format, start, len);
6509 print_format[len] = 0;
6510
6511 if (event->tep->long_size == 8 && ls == 1 &&
6512 sizeof(long) != 8) {
6513 char *p;
6514
6515 /* make %l into %ll */
6516 if (ls == 1 && (p = strchr(print_format, 'l')))
6517 memmove(p+1, p, strlen(p)+1);
6518 ls = 2;
6519 }
6520 if (ls < -2 || ls > 2) {
6521 do_warning_event(event, "bad count (%d)", ls);
6522 event->flags |= TEP_EVENT_FL_FAILED;
6523 }
6524 parse_arg_add(parse, print_format,
6525 PRINT_FMT_ARG_DIGIT, *arg, len_arg, ls);
6526 *arg = (*arg)->next;
6527 ret++;
6528 return ret;
6529 case 's':
6530 if (!*arg) {
6531 do_warning_event(event, "no matching argument");
6532 event->flags |= TEP_EVENT_FL_FAILED;
6533 goto out_failed;
6534 }
6535
6536 len = ((unsigned long)format + 1) -
6537 (unsigned long)start;
6538
6539 /* should never happen */
6540 if (len > 31) {
6541 do_warning_event(event, "bad format!");
6542 event->flags |= TEP_EVENT_FL_FAILED;
6543 len = 31;
6544 }
6545
6546 memcpy(print_format, start, len);
6547 print_format[len] = 0;
6548
6549 parse_arg_add(parse, print_format,
6550 PRINT_FMT_ARG_STRING, *arg, len_arg, 0);
6551 *arg = (*arg)->next;
6552 ret++;
6553 return ret;
6554 default:
6555 snprintf(print_format, 32, ">%c<", *format);
6556 parse_arg_add(parse, print_format,
6557 PRINT_FMT_STRING, NULL, NULL, 0);
6558 ret++;
6559 return ret;
6560 }
6561 ret++;
6562 }
6563
6564 out_failed:
6565 return ret;
6566
6567 }
6568
parse_arg_string(struct tep_print_parse ** parse,const char * format)6569 static int parse_arg_string(struct tep_print_parse **parse, const char *format)
6570 {
6571 struct trace_seq s;
6572 int ret = 0;
6573
6574 trace_seq_init(&s);
6575 for (; *format; format++) {
6576 if (*format == '\\') {
6577 format++;
6578 ret++;
6579 switch (*format) {
6580 case 'n':
6581 trace_seq_putc(&s, '\n');
6582 break;
6583 case 't':
6584 trace_seq_putc(&s, '\t');
6585 break;
6586 case 'r':
6587 trace_seq_putc(&s, '\r');
6588 break;
6589 case '\\':
6590 trace_seq_putc(&s, '\\');
6591 break;
6592 default:
6593 trace_seq_putc(&s, *format);
6594 break;
6595 }
6596 } else if (*format == '%') {
6597 if (*(format + 1) == '%') {
6598 trace_seq_putc(&s, '%');
6599 format++;
6600 ret++;
6601 } else
6602 break;
6603 } else
6604 trace_seq_putc(&s, *format);
6605
6606 ret++;
6607 }
6608 trace_seq_terminate(&s);
6609 parse_arg_add(parse, s.buffer, PRINT_FMT_STRING, NULL, NULL, 0);
6610 trace_seq_destroy(&s);
6611
6612 return ret;
6613 }
6614
6615 static struct tep_print_parse *
parse_args(struct tep_event * event,const char * format,struct tep_print_arg * arg)6616 parse_args(struct tep_event *event, const char *format, struct tep_print_arg *arg)
6617 {
6618 struct tep_print_parse *parse_ret = NULL;
6619 struct tep_print_parse **parse = NULL;
6620 int ret;
6621 int len;
6622
6623 len = strlen(format);
6624 while (*format) {
6625 if (!parse_ret)
6626 parse = &parse_ret;
6627 if (*format == '%' && *(format + 1) != '%')
6628 ret = parse_arg_format(parse, event, format, &arg);
6629 else
6630 ret = parse_arg_string(parse, format);
6631 if (*parse)
6632 parse = &((*parse)->next);
6633
6634 len -= ret;
6635 if (len > 0)
6636 format += ret;
6637 else
6638 break;
6639 }
6640 return parse_ret;
6641 }
6642
print_parse_data(struct tep_print_parse * parse,struct trace_seq * s,void * data,int size,struct tep_event * event)6643 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
6644 void *data, int size, struct tep_event *event)
6645 {
6646 int len_arg;
6647
6648 if (parse->len_as_arg)
6649 len_arg = eval_num_arg(data, size, event, parse->len_as_arg);
6650
6651 switch (parse->type) {
6652 case PRINT_FMT_ARG_DIGIT:
6653 print_arg_number(s, parse->format,
6654 parse->len_as_arg ? len_arg : -1, data,
6655 size, parse->ls, event, parse->arg);
6656 break;
6657 case PRINT_FMT_ARG_POINTER:
6658 print_arg_pointer(s, parse->format,
6659 parse->len_as_arg ? len_arg : 1,
6660 data, size, event, parse->arg);
6661 break;
6662 case PRINT_FMT_ARG_STRING:
6663 print_arg_string(s, parse->format,
6664 parse->len_as_arg ? len_arg : -1,
6665 data, size, event, parse->arg);
6666 break;
6667 case PRINT_FMT_STRING:
6668 default:
6669 trace_seq_printf(s, "%s", parse->format);
6670 /* Return 1 on non field. */
6671 return 1;
6672 }
6673 /* Return 0 on field being processed. */
6674 return 0;
6675 }
6676
print_event_cache(struct tep_print_parse * parse,struct trace_seq * s,void * data,int size,struct tep_event * event)6677 static void print_event_cache(struct tep_print_parse *parse, struct trace_seq *s,
6678 void *data, int size, struct tep_event *event)
6679 {
6680 while (parse) {
6681 print_parse_data(parse, s, data, size, event);
6682 parse = parse->next;
6683 }
6684 }
6685
pretty_print(struct trace_seq * s,void * data,int size,struct tep_event * event)6686 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
6687 {
6688 struct tep_print_parse *parse = event->print_fmt.print_cache;
6689 struct tep_print_arg *args = NULL;
6690 char *bprint_fmt = NULL;
6691
6692 if (event->flags & TEP_EVENT_FL_FAILED) {
6693 trace_seq_printf(s, "[FAILED TO PARSE]");
6694 tep_print_fields(s, data, size, event);
6695 return;
6696 }
6697
6698 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
6699 bprint_fmt = get_bprint_format(data, size, event);
6700 args = make_bprint_args(bprint_fmt, data, size, event);
6701 parse = parse_args(event, bprint_fmt, args);
6702 }
6703
6704 print_event_cache(parse, s, data, size, event);
6705
6706 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
6707 free_parse_args(parse);
6708 free_args(args);
6709 free(bprint_fmt);
6710 }
6711 }
6712
6713 /*
6714 * This parses out the Latency format (interrupts disabled,
6715 * need rescheduling, in hard/soft interrupt, preempt count
6716 * and lock depth) and places it into the trace_seq.
6717 */
data_latency_format(struct tep_handle * tep,struct trace_seq * s,char * format,struct tep_record * record)6718 static void data_latency_format(struct tep_handle *tep, struct trace_seq *s,
6719 char *format, struct tep_record *record)
6720 {
6721 static int check_lock_depth = 1;
6722 static int check_migrate_disable = 1;
6723 static int lock_depth_exists;
6724 static int migrate_disable_exists;
6725 unsigned int lat_flags;
6726 struct trace_seq sq;
6727 unsigned int pc;
6728 int lock_depth = 0;
6729 int migrate_disable = 0;
6730 int hardirq;
6731 int softirq;
6732 void *data = record->data;
6733
6734 trace_seq_init(&sq);
6735 lat_flags = parse_common_flags(tep, data);
6736 pc = parse_common_pc(tep, data);
6737 /* lock_depth may not always exist */
6738 if (lock_depth_exists)
6739 lock_depth = parse_common_lock_depth(tep, data);
6740 else if (check_lock_depth) {
6741 lock_depth = parse_common_lock_depth(tep, data);
6742 if (lock_depth < 0)
6743 check_lock_depth = 0;
6744 else
6745 lock_depth_exists = 1;
6746 }
6747
6748 /* migrate_disable may not always exist */
6749 if (migrate_disable_exists)
6750 migrate_disable = parse_common_migrate_disable(tep, data);
6751 else if (check_migrate_disable) {
6752 migrate_disable = parse_common_migrate_disable(tep, data);
6753 if (migrate_disable < 0)
6754 check_migrate_disable = 0;
6755 else
6756 migrate_disable_exists = 1;
6757 }
6758
6759 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
6760 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
6761
6762 trace_seq_printf(&sq, "%c%c%c",
6763 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
6764 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
6765 'X' : '.',
6766 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
6767 'N' : '.',
6768 (hardirq && softirq) ? 'H' :
6769 hardirq ? 'h' : softirq ? 's' : '.');
6770
6771 if (pc & 0xf)
6772 trace_seq_printf(&sq, "%x", pc & 0xf);
6773 else
6774 trace_seq_printf(&sq, ".");
6775
6776 if (pc & 0xf0)
6777 trace_seq_printf(&sq, "%x", pc >> 4);
6778 else
6779 trace_seq_printf(&sq, ".");
6780
6781 if (migrate_disable_exists) {
6782 if (migrate_disable < 0)
6783 trace_seq_printf(&sq, ".");
6784 else
6785 trace_seq_printf(&sq, "%d", migrate_disable);
6786 }
6787
6788 if (lock_depth_exists) {
6789 if (lock_depth < 0)
6790 trace_seq_printf(&sq, ".");
6791 else
6792 trace_seq_printf(&sq, "%d", lock_depth);
6793 }
6794
6795 if (sq.state == TRACE_SEQ__MEM_ALLOC_FAILED) {
6796 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
6797 return;
6798 }
6799
6800 trace_seq_terminate(&sq);
6801 trace_seq_puts(s, sq.buffer);
6802 trace_seq_destroy(&sq);
6803 trace_seq_terminate(s);
6804 }
6805
6806 /**
6807 * tep_data_type - parse out the given event type
6808 * @tep: a handle to the trace event parser context
6809 * @rec: the record to read from
6810 *
6811 * This returns the event id from the @rec.
6812 */
tep_data_type(struct tep_handle * tep,struct tep_record * rec)6813 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
6814 {
6815 return trace_parse_common_type(tep, rec->data);
6816 }
6817
6818 /**
6819 * tep_data_pid - parse the PID from record
6820 * @tep: a handle to the trace event parser context
6821 * @rec: the record to parse
6822 *
6823 * This returns the PID from a record.
6824 */
tep_data_pid(struct tep_handle * tep,struct tep_record * rec)6825 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
6826 {
6827 return parse_common_pid(tep, rec->data);
6828 }
6829
6830 /**
6831 * tep_data_preempt_count - parse the preempt count from the record
6832 * @tep: a handle to the trace event parser context
6833 * @rec: the record to parse
6834 *
6835 * This returns the preempt count from a record.
6836 */
tep_data_preempt_count(struct tep_handle * tep,struct tep_record * rec)6837 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
6838 {
6839 return parse_common_pc(tep, rec->data);
6840 }
6841
6842 /**
6843 * tep_data_flags - parse the latency flags from the record
6844 * @tep: a handle to the trace event parser context
6845 * @rec: the record to parse
6846 *
6847 * This returns the latency flags from a record.
6848 *
6849 * Use trace_flag_type enum for the flags (see event-parse.h).
6850 */
tep_data_flags(struct tep_handle * tep,struct tep_record * rec)6851 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
6852 {
6853 return parse_common_flags(tep, rec->data);
6854 }
6855
6856 /**
6857 * tep_data_comm_from_pid - return the command line from PID
6858 * @tep: a handle to the trace event parser context
6859 * @pid: the PID of the task to search for
6860 *
6861 * This returns a pointer to the command line that has the given
6862 * @pid.
6863 */
tep_data_comm_from_pid(struct tep_handle * tep,int pid)6864 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
6865 {
6866 const char *comm;
6867
6868 comm = find_cmdline(tep, pid);
6869 return comm;
6870 }
6871
6872 static struct tep_cmdline *
pid_from_cmdlist(struct tep_handle * tep,const char * comm,struct tep_cmdline * next)6873 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
6874 {
6875 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
6876
6877 if (cmdlist)
6878 cmdlist = cmdlist->next;
6879 else
6880 cmdlist = tep->cmdlist;
6881
6882 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
6883 cmdlist = cmdlist->next;
6884
6885 return (struct tep_cmdline *)cmdlist;
6886 }
6887
6888 /**
6889 * tep_data_pid_from_comm - return the pid from a given comm
6890 * @tep: a handle to the trace event parser context
6891 * @comm: the cmdline to find the pid from
6892 * @next: the cmdline structure to find the next comm
6893 *
6894 * This returns the cmdline structure that holds a pid for a given
6895 * comm, or NULL if none found. As there may be more than one pid for
6896 * a given comm, the result of this call can be passed back into
6897 * a recurring call in the @next parameter, and then it will find the
6898 * next pid.
6899 * Also, it does a linear search, so it may be slow.
6900 */
tep_data_pid_from_comm(struct tep_handle * tep,const char * comm,struct tep_cmdline * next)6901 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
6902 struct tep_cmdline *next)
6903 {
6904 struct tep_cmdline *cmdline;
6905
6906 /*
6907 * If the cmdlines have not been converted yet, then use
6908 * the list.
6909 */
6910 if (!tep->cmdlines)
6911 return pid_from_cmdlist(tep, comm, next);
6912
6913 if (next) {
6914 /*
6915 * The next pointer could have been still from
6916 * a previous call before cmdlines were created
6917 */
6918 if (next < tep->cmdlines ||
6919 next >= tep->cmdlines + tep->cmdline_count)
6920 next = NULL;
6921 else
6922 cmdline = next++;
6923 }
6924
6925 if (!next)
6926 cmdline = tep->cmdlines;
6927
6928 while (cmdline < tep->cmdlines + tep->cmdline_count) {
6929 if (strcmp(cmdline->comm, comm) == 0)
6930 return cmdline;
6931 cmdline++;
6932 }
6933 return NULL;
6934 }
6935
6936 /**
6937 * tep_cmdline_pid - return the pid associated to a given cmdline
6938 * @tep: a handle to the trace event parser context
6939 * @cmdline: The cmdline structure to get the pid from
6940 *
6941 * Returns the pid for a give cmdline. If @cmdline is NULL, then
6942 * -1 is returned.
6943 */
tep_cmdline_pid(struct tep_handle * tep,struct tep_cmdline * cmdline)6944 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
6945 {
6946 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
6947
6948 if (!cmdline)
6949 return -1;
6950
6951 /*
6952 * If cmdlines have not been created yet, or cmdline is
6953 * not part of the array, then treat it as a cmdlist instead.
6954 */
6955 if (!tep->cmdlines ||
6956 cmdline < tep->cmdlines ||
6957 cmdline >= tep->cmdlines + tep->cmdline_count)
6958 return cmdlist->pid;
6959
6960 return cmdline->pid;
6961 }
6962
6963 /*
6964 * This parses the raw @data using the given @event information and
6965 * writes the print format into the trace_seq.
6966 */
print_event_info(struct trace_seq * s,char * format,bool raw,struct tep_event * event,struct tep_record * record)6967 static void print_event_info(struct trace_seq *s, char *format, bool raw,
6968 struct tep_event *event, struct tep_record *record)
6969 {
6970 int print_pretty = 1;
6971
6972 if (raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
6973 tep_print_fields(s, record->data, record->size, event);
6974 else {
6975
6976 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
6977 print_pretty = event->handler(s, record, event,
6978 event->context);
6979
6980 if (print_pretty)
6981 pretty_print(s, record->data, record->size, event);
6982 }
6983
6984 trace_seq_terminate(s);
6985 }
6986
6987 /**
6988 * tep_find_event_by_record - return the event from a given record
6989 * @tep: a handle to the trace event parser context
6990 * @record: The record to get the event from
6991 *
6992 * Returns the associated event for a given record, or NULL if non is
6993 * is found.
6994 */
6995 struct tep_event *
tep_find_event_by_record(struct tep_handle * tep,struct tep_record * record)6996 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
6997 {
6998 int type;
6999
7000 if (record->size < 0) {
7001 do_warning("ug! negative record size %d", record->size);
7002 return NULL;
7003 }
7004
7005 type = trace_parse_common_type(tep, record->data);
7006
7007 return tep_find_event(tep, type);
7008 }
7009
7010 /*
7011 * Writes the timestamp of the record into @s. Time divisor and precision can be
7012 * specified as part of printf @format string. Example:
7013 * "%3.1000d" - divide the time by 1000 and print the first 3 digits
7014 * before the dot. Thus, the timestamp "123456000" will be printed as
7015 * "123.456"
7016 */
print_event_time(struct tep_handle * tep,struct trace_seq * s,char * format,struct tep_event * event,struct tep_record * record)7017 static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
7018 char *format, struct tep_event *event,
7019 struct tep_record *record)
7020 {
7021 unsigned long long time;
7022 char *divstr;
7023 int prec = 0, pr;
7024 int div = 0;
7025 int p10 = 1;
7026
7027 if (isdigit(*(format + 1)))
7028 prec = atoi(format + 1);
7029 divstr = strchr(format, '.');
7030 if (divstr && isdigit(*(divstr + 1)))
7031 div = atoi(divstr + 1);
7032 time = record->ts;
7033 if (div) {
7034 time += div / 2;
7035 time /= div;
7036 }
7037 pr = prec;
7038 while (pr--)
7039 p10 *= 10;
7040
7041 if (p10 > 1)
7042 trace_seq_printf(s, "%5llu.%0*llu", time / p10, prec, time % p10);
7043 else
7044 trace_seq_printf(s, "%12llu", time);
7045 }
7046
7047 struct print_event_type {
7048 enum {
7049 EVENT_TYPE_INT = 1,
7050 EVENT_TYPE_STRING,
7051 EVENT_TYPE_UNKNOWN,
7052 } type;
7053 char format[32];
7054 };
7055
print_string(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,struct tep_event * event,const char * arg,struct print_event_type * type)7056 static void print_string(struct tep_handle *tep, struct trace_seq *s,
7057 struct tep_record *record, struct tep_event *event,
7058 const char *arg, struct print_event_type *type)
7059 {
7060 const char *comm;
7061 int pid;
7062
7063 if (strncmp(arg, TEP_PRINT_LATENCY, strlen(TEP_PRINT_LATENCY)) == 0) {
7064 data_latency_format(tep, s, type->format, record);
7065 } else if (strncmp(arg, TEP_PRINT_COMM, strlen(TEP_PRINT_COMM)) == 0) {
7066 pid = parse_common_pid(tep, record->data);
7067 comm = find_cmdline(tep, pid);
7068 trace_seq_printf(s, type->format, comm);
7069 } else if (strncmp(arg, TEP_PRINT_INFO_RAW, strlen(TEP_PRINT_INFO_RAW)) == 0) {
7070 print_event_info(s, type->format, true, event, record);
7071 } else if (strncmp(arg, TEP_PRINT_INFO, strlen(TEP_PRINT_INFO)) == 0) {
7072 print_event_info(s, type->format, false, event, record);
7073 } else if (strncmp(arg, TEP_PRINT_NAME, strlen(TEP_PRINT_NAME)) == 0) {
7074 trace_seq_printf(s, type->format, event->name);
7075 } else {
7076 trace_seq_printf(s, "[UNKNOWN TEP TYPE %s]", arg);
7077 }
7078
7079 }
7080
print_int(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,struct tep_event * event,int arg,struct print_event_type * type)7081 static void print_int(struct tep_handle *tep, struct trace_seq *s,
7082 struct tep_record *record, struct tep_event *event,
7083 int arg, struct print_event_type *type)
7084 {
7085 int param;
7086
7087 switch (arg) {
7088 case TEP_PRINT_CPU:
7089 param = record->cpu;
7090 break;
7091 case TEP_PRINT_PID:
7092 param = parse_common_pid(tep, record->data);
7093 break;
7094 case TEP_PRINT_TIME:
7095 return print_event_time(tep, s, type->format, event, record);
7096 default:
7097 return;
7098 }
7099 trace_seq_printf(s, type->format, param);
7100 }
7101
tep_print_event_param_type(char * format,struct print_event_type * type)7102 static int tep_print_event_param_type(char *format,
7103 struct print_event_type *type)
7104 {
7105 char *str = format + 1;
7106 int i = 1;
7107
7108 type->type = EVENT_TYPE_UNKNOWN;
7109 while (*str) {
7110 switch (*str) {
7111 case 'd':
7112 case 'u':
7113 case 'i':
7114 case 'x':
7115 case 'X':
7116 case 'o':
7117 type->type = EVENT_TYPE_INT;
7118 break;
7119 case 's':
7120 type->type = EVENT_TYPE_STRING;
7121 break;
7122 }
7123 str++;
7124 i++;
7125 if (type->type != EVENT_TYPE_UNKNOWN)
7126 break;
7127 }
7128 memset(type->format, 0, 32);
7129 memcpy(type->format, format, i < 32 ? i : 31);
7130 return i;
7131 }
7132
7133 /**
7134 * tep_print_event - Write various event information
7135 * @tep: a handle to the trace event parser context
7136 * @s: the trace_seq to write to
7137 * @record: The record to get the event from
7138 * @format: a printf format string. Supported event fileds:
7139 * TEP_PRINT_PID, "%d" - event PID
7140 * TEP_PRINT_CPU, "%d" - event CPU
7141 * TEP_PRINT_COMM, "%s" - event command string
7142 * TEP_PRINT_NAME, "%s" - event name
7143 * TEP_PRINT_LATENCY, "%s" - event latency
7144 * TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
7145 * can be specified as part of this format string:
7146 * "%precision.divisord". Example:
7147 * "%3.1000d" - divide the time by 1000 and print the first
7148 * 3 digits before the dot. Thus, the time stamp
7149 * "123456000" will be printed as "123.456"
7150 * TEP_PRINT_INFO, "%s" - event information. If any width is specified in
7151 * the format string, the event information will be printed
7152 * in raw format.
7153 * Writes the specified event information into @s.
7154 */
tep_print_event(struct tep_handle * tep,struct trace_seq * s,struct tep_record * record,const char * fmt,...)7155 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
7156 struct tep_record *record, const char *fmt, ...)
7157 {
7158 struct print_event_type type;
7159 struct tep_event *event;
7160 char *current;
7161 char *format;
7162 char *str;
7163 int offset;
7164 va_list args;
7165
7166 event = tep_find_event_by_record(tep, record);
7167 if (!event) {
7168 trace_seq_printf(s, "[UNKNOWN EVENT]");
7169 return;
7170 }
7171
7172 str = current = format = strdup(fmt);
7173 if (!format)
7174 return;
7175
7176 va_start(args, fmt);
7177 while (*current) {
7178 current = strchr(str, '%');
7179 if (!current) {
7180 trace_seq_puts(s, str);
7181 break;
7182 }
7183 memset(&type, 0, sizeof(type));
7184 offset = tep_print_event_param_type(current, &type);
7185 *current = '\0';
7186 trace_seq_puts(s, str);
7187 current += offset;
7188 switch (type.type) {
7189 case EVENT_TYPE_STRING:
7190 print_string(tep, s, record, event,
7191 va_arg(args, char*), &type);
7192 break;
7193 case EVENT_TYPE_INT:
7194 print_int(tep, s, record, event,
7195 va_arg(args, int), &type);
7196 break;
7197 case EVENT_TYPE_UNKNOWN:
7198 default:
7199 trace_seq_printf(s, "[UNKNOWN TYPE]");
7200 break;
7201 }
7202 str = current;
7203
7204 }
7205 va_end(args);
7206 free(format);
7207 }
7208
events_id_cmp(const void * a,const void * b)7209 static int events_id_cmp(const void *a, const void *b)
7210 {
7211 struct tep_event * const * ea = a;
7212 struct tep_event * const * eb = b;
7213
7214 if ((*ea)->id < (*eb)->id)
7215 return -1;
7216
7217 if ((*ea)->id > (*eb)->id)
7218 return 1;
7219
7220 return 0;
7221 }
7222
events_name_cmp(const void * a,const void * b)7223 static int events_name_cmp(const void *a, const void *b)
7224 {
7225 struct tep_event * const * ea = a;
7226 struct tep_event * const * eb = b;
7227 int res;
7228
7229 res = strcmp((*ea)->name, (*eb)->name);
7230 if (res)
7231 return res;
7232
7233 res = strcmp((*ea)->system, (*eb)->system);
7234 if (res)
7235 return res;
7236
7237 return events_id_cmp(a, b);
7238 }
7239
events_system_cmp(const void * a,const void * b)7240 static int events_system_cmp(const void *a, const void *b)
7241 {
7242 struct tep_event * const * ea = a;
7243 struct tep_event * const * eb = b;
7244 int res;
7245
7246 res = strcmp((*ea)->system, (*eb)->system);
7247 if (res)
7248 return res;
7249
7250 res = strcmp((*ea)->name, (*eb)->name);
7251 if (res)
7252 return res;
7253
7254 return events_id_cmp(a, b);
7255 }
7256
list_events_copy(struct tep_handle * tep)7257 static struct tep_event **list_events_copy(struct tep_handle *tep)
7258 {
7259 struct tep_event **events;
7260
7261 if (!tep)
7262 return NULL;
7263
7264 events = malloc(sizeof(*events) * (tep->nr_events + 1));
7265 if (!events)
7266 return NULL;
7267
7268 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
7269 events[tep->nr_events] = NULL;
7270 return events;
7271 }
7272
list_events_sort(struct tep_event ** events,int nr_events,enum tep_event_sort_type sort_type)7273 static void list_events_sort(struct tep_event **events, int nr_events,
7274 enum tep_event_sort_type sort_type)
7275 {
7276 int (*sort)(const void *a, const void *b);
7277
7278 switch (sort_type) {
7279 case TEP_EVENT_SORT_ID:
7280 sort = events_id_cmp;
7281 break;
7282 case TEP_EVENT_SORT_NAME:
7283 sort = events_name_cmp;
7284 break;
7285 case TEP_EVENT_SORT_SYSTEM:
7286 sort = events_system_cmp;
7287 break;
7288 default:
7289 sort = NULL;
7290 }
7291
7292 if (sort)
7293 qsort(events, nr_events, sizeof(*events), sort);
7294 }
7295
7296 /**
7297 * tep_list_events - Get events, sorted by given criteria.
7298 * @tep: a handle to the tep context
7299 * @sort_type: desired sort order of the events in the array
7300 *
7301 * Returns an array of pointers to all events, sorted by the given
7302 * @sort_type criteria. The last element of the array is NULL. The returned
7303 * memory must not be freed, it is managed by the library.
7304 * The function is not thread safe.
7305 */
tep_list_events(struct tep_handle * tep,enum tep_event_sort_type sort_type)7306 struct tep_event **tep_list_events(struct tep_handle *tep,
7307 enum tep_event_sort_type sort_type)
7308 {
7309 struct tep_event **events;
7310
7311 if (!tep)
7312 return NULL;
7313
7314 events = tep->sort_events;
7315 if (events && tep->last_type == sort_type)
7316 return events;
7317
7318 if (!events) {
7319 events = list_events_copy(tep);
7320 if (!events)
7321 return NULL;
7322
7323 tep->sort_events = events;
7324
7325 /* the internal events are sorted by id */
7326 if (sort_type == TEP_EVENT_SORT_ID) {
7327 tep->last_type = sort_type;
7328 return events;
7329 }
7330 }
7331
7332 list_events_sort(events, tep->nr_events, sort_type);
7333 tep->last_type = sort_type;
7334
7335 return events;
7336 }
7337
7338
7339 /**
7340 * tep_list_events_copy - Thread safe version of tep_list_events()
7341 * @tep: a handle to the tep context
7342 * @sort_type: desired sort order of the events in the array
7343 *
7344 * Returns an array of pointers to all events, sorted by the given
7345 * @sort_type criteria. The last element of the array is NULL. The returned
7346 * array is newly allocated inside the function and must be freed by the caller
7347 */
tep_list_events_copy(struct tep_handle * tep,enum tep_event_sort_type sort_type)7348 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
7349 enum tep_event_sort_type sort_type)
7350 {
7351 struct tep_event **events;
7352
7353 if (!tep)
7354 return NULL;
7355
7356 events = list_events_copy(tep);
7357 if (!events)
7358 return NULL;
7359
7360 /* the internal events are sorted by id */
7361 if (sort_type == TEP_EVENT_SORT_ID)
7362 return events;
7363
7364 list_events_sort(events, tep->nr_events, sort_type);
7365
7366 return events;
7367 }
7368
7369 static struct tep_format_field **
get_event_fields(const char * type,const char * name,int count,struct tep_format_field * list)7370 get_event_fields(const char *type, const char *name,
7371 int count, struct tep_format_field *list)
7372 {
7373 struct tep_format_field **fields;
7374 struct tep_format_field *field;
7375 int i = 0;
7376
7377 fields = malloc(sizeof(*fields) * (count + 1));
7378 if (!fields)
7379 return NULL;
7380
7381 for (field = list; field; field = field->next) {
7382 fields[i++] = field;
7383 if (i == count + 1) {
7384 do_warning("event %s has more %s fields than specified",
7385 name, type);
7386 i--;
7387 break;
7388 }
7389 }
7390
7391 if (i != count)
7392 do_warning("event %s has less %s fields than specified",
7393 name, type);
7394
7395 fields[i] = NULL;
7396
7397 return fields;
7398 }
7399
7400 /**
7401 * tep_event_common_fields - return a list of common fields for an event
7402 * @event: the event to return the common fields of.
7403 *
7404 * Returns an allocated array of fields. The last item in the array is NULL.
7405 * The array must be freed with free().
7406 */
tep_event_common_fields(struct tep_event * event)7407 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
7408 {
7409 return get_event_fields("common", event->name,
7410 event->format.nr_common,
7411 event->format.common_fields);
7412 }
7413
7414 /**
7415 * tep_event_fields - return a list of event specific fields for an event
7416 * @event: the event to return the fields of.
7417 *
7418 * Returns an allocated array of fields. The last item in the array is NULL.
7419 * The array must be freed with free().
7420 */
tep_event_fields(struct tep_event * event)7421 struct tep_format_field **tep_event_fields(struct tep_event *event)
7422 {
7423 return get_event_fields("event", event->name,
7424 event->format.nr_fields,
7425 event->format.fields);
7426 }
7427
print_fields(struct trace_seq * s,struct tep_print_flag_sym * field)7428 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
7429 {
7430 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
7431 if (field->next) {
7432 trace_seq_puts(s, ", ");
7433 print_fields(s, field->next);
7434 }
7435 }
7436
7437 /* for debugging */
print_args(struct tep_print_arg * args)7438 static void print_args(struct tep_print_arg *args)
7439 {
7440 int print_paren = 1;
7441 struct trace_seq s;
7442
7443 switch (args->type) {
7444 case TEP_PRINT_NULL:
7445 printf("null");
7446 break;
7447 case TEP_PRINT_ATOM:
7448 printf("%s", args->atom.atom);
7449 break;
7450 case TEP_PRINT_FIELD:
7451 printf("REC->%s", args->field.name);
7452 break;
7453 case TEP_PRINT_FLAGS:
7454 printf("__print_flags(");
7455 print_args(args->flags.field);
7456 printf(", %s, ", args->flags.delim);
7457 trace_seq_init(&s);
7458 print_fields(&s, args->flags.flags);
7459 trace_seq_do_printf(&s);
7460 trace_seq_destroy(&s);
7461 printf(")");
7462 break;
7463 case TEP_PRINT_SYMBOL:
7464 printf("__print_symbolic(");
7465 print_args(args->symbol.field);
7466 printf(", ");
7467 trace_seq_init(&s);
7468 print_fields(&s, args->symbol.symbols);
7469 trace_seq_do_printf(&s);
7470 trace_seq_destroy(&s);
7471 printf(")");
7472 break;
7473 case TEP_PRINT_HEX:
7474 printf("__print_hex(");
7475 print_args(args->hex.field);
7476 printf(", ");
7477 print_args(args->hex.size);
7478 printf(")");
7479 break;
7480 case TEP_PRINT_HEX_STR:
7481 printf("__print_hex_str(");
7482 print_args(args->hex.field);
7483 printf(", ");
7484 print_args(args->hex.size);
7485 printf(")");
7486 break;
7487 case TEP_PRINT_INT_ARRAY:
7488 printf("__print_array(");
7489 print_args(args->int_array.field);
7490 printf(", ");
7491 print_args(args->int_array.count);
7492 printf(", ");
7493 print_args(args->int_array.el_size);
7494 printf(")");
7495 break;
7496 case TEP_PRINT_STRING:
7497 case TEP_PRINT_BSTRING:
7498 printf("__get_str(%s)", args->string.string);
7499 break;
7500 case TEP_PRINT_BITMASK:
7501 printf("__get_bitmask(%s)", args->bitmask.bitmask);
7502 break;
7503 case TEP_PRINT_CPUMASK:
7504 printf("__get_cpumask(%s)", args->bitmask.bitmask);
7505 break;
7506 case TEP_PRINT_TYPE:
7507 printf("(%s)", args->typecast.type);
7508 print_args(args->typecast.item);
7509 break;
7510 case TEP_PRINT_OP:
7511 if (strcmp(args->op.op, ":") == 0)
7512 print_paren = 0;
7513 if (print_paren)
7514 printf("(");
7515 print_args(args->op.left);
7516 printf(" %s ", args->op.op);
7517 print_args(args->op.right);
7518 if (print_paren)
7519 printf(")");
7520 break;
7521 default:
7522 /* we should warn... */
7523 return;
7524 }
7525 if (args->next) {
7526 printf("\n");
7527 print_args(args->next);
7528 }
7529 }
7530
parse_header_field(struct tep_handle * tep,const char * field,int * offset,int * size,int mandatory)7531 static void parse_header_field(struct tep_handle *tep, const char *field,
7532 int *offset, int *size, int mandatory)
7533 {
7534 unsigned long long save_input_buf_ptr;
7535 unsigned long long save_input_buf_siz;
7536 char *token;
7537 int type;
7538
7539 save_input_buf_ptr = tep->input_buf_ptr;
7540 save_input_buf_siz = tep->input_buf_siz;
7541
7542 if (read_expected(tep, TEP_EVENT_ITEM, "field") < 0)
7543 return;
7544 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7545 return;
7546
7547 /* type */
7548 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7549 goto fail;
7550 free_token(token);
7551
7552 /*
7553 * If this is not a mandatory field, then test it first.
7554 */
7555 if (mandatory) {
7556 if (read_expected(tep, TEP_EVENT_ITEM, field) < 0)
7557 return;
7558 } else {
7559 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7560 goto fail;
7561 if (strcmp(token, field) != 0)
7562 goto discard;
7563 free_token(token);
7564 }
7565
7566 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7567 return;
7568 if (read_expected(tep, TEP_EVENT_ITEM, "offset") < 0)
7569 return;
7570 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7571 return;
7572 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7573 goto fail;
7574 *offset = atoi(token);
7575 free_token(token);
7576 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7577 return;
7578 if (read_expected(tep, TEP_EVENT_ITEM, "size") < 0)
7579 return;
7580 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7581 return;
7582 if (read_expect_type(tep, TEP_EVENT_ITEM, &token) < 0)
7583 goto fail;
7584 *size = atoi(token);
7585 free_token(token);
7586 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7587 return;
7588 type = read_token(tep, &token);
7589 if (type != TEP_EVENT_NEWLINE) {
7590 /* newer versions of the kernel have a "signed" type */
7591 if (type != TEP_EVENT_ITEM)
7592 goto fail;
7593
7594 if (strcmp(token, "signed") != 0)
7595 goto fail;
7596
7597 free_token(token);
7598
7599 if (read_expected(tep, TEP_EVENT_OP, ":") < 0)
7600 return;
7601
7602 if (read_expect_type(tep, TEP_EVENT_ITEM, &token))
7603 goto fail;
7604
7605 free_token(token);
7606 if (read_expected(tep, TEP_EVENT_OP, ";") < 0)
7607 return;
7608
7609 if (read_expect_type(tep, TEP_EVENT_NEWLINE, &token))
7610 goto fail;
7611 }
7612 fail:
7613 free_token(token);
7614 return;
7615
7616 discard:
7617 tep->input_buf_ptr = save_input_buf_ptr;
7618 tep->input_buf_siz = save_input_buf_siz;
7619 *offset = 0;
7620 *size = 0;
7621 free_token(token);
7622 }
7623
7624 /**
7625 * tep_parse_header_page - parse the data stored in the header page
7626 * @tep: a handle to the trace event parser context
7627 * @buf: the buffer storing the header page format string
7628 * @size: the size of @buf
7629 * @long_size: the long size to use if there is no header
7630 *
7631 * This parses the header page format for information on the
7632 * ring buffer used. The @buf should be copied from
7633 *
7634 * /sys/kernel/debug/tracing/events/header_page
7635 */
tep_parse_header_page(struct tep_handle * tep,char * buf,unsigned long size,int long_size)7636 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
7637 int long_size)
7638 {
7639 int ignore;
7640
7641 if (!size) {
7642 /*
7643 * Old kernels did not have header page info.
7644 * Sorry but we just use what we find here in user space.
7645 */
7646 tep->header_page_ts_size = sizeof(long long);
7647 tep->header_page_size_size = long_size;
7648 tep->header_page_data_offset = sizeof(long long) + long_size;
7649 tep->header_page_data_size = getpagesize() - tep->header_page_data_offset;
7650 tep->old_format = 1;
7651 return -1;
7652 }
7653 init_input_buf(tep, buf, size);
7654
7655 parse_header_field(tep, "timestamp", &tep->header_page_ts_offset,
7656 &tep->header_page_ts_size, 1);
7657 parse_header_field(tep, "commit", &tep->header_page_size_offset,
7658 &tep->header_page_size_size, 1);
7659 parse_header_field(tep, "overwrite", &tep->header_page_overwrite,
7660 &ignore, 0);
7661 parse_header_field(tep, "data", &tep->header_page_data_offset,
7662 &tep->header_page_data_size, 1);
7663
7664 return 0;
7665 }
7666
event_matches(struct tep_event * event,int id,const char * sys_name,const char * event_name)7667 static int event_matches(struct tep_event *event,
7668 int id, const char *sys_name,
7669 const char *event_name)
7670 {
7671 if (id >= 0 && id != event->id)
7672 return 0;
7673
7674 if (event_name && (strcmp(event_name, event->name) != 0))
7675 return 0;
7676
7677 if (sys_name && (strcmp(sys_name, event->system) != 0))
7678 return 0;
7679
7680 return 1;
7681 }
7682
free_handler(struct event_handler * handle)7683 static void free_handler(struct event_handler *handle)
7684 {
7685 free((void *)handle->sys_name);
7686 free((void *)handle->event_name);
7687 free(handle);
7688 }
7689
find_event_handle(struct tep_handle * tep,struct tep_event * event)7690 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
7691 {
7692 struct event_handler *handle, **next;
7693
7694 for (next = &tep->handlers; *next;
7695 next = &(*next)->next) {
7696 handle = *next;
7697 if (event_matches(event, handle->id,
7698 handle->sys_name,
7699 handle->event_name))
7700 break;
7701 }
7702
7703 if (!(*next))
7704 return 0;
7705
7706 tep_info("overriding event (%d) %s:%s with new print handler",
7707 event->id, event->system, event->name);
7708
7709 event->handler = handle->func;
7710 event->context = handle->context;
7711
7712 *next = handle->next;
7713 free_handler(handle);
7714
7715 return 1;
7716 }
7717
7718 /**
7719 * parse_format - parse the event format
7720 * @buf: the buffer storing the event format string
7721 * @size: the size of @buf
7722 * @sys: the system the event belongs to
7723 *
7724 * This parses the event format and creates an event structure
7725 * to quickly parse raw data for a given event.
7726 *
7727 * These files currently come from:
7728 *
7729 * /sys/kernel/debug/tracing/events/.../.../format
7730 */
parse_format(struct tep_event ** eventp,struct tep_handle * tep,const char * buf,unsigned long size,const char * sys)7731 static enum tep_errno parse_format(struct tep_event **eventp,
7732 struct tep_handle *tep, const char *buf,
7733 unsigned long size, const char *sys)
7734 {
7735 struct tep_event *event;
7736 int ret;
7737
7738 init_input_buf(tep, buf, size);
7739
7740 *eventp = event = alloc_event();
7741 if (!event)
7742 return TEP_ERRNO__MEM_ALLOC_FAILED;
7743
7744 event->name = event_read_name(tep);
7745 if (!event->name) {
7746 /* Bad event? */
7747 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7748 goto event_alloc_failed;
7749 }
7750
7751 if (strcmp(sys, "ftrace") == 0) {
7752 event->flags |= TEP_EVENT_FL_ISFTRACE;
7753
7754 if (strcmp(event->name, "bprint") == 0)
7755 event->flags |= TEP_EVENT_FL_ISBPRINT;
7756 }
7757
7758 event->id = event_read_id(tep);
7759 if (event->id < 0) {
7760 ret = TEP_ERRNO__READ_ID_FAILED;
7761 /*
7762 * This isn't an allocation error actually.
7763 * But as the ID is critical, just bail out.
7764 */
7765 goto event_alloc_failed;
7766 }
7767
7768 event->system = strdup(sys);
7769 if (!event->system) {
7770 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7771 goto event_alloc_failed;
7772 }
7773
7774 /* Add tep to event so that it can be referenced */
7775 event->tep = tep;
7776
7777 ret = event_read_format(event);
7778 if (ret < 0) {
7779 ret = TEP_ERRNO__READ_FORMAT_FAILED;
7780 goto event_parse_failed;
7781 }
7782
7783 /*
7784 * If the event has an override, don't print warnings if the event
7785 * print format fails to parse.
7786 */
7787 if (tep && find_event_handle(tep, event))
7788 show_warning = 0;
7789
7790 ret = event_read_print(event);
7791 show_warning = 1;
7792
7793 if (ret < 0) {
7794 ret = TEP_ERRNO__READ_PRINT_FAILED;
7795 goto event_parse_failed;
7796 }
7797
7798 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
7799 struct tep_format_field *field;
7800 struct tep_print_arg *arg, **list;
7801
7802 /* old ftrace had no args */
7803 list = &event->print_fmt.args;
7804 for (field = event->format.fields; field; field = field->next) {
7805 arg = alloc_arg();
7806 if (!arg) {
7807 event->flags |= TEP_EVENT_FL_FAILED;
7808 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
7809 }
7810 arg->type = TEP_PRINT_FIELD;
7811 arg->field.name = strdup(field->name);
7812 if (!arg->field.name) {
7813 event->flags |= TEP_EVENT_FL_FAILED;
7814 free_arg(arg);
7815 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
7816 }
7817 arg->field.field = field;
7818 *list = arg;
7819 list = &arg->next;
7820 }
7821 }
7822
7823 if (!(event->flags & TEP_EVENT_FL_ISBPRINT))
7824 event->print_fmt.print_cache = parse_args(event,
7825 event->print_fmt.format,
7826 event->print_fmt.args);
7827
7828 return 0;
7829
7830 event_parse_failed:
7831 event->flags |= TEP_EVENT_FL_FAILED;
7832 return ret;
7833
7834 event_alloc_failed:
7835 free(event->system);
7836 free(event->name);
7837 free(event);
7838 *eventp = NULL;
7839 return ret;
7840 }
7841
7842 static enum tep_errno
__parse_event(struct tep_handle * tep,struct tep_event ** eventp,const char * buf,unsigned long size,const char * sys)7843 __parse_event(struct tep_handle *tep,
7844 struct tep_event **eventp,
7845 const char *buf, unsigned long size,
7846 const char *sys)
7847 {
7848 int ret = parse_format(eventp, tep, buf, size, sys);
7849 struct tep_event *event = *eventp;
7850
7851 if (event == NULL)
7852 return ret;
7853
7854 if (tep && add_event(tep, event)) {
7855 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7856 goto event_add_failed;
7857 }
7858
7859 #define PRINT_ARGS 0
7860 if (PRINT_ARGS && event->print_fmt.args)
7861 print_args(event->print_fmt.args);
7862
7863 return 0;
7864
7865 event_add_failed:
7866 free_tep_event(event);
7867 return ret;
7868 }
7869
7870 /**
7871 * tep_parse_format - parse the event format
7872 * @tep: a handle to the trace event parser context
7873 * @eventp: returned format
7874 * @buf: the buffer storing the event format string
7875 * @size: the size of @buf
7876 * @sys: the system the event belongs to
7877 *
7878 * This parses the event format and creates an event structure
7879 * to quickly parse raw data for a given event.
7880 *
7881 * These files currently come from:
7882 *
7883 * /sys/kernel/debug/tracing/events/.../.../format
7884 */
tep_parse_format(struct tep_handle * tep,struct tep_event ** eventp,const char * buf,unsigned long size,const char * sys)7885 enum tep_errno tep_parse_format(struct tep_handle *tep,
7886 struct tep_event **eventp,
7887 const char *buf,
7888 unsigned long size, const char *sys)
7889 {
7890 return __parse_event(tep, eventp, buf, size, sys);
7891 }
7892
7893 /**
7894 * tep_parse_event - parse the event format
7895 * @tep: a handle to the trace event parser context
7896 * @buf: the buffer storing the event format string
7897 * @size: the size of @buf
7898 * @sys: the system the event belongs to
7899 *
7900 * This parses the event format and creates an event structure
7901 * to quickly parse raw data for a given event.
7902 *
7903 * These files currently come from:
7904 *
7905 * /sys/kernel/debug/tracing/events/.../.../format
7906 */
tep_parse_event(struct tep_handle * tep,const char * buf,unsigned long size,const char * sys)7907 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
7908 unsigned long size, const char *sys)
7909 {
7910 struct tep_event *event = NULL;
7911 return __parse_event(tep, &event, buf, size, sys);
7912 }
7913
get_field_val(struct trace_seq * s,struct tep_format_field * field,const char * name,struct tep_record * record,unsigned long long * val,int err)7914 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
7915 const char *name, struct tep_record *record,
7916 unsigned long long *val, int err)
7917 {
7918 if (!field) {
7919 if (err)
7920 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
7921 return -1;
7922 }
7923
7924 if (tep_read_number_field(field, record->data, val)) {
7925 if (err)
7926 trace_seq_printf(s, " %s=INVALID", name);
7927 return -1;
7928 }
7929
7930 return 0;
7931 }
7932
7933 /**
7934 * tep_get_field_raw - return the raw pointer into the data field
7935 * @s: The seq to print to on error
7936 * @event: the event that the field is for
7937 * @name: The name of the field
7938 * @record: The record with the field name.
7939 * @len: place to store the field length.
7940 * @err: print default error if failed.
7941 *
7942 * Returns a pointer into record->data of the field and places
7943 * the length of the field in @len.
7944 *
7945 * On failure, it returns NULL.
7946 */
tep_get_field_raw(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,int * len,int err)7947 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
7948 const char *name, struct tep_record *record,
7949 int *len, int err)
7950 {
7951 struct tep_format_field *field;
7952 void *data = record->data;
7953 unsigned offset;
7954 int dummy;
7955
7956 if (!event)
7957 return NULL;
7958
7959 field = tep_find_field(event, name);
7960
7961 if (!field) {
7962 if (err)
7963 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
7964 return NULL;
7965 }
7966
7967 /* Allow @len to be NULL */
7968 if (!len)
7969 len = &dummy;
7970
7971 offset = field->offset;
7972 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
7973 offset = tep_read_number(event->tep,
7974 data + offset, field->size);
7975 *len = offset >> 16;
7976 offset &= 0xffff;
7977 if (field->flags & TEP_FIELD_IS_RELATIVE)
7978 offset += field->offset + field->size;
7979 } else
7980 *len = field->size;
7981
7982 return data + offset;
7983 }
7984
7985 /**
7986 * tep_get_field_val - find a field and return its value
7987 * @s: The seq to print to on error
7988 * @event: the event that the field is for
7989 * @name: The name of the field
7990 * @record: The record with the field name.
7991 * @val: place to store the value of the field.
7992 * @err: print default error if failed.
7993 *
7994 * Returns 0 on success -1 on field not found.
7995 */
tep_get_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)7996 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
7997 const char *name, struct tep_record *record,
7998 unsigned long long *val, int err)
7999 {
8000 struct tep_format_field *field;
8001
8002 if (!event)
8003 return -1;
8004
8005 field = tep_find_field(event, name);
8006
8007 return get_field_val(s, field, name, record, val, err);
8008 }
8009
8010 /**
8011 * tep_get_common_field_val - find a common field and return its value
8012 * @s: The seq to print to on error
8013 * @event: the event that the field is for
8014 * @name: The name of the field
8015 * @record: The record with the field name.
8016 * @val: place to store the value of the field.
8017 * @err: print default error if failed.
8018 *
8019 * Returns 0 on success -1 on field not found.
8020 */
tep_get_common_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)8021 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
8022 const char *name, struct tep_record *record,
8023 unsigned long long *val, int err)
8024 {
8025 struct tep_format_field *field;
8026
8027 if (!event)
8028 return -1;
8029
8030 field = tep_find_common_field(event, name);
8031
8032 return get_field_val(s, field, name, record, val, err);
8033 }
8034
8035 /**
8036 * tep_get_any_field_val - find a any field and return its value
8037 * @s: The seq to print to on error
8038 * @event: the event that the field is for
8039 * @name: The name of the field
8040 * @record: The record with the field name.
8041 * @val: place to store the value of the field.
8042 * @err: print default error if failed.
8043 *
8044 * Returns 0 on success -1 on field not found.
8045 */
tep_get_any_field_val(struct trace_seq * s,struct tep_event * event,const char * name,struct tep_record * record,unsigned long long * val,int err)8046 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
8047 const char *name, struct tep_record *record,
8048 unsigned long long *val, int err)
8049 {
8050 struct tep_format_field *field;
8051
8052 if (!event)
8053 return -1;
8054
8055 field = tep_find_any_field(event, name);
8056
8057 return get_field_val(s, field, name, record, val, err);
8058 }
8059
8060 /**
8061 * tep_print_num_field - print a field and a format
8062 * @s: The seq to print to
8063 * @fmt: The printf format to print the field with.
8064 * @event: the event that the field is for
8065 * @name: The name of the field
8066 * @record: The record with the field name.
8067 * @err: print default error if failed.
8068 *
8069 * Returns positive value on success, negative in case of an error,
8070 * or 0 if buffer is full.
8071 */
tep_print_num_field(struct trace_seq * s,const char * fmt,struct tep_event * event,const char * name,struct tep_record * record,int err)8072 int tep_print_num_field(struct trace_seq *s, const char *fmt,
8073 struct tep_event *event, const char *name,
8074 struct tep_record *record, int err)
8075 {
8076 struct tep_format_field *field = tep_find_field(event, name);
8077 unsigned long long val;
8078
8079 if (!field)
8080 goto failed;
8081
8082 if (tep_read_number_field(field, record->data, &val))
8083 goto failed;
8084
8085 return trace_seq_printf(s, fmt, val);
8086
8087 failed:
8088 if (err)
8089 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
8090 return -1;
8091 }
8092
8093 /**
8094 * tep_print_func_field - print a field and a format for function pointers
8095 * @s: The seq to print to
8096 * @fmt: The printf format to print the field with.
8097 * @event: the event that the field is for
8098 * @name: The name of the field
8099 * @record: The record with the field name.
8100 * @err: print default error if failed.
8101 *
8102 * Returns positive value on success, negative in case of an error,
8103 * or 0 if buffer is full.
8104 */
tep_print_func_field(struct trace_seq * s,const char * fmt,struct tep_event * event,const char * name,struct tep_record * record,int err)8105 int tep_print_func_field(struct trace_seq *s, const char *fmt,
8106 struct tep_event *event, const char *name,
8107 struct tep_record *record, int err)
8108 {
8109 struct tep_format_field *field = tep_find_field(event, name);
8110 struct tep_handle *tep = event->tep;
8111 unsigned long long val;
8112 struct func_map *func;
8113 char tmp[128];
8114
8115 if (!field)
8116 goto failed;
8117
8118 if (tep_read_number_field(field, record->data, &val))
8119 goto failed;
8120
8121 func = find_func(tep, val);
8122
8123 if (func)
8124 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
8125 else
8126 sprintf(tmp, "0x%08llx", val);
8127
8128 return trace_seq_printf(s, fmt, tmp);
8129
8130 failed:
8131 if (err)
8132 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
8133 return -1;
8134 }
8135
free_func_handle(struct tep_function_handler * func)8136 static void free_func_handle(struct tep_function_handler *func)
8137 {
8138 struct func_params *params;
8139
8140 free(func->name);
8141
8142 while (func->params) {
8143 params = func->params;
8144 func->params = params->next;
8145 free(params);
8146 }
8147
8148 free(func);
8149 }
8150
8151 /**
8152 * tep_register_print_function - register a helper function
8153 * @tep: a handle to the trace event parser context
8154 * @func: the function to process the helper function
8155 * @ret_type: the return type of the helper function
8156 * @name: the name of the helper function
8157 * @parameters: A list of enum tep_func_arg_type
8158 *
8159 * Some events may have helper functions in the print format arguments.
8160 * This allows a plugin to dynamically create a way to process one
8161 * of these functions.
8162 *
8163 * The @parameters is a variable list of tep_func_arg_type enums that
8164 * must end with TEP_FUNC_ARG_VOID.
8165 */
tep_register_print_function(struct tep_handle * tep,tep_func_handler func,enum tep_func_arg_type ret_type,char * name,...)8166 int tep_register_print_function(struct tep_handle *tep,
8167 tep_func_handler func,
8168 enum tep_func_arg_type ret_type,
8169 char *name, ...)
8170 {
8171 struct tep_function_handler *func_handle;
8172 struct func_params **next_param;
8173 struct func_params *param;
8174 enum tep_func_arg_type type;
8175 va_list ap;
8176 int ret;
8177
8178 func_handle = find_func_handler(tep, name);
8179 if (func_handle) {
8180 /*
8181 * This is most like caused by the users own
8182 * plugins updating the function. This overrides the
8183 * system defaults.
8184 */
8185 tep_info("override of function helper '%s'", name);
8186 remove_func_handler(tep, name);
8187 }
8188
8189 func_handle = calloc(1, sizeof(*func_handle));
8190 if (!func_handle) {
8191 do_warning("Failed to allocate function handler");
8192 return TEP_ERRNO__MEM_ALLOC_FAILED;
8193 }
8194
8195 func_handle->ret_type = ret_type;
8196 func_handle->name = strdup(name);
8197 func_handle->func = func;
8198 if (!func_handle->name) {
8199 do_warning("Failed to allocate function name");
8200 free(func_handle);
8201 return TEP_ERRNO__MEM_ALLOC_FAILED;
8202 }
8203
8204 next_param = &(func_handle->params);
8205 va_start(ap, name);
8206 for (;;) {
8207 type = va_arg(ap, enum tep_func_arg_type);
8208 if (type == TEP_FUNC_ARG_VOID)
8209 break;
8210
8211 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
8212 do_warning("Invalid argument type %d", type);
8213 ret = TEP_ERRNO__INVALID_ARG_TYPE;
8214 goto out_free;
8215 }
8216
8217 param = malloc(sizeof(*param));
8218 if (!param) {
8219 do_warning("Failed to allocate function param");
8220 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
8221 goto out_free;
8222 }
8223 param->type = type;
8224 param->next = NULL;
8225
8226 *next_param = param;
8227 next_param = &(param->next);
8228
8229 func_handle->nr_args++;
8230 }
8231 va_end(ap);
8232
8233 func_handle->next = tep->func_handlers;
8234 tep->func_handlers = func_handle;
8235
8236 return 0;
8237 out_free:
8238 va_end(ap);
8239 free_func_handle(func_handle);
8240 return ret;
8241 }
8242
8243 /**
8244 * tep_unregister_print_function - unregister a helper function
8245 * @tep: a handle to the trace event parser context
8246 * @func: the function to process the helper function
8247 * @name: the name of the helper function
8248 *
8249 * This function removes existing print handler for function @name.
8250 *
8251 * Returns 0 if the handler was removed successully, -1 otherwise.
8252 */
tep_unregister_print_function(struct tep_handle * tep,tep_func_handler func,char * name)8253 int tep_unregister_print_function(struct tep_handle *tep,
8254 tep_func_handler func, char *name)
8255 {
8256 struct tep_function_handler *func_handle;
8257
8258 func_handle = find_func_handler(tep, name);
8259 if (func_handle && func_handle->func == func) {
8260 remove_func_handler(tep, name);
8261 return 0;
8262 }
8263 return -1;
8264 }
8265
search_event(struct tep_handle * tep,int id,const char * sys_name,const char * event_name)8266 static struct tep_event *search_event(struct tep_handle *tep, int id,
8267 const char *sys_name,
8268 const char *event_name)
8269 {
8270 struct tep_event *event;
8271
8272 if (id >= 0) {
8273 /* search by id */
8274 event = tep_find_event(tep, id);
8275 if (!event)
8276 return NULL;
8277 if (event_name && (strcmp(event_name, event->name) != 0))
8278 return NULL;
8279 if (sys_name && (strcmp(sys_name, event->system) != 0))
8280 return NULL;
8281 } else {
8282 event = tep_find_event_by_name(tep, sys_name, event_name);
8283 if (!event)
8284 return NULL;
8285 }
8286 return event;
8287 }
8288
8289 /**
8290 * tep_register_event_handler - register a way to parse an event
8291 * @tep: a handle to the trace event parser context
8292 * @id: the id of the event to register
8293 * @sys_name: the system name the event belongs to
8294 * @event_name: the name of the event
8295 * @func: the function to call to parse the event information
8296 * @context: the data to be passed to @func
8297 *
8298 * This function allows a developer to override the parsing of
8299 * a given event. If for some reason the default print format
8300 * is not sufficient, this function will register a function
8301 * for an event to be used to parse the data instead.
8302 *
8303 * If @id is >= 0, then it is used to find the event.
8304 * else @sys_name and @event_name are used.
8305 *
8306 * Returns:
8307 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
8308 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
8309 * negative TEP_ERRNO_... in case of an error
8310 *
8311 */
tep_register_event_handler(struct tep_handle * tep,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8312 int tep_register_event_handler(struct tep_handle *tep, int id,
8313 const char *sys_name, const char *event_name,
8314 tep_event_handler_func func, void *context)
8315 {
8316 struct tep_event *event;
8317 struct event_handler *handle;
8318
8319 event = search_event(tep, id, sys_name, event_name);
8320 if (event == NULL)
8321 goto not_found;
8322
8323 tep_info("overriding event (%d) %s:%s with new print handler",
8324 event->id, event->system, event->name);
8325
8326 event->handler = func;
8327 event->context = context;
8328 return TEP_REGISTER_SUCCESS_OVERWRITE;
8329
8330 not_found:
8331 /* Save for later use. */
8332 handle = calloc(1, sizeof(*handle));
8333 if (!handle) {
8334 do_warning("Failed to allocate event handler");
8335 return TEP_ERRNO__MEM_ALLOC_FAILED;
8336 }
8337
8338 handle->id = id;
8339 if (event_name)
8340 handle->event_name = strdup(event_name);
8341 if (sys_name)
8342 handle->sys_name = strdup(sys_name);
8343
8344 if ((event_name && !handle->event_name) ||
8345 (sys_name && !handle->sys_name)) {
8346 do_warning("Failed to allocate event/sys name");
8347 free((void *)handle->event_name);
8348 free((void *)handle->sys_name);
8349 free(handle);
8350 return TEP_ERRNO__MEM_ALLOC_FAILED;
8351 }
8352
8353 handle->func = func;
8354 handle->next = tep->handlers;
8355 tep->handlers = handle;
8356 handle->context = context;
8357
8358 return TEP_REGISTER_SUCCESS;
8359 }
8360
handle_matches(struct event_handler * handler,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8361 static int handle_matches(struct event_handler *handler, int id,
8362 const char *sys_name, const char *event_name,
8363 tep_event_handler_func func, void *context)
8364 {
8365 if (id >= 0 && id != handler->id)
8366 return 0;
8367
8368 if (event_name && (strcmp(event_name, handler->event_name) != 0))
8369 return 0;
8370
8371 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
8372 return 0;
8373
8374 if (func != handler->func || context != handler->context)
8375 return 0;
8376
8377 return 1;
8378 }
8379
8380 /**
8381 * tep_unregister_event_handler - unregister an existing event handler
8382 * @tep: a handle to the trace event parser context
8383 * @id: the id of the event to unregister
8384 * @sys_name: the system name the handler belongs to
8385 * @event_name: the name of the event handler
8386 * @func: the function to call to parse the event information
8387 * @context: the data to be passed to @func
8388 *
8389 * This function removes existing event handler (parser).
8390 *
8391 * If @id is >= 0, then it is used to find the event.
8392 * else @sys_name and @event_name are used.
8393 *
8394 * Returns 0 if handler was removed successfully, -1 if event was not found.
8395 */
tep_unregister_event_handler(struct tep_handle * tep,int id,const char * sys_name,const char * event_name,tep_event_handler_func func,void * context)8396 int tep_unregister_event_handler(struct tep_handle *tep, int id,
8397 const char *sys_name, const char *event_name,
8398 tep_event_handler_func func, void *context)
8399 {
8400 struct tep_event *event;
8401 struct event_handler *handle;
8402 struct event_handler **next;
8403
8404 event = search_event(tep, id, sys_name, event_name);
8405 if (event == NULL)
8406 goto not_found;
8407
8408 if (event->handler == func && event->context == context) {
8409 tep_info("removing override handler for event (%d) %s:%s. Going back to default handler.",
8410 event->id, event->system, event->name);
8411
8412 event->handler = NULL;
8413 event->context = NULL;
8414 return 0;
8415 }
8416
8417 not_found:
8418 for (next = &tep->handlers; *next; next = &(*next)->next) {
8419 handle = *next;
8420 if (handle_matches(handle, id, sys_name, event_name,
8421 func, context))
8422 break;
8423 }
8424
8425 if (!(*next))
8426 return -1;
8427
8428 *next = handle->next;
8429 free_handler(handle);
8430
8431 return 0;
8432 }
8433
8434 /**
8435 * tep_alloc - create a tep handle
8436 */
tep_alloc(void)8437 struct tep_handle *tep_alloc(void)
8438 {
8439 struct tep_handle *tep = calloc(1, sizeof(*tep));
8440
8441 if (tep) {
8442 tep->ref_count = 1;
8443 tep->host_bigendian = tep_is_bigendian();
8444 }
8445
8446 return tep;
8447 }
8448
tep_ref(struct tep_handle * tep)8449 void tep_ref(struct tep_handle *tep)
8450 {
8451 tep->ref_count++;
8452 }
8453
tep_get_ref(struct tep_handle * tep)8454 int tep_get_ref(struct tep_handle *tep)
8455 {
8456 if (tep)
8457 return tep->ref_count;
8458 return 0;
8459 }
8460
free_tep_format_field(struct tep_format_field * field)8461 __hidden void free_tep_format_field(struct tep_format_field *field)
8462 {
8463 free(field->type);
8464 if (field->alias != field->name)
8465 free(field->alias);
8466 free(field->name);
8467 free(field);
8468 }
8469
free_format_fields(struct tep_format_field * field)8470 static void free_format_fields(struct tep_format_field *field)
8471 {
8472 struct tep_format_field *next;
8473
8474 while (field) {
8475 next = field->next;
8476 free_tep_format_field(field);
8477 field = next;
8478 }
8479 }
8480
free_formats(struct tep_format * format)8481 static void free_formats(struct tep_format *format)
8482 {
8483 free_format_fields(format->common_fields);
8484 free_format_fields(format->fields);
8485 }
8486
free_tep_event(struct tep_event * event)8487 __hidden void free_tep_event(struct tep_event *event)
8488 {
8489 free(event->name);
8490 free(event->system);
8491
8492 free_formats(&event->format);
8493
8494 free(event->print_fmt.format);
8495 free_args(event->print_fmt.args);
8496 free_parse_args(event->print_fmt.print_cache);
8497 free(event);
8498 }
8499
8500 /**
8501 * tep_free - free a tep handle
8502 * @tep: the tep handle to free
8503 */
tep_free(struct tep_handle * tep)8504 void tep_free(struct tep_handle *tep)
8505 {
8506 struct cmdline_list *cmdlist, *cmdnext;
8507 struct func_list *funclist, *funcnext;
8508 struct printk_list *printklist, *printknext;
8509 struct tep_function_handler *func_handler;
8510 struct event_handler *handle;
8511 int i;
8512
8513 if (!tep)
8514 return;
8515
8516 cmdlist = tep->cmdlist;
8517 funclist = tep->funclist;
8518 printklist = tep->printklist;
8519
8520 tep->ref_count--;
8521 if (tep->ref_count)
8522 return;
8523
8524 if (tep->cmdlines) {
8525 for (i = 0; i < tep->cmdline_count; i++)
8526 free(tep->cmdlines[i].comm);
8527 free(tep->cmdlines);
8528 }
8529
8530 while (cmdlist) {
8531 cmdnext = cmdlist->next;
8532 free(cmdlist->comm);
8533 free(cmdlist);
8534 cmdlist = cmdnext;
8535 }
8536
8537 if (tep->func_map) {
8538 for (i = 0; i < (int)tep->func_count; i++) {
8539 free(tep->func_map[i].func);
8540 free(tep->func_map[i].mod);
8541 }
8542 free(tep->func_map);
8543 }
8544
8545 while (funclist) {
8546 funcnext = funclist->next;
8547 free(funclist->func);
8548 free(funclist->mod);
8549 free(funclist);
8550 funclist = funcnext;
8551 }
8552
8553 while (tep->func_handlers) {
8554 func_handler = tep->func_handlers;
8555 tep->func_handlers = func_handler->next;
8556 free_func_handle(func_handler);
8557 }
8558
8559 if (tep->printk_map) {
8560 for (i = 0; i < (int)tep->printk_count; i++)
8561 free(tep->printk_map[i].printk);
8562 free(tep->printk_map);
8563 }
8564
8565 while (printklist) {
8566 printknext = printklist->next;
8567 free(printklist->printk);
8568 free(printklist);
8569 printklist = printknext;
8570 }
8571
8572 for (i = 0; i < tep->nr_events; i++)
8573 free_tep_event(tep->events[i]);
8574
8575 while (tep->handlers) {
8576 handle = tep->handlers;
8577 tep->handlers = handle->next;
8578 free_handler(handle);
8579 }
8580
8581 free(tep->events);
8582 free(tep->sort_events);
8583 free(tep->func_resolver);
8584 free_tep_plugin_paths(tep);
8585
8586 free(tep);
8587 }
8588
tep_unref(struct tep_handle * tep)8589 void tep_unref(struct tep_handle *tep)
8590 {
8591 tep_free(tep);
8592 }
8593