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