Fix: combined tracing of lttng-ust 2.12/2.13 generates corrupted traces
[lttng-ust.git] / src / lib / lttng-ust / lttng-bytecode.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST bytecode header.
7 */
8
9 #ifndef _LTTNG_BYTECODE_H
10 #define _LTTNG_BYTECODE_H
11
12 #include <errno.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include "common/macros.h"
16 #include <lttng/ust-events.h>
17 #include "common/ust-context-provider.h"
18 #include <stdint.h>
19 #include <assert.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include "common/logging.h"
25 #include "bytecode.h"
26 #include "lib/lttng-ust/events.h"
27
28 /* Interpreter stack length, in number of entries */
29 #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */
30 #define INTERPRETER_STACK_EMPTY 1
31
32 #define BYTECODE_MAX_DATA_LEN 65536
33
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
48 #define dbg_printf(fmt, args...) \
49 printf("[debug bytecode in %s:%s@%u] " fmt, \
50 __FILE__, __func__, __LINE__, ## args)
51 #else
52 #define dbg_printf(fmt, args...) \
53 do { \
54 /* do nothing but check printf format */ \
55 if (0) \
56 printf("[debug bytecode in %s:%s@%u] " fmt, \
57 __FILE__, __func__, __LINE__, ## args); \
58 } while (0)
59 #endif
60
61 /* Linked bytecode. Child of struct lttng_ust_bytecode_runtime. */
62 struct bytecode_runtime {
63 struct lttng_ust_bytecode_runtime p;
64 size_t data_len;
65 size_t data_alloc_len;
66 char *data;
67 uint16_t len;
68 char code[0];
69 };
70
71 enum entry_type {
72 REG_S64,
73 REG_U64,
74 REG_DOUBLE,
75 REG_STRING,
76 REG_STAR_GLOB_STRING,
77 REG_UNKNOWN,
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
98 OBJECT_TYPE_SIGNED_ENUM,
99 OBJECT_TYPE_UNSIGNED_ENUM,
100
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
113 struct bytecode_get_index_data {
114 uint64_t offset; /* in bytes */
115 size_t ctx_index;
116 size_t array_len;
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 */
123 const struct lttng_ust_event_field *field;
124 struct {
125 size_t len;
126 enum object_type type;
127 bool rev_bo; /* reverse byte order */
128 } elem;
129 };
130
131 /* Validation stack */
132 struct vstack_load {
133 enum load_type type;
134 enum object_type object_type;
135 const struct lttng_ust_event_field *field;
136 bool rev_bo; /* reverse byte order */
137 };
138
139 struct vstack_entry {
140 enum entry_type type;
141 struct vstack_load load;
142 };
143
144 struct vstack {
145 int top; /* top of stack */
146 struct vstack_entry e[INTERPRETER_STACK_LEN];
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 {
174 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
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 */
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
200 struct load_ptr {
201 enum load_type type;
202 enum object_type object_type;
203 const void *ptr;
204 size_t nr_elem;
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;
212 const struct lttng_ust_event_field *field;
213 };
214
215 struct estack_entry {
216 enum entry_type type; /* For dynamic typing. */
217 union {
218 int64_t v;
219 double d;
220
221 struct {
222 const char *str;
223 size_t seq_len;
224 enum estack_string_literal_type literal_type;
225 } s;
226 struct load_ptr ptr;
227 } u;
228 };
229
230 struct estack {
231 int top; /* top of stack */
232 struct estack_entry e[INTERPRETER_STACK_LEN];
233 };
234
235 /*
236 * Always use aliased type for ax/bx (top of stack).
237 * When ax/bx are S64, use aliased value.
238 */
239 #define estack_ax_v ax
240 #define estack_bx_v bx
241 #define estack_ax_t ax_t
242 #define estack_bx_t bx_t
243
244 /*
245 * ax and bx registers can hold either integer, double or string.
246 */
247 #define estack_ax(stack, top) \
248 ({ \
249 assert((top) > INTERPRETER_STACK_EMPTY); \
250 &(stack)->e[top]; \
251 })
252
253 #define estack_bx(stack, top) \
254 ({ \
255 assert((top) > INTERPRETER_STACK_EMPTY + 1); \
256 &(stack)->e[(top) - 1]; \
257 })
258
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) \
263 do { \
264 assert((top) < INTERPRETER_STACK_LEN - 1); \
265 (stack)->e[(top) - 1].u.v = (bx); \
266 (stack)->e[(top) - 1].type = (bx_t); \
267 (bx) = (ax); \
268 (bx_t) = (ax_t); \
269 ++(top); \
270 } while (0)
271
272 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
273 do { \
274 assert((top) > INTERPRETER_STACK_EMPTY); \
275 (ax) = (bx); \
276 (ax_t) = (bx_t); \
277 (bx) = (stack)->e[(top) - 2].u.v; \
278 (bx_t) = (stack)->e[(top) - 2].type; \
279 (top)--; \
280 } while (0)
281
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. */
313 const struct lttng_ust_type_common *nested_type;
314 } sequence;
315 } u;
316 };
317
318 const char *lttng_bytecode_print_op(enum bytecode_op op)
319 __attribute__((visibility("hidden")));
320
321 void lttng_bytecode_sync_state(struct lttng_ust_bytecode_runtime *runtime)
322 __attribute__((visibility("hidden")));
323
324 int lttng_bytecode_validate(struct bytecode_runtime *bytecode)
325 __attribute__((visibility("hidden")));
326
327 int lttng_bytecode_specialize(const struct lttng_ust_event_desc *event_desc,
328 struct bytecode_runtime *bytecode)
329 __attribute__((visibility("hidden")));
330
331 int lttng_bytecode_interpret_error(struct lttng_ust_bytecode_runtime *bytecode_runtime,
332 const char *stack_data,
333 struct lttng_ust_probe_ctx *probe_ctx,
334 void *ctx)
335 __attribute__((visibility("hidden")));
336
337 int lttng_bytecode_interpret(struct lttng_ust_bytecode_runtime *bytecode_runtime,
338 const char *stack_data,
339 struct lttng_ust_probe_ctx *probe_ctx,
340 void *ctx)
341 __attribute__((visibility("hidden")));
342
343 #endif /* _LTTNG_BYTECODE_H */
This page took 0.055831 seconds and 4 git commands to generate.