2 * trace_events_filter - generic event filtering
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
21 #include <linux/debugfs.h>
22 #include <linux/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/ctype.h>
28 static int filter_pred_64(struct filter_pred *pred, void *event)
30 u64 *addr = (u64 *)(event + pred->offset);
31 u64 val = (u64)pred->val;
34 match = (val == *addr) ^ pred->not;
39 static int filter_pred_32(struct filter_pred *pred, void *event)
41 u32 *addr = (u32 *)(event + pred->offset);
42 u32 val = (u32)pred->val;
45 match = (val == *addr) ^ pred->not;
50 static int filter_pred_16(struct filter_pred *pred, void *event)
52 u16 *addr = (u16 *)(event + pred->offset);
53 u16 val = (u16)pred->val;
56 match = (val == *addr) ^ pred->not;
61 static int filter_pred_8(struct filter_pred *pred, void *event)
63 u8 *addr = (u8 *)(event + pred->offset);
64 u8 val = (u8)pred->val;
67 match = (val == *addr) ^ pred->not;
72 static int filter_pred_string(struct filter_pred *pred, void *event)
74 char *addr = (char *)(event + pred->offset);
77 cmp = strncmp(addr, pred->str_val, pred->str_len);
79 match = (!cmp) ^ pred->not;
84 /* return 1 if event matches, 0 otherwise (discard) */
85 int filter_match_preds(struct ftrace_event_call *call, void *rec)
87 int i, matched, and_failed = 0;
88 struct filter_pred *pred;
90 for (i = 0; i < MAX_FILTER_PRED; i++) {
92 pred = call->preds[i];
93 if (and_failed && !pred->or)
95 matched = pred->fn(pred, rec);
96 if (!matched && !pred->or) {
99 } else if (matched && pred->or)
111 int filter_print_preds(struct filter_pred **preds, char *buf)
113 ssize_t this_len = 0;
115 struct filter_pred *pred;
119 this_len += sprintf(buf + this_len, "none\n");
123 for (i = 0; i < MAX_FILTER_PRED; i++) {
126 field_name = pred->field_name;
128 this_len += sprintf(buf + this_len,
129 pred->or ? "|| " : "&& ");
130 this_len += sprintf(buf + this_len,
132 this_len += sprintf(buf + this_len,
133 pred->not ? "!= " : "== ");
135 this_len += sprintf(buf + this_len,
136 "%s\n", pred->str_val);
138 this_len += sprintf(buf + this_len,
139 "%llu\n", pred->val);
147 static struct ftrace_event_field *
148 find_event_field(struct ftrace_event_call *call, char *name)
150 struct ftrace_event_field *field;
151 struct list_head *entry, *tmp;
153 list_for_each_safe(entry, tmp, &call->fields) {
154 field = list_entry(entry, struct ftrace_event_field, link);
155 if (!strcmp(field->name, name))
162 void filter_free_pred(struct filter_pred *pred)
167 kfree(pred->field_name);
168 kfree(pred->str_val);
172 void filter_free_preds(struct ftrace_event_call *call)
177 for (i = 0; i < MAX_FILTER_PRED; i++)
178 filter_free_pred(call->preds[i]);
184 void filter_free_subsystem_preds(struct event_subsystem *system)
186 struct ftrace_event_call *call = __start_ftrace_events;
190 for (i = 0; i < MAX_FILTER_PRED; i++)
191 filter_free_pred(system->preds[i]);
192 kfree(system->preds);
193 system->preds = NULL;
196 events_for_each(call) {
197 if (!call->name || !call->regfunc)
200 if (!strcmp(call->system, system->name))
201 filter_free_preds(call);
205 static int __filter_add_pred(struct ftrace_event_call *call,
206 struct filter_pred *pred)
210 if (call->preds && !pred->compound)
211 filter_free_preds(call);
214 call->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred),
220 for (i = 0; i < MAX_FILTER_PRED; i++) {
221 if (!call->preds[i]) {
222 call->preds[i] = pred;
230 static int is_string_field(const char *type)
232 if (strchr(type, '[') && strstr(type, "char"))
238 int filter_add_pred(struct ftrace_event_call *call, struct filter_pred *pred)
240 struct ftrace_event_field *field;
242 field = find_event_field(call, pred->field_name);
246 pred->offset = field->offset;
248 if (is_string_field(field->type)) {
249 pred->fn = filter_pred_string;
250 pred->str_len = field->size;
251 return __filter_add_pred(call, pred);
254 switch (field->size) {
256 pred->fn = filter_pred_64;
259 pred->fn = filter_pred_32;
262 pred->fn = filter_pred_16;
265 pred->fn = filter_pred_8;
271 return __filter_add_pred(call, pred);
274 static struct filter_pred *copy_pred(struct filter_pred *pred)
276 struct filter_pred *new_pred = kmalloc(sizeof(*pred), GFP_KERNEL);
280 memcpy(new_pred, pred, sizeof(*pred));
282 new_pred->str_val = kstrdup(pred->str_val, GFP_KERNEL);
283 new_pred->field_name = kstrdup(pred->field_name, GFP_KERNEL);
284 if (!new_pred->str_val) {
293 int filter_add_subsystem_pred(struct event_subsystem *system,
294 struct filter_pred *pred)
296 struct ftrace_event_call *call = __start_ftrace_events;
297 struct filter_pred *event_pred;
300 if (system->preds && !pred->compound)
301 filter_free_subsystem_preds(system);
303 if (!system->preds) {
304 system->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred),
310 for (i = 0; i < MAX_FILTER_PRED; i++) {
311 if (!system->preds[i]) {
312 system->preds[i] = pred;
315 if (i == MAX_FILTER_PRED - 1)
319 events_for_each(call) {
320 if (!call->name || !call->regfunc)
323 if (!strcmp(call->system, system->name)) {
324 event_pred = copy_pred(pred);
326 filter_add_pred(call, event_pred);
333 int filter_parse(char **pbuf, struct filter_pred *pred)
335 char *tmp, *tok, *val_str = NULL;
338 /* field ==/!= number, or/and field ==/!= number, number */
339 while ((tok = strsep(pbuf, " \n"))) {
341 if (!strcmp(tok, "0")) {
344 } else if (!strcmp(tok, "&&")) {
347 } else if (!strcmp(tok, "||")) {
351 pred->field_name = tok;
356 if (!pred->field_name)
357 pred->field_name = tok;
358 else if (!strcmp(tok, "!="))
360 else if (!strcmp(tok, "=="))
363 pred->field_name = NULL;
370 if (pred->compound) {
371 if (!strcmp(tok, "!="))
373 else if (!strcmp(tok, "=="))
376 pred->field_name = NULL;
392 pred->field_name = kstrdup(pred->field_name, GFP_KERNEL);
393 if (!pred->field_name)
396 pred->val = simple_strtoull(val_str, &tmp, 10);
397 if (tmp == val_str) {
398 pred->str_val = kstrdup(val_str, GFP_KERNEL);