Commit | Line | Data |
---|---|---|
f7d82350 SR |
1 | /* |
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 | |
16 | * License along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | * | |
19 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
20 | * | |
21 | * The parts for function graph printing was taken and modified from the | |
22 | * Linux Kernel that were written by | |
23 | * - Copyright (C) 2009 Frederic Weisbecker, | |
24 | * Frederic Weisbecker gave his permission to relicense the code to | |
25 | * the Lesser General Public License. | |
26 | */ | |
27 | #define _GNU_SOURCE | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <stdarg.h> | |
32 | #include <ctype.h> | |
33 | #include <errno.h> | |
34 | ||
35 | #include "event-parse.h" | |
668fe01f | 36 | #include "event-utils.h" |
f7d82350 SR |
37 | |
38 | static const char *input_buf; | |
39 | static unsigned long long input_buf_ptr; | |
40 | static unsigned long long input_buf_siz; | |
41 | ||
5205aec9 TZ |
42 | static int is_flag_field; |
43 | static int is_symbolic_field; | |
44 | ||
f7d82350 SR |
45 | static 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 | ||
53 | static 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 | ||
60 | const char *pevent_get_input_buf(void) | |
61 | { | |
62 | return input_buf; | |
63 | } | |
64 | ||
65 | unsigned long long pevent_get_input_buf_ptr(void) | |
66 | { | |
67 | return input_buf_ptr; | |
68 | } | |
69 | ||
70 | struct 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 | ||
79 | struct pevent_func_params { | |
80 | struct pevent_func_params *next; | |
81 | enum pevent_func_arg_type type; | |
82 | }; | |
83 | ||
84 | struct 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 | ||
93 | static unsigned long long | |
94 | process_defined_func(struct trace_seq *s, void *data, int size, | |
95 | struct event_format *event, struct print_arg *arg); | |
96 | ||
97 | static 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 | */ | |
107 | void pevent_buffer_init(const char *buf, unsigned long long size) | |
108 | { | |
109 | init_input_buf(buf, size); | |
110 | } | |
111 | ||
112 | void breakpoint(void) | |
113 | { | |
114 | static int x; | |
115 | x++; | |
116 | } | |
117 | ||
118 | struct print_arg *alloc_arg(void) | |
119 | { | |
120 | struct print_arg *arg; | |
121 | ||
122 | arg = malloc_or_die(sizeof(*arg)); | |
123 | if (!arg) | |
124 | return NULL; | |
125 | memset(arg, 0, sizeof(*arg)); | |
126 | ||
127 | return arg; | |
128 | } | |
129 | ||
130 | struct cmdline { | |
131 | char *comm; | |
132 | int pid; | |
133 | }; | |
134 | ||
135 | static int cmdline_cmp(const void *a, const void *b) | |
136 | { | |
137 | const struct cmdline *ca = a; | |
138 | const struct cmdline *cb = b; | |
139 | ||
140 | if (ca->pid < cb->pid) | |
141 | return -1; | |
142 | if (ca->pid > cb->pid) | |
143 | return 1; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | struct cmdline_list { | |
149 | struct cmdline_list *next; | |
150 | char *comm; | |
151 | int pid; | |
152 | }; | |
153 | ||
154 | static int cmdline_init(struct pevent *pevent) | |
155 | { | |
156 | struct cmdline_list *cmdlist = pevent->cmdlist; | |
157 | struct cmdline_list *item; | |
158 | struct cmdline *cmdlines; | |
159 | int i; | |
160 | ||
161 | cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count); | |
162 | ||
163 | i = 0; | |
164 | while (cmdlist) { | |
165 | cmdlines[i].pid = cmdlist->pid; | |
166 | cmdlines[i].comm = cmdlist->comm; | |
167 | i++; | |
168 | item = cmdlist; | |
169 | cmdlist = cmdlist->next; | |
170 | free(item); | |
171 | } | |
172 | ||
173 | qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); | |
174 | ||
175 | pevent->cmdlines = cmdlines; | |
176 | pevent->cmdlist = NULL; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static char *find_cmdline(struct pevent *pevent, int pid) | |
182 | { | |
183 | const struct cmdline *comm; | |
184 | struct cmdline key; | |
185 | ||
186 | if (!pid) | |
187 | return "<idle>"; | |
188 | ||
189 | if (!pevent->cmdlines) | |
190 | cmdline_init(pevent); | |
191 | ||
192 | key.pid = pid; | |
193 | ||
194 | comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, | |
195 | sizeof(*pevent->cmdlines), cmdline_cmp); | |
196 | ||
197 | if (comm) | |
198 | return comm->comm; | |
199 | return "<...>"; | |
200 | } | |
201 | ||
202 | /** | |
203 | * pevent_pid_is_registered - return if a pid has a cmdline registered | |
204 | * @pevent: handle for the pevent | |
205 | * @pid: The pid to check if it has a cmdline registered with. | |
206 | * | |
207 | * Returns 1 if the pid has a cmdline mapped to it | |
208 | * 0 otherwise. | |
209 | */ | |
210 | int pevent_pid_is_registered(struct pevent *pevent, int pid) | |
211 | { | |
212 | const struct cmdline *comm; | |
213 | struct cmdline key; | |
214 | ||
215 | if (!pid) | |
216 | return 1; | |
217 | ||
218 | if (!pevent->cmdlines) | |
219 | cmdline_init(pevent); | |
220 | ||
221 | key.pid = pid; | |
222 | ||
223 | comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, | |
224 | sizeof(*pevent->cmdlines), cmdline_cmp); | |
225 | ||
226 | if (comm) | |
227 | return 1; | |
228 | return 0; | |
229 | } | |
230 | ||
231 | /* | |
232 | * If the command lines have been converted to an array, then | |
233 | * we must add this pid. This is much slower than when cmdlines | |
234 | * are added before the array is initialized. | |
235 | */ | |
236 | static int add_new_comm(struct pevent *pevent, const char *comm, int pid) | |
237 | { | |
238 | struct cmdline *cmdlines = pevent->cmdlines; | |
239 | const struct cmdline *cmdline; | |
240 | struct cmdline key; | |
241 | ||
242 | if (!pid) | |
243 | return 0; | |
244 | ||
245 | /* avoid duplicates */ | |
246 | key.pid = pid; | |
247 | ||
248 | cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, | |
249 | sizeof(*pevent->cmdlines), cmdline_cmp); | |
250 | if (cmdline) { | |
251 | errno = EEXIST; | |
252 | return -1; | |
253 | } | |
254 | ||
255 | cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); | |
256 | if (!cmdlines) { | |
257 | errno = ENOMEM; | |
258 | return -1; | |
259 | } | |
260 | ||
261 | cmdlines[pevent->cmdline_count].pid = pid; | |
262 | cmdlines[pevent->cmdline_count].comm = strdup(comm); | |
263 | if (!cmdlines[pevent->cmdline_count].comm) | |
264 | die("malloc comm"); | |
265 | ||
266 | if (cmdlines[pevent->cmdline_count].comm) | |
267 | pevent->cmdline_count++; | |
268 | ||
269 | qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); | |
270 | pevent->cmdlines = cmdlines; | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | /** | |
276 | * pevent_register_comm - register a pid / comm mapping | |
277 | * @pevent: handle for the pevent | |
278 | * @comm: the command line to register | |
279 | * @pid: the pid to map the command line to | |
280 | * | |
281 | * This adds a mapping to search for command line names with | |
282 | * a given pid. The comm is duplicated. | |
283 | */ | |
284 | int pevent_register_comm(struct pevent *pevent, const char *comm, int pid) | |
285 | { | |
286 | struct cmdline_list *item; | |
287 | ||
288 | if (pevent->cmdlines) | |
289 | return add_new_comm(pevent, comm, pid); | |
290 | ||
291 | item = malloc_or_die(sizeof(*item)); | |
292 | item->comm = strdup(comm); | |
293 | if (!item->comm) | |
294 | die("malloc comm"); | |
295 | item->pid = pid; | |
296 | item->next = pevent->cmdlist; | |
297 | ||
298 | pevent->cmdlist = item; | |
299 | pevent->cmdline_count++; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | struct func_map { | |
305 | unsigned long long addr; | |
306 | char *func; | |
307 | char *mod; | |
308 | }; | |
309 | ||
310 | struct func_list { | |
311 | struct func_list *next; | |
312 | unsigned long long addr; | |
313 | char *func; | |
314 | char *mod; | |
315 | }; | |
316 | ||
317 | static int func_cmp(const void *a, const void *b) | |
318 | { | |
319 | const struct func_map *fa = a; | |
320 | const struct func_map *fb = b; | |
321 | ||
322 | if (fa->addr < fb->addr) | |
323 | return -1; | |
324 | if (fa->addr > fb->addr) | |
325 | return 1; | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | /* | |
331 | * We are searching for a record in between, not an exact | |
332 | * match. | |
333 | */ | |
334 | static int func_bcmp(const void *a, const void *b) | |
335 | { | |
336 | const struct func_map *fa = a; | |
337 | const struct func_map *fb = b; | |
338 | ||
339 | if ((fa->addr == fb->addr) || | |
340 | ||
341 | (fa->addr > fb->addr && | |
342 | fa->addr < (fb+1)->addr)) | |
343 | return 0; | |
344 | ||
345 | if (fa->addr < fb->addr) | |
346 | return -1; | |
347 | ||
348 | return 1; | |
349 | } | |
350 | ||
351 | static int func_map_init(struct pevent *pevent) | |
352 | { | |
353 | struct func_list *funclist; | |
354 | struct func_list *item; | |
355 | struct func_map *func_map; | |
356 | int i; | |
357 | ||
358 | func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1)); | |
359 | funclist = pevent->funclist; | |
360 | ||
361 | i = 0; | |
362 | while (funclist) { | |
363 | func_map[i].func = funclist->func; | |
364 | func_map[i].addr = funclist->addr; | |
365 | func_map[i].mod = funclist->mod; | |
366 | i++; | |
367 | item = funclist; | |
368 | funclist = funclist->next; | |
369 | free(item); | |
370 | } | |
371 | ||
372 | qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp); | |
373 | ||
374 | /* | |
375 | * Add a special record at the end. | |
376 | */ | |
377 | func_map[pevent->func_count].func = NULL; | |
378 | func_map[pevent->func_count].addr = 0; | |
379 | func_map[pevent->func_count].mod = NULL; | |
380 | ||
381 | pevent->func_map = func_map; | |
382 | pevent->funclist = NULL; | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static struct func_map * | |
388 | find_func(struct pevent *pevent, unsigned long long addr) | |
389 | { | |
390 | struct func_map *func; | |
391 | struct func_map key; | |
392 | ||
393 | if (!pevent->func_map) | |
394 | func_map_init(pevent); | |
395 | ||
396 | key.addr = addr; | |
397 | ||
398 | func = bsearch(&key, pevent->func_map, pevent->func_count, | |
399 | sizeof(*pevent->func_map), func_bcmp); | |
400 | ||
401 | return func; | |
402 | } | |
403 | ||
404 | /** | |
405 | * pevent_find_function - find a function by a given address | |
406 | * @pevent: handle for the pevent | |
407 | * @addr: the address to find the function with | |
408 | * | |
409 | * Returns a pointer to the function stored that has the given | |
410 | * address. Note, the address does not have to be exact, it | |
411 | * will select the function that would contain the address. | |
412 | */ | |
413 | const char *pevent_find_function(struct pevent *pevent, unsigned long long addr) | |
414 | { | |
415 | struct func_map *map; | |
416 | ||
417 | map = find_func(pevent, addr); | |
418 | if (!map) | |
419 | return NULL; | |
420 | ||
421 | return map->func; | |
422 | } | |
423 | ||
424 | /** | |
425 | * pevent_find_function_address - find a function address by a given address | |
426 | * @pevent: handle for the pevent | |
427 | * @addr: the address to find the function with | |
428 | * | |
429 | * Returns the address the function starts at. This can be used in | |
430 | * conjunction with pevent_find_function to print both the function | |
431 | * name and the function offset. | |
432 | */ | |
433 | unsigned long long | |
434 | pevent_find_function_address(struct pevent *pevent, unsigned long long addr) | |
435 | { | |
436 | struct func_map *map; | |
437 | ||
438 | map = find_func(pevent, addr); | |
439 | if (!map) | |
440 | return 0; | |
441 | ||
442 | return map->addr; | |
443 | } | |
444 | ||
445 | /** | |
446 | * pevent_register_function - register a function with a given address | |
447 | * @pevent: handle for the pevent | |
448 | * @function: the function name to register | |
449 | * @addr: the address the function starts at | |
450 | * @mod: the kernel module the function may be in (NULL for none) | |
451 | * | |
452 | * This registers a function name with an address and module. | |
453 | * The @func passed in is duplicated. | |
454 | */ | |
455 | int pevent_register_function(struct pevent *pevent, char *func, | |
456 | unsigned long long addr, char *mod) | |
457 | { | |
458 | struct func_list *item; | |
459 | ||
460 | item = malloc_or_die(sizeof(*item)); | |
461 | ||
462 | item->next = pevent->funclist; | |
463 | item->func = strdup(func); | |
464 | if (mod) | |
465 | item->mod = strdup(mod); | |
466 | else | |
467 | item->mod = NULL; | |
468 | item->addr = addr; | |
469 | ||
470 | pevent->funclist = item; | |
471 | ||
472 | pevent->func_count++; | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | /** | |
478 | * pevent_print_funcs - print out the stored functions | |
479 | * @pevent: handle for the pevent | |
480 | * | |
481 | * This prints out the stored functions. | |
482 | */ | |
483 | void pevent_print_funcs(struct pevent *pevent) | |
484 | { | |
485 | int i; | |
486 | ||
487 | if (!pevent->func_map) | |
488 | func_map_init(pevent); | |
489 | ||
490 | for (i = 0; i < (int)pevent->func_count; i++) { | |
491 | printf("%016llx %s", | |
492 | pevent->func_map[i].addr, | |
493 | pevent->func_map[i].func); | |
494 | if (pevent->func_map[i].mod) | |
495 | printf(" [%s]\n", pevent->func_map[i].mod); | |
496 | else | |
497 | printf("\n"); | |
498 | } | |
499 | } | |
500 | ||
501 | struct printk_map { | |
502 | unsigned long long addr; | |
503 | char *printk; | |
504 | }; | |
505 | ||
506 | struct printk_list { | |
507 | struct printk_list *next; | |
508 | unsigned long long addr; | |
509 | char *printk; | |
510 | }; | |
511 | ||
512 | static int printk_cmp(const void *a, const void *b) | |
513 | { | |
514 | const struct func_map *fa = a; | |
515 | const struct func_map *fb = b; | |
516 | ||
517 | if (fa->addr < fb->addr) | |
518 | return -1; | |
519 | if (fa->addr > fb->addr) | |
520 | return 1; | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
525 | static void printk_map_init(struct pevent *pevent) | |
526 | { | |
527 | struct printk_list *printklist; | |
528 | struct printk_list *item; | |
529 | struct printk_map *printk_map; | |
530 | int i; | |
531 | ||
532 | printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1)); | |
533 | ||
534 | printklist = pevent->printklist; | |
535 | ||
536 | i = 0; | |
537 | while (printklist) { | |
538 | printk_map[i].printk = printklist->printk; | |
539 | printk_map[i].addr = printklist->addr; | |
540 | i++; | |
541 | item = printklist; | |
542 | printklist = printklist->next; | |
543 | free(item); | |
544 | } | |
545 | ||
546 | qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp); | |
547 | ||
548 | pevent->printk_map = printk_map; | |
549 | pevent->printklist = NULL; | |
550 | } | |
551 | ||
552 | static struct printk_map * | |
553 | find_printk(struct pevent *pevent, unsigned long long addr) | |
554 | { | |
555 | struct printk_map *printk; | |
556 | struct printk_map key; | |
557 | ||
558 | if (!pevent->printk_map) | |
559 | printk_map_init(pevent); | |
560 | ||
561 | key.addr = addr; | |
562 | ||
563 | printk = bsearch(&key, pevent->printk_map, pevent->printk_count, | |
564 | sizeof(*pevent->printk_map), printk_cmp); | |
565 | ||
566 | return printk; | |
567 | } | |
568 | ||
569 | /** | |
570 | * pevent_register_print_string - register a string by its address | |
571 | * @pevent: handle for the pevent | |
572 | * @fmt: the string format to register | |
573 | * @addr: the address the string was located at | |
574 | * | |
575 | * This registers a string by the address it was stored in the kernel. | |
576 | * The @fmt passed in is duplicated. | |
577 | */ | |
578 | int pevent_register_print_string(struct pevent *pevent, char *fmt, | |
579 | unsigned long long addr) | |
580 | { | |
581 | struct printk_list *item; | |
582 | ||
583 | item = malloc_or_die(sizeof(*item)); | |
584 | ||
585 | item->next = pevent->printklist; | |
586 | pevent->printklist = item; | |
587 | item->printk = strdup(fmt); | |
588 | item->addr = addr; | |
589 | ||
590 | pevent->printk_count++; | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | /** | |
596 | * pevent_print_printk - print out the stored strings | |
597 | * @pevent: handle for the pevent | |
598 | * | |
599 | * This prints the string formats that were stored. | |
600 | */ | |
601 | void pevent_print_printk(struct pevent *pevent) | |
602 | { | |
603 | int i; | |
604 | ||
605 | if (!pevent->printk_map) | |
606 | printk_map_init(pevent); | |
607 | ||
608 | for (i = 0; i < (int)pevent->printk_count; i++) { | |
609 | printf("%016llx %s\n", | |
610 | pevent->printk_map[i].addr, | |
611 | pevent->printk_map[i].printk); | |
612 | } | |
613 | } | |
614 | ||
615 | static struct event_format *alloc_event(void) | |
616 | { | |
617 | struct event_format *event; | |
618 | ||
619 | event = malloc_or_die(sizeof(*event)); | |
620 | memset(event, 0, sizeof(*event)); | |
621 | ||
622 | return event; | |
623 | } | |
624 | ||
625 | static void add_event(struct pevent *pevent, struct event_format *event) | |
626 | { | |
627 | int i; | |
628 | ||
629 | if (!pevent->events) | |
630 | pevent->events = malloc_or_die(sizeof(event)); | |
631 | else | |
632 | pevent->events = | |
633 | realloc(pevent->events, sizeof(event) * | |
634 | (pevent->nr_events + 1)); | |
635 | if (!pevent->events) | |
636 | die("Can not allocate events"); | |
637 | ||
638 | for (i = 0; i < pevent->nr_events; i++) { | |
639 | if (pevent->events[i]->id > event->id) | |
640 | break; | |
641 | } | |
642 | if (i < pevent->nr_events) | |
643 | memmove(&pevent->events[i + 1], | |
644 | &pevent->events[i], | |
645 | sizeof(event) * (pevent->nr_events - i)); | |
646 | ||
647 | pevent->events[i] = event; | |
648 | pevent->nr_events++; | |
649 | ||
650 | event->pevent = pevent; | |
651 | } | |
652 | ||
653 | static int event_item_type(enum event_type type) | |
654 | { | |
655 | switch (type) { | |
656 | case EVENT_ITEM ... EVENT_SQUOTE: | |
657 | return 1; | |
658 | case EVENT_ERROR ... EVENT_DELIM: | |
659 | default: | |
660 | return 0; | |
661 | } | |
662 | } | |
663 | ||
664 | static void free_flag_sym(struct print_flag_sym *fsym) | |
665 | { | |
666 | struct print_flag_sym *next; | |
667 | ||
668 | while (fsym) { | |
669 | next = fsym->next; | |
670 | free(fsym->value); | |
671 | free(fsym->str); | |
672 | free(fsym); | |
673 | fsym = next; | |
674 | } | |
675 | } | |
676 | ||
677 | static void free_arg(struct print_arg *arg) | |
678 | { | |
679 | struct print_arg *farg; | |
680 | ||
681 | if (!arg) | |
682 | return; | |
683 | ||
684 | switch (arg->type) { | |
685 | case PRINT_ATOM: | |
686 | free(arg->atom.atom); | |
687 | break; | |
688 | case PRINT_FIELD: | |
689 | free(arg->field.name); | |
690 | break; | |
691 | case PRINT_FLAGS: | |
692 | free_arg(arg->flags.field); | |
693 | free(arg->flags.delim); | |
694 | free_flag_sym(arg->flags.flags); | |
695 | break; | |
696 | case PRINT_SYMBOL: | |
697 | free_arg(arg->symbol.field); | |
698 | free_flag_sym(arg->symbol.symbols); | |
699 | break; | |
700 | case PRINT_TYPE: | |
701 | free(arg->typecast.type); | |
702 | free_arg(arg->typecast.item); | |
703 | break; | |
704 | case PRINT_STRING: | |
705 | case PRINT_BSTRING: | |
706 | free(arg->string.string); | |
707 | break; | |
708 | case PRINT_DYNAMIC_ARRAY: | |
709 | free(arg->dynarray.index); | |
710 | break; | |
711 | case PRINT_OP: | |
712 | free(arg->op.op); | |
713 | free_arg(arg->op.left); | |
714 | free_arg(arg->op.right); | |
715 | break; | |
716 | case PRINT_FUNC: | |
717 | while (arg->func.args) { | |
718 | farg = arg->func.args; | |
719 | arg->func.args = farg->next; | |
720 | free_arg(farg); | |
721 | } | |
722 | break; | |
723 | ||
724 | case PRINT_NULL: | |
725 | default: | |
726 | break; | |
727 | } | |
728 | ||
729 | free(arg); | |
730 | } | |
731 | ||
732 | static enum event_type get_type(int ch) | |
733 | { | |
734 | if (ch == '\n') | |
735 | return EVENT_NEWLINE; | |
736 | if (isspace(ch)) | |
737 | return EVENT_SPACE; | |
738 | if (isalnum(ch) || ch == '_') | |
739 | return EVENT_ITEM; | |
740 | if (ch == '\'') | |
741 | return EVENT_SQUOTE; | |
742 | if (ch == '"') | |
743 | return EVENT_DQUOTE; | |
744 | if (!isprint(ch)) | |
745 | return EVENT_NONE; | |
746 | if (ch == '(' || ch == ')' || ch == ',') | |
747 | return EVENT_DELIM; | |
748 | ||
749 | return EVENT_OP; | |
750 | } | |
751 | ||
752 | static int __read_char(void) | |
753 | { | |
754 | if (input_buf_ptr >= input_buf_siz) | |
755 | return -1; | |
756 | ||
757 | return input_buf[input_buf_ptr++]; | |
758 | } | |
759 | ||
760 | static int __peek_char(void) | |
761 | { | |
762 | if (input_buf_ptr >= input_buf_siz) | |
763 | return -1; | |
764 | ||
765 | return input_buf[input_buf_ptr]; | |
766 | } | |
767 | ||
768 | /** | |
769 | * pevent_peek_char - peek at the next character that will be read | |
770 | * | |
771 | * Returns the next character read, or -1 if end of buffer. | |
772 | */ | |
773 | int pevent_peek_char(void) | |
774 | { | |
775 | return __peek_char(); | |
776 | } | |
777 | ||
778 | static enum event_type force_token(const char *str, char **tok); | |
779 | ||
780 | static enum event_type __read_token(char **tok) | |
781 | { | |
782 | char buf[BUFSIZ]; | |
783 | int ch, last_ch, quote_ch, next_ch; | |
784 | int i = 0; | |
785 | int tok_size = 0; | |
786 | enum event_type type; | |
787 | ||
788 | *tok = NULL; | |
789 | ||
790 | ||
791 | ch = __read_char(); | |
792 | if (ch < 0) | |
793 | return EVENT_NONE; | |
794 | ||
795 | type = get_type(ch); | |
796 | if (type == EVENT_NONE) | |
797 | return type; | |
798 | ||
799 | buf[i++] = ch; | |
800 | ||
801 | switch (type) { | |
802 | case EVENT_NEWLINE: | |
803 | case EVENT_DELIM: | |
804 | *tok = malloc_or_die(2); | |
805 | (*tok)[0] = ch; | |
806 | (*tok)[1] = 0; | |
807 | return type; | |
808 | ||
809 | case EVENT_OP: | |
810 | switch (ch) { | |
811 | case '-': | |
812 | next_ch = __peek_char(); | |
813 | if (next_ch == '>') { | |
814 | buf[i++] = __read_char(); | |
815 | break; | |
816 | } | |
817 | /* fall through */ | |
818 | case '+': | |
819 | case '|': | |
820 | case '&': | |
821 | case '>': | |
822 | case '<': | |
823 | last_ch = ch; | |
824 | ch = __peek_char(); | |
825 | if (ch != last_ch) | |
826 | goto test_equal; | |
827 | buf[i++] = __read_char(); | |
828 | switch (last_ch) { | |
829 | case '>': | |
830 | case '<': | |
831 | goto test_equal; | |
832 | default: | |
833 | break; | |
834 | } | |
835 | break; | |
836 | case '!': | |
837 | case '=': | |
838 | goto test_equal; | |
839 | default: /* what should we do instead? */ | |
840 | break; | |
841 | } | |
842 | buf[i] = 0; | |
843 | *tok = strdup(buf); | |
844 | return type; | |
845 | ||
846 | test_equal: | |
847 | ch = __peek_char(); | |
848 | if (ch == '=') | |
849 | buf[i++] = __read_char(); | |
850 | goto out; | |
851 | ||
852 | case EVENT_DQUOTE: | |
853 | case EVENT_SQUOTE: | |
854 | /* don't keep quotes */ | |
855 | i--; | |
856 | quote_ch = ch; | |
857 | last_ch = 0; | |
858 | concat: | |
859 | do { | |
860 | if (i == (BUFSIZ - 1)) { | |
861 | buf[i] = 0; | |
862 | if (*tok) { | |
863 | *tok = realloc(*tok, tok_size + BUFSIZ); | |
864 | if (!*tok) | |
865 | return EVENT_NONE; | |
866 | strcat(*tok, buf); | |
867 | } else | |
868 | *tok = strdup(buf); | |
869 | ||
870 | if (!*tok) | |
871 | return EVENT_NONE; | |
872 | tok_size += BUFSIZ; | |
873 | i = 0; | |
874 | } | |
875 | last_ch = ch; | |
876 | ch = __read_char(); | |
877 | buf[i++] = ch; | |
878 | /* the '\' '\' will cancel itself */ | |
879 | if (ch == '\\' && last_ch == '\\') | |
880 | last_ch = 0; | |
881 | } while (ch != quote_ch || last_ch == '\\'); | |
882 | /* remove the last quote */ | |
883 | i--; | |
884 | ||
885 | /* | |
886 | * For strings (double quotes) check the next token. | |
887 | * If it is another string, concatinate the two. | |
888 | */ | |
889 | if (type == EVENT_DQUOTE) { | |
890 | unsigned long long save_input_buf_ptr = input_buf_ptr; | |
891 | ||
892 | do { | |
893 | ch = __read_char(); | |
894 | } while (isspace(ch)); | |
895 | if (ch == '"') | |
896 | goto concat; | |
897 | input_buf_ptr = save_input_buf_ptr; | |
898 | } | |
899 | ||
900 | goto out; | |
901 | ||
902 | case EVENT_ERROR ... EVENT_SPACE: | |
903 | case EVENT_ITEM: | |
904 | default: | |
905 | break; | |
906 | } | |
907 | ||
908 | while (get_type(__peek_char()) == type) { | |
909 | if (i == (BUFSIZ - 1)) { | |
910 | buf[i] = 0; | |
911 | if (*tok) { | |
912 | *tok = realloc(*tok, tok_size + BUFSIZ); | |
913 | if (!*tok) | |
914 | return EVENT_NONE; | |
915 | strcat(*tok, buf); | |
916 | } else | |
917 | *tok = strdup(buf); | |
918 | ||
919 | if (!*tok) | |
920 | return EVENT_NONE; | |
921 | tok_size += BUFSIZ; | |
922 | i = 0; | |
923 | } | |
924 | ch = __read_char(); | |
925 | buf[i++] = ch; | |
926 | } | |
927 | ||
928 | out: | |
929 | buf[i] = 0; | |
930 | if (*tok) { | |
931 | *tok = realloc(*tok, tok_size + i); | |
932 | if (!*tok) | |
933 | return EVENT_NONE; | |
934 | strcat(*tok, buf); | |
935 | } else | |
936 | *tok = strdup(buf); | |
937 | if (!*tok) | |
938 | return EVENT_NONE; | |
939 | ||
940 | if (type == EVENT_ITEM) { | |
941 | /* | |
942 | * Older versions of the kernel has a bug that | |
943 | * creates invalid symbols and will break the mac80211 | |
944 | * parsing. This is a work around to that bug. | |
945 | * | |
946 | * See Linux kernel commit: | |
947 | * 811cb50baf63461ce0bdb234927046131fc7fa8b | |
948 | */ | |
949 | if (strcmp(*tok, "LOCAL_PR_FMT") == 0) { | |
950 | free(*tok); | |
951 | *tok = NULL; | |
952 | return force_token("\"\%s\" ", tok); | |
953 | } else if (strcmp(*tok, "STA_PR_FMT") == 0) { | |
954 | free(*tok); | |
955 | *tok = NULL; | |
956 | return force_token("\" sta:%pM\" ", tok); | |
957 | } else if (strcmp(*tok, "VIF_PR_FMT") == 0) { | |
958 | free(*tok); | |
959 | *tok = NULL; | |
960 | return force_token("\" vif:%p(%d)\" ", tok); | |
961 | } | |
962 | } | |
963 | ||
964 | return type; | |
965 | } | |
966 | ||
967 | static enum event_type force_token(const char *str, char **tok) | |
968 | { | |
969 | const char *save_input_buf; | |
970 | unsigned long long save_input_buf_ptr; | |
971 | unsigned long long save_input_buf_siz; | |
972 | enum event_type type; | |
973 | ||
974 | /* save off the current input pointers */ | |
975 | save_input_buf = input_buf; | |
976 | save_input_buf_ptr = input_buf_ptr; | |
977 | save_input_buf_siz = input_buf_siz; | |
978 | ||
979 | init_input_buf(str, strlen(str)); | |
980 | ||
981 | type = __read_token(tok); | |
982 | ||
983 | /* reset back to original token */ | |
984 | input_buf = save_input_buf; | |
985 | input_buf_ptr = save_input_buf_ptr; | |
986 | input_buf_siz = save_input_buf_siz; | |
987 | ||
988 | return type; | |
989 | } | |
990 | ||
991 | static void free_token(char *tok) | |
992 | { | |
993 | if (tok) | |
994 | free(tok); | |
995 | } | |
996 | ||
997 | static enum event_type read_token(char **tok) | |
998 | { | |
999 | enum event_type type; | |
1000 | ||
1001 | for (;;) { | |
1002 | type = __read_token(tok); | |
1003 | if (type != EVENT_SPACE) | |
1004 | return type; | |
1005 | ||
1006 | free_token(*tok); | |
1007 | } | |
1008 | ||
1009 | /* not reached */ | |
1010 | *tok = NULL; | |
1011 | return EVENT_NONE; | |
1012 | } | |
1013 | ||
1014 | /** | |
1015 | * pevent_read_token - access to utilites to use the pevent parser | |
1016 | * @tok: The token to return | |
1017 | * | |
1018 | * This will parse tokens from the string given by | |
1019 | * pevent_init_data(). | |
1020 | * | |
1021 | * Returns the token type. | |
1022 | */ | |
1023 | enum event_type pevent_read_token(char **tok) | |
1024 | { | |
1025 | return read_token(tok); | |
1026 | } | |
1027 | ||
1028 | /** | |
1029 | * pevent_free_token - free a token returned by pevent_read_token | |
1030 | * @token: the token to free | |
1031 | */ | |
1032 | void pevent_free_token(char *token) | |
1033 | { | |
1034 | free_token(token); | |
1035 | } | |
1036 | ||
1037 | /* no newline */ | |
1038 | static enum event_type read_token_item(char **tok) | |
1039 | { | |
1040 | enum event_type type; | |
1041 | ||
1042 | for (;;) { | |
1043 | type = __read_token(tok); | |
1044 | if (type != EVENT_SPACE && type != EVENT_NEWLINE) | |
1045 | return type; | |
1046 | free_token(*tok); | |
1047 | *tok = NULL; | |
1048 | } | |
1049 | ||
1050 | /* not reached */ | |
1051 | *tok = NULL; | |
1052 | return EVENT_NONE; | |
1053 | } | |
1054 | ||
1055 | static int test_type(enum event_type type, enum event_type expect) | |
1056 | { | |
1057 | if (type != expect) { | |
1058 | do_warning("Error: expected type %d but read %d", | |
1059 | expect, type); | |
1060 | return -1; | |
1061 | } | |
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | static int test_type_token(enum event_type type, const char *token, | |
1066 | enum event_type expect, const char *expect_tok) | |
1067 | { | |
1068 | if (type != expect) { | |
1069 | do_warning("Error: expected type %d but read %d", | |
1070 | expect, type); | |
1071 | return -1; | |
1072 | } | |
1073 | ||
1074 | if (strcmp(token, expect_tok) != 0) { | |
1075 | do_warning("Error: expected '%s' but read '%s'", | |
1076 | expect_tok, token); | |
1077 | return -1; | |
1078 | } | |
1079 | return 0; | |
1080 | } | |
1081 | ||
1082 | static int __read_expect_type(enum event_type expect, char **tok, int newline_ok) | |
1083 | { | |
1084 | enum event_type type; | |
1085 | ||
1086 | if (newline_ok) | |
1087 | type = read_token(tok); | |
1088 | else | |
1089 | type = read_token_item(tok); | |
1090 | return test_type(type, expect); | |
1091 | } | |
1092 | ||
1093 | static int read_expect_type(enum event_type expect, char **tok) | |
1094 | { | |
1095 | return __read_expect_type(expect, tok, 1); | |
1096 | } | |
1097 | ||
1098 | static int __read_expected(enum event_type expect, const char *str, | |
1099 | int newline_ok) | |
1100 | { | |
1101 | enum event_type type; | |
1102 | char *token; | |
1103 | int ret; | |
1104 | ||
1105 | if (newline_ok) | |
1106 | type = read_token(&token); | |
1107 | else | |
1108 | type = read_token_item(&token); | |
1109 | ||
1110 | ret = test_type_token(type, token, expect, str); | |
1111 | ||
1112 | free_token(token); | |
1113 | ||
1114 | return ret; | |
1115 | } | |
1116 | ||
1117 | static int read_expected(enum event_type expect, const char *str) | |
1118 | { | |
1119 | return __read_expected(expect, str, 1); | |
1120 | } | |
1121 | ||
1122 | static int read_expected_item(enum event_type expect, const char *str) | |
1123 | { | |
1124 | return __read_expected(expect, str, 0); | |
1125 | } | |
1126 | ||
1127 | static char *event_read_name(void) | |
1128 | { | |
1129 | char *token; | |
1130 | ||
1131 | if (read_expected(EVENT_ITEM, "name") < 0) | |
1132 | return NULL; | |
1133 | ||
1134 | if (read_expected(EVENT_OP, ":") < 0) | |
1135 | return NULL; | |
1136 | ||
1137 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
1138 | goto fail; | |
1139 | ||
1140 | return token; | |
1141 | ||
1142 | fail: | |
1143 | free_token(token); | |
1144 | return NULL; | |
1145 | } | |
1146 | ||
1147 | static int event_read_id(void) | |
1148 | { | |
1149 | char *token; | |
1150 | int id; | |
1151 | ||
1152 | if (read_expected_item(EVENT_ITEM, "ID") < 0) | |
1153 | return -1; | |
1154 | ||
1155 | if (read_expected(EVENT_OP, ":") < 0) | |
1156 | return -1; | |
1157 | ||
1158 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
1159 | goto fail; | |
1160 | ||
1161 | id = strtoul(token, NULL, 0); | |
1162 | free_token(token); | |
1163 | return id; | |
1164 | ||
1165 | fail: | |
1166 | free_token(token); | |
1167 | return -1; | |
1168 | } | |
1169 | ||
1170 | static int field_is_string(struct format_field *field) | |
1171 | { | |
1172 | if ((field->flags & FIELD_IS_ARRAY) && | |
1173 | (strstr(field->type, "char") || strstr(field->type, "u8") || | |
1174 | strstr(field->type, "s8"))) | |
1175 | return 1; | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | static int field_is_dynamic(struct format_field *field) | |
1181 | { | |
1182 | if (strncmp(field->type, "__data_loc", 10) == 0) | |
1183 | return 1; | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static int field_is_long(struct format_field *field) | |
1189 | { | |
1190 | /* includes long long */ | |
1191 | if (strstr(field->type, "long")) | |
1192 | return 1; | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | ||
1197 | static int event_read_fields(struct event_format *event, struct format_field **fields) | |
1198 | { | |
1199 | struct format_field *field = NULL; | |
1200 | enum event_type type; | |
1201 | char *token; | |
1202 | char *last_token; | |
1203 | int count = 0; | |
1204 | ||
1205 | do { | |
1206 | type = read_token(&token); | |
1207 | if (type == EVENT_NEWLINE) { | |
1208 | free_token(token); | |
1209 | return count; | |
1210 | } | |
1211 | ||
1212 | count++; | |
1213 | ||
1214 | if (test_type_token(type, token, EVENT_ITEM, "field")) | |
1215 | goto fail; | |
1216 | free_token(token); | |
1217 | ||
1218 | type = read_token(&token); | |
1219 | /* | |
1220 | * The ftrace fields may still use the "special" name. | |
1221 | * Just ignore it. | |
1222 | */ | |
1223 | if (event->flags & EVENT_FL_ISFTRACE && | |
1224 | type == EVENT_ITEM && strcmp(token, "special") == 0) { | |
1225 | free_token(token); | |
1226 | type = read_token(&token); | |
1227 | } | |
1228 | ||
1229 | if (test_type_token(type, token, EVENT_OP, ":") < 0) | |
1230 | goto fail; | |
1231 | ||
1232 | free_token(token); | |
1233 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
1234 | goto fail; | |
1235 | ||
1236 | last_token = token; | |
1237 | ||
1238 | field = malloc_or_die(sizeof(*field)); | |
1239 | memset(field, 0, sizeof(*field)); | |
1240 | field->event = event; | |
1241 | ||
1242 | /* read the rest of the type */ | |
1243 | for (;;) { | |
1244 | type = read_token(&token); | |
1245 | if (type == EVENT_ITEM || | |
1246 | (type == EVENT_OP && strcmp(token, "*") == 0) || | |
1247 | /* | |
1248 | * Some of the ftrace fields are broken and have | |
1249 | * an illegal "." in them. | |
1250 | */ | |
1251 | (event->flags & EVENT_FL_ISFTRACE && | |
1252 | type == EVENT_OP && strcmp(token, ".") == 0)) { | |
1253 | ||
1254 | if (strcmp(token, "*") == 0) | |
1255 | field->flags |= FIELD_IS_POINTER; | |
1256 | ||
1257 | if (field->type) { | |
1258 | field->type = realloc(field->type, | |
1259 | strlen(field->type) + | |
1260 | strlen(last_token) + 2); | |
1261 | strcat(field->type, " "); | |
1262 | strcat(field->type, last_token); | |
1263 | free(last_token); | |
1264 | } else | |
1265 | field->type = last_token; | |
1266 | last_token = token; | |
1267 | continue; | |
1268 | } | |
1269 | ||
1270 | break; | |
1271 | } | |
1272 | ||
1273 | if (!field->type) { | |
1274 | die("no type found"); | |
1275 | goto fail; | |
1276 | } | |
1277 | field->name = last_token; | |
1278 | ||
1279 | if (test_type(type, EVENT_OP)) | |
1280 | goto fail; | |
1281 | ||
1282 | if (strcmp(token, "[") == 0) { | |
1283 | enum event_type last_type = type; | |
1284 | char *brackets = token; | |
1285 | int len; | |
1286 | ||
1287 | field->flags |= FIELD_IS_ARRAY; | |
1288 | ||
1289 | type = read_token(&token); | |
1290 | ||
1291 | if (type == EVENT_ITEM) | |
1292 | field->arraylen = strtoul(token, NULL, 0); | |
1293 | else | |
1294 | field->arraylen = 0; | |
1295 | ||
1296 | while (strcmp(token, "]") != 0) { | |
1297 | if (last_type == EVENT_ITEM && | |
1298 | type == EVENT_ITEM) | |
1299 | len = 2; | |
1300 | else | |
1301 | len = 1; | |
1302 | last_type = type; | |
1303 | ||
1304 | brackets = realloc(brackets, | |
1305 | strlen(brackets) + | |
1306 | strlen(token) + len); | |
1307 | if (len == 2) | |
1308 | strcat(brackets, " "); | |
1309 | strcat(brackets, token); | |
1310 | /* We only care about the last token */ | |
1311 | field->arraylen = strtoul(token, NULL, 0); | |
1312 | free_token(token); | |
1313 | type = read_token(&token); | |
1314 | if (type == EVENT_NONE) { | |
1315 | die("failed to find token"); | |
1316 | goto fail; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | free_token(token); | |
1321 | ||
1322 | brackets = realloc(brackets, strlen(brackets) + 2); | |
1323 | strcat(brackets, "]"); | |
1324 | ||
1325 | /* add brackets to type */ | |
1326 | ||
1327 | type = read_token(&token); | |
1328 | /* | |
1329 | * If the next token is not an OP, then it is of | |
1330 | * the format: type [] item; | |
1331 | */ | |
1332 | if (type == EVENT_ITEM) { | |
1333 | field->type = realloc(field->type, | |
1334 | strlen(field->type) + | |
1335 | strlen(field->name) + | |
1336 | strlen(brackets) + 2); | |
1337 | strcat(field->type, " "); | |
1338 | strcat(field->type, field->name); | |
1339 | free_token(field->name); | |
1340 | strcat(field->type, brackets); | |
1341 | field->name = token; | |
1342 | type = read_token(&token); | |
1343 | } else { | |
1344 | field->type = realloc(field->type, | |
1345 | strlen(field->type) + | |
1346 | strlen(brackets) + 1); | |
1347 | strcat(field->type, brackets); | |
1348 | } | |
1349 | free(brackets); | |
1350 | } | |
1351 | ||
1352 | if (field_is_string(field)) | |
1353 | field->flags |= FIELD_IS_STRING; | |
1354 | if (field_is_dynamic(field)) | |
1355 | field->flags |= FIELD_IS_DYNAMIC; | |
1356 | if (field_is_long(field)) | |
1357 | field->flags |= FIELD_IS_LONG; | |
1358 | ||
1359 | if (test_type_token(type, token, EVENT_OP, ";")) | |
1360 | goto fail; | |
1361 | free_token(token); | |
1362 | ||
1363 | if (read_expected(EVENT_ITEM, "offset") < 0) | |
1364 | goto fail_expect; | |
1365 | ||
1366 | if (read_expected(EVENT_OP, ":") < 0) | |
1367 | goto fail_expect; | |
1368 | ||
1369 | if (read_expect_type(EVENT_ITEM, &token)) | |
1370 | goto fail; | |
1371 | field->offset = strtoul(token, NULL, 0); | |
1372 | free_token(token); | |
1373 | ||
1374 | if (read_expected(EVENT_OP, ";") < 0) | |
1375 | goto fail_expect; | |
1376 | ||
1377 | if (read_expected(EVENT_ITEM, "size") < 0) | |
1378 | goto fail_expect; | |
1379 | ||
1380 | if (read_expected(EVENT_OP, ":") < 0) | |
1381 | goto fail_expect; | |
1382 | ||
1383 | if (read_expect_type(EVENT_ITEM, &token)) | |
1384 | goto fail; | |
1385 | field->size = strtoul(token, NULL, 0); | |
1386 | free_token(token); | |
1387 | ||
1388 | if (read_expected(EVENT_OP, ";") < 0) | |
1389 | goto fail_expect; | |
1390 | ||
1391 | type = read_token(&token); | |
1392 | if (type != EVENT_NEWLINE) { | |
1393 | /* newer versions of the kernel have a "signed" type */ | |
1394 | if (test_type_token(type, token, EVENT_ITEM, "signed")) | |
1395 | goto fail; | |
1396 | ||
1397 | free_token(token); | |
1398 | ||
1399 | if (read_expected(EVENT_OP, ":") < 0) | |
1400 | goto fail_expect; | |
1401 | ||
1402 | if (read_expect_type(EVENT_ITEM, &token)) | |
1403 | goto fail; | |
1404 | ||
1405 | /* add signed type */ | |
1406 | ||
1407 | free_token(token); | |
1408 | if (read_expected(EVENT_OP, ";") < 0) | |
1409 | goto fail_expect; | |
1410 | ||
1411 | if (read_expect_type(EVENT_NEWLINE, &token)) | |
1412 | goto fail; | |
1413 | } | |
1414 | ||
1415 | free_token(token); | |
1416 | ||
1417 | if (field->flags & FIELD_IS_ARRAY) { | |
1418 | if (field->arraylen) | |
1419 | field->elementsize = field->size / field->arraylen; | |
1420 | else if (field->flags & FIELD_IS_STRING) | |
1421 | field->elementsize = 1; | |
1422 | else | |
1423 | field->elementsize = event->pevent->long_size; | |
1424 | } else | |
1425 | field->elementsize = field->size; | |
1426 | ||
1427 | *fields = field; | |
1428 | fields = &field->next; | |
1429 | ||
1430 | } while (1); | |
1431 | ||
1432 | return 0; | |
1433 | ||
1434 | fail: | |
1435 | free_token(token); | |
1436 | fail_expect: | |
57d34dc5 NK |
1437 | if (field) { |
1438 | free(field->type); | |
1439 | free(field->name); | |
f7d82350 | 1440 | free(field); |
57d34dc5 | 1441 | } |
f7d82350 SR |
1442 | return -1; |
1443 | } | |
1444 | ||
1445 | static int event_read_format(struct event_format *event) | |
1446 | { | |
1447 | char *token; | |
1448 | int ret; | |
1449 | ||
1450 | if (read_expected_item(EVENT_ITEM, "format") < 0) | |
1451 | return -1; | |
1452 | ||
1453 | if (read_expected(EVENT_OP, ":") < 0) | |
1454 | return -1; | |
1455 | ||
1456 | if (read_expect_type(EVENT_NEWLINE, &token)) | |
1457 | goto fail; | |
1458 | free_token(token); | |
1459 | ||
1460 | ret = event_read_fields(event, &event->format.common_fields); | |
1461 | if (ret < 0) | |
1462 | return ret; | |
1463 | event->format.nr_common = ret; | |
1464 | ||
1465 | ret = event_read_fields(event, &event->format.fields); | |
1466 | if (ret < 0) | |
1467 | return ret; | |
1468 | event->format.nr_fields = ret; | |
1469 | ||
1470 | return 0; | |
1471 | ||
1472 | fail: | |
1473 | free_token(token); | |
1474 | return -1; | |
1475 | } | |
1476 | ||
1477 | static enum event_type | |
1478 | process_arg_token(struct event_format *event, struct print_arg *arg, | |
1479 | char **tok, enum event_type type); | |
1480 | ||
1481 | static enum event_type | |
1482 | process_arg(struct event_format *event, struct print_arg *arg, char **tok) | |
1483 | { | |
1484 | enum event_type type; | |
1485 | char *token; | |
1486 | ||
1487 | type = read_token(&token); | |
1488 | *tok = token; | |
1489 | ||
1490 | return process_arg_token(event, arg, tok, type); | |
1491 | } | |
1492 | ||
1493 | static enum event_type | |
1494 | process_op(struct event_format *event, struct print_arg *arg, char **tok); | |
1495 | ||
1496 | static enum event_type | |
1497 | process_cond(struct event_format *event, struct print_arg *top, char **tok) | |
1498 | { | |
1499 | struct print_arg *arg, *left, *right; | |
1500 | enum event_type type; | |
1501 | char *token = NULL; | |
1502 | ||
1503 | arg = alloc_arg(); | |
1504 | left = alloc_arg(); | |
1505 | right = alloc_arg(); | |
1506 | ||
1507 | arg->type = PRINT_OP; | |
1508 | arg->op.left = left; | |
1509 | arg->op.right = right; | |
1510 | ||
1511 | *tok = NULL; | |
1512 | type = process_arg(event, left, &token); | |
1513 | ||
1514 | again: | |
1515 | /* Handle other operations in the arguments */ | |
1516 | if (type == EVENT_OP && strcmp(token, ":") != 0) { | |
1517 | type = process_op(event, left, &token); | |
1518 | goto again; | |
1519 | } | |
1520 | ||
1521 | if (test_type_token(type, token, EVENT_OP, ":")) | |
1522 | goto out_free; | |
1523 | ||
1524 | arg->op.op = token; | |
1525 | ||
1526 | type = process_arg(event, right, &token); | |
1527 | ||
1528 | top->op.right = arg; | |
1529 | ||
1530 | *tok = token; | |
1531 | return type; | |
1532 | ||
1533 | out_free: | |
1534 | /* Top may point to itself */ | |
1535 | top->op.right = NULL; | |
1536 | free_token(token); | |
1537 | free_arg(arg); | |
1538 | return EVENT_ERROR; | |
1539 | } | |
1540 | ||
1541 | static enum event_type | |
1542 | process_array(struct event_format *event, struct print_arg *top, char **tok) | |
1543 | { | |
1544 | struct print_arg *arg; | |
1545 | enum event_type type; | |
1546 | char *token = NULL; | |
1547 | ||
1548 | arg = alloc_arg(); | |
1549 | ||
1550 | *tok = NULL; | |
1551 | type = process_arg(event, arg, &token); | |
1552 | if (test_type_token(type, token, EVENT_OP, "]")) | |
1553 | goto out_free; | |
1554 | ||
1555 | top->op.right = arg; | |
1556 | ||
1557 | free_token(token); | |
1558 | type = read_token_item(&token); | |
1559 | *tok = token; | |
1560 | ||
1561 | return type; | |
1562 | ||
1563 | out_free: | |
1564 | free_token(*tok); | |
1565 | *tok = NULL; | |
1566 | free_arg(arg); | |
1567 | return EVENT_ERROR; | |
1568 | } | |
1569 | ||
1570 | static int get_op_prio(char *op) | |
1571 | { | |
1572 | if (!op[1]) { | |
1573 | switch (op[0]) { | |
1574 | case '~': | |
1575 | case '!': | |
1576 | return 4; | |
1577 | case '*': | |
1578 | case '/': | |
1579 | case '%': | |
1580 | return 6; | |
1581 | case '+': | |
1582 | case '-': | |
1583 | return 7; | |
1584 | /* '>>' and '<<' are 8 */ | |
1585 | case '<': | |
1586 | case '>': | |
1587 | return 9; | |
1588 | /* '==' and '!=' are 10 */ | |
1589 | case '&': | |
1590 | return 11; | |
1591 | case '^': | |
1592 | return 12; | |
1593 | case '|': | |
1594 | return 13; | |
1595 | case '?': | |
1596 | return 16; | |
1597 | default: | |
14ffde0e | 1598 | do_warning("unknown op '%c'", op[0]); |
f7d82350 SR |
1599 | return -1; |
1600 | } | |
1601 | } else { | |
1602 | if (strcmp(op, "++") == 0 || | |
1603 | strcmp(op, "--") == 0) { | |
1604 | return 3; | |
1605 | } else if (strcmp(op, ">>") == 0 || | |
1606 | strcmp(op, "<<") == 0) { | |
1607 | return 8; | |
1608 | } else if (strcmp(op, ">=") == 0 || | |
1609 | strcmp(op, "<=") == 0) { | |
1610 | return 9; | |
1611 | } else if (strcmp(op, "==") == 0 || | |
1612 | strcmp(op, "!=") == 0) { | |
1613 | return 10; | |
1614 | } else if (strcmp(op, "&&") == 0) { | |
1615 | return 14; | |
1616 | } else if (strcmp(op, "||") == 0) { | |
1617 | return 15; | |
1618 | } else { | |
14ffde0e | 1619 | do_warning("unknown op '%s'", op); |
f7d82350 SR |
1620 | return -1; |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | ||
14ffde0e | 1625 | static int set_op_prio(struct print_arg *arg) |
f7d82350 SR |
1626 | { |
1627 | ||
1628 | /* single ops are the greatest */ | |
14ffde0e | 1629 | if (!arg->op.left || arg->op.left->type == PRINT_NULL) |
f7d82350 | 1630 | arg->op.prio = 0; |
14ffde0e VN |
1631 | else |
1632 | arg->op.prio = get_op_prio(arg->op.op); | |
f7d82350 | 1633 | |
14ffde0e | 1634 | return arg->op.prio; |
f7d82350 SR |
1635 | } |
1636 | ||
1637 | /* Note, *tok does not get freed, but will most likely be saved */ | |
1638 | static enum event_type | |
1639 | process_op(struct event_format *event, struct print_arg *arg, char **tok) | |
1640 | { | |
1641 | struct print_arg *left, *right = NULL; | |
1642 | enum event_type type; | |
1643 | char *token; | |
1644 | ||
1645 | /* the op is passed in via tok */ | |
1646 | token = *tok; | |
1647 | ||
1648 | if (arg->type == PRINT_OP && !arg->op.left) { | |
1649 | /* handle single op */ | |
1650 | if (token[1]) { | |
1651 | die("bad op token %s", token); | |
1652 | goto out_free; | |
1653 | } | |
1654 | switch (token[0]) { | |
1655 | case '~': | |
1656 | case '!': | |
1657 | case '+': | |
1658 | case '-': | |
1659 | break; | |
1660 | default: | |
1661 | do_warning("bad op token %s", token); | |
1662 | goto out_free; | |
1663 | ||
1664 | } | |
1665 | ||
1666 | /* make an empty left */ | |
1667 | left = alloc_arg(); | |
1668 | left->type = PRINT_NULL; | |
1669 | arg->op.left = left; | |
1670 | ||
1671 | right = alloc_arg(); | |
1672 | arg->op.right = right; | |
1673 | ||
1674 | /* do not free the token, it belongs to an op */ | |
1675 | *tok = NULL; | |
1676 | type = process_arg(event, right, tok); | |
1677 | ||
1678 | } else if (strcmp(token, "?") == 0) { | |
1679 | ||
1680 | left = alloc_arg(); | |
1681 | /* copy the top arg to the left */ | |
1682 | *left = *arg; | |
1683 | ||
1684 | arg->type = PRINT_OP; | |
1685 | arg->op.op = token; | |
1686 | arg->op.left = left; | |
1687 | arg->op.prio = 0; | |
1688 | ||
1689 | type = process_cond(event, arg, tok); | |
1690 | ||
1691 | } else if (strcmp(token, ">>") == 0 || | |
1692 | strcmp(token, "<<") == 0 || | |
1693 | strcmp(token, "&") == 0 || | |
1694 | strcmp(token, "|") == 0 || | |
1695 | strcmp(token, "&&") == 0 || | |
1696 | strcmp(token, "||") == 0 || | |
1697 | strcmp(token, "-") == 0 || | |
1698 | strcmp(token, "+") == 0 || | |
1699 | strcmp(token, "*") == 0 || | |
1700 | strcmp(token, "^") == 0 || | |
1701 | strcmp(token, "/") == 0 || | |
1702 | strcmp(token, "<") == 0 || | |
1703 | strcmp(token, ">") == 0 || | |
1704 | strcmp(token, "==") == 0 || | |
1705 | strcmp(token, "!=") == 0) { | |
1706 | ||
1707 | left = alloc_arg(); | |
1708 | ||
1709 | /* copy the top arg to the left */ | |
1710 | *left = *arg; | |
1711 | ||
1712 | arg->type = PRINT_OP; | |
1713 | arg->op.op = token; | |
1714 | arg->op.left = left; | |
1715 | ||
14ffde0e VN |
1716 | if (set_op_prio(arg) == -1) { |
1717 | event->flags |= EVENT_FL_FAILED; | |
d1de1087 NK |
1718 | /* arg->op.op (= token) will be freed at out_free */ |
1719 | arg->op.op = NULL; | |
14ffde0e VN |
1720 | goto out_free; |
1721 | } | |
f7d82350 SR |
1722 | |
1723 | type = read_token_item(&token); | |
1724 | *tok = token; | |
1725 | ||
1726 | /* could just be a type pointer */ | |
1727 | if ((strcmp(arg->op.op, "*") == 0) && | |
1728 | type == EVENT_DELIM && (strcmp(token, ")") == 0)) { | |
1729 | if (left->type != PRINT_ATOM) | |
1730 | die("bad pointer type"); | |
1731 | left->atom.atom = realloc(left->atom.atom, | |
1732 | strlen(left->atom.atom) + 3); | |
1733 | strcat(left->atom.atom, " *"); | |
1734 | free(arg->op.op); | |
1735 | *arg = *left; | |
1736 | free(left); | |
1737 | ||
1738 | return type; | |
1739 | } | |
1740 | ||
1741 | right = alloc_arg(); | |
1742 | type = process_arg_token(event, right, tok, type); | |
1743 | arg->op.right = right; | |
1744 | ||
1745 | } else if (strcmp(token, "[") == 0) { | |
1746 | ||
1747 | left = alloc_arg(); | |
1748 | *left = *arg; | |
1749 | ||
1750 | arg->type = PRINT_OP; | |
1751 | arg->op.op = token; | |
1752 | arg->op.left = left; | |
1753 | ||
1754 | arg->op.prio = 0; | |
1755 | ||
1756 | type = process_array(event, arg, tok); | |
1757 | ||
1758 | } else { | |
1759 | do_warning("unknown op '%s'", token); | |
1760 | event->flags |= EVENT_FL_FAILED; | |
1761 | /* the arg is now the left side */ | |
1762 | goto out_free; | |
1763 | } | |
1764 | ||
1765 | if (type == EVENT_OP && strcmp(*tok, ":") != 0) { | |
1766 | int prio; | |
1767 | ||
1768 | /* higher prios need to be closer to the root */ | |
1769 | prio = get_op_prio(*tok); | |
1770 | ||
1771 | if (prio > arg->op.prio) | |
1772 | return process_op(event, arg, tok); | |
1773 | ||
1774 | return process_op(event, right, tok); | |
1775 | } | |
1776 | ||
1777 | return type; | |
1778 | ||
1779 | out_free: | |
1780 | free_token(token); | |
1781 | *tok = NULL; | |
1782 | return EVENT_ERROR; | |
1783 | } | |
1784 | ||
1785 | static enum event_type | |
1786 | process_entry(struct event_format *event __unused, struct print_arg *arg, | |
1787 | char **tok) | |
1788 | { | |
1789 | enum event_type type; | |
1790 | char *field; | |
1791 | char *token; | |
1792 | ||
1793 | if (read_expected(EVENT_OP, "->") < 0) | |
1794 | goto out_err; | |
1795 | ||
1796 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
1797 | goto out_free; | |
1798 | field = token; | |
1799 | ||
1800 | arg->type = PRINT_FIELD; | |
1801 | arg->field.name = field; | |
1802 | ||
5205aec9 TZ |
1803 | if (is_flag_field) { |
1804 | arg->field.field = pevent_find_any_field(event, arg->field.name); | |
1805 | arg->field.field->flags |= FIELD_IS_FLAG; | |
1806 | is_flag_field = 0; | |
1807 | } else if (is_symbolic_field) { | |
1808 | arg->field.field = pevent_find_any_field(event, arg->field.name); | |
1809 | arg->field.field->flags |= FIELD_IS_SYMBOLIC; | |
1810 | is_symbolic_field = 0; | |
1811 | } | |
1812 | ||
f7d82350 SR |
1813 | type = read_token(&token); |
1814 | *tok = token; | |
1815 | ||
1816 | return type; | |
1817 | ||
1818 | out_free: | |
1819 | free_token(token); | |
1820 | out_err: | |
1821 | *tok = NULL; | |
1822 | return EVENT_ERROR; | |
1823 | } | |
1824 | ||
1825 | static char *arg_eval (struct print_arg *arg); | |
1826 | ||
1827 | static unsigned long long | |
1828 | eval_type_str(unsigned long long val, const char *type, int pointer) | |
1829 | { | |
1830 | int sign = 0; | |
1831 | char *ref; | |
1832 | int len; | |
1833 | ||
1834 | len = strlen(type); | |
1835 | ||
1836 | if (pointer) { | |
1837 | ||
1838 | if (type[len-1] != '*') { | |
1839 | do_warning("pointer expected with non pointer type"); | |
1840 | return val; | |
1841 | } | |
1842 | ||
1843 | ref = malloc_or_die(len); | |
1844 | memcpy(ref, type, len); | |
1845 | ||
1846 | /* chop off the " *" */ | |
1847 | ref[len - 2] = 0; | |
1848 | ||
1849 | val = eval_type_str(val, ref, 0); | |
1850 | free(ref); | |
1851 | return val; | |
1852 | } | |
1853 | ||
1854 | /* check if this is a pointer */ | |
1855 | if (type[len - 1] == '*') | |
1856 | return val; | |
1857 | ||
1858 | /* Try to figure out the arg size*/ | |
1859 | if (strncmp(type, "struct", 6) == 0) | |
1860 | /* all bets off */ | |
1861 | return val; | |
1862 | ||
1863 | if (strcmp(type, "u8") == 0) | |
1864 | return val & 0xff; | |
1865 | ||
1866 | if (strcmp(type, "u16") == 0) | |
1867 | return val & 0xffff; | |
1868 | ||
1869 | if (strcmp(type, "u32") == 0) | |
1870 | return val & 0xffffffff; | |
1871 | ||
1872 | if (strcmp(type, "u64") == 0 || | |
1873 | strcmp(type, "s64")) | |
1874 | return val; | |
1875 | ||
1876 | if (strcmp(type, "s8") == 0) | |
1877 | return (unsigned long long)(char)val & 0xff; | |
1878 | ||
1879 | if (strcmp(type, "s16") == 0) | |
1880 | return (unsigned long long)(short)val & 0xffff; | |
1881 | ||
1882 | if (strcmp(type, "s32") == 0) | |
1883 | return (unsigned long long)(int)val & 0xffffffff; | |
1884 | ||
1885 | if (strncmp(type, "unsigned ", 9) == 0) { | |
1886 | sign = 0; | |
1887 | type += 9; | |
1888 | } | |
1889 | ||
1890 | if (strcmp(type, "char") == 0) { | |
1891 | if (sign) | |
1892 | return (unsigned long long)(char)val & 0xff; | |
1893 | else | |
1894 | return val & 0xff; | |
1895 | } | |
1896 | ||
1897 | if (strcmp(type, "short") == 0) { | |
1898 | if (sign) | |
1899 | return (unsigned long long)(short)val & 0xffff; | |
1900 | else | |
1901 | return val & 0xffff; | |
1902 | } | |
1903 | ||
1904 | if (strcmp(type, "int") == 0) { | |
1905 | if (sign) | |
1906 | return (unsigned long long)(int)val & 0xffffffff; | |
1907 | else | |
1908 | return val & 0xffffffff; | |
1909 | } | |
1910 | ||
1911 | return val; | |
1912 | } | |
1913 | ||
1914 | /* | |
1915 | * Try to figure out the type. | |
1916 | */ | |
1917 | static unsigned long long | |
1918 | eval_type(unsigned long long val, struct print_arg *arg, int pointer) | |
1919 | { | |
1920 | if (arg->type != PRINT_TYPE) | |
1921 | die("expected type argument"); | |
1922 | ||
1923 | return eval_type_str(val, arg->typecast.type, pointer); | |
1924 | } | |
1925 | ||
d69afed5 | 1926 | static int arg_num_eval(struct print_arg *arg, long long *val) |
f7d82350 SR |
1927 | { |
1928 | long long left, right; | |
d69afed5 | 1929 | int ret = 1; |
f7d82350 SR |
1930 | |
1931 | switch (arg->type) { | |
1932 | case PRINT_ATOM: | |
d69afed5 | 1933 | *val = strtoll(arg->atom.atom, NULL, 0); |
f7d82350 SR |
1934 | break; |
1935 | case PRINT_TYPE: | |
d69afed5 VN |
1936 | ret = arg_num_eval(arg->typecast.item, val); |
1937 | if (!ret) | |
1938 | break; | |
1939 | *val = eval_type(*val, arg, 0); | |
f7d82350 SR |
1940 | break; |
1941 | case PRINT_OP: | |
1942 | switch (arg->op.op[0]) { | |
1943 | case '|': | |
d69afed5 VN |
1944 | ret = arg_num_eval(arg->op.left, &left); |
1945 | if (!ret) | |
1946 | break; | |
1947 | ret = arg_num_eval(arg->op.right, &right); | |
1948 | if (!ret) | |
1949 | break; | |
f7d82350 | 1950 | if (arg->op.op[1]) |
d69afed5 | 1951 | *val = left || right; |
f7d82350 | 1952 | else |
d69afed5 | 1953 | *val = left | right; |
f7d82350 SR |
1954 | break; |
1955 | case '&': | |
d69afed5 VN |
1956 | ret = arg_num_eval(arg->op.left, &left); |
1957 | if (!ret) | |
1958 | break; | |
1959 | ret = arg_num_eval(arg->op.right, &right); | |
1960 | if (!ret) | |
1961 | break; | |
f7d82350 | 1962 | if (arg->op.op[1]) |
d69afed5 | 1963 | *val = left && right; |
f7d82350 | 1964 | else |
d69afed5 | 1965 | *val = left & right; |
f7d82350 SR |
1966 | break; |
1967 | case '<': | |
d69afed5 VN |
1968 | ret = arg_num_eval(arg->op.left, &left); |
1969 | if (!ret) | |
1970 | break; | |
1971 | ret = arg_num_eval(arg->op.right, &right); | |
1972 | if (!ret) | |
1973 | break; | |
f7d82350 SR |
1974 | switch (arg->op.op[1]) { |
1975 | case 0: | |
d69afed5 | 1976 | *val = left < right; |
f7d82350 SR |
1977 | break; |
1978 | case '<': | |
d69afed5 | 1979 | *val = left << right; |
f7d82350 SR |
1980 | break; |
1981 | case '=': | |
d69afed5 | 1982 | *val = left <= right; |
f7d82350 SR |
1983 | break; |
1984 | default: | |
d69afed5 VN |
1985 | do_warning("unknown op '%s'", arg->op.op); |
1986 | ret = 0; | |
f7d82350 SR |
1987 | } |
1988 | break; | |
1989 | case '>': | |
d69afed5 VN |
1990 | ret = arg_num_eval(arg->op.left, &left); |
1991 | if (!ret) | |
1992 | break; | |
1993 | ret = arg_num_eval(arg->op.right, &right); | |
1994 | if (!ret) | |
1995 | break; | |
f7d82350 SR |
1996 | switch (arg->op.op[1]) { |
1997 | case 0: | |
d69afed5 | 1998 | *val = left > right; |
f7d82350 SR |
1999 | break; |
2000 | case '>': | |
d69afed5 | 2001 | *val = left >> right; |
f7d82350 SR |
2002 | break; |
2003 | case '=': | |
d69afed5 | 2004 | *val = left >= right; |
f7d82350 SR |
2005 | break; |
2006 | default: | |
d69afed5 VN |
2007 | do_warning("unknown op '%s'", arg->op.op); |
2008 | ret = 0; | |
f7d82350 SR |
2009 | } |
2010 | break; | |
2011 | case '=': | |
d69afed5 VN |
2012 | ret = arg_num_eval(arg->op.left, &left); |
2013 | if (!ret) | |
2014 | break; | |
2015 | ret = arg_num_eval(arg->op.right, &right); | |
2016 | if (!ret) | |
2017 | break; | |
f7d82350 | 2018 | |
d69afed5 VN |
2019 | if (arg->op.op[1] != '=') { |
2020 | do_warning("unknown op '%s'", arg->op.op); | |
2021 | ret = 0; | |
2022 | } else | |
2023 | *val = left == right; | |
f7d82350 SR |
2024 | break; |
2025 | case '!': | |
d69afed5 VN |
2026 | ret = arg_num_eval(arg->op.left, &left); |
2027 | if (!ret) | |
2028 | break; | |
2029 | ret = arg_num_eval(arg->op.right, &right); | |
2030 | if (!ret) | |
2031 | break; | |
f7d82350 SR |
2032 | |
2033 | switch (arg->op.op[1]) { | |
2034 | case '=': | |
d69afed5 | 2035 | *val = left != right; |
f7d82350 SR |
2036 | break; |
2037 | default: | |
d69afed5 VN |
2038 | do_warning("unknown op '%s'", arg->op.op); |
2039 | ret = 0; | |
f7d82350 SR |
2040 | } |
2041 | break; | |
2042 | case '-': | |
2043 | /* check for negative */ | |
2044 | if (arg->op.left->type == PRINT_NULL) | |
2045 | left = 0; | |
2046 | else | |
d69afed5 VN |
2047 | ret = arg_num_eval(arg->op.left, &left); |
2048 | if (!ret) | |
2049 | break; | |
2050 | ret = arg_num_eval(arg->op.right, &right); | |
2051 | if (!ret) | |
2052 | break; | |
2053 | *val = left - right; | |
f7d82350 | 2054 | break; |
b4828598 VN |
2055 | case '+': |
2056 | if (arg->op.left->type == PRINT_NULL) | |
2057 | left = 0; | |
2058 | else | |
2059 | ret = arg_num_eval(arg->op.left, &left); | |
2060 | if (!ret) | |
2061 | break; | |
2062 | ret = arg_num_eval(arg->op.right, &right); | |
2063 | if (!ret) | |
2064 | break; | |
2065 | *val = left + right; | |
2066 | break; | |
f7d82350 | 2067 | default: |
d69afed5 VN |
2068 | do_warning("unknown op '%s'", arg->op.op); |
2069 | ret = 0; | |
f7d82350 SR |
2070 | } |
2071 | break; | |
2072 | ||
2073 | case PRINT_NULL: | |
2074 | case PRINT_FIELD ... PRINT_SYMBOL: | |
2075 | case PRINT_STRING: | |
2076 | case PRINT_BSTRING: | |
2077 | default: | |
d69afed5 VN |
2078 | do_warning("invalid eval type %d", arg->type); |
2079 | ret = 0; | |
f7d82350 SR |
2080 | |
2081 | } | |
d69afed5 | 2082 | return ret; |
f7d82350 SR |
2083 | } |
2084 | ||
2085 | static char *arg_eval (struct print_arg *arg) | |
2086 | { | |
2087 | long long val; | |
2088 | static char buf[20]; | |
2089 | ||
2090 | switch (arg->type) { | |
2091 | case PRINT_ATOM: | |
2092 | return arg->atom.atom; | |
2093 | case PRINT_TYPE: | |
2094 | return arg_eval(arg->typecast.item); | |
2095 | case PRINT_OP: | |
d69afed5 VN |
2096 | if (!arg_num_eval(arg, &val)) |
2097 | break; | |
f7d82350 SR |
2098 | sprintf(buf, "%lld", val); |
2099 | return buf; | |
2100 | ||
2101 | case PRINT_NULL: | |
2102 | case PRINT_FIELD ... PRINT_SYMBOL: | |
2103 | case PRINT_STRING: | |
2104 | case PRINT_BSTRING: | |
2105 | default: | |
2106 | die("invalid eval type %d", arg->type); | |
2107 | break; | |
2108 | } | |
2109 | ||
2110 | return NULL; | |
2111 | } | |
2112 | ||
2113 | static enum event_type | |
2114 | process_fields(struct event_format *event, struct print_flag_sym **list, char **tok) | |
2115 | { | |
2116 | enum event_type type; | |
2117 | struct print_arg *arg = NULL; | |
2118 | struct print_flag_sym *field; | |
2119 | char *token = *tok; | |
2120 | char *value; | |
2121 | ||
2122 | do { | |
2123 | free_token(token); | |
2124 | type = read_token_item(&token); | |
2125 | if (test_type_token(type, token, EVENT_OP, "{")) | |
2126 | break; | |
2127 | ||
2128 | arg = alloc_arg(); | |
2129 | ||
2130 | free_token(token); | |
2131 | type = process_arg(event, arg, &token); | |
00b9da72 SH |
2132 | |
2133 | if (type == EVENT_OP) | |
2134 | type = process_op(event, arg, &token); | |
2135 | ||
2136 | if (type == EVENT_ERROR) | |
2137 | goto out_free; | |
2138 | ||
f7d82350 SR |
2139 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
2140 | goto out_free; | |
2141 | ||
2142 | field = malloc_or_die(sizeof(*field)); | |
54a36258 | 2143 | memset(field, 0, sizeof(*field)); |
f7d82350 SR |
2144 | |
2145 | value = arg_eval(arg); | |
d69afed5 VN |
2146 | if (value == NULL) |
2147 | goto out_free; | |
f7d82350 SR |
2148 | field->value = strdup(value); |
2149 | ||
2150 | free_arg(arg); | |
2151 | arg = alloc_arg(); | |
2152 | ||
2153 | free_token(token); | |
2154 | type = process_arg(event, arg, &token); | |
2155 | if (test_type_token(type, token, EVENT_OP, "}")) | |
2156 | goto out_free; | |
2157 | ||
2158 | value = arg_eval(arg); | |
d69afed5 VN |
2159 | if (value == NULL) |
2160 | goto out_free; | |
f7d82350 SR |
2161 | field->str = strdup(value); |
2162 | free_arg(arg); | |
2163 | arg = NULL; | |
2164 | ||
2165 | *list = field; | |
2166 | list = &field->next; | |
2167 | ||
2168 | free_token(token); | |
2169 | type = read_token_item(&token); | |
2170 | } while (type == EVENT_DELIM && strcmp(token, ",") == 0); | |
2171 | ||
2172 | *tok = token; | |
2173 | return type; | |
2174 | ||
2175 | out_free: | |
2176 | free_arg(arg); | |
2177 | free_token(token); | |
2178 | *tok = NULL; | |
2179 | ||
2180 | return EVENT_ERROR; | |
2181 | } | |
2182 | ||
2183 | static enum event_type | |
2184 | process_flags(struct event_format *event, struct print_arg *arg, char **tok) | |
2185 | { | |
2186 | struct print_arg *field; | |
2187 | enum event_type type; | |
2188 | char *token; | |
2189 | ||
2190 | memset(arg, 0, sizeof(*arg)); | |
2191 | arg->type = PRINT_FLAGS; | |
2192 | ||
2193 | field = alloc_arg(); | |
2194 | ||
2195 | type = process_arg(event, field, &token); | |
2196 | ||
2197 | /* Handle operations in the first argument */ | |
2198 | while (type == EVENT_OP) | |
2199 | type = process_op(event, field, &token); | |
2200 | ||
2201 | if (test_type_token(type, token, EVENT_DELIM, ",")) | |
2202 | goto out_free; | |
2203 | free_token(token); | |
2204 | ||
2205 | arg->flags.field = field; | |
2206 | ||
2207 | type = read_token_item(&token); | |
2208 | if (event_item_type(type)) { | |
2209 | arg->flags.delim = token; | |
2210 | type = read_token_item(&token); | |
2211 | } | |
2212 | ||
2213 | if (test_type_token(type, token, EVENT_DELIM, ",")) | |
2214 | goto out_free; | |
2215 | ||
2216 | type = process_fields(event, &arg->flags.flags, &token); | |
2217 | if (test_type_token(type, token, EVENT_DELIM, ")")) | |
2218 | goto out_free; | |
2219 | ||
2220 | free_token(token); | |
2221 | type = read_token_item(tok); | |
2222 | return type; | |
2223 | ||
2224 | out_free: | |
2225 | free_token(token); | |
2226 | *tok = NULL; | |
2227 | return EVENT_ERROR; | |
2228 | } | |
2229 | ||
2230 | static enum event_type | |
2231 | process_symbols(struct event_format *event, struct print_arg *arg, char **tok) | |
2232 | { | |
2233 | struct print_arg *field; | |
2234 | enum event_type type; | |
2235 | char *token; | |
2236 | ||
2237 | memset(arg, 0, sizeof(*arg)); | |
2238 | arg->type = PRINT_SYMBOL; | |
2239 | ||
2240 | field = alloc_arg(); | |
2241 | ||
2242 | type = process_arg(event, field, &token); | |
2243 | if (test_type_token(type, token, EVENT_DELIM, ",")) | |
2244 | goto out_free; | |
2245 | ||
2246 | arg->symbol.field = field; | |
2247 | ||
2248 | type = process_fields(event, &arg->symbol.symbols, &token); | |
2249 | if (test_type_token(type, token, EVENT_DELIM, ")")) | |
2250 | goto out_free; | |
2251 | ||
2252 | free_token(token); | |
2253 | type = read_token_item(tok); | |
2254 | return type; | |
2255 | ||
2256 | out_free: | |
2257 | free_token(token); | |
2258 | *tok = NULL; | |
2259 | return EVENT_ERROR; | |
2260 | } | |
2261 | ||
2262 | static enum event_type | |
2263 | process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok) | |
2264 | { | |
2265 | struct format_field *field; | |
2266 | enum event_type type; | |
2267 | char *token; | |
2268 | ||
2269 | memset(arg, 0, sizeof(*arg)); | |
2270 | arg->type = PRINT_DYNAMIC_ARRAY; | |
2271 | ||
2272 | /* | |
2273 | * The item within the parenthesis is another field that holds | |
2274 | * the index into where the array starts. | |
2275 | */ | |
2276 | type = read_token(&token); | |
2277 | *tok = token; | |
2278 | if (type != EVENT_ITEM) | |
2279 | goto out_free; | |
2280 | ||
2281 | /* Find the field */ | |
2282 | ||
2283 | field = pevent_find_field(event, token); | |
2284 | if (!field) | |
2285 | goto out_free; | |
2286 | ||
2287 | arg->dynarray.field = field; | |
2288 | arg->dynarray.index = 0; | |
2289 | ||
2290 | if (read_expected(EVENT_DELIM, ")") < 0) | |
2291 | goto out_free; | |
2292 | ||
2293 | free_token(token); | |
2294 | type = read_token_item(&token); | |
2295 | *tok = token; | |
2296 | if (type != EVENT_OP || strcmp(token, "[") != 0) | |
2297 | return type; | |
2298 | ||
2299 | free_token(token); | |
2300 | arg = alloc_arg(); | |
2301 | type = process_arg(event, arg, &token); | |
2302 | if (type == EVENT_ERROR) | |
2303 | goto out_free; | |
2304 | ||
2305 | if (!test_type_token(type, token, EVENT_OP, "]")) | |
2306 | goto out_free; | |
2307 | ||
2308 | free_token(token); | |
2309 | type = read_token_item(tok); | |
2310 | return type; | |
2311 | ||
2312 | out_free: | |
2313 | free(arg); | |
2314 | free_token(token); | |
2315 | *tok = NULL; | |
2316 | return EVENT_ERROR; | |
2317 | } | |
2318 | ||
2319 | static enum event_type | |
2320 | process_paren(struct event_format *event, struct print_arg *arg, char **tok) | |
2321 | { | |
2322 | struct print_arg *item_arg; | |
2323 | enum event_type type; | |
2324 | char *token; | |
2325 | ||
2326 | type = process_arg(event, arg, &token); | |
2327 | ||
2328 | if (type == EVENT_ERROR) | |
2329 | goto out_free; | |
2330 | ||
2331 | if (type == EVENT_OP) | |
2332 | type = process_op(event, arg, &token); | |
2333 | ||
2334 | if (type == EVENT_ERROR) | |
2335 | goto out_free; | |
2336 | ||
2337 | if (test_type_token(type, token, EVENT_DELIM, ")")) | |
2338 | goto out_free; | |
2339 | ||
2340 | free_token(token); | |
2341 | type = read_token_item(&token); | |
2342 | ||
2343 | /* | |
2344 | * If the next token is an item or another open paren, then | |
2345 | * this was a typecast. | |
2346 | */ | |
2347 | if (event_item_type(type) || | |
2348 | (type == EVENT_DELIM && strcmp(token, "(") == 0)) { | |
2349 | ||
2350 | /* make this a typecast and contine */ | |
2351 | ||
2352 | /* prevous must be an atom */ | |
2353 | if (arg->type != PRINT_ATOM) | |
2354 | die("previous needed to be PRINT_ATOM"); | |
2355 | ||
2356 | item_arg = alloc_arg(); | |
2357 | ||
2358 | arg->type = PRINT_TYPE; | |
2359 | arg->typecast.type = arg->atom.atom; | |
2360 | arg->typecast.item = item_arg; | |
2361 | type = process_arg_token(event, item_arg, &token, type); | |
2362 | ||
2363 | } | |
2364 | ||
2365 | *tok = token; | |
2366 | return type; | |
2367 | ||
2368 | out_free: | |
2369 | free_token(token); | |
2370 | *tok = NULL; | |
2371 | return EVENT_ERROR; | |
2372 | } | |
2373 | ||
2374 | ||
2375 | static enum event_type | |
2376 | process_str(struct event_format *event __unused, struct print_arg *arg, char **tok) | |
2377 | { | |
2378 | enum event_type type; | |
2379 | char *token; | |
2380 | ||
2381 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
2382 | goto out_free; | |
2383 | ||
2384 | arg->type = PRINT_STRING; | |
2385 | arg->string.string = token; | |
2386 | arg->string.offset = -1; | |
2387 | ||
2388 | if (read_expected(EVENT_DELIM, ")") < 0) | |
2389 | goto out_err; | |
2390 | ||
2391 | type = read_token(&token); | |
2392 | *tok = token; | |
2393 | ||
2394 | return type; | |
2395 | ||
2396 | out_free: | |
2397 | free_token(token); | |
2398 | out_err: | |
2399 | *tok = NULL; | |
2400 | return EVENT_ERROR; | |
2401 | } | |
2402 | ||
2403 | static struct pevent_function_handler * | |
2404 | find_func_handler(struct pevent *pevent, char *func_name) | |
2405 | { | |
2406 | struct pevent_function_handler *func; | |
2407 | ||
2408 | for (func = pevent->func_handlers; func; func = func->next) { | |
2409 | if (strcmp(func->name, func_name) == 0) | |
2410 | break; | |
2411 | } | |
2412 | ||
2413 | return func; | |
2414 | } | |
2415 | ||
2416 | static void remove_func_handler(struct pevent *pevent, char *func_name) | |
2417 | { | |
2418 | struct pevent_function_handler *func; | |
2419 | struct pevent_function_handler **next; | |
2420 | ||
2421 | next = &pevent->func_handlers; | |
2422 | while ((func = *next)) { | |
2423 | if (strcmp(func->name, func_name) == 0) { | |
2424 | *next = func->next; | |
2425 | free_func_handle(func); | |
2426 | break; | |
2427 | } | |
2428 | next = &func->next; | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | static enum event_type | |
2433 | process_func_handler(struct event_format *event, struct pevent_function_handler *func, | |
2434 | struct print_arg *arg, char **tok) | |
2435 | { | |
2436 | struct print_arg **next_arg; | |
2437 | struct print_arg *farg; | |
2438 | enum event_type type; | |
2439 | char *token; | |
2440 | char *test; | |
2441 | int i; | |
2442 | ||
2443 | arg->type = PRINT_FUNC; | |
2444 | arg->func.func = func; | |
2445 | ||
2446 | *tok = NULL; | |
2447 | ||
2448 | next_arg = &(arg->func.args); | |
2449 | for (i = 0; i < func->nr_args; i++) { | |
2450 | farg = alloc_arg(); | |
2451 | type = process_arg(event, farg, &token); | |
2452 | if (i < (func->nr_args - 1)) | |
2453 | test = ","; | |
2454 | else | |
2455 | test = ")"; | |
2456 | ||
2457 | if (test_type_token(type, token, EVENT_DELIM, test)) { | |
2458 | free_arg(farg); | |
2459 | free_token(token); | |
2460 | return EVENT_ERROR; | |
2461 | } | |
2462 | ||
2463 | *next_arg = farg; | |
2464 | next_arg = &(farg->next); | |
2465 | free_token(token); | |
2466 | } | |
2467 | ||
2468 | type = read_token(&token); | |
2469 | *tok = token; | |
2470 | ||
2471 | return type; | |
2472 | } | |
2473 | ||
2474 | static enum event_type | |
2475 | process_function(struct event_format *event, struct print_arg *arg, | |
2476 | char *token, char **tok) | |
2477 | { | |
2478 | struct pevent_function_handler *func; | |
2479 | ||
2480 | if (strcmp(token, "__print_flags") == 0) { | |
2481 | free_token(token); | |
5205aec9 | 2482 | is_flag_field = 1; |
f7d82350 SR |
2483 | return process_flags(event, arg, tok); |
2484 | } | |
2485 | if (strcmp(token, "__print_symbolic") == 0) { | |
2486 | free_token(token); | |
5205aec9 | 2487 | is_symbolic_field = 1; |
f7d82350 SR |
2488 | return process_symbols(event, arg, tok); |
2489 | } | |
2490 | if (strcmp(token, "__get_str") == 0) { | |
2491 | free_token(token); | |
2492 | return process_str(event, arg, tok); | |
2493 | } | |
2494 | if (strcmp(token, "__get_dynamic_array") == 0) { | |
2495 | free_token(token); | |
2496 | return process_dynamic_array(event, arg, tok); | |
2497 | } | |
2498 | ||
2499 | func = find_func_handler(event->pevent, token); | |
2500 | if (func) { | |
2501 | free_token(token); | |
2502 | return process_func_handler(event, func, arg, tok); | |
2503 | } | |
2504 | ||
2505 | do_warning("function %s not defined", token); | |
2506 | free_token(token); | |
2507 | return EVENT_ERROR; | |
2508 | } | |
2509 | ||
2510 | static enum event_type | |
2511 | process_arg_token(struct event_format *event, struct print_arg *arg, | |
2512 | char **tok, enum event_type type) | |
2513 | { | |
2514 | char *token; | |
2515 | char *atom; | |
2516 | ||
2517 | token = *tok; | |
2518 | ||
2519 | switch (type) { | |
2520 | case EVENT_ITEM: | |
2521 | if (strcmp(token, "REC") == 0) { | |
2522 | free_token(token); | |
2523 | type = process_entry(event, arg, &token); | |
2524 | break; | |
2525 | } | |
2526 | atom = token; | |
2527 | /* test the next token */ | |
2528 | type = read_token_item(&token); | |
2529 | ||
2530 | /* | |
2531 | * If the next token is a parenthesis, then this | |
2532 | * is a function. | |
2533 | */ | |
2534 | if (type == EVENT_DELIM && strcmp(token, "(") == 0) { | |
2535 | free_token(token); | |
2536 | token = NULL; | |
2537 | /* this will free atom. */ | |
2538 | type = process_function(event, arg, atom, &token); | |
2539 | break; | |
2540 | } | |
2541 | /* atoms can be more than one token long */ | |
2542 | while (type == EVENT_ITEM) { | |
2543 | atom = realloc(atom, strlen(atom) + strlen(token) + 2); | |
2544 | strcat(atom, " "); | |
2545 | strcat(atom, token); | |
2546 | free_token(token); | |
2547 | type = read_token_item(&token); | |
2548 | } | |
2549 | ||
2550 | arg->type = PRINT_ATOM; | |
2551 | arg->atom.atom = atom; | |
2552 | break; | |
2553 | ||
2554 | case EVENT_DQUOTE: | |
2555 | case EVENT_SQUOTE: | |
2556 | arg->type = PRINT_ATOM; | |
2557 | arg->atom.atom = token; | |
2558 | type = read_token_item(&token); | |
2559 | break; | |
2560 | case EVENT_DELIM: | |
2561 | if (strcmp(token, "(") == 0) { | |
2562 | free_token(token); | |
2563 | type = process_paren(event, arg, &token); | |
2564 | break; | |
2565 | } | |
2566 | case EVENT_OP: | |
2567 | /* handle single ops */ | |
2568 | arg->type = PRINT_OP; | |
2569 | arg->op.op = token; | |
2570 | arg->op.left = NULL; | |
2571 | type = process_op(event, arg, &token); | |
2572 | ||
2573 | /* On error, the op is freed */ | |
2574 | if (type == EVENT_ERROR) | |
2575 | arg->op.op = NULL; | |
2576 | ||
2577 | /* return error type if errored */ | |
2578 | break; | |
2579 | ||
2580 | case EVENT_ERROR ... EVENT_NEWLINE: | |
2581 | default: | |
2582 | die("unexpected type %d", type); | |
2583 | } | |
2584 | *tok = token; | |
2585 | ||
2586 | return type; | |
2587 | } | |
2588 | ||
2589 | static int event_read_print_args(struct event_format *event, struct print_arg **list) | |
2590 | { | |
2591 | enum event_type type = EVENT_ERROR; | |
2592 | struct print_arg *arg; | |
2593 | char *token; | |
2594 | int args = 0; | |
2595 | ||
2596 | do { | |
2597 | if (type == EVENT_NEWLINE) { | |
2598 | type = read_token_item(&token); | |
2599 | continue; | |
2600 | } | |
2601 | ||
2602 | arg = alloc_arg(); | |
2603 | ||
2604 | type = process_arg(event, arg, &token); | |
2605 | ||
2606 | if (type == EVENT_ERROR) { | |
2607 | free_token(token); | |
2608 | free_arg(arg); | |
2609 | return -1; | |
2610 | } | |
2611 | ||
2612 | *list = arg; | |
2613 | args++; | |
2614 | ||
2615 | if (type == EVENT_OP) { | |
2616 | type = process_op(event, arg, &token); | |
2617 | free_token(token); | |
2618 | if (type == EVENT_ERROR) { | |
2619 | *list = NULL; | |
2620 | free_arg(arg); | |
2621 | return -1; | |
2622 | } | |
2623 | list = &arg->next; | |
2624 | continue; | |
2625 | } | |
2626 | ||
2627 | if (type == EVENT_DELIM && strcmp(token, ",") == 0) { | |
2628 | free_token(token); | |
2629 | *list = arg; | |
2630 | list = &arg->next; | |
2631 | continue; | |
2632 | } | |
2633 | break; | |
2634 | } while (type != EVENT_NONE); | |
2635 | ||
2636 | if (type != EVENT_NONE && type != EVENT_ERROR) | |
2637 | free_token(token); | |
2638 | ||
2639 | return args; | |
2640 | } | |
2641 | ||
2642 | static int event_read_print(struct event_format *event) | |
2643 | { | |
2644 | enum event_type type; | |
2645 | char *token; | |
2646 | int ret; | |
2647 | ||
2648 | if (read_expected_item(EVENT_ITEM, "print") < 0) | |
2649 | return -1; | |
2650 | ||
2651 | if (read_expected(EVENT_ITEM, "fmt") < 0) | |
2652 | return -1; | |
2653 | ||
2654 | if (read_expected(EVENT_OP, ":") < 0) | |
2655 | return -1; | |
2656 | ||
2657 | if (read_expect_type(EVENT_DQUOTE, &token) < 0) | |
2658 | goto fail; | |
2659 | ||
2660 | concat: | |
2661 | event->print_fmt.format = token; | |
2662 | event->print_fmt.args = NULL; | |
2663 | ||
2664 | /* ok to have no arg */ | |
2665 | type = read_token_item(&token); | |
2666 | ||
2667 | if (type == EVENT_NONE) | |
2668 | return 0; | |
2669 | ||
2670 | /* Handle concatenation of print lines */ | |
2671 | if (type == EVENT_DQUOTE) { | |
2672 | char *cat; | |
2673 | ||
2674 | cat = malloc_or_die(strlen(event->print_fmt.format) + | |
2675 | strlen(token) + 1); | |
2676 | strcpy(cat, event->print_fmt.format); | |
2677 | strcat(cat, token); | |
2678 | free_token(token); | |
2679 | free_token(event->print_fmt.format); | |
2680 | event->print_fmt.format = NULL; | |
2681 | token = cat; | |
2682 | goto concat; | |
2683 | } | |
2684 | ||
2685 | if (test_type_token(type, token, EVENT_DELIM, ",")) | |
2686 | goto fail; | |
2687 | ||
2688 | free_token(token); | |
2689 | ||
2690 | ret = event_read_print_args(event, &event->print_fmt.args); | |
2691 | if (ret < 0) | |
2692 | return -1; | |
2693 | ||
2694 | return ret; | |
2695 | ||
2696 | fail: | |
2697 | free_token(token); | |
2698 | return -1; | |
2699 | } | |
2700 | ||
2701 | /** | |
2702 | * pevent_find_common_field - return a common field by event | |
2703 | * @event: handle for the event | |
2704 | * @name: the name of the common field to return | |
2705 | * | |
2706 | * Returns a common field from the event by the given @name. | |
2707 | * This only searchs the common fields and not all field. | |
2708 | */ | |
2709 | struct format_field * | |
2710 | pevent_find_common_field(struct event_format *event, const char *name) | |
2711 | { | |
2712 | struct format_field *format; | |
2713 | ||
2714 | for (format = event->format.common_fields; | |
2715 | format; format = format->next) { | |
2716 | if (strcmp(format->name, name) == 0) | |
2717 | break; | |
2718 | } | |
2719 | ||
2720 | return format; | |
2721 | } | |
2722 | ||
2723 | /** | |
2724 | * pevent_find_field - find a non-common field | |
2725 | * @event: handle for the event | |
2726 | * @name: the name of the non-common field | |
2727 | * | |
2728 | * Returns a non-common field by the given @name. | |
2729 | * This does not search common fields. | |
2730 | */ | |
2731 | struct format_field * | |
2732 | pevent_find_field(struct event_format *event, const char *name) | |
2733 | { | |
2734 | struct format_field *format; | |
2735 | ||
2736 | for (format = event->format.fields; | |
2737 | format; format = format->next) { | |
2738 | if (strcmp(format->name, name) == 0) | |
2739 | break; | |
2740 | } | |
2741 | ||
2742 | return format; | |
2743 | } | |
2744 | ||
2745 | /** | |
2746 | * pevent_find_any_field - find any field by name | |
2747 | * @event: handle for the event | |
2748 | * @name: the name of the field | |
2749 | * | |
2750 | * Returns a field by the given @name. | |
2751 | * This searchs the common field names first, then | |
2752 | * the non-common ones if a common one was not found. | |
2753 | */ | |
2754 | struct format_field * | |
2755 | pevent_find_any_field(struct event_format *event, const char *name) | |
2756 | { | |
2757 | struct format_field *format; | |
2758 | ||
2759 | format = pevent_find_common_field(event, name); | |
2760 | if (format) | |
2761 | return format; | |
2762 | return pevent_find_field(event, name); | |
2763 | } | |
2764 | ||
2765 | /** | |
2766 | * pevent_read_number - read a number from data | |
2767 | * @pevent: handle for the pevent | |
2768 | * @ptr: the raw data | |
2769 | * @size: the size of the data that holds the number | |
2770 | * | |
2771 | * Returns the number (converted to host) from the | |
2772 | * raw data. | |
2773 | */ | |
2774 | unsigned long long pevent_read_number(struct pevent *pevent, | |
2775 | const void *ptr, int size) | |
2776 | { | |
2777 | switch (size) { | |
2778 | case 1: | |
2779 | return *(unsigned char *)ptr; | |
2780 | case 2: | |
2781 | return data2host2(pevent, ptr); | |
2782 | case 4: | |
2783 | return data2host4(pevent, ptr); | |
2784 | case 8: | |
2785 | return data2host8(pevent, ptr); | |
2786 | default: | |
2787 | /* BUG! */ | |
2788 | return 0; | |
2789 | } | |
2790 | } | |
2791 | ||
2792 | /** | |
2793 | * pevent_read_number_field - read a number from data | |
2794 | * @field: a handle to the field | |
2795 | * @data: the raw data to read | |
2796 | * @value: the value to place the number in | |
2797 | * | |
2798 | * Reads raw data according to a field offset and size, | |
2799 | * and translates it into @value. | |
2800 | * | |
2801 | * Returns 0 on success, -1 otherwise. | |
2802 | */ | |
2803 | int pevent_read_number_field(struct format_field *field, const void *data, | |
2804 | unsigned long long *value) | |
2805 | { | |
2806 | if (!field) | |
2807 | return -1; | |
2808 | switch (field->size) { | |
2809 | case 1: | |
2810 | case 2: | |
2811 | case 4: | |
2812 | case 8: | |
2813 | *value = pevent_read_number(field->event->pevent, | |
2814 | data + field->offset, field->size); | |
2815 | return 0; | |
2816 | default: | |
2817 | return -1; | |
2818 | } | |
2819 | } | |
2820 | ||
2821 | static int get_common_info(struct pevent *pevent, | |
2822 | const char *type, int *offset, int *size) | |
2823 | { | |
2824 | struct event_format *event; | |
2825 | struct format_field *field; | |
2826 | ||
2827 | /* | |
2828 | * All events should have the same common elements. | |
2829 | * Pick any event to find where the type is; | |
2830 | */ | |
2831 | if (!pevent->events) | |
2832 | die("no event_list!"); | |
2833 | ||
2834 | event = pevent->events[0]; | |
2835 | field = pevent_find_common_field(event, type); | |
2836 | if (!field) | |
2837 | die("field '%s' not found", type); | |
2838 | ||
2839 | *offset = field->offset; | |
2840 | *size = field->size; | |
2841 | ||
2842 | return 0; | |
2843 | } | |
2844 | ||
2845 | static int __parse_common(struct pevent *pevent, void *data, | |
2846 | int *size, int *offset, const char *name) | |
2847 | { | |
2848 | int ret; | |
2849 | ||
2850 | if (!*size) { | |
2851 | ret = get_common_info(pevent, name, offset, size); | |
2852 | if (ret < 0) | |
2853 | return ret; | |
2854 | } | |
2855 | return pevent_read_number(pevent, data + *offset, *size); | |
2856 | } | |
2857 | ||
2858 | static int trace_parse_common_type(struct pevent *pevent, void *data) | |
2859 | { | |
2860 | return __parse_common(pevent, data, | |
2861 | &pevent->type_size, &pevent->type_offset, | |
2862 | "common_type"); | |
2863 | } | |
2864 | ||
2865 | static int parse_common_pid(struct pevent *pevent, void *data) | |
2866 | { | |
2867 | return __parse_common(pevent, data, | |
2868 | &pevent->pid_size, &pevent->pid_offset, | |
2869 | "common_pid"); | |
2870 | } | |
2871 | ||
2872 | static int parse_common_pc(struct pevent *pevent, void *data) | |
2873 | { | |
2874 | return __parse_common(pevent, data, | |
2875 | &pevent->pc_size, &pevent->pc_offset, | |
2876 | "common_preempt_count"); | |
2877 | } | |
2878 | ||
2879 | static int parse_common_flags(struct pevent *pevent, void *data) | |
2880 | { | |
2881 | return __parse_common(pevent, data, | |
2882 | &pevent->flags_size, &pevent->flags_offset, | |
2883 | "common_flags"); | |
2884 | } | |
2885 | ||
2886 | static int parse_common_lock_depth(struct pevent *pevent, void *data) | |
2887 | { | |
2888 | int ret; | |
2889 | ||
2890 | ret = __parse_common(pevent, data, | |
2891 | &pevent->ld_size, &pevent->ld_offset, | |
2892 | "common_lock_depth"); | |
2893 | if (ret < 0) | |
2894 | return -1; | |
2895 | ||
2896 | return ret; | |
2897 | } | |
2898 | ||
2899 | static int events_id_cmp(const void *a, const void *b); | |
2900 | ||
2901 | /** | |
2902 | * pevent_find_event - find an event by given id | |
2903 | * @pevent: a handle to the pevent | |
2904 | * @id: the id of the event | |
2905 | * | |
2906 | * Returns an event that has a given @id. | |
2907 | */ | |
2908 | struct event_format *pevent_find_event(struct pevent *pevent, int id) | |
2909 | { | |
2910 | struct event_format **eventptr; | |
2911 | struct event_format key; | |
2912 | struct event_format *pkey = &key; | |
2913 | ||
2914 | /* Check cache first */ | |
2915 | if (pevent->last_event && pevent->last_event->id == id) | |
2916 | return pevent->last_event; | |
2917 | ||
2918 | key.id = id; | |
2919 | ||
2920 | eventptr = bsearch(&pkey, pevent->events, pevent->nr_events, | |
2921 | sizeof(*pevent->events), events_id_cmp); | |
2922 | ||
2923 | if (eventptr) { | |
2924 | pevent->last_event = *eventptr; | |
2925 | return *eventptr; | |
2926 | } | |
2927 | ||
2928 | return NULL; | |
2929 | } | |
2930 | ||
2931 | /** | |
2932 | * pevent_find_event_by_name - find an event by given name | |
2933 | * @pevent: a handle to the pevent | |
2934 | * @sys: the system name to search for | |
2935 | * @name: the name of the event to search for | |
2936 | * | |
2937 | * This returns an event with a given @name and under the system | |
2938 | * @sys. If @sys is NULL the first event with @name is returned. | |
2939 | */ | |
2940 | struct event_format * | |
2941 | pevent_find_event_by_name(struct pevent *pevent, | |
2942 | const char *sys, const char *name) | |
2943 | { | |
2944 | struct event_format *event; | |
2945 | int i; | |
2946 | ||
2947 | if (pevent->last_event && | |
2948 | strcmp(pevent->last_event->name, name) == 0 && | |
2949 | (!sys || strcmp(pevent->last_event->system, sys) == 0)) | |
2950 | return pevent->last_event; | |
2951 | ||
2952 | for (i = 0; i < pevent->nr_events; i++) { | |
2953 | event = pevent->events[i]; | |
2954 | if (strcmp(event->name, name) == 0) { | |
2955 | if (!sys) | |
2956 | break; | |
2957 | if (strcmp(event->system, sys) == 0) | |
2958 | break; | |
2959 | } | |
2960 | } | |
2961 | if (i == pevent->nr_events) | |
2962 | event = NULL; | |
2963 | ||
2964 | pevent->last_event = event; | |
2965 | return event; | |
2966 | } | |
2967 | ||
2968 | static unsigned long long | |
2969 | eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg) | |
2970 | { | |
2971 | struct pevent *pevent = event->pevent; | |
2972 | unsigned long long val = 0; | |
2973 | unsigned long long left, right; | |
2974 | struct print_arg *typearg = NULL; | |
2975 | struct print_arg *larg; | |
2976 | unsigned long offset; | |
2977 | unsigned int field_size; | |
2978 | ||
2979 | switch (arg->type) { | |
2980 | case PRINT_NULL: | |
2981 | /* ?? */ | |
2982 | return 0; | |
2983 | case PRINT_ATOM: | |
2984 | return strtoull(arg->atom.atom, NULL, 0); | |
2985 | case PRINT_FIELD: | |
2986 | if (!arg->field.field) { | |
2987 | arg->field.field = pevent_find_any_field(event, arg->field.name); | |
2988 | if (!arg->field.field) | |
2989 | die("field %s not found", arg->field.name); | |
2990 | } | |
2991 | /* must be a number */ | |
2992 | val = pevent_read_number(pevent, data + arg->field.field->offset, | |
2993 | arg->field.field->size); | |
2994 | break; | |
2995 | case PRINT_FLAGS: | |
2996 | case PRINT_SYMBOL: | |
2997 | break; | |
2998 | case PRINT_TYPE: | |
2999 | val = eval_num_arg(data, size, event, arg->typecast.item); | |
3000 | return eval_type(val, arg, 0); | |
3001 | case PRINT_STRING: | |
3002 | case PRINT_BSTRING: | |
3003 | return 0; | |
3004 | case PRINT_FUNC: { | |
3005 | struct trace_seq s; | |
3006 | trace_seq_init(&s); | |
3007 | val = process_defined_func(&s, data, size, event, arg); | |
3008 | trace_seq_destroy(&s); | |
3009 | return val; | |
3010 | } | |
3011 | case PRINT_OP: | |
3012 | if (strcmp(arg->op.op, "[") == 0) { | |
3013 | /* | |
3014 | * Arrays are special, since we don't want | |
3015 | * to read the arg as is. | |
3016 | */ | |
3017 | right = eval_num_arg(data, size, event, arg->op.right); | |
3018 | ||
3019 | /* handle typecasts */ | |
3020 | larg = arg->op.left; | |
3021 | while (larg->type == PRINT_TYPE) { | |
3022 | if (!typearg) | |
3023 | typearg = larg; | |
3024 | larg = larg->typecast.item; | |
3025 | } | |
3026 | ||
3027 | /* Default to long size */ | |
3028 | field_size = pevent->long_size; | |
3029 | ||
3030 | switch (larg->type) { | |
3031 | case PRINT_DYNAMIC_ARRAY: | |
3032 | offset = pevent_read_number(pevent, | |
3033 | data + larg->dynarray.field->offset, | |
3034 | larg->dynarray.field->size); | |
3035 | if (larg->dynarray.field->elementsize) | |
3036 | field_size = larg->dynarray.field->elementsize; | |
3037 | /* | |
3038 | * The actual length of the dynamic array is stored | |
3039 | * in the top half of the field, and the offset | |
3040 | * is in the bottom half of the 32 bit field. | |
3041 | */ | |
3042 | offset &= 0xffff; | |
3043 | offset += right; | |
3044 | break; | |
3045 | case PRINT_FIELD: | |
3046 | if (!larg->field.field) { | |
3047 | larg->field.field = | |
3048 | pevent_find_any_field(event, larg->field.name); | |
3049 | if (!larg->field.field) | |
3050 | die("field %s not found", larg->field.name); | |
3051 | } | |
3052 | field_size = larg->field.field->elementsize; | |
3053 | offset = larg->field.field->offset + | |
3054 | right * larg->field.field->elementsize; | |
3055 | break; | |
3056 | default: | |
3057 | goto default_op; /* oops, all bets off */ | |
3058 | } | |
3059 | val = pevent_read_number(pevent, | |
3060 | data + offset, field_size); | |
3061 | if (typearg) | |
3062 | val = eval_type(val, typearg, 1); | |
3063 | break; | |
3064 | } else if (strcmp(arg->op.op, "?") == 0) { | |
3065 | left = eval_num_arg(data, size, event, arg->op.left); | |
3066 | arg = arg->op.right; | |
3067 | if (left) | |
3068 | val = eval_num_arg(data, size, event, arg->op.left); | |
3069 | else | |
3070 | val = eval_num_arg(data, size, event, arg->op.right); | |
3071 | break; | |
3072 | } | |
3073 | default_op: | |
3074 | left = eval_num_arg(data, size, event, arg->op.left); | |
3075 | right = eval_num_arg(data, size, event, arg->op.right); | |
3076 | switch (arg->op.op[0]) { | |
3077 | case '!': | |
3078 | switch (arg->op.op[1]) { | |
3079 | case 0: | |
3080 | val = !right; | |
3081 | break; | |
3082 | case '=': | |
3083 | val = left != right; | |
3084 | break; | |
3085 | default: | |
3086 | die("unknown op '%s'", arg->op.op); | |
3087 | } | |
3088 | break; | |
3089 | case '~': | |
3090 | val = ~right; | |
3091 | break; | |
3092 | case '|': | |
3093 | if (arg->op.op[1]) | |
3094 | val = left || right; | |
3095 | else | |
3096 | val = left | right; | |
3097 | break; | |
3098 | case '&': | |
3099 | if (arg->op.op[1]) | |
3100 | val = left && right; | |
3101 | else | |
3102 | val = left & right; | |
3103 | break; | |
3104 | case '<': | |
3105 | switch (arg->op.op[1]) { | |
3106 | case 0: | |
3107 | val = left < right; | |
3108 | break; | |
3109 | case '<': | |
3110 | val = left << right; | |
3111 | break; | |
3112 | case '=': | |
3113 | val = left <= right; | |
3114 | break; | |
3115 | default: | |
3116 | die("unknown op '%s'", arg->op.op); | |
3117 | } | |
3118 | break; | |
3119 | case '>': | |
3120 | switch (arg->op.op[1]) { | |
3121 | case 0: | |
3122 | val = left > right; | |
3123 | break; | |
3124 | case '>': | |
3125 | val = left >> right; | |
3126 | break; | |
3127 | case '=': | |
3128 | val = left >= right; | |
3129 | break; | |
3130 | default: | |
3131 | die("unknown op '%s'", arg->op.op); | |
3132 | } | |
3133 | break; | |
3134 | case '=': | |
3135 | if (arg->op.op[1] != '=') | |
3136 | die("unknown op '%s'", arg->op.op); | |
3137 | val = left == right; | |
3138 | break; | |
3139 | case '-': | |
3140 | val = left - right; | |
3141 | break; | |
3142 | case '+': | |
3143 | val = left + right; | |
3144 | break; | |
2e7a5fc8 SR |
3145 | case '/': |
3146 | val = left / right; | |
3147 | break; | |
3148 | case '*': | |
3149 | val = left * right; | |
3150 | break; | |
f7d82350 SR |
3151 | default: |
3152 | die("unknown op '%s'", arg->op.op); | |
3153 | } | |
3154 | break; | |
3155 | default: /* not sure what to do there */ | |
3156 | return 0; | |
3157 | } | |
3158 | return val; | |
3159 | } | |
3160 | ||
3161 | struct flag { | |
3162 | const char *name; | |
3163 | unsigned long long value; | |
3164 | }; | |
3165 | ||
3166 | static const struct flag flags[] = { | |
3167 | { "HI_SOFTIRQ", 0 }, | |
3168 | { "TIMER_SOFTIRQ", 1 }, | |
3169 | { "NET_TX_SOFTIRQ", 2 }, | |
3170 | { "NET_RX_SOFTIRQ", 3 }, | |
3171 | { "BLOCK_SOFTIRQ", 4 }, | |
3172 | { "BLOCK_IOPOLL_SOFTIRQ", 5 }, | |
3173 | { "TASKLET_SOFTIRQ", 6 }, | |
3174 | { "SCHED_SOFTIRQ", 7 }, | |
3175 | { "HRTIMER_SOFTIRQ", 8 }, | |
3176 | { "RCU_SOFTIRQ", 9 }, | |
3177 | ||
3178 | { "HRTIMER_NORESTART", 0 }, | |
3179 | { "HRTIMER_RESTART", 1 }, | |
3180 | }; | |
3181 | ||
3182 | static unsigned long long eval_flag(const char *flag) | |
3183 | { | |
3184 | int i; | |
3185 | ||
3186 | /* | |
3187 | * Some flags in the format files do not get converted. | |
3188 | * If the flag is not numeric, see if it is something that | |
3189 | * we already know about. | |
3190 | */ | |
3191 | if (isdigit(flag[0])) | |
3192 | return strtoull(flag, NULL, 0); | |
3193 | ||
3194 | for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) | |
3195 | if (strcmp(flags[i].name, flag) == 0) | |
3196 | return flags[i].value; | |
3197 | ||
3198 | return 0; | |
3199 | } | |
3200 | ||
3201 | static void print_str_to_seq(struct trace_seq *s, const char *format, | |
3202 | int len_arg, const char *str) | |
3203 | { | |
3204 | if (len_arg >= 0) | |
3205 | trace_seq_printf(s, format, len_arg, str); | |
3206 | else | |
3207 | trace_seq_printf(s, format, str); | |
3208 | } | |
3209 | ||
3210 | static void print_str_arg(struct trace_seq *s, void *data, int size, | |
3211 | struct event_format *event, const char *format, | |
3212 | int len_arg, struct print_arg *arg) | |
3213 | { | |
3214 | struct pevent *pevent = event->pevent; | |
3215 | struct print_flag_sym *flag; | |
3216 | unsigned long long val, fval; | |
3217 | unsigned long addr; | |
3218 | char *str; | |
3219 | int print; | |
3220 | int len; | |
3221 | ||
3222 | switch (arg->type) { | |
3223 | case PRINT_NULL: | |
3224 | /* ?? */ | |
3225 | return; | |
3226 | case PRINT_ATOM: | |
3227 | print_str_to_seq(s, format, len_arg, arg->atom.atom); | |
3228 | return; | |
3229 | case PRINT_FIELD: | |
3230 | if (!arg->field.field) { | |
3231 | arg->field.field = pevent_find_any_field(event, arg->field.name); | |
3232 | if (!arg->field.field) | |
3233 | die("field %s not found", arg->field.name); | |
3234 | } | |
3235 | /* Zero sized fields, mean the rest of the data */ | |
3236 | len = arg->field.field->size ? : size - arg->field.field->offset; | |
3237 | ||
3238 | /* | |
3239 | * Some events pass in pointers. If this is not an array | |
3240 | * and the size is the same as long_size, assume that it | |
3241 | * is a pointer. | |
3242 | */ | |
3243 | if (!(arg->field.field->flags & FIELD_IS_ARRAY) && | |
3244 | arg->field.field->size == pevent->long_size) { | |
3245 | addr = *(unsigned long *)(data + arg->field.field->offset); | |
3246 | trace_seq_printf(s, "%lx", addr); | |
3247 | break; | |
3248 | } | |
3249 | str = malloc_or_die(len + 1); | |
3250 | memcpy(str, data + arg->field.field->offset, len); | |
3251 | str[len] = 0; | |
3252 | print_str_to_seq(s, format, len_arg, str); | |
3253 | free(str); | |
3254 | break; | |
3255 | case PRINT_FLAGS: | |
3256 | val = eval_num_arg(data, size, event, arg->flags.field); | |
3257 | print = 0; | |
3258 | for (flag = arg->flags.flags; flag; flag = flag->next) { | |
3259 | fval = eval_flag(flag->value); | |
3260 | if (!val && !fval) { | |
3261 | print_str_to_seq(s, format, len_arg, flag->str); | |
3262 | break; | |
3263 | } | |
3264 | if (fval && (val & fval) == fval) { | |
3265 | if (print && arg->flags.delim) | |
3266 | trace_seq_puts(s, arg->flags.delim); | |
3267 | print_str_to_seq(s, format, len_arg, flag->str); | |
3268 | print = 1; | |
3269 | val &= ~fval; | |
3270 | } | |
3271 | } | |
3272 | break; | |
3273 | case PRINT_SYMBOL: | |
3274 | val = eval_num_arg(data, size, event, arg->symbol.field); | |
3275 | for (flag = arg->symbol.symbols; flag; flag = flag->next) { | |
3276 | fval = eval_flag(flag->value); | |
3277 | if (val == fval) { | |
3278 | print_str_to_seq(s, format, len_arg, flag->str); | |
3279 | break; | |
3280 | } | |
3281 | } | |
3282 | break; | |
3283 | ||
3284 | case PRINT_TYPE: | |
3285 | break; | |
3286 | case PRINT_STRING: { | |
3287 | int str_offset; | |
3288 | ||
3289 | if (arg->string.offset == -1) { | |
3290 | struct format_field *f; | |
3291 | ||
3292 | f = pevent_find_any_field(event, arg->string.string); | |
3293 | arg->string.offset = f->offset; | |
3294 | } | |
3295 | str_offset = data2host4(pevent, data + arg->string.offset); | |
3296 | str_offset &= 0xffff; | |
3297 | print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset); | |
3298 | break; | |
3299 | } | |
3300 | case PRINT_BSTRING: | |
3301 | trace_seq_printf(s, format, arg->string.string); | |
3302 | break; | |
3303 | case PRINT_OP: | |
3304 | /* | |
3305 | * The only op for string should be ? : | |
3306 | */ | |
3307 | if (arg->op.op[0] != '?') | |
3308 | return; | |
3309 | val = eval_num_arg(data, size, event, arg->op.left); | |
3310 | if (val) | |
3311 | print_str_arg(s, data, size, event, | |
3312 | format, len_arg, arg->op.right->op.left); | |
3313 | else | |
3314 | print_str_arg(s, data, size, event, | |
3315 | format, len_arg, arg->op.right->op.right); | |
3316 | break; | |
3317 | case PRINT_FUNC: | |
3318 | process_defined_func(s, data, size, event, arg); | |
3319 | break; | |
3320 | default: | |
3321 | /* well... */ | |
3322 | break; | |
3323 | } | |
3324 | } | |
3325 | ||
3326 | static unsigned long long | |
3327 | process_defined_func(struct trace_seq *s, void *data, int size, | |
3328 | struct event_format *event, struct print_arg *arg) | |
3329 | { | |
3330 | struct pevent_function_handler *func_handle = arg->func.func; | |
3331 | struct pevent_func_params *param; | |
3332 | unsigned long long *args; | |
3333 | unsigned long long ret; | |
3334 | struct print_arg *farg; | |
3335 | struct trace_seq str; | |
3336 | struct save_str { | |
3337 | struct save_str *next; | |
3338 | char *str; | |
3339 | } *strings = NULL, *string; | |
3340 | int i; | |
3341 | ||
3342 | if (!func_handle->nr_args) { | |
3343 | ret = (*func_handle->func)(s, NULL); | |
3344 | goto out; | |
3345 | } | |
3346 | ||
3347 | farg = arg->func.args; | |
3348 | param = func_handle->params; | |
3349 | ||
3350 | args = malloc_or_die(sizeof(*args) * func_handle->nr_args); | |
3351 | for (i = 0; i < func_handle->nr_args; i++) { | |
3352 | switch (param->type) { | |
3353 | case PEVENT_FUNC_ARG_INT: | |
3354 | case PEVENT_FUNC_ARG_LONG: | |
3355 | case PEVENT_FUNC_ARG_PTR: | |
3356 | args[i] = eval_num_arg(data, size, event, farg); | |
3357 | break; | |
3358 | case PEVENT_FUNC_ARG_STRING: | |
3359 | trace_seq_init(&str); | |
3360 | print_str_arg(&str, data, size, event, "%s", -1, farg); | |
3361 | trace_seq_terminate(&str); | |
3362 | string = malloc_or_die(sizeof(*string)); | |
3363 | string->next = strings; | |
3364 | string->str = strdup(str.buffer); | |
3365 | strings = string; | |
3366 | trace_seq_destroy(&str); | |
3367 | break; | |
3368 | default: | |
3369 | /* | |
3370 | * Something went totally wrong, this is not | |
3371 | * an input error, something in this code broke. | |
3372 | */ | |
3373 | die("Unexpected end of arguments\n"); | |
3374 | break; | |
3375 | } | |
3376 | farg = farg->next; | |
3377 | } | |
3378 | ||
3379 | ret = (*func_handle->func)(s, args); | |
3380 | free(args); | |
3381 | while (strings) { | |
3382 | string = strings; | |
3383 | strings = string->next; | |
3384 | free(string->str); | |
3385 | free(string); | |
3386 | } | |
3387 | ||
3388 | out: | |
3389 | /* TBD : handle return type here */ | |
3390 | return ret; | |
3391 | } | |
3392 | ||
3393 | static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event) | |
3394 | { | |
3395 | struct pevent *pevent = event->pevent; | |
3396 | struct format_field *field, *ip_field; | |
3397 | struct print_arg *args, *arg, **next; | |
3398 | unsigned long long ip, val; | |
3399 | char *ptr; | |
3400 | void *bptr; | |
3401 | ||
3402 | field = pevent->bprint_buf_field; | |
3403 | ip_field = pevent->bprint_ip_field; | |
3404 | ||
3405 | if (!field) { | |
3406 | field = pevent_find_field(event, "buf"); | |
3407 | if (!field) | |
3408 | die("can't find buffer field for binary printk"); | |
3409 | ip_field = pevent_find_field(event, "ip"); | |
3410 | if (!ip_field) | |
3411 | die("can't find ip field for binary printk"); | |
3412 | pevent->bprint_buf_field = field; | |
3413 | pevent->bprint_ip_field = ip_field; | |
3414 | } | |
3415 | ||
3416 | ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size); | |
3417 | ||
3418 | /* | |
3419 | * The first arg is the IP pointer. | |
3420 | */ | |
3421 | args = alloc_arg(); | |
3422 | arg = args; | |
3423 | arg->next = NULL; | |
3424 | next = &arg->next; | |
3425 | ||
3426 | arg->type = PRINT_ATOM; | |
3427 | arg->atom.atom = malloc_or_die(32); | |
3428 | sprintf(arg->atom.atom, "%lld", ip); | |
3429 | ||
3430 | /* skip the first "%pf : " */ | |
3431 | for (ptr = fmt + 6, bptr = data + field->offset; | |
3432 | bptr < data + size && *ptr; ptr++) { | |
3433 | int ls = 0; | |
3434 | ||
3435 | if (*ptr == '%') { | |
3436 | process_again: | |
3437 | ptr++; | |
3438 | switch (*ptr) { | |
3439 | case '%': | |
3440 | break; | |
3441 | case 'l': | |
3442 | ls++; | |
3443 | goto process_again; | |
3444 | case 'L': | |
3445 | ls = 2; | |
3446 | goto process_again; | |
3447 | case '0' ... '9': | |
3448 | goto process_again; | |
3449 | case 'p': | |
3450 | ls = 1; | |
3451 | /* fall through */ | |
3452 | case 'd': | |
3453 | case 'u': | |
3454 | case 'x': | |
3455 | case 'i': | |
3456 | /* the pointers are always 4 bytes aligned */ | |
3457 | bptr = (void *)(((unsigned long)bptr + 3) & | |
3458 | ~3); | |
3459 | switch (ls) { | |
3460 | case 0: | |
3461 | ls = 4; | |
3462 | break; | |
3463 | case 1: | |
3464 | ls = pevent->long_size; | |
3465 | break; | |
3466 | case 2: | |
3467 | ls = 8; | |
3468 | default: | |
3469 | break; | |
3470 | } | |
3471 | val = pevent_read_number(pevent, bptr, ls); | |
3472 | bptr += ls; | |
3473 | arg = alloc_arg(); | |
3474 | arg->next = NULL; | |
3475 | arg->type = PRINT_ATOM; | |
3476 | arg->atom.atom = malloc_or_die(32); | |
3477 | sprintf(arg->atom.atom, "%lld", val); | |
3478 | *next = arg; | |
3479 | next = &arg->next; | |
3480 | break; | |
3481 | case 's': | |
3482 | arg = alloc_arg(); | |
3483 | arg->next = NULL; | |
3484 | arg->type = PRINT_BSTRING; | |
3485 | arg->string.string = strdup(bptr); | |
3486 | bptr += strlen(bptr) + 1; | |
3487 | *next = arg; | |
3488 | next = &arg->next; | |
3489 | default: | |
3490 | break; | |
3491 | } | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | return args; | |
3496 | } | |
3497 | ||
3498 | static void free_args(struct print_arg *args) | |
3499 | { | |
3500 | struct print_arg *next; | |
3501 | ||
3502 | while (args) { | |
3503 | next = args->next; | |
3504 | ||
3505 | free_arg(args); | |
3506 | args = next; | |
3507 | } | |
3508 | } | |
3509 | ||
3510 | static char * | |
3511 | get_bprint_format(void *data, int size __unused, struct event_format *event) | |
3512 | { | |
3513 | struct pevent *pevent = event->pevent; | |
3514 | unsigned long long addr; | |
3515 | struct format_field *field; | |
3516 | struct printk_map *printk; | |
3517 | char *format; | |
3518 | char *p; | |
3519 | ||
3520 | field = pevent->bprint_fmt_field; | |
3521 | ||
3522 | if (!field) { | |
3523 | field = pevent_find_field(event, "fmt"); | |
3524 | if (!field) | |
3525 | die("can't find format field for binary printk"); | |
3526 | pevent->bprint_fmt_field = field; | |
3527 | } | |
3528 | ||
3529 | addr = pevent_read_number(pevent, data + field->offset, field->size); | |
3530 | ||
3531 | printk = find_printk(pevent, addr); | |
3532 | if (!printk) { | |
3533 | format = malloc_or_die(45); | |
3534 | sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n", | |
3535 | addr); | |
3536 | return format; | |
3537 | } | |
3538 | ||
3539 | p = printk->printk; | |
3540 | /* Remove any quotes. */ | |
3541 | if (*p == '"') | |
3542 | p++; | |
3543 | format = malloc_or_die(strlen(p) + 10); | |
3544 | sprintf(format, "%s : %s", "%pf", p); | |
3545 | /* remove ending quotes and new line since we will add one too */ | |
3546 | p = format + strlen(format) - 1; | |
3547 | if (*p == '"') | |
3548 | *p = 0; | |
3549 | ||
3550 | p -= 2; | |
3551 | if (strcmp(p, "\\n") == 0) | |
3552 | *p = 0; | |
3553 | ||
3554 | return format; | |
3555 | } | |
3556 | ||
3557 | static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size, | |
3558 | struct event_format *event, struct print_arg *arg) | |
3559 | { | |
3560 | unsigned char *buf; | |
3561 | char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"; | |
3562 | ||
3563 | if (arg->type == PRINT_FUNC) { | |
3564 | process_defined_func(s, data, size, event, arg); | |
3565 | return; | |
3566 | } | |
3567 | ||
3568 | if (arg->type != PRINT_FIELD) { | |
3569 | trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", | |
3570 | arg->type); | |
3571 | return; | |
3572 | } | |
3573 | ||
3574 | if (mac == 'm') | |
3575 | fmt = "%.2x%.2x%.2x%.2x%.2x%.2x"; | |
3576 | if (!arg->field.field) { | |
3577 | arg->field.field = | |
3578 | pevent_find_any_field(event, arg->field.name); | |
3579 | if (!arg->field.field) | |
3580 | die("field %s not found", arg->field.name); | |
3581 | } | |
3582 | if (arg->field.field->size != 6) { | |
3583 | trace_seq_printf(s, "INVALIDMAC"); | |
3584 | return; | |
3585 | } | |
3586 | buf = data + arg->field.field->offset; | |
3587 | trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); | |
3588 | } | |
3589 | ||
3590 | static void print_event_fields(struct trace_seq *s, void *data, int size, | |
3591 | struct event_format *event) | |
3592 | { | |
3593 | struct format_field *field; | |
3594 | unsigned long long val; | |
3595 | unsigned int offset, len, i; | |
3596 | ||
3597 | field = event->format.fields; | |
3598 | while (field) { | |
3599 | trace_seq_printf(s, " %s=", field->name); | |
3600 | if (field->flags & FIELD_IS_ARRAY) { | |
3601 | offset = field->offset; | |
3602 | len = field->size; | |
3603 | if (field->flags & FIELD_IS_DYNAMIC) { | |
3604 | val = pevent_read_number(event->pevent, data + offset, len); | |
3605 | offset = val; | |
3606 | len = offset >> 16; | |
3607 | offset &= 0xffff; | |
3608 | } | |
3609 | if (field->flags & FIELD_IS_STRING) { | |
3610 | trace_seq_printf(s, "%s", (char *)data + offset); | |
3611 | } else { | |
3612 | trace_seq_puts(s, "ARRAY["); | |
3613 | for (i = 0; i < len; i++) { | |
3614 | if (i) | |
3615 | trace_seq_puts(s, ", "); | |
3616 | trace_seq_printf(s, "%02x", | |
3617 | *((unsigned char *)data + offset + i)); | |
3618 | } | |
3619 | trace_seq_putc(s, ']'); | |
3620 | } | |
3621 | } else { | |
3622 | val = pevent_read_number(event->pevent, data + field->offset, | |
3623 | field->size); | |
3624 | if (field->flags & FIELD_IS_POINTER) { | |
3625 | trace_seq_printf(s, "0x%llx", val); | |
3626 | } else if (field->flags & FIELD_IS_SIGNED) { | |
3627 | switch (field->size) { | |
3628 | case 4: | |
3629 | /* | |
3630 | * If field is long then print it in hex. | |
3631 | * A long usually stores pointers. | |
3632 | */ | |
3633 | if (field->flags & FIELD_IS_LONG) | |
3634 | trace_seq_printf(s, "0x%x", (int)val); | |
3635 | else | |
3636 | trace_seq_printf(s, "%d", (int)val); | |
3637 | break; | |
3638 | case 2: | |
3639 | trace_seq_printf(s, "%2d", (short)val); | |
3640 | break; | |
3641 | case 1: | |
3642 | trace_seq_printf(s, "%1d", (char)val); | |
3643 | break; | |
3644 | default: | |
3645 | trace_seq_printf(s, "%lld", val); | |
3646 | } | |
3647 | } else { | |
3648 | if (field->flags & FIELD_IS_LONG) | |
3649 | trace_seq_printf(s, "0x%llx", val); | |
3650 | else | |
3651 | trace_seq_printf(s, "%llu", val); | |
3652 | } | |
3653 | } | |
3654 | field = field->next; | |
3655 | } | |
3656 | } | |
3657 | ||
3658 | static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event) | |
3659 | { | |
3660 | struct pevent *pevent = event->pevent; | |
3661 | struct print_fmt *print_fmt = &event->print_fmt; | |
3662 | struct print_arg *arg = print_fmt->args; | |
3663 | struct print_arg *args = NULL; | |
3664 | const char *ptr = print_fmt->format; | |
3665 | unsigned long long val; | |
3666 | struct func_map *func; | |
3667 | const char *saveptr; | |
3668 | char *bprint_fmt = NULL; | |
3669 | char format[32]; | |
3670 | int show_func; | |
3671 | int len_as_arg; | |
3672 | int len_arg; | |
3673 | int len; | |
3674 | int ls; | |
3675 | ||
3676 | if (event->flags & EVENT_FL_FAILED) { | |
3677 | trace_seq_printf(s, "[FAILED TO PARSE]"); | |
3678 | print_event_fields(s, data, size, event); | |
3679 | return; | |
3680 | } | |
3681 | ||
3682 | if (event->flags & EVENT_FL_ISBPRINT) { | |
3683 | bprint_fmt = get_bprint_format(data, size, event); | |
3684 | args = make_bprint_args(bprint_fmt, data, size, event); | |
3685 | arg = args; | |
3686 | ptr = bprint_fmt; | |
3687 | } | |
3688 | ||
3689 | for (; *ptr; ptr++) { | |
3690 | ls = 0; | |
3691 | if (*ptr == '\\') { | |
3692 | ptr++; | |
3693 | switch (*ptr) { | |
3694 | case 'n': | |
3695 | trace_seq_putc(s, '\n'); | |
3696 | break; | |
3697 | case 't': | |
3698 | trace_seq_putc(s, '\t'); | |
3699 | break; | |
3700 | case 'r': | |
3701 | trace_seq_putc(s, '\r'); | |
3702 | break; | |
3703 | case '\\': | |
3704 | trace_seq_putc(s, '\\'); | |
3705 | break; | |
3706 | default: | |
3707 | trace_seq_putc(s, *ptr); | |
3708 | break; | |
3709 | } | |
3710 | ||
3711 | } else if (*ptr == '%') { | |
3712 | saveptr = ptr; | |
3713 | show_func = 0; | |
3714 | len_as_arg = 0; | |
3715 | cont_process: | |
3716 | ptr++; | |
3717 | switch (*ptr) { | |
3718 | case '%': | |
3719 | trace_seq_putc(s, '%'); | |
3720 | break; | |
3721 | case '#': | |
3722 | /* FIXME: need to handle properly */ | |
3723 | goto cont_process; | |
3724 | case 'h': | |
3725 | ls--; | |
3726 | goto cont_process; | |
3727 | case 'l': | |
3728 | ls++; | |
3729 | goto cont_process; | |
3730 | case 'L': | |
3731 | ls = 2; | |
3732 | goto cont_process; | |
3733 | case '*': | |
3734 | /* The argument is the length. */ | |
3735 | if (!arg) | |
3736 | die("no argument match"); | |
3737 | len_arg = eval_num_arg(data, size, event, arg); | |
3738 | len_as_arg = 1; | |
3739 | arg = arg->next; | |
3740 | goto cont_process; | |
3741 | case '.': | |
3742 | case 'z': | |
3743 | case 'Z': | |
3744 | case '0' ... '9': | |
3745 | goto cont_process; | |
3746 | case 'p': | |
3747 | if (pevent->long_size == 4) | |
3748 | ls = 1; | |
3749 | else | |
3750 | ls = 2; | |
3751 | ||
3752 | if (*(ptr+1) == 'F' || | |
3753 | *(ptr+1) == 'f') { | |
3754 | ptr++; | |
3755 | show_func = *ptr; | |
3756 | } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') { | |
3757 | print_mac_arg(s, *(ptr+1), data, size, event, arg); | |
3758 | ptr++; | |
3759 | break; | |
3760 | } | |
3761 | ||
3762 | /* fall through */ | |
3763 | case 'd': | |
3764 | case 'i': | |
3765 | case 'x': | |
3766 | case 'X': | |
3767 | case 'u': | |
3768 | if (!arg) | |
3769 | die("no argument match"); | |
3770 | ||
3771 | len = ((unsigned long)ptr + 1) - | |
3772 | (unsigned long)saveptr; | |
3773 | ||
3774 | /* should never happen */ | |
3775 | if (len > 31) | |
3776 | die("bad format!"); | |
3777 | ||
3778 | memcpy(format, saveptr, len); | |
3779 | format[len] = 0; | |
3780 | ||
3781 | val = eval_num_arg(data, size, event, arg); | |
3782 | arg = arg->next; | |
3783 | ||
3784 | if (show_func) { | |
3785 | func = find_func(pevent, val); | |
3786 | if (func) { | |
3787 | trace_seq_puts(s, func->func); | |
3788 | if (show_func == 'F') | |
3789 | trace_seq_printf(s, | |
3790 | "+0x%llx", | |
3791 | val - func->addr); | |
3792 | break; | |
3793 | } | |
3794 | } | |
3795 | if (pevent->long_size == 8 && ls) { | |
3796 | char *p; | |
3797 | ||
3798 | ls = 2; | |
3799 | /* make %l into %ll */ | |
3800 | p = strchr(format, 'l'); | |
3801 | if (p) | |
3802 | memmove(p, p+1, strlen(p)+1); | |
3803 | else if (strcmp(format, "%p") == 0) | |
3804 | strcpy(format, "0x%llx"); | |
3805 | } | |
3806 | switch (ls) { | |
3807 | case -2: | |
3808 | if (len_as_arg) | |
3809 | trace_seq_printf(s, format, len_arg, (char)val); | |
3810 | else | |
3811 | trace_seq_printf(s, format, (char)val); | |
3812 | break; | |
3813 | case -1: | |
3814 | if (len_as_arg) | |
3815 | trace_seq_printf(s, format, len_arg, (short)val); | |
3816 | else | |
3817 | trace_seq_printf(s, format, (short)val); | |
3818 | break; | |
3819 | case 0: | |
3820 | if (len_as_arg) | |
3821 | trace_seq_printf(s, format, len_arg, (int)val); | |
3822 | else | |
3823 | trace_seq_printf(s, format, (int)val); | |
3824 | break; | |
3825 | case 1: | |
3826 | if (len_as_arg) | |
3827 | trace_seq_printf(s, format, len_arg, (long)val); | |
3828 | else | |
3829 | trace_seq_printf(s, format, (long)val); | |
3830 | break; | |
3831 | case 2: | |
3832 | if (len_as_arg) | |
3833 | trace_seq_printf(s, format, len_arg, | |
3834 | (long long)val); | |
3835 | else | |
3836 | trace_seq_printf(s, format, (long long)val); | |
3837 | break; | |
3838 | default: | |
3839 | die("bad count (%d)", ls); | |
3840 | } | |
3841 | break; | |
3842 | case 's': | |
3843 | if (!arg) | |
3844 | die("no matching argument"); | |
3845 | ||
3846 | len = ((unsigned long)ptr + 1) - | |
3847 | (unsigned long)saveptr; | |
3848 | ||
3849 | /* should never happen */ | |
3850 | if (len > 31) | |
3851 | die("bad format!"); | |
3852 | ||
3853 | memcpy(format, saveptr, len); | |
3854 | format[len] = 0; | |
3855 | if (!len_as_arg) | |
3856 | len_arg = -1; | |
3857 | print_str_arg(s, data, size, event, | |
3858 | format, len_arg, arg); | |
3859 | arg = arg->next; | |
3860 | break; | |
3861 | default: | |
3862 | trace_seq_printf(s, ">%c<", *ptr); | |
3863 | ||
3864 | } | |
3865 | } else | |
3866 | trace_seq_putc(s, *ptr); | |
3867 | } | |
3868 | ||
3869 | if (args) { | |
3870 | free_args(args); | |
3871 | free(bprint_fmt); | |
3872 | } | |
3873 | } | |
3874 | ||
3875 | /** | |
3876 | * pevent_data_lat_fmt - parse the data for the latency format | |
3877 | * @pevent: a handle to the pevent | |
3878 | * @s: the trace_seq to write to | |
3879 | * @data: the raw data to read from | |
3880 | * @size: currently unused. | |
3881 | * | |
3882 | * This parses out the Latency format (interrupts disabled, | |
3883 | * need rescheduling, in hard/soft interrupt, preempt count | |
3884 | * and lock depth) and places it into the trace_seq. | |
3885 | */ | |
3886 | void pevent_data_lat_fmt(struct pevent *pevent, | |
1c698186 | 3887 | struct trace_seq *s, struct pevent_record *record) |
f7d82350 SR |
3888 | { |
3889 | static int check_lock_depth = 1; | |
3890 | static int lock_depth_exists; | |
3891 | unsigned int lat_flags; | |
3892 | unsigned int pc; | |
3893 | int lock_depth; | |
3894 | int hardirq; | |
3895 | int softirq; | |
3896 | void *data = record->data; | |
3897 | ||
3898 | lat_flags = parse_common_flags(pevent, data); | |
3899 | pc = parse_common_pc(pevent, data); | |
3900 | /* lock_depth may not always exist */ | |
3901 | if (check_lock_depth) { | |
3902 | struct format_field *field; | |
3903 | struct event_format *event; | |
3904 | ||
3905 | check_lock_depth = 0; | |
3906 | event = pevent->events[0]; | |
3907 | field = pevent_find_common_field(event, "common_lock_depth"); | |
3908 | if (field) | |
3909 | lock_depth_exists = 1; | |
3910 | } | |
3911 | if (lock_depth_exists) | |
3912 | lock_depth = parse_common_lock_depth(pevent, data); | |
3913 | ||
3914 | hardirq = lat_flags & TRACE_FLAG_HARDIRQ; | |
3915 | softirq = lat_flags & TRACE_FLAG_SOFTIRQ; | |
3916 | ||
3917 | trace_seq_printf(s, "%c%c%c", | |
3918 | (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : | |
3919 | (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? | |
3920 | 'X' : '.', | |
3921 | (lat_flags & TRACE_FLAG_NEED_RESCHED) ? | |
3922 | 'N' : '.', | |
3923 | (hardirq && softirq) ? 'H' : | |
3924 | hardirq ? 'h' : softirq ? 's' : '.'); | |
3925 | ||
3926 | if (pc) | |
3927 | trace_seq_printf(s, "%x", pc); | |
3928 | else | |
3929 | trace_seq_putc(s, '.'); | |
3930 | ||
3931 | if (lock_depth_exists) { | |
3932 | if (lock_depth < 0) | |
3933 | trace_seq_putc(s, '.'); | |
3934 | else | |
3935 | trace_seq_printf(s, "%d", lock_depth); | |
3936 | } | |
3937 | ||
3938 | trace_seq_terminate(s); | |
3939 | } | |
3940 | ||
3941 | /** | |
3942 | * pevent_data_type - parse out the given event type | |
3943 | * @pevent: a handle to the pevent | |
3944 | * @rec: the record to read from | |
3945 | * | |
3946 | * This returns the event id from the @rec. | |
3947 | */ | |
1c698186 | 3948 | int pevent_data_type(struct pevent *pevent, struct pevent_record *rec) |
f7d82350 SR |
3949 | { |
3950 | return trace_parse_common_type(pevent, rec->data); | |
3951 | } | |
3952 | ||
3953 | /** | |
3954 | * pevent_data_event_from_type - find the event by a given type | |
3955 | * @pevent: a handle to the pevent | |
3956 | * @type: the type of the event. | |
3957 | * | |
3958 | * This returns the event form a given @type; | |
3959 | */ | |
3960 | struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type) | |
3961 | { | |
3962 | return pevent_find_event(pevent, type); | |
3963 | } | |
3964 | ||
3965 | /** | |
3966 | * pevent_data_pid - parse the PID from raw data | |
3967 | * @pevent: a handle to the pevent | |
3968 | * @rec: the record to parse | |
3969 | * | |
3970 | * This returns the PID from a raw data. | |
3971 | */ | |
1c698186 | 3972 | int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec) |
f7d82350 SR |
3973 | { |
3974 | return parse_common_pid(pevent, rec->data); | |
3975 | } | |
3976 | ||
3977 | /** | |
3978 | * pevent_data_comm_from_pid - return the command line from PID | |
3979 | * @pevent: a handle to the pevent | |
3980 | * @pid: the PID of the task to search for | |
3981 | * | |
3982 | * This returns a pointer to the command line that has the given | |
3983 | * @pid. | |
3984 | */ | |
3985 | const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid) | |
3986 | { | |
3987 | const char *comm; | |
3988 | ||
3989 | comm = find_cmdline(pevent, pid); | |
3990 | return comm; | |
3991 | } | |
3992 | ||
3993 | /** | |
3994 | * pevent_data_comm_from_pid - parse the data into the print format | |
3995 | * @s: the trace_seq to write to | |
3996 | * @event: the handle to the event | |
3997 | * @cpu: the cpu the event was recorded on | |
3998 | * @data: the raw data | |
3999 | * @size: the size of the raw data | |
4000 | * @nsecs: the timestamp of the event | |
4001 | * | |
4002 | * This parses the raw @data using the given @event information and | |
4003 | * writes the print format into the trace_seq. | |
4004 | */ | |
4005 | void pevent_event_info(struct trace_seq *s, struct event_format *event, | |
1c698186 | 4006 | struct pevent_record *record) |
f7d82350 SR |
4007 | { |
4008 | int print_pretty = 1; | |
4009 | ||
4010 | if (event->pevent->print_raw) | |
4011 | print_event_fields(s, record->data, record->size, event); | |
4012 | else { | |
4013 | ||
4014 | if (event->handler) | |
4015 | print_pretty = event->handler(s, record, event, | |
4016 | event->context); | |
4017 | ||
4018 | if (print_pretty) | |
4019 | pretty_print(s, record->data, record->size, event); | |
4020 | } | |
4021 | ||
4022 | trace_seq_terminate(s); | |
4023 | } | |
4024 | ||
4025 | void pevent_print_event(struct pevent *pevent, struct trace_seq *s, | |
1c698186 | 4026 | struct pevent_record *record) |
f7d82350 SR |
4027 | { |
4028 | static char *spaces = " "; /* 20 spaces */ | |
4029 | struct event_format *event; | |
4030 | unsigned long secs; | |
4031 | unsigned long usecs; | |
4dc1024a | 4032 | unsigned long nsecs; |
f7d82350 SR |
4033 | const char *comm; |
4034 | void *data = record->data; | |
4035 | int type; | |
4036 | int pid; | |
4037 | int len; | |
4dc1024a | 4038 | int p; |
f7d82350 SR |
4039 | |
4040 | secs = record->ts / NSECS_PER_SEC; | |
4dc1024a | 4041 | nsecs = record->ts - secs * NSECS_PER_SEC; |
f7d82350 SR |
4042 | |
4043 | if (record->size < 0) { | |
4044 | do_warning("ug! negative record size %d", record->size); | |
4045 | return; | |
4046 | } | |
4047 | ||
4048 | type = trace_parse_common_type(pevent, data); | |
4049 | ||
4050 | event = pevent_find_event(pevent, type); | |
4051 | if (!event) { | |
4052 | do_warning("ug! no event found for type %d", type); | |
4053 | return; | |
4054 | } | |
4055 | ||
4056 | pid = parse_common_pid(pevent, data); | |
4057 | comm = find_cmdline(pevent, pid); | |
4058 | ||
4059 | if (pevent->latency_format) { | |
4060 | trace_seq_printf(s, "%8.8s-%-5d %3d", | |
4061 | comm, pid, record->cpu); | |
4062 | pevent_data_lat_fmt(pevent, s, record); | |
4063 | } else | |
4064 | trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu); | |
4065 | ||
4dc1024a SR |
4066 | if (pevent->flags & PEVENT_NSEC_OUTPUT) { |
4067 | usecs = nsecs; | |
4068 | p = 9; | |
4069 | } else { | |
4070 | usecs = (nsecs + 500) / NSECS_PER_USEC; | |
4071 | p = 6; | |
4072 | } | |
4073 | ||
4074 | trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name); | |
f7d82350 SR |
4075 | |
4076 | /* Space out the event names evenly. */ | |
4077 | len = strlen(event->name); | |
4078 | if (len < 20) | |
4079 | trace_seq_printf(s, "%.*s", 20 - len, spaces); | |
4080 | ||
4081 | pevent_event_info(s, event, record); | |
4082 | } | |
4083 | ||
4084 | static int events_id_cmp(const void *a, const void *b) | |
4085 | { | |
4086 | struct event_format * const * ea = a; | |
4087 | struct event_format * const * eb = b; | |
4088 | ||
4089 | if ((*ea)->id < (*eb)->id) | |
4090 | return -1; | |
4091 | ||
4092 | if ((*ea)->id > (*eb)->id) | |
4093 | return 1; | |
4094 | ||
4095 | return 0; | |
4096 | } | |
4097 | ||
4098 | static int events_name_cmp(const void *a, const void *b) | |
4099 | { | |
4100 | struct event_format * const * ea = a; | |
4101 | struct event_format * const * eb = b; | |
4102 | int res; | |
4103 | ||
4104 | res = strcmp((*ea)->name, (*eb)->name); | |
4105 | if (res) | |
4106 | return res; | |
4107 | ||
4108 | res = strcmp((*ea)->system, (*eb)->system); | |
4109 | if (res) | |
4110 | return res; | |
4111 | ||
4112 | return events_id_cmp(a, b); | |
4113 | } | |
4114 | ||
4115 | static int events_system_cmp(const void *a, const void *b) | |
4116 | { | |
4117 | struct event_format * const * ea = a; | |
4118 | struct event_format * const * eb = b; | |
4119 | int res; | |
4120 | ||
4121 | res = strcmp((*ea)->system, (*eb)->system); | |
4122 | if (res) | |
4123 | return res; | |
4124 | ||
4125 | res = strcmp((*ea)->name, (*eb)->name); | |
4126 | if (res) | |
4127 | return res; | |
4128 | ||
4129 | return events_id_cmp(a, b); | |
4130 | } | |
4131 | ||
4132 | struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type) | |
4133 | { | |
4134 | struct event_format **events; | |
4135 | int (*sort)(const void *a, const void *b); | |
4136 | ||
4137 | events = pevent->sort_events; | |
4138 | ||
4139 | if (events && pevent->last_type == sort_type) | |
4140 | return events; | |
4141 | ||
4142 | if (!events) { | |
4143 | events = malloc(sizeof(*events) * (pevent->nr_events + 1)); | |
4144 | if (!events) | |
4145 | return NULL; | |
4146 | ||
4147 | memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events); | |
4148 | events[pevent->nr_events] = NULL; | |
4149 | ||
4150 | pevent->sort_events = events; | |
4151 | ||
4152 | /* the internal events are sorted by id */ | |
4153 | if (sort_type == EVENT_SORT_ID) { | |
4154 | pevent->last_type = sort_type; | |
4155 | return events; | |
4156 | } | |
4157 | } | |
4158 | ||
4159 | switch (sort_type) { | |
4160 | case EVENT_SORT_ID: | |
4161 | sort = events_id_cmp; | |
4162 | break; | |
4163 | case EVENT_SORT_NAME: | |
4164 | sort = events_name_cmp; | |
4165 | break; | |
4166 | case EVENT_SORT_SYSTEM: | |
4167 | sort = events_system_cmp; | |
4168 | break; | |
4169 | default: | |
4170 | return events; | |
4171 | } | |
4172 | ||
4173 | qsort(events, pevent->nr_events, sizeof(*events), sort); | |
4174 | pevent->last_type = sort_type; | |
4175 | ||
4176 | return events; | |
4177 | } | |
4178 | ||
4179 | static struct format_field ** | |
4180 | get_event_fields(const char *type, const char *name, | |
4181 | int count, struct format_field *list) | |
4182 | { | |
4183 | struct format_field **fields; | |
4184 | struct format_field *field; | |
4185 | int i = 0; | |
4186 | ||
4187 | fields = malloc_or_die(sizeof(*fields) * (count + 1)); | |
4188 | for (field = list; field; field = field->next) { | |
4189 | fields[i++] = field; | |
4190 | if (i == count + 1) { | |
4191 | do_warning("event %s has more %s fields than specified", | |
4192 | name, type); | |
4193 | i--; | |
4194 | break; | |
4195 | } | |
4196 | } | |
4197 | ||
4198 | if (i != count) | |
4199 | do_warning("event %s has less %s fields than specified", | |
4200 | name, type); | |
4201 | ||
4202 | fields[i] = NULL; | |
4203 | ||
4204 | return fields; | |
4205 | } | |
4206 | ||
4207 | /** | |
4208 | * pevent_event_common_fields - return a list of common fields for an event | |
4209 | * @event: the event to return the common fields of. | |
4210 | * | |
4211 | * Returns an allocated array of fields. The last item in the array is NULL. | |
4212 | * The array must be freed with free(). | |
4213 | */ | |
4214 | struct format_field **pevent_event_common_fields(struct event_format *event) | |
4215 | { | |
4216 | return get_event_fields("common", event->name, | |
4217 | event->format.nr_common, | |
4218 | event->format.common_fields); | |
4219 | } | |
4220 | ||
4221 | /** | |
4222 | * pevent_event_fields - return a list of event specific fields for an event | |
4223 | * @event: the event to return the fields of. | |
4224 | * | |
4225 | * Returns an allocated array of fields. The last item in the array is NULL. | |
4226 | * The array must be freed with free(). | |
4227 | */ | |
4228 | struct format_field **pevent_event_fields(struct event_format *event) | |
4229 | { | |
4230 | return get_event_fields("event", event->name, | |
4231 | event->format.nr_fields, | |
4232 | event->format.fields); | |
4233 | } | |
4234 | ||
4235 | static void print_fields(struct trace_seq *s, struct print_flag_sym *field) | |
4236 | { | |
4237 | trace_seq_printf(s, "{ %s, %s }", field->value, field->str); | |
4238 | if (field->next) { | |
4239 | trace_seq_puts(s, ", "); | |
4240 | print_fields(s, field->next); | |
4241 | } | |
4242 | } | |
4243 | ||
4244 | /* for debugging */ | |
4245 | static void print_args(struct print_arg *args) | |
4246 | { | |
4247 | int print_paren = 1; | |
4248 | struct trace_seq s; | |
4249 | ||
4250 | switch (args->type) { | |
4251 | case PRINT_NULL: | |
4252 | printf("null"); | |
4253 | break; | |
4254 | case PRINT_ATOM: | |
4255 | printf("%s", args->atom.atom); | |
4256 | break; | |
4257 | case PRINT_FIELD: | |
4258 | printf("REC->%s", args->field.name); | |
4259 | break; | |
4260 | case PRINT_FLAGS: | |
4261 | printf("__print_flags("); | |
4262 | print_args(args->flags.field); | |
4263 | printf(", %s, ", args->flags.delim); | |
4264 | trace_seq_init(&s); | |
4265 | print_fields(&s, args->flags.flags); | |
4266 | trace_seq_do_printf(&s); | |
4267 | trace_seq_destroy(&s); | |
4268 | printf(")"); | |
4269 | break; | |
4270 | case PRINT_SYMBOL: | |
4271 | printf("__print_symbolic("); | |
4272 | print_args(args->symbol.field); | |
4273 | printf(", "); | |
4274 | trace_seq_init(&s); | |
4275 | print_fields(&s, args->symbol.symbols); | |
4276 | trace_seq_do_printf(&s); | |
4277 | trace_seq_destroy(&s); | |
4278 | printf(")"); | |
4279 | break; | |
4280 | case PRINT_STRING: | |
4281 | case PRINT_BSTRING: | |
4282 | printf("__get_str(%s)", args->string.string); | |
4283 | break; | |
4284 | case PRINT_TYPE: | |
4285 | printf("(%s)", args->typecast.type); | |
4286 | print_args(args->typecast.item); | |
4287 | break; | |
4288 | case PRINT_OP: | |
4289 | if (strcmp(args->op.op, ":") == 0) | |
4290 | print_paren = 0; | |
4291 | if (print_paren) | |
4292 | printf("("); | |
4293 | print_args(args->op.left); | |
4294 | printf(" %s ", args->op.op); | |
4295 | print_args(args->op.right); | |
4296 | if (print_paren) | |
4297 | printf(")"); | |
4298 | break; | |
4299 | default: | |
4300 | /* we should warn... */ | |
4301 | return; | |
4302 | } | |
4303 | if (args->next) { | |
4304 | printf("\n"); | |
4305 | print_args(args->next); | |
4306 | } | |
4307 | } | |
4308 | ||
4309 | static void parse_header_field(const char *field, | |
4310 | int *offset, int *size, int mandatory) | |
4311 | { | |
4312 | unsigned long long save_input_buf_ptr; | |
4313 | unsigned long long save_input_buf_siz; | |
4314 | char *token; | |
4315 | int type; | |
4316 | ||
4317 | save_input_buf_ptr = input_buf_ptr; | |
4318 | save_input_buf_siz = input_buf_siz; | |
4319 | ||
4320 | if (read_expected(EVENT_ITEM, "field") < 0) | |
4321 | return; | |
4322 | if (read_expected(EVENT_OP, ":") < 0) | |
4323 | return; | |
4324 | ||
4325 | /* type */ | |
4326 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
4327 | goto fail; | |
4328 | free_token(token); | |
4329 | ||
4330 | /* | |
4331 | * If this is not a mandatory field, then test it first. | |
4332 | */ | |
4333 | if (mandatory) { | |
4334 | if (read_expected(EVENT_ITEM, field) < 0) | |
4335 | return; | |
4336 | } else { | |
4337 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
4338 | goto fail; | |
4339 | if (strcmp(token, field) != 0) | |
4340 | goto discard; | |
4341 | free_token(token); | |
4342 | } | |
4343 | ||
4344 | if (read_expected(EVENT_OP, ";") < 0) | |
4345 | return; | |
4346 | if (read_expected(EVENT_ITEM, "offset") < 0) | |
4347 | return; | |
4348 | if (read_expected(EVENT_OP, ":") < 0) | |
4349 | return; | |
4350 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
4351 | goto fail; | |
4352 | *offset = atoi(token); | |
4353 | free_token(token); | |
4354 | if (read_expected(EVENT_OP, ";") < 0) | |
4355 | return; | |
4356 | if (read_expected(EVENT_ITEM, "size") < 0) | |
4357 | return; | |
4358 | if (read_expected(EVENT_OP, ":") < 0) | |
4359 | return; | |
4360 | if (read_expect_type(EVENT_ITEM, &token) < 0) | |
4361 | goto fail; | |
4362 | *size = atoi(token); | |
4363 | free_token(token); | |
4364 | if (read_expected(EVENT_OP, ";") < 0) | |
4365 | return; | |
4366 | type = read_token(&token); | |
4367 | if (type != EVENT_NEWLINE) { | |
4368 | /* newer versions of the kernel have a "signed" type */ | |
4369 | if (type != EVENT_ITEM) | |
4370 | goto fail; | |
4371 | ||
4372 | if (strcmp(token, "signed") != 0) | |
4373 | goto fail; | |
4374 | ||
4375 | free_token(token); | |
4376 | ||
4377 | if (read_expected(EVENT_OP, ":") < 0) | |
4378 | return; | |
4379 | ||
4380 | if (read_expect_type(EVENT_ITEM, &token)) | |
4381 | goto fail; | |
4382 | ||
4383 | free_token(token); | |
4384 | if (read_expected(EVENT_OP, ";") < 0) | |
4385 | return; | |
4386 | ||
4387 | if (read_expect_type(EVENT_NEWLINE, &token)) | |
4388 | goto fail; | |
4389 | } | |
4390 | fail: | |
4391 | free_token(token); | |
4392 | return; | |
4393 | ||
4394 | discard: | |
4395 | input_buf_ptr = save_input_buf_ptr; | |
4396 | input_buf_siz = save_input_buf_siz; | |
4397 | *offset = 0; | |
4398 | *size = 0; | |
4399 | free_token(token); | |
4400 | } | |
4401 | ||
4402 | /** | |
4403 | * pevent_parse_header_page - parse the data stored in the header page | |
4404 | * @pevent: the handle to the pevent | |
4405 | * @buf: the buffer storing the header page format string | |
4406 | * @size: the size of @buf | |
4407 | * @long_size: the long size to use if there is no header | |
4408 | * | |
4409 | * This parses the header page format for information on the | |
4410 | * ring buffer used. The @buf should be copied from | |
4411 | * | |
4412 | * /sys/kernel/debug/tracing/events/header_page | |
4413 | */ | |
4414 | int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size, | |
4415 | int long_size) | |
4416 | { | |
4417 | int ignore; | |
4418 | ||
4419 | if (!size) { | |
4420 | /* | |
4421 | * Old kernels did not have header page info. | |
4422 | * Sorry but we just use what we find here in user space. | |
4423 | */ | |
4424 | pevent->header_page_ts_size = sizeof(long long); | |
4425 | pevent->header_page_size_size = long_size; | |
4426 | pevent->header_page_data_offset = sizeof(long long) + long_size; | |
4427 | pevent->old_format = 1; | |
4428 | return -1; | |
4429 | } | |
4430 | init_input_buf(buf, size); | |
4431 | ||
4432 | parse_header_field("timestamp", &pevent->header_page_ts_offset, | |
4433 | &pevent->header_page_ts_size, 1); | |
4434 | parse_header_field("commit", &pevent->header_page_size_offset, | |
4435 | &pevent->header_page_size_size, 1); | |
4436 | parse_header_field("overwrite", &pevent->header_page_overwrite, | |
4437 | &ignore, 0); | |
4438 | parse_header_field("data", &pevent->header_page_data_offset, | |
4439 | &pevent->header_page_data_size, 1); | |
4440 | ||
4441 | return 0; | |
4442 | } | |
4443 | ||
4444 | static int event_matches(struct event_format *event, | |
4445 | int id, const char *sys_name, | |
4446 | const char *event_name) | |
4447 | { | |
4448 | if (id >= 0 && id != event->id) | |
4449 | return 0; | |
4450 | ||
4451 | if (event_name && (strcmp(event_name, event->name) != 0)) | |
4452 | return 0; | |
4453 | ||
4454 | if (sys_name && (strcmp(sys_name, event->system) != 0)) | |
4455 | return 0; | |
4456 | ||
4457 | return 1; | |
4458 | } | |
4459 | ||
4460 | static void free_handler(struct event_handler *handle) | |
4461 | { | |
4462 | free((void *)handle->sys_name); | |
4463 | free((void *)handle->event_name); | |
4464 | free(handle); | |
4465 | } | |
4466 | ||
4467 | static int find_event_handle(struct pevent *pevent, struct event_format *event) | |
4468 | { | |
4469 | struct event_handler *handle, **next; | |
4470 | ||
4471 | for (next = &pevent->handlers; *next; | |
4472 | next = &(*next)->next) { | |
4473 | handle = *next; | |
4474 | if (event_matches(event, handle->id, | |
4475 | handle->sys_name, | |
4476 | handle->event_name)) | |
4477 | break; | |
4478 | } | |
4479 | ||
4480 | if (!(*next)) | |
4481 | return 0; | |
4482 | ||
4483 | pr_stat("overriding event (%d) %s:%s with new print handler", | |
4484 | event->id, event->system, event->name); | |
4485 | ||
4486 | event->handler = handle->func; | |
4487 | event->context = handle->context; | |
4488 | ||
4489 | *next = handle->next; | |
4490 | free_handler(handle); | |
4491 | ||
4492 | return 1; | |
4493 | } | |
4494 | ||
4495 | /** | |
4496 | * pevent_parse_event - parse the event format | |
4497 | * @pevent: the handle to the pevent | |
4498 | * @buf: the buffer storing the event format string | |
4499 | * @size: the size of @buf | |
4500 | * @sys: the system the event belongs to | |
4501 | * | |
4502 | * This parses the event format and creates an event structure | |
4503 | * to quickly parse raw data for a given event. | |
4504 | * | |
4505 | * These files currently come from: | |
4506 | * | |
4507 | * /sys/kernel/debug/tracing/events/.../.../format | |
4508 | */ | |
4509 | int pevent_parse_event(struct pevent *pevent, | |
4510 | const char *buf, unsigned long size, | |
4511 | const char *sys) | |
4512 | { | |
4513 | struct event_format *event; | |
4514 | int ret; | |
4515 | ||
4516 | init_input_buf(buf, size); | |
4517 | ||
4518 | event = alloc_event(); | |
4519 | if (!event) | |
4520 | return -ENOMEM; | |
4521 | ||
4522 | event->name = event_read_name(); | |
4523 | if (!event->name) { | |
4524 | /* Bad event? */ | |
4525 | free(event); | |
4526 | return -1; | |
4527 | } | |
4528 | ||
4529 | if (strcmp(sys, "ftrace") == 0) { | |
4530 | ||
4531 | event->flags |= EVENT_FL_ISFTRACE; | |
4532 | ||
4533 | if (strcmp(event->name, "bprint") == 0) | |
4534 | event->flags |= EVENT_FL_ISBPRINT; | |
4535 | } | |
4536 | ||
4537 | event->id = event_read_id(); | |
4538 | if (event->id < 0) | |
4539 | die("failed to read event id"); | |
4540 | ||
4541 | event->system = strdup(sys); | |
4542 | ||
4543 | /* Add pevent to event so that it can be referenced */ | |
4544 | event->pevent = pevent; | |
4545 | ||
4546 | ret = event_read_format(event); | |
4547 | if (ret < 0) { | |
4548 | do_warning("failed to read event format for %s", event->name); | |
4549 | goto event_failed; | |
4550 | } | |
4551 | ||
4552 | /* | |
4553 | * If the event has an override, don't print warnings if the event | |
4554 | * print format fails to parse. | |
4555 | */ | |
4556 | if (find_event_handle(pevent, event)) | |
4557 | show_warning = 0; | |
4558 | ||
4559 | ret = event_read_print(event); | |
4560 | if (ret < 0) { | |
4561 | do_warning("failed to read event print fmt for %s", | |
4562 | event->name); | |
4563 | show_warning = 1; | |
4564 | goto event_failed; | |
4565 | } | |
4566 | show_warning = 1; | |
4567 | ||
4568 | add_event(pevent, event); | |
4569 | ||
4570 | if (!ret && (event->flags & EVENT_FL_ISFTRACE)) { | |
4571 | struct format_field *field; | |
4572 | struct print_arg *arg, **list; | |
4573 | ||
4574 | /* old ftrace had no args */ | |
4575 | ||
4576 | list = &event->print_fmt.args; | |
4577 | for (field = event->format.fields; field; field = field->next) { | |
4578 | arg = alloc_arg(); | |
4579 | *list = arg; | |
4580 | list = &arg->next; | |
4581 | arg->type = PRINT_FIELD; | |
4582 | arg->field.name = strdup(field->name); | |
4583 | arg->field.field = field; | |
4584 | } | |
4585 | return 0; | |
4586 | } | |
4587 | ||
4588 | #define PRINT_ARGS 0 | |
4589 | if (PRINT_ARGS && event->print_fmt.args) | |
4590 | print_args(event->print_fmt.args); | |
4591 | ||
4592 | return 0; | |
4593 | ||
4594 | event_failed: | |
4595 | event->flags |= EVENT_FL_FAILED; | |
4596 | /* still add it even if it failed */ | |
4597 | add_event(pevent, event); | |
4598 | return -1; | |
4599 | } | |
4600 | ||
4601 | int get_field_val(struct trace_seq *s, struct format_field *field, | |
1c698186 | 4602 | const char *name, struct pevent_record *record, |
f7d82350 SR |
4603 | unsigned long long *val, int err) |
4604 | { | |
4605 | if (!field) { | |
4606 | if (err) | |
4607 | trace_seq_printf(s, "<CANT FIND FIELD %s>", name); | |
4608 | return -1; | |
4609 | } | |
4610 | ||
4611 | if (pevent_read_number_field(field, record->data, val)) { | |
4612 | if (err) | |
4613 | trace_seq_printf(s, " %s=INVALID", name); | |
4614 | return -1; | |
4615 | } | |
4616 | ||
4617 | return 0; | |
4618 | } | |
4619 | ||
4620 | /** | |
4621 | * pevent_get_field_raw - return the raw pointer into the data field | |
4622 | * @s: The seq to print to on error | |
4623 | * @event: the event that the field is for | |
4624 | * @name: The name of the field | |
4625 | * @record: The record with the field name. | |
4626 | * @len: place to store the field length. | |
4627 | * @err: print default error if failed. | |
4628 | * | |
4629 | * Returns a pointer into record->data of the field and places | |
4630 | * the length of the field in @len. | |
4631 | * | |
4632 | * On failure, it returns NULL. | |
4633 | */ | |
4634 | void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event, | |
1c698186 | 4635 | const char *name, struct pevent_record *record, |
f7d82350 SR |
4636 | int *len, int err) |
4637 | { | |
4638 | struct format_field *field; | |
4639 | void *data = record->data; | |
4640 | unsigned offset; | |
4641 | int dummy; | |
4642 | ||
4643 | if (!event) | |
4644 | return NULL; | |
4645 | ||
4646 | field = pevent_find_field(event, name); | |
4647 | ||
4648 | if (!field) { | |
4649 | if (err) | |
4650 | trace_seq_printf(s, "<CANT FIND FIELD %s>", name); | |
4651 | return NULL; | |
4652 | } | |
4653 | ||
4654 | /* Allow @len to be NULL */ | |
4655 | if (!len) | |
4656 | len = &dummy; | |
4657 | ||
4658 | offset = field->offset; | |
4659 | if (field->flags & FIELD_IS_DYNAMIC) { | |
4660 | offset = pevent_read_number(event->pevent, | |
4661 | data + offset, field->size); | |
4662 | *len = offset >> 16; | |
4663 | offset &= 0xffff; | |
4664 | } else | |
4665 | *len = field->size; | |
4666 | ||
4667 | return data + offset; | |
4668 | } | |
4669 | ||
4670 | /** | |
4671 | * pevent_get_field_val - find a field and return its value | |
4672 | * @s: The seq to print to on error | |
4673 | * @event: the event that the field is for | |
4674 | * @name: The name of the field | |
4675 | * @record: The record with the field name. | |
4676 | * @val: place to store the value of the field. | |
4677 | * @err: print default error if failed. | |
4678 | * | |
4679 | * Returns 0 on success -1 on field not found. | |
4680 | */ | |
4681 | int pevent_get_field_val(struct trace_seq *s, struct event_format *event, | |
1c698186 | 4682 | const char *name, struct pevent_record *record, |
f7d82350 SR |
4683 | unsigned long long *val, int err) |
4684 | { | |
4685 | struct format_field *field; | |
4686 | ||
4687 | if (!event) | |
4688 | return -1; | |
4689 | ||
4690 | field = pevent_find_field(event, name); | |
4691 | ||
4692 | return get_field_val(s, field, name, record, val, err); | |
4693 | } | |
4694 | ||
4695 | /** | |
4696 | * pevent_get_common_field_val - find a common field and return its value | |
4697 | * @s: The seq to print to on error | |
4698 | * @event: the event that the field is for | |
4699 | * @name: The name of the field | |
4700 | * @record: The record with the field name. | |
4701 | * @val: place to store the value of the field. | |
4702 | * @err: print default error if failed. | |
4703 | * | |
4704 | * Returns 0 on success -1 on field not found. | |
4705 | */ | |
4706 | int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event, | |
1c698186 | 4707 | const char *name, struct pevent_record *record, |
f7d82350 SR |
4708 | unsigned long long *val, int err) |
4709 | { | |
4710 | struct format_field *field; | |
4711 | ||
4712 | if (!event) | |
4713 | return -1; | |
4714 | ||
4715 | field = pevent_find_common_field(event, name); | |
4716 | ||
4717 | return get_field_val(s, field, name, record, val, err); | |
4718 | } | |
4719 | ||
4720 | /** | |
4721 | * pevent_get_any_field_val - find a any field and return its value | |
4722 | * @s: The seq to print to on error | |
4723 | * @event: the event that the field is for | |
4724 | * @name: The name of the field | |
4725 | * @record: The record with the field name. | |
4726 | * @val: place to store the value of the field. | |
4727 | * @err: print default error if failed. | |
4728 | * | |
4729 | * Returns 0 on success -1 on field not found. | |
4730 | */ | |
4731 | int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event, | |
1c698186 | 4732 | const char *name, struct pevent_record *record, |
f7d82350 SR |
4733 | unsigned long long *val, int err) |
4734 | { | |
4735 | struct format_field *field; | |
4736 | ||
4737 | if (!event) | |
4738 | return -1; | |
4739 | ||
4740 | field = pevent_find_any_field(event, name); | |
4741 | ||
4742 | return get_field_val(s, field, name, record, val, err); | |
4743 | } | |
4744 | ||
4745 | /** | |
4746 | * pevent_print_num_field - print a field and a format | |
4747 | * @s: The seq to print to | |
4748 | * @fmt: The printf format to print the field with. | |
4749 | * @event: the event that the field is for | |
4750 | * @name: The name of the field | |
4751 | * @record: The record with the field name. | |
4752 | * @err: print default error if failed. | |
4753 | * | |
4754 | * Returns: 0 on success, -1 field not fould, or 1 if buffer is full. | |
4755 | */ | |
4756 | int pevent_print_num_field(struct trace_seq *s, const char *fmt, | |
4757 | struct event_format *event, const char *name, | |
1c698186 | 4758 | struct pevent_record *record, int err) |
f7d82350 SR |
4759 | { |
4760 | struct format_field *field = pevent_find_field(event, name); | |
4761 | unsigned long long val; | |
4762 | ||
4763 | if (!field) | |
4764 | goto failed; | |
4765 | ||
4766 | if (pevent_read_number_field(field, record->data, &val)) | |
4767 | goto failed; | |
4768 | ||
4769 | return trace_seq_printf(s, fmt, val); | |
4770 | ||
4771 | failed: | |
4772 | if (err) | |
4773 | trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name); | |
4774 | return -1; | |
4775 | } | |
4776 | ||
4777 | static void free_func_handle(struct pevent_function_handler *func) | |
4778 | { | |
4779 | struct pevent_func_params *params; | |
4780 | ||
4781 | free(func->name); | |
4782 | ||
4783 | while (func->params) { | |
4784 | params = func->params; | |
4785 | func->params = params->next; | |
4786 | free(params); | |
4787 | } | |
4788 | ||
4789 | free(func); | |
4790 | } | |
4791 | ||
4792 | /** | |
4793 | * pevent_register_print_function - register a helper function | |
4794 | * @pevent: the handle to the pevent | |
4795 | * @func: the function to process the helper function | |
4796 | * @name: the name of the helper function | |
4797 | * @parameters: A list of enum pevent_func_arg_type | |
4798 | * | |
4799 | * Some events may have helper functions in the print format arguments. | |
4800 | * This allows a plugin to dynmically create a way to process one | |
4801 | * of these functions. | |
4802 | * | |
4803 | * The @parameters is a variable list of pevent_func_arg_type enums that | |
4804 | * must end with PEVENT_FUNC_ARG_VOID. | |
4805 | */ | |
4806 | int pevent_register_print_function(struct pevent *pevent, | |
4807 | pevent_func_handler func, | |
4808 | enum pevent_func_arg_type ret_type, | |
4809 | char *name, ...) | |
4810 | { | |
4811 | struct pevent_function_handler *func_handle; | |
4812 | struct pevent_func_params **next_param; | |
4813 | struct pevent_func_params *param; | |
4814 | enum pevent_func_arg_type type; | |
4815 | va_list ap; | |
4816 | ||
4817 | func_handle = find_func_handler(pevent, name); | |
4818 | if (func_handle) { | |
4819 | /* | |
4820 | * This is most like caused by the users own | |
4821 | * plugins updating the function. This overrides the | |
4822 | * system defaults. | |
4823 | */ | |
4824 | pr_stat("override of function helper '%s'", name); | |
4825 | remove_func_handler(pevent, name); | |
4826 | } | |
4827 | ||
4828 | func_handle = malloc_or_die(sizeof(*func_handle)); | |
4829 | memset(func_handle, 0, sizeof(*func_handle)); | |
4830 | ||
4831 | func_handle->ret_type = ret_type; | |
4832 | func_handle->name = strdup(name); | |
4833 | func_handle->func = func; | |
4834 | if (!func_handle->name) | |
4835 | die("Failed to allocate function name"); | |
4836 | ||
4837 | next_param = &(func_handle->params); | |
4838 | va_start(ap, name); | |
4839 | for (;;) { | |
4840 | type = va_arg(ap, enum pevent_func_arg_type); | |
4841 | if (type == PEVENT_FUNC_ARG_VOID) | |
4842 | break; | |
4843 | ||
4844 | if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) { | |
4845 | warning("Invalid argument type %d", type); | |
4846 | goto out_free; | |
4847 | } | |
4848 | ||
4849 | param = malloc_or_die(sizeof(*param)); | |
4850 | param->type = type; | |
4851 | param->next = NULL; | |
4852 | ||
4853 | *next_param = param; | |
4854 | next_param = &(param->next); | |
4855 | ||
4856 | func_handle->nr_args++; | |
4857 | } | |
4858 | va_end(ap); | |
4859 | ||
4860 | func_handle->next = pevent->func_handlers; | |
4861 | pevent->func_handlers = func_handle; | |
4862 | ||
4863 | return 0; | |
4864 | out_free: | |
4865 | va_end(ap); | |
4866 | free_func_handle(func_handle); | |
4867 | return -1; | |
4868 | } | |
4869 | ||
4870 | /** | |
4871 | * pevent_register_event_handle - register a way to parse an event | |
4872 | * @pevent: the handle to the pevent | |
4873 | * @id: the id of the event to register | |
4874 | * @sys_name: the system name the event belongs to | |
4875 | * @event_name: the name of the event | |
4876 | * @func: the function to call to parse the event information | |
4877 | * | |
4878 | * This function allows a developer to override the parsing of | |
4879 | * a given event. If for some reason the default print format | |
4880 | * is not sufficient, this function will register a function | |
4881 | * for an event to be used to parse the data instead. | |
4882 | * | |
4883 | * If @id is >= 0, then it is used to find the event. | |
4884 | * else @sys_name and @event_name are used. | |
4885 | */ | |
4886 | int pevent_register_event_handler(struct pevent *pevent, | |
4887 | int id, char *sys_name, char *event_name, | |
4888 | pevent_event_handler_func func, | |
4889 | void *context) | |
4890 | { | |
4891 | struct event_format *event; | |
4892 | struct event_handler *handle; | |
4893 | ||
4894 | if (id >= 0) { | |
4895 | /* search by id */ | |
4896 | event = pevent_find_event(pevent, id); | |
4897 | if (!event) | |
4898 | goto not_found; | |
4899 | if (event_name && (strcmp(event_name, event->name) != 0)) | |
4900 | goto not_found; | |
4901 | if (sys_name && (strcmp(sys_name, event->system) != 0)) | |
4902 | goto not_found; | |
4903 | } else { | |
4904 | event = pevent_find_event_by_name(pevent, sys_name, event_name); | |
4905 | if (!event) | |
4906 | goto not_found; | |
4907 | } | |
4908 | ||
4909 | pr_stat("overriding event (%d) %s:%s with new print handler", | |
4910 | event->id, event->system, event->name); | |
4911 | ||
4912 | event->handler = func; | |
4913 | event->context = context; | |
4914 | return 0; | |
4915 | ||
4916 | not_found: | |
4917 | /* Save for later use. */ | |
4918 | handle = malloc_or_die(sizeof(*handle)); | |
42c80139 | 4919 | memset(handle, 0, sizeof(*handle)); |
f7d82350 SR |
4920 | handle->id = id; |
4921 | if (event_name) | |
4922 | handle->event_name = strdup(event_name); | |
4923 | if (sys_name) | |
4924 | handle->sys_name = strdup(sys_name); | |
4925 | ||
4926 | handle->func = func; | |
4927 | handle->next = pevent->handlers; | |
4928 | pevent->handlers = handle; | |
4929 | handle->context = context; | |
4930 | ||
4931 | return -1; | |
4932 | } | |
4933 | ||
4934 | /** | |
4935 | * pevent_alloc - create a pevent handle | |
4936 | */ | |
4937 | struct pevent *pevent_alloc(void) | |
4938 | { | |
4939 | struct pevent *pevent; | |
4940 | ||
4941 | pevent = malloc(sizeof(*pevent)); | |
4942 | if (!pevent) | |
4943 | return NULL; | |
4944 | memset(pevent, 0, sizeof(*pevent)); | |
4945 | pevent->ref_count = 1; | |
4946 | ||
4947 | return pevent; | |
4948 | } | |
4949 | ||
4950 | void pevent_ref(struct pevent *pevent) | |
4951 | { | |
4952 | pevent->ref_count++; | |
4953 | } | |
4954 | ||
4955 | static void free_format_fields(struct format_field *field) | |
4956 | { | |
4957 | struct format_field *next; | |
4958 | ||
4959 | while (field) { | |
4960 | next = field->next; | |
4961 | free(field->type); | |
4962 | free(field->name); | |
4963 | free(field); | |
4964 | field = next; | |
4965 | } | |
4966 | } | |
4967 | ||
4968 | static void free_formats(struct format *format) | |
4969 | { | |
4970 | free_format_fields(format->common_fields); | |
4971 | free_format_fields(format->fields); | |
4972 | } | |
4973 | ||
4974 | static void free_event(struct event_format *event) | |
4975 | { | |
4976 | free(event->name); | |
4977 | free(event->system); | |
4978 | ||
4979 | free_formats(&event->format); | |
4980 | ||
4981 | free(event->print_fmt.format); | |
4982 | free_args(event->print_fmt.args); | |
4983 | ||
4984 | free(event); | |
4985 | } | |
4986 | ||
4987 | /** | |
4988 | * pevent_free - free a pevent handle | |
4989 | * @pevent: the pevent handle to free | |
4990 | */ | |
4991 | void pevent_free(struct pevent *pevent) | |
4992 | { | |
a2525a08 SR |
4993 | struct cmdline_list *cmdlist, *cmdnext; |
4994 | struct func_list *funclist, *funcnext; | |
4995 | struct printk_list *printklist, *printknext; | |
f7d82350 SR |
4996 | struct pevent_function_handler *func_handler; |
4997 | struct event_handler *handle; | |
4998 | int i; | |
4999 | ||
a2525a08 SR |
5000 | if (!pevent) |
5001 | return; | |
5002 | ||
5003 | cmdlist = pevent->cmdlist; | |
5004 | funclist = pevent->funclist; | |
5005 | printklist = pevent->printklist; | |
5006 | ||
f7d82350 SR |
5007 | pevent->ref_count--; |
5008 | if (pevent->ref_count) | |
5009 | return; | |
5010 | ||
5011 | if (pevent->cmdlines) { | |
5012 | for (i = 0; i < pevent->cmdline_count; i++) | |
5013 | free(pevent->cmdlines[i].comm); | |
5014 | free(pevent->cmdlines); | |
5015 | } | |
5016 | ||
5017 | while (cmdlist) { | |
5018 | cmdnext = cmdlist->next; | |
5019 | free(cmdlist->comm); | |
5020 | free(cmdlist); | |
5021 | cmdlist = cmdnext; | |
5022 | } | |
5023 | ||
5024 | if (pevent->func_map) { | |
5025 | for (i = 0; i < pevent->func_count; i++) { | |
5026 | free(pevent->func_map[i].func); | |
5027 | free(pevent->func_map[i].mod); | |
5028 | } | |
5029 | free(pevent->func_map); | |
5030 | } | |
5031 | ||
5032 | while (funclist) { | |
5033 | funcnext = funclist->next; | |
5034 | free(funclist->func); | |
5035 | free(funclist->mod); | |
5036 | free(funclist); | |
5037 | funclist = funcnext; | |
5038 | } | |
5039 | ||
5040 | while (pevent->func_handlers) { | |
5041 | func_handler = pevent->func_handlers; | |
5042 | pevent->func_handlers = func_handler->next; | |
5043 | free_func_handle(func_handler); | |
5044 | } | |
5045 | ||
5046 | if (pevent->printk_map) { | |
5047 | for (i = 0; i < pevent->printk_count; i++) | |
5048 | free(pevent->printk_map[i].printk); | |
5049 | free(pevent->printk_map); | |
5050 | } | |
5051 | ||
5052 | while (printklist) { | |
5053 | printknext = printklist->next; | |
5054 | free(printklist->printk); | |
5055 | free(printklist); | |
5056 | printklist = printknext; | |
5057 | } | |
5058 | ||
5059 | for (i = 0; i < pevent->nr_events; i++) | |
5060 | free_event(pevent->events[i]); | |
5061 | ||
5062 | while (pevent->handlers) { | |
5063 | handle = pevent->handlers; | |
5064 | pevent->handlers = handle->next; | |
5065 | free_handler(handle); | |
5066 | } | |
5067 | ||
5068 | free(pevent->events); | |
5069 | free(pevent->sort_events); | |
5070 | ||
5071 | free(pevent); | |
5072 | } | |
5073 | ||
5074 | void pevent_unref(struct pevent *pevent) | |
5075 | { | |
5076 | pevent_free(pevent); | |
5077 | } |