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