1 #ifndef _LTTNG_BYTECODE_H
2 #define _LTTNG_BYTECODE_H
7 * LTTng UST bytecode header.
9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <lttng/ust-events.h>
35 #include <lttng/ust-context-provider.h>
42 #include <usterr-signal-safe.h>
45 /* Interpreter stack length, in number of entries */
46 #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */
47 #define INTERPRETER_STACK_EMPTY 1
49 #define BYTECODE_MAX_DATA_LEN 65536
52 #define min_t(type, a, b) \
53 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
57 #define likely(x) __builtin_expect(!!(x), 1)
61 #define unlikely(x) __builtin_expect(!!(x), 0)
65 #define dbg_printf(fmt, args...) \
66 printf("[debug bytecode in %s:%s@%u] " fmt, \
67 __FILE__, __func__, __LINE__, ## args)
69 #define dbg_printf(fmt, args...) \
71 /* do nothing but check printf format */ \
73 printf("[debug bytecode in %s:%s@%u] " fmt, \
74 __FILE__, __func__, __LINE__, ## args); \
78 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
79 struct bytecode_runtime
{
80 struct lttng_bytecode_runtime p
;
82 size_t data_alloc_len
;
100 LOAD_ROOT_APP_CONTEXT
,
115 OBJECT_TYPE_SIGNED_ENUM
,
116 OBJECT_TYPE_UNSIGNED_ENUM
,
120 OBJECT_TYPE_STRING_SEQUENCE
,
122 OBJECT_TYPE_SEQUENCE
,
130 struct bytecode_get_index_data
{
131 uint64_t offset
; /* in bytes */
135 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
136 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
137 * interpreter needs to find it from the event fields and types to
140 const struct lttng_event_field
*field
;
143 enum object_type type
;
144 bool rev_bo
; /* reverse byte order */
148 /* Validation stack */
151 enum object_type object_type
;
152 const struct lttng_event_field
*field
;
153 bool rev_bo
; /* reverse byte order */
156 struct vstack_entry
{
157 enum entry_type type
;
158 struct vstack_load load
;
162 int top
; /* top of stack */
163 struct vstack_entry e
[INTERPRETER_STACK_LEN
];
167 void vstack_init(struct vstack
*stack
)
173 struct vstack_entry
*vstack_ax(struct vstack
*stack
)
175 if (unlikely(stack
->top
< 0))
177 return &stack
->e
[stack
->top
];
181 struct vstack_entry
*vstack_bx(struct vstack
*stack
)
183 if (unlikely(stack
->top
< 1))
185 return &stack
->e
[stack
->top
- 1];
189 int vstack_push(struct vstack
*stack
)
191 if (stack
->top
>= INTERPRETER_STACK_LEN
- 1) {
200 int vstack_pop(struct vstack
*stack
)
202 if (unlikely(stack
->top
< 0)) {
203 ERR("Stack empty\n");
210 /* Execution stack */
211 enum estack_string_literal_type
{
212 ESTACK_STRING_LITERAL_TYPE_NONE
,
213 ESTACK_STRING_LITERAL_TYPE_PLAIN
,
214 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB
,
219 enum object_type object_type
;
223 /* Temporary place-holders for contexts. */
229 const struct lttng_event_field
*field
;
232 struct estack_entry
{
233 enum entry_type type
; /* For dynamic typing. */
241 enum estack_string_literal_type literal_type
;
248 int top
; /* top of stack */
249 struct estack_entry e
[INTERPRETER_STACK_LEN
];
253 * Always use aliased type for ax/bx (top of stack).
254 * When ax/bx are S64, use aliased value.
256 #define estack_ax_v ax
257 #define estack_bx_v bx
258 #define estack_ax_t ax_t
259 #define estack_bx_t bx_t
262 * ax and bx registers can hold either integer, double or string.
264 #define estack_ax(stack, top) \
266 assert((top) > INTERPRETER_STACK_EMPTY); \
270 #define estack_bx(stack, top) \
272 assert((top) > INTERPRETER_STACK_EMPTY + 1); \
273 &(stack)->e[(top) - 1]; \
277 * Currently, only integers (REG_S64) can be pushed into the stack.
279 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
281 assert((top) < INTERPRETER_STACK_LEN - 1); \
282 (stack)->e[(top) - 1].u.v = (bx); \
283 (stack)->e[(top) - 1].type = (bx_t); \
289 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
291 assert((top) > INTERPRETER_STACK_EMPTY); \
294 (bx) = (stack)->e[(top) - 2].u.v; \
295 (bx_t) = (stack)->e[(top) - 2].type; \
299 enum lttng_interpreter_type
{
300 LTTNG_INTERPRETER_TYPE_S64
,
301 LTTNG_INTERPRETER_TYPE_U64
,
302 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
,
303 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
,
304 LTTNG_INTERPRETER_TYPE_DOUBLE
,
305 LTTNG_INTERPRETER_TYPE_STRING
,
306 LTTNG_INTERPRETER_TYPE_SEQUENCE
,
310 * Represents the output parameter of the lttng interpreter.
311 * Currently capturable field classes are integer, double, string and sequence
314 struct lttng_interpreter_output
{
315 enum lttng_interpreter_type type
;
330 const struct lttng_type
*nested_type
;
335 const char *print_op(enum bytecode_op op
);
337 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime
*runtime
);
338 void lttng_bytecode_capture_sync_state(struct lttng_bytecode_runtime
*runtime
);
340 int lttng_bytecode_validate(struct bytecode_runtime
*bytecode
);
341 int lttng_bytecode_specialize(const struct lttng_event_desc
*event_desc
,
342 struct bytecode_runtime
*bytecode
);
344 uint64_t lttng_bytecode_filter_interpret_false(void *filter_data
,
345 const char *filter_stack_data
);
346 uint64_t lttng_bytecode_filter_interpret(void *filter_data
,
347 const char *filter_stack_data
);
349 uint64_t lttng_bytecode_capture_interpret_false(void *capture_data
,
350 const char *capture_stack_data
,
351 struct lttng_interpreter_output
*output
);
352 uint64_t lttng_bytecode_capture_interpret(void *capture_data
,
353 const char *capture_stack_data
,
354 struct lttng_interpreter_output
*output
);
356 #endif /* _LTTNG_BYTECODE_H */