5bff5b00497a068a3f872f1eea76a78b0201bc85
[lttng-modules.git] / include / lttng / lttng-bytecode.h
1 /* SPDX-License-Identifier: MIT
2 *
3 * lttng/lttng-bytecode.h
4 *
5 * LTTng modules bytecode header.
6 *
7 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #ifndef _LTTNG_BYTECODE_H
11 #define _LTTNG_BYTECODE_H
12
13 #include <linux/kernel.h>
14
15 #include <lttng/events.h>
16 #include <lttng/events-internal.h>
17 #include <lttng/bytecode.h>
18
19 /* Interpreter stack length, in number of entries */
20 #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */
21 #define INTERPRETER_STACK_EMPTY 1
22 #define INTERPRETER_MAX_DATA_LEN 65536
23
24 #ifdef DEBUG
25 #define dbg_printk(fmt, args...) \
26 printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \
27 __FILE__, __func__, __LINE__, ## args)
28 #else
29 #define dbg_printk(fmt, args...) \
30 do { \
31 /* do nothing but check printf format */ \
32 if (0) \
33 printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \
34 __FILE__, __func__, __LINE__, ## args); \
35 } while (0)
36 #endif
37
38 /* Linked bytecode. Child of struct lttng_kernel_bytecode_runtime. */
39 struct bytecode_runtime {
40 struct lttng_kernel_bytecode_runtime p;
41 size_t data_len;
42 size_t data_alloc_len;
43 char *data;
44 uint16_t len;
45 char code[0];
46 };
47
48 enum entry_type {
49 REG_S64,
50 REG_U64,
51 REG_DOUBLE,
52 REG_STRING,
53 REG_STAR_GLOB_STRING,
54 REG_TYPE_UNKNOWN,
55 REG_PTR,
56 };
57
58 enum load_type {
59 LOAD_ROOT_CONTEXT,
60 LOAD_ROOT_APP_CONTEXT,
61 LOAD_ROOT_PAYLOAD,
62 LOAD_OBJECT,
63 };
64
65 enum object_type {
66 OBJECT_TYPE_S8,
67 OBJECT_TYPE_S16,
68 OBJECT_TYPE_S32,
69 OBJECT_TYPE_S64,
70 OBJECT_TYPE_U8,
71 OBJECT_TYPE_U16,
72 OBJECT_TYPE_U32,
73 OBJECT_TYPE_U64,
74
75 OBJECT_TYPE_SIGNED_ENUM,
76 OBJECT_TYPE_UNSIGNED_ENUM,
77
78 OBJECT_TYPE_DOUBLE,
79 OBJECT_TYPE_STRING,
80 OBJECT_TYPE_STRING_SEQUENCE,
81
82 OBJECT_TYPE_SEQUENCE,
83 OBJECT_TYPE_ARRAY,
84 OBJECT_TYPE_STRUCT,
85 OBJECT_TYPE_VARIANT,
86
87 OBJECT_TYPE_DYNAMIC,
88 };
89
90 struct bytecode_get_index_data {
91 uint64_t offset; /* in bytes */
92 size_t ctx_index;
93 size_t array_len;
94 /*
95 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
96 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
97 * interpreter needs to find it from the event fields and types to
98 * support variants.
99 */
100 const struct lttng_kernel_event_field *field;
101 struct {
102 size_t len;
103 enum object_type type;
104 bool rev_bo; /* reverse byte order */
105 bool user; /* from userspace */
106 } elem;
107 };
108
109 /* Validation stack */
110 struct vstack_load {
111 enum load_type type;
112 enum object_type object_type;
113 const struct lttng_kernel_event_field *field;
114 bool rev_bo; /* reverse byte order */
115 bool user; /* from userspace */
116 };
117
118 struct vstack_entry {
119 enum entry_type type;
120 struct vstack_load load;
121 };
122
123 struct vstack {
124 int top; /* top of stack */
125 struct vstack_entry e[INTERPRETER_STACK_LEN];
126 };
127
128 static inline
129 void vstack_init(struct vstack *stack)
130 {
131 stack->top = -1;
132 }
133
134 static inline
135 struct vstack_entry *vstack_ax(struct vstack *stack)
136 {
137 if (unlikely(stack->top < 0))
138 return NULL;
139 return &stack->e[stack->top];
140 }
141
142 static inline
143 struct vstack_entry *vstack_bx(struct vstack *stack)
144 {
145 if (unlikely(stack->top < 1))
146 return NULL;
147 return &stack->e[stack->top - 1];
148 }
149
150 static inline
151 int vstack_push(struct vstack *stack)
152 {
153 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
154 printk(KERN_WARNING "LTTng: filter: Stack full\n");
155 return -EINVAL;
156 }
157 ++stack->top;
158 return 0;
159 }
160
161 static inline
162 int vstack_pop(struct vstack *stack)
163 {
164 if (unlikely(stack->top < 0)) {
165 printk(KERN_WARNING "LTTng: filter: Stack empty\n");
166 return -EINVAL;
167 }
168 stack->top--;
169 return 0;
170 }
171
172 /* Execution stack */
173 enum estack_string_literal_type {
174 ESTACK_STRING_LITERAL_TYPE_NONE,
175 ESTACK_STRING_LITERAL_TYPE_PLAIN,
176 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
177 };
178
179 struct load_ptr {
180 enum load_type type;
181 enum object_type object_type;
182 const void *ptr;
183 size_t nr_elem;
184 bool rev_bo;
185 bool user; /* from userspace */
186 /* Temporary place-holders for contexts. */
187 union {
188 int64_t s64;
189 uint64_t u64;
190 double d;
191 } u;
192 const struct lttng_kernel_event_field *field;
193 };
194
195 struct estack_entry {
196 enum entry_type type;
197 union {
198 int64_t v;
199
200 struct {
201 const char *str;
202 const char __user *user_str;
203 size_t seq_len;
204 enum estack_string_literal_type literal_type;
205 bool user; /* is string from userspace ? */
206 } s;
207 struct load_ptr ptr;
208 } u;
209 };
210
211 struct estack {
212 int top; /* top of stack */
213 struct estack_entry e[INTERPRETER_STACK_LEN];
214 };
215
216 #define estack_ax_v ax
217 #define estack_bx_v bx
218
219 #define estack_ax_t ax_t
220 #define estack_bx_t bx_t
221
222 #define estack_ax(stack, top) \
223 ({ \
224 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
225 &(stack)->e[top]; \
226 })
227
228 #define estack_bx(stack, top) \
229 ({ \
230 BUG_ON((top) <= INTERPRETER_STACK_EMPTY + 1); \
231 &(stack)->e[(top) - 1]; \
232 })
233
234 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
235 do { \
236 BUG_ON((top) >= INTERPRETER_STACK_LEN - 1); \
237 (stack)->e[(top) - 1].u.v = (bx); \
238 (stack)->e[(top) - 1].type = (bx_t); \
239 (bx) = (ax); \
240 (bx_t) = (ax_t); \
241 ++(top); \
242 } while (0)
243
244 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
245 do { \
246 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
247 (ax) = (bx); \
248 (ax_t) = (bx_t); \
249 (bx) = (stack)->e[(top) - 2].u.v; \
250 (bx_t) = (stack)->e[(top) - 2].type; \
251 (top)--; \
252 } while (0)
253
254 enum lttng_interpreter_type {
255 LTTNG_INTERPRETER_TYPE_S64,
256 LTTNG_INTERPRETER_TYPE_U64,
257 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM,
258 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM,
259 LTTNG_INTERPRETER_TYPE_DOUBLE,
260 LTTNG_INTERPRETER_TYPE_STRING,
261 LTTNG_INTERPRETER_TYPE_SEQUENCE,
262 };
263
264 /*
265 * Represents the output parameter of the lttng interpreter.
266 * Currently capturable field classes are integer, double, string and sequence
267 * of integer.
268 */
269 struct lttng_interpreter_output {
270 enum lttng_interpreter_type type;
271 union {
272 int64_t s;
273 uint64_t u;
274
275 struct {
276 const char *str;
277 const char __user *user_str;
278 size_t len;
279 bool user; /* is string from userspace ? */
280 } str;
281 struct {
282 const void *ptr;
283 size_t nr_elem;
284
285 /* Inner type. */
286 const struct lttng_kernel_type_common *nested_type;
287 } sequence;
288 } u;
289 };
290
291 const char *lttng_bytecode_print_op(enum bytecode_op op);
292
293 void lttng_bytecode_sync_state(struct lttng_kernel_bytecode_runtime *runtime);
294 int lttng_bytecode_validate(struct bytecode_runtime *bytecode);
295 int lttng_bytecode_validate_load(struct bytecode_runtime *bytecode);
296 int lttng_bytecode_specialize(const struct lttng_kernel_event_desc *event_desc,
297 struct bytecode_runtime *bytecode);
298
299 int lttng_bytecode_interpret_error(struct lttng_kernel_bytecode_runtime *bytecode_runtime,
300 const char *stack_data,
301 struct lttng_kernel_probe_ctx *probe_ctx,
302 void *ctx);
303
304 int lttng_bytecode_interpret(struct lttng_kernel_bytecode_runtime *kernel_bytecode,
305 const char *interpreter_stack_data,
306 struct lttng_kernel_probe_ctx *lttng_probe_ctx,
307 void *caller_ctx);
308
309 #endif /* _LTTNG_FILTER_H */
This page took 0.034078 seconds and 3 git commands to generate.