Commit | Line | Data |
---|---|---|
07dfc1d0 MD |
1 | /* |
2 | * lttng-filter.c | |
3 | * | |
4 | * LTTng modules filter code. | |
5 | * | |
6 | * Copyright (C) 2010-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <linux/list.h> | |
24 | #include <linux/slab.h> | |
25 | ||
26 | #include "lttng-filter.h" | |
27 | ||
28 | static const char *opnames[] = { | |
29 | [ FILTER_OP_UNKNOWN ] = "UNKNOWN", | |
30 | ||
31 | [ FILTER_OP_RETURN ] = "RETURN", | |
32 | ||
33 | /* binary */ | |
34 | [ FILTER_OP_MUL ] = "MUL", | |
35 | [ FILTER_OP_DIV ] = "DIV", | |
36 | [ FILTER_OP_MOD ] = "MOD", | |
37 | [ FILTER_OP_PLUS ] = "PLUS", | |
38 | [ FILTER_OP_MINUS ] = "MINUS", | |
39 | [ FILTER_OP_RSHIFT ] = "RSHIFT", | |
40 | [ FILTER_OP_LSHIFT ] = "LSHIFT", | |
41 | [ FILTER_OP_BIN_AND ] = "BIN_AND", | |
42 | [ FILTER_OP_BIN_OR ] = "BIN_OR", | |
43 | [ FILTER_OP_BIN_XOR ] = "BIN_XOR", | |
44 | ||
45 | /* binary comparators */ | |
46 | [ FILTER_OP_EQ ] = "EQ", | |
47 | [ FILTER_OP_NE ] = "NE", | |
48 | [ FILTER_OP_GT ] = "GT", | |
49 | [ FILTER_OP_LT ] = "LT", | |
50 | [ FILTER_OP_GE ] = "GE", | |
51 | [ FILTER_OP_LE ] = "LE", | |
52 | ||
53 | /* string binary comparators */ | |
54 | [ FILTER_OP_EQ_STRING ] = "EQ_STRING", | |
55 | [ FILTER_OP_NE_STRING ] = "NE_STRING", | |
56 | [ FILTER_OP_GT_STRING ] = "GT_STRING", | |
57 | [ FILTER_OP_LT_STRING ] = "LT_STRING", | |
58 | [ FILTER_OP_GE_STRING ] = "GE_STRING", | |
59 | [ FILTER_OP_LE_STRING ] = "LE_STRING", | |
60 | ||
61 | /* s64 binary comparators */ | |
62 | [ FILTER_OP_EQ_S64 ] = "EQ_S64", | |
63 | [ FILTER_OP_NE_S64 ] = "NE_S64", | |
64 | [ FILTER_OP_GT_S64 ] = "GT_S64", | |
65 | [ FILTER_OP_LT_S64 ] = "LT_S64", | |
66 | [ FILTER_OP_GE_S64 ] = "GE_S64", | |
67 | [ FILTER_OP_LE_S64 ] = "LE_S64", | |
68 | ||
69 | /* double binary comparators */ | |
70 | [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE", | |
71 | [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE", | |
72 | [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE", | |
73 | [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE", | |
74 | [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE", | |
75 | [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE", | |
76 | ||
77 | /* Mixed S64-double binary comparators */ | |
78 | [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64", | |
79 | [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64", | |
80 | [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64", | |
81 | [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64", | |
82 | [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64", | |
83 | [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64", | |
84 | ||
85 | [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE", | |
86 | [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE", | |
87 | [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE", | |
88 | [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE", | |
89 | [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE", | |
90 | [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE", | |
91 | ||
92 | /* unary */ | |
93 | [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS", | |
94 | [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS", | |
95 | [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT", | |
96 | [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64", | |
97 | [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64", | |
98 | [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64", | |
99 | [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE", | |
100 | [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE", | |
101 | [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE", | |
102 | ||
103 | /* logical */ | |
104 | [ FILTER_OP_AND ] = "AND", | |
105 | [ FILTER_OP_OR ] = "OR", | |
106 | ||
107 | /* load field ref */ | |
108 | [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF", | |
109 | [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING", | |
110 | [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE", | |
111 | [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64", | |
112 | [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE", | |
113 | ||
114 | /* load from immediate operand */ | |
115 | [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING", | |
116 | [ FILTER_OP_LOAD_S64 ] = "LOAD_S64", | |
117 | [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE", | |
118 | ||
119 | /* cast */ | |
120 | [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64", | |
121 | [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64", | |
122 | [ FILTER_OP_CAST_NOP ] = "CAST_NOP", | |
123 | ||
124 | /* get context ref */ | |
125 | [ FILTER_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF", | |
126 | [ FILTER_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING", | |
127 | [ FILTER_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64", | |
128 | [ FILTER_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE", | |
129 | }; | |
130 | ||
131 | const char *lttng_filter_print_op(enum filter_op op) | |
132 | { | |
133 | if (op >= NR_FILTER_OPS) | |
134 | return "UNKNOWN"; | |
135 | else | |
136 | return opnames[op]; | |
137 | } | |
138 | ||
139 | static | |
140 | int apply_field_reloc(struct lttng_event *event, | |
141 | struct bytecode_runtime *runtime, | |
142 | uint32_t runtime_len, | |
143 | uint32_t reloc_offset, | |
144 | const char *field_name) | |
145 | { | |
146 | const struct lttng_event_desc *desc; | |
147 | const struct lttng_event_field *fields, *field = NULL; | |
148 | unsigned int nr_fields, i; | |
149 | struct field_ref *field_ref; | |
150 | struct load_op *op; | |
151 | uint32_t field_offset = 0; | |
152 | ||
153 | dbg_printk("Apply field reloc: %u %s\n", reloc_offset, field_name); | |
154 | ||
155 | /* Lookup event by name */ | |
156 | desc = event->desc; | |
157 | if (!desc) | |
158 | return -EINVAL; | |
159 | fields = desc->fields; | |
160 | if (!fields) | |
161 | return -EINVAL; | |
162 | nr_fields = desc->nr_fields; | |
163 | for (i = 0; i < nr_fields; i++) { | |
164 | if (!strcmp(fields[i].name, field_name)) { | |
165 | field = &fields[i]; | |
166 | break; | |
167 | } | |
168 | /* compute field offset */ | |
169 | switch (fields[i].type.atype) { | |
170 | case atype_integer: | |
171 | case atype_enum: | |
172 | field_offset += sizeof(int64_t); | |
173 | break; | |
174 | case atype_array: | |
175 | case atype_sequence: | |
176 | field_offset += sizeof(unsigned long); | |
177 | field_offset += sizeof(void *); | |
178 | break; | |
179 | case atype_string: | |
180 | field_offset += sizeof(void *); | |
181 | break; | |
182 | default: | |
183 | return -EINVAL; | |
184 | } | |
185 | } | |
186 | if (!field) | |
187 | return -EINVAL; | |
188 | ||
189 | /* Check if field offset is too large for 16-bit offset */ | |
190 | if (field_offset > FILTER_BYTECODE_MAX_LEN - 1) | |
191 | return -EINVAL; | |
192 | ||
193 | /* set type */ | |
194 | op = (struct load_op *) &runtime->data[reloc_offset]; | |
195 | field_ref = (struct field_ref *) op->data; | |
196 | switch (field->type.atype) { | |
197 | case atype_integer: | |
198 | case atype_enum: | |
199 | op->op = FILTER_OP_LOAD_FIELD_REF_S64; | |
200 | break; | |
201 | case atype_array: | |
202 | case atype_sequence: | |
203 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; | |
204 | break; | |
205 | case atype_string: | |
206 | op->op = FILTER_OP_LOAD_FIELD_REF_STRING; | |
207 | break; | |
208 | default: | |
209 | return -EINVAL; | |
210 | } | |
211 | /* set offset */ | |
212 | field_ref->offset = (uint16_t) field_offset; | |
213 | return 0; | |
214 | } | |
215 | ||
216 | static | |
217 | int apply_context_reloc(struct lttng_event *event, | |
218 | struct bytecode_runtime *runtime, | |
219 | uint32_t runtime_len, | |
220 | uint32_t reloc_offset, | |
221 | const char *context_name) | |
222 | { | |
223 | struct field_ref *field_ref; | |
224 | struct load_op *op; | |
225 | struct lttng_ctx_field *ctx_field; | |
226 | int idx; | |
227 | ||
228 | dbg_printk("Apply context reloc: %u %s\n", reloc_offset, context_name); | |
229 | ||
230 | /* Get context index */ | |
231 | idx = lttng_get_context_index(lttng_static_ctx, context_name); | |
232 | if (idx < 0) | |
233 | return -ENOENT; | |
234 | ||
235 | /* Check if idx is too large for 16-bit offset */ | |
236 | if (idx > FILTER_BYTECODE_MAX_LEN - 1) | |
237 | return -EINVAL; | |
238 | ||
239 | /* Get context return type */ | |
240 | ctx_field = <tng_static_ctx->fields[idx]; | |
241 | op = (struct load_op *) &runtime->data[reloc_offset]; | |
242 | field_ref = (struct field_ref *) op->data; | |
243 | switch (ctx_field->event_field.type.atype) { | |
244 | case atype_integer: | |
245 | case atype_enum: | |
246 | op->op = FILTER_OP_GET_CONTEXT_REF_S64; | |
247 | break; | |
248 | /* Sequence and array supported as string */ | |
249 | case atype_string: | |
250 | case atype_array: | |
251 | case atype_sequence: | |
252 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; | |
253 | break; | |
254 | default: | |
255 | return -EINVAL; | |
256 | } | |
257 | /* set offset to context index within channel contexts */ | |
258 | field_ref->offset = (uint16_t) idx; | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static | |
263 | int apply_reloc(struct lttng_event *event, | |
264 | struct bytecode_runtime *runtime, | |
265 | uint32_t runtime_len, | |
266 | uint32_t reloc_offset, | |
267 | const char *name) | |
268 | { | |
269 | struct load_op *op; | |
270 | ||
271 | dbg_printk("Apply reloc: %u %s\n", reloc_offset, name); | |
272 | ||
273 | /* Ensure that the reloc is within the code */ | |
274 | if (runtime_len - reloc_offset < sizeof(uint16_t)) | |
275 | return -EINVAL; | |
276 | ||
277 | op = (struct load_op *) &runtime->data[reloc_offset]; | |
278 | switch (op->op) { | |
279 | case FILTER_OP_LOAD_FIELD_REF: | |
280 | return apply_field_reloc(event, runtime, runtime_len, | |
281 | reloc_offset, name); | |
282 | case FILTER_OP_GET_CONTEXT_REF: | |
283 | return apply_context_reloc(event, runtime, runtime_len, | |
284 | reloc_offset, name); | |
285 | default: | |
286 | printk(KERN_WARNING "Unknown reloc op type %u\n", op->op); | |
287 | return -EINVAL; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | static | |
293 | int bytecode_is_linked(struct lttng_filter_bytecode_node *filter_bytecode, | |
294 | struct lttng_event *event) | |
295 | { | |
296 | struct lttng_bytecode_runtime *bc_runtime; | |
297 | ||
298 | list_for_each_entry(bc_runtime, | |
299 | &event->bytecode_runtime_head, node) { | |
300 | if (bc_runtime->bc == filter_bytecode) | |
301 | return 1; | |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
306 | /* | |
307 | * Take a bytecode with reloc table and link it to an event to create a | |
308 | * bytecode runtime. | |
309 | */ | |
310 | static | |
311 | int _lttng_filter_event_link_bytecode(struct lttng_event *event, | |
312 | struct lttng_filter_bytecode_node *filter_bytecode, | |
313 | struct list_head *insert_loc) | |
314 | { | |
315 | int ret, offset, next_offset; | |
316 | struct bytecode_runtime *runtime = NULL; | |
317 | size_t runtime_alloc_len; | |
318 | ||
319 | if (!filter_bytecode) | |
320 | return 0; | |
321 | /* Bytecode already linked */ | |
322 | if (bytecode_is_linked(filter_bytecode, event)) | |
323 | return 0; | |
324 | ||
325 | dbg_printk("Linking...\n"); | |
326 | ||
327 | /* We don't need the reloc table in the runtime */ | |
328 | runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset; | |
329 | runtime = kzalloc(runtime_alloc_len, GFP_KERNEL); | |
330 | if (!runtime) { | |
331 | ret = -ENOMEM; | |
332 | goto alloc_error; | |
333 | } | |
334 | runtime->p.bc = filter_bytecode; | |
335 | runtime->len = filter_bytecode->bc.reloc_offset; | |
336 | /* copy original bytecode */ | |
337 | memcpy(runtime->data, filter_bytecode->bc.data, runtime->len); | |
338 | /* | |
339 | * apply relocs. Those are a uint16_t (offset in bytecode) | |
340 | * followed by a string (field name). | |
341 | */ | |
342 | for (offset = filter_bytecode->bc.reloc_offset; | |
343 | offset < filter_bytecode->bc.len; | |
344 | offset = next_offset) { | |
345 | uint16_t reloc_offset = | |
346 | *(uint16_t *) &filter_bytecode->bc.data[offset]; | |
347 | const char *name = | |
348 | (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)]; | |
349 | ||
350 | ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name); | |
351 | if (ret) { | |
352 | goto link_error; | |
353 | } | |
354 | next_offset = offset + sizeof(uint16_t) + strlen(name) + 1; | |
355 | } | |
356 | /* Validate bytecode */ | |
357 | ret = lttng_filter_validate_bytecode(runtime); | |
358 | if (ret) { | |
359 | goto link_error; | |
360 | } | |
361 | /* Specialize bytecode */ | |
362 | ret = lttng_filter_specialize_bytecode(runtime); | |
363 | if (ret) { | |
364 | goto link_error; | |
365 | } | |
366 | runtime->p.filter = lttng_filter_interpret_bytecode; | |
367 | runtime->p.link_failed = 0; | |
368 | list_add_rcu(&runtime->p.node, insert_loc); | |
369 | dbg_printk("Linking successful.\n"); | |
370 | return 0; | |
371 | ||
372 | link_error: | |
373 | runtime->p.filter = lttng_filter_false; | |
374 | runtime->p.link_failed = 1; | |
375 | list_add_rcu(&runtime->p.node, insert_loc); | |
376 | alloc_error: | |
377 | dbg_printk("Linking failed.\n"); | |
378 | return ret; | |
379 | } | |
380 | ||
381 | void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime) | |
382 | { | |
383 | struct lttng_filter_bytecode_node *bc = runtime->bc; | |
384 | ||
385 | if (!bc->enabler->enabled || runtime->link_failed) | |
386 | runtime->filter = lttng_filter_false; | |
387 | else | |
388 | runtime->filter = lttng_filter_interpret_bytecode; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Link bytecode for all enablers referenced by an event. | |
393 | */ | |
394 | void lttng_enabler_event_link_bytecode(struct lttng_event *event, | |
395 | struct lttng_enabler *enabler) | |
396 | { | |
397 | struct lttng_filter_bytecode_node *bc; | |
398 | struct lttng_bytecode_runtime *runtime; | |
399 | ||
400 | /* Can only be called for events with desc attached */ | |
401 | WARN_ON_ONCE(!event->desc); | |
402 | ||
403 | /* Link each bytecode. */ | |
404 | list_for_each_entry(bc, &enabler->filter_bytecode_head, node) { | |
405 | int found = 0, ret; | |
406 | struct list_head *insert_loc; | |
407 | ||
408 | list_for_each_entry(runtime, | |
409 | &event->bytecode_runtime_head, node) { | |
410 | if (runtime->bc == bc) { | |
411 | found = 1; | |
412 | break; | |
413 | } | |
414 | } | |
415 | /* Skip bytecode already linked */ | |
416 | if (found) | |
417 | continue; | |
418 | ||
419 | /* | |
420 | * Insert at specified priority (seqnum) in increasing | |
421 | * order. | |
422 | */ | |
423 | list_for_each_entry_reverse(runtime, | |
424 | &event->bytecode_runtime_head, node) { | |
425 | if (runtime->bc->bc.seqnum < bc->bc.seqnum) { | |
426 | /* insert here */ | |
427 | insert_loc = &runtime->node; | |
428 | goto add_within; | |
429 | } | |
430 | } | |
431 | /* Add to head to list */ | |
432 | insert_loc = &event->bytecode_runtime_head; | |
433 | add_within: | |
434 | dbg_printk("linking bytecode\n"); | |
435 | ret = _lttng_filter_event_link_bytecode(event, bc, | |
436 | insert_loc); | |
437 | if (ret) { | |
438 | dbg_printk("[lttng filter] warning: cannot link event bytecode\n"); | |
439 | } | |
440 | } | |
441 | } | |
442 | ||
443 | /* | |
444 | * We own the filter_bytecode if we return success. | |
445 | */ | |
446 | int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler, | |
447 | struct lttng_filter_bytecode_node *filter_bytecode) | |
448 | { | |
449 | list_add(&filter_bytecode->node, &enabler->filter_bytecode_head); | |
450 | return 0; | |
451 | } | |
452 | ||
453 | void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler) | |
454 | { | |
455 | struct lttng_filter_bytecode_node *filter_bytecode, *tmp; | |
456 | ||
457 | list_for_each_entry_safe(filter_bytecode, tmp, | |
458 | &enabler->filter_bytecode_head, node) { | |
459 | kfree(filter_bytecode); | |
460 | } | |
461 | } | |
462 | ||
463 | void lttng_free_event_filter_runtime(struct lttng_event *event) | |
464 | { | |
465 | struct bytecode_runtime *runtime, *tmp; | |
466 | ||
467 | list_for_each_entry_safe(runtime, tmp, | |
468 | &event->bytecode_runtime_head, p.node) { | |
469 | kfree(runtime); | |
470 | } | |
471 | } |