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 "context-internal.h"
16 #include "lttng-bytecode.h"
17 #include "ust-events-internal.h"
18 #include "ust-helper.h"
20 static const char *opnames
[] = {
21 [ BYTECODE_OP_UNKNOWN
] = "UNKNOWN",
23 [ BYTECODE_OP_RETURN
] = "RETURN",
26 [ BYTECODE_OP_MUL
] = "MUL",
27 [ BYTECODE_OP_DIV
] = "DIV",
28 [ BYTECODE_OP_MOD
] = "MOD",
29 [ BYTECODE_OP_PLUS
] = "PLUS",
30 [ BYTECODE_OP_MINUS
] = "MINUS",
31 [ BYTECODE_OP_BIT_RSHIFT
] = "BIT_RSHIFT",
32 [ BYTECODE_OP_BIT_LSHIFT
] = "BIT_LSHIFT",
33 [ BYTECODE_OP_BIT_AND
] = "BIT_AND",
34 [ BYTECODE_OP_BIT_OR
] = "BIT_OR",
35 [ BYTECODE_OP_BIT_XOR
] = "BIT_XOR",
37 /* binary comparators */
38 [ BYTECODE_OP_EQ
] = "EQ",
39 [ BYTECODE_OP_NE
] = "NE",
40 [ BYTECODE_OP_GT
] = "GT",
41 [ BYTECODE_OP_LT
] = "LT",
42 [ BYTECODE_OP_GE
] = "GE",
43 [ BYTECODE_OP_LE
] = "LE",
45 /* string binary comparators */
46 [ BYTECODE_OP_EQ_STRING
] = "EQ_STRING",
47 [ BYTECODE_OP_NE_STRING
] = "NE_STRING",
48 [ BYTECODE_OP_GT_STRING
] = "GT_STRING",
49 [ BYTECODE_OP_LT_STRING
] = "LT_STRING",
50 [ BYTECODE_OP_GE_STRING
] = "GE_STRING",
51 [ BYTECODE_OP_LE_STRING
] = "LE_STRING",
53 /* s64 binary comparators */
54 [ BYTECODE_OP_EQ_S64
] = "EQ_S64",
55 [ BYTECODE_OP_NE_S64
] = "NE_S64",
56 [ BYTECODE_OP_GT_S64
] = "GT_S64",
57 [ BYTECODE_OP_LT_S64
] = "LT_S64",
58 [ BYTECODE_OP_GE_S64
] = "GE_S64",
59 [ BYTECODE_OP_LE_S64
] = "LE_S64",
61 /* double binary comparators */
62 [ BYTECODE_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
63 [ BYTECODE_OP_NE_DOUBLE
] = "NE_DOUBLE",
64 [ BYTECODE_OP_GT_DOUBLE
] = "GT_DOUBLE",
65 [ BYTECODE_OP_LT_DOUBLE
] = "LT_DOUBLE",
66 [ BYTECODE_OP_GE_DOUBLE
] = "GE_DOUBLE",
67 [ BYTECODE_OP_LE_DOUBLE
] = "LE_DOUBLE",
69 /* Mixed S64-double binary comparators */
70 [ BYTECODE_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
71 [ BYTECODE_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
72 [ BYTECODE_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
73 [ BYTECODE_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
74 [ BYTECODE_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
75 [ BYTECODE_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
77 [ BYTECODE_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
78 [ BYTECODE_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
79 [ BYTECODE_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
80 [ BYTECODE_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
81 [ BYTECODE_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
82 [ BYTECODE_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
85 [ BYTECODE_OP_UNARY_PLUS
] = "UNARY_PLUS",
86 [ BYTECODE_OP_UNARY_MINUS
] = "UNARY_MINUS",
87 [ BYTECODE_OP_UNARY_NOT
] = "UNARY_NOT",
88 [ BYTECODE_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
89 [ BYTECODE_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
90 [ BYTECODE_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
91 [ BYTECODE_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
92 [ BYTECODE_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
93 [ BYTECODE_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
96 [ BYTECODE_OP_AND
] = "AND",
97 [ BYTECODE_OP_OR
] = "OR",
100 [ BYTECODE_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
101 [ BYTECODE_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
102 [ BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
103 [ BYTECODE_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
104 [ BYTECODE_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
106 /* load from immediate operand */
107 [ BYTECODE_OP_LOAD_STRING
] = "LOAD_STRING",
108 [ BYTECODE_OP_LOAD_S64
] = "LOAD_S64",
109 [ BYTECODE_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
112 [ BYTECODE_OP_CAST_TO_S64
] = "CAST_TO_S64",
113 [ BYTECODE_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
114 [ BYTECODE_OP_CAST_NOP
] = "CAST_NOP",
116 /* get context ref */
117 [ BYTECODE_OP_GET_CONTEXT_REF
] = "GET_CONTEXT_REF",
118 [ BYTECODE_OP_GET_CONTEXT_REF_STRING
] = "GET_CONTEXT_REF_STRING",
119 [ BYTECODE_OP_GET_CONTEXT_REF_S64
] = "GET_CONTEXT_REF_S64",
120 [ BYTECODE_OP_GET_CONTEXT_REF_DOUBLE
] = "GET_CONTEXT_REF_DOUBLE",
122 /* load userspace field ref */
123 [ BYTECODE_OP_LOAD_FIELD_REF_USER_STRING
] = "LOAD_FIELD_REF_USER_STRING",
124 [ BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE
] = "LOAD_FIELD_REF_USER_SEQUENCE",
127 * load immediate star globbing pattern (literal string)
130 [ BYTECODE_OP_LOAD_STAR_GLOB_STRING
] = "LOAD_STAR_GLOB_STRING",
132 /* globbing pattern binary operator: apply to */
133 [ BYTECODE_OP_EQ_STAR_GLOB_STRING
] = "EQ_STAR_GLOB_STRING",
134 [ BYTECODE_OP_NE_STAR_GLOB_STRING
] = "NE_STAR_GLOB_STRING",
137 * Instructions for recursive traversal through composed types.
139 [ BYTECODE_OP_GET_CONTEXT_ROOT
] = "GET_CONTEXT_ROOT",
140 [ BYTECODE_OP_GET_APP_CONTEXT_ROOT
] = "GET_APP_CONTEXT_ROOT",
141 [ BYTECODE_OP_GET_PAYLOAD_ROOT
] = "GET_PAYLOAD_ROOT",
143 [ BYTECODE_OP_GET_SYMBOL
] = "GET_SYMBOL",
144 [ BYTECODE_OP_GET_SYMBOL_FIELD
] = "GET_SYMBOL_FIELD",
145 [ BYTECODE_OP_GET_INDEX_U16
] = "GET_INDEX_U16",
146 [ BYTECODE_OP_GET_INDEX_U64
] = "GET_INDEX_U64",
148 [ BYTECODE_OP_LOAD_FIELD
] = "LOAD_FIELD",
149 [ BYTECODE_OP_LOAD_FIELD_S8
] = "LOAD_FIELD_S8",
150 [ BYTECODE_OP_LOAD_FIELD_S16
] = "LOAD_FIELD_S16",
151 [ BYTECODE_OP_LOAD_FIELD_S32
] = "LOAD_FIELD_S32",
152 [ BYTECODE_OP_LOAD_FIELD_S64
] = "LOAD_FIELD_S64",
153 [ BYTECODE_OP_LOAD_FIELD_U8
] = "LOAD_FIELD_U8",
154 [ BYTECODE_OP_LOAD_FIELD_U16
] = "LOAD_FIELD_U16",
155 [ BYTECODE_OP_LOAD_FIELD_U32
] = "LOAD_FIELD_U32",
156 [ BYTECODE_OP_LOAD_FIELD_U64
] = "LOAD_FIELD_U64",
157 [ BYTECODE_OP_LOAD_FIELD_STRING
] = "LOAD_FIELD_STRING",
158 [ BYTECODE_OP_LOAD_FIELD_SEQUENCE
] = "LOAD_FIELD_SEQUENCE",
159 [ BYTECODE_OP_LOAD_FIELD_DOUBLE
] = "LOAD_FIELD_DOUBLE",
161 [ BYTECODE_OP_UNARY_BIT_NOT
] = "UNARY_BIT_NOT",
163 [ BYTECODE_OP_RETURN_S64
] = "RETURN_S64",
166 const char *lttng_bytecode_print_op(enum bytecode_op op
)
168 if (op
>= NR_BYTECODE_OPS
)
175 int apply_field_reloc(const struct lttng_ust_event_desc
*event_desc
,
176 struct bytecode_runtime
*runtime
,
177 uint32_t runtime_len
__attribute__((unused
)),
178 uint32_t reloc_offset
,
179 const char *field_name
,
180 enum bytecode_op bytecode_op
)
182 const struct lttng_ust_event_field
**fields
, *field
= NULL
;
183 unsigned int nr_fields
, i
;
185 uint32_t field_offset
= 0;
187 dbg_printf("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
189 /* Lookup event by name */
192 fields
= event_desc
->fields
;
195 nr_fields
= event_desc
->nr_fields
;
196 for (i
= 0; i
< nr_fields
; i
++) {
197 if (fields
[i
]->nofilter
) {
200 if (!strcmp(fields
[i
]->name
, field_name
)) {
204 /* compute field offset */
205 switch (fields
[i
]->type
->type
) {
206 case lttng_ust_type_integer
:
207 case lttng_ust_type_enum
:
208 field_offset
+= sizeof(int64_t);
210 case lttng_ust_type_array
:
211 case lttng_ust_type_sequence
:
212 field_offset
+= sizeof(unsigned long);
213 field_offset
+= sizeof(void *);
215 case lttng_ust_type_string
:
216 field_offset
+= sizeof(void *);
218 case lttng_ust_type_float
:
219 field_offset
+= sizeof(double);
228 /* Check if field offset is too large for 16-bit offset */
229 if (field_offset
> LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
- 1)
233 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
235 switch (bytecode_op
) {
236 case BYTECODE_OP_LOAD_FIELD_REF
:
238 struct field_ref
*field_ref
;
240 field_ref
= (struct field_ref
*) op
->data
;
241 switch (field
->type
->type
) {
242 case lttng_ust_type_integer
:
243 case lttng_ust_type_enum
:
244 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_S64
;
246 case lttng_ust_type_array
:
248 struct lttng_ust_type_array
*array
= (struct lttng_ust_type_array
*) field
->type
;
250 if (array
->encoding
== lttng_ust_string_encoding_none
)
252 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
;
255 case lttng_ust_type_sequence
:
257 struct lttng_ust_type_sequence
*sequence
= (struct lttng_ust_type_sequence
*) field
->type
;
259 if (sequence
->encoding
== lttng_ust_string_encoding_none
)
261 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
;
264 case lttng_ust_type_string
:
265 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_STRING
;
267 case lttng_ust_type_float
:
268 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_DOUBLE
;
274 field_ref
->offset
= (uint16_t) field_offset
;
284 int apply_context_reloc(struct bytecode_runtime
*runtime
,
285 uint32_t runtime_len
__attribute__((unused
)),
286 uint32_t reloc_offset
,
287 const char *context_name
,
288 enum bytecode_op bytecode_op
)
291 const struct lttng_ust_ctx_field
*ctx_field
;
293 struct lttng_ust_ctx
**pctx
= runtime
->p
.pctx
;
295 dbg_printf("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
297 /* Get context index */
298 idx
= lttng_get_context_index(*pctx
, context_name
);
300 if (lttng_context_is_app(context_name
)) {
303 ret
= lttng_ust_add_app_context_to_ctx_rcu(context_name
,
307 idx
= lttng_get_context_index(*pctx
, context_name
);
314 /* Check if idx is too large for 16-bit offset */
315 if (idx
> LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
- 1)
318 /* Get context return type */
319 ctx_field
= &(*pctx
)->fields
[idx
];
320 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
322 switch (bytecode_op
) {
323 case BYTECODE_OP_GET_CONTEXT_REF
:
325 struct field_ref
*field_ref
;
327 field_ref
= (struct field_ref
*) op
->data
;
328 switch (ctx_field
->event_field
->type
->type
) {
329 case lttng_ust_type_integer
:
330 case lttng_ust_type_enum
:
331 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_S64
;
333 /* Sequence and array supported only as string */
334 case lttng_ust_type_array
:
336 struct lttng_ust_type_array
*array
= (struct lttng_ust_type_array
*) ctx_field
->event_field
->type
;
338 if (array
->encoding
== lttng_ust_string_encoding_none
)
340 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
343 case lttng_ust_type_sequence
:
345 struct lttng_ust_type_sequence
*sequence
= (struct lttng_ust_type_sequence
*) ctx_field
->event_field
->type
;
347 if (sequence
->encoding
== lttng_ust_string_encoding_none
)
349 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
352 case lttng_ust_type_string
:
353 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
355 case lttng_ust_type_float
:
356 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_DOUBLE
;
358 case lttng_ust_type_dynamic
:
359 op
->op
= BYTECODE_OP_GET_CONTEXT_REF
;
364 /* set offset to context index within channel contexts */
365 field_ref
->offset
= (uint16_t) idx
;
375 int apply_reloc(const struct lttng_ust_event_desc
*event_desc
,
376 struct bytecode_runtime
*runtime
,
377 uint32_t runtime_len
,
378 uint32_t reloc_offset
,
383 dbg_printf("Apply reloc: %u %s\n", reloc_offset
, name
);
385 /* Ensure that the reloc is within the code */
386 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
389 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
391 case BYTECODE_OP_LOAD_FIELD_REF
:
392 return apply_field_reloc(event_desc
, runtime
, runtime_len
,
393 reloc_offset
, name
, op
->op
);
394 case BYTECODE_OP_GET_CONTEXT_REF
:
395 return apply_context_reloc(runtime
, runtime_len
,
396 reloc_offset
, name
, op
->op
);
397 case BYTECODE_OP_GET_SYMBOL
:
398 case BYTECODE_OP_GET_SYMBOL_FIELD
:
400 * Will be handled by load specialize phase or
401 * dynamically by interpreter.
405 ERR("Unknown reloc op type %u\n", op
->op
);
412 int bytecode_is_linked(struct lttng_ust_bytecode_node
*bytecode
,
413 struct cds_list_head
*bytecode_runtime_head
)
415 struct lttng_ust_bytecode_runtime
*bc_runtime
;
417 cds_list_for_each_entry(bc_runtime
, bytecode_runtime_head
, node
) {
418 if (bc_runtime
->bc
== bytecode
)
425 * Take a bytecode with reloc table and link it to an event to create a
429 int link_bytecode(const struct lttng_ust_event_desc
*event_desc
,
430 struct lttng_ust_ctx
**ctx
,
431 struct lttng_ust_bytecode_node
*bytecode
,
432 struct cds_list_head
*bytecode_runtime_head
,
433 struct cds_list_head
*insert_loc
)
435 int ret
, offset
, next_offset
;
436 struct bytecode_runtime
*runtime
= NULL
;
437 size_t runtime_alloc_len
;
441 /* Bytecode already linked */
442 if (bytecode_is_linked(bytecode
, bytecode_runtime_head
))
445 dbg_printf("Linking...\n");
447 /* We don't need the reloc table in the runtime */
448 runtime_alloc_len
= sizeof(*runtime
) + bytecode
->bc
.reloc_offset
;
449 runtime
= zmalloc(runtime_alloc_len
);
454 runtime
->p
.type
= bytecode
->type
;
455 runtime
->p
.bc
= bytecode
;
456 runtime
->p
.pctx
= ctx
;
457 runtime
->len
= bytecode
->bc
.reloc_offset
;
458 /* copy original bytecode */
459 memcpy(runtime
->code
, bytecode
->bc
.data
, runtime
->len
);
461 * apply relocs. Those are a uint16_t (offset in bytecode)
462 * followed by a string (field name).
464 for (offset
= bytecode
->bc
.reloc_offset
;
465 offset
< bytecode
->bc
.len
;
466 offset
= next_offset
) {
467 uint16_t reloc_offset
=
468 *(uint16_t *) &bytecode
->bc
.data
[offset
];
470 (const char *) &bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
472 ret
= apply_reloc(event_desc
, runtime
, runtime
->len
, reloc_offset
, name
);
476 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
478 /* Validate bytecode */
479 ret
= lttng_bytecode_validate(runtime
);
483 /* Specialize bytecode */
484 ret
= lttng_bytecode_specialize(event_desc
, runtime
);
489 runtime
->p
.interpreter_func
= lttng_bytecode_interpret
;
490 runtime
->p
.link_failed
= 0;
491 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
492 dbg_printf("Linking successful.\n");
496 runtime
->p
.interpreter_func
= lttng_bytecode_interpret_error
;
497 runtime
->p
.link_failed
= 1;
498 cds_list_add_rcu(&runtime
->p
.node
, insert_loc
);
500 dbg_printf("Linking failed.\n");
504 void lttng_bytecode_sync_state(struct lttng_ust_bytecode_runtime
*runtime
)
506 struct lttng_ust_bytecode_node
*bc
= runtime
->bc
;
508 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
509 runtime
->interpreter_func
= lttng_bytecode_interpret_error
;
511 runtime
->interpreter_func
= lttng_bytecode_interpret
;
515 * Given the lists of bytecode programs of an instance (trigger or event) and
516 * of a matching enabler, try to link all the enabler's bytecode programs with
519 * This function is called after we confirmed that name enabler and the
520 * instance are name matching (or glob pattern matching).
522 void lttng_enabler_link_bytecode(const struct lttng_ust_event_desc
*event_desc
,
523 struct lttng_ust_ctx
**ctx
,
524 struct cds_list_head
*instance_bytecode_head
,
525 struct cds_list_head
*enabler_bytecode_head
)
527 struct lttng_ust_bytecode_node
*enabler_bc
;
528 struct lttng_ust_bytecode_runtime
*runtime
;
532 /* Go over all the bytecode programs of the enabler. */
533 cds_list_for_each_entry(enabler_bc
, enabler_bytecode_head
, node
) {
535 struct cds_list_head
*insert_loc
;
538 * Check if the current enabler bytecode program is already
539 * linked with the instance.
541 cds_list_for_each_entry(runtime
, instance_bytecode_head
, node
) {
542 if (runtime
->bc
== enabler_bc
) {
549 * Skip bytecode already linked, go to the next enabler
556 * Insert at specified priority (seqnum) in increasing
557 * order. If there already is a bytecode of the same priority,
558 * insert the new bytecode right after it.
560 cds_list_for_each_entry_reverse(runtime
,
561 instance_bytecode_head
, node
) {
562 if (runtime
->bc
->bc
.seqnum
<= enabler_bc
->bc
.seqnum
) {
564 insert_loc
= &runtime
->node
;
569 /* Add to head to list */
570 insert_loc
= instance_bytecode_head
;
572 dbg_printf("linking bytecode\n");
573 ret
= link_bytecode(event_desc
, ctx
, enabler_bc
, instance_bytecode_head
, insert_loc
);
575 dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
581 void free_filter_runtime(struct cds_list_head
*bytecode_runtime_head
)
583 struct bytecode_runtime
*runtime
, *tmp
;
585 cds_list_for_each_entry_safe(runtime
, tmp
, bytecode_runtime_head
,
592 void lttng_free_event_filter_runtime(struct lttng_ust_event_common
*event
)
594 free_filter_runtime(&event
->priv
->filter_bytecode_runtime_head
);