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