bytecode: Add `OBJECT_TYPE_{UN,}SIGNED_ENUM` type
[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_SIGNED_ENUM,
75 OBJECT_TYPE_UNSIGNED_ENUM,
76
77 OBJECT_TYPE_DOUBLE,
78 OBJECT_TYPE_STRING,
79 OBJECT_TYPE_STRING_SEQUENCE,
80
81 OBJECT_TYPE_SEQUENCE,
82 OBJECT_TYPE_ARRAY,
83 OBJECT_TYPE_STRUCT,
84 OBJECT_TYPE_VARIANT,
85
86 OBJECT_TYPE_DYNAMIC,
87 };
88
89 struct bytecode_get_index_data {
90 uint64_t offset; /* in bytes */
91 size_t ctx_index;
92 size_t array_len;
93 /*
94 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
95 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
96 * interpreter needs to find it from the event fields and types to
97 * support variants.
98 */
99 const struct lttng_event_field *field;
100 struct {
101 size_t len;
102 enum object_type type;
103 bool rev_bo; /* reverse byte order */
104 } elem;
105 };
106
107 /* Validation stack */
108 struct vstack_load {
109 enum load_type type;
110 enum object_type object_type;
111 const struct lttng_event_field *field;
112 bool rev_bo; /* reverse byte order */
113 };
114
115 struct vstack_entry {
116 enum entry_type type;
117 struct vstack_load load;
118 };
119
120 struct vstack {
121 int top; /* top of stack */
122 struct vstack_entry e[INTERPRETER_STACK_LEN];
123 };
124
125 static inline
126 void vstack_init(struct vstack *stack)
127 {
128 stack->top = -1;
129 }
130
131 static inline
132 struct vstack_entry *vstack_ax(struct vstack *stack)
133 {
134 if (unlikely(stack->top < 0))
135 return NULL;
136 return &stack->e[stack->top];
137 }
138
139 static inline
140 struct vstack_entry *vstack_bx(struct vstack *stack)
141 {
142 if (unlikely(stack->top < 1))
143 return NULL;
144 return &stack->e[stack->top - 1];
145 }
146
147 static inline
148 int vstack_push(struct vstack *stack)
149 {
150 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
151 printk(KERN_WARNING "LTTng: filter: Stack full\n");
152 return -EINVAL;
153 }
154 ++stack->top;
155 return 0;
156 }
157
158 static inline
159 int vstack_pop(struct vstack *stack)
160 {
161 if (unlikely(stack->top < 0)) {
162 printk(KERN_WARNING "LTTng: filter: Stack empty\n");
163 return -EINVAL;
164 }
165 stack->top--;
166 return 0;
167 }
168
169 /* Execution stack */
170 enum estack_string_literal_type {
171 ESTACK_STRING_LITERAL_TYPE_NONE,
172 ESTACK_STRING_LITERAL_TYPE_PLAIN,
173 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
174 };
175
176 struct load_ptr {
177 enum load_type type;
178 enum object_type object_type;
179 const void *ptr;
180 size_t nr_elem;
181 bool rev_bo;
182 /* Temporary place-holders for contexts. */
183 union {
184 int64_t s64;
185 uint64_t u64;
186 double d;
187 } u;
188 const struct lttng_event_field *field;
189 };
190
191 struct estack_entry {
192 enum entry_type type;
193 union {
194 int64_t v;
195
196 struct {
197 const char *str;
198 const char __user *user_str;
199 size_t seq_len;
200 enum estack_string_literal_type literal_type;
201 int user; /* is string from userspace ? */
202 } s;
203 struct load_ptr ptr;
204 } u;
205 };
206
207 struct estack {
208 int top; /* top of stack */
209 struct estack_entry e[INTERPRETER_STACK_LEN];
210 };
211
212 #define estack_ax_v ax
213 #define estack_bx_v bx
214
215 #define estack_ax_t ax_t
216 #define estack_bx_t bx_t
217
218 #define estack_ax(stack, top) \
219 ({ \
220 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
221 &(stack)->e[top]; \
222 })
223
224 #define estack_bx(stack, top) \
225 ({ \
226 BUG_ON((top) <= INTERPRETER_STACK_EMPTY + 1); \
227 &(stack)->e[(top) - 1]; \
228 })
229
230 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
231 do { \
232 BUG_ON((top) >= INTERPRETER_STACK_LEN - 1); \
233 (stack)->e[(top) - 1].u.v = (bx); \
234 (stack)->e[(top) - 1].type = (bx_t); \
235 (bx) = (ax); \
236 (bx_t) = (ax_t); \
237 ++(top); \
238 } while (0)
239
240 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
241 do { \
242 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
243 (ax) = (bx); \
244 (ax_t) = (bx_t); \
245 (bx) = (stack)->e[(top) - 2].u.v; \
246 (bx_t) = (stack)->e[(top) - 2].type; \
247 (top)--; \
248 } while (0)
249
250 enum lttng_interpreter_type {
251 LTTNG_INTERPRETER_TYPE_S64,
252 LTTNG_INTERPRETER_TYPE_U64,
253 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM,
254 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM,
255 LTTNG_INTERPRETER_TYPE_DOUBLE,
256 LTTNG_INTERPRETER_TYPE_STRING,
257 LTTNG_INTERPRETER_TYPE_SEQUENCE,
258 };
259
260 /*
261 * Represents the output parameter of the lttng interpreter.
262 * Currently capturable field classes are integer, double, string and sequence
263 * of integer.
264 */
265 struct lttng_interpreter_output {
266 enum lttng_interpreter_type type;
267 union {
268 int64_t s;
269 uint64_t u;
270
271 struct {
272 const char *str;
273 size_t len;
274 } str;
275 struct {
276 const void *ptr;
277 size_t nr_elem;
278
279 /* Inner type. */
280 const struct lttng_type *nested_type;
281 } sequence;
282 } u;
283 };
284
285 const char *lttng_bytecode_print_op(enum bytecode_op op);
286
287 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime);
288 int lttng_bytecode_validate(struct bytecode_runtime *bytecode);
289 int lttng_bytecode_specialize(const struct lttng_event_desc *event_desc,
290 struct bytecode_runtime *bytecode);
291
292 uint64_t lttng_bytecode_filter_interpret_false(void *filter_data,
293 struct lttng_probe_ctx *lttng_probe_ctx,
294 const char *filter_stack_data);
295 uint64_t lttng_bytecode_filter_interpret(void *filter_data,
296 struct lttng_probe_ctx *lttng_probe_ctx,
297 const char *filter_stack_data);
298
299 #endif /* _LTTNG_FILTER_H */
This page took 0.034661 seconds and 4 git commands to generate.