2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST bytecode code.
13 #include <urcu/rculist.h>
15 #include "lttng-bytecode.h"
16 #include "ust-events-internal.h"
18 static const char *opnames
[] = {
19 [ BYTECODE_OP_UNKNOWN
] = "UNKNOWN",
21 [ BYTECODE_OP_RETURN
] = "RETURN",
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",
35 /* binary comparators */
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",
43 /* string binary comparators */
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",
51 /* s64 binary comparators */
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",
59 /* double binary comparators */
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",
67 /* Mixed S64-double binary comparators */
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",
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",
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",
94 [ BYTECODE_OP_AND
] = "AND",
95 [ BYTECODE_OP_OR
] = "OR",
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",
104 /* load from immediate operand */
105 [ BYTECODE_OP_LOAD_STRING
] = "LOAD_STRING",
106 [ BYTECODE_OP_LOAD_S64
] = "LOAD_S64",
107 [ BYTECODE_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
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",
114 /* get context ref */
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",
120 /* load userspace field ref */
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",
125 * load immediate star globbing pattern (literal string)
128 [ BYTECODE_OP_LOAD_STAR_GLOB_STRING
] = "LOAD_STAR_GLOB_STRING",
130 /* globbing pattern binary operator: apply to */
131 [ BYTECODE_OP_EQ_STAR_GLOB_STRING
] = "EQ_STAR_GLOB_STRING",
132 [ BYTECODE_OP_NE_STAR_GLOB_STRING
] = "NE_STAR_GLOB_STRING",
135 * Instructions for recursive traversal through composed types.
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",
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",
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",
159 [ BYTECODE_OP_UNARY_BIT_NOT
] = "UNARY_BIT_NOT",
161 [ BYTECODE_OP_RETURN_S64
] = "RETURN_S64",
164 const char *print_op(enum bytecode_op op
)
166 if (op
>= NR_BYTECODE_OPS
)
173 int apply_field_reloc(const struct lttng_event_desc
*event_desc
,
174 struct bytecode_runtime
*runtime
,
175 uint32_t runtime_len
,
176 uint32_t reloc_offset
,
177 const char *field_name
,
178 enum bytecode_op bytecode_op
)
180 const struct lttng_event_field
*fields
, *field
= NULL
;
181 unsigned int nr_fields
, i
;
183 uint32_t field_offset
= 0;
185 dbg_printf("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
187 /* Lookup event by name */
190 fields
= event_desc
->fields
;
193 nr_fields
= event_desc
->nr_fields
;
194 for (i
= 0; i
< nr_fields
; i
++) {
195 if (fields
[i
].u
.ext
.nofilter
) {
198 if (!strcmp(fields
[i
].name
, field_name
)) {
202 /* compute field offset */
203 switch (fields
[i
].type
.atype
) {
206 case atype_enum_nestable
:
207 field_offset
+= sizeof(int64_t);
210 case atype_array_nestable
:
212 case atype_sequence_nestable
:
213 field_offset
+= sizeof(unsigned long);
214 field_offset
+= sizeof(void *);
217 field_offset
+= sizeof(void *);
220 field_offset
+= sizeof(double);
229 /* Check if field offset is too large for 16-bit offset */
230 if (field_offset
> FILTER_BYTECODE_MAX_LEN
- 1)
234 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
236 switch (bytecode_op
) {
237 case BYTECODE_OP_LOAD_FIELD_REF
:
239 struct field_ref
*field_ref
;
241 field_ref
= (struct field_ref
*) op
->data
;
242 switch (field
->type
.atype
) {
245 case atype_enum_nestable
:
246 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_S64
;
249 case atype_array_nestable
:
251 case atype_sequence_nestable
:
252 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
;
255 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_STRING
;
258 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_DOUBLE
;
264 field_ref
->offset
= (uint16_t) field_offset
;
274 int apply_context_reloc(struct bytecode_runtime
*runtime
,
275 uint32_t runtime_len
,
276 uint32_t reloc_offset
,
277 const char *context_name
,
278 enum bytecode_op bytecode_op
)
281 struct lttng_ctx_field
*ctx_field
;
283 struct lttng_ctx
**pctx
= runtime
->p
.pctx
;
285 dbg_printf("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
287 /* Get context index */
288 idx
= lttng_get_context_index(*pctx
, context_name
);
290 if (lttng_context_is_app(context_name
)) {
293 ret
= lttng_ust_add_app_context_to_ctx_rcu(context_name
,
297 idx
= lttng_get_context_index(*pctx
, context_name
);
304 /* Check if idx is too large for 16-bit offset */
305 if (idx
> FILTER_BYTECODE_MAX_LEN
- 1)
308 /* Get context return type */
309 ctx_field
= &(*pctx
)->fields
[idx
];
310 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
312 switch (bytecode_op
) {
313 case BYTECODE_OP_GET_CONTEXT_REF
:
315 struct field_ref
*field_ref
;
317 field_ref
= (struct field_ref
*) op
->data
;
318 switch (ctx_field
->event_field
.type
.atype
) {
321 case atype_enum_nestable
:
322 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_S64
;
324 /* Sequence and array supported as string */
327 case atype_array_nestable
:
329 case atype_sequence_nestable
:
330 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
333 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_DOUBLE
;
336 op
->op
= BYTECODE_OP_GET_CONTEXT_REF
;
341 /* set offset to context index within channel contexts */
342 field_ref
->offset
= (uint16_t) idx
;
352 int apply_reloc(const struct lttng_event_desc
*event_desc
,
353 struct bytecode_runtime
*runtime
,
354 uint32_t runtime_len
,
355 uint32_t reloc_offset
,
360 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, name
);
362 /* Ensure that the reloc is within the code */
363 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
366 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
368 case BYTECODE_OP_LOAD_FIELD_REF
:
369 return apply_field_reloc(event_desc
, runtime
, runtime_len
,
370 reloc_offset
, name
, op
->op
);
371 case BYTECODE_OP_GET_CONTEXT_REF
:
372 return apply_context_reloc(runtime
, runtime_len
,
373 reloc_offset
, name
, op
->op
);
374 case BYTECODE_OP_GET_SYMBOL
:
375 case BYTECODE_OP_GET_SYMBOL_FIELD
:
377 * Will be handled by load specialize phase or
378 * dynamically by interpreter.
382 ERR("Unknown reloc op type %u\n", op
->op
);
389 int bytecode_is_linked(struct lttng_ust_bytecode_node
*bytecode
,
390 struct cds_list_head
*bytecode_runtime_head
)
392 struct lttng_bytecode_runtime
*bc_runtime
;
394 cds_list_for_each_entry(bc_runtime
, bytecode_runtime_head
, node
) {
395 if (bc_runtime
->bc
== bytecode
)
402 * Take a bytecode with reloc table and link it to an event to create a
406 int link_bytecode(const struct lttng_event_desc
*event_desc
,
407 struct lttng_ctx
**ctx
,
408 struct lttng_ust_bytecode_node
*bytecode
,
409 struct cds_list_head
*insert_loc
)
411 int ret
, offset
, next_offset
;
412 struct bytecode_runtime
*runtime
= NULL
;
413 size_t runtime_alloc_len
;
417 /* Bytecode already linked */
418 if (bytecode_is_linked(bytecode
, insert_loc
))
421 dbg_printf("Linking...\n");
423 /* We don't need the reloc table in the runtime */
424 runtime_alloc_len
= sizeof(*runtime
) + bytecode
->bc
.reloc_offset
;
425 runtime
= zmalloc(runtime_alloc_len
);
430 runtime
->p
.bc
= bytecode
;
431 runtime
->p
.pctx
= ctx
;
432 runtime
->len
= bytecode
->bc
.reloc_offset
;
433 /* copy original bytecode */
434 memcpy(runtime
->code
, bytecode
->bc
.data
, runtime
->len
);
436 * apply relocs. Those are a uint16_t (offset in bytecode)
437 * followed by a string (field name).
439 for (offset
= bytecode
->bc
.reloc_offset
;
440 offset
< bytecode
->bc
.len
;
441 offset
= next_offset
) {
442 uint16_t reloc_offset
=
443 *(uint16_t *) &bytecode
->bc
.data
[offset
];
445 (const char *) &bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
447 ret
= apply_reloc(event_desc
, runtime
, runtime
->len
, reloc_offset
, name
);
451 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
453 /* Validate bytecode */
454 ret
= lttng_bytecode_validate(runtime
);
458 /* Specialize bytecode */
459 ret
= lttng_bytecode_specialize(event_desc
, runtime
);
464 switch (bytecode
->type
) {
465 case LTTNG_UST_BYTECODE_NODE_TYPE_FILTER
:
466 runtime
->p
.interpreter_funcs
.filter
= lttng_bytecode_filter_interpret
;
468 case LTTNG_UST_BYTECODE_NODE_TYPE_CAPTURE
:
469 runtime
->p
.interpreter_funcs
.capture
= lttng_bytecode_capture_interpret
;
475 runtime
->p
.link_failed
= 0;
476 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
477 dbg_printf("Linking successful.\n");
481 switch (bytecode
->type
) {
482 case LTTNG_UST_BYTECODE_NODE_TYPE_FILTER
:
483 runtime
->p
.interpreter_funcs
.filter
= lttng_bytecode_filter_interpret_false
;
485 case LTTNG_UST_BYTECODE_NODE_TYPE_CAPTURE
:
486 runtime
->p
.interpreter_funcs
.capture
= lttng_bytecode_capture_interpret_false
;
492 runtime
->p
.link_failed
= 1;
493 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
495 dbg_printf("Linking failed.\n");
499 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
501 struct lttng_ust_bytecode_node
*bc
= runtime
->bc
;
503 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
504 runtime
->interpreter_funcs
.filter
= lttng_bytecode_filter_interpret_false
;
506 runtime
->interpreter_funcs
.filter
= lttng_bytecode_filter_interpret
;
509 void lttng_bytecode_capture_sync_state(struct lttng_bytecode_runtime
*runtime
)
511 struct lttng_ust_bytecode_node
*bc
= runtime
->bc
;
513 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
514 runtime
->interpreter_funcs
.capture
= lttng_bytecode_capture_interpret_false
;
516 runtime
->interpreter_funcs
.capture
= lttng_bytecode_capture_interpret
;
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
524 * This function is called after we confirmed that name enabler and the
525 * instance are name matching (or glob pattern matching).
527 void lttng_enabler_link_bytecode(const struct lttng_event_desc
*event_desc
,
528 struct lttng_ctx
**ctx
,
529 struct cds_list_head
*instance_bytecode_head
,
530 struct cds_list_head
*enabler_bytecode_head
)
532 struct lttng_ust_bytecode_node
*enabler_bc
;
533 struct lttng_bytecode_runtime
*runtime
;
537 /* Go over all the bytecode programs of the enabler. */
538 cds_list_for_each_entry(enabler_bc
, enabler_bytecode_head
, node
) {
540 struct cds_list_head
*insert_loc
;
543 * Check if the current enabler bytecode program is already
544 * linked with the instance.
546 cds_list_for_each_entry(runtime
, instance_bytecode_head
, node
) {
547 if (runtime
->bc
== enabler_bc
) {
554 * Skip bytecode already linked, go to the next enabler
561 * Insert at specified priority (seqnum) in increasing
562 * order. If there already is a bytecode of the same priority,
563 * insert the new bytecode right after it.
565 cds_list_for_each_entry_reverse(runtime
,
566 instance_bytecode_head
, node
) {
567 if (runtime
->bc
->bc
.seqnum
<= enabler_bc
->bc
.seqnum
) {
569 insert_loc
= &runtime
->node
;
574 /* Add to head to list */
575 insert_loc
= instance_bytecode_head
;
577 dbg_printf("linking bytecode\n");
578 ret
= link_bytecode(event_desc
, ctx
, enabler_bc
, insert_loc
);
580 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
586 * We own the bytecode if we return success.
588 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
589 struct lttng_ust_bytecode_node
*bytecode
)
591 cds_list_add(&bytecode
->node
, &enabler
->filter_bytecode_head
);
596 void free_filter_runtime(struct cds_list_head
*bytecode_runtime_head
)
598 struct bytecode_runtime
*runtime
, *tmp
;
600 cds_list_for_each_entry_safe(runtime
, tmp
, bytecode_runtime_head
,
607 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
609 free_filter_runtime(&event
->filter_bytecode_runtime_head
);
612 void lttng_free_event_notifier_filter_runtime(
613 struct lttng_event_notifier
*event_notifier
)
615 free_filter_runtime(&event_notifier
->filter_bytecode_runtime_head
);