blob: e1c743c5283434d9b728860a44f4c6f0d0083221 [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
35#include "event-parse.h"
Steven Rostedt668fe012012-04-06 00:47:55 +020036#include "event-utils.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020037
38static const char *input_buf;
39static unsigned long long input_buf_ptr;
40static unsigned long long input_buf_siz;
41
Tom Zanussi5205aec2012-04-06 00:47:58 +020042static int is_flag_field;
43static int is_symbolic_field;
44
Steven Rostedtf7d82352012-04-06 00:47:53 +020045static int show_warning = 1;
46
47#define do_warning(fmt, ...) \
48 do { \
49 if (show_warning) \
50 warning(fmt, ##__VA_ARGS__); \
51 } while (0)
52
53static void init_input_buf(const char *buf, unsigned long long size)
54{
55 input_buf = buf;
56 input_buf_siz = size;
57 input_buf_ptr = 0;
58}
59
60const char *pevent_get_input_buf(void)
61{
62 return input_buf;
63}
64
65unsigned long long pevent_get_input_buf_ptr(void)
66{
67 return input_buf_ptr;
68}
69
70struct event_handler {
71 struct event_handler *next;
72 int id;
73 const char *sys_name;
74 const char *event_name;
75 pevent_event_handler_func func;
76 void *context;
77};
78
79struct pevent_func_params {
80 struct pevent_func_params *next;
81 enum pevent_func_arg_type type;
82};
83
84struct pevent_function_handler {
85 struct pevent_function_handler *next;
86 enum pevent_func_arg_type ret_type;
87 char *name;
88 pevent_func_handler func;
89 struct pevent_func_params *params;
90 int nr_args;
91};
92
93static unsigned long long
94process_defined_func(struct trace_seq *s, void *data, int size,
95 struct event_format *event, struct print_arg *arg);
96
97static void free_func_handle(struct pevent_function_handler *func);
98
99/**
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
103 *
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
106 */
107void pevent_buffer_init(const char *buf, unsigned long long size)
108{
109 init_input_buf(buf, size);
110}
111
112void breakpoint(void)
113{
114 static int x;
115 x++;
116}
117
118struct print_arg *alloc_arg(void)
119{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300120 return calloc(1, sizeof(struct print_arg));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200121}
122
123struct cmdline {
124 char *comm;
125 int pid;
126};
127
128static int cmdline_cmp(const void *a, const void *b)
129{
130 const struct cmdline *ca = a;
131 const struct cmdline *cb = b;
132
133 if (ca->pid < cb->pid)
134 return -1;
135 if (ca->pid > cb->pid)
136 return 1;
137
138 return 0;
139}
140
141struct cmdline_list {
142 struct cmdline_list *next;
143 char *comm;
144 int pid;
145};
146
147static int cmdline_init(struct pevent *pevent)
148{
149 struct cmdline_list *cmdlist = pevent->cmdlist;
150 struct cmdline_list *item;
151 struct cmdline *cmdlines;
152 int i;
153
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300154 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
155 if (!cmdlines)
156 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200157
158 i = 0;
159 while (cmdlist) {
160 cmdlines[i].pid = cmdlist->pid;
161 cmdlines[i].comm = cmdlist->comm;
162 i++;
163 item = cmdlist;
164 cmdlist = cmdlist->next;
165 free(item);
166 }
167
168 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
169
170 pevent->cmdlines = cmdlines;
171 pevent->cmdlist = NULL;
172
173 return 0;
174}
175
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -0300176static const char *find_cmdline(struct pevent *pevent, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200177{
178 const struct cmdline *comm;
179 struct cmdline key;
180
181 if (!pid)
182 return "<idle>";
183
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300184 if (!pevent->cmdlines && cmdline_init(pevent))
185 return "<not enough memory for cmdlines!>";
Steven Rostedtf7d82352012-04-06 00:47:53 +0200186
187 key.pid = pid;
188
189 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
190 sizeof(*pevent->cmdlines), cmdline_cmp);
191
192 if (comm)
193 return comm->comm;
194 return "<...>";
195}
196
197/**
198 * pevent_pid_is_registered - return if a pid has a cmdline registered
199 * @pevent: handle for the pevent
200 * @pid: The pid to check if it has a cmdline registered with.
201 *
202 * Returns 1 if the pid has a cmdline mapped to it
203 * 0 otherwise.
204 */
205int pevent_pid_is_registered(struct pevent *pevent, int pid)
206{
207 const struct cmdline *comm;
208 struct cmdline key;
209
210 if (!pid)
211 return 1;
212
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300213 if (!pevent->cmdlines && cmdline_init(pevent))
214 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200215
216 key.pid = pid;
217
218 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
219 sizeof(*pevent->cmdlines), cmdline_cmp);
220
221 if (comm)
222 return 1;
223 return 0;
224}
225
226/*
227 * If the command lines have been converted to an array, then
228 * we must add this pid. This is much slower than when cmdlines
229 * are added before the array is initialized.
230 */
231static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
232{
233 struct cmdline *cmdlines = pevent->cmdlines;
234 const struct cmdline *cmdline;
235 struct cmdline key;
236
237 if (!pid)
238 return 0;
239
240 /* avoid duplicates */
241 key.pid = pid;
242
243 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
244 sizeof(*pevent->cmdlines), cmdline_cmp);
245 if (cmdline) {
246 errno = EEXIST;
247 return -1;
248 }
249
250 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
251 if (!cmdlines) {
252 errno = ENOMEM;
253 return -1;
254 }
255
Steven Rostedtf7d82352012-04-06 00:47:53 +0200256 cmdlines[pevent->cmdline_count].comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300257 if (!cmdlines[pevent->cmdline_count].comm) {
258 free(cmdlines);
259 errno = ENOMEM;
260 return -1;
261 }
262
263 cmdlines[pevent->cmdline_count].pid = pid;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200264
265 if (cmdlines[pevent->cmdline_count].comm)
266 pevent->cmdline_count++;
267
268 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
269 pevent->cmdlines = cmdlines;
270
271 return 0;
272}
273
274/**
275 * pevent_register_comm - register a pid / comm mapping
276 * @pevent: handle for the pevent
277 * @comm: the command line to register
278 * @pid: the pid to map the command line to
279 *
280 * This adds a mapping to search for command line names with
281 * a given pid. The comm is duplicated.
282 */
283int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
284{
285 struct cmdline_list *item;
286
287 if (pevent->cmdlines)
288 return add_new_comm(pevent, comm, pid);
289
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300290 item = malloc(sizeof(*item));
291 if (!item)
292 return -1;
293
Steven Rostedtf7d82352012-04-06 00:47:53 +0200294 item->comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300295 if (!item->comm) {
296 free(item);
297 return -1;
298 }
Steven Rostedtf7d82352012-04-06 00:47:53 +0200299 item->pid = pid;
300 item->next = pevent->cmdlist;
301
302 pevent->cmdlist = item;
303 pevent->cmdline_count++;
304
305 return 0;
306}
307
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400308void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
309{
310 pevent->trace_clock = trace_clock;
311}
312
Steven Rostedtf7d82352012-04-06 00:47:53 +0200313struct func_map {
314 unsigned long long addr;
315 char *func;
316 char *mod;
317};
318
319struct func_list {
320 struct func_list *next;
321 unsigned long long addr;
322 char *func;
323 char *mod;
324};
325
326static int func_cmp(const void *a, const void *b)
327{
328 const struct func_map *fa = a;
329 const struct func_map *fb = b;
330
331 if (fa->addr < fb->addr)
332 return -1;
333 if (fa->addr > fb->addr)
334 return 1;
335
336 return 0;
337}
338
339/*
340 * We are searching for a record in between, not an exact
341 * match.
342 */
343static int func_bcmp(const void *a, const void *b)
344{
345 const struct func_map *fa = a;
346 const struct func_map *fb = b;
347
348 if ((fa->addr == fb->addr) ||
349
350 (fa->addr > fb->addr &&
351 fa->addr < (fb+1)->addr))
352 return 0;
353
354 if (fa->addr < fb->addr)
355 return -1;
356
357 return 1;
358}
359
360static int func_map_init(struct pevent *pevent)
361{
362 struct func_list *funclist;
363 struct func_list *item;
364 struct func_map *func_map;
365 int i;
366
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300367 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
368 if (!func_map)
369 return -1;
370
Steven Rostedtf7d82352012-04-06 00:47:53 +0200371 funclist = pevent->funclist;
372
373 i = 0;
374 while (funclist) {
375 func_map[i].func = funclist->func;
376 func_map[i].addr = funclist->addr;
377 func_map[i].mod = funclist->mod;
378 i++;
379 item = funclist;
380 funclist = funclist->next;
381 free(item);
382 }
383
384 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
385
386 /*
387 * Add a special record at the end.
388 */
389 func_map[pevent->func_count].func = NULL;
390 func_map[pevent->func_count].addr = 0;
391 func_map[pevent->func_count].mod = NULL;
392
393 pevent->func_map = func_map;
394 pevent->funclist = NULL;
395
396 return 0;
397}
398
399static struct func_map *
400find_func(struct pevent *pevent, unsigned long long addr)
401{
402 struct func_map *func;
403 struct func_map key;
404
405 if (!pevent->func_map)
406 func_map_init(pevent);
407
408 key.addr = addr;
409
410 func = bsearch(&key, pevent->func_map, pevent->func_count,
411 sizeof(*pevent->func_map), func_bcmp);
412
413 return func;
414}
415
416/**
417 * pevent_find_function - find a function by a given address
418 * @pevent: handle for the pevent
419 * @addr: the address to find the function with
420 *
421 * Returns a pointer to the function stored that has the given
422 * address. Note, the address does not have to be exact, it
423 * will select the function that would contain the address.
424 */
425const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
426{
427 struct func_map *map;
428
429 map = find_func(pevent, addr);
430 if (!map)
431 return NULL;
432
433 return map->func;
434}
435
436/**
437 * pevent_find_function_address - find a function address by a given address
438 * @pevent: handle for the pevent
439 * @addr: the address to find the function with
440 *
441 * Returns the address the function starts at. This can be used in
442 * conjunction with pevent_find_function to print both the function
443 * name and the function offset.
444 */
445unsigned long long
446pevent_find_function_address(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 0;
453
454 return map->addr;
455}
456
457/**
458 * pevent_register_function - register a function with a given address
459 * @pevent: handle for the pevent
460 * @function: the function name to register
461 * @addr: the address the function starts at
462 * @mod: the kernel module the function may be in (NULL for none)
463 *
464 * This registers a function name with an address and module.
465 * The @func passed in is duplicated.
466 */
467int pevent_register_function(struct pevent *pevent, char *func,
468 unsigned long long addr, char *mod)
469{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300470 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200471
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300472 if (!item)
473 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200474
475 item->next = pevent->funclist;
476 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300477 if (!item->func)
478 goto out_free;
479
480 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200481 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300482 if (!item->mod)
483 goto out_free_func;
484 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200485 item->mod = NULL;
486 item->addr = addr;
487
Namhyung Kimca638582012-04-09 11:54:31 +0900488 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200489 pevent->func_count++;
490
491 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300492
493out_free_func:
494 free(item->func);
495 item->func = NULL;
496out_free:
497 free(item);
498 errno = ENOMEM;
499 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200500}
501
502/**
503 * pevent_print_funcs - print out the stored functions
504 * @pevent: handle for the pevent
505 *
506 * This prints out the stored functions.
507 */
508void pevent_print_funcs(struct pevent *pevent)
509{
510 int i;
511
512 if (!pevent->func_map)
513 func_map_init(pevent);
514
515 for (i = 0; i < (int)pevent->func_count; i++) {
516 printf("%016llx %s",
517 pevent->func_map[i].addr,
518 pevent->func_map[i].func);
519 if (pevent->func_map[i].mod)
520 printf(" [%s]\n", pevent->func_map[i].mod);
521 else
522 printf("\n");
523 }
524}
525
526struct printk_map {
527 unsigned long long addr;
528 char *printk;
529};
530
531struct printk_list {
532 struct printk_list *next;
533 unsigned long long addr;
534 char *printk;
535};
536
537static int printk_cmp(const void *a, const void *b)
538{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900539 const struct printk_map *pa = a;
540 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200541
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900542 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200543 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900544 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200545 return 1;
546
547 return 0;
548}
549
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300550static int printk_map_init(struct pevent *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200551{
552 struct printk_list *printklist;
553 struct printk_list *item;
554 struct printk_map *printk_map;
555 int i;
556
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300557 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
558 if (!printk_map)
559 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200560
561 printklist = pevent->printklist;
562
563 i = 0;
564 while (printklist) {
565 printk_map[i].printk = printklist->printk;
566 printk_map[i].addr = printklist->addr;
567 i++;
568 item = printklist;
569 printklist = printklist->next;
570 free(item);
571 }
572
573 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
574
575 pevent->printk_map = printk_map;
576 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300577
578 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200579}
580
581static struct printk_map *
582find_printk(struct pevent *pevent, unsigned long long addr)
583{
584 struct printk_map *printk;
585 struct printk_map key;
586
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300587 if (!pevent->printk_map && printk_map_init(pevent))
588 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200589
590 key.addr = addr;
591
592 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
593 sizeof(*pevent->printk_map), printk_cmp);
594
595 return printk;
596}
597
598/**
599 * pevent_register_print_string - register a string by its address
600 * @pevent: handle for the pevent
601 * @fmt: the string format to register
602 * @addr: the address the string was located at
603 *
604 * This registers a string by the address it was stored in the kernel.
605 * The @fmt passed in is duplicated.
606 */
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400607int pevent_register_print_string(struct pevent *pevent, const char *fmt,
Steven Rostedtf7d82352012-04-06 00:47:53 +0200608 unsigned long long addr)
609{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300610 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400611 char *p;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200612
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300613 if (!item)
614 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200615
616 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200617 item->addr = addr;
618
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400619 /* Strip off quotes and '\n' from the end */
620 if (fmt[0] == '"')
621 fmt++;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300622 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900623 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300624 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900625
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400626 p = item->printk + strlen(item->printk) - 1;
627 if (*p == '"')
628 *p = 0;
629
630 p -= 2;
631 if (strcmp(p, "\\n") == 0)
632 *p = 0;
633
Namhyung Kimca638582012-04-09 11:54:31 +0900634 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200635 pevent->printk_count++;
636
637 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300638
639out_free:
640 free(item);
641 errno = ENOMEM;
642 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200643}
644
645/**
646 * pevent_print_printk - print out the stored strings
647 * @pevent: handle for the pevent
648 *
649 * This prints the string formats that were stored.
650 */
651void pevent_print_printk(struct pevent *pevent)
652{
653 int i;
654
655 if (!pevent->printk_map)
656 printk_map_init(pevent);
657
658 for (i = 0; i < (int)pevent->printk_count; i++) {
659 printf("%016llx %s\n",
660 pevent->printk_map[i].addr,
661 pevent->printk_map[i].printk);
662 }
663}
664
665static struct event_format *alloc_event(void)
666{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300667 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200668}
669
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300670static int add_event(struct pevent *pevent, struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200671{
672 int i;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300673 struct event_format **events = realloc(pevent->events, sizeof(event) *
674 (pevent->nr_events + 1));
675 if (!events)
676 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200677
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300678 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200679
680 for (i = 0; i < pevent->nr_events; i++) {
681 if (pevent->events[i]->id > event->id)
682 break;
683 }
684 if (i < pevent->nr_events)
685 memmove(&pevent->events[i + 1],
686 &pevent->events[i],
687 sizeof(event) * (pevent->nr_events - i));
688
689 pevent->events[i] = event;
690 pevent->nr_events++;
691
692 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300693
694 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200695}
696
697static int event_item_type(enum event_type type)
698{
699 switch (type) {
700 case EVENT_ITEM ... EVENT_SQUOTE:
701 return 1;
702 case EVENT_ERROR ... EVENT_DELIM:
703 default:
704 return 0;
705 }
706}
707
708static void free_flag_sym(struct print_flag_sym *fsym)
709{
710 struct print_flag_sym *next;
711
712 while (fsym) {
713 next = fsym->next;
714 free(fsym->value);
715 free(fsym->str);
716 free(fsym);
717 fsym = next;
718 }
719}
720
721static void free_arg(struct print_arg *arg)
722{
723 struct print_arg *farg;
724
725 if (!arg)
726 return;
727
728 switch (arg->type) {
729 case PRINT_ATOM:
730 free(arg->atom.atom);
731 break;
732 case PRINT_FIELD:
733 free(arg->field.name);
734 break;
735 case PRINT_FLAGS:
736 free_arg(arg->flags.field);
737 free(arg->flags.delim);
738 free_flag_sym(arg->flags.flags);
739 break;
740 case PRINT_SYMBOL:
741 free_arg(arg->symbol.field);
742 free_flag_sym(arg->symbol.symbols);
743 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900744 case PRINT_HEX:
745 free_arg(arg->hex.field);
746 free_arg(arg->hex.size);
747 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200748 case PRINT_TYPE:
749 free(arg->typecast.type);
750 free_arg(arg->typecast.item);
751 break;
752 case PRINT_STRING:
753 case PRINT_BSTRING:
754 free(arg->string.string);
755 break;
756 case PRINT_DYNAMIC_ARRAY:
757 free(arg->dynarray.index);
758 break;
759 case PRINT_OP:
760 free(arg->op.op);
761 free_arg(arg->op.left);
762 free_arg(arg->op.right);
763 break;
764 case PRINT_FUNC:
765 while (arg->func.args) {
766 farg = arg->func.args;
767 arg->func.args = farg->next;
768 free_arg(farg);
769 }
770 break;
771
772 case PRINT_NULL:
773 default:
774 break;
775 }
776
777 free(arg);
778}
779
780static enum event_type get_type(int ch)
781{
782 if (ch == '\n')
783 return EVENT_NEWLINE;
784 if (isspace(ch))
785 return EVENT_SPACE;
786 if (isalnum(ch) || ch == '_')
787 return EVENT_ITEM;
788 if (ch == '\'')
789 return EVENT_SQUOTE;
790 if (ch == '"')
791 return EVENT_DQUOTE;
792 if (!isprint(ch))
793 return EVENT_NONE;
794 if (ch == '(' || ch == ')' || ch == ',')
795 return EVENT_DELIM;
796
797 return EVENT_OP;
798}
799
800static int __read_char(void)
801{
802 if (input_buf_ptr >= input_buf_siz)
803 return -1;
804
805 return input_buf[input_buf_ptr++];
806}
807
808static int __peek_char(void)
809{
810 if (input_buf_ptr >= input_buf_siz)
811 return -1;
812
813 return input_buf[input_buf_ptr];
814}
815
816/**
817 * pevent_peek_char - peek at the next character that will be read
818 *
819 * Returns the next character read, or -1 if end of buffer.
820 */
821int pevent_peek_char(void)
822{
823 return __peek_char();
824}
825
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900826static int extend_token(char **tok, char *buf, int size)
827{
828 char *newtok = realloc(*tok, size);
829
830 if (!newtok) {
831 free(*tok);
832 *tok = NULL;
833 return -1;
834 }
835
836 if (!*tok)
837 strcpy(newtok, buf);
838 else
839 strcat(newtok, buf);
840 *tok = newtok;
841
842 return 0;
843}
844
Steven Rostedtf7d82352012-04-06 00:47:53 +0200845static enum event_type force_token(const char *str, char **tok);
846
847static enum event_type __read_token(char **tok)
848{
849 char buf[BUFSIZ];
850 int ch, last_ch, quote_ch, next_ch;
851 int i = 0;
852 int tok_size = 0;
853 enum event_type type;
854
855 *tok = NULL;
856
857
858 ch = __read_char();
859 if (ch < 0)
860 return EVENT_NONE;
861
862 type = get_type(ch);
863 if (type == EVENT_NONE)
864 return type;
865
866 buf[i++] = ch;
867
868 switch (type) {
869 case EVENT_NEWLINE:
870 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300871 if (asprintf(tok, "%c", ch) < 0)
872 return EVENT_ERROR;
873
Steven Rostedtf7d82352012-04-06 00:47:53 +0200874 return type;
875
876 case EVENT_OP:
877 switch (ch) {
878 case '-':
879 next_ch = __peek_char();
880 if (next_ch == '>') {
881 buf[i++] = __read_char();
882 break;
883 }
884 /* fall through */
885 case '+':
886 case '|':
887 case '&':
888 case '>':
889 case '<':
890 last_ch = ch;
891 ch = __peek_char();
892 if (ch != last_ch)
893 goto test_equal;
894 buf[i++] = __read_char();
895 switch (last_ch) {
896 case '>':
897 case '<':
898 goto test_equal;
899 default:
900 break;
901 }
902 break;
903 case '!':
904 case '=':
905 goto test_equal;
906 default: /* what should we do instead? */
907 break;
908 }
909 buf[i] = 0;
910 *tok = strdup(buf);
911 return type;
912
913 test_equal:
914 ch = __peek_char();
915 if (ch == '=')
916 buf[i++] = __read_char();
917 goto out;
918
919 case EVENT_DQUOTE:
920 case EVENT_SQUOTE:
921 /* don't keep quotes */
922 i--;
923 quote_ch = ch;
924 last_ch = 0;
925 concat:
926 do {
927 if (i == (BUFSIZ - 1)) {
928 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200929 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900930
931 if (extend_token(tok, buf, tok_size) < 0)
932 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200933 i = 0;
934 }
935 last_ch = ch;
936 ch = __read_char();
937 buf[i++] = ch;
938 /* the '\' '\' will cancel itself */
939 if (ch == '\\' && last_ch == '\\')
940 last_ch = 0;
941 } while (ch != quote_ch || last_ch == '\\');
942 /* remove the last quote */
943 i--;
944
945 /*
946 * For strings (double quotes) check the next token.
947 * If it is another string, concatinate the two.
948 */
949 if (type == EVENT_DQUOTE) {
950 unsigned long long save_input_buf_ptr = input_buf_ptr;
951
952 do {
953 ch = __read_char();
954 } while (isspace(ch));
955 if (ch == '"')
956 goto concat;
957 input_buf_ptr = save_input_buf_ptr;
958 }
959
960 goto out;
961
962 case EVENT_ERROR ... EVENT_SPACE:
963 case EVENT_ITEM:
964 default:
965 break;
966 }
967
968 while (get_type(__peek_char()) == type) {
969 if (i == (BUFSIZ - 1)) {
970 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200971 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900972
973 if (extend_token(tok, buf, tok_size) < 0)
974 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200975 i = 0;
976 }
977 ch = __read_char();
978 buf[i++] = ch;
979 }
980
981 out:
982 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900983 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200984 return EVENT_NONE;
985
986 if (type == EVENT_ITEM) {
987 /*
988 * Older versions of the kernel has a bug that
989 * creates invalid symbols and will break the mac80211
990 * parsing. This is a work around to that bug.
991 *
992 * See Linux kernel commit:
993 * 811cb50baf63461ce0bdb234927046131fc7fa8b
994 */
995 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
996 free(*tok);
997 *tok = NULL;
998 return force_token("\"\%s\" ", tok);
999 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1000 free(*tok);
1001 *tok = NULL;
1002 return force_token("\" sta:%pM\" ", tok);
1003 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1004 free(*tok);
1005 *tok = NULL;
1006 return force_token("\" vif:%p(%d)\" ", tok);
1007 }
1008 }
1009
1010 return type;
1011}
1012
1013static enum event_type force_token(const char *str, char **tok)
1014{
1015 const char *save_input_buf;
1016 unsigned long long save_input_buf_ptr;
1017 unsigned long long save_input_buf_siz;
1018 enum event_type type;
1019
1020 /* save off the current input pointers */
1021 save_input_buf = input_buf;
1022 save_input_buf_ptr = input_buf_ptr;
1023 save_input_buf_siz = input_buf_siz;
1024
1025 init_input_buf(str, strlen(str));
1026
1027 type = __read_token(tok);
1028
1029 /* reset back to original token */
1030 input_buf = save_input_buf;
1031 input_buf_ptr = save_input_buf_ptr;
1032 input_buf_siz = save_input_buf_siz;
1033
1034 return type;
1035}
1036
1037static void free_token(char *tok)
1038{
1039 if (tok)
1040 free(tok);
1041}
1042
1043static enum event_type read_token(char **tok)
1044{
1045 enum event_type type;
1046
1047 for (;;) {
1048 type = __read_token(tok);
1049 if (type != EVENT_SPACE)
1050 return type;
1051
1052 free_token(*tok);
1053 }
1054
1055 /* not reached */
1056 *tok = NULL;
1057 return EVENT_NONE;
1058}
1059
1060/**
1061 * pevent_read_token - access to utilites to use the pevent parser
1062 * @tok: The token to return
1063 *
1064 * This will parse tokens from the string given by
1065 * pevent_init_data().
1066 *
1067 * Returns the token type.
1068 */
1069enum event_type pevent_read_token(char **tok)
1070{
1071 return read_token(tok);
1072}
1073
1074/**
1075 * pevent_free_token - free a token returned by pevent_read_token
1076 * @token: the token to free
1077 */
1078void pevent_free_token(char *token)
1079{
1080 free_token(token);
1081}
1082
1083/* no newline */
1084static enum event_type read_token_item(char **tok)
1085{
1086 enum event_type type;
1087
1088 for (;;) {
1089 type = __read_token(tok);
1090 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1091 return type;
1092 free_token(*tok);
1093 *tok = NULL;
1094 }
1095
1096 /* not reached */
1097 *tok = NULL;
1098 return EVENT_NONE;
1099}
1100
1101static int test_type(enum event_type type, enum event_type expect)
1102{
1103 if (type != expect) {
1104 do_warning("Error: expected type %d but read %d",
1105 expect, type);
1106 return -1;
1107 }
1108 return 0;
1109}
1110
1111static int test_type_token(enum event_type type, const char *token,
1112 enum event_type expect, const char *expect_tok)
1113{
1114 if (type != expect) {
1115 do_warning("Error: expected type %d but read %d",
1116 expect, type);
1117 return -1;
1118 }
1119
1120 if (strcmp(token, expect_tok) != 0) {
1121 do_warning("Error: expected '%s' but read '%s'",
1122 expect_tok, token);
1123 return -1;
1124 }
1125 return 0;
1126}
1127
1128static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1129{
1130 enum event_type type;
1131
1132 if (newline_ok)
1133 type = read_token(tok);
1134 else
1135 type = read_token_item(tok);
1136 return test_type(type, expect);
1137}
1138
1139static int read_expect_type(enum event_type expect, char **tok)
1140{
1141 return __read_expect_type(expect, tok, 1);
1142}
1143
1144static int __read_expected(enum event_type expect, const char *str,
1145 int newline_ok)
1146{
1147 enum event_type type;
1148 char *token;
1149 int ret;
1150
1151 if (newline_ok)
1152 type = read_token(&token);
1153 else
1154 type = read_token_item(&token);
1155
1156 ret = test_type_token(type, token, expect, str);
1157
1158 free_token(token);
1159
1160 return ret;
1161}
1162
1163static int read_expected(enum event_type expect, const char *str)
1164{
1165 return __read_expected(expect, str, 1);
1166}
1167
1168static int read_expected_item(enum event_type expect, const char *str)
1169{
1170 return __read_expected(expect, str, 0);
1171}
1172
1173static char *event_read_name(void)
1174{
1175 char *token;
1176
1177 if (read_expected(EVENT_ITEM, "name") < 0)
1178 return NULL;
1179
1180 if (read_expected(EVENT_OP, ":") < 0)
1181 return NULL;
1182
1183 if (read_expect_type(EVENT_ITEM, &token) < 0)
1184 goto fail;
1185
1186 return token;
1187
1188 fail:
1189 free_token(token);
1190 return NULL;
1191}
1192
1193static int event_read_id(void)
1194{
1195 char *token;
1196 int id;
1197
1198 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1199 return -1;
1200
1201 if (read_expected(EVENT_OP, ":") < 0)
1202 return -1;
1203
1204 if (read_expect_type(EVENT_ITEM, &token) < 0)
1205 goto fail;
1206
1207 id = strtoul(token, NULL, 0);
1208 free_token(token);
1209 return id;
1210
1211 fail:
1212 free_token(token);
1213 return -1;
1214}
1215
1216static int field_is_string(struct format_field *field)
1217{
1218 if ((field->flags & FIELD_IS_ARRAY) &&
1219 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1220 strstr(field->type, "s8")))
1221 return 1;
1222
1223 return 0;
1224}
1225
1226static int field_is_dynamic(struct format_field *field)
1227{
1228 if (strncmp(field->type, "__data_loc", 10) == 0)
1229 return 1;
1230
1231 return 0;
1232}
1233
1234static int field_is_long(struct format_field *field)
1235{
1236 /* includes long long */
1237 if (strstr(field->type, "long"))
1238 return 1;
1239
1240 return 0;
1241}
1242
Jiri Olsae23c1a52013-01-24 21:46:43 +01001243static unsigned int type_size(const char *name)
1244{
1245 /* This covers all FIELD_IS_STRING types. */
1246 static struct {
1247 const char *type;
1248 unsigned int size;
1249 } table[] = {
1250 { "u8", 1 },
1251 { "u16", 2 },
1252 { "u32", 4 },
1253 { "u64", 8 },
1254 { "s8", 1 },
1255 { "s16", 2 },
1256 { "s32", 4 },
1257 { "s64", 8 },
1258 { "char", 1 },
1259 { },
1260 };
1261 int i;
1262
1263 for (i = 0; table[i].type; i++) {
1264 if (!strcmp(table[i].type, name))
1265 return table[i].size;
1266 }
1267
1268 return 0;
1269}
1270
Steven Rostedtf7d82352012-04-06 00:47:53 +02001271static int event_read_fields(struct event_format *event, struct format_field **fields)
1272{
1273 struct format_field *field = NULL;
1274 enum event_type type;
1275 char *token;
1276 char *last_token;
1277 int count = 0;
1278
1279 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001280 unsigned int size_dynamic = 0;
1281
Steven Rostedtf7d82352012-04-06 00:47:53 +02001282 type = read_token(&token);
1283 if (type == EVENT_NEWLINE) {
1284 free_token(token);
1285 return count;
1286 }
1287
1288 count++;
1289
1290 if (test_type_token(type, token, EVENT_ITEM, "field"))
1291 goto fail;
1292 free_token(token);
1293
1294 type = read_token(&token);
1295 /*
1296 * The ftrace fields may still use the "special" name.
1297 * Just ignore it.
1298 */
1299 if (event->flags & EVENT_FL_ISFTRACE &&
1300 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1301 free_token(token);
1302 type = read_token(&token);
1303 }
1304
1305 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1306 goto fail;
1307
1308 free_token(token);
1309 if (read_expect_type(EVENT_ITEM, &token) < 0)
1310 goto fail;
1311
1312 last_token = token;
1313
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001314 field = calloc(1, sizeof(*field));
1315 if (!field)
1316 goto fail;
1317
Steven Rostedtf7d82352012-04-06 00:47:53 +02001318 field->event = event;
1319
1320 /* read the rest of the type */
1321 for (;;) {
1322 type = read_token(&token);
1323 if (type == EVENT_ITEM ||
1324 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1325 /*
1326 * Some of the ftrace fields are broken and have
1327 * an illegal "." in them.
1328 */
1329 (event->flags & EVENT_FL_ISFTRACE &&
1330 type == EVENT_OP && strcmp(token, ".") == 0)) {
1331
1332 if (strcmp(token, "*") == 0)
1333 field->flags |= FIELD_IS_POINTER;
1334
1335 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001336 char *new_type;
1337 new_type = realloc(field->type,
1338 strlen(field->type) +
1339 strlen(last_token) + 2);
1340 if (!new_type) {
1341 free(last_token);
1342 goto fail;
1343 }
1344 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001345 strcat(field->type, " ");
1346 strcat(field->type, last_token);
1347 free(last_token);
1348 } else
1349 field->type = last_token;
1350 last_token = token;
1351 continue;
1352 }
1353
1354 break;
1355 }
1356
1357 if (!field->type) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001358 do_warning("%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001359 goto fail;
1360 }
1361 field->name = last_token;
1362
1363 if (test_type(type, EVENT_OP))
1364 goto fail;
1365
1366 if (strcmp(token, "[") == 0) {
1367 enum event_type last_type = type;
1368 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001369 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001370 int len;
1371
1372 field->flags |= FIELD_IS_ARRAY;
1373
1374 type = read_token(&token);
1375
1376 if (type == EVENT_ITEM)
1377 field->arraylen = strtoul(token, NULL, 0);
1378 else
1379 field->arraylen = 0;
1380
1381 while (strcmp(token, "]") != 0) {
1382 if (last_type == EVENT_ITEM &&
1383 type == EVENT_ITEM)
1384 len = 2;
1385 else
1386 len = 1;
1387 last_type = type;
1388
Namhyung Kimd2864472012-04-09 11:54:33 +09001389 new_brackets = realloc(brackets,
1390 strlen(brackets) +
1391 strlen(token) + len);
1392 if (!new_brackets) {
1393 free(brackets);
1394 goto fail;
1395 }
1396 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001397 if (len == 2)
1398 strcat(brackets, " ");
1399 strcat(brackets, token);
1400 /* We only care about the last token */
1401 field->arraylen = strtoul(token, NULL, 0);
1402 free_token(token);
1403 type = read_token(&token);
1404 if (type == EVENT_NONE) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001405 do_warning("failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001406 goto fail;
1407 }
1408 }
1409
1410 free_token(token);
1411
Namhyung Kimd2864472012-04-09 11:54:33 +09001412 new_brackets = realloc(brackets, strlen(brackets) + 2);
1413 if (!new_brackets) {
1414 free(brackets);
1415 goto fail;
1416 }
1417 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001418 strcat(brackets, "]");
1419
1420 /* add brackets to type */
1421
1422 type = read_token(&token);
1423 /*
1424 * If the next token is not an OP, then it is of
1425 * the format: type [] item;
1426 */
1427 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001428 char *new_type;
1429 new_type = realloc(field->type,
1430 strlen(field->type) +
1431 strlen(field->name) +
1432 strlen(brackets) + 2);
1433 if (!new_type) {
1434 free(brackets);
1435 goto fail;
1436 }
1437 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001438 strcat(field->type, " ");
1439 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001440 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001441 free_token(field->name);
1442 strcat(field->type, brackets);
1443 field->name = token;
1444 type = read_token(&token);
1445 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001446 char *new_type;
1447 new_type = realloc(field->type,
1448 strlen(field->type) +
1449 strlen(brackets) + 1);
1450 if (!new_type) {
1451 free(brackets);
1452 goto fail;
1453 }
1454 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001455 strcat(field->type, brackets);
1456 }
1457 free(brackets);
1458 }
1459
1460 if (field_is_string(field))
1461 field->flags |= FIELD_IS_STRING;
1462 if (field_is_dynamic(field))
1463 field->flags |= FIELD_IS_DYNAMIC;
1464 if (field_is_long(field))
1465 field->flags |= FIELD_IS_LONG;
1466
1467 if (test_type_token(type, token, EVENT_OP, ";"))
1468 goto fail;
1469 free_token(token);
1470
1471 if (read_expected(EVENT_ITEM, "offset") < 0)
1472 goto fail_expect;
1473
1474 if (read_expected(EVENT_OP, ":") < 0)
1475 goto fail_expect;
1476
1477 if (read_expect_type(EVENT_ITEM, &token))
1478 goto fail;
1479 field->offset = strtoul(token, NULL, 0);
1480 free_token(token);
1481
1482 if (read_expected(EVENT_OP, ";") < 0)
1483 goto fail_expect;
1484
1485 if (read_expected(EVENT_ITEM, "size") < 0)
1486 goto fail_expect;
1487
1488 if (read_expected(EVENT_OP, ":") < 0)
1489 goto fail_expect;
1490
1491 if (read_expect_type(EVENT_ITEM, &token))
1492 goto fail;
1493 field->size = strtoul(token, NULL, 0);
1494 free_token(token);
1495
1496 if (read_expected(EVENT_OP, ";") < 0)
1497 goto fail_expect;
1498
1499 type = read_token(&token);
1500 if (type != EVENT_NEWLINE) {
1501 /* newer versions of the kernel have a "signed" type */
1502 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1503 goto fail;
1504
1505 free_token(token);
1506
1507 if (read_expected(EVENT_OP, ":") < 0)
1508 goto fail_expect;
1509
1510 if (read_expect_type(EVENT_ITEM, &token))
1511 goto fail;
1512
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001513 if (strtoul(token, NULL, 0))
1514 field->flags |= FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001515
1516 free_token(token);
1517 if (read_expected(EVENT_OP, ";") < 0)
1518 goto fail_expect;
1519
1520 if (read_expect_type(EVENT_NEWLINE, &token))
1521 goto fail;
1522 }
1523
1524 free_token(token);
1525
1526 if (field->flags & FIELD_IS_ARRAY) {
1527 if (field->arraylen)
1528 field->elementsize = field->size / field->arraylen;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001529 else if (field->flags & FIELD_IS_DYNAMIC)
1530 field->elementsize = size_dynamic;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001531 else if (field->flags & FIELD_IS_STRING)
1532 field->elementsize = 1;
Jiri Olsae23c1a52013-01-24 21:46:43 +01001533 else if (field->flags & FIELD_IS_LONG)
1534 field->elementsize = event->pevent ?
1535 event->pevent->long_size :
1536 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001537 } else
1538 field->elementsize = field->size;
1539
1540 *fields = field;
1541 fields = &field->next;
1542
1543 } while (1);
1544
1545 return 0;
1546
1547fail:
1548 free_token(token);
1549fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001550 if (field) {
1551 free(field->type);
1552 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001553 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001554 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001555 return -1;
1556}
1557
1558static int event_read_format(struct event_format *event)
1559{
1560 char *token;
1561 int ret;
1562
1563 if (read_expected_item(EVENT_ITEM, "format") < 0)
1564 return -1;
1565
1566 if (read_expected(EVENT_OP, ":") < 0)
1567 return -1;
1568
1569 if (read_expect_type(EVENT_NEWLINE, &token))
1570 goto fail;
1571 free_token(token);
1572
1573 ret = event_read_fields(event, &event->format.common_fields);
1574 if (ret < 0)
1575 return ret;
1576 event->format.nr_common = ret;
1577
1578 ret = event_read_fields(event, &event->format.fields);
1579 if (ret < 0)
1580 return ret;
1581 event->format.nr_fields = ret;
1582
1583 return 0;
1584
1585 fail:
1586 free_token(token);
1587 return -1;
1588}
1589
1590static enum event_type
1591process_arg_token(struct event_format *event, struct print_arg *arg,
1592 char **tok, enum event_type type);
1593
1594static enum event_type
1595process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1596{
1597 enum event_type type;
1598 char *token;
1599
1600 type = read_token(&token);
1601 *tok = token;
1602
1603 return process_arg_token(event, arg, tok, type);
1604}
1605
1606static enum event_type
1607process_op(struct event_format *event, struct print_arg *arg, char **tok);
1608
1609static enum event_type
1610process_cond(struct event_format *event, struct print_arg *top, char **tok)
1611{
1612 struct print_arg *arg, *left, *right;
1613 enum event_type type;
1614 char *token = NULL;
1615
1616 arg = alloc_arg();
1617 left = alloc_arg();
1618 right = alloc_arg();
1619
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001620 if (!arg || !left || !right) {
1621 do_warning("%s: not enough memory!", __func__);
1622 /* arg will be freed at out_free */
1623 free_arg(left);
1624 free_arg(right);
1625 goto out_free;
1626 }
1627
Steven Rostedtf7d82352012-04-06 00:47:53 +02001628 arg->type = PRINT_OP;
1629 arg->op.left = left;
1630 arg->op.right = right;
1631
1632 *tok = NULL;
1633 type = process_arg(event, left, &token);
1634
1635 again:
1636 /* Handle other operations in the arguments */
1637 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1638 type = process_op(event, left, &token);
1639 goto again;
1640 }
1641
1642 if (test_type_token(type, token, EVENT_OP, ":"))
1643 goto out_free;
1644
1645 arg->op.op = token;
1646
1647 type = process_arg(event, right, &token);
1648
1649 top->op.right = arg;
1650
1651 *tok = token;
1652 return type;
1653
1654out_free:
1655 /* Top may point to itself */
1656 top->op.right = NULL;
1657 free_token(token);
1658 free_arg(arg);
1659 return EVENT_ERROR;
1660}
1661
1662static enum event_type
1663process_array(struct event_format *event, struct print_arg *top, char **tok)
1664{
1665 struct print_arg *arg;
1666 enum event_type type;
1667 char *token = NULL;
1668
1669 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001670 if (!arg) {
1671 do_warning("%s: not enough memory!", __func__);
1672 /* '*tok' is set to top->op.op. No need to free. */
1673 *tok = NULL;
1674 return EVENT_ERROR;
1675 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001676
1677 *tok = NULL;
1678 type = process_arg(event, arg, &token);
1679 if (test_type_token(type, token, EVENT_OP, "]"))
1680 goto out_free;
1681
1682 top->op.right = arg;
1683
1684 free_token(token);
1685 type = read_token_item(&token);
1686 *tok = token;
1687
1688 return type;
1689
1690out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001691 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001692 free_arg(arg);
1693 return EVENT_ERROR;
1694}
1695
1696static int get_op_prio(char *op)
1697{
1698 if (!op[1]) {
1699 switch (op[0]) {
1700 case '~':
1701 case '!':
1702 return 4;
1703 case '*':
1704 case '/':
1705 case '%':
1706 return 6;
1707 case '+':
1708 case '-':
1709 return 7;
1710 /* '>>' and '<<' are 8 */
1711 case '<':
1712 case '>':
1713 return 9;
1714 /* '==' and '!=' are 10 */
1715 case '&':
1716 return 11;
1717 case '^':
1718 return 12;
1719 case '|':
1720 return 13;
1721 case '?':
1722 return 16;
1723 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001724 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001725 return -1;
1726 }
1727 } else {
1728 if (strcmp(op, "++") == 0 ||
1729 strcmp(op, "--") == 0) {
1730 return 3;
1731 } else if (strcmp(op, ">>") == 0 ||
1732 strcmp(op, "<<") == 0) {
1733 return 8;
1734 } else if (strcmp(op, ">=") == 0 ||
1735 strcmp(op, "<=") == 0) {
1736 return 9;
1737 } else if (strcmp(op, "==") == 0 ||
1738 strcmp(op, "!=") == 0) {
1739 return 10;
1740 } else if (strcmp(op, "&&") == 0) {
1741 return 14;
1742 } else if (strcmp(op, "||") == 0) {
1743 return 15;
1744 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001745 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001746 return -1;
1747 }
1748 }
1749}
1750
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001751static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001752{
1753
1754 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001755 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001756 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001757 else
1758 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001759
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001760 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001761}
1762
1763/* Note, *tok does not get freed, but will most likely be saved */
1764static enum event_type
1765process_op(struct event_format *event, struct print_arg *arg, char **tok)
1766{
1767 struct print_arg *left, *right = NULL;
1768 enum event_type type;
1769 char *token;
1770
1771 /* the op is passed in via tok */
1772 token = *tok;
1773
1774 if (arg->type == PRINT_OP && !arg->op.left) {
1775 /* handle single op */
1776 if (token[1]) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001777 do_warning("bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001778 goto out_free;
1779 }
1780 switch (token[0]) {
1781 case '~':
1782 case '!':
1783 case '+':
1784 case '-':
1785 break;
1786 default:
1787 do_warning("bad op token %s", token);
1788 goto out_free;
1789
1790 }
1791
1792 /* make an empty left */
1793 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001794 if (!left)
1795 goto out_warn_free;
1796
Steven Rostedtf7d82352012-04-06 00:47:53 +02001797 left->type = PRINT_NULL;
1798 arg->op.left = left;
1799
1800 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001801 if (!right)
1802 goto out_warn_free;
1803
Steven Rostedtf7d82352012-04-06 00:47:53 +02001804 arg->op.right = right;
1805
1806 /* do not free the token, it belongs to an op */
1807 *tok = NULL;
1808 type = process_arg(event, right, tok);
1809
1810 } else if (strcmp(token, "?") == 0) {
1811
1812 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001813 if (!left)
1814 goto out_warn_free;
1815
Steven Rostedtf7d82352012-04-06 00:47:53 +02001816 /* copy the top arg to the left */
1817 *left = *arg;
1818
1819 arg->type = PRINT_OP;
1820 arg->op.op = token;
1821 arg->op.left = left;
1822 arg->op.prio = 0;
1823
Namhyung Kim41e51a22012-09-19 15:58:42 +09001824 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001825 type = process_cond(event, arg, tok);
1826
1827 } else if (strcmp(token, ">>") == 0 ||
1828 strcmp(token, "<<") == 0 ||
1829 strcmp(token, "&") == 0 ||
1830 strcmp(token, "|") == 0 ||
1831 strcmp(token, "&&") == 0 ||
1832 strcmp(token, "||") == 0 ||
1833 strcmp(token, "-") == 0 ||
1834 strcmp(token, "+") == 0 ||
1835 strcmp(token, "*") == 0 ||
1836 strcmp(token, "^") == 0 ||
1837 strcmp(token, "/") == 0 ||
1838 strcmp(token, "<") == 0 ||
1839 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001840 strcmp(token, "<=") == 0 ||
1841 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001842 strcmp(token, "==") == 0 ||
1843 strcmp(token, "!=") == 0) {
1844
1845 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001846 if (!left)
1847 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001848
1849 /* copy the top arg to the left */
1850 *left = *arg;
1851
1852 arg->type = PRINT_OP;
1853 arg->op.op = token;
1854 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001855 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001856
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001857 if (set_op_prio(arg) == -1) {
1858 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001859 /* arg->op.op (= token) will be freed at out_free */
1860 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001861 goto out_free;
1862 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001863
1864 type = read_token_item(&token);
1865 *tok = token;
1866
1867 /* could just be a type pointer */
1868 if ((strcmp(arg->op.op, "*") == 0) &&
1869 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001870 char *new_atom;
1871
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001872 if (left->type != PRINT_ATOM) {
1873 do_warning("bad pointer type");
1874 goto out_free;
1875 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001876 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001877 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001878 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001879 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001880
1881 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001882 strcat(left->atom.atom, " *");
1883 free(arg->op.op);
1884 *arg = *left;
1885 free(left);
1886
1887 return type;
1888 }
1889
1890 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001891 if (!right)
1892 goto out_warn_free;
1893
Steven Rostedtf7d82352012-04-06 00:47:53 +02001894 type = process_arg_token(event, right, tok, type);
1895 arg->op.right = right;
1896
1897 } else if (strcmp(token, "[") == 0) {
1898
1899 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001900 if (!left)
1901 goto out_warn_free;
1902
Steven Rostedtf7d82352012-04-06 00:47:53 +02001903 *left = *arg;
1904
1905 arg->type = PRINT_OP;
1906 arg->op.op = token;
1907 arg->op.left = left;
1908
1909 arg->op.prio = 0;
1910
Namhyung Kim41e51a22012-09-19 15:58:42 +09001911 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001912 type = process_array(event, arg, tok);
1913
1914 } else {
1915 do_warning("unknown op '%s'", token);
1916 event->flags |= EVENT_FL_FAILED;
1917 /* the arg is now the left side */
1918 goto out_free;
1919 }
1920
1921 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1922 int prio;
1923
1924 /* higher prios need to be closer to the root */
1925 prio = get_op_prio(*tok);
1926
1927 if (prio > arg->op.prio)
1928 return process_op(event, arg, tok);
1929
1930 return process_op(event, right, tok);
1931 }
1932
1933 return type;
1934
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001935out_warn_free:
1936 do_warning("%s: not enough memory!", __func__);
1937out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001938 free_token(token);
1939 *tok = NULL;
1940 return EVENT_ERROR;
1941}
1942
1943static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001944process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001945 char **tok)
1946{
1947 enum event_type type;
1948 char *field;
1949 char *token;
1950
1951 if (read_expected(EVENT_OP, "->") < 0)
1952 goto out_err;
1953
1954 if (read_expect_type(EVENT_ITEM, &token) < 0)
1955 goto out_free;
1956 field = token;
1957
1958 arg->type = PRINT_FIELD;
1959 arg->field.name = field;
1960
Tom Zanussi5205aec2012-04-06 00:47:58 +02001961 if (is_flag_field) {
1962 arg->field.field = pevent_find_any_field(event, arg->field.name);
1963 arg->field.field->flags |= FIELD_IS_FLAG;
1964 is_flag_field = 0;
1965 } else if (is_symbolic_field) {
1966 arg->field.field = pevent_find_any_field(event, arg->field.name);
1967 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1968 is_symbolic_field = 0;
1969 }
1970
Steven Rostedtf7d82352012-04-06 00:47:53 +02001971 type = read_token(&token);
1972 *tok = token;
1973
1974 return type;
1975
1976 out_free:
1977 free_token(token);
1978 out_err:
1979 *tok = NULL;
1980 return EVENT_ERROR;
1981}
1982
1983static char *arg_eval (struct print_arg *arg);
1984
1985static unsigned long long
1986eval_type_str(unsigned long long val, const char *type, int pointer)
1987{
1988 int sign = 0;
1989 char *ref;
1990 int len;
1991
1992 len = strlen(type);
1993
1994 if (pointer) {
1995
1996 if (type[len-1] != '*') {
1997 do_warning("pointer expected with non pointer type");
1998 return val;
1999 }
2000
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002001 ref = malloc(len);
2002 if (!ref) {
2003 do_warning("%s: not enough memory!", __func__);
2004 return val;
2005 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002006 memcpy(ref, type, len);
2007
2008 /* chop off the " *" */
2009 ref[len - 2] = 0;
2010
2011 val = eval_type_str(val, ref, 0);
2012 free(ref);
2013 return val;
2014 }
2015
2016 /* check if this is a pointer */
2017 if (type[len - 1] == '*')
2018 return val;
2019
2020 /* Try to figure out the arg size*/
2021 if (strncmp(type, "struct", 6) == 0)
2022 /* all bets off */
2023 return val;
2024
2025 if (strcmp(type, "u8") == 0)
2026 return val & 0xff;
2027
2028 if (strcmp(type, "u16") == 0)
2029 return val & 0xffff;
2030
2031 if (strcmp(type, "u32") == 0)
2032 return val & 0xffffffff;
2033
2034 if (strcmp(type, "u64") == 0 ||
2035 strcmp(type, "s64"))
2036 return val;
2037
2038 if (strcmp(type, "s8") == 0)
2039 return (unsigned long long)(char)val & 0xff;
2040
2041 if (strcmp(type, "s16") == 0)
2042 return (unsigned long long)(short)val & 0xffff;
2043
2044 if (strcmp(type, "s32") == 0)
2045 return (unsigned long long)(int)val & 0xffffffff;
2046
2047 if (strncmp(type, "unsigned ", 9) == 0) {
2048 sign = 0;
2049 type += 9;
2050 }
2051
2052 if (strcmp(type, "char") == 0) {
2053 if (sign)
2054 return (unsigned long long)(char)val & 0xff;
2055 else
2056 return val & 0xff;
2057 }
2058
2059 if (strcmp(type, "short") == 0) {
2060 if (sign)
2061 return (unsigned long long)(short)val & 0xffff;
2062 else
2063 return val & 0xffff;
2064 }
2065
2066 if (strcmp(type, "int") == 0) {
2067 if (sign)
2068 return (unsigned long long)(int)val & 0xffffffff;
2069 else
2070 return val & 0xffffffff;
2071 }
2072
2073 return val;
2074}
2075
2076/*
2077 * Try to figure out the type.
2078 */
2079static unsigned long long
2080eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2081{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002082 if (arg->type != PRINT_TYPE) {
2083 do_warning("expected type argument");
2084 return 0;
2085 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002086
2087 return eval_type_str(val, arg->typecast.type, pointer);
2088}
2089
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002090static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002091{
2092 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002093 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002094
2095 switch (arg->type) {
2096 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002097 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002098 break;
2099 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002100 ret = arg_num_eval(arg->typecast.item, val);
2101 if (!ret)
2102 break;
2103 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002104 break;
2105 case PRINT_OP:
2106 switch (arg->op.op[0]) {
2107 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002108 ret = arg_num_eval(arg->op.left, &left);
2109 if (!ret)
2110 break;
2111 ret = arg_num_eval(arg->op.right, &right);
2112 if (!ret)
2113 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002114 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002115 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002116 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002117 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002118 break;
2119 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002120 ret = arg_num_eval(arg->op.left, &left);
2121 if (!ret)
2122 break;
2123 ret = arg_num_eval(arg->op.right, &right);
2124 if (!ret)
2125 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002126 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002127 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002128 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002129 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002130 break;
2131 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002132 ret = arg_num_eval(arg->op.left, &left);
2133 if (!ret)
2134 break;
2135 ret = arg_num_eval(arg->op.right, &right);
2136 if (!ret)
2137 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002138 switch (arg->op.op[1]) {
2139 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002140 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002141 break;
2142 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002143 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002144 break;
2145 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002146 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002147 break;
2148 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002149 do_warning("unknown op '%s'", arg->op.op);
2150 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002151 }
2152 break;
2153 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002154 ret = arg_num_eval(arg->op.left, &left);
2155 if (!ret)
2156 break;
2157 ret = arg_num_eval(arg->op.right, &right);
2158 if (!ret)
2159 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002160 switch (arg->op.op[1]) {
2161 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002162 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002163 break;
2164 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002165 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002166 break;
2167 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002168 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002169 break;
2170 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002171 do_warning("unknown op '%s'", arg->op.op);
2172 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002173 }
2174 break;
2175 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002176 ret = arg_num_eval(arg->op.left, &left);
2177 if (!ret)
2178 break;
2179 ret = arg_num_eval(arg->op.right, &right);
2180 if (!ret)
2181 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002182
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002183 if (arg->op.op[1] != '=') {
2184 do_warning("unknown op '%s'", arg->op.op);
2185 ret = 0;
2186 } else
2187 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002188 break;
2189 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002190 ret = arg_num_eval(arg->op.left, &left);
2191 if (!ret)
2192 break;
2193 ret = arg_num_eval(arg->op.right, &right);
2194 if (!ret)
2195 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002196
2197 switch (arg->op.op[1]) {
2198 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002199 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002200 break;
2201 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002202 do_warning("unknown op '%s'", arg->op.op);
2203 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002204 }
2205 break;
2206 case '-':
2207 /* check for negative */
2208 if (arg->op.left->type == PRINT_NULL)
2209 left = 0;
2210 else
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;
2217 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002218 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002219 case '+':
2220 if (arg->op.left->type == PRINT_NULL)
2221 left = 0;
2222 else
2223 ret = arg_num_eval(arg->op.left, &left);
2224 if (!ret)
2225 break;
2226 ret = arg_num_eval(arg->op.right, &right);
2227 if (!ret)
2228 break;
2229 *val = left + right;
2230 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002231 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002232 do_warning("unknown op '%s'", arg->op.op);
2233 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002234 }
2235 break;
2236
2237 case PRINT_NULL:
2238 case PRINT_FIELD ... PRINT_SYMBOL:
2239 case PRINT_STRING:
2240 case PRINT_BSTRING:
2241 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002242 do_warning("invalid eval type %d", arg->type);
2243 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002244
2245 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002246 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002247}
2248
2249static char *arg_eval (struct print_arg *arg)
2250{
2251 long long val;
2252 static char buf[20];
2253
2254 switch (arg->type) {
2255 case PRINT_ATOM:
2256 return arg->atom.atom;
2257 case PRINT_TYPE:
2258 return arg_eval(arg->typecast.item);
2259 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002260 if (!arg_num_eval(arg, &val))
2261 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002262 sprintf(buf, "%lld", val);
2263 return buf;
2264
2265 case PRINT_NULL:
2266 case PRINT_FIELD ... PRINT_SYMBOL:
2267 case PRINT_STRING:
2268 case PRINT_BSTRING:
2269 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002270 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002271 break;
2272 }
2273
2274 return NULL;
2275}
2276
2277static enum event_type
2278process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2279{
2280 enum event_type type;
2281 struct print_arg *arg = NULL;
2282 struct print_flag_sym *field;
2283 char *token = *tok;
2284 char *value;
2285
2286 do {
2287 free_token(token);
2288 type = read_token_item(&token);
2289 if (test_type_token(type, token, EVENT_OP, "{"))
2290 break;
2291
2292 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002293 if (!arg)
2294 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002295
2296 free_token(token);
2297 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002298
2299 if (type == EVENT_OP)
2300 type = process_op(event, arg, &token);
2301
2302 if (type == EVENT_ERROR)
2303 goto out_free;
2304
Steven Rostedtf7d82352012-04-06 00:47:53 +02002305 if (test_type_token(type, token, EVENT_DELIM, ","))
2306 goto out_free;
2307
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002308 field = calloc(1, sizeof(*field));
2309 if (!field)
2310 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002311
2312 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002313 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002314 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002315 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002316 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002317 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002318
2319 free_arg(arg);
2320 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002321 if (!arg)
2322 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002323
2324 free_token(token);
2325 type = process_arg(event, arg, &token);
2326 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002327 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002328
2329 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002330 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002331 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002332 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002333 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002334 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002335 free_arg(arg);
2336 arg = NULL;
2337
2338 *list = field;
2339 list = &field->next;
2340
2341 free_token(token);
2342 type = read_token_item(&token);
2343 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2344
2345 *tok = token;
2346 return type;
2347
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002348out_free_field:
2349 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002350out_free:
2351 free_arg(arg);
2352 free_token(token);
2353 *tok = NULL;
2354
2355 return EVENT_ERROR;
2356}
2357
2358static enum event_type
2359process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2360{
2361 struct print_arg *field;
2362 enum event_type type;
2363 char *token;
2364
2365 memset(arg, 0, sizeof(*arg));
2366 arg->type = PRINT_FLAGS;
2367
2368 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002369 if (!field) {
2370 do_warning("%s: not enough memory!", __func__);
2371 goto out_free;
2372 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002373
2374 type = process_arg(event, field, &token);
2375
2376 /* Handle operations in the first argument */
2377 while (type == EVENT_OP)
2378 type = process_op(event, field, &token);
2379
2380 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002381 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002382 free_token(token);
2383
2384 arg->flags.field = field;
2385
2386 type = read_token_item(&token);
2387 if (event_item_type(type)) {
2388 arg->flags.delim = token;
2389 type = read_token_item(&token);
2390 }
2391
2392 if (test_type_token(type, token, EVENT_DELIM, ","))
2393 goto out_free;
2394
2395 type = process_fields(event, &arg->flags.flags, &token);
2396 if (test_type_token(type, token, EVENT_DELIM, ")"))
2397 goto out_free;
2398
2399 free_token(token);
2400 type = read_token_item(tok);
2401 return type;
2402
Namhyung Kim70d93042012-09-19 15:58:44 +09002403out_free_field:
2404 free_arg(field);
2405out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002406 free_token(token);
2407 *tok = NULL;
2408 return EVENT_ERROR;
2409}
2410
2411static enum event_type
2412process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2413{
2414 struct print_arg *field;
2415 enum event_type type;
2416 char *token;
2417
2418 memset(arg, 0, sizeof(*arg));
2419 arg->type = PRINT_SYMBOL;
2420
2421 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002422 if (!field) {
2423 do_warning("%s: not enough memory!", __func__);
2424 goto out_free;
2425 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002426
2427 type = process_arg(event, field, &token);
2428 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002429 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002430
2431 arg->symbol.field = field;
2432
2433 type = process_fields(event, &arg->symbol.symbols, &token);
2434 if (test_type_token(type, token, EVENT_DELIM, ")"))
2435 goto out_free;
2436
2437 free_token(token);
2438 type = read_token_item(tok);
2439 return type;
2440
Namhyung Kim70d93042012-09-19 15:58:44 +09002441out_free_field:
2442 free_arg(field);
2443out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002444 free_token(token);
2445 *tok = NULL;
2446 return EVENT_ERROR;
2447}
2448
2449static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002450process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2451{
2452 struct print_arg *field;
2453 enum event_type type;
2454 char *token;
2455
2456 memset(arg, 0, sizeof(*arg));
2457 arg->type = PRINT_HEX;
2458
2459 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002460 if (!field) {
2461 do_warning("%s: not enough memory!", __func__);
2462 goto out_free;
2463 }
2464
Namhyung Kime080e6f2012-06-27 09:41:41 +09002465 type = process_arg(event, field, &token);
2466
2467 if (test_type_token(type, token, EVENT_DELIM, ","))
2468 goto out_free;
2469
2470 arg->hex.field = field;
2471
2472 free_token(token);
2473
2474 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002475 if (!field) {
2476 do_warning("%s: not enough memory!", __func__);
2477 *tok = NULL;
2478 return EVENT_ERROR;
2479 }
2480
Namhyung Kime080e6f2012-06-27 09:41:41 +09002481 type = process_arg(event, field, &token);
2482
2483 if (test_type_token(type, token, EVENT_DELIM, ")"))
2484 goto out_free;
2485
2486 arg->hex.size = field;
2487
2488 free_token(token);
2489 type = read_token_item(tok);
2490 return type;
2491
2492 out_free:
2493 free_arg(field);
2494 free_token(token);
2495 *tok = NULL;
2496 return EVENT_ERROR;
2497}
2498
2499static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002500process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2501{
2502 struct format_field *field;
2503 enum event_type type;
2504 char *token;
2505
2506 memset(arg, 0, sizeof(*arg));
2507 arg->type = PRINT_DYNAMIC_ARRAY;
2508
2509 /*
2510 * The item within the parenthesis is another field that holds
2511 * the index into where the array starts.
2512 */
2513 type = read_token(&token);
2514 *tok = token;
2515 if (type != EVENT_ITEM)
2516 goto out_free;
2517
2518 /* Find the field */
2519
2520 field = pevent_find_field(event, token);
2521 if (!field)
2522 goto out_free;
2523
2524 arg->dynarray.field = field;
2525 arg->dynarray.index = 0;
2526
2527 if (read_expected(EVENT_DELIM, ")") < 0)
2528 goto out_free;
2529
2530 free_token(token);
2531 type = read_token_item(&token);
2532 *tok = token;
2533 if (type != EVENT_OP || strcmp(token, "[") != 0)
2534 return type;
2535
2536 free_token(token);
2537 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002538 if (!arg) {
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002539 do_warning("%s: not enough memory!", __func__);
2540 *tok = NULL;
2541 return EVENT_ERROR;
2542 }
2543
Steven Rostedtf7d82352012-04-06 00:47:53 +02002544 type = process_arg(event, arg, &token);
2545 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002546 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002547
2548 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002549 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002550
2551 free_token(token);
2552 type = read_token_item(tok);
2553 return type;
2554
Namhyung Kimb3511d02012-05-23 11:36:50 +09002555 out_free_arg:
2556 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002557 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002558 free_token(token);
2559 *tok = NULL;
2560 return EVENT_ERROR;
2561}
2562
2563static enum event_type
2564process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2565{
2566 struct print_arg *item_arg;
2567 enum event_type type;
2568 char *token;
2569
2570 type = process_arg(event, arg, &token);
2571
2572 if (type == EVENT_ERROR)
2573 goto out_free;
2574
2575 if (type == EVENT_OP)
2576 type = process_op(event, arg, &token);
2577
2578 if (type == EVENT_ERROR)
2579 goto out_free;
2580
2581 if (test_type_token(type, token, EVENT_DELIM, ")"))
2582 goto out_free;
2583
2584 free_token(token);
2585 type = read_token_item(&token);
2586
2587 /*
2588 * If the next token is an item or another open paren, then
2589 * this was a typecast.
2590 */
2591 if (event_item_type(type) ||
2592 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2593
2594 /* make this a typecast and contine */
2595
2596 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002597 if (arg->type != PRINT_ATOM) {
2598 do_warning("previous needed to be PRINT_ATOM");
2599 goto out_free;
2600 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002601
2602 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002603 if (!item_arg) {
2604 do_warning("%s: not enough memory!", __func__);
2605 goto out_free;
2606 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002607
2608 arg->type = PRINT_TYPE;
2609 arg->typecast.type = arg->atom.atom;
2610 arg->typecast.item = item_arg;
2611 type = process_arg_token(event, item_arg, &token, type);
2612
2613 }
2614
2615 *tok = token;
2616 return type;
2617
2618 out_free:
2619 free_token(token);
2620 *tok = NULL;
2621 return EVENT_ERROR;
2622}
2623
2624
2625static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002626process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2627 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002628{
2629 enum event_type type;
2630 char *token;
2631
2632 if (read_expect_type(EVENT_ITEM, &token) < 0)
2633 goto out_free;
2634
2635 arg->type = PRINT_STRING;
2636 arg->string.string = token;
2637 arg->string.offset = -1;
2638
2639 if (read_expected(EVENT_DELIM, ")") < 0)
2640 goto out_err;
2641
2642 type = read_token(&token);
2643 *tok = token;
2644
2645 return type;
2646
2647 out_free:
2648 free_token(token);
2649 out_err:
2650 *tok = NULL;
2651 return EVENT_ERROR;
2652}
2653
2654static struct pevent_function_handler *
2655find_func_handler(struct pevent *pevent, char *func_name)
2656{
2657 struct pevent_function_handler *func;
2658
Steven Rostedt101782e2012-10-01 20:13:51 -04002659 if (!pevent)
2660 return NULL;
2661
Steven Rostedtf7d82352012-04-06 00:47:53 +02002662 for (func = pevent->func_handlers; func; func = func->next) {
2663 if (strcmp(func->name, func_name) == 0)
2664 break;
2665 }
2666
2667 return func;
2668}
2669
2670static void remove_func_handler(struct pevent *pevent, char *func_name)
2671{
2672 struct pevent_function_handler *func;
2673 struct pevent_function_handler **next;
2674
2675 next = &pevent->func_handlers;
2676 while ((func = *next)) {
2677 if (strcmp(func->name, func_name) == 0) {
2678 *next = func->next;
2679 free_func_handle(func);
2680 break;
2681 }
2682 next = &func->next;
2683 }
2684}
2685
2686static enum event_type
2687process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2688 struct print_arg *arg, char **tok)
2689{
2690 struct print_arg **next_arg;
2691 struct print_arg *farg;
2692 enum event_type type;
2693 char *token;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03002694 const char *test;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002695 int i;
2696
2697 arg->type = PRINT_FUNC;
2698 arg->func.func = func;
2699
2700 *tok = NULL;
2701
2702 next_arg = &(arg->func.args);
2703 for (i = 0; i < func->nr_args; i++) {
2704 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002705 if (!farg) {
2706 do_warning("%s: not enough memory!", __func__);
2707 return EVENT_ERROR;
2708 }
2709
Steven Rostedtf7d82352012-04-06 00:47:53 +02002710 type = process_arg(event, farg, &token);
2711 if (i < (func->nr_args - 1))
2712 test = ",";
2713 else
2714 test = ")";
2715
2716 if (test_type_token(type, token, EVENT_DELIM, test)) {
2717 free_arg(farg);
2718 free_token(token);
2719 return EVENT_ERROR;
2720 }
2721
2722 *next_arg = farg;
2723 next_arg = &(farg->next);
2724 free_token(token);
2725 }
2726
2727 type = read_token(&token);
2728 *tok = token;
2729
2730 return type;
2731}
2732
2733static enum event_type
2734process_function(struct event_format *event, struct print_arg *arg,
2735 char *token, char **tok)
2736{
2737 struct pevent_function_handler *func;
2738
2739 if (strcmp(token, "__print_flags") == 0) {
2740 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002741 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002742 return process_flags(event, arg, tok);
2743 }
2744 if (strcmp(token, "__print_symbolic") == 0) {
2745 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002746 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002747 return process_symbols(event, arg, tok);
2748 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002749 if (strcmp(token, "__print_hex") == 0) {
2750 free_token(token);
2751 return process_hex(event, arg, tok);
2752 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002753 if (strcmp(token, "__get_str") == 0) {
2754 free_token(token);
2755 return process_str(event, arg, tok);
2756 }
2757 if (strcmp(token, "__get_dynamic_array") == 0) {
2758 free_token(token);
2759 return process_dynamic_array(event, arg, tok);
2760 }
2761
2762 func = find_func_handler(event->pevent, token);
2763 if (func) {
2764 free_token(token);
2765 return process_func_handler(event, func, arg, tok);
2766 }
2767
2768 do_warning("function %s not defined", token);
2769 free_token(token);
2770 return EVENT_ERROR;
2771}
2772
2773static enum event_type
2774process_arg_token(struct event_format *event, struct print_arg *arg,
2775 char **tok, enum event_type type)
2776{
2777 char *token;
2778 char *atom;
2779
2780 token = *tok;
2781
2782 switch (type) {
2783 case EVENT_ITEM:
2784 if (strcmp(token, "REC") == 0) {
2785 free_token(token);
2786 type = process_entry(event, arg, &token);
2787 break;
2788 }
2789 atom = token;
2790 /* test the next token */
2791 type = read_token_item(&token);
2792
2793 /*
2794 * If the next token is a parenthesis, then this
2795 * is a function.
2796 */
2797 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2798 free_token(token);
2799 token = NULL;
2800 /* this will free atom. */
2801 type = process_function(event, arg, atom, &token);
2802 break;
2803 }
2804 /* atoms can be more than one token long */
2805 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002806 char *new_atom;
2807 new_atom = realloc(atom,
2808 strlen(atom) + strlen(token) + 2);
2809 if (!new_atom) {
2810 free(atom);
2811 *tok = NULL;
2812 free_token(token);
2813 return EVENT_ERROR;
2814 }
2815 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002816 strcat(atom, " ");
2817 strcat(atom, token);
2818 free_token(token);
2819 type = read_token_item(&token);
2820 }
2821
2822 arg->type = PRINT_ATOM;
2823 arg->atom.atom = atom;
2824 break;
2825
2826 case EVENT_DQUOTE:
2827 case EVENT_SQUOTE:
2828 arg->type = PRINT_ATOM;
2829 arg->atom.atom = token;
2830 type = read_token_item(&token);
2831 break;
2832 case EVENT_DELIM:
2833 if (strcmp(token, "(") == 0) {
2834 free_token(token);
2835 type = process_paren(event, arg, &token);
2836 break;
2837 }
2838 case EVENT_OP:
2839 /* handle single ops */
2840 arg->type = PRINT_OP;
2841 arg->op.op = token;
2842 arg->op.left = NULL;
2843 type = process_op(event, arg, &token);
2844
2845 /* On error, the op is freed */
2846 if (type == EVENT_ERROR)
2847 arg->op.op = NULL;
2848
2849 /* return error type if errored */
2850 break;
2851
2852 case EVENT_ERROR ... EVENT_NEWLINE:
2853 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002854 do_warning("unexpected type %d", type);
2855 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002856 }
2857 *tok = token;
2858
2859 return type;
2860}
2861
2862static int event_read_print_args(struct event_format *event, struct print_arg **list)
2863{
2864 enum event_type type = EVENT_ERROR;
2865 struct print_arg *arg;
2866 char *token;
2867 int args = 0;
2868
2869 do {
2870 if (type == EVENT_NEWLINE) {
2871 type = read_token_item(&token);
2872 continue;
2873 }
2874
2875 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002876 if (!arg) {
2877 do_warning("%s: not enough memory!", __func__);
2878 return -1;
2879 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002880
2881 type = process_arg(event, arg, &token);
2882
2883 if (type == EVENT_ERROR) {
2884 free_token(token);
2885 free_arg(arg);
2886 return -1;
2887 }
2888
2889 *list = arg;
2890 args++;
2891
2892 if (type == EVENT_OP) {
2893 type = process_op(event, arg, &token);
2894 free_token(token);
2895 if (type == EVENT_ERROR) {
2896 *list = NULL;
2897 free_arg(arg);
2898 return -1;
2899 }
2900 list = &arg->next;
2901 continue;
2902 }
2903
2904 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2905 free_token(token);
2906 *list = arg;
2907 list = &arg->next;
2908 continue;
2909 }
2910 break;
2911 } while (type != EVENT_NONE);
2912
2913 if (type != EVENT_NONE && type != EVENT_ERROR)
2914 free_token(token);
2915
2916 return args;
2917}
2918
2919static int event_read_print(struct event_format *event)
2920{
2921 enum event_type type;
2922 char *token;
2923 int ret;
2924
2925 if (read_expected_item(EVENT_ITEM, "print") < 0)
2926 return -1;
2927
2928 if (read_expected(EVENT_ITEM, "fmt") < 0)
2929 return -1;
2930
2931 if (read_expected(EVENT_OP, ":") < 0)
2932 return -1;
2933
2934 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2935 goto fail;
2936
2937 concat:
2938 event->print_fmt.format = token;
2939 event->print_fmt.args = NULL;
2940
2941 /* ok to have no arg */
2942 type = read_token_item(&token);
2943
2944 if (type == EVENT_NONE)
2945 return 0;
2946
2947 /* Handle concatenation of print lines */
2948 if (type == EVENT_DQUOTE) {
2949 char *cat;
2950
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03002951 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2952 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002953 free_token(token);
2954 free_token(event->print_fmt.format);
2955 event->print_fmt.format = NULL;
2956 token = cat;
2957 goto concat;
2958 }
2959
2960 if (test_type_token(type, token, EVENT_DELIM, ","))
2961 goto fail;
2962
2963 free_token(token);
2964
2965 ret = event_read_print_args(event, &event->print_fmt.args);
2966 if (ret < 0)
2967 return -1;
2968
2969 return ret;
2970
2971 fail:
2972 free_token(token);
2973 return -1;
2974}
2975
2976/**
2977 * pevent_find_common_field - return a common field by event
2978 * @event: handle for the event
2979 * @name: the name of the common field to return
2980 *
2981 * Returns a common field from the event by the given @name.
2982 * This only searchs the common fields and not all field.
2983 */
2984struct format_field *
2985pevent_find_common_field(struct event_format *event, const char *name)
2986{
2987 struct format_field *format;
2988
2989 for (format = event->format.common_fields;
2990 format; format = format->next) {
2991 if (strcmp(format->name, name) == 0)
2992 break;
2993 }
2994
2995 return format;
2996}
2997
2998/**
2999 * pevent_find_field - find a non-common field
3000 * @event: handle for the event
3001 * @name: the name of the non-common field
3002 *
3003 * Returns a non-common field by the given @name.
3004 * This does not search common fields.
3005 */
3006struct format_field *
3007pevent_find_field(struct event_format *event, const char *name)
3008{
3009 struct format_field *format;
3010
3011 for (format = event->format.fields;
3012 format; format = format->next) {
3013 if (strcmp(format->name, name) == 0)
3014 break;
3015 }
3016
3017 return format;
3018}
3019
3020/**
3021 * pevent_find_any_field - find any field by name
3022 * @event: handle for the event
3023 * @name: the name of the field
3024 *
3025 * Returns a field by the given @name.
3026 * This searchs the common field names first, then
3027 * the non-common ones if a common one was not found.
3028 */
3029struct format_field *
3030pevent_find_any_field(struct event_format *event, const char *name)
3031{
3032 struct format_field *format;
3033
3034 format = pevent_find_common_field(event, name);
3035 if (format)
3036 return format;
3037 return pevent_find_field(event, name);
3038}
3039
3040/**
3041 * pevent_read_number - read a number from data
3042 * @pevent: handle for the pevent
3043 * @ptr: the raw data
3044 * @size: the size of the data that holds the number
3045 *
3046 * Returns the number (converted to host) from the
3047 * raw data.
3048 */
3049unsigned long long pevent_read_number(struct pevent *pevent,
3050 const void *ptr, int size)
3051{
3052 switch (size) {
3053 case 1:
3054 return *(unsigned char *)ptr;
3055 case 2:
3056 return data2host2(pevent, ptr);
3057 case 4:
3058 return data2host4(pevent, ptr);
3059 case 8:
3060 return data2host8(pevent, ptr);
3061 default:
3062 /* BUG! */
3063 return 0;
3064 }
3065}
3066
3067/**
3068 * pevent_read_number_field - read a number from data
3069 * @field: a handle to the field
3070 * @data: the raw data to read
3071 * @value: the value to place the number in
3072 *
3073 * Reads raw data according to a field offset and size,
3074 * and translates it into @value.
3075 *
3076 * Returns 0 on success, -1 otherwise.
3077 */
3078int pevent_read_number_field(struct format_field *field, const void *data,
3079 unsigned long long *value)
3080{
3081 if (!field)
3082 return -1;
3083 switch (field->size) {
3084 case 1:
3085 case 2:
3086 case 4:
3087 case 8:
3088 *value = pevent_read_number(field->event->pevent,
3089 data + field->offset, field->size);
3090 return 0;
3091 default:
3092 return -1;
3093 }
3094}
3095
3096static int get_common_info(struct pevent *pevent,
3097 const char *type, int *offset, int *size)
3098{
3099 struct event_format *event;
3100 struct format_field *field;
3101
3102 /*
3103 * All events should have the same common elements.
3104 * Pick any event to find where the type is;
3105 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003106 if (!pevent->events) {
3107 do_warning("no event_list!");
3108 return -1;
3109 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003110
3111 event = pevent->events[0];
3112 field = pevent_find_common_field(event, type);
3113 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003114 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003115
3116 *offset = field->offset;
3117 *size = field->size;
3118
3119 return 0;
3120}
3121
3122static int __parse_common(struct pevent *pevent, void *data,
3123 int *size, int *offset, const char *name)
3124{
3125 int ret;
3126
3127 if (!*size) {
3128 ret = get_common_info(pevent, name, offset, size);
3129 if (ret < 0)
3130 return ret;
3131 }
3132 return pevent_read_number(pevent, data + *offset, *size);
3133}
3134
3135static int trace_parse_common_type(struct pevent *pevent, void *data)
3136{
3137 return __parse_common(pevent, data,
3138 &pevent->type_size, &pevent->type_offset,
3139 "common_type");
3140}
3141
3142static int parse_common_pid(struct pevent *pevent, void *data)
3143{
3144 return __parse_common(pevent, data,
3145 &pevent->pid_size, &pevent->pid_offset,
3146 "common_pid");
3147}
3148
3149static int parse_common_pc(struct pevent *pevent, void *data)
3150{
3151 return __parse_common(pevent, data,
3152 &pevent->pc_size, &pevent->pc_offset,
3153 "common_preempt_count");
3154}
3155
3156static int parse_common_flags(struct pevent *pevent, void *data)
3157{
3158 return __parse_common(pevent, data,
3159 &pevent->flags_size, &pevent->flags_offset,
3160 "common_flags");
3161}
3162
3163static int parse_common_lock_depth(struct pevent *pevent, void *data)
3164{
Steven Rostedt0866a972012-05-22 14:52:40 +09003165 return __parse_common(pevent, data,
3166 &pevent->ld_size, &pevent->ld_offset,
3167 "common_lock_depth");
3168}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003169
Steven Rostedt0866a972012-05-22 14:52:40 +09003170static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3171{
3172 return __parse_common(pevent, data,
3173 &pevent->ld_size, &pevent->ld_offset,
3174 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003175}
3176
3177static int events_id_cmp(const void *a, const void *b);
3178
3179/**
3180 * pevent_find_event - find an event by given id
3181 * @pevent: a handle to the pevent
3182 * @id: the id of the event
3183 *
3184 * Returns an event that has a given @id.
3185 */
3186struct event_format *pevent_find_event(struct pevent *pevent, int id)
3187{
3188 struct event_format **eventptr;
3189 struct event_format key;
3190 struct event_format *pkey = &key;
3191
3192 /* Check cache first */
3193 if (pevent->last_event && pevent->last_event->id == id)
3194 return pevent->last_event;
3195
3196 key.id = id;
3197
3198 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3199 sizeof(*pevent->events), events_id_cmp);
3200
3201 if (eventptr) {
3202 pevent->last_event = *eventptr;
3203 return *eventptr;
3204 }
3205
3206 return NULL;
3207}
3208
3209/**
3210 * pevent_find_event_by_name - find an event by given name
3211 * @pevent: a handle to the pevent
3212 * @sys: the system name to search for
3213 * @name: the name of the event to search for
3214 *
3215 * This returns an event with a given @name and under the system
3216 * @sys. If @sys is NULL the first event with @name is returned.
3217 */
3218struct event_format *
3219pevent_find_event_by_name(struct pevent *pevent,
3220 const char *sys, const char *name)
3221{
3222 struct event_format *event;
3223 int i;
3224
3225 if (pevent->last_event &&
3226 strcmp(pevent->last_event->name, name) == 0 &&
3227 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3228 return pevent->last_event;
3229
3230 for (i = 0; i < pevent->nr_events; i++) {
3231 event = pevent->events[i];
3232 if (strcmp(event->name, name) == 0) {
3233 if (!sys)
3234 break;
3235 if (strcmp(event->system, sys) == 0)
3236 break;
3237 }
3238 }
3239 if (i == pevent->nr_events)
3240 event = NULL;
3241
3242 pevent->last_event = event;
3243 return event;
3244}
3245
3246static unsigned long long
3247eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3248{
3249 struct pevent *pevent = event->pevent;
3250 unsigned long long val = 0;
3251 unsigned long long left, right;
3252 struct print_arg *typearg = NULL;
3253 struct print_arg *larg;
3254 unsigned long offset;
3255 unsigned int field_size;
3256
3257 switch (arg->type) {
3258 case PRINT_NULL:
3259 /* ?? */
3260 return 0;
3261 case PRINT_ATOM:
3262 return strtoull(arg->atom.atom, NULL, 0);
3263 case PRINT_FIELD:
3264 if (!arg->field.field) {
3265 arg->field.field = pevent_find_any_field(event, arg->field.name);
3266 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003267 goto out_warning_field;
3268
Steven Rostedtf7d82352012-04-06 00:47:53 +02003269 }
3270 /* must be a number */
3271 val = pevent_read_number(pevent, data + arg->field.field->offset,
3272 arg->field.field->size);
3273 break;
3274 case PRINT_FLAGS:
3275 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003276 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003277 break;
3278 case PRINT_TYPE:
3279 val = eval_num_arg(data, size, event, arg->typecast.item);
3280 return eval_type(val, arg, 0);
3281 case PRINT_STRING:
3282 case PRINT_BSTRING:
3283 return 0;
3284 case PRINT_FUNC: {
3285 struct trace_seq s;
3286 trace_seq_init(&s);
3287 val = process_defined_func(&s, data, size, event, arg);
3288 trace_seq_destroy(&s);
3289 return val;
3290 }
3291 case PRINT_OP:
3292 if (strcmp(arg->op.op, "[") == 0) {
3293 /*
3294 * Arrays are special, since we don't want
3295 * to read the arg as is.
3296 */
3297 right = eval_num_arg(data, size, event, arg->op.right);
3298
3299 /* handle typecasts */
3300 larg = arg->op.left;
3301 while (larg->type == PRINT_TYPE) {
3302 if (!typearg)
3303 typearg = larg;
3304 larg = larg->typecast.item;
3305 }
3306
3307 /* Default to long size */
3308 field_size = pevent->long_size;
3309
3310 switch (larg->type) {
3311 case PRINT_DYNAMIC_ARRAY:
3312 offset = pevent_read_number(pevent,
3313 data + larg->dynarray.field->offset,
3314 larg->dynarray.field->size);
3315 if (larg->dynarray.field->elementsize)
3316 field_size = larg->dynarray.field->elementsize;
3317 /*
3318 * The actual length of the dynamic array is stored
3319 * in the top half of the field, and the offset
3320 * is in the bottom half of the 32 bit field.
3321 */
3322 offset &= 0xffff;
3323 offset += right;
3324 break;
3325 case PRINT_FIELD:
3326 if (!larg->field.field) {
3327 larg->field.field =
3328 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003329 if (!larg->field.field) {
3330 arg = larg;
3331 goto out_warning_field;
3332 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003333 }
3334 field_size = larg->field.field->elementsize;
3335 offset = larg->field.field->offset +
3336 right * larg->field.field->elementsize;
3337 break;
3338 default:
3339 goto default_op; /* oops, all bets off */
3340 }
3341 val = pevent_read_number(pevent,
3342 data + offset, field_size);
3343 if (typearg)
3344 val = eval_type(val, typearg, 1);
3345 break;
3346 } else if (strcmp(arg->op.op, "?") == 0) {
3347 left = eval_num_arg(data, size, event, arg->op.left);
3348 arg = arg->op.right;
3349 if (left)
3350 val = eval_num_arg(data, size, event, arg->op.left);
3351 else
3352 val = eval_num_arg(data, size, event, arg->op.right);
3353 break;
3354 }
3355 default_op:
3356 left = eval_num_arg(data, size, event, arg->op.left);
3357 right = eval_num_arg(data, size, event, arg->op.right);
3358 switch (arg->op.op[0]) {
3359 case '!':
3360 switch (arg->op.op[1]) {
3361 case 0:
3362 val = !right;
3363 break;
3364 case '=':
3365 val = left != right;
3366 break;
3367 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003368 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003369 }
3370 break;
3371 case '~':
3372 val = ~right;
3373 break;
3374 case '|':
3375 if (arg->op.op[1])
3376 val = left || right;
3377 else
3378 val = left | right;
3379 break;
3380 case '&':
3381 if (arg->op.op[1])
3382 val = left && right;
3383 else
3384 val = left & right;
3385 break;
3386 case '<':
3387 switch (arg->op.op[1]) {
3388 case 0:
3389 val = left < right;
3390 break;
3391 case '<':
3392 val = left << right;
3393 break;
3394 case '=':
3395 val = left <= right;
3396 break;
3397 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003398 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003399 }
3400 break;
3401 case '>':
3402 switch (arg->op.op[1]) {
3403 case 0:
3404 val = left > right;
3405 break;
3406 case '>':
3407 val = left >> right;
3408 break;
3409 case '=':
3410 val = left >= right;
3411 break;
3412 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003413 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003414 }
3415 break;
3416 case '=':
3417 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003418 goto out_warning_op;
3419
Steven Rostedtf7d82352012-04-06 00:47:53 +02003420 val = left == right;
3421 break;
3422 case '-':
3423 val = left - right;
3424 break;
3425 case '+':
3426 val = left + right;
3427 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003428 case '/':
3429 val = left / right;
3430 break;
3431 case '*':
3432 val = left * right;
3433 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003434 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003435 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003436 }
3437 break;
3438 default: /* not sure what to do there */
3439 return 0;
3440 }
3441 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003442
3443out_warning_op:
3444 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3445 return 0;
3446
3447out_warning_field:
3448 do_warning("%s: field %s not found", __func__, arg->field.name);
3449 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003450}
3451
3452struct flag {
3453 const char *name;
3454 unsigned long long value;
3455};
3456
3457static const struct flag flags[] = {
3458 { "HI_SOFTIRQ", 0 },
3459 { "TIMER_SOFTIRQ", 1 },
3460 { "NET_TX_SOFTIRQ", 2 },
3461 { "NET_RX_SOFTIRQ", 3 },
3462 { "BLOCK_SOFTIRQ", 4 },
3463 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3464 { "TASKLET_SOFTIRQ", 6 },
3465 { "SCHED_SOFTIRQ", 7 },
3466 { "HRTIMER_SOFTIRQ", 8 },
3467 { "RCU_SOFTIRQ", 9 },
3468
3469 { "HRTIMER_NORESTART", 0 },
3470 { "HRTIMER_RESTART", 1 },
3471};
3472
3473static unsigned long long eval_flag(const char *flag)
3474{
3475 int i;
3476
3477 /*
3478 * Some flags in the format files do not get converted.
3479 * If the flag is not numeric, see if it is something that
3480 * we already know about.
3481 */
3482 if (isdigit(flag[0]))
3483 return strtoull(flag, NULL, 0);
3484
3485 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3486 if (strcmp(flags[i].name, flag) == 0)
3487 return flags[i].value;
3488
3489 return 0;
3490}
3491
3492static void print_str_to_seq(struct trace_seq *s, const char *format,
3493 int len_arg, const char *str)
3494{
3495 if (len_arg >= 0)
3496 trace_seq_printf(s, format, len_arg, str);
3497 else
3498 trace_seq_printf(s, format, str);
3499}
3500
3501static void print_str_arg(struct trace_seq *s, void *data, int size,
3502 struct event_format *event, const char *format,
3503 int len_arg, struct print_arg *arg)
3504{
3505 struct pevent *pevent = event->pevent;
3506 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003507 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003508 struct printk_map *printk;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003509 unsigned long long val, fval;
3510 unsigned long addr;
3511 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003512 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003513 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003514 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003515
3516 switch (arg->type) {
3517 case PRINT_NULL:
3518 /* ?? */
3519 return;
3520 case PRINT_ATOM:
3521 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3522 return;
3523 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003524 field = arg->field.field;
3525 if (!field) {
3526 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003527 if (!field) {
3528 str = arg->field.name;
3529 goto out_warning_field;
3530 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003531 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003532 }
3533 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003534 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003535
3536 /*
3537 * Some events pass in pointers. If this is not an array
3538 * and the size is the same as long_size, assume that it
3539 * is a pointer.
3540 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003541 if (!(field->flags & FIELD_IS_ARRAY) &&
3542 field->size == pevent->long_size) {
3543 addr = *(unsigned long *)(data + field->offset);
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003544 /* Check if it matches a print format */
3545 printk = find_printk(pevent, addr);
3546 if (printk)
3547 trace_seq_puts(s, printk->printk);
3548 else
3549 trace_seq_printf(s, "%lx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003550 break;
3551 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003552 str = malloc(len + 1);
3553 if (!str) {
3554 do_warning("%s: not enough memory!", __func__);
3555 return;
3556 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003557 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003558 str[len] = 0;
3559 print_str_to_seq(s, format, len_arg, str);
3560 free(str);
3561 break;
3562 case PRINT_FLAGS:
3563 val = eval_num_arg(data, size, event, arg->flags.field);
3564 print = 0;
3565 for (flag = arg->flags.flags; flag; flag = flag->next) {
3566 fval = eval_flag(flag->value);
3567 if (!val && !fval) {
3568 print_str_to_seq(s, format, len_arg, flag->str);
3569 break;
3570 }
3571 if (fval && (val & fval) == fval) {
3572 if (print && arg->flags.delim)
3573 trace_seq_puts(s, arg->flags.delim);
3574 print_str_to_seq(s, format, len_arg, flag->str);
3575 print = 1;
3576 val &= ~fval;
3577 }
3578 }
3579 break;
3580 case PRINT_SYMBOL:
3581 val = eval_num_arg(data, size, event, arg->symbol.field);
3582 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3583 fval = eval_flag(flag->value);
3584 if (val == fval) {
3585 print_str_to_seq(s, format, len_arg, flag->str);
3586 break;
3587 }
3588 }
3589 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003590 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003591 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3592 unsigned long offset;
3593 offset = pevent_read_number(pevent,
3594 data + arg->hex.field->dynarray.field->offset,
3595 arg->hex.field->dynarray.field->size);
3596 hex = data + (offset & 0xffff);
3597 } else {
3598 field = arg->hex.field->field.field;
3599 if (!field) {
3600 str = arg->hex.field->field.name;
3601 field = pevent_find_any_field(event, str);
3602 if (!field)
3603 goto out_warning_field;
3604 arg->hex.field->field.field = field;
3605 }
3606 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003607 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003608 len = eval_num_arg(data, size, event, arg->hex.size);
3609 for (i = 0; i < len; i++) {
3610 if (i)
3611 trace_seq_putc(s, ' ');
3612 trace_seq_printf(s, "%02x", hex[i]);
3613 }
3614 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003615
3616 case PRINT_TYPE:
3617 break;
3618 case PRINT_STRING: {
3619 int str_offset;
3620
3621 if (arg->string.offset == -1) {
3622 struct format_field *f;
3623
3624 f = pevent_find_any_field(event, arg->string.string);
3625 arg->string.offset = f->offset;
3626 }
3627 str_offset = data2host4(pevent, data + arg->string.offset);
3628 str_offset &= 0xffff;
3629 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3630 break;
3631 }
3632 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003633 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003634 break;
3635 case PRINT_OP:
3636 /*
3637 * The only op for string should be ? :
3638 */
3639 if (arg->op.op[0] != '?')
3640 return;
3641 val = eval_num_arg(data, size, event, arg->op.left);
3642 if (val)
3643 print_str_arg(s, data, size, event,
3644 format, len_arg, arg->op.right->op.left);
3645 else
3646 print_str_arg(s, data, size, event,
3647 format, len_arg, arg->op.right->op.right);
3648 break;
3649 case PRINT_FUNC:
3650 process_defined_func(s, data, size, event, arg);
3651 break;
3652 default:
3653 /* well... */
3654 break;
3655 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003656
3657 return;
3658
3659out_warning_field:
3660 do_warning("%s: field %s not found", __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003661}
3662
3663static unsigned long long
3664process_defined_func(struct trace_seq *s, void *data, int size,
3665 struct event_format *event, struct print_arg *arg)
3666{
3667 struct pevent_function_handler *func_handle = arg->func.func;
3668 struct pevent_func_params *param;
3669 unsigned long long *args;
3670 unsigned long long ret;
3671 struct print_arg *farg;
3672 struct trace_seq str;
3673 struct save_str {
3674 struct save_str *next;
3675 char *str;
3676 } *strings = NULL, *string;
3677 int i;
3678
3679 if (!func_handle->nr_args) {
3680 ret = (*func_handle->func)(s, NULL);
3681 goto out;
3682 }
3683
3684 farg = arg->func.args;
3685 param = func_handle->params;
3686
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003687 ret = ULLONG_MAX;
3688 args = malloc(sizeof(*args) * func_handle->nr_args);
3689 if (!args)
3690 goto out;
3691
Steven Rostedtf7d82352012-04-06 00:47:53 +02003692 for (i = 0; i < func_handle->nr_args; i++) {
3693 switch (param->type) {
3694 case PEVENT_FUNC_ARG_INT:
3695 case PEVENT_FUNC_ARG_LONG:
3696 case PEVENT_FUNC_ARG_PTR:
3697 args[i] = eval_num_arg(data, size, event, farg);
3698 break;
3699 case PEVENT_FUNC_ARG_STRING:
3700 trace_seq_init(&str);
3701 print_str_arg(&str, data, size, event, "%s", -1, farg);
3702 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003703 string = malloc(sizeof(*string));
3704 if (!string) {
3705 do_warning("%s(%d): malloc str", __func__, __LINE__);
3706 goto out_free;
3707 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003708 string->next = strings;
3709 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003710 if (!string->str) {
3711 free(string);
3712 do_warning("%s(%d): malloc str", __func__, __LINE__);
3713 goto out_free;
3714 }
Robert Richter0cf26012012-08-07 19:43:14 +02003715 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003716 strings = string;
3717 trace_seq_destroy(&str);
3718 break;
3719 default:
3720 /*
3721 * Something went totally wrong, this is not
3722 * an input error, something in this code broke.
3723 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003724 do_warning("Unexpected end of arguments\n");
3725 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003726 }
3727 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003728 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003729 }
3730
3731 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003732out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003733 free(args);
3734 while (strings) {
3735 string = strings;
3736 strings = string->next;
3737 free(string->str);
3738 free(string);
3739 }
3740
3741 out:
3742 /* TBD : handle return type here */
3743 return ret;
3744}
3745
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003746static void free_args(struct print_arg *args)
3747{
3748 struct print_arg *next;
3749
3750 while (args) {
3751 next = args->next;
3752
3753 free_arg(args);
3754 args = next;
3755 }
3756}
3757
Steven Rostedtf7d82352012-04-06 00:47:53 +02003758static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3759{
3760 struct pevent *pevent = event->pevent;
3761 struct format_field *field, *ip_field;
3762 struct print_arg *args, *arg, **next;
3763 unsigned long long ip, val;
3764 char *ptr;
3765 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003766 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003767
3768 field = pevent->bprint_buf_field;
3769 ip_field = pevent->bprint_ip_field;
3770
3771 if (!field) {
3772 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003773 if (!field) {
3774 do_warning("can't find buffer field for binary printk");
3775 return NULL;
3776 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003777 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003778 if (!ip_field) {
3779 do_warning("can't find ip field for binary printk");
3780 return NULL;
3781 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003782 pevent->bprint_buf_field = field;
3783 pevent->bprint_ip_field = ip_field;
3784 }
3785
3786 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3787
3788 /*
3789 * The first arg is the IP pointer.
3790 */
3791 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003792 if (!args) {
3793 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3794 return NULL;
3795 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003796 arg = args;
3797 arg->next = NULL;
3798 next = &arg->next;
3799
3800 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003801
3802 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3803 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003804
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003805 /* skip the first "%pf: " */
3806 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003807 bptr < data + size && *ptr; ptr++) {
3808 int ls = 0;
3809
3810 if (*ptr == '%') {
3811 process_again:
3812 ptr++;
3813 switch (*ptr) {
3814 case '%':
3815 break;
3816 case 'l':
3817 ls++;
3818 goto process_again;
3819 case 'L':
3820 ls = 2;
3821 goto process_again;
3822 case '0' ... '9':
3823 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003824 case '.':
3825 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003826 case 'p':
3827 ls = 1;
3828 /* fall through */
3829 case 'd':
3830 case 'u':
3831 case 'x':
3832 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003833 switch (ls) {
3834 case 0:
3835 vsize = 4;
3836 break;
3837 case 1:
3838 vsize = pevent->long_size;
3839 break;
3840 case 2:
3841 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003842 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003843 default:
3844 vsize = ls; /* ? */
3845 break;
3846 }
3847 /* fall through */
3848 case '*':
3849 if (*ptr == '*')
3850 vsize = 4;
3851
Steven Rostedtf7d82352012-04-06 00:47:53 +02003852 /* the pointers are always 4 bytes aligned */
3853 bptr = (void *)(((unsigned long)bptr + 3) &
3854 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003855 val = pevent_read_number(pevent, bptr, vsize);
3856 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003857 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003858 if (!arg) {
3859 do_warning("%s(%d): not enough memory!",
3860 __func__, __LINE__);
3861 goto out_free;
3862 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003863 arg->next = NULL;
3864 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003865 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3866 free(arg);
3867 goto out_free;
3868 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003869 *next = arg;
3870 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003871 /*
3872 * The '*' case means that an arg is used as the length.
3873 * We need to continue to figure out for what.
3874 */
3875 if (*ptr == '*')
3876 goto process_again;
3877
Steven Rostedtf7d82352012-04-06 00:47:53 +02003878 break;
3879 case 's':
3880 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003881 if (!arg) {
3882 do_warning("%s(%d): not enough memory!",
3883 __func__, __LINE__);
3884 goto out_free;
3885 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003886 arg->next = NULL;
3887 arg->type = PRINT_BSTRING;
3888 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003889 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003890 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003891 bptr += strlen(bptr) + 1;
3892 *next = arg;
3893 next = &arg->next;
3894 default:
3895 break;
3896 }
3897 }
3898 }
3899
3900 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003901
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003902out_free:
3903 free_args(args);
3904 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003905}
3906
3907static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003908get_bprint_format(void *data, int size __maybe_unused,
3909 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003910{
3911 struct pevent *pevent = event->pevent;
3912 unsigned long long addr;
3913 struct format_field *field;
3914 struct printk_map *printk;
3915 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003916
3917 field = pevent->bprint_fmt_field;
3918
3919 if (!field) {
3920 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003921 if (!field) {
3922 do_warning("can't find format field for binary printk");
3923 return NULL;
3924 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003925 pevent->bprint_fmt_field = field;
3926 }
3927
3928 addr = pevent_read_number(pevent, data + field->offset, field->size);
3929
3930 printk = find_printk(pevent, addr);
3931 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003932 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003933 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003934 return format;
3935 }
3936
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003937 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003938 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003939
3940 return format;
3941}
3942
3943static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3944 struct event_format *event, struct print_arg *arg)
3945{
3946 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03003947 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02003948
3949 if (arg->type == PRINT_FUNC) {
3950 process_defined_func(s, data, size, event, arg);
3951 return;
3952 }
3953
3954 if (arg->type != PRINT_FIELD) {
3955 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3956 arg->type);
3957 return;
3958 }
3959
3960 if (mac == 'm')
3961 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3962 if (!arg->field.field) {
3963 arg->field.field =
3964 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003965 if (!arg->field.field) {
3966 do_warning("%s: field %s not found",
3967 __func__, arg->field.name);
3968 return;
3969 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003970 }
3971 if (arg->field.field->size != 6) {
3972 trace_seq_printf(s, "INVALIDMAC");
3973 return;
3974 }
3975 buf = data + arg->field.field->offset;
3976 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3977}
3978
Namhyung Kim600da3c2012-06-22 17:10:15 +09003979static int is_printable_array(char *p, unsigned int len)
3980{
3981 unsigned int i;
3982
3983 for (i = 0; i < len && p[i]; i++)
3984 if (!isprint(p[i]))
3985 return 0;
3986 return 1;
3987}
3988
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03003989static void print_event_fields(struct trace_seq *s, void *data,
3990 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003991 struct event_format *event)
3992{
3993 struct format_field *field;
3994 unsigned long long val;
3995 unsigned int offset, len, i;
3996
3997 field = event->format.fields;
3998 while (field) {
3999 trace_seq_printf(s, " %s=", field->name);
4000 if (field->flags & FIELD_IS_ARRAY) {
4001 offset = field->offset;
4002 len = field->size;
4003 if (field->flags & FIELD_IS_DYNAMIC) {
4004 val = pevent_read_number(event->pevent, data + offset, len);
4005 offset = val;
4006 len = offset >> 16;
4007 offset &= 0xffff;
4008 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09004009 if (field->flags & FIELD_IS_STRING &&
4010 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004011 trace_seq_printf(s, "%s", (char *)data + offset);
4012 } else {
4013 trace_seq_puts(s, "ARRAY[");
4014 for (i = 0; i < len; i++) {
4015 if (i)
4016 trace_seq_puts(s, ", ");
4017 trace_seq_printf(s, "%02x",
4018 *((unsigned char *)data + offset + i));
4019 }
4020 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09004021 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004022 }
4023 } else {
4024 val = pevent_read_number(event->pevent, data + field->offset,
4025 field->size);
4026 if (field->flags & FIELD_IS_POINTER) {
4027 trace_seq_printf(s, "0x%llx", val);
4028 } else if (field->flags & FIELD_IS_SIGNED) {
4029 switch (field->size) {
4030 case 4:
4031 /*
4032 * If field is long then print it in hex.
4033 * A long usually stores pointers.
4034 */
4035 if (field->flags & FIELD_IS_LONG)
4036 trace_seq_printf(s, "0x%x", (int)val);
4037 else
4038 trace_seq_printf(s, "%d", (int)val);
4039 break;
4040 case 2:
4041 trace_seq_printf(s, "%2d", (short)val);
4042 break;
4043 case 1:
4044 trace_seq_printf(s, "%1d", (char)val);
4045 break;
4046 default:
4047 trace_seq_printf(s, "%lld", val);
4048 }
4049 } else {
4050 if (field->flags & FIELD_IS_LONG)
4051 trace_seq_printf(s, "0x%llx", val);
4052 else
4053 trace_seq_printf(s, "%llu", val);
4054 }
4055 }
4056 field = field->next;
4057 }
4058}
4059
4060static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4061{
4062 struct pevent *pevent = event->pevent;
4063 struct print_fmt *print_fmt = &event->print_fmt;
4064 struct print_arg *arg = print_fmt->args;
4065 struct print_arg *args = NULL;
4066 const char *ptr = print_fmt->format;
4067 unsigned long long val;
4068 struct func_map *func;
4069 const char *saveptr;
4070 char *bprint_fmt = NULL;
4071 char format[32];
4072 int show_func;
4073 int len_as_arg;
4074 int len_arg;
4075 int len;
4076 int ls;
4077
4078 if (event->flags & EVENT_FL_FAILED) {
4079 trace_seq_printf(s, "[FAILED TO PARSE]");
4080 print_event_fields(s, data, size, event);
4081 return;
4082 }
4083
4084 if (event->flags & EVENT_FL_ISBPRINT) {
4085 bprint_fmt = get_bprint_format(data, size, event);
4086 args = make_bprint_args(bprint_fmt, data, size, event);
4087 arg = args;
4088 ptr = bprint_fmt;
4089 }
4090
4091 for (; *ptr; ptr++) {
4092 ls = 0;
4093 if (*ptr == '\\') {
4094 ptr++;
4095 switch (*ptr) {
4096 case 'n':
4097 trace_seq_putc(s, '\n');
4098 break;
4099 case 't':
4100 trace_seq_putc(s, '\t');
4101 break;
4102 case 'r':
4103 trace_seq_putc(s, '\r');
4104 break;
4105 case '\\':
4106 trace_seq_putc(s, '\\');
4107 break;
4108 default:
4109 trace_seq_putc(s, *ptr);
4110 break;
4111 }
4112
4113 } else if (*ptr == '%') {
4114 saveptr = ptr;
4115 show_func = 0;
4116 len_as_arg = 0;
4117 cont_process:
4118 ptr++;
4119 switch (*ptr) {
4120 case '%':
4121 trace_seq_putc(s, '%');
4122 break;
4123 case '#':
4124 /* FIXME: need to handle properly */
4125 goto cont_process;
4126 case 'h':
4127 ls--;
4128 goto cont_process;
4129 case 'l':
4130 ls++;
4131 goto cont_process;
4132 case 'L':
4133 ls = 2;
4134 goto cont_process;
4135 case '*':
4136 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004137 if (!arg) {
4138 do_warning("no argument match");
4139 event->flags |= EVENT_FL_FAILED;
4140 goto out_failed;
4141 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004142 len_arg = eval_num_arg(data, size, event, arg);
4143 len_as_arg = 1;
4144 arg = arg->next;
4145 goto cont_process;
4146 case '.':
4147 case 'z':
4148 case 'Z':
4149 case '0' ... '9':
4150 goto cont_process;
4151 case 'p':
4152 if (pevent->long_size == 4)
4153 ls = 1;
4154 else
4155 ls = 2;
4156
4157 if (*(ptr+1) == 'F' ||
4158 *(ptr+1) == 'f') {
4159 ptr++;
4160 show_func = *ptr;
4161 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4162 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4163 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004164 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004165 break;
4166 }
4167
4168 /* fall through */
4169 case 'd':
4170 case 'i':
4171 case 'x':
4172 case 'X':
4173 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004174 if (!arg) {
4175 do_warning("no argument match");
4176 event->flags |= EVENT_FL_FAILED;
4177 goto out_failed;
4178 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004179
4180 len = ((unsigned long)ptr + 1) -
4181 (unsigned long)saveptr;
4182
4183 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004184 if (len > 31) {
4185 do_warning("bad format!");
4186 event->flags |= EVENT_FL_FAILED;
4187 len = 31;
4188 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004189
4190 memcpy(format, saveptr, len);
4191 format[len] = 0;
4192
4193 val = eval_num_arg(data, size, event, arg);
4194 arg = arg->next;
4195
4196 if (show_func) {
4197 func = find_func(pevent, val);
4198 if (func) {
4199 trace_seq_puts(s, func->func);
4200 if (show_func == 'F')
4201 trace_seq_printf(s,
4202 "+0x%llx",
4203 val - func->addr);
4204 break;
4205 }
4206 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004207 if (pevent->long_size == 8 && ls &&
4208 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004209 char *p;
4210
4211 ls = 2;
4212 /* make %l into %ll */
4213 p = strchr(format, 'l');
4214 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004215 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004216 else if (strcmp(format, "%p") == 0)
4217 strcpy(format, "0x%llx");
4218 }
4219 switch (ls) {
4220 case -2:
4221 if (len_as_arg)
4222 trace_seq_printf(s, format, len_arg, (char)val);
4223 else
4224 trace_seq_printf(s, format, (char)val);
4225 break;
4226 case -1:
4227 if (len_as_arg)
4228 trace_seq_printf(s, format, len_arg, (short)val);
4229 else
4230 trace_seq_printf(s, format, (short)val);
4231 break;
4232 case 0:
4233 if (len_as_arg)
4234 trace_seq_printf(s, format, len_arg, (int)val);
4235 else
4236 trace_seq_printf(s, format, (int)val);
4237 break;
4238 case 1:
4239 if (len_as_arg)
4240 trace_seq_printf(s, format, len_arg, (long)val);
4241 else
4242 trace_seq_printf(s, format, (long)val);
4243 break;
4244 case 2:
4245 if (len_as_arg)
4246 trace_seq_printf(s, format, len_arg,
4247 (long long)val);
4248 else
4249 trace_seq_printf(s, format, (long long)val);
4250 break;
4251 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004252 do_warning("bad count (%d)", ls);
4253 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004254 }
4255 break;
4256 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004257 if (!arg) {
4258 do_warning("no matching argument");
4259 event->flags |= EVENT_FL_FAILED;
4260 goto out_failed;
4261 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004262
4263 len = ((unsigned long)ptr + 1) -
4264 (unsigned long)saveptr;
4265
4266 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004267 if (len > 31) {
4268 do_warning("bad format!");
4269 event->flags |= EVENT_FL_FAILED;
4270 len = 31;
4271 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004272
4273 memcpy(format, saveptr, len);
4274 format[len] = 0;
4275 if (!len_as_arg)
4276 len_arg = -1;
4277 print_str_arg(s, data, size, event,
4278 format, len_arg, arg);
4279 arg = arg->next;
4280 break;
4281 default:
4282 trace_seq_printf(s, ">%c<", *ptr);
4283
4284 }
4285 } else
4286 trace_seq_putc(s, *ptr);
4287 }
4288
Namhyung Kim245c5a12012-09-07 11:49:45 +09004289 if (event->flags & EVENT_FL_FAILED) {
4290out_failed:
4291 trace_seq_printf(s, "[FAILED TO PARSE]");
4292 }
4293
Steven Rostedtf7d82352012-04-06 00:47:53 +02004294 if (args) {
4295 free_args(args);
4296 free(bprint_fmt);
4297 }
4298}
4299
4300/**
4301 * pevent_data_lat_fmt - parse the data for the latency format
4302 * @pevent: a handle to the pevent
4303 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004304 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004305 *
4306 * This parses out the Latency format (interrupts disabled,
4307 * need rescheduling, in hard/soft interrupt, preempt count
4308 * and lock depth) and places it into the trace_seq.
4309 */
4310void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004311 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004312{
4313 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004314 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004315 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004316 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004317 unsigned int lat_flags;
4318 unsigned int pc;
4319 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004320 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004321 int hardirq;
4322 int softirq;
4323 void *data = record->data;
4324
4325 lat_flags = parse_common_flags(pevent, data);
4326 pc = parse_common_pc(pevent, data);
4327 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004328 if (lock_depth_exists)
4329 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004330 else if (check_lock_depth) {
4331 lock_depth = parse_common_lock_depth(pevent, data);
4332 if (lock_depth < 0)
4333 check_lock_depth = 0;
4334 else
4335 lock_depth_exists = 1;
4336 }
4337
4338 /* migrate_disable may not always exist */
4339 if (migrate_disable_exists)
4340 migrate_disable = parse_common_migrate_disable(pevent, data);
4341 else if (check_migrate_disable) {
4342 migrate_disable = parse_common_migrate_disable(pevent, data);
4343 if (migrate_disable < 0)
4344 check_migrate_disable = 0;
4345 else
4346 migrate_disable_exists = 1;
4347 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004348
4349 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4350 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4351
4352 trace_seq_printf(s, "%c%c%c",
4353 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4354 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4355 'X' : '.',
4356 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4357 'N' : '.',
4358 (hardirq && softirq) ? 'H' :
4359 hardirq ? 'h' : softirq ? 's' : '.');
4360
4361 if (pc)
4362 trace_seq_printf(s, "%x", pc);
4363 else
4364 trace_seq_putc(s, '.');
4365
Steven Rostedt0866a972012-05-22 14:52:40 +09004366 if (migrate_disable_exists) {
4367 if (migrate_disable < 0)
4368 trace_seq_putc(s, '.');
4369 else
4370 trace_seq_printf(s, "%d", migrate_disable);
4371 }
4372
Steven Rostedtf7d82352012-04-06 00:47:53 +02004373 if (lock_depth_exists) {
4374 if (lock_depth < 0)
4375 trace_seq_putc(s, '.');
4376 else
4377 trace_seq_printf(s, "%d", lock_depth);
4378 }
4379
4380 trace_seq_terminate(s);
4381}
4382
4383/**
4384 * pevent_data_type - parse out the given event type
4385 * @pevent: a handle to the pevent
4386 * @rec: the record to read from
4387 *
4388 * This returns the event id from the @rec.
4389 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004390int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004391{
4392 return trace_parse_common_type(pevent, rec->data);
4393}
4394
4395/**
4396 * pevent_data_event_from_type - find the event by a given type
4397 * @pevent: a handle to the pevent
4398 * @type: the type of the event.
4399 *
4400 * This returns the event form a given @type;
4401 */
4402struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4403{
4404 return pevent_find_event(pevent, type);
4405}
4406
4407/**
4408 * pevent_data_pid - parse the PID from raw data
4409 * @pevent: a handle to the pevent
4410 * @rec: the record to parse
4411 *
4412 * This returns the PID from a raw data.
4413 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004414int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004415{
4416 return parse_common_pid(pevent, rec->data);
4417}
4418
4419/**
4420 * pevent_data_comm_from_pid - return the command line from PID
4421 * @pevent: a handle to the pevent
4422 * @pid: the PID of the task to search for
4423 *
4424 * This returns a pointer to the command line that has the given
4425 * @pid.
4426 */
4427const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4428{
4429 const char *comm;
4430
4431 comm = find_cmdline(pevent, pid);
4432 return comm;
4433}
4434
4435/**
4436 * pevent_data_comm_from_pid - parse the data into the print format
4437 * @s: the trace_seq to write to
4438 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004439 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004440 *
4441 * This parses the raw @data using the given @event information and
4442 * writes the print format into the trace_seq.
4443 */
4444void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004445 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004446{
4447 int print_pretty = 1;
4448
4449 if (event->pevent->print_raw)
4450 print_event_fields(s, record->data, record->size, event);
4451 else {
4452
4453 if (event->handler)
4454 print_pretty = event->handler(s, record, event,
4455 event->context);
4456
4457 if (print_pretty)
4458 pretty_print(s, record->data, record->size, event);
4459 }
4460
4461 trace_seq_terminate(s);
4462}
4463
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004464static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4465{
4466 if (!use_trace_clock)
4467 return true;
4468
4469 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4470 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4471 return true;
4472
4473 /* trace_clock is setting in tsc or counter mode */
4474 return false;
4475}
4476
Steven Rostedtf7d82352012-04-06 00:47:53 +02004477void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004478 struct pevent_record *record, bool use_trace_clock)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004479{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004480 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004481 struct event_format *event;
4482 unsigned long secs;
4483 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004484 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004485 const char *comm;
4486 void *data = record->data;
4487 int type;
4488 int pid;
4489 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004490 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004491 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004492
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004493 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4494 use_trace_clock);
4495 if (use_usec_format) {
4496 secs = record->ts / NSECS_PER_SEC;
4497 nsecs = record->ts - secs * NSECS_PER_SEC;
4498 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004499
4500 if (record->size < 0) {
4501 do_warning("ug! negative record size %d", record->size);
4502 return;
4503 }
4504
4505 type = trace_parse_common_type(pevent, data);
4506
4507 event = pevent_find_event(pevent, type);
4508 if (!event) {
4509 do_warning("ug! no event found for type %d", type);
4510 return;
4511 }
4512
4513 pid = parse_common_pid(pevent, data);
4514 comm = find_cmdline(pevent, pid);
4515
4516 if (pevent->latency_format) {
4517 trace_seq_printf(s, "%8.8s-%-5d %3d",
4518 comm, pid, record->cpu);
4519 pevent_data_lat_fmt(pevent, s, record);
4520 } else
4521 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4522
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004523 if (use_usec_format) {
4524 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4525 usecs = nsecs;
4526 p = 9;
4527 } else {
4528 usecs = (nsecs + 500) / NSECS_PER_USEC;
4529 p = 6;
4530 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02004531
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004532 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4533 secs, p, usecs, event->name);
4534 } else
4535 trace_seq_printf(s, " %12llu: %s: ",
4536 record->ts, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004537
4538 /* Space out the event names evenly. */
4539 len = strlen(event->name);
4540 if (len < 20)
4541 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4542
4543 pevent_event_info(s, event, record);
4544}
4545
4546static int events_id_cmp(const void *a, const void *b)
4547{
4548 struct event_format * const * ea = a;
4549 struct event_format * const * eb = b;
4550
4551 if ((*ea)->id < (*eb)->id)
4552 return -1;
4553
4554 if ((*ea)->id > (*eb)->id)
4555 return 1;
4556
4557 return 0;
4558}
4559
4560static int events_name_cmp(const void *a, const void *b)
4561{
4562 struct event_format * const * ea = a;
4563 struct event_format * const * eb = b;
4564 int res;
4565
4566 res = strcmp((*ea)->name, (*eb)->name);
4567 if (res)
4568 return res;
4569
4570 res = strcmp((*ea)->system, (*eb)->system);
4571 if (res)
4572 return res;
4573
4574 return events_id_cmp(a, b);
4575}
4576
4577static int events_system_cmp(const void *a, const void *b)
4578{
4579 struct event_format * const * ea = a;
4580 struct event_format * const * eb = b;
4581 int res;
4582
4583 res = strcmp((*ea)->system, (*eb)->system);
4584 if (res)
4585 return res;
4586
4587 res = strcmp((*ea)->name, (*eb)->name);
4588 if (res)
4589 return res;
4590
4591 return events_id_cmp(a, b);
4592}
4593
4594struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4595{
4596 struct event_format **events;
4597 int (*sort)(const void *a, const void *b);
4598
4599 events = pevent->sort_events;
4600
4601 if (events && pevent->last_type == sort_type)
4602 return events;
4603
4604 if (!events) {
4605 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4606 if (!events)
4607 return NULL;
4608
4609 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4610 events[pevent->nr_events] = NULL;
4611
4612 pevent->sort_events = events;
4613
4614 /* the internal events are sorted by id */
4615 if (sort_type == EVENT_SORT_ID) {
4616 pevent->last_type = sort_type;
4617 return events;
4618 }
4619 }
4620
4621 switch (sort_type) {
4622 case EVENT_SORT_ID:
4623 sort = events_id_cmp;
4624 break;
4625 case EVENT_SORT_NAME:
4626 sort = events_name_cmp;
4627 break;
4628 case EVENT_SORT_SYSTEM:
4629 sort = events_system_cmp;
4630 break;
4631 default:
4632 return events;
4633 }
4634
4635 qsort(events, pevent->nr_events, sizeof(*events), sort);
4636 pevent->last_type = sort_type;
4637
4638 return events;
4639}
4640
4641static struct format_field **
4642get_event_fields(const char *type, const char *name,
4643 int count, struct format_field *list)
4644{
4645 struct format_field **fields;
4646 struct format_field *field;
4647 int i = 0;
4648
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004649 fields = malloc(sizeof(*fields) * (count + 1));
4650 if (!fields)
4651 return NULL;
4652
Steven Rostedtf7d82352012-04-06 00:47:53 +02004653 for (field = list; field; field = field->next) {
4654 fields[i++] = field;
4655 if (i == count + 1) {
4656 do_warning("event %s has more %s fields than specified",
4657 name, type);
4658 i--;
4659 break;
4660 }
4661 }
4662
4663 if (i != count)
4664 do_warning("event %s has less %s fields than specified",
4665 name, type);
4666
4667 fields[i] = NULL;
4668
4669 return fields;
4670}
4671
4672/**
4673 * pevent_event_common_fields - return a list of common fields for an event
4674 * @event: the event to return the common fields of.
4675 *
4676 * Returns an allocated array of fields. The last item in the array is NULL.
4677 * The array must be freed with free().
4678 */
4679struct format_field **pevent_event_common_fields(struct event_format *event)
4680{
4681 return get_event_fields("common", event->name,
4682 event->format.nr_common,
4683 event->format.common_fields);
4684}
4685
4686/**
4687 * pevent_event_fields - return a list of event specific fields for an event
4688 * @event: the event to return the fields of.
4689 *
4690 * Returns an allocated array of fields. The last item in the array is NULL.
4691 * The array must be freed with free().
4692 */
4693struct format_field **pevent_event_fields(struct event_format *event)
4694{
4695 return get_event_fields("event", event->name,
4696 event->format.nr_fields,
4697 event->format.fields);
4698}
4699
4700static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4701{
4702 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4703 if (field->next) {
4704 trace_seq_puts(s, ", ");
4705 print_fields(s, field->next);
4706 }
4707}
4708
4709/* for debugging */
4710static void print_args(struct print_arg *args)
4711{
4712 int print_paren = 1;
4713 struct trace_seq s;
4714
4715 switch (args->type) {
4716 case PRINT_NULL:
4717 printf("null");
4718 break;
4719 case PRINT_ATOM:
4720 printf("%s", args->atom.atom);
4721 break;
4722 case PRINT_FIELD:
4723 printf("REC->%s", args->field.name);
4724 break;
4725 case PRINT_FLAGS:
4726 printf("__print_flags(");
4727 print_args(args->flags.field);
4728 printf(", %s, ", args->flags.delim);
4729 trace_seq_init(&s);
4730 print_fields(&s, args->flags.flags);
4731 trace_seq_do_printf(&s);
4732 trace_seq_destroy(&s);
4733 printf(")");
4734 break;
4735 case PRINT_SYMBOL:
4736 printf("__print_symbolic(");
4737 print_args(args->symbol.field);
4738 printf(", ");
4739 trace_seq_init(&s);
4740 print_fields(&s, args->symbol.symbols);
4741 trace_seq_do_printf(&s);
4742 trace_seq_destroy(&s);
4743 printf(")");
4744 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004745 case PRINT_HEX:
4746 printf("__print_hex(");
4747 print_args(args->hex.field);
4748 printf(", ");
4749 print_args(args->hex.size);
4750 printf(")");
4751 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004752 case PRINT_STRING:
4753 case PRINT_BSTRING:
4754 printf("__get_str(%s)", args->string.string);
4755 break;
4756 case PRINT_TYPE:
4757 printf("(%s)", args->typecast.type);
4758 print_args(args->typecast.item);
4759 break;
4760 case PRINT_OP:
4761 if (strcmp(args->op.op, ":") == 0)
4762 print_paren = 0;
4763 if (print_paren)
4764 printf("(");
4765 print_args(args->op.left);
4766 printf(" %s ", args->op.op);
4767 print_args(args->op.right);
4768 if (print_paren)
4769 printf(")");
4770 break;
4771 default:
4772 /* we should warn... */
4773 return;
4774 }
4775 if (args->next) {
4776 printf("\n");
4777 print_args(args->next);
4778 }
4779}
4780
4781static void parse_header_field(const char *field,
4782 int *offset, int *size, int mandatory)
4783{
4784 unsigned long long save_input_buf_ptr;
4785 unsigned long long save_input_buf_siz;
4786 char *token;
4787 int type;
4788
4789 save_input_buf_ptr = input_buf_ptr;
4790 save_input_buf_siz = input_buf_siz;
4791
4792 if (read_expected(EVENT_ITEM, "field") < 0)
4793 return;
4794 if (read_expected(EVENT_OP, ":") < 0)
4795 return;
4796
4797 /* type */
4798 if (read_expect_type(EVENT_ITEM, &token) < 0)
4799 goto fail;
4800 free_token(token);
4801
4802 /*
4803 * If this is not a mandatory field, then test it first.
4804 */
4805 if (mandatory) {
4806 if (read_expected(EVENT_ITEM, field) < 0)
4807 return;
4808 } else {
4809 if (read_expect_type(EVENT_ITEM, &token) < 0)
4810 goto fail;
4811 if (strcmp(token, field) != 0)
4812 goto discard;
4813 free_token(token);
4814 }
4815
4816 if (read_expected(EVENT_OP, ";") < 0)
4817 return;
4818 if (read_expected(EVENT_ITEM, "offset") < 0)
4819 return;
4820 if (read_expected(EVENT_OP, ":") < 0)
4821 return;
4822 if (read_expect_type(EVENT_ITEM, &token) < 0)
4823 goto fail;
4824 *offset = atoi(token);
4825 free_token(token);
4826 if (read_expected(EVENT_OP, ";") < 0)
4827 return;
4828 if (read_expected(EVENT_ITEM, "size") < 0)
4829 return;
4830 if (read_expected(EVENT_OP, ":") < 0)
4831 return;
4832 if (read_expect_type(EVENT_ITEM, &token) < 0)
4833 goto fail;
4834 *size = atoi(token);
4835 free_token(token);
4836 if (read_expected(EVENT_OP, ";") < 0)
4837 return;
4838 type = read_token(&token);
4839 if (type != EVENT_NEWLINE) {
4840 /* newer versions of the kernel have a "signed" type */
4841 if (type != EVENT_ITEM)
4842 goto fail;
4843
4844 if (strcmp(token, "signed") != 0)
4845 goto fail;
4846
4847 free_token(token);
4848
4849 if (read_expected(EVENT_OP, ":") < 0)
4850 return;
4851
4852 if (read_expect_type(EVENT_ITEM, &token))
4853 goto fail;
4854
4855 free_token(token);
4856 if (read_expected(EVENT_OP, ";") < 0)
4857 return;
4858
4859 if (read_expect_type(EVENT_NEWLINE, &token))
4860 goto fail;
4861 }
4862 fail:
4863 free_token(token);
4864 return;
4865
4866 discard:
4867 input_buf_ptr = save_input_buf_ptr;
4868 input_buf_siz = save_input_buf_siz;
4869 *offset = 0;
4870 *size = 0;
4871 free_token(token);
4872}
4873
4874/**
4875 * pevent_parse_header_page - parse the data stored in the header page
4876 * @pevent: the handle to the pevent
4877 * @buf: the buffer storing the header page format string
4878 * @size: the size of @buf
4879 * @long_size: the long size to use if there is no header
4880 *
4881 * This parses the header page format for information on the
4882 * ring buffer used. The @buf should be copied from
4883 *
4884 * /sys/kernel/debug/tracing/events/header_page
4885 */
4886int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4887 int long_size)
4888{
4889 int ignore;
4890
4891 if (!size) {
4892 /*
4893 * Old kernels did not have header page info.
4894 * Sorry but we just use what we find here in user space.
4895 */
4896 pevent->header_page_ts_size = sizeof(long long);
4897 pevent->header_page_size_size = long_size;
4898 pevent->header_page_data_offset = sizeof(long long) + long_size;
4899 pevent->old_format = 1;
4900 return -1;
4901 }
4902 init_input_buf(buf, size);
4903
4904 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4905 &pevent->header_page_ts_size, 1);
4906 parse_header_field("commit", &pevent->header_page_size_offset,
4907 &pevent->header_page_size_size, 1);
4908 parse_header_field("overwrite", &pevent->header_page_overwrite,
4909 &ignore, 0);
4910 parse_header_field("data", &pevent->header_page_data_offset,
4911 &pevent->header_page_data_size, 1);
4912
4913 return 0;
4914}
4915
4916static int event_matches(struct event_format *event,
4917 int id, const char *sys_name,
4918 const char *event_name)
4919{
4920 if (id >= 0 && id != event->id)
4921 return 0;
4922
4923 if (event_name && (strcmp(event_name, event->name) != 0))
4924 return 0;
4925
4926 if (sys_name && (strcmp(sys_name, event->system) != 0))
4927 return 0;
4928
4929 return 1;
4930}
4931
4932static void free_handler(struct event_handler *handle)
4933{
4934 free((void *)handle->sys_name);
4935 free((void *)handle->event_name);
4936 free(handle);
4937}
4938
4939static int find_event_handle(struct pevent *pevent, struct event_format *event)
4940{
4941 struct event_handler *handle, **next;
4942
4943 for (next = &pevent->handlers; *next;
4944 next = &(*next)->next) {
4945 handle = *next;
4946 if (event_matches(event, handle->id,
4947 handle->sys_name,
4948 handle->event_name))
4949 break;
4950 }
4951
4952 if (!(*next))
4953 return 0;
4954
4955 pr_stat("overriding event (%d) %s:%s with new print handler",
4956 event->id, event->system, event->name);
4957
4958 event->handler = handle->func;
4959 event->context = handle->context;
4960
4961 *next = handle->next;
4962 free_handler(handle);
4963
4964 return 1;
4965}
4966
4967/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004968 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02004969 * @buf: the buffer storing the event format string
4970 * @size: the size of @buf
4971 * @sys: the system the event belongs to
4972 *
4973 * This parses the event format and creates an event structure
4974 * to quickly parse raw data for a given event.
4975 *
4976 * These files currently come from:
4977 *
4978 * /sys/kernel/debug/tracing/events/.../.../format
4979 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004980enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4981 struct pevent *pevent, const char *buf,
4982 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004983{
4984 struct event_format *event;
4985 int ret;
4986
4987 init_input_buf(buf, size);
4988
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004989 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02004990 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09004991 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004992
4993 event->name = event_read_name();
4994 if (!event->name) {
4995 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004996 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4997 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004998 }
4999
5000 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005001 event->flags |= EVENT_FL_ISFTRACE;
5002
5003 if (strcmp(event->name, "bprint") == 0)
5004 event->flags |= EVENT_FL_ISBPRINT;
5005 }
5006
5007 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005008 if (event->id < 0) {
5009 ret = PEVENT_ERRNO__READ_ID_FAILED;
5010 /*
5011 * This isn't an allocation error actually.
5012 * But as the ID is critical, just bail out.
5013 */
5014 goto event_alloc_failed;
5015 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005016
5017 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005018 if (!event->system) {
5019 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5020 goto event_alloc_failed;
5021 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005022
Steven Rostedt101782e2012-10-01 20:13:51 -04005023 /* Add pevent to event so that it can be referenced */
5024 event->pevent = pevent;
5025
Steven Rostedtf7d82352012-04-06 00:47:53 +02005026 ret = event_read_format(event);
5027 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005028 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5029 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005030 }
5031
5032 /*
5033 * If the event has an override, don't print warnings if the event
5034 * print format fails to parse.
5035 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005036 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005037 show_warning = 0;
5038
5039 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005040 show_warning = 1;
5041
Steven Rostedtf7d82352012-04-06 00:47:53 +02005042 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005043 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5044 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005045 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005046
5047 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5048 struct format_field *field;
5049 struct print_arg *arg, **list;
5050
5051 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005052 list = &event->print_fmt.args;
5053 for (field = event->format.fields; field; field = field->next) {
5054 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09005055 if (!arg) {
5056 event->flags |= EVENT_FL_FAILED;
5057 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5058 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005059 arg->type = PRINT_FIELD;
5060 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09005061 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09005062 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005063 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005064 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005065 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005066 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005067 *list = arg;
5068 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005069 }
5070 return 0;
5071 }
5072
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005073 return 0;
5074
5075 event_parse_failed:
5076 event->flags |= EVENT_FL_FAILED;
5077 return ret;
5078
5079 event_alloc_failed:
5080 free(event->system);
5081 free(event->name);
5082 free(event);
5083 *eventp = NULL;
5084 return ret;
5085}
5086
5087/**
5088 * pevent_parse_format - parse the event format
5089 * @buf: the buffer storing the event format string
5090 * @size: the size of @buf
5091 * @sys: the system the event belongs to
5092 *
5093 * This parses the event format and creates an event structure
5094 * to quickly parse raw data for a given event.
5095 *
5096 * These files currently come from:
5097 *
5098 * /sys/kernel/debug/tracing/events/.../.../format
5099 */
5100enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5101 unsigned long size, const char *sys)
5102{
5103 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5104}
5105
5106/**
5107 * pevent_parse_event - parse the event format
5108 * @pevent: the handle to the pevent
5109 * @buf: the buffer storing the event format string
5110 * @size: the size of @buf
5111 * @sys: the system the event belongs to
5112 *
5113 * This parses the event format and creates an event structure
5114 * to quickly parse raw data for a given event.
5115 *
5116 * These files currently come from:
5117 *
5118 * /sys/kernel/debug/tracing/events/.../.../format
5119 */
5120enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5121 unsigned long size, const char *sys)
5122{
5123 struct event_format *event = NULL;
5124 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5125
5126 if (event == NULL)
5127 return ret;
5128
Namhyung Kimf1b22562012-09-25 21:25:19 +09005129 if (add_event(pevent, event)) {
5130 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005131 goto event_add_failed;
Namhyung Kimf1b22562012-09-25 21:25:19 +09005132 }
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005133
Steven Rostedtf7d82352012-04-06 00:47:53 +02005134#define PRINT_ARGS 0
5135 if (PRINT_ARGS && event->print_fmt.args)
5136 print_args(event->print_fmt.args);
5137
5138 return 0;
5139
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005140event_add_failed:
Namhyung Kimf1b22562012-09-25 21:25:19 +09005141 pevent_free_format(event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005142 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005143}
5144
Namhyung Kim2f197b92012-08-22 16:00:30 +09005145#undef _PE
5146#define _PE(code, str) str
5147static const char * const pevent_error_str[] = {
5148 PEVENT_ERRORS
5149};
5150#undef _PE
5151
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005152int pevent_strerror(struct pevent *pevent __maybe_unused,
5153 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005154{
5155 int idx;
5156 const char *msg;
5157
5158 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005159 msg = strerror_r(errnum, buf, buflen);
5160 if (msg != buf) {
5161 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005162 memcpy(buf, msg, min(buflen - 1, len));
5163 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005164 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005165 return 0;
5166 }
5167
5168 if (errnum <= __PEVENT_ERRNO__START ||
5169 errnum >= __PEVENT_ERRNO__END)
5170 return -1;
5171
Namhyung Kimf63fe792012-08-23 16:37:00 +09005172 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005173 msg = pevent_error_str[idx];
5174
5175 switch (errnum) {
5176 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5177 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5178 case PEVENT_ERRNO__READ_ID_FAILED:
5179 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5180 case PEVENT_ERRNO__READ_PRINT_FAILED:
5181 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
Arnaldo Carvalho de Melo7a905612012-11-09 15:50:33 -03005182 case PEVENT_ERRNO__INVALID_ARG_TYPE:
Namhyung Kim2f197b92012-08-22 16:00:30 +09005183 snprintf(buf, buflen, "%s", msg);
5184 break;
5185
5186 default:
5187 /* cannot reach here */
5188 break;
5189 }
5190
5191 return 0;
5192}
5193
Steven Rostedtf7d82352012-04-06 00:47:53 +02005194int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005195 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005196 unsigned long long *val, int err)
5197{
5198 if (!field) {
5199 if (err)
5200 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5201 return -1;
5202 }
5203
5204 if (pevent_read_number_field(field, record->data, val)) {
5205 if (err)
5206 trace_seq_printf(s, " %s=INVALID", name);
5207 return -1;
5208 }
5209
5210 return 0;
5211}
5212
5213/**
5214 * pevent_get_field_raw - return the raw pointer into the data field
5215 * @s: The seq to print to on error
5216 * @event: the event that the field is for
5217 * @name: The name of the field
5218 * @record: The record with the field name.
5219 * @len: place to store the field length.
5220 * @err: print default error if failed.
5221 *
5222 * Returns a pointer into record->data of the field and places
5223 * the length of the field in @len.
5224 *
5225 * On failure, it returns NULL.
5226 */
5227void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005228 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005229 int *len, int err)
5230{
5231 struct format_field *field;
5232 void *data = record->data;
5233 unsigned offset;
5234 int dummy;
5235
5236 if (!event)
5237 return NULL;
5238
5239 field = pevent_find_field(event, name);
5240
5241 if (!field) {
5242 if (err)
5243 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5244 return NULL;
5245 }
5246
5247 /* Allow @len to be NULL */
5248 if (!len)
5249 len = &dummy;
5250
5251 offset = field->offset;
5252 if (field->flags & FIELD_IS_DYNAMIC) {
5253 offset = pevent_read_number(event->pevent,
5254 data + offset, field->size);
5255 *len = offset >> 16;
5256 offset &= 0xffff;
5257 } else
5258 *len = field->size;
5259
5260 return data + offset;
5261}
5262
5263/**
5264 * pevent_get_field_val - find a field and return its value
5265 * @s: The seq to print to on error
5266 * @event: the event that the field is for
5267 * @name: The name of the field
5268 * @record: The record with the field name.
5269 * @val: place to store the value of the field.
5270 * @err: print default error if failed.
5271 *
5272 * Returns 0 on success -1 on field not found.
5273 */
5274int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005275 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005276 unsigned long long *val, int err)
5277{
5278 struct format_field *field;
5279
5280 if (!event)
5281 return -1;
5282
5283 field = pevent_find_field(event, name);
5284
5285 return get_field_val(s, field, name, record, val, err);
5286}
5287
5288/**
5289 * pevent_get_common_field_val - find a common field and return its value
5290 * @s: The seq to print to on error
5291 * @event: the event that the field is for
5292 * @name: The name of the field
5293 * @record: The record with the field name.
5294 * @val: place to store the value of the field.
5295 * @err: print default error if failed.
5296 *
5297 * Returns 0 on success -1 on field not found.
5298 */
5299int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005300 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005301 unsigned long long *val, int err)
5302{
5303 struct format_field *field;
5304
5305 if (!event)
5306 return -1;
5307
5308 field = pevent_find_common_field(event, name);
5309
5310 return get_field_val(s, field, name, record, val, err);
5311}
5312
5313/**
5314 * pevent_get_any_field_val - find a any field and return its value
5315 * @s: The seq to print to on error
5316 * @event: the event that the field is for
5317 * @name: The name of the field
5318 * @record: The record with the field name.
5319 * @val: place to store the value of the field.
5320 * @err: print default error if failed.
5321 *
5322 * Returns 0 on success -1 on field not found.
5323 */
5324int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005325 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005326 unsigned long long *val, int err)
5327{
5328 struct format_field *field;
5329
5330 if (!event)
5331 return -1;
5332
5333 field = pevent_find_any_field(event, name);
5334
5335 return get_field_val(s, field, name, record, val, err);
5336}
5337
5338/**
5339 * pevent_print_num_field - print a field and a format
5340 * @s: The seq to print to
5341 * @fmt: The printf format to print the field with.
5342 * @event: the event that the field is for
5343 * @name: The name of the field
5344 * @record: The record with the field name.
5345 * @err: print default error if failed.
5346 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005347 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005348 */
5349int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5350 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005351 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005352{
5353 struct format_field *field = pevent_find_field(event, name);
5354 unsigned long long val;
5355
5356 if (!field)
5357 goto failed;
5358
5359 if (pevent_read_number_field(field, record->data, &val))
5360 goto failed;
5361
5362 return trace_seq_printf(s, fmt, val);
5363
5364 failed:
5365 if (err)
5366 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5367 return -1;
5368}
5369
5370static void free_func_handle(struct pevent_function_handler *func)
5371{
5372 struct pevent_func_params *params;
5373
5374 free(func->name);
5375
5376 while (func->params) {
5377 params = func->params;
5378 func->params = params->next;
5379 free(params);
5380 }
5381
5382 free(func);
5383}
5384
5385/**
5386 * pevent_register_print_function - register a helper function
5387 * @pevent: the handle to the pevent
5388 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005389 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005390 * @name: the name of the helper function
5391 * @parameters: A list of enum pevent_func_arg_type
5392 *
5393 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005394 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005395 * of these functions.
5396 *
5397 * The @parameters is a variable list of pevent_func_arg_type enums that
5398 * must end with PEVENT_FUNC_ARG_VOID.
5399 */
5400int pevent_register_print_function(struct pevent *pevent,
5401 pevent_func_handler func,
5402 enum pevent_func_arg_type ret_type,
5403 char *name, ...)
5404{
5405 struct pevent_function_handler *func_handle;
5406 struct pevent_func_params **next_param;
5407 struct pevent_func_params *param;
5408 enum pevent_func_arg_type type;
5409 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005410 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005411
5412 func_handle = find_func_handler(pevent, name);
5413 if (func_handle) {
5414 /*
5415 * This is most like caused by the users own
5416 * plugins updating the function. This overrides the
5417 * system defaults.
5418 */
5419 pr_stat("override of function helper '%s'", name);
5420 remove_func_handler(pevent, name);
5421 }
5422
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005423 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005424 if (!func_handle) {
5425 do_warning("Failed to allocate function handler");
5426 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5427 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005428
5429 func_handle->ret_type = ret_type;
5430 func_handle->name = strdup(name);
5431 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005432 if (!func_handle->name) {
5433 do_warning("Failed to allocate function name");
5434 free(func_handle);
5435 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5436 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005437
5438 next_param = &(func_handle->params);
5439 va_start(ap, name);
5440 for (;;) {
5441 type = va_arg(ap, enum pevent_func_arg_type);
5442 if (type == PEVENT_FUNC_ARG_VOID)
5443 break;
5444
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03005445 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005446 do_warning("Invalid argument type %d", type);
5447 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005448 goto out_free;
5449 }
5450
Namhyung Kim67ed9392012-09-07 11:49:47 +09005451 param = malloc(sizeof(*param));
5452 if (!param) {
5453 do_warning("Failed to allocate function param");
5454 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5455 goto out_free;
5456 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005457 param->type = type;
5458 param->next = NULL;
5459
5460 *next_param = param;
5461 next_param = &(param->next);
5462
5463 func_handle->nr_args++;
5464 }
5465 va_end(ap);
5466
5467 func_handle->next = pevent->func_handlers;
5468 pevent->func_handlers = func_handle;
5469
5470 return 0;
5471 out_free:
5472 va_end(ap);
5473 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005474 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005475}
5476
5477/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005478 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005479 * @pevent: the handle to the pevent
5480 * @id: the id of the event to register
5481 * @sys_name: the system name the event belongs to
5482 * @event_name: the name of the event
5483 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005484 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005485 *
5486 * This function allows a developer to override the parsing of
5487 * a given event. If for some reason the default print format
5488 * is not sufficient, this function will register a function
5489 * for an event to be used to parse the data instead.
5490 *
5491 * If @id is >= 0, then it is used to find the event.
5492 * else @sys_name and @event_name are used.
5493 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09005494int pevent_register_event_handler(struct pevent *pevent, int id,
5495 const char *sys_name, const char *event_name,
5496 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005497{
5498 struct event_format *event;
5499 struct event_handler *handle;
5500
5501 if (id >= 0) {
5502 /* search by id */
5503 event = pevent_find_event(pevent, id);
5504 if (!event)
5505 goto not_found;
5506 if (event_name && (strcmp(event_name, event->name) != 0))
5507 goto not_found;
5508 if (sys_name && (strcmp(sys_name, event->system) != 0))
5509 goto not_found;
5510 } else {
5511 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5512 if (!event)
5513 goto not_found;
5514 }
5515
5516 pr_stat("overriding event (%d) %s:%s with new print handler",
5517 event->id, event->system, event->name);
5518
5519 event->handler = func;
5520 event->context = context;
5521 return 0;
5522
5523 not_found:
5524 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005525 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005526 if (!handle) {
5527 do_warning("Failed to allocate event handler");
5528 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5529 }
5530
Steven Rostedtf7d82352012-04-06 00:47:53 +02005531 handle->id = id;
5532 if (event_name)
5533 handle->event_name = strdup(event_name);
5534 if (sys_name)
5535 handle->sys_name = strdup(sys_name);
5536
Namhyung Kimca638582012-04-09 11:54:31 +09005537 if ((event_name && !handle->event_name) ||
5538 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005539 do_warning("Failed to allocate event/sys name");
5540 free((void *)handle->event_name);
5541 free((void *)handle->sys_name);
5542 free(handle);
5543 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005544 }
5545
Steven Rostedtf7d82352012-04-06 00:47:53 +02005546 handle->func = func;
5547 handle->next = pevent->handlers;
5548 pevent->handlers = handle;
5549 handle->context = context;
5550
5551 return -1;
5552}
5553
5554/**
5555 * pevent_alloc - create a pevent handle
5556 */
5557struct pevent *pevent_alloc(void)
5558{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005559 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005560
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005561 if (pevent)
5562 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005563
5564 return pevent;
5565}
5566
5567void pevent_ref(struct pevent *pevent)
5568{
5569 pevent->ref_count++;
5570}
5571
5572static void free_format_fields(struct format_field *field)
5573{
5574 struct format_field *next;
5575
5576 while (field) {
5577 next = field->next;
5578 free(field->type);
5579 free(field->name);
5580 free(field);
5581 field = next;
5582 }
5583}
5584
5585static void free_formats(struct format *format)
5586{
5587 free_format_fields(format->common_fields);
5588 free_format_fields(format->fields);
5589}
5590
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005591void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005592{
5593 free(event->name);
5594 free(event->system);
5595
5596 free_formats(&event->format);
5597
5598 free(event->print_fmt.format);
5599 free_args(event->print_fmt.args);
5600
5601 free(event);
5602}
5603
5604/**
5605 * pevent_free - free a pevent handle
5606 * @pevent: the pevent handle to free
5607 */
5608void pevent_free(struct pevent *pevent)
5609{
Steven Rostedta2525a02012-04-06 00:48:02 +02005610 struct cmdline_list *cmdlist, *cmdnext;
5611 struct func_list *funclist, *funcnext;
5612 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005613 struct pevent_function_handler *func_handler;
5614 struct event_handler *handle;
5615 int i;
5616
Steven Rostedta2525a02012-04-06 00:48:02 +02005617 if (!pevent)
5618 return;
5619
5620 cmdlist = pevent->cmdlist;
5621 funclist = pevent->funclist;
5622 printklist = pevent->printklist;
5623
Steven Rostedtf7d82352012-04-06 00:47:53 +02005624 pevent->ref_count--;
5625 if (pevent->ref_count)
5626 return;
5627
5628 if (pevent->cmdlines) {
5629 for (i = 0; i < pevent->cmdline_count; i++)
5630 free(pevent->cmdlines[i].comm);
5631 free(pevent->cmdlines);
5632 }
5633
5634 while (cmdlist) {
5635 cmdnext = cmdlist->next;
5636 free(cmdlist->comm);
5637 free(cmdlist);
5638 cmdlist = cmdnext;
5639 }
5640
5641 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005642 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005643 free(pevent->func_map[i].func);
5644 free(pevent->func_map[i].mod);
5645 }
5646 free(pevent->func_map);
5647 }
5648
5649 while (funclist) {
5650 funcnext = funclist->next;
5651 free(funclist->func);
5652 free(funclist->mod);
5653 free(funclist);
5654 funclist = funcnext;
5655 }
5656
5657 while (pevent->func_handlers) {
5658 func_handler = pevent->func_handlers;
5659 pevent->func_handlers = func_handler->next;
5660 free_func_handle(func_handler);
5661 }
5662
5663 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005664 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005665 free(pevent->printk_map[i].printk);
5666 free(pevent->printk_map);
5667 }
5668
5669 while (printklist) {
5670 printknext = printklist->next;
5671 free(printklist->printk);
5672 free(printklist);
5673 printklist = printknext;
5674 }
5675
5676 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005677 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005678
5679 while (pevent->handlers) {
5680 handle = pevent->handlers;
5681 pevent->handlers = handle->next;
5682 free_handler(handle);
5683 }
5684
5685 free(pevent->events);
5686 free(pevent->sort_events);
5687
5688 free(pevent);
5689}
5690
5691void pevent_unref(struct pevent *pevent)
5692{
5693 pevent_free(pevent);
5694}