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