tracing/filters: Provide basic regex support
[linux-2.6.git] / kernel / trace / trace_events_filter.c
index 27c2dbe..d3c94c1 100644 (file)
@@ -18,8 +18,6 @@
  * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
  */
 
-#include <linux/debugfs.h>
-#include <linux/uaccess.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
 #include <linux/mutex.h>
@@ -121,6 +119,47 @@ struct filter_parse_state {
        } operand;
 };
 
+#define DEFINE_COMPARISON_PRED(type)                                   \
+static int filter_pred_##type(struct filter_pred *pred, void *event,   \
+                             int val1, int val2)                       \
+{                                                                      \
+       type *addr = (type *)(event + pred->offset);                    \
+       type val = (type)pred->val;                                     \
+       int match = 0;                                                  \
+                                                                       \
+       switch (pred->op) {                                             \
+       case OP_LT:                                                     \
+               match = (*addr < val);                                  \
+               break;                                                  \
+       case OP_LE:                                                     \
+               match = (*addr <= val);                                 \
+               break;                                                  \
+       case OP_GT:                                                     \
+               match = (*addr > val);                                  \
+               break;                                                  \
+       case OP_GE:                                                     \
+               match = (*addr >= val);                                 \
+               break;                                                  \
+       default:                                                        \
+               break;                                                  \
+       }                                                               \
+                                                                       \
+       return match;                                                   \
+}
+
+#define DEFINE_EQUALITY_PRED(size)                                     \
+static int filter_pred_##size(struct filter_pred *pred, void *event,   \
+                             int val1, int val2)                       \
+{                                                                      \
+       u##size *addr = (u##size *)(event + pred->offset);              \
+       u##size val = (u##size)pred->val;                               \
+       int match;                                                      \
+                                                                       \
+       match = (val == *addr) ^ pred->not;                             \
+                                                                       \
+       return match;                                                   \
+}
+
 DEFINE_COMPARISON_PRED(s64);
 DEFINE_COMPARISON_PRED(u64);
 DEFINE_COMPARISON_PRED(s32);
@@ -156,9 +195,23 @@ static int filter_pred_string(struct filter_pred *pred, void *event,
        char *addr = (char *)(event + pred->offset);
        int cmp, match;
 
-       cmp = strncmp(addr, pred->str_val, pred->str_len);
+       cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
 
-       match = (!cmp) ^ pred->not;
+       match = cmp ^ pred->not;
+
+       return match;
+}
+
+/* Filter predicate for char * pointers */
+static int filter_pred_pchar(struct filter_pred *pred, void *event,
+                            int val1, int val2)
+{
+       char **addr = (char **)(event + pred->offset);
+       int cmp, match;
+
+       cmp = pred->regex.match(*addr, &pred->regex, pred->regex.field_len);
+
+       match = cmp ^ pred->not;
 
        return match;
 }
@@ -182,9 +235,9 @@ static int filter_pred_strloc(struct filter_pred *pred, void *event,
        char *addr = (char *)(event + str_loc);
        int cmp, match;
 
-       cmp = strncmp(addr, pred->str_val, str_len);
+       cmp = pred->regex.match(addr, &pred->regex, str_len);
 
-       match = (!cmp) ^ pred->not;
+       match = cmp ^ pred->not;
 
        return match;
 }
@@ -195,6 +248,126 @@ static int filter_pred_none(struct filter_pred *pred, void *event,
        return 0;
 }
 
