blob: bdb1dd618e0ee6d76a8a9a6793369e9cf69c64e1 [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
Josef Bacikdeab6f52015-03-24 09:57:49 -0400307 if (comm)
308 item->comm = strdup(comm);
309 else
310 item->comm = strdup("<...>");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300311 if (!item->comm) {
312 free(item);
313 return -1;
314 }
Steven Rostedtf7d82352012-04-06 00:47:53 +0200315 item->pid = pid;
316 item->next = pevent->cmdlist;
317
318 pevent->cmdlist = item;
319 pevent->cmdline_count++;
320
321 return 0;
322}
323
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -0400324int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400325{
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -0400326 pevent->trace_clock = strdup(trace_clock);
327 if (!pevent->trace_clock) {
328 errno = ENOMEM;
329 return -1;
330 }
331 return 0;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400332}
333
Steven Rostedtf7d82352012-04-06 00:47:53 +0200334struct func_map {
335 unsigned long long addr;
336 char *func;
337 char *mod;
338};
339
340struct func_list {
341 struct func_list *next;
342 unsigned long long addr;
343 char *func;
344 char *mod;
345};
346
347static int func_cmp(const void *a, const void *b)
348{
349 const struct func_map *fa = a;
350 const struct func_map *fb = b;
351
352 if (fa->addr < fb->addr)
353 return -1;
354 if (fa->addr > fb->addr)
355 return 1;
356
357 return 0;
358}
359
360/*
361 * We are searching for a record in between, not an exact
362 * match.
363 */
364static int func_bcmp(const void *a, const void *b)
365{
366 const struct func_map *fa = a;
367 const struct func_map *fb = b;
368
369 if ((fa->addr == fb->addr) ||
370
371 (fa->addr > fb->addr &&
372 fa->addr < (fb+1)->addr))
373 return 0;
374
375 if (fa->addr < fb->addr)
376 return -1;
377
378 return 1;
379}
380
381static int func_map_init(struct pevent *pevent)
382{
383 struct func_list *funclist;
384 struct func_list *item;
385 struct func_map *func_map;
386 int i;
387
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300388 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
389 if (!func_map)
390 return -1;
391
Steven Rostedtf7d82352012-04-06 00:47:53 +0200392 funclist = pevent->funclist;
393
394 i = 0;
395 while (funclist) {
396 func_map[i].func = funclist->func;
397 func_map[i].addr = funclist->addr;
398 func_map[i].mod = funclist->mod;
399 i++;
400 item = funclist;
401 funclist = funclist->next;
402 free(item);
403 }
404
405 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
406
407 /*
408 * Add a special record at the end.
409 */
410 func_map[pevent->func_count].func = NULL;
411 func_map[pevent->func_count].addr = 0;
412 func_map[pevent->func_count].mod = NULL;
413
414 pevent->func_map = func_map;
415 pevent->funclist = NULL;
416
417 return 0;
418}
419
420static struct func_map *
421find_func(struct pevent *pevent, unsigned long long addr)
422{
423 struct func_map *func;
424 struct func_map key;
425
426 if (!pevent->func_map)
427 func_map_init(pevent);
428
429 key.addr = addr;
430
431 func = bsearch(&key, pevent->func_map, pevent->func_count,
432 sizeof(*pevent->func_map), func_bcmp);
433
434 return func;
435}
436
437/**
438 * pevent_find_function - find a function by a given address
439 * @pevent: handle for the pevent
440 * @addr: the address to find the function with
441 *
442 * Returns a pointer to the function stored that has the given
443 * address. Note, the address does not have to be exact, it
444 * will select the function that would contain the address.
445 */
446const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
447{
448 struct func_map *map;
449
450 map = find_func(pevent, addr);
451 if (!map)
452 return NULL;
453
454 return map->func;
455}
456
457/**
458 * pevent_find_function_address - find a function address by a given address
459 * @pevent: handle for the pevent
460 * @addr: the address to find the function with
461 *
462 * Returns the address the function starts at. This can be used in
463 * conjunction with pevent_find_function to print both the function
464 * name and the function offset.
465 */
466unsigned long long
467pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
468{
469 struct func_map *map;
470
471 map = find_func(pevent, addr);
472 if (!map)
473 return 0;
474
475 return map->addr;
476}
477
478/**
479 * pevent_register_function - register a function with a given address
480 * @pevent: handle for the pevent
481 * @function: the function name to register
482 * @addr: the address the function starts at
483 * @mod: the kernel module the function may be in (NULL for none)
484 *
485 * This registers a function name with an address and module.
486 * The @func passed in is duplicated.
487 */
488int pevent_register_function(struct pevent *pevent, char *func,
489 unsigned long long addr, char *mod)
490{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300491 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200492
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300493 if (!item)
494 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200495
496 item->next = pevent->funclist;
497 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300498 if (!item->func)
499 goto out_free;
500
501 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200502 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300503 if (!item->mod)
504 goto out_free_func;
505 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200506 item->mod = NULL;
507 item->addr = addr;
508
Namhyung Kimca638582012-04-09 11:54:31 +0900509 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200510 pevent->func_count++;
511
512 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300513
514out_free_func:
515 free(item->func);
516 item->func = NULL;
517out_free:
518 free(item);
519 errno = ENOMEM;
520 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200521}
522
523/**
524 * pevent_print_funcs - print out the stored functions
525 * @pevent: handle for the pevent
526 *
527 * This prints out the stored functions.
528 */
529void pevent_print_funcs(struct pevent *pevent)
530{
531 int i;
532
533 if (!pevent->func_map)
534 func_map_init(pevent);
535
536 for (i = 0; i < (int)pevent->func_count; i++) {
537 printf("%016llx %s",
538 pevent->func_map[i].addr,
539 pevent->func_map[i].func);
540 if (pevent->func_map[i].mod)
541 printf(" [%s]\n", pevent->func_map[i].mod);
542 else
543 printf("\n");
544 }
545}
546
547struct printk_map {
548 unsigned long long addr;
549 char *printk;
550};
551
552struct printk_list {
553 struct printk_list *next;
554 unsigned long long addr;
555 char *printk;
556};
557
558static int printk_cmp(const void *a, const void *b)
559{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900560 const struct printk_map *pa = a;
561 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200562
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900563 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200564 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900565 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200566 return 1;
567
568 return 0;
569}
570
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300571static int printk_map_init(struct pevent *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200572{
573 struct printk_list *printklist;
574 struct printk_list *item;
575 struct printk_map *printk_map;
576 int i;
577
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300578 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
579 if (!printk_map)
580 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200581
582 printklist = pevent->printklist;
583
584 i = 0;
585 while (printklist) {
586 printk_map[i].printk = printklist->printk;
587 printk_map[i].addr = printklist->addr;
588 i++;
589 item = printklist;
590 printklist = printklist->next;
591 free(item);
592 }
593
594 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
595
596 pevent->printk_map = printk_map;
597 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300598
599 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200600}
601
602static struct printk_map *
603find_printk(struct pevent *pevent, unsigned long long addr)
604{
605 struct printk_map *printk;
606 struct printk_map key;
607
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300608 if (!pevent->printk_map && printk_map_init(pevent))
609 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200610
611 key.addr = addr;
612
613 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
614 sizeof(*pevent->printk_map), printk_cmp);
615
616 return printk;
617}
618
619/**
620 * pevent_register_print_string - register a string by its address
621 * @pevent: handle for the pevent
622 * @fmt: the string format to register
623 * @addr: the address the string was located at
624 *
625 * This registers a string by the address it was stored in the kernel.
626 * The @fmt passed in is duplicated.
627 */
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400628int pevent_register_print_string(struct pevent *pevent, const char *fmt,
Steven Rostedtf7d82352012-04-06 00:47:53 +0200629 unsigned long long addr)
630{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300631 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400632 char *p;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200633
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300634 if (!item)
635 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200636
637 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200638 item->addr = addr;
639
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400640 /* Strip off quotes and '\n' from the end */
641 if (fmt[0] == '"')
642 fmt++;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300643 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900644 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300645 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900646
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400647 p = item->printk + strlen(item->printk) - 1;
648 if (*p == '"')
649 *p = 0;
650
651 p -= 2;
652 if (strcmp(p, "\\n") == 0)
653 *p = 0;
654
Namhyung Kimca638582012-04-09 11:54:31 +0900655 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200656 pevent->printk_count++;
657
658 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300659
660out_free:
661 free(item);
662 errno = ENOMEM;
663 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200664}
665
666/**
667 * pevent_print_printk - print out the stored strings
668 * @pevent: handle for the pevent
669 *
670 * This prints the string formats that were stored.
671 */
672void pevent_print_printk(struct pevent *pevent)
673{
674 int i;
675
676 if (!pevent->printk_map)
677 printk_map_init(pevent);
678
679 for (i = 0; i < (int)pevent->printk_count; i++) {
680 printf("%016llx %s\n",
681 pevent->printk_map[i].addr,
682 pevent->printk_map[i].printk);
683 }
684}
685
686static struct event_format *alloc_event(void)
687{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300688 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200689}
690
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300691static int add_event(struct pevent *pevent, struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200692{
693 int i;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300694 struct event_format **events = realloc(pevent->events, sizeof(event) *
695 (pevent->nr_events + 1));
696 if (!events)
697 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200698
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300699 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200700
701 for (i = 0; i < pevent->nr_events; i++) {
702 if (pevent->events[i]->id > event->id)
703 break;
704 }
705 if (i < pevent->nr_events)
706 memmove(&pevent->events[i + 1],
707 &pevent->events[i],
708 sizeof(event) * (pevent->nr_events - i));
709
710 pevent->events[i] = event;
711 pevent->nr_events++;
712
713 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300714
715 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200716}
717
718static int event_item_type(enum event_type type)
719{
720 switch (type) {
721 case EVENT_ITEM ... EVENT_SQUOTE:
722 return 1;
723 case EVENT_ERROR ... EVENT_DELIM:
724 default:
725 return 0;
726 }
727}
728
729static void free_flag_sym(struct print_flag_sym *fsym)
730{
731 struct print_flag_sym *next;
732
733 while (fsym) {
734 next = fsym->next;
735 free(fsym->value);
736 free(fsym->str);
737 free(fsym);
738 fsym = next;
739 }
740}
741
742static void free_arg(struct print_arg *arg)
743{
744 struct print_arg *farg;
745
746 if (!arg)
747 return;
748
749 switch (arg->type) {
750 case PRINT_ATOM:
751 free(arg->atom.atom);
752 break;
753 case PRINT_FIELD:
754 free(arg->field.name);
755 break;
756 case PRINT_FLAGS:
757 free_arg(arg->flags.field);
758 free(arg->flags.delim);
759 free_flag_sym(arg->flags.flags);
760 break;
761 case PRINT_SYMBOL:
762 free_arg(arg->symbol.field);
763 free_flag_sym(arg->symbol.symbols);
764 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900765 case PRINT_HEX:
766 free_arg(arg->hex.field);
767 free_arg(arg->hex.size);
768 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200769 case PRINT_TYPE:
770 free(arg->typecast.type);
771 free_arg(arg->typecast.item);
772 break;
773 case PRINT_STRING:
774 case PRINT_BSTRING:
775 free(arg->string.string);
776 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400777 case PRINT_BITMASK:
778 free(arg->bitmask.bitmask);
779 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200780 case PRINT_DYNAMIC_ARRAY:
781 free(arg->dynarray.index);
782 break;
783 case PRINT_OP:
784 free(arg->op.op);
785 free_arg(arg->op.left);
786 free_arg(arg->op.right);
787 break;
788 case PRINT_FUNC:
789 while (arg->func.args) {
790 farg = arg->func.args;
791 arg->func.args = farg->next;
792 free_arg(farg);
793 }
794 break;
795
796 case PRINT_NULL:
797 default:
798 break;
799 }
800
801 free(arg);
802}
803
804static enum event_type get_type(int ch)
805{
806 if (ch == '\n')
807 return EVENT_NEWLINE;
808 if (isspace(ch))
809 return EVENT_SPACE;
810 if (isalnum(ch) || ch == '_')
811 return EVENT_ITEM;
812 if (ch == '\'')
813 return EVENT_SQUOTE;
814 if (ch == '"')
815 return EVENT_DQUOTE;
816 if (!isprint(ch))
817 return EVENT_NONE;
818 if (ch == '(' || ch == ')' || ch == ',')
819 return EVENT_DELIM;
820
821 return EVENT_OP;
822}
823
824static int __read_char(void)
825{
826 if (input_buf_ptr >= input_buf_siz)
827 return -1;
828
829 return input_buf[input_buf_ptr++];
830}
831
832static int __peek_char(void)
833{
834 if (input_buf_ptr >= input_buf_siz)
835 return -1;
836
837 return input_buf[input_buf_ptr];
838}
839
840/**
841 * pevent_peek_char - peek at the next character that will be read
842 *
843 * Returns the next character read, or -1 if end of buffer.
844 */
845int pevent_peek_char(void)
846{
847 return __peek_char();
848}
849
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900850static int extend_token(char **tok, char *buf, int size)
851{
852 char *newtok = realloc(*tok, size);
853
854 if (!newtok) {
855 free(*tok);
856 *tok = NULL;
857 return -1;
858 }
859
860 if (!*tok)
861 strcpy(newtok, buf);
862 else
863 strcat(newtok, buf);
864 *tok = newtok;
865
866 return 0;
867}
868
Steven Rostedtf7d82352012-04-06 00:47:53 +0200869static enum event_type force_token(const char *str, char **tok);
870
871static enum event_type __read_token(char **tok)
872{
873 char buf[BUFSIZ];
874 int ch, last_ch, quote_ch, next_ch;
875 int i = 0;
876 int tok_size = 0;
877 enum event_type type;
878
879 *tok = NULL;
880
881
882 ch = __read_char();
883 if (ch < 0)
884 return EVENT_NONE;
885
886 type = get_type(ch);
887 if (type == EVENT_NONE)
888 return type;
889
890 buf[i++] = ch;
891
892 switch (type) {
893 case EVENT_NEWLINE:
894 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300895 if (asprintf(tok, "%c", ch) < 0)
896 return EVENT_ERROR;
897
Steven Rostedtf7d82352012-04-06 00:47:53 +0200898 return type;
899
900 case EVENT_OP:
901 switch (ch) {
902 case '-':
903 next_ch = __peek_char();
904 if (next_ch == '>') {
905 buf[i++] = __read_char();
906 break;
907 }
908 /* fall through */
909 case '+':
910 case '|':
911 case '&':
912 case '>':
913 case '<':
914 last_ch = ch;
915 ch = __peek_char();
916 if (ch != last_ch)
917 goto test_equal;
918 buf[i++] = __read_char();
919 switch (last_ch) {
920 case '>':
921 case '<':
922 goto test_equal;
923 default:
924 break;
925 }
926 break;
927 case '!':
928 case '=':
929 goto test_equal;
930 default: /* what should we do instead? */
931 break;
932 }
933 buf[i] = 0;
934 *tok = strdup(buf);
935 return type;
936
937 test_equal:
938 ch = __peek_char();
939 if (ch == '=')
940 buf[i++] = __read_char();
941 goto out;
942
943 case EVENT_DQUOTE:
944 case EVENT_SQUOTE:
945 /* don't keep quotes */
946 i--;
947 quote_ch = ch;
948 last_ch = 0;
949 concat:
950 do {
951 if (i == (BUFSIZ - 1)) {
952 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200953 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900954
955 if (extend_token(tok, buf, tok_size) < 0)
956 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200957 i = 0;
958 }
959 last_ch = ch;
960 ch = __read_char();
961 buf[i++] = ch;
962 /* the '\' '\' will cancel itself */
963 if (ch == '\\' && last_ch == '\\')
964 last_ch = 0;
965 } while (ch != quote_ch || last_ch == '\\');
966 /* remove the last quote */
967 i--;
968
969 /*
970 * For strings (double quotes) check the next token.
971 * If it is another string, concatinate the two.
972 */
973 if (type == EVENT_DQUOTE) {
974 unsigned long long save_input_buf_ptr = input_buf_ptr;
975
976 do {
977 ch = __read_char();
978 } while (isspace(ch));
979 if (ch == '"')
980 goto concat;
981 input_buf_ptr = save_input_buf_ptr;
982 }
983
984 goto out;
985
986 case EVENT_ERROR ... EVENT_SPACE:
987 case EVENT_ITEM:
988 default:
989 break;
990 }
991
992 while (get_type(__peek_char()) == type) {
993 if (i == (BUFSIZ - 1)) {
994 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200995 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900996
997 if (extend_token(tok, buf, tok_size) < 0)
998 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200999 i = 0;
1000 }
1001 ch = __read_char();
1002 buf[i++] = ch;
1003 }
1004
1005 out:
1006 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001007 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001008 return EVENT_NONE;
1009
1010 if (type == EVENT_ITEM) {
1011 /*
1012 * Older versions of the kernel has a bug that
1013 * creates invalid symbols and will break the mac80211
1014 * parsing. This is a work around to that bug.
1015 *
1016 * See Linux kernel commit:
1017 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1018 */
1019 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1020 free(*tok);
1021 *tok = NULL;
1022 return force_token("\"\%s\" ", tok);
1023 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1024 free(*tok);
1025 *tok = NULL;
1026 return force_token("\" sta:%pM\" ", tok);
1027 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1028 free(*tok);
1029 *tok = NULL;
1030 return force_token("\" vif:%p(%d)\" ", tok);
1031 }
1032 }
1033
1034 return type;
1035}
1036
1037static enum event_type force_token(const char *str, char **tok)
1038{
1039 const char *save_input_buf;
1040 unsigned long long save_input_buf_ptr;
1041 unsigned long long save_input_buf_siz;
1042 enum event_type type;
1043
1044 /* save off the current input pointers */
1045 save_input_buf = input_buf;
1046 save_input_buf_ptr = input_buf_ptr;
1047 save_input_buf_siz = input_buf_siz;
1048
1049 init_input_buf(str, strlen(str));
1050
1051 type = __read_token(tok);
1052
1053 /* reset back to original token */
1054 input_buf = save_input_buf;
1055 input_buf_ptr = save_input_buf_ptr;
1056 input_buf_siz = save_input_buf_siz;
1057
1058 return type;
1059}
1060
1061static void free_token(char *tok)
1062{
1063 if (tok)
1064 free(tok);
1065}
1066
1067static enum event_type read_token(char **tok)
1068{
1069 enum event_type type;
1070
1071 for (;;) {
1072 type = __read_token(tok);
1073 if (type != EVENT_SPACE)
1074 return type;
1075
1076 free_token(*tok);
1077 }
1078
1079 /* not reached */
1080 *tok = NULL;
1081 return EVENT_NONE;
1082}
1083
1084/**
1085 * pevent_read_token - access to utilites to use the pevent parser
1086 * @tok: The token to return
1087 *
1088 * This will parse tokens from the string given by
1089 * pevent_init_data().
1090 *
1091 * Returns the token type.
1092 */
1093enum event_type pevent_read_token(char **tok)
1094{
1095 return read_token(tok);
1096}
1097
1098/**
1099 * pevent_free_token - free a token returned by pevent_read_token
1100 * @token: the token to free
1101 */
1102void pevent_free_token(char *token)
1103{
1104 free_token(token);
1105}
1106
1107/* no newline */
1108static enum event_type read_token_item(char **tok)
1109{
1110 enum event_type type;
1111
1112 for (;;) {
1113 type = __read_token(tok);
1114 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1115 return type;
1116 free_token(*tok);
1117 *tok = NULL;
1118 }
1119
1120 /* not reached */
1121 *tok = NULL;
1122 return EVENT_NONE;
1123}
1124
1125static int test_type(enum event_type type, enum event_type expect)
1126{
1127 if (type != expect) {
1128 do_warning("Error: expected type %d but read %d",
1129 expect, type);
1130 return -1;
1131 }
1132 return 0;
1133}
1134
1135static int test_type_token(enum event_type type, const char *token,
1136 enum event_type expect, const char *expect_tok)
1137{
1138 if (type != expect) {
1139 do_warning("Error: expected type %d but read %d",
1140 expect, type);
1141 return -1;
1142 }
1143
1144 if (strcmp(token, expect_tok) != 0) {
1145 do_warning("Error: expected '%s' but read '%s'",
1146 expect_tok, token);
1147 return -1;
1148 }
1149 return 0;
1150}
1151
1152static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1153{
1154 enum event_type type;
1155
1156 if (newline_ok)
1157 type = read_token(tok);
1158 else
1159 type = read_token_item(tok);
1160 return test_type(type, expect);
1161}
1162
1163static int read_expect_type(enum event_type expect, char **tok)
1164{
1165 return __read_expect_type(expect, tok, 1);
1166}
1167
1168static int __read_expected(enum event_type expect, const char *str,
1169 int newline_ok)
1170{
1171 enum event_type type;
1172 char *token;
1173 int ret;
1174
1175 if (newline_ok)
1176 type = read_token(&token);
1177 else
1178 type = read_token_item(&token);
1179
1180 ret = test_type_token(type, token, expect, str);
1181
1182 free_token(token);
1183
1184 return ret;
1185}
1186
1187static int read_expected(enum event_type expect, const char *str)
1188{
1189 return __read_expected(expect, str, 1);
1190}
1191
1192static int read_expected_item(enum event_type expect, const char *str)
1193{
1194 return __read_expected(expect, str, 0);
1195}
1196
1197static char *event_read_name(void)
1198{
1199 char *token;
1200
1201 if (read_expected(EVENT_ITEM, "name") < 0)
1202 return NULL;
1203
1204 if (read_expected(EVENT_OP, ":") < 0)
1205 return NULL;
1206
1207 if (read_expect_type(EVENT_ITEM, &token) < 0)
1208 goto fail;
1209
1210 return token;
1211
1212 fail:
1213 free_token(token);
1214 return NULL;
1215}
1216
1217static int event_read_id(void)
1218{
1219 char *token;
1220 int id;
1221
1222 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1223 return -1;
1224
1225 if (read_expected(EVENT_OP, ":") < 0)
1226 return -1;
1227
1228 if (read_expect_type(EVENT_ITEM, &token) < 0)
1229 goto fail;
1230
1231 id = strtoul(token, NULL, 0);
1232 free_token(token);
1233 return id;
1234
1235 fail:
1236 free_token(token);
1237 return -1;
1238}
1239
1240static int field_is_string(struct format_field *field)
1241{
1242 if ((field->flags & FIELD_IS_ARRAY) &&
1243 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1244 strstr(field->type, "s8")))
1245 return 1;
1246
1247 return 0;
1248}
1249
1250static int field_is_dynamic(struct format_field *field)
1251{
1252 if (strncmp(field->type, "__data_loc", 10) == 0)
1253 return 1;
1254
1255 return 0;
1256}
1257
1258static int field_is_long(struct format_field *field)
1259{
1260 /* includes long long */
1261 if (strstr(field->type, "long"))
1262 return 1;
1263
1264 return 0;
1265}
1266
Jiri Olsae23c1a52013-01-24 21:46:43 +01001267static unsigned int type_size(const char *name)
1268{
1269 /* This covers all FIELD_IS_STRING types. */
1270 static struct {
1271 const char *type;
1272 unsigned int size;
1273 } table[] = {
1274 { "u8", 1 },
1275 { "u16", 2 },
1276 { "u32", 4 },
1277 { "u64", 8 },
1278 { "s8", 1 },
1279 { "s16", 2 },
1280 { "s32", 4 },
1281 { "s64", 8 },
1282 { "char", 1 },
1283 { },
1284 };
1285 int i;
1286
1287 for (i = 0; table[i].type; i++) {
1288 if (!strcmp(table[i].type, name))
1289 return table[i].size;
1290 }
1291
1292 return 0;
1293}
1294
Steven Rostedtf7d82352012-04-06 00:47:53 +02001295static int event_read_fields(struct event_format *event, struct format_field **fields)
1296{
1297 struct format_field *field = NULL;
1298 enum event_type type;
1299 char *token;
1300 char *last_token;
1301 int count = 0;
1302
1303 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001304 unsigned int size_dynamic = 0;
1305
Steven Rostedtf7d82352012-04-06 00:47:53 +02001306 type = read_token(&token);
1307 if (type == EVENT_NEWLINE) {
1308 free_token(token);
1309 return count;
1310 }
1311
1312 count++;
1313
1314 if (test_type_token(type, token, EVENT_ITEM, "field"))
1315 goto fail;
1316 free_token(token);
1317
1318 type = read_token(&token);
1319 /*
1320 * The ftrace fields may still use the "special" name.
1321 * Just ignore it.
1322 */
1323 if (event->flags & EVENT_FL_ISFTRACE &&
1324 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1325 free_token(token);
1326 type = read_token(&token);
1327 }
1328
1329 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1330 goto fail;
1331
1332 free_token(token);
1333 if (read_expect_type(EVENT_ITEM, &token) < 0)
1334 goto fail;
1335
1336 last_token = token;
1337
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001338 field = calloc(1, sizeof(*field));
1339 if (!field)
1340 goto fail;
1341
Steven Rostedtf7d82352012-04-06 00:47:53 +02001342 field->event = event;
1343
1344 /* read the rest of the type */
1345 for (;;) {
1346 type = read_token(&token);
1347 if (type == EVENT_ITEM ||
1348 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1349 /*
1350 * Some of the ftrace fields are broken and have
1351 * an illegal "." in them.
1352 */
1353 (event->flags & EVENT_FL_ISFTRACE &&
1354 type == EVENT_OP && strcmp(token, ".") == 0)) {
1355
1356 if (strcmp(token, "*") == 0)
1357 field->flags |= FIELD_IS_POINTER;
1358
1359 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001360 char *new_type;
1361 new_type = realloc(field->type,
1362 strlen(field->type) +
1363 strlen(last_token) + 2);
1364 if (!new_type) {
1365 free(last_token);
1366 goto fail;
1367 }
1368 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001369 strcat(field->type, " ");
1370 strcat(field->type, last_token);
1371 free(last_token);
1372 } else
1373 field->type = last_token;
1374 last_token = token;
1375 continue;
1376 }
1377
1378 break;
1379 }
1380
1381 if (!field->type) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001382 do_warning_event(event, "%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001383 goto fail;
1384 }
1385 field->name = last_token;
1386
1387 if (test_type(type, EVENT_OP))
1388 goto fail;
1389
1390 if (strcmp(token, "[") == 0) {
1391 enum event_type last_type = type;
1392 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001393 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001394 int len;
1395
1396 field->flags |= FIELD_IS_ARRAY;
1397
1398 type = read_token(&token);
1399
1400 if (type == EVENT_ITEM)
1401 field->arraylen = strtoul(token, NULL, 0);
1402 else
1403 field->arraylen = 0;
1404
1405 while (strcmp(token, "]") != 0) {
1406 if (last_type == EVENT_ITEM &&
1407 type == EVENT_ITEM)
1408 len = 2;
1409 else
1410 len = 1;
1411 last_type = type;
1412
Namhyung Kimd2864472012-04-09 11:54:33 +09001413 new_brackets = realloc(brackets,
1414 strlen(brackets) +
1415 strlen(token) + len);
1416 if (!new_brackets) {
1417 free(brackets);
1418 goto fail;
1419 }
1420 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001421 if (len == 2)
1422 strcat(brackets, " ");
1423 strcat(brackets, token);
1424 /* We only care about the last token */
1425 field->arraylen = strtoul(token, NULL, 0);
1426 free_token(token);
1427 type = read_token(&token);
1428 if (type == EVENT_NONE) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001429 do_warning_event(event, "failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001430 goto fail;
1431 }
1432 }
1433
1434 free_token(token);
1435
Namhyung Kimd2864472012-04-09 11:54:33 +09001436 new_brackets = realloc(brackets, strlen(brackets) + 2);
1437 if (!new_brackets) {
1438 free(brackets);
1439 goto fail;
1440 }
1441 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001442 strcat(brackets, "]");
1443
1444 /* add brackets to type */
1445
1446 type = read_token(&token);
1447 /*
1448 * If the next token is not an OP, then it is of
1449 * the format: type [] item;
1450 */
1451 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001452 char *new_type;
1453 new_type = realloc(field->type,
1454 strlen(field->type) +
1455 strlen(field->name) +
1456 strlen(brackets) + 2);
1457 if (!new_type) {
1458 free(brackets);
1459 goto fail;
1460 }
1461 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001462 strcat(field->type, " ");
1463 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001464 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001465 free_token(field->name);
1466 strcat(field->type, brackets);
1467 field->name = token;
1468 type = read_token(&token);
1469 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001470 char *new_type;
1471 new_type = realloc(field->type,
1472 strlen(field->type) +
1473 strlen(brackets) + 1);
1474 if (!new_type) {
1475 free(brackets);
1476 goto fail;
1477 }
1478 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001479 strcat(field->type, brackets);
1480 }
1481 free(brackets);
1482 }
1483
1484 if (field_is_string(field))
1485 field->flags |= FIELD_IS_STRING;
1486 if (field_is_dynamic(field))
1487 field->flags |= FIELD_IS_DYNAMIC;
1488 if (field_is_long(field))
1489 field->flags |= FIELD_IS_LONG;
1490
1491 if (test_type_token(type, token, EVENT_OP, ";"))
1492 goto fail;
1493 free_token(token);
1494
1495 if (read_expected(EVENT_ITEM, "offset") < 0)
1496 goto fail_expect;
1497
1498 if (read_expected(EVENT_OP, ":") < 0)
1499 goto fail_expect;
1500
1501 if (read_expect_type(EVENT_ITEM, &token))
1502 goto fail;
1503 field->offset = strtoul(token, NULL, 0);
1504 free_token(token);
1505
1506 if (read_expected(EVENT_OP, ";") < 0)
1507 goto fail_expect;
1508
1509 if (read_expected(EVENT_ITEM, "size") < 0)
1510 goto fail_expect;
1511
1512 if (read_expected(EVENT_OP, ":") < 0)
1513 goto fail_expect;
1514
1515 if (read_expect_type(EVENT_ITEM, &token))
1516 goto fail;
1517 field->size = strtoul(token, NULL, 0);
1518 free_token(token);
1519
1520 if (read_expected(EVENT_OP, ";") < 0)
1521 goto fail_expect;
1522
1523 type = read_token(&token);
1524 if (type != EVENT_NEWLINE) {
1525 /* newer versions of the kernel have a "signed" type */
1526 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1527 goto fail;
1528
1529 free_token(token);
1530
1531 if (read_expected(EVENT_OP, ":") < 0)
1532 goto fail_expect;
1533
1534 if (read_expect_type(EVENT_ITEM, &token))
1535 goto fail;
1536
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001537 if (strtoul(token, NULL, 0))
1538 field->flags |= FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001539
1540 free_token(token);
1541 if (read_expected(EVENT_OP, ";") < 0)
1542 goto fail_expect;
1543
1544 if (read_expect_type(EVENT_NEWLINE, &token))
1545 goto fail;
1546 }
1547
1548 free_token(token);
1549
1550 if (field->flags & FIELD_IS_ARRAY) {
1551 if (field->arraylen)
1552 field->elementsize = field->size / field->arraylen;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001553 else if (field->flags & FIELD_IS_DYNAMIC)
1554 field->elementsize = size_dynamic;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001555 else if (field->flags & FIELD_IS_STRING)
1556 field->elementsize = 1;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001557 else if (field->flags & FIELD_IS_LONG)
1558 field->elementsize = event->pevent ?
1559 event->pevent->long_size :
1560 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001561 } else
1562 field->elementsize = field->size;
1563
1564 *fields = field;
1565 fields = &field->next;
1566
1567 } while (1);
1568
1569 return 0;
1570
1571fail:
1572 free_token(token);
1573fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001574 if (field) {
1575 free(field->type);
1576 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001577 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001578 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001579 return -1;
1580}
1581
1582static int event_read_format(struct event_format *event)
1583{
1584 char *token;
1585 int ret;
1586
1587 if (read_expected_item(EVENT_ITEM, "format") < 0)
1588 return -1;
1589
1590 if (read_expected(EVENT_OP, ":") < 0)
1591 return -1;
1592
1593 if (read_expect_type(EVENT_NEWLINE, &token))
1594 goto fail;
1595 free_token(token);
1596
1597 ret = event_read_fields(event, &event->format.common_fields);
1598 if (ret < 0)
1599 return ret;
1600 event->format.nr_common = ret;
1601
1602 ret = event_read_fields(event, &event->format.fields);
1603 if (ret < 0)
1604 return ret;
1605 event->format.nr_fields = ret;
1606
1607 return 0;
1608
1609 fail:
1610 free_token(token);
1611 return -1;
1612}
1613
1614static enum event_type
1615process_arg_token(struct event_format *event, struct print_arg *arg,
1616 char **tok, enum event_type type);
1617
1618static enum event_type
1619process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1620{
1621 enum event_type type;
1622 char *token;
1623
1624 type = read_token(&token);
1625 *tok = token;
1626
1627 return process_arg_token(event, arg, tok, type);
1628}
1629
1630static enum event_type
1631process_op(struct event_format *event, struct print_arg *arg, char **tok);
1632
Steven Rostedteff2c922013-11-18 14:23:14 -05001633/*
1634 * For __print_symbolic() and __print_flags, we need to completely
1635 * evaluate the first argument, which defines what to print next.
1636 */
1637static enum event_type
1638process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1639{
1640 enum event_type type;
1641
1642 type = process_arg(event, arg, tok);
1643
1644 while (type == EVENT_OP) {
1645 type = process_op(event, arg, tok);
1646 }
1647
1648 return type;
1649}
1650
Steven Rostedtf7d82352012-04-06 00:47:53 +02001651static enum event_type
1652process_cond(struct event_format *event, struct print_arg *top, char **tok)
1653{
1654 struct print_arg *arg, *left, *right;
1655 enum event_type type;
1656 char *token = NULL;
1657
1658 arg = alloc_arg();
1659 left = alloc_arg();
1660 right = alloc_arg();
1661
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001662 if (!arg || !left || !right) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001663 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001664 /* arg will be freed at out_free */
1665 free_arg(left);
1666 free_arg(right);
1667 goto out_free;
1668 }
1669
Steven Rostedtf7d82352012-04-06 00:47:53 +02001670 arg->type = PRINT_OP;
1671 arg->op.left = left;
1672 arg->op.right = right;
1673
1674 *tok = NULL;
1675 type = process_arg(event, left, &token);
1676
1677 again:
1678 /* Handle other operations in the arguments */
1679 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1680 type = process_op(event, left, &token);
1681 goto again;
1682 }
1683
1684 if (test_type_token(type, token, EVENT_OP, ":"))
1685 goto out_free;
1686
1687 arg->op.op = token;
1688
1689 type = process_arg(event, right, &token);
1690
1691 top->op.right = arg;
1692
1693 *tok = token;
1694 return type;
1695
1696out_free:
1697 /* Top may point to itself */
1698 top->op.right = NULL;
1699 free_token(token);
1700 free_arg(arg);
1701 return EVENT_ERROR;
1702}
1703
1704static enum event_type
1705process_array(struct event_format *event, struct print_arg *top, char **tok)
1706{
1707 struct print_arg *arg;
1708 enum event_type type;
1709 char *token = NULL;
1710
1711 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001712 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001713 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001714 /* '*tok' is set to top->op.op. No need to free. */
1715 *tok = NULL;
1716 return EVENT_ERROR;
1717 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001718
1719 *tok = NULL;
1720 type = process_arg(event, arg, &token);
1721 if (test_type_token(type, token, EVENT_OP, "]"))
1722 goto out_free;
1723
1724 top->op.right = arg;
1725
1726 free_token(token);
1727 type = read_token_item(&token);
1728 *tok = token;
1729
1730 return type;
1731
1732out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001733 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001734 free_arg(arg);
1735 return EVENT_ERROR;
1736}
1737
1738static int get_op_prio(char *op)
1739{
1740 if (!op[1]) {
1741 switch (op[0]) {
1742 case '~':
1743 case '!':
1744 return 4;
1745 case '*':
1746 case '/':
1747 case '%':
1748 return 6;
1749 case '+':
1750 case '-':
1751 return 7;
1752 /* '>>' and '<<' are 8 */
1753 case '<':
1754 case '>':
1755 return 9;
1756 /* '==' and '!=' are 10 */
1757 case '&':
1758 return 11;
1759 case '^':
1760 return 12;
1761 case '|':
1762 return 13;
1763 case '?':
1764 return 16;
1765 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001766 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001767 return -1;
1768 }
1769 } else {
1770 if (strcmp(op, "++") == 0 ||
1771 strcmp(op, "--") == 0) {
1772 return 3;
1773 } else if (strcmp(op, ">>") == 0 ||
1774 strcmp(op, "<<") == 0) {
1775 return 8;
1776 } else if (strcmp(op, ">=") == 0 ||
1777 strcmp(op, "<=") == 0) {
1778 return 9;
1779 } else if (strcmp(op, "==") == 0 ||
1780 strcmp(op, "!=") == 0) {
1781 return 10;
1782 } else if (strcmp(op, "&&") == 0) {
1783 return 14;
1784 } else if (strcmp(op, "||") == 0) {
1785 return 15;
1786 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001787 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001788 return -1;
1789 }
1790 }
1791}
1792
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001793static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001794{
1795
1796 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001797 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001798 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001799 else
1800 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001801
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001802 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001803}
1804
1805/* Note, *tok does not get freed, but will most likely be saved */
1806static enum event_type
1807process_op(struct event_format *event, struct print_arg *arg, char **tok)
1808{
1809 struct print_arg *left, *right = NULL;
1810 enum event_type type;
1811 char *token;
1812
1813 /* the op is passed in via tok */
1814 token = *tok;
1815
1816 if (arg->type == PRINT_OP && !arg->op.left) {
1817 /* handle single op */
1818 if (token[1]) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001819 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001820 goto out_free;
1821 }
1822 switch (token[0]) {
1823 case '~':
1824 case '!':
1825 case '+':
1826 case '-':
1827 break;
1828 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001829 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001830 goto out_free;
1831
1832 }
1833
1834 /* make an empty left */
1835 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001836 if (!left)
1837 goto out_warn_free;
1838
Steven Rostedtf7d82352012-04-06 00:47:53 +02001839 left->type = PRINT_NULL;
1840 arg->op.left = left;
1841
1842 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001843 if (!right)
1844 goto out_warn_free;
1845
Steven Rostedtf7d82352012-04-06 00:47:53 +02001846 arg->op.right = right;
1847
1848 /* do not free the token, it belongs to an op */
1849 *tok = NULL;
1850 type = process_arg(event, right, tok);
1851
1852 } else if (strcmp(token, "?") == 0) {
1853
1854 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001855 if (!left)
1856 goto out_warn_free;
1857
Steven Rostedtf7d82352012-04-06 00:47:53 +02001858 /* copy the top arg to the left */
1859 *left = *arg;
1860
1861 arg->type = PRINT_OP;
1862 arg->op.op = token;
1863 arg->op.left = left;
1864 arg->op.prio = 0;
1865
Namhyung Kim41e51a22012-09-19 15:58:42 +09001866 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001867 type = process_cond(event, arg, tok);
1868
1869 } else if (strcmp(token, ">>") == 0 ||
1870 strcmp(token, "<<") == 0 ||
1871 strcmp(token, "&") == 0 ||
1872 strcmp(token, "|") == 0 ||
1873 strcmp(token, "&&") == 0 ||
1874 strcmp(token, "||") == 0 ||
1875 strcmp(token, "-") == 0 ||
1876 strcmp(token, "+") == 0 ||
1877 strcmp(token, "*") == 0 ||
1878 strcmp(token, "^") == 0 ||
1879 strcmp(token, "/") == 0 ||
1880 strcmp(token, "<") == 0 ||
1881 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001882 strcmp(token, "<=") == 0 ||
1883 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001884 strcmp(token, "==") == 0 ||
1885 strcmp(token, "!=") == 0) {
1886
1887 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001888 if (!left)
1889 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001890
1891 /* copy the top arg to the left */
1892 *left = *arg;
1893
1894 arg->type = PRINT_OP;
1895 arg->op.op = token;
1896 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001897 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001898
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001899 if (set_op_prio(arg) == -1) {
1900 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001901 /* arg->op.op (= token) will be freed at out_free */
1902 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001903 goto out_free;
1904 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001905
1906 type = read_token_item(&token);
1907 *tok = token;
1908
1909 /* could just be a type pointer */
1910 if ((strcmp(arg->op.op, "*") == 0) &&
1911 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001912 char *new_atom;
1913
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001914 if (left->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001915 do_warning_event(event, "bad pointer type");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001916 goto out_free;
1917 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001918 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001919 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001920 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001921 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001922
1923 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001924 strcat(left->atom.atom, " *");
1925 free(arg->op.op);
1926 *arg = *left;
1927 free(left);
1928
1929 return type;
1930 }
1931
1932 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001933 if (!right)
1934 goto out_warn_free;
1935
Steven Rostedtf7d82352012-04-06 00:47:53 +02001936 type = process_arg_token(event, right, tok, type);
1937 arg->op.right = right;
1938
1939 } else if (strcmp(token, "[") == 0) {
1940
1941 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001942 if (!left)
1943 goto out_warn_free;
1944
Steven Rostedtf7d82352012-04-06 00:47:53 +02001945 *left = *arg;
1946
1947 arg->type = PRINT_OP;
1948 arg->op.op = token;
1949 arg->op.left = left;
1950
1951 arg->op.prio = 0;
1952
Namhyung Kim41e51a22012-09-19 15:58:42 +09001953 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001954 type = process_array(event, arg, tok);
1955
1956 } else {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001957 do_warning_event(event, "unknown op '%s'", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001958 event->flags |= EVENT_FL_FAILED;
1959 /* the arg is now the left side */
1960 goto out_free;
1961 }
1962
1963 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1964 int prio;
1965
1966 /* higher prios need to be closer to the root */
1967 prio = get_op_prio(*tok);
1968
1969 if (prio > arg->op.prio)
1970 return process_op(event, arg, tok);
1971
1972 return process_op(event, right, tok);
1973 }
1974
1975 return type;
1976
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001977out_warn_free:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001978 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001979out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001980 free_token(token);
1981 *tok = NULL;
1982 return EVENT_ERROR;
1983}
1984
1985static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001986process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001987 char **tok)
1988{
1989 enum event_type type;
1990 char *field;
1991 char *token;
1992
1993 if (read_expected(EVENT_OP, "->") < 0)
1994 goto out_err;
1995
1996 if (read_expect_type(EVENT_ITEM, &token) < 0)
1997 goto out_free;
1998 field = token;
1999
2000 arg->type = PRINT_FIELD;
2001 arg->field.name = field;
2002
Tom Zanussi5205aec2012-04-06 00:47:58 +02002003 if (is_flag_field) {
2004 arg->field.field = pevent_find_any_field(event, arg->field.name);
2005 arg->field.field->flags |= FIELD_IS_FLAG;
2006 is_flag_field = 0;
2007 } else if (is_symbolic_field) {
2008 arg->field.field = pevent_find_any_field(event, arg->field.name);
2009 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2010 is_symbolic_field = 0;
2011 }
2012
Steven Rostedtf7d82352012-04-06 00:47:53 +02002013 type = read_token(&token);
2014 *tok = token;
2015
2016 return type;
2017
2018 out_free:
2019 free_token(token);
2020 out_err:
2021 *tok = NULL;
2022 return EVENT_ERROR;
2023}
2024
Javi Merino929a6bb2015-03-20 18:12:55 +00002025static int alloc_and_process_delim(struct event_format *event, char *next_token,
2026 struct print_arg **print_arg)
2027{
2028 struct print_arg *field;
2029 enum event_type type;
2030 char *token;
2031 int ret = 0;
2032
2033 field = alloc_arg();
2034 if (!field) {
2035 do_warning_event(event, "%s: not enough memory!", __func__);
2036 errno = ENOMEM;
2037 return -1;
2038 }
2039
2040 type = process_arg(event, field, &token);
2041
2042 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2043 errno = EINVAL;
2044 ret = -1;
2045 free_arg(field);
2046 goto out_free_token;
2047 }
2048
2049 *print_arg = field;
2050
2051out_free_token:
2052 free_token(token);
2053
2054 return ret;
2055}
2056
Steven Rostedtf7d82352012-04-06 00:47:53 +02002057static char *arg_eval (struct print_arg *arg);
2058
2059static unsigned long long
2060eval_type_str(unsigned long long val, const char *type, int pointer)
2061{
2062 int sign = 0;
2063 char *ref;
2064 int len;
2065
2066 len = strlen(type);
2067
2068 if (pointer) {
2069
2070 if (type[len-1] != '*') {
2071 do_warning("pointer expected with non pointer type");
2072 return val;
2073 }
2074
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002075 ref = malloc(len);
2076 if (!ref) {
2077 do_warning("%s: not enough memory!", __func__);
2078 return val;
2079 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002080 memcpy(ref, type, len);
2081
2082 /* chop off the " *" */
2083 ref[len - 2] = 0;
2084
2085 val = eval_type_str(val, ref, 0);
2086 free(ref);
2087 return val;
2088 }
2089
2090 /* check if this is a pointer */
2091 if (type[len - 1] == '*')
2092 return val;
2093
2094 /* Try to figure out the arg size*/
2095 if (strncmp(type, "struct", 6) == 0)
2096 /* all bets off */
2097 return val;
2098
2099 if (strcmp(type, "u8") == 0)
2100 return val & 0xff;
2101
2102 if (strcmp(type, "u16") == 0)
2103 return val & 0xffff;
2104
2105 if (strcmp(type, "u32") == 0)
2106 return val & 0xffffffff;
2107
2108 if (strcmp(type, "u64") == 0 ||
2109 strcmp(type, "s64"))
2110 return val;
2111
2112 if (strcmp(type, "s8") == 0)
2113 return (unsigned long long)(char)val & 0xff;
2114
2115 if (strcmp(type, "s16") == 0)
2116 return (unsigned long long)(short)val & 0xffff;
2117
2118 if (strcmp(type, "s32") == 0)
2119 return (unsigned long long)(int)val & 0xffffffff;
2120
2121 if (strncmp(type, "unsigned ", 9) == 0) {
2122 sign = 0;
2123 type += 9;
2124 }
2125
2126 if (strcmp(type, "char") == 0) {
2127 if (sign)
2128 return (unsigned long long)(char)val & 0xff;
2129 else
2130 return val & 0xff;
2131 }
2132
2133 if (strcmp(type, "short") == 0) {
2134 if (sign)
2135 return (unsigned long long)(short)val & 0xffff;
2136 else
2137 return val & 0xffff;
2138 }
2139
2140 if (strcmp(type, "int") == 0) {
2141 if (sign)
2142 return (unsigned long long)(int)val & 0xffffffff;
2143 else
2144 return val & 0xffffffff;
2145 }
2146
2147 return val;
2148}
2149
2150/*
2151 * Try to figure out the type.
2152 */
2153static unsigned long long
2154eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2155{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002156 if (arg->type != PRINT_TYPE) {
2157 do_warning("expected type argument");
2158 return 0;
2159 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002160
2161 return eval_type_str(val, arg->typecast.type, pointer);
2162}
2163
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002164static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002165{
2166 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002167 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002168
2169 switch (arg->type) {
2170 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002171 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002172 break;
2173 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002174 ret = arg_num_eval(arg->typecast.item, val);
2175 if (!ret)
2176 break;
2177 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002178 break;
2179 case PRINT_OP:
2180 switch (arg->op.op[0]) {
2181 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002182 ret = arg_num_eval(arg->op.left, &left);
2183 if (!ret)
2184 break;
2185 ret = arg_num_eval(arg->op.right, &right);
2186 if (!ret)
2187 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002188 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002189 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002190 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002191 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002192 break;
2193 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002194 ret = arg_num_eval(arg->op.left, &left);
2195 if (!ret)
2196 break;
2197 ret = arg_num_eval(arg->op.right, &right);
2198 if (!ret)
2199 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002200 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002201 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002202 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002203 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002204 break;
2205 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002206 ret = arg_num_eval(arg->op.left, &left);
2207 if (!ret)
2208 break;
2209 ret = arg_num_eval(arg->op.right, &right);
2210 if (!ret)
2211 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002212 switch (arg->op.op[1]) {
2213 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002214 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002215 break;
2216 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002217 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002218 break;
2219 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002220 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002221 break;
2222 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002223 do_warning("unknown op '%s'", arg->op.op);
2224 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002225 }
2226 break;
2227 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002228 ret = arg_num_eval(arg->op.left, &left);
2229 if (!ret)
2230 break;
2231 ret = arg_num_eval(arg->op.right, &right);
2232 if (!ret)
2233 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002234 switch (arg->op.op[1]) {
2235 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002236 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002237 break;
2238 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002239 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002240 break;
2241 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002242 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002243 break;
2244 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002245 do_warning("unknown op '%s'", arg->op.op);
2246 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002247 }
2248 break;
2249 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002250 ret = arg_num_eval(arg->op.left, &left);
2251 if (!ret)
2252 break;
2253 ret = arg_num_eval(arg->op.right, &right);
2254 if (!ret)
2255 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002256
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002257 if (arg->op.op[1] != '=') {
2258 do_warning("unknown op '%s'", arg->op.op);
2259 ret = 0;
2260 } else
2261 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002262 break;
2263 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002264 ret = arg_num_eval(arg->op.left, &left);
2265 if (!ret)
2266 break;
2267 ret = arg_num_eval(arg->op.right, &right);
2268 if (!ret)
2269 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002270
2271 switch (arg->op.op[1]) {
2272 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002273 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002274 break;
2275 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002276 do_warning("unknown op '%s'", arg->op.op);
2277 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002278 }
2279 break;
2280 case '-':
2281 /* check for negative */
2282 if (arg->op.left->type == PRINT_NULL)
2283 left = 0;
2284 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002285 ret = arg_num_eval(arg->op.left, &left);
2286 if (!ret)
2287 break;
2288 ret = arg_num_eval(arg->op.right, &right);
2289 if (!ret)
2290 break;
2291 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002292 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002293 case '+':
2294 if (arg->op.left->type == PRINT_NULL)
2295 left = 0;
2296 else
2297 ret = arg_num_eval(arg->op.left, &left);
2298 if (!ret)
2299 break;
2300 ret = arg_num_eval(arg->op.right, &right);
2301 if (!ret)
2302 break;
2303 *val = left + right;
2304 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002305 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002306 do_warning("unknown op '%s'", arg->op.op);
2307 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002308 }
2309 break;
2310
2311 case PRINT_NULL:
2312 case PRINT_FIELD ... PRINT_SYMBOL:
2313 case PRINT_STRING:
2314 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002315 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002316 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002317 do_warning("invalid eval type %d", arg->type);
2318 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002319
2320 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002321 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002322}
2323
2324static char *arg_eval (struct print_arg *arg)
2325{
2326 long long val;
2327 static char buf[20];
2328
2329 switch (arg->type) {
2330 case PRINT_ATOM:
2331 return arg->atom.atom;
2332 case PRINT_TYPE:
2333 return arg_eval(arg->typecast.item);
2334 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002335 if (!arg_num_eval(arg, &val))
2336 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002337 sprintf(buf, "%lld", val);
2338 return buf;
2339
2340 case PRINT_NULL:
2341 case PRINT_FIELD ... PRINT_SYMBOL:
2342 case PRINT_STRING:
2343 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002344 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002345 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002346 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002347 break;
2348 }
2349
2350 return NULL;
2351}
2352
2353static enum event_type
2354process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2355{
2356 enum event_type type;
2357 struct print_arg *arg = NULL;
2358 struct print_flag_sym *field;
2359 char *token = *tok;
2360 char *value;
2361
2362 do {
2363 free_token(token);
2364 type = read_token_item(&token);
2365 if (test_type_token(type, token, EVENT_OP, "{"))
2366 break;
2367
2368 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002369 if (!arg)
2370 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002371
2372 free_token(token);
2373 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002374
2375 if (type == EVENT_OP)
2376 type = process_op(event, arg, &token);
2377
2378 if (type == EVENT_ERROR)
2379 goto out_free;
2380
Steven Rostedtf7d82352012-04-06 00:47:53 +02002381 if (test_type_token(type, token, EVENT_DELIM, ","))
2382 goto out_free;
2383
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002384 field = calloc(1, sizeof(*field));
2385 if (!field)
2386 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002387
2388 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002389 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002390 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002391 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002392 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002393 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002394
2395 free_arg(arg);
2396 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002397 if (!arg)
2398 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002399
2400 free_token(token);
2401 type = process_arg(event, arg, &token);
2402 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002403 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002404
2405 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002406 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002407 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002408 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002409 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002410 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002411 free_arg(arg);
2412 arg = NULL;
2413
2414 *list = field;
2415 list = &field->next;
2416
2417 free_token(token);
2418 type = read_token_item(&token);
2419 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2420
2421 *tok = token;
2422 return type;
2423
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002424out_free_field:
2425 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002426out_free:
2427 free_arg(arg);
2428 free_token(token);
2429 *tok = NULL;
2430
2431 return EVENT_ERROR;
2432}
2433
2434static enum event_type
2435process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2436{
2437 struct print_arg *field;
2438 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002439 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002440
2441 memset(arg, 0, sizeof(*arg));
2442 arg->type = PRINT_FLAGS;
2443
2444 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002445 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002446 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002447 goto out_free;
2448 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002449
Steven Rostedteff2c922013-11-18 14:23:14 -05002450 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002451
2452 /* Handle operations in the first argument */
2453 while (type == EVENT_OP)
2454 type = process_op(event, field, &token);
2455
2456 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002457 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002458 free_token(token);
2459
2460 arg->flags.field = field;
2461
2462 type = read_token_item(&token);
2463 if (event_item_type(type)) {
2464 arg->flags.delim = token;
2465 type = read_token_item(&token);
2466 }
2467
2468 if (test_type_token(type, token, EVENT_DELIM, ","))
2469 goto out_free;
2470
2471 type = process_fields(event, &arg->flags.flags, &token);
2472 if (test_type_token(type, token, EVENT_DELIM, ")"))
2473 goto out_free;
2474
2475 free_token(token);
2476 type = read_token_item(tok);
2477 return type;
2478
Namhyung Kim70d93042012-09-19 15:58:44 +09002479out_free_field:
2480 free_arg(field);
2481out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002482 free_token(token);
2483 *tok = NULL;
2484 return EVENT_ERROR;
2485}
2486
2487static enum event_type
2488process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2489{
2490 struct print_arg *field;
2491 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002492 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002493
2494 memset(arg, 0, sizeof(*arg));
2495 arg->type = PRINT_SYMBOL;
2496
2497 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002498 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002499 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002500 goto out_free;
2501 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002502
Steven Rostedteff2c922013-11-18 14:23:14 -05002503 type = process_field_arg(event, field, &token);
2504
Steven Rostedtf7d82352012-04-06 00:47:53 +02002505 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002506 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002507
2508 arg->symbol.field = field;
2509
2510 type = process_fields(event, &arg->symbol.symbols, &token);
2511 if (test_type_token(type, token, EVENT_DELIM, ")"))
2512 goto out_free;
2513
2514 free_token(token);
2515 type = read_token_item(tok);
2516 return type;
2517
Namhyung Kim70d93042012-09-19 15:58:44 +09002518out_free_field:
2519 free_arg(field);
2520out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002521 free_token(token);
2522 *tok = NULL;
2523 return EVENT_ERROR;
2524}
2525
2526static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002527process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2528{
Namhyung Kime080e6f2012-06-27 09:41:41 +09002529 memset(arg, 0, sizeof(*arg));
2530 arg->type = PRINT_HEX;
2531
Javi Merino929a6bb2015-03-20 18:12:55 +00002532 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2533 goto out;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002534
Javi Merino929a6bb2015-03-20 18:12:55 +00002535 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2536 goto free_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002537
Javi Merino929a6bb2015-03-20 18:12:55 +00002538 return read_token_item(tok);
Namhyung Kime080e6f2012-06-27 09:41:41 +09002539
Javi Merino929a6bb2015-03-20 18:12:55 +00002540free_field:
2541 free_arg(arg->hex.field);
2542out:
Namhyung Kime080e6f2012-06-27 09:41:41 +09002543 *tok = NULL;
2544 return EVENT_ERROR;
2545}
2546
2547static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002548process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2549{
2550 struct format_field *field;
2551 enum event_type type;
2552 char *token;
2553
2554 memset(arg, 0, sizeof(*arg));
2555 arg->type = PRINT_DYNAMIC_ARRAY;
2556
2557 /*
2558 * The item within the parenthesis is another field that holds
2559 * the index into where the array starts.
2560 */
2561 type = read_token(&token);
2562 *tok = token;
2563 if (type != EVENT_ITEM)
2564 goto out_free;
2565
2566 /* Find the field */
2567
2568 field = pevent_find_field(event, token);
2569 if (!field)
2570 goto out_free;
2571
2572 arg->dynarray.field = field;
2573 arg->dynarray.index = 0;
2574
2575 if (read_expected(EVENT_DELIM, ")") < 0)
2576 goto out_free;
2577
2578 free_token(token);
2579 type = read_token_item(&token);
2580 *tok = token;
2581 if (type != EVENT_OP || strcmp(token, "[") != 0)
2582 return type;
2583
2584 free_token(token);
2585 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002586 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002587 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002588 *tok = NULL;
2589 return EVENT_ERROR;
2590 }
2591
Steven Rostedtf7d82352012-04-06 00:47:53 +02002592 type = process_arg(event, arg, &token);
2593 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002594 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002595
2596 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002597 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002598
2599 free_token(token);
2600 type = read_token_item(tok);
2601 return type;
2602
Namhyung Kimb3511d02012-05-23 11:36:50 +09002603 out_free_arg:
2604 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002605 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002606 free_token(token);
2607 *tok = NULL;
2608 return EVENT_ERROR;
2609}
2610
2611static enum event_type
2612process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2613{
2614 struct print_arg *item_arg;
2615 enum event_type type;
2616 char *token;
2617
2618 type = process_arg(event, arg, &token);
2619
2620 if (type == EVENT_ERROR)
2621 goto out_free;
2622
2623 if (type == EVENT_OP)
2624 type = process_op(event, arg, &token);
2625
2626 if (type == EVENT_ERROR)
2627 goto out_free;
2628
2629 if (test_type_token(type, token, EVENT_DELIM, ")"))
2630 goto out_free;
2631
2632 free_token(token);
2633 type = read_token_item(&token);
2634
2635 /*
2636 * If the next token is an item or another open paren, then
2637 * this was a typecast.
2638 */
2639 if (event_item_type(type) ||
2640 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2641
2642 /* make this a typecast and contine */
2643
2644 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002645 if (arg->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002646 do_warning_event(event, "previous needed to be PRINT_ATOM");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002647 goto out_free;
2648 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002649
2650 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002651 if (!item_arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002652 do_warning_event(event, "%s: not enough memory!",
2653 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002654 goto out_free;
2655 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002656
2657 arg->type = PRINT_TYPE;
2658 arg->typecast.type = arg->atom.atom;
2659 arg->typecast.item = item_arg;
2660 type = process_arg_token(event, item_arg, &token, type);
2661
2662 }
2663
2664 *tok = token;
2665 return type;
2666
2667 out_free:
2668 free_token(token);
2669 *tok = NULL;
2670 return EVENT_ERROR;
2671}
2672
2673
2674static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002675process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2676 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002677{
2678 enum event_type type;
2679 char *token;
2680
2681 if (read_expect_type(EVENT_ITEM, &token) < 0)
2682 goto out_free;
2683
2684 arg->type = PRINT_STRING;
2685 arg->string.string = token;
2686 arg->string.offset = -1;
2687
2688 if (read_expected(EVENT_DELIM, ")") < 0)
2689 goto out_err;
2690
2691 type = read_token(&token);
2692 *tok = token;
2693
2694 return type;
2695
2696 out_free:
2697 free_token(token);
2698 out_err:
2699 *tok = NULL;
2700 return EVENT_ERROR;
2701}
2702
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002703static enum event_type
2704process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2705 char **tok)
2706{
2707 enum event_type type;
2708 char *token;
2709
2710 if (read_expect_type(EVENT_ITEM, &token) < 0)
2711 goto out_free;
2712
2713 arg->type = PRINT_BITMASK;
2714 arg->bitmask.bitmask = token;
2715 arg->bitmask.offset = -1;
2716
2717 if (read_expected(EVENT_DELIM, ")") < 0)
2718 goto out_err;
2719
2720 type = read_token(&token);
2721 *tok = token;
2722
2723 return type;
2724
2725 out_free:
2726 free_token(token);
2727 out_err:
2728 *tok = NULL;
2729 return EVENT_ERROR;
2730}
2731
Steven Rostedtf7d82352012-04-06 00:47:53 +02002732static struct pevent_function_handler *
2733find_func_handler(struct pevent *pevent, char *func_name)
2734{
2735 struct pevent_function_handler *func;
2736
Steven Rostedt101782e2012-10-01 20:13:51 -04002737 if (!pevent)
2738 return NULL;
2739
Steven Rostedtf7d82352012-04-06 00:47:53 +02002740 for (func = pevent->func_handlers; func; func = func->next) {
2741 if (strcmp(func->name, func_name) == 0)
2742 break;
2743 }
2744
2745 return func;
2746}
2747
2748static void remove_func_handler(struct pevent *pevent, char *func_name)
2749{
2750 struct pevent_function_handler *func;
2751 struct pevent_function_handler **next;
2752
2753 next = &pevent->func_handlers;
2754 while ((func = *next)) {
2755 if (strcmp(func->name, func_name) == 0) {
2756 *next = func->next;
2757 free_func_handle(func);
2758 break;
2759 }
2760 next = &func->next;
2761 }
2762}
2763
2764static enum event_type
2765process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2766 struct print_arg *arg, char **tok)
2767{
2768 struct print_arg **next_arg;
2769 struct print_arg *farg;
2770 enum event_type type;
2771 char *token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002772 int i;
2773
2774 arg->type = PRINT_FUNC;
2775 arg->func.func = func;
2776
2777 *tok = NULL;
2778
2779 next_arg = &(arg->func.args);
2780 for (i = 0; i < func->nr_args; i++) {
2781 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002782 if (!farg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002783 do_warning_event(event, "%s: not enough memory!",
2784 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002785 return EVENT_ERROR;
2786 }
2787
Steven Rostedtf7d82352012-04-06 00:47:53 +02002788 type = process_arg(event, farg, &token);
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002789 if (i < (func->nr_args - 1)) {
2790 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002791 do_warning_event(event,
2792 "Error: function '%s()' expects %d arguments but event %s only uses %d",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002793 func->name, func->nr_args,
2794 event->name, i + 1);
2795 goto err;
2796 }
2797 } else {
2798 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002799 do_warning_event(event,
2800 "Error: function '%s()' only expects %d arguments but event %s has more",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002801 func->name, func->nr_args, event->name);
2802 goto err;
2803 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002804 }
2805
2806 *next_arg = farg;
2807 next_arg = &(farg->next);
2808 free_token(token);
2809 }
2810
2811 type = read_token(&token);
2812 *tok = token;
2813
2814 return type;
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002815
2816err:
2817 free_arg(farg);
2818 free_token(token);
2819 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002820}
2821
2822static enum event_type
2823process_function(struct event_format *event, struct print_arg *arg,
2824 char *token, char **tok)
2825{
2826 struct pevent_function_handler *func;
2827
2828 if (strcmp(token, "__print_flags") == 0) {
2829 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002830 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002831 return process_flags(event, arg, tok);
2832 }
2833 if (strcmp(token, "__print_symbolic") == 0) {
2834 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002835 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002836 return process_symbols(event, arg, tok);
2837 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002838 if (strcmp(token, "__print_hex") == 0) {
2839 free_token(token);
2840 return process_hex(event, arg, tok);
2841 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002842 if (strcmp(token, "__get_str") == 0) {
2843 free_token(token);
2844 return process_str(event, arg, tok);
2845 }
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002846 if (strcmp(token, "__get_bitmask") == 0) {
2847 free_token(token);
2848 return process_bitmask(event, arg, tok);
2849 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002850 if (strcmp(token, "__get_dynamic_array") == 0) {
2851 free_token(token);
2852 return process_dynamic_array(event, arg, tok);
2853 }
2854
2855 func = find_func_handler(event->pevent, token);
2856 if (func) {
2857 free_token(token);
2858 return process_func_handler(event, func, arg, tok);
2859 }
2860
Namhyung Kim3388cc32014-03-19 10:22:53 +09002861 do_warning_event(event, "function %s not defined", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002862 free_token(token);
2863 return EVENT_ERROR;
2864}
2865
2866static enum event_type
2867process_arg_token(struct event_format *event, struct print_arg *arg,
2868 char **tok, enum event_type type)
2869{
2870 char *token;
2871 char *atom;
2872
2873 token = *tok;
2874
2875 switch (type) {
2876 case EVENT_ITEM:
2877 if (strcmp(token, "REC") == 0) {
2878 free_token(token);
2879 type = process_entry(event, arg, &token);
2880 break;
2881 }
2882 atom = token;
2883 /* test the next token */
2884 type = read_token_item(&token);
2885
2886 /*
2887 * If the next token is a parenthesis, then this
2888 * is a function.
2889 */
2890 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2891 free_token(token);
2892 token = NULL;
2893 /* this will free atom. */
2894 type = process_function(event, arg, atom, &token);
2895 break;
2896 }
2897 /* atoms can be more than one token long */
2898 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002899 char *new_atom;
2900 new_atom = realloc(atom,
2901 strlen(atom) + strlen(token) + 2);
2902 if (!new_atom) {
2903 free(atom);
2904 *tok = NULL;
2905 free_token(token);
2906 return EVENT_ERROR;
2907 }
2908 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002909 strcat(atom, " ");
2910 strcat(atom, token);
2911 free_token(token);
2912 type = read_token_item(&token);
2913 }
2914
2915 arg->type = PRINT_ATOM;
2916 arg->atom.atom = atom;
2917 break;
2918
2919 case EVENT_DQUOTE:
2920 case EVENT_SQUOTE:
2921 arg->type = PRINT_ATOM;
2922 arg->atom.atom = token;
2923 type = read_token_item(&token);
2924 break;
2925 case EVENT_DELIM:
2926 if (strcmp(token, "(") == 0) {
2927 free_token(token);
2928 type = process_paren(event, arg, &token);
2929 break;
2930 }
2931 case EVENT_OP:
2932 /* handle single ops */
2933 arg->type = PRINT_OP;
2934 arg->op.op = token;
2935 arg->op.left = NULL;
2936 type = process_op(event, arg, &token);
2937
2938 /* On error, the op is freed */
2939 if (type == EVENT_ERROR)
2940 arg->op.op = NULL;
2941
2942 /* return error type if errored */
2943 break;
2944
2945 case EVENT_ERROR ... EVENT_NEWLINE:
2946 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09002947 do_warning_event(event, "unexpected type %d", type);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002948 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002949 }
2950 *tok = token;
2951
2952 return type;
2953}
2954
2955static int event_read_print_args(struct event_format *event, struct print_arg **list)
2956{
2957 enum event_type type = EVENT_ERROR;
2958 struct print_arg *arg;
2959 char *token;
2960 int args = 0;
2961
2962 do {
2963 if (type == EVENT_NEWLINE) {
2964 type = read_token_item(&token);
2965 continue;
2966 }
2967
2968 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002969 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002970 do_warning_event(event, "%s: not enough memory!",
2971 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002972 return -1;
2973 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002974
2975 type = process_arg(event, arg, &token);
2976
2977 if (type == EVENT_ERROR) {
2978 free_token(token);
2979 free_arg(arg);
2980 return -1;
2981 }
2982
2983 *list = arg;
2984 args++;
2985
2986 if (type == EVENT_OP) {
2987 type = process_op(event, arg, &token);
2988 free_token(token);
2989 if (type == EVENT_ERROR) {
2990 *list = NULL;
2991 free_arg(arg);
2992 return -1;
2993 }
2994 list = &arg->next;
2995 continue;
2996 }
2997
2998 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2999 free_token(token);
3000 *list = arg;
3001 list = &arg->next;
3002 continue;
3003 }
3004 break;
3005 } while (type != EVENT_NONE);
3006
3007 if (type != EVENT_NONE && type != EVENT_ERROR)
3008 free_token(token);
3009
3010 return args;
3011}
3012
3013static int event_read_print(struct event_format *event)
3014{
3015 enum event_type type;
3016 char *token;
3017 int ret;
3018
3019 if (read_expected_item(EVENT_ITEM, "print") < 0)
3020 return -1;
3021
3022 if (read_expected(EVENT_ITEM, "fmt") < 0)
3023 return -1;
3024
3025 if (read_expected(EVENT_OP, ":") < 0)
3026 return -1;
3027
3028 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3029 goto fail;
3030
3031 concat:
3032 event->print_fmt.format = token;
3033 event->print_fmt.args = NULL;
3034
3035 /* ok to have no arg */
3036 type = read_token_item(&token);
3037
3038 if (type == EVENT_NONE)
3039 return 0;
3040
3041 /* Handle concatenation of print lines */
3042 if (type == EVENT_DQUOTE) {
3043 char *cat;
3044
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003045 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3046 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003047 free_token(token);
3048 free_token(event->print_fmt.format);
3049 event->print_fmt.format = NULL;
3050 token = cat;
3051 goto concat;
3052 }
3053
3054 if (test_type_token(type, token, EVENT_DELIM, ","))
3055 goto fail;
3056
3057 free_token(token);
3058
3059 ret = event_read_print_args(event, &event->print_fmt.args);
3060 if (ret < 0)
3061 return -1;
3062
3063 return ret;
3064
3065 fail:
3066 free_token(token);
3067 return -1;
3068}
3069
3070/**
3071 * pevent_find_common_field - return a common field by event
3072 * @event: handle for the event
3073 * @name: the name of the common field to return
3074 *
3075 * Returns a common field from the event by the given @name.
3076 * This only searchs the common fields and not all field.
3077 */
3078struct format_field *
3079pevent_find_common_field(struct event_format *event, const char *name)
3080{
3081 struct format_field *format;
3082
3083 for (format = event->format.common_fields;
3084 format; format = format->next) {
3085 if (strcmp(format->name, name) == 0)
3086 break;
3087 }
3088
3089 return format;
3090}
3091
3092/**
3093 * pevent_find_field - find a non-common field
3094 * @event: handle for the event
3095 * @name: the name of the non-common field
3096 *
3097 * Returns a non-common field by the given @name.
3098 * This does not search common fields.
3099 */
3100struct format_field *
3101pevent_find_field(struct event_format *event, const char *name)
3102{
3103 struct format_field *format;
3104
3105 for (format = event->format.fields;
3106 format; format = format->next) {
3107 if (strcmp(format->name, name) == 0)
3108 break;
3109 }
3110
3111 return format;
3112}
3113
3114/**
3115 * pevent_find_any_field - find any field by name
3116 * @event: handle for the event
3117 * @name: the name of the field
3118 *
3119 * Returns a field by the given @name.
3120 * This searchs the common field names first, then
3121 * the non-common ones if a common one was not found.
3122 */
3123struct format_field *
3124pevent_find_any_field(struct event_format *event, const char *name)
3125{
3126 struct format_field *format;
3127
3128 format = pevent_find_common_field(event, name);
3129 if (format)
3130 return format;
3131 return pevent_find_field(event, name);
3132}
3133
3134/**
3135 * pevent_read_number - read a number from data
3136 * @pevent: handle for the pevent
3137 * @ptr: the raw data
3138 * @size: the size of the data that holds the number
3139 *
3140 * Returns the number (converted to host) from the
3141 * raw data.
3142 */
3143unsigned long long pevent_read_number(struct pevent *pevent,
3144 const void *ptr, int size)
3145{
3146 switch (size) {
3147 case 1:
3148 return *(unsigned char *)ptr;
3149 case 2:
3150 return data2host2(pevent, ptr);
3151 case 4:
3152 return data2host4(pevent, ptr);
3153 case 8:
3154 return data2host8(pevent, ptr);
3155 default:
3156 /* BUG! */
3157 return 0;
3158 }
3159}
3160
3161/**
3162 * pevent_read_number_field - read a number from data
3163 * @field: a handle to the field
3164 * @data: the raw data to read
3165 * @value: the value to place the number in
3166 *
3167 * Reads raw data according to a field offset and size,
3168 * and translates it into @value.
3169 *
3170 * Returns 0 on success, -1 otherwise.
3171 */
3172int pevent_read_number_field(struct format_field *field, const void *data,
3173 unsigned long long *value)
3174{
3175 if (!field)
3176 return -1;
3177 switch (field->size) {
3178 case 1:
3179 case 2:
3180 case 4:
3181 case 8:
3182 *value = pevent_read_number(field->event->pevent,
3183 data + field->offset, field->size);
3184 return 0;
3185 default:
3186 return -1;
3187 }
3188}
3189
3190static int get_common_info(struct pevent *pevent,
3191 const char *type, int *offset, int *size)
3192{
3193 struct event_format *event;
3194 struct format_field *field;
3195
3196 /*
3197 * All events should have the same common elements.
3198 * Pick any event to find where the type is;
3199 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003200 if (!pevent->events) {
3201 do_warning("no event_list!");
3202 return -1;
3203 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003204
3205 event = pevent->events[0];
3206 field = pevent_find_common_field(event, type);
3207 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003208 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003209
3210 *offset = field->offset;
3211 *size = field->size;
3212
3213 return 0;
3214}
3215
3216static int __parse_common(struct pevent *pevent, void *data,
3217 int *size, int *offset, const char *name)
3218{
3219 int ret;
3220
3221 if (!*size) {
3222 ret = get_common_info(pevent, name, offset, size);
3223 if (ret < 0)
3224 return ret;
3225 }
3226 return pevent_read_number(pevent, data + *offset, *size);
3227}
3228
3229static int trace_parse_common_type(struct pevent *pevent, void *data)
3230{
3231 return __parse_common(pevent, data,
3232 &pevent->type_size, &pevent->type_offset,
3233 "common_type");
3234}
3235
3236static int parse_common_pid(struct pevent *pevent, void *data)
3237{
3238 return __parse_common(pevent, data,
3239 &pevent->pid_size, &pevent->pid_offset,
3240 "common_pid");
3241}
3242
3243static int parse_common_pc(struct pevent *pevent, void *data)
3244{
3245 return __parse_common(pevent, data,
3246 &pevent->pc_size, &pevent->pc_offset,
3247 "common_preempt_count");
3248}
3249
3250static int parse_common_flags(struct pevent *pevent, void *data)
3251{
3252 return __parse_common(pevent, data,
3253 &pevent->flags_size, &pevent->flags_offset,
3254 "common_flags");
3255}
3256
3257static int parse_common_lock_depth(struct pevent *pevent, void *data)
3258{
Steven Rostedt0866a972012-05-22 14:52:40 +09003259 return __parse_common(pevent, data,
3260 &pevent->ld_size, &pevent->ld_offset,
3261 "common_lock_depth");
3262}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003263
Steven Rostedt0866a972012-05-22 14:52:40 +09003264static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3265{
3266 return __parse_common(pevent, data,
3267 &pevent->ld_size, &pevent->ld_offset,
3268 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003269}
3270
3271static int events_id_cmp(const void *a, const void *b);
3272
3273/**
3274 * pevent_find_event - find an event by given id
3275 * @pevent: a handle to the pevent
3276 * @id: the id of the event
3277 *
3278 * Returns an event that has a given @id.
3279 */
3280struct event_format *pevent_find_event(struct pevent *pevent, int id)
3281{
3282 struct event_format **eventptr;
3283 struct event_format key;
3284 struct event_format *pkey = &key;
3285
3286 /* Check cache first */
3287 if (pevent->last_event && pevent->last_event->id == id)
3288 return pevent->last_event;
3289
3290 key.id = id;
3291
3292 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3293 sizeof(*pevent->events), events_id_cmp);
3294
3295 if (eventptr) {
3296 pevent->last_event = *eventptr;
3297 return *eventptr;
3298 }
3299
3300 return NULL;
3301}
3302
3303/**
3304 * pevent_find_event_by_name - find an event by given name
3305 * @pevent: a handle to the pevent
3306 * @sys: the system name to search for
3307 * @name: the name of the event to search for
3308 *
3309 * This returns an event with a given @name and under the system
3310 * @sys. If @sys is NULL the first event with @name is returned.
3311 */
3312struct event_format *
3313pevent_find_event_by_name(struct pevent *pevent,
3314 const char *sys, const char *name)
3315{
3316 struct event_format *event;
3317 int i;
3318
3319 if (pevent->last_event &&
3320 strcmp(pevent->last_event->name, name) == 0 &&
3321 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3322 return pevent->last_event;
3323
3324 for (i = 0; i < pevent->nr_events; i++) {
3325 event = pevent->events[i];
3326 if (strcmp(event->name, name) == 0) {
3327 if (!sys)
3328 break;
3329 if (strcmp(event->system, sys) == 0)
3330 break;
3331 }
3332 }
3333 if (i == pevent->nr_events)
3334 event = NULL;
3335
3336 pevent->last_event = event;
3337 return event;
3338}
3339
3340static unsigned long long
3341eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3342{
3343 struct pevent *pevent = event->pevent;
3344 unsigned long long val = 0;
3345 unsigned long long left, right;
3346 struct print_arg *typearg = NULL;
3347 struct print_arg *larg;
3348 unsigned long offset;
3349 unsigned int field_size;
3350
3351 switch (arg->type) {
3352 case PRINT_NULL:
3353 /* ?? */
3354 return 0;
3355 case PRINT_ATOM:
3356 return strtoull(arg->atom.atom, NULL, 0);
3357 case PRINT_FIELD:
3358 if (!arg->field.field) {
3359 arg->field.field = pevent_find_any_field(event, arg->field.name);
3360 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003361 goto out_warning_field;
3362
Steven Rostedtf7d82352012-04-06 00:47:53 +02003363 }
3364 /* must be a number */
3365 val = pevent_read_number(pevent, data + arg->field.field->offset,
3366 arg->field.field->size);
3367 break;
3368 case PRINT_FLAGS:
3369 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003370 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003371 break;
3372 case PRINT_TYPE:
3373 val = eval_num_arg(data, size, event, arg->typecast.item);
3374 return eval_type(val, arg, 0);
3375 case PRINT_STRING:
3376 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003377 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003378 return 0;
3379 case PRINT_FUNC: {
3380 struct trace_seq s;
3381 trace_seq_init(&s);
3382 val = process_defined_func(&s, data, size, event, arg);
3383 trace_seq_destroy(&s);
3384 return val;
3385 }
3386 case PRINT_OP:
3387 if (strcmp(arg->op.op, "[") == 0) {
3388 /*
3389 * Arrays are special, since we don't want
3390 * to read the arg as is.
3391 */
3392 right = eval_num_arg(data, size, event, arg->op.right);
3393
3394 /* handle typecasts */
3395 larg = arg->op.left;
3396 while (larg->type == PRINT_TYPE) {
3397 if (!typearg)
3398 typearg = larg;
3399 larg = larg->typecast.item;
3400 }
3401
3402 /* Default to long size */
3403 field_size = pevent->long_size;
3404
3405 switch (larg->type) {
3406 case PRINT_DYNAMIC_ARRAY:
3407 offset = pevent_read_number(pevent,
3408 data + larg->dynarray.field->offset,
3409 larg->dynarray.field->size);
3410 if (larg->dynarray.field->elementsize)
3411 field_size = larg->dynarray.field->elementsize;
3412 /*
3413 * The actual length of the dynamic array is stored
3414 * in the top half of the field, and the offset
3415 * is in the bottom half of the 32 bit field.
3416 */
3417 offset &= 0xffff;
3418 offset += right;
3419 break;
3420 case PRINT_FIELD:
3421 if (!larg->field.field) {
3422 larg->field.field =
3423 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003424 if (!larg->field.field) {
3425 arg = larg;
3426 goto out_warning_field;
3427 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003428 }
3429 field_size = larg->field.field->elementsize;
3430 offset = larg->field.field->offset +
3431 right * larg->field.field->elementsize;
3432 break;
3433 default:
3434 goto default_op; /* oops, all bets off */
3435 }
3436 val = pevent_read_number(pevent,
3437 data + offset, field_size);
3438 if (typearg)
3439 val = eval_type(val, typearg, 1);
3440 break;
3441 } else if (strcmp(arg->op.op, "?") == 0) {
3442 left = eval_num_arg(data, size, event, arg->op.left);
3443 arg = arg->op.right;
3444 if (left)
3445 val = eval_num_arg(data, size, event, arg->op.left);
3446 else
3447 val = eval_num_arg(data, size, event, arg->op.right);
3448 break;
3449 }
3450 default_op:
3451 left = eval_num_arg(data, size, event, arg->op.left);
3452 right = eval_num_arg(data, size, event, arg->op.right);
3453 switch (arg->op.op[0]) {
3454 case '!':
3455 switch (arg->op.op[1]) {
3456 case 0:
3457 val = !right;
3458 break;
3459 case '=':
3460 val = left != right;
3461 break;
3462 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003463 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003464 }
3465 break;
3466 case '~':
3467 val = ~right;
3468 break;
3469 case '|':
3470 if (arg->op.op[1])
3471 val = left || right;
3472 else
3473 val = left | right;
3474 break;
3475 case '&':
3476 if (arg->op.op[1])
3477 val = left && right;
3478 else
3479 val = left & right;
3480 break;
3481 case '<':
3482 switch (arg->op.op[1]) {
3483 case 0:
3484 val = left < right;
3485 break;
3486 case '<':
3487 val = left << right;
3488 break;
3489 case '=':
3490 val = left <= right;
3491 break;
3492 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003493 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003494 }
3495 break;
3496 case '>':
3497 switch (arg->op.op[1]) {
3498 case 0:
3499 val = left > right;
3500 break;
3501 case '>':
3502 val = left >> right;
3503 break;
3504 case '=':
3505 val = left >= right;
3506 break;
3507 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003508 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003509 }
3510 break;
3511 case '=':
3512 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003513 goto out_warning_op;
3514
Steven Rostedtf7d82352012-04-06 00:47:53 +02003515 val = left == right;
3516 break;
3517 case '-':
3518 val = left - right;
3519 break;
3520 case '+':
3521 val = left + right;
3522 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003523 case '/':
3524 val = left / right;
3525 break;
3526 case '*':
3527 val = left * right;
3528 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003529 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003530 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003531 }
3532 break;
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003533 case PRINT_DYNAMIC_ARRAY:
3534 /* Without [], we pass the address to the dynamic data */
3535 offset = pevent_read_number(pevent,
3536 data + arg->dynarray.field->offset,
3537 arg->dynarray.field->size);
3538 /*
3539 * The actual length of the dynamic array is stored
3540 * in the top half of the field, and the offset
3541 * is in the bottom half of the 32 bit field.
3542 */
3543 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003544 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003545 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003546 default: /* not sure what to do there */
3547 return 0;
3548 }
3549 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003550
3551out_warning_op:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003552 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003553 return 0;
3554
3555out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003556 do_warning_event(event, "%s: field %s not found",
3557 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003558 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003559}
3560
3561struct flag {
3562 const char *name;
3563 unsigned long long value;
3564};
3565
3566static const struct flag flags[] = {
3567 { "HI_SOFTIRQ", 0 },
3568 { "TIMER_SOFTIRQ", 1 },
3569 { "NET_TX_SOFTIRQ", 2 },
3570 { "NET_RX_SOFTIRQ", 3 },
3571 { "BLOCK_SOFTIRQ", 4 },
3572 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3573 { "TASKLET_SOFTIRQ", 6 },
3574 { "SCHED_SOFTIRQ", 7 },
3575 { "HRTIMER_SOFTIRQ", 8 },
3576 { "RCU_SOFTIRQ", 9 },
3577
3578 { "HRTIMER_NORESTART", 0 },
3579 { "HRTIMER_RESTART", 1 },
3580};
3581
3582static unsigned long long eval_flag(const char *flag)
3583{
3584 int i;
3585
3586 /*
3587 * Some flags in the format files do not get converted.
3588 * If the flag is not numeric, see if it is something that
3589 * we already know about.
3590 */
3591 if (isdigit(flag[0]))
3592 return strtoull(flag, NULL, 0);
3593
3594 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3595 if (strcmp(flags[i].name, flag) == 0)
3596 return flags[i].value;
3597
3598 return 0;
3599}
3600
3601static void print_str_to_seq(struct trace_seq *s, const char *format,
3602 int len_arg, const char *str)
3603{
3604 if (len_arg >= 0)
3605 trace_seq_printf(s, format, len_arg, str);
3606 else
3607 trace_seq_printf(s, format, str);
3608}
3609
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003610static void print_bitmask_to_seq(struct pevent *pevent,
3611 struct trace_seq *s, const char *format,
3612 int len_arg, const void *data, int size)
3613{
3614 int nr_bits = size * 8;
3615 int str_size = (nr_bits + 3) / 4;
3616 int len = 0;
3617 char buf[3];
3618 char *str;
3619 int index;
3620 int i;
3621
3622 /*
3623 * The kernel likes to put in commas every 32 bits, we
3624 * can do the same.
3625 */
3626 str_size += (nr_bits - 1) / 32;
3627
3628 str = malloc(str_size + 1);
3629 if (!str) {
3630 do_warning("%s: not enough memory!", __func__);
3631 return;
3632 }
3633 str[str_size] = 0;
3634
3635 /* Start out with -2 for the two chars per byte */
3636 for (i = str_size - 2; i >= 0; i -= 2) {
3637 /*
3638 * data points to a bit mask of size bytes.
3639 * In the kernel, this is an array of long words, thus
3640 * endianess is very important.
3641 */
3642 if (pevent->file_bigendian)
3643 index = size - (len + 1);
3644 else
3645 index = len;
3646
3647 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3648 memcpy(str + i, buf, 2);
3649 len++;
3650 if (!(len & 3) && i > 0) {
3651 i--;
3652 str[i] = ',';
3653 }
3654 }
3655
3656 if (len_arg >= 0)
3657 trace_seq_printf(s, format, len_arg, str);
3658 else
3659 trace_seq_printf(s, format, str);
3660
3661 free(str);
3662}
3663
Steven Rostedtf7d82352012-04-06 00:47:53 +02003664static void print_str_arg(struct trace_seq *s, void *data, int size,
3665 struct event_format *event, const char *format,
3666 int len_arg, struct print_arg *arg)
3667{
3668 struct pevent *pevent = event->pevent;
3669 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003670 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003671 struct printk_map *printk;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003672 unsigned long long val, fval;
3673 unsigned long addr;
3674 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003675 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003676 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003677 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003678
3679 switch (arg->type) {
3680 case PRINT_NULL:
3681 /* ?? */
3682 return;
3683 case PRINT_ATOM:
3684 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3685 return;
3686 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003687 field = arg->field.field;
3688 if (!field) {
3689 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003690 if (!field) {
3691 str = arg->field.name;
3692 goto out_warning_field;
3693 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003694 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003695 }
3696 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003697 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003698
3699 /*
3700 * Some events pass in pointers. If this is not an array
3701 * and the size is the same as long_size, assume that it
3702 * is a pointer.
3703 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003704 if (!(field->flags & FIELD_IS_ARRAY) &&
3705 field->size == pevent->long_size) {
3706 addr = *(unsigned long *)(data + field->offset);
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003707 /* Check if it matches a print format */
3708 printk = find_printk(pevent, addr);
3709 if (printk)
3710 trace_seq_puts(s, printk->printk);
3711 else
3712 trace_seq_printf(s, "%lx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003713 break;
3714 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003715 str = malloc(len + 1);
3716 if (!str) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003717 do_warning_event(event, "%s: not enough memory!",
3718 __func__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003719 return;
3720 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003721 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003722 str[len] = 0;
3723 print_str_to_seq(s, format, len_arg, str);
3724 free(str);
3725 break;
3726 case PRINT_FLAGS:
3727 val = eval_num_arg(data, size, event, arg->flags.field);
3728 print = 0;
3729 for (flag = arg->flags.flags; flag; flag = flag->next) {
3730 fval = eval_flag(flag->value);
3731 if (!val && !fval) {
3732 print_str_to_seq(s, format, len_arg, flag->str);
3733 break;
3734 }
3735 if (fval && (val & fval) == fval) {
3736 if (print && arg->flags.delim)
3737 trace_seq_puts(s, arg->flags.delim);
3738 print_str_to_seq(s, format, len_arg, flag->str);
3739 print = 1;
3740 val &= ~fval;
3741 }
3742 }
3743 break;
3744 case PRINT_SYMBOL:
3745 val = eval_num_arg(data, size, event, arg->symbol.field);
3746 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3747 fval = eval_flag(flag->value);
3748 if (val == fval) {
3749 print_str_to_seq(s, format, len_arg, flag->str);
3750 break;
3751 }
3752 }
3753 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003754 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003755 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3756 unsigned long offset;
3757 offset = pevent_read_number(pevent,
3758 data + arg->hex.field->dynarray.field->offset,
3759 arg->hex.field->dynarray.field->size);
3760 hex = data + (offset & 0xffff);
3761 } else {
3762 field = arg->hex.field->field.field;
3763 if (!field) {
3764 str = arg->hex.field->field.name;
3765 field = pevent_find_any_field(event, str);
3766 if (!field)
3767 goto out_warning_field;
3768 arg->hex.field->field.field = field;
3769 }
3770 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003771 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003772 len = eval_num_arg(data, size, event, arg->hex.size);
3773 for (i = 0; i < len; i++) {
3774 if (i)
3775 trace_seq_putc(s, ' ');
3776 trace_seq_printf(s, "%02x", hex[i]);
3777 }
3778 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003779
3780 case PRINT_TYPE:
3781 break;
3782 case PRINT_STRING: {
3783 int str_offset;
3784
3785 if (arg->string.offset == -1) {
3786 struct format_field *f;
3787
3788 f = pevent_find_any_field(event, arg->string.string);
3789 arg->string.offset = f->offset;
3790 }
3791 str_offset = data2host4(pevent, data + arg->string.offset);
3792 str_offset &= 0xffff;
3793 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3794 break;
3795 }
3796 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003797 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003798 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003799 case PRINT_BITMASK: {
3800 int bitmask_offset;
3801 int bitmask_size;
3802
3803 if (arg->bitmask.offset == -1) {
3804 struct format_field *f;
3805
3806 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3807 arg->bitmask.offset = f->offset;
3808 }
3809 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3810 bitmask_size = bitmask_offset >> 16;
3811 bitmask_offset &= 0xffff;
3812 print_bitmask_to_seq(pevent, s, format, len_arg,
3813 data + bitmask_offset, bitmask_size);
3814 break;
3815 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003816 case PRINT_OP:
3817 /*
3818 * The only op for string should be ? :
3819 */
3820 if (arg->op.op[0] != '?')
3821 return;
3822 val = eval_num_arg(data, size, event, arg->op.left);
3823 if (val)
3824 print_str_arg(s, data, size, event,
3825 format, len_arg, arg->op.right->op.left);
3826 else
3827 print_str_arg(s, data, size, event,
3828 format, len_arg, arg->op.right->op.right);
3829 break;
3830 case PRINT_FUNC:
3831 process_defined_func(s, data, size, event, arg);
3832 break;
3833 default:
3834 /* well... */
3835 break;
3836 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003837
3838 return;
3839
3840out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003841 do_warning_event(event, "%s: field %s not found",
3842 __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003843}
3844
3845static unsigned long long
3846process_defined_func(struct trace_seq *s, void *data, int size,
3847 struct event_format *event, struct print_arg *arg)
3848{
3849 struct pevent_function_handler *func_handle = arg->func.func;
3850 struct pevent_func_params *param;
3851 unsigned long long *args;
3852 unsigned long long ret;
3853 struct print_arg *farg;
3854 struct trace_seq str;
3855 struct save_str {
3856 struct save_str *next;
3857 char *str;
3858 } *strings = NULL, *string;
3859 int i;
3860
3861 if (!func_handle->nr_args) {
3862 ret = (*func_handle->func)(s, NULL);
3863 goto out;
3864 }
3865
3866 farg = arg->func.args;
3867 param = func_handle->params;
3868
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003869 ret = ULLONG_MAX;
3870 args = malloc(sizeof(*args) * func_handle->nr_args);
3871 if (!args)
3872 goto out;
3873
Steven Rostedtf7d82352012-04-06 00:47:53 +02003874 for (i = 0; i < func_handle->nr_args; i++) {
3875 switch (param->type) {
3876 case PEVENT_FUNC_ARG_INT:
3877 case PEVENT_FUNC_ARG_LONG:
3878 case PEVENT_FUNC_ARG_PTR:
3879 args[i] = eval_num_arg(data, size, event, farg);
3880 break;
3881 case PEVENT_FUNC_ARG_STRING:
3882 trace_seq_init(&str);
3883 print_str_arg(&str, data, size, event, "%s", -1, farg);
3884 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003885 string = malloc(sizeof(*string));
3886 if (!string) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003887 do_warning_event(event, "%s(%d): malloc str",
3888 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003889 goto out_free;
3890 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003891 string->next = strings;
3892 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003893 if (!string->str) {
3894 free(string);
Namhyung Kim3388cc32014-03-19 10:22:53 +09003895 do_warning_event(event, "%s(%d): malloc str",
3896 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003897 goto out_free;
3898 }
Robert Richter0cf26012012-08-07 19:43:14 +02003899 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003900 strings = string;
3901 trace_seq_destroy(&str);
3902 break;
3903 default:
3904 /*
3905 * Something went totally wrong, this is not
3906 * an input error, something in this code broke.
3907 */
Namhyung Kim3388cc32014-03-19 10:22:53 +09003908 do_warning_event(event, "Unexpected end of arguments\n");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003909 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003910 }
3911 farg = farg->next;
Namhyung Kim21c69e722012-05-23 11:36:51 +09003912 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003913 }
3914
3915 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003916out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003917 free(args);
3918 while (strings) {
3919 string = strings;
3920 strings = string->next;
3921 free(string->str);
3922 free(string);
3923 }
3924
3925 out:
3926 /* TBD : handle return type here */
3927 return ret;
3928}
3929
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003930static void free_args(struct print_arg *args)
3931{
3932 struct print_arg *next;
3933
3934 while (args) {
3935 next = args->next;
3936
3937 free_arg(args);
3938 args = next;
3939 }
3940}
3941
Steven Rostedtf7d82352012-04-06 00:47:53 +02003942static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3943{
3944 struct pevent *pevent = event->pevent;
3945 struct format_field *field, *ip_field;
3946 struct print_arg *args, *arg, **next;
3947 unsigned long long ip, val;
3948 char *ptr;
3949 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003950 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003951
3952 field = pevent->bprint_buf_field;
3953 ip_field = pevent->bprint_ip_field;
3954
3955 if (!field) {
3956 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003957 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003958 do_warning_event(event, "can't find buffer field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003959 return NULL;
3960 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003961 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003962 if (!ip_field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003963 do_warning_event(event, "can't find ip field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003964 return NULL;
3965 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003966 pevent->bprint_buf_field = field;
3967 pevent->bprint_ip_field = ip_field;
3968 }
3969
3970 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3971
3972 /*
3973 * The first arg is the IP pointer.
3974 */
3975 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003976 if (!args) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003977 do_warning_event(event, "%s(%d): not enough memory!",
3978 __func__, __LINE__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003979 return NULL;
3980 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003981 arg = args;
3982 arg->next = NULL;
3983 next = &arg->next;
3984
3985 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003986
3987 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3988 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003989
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003990 /* skip the first "%pf: " */
3991 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003992 bptr < data + size && *ptr; ptr++) {
3993 int ls = 0;
3994
3995 if (*ptr == '%') {
3996 process_again:
3997 ptr++;
3998 switch (*ptr) {
3999 case '%':
4000 break;
4001 case 'l':
4002 ls++;
4003 goto process_again;
4004 case 'L':
4005 ls = 2;
4006 goto process_again;
4007 case '0' ... '9':
4008 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004009 case '.':
4010 goto process_again;
Steven Rostedt (Red Hat)55426292015-03-24 09:57:51 -04004011 case 'z':
4012 case 'Z':
4013 ls = 1;
4014 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004015 case 'p':
4016 ls = 1;
4017 /* fall through */
4018 case 'd':
4019 case 'u':
4020 case 'x':
4021 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004022 switch (ls) {
4023 case 0:
4024 vsize = 4;
4025 break;
4026 case 1:
4027 vsize = pevent->long_size;
4028 break;
4029 case 2:
4030 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02004031 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004032 default:
4033 vsize = ls; /* ? */
4034 break;
4035 }
4036 /* fall through */
4037 case '*':
4038 if (*ptr == '*')
4039 vsize = 4;
4040
Steven Rostedtf7d82352012-04-06 00:47:53 +02004041 /* the pointers are always 4 bytes aligned */
4042 bptr = (void *)(((unsigned long)bptr + 3) &
4043 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004044 val = pevent_read_number(pevent, bptr, vsize);
4045 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004046 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004047 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004048 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004049 __func__, __LINE__);
4050 goto out_free;
4051 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004052 arg->next = NULL;
4053 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004054 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4055 free(arg);
4056 goto out_free;
4057 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004058 *next = arg;
4059 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004060 /*
4061 * The '*' case means that an arg is used as the length.
4062 * We need to continue to figure out for what.
4063 */
4064 if (*ptr == '*')
4065 goto process_again;
4066
Steven Rostedtf7d82352012-04-06 00:47:53 +02004067 break;
4068 case 's':
4069 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004070 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004071 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004072 __func__, __LINE__);
4073 goto out_free;
4074 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004075 arg->next = NULL;
4076 arg->type = PRINT_BSTRING;
4077 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09004078 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004079 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004080 bptr += strlen(bptr) + 1;
4081 *next = arg;
4082 next = &arg->next;
4083 default:
4084 break;
4085 }
4086 }
4087 }
4088
4089 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004090
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004091out_free:
4092 free_args(args);
4093 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004094}
4095
4096static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004097get_bprint_format(void *data, int size __maybe_unused,
4098 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004099{
4100 struct pevent *pevent = event->pevent;
4101 unsigned long long addr;
4102 struct format_field *field;
4103 struct printk_map *printk;
4104 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004105
4106 field = pevent->bprint_fmt_field;
4107
4108 if (!field) {
4109 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004110 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004111 do_warning_event(event, "can't find format field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004112 return NULL;
4113 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004114 pevent->bprint_fmt_field = field;
4115 }
4116
4117 addr = pevent_read_number(pevent, data + field->offset, field->size);
4118
4119 printk = find_printk(pevent, addr);
4120 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004121 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004122 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004123 return format;
4124 }
4125
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004126 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004127 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004128
4129 return format;
4130}
4131
4132static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4133 struct event_format *event, struct print_arg *arg)
4134{
4135 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004136 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02004137
4138 if (arg->type == PRINT_FUNC) {
4139 process_defined_func(s, data, size, event, arg);
4140 return;
4141 }
4142
4143 if (arg->type != PRINT_FIELD) {
4144 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4145 arg->type);
4146 return;
4147 }
4148
4149 if (mac == 'm')
4150 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4151 if (!arg->field.field) {
4152 arg->field.field =
4153 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004154 if (!arg->field.field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004155 do_warning_event(event, "%s: field %s not found",
4156 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004157 return;
4158 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004159 }
4160 if (arg->field.field->size != 6) {
4161 trace_seq_printf(s, "INVALIDMAC");
4162 return;
4163 }
4164 buf = data + arg->field.field->offset;
4165 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4166}
4167
David Ahern3d199b52014-12-18 19:11:11 -07004168static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4169{
4170 const char *fmt;
4171
4172 if (i == 'i')
4173 fmt = "%03d.%03d.%03d.%03d";
4174 else
4175 fmt = "%d.%d.%d.%d";
4176
4177 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4178}
4179
4180static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4181{
4182 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4183 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4184}
4185
4186static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4187{
4188 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4189}
4190
4191static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4192{
4193 int i, j, range;
4194 unsigned char zerolength[8];
4195 int longest = 1;
4196 int colonpos = -1;
4197 uint16_t word;
4198 uint8_t hi, lo;
4199 bool needcolon = false;
4200 bool useIPv4;
4201 struct in6_addr in6;
4202
4203 memcpy(&in6, addr, sizeof(struct in6_addr));
4204
4205 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4206
4207 memset(zerolength, 0, sizeof(zerolength));
4208
4209 if (useIPv4)
4210 range = 6;
4211 else
4212 range = 8;
4213
4214 /* find position of longest 0 run */
4215 for (i = 0; i < range; i++) {
4216 for (j = i; j < range; j++) {
4217 if (in6.s6_addr16[j] != 0)
4218 break;
4219 zerolength[i]++;
4220 }
4221 }
4222 for (i = 0; i < range; i++) {
4223 if (zerolength[i] > longest) {
4224 longest = zerolength[i];
4225 colonpos = i;
4226 }
4227 }
4228 if (longest == 1) /* don't compress a single 0 */
4229 colonpos = -1;
4230
4231 /* emit address */
4232 for (i = 0; i < range; i++) {
4233 if (i == colonpos) {
4234 if (needcolon || i == 0)
4235 trace_seq_printf(s, ":");
4236 trace_seq_printf(s, ":");
4237 needcolon = false;
4238 i += longest - 1;
4239 continue;
4240 }
4241 if (needcolon) {
4242 trace_seq_printf(s, ":");
4243 needcolon = false;
4244 }
4245 /* hex u16 without leading 0s */
4246 word = ntohs(in6.s6_addr16[i]);
4247 hi = word >> 8;
4248 lo = word & 0xff;
4249 if (hi)
4250 trace_seq_printf(s, "%x%02x", hi, lo);
4251 else
4252 trace_seq_printf(s, "%x", lo);
4253
4254 needcolon = true;
4255 }
4256
4257 if (useIPv4) {
4258 if (needcolon)
4259 trace_seq_printf(s, ":");
4260 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4261 }
4262
4263 return;
4264}
4265
4266static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4267{
4268 int j;
4269
4270 for (j = 0; j < 16; j += 2) {
4271 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4272 if (i == 'I' && j < 14)
4273 trace_seq_printf(s, ":");
4274 }
4275}
4276
4277/*
4278 * %pi4 print an IPv4 address with leading zeros
4279 * %pI4 print an IPv4 address without leading zeros
4280 * %pi6 print an IPv6 address without colons
4281 * %pI6 print an IPv6 address with colons
4282 * %pI6c print an IPv6 address in compressed form with colons
4283 * %pISpc print an IP address based on sockaddr; p adds port.
4284 */
4285static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4286 void *data, int size, struct event_format *event,
4287 struct print_arg *arg)
4288{
4289 unsigned char *buf;
4290
4291 if (arg->type == PRINT_FUNC) {
4292 process_defined_func(s, data, size, event, arg);
4293 return 0;
4294 }
4295
4296 if (arg->type != PRINT_FIELD) {
4297 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4298 return 0;
4299 }
4300
4301 if (!arg->field.field) {
4302 arg->field.field =
4303 pevent_find_any_field(event, arg->field.name);
4304 if (!arg->field.field) {
4305 do_warning("%s: field %s not found",
4306 __func__, arg->field.name);
4307 return 0;
4308 }
4309 }
4310
4311 buf = data + arg->field.field->offset;
4312
4313 if (arg->field.field->size != 4) {
4314 trace_seq_printf(s, "INVALIDIPv4");
4315 return 0;
4316 }
4317 print_ip4_addr(s, i, buf);
4318
4319 return 0;
4320}
4321
4322static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4323 void *data, int size, struct event_format *event,
4324 struct print_arg *arg)
4325{
4326 char have_c = 0;
4327 unsigned char *buf;
4328 int rc = 0;
4329
4330 /* pI6c */
4331 if (i == 'I' && *ptr == 'c') {
4332 have_c = 1;
4333 ptr++;
4334 rc++;
4335 }
4336
4337 if (arg->type == PRINT_FUNC) {
4338 process_defined_func(s, data, size, event, arg);
4339 return rc;
4340 }
4341
4342 if (arg->type != PRINT_FIELD) {
4343 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4344 return rc;
4345 }
4346
4347 if (!arg->field.field) {
4348 arg->field.field =
4349 pevent_find_any_field(event, arg->field.name);
4350 if (!arg->field.field) {
4351 do_warning("%s: field %s not found",
4352 __func__, arg->field.name);
4353 return rc;
4354 }
4355 }
4356
4357 buf = data + arg->field.field->offset;
4358
4359 if (arg->field.field->size != 16) {
4360 trace_seq_printf(s, "INVALIDIPv6");
4361 return rc;
4362 }
4363
4364 if (have_c)
4365 print_ip6c_addr(s, buf);
4366 else
4367 print_ip6_addr(s, i, buf);
4368
4369 return rc;
4370}
4371
4372static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4373 void *data, int size, struct event_format *event,
4374 struct print_arg *arg)
4375{
4376 char have_c = 0, have_p = 0;
4377 unsigned char *buf;
4378 struct sockaddr_storage *sa;
4379 int rc = 0;
4380
4381 /* pISpc */
4382 if (i == 'I') {
4383 if (*ptr == 'p') {
4384 have_p = 1;
4385 ptr++;
4386 rc++;
4387 }
4388 if (*ptr == 'c') {
4389 have_c = 1;
4390 ptr++;
4391 rc++;
4392 }
4393 }
4394
4395 if (arg->type == PRINT_FUNC) {
4396 process_defined_func(s, data, size, event, arg);
4397 return rc;
4398 }
4399
4400 if (arg->type != PRINT_FIELD) {
4401 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4402 return rc;
4403 }
4404
4405 if (!arg->field.field) {
4406 arg->field.field =
4407 pevent_find_any_field(event, arg->field.name);
4408 if (!arg->field.field) {
4409 do_warning("%s: field %s not found",
4410 __func__, arg->field.name);
4411 return rc;
4412 }
4413 }
4414
4415 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4416
4417 if (sa->ss_family == AF_INET) {
4418 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4419
4420 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4421 trace_seq_printf(s, "INVALIDIPv4");
4422 return rc;
4423 }
4424
4425 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4426 if (have_p)
4427 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4428
4429
4430 } else if (sa->ss_family == AF_INET6) {
4431 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4432
4433 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4434 trace_seq_printf(s, "INVALIDIPv6");
4435 return rc;
4436 }
4437
4438 if (have_p)
4439 trace_seq_printf(s, "[");
4440
4441 buf = (unsigned char *) &sa6->sin6_addr;
4442 if (have_c)
4443 print_ip6c_addr(s, buf);
4444 else
4445 print_ip6_addr(s, i, buf);
4446
4447 if (have_p)
4448 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4449 }
4450
4451 return rc;
4452}
4453
4454static int print_ip_arg(struct trace_seq *s, const char *ptr,
4455 void *data, int size, struct event_format *event,
4456 struct print_arg *arg)
4457{
4458 char i = *ptr; /* 'i' or 'I' */
4459 char ver;
4460 int rc = 0;
4461
4462 ptr++;
4463 rc++;
4464
4465 ver = *ptr;
4466 ptr++;
4467 rc++;
4468
4469 switch (ver) {
4470 case '4':
4471 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4472 break;
4473 case '6':
4474 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4475 break;
4476 case 'S':
4477 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4478 break;
4479 default:
4480 return 0;
4481 }
4482
4483 return rc;
4484}
4485
Namhyung Kim600da3c2012-06-22 17:10:15 +09004486static int is_printable_array(char *p, unsigned int len)
4487{
4488 unsigned int i;
4489
4490 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004491 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004492 return 0;
4493 return 1;
4494}
4495
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03004496static void print_event_fields(struct trace_seq *s, void *data,
4497 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004498 struct event_format *event)
4499{
4500 struct format_field *field;
4501 unsigned long long val;
4502 unsigned int offset, len, i;
4503
4504 field = event->format.fields;
4505 while (field) {
4506 trace_seq_printf(s, " %s=", field->name);
4507 if (field->flags & FIELD_IS_ARRAY) {
4508 offset = field->offset;
4509 len = field->size;
4510 if (field->flags & FIELD_IS_DYNAMIC) {
4511 val = pevent_read_number(event->pevent, data + offset, len);
4512 offset = val;
4513 len = offset >> 16;
4514 offset &= 0xffff;
4515 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09004516 if (field->flags & FIELD_IS_STRING &&
4517 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004518 trace_seq_printf(s, "%s", (char *)data + offset);
4519 } else {
4520 trace_seq_puts(s, "ARRAY[");
4521 for (i = 0; i < len; i++) {
4522 if (i)
4523 trace_seq_puts(s, ", ");
4524 trace_seq_printf(s, "%02x",
4525 *((unsigned char *)data + offset + i));
4526 }
4527 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09004528 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004529 }
4530 } else {
4531 val = pevent_read_number(event->pevent, data + field->offset,
4532 field->size);
4533 if (field->flags & FIELD_IS_POINTER) {
4534 trace_seq_printf(s, "0x%llx", val);
4535 } else if (field->flags & FIELD_IS_SIGNED) {
4536 switch (field->size) {
4537 case 4:
4538 /*
4539 * If field is long then print it in hex.
4540 * A long usually stores pointers.
4541 */
4542 if (field->flags & FIELD_IS_LONG)
4543 trace_seq_printf(s, "0x%x", (int)val);
4544 else
4545 trace_seq_printf(s, "%d", (int)val);
4546 break;
4547 case 2:
4548 trace_seq_printf(s, "%2d", (short)val);
4549 break;
4550 case 1:
4551 trace_seq_printf(s, "%1d", (char)val);
4552 break;
4553 default:
4554 trace_seq_printf(s, "%lld", val);
4555 }
4556 } else {
4557 if (field->flags & FIELD_IS_LONG)
4558 trace_seq_printf(s, "0x%llx", val);
4559 else
4560 trace_seq_printf(s, "%llu", val);
4561 }
4562 }
4563 field = field->next;
4564 }
4565}
4566
4567static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4568{
4569 struct pevent *pevent = event->pevent;
4570 struct print_fmt *print_fmt = &event->print_fmt;
4571 struct print_arg *arg = print_fmt->args;
4572 struct print_arg *args = NULL;
4573 const char *ptr = print_fmt->format;
4574 unsigned long long val;
4575 struct func_map *func;
4576 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004577 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004578 char *bprint_fmt = NULL;
4579 char format[32];
4580 int show_func;
4581 int len_as_arg;
4582 int len_arg;
4583 int len;
4584 int ls;
4585
4586 if (event->flags & EVENT_FL_FAILED) {
4587 trace_seq_printf(s, "[FAILED TO PARSE]");
4588 print_event_fields(s, data, size, event);
4589 return;
4590 }
4591
4592 if (event->flags & EVENT_FL_ISBPRINT) {
4593 bprint_fmt = get_bprint_format(data, size, event);
4594 args = make_bprint_args(bprint_fmt, data, size, event);
4595 arg = args;
4596 ptr = bprint_fmt;
4597 }
4598
4599 for (; *ptr; ptr++) {
4600 ls = 0;
4601 if (*ptr == '\\') {
4602 ptr++;
4603 switch (*ptr) {
4604 case 'n':
4605 trace_seq_putc(s, '\n');
4606 break;
4607 case 't':
4608 trace_seq_putc(s, '\t');
4609 break;
4610 case 'r':
4611 trace_seq_putc(s, '\r');
4612 break;
4613 case '\\':
4614 trace_seq_putc(s, '\\');
4615 break;
4616 default:
4617 trace_seq_putc(s, *ptr);
4618 break;
4619 }
4620
4621 } else if (*ptr == '%') {
4622 saveptr = ptr;
4623 show_func = 0;
4624 len_as_arg = 0;
4625 cont_process:
4626 ptr++;
4627 switch (*ptr) {
4628 case '%':
4629 trace_seq_putc(s, '%');
4630 break;
4631 case '#':
4632 /* FIXME: need to handle properly */
4633 goto cont_process;
4634 case 'h':
4635 ls--;
4636 goto cont_process;
4637 case 'l':
4638 ls++;
4639 goto cont_process;
4640 case 'L':
4641 ls = 2;
4642 goto cont_process;
4643 case '*':
4644 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004645 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004646 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004647 event->flags |= EVENT_FL_FAILED;
4648 goto out_failed;
4649 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004650 len_arg = eval_num_arg(data, size, event, arg);
4651 len_as_arg = 1;
4652 arg = arg->next;
4653 goto cont_process;
4654 case '.':
4655 case 'z':
4656 case 'Z':
4657 case '0' ... '9':
4658 goto cont_process;
4659 case 'p':
4660 if (pevent->long_size == 4)
4661 ls = 1;
4662 else
4663 ls = 2;
4664
4665 if (*(ptr+1) == 'F' ||
4666 *(ptr+1) == 'f') {
4667 ptr++;
4668 show_func = *ptr;
4669 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4670 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4671 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004672 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004673 break;
David Ahern3d199b52014-12-18 19:11:11 -07004674 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4675 int n;
4676
4677 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4678 if (n > 0) {
4679 ptr += n;
4680 arg = arg->next;
4681 break;
4682 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004683 }
4684
4685 /* fall through */
4686 case 'd':
4687 case 'i':
4688 case 'x':
4689 case 'X':
4690 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004691 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004692 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004693 event->flags |= EVENT_FL_FAILED;
4694 goto out_failed;
4695 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004696
4697 len = ((unsigned long)ptr + 1) -
4698 (unsigned long)saveptr;
4699
4700 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004701 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004702 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004703 event->flags |= EVENT_FL_FAILED;
4704 len = 31;
4705 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004706
4707 memcpy(format, saveptr, len);
4708 format[len] = 0;
4709
4710 val = eval_num_arg(data, size, event, arg);
4711 arg = arg->next;
4712
4713 if (show_func) {
4714 func = find_func(pevent, val);
4715 if (func) {
4716 trace_seq_puts(s, func->func);
4717 if (show_func == 'F')
4718 trace_seq_printf(s,
4719 "+0x%llx",
4720 val - func->addr);
4721 break;
4722 }
4723 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004724 if (pevent->long_size == 8 && ls &&
4725 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004726 char *p;
4727
4728 ls = 2;
4729 /* make %l into %ll */
4730 p = strchr(format, 'l');
4731 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004732 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004733 else if (strcmp(format, "%p") == 0)
4734 strcpy(format, "0x%llx");
4735 }
4736 switch (ls) {
4737 case -2:
4738 if (len_as_arg)
4739 trace_seq_printf(s, format, len_arg, (char)val);
4740 else
4741 trace_seq_printf(s, format, (char)val);
4742 break;
4743 case -1:
4744 if (len_as_arg)
4745 trace_seq_printf(s, format, len_arg, (short)val);
4746 else
4747 trace_seq_printf(s, format, (short)val);
4748 break;
4749 case 0:
4750 if (len_as_arg)
4751 trace_seq_printf(s, format, len_arg, (int)val);
4752 else
4753 trace_seq_printf(s, format, (int)val);
4754 break;
4755 case 1:
4756 if (len_as_arg)
4757 trace_seq_printf(s, format, len_arg, (long)val);
4758 else
4759 trace_seq_printf(s, format, (long)val);
4760 break;
4761 case 2:
4762 if (len_as_arg)
4763 trace_seq_printf(s, format, len_arg,
4764 (long long)val);
4765 else
4766 trace_seq_printf(s, format, (long long)val);
4767 break;
4768 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09004769 do_warning_event(event, "bad count (%d)", ls);
Namhyung Kim245c5a12012-09-07 11:49:45 +09004770 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004771 }
4772 break;
4773 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004774 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004775 do_warning_event(event, "no matching argument");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004776 event->flags |= EVENT_FL_FAILED;
4777 goto out_failed;
4778 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004779
4780 len = ((unsigned long)ptr + 1) -
4781 (unsigned long)saveptr;
4782
4783 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004784 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004785 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004786 event->flags |= EVENT_FL_FAILED;
4787 len = 31;
4788 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004789
4790 memcpy(format, saveptr, len);
4791 format[len] = 0;
4792 if (!len_as_arg)
4793 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05004794 /* Use helper trace_seq */
4795 trace_seq_init(&p);
4796 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004797 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05004798 trace_seq_terminate(&p);
4799 trace_seq_puts(s, p.buffer);
Steven Rostedtde04f862014-04-22 19:23:30 -04004800 trace_seq_destroy(&p);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004801 arg = arg->next;
4802 break;
4803 default:
4804 trace_seq_printf(s, ">%c<", *ptr);
4805
4806 }
4807 } else
4808 trace_seq_putc(s, *ptr);
4809 }
4810
Namhyung Kim245c5a12012-09-07 11:49:45 +09004811 if (event->flags & EVENT_FL_FAILED) {
4812out_failed:
4813 trace_seq_printf(s, "[FAILED TO PARSE]");
4814 }
4815
Steven Rostedtf7d82352012-04-06 00:47:53 +02004816 if (args) {
4817 free_args(args);
4818 free(bprint_fmt);
4819 }
4820}
4821
4822/**
4823 * pevent_data_lat_fmt - parse the data for the latency format
4824 * @pevent: a handle to the pevent
4825 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004826 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004827 *
4828 * This parses out the Latency format (interrupts disabled,
4829 * need rescheduling, in hard/soft interrupt, preempt count
4830 * and lock depth) and places it into the trace_seq.
4831 */
4832void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004833 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004834{
4835 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004836 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004837 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004838 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004839 unsigned int lat_flags;
4840 unsigned int pc;
4841 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004842 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004843 int hardirq;
4844 int softirq;
4845 void *data = record->data;
4846
4847 lat_flags = parse_common_flags(pevent, data);
4848 pc = parse_common_pc(pevent, data);
4849 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004850 if (lock_depth_exists)
4851 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004852 else if (check_lock_depth) {
4853 lock_depth = parse_common_lock_depth(pevent, data);
4854 if (lock_depth < 0)
4855 check_lock_depth = 0;
4856 else
4857 lock_depth_exists = 1;
4858 }
4859
4860 /* migrate_disable may not always exist */
4861 if (migrate_disable_exists)
4862 migrate_disable = parse_common_migrate_disable(pevent, data);
4863 else if (check_migrate_disable) {
4864 migrate_disable = parse_common_migrate_disable(pevent, data);
4865 if (migrate_disable < 0)
4866 check_migrate_disable = 0;
4867 else
4868 migrate_disable_exists = 1;
4869 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004870
4871 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4872 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4873
4874 trace_seq_printf(s, "%c%c%c",
4875 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4876 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4877 'X' : '.',
4878 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4879 'N' : '.',
4880 (hardirq && softirq) ? 'H' :
4881 hardirq ? 'h' : softirq ? 's' : '.');
4882
4883 if (pc)
4884 trace_seq_printf(s, "%x", pc);
4885 else
4886 trace_seq_putc(s, '.');
4887
Steven Rostedt0866a972012-05-22 14:52:40 +09004888 if (migrate_disable_exists) {
4889 if (migrate_disable < 0)
4890 trace_seq_putc(s, '.');
4891 else
4892 trace_seq_printf(s, "%d", migrate_disable);
4893 }
4894
Steven Rostedtf7d82352012-04-06 00:47:53 +02004895 if (lock_depth_exists) {
4896 if (lock_depth < 0)
4897 trace_seq_putc(s, '.');
4898 else
4899 trace_seq_printf(s, "%d", lock_depth);
4900 }
4901
4902 trace_seq_terminate(s);
4903}
4904
4905/**
4906 * pevent_data_type - parse out the given event type
4907 * @pevent: a handle to the pevent
4908 * @rec: the record to read from
4909 *
4910 * This returns the event id from the @rec.
4911 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004912int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004913{
4914 return trace_parse_common_type(pevent, rec->data);
4915}
4916
4917/**
4918 * pevent_data_event_from_type - find the event by a given type
4919 * @pevent: a handle to the pevent
4920 * @type: the type of the event.
4921 *
4922 * This returns the event form a given @type;
4923 */
4924struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4925{
4926 return pevent_find_event(pevent, type);
4927}
4928
4929/**
4930 * pevent_data_pid - parse the PID from raw data
4931 * @pevent: a handle to the pevent
4932 * @rec: the record to parse
4933 *
4934 * This returns the PID from a raw data.
4935 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004936int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004937{
4938 return parse_common_pid(pevent, rec->data);
4939}
4940
4941/**
4942 * pevent_data_comm_from_pid - return the command line from PID
4943 * @pevent: a handle to the pevent
4944 * @pid: the PID of the task to search for
4945 *
4946 * This returns a pointer to the command line that has the given
4947 * @pid.
4948 */
4949const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4950{
4951 const char *comm;
4952
4953 comm = find_cmdline(pevent, pid);
4954 return comm;
4955}
4956
4957/**
4958 * pevent_data_comm_from_pid - parse the data into the print format
4959 * @s: the trace_seq to write to
4960 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004961 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004962 *
4963 * This parses the raw @data using the given @event information and
4964 * writes the print format into the trace_seq.
4965 */
4966void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004967 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004968{
4969 int print_pretty = 1;
4970
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004971 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004972 print_event_fields(s, record->data, record->size, event);
4973 else {
4974
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004975 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004976 print_pretty = event->handler(s, record, event,
4977 event->context);
4978
4979 if (print_pretty)
4980 pretty_print(s, record->data, record->size, event);
4981 }
4982
4983 trace_seq_terminate(s);
4984}
4985
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004986static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4987{
4988 if (!use_trace_clock)
4989 return true;
4990
4991 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4992 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4993 return true;
4994
4995 /* trace_clock is setting in tsc or counter mode */
4996 return false;
4997}
4998
Steven Rostedtf7d82352012-04-06 00:47:53 +02004999void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005000 struct pevent_record *record, bool use_trace_clock)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005001{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03005002 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005003 struct event_format *event;
5004 unsigned long secs;
5005 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005006 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005007 const char *comm;
5008 void *data = record->data;
5009 int type;
5010 int pid;
5011 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005012 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005013 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005014
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005015 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5016 use_trace_clock);
5017 if (use_usec_format) {
5018 secs = record->ts / NSECS_PER_SEC;
5019 nsecs = record->ts - secs * NSECS_PER_SEC;
5020 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005021
5022 if (record->size < 0) {
5023 do_warning("ug! negative record size %d", record->size);
5024 return;
5025 }
5026
5027 type = trace_parse_common_type(pevent, data);
5028
5029 event = pevent_find_event(pevent, type);
5030 if (!event) {
5031 do_warning("ug! no event found for type %d", type);
5032 return;
5033 }
5034
5035 pid = parse_common_pid(pevent, data);
5036 comm = find_cmdline(pevent, pid);
5037
5038 if (pevent->latency_format) {
5039 trace_seq_printf(s, "%8.8s-%-5d %3d",
5040 comm, pid, record->cpu);
5041 pevent_data_lat_fmt(pevent, s, record);
5042 } else
5043 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5044
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005045 if (use_usec_format) {
5046 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5047 usecs = nsecs;
5048 p = 9;
5049 } else {
5050 usecs = (nsecs + 500) / NSECS_PER_USEC;
5051 p = 6;
5052 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02005053
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005054 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5055 secs, p, usecs, event->name);
5056 } else
5057 trace_seq_printf(s, " %12llu: %s: ",
5058 record->ts, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005059
5060 /* Space out the event names evenly. */
5061 len = strlen(event->name);
5062 if (len < 20)
5063 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5064
5065 pevent_event_info(s, event, record);
5066}
5067
5068static int events_id_cmp(const void *a, const void *b)
5069{
5070 struct event_format * const * ea = a;
5071 struct event_format * const * eb = b;
5072
5073 if ((*ea)->id < (*eb)->id)
5074 return -1;
5075
5076 if ((*ea)->id > (*eb)->id)
5077 return 1;
5078
5079 return 0;
5080}
5081
5082static int events_name_cmp(const void *a, const void *b)
5083{
5084 struct event_format * const * ea = a;
5085 struct event_format * const * eb = b;
5086 int res;
5087
5088 res = strcmp((*ea)->name, (*eb)->name);
5089 if (res)
5090 return res;
5091
5092 res = strcmp((*ea)->system, (*eb)->system);
5093 if (res)
5094 return res;
5095
5096 return events_id_cmp(a, b);
5097}
5098
5099static int events_system_cmp(const void *a, const void *b)
5100{
5101 struct event_format * const * ea = a;
5102 struct event_format * const * eb = b;
5103 int res;
5104
5105 res = strcmp((*ea)->system, (*eb)->system);
5106 if (res)
5107 return res;
5108
5109 res = strcmp((*ea)->name, (*eb)->name);
5110 if (res)
5111 return res;
5112
5113 return events_id_cmp(a, b);
5114}
5115
5116struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5117{
5118 struct event_format **events;
5119 int (*sort)(const void *a, const void *b);
5120
5121 events = pevent->sort_events;
5122
5123 if (events && pevent->last_type == sort_type)
5124 return events;
5125
5126 if (!events) {
5127 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5128 if (!events)
5129 return NULL;
5130
5131 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5132 events[pevent->nr_events] = NULL;
5133
5134 pevent->sort_events = events;
5135
5136 /* the internal events are sorted by id */
5137 if (sort_type == EVENT_SORT_ID) {
5138 pevent->last_type = sort_type;
5139 return events;
5140 }
5141 }
5142
5143 switch (sort_type) {
5144 case EVENT_SORT_ID:
5145 sort = events_id_cmp;
5146 break;
5147 case EVENT_SORT_NAME:
5148 sort = events_name_cmp;
5149 break;
5150 case EVENT_SORT_SYSTEM:
5151 sort = events_system_cmp;
5152 break;
5153 default:
5154 return events;
5155 }
5156
5157 qsort(events, pevent->nr_events, sizeof(*events), sort);
5158 pevent->last_type = sort_type;
5159
5160 return events;
5161}
5162
5163static struct format_field **
5164get_event_fields(const char *type, const char *name,
5165 int count, struct format_field *list)
5166{
5167 struct format_field **fields;
5168 struct format_field *field;
5169 int i = 0;
5170
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03005171 fields = malloc(sizeof(*fields) * (count + 1));
5172 if (!fields)
5173 return NULL;
5174
Steven Rostedtf7d82352012-04-06 00:47:53 +02005175 for (field = list; field; field = field->next) {
5176 fields[i++] = field;
5177 if (i == count + 1) {
5178 do_warning("event %s has more %s fields than specified",
5179 name, type);
5180 i--;
5181 break;
5182 }
5183 }
5184
5185 if (i != count)
5186 do_warning("event %s has less %s fields than specified",
5187 name, type);
5188
5189 fields[i] = NULL;
5190
5191 return fields;
5192}
5193
5194/**
5195 * pevent_event_common_fields - return a list of common fields for an event
5196 * @event: the event to return the common fields of.
5197 *
5198 * Returns an allocated array of fields. The last item in the array is NULL.
5199 * The array must be freed with free().
5200 */
5201struct format_field **pevent_event_common_fields(struct event_format *event)
5202{
5203 return get_event_fields("common", event->name,
5204 event->format.nr_common,
5205 event->format.common_fields);
5206}
5207
5208/**
5209 * pevent_event_fields - return a list of event specific fields for an event
5210 * @event: the event to return the fields of.
5211 *
5212 * Returns an allocated array of fields. The last item in the array is NULL.
5213 * The array must be freed with free().
5214 */
5215struct format_field **pevent_event_fields(struct event_format *event)
5216{
5217 return get_event_fields("event", event->name,
5218 event->format.nr_fields,
5219 event->format.fields);
5220}
5221
5222static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5223{
5224 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5225 if (field->next) {
5226 trace_seq_puts(s, ", ");
5227 print_fields(s, field->next);
5228 }
5229}
5230
5231/* for debugging */
5232static void print_args(struct print_arg *args)
5233{
5234 int print_paren = 1;
5235 struct trace_seq s;
5236
5237 switch (args->type) {
5238 case PRINT_NULL:
5239 printf("null");
5240 break;
5241 case PRINT_ATOM:
5242 printf("%s", args->atom.atom);
5243 break;
5244 case PRINT_FIELD:
5245 printf("REC->%s", args->field.name);
5246 break;
5247 case PRINT_FLAGS:
5248 printf("__print_flags(");
5249 print_args(args->flags.field);
5250 printf(", %s, ", args->flags.delim);
5251 trace_seq_init(&s);
5252 print_fields(&s, args->flags.flags);
5253 trace_seq_do_printf(&s);
5254 trace_seq_destroy(&s);
5255 printf(")");
5256 break;
5257 case PRINT_SYMBOL:
5258 printf("__print_symbolic(");
5259 print_args(args->symbol.field);
5260 printf(", ");
5261 trace_seq_init(&s);
5262 print_fields(&s, args->symbol.symbols);
5263 trace_seq_do_printf(&s);
5264 trace_seq_destroy(&s);
5265 printf(")");
5266 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09005267 case PRINT_HEX:
5268 printf("__print_hex(");
5269 print_args(args->hex.field);
5270 printf(", ");
5271 print_args(args->hex.size);
5272 printf(")");
5273 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005274 case PRINT_STRING:
5275 case PRINT_BSTRING:
5276 printf("__get_str(%s)", args->string.string);
5277 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04005278 case PRINT_BITMASK:
5279 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5280 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005281 case PRINT_TYPE:
5282 printf("(%s)", args->typecast.type);
5283 print_args(args->typecast.item);
5284 break;
5285 case PRINT_OP:
5286 if (strcmp(args->op.op, ":") == 0)
5287 print_paren = 0;
5288 if (print_paren)
5289 printf("(");
5290 print_args(args->op.left);
5291 printf(" %s ", args->op.op);
5292 print_args(args->op.right);
5293 if (print_paren)
5294 printf(")");
5295 break;
5296 default:
5297 /* we should warn... */
5298 return;
5299 }
5300 if (args->next) {
5301 printf("\n");
5302 print_args(args->next);
5303 }
5304}
5305
5306static void parse_header_field(const char *field,
5307 int *offset, int *size, int mandatory)
5308{
5309 unsigned long long save_input_buf_ptr;
5310 unsigned long long save_input_buf_siz;
5311 char *token;
5312 int type;
5313
5314 save_input_buf_ptr = input_buf_ptr;
5315 save_input_buf_siz = input_buf_siz;
5316
5317 if (read_expected(EVENT_ITEM, "field") < 0)
5318 return;
5319 if (read_expected(EVENT_OP, ":") < 0)
5320 return;
5321
5322 /* type */
5323 if (read_expect_type(EVENT_ITEM, &token) < 0)
5324 goto fail;
5325 free_token(token);
5326
5327 /*
5328 * If this is not a mandatory field, then test it first.
5329 */
5330 if (mandatory) {
5331 if (read_expected(EVENT_ITEM, field) < 0)
5332 return;
5333 } else {
5334 if (read_expect_type(EVENT_ITEM, &token) < 0)
5335 goto fail;
5336 if (strcmp(token, field) != 0)
5337 goto discard;
5338 free_token(token);
5339 }
5340
5341 if (read_expected(EVENT_OP, ";") < 0)
5342 return;
5343 if (read_expected(EVENT_ITEM, "offset") < 0)
5344 return;
5345 if (read_expected(EVENT_OP, ":") < 0)
5346 return;
5347 if (read_expect_type(EVENT_ITEM, &token) < 0)
5348 goto fail;
5349 *offset = atoi(token);
5350 free_token(token);
5351 if (read_expected(EVENT_OP, ";") < 0)
5352 return;
5353 if (read_expected(EVENT_ITEM, "size") < 0)
5354 return;
5355 if (read_expected(EVENT_OP, ":") < 0)
5356 return;
5357 if (read_expect_type(EVENT_ITEM, &token) < 0)
5358 goto fail;
5359 *size = atoi(token);
5360 free_token(token);
5361 if (read_expected(EVENT_OP, ";") < 0)
5362 return;
5363 type = read_token(&token);
5364 if (type != EVENT_NEWLINE) {
5365 /* newer versions of the kernel have a "signed" type */
5366 if (type != EVENT_ITEM)
5367 goto fail;
5368
5369 if (strcmp(token, "signed") != 0)
5370 goto fail;
5371
5372 free_token(token);
5373
5374 if (read_expected(EVENT_OP, ":") < 0)
5375 return;
5376
5377 if (read_expect_type(EVENT_ITEM, &token))
5378 goto fail;
5379
5380 free_token(token);
5381 if (read_expected(EVENT_OP, ";") < 0)
5382 return;
5383
5384 if (read_expect_type(EVENT_NEWLINE, &token))
5385 goto fail;
5386 }
5387 fail:
5388 free_token(token);
5389 return;
5390
5391 discard:
5392 input_buf_ptr = save_input_buf_ptr;
5393 input_buf_siz = save_input_buf_siz;
5394 *offset = 0;
5395 *size = 0;
5396 free_token(token);
5397}
5398
5399/**
5400 * pevent_parse_header_page - parse the data stored in the header page
5401 * @pevent: the handle to the pevent
5402 * @buf: the buffer storing the header page format string
5403 * @size: the size of @buf
5404 * @long_size: the long size to use if there is no header
5405 *
5406 * This parses the header page format for information on the
5407 * ring buffer used. The @buf should be copied from
5408 *
5409 * /sys/kernel/debug/tracing/events/header_page
5410 */
5411int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5412 int long_size)
5413{
5414 int ignore;
5415
5416 if (!size) {
5417 /*
5418 * Old kernels did not have header page info.
5419 * Sorry but we just use what we find here in user space.
5420 */
5421 pevent->header_page_ts_size = sizeof(long long);
5422 pevent->header_page_size_size = long_size;
5423 pevent->header_page_data_offset = sizeof(long long) + long_size;
5424 pevent->old_format = 1;
5425 return -1;
5426 }
5427 init_input_buf(buf, size);
5428
5429 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5430 &pevent->header_page_ts_size, 1);
5431 parse_header_field("commit", &pevent->header_page_size_offset,
5432 &pevent->header_page_size_size, 1);
5433 parse_header_field("overwrite", &pevent->header_page_overwrite,
5434 &ignore, 0);
5435 parse_header_field("data", &pevent->header_page_data_offset,
5436 &pevent->header_page_data_size, 1);
5437
5438 return 0;
5439}
5440
5441static int event_matches(struct event_format *event,
5442 int id, const char *sys_name,
5443 const char *event_name)
5444{
5445 if (id >= 0 && id != event->id)
5446 return 0;
5447
5448 if (event_name && (strcmp(event_name, event->name) != 0))
5449 return 0;
5450
5451 if (sys_name && (strcmp(sys_name, event->system) != 0))
5452 return 0;
5453
5454 return 1;
5455}
5456
5457static void free_handler(struct event_handler *handle)
5458{
5459 free((void *)handle->sys_name);
5460 free((void *)handle->event_name);
5461 free(handle);
5462}
5463
5464static int find_event_handle(struct pevent *pevent, struct event_format *event)
5465{
5466 struct event_handler *handle, **next;
5467
5468 for (next = &pevent->handlers; *next;
5469 next = &(*next)->next) {
5470 handle = *next;
5471 if (event_matches(event, handle->id,
5472 handle->sys_name,
5473 handle->event_name))
5474 break;
5475 }
5476
5477 if (!(*next))
5478 return 0;
5479
5480 pr_stat("overriding event (%d) %s:%s with new print handler",
5481 event->id, event->system, event->name);
5482
5483 event->handler = handle->func;
5484 event->context = handle->context;
5485
5486 *next = handle->next;
5487 free_handler(handle);
5488
5489 return 1;
5490}
5491
5492/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005493 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005494 * @buf: the buffer storing the event format string
5495 * @size: the size of @buf
5496 * @sys: the system the event belongs to
5497 *
5498 * This parses the event format and creates an event structure
5499 * to quickly parse raw data for a given event.
5500 *
5501 * These files currently come from:
5502 *
5503 * /sys/kernel/debug/tracing/events/.../.../format
5504 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005505enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5506 struct pevent *pevent, const char *buf,
5507 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005508{
5509 struct event_format *event;
5510 int ret;
5511
5512 init_input_buf(buf, size);
5513
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005514 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02005515 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09005516 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005517
5518 event->name = event_read_name();
5519 if (!event->name) {
5520 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09005521 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5522 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005523 }
5524
5525 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005526 event->flags |= EVENT_FL_ISFTRACE;
5527
5528 if (strcmp(event->name, "bprint") == 0)
5529 event->flags |= EVENT_FL_ISBPRINT;
5530 }
5531
5532 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005533 if (event->id < 0) {
5534 ret = PEVENT_ERRNO__READ_ID_FAILED;
5535 /*
5536 * This isn't an allocation error actually.
5537 * But as the ID is critical, just bail out.
5538 */
5539 goto event_alloc_failed;
5540 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005541
5542 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005543 if (!event->system) {
5544 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5545 goto event_alloc_failed;
5546 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005547
Steven Rostedt101782e2012-10-01 20:13:51 -04005548 /* Add pevent to event so that it can be referenced */
5549 event->pevent = pevent;
5550
Steven Rostedtf7d82352012-04-06 00:47:53 +02005551 ret = event_read_format(event);
5552 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005553 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5554 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005555 }
5556
5557 /*
5558 * If the event has an override, don't print warnings if the event
5559 * print format fails to parse.
5560 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005561 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005562 show_warning = 0;
5563
5564 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005565 show_warning = 1;
5566
Steven Rostedtf7d82352012-04-06 00:47:53 +02005567 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005568 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5569 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005570 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005571
5572 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5573 struct format_field *field;
5574 struct print_arg *arg, **list;
5575
5576 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005577 list = &event->print_fmt.args;
5578 for (field = event->format.fields; field; field = field->next) {
5579 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09005580 if (!arg) {
5581 event->flags |= EVENT_FL_FAILED;
5582 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5583 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005584 arg->type = PRINT_FIELD;
5585 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09005586 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09005587 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005588 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005589 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005590 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005591 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005592 *list = arg;
5593 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005594 }
5595 return 0;
5596 }
5597
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005598 return 0;
5599
5600 event_parse_failed:
5601 event->flags |= EVENT_FL_FAILED;
5602 return ret;
5603
5604 event_alloc_failed:
5605 free(event->system);
5606 free(event->name);
5607 free(event);
5608 *eventp = NULL;
5609 return ret;
5610}
5611
Jiri Olsa71ad9582013-12-03 14:09:19 +01005612static enum pevent_errno
5613__pevent_parse_event(struct pevent *pevent,
5614 struct event_format **eventp,
5615 const char *buf, unsigned long size,
5616 const char *sys)
5617{
5618 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5619 struct event_format *event = *eventp;
5620
5621 if (event == NULL)
5622 return ret;
5623
5624 if (pevent && add_event(pevent, event)) {
5625 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5626 goto event_add_failed;
5627 }
5628
5629#define PRINT_ARGS 0
5630 if (PRINT_ARGS && event->print_fmt.args)
5631 print_args(event->print_fmt.args);
5632
5633 return 0;
5634
5635event_add_failed:
5636 pevent_free_format(event);
5637 return ret;
5638}
5639
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005640/**
5641 * pevent_parse_format - parse the event format
Jiri Olsa71ad9582013-12-03 14:09:19 +01005642 * @pevent: the handle to the pevent
5643 * @eventp: returned format
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005644 * @buf: the buffer storing the event format string
5645 * @size: the size of @buf
5646 * @sys: the system the event belongs to
5647 *
5648 * This parses the event format and creates an event structure
5649 * to quickly parse raw data for a given event.
5650 *
5651 * These files currently come from:
5652 *
5653 * /sys/kernel/debug/tracing/events/.../.../format
5654 */
Jiri Olsa71ad9582013-12-03 14:09:19 +01005655enum pevent_errno pevent_parse_format(struct pevent *pevent,
5656 struct event_format **eventp,
5657 const char *buf,
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005658 unsigned long size, const char *sys)
5659{
Jiri Olsa71ad9582013-12-03 14:09:19 +01005660 return __pevent_parse_event(pevent, eventp, buf, size, sys);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005661}
5662
5663/**
5664 * pevent_parse_event - parse the event format
5665 * @pevent: the handle to the pevent
5666 * @buf: the buffer storing the event format string
5667 * @size: the size of @buf
5668 * @sys: the system the event belongs to
5669 *
5670 * This parses the event format and creates an event structure
5671 * to quickly parse raw data for a given event.
5672 *
5673 * These files currently come from:
5674 *
5675 * /sys/kernel/debug/tracing/events/.../.../format
5676 */
5677enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5678 unsigned long size, const char *sys)
5679{
5680 struct event_format *event = NULL;
Jiri Olsa71ad9582013-12-03 14:09:19 +01005681 return __pevent_parse_event(pevent, &event, buf, size, sys);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005682}
5683
Namhyung Kim2f197b92012-08-22 16:00:30 +09005684#undef _PE
5685#define _PE(code, str) str
5686static const char * const pevent_error_str[] = {
5687 PEVENT_ERRORS
5688};
5689#undef _PE
5690
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005691int pevent_strerror(struct pevent *pevent __maybe_unused,
5692 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005693{
5694 int idx;
5695 const char *msg;
5696
5697 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005698 msg = strerror_r(errnum, buf, buflen);
5699 if (msg != buf) {
5700 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005701 memcpy(buf, msg, min(buflen - 1, len));
5702 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005703 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005704 return 0;
5705 }
5706
5707 if (errnum <= __PEVENT_ERRNO__START ||
5708 errnum >= __PEVENT_ERRNO__END)
5709 return -1;
5710
Namhyung Kimf63fe792012-08-23 16:37:00 +09005711 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005712 msg = pevent_error_str[idx];
Namhyung Kimbf19b822013-12-12 16:36:17 +09005713 snprintf(buf, buflen, "%s", msg);
Namhyung Kim2f197b92012-08-22 16:00:30 +09005714
5715 return 0;
5716}
5717
Steven Rostedtf7d82352012-04-06 00:47:53 +02005718int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005719 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005720 unsigned long long *val, int err)
5721{
5722 if (!field) {
5723 if (err)
5724 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5725 return -1;
5726 }
5727
5728 if (pevent_read_number_field(field, record->data, val)) {
5729 if (err)
5730 trace_seq_printf(s, " %s=INVALID", name);
5731 return -1;
5732 }
5733
5734 return 0;
5735}
5736
5737/**
5738 * pevent_get_field_raw - return the raw pointer into the data field
5739 * @s: The seq to print to on error
5740 * @event: the event that the field is for
5741 * @name: The name of the field
5742 * @record: The record with the field name.
5743 * @len: place to store the field length.
5744 * @err: print default error if failed.
5745 *
5746 * Returns a pointer into record->data of the field and places
5747 * the length of the field in @len.
5748 *
5749 * On failure, it returns NULL.
5750 */
5751void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005752 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005753 int *len, int err)
5754{
5755 struct format_field *field;
5756 void *data = record->data;
5757 unsigned offset;
5758 int dummy;
5759
5760 if (!event)
5761 return NULL;
5762
5763 field = pevent_find_field(event, name);
5764
5765 if (!field) {
5766 if (err)
5767 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5768 return NULL;
5769 }
5770
5771 /* Allow @len to be NULL */
5772 if (!len)
5773 len = &dummy;
5774
5775 offset = field->offset;
5776 if (field->flags & FIELD_IS_DYNAMIC) {
5777 offset = pevent_read_number(event->pevent,
5778 data + offset, field->size);
5779 *len = offset >> 16;
5780 offset &= 0xffff;
5781 } else
5782 *len = field->size;
5783
5784 return data + offset;
5785}
5786
5787/**
5788 * pevent_get_field_val - find a field and return its value
5789 * @s: The seq to print to on error
5790 * @event: the event that the field is for
5791 * @name: The name of the field
5792 * @record: The record with the field name.
5793 * @val: place to store the value of the field.
5794 * @err: print default error if failed.
5795 *
5796 * Returns 0 on success -1 on field not found.
5797 */
5798int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005799 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005800 unsigned long long *val, int err)
5801{
5802 struct format_field *field;
5803
5804 if (!event)
5805 return -1;
5806
5807 field = pevent_find_field(event, name);
5808
5809 return get_field_val(s, field, name, record, val, err);
5810}
5811
5812/**
5813 * pevent_get_common_field_val - find a common field and return its value
5814 * @s: The seq to print to on error
5815 * @event: the event that the field is for
5816 * @name: The name of the field
5817 * @record: The record with the field name.
5818 * @val: place to store the value of the field.
5819 * @err: print default error if failed.
5820 *
5821 * Returns 0 on success -1 on field not found.
5822 */
5823int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005824 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005825 unsigned long long *val, int err)
5826{
5827 struct format_field *field;
5828
5829 if (!event)
5830 return -1;
5831
5832 field = pevent_find_common_field(event, name);
5833
5834 return get_field_val(s, field, name, record, val, err);
5835}
5836
5837/**
5838 * pevent_get_any_field_val - find a any field and return its value
5839 * @s: The seq to print to on error
5840 * @event: the event that the field is for
5841 * @name: The name of the field
5842 * @record: The record with the field name.
5843 * @val: place to store the value of the field.
5844 * @err: print default error if failed.
5845 *
5846 * Returns 0 on success -1 on field not found.
5847 */
5848int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005849 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005850 unsigned long long *val, int err)
5851{
5852 struct format_field *field;
5853
5854 if (!event)
5855 return -1;
5856
5857 field = pevent_find_any_field(event, name);
5858
5859 return get_field_val(s, field, name, record, val, err);
5860}
5861
5862/**
5863 * pevent_print_num_field - print a field and a format
5864 * @s: The seq to print to
5865 * @fmt: The printf format to print the field with.
5866 * @event: the event that the field is for
5867 * @name: The name of the field
5868 * @record: The record with the field name.
5869 * @err: print default error if failed.
5870 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005871 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005872 */
5873int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5874 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005875 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005876{
5877 struct format_field *field = pevent_find_field(event, name);
5878 unsigned long long val;
5879
5880 if (!field)
5881 goto failed;
5882
5883 if (pevent_read_number_field(field, record->data, &val))
5884 goto failed;
5885
5886 return trace_seq_printf(s, fmt, val);
5887
5888 failed:
5889 if (err)
5890 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5891 return -1;
5892}
5893
Steven Rostedt6d862b82013-11-01 17:54:00 -04005894/**
5895 * pevent_print_func_field - print a field and a format for function pointers
5896 * @s: The seq to print to
5897 * @fmt: The printf format to print the field with.
5898 * @event: the event that the field is for
5899 * @name: The name of the field
5900 * @record: The record with the field name.
5901 * @err: print default error if failed.
5902 *
5903 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5904 */
5905int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5906 struct event_format *event, const char *name,
5907 struct pevent_record *record, int err)
5908{
5909 struct format_field *field = pevent_find_field(event, name);
5910 struct pevent *pevent = event->pevent;
5911 unsigned long long val;
5912 struct func_map *func;
5913 char tmp[128];
5914
5915 if (!field)
5916 goto failed;
5917
5918 if (pevent_read_number_field(field, record->data, &val))
5919 goto failed;
5920
5921 func = find_func(pevent, val);
5922
5923 if (func)
5924 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5925 else
5926 sprintf(tmp, "0x%08llx", val);
5927
5928 return trace_seq_printf(s, fmt, tmp);
5929
5930 failed:
5931 if (err)
5932 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5933 return -1;
5934}
5935
Steven Rostedtf7d82352012-04-06 00:47:53 +02005936static void free_func_handle(struct pevent_function_handler *func)
5937{
5938 struct pevent_func_params *params;
5939
5940 free(func->name);
5941
5942 while (func->params) {
5943 params = func->params;
5944 func->params = params->next;
5945 free(params);
5946 }
5947
5948 free(func);
5949}
5950
5951/**
5952 * pevent_register_print_function - register a helper function
5953 * @pevent: the handle to the pevent
5954 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005955 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005956 * @name: the name of the helper function
5957 * @parameters: A list of enum pevent_func_arg_type
5958 *
5959 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005960 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005961 * of these functions.
5962 *
5963 * The @parameters is a variable list of pevent_func_arg_type enums that
5964 * must end with PEVENT_FUNC_ARG_VOID.
5965 */
5966int pevent_register_print_function(struct pevent *pevent,
5967 pevent_func_handler func,
5968 enum pevent_func_arg_type ret_type,
5969 char *name, ...)
5970{
5971 struct pevent_function_handler *func_handle;
5972 struct pevent_func_params **next_param;
5973 struct pevent_func_params *param;
5974 enum pevent_func_arg_type type;
5975 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005976 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005977
5978 func_handle = find_func_handler(pevent, name);
5979 if (func_handle) {
5980 /*
5981 * This is most like caused by the users own
5982 * plugins updating the function. This overrides the
5983 * system defaults.
5984 */
5985 pr_stat("override of function helper '%s'", name);
5986 remove_func_handler(pevent, name);
5987 }
5988
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005989 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005990 if (!func_handle) {
5991 do_warning("Failed to allocate function handler");
5992 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5993 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005994
5995 func_handle->ret_type = ret_type;
5996 func_handle->name = strdup(name);
5997 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005998 if (!func_handle->name) {
5999 do_warning("Failed to allocate function name");
6000 free(func_handle);
6001 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6002 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006003
6004 next_param = &(func_handle->params);
6005 va_start(ap, name);
6006 for (;;) {
6007 type = va_arg(ap, enum pevent_func_arg_type);
6008 if (type == PEVENT_FUNC_ARG_VOID)
6009 break;
6010
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03006011 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09006012 do_warning("Invalid argument type %d", type);
6013 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006014 goto out_free;
6015 }
6016
Namhyung Kim67ed9392012-09-07 11:49:47 +09006017 param = malloc(sizeof(*param));
6018 if (!param) {
6019 do_warning("Failed to allocate function param");
6020 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6021 goto out_free;
6022 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006023 param->type = type;
6024 param->next = NULL;
6025
6026 *next_param = param;
6027 next_param = &(param->next);
6028
6029 func_handle->nr_args++;
6030 }
6031 va_end(ap);
6032
6033 func_handle->next = pevent->func_handlers;
6034 pevent->func_handlers = func_handle;
6035
6036 return 0;
6037 out_free:
6038 va_end(ap);
6039 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09006040 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006041}
6042
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006043/**
6044 * pevent_unregister_print_function - unregister a helper function
6045 * @pevent: the handle to the pevent
6046 * @func: the function to process the helper function
6047 * @name: the name of the helper function
6048 *
6049 * This function removes existing print handler for function @name.
6050 *
6051 * Returns 0 if the handler was removed successully, -1 otherwise.
6052 */
6053int pevent_unregister_print_function(struct pevent *pevent,
6054 pevent_func_handler func, char *name)
6055{
6056 struct pevent_function_handler *func_handle;
6057
6058 func_handle = find_func_handler(pevent, name);
6059 if (func_handle && func_handle->func == func) {
6060 remove_func_handler(pevent, name);
6061 return 0;
6062 }
6063 return -1;
6064}
6065
Namhyung Kimad137012014-01-16 11:31:07 +09006066static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6067 const char *sys_name,
6068 const char *event_name)
6069{
6070 struct event_format *event;
6071
6072 if (id >= 0) {
6073 /* search by id */
6074 event = pevent_find_event(pevent, id);
6075 if (!event)
6076 return NULL;
6077 if (event_name && (strcmp(event_name, event->name) != 0))
6078 return NULL;
6079 if (sys_name && (strcmp(sys_name, event->system) != 0))
6080 return NULL;
6081 } else {
6082 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6083 if (!event)
6084 return NULL;
6085 }
6086 return event;
6087}
6088
Steven Rostedtf7d82352012-04-06 00:47:53 +02006089/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006090 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02006091 * @pevent: the handle to the pevent
6092 * @id: the id of the event to register
6093 * @sys_name: the system name the event belongs to
6094 * @event_name: the name of the event
6095 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006096 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02006097 *
6098 * This function allows a developer to override the parsing of
6099 * a given event. If for some reason the default print format
6100 * is not sufficient, this function will register a function
6101 * for an event to be used to parse the data instead.
6102 *
6103 * If @id is >= 0, then it is used to find the event.
6104 * else @sys_name and @event_name are used.
6105 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09006106int pevent_register_event_handler(struct pevent *pevent, int id,
6107 const char *sys_name, const char *event_name,
6108 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006109{
6110 struct event_format *event;
6111 struct event_handler *handle;
6112
Namhyung Kimad137012014-01-16 11:31:07 +09006113 event = pevent_search_event(pevent, id, sys_name, event_name);
6114 if (event == NULL)
6115 goto not_found;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006116
6117 pr_stat("overriding event (%d) %s:%s with new print handler",
6118 event->id, event->system, event->name);
6119
6120 event->handler = func;
6121 event->context = context;
6122 return 0;
6123
6124 not_found:
6125 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006126 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006127 if (!handle) {
6128 do_warning("Failed to allocate event handler");
6129 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6130 }
6131
Steven Rostedtf7d82352012-04-06 00:47:53 +02006132 handle->id = id;
6133 if (event_name)
6134 handle->event_name = strdup(event_name);
6135 if (sys_name)
6136 handle->sys_name = strdup(sys_name);
6137
Namhyung Kimca638582012-04-09 11:54:31 +09006138 if ((event_name && !handle->event_name) ||
6139 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006140 do_warning("Failed to allocate event/sys name");
6141 free((void *)handle->event_name);
6142 free((void *)handle->sys_name);
6143 free(handle);
6144 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006145 }
6146
Steven Rostedtf7d82352012-04-06 00:47:53 +02006147 handle->func = func;
6148 handle->next = pevent->handlers;
6149 pevent->handlers = handle;
6150 handle->context = context;
6151
6152 return -1;
6153}
6154
Namhyung Kimad137012014-01-16 11:31:07 +09006155static int handle_matches(struct event_handler *handler, int id,
6156 const char *sys_name, const char *event_name,
6157 pevent_event_handler_func func, void *context)
6158{
6159 if (id >= 0 && id != handler->id)
6160 return 0;
6161
6162 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6163 return 0;
6164
6165 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6166 return 0;
6167
6168 if (func != handler->func || context != handler->context)
6169 return 0;
6170
6171 return 1;
6172}
6173
6174/**
6175 * pevent_unregister_event_handler - unregister an existing event handler
6176 * @pevent: the handle to the pevent
6177 * @id: the id of the event to unregister
6178 * @sys_name: the system name the handler belongs to
6179 * @event_name: the name of the event handler
6180 * @func: the function to call to parse the event information
6181 * @context: the data to be passed to @func
6182 *
6183 * This function removes existing event handler (parser).
6184 *
6185 * If @id is >= 0, then it is used to find the event.
6186 * else @sys_name and @event_name are used.
6187 *
6188 * Returns 0 if handler was removed successfully, -1 if event was not found.
6189 */
6190int pevent_unregister_event_handler(struct pevent *pevent, int id,
6191 const char *sys_name, const char *event_name,
6192 pevent_event_handler_func func, void *context)
6193{
6194 struct event_format *event;
6195 struct event_handler *handle;
6196 struct event_handler **next;
6197
6198 event = pevent_search_event(pevent, id, sys_name, event_name);
6199 if (event == NULL)
6200 goto not_found;
6201
6202 if (event->handler == func && event->context == context) {
6203 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6204 event->id, event->system, event->name);
6205
6206 event->handler = NULL;
6207 event->context = NULL;
6208 return 0;
6209 }
6210
6211not_found:
6212 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6213 handle = *next;
6214 if (handle_matches(handle, id, sys_name, event_name,
6215 func, context))
6216 break;
6217 }
6218
6219 if (!(*next))
6220 return -1;
6221
6222 *next = handle->next;
6223 free_handler(handle);
6224
6225 return 0;
6226}
6227
Steven Rostedtf7d82352012-04-06 00:47:53 +02006228/**
6229 * pevent_alloc - create a pevent handle
6230 */
6231struct pevent *pevent_alloc(void)
6232{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006233 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02006234
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006235 if (pevent)
6236 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006237
6238 return pevent;
6239}
6240
6241void pevent_ref(struct pevent *pevent)
6242{
6243 pevent->ref_count++;
6244}
6245
David Ahern00ae1122015-03-19 12:36:21 -06006246void pevent_free_format_field(struct format_field *field)
6247{
6248 free(field->type);
6249 free(field->name);
6250 free(field);
6251}
6252
Steven Rostedtf7d82352012-04-06 00:47:53 +02006253static void free_format_fields(struct format_field *field)
6254{
6255 struct format_field *next;
6256
6257 while (field) {
6258 next = field->next;
David Ahern00ae1122015-03-19 12:36:21 -06006259 pevent_free_format_field(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006260 field = next;
6261 }
6262}
6263
6264static void free_formats(struct format *format)
6265{
6266 free_format_fields(format->common_fields);
6267 free_format_fields(format->fields);
6268}
6269
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006270void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006271{
6272 free(event->name);
6273 free(event->system);
6274
6275 free_formats(&event->format);
6276
6277 free(event->print_fmt.format);
6278 free_args(event->print_fmt.args);
6279
6280 free(event);
6281}
6282
6283/**
6284 * pevent_free - free a pevent handle
6285 * @pevent: the pevent handle to free
6286 */
6287void pevent_free(struct pevent *pevent)
6288{
Steven Rostedta2525a02012-04-06 00:48:02 +02006289 struct cmdline_list *cmdlist, *cmdnext;
6290 struct func_list *funclist, *funcnext;
6291 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006292 struct pevent_function_handler *func_handler;
6293 struct event_handler *handle;
6294 int i;
6295
Steven Rostedta2525a02012-04-06 00:48:02 +02006296 if (!pevent)
6297 return;
6298
6299 cmdlist = pevent->cmdlist;
6300 funclist = pevent->funclist;
6301 printklist = pevent->printklist;
6302
Steven Rostedtf7d82352012-04-06 00:47:53 +02006303 pevent->ref_count--;
6304 if (pevent->ref_count)
6305 return;
6306
6307 if (pevent->cmdlines) {
6308 for (i = 0; i < pevent->cmdline_count; i++)
6309 free(pevent->cmdlines[i].comm);
6310 free(pevent->cmdlines);
6311 }
6312
6313 while (cmdlist) {
6314 cmdnext = cmdlist->next;
6315 free(cmdlist->comm);
6316 free(cmdlist);
6317 cmdlist = cmdnext;
6318 }
6319
6320 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006321 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02006322 free(pevent->func_map[i].func);
6323 free(pevent->func_map[i].mod);
6324 }
6325 free(pevent->func_map);
6326 }
6327
6328 while (funclist) {
6329 funcnext = funclist->next;
6330 free(funclist->func);
6331 free(funclist->mod);
6332 free(funclist);
6333 funclist = funcnext;
6334 }
6335
6336 while (pevent->func_handlers) {
6337 func_handler = pevent->func_handlers;
6338 pevent->func_handlers = func_handler->next;
6339 free_func_handle(func_handler);
6340 }
6341
6342 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006343 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006344 free(pevent->printk_map[i].printk);
6345 free(pevent->printk_map);
6346 }
6347
6348 while (printklist) {
6349 printknext = printklist->next;
6350 free(printklist->printk);
6351 free(printklist);
6352 printklist = printknext;
6353 }
6354
6355 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006356 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006357
6358 while (pevent->handlers) {
6359 handle = pevent->handlers;
6360 pevent->handlers = handle->next;
6361 free_handler(handle);
6362 }
6363
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -04006364 free(pevent->trace_clock);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006365 free(pevent->events);
6366 free(pevent->sort_events);
6367
6368 free(pevent);
6369}
6370
6371void pevent_unref(struct pevent *pevent)
6372{
6373 pevent_free(pevent);
6374}