blob: 900fca01bdd3fcbc8617929a822ba5d90ba47ae0 [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
Steven Rostedteff2c922013-11-18 14:23:14 -05001609/*
1610 * For __print_symbolic() and __print_flags, we need to completely
1611 * evaluate the first argument, which defines what to print next.
1612 */
1613static enum event_type
1614process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1615{
1616 enum event_type type;
1617
1618 type = process_arg(event, arg, tok);
1619
1620 while (type == EVENT_OP) {
1621 type = process_op(event, arg, tok);
1622 }
1623
1624 return type;
1625}
1626
Steven Rostedtf7d82352012-04-06 00:47:53 +02001627static enum event_type
1628process_cond(struct event_format *event, struct print_arg *top, char **tok)
1629{
1630 struct print_arg *arg, *left, *right;
1631 enum event_type type;
1632 char *token = NULL;
1633
1634 arg = alloc_arg();
1635 left = alloc_arg();
1636 right = alloc_arg();
1637
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001638 if (!arg || !left || !right) {
1639 do_warning("%s: not enough memory!", __func__);
1640 /* arg will be freed at out_free */
1641 free_arg(left);
1642 free_arg(right);
1643 goto out_free;
1644 }
1645
Steven Rostedtf7d82352012-04-06 00:47:53 +02001646 arg->type = PRINT_OP;
1647 arg->op.left = left;
1648 arg->op.right = right;
1649
1650 *tok = NULL;
1651 type = process_arg(event, left, &token);
1652
1653 again:
1654 /* Handle other operations in the arguments */
1655 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1656 type = process_op(event, left, &token);
1657 goto again;
1658 }
1659
1660 if (test_type_token(type, token, EVENT_OP, ":"))
1661 goto out_free;
1662
1663 arg->op.op = token;
1664
1665 type = process_arg(event, right, &token);
1666
1667 top->op.right = arg;
1668
1669 *tok = token;
1670 return type;
1671
1672out_free:
1673 /* Top may point to itself */
1674 top->op.right = NULL;
1675 free_token(token);
1676 free_arg(arg);
1677 return EVENT_ERROR;
1678}
1679
1680static enum event_type
1681process_array(struct event_format *event, struct print_arg *top, char **tok)
1682{
1683 struct print_arg *arg;
1684 enum event_type type;
1685 char *token = NULL;
1686
1687 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001688 if (!arg) {
1689 do_warning("%s: not enough memory!", __func__);
1690 /* '*tok' is set to top->op.op. No need to free. */
1691 *tok = NULL;
1692 return EVENT_ERROR;
1693 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001694
1695 *tok = NULL;
1696 type = process_arg(event, arg, &token);
1697 if (test_type_token(type, token, EVENT_OP, "]"))
1698 goto out_free;
1699
1700 top->op.right = arg;
1701
1702 free_token(token);
1703 type = read_token_item(&token);
1704 *tok = token;
1705
1706 return type;
1707
1708out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001709 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001710 free_arg(arg);
1711 return EVENT_ERROR;
1712}
1713
1714static int get_op_prio(char *op)
1715{
1716 if (!op[1]) {
1717 switch (op[0]) {
1718 case '~':
1719 case '!':
1720 return 4;
1721 case '*':
1722 case '/':
1723 case '%':
1724 return 6;
1725 case '+':
1726 case '-':
1727 return 7;
1728 /* '>>' and '<<' are 8 */
1729 case '<':
1730 case '>':
1731 return 9;
1732 /* '==' and '!=' are 10 */
1733 case '&':
1734 return 11;
1735 case '^':
1736 return 12;
1737 case '|':
1738 return 13;
1739 case '?':
1740 return 16;
1741 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001742 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001743 return -1;
1744 }
1745 } else {
1746 if (strcmp(op, "++") == 0 ||
1747 strcmp(op, "--") == 0) {
1748 return 3;
1749 } else if (strcmp(op, ">>") == 0 ||
1750 strcmp(op, "<<") == 0) {
1751 return 8;
1752 } else if (strcmp(op, ">=") == 0 ||
1753 strcmp(op, "<=") == 0) {
1754 return 9;
1755 } else if (strcmp(op, "==") == 0 ||
1756 strcmp(op, "!=") == 0) {
1757 return 10;
1758 } else if (strcmp(op, "&&") == 0) {
1759 return 14;
1760 } else if (strcmp(op, "||") == 0) {
1761 return 15;
1762 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001763 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001764 return -1;
1765 }
1766 }
1767}
1768
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001769static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001770{
1771
1772 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001773 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001774 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001775 else
1776 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001777
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001778 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001779}
1780
1781/* Note, *tok does not get freed, but will most likely be saved */
1782static enum event_type
1783process_op(struct event_format *event, struct print_arg *arg, char **tok)
1784{
1785 struct print_arg *left, *right = NULL;
1786 enum event_type type;
1787 char *token;
1788
1789 /* the op is passed in via tok */
1790 token = *tok;
1791
1792 if (arg->type == PRINT_OP && !arg->op.left) {
1793 /* handle single op */
1794 if (token[1]) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001795 do_warning("bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001796 goto out_free;
1797 }
1798 switch (token[0]) {
1799 case '~':
1800 case '!':
1801 case '+':
1802 case '-':
1803 break;
1804 default:
1805 do_warning("bad op token %s", token);
1806 goto out_free;
1807
1808 }
1809
1810 /* make an empty left */
1811 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001812 if (!left)
1813 goto out_warn_free;
1814
Steven Rostedtf7d82352012-04-06 00:47:53 +02001815 left->type = PRINT_NULL;
1816 arg->op.left = left;
1817
1818 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001819 if (!right)
1820 goto out_warn_free;
1821
Steven Rostedtf7d82352012-04-06 00:47:53 +02001822 arg->op.right = right;
1823
1824 /* do not free the token, it belongs to an op */
1825 *tok = NULL;
1826 type = process_arg(event, right, tok);
1827
1828 } else if (strcmp(token, "?") == 0) {
1829
1830 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001831 if (!left)
1832 goto out_warn_free;
1833
Steven Rostedtf7d82352012-04-06 00:47:53 +02001834 /* copy the top arg to the left */
1835 *left = *arg;
1836
1837 arg->type = PRINT_OP;
1838 arg->op.op = token;
1839 arg->op.left = left;
1840 arg->op.prio = 0;
1841
Namhyung Kim41e51a22012-09-19 15:58:42 +09001842 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001843 type = process_cond(event, arg, tok);
1844
1845 } else if (strcmp(token, ">>") == 0 ||
1846 strcmp(token, "<<") == 0 ||
1847 strcmp(token, "&") == 0 ||
1848 strcmp(token, "|") == 0 ||
1849 strcmp(token, "&&") == 0 ||
1850 strcmp(token, "||") == 0 ||
1851 strcmp(token, "-") == 0 ||
1852 strcmp(token, "+") == 0 ||
1853 strcmp(token, "*") == 0 ||
1854 strcmp(token, "^") == 0 ||
1855 strcmp(token, "/") == 0 ||
1856 strcmp(token, "<") == 0 ||
1857 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001858 strcmp(token, "<=") == 0 ||
1859 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001860 strcmp(token, "==") == 0 ||
1861 strcmp(token, "!=") == 0) {
1862
1863 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001864 if (!left)
1865 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001866
1867 /* copy the top arg to the left */
1868 *left = *arg;
1869
1870 arg->type = PRINT_OP;
1871 arg->op.op = token;
1872 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001873 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001874
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001875 if (set_op_prio(arg) == -1) {
1876 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001877 /* arg->op.op (= token) will be freed at out_free */
1878 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001879 goto out_free;
1880 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001881
1882 type = read_token_item(&token);
1883 *tok = token;
1884
1885 /* could just be a type pointer */
1886 if ((strcmp(arg->op.op, "*") == 0) &&
1887 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001888 char *new_atom;
1889
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001890 if (left->type != PRINT_ATOM) {
1891 do_warning("bad pointer type");
1892 goto out_free;
1893 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001894 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001895 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001896 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001897 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001898
1899 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001900 strcat(left->atom.atom, " *");
1901 free(arg->op.op);
1902 *arg = *left;
1903 free(left);
1904
1905 return type;
1906 }
1907
1908 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001909 if (!right)
1910 goto out_warn_free;
1911
Steven Rostedtf7d82352012-04-06 00:47:53 +02001912 type = process_arg_token(event, right, tok, type);
1913 arg->op.right = right;
1914
1915 } else if (strcmp(token, "[") == 0) {
1916
1917 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001918 if (!left)
1919 goto out_warn_free;
1920
Steven Rostedtf7d82352012-04-06 00:47:53 +02001921 *left = *arg;
1922
1923 arg->type = PRINT_OP;
1924 arg->op.op = token;
1925 arg->op.left = left;
1926
1927 arg->op.prio = 0;
1928
Namhyung Kim41e51a22012-09-19 15:58:42 +09001929 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001930 type = process_array(event, arg, tok);
1931
1932 } else {
1933 do_warning("unknown op '%s'", token);
1934 event->flags |= EVENT_FL_FAILED;
1935 /* the arg is now the left side */
1936 goto out_free;
1937 }
1938
1939 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1940 int prio;
1941
1942 /* higher prios need to be closer to the root */
1943 prio = get_op_prio(*tok);
1944
1945 if (prio > arg->op.prio)
1946 return process_op(event, arg, tok);
1947
1948 return process_op(event, right, tok);
1949 }
1950
1951 return type;
1952
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001953out_warn_free:
1954 do_warning("%s: not enough memory!", __func__);
1955out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001956 free_token(token);
1957 *tok = NULL;
1958 return EVENT_ERROR;
1959}
1960
1961static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001962process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001963 char **tok)
1964{
1965 enum event_type type;
1966 char *field;
1967 char *token;
1968
1969 if (read_expected(EVENT_OP, "->") < 0)
1970 goto out_err;
1971
1972 if (read_expect_type(EVENT_ITEM, &token) < 0)
1973 goto out_free;
1974 field = token;
1975
1976 arg->type = PRINT_FIELD;
1977 arg->field.name = field;
1978
Tom Zanussi5205aec2012-04-06 00:47:58 +02001979 if (is_flag_field) {
1980 arg->field.field = pevent_find_any_field(event, arg->field.name);
1981 arg->field.field->flags |= FIELD_IS_FLAG;
1982 is_flag_field = 0;
1983 } else if (is_symbolic_field) {
1984 arg->field.field = pevent_find_any_field(event, arg->field.name);
1985 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1986 is_symbolic_field = 0;
1987 }
1988
Steven Rostedtf7d82352012-04-06 00:47:53 +02001989 type = read_token(&token);
1990 *tok = token;
1991
1992 return type;
1993
1994 out_free:
1995 free_token(token);
1996 out_err:
1997 *tok = NULL;
1998 return EVENT_ERROR;
1999}
2000
2001static char *arg_eval (struct print_arg *arg);
2002
2003static unsigned long long
2004eval_type_str(unsigned long long val, const char *type, int pointer)
2005{
2006 int sign = 0;
2007 char *ref;
2008 int len;
2009
2010 len = strlen(type);
2011
2012 if (pointer) {
2013
2014 if (type[len-1] != '*') {
2015 do_warning("pointer expected with non pointer type");
2016 return val;
2017 }
2018
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002019 ref = malloc(len);
2020 if (!ref) {
2021 do_warning("%s: not enough memory!", __func__);
2022 return val;
2023 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002024 memcpy(ref, type, len);
2025
2026 /* chop off the " *" */
2027 ref[len - 2] = 0;
2028
2029 val = eval_type_str(val, ref, 0);
2030 free(ref);
2031 return val;
2032 }
2033
2034 /* check if this is a pointer */
2035 if (type[len - 1] == '*')
2036 return val;
2037
2038 /* Try to figure out the arg size*/
2039 if (strncmp(type, "struct", 6) == 0)
2040 /* all bets off */
2041 return val;
2042
2043 if (strcmp(type, "u8") == 0)
2044 return val & 0xff;
2045
2046 if (strcmp(type, "u16") == 0)
2047 return val & 0xffff;
2048
2049 if (strcmp(type, "u32") == 0)
2050 return val & 0xffffffff;
2051
2052 if (strcmp(type, "u64") == 0 ||
2053 strcmp(type, "s64"))
2054 return val;
2055
2056 if (strcmp(type, "s8") == 0)
2057 return (unsigned long long)(char)val & 0xff;
2058
2059 if (strcmp(type, "s16") == 0)
2060 return (unsigned long long)(short)val & 0xffff;
2061
2062 if (strcmp(type, "s32") == 0)
2063 return (unsigned long long)(int)val & 0xffffffff;
2064
2065 if (strncmp(type, "unsigned ", 9) == 0) {
2066 sign = 0;
2067 type += 9;
2068 }
2069
2070 if (strcmp(type, "char") == 0) {
2071 if (sign)
2072 return (unsigned long long)(char)val & 0xff;
2073 else
2074 return val & 0xff;
2075 }
2076
2077 if (strcmp(type, "short") == 0) {
2078 if (sign)
2079 return (unsigned long long)(short)val & 0xffff;
2080 else
2081 return val & 0xffff;
2082 }
2083
2084 if (strcmp(type, "int") == 0) {
2085 if (sign)
2086 return (unsigned long long)(int)val & 0xffffffff;
2087 else
2088 return val & 0xffffffff;
2089 }
2090
2091 return val;
2092}
2093
2094/*
2095 * Try to figure out the type.
2096 */
2097static unsigned long long
2098eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2099{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002100 if (arg->type != PRINT_TYPE) {
2101 do_warning("expected type argument");
2102 return 0;
2103 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002104
2105 return eval_type_str(val, arg->typecast.type, pointer);
2106}
2107
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002108static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002109{
2110 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002111 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002112
2113 switch (arg->type) {
2114 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002115 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002116 break;
2117 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002118 ret = arg_num_eval(arg->typecast.item, val);
2119 if (!ret)
2120 break;
2121 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002122 break;
2123 case PRINT_OP:
2124 switch (arg->op.op[0]) {
2125 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002126 ret = arg_num_eval(arg->op.left, &left);
2127 if (!ret)
2128 break;
2129 ret = arg_num_eval(arg->op.right, &right);
2130 if (!ret)
2131 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002132 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002133 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002134 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002135 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002136 break;
2137 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002138 ret = arg_num_eval(arg->op.left, &left);
2139 if (!ret)
2140 break;
2141 ret = arg_num_eval(arg->op.right, &right);
2142 if (!ret)
2143 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002144 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002145 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002146 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002147 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002148 break;
2149 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002150 ret = arg_num_eval(arg->op.left, &left);
2151 if (!ret)
2152 break;
2153 ret = arg_num_eval(arg->op.right, &right);
2154 if (!ret)
2155 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002156 switch (arg->op.op[1]) {
2157 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002158 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002159 break;
2160 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002161 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002162 break;
2163 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002164 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002165 break;
2166 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002167 do_warning("unknown op '%s'", arg->op.op);
2168 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002169 }
2170 break;
2171 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002172 ret = arg_num_eval(arg->op.left, &left);
2173 if (!ret)
2174 break;
2175 ret = arg_num_eval(arg->op.right, &right);
2176 if (!ret)
2177 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002178 switch (arg->op.op[1]) {
2179 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002180 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002181 break;
2182 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002183 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002184 break;
2185 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002186 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002187 break;
2188 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002189 do_warning("unknown op '%s'", arg->op.op);
2190 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002191 }
2192 break;
2193 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002194 ret = arg_num_eval(arg->op.left, &left);
2195 if (!ret)
2196 break;
2197 ret = arg_num_eval(arg->op.right, &right);
2198 if (!ret)
2199 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002200
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002201 if (arg->op.op[1] != '=') {
2202 do_warning("unknown op '%s'", arg->op.op);
2203 ret = 0;
2204 } else
2205 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002206 break;
2207 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002208 ret = arg_num_eval(arg->op.left, &left);
2209 if (!ret)
2210 break;
2211 ret = arg_num_eval(arg->op.right, &right);
2212 if (!ret)
2213 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002214
2215 switch (arg->op.op[1]) {
2216 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002217 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002218 break;
2219 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002220 do_warning("unknown op '%s'", arg->op.op);
2221 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002222 }
2223 break;
2224 case '-':
2225 /* check for negative */
2226 if (arg->op.left->type == PRINT_NULL)
2227 left = 0;
2228 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002229 ret = arg_num_eval(arg->op.left, &left);
2230 if (!ret)
2231 break;
2232 ret = arg_num_eval(arg->op.right, &right);
2233 if (!ret)
2234 break;
2235 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002236 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002237 case '+':
2238 if (arg->op.left->type == PRINT_NULL)
2239 left = 0;
2240 else
2241 ret = arg_num_eval(arg->op.left, &left);
2242 if (!ret)
2243 break;
2244 ret = arg_num_eval(arg->op.right, &right);
2245 if (!ret)
2246 break;
2247 *val = left + right;
2248 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002249 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002250 do_warning("unknown op '%s'", arg->op.op);
2251 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002252 }
2253 break;
2254
2255 case PRINT_NULL:
2256 case PRINT_FIELD ... PRINT_SYMBOL:
2257 case PRINT_STRING:
2258 case PRINT_BSTRING:
2259 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002260 do_warning("invalid eval type %d", arg->type);
2261 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002262
2263 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002264 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002265}
2266
2267static char *arg_eval (struct print_arg *arg)
2268{
2269 long long val;
2270 static char buf[20];
2271
2272 switch (arg->type) {
2273 case PRINT_ATOM:
2274 return arg->atom.atom;
2275 case PRINT_TYPE:
2276 return arg_eval(arg->typecast.item);
2277 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002278 if (!arg_num_eval(arg, &val))
2279 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002280 sprintf(buf, "%lld", val);
2281 return buf;
2282
2283 case PRINT_NULL:
2284 case PRINT_FIELD ... PRINT_SYMBOL:
2285 case PRINT_STRING:
2286 case PRINT_BSTRING:
2287 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002288 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002289 break;
2290 }
2291
2292 return NULL;
2293}
2294
2295static enum event_type
2296process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2297{
2298 enum event_type type;
2299 struct print_arg *arg = NULL;
2300 struct print_flag_sym *field;
2301 char *token = *tok;
2302 char *value;
2303
2304 do {
2305 free_token(token);
2306 type = read_token_item(&token);
2307 if (test_type_token(type, token, EVENT_OP, "{"))
2308 break;
2309
2310 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002311 if (!arg)
2312 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002313
2314 free_token(token);
2315 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002316
2317 if (type == EVENT_OP)
2318 type = process_op(event, arg, &token);
2319
2320 if (type == EVENT_ERROR)
2321 goto out_free;
2322
Steven Rostedtf7d82352012-04-06 00:47:53 +02002323 if (test_type_token(type, token, EVENT_DELIM, ","))
2324 goto out_free;
2325
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002326 field = calloc(1, sizeof(*field));
2327 if (!field)
2328 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002329
2330 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002331 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002332 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002333 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002334 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002335 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002336
2337 free_arg(arg);
2338 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002339 if (!arg)
2340 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002341
2342 free_token(token);
2343 type = process_arg(event, arg, &token);
2344 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002345 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002346
2347 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002348 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002349 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002350 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002351 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002352 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002353 free_arg(arg);
2354 arg = NULL;
2355
2356 *list = field;
2357 list = &field->next;
2358
2359 free_token(token);
2360 type = read_token_item(&token);
2361 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2362
2363 *tok = token;
2364 return type;
2365
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002366out_free_field:
2367 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002368out_free:
2369 free_arg(arg);
2370 free_token(token);
2371 *tok = NULL;
2372
2373 return EVENT_ERROR;
2374}
2375
2376static enum event_type
2377process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2378{
2379 struct print_arg *field;
2380 enum event_type type;
2381 char *token;
2382
2383 memset(arg, 0, sizeof(*arg));
2384 arg->type = PRINT_FLAGS;
2385
2386 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002387 if (!field) {
2388 do_warning("%s: not enough memory!", __func__);
2389 goto out_free;
2390 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002391
Steven Rostedteff2c922013-11-18 14:23:14 -05002392 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002393
2394 /* Handle operations in the first argument */
2395 while (type == EVENT_OP)
2396 type = process_op(event, field, &token);
2397
2398 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002399 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002400 free_token(token);
2401
2402 arg->flags.field = field;
2403
2404 type = read_token_item(&token);
2405 if (event_item_type(type)) {
2406 arg->flags.delim = token;
2407 type = read_token_item(&token);
2408 }
2409
2410 if (test_type_token(type, token, EVENT_DELIM, ","))
2411 goto out_free;
2412
2413 type = process_fields(event, &arg->flags.flags, &token);
2414 if (test_type_token(type, token, EVENT_DELIM, ")"))
2415 goto out_free;
2416
2417 free_token(token);
2418 type = read_token_item(tok);
2419 return type;
2420
Namhyung Kim70d93042012-09-19 15:58:44 +09002421out_free_field:
2422 free_arg(field);
2423out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002424 free_token(token);
2425 *tok = NULL;
2426 return EVENT_ERROR;
2427}
2428
2429static enum event_type
2430process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2431{
2432 struct print_arg *field;
2433 enum event_type type;
2434 char *token;
2435
2436 memset(arg, 0, sizeof(*arg));
2437 arg->type = PRINT_SYMBOL;
2438
2439 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002440 if (!field) {
2441 do_warning("%s: not enough memory!", __func__);
2442 goto out_free;
2443 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002444
Steven Rostedteff2c922013-11-18 14:23:14 -05002445 type = process_field_arg(event, field, &token);
2446
Steven Rostedtf7d82352012-04-06 00:47:53 +02002447 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002448 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002449
2450 arg->symbol.field = field;
2451
2452 type = process_fields(event, &arg->symbol.symbols, &token);
2453 if (test_type_token(type, token, EVENT_DELIM, ")"))
2454 goto out_free;
2455
2456 free_token(token);
2457 type = read_token_item(tok);
2458 return type;
2459
Namhyung Kim70d93042012-09-19 15:58:44 +09002460out_free_field:
2461 free_arg(field);
2462out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002463 free_token(token);
2464 *tok = NULL;
2465 return EVENT_ERROR;
2466}
2467
2468static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002469process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2470{
2471 struct print_arg *field;
2472 enum event_type type;
2473 char *token;
2474
2475 memset(arg, 0, sizeof(*arg));
2476 arg->type = PRINT_HEX;
2477
2478 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002479 if (!field) {
2480 do_warning("%s: not enough memory!", __func__);
2481 goto out_free;
2482 }
2483
Namhyung Kime080e6f2012-06-27 09:41:41 +09002484 type = process_arg(event, field, &token);
2485
2486 if (test_type_token(type, token, EVENT_DELIM, ","))
2487 goto out_free;
2488
2489 arg->hex.field = field;
2490
2491 free_token(token);
2492
2493 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002494 if (!field) {
2495 do_warning("%s: not enough memory!", __func__);
2496 *tok = NULL;
2497 return EVENT_ERROR;
2498 }
2499
Namhyung Kime080e6f2012-06-27 09:41:41 +09002500 type = process_arg(event, field, &token);
2501
2502 if (test_type_token(type, token, EVENT_DELIM, ")"))
2503 goto out_free;
2504
2505 arg->hex.size = field;
2506
2507 free_token(token);
2508 type = read_token_item(tok);
2509 return type;
2510
2511 out_free:
2512 free_arg(field);
2513 free_token(token);
2514 *tok = NULL;
2515 return EVENT_ERROR;
2516}
2517
2518static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002519process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2520{
2521 struct format_field *field;
2522 enum event_type type;
2523 char *token;
2524
2525 memset(arg, 0, sizeof(*arg));
2526 arg->type = PRINT_DYNAMIC_ARRAY;
2527
2528 /*
2529 * The item within the parenthesis is another field that holds
2530 * the index into where the array starts.
2531 */
2532 type = read_token(&token);
2533 *tok = token;
2534 if (type != EVENT_ITEM)
2535 goto out_free;
2536
2537 /* Find the field */
2538
2539 field = pevent_find_field(event, token);
2540 if (!field)
2541 goto out_free;
2542
2543 arg->dynarray.field = field;
2544 arg->dynarray.index = 0;
2545
2546 if (read_expected(EVENT_DELIM, ")") < 0)
2547 goto out_free;
2548
2549 free_token(token);
2550 type = read_token_item(&token);
2551 *tok = token;
2552 if (type != EVENT_OP || strcmp(token, "[") != 0)
2553 return type;
2554
2555 free_token(token);
2556 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002557 if (!arg) {
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002558 do_warning("%s: not enough memory!", __func__);
2559 *tok = NULL;
2560 return EVENT_ERROR;
2561 }
2562
Steven Rostedtf7d82352012-04-06 00:47:53 +02002563 type = process_arg(event, arg, &token);
2564 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002565 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002566
2567 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002568 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002569
2570 free_token(token);
2571 type = read_token_item(tok);
2572 return type;
2573
Namhyung Kimb3511d02012-05-23 11:36:50 +09002574 out_free_arg:
2575 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002576 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002577 free_token(token);
2578 *tok = NULL;
2579 return EVENT_ERROR;
2580}
2581
2582static enum event_type
2583process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2584{
2585 struct print_arg *item_arg;
2586 enum event_type type;
2587 char *token;
2588
2589 type = process_arg(event, arg, &token);
2590
2591 if (type == EVENT_ERROR)
2592 goto out_free;
2593
2594 if (type == EVENT_OP)
2595 type = process_op(event, arg, &token);
2596
2597 if (type == EVENT_ERROR)
2598 goto out_free;
2599
2600 if (test_type_token(type, token, EVENT_DELIM, ")"))
2601 goto out_free;
2602
2603 free_token(token);
2604 type = read_token_item(&token);
2605
2606 /*
2607 * If the next token is an item or another open paren, then
2608 * this was a typecast.
2609 */
2610 if (event_item_type(type) ||
2611 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2612
2613 /* make this a typecast and contine */
2614
2615 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002616 if (arg->type != PRINT_ATOM) {
2617 do_warning("previous needed to be PRINT_ATOM");
2618 goto out_free;
2619 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002620
2621 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002622 if (!item_arg) {
2623 do_warning("%s: not enough memory!", __func__);
2624 goto out_free;
2625 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002626
2627 arg->type = PRINT_TYPE;
2628 arg->typecast.type = arg->atom.atom;
2629 arg->typecast.item = item_arg;
2630 type = process_arg_token(event, item_arg, &token, type);
2631
2632 }
2633
2634 *tok = token;
2635 return type;
2636
2637 out_free:
2638 free_token(token);
2639 *tok = NULL;
2640 return EVENT_ERROR;
2641}
2642
2643
2644static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002645process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2646 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002647{
2648 enum event_type type;
2649 char *token;
2650
2651 if (read_expect_type(EVENT_ITEM, &token) < 0)
2652 goto out_free;
2653
2654 arg->type = PRINT_STRING;
2655 arg->string.string = token;
2656 arg->string.offset = -1;
2657
2658 if (read_expected(EVENT_DELIM, ")") < 0)
2659 goto out_err;
2660
2661 type = read_token(&token);
2662 *tok = token;
2663
2664 return type;
2665
2666 out_free:
2667 free_token(token);
2668 out_err:
2669 *tok = NULL;
2670 return EVENT_ERROR;
2671}
2672
2673static struct pevent_function_handler *
2674find_func_handler(struct pevent *pevent, char *func_name)
2675{
2676 struct pevent_function_handler *func;
2677
Steven Rostedt101782e2012-10-01 20:13:51 -04002678 if (!pevent)
2679 return NULL;
2680
Steven Rostedtf7d82352012-04-06 00:47:53 +02002681 for (func = pevent->func_handlers; func; func = func->next) {
2682 if (strcmp(func->name, func_name) == 0)
2683 break;
2684 }
2685
2686 return func;
2687}
2688
2689static void remove_func_handler(struct pevent *pevent, char *func_name)
2690{
2691 struct pevent_function_handler *func;
2692 struct pevent_function_handler **next;
2693
2694 next = &pevent->func_handlers;
2695 while ((func = *next)) {
2696 if (strcmp(func->name, func_name) == 0) {
2697 *next = func->next;
2698 free_func_handle(func);
2699 break;
2700 }
2701 next = &func->next;
2702 }
2703}
2704
2705static enum event_type
2706process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2707 struct print_arg *arg, char **tok)
2708{
2709 struct print_arg **next_arg;
2710 struct print_arg *farg;
2711 enum event_type type;
2712 char *token;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03002713 const char *test;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002714 int i;
2715
2716 arg->type = PRINT_FUNC;
2717 arg->func.func = func;
2718
2719 *tok = NULL;
2720
2721 next_arg = &(arg->func.args);
2722 for (i = 0; i < func->nr_args; i++) {
2723 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002724 if (!farg) {
2725 do_warning("%s: not enough memory!", __func__);
2726 return EVENT_ERROR;
2727 }
2728
Steven Rostedtf7d82352012-04-06 00:47:53 +02002729 type = process_arg(event, farg, &token);
2730 if (i < (func->nr_args - 1))
2731 test = ",";
2732 else
2733 test = ")";
2734
2735 if (test_type_token(type, token, EVENT_DELIM, test)) {
2736 free_arg(farg);
2737 free_token(token);
2738 return EVENT_ERROR;
2739 }
2740
2741 *next_arg = farg;
2742 next_arg = &(farg->next);
2743 free_token(token);
2744 }
2745
2746 type = read_token(&token);
2747 *tok = token;
2748
2749 return type;
2750}
2751
2752static enum event_type
2753process_function(struct event_format *event, struct print_arg *arg,
2754 char *token, char **tok)
2755{
2756 struct pevent_function_handler *func;
2757
2758 if (strcmp(token, "__print_flags") == 0) {
2759 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002760 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002761 return process_flags(event, arg, tok);
2762 }
2763 if (strcmp(token, "__print_symbolic") == 0) {
2764 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002765 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002766 return process_symbols(event, arg, tok);
2767 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002768 if (strcmp(token, "__print_hex") == 0) {
2769 free_token(token);
2770 return process_hex(event, arg, tok);
2771 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002772 if (strcmp(token, "__get_str") == 0) {
2773 free_token(token);
2774 return process_str(event, arg, tok);
2775 }
2776 if (strcmp(token, "__get_dynamic_array") == 0) {
2777 free_token(token);
2778 return process_dynamic_array(event, arg, tok);
2779 }
2780
2781 func = find_func_handler(event->pevent, token);
2782 if (func) {
2783 free_token(token);
2784 return process_func_handler(event, func, arg, tok);
2785 }
2786
2787 do_warning("function %s not defined", token);
2788 free_token(token);
2789 return EVENT_ERROR;
2790}
2791
2792static enum event_type
2793process_arg_token(struct event_format *event, struct print_arg *arg,
2794 char **tok, enum event_type type)
2795{
2796 char *token;
2797 char *atom;
2798
2799 token = *tok;
2800
2801 switch (type) {
2802 case EVENT_ITEM:
2803 if (strcmp(token, "REC") == 0) {
2804 free_token(token);
2805 type = process_entry(event, arg, &token);
2806 break;
2807 }
2808 atom = token;
2809 /* test the next token */
2810 type = read_token_item(&token);
2811
2812 /*
2813 * If the next token is a parenthesis, then this
2814 * is a function.
2815 */
2816 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2817 free_token(token);
2818 token = NULL;
2819 /* this will free atom. */
2820 type = process_function(event, arg, atom, &token);
2821 break;
2822 }
2823 /* atoms can be more than one token long */
2824 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002825 char *new_atom;
2826 new_atom = realloc(atom,
2827 strlen(atom) + strlen(token) + 2);
2828 if (!new_atom) {
2829 free(atom);
2830 *tok = NULL;
2831 free_token(token);
2832 return EVENT_ERROR;
2833 }
2834 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002835 strcat(atom, " ");
2836 strcat(atom, token);
2837 free_token(token);
2838 type = read_token_item(&token);
2839 }
2840
2841 arg->type = PRINT_ATOM;
2842 arg->atom.atom = atom;
2843 break;
2844
2845 case EVENT_DQUOTE:
2846 case EVENT_SQUOTE:
2847 arg->type = PRINT_ATOM;
2848 arg->atom.atom = token;
2849 type = read_token_item(&token);
2850 break;
2851 case EVENT_DELIM:
2852 if (strcmp(token, "(") == 0) {
2853 free_token(token);
2854 type = process_paren(event, arg, &token);
2855 break;
2856 }
2857 case EVENT_OP:
2858 /* handle single ops */
2859 arg->type = PRINT_OP;
2860 arg->op.op = token;
2861 arg->op.left = NULL;
2862 type = process_op(event, arg, &token);
2863
2864 /* On error, the op is freed */
2865 if (type == EVENT_ERROR)
2866 arg->op.op = NULL;
2867
2868 /* return error type if errored */
2869 break;
2870
2871 case EVENT_ERROR ... EVENT_NEWLINE:
2872 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002873 do_warning("unexpected type %d", type);
2874 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002875 }
2876 *tok = token;
2877
2878 return type;
2879}
2880
2881static int event_read_print_args(struct event_format *event, struct print_arg **list)
2882{
2883 enum event_type type = EVENT_ERROR;
2884 struct print_arg *arg;
2885 char *token;
2886 int args = 0;
2887
2888 do {
2889 if (type == EVENT_NEWLINE) {
2890 type = read_token_item(&token);
2891 continue;
2892 }
2893
2894 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002895 if (!arg) {
2896 do_warning("%s: not enough memory!", __func__);
2897 return -1;
2898 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002899
2900 type = process_arg(event, arg, &token);
2901
2902 if (type == EVENT_ERROR) {
2903 free_token(token);
2904 free_arg(arg);
2905 return -1;
2906 }
2907
2908 *list = arg;
2909 args++;
2910
2911 if (type == EVENT_OP) {
2912 type = process_op(event, arg, &token);
2913 free_token(token);
2914 if (type == EVENT_ERROR) {
2915 *list = NULL;
2916 free_arg(arg);
2917 return -1;
2918 }
2919 list = &arg->next;
2920 continue;
2921 }
2922
2923 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2924 free_token(token);
2925 *list = arg;
2926 list = &arg->next;
2927 continue;
2928 }
2929 break;
2930 } while (type != EVENT_NONE);
2931
2932 if (type != EVENT_NONE && type != EVENT_ERROR)
2933 free_token(token);
2934
2935 return args;
2936}
2937
2938static int event_read_print(struct event_format *event)
2939{
2940 enum event_type type;
2941 char *token;
2942 int ret;
2943
2944 if (read_expected_item(EVENT_ITEM, "print") < 0)
2945 return -1;
2946
2947 if (read_expected(EVENT_ITEM, "fmt") < 0)
2948 return -1;
2949
2950 if (read_expected(EVENT_OP, ":") < 0)
2951 return -1;
2952
2953 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2954 goto fail;
2955
2956 concat:
2957 event->print_fmt.format = token;
2958 event->print_fmt.args = NULL;
2959
2960 /* ok to have no arg */
2961 type = read_token_item(&token);
2962
2963 if (type == EVENT_NONE)
2964 return 0;
2965
2966 /* Handle concatenation of print lines */
2967 if (type == EVENT_DQUOTE) {
2968 char *cat;
2969
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03002970 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2971 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002972 free_token(token);
2973 free_token(event->print_fmt.format);
2974 event->print_fmt.format = NULL;
2975 token = cat;
2976 goto concat;
2977 }
2978
2979 if (test_type_token(type, token, EVENT_DELIM, ","))
2980 goto fail;
2981
2982 free_token(token);
2983
2984 ret = event_read_print_args(event, &event->print_fmt.args);
2985 if (ret < 0)
2986 return -1;
2987
2988 return ret;
2989
2990 fail:
2991 free_token(token);
2992 return -1;
2993}
2994
2995/**
2996 * pevent_find_common_field - return a common field by event
2997 * @event: handle for the event
2998 * @name: the name of the common field to return
2999 *
3000 * Returns a common field from the event by the given @name.
3001 * This only searchs the common fields and not all field.
3002 */
3003struct format_field *
3004pevent_find_common_field(struct event_format *event, const char *name)
3005{
3006 struct format_field *format;
3007
3008 for (format = event->format.common_fields;
3009 format; format = format->next) {
3010 if (strcmp(format->name, name) == 0)
3011 break;
3012 }
3013
3014 return format;
3015}
3016
3017/**
3018 * pevent_find_field - find a non-common field
3019 * @event: handle for the event
3020 * @name: the name of the non-common field
3021 *
3022 * Returns a non-common field by the given @name.
3023 * This does not search common fields.
3024 */
3025struct format_field *
3026pevent_find_field(struct event_format *event, const char *name)
3027{
3028 struct format_field *format;
3029
3030 for (format = event->format.fields;
3031 format; format = format->next) {
3032 if (strcmp(format->name, name) == 0)
3033 break;
3034 }
3035
3036 return format;
3037}
3038
3039/**
3040 * pevent_find_any_field - find any field by name
3041 * @event: handle for the event
3042 * @name: the name of the field
3043 *
3044 * Returns a field by the given @name.
3045 * This searchs the common field names first, then
3046 * the non-common ones if a common one was not found.
3047 */
3048struct format_field *
3049pevent_find_any_field(struct event_format *event, const char *name)
3050{
3051 struct format_field *format;
3052
3053 format = pevent_find_common_field(event, name);
3054 if (format)
3055 return format;
3056 return pevent_find_field(event, name);
3057}
3058
3059/**
3060 * pevent_read_number - read a number from data
3061 * @pevent: handle for the pevent
3062 * @ptr: the raw data
3063 * @size: the size of the data that holds the number
3064 *
3065 * Returns the number (converted to host) from the
3066 * raw data.
3067 */
3068unsigned long long pevent_read_number(struct pevent *pevent,
3069 const void *ptr, int size)
3070{
3071 switch (size) {
3072 case 1:
3073 return *(unsigned char *)ptr;
3074 case 2:
3075 return data2host2(pevent, ptr);
3076 case 4:
3077 return data2host4(pevent, ptr);
3078 case 8:
3079 return data2host8(pevent, ptr);
3080 default:
3081 /* BUG! */
3082 return 0;
3083 }
3084}
3085
3086/**
3087 * pevent_read_number_field - read a number from data
3088 * @field: a handle to the field
3089 * @data: the raw data to read
3090 * @value: the value to place the number in
3091 *
3092 * Reads raw data according to a field offset and size,
3093 * and translates it into @value.
3094 *
3095 * Returns 0 on success, -1 otherwise.
3096 */
3097int pevent_read_number_field(struct format_field *field, const void *data,
3098 unsigned long long *value)
3099{
3100 if (!field)
3101 return -1;
3102 switch (field->size) {
3103 case 1:
3104 case 2:
3105 case 4:
3106 case 8:
3107 *value = pevent_read_number(field->event->pevent,
3108 data + field->offset, field->size);
3109 return 0;
3110 default:
3111 return -1;
3112 }
3113}
3114
3115static int get_common_info(struct pevent *pevent,
3116 const char *type, int *offset, int *size)
3117{
3118 struct event_format *event;
3119 struct format_field *field;
3120
3121 /*
3122 * All events should have the same common elements.
3123 * Pick any event to find where the type is;
3124 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003125 if (!pevent->events) {
3126 do_warning("no event_list!");
3127 return -1;
3128 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003129
3130 event = pevent->events[0];
3131 field = pevent_find_common_field(event, type);
3132 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003133 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003134
3135 *offset = field->offset;
3136 *size = field->size;
3137
3138 return 0;
3139}
3140
3141static int __parse_common(struct pevent *pevent, void *data,
3142 int *size, int *offset, const char *name)
3143{
3144 int ret;
3145
3146 if (!*size) {
3147 ret = get_common_info(pevent, name, offset, size);
3148 if (ret < 0)
3149 return ret;
3150 }
3151 return pevent_read_number(pevent, data + *offset, *size);
3152}
3153
3154static int trace_parse_common_type(struct pevent *pevent, void *data)
3155{
3156 return __parse_common(pevent, data,
3157 &pevent->type_size, &pevent->type_offset,
3158 "common_type");
3159}
3160
3161static int parse_common_pid(struct pevent *pevent, void *data)
3162{
3163 return __parse_common(pevent, data,
3164 &pevent->pid_size, &pevent->pid_offset,
3165 "common_pid");
3166}
3167
3168static int parse_common_pc(struct pevent *pevent, void *data)
3169{
3170 return __parse_common(pevent, data,
3171 &pevent->pc_size, &pevent->pc_offset,
3172 "common_preempt_count");
3173}
3174
3175static int parse_common_flags(struct pevent *pevent, void *data)
3176{
3177 return __parse_common(pevent, data,
3178 &pevent->flags_size, &pevent->flags_offset,
3179 "common_flags");
3180}
3181
3182static int parse_common_lock_depth(struct pevent *pevent, void *data)
3183{
Steven Rostedt0866a972012-05-22 14:52:40 +09003184 return __parse_common(pevent, data,
3185 &pevent->ld_size, &pevent->ld_offset,
3186 "common_lock_depth");
3187}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003188
Steven Rostedt0866a972012-05-22 14:52:40 +09003189static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3190{
3191 return __parse_common(pevent, data,
3192 &pevent->ld_size, &pevent->ld_offset,
3193 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003194}
3195
3196static int events_id_cmp(const void *a, const void *b);
3197
3198/**
3199 * pevent_find_event - find an event by given id
3200 * @pevent: a handle to the pevent
3201 * @id: the id of the event
3202 *
3203 * Returns an event that has a given @id.
3204 */
3205struct event_format *pevent_find_event(struct pevent *pevent, int id)
3206{
3207 struct event_format **eventptr;
3208 struct event_format key;
3209 struct event_format *pkey = &key;
3210
3211 /* Check cache first */
3212 if (pevent->last_event && pevent->last_event->id == id)
3213 return pevent->last_event;
3214
3215 key.id = id;
3216
3217 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3218 sizeof(*pevent->events), events_id_cmp);
3219
3220 if (eventptr) {
3221 pevent->last_event = *eventptr;
3222 return *eventptr;
3223 }
3224
3225 return NULL;
3226}
3227
3228/**
3229 * pevent_find_event_by_name - find an event by given name
3230 * @pevent: a handle to the pevent
3231 * @sys: the system name to search for
3232 * @name: the name of the event to search for
3233 *
3234 * This returns an event with a given @name and under the system
3235 * @sys. If @sys is NULL the first event with @name is returned.
3236 */
3237struct event_format *
3238pevent_find_event_by_name(struct pevent *pevent,
3239 const char *sys, const char *name)
3240{
3241 struct event_format *event;
3242 int i;
3243
3244 if (pevent->last_event &&
3245 strcmp(pevent->last_event->name, name) == 0 &&
3246 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3247 return pevent->last_event;
3248
3249 for (i = 0; i < pevent->nr_events; i++) {
3250 event = pevent->events[i];
3251 if (strcmp(event->name, name) == 0) {
3252 if (!sys)
3253 break;
3254 if (strcmp(event->system, sys) == 0)
3255 break;
3256 }
3257 }
3258 if (i == pevent->nr_events)
3259 event = NULL;
3260
3261 pevent->last_event = event;
3262 return event;
3263}
3264
3265static unsigned long long
3266eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3267{
3268 struct pevent *pevent = event->pevent;
3269 unsigned long long val = 0;
3270 unsigned long long left, right;
3271 struct print_arg *typearg = NULL;
3272 struct print_arg *larg;
3273 unsigned long offset;
3274 unsigned int field_size;
3275
3276 switch (arg->type) {
3277 case PRINT_NULL:
3278 /* ?? */
3279 return 0;
3280 case PRINT_ATOM:
3281 return strtoull(arg->atom.atom, NULL, 0);
3282 case PRINT_FIELD:
3283 if (!arg->field.field) {
3284 arg->field.field = pevent_find_any_field(event, arg->field.name);
3285 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003286 goto out_warning_field;
3287
Steven Rostedtf7d82352012-04-06 00:47:53 +02003288 }
3289 /* must be a number */
3290 val = pevent_read_number(pevent, data + arg->field.field->offset,
3291 arg->field.field->size);
3292 break;
3293 case PRINT_FLAGS:
3294 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003295 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003296 break;
3297 case PRINT_TYPE:
3298 val = eval_num_arg(data, size, event, arg->typecast.item);
3299 return eval_type(val, arg, 0);
3300 case PRINT_STRING:
3301 case PRINT_BSTRING:
3302 return 0;
3303 case PRINT_FUNC: {
3304 struct trace_seq s;
3305 trace_seq_init(&s);
3306 val = process_defined_func(&s, data, size, event, arg);
3307 trace_seq_destroy(&s);
3308 return val;
3309 }
3310 case PRINT_OP:
3311 if (strcmp(arg->op.op, "[") == 0) {
3312 /*
3313 * Arrays are special, since we don't want
3314 * to read the arg as is.
3315 */
3316 right = eval_num_arg(data, size, event, arg->op.right);
3317
3318 /* handle typecasts */
3319 larg = arg->op.left;
3320 while (larg->type == PRINT_TYPE) {
3321 if (!typearg)
3322 typearg = larg;
3323 larg = larg->typecast.item;
3324 }
3325
3326 /* Default to long size */
3327 field_size = pevent->long_size;
3328
3329 switch (larg->type) {
3330 case PRINT_DYNAMIC_ARRAY:
3331 offset = pevent_read_number(pevent,
3332 data + larg->dynarray.field->offset,
3333 larg->dynarray.field->size);
3334 if (larg->dynarray.field->elementsize)
3335 field_size = larg->dynarray.field->elementsize;
3336 /*
3337 * The actual length of the dynamic array is stored
3338 * in the top half of the field, and the offset
3339 * is in the bottom half of the 32 bit field.
3340 */
3341 offset &= 0xffff;
3342 offset += right;
3343 break;
3344 case PRINT_FIELD:
3345 if (!larg->field.field) {
3346 larg->field.field =
3347 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003348 if (!larg->field.field) {
3349 arg = larg;
3350 goto out_warning_field;
3351 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003352 }
3353 field_size = larg->field.field->elementsize;
3354 offset = larg->field.field->offset +
3355 right * larg->field.field->elementsize;
3356 break;
3357 default:
3358 goto default_op; /* oops, all bets off */
3359 }
3360 val = pevent_read_number(pevent,
3361 data + offset, field_size);
3362 if (typearg)
3363 val = eval_type(val, typearg, 1);
3364 break;
3365 } else if (strcmp(arg->op.op, "?") == 0) {
3366 left = eval_num_arg(data, size, event, arg->op.left);
3367 arg = arg->op.right;
3368 if (left)
3369 val = eval_num_arg(data, size, event, arg->op.left);
3370 else
3371 val = eval_num_arg(data, size, event, arg->op.right);
3372 break;
3373 }
3374 default_op:
3375 left = eval_num_arg(data, size, event, arg->op.left);
3376 right = eval_num_arg(data, size, event, arg->op.right);
3377 switch (arg->op.op[0]) {
3378 case '!':
3379 switch (arg->op.op[1]) {
3380 case 0:
3381 val = !right;
3382 break;
3383 case '=':
3384 val = left != right;
3385 break;
3386 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003387 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003388 }
3389 break;
3390 case '~':
3391 val = ~right;
3392 break;
3393 case '|':
3394 if (arg->op.op[1])
3395 val = left || right;
3396 else
3397 val = left | right;
3398 break;
3399 case '&':
3400 if (arg->op.op[1])
3401 val = left && right;
3402 else
3403 val = left & right;
3404 break;
3405 case '<':
3406 switch (arg->op.op[1]) {
3407 case 0:
3408 val = left < right;
3409 break;
3410 case '<':
3411 val = left << right;
3412 break;
3413 case '=':
3414 val = left <= right;
3415 break;
3416 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003417 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003418 }
3419 break;
3420 case '>':
3421 switch (arg->op.op[1]) {
3422 case 0:
3423 val = left > right;
3424 break;
3425 case '>':
3426 val = left >> right;
3427 break;
3428 case '=':
3429 val = left >= right;
3430 break;
3431 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003432 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003433 }
3434 break;
3435 case '=':
3436 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003437 goto out_warning_op;
3438
Steven Rostedtf7d82352012-04-06 00:47:53 +02003439 val = left == right;
3440 break;
3441 case '-':
3442 val = left - right;
3443 break;
3444 case '+':
3445 val = left + right;
3446 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003447 case '/':
3448 val = left / right;
3449 break;
3450 case '*':
3451 val = left * right;
3452 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003453 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003454 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003455 }
3456 break;
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003457 case PRINT_DYNAMIC_ARRAY:
3458 /* Without [], we pass the address to the dynamic data */
3459 offset = pevent_read_number(pevent,
3460 data + arg->dynarray.field->offset,
3461 arg->dynarray.field->size);
3462 /*
3463 * The actual length of the dynamic array is stored
3464 * in the top half of the field, and the offset
3465 * is in the bottom half of the 32 bit field.
3466 */
3467 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003468 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003469 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003470 default: /* not sure what to do there */
3471 return 0;
3472 }
3473 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003474
3475out_warning_op:
3476 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3477 return 0;
3478
3479out_warning_field:
3480 do_warning("%s: field %s not found", __func__, arg->field.name);
3481 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003482}
3483
3484struct flag {
3485 const char *name;
3486 unsigned long long value;
3487};
3488
3489static const struct flag flags[] = {
3490 { "HI_SOFTIRQ", 0 },
3491 { "TIMER_SOFTIRQ", 1 },
3492 { "NET_TX_SOFTIRQ", 2 },
3493 { "NET_RX_SOFTIRQ", 3 },
3494 { "BLOCK_SOFTIRQ", 4 },
3495 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3496 { "TASKLET_SOFTIRQ", 6 },
3497 { "SCHED_SOFTIRQ", 7 },
3498 { "HRTIMER_SOFTIRQ", 8 },
3499 { "RCU_SOFTIRQ", 9 },
3500
3501 { "HRTIMER_NORESTART", 0 },
3502 { "HRTIMER_RESTART", 1 },
3503};
3504
3505static unsigned long long eval_flag(const char *flag)
3506{
3507 int i;
3508
3509 /*
3510 * Some flags in the format files do not get converted.
3511 * If the flag is not numeric, see if it is something that
3512 * we already know about.
3513 */
3514 if (isdigit(flag[0]))
3515 return strtoull(flag, NULL, 0);
3516
3517 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3518 if (strcmp(flags[i].name, flag) == 0)
3519 return flags[i].value;
3520
3521 return 0;
3522}
3523
3524static void print_str_to_seq(struct trace_seq *s, const char *format,
3525 int len_arg, const char *str)
3526{
3527 if (len_arg >= 0)
3528 trace_seq_printf(s, format, len_arg, str);
3529 else
3530 trace_seq_printf(s, format, str);
3531}
3532
3533static void print_str_arg(struct trace_seq *s, void *data, int size,
3534 struct event_format *event, const char *format,
3535 int len_arg, struct print_arg *arg)
3536{
3537 struct pevent *pevent = event->pevent;
3538 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003539 struct format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003540 struct printk_map *printk;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003541 unsigned long long val, fval;
3542 unsigned long addr;
3543 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003544 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003545 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003546 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003547
3548 switch (arg->type) {
3549 case PRINT_NULL:
3550 /* ?? */
3551 return;
3552 case PRINT_ATOM:
3553 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3554 return;
3555 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003556 field = arg->field.field;
3557 if (!field) {
3558 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003559 if (!field) {
3560 str = arg->field.name;
3561 goto out_warning_field;
3562 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003563 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003564 }
3565 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003566 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003567
3568 /*
3569 * Some events pass in pointers. If this is not an array
3570 * and the size is the same as long_size, assume that it
3571 * is a pointer.
3572 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003573 if (!(field->flags & FIELD_IS_ARRAY) &&
3574 field->size == pevent->long_size) {
3575 addr = *(unsigned long *)(data + field->offset);
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003576 /* Check if it matches a print format */
3577 printk = find_printk(pevent, addr);
3578 if (printk)
3579 trace_seq_puts(s, printk->printk);
3580 else
3581 trace_seq_printf(s, "%lx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003582 break;
3583 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003584 str = malloc(len + 1);
3585 if (!str) {
3586 do_warning("%s: not enough memory!", __func__);
3587 return;
3588 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003589 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003590 str[len] = 0;
3591 print_str_to_seq(s, format, len_arg, str);
3592 free(str);
3593 break;
3594 case PRINT_FLAGS:
3595 val = eval_num_arg(data, size, event, arg->flags.field);
3596 print = 0;
3597 for (flag = arg->flags.flags; flag; flag = flag->next) {
3598 fval = eval_flag(flag->value);
3599 if (!val && !fval) {
3600 print_str_to_seq(s, format, len_arg, flag->str);
3601 break;
3602 }
3603 if (fval && (val & fval) == fval) {
3604 if (print && arg->flags.delim)
3605 trace_seq_puts(s, arg->flags.delim);
3606 print_str_to_seq(s, format, len_arg, flag->str);
3607 print = 1;
3608 val &= ~fval;
3609 }
3610 }
3611 break;
3612 case PRINT_SYMBOL:
3613 val = eval_num_arg(data, size, event, arg->symbol.field);
3614 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3615 fval = eval_flag(flag->value);
3616 if (val == fval) {
3617 print_str_to_seq(s, format, len_arg, flag->str);
3618 break;
3619 }
3620 }
3621 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003622 case PRINT_HEX:
Howard Cochranb30f75e2013-11-01 17:53:56 -04003623 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3624 unsigned long offset;
3625 offset = pevent_read_number(pevent,
3626 data + arg->hex.field->dynarray.field->offset,
3627 arg->hex.field->dynarray.field->size);
3628 hex = data + (offset & 0xffff);
3629 } else {
3630 field = arg->hex.field->field.field;
3631 if (!field) {
3632 str = arg->hex.field->field.name;
3633 field = pevent_find_any_field(event, str);
3634 if (!field)
3635 goto out_warning_field;
3636 arg->hex.field->field.field = field;
3637 }
3638 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003639 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003640 len = eval_num_arg(data, size, event, arg->hex.size);
3641 for (i = 0; i < len; i++) {
3642 if (i)
3643 trace_seq_putc(s, ' ');
3644 trace_seq_printf(s, "%02x", hex[i]);
3645 }
3646 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003647
3648 case PRINT_TYPE:
3649 break;
3650 case PRINT_STRING: {
3651 int str_offset;
3652
3653 if (arg->string.offset == -1) {
3654 struct format_field *f;
3655
3656 f = pevent_find_any_field(event, arg->string.string);
3657 arg->string.offset = f->offset;
3658 }
3659 str_offset = data2host4(pevent, data + arg->string.offset);
3660 str_offset &= 0xffff;
3661 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3662 break;
3663 }
3664 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003665 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003666 break;
3667 case PRINT_OP:
3668 /*
3669 * The only op for string should be ? :
3670 */
3671 if (arg->op.op[0] != '?')
3672 return;
3673 val = eval_num_arg(data, size, event, arg->op.left);
3674 if (val)
3675 print_str_arg(s, data, size, event,
3676 format, len_arg, arg->op.right->op.left);
3677 else
3678 print_str_arg(s, data, size, event,
3679 format, len_arg, arg->op.right->op.right);
3680 break;
3681 case PRINT_FUNC:
3682 process_defined_func(s, data, size, event, arg);
3683 break;
3684 default:
3685 /* well... */
3686 break;
3687 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003688
3689 return;
3690
3691out_warning_field:
3692 do_warning("%s: field %s not found", __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003693}
3694
3695static unsigned long long
3696process_defined_func(struct trace_seq *s, void *data, int size,
3697 struct event_format *event, struct print_arg *arg)
3698{
3699 struct pevent_function_handler *func_handle = arg->func.func;
3700 struct pevent_func_params *param;
3701 unsigned long long *args;
3702 unsigned long long ret;
3703 struct print_arg *farg;
3704 struct trace_seq str;
3705 struct save_str {
3706 struct save_str *next;
3707 char *str;
3708 } *strings = NULL, *string;
3709 int i;
3710
3711 if (!func_handle->nr_args) {
3712 ret = (*func_handle->func)(s, NULL);
3713 goto out;
3714 }
3715
3716 farg = arg->func.args;
3717 param = func_handle->params;
3718
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003719 ret = ULLONG_MAX;
3720 args = malloc(sizeof(*args) * func_handle->nr_args);
3721 if (!args)
3722 goto out;
3723
Steven Rostedtf7d82352012-04-06 00:47:53 +02003724 for (i = 0; i < func_handle->nr_args; i++) {
3725 switch (param->type) {
3726 case PEVENT_FUNC_ARG_INT:
3727 case PEVENT_FUNC_ARG_LONG:
3728 case PEVENT_FUNC_ARG_PTR:
3729 args[i] = eval_num_arg(data, size, event, farg);
3730 break;
3731 case PEVENT_FUNC_ARG_STRING:
3732 trace_seq_init(&str);
3733 print_str_arg(&str, data, size, event, "%s", -1, farg);
3734 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003735 string = malloc(sizeof(*string));
3736 if (!string) {
3737 do_warning("%s(%d): malloc str", __func__, __LINE__);
3738 goto out_free;
3739 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003740 string->next = strings;
3741 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003742 if (!string->str) {
3743 free(string);
3744 do_warning("%s(%d): malloc str", __func__, __LINE__);
3745 goto out_free;
3746 }
Robert Richter0cf26012012-08-07 19:43:14 +02003747 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003748 strings = string;
3749 trace_seq_destroy(&str);
3750 break;
3751 default:
3752 /*
3753 * Something went totally wrong, this is not
3754 * an input error, something in this code broke.
3755 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003756 do_warning("Unexpected end of arguments\n");
3757 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003758 }
3759 farg = farg->next;
Namhyung Kim21c69e72012-05-23 11:36:51 +09003760 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003761 }
3762
3763 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003764out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003765 free(args);
3766 while (strings) {
3767 string = strings;
3768 strings = string->next;
3769 free(string->str);
3770 free(string);
3771 }
3772
3773 out:
3774 /* TBD : handle return type here */
3775 return ret;
3776}
3777
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003778static void free_args(struct print_arg *args)
3779{
3780 struct print_arg *next;
3781
3782 while (args) {
3783 next = args->next;
3784
3785 free_arg(args);
3786 args = next;
3787 }
3788}
3789
Steven Rostedtf7d82352012-04-06 00:47:53 +02003790static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3791{
3792 struct pevent *pevent = event->pevent;
3793 struct format_field *field, *ip_field;
3794 struct print_arg *args, *arg, **next;
3795 unsigned long long ip, val;
3796 char *ptr;
3797 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003798 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003799
3800 field = pevent->bprint_buf_field;
3801 ip_field = pevent->bprint_ip_field;
3802
3803 if (!field) {
3804 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003805 if (!field) {
3806 do_warning("can't find buffer field for binary printk");
3807 return NULL;
3808 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003809 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003810 if (!ip_field) {
3811 do_warning("can't find ip field for binary printk");
3812 return NULL;
3813 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003814 pevent->bprint_buf_field = field;
3815 pevent->bprint_ip_field = ip_field;
3816 }
3817
3818 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3819
3820 /*
3821 * The first arg is the IP pointer.
3822 */
3823 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003824 if (!args) {
3825 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3826 return NULL;
3827 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003828 arg = args;
3829 arg->next = NULL;
3830 next = &arg->next;
3831
3832 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003833
3834 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3835 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003836
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003837 /* skip the first "%pf: " */
3838 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003839 bptr < data + size && *ptr; ptr++) {
3840 int ls = 0;
3841
3842 if (*ptr == '%') {
3843 process_again:
3844 ptr++;
3845 switch (*ptr) {
3846 case '%':
3847 break;
3848 case 'l':
3849 ls++;
3850 goto process_again;
3851 case 'L':
3852 ls = 2;
3853 goto process_again;
3854 case '0' ... '9':
3855 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003856 case '.':
3857 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003858 case 'p':
3859 ls = 1;
3860 /* fall through */
3861 case 'd':
3862 case 'u':
3863 case 'x':
3864 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003865 switch (ls) {
3866 case 0:
3867 vsize = 4;
3868 break;
3869 case 1:
3870 vsize = pevent->long_size;
3871 break;
3872 case 2:
3873 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003874 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003875 default:
3876 vsize = ls; /* ? */
3877 break;
3878 }
3879 /* fall through */
3880 case '*':
3881 if (*ptr == '*')
3882 vsize = 4;
3883
Steven Rostedtf7d82352012-04-06 00:47:53 +02003884 /* the pointers are always 4 bytes aligned */
3885 bptr = (void *)(((unsigned long)bptr + 3) &
3886 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003887 val = pevent_read_number(pevent, bptr, vsize);
3888 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003889 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003890 if (!arg) {
3891 do_warning("%s(%d): not enough memory!",
3892 __func__, __LINE__);
3893 goto out_free;
3894 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003895 arg->next = NULL;
3896 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003897 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3898 free(arg);
3899 goto out_free;
3900 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003901 *next = arg;
3902 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003903 /*
3904 * The '*' case means that an arg is used as the length.
3905 * We need to continue to figure out for what.
3906 */
3907 if (*ptr == '*')
3908 goto process_again;
3909
Steven Rostedtf7d82352012-04-06 00:47:53 +02003910 break;
3911 case 's':
3912 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003913 if (!arg) {
3914 do_warning("%s(%d): not enough memory!",
3915 __func__, __LINE__);
3916 goto out_free;
3917 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003918 arg->next = NULL;
3919 arg->type = PRINT_BSTRING;
3920 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003921 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003922 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003923 bptr += strlen(bptr) + 1;
3924 *next = arg;
3925 next = &arg->next;
3926 default:
3927 break;
3928 }
3929 }
3930 }
3931
3932 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003933
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003934out_free:
3935 free_args(args);
3936 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003937}
3938
3939static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003940get_bprint_format(void *data, int size __maybe_unused,
3941 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003942{
3943 struct pevent *pevent = event->pevent;
3944 unsigned long long addr;
3945 struct format_field *field;
3946 struct printk_map *printk;
3947 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003948
3949 field = pevent->bprint_fmt_field;
3950
3951 if (!field) {
3952 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003953 if (!field) {
3954 do_warning("can't find format field for binary printk");
3955 return NULL;
3956 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003957 pevent->bprint_fmt_field = field;
3958 }
3959
3960 addr = pevent_read_number(pevent, data + field->offset, field->size);
3961
3962 printk = find_printk(pevent, addr);
3963 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003964 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003965 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003966 return format;
3967 }
3968
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04003969 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003970 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003971
3972 return format;
3973}
3974
3975static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3976 struct event_format *event, struct print_arg *arg)
3977{
3978 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03003979 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02003980
3981 if (arg->type == PRINT_FUNC) {
3982 process_defined_func(s, data, size, event, arg);
3983 return;
3984 }
3985
3986 if (arg->type != PRINT_FIELD) {
3987 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3988 arg->type);
3989 return;
3990 }
3991
3992 if (mac == 'm')
3993 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3994 if (!arg->field.field) {
3995 arg->field.field =
3996 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003997 if (!arg->field.field) {
3998 do_warning("%s: field %s not found",
3999 __func__, arg->field.name);
4000 return;
4001 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004002 }
4003 if (arg->field.field->size != 6) {
4004 trace_seq_printf(s, "INVALIDMAC");
4005 return;
4006 }
4007 buf = data + arg->field.field->offset;
4008 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4009}
4010
Namhyung Kim600da3c2012-06-22 17:10:15 +09004011static int is_printable_array(char *p, unsigned int len)
4012{
4013 unsigned int i;
4014
4015 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004016 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004017 return 0;
4018 return 1;
4019}
4020
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03004021static void print_event_fields(struct trace_seq *s, void *data,
4022 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004023 struct event_format *event)
4024{
4025 struct format_field *field;
4026 unsigned long long val;
4027 unsigned int offset, len, i;
4028
4029 field = event->format.fields;
4030 while (field) {
4031 trace_seq_printf(s, " %s=", field->name);
4032 if (field->flags & FIELD_IS_ARRAY) {
4033 offset = field->offset;
4034 len = field->size;
4035 if (field->flags & FIELD_IS_DYNAMIC) {
4036 val = pevent_read_number(event->pevent, data + offset, len);
4037 offset = val;
4038 len = offset >> 16;
4039 offset &= 0xffff;
4040 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09004041 if (field->flags & FIELD_IS_STRING &&
4042 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004043 trace_seq_printf(s, "%s", (char *)data + offset);
4044 } else {
4045 trace_seq_puts(s, "ARRAY[");
4046 for (i = 0; i < len; i++) {
4047 if (i)
4048 trace_seq_puts(s, ", ");
4049 trace_seq_printf(s, "%02x",
4050 *((unsigned char *)data + offset + i));
4051 }
4052 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09004053 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004054 }
4055 } else {
4056 val = pevent_read_number(event->pevent, data + field->offset,
4057 field->size);
4058 if (field->flags & FIELD_IS_POINTER) {
4059 trace_seq_printf(s, "0x%llx", val);
4060 } else if (field->flags & FIELD_IS_SIGNED) {
4061 switch (field->size) {
4062 case 4:
4063 /*
4064 * If field is long then print it in hex.
4065 * A long usually stores pointers.
4066 */
4067 if (field->flags & FIELD_IS_LONG)
4068 trace_seq_printf(s, "0x%x", (int)val);
4069 else
4070 trace_seq_printf(s, "%d", (int)val);
4071 break;
4072 case 2:
4073 trace_seq_printf(s, "%2d", (short)val);
4074 break;
4075 case 1:
4076 trace_seq_printf(s, "%1d", (char)val);
4077 break;
4078 default:
4079 trace_seq_printf(s, "%lld", val);
4080 }
4081 } else {
4082 if (field->flags & FIELD_IS_LONG)
4083 trace_seq_printf(s, "0x%llx", val);
4084 else
4085 trace_seq_printf(s, "%llu", val);
4086 }
4087 }
4088 field = field->next;
4089 }
4090}
4091
4092static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4093{
4094 struct pevent *pevent = event->pevent;
4095 struct print_fmt *print_fmt = &event->print_fmt;
4096 struct print_arg *arg = print_fmt->args;
4097 struct print_arg *args = NULL;
4098 const char *ptr = print_fmt->format;
4099 unsigned long long val;
4100 struct func_map *func;
4101 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004102 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004103 char *bprint_fmt = NULL;
4104 char format[32];
4105 int show_func;
4106 int len_as_arg;
4107 int len_arg;
4108 int len;
4109 int ls;
4110
4111 if (event->flags & EVENT_FL_FAILED) {
4112 trace_seq_printf(s, "[FAILED TO PARSE]");
4113 print_event_fields(s, data, size, event);
4114 return;
4115 }
4116
4117 if (event->flags & EVENT_FL_ISBPRINT) {
4118 bprint_fmt = get_bprint_format(data, size, event);
4119 args = make_bprint_args(bprint_fmt, data, size, event);
4120 arg = args;
4121 ptr = bprint_fmt;
4122 }
4123
4124 for (; *ptr; ptr++) {
4125 ls = 0;
4126 if (*ptr == '\\') {
4127 ptr++;
4128 switch (*ptr) {
4129 case 'n':
4130 trace_seq_putc(s, '\n');
4131 break;
4132 case 't':
4133 trace_seq_putc(s, '\t');
4134 break;
4135 case 'r':
4136 trace_seq_putc(s, '\r');
4137 break;
4138 case '\\':
4139 trace_seq_putc(s, '\\');
4140 break;
4141 default:
4142 trace_seq_putc(s, *ptr);
4143 break;
4144 }
4145
4146 } else if (*ptr == '%') {
4147 saveptr = ptr;
4148 show_func = 0;
4149 len_as_arg = 0;
4150 cont_process:
4151 ptr++;
4152 switch (*ptr) {
4153 case '%':
4154 trace_seq_putc(s, '%');
4155 break;
4156 case '#':
4157 /* FIXME: need to handle properly */
4158 goto cont_process;
4159 case 'h':
4160 ls--;
4161 goto cont_process;
4162 case 'l':
4163 ls++;
4164 goto cont_process;
4165 case 'L':
4166 ls = 2;
4167 goto cont_process;
4168 case '*':
4169 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004170 if (!arg) {
4171 do_warning("no argument match");
4172 event->flags |= EVENT_FL_FAILED;
4173 goto out_failed;
4174 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004175 len_arg = eval_num_arg(data, size, event, arg);
4176 len_as_arg = 1;
4177 arg = arg->next;
4178 goto cont_process;
4179 case '.':
4180 case 'z':
4181 case 'Z':
4182 case '0' ... '9':
4183 goto cont_process;
4184 case 'p':
4185 if (pevent->long_size == 4)
4186 ls = 1;
4187 else
4188 ls = 2;
4189
4190 if (*(ptr+1) == 'F' ||
4191 *(ptr+1) == 'f') {
4192 ptr++;
4193 show_func = *ptr;
4194 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4195 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4196 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004197 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004198 break;
4199 }
4200
4201 /* fall through */
4202 case 'd':
4203 case 'i':
4204 case 'x':
4205 case 'X':
4206 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004207 if (!arg) {
4208 do_warning("no argument match");
4209 event->flags |= EVENT_FL_FAILED;
4210 goto out_failed;
4211 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004212
4213 len = ((unsigned long)ptr + 1) -
4214 (unsigned long)saveptr;
4215
4216 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004217 if (len > 31) {
4218 do_warning("bad format!");
4219 event->flags |= EVENT_FL_FAILED;
4220 len = 31;
4221 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004222
4223 memcpy(format, saveptr, len);
4224 format[len] = 0;
4225
4226 val = eval_num_arg(data, size, event, arg);
4227 arg = arg->next;
4228
4229 if (show_func) {
4230 func = find_func(pevent, val);
4231 if (func) {
4232 trace_seq_puts(s, func->func);
4233 if (show_func == 'F')
4234 trace_seq_printf(s,
4235 "+0x%llx",
4236 val - func->addr);
4237 break;
4238 }
4239 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004240 if (pevent->long_size == 8 && ls &&
4241 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004242 char *p;
4243
4244 ls = 2;
4245 /* make %l into %ll */
4246 p = strchr(format, 'l');
4247 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004248 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004249 else if (strcmp(format, "%p") == 0)
4250 strcpy(format, "0x%llx");
4251 }
4252 switch (ls) {
4253 case -2:
4254 if (len_as_arg)
4255 trace_seq_printf(s, format, len_arg, (char)val);
4256 else
4257 trace_seq_printf(s, format, (char)val);
4258 break;
4259 case -1:
4260 if (len_as_arg)
4261 trace_seq_printf(s, format, len_arg, (short)val);
4262 else
4263 trace_seq_printf(s, format, (short)val);
4264 break;
4265 case 0:
4266 if (len_as_arg)
4267 trace_seq_printf(s, format, len_arg, (int)val);
4268 else
4269 trace_seq_printf(s, format, (int)val);
4270 break;
4271 case 1:
4272 if (len_as_arg)
4273 trace_seq_printf(s, format, len_arg, (long)val);
4274 else
4275 trace_seq_printf(s, format, (long)val);
4276 break;
4277 case 2:
4278 if (len_as_arg)
4279 trace_seq_printf(s, format, len_arg,
4280 (long long)val);
4281 else
4282 trace_seq_printf(s, format, (long long)val);
4283 break;
4284 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004285 do_warning("bad count (%d)", ls);
4286 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004287 }
4288 break;
4289 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004290 if (!arg) {
4291 do_warning("no matching argument");
4292 event->flags |= EVENT_FL_FAILED;
4293 goto out_failed;
4294 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004295
4296 len = ((unsigned long)ptr + 1) -
4297 (unsigned long)saveptr;
4298
4299 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004300 if (len > 31) {
4301 do_warning("bad format!");
4302 event->flags |= EVENT_FL_FAILED;
4303 len = 31;
4304 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004305
4306 memcpy(format, saveptr, len);
4307 format[len] = 0;
4308 if (!len_as_arg)
4309 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05004310 /* Use helper trace_seq */
4311 trace_seq_init(&p);
4312 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02004313 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05004314 trace_seq_terminate(&p);
4315 trace_seq_puts(s, p.buffer);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004316 arg = arg->next;
4317 break;
4318 default:
4319 trace_seq_printf(s, ">%c<", *ptr);
4320
4321 }
4322 } else
4323 trace_seq_putc(s, *ptr);
4324 }
4325
Namhyung Kim245c5a12012-09-07 11:49:45 +09004326 if (event->flags & EVENT_FL_FAILED) {
4327out_failed:
4328 trace_seq_printf(s, "[FAILED TO PARSE]");
4329 }
4330
Steven Rostedtf7d82352012-04-06 00:47:53 +02004331 if (args) {
4332 free_args(args);
4333 free(bprint_fmt);
4334 }
4335}
4336
4337/**
4338 * pevent_data_lat_fmt - parse the data for the latency format
4339 * @pevent: a handle to the pevent
4340 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004341 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004342 *
4343 * This parses out the Latency format (interrupts disabled,
4344 * need rescheduling, in hard/soft interrupt, preempt count
4345 * and lock depth) and places it into the trace_seq.
4346 */
4347void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004348 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004349{
4350 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004351 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004352 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004353 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004354 unsigned int lat_flags;
4355 unsigned int pc;
4356 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004357 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004358 int hardirq;
4359 int softirq;
4360 void *data = record->data;
4361
4362 lat_flags = parse_common_flags(pevent, data);
4363 pc = parse_common_pc(pevent, data);
4364 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004365 if (lock_depth_exists)
4366 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004367 else if (check_lock_depth) {
4368 lock_depth = parse_common_lock_depth(pevent, data);
4369 if (lock_depth < 0)
4370 check_lock_depth = 0;
4371 else
4372 lock_depth_exists = 1;
4373 }
4374
4375 /* migrate_disable may not always exist */
4376 if (migrate_disable_exists)
4377 migrate_disable = parse_common_migrate_disable(pevent, data);
4378 else if (check_migrate_disable) {
4379 migrate_disable = parse_common_migrate_disable(pevent, data);
4380 if (migrate_disable < 0)
4381 check_migrate_disable = 0;
4382 else
4383 migrate_disable_exists = 1;
4384 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004385
4386 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4387 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4388
4389 trace_seq_printf(s, "%c%c%c",
4390 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4391 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4392 'X' : '.',
4393 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4394 'N' : '.',
4395 (hardirq && softirq) ? 'H' :
4396 hardirq ? 'h' : softirq ? 's' : '.');
4397
4398 if (pc)
4399 trace_seq_printf(s, "%x", pc);
4400 else
4401 trace_seq_putc(s, '.');
4402
Steven Rostedt0866a972012-05-22 14:52:40 +09004403 if (migrate_disable_exists) {
4404 if (migrate_disable < 0)
4405 trace_seq_putc(s, '.');
4406 else
4407 trace_seq_printf(s, "%d", migrate_disable);
4408 }
4409
Steven Rostedtf7d82352012-04-06 00:47:53 +02004410 if (lock_depth_exists) {
4411 if (lock_depth < 0)
4412 trace_seq_putc(s, '.');
4413 else
4414 trace_seq_printf(s, "%d", lock_depth);
4415 }
4416
4417 trace_seq_terminate(s);
4418}
4419
4420/**
4421 * pevent_data_type - parse out the given event type
4422 * @pevent: a handle to the pevent
4423 * @rec: the record to read from
4424 *
4425 * This returns the event id from the @rec.
4426 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004427int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004428{
4429 return trace_parse_common_type(pevent, rec->data);
4430}
4431
4432/**
4433 * pevent_data_event_from_type - find the event by a given type
4434 * @pevent: a handle to the pevent
4435 * @type: the type of the event.
4436 *
4437 * This returns the event form a given @type;
4438 */
4439struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4440{
4441 return pevent_find_event(pevent, type);
4442}
4443
4444/**
4445 * pevent_data_pid - parse the PID from raw data
4446 * @pevent: a handle to the pevent
4447 * @rec: the record to parse
4448 *
4449 * This returns the PID from a raw data.
4450 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004451int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004452{
4453 return parse_common_pid(pevent, rec->data);
4454}
4455
4456/**
4457 * pevent_data_comm_from_pid - return the command line from PID
4458 * @pevent: a handle to the pevent
4459 * @pid: the PID of the task to search for
4460 *
4461 * This returns a pointer to the command line that has the given
4462 * @pid.
4463 */
4464const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4465{
4466 const char *comm;
4467
4468 comm = find_cmdline(pevent, pid);
4469 return comm;
4470}
4471
4472/**
4473 * pevent_data_comm_from_pid - parse the data into the print format
4474 * @s: the trace_seq to write to
4475 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004476 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004477 *
4478 * This parses the raw @data using the given @event information and
4479 * writes the print format into the trace_seq.
4480 */
4481void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004482 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004483{
4484 int print_pretty = 1;
4485
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004486 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004487 print_event_fields(s, record->data, record->size, event);
4488 else {
4489
Steven Rostedtc6c2b962013-11-01 17:53:59 -04004490 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004491 print_pretty = event->handler(s, record, event,
4492 event->context);
4493
4494 if (print_pretty)
4495 pretty_print(s, record->data, record->size, event);
4496 }
4497
4498 trace_seq_terminate(s);
4499}
4500
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004501static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4502{
4503 if (!use_trace_clock)
4504 return true;
4505
4506 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4507 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4508 return true;
4509
4510 /* trace_clock is setting in tsc or counter mode */
4511 return false;
4512}
4513
Steven Rostedtf7d82352012-04-06 00:47:53 +02004514void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004515 struct pevent_record *record, bool use_trace_clock)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004516{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004517 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004518 struct event_format *event;
4519 unsigned long secs;
4520 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004521 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004522 const char *comm;
4523 void *data = record->data;
4524 int type;
4525 int pid;
4526 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004527 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004528 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004529
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004530 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4531 use_trace_clock);
4532 if (use_usec_format) {
4533 secs = record->ts / NSECS_PER_SEC;
4534 nsecs = record->ts - secs * NSECS_PER_SEC;
4535 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004536
4537 if (record->size < 0) {
4538 do_warning("ug! negative record size %d", record->size);
4539 return;
4540 }
4541
4542 type = trace_parse_common_type(pevent, data);
4543
4544 event = pevent_find_event(pevent, type);
4545 if (!event) {
4546 do_warning("ug! no event found for type %d", type);
4547 return;
4548 }
4549
4550 pid = parse_common_pid(pevent, data);
4551 comm = find_cmdline(pevent, pid);
4552
4553 if (pevent->latency_format) {
4554 trace_seq_printf(s, "%8.8s-%-5d %3d",
4555 comm, pid, record->cpu);
4556 pevent_data_lat_fmt(pevent, s, record);
4557 } else
4558 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4559
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004560 if (use_usec_format) {
4561 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4562 usecs = nsecs;
4563 p = 9;
4564 } else {
4565 usecs = (nsecs + 500) / NSECS_PER_USEC;
4566 p = 6;
4567 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02004568
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04004569 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4570 secs, p, usecs, event->name);
4571 } else
4572 trace_seq_printf(s, " %12llu: %s: ",
4573 record->ts, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004574
4575 /* Space out the event names evenly. */
4576 len = strlen(event->name);
4577 if (len < 20)
4578 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4579
4580 pevent_event_info(s, event, record);
4581}
4582
4583static int events_id_cmp(const void *a, const void *b)
4584{
4585 struct event_format * const * ea = a;
4586 struct event_format * const * eb = b;
4587
4588 if ((*ea)->id < (*eb)->id)
4589 return -1;
4590
4591 if ((*ea)->id > (*eb)->id)
4592 return 1;
4593
4594 return 0;
4595}
4596
4597static int events_name_cmp(const void *a, const void *b)
4598{
4599 struct event_format * const * ea = a;
4600 struct event_format * const * eb = b;
4601 int res;
4602
4603 res = strcmp((*ea)->name, (*eb)->name);
4604 if (res)
4605 return res;
4606
4607 res = strcmp((*ea)->system, (*eb)->system);
4608 if (res)
4609 return res;
4610
4611 return events_id_cmp(a, b);
4612}
4613
4614static int events_system_cmp(const void *a, const void *b)
4615{
4616 struct event_format * const * ea = a;
4617 struct event_format * const * eb = b;
4618 int res;
4619
4620 res = strcmp((*ea)->system, (*eb)->system);
4621 if (res)
4622 return res;
4623
4624 res = strcmp((*ea)->name, (*eb)->name);
4625 if (res)
4626 return res;
4627
4628 return events_id_cmp(a, b);
4629}
4630
4631struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4632{
4633 struct event_format **events;
4634 int (*sort)(const void *a, const void *b);
4635
4636 events = pevent->sort_events;
4637
4638 if (events && pevent->last_type == sort_type)
4639 return events;
4640
4641 if (!events) {
4642 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4643 if (!events)
4644 return NULL;
4645
4646 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4647 events[pevent->nr_events] = NULL;
4648
4649 pevent->sort_events = events;
4650
4651 /* the internal events are sorted by id */
4652 if (sort_type == EVENT_SORT_ID) {
4653 pevent->last_type = sort_type;
4654 return events;
4655 }
4656 }
4657
4658 switch (sort_type) {
4659 case EVENT_SORT_ID:
4660 sort = events_id_cmp;
4661 break;
4662 case EVENT_SORT_NAME:
4663 sort = events_name_cmp;
4664 break;
4665 case EVENT_SORT_SYSTEM:
4666 sort = events_system_cmp;
4667 break;
4668 default:
4669 return events;
4670 }
4671
4672 qsort(events, pevent->nr_events, sizeof(*events), sort);
4673 pevent->last_type = sort_type;
4674
4675 return events;
4676}
4677
4678static struct format_field **
4679get_event_fields(const char *type, const char *name,
4680 int count, struct format_field *list)
4681{
4682 struct format_field **fields;
4683 struct format_field *field;
4684 int i = 0;
4685
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004686 fields = malloc(sizeof(*fields) * (count + 1));
4687 if (!fields)
4688 return NULL;
4689
Steven Rostedtf7d82352012-04-06 00:47:53 +02004690 for (field = list; field; field = field->next) {
4691 fields[i++] = field;
4692 if (i == count + 1) {
4693 do_warning("event %s has more %s fields than specified",
4694 name, type);
4695 i--;
4696 break;
4697 }
4698 }
4699
4700 if (i != count)
4701 do_warning("event %s has less %s fields than specified",
4702 name, type);
4703
4704 fields[i] = NULL;
4705
4706 return fields;
4707}
4708
4709/**
4710 * pevent_event_common_fields - return a list of common fields for an event
4711 * @event: the event to return the common fields of.
4712 *
4713 * Returns an allocated array of fields. The last item in the array is NULL.
4714 * The array must be freed with free().
4715 */
4716struct format_field **pevent_event_common_fields(struct event_format *event)
4717{
4718 return get_event_fields("common", event->name,
4719 event->format.nr_common,
4720 event->format.common_fields);
4721}
4722
4723/**
4724 * pevent_event_fields - return a list of event specific fields for an event
4725 * @event: the event to return the fields of.
4726 *
4727 * Returns an allocated array of fields. The last item in the array is NULL.
4728 * The array must be freed with free().
4729 */
4730struct format_field **pevent_event_fields(struct event_format *event)
4731{
4732 return get_event_fields("event", event->name,
4733 event->format.nr_fields,
4734 event->format.fields);
4735}
4736
4737static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4738{
4739 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4740 if (field->next) {
4741 trace_seq_puts(s, ", ");
4742 print_fields(s, field->next);
4743 }
4744}
4745
4746/* for debugging */
4747static void print_args(struct print_arg *args)
4748{
4749 int print_paren = 1;
4750 struct trace_seq s;
4751
4752 switch (args->type) {
4753 case PRINT_NULL:
4754 printf("null");
4755 break;
4756 case PRINT_ATOM:
4757 printf("%s", args->atom.atom);
4758 break;
4759 case PRINT_FIELD:
4760 printf("REC->%s", args->field.name);
4761 break;
4762 case PRINT_FLAGS:
4763 printf("__print_flags(");
4764 print_args(args->flags.field);
4765 printf(", %s, ", args->flags.delim);
4766 trace_seq_init(&s);
4767 print_fields(&s, args->flags.flags);
4768 trace_seq_do_printf(&s);
4769 trace_seq_destroy(&s);
4770 printf(")");
4771 break;
4772 case PRINT_SYMBOL:
4773 printf("__print_symbolic(");
4774 print_args(args->symbol.field);
4775 printf(", ");
4776 trace_seq_init(&s);
4777 print_fields(&s, args->symbol.symbols);
4778 trace_seq_do_printf(&s);
4779 trace_seq_destroy(&s);
4780 printf(")");
4781 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004782 case PRINT_HEX:
4783 printf("__print_hex(");
4784 print_args(args->hex.field);
4785 printf(", ");
4786 print_args(args->hex.size);
4787 printf(")");
4788 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004789 case PRINT_STRING:
4790 case PRINT_BSTRING:
4791 printf("__get_str(%s)", args->string.string);
4792 break;
4793 case PRINT_TYPE:
4794 printf("(%s)", args->typecast.type);
4795 print_args(args->typecast.item);
4796 break;
4797 case PRINT_OP:
4798 if (strcmp(args->op.op, ":") == 0)
4799 print_paren = 0;
4800 if (print_paren)
4801 printf("(");
4802 print_args(args->op.left);
4803 printf(" %s ", args->op.op);
4804 print_args(args->op.right);
4805 if (print_paren)
4806 printf(")");
4807 break;
4808 default:
4809 /* we should warn... */
4810 return;
4811 }
4812 if (args->next) {
4813 printf("\n");
4814 print_args(args->next);
4815 }
4816}
4817
4818static void parse_header_field(const char *field,
4819 int *offset, int *size, int mandatory)
4820{
4821 unsigned long long save_input_buf_ptr;
4822 unsigned long long save_input_buf_siz;
4823 char *token;
4824 int type;
4825
4826 save_input_buf_ptr = input_buf_ptr;
4827 save_input_buf_siz = input_buf_siz;
4828
4829 if (read_expected(EVENT_ITEM, "field") < 0)
4830 return;
4831 if (read_expected(EVENT_OP, ":") < 0)
4832 return;
4833
4834 /* type */
4835 if (read_expect_type(EVENT_ITEM, &token) < 0)
4836 goto fail;
4837 free_token(token);
4838
4839 /*
4840 * If this is not a mandatory field, then test it first.
4841 */
4842 if (mandatory) {
4843 if (read_expected(EVENT_ITEM, field) < 0)
4844 return;
4845 } else {
4846 if (read_expect_type(EVENT_ITEM, &token) < 0)
4847 goto fail;
4848 if (strcmp(token, field) != 0)
4849 goto discard;
4850 free_token(token);
4851 }
4852
4853 if (read_expected(EVENT_OP, ";") < 0)
4854 return;
4855 if (read_expected(EVENT_ITEM, "offset") < 0)
4856 return;
4857 if (read_expected(EVENT_OP, ":") < 0)
4858 return;
4859 if (read_expect_type(EVENT_ITEM, &token) < 0)
4860 goto fail;
4861 *offset = atoi(token);
4862 free_token(token);
4863 if (read_expected(EVENT_OP, ";") < 0)
4864 return;
4865 if (read_expected(EVENT_ITEM, "size") < 0)
4866 return;
4867 if (read_expected(EVENT_OP, ":") < 0)
4868 return;
4869 if (read_expect_type(EVENT_ITEM, &token) < 0)
4870 goto fail;
4871 *size = atoi(token);
4872 free_token(token);
4873 if (read_expected(EVENT_OP, ";") < 0)
4874 return;
4875 type = read_token(&token);
4876 if (type != EVENT_NEWLINE) {
4877 /* newer versions of the kernel have a "signed" type */
4878 if (type != EVENT_ITEM)
4879 goto fail;
4880
4881 if (strcmp(token, "signed") != 0)
4882 goto fail;
4883
4884 free_token(token);
4885
4886 if (read_expected(EVENT_OP, ":") < 0)
4887 return;
4888
4889 if (read_expect_type(EVENT_ITEM, &token))
4890 goto fail;
4891
4892 free_token(token);
4893 if (read_expected(EVENT_OP, ";") < 0)
4894 return;
4895
4896 if (read_expect_type(EVENT_NEWLINE, &token))
4897 goto fail;
4898 }
4899 fail:
4900 free_token(token);
4901 return;
4902
4903 discard:
4904 input_buf_ptr = save_input_buf_ptr;
4905 input_buf_siz = save_input_buf_siz;
4906 *offset = 0;
4907 *size = 0;
4908 free_token(token);
4909}
4910
4911/**
4912 * pevent_parse_header_page - parse the data stored in the header page
4913 * @pevent: the handle to the pevent
4914 * @buf: the buffer storing the header page format string
4915 * @size: the size of @buf
4916 * @long_size: the long size to use if there is no header
4917 *
4918 * This parses the header page format for information on the
4919 * ring buffer used. The @buf should be copied from
4920 *
4921 * /sys/kernel/debug/tracing/events/header_page
4922 */
4923int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4924 int long_size)
4925{
4926 int ignore;
4927
4928 if (!size) {
4929 /*
4930 * Old kernels did not have header page info.
4931 * Sorry but we just use what we find here in user space.
4932 */
4933 pevent->header_page_ts_size = sizeof(long long);
4934 pevent->header_page_size_size = long_size;
4935 pevent->header_page_data_offset = sizeof(long long) + long_size;
4936 pevent->old_format = 1;
4937 return -1;
4938 }
4939 init_input_buf(buf, size);
4940
4941 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4942 &pevent->header_page_ts_size, 1);
4943 parse_header_field("commit", &pevent->header_page_size_offset,
4944 &pevent->header_page_size_size, 1);
4945 parse_header_field("overwrite", &pevent->header_page_overwrite,
4946 &ignore, 0);
4947 parse_header_field("data", &pevent->header_page_data_offset,
4948 &pevent->header_page_data_size, 1);
4949
4950 return 0;
4951}
4952
4953static int event_matches(struct event_format *event,
4954 int id, const char *sys_name,
4955 const char *event_name)
4956{
4957 if (id >= 0 && id != event->id)
4958 return 0;
4959
4960 if (event_name && (strcmp(event_name, event->name) != 0))
4961 return 0;
4962
4963 if (sys_name && (strcmp(sys_name, event->system) != 0))
4964 return 0;
4965
4966 return 1;
4967}
4968
4969static void free_handler(struct event_handler *handle)
4970{
4971 free((void *)handle->sys_name);
4972 free((void *)handle->event_name);
4973 free(handle);
4974}
4975
4976static int find_event_handle(struct pevent *pevent, struct event_format *event)
4977{
4978 struct event_handler *handle, **next;
4979
4980 for (next = &pevent->handlers; *next;
4981 next = &(*next)->next) {
4982 handle = *next;
4983 if (event_matches(event, handle->id,
4984 handle->sys_name,
4985 handle->event_name))
4986 break;
4987 }
4988
4989 if (!(*next))
4990 return 0;
4991
4992 pr_stat("overriding event (%d) %s:%s with new print handler",
4993 event->id, event->system, event->name);
4994
4995 event->handler = handle->func;
4996 event->context = handle->context;
4997
4998 *next = handle->next;
4999 free_handler(handle);
5000
5001 return 1;
5002}
5003
5004/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005005 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005006 * @buf: the buffer storing the event format string
5007 * @size: the size of @buf
5008 * @sys: the system the event belongs to
5009 *
5010 * This parses the event format and creates an event structure
5011 * to quickly parse raw data for a given event.
5012 *
5013 * These files currently come from:
5014 *
5015 * /sys/kernel/debug/tracing/events/.../.../format
5016 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005017enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5018 struct pevent *pevent, const char *buf,
5019 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005020{
5021 struct event_format *event;
5022 int ret;
5023
5024 init_input_buf(buf, size);
5025
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005026 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02005027 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09005028 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005029
5030 event->name = event_read_name();
5031 if (!event->name) {
5032 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09005033 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5034 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005035 }
5036
5037 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005038 event->flags |= EVENT_FL_ISFTRACE;
5039
5040 if (strcmp(event->name, "bprint") == 0)
5041 event->flags |= EVENT_FL_ISBPRINT;
5042 }
5043
5044 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09005045 if (event->id < 0) {
5046 ret = PEVENT_ERRNO__READ_ID_FAILED;
5047 /*
5048 * This isn't an allocation error actually.
5049 * But as the ID is critical, just bail out.
5050 */
5051 goto event_alloc_failed;
5052 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005053
5054 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005055 if (!event->system) {
5056 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5057 goto event_alloc_failed;
5058 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005059
Steven Rostedt101782e2012-10-01 20:13:51 -04005060 /* Add pevent to event so that it can be referenced */
5061 event->pevent = pevent;
5062
Steven Rostedtf7d82352012-04-06 00:47:53 +02005063 ret = event_read_format(event);
5064 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005065 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5066 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005067 }
5068
5069 /*
5070 * If the event has an override, don't print warnings if the event
5071 * print format fails to parse.
5072 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005073 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005074 show_warning = 0;
5075
5076 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005077 show_warning = 1;
5078
Steven Rostedtf7d82352012-04-06 00:47:53 +02005079 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09005080 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5081 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005082 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005083
5084 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5085 struct format_field *field;
5086 struct print_arg *arg, **list;
5087
5088 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005089 list = &event->print_fmt.args;
5090 for (field = event->format.fields; field; field = field->next) {
5091 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09005092 if (!arg) {
5093 event->flags |= EVENT_FL_FAILED;
5094 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5095 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005096 arg->type = PRINT_FIELD;
5097 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09005098 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09005099 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005100 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005101 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005102 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005103 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09005104 *list = arg;
5105 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005106 }
5107 return 0;
5108 }
5109
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005110 return 0;
5111
5112 event_parse_failed:
5113 event->flags |= EVENT_FL_FAILED;
5114 return ret;
5115
5116 event_alloc_failed:
5117 free(event->system);
5118 free(event->name);
5119 free(event);
5120 *eventp = NULL;
5121 return ret;
5122}
5123
5124/**
5125 * pevent_parse_format - parse the event format
5126 * @buf: the buffer storing the event format string
5127 * @size: the size of @buf
5128 * @sys: the system the event belongs to
5129 *
5130 * This parses the event format and creates an event structure
5131 * to quickly parse raw data for a given event.
5132 *
5133 * These files currently come from:
5134 *
5135 * /sys/kernel/debug/tracing/events/.../.../format
5136 */
5137enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5138 unsigned long size, const char *sys)
5139{
5140 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5141}
5142
5143/**
5144 * pevent_parse_event - parse the event format
5145 * @pevent: the handle to the pevent
5146 * @buf: the buffer storing the event format string
5147 * @size: the size of @buf
5148 * @sys: the system the event belongs to
5149 *
5150 * This parses the event format and creates an event structure
5151 * to quickly parse raw data for a given event.
5152 *
5153 * These files currently come from:
5154 *
5155 * /sys/kernel/debug/tracing/events/.../.../format
5156 */
5157enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5158 unsigned long size, const char *sys)
5159{
5160 struct event_format *event = NULL;
5161 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5162
5163 if (event == NULL)
5164 return ret;
5165
Namhyung Kimf1b22562012-09-25 21:25:19 +09005166 if (add_event(pevent, event)) {
5167 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005168 goto event_add_failed;
Namhyung Kimf1b22562012-09-25 21:25:19 +09005169 }
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005170
Steven Rostedtf7d82352012-04-06 00:47:53 +02005171#define PRINT_ARGS 0
5172 if (PRINT_ARGS && event->print_fmt.args)
5173 print_args(event->print_fmt.args);
5174
5175 return 0;
5176
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005177event_add_failed:
Namhyung Kimf1b22562012-09-25 21:25:19 +09005178 pevent_free_format(event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005179 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005180}
5181
Namhyung Kim2f197b92012-08-22 16:00:30 +09005182#undef _PE
5183#define _PE(code, str) str
5184static const char * const pevent_error_str[] = {
5185 PEVENT_ERRORS
5186};
5187#undef _PE
5188
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005189int pevent_strerror(struct pevent *pevent __maybe_unused,
5190 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005191{
5192 int idx;
5193 const char *msg;
5194
5195 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005196 msg = strerror_r(errnum, buf, buflen);
5197 if (msg != buf) {
5198 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005199 memcpy(buf, msg, min(buflen - 1, len));
5200 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005201 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005202 return 0;
5203 }
5204
5205 if (errnum <= __PEVENT_ERRNO__START ||
5206 errnum >= __PEVENT_ERRNO__END)
5207 return -1;
5208
Namhyung Kimf63fe792012-08-23 16:37:00 +09005209 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005210 msg = pevent_error_str[idx];
5211
5212 switch (errnum) {
5213 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5214 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5215 case PEVENT_ERRNO__READ_ID_FAILED:
5216 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5217 case PEVENT_ERRNO__READ_PRINT_FAILED:
5218 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
Arnaldo Carvalho de Melo7a905612012-11-09 15:50:33 -03005219 case PEVENT_ERRNO__INVALID_ARG_TYPE:
Namhyung Kim2f197b92012-08-22 16:00:30 +09005220 snprintf(buf, buflen, "%s", msg);
5221 break;
5222
5223 default:
5224 /* cannot reach here */
5225 break;
5226 }
5227
5228 return 0;
5229}
5230
Steven Rostedtf7d82352012-04-06 00:47:53 +02005231int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005232 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005233 unsigned long long *val, int err)
5234{
5235 if (!field) {
5236 if (err)
5237 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5238 return -1;
5239 }
5240
5241 if (pevent_read_number_field(field, record->data, val)) {
5242 if (err)
5243 trace_seq_printf(s, " %s=INVALID", name);
5244 return -1;
5245 }
5246
5247 return 0;
5248}
5249
5250/**
5251 * pevent_get_field_raw - return the raw pointer into the data field
5252 * @s: The seq to print to on error
5253 * @event: the event that the field is for
5254 * @name: The name of the field
5255 * @record: The record with the field name.
5256 * @len: place to store the field length.
5257 * @err: print default error if failed.
5258 *
5259 * Returns a pointer into record->data of the field and places
5260 * the length of the field in @len.
5261 *
5262 * On failure, it returns NULL.
5263 */
5264void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005265 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005266 int *len, int err)
5267{
5268 struct format_field *field;
5269 void *data = record->data;
5270 unsigned offset;
5271 int dummy;
5272
5273 if (!event)
5274 return NULL;
5275
5276 field = pevent_find_field(event, name);
5277
5278 if (!field) {
5279 if (err)
5280 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5281 return NULL;
5282 }
5283
5284 /* Allow @len to be NULL */
5285 if (!len)
5286 len = &dummy;
5287
5288 offset = field->offset;
5289 if (field->flags & FIELD_IS_DYNAMIC) {
5290 offset = pevent_read_number(event->pevent,
5291 data + offset, field->size);
5292 *len = offset >> 16;
5293 offset &= 0xffff;
5294 } else
5295 *len = field->size;
5296
5297 return data + offset;
5298}
5299
5300/**
5301 * pevent_get_field_val - find a field and return its value
5302 * @s: The seq to print to on error
5303 * @event: the event that the field is for
5304 * @name: The name of the field
5305 * @record: The record with the field name.
5306 * @val: place to store the value of the field.
5307 * @err: print default error if failed.
5308 *
5309 * Returns 0 on success -1 on field not found.
5310 */
5311int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005312 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005313 unsigned long long *val, int err)
5314{
5315 struct format_field *field;
5316
5317 if (!event)
5318 return -1;
5319
5320 field = pevent_find_field(event, name);
5321
5322 return get_field_val(s, field, name, record, val, err);
5323}
5324
5325/**
5326 * pevent_get_common_field_val - find a common field and return its value
5327 * @s: The seq to print to on error
5328 * @event: the event that the field is for
5329 * @name: The name of the field
5330 * @record: The record with the field name.
5331 * @val: place to store the value of the field.
5332 * @err: print default error if failed.
5333 *
5334 * Returns 0 on success -1 on field not found.
5335 */
5336int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005337 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005338 unsigned long long *val, int err)
5339{
5340 struct format_field *field;
5341
5342 if (!event)
5343 return -1;
5344
5345 field = pevent_find_common_field(event, name);
5346
5347 return get_field_val(s, field, name, record, val, err);
5348}
5349
5350/**
5351 * pevent_get_any_field_val - find a any field and return its value
5352 * @s: The seq to print to on error
5353 * @event: the event that the field is for
5354 * @name: The name of the field
5355 * @record: The record with the field name.
5356 * @val: place to store the value of the field.
5357 * @err: print default error if failed.
5358 *
5359 * Returns 0 on success -1 on field not found.
5360 */
5361int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005362 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005363 unsigned long long *val, int err)
5364{
5365 struct format_field *field;
5366
5367 if (!event)
5368 return -1;
5369
5370 field = pevent_find_any_field(event, name);
5371
5372 return get_field_val(s, field, name, record, val, err);
5373}
5374
5375/**
5376 * pevent_print_num_field - print a field and a format
5377 * @s: The seq to print to
5378 * @fmt: The printf format to print the field with.
5379 * @event: the event that the field is for
5380 * @name: The name of the field
5381 * @record: The record with the field name.
5382 * @err: print default error if failed.
5383 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005384 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005385 */
5386int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5387 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005388 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005389{
5390 struct format_field *field = pevent_find_field(event, name);
5391 unsigned long long val;
5392
5393 if (!field)
5394 goto failed;
5395
5396 if (pevent_read_number_field(field, record->data, &val))
5397 goto failed;
5398
5399 return trace_seq_printf(s, fmt, val);
5400
5401 failed:
5402 if (err)
5403 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5404 return -1;
5405}
5406
Steven Rostedt6d862b82013-11-01 17:54:00 -04005407/**
5408 * pevent_print_func_field - print a field and a format for function pointers
5409 * @s: The seq to print to
5410 * @fmt: The printf format to print the field with.
5411 * @event: the event that the field is for
5412 * @name: The name of the field
5413 * @record: The record with the field name.
5414 * @err: print default error if failed.
5415 *
5416 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5417 */
5418int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5419 struct event_format *event, const char *name,
5420 struct pevent_record *record, int err)
5421{
5422 struct format_field *field = pevent_find_field(event, name);
5423 struct pevent *pevent = event->pevent;
5424 unsigned long long val;
5425 struct func_map *func;
5426 char tmp[128];
5427
5428 if (!field)
5429 goto failed;
5430
5431 if (pevent_read_number_field(field, record->data, &val))
5432 goto failed;
5433
5434 func = find_func(pevent, val);
5435
5436 if (func)
5437 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5438 else
5439 sprintf(tmp, "0x%08llx", val);
5440
5441 return trace_seq_printf(s, fmt, tmp);
5442
5443 failed:
5444 if (err)
5445 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5446 return -1;
5447}
5448
Steven Rostedtf7d82352012-04-06 00:47:53 +02005449static void free_func_handle(struct pevent_function_handler *func)
5450{
5451 struct pevent_func_params *params;
5452
5453 free(func->name);
5454
5455 while (func->params) {
5456 params = func->params;
5457 func->params = params->next;
5458 free(params);
5459 }
5460
5461 free(func);
5462}
5463
5464/**
5465 * pevent_register_print_function - register a helper function
5466 * @pevent: the handle to the pevent
5467 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005468 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005469 * @name: the name of the helper function
5470 * @parameters: A list of enum pevent_func_arg_type
5471 *
5472 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005473 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005474 * of these functions.
5475 *
5476 * The @parameters is a variable list of pevent_func_arg_type enums that
5477 * must end with PEVENT_FUNC_ARG_VOID.
5478 */
5479int pevent_register_print_function(struct pevent *pevent,
5480 pevent_func_handler func,
5481 enum pevent_func_arg_type ret_type,
5482 char *name, ...)
5483{
5484 struct pevent_function_handler *func_handle;
5485 struct pevent_func_params **next_param;
5486 struct pevent_func_params *param;
5487 enum pevent_func_arg_type type;
5488 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005489 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005490
5491 func_handle = find_func_handler(pevent, name);
5492 if (func_handle) {
5493 /*
5494 * This is most like caused by the users own
5495 * plugins updating the function. This overrides the
5496 * system defaults.
5497 */
5498 pr_stat("override of function helper '%s'", name);
5499 remove_func_handler(pevent, name);
5500 }
5501
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005502 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005503 if (!func_handle) {
5504 do_warning("Failed to allocate function handler");
5505 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5506 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005507
5508 func_handle->ret_type = ret_type;
5509 func_handle->name = strdup(name);
5510 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005511 if (!func_handle->name) {
5512 do_warning("Failed to allocate function name");
5513 free(func_handle);
5514 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5515 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005516
5517 next_param = &(func_handle->params);
5518 va_start(ap, name);
5519 for (;;) {
5520 type = va_arg(ap, enum pevent_func_arg_type);
5521 if (type == PEVENT_FUNC_ARG_VOID)
5522 break;
5523
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03005524 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005525 do_warning("Invalid argument type %d", type);
5526 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005527 goto out_free;
5528 }
5529
Namhyung Kim67ed9392012-09-07 11:49:47 +09005530 param = malloc(sizeof(*param));
5531 if (!param) {
5532 do_warning("Failed to allocate function param");
5533 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5534 goto out_free;
5535 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005536 param->type = type;
5537 param->next = NULL;
5538
5539 *next_param = param;
5540 next_param = &(param->next);
5541
5542 func_handle->nr_args++;
5543 }
5544 va_end(ap);
5545
5546 func_handle->next = pevent->func_handlers;
5547 pevent->func_handlers = func_handle;
5548
5549 return 0;
5550 out_free:
5551 va_end(ap);
5552 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005553 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005554}
5555
5556/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005557 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005558 * @pevent: the handle to the pevent
5559 * @id: the id of the event to register
5560 * @sys_name: the system name the event belongs to
5561 * @event_name: the name of the event
5562 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005563 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005564 *
5565 * This function allows a developer to override the parsing of
5566 * a given event. If for some reason the default print format
5567 * is not sufficient, this function will register a function
5568 * for an event to be used to parse the data instead.
5569 *
5570 * If @id is >= 0, then it is used to find the event.
5571 * else @sys_name and @event_name are used.
5572 */
Namhyung Kim79d5adf2013-06-04 14:20:18 +09005573int pevent_register_event_handler(struct pevent *pevent, int id,
5574 const char *sys_name, const char *event_name,
5575 pevent_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005576{
5577 struct event_format *event;
5578 struct event_handler *handle;
5579
5580 if (id >= 0) {
5581 /* search by id */
5582 event = pevent_find_event(pevent, id);
5583 if (!event)
5584 goto not_found;
5585 if (event_name && (strcmp(event_name, event->name) != 0))
5586 goto not_found;
5587 if (sys_name && (strcmp(sys_name, event->system) != 0))
5588 goto not_found;
5589 } else {
5590 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5591 if (!event)
5592 goto not_found;
5593 }
5594
5595 pr_stat("overriding event (%d) %s:%s with new print handler",
5596 event->id, event->system, event->name);
5597
5598 event->handler = func;
5599 event->context = context;
5600 return 0;
5601
5602 not_found:
5603 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005604 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005605 if (!handle) {
5606 do_warning("Failed to allocate event handler");
5607 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5608 }
5609
Steven Rostedtf7d82352012-04-06 00:47:53 +02005610 handle->id = id;
5611 if (event_name)
5612 handle->event_name = strdup(event_name);
5613 if (sys_name)
5614 handle->sys_name = strdup(sys_name);
5615
Namhyung Kimca638582012-04-09 11:54:31 +09005616 if ((event_name && !handle->event_name) ||
5617 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005618 do_warning("Failed to allocate event/sys name");
5619 free((void *)handle->event_name);
5620 free((void *)handle->sys_name);
5621 free(handle);
5622 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005623 }
5624
Steven Rostedtf7d82352012-04-06 00:47:53 +02005625 handle->func = func;
5626 handle->next = pevent->handlers;
5627 pevent->handlers = handle;
5628 handle->context = context;
5629
5630 return -1;
5631}
5632
5633/**
5634 * pevent_alloc - create a pevent handle
5635 */
5636struct pevent *pevent_alloc(void)
5637{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005638 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005639
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005640 if (pevent)
5641 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005642
5643 return pevent;
5644}
5645
5646void pevent_ref(struct pevent *pevent)
5647{
5648 pevent->ref_count++;
5649}
5650
5651static void free_format_fields(struct format_field *field)
5652{
5653 struct format_field *next;
5654
5655 while (field) {
5656 next = field->next;
5657 free(field->type);
5658 free(field->name);
5659 free(field);
5660 field = next;
5661 }
5662}
5663
5664static void free_formats(struct format *format)
5665{
5666 free_format_fields(format->common_fields);
5667 free_format_fields(format->fields);
5668}
5669
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005670void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005671{
5672 free(event->name);
5673 free(event->system);
5674
5675 free_formats(&event->format);
5676
5677 free(event->print_fmt.format);
5678 free_args(event->print_fmt.args);
5679
5680 free(event);
5681}
5682
5683/**
5684 * pevent_free - free a pevent handle
5685 * @pevent: the pevent handle to free
5686 */
5687void pevent_free(struct pevent *pevent)
5688{
Steven Rostedta2525a02012-04-06 00:48:02 +02005689 struct cmdline_list *cmdlist, *cmdnext;
5690 struct func_list *funclist, *funcnext;
5691 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005692 struct pevent_function_handler *func_handler;
5693 struct event_handler *handle;
5694 int i;
5695
Steven Rostedta2525a02012-04-06 00:48:02 +02005696 if (!pevent)
5697 return;
5698
5699 cmdlist = pevent->cmdlist;
5700 funclist = pevent->funclist;
5701 printklist = pevent->printklist;
5702
Steven Rostedtf7d82352012-04-06 00:47:53 +02005703 pevent->ref_count--;
5704 if (pevent->ref_count)
5705 return;
5706
5707 if (pevent->cmdlines) {
5708 for (i = 0; i < pevent->cmdline_count; i++)
5709 free(pevent->cmdlines[i].comm);
5710 free(pevent->cmdlines);
5711 }
5712
5713 while (cmdlist) {
5714 cmdnext = cmdlist->next;
5715 free(cmdlist->comm);
5716 free(cmdlist);
5717 cmdlist = cmdnext;
5718 }
5719
5720 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005721 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005722 free(pevent->func_map[i].func);
5723 free(pevent->func_map[i].mod);
5724 }
5725 free(pevent->func_map);
5726 }
5727
5728 while (funclist) {
5729 funcnext = funclist->next;
5730 free(funclist->func);
5731 free(funclist->mod);
5732 free(funclist);
5733 funclist = funcnext;
5734 }
5735
5736 while (pevent->func_handlers) {
5737 func_handler = pevent->func_handlers;
5738 pevent->func_handlers = func_handler->next;
5739 free_func_handle(func_handler);
5740 }
5741
5742 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005743 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005744 free(pevent->printk_map[i].printk);
5745 free(pevent->printk_map);
5746 }
5747
5748 while (printklist) {
5749 printknext = printklist->next;
5750 free(printklist->printk);
5751 free(printklist);
5752 printklist = printknext;
5753 }
5754
5755 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005756 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005757
5758 while (pevent->handlers) {
5759 handle = pevent->handlers;
5760 pevent->handlers = handle->next;
5761 free_handler(handle);
5762 }
5763
5764 free(pevent->events);
5765 free(pevent->sort_events);
5766
5767 free(pevent);
5768}
5769
5770void pevent_unref(struct pevent *pevent)
5771{
5772 pevent_free(pevent);
5773}