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