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
,
117 OBJECT_TYPE_STRING_SEQUENCE
,
119 OBJECT_TYPE_SEQUENCE
,
127 struct bytecode_get_index_data
{
128 uint64_t offset
; /* in bytes */
132 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
133 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
134 * interpreter needs to find it from the event fields and types to
137 const struct lttng_event_field
*field
;
140 enum object_type type
;
141 bool rev_bo
; /* reverse byte order */
145 /* Validation stack */
148 enum object_type object_type
;
149 const struct lttng_event_field
*field
;
150 bool rev_bo
; /* reverse byte order */
153 struct vstack_entry
{
154 enum entry_type type
;
155 struct vstack_load load
;
159 int top
; /* top of stack */
160 struct vstack_entry e
[INTERPRETER_STACK_LEN
];
164 void vstack_init(struct vstack
*stack
)
170 struct vstack_entry
*vstack_ax(struct vstack
*stack
)
172 if (unlikely(stack
->top
< 0))
174 return &stack
->e
[stack
->top
];
178 struct vstack_entry
*vstack_bx(struct vstack
*stack
)
180 if (unlikely(stack
->top
< 1))
182 return &stack
->e
[stack
->top
- 1];
186 int vstack_push(struct vstack
*stack
)
188 if (stack
->top
>= INTERPRETER_STACK_LEN
- 1) {
197 int vstack_pop(struct vstack
*stack
)
199 if (unlikely(stack
->top
< 0)) {
200 ERR("Stack empty\n");
207 /* Execution stack */
208 enum estack_string_literal_type
{
209 ESTACK_STRING_LITERAL_TYPE_NONE
,
210 ESTACK_STRING_LITERAL_TYPE_PLAIN
,
211 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB
,
216 enum object_type object_type
;
220 /* Temporary place-holders for contexts. */
226 const struct lttng_event_field
*field
;
229 struct estack_entry
{
230 enum entry_type type
; /* For dynamic typing. */
238 enum estack_string_literal_type literal_type
;
245 int top
; /* top of stack */
246 struct estack_entry e
[INTERPRETER_STACK_LEN
];
250 * Always use aliased type for ax/bx (top of stack).
251 * When ax/bx are S64, use aliased value.
253 #define estack_ax_v ax
254 #define estack_bx_v bx
255 #define estack_ax_t ax_t
256 #define estack_bx_t bx_t
259 * ax and bx registers can hold either integer, double or string.
261 #define estack_ax(stack, top) \
263 assert((top) > INTERPRETER_STACK_EMPTY); \
267 #define estack_bx(stack, top) \
269 assert((top) > INTERPRETER_STACK_EMPTY + 1); \
270 &(stack)->e[(top) - 1]; \
274 * Currently, only integers (REG_S64) can be pushed into the stack.
276 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
278 assert((top) < INTERPRETER_STACK_LEN - 1); \
279 (stack)->e[(top) - 1].u.v = (bx); \
280 (stack)->e[(top) - 1].type = (bx_t); \
286 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
288 assert((top) > INTERPRETER_STACK_EMPTY); \
291 (bx) = (stack)->e[(top) - 2].u.v; \
292 (bx_t) = (stack)->e[(top) - 2].type; \
296 enum lttng_interpreter_type
{
297 LTTNG_INTERPRETER_TYPE_S64
,
298 LTTNG_INTERPRETER_TYPE_U64
,
299 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM
,
300 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM
,
301 LTTNG_INTERPRETER_TYPE_DOUBLE
,
302 LTTNG_INTERPRETER_TYPE_STRING
,
303 LTTNG_INTERPRETER_TYPE_SEQUENCE
,
307 * Represents the output parameter of the lttng interpreter.
308 * Currently capturable field classes are integer, double, string and sequence
311 struct lttng_interpreter_output
{
312 enum lttng_interpreter_type type
;
327 const struct lttng_type
*nested_type
;
332 const char *print_op(enum bytecode_op op
);
334 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime
*runtime
);
336 int lttng_bytecode_validate(struct bytecode_runtime
*bytecode
);
337 int lttng_bytecode_specialize(const struct lttng_event_desc
*event_desc
,
338 struct bytecode_runtime
*bytecode
);
340 uint64_t lttng_bytecode_filter_interpret_false(void *filter_data
,
341 const char *filter_stack_data
);
342 uint64_t lttng_bytecode_filter_interpret(void *filter_data
,
343 const char *filter_stack_data
);
345 #endif /* _LTTNG_BYTECODE_H */