bytecode: generalize `struct lttng_filter_bytecode_node`
[lttng-modules.git] / include / lttng / filter.h
... / ...
CommitLineData
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...) \
30do { \
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. */
39struct 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
48enum 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
58enum load_type {
59 LOAD_ROOT_CONTEXT,
60 LOAD_ROOT_APP_CONTEXT,
61 LOAD_ROOT_PAYLOAD,
62 LOAD_OBJECT,
63};
64
65enum 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
87struct 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 */
106struct 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
113struct vstack_entry {
114 enum entry_type type;
115 struct vstack_load load;
116};
117
118struct vstack {
119 int top; /* top of stack */
120 struct vstack_entry e[FILTER_STACK_LEN];
121};
122
123static inline
124void vstack_init(struct vstack *stack)
125{
126 stack->top = -1;
127}
128
129static inline
130struct 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
137static inline
138struct 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
145static inline
146int 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
156static inline
157int 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 */
168enum 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
174struct 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
189struct 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
205struct 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
248enum 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 */
263struct 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
283const char *lttng_filter_print_op(enum filter_op op);
284
285int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
286int lttng_filter_specialize_bytecode(const struct lttng_event_desc *event_desc,
287 struct bytecode_runtime *bytecode);
288
289uint64_t lttng_filter_interpret_bytecode_false(void *filter_data,
290 struct lttng_probe_ctx *lttng_probe_ctx,
291 const char *filter_stack_data);
292uint64_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.023474 seconds and 4 git commands to generate.