2 * Copyright 2020 EfficiOS, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "event-expr-to-bytecode.h"
11 #include <lttng/event-expr.h>
12 #include <common/bytecode/bytecode.h>
16 int event_expr_to_bytecode_recursive(const struct lttng_event_expr
*expr
,
17 struct lttng_bytecode_alloc
**bytecode
,
18 struct lttng_bytecode_alloc
**bytecode_reloc
)
20 enum lttng_event_expr_status event_expr_status
;
23 switch (lttng_event_expr_get_type(expr
)) {
24 case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD
:
28 status
= bytecode_push_get_payload_root(bytecode
);
33 name
= lttng_event_expr_event_payload_field_get_name(expr
);
39 status
= bytecode_push_get_symbol(bytecode
, bytecode_reloc
, name
);
47 case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD
:
51 status
= bytecode_push_get_context_root(bytecode
);
56 name
= lttng_event_expr_channel_context_field_get_name(expr
);
62 status
= bytecode_push_get_symbol(bytecode
, bytecode_reloc
, name
);
70 case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD
:
72 const char *provider_name
, *type_name
;
76 status
= bytecode_push_get_app_context_root(bytecode
);
81 provider_name
= lttng_event_expr_app_specific_context_field_get_provider_name(expr
);
87 type_name
= lttng_event_expr_app_specific_context_field_get_type_name(expr
);
93 /* Reconstitute the app context field name from its two parts. */
94 ret
= asprintf(&name
, "%s:%s", provider_name
, type_name
);
100 status
= bytecode_push_get_symbol(bytecode
, bytecode_reloc
, name
);
109 case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT
:
111 const struct lttng_event_expr
*parent
;
114 parent
= lttng_event_expr_array_field_element_get_parent_expr(expr
);
120 status
= event_expr_to_bytecode_recursive(parent
, bytecode
, bytecode_reloc
);
125 event_expr_status
= lttng_event_expr_array_field_element_get_index(
127 if (event_expr_status
!= LTTNG_EVENT_EXPR_STATUS_OK
) {
132 status
= bytecode_push_get_index_u64(bytecode
, index
);
150 int lttng_event_expr_to_bytecode(const struct lttng_event_expr
*expr
,
151 struct lttng_bytecode
**bytecode_out
)
153 struct lttng_bytecode_alloc
*bytecode
= NULL
;
154 struct lttng_bytecode_alloc
*bytecode_reloc
= NULL
;
155 struct return_op ret_insn
;
158 status
= bytecode_init(&bytecode
);
163 status
= bytecode_init(&bytecode_reloc
);
168 status
= event_expr_to_bytecode_recursive (expr
, &bytecode
, &bytecode_reloc
);
173 ret_insn
.op
= BYTECODE_OP_RETURN
;
174 bytecode_push(&bytecode
, &ret_insn
, 1, sizeof(ret_insn
));
176 /* Append symbol table to bytecode. */
177 bytecode
->b
.reloc_table_offset
= bytecode_get_len(&bytecode
->b
);
178 status
= bytecode_push(&bytecode
, bytecode_reloc
->b
.data
,
179 1, bytecode_get_len(&bytecode_reloc
->b
));
184 /* Copy the `lttng_bytecode` out of the `lttng_bytecode_alloc`. */
185 *bytecode_out
= bytecode_copy(&bytecode
->b
);
186 if (!*bytecode_out
) {
196 if (bytecode_reloc
) {
197 free(bytecode_reloc
);