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