blob: 3a7bd175f73c3716094217cac20bced98d52dcf6 [file] [log] [blame]
Steven Rostedtf7d82352012-04-06 00:47:53 +02001/*
2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
Jon Stanley7b9f6b42012-09-07 16:32:46 -040016 * License along with this program; if not, see <http://www.gnu.org/licenses>
Steven Rostedtf7d82352012-04-06 00:47:53 +020017 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 *
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
25 */
Steven Rostedtf7d82352012-04-06 00:47:53 +020026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdarg.h>
30#include <ctype.h>
31#include <errno.h>
Robert Richter0cf26012012-08-07 19:43:14 +020032#include <stdint.h>
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -030033#include <limits.h>
Arnaldo Carvalho de Meloc3cec9e2016-07-08 15:21:37 -030034#include <linux/string.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020035
David Ahern3d199b52014-12-18 19:11:11 -070036#include <netinet/ip6.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020037#include "event-parse.h"
Steven Rostedt668fe012012-04-06 00:47:55 +020038#include "event-utils.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020039
40static const char *input_buf;
41static unsigned long long input_buf_ptr;
42static unsigned long long input_buf_siz;
43
Tom Zanussi5205aec2012-04-06 00:47:58 +020044static int is_flag_field;
45static int is_symbolic_field;
46
Steven Rostedtf7d82352012-04-06 00:47:53 +020047static int show_warning = 1;
48
49#define do_warning(fmt, ...) \
50 do { \
51 if (show_warning) \
52 warning(fmt, ##__VA_ARGS__); \
53 } while (0)
54
Namhyung Kim3388cc32014-03-19 10:22:53 +090055#define do_warning_event(event, fmt, ...) \
56 do { \
57 if (!show_warning) \
58 continue; \
59 \
60 if (event) \
61 warning("[%s:%s] " fmt, event->system, \
62 event->name, ##__VA_ARGS__); \
63 else \
64 warning(fmt, ##__VA_ARGS__); \
65 } while (0)
66
Steven Rostedtf7d82352012-04-06 00:47:53 +020067static void init_input_buf(const char *buf, unsigned long long size)
68{
69 input_buf = buf;
70 input_buf_siz = size;
71 input_buf_ptr = 0;
72}
73
74const char *pevent_get_input_buf(void)
75{
76 return input_buf;
77}
78
79unsigned long long pevent_get_input_buf_ptr(void)
80{
81 return input_buf_ptr;
82}
83
84struct event_handler {
85 struct event_handler *next;
86 int id;
87 const char *sys_name;
88 const char *event_name;
89 pevent_event_handler_func func;
90 void *context;
91};
92
93struct pevent_func_params {
94 struct pevent_func_params *next;
95 enum pevent_func_arg_type type;
96};
97
98struct pevent_function_handler {
99 struct pevent_function_handler *next;
100 enum pevent_func_arg_type ret_type;
101 char *name;
102 pevent_func_handler func;
103 struct pevent_func_params *params;
104 int nr_args;
105};
106
107static unsigned long long
108process_defined_func(struct trace_seq *s, void *data, int size,
109 struct event_format *event, struct print_arg *arg);
110
111static void free_func_handle(struct pevent_function_handler *func);
112
113/**
114 * pevent_buffer_init - init buffer for parsing
115 * @buf: buffer to parse
116 * @size: the size of the buffer
117 *
118 * For use with pevent_read_token(), this initializes the internal
119 * buffer that pevent_read_token() will parse.
120 */
121void pevent_buffer_init(const char *buf, unsigned long long size)
122{
123 init_input_buf(buf, size);
124}
125
126void breakpoint(void)
127{
128 static int x;
129 x++;
130}
131
132struct print_arg *alloc_arg(void)
133{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300134 return calloc(1, sizeof(struct print_arg));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200135}
136
137struct cmdline {
138 char *comm;
139 int pid;
140};
141
142static int cmdline_cmp(const void *a, const void *b)
143{
144 const struct cmdline *ca = a;
145 const struct cmdline *cb = b;
146
147 if (ca->pid < cb->pid)
148 return -1;
149 if (ca->pid > cb->pid)
150 return 1;
151
152 return 0;
153}
154
155struct cmdline_list {
156 struct cmdline_list *next;
157 char *comm;
158 int pid;
159};
160
161static int cmdline_init(struct pevent *pevent)
162{
163 struct cmdline_list *cmdlist = pevent->cmdlist;
164 struct cmdline_list *item;
165 struct cmdline *cmdlines;
166 int i;
167
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300168 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
169 if (!cmdlines)
170 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200171
172 i = 0;
173 while (cmdlist) {
174 cmdlines[i].pid = cmdlist->pid;
175 cmdlines[i].comm = cmdlist->comm;
176 i++;
177 item = cmdlist;
178 cmdlist = cmdlist->next;
179 free(item);
180 }
181
182 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
183
184 pevent->cmdlines = cmdlines;
185 pevent->cmdlist = NULL;
186
187 return 0;
188}
189
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -0300190static const char *find_cmdline(struct pevent *pevent, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200191{
192 const struct cmdline *comm;
193 struct cmdline key;
194
195 if (!pid)
196 return "<idle>";
197
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300198 if (!pevent->cmdlines && cmdline_init(pevent))
199 return "<not enough memory for cmdlines!>";
Steven Rostedtf7d82352012-04-06 00:47:53 +0200200
201 key.pid = pid;
202
203 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
204 sizeof(*pevent->cmdlines), cmdline_cmp);
205
206 if (comm)
207 return comm->comm;
208 return "<...>";
209}
210
211/**
212 * pevent_pid_is_registered - return if a pid has a cmdline registered
213 * @pevent: handle for the pevent
214 * @pid: The pid to check if it has a cmdline registered with.
215 *
216 * Returns 1 if the pid has a cmdline mapped to it
217 * 0 otherwise.
218 */
219int pevent_pid_is_registered(struct pevent *pevent, int pid)
220{
221 const struct cmdline *comm;
222 struct cmdline key;
223
224 if (!pid)
225 return 1;
226
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300227 if (!pevent->cmdlines && cmdline_init(pevent))
228 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200229
230 key.pid = pid;
231
232 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
233 sizeof(*pevent->cmdlines), cmdline_cmp);
234
235 if (comm)
236 return 1;
237 return 0;
238}
239
240/*
241 * If the command lines have been converted to an array, then
242 * we must add this pid. This is much slower than when cmdlines
243 * are added before the array is initialized.
244 */
245static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
246{
247 struct cmdline *cmdlines = pevent->cmdlines;
248 const struct cmdline *cmdline;
249 struct cmdline key;
250
251 if (!pid)
252 return 0;
253
254 /* avoid duplicates */
255 key.pid = pid;
256
257 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
258 sizeof(*pevent->cmdlines), cmdline_cmp);
259 if (cmdline) {
260 errno = EEXIST;
261 return -1;
262 }
263
264 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
265 if (!cmdlines) {
266 errno = ENOMEM;
267 return -1;
268 }
269
Steven Rostedtf7d82352012-04-06 00:47:53 +0200270 cmdlines[pevent->cmdline_count].comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300271 if (!cmdlines[pevent->cmdline_count].comm) {
272 free(cmdlines);
273 errno = ENOMEM;
274 return -1;
275 }
276
277 cmdlines[pevent->cmdline_count].pid = pid;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200278
279 if (cmdlines[pevent->cmdline_count].comm)
280 pevent->cmdline_count++;
281
282 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
283 pevent->cmdlines = cmdlines;
284
285 return 0;
286}
287
288/**
289 * pevent_register_comm - register a pid / comm mapping
290 * @pevent: handle for the pevent
291 * @comm: the command line to register
292 * @pid: the pid to map the command line to
293 *
294 * This adds a mapping to search for command line names with
295 * a given pid. The comm is duplicated.
296 */
297int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
298{
299 struct cmdline_list *item;
300
301 if (pevent->cmdlines)
302 return add_new_comm(pevent, comm, pid);
303
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300304 item = malloc(sizeof(*item));
305 if (!item)
306 return -1;
307
Josef Bacikdeab6f52015-03-24 09:57:49 -0400308 if (comm)
309 item->comm = strdup(comm);
310 else
311 item->comm = strdup("<...>");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300312 if (!item->comm) {
313 free(item);
314 return -1;
315 }
Steven Rostedtf7d82352012-04-06 00:47:53 +0200316 item->pid = pid;
317 item->next = pevent->cmdlist;
318
319 pevent->cmdlist = item;
320 pevent->cmdline_count++;
321
322 return 0;
323}
324
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -0400325int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400326{
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -0400327 pevent->trace_clock = strdup(trace_clock);
328 if (!pevent->trace_clock) {
329 errno = ENOMEM;
330 return -1;
331 }
332 return 0;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400333}
334
Steven Rostedtf7d82352012-04-06 00:47:53 +0200335struct func_map {
336 unsigned long long addr;
337 char *func;
338 char *mod;
339};
340
341struct func_list {
342 struct func_list *next;
343 unsigned long long addr;
344 char *func;
345 char *mod;
346};
347
348static int func_cmp(const void *a, const void *b)
349{
350 const struct func_map *fa = a;
351 const struct func_map *fb = b;
352
353 if (fa->addr < fb->addr)
354 return -1;
355 if (fa->addr > fb->addr)
356 return 1;
357
358 return 0;
359}
360
361/*
362 * We are searching for a record in between, not an exact
363 * match.
364 */
365static int func_bcmp(const void *a, const void *b)
366{
367 const struct func_map *fa = a;
368 const struct func_map *fb = b;
369
370 if ((fa->addr == fb->addr) ||
371
372 (fa->addr > fb->addr &&
373 fa->addr < (fb+1)->addr))
374 return 0;
375
376 if (fa->addr < fb->addr)
377 return -1;
378
379 return 1;
380}
381
382static int func_map_init(struct pevent *pevent)
383{
384 struct func_list *funclist;
385 struct func_list *item;
386 struct func_map *func_map;
387 int i;
388
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300389 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
390 if (!func_map)
391 return -1;
392
Steven Rostedtf7d82352012-04-06 00:47:53 +0200393 funclist = pevent->funclist;
394
395 i = 0;
396 while (funclist) {
397 func_map[i].func = funclist->func;
398 func_map[i].addr = funclist->addr;
399 func_map[i].mod = funclist->mod;
400 i++;
401 item = funclist;
402 funclist = funclist->next;
403 free(item);
404 }
405
406 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
407
408 /*
409 * Add a special record at the end.
410 */
411 func_map[pevent->func_count].func = NULL;
412 func_map[pevent->func_count].addr = 0;
413 func_map[pevent->func_count].mod = NULL;
414
415 pevent->func_map = func_map;
416 pevent->funclist = NULL;
417
418 return 0;
419}
420
421static struct func_map *
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300422__find_func(struct pevent *pevent, unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200423{
424 struct func_map *func;
425 struct func_map key;
426
427 if (!pevent->func_map)
428 func_map_init(pevent);
429
430 key.addr = addr;
431
432 func = bsearch(&key, pevent->func_map, pevent->func_count,
433 sizeof(*pevent->func_map), func_bcmp);
434
435 return func;
436}
437
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300438struct func_resolver {
439 pevent_func_resolver_t *func;
440 void *priv;
441 struct func_map map;
442};
443
444/**
445 * pevent_set_function_resolver - set an alternative function resolver
446 * @pevent: handle for the pevent
447 * @resolver: function to be used
448 * @priv: resolver function private state.
449 *
450 * Some tools may have already a way to resolve kernel functions, allow them to
451 * keep using it instead of duplicating all the entries inside
452 * pevent->funclist.
453 */
454int pevent_set_function_resolver(struct pevent *pevent,
455 pevent_func_resolver_t *func, void *priv)
456{
457 struct func_resolver *resolver = malloc(sizeof(*resolver));
458
459 if (resolver == NULL)
460 return -1;
461
462 resolver->func = func;
463 resolver->priv = priv;
464
465 free(pevent->func_resolver);
466 pevent->func_resolver = resolver;
467
468 return 0;
469}
470
471/**
472 * pevent_reset_function_resolver - reset alternative function resolver
473 * @pevent: handle for the pevent
474 *
475 * Stop using whatever alternative resolver was set, use the default
476 * one instead.
477 */
478void pevent_reset_function_resolver(struct pevent *pevent)
479{
480 free(pevent->func_resolver);
481 pevent->func_resolver = NULL;
482}
483
484static struct func_map *
485find_func(struct pevent *pevent, unsigned long long addr)
486{
487 struct func_map *map;
488
489 if (!pevent->func_resolver)
490 return __find_func(pevent, addr);
491
492 map = &pevent->func_resolver->map;
493 map->mod = NULL;
494 map->addr = addr;
495 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
496 &map->addr, &map->mod);
497 if (map->func == NULL)
498 return NULL;
499
500 return map;
501}
502
Steven Rostedtf7d82352012-04-06 00:47:53 +0200503/**
504 * pevent_find_function - find a function by a given address
505 * @pevent: handle for the pevent
506 * @addr: the address to find the function with
507 *
508 * Returns a pointer to the function stored that has the given
509 * address. Note, the address does not have to be exact, it
510 * will select the function that would contain the address.
511 */
512const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
513{
514 struct func_map *map;
515
516 map = find_func(pevent, addr);
517 if (!map)
518 return NULL;
519
520 return map->func;
521}
522
523/**
524 * pevent_find_function_address - find a function address by a given address
525 * @pevent: handle for the pevent
526 * @addr: the address to find the function with
527 *
528 * Returns the address the function starts at. This can be used in
529 * conjunction with pevent_find_function to print both the function
530 * name and the function offset.
531 */
532unsigned long long
533pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
534{
535 struct func_map *map;
536
537 map = find_func(pevent, addr);
538 if (!map)
539 return 0;
540
541 return map->addr;
542}
543
544/**
545 * pevent_register_function - register a function with a given address
546 * @pevent: handle for the pevent
547 * @function: the function name to register
548 * @addr: the address the function starts at
549 * @mod: the kernel module the function may be in (NULL for none)
550 *
551 * This registers a function name with an address and module.
552 * The @func passed in is duplicated.
553 */
554int pevent_register_function(struct pevent *pevent, char *func,
555 unsigned long long addr, char *mod)
556{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300557 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200558
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300559 if (!item)
560 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200561
562 item->next = pevent->funclist;
563 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300564 if (!item->func)
565 goto out_free;
566
567 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200568 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300569 if (!item->mod)
570 goto out_free_func;
571 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200572 item->mod = NULL;
573 item->addr = addr;
574
Namhyung Kimca638582012-04-09 11:54:31 +0900575 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200576 pevent->func_count++;
577
578 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300579
580out_free_func:
581 free(item->func);
582 item->func = NULL;
583out_free:
584 free(item);
585 errno = ENOMEM;
586 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200587}
588
589/**
590 * pevent_print_funcs - print out the stored functions
591 * @pevent: handle for the pevent
592 *
593 * This prints out the stored functions.
594 */
595void pevent_print_funcs(struct pevent *pevent)
596{
597 int i;
598
599 if (!pevent->func_map)
600 func_map_init(pevent);
601
602 for (i = 0; i < (int)pevent->func_count; i++) {
603 printf("%016llx %s",
604 pevent->func_map[i].addr,
605 pevent->func_map[i].func);
606 if (pevent->func_map[i].mod)
607 printf(" [%s]\n", pevent->func_map[i].mod);
608 else
609 printf("\n");
610 }
611}
612
613struct printk_map {
614 unsigned long long addr;
615 char *printk;
616};
617
618struct printk_list {
619 struct printk_list *next;
620 unsigned long long addr;
621 char *printk;
622};
623
624static int printk_cmp(const void *a, const void *b)
625{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900626 const struct printk_map *pa = a;
627 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200628
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900629 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200630 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900631 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200632 return 1;
633
634 return 0;
635}
636
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300637static int printk_map_init(struct pevent *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200638{
639 struct printk_list *printklist;
640 struct printk_list *item;
641 struct printk_map *printk_map;
642 int i;
643
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300644 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
645 if (!printk_map)
646 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200647
648 printklist = pevent->printklist;
649
650 i = 0;
651 while (printklist) {
652 printk_map[i].printk = printklist->printk;
653 printk_map[i].addr = printklist->addr;
654 i++;
655 item = printklist;
656 printklist = printklist->next;
657 free(item);
658 }
659
660 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
661
662 pevent->printk_map = printk_map;
663 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300664
665 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200666}
667
668static struct printk_map *
669find_printk(struct pevent *pevent, unsigned long long addr)
670{
671 struct printk_map *printk;
672 struct printk_map key;
673
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300674 if (!pevent->printk_map && printk_map_init(pevent))
675 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200676
677 key.addr = addr;
678
679 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
680 sizeof(*pevent->printk_map), printk_cmp);
681
682 return printk;
683}
684
685/**
686 * pevent_register_print_string - register a string by its address
687 * @pevent: handle for the pevent
688 * @fmt: the string format to register
689 * @addr: the address the string was located at
690 *
691 * This registers a string by the address it was stored in the kernel.
692 * The @fmt passed in is duplicated.
693 */
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400694int pevent_register_print_string(struct pevent *pevent, const char *fmt,
Steven Rostedtf7d82352012-04-06 00:47:53 +0200695 unsigned long long addr)
696{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300697 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400698 char *p;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200699
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300700 if (!item)
701 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200702
703 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200704 item->addr = addr;
705
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400706 /* Strip off quotes and '\n' from the end */
707 if (fmt[0] == '"')
708 fmt++;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300709 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900710 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300711 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900712
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400713 p = item->printk + strlen(item->printk) - 1;
714 if (*p == '"')
715 *p = 0;
716
717 p -= 2;
718 if (strcmp(p, "\\n") == 0)
719 *p = 0;
720
Namhyung Kimca638582012-04-09 11:54:31 +0900721 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200722 pevent->printk_count++;
723
724 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300725
726out_free:
727 free(item);
728 errno = ENOMEM;
729 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200730}
731
732/**
733 * pevent_print_printk - print out the stored strings
734 * @pevent: handle for the pevent
735 *
736 * This prints the string formats that were stored.
737 */
738void pevent_print_printk(struct pevent *pevent)
739{
740 int i;
741
742 if (!pevent->printk_map)
743 printk_map_init(pevent);
744
745 for (i = 0; i < (int)pevent->printk_count; i++) {
746 printf("%016llx %s\n",
747 pevent->printk_map[i].addr,
748 pevent->printk_map[i].printk);
749 }
750}
751
752static struct event_format *alloc_event(void)
753{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300754 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200755}
756
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300757static int add_event(struct pevent *pevent, struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200758{
759 int i;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300760 struct event_format **events = realloc(pevent->events, sizeof(event) *
761 (pevent->nr_events + 1));
762 if (!events)
763 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200764
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300765 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200766
767 for (i = 0; i < pevent->nr_events; i++) {
768 if (pevent->events[i]->id > event->id)
769 break;
770 }
771 if (i < pevent->nr_events)
772 memmove(&pevent->events[i + 1],
773 &pevent->events[i],
774 sizeof(event) * (pevent->nr_events - i));
775
776 pevent->events[i] = event;
777 pevent->nr_events++;
778
779 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300780
781 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200782}
783
784static int event_item_type(enum event_type type)
785{
786 switch (type) {
787 case EVENT_ITEM ... EVENT_SQUOTE:
788 return 1;
789 case EVENT_ERROR ... EVENT_DELIM:
790 default:
791 return 0;
792 }
793}
794
795static void free_flag_sym(struct print_flag_sym *fsym)
796{
797 struct print_flag_sym *next;
798
799 while (fsym) {
800 next = fsym->next;
801 free(fsym->value);
802 free(fsym->str);
803 free(fsym);
804 fsym = next;
805 }
806}
807
808static void free_arg(struct print_arg *arg)
809{
810 struct print_arg *farg;
811
812 if (!arg)
813 return;
814
815 switch (arg->type) {
816 case PRINT_ATOM:
817 free(arg->atom.atom);
818 break;
819 case PRINT_FIELD:
820 free(arg->field.name);
821 break;
822 case PRINT_FLAGS:
823 free_arg(arg->flags.field);
824 free(arg->flags.delim);
825 free_flag_sym(arg->flags.flags);
826 break;
827 case PRINT_SYMBOL:
828 free_arg(arg->symbol.field);
829 free_flag_sym(arg->symbol.symbols);
830 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900831 case PRINT_HEX:
832 free_arg(arg->hex.field);
833 free_arg(arg->hex.size);
834 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000835 case PRINT_INT_ARRAY:
836 free_arg(arg->int_array.field);
837 free_arg(arg->int_array.count);
838 free_arg(arg->int_array.el_size);
839 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200840 case PRINT_TYPE:
841 free(arg->typecast.type);
842 free_arg(arg->typecast.item);
843 break;
844 case PRINT_STRING:
845 case PRINT_BSTRING:
846 free(arg->string.string);
847 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400848 case PRINT_BITMASK:
849 free(arg->bitmask.bitmask);
850 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200851 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000852 case PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200853 free(arg->dynarray.index);
854 break;
855 case PRINT_OP:
856 free(arg->op.op);
857 free_arg(arg->op.left);
858 free_arg(arg->op.right);
859 break;
860 case PRINT_FUNC:
861 while (arg->func.args) {
862 farg = arg->func.args;
863 arg->func.args = farg->next;
864 free_arg(farg);
865 }
866 break;
867
868 case PRINT_NULL:
869 default:
870 break;
871 }
872
873 free(arg);
874}
875
876static enum event_type get_type(int ch)
877{
878 if (ch == '\n')
879 return EVENT_NEWLINE;
880 if (isspace(ch))
881 return EVENT_SPACE;
882 if (isalnum(ch) || ch == '_')
883 return EVENT_ITEM;
884 if (ch == '\'')
885 return EVENT_SQUOTE;
886 if (ch == '"')
887 return EVENT_DQUOTE;
888 if (!isprint(ch))
889 return EVENT_NONE;
890 if (ch == '(' || ch == ')' || ch == ',')
891 return EVENT_DELIM;
892
893 return EVENT_OP;
894}
895
896static int __read_char(void)
897{
898 if (input_buf_ptr >= input_buf_siz)
899 return -1;
900
901 return input_buf[input_buf_ptr++];
902}
903
904static int __peek_char(void)
905{
906 if (input_buf_ptr >= input_buf_siz)
907 return -1;
908
909 return input_buf[input_buf_ptr];
910}
911
912/**
913 * pevent_peek_char - peek at the next character that will be read
914 *
915 * Returns the next character read, or -1 if end of buffer.
916 */
917int pevent_peek_char(void)
918{
919 return __peek_char();
920}
921
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900922static int extend_token(char **tok, char *buf, int size)
923{
924 char *newtok = realloc(*tok, size);
925
926 if (!newtok) {
927 free(*tok);
928 *tok = NULL;
929 return -1;
930 }
931
932 if (!*tok)
933 strcpy(newtok, buf);
934 else
935 strcat(newtok, buf);
936 *tok = newtok;
937
938 return 0;
939}
940
Steven Rostedtf7d82352012-04-06 00:47:53 +0200941static enum event_type force_token(const char *str, char **tok);
942
943static enum event_type __read_token(char **tok)
944{
945 char buf[BUFSIZ];
946 int ch, last_ch, quote_ch, next_ch;
947 int i = 0;
948 int tok_size = 0;
949 enum event_type type;
950
951 *tok = NULL;
952
953
954 ch = __read_char();
955 if (ch < 0)
956 return EVENT_NONE;
957
958 type = get_type(ch);
959 if (type == EVENT_NONE)
960 return type;
961
962 buf[i++] = ch;
963
964 switch (type) {
965 case EVENT_NEWLINE:
966 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300967 if (asprintf(tok, "%c", ch) < 0)
968 return EVENT_ERROR;
969
Steven Rostedtf7d82352012-04-06 00:47:53 +0200970 return type;
971
972 case EVENT_OP:
973 switch (ch) {
974 case '-':
975 next_ch = __peek_char();
976 if (next_ch == '>') {
977 buf[i++] = __read_char();
978 break;
979 }
980 /* fall through */
981 case '+':
982 case '|':
983 case '&':
984 case '>':
985 case '<':
986 last_ch = ch;
987 ch = __peek_char();
988 if (ch != last_ch)
989 goto test_equal;
990 buf[i++] = __read_char();
991 switch (last_ch) {
992 case '>':
993 case '<':
994 goto test_equal;
995 default:
996 break;
997 }
998 break;
999 case '!':
1000 case '=':
1001 goto test_equal;
1002 default: /* what should we do instead? */
1003 break;
1004 }
1005 buf[i] = 0;
1006 *tok = strdup(buf);
1007 return type;
1008
1009 test_equal:
1010 ch = __peek_char();
1011 if (ch == '=')
1012 buf[i++] = __read_char();
1013 goto out;
1014
1015 case EVENT_DQUOTE:
1016 case EVENT_SQUOTE:
1017 /* don't keep quotes */
1018 i--;
1019 quote_ch = ch;
1020 last_ch = 0;
1021 concat:
1022 do {
1023 if (i == (BUFSIZ - 1)) {
1024 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001025 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001026
1027 if (extend_token(tok, buf, tok_size) < 0)
1028 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001029 i = 0;
1030 }
1031 last_ch = ch;
1032 ch = __read_char();
1033 buf[i++] = ch;
1034 /* the '\' '\' will cancel itself */
1035 if (ch == '\\' && last_ch == '\\')
1036 last_ch = 0;
1037 } while (ch != quote_ch || last_ch == '\\');
1038 /* remove the last quote */
1039 i--;
1040
1041 /*
1042 * For strings (double quotes) check the next token.
1043 * If it is another string, concatinate the two.
1044 */
1045 if (type == EVENT_DQUOTE) {
1046 unsigned long long save_input_buf_ptr = input_buf_ptr;
1047
1048 do {
1049 ch = __read_char();
1050 } while (isspace(ch));
1051 if (ch == '"')
1052 goto concat;
1053 input_buf_ptr = save_input_buf_ptr;
1054 }
1055
1056 goto out;
1057
1058 case EVENT_ERROR ... EVENT_SPACE:
1059 case EVENT_ITEM:
1060 default:
1061 break;
1062 }
1063
1064 while (get_type(__peek_char()) == type) {
1065 if (i == (BUFSIZ - 1)) {
1066 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001067 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001068
1069 if (extend_token(tok, buf, tok_size) < 0)
1070 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001071 i = 0;
1072 }
1073 ch = __read_char();
1074 buf[i++] = ch;
1075 }
1076
1077 out:
1078 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001079 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001080 return EVENT_NONE;
1081
1082 if (type == EVENT_ITEM) {
1083 /*
1084 * Older versions of the kernel has a bug that
1085 * creates invalid symbols and will break the mac80211
1086 * parsing. This is a work around to that bug.
1087 *
1088 * See Linux kernel commit:
1089 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1090 */
1091 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1092 free(*tok);
1093 *tok = NULL;
1094 return force_token("\"\%s\" ", tok);
1095 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1096 free(*tok);
1097 *tok = NULL;
1098 return force_token("\" sta:%pM\" ", tok);
1099 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1100 free(*tok);
1101 *tok = NULL;
1102 return force_token("\" vif:%p(%d)\" ", tok);
1103 }
1104 }
1105
1106 return type;
1107}
1108
1109static enum event_type force_token(const char *str, char **tok)
1110{
1111 const char *save_input_buf;
1112 unsigned long long save_input_buf_ptr;
1113 unsigned long long save_input_buf_siz;
1114 enum event_type type;
1115
1116 /* save off the current input pointers */
1117 save_input_buf = input_buf;
1118 save_input_buf_ptr = input_buf_ptr;
1119 save_input_buf_siz = input_buf_siz;
1120
1121 init_input_buf(str, strlen(str));
1122
1123 type = __read_token(tok);
1124
1125 /* reset back to original token */
1126 input_buf = save_input_buf;
1127 input_buf_ptr = save_input_buf_ptr;
1128 input_buf_siz = save_input_buf_siz;
1129
1130 return type;
1131}
1132
1133static void free_token(char *tok)
1134{
1135 if (tok)
1136 free(tok);
1137}
1138
1139static enum event_type read_token(char **tok)
1140{
1141 enum event_type type;
1142
1143 for (;;) {
1144 type = __read_token(tok);
1145 if (type != EVENT_SPACE)
1146 return type;
1147
1148 free_token(*tok);
1149 }
1150
1151 /* not reached */
1152 *tok = NULL;
1153 return EVENT_NONE;
1154}
1155
1156/**
1157 * pevent_read_token - access to utilites to use the pevent parser
1158 * @tok: The token to return
1159 *
1160 * This will parse tokens from the string given by
1161 * pevent_init_data().
1162 *
1163 * Returns the token type.
1164 */
1165enum event_type pevent_read_token(char **tok)
1166{
1167 return read_token(tok);
1168}
1169
1170/**
1171 * pevent_free_token - free a token returned by pevent_read_token
1172 * @token: the token to free
1173 */
1174void pevent_free_token(char *token)
1175{
1176 free_token(token);
1177}
1178
1179/* no newline */
1180static enum event_type read_token_item(char **tok)
1181{
1182 enum event_type type;
1183
1184 for (;;) {
1185 type = __read_token(tok);
1186 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1187 return type;
1188 free_token(*tok);
1189 *tok = NULL;
1190 }
1191
1192 /* not reached */
1193 *tok = NULL;
1194 return EVENT_NONE;
1195}
1196
1197static int test_type(enum event_type type, enum event_type expect)
1198{
1199 if (type != expect) {
1200 do_warning("Error: expected type %d but read %d",
1201 expect, type);
1202 return -1;
1203 }
1204 return 0;
1205}
1206
1207static int test_type_token(enum event_type type, const char *token,
1208 enum event_type expect, const char *expect_tok)
1209{
1210 if (type != expect) {
1211 do_warning("Error: expected type %d but read %d",
1212 expect, type);
1213 return -1;
1214 }
1215
1216 if (strcmp(token, expect_tok) != 0) {
1217 do_warning("Error: expected '%s' but read '%s'",
1218 expect_tok, token);
1219 return -1;
1220 }
1221 return 0;
1222}
1223
1224static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1225{
1226 enum event_type type;
1227
1228 if (newline_ok)
1229 type = read_token(tok);
1230 else
1231 type = read_token_item(tok);
1232 return test_type(type, expect);
1233}
1234
1235static int read_expect_type(enum event_type expect, char **tok)
1236{
1237 return __read_expect_type(expect, tok, 1);
1238}
1239
1240static int __read_expected(enum event_type expect, const char *str,
1241 int newline_ok)
1242{
1243 enum event_type type;
1244 char *token;
1245 int ret;
1246
1247 if (newline_ok)
1248 type = read_token(&token);
1249 else
1250 type = read_token_item(&token);
1251
1252 ret = test_type_token(type, token, expect, str);
1253
1254 free_token(token);
1255
1256 return ret;
1257}
1258
1259static int read_expected(enum event_type expect, const char *str)
1260{
1261 return __read_expected(expect, str, 1);
1262}
1263
1264static int read_expected_item(enum event_type expect, const char *str)
1265{
1266 return __read_expected(expect, str, 0);
1267}
1268
1269static char *event_read_name(void)
1270{
1271 char *token;
1272
1273 if (read_expected(EVENT_ITEM, "name") < 0)
1274 return NULL;
1275
1276 if (read_expected(EVENT_OP, ":") < 0)
1277 return NULL;
1278
1279 if (read_expect_type(EVENT_ITEM, &token) < 0)
1280 goto fail;
1281
1282 return token;
1283
1284 fail:
1285 free_token(token);
1286 return NULL;
1287}
1288
1289static int event_read_id(void)
1290{
1291 char *token;
1292 int id;
1293
1294 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1295 return -1;
1296
1297 if (read_expected(EVENT_OP, ":") < 0)
1298 return -1;
1299
1300 if (read_expect_type(EVENT_ITEM, &token) < 0)
1301 goto fail;
1302
1303 id = strtoul(token, NULL, 0);
1304 free_token(token);
1305 return id;
1306
1307 fail:
1308 free_token(token);
1309 return -1;
1310}
1311
1312static int field_is_string(struct format_field *field)
1313{
1314 if ((field->flags & FIELD_IS_ARRAY) &&
1315 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1316 strstr(field->type, "s8")))
1317 return 1;
1318
1319 return 0;
1320}
1321
1322static int field_is_dynamic(struct format_field *field)
1323{
1324 if (strncmp(field->type, "__data_loc", 10) == 0)
1325 return 1;
1326
1327 return 0;
1328}
1329
1330static int field_is_long(struct format_field *field)
1331{
1332 /* includes long long */
1333 if (strstr(field->type, "long"))
1334 return 1;
1335
1336 return 0;
1337}
1338
Jiri Olsae23c1a52013-01-24 21:46:43 +01001339static unsigned int type_size(const char *name)
1340{
1341 /* This covers all FIELD_IS_STRING types. */
1342 static struct {
1343 const char *type;
1344 unsigned int size;
1345 } table[] = {
1346 { "u8", 1 },
1347 { "u16", 2 },
1348 { "u32", 4 },
1349 { "u64", 8 },
1350 { "s8", 1 },
1351 { "s16", 2 },
1352 { "s32", 4 },
1353 { "s64", 8 },
1354 { "char", 1 },
1355 { },
1356 };
1357 int i;
1358
1359 for (i = 0; table[i].type; i++) {
1360 if (!strcmp(table[i].type, name))
1361 return table[i].size;
1362 }
1363
1364 return 0;
1365}
1366
Steven Rostedtf7d82352012-04-06 00:47:53 +02001367static int event_read_fields(struct event_format *event, struct format_field **fields)
1368{
1369 struct format_field *field = NULL;
1370 enum event_type type;
1371 char *token;
1372 char *last_token;
1373 int count = 0;
1374
1375 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001376 unsigned int size_dynamic = 0;
1377
Steven Rostedtf7d82352012-04-06 00:47:53 +02001378 type = read_token(&token);
1379 if (type == EVENT_NEWLINE) {
1380 free_token(token);
1381 return count;
1382 }
1383
1384 count++;
1385
1386 if (test_type_token(type, token, EVENT_ITEM, "field"))
1387 goto fail;
1388 free_token(token);
1389
1390 type = read_token(&token);
1391 /*
1392 * The ftrace fields may still use the "special" name.
1393 * Just ignore it.
1394 */
1395 if (event->flags & EVENT_FL_ISFTRACE &&
1396 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1397 free_token(token);
1398 type = read_token(&token);
1399 }
1400
1401 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1402 goto fail;
1403
1404 free_token(token);
1405 if (read_expect_type(EVENT_ITEM, &token) < 0)
1406 goto fail;
1407
1408 last_token = token;
1409
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001410 field = calloc(1, sizeof(*field));
1411 if (!field)
1412 goto fail;
1413
Steven Rostedtf7d82352012-04-06 00:47:53 +02001414 field->event = event;
1415
1416 /* read the rest of the type */
1417 for (;;) {
1418 type = read_token(&token);
1419 if (type == EVENT_ITEM ||
1420 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1421 /*
1422 * Some of the ftrace fields are broken and have
1423 * an illegal "." in them.
1424 */
1425 (event->flags & EVENT_FL_ISFTRACE &&
1426 type == EVENT_OP && strcmp(token, ".") == 0)) {
1427
1428 if (strcmp(token, "*") == 0)
1429 field->flags |= FIELD_IS_POINTER;
1430
1431 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001432 char *new_type;
1433 new_type = realloc(field->type,
1434 strlen(field->type) +
1435 strlen(last_token) + 2);
1436 if (!new_type) {
1437 free(last_token);
1438 goto fail;
1439 }
1440 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001441 strcat(field->type, " ");
1442 strcat(field->type, last_token);
1443 free(last_token);
1444 } else
1445 field->type = last_token;
1446 last_token = token;
1447 continue;
1448 }
1449
1450 break;
1451 }
1452
1453 if (!field->type) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001454 do_warning_event(event, "%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001455 goto fail;
1456 }
Jiri Olsad3542432015-04-18 17:50:18 +02001457 field->name = field->alias = last_token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001458
1459 if (test_type(type, EVENT_OP))
1460 goto fail;
1461
1462 if (strcmp(token, "[") == 0) {
1463 enum event_type last_type = type;
1464 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001465 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001466 int len;
1467
1468 field->flags |= FIELD_IS_ARRAY;
1469
1470 type = read_token(&token);
1471
1472 if (type == EVENT_ITEM)
1473 field->arraylen = strtoul(token, NULL, 0);
1474 else
1475 field->arraylen = 0;
1476
1477 while (strcmp(token, "]") != 0) {
1478 if (last_type == EVENT_ITEM &&
1479 type == EVENT_ITEM)
1480 len = 2;
1481 else
1482 len = 1;
1483 last_type = type;
1484
Namhyung Kimd2864472012-04-09 11:54:33 +09001485 new_brackets = realloc(brackets,
1486 strlen(brackets) +
1487 strlen(token) + len);
1488 if (!new_brackets) {
1489 free(brackets);
1490 goto fail;
1491 }
1492 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001493 if (len == 2)
1494 strcat(brackets, " ");
1495 strcat(brackets, token);
1496 /* We only care about the last token */
1497 field->arraylen = strtoul(token, NULL, 0);
1498 free_token(token);
1499 type = read_token(&token);
1500 if (type == EVENT_NONE) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001501 do_warning_event(event, "failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001502 goto fail;
1503 }
1504 }
1505
1506 free_token(token);
1507
Namhyung Kimd2864472012-04-09 11:54:33 +09001508 new_brackets = realloc(brackets, strlen(brackets) + 2);
1509 if (!new_brackets) {
1510 free(brackets);
1511 goto fail;
1512 }
1513 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001514 strcat(brackets, "]");
1515
1516 /* add brackets to type */
1517
1518 type = read_token(&token);
1519 /*
1520 * If the next token is not an OP, then it is of
1521 * the format: type [] item;
1522 */
1523 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001524 char *new_type;
1525 new_type = realloc(field->type,
1526 strlen(field->type) +
1527 strlen(field->name) +
1528 strlen(brackets) + 2);
1529 if (!new_type) {
1530 free(brackets);
1531 goto fail;
1532 }
1533 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001534 strcat(field->type, " ");
1535 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001536 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001537 free_token(field->name);
1538 strcat(field->type, brackets);
Jiri Olsad3542432015-04-18 17:50:18 +02001539 field->name = field->alias = token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001540 type = read_token(&token);
1541 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001542 char *new_type;
1543 new_type = realloc(field->type,
1544 strlen(field->type) +
1545 strlen(brackets) + 1);
1546 if (!new_type) {
1547 free(brackets);
1548 goto fail;
1549 }
1550 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001551 strcat(field->type, brackets);
1552 }
1553 free(brackets);
1554 }
1555
1556 if (field_is_string(field))
1557 field->flags |= FIELD_IS_STRING;
1558 if (field_is_dynamic(field))
1559 field->flags |= FIELD_IS_DYNAMIC;
1560 if (field_is_long(field))
1561 field->flags |= FIELD_IS_LONG;
1562
1563 if (test_type_token(type, token, EVENT_OP, ";"))
1564 goto fail;
1565 free_token(token);
1566
1567 if (read_expected(EVENT_ITEM, "offset") < 0)
1568 goto fail_expect;
1569
1570 if (read_expected(EVENT_OP, ":") < 0)
1571 goto fail_expect;
1572
1573 if (read_expect_type(EVENT_ITEM, &token))
1574 goto fail;
1575 field->offset = strtoul(token, NULL, 0);
1576 free_token(token);
1577
1578 if (read_expected(EVENT_OP, ";") < 0)
1579 goto fail_expect;
1580
1581 if (read_expected(EVENT_ITEM, "size") < 0)
1582 goto fail_expect;
1583
1584 if (read_expected(EVENT_OP, ":") < 0)
1585 goto fail_expect;
1586
1587 if (read_expect_type(EVENT_ITEM, &token))
1588 goto fail;
1589 field->size = strtoul(token, NULL, 0);
1590 free_token(token);
1591
1592 if (read_expected(EVENT_OP, ";") < 0)
1593 goto fail_expect;
1594
1595 type = read_token(&token);
1596 if (type != EVENT_NEWLINE) {
1597 /* newer versions of the kernel have a "signed" type */
1598 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1599 goto fail;
1600
1601 free_token(token);
1602
1603 if (read_expected(EVENT_OP, ":") < 0)
1604 goto fail_expect;
1605
1606 if (read_expect_type(EVENT_ITEM, &token))
1607 goto fail;
1608
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001609 if (strtoul(token, NULL, 0))
1610 field->flags |= FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001611
1612 free_token(token);
1613 if (read_expected(EVENT_OP, ";") < 0)
1614 goto fail_expect;
1615
1616 if (read_expect_type(EVENT_NEWLINE, &token))
1617 goto fail;
1618 }
1619
1620 free_token(token);
1621
1622 if (field->flags & FIELD_IS_ARRAY) {
1623 if (field->arraylen)
1624 field->elementsize = field->size / field->arraylen;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001625 else if (field->flags & FIELD_IS_DYNAMIC)
1626 field->elementsize = size_dynamic;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001627 else if (field->flags & FIELD_IS_STRING)
1628 field->elementsize = 1;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001629 else if (field->flags & FIELD_IS_LONG)
1630 field->elementsize = event->pevent ?
1631 event->pevent->long_size :
1632 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001633 } else
1634 field->elementsize = field->size;
1635
1636 *fields = field;
1637 fields = &field->next;
1638
1639 } while (1);
1640
1641 return 0;
1642
1643fail:
1644 free_token(token);
1645fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001646 if (field) {
1647 free(field->type);
1648 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001649 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001650 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001651 return -1;
1652}
1653
1654static int event_read_format(struct event_format *event)
1655{
1656 char *token;
1657 int ret;
1658
1659 if (read_expected_item(EVENT_ITEM, "format") < 0)
1660 return -1;
1661
1662 if (read_expected(EVENT_OP, ":") < 0)
1663 return -1;
1664
1665 if (read_expect_type(EVENT_NEWLINE, &token))
1666 goto fail;
1667 free_token(token);
1668
1669 ret = event_read_fields(event, &event->format.common_fields);
1670 if (ret < 0)
1671 return ret;
1672 event->format.nr_common = ret;
1673
1674 ret = event_read_fields(event, &event->format.fields);
1675 if (ret < 0)
1676 return ret;
1677 event->format.nr_fields = ret;
1678
1679 return 0;
1680
1681 fail:
1682 free_token(token);
1683 return -1;
1684}
1685
1686static enum event_type
1687process_arg_token(struct event_format *event, struct print_arg *arg,
1688 char **tok, enum event_type type);
1689
1690static enum event_type
1691process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1692{
1693 enum event_type type;
1694 char *token;
1695
1696 type = read_token(&token);
1697 *tok = token;
1698
1699 return process_arg_token(event, arg, tok, type);
1700}
1701
1702static enum event_type
1703process_op(struct event_format *event, struct print_arg *arg, char **tok);
1704
Steven Rostedteff2c922013-11-18 14:23:14 -05001705/*
1706 * For __print_symbolic() and __print_flags, we need to completely
1707 * evaluate the first argument, which defines what to print next.
1708 */
1709static enum event_type
1710process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1711{
1712 enum event_type type;
1713
1714 type = process_arg(event, arg, tok);
1715
1716 while (type == EVENT_OP) {
1717 type = process_op(event, arg, tok);
1718 }
1719
1720 return type;
1721}
1722
Steven Rostedtf7d82352012-04-06 00:47:53 +02001723static enum event_type
1724process_cond(struct event_format *event, struct print_arg *top, char **tok)
1725{
1726 struct print_arg *arg, *left, *right;
1727 enum event_type type;
1728 char *token = NULL;
1729
1730 arg = alloc_arg();
1731 left = alloc_arg();
1732 right = alloc_arg();
1733
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001734 if (!arg || !left || !right) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001735 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001736 /* arg will be freed at out_free */
1737 free_arg(left);
1738 free_arg(right);
1739 goto out_free;
1740 }
1741
Steven Rostedtf7d82352012-04-06 00:47:53 +02001742 arg->type = PRINT_OP;
1743 arg->op.left = left;
1744 arg->op.right = right;
1745
1746 *tok = NULL;
1747 type = process_arg(event, left, &token);
1748
1749 again:
Dean Nelson6f56e9c2015-08-20 11:16:32 -04001750 if (type == EVENT_ERROR)
1751 goto out_free;
1752
Steven Rostedtf7d82352012-04-06 00:47:53 +02001753 /* Handle other operations in the arguments */
1754 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1755 type = process_op(event, left, &token);
1756 goto again;
1757 }
1758
1759 if (test_type_token(type, token, EVENT_OP, ":"))
1760 goto out_free;
1761
1762 arg->op.op = token;
1763
1764 type = process_arg(event, right, &token);
1765
1766 top->op.right = arg;
1767
1768 *tok = token;
1769 return type;
1770
1771out_free:
1772 /* Top may point to itself */
1773 top->op.right = NULL;
1774 free_token(token);
1775 free_arg(arg);
1776 return EVENT_ERROR;
1777}
1778
1779static enum event_type
1780process_array(struct event_format *event, struct print_arg *top, char **tok)
1781{
1782 struct print_arg *arg;
1783 enum event_type type;
1784 char *token = NULL;
1785
1786 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001787 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001788 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001789 /* '*tok' is set to top->op.op. No need to free. */
1790 *tok = NULL;
1791 return EVENT_ERROR;
1792 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001793
1794 *tok = NULL;
1795 type = process_arg(event, arg, &token);
1796 if (test_type_token(type, token, EVENT_OP, "]"))
1797 goto out_free;
1798
1799 top->op.right = arg;
1800
1801 free_token(token);
1802 type = read_token_item(&token);
1803 *tok = token;
1804
1805 return type;
1806
1807out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001808 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001809 free_arg(arg);
1810 return EVENT_ERROR;
1811}
1812
1813static int get_op_prio(char *op)
1814{
1815 if (!op[1]) {
1816 switch (op[0]) {
1817 case '~':
1818 case '!':
1819 return 4;
1820 case '*':
1821 case '/':
1822 case '%':
1823 return 6;
1824 case '+':
1825 case '-':
1826 return 7;
1827 /* '>>' and '<<' are 8 */
1828 case '<':
1829 case '>':
1830 return 9;
1831 /* '==' and '!=' are 10 */
1832 case '&':
1833 return 11;
1834 case '^':
1835 return 12;
1836 case '|':
1837 return 13;
1838 case '?':
1839 return 16;
1840 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001841 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001842 return -1;
1843 }
1844 } else {
1845 if (strcmp(op, "++") == 0 ||
1846 strcmp(op, "--") == 0) {
1847 return 3;
1848 } else if (strcmp(op, ">>") == 0 ||
1849 strcmp(op, "<<") == 0) {
1850 return 8;
1851 } else if (strcmp(op, ">=") == 0 ||
1852 strcmp(op, "<=") == 0) {
1853 return 9;
1854 } else if (strcmp(op, "==") == 0 ||
1855 strcmp(op, "!=") == 0) {
1856 return 10;
1857 } else if (strcmp(op, "&&") == 0) {
1858 return 14;
1859 } else if (strcmp(op, "||") == 0) {
1860 return 15;
1861 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001862 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001863 return -1;
1864 }
1865 }
1866}
1867
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001868static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001869{
1870
1871 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001872 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001873 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001874 else
1875 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001876
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001877 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001878}
1879
1880/* Note, *tok does not get freed, but will most likely be saved */
1881static enum event_type
1882process_op(struct event_format *event, struct print_arg *arg, char **tok)
1883{
1884 struct print_arg *left, *right = NULL;
1885 enum event_type type;
1886 char *token;
1887
1888 /* the op is passed in via tok */
1889 token = *tok;
1890
1891 if (arg->type == PRINT_OP && !arg->op.left) {
1892 /* handle single op */
1893 if (token[1]) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001894 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001895 goto out_free;
1896 }
1897 switch (token[0]) {
1898 case '~':
1899 case '!':
1900 case '+':
1901 case '-':
1902 break;
1903 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001904 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001905 goto out_free;
1906
1907 }
1908
1909 /* make an empty left */
1910 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001911 if (!left)
1912 goto out_warn_free;
1913
Steven Rostedtf7d82352012-04-06 00:47:53 +02001914 left->type = PRINT_NULL;
1915 arg->op.left = left;
1916
1917 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001918 if (!right)
1919 goto out_warn_free;
1920
Steven Rostedtf7d82352012-04-06 00:47:53 +02001921 arg->op.right = right;
1922
1923 /* do not free the token, it belongs to an op */
1924 *tok = NULL;
1925 type = process_arg(event, right, tok);
1926
1927 } else if (strcmp(token, "?") == 0) {
1928
1929 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001930 if (!left)
1931 goto out_warn_free;
1932
Steven Rostedtf7d82352012-04-06 00:47:53 +02001933 /* copy the top arg to the left */
1934 *left = *arg;
1935
1936 arg->type = PRINT_OP;
1937 arg->op.op = token;
1938 arg->op.left = left;
1939 arg->op.prio = 0;
1940
Namhyung Kim41e51a22012-09-19 15:58:42 +09001941 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001942 type = process_cond(event, arg, tok);
1943
1944 } else if (strcmp(token, ">>") == 0 ||
1945 strcmp(token, "<<") == 0 ||
1946 strcmp(token, "&") == 0 ||
1947 strcmp(token, "|") == 0 ||
1948 strcmp(token, "&&") == 0 ||
1949 strcmp(token, "||") == 0 ||
1950 strcmp(token, "-") == 0 ||
1951 strcmp(token, "+") == 0 ||
1952 strcmp(token, "*") == 0 ||
1953 strcmp(token, "^") == 0 ||
1954 strcmp(token, "/") == 0 ||
Daniel Bristot de Oliveira0e47b382016-02-22 14:08:22 -03001955 strcmp(token, "%") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001956 strcmp(token, "<") == 0 ||
1957 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001958 strcmp(token, "<=") == 0 ||
1959 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001960 strcmp(token, "==") == 0 ||
1961 strcmp(token, "!=") == 0) {
1962
1963 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001964 if (!left)
1965 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001966
1967 /* copy the top arg to the left */
1968 *left = *arg;
1969
1970 arg->type = PRINT_OP;
1971 arg->op.op = token;
1972 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001973 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001974
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001975 if (set_op_prio(arg) == -1) {
1976 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001977 /* arg->op.op (= token) will be freed at out_free */
1978 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001979 goto out_free;
1980 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001981
1982 type = read_token_item(&token);
1983 *tok = token;
1984
1985 /* could just be a type pointer */
1986 if ((strcmp(arg->op.op, "*") == 0) &&
1987 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001988 char *new_atom;
1989
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001990 if (left->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001991 do_warning_event(event, "bad pointer type");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001992 goto out_free;
1993 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001994 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001995 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001996 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001997 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001998
1999 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002000 strcat(left->atom.atom, " *");
2001 free(arg->op.op);
2002 *arg = *left;
2003 free(left);
2004
2005 return type;
2006 }
2007
2008 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002009 if (!right)
2010 goto out_warn_free;
2011
Steven Rostedtf7d82352012-04-06 00:47:53 +02002012 type = process_arg_token(event, right, tok, type);
Dean Nelson6f56e9c2015-08-20 11:16:32 -04002013 if (type == EVENT_ERROR) {
2014 free_arg(right);
2015 /* token was freed in process_arg_token() via *tok */
2016 token = NULL;
2017 goto out_free;
2018 }
Namhyung Kim3201f0d2015-04-06 14:36:16 +09002019
2020 if (right->type == PRINT_OP &&
2021 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2022 struct print_arg tmp;
2023
2024 /* rotate ops according to the priority */
2025 arg->op.right = right->op.left;
2026
2027 tmp = *arg;
2028 *arg = *right;
2029 *right = tmp;
2030
2031 arg->op.left = right;
2032 } else {
2033 arg->op.right = right;
2034 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002035
2036 } else if (strcmp(token, "[") == 0) {
2037
2038 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002039 if (!left)
2040 goto out_warn_free;
2041
Steven Rostedtf7d82352012-04-06 00:47:53 +02002042 *left = *arg;
2043
2044 arg->type = PRINT_OP;
2045 arg->op.op = token;
2046 arg->op.left = left;
2047
2048 arg->op.prio = 0;
2049
Namhyung Kim41e51a22012-09-19 15:58:42 +09002050 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02002051 type = process_array(event, arg, tok);
2052
2053 } else {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002054 do_warning_event(event, "unknown op '%s'", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002055 event->flags |= EVENT_FL_FAILED;
2056 /* the arg is now the left side */
2057 goto out_free;
2058 }
2059
2060 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2061 int prio;
2062
2063 /* higher prios need to be closer to the root */
2064 prio = get_op_prio(*tok);
2065
2066 if (prio > arg->op.prio)
2067 return process_op(event, arg, tok);
2068
2069 return process_op(event, right, tok);
2070 }
2071
2072 return type;
2073
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002074out_warn_free:
Namhyung Kim3388cc32014-03-19 10:22:53 +09002075 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002076out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002077 free_token(token);
2078 *tok = NULL;
2079 return EVENT_ERROR;
2080}
2081
2082static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002083process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02002084 char **tok)
2085{
2086 enum event_type type;
2087 char *field;
2088 char *token;
2089
2090 if (read_expected(EVENT_OP, "->") < 0)
2091 goto out_err;
2092
2093 if (read_expect_type(EVENT_ITEM, &token) < 0)
2094 goto out_free;
2095 field = token;
2096
2097 arg->type = PRINT_FIELD;
2098 arg->field.name = field;
2099
Tom Zanussi5205aec2012-04-06 00:47:58 +02002100 if (is_flag_field) {
2101 arg->field.field = pevent_find_any_field(event, arg->field.name);
2102 arg->field.field->flags |= FIELD_IS_FLAG;
2103 is_flag_field = 0;
2104 } else if (is_symbolic_field) {
2105 arg->field.field = pevent_find_any_field(event, arg->field.name);
2106 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2107 is_symbolic_field = 0;
2108 }
2109
Steven Rostedtf7d82352012-04-06 00:47:53 +02002110 type = read_token(&token);
2111 *tok = token;
2112
2113 return type;
2114
2115 out_free:
2116 free_token(token);
2117 out_err:
2118 *tok = NULL;
2119 return EVENT_ERROR;
2120}
2121
Javi Merino929a6bb2015-03-20 18:12:55 +00002122static int alloc_and_process_delim(struct event_format *event, char *next_token,
2123 struct print_arg **print_arg)
2124{
2125 struct print_arg *field;
2126 enum event_type type;
2127 char *token;
2128 int ret = 0;
2129
2130 field = alloc_arg();
2131 if (!field) {
2132 do_warning_event(event, "%s: not enough memory!", __func__);
2133 errno = ENOMEM;
2134 return -1;
2135 }
2136
2137 type = process_arg(event, field, &token);
2138
2139 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2140 errno = EINVAL;
2141 ret = -1;
2142 free_arg(field);
2143 goto out_free_token;
2144 }
2145
2146 *print_arg = field;
2147
2148out_free_token:
2149 free_token(token);
2150
2151 return ret;
2152}
2153
Steven Rostedtf7d82352012-04-06 00:47:53 +02002154static char *arg_eval (struct print_arg *arg);
2155
2156static unsigned long long
2157eval_type_str(unsigned long long val, const char *type, int pointer)
2158{
2159 int sign = 0;
2160 char *ref;
2161 int len;
2162
2163 len = strlen(type);
2164
2165 if (pointer) {
2166
2167 if (type[len-1] != '*') {
2168 do_warning("pointer expected with non pointer type");
2169 return val;
2170 }
2171
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002172 ref = malloc(len);
2173 if (!ref) {
2174 do_warning("%s: not enough memory!", __func__);
2175 return val;
2176 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002177 memcpy(ref, type, len);
2178
2179 /* chop off the " *" */
2180 ref[len - 2] = 0;
2181
2182 val = eval_type_str(val, ref, 0);
2183 free(ref);
2184 return val;
2185 }
2186
2187 /* check if this is a pointer */
2188 if (type[len - 1] == '*')
2189 return val;
2190
2191 /* Try to figure out the arg size*/
2192 if (strncmp(type, "struct", 6) == 0)
2193 /* all bets off */
2194 return val;
2195
2196 if (strcmp(type, "u8") == 0)
2197 return val & 0xff;
2198
2199 if (strcmp(type, "u16") == 0)
2200 return val & 0xffff;
2201
2202 if (strcmp(type, "u32") == 0)
2203 return val & 0xffffffff;
2204
2205 if (strcmp(type, "u64") == 0 ||
2206 strcmp(type, "s64"))
2207 return val;
2208
2209 if (strcmp(type, "s8") == 0)
2210 return (unsigned long long)(char)val & 0xff;
2211
2212 if (strcmp(type, "s16") == 0)
2213 return (unsigned long long)(short)val & 0xffff;
2214
2215 if (strcmp(type, "s32") == 0)
2216 return (unsigned long long)(int)val & 0xffffffff;
2217
2218 if (strncmp(type, "unsigned ", 9) == 0) {
2219 sign = 0;
2220 type += 9;
2221 }
2222
2223 if (strcmp(type, "char") == 0) {
2224 if (sign)
2225 return (unsigned long long)(char)val & 0xff;
2226 else
2227 return val & 0xff;
2228 }
2229
2230 if (strcmp(type, "short") == 0) {
2231 if (sign)
2232 return (unsigned long long)(short)val & 0xffff;
2233 else
2234 return val & 0xffff;
2235 }
2236
2237 if (strcmp(type, "int") == 0) {
2238 if (sign)
2239 return (unsigned long long)(int)val & 0xffffffff;
2240 else
2241 return val & 0xffffffff;
2242 }
2243
2244 return val;
2245}
2246
2247/*
2248 * Try to figure out the type.
2249 */
2250static unsigned long long
2251eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2252{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002253 if (arg->type != PRINT_TYPE) {
2254 do_warning("expected type argument");
2255 return 0;
2256 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002257
2258 return eval_type_str(val, arg->typecast.type, pointer);
2259}
2260
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002261static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002262{
2263 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002264 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002265
2266 switch (arg->type) {
2267 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002268 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002269 break;
2270 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002271 ret = arg_num_eval(arg->typecast.item, val);
2272 if (!ret)
2273 break;
2274 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002275 break;
2276 case PRINT_OP:
2277 switch (arg->op.op[0]) {
2278 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002279 ret = arg_num_eval(arg->op.left, &left);
2280 if (!ret)
2281 break;
2282 ret = arg_num_eval(arg->op.right, &right);
2283 if (!ret)
2284 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002285 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002286 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002287 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002288 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002289 break;
2290 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002291 ret = arg_num_eval(arg->op.left, &left);
2292 if (!ret)
2293 break;
2294 ret = arg_num_eval(arg->op.right, &right);
2295 if (!ret)
2296 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002297 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002298 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002299 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002300 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002301 break;
2302 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002303 ret = arg_num_eval(arg->op.left, &left);
2304 if (!ret)
2305 break;
2306 ret = arg_num_eval(arg->op.right, &right);
2307 if (!ret)
2308 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002309 switch (arg->op.op[1]) {
2310 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002311 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002312 break;
2313 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002314 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002315 break;
2316 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002317 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002318 break;
2319 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002320 do_warning("unknown op '%s'", arg->op.op);
2321 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002322 }
2323 break;
2324 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002325 ret = arg_num_eval(arg->op.left, &left);
2326 if (!ret)
2327 break;
2328 ret = arg_num_eval(arg->op.right, &right);
2329 if (!ret)
2330 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002331 switch (arg->op.op[1]) {
2332 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002333 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002334 break;
2335 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002336 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002337 break;
2338 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002339 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002340 break;
2341 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002342 do_warning("unknown op '%s'", arg->op.op);
2343 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002344 }
2345 break;
2346 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002347 ret = arg_num_eval(arg->op.left, &left);
2348 if (!ret)
2349 break;
2350 ret = arg_num_eval(arg->op.right, &right);
2351 if (!ret)
2352 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002353
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002354 if (arg->op.op[1] != '=') {
2355 do_warning("unknown op '%s'", arg->op.op);
2356 ret = 0;
2357 } else
2358 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002359 break;
2360 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002361 ret = arg_num_eval(arg->op.left, &left);
2362 if (!ret)
2363 break;
2364 ret = arg_num_eval(arg->op.right, &right);
2365 if (!ret)
2366 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002367
2368 switch (arg->op.op[1]) {
2369 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002370 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002371 break;
2372 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002373 do_warning("unknown op '%s'", arg->op.op);
2374 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002375 }
2376 break;
2377 case '-':
2378 /* check for negative */
2379 if (arg->op.left->type == PRINT_NULL)
2380 left = 0;
2381 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002382 ret = arg_num_eval(arg->op.left, &left);
2383 if (!ret)
2384 break;
2385 ret = arg_num_eval(arg->op.right, &right);
2386 if (!ret)
2387 break;
2388 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002389 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002390 case '+':
2391 if (arg->op.left->type == PRINT_NULL)
2392 left = 0;
2393 else
2394 ret = arg_num_eval(arg->op.left, &left);
2395 if (!ret)
2396 break;
2397 ret = arg_num_eval(arg->op.right, &right);
2398 if (!ret)
2399 break;
2400 *val = left + right;
2401 break;
Steven Rostedt9eb42de2016-02-26 18:13:28 -05002402 case '~':
2403 ret = arg_num_eval(arg->op.right, &right);
2404 if (!ret)
2405 break;
2406 *val = ~right;
2407 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002408 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002409 do_warning("unknown op '%s'", arg->op.op);
2410 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002411 }
2412 break;
2413
2414 case PRINT_NULL:
2415 case PRINT_FIELD ... PRINT_SYMBOL:
2416 case PRINT_STRING:
2417 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002418 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002419 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002420 do_warning("invalid eval type %d", arg->type);
2421 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002422
2423 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002424 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002425}
2426
2427static char *arg_eval (struct print_arg *arg)
2428{
2429 long long val;
2430 static char buf[20];
2431
2432 switch (arg->type) {
2433 case PRINT_ATOM:
2434 return arg->atom.atom;
2435 case PRINT_TYPE:
2436 return arg_eval(arg->typecast.item);
2437 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002438 if (!arg_num_eval(arg, &val))
2439 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002440 sprintf(buf, "%lld", val);
2441 return buf;
2442
2443 case PRINT_NULL:
2444 case PRINT_FIELD ... PRINT_SYMBOL:
2445 case PRINT_STRING:
2446 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002447 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002448 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002449 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002450 break;
2451 }
2452
2453 return NULL;
2454}
2455
2456static enum event_type
2457process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2458{
2459 enum event_type type;
2460 struct print_arg *arg = NULL;
2461 struct print_flag_sym *field;
2462 char *token = *tok;
2463 char *value;
2464
2465 do {
2466 free_token(token);
2467 type = read_token_item(&token);
2468 if (test_type_token(type, token, EVENT_OP, "{"))
2469 break;
2470
2471 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002472 if (!arg)
2473 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002474
2475 free_token(token);
2476 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002477
2478 if (type == EVENT_OP)
2479 type = process_op(event, arg, &token);
2480
2481 if (type == EVENT_ERROR)
2482 goto out_free;
2483
Steven Rostedtf7d82352012-04-06 00:47:53 +02002484 if (test_type_token(type, token, EVENT_DELIM, ","))
2485 goto out_free;
2486
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002487 field = calloc(1, sizeof(*field));
2488 if (!field)
2489 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002490
2491 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002492 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002493 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002494 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002495 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002496 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002497
2498 free_arg(arg);
2499 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002500 if (!arg)
2501 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002502
2503 free_token(token);
2504 type = process_arg(event, arg, &token);
2505 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002506 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002507
2508 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002509 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002510 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002511 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002512 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002513 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002514 free_arg(arg);
2515 arg = NULL;
2516
2517 *list = field;
2518 list = &field->next;
2519
2520 free_token(token);
2521 type = read_token_item(&token);
2522 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2523
2524 *tok = token;
2525 return type;
2526
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002527out_free_field:
2528 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002529out_free:
2530 free_arg(arg);
2531 free_token(token);
2532 *tok = NULL;
2533
2534 return EVENT_ERROR;
2535}
2536
2537static enum event_type
2538process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2539{
2540 struct print_arg *field;
2541 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002542 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002543
2544 memset(arg, 0, sizeof(*arg));
2545 arg->type = PRINT_FLAGS;
2546
2547 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002548 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002549 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002550 goto out_free;
2551 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002552
Steven Rostedteff2c922013-11-18 14:23:14 -05002553 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002554
2555 /* Handle operations in the first argument */
2556 while (type == EVENT_OP)
2557 type = process_op(event, field, &token);
2558
2559 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002560 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002561 free_token(token);
2562
2563 arg->flags.field = field;
2564
2565 type = read_token_item(&token);
2566 if (event_item_type(type)) {
2567 arg->flags.delim = token;
2568 type = read_token_item(&token);
2569 }
2570
2571 if (test_type_token(type, token, EVENT_DELIM, ","))
2572 goto out_free;
2573
2574 type = process_fields(event, &arg->flags.flags, &token);
2575 if (test_type_token(type, token, EVENT_DELIM, ")"))
2576 goto out_free;
2577
2578 free_token(token);
2579 type = read_token_item(tok);
2580 return type;
2581
Namhyung Kim70d93042012-09-19 15:58:44 +09002582out_free_field:
2583 free_arg(field);
2584out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002585 free_token(token);
2586 *tok = NULL;
2587 return EVENT_ERROR;
2588}
2589
2590static enum event_type
2591process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2592{
2593 struct print_arg *field;
2594 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002595 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002596
2597 memset(arg, 0, sizeof(*arg));
2598 arg->type = PRINT_SYMBOL;
2599
2600 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002601 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002602 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002603 goto out_free;
2604 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002605
Steven Rostedteff2c922013-11-18 14:23:14 -05002606 type = process_field_arg(event, field, &token);
2607
Steven Rostedtf7d82352012-04-06 00:47:53 +02002608 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002609 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002610
2611 arg->symbol.field = field;
2612
2613 type = process_fields(event, &arg->symbol.symbols, &token);
2614 if (test_type_token(type, token, EVENT_DELIM, ")"))
2615 goto out_free;
2616
2617 free_token(token);
2618 type = read_token_item(tok);
2619 return type;
2620
Namhyung Kim70d93042012-09-19 15:58:44 +09002621out_free_field:
2622 free_arg(field);
2623out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002624 free_token(token);
2625 *tok = NULL;
2626 return EVENT_ERROR;
2627}
2628
2629static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002630process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2631{
Namhyung Kime080e6f2012-06-27 09:41:41 +09002632 memset(arg, 0, sizeof(*arg));
2633 arg->type = PRINT_HEX;
2634
Javi Merino929a6bb2015-03-20 18:12:55 +00002635 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2636 goto out;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002637
Javi Merino929a6bb2015-03-20 18:12:55 +00002638 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2639 goto free_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002640
Javi Merino929a6bb2015-03-20 18:12:55 +00002641 return read_token_item(tok);
Namhyung Kime080e6f2012-06-27 09:41:41 +09002642
Javi Merino929a6bb2015-03-20 18:12:55 +00002643free_field:
2644 free_arg(arg->hex.field);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002645 arg->hex.field = NULL;
Javi Merino929a6bb2015-03-20 18:12:55 +00002646out:
Namhyung Kime080e6f2012-06-27 09:41:41 +09002647 *tok = NULL;
2648 return EVENT_ERROR;
2649}
Namhyung Kime080e6f2012-06-27 09:41:41 +09002650
Namhyung Kime080e6f2012-06-27 09:41:41 +09002651static enum event_type
Javi Merinob839e1e82015-03-24 11:07:19 +00002652process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2653{
2654 memset(arg, 0, sizeof(*arg));
2655 arg->type = PRINT_INT_ARRAY;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002656
Javi Merinob839e1e82015-03-24 11:07:19 +00002657 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2658 goto out;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002659
Javi Merinob839e1e82015-03-24 11:07:19 +00002660 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2661 goto free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002662
Javi Merinob839e1e82015-03-24 11:07:19 +00002663 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2664 goto free_size;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002665
Javi Merinob839e1e82015-03-24 11:07:19 +00002666 return read_token_item(tok);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002667
Javi Merinob839e1e82015-03-24 11:07:19 +00002668free_size:
2669 free_arg(arg->int_array.count);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002670 arg->int_array.count = NULL;
Javi Merinob839e1e82015-03-24 11:07:19 +00002671free_field:
2672 free_arg(arg->int_array.field);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002673 arg->int_array.field = NULL;
Javi Merinob839e1e82015-03-24 11:07:19 +00002674out:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002675 *tok = NULL;
2676 return EVENT_ERROR;
2677}
2678
2679static enum event_type
2680process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2681{
2682 struct format_field *field;
2683 enum event_type type;
2684 char *token;
2685
2686 memset(arg, 0, sizeof(*arg));
2687 arg->type = PRINT_DYNAMIC_ARRAY;
2688
2689 /*
2690 * The item within the parenthesis is another field that holds
2691 * the index into where the array starts.
2692 */
2693 type = read_token(&token);
2694 *tok = token;
2695 if (type != EVENT_ITEM)
2696 goto out_free;
2697
2698 /* Find the field */
2699
2700 field = pevent_find_field(event, token);
2701 if (!field)
2702 goto out_free;
2703
2704 arg->dynarray.field = field;
2705 arg->dynarray.index = 0;
2706
2707 if (read_expected(EVENT_DELIM, ")") < 0)
2708 goto out_free;
2709
2710 free_token(token);
2711 type = read_token_item(&token);
2712 *tok = token;
2713 if (type != EVENT_OP || strcmp(token, "[") != 0)
2714 return type;
2715
2716 free_token(token);
2717 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002718 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002719 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002720 *tok = NULL;
2721 return EVENT_ERROR;
2722 }
2723
Steven Rostedtf7d82352012-04-06 00:47:53 +02002724 type = process_arg(event, arg, &token);
2725 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002726 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002727
2728 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002729 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002730
2731 free_token(token);
2732 type = read_token_item(tok);
2733 return type;
2734
Namhyung Kimb3511d02012-05-23 11:36:50 +09002735 out_free_arg:
2736 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002737 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002738 free_token(token);
2739 *tok = NULL;
2740 return EVENT_ERROR;
2741}
2742
2743static enum event_type
He Kuang76055942015-08-29 04:22:05 +00002744process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2745 char **tok)
2746{
2747 struct format_field *field;
2748 enum event_type type;
2749 char *token;
2750
2751 if (read_expect_type(EVENT_ITEM, &token) < 0)
2752 goto out_free;
2753
2754 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2755
2756 /* Find the field */
2757 field = pevent_find_field(event, token);
2758 if (!field)
2759 goto out_free;
2760
2761 arg->dynarray.field = field;
2762 arg->dynarray.index = 0;
2763
2764 if (read_expected(EVENT_DELIM, ")") < 0)
2765 goto out_err;
2766
2767 type = read_token(&token);
2768 *tok = token;
2769
2770 return type;
2771
2772 out_free:
2773 free_token(token);
2774 out_err:
2775 *tok = NULL;
2776 return EVENT_ERROR;
2777}
2778
2779static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002780process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2781{
2782 struct print_arg *item_arg;
2783 enum event_type type;
2784 char *token;
2785
2786 type = process_arg(event, arg, &token);
2787
2788 if (type == EVENT_ERROR)
2789 goto out_free;
2790
2791 if (type == EVENT_OP)
2792 type = process_op(event, arg, &token);
2793
2794 if (type == EVENT_ERROR)
2795 goto out_free;
2796
2797 if (test_type_token(type, token, EVENT_DELIM, ")"))
2798 goto out_free;
2799
2800 free_token(token);
2801 type = read_token_item(&token);
2802
2803 /*
2804 * If the next token is an item or another open paren, then
2805 * this was a typecast.
2806 */
2807 if (event_item_type(type) ||
2808 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2809
2810 /* make this a typecast and contine */
2811
2812 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002813 if (arg->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002814 do_warning_event(event, "previous needed to be PRINT_ATOM");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002815 goto out_free;
2816 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002817
2818 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002819 if (!item_arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002820 do_warning_event(event, "%s: not enough memory!",
2821 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002822 goto out_free;
2823 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002824
2825 arg->type = PRINT_TYPE;
2826 arg->typecast.type = arg->atom.atom;
2827 arg->typecast.item = item_arg;
2828 type = process_arg_token(event, item_arg, &token, type);
2829
2830 }
2831
2832 *tok = token;
2833 return type;
2834
2835 out_free:
2836 free_token(token);
2837 *tok = NULL;
2838 return EVENT_ERROR;
2839}
2840
2841
2842static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002843process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2844 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002845{
2846 enum event_type type;
2847 char *token;
2848
2849 if (read_expect_type(EVENT_ITEM, &token) < 0)
2850 goto out_free;
2851
2852 arg->type = PRINT_STRING;
2853 arg->string.string = token;
2854 arg->string.offset = -1;
2855
2856 if (read_expected(EVENT_DELIM, ")") < 0)
2857 goto out_err;
2858
2859 type = read_token(&token);
2860 *tok = token;
2861
2862 return type;
2863
2864 out_free:
2865 free_token(token);
2866 out_err:
2867 *tok = NULL;
2868 return EVENT_ERROR;
2869}
2870
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002871static enum event_type
2872process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2873 char **tok)
2874{
2875 enum event_type type;
2876 char *token;
2877
2878 if (read_expect_type(EVENT_ITEM, &token) < 0)
2879 goto out_free;
2880
2881 arg->type = PRINT_BITMASK;
2882 arg->bitmask.bitmask = token;
2883 arg->bitmask.offset = -1;
2884
2885 if (read_expected(EVENT_DELIM, ")") < 0)
2886 goto out_err;
2887
2888 type = read_token(&token);
2889 *tok = token;
2890
2891 return type;
2892
2893 out_free:
2894 free_token(token);
2895 out_err:
2896 *tok = NULL;
2897 return EVENT_ERROR;
2898}
2899
Steven Rostedtf7d82352012-04-06 00:47:53 +02002900static struct pevent_function_handler *
2901find_func_handler(struct pevent *pevent, char *func_name)
2902{
2903 struct pevent_function_handler *func;
2904
Steven Rostedt101782e2012-10-01 20:13:51 -04002905 if (!pevent)
2906 return NULL;
2907
Steven Rostedtf7d82352012-04-06 00:47:53 +02002908 for (func = pevent->func_handlers; func; func = func->next) {
2909 if (strcmp(func->name, func_name) == 0)
2910 break;
2911 }
2912
2913 return func;
2914}
2915
2916static void remove_func_handler(struct pevent *pevent, char *func_name)
2917{
2918 struct pevent_function_handler *func;
2919 struct pevent_function_handler **next;
2920
2921 next = &pevent->func_handlers;
2922 while ((func = *next)) {
2923 if (strcmp(func->name, func_name) == 0) {
2924 *next = func->next;
2925 free_func_handle(func);
2926 break;
2927 }
2928 next = &func->next;
2929 }
2930}
2931
2932static enum event_type
2933process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2934 struct print_arg *arg, char **tok)
2935{
2936 struct print_arg **next_arg;
2937 struct print_arg *farg;
2938 enum event_type type;
2939 char *token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002940 int i;
2941
2942 arg->type = PRINT_FUNC;
2943 arg->func.func = func;
2944
2945 *tok = NULL;
2946
2947 next_arg = &(arg->func.args);
2948 for (i = 0; i < func->nr_args; i++) {
2949 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002950 if (!farg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002951 do_warning_event(event, "%s: not enough memory!",
2952 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002953 return EVENT_ERROR;
2954 }
2955
Steven Rostedtf7d82352012-04-06 00:47:53 +02002956 type = process_arg(event, farg, &token);
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002957 if (i < (func->nr_args - 1)) {
2958 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002959 do_warning_event(event,
2960 "Error: function '%s()' expects %d arguments but event %s only uses %d",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002961 func->name, func->nr_args,
2962 event->name, i + 1);
2963 goto err;
2964 }
2965 } else {
2966 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002967 do_warning_event(event,
2968 "Error: function '%s()' only expects %d arguments but event %s has more",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002969 func->name, func->nr_args, event->name);
2970 goto err;
2971 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002972 }
2973
2974 *next_arg = farg;
2975 next_arg = &(farg->next);
2976 free_token(token);
2977 }
2978
2979 type = read_token(&token);
2980 *tok = token;
2981
2982 return type;
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002983
2984err:
2985 free_arg(farg);
2986 free_token(token);
2987 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002988}
2989
2990static enum event_type
2991process_function(struct event_format *event, struct print_arg *arg,
2992 char *token, char **tok)
2993{
2994 struct pevent_function_handler *func;
2995
2996 if (strcmp(token, "__print_flags") == 0) {
2997 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002998 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002999 return process_flags(event, arg, tok);
3000 }
3001 if (strcmp(token, "__print_symbolic") == 0) {
3002 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02003003 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003004 return process_symbols(event, arg, tok);
3005 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003006 if (strcmp(token, "__print_hex") == 0) {
3007 free_token(token);
3008 return process_hex(event, arg, tok);
3009 }
Javi Merinob839e1e82015-03-24 11:07:19 +00003010 if (strcmp(token, "__print_array") == 0) {
3011 free_token(token);
3012 return process_int_array(event, arg, tok);
3013 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003014 if (strcmp(token, "__get_str") == 0) {
3015 free_token(token);
3016 return process_str(event, arg, tok);
3017 }
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003018 if (strcmp(token, "__get_bitmask") == 0) {
3019 free_token(token);
3020 return process_bitmask(event, arg, tok);
3021 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003022 if (strcmp(token, "__get_dynamic_array") == 0) {
3023 free_token(token);
3024 return process_dynamic_array(event, arg, tok);
3025 }
He Kuang76055942015-08-29 04:22:05 +00003026 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3027 free_token(token);
3028 return process_dynamic_array_len(event, arg, tok);
3029 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003030
3031 func = find_func_handler(event->pevent, token);
3032 if (func) {
3033 free_token(token);
3034 return process_func_handler(event, func, arg, tok);
3035 }
3036
Namhyung Kim3388cc32014-03-19 10:22:53 +09003037 do_warning_event(event, "function %s not defined", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003038 free_token(token);
3039 return EVENT_ERROR;
3040}
3041
3042static enum event_type
3043process_arg_token(struct event_format *event, struct print_arg *arg,
3044 char **tok, enum event_type type)
3045{
3046 char *token;
3047 char *atom;
3048
3049 token = *tok;
3050
3051 switch (type) {
3052 case EVENT_ITEM:
3053 if (strcmp(token, "REC") == 0) {
3054 free_token(token);
3055 type = process_entry(event, arg, &token);
3056 break;
3057 }
3058 atom = token;
3059 /* test the next token */
3060 type = read_token_item(&token);
3061
3062 /*
3063 * If the next token is a parenthesis, then this
3064 * is a function.
3065 */
3066 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3067 free_token(token);
3068 token = NULL;
3069 /* this will free atom. */
3070 type = process_function(event, arg, atom, &token);
3071 break;
3072 }
3073 /* atoms can be more than one token long */
3074 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09003075 char *new_atom;
3076 new_atom = realloc(atom,
3077 strlen(atom) + strlen(token) + 2);
3078 if (!new_atom) {
3079 free(atom);
3080 *tok = NULL;
3081 free_token(token);
3082 return EVENT_ERROR;
3083 }
3084 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003085 strcat(atom, " ");
3086 strcat(atom, token);
3087 free_token(token);
3088 type = read_token_item(&token);
3089 }
3090
3091 arg->type = PRINT_ATOM;
3092 arg->atom.atom = atom;
3093 break;
3094
3095 case EVENT_DQUOTE:
3096 case EVENT_SQUOTE:
3097 arg->type = PRINT_ATOM;
3098 arg->atom.atom = token;
3099 type = read_token_item(&token);
3100 break;
3101 case EVENT_DELIM:
3102 if (strcmp(token, "(") == 0) {
3103 free_token(token);
3104 type = process_paren(event, arg, &token);
3105 break;
3106 }
3107 case EVENT_OP:
3108 /* handle single ops */
3109 arg->type = PRINT_OP;
3110 arg->op.op = token;
3111 arg->op.left = NULL;
3112 type = process_op(event, arg, &token);
3113
3114 /* On error, the op is freed */
3115 if (type == EVENT_ERROR)
3116 arg->op.op = NULL;
3117
3118 /* return error type if errored */
3119 break;
3120
3121 case EVENT_ERROR ... EVENT_NEWLINE:
3122 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003123 do_warning_event(event, "unexpected type %d", type);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003124 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003125 }
3126 *tok = token;
3127
3128 return type;
3129}
3130
3131static int event_read_print_args(struct event_format *event, struct print_arg **list)
3132{
3133 enum event_type type = EVENT_ERROR;
3134 struct print_arg *arg;
3135 char *token;
3136 int args = 0;
3137
3138 do {
3139 if (type == EVENT_NEWLINE) {
3140 type = read_token_item(&token);
3141 continue;
3142 }
3143
3144 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003145 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003146 do_warning_event(event, "%s: not enough memory!",
3147 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003148 return -1;
3149 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003150
3151 type = process_arg(event, arg, &token);
3152
3153 if (type == EVENT_ERROR) {
3154 free_token(token);
3155 free_arg(arg);
3156 return -1;
3157 }
3158
3159 *list = arg;
3160 args++;
3161
3162 if (type == EVENT_OP) {
3163 type = process_op(event, arg, &token);
3164 free_token(token);
3165 if (type == EVENT_ERROR) {
3166 *list = NULL;
3167 free_arg(arg);
3168 return -1;
3169 }
3170 list = &arg->next;
3171 continue;
3172 }
3173
3174 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3175 free_token(token);
3176 *list = arg;
3177 list = &arg->next;
3178 continue;
3179 }
3180 break;
3181 } while (type != EVENT_NONE);
3182
3183 if (type != EVENT_NONE && type != EVENT_ERROR)
3184 free_token(token);
3185
3186 return args;
3187}
3188
3189static int event_read_print(struct event_format *event)
3190{
3191 enum event_type type;
3192 char *token;
3193 int ret;
3194
3195 if (read_expected_item(EVENT_ITEM, "print") < 0)
3196 return -1;
3197
3198 if (read_expected(EVENT_ITEM, "fmt") < 0)
3199 return -1;
3200
3201 if (read_expected(EVENT_OP, ":") < 0)
3202 return -1;
3203
3204 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3205 goto fail;
3206
3207 concat:
3208 event->print_fmt.format = token;
3209 event->print_fmt.args = NULL;
3210
3211 /* ok to have no arg */
3212 type = read_token_item(&token);
3213
3214 if (type == EVENT_NONE)
3215 return 0;
3216
3217 /* Handle concatenation of print lines */
3218 if (type == EVENT_DQUOTE) {
3219 char *cat;
3220
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003221 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3222 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003223 free_token(token);
3224 free_token(event->print_fmt.format);
3225 event->print_fmt.format = NULL;
3226 token = cat;
3227 goto concat;
3228 }
3229
3230 if (test_type_token(type, token, EVENT_DELIM, ","))
3231 goto fail;
3232
3233 free_token(token);
3234
3235 ret = event_read_print_args(event, &event->print_fmt.args);
3236 if (ret < 0)
3237 return -1;
3238
3239 return ret;
3240
3241 fail:
3242 free_token(token);
3243 return -1;
3244}
3245
3246/**
3247 * pevent_find_common_field - return a common field by event
3248 * @event: handle for the event
3249 * @name: the name of the common field to return
3250 *
3251 * Returns a common field from the event by the given @name.
3252 * This only searchs the common fields and not all field.
3253 */
3254struct format_field *
3255pevent_find_common_field(struct event_format *event, const char *name)
3256{
3257 struct format_field *format;
3258
3259 for (format = event->format.common_fields;
3260 format; format = format->next) {
3261 if (strcmp(format->name, name) == 0)
3262 break;
3263 }
3264
3265 return format;
3266}
3267
3268/**
3269 * pevent_find_field - find a non-common field
3270 * @event: handle for the event
3271 * @name: the name of the non-common field
3272 *
3273 * Returns a non-common field by the given @name.
3274 * This does not search common fields.
3275 */
3276struct format_field *
3277pevent_find_field(struct event_format *event, const char *name)
3278{
3279 struct format_field *format;
3280
3281 for (format = event->format.fields;
3282 format; format = format->next) {
3283 if (strcmp(format->name, name) == 0)
3284 break;
3285 }
3286
3287 return format;
3288}
3289
3290/**
3291 * pevent_find_any_field - find any field by name
3292 * @event: handle for the event
3293 * @name: the name of the field
3294 *
3295 * Returns a field by the given @name.
3296 * This searchs the common field names first, then
3297 * the non-common ones if a common one was not found.
3298 */
3299struct format_field *
3300pevent_find_any_field(struct event_format *event, const char *name)
3301{
3302 struct format_field *format;
3303
3304 format = pevent_find_common_field(event, name);
3305 if (format)
3306 return format;
3307 return pevent_find_field(event, name);
3308}
3309
3310/**
3311 * pevent_read_number - read a number from data
3312 * @pevent: handle for the pevent
3313 * @ptr: the raw data
3314 * @size: the size of the data that holds the number
3315 *
3316 * Returns the number (converted to host) from the
3317 * raw data.
3318 */
3319unsigned long long pevent_read_number(struct pevent *pevent,
3320 const void *ptr, int size)
3321{
3322 switch (size) {
3323 case 1:
3324 return *(unsigned char *)ptr;
3325 case 2:
3326 return data2host2(pevent, ptr);
3327 case 4:
3328 return data2host4(pevent, ptr);
3329 case 8:
3330 return data2host8(pevent, ptr);
3331 default:
3332 /* BUG! */
3333 return 0;
3334 }
3335}
3336
3337/**
3338 * pevent_read_number_field - read a number from data
3339 * @field: a handle to the field
3340 * @data: the raw data to read
3341 * @value: the value to place the number in
3342 *
3343 * Reads raw data according to a field offset and size,
3344 * and translates it into @value.
3345 *
3346 * Returns 0 on success, -1 otherwise.
3347 */
3348int pevent_read_number_field(struct format_field *field, const void *data,
3349 unsigned long long *value)
3350{
3351 if (!field)
3352 return -1;
3353 switch (field->size) {
3354 case 1:
3355 case 2:
3356 case 4:
3357 case 8:
3358 *value = pevent_read_number(field->event->pevent,
3359 data + field->offset, field->size);
3360 return 0;
3361 default:
3362 return -1;
3363 }
3364}
3365
3366static int get_common_info(struct pevent *pevent,
3367 const char *type, int *offset, int *size)
3368{
3369 struct event_format *event;
3370 struct format_field *field;
3371
3372 /*
3373 * All events should have the same common elements.
3374 * Pick any event to find where the type is;
3375 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003376 if (!pevent->events) {
3377 do_warning("no event_list!");
3378 return -1;
3379 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003380
3381 event = pevent->events[0];
3382 field = pevent_find_common_field(event, type);
3383 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003384 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003385
3386 *offset = field->offset;
3387 *size = field->size;
3388
3389 return 0;
3390}
3391
3392static int __parse_common(struct pevent *pevent, void *data,
3393 int *size, int *offset, const char *name)
3394{
3395 int ret;
3396
3397 if (!*size) {
3398 ret = get_common_info(pevent, name, offset, size);
3399 if (ret < 0)
3400 return ret;
3401 }
3402 return pevent_read_number(pevent, data + *offset, *size);
3403}
3404
3405static int trace_parse_common_type(struct pevent *pevent, void *data)
3406{
3407 return __parse_common(pevent, data,
3408 &pevent->type_size, &pevent->type_offset,
3409 "common_type");
3410}
3411
3412static int parse_common_pid(struct pevent *pevent, void *data)
3413{
3414 return __parse_common(pevent, data,
3415 &pevent->pid_size, &pevent->pid_offset,
3416 "common_pid");
3417}
3418
3419static int parse_common_pc(struct pevent *pevent, void *data)
3420{
3421 return __parse_common(pevent, data,
3422 &pevent->pc_size, &pevent->pc_offset,
3423 "common_preempt_count");
3424}
3425
3426static int parse_common_flags(struct pevent *pevent, void *data)
3427{
3428 return __parse_common(pevent, data,
3429 &pevent->flags_size, &pevent->flags_offset,
3430 "common_flags");
3431}
3432
3433static int parse_common_lock_depth(struct pevent *pevent, void *data)
3434{
Steven Rostedt0866a972012-05-22 14:52:40 +09003435 return __parse_common(pevent, data,
3436 &pevent->ld_size, &pevent->ld_offset,
3437 "common_lock_depth");
3438}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003439
Steven Rostedt0866a972012-05-22 14:52:40 +09003440static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3441{
3442 return __parse_common(pevent, data,
3443 &pevent->ld_size, &pevent->ld_offset,
3444 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003445}
3446
3447static int events_id_cmp(const void *a, const void *b);
3448
3449/**
3450 * pevent_find_event - find an event by given id
3451 * @pevent: a handle to the pevent
3452 * @id: the id of the event
3453 *
3454 * Returns an event that has a given @id.
3455 */
3456struct event_format *pevent_find_event(struct pevent *pevent, int id)
3457{
3458 struct event_format **eventptr;
3459 struct event_format key;
3460 struct event_format *pkey = &key;
3461
3462 /* Check cache first */
3463 if (pevent->last_event && pevent->last_event->id == id)
3464 return pevent->last_event;
3465
3466 key.id = id;
3467
3468 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3469 sizeof(*pevent->events), events_id_cmp);
3470
3471 if (eventptr) {
3472 pevent->last_event = *eventptr;
3473 return *eventptr;
3474 }
3475
3476 return NULL;
3477}
3478
3479/**
3480 * pevent_find_event_by_name - find an event by given name
3481 * @pevent: a handle to the pevent
3482 * @sys: the system name to search for
3483 * @name: the name of the event to search for
3484 *
3485 * This returns an event with a given @name and under the system
3486 * @sys. If @sys is NULL the first event with @name is returned.
3487 */
3488struct event_format *
3489pevent_find_event_by_name(struct pevent *pevent,
3490 const char *sys, const char *name)
3491{
3492 struct event_format *event;
3493 int i;
3494
3495 if (pevent->last_event &&
3496 strcmp(pevent->last_event->name, name) == 0 &&
3497 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3498 return pevent->last_event;
3499
3500 for (i = 0; i < pevent->nr_events; i++) {
3501 event = pevent->events[i];
3502 if (strcmp(event->name, name) == 0) {
3503 if (!sys)
3504 break;
3505 if (strcmp(event->system, sys) == 0)
3506 break;
3507 }
3508 }
3509 if (i == pevent->nr_events)
3510 event = NULL;
3511
3512 pevent->last_event = event;
3513 return event;
3514}
3515
3516static unsigned long long
3517eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3518{
3519 struct pevent *pevent = event->pevent;
3520 unsigned long long val = 0;
3521 unsigned long long left, right;
3522 struct print_arg *typearg = NULL;
3523 struct print_arg *larg;
3524 unsigned long offset;
3525 unsigned int field_size;
3526
3527 switch (arg->type) {
3528 case PRINT_NULL:
3529 /* ?? */
3530 return 0;
3531 case PRINT_ATOM:
3532 return strtoull(arg->atom.atom, NULL, 0);
3533 case PRINT_FIELD:
3534 if (!arg->field.field) {
3535 arg->field.field = pevent_find_any_field(event, arg->field.name);
3536 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003537 goto out_warning_field;
3538
Steven Rostedtf7d82352012-04-06 00:47:53 +02003539 }
3540 /* must be a number */
3541 val = pevent_read_number(pevent, data + arg->field.field->offset,
3542 arg->field.field->size);
3543 break;
3544 case PRINT_FLAGS:
3545 case PRINT_SYMBOL:
Javi Merinob839e1e82015-03-24 11:07:19 +00003546 case PRINT_INT_ARRAY:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003547 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003548 break;
3549 case PRINT_TYPE:
3550 val = eval_num_arg(data, size, event, arg->typecast.item);
3551 return eval_type(val, arg, 0);
3552 case PRINT_STRING:
3553 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003554 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003555 return 0;
3556 case PRINT_FUNC: {
3557 struct trace_seq s;
3558 trace_seq_init(&s);
3559 val = process_defined_func(&s, data, size, event, arg);
3560 trace_seq_destroy(&s);
3561 return val;
3562 }
3563 case PRINT_OP:
3564 if (strcmp(arg->op.op, "[") == 0) {
3565 /*
3566 * Arrays are special, since we don't want
3567 * to read the arg as is.
3568 */
3569 right = eval_num_arg(data, size, event, arg->op.right);
3570
3571 /* handle typecasts */
3572 larg = arg->op.left;
3573 while (larg->type == PRINT_TYPE) {
3574 if (!typearg)
3575 typearg = larg;
3576 larg = larg->typecast.item;
3577 }
3578
3579 /* Default to long size */
3580 field_size = pevent->long_size;
3581
3582 switch (larg->type) {
3583 case PRINT_DYNAMIC_ARRAY:
3584 offset = pevent_read_number(pevent,
3585 data + larg->dynarray.field->offset,
3586 larg->dynarray.field->size);
3587 if (larg->dynarray.field->elementsize)
3588 field_size = larg->dynarray.field->elementsize;
3589 /*
3590 * The actual length of the dynamic array is stored
3591 * in the top half of the field, and the offset
3592 * is in the bottom half of the 32 bit field.
3593 */
3594 offset &= 0xffff;
3595 offset += right;
3596 break;
3597 case PRINT_FIELD:
3598 if (!larg->field.field) {
3599 larg->field.field =
3600 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003601 if (!larg->field.field) {
3602 arg = larg;
3603 goto out_warning_field;
3604 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003605 }
3606 field_size = larg->field.field->elementsize;
3607 offset = larg->field.field->offset +
3608 right * larg->field.field->elementsize;
3609 break;
3610 default:
3611 goto default_op; /* oops, all bets off */
3612 }
3613 val = pevent_read_number(pevent,
3614 data + offset, field_size);
3615 if (typearg)
3616 val = eval_type(val, typearg, 1);
3617 break;
3618 } else if (strcmp(arg->op.op, "?") == 0) {
3619 left = eval_num_arg(data, size, event, arg->op.left);
3620 arg = arg->op.right;
3621 if (left)
3622 val = eval_num_arg(data, size, event, arg->op.left);
3623 else
3624 val = eval_num_arg(data, size, event, arg->op.right);
3625 break;
3626 }
3627 default_op:
3628 left = eval_num_arg(data, size, event, arg->op.left);
3629 right = eval_num_arg(data, size, event, arg->op.right);
3630 switch (arg->op.op[0]) {
3631 case '!':
3632 switch (arg->op.op[1]) {
3633 case 0:
3634 val = !right;
3635 break;
3636 case '=':
3637 val = left != right;
3638 break;
3639 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003640 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003641 }
3642 break;
3643 case '~':
3644 val = ~right;
3645 break;
3646 case '|':
3647 if (arg->op.op[1])
3648 val = left || right;
3649 else
3650 val = left | right;
3651 break;
3652 case '&':
3653 if (arg->op.op[1])
3654 val = left && right;
3655 else
3656 val = left & right;
3657 break;
3658 case '<':
3659 switch (arg->op.op[1]) {
3660 case 0:
3661 val = left < right;
3662 break;
3663 case '<':
3664 val = left << right;
3665 break;
3666 case '=':
3667 val = left <= right;
3668 break;
3669 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003670 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003671 }
3672 break;
3673 case '>':
3674 switch (arg->op.op[1]) {
3675 case 0:
3676 val = left > right;
3677 break;
3678 case '>':
3679 val = left >> right;
3680 break;
3681 case '=':
3682 val = left >= right;
3683 break;
3684 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003685 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003686 }
3687 break;
3688 case '=':
3689 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003690 goto out_warning_op;
3691
Steven Rostedtf7d82352012-04-06 00:47:53 +02003692 val = left == right;
3693 break;
3694 case '-':
3695 val = left - right;
3696 break;
3697 case '+':
3698 val = left + right;
3699 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003700 case '/':
3701 val = left / right;
3702 break;
Daniel Bristot de Oliveira0e47b382016-02-22 14:08:22 -03003703 case '%':
3704 val = left % right;
3705 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003706 case '*':
3707 val = left * right;
3708 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003709 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003710 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003711 }
3712 break;
He Kuang76055942015-08-29 04:22:05 +00003713 case PRINT_DYNAMIC_ARRAY_LEN:
3714 offset = pevent_read_number(pevent,
3715 data + arg->dynarray.field->offset,
3716 arg->dynarray.field->size);
3717 /*
3718 * The total allocated length of the dynamic array is
3719 * stored in the top half of the field, and the offset
3720 * is in the bottom half of the 32 bit field.
3721 */
3722 val = (unsigned long long)(offset >> 16);
3723 break;
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003724 case PRINT_DYNAMIC_ARRAY:
3725 /* Without [], we pass the address to the dynamic data */
3726 offset = pevent_read_number(pevent,
3727 data + arg->dynarray.field->offset,
3728 arg->dynarray.field->size);
3729 /*
He Kuang76055942015-08-29 04:22:05 +00003730 * The total allocated length of the dynamic array is
3731 * stored in the top half of the field, and the offset
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003732 * is in the bottom half of the 32 bit field.
3733 */
3734 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003735 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003736 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003737 default: /* not sure what to do there */
3738 return 0;
3739 }
3740 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003741
3742out_warning_op:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003743 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003744 return 0;
3745
3746out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003747 do_warning_event(event, "%s: field %s not found",
3748 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003749 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003750}
3751
3752struct flag {
3753 const char *name;
3754 unsigned long long value;
3755};
3756
3757static const struct flag flags[] = {
3758 { "HI_SOFTIRQ", 0 },
3759 { "TIMER_SOFTIRQ", 1 },
3760 { "NET_TX_SOFTIRQ", 2 },
3761 { "NET_RX_SOFTIRQ", 3 },
3762 { "BLOCK_SOFTIRQ", 4 },
Christoph Hellwig511cbce2015-11-10 14:56:14 +01003763 { "IRQ_POLL_SOFTIRQ", 5 },
Steven Rostedtf7d82352012-04-06 00:47:53 +02003764 { "TASKLET_SOFTIRQ", 6 },
3765 { "SCHED_SOFTIRQ", 7 },
3766 { "HRTIMER_SOFTIRQ", 8 },
3767 { "RCU_SOFTIRQ", 9 },
3768
3769 { "HRTIMER_NORESTART", 0 },
3770 { "HRTIMER_RESTART", 1 },
3771};
3772
Steven Rostedt7c27f782015-03-24 14:58:13 -04003773static long long eval_flag(const char *flag)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003774{
3775 int i;
3776
3777 /*
3778 * Some flags in the format files do not get converted.
3779 * If the flag is not numeric, see if it is something that
3780 * we already know about.
3781 */
3782 if (isdigit(flag[0]))
3783 return strtoull(flag, NULL, 0);
3784
3785 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3786 if (strcmp(flags[i].name, flag) == 0)
3787 return flags[i].value;
3788
Steven Rostedt7c27f782015-03-24 14:58:13 -04003789 return -1LL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003790}
3791
3792static void print_str_to_seq(struct trace_seq *s, const char *format,
3793 int len_arg, const char *str)
3794{
3795 if (len_arg >= 0)
3796 trace_seq_printf(s, format, len_arg, str);
3797 else
3798 trace_seq_printf(s, format, str);
3799}
3800
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003801static void print_bitmask_to_seq(struct pevent *pevent,
3802 struct trace_seq *s, const char *format,
3803 int len_arg, const void *data, int size)
3804{
3805 int nr_bits = size * 8;
3806 int str_size = (nr_bits + 3) / 4;
3807 int len = 0;
3808 char buf[3];
3809 char *str;
3810 int index;
3811 int i;
3812
3813 /*
3814 * The kernel likes to put in commas every 32 bits, we
3815 * can do the same.
3816 */
3817 str_size += (nr_bits - 1) / 32;
3818
3819 str = malloc(str_size + 1);
3820 if (!str) {
3821 do_warning("%s: not enough memory!", __func__);
3822 return;
3823 }
3824 str[str_size] = 0;
3825
3826 /* Start out with -2 for the two chars per byte */
3827 for (i = str_size - 2; i >= 0; i -= 2) {
3828 /*
3829 * data points to a bit mask of size bytes.
3830 * In the kernel, this is an array of long words, thus
3831 * endianess is very important.
3832 */
3833 if (pevent->file_bigendian)
3834 index = size - (len + 1);
3835 else
3836 index = len;
3837
3838 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3839 memcpy(str + i, buf, 2);
3840 len++;
3841 if (!(len & 3) && i > 0) {
3842 i--;
3843 str[i] = ',';
3844 }
3845 }
3846
3847 if (len_arg >= 0)
3848 trace_seq_printf(s, format, len_arg, str);
3849 else
3850 trace_seq_printf(s, format, str);
3851
3852 free(str);
3853}
3854
Steven Rostedtf7d82352012-04-06 00:47:53 +02003855static void print_str_arg(struct trace_seq *s, void *data, int size,
3856 struct event_format *event, const char *format,
3857 int len_arg, struct print_arg *arg)
3858{
3859 struct pevent *pevent = event->pevent;
3860 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003861 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003862 struct printk_map *printk;
Steven Rostedt7c27f782015-03-24 14:58:13 -04003863 long long val, fval;
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003864 unsigned long long addr;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003865 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003866 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003867 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003868 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003869
3870 switch (arg->type) {
3871 case PRINT_NULL:
3872 /* ?? */
3873 return;
3874 case PRINT_ATOM:
3875 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3876 return;
3877 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003878 field = arg->field.field;
3879 if (!field) {
3880 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003881 if (!field) {
3882 str = arg->field.name;
3883 goto out_warning_field;
3884 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003885 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003886 }
3887 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003888 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003889
3890 /*
3891 * Some events pass in pointers. If this is not an array
3892 * and the size is the same as long_size, assume that it
3893 * is a pointer.
3894 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003895 if (!(field->flags & FIELD_IS_ARRAY) &&
3896 field->size == pevent->long_size) {
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003897
3898 /* Handle heterogeneous recording and processing
3899 * architectures
3900 *
3901 * CASE I:
3902 * Traces recorded on 32-bit devices (32-bit
3903 * addressing) and processed on 64-bit devices:
3904 * In this case, only 32 bits should be read.
3905 *
3906 * CASE II:
3907 * Traces recorded on 64 bit devices and processed
3908 * on 32-bit devices:
3909 * In this case, 64 bits must be read.
3910 */
3911 addr = (pevent->long_size == 8) ?
3912 *(unsigned long long *)(data + field->offset) :
3913 (unsigned long long)*(unsigned int *)(data + field->offset);
3914
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003915 /* Check if it matches a print format */
3916 printk = find_printk(pevent, addr);
3917 if (printk)
3918 trace_seq_puts(s, printk->printk);
3919 else
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003920 trace_seq_printf(s, "%llx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003921 break;
3922 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003923 str = malloc(len + 1);
3924 if (!str) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003925 do_warning_event(event, "%s: not enough memory!",
3926 __func__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003927 return;
3928 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003929 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003930 str[len] = 0;
3931 print_str_to_seq(s, format, len_arg, str);
3932 free(str);
3933 break;
3934 case PRINT_FLAGS:
3935 val = eval_num_arg(data, size, event, arg->flags.field);
3936 print = 0;
3937 for (flag = arg->flags.flags; flag; flag = flag->next) {
3938 fval = eval_flag(flag->value);
Steven Rostedt7c27f782015-03-24 14:58:13 -04003939 if (!val && fval < 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003940 print_str_to_seq(s, format, len_arg, flag->str);
3941 break;
3942 }
Steven Rostedt7c27f782015-03-24 14:58:13 -04003943 if (fval > 0 && (val & fval) == fval) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003944 if (print && arg->flags.delim)
3945 trace_seq_puts(s, arg->flags.delim);
3946 print_str_to_seq(s, format, len_arg, flag->str);
3947 print = 1;
3948 val &= ~fval;
3949 }
3950 }
3951 break;
3952 case PRINT_SYMBOL:
3953 val = eval_num_arg(data, size, event, arg->symbol.field);
3954 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3955 fval = eval_flag(flag->value);
3956 if (val == fval) {
3957 print_str_to_seq(s, format, len_arg, flag->str);
3958 break;
3959 }
3960 }
3961 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003962 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003963 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3964 unsigned long offset;
3965 offset = pevent_read_number(pevent,
3966 data + arg->hex.field->dynarray.field->offset,
3967 arg->hex.field->dynarray.field->size);
3968 hex = data + (offset & 0xffff);
3969 } else {
3970 field = arg->hex.field->field.field;
3971 if (!field) {
3972 str = arg->hex.field->field.name;
3973 field = pevent_find_any_field(event, str);
3974 if (!field)
3975 goto out_warning_field;
3976 arg->hex.field->field.field = field;
3977 }
3978 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003979 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003980 len = eval_num_arg(data, size, event, arg->hex.size);
3981 for (i = 0; i < len; i++) {
3982 if (i)
3983 trace_seq_putc(s, ' ');
3984 trace_seq_printf(s, "%02x", hex[i]);
3985 }
3986 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003987
Javi Merinob839e1e82015-03-24 11:07:19 +00003988 case PRINT_INT_ARRAY: {
3989 void *num;
3990 int el_size;
3991
3992 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3993 unsigned long offset;
3994 struct format_field *field =
3995 arg->int_array.field->dynarray.field;
3996 offset = pevent_read_number(pevent,
3997 data + field->offset,
3998 field->size);
3999 num = data + (offset & 0xffff);
4000 } else {
4001 field = arg->int_array.field->field.field;
4002 if (!field) {
4003 str = arg->int_array.field->field.name;
4004 field = pevent_find_any_field(event, str);
4005 if (!field)
4006 goto out_warning_field;
4007 arg->int_array.field->field.field = field;
4008 }
4009 num = data + field->offset;
4010 }
4011 len = eval_num_arg(data, size, event, arg->int_array.count);
4012 el_size = eval_num_arg(data, size, event,
4013 arg->int_array.el_size);
4014 for (i = 0; i < len; i++) {
4015 if (i)
4016 trace_seq_putc(s, ' ');
4017
4018 if (el_size == 1) {
4019 trace_seq_printf(s, "%u", *(uint8_t *)num);
4020 } else if (el_size == 2) {
4021 trace_seq_printf(s, "%u", *(uint16_t *)num);
4022 } else if (el_size == 4) {
4023 trace_seq_printf(s, "%u", *(uint32_t *)num);
4024 } else if (el_size == 8) {
Namhyung Kim410ceb82015-04-24 10:45:16 +09004025 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
Javi Merinob839e1e82015-03-24 11:07:19 +00004026 } else {
4027 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4028 el_size, *(uint8_t *)num);
4029 el_size = 1;
4030 }
4031
4032 num += el_size;
4033 }
4034 break;
4035 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004036 case PRINT_TYPE:
4037 break;
4038 case PRINT_STRING: {
4039 int str_offset;
4040
4041 if (arg->string.offset == -1) {
4042 struct format_field *f;
4043
4044 f = pevent_find_any_field(event, arg->string.string);
4045 arg->string.offset = f->offset;
4046 }
4047 str_offset = data2host4(pevent, data + arg->string.offset);
4048 str_offset &= 0xffff;
4049 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4050 break;
4051 }
4052 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004053 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004054 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04004055 case PRINT_BITMASK: {
4056 int bitmask_offset;
4057 int bitmask_size;
4058
4059 if (arg->bitmask.offset == -1) {
4060 struct format_field *f;
4061
4062 f = pevent_find_any_field(event, arg->bitmask.bitmask);
4063 arg->bitmask.offset = f->offset;
4064 }
4065 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4066 bitmask_size = bitmask_offset >> 16;
4067 bitmask_offset &= 0xffff;
4068 print_bitmask_to_seq(pevent, s, format, len_arg,
4069 data + bitmask_offset, bitmask_size);
4070 break;
4071 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004072 case PRINT_OP:
4073 /*
4074 * The only op for string should be ? :
4075 */
4076 if (arg->op.op[0] != '?')
4077 return;
4078 val = eval_num_arg(data, size, event, arg->op.left);
4079 if (val)
4080 print_str_arg(s, data, size, event,
4081 format, len_arg, arg->op.right->op.left);
4082 else
4083 print_str_arg(s, data, size, event,
4084 format, len_arg, arg->op.right->op.right);
4085 break;
4086 case PRINT_FUNC:
4087 process_defined_func(s, data, size, event, arg);
4088 break;
4089 default:
4090 /* well... */
4091 break;
4092 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004093
4094 return;
4095
4096out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09004097 do_warning_event(event, "%s: field %s not found",
4098 __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004099}
4100
4101static unsigned long long
4102process_defined_func(struct trace_seq *s, void *data, int size,
4103 struct event_format *event, struct print_arg *arg)
4104{
4105 struct pevent_function_handler *func_handle = arg->func.func;
4106 struct pevent_func_params *param;
4107 unsigned long long *args;
4108 unsigned long long ret;
4109 struct print_arg *farg;
4110 struct trace_seq str;
4111 struct save_str {
4112 struct save_str *next;
4113 char *str;
4114 } *strings = NULL, *string;
4115 int i;
4116
4117 if (!func_handle->nr_args) {
4118 ret = (*func_handle->func)(s, NULL);
4119 goto out;
4120 }
4121
4122 farg = arg->func.args;
4123 param = func_handle->params;
4124
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004125 ret = ULLONG_MAX;
4126 args = malloc(sizeof(*args) * func_handle->nr_args);
4127 if (!args)
4128 goto out;
4129
Steven Rostedtf7d82352012-04-06 00:47:53 +02004130 for (i = 0; i < func_handle->nr_args; i++) {
4131 switch (param->type) {
4132 case PEVENT_FUNC_ARG_INT:
4133 case PEVENT_FUNC_ARG_LONG:
4134 case PEVENT_FUNC_ARG_PTR:
4135 args[i] = eval_num_arg(data, size, event, farg);
4136 break;
4137 case PEVENT_FUNC_ARG_STRING:
4138 trace_seq_init(&str);
4139 print_str_arg(&str, data, size, event, "%s", -1, farg);
4140 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004141 string = malloc(sizeof(*string));
4142 if (!string) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004143 do_warning_event(event, "%s(%d): malloc str",
4144 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004145 goto out_free;
4146 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004147 string->next = strings;
4148 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004149 if (!string->str) {
4150 free(string);
Namhyung Kim3388cc32014-03-19 10:22:53 +09004151 do_warning_event(event, "%s(%d): malloc str",
4152 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004153 goto out_free;
4154 }
Robert Richter0cf26012012-08-07 19:43:14 +02004155 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004156 strings = string;
4157 trace_seq_destroy(&str);
4158 break;
4159 default:
4160 /*
4161 * Something went totally wrong, this is not
4162 * an input error, something in this code broke.
4163 */
Namhyung Kim3388cc32014-03-19 10:22:53 +09004164 do_warning_event(event, "Unexpected end of arguments\n");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004165 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004166 }
4167 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09004168 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004169 }
4170
4171 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004172out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004173 free(args);
4174 while (strings) {
4175 string = strings;
4176 strings = string->next;
4177 free(string->str);
4178 free(string);
4179 }
4180
4181 out:
4182 /* TBD : handle return type here */
4183 return ret;
4184}
4185
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004186static void free_args(struct print_arg *args)
4187{
4188 struct print_arg *next;
4189
4190 while (args) {
4191 next = args->next;
4192
4193 free_arg(args);
4194 args = next;
4195 }
4196}
4197
Steven Rostedtf7d82352012-04-06 00:47:53 +02004198static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4199{
4200 struct pevent *pevent = event->pevent;
4201 struct format_field *field, *ip_field;
4202 struct print_arg *args, *arg, **next;
4203 unsigned long long ip, val;
4204 char *ptr;
4205 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004206 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004207
4208 field = pevent->bprint_buf_field;
4209 ip_field = pevent->bprint_ip_field;
4210
4211 if (!field) {
4212 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004213 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004214 do_warning_event(event, "can't find buffer field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004215 return NULL;
4216 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004217 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004218 if (!ip_field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004219 do_warning_event(event, "can't find ip field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004220 return NULL;
4221 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004222 pevent->bprint_buf_field = field;
4223 pevent->bprint_ip_field = ip_field;
4224 }
4225
4226 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4227
4228 /*
4229 * The first arg is the IP pointer.
4230 */
4231 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004232 if (!args) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004233 do_warning_event(event, "%s(%d): not enough memory!",
4234 __func__, __LINE__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004235 return NULL;
4236 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004237 arg = args;
4238 arg->next = NULL;
4239 next = &arg->next;
4240
4241 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004242
4243 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4244 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004245
Scott Woodbbedb172015-03-11 22:13:57 -05004246 /* skip the first "%ps: " */
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004247 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004248 bptr < data + size && *ptr; ptr++) {
4249 int ls = 0;
4250
4251 if (*ptr == '%') {
4252 process_again:
4253 ptr++;
4254 switch (*ptr) {
4255 case '%':
4256 break;
4257 case 'l':
4258 ls++;
4259 goto process_again;
4260 case 'L':
4261 ls = 2;
4262 goto process_again;
4263 case '0' ... '9':
4264 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004265 case '.':
4266 goto process_again;
Steven Rostedt (Red Hat)55426292015-03-24 09:57:51 -04004267 case 'z':
4268 case 'Z':
4269 ls = 1;
4270 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004271 case 'p':
4272 ls = 1;
4273 /* fall through */
4274 case 'd':
4275 case 'u':
4276 case 'x':
4277 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004278 switch (ls) {
4279 case 0:
4280 vsize = 4;
4281 break;
4282 case 1:
4283 vsize = pevent->long_size;
4284 break;
4285 case 2:
4286 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02004287 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004288 default:
4289 vsize = ls; /* ? */
4290 break;
4291 }
4292 /* fall through */
4293 case '*':
4294 if (*ptr == '*')
4295 vsize = 4;
4296
Steven Rostedtf7d82352012-04-06 00:47:53 +02004297 /* the pointers are always 4 bytes aligned */
4298 bptr = (void *)(((unsigned long)bptr + 3) &
4299 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004300 val = pevent_read_number(pevent, bptr, vsize);
4301 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004302 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004303 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004304 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004305 __func__, __LINE__);
4306 goto out_free;
4307 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004308 arg->next = NULL;
4309 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004310 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4311 free(arg);
4312 goto out_free;
4313 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004314 *next = arg;
4315 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004316 /*
4317 * The '*' case means that an arg is used as the length.
4318 * We need to continue to figure out for what.
4319 */
4320 if (*ptr == '*')
4321 goto process_again;
4322
Steven Rostedtf7d82352012-04-06 00:47:53 +02004323 break;
4324 case 's':
4325 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004326 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004327 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004328 __func__, __LINE__);
4329 goto out_free;
4330 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004331 arg->next = NULL;
4332 arg->type = PRINT_BSTRING;
4333 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09004334 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004335 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004336 bptr += strlen(bptr) + 1;
4337 *next = arg;
4338 next = &arg->next;
4339 default:
4340 break;
4341 }
4342 }
4343 }
4344
4345 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004346
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004347out_free:
4348 free_args(args);
4349 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004350}
4351
4352static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004353get_bprint_format(void *data, int size __maybe_unused,
4354 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004355{
4356 struct pevent *pevent = event->pevent;
4357 unsigned long long addr;
4358 struct format_field *field;
4359 struct printk_map *printk;
4360 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004361
4362 field = pevent->bprint_fmt_field;
4363
4364 if (!field) {
4365 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004366 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004367 do_warning_event(event, "can't find format field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004368 return NULL;
4369 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004370 pevent->bprint_fmt_field = field;
4371 }
4372
4373 addr = pevent_read_number(pevent, data + field->offset, field->size);
4374
4375 printk = find_printk(pevent, addr);
4376 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004377 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004378 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004379 return format;
4380 }
4381
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004382 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004383 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004384
4385 return format;
4386}
4387
4388static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4389 struct event_format *event, struct print_arg *arg)
4390{
4391 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004392 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02004393
4394 if (arg->type == PRINT_FUNC) {
4395 process_defined_func(s, data, size, event, arg);
4396 return;
4397 }
4398
4399 if (arg->type != PRINT_FIELD) {
4400 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4401 arg->type);
4402 return;
4403 }
4404
4405 if (mac == 'm')
4406 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4407 if (!arg->field.field) {
4408 arg->field.field =
4409 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004410 if (!arg->field.field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004411 do_warning_event(event, "%s: field %s not found",
4412 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004413 return;
4414 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004415 }
4416 if (arg->field.field->size != 6) {
4417 trace_seq_printf(s, "INVALIDMAC");
4418 return;
4419 }
4420 buf = data + arg->field.field->offset;
4421 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4422}
4423
David Ahern3d199b52014-12-18 19:11:11 -07004424static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4425{
4426 const char *fmt;
4427
4428 if (i == 'i')
4429 fmt = "%03d.%03d.%03d.%03d";
4430 else
4431 fmt = "%d.%d.%d.%d";
4432
4433 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4434}
4435
4436static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4437{
4438 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4439 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4440}
4441
4442static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4443{
4444 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4445}
4446
4447static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4448{
4449 int i, j, range;
4450 unsigned char zerolength[8];
4451 int longest = 1;
4452 int colonpos = -1;
4453 uint16_t word;
4454 uint8_t hi, lo;
4455 bool needcolon = false;
4456 bool useIPv4;
4457 struct in6_addr in6;
4458
4459 memcpy(&in6, addr, sizeof(struct in6_addr));
4460
4461 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4462
4463 memset(zerolength, 0, sizeof(zerolength));
4464
4465 if (useIPv4)
4466 range = 6;
4467 else
4468 range = 8;
4469
4470 /* find position of longest 0 run */
4471 for (i = 0; i < range; i++) {
4472 for (j = i; j < range; j++) {
4473 if (in6.s6_addr16[j] != 0)
4474 break;
4475 zerolength[i]++;
4476 }
4477 }
4478 for (i = 0; i < range; i++) {
4479 if (zerolength[i] > longest) {
4480 longest = zerolength[i];
4481 colonpos = i;
4482 }
4483 }
4484 if (longest == 1) /* don't compress a single 0 */
4485 colonpos = -1;
4486
4487 /* emit address */
4488 for (i = 0; i < range; i++) {
4489 if (i == colonpos) {
4490 if (needcolon || i == 0)
4491 trace_seq_printf(s, ":");
4492 trace_seq_printf(s, ":");
4493 needcolon = false;
4494 i += longest - 1;
4495 continue;
4496 }
4497 if (needcolon) {
4498 trace_seq_printf(s, ":");
4499 needcolon = false;
4500 }
4501 /* hex u16 without leading 0s */
4502 word = ntohs(in6.s6_addr16[i]);
4503 hi = word >> 8;
4504 lo = word & 0xff;
4505 if (hi)
4506 trace_seq_printf(s, "%x%02x", hi, lo);
4507 else
4508 trace_seq_printf(s, "%x", lo);
4509
4510 needcolon = true;
4511 }
4512
4513 if (useIPv4) {
4514 if (needcolon)
4515 trace_seq_printf(s, ":");
4516 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4517 }
4518
4519 return;
4520}
4521
4522static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4523{
4524 int j;
4525
4526 for (j = 0; j < 16; j += 2) {
4527 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4528 if (i == 'I' && j < 14)
4529 trace_seq_printf(s, ":");
4530 }
4531}
4532
4533/*
4534 * %pi4 print an IPv4 address with leading zeros
4535 * %pI4 print an IPv4 address without leading zeros
4536 * %pi6 print an IPv6 address without colons
4537 * %pI6 print an IPv6 address with colons
4538 * %pI6c print an IPv6 address in compressed form with colons
4539 * %pISpc print an IP address based on sockaddr; p adds port.
4540 */
4541static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4542 void *data, int size, struct event_format *event,
4543 struct print_arg *arg)
4544{
4545 unsigned char *buf;
4546
4547 if (arg->type == PRINT_FUNC) {
4548 process_defined_func(s, data, size, event, arg);
4549 return 0;
4550 }
4551
4552 if (arg->type != PRINT_FIELD) {
4553 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4554 return 0;
4555 }
4556
4557 if (!arg->field.field) {
4558 arg->field.field =
4559 pevent_find_any_field(event, arg->field.name);
4560 if (!arg->field.field) {
4561 do_warning("%s: field %s not found",
4562 __func__, arg->field.name);
4563 return 0;
4564 }
4565 }
4566
4567 buf = data + arg->field.field->offset;
4568
4569 if (arg->field.field->size != 4) {
4570 trace_seq_printf(s, "INVALIDIPv4");
4571 return 0;
4572 }
4573 print_ip4_addr(s, i, buf);
4574
4575 return 0;
4576}
4577
4578static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4579 void *data, int size, struct event_format *event,
4580 struct print_arg *arg)
4581{
4582 char have_c = 0;
4583 unsigned char *buf;
4584 int rc = 0;
4585
4586 /* pI6c */
4587 if (i == 'I' && *ptr == 'c') {
4588 have_c = 1;
4589 ptr++;
4590 rc++;
4591 }
4592
4593 if (arg->type == PRINT_FUNC) {
4594 process_defined_func(s, data, size, event, arg);
4595 return rc;
4596 }
4597
4598 if (arg->type != PRINT_FIELD) {
4599 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4600 return rc;
4601 }
4602
4603 if (!arg->field.field) {
4604 arg->field.field =
4605 pevent_find_any_field(event, arg->field.name);
4606 if (!arg->field.field) {
4607 do_warning("%s: field %s not found",
4608 __func__, arg->field.name);
4609 return rc;
4610 }
4611 }
4612
4613 buf = data + arg->field.field->offset;
4614
4615 if (arg->field.field->size != 16) {
4616 trace_seq_printf(s, "INVALIDIPv6");
4617 return rc;
4618 }
4619
4620 if (have_c)
4621 print_ip6c_addr(s, buf);
4622 else
4623 print_ip6_addr(s, i, buf);
4624
4625 return rc;
4626}
4627
4628static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4629 void *data, int size, struct event_format *event,
4630 struct print_arg *arg)
4631{
4632 char have_c = 0, have_p = 0;
4633 unsigned char *buf;
4634 struct sockaddr_storage *sa;
4635 int rc = 0;
4636
4637 /* pISpc */
4638 if (i == 'I') {
4639 if (*ptr == 'p') {
4640 have_p = 1;
4641 ptr++;
4642 rc++;
4643 }
4644 if (*ptr == 'c') {
4645 have_c = 1;
4646 ptr++;
4647 rc++;
4648 }
4649 }
4650
4651 if (arg->type == PRINT_FUNC) {
4652 process_defined_func(s, data, size, event, arg);
4653 return rc;
4654 }
4655
4656 if (arg->type != PRINT_FIELD) {
4657 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4658 return rc;
4659 }
4660
4661 if (!arg->field.field) {
4662 arg->field.field =
4663 pevent_find_any_field(event, arg->field.name);
4664 if (!arg->field.field) {
4665 do_warning("%s: field %s not found",
4666 __func__, arg->field.name);
4667 return rc;
4668 }
4669 }
4670
4671 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4672
4673 if (sa->ss_family == AF_INET) {
4674 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4675
4676 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4677 trace_seq_printf(s, "INVALIDIPv4");
4678 return rc;
4679 }
4680
4681 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4682 if (have_p)
4683 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4684
4685
4686 } else if (sa->ss_family == AF_INET6) {
4687 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4688
4689 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4690 trace_seq_printf(s, "INVALIDIPv6");
4691 return rc;
4692 }
4693
4694 if (have_p)
4695 trace_seq_printf(s, "[");
4696
4697 buf = (unsigned char *) &sa6->sin6_addr;
4698 if (have_c)
4699 print_ip6c_addr(s, buf);
4700 else
4701 print_ip6_addr(s, i, buf);
4702
4703 if (have_p)
4704 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4705 }
4706
4707 return rc;
4708}
4709
4710static int print_ip_arg(struct trace_seq *s, const char *ptr,
4711 void *data, int size, struct event_format *event,
4712 struct print_arg *arg)
4713{
4714 char i = *ptr; /* 'i' or 'I' */
4715 char ver;
4716 int rc = 0;
4717
4718 ptr++;
4719 rc++;
4720
4721 ver = *ptr;
4722 ptr++;
4723 rc++;
4724
4725 switch (ver) {
4726 case '4':
4727 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4728 break;
4729 case '6':
4730 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4731 break;
4732 case 'S':
4733 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4734 break;
4735 default:
4736 return 0;
4737 }
4738
4739 return rc;
4740}
4741
Namhyung Kim600da3c2012-06-22 17:10:15 +09004742static int is_printable_array(char *p, unsigned int len)
4743{
4744 unsigned int i;
4745
4746 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004747 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004748 return 0;
4749 return 1;
4750}
4751
Namhyung Kimbe45d402015-12-23 22:08:41 +09004752void pevent_print_field(struct trace_seq *s, void *data,
4753 struct format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004754{
Steven Rostedtf7d82352012-04-06 00:47:53 +02004755 unsigned long long val;
4756 unsigned int offset, len, i;
Namhyung Kimbe45d402015-12-23 22:08:41 +09004757 struct pevent *pevent = field->event->pevent;
4758
4759 if (field->flags & FIELD_IS_ARRAY) {
4760 offset = field->offset;
4761 len = field->size;
4762 if (field->flags & FIELD_IS_DYNAMIC) {
4763 val = pevent_read_number(pevent, data + offset, len);
4764 offset = val;
4765 len = offset >> 16;
4766 offset &= 0xffff;
4767 }
4768 if (field->flags & FIELD_IS_STRING &&
4769 is_printable_array(data + offset, len)) {
4770 trace_seq_printf(s, "%s", (char *)data + offset);
4771 } else {
4772 trace_seq_puts(s, "ARRAY[");
4773 for (i = 0; i < len; i++) {
4774 if (i)
4775 trace_seq_puts(s, ", ");
4776 trace_seq_printf(s, "%02x",
4777 *((unsigned char *)data + offset + i));
4778 }
4779 trace_seq_putc(s, ']');
4780 field->flags &= ~FIELD_IS_STRING;
4781 }
4782 } else {
4783 val = pevent_read_number(pevent, data + field->offset,
4784 field->size);
4785 if (field->flags & FIELD_IS_POINTER) {
4786 trace_seq_printf(s, "0x%llx", val);
4787 } else if (field->flags & FIELD_IS_SIGNED) {
4788 switch (field->size) {
4789 case 4:
4790 /*
4791 * If field is long then print it in hex.
4792 * A long usually stores pointers.
4793 */
4794 if (field->flags & FIELD_IS_LONG)
4795 trace_seq_printf(s, "0x%x", (int)val);
4796 else
4797 trace_seq_printf(s, "%d", (int)val);
4798 break;
4799 case 2:
4800 trace_seq_printf(s, "%2d", (short)val);
4801 break;
4802 case 1:
4803 trace_seq_printf(s, "%1d", (char)val);
4804 break;
4805 default:
4806 trace_seq_printf(s, "%lld", val);
4807 }
4808 } else {
4809 if (field->flags & FIELD_IS_LONG)
4810 trace_seq_printf(s, "0x%llx", val);
4811 else
4812 trace_seq_printf(s, "%llu", val);
4813 }
4814 }
4815}
4816
4817void pevent_print_fields(struct trace_seq *s, void *data,
4818 int size __maybe_unused, struct event_format *event)
4819{
4820 struct format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004821
4822 field = event->format.fields;
4823 while (field) {
4824 trace_seq_printf(s, " %s=", field->name);
Namhyung Kimbe45d402015-12-23 22:08:41 +09004825 pevent_print_field(s, data, field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004826 field = field->next;
4827 }
4828}
4829
4830static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4831{
4832 struct pevent *pevent = event->pevent;
4833 struct print_fmt *print_fmt = &event->print_fmt;
4834 struct print_arg *arg = print_fmt->args;
4835 struct print_arg *args = NULL;
4836 const char *ptr = print_fmt->format;
4837 unsigned long long val;
4838 struct func_map *func;
4839 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004840 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004841 char *bprint_fmt = NULL;
4842 char format[32];
4843 int show_func;
4844 int len_as_arg;
4845 int len_arg;
4846 int len;
4847 int ls;
4848
4849 if (event->flags & EVENT_FL_FAILED) {
4850 trace_seq_printf(s, "[FAILED TO PARSE]");
Namhyung Kimbe45d402015-12-23 22:08:41 +09004851 pevent_print_fields(s, data, size, event);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004852 return;
4853 }
4854
4855 if (event->flags & EVENT_FL_ISBPRINT) {
4856 bprint_fmt = get_bprint_format(data, size, event);
4857 args = make_bprint_args(bprint_fmt, data, size, event);
4858 arg = args;
4859 ptr = bprint_fmt;
4860 }
4861
4862 for (; *ptr; ptr++) {
4863 ls = 0;
4864 if (*ptr == '\\') {
4865 ptr++;
4866 switch (*ptr) {
4867 case 'n':
4868 trace_seq_putc(s, '\n');
4869 break;
4870 case 't':
4871 trace_seq_putc(s, '\t');
4872 break;
4873 case 'r':
4874 trace_seq_putc(s, '\r');
4875 break;
4876 case '\\':
4877 trace_seq_putc(s, '\\');
4878 break;
4879 default:
4880 trace_seq_putc(s, *ptr);
4881 break;
4882 }
4883
4884 } else if (*ptr == '%') {
4885 saveptr = ptr;
4886 show_func = 0;
4887 len_as_arg = 0;
4888 cont_process:
4889 ptr++;
4890 switch (*ptr) {
4891 case '%':
4892 trace_seq_putc(s, '%');
4893 break;
4894 case '#':
4895 /* FIXME: need to handle properly */
4896 goto cont_process;
4897 case 'h':
4898 ls--;
4899 goto cont_process;
4900 case 'l':
4901 ls++;
4902 goto cont_process;
4903 case 'L':
4904 ls = 2;
4905 goto cont_process;
4906 case '*':
4907 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004908 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004909 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004910 event->flags |= EVENT_FL_FAILED;
4911 goto out_failed;
4912 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004913 len_arg = eval_num_arg(data, size, event, arg);
4914 len_as_arg = 1;
4915 arg = arg->next;
4916 goto cont_process;
4917 case '.':
4918 case 'z':
4919 case 'Z':
4920 case '0' ... '9':
Steven Rostedt1d945012015-08-27 09:46:01 -04004921 case '-':
Steven Rostedtf7d82352012-04-06 00:47:53 +02004922 goto cont_process;
4923 case 'p':
4924 if (pevent->long_size == 4)
4925 ls = 1;
4926 else
4927 ls = 2;
4928
Scott Woodb6bd9c72015-08-31 16:16:37 -05004929 if (*(ptr+1) == 'F' || *(ptr+1) == 'f' ||
4930 *(ptr+1) == 'S' || *(ptr+1) == 's') {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004931 ptr++;
4932 show_func = *ptr;
4933 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4934 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4935 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004936 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004937 break;
David Ahern3d199b52014-12-18 19:11:11 -07004938 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4939 int n;
4940
4941 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4942 if (n > 0) {
4943 ptr += n;
4944 arg = arg->next;
4945 break;
4946 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004947 }
4948
4949 /* fall through */
4950 case 'd':
4951 case 'i':
4952 case 'x':
4953 case 'X':
4954 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004955 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004956 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004957 event->flags |= EVENT_FL_FAILED;
4958 goto out_failed;
4959 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004960
4961 len = ((unsigned long)ptr + 1) -
4962 (unsigned long)saveptr;
4963
4964 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004965 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004966 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004967 event->flags |= EVENT_FL_FAILED;
4968 len = 31;
4969 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004970
4971 memcpy(format, saveptr, len);
4972 format[len] = 0;
4973
4974 val = eval_num_arg(data, size, event, arg);
4975 arg = arg->next;
4976
4977 if (show_func) {
4978 func = find_func(pevent, val);
4979 if (func) {
4980 trace_seq_puts(s, func->func);
4981 if (show_func == 'F')
4982 trace_seq_printf(s,
4983 "+0x%llx",
4984 val - func->addr);
4985 break;
4986 }
4987 }
Steven Rostedt (Red Hat)a66673a2016-02-09 15:40:17 -05004988 if (pevent->long_size == 8 && ls == 1 &&
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004989 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004990 char *p;
4991
Steven Rostedtf7d82352012-04-06 00:47:53 +02004992 /* make %l into %ll */
Steven Rostedt32abc2e2015-11-16 17:25:16 -05004993 if (ls == 1 && (p = strchr(format, 'l')))
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004994 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004995 else if (strcmp(format, "%p") == 0)
4996 strcpy(format, "0x%llx");
Steven Rostedt32abc2e2015-11-16 17:25:16 -05004997 ls = 2;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004998 }
4999 switch (ls) {
5000 case -2:
5001 if (len_as_arg)
5002 trace_seq_printf(s, format, len_arg, (char)val);
5003 else
5004 trace_seq_printf(s, format, (char)val);
5005 break;
5006 case -1:
5007 if (len_as_arg)
5008 trace_seq_printf(s, format, len_arg, (short)val);
5009 else
5010 trace_seq_printf(s, format, (short)val);
5011 break;
5012 case 0:
5013 if (len_as_arg)
5014 trace_seq_printf(s, format, len_arg, (int)val);
5015 else
5016 trace_seq_printf(s, format, (int)val);
5017 break;
5018 case 1:
5019 if (len_as_arg)
5020 trace_seq_printf(s, format, len_arg, (long)val);
5021 else
5022 trace_seq_printf(s, format, (long)val);
5023 break;
5024 case 2:
5025 if (len_as_arg)
5026 trace_seq_printf(s, format, len_arg,
5027 (long long)val);
5028 else
5029 trace_seq_printf(s, format, (long long)val);
5030 break;
5031 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09005032 do_warning_event(event, "bad count (%d)", ls);
Namhyung Kim245c5a12012-09-07 11:49:45 +09005033 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005034 }
5035 break;
5036 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09005037 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005038 do_warning_event(event, "no matching argument");
Namhyung Kim245c5a12012-09-07 11:49:45 +09005039 event->flags |= EVENT_FL_FAILED;
5040 goto out_failed;
5041 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005042
5043 len = ((unsigned long)ptr + 1) -
5044 (unsigned long)saveptr;
5045
5046 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09005047 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005048 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09005049 event->flags |= EVENT_FL_FAILED;
5050 len = 31;
5051 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005052
5053 memcpy(format, saveptr, len);
5054 format[len] = 0;
5055 if (!len_as_arg)
5056 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05005057 /* Use helper trace_seq */
5058 trace_seq_init(&p);
5059 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005060 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05005061 trace_seq_terminate(&p);
5062 trace_seq_puts(s, p.buffer);
Steven Rostedtde04f862014-04-22 19:23:30 -04005063 trace_seq_destroy(&p);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005064 arg = arg->next;
5065 break;
5066 default:
5067 trace_seq_printf(s, ">%c<", *ptr);
5068
5069 }
5070 } else
5071 trace_seq_putc(s, *ptr);
5072 }
5073
Namhyung Kim245c5a12012-09-07 11:49:45 +09005074 if (event->flags & EVENT_FL_FAILED) {
5075out_failed:
5076 trace_seq_printf(s, "[FAILED TO PARSE]");
5077 }
5078
Steven Rostedtf7d82352012-04-06 00:47:53 +02005079 if (args) {
5080 free_args(args);
5081 free(bprint_fmt);
5082 }
5083}
5084
5085/**
5086 * pevent_data_lat_fmt - parse the data for the latency format
5087 * @pevent: a handle to the pevent
5088 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005089 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02005090 *
5091 * This parses out the Latency format (interrupts disabled,
5092 * need rescheduling, in hard/soft interrupt, preempt count
5093 * and lock depth) and places it into the trace_seq.
5094 */
5095void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02005096 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005097{
5098 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09005099 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005100 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09005101 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005102 unsigned int lat_flags;
5103 unsigned int pc;
5104 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09005105 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005106 int hardirq;
5107 int softirq;
5108 void *data = record->data;
5109
5110 lat_flags = parse_common_flags(pevent, data);
5111 pc = parse_common_pc(pevent, data);
5112 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005113 if (lock_depth_exists)
5114 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09005115 else if (check_lock_depth) {
5116 lock_depth = parse_common_lock_depth(pevent, data);
5117 if (lock_depth < 0)
5118 check_lock_depth = 0;
5119 else
5120 lock_depth_exists = 1;
5121 }
5122
5123 /* migrate_disable may not always exist */
5124 if (migrate_disable_exists)
5125 migrate_disable = parse_common_migrate_disable(pevent, data);
5126 else if (check_migrate_disable) {
5127 migrate_disable = parse_common_migrate_disable(pevent, data);
5128 if (migrate_disable < 0)
5129 check_migrate_disable = 0;
5130 else
5131 migrate_disable_exists = 1;
5132 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005133
5134 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5135 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5136
5137 trace_seq_printf(s, "%c%c%c",
5138 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5139 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5140 'X' : '.',
5141 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5142 'N' : '.',
5143 (hardirq && softirq) ? 'H' :
5144 hardirq ? 'h' : softirq ? 's' : '.');
5145
5146 if (pc)
5147 trace_seq_printf(s, "%x", pc);
5148 else
5149 trace_seq_putc(s, '.');
5150
Steven Rostedt0866a972012-05-22 14:52:40 +09005151 if (migrate_disable_exists) {
5152 if (migrate_disable < 0)
5153 trace_seq_putc(s, '.');
5154 else
5155 trace_seq_printf(s, "%d", migrate_disable);
5156 }
5157
Steven Rostedtf7d82352012-04-06 00:47:53 +02005158 if (lock_depth_exists) {
5159 if (lock_depth < 0)
5160 trace_seq_putc(s, '.');
5161 else
5162 trace_seq_printf(s, "%d", lock_depth);
5163 }
5164
5165 trace_seq_terminate(s);
5166}
5167
5168/**
5169 * pevent_data_type - parse out the given event type
5170 * @pevent: a handle to the pevent
5171 * @rec: the record to read from
5172 *
5173 * This returns the event id from the @rec.
5174 */
Steven Rostedt1c698182012-04-06 00:48:06 +02005175int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005176{
5177 return trace_parse_common_type(pevent, rec->data);
5178}
5179
5180/**
5181 * pevent_data_event_from_type - find the event by a given type
5182 * @pevent: a handle to the pevent
5183 * @type: the type of the event.
5184 *
5185 * This returns the event form a given @type;
5186 */
5187struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5188{
5189 return pevent_find_event(pevent, type);
5190}
5191
5192/**
5193 * pevent_data_pid - parse the PID from raw data
5194 * @pevent: a handle to the pevent
5195 * @rec: the record to parse
5196 *
5197 * This returns the PID from a raw data.
5198 */
Steven Rostedt1c698182012-04-06 00:48:06 +02005199int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005200{
5201 return parse_common_pid(pevent, rec->data);
5202}
5203
5204/**
5205 * pevent_data_comm_from_pid - return the command line from PID
5206 * @pevent: a handle to the pevent
5207 * @pid: the PID of the task to search for
5208 *
5209 * This returns a pointer to the command line that has the given
5210 * @pid.
5211 */
5212const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5213{
5214 const char *comm;
5215
5216 comm = find_cmdline(pevent, pid);
5217 return comm;
5218}
5219
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005220static struct cmdline *
5221pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5222{
5223 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5224
5225 if (cmdlist)
5226 cmdlist = cmdlist->next;
5227 else
5228 cmdlist = pevent->cmdlist;
5229
5230 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5231 cmdlist = cmdlist->next;
5232
5233 return (struct cmdline *)cmdlist;
5234}
5235
5236/**
5237 * pevent_data_pid_from_comm - return the pid from a given comm
5238 * @pevent: a handle to the pevent
5239 * @comm: the cmdline to find the pid from
5240 * @next: the cmdline structure to find the next comm
5241 *
5242 * This returns the cmdline structure that holds a pid for a given
5243 * comm, or NULL if none found. As there may be more than one pid for
5244 * a given comm, the result of this call can be passed back into
5245 * a recurring call in the @next paramater, and then it will find the
5246 * next pid.
5247 * Also, it does a linear seach, so it may be slow.
5248 */
5249struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5250 struct cmdline *next)
5251{
5252 struct cmdline *cmdline;
5253
5254 /*
5255 * If the cmdlines have not been converted yet, then use
5256 * the list.
5257 */
5258 if (!pevent->cmdlines)
5259 return pid_from_cmdlist(pevent, comm, next);
5260
5261 if (next) {
5262 /*
5263 * The next pointer could have been still from
5264 * a previous call before cmdlines were created
5265 */
5266 if (next < pevent->cmdlines ||
5267 next >= pevent->cmdlines + pevent->cmdline_count)
5268 next = NULL;
5269 else
5270 cmdline = next++;
5271 }
5272
5273 if (!next)
5274 cmdline = pevent->cmdlines;
5275
5276 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5277 if (strcmp(cmdline->comm, comm) == 0)
5278 return cmdline;
5279 cmdline++;
5280 }
5281 return NULL;
5282}
5283
5284/**
5285 * pevent_cmdline_pid - return the pid associated to a given cmdline
5286 * @cmdline: The cmdline structure to get the pid from
5287 *
5288 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5289 * -1 is returned.
5290 */
5291int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5292{
5293 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5294
5295 if (!cmdline)
5296 return -1;
5297
5298 /*
5299 * If cmdlines have not been created yet, or cmdline is
5300 * not part of the array, then treat it as a cmdlist instead.
5301 */
5302 if (!pevent->cmdlines ||
5303 cmdline < pevent->cmdlines ||
5304 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5305 return cmdlist->pid;
5306
5307 return cmdline->pid;
5308}
5309
Steven Rostedtf7d82352012-04-06 00:47:53 +02005310/**
5311 * pevent_data_comm_from_pid - parse the data into the print format
5312 * @s: the trace_seq to write to
5313 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005314 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02005315 *
5316 * This parses the raw @data using the given @event information and
5317 * writes the print format into the trace_seq.
5318 */
5319void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005320 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005321{
5322 int print_pretty = 1;
5323
Steven Rostedtc6c2b962013-11-01 17:53:59 -04005324 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
Namhyung Kimbe45d402015-12-23 22:08:41 +09005325 pevent_print_fields(s, record->data, record->size, event);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005326 else {
5327
Steven Rostedtc6c2b962013-11-01 17:53:59 -04005328 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005329 print_pretty = event->handler(s, record, event,
5330 event->context);
5331
5332 if (print_pretty)
5333 pretty_print(s, record->data, record->size, event);
5334 }
5335
5336 trace_seq_terminate(s);
5337}
5338
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005339static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5340{
5341 if (!use_trace_clock)
5342 return true;
5343
5344 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5345 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5346 return true;
5347
5348 /* trace_clock is setting in tsc or counter mode */
5349 return false;
5350}
5351
Steven Rostedta6745332016-02-29 09:01:28 -05005352/**
5353 * pevent_find_event_by_record - return the event from a given record
5354 * @pevent: a handle to the pevent
5355 * @record: The record to get the event from
5356 *
5357 * Returns the associated event for a given record, or NULL if non is
5358 * is found.
5359 */
5360struct event_format *
5361pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005362{
Steven Rostedta6745332016-02-29 09:01:28 -05005363 int type;
5364
5365 if (record->size < 0) {
5366 do_warning("ug! negative record size %d", record->size);
5367 return NULL;
5368 }
5369
5370 type = trace_parse_common_type(pevent, record->data);
5371
5372 return pevent_find_event(pevent, type);
5373}
5374
5375/**
5376 * pevent_print_event_task - Write the event task comm, pid and CPU
5377 * @pevent: a handle to the pevent
5378 * @s: the trace_seq to write to
5379 * @event: the handle to the record's event
5380 * @record: The record to get the event from
5381 *
5382 * Writes the tasks comm, pid and CPU to @s.
5383 */
5384void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
5385 struct event_format *event,
5386 struct pevent_record *record)
5387{
5388 void *data = record->data;
5389 const char *comm;
5390 int pid;
5391
5392 pid = parse_common_pid(pevent, data);
5393 comm = find_cmdline(pevent, pid);
5394
5395 if (pevent->latency_format) {
5396 trace_seq_printf(s, "%8.8s-%-5d %3d",
5397 comm, pid, record->cpu);
5398 } else
5399 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5400}
5401
5402/**
5403 * pevent_print_event_time - Write the event timestamp
5404 * @pevent: a handle to the pevent
5405 * @s: the trace_seq to write to
5406 * @event: the handle to the record's event
5407 * @record: The record to get the event from
5408 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5409 *
5410 * Writes the timestamp of the record into @s.
5411 */
5412void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
5413 struct event_format *event,
5414 struct pevent_record *record,
5415 bool use_trace_clock)
5416{
Steven Rostedtf7d82352012-04-06 00:47:53 +02005417 unsigned long secs;
5418 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005419 unsigned long nsecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005420 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005421 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005422
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005423 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5424 use_trace_clock);
5425 if (use_usec_format) {
5426 secs = record->ts / NSECS_PER_SEC;
5427 nsecs = record->ts - secs * NSECS_PER_SEC;
5428 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005429
Steven Rostedtf7d82352012-04-06 00:47:53 +02005430 if (pevent->latency_format) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005431 pevent_data_lat_fmt(pevent, s, record);
Steven Rostedt4f3c88762016-03-23 10:16:28 -04005432 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005433
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005434 if (use_usec_format) {
5435 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5436 usecs = nsecs;
5437 p = 9;
5438 } else {
5439 usecs = (nsecs + 500) / NSECS_PER_USEC;
Chaos.Chen21a30102016-02-09 15:40:14 -05005440 /* To avoid usecs larger than 1 sec */
5441 if (usecs >= 1000000) {
5442 usecs -= 1000000;
5443 secs++;
5444 }
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005445 p = 6;
5446 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02005447
Steven Rostedta6745332016-02-29 09:01:28 -05005448 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005449 } else
Steven Rostedta6745332016-02-29 09:01:28 -05005450 trace_seq_printf(s, " %12llu:", record->ts);
5451}
5452
5453/**
5454 * pevent_print_event_data - Write the event data section
5455 * @pevent: a handle to the pevent
5456 * @s: the trace_seq to write to
5457 * @event: the handle to the record's event
5458 * @record: The record to get the event from
5459 *
5460 * Writes the parsing of the record's data to @s.
5461 */
5462void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
5463 struct event_format *event,
5464 struct pevent_record *record)
5465{
5466 static const char *spaces = " "; /* 20 spaces */
5467 int len;
5468
5469 trace_seq_printf(s, " %s: ", event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005470
5471 /* Space out the event names evenly. */
5472 len = strlen(event->name);
5473 if (len < 20)
5474 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5475
5476 pevent_event_info(s, event, record);
5477}
5478
Steven Rostedta6745332016-02-29 09:01:28 -05005479void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5480 struct pevent_record *record, bool use_trace_clock)
5481{
5482 struct event_format *event;
5483
5484 event = pevent_find_event_by_record(pevent, record);
5485 if (!event) {
5486 do_warning("ug! no event found for type %d",
5487 trace_parse_common_type(pevent, record->data));
5488 return;
5489 }
5490
5491 pevent_print_event_task(pevent, s, event, record);
5492 pevent_print_event_time(pevent, s, event, record, use_trace_clock);
5493 pevent_print_event_data(pevent, s, event, record);
5494}
5495
Steven Rostedtf7d82352012-04-06 00:47:53 +02005496static int events_id_cmp(const void *a, const void *b)
5497{
5498 struct event_format * const * ea = a;
5499 struct event_format * const * eb = b;
5500
5501 if ((*ea)->id < (*eb)->id)
5502 return -1;
5503
5504 if ((*ea)->id > (*eb)->id)
5505 return 1;
5506
5507 return 0;
5508}
5509
5510static int events_name_cmp(const void *a, const void *b)
5511{
5512 struct event_format * const * ea = a;
5513 struct event_format * const * eb = b;
5514 int res;
5515
5516 res = strcmp((*ea)->name, (*eb)->name);
5517 if (res)
5518 return res;
5519
5520 res = strcmp((*ea)->system, (*eb)->system);
5521 if (res)
5522 return res;
5523
5524 return events_id_cmp(a, b);
5525}
5526
5527static int events_system_cmp(const void *a, const void *b)
5528{
5529 struct event_format * const * ea = a;
5530 struct event_format * const * eb = b;
5531 int res;
5532
5533 res = strcmp((*ea)->system, (*eb)->system);
5534 if (res)
5535 return res;
5536
5537 res = strcmp((*ea)->name, (*eb)->name);
5538 if (res)
5539 return res;
5540
5541 return events_id_cmp(a, b);
5542}
5543
5544struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5545{
5546 struct event_format **events;
5547 int (*sort)(const void *a, const void *b);
5548
5549 events = pevent->sort_events;
5550
5551 if (events && pevent->last_type == sort_type)
5552 return events;
5553
5554 if (!events) {
5555 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5556 if (!events)
5557 return NULL;
5558
5559 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5560 events[pevent->nr_events] = NULL;
5561
5562 pevent->sort_events = events;
5563
5564 /* the internal events are sorted by id */
5565 if (sort_type == EVENT_SORT_ID) {
5566 pevent->last_type = sort_type;
5567 return events;
5568 }
5569 }
5570
5571 switch (sort_type) {
5572 case EVENT_SORT_ID:
5573 sort = events_id_cmp;
5574 break;
5575 case EVENT_SORT_NAME:
5576 sort = events_name_cmp;
5577 break;
5578 case EVENT_SORT_SYSTEM:
5579 sort = events_system_cmp;
5580 break;
5581 default:
5582 return events;
5583 }
5584
5585 qsort(events, pevent->nr_events, sizeof(*events), sort);
5586 pevent->last_type = sort_type;
5587
5588 return events;
5589}
5590
5591static struct format_field **
5592get_event_fields(const char *type, const char *name,
5593 int count, struct format_field *list)
5594{
5595 struct format_field **fields;
5596 struct format_field *field;
5597 int i = 0;
5598
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03005599 fields = malloc(sizeof(*fields) * (count + 1));
5600 if (!fields)
5601 return NULL;
5602
Steven Rostedtf7d82352012-04-06 00:47:53 +02005603 for (field = list; field; field = field->next) {
5604 fields[i++] = field;
5605 if (i == count + 1) {
5606 do_warning("event %s has more %s fields than specified",
5607 name, type);
5608 i--;
5609 break;
5610 }
5611 }
5612
5613 if (i != count)
5614 do_warning("event %s has less %s fields than specified",
5615 name, type);
5616
5617 fields[i] = NULL;
5618
5619 return fields;
5620}
5621
5622/**
5623 * pevent_event_common_fields - return a list of common fields for an event
5624 * @event: the event to return the common fields of.
5625 *
5626 * Returns an allocated array of fields. The last item in the array is NULL.
5627 * The array must be freed with free().
5628 */
5629struct format_field **pevent_event_common_fields(struct event_format *event)
5630{
5631 return get_event_fields("common", event->name,
5632 event->format.nr_common,
5633 event->format.common_fields);
5634}
5635
5636/**
5637 * pevent_event_fields - return a list of event specific fields for an event
5638 * @event: the event to return the fields of.
5639 *
5640 * Returns an allocated array of fields. The last item in the array is NULL.
5641 * The array must be freed with free().
5642 */
5643struct format_field **pevent_event_fields(struct event_format *event)
5644{
5645 return get_event_fields("event", event->name,
5646 event->format.nr_fields,
5647 event->format.fields);
5648}
5649
5650static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5651{
5652 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5653 if (field->next) {
5654 trace_seq_puts(s, ", ");
5655 print_fields(s, field->next);
5656 }
5657}
5658
5659/* for debugging */
5660static void print_args(struct print_arg *args)
5661{
5662 int print_paren = 1;
5663 struct trace_seq s;
5664
5665 switch (args->type) {
5666 case PRINT_NULL:
5667 printf("null");
5668 break;
5669 case PRINT_ATOM:
5670 printf("%s", args->atom.atom);
5671 break;
5672 case PRINT_FIELD:
5673 printf("REC->%s", args->field.name);
5674 break;
5675 case PRINT_FLAGS:
5676 printf("__print_flags(");
5677 print_args(args->flags.field);
5678 printf(", %s, ", args->flags.delim);
5679 trace_seq_init(&s);
5680 print_fields(&s, args->flags.flags);
5681 trace_seq_do_printf(&s);
5682 trace_seq_destroy(&s);
5683 printf(")");
5684 break;
5685 case PRINT_SYMBOL:
5686 printf("__print_symbolic(");
5687 print_args(args->symbol.field);
5688 printf(", ");
5689 trace_seq_init(&s);
5690 print_fields(&s, args->symbol.symbols);
5691 trace_seq_do_printf(&s);
5692 trace_seq_destroy(&s);
5693 printf(")");
5694 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09005695 case PRINT_HEX:
5696 printf("__print_hex(");
5697 print_args(args->hex.field);
5698 printf(", ");
5699 print_args(args->hex.size);
5700 printf(")");
5701 break;
Javi Merinob839e1e82015-03-24 11:07:19 +00005702 case PRINT_INT_ARRAY:
5703 printf("__print_array(");
5704 print_args(args->int_array.field);
5705 printf(", ");
5706 print_args(args->int_array.count);
5707 printf(", ");
5708 print_args(args->int_array.el_size);
5709 printf(")");
5710 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005711 case PRINT_STRING:
5712 case PRINT_BSTRING:
5713 printf("__get_str(%s)", args->string.string);
5714 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04005715 case PRINT_BITMASK:
5716 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5717 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005718 case PRINT_TYPE:
5719 printf("(%s)", args->typecast.type);
5720 print_args(args->typecast.item);
5721 break;
5722 case PRINT_OP:
5723 if (strcmp(args->op.op, ":") == 0)
5724 print_paren = 0;
5725 if (print_paren)
5726 printf("(");
5727 print_args(args->op.left);
5728 printf(" %s ", args->op.op);
5729 print_args(args->op.right);
5730 if (print_paren)
5731 printf(")");
5732 break;
5733 default:
5734 /* we should warn... */
5735 return;
5736 }
5737 if (args->next) {
5738 printf("\n");
5739 print_args(args->next);
5740 }
5741}
5742
5743static void parse_header_field(const char *field,
5744 int *offset, int *size, int mandatory)
5745{
5746 unsigned long long save_input_buf_ptr;
5747 unsigned long long save_input_buf_siz;
5748 char *token;
5749 int type;
5750
5751 save_input_buf_ptr = input_buf_ptr;
5752 save_input_buf_siz = input_buf_siz;
5753
5754 if (read_expected(EVENT_ITEM, "field") < 0)
5755 return;
5756 if (read_expected(EVENT_OP, ":") < 0)
5757 return;
5758
5759 /* type */
5760 if (read_expect_type(EVENT_ITEM, &token) < 0)
5761 goto fail;
5762 free_token(token);
5763
5764 /*
5765 * If this is not a mandatory field, then test it first.
5766 */
5767 if (mandatory) {
5768 if (read_expected(EVENT_ITEM, field) < 0)
5769 return;
5770 } else {
5771 if (read_expect_type(EVENT_ITEM, &token) < 0)
5772 goto fail;
5773 if (strcmp(token, field) != 0)
5774 goto discard;
5775 free_token(token);
5776 }
5777
5778 if (read_expected(EVENT_OP, ";") < 0)
5779 return;
5780 if (read_expected(EVENT_ITEM, "offset") < 0)
5781 return;
5782 if (read_expected(EVENT_OP, ":") < 0)
5783 return;
5784 if (read_expect_type(EVENT_ITEM, &token) < 0)
5785 goto fail;
5786 *offset = atoi(token);
5787 free_token(token);
5788 if (read_expected(EVENT_OP, ";") < 0)
5789 return;
5790 if (read_expected(EVENT_ITEM, "size") < 0)
5791 return;
5792 if (read_expected(EVENT_OP, ":") < 0)
5793 return;
5794 if (read_expect_type(EVENT_ITEM, &token) < 0)
5795 goto fail;
5796 *size = atoi(token);
5797 free_token(token);
5798 if (read_expected(EVENT_OP, ";") < 0)
5799 return;
5800 type = read_token(&token);
5801 if (type != EVENT_NEWLINE) {
5802 /* newer versions of the kernel have a "signed" type */
5803 if (type != EVENT_ITEM)
5804 goto fail;
5805
5806 if (strcmp(token, "signed") != 0)
5807 goto fail;
5808
5809 free_token(token);
5810
5811 if (read_expected(EVENT_OP, ":") < 0)
5812 return;
5813
5814 if (read_expect_type(EVENT_ITEM, &token))
5815 goto fail;
5816
5817 free_token(token);
5818 if (read_expected(EVENT_OP, ";") < 0)
5819 return;
5820
5821 if (read_expect_type(EVENT_NEWLINE, &token))
5822 goto fail;
5823 }
5824 fail:
5825 free_token(token);
5826 return;
5827
5828 discard:
5829 input_buf_ptr = save_input_buf_ptr;
5830 input_buf_siz = save_input_buf_siz;
5831 *offset = 0;
5832 *size = 0;
5833 free_token(token);
5834}
5835
5836/**
5837 * pevent_parse_header_page - parse the data stored in the header page
5838 * @pevent: the handle to the pevent
5839 * @buf: the buffer storing the header page format string
5840 * @size: the size of @buf
5841 * @long_size: the long size to use if there is no header
5842 *
5843 * This parses the header page format for information on the
5844 * ring buffer used. The @buf should be copied from
5845 *
5846 * /sys/kernel/debug/tracing/events/header_page
5847 */
5848int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5849 int long_size)
5850{
5851 int ignore;
5852
5853 if (!size) {
5854 /*
5855 * Old kernels did not have header page info.
5856 * Sorry but we just use what we find here in user space.
5857 */
5858 pevent->header_page_ts_size = sizeof(long long);
5859 pevent->header_page_size_size = long_size;
5860 pevent->header_page_data_offset = sizeof(long long) + long_size;
5861 pevent->old_format = 1;
5862 return -1;
5863 }
5864 init_input_buf(buf, size);
5865
5866 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5867 &pevent->header_page_ts_size, 1);
5868 parse_header_field("commit", &pevent->header_page_size_offset,
5869 &pevent->header_page_size_size, 1);
5870 parse_header_field("overwrite", &pevent->header_page_overwrite,
5871 &ignore, 0);
5872 parse_header_field("data", &pevent->header_page_data_offset,
5873 &pevent->header_page_data_size, 1);
5874
5875 return 0;
5876}
5877
5878static int event_matches(struct event_format *event,
5879 int id, const char *sys_name,
5880 const char *event_name)
5881{
5882 if (id >= 0 && id != event->id)
5883 return 0;
5884
5885 if (event_name && (strcmp(event_name, event->name) != 0))
5886 return 0;
5887
5888 if (sys_name && (strcmp(sys_name, event->system) != 0))
5889 return 0;
5890
5891 return 1;
5892}
5893
5894static void free_handler(struct event_handler *handle)
5895{
5896 free((void *)handle->sys_name);
5897 free((void *)handle->event_name);
5898 free(handle);
5899}
5900
5901static int find_event_handle(struct pevent *pevent, struct event_format *event)
5902{
5903 struct event_handler *handle, **next;
5904
5905 for (next = &pevent->handlers; *next;
5906 next = &(*next)->next) {
5907 handle = *next;
5908 if (event_matches(event, handle->id,
5909 handle->sys_name,
5910 handle->event_name))
5911 break;
5912 }
5913
5914 if (!(*next))
5915 return 0;
5916
5917 pr_stat("overriding event (%d) %s:%s with new print handler",
5918 event->id, event->system, event->name);
5919
5920 event->handler = handle->func;
5921 event->context = handle->context;
5922
5923 *next = handle->next;
5924 free_handler(handle);
5925
5926 return 1;
5927}
5928
5929/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005930 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005931 * @buf: the buffer storing the event format string
5932 * @size: the size of @buf
5933 * @sys: the system the event belongs to
5934 *
5935 * This parses the event format and creates an event structure
5936 * to quickly parse raw data for a given event.
5937 *
5938 * These files currently come from:
5939 *
5940 * /sys/kernel/debug/tracing/events/.../.../format
5941 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005942enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5943 struct pevent *pevent, const char *buf,
5944 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005945{
5946 struct event_format *event;
5947 int ret;
5948
5949 init_input_buf(buf, size);
5950
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005951 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02005952 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09005953 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005954
5955 event->name = event_read_name();
5956 if (!event->name) {
5957 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09005958 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5959 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005960 }
5961
5962 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005963 event->flags |= EVENT_FL_ISFTRACE;
5964
5965 if (strcmp(event->name, "bprint") == 0)
5966 event->flags |= EVENT_FL_ISBPRINT;
5967 }
5968
5969 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005970 if (event->id < 0) {
5971 ret = PEVENT_ERRNO__READ_ID_FAILED;
5972 /*
5973 * This isn't an allocation error actually.
5974 * But as the ID is critical, just bail out.
5975 */
5976 goto event_alloc_failed;
5977 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005978
5979 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005980 if (!event->system) {
5981 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5982 goto event_alloc_failed;
5983 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005984
Steven Rostedt101782e2012-10-01 20:13:51 -04005985 /* Add pevent to event so that it can be referenced */
5986 event->pevent = pevent;
5987
Steven Rostedtf7d82352012-04-06 00:47:53 +02005988 ret = event_read_format(event);
5989 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005990 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5991 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005992 }
5993
5994 /*
5995 * If the event has an override, don't print warnings if the event
5996 * print format fails to parse.
5997 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005998 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005999 show_warning = 0;
6000
6001 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006002 show_warning = 1;
6003
Steven Rostedtf7d82352012-04-06 00:47:53 +02006004 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09006005 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
6006 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006007 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006008
6009 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6010 struct format_field *field;
6011 struct print_arg *arg, **list;
6012
6013 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02006014 list = &event->print_fmt.args;
6015 for (field = event->format.fields; field; field = field->next) {
6016 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09006017 if (!arg) {
6018 event->flags |= EVENT_FL_FAILED;
6019 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6020 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006021 arg->type = PRINT_FIELD;
6022 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09006023 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09006024 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09006025 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09006026 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006027 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006028 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09006029 *list = arg;
6030 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006031 }
6032 return 0;
6033 }
6034
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006035 return 0;
6036
6037 event_parse_failed:
6038 event->flags |= EVENT_FL_FAILED;
6039 return ret;
6040
6041 event_alloc_failed:
6042 free(event->system);
6043 free(event->name);
6044 free(event);
6045 *eventp = NULL;
6046 return ret;
6047}
6048
Jiri Olsa71ad9582013-12-03 14:09:19 +01006049static enum pevent_errno
6050__pevent_parse_event(struct pevent *pevent,
6051 struct event_format **eventp,
6052 const char *buf, unsigned long size,
6053 const char *sys)
6054{
6055 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
6056 struct event_format *event = *eventp;
6057
6058 if (event == NULL)
6059 return ret;
6060
6061 if (pevent && add_event(pevent, event)) {
6062 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6063 goto event_add_failed;
6064 }
6065
6066#define PRINT_ARGS 0
6067 if (PRINT_ARGS && event->print_fmt.args)
6068 print_args(event->print_fmt.args);
6069
6070 return 0;
6071
6072event_add_failed:
6073 pevent_free_format(event);
6074 return ret;
6075}
6076
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006077/**
6078 * pevent_parse_format - parse the event format
Jiri Olsa71ad9582013-12-03 14:09:19 +01006079 * @pevent: the handle to the pevent
6080 * @eventp: returned format
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006081 * @buf: the buffer storing the event format string
6082 * @size: the size of @buf
6083 * @sys: the system the event belongs to
6084 *
6085 * This parses the event format and creates an event structure
6086 * to quickly parse raw data for a given event.
6087 *
6088 * These files currently come from:
6089 *
6090 * /sys/kernel/debug/tracing/events/.../.../format
6091 */
Jiri Olsa71ad9582013-12-03 14:09:19 +01006092enum pevent_errno pevent_parse_format(struct pevent *pevent,
6093 struct event_format **eventp,
6094 const char *buf,
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006095 unsigned long size, const char *sys)
6096{
Jiri Olsa71ad9582013-12-03 14:09:19 +01006097 return __pevent_parse_event(pevent, eventp, buf, size, sys);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006098}
6099
6100/**
6101 * pevent_parse_event - parse the event format
6102 * @pevent: the handle to the pevent
6103 * @buf: the buffer storing the event format string
6104 * @size: the size of @buf
6105 * @sys: the system the event belongs to
6106 *
6107 * This parses the event format and creates an event structure
6108 * to quickly parse raw data for a given event.
6109 *
6110 * These files currently come from:
6111 *
6112 * /sys/kernel/debug/tracing/events/.../.../format
6113 */
6114enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
6115 unsigned long size, const char *sys)
6116{
6117 struct event_format *event = NULL;
Jiri Olsa71ad9582013-12-03 14:09:19 +01006118 return __pevent_parse_event(pevent, &event, buf, size, sys);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006119}
6120
Namhyung Kim2f197b92012-08-22 16:00:30 +09006121#undef _PE
6122#define _PE(code, str) str
6123static const char * const pevent_error_str[] = {
6124 PEVENT_ERRORS
6125};
6126#undef _PE
6127
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03006128int pevent_strerror(struct pevent *pevent __maybe_unused,
6129 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09006130{
6131 int idx;
6132 const char *msg;
6133
6134 if (errnum >= 0) {
Arnaldo Carvalho de Meloc3cec9e2016-07-08 15:21:37 -03006135 str_error_r(errnum, buf, buflen);
Namhyung Kim2f197b92012-08-22 16:00:30 +09006136 return 0;
6137 }
6138
6139 if (errnum <= __PEVENT_ERRNO__START ||
6140 errnum >= __PEVENT_ERRNO__END)
6141 return -1;
6142
Namhyung Kimf63fe792012-08-23 16:37:00 +09006143 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09006144 msg = pevent_error_str[idx];
Namhyung Kimbf19b822013-12-12 16:36:17 +09006145 snprintf(buf, buflen, "%s", msg);
Namhyung Kim2f197b92012-08-22 16:00:30 +09006146
6147 return 0;
6148}
6149
Steven Rostedtf7d82352012-04-06 00:47:53 +02006150int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02006151 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006152 unsigned long long *val, int err)
6153{
6154 if (!field) {
6155 if (err)
6156 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6157 return -1;
6158 }
6159
6160 if (pevent_read_number_field(field, record->data, val)) {
6161 if (err)
6162 trace_seq_printf(s, " %s=INVALID", name);
6163 return -1;
6164 }
6165
6166 return 0;
6167}
6168
6169/**
6170 * pevent_get_field_raw - return the raw pointer into the data field
6171 * @s: The seq to print to on error
6172 * @event: the event that the field is for
6173 * @name: The name of the field
6174 * @record: The record with the field name.
6175 * @len: place to store the field length.
6176 * @err: print default error if failed.
6177 *
6178 * Returns a pointer into record->data of the field and places
6179 * the length of the field in @len.
6180 *
6181 * On failure, it returns NULL.
6182 */
6183void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006184 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006185 int *len, int err)
6186{
6187 struct format_field *field;
6188 void *data = record->data;
6189 unsigned offset;
6190 int dummy;
6191
6192 if (!event)
6193 return NULL;
6194
6195 field = pevent_find_field(event, name);
6196
6197 if (!field) {
6198 if (err)
6199 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6200 return NULL;
6201 }
6202
6203 /* Allow @len to be NULL */
6204 if (!len)
6205 len = &dummy;
6206
6207 offset = field->offset;
6208 if (field->flags & FIELD_IS_DYNAMIC) {
6209 offset = pevent_read_number(event->pevent,
6210 data + offset, field->size);
6211 *len = offset >> 16;
6212 offset &= 0xffff;
6213 } else
6214 *len = field->size;
6215
6216 return data + offset;
6217}
6218
6219/**
6220 * pevent_get_field_val - find a field and return its value
6221 * @s: The seq to print to on error
6222 * @event: the event that the field is for
6223 * @name: The name of the field
6224 * @record: The record with the field name.
6225 * @val: place to store the value of the field.
6226 * @err: print default error if failed.
6227 *
6228 * Returns 0 on success -1 on field not found.
6229 */
6230int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006231 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006232 unsigned long long *val, int err)
6233{
6234 struct format_field *field;
6235
6236 if (!event)
6237 return -1;
6238
6239 field = pevent_find_field(event, name);
6240
6241 return get_field_val(s, field, name, record, val, err);
6242}
6243
6244/**
6245 * pevent_get_common_field_val - find a common field and return its value
6246 * @s: The seq to print to on error
6247 * @event: the event that the field is for
6248 * @name: The name of the field
6249 * @record: The record with the field name.
6250 * @val: place to store the value of the field.
6251 * @err: print default error if failed.
6252 *
6253 * Returns 0 on success -1 on field not found.
6254 */
6255int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006256 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006257 unsigned long long *val, int err)
6258{
6259 struct format_field *field;
6260
6261 if (!event)
6262 return -1;
6263
6264 field = pevent_find_common_field(event, name);
6265
6266 return get_field_val(s, field, name, record, val, err);
6267}
6268
6269/**
6270 * pevent_get_any_field_val - find a any field and return its value
6271 * @s: The seq to print to on error
6272 * @event: the event that the field is for
6273 * @name: The name of the field
6274 * @record: The record with the field name.
6275 * @val: place to store the value of the field.
6276 * @err: print default error if failed.
6277 *
6278 * Returns 0 on success -1 on field not found.
6279 */
6280int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006281 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006282 unsigned long long *val, int err)
6283{
6284 struct format_field *field;
6285
6286 if (!event)
6287 return -1;
6288
6289 field = pevent_find_any_field(event, name);
6290
6291 return get_field_val(s, field, name, record, val, err);
6292}
6293
6294/**
6295 * pevent_print_num_field - print a field and a format
6296 * @s: The seq to print to
6297 * @fmt: The printf format to print the field with.
6298 * @event: the event that the field is for
6299 * @name: The name of the field
6300 * @record: The record with the field name.
6301 * @err: print default error if failed.
6302 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006303 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02006304 */
6305int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6306 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02006307 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006308{
6309 struct format_field *field = pevent_find_field(event, name);
6310 unsigned long long val;
6311
6312 if (!field)
6313 goto failed;
6314
6315 if (pevent_read_number_field(field, record->data, &val))
6316 goto failed;
6317
6318 return trace_seq_printf(s, fmt, val);
6319
6320 failed:
6321 if (err)
6322 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6323 return -1;
6324}
6325
Steven Rostedt6d862b82013-11-01 17:54:00 -04006326/**
6327 * pevent_print_func_field - print a field and a format for function pointers
6328 * @s: The seq to print to
6329 * @fmt: The printf format to print the field with.
6330 * @event: the event that the field is for
6331 * @name: The name of the field
6332 * @record: The record with the field name.
6333 * @err: print default error if failed.
6334 *
6335 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6336 */
6337int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6338 struct event_format *event, const char *name,
6339 struct pevent_record *record, int err)
6340{
6341 struct format_field *field = pevent_find_field(event, name);
6342 struct pevent *pevent = event->pevent;
6343 unsigned long long val;
6344 struct func_map *func;
6345 char tmp[128];
6346
6347 if (!field)
6348 goto failed;
6349
6350 if (pevent_read_number_field(field, record->data, &val))
6351 goto failed;
6352
6353 func = find_func(pevent, val);
6354
6355 if (func)
6356 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6357 else
6358 sprintf(tmp, "0x%08llx", val);
6359
6360 return trace_seq_printf(s, fmt, tmp);
6361
6362 failed:
6363 if (err)
6364 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6365 return -1;
6366}
6367
Steven Rostedtf7d82352012-04-06 00:47:53 +02006368static void free_func_handle(struct pevent_function_handler *func)
6369{
6370 struct pevent_func_params *params;
6371
6372 free(func->name);
6373
6374 while (func->params) {
6375 params = func->params;
6376 func->params = params->next;
6377 free(params);
6378 }
6379
6380 free(func);
6381}
6382
6383/**
6384 * pevent_register_print_function - register a helper function
6385 * @pevent: the handle to the pevent
6386 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006387 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02006388 * @name: the name of the helper function
6389 * @parameters: A list of enum pevent_func_arg_type
6390 *
6391 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006392 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02006393 * of these functions.
6394 *
6395 * The @parameters is a variable list of pevent_func_arg_type enums that
6396 * must end with PEVENT_FUNC_ARG_VOID.
6397 */
6398int pevent_register_print_function(struct pevent *pevent,
6399 pevent_func_handler func,
6400 enum pevent_func_arg_type ret_type,
6401 char *name, ...)
6402{
6403 struct pevent_function_handler *func_handle;
6404 struct pevent_func_params **next_param;
6405 struct pevent_func_params *param;
6406 enum pevent_func_arg_type type;
6407 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006408 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006409
6410 func_handle = find_func_handler(pevent, name);
6411 if (func_handle) {
6412 /*
6413 * This is most like caused by the users own
6414 * plugins updating the function. This overrides the
6415 * system defaults.
6416 */
6417 pr_stat("override of function helper '%s'", name);
6418 remove_func_handler(pevent, name);
6419 }
6420
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006421 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09006422 if (!func_handle) {
6423 do_warning("Failed to allocate function handler");
6424 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6425 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006426
6427 func_handle->ret_type = ret_type;
6428 func_handle->name = strdup(name);
6429 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006430 if (!func_handle->name) {
6431 do_warning("Failed to allocate function name");
6432 free(func_handle);
6433 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6434 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006435
6436 next_param = &(func_handle->params);
6437 va_start(ap, name);
6438 for (;;) {
6439 type = va_arg(ap, enum pevent_func_arg_type);
6440 if (type == PEVENT_FUNC_ARG_VOID)
6441 break;
6442
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03006443 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09006444 do_warning("Invalid argument type %d", type);
6445 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006446 goto out_free;
6447 }
6448
Namhyung Kim67ed9392012-09-07 11:49:47 +09006449 param = malloc(sizeof(*param));
6450 if (!param) {
6451 do_warning("Failed to allocate function param");
6452 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6453 goto out_free;
6454 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006455 param->type = type;
6456 param->next = NULL;
6457
6458 *next_param = param;
6459 next_param = &(param->next);
6460
6461 func_handle->nr_args++;
6462 }
6463 va_end(ap);
6464
6465 func_handle->next = pevent->func_handlers;
6466 pevent->func_handlers = func_handle;
6467
6468 return 0;
6469 out_free:
6470 va_end(ap);
6471 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09006472 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006473}
6474
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006475/**
6476 * pevent_unregister_print_function - unregister a helper function
6477 * @pevent: the handle to the pevent
6478 * @func: the function to process the helper function
6479 * @name: the name of the helper function
6480 *
6481 * This function removes existing print handler for function @name.
6482 *
6483 * Returns 0 if the handler was removed successully, -1 otherwise.
6484 */
6485int pevent_unregister_print_function(struct pevent *pevent,
6486 pevent_func_handler func, char *name)
6487{
6488 struct pevent_function_handler *func_handle;
6489
6490 func_handle = find_func_handler(pevent, name);
6491 if (func_handle && func_handle->func == func) {
6492 remove_func_handler(pevent, name);
6493 return 0;
6494 }
6495 return -1;
6496}
6497
Namhyung Kimad137012014-01-16 11:31:07 +09006498static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6499 const char *sys_name,
6500 const char *event_name)
6501{
6502 struct event_format *event;
6503
6504 if (id >= 0) {
6505 /* search by id */
6506 event = pevent_find_event(pevent, id);
6507 if (!event)
6508 return NULL;
6509 if (event_name && (strcmp(event_name, event->name) != 0))
6510 return NULL;
6511 if (sys_name && (strcmp(sys_name, event->system) != 0))
6512 return NULL;
6513 } else {
6514 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6515 if (!event)
6516 return NULL;
6517 }
6518 return event;
6519}
6520
Steven Rostedtf7d82352012-04-06 00:47:53 +02006521/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006522 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02006523 * @pevent: the handle to the pevent
6524 * @id: the id of the event to register
6525 * @sys_name: the system name the event belongs to
6526 * @event_name: the name of the event
6527 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006528 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02006529 *
6530 * This function allows a developer to override the parsing of
6531 * a given event. If for some reason the default print format
6532 * is not sufficient, this function will register a function
6533 * for an event to be used to parse the data instead.
6534 *
6535 * If @id is >= 0, then it is used to find the event.
6536 * else @sys_name and @event_name are used.
6537 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09006538int pevent_register_event_handler(struct pevent *pevent, int id,
6539 const char *sys_name, const char *event_name,
6540 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006541{
6542 struct event_format *event;
6543 struct event_handler *handle;
6544
Namhyung Kimad137012014-01-16 11:31:07 +09006545 event = pevent_search_event(pevent, id, sys_name, event_name);
6546 if (event == NULL)
6547 goto not_found;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006548
6549 pr_stat("overriding event (%d) %s:%s with new print handler",
6550 event->id, event->system, event->name);
6551
6552 event->handler = func;
6553 event->context = context;
6554 return 0;
6555
6556 not_found:
6557 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006558 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006559 if (!handle) {
6560 do_warning("Failed to allocate event handler");
6561 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6562 }
6563
Steven Rostedtf7d82352012-04-06 00:47:53 +02006564 handle->id = id;
6565 if (event_name)
6566 handle->event_name = strdup(event_name);
6567 if (sys_name)
6568 handle->sys_name = strdup(sys_name);
6569
Namhyung Kimca638582012-04-09 11:54:31 +09006570 if ((event_name && !handle->event_name) ||
6571 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006572 do_warning("Failed to allocate event/sys name");
6573 free((void *)handle->event_name);
6574 free((void *)handle->sys_name);
6575 free(handle);
6576 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006577 }
6578
Steven Rostedtf7d82352012-04-06 00:47:53 +02006579 handle->func = func;
6580 handle->next = pevent->handlers;
6581 pevent->handlers = handle;
6582 handle->context = context;
6583
6584 return -1;
6585}
6586
Namhyung Kimad137012014-01-16 11:31:07 +09006587static int handle_matches(struct event_handler *handler, int id,
6588 const char *sys_name, const char *event_name,
6589 pevent_event_handler_func func, void *context)
6590{
6591 if (id >= 0 && id != handler->id)
6592 return 0;
6593
6594 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6595 return 0;
6596
6597 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6598 return 0;
6599
6600 if (func != handler->func || context != handler->context)
6601 return 0;
6602
6603 return 1;
6604}
6605
6606/**
6607 * pevent_unregister_event_handler - unregister an existing event handler
6608 * @pevent: the handle to the pevent
6609 * @id: the id of the event to unregister
6610 * @sys_name: the system name the handler belongs to
6611 * @event_name: the name of the event handler
6612 * @func: the function to call to parse the event information
6613 * @context: the data to be passed to @func
6614 *
6615 * This function removes existing event handler (parser).
6616 *
6617 * If @id is >= 0, then it is used to find the event.
6618 * else @sys_name and @event_name are used.
6619 *
6620 * Returns 0 if handler was removed successfully, -1 if event was not found.
6621 */
6622int pevent_unregister_event_handler(struct pevent *pevent, int id,
6623 const char *sys_name, const char *event_name,
6624 pevent_event_handler_func func, void *context)
6625{
6626 struct event_format *event;
6627 struct event_handler *handle;
6628 struct event_handler **next;
6629
6630 event = pevent_search_event(pevent, id, sys_name, event_name);
6631 if (event == NULL)
6632 goto not_found;
6633
6634 if (event->handler == func && event->context == context) {
6635 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6636 event->id, event->system, event->name);
6637
6638 event->handler = NULL;
6639 event->context = NULL;
6640 return 0;
6641 }
6642
6643not_found:
6644 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6645 handle = *next;
6646 if (handle_matches(handle, id, sys_name, event_name,
6647 func, context))
6648 break;
6649 }
6650
6651 if (!(*next))
6652 return -1;
6653
6654 *next = handle->next;
6655 free_handler(handle);
6656
6657 return 0;
6658}
6659
Steven Rostedtf7d82352012-04-06 00:47:53 +02006660/**
6661 * pevent_alloc - create a pevent handle
6662 */
6663struct pevent *pevent_alloc(void)
6664{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006665 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02006666
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006667 if (pevent)
6668 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006669
6670 return pevent;
6671}
6672
6673void pevent_ref(struct pevent *pevent)
6674{
6675 pevent->ref_count++;
6676}
6677
David Ahern00ae1122015-03-19 12:36:21 -06006678void pevent_free_format_field(struct format_field *field)
6679{
6680 free(field->type);
Jiri Olsad3542432015-04-18 17:50:18 +02006681 if (field->alias != field->name)
6682 free(field->alias);
David Ahern00ae1122015-03-19 12:36:21 -06006683 free(field->name);
6684 free(field);
6685}
6686
Steven Rostedtf7d82352012-04-06 00:47:53 +02006687static void free_format_fields(struct format_field *field)
6688{
6689 struct format_field *next;
6690
6691 while (field) {
6692 next = field->next;
David Ahern00ae1122015-03-19 12:36:21 -06006693 pevent_free_format_field(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006694 field = next;
6695 }
6696}
6697
6698static void free_formats(struct format *format)
6699{
6700 free_format_fields(format->common_fields);
6701 free_format_fields(format->fields);
6702}
6703
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006704void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006705{
6706 free(event->name);
6707 free(event->system);
6708
6709 free_formats(&event->format);
6710
6711 free(event->print_fmt.format);
6712 free_args(event->print_fmt.args);
6713
6714 free(event);
6715}
6716
6717/**
6718 * pevent_free - free a pevent handle
6719 * @pevent: the pevent handle to free
6720 */
6721void pevent_free(struct pevent *pevent)
6722{
Steven Rostedta2525a02012-04-06 00:48:02 +02006723 struct cmdline_list *cmdlist, *cmdnext;
6724 struct func_list *funclist, *funcnext;
6725 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006726 struct pevent_function_handler *func_handler;
6727 struct event_handler *handle;
6728 int i;
6729
Steven Rostedta2525a02012-04-06 00:48:02 +02006730 if (!pevent)
6731 return;
6732
6733 cmdlist = pevent->cmdlist;
6734 funclist = pevent->funclist;
6735 printklist = pevent->printklist;
6736
Steven Rostedtf7d82352012-04-06 00:47:53 +02006737 pevent->ref_count--;
6738 if (pevent->ref_count)
6739 return;
6740
6741 if (pevent->cmdlines) {
6742 for (i = 0; i < pevent->cmdline_count; i++)
6743 free(pevent->cmdlines[i].comm);
6744 free(pevent->cmdlines);
6745 }
6746
6747 while (cmdlist) {
6748 cmdnext = cmdlist->next;
6749 free(cmdlist->comm);
6750 free(cmdlist);
6751 cmdlist = cmdnext;
6752 }
6753
6754 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006755 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02006756 free(pevent->func_map[i].func);
6757 free(pevent->func_map[i].mod);
6758 }
6759 free(pevent->func_map);
6760 }
6761
6762 while (funclist) {
6763 funcnext = funclist->next;
6764 free(funclist->func);
6765 free(funclist->mod);
6766 free(funclist);
6767 funclist = funcnext;
6768 }
6769
6770 while (pevent->func_handlers) {
6771 func_handler = pevent->func_handlers;
6772 pevent->func_handlers = func_handler->next;
6773 free_func_handle(func_handler);
6774 }
6775
6776 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006777 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006778 free(pevent->printk_map[i].printk);
6779 free(pevent->printk_map);
6780 }
6781
6782 while (printklist) {
6783 printknext = printklist->next;
6784 free(printklist->printk);
6785 free(printklist);
6786 printklist = printknext;
6787 }
6788
6789 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006790 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006791
6792 while (pevent->handlers) {
6793 handle = pevent->handlers;
6794 pevent->handlers = handle->next;
6795 free_handler(handle);
6796 }
6797
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -04006798 free(pevent->trace_clock);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006799 free(pevent->events);
6800 free(pevent->sort_events);
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -03006801 free(pevent->func_resolver);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006802
6803 free(pevent);
6804}
6805
6806void pevent_unref(struct pevent *pevent)
6807{
6808 pevent_free(pevent);
6809}