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