blob: d7c37a7d9255a6f95483661af2a107529382c7e5 [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>
Steven Rostedtf7d82352012-04-06 00:47:53 +020034
David Ahern3d199b52014-12-18 19:11:11 -070035#include <netinet/ip6.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020036#include "event-parse.h"
Steven Rostedt668fe012012-04-06 00:47:55 +020037#include "event-utils.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020038
39static const char *input_buf;
40static unsigned long long input_buf_ptr;
41static unsigned long long input_buf_siz;
42
Tom Zanussi5205aec2012-04-06 00:47:58 +020043static int is_flag_field;
44static int is_symbolic_field;
45
Steven Rostedtf7d82352012-04-06 00:47:53 +020046static int show_warning = 1;
47
48#define do_warning(fmt, ...) \
49 do { \
50 if (show_warning) \
51 warning(fmt, ##__VA_ARGS__); \
52 } while (0)
53
Namhyung Kim3388cc32014-03-19 10:22:53 +090054#define do_warning_event(event, fmt, ...) \
55 do { \
56 if (!show_warning) \
57 continue; \
58 \
59 if (event) \
60 warning("[%s:%s] " fmt, event->system, \
61 event->name, ##__VA_ARGS__); \
62 else \
63 warning(fmt, ##__VA_ARGS__); \
64 } while (0)
65
Steven Rostedtf7d82352012-04-06 00:47:53 +020066static void init_input_buf(const char *buf, unsigned long long size)
67{
68 input_buf = buf;
69 input_buf_siz = size;
70 input_buf_ptr = 0;
71}
72
73const char *pevent_get_input_buf(void)
74{
75 return input_buf;
76}
77
78unsigned long long pevent_get_input_buf_ptr(void)
79{
80 return input_buf_ptr;
81}
82
83struct event_handler {
84 struct event_handler *next;
85 int id;
86 const char *sys_name;
87 const char *event_name;
88 pevent_event_handler_func func;
89 void *context;
90};
91
92struct pevent_func_params {
93 struct pevent_func_params *next;
94 enum pevent_func_arg_type type;
95};
96
97struct pevent_function_handler {
98 struct pevent_function_handler *next;
99 enum pevent_func_arg_type ret_type;
100 char *name;
101 pevent_func_handler func;
102 struct pevent_func_params *params;
103 int nr_args;
104};
105
106static unsigned long long
107process_defined_func(struct trace_seq *s, void *data, int size,
108 struct event_format *event, struct print_arg *arg);
109
110static void free_func_handle(struct pevent_function_handler *func);
111
112/**
113 * pevent_buffer_init - init buffer for parsing
114 * @buf: buffer to parse
115 * @size: the size of the buffer
116 *
117 * For use with pevent_read_token(), this initializes the internal
118 * buffer that pevent_read_token() will parse.
119 */
120void pevent_buffer_init(const char *buf, unsigned long long size)
121{
122 init_input_buf(buf, size);
123}
124
125void breakpoint(void)
126{
127 static int x;
128 x++;
129}
130
131struct print_arg *alloc_arg(void)
132{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300133 return calloc(1, sizeof(struct print_arg));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200134}
135
136struct cmdline {
137 char *comm;
138 int pid;
139};
140
141static int cmdline_cmp(const void *a, const void *b)
142{
143 const struct cmdline *ca = a;
144 const struct cmdline *cb = b;
145
146 if (ca->pid < cb->pid)
147 return -1;
148 if (ca->pid > cb->pid)
149 return 1;
150
151 return 0;
152}
153
154struct cmdline_list {
155 struct cmdline_list *next;
156 char *comm;
157 int pid;
158};
159
160static int cmdline_init(struct pevent *pevent)
161{
162 struct cmdline_list *cmdlist = pevent->cmdlist;
163 struct cmdline_list *item;
164 struct cmdline *cmdlines;
165 int i;
166
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300167 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
168 if (!cmdlines)
169 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200170
171 i = 0;
172 while (cmdlist) {
173 cmdlines[i].pid = cmdlist->pid;
174 cmdlines[i].comm = cmdlist->comm;
175 i++;
176 item = cmdlist;
177 cmdlist = cmdlist->next;
178 free(item);
179 }
180
181 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
182
183 pevent->cmdlines = cmdlines;
184 pevent->cmdlist = NULL;
185
186 return 0;
187}
188
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -0300189static const char *find_cmdline(struct pevent *pevent, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200190{
191 const struct cmdline *comm;
192 struct cmdline key;
193
194 if (!pid)
195 return "<idle>";
196
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300197 if (!pevent->cmdlines && cmdline_init(pevent))
198 return "<not enough memory for cmdlines!>";
Steven Rostedtf7d82352012-04-06 00:47:53 +0200199
200 key.pid = pid;
201
202 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
203 sizeof(*pevent->cmdlines), cmdline_cmp);
204
205 if (comm)
206 return comm->comm;
207 return "<...>";
208}
209
210/**
211 * pevent_pid_is_registered - return if a pid has a cmdline registered
212 * @pevent: handle for the pevent
213 * @pid: The pid to check if it has a cmdline registered with.
214 *
215 * Returns 1 if the pid has a cmdline mapped to it
216 * 0 otherwise.
217 */
218int pevent_pid_is_registered(struct pevent *pevent, int pid)
219{
220 const struct cmdline *comm;
221 struct cmdline key;
222
223 if (!pid)
224 return 1;
225
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300226 if (!pevent->cmdlines && cmdline_init(pevent))
227 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200228
229 key.pid = pid;
230
231 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
232 sizeof(*pevent->cmdlines), cmdline_cmp);
233
234 if (comm)
235 return 1;
236 return 0;
237}
238
239/*
240 * If the command lines have been converted to an array, then
241 * we must add this pid. This is much slower than when cmdlines
242 * are added before the array is initialized.
243 */
244static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
245{
246 struct cmdline *cmdlines = pevent->cmdlines;
247 const struct cmdline *cmdline;
248 struct cmdline key;
249
250 if (!pid)
251 return 0;
252
253 /* avoid duplicates */
254 key.pid = pid;
255
256 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
257 sizeof(*pevent->cmdlines), cmdline_cmp);
258 if (cmdline) {
259 errno = EEXIST;
260 return -1;
261 }
262
263 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
264 if (!cmdlines) {
265 errno = ENOMEM;
266 return -1;
267 }
268
Steven Rostedtf7d82352012-04-06 00:47:53 +0200269 cmdlines[pevent->cmdline_count].comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300270 if (!cmdlines[pevent->cmdline_count].comm) {
271 free(cmdlines);
272 errno = ENOMEM;
273 return -1;
274 }
275
276 cmdlines[pevent->cmdline_count].pid = pid;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200277
278 if (cmdlines[pevent->cmdline_count].comm)
279 pevent->cmdline_count++;
280
281 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
282 pevent->cmdlines = cmdlines;
283
284 return 0;
285}
286
287/**
288 * pevent_register_comm - register a pid / comm mapping
289 * @pevent: handle for the pevent
290 * @comm: the command line to register
291 * @pid: the pid to map the command line to
292 *
293 * This adds a mapping to search for command line names with
294 * a given pid. The comm is duplicated.
295 */
296int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
297{
298 struct cmdline_list *item;
299
300 if (pevent->cmdlines)
301 return add_new_comm(pevent, comm, pid);
302
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300303 item = malloc(sizeof(*item));
304 if (!item)
305 return -1;
306
Steven Rostedtf7d82352012-04-06 00:47:53 +0200307 item->comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300308 if (!item->comm) {
309 free(item);
310 return -1;
311 }
Steven Rostedtf7d82352012-04-06 00:47:53 +0200312 item->pid = pid;
313 item->next = pevent->cmdlist;
314
315 pevent->cmdlist = item;
316 pevent->cmdline_count++;
317
318 return 0;
319}
320
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400321void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
322{
323 pevent->trace_clock = trace_clock;
324}
325
Steven Rostedtf7d82352012-04-06 00:47:53 +0200326struct func_map {
327 unsigned long long addr;
328 char *func;
329 char *mod;
330};
331
332struct func_list {
333 struct func_list *next;
334 unsigned long long addr;
335 char *func;
336 char *mod;
337};
338
339static int func_cmp(const void *a, const void *b)
340{
341 const struct func_map *fa = a;
342 const struct func_map *fb = b;
343
344 if (fa->addr < fb->addr)
345 return -1;
346 if (fa->addr > fb->addr)
347 return 1;
348
349 return 0;
350}
351
352/*
353 * We are searching for a record in between, not an exact
354 * match.
355 */
356static int func_bcmp(const void *a, const void *b)
357{
358 const struct func_map *fa = a;
359 const struct func_map *fb = b;
360
361 if ((fa->addr == fb->addr) ||
362
363 (fa->addr > fb->addr &&
364 fa->addr < (fb+1)->addr))
365 return 0;
366
367 if (fa->addr < fb->addr)
368 return -1;
369
370 return 1;
371}
372
373static int func_map_init(struct pevent *pevent)
374{
375 struct func_list *funclist;
376 struct func_list *item;
377 struct func_map *func_map;
378 int i;
379
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300380 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
381 if (!func_map)
382 return -1;
383
Steven Rostedtf7d82352012-04-06 00:47:53 +0200384 funclist = pevent->funclist;
385
386 i = 0;
387 while (funclist) {
388 func_map[i].func = funclist->func;
389 func_map[i].addr = funclist->addr;
390 func_map[i].mod = funclist->mod;
391 i++;
392 item = funclist;
393 funclist = funclist->next;
394 free(item);
395 }
396
397 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
398
399 /*
400 * Add a special record at the end.
401 */
402 func_map[pevent->func_count].func = NULL;
403 func_map[pevent->func_count].addr = 0;
404 func_map[pevent->func_count].mod = NULL;
405
406 pevent->func_map = func_map;
407 pevent->funclist = NULL;
408
409 return 0;
410}
411
412static struct func_map *
413find_func(struct pevent *pevent, unsigned long long addr)
414{
415 struct func_map *func;
416 struct func_map key;
417
418 if (!pevent->func_map)
419 func_map_init(pevent);
420
421 key.addr = addr;
422
423 func = bsearch(&key, pevent->func_map, pevent->func_count,
424 sizeof(*pevent->func_map), func_bcmp);
425
426 return func;
427}
428
429/**
430 * pevent_find_function - find a function by a given address
431 * @pevent: handle for the pevent
432 * @addr: the address to find the function with
433 *
434 * Returns a pointer to the function stored that has the given
435 * address. Note, the address does not have to be exact, it
436 * will select the function that would contain the address.
437 */
438const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
439{
440 struct func_map *map;
441
442 map = find_func(pevent, addr);
443 if (!map)
444 return NULL;
445
446 return map->func;
447}
448
449/**
450 * pevent_find_function_address - find a function address by a given address
451 * @pevent: handle for the pevent
452 * @addr: the address to find the function with
453 *
454 * Returns the address the function starts at. This can be used in
455 * conjunction with pevent_find_function to print both the function
456 * name and the function offset.
457 */
458unsigned long long
459pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
460{
461 struct func_map *map;
462
463 map = find_func(pevent, addr);
464 if (!map)
465 return 0;
466
467 return map->addr;
468}
469
470/**
471 * pevent_register_function - register a function with a given address
472 * @pevent: handle for the pevent
473 * @function: the function name to register
474 * @addr: the address the function starts at
475 * @mod: the kernel module the function may be in (NULL for none)
476 *
477 * This registers a function name with an address and module.
478 * The @func passed in is duplicated.
479 */
480int pevent_register_function(struct pevent *pevent, char *func,
481 unsigned long long addr, char *mod)
482{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300483 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200484
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300485 if (!item)
486 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200487
488 item->next = pevent->funclist;
489 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300490 if (!item->func)
491 goto out_free;
492
493 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200494 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300495 if (!item->mod)
496 goto out_free_func;
497 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200498 item->mod = NULL;
499 item->addr = addr;
500
Namhyung Kimca638582012-04-09 11:54:31 +0900501 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200502 pevent->func_count++;
503
504 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300505
506out_free_func:
507 free(item->func);
508 item->func = NULL;
509out_free:
510 free(item);
511 errno = ENOMEM;
512 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200513}
514
515/**
516 * pevent_print_funcs - print out the stored functions
517 * @pevent: handle for the pevent
518 *
519 * This prints out the stored functions.
520 */
521void pevent_print_funcs(struct pevent *pevent)
522{
523 int i;
524
525 if (!pevent->func_map)
526 func_map_init(pevent);
527
528 for (i = 0; i < (int)pevent->func_count; i++) {
529 printf("%016llx %s",
530 pevent->func_map[i].addr,
531 pevent->func_map[i].func);
532 if (pevent->func_map[i].mod)
533 printf(" [%s]\n", pevent->func_map[i].mod);
534 else
535 printf("\n");
536 }
537}
538
539struct printk_map {
540 unsigned long long addr;
541 char *printk;
542};
543
544struct printk_list {
545 struct printk_list *next;
546 unsigned long long addr;
547 char *printk;
548};
549
550static int printk_cmp(const void *a, const void *b)
551{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900552 const struct printk_map *pa = a;
553 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200554
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900555 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200556 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900557 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200558 return 1;
559
560 return 0;
561}
562
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300563static int printk_map_init(struct pevent *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200564{
565 struct printk_list *printklist;
566 struct printk_list *item;
567 struct printk_map *printk_map;
568 int i;
569
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300570 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
571 if (!printk_map)
572 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200573
574 printklist = pevent->printklist;
575
576 i = 0;
577 while (printklist) {
578 printk_map[i].printk = printklist->printk;
579 printk_map[i].addr = printklist->addr;
580 i++;
581 item = printklist;
582 printklist = printklist->next;
583 free(item);
584 }
585
586 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
587
588 pevent->printk_map = printk_map;
589 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300590
591 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200592}
593
594static struct printk_map *
595find_printk(struct pevent *pevent, unsigned long long addr)
596{
597 struct printk_map *printk;
598 struct printk_map key;
599
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300600 if (!pevent->printk_map && printk_map_init(pevent))
601 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200602
603 key.addr = addr;
604
605 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
606 sizeof(*pevent->printk_map), printk_cmp);
607
608 return printk;
609}
610
611/**
612 * pevent_register_print_string - register a string by its address
613 * @pevent: handle for the pevent
614 * @fmt: the string format to register
615 * @addr: the address the string was located at
616 *
617 * This registers a string by the address it was stored in the kernel.
618 * The @fmt passed in is duplicated.
619 */
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400620int pevent_register_print_string(struct pevent *pevent, const char *fmt,
Steven Rostedtf7d82352012-04-06 00:47:53 +0200621 unsigned long long addr)
622{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300623 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400624 char *p;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200625
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300626 if (!item)
627 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200628
629 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200630 item->addr = addr;
631
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400632 /* Strip off quotes and '\n' from the end */
633 if (fmt[0] == '"')
634 fmt++;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300635 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900636 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300637 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900638
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400639 p = item->printk + strlen(item->printk) - 1;
640 if (*p == '"')
641 *p = 0;
642
643 p -= 2;
644 if (strcmp(p, "\\n") == 0)
645 *p = 0;
646
Namhyung Kimca638582012-04-09 11:54:31 +0900647 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200648 pevent->printk_count++;
649
650 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300651
652out_free:
653 free(item);
654 errno = ENOMEM;
655 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200656}
657
658/**
659 * pevent_print_printk - print out the stored strings
660 * @pevent: handle for the pevent
661 *
662 * This prints the string formats that were stored.
663 */
664void pevent_print_printk(struct pevent *pevent)
665{
666 int i;
667
668 if (!pevent->printk_map)
669 printk_map_init(pevent);
670
671 for (i = 0; i < (int)pevent->printk_count; i++) {
672 printf("%016llx %s\n",
673 pevent->printk_map[i].addr,
674 pevent->printk_map[i].printk);
675 }
676}
677
678static struct event_format *alloc_event(void)
679{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300680 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200681}
682
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300683static int add_event(struct pevent *pevent, struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200684{
685 int i;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300686 struct event_format **events = realloc(pevent->events, sizeof(event) *
687 (pevent->nr_events + 1));
688 if (!events)
689 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200690
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300691 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200692
693 for (i = 0; i < pevent->nr_events; i++) {
694 if (pevent->events[i]->id > event->id)
695 break;
696 }
697 if (i < pevent->nr_events)
698 memmove(&pevent->events[i + 1],
699 &pevent->events[i],
700 sizeof(event) * (pevent->nr_events - i));
701
702 pevent->events[i] = event;
703 pevent->nr_events++;
704
705 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300706
707 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200708}
709
710static int event_item_type(enum event_type type)
711{
712 switch (type) {
713 case EVENT_ITEM ... EVENT_SQUOTE:
714 return 1;
715 case EVENT_ERROR ... EVENT_DELIM:
716 default:
717 return 0;
718 }
719}
720
721static void free_flag_sym(struct print_flag_sym *fsym)
722{
723 struct print_flag_sym *next;
724
725 while (fsym) {
726 next = fsym->next;
727 free(fsym->value);
728 free(fsym->str);
729 free(fsym);
730 fsym = next;
731 }
732}
733
734static void free_arg(struct print_arg *arg)
735{
736 struct print_arg *farg;
737
738 if (!arg)
739 return;
740
741 switch (arg->type) {
742 case PRINT_ATOM:
743 free(arg->atom.atom);
744 break;
745 case PRINT_FIELD:
746 free(arg->field.name);
747 break;
748 case PRINT_FLAGS:
749 free_arg(arg->flags.field);
750 free(arg->flags.delim);
751 free_flag_sym(arg->flags.flags);
752 break;
753 case PRINT_SYMBOL:
754 free_arg(arg->symbol.field);
755 free_flag_sym(arg->symbol.symbols);
756 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900757 case PRINT_HEX:
758 free_arg(arg->hex.field);
759 free_arg(arg->hex.size);
760 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200761 case PRINT_TYPE:
762 free(arg->typecast.type);
763 free_arg(arg->typecast.item);
764 break;
765 case PRINT_STRING:
766 case PRINT_BSTRING:
767 free(arg->string.string);
768 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400769 case PRINT_BITMASK:
770 free(arg->bitmask.bitmask);
771 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200772 case PRINT_DYNAMIC_ARRAY:
773 free(arg->dynarray.index);
774 break;
775 case PRINT_OP:
776 free(arg->op.op);
777 free_arg(arg->op.left);
778 free_arg(arg->op.right);
779 break;
780 case PRINT_FUNC:
781 while (arg->func.args) {
782 farg = arg->func.args;
783 arg->func.args = farg->next;
784 free_arg(farg);
785 }
786 break;
787
788 case PRINT_NULL:
789 default:
790 break;
791 }
792
793 free(arg);
794}
795
796static enum event_type get_type(int ch)
797{
798 if (ch == '\n')
799 return EVENT_NEWLINE;
800 if (isspace(ch))
801 return EVENT_SPACE;
802 if (isalnum(ch) || ch == '_')
803 return EVENT_ITEM;
804 if (ch == '\'')
805 return EVENT_SQUOTE;
806 if (ch == '"')
807 return EVENT_DQUOTE;
808 if (!isprint(ch))
809 return EVENT_NONE;
810 if (ch == '(' || ch == ')' || ch == ',')
811 return EVENT_DELIM;
812
813 return EVENT_OP;
814}
815
816static int __read_char(void)
817{
818 if (input_buf_ptr >= input_buf_siz)
819 return -1;
820
821 return input_buf[input_buf_ptr++];
822}
823
824static int __peek_char(void)
825{
826 if (input_buf_ptr >= input_buf_siz)
827 return -1;
828
829 return input_buf[input_buf_ptr];
830}
831
832/**
833 * pevent_peek_char - peek at the next character that will be read
834 *
835 * Returns the next character read, or -1 if end of buffer.
836 */
837int pevent_peek_char(void)
838{
839 return __peek_char();
840}
841
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900842static int extend_token(char **tok, char *buf, int size)
843{
844 char *newtok = realloc(*tok, size);
845
846 if (!newtok) {
847 free(*tok);
848 *tok = NULL;
849 return -1;
850 }
851
852 if (!*tok)
853 strcpy(newtok, buf);
854 else
855 strcat(newtok, buf);
856 *tok = newtok;
857
858 return 0;
859}
860
Steven Rostedtf7d82352012-04-06 00:47:53 +0200861static enum event_type force_token(const char *str, char **tok);
862
863static enum event_type __read_token(char **tok)
864{
865 char buf[BUFSIZ];
866 int ch, last_ch, quote_ch, next_ch;
867 int i = 0;
868 int tok_size = 0;
869 enum event_type type;
870
871 *tok = NULL;
872
873
874 ch = __read_char();
875 if (ch < 0)
876 return EVENT_NONE;
877
878 type = get_type(ch);
879 if (type == EVENT_NONE)
880 return type;
881
882 buf[i++] = ch;
883
884 switch (type) {
885 case EVENT_NEWLINE:
886 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300887 if (asprintf(tok, "%c", ch) < 0)
888 return EVENT_ERROR;
889
Steven Rostedtf7d82352012-04-06 00:47:53 +0200890 return type;
891
892 case EVENT_OP:
893 switch (ch) {
894 case '-':
895 next_ch = __peek_char();
896 if (next_ch == '>') {
897 buf[i++] = __read_char();
898 break;
899 }
900 /* fall through */
901 case '+':
902 case '|':
903 case '&':
904 case '>':
905 case '<':
906 last_ch = ch;
907 ch = __peek_char();
908 if (ch != last_ch)
909 goto test_equal;
910 buf[i++] = __read_char();
911 switch (last_ch) {
912 case '>':
913 case '<':
914 goto test_equal;
915 default:
916 break;
917 }
918 break;
919 case '!':
920 case '=':
921 goto test_equal;
922 default: /* what should we do instead? */
923 break;
924 }
925 buf[i] = 0;
926 *tok = strdup(buf);
927 return type;
928
929 test_equal:
930 ch = __peek_char();
931 if (ch == '=')
932 buf[i++] = __read_char();
933 goto out;
934
935 case EVENT_DQUOTE:
936 case EVENT_SQUOTE:
937 /* don't keep quotes */
938 i--;
939 quote_ch = ch;
940 last_ch = 0;
941 concat:
942 do {
943 if (i == (BUFSIZ - 1)) {
944 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200945 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900946
947 if (extend_token(tok, buf, tok_size) < 0)
948 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200949 i = 0;
950 }
951 last_ch = ch;
952 ch = __read_char();
953 buf[i++] = ch;
954 /* the '\' '\' will cancel itself */
955 if (ch == '\\' && last_ch == '\\')
956 last_ch = 0;
957 } while (ch != quote_ch || last_ch == '\\');
958 /* remove the last quote */
959 i--;
960
961 /*
962 * For strings (double quotes) check the next token.
963 * If it is another string, concatinate the two.
964 */
965 if (type == EVENT_DQUOTE) {
966 unsigned long long save_input_buf_ptr = input_buf_ptr;
967
968 do {
969 ch = __read_char();
970 } while (isspace(ch));
971 if (ch == '"')
972 goto concat;
973 input_buf_ptr = save_input_buf_ptr;
974 }
975
976 goto out;
977
978 case EVENT_ERROR ... EVENT_SPACE:
979 case EVENT_ITEM:
980 default:
981 break;
982 }
983
984 while (get_type(__peek_char()) == type) {
985 if (i == (BUFSIZ - 1)) {
986 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200987 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900988
989 if (extend_token(tok, buf, tok_size) < 0)
990 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200991 i = 0;
992 }
993 ch = __read_char();
994 buf[i++] = ch;
995 }
996
997 out:
998 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900999 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001000 return EVENT_NONE;
1001
1002 if (type == EVENT_ITEM) {
1003 /*
1004 * Older versions of the kernel has a bug that
1005 * creates invalid symbols and will break the mac80211
1006 * parsing. This is a work around to that bug.
1007 *
1008 * See Linux kernel commit:
1009 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1010 */
1011 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1012 free(*tok);
1013 *tok = NULL;
1014 return force_token("\"\%s\" ", tok);
1015 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1016 free(*tok);
1017 *tok = NULL;
1018 return force_token("\" sta:%pM\" ", tok);
1019 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1020 free(*tok);
1021 *tok = NULL;
1022 return force_token("\" vif:%p(%d)\" ", tok);
1023 }
1024 }
1025
1026 return type;
1027}
1028
1029static enum event_type force_token(const char *str, char **tok)
1030{
1031 const char *save_input_buf;
1032 unsigned long long save_input_buf_ptr;
1033 unsigned long long save_input_buf_siz;
1034 enum event_type type;
1035
1036 /* save off the current input pointers */
1037 save_input_buf = input_buf;
1038 save_input_buf_ptr = input_buf_ptr;
1039 save_input_buf_siz = input_buf_siz;
1040
1041 init_input_buf(str, strlen(str));
1042
1043 type = __read_token(tok);
1044
1045 /* reset back to original token */
1046 input_buf = save_input_buf;
1047 input_buf_ptr = save_input_buf_ptr;
1048 input_buf_siz = save_input_buf_siz;
1049
1050 return type;
1051}
1052
1053static void free_token(char *tok)
1054{
1055 if (tok)
1056 free(tok);
1057}
1058
1059static enum event_type read_token(char **tok)
1060{
1061 enum event_type type;
1062
1063 for (;;) {
1064 type = __read_token(tok);
1065 if (type != EVENT_SPACE)
1066 return type;
1067
1068 free_token(*tok);
1069 }
1070
1071 /* not reached */
1072 *tok = NULL;
1073 return EVENT_NONE;
1074}
1075
1076/**
1077 * pevent_read_token - access to utilites to use the pevent parser
1078 * @tok: The token to return
1079 *
1080 * This will parse tokens from the string given by
1081 * pevent_init_data().
1082 *
1083 * Returns the token type.
1084 */
1085enum event_type pevent_read_token(char **tok)
1086{
1087 return read_token(tok);
1088}
1089
1090/**
1091 * pevent_free_token - free a token returned by pevent_read_token
1092 * @token: the token to free
1093 */
1094void pevent_free_token(char *token)
1095{
1096 free_token(token);
1097}
1098
1099/* no newline */
1100static enum event_type read_token_item(char **tok)
1101{
1102 enum event_type type;
1103
1104 for (;;) {
1105 type = __read_token(tok);
1106 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1107 return type;
1108 free_token(*tok);
1109 *tok = NULL;
1110 }
1111
1112 /* not reached */
1113 *tok = NULL;
1114 return EVENT_NONE;
1115}
1116
1117static int test_type(enum event_type type, enum event_type expect)
1118{
1119 if (type != expect) {
1120 do_warning("Error: expected type %d but read %d",
1121 expect, type);
1122 return -1;
1123 }
1124 return 0;
1125}
1126
1127static int test_type_token(enum event_type type, const char *token,
1128 enum event_type expect, const char *expect_tok)
1129{
1130 if (type != expect) {
1131 do_warning("Error: expected type %d but read %d",
1132 expect, type);
1133 return -1;
1134 }
1135
1136 if (strcmp(token, expect_tok) != 0) {
1137 do_warning("Error: expected '%s' but read '%s'",
1138 expect_tok, token);
1139 return -1;
1140 }
1141 return 0;
1142}
1143
1144static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1145{
1146 enum event_type type;
1147
1148 if (newline_ok)
1149 type = read_token(tok);
1150 else
1151 type = read_token_item(tok);
1152 return test_type(type, expect);
1153}
1154
1155static int read_expect_type(enum event_type expect, char **tok)
1156{
1157 return __read_expect_type(expect, tok, 1);
1158}
1159
1160static int __read_expected(enum event_type expect, const char *str,
1161 int newline_ok)
1162{
1163 enum event_type type;
1164 char *token;
1165 int ret;
1166
1167 if (newline_ok)
1168 type = read_token(&token);
1169 else
1170 type = read_token_item(&token);
1171
1172 ret = test_type_token(type, token, expect, str);
1173
1174 free_token(token);
1175
1176 return ret;
1177}
1178
1179static int read_expected(enum event_type expect, const char *str)
1180{
1181 return __read_expected(expect, str, 1);
1182}
1183
1184static int read_expected_item(enum event_type expect, const char *str)
1185{
1186 return __read_expected(expect, str, 0);
1187}
1188
1189static char *event_read_name(void)
1190{
1191 char *token;
1192
1193 if (read_expected(EVENT_ITEM, "name") < 0)
1194 return NULL;
1195
1196 if (read_expected(EVENT_OP, ":") < 0)
1197 return NULL;
1198
1199 if (read_expect_type(EVENT_ITEM, &token) < 0)
1200 goto fail;
1201
1202 return token;
1203
1204 fail:
1205 free_token(token);
1206 return NULL;
1207}
1208
1209static int event_read_id(void)
1210{
1211 char *token;
1212 int id;
1213
1214 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1215 return -1;
1216
1217 if (read_expected(EVENT_OP, ":") < 0)
1218 return -1;
1219
1220 if (read_expect_type(EVENT_ITEM, &token) < 0)
1221 goto fail;
1222
1223 id = strtoul(token, NULL, 0);
1224 free_token(token);
1225 return id;
1226
1227 fail:
1228 free_token(token);
1229 return -1;
1230}
1231
1232static int field_is_string(struct format_field *field)
1233{
1234 if ((field->flags & FIELD_IS_ARRAY) &&
1235 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1236 strstr(field->type, "s8")))
1237 return 1;
1238
1239 return 0;
1240}
1241
1242static int field_is_dynamic(struct format_field *field)
1243{
1244 if (strncmp(field->type, "__data_loc", 10) == 0)
1245 return 1;
1246
1247 return 0;
1248}
1249
1250static int field_is_long(struct format_field *field)
1251{
1252 /* includes long long */
1253 if (strstr(field->type, "long"))
1254 return 1;
1255
1256 return 0;
1257}
1258
Jiri Olsae23c1a52013-01-24 21:46:43 +01001259static unsigned int type_size(const char *name)
1260{
1261 /* This covers all FIELD_IS_STRING types. */
1262 static struct {
1263 const char *type;
1264 unsigned int size;
1265 } table[] = {
1266 { "u8", 1 },
1267 { "u16", 2 },
1268 { "u32", 4 },
1269 { "u64", 8 },
1270 { "s8", 1 },
1271 { "s16", 2 },
1272 { "s32", 4 },
1273 { "s64", 8 },
1274 { "char", 1 },
1275 { },
1276 };
1277 int i;
1278
1279 for (i = 0; table[i].type; i++) {
1280 if (!strcmp(table[i].type, name))
1281 return table[i].size;
1282 }
1283
1284 return 0;
1285}
1286
Steven Rostedtf7d82352012-04-06 00:47:53 +02001287static int event_read_fields(struct event_format *event, struct format_field **fields)
1288{
1289 struct format_field *field = NULL;
1290 enum event_type type;
1291 char *token;
1292 char *last_token;
1293 int count = 0;
1294
1295 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001296 unsigned int size_dynamic = 0;
1297
Steven Rostedtf7d82352012-04-06 00:47:53 +02001298 type = read_token(&token);
1299 if (type == EVENT_NEWLINE) {
1300 free_token(token);
1301 return count;
1302 }
1303
1304 count++;
1305
1306 if (test_type_token(type, token, EVENT_ITEM, "field"))
1307 goto fail;
1308 free_token(token);
1309
1310 type = read_token(&token);
1311 /*
1312 * The ftrace fields may still use the "special" name.
1313 * Just ignore it.
1314 */
1315 if (event->flags & EVENT_FL_ISFTRACE &&
1316 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1317 free_token(token);
1318 type = read_token(&token);
1319 }
1320
1321 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1322 goto fail;
1323
1324 free_token(token);
1325 if (read_expect_type(EVENT_ITEM, &token) < 0)
1326 goto fail;
1327
1328 last_token = token;
1329
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001330 field = calloc(1, sizeof(*field));
1331 if (!field)
1332 goto fail;
1333
Steven Rostedtf7d82352012-04-06 00:47:53 +02001334 field->event = event;
1335
1336 /* read the rest of the type */
1337 for (;;) {
1338 type = read_token(&token);
1339 if (type == EVENT_ITEM ||
1340 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1341 /*
1342 * Some of the ftrace fields are broken and have
1343 * an illegal "." in them.
1344 */
1345 (event->flags & EVENT_FL_ISFTRACE &&
1346 type == EVENT_OP && strcmp(token, ".") == 0)) {
1347
1348 if (strcmp(token, "*") == 0)
1349 field->flags |= FIELD_IS_POINTER;
1350
1351 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001352 char *new_type;
1353 new_type = realloc(field->type,
1354 strlen(field->type) +
1355 strlen(last_token) + 2);
1356 if (!new_type) {
1357 free(last_token);
1358 goto fail;
1359 }
1360 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001361 strcat(field->type, " ");
1362 strcat(field->type, last_token);
1363 free(last_token);
1364 } else
1365 field->type = last_token;
1366 last_token = token;
1367 continue;
1368 }
1369
1370 break;
1371 }
1372
1373 if (!field->type) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001374 do_warning_event(event, "%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001375 goto fail;
1376 }
1377 field->name = last_token;
1378
1379 if (test_type(type, EVENT_OP))
1380 goto fail;
1381
1382 if (strcmp(token, "[") == 0) {
1383 enum event_type last_type = type;
1384 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001385 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001386 int len;
1387
1388 field->flags |= FIELD_IS_ARRAY;
1389
1390 type = read_token(&token);
1391
1392 if (type == EVENT_ITEM)
1393 field->arraylen = strtoul(token, NULL, 0);
1394 else
1395 field->arraylen = 0;
1396
1397 while (strcmp(token, "]") != 0) {
1398 if (last_type == EVENT_ITEM &&
1399 type == EVENT_ITEM)
1400 len = 2;
1401 else
1402 len = 1;
1403 last_type = type;
1404
Namhyung Kimd2864472012-04-09 11:54:33 +09001405 new_brackets = realloc(brackets,
1406 strlen(brackets) +
1407 strlen(token) + len);
1408 if (!new_brackets) {
1409 free(brackets);
1410 goto fail;
1411 }
1412 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001413 if (len == 2)
1414 strcat(brackets, " ");
1415 strcat(brackets, token);
1416 /* We only care about the last token */
1417 field->arraylen = strtoul(token, NULL, 0);
1418 free_token(token);
1419 type = read_token(&token);
1420 if (type == EVENT_NONE) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001421 do_warning_event(event, "failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001422 goto fail;
1423 }
1424 }
1425
1426 free_token(token);
1427
Namhyung Kimd2864472012-04-09 11:54:33 +09001428 new_brackets = realloc(brackets, strlen(brackets) + 2);
1429 if (!new_brackets) {
1430 free(brackets);
1431 goto fail;
1432 }
1433 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001434 strcat(brackets, "]");
1435
1436 /* add brackets to type */
1437
1438 type = read_token(&token);
1439 /*
1440 * If the next token is not an OP, then it is of
1441 * the format: type [] item;
1442 */
1443 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001444 char *new_type;
1445 new_type = realloc(field->type,
1446 strlen(field->type) +
1447 strlen(field->name) +
1448 strlen(brackets) + 2);
1449 if (!new_type) {
1450 free(brackets);
1451 goto fail;
1452 }
1453 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001454 strcat(field->type, " ");
1455 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001456 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001457 free_token(field->name);
1458 strcat(field->type, brackets);
1459 field->name = token;
1460 type = read_token(&token);
1461 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001462 char *new_type;
1463 new_type = realloc(field->type,
1464 strlen(field->type) +
1465 strlen(brackets) + 1);
1466 if (!new_type) {
1467 free(brackets);
1468 goto fail;
1469 }
1470 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001471 strcat(field->type, brackets);
1472 }
1473 free(brackets);
1474 }
1475
1476 if (field_is_string(field))
1477 field->flags |= FIELD_IS_STRING;
1478 if (field_is_dynamic(field))
1479 field->flags |= FIELD_IS_DYNAMIC;
1480 if (field_is_long(field))
1481 field->flags |= FIELD_IS_LONG;
1482
1483 if (test_type_token(type, token, EVENT_OP, ";"))
1484 goto fail;
1485 free_token(token);
1486
1487 if (read_expected(EVENT_ITEM, "offset") < 0)
1488 goto fail_expect;
1489
1490 if (read_expected(EVENT_OP, ":") < 0)
1491 goto fail_expect;
1492
1493 if (read_expect_type(EVENT_ITEM, &token))
1494 goto fail;
1495 field->offset = strtoul(token, NULL, 0);
1496 free_token(token);
1497
1498 if (read_expected(EVENT_OP, ";") < 0)
1499 goto fail_expect;
1500
1501 if (read_expected(EVENT_ITEM, "size") < 0)
1502 goto fail_expect;
1503
1504 if (read_expected(EVENT_OP, ":") < 0)
1505 goto fail_expect;
1506
1507 if (read_expect_type(EVENT_ITEM, &token))
1508 goto fail;
1509 field->size = strtoul(token, NULL, 0);
1510 free_token(token);
1511
1512 if (read_expected(EVENT_OP, ";") < 0)
1513 goto fail_expect;
1514
1515 type = read_token(&token);
1516 if (type != EVENT_NEWLINE) {
1517 /* newer versions of the kernel have a "signed" type */
1518 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1519 goto fail;
1520
1521 free_token(token);
1522
1523 if (read_expected(EVENT_OP, ":") < 0)
1524 goto fail_expect;
1525
1526 if (read_expect_type(EVENT_ITEM, &token))
1527 goto fail;
1528
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001529 if (strtoul(token, NULL, 0))
1530 field->flags |= FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001531
1532 free_token(token);
1533 if (read_expected(EVENT_OP, ";") < 0)
1534 goto fail_expect;
1535
1536 if (read_expect_type(EVENT_NEWLINE, &token))
1537 goto fail;
1538 }
1539
1540 free_token(token);
1541
1542 if (field->flags & FIELD_IS_ARRAY) {
1543 if (field->arraylen)
1544 field->elementsize = field->size / field->arraylen;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001545 else if (field->flags & FIELD_IS_DYNAMIC)
1546 field->elementsize = size_dynamic;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001547 else if (field->flags & FIELD_IS_STRING)
1548 field->elementsize = 1;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001549 else if (field->flags & FIELD_IS_LONG)
1550 field->elementsize = event->pevent ?
1551 event->pevent->long_size :
1552 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001553 } else
1554 field->elementsize = field->size;
1555
1556 *fields = field;
1557 fields = &field->next;
1558
1559 } while (1);
1560
1561 return 0;
1562
1563fail:
1564 free_token(token);
1565fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001566 if (field) {
1567 free(field->type);
1568 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001569 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001570 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001571 return -1;
1572}
1573
1574static int event_read_format(struct event_format *event)
1575{
1576 char *token;
1577 int ret;
1578
1579 if (read_expected_item(EVENT_ITEM, "format") < 0)
1580 return -1;
1581
1582 if (read_expected(EVENT_OP, ":") < 0)
1583 return -1;
1584
1585 if (read_expect_type(EVENT_NEWLINE, &token))
1586 goto fail;
1587 free_token(token);
1588
1589 ret = event_read_fields(event, &event->format.common_fields);
1590 if (ret < 0)
1591 return ret;
1592 event->format.nr_common = ret;
1593
1594 ret = event_read_fields(event, &event->format.fields);
1595 if (ret < 0)
1596 return ret;
1597 event->format.nr_fields = ret;
1598
1599 return 0;
1600
1601 fail:
1602 free_token(token);
1603 return -1;
1604}
1605
1606static enum event_type
1607process_arg_token(struct event_format *event, struct print_arg *arg,
1608 char **tok, enum event_type type);
1609
1610static enum event_type
1611process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1612{
1613 enum event_type type;
1614 char *token;
1615
1616 type = read_token(&token);
1617 *tok = token;
1618
1619 return process_arg_token(event, arg, tok, type);
1620}
1621
1622static enum event_type
1623process_op(struct event_format *event, struct print_arg *arg, char **tok);
1624
Steven Rostedteff2c922013-11-18 14:23:14 -05001625/*
1626 * For __print_symbolic() and __print_flags, we need to completely
1627 * evaluate the first argument, which defines what to print next.
1628 */
1629static enum event_type
1630process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1631{
1632 enum event_type type;
1633
1634 type = process_arg(event, arg, tok);
1635
1636 while (type == EVENT_OP) {
1637 type = process_op(event, arg, tok);
1638 }
1639
1640 return type;
1641}
1642
Steven Rostedtf7d82352012-04-06 00:47:53 +02001643static enum event_type
1644process_cond(struct event_format *event, struct print_arg *top, char **tok)
1645{
1646 struct print_arg *arg, *left, *right;
1647 enum event_type type;
1648 char *token = NULL;
1649
1650 arg = alloc_arg();
1651 left = alloc_arg();
1652 right = alloc_arg();
1653
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001654 if (!arg || !left || !right) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001655 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001656 /* arg will be freed at out_free */
1657 free_arg(left);
1658 free_arg(right);
1659 goto out_free;
1660 }
1661
Steven Rostedtf7d82352012-04-06 00:47:53 +02001662 arg->type = PRINT_OP;
1663 arg->op.left = left;
1664 arg->op.right = right;
1665
1666 *tok = NULL;
1667 type = process_arg(event, left, &token);
1668
1669 again:
1670 /* Handle other operations in the arguments */
1671 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1672 type = process_op(event, left, &token);
1673 goto again;
1674 }
1675
1676 if (test_type_token(type, token, EVENT_OP, ":"))
1677 goto out_free;
1678
1679 arg->op.op = token;
1680
1681 type = process_arg(event, right, &token);
1682
1683 top->op.right = arg;
1684
1685 *tok = token;
1686 return type;
1687
1688out_free:
1689 /* Top may point to itself */
1690 top->op.right = NULL;
1691 free_token(token);
1692 free_arg(arg);
1693 return EVENT_ERROR;
1694}
1695
1696static enum event_type
1697process_array(struct event_format *event, struct print_arg *top, char **tok)
1698{
1699 struct print_arg *arg;
1700 enum event_type type;
1701 char *token = NULL;
1702
1703 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001704 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001705 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001706 /* '*tok' is set to top->op.op. No need to free. */
1707 *tok = NULL;
1708 return EVENT_ERROR;
1709 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001710
1711 *tok = NULL;
1712 type = process_arg(event, arg, &token);
1713 if (test_type_token(type, token, EVENT_OP, "]"))
1714 goto out_free;
1715
1716 top->op.right = arg;
1717
1718 free_token(token);
1719 type = read_token_item(&token);
1720 *tok = token;
1721
1722 return type;
1723
1724out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001725 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001726 free_arg(arg);
1727 return EVENT_ERROR;
1728}
1729
1730static int get_op_prio(char *op)
1731{
1732 if (!op[1]) {
1733 switch (op[0]) {
1734 case '~':
1735 case '!':
1736 return 4;
1737 case '*':
1738 case '/':
1739 case '%':
1740 return 6;
1741 case '+':
1742 case '-':
1743 return 7;
1744 /* '>>' and '<<' are 8 */
1745 case '<':
1746 case '>':
1747 return 9;
1748 /* '==' and '!=' are 10 */
1749 case '&':
1750 return 11;
1751 case '^':
1752 return 12;
1753 case '|':
1754 return 13;
1755 case '?':
1756 return 16;
1757 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001758 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001759 return -1;
1760 }
1761 } else {
1762 if (strcmp(op, "++") == 0 ||
1763 strcmp(op, "--") == 0) {
1764 return 3;
1765 } else if (strcmp(op, ">>") == 0 ||
1766 strcmp(op, "<<") == 0) {
1767 return 8;
1768 } else if (strcmp(op, ">=") == 0 ||
1769 strcmp(op, "<=") == 0) {
1770 return 9;
1771 } else if (strcmp(op, "==") == 0 ||
1772 strcmp(op, "!=") == 0) {
1773 return 10;
1774 } else if (strcmp(op, "&&") == 0) {
1775 return 14;
1776 } else if (strcmp(op, "||") == 0) {
1777 return 15;
1778 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001779 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001780 return -1;
1781 }
1782 }
1783}
1784
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001785static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001786{
1787
1788 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001789 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001790 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001791 else
1792 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001793
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001794 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001795}
1796
1797/* Note, *tok does not get freed, but will most likely be saved */
1798static enum event_type
1799process_op(struct event_format *event, struct print_arg *arg, char **tok)
1800{
1801 struct print_arg *left, *right = NULL;
1802 enum event_type type;
1803 char *token;
1804
1805 /* the op is passed in via tok */
1806 token = *tok;
1807
1808 if (arg->type == PRINT_OP && !arg->op.left) {
1809 /* handle single op */
1810 if (token[1]) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001811 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001812 goto out_free;
1813 }
1814 switch (token[0]) {
1815 case '~':
1816 case '!':
1817 case '+':
1818 case '-':
1819 break;
1820 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001821 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001822 goto out_free;
1823
1824 }
1825
1826 /* make an empty left */
1827 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001828 if (!left)
1829 goto out_warn_free;
1830
Steven Rostedtf7d82352012-04-06 00:47:53 +02001831 left->type = PRINT_NULL;
1832 arg->op.left = left;
1833
1834 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001835 if (!right)
1836 goto out_warn_free;
1837
Steven Rostedtf7d82352012-04-06 00:47:53 +02001838 arg->op.right = right;
1839
1840 /* do not free the token, it belongs to an op */
1841 *tok = NULL;
1842 type = process_arg(event, right, tok);
1843
1844 } else if (strcmp(token, "?") == 0) {
1845
1846 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001847 if (!left)
1848 goto out_warn_free;
1849
Steven Rostedtf7d82352012-04-06 00:47:53 +02001850 /* copy the top arg to the left */
1851 *left = *arg;
1852
1853 arg->type = PRINT_OP;
1854 arg->op.op = token;
1855 arg->op.left = left;
1856 arg->op.prio = 0;
1857
Namhyung Kim41e51a22012-09-19 15:58:42 +09001858 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001859 type = process_cond(event, arg, tok);
1860
1861 } else if (strcmp(token, ">>") == 0 ||
1862 strcmp(token, "<<") == 0 ||
1863 strcmp(token, "&") == 0 ||
1864 strcmp(token, "|") == 0 ||
1865 strcmp(token, "&&") == 0 ||
1866 strcmp(token, "||") == 0 ||
1867 strcmp(token, "-") == 0 ||
1868 strcmp(token, "+") == 0 ||
1869 strcmp(token, "*") == 0 ||
1870 strcmp(token, "^") == 0 ||
1871 strcmp(token, "/") == 0 ||
1872 strcmp(token, "<") == 0 ||
1873 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001874 strcmp(token, "<=") == 0 ||
1875 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001876 strcmp(token, "==") == 0 ||
1877 strcmp(token, "!=") == 0) {
1878
1879 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001880 if (!left)
1881 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001882
1883 /* copy the top arg to the left */
1884 *left = *arg;
1885
1886 arg->type = PRINT_OP;
1887 arg->op.op = token;
1888 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001889 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001890
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001891 if (set_op_prio(arg) == -1) {
1892 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001893 /* arg->op.op (= token) will be freed at out_free */
1894 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001895 goto out_free;
1896 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001897
1898 type = read_token_item(&token);
1899 *tok = token;
1900
1901 /* could just be a type pointer */
1902 if ((strcmp(arg->op.op, "*") == 0) &&
1903 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001904 char *new_atom;
1905
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001906 if (left->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001907 do_warning_event(event, "bad pointer type");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001908 goto out_free;
1909 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001910 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001911 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001912 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001913 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001914
1915 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001916 strcat(left->atom.atom, " *");
1917 free(arg->op.op);
1918 *arg = *left;
1919 free(left);
1920
1921 return type;
1922 }
1923
1924 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001925 if (!right)
1926 goto out_warn_free;
1927
Steven Rostedtf7d82352012-04-06 00:47:53 +02001928 type = process_arg_token(event, right, tok, type);
1929 arg->op.right = right;
1930
1931 } else if (strcmp(token, "[") == 0) {
1932
1933 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001934 if (!left)
1935 goto out_warn_free;
1936
Steven Rostedtf7d82352012-04-06 00:47:53 +02001937 *left = *arg;
1938
1939 arg->type = PRINT_OP;
1940 arg->op.op = token;
1941 arg->op.left = left;
1942
1943 arg->op.prio = 0;
1944
Namhyung Kim41e51a22012-09-19 15:58:42 +09001945 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001946 type = process_array(event, arg, tok);
1947
1948 } else {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001949 do_warning_event(event, "unknown op '%s'", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001950 event->flags |= EVENT_FL_FAILED;
1951 /* the arg is now the left side */
1952 goto out_free;
1953 }
1954
1955 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1956 int prio;
1957
1958 /* higher prios need to be closer to the root */
1959 prio = get_op_prio(*tok);
1960
1961 if (prio > arg->op.prio)
1962 return process_op(event, arg, tok);
1963
1964 return process_op(event, right, tok);
1965 }
1966
1967 return type;
1968
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001969out_warn_free:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001970 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001971out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001972 free_token(token);
1973 *tok = NULL;
1974 return EVENT_ERROR;
1975}
1976
1977static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001978process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001979 char **tok)
1980{
1981 enum event_type type;
1982 char *field;
1983 char *token;
1984
1985 if (read_expected(EVENT_OP, "->") < 0)
1986 goto out_err;
1987
1988 if (read_expect_type(EVENT_ITEM, &token) < 0)
1989 goto out_free;
1990 field = token;
1991
1992 arg->type = PRINT_FIELD;
1993 arg->field.name = field;
1994
Tom Zanussi5205aec2012-04-06 00:47:58 +02001995 if (is_flag_field) {
1996 arg->field.field = pevent_find_any_field(event, arg->field.name);
1997 arg->field.field->flags |= FIELD_IS_FLAG;
1998 is_flag_field = 0;
1999 } else if (is_symbolic_field) {
2000 arg->field.field = pevent_find_any_field(event, arg->field.name);
2001 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2002 is_symbolic_field = 0;
2003 }
2004
Steven Rostedtf7d82352012-04-06 00:47:53 +02002005 type = read_token(&token);
2006 *tok = token;
2007
2008 return type;
2009
2010 out_free:
2011 free_token(token);
2012 out_err:
2013 *tok = NULL;
2014 return EVENT_ERROR;
2015}
2016
2017static char *arg_eval (struct print_arg *arg);
2018
2019static unsigned long long
2020eval_type_str(unsigned long long val, const char *type, int pointer)
2021{
2022 int sign = 0;
2023 char *ref;
2024 int len;
2025
2026 len = strlen(type);
2027
2028 if (pointer) {
2029
2030 if (type[len-1] != '*') {
2031 do_warning("pointer expected with non pointer type");
2032 return val;
2033 }
2034
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002035 ref = malloc(len);
2036 if (!ref) {
2037 do_warning("%s: not enough memory!", __func__);
2038 return val;
2039 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002040 memcpy(ref, type, len);
2041
2042 /* chop off the " *" */
2043 ref[len - 2] = 0;
2044
2045 val = eval_type_str(val, ref, 0);
2046 free(ref);
2047 return val;
2048 }
2049
2050 /* check if this is a pointer */
2051 if (type[len - 1] == '*')
2052 return val;
2053
2054 /* Try to figure out the arg size*/
2055 if (strncmp(type, "struct", 6) == 0)
2056 /* all bets off */
2057 return val;
2058
2059 if (strcmp(type, "u8") == 0)
2060 return val & 0xff;
2061
2062 if (strcmp(type, "u16") == 0)
2063 return val & 0xffff;
2064
2065 if (strcmp(type, "u32") == 0)
2066 return val & 0xffffffff;
2067
2068 if (strcmp(type, "u64") == 0 ||
2069 strcmp(type, "s64"))
2070 return val;
2071
2072 if (strcmp(type, "s8") == 0)
2073 return (unsigned long long)(char)val & 0xff;
2074
2075 if (strcmp(type, "s16") == 0)
2076 return (unsigned long long)(short)val & 0xffff;
2077
2078 if (strcmp(type, "s32") == 0)
2079 return (unsigned long long)(int)val & 0xffffffff;
2080
2081 if (strncmp(type, "unsigned ", 9) == 0) {
2082 sign = 0;
2083 type += 9;
2084 }
2085
2086 if (strcmp(type, "char") == 0) {
2087 if (sign)
2088 return (unsigned long long)(char)val & 0xff;
2089 else
2090 return val & 0xff;
2091 }
2092
2093 if (strcmp(type, "short") == 0) {
2094 if (sign)
2095 return (unsigned long long)(short)val & 0xffff;
2096 else
2097 return val & 0xffff;
2098 }
2099
2100 if (strcmp(type, "int") == 0) {
2101 if (sign)
2102 return (unsigned long long)(int)val & 0xffffffff;
2103 else
2104 return val & 0xffffffff;
2105 }
2106
2107 return val;
2108}
2109
2110/*
2111 * Try to figure out the type.
2112 */
2113static unsigned long long
2114eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2115{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002116 if (arg->type != PRINT_TYPE) {
2117 do_warning("expected type argument");
2118 return 0;
2119 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002120
2121 return eval_type_str(val, arg->typecast.type, pointer);
2122}
2123
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002124static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002125{
2126 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002127 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002128
2129 switch (arg->type) {
2130 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002131 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002132 break;
2133 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002134 ret = arg_num_eval(arg->typecast.item, val);
2135 if (!ret)
2136 break;
2137 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002138 break;
2139 case PRINT_OP:
2140 switch (arg->op.op[0]) {
2141 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002142 ret = arg_num_eval(arg->op.left, &left);
2143 if (!ret)
2144 break;
2145 ret = arg_num_eval(arg->op.right, &right);
2146 if (!ret)
2147 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002148 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002149 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002150 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002151 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002152 break;
2153 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002154 ret = arg_num_eval(arg->op.left, &left);
2155 if (!ret)
2156 break;
2157 ret = arg_num_eval(arg->op.right, &right);
2158 if (!ret)
2159 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002160 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002161 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002162 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002163 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002164 break;
2165 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002166 ret = arg_num_eval(arg->op.left, &left);
2167 if (!ret)
2168 break;
2169 ret = arg_num_eval(arg->op.right, &right);
2170 if (!ret)
2171 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002172 switch (arg->op.op[1]) {
2173 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002174 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002175 break;
2176 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002177 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002178 break;
2179 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002180 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002181 break;
2182 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002183 do_warning("unknown op '%s'", arg->op.op);
2184 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002185 }
2186 break;
2187 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002188 ret = arg_num_eval(arg->op.left, &left);
2189 if (!ret)
2190 break;
2191 ret = arg_num_eval(arg->op.right, &right);
2192 if (!ret)
2193 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002194 switch (arg->op.op[1]) {
2195 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002196 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002197 break;
2198 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002199 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002200 break;
2201 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002202 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002203 break;
2204 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002205 do_warning("unknown op '%s'", arg->op.op);
2206 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002207 }
2208 break;
2209 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002210 ret = arg_num_eval(arg->op.left, &left);
2211 if (!ret)
2212 break;
2213 ret = arg_num_eval(arg->op.right, &right);
2214 if (!ret)
2215 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002216
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002217 if (arg->op.op[1] != '=') {
2218 do_warning("unknown op '%s'", arg->op.op);
2219 ret = 0;
2220 } else
2221 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002222 break;
2223 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002224 ret = arg_num_eval(arg->op.left, &left);
2225 if (!ret)
2226 break;
2227 ret = arg_num_eval(arg->op.right, &right);
2228 if (!ret)
2229 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002230
2231 switch (arg->op.op[1]) {
2232 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002233 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002234 break;
2235 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002236 do_warning("unknown op '%s'", arg->op.op);
2237 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002238 }
2239 break;
2240 case '-':
2241 /* check for negative */
2242 if (arg->op.left->type == PRINT_NULL)
2243 left = 0;
2244 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002245 ret = arg_num_eval(arg->op.left, &left);
2246 if (!ret)
2247 break;
2248 ret = arg_num_eval(arg->op.right, &right);
2249 if (!ret)
2250 break;
2251 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002252 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002253 case '+':
2254 if (arg->op.left->type == PRINT_NULL)
2255 left = 0;
2256 else
2257 ret = arg_num_eval(arg->op.left, &left);
2258 if (!ret)
2259 break;
2260 ret = arg_num_eval(arg->op.right, &right);
2261 if (!ret)
2262 break;
2263 *val = left + right;
2264 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002265 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002266 do_warning("unknown op '%s'", arg->op.op);
2267 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002268 }
2269 break;
2270
2271 case PRINT_NULL:
2272 case PRINT_FIELD ... PRINT_SYMBOL:
2273 case PRINT_STRING:
2274 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002275 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002276 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002277 do_warning("invalid eval type %d", arg->type);
2278 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002279
2280 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002281 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002282}
2283
2284static char *arg_eval (struct print_arg *arg)
2285{
2286 long long val;
2287 static char buf[20];
2288
2289 switch (arg->type) {
2290 case PRINT_ATOM:
2291 return arg->atom.atom;
2292 case PRINT_TYPE:
2293 return arg_eval(arg->typecast.item);
2294 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002295 if (!arg_num_eval(arg, &val))
2296 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002297 sprintf(buf, "%lld", val);
2298 return buf;
2299
2300 case PRINT_NULL:
2301 case PRINT_FIELD ... PRINT_SYMBOL:
2302 case PRINT_STRING:
2303 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002304 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002305 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002306 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002307 break;
2308 }
2309
2310 return NULL;
2311}
2312
2313static enum event_type
2314process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2315{
2316 enum event_type type;
2317 struct print_arg *arg = NULL;
2318 struct print_flag_sym *field;
2319 char *token = *tok;
2320 char *value;
2321
2322 do {
2323 free_token(token);
2324 type = read_token_item(&token);
2325 if (test_type_token(type, token, EVENT_OP, "{"))
2326 break;
2327
2328 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002329 if (!arg)
2330 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002331
2332 free_token(token);
2333 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002334
2335 if (type == EVENT_OP)
2336 type = process_op(event, arg, &token);
2337
2338 if (type == EVENT_ERROR)
2339 goto out_free;
2340
Steven Rostedtf7d82352012-04-06 00:47:53 +02002341 if (test_type_token(type, token, EVENT_DELIM, ","))
2342 goto out_free;
2343
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002344 field = calloc(1, sizeof(*field));
2345 if (!field)
2346 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002347
2348 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002349 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002350 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002351 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002352 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002353 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002354
2355 free_arg(arg);
2356 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002357 if (!arg)
2358 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002359
2360 free_token(token);
2361 type = process_arg(event, arg, &token);
2362 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002363 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002364
2365 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002366 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002367 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002368 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002369 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002370 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002371 free_arg(arg);
2372 arg = NULL;
2373
2374 *list = field;
2375 list = &field->next;
2376
2377 free_token(token);
2378 type = read_token_item(&token);
2379 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2380
2381 *tok = token;
2382 return type;
2383
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002384out_free_field:
2385 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002386out_free:
2387 free_arg(arg);
2388 free_token(token);
2389 *tok = NULL;
2390
2391 return EVENT_ERROR;
2392}
2393
2394static enum event_type
2395process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2396{
2397 struct print_arg *field;
2398 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002399 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002400
2401 memset(arg, 0, sizeof(*arg));
2402 arg->type = PRINT_FLAGS;
2403
2404 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002405 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002406 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002407 goto out_free;
2408 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002409
Steven Rostedteff2c922013-11-18 14:23:14 -05002410 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002411
2412 /* Handle operations in the first argument */
2413 while (type == EVENT_OP)
2414 type = process_op(event, field, &token);
2415
2416 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002417 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002418 free_token(token);
2419
2420 arg->flags.field = field;
2421
2422 type = read_token_item(&token);
2423 if (event_item_type(type)) {
2424 arg->flags.delim = token;
2425 type = read_token_item(&token);
2426 }
2427
2428 if (test_type_token(type, token, EVENT_DELIM, ","))
2429 goto out_free;
2430
2431 type = process_fields(event, &arg->flags.flags, &token);
2432 if (test_type_token(type, token, EVENT_DELIM, ")"))
2433 goto out_free;
2434
2435 free_token(token);
2436 type = read_token_item(tok);
2437 return type;
2438
Namhyung Kim70d93042012-09-19 15:58:44 +09002439out_free_field:
2440 free_arg(field);
2441out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002442 free_token(token);
2443 *tok = NULL;
2444 return EVENT_ERROR;
2445}
2446
2447static enum event_type
2448process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2449{
2450 struct print_arg *field;
2451 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002452 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002453
2454 memset(arg, 0, sizeof(*arg));
2455 arg->type = PRINT_SYMBOL;
2456
2457 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002458 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002459 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002460 goto out_free;
2461 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002462
Steven Rostedteff2c922013-11-18 14:23:14 -05002463 type = process_field_arg(event, field, &token);
2464
Steven Rostedtf7d82352012-04-06 00:47:53 +02002465 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002466 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002467
2468 arg->symbol.field = field;
2469
2470 type = process_fields(event, &arg->symbol.symbols, &token);
2471 if (test_type_token(type, token, EVENT_DELIM, ")"))
2472 goto out_free;
2473
2474 free_token(token);
2475 type = read_token_item(tok);
2476 return type;
2477
Namhyung Kim70d93042012-09-19 15:58:44 +09002478out_free_field:
2479 free_arg(field);
2480out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002481 free_token(token);
2482 *tok = NULL;
2483 return EVENT_ERROR;
2484}
2485
2486static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002487process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2488{
2489 struct print_arg *field;
2490 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002491 char *token = NULL;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002492
2493 memset(arg, 0, sizeof(*arg));
2494 arg->type = PRINT_HEX;
2495
2496 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002497 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002498 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002499 goto out_free;
2500 }
2501
Namhyung Kime080e6f2012-06-27 09:41:41 +09002502 type = process_arg(event, field, &token);
2503
2504 if (test_type_token(type, token, EVENT_DELIM, ","))
2505 goto out_free;
2506
2507 arg->hex.field = field;
2508
2509 free_token(token);
2510
2511 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002512 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002513 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002514 *tok = NULL;
2515 return EVENT_ERROR;
2516 }
2517
Namhyung Kime080e6f2012-06-27 09:41:41 +09002518 type = process_arg(event, field, &token);
2519
2520 if (test_type_token(type, token, EVENT_DELIM, ")"))
2521 goto out_free;
2522
2523 arg->hex.size = field;
2524
2525 free_token(token);
2526 type = read_token_item(tok);
2527 return type;
2528
2529 out_free:
2530 free_arg(field);
2531 free_token(token);
2532 *tok = NULL;
2533 return EVENT_ERROR;
2534}
2535
2536static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002537process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2538{
2539 struct format_field *field;
2540 enum event_type type;
2541 char *token;
2542
2543 memset(arg, 0, sizeof(*arg));
2544 arg->type = PRINT_DYNAMIC_ARRAY;
2545
2546 /*
2547 * The item within the parenthesis is another field that holds
2548 * the index into where the array starts.
2549 */
2550 type = read_token(&token);
2551 *tok = token;
2552 if (type != EVENT_ITEM)
2553 goto out_free;
2554
2555 /* Find the field */
2556
2557 field = pevent_find_field(event, token);
2558 if (!field)
2559 goto out_free;
2560
2561 arg->dynarray.field = field;
2562 arg->dynarray.index = 0;
2563
2564 if (read_expected(EVENT_DELIM, ")") < 0)
2565 goto out_free;
2566
2567 free_token(token);
2568 type = read_token_item(&token);
2569 *tok = token;
2570 if (type != EVENT_OP || strcmp(token, "[") != 0)
2571 return type;
2572
2573 free_token(token);
2574 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002575 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002576 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002577 *tok = NULL;
2578 return EVENT_ERROR;
2579 }
2580
Steven Rostedtf7d82352012-04-06 00:47:53 +02002581 type = process_arg(event, arg, &token);
2582 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002583 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002584
2585 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002586 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002587
2588 free_token(token);
2589 type = read_token_item(tok);
2590 return type;
2591
Namhyung Kimb3511d02012-05-23 11:36:50 +09002592 out_free_arg:
2593 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002594 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002595 free_token(token);
2596 *tok = NULL;
2597 return EVENT_ERROR;
2598}
2599
2600static enum event_type
2601process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2602{
2603 struct print_arg *item_arg;
2604 enum event_type type;
2605 char *token;
2606
2607 type = process_arg(event, arg, &token);
2608
2609 if (type == EVENT_ERROR)
2610 goto out_free;
2611
2612 if (type == EVENT_OP)
2613 type = process_op(event, arg, &token);
2614
2615 if (type == EVENT_ERROR)
2616 goto out_free;
2617
2618 if (test_type_token(type, token, EVENT_DELIM, ")"))
2619 goto out_free;
2620
2621 free_token(token);
2622 type = read_token_item(&token);
2623
2624 /*
2625 * If the next token is an item or another open paren, then
2626 * this was a typecast.
2627 */
2628 if (event_item_type(type) ||
2629 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2630
2631 /* make this a typecast and contine */
2632
2633 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002634 if (arg->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002635 do_warning_event(event, "previous needed to be PRINT_ATOM");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002636 goto out_free;
2637 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002638
2639 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002640 if (!item_arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002641 do_warning_event(event, "%s: not enough memory!",
2642 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002643 goto out_free;
2644 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002645
2646 arg->type = PRINT_TYPE;
2647 arg->typecast.type = arg->atom.atom;
2648 arg->typecast.item = item_arg;
2649 type = process_arg_token(event, item_arg, &token, type);
2650
2651 }
2652
2653 *tok = token;
2654 return type;
2655
2656 out_free:
2657 free_token(token);
2658 *tok = NULL;
2659 return EVENT_ERROR;
2660}
2661
2662
2663static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002664process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2665 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002666{
2667 enum event_type type;
2668 char *token;
2669
2670 if (read_expect_type(EVENT_ITEM, &token) < 0)
2671 goto out_free;
2672
2673 arg->type = PRINT_STRING;
2674 arg->string.string = token;
2675 arg->string.offset = -1;
2676
2677 if (read_expected(EVENT_DELIM, ")") < 0)
2678 goto out_err;
2679
2680 type = read_token(&token);
2681 *tok = token;
2682
2683 return type;
2684
2685 out_free:
2686 free_token(token);
2687 out_err:
2688 *tok = NULL;
2689 return EVENT_ERROR;
2690}
2691
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002692static enum event_type
2693process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2694 char **tok)
2695{
2696 enum event_type type;
2697 char *token;
2698
2699 if (read_expect_type(EVENT_ITEM, &token) < 0)
2700 goto out_free;
2701
2702 arg->type = PRINT_BITMASK;
2703 arg->bitmask.bitmask = token;
2704 arg->bitmask.offset = -1;
2705
2706 if (read_expected(EVENT_DELIM, ")") < 0)
2707 goto out_err;
2708
2709 type = read_token(&token);
2710 *tok = token;
2711
2712 return type;
2713
2714 out_free:
2715 free_token(token);
2716 out_err:
2717 *tok = NULL;
2718 return EVENT_ERROR;
2719}
2720
Steven Rostedtf7d82352012-04-06 00:47:53 +02002721static struct pevent_function_handler *
2722find_func_handler(struct pevent *pevent, char *func_name)
2723{
2724 struct pevent_function_handler *func;
2725
Steven Rostedt101782e2012-10-01 20:13:51 -04002726 if (!pevent)
2727 return NULL;
2728
Steven Rostedtf7d82352012-04-06 00:47:53 +02002729 for (func = pevent->func_handlers; func; func = func->next) {
2730 if (strcmp(func->name, func_name) == 0)
2731 break;
2732 }
2733
2734 return func;
2735}
2736
2737static void remove_func_handler(struct pevent *pevent, char *func_name)
2738{
2739 struct pevent_function_handler *func;
2740 struct pevent_function_handler **next;
2741
2742 next = &pevent->func_handlers;
2743 while ((func = *next)) {
2744 if (strcmp(func->name, func_name) == 0) {
2745 *next = func->next;
2746 free_func_handle(func);
2747 break;
2748 }
2749 next = &func->next;
2750 }
2751}
2752
2753static enum event_type
2754process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2755 struct print_arg *arg, char **tok)
2756{
2757 struct print_arg **next_arg;
2758 struct print_arg *farg;
2759 enum event_type type;
2760 char *token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002761 int i;
2762
2763 arg->type = PRINT_FUNC;
2764 arg->func.func = func;
2765
2766 *tok = NULL;
2767
2768 next_arg = &(arg->func.args);
2769 for (i = 0; i < func->nr_args; i++) {
2770 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002771 if (!farg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002772 do_warning_event(event, "%s: not enough memory!",
2773 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002774 return EVENT_ERROR;
2775 }
2776
Steven Rostedtf7d82352012-04-06 00:47:53 +02002777 type = process_arg(event, farg, &token);
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002778 if (i < (func->nr_args - 1)) {
2779 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002780 do_warning_event(event,
2781 "Error: function '%s()' expects %d arguments but event %s only uses %d",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002782 func->name, func->nr_args,
2783 event->name, i + 1);
2784 goto err;
2785 }
2786 } else {
2787 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002788 do_warning_event(event,
2789 "Error: function '%s()' only expects %d arguments but event %s has more",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002790 func->name, func->nr_args, event->name);
2791 goto err;
2792 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002793 }
2794
2795 *next_arg = farg;
2796 next_arg = &(farg->next);
2797 free_token(token);
2798 }
2799
2800 type = read_token(&token);
2801 *tok = token;
2802
2803 return type;
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002804
2805err:
2806 free_arg(farg);
2807 free_token(token);
2808 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002809}
2810
2811static enum event_type
2812process_function(struct event_format *event, struct print_arg *arg,
2813 char *token, char **tok)
2814{
2815 struct pevent_function_handler *func;
2816
2817 if (strcmp(token, "__print_flags") == 0) {
2818 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002819 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002820 return process_flags(event, arg, tok);
2821 }
2822 if (strcmp(token, "__print_symbolic") == 0) {
2823 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002824 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002825 return process_symbols(event, arg, tok);
2826 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002827 if (strcmp(token, "__print_hex") == 0) {
2828 free_token(token);
2829 return process_hex(event, arg, tok);
2830 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002831 if (strcmp(token, "__get_str") == 0) {
2832 free_token(token);
2833 return process_str(event, arg, tok);
2834 }
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002835 if (strcmp(token, "__get_bitmask") == 0) {
2836 free_token(token);
2837 return process_bitmask(event, arg, tok);
2838 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002839 if (strcmp(token, "__get_dynamic_array") == 0) {
2840 free_token(token);
2841 return process_dynamic_array(event, arg, tok);
2842 }
2843
2844 func = find_func_handler(event->pevent, token);
2845 if (func) {
2846 free_token(token);
2847 return process_func_handler(event, func, arg, tok);
2848 }
2849
Namhyung Kim3388cc32014-03-19 10:22:53 +09002850 do_warning_event(event, "function %s not defined", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002851 free_token(token);
2852 return EVENT_ERROR;
2853}
2854
2855static enum event_type
2856process_arg_token(struct event_format *event, struct print_arg *arg,
2857 char **tok, enum event_type type)
2858{
2859 char *token;
2860 char *atom;
2861
2862 token = *tok;
2863
2864 switch (type) {
2865 case EVENT_ITEM:
2866 if (strcmp(token, "REC") == 0) {
2867 free_token(token);
2868 type = process_entry(event, arg, &token);
2869 break;
2870 }
2871 atom = token;
2872 /* test the next token */
2873 type = read_token_item(&token);
2874
2875 /*
2876 * If the next token is a parenthesis, then this
2877 * is a function.
2878 */
2879 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2880 free_token(token);
2881 token = NULL;
2882 /* this will free atom. */
2883 type = process_function(event, arg, atom, &token);
2884 break;
2885 }
2886 /* atoms can be more than one token long */
2887 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002888 char *new_atom;
2889 new_atom = realloc(atom,
2890 strlen(atom) + strlen(token) + 2);
2891 if (!new_atom) {
2892 free(atom);
2893 *tok = NULL;
2894 free_token(token);
2895 return EVENT_ERROR;
2896 }
2897 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002898 strcat(atom, " ");
2899 strcat(atom, token);
2900 free_token(token);
2901 type = read_token_item(&token);
2902 }
2903
2904 arg->type = PRINT_ATOM;
2905 arg->atom.atom = atom;
2906 break;
2907
2908 case EVENT_DQUOTE:
2909 case EVENT_SQUOTE:
2910 arg->type = PRINT_ATOM;
2911 arg->atom.atom = token;
2912 type = read_token_item(&token);
2913 break;
2914 case EVENT_DELIM:
2915 if (strcmp(token, "(") == 0) {
2916 free_token(token);
2917 type = process_paren(event, arg, &token);
2918 break;
2919 }
2920 case EVENT_OP:
2921 /* handle single ops */
2922 arg->type = PRINT_OP;
2923 arg->op.op = token;
2924 arg->op.left = NULL;
2925 type = process_op(event, arg, &token);
2926
2927 /* On error, the op is freed */
2928 if (type == EVENT_ERROR)
2929 arg->op.op = NULL;
2930
2931 /* return error type if errored */
2932 break;
2933
2934 case EVENT_ERROR ... EVENT_NEWLINE:
2935 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09002936 do_warning_event(event, "unexpected type %d", type);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002937 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002938 }
2939 *tok = token;
2940
2941 return type;
2942}
2943
2944static int event_read_print_args(struct event_format *event, struct print_arg **list)
2945{
2946 enum event_type type = EVENT_ERROR;
2947 struct print_arg *arg;
2948 char *token;
2949 int args = 0;
2950
2951 do {
2952 if (type == EVENT_NEWLINE) {
2953 type = read_token_item(&token);
2954 continue;
2955 }
2956
2957 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002958 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002959 do_warning_event(event, "%s: not enough memory!",
2960 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002961 return -1;
2962 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002963
2964 type = process_arg(event, arg, &token);
2965
2966 if (type == EVENT_ERROR) {
2967 free_token(token);
2968 free_arg(arg);
2969 return -1;
2970 }
2971
2972 *list = arg;
2973 args++;
2974
2975 if (type == EVENT_OP) {
2976 type = process_op(event, arg, &token);
2977 free_token(token);
2978 if (type == EVENT_ERROR) {
2979 *list = NULL;
2980 free_arg(arg);
2981 return -1;
2982 }
2983 list = &arg->next;
2984 continue;
2985 }
2986
2987 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2988 free_token(token);
2989 *list = arg;
2990 list = &arg->next;
2991 continue;
2992 }
2993 break;
2994 } while (type != EVENT_NONE);
2995
2996 if (type != EVENT_NONE && type != EVENT_ERROR)
2997 free_token(token);
2998
2999 return args;
3000}
3001
3002static int event_read_print(struct event_format *event)
3003{
3004 enum event_type type;
3005 char *token;
3006 int ret;
3007
3008 if (read_expected_item(EVENT_ITEM, "print") < 0)
3009 return -1;
3010
3011 if (read_expected(EVENT_ITEM, "fmt") < 0)
3012 return -1;
3013
3014 if (read_expected(EVENT_OP, ":") < 0)
3015 return -1;
3016
3017 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3018 goto fail;
3019
3020 concat:
3021 event->print_fmt.format = token;
3022 event->print_fmt.args = NULL;
3023
3024 /* ok to have no arg */
3025 type = read_token_item(&token);
3026
3027 if (type == EVENT_NONE)
3028 return 0;
3029
3030 /* Handle concatenation of print lines */
3031 if (type == EVENT_DQUOTE) {
3032 char *cat;
3033
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003034 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3035 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003036 free_token(token);
3037 free_token(event->print_fmt.format);
3038 event->print_fmt.format = NULL;
3039 token = cat;
3040 goto concat;
3041 }
3042
3043 if (test_type_token(type, token, EVENT_DELIM, ","))
3044 goto fail;
3045
3046 free_token(token);
3047
3048 ret = event_read_print_args(event, &event->print_fmt.args);
3049 if (ret < 0)
3050 return -1;
3051
3052 return ret;
3053
3054 fail:
3055 free_token(token);
3056 return -1;
3057}
3058
3059/**
3060 * pevent_find_common_field - return a common field by event
3061 * @event: handle for the event
3062 * @name: the name of the common field to return
3063 *
3064 * Returns a common field from the event by the given @name.
3065 * This only searchs the common fields and not all field.
3066 */
3067struct format_field *
3068pevent_find_common_field(struct event_format *event, const char *name)
3069{
3070 struct format_field *format;
3071
3072 for (format = event->format.common_fields;
3073 format; format = format->next) {
3074 if (strcmp(format->name, name) == 0)
3075 break;
3076 }
3077
3078 return format;
3079}
3080
3081/**
3082 * pevent_find_field - find a non-common field
3083 * @event: handle for the event
3084 * @name: the name of the non-common field
3085 *
3086 * Returns a non-common field by the given @name.
3087 * This does not search common fields.
3088 */
3089struct format_field *
3090pevent_find_field(struct event_format *event, const char *name)
3091{
3092 struct format_field *format;
3093
3094 for (format = event->format.fields;
3095 format; format = format->next) {
3096 if (strcmp(format->name, name) == 0)
3097 break;
3098 }
3099
3100 return format;
3101}
3102
3103/**
3104 * pevent_find_any_field - find any field by name
3105 * @event: handle for the event
3106 * @name: the name of the field
3107 *
3108 * Returns a field by the given @name.
3109 * This searchs the common field names first, then
3110 * the non-common ones if a common one was not found.
3111 */
3112struct format_field *
3113pevent_find_any_field(struct event_format *event, const char *name)
3114{
3115 struct format_field *format;
3116
3117 format = pevent_find_common_field(event, name);
3118 if (format)
3119 return format;
3120 return pevent_find_field(event, name);
3121}
3122
3123/**
3124 * pevent_read_number - read a number from data
3125 * @pevent: handle for the pevent
3126 * @ptr: the raw data
3127 * @size: the size of the data that holds the number
3128 *
3129 * Returns the number (converted to host) from the
3130 * raw data.
3131 */
3132unsigned long long pevent_read_number(struct pevent *pevent,
3133 const void *ptr, int size)
3134{
3135 switch (size) {
3136 case 1:
3137 return *(unsigned char *)ptr;
3138 case 2:
3139 return data2host2(pevent, ptr);
3140 case 4:
3141 return data2host4(pevent, ptr);
3142 case 8:
3143 return data2host8(pevent, ptr);
3144 default:
3145 /* BUG! */
3146 return 0;
3147 }
3148}
3149
3150/**
3151 * pevent_read_number_field - read a number from data
3152 * @field: a handle to the field
3153 * @data: the raw data to read
3154 * @value: the value to place the number in
3155 *
3156 * Reads raw data according to a field offset and size,
3157 * and translates it into @value.
3158 *
3159 * Returns 0 on success, -1 otherwise.
3160 */
3161int pevent_read_number_field(struct format_field *field, const void *data,
3162 unsigned long long *value)
3163{
3164 if (!field)
3165 return -1;
3166 switch (field->size) {
3167 case 1:
3168 case 2:
3169 case 4:
3170 case 8:
3171 *value = pevent_read_number(field->event->pevent,
3172 data + field->offset, field->size);
3173 return 0;
3174 default:
3175 return -1;
3176 }
3177}
3178
3179static int get_common_info(struct pevent *pevent,
3180 const char *type, int *offset, int *size)
3181{
3182 struct event_format *event;
3183 struct format_field *field;
3184
3185 /*
3186 * All events should have the same common elements.
3187 * Pick any event to find where the type is;
3188 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003189 if (!pevent->events) {
3190 do_warning("no event_list!");
3191 return -1;
3192 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003193
3194 event = pevent->events[0];
3195 field = pevent_find_common_field(event, type);
3196 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003197 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003198
3199 *offset = field->offset;
3200 *size = field->size;
3201
3202 return 0;
3203}
3204
3205static int __parse_common(struct pevent *pevent, void *data,
3206 int *size, int *offset, const char *name)
3207{
3208 int ret;
3209
3210 if (!*size) {
3211 ret = get_common_info(pevent, name, offset, size);
3212 if (ret < 0)
3213 return ret;
3214 }
3215 return pevent_read_number(pevent, data + *offset, *size);
3216}
3217
3218static int trace_parse_common_type(struct pevent *pevent, void *data)
3219{
3220 return __parse_common(pevent, data,
3221 &pevent->type_size, &pevent->type_offset,
3222 "common_type");
3223}
3224
3225static int parse_common_pid(struct pevent *pevent, void *data)
3226{
3227 return __parse_common(pevent, data,
3228 &pevent->pid_size, &pevent->pid_offset,
3229 "common_pid");
3230}
3231
3232static int parse_common_pc(struct pevent *pevent, void *data)
3233{
3234 return __parse_common(pevent, data,
3235 &pevent->pc_size, &pevent->pc_offset,
3236 "common_preempt_count");
3237}
3238
3239static int parse_common_flags(struct pevent *pevent, void *data)
3240{
3241 return __parse_common(pevent, data,
3242 &pevent->flags_size, &pevent->flags_offset,
3243 "common_flags");
3244}
3245
3246static int parse_common_lock_depth(struct pevent *pevent, void *data)
3247{
Steven Rostedt0866a972012-05-22 14:52:40 +09003248 return __parse_common(pevent, data,
3249 &pevent->ld_size, &pevent->ld_offset,
3250 "common_lock_depth");
3251}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003252
Steven Rostedt0866a972012-05-22 14:52:40 +09003253static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3254{
3255 return __parse_common(pevent, data,
3256 &pevent->ld_size, &pevent->ld_offset,
3257 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003258}
3259
3260static int events_id_cmp(const void *a, const void *b);
3261
3262/**
3263 * pevent_find_event - find an event by given id
3264 * @pevent: a handle to the pevent
3265 * @id: the id of the event
3266 *
3267 * Returns an event that has a given @id.
3268 */
3269struct event_format *pevent_find_event(struct pevent *pevent, int id)
3270{
3271 struct event_format **eventptr;
3272 struct event_format key;
3273 struct event_format *pkey = &key;
3274
3275 /* Check cache first */
3276 if (pevent->last_event && pevent->last_event->id == id)
3277 return pevent->last_event;
3278
3279 key.id = id;
3280
3281 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3282 sizeof(*pevent->events), events_id_cmp);
3283
3284 if (eventptr) {
3285 pevent->last_event = *eventptr;
3286 return *eventptr;
3287 }
3288
3289 return NULL;
3290}
3291
3292/**
3293 * pevent_find_event_by_name - find an event by given name
3294 * @pevent: a handle to the pevent
3295 * @sys: the system name to search for
3296 * @name: the name of the event to search for
3297 *
3298 * This returns an event with a given @name and under the system
3299 * @sys. If @sys is NULL the first event with @name is returned.
3300 */
3301struct event_format *
3302pevent_find_event_by_name(struct pevent *pevent,
3303 const char *sys, const char *name)
3304{
3305 struct event_format *event;
3306 int i;
3307
3308 if (pevent->last_event &&
3309 strcmp(pevent->last_event->name, name) == 0 &&
3310 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3311 return pevent->last_event;
3312
3313 for (i = 0; i < pevent->nr_events; i++) {
3314 event = pevent->events[i];
3315 if (strcmp(event->name, name) == 0) {
3316 if (!sys)
3317 break;
3318 if (strcmp(event->system, sys) == 0)
3319 break;
3320 }
3321 }
3322 if (i == pevent->nr_events)
3323 event = NULL;
3324
3325 pevent->last_event = event;
3326 return event;
3327}
3328
3329static unsigned long long
3330eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3331{
3332 struct pevent *pevent = event->pevent;
3333 unsigned long long val = 0;
3334 unsigned long long left, right;
3335 struct print_arg *typearg = NULL;
3336 struct print_arg *larg;
3337 unsigned long offset;
3338 unsigned int field_size;
3339
3340 switch (arg->type) {
3341 case PRINT_NULL:
3342 /* ?? */
3343 return 0;
3344 case PRINT_ATOM:
3345 return strtoull(arg->atom.atom, NULL, 0);
3346 case PRINT_FIELD:
3347 if (!arg->field.field) {
3348 arg->field.field = pevent_find_any_field(event, arg->field.name);
3349 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003350 goto out_warning_field;
3351
Steven Rostedtf7d82352012-04-06 00:47:53 +02003352 }
3353 /* must be a number */
3354 val = pevent_read_number(pevent, data + arg->field.field->offset,
3355 arg->field.field->size);
3356 break;
3357 case PRINT_FLAGS:
3358 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003359 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003360 break;
3361 case PRINT_TYPE:
3362 val = eval_num_arg(data, size, event, arg->typecast.item);
3363 return eval_type(val, arg, 0);
3364 case PRINT_STRING:
3365 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003366 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003367 return 0;
3368 case PRINT_FUNC: {
3369 struct trace_seq s;
3370 trace_seq_init(&s);
3371 val = process_defined_func(&s, data, size, event, arg);
3372 trace_seq_destroy(&s);
3373 return val;
3374 }
3375 case PRINT_OP:
3376 if (strcmp(arg->op.op, "[") == 0) {
3377 /*
3378 * Arrays are special, since we don't want
3379 * to read the arg as is.
3380 */
3381 right = eval_num_arg(data, size, event, arg->op.right);
3382
3383 /* handle typecasts */
3384 larg = arg->op.left;
3385 while (larg->type == PRINT_TYPE) {
3386 if (!typearg)
3387 typearg = larg;
3388 larg = larg->typecast.item;
3389 }
3390
3391 /* Default to long size */
3392 field_size = pevent->long_size;
3393
3394 switch (larg->type) {
3395 case PRINT_DYNAMIC_ARRAY:
3396 offset = pevent_read_number(pevent,
3397 data + larg->dynarray.field->offset,
3398 larg->dynarray.field->size);
3399 if (larg->dynarray.field->elementsize)
3400 field_size = larg->dynarray.field->elementsize;
3401 /*
3402 * The actual length of the dynamic array is stored
3403 * in the top half of the field, and the offset
3404 * is in the bottom half of the 32 bit field.
3405 */
3406 offset &= 0xffff;
3407 offset += right;
3408 break;
3409 case PRINT_FIELD:
3410 if (!larg->field.field) {
3411 larg->field.field =
3412 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003413 if (!larg->field.field) {
3414 arg = larg;
3415 goto out_warning_field;
3416 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003417 }
3418 field_size = larg->field.field->elementsize;
3419 offset = larg->field.field->offset +
3420 right * larg->field.field->elementsize;
3421 break;
3422 default:
3423 goto default_op; /* oops, all bets off */
3424 }
3425 val = pevent_read_number(pevent,
3426 data + offset, field_size);
3427 if (typearg)
3428 val = eval_type(val, typearg, 1);
3429 break;
3430 } else if (strcmp(arg->op.op, "?") == 0) {
3431 left = eval_num_arg(data, size, event, arg->op.left);
3432 arg = arg->op.right;
3433 if (left)
3434 val = eval_num_arg(data, size, event, arg->op.left);
3435 else
3436 val = eval_num_arg(data, size, event, arg->op.right);
3437 break;
3438 }
3439 default_op:
3440 left = eval_num_arg(data, size, event, arg->op.left);
3441 right = eval_num_arg(data, size, event, arg->op.right);
3442 switch (arg->op.op[0]) {
3443 case '!':
3444 switch (arg->op.op[1]) {
3445 case 0:
3446 val = !right;
3447 break;
3448 case '=':
3449 val = left != right;
3450 break;
3451 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003452 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003453 }
3454 break;
3455 case '~':
3456 val = ~right;
3457 break;
3458 case '|':
3459 if (arg->op.op[1])
3460 val = left || right;
3461 else
3462 val = left | right;
3463 break;
3464 case '&':
3465 if (arg->op.op[1])
3466 val = left && right;
3467 else
3468 val = left & right;
3469 break;
3470 case '<':
3471 switch (arg->op.op[1]) {
3472 case 0:
3473 val = left < right;
3474 break;
3475 case '<':
3476 val = left << right;
3477 break;
3478 case '=':
3479 val = left <= right;
3480 break;
3481 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003482 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003483 }
3484 break;
3485 case '>':
3486 switch (arg->op.op[1]) {
3487 case 0:
3488 val = left > right;
3489 break;
3490 case '>':
3491 val = left >> right;
3492 break;
3493 case '=':
3494 val = left >= right;
3495 break;
3496 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003497 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003498 }
3499 break;
3500 case '=':
3501 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003502 goto out_warning_op;
3503
Steven Rostedtf7d82352012-04-06 00:47:53 +02003504 val = left == right;
3505 break;
3506 case '-':
3507 val = left - right;
3508 break;
3509 case '+':
3510 val = left + right;
3511 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003512 case '/':
3513 val = left / right;
3514 break;
3515 case '*':
3516 val = left * right;
3517 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003518 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003519 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003520 }
3521 break;
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003522 case PRINT_DYNAMIC_ARRAY:
3523 /* Without [], we pass the address to the dynamic data */
3524 offset = pevent_read_number(pevent,
3525 data + arg->dynarray.field->offset,
3526 arg->dynarray.field->size);
3527 /*
3528 * The actual length of the dynamic array is stored
3529 * in the top half of the field, and the offset
3530 * is in the bottom half of the 32 bit field.
3531 */
3532 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003533 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003534 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003535 default: /* not sure what to do there */
3536 return 0;
3537 }
3538 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003539
3540out_warning_op:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003541 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003542 return 0;
3543
3544out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003545 do_warning_event(event, "%s: field %s not found",
3546 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003547 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003548}
3549
3550struct flag {
3551 const char *name;
3552 unsigned long long value;
3553};
3554
3555static const struct flag flags[] = {
3556 { "HI_SOFTIRQ", 0 },
3557 { "TIMER_SOFTIRQ", 1 },
3558 { "NET_TX_SOFTIRQ", 2 },
3559 { "NET_RX_SOFTIRQ", 3 },
3560 { "BLOCK_SOFTIRQ", 4 },
3561 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3562 { "TASKLET_SOFTIRQ", 6 },
3563 { "SCHED_SOFTIRQ", 7 },
3564 { "HRTIMER_SOFTIRQ", 8 },
3565 { "RCU_SOFTIRQ", 9 },
3566
3567 { "HRTIMER_NORESTART", 0 },
3568 { "HRTIMER_RESTART", 1 },
3569};
3570
3571static unsigned long long eval_flag(const char *flag)
3572{
3573 int i;
3574
3575 /*
3576 * Some flags in the format files do not get converted.
3577 * If the flag is not numeric, see if it is something that
3578 * we already know about.
3579 */
3580 if (isdigit(flag[0]))
3581 return strtoull(flag, NULL, 0);
3582
3583 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3584 if (strcmp(flags[i].name, flag) == 0)
3585 return flags[i].value;
3586
3587 return 0;
3588}
3589
3590static void print_str_to_seq(struct trace_seq *s, const char *format,
3591 int len_arg, const char *str)
3592{
3593 if (len_arg >= 0)
3594 trace_seq_printf(s, format, len_arg, str);
3595 else
3596 trace_seq_printf(s, format, str);
3597}
3598
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003599static void print_bitmask_to_seq(struct pevent *pevent,
3600 struct trace_seq *s, const char *format,
3601 int len_arg, const void *data, int size)
3602{
3603 int nr_bits = size * 8;
3604 int str_size = (nr_bits + 3) / 4;
3605 int len = 0;
3606 char buf[3];
3607 char *str;
3608 int index;
3609 int i;
3610
3611 /*
3612 * The kernel likes to put in commas every 32 bits, we
3613 * can do the same.
3614 */
3615 str_size += (nr_bits - 1) / 32;
3616
3617 str = malloc(str_size + 1);
3618 if (!str) {
3619 do_warning("%s: not enough memory!", __func__);
3620 return;
3621 }
3622 str[str_size] = 0;
3623
3624 /* Start out with -2 for the two chars per byte */
3625 for (i = str_size - 2; i >= 0; i -= 2) {
3626 /*
3627 * data points to a bit mask of size bytes.
3628 * In the kernel, this is an array of long words, thus
3629 * endianess is very important.
3630 */
3631 if (pevent->file_bigendian)
3632 index = size - (len + 1);
3633 else
3634 index = len;
3635
3636 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3637 memcpy(str + i, buf, 2);
3638 len++;
3639 if (!(len & 3) && i > 0) {
3640 i--;
3641 str[i] = ',';
3642 }
3643 }
3644
3645 if (len_arg >= 0)
3646 trace_seq_printf(s, format, len_arg, str);
3647 else
3648 trace_seq_printf(s, format, str);
3649
3650 free(str);
3651}
3652
Steven Rostedtf7d82352012-04-06 00:47:53 +02003653static void print_str_arg(struct trace_seq *s, void *data, int size,
3654 struct event_format *event, const char *format,
3655 int len_arg, struct print_arg *arg)
3656{
3657 struct pevent *pevent = event->pevent;
3658 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003659 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003660 struct printk_map *printk;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003661 unsigned long long val, fval;
3662 unsigned long addr;
3663 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003664 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003665 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003666 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003667
3668 switch (arg->type) {
3669 case PRINT_NULL:
3670 /* ?? */
3671 return;
3672 case PRINT_ATOM:
3673 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3674 return;
3675 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003676 field = arg->field.field;
3677 if (!field) {
3678 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003679 if (!field) {
3680 str = arg->field.name;
3681 goto out_warning_field;
3682 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003683 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003684 }
3685 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003686 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003687
3688 /*
3689 * Some events pass in pointers. If this is not an array
3690 * and the size is the same as long_size, assume that it
3691 * is a pointer.
3692 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003693 if (!(field->flags & FIELD_IS_ARRAY) &&
3694 field->size == pevent->long_size) {
3695 addr = *(unsigned long *)(data + field->offset);
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003696 /* Check if it matches a print format */
3697 printk = find_printk(pevent, addr);
3698 if (printk)
3699 trace_seq_puts(s, printk->printk);
3700 else
3701 trace_seq_printf(s, "%lx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003702 break;
3703 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003704 str = malloc(len + 1);
3705 if (!str) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003706 do_warning_event(event, "%s: not enough memory!",
3707 __func__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003708 return;
3709 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003710 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003711 str[len] = 0;
3712 print_str_to_seq(s, format, len_arg, str);
3713 free(str);
3714 break;
3715 case PRINT_FLAGS:
3716 val = eval_num_arg(data, size, event, arg->flags.field);
3717 print = 0;
3718 for (flag = arg->flags.flags; flag; flag = flag->next) {
3719 fval = eval_flag(flag->value);
3720 if (!val && !fval) {
3721 print_str_to_seq(s, format, len_arg, flag->str);
3722 break;
3723 }
3724 if (fval && (val & fval) == fval) {
3725 if (print && arg->flags.delim)
3726 trace_seq_puts(s, arg->flags.delim);
3727 print_str_to_seq(s, format, len_arg, flag->str);
3728 print = 1;
3729 val &= ~fval;
3730 }
3731 }
3732 break;
3733 case PRINT_SYMBOL:
3734 val = eval_num_arg(data, size, event, arg->symbol.field);
3735 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3736 fval = eval_flag(flag->value);
3737 if (val == fval) {
3738 print_str_to_seq(s, format, len_arg, flag->str);
3739 break;
3740 }
3741 }
3742 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003743 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003744 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3745 unsigned long offset;
3746 offset = pevent_read_number(pevent,
3747 data + arg->hex.field->dynarray.field->offset,
3748 arg->hex.field->dynarray.field->size);
3749 hex = data + (offset & 0xffff);
3750 } else {
3751 field = arg->hex.field->field.field;
3752 if (!field) {
3753 str = arg->hex.field->field.name;
3754 field = pevent_find_any_field(event, str);
3755 if (!field)
3756 goto out_warning_field;
3757 arg->hex.field->field.field = field;
3758 }
3759 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003760 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003761 len = eval_num_arg(data, size, event, arg->hex.size);
3762 for (i = 0; i < len; i++) {
3763 if (i)
3764 trace_seq_putc(s, ' ');
3765 trace_seq_printf(s, "%02x", hex[i]);
3766 }
3767 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003768
3769 case PRINT_TYPE:
3770 break;
3771 case PRINT_STRING: {
3772 int str_offset;
3773
3774 if (arg->string.offset == -1) {
3775 struct format_field *f;
3776
3777 f = pevent_find_any_field(event, arg->string.string);
3778 arg->string.offset = f->offset;
3779 }
3780 str_offset = data2host4(pevent, data + arg->string.offset);
3781 str_offset &= 0xffff;
3782 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3783 break;
3784 }
3785 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003786 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003787 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003788 case PRINT_BITMASK: {
3789 int bitmask_offset;
3790 int bitmask_size;
3791
3792 if (arg->bitmask.offset == -1) {
3793 struct format_field *f;
3794
3795 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3796 arg->bitmask.offset = f->offset;
3797 }
3798 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3799 bitmask_size = bitmask_offset >> 16;
3800 bitmask_offset &= 0xffff;
3801 print_bitmask_to_seq(pevent, s, format, len_arg,
3802 data + bitmask_offset, bitmask_size);
3803 break;
3804 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003805 case PRINT_OP:
3806 /*
3807 * The only op for string should be ? :
3808 */
3809 if (arg->op.op[0] != '?')
3810 return;
3811 val = eval_num_arg(data, size, event, arg->op.left);
3812 if (val)
3813 print_str_arg(s, data, size, event,
3814 format, len_arg, arg->op.right->op.left);
3815 else
3816 print_str_arg(s, data, size, event,
3817 format, len_arg, arg->op.right->op.right);
3818 break;
3819 case PRINT_FUNC:
3820 process_defined_func(s, data, size, event, arg);
3821 break;
3822 default:
3823 /* well... */
3824 break;
3825 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003826
3827 return;
3828
3829out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003830 do_warning_event(event, "%s: field %s not found",
3831 __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003832}
3833
3834static unsigned long long
3835process_defined_func(struct trace_seq *s, void *data, int size,
3836 struct event_format *event, struct print_arg *arg)
3837{
3838 struct pevent_function_handler *func_handle = arg->func.func;
3839 struct pevent_func_params *param;
3840 unsigned long long *args;
3841 unsigned long long ret;
3842 struct print_arg *farg;
3843 struct trace_seq str;
3844 struct save_str {
3845 struct save_str *next;
3846 char *str;
3847 } *strings = NULL, *string;
3848 int i;
3849
3850 if (!func_handle->nr_args) {
3851 ret = (*func_handle->func)(s, NULL);
3852 goto out;
3853 }
3854
3855 farg = arg->func.args;
3856 param = func_handle->params;
3857
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003858 ret = ULLONG_MAX;
3859 args = malloc(sizeof(*args) * func_handle->nr_args);
3860 if (!args)
3861 goto out;
3862
Steven Rostedtf7d82352012-04-06 00:47:53 +02003863 for (i = 0; i < func_handle->nr_args; i++) {
3864 switch (param->type) {
3865 case PEVENT_FUNC_ARG_INT:
3866 case PEVENT_FUNC_ARG_LONG:
3867 case PEVENT_FUNC_ARG_PTR:
3868 args[i] = eval_num_arg(data, size, event, farg);
3869 break;
3870 case PEVENT_FUNC_ARG_STRING:
3871 trace_seq_init(&str);
3872 print_str_arg(&str, data, size, event, "%s", -1, farg);
3873 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003874 string = malloc(sizeof(*string));
3875 if (!string) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003876 do_warning_event(event, "%s(%d): malloc str",
3877 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003878 goto out_free;
3879 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003880 string->next = strings;
3881 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003882 if (!string->str) {
3883 free(string);
Namhyung Kim3388cc32014-03-19 10:22:53 +09003884 do_warning_event(event, "%s(%d): malloc str",
3885 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003886 goto out_free;
3887 }
Robert Richter0cf26012012-08-07 19:43:14 +02003888 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003889 strings = string;
3890 trace_seq_destroy(&str);
3891 break;
3892 default:
3893 /*
3894 * Something went totally wrong, this is not
3895 * an input error, something in this code broke.
3896 */
Namhyung Kim3388cc32014-03-19 10:22:53 +09003897 do_warning_event(event, "Unexpected end of arguments\n");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003898 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003899 }
3900 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003901 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003902 }
3903
3904 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003905out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003906 free(args);
3907 while (strings) {
3908 string = strings;
3909 strings = string->next;
3910 free(string->str);
3911 free(string);
3912 }
3913
3914 out:
3915 /* TBD : handle return type here */
3916 return ret;
3917}
3918
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003919static void free_args(struct print_arg *args)
3920{
3921 struct print_arg *next;
3922
3923 while (args) {
3924 next = args->next;
3925
3926 free_arg(args);
3927 args = next;
3928 }
3929}
3930
Steven Rostedtf7d82352012-04-06 00:47:53 +02003931static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3932{
3933 struct pevent *pevent = event->pevent;
3934 struct format_field *field, *ip_field;
3935 struct print_arg *args, *arg, **next;
3936 unsigned long long ip, val;
3937 char *ptr;
3938 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003939 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003940
3941 field = pevent->bprint_buf_field;
3942 ip_field = pevent->bprint_ip_field;
3943
3944 if (!field) {
3945 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003946 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003947 do_warning_event(event, "can't find buffer field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003948 return NULL;
3949 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003950 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003951 if (!ip_field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003952 do_warning_event(event, "can't find ip field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003953 return NULL;
3954 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003955 pevent->bprint_buf_field = field;
3956 pevent->bprint_ip_field = ip_field;
3957 }
3958
3959 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3960
3961 /*
3962 * The first arg is the IP pointer.
3963 */
3964 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003965 if (!args) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003966 do_warning_event(event, "%s(%d): not enough memory!",
3967 __func__, __LINE__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003968 return NULL;
3969 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003970 arg = args;
3971 arg->next = NULL;
3972 next = &arg->next;
3973
3974 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003975
3976 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3977 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003978
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003979 /* skip the first "%pf: " */
3980 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003981 bptr < data + size && *ptr; ptr++) {
3982 int ls = 0;
3983
3984 if (*ptr == '%') {
3985 process_again:
3986 ptr++;
3987 switch (*ptr) {
3988 case '%':
3989 break;
3990 case 'l':
3991 ls++;
3992 goto process_again;
3993 case 'L':
3994 ls = 2;
3995 goto process_again;
3996 case '0' ... '9':
3997 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003998 case '.':
3999 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004000 case 'p':
4001 ls = 1;
4002 /* fall through */
4003 case 'd':
4004 case 'u':
4005 case 'x':
4006 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004007 switch (ls) {
4008 case 0:
4009 vsize = 4;
4010 break;
4011 case 1:
4012 vsize = pevent->long_size;
4013 break;
4014 case 2:
4015 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02004016 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004017 default:
4018 vsize = ls; /* ? */
4019 break;
4020 }
4021 /* fall through */
4022 case '*':
4023 if (*ptr == '*')
4024 vsize = 4;
4025
Steven Rostedtf7d82352012-04-06 00:47:53 +02004026 /* the pointers are always 4 bytes aligned */
4027 bptr = (void *)(((unsigned long)bptr + 3) &
4028 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004029 val = pevent_read_number(pevent, bptr, vsize);
4030 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004031 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004032 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004033 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004034 __func__, __LINE__);
4035 goto out_free;
4036 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004037 arg->next = NULL;
4038 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004039 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4040 free(arg);
4041 goto out_free;
4042 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004043 *next = arg;
4044 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004045 /*
4046 * The '*' case means that an arg is used as the length.
4047 * We need to continue to figure out for what.
4048 */
4049 if (*ptr == '*')
4050 goto process_again;
4051
Steven Rostedtf7d82352012-04-06 00:47:53 +02004052 break;
4053 case 's':
4054 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004055 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004056 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004057 __func__, __LINE__);
4058 goto out_free;
4059 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004060 arg->next = NULL;
4061 arg->type = PRINT_BSTRING;
4062 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09004063 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004064 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004065 bptr += strlen(bptr) + 1;
4066 *next = arg;
4067 next = &arg->next;
4068 default:
4069 break;
4070 }
4071 }
4072 }
4073
4074 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004075
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004076out_free:
4077 free_args(args);
4078 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004079}
4080
4081static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004082get_bprint_format(void *data, int size __maybe_unused,
4083 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004084{
4085 struct pevent *pevent = event->pevent;
4086 unsigned long long addr;
4087 struct format_field *field;
4088 struct printk_map *printk;
4089 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004090
4091 field = pevent->bprint_fmt_field;
4092
4093 if (!field) {
4094 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004095 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004096 do_warning_event(event, "can't find format field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004097 return NULL;
4098 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004099 pevent->bprint_fmt_field = field;
4100 }
4101
4102 addr = pevent_read_number(pevent, data + field->offset, field->size);
4103
4104 printk = find_printk(pevent, addr);
4105 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004106 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004107 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004108 return format;
4109 }
4110
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004111 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004112 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004113
4114 return format;
4115}
4116
4117static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4118 struct event_format *event, struct print_arg *arg)
4119{
4120 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004121 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02004122
4123 if (arg->type == PRINT_FUNC) {
4124 process_defined_func(s, data, size, event, arg);
4125 return;
4126 }
4127
4128 if (arg->type != PRINT_FIELD) {
4129 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4130 arg->type);
4131 return;
4132 }
4133
4134 if (mac == 'm')
4135 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4136 if (!arg->field.field) {
4137 arg->field.field =
4138 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004139 if (!arg->field.field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004140 do_warning_event(event, "%s: field %s not found",
4141 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004142 return;
4143 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004144 }
4145 if (arg->field.field->size != 6) {
4146 trace_seq_printf(s, "INVALIDMAC");
4147 return;
4148 }
4149 buf = data + arg->field.field->offset;
4150 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4151}
4152
David Ahern3d199b52014-12-18 19:11:11 -07004153static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4154{
4155 const char *fmt;
4156
4157 if (i == 'i')
4158 fmt = "%03d.%03d.%03d.%03d";
4159 else
4160 fmt = "%d.%d.%d.%d";
4161
4162 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4163}
4164
4165static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4166{
4167 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4168 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4169}
4170
4171static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4172{
4173 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4174}
4175
4176static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4177{
4178 int i, j, range;
4179 unsigned char zerolength[8];
4180 int longest = 1;
4181 int colonpos = -1;
4182 uint16_t word;
4183 uint8_t hi, lo;
4184 bool needcolon = false;
4185 bool useIPv4;
4186 struct in6_addr in6;
4187
4188 memcpy(&in6, addr, sizeof(struct in6_addr));
4189
4190 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4191
4192 memset(zerolength, 0, sizeof(zerolength));
4193
4194 if (useIPv4)
4195 range = 6;
4196 else
4197 range = 8;
4198
4199 /* find position of longest 0 run */
4200 for (i = 0; i < range; i++) {
4201 for (j = i; j < range; j++) {
4202 if (in6.s6_addr16[j] != 0)
4203 break;
4204 zerolength[i]++;
4205 }
4206 }
4207 for (i = 0; i < range; i++) {
4208 if (zerolength[i] > longest) {
4209 longest = zerolength[i];
4210 colonpos = i;
4211 }
4212 }
4213 if (longest == 1) /* don't compress a single 0 */
4214 colonpos = -1;
4215
4216 /* emit address */
4217 for (i = 0; i < range; i++) {
4218 if (i == colonpos) {
4219 if (needcolon || i == 0)
4220 trace_seq_printf(s, ":");
4221 trace_seq_printf(s, ":");
4222 needcolon = false;
4223 i += longest - 1;
4224 continue;
4225 }
4226 if (needcolon) {
4227 trace_seq_printf(s, ":");
4228 needcolon = false;
4229 }
4230 /* hex u16 without leading 0s */
4231 word = ntohs(in6.s6_addr16[i]);
4232 hi = word >> 8;
4233 lo = word & 0xff;
4234 if (hi)
4235 trace_seq_printf(s, "%x%02x", hi, lo);
4236 else
4237 trace_seq_printf(s, "%x", lo);
4238
4239 needcolon = true;
4240 }
4241
4242 if (useIPv4) {
4243 if (needcolon)
4244 trace_seq_printf(s, ":");
4245 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4246 }
4247
4248 return;
4249}
4250
4251static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4252{
4253 int j;
4254
4255 for (j = 0; j < 16; j += 2) {
4256 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4257 if (i == 'I' && j < 14)
4258 trace_seq_printf(s, ":");
4259 }
4260}
4261
4262/*
4263 * %pi4 print an IPv4 address with leading zeros
4264 * %pI4 print an IPv4 address without leading zeros
4265 * %pi6 print an IPv6 address without colons
4266 * %pI6 print an IPv6 address with colons
4267 * %pI6c print an IPv6 address in compressed form with colons
4268 * %pISpc print an IP address based on sockaddr; p adds port.
4269 */
4270static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4271 void *data, int size, struct event_format *event,
4272 struct print_arg *arg)
4273{
4274 unsigned char *buf;
4275
4276 if (arg->type == PRINT_FUNC) {
4277 process_defined_func(s, data, size, event, arg);
4278 return 0;
4279 }
4280
4281 if (arg->type != PRINT_FIELD) {
4282 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4283 return 0;
4284 }
4285
4286 if (!arg->field.field) {
4287 arg->field.field =
4288 pevent_find_any_field(event, arg->field.name);
4289 if (!arg->field.field) {
4290 do_warning("%s: field %s not found",
4291 __func__, arg->field.name);
4292 return 0;
4293 }
4294 }
4295
4296 buf = data + arg->field.field->offset;
4297
4298 if (arg->field.field->size != 4) {
4299 trace_seq_printf(s, "INVALIDIPv4");
4300 return 0;
4301 }
4302 print_ip4_addr(s, i, buf);
4303
4304 return 0;
4305}
4306
4307static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4308 void *data, int size, struct event_format *event,
4309 struct print_arg *arg)
4310{
4311 char have_c = 0;
4312 unsigned char *buf;
4313 int rc = 0;
4314
4315 /* pI6c */
4316 if (i == 'I' && *ptr == 'c') {
4317 have_c = 1;
4318 ptr++;
4319 rc++;
4320 }
4321
4322 if (arg->type == PRINT_FUNC) {
4323 process_defined_func(s, data, size, event, arg);
4324 return rc;
4325 }
4326
4327 if (arg->type != PRINT_FIELD) {
4328 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4329 return rc;
4330 }
4331
4332 if (!arg->field.field) {
4333 arg->field.field =
4334 pevent_find_any_field(event, arg->field.name);
4335 if (!arg->field.field) {
4336 do_warning("%s: field %s not found",
4337 __func__, arg->field.name);
4338 return rc;
4339 }
4340 }
4341
4342 buf = data + arg->field.field->offset;
4343
4344 if (arg->field.field->size != 16) {
4345 trace_seq_printf(s, "INVALIDIPv6");
4346 return rc;
4347 }
4348
4349 if (have_c)
4350 print_ip6c_addr(s, buf);
4351 else
4352 print_ip6_addr(s, i, buf);
4353
4354 return rc;
4355}
4356
4357static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4358 void *data, int size, struct event_format *event,
4359 struct print_arg *arg)
4360{
4361 char have_c = 0, have_p = 0;
4362 unsigned char *buf;
4363 struct sockaddr_storage *sa;
4364 int rc = 0;
4365
4366 /* pISpc */
4367 if (i == 'I') {
4368 if (*ptr == 'p') {
4369 have_p = 1;
4370 ptr++;
4371 rc++;
4372 }
4373 if (*ptr == 'c') {
4374 have_c = 1;
4375 ptr++;
4376 rc++;
4377 }
4378 }
4379
4380 if (arg->type == PRINT_FUNC) {
4381 process_defined_func(s, data, size, event, arg);
4382 return rc;
4383 }
4384
4385 if (arg->type != PRINT_FIELD) {
4386 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4387 return rc;
4388 }
4389
4390 if (!arg->field.field) {
4391 arg->field.field =
4392 pevent_find_any_field(event, arg->field.name);
4393 if (!arg->field.field) {
4394 do_warning("%s: field %s not found",
4395 __func__, arg->field.name);
4396 return rc;
4397 }
4398 }
4399
4400 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4401
4402 if (sa->ss_family == AF_INET) {
4403 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4404
4405 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4406 trace_seq_printf(s, "INVALIDIPv4");
4407 return rc;
4408 }
4409
4410 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4411 if (have_p)
4412 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4413
4414
4415 } else if (sa->ss_family == AF_INET6) {
4416 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4417
4418 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4419 trace_seq_printf(s, "INVALIDIPv6");
4420 return rc;
4421 }
4422
4423 if (have_p)
4424 trace_seq_printf(s, "[");
4425
4426 buf = (unsigned char *) &sa6->sin6_addr;
4427 if (have_c)
4428 print_ip6c_addr(s, buf);
4429 else
4430 print_ip6_addr(s, i, buf);
4431
4432 if (have_p)
4433 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4434 }
4435
4436 return rc;
4437}
4438
4439static int print_ip_arg(struct trace_seq *s, const char *ptr,
4440 void *data, int size, struct event_format *event,
4441 struct print_arg *arg)
4442{
4443 char i = *ptr; /* 'i' or 'I' */
4444 char ver;
4445 int rc = 0;
4446
4447 ptr++;
4448 rc++;
4449
4450 ver = *ptr;
4451 ptr++;
4452 rc++;
4453
4454 switch (ver) {
4455 case '4':
4456 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4457 break;
4458 case '6':
4459 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4460 break;
4461 case 'S':
4462 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4463 break;
4464 default:
4465 return 0;
4466 }
4467
4468 return rc;
4469}
4470
Namhyung Kim600da3c2012-06-22 17:10:15 +09004471static int is_printable_array(char *p, unsigned int len)
4472{
4473 unsigned int i;
4474
4475 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004476 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004477 return 0;
4478 return 1;
4479}
4480
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03004481static void print_event_fields(struct trace_seq *s, void *data,
4482 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004483 struct event_format *event)
4484{
4485 struct format_field *field;
4486 unsigned long long val;
4487 unsigned int offset, len, i;
4488
4489 field = event->format.fields;
4490 while (field) {
4491 trace_seq_printf(s, " %s=", field->name);
4492 if (field->flags & FIELD_IS_ARRAY) {
4493 offset = field->offset;
4494 len = field->size;
4495 if (field->flags & FIELD_IS_DYNAMIC) {
4496 val = pevent_read_number(event->pevent, data + offset, len);
4497 offset = val;
4498 len = offset >> 16;
4499 offset &= 0xffff;
4500 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09004501 if (field->flags & FIELD_IS_STRING &&
4502 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004503 trace_seq_printf(s, "%s", (char *)data + offset);
4504 } else {
4505 trace_seq_puts(s, "ARRAY[");
4506 for (i = 0; i < len; i++) {
4507 if (i)
4508 trace_seq_puts(s, ", ");
4509 trace_seq_printf(s, "%02x",
4510 *((unsigned char *)data + offset + i));
4511 }
4512 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09004513 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004514 }
4515 } else {
4516 val = pevent_read_number(event->pevent, data + field->offset,
4517 field->size);
4518 if (field->flags & FIELD_IS_POINTER) {
4519 trace_seq_printf(s, "0x%llx", val);
4520 } else if (field->flags & FIELD_IS_SIGNED) {
4521 switch (field->size) {
4522 case 4:
4523 /*
4524 * If field is long then print it in hex.
4525 * A long usually stores pointers.
4526 */
4527 if (field->flags & FIELD_IS_LONG)
4528 trace_seq_printf(s, "0x%x", (int)val);
4529 else
4530 trace_seq_printf(s, "%d", (int)val);
4531 break;
4532 case 2:
4533 trace_seq_printf(s, "%2d", (short)val);
4534 break;
4535 case 1:
4536 trace_seq_printf(s, "%1d", (char)val);
4537 break;
4538 default:
4539 trace_seq_printf(s, "%lld", val);
4540 }
4541 } else {
4542 if (field->flags & FIELD_IS_LONG)
4543 trace_seq_printf(s, "0x%llx", val);
4544 else
4545 trace_seq_printf(s, "%llu", val);
4546 }
4547 }
4548 field = field->next;
4549 }
4550}
4551
4552static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4553{
4554 struct pevent *pevent = event->pevent;
4555 struct print_fmt *print_fmt = &event->print_fmt;
4556 struct print_arg *arg = print_fmt->args;
4557 struct print_arg *args = NULL;
4558 const char *ptr = print_fmt->format;
4559 unsigned long long val;
4560 struct func_map *func;
4561 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004562 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004563 char *bprint_fmt = NULL;
4564 char format[32];
4565 int show_func;
4566 int len_as_arg;
4567 int len_arg;
4568 int len;
4569 int ls;
4570
4571 if (event->flags & EVENT_FL_FAILED) {
4572 trace_seq_printf(s, "[FAILED TO PARSE]");
4573 print_event_fields(s, data, size, event);
4574 return;
4575 }
4576
4577 if (event->flags & EVENT_FL_ISBPRINT) {
4578 bprint_fmt = get_bprint_format(data, size, event);
4579 args = make_bprint_args(bprint_fmt, data, size, event);
4580 arg = args;
4581 ptr = bprint_fmt;
4582 }
4583
4584 for (; *ptr; ptr++) {
4585 ls = 0;
4586 if (*ptr == '\\') {
4587 ptr++;
4588 switch (*ptr) {
4589 case 'n':
4590 trace_seq_putc(s, '\n');
4591 break;
4592 case 't':
4593 trace_seq_putc(s, '\t');
4594 break;
4595 case 'r':
4596 trace_seq_putc(s, '\r');
4597 break;
4598 case '\\':
4599 trace_seq_putc(s, '\\');
4600 break;
4601 default:
4602 trace_seq_putc(s, *ptr);
4603 break;
4604 }
4605
4606 } else if (*ptr == '%') {
4607 saveptr = ptr;
4608 show_func = 0;
4609 len_as_arg = 0;
4610 cont_process:
4611 ptr++;
4612 switch (*ptr) {
4613 case '%':
4614 trace_seq_putc(s, '%');
4615 break;
4616 case '#':
4617 /* FIXME: need to handle properly */
4618 goto cont_process;
4619 case 'h':
4620 ls--;
4621 goto cont_process;
4622 case 'l':
4623 ls++;
4624 goto cont_process;
4625 case 'L':
4626 ls = 2;
4627 goto cont_process;
4628 case '*':
4629 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004630 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004631 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004632 event->flags |= EVENT_FL_FAILED;
4633 goto out_failed;
4634 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004635 len_arg = eval_num_arg(data, size, event, arg);
4636 len_as_arg = 1;
4637 arg = arg->next;
4638 goto cont_process;
4639 case '.':
4640 case 'z':
4641 case 'Z':
4642 case '0' ... '9':
4643 goto cont_process;
4644 case 'p':
4645 if (pevent->long_size == 4)
4646 ls = 1;
4647 else
4648 ls = 2;
4649
4650 if (*(ptr+1) == 'F' ||
4651 *(ptr+1) == 'f') {
4652 ptr++;
4653 show_func = *ptr;
4654 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4655 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4656 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004657 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004658 break;
David Ahern3d199b52014-12-18 19:11:11 -07004659 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4660 int n;
4661
4662 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4663 if (n > 0) {
4664 ptr += n;
4665 arg = arg->next;
4666 break;
4667 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004668 }
4669
4670 /* fall through */
4671 case 'd':
4672 case 'i':
4673 case 'x':
4674 case 'X':
4675 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004676 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004677 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004678 event->flags |= EVENT_FL_FAILED;
4679 goto out_failed;
4680 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004681
4682 len = ((unsigned long)ptr + 1) -
4683 (unsigned long)saveptr;
4684
4685 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004686 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004687 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004688 event->flags |= EVENT_FL_FAILED;
4689 len = 31;
4690 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004691
4692 memcpy(format, saveptr, len);
4693 format[len] = 0;
4694
4695 val = eval_num_arg(data, size, event, arg);
4696 arg = arg->next;
4697
4698 if (show_func) {
4699 func = find_func(pevent, val);
4700 if (func) {
4701 trace_seq_puts(s, func->func);
4702 if (show_func == 'F')
4703 trace_seq_printf(s,
4704 "+0x%llx",
4705 val - func->addr);
4706 break;
4707 }
4708 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004709 if (pevent->long_size == 8 && ls &&
4710 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004711 char *p;
4712
4713 ls = 2;
4714 /* make %l into %ll */
4715 p = strchr(format, 'l');
4716 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004717 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004718 else if (strcmp(format, "%p") == 0)
4719 strcpy(format, "0x%llx");
4720 }
4721 switch (ls) {
4722 case -2:
4723 if (len_as_arg)
4724 trace_seq_printf(s, format, len_arg, (char)val);
4725 else
4726 trace_seq_printf(s, format, (char)val);
4727 break;
4728 case -1:
4729 if (len_as_arg)
4730 trace_seq_printf(s, format, len_arg, (short)val);
4731 else
4732 trace_seq_printf(s, format, (short)val);
4733 break;
4734 case 0:
4735 if (len_as_arg)
4736 trace_seq_printf(s, format, len_arg, (int)val);
4737 else
4738 trace_seq_printf(s, format, (int)val);
4739 break;
4740 case 1:
4741 if (len_as_arg)
4742 trace_seq_printf(s, format, len_arg, (long)val);
4743 else
4744 trace_seq_printf(s, format, (long)val);
4745 break;
4746 case 2:
4747 if (len_as_arg)
4748 trace_seq_printf(s, format, len_arg,
4749 (long long)val);
4750 else
4751 trace_seq_printf(s, format, (long long)val);
4752 break;
4753 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09004754 do_warning_event(event, "bad count (%d)", ls);
Namhyung Kim245c5a12012-09-07 11:49:45 +09004755 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004756 }
4757 break;
4758 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004759 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004760 do_warning_event(event, "no matching argument");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004761 event->flags |= EVENT_FL_FAILED;
4762 goto out_failed;
4763 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004764
4765 len = ((unsigned long)ptr + 1) -
4766 (unsigned long)saveptr;
4767
4768 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004769 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004770 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004771 event->flags |= EVENT_FL_FAILED;
4772 len = 31;
4773 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004774
4775 memcpy(format, saveptr, len);
4776 format[len] = 0;
4777 if (!len_as_arg)
4778 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05004779 /* Use helper trace_seq */
4780 trace_seq_init(&p);
4781 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004782 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05004783 trace_seq_terminate(&p);
4784 trace_seq_puts(s, p.buffer);
Steven Rostedtde04f862014-04-22 19:23:30 -04004785 trace_seq_destroy(&p);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004786 arg = arg->next;
4787 break;
4788 default:
4789 trace_seq_printf(s, ">%c<", *ptr);
4790
4791 }
4792 } else
4793 trace_seq_putc(s, *ptr);
4794 }
4795
Namhyung Kim245c5a12012-09-07 11:49:45 +09004796 if (event->flags & EVENT_FL_FAILED) {
4797out_failed:
4798 trace_seq_printf(s, "[FAILED TO PARSE]");
4799 }
4800
Steven Rostedtf7d82352012-04-06 00:47:53 +02004801 if (args) {
4802 free_args(args);
4803 free(bprint_fmt);
4804 }
4805}
4806
4807/**
4808 * pevent_data_lat_fmt - parse the data for the latency format
4809 * @pevent: a handle to the pevent
4810 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004811 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004812 *
4813 * This parses out the Latency format (interrupts disabled,
4814 * need rescheduling, in hard/soft interrupt, preempt count
4815 * and lock depth) and places it into the trace_seq.
4816 */
4817void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004818 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004819{
4820 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004821 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004822 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004823 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004824 unsigned int lat_flags;
4825 unsigned int pc;
4826 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004827 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004828 int hardirq;
4829 int softirq;
4830 void *data = record->data;
4831
4832 lat_flags = parse_common_flags(pevent, data);
4833 pc = parse_common_pc(pevent, data);
4834 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004835 if (lock_depth_exists)
4836 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004837 else if (check_lock_depth) {
4838 lock_depth = parse_common_lock_depth(pevent, data);
4839 if (lock_depth < 0)
4840 check_lock_depth = 0;
4841 else
4842 lock_depth_exists = 1;
4843 }
4844
4845 /* migrate_disable may not always exist */
4846 if (migrate_disable_exists)
4847 migrate_disable = parse_common_migrate_disable(pevent, data);
4848 else if (check_migrate_disable) {
4849 migrate_disable = parse_common_migrate_disable(pevent, data);
4850 if (migrate_disable < 0)
4851 check_migrate_disable = 0;
4852 else
4853 migrate_disable_exists = 1;
4854 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004855
4856 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4857 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4858
4859 trace_seq_printf(s, "%c%c%c",
4860 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4861 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4862 'X' : '.',
4863 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4864 'N' : '.',
4865 (hardirq && softirq) ? 'H' :
4866 hardirq ? 'h' : softirq ? 's' : '.');
4867
4868 if (pc)
4869 trace_seq_printf(s, "%x", pc);
4870 else
4871 trace_seq_putc(s, '.');
4872
Steven Rostedt0866a972012-05-22 14:52:40 +09004873 if (migrate_disable_exists) {
4874 if (migrate_disable < 0)
4875 trace_seq_putc(s, '.');
4876 else
4877 trace_seq_printf(s, "%d", migrate_disable);
4878 }
4879
Steven Rostedtf7d82352012-04-06 00:47:53 +02004880 if (lock_depth_exists) {
4881 if (lock_depth < 0)
4882 trace_seq_putc(s, '.');
4883 else
4884 trace_seq_printf(s, "%d", lock_depth);
4885 }
4886
4887 trace_seq_terminate(s);
4888}
4889
4890/**
4891 * pevent_data_type - parse out the given event type
4892 * @pevent: a handle to the pevent
4893 * @rec: the record to read from
4894 *
4895 * This returns the event id from the @rec.
4896 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004897int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004898{
4899 return trace_parse_common_type(pevent, rec->data);
4900}
4901
4902/**
4903 * pevent_data_event_from_type - find the event by a given type
4904 * @pevent: a handle to the pevent
4905 * @type: the type of the event.
4906 *
4907 * This returns the event form a given @type;
4908 */
4909struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4910{
4911 return pevent_find_event(pevent, type);
4912}
4913
4914/**
4915 * pevent_data_pid - parse the PID from raw data
4916 * @pevent: a handle to the pevent
4917 * @rec: the record to parse
4918 *
4919 * This returns the PID from a raw data.
4920 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004921int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004922{
4923 return parse_common_pid(pevent, rec->data);
4924}
4925
4926/**
4927 * pevent_data_comm_from_pid - return the command line from PID
4928 * @pevent: a handle to the pevent
4929 * @pid: the PID of the task to search for
4930 *
4931 * This returns a pointer to the command line that has the given
4932 * @pid.
4933 */
4934const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4935{
4936 const char *comm;
4937
4938 comm = find_cmdline(pevent, pid);
4939 return comm;
4940}
4941
4942/**
4943 * pevent_data_comm_from_pid - parse the data into the print format
4944 * @s: the trace_seq to write to
4945 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004946 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004947 *
4948 * This parses the raw @data using the given @event information and
4949 * writes the print format into the trace_seq.
4950 */
4951void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004952 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004953{
4954 int print_pretty = 1;
4955
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004956 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004957 print_event_fields(s, record->data, record->size, event);
4958 else {
4959
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004960 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004961 print_pretty = event->handler(s, record, event,
4962 event->context);
4963
4964 if (print_pretty)
4965 pretty_print(s, record->data, record->size, event);
4966 }
4967
4968 trace_seq_terminate(s);
4969}
4970
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004971static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4972{
4973 if (!use_trace_clock)
4974 return true;
4975
4976 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4977 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4978 return true;
4979
4980 /* trace_clock is setting in tsc or counter mode */
4981 return false;
4982}
4983
Steven Rostedtf7d82352012-04-06 00:47:53 +02004984void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004985 struct pevent_record *record, bool use_trace_clock)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004986{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004987 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004988 struct event_format *event;
4989 unsigned long secs;
4990 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004991 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004992 const char *comm;
4993 void *data = record->data;
4994 int type;
4995 int pid;
4996 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004997 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004998 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004999
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005000 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5001 use_trace_clock);
5002 if (use_usec_format) {
5003 secs = record->ts / NSECS_PER_SEC;
5004 nsecs = record->ts - secs * NSECS_PER_SEC;
5005 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005006
5007 if (record->size < 0) {
5008 do_warning("ug! negative record size %d", record->size);
5009 return;
5010 }
5011
5012 type = trace_parse_common_type(pevent, data);
5013
5014 event = pevent_find_event(pevent, type);
5015 if (!event) {
5016 do_warning("ug! no event found for type %d", type);
5017 return;
5018 }
5019
5020 pid = parse_common_pid(pevent, data);
5021 comm = find_cmdline(pevent, pid);
5022
5023 if (pevent->latency_format) {
5024 trace_seq_printf(s, "%8.8s-%-5d %3d",
5025 comm, pid, record->cpu);
5026 pevent_data_lat_fmt(pevent, s, record);
5027 } else
5028 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5029
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005030 if (use_usec_format) {
5031 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5032 usecs = nsecs;
5033 p = 9;
5034 } else {
5035 usecs = (nsecs + 500) / NSECS_PER_USEC;
5036 p = 6;
5037 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02005038
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005039 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5040 secs, p, usecs, event->name);
5041 } else
5042 trace_seq_printf(s, " %12llu: %s: ",
5043 record->ts, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005044
5045 /* Space out the event names evenly. */
5046 len = strlen(event->name);
5047 if (len < 20)
5048 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5049
5050 pevent_event_info(s, event, record);
5051}
5052
5053static int events_id_cmp(const void *a, const void *b)
5054{
5055 struct event_format * const * ea = a;
5056 struct event_format * const * eb = b;
5057
5058 if ((*ea)->id < (*eb)->id)
5059 return -1;
5060
5061 if ((*ea)->id > (*eb)->id)
5062 return 1;
5063
5064 return 0;
5065}
5066
5067static int events_name_cmp(const void *a, const void *b)
5068{
5069 struct event_format * const * ea = a;
5070 struct event_format * const * eb = b;
5071 int res;
5072
5073 res = strcmp((*ea)->name, (*eb)->name);
5074 if (res)
5075 return res;
5076
5077 res = strcmp((*ea)->system, (*eb)->system);
5078 if (res)
5079 return res;
5080
5081 return events_id_cmp(a, b);
5082}
5083
5084static int events_system_cmp(const void *a, const void *b)
5085{
5086 struct event_format * const * ea = a;
5087 struct event_format * const * eb = b;
5088 int res;
5089
5090 res = strcmp((*ea)->system, (*eb)->system);
5091 if (res)
5092 return res;
5093
5094 res = strcmp((*ea)->name, (*eb)->name);
5095 if (res)
5096 return res;
5097
5098 return events_id_cmp(a, b);
5099}
5100
5101struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5102{
5103 struct event_format **events;
5104 int (*sort)(const void *a, const void *b);
5105
5106 events = pevent->sort_events;
5107
5108 if (events && pevent->last_type == sort_type)
5109 return events;
5110
5111 if (!events) {
5112 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5113 if (!events)
5114 return NULL;
5115
5116 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5117 events[pevent->nr_events] = NULL;
5118
5119 pevent->sort_events = events;
5120
5121 /* the internal events are sorted by id */
5122 if (sort_type == EVENT_SORT_ID) {
5123 pevent->last_type = sort_type;
5124 return events;
5125 }
5126 }
5127
5128 switch (sort_type) {
5129 case EVENT_SORT_ID:
5130 sort = events_id_cmp;
5131 break;
5132 case EVENT_SORT_NAME:
5133 sort = events_name_cmp;
5134 break;
5135 case EVENT_SORT_SYSTEM:
5136 sort = events_system_cmp;
5137 break;
5138 default:
5139 return events;
5140 }
5141
5142 qsort(events, pevent->nr_events, sizeof(*events), sort);
5143 pevent->last_type = sort_type;
5144
5145 return events;
5146}
5147
5148static struct format_field **
5149get_event_fields(const char *type, const char *name,
5150 int count, struct format_field *list)
5151{
5152 struct format_field **fields;
5153 struct format_field *field;
5154 int i = 0;
5155
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03005156 fields = malloc(sizeof(*fields) * (count + 1));
5157 if (!fields)
5158 return NULL;
5159
Steven Rostedtf7d82352012-04-06 00:47:53 +02005160 for (field = list; field; field = field->next) {
5161 fields[i++] = field;
5162 if (i == count + 1) {
5163 do_warning("event %s has more %s fields than specified",
5164 name, type);
5165 i--;
5166 break;
5167 }
5168 }
5169
5170 if (i != count)
5171 do_warning("event %s has less %s fields than specified",
5172 name, type);
5173
5174 fields[i] = NULL;
5175
5176 return fields;
5177}
5178
5179/**
5180 * pevent_event_common_fields - return a list of common fields for an event
5181 * @event: the event to return the common fields of.
5182 *
5183 * Returns an allocated array of fields. The last item in the array is NULL.
5184 * The array must be freed with free().
5185 */
5186struct format_field **pevent_event_common_fields(struct event_format *event)
5187{
5188 return get_event_fields("common", event->name,
5189 event->format.nr_common,
5190 event->format.common_fields);
5191}
5192
5193/**
5194 * pevent_event_fields - return a list of event specific fields for an event
5195 * @event: the event to return the fields of.
5196 *
5197 * Returns an allocated array of fields. The last item in the array is NULL.
5198 * The array must be freed with free().
5199 */
5200struct format_field **pevent_event_fields(struct event_format *event)
5201{
5202 return get_event_fields("event", event->name,
5203 event->format.nr_fields,
5204 event->format.fields);
5205}
5206
5207static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5208{
5209 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5210 if (field->next) {
5211 trace_seq_puts(s, ", ");
5212 print_fields(s, field->next);
5213 }
5214}
5215
5216/* for debugging */
5217static void print_args(struct print_arg *args)
5218{
5219 int print_paren = 1;
5220 struct trace_seq s;
5221
5222 switch (args->type) {
5223 case PRINT_NULL:
5224 printf("null");
5225 break;
5226 case PRINT_ATOM:
5227 printf("%s", args->atom.atom);
5228 break;
5229 case PRINT_FIELD:
5230 printf("REC->%s", args->field.name);
5231 break;
5232 case PRINT_FLAGS:
5233 printf("__print_flags(");
5234 print_args(args->flags.field);
5235 printf(", %s, ", args->flags.delim);
5236 trace_seq_init(&s);
5237 print_fields(&s, args->flags.flags);
5238 trace_seq_do_printf(&s);
5239 trace_seq_destroy(&s);
5240 printf(")");
5241 break;
5242 case PRINT_SYMBOL:
5243 printf("__print_symbolic(");
5244 print_args(args->symbol.field);
5245 printf(", ");
5246 trace_seq_init(&s);
5247 print_fields(&s, args->symbol.symbols);
5248 trace_seq_do_printf(&s);
5249 trace_seq_destroy(&s);
5250 printf(")");
5251 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09005252 case PRINT_HEX:
5253 printf("__print_hex(");
5254 print_args(args->hex.field);
5255 printf(", ");
5256 print_args(args->hex.size);
5257 printf(")");
5258 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005259 case PRINT_STRING:
5260 case PRINT_BSTRING:
5261 printf("__get_str(%s)", args->string.string);
5262 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04005263 case PRINT_BITMASK:
5264 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5265 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005266 case PRINT_TYPE:
5267 printf("(%s)", args->typecast.type);
5268 print_args(args->typecast.item);
5269 break;
5270 case PRINT_OP:
5271 if (strcmp(args->op.op, ":") == 0)
5272 print_paren = 0;
5273 if (print_paren)
5274 printf("(");
5275 print_args(args->op.left);
5276 printf(" %s ", args->op.op);
5277 print_args(args->op.right);
5278 if (print_paren)
5279 printf(")");
5280 break;
5281 default:
5282 /* we should warn... */
5283 return;
5284 }
5285 if (args->next) {
5286 printf("\n");
5287 print_args(args->next);
5288 }
5289}
5290
5291static void parse_header_field(const char *field,
5292 int *offset, int *size, int mandatory)
5293{
5294 unsigned long long save_input_buf_ptr;
5295 unsigned long long save_input_buf_siz;
5296 char *token;
5297 int type;
5298
5299 save_input_buf_ptr = input_buf_ptr;
5300 save_input_buf_siz = input_buf_siz;
5301
5302 if (read_expected(EVENT_ITEM, "field") < 0)
5303 return;
5304 if (read_expected(EVENT_OP, ":") < 0)
5305 return;
5306
5307 /* type */
5308 if (read_expect_type(EVENT_ITEM, &token) < 0)
5309 goto fail;
5310 free_token(token);
5311
5312 /*
5313 * If this is not a mandatory field, then test it first.
5314 */
5315 if (mandatory) {
5316 if (read_expected(EVENT_ITEM, field) < 0)
5317 return;
5318 } else {
5319 if (read_expect_type(EVENT_ITEM, &token) < 0)
5320 goto fail;
5321 if (strcmp(token, field) != 0)
5322 goto discard;
5323 free_token(token);
5324 }
5325
5326 if (read_expected(EVENT_OP, ";") < 0)
5327 return;
5328 if (read_expected(EVENT_ITEM, "offset") < 0)
5329 return;
5330 if (read_expected(EVENT_OP, ":") < 0)
5331 return;
5332 if (read_expect_type(EVENT_ITEM, &token) < 0)
5333 goto fail;
5334 *offset = atoi(token);
5335 free_token(token);
5336 if (read_expected(EVENT_OP, ";") < 0)
5337 return;
5338 if (read_expected(EVENT_ITEM, "size") < 0)
5339 return;
5340 if (read_expected(EVENT_OP, ":") < 0)
5341 return;
5342 if (read_expect_type(EVENT_ITEM, &token) < 0)
5343 goto fail;
5344 *size = atoi(token);
5345 free_token(token);
5346 if (read_expected(EVENT_OP, ";") < 0)
5347 return;
5348 type = read_token(&token);
5349 if (type != EVENT_NEWLINE) {
5350 /* newer versions of the kernel have a "signed" type */
5351 if (type != EVENT_ITEM)
5352 goto fail;
5353
5354 if (strcmp(token, "signed") != 0)
5355 goto fail;
5356
5357 free_token(token);
5358
5359 if (read_expected(EVENT_OP, ":") < 0)
5360 return;
5361
5362 if (read_expect_type(EVENT_ITEM, &token))
5363 goto fail;
5364
5365 free_token(token);
5366 if (read_expected(EVENT_OP, ";") < 0)
5367 return;
5368
5369 if (read_expect_type(EVENT_NEWLINE, &token))
5370 goto fail;
5371 }
5372 fail:
5373 free_token(token);
5374 return;
5375
5376 discard:
5377 input_buf_ptr = save_input_buf_ptr;
5378 input_buf_siz = save_input_buf_siz;
5379 *offset = 0;
5380 *size = 0;
5381 free_token(token);
5382}
5383
5384/**
5385 * pevent_parse_header_page - parse the data stored in the header page
5386 * @pevent: the handle to the pevent
5387 * @buf: the buffer storing the header page format string
5388 * @size: the size of @buf
5389 * @long_size: the long size to use if there is no header
5390 *
5391 * This parses the header page format for information on the
5392 * ring buffer used. The @buf should be copied from
5393 *
5394 * /sys/kernel/debug/tracing/events/header_page
5395 */
5396int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5397 int long_size)
5398{
5399 int ignore;
5400
5401 if (!size) {
5402 /*
5403 * Old kernels did not have header page info.
5404 * Sorry but we just use what we find here in user space.
5405 */
5406 pevent->header_page_ts_size = sizeof(long long);
5407 pevent->header_page_size_size = long_size;
5408 pevent->header_page_data_offset = sizeof(long long) + long_size;
5409 pevent->old_format = 1;
5410 return -1;
5411 }
5412 init_input_buf(buf, size);
5413
5414 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5415 &pevent->header_page_ts_size, 1);
5416 parse_header_field("commit", &pevent->header_page_size_offset,
5417 &pevent->header_page_size_size, 1);
5418 parse_header_field("overwrite", &pevent->header_page_overwrite,
5419 &ignore, 0);
5420 parse_header_field("data", &pevent->header_page_data_offset,
5421 &pevent->header_page_data_size, 1);
5422
5423 return 0;
5424}
5425
5426static int event_matches(struct event_format *event,
5427 int id, const char *sys_name,
5428 const char *event_name)
5429{
5430 if (id >= 0 && id != event->id)
5431 return 0;
5432
5433 if (event_name && (strcmp(event_name, event->name) != 0))
5434 return 0;
5435
5436 if (sys_name && (strcmp(sys_name, event->system) != 0))
5437 return 0;
5438
5439 return 1;
5440}
5441
5442static void free_handler(struct event_handler *handle)
5443{
5444 free((void *)handle->sys_name);
5445 free((void *)handle->event_name);
5446 free(handle);
5447}
5448
5449static int find_event_handle(struct pevent *pevent, struct event_format *event)
5450{
5451 struct event_handler *handle, **next;
5452
5453 for (next = &pevent->handlers; *next;
5454 next = &(*next)->next) {
5455 handle = *next;
5456 if (event_matches(event, handle->id,
5457 handle->sys_name,
5458 handle->event_name))
5459 break;
5460 }
5461
5462 if (!(*next))
5463 return 0;
5464
5465 pr_stat("overriding event (%d) %s:%s with new print handler",
5466 event->id, event->system, event->name);
5467
5468 event->handler = handle->func;
5469 event->context = handle->context;
5470
5471 *next = handle->next;
5472 free_handler(handle);
5473
5474 return 1;
5475}
5476
5477/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005478 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005479 * @buf: the buffer storing the event format string
5480 * @size: the size of @buf
5481 * @sys: the system the event belongs to
5482 *
5483 * This parses the event format and creates an event structure
5484 * to quickly parse raw data for a given event.
5485 *
5486 * These files currently come from:
5487 *
5488 * /sys/kernel/debug/tracing/events/.../.../format
5489 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005490enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5491 struct pevent *pevent, const char *buf,
5492 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005493{
5494 struct event_format *event;
5495 int ret;
5496
5497 init_input_buf(buf, size);
5498
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005499 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02005500 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09005501 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005502
5503 event->name = event_read_name();
5504 if (!event->name) {
5505 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09005506 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5507 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005508 }
5509
5510 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005511 event->flags |= EVENT_FL_ISFTRACE;
5512
5513 if (strcmp(event->name, "bprint") == 0)
5514 event->flags |= EVENT_FL_ISBPRINT;
5515 }
5516
5517 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005518 if (event->id < 0) {
5519 ret = PEVENT_ERRNO__READ_ID_FAILED;
5520 /*
5521 * This isn't an allocation error actually.
5522 * But as the ID is critical, just bail out.
5523 */
5524 goto event_alloc_failed;
5525 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005526
5527 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005528 if (!event->system) {
5529 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5530 goto event_alloc_failed;
5531 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005532
Steven Rostedt101782e2012-10-01 20:13:51 -04005533 /* Add pevent to event so that it can be referenced */
5534 event->pevent = pevent;
5535
Steven Rostedtf7d82352012-04-06 00:47:53 +02005536 ret = event_read_format(event);
5537 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005538 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5539 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005540 }
5541
5542 /*
5543 * If the event has an override, don't print warnings if the event
5544 * print format fails to parse.
5545 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005546 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005547 show_warning = 0;
5548
5549 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005550 show_warning = 1;
5551
Steven Rostedtf7d82352012-04-06 00:47:53 +02005552 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005553 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5554 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005555 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005556
5557 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5558 struct format_field *field;
5559 struct print_arg *arg, **list;
5560
5561 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005562 list = &event->print_fmt.args;
5563 for (field = event->format.fields; field; field = field->next) {
5564 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09005565 if (!arg) {
5566 event->flags |= EVENT_FL_FAILED;
5567 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5568 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005569 arg->type = PRINT_FIELD;
5570 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09005571 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09005572 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005573 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005574 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005575 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005576 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005577 *list = arg;
5578 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005579 }
5580 return 0;
5581 }
5582
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005583 return 0;
5584
5585 event_parse_failed:
5586 event->flags |= EVENT_FL_FAILED;
5587 return ret;
5588
5589 event_alloc_failed:
5590 free(event->system);
5591 free(event->name);
5592 free(event);
5593 *eventp = NULL;
5594 return ret;
5595}
5596
Jiri Olsa71ad9582013-12-03 14:09:19 +01005597static enum pevent_errno
5598__pevent_parse_event(struct pevent *pevent,
5599 struct event_format **eventp,
5600 const char *buf, unsigned long size,
5601 const char *sys)
5602{
5603 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5604 struct event_format *event = *eventp;
5605
5606 if (event == NULL)
5607 return ret;
5608
5609 if (pevent && add_event(pevent, event)) {
5610 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5611 goto event_add_failed;
5612 }
5613
5614#define PRINT_ARGS 0
5615 if (PRINT_ARGS && event->print_fmt.args)
5616 print_args(event->print_fmt.args);
5617
5618 return 0;
5619
5620event_add_failed:
5621 pevent_free_format(event);
5622 return ret;
5623}
5624
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005625/**
5626 * pevent_parse_format - parse the event format
Jiri Olsa71ad9582013-12-03 14:09:19 +01005627 * @pevent: the handle to the pevent
5628 * @eventp: returned format
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005629 * @buf: the buffer storing the event format string
5630 * @size: the size of @buf
5631 * @sys: the system the event belongs to
5632 *
5633 * This parses the event format and creates an event structure
5634 * to quickly parse raw data for a given event.
5635 *
5636 * These files currently come from:
5637 *
5638 * /sys/kernel/debug/tracing/events/.../.../format
5639 */
Jiri Olsa71ad9582013-12-03 14:09:19 +01005640enum pevent_errno pevent_parse_format(struct pevent *pevent,
5641 struct event_format **eventp,
5642 const char *buf,
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005643 unsigned long size, const char *sys)
5644{
Jiri Olsa71ad9582013-12-03 14:09:19 +01005645 return __pevent_parse_event(pevent, eventp, buf, size, sys);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005646}
5647
5648/**
5649 * pevent_parse_event - parse the event format
5650 * @pevent: the handle to the pevent
5651 * @buf: the buffer storing the event format string
5652 * @size: the size of @buf
5653 * @sys: the system the event belongs to
5654 *
5655 * This parses the event format and creates an event structure
5656 * to quickly parse raw data for a given event.
5657 *
5658 * These files currently come from:
5659 *
5660 * /sys/kernel/debug/tracing/events/.../.../format
5661 */
5662enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5663 unsigned long size, const char *sys)
5664{
5665 struct event_format *event = NULL;
Jiri Olsa71ad9582013-12-03 14:09:19 +01005666 return __pevent_parse_event(pevent, &event, buf, size, sys);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005667}
5668
Namhyung Kim2f197b92012-08-22 16:00:30 +09005669#undef _PE
5670#define _PE(code, str) str
5671static const char * const pevent_error_str[] = {
5672 PEVENT_ERRORS
5673};
5674#undef _PE
5675
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005676int pevent_strerror(struct pevent *pevent __maybe_unused,
5677 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005678{
5679 int idx;
5680 const char *msg;
5681
5682 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005683 msg = strerror_r(errnum, buf, buflen);
5684 if (msg != buf) {
5685 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005686 memcpy(buf, msg, min(buflen - 1, len));
5687 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005688 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005689 return 0;
5690 }
5691
5692 if (errnum <= __PEVENT_ERRNO__START ||
5693 errnum >= __PEVENT_ERRNO__END)
5694 return -1;
5695
Namhyung Kimf63fe792012-08-23 16:37:00 +09005696 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005697 msg = pevent_error_str[idx];
Namhyung Kimbf19b822013-12-12 16:36:17 +09005698 snprintf(buf, buflen, "%s", msg);
Namhyung Kim2f197b92012-08-22 16:00:30 +09005699
5700 return 0;
5701}
5702
Steven Rostedtf7d82352012-04-06 00:47:53 +02005703int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005704 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005705 unsigned long long *val, int err)
5706{
5707 if (!field) {
5708 if (err)
5709 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5710 return -1;
5711 }
5712
5713 if (pevent_read_number_field(field, record->data, val)) {
5714 if (err)
5715 trace_seq_printf(s, " %s=INVALID", name);
5716 return -1;
5717 }
5718
5719 return 0;
5720}
5721
5722/**
5723 * pevent_get_field_raw - return the raw pointer into the data field
5724 * @s: The seq to print to on error
5725 * @event: the event that the field is for
5726 * @name: The name of the field
5727 * @record: The record with the field name.
5728 * @len: place to store the field length.
5729 * @err: print default error if failed.
5730 *
5731 * Returns a pointer into record->data of the field and places
5732 * the length of the field in @len.
5733 *
5734 * On failure, it returns NULL.
5735 */
5736void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005737 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005738 int *len, int err)
5739{
5740 struct format_field *field;
5741 void *data = record->data;
5742 unsigned offset;
5743 int dummy;
5744
5745 if (!event)
5746 return NULL;
5747
5748 field = pevent_find_field(event, name);
5749
5750 if (!field) {
5751 if (err)
5752 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5753 return NULL;
5754 }
5755
5756 /* Allow @len to be NULL */
5757 if (!len)
5758 len = &dummy;
5759
5760 offset = field->offset;
5761 if (field->flags & FIELD_IS_DYNAMIC) {
5762 offset = pevent_read_number(event->pevent,
5763 data + offset, field->size);
5764 *len = offset >> 16;
5765 offset &= 0xffff;
5766 } else
5767 *len = field->size;
5768
5769 return data + offset;
5770}
5771
5772/**
5773 * pevent_get_field_val - find a field and return its value
5774 * @s: The seq to print to on error
5775 * @event: the event that the field is for
5776 * @name: The name of the field
5777 * @record: The record with the field name.
5778 * @val: place to store the value of the field.
5779 * @err: print default error if failed.
5780 *
5781 * Returns 0 on success -1 on field not found.
5782 */
5783int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005784 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005785 unsigned long long *val, int err)
5786{
5787 struct format_field *field;
5788
5789 if (!event)
5790 return -1;
5791
5792 field = pevent_find_field(event, name);
5793
5794 return get_field_val(s, field, name, record, val, err);
5795}
5796
5797/**
5798 * pevent_get_common_field_val - find a common field and return its value
5799 * @s: The seq to print to on error
5800 * @event: the event that the field is for
5801 * @name: The name of the field
5802 * @record: The record with the field name.
5803 * @val: place to store the value of the field.
5804 * @err: print default error if failed.
5805 *
5806 * Returns 0 on success -1 on field not found.
5807 */
5808int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005809 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005810 unsigned long long *val, int err)
5811{
5812 struct format_field *field;
5813
5814 if (!event)
5815 return -1;
5816
5817 field = pevent_find_common_field(event, name);
5818
5819 return get_field_val(s, field, name, record, val, err);
5820}
5821
5822/**
5823 * pevent_get_any_field_val - find a any field and return its value
5824 * @s: The seq to print to on error
5825 * @event: the event that the field is for
5826 * @name: The name of the field
5827 * @record: The record with the field name.
5828 * @val: place to store the value of the field.
5829 * @err: print default error if failed.
5830 *
5831 * Returns 0 on success -1 on field not found.
5832 */
5833int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005834 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005835 unsigned long long *val, int err)
5836{
5837 struct format_field *field;
5838
5839 if (!event)
5840 return -1;
5841
5842 field = pevent_find_any_field(event, name);
5843
5844 return get_field_val(s, field, name, record, val, err);
5845}
5846
5847/**
5848 * pevent_print_num_field - print a field and a format
5849 * @s: The seq to print to
5850 * @fmt: The printf format to print the field with.
5851 * @event: the event that the field is for
5852 * @name: The name of the field
5853 * @record: The record with the field name.
5854 * @err: print default error if failed.
5855 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005856 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005857 */
5858int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5859 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005860 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005861{
5862 struct format_field *field = pevent_find_field(event, name);
5863 unsigned long long val;
5864
5865 if (!field)
5866 goto failed;
5867
5868 if (pevent_read_number_field(field, record->data, &val))
5869 goto failed;
5870
5871 return trace_seq_printf(s, fmt, val);
5872
5873 failed:
5874 if (err)
5875 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5876 return -1;
5877}
5878
Steven Rostedt6d862b82013-11-01 17:54:00 -04005879/**
5880 * pevent_print_func_field - print a field and a format for function pointers
5881 * @s: The seq to print to
5882 * @fmt: The printf format to print the field with.
5883 * @event: the event that the field is for
5884 * @name: The name of the field
5885 * @record: The record with the field name.
5886 * @err: print default error if failed.
5887 *
5888 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5889 */
5890int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5891 struct event_format *event, const char *name,
5892 struct pevent_record *record, int err)
5893{
5894 struct format_field *field = pevent_find_field(event, name);
5895 struct pevent *pevent = event->pevent;
5896 unsigned long long val;
5897 struct func_map *func;
5898 char tmp[128];
5899
5900 if (!field)
5901 goto failed;
5902
5903 if (pevent_read_number_field(field, record->data, &val))
5904 goto failed;
5905
5906 func = find_func(pevent, val);
5907
5908 if (func)
5909 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5910 else
5911 sprintf(tmp, "0x%08llx", val);
5912
5913 return trace_seq_printf(s, fmt, tmp);
5914
5915 failed:
5916 if (err)
5917 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5918 return -1;
5919}
5920
Steven Rostedtf7d82352012-04-06 00:47:53 +02005921static void free_func_handle(struct pevent_function_handler *func)
5922{
5923 struct pevent_func_params *params;
5924
5925 free(func->name);
5926
5927 while (func->params) {
5928 params = func->params;
5929 func->params = params->next;
5930 free(params);
5931 }
5932
5933 free(func);
5934}
5935
5936/**
5937 * pevent_register_print_function - register a helper function
5938 * @pevent: the handle to the pevent
5939 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005940 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005941 * @name: the name of the helper function
5942 * @parameters: A list of enum pevent_func_arg_type
5943 *
5944 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005945 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005946 * of these functions.
5947 *
5948 * The @parameters is a variable list of pevent_func_arg_type enums that
5949 * must end with PEVENT_FUNC_ARG_VOID.
5950 */
5951int pevent_register_print_function(struct pevent *pevent,
5952 pevent_func_handler func,
5953 enum pevent_func_arg_type ret_type,
5954 char *name, ...)
5955{
5956 struct pevent_function_handler *func_handle;
5957 struct pevent_func_params **next_param;
5958 struct pevent_func_params *param;
5959 enum pevent_func_arg_type type;
5960 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005961 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005962
5963 func_handle = find_func_handler(pevent, name);
5964 if (func_handle) {
5965 /*
5966 * This is most like caused by the users own
5967 * plugins updating the function. This overrides the
5968 * system defaults.
5969 */
5970 pr_stat("override of function helper '%s'", name);
5971 remove_func_handler(pevent, name);
5972 }
5973
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005974 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005975 if (!func_handle) {
5976 do_warning("Failed to allocate function handler");
5977 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5978 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005979
5980 func_handle->ret_type = ret_type;
5981 func_handle->name = strdup(name);
5982 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005983 if (!func_handle->name) {
5984 do_warning("Failed to allocate function name");
5985 free(func_handle);
5986 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5987 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005988
5989 next_param = &(func_handle->params);
5990 va_start(ap, name);
5991 for (;;) {
5992 type = va_arg(ap, enum pevent_func_arg_type);
5993 if (type == PEVENT_FUNC_ARG_VOID)
5994 break;
5995
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03005996 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005997 do_warning("Invalid argument type %d", type);
5998 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005999 goto out_free;
6000 }
6001
Namhyung Kim67ed9392012-09-07 11:49:47 +09006002 param = malloc(sizeof(*param));
6003 if (!param) {
6004 do_warning("Failed to allocate function param");
6005 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6006 goto out_free;
6007 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006008 param->type = type;
6009 param->next = NULL;
6010
6011 *next_param = param;
6012 next_param = &(param->next);
6013
6014 func_handle->nr_args++;
6015 }
6016 va_end(ap);
6017
6018 func_handle->next = pevent->func_handlers;
6019 pevent->func_handlers = func_handle;
6020
6021 return 0;
6022 out_free:
6023 va_end(ap);
6024 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09006025 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006026}
6027
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006028/**
6029 * pevent_unregister_print_function - unregister a helper function
6030 * @pevent: the handle to the pevent
6031 * @func: the function to process the helper function
6032 * @name: the name of the helper function
6033 *
6034 * This function removes existing print handler for function @name.
6035 *
6036 * Returns 0 if the handler was removed successully, -1 otherwise.
6037 */
6038int pevent_unregister_print_function(struct pevent *pevent,
6039 pevent_func_handler func, char *name)
6040{
6041 struct pevent_function_handler *func_handle;
6042
6043 func_handle = find_func_handler(pevent, name);
6044 if (func_handle && func_handle->func == func) {
6045 remove_func_handler(pevent, name);
6046 return 0;
6047 }
6048 return -1;
6049}
6050
Namhyung Kimad137012014-01-16 11:31:07 +09006051static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6052 const char *sys_name,
6053 const char *event_name)
6054{
6055 struct event_format *event;
6056
6057 if (id >= 0) {
6058 /* search by id */
6059 event = pevent_find_event(pevent, id);
6060 if (!event)
6061 return NULL;
6062 if (event_name && (strcmp(event_name, event->name) != 0))
6063 return NULL;
6064 if (sys_name && (strcmp(sys_name, event->system) != 0))
6065 return NULL;
6066 } else {
6067 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6068 if (!event)
6069 return NULL;
6070 }
6071 return event;
6072}
6073
Steven Rostedtf7d82352012-04-06 00:47:53 +02006074/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006075 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02006076 * @pevent: the handle to the pevent
6077 * @id: the id of the event to register
6078 * @sys_name: the system name the event belongs to
6079 * @event_name: the name of the event
6080 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006081 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02006082 *
6083 * This function allows a developer to override the parsing of
6084 * a given event. If for some reason the default print format
6085 * is not sufficient, this function will register a function
6086 * for an event to be used to parse the data instead.
6087 *
6088 * If @id is >= 0, then it is used to find the event.
6089 * else @sys_name and @event_name are used.
6090 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09006091int pevent_register_event_handler(struct pevent *pevent, int id,
6092 const char *sys_name, const char *event_name,
6093 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006094{
6095 struct event_format *event;
6096 struct event_handler *handle;
6097
Namhyung Kimad137012014-01-16 11:31:07 +09006098 event = pevent_search_event(pevent, id, sys_name, event_name);
6099 if (event == NULL)
6100 goto not_found;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006101
6102 pr_stat("overriding event (%d) %s:%s with new print handler",
6103 event->id, event->system, event->name);
6104
6105 event->handler = func;
6106 event->context = context;
6107 return 0;
6108
6109 not_found:
6110 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006111 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006112 if (!handle) {
6113 do_warning("Failed to allocate event handler");
6114 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6115 }
6116
Steven Rostedtf7d82352012-04-06 00:47:53 +02006117 handle->id = id;
6118 if (event_name)
6119 handle->event_name = strdup(event_name);
6120 if (sys_name)
6121 handle->sys_name = strdup(sys_name);
6122
Namhyung Kimca638582012-04-09 11:54:31 +09006123 if ((event_name && !handle->event_name) ||
6124 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006125 do_warning("Failed to allocate event/sys name");
6126 free((void *)handle->event_name);
6127 free((void *)handle->sys_name);
6128 free(handle);
6129 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006130 }
6131
Steven Rostedtf7d82352012-04-06 00:47:53 +02006132 handle->func = func;
6133 handle->next = pevent->handlers;
6134 pevent->handlers = handle;
6135 handle->context = context;
6136
6137 return -1;
6138}
6139
Namhyung Kimad137012014-01-16 11:31:07 +09006140static int handle_matches(struct event_handler *handler, int id,
6141 const char *sys_name, const char *event_name,
6142 pevent_event_handler_func func, void *context)
6143{
6144 if (id >= 0 && id != handler->id)
6145 return 0;
6146
6147 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6148 return 0;
6149
6150 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6151 return 0;
6152
6153 if (func != handler->func || context != handler->context)
6154 return 0;
6155
6156 return 1;
6157}
6158
6159/**
6160 * pevent_unregister_event_handler - unregister an existing event handler
6161 * @pevent: the handle to the pevent
6162 * @id: the id of the event to unregister
6163 * @sys_name: the system name the handler belongs to
6164 * @event_name: the name of the event handler
6165 * @func: the function to call to parse the event information
6166 * @context: the data to be passed to @func
6167 *
6168 * This function removes existing event handler (parser).
6169 *
6170 * If @id is >= 0, then it is used to find the event.
6171 * else @sys_name and @event_name are used.
6172 *
6173 * Returns 0 if handler was removed successfully, -1 if event was not found.
6174 */
6175int pevent_unregister_event_handler(struct pevent *pevent, int id,
6176 const char *sys_name, const char *event_name,
6177 pevent_event_handler_func func, void *context)
6178{
6179 struct event_format *event;
6180 struct event_handler *handle;
6181 struct event_handler **next;
6182
6183 event = pevent_search_event(pevent, id, sys_name, event_name);
6184 if (event == NULL)
6185 goto not_found;
6186
6187 if (event->handler == func && event->context == context) {
6188 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6189 event->id, event->system, event->name);
6190
6191 event->handler = NULL;
6192 event->context = NULL;
6193 return 0;
6194 }
6195
6196not_found:
6197 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6198 handle = *next;
6199 if (handle_matches(handle, id, sys_name, event_name,
6200 func, context))
6201 break;
6202 }
6203
6204 if (!(*next))
6205 return -1;
6206
6207 *next = handle->next;
6208 free_handler(handle);
6209
6210 return 0;
6211}
6212
Steven Rostedtf7d82352012-04-06 00:47:53 +02006213/**
6214 * pevent_alloc - create a pevent handle
6215 */
6216struct pevent *pevent_alloc(void)
6217{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006218 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02006219
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006220 if (pevent)
6221 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006222
6223 return pevent;
6224}
6225
6226void pevent_ref(struct pevent *pevent)
6227{
6228 pevent->ref_count++;
6229}
6230
David Ahern00ae1122015-03-19 12:36:21 -06006231void pevent_free_format_field(struct format_field *field)
6232{
6233 free(field->type);
6234 free(field->name);
6235 free(field);
6236}
6237
Steven Rostedtf7d82352012-04-06 00:47:53 +02006238static void free_format_fields(struct format_field *field)
6239{
6240 struct format_field *next;
6241
6242 while (field) {
6243 next = field->next;
David Ahern00ae1122015-03-19 12:36:21 -06006244 pevent_free_format_field(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006245 field = next;
6246 }
6247}
6248
6249static void free_formats(struct format *format)
6250{
6251 free_format_fields(format->common_fields);
6252 free_format_fields(format->fields);
6253}
6254
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006255void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006256{
6257 free(event->name);
6258 free(event->system);
6259
6260 free_formats(&event->format);
6261
6262 free(event->print_fmt.format);
6263 free_args(event->print_fmt.args);
6264
6265 free(event);
6266}
6267
6268/**
6269 * pevent_free - free a pevent handle
6270 * @pevent: the pevent handle to free
6271 */
6272void pevent_free(struct pevent *pevent)
6273{
Steven Rostedta2525a02012-04-06 00:48:02 +02006274 struct cmdline_list *cmdlist, *cmdnext;
6275 struct func_list *funclist, *funcnext;
6276 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006277 struct pevent_function_handler *func_handler;
6278 struct event_handler *handle;
6279 int i;
6280
Steven Rostedta2525a02012-04-06 00:48:02 +02006281 if (!pevent)
6282 return;
6283
6284 cmdlist = pevent->cmdlist;
6285 funclist = pevent->funclist;
6286 printklist = pevent->printklist;
6287
Steven Rostedtf7d82352012-04-06 00:47:53 +02006288 pevent->ref_count--;
6289 if (pevent->ref_count)
6290 return;
6291
6292 if (pevent->cmdlines) {
6293 for (i = 0; i < pevent->cmdline_count; i++)
6294 free(pevent->cmdlines[i].comm);
6295 free(pevent->cmdlines);
6296 }
6297
6298 while (cmdlist) {
6299 cmdnext = cmdlist->next;
6300 free(cmdlist->comm);
6301 free(cmdlist);
6302 cmdlist = cmdnext;
6303 }
6304
6305 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006306 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02006307 free(pevent->func_map[i].func);
6308 free(pevent->func_map[i].mod);
6309 }
6310 free(pevent->func_map);
6311 }
6312
6313 while (funclist) {
6314 funcnext = funclist->next;
6315 free(funclist->func);
6316 free(funclist->mod);
6317 free(funclist);
6318 funclist = funcnext;
6319 }
6320
6321 while (pevent->func_handlers) {
6322 func_handler = pevent->func_handlers;
6323 pevent->func_handlers = func_handler->next;
6324 free_func_handle(func_handler);
6325 }
6326
6327 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006328 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006329 free(pevent->printk_map[i].printk);
6330 free(pevent->printk_map);
6331 }
6332
6333 while (printklist) {
6334 printknext = printklist->next;
6335 free(printklist->printk);
6336 free(printklist);
6337 printklist = printknext;
6338 }
6339
6340 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006341 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006342
6343 while (pevent->handlers) {
6344 handle = pevent->handlers;
6345 pevent->handlers = handle->next;
6346 free_handler(handle);
6347 }
6348
6349 free(pevent->events);
6350 free(pevent->sort_events);
6351
6352 free(pevent);
6353}
6354
6355void pevent_unref(struct pevent *pevent)
6356{
6357 pevent_free(pevent);
6358}