blob: b6d11eea8a57a35cfb3d072ae172b4dbe479aa2f [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;
Javi Merinob839e1e82015-03-24 11:07:19 +0000769 case PRINT_INT_ARRAY:
770 free_arg(arg->int_array.field);
771 free_arg(arg->int_array.count);
772 free_arg(arg->int_array.el_size);
773 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200774 case PRINT_TYPE:
775 free(arg->typecast.type);
776 free_arg(arg->typecast.item);
777 break;
778 case PRINT_STRING:
779 case PRINT_BSTRING:
780 free(arg->string.string);
781 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400782 case PRINT_BITMASK:
783 free(arg->bitmask.bitmask);
784 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200785 case PRINT_DYNAMIC_ARRAY:
786 free(arg->dynarray.index);
787 break;
788 case PRINT_OP:
789 free(arg->op.op);
790 free_arg(arg->op.left);
791 free_arg(arg->op.right);
792 break;
793 case PRINT_FUNC:
794 while (arg->func.args) {
795 farg = arg->func.args;
796 arg->func.args = farg->next;
797 free_arg(farg);
798 }
799 break;
800
801 case PRINT_NULL:
802 default:
803 break;
804 }
805
806 free(arg);
807}
808
809static enum event_type get_type(int ch)
810{
811 if (ch == '\n')
812 return EVENT_NEWLINE;
813 if (isspace(ch))
814 return EVENT_SPACE;
815 if (isalnum(ch) || ch == '_')
816 return EVENT_ITEM;
817 if (ch == '\'')
818 return EVENT_SQUOTE;
819 if (ch == '"')
820 return EVENT_DQUOTE;
821 if (!isprint(ch))
822 return EVENT_NONE;
823 if (ch == '(' || ch == ')' || ch == ',')
824 return EVENT_DELIM;
825
826 return EVENT_OP;
827}
828
829static int __read_char(void)
830{
831 if (input_buf_ptr >= input_buf_siz)
832 return -1;
833
834 return input_buf[input_buf_ptr++];
835}
836
837static int __peek_char(void)
838{
839 if (input_buf_ptr >= input_buf_siz)
840 return -1;
841
842 return input_buf[input_buf_ptr];
843}
844
845/**
846 * pevent_peek_char - peek at the next character that will be read
847 *
848 * Returns the next character read, or -1 if end of buffer.
849 */
850int pevent_peek_char(void)
851{
852 return __peek_char();
853}
854
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900855static int extend_token(char **tok, char *buf, int size)
856{
857 char *newtok = realloc(*tok, size);
858
859 if (!newtok) {
860 free(*tok);
861 *tok = NULL;
862 return -1;
863 }
864
865 if (!*tok)
866 strcpy(newtok, buf);
867 else
868 strcat(newtok, buf);
869 *tok = newtok;
870
871 return 0;
872}
873
Steven Rostedtf7d82352012-04-06 00:47:53 +0200874static enum event_type force_token(const char *str, char **tok);
875
876static enum event_type __read_token(char **tok)
877{
878 char buf[BUFSIZ];
879 int ch, last_ch, quote_ch, next_ch;
880 int i = 0;
881 int tok_size = 0;
882 enum event_type type;
883
884 *tok = NULL;
885
886
887 ch = __read_char();
888 if (ch < 0)
889 return EVENT_NONE;
890
891 type = get_type(ch);
892 if (type == EVENT_NONE)
893 return type;
894
895 buf[i++] = ch;
896
897 switch (type) {
898 case EVENT_NEWLINE:
899 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300900 if (asprintf(tok, "%c", ch) < 0)
901 return EVENT_ERROR;
902
Steven Rostedtf7d82352012-04-06 00:47:53 +0200903 return type;
904
905 case EVENT_OP:
906 switch (ch) {
907 case '-':
908 next_ch = __peek_char();
909 if (next_ch == '>') {
910 buf[i++] = __read_char();
911 break;
912 }
913 /* fall through */
914 case '+':
915 case '|':
916 case '&':
917 case '>':
918 case '<':
919 last_ch = ch;
920 ch = __peek_char();
921 if (ch != last_ch)
922 goto test_equal;
923 buf[i++] = __read_char();
924 switch (last_ch) {
925 case '>':
926 case '<':
927 goto test_equal;
928 default:
929 break;
930 }
931 break;
932 case '!':
933 case '=':
934 goto test_equal;
935 default: /* what should we do instead? */
936 break;
937 }
938 buf[i] = 0;
939 *tok = strdup(buf);
940 return type;
941
942 test_equal:
943 ch = __peek_char();
944 if (ch == '=')
945 buf[i++] = __read_char();
946 goto out;
947
948 case EVENT_DQUOTE:
949 case EVENT_SQUOTE:
950 /* don't keep quotes */
951 i--;
952 quote_ch = ch;
953 last_ch = 0;
954 concat:
955 do {
956 if (i == (BUFSIZ - 1)) {
957 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200958 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900959
960 if (extend_token(tok, buf, tok_size) < 0)
961 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200962 i = 0;
963 }
964 last_ch = ch;
965 ch = __read_char();
966 buf[i++] = ch;
967 /* the '\' '\' will cancel itself */
968 if (ch == '\\' && last_ch == '\\')
969 last_ch = 0;
970 } while (ch != quote_ch || last_ch == '\\');
971 /* remove the last quote */
972 i--;
973
974 /*
975 * For strings (double quotes) check the next token.
976 * If it is another string, concatinate the two.
977 */
978 if (type == EVENT_DQUOTE) {
979 unsigned long long save_input_buf_ptr = input_buf_ptr;
980
981 do {
982 ch = __read_char();
983 } while (isspace(ch));
984 if (ch == '"')
985 goto concat;
986 input_buf_ptr = save_input_buf_ptr;
987 }
988
989 goto out;
990
991 case EVENT_ERROR ... EVENT_SPACE:
992 case EVENT_ITEM:
993 default:
994 break;
995 }
996
997 while (get_type(__peek_char()) == type) {
998 if (i == (BUFSIZ - 1)) {
999 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001000 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001001
1002 if (extend_token(tok, buf, tok_size) < 0)
1003 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001004 i = 0;
1005 }
1006 ch = __read_char();
1007 buf[i++] = ch;
1008 }
1009
1010 out:
1011 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001012 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001013 return EVENT_NONE;
1014
1015 if (type == EVENT_ITEM) {
1016 /*
1017 * Older versions of the kernel has a bug that
1018 * creates invalid symbols and will break the mac80211
1019 * parsing. This is a work around to that bug.
1020 *
1021 * See Linux kernel commit:
1022 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1023 */
1024 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1025 free(*tok);
1026 *tok = NULL;
1027 return force_token("\"\%s\" ", tok);
1028 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1029 free(*tok);
1030 *tok = NULL;
1031 return force_token("\" sta:%pM\" ", tok);
1032 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1033 free(*tok);
1034 *tok = NULL;
1035 return force_token("\" vif:%p(%d)\" ", tok);
1036 }
1037 }
1038
1039 return type;
1040}
1041
1042static enum event_type force_token(const char *str, char **tok)
1043{
1044 const char *save_input_buf;
1045 unsigned long long save_input_buf_ptr;
1046 unsigned long long save_input_buf_siz;
1047 enum event_type type;
1048
1049 /* save off the current input pointers */
1050 save_input_buf = input_buf;
1051 save_input_buf_ptr = input_buf_ptr;
1052 save_input_buf_siz = input_buf_siz;
1053
1054 init_input_buf(str, strlen(str));
1055
1056 type = __read_token(tok);
1057
1058 /* reset back to original token */
1059 input_buf = save_input_buf;
1060 input_buf_ptr = save_input_buf_ptr;
1061 input_buf_siz = save_input_buf_siz;
1062
1063 return type;
1064}
1065
1066static void free_token(char *tok)
1067{
1068 if (tok)
1069 free(tok);
1070}
1071
1072static enum event_type read_token(char **tok)
1073{
1074 enum event_type type;
1075
1076 for (;;) {
1077 type = __read_token(tok);
1078 if (type != EVENT_SPACE)
1079 return type;
1080
1081 free_token(*tok);
1082 }
1083
1084 /* not reached */
1085 *tok = NULL;
1086 return EVENT_NONE;
1087}
1088
1089/**
1090 * pevent_read_token - access to utilites to use the pevent parser
1091 * @tok: The token to return
1092 *
1093 * This will parse tokens from the string given by
1094 * pevent_init_data().
1095 *
1096 * Returns the token type.
1097 */
1098enum event_type pevent_read_token(char **tok)
1099{
1100 return read_token(tok);
1101}
1102
1103/**
1104 * pevent_free_token - free a token returned by pevent_read_token
1105 * @token: the token to free
1106 */
1107void pevent_free_token(char *token)
1108{
1109 free_token(token);
1110}
1111
1112/* no newline */
1113static enum event_type read_token_item(char **tok)
1114{
1115 enum event_type type;
1116
1117 for (;;) {
1118 type = __read_token(tok);
1119 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1120 return type;
1121 free_token(*tok);
1122 *tok = NULL;
1123 }
1124
1125 /* not reached */
1126 *tok = NULL;
1127 return EVENT_NONE;
1128}
1129
1130static int test_type(enum event_type type, enum event_type expect)
1131{
1132 if (type != expect) {
1133 do_warning("Error: expected type %d but read %d",
1134 expect, type);
1135 return -1;
1136 }
1137 return 0;
1138}
1139
1140static int test_type_token(enum event_type type, const char *token,
1141 enum event_type expect, const char *expect_tok)
1142{
1143 if (type != expect) {
1144 do_warning("Error: expected type %d but read %d",
1145 expect, type);
1146 return -1;
1147 }
1148
1149 if (strcmp(token, expect_tok) != 0) {
1150 do_warning("Error: expected '%s' but read '%s'",
1151 expect_tok, token);
1152 return -1;
1153 }
1154 return 0;
1155}
1156
1157static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1158{
1159 enum event_type type;
1160
1161 if (newline_ok)
1162 type = read_token(tok);
1163 else
1164 type = read_token_item(tok);
1165 return test_type(type, expect);
1166}
1167
1168static int read_expect_type(enum event_type expect, char **tok)
1169{
1170 return __read_expect_type(expect, tok, 1);
1171}
1172
1173static int __read_expected(enum event_type expect, const char *str,
1174 int newline_ok)
1175{
1176 enum event_type type;
1177 char *token;
1178 int ret;
1179
1180 if (newline_ok)
1181 type = read_token(&token);
1182 else
1183 type = read_token_item(&token);
1184
1185 ret = test_type_token(type, token, expect, str);
1186
1187 free_token(token);
1188
1189 return ret;
1190}
1191
1192static int read_expected(enum event_type expect, const char *str)
1193{
1194 return __read_expected(expect, str, 1);
1195}
1196
1197static int read_expected_item(enum event_type expect, const char *str)
1198{
1199 return __read_expected(expect, str, 0);
1200}
1201
1202static char *event_read_name(void)
1203{
1204 char *token;
1205
1206 if (read_expected(EVENT_ITEM, "name") < 0)
1207 return NULL;
1208
1209 if (read_expected(EVENT_OP, ":") < 0)
1210 return NULL;
1211
1212 if (read_expect_type(EVENT_ITEM, &token) < 0)
1213 goto fail;
1214
1215 return token;
1216
1217 fail:
1218 free_token(token);
1219 return NULL;
1220}
1221
1222static int event_read_id(void)
1223{
1224 char *token;
1225 int id;
1226
1227 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1228 return -1;
1229
1230 if (read_expected(EVENT_OP, ":") < 0)
1231 return -1;
1232
1233 if (read_expect_type(EVENT_ITEM, &token) < 0)
1234 goto fail;
1235
1236 id = strtoul(token, NULL, 0);
1237 free_token(token);
1238 return id;
1239
1240 fail:
1241 free_token(token);
1242 return -1;
1243}
1244
1245static int field_is_string(struct format_field *field)
1246{
1247 if ((field->flags & FIELD_IS_ARRAY) &&
1248 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1249 strstr(field->type, "s8")))
1250 return 1;
1251
1252 return 0;
1253}
1254
1255static int field_is_dynamic(struct format_field *field)
1256{
1257 if (strncmp(field->type, "__data_loc", 10) == 0)
1258 return 1;
1259
1260 return 0;
1261}
1262
1263static int field_is_long(struct format_field *field)
1264{
1265 /* includes long long */
1266 if (strstr(field->type, "long"))
1267 return 1;
1268
1269 return 0;
1270}
1271
Jiri Olsae23c1a52013-01-24 21:46:43 +01001272static unsigned int type_size(const char *name)
1273{
1274 /* This covers all FIELD_IS_STRING types. */
1275 static struct {
1276 const char *type;
1277 unsigned int size;
1278 } table[] = {
1279 { "u8", 1 },
1280 { "u16", 2 },
1281 { "u32", 4 },
1282 { "u64", 8 },
1283 { "s8", 1 },
1284 { "s16", 2 },
1285 { "s32", 4 },
1286 { "s64", 8 },
1287 { "char", 1 },
1288 { },
1289 };
1290 int i;
1291
1292 for (i = 0; table[i].type; i++) {
1293 if (!strcmp(table[i].type, name))
1294 return table[i].size;
1295 }
1296
1297 return 0;
1298}
1299
Steven Rostedtf7d82352012-04-06 00:47:53 +02001300static int event_read_fields(struct event_format *event, struct format_field **fields)
1301{
1302 struct format_field *field = NULL;
1303 enum event_type type;
1304 char *token;
1305 char *last_token;
1306 int count = 0;
1307
1308 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001309 unsigned int size_dynamic = 0;
1310
Steven Rostedtf7d82352012-04-06 00:47:53 +02001311 type = read_token(&token);
1312 if (type == EVENT_NEWLINE) {
1313 free_token(token);
1314 return count;
1315 }
1316
1317 count++;
1318
1319 if (test_type_token(type, token, EVENT_ITEM, "field"))
1320 goto fail;
1321 free_token(token);
1322
1323 type = read_token(&token);
1324 /*
1325 * The ftrace fields may still use the "special" name.
1326 * Just ignore it.
1327 */
1328 if (event->flags & EVENT_FL_ISFTRACE &&
1329 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1330 free_token(token);
1331 type = read_token(&token);
1332 }
1333
1334 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1335 goto fail;
1336
1337 free_token(token);
1338 if (read_expect_type(EVENT_ITEM, &token) < 0)
1339 goto fail;
1340
1341 last_token = token;
1342
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001343 field = calloc(1, sizeof(*field));
1344 if (!field)
1345 goto fail;
1346
Steven Rostedtf7d82352012-04-06 00:47:53 +02001347 field->event = event;
1348
1349 /* read the rest of the type */
1350 for (;;) {
1351 type = read_token(&token);
1352 if (type == EVENT_ITEM ||
1353 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1354 /*
1355 * Some of the ftrace fields are broken and have
1356 * an illegal "." in them.
1357 */
1358 (event->flags & EVENT_FL_ISFTRACE &&
1359 type == EVENT_OP && strcmp(token, ".") == 0)) {
1360
1361 if (strcmp(token, "*") == 0)
1362 field->flags |= FIELD_IS_POINTER;
1363
1364 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001365 char *new_type;
1366 new_type = realloc(field->type,
1367 strlen(field->type) +
1368 strlen(last_token) + 2);
1369 if (!new_type) {
1370 free(last_token);
1371 goto fail;
1372 }
1373 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001374 strcat(field->type, " ");
1375 strcat(field->type, last_token);
1376 free(last_token);
1377 } else
1378 field->type = last_token;
1379 last_token = token;
1380 continue;
1381 }
1382
1383 break;
1384 }
1385
1386 if (!field->type) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001387 do_warning_event(event, "%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001388 goto fail;
1389 }
1390 field->name = last_token;
1391
1392 if (test_type(type, EVENT_OP))
1393 goto fail;
1394
1395 if (strcmp(token, "[") == 0) {
1396 enum event_type last_type = type;
1397 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001398 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001399 int len;
1400
1401 field->flags |= FIELD_IS_ARRAY;
1402
1403 type = read_token(&token);
1404
1405 if (type == EVENT_ITEM)
1406 field->arraylen = strtoul(token, NULL, 0);
1407 else
1408 field->arraylen = 0;
1409
1410 while (strcmp(token, "]") != 0) {
1411 if (last_type == EVENT_ITEM &&
1412 type == EVENT_ITEM)
1413 len = 2;
1414 else
1415 len = 1;
1416 last_type = type;
1417
Namhyung Kimd2864472012-04-09 11:54:33 +09001418 new_brackets = realloc(brackets,
1419 strlen(brackets) +
1420 strlen(token) + len);
1421 if (!new_brackets) {
1422 free(brackets);
1423 goto fail;
1424 }
1425 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001426 if (len == 2)
1427 strcat(brackets, " ");
1428 strcat(brackets, token);
1429 /* We only care about the last token */
1430 field->arraylen = strtoul(token, NULL, 0);
1431 free_token(token);
1432 type = read_token(&token);
1433 if (type == EVENT_NONE) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001434 do_warning_event(event, "failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001435 goto fail;
1436 }
1437 }
1438
1439 free_token(token);
1440
Namhyung Kimd2864472012-04-09 11:54:33 +09001441 new_brackets = realloc(brackets, strlen(brackets) + 2);
1442 if (!new_brackets) {
1443 free(brackets);
1444 goto fail;
1445 }
1446 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001447 strcat(brackets, "]");
1448
1449 /* add brackets to type */
1450
1451 type = read_token(&token);
1452 /*
1453 * If the next token is not an OP, then it is of
1454 * the format: type [] item;
1455 */
1456 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001457 char *new_type;
1458 new_type = realloc(field->type,
1459 strlen(field->type) +
1460 strlen(field->name) +
1461 strlen(brackets) + 2);
1462 if (!new_type) {
1463 free(brackets);
1464 goto fail;
1465 }
1466 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001467 strcat(field->type, " ");
1468 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001469 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001470 free_token(field->name);
1471 strcat(field->type, brackets);
1472 field->name = token;
1473 type = read_token(&token);
1474 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001475 char *new_type;
1476 new_type = realloc(field->type,
1477 strlen(field->type) +
1478 strlen(brackets) + 1);
1479 if (!new_type) {
1480 free(brackets);
1481 goto fail;
1482 }
1483 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001484 strcat(field->type, brackets);
1485 }
1486 free(brackets);
1487 }
1488
1489 if (field_is_string(field))
1490 field->flags |= FIELD_IS_STRING;
1491 if (field_is_dynamic(field))
1492 field->flags |= FIELD_IS_DYNAMIC;
1493 if (field_is_long(field))
1494 field->flags |= FIELD_IS_LONG;
1495
1496 if (test_type_token(type, token, EVENT_OP, ";"))
1497 goto fail;
1498 free_token(token);
1499
1500 if (read_expected(EVENT_ITEM, "offset") < 0)
1501 goto fail_expect;
1502
1503 if (read_expected(EVENT_OP, ":") < 0)
1504 goto fail_expect;
1505
1506 if (read_expect_type(EVENT_ITEM, &token))
1507 goto fail;
1508 field->offset = strtoul(token, NULL, 0);
1509 free_token(token);
1510
1511 if (read_expected(EVENT_OP, ";") < 0)
1512 goto fail_expect;
1513
1514 if (read_expected(EVENT_ITEM, "size") < 0)
1515 goto fail_expect;
1516
1517 if (read_expected(EVENT_OP, ":") < 0)
1518 goto fail_expect;
1519
1520 if (read_expect_type(EVENT_ITEM, &token))
1521 goto fail;
1522 field->size = strtoul(token, NULL, 0);
1523 free_token(token);
1524
1525 if (read_expected(EVENT_OP, ";") < 0)
1526 goto fail_expect;
1527
1528 type = read_token(&token);
1529 if (type != EVENT_NEWLINE) {
1530 /* newer versions of the kernel have a "signed" type */
1531 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1532 goto fail;
1533
1534 free_token(token);
1535
1536 if (read_expected(EVENT_OP, ":") < 0)
1537 goto fail_expect;
1538
1539 if (read_expect_type(EVENT_ITEM, &token))
1540 goto fail;
1541
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001542 if (strtoul(token, NULL, 0))
1543 field->flags |= FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001544
1545 free_token(token);
1546 if (read_expected(EVENT_OP, ";") < 0)
1547 goto fail_expect;
1548
1549 if (read_expect_type(EVENT_NEWLINE, &token))
1550 goto fail;
1551 }
1552
1553 free_token(token);
1554
1555 if (field->flags & FIELD_IS_ARRAY) {
1556 if (field->arraylen)
1557 field->elementsize = field->size / field->arraylen;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001558 else if (field->flags & FIELD_IS_DYNAMIC)
1559 field->elementsize = size_dynamic;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001560 else if (field->flags & FIELD_IS_STRING)
1561 field->elementsize = 1;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001562 else if (field->flags & FIELD_IS_LONG)
1563 field->elementsize = event->pevent ?
1564 event->pevent->long_size :
1565 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001566 } else
1567 field->elementsize = field->size;
1568
1569 *fields = field;
1570 fields = &field->next;
1571
1572 } while (1);
1573
1574 return 0;
1575
1576fail:
1577 free_token(token);
1578fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001579 if (field) {
1580 free(field->type);
1581 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001582 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001583 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001584 return -1;
1585}
1586
1587static int event_read_format(struct event_format *event)
1588{
1589 char *token;
1590 int ret;
1591
1592 if (read_expected_item(EVENT_ITEM, "format") < 0)
1593 return -1;
1594
1595 if (read_expected(EVENT_OP, ":") < 0)
1596 return -1;
1597
1598 if (read_expect_type(EVENT_NEWLINE, &token))
1599 goto fail;
1600 free_token(token);
1601
1602 ret = event_read_fields(event, &event->format.common_fields);
1603 if (ret < 0)
1604 return ret;
1605 event->format.nr_common = ret;
1606
1607 ret = event_read_fields(event, &event->format.fields);
1608 if (ret < 0)
1609 return ret;
1610 event->format.nr_fields = ret;
1611
1612 return 0;
1613
1614 fail:
1615 free_token(token);
1616 return -1;
1617}
1618
1619static enum event_type
1620process_arg_token(struct event_format *event, struct print_arg *arg,
1621 char **tok, enum event_type type);
1622
1623static enum event_type
1624process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1625{
1626 enum event_type type;
1627 char *token;
1628
1629 type = read_token(&token);
1630 *tok = token;
1631
1632 return process_arg_token(event, arg, tok, type);
1633}
1634
1635static enum event_type
1636process_op(struct event_format *event, struct print_arg *arg, char **tok);
1637
Steven Rostedteff2c922013-11-18 14:23:14 -05001638/*
1639 * For __print_symbolic() and __print_flags, we need to completely
1640 * evaluate the first argument, which defines what to print next.
1641 */
1642static enum event_type
1643process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1644{
1645 enum event_type type;
1646
1647 type = process_arg(event, arg, tok);
1648
1649 while (type == EVENT_OP) {
1650 type = process_op(event, arg, tok);
1651 }
1652
1653 return type;
1654}
1655
Steven Rostedtf7d82352012-04-06 00:47:53 +02001656static enum event_type
1657process_cond(struct event_format *event, struct print_arg *top, char **tok)
1658{
1659 struct print_arg *arg, *left, *right;
1660 enum event_type type;
1661 char *token = NULL;
1662
1663 arg = alloc_arg();
1664 left = alloc_arg();
1665 right = alloc_arg();
1666
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001667 if (!arg || !left || !right) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001668 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001669 /* arg will be freed at out_free */
1670 free_arg(left);
1671 free_arg(right);
1672 goto out_free;
1673 }
1674
Steven Rostedtf7d82352012-04-06 00:47:53 +02001675 arg->type = PRINT_OP;
1676 arg->op.left = left;
1677 arg->op.right = right;
1678
1679 *tok = NULL;
1680 type = process_arg(event, left, &token);
1681
1682 again:
1683 /* Handle other operations in the arguments */
1684 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1685 type = process_op(event, left, &token);
1686 goto again;
1687 }
1688
1689 if (test_type_token(type, token, EVENT_OP, ":"))
1690 goto out_free;
1691
1692 arg->op.op = token;
1693
1694 type = process_arg(event, right, &token);
1695
1696 top->op.right = arg;
1697
1698 *tok = token;
1699 return type;
1700
1701out_free:
1702 /* Top may point to itself */
1703 top->op.right = NULL;
1704 free_token(token);
1705 free_arg(arg);
1706 return EVENT_ERROR;
1707}
1708
1709static enum event_type
1710process_array(struct event_format *event, struct print_arg *top, char **tok)
1711{
1712 struct print_arg *arg;
1713 enum event_type type;
1714 char *token = NULL;
1715
1716 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001717 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001718 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001719 /* '*tok' is set to top->op.op. No need to free. */
1720 *tok = NULL;
1721 return EVENT_ERROR;
1722 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001723
1724 *tok = NULL;
1725 type = process_arg(event, arg, &token);
1726 if (test_type_token(type, token, EVENT_OP, "]"))
1727 goto out_free;
1728
1729 top->op.right = arg;
1730
1731 free_token(token);
1732 type = read_token_item(&token);
1733 *tok = token;
1734
1735 return type;
1736
1737out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001738 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001739 free_arg(arg);
1740 return EVENT_ERROR;
1741}
1742
1743static int get_op_prio(char *op)
1744{
1745 if (!op[1]) {
1746 switch (op[0]) {
1747 case '~':
1748 case '!':
1749 return 4;
1750 case '*':
1751 case '/':
1752 case '%':
1753 return 6;
1754 case '+':
1755 case '-':
1756 return 7;
1757 /* '>>' and '<<' are 8 */
1758 case '<':
1759 case '>':
1760 return 9;
1761 /* '==' and '!=' are 10 */
1762 case '&':
1763 return 11;
1764 case '^':
1765 return 12;
1766 case '|':
1767 return 13;
1768 case '?':
1769 return 16;
1770 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001771 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001772 return -1;
1773 }
1774 } else {
1775 if (strcmp(op, "++") == 0 ||
1776 strcmp(op, "--") == 0) {
1777 return 3;
1778 } else if (strcmp(op, ">>") == 0 ||
1779 strcmp(op, "<<") == 0) {
1780 return 8;
1781 } else if (strcmp(op, ">=") == 0 ||
1782 strcmp(op, "<=") == 0) {
1783 return 9;
1784 } else if (strcmp(op, "==") == 0 ||
1785 strcmp(op, "!=") == 0) {
1786 return 10;
1787 } else if (strcmp(op, "&&") == 0) {
1788 return 14;
1789 } else if (strcmp(op, "||") == 0) {
1790 return 15;
1791 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001792 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001793 return -1;
1794 }
1795 }
1796}
1797
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001798static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001799{
1800
1801 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001802 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001803 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001804 else
1805 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001806
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001807 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001808}
1809
1810/* Note, *tok does not get freed, but will most likely be saved */
1811static enum event_type
1812process_op(struct event_format *event, struct print_arg *arg, char **tok)
1813{
1814 struct print_arg *left, *right = NULL;
1815 enum event_type type;
1816 char *token;
1817
1818 /* the op is passed in via tok */
1819 token = *tok;
1820
1821 if (arg->type == PRINT_OP && !arg->op.left) {
1822 /* handle single op */
1823 if (token[1]) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001824 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001825 goto out_free;
1826 }
1827 switch (token[0]) {
1828 case '~':
1829 case '!':
1830 case '+':
1831 case '-':
1832 break;
1833 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001834 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001835 goto out_free;
1836
1837 }
1838
1839 /* make an empty left */
1840 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001841 if (!left)
1842 goto out_warn_free;
1843
Steven Rostedtf7d82352012-04-06 00:47:53 +02001844 left->type = PRINT_NULL;
1845 arg->op.left = left;
1846
1847 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001848 if (!right)
1849 goto out_warn_free;
1850
Steven Rostedtf7d82352012-04-06 00:47:53 +02001851 arg->op.right = right;
1852
1853 /* do not free the token, it belongs to an op */
1854 *tok = NULL;
1855 type = process_arg(event, right, tok);
1856
1857 } else if (strcmp(token, "?") == 0) {
1858
1859 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001860 if (!left)
1861 goto out_warn_free;
1862
Steven Rostedtf7d82352012-04-06 00:47:53 +02001863 /* copy the top arg to the left */
1864 *left = *arg;
1865
1866 arg->type = PRINT_OP;
1867 arg->op.op = token;
1868 arg->op.left = left;
1869 arg->op.prio = 0;
1870
Namhyung Kim41e51a22012-09-19 15:58:42 +09001871 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001872 type = process_cond(event, arg, tok);
1873
1874 } else if (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 ||
1882 strcmp(token, "*") == 0 ||
1883 strcmp(token, "^") == 0 ||
1884 strcmp(token, "/") == 0 ||
1885 strcmp(token, "<") == 0 ||
1886 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001887 strcmp(token, "<=") == 0 ||
1888 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001889 strcmp(token, "==") == 0 ||
1890 strcmp(token, "!=") == 0) {
1891
1892 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001893 if (!left)
1894 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001895
1896 /* copy the top arg to the left */
1897 *left = *arg;
1898
1899 arg->type = PRINT_OP;
1900 arg->op.op = token;
1901 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001902 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001903
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001904 if (set_op_prio(arg) == -1) {
1905 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001906 /* arg->op.op (= token) will be freed at out_free */
1907 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001908 goto out_free;
1909 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001910
1911 type = read_token_item(&token);
1912 *tok = token;
1913
1914 /* could just be a type pointer */
1915 if ((strcmp(arg->op.op, "*") == 0) &&
1916 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001917 char *new_atom;
1918
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001919 if (left->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001920 do_warning_event(event, "bad pointer type");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001921 goto out_free;
1922 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001923 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001924 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001925 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001926 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001927
1928 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001929 strcat(left->atom.atom, " *");
1930 free(arg->op.op);
1931 *arg = *left;
1932 free(left);
1933
1934 return type;
1935 }
1936
1937 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001938 if (!right)
1939 goto out_warn_free;
1940
Steven Rostedtf7d82352012-04-06 00:47:53 +02001941 type = process_arg_token(event, right, tok, type);
1942 arg->op.right = right;
1943
1944 } else if (strcmp(token, "[") == 0) {
1945
1946 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001947 if (!left)
1948 goto out_warn_free;
1949
Steven Rostedtf7d82352012-04-06 00:47:53 +02001950 *left = *arg;
1951
1952 arg->type = PRINT_OP;
1953 arg->op.op = token;
1954 arg->op.left = left;
1955
1956 arg->op.prio = 0;
1957
Namhyung Kim41e51a22012-09-19 15:58:42 +09001958 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001959 type = process_array(event, arg, tok);
1960
1961 } else {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001962 do_warning_event(event, "unknown op '%s'", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001963 event->flags |= EVENT_FL_FAILED;
1964 /* the arg is now the left side */
1965 goto out_free;
1966 }
1967
1968 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1969 int prio;
1970
1971 /* higher prios need to be closer to the root */
1972 prio = get_op_prio(*tok);
1973
1974 if (prio > arg->op.prio)
1975 return process_op(event, arg, tok);
1976
1977 return process_op(event, right, tok);
1978 }
1979
1980 return type;
1981
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001982out_warn_free:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001983 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001984out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001985 free_token(token);
1986 *tok = NULL;
1987 return EVENT_ERROR;
1988}
1989
1990static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001991process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001992 char **tok)
1993{
1994 enum event_type type;
1995 char *field;
1996 char *token;
1997
1998 if (read_expected(EVENT_OP, "->") < 0)
1999 goto out_err;
2000
2001 if (read_expect_type(EVENT_ITEM, &token) < 0)
2002 goto out_free;
2003 field = token;
2004
2005 arg->type = PRINT_FIELD;
2006 arg->field.name = field;
2007
Tom Zanussi5205aec2012-04-06 00:47:58 +02002008 if (is_flag_field) {
2009 arg->field.field = pevent_find_any_field(event, arg->field.name);
2010 arg->field.field->flags |= FIELD_IS_FLAG;
2011 is_flag_field = 0;
2012 } else if (is_symbolic_field) {
2013 arg->field.field = pevent_find_any_field(event, arg->field.name);
2014 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2015 is_symbolic_field = 0;
2016 }
2017
Steven Rostedtf7d82352012-04-06 00:47:53 +02002018 type = read_token(&token);
2019 *tok = token;
2020
2021 return type;
2022
2023 out_free:
2024 free_token(token);
2025 out_err:
2026 *tok = NULL;
2027 return EVENT_ERROR;
2028}
2029
Javi Merino929a6bb2015-03-20 18:12:55 +00002030static int alloc_and_process_delim(struct event_format *event, char *next_token,
2031 struct print_arg **print_arg)
2032{
2033 struct print_arg *field;
2034 enum event_type type;
2035 char *token;
2036 int ret = 0;
2037
2038 field = alloc_arg();
2039 if (!field) {
2040 do_warning_event(event, "%s: not enough memory!", __func__);
2041 errno = ENOMEM;
2042 return -1;
2043 }
2044
2045 type = process_arg(event, field, &token);
2046
2047 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2048 errno = EINVAL;
2049 ret = -1;
2050 free_arg(field);
2051 goto out_free_token;
2052 }
2053
2054 *print_arg = field;
2055
2056out_free_token:
2057 free_token(token);
2058
2059 return ret;
2060}
2061
Steven Rostedtf7d82352012-04-06 00:47:53 +02002062static char *arg_eval (struct print_arg *arg);
2063
2064static unsigned long long
2065eval_type_str(unsigned long long val, const char *type, int pointer)
2066{
2067 int sign = 0;
2068 char *ref;
2069 int len;
2070
2071 len = strlen(type);
2072
2073 if (pointer) {
2074
2075 if (type[len-1] != '*') {
2076 do_warning("pointer expected with non pointer type");
2077 return val;
2078 }
2079
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002080 ref = malloc(len);
2081 if (!ref) {
2082 do_warning("%s: not enough memory!", __func__);
2083 return val;
2084 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002085 memcpy(ref, type, len);
2086
2087 /* chop off the " *" */
2088 ref[len - 2] = 0;
2089
2090 val = eval_type_str(val, ref, 0);
2091 free(ref);
2092 return val;
2093 }
2094
2095 /* check if this is a pointer */
2096 if (type[len - 1] == '*')
2097 return val;
2098
2099 /* Try to figure out the arg size*/
2100 if (strncmp(type, "struct", 6) == 0)
2101 /* all bets off */
2102 return val;
2103
2104 if (strcmp(type, "u8") == 0)
2105 return val & 0xff;
2106
2107 if (strcmp(type, "u16") == 0)
2108 return val & 0xffff;
2109
2110 if (strcmp(type, "u32") == 0)
2111 return val & 0xffffffff;
2112
2113 if (strcmp(type, "u64") == 0 ||
2114 strcmp(type, "s64"))
2115 return val;
2116
2117 if (strcmp(type, "s8") == 0)
2118 return (unsigned long long)(char)val & 0xff;
2119
2120 if (strcmp(type, "s16") == 0)
2121 return (unsigned long long)(short)val & 0xffff;
2122
2123 if (strcmp(type, "s32") == 0)
2124 return (unsigned long long)(int)val & 0xffffffff;
2125
2126 if (strncmp(type, "unsigned ", 9) == 0) {
2127 sign = 0;
2128 type += 9;
2129 }
2130
2131 if (strcmp(type, "char") == 0) {
2132 if (sign)
2133 return (unsigned long long)(char)val & 0xff;
2134 else
2135 return val & 0xff;
2136 }
2137
2138 if (strcmp(type, "short") == 0) {
2139 if (sign)
2140 return (unsigned long long)(short)val & 0xffff;
2141 else
2142 return val & 0xffff;
2143 }
2144
2145 if (strcmp(type, "int") == 0) {
2146 if (sign)
2147 return (unsigned long long)(int)val & 0xffffffff;
2148 else
2149 return val & 0xffffffff;
2150 }
2151
2152 return val;
2153}
2154
2155/*
2156 * Try to figure out the type.
2157 */
2158static unsigned long long
2159eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2160{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002161 if (arg->type != PRINT_TYPE) {
2162 do_warning("expected type argument");
2163 return 0;
2164 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002165
2166 return eval_type_str(val, arg->typecast.type, pointer);
2167}
2168
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002169static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002170{
2171 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002172 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002173
2174 switch (arg->type) {
2175 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002176 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002177 break;
2178 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002179 ret = arg_num_eval(arg->typecast.item, val);
2180 if (!ret)
2181 break;
2182 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002183 break;
2184 case PRINT_OP:
2185 switch (arg->op.op[0]) {
2186 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002187 ret = arg_num_eval(arg->op.left, &left);
2188 if (!ret)
2189 break;
2190 ret = arg_num_eval(arg->op.right, &right);
2191 if (!ret)
2192 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002193 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002194 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002195 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002196 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002197 break;
2198 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002199 ret = arg_num_eval(arg->op.left, &left);
2200 if (!ret)
2201 break;
2202 ret = arg_num_eval(arg->op.right, &right);
2203 if (!ret)
2204 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002205 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002206 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002207 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002208 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002209 break;
2210 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002211 ret = arg_num_eval(arg->op.left, &left);
2212 if (!ret)
2213 break;
2214 ret = arg_num_eval(arg->op.right, &right);
2215 if (!ret)
2216 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002217 switch (arg->op.op[1]) {
2218 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002219 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002220 break;
2221 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002222 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002223 break;
2224 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002225 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002226 break;
2227 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002228 do_warning("unknown op '%s'", arg->op.op);
2229 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002230 }
2231 break;
2232 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002233 ret = arg_num_eval(arg->op.left, &left);
2234 if (!ret)
2235 break;
2236 ret = arg_num_eval(arg->op.right, &right);
2237 if (!ret)
2238 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002239 switch (arg->op.op[1]) {
2240 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002241 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002242 break;
2243 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002244 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002245 break;
2246 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002247 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002248 break;
2249 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002250 do_warning("unknown op '%s'", arg->op.op);
2251 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002252 }
2253 break;
2254 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002255 ret = arg_num_eval(arg->op.left, &left);
2256 if (!ret)
2257 break;
2258 ret = arg_num_eval(arg->op.right, &right);
2259 if (!ret)
2260 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002261
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002262 if (arg->op.op[1] != '=') {
2263 do_warning("unknown op '%s'", arg->op.op);
2264 ret = 0;
2265 } else
2266 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002267 break;
2268 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002269 ret = arg_num_eval(arg->op.left, &left);
2270 if (!ret)
2271 break;
2272 ret = arg_num_eval(arg->op.right, &right);
2273 if (!ret)
2274 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002275
2276 switch (arg->op.op[1]) {
2277 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002278 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002279 break;
2280 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002281 do_warning("unknown op '%s'", arg->op.op);
2282 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002283 }
2284 break;
2285 case '-':
2286 /* check for negative */
2287 if (arg->op.left->type == PRINT_NULL)
2288 left = 0;
2289 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002290 ret = arg_num_eval(arg->op.left, &left);
2291 if (!ret)
2292 break;
2293 ret = arg_num_eval(arg->op.right, &right);
2294 if (!ret)
2295 break;
2296 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002297 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002298 case '+':
2299 if (arg->op.left->type == PRINT_NULL)
2300 left = 0;
2301 else
2302 ret = arg_num_eval(arg->op.left, &left);
2303 if (!ret)
2304 break;
2305 ret = arg_num_eval(arg->op.right, &right);
2306 if (!ret)
2307 break;
2308 *val = left + right;
2309 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002310 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002311 do_warning("unknown op '%s'", arg->op.op);
2312 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002313 }
2314 break;
2315
2316 case PRINT_NULL:
2317 case PRINT_FIELD ... PRINT_SYMBOL:
2318 case PRINT_STRING:
2319 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002320 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002321 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002322 do_warning("invalid eval type %d", arg->type);
2323 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002324
2325 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002326 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002327}
2328
2329static char *arg_eval (struct print_arg *arg)
2330{
2331 long long val;
2332 static char buf[20];
2333
2334 switch (arg->type) {
2335 case PRINT_ATOM:
2336 return arg->atom.atom;
2337 case PRINT_TYPE:
2338 return arg_eval(arg->typecast.item);
2339 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002340 if (!arg_num_eval(arg, &val))
2341 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002342 sprintf(buf, "%lld", val);
2343 return buf;
2344
2345 case PRINT_NULL:
2346 case PRINT_FIELD ... PRINT_SYMBOL:
2347 case PRINT_STRING:
2348 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002349 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002350 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002351 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002352 break;
2353 }
2354
2355 return NULL;
2356}
2357
2358static enum event_type
2359process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2360{
2361 enum event_type type;
2362 struct print_arg *arg = NULL;
2363 struct print_flag_sym *field;
2364 char *token = *tok;
2365 char *value;
2366
2367 do {
2368 free_token(token);
2369 type = read_token_item(&token);
2370 if (test_type_token(type, token, EVENT_OP, "{"))
2371 break;
2372
2373 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002374 if (!arg)
2375 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002376
2377 free_token(token);
2378 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002379
2380 if (type == EVENT_OP)
2381 type = process_op(event, arg, &token);
2382
2383 if (type == EVENT_ERROR)
2384 goto out_free;
2385
Steven Rostedtf7d82352012-04-06 00:47:53 +02002386 if (test_type_token(type, token, EVENT_DELIM, ","))
2387 goto out_free;
2388
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002389 field = calloc(1, sizeof(*field));
2390 if (!field)
2391 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002392
2393 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002394 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002395 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002396 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002397 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002398 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002399
2400 free_arg(arg);
2401 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002402 if (!arg)
2403 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002404
2405 free_token(token);
2406 type = process_arg(event, arg, &token);
2407 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002408 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002409
2410 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002411 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002412 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002413 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002414 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002415 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002416 free_arg(arg);
2417 arg = NULL;
2418
2419 *list = field;
2420 list = &field->next;
2421
2422 free_token(token);
2423 type = read_token_item(&token);
2424 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2425
2426 *tok = token;
2427 return type;
2428
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002429out_free_field:
2430 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002431out_free:
2432 free_arg(arg);
2433 free_token(token);
2434 *tok = NULL;
2435
2436 return EVENT_ERROR;
2437}
2438
2439static enum event_type
2440process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2441{
2442 struct print_arg *field;
2443 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002444 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002445
2446 memset(arg, 0, sizeof(*arg));
2447 arg->type = PRINT_FLAGS;
2448
2449 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002450 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002451 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002452 goto out_free;
2453 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002454
Steven Rostedteff2c922013-11-18 14:23:14 -05002455 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002456
2457 /* Handle operations in the first argument */
2458 while (type == EVENT_OP)
2459 type = process_op(event, field, &token);
2460
2461 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002462 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002463 free_token(token);
2464
2465 arg->flags.field = field;
2466
2467 type = read_token_item(&token);
2468 if (event_item_type(type)) {
2469 arg->flags.delim = token;
2470 type = read_token_item(&token);
2471 }
2472
2473 if (test_type_token(type, token, EVENT_DELIM, ","))
2474 goto out_free;
2475
2476 type = process_fields(event, &arg->flags.flags, &token);
2477 if (test_type_token(type, token, EVENT_DELIM, ")"))
2478 goto out_free;
2479
2480 free_token(token);
2481 type = read_token_item(tok);
2482 return type;
2483
Namhyung Kim70d93042012-09-19 15:58:44 +09002484out_free_field:
2485 free_arg(field);
2486out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002487 free_token(token);
2488 *tok = NULL;
2489 return EVENT_ERROR;
2490}
2491
2492static enum event_type
2493process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2494{
2495 struct print_arg *field;
2496 enum event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002497 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002498
2499 memset(arg, 0, sizeof(*arg));
2500 arg->type = PRINT_SYMBOL;
2501
2502 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002503 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002504 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002505 goto out_free;
2506 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002507
Steven Rostedteff2c922013-11-18 14:23:14 -05002508 type = process_field_arg(event, field, &token);
2509
Steven Rostedtf7d82352012-04-06 00:47:53 +02002510 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002511 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002512
2513 arg->symbol.field = field;
2514
2515 type = process_fields(event, &arg->symbol.symbols, &token);
2516 if (test_type_token(type, token, EVENT_DELIM, ")"))
2517 goto out_free;
2518
2519 free_token(token);
2520 type = read_token_item(tok);
2521 return type;
2522
Namhyung Kim70d93042012-09-19 15:58:44 +09002523out_free_field:
2524 free_arg(field);
2525out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002526 free_token(token);
2527 *tok = NULL;
2528 return EVENT_ERROR;
2529}
2530
2531static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002532process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2533{
Namhyung Kime080e6f2012-06-27 09:41:41 +09002534 memset(arg, 0, sizeof(*arg));
2535 arg->type = PRINT_HEX;
2536
Javi Merino929a6bb2015-03-20 18:12:55 +00002537 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2538 goto out;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002539
Javi Merino929a6bb2015-03-20 18:12:55 +00002540 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2541 goto free_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002542
Javi Merino929a6bb2015-03-20 18:12:55 +00002543 return read_token_item(tok);
Namhyung Kime080e6f2012-06-27 09:41:41 +09002544
Javi Merino929a6bb2015-03-20 18:12:55 +00002545free_field:
2546 free_arg(arg->hex.field);
2547out:
Namhyung Kime080e6f2012-06-27 09:41:41 +09002548 *tok = NULL;
2549 return EVENT_ERROR;
2550}
2551
2552static enum event_type
Javi Merinob839e1e82015-03-24 11:07:19 +00002553process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2554{
2555 memset(arg, 0, sizeof(*arg));
2556 arg->type = PRINT_INT_ARRAY;
2557
2558 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2559 goto out;
2560
2561 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2562 goto free_field;
2563
2564 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2565 goto free_size;
2566
2567 return read_token_item(tok);
2568
2569free_size:
2570 free_arg(arg->int_array.count);
2571free_field:
2572 free_arg(arg->int_array.field);
2573out:
2574 *tok = NULL;
2575 return EVENT_ERROR;
2576}
2577
2578static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002579process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2580{
2581 struct format_field *field;
2582 enum event_type type;
2583 char *token;
2584
2585 memset(arg, 0, sizeof(*arg));
2586 arg->type = PRINT_DYNAMIC_ARRAY;
2587
2588 /*
2589 * The item within the parenthesis is another field that holds
2590 * the index into where the array starts.
2591 */
2592 type = read_token(&token);
2593 *tok = token;
2594 if (type != EVENT_ITEM)
2595 goto out_free;
2596
2597 /* Find the field */
2598
2599 field = pevent_find_field(event, token);
2600 if (!field)
2601 goto out_free;
2602
2603 arg->dynarray.field = field;
2604 arg->dynarray.index = 0;
2605
2606 if (read_expected(EVENT_DELIM, ")") < 0)
2607 goto out_free;
2608
2609 free_token(token);
2610 type = read_token_item(&token);
2611 *tok = token;
2612 if (type != EVENT_OP || strcmp(token, "[") != 0)
2613 return type;
2614
2615 free_token(token);
2616 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002617 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002618 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002619 *tok = NULL;
2620 return EVENT_ERROR;
2621 }
2622
Steven Rostedtf7d82352012-04-06 00:47:53 +02002623 type = process_arg(event, arg, &token);
2624 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002625 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002626
2627 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002628 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002629
2630 free_token(token);
2631 type = read_token_item(tok);
2632 return type;
2633
Namhyung Kimb3511d02012-05-23 11:36:50 +09002634 out_free_arg:
2635 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002636 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002637 free_token(token);
2638 *tok = NULL;
2639 return EVENT_ERROR;
2640}
2641
2642static enum event_type
2643process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2644{
2645 struct print_arg *item_arg;
2646 enum event_type type;
2647 char *token;
2648
2649 type = process_arg(event, arg, &token);
2650
2651 if (type == EVENT_ERROR)
2652 goto out_free;
2653
2654 if (type == EVENT_OP)
2655 type = process_op(event, arg, &token);
2656
2657 if (type == EVENT_ERROR)
2658 goto out_free;
2659
2660 if (test_type_token(type, token, EVENT_DELIM, ")"))
2661 goto out_free;
2662
2663 free_token(token);
2664 type = read_token_item(&token);
2665
2666 /*
2667 * If the next token is an item or another open paren, then
2668 * this was a typecast.
2669 */
2670 if (event_item_type(type) ||
2671 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2672
2673 /* make this a typecast and contine */
2674
2675 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002676 if (arg->type != PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002677 do_warning_event(event, "previous needed to be PRINT_ATOM");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002678 goto out_free;
2679 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002680
2681 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002682 if (!item_arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002683 do_warning_event(event, "%s: not enough memory!",
2684 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002685 goto out_free;
2686 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002687
2688 arg->type = PRINT_TYPE;
2689 arg->typecast.type = arg->atom.atom;
2690 arg->typecast.item = item_arg;
2691 type = process_arg_token(event, item_arg, &token, type);
2692
2693 }
2694
2695 *tok = token;
2696 return type;
2697
2698 out_free:
2699 free_token(token);
2700 *tok = NULL;
2701 return EVENT_ERROR;
2702}
2703
2704
2705static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002706process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2707 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002708{
2709 enum event_type type;
2710 char *token;
2711
2712 if (read_expect_type(EVENT_ITEM, &token) < 0)
2713 goto out_free;
2714
2715 arg->type = PRINT_STRING;
2716 arg->string.string = token;
2717 arg->string.offset = -1;
2718
2719 if (read_expected(EVENT_DELIM, ")") < 0)
2720 goto out_err;
2721
2722 type = read_token(&token);
2723 *tok = token;
2724
2725 return type;
2726
2727 out_free:
2728 free_token(token);
2729 out_err:
2730 *tok = NULL;
2731 return EVENT_ERROR;
2732}
2733
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002734static enum event_type
2735process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2736 char **tok)
2737{
2738 enum event_type type;
2739 char *token;
2740
2741 if (read_expect_type(EVENT_ITEM, &token) < 0)
2742 goto out_free;
2743
2744 arg->type = PRINT_BITMASK;
2745 arg->bitmask.bitmask = token;
2746 arg->bitmask.offset = -1;
2747
2748 if (read_expected(EVENT_DELIM, ")") < 0)
2749 goto out_err;
2750
2751 type = read_token(&token);
2752 *tok = token;
2753
2754 return type;
2755
2756 out_free:
2757 free_token(token);
2758 out_err:
2759 *tok = NULL;
2760 return EVENT_ERROR;
2761}
2762
Steven Rostedtf7d82352012-04-06 00:47:53 +02002763static struct pevent_function_handler *
2764find_func_handler(struct pevent *pevent, char *func_name)
2765{
2766 struct pevent_function_handler *func;
2767
Steven Rostedt101782e2012-10-01 20:13:51 -04002768 if (!pevent)
2769 return NULL;
2770
Steven Rostedtf7d82352012-04-06 00:47:53 +02002771 for (func = pevent->func_handlers; func; func = func->next) {
2772 if (strcmp(func->name, func_name) == 0)
2773 break;
2774 }
2775
2776 return func;
2777}
2778
2779static void remove_func_handler(struct pevent *pevent, char *func_name)
2780{
2781 struct pevent_function_handler *func;
2782 struct pevent_function_handler **next;
2783
2784 next = &pevent->func_handlers;
2785 while ((func = *next)) {
2786 if (strcmp(func->name, func_name) == 0) {
2787 *next = func->next;
2788 free_func_handle(func);
2789 break;
2790 }
2791 next = &func->next;
2792 }
2793}
2794
2795static enum event_type
2796process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2797 struct print_arg *arg, char **tok)
2798{
2799 struct print_arg **next_arg;
2800 struct print_arg *farg;
2801 enum event_type type;
2802 char *token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002803 int i;
2804
2805 arg->type = PRINT_FUNC;
2806 arg->func.func = func;
2807
2808 *tok = NULL;
2809
2810 next_arg = &(arg->func.args);
2811 for (i = 0; i < func->nr_args; i++) {
2812 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002813 if (!farg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002814 do_warning_event(event, "%s: not enough memory!",
2815 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002816 return EVENT_ERROR;
2817 }
2818
Steven Rostedtf7d82352012-04-06 00:47:53 +02002819 type = process_arg(event, farg, &token);
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002820 if (i < (func->nr_args - 1)) {
2821 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002822 do_warning_event(event,
2823 "Error: function '%s()' expects %d arguments but event %s only uses %d",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002824 func->name, func->nr_args,
2825 event->name, i + 1);
2826 goto err;
2827 }
2828 } else {
2829 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09002830 do_warning_event(event,
2831 "Error: function '%s()' only expects %d arguments but event %s has more",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002832 func->name, func->nr_args, event->name);
2833 goto err;
2834 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002835 }
2836
2837 *next_arg = farg;
2838 next_arg = &(farg->next);
2839 free_token(token);
2840 }
2841
2842 type = read_token(&token);
2843 *tok = token;
2844
2845 return type;
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05002846
2847err:
2848 free_arg(farg);
2849 free_token(token);
2850 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002851}
2852
2853static enum event_type
2854process_function(struct event_format *event, struct print_arg *arg,
2855 char *token, char **tok)
2856{
2857 struct pevent_function_handler *func;
2858
2859 if (strcmp(token, "__print_flags") == 0) {
2860 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002861 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002862 return process_flags(event, arg, tok);
2863 }
2864 if (strcmp(token, "__print_symbolic") == 0) {
2865 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002866 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002867 return process_symbols(event, arg, tok);
2868 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002869 if (strcmp(token, "__print_hex") == 0) {
2870 free_token(token);
2871 return process_hex(event, arg, tok);
2872 }
Javi Merinob839e1e82015-03-24 11:07:19 +00002873 if (strcmp(token, "__print_array") == 0) {
2874 free_token(token);
2875 return process_int_array(event, arg, tok);
2876 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002877 if (strcmp(token, "__get_str") == 0) {
2878 free_token(token);
2879 return process_str(event, arg, tok);
2880 }
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002881 if (strcmp(token, "__get_bitmask") == 0) {
2882 free_token(token);
2883 return process_bitmask(event, arg, tok);
2884 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002885 if (strcmp(token, "__get_dynamic_array") == 0) {
2886 free_token(token);
2887 return process_dynamic_array(event, arg, tok);
2888 }
2889
2890 func = find_func_handler(event->pevent, token);
2891 if (func) {
2892 free_token(token);
2893 return process_func_handler(event, func, arg, tok);
2894 }
2895
Namhyung Kim3388cc32014-03-19 10:22:53 +09002896 do_warning_event(event, "function %s not defined", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002897 free_token(token);
2898 return EVENT_ERROR;
2899}
2900
2901static enum event_type
2902process_arg_token(struct event_format *event, struct print_arg *arg,
2903 char **tok, enum event_type type)
2904{
2905 char *token;
2906 char *atom;
2907
2908 token = *tok;
2909
2910 switch (type) {
2911 case EVENT_ITEM:
2912 if (strcmp(token, "REC") == 0) {
2913 free_token(token);
2914 type = process_entry(event, arg, &token);
2915 break;
2916 }
2917 atom = token;
2918 /* test the next token */
2919 type = read_token_item(&token);
2920
2921 /*
2922 * If the next token is a parenthesis, then this
2923 * is a function.
2924 */
2925 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2926 free_token(token);
2927 token = NULL;
2928 /* this will free atom. */
2929 type = process_function(event, arg, atom, &token);
2930 break;
2931 }
2932 /* atoms can be more than one token long */
2933 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002934 char *new_atom;
2935 new_atom = realloc(atom,
2936 strlen(atom) + strlen(token) + 2);
2937 if (!new_atom) {
2938 free(atom);
2939 *tok = NULL;
2940 free_token(token);
2941 return EVENT_ERROR;
2942 }
2943 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002944 strcat(atom, " ");
2945 strcat(atom, token);
2946 free_token(token);
2947 type = read_token_item(&token);
2948 }
2949
2950 arg->type = PRINT_ATOM;
2951 arg->atom.atom = atom;
2952 break;
2953
2954 case EVENT_DQUOTE:
2955 case EVENT_SQUOTE:
2956 arg->type = PRINT_ATOM;
2957 arg->atom.atom = token;
2958 type = read_token_item(&token);
2959 break;
2960 case EVENT_DELIM:
2961 if (strcmp(token, "(") == 0) {
2962 free_token(token);
2963 type = process_paren(event, arg, &token);
2964 break;
2965 }
2966 case EVENT_OP:
2967 /* handle single ops */
2968 arg->type = PRINT_OP;
2969 arg->op.op = token;
2970 arg->op.left = NULL;
2971 type = process_op(event, arg, &token);
2972
2973 /* On error, the op is freed */
2974 if (type == EVENT_ERROR)
2975 arg->op.op = NULL;
2976
2977 /* return error type if errored */
2978 break;
2979
2980 case EVENT_ERROR ... EVENT_NEWLINE:
2981 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09002982 do_warning_event(event, "unexpected type %d", type);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002983 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002984 }
2985 *tok = token;
2986
2987 return type;
2988}
2989
2990static int event_read_print_args(struct event_format *event, struct print_arg **list)
2991{
2992 enum event_type type = EVENT_ERROR;
2993 struct print_arg *arg;
2994 char *token;
2995 int args = 0;
2996
2997 do {
2998 if (type == EVENT_NEWLINE) {
2999 type = read_token_item(&token);
3000 continue;
3001 }
3002
3003 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003004 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003005 do_warning_event(event, "%s: not enough memory!",
3006 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003007 return -1;
3008 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003009
3010 type = process_arg(event, arg, &token);
3011
3012 if (type == EVENT_ERROR) {
3013 free_token(token);
3014 free_arg(arg);
3015 return -1;
3016 }
3017
3018 *list = arg;
3019 args++;
3020
3021 if (type == EVENT_OP) {
3022 type = process_op(event, arg, &token);
3023 free_token(token);
3024 if (type == EVENT_ERROR) {
3025 *list = NULL;
3026 free_arg(arg);
3027 return -1;
3028 }
3029 list = &arg->next;
3030 continue;
3031 }
3032
3033 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3034 free_token(token);
3035 *list = arg;
3036 list = &arg->next;
3037 continue;
3038 }
3039 break;
3040 } while (type != EVENT_NONE);
3041
3042 if (type != EVENT_NONE && type != EVENT_ERROR)
3043 free_token(token);
3044
3045 return args;
3046}
3047
3048static int event_read_print(struct event_format *event)
3049{
3050 enum event_type type;
3051 char *token;
3052 int ret;
3053
3054 if (read_expected_item(EVENT_ITEM, "print") < 0)
3055 return -1;
3056
3057 if (read_expected(EVENT_ITEM, "fmt") < 0)
3058 return -1;
3059
3060 if (read_expected(EVENT_OP, ":") < 0)
3061 return -1;
3062
3063 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3064 goto fail;
3065
3066 concat:
3067 event->print_fmt.format = token;
3068 event->print_fmt.args = NULL;
3069
3070 /* ok to have no arg */
3071 type = read_token_item(&token);
3072
3073 if (type == EVENT_NONE)
3074 return 0;
3075
3076 /* Handle concatenation of print lines */
3077 if (type == EVENT_DQUOTE) {
3078 char *cat;
3079
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003080 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3081 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003082 free_token(token);
3083 free_token(event->print_fmt.format);
3084 event->print_fmt.format = NULL;
3085 token = cat;
3086 goto concat;
3087 }
3088
3089 if (test_type_token(type, token, EVENT_DELIM, ","))
3090 goto fail;
3091
3092 free_token(token);
3093
3094 ret = event_read_print_args(event, &event->print_fmt.args);
3095 if (ret < 0)
3096 return -1;
3097
3098 return ret;
3099
3100 fail:
3101 free_token(token);
3102 return -1;
3103}
3104
3105/**
3106 * pevent_find_common_field - return a common field by event
3107 * @event: handle for the event
3108 * @name: the name of the common field to return
3109 *
3110 * Returns a common field from the event by the given @name.
3111 * This only searchs the common fields and not all field.
3112 */
3113struct format_field *
3114pevent_find_common_field(struct event_format *event, const char *name)
3115{
3116 struct format_field *format;
3117
3118 for (format = event->format.common_fields;
3119 format; format = format->next) {
3120 if (strcmp(format->name, name) == 0)
3121 break;
3122 }
3123
3124 return format;
3125}
3126
3127/**
3128 * pevent_find_field - find a non-common field
3129 * @event: handle for the event
3130 * @name: the name of the non-common field
3131 *
3132 * Returns a non-common field by the given @name.
3133 * This does not search common fields.
3134 */
3135struct format_field *
3136pevent_find_field(struct event_format *event, const char *name)
3137{
3138 struct format_field *format;
3139
3140 for (format = event->format.fields;
3141 format; format = format->next) {
3142 if (strcmp(format->name, name) == 0)
3143 break;
3144 }
3145
3146 return format;
3147}
3148
3149/**
3150 * pevent_find_any_field - find any field by name
3151 * @event: handle for the event
3152 * @name: the name of the field
3153 *
3154 * Returns a field by the given @name.
3155 * This searchs the common field names first, then
3156 * the non-common ones if a common one was not found.
3157 */
3158struct format_field *
3159pevent_find_any_field(struct event_format *event, const char *name)
3160{
3161 struct format_field *format;
3162
3163 format = pevent_find_common_field(event, name);
3164 if (format)
3165 return format;
3166 return pevent_find_field(event, name);
3167}
3168
3169/**
3170 * pevent_read_number - read a number from data
3171 * @pevent: handle for the pevent
3172 * @ptr: the raw data
3173 * @size: the size of the data that holds the number
3174 *
3175 * Returns the number (converted to host) from the
3176 * raw data.
3177 */
3178unsigned long long pevent_read_number(struct pevent *pevent,
3179 const void *ptr, int size)
3180{
3181 switch (size) {
3182 case 1:
3183 return *(unsigned char *)ptr;
3184 case 2:
3185 return data2host2(pevent, ptr);
3186 case 4:
3187 return data2host4(pevent, ptr);
3188 case 8:
3189 return data2host8(pevent, ptr);
3190 default:
3191 /* BUG! */
3192 return 0;
3193 }
3194}
3195
3196/**
3197 * pevent_read_number_field - read a number from data
3198 * @field: a handle to the field
3199 * @data: the raw data to read
3200 * @value: the value to place the number in
3201 *
3202 * Reads raw data according to a field offset and size,
3203 * and translates it into @value.
3204 *
3205 * Returns 0 on success, -1 otherwise.
3206 */
3207int pevent_read_number_field(struct format_field *field, const void *data,
3208 unsigned long long *value)
3209{
3210 if (!field)
3211 return -1;
3212 switch (field->size) {
3213 case 1:
3214 case 2:
3215 case 4:
3216 case 8:
3217 *value = pevent_read_number(field->event->pevent,
3218 data + field->offset, field->size);
3219 return 0;
3220 default:
3221 return -1;
3222 }
3223}
3224
3225static int get_common_info(struct pevent *pevent,
3226 const char *type, int *offset, int *size)
3227{
3228 struct event_format *event;
3229 struct format_field *field;
3230
3231 /*
3232 * All events should have the same common elements.
3233 * Pick any event to find where the type is;
3234 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003235 if (!pevent->events) {
3236 do_warning("no event_list!");
3237 return -1;
3238 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003239
3240 event = pevent->events[0];
3241 field = pevent_find_common_field(event, type);
3242 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003243 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003244
3245 *offset = field->offset;
3246 *size = field->size;
3247
3248 return 0;
3249}
3250
3251static int __parse_common(struct pevent *pevent, void *data,
3252 int *size, int *offset, const char *name)
3253{
3254 int ret;
3255
3256 if (!*size) {
3257 ret = get_common_info(pevent, name, offset, size);
3258 if (ret < 0)
3259 return ret;
3260 }
3261 return pevent_read_number(pevent, data + *offset, *size);
3262}
3263
3264static int trace_parse_common_type(struct pevent *pevent, void *data)
3265{
3266 return __parse_common(pevent, data,
3267 &pevent->type_size, &pevent->type_offset,
3268 "common_type");
3269}
3270
3271static int parse_common_pid(struct pevent *pevent, void *data)
3272{
3273 return __parse_common(pevent, data,
3274 &pevent->pid_size, &pevent->pid_offset,
3275 "common_pid");
3276}
3277
3278static int parse_common_pc(struct pevent *pevent, void *data)
3279{
3280 return __parse_common(pevent, data,
3281 &pevent->pc_size, &pevent->pc_offset,
3282 "common_preempt_count");
3283}
3284
3285static int parse_common_flags(struct pevent *pevent, void *data)
3286{
3287 return __parse_common(pevent, data,
3288 &pevent->flags_size, &pevent->flags_offset,
3289 "common_flags");
3290}
3291
3292static int parse_common_lock_depth(struct pevent *pevent, void *data)
3293{
Steven Rostedt0866a972012-05-22 14:52:40 +09003294 return __parse_common(pevent, data,
3295 &pevent->ld_size, &pevent->ld_offset,
3296 "common_lock_depth");
3297}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003298
Steven Rostedt0866a972012-05-22 14:52:40 +09003299static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3300{
3301 return __parse_common(pevent, data,
3302 &pevent->ld_size, &pevent->ld_offset,
3303 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003304}
3305
3306static int events_id_cmp(const void *a, const void *b);
3307
3308/**
3309 * pevent_find_event - find an event by given id
3310 * @pevent: a handle to the pevent
3311 * @id: the id of the event
3312 *
3313 * Returns an event that has a given @id.
3314 */
3315struct event_format *pevent_find_event(struct pevent *pevent, int id)
3316{
3317 struct event_format **eventptr;
3318 struct event_format key;
3319 struct event_format *pkey = &key;
3320
3321 /* Check cache first */
3322 if (pevent->last_event && pevent->last_event->id == id)
3323 return pevent->last_event;
3324
3325 key.id = id;
3326
3327 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3328 sizeof(*pevent->events), events_id_cmp);
3329
3330 if (eventptr) {
3331 pevent->last_event = *eventptr;
3332 return *eventptr;
3333 }
3334
3335 return NULL;
3336}
3337
3338/**
3339 * pevent_find_event_by_name - find an event by given name
3340 * @pevent: a handle to the pevent
3341 * @sys: the system name to search for
3342 * @name: the name of the event to search for
3343 *
3344 * This returns an event with a given @name and under the system
3345 * @sys. If @sys is NULL the first event with @name is returned.
3346 */
3347struct event_format *
3348pevent_find_event_by_name(struct pevent *pevent,
3349 const char *sys, const char *name)
3350{
3351 struct event_format *event;
3352 int i;
3353
3354 if (pevent->last_event &&
3355 strcmp(pevent->last_event->name, name) == 0 &&
3356 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3357 return pevent->last_event;
3358
3359 for (i = 0; i < pevent->nr_events; i++) {
3360 event = pevent->events[i];
3361 if (strcmp(event->name, name) == 0) {
3362 if (!sys)
3363 break;
3364 if (strcmp(event->system, sys) == 0)
3365 break;
3366 }
3367 }
3368 if (i == pevent->nr_events)
3369 event = NULL;
3370
3371 pevent->last_event = event;
3372 return event;
3373}
3374
3375static unsigned long long
3376eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3377{
3378 struct pevent *pevent = event->pevent;
3379 unsigned long long val = 0;
3380 unsigned long long left, right;
3381 struct print_arg *typearg = NULL;
3382 struct print_arg *larg;
3383 unsigned long offset;
3384 unsigned int field_size;
3385
3386 switch (arg->type) {
3387 case PRINT_NULL:
3388 /* ?? */
3389 return 0;
3390 case PRINT_ATOM:
3391 return strtoull(arg->atom.atom, NULL, 0);
3392 case PRINT_FIELD:
3393 if (!arg->field.field) {
3394 arg->field.field = pevent_find_any_field(event, arg->field.name);
3395 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003396 goto out_warning_field;
3397
Steven Rostedtf7d82352012-04-06 00:47:53 +02003398 }
3399 /* must be a number */
3400 val = pevent_read_number(pevent, data + arg->field.field->offset,
3401 arg->field.field->size);
3402 break;
3403 case PRINT_FLAGS:
3404 case PRINT_SYMBOL:
Javi Merinob839e1e82015-03-24 11:07:19 +00003405 case PRINT_INT_ARRAY:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003406 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003407 break;
3408 case PRINT_TYPE:
3409 val = eval_num_arg(data, size, event, arg->typecast.item);
3410 return eval_type(val, arg, 0);
3411 case PRINT_STRING:
3412 case PRINT_BSTRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003413 case PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003414 return 0;
3415 case PRINT_FUNC: {
3416 struct trace_seq s;
3417 trace_seq_init(&s);
3418 val = process_defined_func(&s, data, size, event, arg);
3419 trace_seq_destroy(&s);
3420 return val;
3421 }
3422 case PRINT_OP:
3423 if (strcmp(arg->op.op, "[") == 0) {
3424 /*
3425 * Arrays are special, since we don't want
3426 * to read the arg as is.
3427 */
3428 right = eval_num_arg(data, size, event, arg->op.right);
3429
3430 /* handle typecasts */
3431 larg = arg->op.left;
3432 while (larg->type == PRINT_TYPE) {
3433 if (!typearg)
3434 typearg = larg;
3435 larg = larg->typecast.item;
3436 }
3437
3438 /* Default to long size */
3439 field_size = pevent->long_size;
3440
3441 switch (larg->type) {
3442 case PRINT_DYNAMIC_ARRAY:
3443 offset = pevent_read_number(pevent,
3444 data + larg->dynarray.field->offset,
3445 larg->dynarray.field->size);
3446 if (larg->dynarray.field->elementsize)
3447 field_size = larg->dynarray.field->elementsize;
3448 /*
3449 * The actual length of the dynamic array is stored
3450 * in the top half of the field, and the offset
3451 * is in the bottom half of the 32 bit field.
3452 */
3453 offset &= 0xffff;
3454 offset += right;
3455 break;
3456 case PRINT_FIELD:
3457 if (!larg->field.field) {
3458 larg->field.field =
3459 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003460 if (!larg->field.field) {
3461 arg = larg;
3462 goto out_warning_field;
3463 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003464 }
3465 field_size = larg->field.field->elementsize;
3466 offset = larg->field.field->offset +
3467 right * larg->field.field->elementsize;
3468 break;
3469 default:
3470 goto default_op; /* oops, all bets off */
3471 }
3472 val = pevent_read_number(pevent,
3473 data + offset, field_size);
3474 if (typearg)
3475 val = eval_type(val, typearg, 1);
3476 break;
3477 } else if (strcmp(arg->op.op, "?") == 0) {
3478 left = eval_num_arg(data, size, event, arg->op.left);
3479 arg = arg->op.right;
3480 if (left)
3481 val = eval_num_arg(data, size, event, arg->op.left);
3482 else
3483 val = eval_num_arg(data, size, event, arg->op.right);
3484 break;
3485 }
3486 default_op:
3487 left = eval_num_arg(data, size, event, arg->op.left);
3488 right = eval_num_arg(data, size, event, arg->op.right);
3489 switch (arg->op.op[0]) {
3490 case '!':
3491 switch (arg->op.op[1]) {
3492 case 0:
3493 val = !right;
3494 break;
3495 case '=':
3496 val = left != right;
3497 break;
3498 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003499 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003500 }
3501 break;
3502 case '~':
3503 val = ~right;
3504 break;
3505 case '|':
3506 if (arg->op.op[1])
3507 val = left || right;
3508 else
3509 val = left | right;
3510 break;
3511 case '&':
3512 if (arg->op.op[1])
3513 val = left && right;
3514 else
3515 val = left & right;
3516 break;
3517 case '<':
3518 switch (arg->op.op[1]) {
3519 case 0:
3520 val = left < right;
3521 break;
3522 case '<':
3523 val = left << right;
3524 break;
3525 case '=':
3526 val = left <= right;
3527 break;
3528 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003529 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003530 }
3531 break;
3532 case '>':
3533 switch (arg->op.op[1]) {
3534 case 0:
3535 val = left > right;
3536 break;
3537 case '>':
3538 val = left >> right;
3539 break;
3540 case '=':
3541 val = left >= right;
3542 break;
3543 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003544 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003545 }
3546 break;
3547 case '=':
3548 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003549 goto out_warning_op;
3550
Steven Rostedtf7d82352012-04-06 00:47:53 +02003551 val = left == right;
3552 break;
3553 case '-':
3554 val = left - right;
3555 break;
3556 case '+':
3557 val = left + right;
3558 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003559 case '/':
3560 val = left / right;
3561 break;
3562 case '*':
3563 val = left * right;
3564 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003565 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003566 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003567 }
3568 break;
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003569 case PRINT_DYNAMIC_ARRAY:
3570 /* Without [], we pass the address to the dynamic data */
3571 offset = pevent_read_number(pevent,
3572 data + arg->dynarray.field->offset,
3573 arg->dynarray.field->size);
3574 /*
3575 * The actual length of the dynamic array is stored
3576 * in the top half of the field, and the offset
3577 * is in the bottom half of the 32 bit field.
3578 */
3579 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003580 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003581 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003582 default: /* not sure what to do there */
3583 return 0;
3584 }
3585 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003586
3587out_warning_op:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003588 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003589 return 0;
3590
3591out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003592 do_warning_event(event, "%s: field %s not found",
3593 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003594 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003595}
3596
3597struct flag {
3598 const char *name;
3599 unsigned long long value;
3600};
3601
3602static const struct flag flags[] = {
3603 { "HI_SOFTIRQ", 0 },
3604 { "TIMER_SOFTIRQ", 1 },
3605 { "NET_TX_SOFTIRQ", 2 },
3606 { "NET_RX_SOFTIRQ", 3 },
3607 { "BLOCK_SOFTIRQ", 4 },
3608 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3609 { "TASKLET_SOFTIRQ", 6 },
3610 { "SCHED_SOFTIRQ", 7 },
3611 { "HRTIMER_SOFTIRQ", 8 },
3612 { "RCU_SOFTIRQ", 9 },
3613
3614 { "HRTIMER_NORESTART", 0 },
3615 { "HRTIMER_RESTART", 1 },
3616};
3617
3618static unsigned long long eval_flag(const char *flag)
3619{
3620 int i;
3621
3622 /*
3623 * Some flags in the format files do not get converted.
3624 * If the flag is not numeric, see if it is something that
3625 * we already know about.
3626 */
3627 if (isdigit(flag[0]))
3628 return strtoull(flag, NULL, 0);
3629
3630 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3631 if (strcmp(flags[i].name, flag) == 0)
3632 return flags[i].value;
3633
3634 return 0;
3635}
3636
3637static void print_str_to_seq(struct trace_seq *s, const char *format,
3638 int len_arg, const char *str)
3639{
3640 if (len_arg >= 0)
3641 trace_seq_printf(s, format, len_arg, str);
3642 else
3643 trace_seq_printf(s, format, str);
3644}
3645
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003646static void print_bitmask_to_seq(struct pevent *pevent,
3647 struct trace_seq *s, const char *format,
3648 int len_arg, const void *data, int size)
3649{
3650 int nr_bits = size * 8;
3651 int str_size = (nr_bits + 3) / 4;
3652 int len = 0;
3653 char buf[3];
3654 char *str;
3655 int index;
3656 int i;
3657
3658 /*
3659 * The kernel likes to put in commas every 32 bits, we
3660 * can do the same.
3661 */
3662 str_size += (nr_bits - 1) / 32;
3663
3664 str = malloc(str_size + 1);
3665 if (!str) {
3666 do_warning("%s: not enough memory!", __func__);
3667 return;
3668 }
3669 str[str_size] = 0;
3670
3671 /* Start out with -2 for the two chars per byte */
3672 for (i = str_size - 2; i >= 0; i -= 2) {
3673 /*
3674 * data points to a bit mask of size bytes.
3675 * In the kernel, this is an array of long words, thus
3676 * endianess is very important.
3677 */
3678 if (pevent->file_bigendian)
3679 index = size - (len + 1);
3680 else
3681 index = len;
3682
3683 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3684 memcpy(str + i, buf, 2);
3685 len++;
3686 if (!(len & 3) && i > 0) {
3687 i--;
3688 str[i] = ',';
3689 }
3690 }
3691
3692 if (len_arg >= 0)
3693 trace_seq_printf(s, format, len_arg, str);
3694 else
3695 trace_seq_printf(s, format, str);
3696
3697 free(str);
3698}
3699
Steven Rostedtf7d82352012-04-06 00:47:53 +02003700static void print_str_arg(struct trace_seq *s, void *data, int size,
3701 struct event_format *event, const char *format,
3702 int len_arg, struct print_arg *arg)
3703{
3704 struct pevent *pevent = event->pevent;
3705 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003706 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003707 struct printk_map *printk;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003708 unsigned long long val, fval;
3709 unsigned long addr;
3710 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003711 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003712 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003713 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003714
3715 switch (arg->type) {
3716 case PRINT_NULL:
3717 /* ?? */
3718 return;
3719 case PRINT_ATOM:
3720 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3721 return;
3722 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003723 field = arg->field.field;
3724 if (!field) {
3725 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003726 if (!field) {
3727 str = arg->field.name;
3728 goto out_warning_field;
3729 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003730 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003731 }
3732 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003733 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003734
3735 /*
3736 * Some events pass in pointers. If this is not an array
3737 * and the size is the same as long_size, assume that it
3738 * is a pointer.
3739 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003740 if (!(field->flags & FIELD_IS_ARRAY) &&
3741 field->size == pevent->long_size) {
3742 addr = *(unsigned long *)(data + field->offset);
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003743 /* Check if it matches a print format */
3744 printk = find_printk(pevent, addr);
3745 if (printk)
3746 trace_seq_puts(s, printk->printk);
3747 else
3748 trace_seq_printf(s, "%lx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003749 break;
3750 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003751 str = malloc(len + 1);
3752 if (!str) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003753 do_warning_event(event, "%s: not enough memory!",
3754 __func__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003755 return;
3756 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003757 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003758 str[len] = 0;
3759 print_str_to_seq(s, format, len_arg, str);
3760 free(str);
3761 break;
3762 case PRINT_FLAGS:
3763 val = eval_num_arg(data, size, event, arg->flags.field);
3764 print = 0;
3765 for (flag = arg->flags.flags; flag; flag = flag->next) {
3766 fval = eval_flag(flag->value);
3767 if (!val && !fval) {
3768 print_str_to_seq(s, format, len_arg, flag->str);
3769 break;
3770 }
3771 if (fval && (val & fval) == fval) {
3772 if (print && arg->flags.delim)
3773 trace_seq_puts(s, arg->flags.delim);
3774 print_str_to_seq(s, format, len_arg, flag->str);
3775 print = 1;
3776 val &= ~fval;
3777 }
3778 }
3779 break;
3780 case PRINT_SYMBOL:
3781 val = eval_num_arg(data, size, event, arg->symbol.field);
3782 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3783 fval = eval_flag(flag->value);
3784 if (val == fval) {
3785 print_str_to_seq(s, format, len_arg, flag->str);
3786 break;
3787 }
3788 }
3789 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003790 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003791 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3792 unsigned long offset;
3793 offset = pevent_read_number(pevent,
3794 data + arg->hex.field->dynarray.field->offset,
3795 arg->hex.field->dynarray.field->size);
3796 hex = data + (offset & 0xffff);
3797 } else {
3798 field = arg->hex.field->field.field;
3799 if (!field) {
3800 str = arg->hex.field->field.name;
3801 field = pevent_find_any_field(event, str);
3802 if (!field)
3803 goto out_warning_field;
3804 arg->hex.field->field.field = field;
3805 }
3806 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003807 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003808 len = eval_num_arg(data, size, event, arg->hex.size);
3809 for (i = 0; i < len; i++) {
3810 if (i)
3811 trace_seq_putc(s, ' ');
3812 trace_seq_printf(s, "%02x", hex[i]);
3813 }
3814 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003815
Javi Merinob839e1e82015-03-24 11:07:19 +00003816 case PRINT_INT_ARRAY: {
3817 void *num;
3818 int el_size;
3819
3820 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3821 unsigned long offset;
3822 struct format_field *field =
3823 arg->int_array.field->dynarray.field;
3824 offset = pevent_read_number(pevent,
3825 data + field->offset,
3826 field->size);
3827 num = data + (offset & 0xffff);
3828 } else {
3829 field = arg->int_array.field->field.field;
3830 if (!field) {
3831 str = arg->int_array.field->field.name;
3832 field = pevent_find_any_field(event, str);
3833 if (!field)
3834 goto out_warning_field;
3835 arg->int_array.field->field.field = field;
3836 }
3837 num = data + field->offset;
3838 }
3839 len = eval_num_arg(data, size, event, arg->int_array.count);
3840 el_size = eval_num_arg(data, size, event,
3841 arg->int_array.el_size);
3842 for (i = 0; i < len; i++) {
3843 if (i)
3844 trace_seq_putc(s, ' ');
3845
3846 if (el_size == 1) {
3847 trace_seq_printf(s, "%u", *(uint8_t *)num);
3848 } else if (el_size == 2) {
3849 trace_seq_printf(s, "%u", *(uint16_t *)num);
3850 } else if (el_size == 4) {
3851 trace_seq_printf(s, "%u", *(uint32_t *)num);
3852 } else if (el_size == 8) {
3853 trace_seq_printf(s, "%lu", *(uint64_t *)num);
3854 } else {
3855 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3856 el_size, *(uint8_t *)num);
3857 el_size = 1;
3858 }
3859
3860 num += el_size;
3861 }
3862 break;
3863 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003864 case PRINT_TYPE:
3865 break;
3866 case PRINT_STRING: {
3867 int str_offset;
3868
3869 if (arg->string.offset == -1) {
3870 struct format_field *f;
3871
3872 f = pevent_find_any_field(event, arg->string.string);
3873 arg->string.offset = f->offset;
3874 }
3875 str_offset = data2host4(pevent, data + arg->string.offset);
3876 str_offset &= 0xffff;
3877 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3878 break;
3879 }
3880 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003881 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003882 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003883 case PRINT_BITMASK: {
3884 int bitmask_offset;
3885 int bitmask_size;
3886
3887 if (arg->bitmask.offset == -1) {
3888 struct format_field *f;
3889
3890 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3891 arg->bitmask.offset = f->offset;
3892 }
3893 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3894 bitmask_size = bitmask_offset >> 16;
3895 bitmask_offset &= 0xffff;
3896 print_bitmask_to_seq(pevent, s, format, len_arg,
3897 data + bitmask_offset, bitmask_size);
3898 break;
3899 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003900 case PRINT_OP:
3901 /*
3902 * The only op for string should be ? :
3903 */
3904 if (arg->op.op[0] != '?')
3905 return;
3906 val = eval_num_arg(data, size, event, arg->op.left);
3907 if (val)
3908 print_str_arg(s, data, size, event,
3909 format, len_arg, arg->op.right->op.left);
3910 else
3911 print_str_arg(s, data, size, event,
3912 format, len_arg, arg->op.right->op.right);
3913 break;
3914 case PRINT_FUNC:
3915 process_defined_func(s, data, size, event, arg);
3916 break;
3917 default:
3918 /* well... */
3919 break;
3920 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003921
3922 return;
3923
3924out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003925 do_warning_event(event, "%s: field %s not found",
3926 __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003927}
3928
3929static unsigned long long
3930process_defined_func(struct trace_seq *s, void *data, int size,
3931 struct event_format *event, struct print_arg *arg)
3932{
3933 struct pevent_function_handler *func_handle = arg->func.func;
3934 struct pevent_func_params *param;
3935 unsigned long long *args;
3936 unsigned long long ret;
3937 struct print_arg *farg;
3938 struct trace_seq str;
3939 struct save_str {
3940 struct save_str *next;
3941 char *str;
3942 } *strings = NULL, *string;
3943 int i;
3944
3945 if (!func_handle->nr_args) {
3946 ret = (*func_handle->func)(s, NULL);
3947 goto out;
3948 }
3949
3950 farg = arg->func.args;
3951 param = func_handle->params;
3952
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003953 ret = ULLONG_MAX;
3954 args = malloc(sizeof(*args) * func_handle->nr_args);
3955 if (!args)
3956 goto out;
3957
Steven Rostedtf7d82352012-04-06 00:47:53 +02003958 for (i = 0; i < func_handle->nr_args; i++) {
3959 switch (param->type) {
3960 case PEVENT_FUNC_ARG_INT:
3961 case PEVENT_FUNC_ARG_LONG:
3962 case PEVENT_FUNC_ARG_PTR:
3963 args[i] = eval_num_arg(data, size, event, farg);
3964 break;
3965 case PEVENT_FUNC_ARG_STRING:
3966 trace_seq_init(&str);
3967 print_str_arg(&str, data, size, event, "%s", -1, farg);
3968 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003969 string = malloc(sizeof(*string));
3970 if (!string) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003971 do_warning_event(event, "%s(%d): malloc str",
3972 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003973 goto out_free;
3974 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003975 string->next = strings;
3976 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003977 if (!string->str) {
3978 free(string);
Namhyung Kim3388cc32014-03-19 10:22:53 +09003979 do_warning_event(event, "%s(%d): malloc str",
3980 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003981 goto out_free;
3982 }
Robert Richter0cf26012012-08-07 19:43:14 +02003983 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003984 strings = string;
3985 trace_seq_destroy(&str);
3986 break;
3987 default:
3988 /*
3989 * Something went totally wrong, this is not
3990 * an input error, something in this code broke.
3991 */
Namhyung Kim3388cc32014-03-19 10:22:53 +09003992 do_warning_event(event, "Unexpected end of arguments\n");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003993 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003994 }
3995 farg = farg->next;
Namhyung Kim21c69e722012-05-23 11:36:51 +09003996 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003997 }
3998
3999 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004000out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004001 free(args);
4002 while (strings) {
4003 string = strings;
4004 strings = string->next;
4005 free(string->str);
4006 free(string);
4007 }
4008
4009 out:
4010 /* TBD : handle return type here */
4011 return ret;
4012}
4013
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004014static void free_args(struct print_arg *args)
4015{
4016 struct print_arg *next;
4017
4018 while (args) {
4019 next = args->next;
4020
4021 free_arg(args);
4022 args = next;
4023 }
4024}
4025
Steven Rostedtf7d82352012-04-06 00:47:53 +02004026static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4027{
4028 struct pevent *pevent = event->pevent;
4029 struct format_field *field, *ip_field;
4030 struct print_arg *args, *arg, **next;
4031 unsigned long long ip, val;
4032 char *ptr;
4033 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004034 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004035
4036 field = pevent->bprint_buf_field;
4037 ip_field = pevent->bprint_ip_field;
4038
4039 if (!field) {
4040 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004041 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004042 do_warning_event(event, "can't find buffer field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004043 return NULL;
4044 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004045 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004046 if (!ip_field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004047 do_warning_event(event, "can't find ip field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004048 return NULL;
4049 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004050 pevent->bprint_buf_field = field;
4051 pevent->bprint_ip_field = ip_field;
4052 }
4053
4054 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4055
4056 /*
4057 * The first arg is the IP pointer.
4058 */
4059 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004060 if (!args) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004061 do_warning_event(event, "%s(%d): not enough memory!",
4062 __func__, __LINE__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004063 return NULL;
4064 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004065 arg = args;
4066 arg->next = NULL;
4067 next = &arg->next;
4068
4069 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004070
4071 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4072 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004073
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004074 /* skip the first "%pf: " */
4075 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004076 bptr < data + size && *ptr; ptr++) {
4077 int ls = 0;
4078
4079 if (*ptr == '%') {
4080 process_again:
4081 ptr++;
4082 switch (*ptr) {
4083 case '%':
4084 break;
4085 case 'l':
4086 ls++;
4087 goto process_again;
4088 case 'L':
4089 ls = 2;
4090 goto process_again;
4091 case '0' ... '9':
4092 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004093 case '.':
4094 goto process_again;
Steven Rostedt (Red Hat)55426292015-03-24 09:57:51 -04004095 case 'z':
4096 case 'Z':
4097 ls = 1;
4098 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004099 case 'p':
4100 ls = 1;
4101 /* fall through */
4102 case 'd':
4103 case 'u':
4104 case 'x':
4105 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004106 switch (ls) {
4107 case 0:
4108 vsize = 4;
4109 break;
4110 case 1:
4111 vsize = pevent->long_size;
4112 break;
4113 case 2:
4114 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02004115 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004116 default:
4117 vsize = ls; /* ? */
4118 break;
4119 }
4120 /* fall through */
4121 case '*':
4122 if (*ptr == '*')
4123 vsize = 4;
4124
Steven Rostedtf7d82352012-04-06 00:47:53 +02004125 /* the pointers are always 4 bytes aligned */
4126 bptr = (void *)(((unsigned long)bptr + 3) &
4127 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004128 val = pevent_read_number(pevent, bptr, vsize);
4129 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004130 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004131 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004132 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004133 __func__, __LINE__);
4134 goto out_free;
4135 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004136 arg->next = NULL;
4137 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004138 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4139 free(arg);
4140 goto out_free;
4141 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004142 *next = arg;
4143 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004144 /*
4145 * The '*' case means that an arg is used as the length.
4146 * We need to continue to figure out for what.
4147 */
4148 if (*ptr == '*')
4149 goto process_again;
4150
Steven Rostedtf7d82352012-04-06 00:47:53 +02004151 break;
4152 case 's':
4153 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004154 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004155 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004156 __func__, __LINE__);
4157 goto out_free;
4158 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004159 arg->next = NULL;
4160 arg->type = PRINT_BSTRING;
4161 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09004162 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004163 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004164 bptr += strlen(bptr) + 1;
4165 *next = arg;
4166 next = &arg->next;
4167 default:
4168 break;
4169 }
4170 }
4171 }
4172
4173 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004174
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004175out_free:
4176 free_args(args);
4177 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004178}
4179
4180static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004181get_bprint_format(void *data, int size __maybe_unused,
4182 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004183{
4184 struct pevent *pevent = event->pevent;
4185 unsigned long long addr;
4186 struct format_field *field;
4187 struct printk_map *printk;
4188 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004189
4190 field = pevent->bprint_fmt_field;
4191
4192 if (!field) {
4193 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004194 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004195 do_warning_event(event, "can't find format field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004196 return NULL;
4197 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004198 pevent->bprint_fmt_field = field;
4199 }
4200
4201 addr = pevent_read_number(pevent, data + field->offset, field->size);
4202
4203 printk = find_printk(pevent, addr);
4204 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004205 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004206 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004207 return format;
4208 }
4209
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004210 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004211 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004212
4213 return format;
4214}
4215
4216static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4217 struct event_format *event, struct print_arg *arg)
4218{
4219 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004220 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02004221
4222 if (arg->type == PRINT_FUNC) {
4223 process_defined_func(s, data, size, event, arg);
4224 return;
4225 }
4226
4227 if (arg->type != PRINT_FIELD) {
4228 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4229 arg->type);
4230 return;
4231 }
4232
4233 if (mac == 'm')
4234 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4235 if (!arg->field.field) {
4236 arg->field.field =
4237 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004238 if (!arg->field.field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004239 do_warning_event(event, "%s: field %s not found",
4240 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004241 return;
4242 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004243 }
4244 if (arg->field.field->size != 6) {
4245 trace_seq_printf(s, "INVALIDMAC");
4246 return;
4247 }
4248 buf = data + arg->field.field->offset;
4249 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4250}
4251
David Ahern3d199b52014-12-18 19:11:11 -07004252static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4253{
4254 const char *fmt;
4255
4256 if (i == 'i')
4257 fmt = "%03d.%03d.%03d.%03d";
4258 else
4259 fmt = "%d.%d.%d.%d";
4260
4261 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4262}
4263
4264static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4265{
4266 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4267 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4268}
4269
4270static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4271{
4272 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4273}
4274
4275static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4276{
4277 int i, j, range;
4278 unsigned char zerolength[8];
4279 int longest = 1;
4280 int colonpos = -1;
4281 uint16_t word;
4282 uint8_t hi, lo;
4283 bool needcolon = false;
4284 bool useIPv4;
4285 struct in6_addr in6;
4286
4287 memcpy(&in6, addr, sizeof(struct in6_addr));
4288
4289 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4290
4291 memset(zerolength, 0, sizeof(zerolength));
4292
4293 if (useIPv4)
4294 range = 6;
4295 else
4296 range = 8;
4297
4298 /* find position of longest 0 run */
4299 for (i = 0; i < range; i++) {
4300 for (j = i; j < range; j++) {
4301 if (in6.s6_addr16[j] != 0)
4302 break;
4303 zerolength[i]++;
4304 }
4305 }
4306 for (i = 0; i < range; i++) {
4307 if (zerolength[i] > longest) {
4308 longest = zerolength[i];
4309 colonpos = i;
4310 }
4311 }
4312 if (longest == 1) /* don't compress a single 0 */
4313 colonpos = -1;
4314
4315 /* emit address */
4316 for (i = 0; i < range; i++) {
4317 if (i == colonpos) {
4318 if (needcolon || i == 0)
4319 trace_seq_printf(s, ":");
4320 trace_seq_printf(s, ":");
4321 needcolon = false;
4322 i += longest - 1;
4323 continue;
4324 }
4325 if (needcolon) {
4326 trace_seq_printf(s, ":");
4327 needcolon = false;
4328 }
4329 /* hex u16 without leading 0s */
4330 word = ntohs(in6.s6_addr16[i]);
4331 hi = word >> 8;
4332 lo = word & 0xff;
4333 if (hi)
4334 trace_seq_printf(s, "%x%02x", hi, lo);
4335 else
4336 trace_seq_printf(s, "%x", lo);
4337
4338 needcolon = true;
4339 }
4340
4341 if (useIPv4) {
4342 if (needcolon)
4343 trace_seq_printf(s, ":");
4344 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4345 }
4346
4347 return;
4348}
4349
4350static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4351{
4352 int j;
4353
4354 for (j = 0; j < 16; j += 2) {
4355 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4356 if (i == 'I' && j < 14)
4357 trace_seq_printf(s, ":");
4358 }
4359}
4360
4361/*
4362 * %pi4 print an IPv4 address with leading zeros
4363 * %pI4 print an IPv4 address without leading zeros
4364 * %pi6 print an IPv6 address without colons
4365 * %pI6 print an IPv6 address with colons
4366 * %pI6c print an IPv6 address in compressed form with colons
4367 * %pISpc print an IP address based on sockaddr; p adds port.
4368 */
4369static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4370 void *data, int size, struct event_format *event,
4371 struct print_arg *arg)
4372{
4373 unsigned char *buf;
4374
4375 if (arg->type == PRINT_FUNC) {
4376 process_defined_func(s, data, size, event, arg);
4377 return 0;
4378 }
4379
4380 if (arg->type != PRINT_FIELD) {
4381 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4382 return 0;
4383 }
4384
4385 if (!arg->field.field) {
4386 arg->field.field =
4387 pevent_find_any_field(event, arg->field.name);
4388 if (!arg->field.field) {
4389 do_warning("%s: field %s not found",
4390 __func__, arg->field.name);
4391 return 0;
4392 }
4393 }
4394
4395 buf = data + arg->field.field->offset;
4396
4397 if (arg->field.field->size != 4) {
4398 trace_seq_printf(s, "INVALIDIPv4");
4399 return 0;
4400 }
4401 print_ip4_addr(s, i, buf);
4402
4403 return 0;
4404}
4405
4406static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4407 void *data, int size, struct event_format *event,
4408 struct print_arg *arg)
4409{
4410 char have_c = 0;
4411 unsigned char *buf;
4412 int rc = 0;
4413
4414 /* pI6c */
4415 if (i == 'I' && *ptr == 'c') {
4416 have_c = 1;
4417 ptr++;
4418 rc++;
4419 }
4420
4421 if (arg->type == PRINT_FUNC) {
4422 process_defined_func(s, data, size, event, arg);
4423 return rc;
4424 }
4425
4426 if (arg->type != PRINT_FIELD) {
4427 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4428 return rc;
4429 }
4430
4431 if (!arg->field.field) {
4432 arg->field.field =
4433 pevent_find_any_field(event, arg->field.name);
4434 if (!arg->field.field) {
4435 do_warning("%s: field %s not found",
4436 __func__, arg->field.name);
4437 return rc;
4438 }
4439 }
4440
4441 buf = data + arg->field.field->offset;
4442
4443 if (arg->field.field->size != 16) {
4444 trace_seq_printf(s, "INVALIDIPv6");
4445 return rc;
4446 }
4447
4448 if (have_c)
4449 print_ip6c_addr(s, buf);
4450 else
4451 print_ip6_addr(s, i, buf);
4452
4453 return rc;
4454}
4455
4456static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4457 void *data, int size, struct event_format *event,
4458 struct print_arg *arg)
4459{
4460 char have_c = 0, have_p = 0;
4461 unsigned char *buf;
4462 struct sockaddr_storage *sa;
4463 int rc = 0;
4464
4465 /* pISpc */
4466 if (i == 'I') {
4467 if (*ptr == 'p') {
4468 have_p = 1;
4469 ptr++;
4470 rc++;
4471 }
4472 if (*ptr == 'c') {
4473 have_c = 1;
4474 ptr++;
4475 rc++;
4476 }
4477 }
4478
4479 if (arg->type == PRINT_FUNC) {
4480 process_defined_func(s, data, size, event, arg);
4481 return rc;
4482 }
4483
4484 if (arg->type != PRINT_FIELD) {
4485 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4486 return rc;
4487 }
4488
4489 if (!arg->field.field) {
4490 arg->field.field =
4491 pevent_find_any_field(event, arg->field.name);
4492 if (!arg->field.field) {
4493 do_warning("%s: field %s not found",
4494 __func__, arg->field.name);
4495 return rc;
4496 }
4497 }
4498
4499 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4500
4501 if (sa->ss_family == AF_INET) {
4502 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4503
4504 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4505 trace_seq_printf(s, "INVALIDIPv4");
4506 return rc;
4507 }
4508
4509 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4510 if (have_p)
4511 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4512
4513
4514 } else if (sa->ss_family == AF_INET6) {
4515 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4516
4517 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4518 trace_seq_printf(s, "INVALIDIPv6");
4519 return rc;
4520 }
4521
4522 if (have_p)
4523 trace_seq_printf(s, "[");
4524
4525 buf = (unsigned char *) &sa6->sin6_addr;
4526 if (have_c)
4527 print_ip6c_addr(s, buf);
4528 else
4529 print_ip6_addr(s, i, buf);
4530
4531 if (have_p)
4532 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4533 }
4534
4535 return rc;
4536}
4537
4538static int print_ip_arg(struct trace_seq *s, const char *ptr,
4539 void *data, int size, struct event_format *event,
4540 struct print_arg *arg)
4541{
4542 char i = *ptr; /* 'i' or 'I' */
4543 char ver;
4544 int rc = 0;
4545
4546 ptr++;
4547 rc++;
4548
4549 ver = *ptr;
4550 ptr++;
4551 rc++;
4552
4553 switch (ver) {
4554 case '4':
4555 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4556 break;
4557 case '6':
4558 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4559 break;
4560 case 'S':
4561 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4562 break;
4563 default:
4564 return 0;
4565 }
4566
4567 return rc;
4568}
4569
Namhyung Kim600da3c2012-06-22 17:10:15 +09004570static int is_printable_array(char *p, unsigned int len)
4571{
4572 unsigned int i;
4573
4574 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004575 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004576 return 0;
4577 return 1;
4578}
4579
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03004580static void print_event_fields(struct trace_seq *s, void *data,
4581 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004582 struct event_format *event)
4583{
4584 struct format_field *field;
4585 unsigned long long val;
4586 unsigned int offset, len, i;
4587
4588 field = event->format.fields;
4589 while (field) {
4590 trace_seq_printf(s, " %s=", field->name);
4591 if (field->flags & FIELD_IS_ARRAY) {
4592 offset = field->offset;
4593 len = field->size;
4594 if (field->flags & FIELD_IS_DYNAMIC) {
4595 val = pevent_read_number(event->pevent, data + offset, len);
4596 offset = val;
4597 len = offset >> 16;
4598 offset &= 0xffff;
4599 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09004600 if (field->flags & FIELD_IS_STRING &&
4601 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004602 trace_seq_printf(s, "%s", (char *)data + offset);
4603 } else {
4604 trace_seq_puts(s, "ARRAY[");
4605 for (i = 0; i < len; i++) {
4606 if (i)
4607 trace_seq_puts(s, ", ");
4608 trace_seq_printf(s, "%02x",
4609 *((unsigned char *)data + offset + i));
4610 }
4611 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09004612 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004613 }
4614 } else {
4615 val = pevent_read_number(event->pevent, data + field->offset,
4616 field->size);
4617 if (field->flags & FIELD_IS_POINTER) {
4618 trace_seq_printf(s, "0x%llx", val);
4619 } else if (field->flags & FIELD_IS_SIGNED) {
4620 switch (field->size) {
4621 case 4:
4622 /*
4623 * If field is long then print it in hex.
4624 * A long usually stores pointers.
4625 */
4626 if (field->flags & FIELD_IS_LONG)
4627 trace_seq_printf(s, "0x%x", (int)val);
4628 else
4629 trace_seq_printf(s, "%d", (int)val);
4630 break;
4631 case 2:
4632 trace_seq_printf(s, "%2d", (short)val);
4633 break;
4634 case 1:
4635 trace_seq_printf(s, "%1d", (char)val);
4636 break;
4637 default:
4638 trace_seq_printf(s, "%lld", val);
4639 }
4640 } else {
4641 if (field->flags & FIELD_IS_LONG)
4642 trace_seq_printf(s, "0x%llx", val);
4643 else
4644 trace_seq_printf(s, "%llu", val);
4645 }
4646 }
4647 field = field->next;
4648 }
4649}
4650
4651static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4652{
4653 struct pevent *pevent = event->pevent;
4654 struct print_fmt *print_fmt = &event->print_fmt;
4655 struct print_arg *arg = print_fmt->args;
4656 struct print_arg *args = NULL;
4657 const char *ptr = print_fmt->format;
4658 unsigned long long val;
4659 struct func_map *func;
4660 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004661 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004662 char *bprint_fmt = NULL;
4663 char format[32];
4664 int show_func;
4665 int len_as_arg;
4666 int len_arg;
4667 int len;
4668 int ls;
4669
4670 if (event->flags & EVENT_FL_FAILED) {
4671 trace_seq_printf(s, "[FAILED TO PARSE]");
4672 print_event_fields(s, data, size, event);
4673 return;
4674 }
4675
4676 if (event->flags & EVENT_FL_ISBPRINT) {
4677 bprint_fmt = get_bprint_format(data, size, event);
4678 args = make_bprint_args(bprint_fmt, data, size, event);
4679 arg = args;
4680 ptr = bprint_fmt;
4681 }
4682
4683 for (; *ptr; ptr++) {
4684 ls = 0;
4685 if (*ptr == '\\') {
4686 ptr++;
4687 switch (*ptr) {
4688 case 'n':
4689 trace_seq_putc(s, '\n');
4690 break;
4691 case 't':
4692 trace_seq_putc(s, '\t');
4693 break;
4694 case 'r':
4695 trace_seq_putc(s, '\r');
4696 break;
4697 case '\\':
4698 trace_seq_putc(s, '\\');
4699 break;
4700 default:
4701 trace_seq_putc(s, *ptr);
4702 break;
4703 }
4704
4705 } else if (*ptr == '%') {
4706 saveptr = ptr;
4707 show_func = 0;
4708 len_as_arg = 0;
4709 cont_process:
4710 ptr++;
4711 switch (*ptr) {
4712 case '%':
4713 trace_seq_putc(s, '%');
4714 break;
4715 case '#':
4716 /* FIXME: need to handle properly */
4717 goto cont_process;
4718 case 'h':
4719 ls--;
4720 goto cont_process;
4721 case 'l':
4722 ls++;
4723 goto cont_process;
4724 case 'L':
4725 ls = 2;
4726 goto cont_process;
4727 case '*':
4728 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004729 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004730 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004731 event->flags |= EVENT_FL_FAILED;
4732 goto out_failed;
4733 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004734 len_arg = eval_num_arg(data, size, event, arg);
4735 len_as_arg = 1;
4736 arg = arg->next;
4737 goto cont_process;
4738 case '.':
4739 case 'z':
4740 case 'Z':
4741 case '0' ... '9':
4742 goto cont_process;
4743 case 'p':
4744 if (pevent->long_size == 4)
4745 ls = 1;
4746 else
4747 ls = 2;
4748
4749 if (*(ptr+1) == 'F' ||
4750 *(ptr+1) == 'f') {
4751 ptr++;
4752 show_func = *ptr;
4753 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4754 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4755 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004756 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004757 break;
David Ahern3d199b52014-12-18 19:11:11 -07004758 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4759 int n;
4760
4761 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4762 if (n > 0) {
4763 ptr += n;
4764 arg = arg->next;
4765 break;
4766 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004767 }
4768
4769 /* fall through */
4770 case 'd':
4771 case 'i':
4772 case 'x':
4773 case 'X':
4774 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004775 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004776 do_warning_event(event, "no argument match");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004777 event->flags |= EVENT_FL_FAILED;
4778 goto out_failed;
4779 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004780
4781 len = ((unsigned long)ptr + 1) -
4782 (unsigned long)saveptr;
4783
4784 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004785 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004786 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004787 event->flags |= EVENT_FL_FAILED;
4788 len = 31;
4789 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004790
4791 memcpy(format, saveptr, len);
4792 format[len] = 0;
4793
4794 val = eval_num_arg(data, size, event, arg);
4795 arg = arg->next;
4796
4797 if (show_func) {
4798 func = find_func(pevent, val);
4799 if (func) {
4800 trace_seq_puts(s, func->func);
4801 if (show_func == 'F')
4802 trace_seq_printf(s,
4803 "+0x%llx",
4804 val - func->addr);
4805 break;
4806 }
4807 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004808 if (pevent->long_size == 8 && ls &&
4809 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004810 char *p;
4811
4812 ls = 2;
4813 /* make %l into %ll */
4814 p = strchr(format, 'l');
4815 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004816 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004817 else if (strcmp(format, "%p") == 0)
4818 strcpy(format, "0x%llx");
4819 }
4820 switch (ls) {
4821 case -2:
4822 if (len_as_arg)
4823 trace_seq_printf(s, format, len_arg, (char)val);
4824 else
4825 trace_seq_printf(s, format, (char)val);
4826 break;
4827 case -1:
4828 if (len_as_arg)
4829 trace_seq_printf(s, format, len_arg, (short)val);
4830 else
4831 trace_seq_printf(s, format, (short)val);
4832 break;
4833 case 0:
4834 if (len_as_arg)
4835 trace_seq_printf(s, format, len_arg, (int)val);
4836 else
4837 trace_seq_printf(s, format, (int)val);
4838 break;
4839 case 1:
4840 if (len_as_arg)
4841 trace_seq_printf(s, format, len_arg, (long)val);
4842 else
4843 trace_seq_printf(s, format, (long)val);
4844 break;
4845 case 2:
4846 if (len_as_arg)
4847 trace_seq_printf(s, format, len_arg,
4848 (long long)val);
4849 else
4850 trace_seq_printf(s, format, (long long)val);
4851 break;
4852 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09004853 do_warning_event(event, "bad count (%d)", ls);
Namhyung Kim245c5a12012-09-07 11:49:45 +09004854 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004855 }
4856 break;
4857 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004858 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004859 do_warning_event(event, "no matching argument");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004860 event->flags |= EVENT_FL_FAILED;
4861 goto out_failed;
4862 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004863
4864 len = ((unsigned long)ptr + 1) -
4865 (unsigned long)saveptr;
4866
4867 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004868 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004869 do_warning_event(event, "bad format!");
Namhyung Kim245c5a12012-09-07 11:49:45 +09004870 event->flags |= EVENT_FL_FAILED;
4871 len = 31;
4872 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004873
4874 memcpy(format, saveptr, len);
4875 format[len] = 0;
4876 if (!len_as_arg)
4877 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05004878 /* Use helper trace_seq */
4879 trace_seq_init(&p);
4880 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004881 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05004882 trace_seq_terminate(&p);
4883 trace_seq_puts(s, p.buffer);
Steven Rostedtde04f862014-04-22 19:23:30 -04004884 trace_seq_destroy(&p);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004885 arg = arg->next;
4886 break;
4887 default:
4888 trace_seq_printf(s, ">%c<", *ptr);
4889
4890 }
4891 } else
4892 trace_seq_putc(s, *ptr);
4893 }
4894
Namhyung Kim245c5a12012-09-07 11:49:45 +09004895 if (event->flags & EVENT_FL_FAILED) {
4896out_failed:
4897 trace_seq_printf(s, "[FAILED TO PARSE]");
4898 }
4899
Steven Rostedtf7d82352012-04-06 00:47:53 +02004900 if (args) {
4901 free_args(args);
4902 free(bprint_fmt);
4903 }
4904}
4905
4906/**
4907 * pevent_data_lat_fmt - parse the data for the latency format
4908 * @pevent: a handle to the pevent
4909 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004910 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004911 *
4912 * This parses out the Latency format (interrupts disabled,
4913 * need rescheduling, in hard/soft interrupt, preempt count
4914 * and lock depth) and places it into the trace_seq.
4915 */
4916void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004917 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004918{
4919 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004920 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004921 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004922 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004923 unsigned int lat_flags;
4924 unsigned int pc;
4925 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004926 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004927 int hardirq;
4928 int softirq;
4929 void *data = record->data;
4930
4931 lat_flags = parse_common_flags(pevent, data);
4932 pc = parse_common_pc(pevent, data);
4933 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004934 if (lock_depth_exists)
4935 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004936 else if (check_lock_depth) {
4937 lock_depth = parse_common_lock_depth(pevent, data);
4938 if (lock_depth < 0)
4939 check_lock_depth = 0;
4940 else
4941 lock_depth_exists = 1;
4942 }
4943
4944 /* migrate_disable may not always exist */
4945 if (migrate_disable_exists)
4946 migrate_disable = parse_common_migrate_disable(pevent, data);
4947 else if (check_migrate_disable) {
4948 migrate_disable = parse_common_migrate_disable(pevent, data);
4949 if (migrate_disable < 0)
4950 check_migrate_disable = 0;
4951 else
4952 migrate_disable_exists = 1;
4953 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004954
4955 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4956 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4957
4958 trace_seq_printf(s, "%c%c%c",
4959 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4960 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4961 'X' : '.',
4962 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4963 'N' : '.',
4964 (hardirq && softirq) ? 'H' :
4965 hardirq ? 'h' : softirq ? 's' : '.');
4966
4967 if (pc)
4968 trace_seq_printf(s, "%x", pc);
4969 else
4970 trace_seq_putc(s, '.');
4971
Steven Rostedt0866a972012-05-22 14:52:40 +09004972 if (migrate_disable_exists) {
4973 if (migrate_disable < 0)
4974 trace_seq_putc(s, '.');
4975 else
4976 trace_seq_printf(s, "%d", migrate_disable);
4977 }
4978
Steven Rostedtf7d82352012-04-06 00:47:53 +02004979 if (lock_depth_exists) {
4980 if (lock_depth < 0)
4981 trace_seq_putc(s, '.');
4982 else
4983 trace_seq_printf(s, "%d", lock_depth);
4984 }
4985
4986 trace_seq_terminate(s);
4987}
4988
4989/**
4990 * pevent_data_type - parse out the given event type
4991 * @pevent: a handle to the pevent
4992 * @rec: the record to read from
4993 *
4994 * This returns the event id from the @rec.
4995 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004996int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004997{
4998 return trace_parse_common_type(pevent, rec->data);
4999}
5000
5001/**
5002 * pevent_data_event_from_type - find the event by a given type
5003 * @pevent: a handle to the pevent
5004 * @type: the type of the event.
5005 *
5006 * This returns the event form a given @type;
5007 */
5008struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5009{
5010 return pevent_find_event(pevent, type);
5011}
5012
5013/**
5014 * pevent_data_pid - parse the PID from raw data
5015 * @pevent: a handle to the pevent
5016 * @rec: the record to parse
5017 *
5018 * This returns the PID from a raw data.
5019 */
Steven Rostedt1c698182012-04-06 00:48:06 +02005020int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005021{
5022 return parse_common_pid(pevent, rec->data);
5023}
5024
5025/**
5026 * pevent_data_comm_from_pid - return the command line from PID
5027 * @pevent: a handle to the pevent
5028 * @pid: the PID of the task to search for
5029 *
5030 * This returns a pointer to the command line that has the given
5031 * @pid.
5032 */
5033const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5034{
5035 const char *comm;
5036
5037 comm = find_cmdline(pevent, pid);
5038 return comm;
5039}
5040
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005041static struct cmdline *
5042pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5043{
5044 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5045
5046 if (cmdlist)
5047 cmdlist = cmdlist->next;
5048 else
5049 cmdlist = pevent->cmdlist;
5050
5051 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5052 cmdlist = cmdlist->next;
5053
5054 return (struct cmdline *)cmdlist;
5055}
5056
5057/**
5058 * pevent_data_pid_from_comm - return the pid from a given comm
5059 * @pevent: a handle to the pevent
5060 * @comm: the cmdline to find the pid from
5061 * @next: the cmdline structure to find the next comm
5062 *
5063 * This returns the cmdline structure that holds a pid for a given
5064 * comm, or NULL if none found. As there may be more than one pid for
5065 * a given comm, the result of this call can be passed back into
5066 * a recurring call in the @next paramater, and then it will find the
5067 * next pid.
5068 * Also, it does a linear seach, so it may be slow.
5069 */
5070struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5071 struct cmdline *next)
5072{
5073 struct cmdline *cmdline;
5074
5075 /*
5076 * If the cmdlines have not been converted yet, then use
5077 * the list.
5078 */
5079 if (!pevent->cmdlines)
5080 return pid_from_cmdlist(pevent, comm, next);
5081
5082 if (next) {
5083 /*
5084 * The next pointer could have been still from
5085 * a previous call before cmdlines were created
5086 */
5087 if (next < pevent->cmdlines ||
5088 next >= pevent->cmdlines + pevent->cmdline_count)
5089 next = NULL;
5090 else
5091 cmdline = next++;
5092 }
5093
5094 if (!next)
5095 cmdline = pevent->cmdlines;
5096
5097 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5098 if (strcmp(cmdline->comm, comm) == 0)
5099 return cmdline;
5100 cmdline++;
5101 }
5102 return NULL;
5103}
5104
5105/**
5106 * pevent_cmdline_pid - return the pid associated to a given cmdline
5107 * @cmdline: The cmdline structure to get the pid from
5108 *
5109 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5110 * -1 is returned.
5111 */
5112int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5113{
5114 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5115
5116 if (!cmdline)
5117 return -1;
5118
5119 /*
5120 * If cmdlines have not been created yet, or cmdline is
5121 * not part of the array, then treat it as a cmdlist instead.
5122 */
5123 if (!pevent->cmdlines ||
5124 cmdline < pevent->cmdlines ||
5125 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5126 return cmdlist->pid;
5127
5128 return cmdline->pid;
5129}
5130
Steven Rostedtf7d82352012-04-06 00:47:53 +02005131/**
5132 * pevent_data_comm_from_pid - parse the data into the print format
5133 * @s: the trace_seq to write to
5134 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005135 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02005136 *
5137 * This parses the raw @data using the given @event information and
5138 * writes the print format into the trace_seq.
5139 */
5140void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005141 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005142{
5143 int print_pretty = 1;
5144
Steven Rostedtc6c2b962013-11-01 17:53:59 -04005145 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005146 print_event_fields(s, record->data, record->size, event);
5147 else {
5148
Steven Rostedtc6c2b962013-11-01 17:53:59 -04005149 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005150 print_pretty = event->handler(s, record, event,
5151 event->context);
5152
5153 if (print_pretty)
5154 pretty_print(s, record->data, record->size, event);
5155 }
5156
5157 trace_seq_terminate(s);
5158}
5159
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005160static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5161{
5162 if (!use_trace_clock)
5163 return true;
5164
5165 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5166 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5167 return true;
5168
5169 /* trace_clock is setting in tsc or counter mode */
5170 return false;
5171}
5172
Steven Rostedtf7d82352012-04-06 00:47:53 +02005173void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005174 struct pevent_record *record, bool use_trace_clock)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005175{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03005176 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005177 struct event_format *event;
5178 unsigned long secs;
5179 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005180 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005181 const char *comm;
5182 void *data = record->data;
5183 int type;
5184 int pid;
5185 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005186 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005187 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005188
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005189 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5190 use_trace_clock);
5191 if (use_usec_format) {
5192 secs = record->ts / NSECS_PER_SEC;
5193 nsecs = record->ts - secs * NSECS_PER_SEC;
5194 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005195
5196 if (record->size < 0) {
5197 do_warning("ug! negative record size %d", record->size);
5198 return;
5199 }
5200
5201 type = trace_parse_common_type(pevent, data);
5202
5203 event = pevent_find_event(pevent, type);
5204 if (!event) {
5205 do_warning("ug! no event found for type %d", type);
5206 return;
5207 }
5208
5209 pid = parse_common_pid(pevent, data);
5210 comm = find_cmdline(pevent, pid);
5211
5212 if (pevent->latency_format) {
5213 trace_seq_printf(s, "%8.8s-%-5d %3d",
5214 comm, pid, record->cpu);
5215 pevent_data_lat_fmt(pevent, s, record);
5216 } else
5217 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5218
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005219 if (use_usec_format) {
5220 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5221 usecs = nsecs;
5222 p = 9;
5223 } else {
5224 usecs = (nsecs + 500) / NSECS_PER_USEC;
5225 p = 6;
5226 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02005227
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005228 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5229 secs, p, usecs, event->name);
5230 } else
5231 trace_seq_printf(s, " %12llu: %s: ",
5232 record->ts, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005233
5234 /* Space out the event names evenly. */
5235 len = strlen(event->name);
5236 if (len < 20)
5237 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5238
5239 pevent_event_info(s, event, record);
5240}
5241
5242static int events_id_cmp(const void *a, const void *b)
5243{
5244 struct event_format * const * ea = a;
5245 struct event_format * const * eb = b;
5246
5247 if ((*ea)->id < (*eb)->id)
5248 return -1;
5249
5250 if ((*ea)->id > (*eb)->id)
5251 return 1;
5252
5253 return 0;
5254}
5255
5256static int events_name_cmp(const void *a, const void *b)
5257{
5258 struct event_format * const * ea = a;
5259 struct event_format * const * eb = b;
5260 int res;
5261
5262 res = strcmp((*ea)->name, (*eb)->name);
5263 if (res)
5264 return res;
5265
5266 res = strcmp((*ea)->system, (*eb)->system);
5267 if (res)
5268 return res;
5269
5270 return events_id_cmp(a, b);
5271}
5272
5273static int events_system_cmp(const void *a, const void *b)
5274{
5275 struct event_format * const * ea = a;
5276 struct event_format * const * eb = b;
5277 int res;
5278
5279 res = strcmp((*ea)->system, (*eb)->system);
5280 if (res)
5281 return res;
5282
5283 res = strcmp((*ea)->name, (*eb)->name);
5284 if (res)
5285 return res;
5286
5287 return events_id_cmp(a, b);
5288}
5289
5290struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5291{
5292 struct event_format **events;
5293 int (*sort)(const void *a, const void *b);
5294
5295 events = pevent->sort_events;
5296
5297 if (events && pevent->last_type == sort_type)
5298 return events;
5299
5300 if (!events) {
5301 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5302 if (!events)
5303 return NULL;
5304
5305 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5306 events[pevent->nr_events] = NULL;
5307
5308 pevent->sort_events = events;
5309
5310 /* the internal events are sorted by id */
5311 if (sort_type == EVENT_SORT_ID) {
5312 pevent->last_type = sort_type;
5313 return events;
5314 }
5315 }
5316
5317 switch (sort_type) {
5318 case EVENT_SORT_ID:
5319 sort = events_id_cmp;
5320 break;
5321 case EVENT_SORT_NAME:
5322 sort = events_name_cmp;
5323 break;
5324 case EVENT_SORT_SYSTEM:
5325 sort = events_system_cmp;
5326 break;
5327 default:
5328 return events;
5329 }
5330
5331 qsort(events, pevent->nr_events, sizeof(*events), sort);
5332 pevent->last_type = sort_type;
5333
5334 return events;
5335}
5336
5337static struct format_field **
5338get_event_fields(const char *type, const char *name,
5339 int count, struct format_field *list)
5340{
5341 struct format_field **fields;
5342 struct format_field *field;
5343 int i = 0;
5344
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03005345 fields = malloc(sizeof(*fields) * (count + 1));
5346 if (!fields)
5347 return NULL;
5348
Steven Rostedtf7d82352012-04-06 00:47:53 +02005349 for (field = list; field; field = field->next) {
5350 fields[i++] = field;
5351 if (i == count + 1) {
5352 do_warning("event %s has more %s fields than specified",
5353 name, type);
5354 i--;
5355 break;
5356 }
5357 }
5358
5359 if (i != count)
5360 do_warning("event %s has less %s fields than specified",
5361 name, type);
5362
5363 fields[i] = NULL;
5364
5365 return fields;
5366}
5367
5368/**
5369 * pevent_event_common_fields - return a list of common fields for an event
5370 * @event: the event to return the common fields of.
5371 *
5372 * Returns an allocated array of fields. The last item in the array is NULL.
5373 * The array must be freed with free().
5374 */
5375struct format_field **pevent_event_common_fields(struct event_format *event)
5376{
5377 return get_event_fields("common", event->name,
5378 event->format.nr_common,
5379 event->format.common_fields);
5380}
5381
5382/**
5383 * pevent_event_fields - return a list of event specific fields for an event
5384 * @event: the event to return the fields of.
5385 *
5386 * Returns an allocated array of fields. The last item in the array is NULL.
5387 * The array must be freed with free().
5388 */
5389struct format_field **pevent_event_fields(struct event_format *event)
5390{
5391 return get_event_fields("event", event->name,
5392 event->format.nr_fields,
5393 event->format.fields);
5394}
5395
5396static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5397{
5398 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5399 if (field->next) {
5400 trace_seq_puts(s, ", ");
5401 print_fields(s, field->next);
5402 }
5403}
5404
5405/* for debugging */
5406static void print_args(struct print_arg *args)
5407{
5408 int print_paren = 1;
5409 struct trace_seq s;
5410
5411 switch (args->type) {
5412 case PRINT_NULL:
5413 printf("null");
5414 break;
5415 case PRINT_ATOM:
5416 printf("%s", args->atom.atom);
5417 break;
5418 case PRINT_FIELD:
5419 printf("REC->%s", args->field.name);
5420 break;
5421 case PRINT_FLAGS:
5422 printf("__print_flags(");
5423 print_args(args->flags.field);
5424 printf(", %s, ", args->flags.delim);
5425 trace_seq_init(&s);
5426 print_fields(&s, args->flags.flags);
5427 trace_seq_do_printf(&s);
5428 trace_seq_destroy(&s);
5429 printf(")");
5430 break;
5431 case PRINT_SYMBOL:
5432 printf("__print_symbolic(");
5433 print_args(args->symbol.field);
5434 printf(", ");
5435 trace_seq_init(&s);
5436 print_fields(&s, args->symbol.symbols);
5437 trace_seq_do_printf(&s);
5438 trace_seq_destroy(&s);
5439 printf(")");
5440 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09005441 case PRINT_HEX:
5442 printf("__print_hex(");
5443 print_args(args->hex.field);
5444 printf(", ");
5445 print_args(args->hex.size);
5446 printf(")");
5447 break;
Javi Merinob839e1e82015-03-24 11:07:19 +00005448 case PRINT_INT_ARRAY:
5449 printf("__print_array(");
5450 print_args(args->int_array.field);
5451 printf(", ");
5452 print_args(args->int_array.count);
5453 printf(", ");
5454 print_args(args->int_array.el_size);
5455 printf(")");
5456 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005457 case PRINT_STRING:
5458 case PRINT_BSTRING:
5459 printf("__get_str(%s)", args->string.string);
5460 break;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04005461 case PRINT_BITMASK:
5462 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5463 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005464 case PRINT_TYPE:
5465 printf("(%s)", args->typecast.type);
5466 print_args(args->typecast.item);
5467 break;
5468 case PRINT_OP:
5469 if (strcmp(args->op.op, ":") == 0)
5470 print_paren = 0;
5471 if (print_paren)
5472 printf("(");
5473 print_args(args->op.left);
5474 printf(" %s ", args->op.op);
5475 print_args(args->op.right);
5476 if (print_paren)
5477 printf(")");
5478 break;
5479 default:
5480 /* we should warn... */
5481 return;
5482 }
5483 if (args->next) {
5484 printf("\n");
5485 print_args(args->next);
5486 }
5487}
5488
5489static void parse_header_field(const char *field,
5490 int *offset, int *size, int mandatory)
5491{
5492 unsigned long long save_input_buf_ptr;
5493 unsigned long long save_input_buf_siz;
5494 char *token;
5495 int type;
5496
5497 save_input_buf_ptr = input_buf_ptr;
5498 save_input_buf_siz = input_buf_siz;
5499
5500 if (read_expected(EVENT_ITEM, "field") < 0)
5501 return;
5502 if (read_expected(EVENT_OP, ":") < 0)
5503 return;
5504
5505 /* type */
5506 if (read_expect_type(EVENT_ITEM, &token) < 0)
5507 goto fail;
5508 free_token(token);
5509
5510 /*
5511 * If this is not a mandatory field, then test it first.
5512 */
5513 if (mandatory) {
5514 if (read_expected(EVENT_ITEM, field) < 0)
5515 return;
5516 } else {
5517 if (read_expect_type(EVENT_ITEM, &token) < 0)
5518 goto fail;
5519 if (strcmp(token, field) != 0)
5520 goto discard;
5521 free_token(token);
5522 }
5523
5524 if (read_expected(EVENT_OP, ";") < 0)
5525 return;
5526 if (read_expected(EVENT_ITEM, "offset") < 0)
5527 return;
5528 if (read_expected(EVENT_OP, ":") < 0)
5529 return;
5530 if (read_expect_type(EVENT_ITEM, &token) < 0)
5531 goto fail;
5532 *offset = atoi(token);
5533 free_token(token);
5534 if (read_expected(EVENT_OP, ";") < 0)
5535 return;
5536 if (read_expected(EVENT_ITEM, "size") < 0)
5537 return;
5538 if (read_expected(EVENT_OP, ":") < 0)
5539 return;
5540 if (read_expect_type(EVENT_ITEM, &token) < 0)
5541 goto fail;
5542 *size = atoi(token);
5543 free_token(token);
5544 if (read_expected(EVENT_OP, ";") < 0)
5545 return;
5546 type = read_token(&token);
5547 if (type != EVENT_NEWLINE) {
5548 /* newer versions of the kernel have a "signed" type */
5549 if (type != EVENT_ITEM)
5550 goto fail;
5551
5552 if (strcmp(token, "signed") != 0)
5553 goto fail;
5554
5555 free_token(token);
5556
5557 if (read_expected(EVENT_OP, ":") < 0)
5558 return;
5559
5560 if (read_expect_type(EVENT_ITEM, &token))
5561 goto fail;
5562
5563 free_token(token);
5564 if (read_expected(EVENT_OP, ";") < 0)
5565 return;
5566
5567 if (read_expect_type(EVENT_NEWLINE, &token))
5568 goto fail;
5569 }
5570 fail:
5571 free_token(token);
5572 return;
5573
5574 discard:
5575 input_buf_ptr = save_input_buf_ptr;
5576 input_buf_siz = save_input_buf_siz;
5577 *offset = 0;
5578 *size = 0;
5579 free_token(token);
5580}
5581
5582/**
5583 * pevent_parse_header_page - parse the data stored in the header page
5584 * @pevent: the handle to the pevent
5585 * @buf: the buffer storing the header page format string
5586 * @size: the size of @buf
5587 * @long_size: the long size to use if there is no header
5588 *
5589 * This parses the header page format for information on the
5590 * ring buffer used. The @buf should be copied from
5591 *
5592 * /sys/kernel/debug/tracing/events/header_page
5593 */
5594int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5595 int long_size)
5596{
5597 int ignore;
5598
5599 if (!size) {
5600 /*
5601 * Old kernels did not have header page info.
5602 * Sorry but we just use what we find here in user space.
5603 */
5604 pevent->header_page_ts_size = sizeof(long long);
5605 pevent->header_page_size_size = long_size;
5606 pevent->header_page_data_offset = sizeof(long long) + long_size;
5607 pevent->old_format = 1;
5608 return -1;
5609 }
5610 init_input_buf(buf, size);
5611
5612 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5613 &pevent->header_page_ts_size, 1);
5614 parse_header_field("commit", &pevent->header_page_size_offset,
5615 &pevent->header_page_size_size, 1);
5616 parse_header_field("overwrite", &pevent->header_page_overwrite,
5617 &ignore, 0);
5618 parse_header_field("data", &pevent->header_page_data_offset,
5619 &pevent->header_page_data_size, 1);
5620
5621 return 0;
5622}
5623
5624static int event_matches(struct event_format *event,
5625 int id, const char *sys_name,
5626 const char *event_name)
5627{
5628 if (id >= 0 && id != event->id)
5629 return 0;
5630
5631 if (event_name && (strcmp(event_name, event->name) != 0))
5632 return 0;
5633
5634 if (sys_name && (strcmp(sys_name, event->system) != 0))
5635 return 0;
5636
5637 return 1;
5638}
5639
5640static void free_handler(struct event_handler *handle)
5641{
5642 free((void *)handle->sys_name);
5643 free((void *)handle->event_name);
5644 free(handle);
5645}
5646
5647static int find_event_handle(struct pevent *pevent, struct event_format *event)
5648{
5649 struct event_handler *handle, **next;
5650
5651 for (next = &pevent->handlers; *next;
5652 next = &(*next)->next) {
5653 handle = *next;
5654 if (event_matches(event, handle->id,
5655 handle->sys_name,
5656 handle->event_name))
5657 break;
5658 }
5659
5660 if (!(*next))
5661 return 0;
5662
5663 pr_stat("overriding event (%d) %s:%s with new print handler",
5664 event->id, event->system, event->name);
5665
5666 event->handler = handle->func;
5667 event->context = handle->context;
5668
5669 *next = handle->next;
5670 free_handler(handle);
5671
5672 return 1;
5673}
5674
5675/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005676 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005677 * @buf: the buffer storing the event format string
5678 * @size: the size of @buf
5679 * @sys: the system the event belongs to
5680 *
5681 * This parses the event format and creates an event structure
5682 * to quickly parse raw data for a given event.
5683 *
5684 * These files currently come from:
5685 *
5686 * /sys/kernel/debug/tracing/events/.../.../format
5687 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005688enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5689 struct pevent *pevent, const char *buf,
5690 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005691{
5692 struct event_format *event;
5693 int ret;
5694
5695 init_input_buf(buf, size);
5696
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005697 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02005698 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09005699 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005700
5701 event->name = event_read_name();
5702 if (!event->name) {
5703 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09005704 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5705 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005706 }
5707
5708 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005709 event->flags |= EVENT_FL_ISFTRACE;
5710
5711 if (strcmp(event->name, "bprint") == 0)
5712 event->flags |= EVENT_FL_ISBPRINT;
5713 }
5714
5715 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005716 if (event->id < 0) {
5717 ret = PEVENT_ERRNO__READ_ID_FAILED;
5718 /*
5719 * This isn't an allocation error actually.
5720 * But as the ID is critical, just bail out.
5721 */
5722 goto event_alloc_failed;
5723 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005724
5725 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005726 if (!event->system) {
5727 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5728 goto event_alloc_failed;
5729 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005730
Steven Rostedt101782e2012-10-01 20:13:51 -04005731 /* Add pevent to event so that it can be referenced */
5732 event->pevent = pevent;
5733
Steven Rostedtf7d82352012-04-06 00:47:53 +02005734 ret = event_read_format(event);
5735 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005736 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5737 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005738 }
5739
5740 /*
5741 * If the event has an override, don't print warnings if the event
5742 * print format fails to parse.
5743 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005744 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005745 show_warning = 0;
5746
5747 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005748 show_warning = 1;
5749
Steven Rostedtf7d82352012-04-06 00:47:53 +02005750 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005751 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5752 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005753 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005754
5755 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5756 struct format_field *field;
5757 struct print_arg *arg, **list;
5758
5759 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005760 list = &event->print_fmt.args;
5761 for (field = event->format.fields; field; field = field->next) {
5762 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09005763 if (!arg) {
5764 event->flags |= EVENT_FL_FAILED;
5765 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5766 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005767 arg->type = PRINT_FIELD;
5768 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09005769 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09005770 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005771 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005772 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005773 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005774 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005775 *list = arg;
5776 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005777 }
5778 return 0;
5779 }
5780
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005781 return 0;
5782
5783 event_parse_failed:
5784 event->flags |= EVENT_FL_FAILED;
5785 return ret;
5786
5787 event_alloc_failed:
5788 free(event->system);
5789 free(event->name);
5790 free(event);
5791 *eventp = NULL;
5792 return ret;
5793}
5794
Jiri Olsa71ad9582013-12-03 14:09:19 +01005795static enum pevent_errno
5796__pevent_parse_event(struct pevent *pevent,
5797 struct event_format **eventp,
5798 const char *buf, unsigned long size,
5799 const char *sys)
5800{
5801 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5802 struct event_format *event = *eventp;
5803
5804 if (event == NULL)
5805 return ret;
5806
5807 if (pevent && add_event(pevent, event)) {
5808 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5809 goto event_add_failed;
5810 }
5811
5812#define PRINT_ARGS 0
5813 if (PRINT_ARGS && event->print_fmt.args)
5814 print_args(event->print_fmt.args);
5815
5816 return 0;
5817
5818event_add_failed:
5819 pevent_free_format(event);
5820 return ret;
5821}
5822
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005823/**
5824 * pevent_parse_format - parse the event format
Jiri Olsa71ad9582013-12-03 14:09:19 +01005825 * @pevent: the handle to the pevent
5826 * @eventp: returned format
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005827 * @buf: the buffer storing the event format string
5828 * @size: the size of @buf
5829 * @sys: the system the event belongs to
5830 *
5831 * This parses the event format and creates an event structure
5832 * to quickly parse raw data for a given event.
5833 *
5834 * These files currently come from:
5835 *
5836 * /sys/kernel/debug/tracing/events/.../.../format
5837 */
Jiri Olsa71ad9582013-12-03 14:09:19 +01005838enum pevent_errno pevent_parse_format(struct pevent *pevent,
5839 struct event_format **eventp,
5840 const char *buf,
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005841 unsigned long size, const char *sys)
5842{
Jiri Olsa71ad9582013-12-03 14:09:19 +01005843 return __pevent_parse_event(pevent, eventp, buf, size, sys);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005844}
5845
5846/**
5847 * pevent_parse_event - parse the event format
5848 * @pevent: the handle to the pevent
5849 * @buf: the buffer storing the event format string
5850 * @size: the size of @buf
5851 * @sys: the system the event belongs to
5852 *
5853 * This parses the event format and creates an event structure
5854 * to quickly parse raw data for a given event.
5855 *
5856 * These files currently come from:
5857 *
5858 * /sys/kernel/debug/tracing/events/.../.../format
5859 */
5860enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5861 unsigned long size, const char *sys)
5862{
5863 struct event_format *event = NULL;
Jiri Olsa71ad9582013-12-03 14:09:19 +01005864 return __pevent_parse_event(pevent, &event, buf, size, sys);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005865}
5866
Namhyung Kim2f197b92012-08-22 16:00:30 +09005867#undef _PE
5868#define _PE(code, str) str
5869static const char * const pevent_error_str[] = {
5870 PEVENT_ERRORS
5871};
5872#undef _PE
5873
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005874int pevent_strerror(struct pevent *pevent __maybe_unused,
5875 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005876{
5877 int idx;
5878 const char *msg;
5879
5880 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005881 msg = strerror_r(errnum, buf, buflen);
5882 if (msg != buf) {
5883 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005884 memcpy(buf, msg, min(buflen - 1, len));
5885 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005886 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005887 return 0;
5888 }
5889
5890 if (errnum <= __PEVENT_ERRNO__START ||
5891 errnum >= __PEVENT_ERRNO__END)
5892 return -1;
5893
Namhyung Kimf63fe792012-08-23 16:37:00 +09005894 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005895 msg = pevent_error_str[idx];
Namhyung Kimbf19b822013-12-12 16:36:17 +09005896 snprintf(buf, buflen, "%s", msg);
Namhyung Kim2f197b92012-08-22 16:00:30 +09005897
5898 return 0;
5899}
5900
Steven Rostedtf7d82352012-04-06 00:47:53 +02005901int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005902 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005903 unsigned long long *val, int err)
5904{
5905 if (!field) {
5906 if (err)
5907 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5908 return -1;
5909 }
5910
5911 if (pevent_read_number_field(field, record->data, val)) {
5912 if (err)
5913 trace_seq_printf(s, " %s=INVALID", name);
5914 return -1;
5915 }
5916
5917 return 0;
5918}
5919
5920/**
5921 * pevent_get_field_raw - return the raw pointer into the data field
5922 * @s: The seq to print to on error
5923 * @event: the event that the field is for
5924 * @name: The name of the field
5925 * @record: The record with the field name.
5926 * @len: place to store the field length.
5927 * @err: print default error if failed.
5928 *
5929 * Returns a pointer into record->data of the field and places
5930 * the length of the field in @len.
5931 *
5932 * On failure, it returns NULL.
5933 */
5934void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005935 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005936 int *len, int err)
5937{
5938 struct format_field *field;
5939 void *data = record->data;
5940 unsigned offset;
5941 int dummy;
5942
5943 if (!event)
5944 return NULL;
5945
5946 field = pevent_find_field(event, name);
5947
5948 if (!field) {
5949 if (err)
5950 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5951 return NULL;
5952 }
5953
5954 /* Allow @len to be NULL */
5955 if (!len)
5956 len = &dummy;
5957
5958 offset = field->offset;
5959 if (field->flags & FIELD_IS_DYNAMIC) {
5960 offset = pevent_read_number(event->pevent,
5961 data + offset, field->size);
5962 *len = offset >> 16;
5963 offset &= 0xffff;
5964 } else
5965 *len = field->size;
5966
5967 return data + offset;
5968}
5969
5970/**
5971 * pevent_get_field_val - find a field and return its value
5972 * @s: The seq to print to on error
5973 * @event: the event that the field is for
5974 * @name: The name of the field
5975 * @record: The record with the field name.
5976 * @val: place to store the value of the field.
5977 * @err: print default error if failed.
5978 *
5979 * Returns 0 on success -1 on field not found.
5980 */
5981int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005982 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005983 unsigned long long *val, int err)
5984{
5985 struct format_field *field;
5986
5987 if (!event)
5988 return -1;
5989
5990 field = pevent_find_field(event, name);
5991
5992 return get_field_val(s, field, name, record, val, err);
5993}
5994
5995/**
5996 * pevent_get_common_field_val - find a common field and return its value
5997 * @s: The seq to print to on error
5998 * @event: the event that the field is for
5999 * @name: The name of the field
6000 * @record: The record with the field name.
6001 * @val: place to store the value of the field.
6002 * @err: print default error if failed.
6003 *
6004 * Returns 0 on success -1 on field not found.
6005 */
6006int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006007 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006008 unsigned long long *val, int err)
6009{
6010 struct format_field *field;
6011
6012 if (!event)
6013 return -1;
6014
6015 field = pevent_find_common_field(event, name);
6016
6017 return get_field_val(s, field, name, record, val, err);
6018}
6019
6020/**
6021 * pevent_get_any_field_val - find a any field and return its value
6022 * @s: The seq to print to on error
6023 * @event: the event that the field is for
6024 * @name: The name of the field
6025 * @record: The record with the field name.
6026 * @val: place to store the value of the field.
6027 * @err: print default error if failed.
6028 *
6029 * Returns 0 on success -1 on field not found.
6030 */
6031int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02006032 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006033 unsigned long long *val, int err)
6034{
6035 struct format_field *field;
6036
6037 if (!event)
6038 return -1;
6039
6040 field = pevent_find_any_field(event, name);
6041
6042 return get_field_val(s, field, name, record, val, err);
6043}
6044
6045/**
6046 * pevent_print_num_field - print a field and a format
6047 * @s: The seq to print to
6048 * @fmt: The printf format to print the field with.
6049 * @event: the event that the field is for
6050 * @name: The name of the field
6051 * @record: The record with the field name.
6052 * @err: print default error if failed.
6053 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006054 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02006055 */
6056int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6057 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02006058 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006059{
6060 struct format_field *field = pevent_find_field(event, name);
6061 unsigned long long val;
6062
6063 if (!field)
6064 goto failed;
6065
6066 if (pevent_read_number_field(field, record->data, &val))
6067 goto failed;
6068
6069 return trace_seq_printf(s, fmt, val);
6070
6071 failed:
6072 if (err)
6073 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6074 return -1;
6075}
6076
Steven Rostedt6d862b82013-11-01 17:54:00 -04006077/**
6078 * pevent_print_func_field - print a field and a format for function pointers
6079 * @s: The seq to print to
6080 * @fmt: The printf format to print the field with.
6081 * @event: the event that the field is for
6082 * @name: The name of the field
6083 * @record: The record with the field name.
6084 * @err: print default error if failed.
6085 *
6086 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6087 */
6088int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6089 struct event_format *event, const char *name,
6090 struct pevent_record *record, int err)
6091{
6092 struct format_field *field = pevent_find_field(event, name);
6093 struct pevent *pevent = event->pevent;
6094 unsigned long long val;
6095 struct func_map *func;
6096 char tmp[128];
6097
6098 if (!field)
6099 goto failed;
6100
6101 if (pevent_read_number_field(field, record->data, &val))
6102 goto failed;
6103
6104 func = find_func(pevent, val);
6105
6106 if (func)
6107 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6108 else
6109 sprintf(tmp, "0x%08llx", val);
6110
6111 return trace_seq_printf(s, fmt, tmp);
6112
6113 failed:
6114 if (err)
6115 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6116 return -1;
6117}
6118
Steven Rostedtf7d82352012-04-06 00:47:53 +02006119static void free_func_handle(struct pevent_function_handler *func)
6120{
6121 struct pevent_func_params *params;
6122
6123 free(func->name);
6124
6125 while (func->params) {
6126 params = func->params;
6127 func->params = params->next;
6128 free(params);
6129 }
6130
6131 free(func);
6132}
6133
6134/**
6135 * pevent_register_print_function - register a helper function
6136 * @pevent: the handle to the pevent
6137 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006138 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02006139 * @name: the name of the helper function
6140 * @parameters: A list of enum pevent_func_arg_type
6141 *
6142 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006143 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02006144 * of these functions.
6145 *
6146 * The @parameters is a variable list of pevent_func_arg_type enums that
6147 * must end with PEVENT_FUNC_ARG_VOID.
6148 */
6149int pevent_register_print_function(struct pevent *pevent,
6150 pevent_func_handler func,
6151 enum pevent_func_arg_type ret_type,
6152 char *name, ...)
6153{
6154 struct pevent_function_handler *func_handle;
6155 struct pevent_func_params **next_param;
6156 struct pevent_func_params *param;
6157 enum pevent_func_arg_type type;
6158 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006159 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006160
6161 func_handle = find_func_handler(pevent, name);
6162 if (func_handle) {
6163 /*
6164 * This is most like caused by the users own
6165 * plugins updating the function. This overrides the
6166 * system defaults.
6167 */
6168 pr_stat("override of function helper '%s'", name);
6169 remove_func_handler(pevent, name);
6170 }
6171
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006172 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09006173 if (!func_handle) {
6174 do_warning("Failed to allocate function handler");
6175 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6176 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006177
6178 func_handle->ret_type = ret_type;
6179 func_handle->name = strdup(name);
6180 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006181 if (!func_handle->name) {
6182 do_warning("Failed to allocate function name");
6183 free(func_handle);
6184 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6185 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006186
6187 next_param = &(func_handle->params);
6188 va_start(ap, name);
6189 for (;;) {
6190 type = va_arg(ap, enum pevent_func_arg_type);
6191 if (type == PEVENT_FUNC_ARG_VOID)
6192 break;
6193
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03006194 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09006195 do_warning("Invalid argument type %d", type);
6196 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006197 goto out_free;
6198 }
6199
Namhyung Kim67ed9392012-09-07 11:49:47 +09006200 param = malloc(sizeof(*param));
6201 if (!param) {
6202 do_warning("Failed to allocate function param");
6203 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6204 goto out_free;
6205 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006206 param->type = type;
6207 param->next = NULL;
6208
6209 *next_param = param;
6210 next_param = &(param->next);
6211
6212 func_handle->nr_args++;
6213 }
6214 va_end(ap);
6215
6216 func_handle->next = pevent->func_handlers;
6217 pevent->func_handlers = func_handle;
6218
6219 return 0;
6220 out_free:
6221 va_end(ap);
6222 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09006223 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006224}
6225
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006226/**
6227 * pevent_unregister_print_function - unregister a helper function
6228 * @pevent: the handle to the pevent
6229 * @func: the function to process the helper function
6230 * @name: the name of the helper function
6231 *
6232 * This function removes existing print handler for function @name.
6233 *
6234 * Returns 0 if the handler was removed successully, -1 otherwise.
6235 */
6236int pevent_unregister_print_function(struct pevent *pevent,
6237 pevent_func_handler func, char *name)
6238{
6239 struct pevent_function_handler *func_handle;
6240
6241 func_handle = find_func_handler(pevent, name);
6242 if (func_handle && func_handle->func == func) {
6243 remove_func_handler(pevent, name);
6244 return 0;
6245 }
6246 return -1;
6247}
6248
Namhyung Kimad137012014-01-16 11:31:07 +09006249static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6250 const char *sys_name,
6251 const char *event_name)
6252{
6253 struct event_format *event;
6254
6255 if (id >= 0) {
6256 /* search by id */
6257 event = pevent_find_event(pevent, id);
6258 if (!event)
6259 return NULL;
6260 if (event_name && (strcmp(event_name, event->name) != 0))
6261 return NULL;
6262 if (sys_name && (strcmp(sys_name, event->system) != 0))
6263 return NULL;
6264 } else {
6265 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6266 if (!event)
6267 return NULL;
6268 }
6269 return event;
6270}
6271
Steven Rostedtf7d82352012-04-06 00:47:53 +02006272/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006273 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02006274 * @pevent: the handle to the pevent
6275 * @id: the id of the event to register
6276 * @sys_name: the system name the event belongs to
6277 * @event_name: the name of the event
6278 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006279 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02006280 *
6281 * This function allows a developer to override the parsing of
6282 * a given event. If for some reason the default print format
6283 * is not sufficient, this function will register a function
6284 * for an event to be used to parse the data instead.
6285 *
6286 * If @id is >= 0, then it is used to find the event.
6287 * else @sys_name and @event_name are used.
6288 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09006289int pevent_register_event_handler(struct pevent *pevent, int id,
6290 const char *sys_name, const char *event_name,
6291 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006292{
6293 struct event_format *event;
6294 struct event_handler *handle;
6295
Namhyung Kimad137012014-01-16 11:31:07 +09006296 event = pevent_search_event(pevent, id, sys_name, event_name);
6297 if (event == NULL)
6298 goto not_found;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006299
6300 pr_stat("overriding event (%d) %s:%s with new print handler",
6301 event->id, event->system, event->name);
6302
6303 event->handler = func;
6304 event->context = context;
6305 return 0;
6306
6307 not_found:
6308 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006309 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006310 if (!handle) {
6311 do_warning("Failed to allocate event handler");
6312 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6313 }
6314
Steven Rostedtf7d82352012-04-06 00:47:53 +02006315 handle->id = id;
6316 if (event_name)
6317 handle->event_name = strdup(event_name);
6318 if (sys_name)
6319 handle->sys_name = strdup(sys_name);
6320
Namhyung Kimca638582012-04-09 11:54:31 +09006321 if ((event_name && !handle->event_name) ||
6322 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006323 do_warning("Failed to allocate event/sys name");
6324 free((void *)handle->event_name);
6325 free((void *)handle->sys_name);
6326 free(handle);
6327 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006328 }
6329
Steven Rostedtf7d82352012-04-06 00:47:53 +02006330 handle->func = func;
6331 handle->next = pevent->handlers;
6332 pevent->handlers = handle;
6333 handle->context = context;
6334
6335 return -1;
6336}
6337
Namhyung Kimad137012014-01-16 11:31:07 +09006338static int handle_matches(struct event_handler *handler, int id,
6339 const char *sys_name, const char *event_name,
6340 pevent_event_handler_func func, void *context)
6341{
6342 if (id >= 0 && id != handler->id)
6343 return 0;
6344
6345 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6346 return 0;
6347
6348 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6349 return 0;
6350
6351 if (func != handler->func || context != handler->context)
6352 return 0;
6353
6354 return 1;
6355}
6356
6357/**
6358 * pevent_unregister_event_handler - unregister an existing event handler
6359 * @pevent: the handle to the pevent
6360 * @id: the id of the event to unregister
6361 * @sys_name: the system name the handler belongs to
6362 * @event_name: the name of the event handler
6363 * @func: the function to call to parse the event information
6364 * @context: the data to be passed to @func
6365 *
6366 * This function removes existing event handler (parser).
6367 *
6368 * If @id is >= 0, then it is used to find the event.
6369 * else @sys_name and @event_name are used.
6370 *
6371 * Returns 0 if handler was removed successfully, -1 if event was not found.
6372 */
6373int pevent_unregister_event_handler(struct pevent *pevent, int id,
6374 const char *sys_name, const char *event_name,
6375 pevent_event_handler_func func, void *context)
6376{
6377 struct event_format *event;
6378 struct event_handler *handle;
6379 struct event_handler **next;
6380
6381 event = pevent_search_event(pevent, id, sys_name, event_name);
6382 if (event == NULL)
6383 goto not_found;
6384
6385 if (event->handler == func && event->context == context) {
6386 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6387 event->id, event->system, event->name);
6388
6389 event->handler = NULL;
6390 event->context = NULL;
6391 return 0;
6392 }
6393
6394not_found:
6395 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6396 handle = *next;
6397 if (handle_matches(handle, id, sys_name, event_name,
6398 func, context))
6399 break;
6400 }
6401
6402 if (!(*next))
6403 return -1;
6404
6405 *next = handle->next;
6406 free_handler(handle);
6407
6408 return 0;
6409}
6410
Steven Rostedtf7d82352012-04-06 00:47:53 +02006411/**
6412 * pevent_alloc - create a pevent handle
6413 */
6414struct pevent *pevent_alloc(void)
6415{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006416 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02006417
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006418 if (pevent)
6419 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006420
6421 return pevent;
6422}
6423
6424void pevent_ref(struct pevent *pevent)
6425{
6426 pevent->ref_count++;
6427}
6428
David Ahern00ae1122015-03-19 12:36:21 -06006429void pevent_free_format_field(struct format_field *field)
6430{
6431 free(field->type);
6432 free(field->name);
6433 free(field);
6434}
6435
Steven Rostedtf7d82352012-04-06 00:47:53 +02006436static void free_format_fields(struct format_field *field)
6437{
6438 struct format_field *next;
6439
6440 while (field) {
6441 next = field->next;
David Ahern00ae1122015-03-19 12:36:21 -06006442 pevent_free_format_field(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006443 field = next;
6444 }
6445}
6446
6447static void free_formats(struct format *format)
6448{
6449 free_format_fields(format->common_fields);
6450 free_format_fields(format->fields);
6451}
6452
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006453void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006454{
6455 free(event->name);
6456 free(event->system);
6457
6458 free_formats(&event->format);
6459
6460 free(event->print_fmt.format);
6461 free_args(event->print_fmt.args);
6462
6463 free(event);
6464}
6465
6466/**
6467 * pevent_free - free a pevent handle
6468 * @pevent: the pevent handle to free
6469 */
6470void pevent_free(struct pevent *pevent)
6471{
Steven Rostedta2525a02012-04-06 00:48:02 +02006472 struct cmdline_list *cmdlist, *cmdnext;
6473 struct func_list *funclist, *funcnext;
6474 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006475 struct pevent_function_handler *func_handler;
6476 struct event_handler *handle;
6477 int i;
6478
Steven Rostedta2525a02012-04-06 00:48:02 +02006479 if (!pevent)
6480 return;
6481
6482 cmdlist = pevent->cmdlist;
6483 funclist = pevent->funclist;
6484 printklist = pevent->printklist;
6485
Steven Rostedtf7d82352012-04-06 00:47:53 +02006486 pevent->ref_count--;
6487 if (pevent->ref_count)
6488 return;
6489
6490 if (pevent->cmdlines) {
6491 for (i = 0; i < pevent->cmdline_count; i++)
6492 free(pevent->cmdlines[i].comm);
6493 free(pevent->cmdlines);
6494 }
6495
6496 while (cmdlist) {
6497 cmdnext = cmdlist->next;
6498 free(cmdlist->comm);
6499 free(cmdlist);
6500 cmdlist = cmdnext;
6501 }
6502
6503 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006504 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02006505 free(pevent->func_map[i].func);
6506 free(pevent->func_map[i].mod);
6507 }
6508 free(pevent->func_map);
6509 }
6510
6511 while (funclist) {
6512 funcnext = funclist->next;
6513 free(funclist->func);
6514 free(funclist->mod);
6515 free(funclist);
6516 funclist = funcnext;
6517 }
6518
6519 while (pevent->func_handlers) {
6520 func_handler = pevent->func_handlers;
6521 pevent->func_handlers = func_handler->next;
6522 free_func_handle(func_handler);
6523 }
6524
6525 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03006526 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006527 free(pevent->printk_map[i].printk);
6528 free(pevent->printk_map);
6529 }
6530
6531 while (printklist) {
6532 printknext = printklist->next;
6533 free(printklist->printk);
6534 free(printklist);
6535 printklist = printknext;
6536 }
6537
6538 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006539 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006540
6541 while (pevent->handlers) {
6542 handle = pevent->handlers;
6543 pevent->handlers = handle->next;
6544 free_handler(handle);
6545 }
6546
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -04006547 free(pevent->trace_clock);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006548 free(pevent->events);
6549 free(pevent->sort_events);
6550
6551 free(pevent);
6552}
6553
6554void pevent_unref(struct pevent *pevent)
6555{
6556 pevent_free(pevent);
6557}