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