fix: kvm instrumentation for CentOS >= 4.18.0-147
[lttng-modules.git] / 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 <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 "[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 "[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_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 filter_get_index_data {
87 uint64_t offset; /* in bytes */
88 size_t ctx_index;
89 size_t array_len;
90 struct {
91 size_t len;
92 enum object_type type;
93 bool rev_bo; /* reverse byte order */
94 } elem;
95 };
96
97 /* Validation stack */
98 struct vstack_load {
99 enum load_type type;
100 enum object_type object_type;
101 const struct lttng_event_field *field;
102 bool rev_bo; /* reverse byte order */
103 };
104
105 struct vstack_entry {
106 enum entry_type type;
107 struct vstack_load load;
108 };
109
110 struct vstack {
111 int top; /* top of stack */
112 struct vstack_entry e[FILTER_STACK_LEN];
113 };
114
115 static inline
116 void vstack_init(struct vstack *stack)
117 {
118 stack->top = -1;
119 }
120
121 static inline
122 struct vstack_entry *vstack_ax(struct vstack *stack)
123 {
124 if (unlikely(stack->top < 0))
125 return NULL;
126 return &stack->e[stack->top];
127 }
128
129 static inline
130 struct vstack_entry *vstack_bx(struct vstack *stack)
131 {
132 if (unlikely(stack->top < 1))
133 return NULL;
134 return &stack->e[stack->top - 1];
135 }
136
137 static inline
138 int vstack_push(struct vstack *stack)
139 {
140 if (stack->top >= FILTER_STACK_LEN - 1) {
141 printk(KERN_WARNING "Stack full\n");
142 return -EINVAL;
143 }
144 ++stack->top;
145 return 0;
146 }
147
148 static inline
149 int vstack_pop(struct vstack *stack)
150 {
151 if (unlikely(stack->top < 0)) {
152 printk(KERN_WARNING "Stack empty\n");
153 return -EINVAL;
154 }
155 stack->top--;
156 return 0;
157 }
158
159 /* Execution stack */
160 enum estack_string_literal_type {
161 ESTACK_STRING_LITERAL_TYPE_NONE,
162 ESTACK_STRING_LITERAL_TYPE_PLAIN,
163 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
164 };
165
166 struct load_ptr {
167 enum load_type type;
168 enum object_type object_type;
169 const void *ptr;
170 bool rev_bo;
171 /* Temporary place-holders for contexts. */
172 union {
173 int64_t s64;
174 uint64_t u64;
175 double d;
176 } u;
177 /*
178 * "field" is only needed when nested under a variant, in which
179 * case we cannot specialize the nested operations.
180 */
181 const struct lttng_event_field *field;
182 };
183
184 struct estack_entry {
185 union {
186 int64_t v;
187
188 struct {
189 const char *str;
190 const char __user *user_str;
191 size_t seq_len;
192 enum estack_string_literal_type literal_type;
193 int user; /* is string from userspace ? */
194 } s;
195 struct load_ptr ptr;
196 } u;
197 };
198
199 struct estack {
200 int top; /* top of stack */
201 struct estack_entry e[FILTER_STACK_LEN];
202 };
203
204 #define estack_ax_v ax
205 #define estack_bx_v bx
206
207 #define estack_ax(stack, top) \
208 ({ \
209 BUG_ON((top) <= FILTER_STACK_EMPTY); \
210 &(stack)->e[top]; \
211 })
212
213 #define estack_bx(stack, top) \
214 ({ \
215 BUG_ON((top) <= FILTER_STACK_EMPTY + 1); \
216 &(stack)->e[(top) - 1]; \
217 })
218
219 #define estack_push(stack, top, ax, bx) \
220 do { \
221 BUG_ON((top) >= FILTER_STACK_LEN - 1); \
222 (stack)->e[(top) - 1].u.v = (bx); \
223 (bx) = (ax); \
224 ++(top); \
225 } while (0)
226
227 #define estack_pop(stack, top, ax, bx) \
228 do { \
229 BUG_ON((top) <= FILTER_STACK_EMPTY); \
230 (ax) = (bx); \
231 (bx) = (stack)->e[(top) - 2].u.v; \
232 (top)--; \
233 } while (0)
234
235 const char *lttng_filter_print_op(enum filter_op op);
236
237 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
238 int lttng_filter_specialize_bytecode(struct lttng_event *event,
239 struct bytecode_runtime *bytecode);
240
241 uint64_t lttng_filter_false(void *filter_data,
242 struct lttng_probe_ctx *lttng_probe_ctx,
243 const char *filter_stack_data);
244 uint64_t lttng_filter_interpret_bytecode(void *filter_data,
245 struct lttng_probe_ctx *lttng_probe_ctx,
246 const char *filter_stack_data);
247
248 #endif /* _LTTNG_FILTER_H */
This page took 0.03305 seconds and 4 git commands to generate.