Commit | Line | Data |
---|---|---|
f7d82350 SR |
1 | /* |
2 | * Copyright (C) 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 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <stdarg.h> | |
24 | #include <errno.h> | |
25 | #include <sys/types.h> | |
26 | ||
27 | #include "event-parse.h" | |
28 | #include "event-utils.h" | |
29 | ||
30 | #define COMM "COMM" | |
31 | ||
32 | static struct format_field comm = { | |
33 | .name = "COMM", | |
34 | }; | |
35 | ||
36 | struct event_list { | |
37 | struct event_list *next; | |
38 | struct event_format *event; | |
39 | }; | |
40 | ||
41 | #define MAX_ERR_STR_SIZE 256 | |
42 | ||
43 | static void show_error(char **error_str, const char *fmt, ...) | |
44 | { | |
45 | unsigned long long index; | |
46 | const char *input; | |
47 | char *error; | |
48 | va_list ap; | |
49 | int len; | |
50 | int i; | |
51 | ||
52 | if (!error_str) | |
53 | return; | |
54 | ||
55 | input = pevent_get_input_buf(); | |
56 | index = pevent_get_input_buf_ptr(); | |
57 | len = input ? strlen(input) : 0; | |
58 | ||
59 | error = malloc_or_die(MAX_ERR_STR_SIZE + (len*2) + 3); | |
60 | ||
61 | if (len) { | |
62 | strcpy(error, input); | |
63 | error[len] = '\n'; | |
64 | for (i = 1; i < len && i < index; i++) | |
65 | error[len+i] = ' '; | |
66 | error[len + i] = '^'; | |
67 | error[len + i + 1] = '\n'; | |
68 | len += i+2; | |
69 | } | |
70 | ||
71 | va_start(ap, fmt); | |
72 | vsnprintf(error + len, MAX_ERR_STR_SIZE, fmt, ap); | |
73 | va_end(ap); | |
74 | ||
75 | *error_str = error; | |
76 | } | |
77 | ||
78 | static void free_token(char *token) | |
79 | { | |
80 | pevent_free_token(token); | |
81 | } | |
82 | ||
83 | static enum event_type read_token(char **tok) | |
84 | { | |
85 | enum event_type type; | |
86 | char *token = NULL; | |
87 | ||
88 | do { | |
89 | free_token(token); | |
90 | type = pevent_read_token(&token); | |
91 | } while (type == EVENT_NEWLINE || type == EVENT_SPACE); | |
92 | ||
93 | /* If token is = or ! check to see if the next char is ~ */ | |
94 | if (token && | |
95 | (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) && | |
96 | pevent_peek_char() == '~') { | |
97 | /* append it */ | |
e54b34ae | 98 | *tok = malloc_or_die(3); |
f7d82350 SR |
99 | sprintf(*tok, "%c%c", *token, '~'); |
100 | free_token(token); | |
101 | /* Now remove the '~' from the buffer */ | |
102 | pevent_read_token(&token); | |
103 | free_token(token); | |
104 | } else | |
105 | *tok = token; | |
106 | ||
107 | return type; | |
108 | } | |
109 | ||
110 | static int filter_cmp(const void *a, const void *b) | |
111 | { | |
112 | const struct filter_type *ea = a; | |
113 | const struct filter_type *eb = b; | |
114 | ||
115 | if (ea->event_id < eb->event_id) | |
116 | return -1; | |
117 | ||
118 | if (ea->event_id > eb->event_id) | |
119 | return 1; | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | static struct filter_type * | |
125 | find_filter_type(struct event_filter *filter, int id) | |
126 | { | |
127 | struct filter_type *filter_type; | |
128 | struct filter_type key; | |
129 | ||
130 | key.event_id = id; | |
131 | ||
132 | filter_type = bsearch(&key, filter->event_filters, | |
133 | filter->filters, | |
134 | sizeof(*filter->event_filters), | |
135 | filter_cmp); | |
136 | ||
137 | return filter_type; | |
138 | } | |
139 | ||
140 | static struct filter_type * | |
141 | add_filter_type(struct event_filter *filter, int id) | |
142 | { | |
143 | struct filter_type *filter_type; | |
144 | int i; | |
145 | ||
146 | filter_type = find_filter_type(filter, id); | |
147 | if (filter_type) | |
148 | return filter_type; | |
149 | ||
f6ced60f NK |
150 | filter->event_filters = realloc(filter->event_filters, |
151 | sizeof(*filter->event_filters) * | |
152 | (filter->filters + 1)); | |
153 | if (!filter->event_filters) | |
154 | die("Could not allocate filter"); | |
f7d82350 SR |
155 | |
156 | for (i = 0; i < filter->filters; i++) { | |
157 | if (filter->event_filters[i].event_id > id) | |
158 | break; | |
159 | } | |
160 | ||
161 | if (i < filter->filters) | |
162 | memmove(&filter->event_filters[i+1], | |
163 | &filter->event_filters[i], | |
164 | sizeof(*filter->event_filters) * | |
165 | (filter->filters - i)); | |
166 | ||
167 | filter_type = &filter->event_filters[i]; | |
168 | filter_type->event_id = id; | |
169 | filter_type->event = pevent_find_event(filter->pevent, id); | |
170 | filter_type->filter = NULL; | |
171 | ||
172 | filter->filters++; | |
173 | ||
174 | return filter_type; | |
175 | } | |
176 | ||
177 | /** | |
178 | * pevent_filter_alloc - create a new event filter | |
179 | * @pevent: The pevent that this filter is associated with | |
180 | */ | |
181 | struct event_filter *pevent_filter_alloc(struct pevent *pevent) | |
182 | { | |
183 | struct event_filter *filter; | |
184 | ||
4f244163 NK |
185 | filter = malloc(sizeof(*filter)); |
186 | if (filter == NULL) | |
187 | return NULL; | |
188 | ||
f7d82350 SR |
189 | memset(filter, 0, sizeof(*filter)); |
190 | filter->pevent = pevent; | |
191 | pevent_ref(pevent); | |
192 | ||
193 | return filter; | |
194 | } | |
195 | ||
196 | static struct filter_arg *allocate_arg(void) | |
197 | { | |
198 | struct filter_arg *arg; | |
199 | ||
200 | arg = malloc_or_die(sizeof(*arg)); | |
201 | memset(arg, 0, sizeof(*arg)); | |
202 | ||
203 | return arg; | |
204 | } | |
205 | ||
206 | static void free_arg(struct filter_arg *arg) | |
207 | { | |
208 | if (!arg) | |
209 | return; | |
210 | ||
211 | switch (arg->type) { | |
212 | case FILTER_ARG_NONE: | |
213 | case FILTER_ARG_BOOLEAN: | |
743df75f SR |
214 | break; |
215 | ||
f7d82350 | 216 | case FILTER_ARG_NUM: |
743df75f SR |
217 | free_arg(arg->num.left); |
218 | free_arg(arg->num.right); | |
219 | break; | |
220 | ||
221 | case FILTER_ARG_EXP: | |
222 | free_arg(arg->exp.left); | |
223 | free_arg(arg->exp.right); | |
f7d82350 SR |
224 | break; |
225 | ||
226 | case FILTER_ARG_STR: | |
227 | free(arg->str.val); | |
228 | regfree(&arg->str.reg); | |
229 | free(arg->str.buffer); | |
230 | break; | |
231 | ||
743df75f SR |
232 | case FILTER_ARG_VALUE: |
233 | if (arg->value.type == FILTER_STRING || | |
234 | arg->value.type == FILTER_CHAR) | |
235 | free(arg->value.str); | |
236 | break; | |
237 | ||
f7d82350 SR |
238 | case FILTER_ARG_OP: |
239 | free_arg(arg->op.left); | |
240 | free_arg(arg->op.right); | |
241 | default: | |
242 | break; | |
243 | } | |
244 | ||
245 | free(arg); | |
246 | } | |
247 | ||
234520d3 | 248 | static int add_event(struct event_list **events, |
f7d82350 SR |
249 | struct event_format *event) |
250 | { | |
251 | struct event_list *list; | |
252 | ||
234520d3 NK |
253 | list = malloc(sizeof(*list)); |
254 | if (list == NULL) | |
255 | return -1; | |
256 | ||
f7d82350 SR |
257 | list->next = *events; |
258 | *events = list; | |
259 | list->event = event; | |
234520d3 | 260 | return 0; |
f7d82350 SR |
261 | } |
262 | ||
263 | static int event_match(struct event_format *event, | |
264 | regex_t *sreg, regex_t *ereg) | |
265 | { | |
266 | if (sreg) { | |
267 | return !regexec(sreg, event->system, 0, NULL, 0) && | |
268 | !regexec(ereg, event->name, 0, NULL, 0); | |
269 | } | |
270 | ||
271 | return !regexec(ereg, event->system, 0, NULL, 0) || | |
272 | !regexec(ereg, event->name, 0, NULL, 0); | |
273 | } | |
274 | ||
275 | static int | |
276 | find_event(struct pevent *pevent, struct event_list **events, | |
277 | char *sys_name, char *event_name) | |
278 | { | |
279 | struct event_format *event; | |
280 | regex_t ereg; | |
281 | regex_t sreg; | |
282 | int match = 0; | |
234520d3 | 283 | int fail = 0; |
f7d82350 SR |
284 | char *reg; |
285 | int ret; | |
286 | int i; | |
287 | ||
288 | if (!event_name) { | |
289 | /* if no name is given, then swap sys and name */ | |
290 | event_name = sys_name; | |
291 | sys_name = NULL; | |
292 | } | |
293 | ||
294 | reg = malloc_or_die(strlen(event_name) + 3); | |
295 | sprintf(reg, "^%s$", event_name); | |
296 | ||
297 | ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB); | |
298 | free(reg); | |
299 | ||
300 | if (ret) | |
301 | return -1; | |
302 | ||
303 | if (sys_name) { | |
304 | reg = malloc_or_die(strlen(sys_name) + 3); | |
305 | sprintf(reg, "^%s$", sys_name); | |
306 | ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB); | |
307 | free(reg); | |
308 | if (ret) { | |
309 | regfree(&ereg); | |
310 | return -1; | |
311 | } | |
312 | } | |
313 | ||
314 | for (i = 0; i < pevent->nr_events; i++) { | |
315 | event = pevent->events[i]; | |
316 | if (event_match(event, sys_name ? &sreg : NULL, &ereg)) { | |
317 | match = 1; | |
234520d3 NK |
318 | if (add_event(events, event) < 0) { |
319 | fail = 1; | |
320 | break; | |
321 | } | |
f7d82350 SR |
322 | } |
323 | } | |
324 | ||
325 | regfree(&ereg); | |
326 | if (sys_name) | |
327 | regfree(&sreg); | |
328 | ||
329 | if (!match) | |
330 | return -1; | |
234520d3 NK |
331 | if (fail) |
332 | return -2; | |
f7d82350 SR |
333 | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static void free_events(struct event_list *events) | |
338 | { | |
339 | struct event_list *event; | |
340 | ||
341 | while (events) { | |
342 | event = events; | |
343 | events = events->next; | |
344 | free(event); | |
345 | } | |
346 | } | |
347 | ||
348 | static struct filter_arg * | |
eaec12d7 NK |
349 | create_arg_item(struct event_format *event, const char *token, |
350 | enum event_type type, char **error_str) | |
f7d82350 SR |
351 | { |
352 | struct format_field *field; | |
353 | struct filter_arg *arg; | |
354 | ||
355 | arg = allocate_arg(); | |
356 | ||
357 | switch (type) { | |
358 | ||
359 | case EVENT_SQUOTE: | |
360 | case EVENT_DQUOTE: | |
361 | arg->type = FILTER_ARG_VALUE; | |
362 | arg->value.type = | |
363 | type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR; | |
364 | arg->value.str = strdup(token); | |
2036fcd1 NK |
365 | if (!arg->value.str) { |
366 | free_arg(arg); | |
367 | show_error(error_str, "failed to allocate string filter arg"); | |
368 | return NULL; | |
369 | } | |
f7d82350 SR |
370 | break; |
371 | case EVENT_ITEM: | |
372 | /* if it is a number, then convert it */ | |
373 | if (isdigit(token[0])) { | |
374 | arg->type = FILTER_ARG_VALUE; | |
375 | arg->value.type = FILTER_NUMBER; | |
376 | arg->value.val = strtoull(token, NULL, 0); | |
377 | break; | |
378 | } | |
379 | /* Consider this a field */ | |
380 | field = pevent_find_any_field(event, token); | |
381 | if (!field) { | |
382 | if (strcmp(token, COMM) != 0) { | |
383 | /* not a field, Make it false */ | |
384 | arg->type = FILTER_ARG_BOOLEAN; | |
668fe01f | 385 | arg->boolean.value = FILTER_FALSE; |
f7d82350 SR |
386 | break; |
387 | } | |
388 | /* If token is 'COMM' then it is special */ | |
389 | field = &comm; | |
390 | } | |
391 | arg->type = FILTER_ARG_FIELD; | |
392 | arg->field.field = field; | |
393 | break; | |
394 | default: | |
395 | free_arg(arg); | |
396 | show_error(error_str, "expected a value but found %s", | |
397 | token); | |
398 | return NULL; | |
399 | } | |
400 | return arg; | |
401 | } | |
402 | ||
403 | static struct filter_arg * | |
404 | create_arg_op(enum filter_op_type btype) | |
405 | { | |
406 | struct filter_arg *arg; | |
407 | ||
408 | arg = allocate_arg(); | |
409 | arg->type = FILTER_ARG_OP; | |
410 | arg->op.type = btype; | |
411 | ||
412 | return arg; | |
413 | } | |
414 | ||
415 | static struct filter_arg * | |
416 | create_arg_exp(enum filter_exp_type etype) | |
417 | { | |
418 | struct filter_arg *arg; | |
419 | ||
420 | arg = allocate_arg(); | |
421 | arg->type = FILTER_ARG_EXP; | |
422 | arg->op.type = etype; | |
423 | ||
424 | return arg; | |
425 | } | |
426 | ||
427 | static struct filter_arg * | |
428 | create_arg_cmp(enum filter_exp_type etype) | |
429 | { | |
430 | struct filter_arg *arg; | |
431 | ||
432 | arg = allocate_arg(); | |
433 | /* Use NUM and change if necessary */ | |
434 | arg->type = FILTER_ARG_NUM; | |
435 | arg->op.type = etype; | |
436 | ||
437 | return arg; | |
438 | } | |
439 | ||
440 | static int add_right(struct filter_arg *op, struct filter_arg *arg, | |
441 | char **error_str) | |
442 | { | |
443 | struct filter_arg *left; | |
444 | char *str; | |
445 | int op_type; | |
446 | int ret; | |
447 | ||
448 | switch (op->type) { | |
449 | case FILTER_ARG_EXP: | |
450 | if (op->exp.right) | |
451 | goto out_fail; | |
452 | op->exp.right = arg; | |
453 | break; | |
454 | ||
455 | case FILTER_ARG_OP: | |
456 | if (op->op.right) | |
457 | goto out_fail; | |
458 | op->op.right = arg; | |
459 | break; | |
460 | ||
461 | case FILTER_ARG_NUM: | |
462 | if (op->op.right) | |
463 | goto out_fail; | |
464 | /* | |
465 | * The arg must be num, str, or field | |
466 | */ | |
467 | switch (arg->type) { | |
468 | case FILTER_ARG_VALUE: | |
469 | case FILTER_ARG_FIELD: | |
470 | break; | |
471 | default: | |
472 | show_error(error_str, | |
473 | "Illegal rvalue"); | |
474 | return -1; | |
475 | } | |
476 | ||
477 | /* | |
478 | * Depending on the type, we may need to | |
479 | * convert this to a string or regex. | |
480 | */ | |
481 | switch (arg->value.type) { | |
482 | case FILTER_CHAR: | |
483 | /* | |
484 | * A char should be converted to number if | |
485 | * the string is 1 byte, and the compare | |
486 | * is not a REGEX. | |
487 | */ | |
488 | if (strlen(arg->value.str) == 1 && | |
489 | op->num.type != FILTER_CMP_REGEX && | |
490 | op->num.type != FILTER_CMP_NOT_REGEX) { | |
491 | arg->value.type = FILTER_NUMBER; | |
492 | goto do_int; | |
493 | } | |
494 | /* fall through */ | |
495 | case FILTER_STRING: | |
496 | ||
497 | /* convert op to a string arg */ | |
498 | op_type = op->num.type; | |
499 | left = op->num.left; | |
500 | str = arg->value.str; | |
501 | ||
502 | /* reset the op for the new field */ | |
503 | memset(op, 0, sizeof(*op)); | |
504 | ||
505 | /* | |
506 | * If left arg was a field not found then | |
507 | * NULL the entire op. | |
508 | */ | |
509 | if (left->type == FILTER_ARG_BOOLEAN) { | |
510 | free_arg(left); | |
511 | free_arg(arg); | |
512 | op->type = FILTER_ARG_BOOLEAN; | |
668fe01f | 513 | op->boolean.value = FILTER_FALSE; |
f7d82350 SR |
514 | break; |
515 | } | |
516 | ||
517 | /* Left arg must be a field */ | |
518 | if (left->type != FILTER_ARG_FIELD) { | |
519 | show_error(error_str, | |
520 | "Illegal lvalue for string comparison"); | |
521 | return -1; | |
522 | } | |
523 | ||
524 | /* Make sure this is a valid string compare */ | |
525 | switch (op_type) { | |
526 | case FILTER_CMP_EQ: | |
527 | op_type = FILTER_CMP_MATCH; | |
528 | break; | |
529 | case FILTER_CMP_NE: | |
530 | op_type = FILTER_CMP_NOT_MATCH; | |
531 | break; | |
532 | ||
533 | case FILTER_CMP_REGEX: | |
534 | case FILTER_CMP_NOT_REGEX: | |
535 | ret = regcomp(&op->str.reg, str, REG_ICASE|REG_NOSUB); | |
536 | if (ret) { | |
537 | show_error(error_str, | |
538 | "RegEx '%s' did not compute", | |
539 | str); | |
540 | return -1; | |
541 | } | |
542 | break; | |
543 | default: | |
544 | show_error(error_str, | |
545 | "Illegal comparison for string"); | |
546 | return -1; | |
547 | } | |
548 | ||
549 | op->type = FILTER_ARG_STR; | |
550 | op->str.type = op_type; | |
551 | op->str.field = left->field.field; | |
552 | op->str.val = strdup(str); | |
553 | if (!op->str.val) | |
554 | die("malloc string"); | |
555 | /* | |
556 | * Need a buffer to copy data for tests | |
557 | */ | |
558 | op->str.buffer = malloc_or_die(op->str.field->size + 1); | |
559 | /* Null terminate this buffer */ | |
560 | op->str.buffer[op->str.field->size] = 0; | |
561 | ||
562 | /* We no longer have left or right args */ | |
563 | free_arg(arg); | |
564 | free_arg(left); | |
565 | ||
566 | break; | |
567 | ||
568 | case FILTER_NUMBER: | |
569 | ||
570 | do_int: | |
571 | switch (op->num.type) { | |
572 | case FILTER_CMP_REGEX: | |
573 | case FILTER_CMP_NOT_REGEX: | |
574 | show_error(error_str, | |
575 | "Op not allowed with integers"); | |
576 | return -1; | |
577 | ||
578 | default: | |
579 | break; | |
580 | } | |
581 | ||
582 | /* numeric compare */ | |
583 | op->num.right = arg; | |
584 | break; | |
585 | default: | |
586 | goto out_fail; | |
587 | } | |
588 | break; | |
589 | default: | |
590 | goto out_fail; | |
591 | } | |
592 | ||
593 | return 0; | |
594 | ||
595 | out_fail: | |
596 | show_error(error_str, | |
597 | "Syntax error"); | |
598 | return -1; | |
599 | } | |
600 | ||
601 | static struct filter_arg * | |
602 | rotate_op_right(struct filter_arg *a, struct filter_arg *b) | |
603 | { | |
604 | struct filter_arg *arg; | |
605 | ||
606 | arg = a->op.right; | |
607 | a->op.right = b; | |
608 | return arg; | |
609 | } | |
610 | ||
611 | static int add_left(struct filter_arg *op, struct filter_arg *arg) | |
612 | { | |
613 | switch (op->type) { | |
614 | case FILTER_ARG_EXP: | |
615 | if (arg->type == FILTER_ARG_OP) | |
616 | arg = rotate_op_right(arg, op); | |
617 | op->exp.left = arg; | |
618 | break; | |
619 | ||
620 | case FILTER_ARG_OP: | |
621 | op->op.left = arg; | |
622 | break; | |
623 | case FILTER_ARG_NUM: | |
624 | if (arg->type == FILTER_ARG_OP) | |
625 | arg = rotate_op_right(arg, op); | |
626 | ||
627 | /* left arg of compares must be a field */ | |
628 | if (arg->type != FILTER_ARG_FIELD && | |
629 | arg->type != FILTER_ARG_BOOLEAN) | |
630 | return -1; | |
631 | op->num.left = arg; | |
632 | break; | |
633 | default: | |
634 | return -1; | |
635 | } | |
636 | return 0; | |
637 | } | |
638 | ||
639 | enum op_type { | |
640 | OP_NONE, | |
641 | OP_BOOL, | |
642 | OP_NOT, | |
643 | OP_EXP, | |
644 | OP_CMP, | |
645 | }; | |
646 | ||
647 | static enum op_type process_op(const char *token, | |
648 | enum filter_op_type *btype, | |
649 | enum filter_cmp_type *ctype, | |
650 | enum filter_exp_type *etype) | |
651 | { | |
652 | *btype = FILTER_OP_NOT; | |
653 | *etype = FILTER_EXP_NONE; | |
654 | *ctype = FILTER_CMP_NONE; | |
655 | ||
656 | if (strcmp(token, "&&") == 0) | |
657 | *btype = FILTER_OP_AND; | |
658 | else if (strcmp(token, "||") == 0) | |
659 | *btype = FILTER_OP_OR; | |
660 | else if (strcmp(token, "!") == 0) | |
661 | return OP_NOT; | |
662 | ||
663 | if (*btype != FILTER_OP_NOT) | |
664 | return OP_BOOL; | |
665 | ||
666 | /* Check for value expressions */ | |
667 | if (strcmp(token, "+") == 0) { | |
668 | *etype = FILTER_EXP_ADD; | |
669 | } else if (strcmp(token, "-") == 0) { | |
670 | *etype = FILTER_EXP_SUB; | |
671 | } else if (strcmp(token, "*") == 0) { | |
672 | *etype = FILTER_EXP_MUL; | |
673 | } else if (strcmp(token, "/") == 0) { | |
674 | *etype = FILTER_EXP_DIV; | |
675 | } else if (strcmp(token, "%") == 0) { | |
676 | *etype = FILTER_EXP_MOD; | |
677 | } else if (strcmp(token, ">>") == 0) { | |
678 | *etype = FILTER_EXP_RSHIFT; | |
679 | } else if (strcmp(token, "<<") == 0) { | |
680 | *etype = FILTER_EXP_LSHIFT; | |
681 | } else if (strcmp(token, "&") == 0) { | |
682 | *etype = FILTER_EXP_AND; | |
683 | } else if (strcmp(token, "|") == 0) { | |
684 | *etype = FILTER_EXP_OR; | |
685 | } else if (strcmp(token, "^") == 0) { | |
686 | *etype = FILTER_EXP_XOR; | |
687 | } else if (strcmp(token, "~") == 0) | |
688 | *etype = FILTER_EXP_NOT; | |
689 | ||
690 | if (*etype != FILTER_EXP_NONE) | |
691 | return OP_EXP; | |
692 | ||
693 | /* Check for compares */ | |
694 | if (strcmp(token, "==") == 0) | |
695 | *ctype = FILTER_CMP_EQ; | |
696 | else if (strcmp(token, "!=") == 0) | |
697 | *ctype = FILTER_CMP_NE; | |
698 | else if (strcmp(token, "<") == 0) | |
699 | *ctype = FILTER_CMP_LT; | |
700 | else if (strcmp(token, ">") == 0) | |
701 | *ctype = FILTER_CMP_GT; | |
702 | else if (strcmp(token, "<=") == 0) | |
703 | *ctype = FILTER_CMP_LE; | |
704 | else if (strcmp(token, ">=") == 0) | |
705 | *ctype = FILTER_CMP_GE; | |
706 | else if (strcmp(token, "=~") == 0) | |
707 | *ctype = FILTER_CMP_REGEX; | |
708 | else if (strcmp(token, "!~") == 0) | |
709 | *ctype = FILTER_CMP_NOT_REGEX; | |
710 | else | |
711 | return OP_NONE; | |
712 | ||
713 | return OP_CMP; | |
714 | } | |
715 | ||
716 | static int check_op_done(struct filter_arg *arg) | |
717 | { | |
718 | switch (arg->type) { | |
719 | case FILTER_ARG_EXP: | |
720 | return arg->exp.right != NULL; | |
721 | ||
722 | case FILTER_ARG_OP: | |
723 | return arg->op.right != NULL; | |
724 | ||
725 | case FILTER_ARG_NUM: | |
726 | return arg->num.right != NULL; | |
727 | ||
728 | case FILTER_ARG_STR: | |
729 | /* A string conversion is always done */ | |
730 | return 1; | |
731 | ||
732 | case FILTER_ARG_BOOLEAN: | |
733 | /* field not found, is ok */ | |
734 | return 1; | |
735 | ||
736 | default: | |
737 | return 0; | |
738 | } | |
739 | } | |
740 | ||
741 | enum filter_vals { | |
742 | FILTER_VAL_NORM, | |
743 | FILTER_VAL_FALSE, | |
744 | FILTER_VAL_TRUE, | |
745 | }; | |
746 | ||
747 | void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child, | |
748 | struct filter_arg *arg) | |
749 | { | |
750 | struct filter_arg *other_child; | |
751 | struct filter_arg **ptr; | |
752 | ||
753 | if (parent->type != FILTER_ARG_OP && | |
754 | arg->type != FILTER_ARG_OP) | |
755 | die("can not reparent other than OP"); | |
756 | ||
757 | /* Get the sibling */ | |
758 | if (old_child->op.right == arg) { | |
759 | ptr = &old_child->op.right; | |
760 | other_child = old_child->op.left; | |
761 | } else if (old_child->op.left == arg) { | |
762 | ptr = &old_child->op.left; | |
763 | other_child = old_child->op.right; | |
764 | } else | |
765 | die("Error in reparent op, find other child"); | |
766 | ||
767 | /* Detach arg from old_child */ | |
768 | *ptr = NULL; | |
769 | ||
770 | /* Check for root */ | |
771 | if (parent == old_child) { | |
772 | free_arg(other_child); | |
773 | *parent = *arg; | |
774 | /* Free arg without recussion */ | |
775 | free(arg); | |
776 | return; | |
777 | } | |
778 | ||
779 | if (parent->op.right == old_child) | |
780 | ptr = &parent->op.right; | |
781 | else if (parent->op.left == old_child) | |
782 | ptr = &parent->op.left; | |
783 | else | |
784 | die("Error in reparent op"); | |
785 | *ptr = arg; | |
786 | ||
787 | free_arg(old_child); | |
788 | } | |
789 | ||
790 | enum filter_vals test_arg(struct filter_arg *parent, struct filter_arg *arg) | |
791 | { | |
792 | enum filter_vals lval, rval; | |
793 | ||
794 | switch (arg->type) { | |
795 | ||
796 | /* bad case */ | |
797 | case FILTER_ARG_BOOLEAN: | |
668fe01f | 798 | return FILTER_VAL_FALSE + arg->boolean.value; |
f7d82350 SR |
799 | |
800 | /* good cases: */ | |
801 | case FILTER_ARG_STR: | |
802 | case FILTER_ARG_VALUE: | |
803 | case FILTER_ARG_FIELD: | |
804 | return FILTER_VAL_NORM; | |
805 | ||
806 | case FILTER_ARG_EXP: | |
807 | lval = test_arg(arg, arg->exp.left); | |
808 | if (lval != FILTER_VAL_NORM) | |
809 | return lval; | |
810 | rval = test_arg(arg, arg->exp.right); | |
811 | if (rval != FILTER_VAL_NORM) | |
812 | return rval; | |
813 | return FILTER_VAL_NORM; | |
814 | ||
815 | case FILTER_ARG_NUM: | |
816 | lval = test_arg(arg, arg->num.left); | |
817 | if (lval != FILTER_VAL_NORM) | |
818 | return lval; | |
819 | rval = test_arg(arg, arg->num.right); | |
820 | if (rval != FILTER_VAL_NORM) | |
821 | return rval; | |
822 | return FILTER_VAL_NORM; | |
823 | ||
824 | case FILTER_ARG_OP: | |
825 | if (arg->op.type != FILTER_OP_NOT) { | |
826 | lval = test_arg(arg, arg->op.left); | |
827 | switch (lval) { | |
828 | case FILTER_VAL_NORM: | |
829 | break; | |
830 | case FILTER_VAL_TRUE: | |
831 | if (arg->op.type == FILTER_OP_OR) | |
832 | return FILTER_VAL_TRUE; | |
833 | rval = test_arg(arg, arg->op.right); | |
834 | if (rval != FILTER_VAL_NORM) | |
835 | return rval; | |
836 | ||
837 | reparent_op_arg(parent, arg, arg->op.right); | |
838 | return FILTER_VAL_NORM; | |
839 | ||
840 | case FILTER_VAL_FALSE: | |
841 | if (arg->op.type == FILTER_OP_AND) | |
842 | return FILTER_VAL_FALSE; | |
843 | rval = test_arg(arg, arg->op.right); | |
844 | if (rval != FILTER_VAL_NORM) | |
845 | return rval; | |
846 | ||
847 | reparent_op_arg(parent, arg, arg->op.right); | |
848 | return FILTER_VAL_NORM; | |
849 | } | |
850 | } | |
851 | ||
852 | rval = test_arg(arg, arg->op.right); | |
853 | switch (rval) { | |
854 | case FILTER_VAL_NORM: | |
855 | break; | |
856 | case FILTER_VAL_TRUE: | |
857 | if (arg->op.type == FILTER_OP_OR) | |
858 | return FILTER_VAL_TRUE; | |
859 | if (arg->op.type == FILTER_OP_NOT) | |
860 | return FILTER_VAL_FALSE; | |
861 | ||
862 | reparent_op_arg(parent, arg, arg->op.left); | |
863 | return FILTER_VAL_NORM; | |
864 | ||
865 | case FILTER_VAL_FALSE: | |
866 | if (arg->op.type == FILTER_OP_AND) | |
867 | return FILTER_VAL_FALSE; | |
868 | if (arg->op.type == FILTER_OP_NOT) | |
869 | return FILTER_VAL_TRUE; | |
870 | ||
871 | reparent_op_arg(parent, arg, arg->op.left); | |
872 | return FILTER_VAL_NORM; | |
873 | } | |
874 | ||
875 | return FILTER_VAL_NORM; | |
876 | default: | |
877 | die("bad arg in filter tree"); | |
878 | } | |
879 | return FILTER_VAL_NORM; | |
880 | } | |
881 | ||
882 | /* Remove any unknown event fields */ | |
883 | static struct filter_arg *collapse_tree(struct filter_arg *arg) | |
884 | { | |
885 | enum filter_vals ret; | |
886 | ||
887 | ret = test_arg(arg, arg); | |
888 | switch (ret) { | |
889 | case FILTER_VAL_NORM: | |
890 | return arg; | |
891 | ||
892 | case FILTER_VAL_TRUE: | |
893 | case FILTER_VAL_FALSE: | |
894 | free_arg(arg); | |
895 | arg = allocate_arg(); | |
896 | arg->type = FILTER_ARG_BOOLEAN; | |
668fe01f | 897 | arg->boolean.value = ret == FILTER_VAL_TRUE; |
f7d82350 SR |
898 | } |
899 | ||
900 | return arg; | |
901 | } | |
902 | ||
903 | static int | |
904 | process_filter(struct event_format *event, struct filter_arg **parg, | |
905 | char **error_str, int not) | |
906 | { | |
907 | enum event_type type; | |
908 | char *token = NULL; | |
909 | struct filter_arg *current_op = NULL; | |
910 | struct filter_arg *current_exp = NULL; | |
911 | struct filter_arg *left_item = NULL; | |
912 | struct filter_arg *arg = NULL; | |
913 | enum op_type op_type; | |
914 | enum filter_op_type btype; | |
915 | enum filter_exp_type etype; | |
916 | enum filter_cmp_type ctype; | |
917 | int ret; | |
918 | ||
919 | *parg = NULL; | |
920 | ||
921 | do { | |
922 | free(token); | |
923 | type = read_token(&token); | |
924 | switch (type) { | |
925 | case EVENT_SQUOTE: | |
926 | case EVENT_DQUOTE: | |
927 | case EVENT_ITEM: | |
928 | arg = create_arg_item(event, token, type, error_str); | |
929 | if (!arg) | |
930 | goto fail; | |
931 | if (!left_item) | |
932 | left_item = arg; | |
933 | else if (current_exp) { | |
934 | ret = add_right(current_exp, arg, error_str); | |
935 | if (ret < 0) | |
936 | goto fail; | |
937 | left_item = NULL; | |
938 | /* Not's only one one expression */ | |
939 | if (not) { | |
940 | arg = NULL; | |
941 | if (current_op) | |
942 | goto fail_print; | |
943 | free(token); | |
944 | *parg = current_exp; | |
945 | return 0; | |
946 | } | |
947 | } else | |
948 | goto fail_print; | |
949 | arg = NULL; | |
950 | break; | |
951 | ||
952 | case EVENT_DELIM: | |
953 | if (*token == ',') { | |
954 | show_error(error_str, | |
955 | "Illegal token ','"); | |
956 | goto fail; | |
957 | } | |
958 | ||
959 | if (*token == '(') { | |
960 | if (left_item) { | |
961 | show_error(error_str, | |
962 | "Open paren can not come after item"); | |
963 | goto fail; | |
964 | } | |
965 | if (current_exp) { | |
966 | show_error(error_str, | |
967 | "Open paren can not come after expression"); | |
968 | goto fail; | |
969 | } | |
970 | ||
971 | ret = process_filter(event, &arg, error_str, 0); | |
972 | if (ret != 1) { | |
973 | if (ret == 0) | |
974 | show_error(error_str, | |
975 | "Unbalanced number of '('"); | |
976 | goto fail; | |
977 | } | |
978 | ret = 0; | |
979 | ||
980 | /* A not wants just one expression */ | |
981 | if (not) { | |
982 | if (current_op) | |
983 | goto fail_print; | |
984 | *parg = arg; | |
985 | return 0; | |
986 | } | |
987 | ||
988 | if (current_op) | |
989 | ret = add_right(current_op, arg, error_str); | |
990 | else | |
991 | current_exp = arg; | |
992 | ||
993 | if (ret < 0) | |
994 | goto fail; | |
995 | ||
996 | } else { /* ')' */ | |
997 | if (!current_op && !current_exp) | |
998 | goto fail_print; | |
999 | ||
1000 | /* Make sure everything is finished at this level */ | |
1001 | if (current_exp && !check_op_done(current_exp)) | |
1002 | goto fail_print; | |
1003 | if (current_op && !check_op_done(current_op)) | |
1004 | goto fail_print; | |
1005 | ||
1006 | if (current_op) | |
1007 | *parg = current_op; | |
1008 | else | |
1009 | *parg = current_exp; | |
1010 | return 1; | |
1011 | } | |
1012 | break; | |
1013 | ||
1014 | case EVENT_OP: | |
1015 | op_type = process_op(token, &btype, &ctype, &etype); | |
1016 | ||
1017 | /* All expect a left arg except for NOT */ | |
1018 | switch (op_type) { | |
1019 | case OP_BOOL: | |
1020 | /* Logic ops need a left expression */ | |
1021 | if (!current_exp && !current_op) | |
1022 | goto fail_print; | |
1023 | /* fall through */ | |
1024 | case OP_NOT: | |
1025 | /* logic only processes ops and exp */ | |
1026 | if (left_item) | |
1027 | goto fail_print; | |
1028 | break; | |
1029 | case OP_EXP: | |
1030 | case OP_CMP: | |
1031 | if (!left_item) | |
1032 | goto fail_print; | |
1033 | break; | |
1034 | case OP_NONE: | |
1035 | show_error(error_str, | |
1036 | "Unknown op token %s", token); | |
1037 | goto fail; | |
1038 | } | |
1039 | ||
1040 | ret = 0; | |
1041 | switch (op_type) { | |
1042 | case OP_BOOL: | |
1043 | arg = create_arg_op(btype); | |
1044 | if (current_op) | |
1045 | ret = add_left(arg, current_op); | |
1046 | else | |
1047 | ret = add_left(arg, current_exp); | |
1048 | current_op = arg; | |
1049 | current_exp = NULL; | |
1050 | break; | |
1051 | ||
1052 | case OP_NOT: | |
1053 | arg = create_arg_op(btype); | |
1054 | if (current_op) | |
1055 | ret = add_right(current_op, arg, error_str); | |
1056 | if (ret < 0) | |
1057 | goto fail; | |
1058 | current_exp = arg; | |
1059 | ret = process_filter(event, &arg, error_str, 1); | |
1060 | if (ret < 0) | |
1061 | goto fail; | |
1062 | ret = add_right(current_exp, arg, error_str); | |
1063 | if (ret < 0) | |
1064 | goto fail; | |
1065 | break; | |
1066 | ||
1067 | case OP_EXP: | |
1068 | case OP_CMP: | |
1069 | if (op_type == OP_EXP) | |
1070 | arg = create_arg_exp(etype); | |
1071 | else | |
1072 | arg = create_arg_cmp(ctype); | |
1073 | ||
1074 | if (current_op) | |
1075 | ret = add_right(current_op, arg, error_str); | |
1076 | if (ret < 0) | |
1077 | goto fail; | |
1078 | ret = add_left(arg, left_item); | |
1079 | if (ret < 0) { | |
1080 | arg = NULL; | |
1081 | goto fail_print; | |
1082 | } | |
1083 | current_exp = arg; | |
1084 | break; | |
1085 | default: | |
1086 | break; | |
1087 | } | |
1088 | arg = NULL; | |
1089 | if (ret < 0) | |
1090 | goto fail_print; | |
1091 | break; | |
1092 | case EVENT_NONE: | |
1093 | break; | |
1094 | default: | |
1095 | goto fail_print; | |
1096 | } | |
1097 | } while (type != EVENT_NONE); | |
1098 | ||
1099 | if (!current_op && !current_exp) | |
1100 | goto fail_print; | |
1101 | ||
1102 | if (!current_op) | |
1103 | current_op = current_exp; | |
1104 | ||
1105 | current_op = collapse_tree(current_op); | |
1106 | ||
1107 | *parg = current_op; | |
1108 | ||
1109 | return 0; | |
1110 | ||
1111 | fail_print: | |
1112 | show_error(error_str, "Syntax error"); | |
1113 | fail: | |
1114 | free_arg(current_op); | |
1115 | free_arg(current_exp); | |
1116 | free_arg(arg); | |
1117 | free(token); | |
1118 | return -1; | |
1119 | } | |
1120 | ||
1121 | static int | |
1122 | process_event(struct event_format *event, const char *filter_str, | |
1123 | struct filter_arg **parg, char **error_str) | |
1124 | { | |
1125 | int ret; | |
1126 | ||
1127 | pevent_buffer_init(filter_str, strlen(filter_str)); | |
1128 | ||
1129 | ret = process_filter(event, parg, error_str, 0); | |
1130 | if (ret == 1) { | |
1131 | show_error(error_str, | |
1132 | "Unbalanced number of ')'"); | |
1133 | return -1; | |
1134 | } | |
1135 | if (ret < 0) | |
1136 | return ret; | |
1137 | ||
1138 | /* If parg is NULL, then make it into FALSE */ | |
1139 | if (!*parg) { | |
1140 | *parg = allocate_arg(); | |
1141 | (*parg)->type = FILTER_ARG_BOOLEAN; | |
668fe01f | 1142 | (*parg)->boolean.value = FILTER_FALSE; |
f7d82350 SR |
1143 | } |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
1148 | static int filter_event(struct event_filter *filter, | |
1149 | struct event_format *event, | |
1150 | const char *filter_str, char **error_str) | |
1151 | { | |
1152 | struct filter_type *filter_type; | |
1153 | struct filter_arg *arg; | |
1154 | int ret; | |
1155 | ||
1156 | if (filter_str) { | |
1157 | ret = process_event(event, filter_str, &arg, error_str); | |
1158 | if (ret < 0) | |
1159 | return ret; | |
1160 | ||
1161 | } else { | |
1162 | /* just add a TRUE arg */ | |
1163 | arg = allocate_arg(); | |
1164 | arg->type = FILTER_ARG_BOOLEAN; | |
668fe01f | 1165 | arg->boolean.value = FILTER_TRUE; |
f7d82350 SR |
1166 | } |
1167 | ||
1168 | filter_type = add_filter_type(filter, event->id); | |
1169 | if (filter_type->filter) | |
1170 | free_arg(filter_type->filter); | |
1171 | filter_type->filter = arg; | |
1172 | ||
1173 | return 0; | |
1174 | } | |
1175 | ||
1176 | /** | |
1177 | * pevent_filter_add_filter_str - add a new filter | |
1178 | * @filter: the event filter to add to | |
1179 | * @filter_str: the filter string that contains the filter | |
1180 | * @error_str: string containing reason for failed filter | |
1181 | * | |
1182 | * Returns 0 if the filter was successfully added | |
1183 | * -1 if there was an error. | |
1184 | * | |
1185 | * On error, if @error_str points to a string pointer, | |
1186 | * it is set to the reason that the filter failed. | |
1187 | * This string must be freed with "free". | |
1188 | */ | |
1189 | int pevent_filter_add_filter_str(struct event_filter *filter, | |
1190 | const char *filter_str, | |
1191 | char **error_str) | |
1192 | { | |
1193 | struct pevent *pevent = filter->pevent; | |
1194 | struct event_list *event; | |
1195 | struct event_list *events = NULL; | |
1196 | const char *filter_start; | |
1197 | const char *next_event; | |
1198 | char *this_event; | |
1199 | char *event_name = NULL; | |
1200 | char *sys_name = NULL; | |
1201 | char *sp; | |
1202 | int rtn = 0; | |
1203 | int len; | |
1204 | int ret; | |
1205 | ||
1206 | /* clear buffer to reset show error */ | |
1207 | pevent_buffer_init("", 0); | |
1208 | ||
1209 | if (error_str) | |
1210 | *error_str = NULL; | |
1211 | ||
1212 | filter_start = strchr(filter_str, ':'); | |
1213 | if (filter_start) | |
1214 | len = filter_start - filter_str; | |
1215 | else | |
1216 | len = strlen(filter_str); | |
1217 | ||
1218 | ||
1219 | do { | |
1220 | next_event = strchr(filter_str, ','); | |
1221 | if (next_event && | |
1222 | (!filter_start || next_event < filter_start)) | |
1223 | len = next_event - filter_str; | |
1224 | else if (filter_start) | |
1225 | len = filter_start - filter_str; | |
1226 | else | |
1227 | len = strlen(filter_str); | |
1228 | ||
1229 | this_event = malloc_or_die(len + 1); | |
1230 | memcpy(this_event, filter_str, len); | |
1231 | this_event[len] = 0; | |
1232 | ||
1233 | if (next_event) | |
1234 | next_event++; | |
1235 | ||
1236 | filter_str = next_event; | |
1237 | ||
1238 | sys_name = strtok_r(this_event, "/", &sp); | |
1239 | event_name = strtok_r(NULL, "/", &sp); | |
1240 | ||
1241 | if (!sys_name) { | |
1242 | show_error(error_str, "No filter found"); | |
1243 | /* This can only happen when events is NULL, but still */ | |
1244 | free_events(events); | |
1245 | free(this_event); | |
1246 | return -1; | |
1247 | } | |
1248 | ||
1249 | /* Find this event */ | |
1250 | ret = find_event(pevent, &events, strim(sys_name), strim(event_name)); | |
1251 | if (ret < 0) { | |
1252 | if (event_name) | |
1253 | show_error(error_str, | |
1254 | "No event found under '%s.%s'", | |
1255 | sys_name, event_name); | |
1256 | else | |
1257 | show_error(error_str, | |
1258 | "No event found under '%s'", | |
1259 | sys_name); | |
1260 | free_events(events); | |
1261 | free(this_event); | |
1262 | return -1; | |
1263 | } | |
1264 | free(this_event); | |
1265 | } while (filter_str); | |
1266 | ||
1267 | /* Skip the ':' */ | |
1268 | if (filter_start) | |
1269 | filter_start++; | |
1270 | ||
1271 | /* filter starts here */ | |
1272 | for (event = events; event; event = event->next) { | |
1273 | ret = filter_event(filter, event->event, filter_start, | |
1274 | error_str); | |
1275 | /* Failures are returned if a parse error happened */ | |
1276 | if (ret < 0) | |
1277 | rtn = ret; | |
1278 | ||
1279 | if (ret >= 0 && pevent->test_filters) { | |
1280 | char *test; | |
1281 | test = pevent_filter_make_string(filter, event->event->id); | |
1282 | printf(" '%s: %s'\n", event->event->name, test); | |
1283 | free(test); | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | free_events(events); | |
1288 | ||
1289 | if (rtn >= 0 && pevent->test_filters) | |
1290 | exit(0); | |
1291 | ||
1292 | return rtn; | |
1293 | } | |
1294 | ||
1295 | static void free_filter_type(struct filter_type *filter_type) | |
1296 | { | |
1297 | free_arg(filter_type->filter); | |
1298 | } | |
1299 | ||
1300 | /** | |
1301 | * pevent_filter_remove_event - remove a filter for an event | |
1302 | * @filter: the event filter to remove from | |
1303 | * @event_id: the event to remove a filter for | |
1304 | * | |
1305 | * Removes the filter saved for an event defined by @event_id | |
1306 | * from the @filter. | |
1307 | * | |
1308 | * Returns 1: if an event was removed | |
1309 | * 0: if the event was not found | |
1310 | */ | |
1311 | int pevent_filter_remove_event(struct event_filter *filter, | |
1312 | int event_id) | |
1313 | { | |
1314 | struct filter_type *filter_type; | |
1315 | unsigned long len; | |
1316 | ||
1317 | if (!filter->filters) | |
1318 | return 0; | |
1319 | ||
1320 | filter_type = find_filter_type(filter, event_id); | |
1321 | ||
1322 | if (!filter_type) | |
1323 | return 0; | |
1324 | ||
1325 | free_filter_type(filter_type); | |
1326 | ||
1327 | /* The filter_type points into the event_filters array */ | |
1328 | len = (unsigned long)(filter->event_filters + filter->filters) - | |
1329 | (unsigned long)(filter_type + 1); | |
1330 | ||
1331 | memmove(filter_type, filter_type + 1, len); | |
1332 | filter->filters--; | |
1333 | ||
1334 | memset(&filter->event_filters[filter->filters], 0, | |
1335 | sizeof(*filter_type)); | |
1336 | ||
1337 | return 1; | |
1338 | } | |
1339 | ||
1340 | /** | |
1341 | * pevent_filter_reset - clear all filters in a filter | |
1342 | * @filter: the event filter to reset | |
1343 | * | |
1344 | * Removes all filters from a filter and resets it. | |
1345 | */ | |
1346 | void pevent_filter_reset(struct event_filter *filter) | |
1347 | { | |
1348 | int i; | |
1349 | ||
1350 | for (i = 0; i < filter->filters; i++) | |
1351 | free_filter_type(&filter->event_filters[i]); | |
1352 | ||
1353 | free(filter->event_filters); | |
1354 | filter->filters = 0; | |
1355 | filter->event_filters = NULL; | |
1356 | } | |
1357 | ||
1358 | void pevent_filter_free(struct event_filter *filter) | |
1359 | { | |
1360 | pevent_unref(filter->pevent); | |
1361 | ||
1362 | pevent_filter_reset(filter); | |
1363 | ||
1364 | free(filter); | |
1365 | } | |
1366 | ||
1367 | static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg); | |
1368 | ||
1369 | static int copy_filter_type(struct event_filter *filter, | |
1370 | struct event_filter *source, | |
1371 | struct filter_type *filter_type) | |
1372 | { | |
1373 | struct filter_arg *arg; | |
1374 | struct event_format *event; | |
1375 | const char *sys; | |
1376 | const char *name; | |
1377 | char *str; | |
1378 | ||
1379 | /* Can't assume that the pevent's are the same */ | |
1380 | sys = filter_type->event->system; | |
1381 | name = filter_type->event->name; | |
1382 | event = pevent_find_event_by_name(filter->pevent, sys, name); | |
1383 | if (!event) | |
1384 | return -1; | |
1385 | ||
1386 | str = arg_to_str(source, filter_type->filter); | |
1387 | if (!str) | |
1388 | return -1; | |
1389 | ||
1390 | if (strcmp(str, "TRUE") == 0 || strcmp(str, "FALSE") == 0) { | |
1391 | /* Add trivial event */ | |
1392 | arg = allocate_arg(); | |
1393 | arg->type = FILTER_ARG_BOOLEAN; | |
1394 | if (strcmp(str, "TRUE") == 0) | |
668fe01f | 1395 | arg->boolean.value = 1; |
f7d82350 | 1396 | else |
668fe01f | 1397 | arg->boolean.value = 0; |
f7d82350 SR |
1398 | |
1399 | filter_type = add_filter_type(filter, event->id); | |
1400 | filter_type->filter = arg; | |
1401 | ||
1402 | free(str); | |
1403 | return 0; | |
1404 | } | |
1405 | ||
1406 | filter_event(filter, event, str, NULL); | |
1407 | free(str); | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | /** | |
1413 | * pevent_filter_copy - copy a filter using another filter | |
1414 | * @dest - the filter to copy to | |
1415 | * @source - the filter to copy from | |
1416 | * | |
1417 | * Returns 0 on success and -1 if not all filters were copied | |
1418 | */ | |
1419 | int pevent_filter_copy(struct event_filter *dest, struct event_filter *source) | |
1420 | { | |
1421 | int ret = 0; | |
1422 | int i; | |
1423 | ||
1424 | pevent_filter_reset(dest); | |
1425 | ||
1426 | for (i = 0; i < source->filters; i++) { | |
1427 | if (copy_filter_type(dest, source, &source->event_filters[i])) | |
1428 | ret = -1; | |
1429 | } | |
1430 | return ret; | |
1431 | } | |
1432 | ||
1433 | ||
1434 | /** | |
1435 | * pevent_update_trivial - update the trivial filters with the given filter | |
1436 | * @dest - the filter to update | |
1437 | * @source - the filter as the source of the update | |
1438 | * @type - the type of trivial filter to update. | |
1439 | * | |
1440 | * Scan dest for trivial events matching @type to replace with the source. | |
1441 | * | |
1442 | * Returns 0 on success and -1 if there was a problem updating, but | |
1443 | * events may have still been updated on error. | |
1444 | */ | |
1445 | int pevent_update_trivial(struct event_filter *dest, struct event_filter *source, | |
1446 | enum filter_trivial_type type) | |
1447 | { | |
1448 | struct pevent *src_pevent; | |
1449 | struct pevent *dest_pevent; | |
1450 | struct event_format *event; | |
1451 | struct filter_type *filter_type; | |
1452 | struct filter_arg *arg; | |
1453 | char *str; | |
1454 | int i; | |
1455 | ||
1456 | src_pevent = source->pevent; | |
1457 | dest_pevent = dest->pevent; | |
1458 | ||
1459 | /* Do nothing if either of the filters has nothing to filter */ | |
1460 | if (!dest->filters || !source->filters) | |
1461 | return 0; | |
1462 | ||
1463 | for (i = 0; i < dest->filters; i++) { | |
1464 | filter_type = &dest->event_filters[i]; | |
1465 | arg = filter_type->filter; | |
1466 | if (arg->type != FILTER_ARG_BOOLEAN) | |
1467 | continue; | |
668fe01f SR |
1468 | if ((arg->boolean.value && type == FILTER_TRIVIAL_FALSE) || |
1469 | (!arg->boolean.value && type == FILTER_TRIVIAL_TRUE)) | |
f7d82350 SR |
1470 | continue; |
1471 | ||
1472 | event = filter_type->event; | |
1473 | ||
1474 | if (src_pevent != dest_pevent) { | |
1475 | /* do a look up */ | |
1476 | event = pevent_find_event_by_name(src_pevent, | |
1477 | event->system, | |
1478 | event->name); | |
1479 | if (!event) | |
1480 | return -1; | |
1481 | } | |
1482 | ||
1483 | str = pevent_filter_make_string(source, event->id); | |
1484 | if (!str) | |
1485 | continue; | |
1486 | ||
1487 | /* Don't bother if the filter is trivial too */ | |
1488 | if (strcmp(str, "TRUE") != 0 && strcmp(str, "FALSE") != 0) | |
1489 | filter_event(dest, event, str, NULL); | |
1490 | free(str); | |
1491 | } | |
1492 | return 0; | |
1493 | } | |
1494 | ||
1495 | /** | |
1496 | * pevent_filter_clear_trivial - clear TRUE and FALSE filters | |
1497 | * @filter: the filter to remove trivial filters from | |
1498 | * @type: remove only true, false, or both | |
1499 | * | |
1500 | * Removes filters that only contain a TRUE or FALES boolean arg. | |
1501 | */ | |
1502 | void pevent_filter_clear_trivial(struct event_filter *filter, | |
1503 | enum filter_trivial_type type) | |
1504 | { | |
1505 | struct filter_type *filter_type; | |
1506 | int count = 0; | |
f6ced60f | 1507 | int *ids = NULL; |
f7d82350 SR |
1508 | int i; |
1509 | ||
1510 | if (!filter->filters) | |
1511 | return; | |
1512 | ||
1513 | /* | |
1514 | * Two steps, first get all ids with trivial filters. | |
1515 | * then remove those ids. | |
1516 | */ | |
1517 | for (i = 0; i < filter->filters; i++) { | |
1518 | filter_type = &filter->event_filters[i]; | |
1519 | if (filter_type->filter->type != FILTER_ARG_BOOLEAN) | |
1520 | continue; | |
1521 | switch (type) { | |
1522 | case FILTER_TRIVIAL_FALSE: | |
668fe01f | 1523 | if (filter_type->filter->boolean.value) |
f7d82350 SR |
1524 | continue; |
1525 | case FILTER_TRIVIAL_TRUE: | |
668fe01f | 1526 | if (!filter_type->filter->boolean.value) |
f7d82350 SR |
1527 | continue; |
1528 | default: | |
1529 | break; | |
1530 | } | |
f6ced60f NK |
1531 | |
1532 | ids = realloc(ids, sizeof(*ids) * (count + 1)); | |
f7d82350 SR |
1533 | if (!ids) |
1534 | die("Can't allocate ids"); | |
1535 | ids[count++] = filter_type->event_id; | |
1536 | } | |
1537 | ||
1538 | if (!count) | |
1539 | return; | |
1540 | ||
1541 | for (i = 0; i < count; i++) | |
1542 | pevent_filter_remove_event(filter, ids[i]); | |
1543 | ||
1544 | free(ids); | |
1545 | } | |
1546 | ||
1547 | /** | |
1548 | * pevent_filter_event_has_trivial - return true event contains trivial filter | |
1549 | * @filter: the filter with the information | |
1550 | * @event_id: the id of the event to test | |
1551 | * @type: trivial type to test for (TRUE, FALSE, EITHER) | |
1552 | * | |
1553 | * Returns 1 if the event contains a matching trivial type | |
1554 | * otherwise 0. | |
1555 | */ | |
1556 | int pevent_filter_event_has_trivial(struct event_filter *filter, | |
1557 | int event_id, | |
1558 | enum filter_trivial_type type) | |
1559 | { | |
1560 | struct filter_type *filter_type; | |
1561 | ||
1562 | if (!filter->filters) | |
1563 | return 0; | |
1564 | ||
1565 | filter_type = find_filter_type(filter, event_id); | |
1566 | ||
1567 | if (!filter_type) | |
1568 | return 0; | |
1569 | ||
1570 | if (filter_type->filter->type != FILTER_ARG_BOOLEAN) | |
1571 | return 0; | |
1572 | ||
1573 | switch (type) { | |
1574 | case FILTER_TRIVIAL_FALSE: | |
668fe01f | 1575 | return !filter_type->filter->boolean.value; |
f7d82350 SR |
1576 | |
1577 | case FILTER_TRIVIAL_TRUE: | |
668fe01f | 1578 | return filter_type->filter->boolean.value; |
f7d82350 SR |
1579 | default: |
1580 | return 1; | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | static int test_filter(struct event_format *event, | |
1c698186 | 1585 | struct filter_arg *arg, struct pevent_record *record); |
f7d82350 SR |
1586 | |
1587 | static const char * | |
1c698186 | 1588 | get_comm(struct event_format *event, struct pevent_record *record) |
f7d82350 SR |
1589 | { |
1590 | const char *comm; | |
1591 | int pid; | |
1592 | ||
1593 | pid = pevent_data_pid(event->pevent, record); | |
1594 | comm = pevent_data_comm_from_pid(event->pevent, pid); | |
1595 | return comm; | |
1596 | } | |
1597 | ||
1598 | static unsigned long long | |
1599 | get_value(struct event_format *event, | |
1c698186 | 1600 | struct format_field *field, struct pevent_record *record) |
f7d82350 SR |
1601 | { |
1602 | unsigned long long val; | |
1603 | ||
1604 | /* Handle our dummy "comm" field */ | |
1605 | if (field == &comm) { | |
1606 | const char *name; | |
1607 | ||
1608 | name = get_comm(event, record); | |
42c59cda | 1609 | return (unsigned long)name; |
f7d82350 SR |
1610 | } |
1611 | ||
1612 | pevent_read_number_field(field, record->data, &val); | |
1613 | ||
1614 | if (!(field->flags & FIELD_IS_SIGNED)) | |
1615 | return val; | |
1616 | ||
1617 | switch (field->size) { | |
1618 | case 1: | |
1619 | return (char)val; | |
1620 | case 2: | |
1621 | return (short)val; | |
1622 | case 4: | |
1623 | return (int)val; | |
1624 | case 8: | |
1625 | return (long long)val; | |
1626 | } | |
1627 | return val; | |
1628 | } | |
1629 | ||
1630 | static unsigned long long | |
1c698186 | 1631 | get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record); |
f7d82350 SR |
1632 | |
1633 | static unsigned long long | |
1c698186 | 1634 | get_exp_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1635 | { |
1636 | unsigned long long lval, rval; | |
1637 | ||
1638 | lval = get_arg_value(event, arg->exp.left, record); | |
1639 | rval = get_arg_value(event, arg->exp.right, record); | |
1640 | ||
1641 | switch (arg->exp.type) { | |
1642 | case FILTER_EXP_ADD: | |
1643 | return lval + rval; | |
1644 | ||
1645 | case FILTER_EXP_SUB: | |
1646 | return lval - rval; | |
1647 | ||
1648 | case FILTER_EXP_MUL: | |
1649 | return lval * rval; | |
1650 | ||
1651 | case FILTER_EXP_DIV: | |
1652 | return lval / rval; | |
1653 | ||
1654 | case FILTER_EXP_MOD: | |
1655 | return lval % rval; | |
1656 | ||
1657 | case FILTER_EXP_RSHIFT: | |
1658 | return lval >> rval; | |
1659 | ||
1660 | case FILTER_EXP_LSHIFT: | |
1661 | return lval << rval; | |
1662 | ||
1663 | case FILTER_EXP_AND: | |
1664 | return lval & rval; | |
1665 | ||
1666 | case FILTER_EXP_OR: | |
1667 | return lval | rval; | |
1668 | ||
1669 | case FILTER_EXP_XOR: | |
1670 | return lval ^ rval; | |
1671 | ||
1672 | case FILTER_EXP_NOT: | |
1673 | default: | |
1674 | die("error in exp"); | |
1675 | } | |
1676 | return 0; | |
1677 | } | |
1678 | ||
1679 | static unsigned long long | |
1c698186 | 1680 | get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1681 | { |
1682 | switch (arg->type) { | |
1683 | case FILTER_ARG_FIELD: | |
1684 | return get_value(event, arg->field.field, record); | |
1685 | ||
1686 | case FILTER_ARG_VALUE: | |
1687 | if (arg->value.type != FILTER_NUMBER) | |
1688 | die("must have number field!"); | |
1689 | return arg->value.val; | |
1690 | ||
1691 | case FILTER_ARG_EXP: | |
1692 | return get_exp_value(event, arg, record); | |
1693 | ||
1694 | default: | |
1695 | die("oops in filter"); | |
1696 | } | |
1697 | return 0; | |
1698 | } | |
1699 | ||
1700 | static int test_num(struct event_format *event, | |
1c698186 | 1701 | struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1702 | { |
1703 | unsigned long long lval, rval; | |
1704 | ||
1705 | lval = get_arg_value(event, arg->num.left, record); | |
1706 | rval = get_arg_value(event, arg->num.right, record); | |
1707 | ||
1708 | switch (arg->num.type) { | |
1709 | case FILTER_CMP_EQ: | |
1710 | return lval == rval; | |
1711 | ||
1712 | case FILTER_CMP_NE: | |
1713 | return lval != rval; | |
1714 | ||
1715 | case FILTER_CMP_GT: | |
1716 | return lval > rval; | |
1717 | ||
1718 | case FILTER_CMP_LT: | |
1719 | return lval < rval; | |
1720 | ||
1721 | case FILTER_CMP_GE: | |
1722 | return lval >= rval; | |
1723 | ||
1724 | case FILTER_CMP_LE: | |
1725 | return lval <= rval; | |
1726 | ||
1727 | default: | |
1728 | /* ?? */ | |
1729 | return 0; | |
1730 | } | |
1731 | } | |
1732 | ||
1c698186 | 1733 | static const char *get_field_str(struct filter_arg *arg, struct pevent_record *record) |
f7d82350 | 1734 | { |
e84c282b SR |
1735 | struct event_format *event; |
1736 | struct pevent *pevent; | |
1737 | unsigned long long addr; | |
1738 | const char *val = NULL; | |
1739 | char hex[64]; | |
f7d82350 | 1740 | |
e84c282b SR |
1741 | /* If the field is not a string convert it */ |
1742 | if (arg->str.field->flags & FIELD_IS_STRING) { | |
1743 | val = record->data + arg->str.field->offset; | |
1744 | ||
1745 | /* | |
1746 | * We need to copy the data since we can't be sure the field | |
1747 | * is null terminated. | |
1748 | */ | |
1749 | if (*(val + arg->str.field->size - 1)) { | |
1750 | /* copy it */ | |
1751 | memcpy(arg->str.buffer, val, arg->str.field->size); | |
1752 | /* the buffer is already NULL terminated */ | |
1753 | val = arg->str.buffer; | |
1754 | } | |
1755 | ||
1756 | } else { | |
1757 | event = arg->str.field->event; | |
1758 | pevent = event->pevent; | |
1759 | addr = get_value(event, arg->str.field, record); | |
1760 | ||
1761 | if (arg->str.field->flags & (FIELD_IS_POINTER | FIELD_IS_LONG)) | |
1762 | /* convert to a kernel symbol */ | |
1763 | val = pevent_find_function(pevent, addr); | |
1764 | ||
1765 | if (val == NULL) { | |
1766 | /* just use the hex of the string name */ | |
1767 | snprintf(hex, 64, "0x%llx", addr); | |
1768 | val = hex; | |
1769 | } | |
f7d82350 | 1770 | } |
e84c282b | 1771 | |
f7d82350 SR |
1772 | return val; |
1773 | } | |
1774 | ||
1775 | static int test_str(struct event_format *event, | |
1c698186 | 1776 | struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1777 | { |
1778 | const char *val; | |
1779 | ||
1780 | if (arg->str.field == &comm) | |
1781 | val = get_comm(event, record); | |
1782 | else | |
1783 | val = get_field_str(arg, record); | |
1784 | ||
1785 | switch (arg->str.type) { | |
1786 | case FILTER_CMP_MATCH: | |
1787 | return strcmp(val, arg->str.val) == 0; | |
1788 | ||
1789 | case FILTER_CMP_NOT_MATCH: | |
1790 | return strcmp(val, arg->str.val) != 0; | |
1791 | ||
1792 | case FILTER_CMP_REGEX: | |
1793 | /* Returns zero on match */ | |
1794 | return !regexec(&arg->str.reg, val, 0, NULL, 0); | |
1795 | ||
1796 | case FILTER_CMP_NOT_REGEX: | |
1797 | return regexec(&arg->str.reg, val, 0, NULL, 0); | |
1798 | ||
1799 | default: | |
1800 | /* ?? */ | |
1801 | return 0; | |
1802 | } | |
1803 | } | |
1804 | ||
1805 | static int test_op(struct event_format *event, | |
1c698186 | 1806 | struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1807 | { |
1808 | switch (arg->op.type) { | |
1809 | case FILTER_OP_AND: | |
1810 | return test_filter(event, arg->op.left, record) && | |
1811 | test_filter(event, arg->op.right, record); | |
1812 | ||
1813 | case FILTER_OP_OR: | |
1814 | return test_filter(event, arg->op.left, record) || | |
1815 | test_filter(event, arg->op.right, record); | |
1816 | ||
1817 | case FILTER_OP_NOT: | |
1818 | return !test_filter(event, arg->op.right, record); | |
1819 | ||
1820 | default: | |
1821 | /* ?? */ | |
1822 | return 0; | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | static int test_filter(struct event_format *event, | |
1c698186 | 1827 | struct filter_arg *arg, struct pevent_record *record) |
f7d82350 SR |
1828 | { |
1829 | switch (arg->type) { | |
1830 | case FILTER_ARG_BOOLEAN: | |
1831 | /* easy case */ | |
668fe01f | 1832 | return arg->boolean.value; |
f7d82350 SR |
1833 | |
1834 | case FILTER_ARG_OP: | |
1835 | return test_op(event, arg, record); | |
1836 | ||
1837 | case FILTER_ARG_NUM: | |
1838 | return test_num(event, arg, record); | |
1839 | ||
1840 | case FILTER_ARG_STR: | |
1841 | return test_str(event, arg, record); | |
1842 | ||
1843 | case FILTER_ARG_EXP: | |
1844 | case FILTER_ARG_VALUE: | |
1845 | case FILTER_ARG_FIELD: | |
1846 | /* | |
1847 | * Expressions, fields and values evaluate | |
1848 | * to true if they return non zero | |
1849 | */ | |
1850 | return !!get_arg_value(event, arg, record); | |
1851 | ||
1852 | default: | |
1853 | die("oops!"); | |
1854 | /* ?? */ | |
1855 | return 0; | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | /** | |
1860 | * pevent_event_filtered - return true if event has filter | |
1861 | * @filter: filter struct with filter information | |
1862 | * @event_id: event id to test if filter exists | |
1863 | * | |
1864 | * Returns 1 if filter found for @event_id | |
1865 | * otherwise 0; | |
1866 | */ | |
1867 | int pevent_event_filtered(struct event_filter *filter, | |
1868 | int event_id) | |
1869 | { | |
1870 | struct filter_type *filter_type; | |
1871 | ||
1872 | if (!filter->filters) | |
1873 | return 0; | |
1874 | ||
1875 | filter_type = find_filter_type(filter, event_id); | |
1876 | ||
1877 | return filter_type ? 1 : 0; | |
1878 | } | |
1879 | ||
1880 | /** | |
1881 | * pevent_filter_match - test if a record matches a filter | |
1882 | * @filter: filter struct with filter information | |
1883 | * @record: the record to test against the filter | |
1884 | * | |
1885 | * Returns: | |
1886 | * 1 - filter found for event and @record matches | |
1887 | * 0 - filter found for event and @record does not match | |
1888 | * -1 - no filter found for @record's event | |
1889 | * -2 - if no filters exist | |
1890 | */ | |
1891 | int pevent_filter_match(struct event_filter *filter, | |
1c698186 | 1892 | struct pevent_record *record) |
f7d82350 SR |
1893 | { |
1894 | struct pevent *pevent = filter->pevent; | |
1895 | struct filter_type *filter_type; | |
1896 | int event_id; | |
1897 | ||
1898 | if (!filter->filters) | |
1899 | return FILTER_NONE; | |
1900 | ||
1901 | event_id = pevent_data_type(pevent, record); | |
1902 | ||
1903 | filter_type = find_filter_type(filter, event_id); | |
1904 | ||
1905 | if (!filter_type) | |
1906 | return FILTER_NOEXIST; | |
1907 | ||
1908 | return test_filter(filter_type->event, filter_type->filter, record) ? | |
1909 | FILTER_MATCH : FILTER_MISS; | |
1910 | } | |
1911 | ||
1912 | static char *op_to_str(struct event_filter *filter, struct filter_arg *arg) | |
1913 | { | |
1914 | char *str = NULL; | |
1915 | char *left = NULL; | |
1916 | char *right = NULL; | |
1917 | char *op = NULL; | |
1918 | int left_val = -1; | |
1919 | int right_val = -1; | |
1920 | int val; | |
1921 | int len; | |
1922 | ||
1923 | switch (arg->op.type) { | |
1924 | case FILTER_OP_AND: | |
1925 | op = "&&"; | |
1926 | /* fall through */ | |
1927 | case FILTER_OP_OR: | |
1928 | if (!op) | |
1929 | op = "||"; | |
1930 | ||
1931 | left = arg_to_str(filter, arg->op.left); | |
1932 | right = arg_to_str(filter, arg->op.right); | |
1933 | if (!left || !right) | |
1934 | break; | |
1935 | ||
1936 | /* Try to consolidate boolean values */ | |
1937 | if (strcmp(left, "TRUE") == 0) | |
1938 | left_val = 1; | |
1939 | else if (strcmp(left, "FALSE") == 0) | |
1940 | left_val = 0; | |
1941 | ||
1942 | if (strcmp(right, "TRUE") == 0) | |
1943 | right_val = 1; | |
1944 | else if (strcmp(right, "FALSE") == 0) | |
1945 | right_val = 0; | |
1946 | ||
1947 | if (left_val >= 0) { | |
1948 | if ((arg->op.type == FILTER_OP_AND && !left_val) || | |
1949 | (arg->op.type == FILTER_OP_OR && left_val)) { | |
1950 | /* Just return left value */ | |
1951 | str = left; | |
1952 | left = NULL; | |
1953 | break; | |
1954 | } | |
1955 | if (right_val >= 0) { | |
1956 | /* just evaluate this. */ | |
1957 | val = 0; | |
1958 | switch (arg->op.type) { | |
1959 | case FILTER_OP_AND: | |
1960 | val = left_val && right_val; | |
1961 | break; | |
1962 | case FILTER_OP_OR: | |
1963 | val = left_val || right_val; | |
1964 | break; | |
1965 | default: | |
1966 | break; | |
1967 | } | |
1968 | str = malloc_or_die(6); | |
1969 | if (val) | |
1970 | strcpy(str, "TRUE"); | |
1971 | else | |
1972 | strcpy(str, "FALSE"); | |
1973 | break; | |
1974 | } | |
1975 | } | |
1976 | if (right_val >= 0) { | |
1977 | if ((arg->op.type == FILTER_OP_AND && !right_val) || | |
1978 | (arg->op.type == FILTER_OP_OR && right_val)) { | |
1979 | /* Just return right value */ | |
1980 | str = right; | |
1981 | right = NULL; | |
1982 | break; | |
1983 | } | |
1984 | /* The right value is meaningless */ | |
1985 | str = left; | |
1986 | left = NULL; | |
1987 | break; | |
1988 | } | |
1989 | ||
1990 | len = strlen(left) + strlen(right) + strlen(op) + 10; | |
1991 | str = malloc_or_die(len); | |
1992 | snprintf(str, len, "(%s) %s (%s)", | |
1993 | left, op, right); | |
1994 | break; | |
1995 | ||
1996 | case FILTER_OP_NOT: | |
1997 | op = "!"; | |
1998 | right = arg_to_str(filter, arg->op.right); | |
1999 | if (!right) | |
2000 | break; | |
2001 | ||
2002 | /* See if we can consolidate */ | |
2003 | if (strcmp(right, "TRUE") == 0) | |
2004 | right_val = 1; | |
2005 | else if (strcmp(right, "FALSE") == 0) | |
2006 | right_val = 0; | |
2007 | if (right_val >= 0) { | |
2008 | /* just return the opposite */ | |
2009 | str = malloc_or_die(6); | |
2010 | if (right_val) | |
2011 | strcpy(str, "FALSE"); | |
2012 | else | |
2013 | strcpy(str, "TRUE"); | |
2014 | break; | |
2015 | } | |
2016 | len = strlen(right) + strlen(op) + 3; | |
2017 | str = malloc_or_die(len); | |
2018 | snprintf(str, len, "%s(%s)", op, right); | |
2019 | break; | |
2020 | ||
2021 | default: | |
2022 | /* ?? */ | |
2023 | break; | |
2024 | } | |
2025 | free(left); | |
2026 | free(right); | |
2027 | return str; | |
2028 | } | |
2029 | ||
2030 | static char *val_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2031 | { | |
2032 | char *str; | |
2033 | ||
2034 | str = malloc_or_die(30); | |
2035 | ||
2036 | snprintf(str, 30, "%lld", arg->value.val); | |
2037 | ||
2038 | return str; | |
2039 | } | |
2040 | ||
2041 | static char *field_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2042 | { | |
2043 | return strdup(arg->field.field->name); | |
2044 | } | |
2045 | ||
2046 | static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2047 | { | |
2048 | char *lstr; | |
2049 | char *rstr; | |
2050 | char *op; | |
0fed4834 | 2051 | char *str = NULL; |
f7d82350 SR |
2052 | int len; |
2053 | ||
2054 | lstr = arg_to_str(filter, arg->exp.left); | |
2055 | rstr = arg_to_str(filter, arg->exp.right); | |
0fed4834 NK |
2056 | if (!lstr || !rstr) |
2057 | goto out; | |
f7d82350 SR |
2058 | |
2059 | switch (arg->exp.type) { | |
2060 | case FILTER_EXP_ADD: | |
2061 | op = "+"; | |
2062 | break; | |
2063 | case FILTER_EXP_SUB: | |
2064 | op = "-"; | |
2065 | break; | |
2066 | case FILTER_EXP_MUL: | |
2067 | op = "*"; | |
2068 | break; | |
2069 | case FILTER_EXP_DIV: | |
2070 | op = "/"; | |
2071 | break; | |
2072 | case FILTER_EXP_MOD: | |
2073 | op = "%"; | |
2074 | break; | |
2075 | case FILTER_EXP_RSHIFT: | |
2076 | op = ">>"; | |
2077 | break; | |
2078 | case FILTER_EXP_LSHIFT: | |
2079 | op = "<<"; | |
2080 | break; | |
2081 | case FILTER_EXP_AND: | |
2082 | op = "&"; | |
2083 | break; | |
2084 | case FILTER_EXP_OR: | |
2085 | op = "|"; | |
2086 | break; | |
2087 | case FILTER_EXP_XOR: | |
2088 | op = "^"; | |
2089 | break; | |
2090 | default: | |
2091 | die("oops in exp"); | |
2092 | } | |
2093 | ||
2094 | len = strlen(op) + strlen(lstr) + strlen(rstr) + 4; | |
2095 | str = malloc_or_die(len); | |
2096 | snprintf(str, len, "%s %s %s", lstr, op, rstr); | |
0fed4834 | 2097 | out: |
f7d82350 SR |
2098 | free(lstr); |
2099 | free(rstr); | |
2100 | ||
2101 | return str; | |
2102 | } | |
2103 | ||
2104 | static char *num_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2105 | { | |
2106 | char *lstr; | |
2107 | char *rstr; | |
2108 | char *str = NULL; | |
2109 | char *op = NULL; | |
2110 | int len; | |
2111 | ||
2112 | lstr = arg_to_str(filter, arg->num.left); | |
2113 | rstr = arg_to_str(filter, arg->num.right); | |
0fed4834 NK |
2114 | if (!lstr || !rstr) |
2115 | goto out; | |
f7d82350 SR |
2116 | |
2117 | switch (arg->num.type) { | |
2118 | case FILTER_CMP_EQ: | |
2119 | op = "=="; | |
2120 | /* fall through */ | |
2121 | case FILTER_CMP_NE: | |
2122 | if (!op) | |
2123 | op = "!="; | |
2124 | /* fall through */ | |
2125 | case FILTER_CMP_GT: | |
2126 | if (!op) | |
2127 | op = ">"; | |
2128 | /* fall through */ | |
2129 | case FILTER_CMP_LT: | |
2130 | if (!op) | |
2131 | op = "<"; | |
2132 | /* fall through */ | |
2133 | case FILTER_CMP_GE: | |
2134 | if (!op) | |
2135 | op = ">="; | |
2136 | /* fall through */ | |
2137 | case FILTER_CMP_LE: | |
2138 | if (!op) | |
2139 | op = "<="; | |
2140 | ||
2141 | len = strlen(lstr) + strlen(op) + strlen(rstr) + 4; | |
2142 | str = malloc_or_die(len); | |
2143 | sprintf(str, "%s %s %s", lstr, op, rstr); | |
2144 | ||
2145 | break; | |
2146 | ||
2147 | default: | |
2148 | /* ?? */ | |
2149 | break; | |
2150 | } | |
2151 | ||
0fed4834 | 2152 | out: |
f7d82350 SR |
2153 | free(lstr); |
2154 | free(rstr); | |
2155 | return str; | |
2156 | } | |
2157 | ||
2158 | static char *str_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2159 | { | |
2160 | char *str = NULL; | |
2161 | char *op = NULL; | |
2162 | int len; | |
2163 | ||
2164 | switch (arg->str.type) { | |
2165 | case FILTER_CMP_MATCH: | |
2166 | op = "=="; | |
2167 | /* fall through */ | |
2168 | case FILTER_CMP_NOT_MATCH: | |
2169 | if (!op) | |
2170 | op = "!="; | |
2171 | /* fall through */ | |
2172 | case FILTER_CMP_REGEX: | |
2173 | if (!op) | |
2174 | op = "=~"; | |
2175 | /* fall through */ | |
2176 | case FILTER_CMP_NOT_REGEX: | |
2177 | if (!op) | |
2178 | op = "!~"; | |
2179 | ||
2180 | len = strlen(arg->str.field->name) + strlen(op) + | |
2181 | strlen(arg->str.val) + 6; | |
2182 | str = malloc_or_die(len); | |
2183 | snprintf(str, len, "%s %s \"%s\"", | |
2184 | arg->str.field->name, | |
2185 | op, arg->str.val); | |
2186 | break; | |
2187 | ||
2188 | default: | |
2189 | /* ?? */ | |
2190 | break; | |
2191 | } | |
2192 | return str; | |
2193 | } | |
2194 | ||
2195 | static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg) | |
2196 | { | |
2197 | char *str; | |
2198 | ||
2199 | switch (arg->type) { | |
2200 | case FILTER_ARG_BOOLEAN: | |
2201 | str = malloc_or_die(6); | |
668fe01f | 2202 | if (arg->boolean.value) |
f7d82350 SR |
2203 | strcpy(str, "TRUE"); |
2204 | else | |
2205 | strcpy(str, "FALSE"); | |
2206 | return str; | |
2207 | ||
2208 | case FILTER_ARG_OP: | |
2209 | return op_to_str(filter, arg); | |
2210 | ||
2211 | case FILTER_ARG_NUM: | |
2212 | return num_to_str(filter, arg); | |
2213 | ||
2214 | case FILTER_ARG_STR: | |
2215 | return str_to_str(filter, arg); | |
2216 | ||
2217 | case FILTER_ARG_VALUE: | |
2218 | return val_to_str(filter, arg); | |
2219 | ||
2220 | case FILTER_ARG_FIELD: | |
2221 | return field_to_str(filter, arg); | |
2222 | ||
2223 | case FILTER_ARG_EXP: | |
2224 | return exp_to_str(filter, arg); | |
2225 | ||
2226 | default: | |
2227 | /* ?? */ | |
2228 | return NULL; | |
2229 | } | |
2230 | ||
2231 | } | |
2232 | ||
2233 | /** | |
2234 | * pevent_filter_make_string - return a string showing the filter | |
2235 | * @filter: filter struct with filter information | |
2236 | * @event_id: the event id to return the filter string with | |
2237 | * | |
2238 | * Returns a string that displays the filter contents. | |
2239 | * This string must be freed with free(str). | |
2240 | * NULL is returned if no filter is found. | |
2241 | */ | |
2242 | char * | |
2243 | pevent_filter_make_string(struct event_filter *filter, int event_id) | |
2244 | { | |
2245 | struct filter_type *filter_type; | |
2246 | ||
2247 | if (!filter->filters) | |
2248 | return NULL; | |
2249 | ||
2250 | filter_type = find_filter_type(filter, event_id); | |
2251 | ||
2252 | if (!filter_type) | |
2253 | return NULL; | |
2254 | ||
2255 | return arg_to_str(filter, filter_type->filter); | |
2256 | } | |
2257 | ||
2258 | /** | |
2259 | * pevent_filter_compare - compare two filters and return if they are the same | |
2260 | * @filter1: Filter to compare with @filter2 | |
2261 | * @filter2: Filter to compare with @filter1 | |
2262 | * | |
2263 | * Returns: | |
2264 | * 1 if the two filters hold the same content. | |
2265 | * 0 if they do not. | |
2266 | */ | |
2267 | int pevent_filter_compare(struct event_filter *filter1, struct event_filter *filter2) | |
2268 | { | |
2269 | struct filter_type *filter_type1; | |
2270 | struct filter_type *filter_type2; | |
2271 | char *str1, *str2; | |
2272 | int result; | |
2273 | int i; | |
2274 | ||
2275 | /* Do the easy checks first */ | |
2276 | if (filter1->filters != filter2->filters) | |
2277 | return 0; | |
2278 | if (!filter1->filters && !filter2->filters) | |
2279 | return 1; | |
2280 | ||
2281 | /* | |
2282 | * Now take a look at each of the events to see if they have the same | |
2283 | * filters to them. | |
2284 | */ | |
2285 | for (i = 0; i < filter1->filters; i++) { | |
2286 | filter_type1 = &filter1->event_filters[i]; | |
2287 | filter_type2 = find_filter_type(filter2, filter_type1->event_id); | |
2288 | if (!filter_type2) | |
2289 | break; | |
2290 | if (filter_type1->filter->type != filter_type2->filter->type) | |
2291 | break; | |
2292 | switch (filter_type1->filter->type) { | |
2293 | case FILTER_TRIVIAL_FALSE: | |
2294 | case FILTER_TRIVIAL_TRUE: | |
2295 | /* trivial types just need the type compared */ | |
2296 | continue; | |
2297 | default: | |
2298 | break; | |
2299 | } | |
2300 | /* The best way to compare complex filters is with strings */ | |
2301 | str1 = arg_to_str(filter1, filter_type1->filter); | |
2302 | str2 = arg_to_str(filter2, filter_type2->filter); | |
0fed4834 NK |
2303 | if (str1 && str2) |
2304 | result = strcmp(str1, str2) != 0; | |
2305 | else | |
2306 | /* bail out if allocation fails */ | |
2307 | result = 1; | |
2308 | ||
f7d82350 SR |
2309 | free(str1); |
2310 | free(str2); | |
2311 | if (result) | |
2312 | break; | |
2313 | } | |
2314 | ||
2315 | if (i < filter1->filters) | |
2316 | return 0; | |
2317 | return 1; | |
2318 | } | |
2319 |