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