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