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