bytecode: add `REG_U64` interpreter register type
[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 <stdbool.h>
33 #include <helper.h>
34 #include <lttng/ust-events.h>
35 #include <lttng/ust-context-provider.h>
36 #include <stdint.h>
37 #include <assert.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <inttypes.h>
41 #include <limits.h>
42 #include <usterr-signal-safe.h>
43 #include "filter-bytecode.h"
44
45 /* Filter stack length, in number of entries */
46 #define FILTER_STACK_LEN 10 /* includes 2 dummy */
47 #define FILTER_STACK_EMPTY 1
48
49 #define FILTER_MAX_DATA_LEN 65536
50
51 #ifndef min_t
52 #define min_t(type, a, b) \
53 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
54 #endif
55
56 #ifndef likely
57 #define likely(x) __builtin_expect(!!(x), 1)
58 #endif
59
60 #ifndef unlikely
61 #define unlikely(x) __builtin_expect(!!(x), 0)
62 #endif
63
64 #ifdef DEBUG
65 #define dbg_printf(fmt, args...) \
66 printf("[debug bytecode in %s:%s@%u] " fmt, \
67 __FILE__, __func__, __LINE__, ## args)
68 #else
69 #define dbg_printf(fmt, args...) \
70 do { \
71 /* do nothing but check printf format */ \
72 if (0) \
73 printf("[debug bytecode in %s:%s@%u] " fmt, \
74 __FILE__, __func__, __LINE__, ## args); \
75 } while (0)
76 #endif
77
78 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
79 struct bytecode_runtime {
80 struct lttng_bytecode_runtime p;
81 size_t data_len;
82 size_t data_alloc_len;
83 char *data;
84 uint16_t len;
85 char code[0];
86 };
87
88 enum entry_type {
89 REG_S64,
90 REG_U64,
91 REG_DOUBLE,
92 REG_STRING,
93 REG_STAR_GLOB_STRING,
94 REG_UNKNOWN,
95 REG_PTR,
96 };
97
98 enum load_type {
99 LOAD_ROOT_CONTEXT,
100 LOAD_ROOT_APP_CONTEXT,
101 LOAD_ROOT_PAYLOAD,
102 LOAD_OBJECT,
103 };
104
105 enum object_type {
106 OBJECT_TYPE_S8,
107 OBJECT_TYPE_S16,
108 OBJECT_TYPE_S32,
109 OBJECT_TYPE_S64,
110 OBJECT_TYPE_U8,
111 OBJECT_TYPE_U16,
112 OBJECT_TYPE_U32,
113 OBJECT_TYPE_U64,
114
115 OBJECT_TYPE_DOUBLE,
116 OBJECT_TYPE_STRING,
117 OBJECT_TYPE_STRING_SEQUENCE,
118
119 OBJECT_TYPE_SEQUENCE,
120 OBJECT_TYPE_ARRAY,
121 OBJECT_TYPE_STRUCT,
122 OBJECT_TYPE_VARIANT,
123
124 OBJECT_TYPE_DYNAMIC,
125 };
126
127 struct filter_get_index_data {
128 uint64_t offset; /* in bytes */
129 size_t ctx_index;
130 size_t array_len;
131 struct {
132 size_t len;
133 enum object_type type;
134 bool rev_bo; /* reverse byte order */
135 } elem;
136 };
137
138 /* Validation stack */
139 struct vstack_load {
140 enum load_type type;
141 enum object_type object_type;
142 const struct lttng_event_field *field;
143 bool rev_bo; /* reverse byte order */
144 };
145
146 struct vstack_entry {
147 enum entry_type type;
148 struct vstack_load load;
149 };
150
151 struct vstack {
152 int top; /* top of stack */
153 struct vstack_entry e[FILTER_STACK_LEN];
154 };
155
156 static inline
157 void vstack_init(struct vstack *stack)
158 {
159 stack->top = -1;
160 }
161
162 static inline
163 struct vstack_entry *vstack_ax(struct vstack *stack)
164 {
165 if (unlikely(stack->top < 0))
166 return NULL;
167 return &stack->e[stack->top];
168 }
169
170 static inline
171 struct vstack_entry *vstack_bx(struct vstack *stack)
172 {
173 if (unlikely(stack->top < 1))
174 return NULL;
175 return &stack->e[stack->top - 1];
176 }
177
178 static inline
179 int vstack_push(struct vstack *stack)
180 {
181 if (stack->top >= FILTER_STACK_LEN - 1) {
182 ERR("Stack full\n");
183 return -EINVAL;
184 }
185 ++stack->top;
186 return 0;
187 }
188
189 static inline
190 int vstack_pop(struct vstack *stack)
191 {
192 if (unlikely(stack->top < 0)) {
193 ERR("Stack empty\n");
194 return -EINVAL;
195 }
196 stack->top--;
197 return 0;
198 }
199
200 /* Execution stack */
201 enum estack_string_literal_type {
202 ESTACK_STRING_LITERAL_TYPE_NONE,
203 ESTACK_STRING_LITERAL_TYPE_PLAIN,
204 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
205 };
206
207 struct load_ptr {
208 enum load_type type;
209 enum object_type object_type;
210 const void *ptr;
211 bool rev_bo;
212 /* Temporary place-holders for contexts. */
213 union {
214 int64_t s64;
215 uint64_t u64;
216 double d;
217 } u;
218 /*
219 * "field" is only needed when nested under a variant, in which
220 * case we cannot specialize the nested operations.
221 */
222 const struct lttng_event_field *field;
223 };
224
225 struct estack_entry {
226 enum entry_type type; /* For dynamic typing. */
227 union {
228 int64_t v;
229 double d;
230
231 struct {
232 const char *str;
233 size_t seq_len;
234 enum estack_string_literal_type literal_type;
235 } s;
236 struct load_ptr ptr;
237 } u;
238 };
239
240 struct estack {
241 int top; /* top of stack */
242 struct estack_entry e[FILTER_STACK_LEN];
243 };
244
245 /*
246 * Always use aliased type for ax/bx (top of stack).
247 * When ax/bx are S64, use aliased value.
248 */
249 #define estack_ax_v ax
250 #define estack_bx_v bx
251 #define estack_ax_t ax_t
252 #define estack_bx_t bx_t
253
254 /*
255 * ax and bx registers can hold either integer, double or string.
256 */
257 #define estack_ax(stack, top) \
258 ({ \
259 assert((top) > FILTER_STACK_EMPTY); \
260 &(stack)->e[top]; \
261 })
262
263 #define estack_bx(stack, top) \
264 ({ \
265 assert((top) > FILTER_STACK_EMPTY + 1); \
266 &(stack)->e[(top) - 1]; \
267 })
268
269 /*
270 * Currently, only integers (REG_S64) can be pushed into the stack.
271 */
272 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
273 do { \
274 assert((top) < FILTER_STACK_LEN - 1); \
275 (stack)->e[(top) - 1].u.v = (bx); \
276 (stack)->e[(top) - 1].type = (bx_t); \
277 (bx) = (ax); \
278 (bx_t) = (ax_t); \
279 ++(top); \
280 } while (0)
281
282 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
283 do { \
284 assert((top) > FILTER_STACK_EMPTY); \
285 (ax) = (bx); \
286 (ax_t) = (bx_t); \
287 (bx) = (stack)->e[(top) - 2].u.v; \
288 (bx_t) = (stack)->e[(top) - 2].type; \
289 (top)--; \
290 } while (0)
291
292 const char *print_op(enum filter_op op);
293
294 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
295 int lttng_filter_specialize_bytecode(const struct lttng_event_desc *event_desc,
296 struct bytecode_runtime *bytecode);
297
298 uint64_t lttng_filter_false(void *filter_data,
299 const char *filter_stack_data);
300 uint64_t lttng_filter_interpret_bytecode(void *filter_data,
301 const char *filter_stack_data);
302
303 #endif /* _LTTNG_FILTER_H */
This page took 0.036088 seconds and 5 git commands to generate.