5742f7f5c34eae8cad45294494f73818add7f942
[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_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
127 struct bytecode_get_index_data {
128 uint64_t offset; /* in bytes */
129 size_t ctx_index;
130 size_t array_len;
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;
138 struct {
139 size_t len;
140 enum object_type type;
141 bool rev_bo; /* reverse byte order */
142 } elem;
143 };
144
145 /* Validation stack */
146 struct 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
153 struct vstack_entry {
154 enum entry_type type;
155 struct vstack_load load;
156 };
157
158 struct vstack {
159 int top; /* top of stack */
160 struct vstack_entry e[INTERPRETER_STACK_LEN];
161 };
162
163 static inline
164 void vstack_init(struct vstack *stack)
165 {
166 stack->top = -1;
167 }
168
169 static inline
170 struct 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
177 static inline
178 struct 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
185 static inline
186 int vstack_push(struct vstack *stack)
187 {
188 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
189 ERR("Stack full\n");
190 return -EINVAL;
191 }
192 ++stack->top;
193 return 0;
194 }
195
196 static inline
197 int 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 */
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,
212 };
213
214 struct load_ptr {
215 enum load_type type;
216 enum object_type object_type;
217 const void *ptr;
218 size_t nr_elem;
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;
226 const struct lttng_event_field *field;
227 };
228
229 struct estack_entry {
230 enum entry_type type; /* For dynamic typing. */
231 union {
232 int64_t v;
233 double d;
234
235 struct {
236 const char *str;
237 size_t seq_len;
238 enum estack_string_literal_type literal_type;
239 } s;
240 struct load_ptr ptr;
241 } u;
242 };
243
244 struct estack {
245 int top; /* top of stack */
246 struct estack_entry e[INTERPRETER_STACK_LEN];
247 };
248
249 /*
250 * Always use aliased type for ax/bx (top of stack).
251 * When ax/bx are S64, use aliased value.
252 */
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
257
258 /*
259 * ax and bx registers can hold either integer, double or string.
260 */
261 #define estack_ax(stack, top) \
262 ({ \
263 assert((top) > INTERPRETER_STACK_EMPTY); \
264 &(stack)->e[top]; \
265 })
266
267 #define estack_bx(stack, top) \
268 ({ \
269 assert((top) > INTERPRETER_STACK_EMPTY + 1); \
270 &(stack)->e[(top) - 1]; \
271 })
272
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) \
277 do { \
278 assert((top) < INTERPRETER_STACK_LEN - 1); \
279 (stack)->e[(top) - 1].u.v = (bx); \
280 (stack)->e[(top) - 1].type = (bx_t); \
281 (bx) = (ax); \
282 (bx_t) = (ax_t); \
283 ++(top); \
284 } while (0)
285
286 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
287 do { \
288 assert((top) > INTERPRETER_STACK_EMPTY); \
289 (ax) = (bx); \
290 (ax_t) = (bx_t); \
291 (bx) = (stack)->e[(top) - 2].u.v; \
292 (bx_t) = (stack)->e[(top) - 2].type; \
293 (top)--; \
294 } while (0)
295
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,
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 */
311 struct 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
332 const char *print_op(enum bytecode_op op);
333
334 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime);
335
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);
339
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);
344
345 #endif /* _LTTNG_BYTECODE_H */
This page took 0.035275 seconds and 3 git commands to generate.