Commit | Line | Data |
---|---|---|
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...) \ | |
70 | do { \ | |
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 | 79 | struct 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 | 88 | enum 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 | ||
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 | ||
70f9f7f9 FD |
115 | OBJECT_TYPE_SIGNED_ENUM, |
116 | OBJECT_TYPE_UNSIGNED_ENUM, | |
117 | ||
47e5f13e MD |
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 | ||
04aa13f8 | 130 | struct bytecode_get_index_data { |
47e5f13e MD |
131 | uint64_t offset; /* in bytes */ |
132 | size_t ctx_index; | |
133 | size_t array_len; | |
f3503ba9 FD |
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; | |
47e5f13e MD |
141 | struct { |
142 | size_t len; | |
143 | enum object_type type; | |
144 | bool rev_bo; /* reverse byte order */ | |
145 | } elem; | |
97b58163 MD |
146 | }; |
147 | ||
0305960f | 148 | /* Validation stack */ |
47e5f13e MD |
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 | ||
0305960f MD |
156 | struct vstack_entry { |
157 | enum entry_type type; | |
47e5f13e | 158 | struct vstack_load load; |
97b58163 MD |
159 | }; |
160 | ||
0305960f MD |
161 | struct vstack { |
162 | int top; /* top of stack */ | |
04aa13f8 | 163 | struct vstack_entry e[INTERPRETER_STACK_LEN]; |
0305960f MD |
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 | { | |
04aa13f8 | 191 | if (stack->top >= INTERPRETER_STACK_LEN - 1) { |
0305960f MD |
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 */ | |
3151a51d PP |
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 | ||
47e5f13e MD |
217 | struct load_ptr { |
218 | enum load_type type; | |
219 | enum object_type object_type; | |
220 | const void *ptr; | |
f3503ba9 | 221 | size_t nr_elem; |
47e5f13e MD |
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; | |
47e5f13e MD |
229 | const struct lttng_event_field *field; |
230 | }; | |
231 | ||
0305960f | 232 | struct estack_entry { |
53569322 | 233 | enum entry_type type; /* For dynamic typing. */ |
0305960f MD |
234 | union { |
235 | int64_t v; | |
236 | double d; | |
237 | ||
238 | struct { | |
239 | const char *str; | |
9b33aac4 | 240 | size_t seq_len; |
3151a51d | 241 | enum estack_string_literal_type literal_type; |
0305960f | 242 | } s; |
47e5f13e | 243 | struct load_ptr ptr; |
0305960f MD |
244 | } u; |
245 | }; | |
97b58163 | 246 | |
0305960f MD |
247 | struct estack { |
248 | int top; /* top of stack */ | |
04aa13f8 | 249 | struct estack_entry e[INTERPRETER_STACK_LEN]; |
97b58163 MD |
250 | }; |
251 | ||
53569322 MD |
252 | /* |
253 | * Always use aliased type for ax/bx (top of stack). | |
254 | * When ax/bx are S64, use aliased value. | |
255 | */ | |
9b33aac4 MD |
256 | #define estack_ax_v ax |
257 | #define estack_bx_v bx | |
53569322 MD |
258 | #define estack_ax_t ax_t |
259 | #define estack_bx_t bx_t | |
9b33aac4 | 260 | |
53569322 MD |
261 | /* |
262 | * ax and bx registers can hold either integer, double or string. | |
263 | */ | |
9b33aac4 MD |
264 | #define estack_ax(stack, top) \ |
265 | ({ \ | |
04aa13f8 | 266 | assert((top) > INTERPRETER_STACK_EMPTY); \ |
9b33aac4 MD |
267 | &(stack)->e[top]; \ |
268 | }) | |
269 | ||
270 | #define estack_bx(stack, top) \ | |
271 | ({ \ | |
04aa13f8 | 272 | assert((top) > INTERPRETER_STACK_EMPTY + 1); \ |
9b33aac4 MD |
273 | &(stack)->e[(top) - 1]; \ |
274 | }) | |
275 | ||
53569322 MD |
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) \ | |
9b33aac4 | 280 | do { \ |
04aa13f8 | 281 | assert((top) < INTERPRETER_STACK_LEN - 1); \ |
9b33aac4 | 282 | (stack)->e[(top) - 1].u.v = (bx); \ |
53569322 | 283 | (stack)->e[(top) - 1].type = (bx_t); \ |
9b33aac4 | 284 | (bx) = (ax); \ |
53569322 | 285 | (bx_t) = (ax_t); \ |
9b33aac4 MD |
286 | ++(top); \ |
287 | } while (0) | |
288 | ||
53569322 | 289 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
9b33aac4 | 290 | do { \ |
04aa13f8 | 291 | assert((top) > INTERPRETER_STACK_EMPTY); \ |
9b33aac4 | 292 | (ax) = (bx); \ |
53569322 | 293 | (ax_t) = (bx_t); \ |
9b33aac4 | 294 | (bx) = (stack)->e[(top) - 2].u.v; \ |
53569322 | 295 | (bx_t) = (stack)->e[(top) - 2].type; \ |
9b33aac4 MD |
296 | (top)--; \ |
297 | } while (0) | |
0305960f | 298 | |
f3503ba9 FD |
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 | ||
04aa13f8 | 335 | const char *print_op(enum bytecode_op op); |
97b58163 | 336 | |
4d451c15 | 337 | void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime); |
d37ecb3f | 338 | void lttng_bytecode_capture_sync_state(struct lttng_bytecode_runtime *runtime); |
4d451c15 | 339 | |
04aa13f8 FD |
340 | int lttng_bytecode_validate(struct bytecode_runtime *bytecode); |
341 | int lttng_bytecode_specialize(const struct lttng_event_desc *event_desc, | |
47e5f13e | 342 | struct bytecode_runtime *bytecode); |
97b58163 | 343 | |
04aa13f8 | 344 | uint64_t lttng_bytecode_filter_interpret_false(void *filter_data, |
97b58163 | 345 | const char *filter_stack_data); |
04aa13f8 | 346 | uint64_t lttng_bytecode_filter_interpret(void *filter_data, |
97b58163 MD |
347 | const char *filter_stack_data); |
348 | ||
d37ecb3f FD |
349 | uint64_t lttng_bytecode_capture_interpret_false(void *capture_data, |
350 | const char *capture_stack_data, | |
351 | struct lttng_interpreter_output *output); | |
352 | uint64_t lttng_bytecode_capture_interpret(void *capture_data, | |
353 | const char *capture_stack_data, | |
354 | struct lttng_interpreter_output *output); | |
355 | ||
04aa13f8 | 356 | #endif /* _LTTNG_BYTECODE_H */ |