blob: dc42e55469d1747392d431d0ea243ebcad43c0ba [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
308struct func_map {
309 unsigned long long addr;
310 char *func;
311 char *mod;
312};
313
314struct func_list {
315 struct func_list *next;
316 unsigned long long addr;
317 char *func;
318 char *mod;
319};
320
321static int func_cmp(const void *a, const void *b)
322{
323 const struct func_map *fa = a;
324 const struct func_map *fb = b;
325
326 if (fa->addr < fb->addr)
327 return -1;
328 if (fa->addr > fb->addr)
329 return 1;
330
331 return 0;
332}
333
334/*
335 * We are searching for a record in between, not an exact
336 * match.
337 */
338static int func_bcmp(const void *a, const void *b)
339{
340 const struct func_map *fa = a;
341 const struct func_map *fb = b;
342
343 if ((fa->addr == fb->addr) ||
344
345 (fa->addr > fb->addr &&
346 fa->addr < (fb+1)->addr))
347 return 0;
348
349 if (fa->addr < fb->addr)
350 return -1;
351
352 return 1;
353}
354
355static int func_map_init(struct pevent *pevent)
356{
357 struct func_list *funclist;
358 struct func_list *item;
359 struct func_map *func_map;
360 int i;
361
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300362 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
363 if (!func_map)
364 return -1;
365
Steven Rostedtf7d82352012-04-06 00:47:53 +0200366 funclist = pevent->funclist;
367
368 i = 0;
369 while (funclist) {
370 func_map[i].func = funclist->func;
371 func_map[i].addr = funclist->addr;
372 func_map[i].mod = funclist->mod;
373 i++;
374 item = funclist;
375 funclist = funclist->next;
376 free(item);
377 }
378
379 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
380
381 /*
382 * Add a special record at the end.
383 */
384 func_map[pevent->func_count].func = NULL;
385 func_map[pevent->func_count].addr = 0;
386 func_map[pevent->func_count].mod = NULL;
387
388 pevent->func_map = func_map;
389 pevent->funclist = NULL;
390
391 return 0;
392}
393
394static struct func_map *
395find_func(struct pevent *pevent, unsigned long long addr)
396{
397 struct func_map *func;
398 struct func_map key;
399
400 if (!pevent->func_map)
401 func_map_init(pevent);
402
403 key.addr = addr;
404
405 func = bsearch(&key, pevent->func_map, pevent->func_count,
406 sizeof(*pevent->func_map), func_bcmp);
407
408 return func;
409}
410
411/**
412 * pevent_find_function - find a function by a given address
413 * @pevent: handle for the pevent
414 * @addr: the address to find the function with
415 *
416 * Returns a pointer to the function stored that has the given
417 * address. Note, the address does not have to be exact, it
418 * will select the function that would contain the address.
419 */
420const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
421{
422 struct func_map *map;
423
424 map = find_func(pevent, addr);
425 if (!map)
426 return NULL;
427
428 return map->func;
429}
430
431/**
432 * pevent_find_function_address - find a function address by a given address
433 * @pevent: handle for the pevent
434 * @addr: the address to find the function with
435 *
436 * Returns the address the function starts at. This can be used in
437 * conjunction with pevent_find_function to print both the function
438 * name and the function offset.
439 */
440unsigned long long
441pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
442{
443 struct func_map *map;
444
445 map = find_func(pevent, addr);
446 if (!map)
447 return 0;
448
449 return map->addr;
450}
451
452/**
453 * pevent_register_function - register a function with a given address
454 * @pevent: handle for the pevent
455 * @function: the function name to register
456 * @addr: the address the function starts at
457 * @mod: the kernel module the function may be in (NULL for none)
458 *
459 * This registers a function name with an address and module.
460 * The @func passed in is duplicated.
461 */
462int pevent_register_function(struct pevent *pevent, char *func,
463 unsigned long long addr, char *mod)
464{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300465 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200466
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300467 if (!item)
468 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200469
470 item->next = pevent->funclist;
471 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300472 if (!item->func)
473 goto out_free;
474
475 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200476 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300477 if (!item->mod)
478 goto out_free_func;
479 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200480 item->mod = NULL;
481 item->addr = addr;
482
Namhyung Kimca638582012-04-09 11:54:31 +0900483 pevent->funclist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200484 pevent->func_count++;
485
486 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300487
488out_free_func:
489 free(item->func);
490 item->func = NULL;
491out_free:
492 free(item);
493 errno = ENOMEM;
494 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200495}
496
497/**
498 * pevent_print_funcs - print out the stored functions
499 * @pevent: handle for the pevent
500 *
501 * This prints out the stored functions.
502 */
503void pevent_print_funcs(struct pevent *pevent)
504{
505 int i;
506
507 if (!pevent->func_map)
508 func_map_init(pevent);
509
510 for (i = 0; i < (int)pevent->func_count; i++) {
511 printf("%016llx %s",
512 pevent->func_map[i].addr,
513 pevent->func_map[i].func);
514 if (pevent->func_map[i].mod)
515 printf(" [%s]\n", pevent->func_map[i].mod);
516 else
517 printf("\n");
518 }
519}
520
521struct printk_map {
522 unsigned long long addr;
523 char *printk;
524};
525
526struct printk_list {
527 struct printk_list *next;
528 unsigned long long addr;
529 char *printk;
530};
531
532static int printk_cmp(const void *a, const void *b)
533{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900534 const struct printk_map *pa = a;
535 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200536
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900537 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200538 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900539 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200540 return 1;
541
542 return 0;
543}
544
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300545static int printk_map_init(struct pevent *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200546{
547 struct printk_list *printklist;
548 struct printk_list *item;
549 struct printk_map *printk_map;
550 int i;
551
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300552 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
553 if (!printk_map)
554 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200555
556 printklist = pevent->printklist;
557
558 i = 0;
559 while (printklist) {
560 printk_map[i].printk = printklist->printk;
561 printk_map[i].addr = printklist->addr;
562 i++;
563 item = printklist;
564 printklist = printklist->next;
565 free(item);
566 }
567
568 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
569
570 pevent->printk_map = printk_map;
571 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300572
573 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200574}
575
576static struct printk_map *
577find_printk(struct pevent *pevent, unsigned long long addr)
578{
579 struct printk_map *printk;
580 struct printk_map key;
581
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300582 if (!pevent->printk_map && printk_map_init(pevent))
583 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200584
585 key.addr = addr;
586
587 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
588 sizeof(*pevent->printk_map), printk_cmp);
589
590 return printk;
591}
592
593/**
594 * pevent_register_print_string - register a string by its address
595 * @pevent: handle for the pevent
596 * @fmt: the string format to register
597 * @addr: the address the string was located at
598 *
599 * This registers a string by the address it was stored in the kernel.
600 * The @fmt passed in is duplicated.
601 */
602int pevent_register_print_string(struct pevent *pevent, char *fmt,
603 unsigned long long addr)
604{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300605 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200606
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300607 if (!item)
608 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200609
610 item->next = pevent->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200611 item->addr = addr;
612
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300613 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900614 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300615 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900616
617 pevent->printklist = item;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200618 pevent->printk_count++;
619
620 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300621
622out_free:
623 free(item);
624 errno = ENOMEM;
625 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200626}
627
628/**
629 * pevent_print_printk - print out the stored strings
630 * @pevent: handle for the pevent
631 *
632 * This prints the string formats that were stored.
633 */
634void pevent_print_printk(struct pevent *pevent)
635{
636 int i;
637
638 if (!pevent->printk_map)
639 printk_map_init(pevent);
640
641 for (i = 0; i < (int)pevent->printk_count; i++) {
642 printf("%016llx %s\n",
643 pevent->printk_map[i].addr,
644 pevent->printk_map[i].printk);
645 }
646}
647
648static struct event_format *alloc_event(void)
649{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -0300650 return calloc(1, sizeof(struct event_format));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200651}
652
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300653static int add_event(struct pevent *pevent, struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200654{
655 int i;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300656 struct event_format **events = realloc(pevent->events, sizeof(event) *
657 (pevent->nr_events + 1));
658 if (!events)
659 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200660
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300661 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200662
663 for (i = 0; i < pevent->nr_events; i++) {
664 if (pevent->events[i]->id > event->id)
665 break;
666 }
667 if (i < pevent->nr_events)
668 memmove(&pevent->events[i + 1],
669 &pevent->events[i],
670 sizeof(event) * (pevent->nr_events - i));
671
672 pevent->events[i] = event;
673 pevent->nr_events++;
674
675 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300676
677 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200678}
679
680static int event_item_type(enum event_type type)
681{
682 switch (type) {
683 case EVENT_ITEM ... EVENT_SQUOTE:
684 return 1;
685 case EVENT_ERROR ... EVENT_DELIM:
686 default:
687 return 0;
688 }
689}
690
691static void free_flag_sym(struct print_flag_sym *fsym)
692{
693 struct print_flag_sym *next;
694
695 while (fsym) {
696 next = fsym->next;
697 free(fsym->value);
698 free(fsym->str);
699 free(fsym);
700 fsym = next;
701 }
702}
703
704static void free_arg(struct print_arg *arg)
705{
706 struct print_arg *farg;
707
708 if (!arg)
709 return;
710
711 switch (arg->type) {
712 case PRINT_ATOM:
713 free(arg->atom.atom);
714 break;
715 case PRINT_FIELD:
716 free(arg->field.name);
717 break;
718 case PRINT_FLAGS:
719 free_arg(arg->flags.field);
720 free(arg->flags.delim);
721 free_flag_sym(arg->flags.flags);
722 break;
723 case PRINT_SYMBOL:
724 free_arg(arg->symbol.field);
725 free_flag_sym(arg->symbol.symbols);
726 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900727 case PRINT_HEX:
728 free_arg(arg->hex.field);
729 free_arg(arg->hex.size);
730 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200731 case PRINT_TYPE:
732 free(arg->typecast.type);
733 free_arg(arg->typecast.item);
734 break;
735 case PRINT_STRING:
736 case PRINT_BSTRING:
737 free(arg->string.string);
738 break;
739 case PRINT_DYNAMIC_ARRAY:
740 free(arg->dynarray.index);
741 break;
742 case PRINT_OP:
743 free(arg->op.op);
744 free_arg(arg->op.left);
745 free_arg(arg->op.right);
746 break;
747 case PRINT_FUNC:
748 while (arg->func.args) {
749 farg = arg->func.args;
750 arg->func.args = farg->next;
751 free_arg(farg);
752 }
753 break;
754
755 case PRINT_NULL:
756 default:
757 break;
758 }
759
760 free(arg);
761}
762
763static enum event_type get_type(int ch)
764{
765 if (ch == '\n')
766 return EVENT_NEWLINE;
767 if (isspace(ch))
768 return EVENT_SPACE;
769 if (isalnum(ch) || ch == '_')
770 return EVENT_ITEM;
771 if (ch == '\'')
772 return EVENT_SQUOTE;
773 if (ch == '"')
774 return EVENT_DQUOTE;
775 if (!isprint(ch))
776 return EVENT_NONE;
777 if (ch == '(' || ch == ')' || ch == ',')
778 return EVENT_DELIM;
779
780 return EVENT_OP;
781}
782
783static int __read_char(void)
784{
785 if (input_buf_ptr >= input_buf_siz)
786 return -1;
787
788 return input_buf[input_buf_ptr++];
789}
790
791static int __peek_char(void)
792{
793 if (input_buf_ptr >= input_buf_siz)
794 return -1;
795
796 return input_buf[input_buf_ptr];
797}
798
799/**
800 * pevent_peek_char - peek at the next character that will be read
801 *
802 * Returns the next character read, or -1 if end of buffer.
803 */
804int pevent_peek_char(void)
805{
806 return __peek_char();
807}
808
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900809static int extend_token(char **tok, char *buf, int size)
810{
811 char *newtok = realloc(*tok, size);
812
813 if (!newtok) {
814 free(*tok);
815 *tok = NULL;
816 return -1;
817 }
818
819 if (!*tok)
820 strcpy(newtok, buf);
821 else
822 strcat(newtok, buf);
823 *tok = newtok;
824
825 return 0;
826}
827
Steven Rostedtf7d82352012-04-06 00:47:53 +0200828static enum event_type force_token(const char *str, char **tok);
829
830static enum event_type __read_token(char **tok)
831{
832 char buf[BUFSIZ];
833 int ch, last_ch, quote_ch, next_ch;
834 int i = 0;
835 int tok_size = 0;
836 enum event_type type;
837
838 *tok = NULL;
839
840
841 ch = __read_char();
842 if (ch < 0)
843 return EVENT_NONE;
844
845 type = get_type(ch);
846 if (type == EVENT_NONE)
847 return type;
848
849 buf[i++] = ch;
850
851 switch (type) {
852 case EVENT_NEWLINE:
853 case EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300854 if (asprintf(tok, "%c", ch) < 0)
855 return EVENT_ERROR;
856
Steven Rostedtf7d82352012-04-06 00:47:53 +0200857 return type;
858
859 case EVENT_OP:
860 switch (ch) {
861 case '-':
862 next_ch = __peek_char();
863 if (next_ch == '>') {
864 buf[i++] = __read_char();
865 break;
866 }
867 /* fall through */
868 case '+':
869 case '|':
870 case '&':
871 case '>':
872 case '<':
873 last_ch = ch;
874 ch = __peek_char();
875 if (ch != last_ch)
876 goto test_equal;
877 buf[i++] = __read_char();
878 switch (last_ch) {
879 case '>':
880 case '<':
881 goto test_equal;
882 default:
883 break;
884 }
885 break;
886 case '!':
887 case '=':
888 goto test_equal;
889 default: /* what should we do instead? */
890 break;
891 }
892 buf[i] = 0;
893 *tok = strdup(buf);
894 return type;
895
896 test_equal:
897 ch = __peek_char();
898 if (ch == '=')
899 buf[i++] = __read_char();
900 goto out;
901
902 case EVENT_DQUOTE:
903 case EVENT_SQUOTE:
904 /* don't keep quotes */
905 i--;
906 quote_ch = ch;
907 last_ch = 0;
908 concat:
909 do {
910 if (i == (BUFSIZ - 1)) {
911 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200912 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900913
914 if (extend_token(tok, buf, tok_size) < 0)
915 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200916 i = 0;
917 }
918 last_ch = ch;
919 ch = __read_char();
920 buf[i++] = ch;
921 /* the '\' '\' will cancel itself */
922 if (ch == '\\' && last_ch == '\\')
923 last_ch = 0;
924 } while (ch != quote_ch || last_ch == '\\');
925 /* remove the last quote */
926 i--;
927
928 /*
929 * For strings (double quotes) check the next token.
930 * If it is another string, concatinate the two.
931 */
932 if (type == EVENT_DQUOTE) {
933 unsigned long long save_input_buf_ptr = input_buf_ptr;
934
935 do {
936 ch = __read_char();
937 } while (isspace(ch));
938 if (ch == '"')
939 goto concat;
940 input_buf_ptr = save_input_buf_ptr;
941 }
942
943 goto out;
944
945 case EVENT_ERROR ... EVENT_SPACE:
946 case EVENT_ITEM:
947 default:
948 break;
949 }
950
951 while (get_type(__peek_char()) == type) {
952 if (i == (BUFSIZ - 1)) {
953 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200954 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900955
956 if (extend_token(tok, buf, tok_size) < 0)
957 return EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200958 i = 0;
959 }
960 ch = __read_char();
961 buf[i++] = ch;
962 }
963
964 out:
965 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900966 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200967 return EVENT_NONE;
968
969 if (type == EVENT_ITEM) {
970 /*
971 * Older versions of the kernel has a bug that
972 * creates invalid symbols and will break the mac80211
973 * parsing. This is a work around to that bug.
974 *
975 * See Linux kernel commit:
976 * 811cb50baf63461ce0bdb234927046131fc7fa8b
977 */
978 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
979 free(*tok);
980 *tok = NULL;
981 return force_token("\"\%s\" ", tok);
982 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
983 free(*tok);
984 *tok = NULL;
985 return force_token("\" sta:%pM\" ", tok);
986 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
987 free(*tok);
988 *tok = NULL;
989 return force_token("\" vif:%p(%d)\" ", tok);
990 }
991 }
992
993 return type;
994}
995
996static enum event_type force_token(const char *str, char **tok)
997{
998 const char *save_input_buf;
999 unsigned long long save_input_buf_ptr;
1000 unsigned long long save_input_buf_siz;
1001 enum event_type type;
1002
1003 /* save off the current input pointers */
1004 save_input_buf = input_buf;
1005 save_input_buf_ptr = input_buf_ptr;
1006 save_input_buf_siz = input_buf_siz;
1007
1008 init_input_buf(str, strlen(str));
1009
1010 type = __read_token(tok);
1011
1012 /* reset back to original token */
1013 input_buf = save_input_buf;
1014 input_buf_ptr = save_input_buf_ptr;
1015 input_buf_siz = save_input_buf_siz;
1016
1017 return type;
1018}
1019
1020static void free_token(char *tok)
1021{
1022 if (tok)
1023 free(tok);
1024}
1025
1026static enum event_type read_token(char **tok)
1027{
1028 enum event_type type;
1029
1030 for (;;) {
1031 type = __read_token(tok);
1032 if (type != EVENT_SPACE)
1033 return type;
1034
1035 free_token(*tok);
1036 }
1037
1038 /* not reached */
1039 *tok = NULL;
1040 return EVENT_NONE;
1041}
1042
1043/**
1044 * pevent_read_token - access to utilites to use the pevent parser
1045 * @tok: The token to return
1046 *
1047 * This will parse tokens from the string given by
1048 * pevent_init_data().
1049 *
1050 * Returns the token type.
1051 */
1052enum event_type pevent_read_token(char **tok)
1053{
1054 return read_token(tok);
1055}
1056
1057/**
1058 * pevent_free_token - free a token returned by pevent_read_token
1059 * @token: the token to free
1060 */
1061void pevent_free_token(char *token)
1062{
1063 free_token(token);
1064}
1065
1066/* no newline */
1067static enum event_type read_token_item(char **tok)
1068{
1069 enum event_type type;
1070
1071 for (;;) {
1072 type = __read_token(tok);
1073 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1074 return type;
1075 free_token(*tok);
1076 *tok = NULL;
1077 }
1078
1079 /* not reached */
1080 *tok = NULL;
1081 return EVENT_NONE;
1082}
1083
1084static int test_type(enum event_type type, enum event_type expect)
1085{
1086 if (type != expect) {
1087 do_warning("Error: expected type %d but read %d",
1088 expect, type);
1089 return -1;
1090 }
1091 return 0;
1092}
1093
1094static int test_type_token(enum event_type type, const char *token,
1095 enum event_type expect, const char *expect_tok)
1096{
1097 if (type != expect) {
1098 do_warning("Error: expected type %d but read %d",
1099 expect, type);
1100 return -1;
1101 }
1102
1103 if (strcmp(token, expect_tok) != 0) {
1104 do_warning("Error: expected '%s' but read '%s'",
1105 expect_tok, token);
1106 return -1;
1107 }
1108 return 0;
1109}
1110
1111static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1112{
1113 enum event_type type;
1114
1115 if (newline_ok)
1116 type = read_token(tok);
1117 else
1118 type = read_token_item(tok);
1119 return test_type(type, expect);
1120}
1121
1122static int read_expect_type(enum event_type expect, char **tok)
1123{
1124 return __read_expect_type(expect, tok, 1);
1125}
1126
1127static int __read_expected(enum event_type expect, const char *str,
1128 int newline_ok)
1129{
1130 enum event_type type;
1131 char *token;
1132 int ret;
1133
1134 if (newline_ok)
1135 type = read_token(&token);
1136 else
1137 type = read_token_item(&token);
1138
1139 ret = test_type_token(type, token, expect, str);
1140
1141 free_token(token);
1142
1143 return ret;
1144}
1145
1146static int read_expected(enum event_type expect, const char *str)
1147{
1148 return __read_expected(expect, str, 1);
1149}
1150
1151static int read_expected_item(enum event_type expect, const char *str)
1152{
1153 return __read_expected(expect, str, 0);
1154}
1155
1156static char *event_read_name(void)
1157{
1158 char *token;
1159
1160 if (read_expected(EVENT_ITEM, "name") < 0)
1161 return NULL;
1162
1163 if (read_expected(EVENT_OP, ":") < 0)
1164 return NULL;
1165
1166 if (read_expect_type(EVENT_ITEM, &token) < 0)
1167 goto fail;
1168
1169 return token;
1170
1171 fail:
1172 free_token(token);
1173 return NULL;
1174}
1175
1176static int event_read_id(void)
1177{
1178 char *token;
1179 int id;
1180
1181 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1182 return -1;
1183
1184 if (read_expected(EVENT_OP, ":") < 0)
1185 return -1;
1186
1187 if (read_expect_type(EVENT_ITEM, &token) < 0)
1188 goto fail;
1189
1190 id = strtoul(token, NULL, 0);
1191 free_token(token);
1192 return id;
1193
1194 fail:
1195 free_token(token);
1196 return -1;
1197}
1198
1199static int field_is_string(struct format_field *field)
1200{
1201 if ((field->flags & FIELD_IS_ARRAY) &&
1202 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1203 strstr(field->type, "s8")))
1204 return 1;
1205
1206 return 0;
1207}
1208
1209static int field_is_dynamic(struct format_field *field)
1210{
1211 if (strncmp(field->type, "__data_loc", 10) == 0)
1212 return 1;
1213
1214 return 0;
1215}
1216
1217static int field_is_long(struct format_field *field)
1218{
1219 /* includes long long */
1220 if (strstr(field->type, "long"))
1221 return 1;
1222
1223 return 0;
1224}
1225
1226static int event_read_fields(struct event_format *event, struct format_field **fields)
1227{
1228 struct format_field *field = NULL;
1229 enum event_type type;
1230 char *token;
1231 char *last_token;
1232 int count = 0;
1233
1234 do {
1235 type = read_token(&token);
1236 if (type == EVENT_NEWLINE) {
1237 free_token(token);
1238 return count;
1239 }
1240
1241 count++;
1242
1243 if (test_type_token(type, token, EVENT_ITEM, "field"))
1244 goto fail;
1245 free_token(token);
1246
1247 type = read_token(&token);
1248 /*
1249 * The ftrace fields may still use the "special" name.
1250 * Just ignore it.
1251 */
1252 if (event->flags & EVENT_FL_ISFTRACE &&
1253 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1254 free_token(token);
1255 type = read_token(&token);
1256 }
1257
1258 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1259 goto fail;
1260
1261 free_token(token);
1262 if (read_expect_type(EVENT_ITEM, &token) < 0)
1263 goto fail;
1264
1265 last_token = token;
1266
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001267 field = calloc(1, sizeof(*field));
1268 if (!field)
1269 goto fail;
1270
Steven Rostedtf7d82352012-04-06 00:47:53 +02001271 field->event = event;
1272
1273 /* read the rest of the type */
1274 for (;;) {
1275 type = read_token(&token);
1276 if (type == EVENT_ITEM ||
1277 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1278 /*
1279 * Some of the ftrace fields are broken and have
1280 * an illegal "." in them.
1281 */
1282 (event->flags & EVENT_FL_ISFTRACE &&
1283 type == EVENT_OP && strcmp(token, ".") == 0)) {
1284
1285 if (strcmp(token, "*") == 0)
1286 field->flags |= FIELD_IS_POINTER;
1287
1288 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001289 char *new_type;
1290 new_type = realloc(field->type,
1291 strlen(field->type) +
1292 strlen(last_token) + 2);
1293 if (!new_type) {
1294 free(last_token);
1295 goto fail;
1296 }
1297 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001298 strcat(field->type, " ");
1299 strcat(field->type, last_token);
1300 free(last_token);
1301 } else
1302 field->type = last_token;
1303 last_token = token;
1304 continue;
1305 }
1306
1307 break;
1308 }
1309
1310 if (!field->type) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001311 do_warning("%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001312 goto fail;
1313 }
1314 field->name = last_token;
1315
1316 if (test_type(type, EVENT_OP))
1317 goto fail;
1318
1319 if (strcmp(token, "[") == 0) {
1320 enum event_type last_type = type;
1321 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001322 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001323 int len;
1324
1325 field->flags |= FIELD_IS_ARRAY;
1326
1327 type = read_token(&token);
1328
1329 if (type == EVENT_ITEM)
1330 field->arraylen = strtoul(token, NULL, 0);
1331 else
1332 field->arraylen = 0;
1333
1334 while (strcmp(token, "]") != 0) {
1335 if (last_type == EVENT_ITEM &&
1336 type == EVENT_ITEM)
1337 len = 2;
1338 else
1339 len = 1;
1340 last_type = type;
1341
Namhyung Kimd2864472012-04-09 11:54:33 +09001342 new_brackets = realloc(brackets,
1343 strlen(brackets) +
1344 strlen(token) + len);
1345 if (!new_brackets) {
1346 free(brackets);
1347 goto fail;
1348 }
1349 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001350 if (len == 2)
1351 strcat(brackets, " ");
1352 strcat(brackets, token);
1353 /* We only care about the last token */
1354 field->arraylen = strtoul(token, NULL, 0);
1355 free_token(token);
1356 type = read_token(&token);
1357 if (type == EVENT_NONE) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001358 do_warning("failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001359 goto fail;
1360 }
1361 }
1362
1363 free_token(token);
1364
Namhyung Kimd2864472012-04-09 11:54:33 +09001365 new_brackets = realloc(brackets, strlen(brackets) + 2);
1366 if (!new_brackets) {
1367 free(brackets);
1368 goto fail;
1369 }
1370 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001371 strcat(brackets, "]");
1372
1373 /* add brackets to type */
1374
1375 type = read_token(&token);
1376 /*
1377 * If the next token is not an OP, then it is of
1378 * the format: type [] item;
1379 */
1380 if (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001381 char *new_type;
1382 new_type = realloc(field->type,
1383 strlen(field->type) +
1384 strlen(field->name) +
1385 strlen(brackets) + 2);
1386 if (!new_type) {
1387 free(brackets);
1388 goto fail;
1389 }
1390 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001391 strcat(field->type, " ");
1392 strcat(field->type, field->name);
1393 free_token(field->name);
1394 strcat(field->type, brackets);
1395 field->name = token;
1396 type = read_token(&token);
1397 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001398 char *new_type;
1399 new_type = realloc(field->type,
1400 strlen(field->type) +
1401 strlen(brackets) + 1);
1402 if (!new_type) {
1403 free(brackets);
1404 goto fail;
1405 }
1406 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001407 strcat(field->type, brackets);
1408 }
1409 free(brackets);
1410 }
1411
1412 if (field_is_string(field))
1413 field->flags |= FIELD_IS_STRING;
1414 if (field_is_dynamic(field))
1415 field->flags |= FIELD_IS_DYNAMIC;
1416 if (field_is_long(field))
1417 field->flags |= FIELD_IS_LONG;
1418
1419 if (test_type_token(type, token, EVENT_OP, ";"))
1420 goto fail;
1421 free_token(token);
1422
1423 if (read_expected(EVENT_ITEM, "offset") < 0)
1424 goto fail_expect;
1425
1426 if (read_expected(EVENT_OP, ":") < 0)
1427 goto fail_expect;
1428
1429 if (read_expect_type(EVENT_ITEM, &token))
1430 goto fail;
1431 field->offset = strtoul(token, NULL, 0);
1432 free_token(token);
1433
1434 if (read_expected(EVENT_OP, ";") < 0)
1435 goto fail_expect;
1436
1437 if (read_expected(EVENT_ITEM, "size") < 0)
1438 goto fail_expect;
1439
1440 if (read_expected(EVENT_OP, ":") < 0)
1441 goto fail_expect;
1442
1443 if (read_expect_type(EVENT_ITEM, &token))
1444 goto fail;
1445 field->size = strtoul(token, NULL, 0);
1446 free_token(token);
1447
1448 if (read_expected(EVENT_OP, ";") < 0)
1449 goto fail_expect;
1450
1451 type = read_token(&token);
1452 if (type != EVENT_NEWLINE) {
1453 /* newer versions of the kernel have a "signed" type */
1454 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1455 goto fail;
1456
1457 free_token(token);
1458
1459 if (read_expected(EVENT_OP, ":") < 0)
1460 goto fail_expect;
1461
1462 if (read_expect_type(EVENT_ITEM, &token))
1463 goto fail;
1464
1465 /* add signed type */
1466
1467 free_token(token);
1468 if (read_expected(EVENT_OP, ";") < 0)
1469 goto fail_expect;
1470
1471 if (read_expect_type(EVENT_NEWLINE, &token))
1472 goto fail;
1473 }
1474
1475 free_token(token);
1476
1477 if (field->flags & FIELD_IS_ARRAY) {
1478 if (field->arraylen)
1479 field->elementsize = field->size / field->arraylen;
1480 else if (field->flags & FIELD_IS_STRING)
1481 field->elementsize = 1;
1482 else
1483 field->elementsize = event->pevent->long_size;
1484 } else
1485 field->elementsize = field->size;
1486
1487 *fields = field;
1488 fields = &field->next;
1489
1490 } while (1);
1491
1492 return 0;
1493
1494fail:
1495 free_token(token);
1496fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001497 if (field) {
1498 free(field->type);
1499 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001500 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001501 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001502 return -1;
1503}
1504
1505static int event_read_format(struct event_format *event)
1506{
1507 char *token;
1508 int ret;
1509
1510 if (read_expected_item(EVENT_ITEM, "format") < 0)
1511 return -1;
1512
1513 if (read_expected(EVENT_OP, ":") < 0)
1514 return -1;
1515
1516 if (read_expect_type(EVENT_NEWLINE, &token))
1517 goto fail;
1518 free_token(token);
1519
1520 ret = event_read_fields(event, &event->format.common_fields);
1521 if (ret < 0)
1522 return ret;
1523 event->format.nr_common = ret;
1524
1525 ret = event_read_fields(event, &event->format.fields);
1526 if (ret < 0)
1527 return ret;
1528 event->format.nr_fields = ret;
1529
1530 return 0;
1531
1532 fail:
1533 free_token(token);
1534 return -1;
1535}
1536
1537static enum event_type
1538process_arg_token(struct event_format *event, struct print_arg *arg,
1539 char **tok, enum event_type type);
1540
1541static enum event_type
1542process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1543{
1544 enum event_type type;
1545 char *token;
1546
1547 type = read_token(&token);
1548 *tok = token;
1549
1550 return process_arg_token(event, arg, tok, type);
1551}
1552
1553static enum event_type
1554process_op(struct event_format *event, struct print_arg *arg, char **tok);
1555
1556static enum event_type
1557process_cond(struct event_format *event, struct print_arg *top, char **tok)
1558{
1559 struct print_arg *arg, *left, *right;
1560 enum event_type type;
1561 char *token = NULL;
1562
1563 arg = alloc_arg();
1564 left = alloc_arg();
1565 right = alloc_arg();
1566
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001567 if (!arg || !left || !right) {
1568 do_warning("%s: not enough memory!", __func__);
1569 /* arg will be freed at out_free */
1570 free_arg(left);
1571 free_arg(right);
1572 goto out_free;
1573 }
1574
Steven Rostedtf7d82352012-04-06 00:47:53 +02001575 arg->type = PRINT_OP;
1576 arg->op.left = left;
1577 arg->op.right = right;
1578
1579 *tok = NULL;
1580 type = process_arg(event, left, &token);
1581
1582 again:
1583 /* Handle other operations in the arguments */
1584 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1585 type = process_op(event, left, &token);
1586 goto again;
1587 }
1588
1589 if (test_type_token(type, token, EVENT_OP, ":"))
1590 goto out_free;
1591
1592 arg->op.op = token;
1593
1594 type = process_arg(event, right, &token);
1595
1596 top->op.right = arg;
1597
1598 *tok = token;
1599 return type;
1600
1601out_free:
1602 /* Top may point to itself */
1603 top->op.right = NULL;
1604 free_token(token);
1605 free_arg(arg);
1606 return EVENT_ERROR;
1607}
1608
1609static enum event_type
1610process_array(struct event_format *event, struct print_arg *top, char **tok)
1611{
1612 struct print_arg *arg;
1613 enum event_type type;
1614 char *token = NULL;
1615
1616 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001617 if (!arg) {
1618 do_warning("%s: not enough memory!", __func__);
1619 /* '*tok' is set to top->op.op. No need to free. */
1620 *tok = NULL;
1621 return EVENT_ERROR;
1622 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001623
1624 *tok = NULL;
1625 type = process_arg(event, arg, &token);
1626 if (test_type_token(type, token, EVENT_OP, "]"))
1627 goto out_free;
1628
1629 top->op.right = arg;
1630
1631 free_token(token);
1632 type = read_token_item(&token);
1633 *tok = token;
1634
1635 return type;
1636
1637out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001638 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001639 free_arg(arg);
1640 return EVENT_ERROR;
1641}
1642
1643static int get_op_prio(char *op)
1644{
1645 if (!op[1]) {
1646 switch (op[0]) {
1647 case '~':
1648 case '!':
1649 return 4;
1650 case '*':
1651 case '/':
1652 case '%':
1653 return 6;
1654 case '+':
1655 case '-':
1656 return 7;
1657 /* '>>' and '<<' are 8 */
1658 case '<':
1659 case '>':
1660 return 9;
1661 /* '==' and '!=' are 10 */
1662 case '&':
1663 return 11;
1664 case '^':
1665 return 12;
1666 case '|':
1667 return 13;
1668 case '?':
1669 return 16;
1670 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001671 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001672 return -1;
1673 }
1674 } else {
1675 if (strcmp(op, "++") == 0 ||
1676 strcmp(op, "--") == 0) {
1677 return 3;
1678 } else if (strcmp(op, ">>") == 0 ||
1679 strcmp(op, "<<") == 0) {
1680 return 8;
1681 } else if (strcmp(op, ">=") == 0 ||
1682 strcmp(op, "<=") == 0) {
1683 return 9;
1684 } else if (strcmp(op, "==") == 0 ||
1685 strcmp(op, "!=") == 0) {
1686 return 10;
1687 } else if (strcmp(op, "&&") == 0) {
1688 return 14;
1689 } else if (strcmp(op, "||") == 0) {
1690 return 15;
1691 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001692 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001693 return -1;
1694 }
1695 }
1696}
1697
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001698static int set_op_prio(struct print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001699{
1700
1701 /* single ops are the greatest */
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001702 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001703 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001704 else
1705 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001706
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001707 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001708}
1709
1710/* Note, *tok does not get freed, but will most likely be saved */
1711static enum event_type
1712process_op(struct event_format *event, struct print_arg *arg, char **tok)
1713{
1714 struct print_arg *left, *right = NULL;
1715 enum event_type type;
1716 char *token;
1717
1718 /* the op is passed in via tok */
1719 token = *tok;
1720
1721 if (arg->type == PRINT_OP && !arg->op.left) {
1722 /* handle single op */
1723 if (token[1]) {
Arnaldo Carvalho de Melob8511922012-09-12 15:39:59 -03001724 do_warning("bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001725 goto out_free;
1726 }
1727 switch (token[0]) {
1728 case '~':
1729 case '!':
1730 case '+':
1731 case '-':
1732 break;
1733 default:
1734 do_warning("bad op token %s", token);
1735 goto out_free;
1736
1737 }
1738
1739 /* make an empty left */
1740 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001741 if (!left)
1742 goto out_warn_free;
1743
Steven Rostedtf7d82352012-04-06 00:47:53 +02001744 left->type = PRINT_NULL;
1745 arg->op.left = left;
1746
1747 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001748 if (!right)
1749 goto out_warn_free;
1750
Steven Rostedtf7d82352012-04-06 00:47:53 +02001751 arg->op.right = right;
1752
1753 /* do not free the token, it belongs to an op */
1754 *tok = NULL;
1755 type = process_arg(event, right, tok);
1756
1757 } else if (strcmp(token, "?") == 0) {
1758
1759 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001760 if (!left)
1761 goto out_warn_free;
1762
Steven Rostedtf7d82352012-04-06 00:47:53 +02001763 /* copy the top arg to the left */
1764 *left = *arg;
1765
1766 arg->type = PRINT_OP;
1767 arg->op.op = token;
1768 arg->op.left = left;
1769 arg->op.prio = 0;
1770
Namhyung Kim41e51a22012-09-19 15:58:42 +09001771 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001772 type = process_cond(event, arg, tok);
1773
1774 } else if (strcmp(token, ">>") == 0 ||
1775 strcmp(token, "<<") == 0 ||
1776 strcmp(token, "&") == 0 ||
1777 strcmp(token, "|") == 0 ||
1778 strcmp(token, "&&") == 0 ||
1779 strcmp(token, "||") == 0 ||
1780 strcmp(token, "-") == 0 ||
1781 strcmp(token, "+") == 0 ||
1782 strcmp(token, "*") == 0 ||
1783 strcmp(token, "^") == 0 ||
1784 strcmp(token, "/") == 0 ||
1785 strcmp(token, "<") == 0 ||
1786 strcmp(token, ">") == 0 ||
1787 strcmp(token, "==") == 0 ||
1788 strcmp(token, "!=") == 0) {
1789
1790 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001791 if (!left)
1792 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001793
1794 /* copy the top arg to the left */
1795 *left = *arg;
1796
1797 arg->type = PRINT_OP;
1798 arg->op.op = token;
1799 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09001800 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001801
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001802 if (set_op_prio(arg) == -1) {
1803 event->flags |= EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09001804 /* arg->op.op (= token) will be freed at out_free */
1805 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001806 goto out_free;
1807 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001808
1809 type = read_token_item(&token);
1810 *tok = token;
1811
1812 /* could just be a type pointer */
1813 if ((strcmp(arg->op.op, "*") == 0) &&
1814 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001815 char *new_atom;
1816
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001817 if (left->type != PRINT_ATOM) {
1818 do_warning("bad pointer type");
1819 goto out_free;
1820 }
Namhyung Kimd2864472012-04-09 11:54:33 +09001821 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001822 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09001823 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001824 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09001825
1826 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001827 strcat(left->atom.atom, " *");
1828 free(arg->op.op);
1829 *arg = *left;
1830 free(left);
1831
1832 return type;
1833 }
1834
1835 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001836 if (!right)
1837 goto out_warn_free;
1838
Steven Rostedtf7d82352012-04-06 00:47:53 +02001839 type = process_arg_token(event, right, tok, type);
1840 arg->op.right = right;
1841
1842 } else if (strcmp(token, "[") == 0) {
1843
1844 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001845 if (!left)
1846 goto out_warn_free;
1847
Steven Rostedtf7d82352012-04-06 00:47:53 +02001848 *left = *arg;
1849
1850 arg->type = PRINT_OP;
1851 arg->op.op = token;
1852 arg->op.left = left;
1853
1854 arg->op.prio = 0;
1855
Namhyung Kim41e51a22012-09-19 15:58:42 +09001856 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001857 type = process_array(event, arg, tok);
1858
1859 } else {
1860 do_warning("unknown op '%s'", token);
1861 event->flags |= EVENT_FL_FAILED;
1862 /* the arg is now the left side */
1863 goto out_free;
1864 }
1865
1866 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1867 int prio;
1868
1869 /* higher prios need to be closer to the root */
1870 prio = get_op_prio(*tok);
1871
1872 if (prio > arg->op.prio)
1873 return process_op(event, arg, tok);
1874
1875 return process_op(event, right, tok);
1876 }
1877
1878 return type;
1879
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001880out_warn_free:
1881 do_warning("%s: not enough memory!", __func__);
1882out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001883 free_token(token);
1884 *tok = NULL;
1885 return EVENT_ERROR;
1886}
1887
1888static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001889process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001890 char **tok)
1891{
1892 enum event_type type;
1893 char *field;
1894 char *token;
1895
1896 if (read_expected(EVENT_OP, "->") < 0)
1897 goto out_err;
1898
1899 if (read_expect_type(EVENT_ITEM, &token) < 0)
1900 goto out_free;
1901 field = token;
1902
1903 arg->type = PRINT_FIELD;
1904 arg->field.name = field;
1905
Tom Zanussi5205aec2012-04-06 00:47:58 +02001906 if (is_flag_field) {
1907 arg->field.field = pevent_find_any_field(event, arg->field.name);
1908 arg->field.field->flags |= FIELD_IS_FLAG;
1909 is_flag_field = 0;
1910 } else if (is_symbolic_field) {
1911 arg->field.field = pevent_find_any_field(event, arg->field.name);
1912 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1913 is_symbolic_field = 0;
1914 }
1915
Steven Rostedtf7d82352012-04-06 00:47:53 +02001916 type = read_token(&token);
1917 *tok = token;
1918
1919 return type;
1920
1921 out_free:
1922 free_token(token);
1923 out_err:
1924 *tok = NULL;
1925 return EVENT_ERROR;
1926}
1927
1928static char *arg_eval (struct print_arg *arg);
1929
1930static unsigned long long
1931eval_type_str(unsigned long long val, const char *type, int pointer)
1932{
1933 int sign = 0;
1934 char *ref;
1935 int len;
1936
1937 len = strlen(type);
1938
1939 if (pointer) {
1940
1941 if (type[len-1] != '*') {
1942 do_warning("pointer expected with non pointer type");
1943 return val;
1944 }
1945
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03001946 ref = malloc(len);
1947 if (!ref) {
1948 do_warning("%s: not enough memory!", __func__);
1949 return val;
1950 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001951 memcpy(ref, type, len);
1952
1953 /* chop off the " *" */
1954 ref[len - 2] = 0;
1955
1956 val = eval_type_str(val, ref, 0);
1957 free(ref);
1958 return val;
1959 }
1960
1961 /* check if this is a pointer */
1962 if (type[len - 1] == '*')
1963 return val;
1964
1965 /* Try to figure out the arg size*/
1966 if (strncmp(type, "struct", 6) == 0)
1967 /* all bets off */
1968 return val;
1969
1970 if (strcmp(type, "u8") == 0)
1971 return val & 0xff;
1972
1973 if (strcmp(type, "u16") == 0)
1974 return val & 0xffff;
1975
1976 if (strcmp(type, "u32") == 0)
1977 return val & 0xffffffff;
1978
1979 if (strcmp(type, "u64") == 0 ||
1980 strcmp(type, "s64"))
1981 return val;
1982
1983 if (strcmp(type, "s8") == 0)
1984 return (unsigned long long)(char)val & 0xff;
1985
1986 if (strcmp(type, "s16") == 0)
1987 return (unsigned long long)(short)val & 0xffff;
1988
1989 if (strcmp(type, "s32") == 0)
1990 return (unsigned long long)(int)val & 0xffffffff;
1991
1992 if (strncmp(type, "unsigned ", 9) == 0) {
1993 sign = 0;
1994 type += 9;
1995 }
1996
1997 if (strcmp(type, "char") == 0) {
1998 if (sign)
1999 return (unsigned long long)(char)val & 0xff;
2000 else
2001 return val & 0xff;
2002 }
2003
2004 if (strcmp(type, "short") == 0) {
2005 if (sign)
2006 return (unsigned long long)(short)val & 0xffff;
2007 else
2008 return val & 0xffff;
2009 }
2010
2011 if (strcmp(type, "int") == 0) {
2012 if (sign)
2013 return (unsigned long long)(int)val & 0xffffffff;
2014 else
2015 return val & 0xffffffff;
2016 }
2017
2018 return val;
2019}
2020
2021/*
2022 * Try to figure out the type.
2023 */
2024static unsigned long long
2025eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2026{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002027 if (arg->type != PRINT_TYPE) {
2028 do_warning("expected type argument");
2029 return 0;
2030 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002031
2032 return eval_type_str(val, arg->typecast.type, pointer);
2033}
2034
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002035static int arg_num_eval(struct print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002036{
2037 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002038 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002039
2040 switch (arg->type) {
2041 case PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002042 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002043 break;
2044 case PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002045 ret = arg_num_eval(arg->typecast.item, val);
2046 if (!ret)
2047 break;
2048 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002049 break;
2050 case PRINT_OP:
2051 switch (arg->op.op[0]) {
2052 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002053 ret = arg_num_eval(arg->op.left, &left);
2054 if (!ret)
2055 break;
2056 ret = arg_num_eval(arg->op.right, &right);
2057 if (!ret)
2058 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002059 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002060 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002061 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002062 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002063 break;
2064 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002065 ret = arg_num_eval(arg->op.left, &left);
2066 if (!ret)
2067 break;
2068 ret = arg_num_eval(arg->op.right, &right);
2069 if (!ret)
2070 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002071 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002072 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002073 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002074 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002075 break;
2076 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002077 ret = arg_num_eval(arg->op.left, &left);
2078 if (!ret)
2079 break;
2080 ret = arg_num_eval(arg->op.right, &right);
2081 if (!ret)
2082 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002083 switch (arg->op.op[1]) {
2084 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002085 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002086 break;
2087 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002088 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002089 break;
2090 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002091 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002092 break;
2093 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002094 do_warning("unknown op '%s'", arg->op.op);
2095 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002096 }
2097 break;
2098 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002099 ret = arg_num_eval(arg->op.left, &left);
2100 if (!ret)
2101 break;
2102 ret = arg_num_eval(arg->op.right, &right);
2103 if (!ret)
2104 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002105 switch (arg->op.op[1]) {
2106 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002107 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002108 break;
2109 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002110 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002111 break;
2112 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002113 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002114 break;
2115 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002116 do_warning("unknown op '%s'", arg->op.op);
2117 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002118 }
2119 break;
2120 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002121 ret = arg_num_eval(arg->op.left, &left);
2122 if (!ret)
2123 break;
2124 ret = arg_num_eval(arg->op.right, &right);
2125 if (!ret)
2126 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002127
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002128 if (arg->op.op[1] != '=') {
2129 do_warning("unknown op '%s'", arg->op.op);
2130 ret = 0;
2131 } else
2132 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002133 break;
2134 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002135 ret = arg_num_eval(arg->op.left, &left);
2136 if (!ret)
2137 break;
2138 ret = arg_num_eval(arg->op.right, &right);
2139 if (!ret)
2140 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002141
2142 switch (arg->op.op[1]) {
2143 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002144 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002145 break;
2146 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002147 do_warning("unknown op '%s'", arg->op.op);
2148 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002149 }
2150 break;
2151 case '-':
2152 /* check for negative */
2153 if (arg->op.left->type == PRINT_NULL)
2154 left = 0;
2155 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002156 ret = arg_num_eval(arg->op.left, &left);
2157 if (!ret)
2158 break;
2159 ret = arg_num_eval(arg->op.right, &right);
2160 if (!ret)
2161 break;
2162 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002163 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002164 case '+':
2165 if (arg->op.left->type == PRINT_NULL)
2166 left = 0;
2167 else
2168 ret = arg_num_eval(arg->op.left, &left);
2169 if (!ret)
2170 break;
2171 ret = arg_num_eval(arg->op.right, &right);
2172 if (!ret)
2173 break;
2174 *val = left + right;
2175 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002176 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002177 do_warning("unknown op '%s'", arg->op.op);
2178 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002179 }
2180 break;
2181
2182 case PRINT_NULL:
2183 case PRINT_FIELD ... PRINT_SYMBOL:
2184 case PRINT_STRING:
2185 case PRINT_BSTRING:
2186 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002187 do_warning("invalid eval type %d", arg->type);
2188 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002189
2190 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002191 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002192}
2193
2194static char *arg_eval (struct print_arg *arg)
2195{
2196 long long val;
2197 static char buf[20];
2198
2199 switch (arg->type) {
2200 case PRINT_ATOM:
2201 return arg->atom.atom;
2202 case PRINT_TYPE:
2203 return arg_eval(arg->typecast.item);
2204 case PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002205 if (!arg_num_eval(arg, &val))
2206 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002207 sprintf(buf, "%lld", val);
2208 return buf;
2209
2210 case PRINT_NULL:
2211 case PRINT_FIELD ... PRINT_SYMBOL:
2212 case PRINT_STRING:
2213 case PRINT_BSTRING:
2214 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002215 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002216 break;
2217 }
2218
2219 return NULL;
2220}
2221
2222static enum event_type
2223process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2224{
2225 enum event_type type;
2226 struct print_arg *arg = NULL;
2227 struct print_flag_sym *field;
2228 char *token = *tok;
2229 char *value;
2230
2231 do {
2232 free_token(token);
2233 type = read_token_item(&token);
2234 if (test_type_token(type, token, EVENT_OP, "{"))
2235 break;
2236
2237 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002238 if (!arg)
2239 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002240
2241 free_token(token);
2242 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002243
2244 if (type == EVENT_OP)
2245 type = process_op(event, arg, &token);
2246
2247 if (type == EVENT_ERROR)
2248 goto out_free;
2249
Steven Rostedtf7d82352012-04-06 00:47:53 +02002250 if (test_type_token(type, token, EVENT_DELIM, ","))
2251 goto out_free;
2252
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002253 field = calloc(1, sizeof(*field));
2254 if (!field)
2255 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002256
2257 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002258 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002259 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002260 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002261 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002262 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002263
2264 free_arg(arg);
2265 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002266 if (!arg)
2267 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002268
2269 free_token(token);
2270 type = process_arg(event, arg, &token);
2271 if (test_type_token(type, token, EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002272 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002273
2274 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002275 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002276 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002277 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002278 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002279 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002280 free_arg(arg);
2281 arg = NULL;
2282
2283 *list = field;
2284 list = &field->next;
2285
2286 free_token(token);
2287 type = read_token_item(&token);
2288 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2289
2290 *tok = token;
2291 return type;
2292
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002293out_free_field:
2294 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002295out_free:
2296 free_arg(arg);
2297 free_token(token);
2298 *tok = NULL;
2299
2300 return EVENT_ERROR;
2301}
2302
2303static enum event_type
2304process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2305{
2306 struct print_arg *field;
2307 enum event_type type;
2308 char *token;
2309
2310 memset(arg, 0, sizeof(*arg));
2311 arg->type = PRINT_FLAGS;
2312
2313 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002314 if (!field) {
2315 do_warning("%s: not enough memory!", __func__);
2316 goto out_free;
2317 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002318
2319 type = process_arg(event, field, &token);
2320
2321 /* Handle operations in the first argument */
2322 while (type == EVENT_OP)
2323 type = process_op(event, field, &token);
2324
2325 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002326 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002327 free_token(token);
2328
2329 arg->flags.field = field;
2330
2331 type = read_token_item(&token);
2332 if (event_item_type(type)) {
2333 arg->flags.delim = token;
2334 type = read_token_item(&token);
2335 }
2336
2337 if (test_type_token(type, token, EVENT_DELIM, ","))
2338 goto out_free;
2339
2340 type = process_fields(event, &arg->flags.flags, &token);
2341 if (test_type_token(type, token, EVENT_DELIM, ")"))
2342 goto out_free;
2343
2344 free_token(token);
2345 type = read_token_item(tok);
2346 return type;
2347
Namhyung Kim70d93042012-09-19 15:58:44 +09002348out_free_field:
2349 free_arg(field);
2350out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002351 free_token(token);
2352 *tok = NULL;
2353 return EVENT_ERROR;
2354}
2355
2356static enum event_type
2357process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2358{
2359 struct print_arg *field;
2360 enum event_type type;
2361 char *token;
2362
2363 memset(arg, 0, sizeof(*arg));
2364 arg->type = PRINT_SYMBOL;
2365
2366 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002367 if (!field) {
2368 do_warning("%s: not enough memory!", __func__);
2369 goto out_free;
2370 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002371
2372 type = process_arg(event, field, &token);
2373 if (test_type_token(type, token, EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002374 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002375
2376 arg->symbol.field = field;
2377
2378 type = process_fields(event, &arg->symbol.symbols, &token);
2379 if (test_type_token(type, token, EVENT_DELIM, ")"))
2380 goto out_free;
2381
2382 free_token(token);
2383 type = read_token_item(tok);
2384 return type;
2385
Namhyung Kim70d93042012-09-19 15:58:44 +09002386out_free_field:
2387 free_arg(field);
2388out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002389 free_token(token);
2390 *tok = NULL;
2391 return EVENT_ERROR;
2392}
2393
2394static enum event_type
Namhyung Kime080e6f2012-06-27 09:41:41 +09002395process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2396{
2397 struct print_arg *field;
2398 enum event_type type;
2399 char *token;
2400
2401 memset(arg, 0, sizeof(*arg));
2402 arg->type = PRINT_HEX;
2403
2404 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002405 if (!field) {
2406 do_warning("%s: not enough memory!", __func__);
2407 goto out_free;
2408 }
2409
Namhyung Kime080e6f2012-06-27 09:41:41 +09002410 type = process_arg(event, field, &token);
2411
2412 if (test_type_token(type, token, EVENT_DELIM, ","))
2413 goto out_free;
2414
2415 arg->hex.field = field;
2416
2417 free_token(token);
2418
2419 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002420 if (!field) {
2421 do_warning("%s: not enough memory!", __func__);
2422 *tok = NULL;
2423 return EVENT_ERROR;
2424 }
2425
Namhyung Kime080e6f2012-06-27 09:41:41 +09002426 type = process_arg(event, field, &token);
2427
2428 if (test_type_token(type, token, EVENT_DELIM, ")"))
2429 goto out_free;
2430
2431 arg->hex.size = field;
2432
2433 free_token(token);
2434 type = read_token_item(tok);
2435 return type;
2436
2437 out_free:
2438 free_arg(field);
2439 free_token(token);
2440 *tok = NULL;
2441 return EVENT_ERROR;
2442}
2443
2444static enum event_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02002445process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2446{
2447 struct format_field *field;
2448 enum event_type type;
2449 char *token;
2450
2451 memset(arg, 0, sizeof(*arg));
2452 arg->type = PRINT_DYNAMIC_ARRAY;
2453
2454 /*
2455 * The item within the parenthesis is another field that holds
2456 * the index into where the array starts.
2457 */
2458 type = read_token(&token);
2459 *tok = token;
2460 if (type != EVENT_ITEM)
2461 goto out_free;
2462
2463 /* Find the field */
2464
2465 field = pevent_find_field(event, token);
2466 if (!field)
2467 goto out_free;
2468
2469 arg->dynarray.field = field;
2470 arg->dynarray.index = 0;
2471
2472 if (read_expected(EVENT_DELIM, ")") < 0)
2473 goto out_free;
2474
2475 free_token(token);
2476 type = read_token_item(&token);
2477 *tok = token;
2478 if (type != EVENT_OP || strcmp(token, "[") != 0)
2479 return type;
2480
2481 free_token(token);
2482 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002483 if (!arg) {
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002484 do_warning("%s: not enough memory!", __func__);
2485 *tok = NULL;
2486 return EVENT_ERROR;
2487 }
2488
Steven Rostedtf7d82352012-04-06 00:47:53 +02002489 type = process_arg(event, arg, &token);
2490 if (type == EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002491 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002492
2493 if (!test_type_token(type, token, EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002494 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002495
2496 free_token(token);
2497 type = read_token_item(tok);
2498 return type;
2499
Namhyung Kimb3511d02012-05-23 11:36:50 +09002500 out_free_arg:
2501 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002502 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002503 free_token(token);
2504 *tok = NULL;
2505 return EVENT_ERROR;
2506}
2507
2508static enum event_type
2509process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2510{
2511 struct print_arg *item_arg;
2512 enum event_type type;
2513 char *token;
2514
2515 type = process_arg(event, arg, &token);
2516
2517 if (type == EVENT_ERROR)
2518 goto out_free;
2519
2520 if (type == EVENT_OP)
2521 type = process_op(event, arg, &token);
2522
2523 if (type == EVENT_ERROR)
2524 goto out_free;
2525
2526 if (test_type_token(type, token, EVENT_DELIM, ")"))
2527 goto out_free;
2528
2529 free_token(token);
2530 type = read_token_item(&token);
2531
2532 /*
2533 * If the next token is an item or another open paren, then
2534 * this was a typecast.
2535 */
2536 if (event_item_type(type) ||
2537 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2538
2539 /* make this a typecast and contine */
2540
2541 /* prevous must be an atom */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002542 if (arg->type != PRINT_ATOM) {
2543 do_warning("previous needed to be PRINT_ATOM");
2544 goto out_free;
2545 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002546
2547 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002548 if (!item_arg) {
2549 do_warning("%s: not enough memory!", __func__);
2550 goto out_free;
2551 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002552
2553 arg->type = PRINT_TYPE;
2554 arg->typecast.type = arg->atom.atom;
2555 arg->typecast.item = item_arg;
2556 type = process_arg_token(event, item_arg, &token, type);
2557
2558 }
2559
2560 *tok = token;
2561 return type;
2562
2563 out_free:
2564 free_token(token);
2565 *tok = NULL;
2566 return EVENT_ERROR;
2567}
2568
2569
2570static enum event_type
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002571process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2572 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002573{
2574 enum event_type type;
2575 char *token;
2576
2577 if (read_expect_type(EVENT_ITEM, &token) < 0)
2578 goto out_free;
2579
2580 arg->type = PRINT_STRING;
2581 arg->string.string = token;
2582 arg->string.offset = -1;
2583
2584 if (read_expected(EVENT_DELIM, ")") < 0)
2585 goto out_err;
2586
2587 type = read_token(&token);
2588 *tok = token;
2589
2590 return type;
2591
2592 out_free:
2593 free_token(token);
2594 out_err:
2595 *tok = NULL;
2596 return EVENT_ERROR;
2597}
2598
2599static struct pevent_function_handler *
2600find_func_handler(struct pevent *pevent, char *func_name)
2601{
2602 struct pevent_function_handler *func;
2603
Steven Rostedt101782e2012-10-01 20:13:51 -04002604 if (!pevent)
2605 return NULL;
2606
Steven Rostedtf7d82352012-04-06 00:47:53 +02002607 for (func = pevent->func_handlers; func; func = func->next) {
2608 if (strcmp(func->name, func_name) == 0)
2609 break;
2610 }
2611
2612 return func;
2613}
2614
2615static void remove_func_handler(struct pevent *pevent, char *func_name)
2616{
2617 struct pevent_function_handler *func;
2618 struct pevent_function_handler **next;
2619
2620 next = &pevent->func_handlers;
2621 while ((func = *next)) {
2622 if (strcmp(func->name, func_name) == 0) {
2623 *next = func->next;
2624 free_func_handle(func);
2625 break;
2626 }
2627 next = &func->next;
2628 }
2629}
2630
2631static enum event_type
2632process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2633 struct print_arg *arg, char **tok)
2634{
2635 struct print_arg **next_arg;
2636 struct print_arg *farg;
2637 enum event_type type;
2638 char *token;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03002639 const char *test;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002640 int i;
2641
2642 arg->type = PRINT_FUNC;
2643 arg->func.func = func;
2644
2645 *tok = NULL;
2646
2647 next_arg = &(arg->func.args);
2648 for (i = 0; i < func->nr_args; i++) {
2649 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002650 if (!farg) {
2651 do_warning("%s: not enough memory!", __func__);
2652 return EVENT_ERROR;
2653 }
2654
Steven Rostedtf7d82352012-04-06 00:47:53 +02002655 type = process_arg(event, farg, &token);
2656 if (i < (func->nr_args - 1))
2657 test = ",";
2658 else
2659 test = ")";
2660
2661 if (test_type_token(type, token, EVENT_DELIM, test)) {
2662 free_arg(farg);
2663 free_token(token);
2664 return EVENT_ERROR;
2665 }
2666
2667 *next_arg = farg;
2668 next_arg = &(farg->next);
2669 free_token(token);
2670 }
2671
2672 type = read_token(&token);
2673 *tok = token;
2674
2675 return type;
2676}
2677
2678static enum event_type
2679process_function(struct event_format *event, struct print_arg *arg,
2680 char *token, char **tok)
2681{
2682 struct pevent_function_handler *func;
2683
2684 if (strcmp(token, "__print_flags") == 0) {
2685 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002686 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002687 return process_flags(event, arg, tok);
2688 }
2689 if (strcmp(token, "__print_symbolic") == 0) {
2690 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02002691 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002692 return process_symbols(event, arg, tok);
2693 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09002694 if (strcmp(token, "__print_hex") == 0) {
2695 free_token(token);
2696 return process_hex(event, arg, tok);
2697 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002698 if (strcmp(token, "__get_str") == 0) {
2699 free_token(token);
2700 return process_str(event, arg, tok);
2701 }
2702 if (strcmp(token, "__get_dynamic_array") == 0) {
2703 free_token(token);
2704 return process_dynamic_array(event, arg, tok);
2705 }
2706
2707 func = find_func_handler(event->pevent, token);
2708 if (func) {
2709 free_token(token);
2710 return process_func_handler(event, func, arg, tok);
2711 }
2712
2713 do_warning("function %s not defined", token);
2714 free_token(token);
2715 return EVENT_ERROR;
2716}
2717
2718static enum event_type
2719process_arg_token(struct event_format *event, struct print_arg *arg,
2720 char **tok, enum event_type type)
2721{
2722 char *token;
2723 char *atom;
2724
2725 token = *tok;
2726
2727 switch (type) {
2728 case EVENT_ITEM:
2729 if (strcmp(token, "REC") == 0) {
2730 free_token(token);
2731 type = process_entry(event, arg, &token);
2732 break;
2733 }
2734 atom = token;
2735 /* test the next token */
2736 type = read_token_item(&token);
2737
2738 /*
2739 * If the next token is a parenthesis, then this
2740 * is a function.
2741 */
2742 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2743 free_token(token);
2744 token = NULL;
2745 /* this will free atom. */
2746 type = process_function(event, arg, atom, &token);
2747 break;
2748 }
2749 /* atoms can be more than one token long */
2750 while (type == EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002751 char *new_atom;
2752 new_atom = realloc(atom,
2753 strlen(atom) + strlen(token) + 2);
2754 if (!new_atom) {
2755 free(atom);
2756 *tok = NULL;
2757 free_token(token);
2758 return EVENT_ERROR;
2759 }
2760 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002761 strcat(atom, " ");
2762 strcat(atom, token);
2763 free_token(token);
2764 type = read_token_item(&token);
2765 }
2766
2767 arg->type = PRINT_ATOM;
2768 arg->atom.atom = atom;
2769 break;
2770
2771 case EVENT_DQUOTE:
2772 case EVENT_SQUOTE:
2773 arg->type = PRINT_ATOM;
2774 arg->atom.atom = token;
2775 type = read_token_item(&token);
2776 break;
2777 case EVENT_DELIM:
2778 if (strcmp(token, "(") == 0) {
2779 free_token(token);
2780 type = process_paren(event, arg, &token);
2781 break;
2782 }
2783 case EVENT_OP:
2784 /* handle single ops */
2785 arg->type = PRINT_OP;
2786 arg->op.op = token;
2787 arg->op.left = NULL;
2788 type = process_op(event, arg, &token);
2789
2790 /* On error, the op is freed */
2791 if (type == EVENT_ERROR)
2792 arg->op.op = NULL;
2793
2794 /* return error type if errored */
2795 break;
2796
2797 case EVENT_ERROR ... EVENT_NEWLINE:
2798 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002799 do_warning("unexpected type %d", type);
2800 return EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002801 }
2802 *tok = token;
2803
2804 return type;
2805}
2806
2807static int event_read_print_args(struct event_format *event, struct print_arg **list)
2808{
2809 enum event_type type = EVENT_ERROR;
2810 struct print_arg *arg;
2811 char *token;
2812 int args = 0;
2813
2814 do {
2815 if (type == EVENT_NEWLINE) {
2816 type = read_token_item(&token);
2817 continue;
2818 }
2819
2820 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002821 if (!arg) {
2822 do_warning("%s: not enough memory!", __func__);
2823 return -1;
2824 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002825
2826 type = process_arg(event, arg, &token);
2827
2828 if (type == EVENT_ERROR) {
2829 free_token(token);
2830 free_arg(arg);
2831 return -1;
2832 }
2833
2834 *list = arg;
2835 args++;
2836
2837 if (type == EVENT_OP) {
2838 type = process_op(event, arg, &token);
2839 free_token(token);
2840 if (type == EVENT_ERROR) {
2841 *list = NULL;
2842 free_arg(arg);
2843 return -1;
2844 }
2845 list = &arg->next;
2846 continue;
2847 }
2848
2849 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2850 free_token(token);
2851 *list = arg;
2852 list = &arg->next;
2853 continue;
2854 }
2855 break;
2856 } while (type != EVENT_NONE);
2857
2858 if (type != EVENT_NONE && type != EVENT_ERROR)
2859 free_token(token);
2860
2861 return args;
2862}
2863
2864static int event_read_print(struct event_format *event)
2865{
2866 enum event_type type;
2867 char *token;
2868 int ret;
2869
2870 if (read_expected_item(EVENT_ITEM, "print") < 0)
2871 return -1;
2872
2873 if (read_expected(EVENT_ITEM, "fmt") < 0)
2874 return -1;
2875
2876 if (read_expected(EVENT_OP, ":") < 0)
2877 return -1;
2878
2879 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2880 goto fail;
2881
2882 concat:
2883 event->print_fmt.format = token;
2884 event->print_fmt.args = NULL;
2885
2886 /* ok to have no arg */
2887 type = read_token_item(&token);
2888
2889 if (type == EVENT_NONE)
2890 return 0;
2891
2892 /* Handle concatenation of print lines */
2893 if (type == EVENT_DQUOTE) {
2894 char *cat;
2895
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03002896 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2897 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002898 free_token(token);
2899 free_token(event->print_fmt.format);
2900 event->print_fmt.format = NULL;
2901 token = cat;
2902 goto concat;
2903 }
2904
2905 if (test_type_token(type, token, EVENT_DELIM, ","))
2906 goto fail;
2907
2908 free_token(token);
2909
2910 ret = event_read_print_args(event, &event->print_fmt.args);
2911 if (ret < 0)
2912 return -1;
2913
2914 return ret;
2915
2916 fail:
2917 free_token(token);
2918 return -1;
2919}
2920
2921/**
2922 * pevent_find_common_field - return a common field by event
2923 * @event: handle for the event
2924 * @name: the name of the common field to return
2925 *
2926 * Returns a common field from the event by the given @name.
2927 * This only searchs the common fields and not all field.
2928 */
2929struct format_field *
2930pevent_find_common_field(struct event_format *event, const char *name)
2931{
2932 struct format_field *format;
2933
2934 for (format = event->format.common_fields;
2935 format; format = format->next) {
2936 if (strcmp(format->name, name) == 0)
2937 break;
2938 }
2939
2940 return format;
2941}
2942
2943/**
2944 * pevent_find_field - find a non-common field
2945 * @event: handle for the event
2946 * @name: the name of the non-common field
2947 *
2948 * Returns a non-common field by the given @name.
2949 * This does not search common fields.
2950 */
2951struct format_field *
2952pevent_find_field(struct event_format *event, const char *name)
2953{
2954 struct format_field *format;
2955
2956 for (format = event->format.fields;
2957 format; format = format->next) {
2958 if (strcmp(format->name, name) == 0)
2959 break;
2960 }
2961
2962 return format;
2963}
2964
2965/**
2966 * pevent_find_any_field - find any field by name
2967 * @event: handle for the event
2968 * @name: the name of the field
2969 *
2970 * Returns a field by the given @name.
2971 * This searchs the common field names first, then
2972 * the non-common ones if a common one was not found.
2973 */
2974struct format_field *
2975pevent_find_any_field(struct event_format *event, const char *name)
2976{
2977 struct format_field *format;
2978
2979 format = pevent_find_common_field(event, name);
2980 if (format)
2981 return format;
2982 return pevent_find_field(event, name);
2983}
2984
2985/**
2986 * pevent_read_number - read a number from data
2987 * @pevent: handle for the pevent
2988 * @ptr: the raw data
2989 * @size: the size of the data that holds the number
2990 *
2991 * Returns the number (converted to host) from the
2992 * raw data.
2993 */
2994unsigned long long pevent_read_number(struct pevent *pevent,
2995 const void *ptr, int size)
2996{
2997 switch (size) {
2998 case 1:
2999 return *(unsigned char *)ptr;
3000 case 2:
3001 return data2host2(pevent, ptr);
3002 case 4:
3003 return data2host4(pevent, ptr);
3004 case 8:
3005 return data2host8(pevent, ptr);
3006 default:
3007 /* BUG! */
3008 return 0;
3009 }
3010}
3011
3012/**
3013 * pevent_read_number_field - read a number from data
3014 * @field: a handle to the field
3015 * @data: the raw data to read
3016 * @value: the value to place the number in
3017 *
3018 * Reads raw data according to a field offset and size,
3019 * and translates it into @value.
3020 *
3021 * Returns 0 on success, -1 otherwise.
3022 */
3023int pevent_read_number_field(struct format_field *field, const void *data,
3024 unsigned long long *value)
3025{
3026 if (!field)
3027 return -1;
3028 switch (field->size) {
3029 case 1:
3030 case 2:
3031 case 4:
3032 case 8:
3033 *value = pevent_read_number(field->event->pevent,
3034 data + field->offset, field->size);
3035 return 0;
3036 default:
3037 return -1;
3038 }
3039}
3040
3041static int get_common_info(struct pevent *pevent,
3042 const char *type, int *offset, int *size)
3043{
3044 struct event_format *event;
3045 struct format_field *field;
3046
3047 /*
3048 * All events should have the same common elements.
3049 * Pick any event to find where the type is;
3050 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003051 if (!pevent->events) {
3052 do_warning("no event_list!");
3053 return -1;
3054 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003055
3056 event = pevent->events[0];
3057 field = pevent_find_common_field(event, type);
3058 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003059 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003060
3061 *offset = field->offset;
3062 *size = field->size;
3063
3064 return 0;
3065}
3066
3067static int __parse_common(struct pevent *pevent, void *data,
3068 int *size, int *offset, const char *name)
3069{
3070 int ret;
3071
3072 if (!*size) {
3073 ret = get_common_info(pevent, name, offset, size);
3074 if (ret < 0)
3075 return ret;
3076 }
3077 return pevent_read_number(pevent, data + *offset, *size);
3078}
3079
3080static int trace_parse_common_type(struct pevent *pevent, void *data)
3081{
3082 return __parse_common(pevent, data,
3083 &pevent->type_size, &pevent->type_offset,
3084 "common_type");
3085}
3086
3087static int parse_common_pid(struct pevent *pevent, void *data)
3088{
3089 return __parse_common(pevent, data,
3090 &pevent->pid_size, &pevent->pid_offset,
3091 "common_pid");
3092}
3093
3094static int parse_common_pc(struct pevent *pevent, void *data)
3095{
3096 return __parse_common(pevent, data,
3097 &pevent->pc_size, &pevent->pc_offset,
3098 "common_preempt_count");
3099}
3100
3101static int parse_common_flags(struct pevent *pevent, void *data)
3102{
3103 return __parse_common(pevent, data,
3104 &pevent->flags_size, &pevent->flags_offset,
3105 "common_flags");
3106}
3107
3108static int parse_common_lock_depth(struct pevent *pevent, void *data)
3109{
Steven Rostedt0866a972012-05-22 14:52:40 +09003110 return __parse_common(pevent, data,
3111 &pevent->ld_size, &pevent->ld_offset,
3112 "common_lock_depth");
3113}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003114
Steven Rostedt0866a972012-05-22 14:52:40 +09003115static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3116{
3117 return __parse_common(pevent, data,
3118 &pevent->ld_size, &pevent->ld_offset,
3119 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003120}
3121
3122static int events_id_cmp(const void *a, const void *b);
3123
3124/**
3125 * pevent_find_event - find an event by given id
3126 * @pevent: a handle to the pevent
3127 * @id: the id of the event
3128 *
3129 * Returns an event that has a given @id.
3130 */
3131struct event_format *pevent_find_event(struct pevent *pevent, int id)
3132{
3133 struct event_format **eventptr;
3134 struct event_format key;
3135 struct event_format *pkey = &key;
3136
3137 /* Check cache first */
3138 if (pevent->last_event && pevent->last_event->id == id)
3139 return pevent->last_event;
3140
3141 key.id = id;
3142
3143 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3144 sizeof(*pevent->events), events_id_cmp);
3145
3146 if (eventptr) {
3147 pevent->last_event = *eventptr;
3148 return *eventptr;
3149 }
3150
3151 return NULL;
3152}
3153
3154/**
3155 * pevent_find_event_by_name - find an event by given name
3156 * @pevent: a handle to the pevent
3157 * @sys: the system name to search for
3158 * @name: the name of the event to search for
3159 *
3160 * This returns an event with a given @name and under the system
3161 * @sys. If @sys is NULL the first event with @name is returned.
3162 */
3163struct event_format *
3164pevent_find_event_by_name(struct pevent *pevent,
3165 const char *sys, const char *name)
3166{
3167 struct event_format *event;
3168 int i;
3169
3170 if (pevent->last_event &&
3171 strcmp(pevent->last_event->name, name) == 0 &&
3172 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3173 return pevent->last_event;
3174
3175 for (i = 0; i < pevent->nr_events; i++) {
3176 event = pevent->events[i];
3177 if (strcmp(event->name, name) == 0) {
3178 if (!sys)
3179 break;
3180 if (strcmp(event->system, sys) == 0)
3181 break;
3182 }
3183 }
3184 if (i == pevent->nr_events)
3185 event = NULL;
3186
3187 pevent->last_event = event;
3188 return event;
3189}
3190
3191static unsigned long long
3192eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3193{
3194 struct pevent *pevent = event->pevent;
3195 unsigned long long val = 0;
3196 unsigned long long left, right;
3197 struct print_arg *typearg = NULL;
3198 struct print_arg *larg;
3199 unsigned long offset;
3200 unsigned int field_size;
3201
3202 switch (arg->type) {
3203 case PRINT_NULL:
3204 /* ?? */
3205 return 0;
3206 case PRINT_ATOM:
3207 return strtoull(arg->atom.atom, NULL, 0);
3208 case PRINT_FIELD:
3209 if (!arg->field.field) {
3210 arg->field.field = pevent_find_any_field(event, arg->field.name);
3211 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003212 goto out_warning_field;
3213
Steven Rostedtf7d82352012-04-06 00:47:53 +02003214 }
3215 /* must be a number */
3216 val = pevent_read_number(pevent, data + arg->field.field->offset,
3217 arg->field.field->size);
3218 break;
3219 case PRINT_FLAGS:
3220 case PRINT_SYMBOL:
Namhyung Kime080e6f2012-06-27 09:41:41 +09003221 case PRINT_HEX:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003222 break;
3223 case PRINT_TYPE:
3224 val = eval_num_arg(data, size, event, arg->typecast.item);
3225 return eval_type(val, arg, 0);
3226 case PRINT_STRING:
3227 case PRINT_BSTRING:
3228 return 0;
3229 case PRINT_FUNC: {
3230 struct trace_seq s;
3231 trace_seq_init(&s);
3232 val = process_defined_func(&s, data, size, event, arg);
3233 trace_seq_destroy(&s);
3234 return val;
3235 }
3236 case PRINT_OP:
3237 if (strcmp(arg->op.op, "[") == 0) {
3238 /*
3239 * Arrays are special, since we don't want
3240 * to read the arg as is.
3241 */
3242 right = eval_num_arg(data, size, event, arg->op.right);
3243
3244 /* handle typecasts */
3245 larg = arg->op.left;
3246 while (larg->type == PRINT_TYPE) {
3247 if (!typearg)
3248 typearg = larg;
3249 larg = larg->typecast.item;
3250 }
3251
3252 /* Default to long size */
3253 field_size = pevent->long_size;
3254
3255 switch (larg->type) {
3256 case PRINT_DYNAMIC_ARRAY:
3257 offset = pevent_read_number(pevent,
3258 data + larg->dynarray.field->offset,
3259 larg->dynarray.field->size);
3260 if (larg->dynarray.field->elementsize)
3261 field_size = larg->dynarray.field->elementsize;
3262 /*
3263 * The actual length of the dynamic array is stored
3264 * in the top half of the field, and the offset
3265 * is in the bottom half of the 32 bit field.
3266 */
3267 offset &= 0xffff;
3268 offset += right;
3269 break;
3270 case PRINT_FIELD:
3271 if (!larg->field.field) {
3272 larg->field.field =
3273 pevent_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003274 if (!larg->field.field) {
3275 arg = larg;
3276 goto out_warning_field;
3277 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003278 }
3279 field_size = larg->field.field->elementsize;
3280 offset = larg->field.field->offset +
3281 right * larg->field.field->elementsize;
3282 break;
3283 default:
3284 goto default_op; /* oops, all bets off */
3285 }
3286 val = pevent_read_number(pevent,
3287 data + offset, field_size);
3288 if (typearg)
3289 val = eval_type(val, typearg, 1);
3290 break;
3291 } else if (strcmp(arg->op.op, "?") == 0) {
3292 left = eval_num_arg(data, size, event, arg->op.left);
3293 arg = arg->op.right;
3294 if (left)
3295 val = eval_num_arg(data, size, event, arg->op.left);
3296 else
3297 val = eval_num_arg(data, size, event, arg->op.right);
3298 break;
3299 }
3300 default_op:
3301 left = eval_num_arg(data, size, event, arg->op.left);
3302 right = eval_num_arg(data, size, event, arg->op.right);
3303 switch (arg->op.op[0]) {
3304 case '!':
3305 switch (arg->op.op[1]) {
3306 case 0:
3307 val = !right;
3308 break;
3309 case '=':
3310 val = left != right;
3311 break;
3312 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003313 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003314 }
3315 break;
3316 case '~':
3317 val = ~right;
3318 break;
3319 case '|':
3320 if (arg->op.op[1])
3321 val = left || right;
3322 else
3323 val = left | right;
3324 break;
3325 case '&':
3326 if (arg->op.op[1])
3327 val = left && right;
3328 else
3329 val = left & right;
3330 break;
3331 case '<':
3332 switch (arg->op.op[1]) {
3333 case 0:
3334 val = left < right;
3335 break;
3336 case '<':
3337 val = left << right;
3338 break;
3339 case '=':
3340 val = left <= right;
3341 break;
3342 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003343 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003344 }
3345 break;
3346 case '>':
3347 switch (arg->op.op[1]) {
3348 case 0:
3349 val = left > right;
3350 break;
3351 case '>':
3352 val = left >> right;
3353 break;
3354 case '=':
3355 val = left >= right;
3356 break;
3357 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003358 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003359 }
3360 break;
3361 case '=':
3362 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003363 goto out_warning_op;
3364
Steven Rostedtf7d82352012-04-06 00:47:53 +02003365 val = left == right;
3366 break;
3367 case '-':
3368 val = left - right;
3369 break;
3370 case '+':
3371 val = left + right;
3372 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003373 case '/':
3374 val = left / right;
3375 break;
3376 case '*':
3377 val = left * right;
3378 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003379 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003380 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003381 }
3382 break;
3383 default: /* not sure what to do there */
3384 return 0;
3385 }
3386 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003387
3388out_warning_op:
3389 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3390 return 0;
3391
3392out_warning_field:
3393 do_warning("%s: field %s not found", __func__, arg->field.name);
3394 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003395}
3396
3397struct flag {
3398 const char *name;
3399 unsigned long long value;
3400};
3401
3402static const struct flag flags[] = {
3403 { "HI_SOFTIRQ", 0 },
3404 { "TIMER_SOFTIRQ", 1 },
3405 { "NET_TX_SOFTIRQ", 2 },
3406 { "NET_RX_SOFTIRQ", 3 },
3407 { "BLOCK_SOFTIRQ", 4 },
3408 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3409 { "TASKLET_SOFTIRQ", 6 },
3410 { "SCHED_SOFTIRQ", 7 },
3411 { "HRTIMER_SOFTIRQ", 8 },
3412 { "RCU_SOFTIRQ", 9 },
3413
3414 { "HRTIMER_NORESTART", 0 },
3415 { "HRTIMER_RESTART", 1 },
3416};
3417
3418static unsigned long long eval_flag(const char *flag)
3419{
3420 int i;
3421
3422 /*
3423 * Some flags in the format files do not get converted.
3424 * If the flag is not numeric, see if it is something that
3425 * we already know about.
3426 */
3427 if (isdigit(flag[0]))
3428 return strtoull(flag, NULL, 0);
3429
3430 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3431 if (strcmp(flags[i].name, flag) == 0)
3432 return flags[i].value;
3433
3434 return 0;
3435}
3436
3437static void print_str_to_seq(struct trace_seq *s, const char *format,
3438 int len_arg, const char *str)
3439{
3440 if (len_arg >= 0)
3441 trace_seq_printf(s, format, len_arg, str);
3442 else
3443 trace_seq_printf(s, format, str);
3444}
3445
3446static void print_str_arg(struct trace_seq *s, void *data, int size,
3447 struct event_format *event, const char *format,
3448 int len_arg, struct print_arg *arg)
3449{
3450 struct pevent *pevent = event->pevent;
3451 struct print_flag_sym *flag;
Namhyung Kimb7008072012-06-27 09:41:40 +09003452 struct format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003453 unsigned long long val, fval;
3454 unsigned long addr;
3455 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003456 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003457 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003458 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003459
3460 switch (arg->type) {
3461 case PRINT_NULL:
3462 /* ?? */
3463 return;
3464 case PRINT_ATOM:
3465 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3466 return;
3467 case PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003468 field = arg->field.field;
3469 if (!field) {
3470 field = pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003471 if (!field) {
3472 str = arg->field.name;
3473 goto out_warning_field;
3474 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003475 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003476 }
3477 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003478 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003479
3480 /*
3481 * Some events pass in pointers. If this is not an array
3482 * and the size is the same as long_size, assume that it
3483 * is a pointer.
3484 */
Namhyung Kimb7008072012-06-27 09:41:40 +09003485 if (!(field->flags & FIELD_IS_ARRAY) &&
3486 field->size == pevent->long_size) {
3487 addr = *(unsigned long *)(data + field->offset);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003488 trace_seq_printf(s, "%lx", addr);
3489 break;
3490 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003491 str = malloc(len + 1);
3492 if (!str) {
3493 do_warning("%s: not enough memory!", __func__);
3494 return;
3495 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003496 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003497 str[len] = 0;
3498 print_str_to_seq(s, format, len_arg, str);
3499 free(str);
3500 break;
3501 case PRINT_FLAGS:
3502 val = eval_num_arg(data, size, event, arg->flags.field);
3503 print = 0;
3504 for (flag = arg->flags.flags; flag; flag = flag->next) {
3505 fval = eval_flag(flag->value);
3506 if (!val && !fval) {
3507 print_str_to_seq(s, format, len_arg, flag->str);
3508 break;
3509 }
3510 if (fval && (val & fval) == fval) {
3511 if (print && arg->flags.delim)
3512 trace_seq_puts(s, arg->flags.delim);
3513 print_str_to_seq(s, format, len_arg, flag->str);
3514 print = 1;
3515 val &= ~fval;
3516 }
3517 }
3518 break;
3519 case PRINT_SYMBOL:
3520 val = eval_num_arg(data, size, event, arg->symbol.field);
3521 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3522 fval = eval_flag(flag->value);
3523 if (val == fval) {
3524 print_str_to_seq(s, format, len_arg, flag->str);
3525 break;
3526 }
3527 }
3528 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003529 case PRINT_HEX:
3530 field = arg->hex.field->field.field;
3531 if (!field) {
3532 str = arg->hex.field->field.name;
3533 field = pevent_find_any_field(event, str);
3534 if (!field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003535 goto out_warning_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003536 arg->hex.field->field.field = field;
3537 }
3538 hex = data + field->offset;
3539 len = eval_num_arg(data, size, event, arg->hex.size);
3540 for (i = 0; i < len; i++) {
3541 if (i)
3542 trace_seq_putc(s, ' ');
3543 trace_seq_printf(s, "%02x", hex[i]);
3544 }
3545 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003546
3547 case PRINT_TYPE:
3548 break;
3549 case PRINT_STRING: {
3550 int str_offset;
3551
3552 if (arg->string.offset == -1) {
3553 struct format_field *f;
3554
3555 f = pevent_find_any_field(event, arg->string.string);
3556 arg->string.offset = f->offset;
3557 }
3558 str_offset = data2host4(pevent, data + arg->string.offset);
3559 str_offset &= 0xffff;
3560 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3561 break;
3562 }
3563 case PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003564 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003565 break;
3566 case PRINT_OP:
3567 /*
3568 * The only op for string should be ? :
3569 */
3570 if (arg->op.op[0] != '?')
3571 return;
3572 val = eval_num_arg(data, size, event, arg->op.left);
3573 if (val)
3574 print_str_arg(s, data, size, event,
3575 format, len_arg, arg->op.right->op.left);
3576 else
3577 print_str_arg(s, data, size, event,
3578 format, len_arg, arg->op.right->op.right);
3579 break;
3580 case PRINT_FUNC:
3581 process_defined_func(s, data, size, event, arg);
3582 break;
3583 default:
3584 /* well... */
3585 break;
3586 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003587
3588 return;
3589
3590out_warning_field:
3591 do_warning("%s: field %s not found", __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003592}
3593
3594static unsigned long long
3595process_defined_func(struct trace_seq *s, void *data, int size,
3596 struct event_format *event, struct print_arg *arg)
3597{
3598 struct pevent_function_handler *func_handle = arg->func.func;
3599 struct pevent_func_params *param;
3600 unsigned long long *args;
3601 unsigned long long ret;
3602 struct print_arg *farg;
3603 struct trace_seq str;
3604 struct save_str {
3605 struct save_str *next;
3606 char *str;
3607 } *strings = NULL, *string;
3608 int i;
3609
3610 if (!func_handle->nr_args) {
3611 ret = (*func_handle->func)(s, NULL);
3612 goto out;
3613 }
3614
3615 farg = arg->func.args;
3616 param = func_handle->params;
3617
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003618 ret = ULLONG_MAX;
3619 args = malloc(sizeof(*args) * func_handle->nr_args);
3620 if (!args)
3621 goto out;
3622
Steven Rostedtf7d82352012-04-06 00:47:53 +02003623 for (i = 0; i < func_handle->nr_args; i++) {
3624 switch (param->type) {
3625 case PEVENT_FUNC_ARG_INT:
3626 case PEVENT_FUNC_ARG_LONG:
3627 case PEVENT_FUNC_ARG_PTR:
3628 args[i] = eval_num_arg(data, size, event, farg);
3629 break;
3630 case PEVENT_FUNC_ARG_STRING:
3631 trace_seq_init(&str);
3632 print_str_arg(&str, data, size, event, "%s", -1, farg);
3633 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003634 string = malloc(sizeof(*string));
3635 if (!string) {
3636 do_warning("%s(%d): malloc str", __func__, __LINE__);
3637 goto out_free;
3638 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003639 string->next = strings;
3640 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003641 if (!string->str) {
3642 free(string);
3643 do_warning("%s(%d): malloc str", __func__, __LINE__);
3644 goto out_free;
3645 }
Robert Richter0cf26012012-08-07 19:43:14 +02003646 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003647 strings = string;
3648 trace_seq_destroy(&str);
3649 break;
3650 default:
3651 /*
3652 * Something went totally wrong, this is not
3653 * an input error, something in this code broke.
3654 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003655 do_warning("Unexpected end of arguments\n");
3656 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003657 }
3658 farg = farg->next;
Namhyung Kim21c69e722012-05-23 11:36:51 +09003659 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003660 }
3661
3662 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003663out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003664 free(args);
3665 while (strings) {
3666 string = strings;
3667 strings = string->next;
3668 free(string->str);
3669 free(string);
3670 }
3671
3672 out:
3673 /* TBD : handle return type here */
3674 return ret;
3675}
3676
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003677static void free_args(struct print_arg *args)
3678{
3679 struct print_arg *next;
3680
3681 while (args) {
3682 next = args->next;
3683
3684 free_arg(args);
3685 args = next;
3686 }
3687}
3688
Steven Rostedtf7d82352012-04-06 00:47:53 +02003689static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3690{
3691 struct pevent *pevent = event->pevent;
3692 struct format_field *field, *ip_field;
3693 struct print_arg *args, *arg, **next;
3694 unsigned long long ip, val;
3695 char *ptr;
3696 void *bptr;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003697 int vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003698
3699 field = pevent->bprint_buf_field;
3700 ip_field = pevent->bprint_ip_field;
3701
3702 if (!field) {
3703 field = pevent_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003704 if (!field) {
3705 do_warning("can't find buffer field for binary printk");
3706 return NULL;
3707 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003708 ip_field = pevent_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003709 if (!ip_field) {
3710 do_warning("can't find ip field for binary printk");
3711 return NULL;
3712 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003713 pevent->bprint_buf_field = field;
3714 pevent->bprint_ip_field = ip_field;
3715 }
3716
3717 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3718
3719 /*
3720 * The first arg is the IP pointer.
3721 */
3722 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003723 if (!args) {
3724 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3725 return NULL;
3726 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003727 arg = args;
3728 arg->next = NULL;
3729 next = &arg->next;
3730
3731 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003732
3733 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3734 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003735
3736 /* skip the first "%pf : " */
3737 for (ptr = fmt + 6, bptr = data + field->offset;
3738 bptr < data + size && *ptr; ptr++) {
3739 int ls = 0;
3740
3741 if (*ptr == '%') {
3742 process_again:
3743 ptr++;
3744 switch (*ptr) {
3745 case '%':
3746 break;
3747 case 'l':
3748 ls++;
3749 goto process_again;
3750 case 'L':
3751 ls = 2;
3752 goto process_again;
3753 case '0' ... '9':
3754 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003755 case '.':
3756 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003757 case 'p':
3758 ls = 1;
3759 /* fall through */
3760 case 'd':
3761 case 'u':
3762 case 'x':
3763 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003764 switch (ls) {
3765 case 0:
3766 vsize = 4;
3767 break;
3768 case 1:
3769 vsize = pevent->long_size;
3770 break;
3771 case 2:
3772 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02003773 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003774 default:
3775 vsize = ls; /* ? */
3776 break;
3777 }
3778 /* fall through */
3779 case '*':
3780 if (*ptr == '*')
3781 vsize = 4;
3782
Steven Rostedtf7d82352012-04-06 00:47:53 +02003783 /* the pointers are always 4 bytes aligned */
3784 bptr = (void *)(((unsigned long)bptr + 3) &
3785 ~3);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003786 val = pevent_read_number(pevent, bptr, vsize);
3787 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003788 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003789 if (!arg) {
3790 do_warning("%s(%d): not enough memory!",
3791 __func__, __LINE__);
3792 goto out_free;
3793 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003794 arg->next = NULL;
3795 arg->type = PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003796 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3797 free(arg);
3798 goto out_free;
3799 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003800 *next = arg;
3801 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05003802 /*
3803 * The '*' case means that an arg is used as the length.
3804 * We need to continue to figure out for what.
3805 */
3806 if (*ptr == '*')
3807 goto process_again;
3808
Steven Rostedtf7d82352012-04-06 00:47:53 +02003809 break;
3810 case 's':
3811 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003812 if (!arg) {
3813 do_warning("%s(%d): not enough memory!",
3814 __func__, __LINE__);
3815 goto out_free;
3816 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003817 arg->next = NULL;
3818 arg->type = PRINT_BSTRING;
3819 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09003820 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003821 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003822 bptr += strlen(bptr) + 1;
3823 *next = arg;
3824 next = &arg->next;
3825 default:
3826 break;
3827 }
3828 }
3829 }
3830
3831 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003832
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003833out_free:
3834 free_args(args);
3835 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003836}
3837
3838static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003839get_bprint_format(void *data, int size __maybe_unused,
3840 struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003841{
3842 struct pevent *pevent = event->pevent;
3843 unsigned long long addr;
3844 struct format_field *field;
3845 struct printk_map *printk;
3846 char *format;
3847 char *p;
3848
3849 field = pevent->bprint_fmt_field;
3850
3851 if (!field) {
3852 field = pevent_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003853 if (!field) {
3854 do_warning("can't find format field for binary printk");
3855 return NULL;
3856 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003857 pevent->bprint_fmt_field = field;
3858 }
3859
3860 addr = pevent_read_number(pevent, data + field->offset, field->size);
3861
3862 printk = find_printk(pevent, addr);
3863 if (!printk) {
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003864 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3865 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003866 return format;
3867 }
3868
3869 p = printk->printk;
3870 /* Remove any quotes. */
3871 if (*p == '"')
3872 p++;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003873 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3874 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003875 /* remove ending quotes and new line since we will add one too */
3876 p = format + strlen(format) - 1;
3877 if (*p == '"')
3878 *p = 0;
3879
3880 p -= 2;
3881 if (strcmp(p, "\\n") == 0)
3882 *p = 0;
3883
3884 return format;
3885}
3886
3887static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3888 struct event_format *event, struct print_arg *arg)
3889{
3890 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03003891 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02003892
3893 if (arg->type == PRINT_FUNC) {
3894 process_defined_func(s, data, size, event, arg);
3895 return;
3896 }
3897
3898 if (arg->type != PRINT_FIELD) {
3899 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3900 arg->type);
3901 return;
3902 }
3903
3904 if (mac == 'm')
3905 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3906 if (!arg->field.field) {
3907 arg->field.field =
3908 pevent_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003909 if (!arg->field.field) {
3910 do_warning("%s: field %s not found",
3911 __func__, arg->field.name);
3912 return;
3913 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003914 }
3915 if (arg->field.field->size != 6) {
3916 trace_seq_printf(s, "INVALIDMAC");
3917 return;
3918 }
3919 buf = data + arg->field.field->offset;
3920 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3921}
3922
Namhyung Kim600da3c2012-06-22 17:10:15 +09003923static int is_printable_array(char *p, unsigned int len)
3924{
3925 unsigned int i;
3926
3927 for (i = 0; i < len && p[i]; i++)
3928 if (!isprint(p[i]))
3929 return 0;
3930 return 1;
3931}
3932
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03003933static void print_event_fields(struct trace_seq *s, void *data,
3934 int size __maybe_unused,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003935 struct event_format *event)
3936{
3937 struct format_field *field;
3938 unsigned long long val;
3939 unsigned int offset, len, i;
3940
3941 field = event->format.fields;
3942 while (field) {
3943 trace_seq_printf(s, " %s=", field->name);
3944 if (field->flags & FIELD_IS_ARRAY) {
3945 offset = field->offset;
3946 len = field->size;
3947 if (field->flags & FIELD_IS_DYNAMIC) {
3948 val = pevent_read_number(event->pevent, data + offset, len);
3949 offset = val;
3950 len = offset >> 16;
3951 offset &= 0xffff;
3952 }
Namhyung Kim600da3c2012-06-22 17:10:15 +09003953 if (field->flags & FIELD_IS_STRING &&
3954 is_printable_array(data + offset, len)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003955 trace_seq_printf(s, "%s", (char *)data + offset);
3956 } else {
3957 trace_seq_puts(s, "ARRAY[");
3958 for (i = 0; i < len; i++) {
3959 if (i)
3960 trace_seq_puts(s, ", ");
3961 trace_seq_printf(s, "%02x",
3962 *((unsigned char *)data + offset + i));
3963 }
3964 trace_seq_putc(s, ']');
Namhyung Kim600da3c2012-06-22 17:10:15 +09003965 field->flags &= ~FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003966 }
3967 } else {
3968 val = pevent_read_number(event->pevent, data + field->offset,
3969 field->size);
3970 if (field->flags & FIELD_IS_POINTER) {
3971 trace_seq_printf(s, "0x%llx", val);
3972 } else if (field->flags & FIELD_IS_SIGNED) {
3973 switch (field->size) {
3974 case 4:
3975 /*
3976 * If field is long then print it in hex.
3977 * A long usually stores pointers.
3978 */
3979 if (field->flags & FIELD_IS_LONG)
3980 trace_seq_printf(s, "0x%x", (int)val);
3981 else
3982 trace_seq_printf(s, "%d", (int)val);
3983 break;
3984 case 2:
3985 trace_seq_printf(s, "%2d", (short)val);
3986 break;
3987 case 1:
3988 trace_seq_printf(s, "%1d", (char)val);
3989 break;
3990 default:
3991 trace_seq_printf(s, "%lld", val);
3992 }
3993 } else {
3994 if (field->flags & FIELD_IS_LONG)
3995 trace_seq_printf(s, "0x%llx", val);
3996 else
3997 trace_seq_printf(s, "%llu", val);
3998 }
3999 }
4000 field = field->next;
4001 }
4002}
4003
4004static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4005{
4006 struct pevent *pevent = event->pevent;
4007 struct print_fmt *print_fmt = &event->print_fmt;
4008 struct print_arg *arg = print_fmt->args;
4009 struct print_arg *args = NULL;
4010 const char *ptr = print_fmt->format;
4011 unsigned long long val;
4012 struct func_map *func;
4013 const char *saveptr;
4014 char *bprint_fmt = NULL;
4015 char format[32];
4016 int show_func;
4017 int len_as_arg;
4018 int len_arg;
4019 int len;
4020 int ls;
4021
4022 if (event->flags & EVENT_FL_FAILED) {
4023 trace_seq_printf(s, "[FAILED TO PARSE]");
4024 print_event_fields(s, data, size, event);
4025 return;
4026 }
4027
4028 if (event->flags & EVENT_FL_ISBPRINT) {
4029 bprint_fmt = get_bprint_format(data, size, event);
4030 args = make_bprint_args(bprint_fmt, data, size, event);
4031 arg = args;
4032 ptr = bprint_fmt;
4033 }
4034
4035 for (; *ptr; ptr++) {
4036 ls = 0;
4037 if (*ptr == '\\') {
4038 ptr++;
4039 switch (*ptr) {
4040 case 'n':
4041 trace_seq_putc(s, '\n');
4042 break;
4043 case 't':
4044 trace_seq_putc(s, '\t');
4045 break;
4046 case 'r':
4047 trace_seq_putc(s, '\r');
4048 break;
4049 case '\\':
4050 trace_seq_putc(s, '\\');
4051 break;
4052 default:
4053 trace_seq_putc(s, *ptr);
4054 break;
4055 }
4056
4057 } else if (*ptr == '%') {
4058 saveptr = ptr;
4059 show_func = 0;
4060 len_as_arg = 0;
4061 cont_process:
4062 ptr++;
4063 switch (*ptr) {
4064 case '%':
4065 trace_seq_putc(s, '%');
4066 break;
4067 case '#':
4068 /* FIXME: need to handle properly */
4069 goto cont_process;
4070 case 'h':
4071 ls--;
4072 goto cont_process;
4073 case 'l':
4074 ls++;
4075 goto cont_process;
4076 case 'L':
4077 ls = 2;
4078 goto cont_process;
4079 case '*':
4080 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004081 if (!arg) {
4082 do_warning("no argument match");
4083 event->flags |= EVENT_FL_FAILED;
4084 goto out_failed;
4085 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004086 len_arg = eval_num_arg(data, size, event, arg);
4087 len_as_arg = 1;
4088 arg = arg->next;
4089 goto cont_process;
4090 case '.':
4091 case 'z':
4092 case 'Z':
4093 case '0' ... '9':
4094 goto cont_process;
4095 case 'p':
4096 if (pevent->long_size == 4)
4097 ls = 1;
4098 else
4099 ls = 2;
4100
4101 if (*(ptr+1) == 'F' ||
4102 *(ptr+1) == 'f') {
4103 ptr++;
4104 show_func = *ptr;
4105 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4106 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4107 ptr++;
Steven Rostedtaaf05c72012-01-09 15:58:09 -05004108 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004109 break;
4110 }
4111
4112 /* fall through */
4113 case 'd':
4114 case 'i':
4115 case 'x':
4116 case 'X':
4117 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004118 if (!arg) {
4119 do_warning("no argument match");
4120 event->flags |= EVENT_FL_FAILED;
4121 goto out_failed;
4122 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004123
4124 len = ((unsigned long)ptr + 1) -
4125 (unsigned long)saveptr;
4126
4127 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004128 if (len > 31) {
4129 do_warning("bad format!");
4130 event->flags |= EVENT_FL_FAILED;
4131 len = 31;
4132 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004133
4134 memcpy(format, saveptr, len);
4135 format[len] = 0;
4136
4137 val = eval_num_arg(data, size, event, arg);
4138 arg = arg->next;
4139
4140 if (show_func) {
4141 func = find_func(pevent, val);
4142 if (func) {
4143 trace_seq_puts(s, func->func);
4144 if (show_func == 'F')
4145 trace_seq_printf(s,
4146 "+0x%llx",
4147 val - func->addr);
4148 break;
4149 }
4150 }
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004151 if (pevent->long_size == 8 && ls &&
4152 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004153 char *p;
4154
4155 ls = 2;
4156 /* make %l into %ll */
4157 p = strchr(format, 'l');
4158 if (p)
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01004159 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004160 else if (strcmp(format, "%p") == 0)
4161 strcpy(format, "0x%llx");
4162 }
4163 switch (ls) {
4164 case -2:
4165 if (len_as_arg)
4166 trace_seq_printf(s, format, len_arg, (char)val);
4167 else
4168 trace_seq_printf(s, format, (char)val);
4169 break;
4170 case -1:
4171 if (len_as_arg)
4172 trace_seq_printf(s, format, len_arg, (short)val);
4173 else
4174 trace_seq_printf(s, format, (short)val);
4175 break;
4176 case 0:
4177 if (len_as_arg)
4178 trace_seq_printf(s, format, len_arg, (int)val);
4179 else
4180 trace_seq_printf(s, format, (int)val);
4181 break;
4182 case 1:
4183 if (len_as_arg)
4184 trace_seq_printf(s, format, len_arg, (long)val);
4185 else
4186 trace_seq_printf(s, format, (long)val);
4187 break;
4188 case 2:
4189 if (len_as_arg)
4190 trace_seq_printf(s, format, len_arg,
4191 (long long)val);
4192 else
4193 trace_seq_printf(s, format, (long long)val);
4194 break;
4195 default:
Namhyung Kim245c5a12012-09-07 11:49:45 +09004196 do_warning("bad count (%d)", ls);
4197 event->flags |= EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004198 }
4199 break;
4200 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09004201 if (!arg) {
4202 do_warning("no matching argument");
4203 event->flags |= EVENT_FL_FAILED;
4204 goto out_failed;
4205 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004206
4207 len = ((unsigned long)ptr + 1) -
4208 (unsigned long)saveptr;
4209
4210 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004211 if (len > 31) {
4212 do_warning("bad format!");
4213 event->flags |= EVENT_FL_FAILED;
4214 len = 31;
4215 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004216
4217 memcpy(format, saveptr, len);
4218 format[len] = 0;
4219 if (!len_as_arg)
4220 len_arg = -1;
4221 print_str_arg(s, data, size, event,
4222 format, len_arg, arg);
4223 arg = arg->next;
4224 break;
4225 default:
4226 trace_seq_printf(s, ">%c<", *ptr);
4227
4228 }
4229 } else
4230 trace_seq_putc(s, *ptr);
4231 }
4232
Namhyung Kim245c5a12012-09-07 11:49:45 +09004233 if (event->flags & EVENT_FL_FAILED) {
4234out_failed:
4235 trace_seq_printf(s, "[FAILED TO PARSE]");
4236 }
4237
Steven Rostedtf7d82352012-04-06 00:47:53 +02004238 if (args) {
4239 free_args(args);
4240 free(bprint_fmt);
4241 }
4242}
4243
4244/**
4245 * pevent_data_lat_fmt - parse the data for the latency format
4246 * @pevent: a handle to the pevent
4247 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004248 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004249 *
4250 * This parses out the Latency format (interrupts disabled,
4251 * need rescheduling, in hard/soft interrupt, preempt count
4252 * and lock depth) and places it into the trace_seq.
4253 */
4254void pevent_data_lat_fmt(struct pevent *pevent,
Steven Rostedt1c698182012-04-06 00:48:06 +02004255 struct trace_seq *s, struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004256{
4257 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09004258 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004259 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09004260 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004261 unsigned int lat_flags;
4262 unsigned int pc;
4263 int lock_depth;
Steven Rostedt0866a972012-05-22 14:52:40 +09004264 int migrate_disable;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004265 int hardirq;
4266 int softirq;
4267 void *data = record->data;
4268
4269 lat_flags = parse_common_flags(pevent, data);
4270 pc = parse_common_pc(pevent, data);
4271 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004272 if (lock_depth_exists)
4273 lock_depth = parse_common_lock_depth(pevent, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09004274 else if (check_lock_depth) {
4275 lock_depth = parse_common_lock_depth(pevent, data);
4276 if (lock_depth < 0)
4277 check_lock_depth = 0;
4278 else
4279 lock_depth_exists = 1;
4280 }
4281
4282 /* migrate_disable may not always exist */
4283 if (migrate_disable_exists)
4284 migrate_disable = parse_common_migrate_disable(pevent, data);
4285 else if (check_migrate_disable) {
4286 migrate_disable = parse_common_migrate_disable(pevent, data);
4287 if (migrate_disable < 0)
4288 check_migrate_disable = 0;
4289 else
4290 migrate_disable_exists = 1;
4291 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004292
4293 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4294 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4295
4296 trace_seq_printf(s, "%c%c%c",
4297 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4298 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4299 'X' : '.',
4300 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4301 'N' : '.',
4302 (hardirq && softirq) ? 'H' :
4303 hardirq ? 'h' : softirq ? 's' : '.');
4304
4305 if (pc)
4306 trace_seq_printf(s, "%x", pc);
4307 else
4308 trace_seq_putc(s, '.');
4309
Steven Rostedt0866a972012-05-22 14:52:40 +09004310 if (migrate_disable_exists) {
4311 if (migrate_disable < 0)
4312 trace_seq_putc(s, '.');
4313 else
4314 trace_seq_printf(s, "%d", migrate_disable);
4315 }
4316
Steven Rostedtf7d82352012-04-06 00:47:53 +02004317 if (lock_depth_exists) {
4318 if (lock_depth < 0)
4319 trace_seq_putc(s, '.');
4320 else
4321 trace_seq_printf(s, "%d", lock_depth);
4322 }
4323
4324 trace_seq_terminate(s);
4325}
4326
4327/**
4328 * pevent_data_type - parse out the given event type
4329 * @pevent: a handle to the pevent
4330 * @rec: the record to read from
4331 *
4332 * This returns the event id from the @rec.
4333 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004334int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004335{
4336 return trace_parse_common_type(pevent, rec->data);
4337}
4338
4339/**
4340 * pevent_data_event_from_type - find the event by a given type
4341 * @pevent: a handle to the pevent
4342 * @type: the type of the event.
4343 *
4344 * This returns the event form a given @type;
4345 */
4346struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4347{
4348 return pevent_find_event(pevent, type);
4349}
4350
4351/**
4352 * pevent_data_pid - parse the PID from raw data
4353 * @pevent: a handle to the pevent
4354 * @rec: the record to parse
4355 *
4356 * This returns the PID from a raw data.
4357 */
Steven Rostedt1c698182012-04-06 00:48:06 +02004358int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004359{
4360 return parse_common_pid(pevent, rec->data);
4361}
4362
4363/**
4364 * pevent_data_comm_from_pid - return the command line from PID
4365 * @pevent: a handle to the pevent
4366 * @pid: the PID of the task to search for
4367 *
4368 * This returns a pointer to the command line that has the given
4369 * @pid.
4370 */
4371const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4372{
4373 const char *comm;
4374
4375 comm = find_cmdline(pevent, pid);
4376 return comm;
4377}
4378
4379/**
4380 * pevent_data_comm_from_pid - parse the data into the print format
4381 * @s: the trace_seq to write to
4382 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09004383 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02004384 *
4385 * This parses the raw @data using the given @event information and
4386 * writes the print format into the trace_seq.
4387 */
4388void pevent_event_info(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02004389 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004390{
4391 int print_pretty = 1;
4392
4393 if (event->pevent->print_raw)
4394 print_event_fields(s, record->data, record->size, event);
4395 else {
4396
4397 if (event->handler)
4398 print_pretty = event->handler(s, record, event,
4399 event->context);
4400
4401 if (print_pretty)
4402 pretty_print(s, record->data, record->size, event);
4403 }
4404
4405 trace_seq_terminate(s);
4406}
4407
4408void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
Steven Rostedt1c698182012-04-06 00:48:06 +02004409 struct pevent_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004410{
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004411 static const char *spaces = " "; /* 20 spaces */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004412 struct event_format *event;
4413 unsigned long secs;
4414 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004415 unsigned long nsecs;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004416 const char *comm;
4417 void *data = record->data;
4418 int type;
4419 int pid;
4420 int len;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004421 int p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004422
4423 secs = record->ts / NSECS_PER_SEC;
Steven Rostedt4dc10242012-04-06 00:47:57 +02004424 nsecs = record->ts - secs * NSECS_PER_SEC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004425
4426 if (record->size < 0) {
4427 do_warning("ug! negative record size %d", record->size);
4428 return;
4429 }
4430
4431 type = trace_parse_common_type(pevent, data);
4432
4433 event = pevent_find_event(pevent, type);
4434 if (!event) {
4435 do_warning("ug! no event found for type %d", type);
4436 return;
4437 }
4438
4439 pid = parse_common_pid(pevent, data);
4440 comm = find_cmdline(pevent, pid);
4441
4442 if (pevent->latency_format) {
4443 trace_seq_printf(s, "%8.8s-%-5d %3d",
4444 comm, pid, record->cpu);
4445 pevent_data_lat_fmt(pevent, s, record);
4446 } else
4447 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4448
Steven Rostedt4dc10242012-04-06 00:47:57 +02004449 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4450 usecs = nsecs;
4451 p = 9;
4452 } else {
4453 usecs = (nsecs + 500) / NSECS_PER_USEC;
4454 p = 6;
4455 }
4456
4457 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004458
4459 /* Space out the event names evenly. */
4460 len = strlen(event->name);
4461 if (len < 20)
4462 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4463
4464 pevent_event_info(s, event, record);
4465}
4466
4467static int events_id_cmp(const void *a, const void *b)
4468{
4469 struct event_format * const * ea = a;
4470 struct event_format * const * eb = b;
4471
4472 if ((*ea)->id < (*eb)->id)
4473 return -1;
4474
4475 if ((*ea)->id > (*eb)->id)
4476 return 1;
4477
4478 return 0;
4479}
4480
4481static int events_name_cmp(const void *a, const void *b)
4482{
4483 struct event_format * const * ea = a;
4484 struct event_format * const * eb = b;
4485 int res;
4486
4487 res = strcmp((*ea)->name, (*eb)->name);
4488 if (res)
4489 return res;
4490
4491 res = strcmp((*ea)->system, (*eb)->system);
4492 if (res)
4493 return res;
4494
4495 return events_id_cmp(a, b);
4496}
4497
4498static int events_system_cmp(const void *a, const void *b)
4499{
4500 struct event_format * const * ea = a;
4501 struct event_format * const * eb = b;
4502 int res;
4503
4504 res = strcmp((*ea)->system, (*eb)->system);
4505 if (res)
4506 return res;
4507
4508 res = strcmp((*ea)->name, (*eb)->name);
4509 if (res)
4510 return res;
4511
4512 return events_id_cmp(a, b);
4513}
4514
4515struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4516{
4517 struct event_format **events;
4518 int (*sort)(const void *a, const void *b);
4519
4520 events = pevent->sort_events;
4521
4522 if (events && pevent->last_type == sort_type)
4523 return events;
4524
4525 if (!events) {
4526 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4527 if (!events)
4528 return NULL;
4529
4530 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4531 events[pevent->nr_events] = NULL;
4532
4533 pevent->sort_events = events;
4534
4535 /* the internal events are sorted by id */
4536 if (sort_type == EVENT_SORT_ID) {
4537 pevent->last_type = sort_type;
4538 return events;
4539 }
4540 }
4541
4542 switch (sort_type) {
4543 case EVENT_SORT_ID:
4544 sort = events_id_cmp;
4545 break;
4546 case EVENT_SORT_NAME:
4547 sort = events_name_cmp;
4548 break;
4549 case EVENT_SORT_SYSTEM:
4550 sort = events_system_cmp;
4551 break;
4552 default:
4553 return events;
4554 }
4555
4556 qsort(events, pevent->nr_events, sizeof(*events), sort);
4557 pevent->last_type = sort_type;
4558
4559 return events;
4560}
4561
4562static struct format_field **
4563get_event_fields(const char *type, const char *name,
4564 int count, struct format_field *list)
4565{
4566 struct format_field **fields;
4567 struct format_field *field;
4568 int i = 0;
4569
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004570 fields = malloc(sizeof(*fields) * (count + 1));
4571 if (!fields)
4572 return NULL;
4573
Steven Rostedtf7d82352012-04-06 00:47:53 +02004574 for (field = list; field; field = field->next) {
4575 fields[i++] = field;
4576 if (i == count + 1) {
4577 do_warning("event %s has more %s fields than specified",
4578 name, type);
4579 i--;
4580 break;
4581 }
4582 }
4583
4584 if (i != count)
4585 do_warning("event %s has less %s fields than specified",
4586 name, type);
4587
4588 fields[i] = NULL;
4589
4590 return fields;
4591}
4592
4593/**
4594 * pevent_event_common_fields - return a list of common fields for an event
4595 * @event: the event to return the common fields of.
4596 *
4597 * Returns an allocated array of fields. The last item in the array is NULL.
4598 * The array must be freed with free().
4599 */
4600struct format_field **pevent_event_common_fields(struct event_format *event)
4601{
4602 return get_event_fields("common", event->name,
4603 event->format.nr_common,
4604 event->format.common_fields);
4605}
4606
4607/**
4608 * pevent_event_fields - return a list of event specific fields for an event
4609 * @event: the event to return the fields of.
4610 *
4611 * Returns an allocated array of fields. The last item in the array is NULL.
4612 * The array must be freed with free().
4613 */
4614struct format_field **pevent_event_fields(struct event_format *event)
4615{
4616 return get_event_fields("event", event->name,
4617 event->format.nr_fields,
4618 event->format.fields);
4619}
4620
4621static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4622{
4623 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4624 if (field->next) {
4625 trace_seq_puts(s, ", ");
4626 print_fields(s, field->next);
4627 }
4628}
4629
4630/* for debugging */
4631static void print_args(struct print_arg *args)
4632{
4633 int print_paren = 1;
4634 struct trace_seq s;
4635
4636 switch (args->type) {
4637 case PRINT_NULL:
4638 printf("null");
4639 break;
4640 case PRINT_ATOM:
4641 printf("%s", args->atom.atom);
4642 break;
4643 case PRINT_FIELD:
4644 printf("REC->%s", args->field.name);
4645 break;
4646 case PRINT_FLAGS:
4647 printf("__print_flags(");
4648 print_args(args->flags.field);
4649 printf(", %s, ", args->flags.delim);
4650 trace_seq_init(&s);
4651 print_fields(&s, args->flags.flags);
4652 trace_seq_do_printf(&s);
4653 trace_seq_destroy(&s);
4654 printf(")");
4655 break;
4656 case PRINT_SYMBOL:
4657 printf("__print_symbolic(");
4658 print_args(args->symbol.field);
4659 printf(", ");
4660 trace_seq_init(&s);
4661 print_fields(&s, args->symbol.symbols);
4662 trace_seq_do_printf(&s);
4663 trace_seq_destroy(&s);
4664 printf(")");
4665 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004666 case PRINT_HEX:
4667 printf("__print_hex(");
4668 print_args(args->hex.field);
4669 printf(", ");
4670 print_args(args->hex.size);
4671 printf(")");
4672 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004673 case PRINT_STRING:
4674 case PRINT_BSTRING:
4675 printf("__get_str(%s)", args->string.string);
4676 break;
4677 case PRINT_TYPE:
4678 printf("(%s)", args->typecast.type);
4679 print_args(args->typecast.item);
4680 break;
4681 case PRINT_OP:
4682 if (strcmp(args->op.op, ":") == 0)
4683 print_paren = 0;
4684 if (print_paren)
4685 printf("(");
4686 print_args(args->op.left);
4687 printf(" %s ", args->op.op);
4688 print_args(args->op.right);
4689 if (print_paren)
4690 printf(")");
4691 break;
4692 default:
4693 /* we should warn... */
4694 return;
4695 }
4696 if (args->next) {
4697 printf("\n");
4698 print_args(args->next);
4699 }
4700}
4701
4702static void parse_header_field(const char *field,
4703 int *offset, int *size, int mandatory)
4704{
4705 unsigned long long save_input_buf_ptr;
4706 unsigned long long save_input_buf_siz;
4707 char *token;
4708 int type;
4709
4710 save_input_buf_ptr = input_buf_ptr;
4711 save_input_buf_siz = input_buf_siz;
4712
4713 if (read_expected(EVENT_ITEM, "field") < 0)
4714 return;
4715 if (read_expected(EVENT_OP, ":") < 0)
4716 return;
4717
4718 /* type */
4719 if (read_expect_type(EVENT_ITEM, &token) < 0)
4720 goto fail;
4721 free_token(token);
4722
4723 /*
4724 * If this is not a mandatory field, then test it first.
4725 */
4726 if (mandatory) {
4727 if (read_expected(EVENT_ITEM, field) < 0)
4728 return;
4729 } else {
4730 if (read_expect_type(EVENT_ITEM, &token) < 0)
4731 goto fail;
4732 if (strcmp(token, field) != 0)
4733 goto discard;
4734 free_token(token);
4735 }
4736
4737 if (read_expected(EVENT_OP, ";") < 0)
4738 return;
4739 if (read_expected(EVENT_ITEM, "offset") < 0)
4740 return;
4741 if (read_expected(EVENT_OP, ":") < 0)
4742 return;
4743 if (read_expect_type(EVENT_ITEM, &token) < 0)
4744 goto fail;
4745 *offset = atoi(token);
4746 free_token(token);
4747 if (read_expected(EVENT_OP, ";") < 0)
4748 return;
4749 if (read_expected(EVENT_ITEM, "size") < 0)
4750 return;
4751 if (read_expected(EVENT_OP, ":") < 0)
4752 return;
4753 if (read_expect_type(EVENT_ITEM, &token) < 0)
4754 goto fail;
4755 *size = atoi(token);
4756 free_token(token);
4757 if (read_expected(EVENT_OP, ";") < 0)
4758 return;
4759 type = read_token(&token);
4760 if (type != EVENT_NEWLINE) {
4761 /* newer versions of the kernel have a "signed" type */
4762 if (type != EVENT_ITEM)
4763 goto fail;
4764
4765 if (strcmp(token, "signed") != 0)
4766 goto fail;
4767
4768 free_token(token);
4769
4770 if (read_expected(EVENT_OP, ":") < 0)
4771 return;
4772
4773 if (read_expect_type(EVENT_ITEM, &token))
4774 goto fail;
4775
4776 free_token(token);
4777 if (read_expected(EVENT_OP, ";") < 0)
4778 return;
4779
4780 if (read_expect_type(EVENT_NEWLINE, &token))
4781 goto fail;
4782 }
4783 fail:
4784 free_token(token);
4785 return;
4786
4787 discard:
4788 input_buf_ptr = save_input_buf_ptr;
4789 input_buf_siz = save_input_buf_siz;
4790 *offset = 0;
4791 *size = 0;
4792 free_token(token);
4793}
4794
4795/**
4796 * pevent_parse_header_page - parse the data stored in the header page
4797 * @pevent: the handle to the pevent
4798 * @buf: the buffer storing the header page format string
4799 * @size: the size of @buf
4800 * @long_size: the long size to use if there is no header
4801 *
4802 * This parses the header page format for information on the
4803 * ring buffer used. The @buf should be copied from
4804 *
4805 * /sys/kernel/debug/tracing/events/header_page
4806 */
4807int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4808 int long_size)
4809{
4810 int ignore;
4811
4812 if (!size) {
4813 /*
4814 * Old kernels did not have header page info.
4815 * Sorry but we just use what we find here in user space.
4816 */
4817 pevent->header_page_ts_size = sizeof(long long);
4818 pevent->header_page_size_size = long_size;
4819 pevent->header_page_data_offset = sizeof(long long) + long_size;
4820 pevent->old_format = 1;
4821 return -1;
4822 }
4823 init_input_buf(buf, size);
4824
4825 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4826 &pevent->header_page_ts_size, 1);
4827 parse_header_field("commit", &pevent->header_page_size_offset,
4828 &pevent->header_page_size_size, 1);
4829 parse_header_field("overwrite", &pevent->header_page_overwrite,
4830 &ignore, 0);
4831 parse_header_field("data", &pevent->header_page_data_offset,
4832 &pevent->header_page_data_size, 1);
4833
4834 return 0;
4835}
4836
4837static int event_matches(struct event_format *event,
4838 int id, const char *sys_name,
4839 const char *event_name)
4840{
4841 if (id >= 0 && id != event->id)
4842 return 0;
4843
4844 if (event_name && (strcmp(event_name, event->name) != 0))
4845 return 0;
4846
4847 if (sys_name && (strcmp(sys_name, event->system) != 0))
4848 return 0;
4849
4850 return 1;
4851}
4852
4853static void free_handler(struct event_handler *handle)
4854{
4855 free((void *)handle->sys_name);
4856 free((void *)handle->event_name);
4857 free(handle);
4858}
4859
4860static int find_event_handle(struct pevent *pevent, struct event_format *event)
4861{
4862 struct event_handler *handle, **next;
4863
4864 for (next = &pevent->handlers; *next;
4865 next = &(*next)->next) {
4866 handle = *next;
4867 if (event_matches(event, handle->id,
4868 handle->sys_name,
4869 handle->event_name))
4870 break;
4871 }
4872
4873 if (!(*next))
4874 return 0;
4875
4876 pr_stat("overriding event (%d) %s:%s with new print handler",
4877 event->id, event->system, event->name);
4878
4879 event->handler = handle->func;
4880 event->context = handle->context;
4881
4882 *next = handle->next;
4883 free_handler(handle);
4884
4885 return 1;
4886}
4887
4888/**
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004889 * __pevent_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02004890 * @buf: the buffer storing the event format string
4891 * @size: the size of @buf
4892 * @sys: the system the event belongs to
4893 *
4894 * This parses the event format and creates an event structure
4895 * to quickly parse raw data for a given event.
4896 *
4897 * These files currently come from:
4898 *
4899 * /sys/kernel/debug/tracing/events/.../.../format
4900 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004901enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4902 struct pevent *pevent, const char *buf,
4903 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004904{
4905 struct event_format *event;
4906 int ret;
4907
4908 init_input_buf(buf, size);
4909
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004910 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02004911 if (!event)
Namhyung Kimbffddff2012-08-22 16:00:29 +09004912 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004913
4914 event->name = event_read_name();
4915 if (!event->name) {
4916 /* Bad event? */
Namhyung Kimbffddff2012-08-22 16:00:29 +09004917 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4918 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004919 }
4920
4921 if (strcmp(sys, "ftrace") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004922 event->flags |= EVENT_FL_ISFTRACE;
4923
4924 if (strcmp(event->name, "bprint") == 0)
4925 event->flags |= EVENT_FL_ISBPRINT;
4926 }
4927
4928 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09004929 if (event->id < 0) {
4930 ret = PEVENT_ERRNO__READ_ID_FAILED;
4931 /*
4932 * This isn't an allocation error actually.
4933 * But as the ID is critical, just bail out.
4934 */
4935 goto event_alloc_failed;
4936 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004937
4938 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004939 if (!event->system) {
4940 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4941 goto event_alloc_failed;
4942 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004943
Steven Rostedt101782e2012-10-01 20:13:51 -04004944 /* Add pevent to event so that it can be referenced */
4945 event->pevent = pevent;
4946
Steven Rostedtf7d82352012-04-06 00:47:53 +02004947 ret = event_read_format(event);
4948 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09004949 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4950 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004951 }
4952
4953 /*
4954 * If the event has an override, don't print warnings if the event
4955 * print format fails to parse.
4956 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004957 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02004958 show_warning = 0;
4959
4960 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004961 show_warning = 1;
4962
Steven Rostedtf7d82352012-04-06 00:47:53 +02004963 if (ret < 0) {
Namhyung Kimbffddff2012-08-22 16:00:29 +09004964 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4965 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004966 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004967
4968 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4969 struct format_field *field;
4970 struct print_arg *arg, **list;
4971
4972 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02004973 list = &event->print_fmt.args;
4974 for (field = event->format.fields; field; field = field->next) {
4975 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004976 if (!arg) {
4977 event->flags |= EVENT_FL_FAILED;
4978 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4979 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004980 arg->type = PRINT_FIELD;
4981 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09004982 if (!arg->field.name) {
Namhyung Kim4b5632b2012-04-23 13:58:34 +09004983 event->flags |= EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004984 free_arg(arg);
Namhyung Kimbffddff2012-08-22 16:00:29 +09004985 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09004986 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004987 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09004988 *list = arg;
4989 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004990 }
4991 return 0;
4992 }
4993
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03004994 return 0;
4995
4996 event_parse_failed:
4997 event->flags |= EVENT_FL_FAILED;
4998 return ret;
4999
5000 event_alloc_failed:
5001 free(event->system);
5002 free(event->name);
5003 free(event);
5004 *eventp = NULL;
5005 return ret;
5006}
5007
5008/**
5009 * pevent_parse_format - parse the event format
5010 * @buf: the buffer storing the event format string
5011 * @size: the size of @buf
5012 * @sys: the system the event belongs to
5013 *
5014 * This parses the event format and creates an event structure
5015 * to quickly parse raw data for a given event.
5016 *
5017 * These files currently come from:
5018 *
5019 * /sys/kernel/debug/tracing/events/.../.../format
5020 */
5021enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5022 unsigned long size, const char *sys)
5023{
5024 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5025}
5026
5027/**
5028 * pevent_parse_event - parse the event format
5029 * @pevent: the handle to the pevent
5030 * @buf: the buffer storing the event format string
5031 * @size: the size of @buf
5032 * @sys: the system the event belongs to
5033 *
5034 * This parses the event format and creates an event structure
5035 * to quickly parse raw data for a given event.
5036 *
5037 * These files currently come from:
5038 *
5039 * /sys/kernel/debug/tracing/events/.../.../format
5040 */
5041enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5042 unsigned long size, const char *sys)
5043{
5044 struct event_format *event = NULL;
5045 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5046
5047 if (event == NULL)
5048 return ret;
5049
Namhyung Kimf1b22562012-09-25 21:25:19 +09005050 if (add_event(pevent, event)) {
5051 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005052 goto event_add_failed;
Namhyung Kimf1b22562012-09-25 21:25:19 +09005053 }
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005054
Steven Rostedtf7d82352012-04-06 00:47:53 +02005055#define PRINT_ARGS 0
5056 if (PRINT_ARGS && event->print_fmt.args)
5057 print_args(event->print_fmt.args);
5058
5059 return 0;
5060
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005061event_add_failed:
Namhyung Kimf1b22562012-09-25 21:25:19 +09005062 pevent_free_format(event);
Namhyung Kimbffddff2012-08-22 16:00:29 +09005063 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005064}
5065
Namhyung Kim2f197b92012-08-22 16:00:30 +09005066#undef _PE
5067#define _PE(code, str) str
5068static const char * const pevent_error_str[] = {
5069 PEVENT_ERRORS
5070};
5071#undef _PE
5072
Arnaldo Carvalho de Meloca383a42012-11-09 15:18:57 -03005073int pevent_strerror(struct pevent *pevent __maybe_unused,
5074 enum pevent_errno errnum, char *buf, size_t buflen)
Namhyung Kim2f197b92012-08-22 16:00:30 +09005075{
5076 int idx;
5077 const char *msg;
5078
5079 if (errnum >= 0) {
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005080 msg = strerror_r(errnum, buf, buflen);
5081 if (msg != buf) {
5082 size_t len = strlen(msg);
Irina Tirdea9612ef62012-09-08 03:43:22 +03005083 memcpy(buf, msg, min(buflen - 1, len));
5084 *(buf + min(buflen - 1, len)) = '\0';
Namhyung Kime1aa7c32012-08-22 16:00:31 +09005085 }
Namhyung Kim2f197b92012-08-22 16:00:30 +09005086 return 0;
5087 }
5088
5089 if (errnum <= __PEVENT_ERRNO__START ||
5090 errnum >= __PEVENT_ERRNO__END)
5091 return -1;
5092
Namhyung Kimf63fe792012-08-23 16:37:00 +09005093 idx = errnum - __PEVENT_ERRNO__START - 1;
Namhyung Kim2f197b92012-08-22 16:00:30 +09005094 msg = pevent_error_str[idx];
5095
5096 switch (errnum) {
5097 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5098 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5099 case PEVENT_ERRNO__READ_ID_FAILED:
5100 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5101 case PEVENT_ERRNO__READ_PRINT_FAILED:
5102 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
Arnaldo Carvalho de Melo7a905612012-11-09 15:50:33 -03005103 case PEVENT_ERRNO__INVALID_ARG_TYPE:
Namhyung Kim2f197b92012-08-22 16:00:30 +09005104 snprintf(buf, buflen, "%s", msg);
5105 break;
5106
5107 default:
5108 /* cannot reach here */
5109 break;
5110 }
5111
5112 return 0;
5113}
5114
Steven Rostedtf7d82352012-04-06 00:47:53 +02005115int get_field_val(struct trace_seq *s, struct format_field *field,
Steven Rostedt1c698182012-04-06 00:48:06 +02005116 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005117 unsigned long long *val, int err)
5118{
5119 if (!field) {
5120 if (err)
5121 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5122 return -1;
5123 }
5124
5125 if (pevent_read_number_field(field, record->data, val)) {
5126 if (err)
5127 trace_seq_printf(s, " %s=INVALID", name);
5128 return -1;
5129 }
5130
5131 return 0;
5132}
5133
5134/**
5135 * pevent_get_field_raw - return the raw pointer into the data field
5136 * @s: The seq to print to on error
5137 * @event: the event that the field is for
5138 * @name: The name of the field
5139 * @record: The record with the field name.
5140 * @len: place to store the field length.
5141 * @err: print default error if failed.
5142 *
5143 * Returns a pointer into record->data of the field and places
5144 * the length of the field in @len.
5145 *
5146 * On failure, it returns NULL.
5147 */
5148void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005149 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005150 int *len, int err)
5151{
5152 struct format_field *field;
5153 void *data = record->data;
5154 unsigned offset;
5155 int dummy;
5156
5157 if (!event)
5158 return NULL;
5159
5160 field = pevent_find_field(event, name);
5161
5162 if (!field) {
5163 if (err)
5164 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5165 return NULL;
5166 }
5167
5168 /* Allow @len to be NULL */
5169 if (!len)
5170 len = &dummy;
5171
5172 offset = field->offset;
5173 if (field->flags & FIELD_IS_DYNAMIC) {
5174 offset = pevent_read_number(event->pevent,
5175 data + offset, field->size);
5176 *len = offset >> 16;
5177 offset &= 0xffff;
5178 } else
5179 *len = field->size;
5180
5181 return data + offset;
5182}
5183
5184/**
5185 * pevent_get_field_val - find a field and return its value
5186 * @s: The seq to print to on error
5187 * @event: the event that the field is for
5188 * @name: The name of the field
5189 * @record: The record with the field name.
5190 * @val: place to store the value of the field.
5191 * @err: print default error if failed.
5192 *
5193 * Returns 0 on success -1 on field not found.
5194 */
5195int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005196 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005197 unsigned long long *val, int err)
5198{
5199 struct format_field *field;
5200
5201 if (!event)
5202 return -1;
5203
5204 field = pevent_find_field(event, name);
5205
5206 return get_field_val(s, field, name, record, val, err);
5207}
5208
5209/**
5210 * pevent_get_common_field_val - find a common field and return its value
5211 * @s: The seq to print to on error
5212 * @event: the event that the field is for
5213 * @name: The name of the field
5214 * @record: The record with the field name.
5215 * @val: place to store the value of the field.
5216 * @err: print default error if failed.
5217 *
5218 * Returns 0 on success -1 on field not found.
5219 */
5220int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005221 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005222 unsigned long long *val, int err)
5223{
5224 struct format_field *field;
5225
5226 if (!event)
5227 return -1;
5228
5229 field = pevent_find_common_field(event, name);
5230
5231 return get_field_val(s, field, name, record, val, err);
5232}
5233
5234/**
5235 * pevent_get_any_field_val - find a any field and return its value
5236 * @s: The seq to print to on error
5237 * @event: the event that the field is for
5238 * @name: The name of the field
5239 * @record: The record with the field name.
5240 * @val: place to store the value of the field.
5241 * @err: print default error if failed.
5242 *
5243 * Returns 0 on success -1 on field not found.
5244 */
5245int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
Steven Rostedt1c698182012-04-06 00:48:06 +02005246 const char *name, struct pevent_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005247 unsigned long long *val, int err)
5248{
5249 struct format_field *field;
5250
5251 if (!event)
5252 return -1;
5253
5254 field = pevent_find_any_field(event, name);
5255
5256 return get_field_val(s, field, name, record, val, err);
5257}
5258
5259/**
5260 * pevent_print_num_field - print a field and a format
5261 * @s: The seq to print to
5262 * @fmt: The printf format to print the field with.
5263 * @event: the event that the field is for
5264 * @name: The name of the field
5265 * @record: The record with the field name.
5266 * @err: print default error if failed.
5267 *
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005268 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005269 */
5270int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5271 struct event_format *event, const char *name,
Steven Rostedt1c698182012-04-06 00:48:06 +02005272 struct pevent_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005273{
5274 struct format_field *field = pevent_find_field(event, name);
5275 unsigned long long val;
5276
5277 if (!field)
5278 goto failed;
5279
5280 if (pevent_read_number_field(field, record->data, &val))
5281 goto failed;
5282
5283 return trace_seq_printf(s, fmt, val);
5284
5285 failed:
5286 if (err)
5287 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5288 return -1;
5289}
5290
5291static void free_func_handle(struct pevent_function_handler *func)
5292{
5293 struct pevent_func_params *params;
5294
5295 free(func->name);
5296
5297 while (func->params) {
5298 params = func->params;
5299 func->params = params->next;
5300 free(params);
5301 }
5302
5303 free(func);
5304}
5305
5306/**
5307 * pevent_register_print_function - register a helper function
5308 * @pevent: the handle to the pevent
5309 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005310 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02005311 * @name: the name of the helper function
5312 * @parameters: A list of enum pevent_func_arg_type
5313 *
5314 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005315 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02005316 * of these functions.
5317 *
5318 * The @parameters is a variable list of pevent_func_arg_type enums that
5319 * must end with PEVENT_FUNC_ARG_VOID.
5320 */
5321int pevent_register_print_function(struct pevent *pevent,
5322 pevent_func_handler func,
5323 enum pevent_func_arg_type ret_type,
5324 char *name, ...)
5325{
5326 struct pevent_function_handler *func_handle;
5327 struct pevent_func_params **next_param;
5328 struct pevent_func_params *param;
5329 enum pevent_func_arg_type type;
5330 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005331 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005332
5333 func_handle = find_func_handler(pevent, name);
5334 if (func_handle) {
5335 /*
5336 * This is most like caused by the users own
5337 * plugins updating the function. This overrides the
5338 * system defaults.
5339 */
5340 pr_stat("override of function helper '%s'", name);
5341 remove_func_handler(pevent, name);
5342 }
5343
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005344 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09005345 if (!func_handle) {
5346 do_warning("Failed to allocate function handler");
5347 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5348 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005349
5350 func_handle->ret_type = ret_type;
5351 func_handle->name = strdup(name);
5352 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09005353 if (!func_handle->name) {
5354 do_warning("Failed to allocate function name");
5355 free(func_handle);
5356 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5357 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005358
5359 next_param = &(func_handle->params);
5360 va_start(ap, name);
5361 for (;;) {
5362 type = va_arg(ap, enum pevent_func_arg_type);
5363 if (type == PEVENT_FUNC_ARG_VOID)
5364 break;
5365
Arnaldo Carvalho de Meloe46466b2012-11-09 15:42:26 -03005366 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09005367 do_warning("Invalid argument type %d", type);
5368 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005369 goto out_free;
5370 }
5371
Namhyung Kim67ed9392012-09-07 11:49:47 +09005372 param = malloc(sizeof(*param));
5373 if (!param) {
5374 do_warning("Failed to allocate function param");
5375 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5376 goto out_free;
5377 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005378 param->type = type;
5379 param->next = NULL;
5380
5381 *next_param = param;
5382 next_param = &(param->next);
5383
5384 func_handle->nr_args++;
5385 }
5386 va_end(ap);
5387
5388 func_handle->next = pevent->func_handlers;
5389 pevent->func_handlers = func_handle;
5390
5391 return 0;
5392 out_free:
5393 va_end(ap);
5394 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09005395 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005396}
5397
5398/**
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005399 * pevent_register_event_handler - register a way to parse an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005400 * @pevent: the handle to the pevent
5401 * @id: the id of the event to register
5402 * @sys_name: the system name the event belongs to
5403 * @event_name: the name of the event
5404 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005405 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02005406 *
5407 * This function allows a developer to override the parsing of
5408 * a given event. If for some reason the default print format
5409 * is not sufficient, this function will register a function
5410 * for an event to be used to parse the data instead.
5411 *
5412 * If @id is >= 0, then it is used to find the event.
5413 * else @sys_name and @event_name are used.
5414 */
5415int pevent_register_event_handler(struct pevent *pevent,
5416 int id, char *sys_name, char *event_name,
5417 pevent_event_handler_func func,
5418 void *context)
5419{
5420 struct event_format *event;
5421 struct event_handler *handle;
5422
5423 if (id >= 0) {
5424 /* search by id */
5425 event = pevent_find_event(pevent, id);
5426 if (!event)
5427 goto not_found;
5428 if (event_name && (strcmp(event_name, event->name) != 0))
5429 goto not_found;
5430 if (sys_name && (strcmp(sys_name, event->system) != 0))
5431 goto not_found;
5432 } else {
5433 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5434 if (!event)
5435 goto not_found;
5436 }
5437
5438 pr_stat("overriding event (%d) %s:%s with new print handler",
5439 event->id, event->system, event->name);
5440
5441 event->handler = func;
5442 event->context = context;
5443 return 0;
5444
5445 not_found:
5446 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005447 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005448 if (!handle) {
5449 do_warning("Failed to allocate event handler");
5450 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5451 }
5452
Steven Rostedtf7d82352012-04-06 00:47:53 +02005453 handle->id = id;
5454 if (event_name)
5455 handle->event_name = strdup(event_name);
5456 if (sys_name)
5457 handle->sys_name = strdup(sys_name);
5458
Namhyung Kimca638582012-04-09 11:54:31 +09005459 if ((event_name && !handle->event_name) ||
5460 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09005461 do_warning("Failed to allocate event/sys name");
5462 free((void *)handle->event_name);
5463 free((void *)handle->sys_name);
5464 free(handle);
5465 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09005466 }
5467
Steven Rostedtf7d82352012-04-06 00:47:53 +02005468 handle->func = func;
5469 handle->next = pevent->handlers;
5470 pevent->handlers = handle;
5471 handle->context = context;
5472
5473 return -1;
5474}
5475
5476/**
5477 * pevent_alloc - create a pevent handle
5478 */
5479struct pevent *pevent_alloc(void)
5480{
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005481 struct pevent *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02005482
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03005483 if (pevent)
5484 pevent->ref_count = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005485
5486 return pevent;
5487}
5488
5489void pevent_ref(struct pevent *pevent)
5490{
5491 pevent->ref_count++;
5492}
5493
5494static void free_format_fields(struct format_field *field)
5495{
5496 struct format_field *next;
5497
5498 while (field) {
5499 next = field->next;
5500 free(field->type);
5501 free(field->name);
5502 free(field);
5503 field = next;
5504 }
5505}
5506
5507static void free_formats(struct format *format)
5508{
5509 free_format_fields(format->common_fields);
5510 free_format_fields(format->fields);
5511}
5512
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005513void pevent_free_format(struct event_format *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005514{
5515 free(event->name);
5516 free(event->system);
5517
5518 free_formats(&event->format);
5519
5520 free(event->print_fmt.format);
5521 free_args(event->print_fmt.args);
5522
5523 free(event);
5524}
5525
5526/**
5527 * pevent_free - free a pevent handle
5528 * @pevent: the pevent handle to free
5529 */
5530void pevent_free(struct pevent *pevent)
5531{
Steven Rostedta2525a02012-04-06 00:48:02 +02005532 struct cmdline_list *cmdlist, *cmdnext;
5533 struct func_list *funclist, *funcnext;
5534 struct printk_list *printklist, *printknext;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005535 struct pevent_function_handler *func_handler;
5536 struct event_handler *handle;
5537 int i;
5538
Steven Rostedta2525a02012-04-06 00:48:02 +02005539 if (!pevent)
5540 return;
5541
5542 cmdlist = pevent->cmdlist;
5543 funclist = pevent->funclist;
5544 printklist = pevent->printklist;
5545
Steven Rostedtf7d82352012-04-06 00:47:53 +02005546 pevent->ref_count--;
5547 if (pevent->ref_count)
5548 return;
5549
5550 if (pevent->cmdlines) {
5551 for (i = 0; i < pevent->cmdline_count; i++)
5552 free(pevent->cmdlines[i].comm);
5553 free(pevent->cmdlines);
5554 }
5555
5556 while (cmdlist) {
5557 cmdnext = cmdlist->next;
5558 free(cmdlist->comm);
5559 free(cmdlist);
5560 cmdlist = cmdnext;
5561 }
5562
5563 if (pevent->func_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005564 for (i = 0; i < (int)pevent->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005565 free(pevent->func_map[i].func);
5566 free(pevent->func_map[i].mod);
5567 }
5568 free(pevent->func_map);
5569 }
5570
5571 while (funclist) {
5572 funcnext = funclist->next;
5573 free(funclist->func);
5574 free(funclist->mod);
5575 free(funclist);
5576 funclist = funcnext;
5577 }
5578
5579 while (pevent->func_handlers) {
5580 func_handler = pevent->func_handlers;
5581 pevent->func_handlers = func_handler->next;
5582 free_func_handle(func_handler);
5583 }
5584
5585 if (pevent->printk_map) {
Arnaldo Carvalho de Melo8a38cce2012-11-09 15:32:00 -03005586 for (i = 0; i < (int)pevent->printk_count; i++)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005587 free(pevent->printk_map[i].printk);
5588 free(pevent->printk_map);
5589 }
5590
5591 while (printklist) {
5592 printknext = printklist->next;
5593 free(printklist->printk);
5594 free(printklist);
5595 printklist = printknext;
5596 }
5597
5598 for (i = 0; i < pevent->nr_events; i++)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03005599 pevent_free_format(pevent->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005600
5601 while (pevent->handlers) {
5602 handle = pevent->handlers;
5603 pevent->handlers = handle->next;
5604 free_handler(handle);
5605 }
5606
5607 free(pevent->events);
5608 free(pevent->sort_events);
5609
5610 free(pevent);
5611}
5612
5613void pevent_unref(struct pevent *pevent)
5614{
5615 pevent_free(pevent);
5616}