Filter: index array, sequences, implement bitwise binary operators
[lttng-ust.git] / liblttng-ust / lttng-filter.h
1 #ifndef _LTTNG_FILTER_H
2 #define _LTTNG_FILTER_H
3
4 /*
5 * lttng-filter.h
6 *
7 * LTTng UST filter header.
8 *
9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <helper.h>
33 #include <lttng/ust-events.h>
34 #include <lttng/ust-context-provider.h>
35 #include <stdint.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <inttypes.h>
40 #include <limits.h>
41 #include <usterr-signal-safe.h>
42 #include "filter-bytecode.h"
43
44 /* Filter stack length, in number of entries */
45 #define FILTER_STACK_LEN 10 /* includes 2 dummy */
46 #define FILTER_STACK_EMPTY 1
47
48 #define FILTER_MAX_DATA_LEN 65536
49
50 #ifndef min_t
51 #define min_t(type, a, b) \
52 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
53 #endif
54
55 #ifndef likely
56 #define likely(x) __builtin_expect(!!(x), 1)
57 #endif
58
59 #ifndef unlikely
60 #define unlikely(x) __builtin_expect(!!(x), 0)
61 #endif
62
63 #ifdef DEBUG
64 #define dbg_printf(fmt, args...) \
65 printf("[debug bytecode in %s:%s@%u] " fmt, \
66 __FILE__, __func__, __LINE__, ## args)
67 #else
68 #define dbg_printf(fmt, args...) \
69 do { \
70 /* do nothing but check printf format */ \
71 if (0) \
72 printf("[debug bytecode in %s:%s@%u] " fmt, \
73 __FILE__, __func__, __LINE__, ## args); \
74 } while (0)
75 #endif
76
77 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
78 struct bytecode_runtime {
79 struct lttng_bytecode_runtime p;
80 size_t data_len;
81 size_t data_alloc_len;
82 char *data;
83 uint16_t len;
84 char code[0];
85 };
86
87 enum entry_type {
88 REG_S64,
89 REG_DOUBLE,
90 REG_STRING,
91 REG_STAR_GLOB_STRING,
92 REG_UNKNOWN,
93 REG_PTR,
94 };
95
96 enum load_type {
97 LOAD_ROOT_CONTEXT,
98 LOAD_ROOT_APP_CONTEXT,
99 LOAD_ROOT_PAYLOAD,
100 LOAD_OBJECT,
101 };
102
103 enum object_type {
104 OBJECT_TYPE_S8,
105 OBJECT_TYPE_S16,
106 OBJECT_TYPE_S32,
107 OBJECT_TYPE_S64,
108 OBJECT_TYPE_U8,
109 OBJECT_TYPE_U16,
110 OBJECT_TYPE_U32,
111 OBJECT_TYPE_U64,
112
113 OBJECT_TYPE_DOUBLE,
114 OBJECT_TYPE_STRING,
115 OBJECT_TYPE_STRING_SEQUENCE,
116
117 OBJECT_TYPE_SEQUENCE,
118 OBJECT_TYPE_ARRAY,
119 OBJECT_TYPE_STRUCT,
120 OBJECT_TYPE_VARIANT,
121
122 OBJECT_TYPE_DYNAMIC,
123 };
124
125 struct filter_get_index_data {
126 uint64_t offset; /* in bytes */
127 size_t ctx_index;
128 size_t array_len;
129 struct {
130 size_t len;
131 enum object_type type;
132 bool rev_bo; /* reverse byte order */
133 } elem;
134 };
135
136 /* Validation stack */
137 struct vstack_load {
138 enum load_type type;
139 enum object_type object_type;
140 const struct lttng_event_field *field;
141 bool rev_bo; /* reverse byte order */
142 };
143
144 struct vstack_entry {
145 enum entry_type type;
146 struct vstack_load load;
147 };
148
149 struct vstack {
150 int top; /* top of stack */
151 struct vstack_entry e[FILTER_STACK_LEN];
152 };
153
154 static inline
155 void vstack_init(struct vstack *stack)
156 {
157 stack->top = -1;
158 }
159
160 static inline
161 struct vstack_entry *vstack_ax(struct vstack *stack)
162 {
163 if (unlikely(stack->top < 0))
164 return NULL;
165 return &stack->e[stack->top];
166 }
167
168 static inline
169 struct vstack_entry *vstack_bx(struct vstack *stack)
170 {
171 if (unlikely(stack->top < 1))
172 return NULL;
173 return &stack->e[stack->top - 1];
174 }
175
176 static inline
177 int vstack_push(struct vstack *stack)
178 {
179 if (stack->top >= FILTER_STACK_LEN - 1) {
180 ERR("Stack full\n");
181 return -EINVAL;
182 }
183 ++stack->top;
184 return 0;
185 }
186
187 static inline
188 int vstack_pop(struct vstack *stack)
189 {
190 if (unlikely(stack->top < 0)) {
191 ERR("Stack empty\n");
192 return -EINVAL;
193 }
194 stack->top--;
195 return 0;
196 }
197
198 /* Execution stack */
199 enum estack_string_literal_type {
200 ESTACK_STRING_LITERAL_TYPE_NONE,
201 ESTACK_STRING_LITERAL_TYPE_PLAIN,
202 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
203 };
204
205 struct load_ptr {
206 enum load_type type;
207 enum object_type object_type;
208 const void *ptr;
209 bool rev_bo;
210 /* Temporary place-holders for contexts. */
211 union {
212 int64_t s64;
213 uint64_t u64;
214 double d;
215 } u;
216 /*
217 * "field" is only needed when nested under a variant, in which
218 * case we cannot specialize the nested operations.
219 */
220 const struct lttng_event_field *field;
221 };
222
223 struct estack_entry {
224 enum entry_type type; /* For dynamic typing. */
225 union {
226 int64_t v;
227 double d;
228
229 struct {
230 const char *str;
231 size_t seq_len;
232 enum estack_string_literal_type literal_type;
233 } s;
234 struct load_ptr ptr;
235 } u;
236 };
237
238 struct estack {
239 int top; /* top of stack */
240 struct estack_entry e[FILTER_STACK_LEN];
241 };
242
243 /*
244 * Always use aliased type for ax/bx (top of stack).
245 * When ax/bx are S64, use aliased value.
246 */
247 #define estack_ax_v ax
248 #define estack_bx_v bx
249 #define estack_ax_t ax_t
250 #define estack_bx_t bx_t
251
252 /*
253 * ax and bx registers can hold either integer, double or string.
254 */
255 #define estack_ax(stack, top) \
256 ({ \
257 assert((top) > FILTER_STACK_EMPTY); \
258 &(stack)->e[top]; \
259 })
260
261 #define estack_bx(stack, top) \
262 ({ \
263 assert((top) > FILTER_STACK_EMPTY + 1); \
264 &(stack)->e[(top) - 1]; \
265 })
266
267 /*
268 * Currently, only integers (REG_S64) can be pushed into the stack.
269 */
270 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
271 do { \
272 assert((top) < FILTER_STACK_LEN - 1); \
273 (stack)->e[(top) - 1].u.v = (bx); \
274 (stack)->e[(top) - 1].type = (bx_t); \
275 (bx) = (ax); \
276 (bx_t) = (ax_t); \
277 ++(top); \
278 } while (0)
279
280 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
281 do { \
282 assert((top) > FILTER_STACK_EMPTY); \
283 (ax) = (bx); \
284 (ax_t) = (bx_t); \
285 (bx) = (stack)->e[(top) - 2].u.v; \
286 (bx_t) = (stack)->e[(top) - 2].type; \
287 (top)--; \
288 } while (0)
289
290 const char *print_op(enum filter_op op);
291
292 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
293 int lttng_filter_specialize_bytecode(struct lttng_event *event,
294 struct bytecode_runtime *bytecode);
295
296 uint64_t lttng_filter_false(void *filter_data,
297 const char *filter_stack_data);
298 uint64_t lttng_filter_interpret_bytecode(void *filter_data,
299 const char *filter_stack_data);
300
301 #endif /* _LTTNG_FILTER_H */
This page took 0.03554 seconds and 4 git commands to generate.