Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * trace_events_filter - generic event filtering |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | * |
| 18 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> |
| 19 | */ |
| 20 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/ctype.h> |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 24 | #include <linux/perf_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 26 | |
| 27 | #include "trace.h" |
Tom Zanussi | 4bda2d5 | 2009-03-24 02:14:31 -0500 | [diff] [blame] | 28 | #include "trace_output.h" |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 29 | |
Steven Rostedt | 49aa295 | 2011-11-02 16:46:46 -0400 | [diff] [blame] | 30 | #define DEFAULT_SYS_FILTER_MESSAGE \ |
| 31 | "### global filter ###\n" \ |
| 32 | "# Use this to set filters for multiple events.\n" \ |
| 33 | "# Only events with the given fields will be affected.\n" \ |
| 34 | "# If no events are modified, an error message will be displayed here" |
| 35 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 36 | enum filter_op_ids |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 37 | { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 38 | OP_OR, |
| 39 | OP_AND, |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 40 | OP_GLOB, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 41 | OP_NE, |
| 42 | OP_EQ, |
| 43 | OP_LT, |
| 44 | OP_LE, |
| 45 | OP_GT, |
| 46 | OP_GE, |
Steven Rostedt | 1a891cf | 2013-06-12 13:16:25 -0400 | [diff] [blame] | 47 | OP_BAND, |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 48 | OP_NOT, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 49 | OP_NONE, |
| 50 | OP_OPEN_PAREN, |
| 51 | }; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 52 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 53 | struct filter_op { |
| 54 | int id; |
| 55 | char *string; |
| 56 | int precedence; |
| 57 | }; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 58 | |
Steven Rostedt | 1a891cf | 2013-06-12 13:16:25 -0400 | [diff] [blame] | 59 | /* Order must be the same as enum filter_op_ids above */ |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 60 | static struct filter_op filter_ops[] = { |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 61 | { OP_OR, "||", 1 }, |
| 62 | { OP_AND, "&&", 2 }, |
| 63 | { OP_GLOB, "~", 4 }, |
| 64 | { OP_NE, "!=", 4 }, |
| 65 | { OP_EQ, "==", 4 }, |
| 66 | { OP_LT, "<", 5 }, |
| 67 | { OP_LE, "<=", 5 }, |
| 68 | { OP_GT, ">", 5 }, |
| 69 | { OP_GE, ">=", 5 }, |
Steven Rostedt | 1a891cf | 2013-06-12 13:16:25 -0400 | [diff] [blame] | 70 | { OP_BAND, "&", 6 }, |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 71 | { OP_NOT, "!", 6 }, |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 72 | { OP_NONE, "OP_NONE", 0 }, |
| 73 | { OP_OPEN_PAREN, "(", 0 }, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | enum { |
| 77 | FILT_ERR_NONE, |
| 78 | FILT_ERR_INVALID_OP, |
| 79 | FILT_ERR_UNBALANCED_PAREN, |
| 80 | FILT_ERR_TOO_MANY_OPERANDS, |
| 81 | FILT_ERR_OPERAND_TOO_LONG, |
| 82 | FILT_ERR_FIELD_NOT_FOUND, |
| 83 | FILT_ERR_ILLEGAL_FIELD_OP, |
| 84 | FILT_ERR_ILLEGAL_INTVAL, |
| 85 | FILT_ERR_BAD_SUBSYS_FILTER, |
| 86 | FILT_ERR_TOO_MANY_PREDS, |
| 87 | FILT_ERR_MISSING_FIELD, |
| 88 | FILT_ERR_INVALID_FILTER, |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 89 | FILT_ERR_IP_FIELD_ONLY, |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 90 | FILT_ERR_ILLEGAL_NOT_OP, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static char *err_text[] = { |
| 94 | "No error", |
| 95 | "Invalid operator", |
| 96 | "Unbalanced parens", |
| 97 | "Too many operands", |
| 98 | "Operand too long", |
| 99 | "Field not found", |
| 100 | "Illegal operation for field type", |
| 101 | "Illegal integer value", |
| 102 | "Couldn't find or set field in one of a subsystem's events", |
| 103 | "Too many terms in predicate expression", |
| 104 | "Missing field name and/or value", |
| 105 | "Meaningless filter expression", |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 106 | "Only 'ip' field is supported for function trace", |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 107 | "Illegal use of '!'", |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct opstack_op { |
| 111 | int op; |
| 112 | struct list_head list; |
| 113 | }; |
| 114 | |
| 115 | struct postfix_elt { |
| 116 | int op; |
| 117 | char *operand; |
| 118 | struct list_head list; |
| 119 | }; |
| 120 | |
| 121 | struct filter_parse_state { |
| 122 | struct filter_op *ops; |
| 123 | struct list_head opstack; |
| 124 | struct list_head postfix; |
| 125 | int lasterr; |
| 126 | int lasterr_pos; |
| 127 | |
| 128 | struct { |
| 129 | char *string; |
| 130 | unsigned int cnt; |
| 131 | unsigned int tail; |
| 132 | } infix; |
| 133 | |
| 134 | struct { |
| 135 | char string[MAX_FILTER_STR_VAL]; |
| 136 | int pos; |
| 137 | unsigned int tail; |
| 138 | } operand; |
| 139 | }; |
| 140 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 141 | struct pred_stack { |
| 142 | struct filter_pred **preds; |
| 143 | int index; |
| 144 | }; |
| 145 | |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 146 | /* If not of not match is equal to not of not, then it is a match */ |
Li Zefan | 197e2ea | 2009-09-10 09:34:19 +0800 | [diff] [blame] | 147 | #define DEFINE_COMPARISON_PRED(type) \ |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 148 | static int filter_pred_##type(struct filter_pred *pred, void *event) \ |
Li Zefan | 197e2ea | 2009-09-10 09:34:19 +0800 | [diff] [blame] | 149 | { \ |
| 150 | type *addr = (type *)(event + pred->offset); \ |
| 151 | type val = (type)pred->val; \ |
| 152 | int match = 0; \ |
| 153 | \ |
| 154 | switch (pred->op) { \ |
| 155 | case OP_LT: \ |
| 156 | match = (*addr < val); \ |
| 157 | break; \ |
| 158 | case OP_LE: \ |
| 159 | match = (*addr <= val); \ |
| 160 | break; \ |
| 161 | case OP_GT: \ |
| 162 | match = (*addr > val); \ |
| 163 | break; \ |
| 164 | case OP_GE: \ |
| 165 | match = (*addr >= val); \ |
| 166 | break; \ |
Steven Rostedt | 1a891cf | 2013-06-12 13:16:25 -0400 | [diff] [blame] | 167 | case OP_BAND: \ |
| 168 | match = (*addr & val); \ |
| 169 | break; \ |
Li Zefan | 197e2ea | 2009-09-10 09:34:19 +0800 | [diff] [blame] | 170 | default: \ |
| 171 | break; \ |
| 172 | } \ |
| 173 | \ |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 174 | return !!match == !pred->not; \ |
Li Zefan | 197e2ea | 2009-09-10 09:34:19 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | #define DEFINE_EQUALITY_PRED(size) \ |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 178 | static int filter_pred_##size(struct filter_pred *pred, void *event) \ |
Li Zefan | 197e2ea | 2009-09-10 09:34:19 +0800 | [diff] [blame] | 179 | { \ |
| 180 | u##size *addr = (u##size *)(event + pred->offset); \ |
| 181 | u##size val = (u##size)pred->val; \ |
| 182 | int match; \ |
| 183 | \ |
| 184 | match = (val == *addr) ^ pred->not; \ |
| 185 | \ |
| 186 | return match; \ |
| 187 | } |
| 188 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 189 | DEFINE_COMPARISON_PRED(s64); |
| 190 | DEFINE_COMPARISON_PRED(u64); |
| 191 | DEFINE_COMPARISON_PRED(s32); |
| 192 | DEFINE_COMPARISON_PRED(u32); |
| 193 | DEFINE_COMPARISON_PRED(s16); |
| 194 | DEFINE_COMPARISON_PRED(u16); |
| 195 | DEFINE_COMPARISON_PRED(s8); |
| 196 | DEFINE_COMPARISON_PRED(u8); |
| 197 | |
| 198 | DEFINE_EQUALITY_PRED(64); |
| 199 | DEFINE_EQUALITY_PRED(32); |
| 200 | DEFINE_EQUALITY_PRED(16); |
| 201 | DEFINE_EQUALITY_PRED(8); |
| 202 | |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 203 | /* Filter predicate for fixed sized arrays of characters */ |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 204 | static int filter_pred_string(struct filter_pred *pred, void *event) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 205 | { |
| 206 | char *addr = (char *)(event + pred->offset); |
| 207 | int cmp, match; |
| 208 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 209 | cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len); |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 210 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 211 | match = cmp ^ pred->not; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 212 | |
| 213 | return match; |
| 214 | } |
| 215 | |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 216 | /* Filter predicate for char * pointers */ |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 217 | static int filter_pred_pchar(struct filter_pred *pred, void *event) |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 218 | { |
| 219 | char **addr = (char **)(event + pred->offset); |
| 220 | int cmp, match; |
Li Zefan | 16da27a | 2010-01-14 10:54:27 +0800 | [diff] [blame] | 221 | int len = strlen(*addr) + 1; /* including tailing '\0' */ |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 222 | |
Li Zefan | 16da27a | 2010-01-14 10:54:27 +0800 | [diff] [blame] | 223 | cmp = pred->regex.match(*addr, &pred->regex, len); |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 224 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 225 | match = cmp ^ pred->not; |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 226 | |
| 227 | return match; |
| 228 | } |
| 229 | |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 230 | /* |
| 231 | * Filter predicate for dynamic sized arrays of characters. |
| 232 | * These are implemented through a list of strings at the end |
| 233 | * of the entry. |
| 234 | * Also each of these strings have a field in the entry which |
| 235 | * contains its offset from the beginning of the entry. |
| 236 | * We have then first to get this field, dereference it |
| 237 | * and add it to the address of the entry, and at last we have |
| 238 | * the address of the string. |
| 239 | */ |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 240 | static int filter_pred_strloc(struct filter_pred *pred, void *event) |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 241 | { |
Li Zefan | 7d536cb | 2009-07-16 10:54:02 +0800 | [diff] [blame] | 242 | u32 str_item = *(u32 *)(event + pred->offset); |
| 243 | int str_loc = str_item & 0xffff; |
| 244 | int str_len = str_item >> 16; |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 245 | char *addr = (char *)(event + str_loc); |
| 246 | int cmp, match; |
| 247 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 248 | cmp = pred->regex.match(addr, &pred->regex, str_len); |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 249 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 250 | match = cmp ^ pred->not; |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 251 | |
| 252 | return match; |
| 253 | } |
| 254 | |
Daniel Wagner | 9f61668 | 2015-08-10 14:35:46 +0200 | [diff] [blame] | 255 | /* Filter predicate for CPUs. */ |
| 256 | static int filter_pred_cpu(struct filter_pred *pred, void *event) |
| 257 | { |
| 258 | int cpu, cmp; |
| 259 | int match = 0; |
| 260 | |
| 261 | cpu = raw_smp_processor_id(); |
| 262 | cmp = pred->val; |
| 263 | |
| 264 | switch (pred->op) { |
| 265 | case OP_EQ: |
| 266 | match = cpu == cmp; |
| 267 | break; |
| 268 | case OP_LT: |
| 269 | match = cpu < cmp; |
| 270 | break; |
| 271 | case OP_LE: |
| 272 | match = cpu <= cmp; |
| 273 | break; |
| 274 | case OP_GT: |
| 275 | match = cpu > cmp; |
| 276 | break; |
| 277 | case OP_GE: |
| 278 | match = cpu >= cmp; |
| 279 | break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | return !!match == !pred->not; |
| 285 | } |
| 286 | |
| 287 | /* Filter predicate for COMM. */ |
| 288 | static int filter_pred_comm(struct filter_pred *pred, void *event) |
| 289 | { |
| 290 | int cmp, match; |
| 291 | |
| 292 | cmp = pred->regex.match(current->comm, &pred->regex, |
| 293 | pred->regex.field_len); |
| 294 | match = cmp ^ pred->not; |
| 295 | |
| 296 | return match; |
| 297 | } |
| 298 | |
Steven Rostedt | 58d9a59 | 2011-01-27 22:37:09 -0500 | [diff] [blame] | 299 | static int filter_pred_none(struct filter_pred *pred, void *event) |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
Li Zefan | d1303dd | 2010-01-14 10:54:40 +0800 | [diff] [blame] | 304 | /* |
| 305 | * regex_match_foo - Basic regex callbacks |
| 306 | * |
| 307 | * @str: the string to be searched |
| 308 | * @r: the regex structure containing the pattern string |
| 309 | * @len: the length of the string to be searched (including '\0') |
| 310 | * |
| 311 | * Note: |
| 312 | * - @str might not be NULL-terminated if it's of type DYN_STRING |
| 313 | * or STATIC_STRING |
| 314 | */ |
| 315 | |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 316 | static int regex_match_full(char *str, struct regex *r, int len) |
| 317 | { |
| 318 | if (strncmp(str, r->pattern, len) == 0) |
| 319 | return 1; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int regex_match_front(char *str, struct regex *r, int len) |
| 324 | { |
Steven Rostedt (VMware) | f94eef3 | 2018-05-09 11:59:32 -0400 | [diff] [blame] | 325 | if (len < r->len) |
| 326 | return 0; |
| 327 | |
Li Zefan | 285caad | 2010-01-14 10:53:21 +0800 | [diff] [blame] | 328 | if (strncmp(str, r->pattern, r->len) == 0) |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 329 | return 1; |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int regex_match_middle(char *str, struct regex *r, int len) |
| 334 | { |
Li Zefan | b2af211 | 2010-01-14 10:54:11 +0800 | [diff] [blame] | 335 | if (strnstr(str, r->pattern, len)) |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 336 | return 1; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int regex_match_end(char *str, struct regex *r, int len) |
| 341 | { |
Li Zefan | a3291c14 | 2010-01-14 10:53:41 +0800 | [diff] [blame] | 342 | int strlen = len - 1; |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 343 | |
Li Zefan | a3291c14 | 2010-01-14 10:53:41 +0800 | [diff] [blame] | 344 | if (strlen >= r->len && |
| 345 | memcmp(str + strlen - r->len, r->pattern, r->len) == 0) |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 346 | return 1; |
| 347 | return 0; |
| 348 | } |
| 349 | |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 350 | /** |
| 351 | * filter_parse_regex - parse a basic regex |
| 352 | * @buff: the raw regex |
| 353 | * @len: length of the regex |
| 354 | * @search: will point to the beginning of the string to compare |
| 355 | * @not: tell whether the match will have to be inverted |
| 356 | * |
| 357 | * This passes in a buffer containing a regex and this function will |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 358 | * set search to point to the search part of the buffer and |
| 359 | * return the type of search it is (see enum above). |
| 360 | * This does modify buff. |
| 361 | * |
| 362 | * Returns enum type. |
| 363 | * search returns the pointer to use for comparison. |
| 364 | * not returns 1 if buff started with a '!' |
| 365 | * 0 otherwise. |
| 366 | */ |
Frederic Weisbecker | 3f6fe06 | 2009-09-24 21:31:51 +0200 | [diff] [blame] | 367 | enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 368 | { |
| 369 | int type = MATCH_FULL; |
| 370 | int i; |
| 371 | |
| 372 | if (buff[0] == '!') { |
| 373 | *not = 1; |
| 374 | buff++; |
| 375 | len--; |
| 376 | } else |
| 377 | *not = 0; |
| 378 | |
| 379 | *search = buff; |
| 380 | |
| 381 | for (i = 0; i < len; i++) { |
| 382 | if (buff[i] == '*') { |
| 383 | if (!i) { |
| 384 | *search = buff + 1; |
| 385 | type = MATCH_END_ONLY; |
| 386 | } else { |
| 387 | if (type == MATCH_END_ONLY) |
| 388 | type = MATCH_MIDDLE_ONLY; |
| 389 | else |
| 390 | type = MATCH_FRONT_ONLY; |
| 391 | buff[i] = 0; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return type; |
| 398 | } |
| 399 | |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 400 | static void filter_build_regex(struct filter_pred *pred) |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 401 | { |
| 402 | struct regex *r = &pred->regex; |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 403 | char *search; |
| 404 | enum regex_type type = MATCH_FULL; |
| 405 | int not = 0; |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 406 | |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 407 | if (pred->op == OP_GLOB) { |
| 408 | type = filter_parse_regex(r->pattern, r->len, &search, ¬); |
| 409 | r->len = strlen(search); |
| 410 | memmove(r->pattern, search, r->len+1); |
| 411 | } |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 412 | |
| 413 | switch (type) { |
| 414 | case MATCH_FULL: |
| 415 | r->match = regex_match_full; |
| 416 | break; |
| 417 | case MATCH_FRONT_ONLY: |
| 418 | r->match = regex_match_front; |
| 419 | break; |
| 420 | case MATCH_MIDDLE_ONLY: |
| 421 | r->match = regex_match_middle; |
| 422 | break; |
| 423 | case MATCH_END_ONLY: |
| 424 | r->match = regex_match_end; |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | pred->not ^= not; |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 431 | enum move_type { |
| 432 | MOVE_DOWN, |
| 433 | MOVE_UP_FROM_LEFT, |
| 434 | MOVE_UP_FROM_RIGHT |
| 435 | }; |
| 436 | |
| 437 | static struct filter_pred * |
| 438 | get_pred_parent(struct filter_pred *pred, struct filter_pred *preds, |
| 439 | int index, enum move_type *move) |
| 440 | { |
| 441 | if (pred->parent & FILTER_PRED_IS_RIGHT) |
| 442 | *move = MOVE_UP_FROM_RIGHT; |
| 443 | else |
| 444 | *move = MOVE_UP_FROM_LEFT; |
| 445 | pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT]; |
| 446 | |
| 447 | return pred; |
| 448 | } |
| 449 | |
Jiri Olsa | f03f597 | 2011-08-11 16:25:49 +0200 | [diff] [blame] | 450 | enum walk_return { |
| 451 | WALK_PRED_ABORT, |
| 452 | WALK_PRED_PARENT, |
| 453 | WALK_PRED_DEFAULT, |
| 454 | }; |
| 455 | |
| 456 | typedef int (*filter_pred_walkcb_t) (enum move_type move, |
| 457 | struct filter_pred *pred, |
| 458 | int *err, void *data); |
| 459 | |
| 460 | static int walk_pred_tree(struct filter_pred *preds, |
| 461 | struct filter_pred *root, |
| 462 | filter_pred_walkcb_t cb, void *data) |
| 463 | { |
| 464 | struct filter_pred *pred = root; |
| 465 | enum move_type move = MOVE_DOWN; |
| 466 | int done = 0; |
| 467 | |
| 468 | if (!preds) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | do { |
| 472 | int err = 0, ret; |
| 473 | |
| 474 | ret = cb(move, pred, &err, data); |
| 475 | if (ret == WALK_PRED_ABORT) |
| 476 | return err; |
| 477 | if (ret == WALK_PRED_PARENT) |
| 478 | goto get_parent; |
| 479 | |
| 480 | switch (move) { |
| 481 | case MOVE_DOWN: |
| 482 | if (pred->left != FILTER_PRED_INVALID) { |
| 483 | pred = &preds[pred->left]; |
| 484 | continue; |
| 485 | } |
| 486 | goto get_parent; |
| 487 | case MOVE_UP_FROM_LEFT: |
| 488 | pred = &preds[pred->right]; |
| 489 | move = MOVE_DOWN; |
| 490 | continue; |
| 491 | case MOVE_UP_FROM_RIGHT: |
| 492 | get_parent: |
| 493 | if (pred == root) |
| 494 | break; |
| 495 | pred = get_pred_parent(pred, preds, |
| 496 | pred->parent, |
| 497 | &move); |
| 498 | continue; |
| 499 | } |
| 500 | done = 1; |
| 501 | } while (!done); |
| 502 | |
| 503 | /* We are fine. */ |
| 504 | return 0; |
| 505 | } |
| 506 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 507 | /* |
| 508 | * A series of AND or ORs where found together. Instead of |
| 509 | * climbing up and down the tree branches, an array of the |
| 510 | * ops were made in order of checks. We can just move across |
| 511 | * the array and short circuit if needed. |
| 512 | */ |
| 513 | static int process_ops(struct filter_pred *preds, |
| 514 | struct filter_pred *op, void *rec) |
| 515 | { |
| 516 | struct filter_pred *pred; |
Ingo Molnar | 1ef1d1c | 2011-03-18 14:41:27 +0100 | [diff] [blame] | 517 | int match = 0; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 518 | int type; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 519 | int i; |
| 520 | |
| 521 | /* |
| 522 | * Micro-optimization: We set type to true if op |
| 523 | * is an OR and false otherwise (AND). Then we |
| 524 | * just need to test if the match is equal to |
| 525 | * the type, and if it is, we can short circuit the |
| 526 | * rest of the checks: |
| 527 | * |
| 528 | * if ((match && op->op == OP_OR) || |
| 529 | * (!match && op->op == OP_AND)) |
| 530 | * return match; |
| 531 | */ |
| 532 | type = op->op == OP_OR; |
| 533 | |
| 534 | for (i = 0; i < op->val; i++) { |
| 535 | pred = &preds[op->ops[i]]; |
Jiri Olsa | f30120f | 2011-08-11 16:25:53 +0200 | [diff] [blame] | 536 | if (!WARN_ON_ONCE(!pred->fn)) |
| 537 | match = pred->fn(pred, rec); |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 538 | if (!!match == type) |
Steven Rostedt (Red Hat) | eabb898 | 2014-12-02 19:08:30 -0500 | [diff] [blame] | 539 | break; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 540 | } |
Steven Rostedt (Red Hat) | eabb898 | 2014-12-02 19:08:30 -0500 | [diff] [blame] | 541 | /* If not of not match is equal to not of not, then it is a match */ |
| 542 | return !!match == !op->not; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 543 | } |
| 544 | |
Jiri Olsa | f30120f | 2011-08-11 16:25:53 +0200 | [diff] [blame] | 545 | struct filter_match_preds_data { |
| 546 | struct filter_pred *preds; |
| 547 | int match; |
| 548 | void *rec; |
| 549 | }; |
| 550 | |
| 551 | static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred, |
| 552 | int *err, void *data) |
| 553 | { |
| 554 | struct filter_match_preds_data *d = data; |
| 555 | |
| 556 | *err = 0; |
| 557 | switch (move) { |
| 558 | case MOVE_DOWN: |
| 559 | /* only AND and OR have children */ |
| 560 | if (pred->left != FILTER_PRED_INVALID) { |
| 561 | /* If ops is set, then it was folded. */ |
| 562 | if (!pred->ops) |
| 563 | return WALK_PRED_DEFAULT; |
| 564 | /* We can treat folded ops as a leaf node */ |
| 565 | d->match = process_ops(d->preds, pred, d->rec); |
| 566 | } else { |
| 567 | if (!WARN_ON_ONCE(!pred->fn)) |
| 568 | d->match = pred->fn(pred, d->rec); |
| 569 | } |
| 570 | |
| 571 | return WALK_PRED_PARENT; |
| 572 | case MOVE_UP_FROM_LEFT: |
| 573 | /* |
| 574 | * Check for short circuits. |
| 575 | * |
| 576 | * Optimization: !!match == (pred->op == OP_OR) |
| 577 | * is the same as: |
| 578 | * if ((match && pred->op == OP_OR) || |
| 579 | * (!match && pred->op == OP_AND)) |
| 580 | */ |
| 581 | if (!!d->match == (pred->op == OP_OR)) |
| 582 | return WALK_PRED_PARENT; |
| 583 | break; |
| 584 | case MOVE_UP_FROM_RIGHT: |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | return WALK_PRED_DEFAULT; |
| 589 | } |
| 590 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 591 | /* return 1 if event matches, 0 otherwise (discard) */ |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 592 | int filter_match_preds(struct event_filter *filter, void *rec) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 593 | { |
Steven Rostedt | 74e9e58 | 2011-01-27 22:49:48 -0500 | [diff] [blame] | 594 | struct filter_pred *preds; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 595 | struct filter_pred *root; |
Jiri Olsa | f30120f | 2011-08-11 16:25:53 +0200 | [diff] [blame] | 596 | struct filter_match_preds_data data = { |
| 597 | /* match is currently meaningless */ |
| 598 | .match = -1, |
| 599 | .rec = rec, |
| 600 | }; |
| 601 | int n_preds, ret; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 602 | |
Steven Rostedt | 6d54057 | 2011-01-27 22:33:26 -0500 | [diff] [blame] | 603 | /* no filter is considered a match */ |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 604 | if (!filter) |
| 605 | return 1; |
| 606 | |
| 607 | n_preds = filter->n_preds; |
Steven Rostedt | 6d54057 | 2011-01-27 22:33:26 -0500 | [diff] [blame] | 608 | if (!n_preds) |
| 609 | return 1; |
| 610 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 611 | /* |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 612 | * n_preds, root and filter->preds are protect with preemption disabled. |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 613 | */ |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 614 | root = rcu_dereference_sched(filter->root); |
| 615 | if (!root) |
| 616 | return 1; |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 617 | |
Jiri Olsa | f30120f | 2011-08-11 16:25:53 +0200 | [diff] [blame] | 618 | data.preds = preds = rcu_dereference_sched(filter->preds); |
| 619 | ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data); |
| 620 | WARN_ON(ret); |
| 621 | return data.match; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 622 | } |
Steven Rostedt | 17c873e | 2009-04-10 18:12:50 -0400 | [diff] [blame] | 623 | EXPORT_SYMBOL_GPL(filter_match_preds); |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 624 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 625 | static void parse_error(struct filter_parse_state *ps, int err, int pos) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 626 | { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 627 | ps->lasterr = err; |
| 628 | ps->lasterr_pos = pos; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 631 | static void remove_filter_string(struct event_filter *filter) |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 632 | { |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 633 | if (!filter) |
| 634 | return; |
| 635 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 636 | kfree(filter->filter_string); |
| 637 | filter->filter_string = NULL; |
| 638 | } |
| 639 | |
| 640 | static int replace_filter_string(struct event_filter *filter, |
| 641 | char *filter_string) |
| 642 | { |
| 643 | kfree(filter->filter_string); |
| 644 | filter->filter_string = kstrdup(filter_string, GFP_KERNEL); |
| 645 | if (!filter->filter_string) |
| 646 | return -ENOMEM; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int append_filter_string(struct event_filter *filter, |
| 652 | char *string) |
| 653 | { |
| 654 | int newlen; |
| 655 | char *new_filter_string; |
| 656 | |
| 657 | BUG_ON(!filter->filter_string); |
| 658 | newlen = strlen(filter->filter_string) + strlen(string) + 1; |
| 659 | new_filter_string = kmalloc(newlen, GFP_KERNEL); |
| 660 | if (!new_filter_string) |
| 661 | return -ENOMEM; |
| 662 | |
| 663 | strcpy(new_filter_string, filter->filter_string); |
| 664 | strcat(new_filter_string, string); |
| 665 | kfree(filter->filter_string); |
| 666 | filter->filter_string = new_filter_string; |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static void append_filter_err(struct filter_parse_state *ps, |
| 672 | struct event_filter *filter) |
| 673 | { |
| 674 | int pos = ps->lasterr_pos; |
| 675 | char *buf, *pbuf; |
| 676 | |
| 677 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
| 678 | if (!buf) |
| 679 | return; |
| 680 | |
| 681 | append_filter_string(filter, "\n"); |
| 682 | memset(buf, ' ', PAGE_SIZE); |
| 683 | if (pos > PAGE_SIZE - 128) |
| 684 | pos = 0; |
| 685 | buf[pos] = '^'; |
| 686 | pbuf = &buf[pos] + 1; |
| 687 | |
| 688 | sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]); |
| 689 | append_filter_string(filter, buf); |
| 690 | free_page((unsigned long) buf); |
| 691 | } |
| 692 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 693 | static inline struct event_filter *event_filter(struct trace_event_file *file) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 694 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 695 | return file->filter; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* caller must hold event_mutex */ |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 699 | void print_event_filter(struct trace_event_file *file, struct trace_seq *s) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 700 | { |
| 701 | struct event_filter *filter = event_filter(file); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 702 | |
Li Zefan | 8e254c1 | 2009-08-31 16:49:41 +0800 | [diff] [blame] | 703 | if (filter && filter->filter_string) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 704 | trace_seq_printf(s, "%s\n", filter->filter_string); |
| 705 | else |
zhangwei(Jovi) | 146c344 | 2013-07-15 16:32:44 +0800 | [diff] [blame] | 706 | trace_seq_puts(s, "none\n"); |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 709 | void print_subsystem_event_filter(struct event_subsystem *system, |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 710 | struct trace_seq *s) |
| 711 | { |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 712 | struct event_filter *filter; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 713 | |
Li Zefan | 00e9583 | 2009-06-16 16:39:41 +0800 | [diff] [blame] | 714 | mutex_lock(&event_mutex); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 715 | filter = system->filter; |
Li Zefan | 8e254c1 | 2009-08-31 16:49:41 +0800 | [diff] [blame] | 716 | if (filter && filter->filter_string) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 717 | trace_seq_printf(s, "%s\n", filter->filter_string); |
| 718 | else |
zhangwei(Jovi) | 146c344 | 2013-07-15 16:32:44 +0800 | [diff] [blame] | 719 | trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n"); |
Li Zefan | 00e9583 | 2009-06-16 16:39:41 +0800 | [diff] [blame] | 720 | mutex_unlock(&event_mutex); |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 721 | } |
| 722 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 723 | static int __alloc_pred_stack(struct pred_stack *stack, int n_preds) |
| 724 | { |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 725 | stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL); |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 726 | if (!stack->preds) |
| 727 | return -ENOMEM; |
| 728 | stack->index = n_preds; |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static void __free_pred_stack(struct pred_stack *stack) |
| 733 | { |
| 734 | kfree(stack->preds); |
| 735 | stack->index = 0; |
| 736 | } |
| 737 | |
| 738 | static int __push_pred_stack(struct pred_stack *stack, |
| 739 | struct filter_pred *pred) |
| 740 | { |
| 741 | int index = stack->index; |
| 742 | |
| 743 | if (WARN_ON(index == 0)) |
| 744 | return -ENOSPC; |
| 745 | |
| 746 | stack->preds[--index] = pred; |
| 747 | stack->index = index; |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static struct filter_pred * |
| 752 | __pop_pred_stack(struct pred_stack *stack) |
| 753 | { |
| 754 | struct filter_pred *pred; |
| 755 | int index = stack->index; |
| 756 | |
| 757 | pred = stack->preds[index++]; |
| 758 | if (!pred) |
| 759 | return NULL; |
| 760 | |
| 761 | stack->index = index; |
| 762 | return pred; |
| 763 | } |
| 764 | |
| 765 | static int filter_set_pred(struct event_filter *filter, |
| 766 | int idx, |
| 767 | struct pred_stack *stack, |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 768 | struct filter_pred *src) |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 769 | { |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 770 | struct filter_pred *dest = &filter->preds[idx]; |
| 771 | struct filter_pred *left; |
| 772 | struct filter_pred *right; |
| 773 | |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 774 | *dest = *src; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 775 | dest->index = idx; |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 776 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 777 | if (dest->op == OP_OR || dest->op == OP_AND) { |
| 778 | right = __pop_pred_stack(stack); |
| 779 | left = __pop_pred_stack(stack); |
| 780 | if (!left || !right) |
| 781 | return -EINVAL; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 782 | /* |
| 783 | * If both children can be folded |
| 784 | * and they are the same op as this op or a leaf, |
| 785 | * then this op can be folded. |
| 786 | */ |
| 787 | if (left->index & FILTER_PRED_FOLD && |
Steven Rostedt (Red Hat) | eabb898 | 2014-12-02 19:08:30 -0500 | [diff] [blame] | 788 | ((left->op == dest->op && !left->not) || |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 789 | left->left == FILTER_PRED_INVALID) && |
| 790 | right->index & FILTER_PRED_FOLD && |
Steven Rostedt (Red Hat) | eabb898 | 2014-12-02 19:08:30 -0500 | [diff] [blame] | 791 | ((right->op == dest->op && !right->not) || |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 792 | right->left == FILTER_PRED_INVALID)) |
| 793 | dest->index |= FILTER_PRED_FOLD; |
| 794 | |
| 795 | dest->left = left->index & ~FILTER_PRED_FOLD; |
| 796 | dest->right = right->index & ~FILTER_PRED_FOLD; |
| 797 | left->parent = dest->index & ~FILTER_PRED_FOLD; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 798 | right->parent = dest->index | FILTER_PRED_IS_RIGHT; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 799 | } else { |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 800 | /* |
| 801 | * Make dest->left invalid to be used as a quick |
| 802 | * way to know this is a leaf node. |
| 803 | */ |
| 804 | dest->left = FILTER_PRED_INVALID; |
| 805 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 806 | /* All leafs allow folding the parent ops. */ |
| 807 | dest->index |= FILTER_PRED_FOLD; |
| 808 | } |
| 809 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 810 | return __push_pred_stack(stack, dest); |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 811 | } |
| 812 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 813 | static void __free_preds(struct event_filter *filter) |
| 814 | { |
Steven Rostedt (Red Hat) | 60705c8 | 2013-05-14 15:40:48 -0400 | [diff] [blame] | 815 | int i; |
| 816 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 817 | if (filter->preds) { |
Steven Rostedt (Red Hat) | 60705c8 | 2013-05-14 15:40:48 -0400 | [diff] [blame] | 818 | for (i = 0; i < filter->n_preds; i++) |
| 819 | kfree(filter->preds[i].ops); |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 820 | kfree(filter->preds); |
| 821 | filter->preds = NULL; |
| 822 | } |
| 823 | filter->a_preds = 0; |
| 824 | filter->n_preds = 0; |
| 825 | } |
| 826 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 827 | static void filter_disable(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 828 | { |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 829 | unsigned long old_flags = file->flags; |
| 830 | |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 831 | file->flags &= ~EVENT_FILE_FL_FILTERED; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 832 | |
| 833 | if (old_flags != file->flags) |
| 834 | trace_buffered_event_disable(); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 835 | } |
| 836 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 837 | static void __free_filter(struct event_filter *filter) |
Li Zefan | 2df75e4 | 2009-05-06 10:33:04 +0800 | [diff] [blame] | 838 | { |
Li Zefan | 8e254c1 | 2009-08-31 16:49:41 +0800 | [diff] [blame] | 839 | if (!filter) |
| 840 | return; |
| 841 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 842 | __free_preds(filter); |
Li Zefan | 57be888 | 2009-06-16 16:39:12 +0800 | [diff] [blame] | 843 | kfree(filter->filter_string); |
Li Zefan | 2df75e4 | 2009-05-06 10:33:04 +0800 | [diff] [blame] | 844 | kfree(filter); |
Li Zefan | 2df75e4 | 2009-05-06 10:33:04 +0800 | [diff] [blame] | 845 | } |
| 846 | |
Tom Zanussi | bac5fb9 | 2013-10-24 08:59:29 -0500 | [diff] [blame] | 847 | void free_event_filter(struct event_filter *filter) |
| 848 | { |
| 849 | __free_filter(filter); |
| 850 | } |
| 851 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 852 | static struct event_filter *__alloc_filter(void) |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 853 | { |
Tom Zanussi | 30e673b | 2009-04-28 03:04:47 -0500 | [diff] [blame] | 854 | struct event_filter *filter; |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 855 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 856 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 857 | return filter; |
| 858 | } |
Tom Zanussi | 30e673b | 2009-04-28 03:04:47 -0500 | [diff] [blame] | 859 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 860 | static int __alloc_preds(struct event_filter *filter, int n_preds) |
| 861 | { |
| 862 | struct filter_pred *pred; |
| 863 | int i; |
| 864 | |
Steven Rostedt | 4defe68 | 2011-02-03 23:29:06 -0500 | [diff] [blame] | 865 | if (filter->preds) |
| 866 | __free_preds(filter); |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 867 | |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 868 | filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL); |
Steven Rostedt | 4defe68 | 2011-02-03 23:29:06 -0500 | [diff] [blame] | 869 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 870 | if (!filter->preds) |
| 871 | return -ENOMEM; |
| 872 | |
Steven Rostedt | 4defe68 | 2011-02-03 23:29:06 -0500 | [diff] [blame] | 873 | filter->a_preds = n_preds; |
| 874 | filter->n_preds = 0; |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 875 | |
| 876 | for (i = 0; i < n_preds; i++) { |
Steven Rostedt | 74e9e58 | 2011-01-27 22:49:48 -0500 | [diff] [blame] | 877 | pred = &filter->preds[i]; |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 878 | pred->fn = filter_pred_none; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 879 | } |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 880 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 881 | return 0; |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 882 | } |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 883 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 884 | static inline void __remove_filter(struct trace_event_file *file) |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 885 | { |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 886 | filter_disable(file); |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 887 | remove_filter_string(file->filter); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Steven Rostedt (Red Hat) | 7967b3e | 2015-05-13 14:59:40 -0400 | [diff] [blame] | 890 | static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 891 | struct trace_array *tr) |
| 892 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 893 | struct trace_event_file *file; |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 894 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 895 | list_for_each_entry(file, &tr->events, list) { |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 896 | if (file->system != dir) |
Li Zefan | 8e254c1 | 2009-08-31 16:49:41 +0800 | [diff] [blame] | 897 | continue; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 898 | __remove_filter(file); |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 902 | static inline void __free_subsystem_filter(struct trace_event_file *file) |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 903 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 904 | __free_filter(file->filter); |
| 905 | file->filter = NULL; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 906 | } |
| 907 | |
Steven Rostedt (Red Hat) | 7967b3e | 2015-05-13 14:59:40 -0400 | [diff] [blame] | 908 | static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 909 | struct trace_array *tr) |
| 910 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 911 | struct trace_event_file *file; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 912 | |
| 913 | list_for_each_entry(file, &tr->events, list) { |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 914 | if (file->system != dir) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 915 | continue; |
| 916 | __free_subsystem_filter(file); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 920 | static int filter_add_pred(struct filter_parse_state *ps, |
| 921 | struct event_filter *filter, |
| 922 | struct filter_pred *pred, |
| 923 | struct pred_stack *stack) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 924 | { |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 925 | int err; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 926 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 927 | if (WARN_ON(filter->n_preds == filter->a_preds)) { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 928 | parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 929 | return -ENOSPC; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 930 | } |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 931 | |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 932 | err = filter_set_pred(filter, filter->n_preds, stack, pred); |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 933 | if (err) |
| 934 | return err; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 935 | |
Tom Zanussi | 30e673b | 2009-04-28 03:04:47 -0500 | [diff] [blame] | 936 | filter->n_preds++; |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 937 | |
| 938 | return 0; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 939 | } |
| 940 | |
Li Zefan | aa38e9f | 2009-08-07 10:33:02 +0800 | [diff] [blame] | 941 | int filter_assign_type(const char *type) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 942 | { |
Li Zefan | 7fcb7c4 | 2009-06-01 15:35:46 +0800 | [diff] [blame] | 943 | if (strstr(type, "__data_loc") && strstr(type, "char")) |
| 944 | return FILTER_DYN_STRING; |
| 945 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 946 | if (strchr(type, '[') && strstr(type, "char")) |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 947 | return FILTER_STATIC_STRING; |
| 948 | |
Li Zefan | aa38e9f | 2009-08-07 10:33:02 +0800 | [diff] [blame] | 949 | return FILTER_OTHER; |
| 950 | } |
| 951 | |
Yaowei Bai | 907bff9 | 2015-09-29 22:43:35 +0800 | [diff] [blame] | 952 | static bool is_legal_op(struct ftrace_event_field *field, int op) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 953 | { |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 954 | if (is_string_field(field) && |
| 955 | (op != OP_EQ && op != OP_NE && op != OP_GLOB)) |
Yaowei Bai | 907bff9 | 2015-09-29 22:43:35 +0800 | [diff] [blame] | 956 | return false; |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 957 | if (!is_string_field(field) && op == OP_GLOB) |
Yaowei Bai | 907bff9 | 2015-09-29 22:43:35 +0800 | [diff] [blame] | 958 | return false; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 959 | |
Yaowei Bai | 907bff9 | 2015-09-29 22:43:35 +0800 | [diff] [blame] | 960 | return true; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static filter_pred_fn_t select_comparison_fn(int op, int field_size, |
| 964 | int field_is_signed) |
| 965 | { |
| 966 | filter_pred_fn_t fn = NULL; |
| 967 | |
| 968 | switch (field_size) { |
| 969 | case 8: |
| 970 | if (op == OP_EQ || op == OP_NE) |
| 971 | fn = filter_pred_64; |
| 972 | else if (field_is_signed) |
| 973 | fn = filter_pred_s64; |
| 974 | else |
| 975 | fn = filter_pred_u64; |
| 976 | break; |
| 977 | case 4: |
| 978 | if (op == OP_EQ || op == OP_NE) |
| 979 | fn = filter_pred_32; |
| 980 | else if (field_is_signed) |
| 981 | fn = filter_pred_s32; |
| 982 | else |
| 983 | fn = filter_pred_u32; |
| 984 | break; |
| 985 | case 2: |
| 986 | if (op == OP_EQ || op == OP_NE) |
| 987 | fn = filter_pred_16; |
| 988 | else if (field_is_signed) |
| 989 | fn = filter_pred_s16; |
| 990 | else |
| 991 | fn = filter_pred_u16; |
| 992 | break; |
| 993 | case 1: |
| 994 | if (op == OP_EQ || op == OP_NE) |
| 995 | fn = filter_pred_8; |
| 996 | else if (field_is_signed) |
| 997 | fn = filter_pred_s8; |
| 998 | else |
| 999 | fn = filter_pred_u8; |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | return fn; |
| 1004 | } |
| 1005 | |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1006 | static int init_pred(struct filter_parse_state *ps, |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1007 | struct ftrace_event_field *field, |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1008 | struct filter_pred *pred) |
| 1009 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1010 | { |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1011 | filter_pred_fn_t fn = filter_pred_none; |
Li Zefan | f66578a | 2009-04-21 17:12:11 +0800 | [diff] [blame] | 1012 | unsigned long long val; |
Li Zefan | 5e4904c | 2009-06-15 10:58:39 +0800 | [diff] [blame] | 1013 | int ret; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1014 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1015 | pred->offset = field->offset; |
| 1016 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1017 | if (!is_legal_op(field, pred->op)) { |
| 1018 | parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0); |
| 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | |
Steven Rostedt (Red Hat) | e57cbaf | 2016-03-03 17:18:20 -0500 | [diff] [blame] | 1022 | if (field->filter_type == FILTER_COMM) { |
| 1023 | filter_build_regex(pred); |
| 1024 | fn = filter_pred_comm; |
| 1025 | pred->regex.field_len = TASK_COMM_LEN; |
| 1026 | } else if (is_string_field(field)) { |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 1027 | filter_build_regex(pred); |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 1028 | |
Steven Rostedt (Red Hat) | e57cbaf | 2016-03-03 17:18:20 -0500 | [diff] [blame] | 1029 | if (field->filter_type == FILTER_STATIC_STRING) { |
Frederic Weisbecker | e8808c1 | 2009-05-03 02:48:52 +0200 | [diff] [blame] | 1030 | fn = filter_pred_string; |
Frederic Weisbecker | 1889d20 | 2009-09-24 21:10:44 +0200 | [diff] [blame] | 1031 | pred->regex.field_len = field->size; |
| 1032 | } else if (field->filter_type == FILTER_DYN_STRING) |
Li Zefan | b0f1a59 | 2009-10-15 11:21:12 +0800 | [diff] [blame] | 1033 | fn = filter_pred_strloc; |
Li Zefan | 16da27a | 2010-01-14 10:54:27 +0800 | [diff] [blame] | 1034 | else |
Li Zefan | 87a342f | 2009-08-07 10:33:43 +0800 | [diff] [blame] | 1035 | fn = filter_pred_pchar; |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 1036 | } else if (is_function_field(field)) { |
| 1037 | if (strcmp(field->name, "ip")) { |
| 1038 | parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | } else { |
Li Zefan | 5e4904c | 2009-06-15 10:58:39 +0800 | [diff] [blame] | 1042 | if (field->is_signed) |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 1043 | ret = kstrtoll(pred->regex.pattern, 0, &val); |
Li Zefan | 5e4904c | 2009-06-15 10:58:39 +0800 | [diff] [blame] | 1044 | else |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 1045 | ret = kstrtoull(pred->regex.pattern, 0, &val); |
Li Zefan | 5e4904c | 2009-06-15 10:58:39 +0800 | [diff] [blame] | 1046 | if (ret) { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1047 | parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0); |
Tom Zanussi | 9f58a15 | 2009-03-24 02:14:42 -0500 | [diff] [blame] | 1048 | return -EINVAL; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1049 | } |
Li Zefan | f66578a | 2009-04-21 17:12:11 +0800 | [diff] [blame] | 1050 | pred->val = val; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1051 | |
Steven Rostedt (Red Hat) | e57cbaf | 2016-03-03 17:18:20 -0500 | [diff] [blame] | 1052 | if (field->filter_type == FILTER_CPU) |
Daniel Wagner | 9f61668 | 2015-08-10 14:35:46 +0200 | [diff] [blame] | 1053 | fn = filter_pred_cpu; |
| 1054 | else |
| 1055 | fn = select_comparison_fn(pred->op, field->size, |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1056 | field->is_signed); |
| 1057 | if (!fn) { |
| 1058 | parse_error(ps, FILT_ERR_INVALID_OP, 0); |
| 1059 | return -EINVAL; |
| 1060 | } |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1061 | } |
| 1062 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1063 | if (pred->op == OP_NE) |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 1064 | pred->not ^= 1; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1065 | |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1066 | pred->fn = fn; |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1067 | return 0; |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1070 | static void parse_init(struct filter_parse_state *ps, |
| 1071 | struct filter_op *ops, |
| 1072 | char *infix_string) |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1073 | { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1074 | memset(ps, '\0', sizeof(*ps)); |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1075 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1076 | ps->infix.string = infix_string; |
| 1077 | ps->infix.cnt = strlen(infix_string); |
| 1078 | ps->ops = ops; |
| 1079 | |
| 1080 | INIT_LIST_HEAD(&ps->opstack); |
| 1081 | INIT_LIST_HEAD(&ps->postfix); |
| 1082 | } |
| 1083 | |
| 1084 | static char infix_next(struct filter_parse_state *ps) |
| 1085 | { |
Steven Rostedt (Red Hat) | 6b88f44 | 2015-06-25 18:10:09 -0400 | [diff] [blame] | 1086 | if (!ps->infix.cnt) |
| 1087 | return 0; |
| 1088 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1089 | ps->infix.cnt--; |
| 1090 | |
| 1091 | return ps->infix.string[ps->infix.tail++]; |
| 1092 | } |
| 1093 | |
| 1094 | static char infix_peek(struct filter_parse_state *ps) |
| 1095 | { |
| 1096 | if (ps->infix.tail == strlen(ps->infix.string)) |
| 1097 | return 0; |
| 1098 | |
| 1099 | return ps->infix.string[ps->infix.tail]; |
| 1100 | } |
| 1101 | |
| 1102 | static void infix_advance(struct filter_parse_state *ps) |
| 1103 | { |
Steven Rostedt (Red Hat) | 6b88f44 | 2015-06-25 18:10:09 -0400 | [diff] [blame] | 1104 | if (!ps->infix.cnt) |
| 1105 | return; |
| 1106 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1107 | ps->infix.cnt--; |
| 1108 | ps->infix.tail++; |
| 1109 | } |
| 1110 | |
| 1111 | static inline int is_precedence_lower(struct filter_parse_state *ps, |
| 1112 | int a, int b) |
| 1113 | { |
| 1114 | return ps->ops[a].precedence < ps->ops[b].precedence; |
| 1115 | } |
| 1116 | |
| 1117 | static inline int is_op_char(struct filter_parse_state *ps, char c) |
| 1118 | { |
| 1119 | int i; |
| 1120 | |
| 1121 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { |
| 1122 | if (ps->ops[i].string[0] == c) |
| 1123 | return 1; |
| 1124 | } |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static int infix_get_op(struct filter_parse_state *ps, char firstc) |
| 1130 | { |
| 1131 | char nextc = infix_peek(ps); |
| 1132 | char opstr[3]; |
| 1133 | int i; |
| 1134 | |
| 1135 | opstr[0] = firstc; |
| 1136 | opstr[1] = nextc; |
| 1137 | opstr[2] = '\0'; |
| 1138 | |
| 1139 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { |
| 1140 | if (!strcmp(opstr, ps->ops[i].string)) { |
| 1141 | infix_advance(ps); |
| 1142 | return ps->ops[i].id; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1146 | opstr[1] = '\0'; |
| 1147 | |
| 1148 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { |
| 1149 | if (!strcmp(opstr, ps->ops[i].string)) |
| 1150 | return ps->ops[i].id; |
| 1151 | } |
| 1152 | |
| 1153 | return OP_NONE; |
| 1154 | } |
| 1155 | |
| 1156 | static inline void clear_operand_string(struct filter_parse_state *ps) |
| 1157 | { |
| 1158 | memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL); |
| 1159 | ps->operand.tail = 0; |
| 1160 | } |
| 1161 | |
| 1162 | static inline int append_operand_char(struct filter_parse_state *ps, char c) |
| 1163 | { |
Li Zefan | 5872144 | 2009-05-15 11:07:56 +0800 | [diff] [blame] | 1164 | if (ps->operand.tail == MAX_FILTER_STR_VAL - 1) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1165 | return -EINVAL; |
| 1166 | |
| 1167 | ps->operand.string[ps->operand.tail++] = c; |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int filter_opstack_push(struct filter_parse_state *ps, int op) |
| 1173 | { |
| 1174 | struct opstack_op *opstack_op; |
| 1175 | |
| 1176 | opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL); |
| 1177 | if (!opstack_op) |
| 1178 | return -ENOMEM; |
| 1179 | |
| 1180 | opstack_op->op = op; |
| 1181 | list_add(&opstack_op->list, &ps->opstack); |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static int filter_opstack_empty(struct filter_parse_state *ps) |
| 1187 | { |
| 1188 | return list_empty(&ps->opstack); |
| 1189 | } |
| 1190 | |
| 1191 | static int filter_opstack_top(struct filter_parse_state *ps) |
| 1192 | { |
| 1193 | struct opstack_op *opstack_op; |
| 1194 | |
| 1195 | if (filter_opstack_empty(ps)) |
| 1196 | return OP_NONE; |
| 1197 | |
| 1198 | opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list); |
| 1199 | |
| 1200 | return opstack_op->op; |
| 1201 | } |
| 1202 | |
| 1203 | static int filter_opstack_pop(struct filter_parse_state *ps) |
| 1204 | { |
| 1205 | struct opstack_op *opstack_op; |
| 1206 | int op; |
| 1207 | |
| 1208 | if (filter_opstack_empty(ps)) |
| 1209 | return OP_NONE; |
| 1210 | |
| 1211 | opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list); |
| 1212 | op = opstack_op->op; |
| 1213 | list_del(&opstack_op->list); |
| 1214 | |
| 1215 | kfree(opstack_op); |
| 1216 | |
| 1217 | return op; |
| 1218 | } |
| 1219 | |
| 1220 | static void filter_opstack_clear(struct filter_parse_state *ps) |
| 1221 | { |
| 1222 | while (!filter_opstack_empty(ps)) |
| 1223 | filter_opstack_pop(ps); |
| 1224 | } |
| 1225 | |
| 1226 | static char *curr_operand(struct filter_parse_state *ps) |
| 1227 | { |
| 1228 | return ps->operand.string; |
| 1229 | } |
| 1230 | |
| 1231 | static int postfix_append_operand(struct filter_parse_state *ps, char *operand) |
| 1232 | { |
| 1233 | struct postfix_elt *elt; |
| 1234 | |
| 1235 | elt = kmalloc(sizeof(*elt), GFP_KERNEL); |
| 1236 | if (!elt) |
| 1237 | return -ENOMEM; |
| 1238 | |
| 1239 | elt->op = OP_NONE; |
| 1240 | elt->operand = kstrdup(operand, GFP_KERNEL); |
| 1241 | if (!elt->operand) { |
| 1242 | kfree(elt); |
| 1243 | return -ENOMEM; |
| 1244 | } |
| 1245 | |
| 1246 | list_add_tail(&elt->list, &ps->postfix); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static int postfix_append_op(struct filter_parse_state *ps, int op) |
| 1252 | { |
| 1253 | struct postfix_elt *elt; |
| 1254 | |
| 1255 | elt = kmalloc(sizeof(*elt), GFP_KERNEL); |
| 1256 | if (!elt) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | elt->op = op; |
| 1260 | elt->operand = NULL; |
| 1261 | |
| 1262 | list_add_tail(&elt->list, &ps->postfix); |
| 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static void postfix_clear(struct filter_parse_state *ps) |
| 1268 | { |
| 1269 | struct postfix_elt *elt; |
| 1270 | |
| 1271 | while (!list_empty(&ps->postfix)) { |
| 1272 | elt = list_first_entry(&ps->postfix, struct postfix_elt, list); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1273 | list_del(&elt->list); |
Li Zefan | 8ad8073 | 2009-10-13 09:28:57 +0800 | [diff] [blame] | 1274 | kfree(elt->operand); |
| 1275 | kfree(elt); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | static int filter_parse(struct filter_parse_state *ps) |
| 1280 | { |
Frederic Weisbecker | 5928c3c | 2009-05-03 03:03:57 +0200 | [diff] [blame] | 1281 | int in_string = 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1282 | int op, top_op; |
| 1283 | char ch; |
| 1284 | |
| 1285 | while ((ch = infix_next(ps))) { |
Frederic Weisbecker | 5928c3c | 2009-05-03 03:03:57 +0200 | [diff] [blame] | 1286 | if (ch == '"') { |
| 1287 | in_string ^= 1; |
| 1288 | continue; |
| 1289 | } |
| 1290 | |
| 1291 | if (in_string) |
| 1292 | goto parse_operand; |
| 1293 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1294 | if (isspace(ch)) |
| 1295 | continue; |
| 1296 | |
| 1297 | if (is_op_char(ps, ch)) { |
| 1298 | op = infix_get_op(ps, ch); |
| 1299 | if (op == OP_NONE) { |
| 1300 | parse_error(ps, FILT_ERR_INVALID_OP, 0); |
| 1301 | return -EINVAL; |
| 1302 | } |
| 1303 | |
| 1304 | if (strlen(curr_operand(ps))) { |
| 1305 | postfix_append_operand(ps, curr_operand(ps)); |
| 1306 | clear_operand_string(ps); |
| 1307 | } |
| 1308 | |
| 1309 | while (!filter_opstack_empty(ps)) { |
| 1310 | top_op = filter_opstack_top(ps); |
| 1311 | if (!is_precedence_lower(ps, top_op, op)) { |
| 1312 | top_op = filter_opstack_pop(ps); |
| 1313 | postfix_append_op(ps, top_op); |
| 1314 | continue; |
| 1315 | } |
| 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | filter_opstack_push(ps, op); |
| 1320 | continue; |
| 1321 | } |
| 1322 | |
| 1323 | if (ch == '(') { |
| 1324 | filter_opstack_push(ps, OP_OPEN_PAREN); |
| 1325 | continue; |
| 1326 | } |
| 1327 | |
| 1328 | if (ch == ')') { |
| 1329 | if (strlen(curr_operand(ps))) { |
| 1330 | postfix_append_operand(ps, curr_operand(ps)); |
| 1331 | clear_operand_string(ps); |
| 1332 | } |
| 1333 | |
| 1334 | top_op = filter_opstack_pop(ps); |
| 1335 | while (top_op != OP_NONE) { |
| 1336 | if (top_op == OP_OPEN_PAREN) |
| 1337 | break; |
| 1338 | postfix_append_op(ps, top_op); |
| 1339 | top_op = filter_opstack_pop(ps); |
| 1340 | } |
| 1341 | if (top_op == OP_NONE) { |
| 1342 | parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0); |
| 1343 | return -EINVAL; |
| 1344 | } |
| 1345 | continue; |
| 1346 | } |
Frederic Weisbecker | 5928c3c | 2009-05-03 03:03:57 +0200 | [diff] [blame] | 1347 | parse_operand: |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1348 | if (append_operand_char(ps, ch)) { |
| 1349 | parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0); |
| 1350 | return -EINVAL; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if (strlen(curr_operand(ps))) |
| 1355 | postfix_append_operand(ps, curr_operand(ps)); |
| 1356 | |
| 1357 | while (!filter_opstack_empty(ps)) { |
| 1358 | top_op = filter_opstack_pop(ps); |
| 1359 | if (top_op == OP_NONE) |
| 1360 | break; |
| 1361 | if (top_op == OP_OPEN_PAREN) { |
| 1362 | parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0); |
| 1363 | return -EINVAL; |
| 1364 | } |
| 1365 | postfix_append_op(ps, top_op); |
| 1366 | } |
| 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1371 | static struct filter_pred *create_pred(struct filter_parse_state *ps, |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1372 | struct trace_event_call *call, |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1373 | int op, char *operand1, char *operand2) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1374 | { |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1375 | struct ftrace_event_field *field; |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1376 | static struct filter_pred pred; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1377 | |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1378 | memset(&pred, 0, sizeof(pred)); |
| 1379 | pred.op = op; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1380 | |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1381 | if (op == OP_AND || op == OP_OR) |
| 1382 | return &pred; |
| 1383 | |
| 1384 | if (!operand1 || !operand2) { |
| 1385 | parse_error(ps, FILT_ERR_MISSING_FIELD, 0); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1386 | return NULL; |
| 1387 | } |
| 1388 | |
zhangwei(Jovi) | b3a8c6f | 2013-03-11 15:13:42 +0800 | [diff] [blame] | 1389 | field = trace_find_event_field(call, operand1); |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1390 | if (!field) { |
| 1391 | parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1392 | return NULL; |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1393 | } |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1394 | |
Jiri Olsa | 81570d9 | 2011-08-11 16:25:45 +0200 | [diff] [blame] | 1395 | strcpy(pred.regex.pattern, operand2); |
| 1396 | pred.regex.len = strlen(pred.regex.pattern); |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 1397 | pred.field = field; |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1398 | return init_pred(ps, field, &pred) ? NULL : &pred; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | static int check_preds(struct filter_parse_state *ps) |
| 1402 | { |
| 1403 | int n_normal_preds = 0, n_logical_preds = 0; |
| 1404 | struct postfix_elt *elt; |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1405 | int cnt = 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1406 | |
| 1407 | list_for_each_entry(elt, &ps->postfix, list) { |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1408 | if (elt->op == OP_NONE) { |
| 1409 | cnt++; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1410 | continue; |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1411 | } |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1412 | |
| 1413 | if (elt->op == OP_AND || elt->op == OP_OR) { |
| 1414 | n_logical_preds++; |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1415 | cnt--; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1416 | continue; |
| 1417 | } |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1418 | if (elt->op != OP_NOT) |
| 1419 | cnt--; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1420 | n_normal_preds++; |
Steven Rostedt (Red Hat) | b4875bb | 2015-06-25 18:02:29 -0400 | [diff] [blame] | 1421 | /* all ops should have operands */ |
| 1422 | if (cnt < 0) |
| 1423 | break; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1424 | } |
| 1425 | |
Steven Rostedt | 2cf30dc | 2015-06-15 17:50:25 -0400 | [diff] [blame] | 1426 | if (cnt != 1 || !n_normal_preds || n_logical_preds >= n_normal_preds) { |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1427 | parse_error(ps, FILT_ERR_INVALID_FILTER, 0); |
Li Zefan | bcabd91 | 2009-04-11 15:52:35 +0800 | [diff] [blame] | 1428 | return -EINVAL; |
| 1429 | } |
| 1430 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
Li Zefan | f66578a | 2009-04-21 17:12:11 +0800 | [diff] [blame] | 1433 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1434 | static int count_preds(struct filter_parse_state *ps) |
| 1435 | { |
| 1436 | struct postfix_elt *elt; |
| 1437 | int n_preds = 0; |
| 1438 | |
| 1439 | list_for_each_entry(elt, &ps->postfix, list) { |
| 1440 | if (elt->op == OP_NONE) |
| 1441 | continue; |
| 1442 | n_preds++; |
| 1443 | } |
| 1444 | |
| 1445 | return n_preds; |
| 1446 | } |
| 1447 | |
Jiri Olsa | f03f597 | 2011-08-11 16:25:49 +0200 | [diff] [blame] | 1448 | struct check_pred_data { |
| 1449 | int count; |
| 1450 | int max; |
| 1451 | }; |
| 1452 | |
| 1453 | static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred, |
| 1454 | int *err, void *data) |
| 1455 | { |
| 1456 | struct check_pred_data *d = data; |
| 1457 | |
| 1458 | if (WARN_ON(d->count++ > d->max)) { |
| 1459 | *err = -EINVAL; |
| 1460 | return WALK_PRED_ABORT; |
| 1461 | } |
| 1462 | return WALK_PRED_DEFAULT; |
| 1463 | } |
| 1464 | |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1465 | /* |
| 1466 | * The tree is walked at filtering of an event. If the tree is not correctly |
| 1467 | * built, it may cause an infinite loop. Check here that the tree does |
| 1468 | * indeed terminate. |
| 1469 | */ |
| 1470 | static int check_pred_tree(struct event_filter *filter, |
| 1471 | struct filter_pred *root) |
| 1472 | { |
Jiri Olsa | f03f597 | 2011-08-11 16:25:49 +0200 | [diff] [blame] | 1473 | struct check_pred_data data = { |
| 1474 | /* |
| 1475 | * The max that we can hit a node is three times. |
| 1476 | * Once going down, once coming up from left, and |
| 1477 | * once coming up from right. This is more than enough |
| 1478 | * since leafs are only hit a single time. |
| 1479 | */ |
| 1480 | .max = 3 * filter->n_preds, |
| 1481 | .count = 0, |
| 1482 | }; |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1483 | |
Jiri Olsa | f03f597 | 2011-08-11 16:25:49 +0200 | [diff] [blame] | 1484 | return walk_pred_tree(filter->preds, root, |
| 1485 | check_pred_tree_cb, &data); |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
Jiri Olsa | c00b060 | 2011-08-11 16:25:50 +0200 | [diff] [blame] | 1488 | static int count_leafs_cb(enum move_type move, struct filter_pred *pred, |
| 1489 | int *err, void *data) |
| 1490 | { |
| 1491 | int *count = data; |
| 1492 | |
| 1493 | if ((move == MOVE_DOWN) && |
| 1494 | (pred->left == FILTER_PRED_INVALID)) |
| 1495 | (*count)++; |
| 1496 | |
| 1497 | return WALK_PRED_DEFAULT; |
| 1498 | } |
| 1499 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1500 | static int count_leafs(struct filter_pred *preds, struct filter_pred *root) |
| 1501 | { |
Jiri Olsa | c00b060 | 2011-08-11 16:25:50 +0200 | [diff] [blame] | 1502 | int count = 0, ret; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1503 | |
Jiri Olsa | c00b060 | 2011-08-11 16:25:50 +0200 | [diff] [blame] | 1504 | ret = walk_pred_tree(preds, root, count_leafs_cb, &count); |
| 1505 | WARN_ON(ret); |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1506 | return count; |
| 1507 | } |
| 1508 | |
Jiri Olsa | 96bc293 | 2011-08-11 16:25:52 +0200 | [diff] [blame] | 1509 | struct fold_pred_data { |
| 1510 | struct filter_pred *root; |
| 1511 | int count; |
| 1512 | int children; |
| 1513 | }; |
| 1514 | |
| 1515 | static int fold_pred_cb(enum move_type move, struct filter_pred *pred, |
| 1516 | int *err, void *data) |
| 1517 | { |
| 1518 | struct fold_pred_data *d = data; |
| 1519 | struct filter_pred *root = d->root; |
| 1520 | |
| 1521 | if (move != MOVE_DOWN) |
| 1522 | return WALK_PRED_DEFAULT; |
| 1523 | if (pred->left != FILTER_PRED_INVALID) |
| 1524 | return WALK_PRED_DEFAULT; |
| 1525 | |
| 1526 | if (WARN_ON(d->count == d->children)) { |
| 1527 | *err = -EINVAL; |
| 1528 | return WALK_PRED_ABORT; |
| 1529 | } |
| 1530 | |
| 1531 | pred->index &= ~FILTER_PRED_FOLD; |
| 1532 | root->ops[d->count++] = pred->index; |
| 1533 | return WALK_PRED_DEFAULT; |
| 1534 | } |
| 1535 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1536 | static int fold_pred(struct filter_pred *preds, struct filter_pred *root) |
| 1537 | { |
Jiri Olsa | 96bc293 | 2011-08-11 16:25:52 +0200 | [diff] [blame] | 1538 | struct fold_pred_data data = { |
| 1539 | .root = root, |
| 1540 | .count = 0, |
| 1541 | }; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1542 | int children; |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1543 | |
| 1544 | /* No need to keep the fold flag */ |
| 1545 | root->index &= ~FILTER_PRED_FOLD; |
| 1546 | |
| 1547 | /* If the root is a leaf then do nothing */ |
| 1548 | if (root->left == FILTER_PRED_INVALID) |
| 1549 | return 0; |
| 1550 | |
| 1551 | /* count the children */ |
| 1552 | children = count_leafs(preds, &preds[root->left]); |
| 1553 | children += count_leafs(preds, &preds[root->right]); |
| 1554 | |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1555 | root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL); |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1556 | if (!root->ops) |
| 1557 | return -ENOMEM; |
| 1558 | |
| 1559 | root->val = children; |
Jiri Olsa | 96bc293 | 2011-08-11 16:25:52 +0200 | [diff] [blame] | 1560 | data.children = children; |
| 1561 | return walk_pred_tree(preds, root, fold_pred_cb, &data); |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1562 | } |
| 1563 | |
Jiri Olsa | 1b797fe | 2011-08-11 16:25:51 +0200 | [diff] [blame] | 1564 | static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred, |
| 1565 | int *err, void *data) |
| 1566 | { |
| 1567 | struct filter_pred *preds = data; |
| 1568 | |
| 1569 | if (move != MOVE_DOWN) |
| 1570 | return WALK_PRED_DEFAULT; |
| 1571 | if (!(pred->index & FILTER_PRED_FOLD)) |
| 1572 | return WALK_PRED_DEFAULT; |
| 1573 | |
| 1574 | *err = fold_pred(preds, pred); |
| 1575 | if (*err) |
| 1576 | return WALK_PRED_ABORT; |
| 1577 | |
| 1578 | /* eveyrhing below is folded, continue with parent */ |
| 1579 | return WALK_PRED_PARENT; |
| 1580 | } |
| 1581 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1582 | /* |
| 1583 | * To optimize the processing of the ops, if we have several "ors" or |
| 1584 | * "ands" together, we can put them in an array and process them all |
| 1585 | * together speeding up the filter logic. |
| 1586 | */ |
| 1587 | static int fold_pred_tree(struct event_filter *filter, |
| 1588 | struct filter_pred *root) |
| 1589 | { |
Jiri Olsa | 1b797fe | 2011-08-11 16:25:51 +0200 | [diff] [blame] | 1590 | return walk_pred_tree(filter->preds, root, fold_pred_tree_cb, |
| 1591 | filter->preds); |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1592 | } |
| 1593 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1594 | static int replace_preds(struct trace_event_call *call, |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 1595 | struct event_filter *filter, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1596 | struct filter_parse_state *ps, |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1597 | bool dry_run) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1598 | { |
| 1599 | char *operand1 = NULL, *operand2 = NULL; |
| 1600 | struct filter_pred *pred; |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1601 | struct filter_pred *root; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1602 | struct postfix_elt *elt; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1603 | struct pred_stack stack = { }; /* init to NULL */ |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1604 | int err; |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1605 | int n_preds = 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1606 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1607 | n_preds = count_preds(ps); |
| 1608 | if (n_preds >= MAX_FILTER_PRED) { |
| 1609 | parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); |
| 1610 | return -ENOSPC; |
| 1611 | } |
| 1612 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1613 | err = check_preds(ps); |
| 1614 | if (err) |
| 1615 | return err; |
| 1616 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1617 | if (!dry_run) { |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1618 | err = __alloc_pred_stack(&stack, n_preds); |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1619 | if (err) |
| 1620 | return err; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1621 | err = __alloc_preds(filter, n_preds); |
| 1622 | if (err) |
| 1623 | goto fail; |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | n_preds = 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1627 | list_for_each_entry(elt, &ps->postfix, list) { |
| 1628 | if (elt->op == OP_NONE) { |
| 1629 | if (!operand1) |
| 1630 | operand1 = elt->operand; |
| 1631 | else if (!operand2) |
| 1632 | operand2 = elt->operand; |
| 1633 | else { |
| 1634 | parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0); |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1635 | err = -EINVAL; |
| 1636 | goto fail; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1637 | } |
| 1638 | continue; |
| 1639 | } |
| 1640 | |
Steven Rostedt (Red Hat) | e12c09c | 2014-12-02 11:55:36 -0500 | [diff] [blame] | 1641 | if (elt->op == OP_NOT) { |
| 1642 | if (!n_preds || operand1 || operand2) { |
| 1643 | parse_error(ps, FILT_ERR_ILLEGAL_NOT_OP, 0); |
| 1644 | err = -EINVAL; |
| 1645 | goto fail; |
| 1646 | } |
| 1647 | if (!dry_run) |
| 1648 | filter->preds[n_preds - 1].not ^= 1; |
| 1649 | continue; |
| 1650 | } |
| 1651 | |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 1652 | if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) { |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1653 | parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1654 | err = -ENOSPC; |
| 1655 | goto fail; |
Li Zefan | 1f9963c | 2009-07-20 10:20:53 +0800 | [diff] [blame] | 1656 | } |
| 1657 | |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1658 | pred = create_pred(ps, call, elt->op, operand1, operand2); |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1659 | if (!pred) { |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1660 | err = -EINVAL; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1661 | goto fail; |
| 1662 | } |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1663 | |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1664 | if (!dry_run) { |
| 1665 | err = filter_add_pred(ps, filter, pred, &stack); |
Jiri Olsa | 61aaef55 | 2011-08-11 16:25:47 +0200 | [diff] [blame] | 1666 | if (err) |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1667 | goto fail; |
Jiri Olsa | 9d96cd1 | 2011-08-11 16:25:46 +0200 | [diff] [blame] | 1668 | } |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1669 | |
| 1670 | operand1 = operand2 = NULL; |
| 1671 | } |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1672 | |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1673 | if (!dry_run) { |
| 1674 | /* We should have one item left on the stack */ |
| 1675 | pred = __pop_pred_stack(&stack); |
| 1676 | if (!pred) |
| 1677 | return -EINVAL; |
| 1678 | /* This item is where we start from in matching */ |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1679 | root = pred; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1680 | /* Make sure the stack is empty */ |
| 1681 | pred = __pop_pred_stack(&stack); |
| 1682 | if (WARN_ON(pred)) { |
| 1683 | err = -EINVAL; |
| 1684 | filter->root = NULL; |
| 1685 | goto fail; |
| 1686 | } |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1687 | err = check_pred_tree(filter, root); |
| 1688 | if (err) |
| 1689 | goto fail; |
| 1690 | |
Steven Rostedt | 43cd414 | 2011-01-27 23:16:51 -0500 | [diff] [blame] | 1691 | /* Optimize the tree */ |
| 1692 | err = fold_pred_tree(filter, root); |
| 1693 | if (err) |
| 1694 | goto fail; |
| 1695 | |
Steven Rostedt | ec126ca | 2011-01-27 23:14:25 -0500 | [diff] [blame] | 1696 | /* We don't set root until we know it works */ |
| 1697 | barrier(); |
| 1698 | filter->root = root; |
Steven Rostedt | 61e9dea | 2011-01-27 22:54:33 -0500 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | err = 0; |
| 1702 | fail: |
| 1703 | __free_pred_stack(&stack); |
| 1704 | return err; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 1705 | } |
| 1706 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1707 | static inline void event_set_filtered_flag(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1708 | { |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 1709 | unsigned long old_flags = file->flags; |
| 1710 | |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 1711 | file->flags |= EVENT_FILE_FL_FILTERED; |
Steven Rostedt (Red Hat) | 0fc1b09 | 2016-05-03 17:15:43 -0400 | [diff] [blame] | 1712 | |
| 1713 | if (old_flags != file->flags) |
| 1714 | trace_buffered_event_enable(); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1715 | } |
| 1716 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1717 | static inline void event_set_filter(struct trace_event_file *file, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1718 | struct event_filter *filter) |
| 1719 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 1720 | rcu_assign_pointer(file->filter, filter); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1721 | } |
| 1722 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1723 | static inline void event_clear_filter(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1724 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 1725 | RCU_INIT_POINTER(file->filter, NULL); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | static inline void |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1729 | event_set_no_set_filter_flag(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1730 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 1731 | file->flags |= EVENT_FILE_FL_NO_SET_FILTER; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static inline void |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1735 | event_clear_no_set_filter_flag(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1736 | { |
Steven Rostedt (Red Hat) | dcb0b55 | 2016-05-02 21:30:04 -0400 | [diff] [blame] | 1737 | file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | static inline bool |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1741 | event_no_set_filter_flag(struct trace_event_file *file) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1742 | { |
Steven Rostedt (Red Hat) | 5d6ad96 | 2015-05-13 15:12:33 -0400 | [diff] [blame] | 1743 | if (file->flags & EVENT_FILE_FL_NO_SET_FILTER) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1744 | return true; |
| 1745 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
| 1748 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1749 | struct filter_list { |
| 1750 | struct list_head list; |
| 1751 | struct event_filter *filter; |
| 1752 | }; |
| 1753 | |
Steven Rostedt (Red Hat) | 7967b3e | 2015-05-13 14:59:40 -0400 | [diff] [blame] | 1754 | static int replace_system_preds(struct trace_subsystem_dir *dir, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1755 | struct trace_array *tr, |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1756 | struct filter_parse_state *ps, |
| 1757 | char *filter_string) |
| 1758 | { |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1759 | struct trace_event_file *file; |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1760 | struct filter_list *filter_item; |
| 1761 | struct filter_list *tmp; |
| 1762 | LIST_HEAD(filter_list); |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1763 | bool fail = true; |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 1764 | int err; |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1765 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1766 | list_for_each_entry(file, &tr->events, list) { |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 1767 | if (file->system != dir) |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1768 | continue; |
| 1769 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1770 | /* |
| 1771 | * Try to see if the filter can be applied |
| 1772 | * (filter arg is ignored on dry_run) |
| 1773 | */ |
Oleg Nesterov | 6355d54 | 2014-07-15 20:48:32 +0200 | [diff] [blame] | 1774 | err = replace_preds(file->event_call, NULL, ps, true); |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1775 | if (err) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1776 | event_set_no_set_filter_flag(file); |
Li Zefan | ed0449a | 2011-11-01 09:09:35 +0800 | [diff] [blame] | 1777 | else |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1778 | event_clear_no_set_filter_flag(file); |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1779 | } |
| 1780 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1781 | list_for_each_entry(file, &tr->events, list) { |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1782 | struct event_filter *filter; |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1783 | |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 1784 | if (file->system != dir) |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1785 | continue; |
| 1786 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1787 | if (event_no_set_filter_flag(file)) |
Li Zefan | ed0449a | 2011-11-01 09:09:35 +0800 | [diff] [blame] | 1788 | continue; |
| 1789 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1790 | filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL); |
| 1791 | if (!filter_item) |
| 1792 | goto fail_mem; |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1793 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1794 | list_add_tail(&filter_item->list, &filter_list); |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1795 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1796 | filter_item->filter = __alloc_filter(); |
| 1797 | if (!filter_item->filter) |
| 1798 | goto fail_mem; |
| 1799 | filter = filter_item->filter; |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1800 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1801 | /* Can only fail on no memory */ |
| 1802 | err = replace_filter_string(filter, filter_string); |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1803 | if (err) |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1804 | goto fail_mem; |
| 1805 | |
Oleg Nesterov | 6355d54 | 2014-07-15 20:48:32 +0200 | [diff] [blame] | 1806 | err = replace_preds(file->event_call, filter, ps, false); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1807 | if (err) { |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1808 | filter_disable(file); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1809 | parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0); |
| 1810 | append_filter_err(ps, filter); |
| 1811 | } else |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1812 | event_set_filtered_flag(file); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1813 | /* |
| 1814 | * Regardless of if this returned an error, we still |
| 1815 | * replace the filter for the call. |
| 1816 | */ |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1817 | filter = event_filter(file); |
| 1818 | event_set_filter(file, filter_item->filter); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1819 | filter_item->filter = filter; |
| 1820 | |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1821 | fail = false; |
| 1822 | } |
| 1823 | |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1824 | if (fail) |
| 1825 | goto fail; |
| 1826 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1827 | /* |
| 1828 | * The calls can still be using the old filters. |
| 1829 | * Do a synchronize_sched() to ensure all calls are |
| 1830 | * done with them before we free them. |
| 1831 | */ |
| 1832 | synchronize_sched(); |
| 1833 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { |
| 1834 | __free_filter(filter_item->filter); |
| 1835 | list_del(&filter_item->list); |
| 1836 | kfree(filter_item); |
| 1837 | } |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1838 | return 0; |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1839 | fail: |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1840 | /* No call succeeded */ |
| 1841 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { |
| 1842 | list_del(&filter_item->list); |
| 1843 | kfree(filter_item); |
| 1844 | } |
Steven Rostedt | 0fc3ca9 | 2011-01-27 22:46:46 -0500 | [diff] [blame] | 1845 | parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0); |
| 1846 | return -EINVAL; |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1847 | fail_mem: |
| 1848 | /* If any call succeeded, we still need to sync */ |
| 1849 | if (!fail) |
| 1850 | synchronize_sched(); |
| 1851 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { |
| 1852 | __free_filter(filter_item->filter); |
| 1853 | list_del(&filter_item->list); |
| 1854 | kfree(filter_item); |
| 1855 | } |
| 1856 | return -ENOMEM; |
Li Zefan | fce29d1 | 2009-10-15 11:20:34 +0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1859 | static int create_filter_start(char *filter_str, bool set_str, |
| 1860 | struct filter_parse_state **psp, |
| 1861 | struct event_filter **filterp) |
| 1862 | { |
| 1863 | struct event_filter *filter; |
| 1864 | struct filter_parse_state *ps = NULL; |
| 1865 | int err = 0; |
| 1866 | |
| 1867 | WARN_ON_ONCE(*psp || *filterp); |
| 1868 | |
| 1869 | /* allocate everything, and if any fails, free all and fail */ |
| 1870 | filter = __alloc_filter(); |
| 1871 | if (filter && set_str) |
| 1872 | err = replace_filter_string(filter, filter_str); |
| 1873 | |
| 1874 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); |
| 1875 | |
| 1876 | if (!filter || !ps || err) { |
| 1877 | kfree(ps); |
| 1878 | __free_filter(filter); |
| 1879 | return -ENOMEM; |
| 1880 | } |
| 1881 | |
| 1882 | /* we're committed to creating a new filter */ |
| 1883 | *filterp = filter; |
| 1884 | *psp = ps; |
| 1885 | |
| 1886 | parse_init(ps, filter_ops, filter_str); |
| 1887 | err = filter_parse(ps); |
| 1888 | if (err && set_str) |
| 1889 | append_filter_err(ps, filter); |
| 1890 | return err; |
| 1891 | } |
| 1892 | |
| 1893 | static void create_filter_finish(struct filter_parse_state *ps) |
| 1894 | { |
| 1895 | if (ps) { |
| 1896 | filter_opstack_clear(ps); |
| 1897 | postfix_clear(ps); |
| 1898 | kfree(ps); |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | /** |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1903 | * create_filter - create a filter for a trace_event_call |
| 1904 | * @call: trace_event_call to create a filter for |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1905 | * @filter_str: filter string |
| 1906 | * @set_str: remember @filter_str and enable detailed error in filter |
| 1907 | * @filterp: out param for created filter (always updated on return) |
| 1908 | * |
| 1909 | * Creates a filter for @call with @filter_str. If @set_str is %true, |
| 1910 | * @filter_str is copied and recorded in the new filter. |
| 1911 | * |
| 1912 | * On success, returns 0 and *@filterp points to the new filter. On |
| 1913 | * failure, returns -errno and *@filterp may point to %NULL or to a new |
| 1914 | * filter. In the latter case, the returned filter contains error |
| 1915 | * information if @set_str is %true and the caller is responsible for |
| 1916 | * freeing it. |
| 1917 | */ |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1918 | static int create_filter(struct trace_event_call *call, |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1919 | char *filter_str, bool set_str, |
| 1920 | struct event_filter **filterp) |
| 1921 | { |
| 1922 | struct event_filter *filter = NULL; |
| 1923 | struct filter_parse_state *ps = NULL; |
| 1924 | int err; |
| 1925 | |
| 1926 | err = create_filter_start(filter_str, set_str, &ps, &filter); |
| 1927 | if (!err) { |
Oleg Nesterov | 6355d54 | 2014-07-15 20:48:32 +0200 | [diff] [blame] | 1928 | err = replace_preds(call, filter, ps, false); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1929 | if (err && set_str) |
| 1930 | append_filter_err(ps, filter); |
| 1931 | } |
Steven Rostedt (VMware) | 8838cd5 | 2017-08-23 12:46:27 -0400 | [diff] [blame] | 1932 | if (err && !set_str) { |
| 1933 | free_event_filter(filter); |
| 1934 | filter = NULL; |
| 1935 | } |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1936 | create_filter_finish(ps); |
| 1937 | |
| 1938 | *filterp = filter; |
| 1939 | return err; |
| 1940 | } |
| 1941 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1942 | int create_event_filter(struct trace_event_call *call, |
Tom Zanussi | bac5fb9 | 2013-10-24 08:59:29 -0500 | [diff] [blame] | 1943 | char *filter_str, bool set_str, |
| 1944 | struct event_filter **filterp) |
| 1945 | { |
| 1946 | return create_filter(call, filter_str, set_str, filterp); |
| 1947 | } |
| 1948 | |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1949 | /** |
| 1950 | * create_system_filter - create a filter for an event_subsystem |
| 1951 | * @system: event_subsystem to create a filter for |
| 1952 | * @filter_str: filter string |
| 1953 | * @filterp: out param for created filter (always updated on return) |
| 1954 | * |
| 1955 | * Identical to create_filter() except that it creates a subsystem filter |
| 1956 | * and always remembers @filter_str. |
| 1957 | */ |
Steven Rostedt (Red Hat) | 7967b3e | 2015-05-13 14:59:40 -0400 | [diff] [blame] | 1958 | static int create_system_filter(struct trace_subsystem_dir *dir, |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1959 | struct trace_array *tr, |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1960 | char *filter_str, struct event_filter **filterp) |
| 1961 | { |
| 1962 | struct event_filter *filter = NULL; |
| 1963 | struct filter_parse_state *ps = NULL; |
| 1964 | int err; |
| 1965 | |
| 1966 | err = create_filter_start(filter_str, true, &ps, &filter); |
| 1967 | if (!err) { |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 1968 | err = replace_system_preds(dir, tr, ps, filter_str); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 1969 | if (!err) { |
| 1970 | /* System filters just show a default message */ |
| 1971 | kfree(filter->filter_string); |
| 1972 | filter->filter_string = NULL; |
| 1973 | } else { |
| 1974 | append_filter_err(ps, filter); |
| 1975 | } |
| 1976 | } |
| 1977 | create_filter_finish(ps); |
| 1978 | |
| 1979 | *filterp = filter; |
| 1980 | return err; |
| 1981 | } |
| 1982 | |
Oleg Nesterov | e2912b0 | 2013-07-26 19:25:40 +0200 | [diff] [blame] | 1983 | /* caller must hold event_mutex */ |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1984 | int apply_event_filter(struct trace_event_file *file, char *filter_string) |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1985 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1986 | struct trace_event_call *call = file->event_call; |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1987 | struct event_filter *filter; |
Oleg Nesterov | e2912b0 | 2013-07-26 19:25:40 +0200 | [diff] [blame] | 1988 | int err; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 1989 | |
| 1990 | if (!strcmp(strstrip(filter_string), "0")) { |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1991 | filter_disable(file); |
| 1992 | filter = event_filter(file); |
| 1993 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 1994 | if (!filter) |
Oleg Nesterov | e2912b0 | 2013-07-26 19:25:40 +0200 | [diff] [blame] | 1995 | return 0; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 1996 | |
| 1997 | event_clear_filter(file); |
| 1998 | |
Steven Rostedt | f76690a | 2011-01-27 22:53:06 -0500 | [diff] [blame] | 1999 | /* Make sure the filter is not being used */ |
| 2000 | synchronize_sched(); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2001 | __free_filter(filter); |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2002 | |
Oleg Nesterov | e2912b0 | 2013-07-26 19:25:40 +0200 | [diff] [blame] | 2003 | return 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2004 | } |
| 2005 | |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2006 | err = create_filter(call, filter_string, true, &filter); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2007 | |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2008 | /* |
| 2009 | * Always swap the call filter with the new filter |
| 2010 | * even if there was an error. If there was an error |
| 2011 | * in the filter, we disable the filter and show the error |
| 2012 | * string |
| 2013 | */ |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2014 | if (filter) { |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2015 | struct event_filter *tmp; |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2016 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2017 | tmp = event_filter(file); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2018 | if (!err) |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2019 | event_set_filtered_flag(file); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2020 | else |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2021 | filter_disable(file); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2022 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2023 | event_set_filter(file, filter); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2024 | |
| 2025 | if (tmp) { |
| 2026 | /* Make sure the call is done with the filter */ |
| 2027 | synchronize_sched(); |
| 2028 | __free_filter(tmp); |
| 2029 | } |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2030 | } |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2031 | |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
Steven Rostedt (Red Hat) | 7967b3e | 2015-05-13 14:59:40 -0400 | [diff] [blame] | 2035 | int apply_subsystem_event_filter(struct trace_subsystem_dir *dir, |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2036 | char *filter_string) |
| 2037 | { |
Steven Rostedt | ae63b31 | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 2038 | struct event_subsystem *system = dir->subsystem; |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 2039 | struct trace_array *tr = dir->tr; |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2040 | struct event_filter *filter; |
| 2041 | int err = 0; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2042 | |
Li Zefan | 00e9583 | 2009-06-16 16:39:41 +0800 | [diff] [blame] | 2043 | mutex_lock(&event_mutex); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2044 | |
Steven Rostedt | e9dbfae | 2011-07-05 11:36:06 -0400 | [diff] [blame] | 2045 | /* Make sure the system still has events */ |
Steven Rostedt | ae63b31 | 2012-05-03 23:09:03 -0400 | [diff] [blame] | 2046 | if (!dir->nr_events) { |
Steven Rostedt | e9dbfae | 2011-07-05 11:36:06 -0400 | [diff] [blame] | 2047 | err = -ENODEV; |
| 2048 | goto out_unlock; |
| 2049 | } |
| 2050 | |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2051 | if (!strcmp(strstrip(filter_string), "0")) { |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 2052 | filter_free_subsystem_preds(dir, tr); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2053 | remove_filter_string(system->filter); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2054 | filter = system->filter; |
| 2055 | system->filter = NULL; |
| 2056 | /* Ensure all filters are no longer used */ |
| 2057 | synchronize_sched(); |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 2058 | filter_free_subsystem_filters(dir, tr); |
Steven Rostedt | 75b8e98 | 2011-02-03 23:25:46 -0500 | [diff] [blame] | 2059 | __free_filter(filter); |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 2060 | goto out_unlock; |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2061 | } |
| 2062 | |
Oleg Nesterov | bb9ef1c | 2014-07-15 20:48:29 +0200 | [diff] [blame] | 2063 | err = create_system_filter(dir, tr, filter_string, &filter); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2064 | if (filter) { |
| 2065 | /* |
| 2066 | * No event actually uses the system filter |
| 2067 | * we can free it without synchronize_sched(). |
| 2068 | */ |
| 2069 | __free_filter(system->filter); |
| 2070 | system->filter = filter; |
| 2071 | } |
Li Zefan | 8cd995b | 2009-05-15 11:07:27 +0800 | [diff] [blame] | 2072 | out_unlock: |
Li Zefan | 00e9583 | 2009-06-16 16:39:41 +0800 | [diff] [blame] | 2073 | mutex_unlock(&event_mutex); |
Tom Zanussi | 8b37256 | 2009-04-28 03:04:59 -0500 | [diff] [blame] | 2074 | |
| 2075 | return err; |
| 2076 | } |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 2077 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 2078 | #ifdef CONFIG_PERF_EVENTS |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2079 | |
| 2080 | void ftrace_profile_free_filter(struct perf_event *event) |
| 2081 | { |
| 2082 | struct event_filter *filter = event->filter; |
| 2083 | |
| 2084 | event->filter = NULL; |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 2085 | __free_filter(filter); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2086 | } |
| 2087 | |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2088 | struct function_filter_data { |
| 2089 | struct ftrace_ops *ops; |
| 2090 | int first_filter; |
| 2091 | int first_notrace; |
| 2092 | }; |
| 2093 | |
| 2094 | #ifdef CONFIG_FUNCTION_TRACER |
| 2095 | static char ** |
| 2096 | ftrace_function_filter_re(char *buf, int len, int *count) |
| 2097 | { |
Rasmus Villemoes | 1bb5647 | 2015-06-25 15:02:25 -0700 | [diff] [blame] | 2098 | char *str, **re; |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2099 | |
| 2100 | str = kstrndup(buf, len, GFP_KERNEL); |
| 2101 | if (!str) |
| 2102 | return NULL; |
| 2103 | |
| 2104 | /* |
| 2105 | * The argv_split function takes white space |
| 2106 | * as a separator, so convert ',' into spaces. |
| 2107 | */ |
Rasmus Villemoes | 1bb5647 | 2015-06-25 15:02:25 -0700 | [diff] [blame] | 2108 | strreplace(str, ',', ' '); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2109 | |
| 2110 | re = argv_split(GFP_KERNEL, str, count); |
| 2111 | kfree(str); |
| 2112 | return re; |
| 2113 | } |
| 2114 | |
| 2115 | static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter, |
| 2116 | int reset, char *re, int len) |
| 2117 | { |
| 2118 | int ret; |
| 2119 | |
| 2120 | if (filter) |
| 2121 | ret = ftrace_set_filter(ops, re, len, reset); |
| 2122 | else |
| 2123 | ret = ftrace_set_notrace(ops, re, len, reset); |
| 2124 | |
| 2125 | return ret; |
| 2126 | } |
| 2127 | |
| 2128 | static int __ftrace_function_set_filter(int filter, char *buf, int len, |
| 2129 | struct function_filter_data *data) |
| 2130 | { |
Jiri Olsa | 92d8d4a | 2012-06-19 17:47:52 +0200 | [diff] [blame] | 2131 | int i, re_cnt, ret = -EINVAL; |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2132 | int *reset; |
| 2133 | char **re; |
| 2134 | |
| 2135 | reset = filter ? &data->first_filter : &data->first_notrace; |
| 2136 | |
| 2137 | /* |
| 2138 | * The 'ip' field could have multiple filters set, separated |
| 2139 | * either by space or comma. We first cut the filter and apply |
| 2140 | * all pieces separatelly. |
| 2141 | */ |
| 2142 | re = ftrace_function_filter_re(buf, len, &re_cnt); |
| 2143 | if (!re) |
| 2144 | return -EINVAL; |
| 2145 | |
| 2146 | for (i = 0; i < re_cnt; i++) { |
| 2147 | ret = ftrace_function_set_regexp(data->ops, filter, *reset, |
| 2148 | re[i], strlen(re[i])); |
| 2149 | if (ret) |
| 2150 | break; |
| 2151 | |
| 2152 | if (*reset) |
| 2153 | *reset = 0; |
| 2154 | } |
| 2155 | |
| 2156 | argv_free(re); |
| 2157 | return ret; |
| 2158 | } |
| 2159 | |
| 2160 | static int ftrace_function_check_pred(struct filter_pred *pred, int leaf) |
| 2161 | { |
| 2162 | struct ftrace_event_field *field = pred->field; |
| 2163 | |
| 2164 | if (leaf) { |
| 2165 | /* |
| 2166 | * Check the leaf predicate for function trace, verify: |
| 2167 | * - only '==' and '!=' is used |
| 2168 | * - the 'ip' field is used |
| 2169 | */ |
| 2170 | if ((pred->op != OP_EQ) && (pred->op != OP_NE)) |
| 2171 | return -EINVAL; |
| 2172 | |
| 2173 | if (strcmp(field->name, "ip")) |
| 2174 | return -EINVAL; |
| 2175 | } else { |
| 2176 | /* |
| 2177 | * Check the non leaf predicate for function trace, verify: |
| 2178 | * - only '||' is used |
| 2179 | */ |
| 2180 | if (pred->op != OP_OR) |
| 2181 | return -EINVAL; |
| 2182 | } |
| 2183 | |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | static int ftrace_function_set_filter_cb(enum move_type move, |
| 2188 | struct filter_pred *pred, |
| 2189 | int *err, void *data) |
| 2190 | { |
| 2191 | /* Checking the node is valid for function trace. */ |
| 2192 | if ((move != MOVE_DOWN) || |
| 2193 | (pred->left != FILTER_PRED_INVALID)) { |
| 2194 | *err = ftrace_function_check_pred(pred, 0); |
| 2195 | } else { |
| 2196 | *err = ftrace_function_check_pred(pred, 1); |
| 2197 | if (*err) |
| 2198 | return WALK_PRED_ABORT; |
| 2199 | |
| 2200 | *err = __ftrace_function_set_filter(pred->op == OP_EQ, |
| 2201 | pred->regex.pattern, |
| 2202 | pred->regex.len, |
| 2203 | data); |
| 2204 | } |
| 2205 | |
| 2206 | return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT; |
| 2207 | } |
| 2208 | |
| 2209 | static int ftrace_function_set_filter(struct perf_event *event, |
| 2210 | struct event_filter *filter) |
| 2211 | { |
| 2212 | struct function_filter_data data = { |
| 2213 | .first_filter = 1, |
| 2214 | .first_notrace = 1, |
| 2215 | .ops = &event->ftrace_ops, |
| 2216 | }; |
| 2217 | |
| 2218 | return walk_pred_tree(filter->preds, filter->root, |
| 2219 | ftrace_function_set_filter_cb, &data); |
| 2220 | } |
| 2221 | #else |
| 2222 | static int ftrace_function_set_filter(struct perf_event *event, |
| 2223 | struct event_filter *filter) |
| 2224 | { |
| 2225 | return -ENODEV; |
| 2226 | } |
| 2227 | #endif /* CONFIG_FUNCTION_TRACER */ |
| 2228 | |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2229 | int ftrace_profile_set_filter(struct perf_event *event, int event_id, |
| 2230 | char *filter_str) |
| 2231 | { |
| 2232 | int err; |
| 2233 | struct event_filter *filter; |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 2234 | struct trace_event_call *call; |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2235 | |
| 2236 | mutex_lock(&event_mutex); |
| 2237 | |
Jiri Olsa | 3f78f93 | 2011-08-11 16:25:48 +0200 | [diff] [blame] | 2238 | call = event->tp_event; |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2239 | |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 2240 | err = -EINVAL; |
Jiri Olsa | 3f78f93 | 2011-08-11 16:25:48 +0200 | [diff] [blame] | 2241 | if (!call) |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 2242 | goto out_unlock; |
| 2243 | |
| 2244 | err = -EEXIST; |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2245 | if (event->filter) |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 2246 | goto out_unlock; |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2247 | |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2248 | err = create_filter(call, filter_str, false, &filter); |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2249 | if (err) |
| 2250 | goto free_filter; |
| 2251 | |
| 2252 | if (ftrace_event_is_function(call)) |
| 2253 | err = ftrace_function_set_filter(event, filter); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2254 | else |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 2255 | event->filter = filter; |
| 2256 | |
| 2257 | free_filter: |
| 2258 | if (err || ftrace_event_is_function(call)) |
Steven Rostedt | c9c53ca | 2011-01-27 22:42:43 -0500 | [diff] [blame] | 2259 | __free_filter(filter); |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2260 | |
Ingo Molnar | a66abe7 | 2009-10-15 12:24:04 +0200 | [diff] [blame] | 2261 | out_unlock: |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2262 | mutex_unlock(&event_mutex); |
| 2263 | |
| 2264 | return err; |
| 2265 | } |
| 2266 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 2267 | #endif /* CONFIG_PERF_EVENTS */ |
Li Zefan | 6fb2915 | 2009-10-15 11:21:42 +0800 | [diff] [blame] | 2268 | |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2269 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 2270 | |
| 2271 | #include <linux/types.h> |
| 2272 | #include <linux/tracepoint.h> |
| 2273 | |
| 2274 | #define CREATE_TRACE_POINTS |
| 2275 | #include "trace_events_filter_test.h" |
| 2276 | |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2277 | #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ |
| 2278 | { \ |
| 2279 | .filter = FILTER, \ |
| 2280 | .rec = { .a = va, .b = vb, .c = vc, .d = vd, \ |
| 2281 | .e = ve, .f = vf, .g = vg, .h = vh }, \ |
| 2282 | .match = m, \ |
| 2283 | .not_visited = nvisit, \ |
| 2284 | } |
| 2285 | #define YES 1 |
| 2286 | #define NO 0 |
| 2287 | |
| 2288 | static struct test_filter_data_t { |
| 2289 | char *filter; |
Steven Rostedt (Red Hat) | a723776 | 2015-05-13 15:27:47 -0400 | [diff] [blame] | 2290 | struct trace_event_raw_ftrace_test_filter rec; |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2291 | int match; |
| 2292 | char *not_visited; |
| 2293 | } test_filter_data[] = { |
| 2294 | #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \ |
| 2295 | "e == 1 && f == 1 && g == 1 && h == 1" |
| 2296 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""), |
| 2297 | DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"), |
| 2298 | DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""), |
| 2299 | #undef FILTER |
| 2300 | #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \ |
| 2301 | "e == 1 || f == 1 || g == 1 || h == 1" |
| 2302 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), |
| 2303 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""), |
| 2304 | DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"), |
| 2305 | #undef FILTER |
| 2306 | #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \ |
| 2307 | "(e == 1 || f == 1) && (g == 1 || h == 1)" |
| 2308 | DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"), |
| 2309 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), |
| 2310 | DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"), |
| 2311 | DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"), |
| 2312 | #undef FILTER |
| 2313 | #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \ |
| 2314 | "(e == 1 && f == 1) || (g == 1 && h == 1)" |
| 2315 | DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"), |
| 2316 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""), |
| 2317 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), |
| 2318 | #undef FILTER |
| 2319 | #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \ |
| 2320 | "(e == 1 && f == 1) || (g == 1 && h == 1)" |
| 2321 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"), |
| 2322 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), |
| 2323 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""), |
| 2324 | #undef FILTER |
| 2325 | #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \ |
| 2326 | "(e == 1 || f == 1)) && (g == 1 || h == 1)" |
| 2327 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"), |
| 2328 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), |
| 2329 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"), |
| 2330 | #undef FILTER |
| 2331 | #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \ |
| 2332 | "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))" |
| 2333 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"), |
| 2334 | DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""), |
| 2335 | DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""), |
| 2336 | #undef FILTER |
| 2337 | #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \ |
| 2338 | "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))" |
| 2339 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"), |
| 2340 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), |
| 2341 | DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"), |
| 2342 | }; |
| 2343 | |
| 2344 | #undef DATA_REC |
| 2345 | #undef FILTER |
| 2346 | #undef YES |
| 2347 | #undef NO |
| 2348 | |
| 2349 | #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t)) |
| 2350 | |
| 2351 | static int test_pred_visited; |
| 2352 | |
| 2353 | static int test_pred_visited_fn(struct filter_pred *pred, void *event) |
| 2354 | { |
| 2355 | struct ftrace_event_field *field = pred->field; |
| 2356 | |
| 2357 | test_pred_visited = 1; |
| 2358 | printk(KERN_INFO "\npred visited %s\n", field->name); |
| 2359 | return 1; |
| 2360 | } |
| 2361 | |
| 2362 | static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred, |
| 2363 | int *err, void *data) |
| 2364 | { |
| 2365 | char *fields = data; |
| 2366 | |
| 2367 | if ((move == MOVE_DOWN) && |
| 2368 | (pred->left == FILTER_PRED_INVALID)) { |
| 2369 | struct ftrace_event_field *field = pred->field; |
| 2370 | |
| 2371 | if (!field) { |
| 2372 | WARN(1, "all leafs should have field defined"); |
| 2373 | return WALK_PRED_DEFAULT; |
| 2374 | } |
| 2375 | if (!strchr(fields, *field->name)) |
| 2376 | return WALK_PRED_DEFAULT; |
| 2377 | |
| 2378 | WARN_ON(!pred->fn); |
| 2379 | pred->fn = test_pred_visited_fn; |
| 2380 | } |
| 2381 | return WALK_PRED_DEFAULT; |
| 2382 | } |
| 2383 | |
| 2384 | static __init int ftrace_test_event_filter(void) |
| 2385 | { |
| 2386 | int i; |
| 2387 | |
| 2388 | printk(KERN_INFO "Testing ftrace filter: "); |
| 2389 | |
| 2390 | for (i = 0; i < DATA_CNT; i++) { |
| 2391 | struct event_filter *filter = NULL; |
| 2392 | struct test_filter_data_t *d = &test_filter_data[i]; |
| 2393 | int err; |
| 2394 | |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2395 | err = create_filter(&event_ftrace_test_filter, d->filter, |
| 2396 | false, &filter); |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2397 | if (err) { |
| 2398 | printk(KERN_INFO |
| 2399 | "Failed to get filter for '%s', err %d\n", |
| 2400 | d->filter, err); |
Tejun Heo | 38b78eb | 2011-12-15 14:31:35 -0800 | [diff] [blame] | 2401 | __free_filter(filter); |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2402 | break; |
| 2403 | } |
| 2404 | |
Steven Rostedt | 86b6ef2 | 2011-08-22 09:41:46 -0400 | [diff] [blame] | 2405 | /* |
| 2406 | * The preemption disabling is not really needed for self |
| 2407 | * tests, but the rcu dereference will complain without it. |
| 2408 | */ |
| 2409 | preempt_disable(); |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2410 | if (*d->not_visited) |
| 2411 | walk_pred_tree(filter->preds, filter->root, |
| 2412 | test_walk_pred_cb, |
| 2413 | d->not_visited); |
| 2414 | |
| 2415 | test_pred_visited = 0; |
| 2416 | err = filter_match_preds(filter, &d->rec); |
Steven Rostedt | 86b6ef2 | 2011-08-22 09:41:46 -0400 | [diff] [blame] | 2417 | preempt_enable(); |
Jiri Olsa | 1d0e78e | 2011-08-11 16:25:54 +0200 | [diff] [blame] | 2418 | |
| 2419 | __free_filter(filter); |
| 2420 | |
| 2421 | if (test_pred_visited) { |
| 2422 | printk(KERN_INFO |
| 2423 | "Failed, unwanted pred visited for filter %s\n", |
| 2424 | d->filter); |
| 2425 | break; |
| 2426 | } |
| 2427 | |
| 2428 | if (err != d->match) { |
| 2429 | printk(KERN_INFO |
| 2430 | "Failed to match filter '%s', expected %d\n", |
| 2431 | d->filter, d->match); |
| 2432 | break; |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | if (i == DATA_CNT) |
| 2437 | printk(KERN_CONT "OK\n"); |
| 2438 | |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | late_initcall(ftrace_test_event_filter); |
| 2443 | |
| 2444 | #endif /* CONFIG_FTRACE_STARTUP_TEST */ |