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