Cleanup: namespace string encoding
[lttng-modules.git] / src / lttng-bytecode.c
1 /* SPDX-License-Identifier: MIT
2 *
3 * lttng-bytecode.c
4 *
5 * LTTng modules bytecode code.
6 *
7 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #include <linux/list.h>
11 #include <linux/slab.h>
12
13 #include <lttng/lttng-bytecode.h>
14
15 static const char *opnames[] = {
16 [ BYTECODE_OP_UNKNOWN ] = "UNKNOWN",
17
18 [ BYTECODE_OP_RETURN ] = "RETURN",
19
20 /* binary */
21 [ BYTECODE_OP_MUL ] = "MUL",
22 [ BYTECODE_OP_DIV ] = "DIV",
23 [ BYTECODE_OP_MOD ] = "MOD",
24 [ BYTECODE_OP_PLUS ] = "PLUS",
25 [ BYTECODE_OP_MINUS ] = "MINUS",
26 [ BYTECODE_OP_BIT_RSHIFT ] = "BIT_RSHIFT",
27 [ BYTECODE_OP_BIT_LSHIFT ] = "BIT_LSHIFT",
28 [ BYTECODE_OP_BIT_AND ] = "BIT_AND",
29 [ BYTECODE_OP_BIT_OR ] = "BIT_OR",
30 [ BYTECODE_OP_BIT_XOR ] = "BIT_XOR",
31
32 /* binary comparators */
33 [ BYTECODE_OP_EQ ] = "EQ",
34 [ BYTECODE_OP_NE ] = "NE",
35 [ BYTECODE_OP_GT ] = "GT",
36 [ BYTECODE_OP_LT ] = "LT",
37 [ BYTECODE_OP_GE ] = "GE",
38 [ BYTECODE_OP_LE ] = "LE",
39
40 /* string binary comparators */
41 [ BYTECODE_OP_EQ_STRING ] = "EQ_STRING",
42 [ BYTECODE_OP_NE_STRING ] = "NE_STRING",
43 [ BYTECODE_OP_GT_STRING ] = "GT_STRING",
44 [ BYTECODE_OP_LT_STRING ] = "LT_STRING",
45 [ BYTECODE_OP_GE_STRING ] = "GE_STRING",
46 [ BYTECODE_OP_LE_STRING ] = "LE_STRING",
47
48 /* s64 binary comparators */
49 [ BYTECODE_OP_EQ_S64 ] = "EQ_S64",
50 [ BYTECODE_OP_NE_S64 ] = "NE_S64",
51 [ BYTECODE_OP_GT_S64 ] = "GT_S64",
52 [ BYTECODE_OP_LT_S64 ] = "LT_S64",
53 [ BYTECODE_OP_GE_S64 ] = "GE_S64",
54 [ BYTECODE_OP_LE_S64 ] = "LE_S64",
55
56 /* double binary comparators */
57 [ BYTECODE_OP_EQ_DOUBLE ] = "EQ_DOUBLE",
58 [ BYTECODE_OP_NE_DOUBLE ] = "NE_DOUBLE",
59 [ BYTECODE_OP_GT_DOUBLE ] = "GT_DOUBLE",
60 [ BYTECODE_OP_LT_DOUBLE ] = "LT_DOUBLE",
61 [ BYTECODE_OP_GE_DOUBLE ] = "GE_DOUBLE",
62 [ BYTECODE_OP_LE_DOUBLE ] = "LE_DOUBLE",
63
64 /* Mixed S64-double binary comparators */
65 [ BYTECODE_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64",
66 [ BYTECODE_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64",
67 [ BYTECODE_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64",
68 [ BYTECODE_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64",
69 [ BYTECODE_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64",
70 [ BYTECODE_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64",
71
72 [ BYTECODE_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE",
73 [ BYTECODE_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE",
74 [ BYTECODE_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE",
75 [ BYTECODE_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE",
76 [ BYTECODE_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE",
77 [ BYTECODE_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE",
78
79 /* unary */
80 [ BYTECODE_OP_UNARY_PLUS ] = "UNARY_PLUS",
81 [ BYTECODE_OP_UNARY_MINUS ] = "UNARY_MINUS",
82 [ BYTECODE_OP_UNARY_NOT ] = "UNARY_NOT",
83 [ BYTECODE_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64",
84 [ BYTECODE_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64",
85 [ BYTECODE_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64",
86 [ BYTECODE_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE",
87 [ BYTECODE_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE",
88 [ BYTECODE_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE",
89
90 /* logical */
91 [ BYTECODE_OP_AND ] = "AND",
92 [ BYTECODE_OP_OR ] = "OR",
93
94 /* load field ref */
95 [ BYTECODE_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
96 [ BYTECODE_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING",
97 [ BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE",
98 [ BYTECODE_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64",
99 [ BYTECODE_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE",
100
101 /* load from immediate operand */
102 [ BYTECODE_OP_LOAD_STRING ] = "LOAD_STRING",
103 [ BYTECODE_OP_LOAD_S64 ] = "LOAD_S64",
104 [ BYTECODE_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
105
106 /* cast */
107 [ BYTECODE_OP_CAST_TO_S64 ] = "CAST_TO_S64",
108 [ BYTECODE_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64",
109 [ BYTECODE_OP_CAST_NOP ] = "CAST_NOP",
110
111 /* get context ref */
112 [ BYTECODE_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF",
113 [ BYTECODE_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING",
114 [ BYTECODE_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64",
115 [ BYTECODE_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE",
116
117 /* load userspace field ref */
118 [ BYTECODE_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING",
119 [ BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE",
120
121 /*
122 * load immediate star globbing pattern (literal string)
123 * from immediate.
124 */
125 [ BYTECODE_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING",
126
127 /* globbing pattern binary operator: apply to */
128 [ BYTECODE_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING",
129 [ BYTECODE_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING",
130
131 /*
132 * Instructions for recursive traversal through composed types.
133 */
134 [ BYTECODE_OP_GET_CONTEXT_ROOT ] = "GET_CONTEXT_ROOT",
135 [ BYTECODE_OP_GET_APP_CONTEXT_ROOT ] = "GET_APP_CONTEXT_ROOT",
136 [ BYTECODE_OP_GET_PAYLOAD_ROOT ] = "GET_PAYLOAD_ROOT",
137
138 [ BYTECODE_OP_GET_SYMBOL ] = "GET_SYMBOL",
139 [ BYTECODE_OP_GET_SYMBOL_FIELD ] = "GET_SYMBOL_FIELD",
140 [ BYTECODE_OP_GET_INDEX_U16 ] = "GET_INDEX_U16",
141 [ BYTECODE_OP_GET_INDEX_U64 ] = "GET_INDEX_U64",
142
143 [ BYTECODE_OP_LOAD_FIELD ] = "LOAD_FIELD",
144 [ BYTECODE_OP_LOAD_FIELD_S8 ] = "LOAD_FIELD_S8",
145 [ BYTECODE_OP_LOAD_FIELD_S16 ] = "LOAD_FIELD_S16",
146 [ BYTECODE_OP_LOAD_FIELD_S32 ] = "LOAD_FIELD_S32",
147 [ BYTECODE_OP_LOAD_FIELD_S64 ] = "LOAD_FIELD_S64",
148 [ BYTECODE_OP_LOAD_FIELD_U8 ] = "LOAD_FIELD_U8",
149 [ BYTECODE_OP_LOAD_FIELD_U16 ] = "LOAD_FIELD_U16",
150 [ BYTECODE_OP_LOAD_FIELD_U32 ] = "LOAD_FIELD_U32",
151 [ BYTECODE_OP_LOAD_FIELD_U64 ] = "LOAD_FIELD_U64",
152 [ BYTECODE_OP_LOAD_FIELD_STRING ] = "LOAD_FIELD_STRING",
153 [ BYTECODE_OP_LOAD_FIELD_SEQUENCE ] = "LOAD_FIELD_SEQUENCE",
154 [ BYTECODE_OP_LOAD_FIELD_DOUBLE ] = "LOAD_FIELD_DOUBLE",
155
156 [ BYTECODE_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT",
157
158 [ BYTECODE_OP_RETURN_S64 ] = "RETURN_S64",
159 };
160
161 const char *lttng_bytecode_print_op(enum bytecode_op op)
162 {
163 if (op >= NR_BYTECODE_OPS)
164 return "UNKNOWN";
165 else
166 return opnames[op];
167 }
168
169 static
170 int apply_field_reloc(const struct lttng_event_desc *event_desc,
171 struct bytecode_runtime *runtime,
172 uint32_t runtime_len,
173 uint32_t reloc_offset,
174 const char *field_name,
175 enum bytecode_op bytecode_op)
176 {
177 const struct lttng_event_field *fields, *field = NULL;
178 unsigned int nr_fields, i;
179 struct load_op *op;
180 uint32_t field_offset = 0;
181
182 dbg_printk("Apply field reloc: %u %s\n", reloc_offset, field_name);
183
184 /* Lookup event by name */
185 if (!event_desc)
186 return -EINVAL;
187 fields = event_desc->fields;
188 if (!fields)
189 return -EINVAL;
190 nr_fields = event_desc->nr_fields;
191 for (i = 0; i < nr_fields; i++) {
192 if (fields[i].nofilter)
193 continue;
194 if (!strcmp(fields[i].name, field_name)) {
195 field = &fields[i];
196 break;
197 }
198 /* compute field offset */
199 switch (fields[i].type.type) {
200 case lttng_kernel_type_integer:
201 case lttng_kernel_type_enum_nestable:
202 field_offset += sizeof(int64_t);
203 break;
204 case lttng_kernel_type_array_nestable:
205 if (!lttng_is_bytewise_integer(fields[i].type.u.array_nestable.elem_type))
206 return -EINVAL;
207 field_offset += sizeof(unsigned long);
208 field_offset += sizeof(void *);
209 break;
210 case lttng_kernel_type_sequence_nestable:
211 if (!lttng_is_bytewise_integer(fields[i].type.u.sequence_nestable.elem_type))
212 return -EINVAL;
213 field_offset += sizeof(unsigned long);
214 field_offset += sizeof(void *);
215 break;
216 case lttng_kernel_type_string:
217 field_offset += sizeof(void *);
218 break;
219 case lttng_kernel_type_struct_nestable: /* Unsupported. */
220 case lttng_kernel_type_variant_nestable: /* Unsupported. */
221 default:
222 return -EINVAL;
223 }
224 }
225 if (!field)
226 return -EINVAL;
227
228 /* Check if field offset is too large for 16-bit offset */
229 if (field_offset > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1)
230 return -EINVAL;
231
232 /* set type */
233 op = (struct load_op *) &runtime->code[reloc_offset];
234
235 switch (bytecode_op) {
236 case BYTECODE_OP_LOAD_FIELD_REF:
237 {
238 struct field_ref *field_ref;
239
240 field_ref = (struct field_ref *) op->data;
241 switch (field->type.type) {
242 case lttng_kernel_type_integer:
243 case lttng_kernel_type_enum_nestable:
244 op->op = BYTECODE_OP_LOAD_FIELD_REF_S64;
245 break;
246 case lttng_kernel_type_array_nestable:
247 {
248 const struct lttng_type *elem_type = field->type.u.array_nestable.elem_type;
249
250 if (!lttng_is_bytewise_integer(elem_type) || elem_type->u.integer.encoding == lttng_kernel_string_encoding_none)
251 return -EINVAL;
252 if (field->user)
253 op->op = BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE;
254 else
255 op->op = BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE;
256 break;
257 }
258 case lttng_kernel_type_sequence_nestable:
259 {
260 const struct lttng_type *elem_type = field->type.u.sequence_nestable.elem_type;
261
262 if (!lttng_is_bytewise_integer(elem_type) || elem_type->u.integer.encoding == lttng_kernel_string_encoding_none)
263 return -EINVAL;
264 if (field->user)
265 op->op = BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE;
266 else
267 op->op = BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE;
268 break;
269 }
270 case lttng_kernel_type_string:
271 if (field->user)
272 op->op = BYTECODE_OP_LOAD_FIELD_REF_USER_STRING;
273 else
274 op->op = BYTECODE_OP_LOAD_FIELD_REF_STRING;
275 break;
276 case lttng_kernel_type_struct_nestable: /* Unsupported. */
277 case lttng_kernel_type_variant_nestable: /* Unsupported. */
278 default:
279 return -EINVAL;
280 }
281 /* set offset */
282 field_ref->offset = (uint16_t) field_offset;
283 break;
284 }
285 default:
286 return -EINVAL;
287 }
288 return 0;
289 }
290
291 static
292 int apply_context_reloc(struct bytecode_runtime *runtime,
293 uint32_t runtime_len,
294 uint32_t reloc_offset,
295 const char *context_name,
296 enum bytecode_op bytecode_op)
297 {
298 struct load_op *op;
299 struct lttng_ctx_field *ctx_field;
300 int idx;
301
302 dbg_printk("Apply context reloc: %u %s\n", reloc_offset, context_name);
303
304 /* Get context index */
305 idx = lttng_get_context_index(lttng_static_ctx, context_name);
306 if (idx < 0)
307 return -ENOENT;
308
309 /* Check if idx is too large for 16-bit offset */
310 if (idx > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1)
311 return -EINVAL;
312
313 /* Get context return type */
314 ctx_field = &lttng_static_ctx->fields[idx];
315 op = (struct load_op *) &runtime->code[reloc_offset];
316
317 switch (bytecode_op) {
318 case BYTECODE_OP_GET_CONTEXT_REF:
319 {
320 struct field_ref *field_ref;
321
322 field_ref = (struct field_ref *) op->data;
323 switch (ctx_field->event_field.type.type) {
324 case lttng_kernel_type_integer:
325 case lttng_kernel_type_enum_nestable:
326 op->op = BYTECODE_OP_GET_CONTEXT_REF_S64;
327 break;
328 /* Sequence and array supported as string */
329 case lttng_kernel_type_string:
330 BUG_ON(ctx_field->event_field.user);
331 op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
332 break;
333 case lttng_kernel_type_array_nestable:
334 {
335 const struct lttng_type *elem_type = ctx_field->event_field.type.u.array_nestable.elem_type;
336
337 if (!lttng_is_bytewise_integer(elem_type) || elem_type->u.integer.encoding == lttng_kernel_string_encoding_none)
338 return -EINVAL;
339 BUG_ON(ctx_field->event_field.user);
340 op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
341 break;
342 }
343 case lttng_kernel_type_sequence_nestable:
344 {
345 const struct lttng_type *elem_type = ctx_field->event_field.type.u.sequence_nestable.elem_type;
346
347 if (!lttng_is_bytewise_integer(elem_type) || elem_type->u.integer.encoding == lttng_kernel_string_encoding_none)
348 return -EINVAL;
349 BUG_ON(ctx_field->event_field.user);
350 op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
351 break;
352 }
353 case lttng_kernel_type_struct_nestable: /* Unsupported. */
354 case lttng_kernel_type_variant_nestable: /* Unsupported. */
355 default:
356 return -EINVAL;
357 }
358 /* set offset to context index within channel contexts */
359 field_ref->offset = (uint16_t) idx;
360 break;
361 }
362 default:
363 return -EINVAL;
364 }
365 return 0;
366 }
367
368 static
369 int apply_reloc(const struct lttng_event_desc *event_desc,
370 struct bytecode_runtime *runtime,
371 uint32_t runtime_len,
372 uint32_t reloc_offset,
373 const char *name)
374 {
375 struct load_op *op;
376
377 dbg_printk("Apply reloc: %u %s\n", reloc_offset, name);
378
379 /* Ensure that the reloc is within the code */
380 if (runtime_len - reloc_offset < sizeof(uint16_t))
381 return -EINVAL;
382
383 op = (struct load_op *) &runtime->code[reloc_offset];
384 switch (op->op) {
385 case BYTECODE_OP_LOAD_FIELD_REF:
386 return apply_field_reloc(event_desc, runtime, runtime_len,
387 reloc_offset, name, op->op);
388 case BYTECODE_OP_GET_CONTEXT_REF:
389 return apply_context_reloc(runtime, runtime_len,
390 reloc_offset, name, op->op);
391 case BYTECODE_OP_GET_SYMBOL:
392 case BYTECODE_OP_GET_SYMBOL_FIELD:
393 /*
394 * Will be handled by load specialize phase or
395 * dynamically by interpreter.
396 */
397 return 0;
398 default:
399 printk(KERN_WARNING "LTTng: filter: Unknown reloc op type %u\n", op->op);
400 return -EINVAL;
401 }
402 return 0;
403 }
404
405 static
406 int bytecode_is_linked(struct lttng_bytecode_node *bytecode,
407 struct list_head *bytecode_runtime_head)
408 {
409 struct lttng_bytecode_runtime *bc_runtime;
410
411 list_for_each_entry(bc_runtime, bytecode_runtime_head, node) {
412 if (bc_runtime->bc == bytecode)
413 return 1;
414 }
415 return 0;
416 }
417
418 /*
419 * Take a bytecode with reloc table and link it to an event to create a
420 * bytecode runtime.
421 */
422 static
423 int link_bytecode(const struct lttng_event_desc *event_desc,
424 struct lttng_ctx *ctx,
425 struct lttng_bytecode_node *bytecode,
426 struct list_head *bytecode_runtime_head,
427 struct list_head *insert_loc)
428 {
429 int ret, offset, next_offset;
430 struct bytecode_runtime *runtime = NULL;
431 size_t runtime_alloc_len;
432
433 if (!bytecode)
434 return 0;
435 /* Bytecode already linked */
436 if (bytecode_is_linked(bytecode, bytecode_runtime_head))
437 return 0;
438
439 dbg_printk("Linking...\n");
440
441 /* We don't need the reloc table in the runtime */
442 runtime_alloc_len = sizeof(*runtime) + bytecode->bc.reloc_offset;
443 runtime = kzalloc(runtime_alloc_len, GFP_KERNEL);
444 if (!runtime) {
445 ret = -ENOMEM;
446 goto alloc_error;
447 }
448 runtime->p.bc = bytecode;
449 runtime->p.ctx = ctx;
450 runtime->len = bytecode->bc.reloc_offset;
451 /* copy original bytecode */
452 memcpy(runtime->code, bytecode->bc.data, runtime->len);
453 /*
454 * apply relocs. Those are a uint16_t (offset in bytecode)
455 * followed by a string (field name).
456 */
457 for (offset = bytecode->bc.reloc_offset;
458 offset < bytecode->bc.len;
459 offset = next_offset) {
460 uint16_t reloc_offset =
461 *(uint16_t *) &bytecode->bc.data[offset];
462 const char *name =
463 (const char *) &bytecode->bc.data[offset + sizeof(uint16_t)];
464
465 ret = apply_reloc(event_desc, runtime, runtime->len, reloc_offset, name);
466 if (ret) {
467 goto link_error;
468 }
469 next_offset = offset + sizeof(uint16_t) + strlen(name) + 1;
470 }
471 /* Validate bytecode */
472 ret = lttng_bytecode_validate(runtime);
473 if (ret) {
474 goto link_error;
475 }
476 /* Specialize bytecode */
477 ret = lttng_bytecode_specialize(event_desc, runtime);
478 if (ret) {
479 goto link_error;
480 }
481
482 switch (bytecode->type) {
483 case LTTNG_BYTECODE_NODE_TYPE_FILTER:
484 runtime->p.interpreter_funcs.filter = lttng_bytecode_filter_interpret;
485 break;
486 case LTTNG_BYTECODE_NODE_TYPE_CAPTURE:
487 runtime->p.interpreter_funcs.capture = lttng_bytecode_capture_interpret_false;
488 break;
489 default:
490 WARN_ON(1);
491 }
492
493 runtime->p.link_failed = 0;
494 list_add_rcu(&runtime->p.node, insert_loc);
495 dbg_printk("Linking successful.\n");
496 return 0;
497
498 link_error:
499
500 switch (bytecode->type) {
501 case LTTNG_BYTECODE_NODE_TYPE_FILTER:
502 runtime->p.interpreter_funcs.filter = lttng_bytecode_filter_interpret_false;
503 break;
504 case LTTNG_BYTECODE_NODE_TYPE_CAPTURE:
505 runtime->p.interpreter_funcs.capture = lttng_bytecode_capture_interpret_false;
506 break;
507 default:
508 WARN_ON(1);
509 }
510 runtime->p.link_failed = 1;
511 list_add_rcu(&runtime->p.node, insert_loc);
512 alloc_error:
513 dbg_printk("Linking failed.\n");
514 return ret;
515 }
516
517 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime)
518 {
519 struct lttng_bytecode_node *bc = runtime->bc;
520
521 if (!bc->enabler->enabled || runtime->link_failed)
522 runtime->interpreter_funcs.filter = lttng_bytecode_filter_interpret_false;
523 else
524 runtime->interpreter_funcs.filter = lttng_bytecode_filter_interpret;
525 }
526
527 void lttng_bytecode_capture_sync_state(struct lttng_bytecode_runtime *runtime)
528 {
529 struct lttng_bytecode_node *bc = runtime->bc;
530
531 if (!bc->enabler->enabled || runtime->link_failed)
532 runtime->interpreter_funcs.capture = lttng_bytecode_capture_interpret_false;
533 else
534 runtime->interpreter_funcs.capture = lttng_bytecode_capture_interpret;
535 }
536
537 /*
538 * Given the lists of bytecode programs of an instance (event or event
539 * notifier) and of a matching enabler, try to link all the enabler's bytecode
540 * programs with the instance.
541 *
542 * This function is called after we confirmed that name enabler and the
543 * instance are matching names (or glob pattern matching).
544 */
545 void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc,
546 struct lttng_ctx *ctx,
547 struct list_head *instance_bytecode_head,
548 struct list_head *enabler_bytecode_head)
549 {
550 struct lttng_bytecode_node *enabler_bc;
551 struct lttng_bytecode_runtime *runtime;
552
553 WARN_ON_ONCE(!event_desc);
554
555 /* Go over all the bytecode programs of the enabler. */
556 list_for_each_entry(enabler_bc, enabler_bytecode_head, node) {
557 int found = 0, ret;
558 struct list_head *insert_loc;
559
560 /*
561 * Check if the current enabler bytecode program is already
562 * linked with the instance.
563 */
564 list_for_each_entry(runtime, instance_bytecode_head, node) {
565 if (runtime->bc == enabler_bc) {
566 found = 1;
567 break;
568 }
569 }
570
571 /*
572 * Skip bytecode already linked, go to the next enabler
573 * bytecode program.
574 */
575 if (found)
576 continue;
577
578 /*
579 * Insert at specified priority (seqnum) in increasing
580 * order. If there already is a bytecode of the same priority,
581 * insert the new bytecode right after it.
582 */
583 list_for_each_entry_reverse(runtime,
584 instance_bytecode_head, node) {
585 if (runtime->bc->bc.seqnum <= enabler_bc->bc.seqnum) {
586 /* insert here */
587 insert_loc = &runtime->node;
588 goto add_within;
589 }
590 }
591 /* Add to head to list */
592 insert_loc = instance_bytecode_head;
593 add_within:
594 dbg_printk("linking bytecode\n");
595 ret = link_bytecode(event_desc, ctx, enabler_bc, instance_bytecode_head, insert_loc);
596 if (ret) {
597 dbg_printk("[lttng filter] warning: cannot link event bytecode\n");
598 }
599 }
600 }
601
602 /*
603 * We own the filter_bytecode if we return success.
604 */
605 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
606 struct lttng_bytecode_node *filter_bytecode)
607 {
608 list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
609 return 0;
610 }
611
612 void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler)
613 {
614 struct lttng_bytecode_node *filter_bytecode, *tmp;
615
616 list_for_each_entry_safe(filter_bytecode, tmp,
617 &enabler->filter_bytecode_head, node) {
618 kfree(filter_bytecode);
619 }
620 }
621
622 void lttng_free_event_filter_runtime(struct lttng_event *event)
623 {
624 struct bytecode_runtime *runtime, *tmp;
625
626 list_for_each_entry_safe(runtime, tmp,
627 &event->filter_bytecode_runtime_head, p.node) {
628 kfree(runtime->data);
629 kfree(runtime);
630 }
631 }
632
633 void lttng_free_event_notifier_filter_runtime(struct lttng_event_notifier *event_notifier)
634 {
635 struct bytecode_runtime *runtime, *tmp;
636
637 list_for_each_entry_safe(runtime, tmp,
638 &event_notifier->filter_bytecode_runtime_head, p.node) {
639 kfree(runtime->data);
640 kfree(runtime);
641 }
642 }
This page took 0.04332 seconds and 4 git commands to generate.