+/* Basic regex callbacks */
+static int regex_match_full(char *str, struct regex *r, int len)
+{
+       if (strncmp(str, r->pattern, len) == 0)
+               return 1;
+       return 0;
+}
+
+static int regex_match_front(char *str, struct regex *r, int len)
+{
+       if (strncmp(str, r->pattern, len) == 0)
+               return 1;
+       return 0;
+}
+
+static int regex_match_middle(char *str, struct regex *r, int len)
+{
+       if (strstr(str, r->pattern))
+               return 1;
+       return 0;
+}
+
+static int regex_match_end(char *str, struct regex *r, int len)
+{
+       char *ptr = strstr(str, r->pattern);
+
+       if (ptr && (ptr[r->len] == 0))
+               return 1;
+       return 0;
+}
+
+enum regex_type {
+       MATCH_FULL,
+       MATCH_FRONT_ONLY,
+       MATCH_MIDDLE_ONLY,
+       MATCH_END_ONLY,
+};
+
+/*
+ * Pass in a buffer containing a regex and this function will
+ * set search to point to the search part of the buffer and
+ * return the type of search it is (see enum above).
+ * This does modify buff.
+ *
+ * Returns enum type.
+ *  search returns the pointer to use for comparison.
+ *  not returns 1 if buff started with a '!'
+ *     0 otherwise.
+ */
+static enum regex_type
+filter_parse_regex(char *buff, int len, char **search, int *not)
+{
+       int type = MATCH_FULL;
+       int i;
+
+       if (buff[0] == '!') {
+               *not = 1;
+               buff++;
+               len--;
+       } else
+               *not = 0;
+
+       *search = buff;
+
+       for (i = 0; i < len; i++) {
+               if (buff[i] == '*') {
+                       if (!i) {
+                               *search = buff + 1;
+                               type = MATCH_END_ONLY;
+                       } else {
+                               if (type == MATCH_END_ONLY)
+                                       type = MATCH_MIDDLE_ONLY;
+                               else
+                                       type = MATCH_FRONT_ONLY;
+                               buff[i] = 0;
+                               break;
+                       }
+               }
+       }
+
+       return type;
+}
+
+static int filter_build_regex(struct filter_pred *pred)
+{
+       struct regex *r = &pred->regex;
+       char *search, *dup;
+       enum regex_type type;
+       int not;
+
+       type = filter_parse_regex(r->pattern, r->len, &search, &not);
+       dup = kstrdup(search, GFP_KERNEL);
+       if (!dup)
+               return -ENOMEM;
+
+       strcpy(r->pattern, dup);
+       kfree(dup);
+
+       r->len = strlen(r->pattern);
+
+       switch (type) {
+       case MATCH_FULL:
+               r->match = regex_match_full;
+               break;
+       case MATCH_FRONT_ONLY:
+               r->match = regex_match_front;
+               break;
+       case MATCH_MIDDLE_ONLY:
+               r->match = regex_match_middle;
+               break;
+       case MATCH_END_ONLY:
+               r->match = regex_match_end;
+               break;
+       }
+
+       pred->not ^= not;
+
+       return 0;
+}
+
 /* return 1 if event matches, 0 otherwise (discard) */
 int filter_match_preds(struct ftrace_event_call *call, void *rec)
 {
@@ -295,7 +468,7 @@ void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
        struct event_filter *filter = call->filter;
 
        mutex_lock(&event_mutex);
-       if (filter->filter_string)
+       if (filter && filter->filter_string)
                trace_seq_printf(s, "%s\n", filter->filter_string);
        else
                trace_seq_printf(s, "none\n");
@@ -308,7 +481,7 @@ void print_subsystem_event_filter(struct event_subsystem *system,
        struct event_filter *filter = system->filter;
 
        mutex_lock(&event_mutex);
-       if (filter->filter_string)
+       if (filter && filter->filter_string)
                trace_seq_printf(s, "%s\n", filter->filter_string);
        else
                trace_seq_printf(s, "none\n");
@@ -341,7 +514,7 @@ static void filter_clear_pred(struct filter_pred *pred)
 {
        kfree(pred->field_name);
        pred->field_name = NULL;
-       pred->str_len = 0;
+       pred->regex.len = 0;
 }
 
 static int filter_set_pred(struct filter_pred *dest,
@@ -376,6 +549,9 @@ void destroy_preds(struct ftrace_event_call *call)
        struct event_filter *filter = call->filter;
        int i;
 
+       if (!filter)
+               return;
+
        for (i = 0; i < MAX_FILTER_PRED; i++) {
                if (filter->preds[i])
                        filter_free_pred(filter->preds[i]);
@@ -386,17 +562,19 @@ void destroy_preds(struct ftrace_event_call *call)
        call->filter = NULL;
 }
 
-int init_preds(struct ftrace_event_call *call)
+static int init_preds(struct ftrace_event_call *call)
 {
        struct event_filter *filter;
        struct filter_pred *pred;
        int i;
 
+       if (call->filter)
+               return 0;
+
        filter = call->filter = kzalloc(sizeof(*filter), GFP_KERNEL);
        if (!call->filter)
                return -ENOMEM;
 
-       call->filter_active = 0;
        filter->n_preds = 0;
 
        filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
@@ -418,7 +596,26 @@ oom:
 
        return -ENOMEM;
 }
-EXPORT_SYMBOL_GPL(init_preds);
+
+static int init_subsystem_preds(struct event_subsystem *system)
+{
+       struct ftrace_event_call *call;
+       int err;
+
+       list_for_each_entry(call, &ftrace_events, list) {
+               if (!call->define_fields)
+                       continue;
+
+               if (strcmp(call->system, system->name) != 0)
+                       continue;
+
+               err = init_preds(call);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
 
 enum {
        FILTER_DISABLE_ALL,
@@ -435,6 +632,9 @@ static void filter_free_subsystem_preds(struct event_subsystem *system,
                if (!call->define_fields)
                        continue;
 
+               if (strcmp(call->system, system->name) != 0)
+                       continue;
+
                if (flag == FILTER_INIT_NO_RESET) {
                        call->filter->no_reset = false;
                        continue;
@@ -443,10 +643,8 @@ static void filter_free_subsystem_preds(struct event_subsystem *system,
                if (flag == FILTER_SKIP_NO_RESET && call->filter->no_reset)
                        continue;
 
-               if (!strcmp(call->system, system->name)) {
-                       filter_disable_preds(call);
-                       remove_filter_string(call->filter);
-               }
+               filter_disable_preds(call);
+               remove_filter_string(call->filter);
        }
 }
 
@@ -475,12 +673,7 @@ static int filter_add_pred_fn(struct filter_parse_state *ps,
        return 0;
 }
 
-enum {
-       FILTER_STATIC_STRING = 1,
-       FILTER_DYN_STRING
-};
-
-static int is_string_field(const char *type)
+int filter_assign_type(const char *type)
 {
        if (strstr(type, "__data_loc") && strstr(type, "char"))
                return FILTER_DYN_STRING;
@@ -488,12 +681,19 @@ static int is_string_field(const char *type)
        if (strchr(type, '[') && strstr(type, "char"))
                return FILTER_STATIC_STRING;
 
-       return 0;
+       return FILTER_OTHER;
+}
+
+static bool is_string_field(struct ftrace_event_field *field)
+{
+       return field->filter_type == FILTER_DYN_STRING ||
+              field->filter_type == FILTER_STATIC_STRING ||
+              field->filter_type == FILTER_PTR_STRING;
 }
 
 static int is_legal_op(struct ftrace_event_field *field, int op)
 {
-       if (is_string_field(field->type) && (op != OP_EQ && op != OP_NE))
+       if (is_string_field(field) && (op != OP_EQ && op != OP_NE))
                return 0;
 
        return 1;
@@ -550,7 +750,6 @@ static int filter_add_pred(struct filter_parse_state *ps,
        struct ftrace_event_field *field;
        filter_pred_fn_t fn;
        unsigned long long val;
-       int string_type;
        int ret;
 
        pred->fn = filter_pred_none;
@@ -578,18 +777,25 @@ static int filter_add_pred(struct filter_parse_state *ps,
                return -EINVAL;
        }
 
-       string_type = is_string_field(field->type);
-       if (string_type) {
-               if (string_type == FILTER_STATIC_STRING)
+       if (is_string_field(field)) {
+               ret = filter_build_regex(pred);
+               if (ret)
+                       return ret;
+
+               if (field->filter_type == FILTER_STATIC_STRING) {
                        fn = filter_pred_string;
-               else
-                       fn = filter_pred_strloc;
-               pred->str_len = field->size;
+                       pred->regex.field_len = field->size;
+               } else if (field->filter_type == FILTER_DYN_STRING)
+                               fn = filter_pred_strloc;
+               else {
+                       fn = filter_pred_pchar;
+                       pred->regex.field_len = strlen(pred->regex.pattern);
+               }
        } else {
                if (field->is_signed)
-                       ret = strict_strtoll(pred->str_val, 0, &val);
+                       ret = strict_strtoll(pred->regex.pattern, 0, &val);
                else
-                       ret = strict_strtoull(pred->str_val, 0, &val);
+                       ret = strict_strtoull(pred->regex.pattern, 0, &val);
                if (ret) {
                        parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
                        return -EINVAL;
@@ -959,8 +1165,8 @@ static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
                return NULL;
        }
 
-       strcpy(pred->str_val, operand2);
-       pred->str_len = strlen(operand2);
+       strcpy(pred->regex.pattern, operand2);
+       pred->regex.len = strlen(pred->regex.pattern);
 
        pred->op = op;
 
@@ -1050,6 +1256,8 @@ static int replace_preds(struct event_subsystem *system,
 
                pred = create_pred(elt->op, operand1, operand2);
 add_pred:
+               if (!pred)
+                       return -ENOMEM;
                if (call)
                        err = filter_add_pred(ps, call, pred, false);
                else
@@ -1073,6 +1281,10 @@ int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
 
        mutex_lock(&event_mutex);
 
+       err = init_preds(call);
+       if (err)
+               goto out_unlock;
+
        if (!strcmp(strstrip(filter_string), "0")) {
                filter_disable_preds(call);
                remove_filter_string(call->filter);
@@ -1118,6 +1330,10 @@ int apply_subsystem_event_filter(struct event_subsystem *system,
 
        mutex_lock(&event_mutex);
 
+       err = init_subsystem_preds(system);
+       if (err)
+               goto out_unlock;
+
        if (!strcmp(strstrip(filter_string), "0")) {
                filter_free_subsystem_preds(system, FILTER_DISABLE_ALL);
                remove_filter_string(system->filter);