bytecode: rename `lttng_filter_sync_state()` -> `lttng_bytecode_filter_sync_state()`
[lttng-ust.git] / liblttng-ust / lttng-bytecode.h
CommitLineData
04aa13f8
FD
1#ifndef _LTTNG_BYTECODE_H
2#define _LTTNG_BYTECODE_H
97b58163
MD
3
4/*
04aa13f8 5 * lttng-bytecode.h
97b58163 6 *
04aa13f8 7 * LTTng UST bytecode header.
97b58163 8 *
7e50015d 9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
97b58163 10 *
7e50015d
MD
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:
97b58163 17 *
7e50015d
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
97b58163 20 *
7e50015d
MD
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
27 * SOFTWARE.
97b58163
MD
28 */
29
30#include <errno.h>
31#include <stdio.h>
91ad5118 32#include <stdbool.h>
97b58163
MD
33#include <helper.h>
34#include <lttng/ust-events.h>
53569322 35#include <lttng/ust-context-provider.h>
97b58163 36#include <stdint.h>
0305960f 37#include <assert.h>
97b58163
MD
38#include <errno.h>
39#include <string.h>
40#include <inttypes.h>
41#include <limits.h>
42#include <usterr-signal-safe.h>
04aa13f8 43#include "bytecode.h"
97b58163 44
04aa13f8
FD
45/* Interpreter stack length, in number of entries */
46#define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */
47#define INTERPRETER_STACK_EMPTY 1
97b58163 48
04aa13f8 49#define BYTECODE_MAX_DATA_LEN 65536
47e5f13e 50
97b58163
MD
51#ifndef min_t
52#define min_t(type, a, b) \
53 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
54#endif
55
56#ifndef likely
57#define likely(x) __builtin_expect(!!(x), 1)
58#endif
59
60#ifndef unlikely
61#define unlikely(x) __builtin_expect(!!(x), 0)
62#endif
63
64#ifdef DEBUG
f488575f
MD
65#define dbg_printf(fmt, args...) \
66 printf("[debug bytecode in %s:%s@%u] " fmt, \
67 __FILE__, __func__, __LINE__, ## args)
97b58163
MD
68#else
69#define dbg_printf(fmt, args...) \
70do { \
71 /* do nothing but check printf format */ \
72 if (0) \
f488575f
MD
73 printf("[debug bytecode in %s:%s@%u] " fmt, \
74 __FILE__, __func__, __LINE__, ## args); \
97b58163
MD
75} while (0)
76#endif
77
f488575f 78/* Linked bytecode. Child of struct lttng_bytecode_runtime. */
97b58163 79struct bytecode_runtime {
f488575f 80 struct lttng_bytecode_runtime p;
47e5f13e
MD
81 size_t data_len;
82 size_t data_alloc_len;
83 char *data;
97b58163 84 uint16_t len;
47e5f13e 85 char code[0];
97b58163
MD
86};
87
0305960f 88enum entry_type {
97b58163 89 REG_S64,
d97f9b78 90 REG_U64,
97b58163
MD
91 REG_DOUBLE,
92 REG_STRING,
3151a51d 93 REG_STAR_GLOB_STRING,
53569322 94 REG_UNKNOWN,
47e5f13e
MD
95 REG_PTR,
96};
97
98enum load_type {
99 LOAD_ROOT_CONTEXT,
100 LOAD_ROOT_APP_CONTEXT,
101 LOAD_ROOT_PAYLOAD,
102 LOAD_OBJECT,
103};
104
105enum object_type {
106 OBJECT_TYPE_S8,
107 OBJECT_TYPE_S16,
108 OBJECT_TYPE_S32,
109 OBJECT_TYPE_S64,
110 OBJECT_TYPE_U8,
111 OBJECT_TYPE_U16,
112 OBJECT_TYPE_U32,
113 OBJECT_TYPE_U64,
114
115 OBJECT_TYPE_DOUBLE,
116 OBJECT_TYPE_STRING,
117 OBJECT_TYPE_STRING_SEQUENCE,
118
119 OBJECT_TYPE_SEQUENCE,
120 OBJECT_TYPE_ARRAY,
121 OBJECT_TYPE_STRUCT,
122 OBJECT_TYPE_VARIANT,
123
124 OBJECT_TYPE_DYNAMIC,
125};
126
04aa13f8 127struct bytecode_get_index_data {
47e5f13e
MD
128 uint64_t offset; /* in bytes */
129 size_t ctx_index;
130 size_t array_len;
f3503ba9
FD
131 /*
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
135 * support variants.
136 */
137 const struct lttng_event_field *field;
47e5f13e
MD
138 struct {
139 size_t len;
140 enum object_type type;
141 bool rev_bo; /* reverse byte order */
142 } elem;
97b58163
MD
143};
144
0305960f 145/* Validation stack */
47e5f13e
MD
146struct vstack_load {
147 enum load_type type;
148 enum object_type object_type;
149 const struct lttng_event_field *field;
150 bool rev_bo; /* reverse byte order */
151};
152
0305960f
MD
153struct vstack_entry {
154 enum entry_type type;
47e5f13e 155 struct vstack_load load;
97b58163
MD
156};
157
0305960f
MD
158struct vstack {
159 int top; /* top of stack */
04aa13f8 160 struct vstack_entry e[INTERPRETER_STACK_LEN];
0305960f
MD
161};
162
163static inline
164void vstack_init(struct vstack *stack)
165{
166 stack->top = -1;
167}
168
169static inline
170struct vstack_entry *vstack_ax(struct vstack *stack)
171{
172 if (unlikely(stack->top < 0))
173 return NULL;
174 return &stack->e[stack->top];
175}
176
177static inline
178struct vstack_entry *vstack_bx(struct vstack *stack)
179{
180 if (unlikely(stack->top < 1))
181 return NULL;
182 return &stack->e[stack->top - 1];
183}
184
185static inline
186int vstack_push(struct vstack *stack)
187{
04aa13f8 188 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
0305960f
MD
189 ERR("Stack full\n");
190 return -EINVAL;
191 }
192 ++stack->top;
193 return 0;
194}
195
196static inline
197int vstack_pop(struct vstack *stack)
198{
199 if (unlikely(stack->top < 0)) {
200 ERR("Stack empty\n");
201 return -EINVAL;
202 }
203 stack->top--;
204 return 0;
205}
206
207/* Execution stack */
3151a51d
PP
208enum estack_string_literal_type {
209 ESTACK_STRING_LITERAL_TYPE_NONE,
210 ESTACK_STRING_LITERAL_TYPE_PLAIN,
211 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
212};
213
47e5f13e
MD
214struct load_ptr {
215 enum load_type type;
216 enum object_type object_type;
217 const void *ptr;
f3503ba9 218 size_t nr_elem;
47e5f13e
MD
219 bool rev_bo;
220 /* Temporary place-holders for contexts. */
221 union {
222 int64_t s64;
223 uint64_t u64;
224 double d;
225 } u;
47e5f13e
MD
226 const struct lttng_event_field *field;
227};
228
0305960f 229struct estack_entry {
53569322 230 enum entry_type type; /* For dynamic typing. */
0305960f
MD
231 union {
232 int64_t v;
233 double d;
234
235 struct {
236 const char *str;
9b33aac4 237 size_t seq_len;
3151a51d 238 enum estack_string_literal_type literal_type;
0305960f 239 } s;
47e5f13e 240 struct load_ptr ptr;
0305960f
MD
241 } u;
242};
97b58163 243
0305960f
MD
244struct estack {
245 int top; /* top of stack */
04aa13f8 246 struct estack_entry e[INTERPRETER_STACK_LEN];
97b58163
MD
247};
248
53569322
MD
249/*
250 * Always use aliased type for ax/bx (top of stack).
251 * When ax/bx are S64, use aliased value.
252 */
9b33aac4
MD
253#define estack_ax_v ax
254#define estack_bx_v bx
53569322
MD
255#define estack_ax_t ax_t
256#define estack_bx_t bx_t
9b33aac4 257
53569322
MD
258/*
259 * ax and bx registers can hold either integer, double or string.
260 */
9b33aac4
MD
261#define estack_ax(stack, top) \
262 ({ \
04aa13f8 263 assert((top) > INTERPRETER_STACK_EMPTY); \
9b33aac4
MD
264 &(stack)->e[top]; \
265 })
266
267#define estack_bx(stack, top) \
268 ({ \
04aa13f8 269 assert((top) > INTERPRETER_STACK_EMPTY + 1); \
9b33aac4
MD
270 &(stack)->e[(top) - 1]; \
271 })
272
53569322
MD
273/*
274 * Currently, only integers (REG_S64) can be pushed into the stack.
275 */
276#define estack_push(stack, top, ax, bx, ax_t, bx_t) \
9b33aac4 277 do { \
04aa13f8 278 assert((top) < INTERPRETER_STACK_LEN - 1); \
9b33aac4 279 (stack)->e[(top) - 1].u.v = (bx); \
53569322 280 (stack)->e[(top) - 1].type = (bx_t); \
9b33aac4 281 (bx) = (ax); \
53569322 282 (bx_t) = (ax_t); \
9b33aac4
MD
283 ++(top); \
284 } while (0)
285
53569322 286#define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
9b33aac4 287 do { \
04aa13f8 288 assert((top) > INTERPRETER_STACK_EMPTY); \
9b33aac4 289 (ax) = (bx); \
53569322 290 (ax_t) = (bx_t); \
9b33aac4 291 (bx) = (stack)->e[(top) - 2].u.v; \
53569322 292 (bx_t) = (stack)->e[(top) - 2].type; \
9b33aac4
MD
293 (top)--; \
294 } while (0)
0305960f 295
f3503ba9
FD
296enum 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,
304};
305
306/*
307 * Represents the output parameter of the lttng interpreter.
308 * Currently capturable field classes are integer, double, string and sequence
309 * of integer.
310 */
311struct lttng_interpreter_output {
312 enum lttng_interpreter_type type;
313 union {
314 int64_t s;
315 uint64_t u;
316 double d;
317
318 struct {
319 const char *str;
320 size_t len;
321 } str;
322 struct {
323 const void *ptr;
324 size_t nr_elem;
325
326 /* Inner type. */
327 const struct lttng_type *nested_type;
328 } sequence;
329 } u;
330};
331
04aa13f8 332const char *print_op(enum bytecode_op op);
97b58163 333
4d451c15
FD
334void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime);
335
04aa13f8
FD
336int lttng_bytecode_validate(struct bytecode_runtime *bytecode);
337int lttng_bytecode_specialize(const struct lttng_event_desc *event_desc,
47e5f13e 338 struct bytecode_runtime *bytecode);
97b58163 339
04aa13f8 340uint64_t lttng_bytecode_filter_interpret_false(void *filter_data,
97b58163 341 const char *filter_stack_data);
04aa13f8 342uint64_t lttng_bytecode_filter_interpret(void *filter_data,
97b58163
MD
343 const char *filter_stack_data);
344
04aa13f8 345#endif /* _LTTNG_BYTECODE_H */
This page took 0.043987 seconds and 4 git commands to generate.