Commit | Line | Data |
---|---|---|
97b58163 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: MIT |
97b58163 | 3 | * |
7e50015d | 4 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
97b58163 | 5 | * |
c0c0989a | 6 | * LTTng UST bytecode header. |
97b58163 MD |
7 | */ |
8 | ||
c0c0989a MJ |
9 | #ifndef _LTTNG_BYTECODE_H |
10 | #define _LTTNG_BYTECODE_H | |
11 | ||
97b58163 MD |
12 | #include <errno.h> |
13 | #include <stdio.h> | |
91ad5118 | 14 | #include <stdbool.h> |
864a1eda | 15 | #include <ust-helper.h> |
97b58163 | 16 | #include <lttng/ust-events.h> |
ae4b659d | 17 | #include <ust-context-provider.h> |
97b58163 | 18 | #include <stdint.h> |
0305960f | 19 | #include <assert.h> |
97b58163 MD |
20 | #include <errno.h> |
21 | #include <string.h> | |
22 | #include <inttypes.h> | |
23 | #include <limits.h> | |
24 | #include <usterr-signal-safe.h> | |
04aa13f8 | 25 | #include "bytecode.h" |
97b58163 | 26 | |
04aa13f8 FD |
27 | /* Interpreter stack length, in number of entries */ |
28 | #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */ | |
29 | #define INTERPRETER_STACK_EMPTY 1 | |
97b58163 | 30 | |
04aa13f8 | 31 | #define BYTECODE_MAX_DATA_LEN 65536 |
47e5f13e | 32 | |
97b58163 MD |
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 | |
f488575f MD |
47 | #define dbg_printf(fmt, args...) \ |
48 | printf("[debug bytecode in %s:%s@%u] " fmt, \ | |
49 | __FILE__, __func__, __LINE__, ## args) | |
97b58163 MD |
50 | #else |
51 | #define dbg_printf(fmt, args...) \ | |
52 | do { \ | |
53 | /* do nothing but check printf format */ \ | |
54 | if (0) \ | |
f488575f MD |
55 | printf("[debug bytecode in %s:%s@%u] " fmt, \ |
56 | __FILE__, __func__, __LINE__, ## args); \ | |
97b58163 MD |
57 | } while (0) |
58 | #endif | |
59 | ||
f488575f | 60 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
97b58163 | 61 | struct bytecode_runtime { |
5469a374 | 62 | struct lttng_ust_bytecode_runtime p; |
47e5f13e MD |
63 | size_t data_len; |
64 | size_t data_alloc_len; | |
65 | char *data; | |
97b58163 | 66 | uint16_t len; |
47e5f13e | 67 | char code[0]; |
97b58163 MD |
68 | }; |
69 | ||
0305960f | 70 | enum entry_type { |
97b58163 | 71 | REG_S64, |
d97f9b78 | 72 | REG_U64, |
97b58163 MD |
73 | REG_DOUBLE, |
74 | REG_STRING, | |
3151a51d | 75 | REG_STAR_GLOB_STRING, |
53569322 | 76 | REG_UNKNOWN, |
47e5f13e MD |
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 | ||
70f9f7f9 FD |
97 | OBJECT_TYPE_SIGNED_ENUM, |
98 | OBJECT_TYPE_UNSIGNED_ENUM, | |
99 | ||
47e5f13e MD |
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 | ||
04aa13f8 | 112 | struct bytecode_get_index_data { |
47e5f13e MD |
113 | uint64_t offset; /* in bytes */ |
114 | size_t ctx_index; | |
115 | size_t array_len; | |
f3503ba9 FD |
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 | */ | |
a084756d | 122 | struct lttng_ust_event_field *field; |
47e5f13e MD |
123 | struct { |
124 | size_t len; | |
125 | enum object_type type; | |
126 | bool rev_bo; /* reverse byte order */ | |
127 | } elem; | |
97b58163 MD |
128 | }; |
129 | ||
0305960f | 130 | /* Validation stack */ |
47e5f13e MD |
131 | struct vstack_load { |
132 | enum load_type type; | |
133 | enum object_type object_type; | |
a084756d | 134 | struct lttng_ust_event_field *field; |
47e5f13e MD |
135 | bool rev_bo; /* reverse byte order */ |
136 | }; | |
137 | ||
0305960f MD |
138 | struct vstack_entry { |
139 | enum entry_type type; | |
47e5f13e | 140 | struct vstack_load load; |
97b58163 MD |
141 | }; |
142 | ||
0305960f MD |
143 | struct vstack { |
144 | int top; /* top of stack */ | |
04aa13f8 | 145 | struct vstack_entry e[INTERPRETER_STACK_LEN]; |
0305960f MD |
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 | { | |
04aa13f8 | 173 | if (stack->top >= INTERPRETER_STACK_LEN - 1) { |
0305960f MD |
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 */ | |
3151a51d PP |
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 | ||
47e5f13e MD |
199 | struct load_ptr { |
200 | enum load_type type; | |
201 | enum object_type object_type; | |
202 | const void *ptr; | |
f3503ba9 | 203 | size_t nr_elem; |
47e5f13e MD |
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; | |
25cff019 | 211 | const struct lttng_ust_event_field *field; |
47e5f13e MD |
212 | }; |
213 | ||
0305960f | 214 | struct estack_entry { |
53569322 | 215 | enum entry_type type; /* For dynamic typing. */ |
0305960f MD |
216 | union { |
217 | int64_t v; | |
218 | double d; | |
219 | ||
220 | struct { | |
221 | const char *str; | |
9b33aac4 | 222 | size_t seq_len; |
3151a51d | 223 | enum estack_string_literal_type literal_type; |
0305960f | 224 | } s; |
47e5f13e | 225 | struct load_ptr ptr; |
0305960f MD |
226 | } u; |
227 | }; | |
97b58163 | 228 | |
0305960f MD |
229 | struct estack { |
230 | int top; /* top of stack */ | |
04aa13f8 | 231 | struct estack_entry e[INTERPRETER_STACK_LEN]; |
97b58163 MD |
232 | }; |
233 | ||
53569322 MD |
234 | /* |
235 | * Always use aliased type for ax/bx (top of stack). | |
236 | * When ax/bx are S64, use aliased value. | |
237 | */ | |
9b33aac4 MD |
238 | #define estack_ax_v ax |
239 | #define estack_bx_v bx | |
53569322 MD |
240 | #define estack_ax_t ax_t |
241 | #define estack_bx_t bx_t | |
9b33aac4 | 242 | |
53569322 MD |
243 | /* |
244 | * ax and bx registers can hold either integer, double or string. | |
245 | */ | |
9b33aac4 MD |
246 | #define estack_ax(stack, top) \ |
247 | ({ \ | |
04aa13f8 | 248 | assert((top) > INTERPRETER_STACK_EMPTY); \ |
9b33aac4 MD |
249 | &(stack)->e[top]; \ |
250 | }) | |
251 | ||
252 | #define estack_bx(stack, top) \ | |
253 | ({ \ | |
04aa13f8 | 254 | assert((top) > INTERPRETER_STACK_EMPTY + 1); \ |
9b33aac4 MD |
255 | &(stack)->e[(top) - 1]; \ |
256 | }) | |
257 | ||
53569322 MD |
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) \ | |
9b33aac4 | 262 | do { \ |
04aa13f8 | 263 | assert((top) < INTERPRETER_STACK_LEN - 1); \ |
9b33aac4 | 264 | (stack)->e[(top) - 1].u.v = (bx); \ |
53569322 | 265 | (stack)->e[(top) - 1].type = (bx_t); \ |
9b33aac4 | 266 | (bx) = (ax); \ |
53569322 | 267 | (bx_t) = (ax_t); \ |
9b33aac4 MD |
268 | ++(top); \ |
269 | } while (0) | |
270 | ||
53569322 | 271 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
9b33aac4 | 272 | do { \ |
04aa13f8 | 273 | assert((top) > INTERPRETER_STACK_EMPTY); \ |
9b33aac4 | 274 | (ax) = (bx); \ |
53569322 | 275 | (ax_t) = (bx_t); \ |
9b33aac4 | 276 | (bx) = (stack)->e[(top) - 2].u.v; \ |
53569322 | 277 | (bx_t) = (stack)->e[(top) - 2].type; \ |
9b33aac4 MD |
278 | (top)--; \ |
279 | } while (0) | |
0305960f | 280 | |
f3503ba9 FD |
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. */ | |
a084756d | 312 | struct lttng_ust_type_common *nested_type; |
f3503ba9 FD |
313 | } sequence; |
314 | } u; | |
315 | }; | |
316 | ||
ddabe860 | 317 | __attribute__((visibility("hidden"))) |
bf617e20 | 318 | const char *lttng_bytecode_print_op(enum bytecode_op op); |
97b58163 | 319 | |
ddabe860 | 320 | __attribute__((visibility("hidden"))) |
5469a374 | 321 | void lttng_bytecode_filter_sync_state(struct lttng_ust_bytecode_runtime *runtime); |
ddabe860 MJ |
322 | |
323 | __attribute__((visibility("hidden"))) | |
5469a374 | 324 | void lttng_bytecode_capture_sync_state(struct lttng_ust_bytecode_runtime *runtime); |
4d451c15 | 325 | |
ddabe860 | 326 | __attribute__((visibility("hidden"))) |
04aa13f8 | 327 | int lttng_bytecode_validate(struct bytecode_runtime *bytecode); |
ddabe860 MJ |
328 | |
329 | __attribute__((visibility("hidden"))) | |
a084756d | 330 | int lttng_bytecode_specialize(struct lttng_ust_event_desc *event_desc, |
47e5f13e | 331 | struct bytecode_runtime *bytecode); |
97b58163 | 332 | |
ddabe860 | 333 | __attribute__((visibility("hidden"))) |
04aa13f8 | 334 | uint64_t lttng_bytecode_filter_interpret_false(void *filter_data, |
97b58163 | 335 | const char *filter_stack_data); |
ddabe860 MJ |
336 | |
337 | __attribute__((visibility("hidden"))) | |
04aa13f8 | 338 | uint64_t lttng_bytecode_filter_interpret(void *filter_data, |
97b58163 MD |
339 | const char *filter_stack_data); |
340 | ||
ddabe860 | 341 | __attribute__((visibility("hidden"))) |
d37ecb3f FD |
342 | uint64_t lttng_bytecode_capture_interpret_false(void *capture_data, |
343 | const char *capture_stack_data, | |
344 | struct lttng_interpreter_output *output); | |
ddabe860 MJ |
345 | |
346 | __attribute__((visibility("hidden"))) | |
d37ecb3f FD |
347 | uint64_t lttng_bytecode_capture_interpret(void *capture_data, |
348 | const char *capture_stack_data, | |
349 | struct lttng_interpreter_output *output); | |
350 | ||
04aa13f8 | 351 | #endif /* _LTTNG_BYTECODE_H */ |