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