SoW-2019-0002: Dynamic Snapshot
[lttng-ust.git] / liblttng-ust / lttng-filter.c
CommitLineData
2d78951a
MD
1/*
2 * lttng-filter.c
3 *
4 * LTTng UST filter code.
5 *
7e50015d 6 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2d78951a 7 *
7e50015d
MD
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:
2d78951a 14 *
7e50015d
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
2d78951a 17 *
7e50015d
MD
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.
2d78951a
MD
25 */
26
3fbec7dc 27#define _LGPL_SOURCE
b4051ad8 28#include <stddef.h>
fb31eb73
FD
29#include <stdint.h>
30
f488575f 31#include <urcu/rculist.h>
fb31eb73 32
97b58163 33#include "lttng-filter.h"
d5fc3224 34#include "ust-events-internal.h"
cd54f6d9
MD
35
36static const char *opnames[] = {
37 [ FILTER_OP_UNKNOWN ] = "UNKNOWN",
38
39 [ FILTER_OP_RETURN ] = "RETURN",
40
41 /* binary */
42 [ FILTER_OP_MUL ] = "MUL",
43 [ FILTER_OP_DIV ] = "DIV",
44 [ FILTER_OP_MOD ] = "MOD",
45 [ FILTER_OP_PLUS ] = "PLUS",
46 [ FILTER_OP_MINUS ] = "MINUS",
0039e2d8
MD
47 [ FILTER_OP_BIT_RSHIFT ] = "BIT_RSHIFT",
48 [ FILTER_OP_BIT_LSHIFT ] = "BIT_LSHIFT",
47e5f13e
MD
49 [ FILTER_OP_BIT_AND ] = "BIT_AND",
50 [ FILTER_OP_BIT_OR ] = "BIT_OR",
51 [ FILTER_OP_BIT_XOR ] = "BIT_XOR",
226106c0
MD
52
53 /* binary comparators */
cd54f6d9
MD
54 [ FILTER_OP_EQ ] = "EQ",
55 [ FILTER_OP_NE ] = "NE",
56 [ FILTER_OP_GT ] = "GT",
57 [ FILTER_OP_LT ] = "LT",
58 [ FILTER_OP_GE ] = "GE",
59 [ FILTER_OP_LE ] = "LE",
60
226106c0
MD
61 /* string binary comparators */
62 [ FILTER_OP_EQ_STRING ] = "EQ_STRING",
63 [ FILTER_OP_NE_STRING ] = "NE_STRING",
64 [ FILTER_OP_GT_STRING ] = "GT_STRING",
65 [ FILTER_OP_LT_STRING ] = "LT_STRING",
66 [ FILTER_OP_GE_STRING ] = "GE_STRING",
67 [ FILTER_OP_LE_STRING ] = "LE_STRING",
68
69 /* s64 binary comparators */
70 [ FILTER_OP_EQ_S64 ] = "EQ_S64",
71 [ FILTER_OP_NE_S64 ] = "NE_S64",
72 [ FILTER_OP_GT_S64 ] = "GT_S64",
73 [ FILTER_OP_LT_S64 ] = "LT_S64",
74 [ FILTER_OP_GE_S64 ] = "GE_S64",
75 [ FILTER_OP_LE_S64 ] = "LE_S64",
76
77 /* double binary comparators */
78 [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE",
79 [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE",
80 [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE",
81 [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE",
82 [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE",
83 [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE",
84
1e5f62b4
MD
85 /* Mixed S64-double binary comparators */
86 [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64",
87 [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64",
88 [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64",
89 [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64",
90 [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64",
91 [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64",
92
93 [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE",
94 [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE",
95 [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE",
96 [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE",
97 [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE",
98 [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE",
226106c0 99
cd54f6d9
MD
100 /* unary */
101 [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS",
102 [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS",
103 [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT",
226106c0
MD
104 [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64",
105 [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64",
106 [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64",
107 [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE",
108 [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE",
109 [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE",
cd54f6d9
MD
110
111 /* logical */
112 [ FILTER_OP_AND ] = "AND",
113 [ FILTER_OP_OR ] = "OR",
114
77aa5901 115 /* load field ref */
cd54f6d9 116 [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
2f0145d1
MD
117 [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING",
118 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE",
119 [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64",
120 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE",
121
77aa5901 122 /* load from immediate operand */
cd54f6d9
MD
123 [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
124 [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
da6eed25 125 [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
49905038
MD
126
127 /* cast */
128 [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64",
129 [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64",
130 [ FILTER_OP_CAST_NOP ] = "CAST_NOP",
77aa5901
MD
131
132 /* get context ref */
133 [ FILTER_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF",
134 [ FILTER_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING",
135 [ FILTER_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64",
136 [ FILTER_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE",
f6753f2d
MD
137
138 /* load userspace field ref */
139 [ FILTER_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING",
140 [ FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE",
141
142 /*
143 * load immediate star globbing pattern (literal string)
144 * from immediate.
145 */
146 [ FILTER_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING",
147
148 /* globbing pattern binary operator: apply to */
149 [ FILTER_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING",
150 [ FILTER_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING",
47e5f13e
MD
151
152 /*
153 * Instructions for recursive traversal through composed types.
154 */
155 [ FILTER_OP_GET_CONTEXT_ROOT ] = "GET_CONTEXT_ROOT",
156 [ FILTER_OP_GET_APP_CONTEXT_ROOT ] = "GET_APP_CONTEXT_ROOT",
157 [ FILTER_OP_GET_PAYLOAD_ROOT ] = "GET_PAYLOAD_ROOT",
158
159 [ FILTER_OP_GET_SYMBOL ] = "GET_SYMBOL",
160 [ FILTER_OP_GET_SYMBOL_FIELD ] = "GET_SYMBOL_FIELD",
161 [ FILTER_OP_GET_INDEX_U16 ] = "GET_INDEX_U16",
162 [ FILTER_OP_GET_INDEX_U64 ] = "GET_INDEX_U64",
163
164 [ FILTER_OP_LOAD_FIELD ] = "LOAD_FIELD",
165 [ FILTER_OP_LOAD_FIELD_S8 ] = "LOAD_FIELD_S8",
166 [ FILTER_OP_LOAD_FIELD_S16 ] = "LOAD_FIELD_S16",
167 [ FILTER_OP_LOAD_FIELD_S32 ] = "LOAD_FIELD_S32",
168 [ FILTER_OP_LOAD_FIELD_S64 ] = "LOAD_FIELD_S64",
169 [ FILTER_OP_LOAD_FIELD_U8 ] = "LOAD_FIELD_U8",
170 [ FILTER_OP_LOAD_FIELD_U16 ] = "LOAD_FIELD_U16",
171 [ FILTER_OP_LOAD_FIELD_U32 ] = "LOAD_FIELD_U32",
172 [ FILTER_OP_LOAD_FIELD_U64 ] = "LOAD_FIELD_U64",
173 [ FILTER_OP_LOAD_FIELD_STRING ] = "LOAD_FIELD_STRING",
174 [ FILTER_OP_LOAD_FIELD_SEQUENCE ] = "LOAD_FIELD_SEQUENCE",
175 [ FILTER_OP_LOAD_FIELD_DOUBLE ] = "LOAD_FIELD_DOUBLE",
0039e2d8
MD
176
177 [ FILTER_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT",
93c591bb
MD
178
179 [ FILTER_OP_RETURN_S64 ] = "RETURN_S64",
cd54f6d9
MD
180};
181
cd54f6d9
MD
182const char *print_op(enum filter_op op)
183{
184 if (op >= NR_FILTER_OPS)
185 return "UNKNOWN";
186 else
187 return opnames[op];
188}
189
cd54f6d9 190static
d5fc3224 191int apply_field_reloc(const struct lttng_event_desc *event_desc,
cd54f6d9
MD
192 struct bytecode_runtime *runtime,
193 uint32_t runtime_len,
194 uint32_t reloc_offset,
47e5f13e
MD
195 const char *field_name,
196 enum filter_op filter_op)
cd54f6d9 197{
cd54f6d9
MD
198 const struct lttng_event_field *fields, *field = NULL;
199 unsigned int nr_fields, i;
2f0145d1 200 struct load_op *op;
cd54f6d9
MD
201 uint32_t field_offset = 0;
202
77aa5901 203 dbg_printf("Apply field reloc: %u %s\n", reloc_offset, field_name);
cd54f6d9
MD
204
205 /* Lookup event by name */
d5fc3224 206 if (!event_desc)
cd54f6d9 207 return -EINVAL;
d5fc3224 208 fields = event_desc->fields;
cd54f6d9
MD
209 if (!fields)
210 return -EINVAL;
d5fc3224 211 nr_fields = event_desc->nr_fields;
cd54f6d9
MD
212 for (i = 0; i < nr_fields; i++) {
213 if (!strcmp(fields[i].name, field_name)) {
214 field = &fields[i];
215 break;
216 }
217 /* compute field offset */
218 switch (fields[i].type.atype) {
219 case atype_integer:
220 case atype_enum:
221 field_offset += sizeof(int64_t);
222 break;
223 case atype_array:
224 case atype_sequence:
225 field_offset += sizeof(unsigned long);
226 field_offset += sizeof(void *);
227 break;
228 case atype_string:
229 field_offset += sizeof(void *);
230 break;
231 case atype_float:
232 field_offset += sizeof(double);
da6eed25 233 break;
cd54f6d9
MD
234 default:
235 return -EINVAL;
236 }
237 }
238 if (!field)
239 return -EINVAL;
240
241 /* Check if field offset is too large for 16-bit offset */
5b4839a8 242 if (field_offset > FILTER_BYTECODE_MAX_LEN - 1)
cd54f6d9
MD
243 return -EINVAL;
244
245 /* set type */
47e5f13e
MD
246 op = (struct load_op *) &runtime->code[reloc_offset];
247
248 switch (filter_op) {
249 case FILTER_OP_LOAD_FIELD_REF:
250 {
251 struct field_ref *field_ref;
252
253 field_ref = (struct field_ref *) op->data;
254 switch (field->type.atype) {
255 case atype_integer:
256 case atype_enum:
257 op->op = FILTER_OP_LOAD_FIELD_REF_S64;
258 break;
259 case atype_array:
260 case atype_sequence:
261 op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE;
262 break;
263 case atype_string:
264 op->op = FILTER_OP_LOAD_FIELD_REF_STRING;
265 break;
266 case atype_float:
267 op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE;
268 break;
269 default:
270 return -EINVAL;
271 }
272 /* set offset */
273 field_ref->offset = (uint16_t) field_offset;
da6eed25 274 break;
47e5f13e 275 }
cd54f6d9
MD
276 default:
277 return -EINVAL;
278 }
2d78951a
MD
279 return 0;
280}
281
77aa5901 282static
d5fc3224 283int apply_context_reloc(struct bytecode_runtime *runtime,
77aa5901
MD
284 uint32_t runtime_len,
285 uint32_t reloc_offset,
47e5f13e
MD
286 const char *context_name,
287 enum filter_op filter_op)
77aa5901 288{
77aa5901
MD
289 struct load_op *op;
290 struct lttng_ctx_field *ctx_field;
291 int idx;
d5fc3224 292 struct lttng_ctx *ctx = *runtime->p.ctx;
77aa5901
MD
293
294 dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name);
295
296 /* Get context index */
d5fc3224 297 idx = lttng_get_context_index(ctx, context_name);
53569322
MD
298 if (idx < 0) {
299 if (lttng_context_is_app(context_name)) {
300 int ret;
301
302 ret = lttng_ust_add_app_context_to_ctx_rcu(context_name,
d5fc3224 303 &ctx);
53569322
MD
304 if (ret)
305 return ret;
d5fc3224 306 idx = lttng_get_context_index(ctx, context_name);
53569322
MD
307 if (idx < 0)
308 return -ENOENT;
309 } else {
310 return -ENOENT;
311 }
312 }
77aa5901
MD
313 /* Check if idx is too large for 16-bit offset */
314 if (idx > FILTER_BYTECODE_MAX_LEN - 1)
315 return -EINVAL;
316
317 /* Get context return type */
d5fc3224 318 ctx_field = &ctx->fields[idx];
47e5f13e
MD
319 op = (struct load_op *) &runtime->code[reloc_offset];
320
321 switch (filter_op) {
322 case FILTER_OP_GET_CONTEXT_REF:
323 {
324 struct field_ref *field_ref;
325
326 field_ref = (struct field_ref *) op->data;
327 switch (ctx_field->event_field.type.atype) {
328 case atype_integer:
329 case atype_enum:
330 op->op = FILTER_OP_GET_CONTEXT_REF_S64;
331 break;
332 /* Sequence and array supported as string */
333 case atype_string:
334 case atype_array:
335 case atype_sequence:
336 op->op = FILTER_OP_GET_CONTEXT_REF_STRING;
337 break;
338 case atype_float:
339 op->op = FILTER_OP_GET_CONTEXT_REF_DOUBLE;
340 break;
341 case atype_dynamic:
342 op->op = FILTER_OP_GET_CONTEXT_REF;
343 break;
344 default:
345 return -EINVAL;
346 }
347 /* set offset to context index within channel contexts */
348 field_ref->offset = (uint16_t) idx;
53569322 349 break;
47e5f13e 350 }
77aa5901
MD
351 default:
352 return -EINVAL;
353 }
77aa5901
MD
354 return 0;
355}
356
357static
d5fc3224 358int apply_reloc(const struct lttng_event_desc *event_desc,
77aa5901
MD
359 struct bytecode_runtime *runtime,
360 uint32_t runtime_len,
361 uint32_t reloc_offset,
362 const char *name)
363{
364 struct load_op *op;
365
366 dbg_printf("Apply reloc: %u %s\n", reloc_offset, name);
367
368 /* Ensure that the reloc is within the code */
369 if (runtime_len - reloc_offset < sizeof(uint16_t))
370 return -EINVAL;
371
47e5f13e 372 op = (struct load_op *) &runtime->code[reloc_offset];
77aa5901
MD
373 switch (op->op) {
374 case FILTER_OP_LOAD_FIELD_REF:
d5fc3224 375 return apply_field_reloc(event_desc, runtime, runtime_len,
47e5f13e 376 reloc_offset, name, op->op);
77aa5901 377 case FILTER_OP_GET_CONTEXT_REF:
d5fc3224 378 return apply_context_reloc(runtime, runtime_len,
47e5f13e
MD
379 reloc_offset, name, op->op);
380 case FILTER_OP_GET_SYMBOL:
381 case FILTER_OP_GET_SYMBOL_FIELD:
382 /*
383 * Will be handled by load specialize phase or
384 * dynamically by interpreter.
385 */
386 return 0;
77aa5901
MD
387 default:
388 ERR("Unknown reloc op type %u\n", op->op);
389 return -EINVAL;
390 }
391 return 0;
392}
393
f488575f
MD
394static
395int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode,
d5fc3224 396 struct cds_list_head *bytecode_runtime_head)
f488575f
MD
397{
398 struct lttng_bytecode_runtime *bc_runtime;
399
d5fc3224 400 cds_list_for_each_entry(bc_runtime, bytecode_runtime_head, node) {
f488575f
MD
401 if (bc_runtime->bc == filter_bytecode)
402 return 1;
403 }
404 return 0;
405}
406
cd54f6d9
MD
407/*
408 * Take a bytecode with reloc table and link it to an event to create a
409 * bytecode runtime.
410 */
2d78951a 411static
d5fc3224
FD
412int _lttng_filter_link_bytecode(const struct lttng_event_desc *event_desc,
413 struct lttng_ctx **ctx,
e58095ef
MD
414 struct lttng_ust_filter_bytecode_node *filter_bytecode,
415 struct cds_list_head *insert_loc)
2d78951a 416{
cd54f6d9
MD
417 int ret, offset, next_offset;
418 struct bytecode_runtime *runtime = NULL;
419 size_t runtime_alloc_len;
420
2d78951a
MD
421 if (!filter_bytecode)
422 return 0;
cd54f6d9 423 /* Bytecode already linked */
d5fc3224 424 if (bytecode_is_linked(filter_bytecode, insert_loc))
cd54f6d9 425 return 0;
2d78951a 426
f488575f 427 dbg_printf("Linking...\n");
cd54f6d9
MD
428
429 /* We don't need the reloc table in the runtime */
f488575f 430 runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset;
cd54f6d9
MD
431 runtime = zmalloc(runtime_alloc_len);
432 if (!runtime) {
433 ret = -ENOMEM;
e0a7d7ab 434 goto alloc_error;
cd54f6d9 435 }
f488575f 436 runtime->p.bc = filter_bytecode;
d5fc3224 437 runtime->p.ctx = ctx;
f488575f 438 runtime->len = filter_bytecode->bc.reloc_offset;
cd54f6d9 439 /* copy original bytecode */
47e5f13e 440 memcpy(runtime->code, filter_bytecode->bc.data, runtime->len);
cd54f6d9
MD
441 /*
442 * apply relocs. Those are a uint16_t (offset in bytecode)
443 * followed by a string (field name).
444 */
f488575f
MD
445 for (offset = filter_bytecode->bc.reloc_offset;
446 offset < filter_bytecode->bc.len;
cd54f6d9
MD
447 offset = next_offset) {
448 uint16_t reloc_offset =
f488575f 449 *(uint16_t *) &filter_bytecode->bc.data[offset];
77aa5901 450 const char *name =
f488575f 451 (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)];
cd54f6d9 452
d5fc3224 453 ret = apply_reloc(event_desc, runtime, runtime->len, reloc_offset, name);
cd54f6d9
MD
454 if (ret) {
455 goto link_error;
456 }
77aa5901 457 next_offset = offset + sizeof(uint16_t) + strlen(name) + 1;
cd54f6d9 458 }
9522a886
MD
459 /* Validate bytecode */
460 ret = lttng_filter_validate_bytecode(runtime);
461 if (ret) {
462 goto link_error;
463 }
08c84b15 464 /* Specialize bytecode */
d5fc3224 465 ret = lttng_filter_specialize_bytecode(event_desc, runtime);
08c84b15
MD
466 if (ret) {
467 goto link_error;
468 }
f488575f 469 runtime->p.filter = lttng_filter_interpret_bytecode;
21af05a9 470 runtime->p.link_failed = 0;
e58095ef 471 cds_list_add_rcu(&runtime->p.node, insert_loc);
f488575f 472 dbg_printf("Linking successful.\n");
2d78951a 473 return 0;
cd54f6d9
MD
474
475link_error:
f488575f 476 runtime->p.filter = lttng_filter_false;
21af05a9 477 runtime->p.link_failed = 1;
e58095ef 478 cds_list_add_rcu(&runtime->p.node, insert_loc);
e0a7d7ab 479alloc_error:
f488575f 480 dbg_printf("Linking failed.\n");
cd54f6d9 481 return ret;
2d78951a
MD
482}
483
e58095ef 484void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime)
2d78951a 485{
e58095ef 486 struct lttng_ust_filter_bytecode_node *bc = runtime->bc;
f488575f 487
21af05a9 488 if (!bc->enabler->enabled || runtime->link_failed)
e58095ef 489 runtime->filter = lttng_filter_false;
21af05a9
MD
490 else
491 runtime->filter = lttng_filter_interpret_bytecode;
2d78951a
MD
492}
493
494/*
e58095ef 495 * Link bytecode for all enablers referenced by an event.
2d78951a 496 */
d5fc3224
FD
497void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc,
498 struct lttng_ctx **ctx,
499 struct cds_list_head *bytecode_runtime_head,
e58095ef 500 struct lttng_enabler *enabler)
2d78951a 501{
e58095ef
MD
502 struct lttng_ust_filter_bytecode_node *bc;
503 struct lttng_bytecode_runtime *runtime;
504
d5fc3224 505 assert(event_desc);
e58095ef
MD
506
507 /* Link each bytecode. */
508 cds_list_for_each_entry(bc, &enabler->filter_bytecode_head, node) {
509 int found = 0, ret;
510 struct cds_list_head *insert_loc;
511
512 cds_list_for_each_entry(runtime,
d5fc3224 513 bytecode_runtime_head, node) {
e58095ef
MD
514 if (runtime->bc == bc) {
515 found = 1;
516 break;
517 }
518 }
519 /* Skip bytecode already linked */
520 if (found)
521 continue;
522
523 /*
524 * Insert at specified priority (seqnum) in increasing
525 * order.
526 */
527 cds_list_for_each_entry_reverse(runtime,
d5fc3224 528 bytecode_runtime_head, node) {
e58095ef
MD
529 if (runtime->bc->bc.seqnum < bc->bc.seqnum) {
530 /* insert here */
531 insert_loc = &runtime->node;
532 goto add_within;
533 }
534 }
d5fc3224 535
e58095ef 536 /* Add to head to list */
d5fc3224 537 insert_loc = bytecode_runtime_head;
e58095ef 538 add_within:
f488575f 539 dbg_printf("linking bytecode\n");
d5fc3224
FD
540 ret = _lttng_filter_link_bytecode(event_desc, ctx, bc,
541 insert_loc);
e58095ef
MD
542 if (ret) {
543 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
544 }
2d78951a 545 }
2d78951a
MD
546}
547
548/*
e58095ef 549 * We own the filter_bytecode if we return success.
2d78951a 550 */
e58095ef 551int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
f488575f 552 struct lttng_ust_filter_bytecode_node *filter_bytecode)
2d78951a 553{
e58095ef 554 cds_list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
2d78951a
MD
555 return 0;
556}
f488575f 557
d5fc3224
FD
558static
559void free_filter_runtime(struct cds_list_head *bytecode_runtime_head)
f488575f
MD
560{
561 struct bytecode_runtime *runtime, *tmp;
562
d5fc3224
FD
563 cds_list_for_each_entry_safe(runtime, tmp, bytecode_runtime_head,
564 p.node) {
47e5f13e 565 free(runtime->data);
f488575f
MD
566 free(runtime);
567 }
568}
d5fc3224
FD
569
570void lttng_free_event_filter_runtime(struct lttng_event *event)
571{
572 free_filter_runtime(&event->bytecode_runtime_head);
573}
574
575void lttng_free_trigger_filter_runtime(struct lttng_trigger *trigger)
576{
577 free_filter_runtime(&trigger->bytecode_runtime_head);
578}
This page took 0.065267 seconds and 5 git commands to generate.