Filter: index array, sequences, implement bitwise binary operators
[lttng-ust.git] / liblttng-ust / lttng-filter.h
CommitLineData
97b58163
MD
1#ifndef _LTTNG_FILTER_H
2#define _LTTNG_FILTER_H
3
4/*
5 * lttng-filter.h
6 *
7 * LTTng UST filter header.
8 *
7e50015d 9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
97b58163 10 *
7e50015d
MD
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:
97b58163 17 *
7e50015d
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
97b58163 20 *
7e50015d
MD
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.
97b58163
MD
28 */
29
30#include <errno.h>
31#include <stdio.h>
32#include <helper.h>
33#include <lttng/ust-events.h>
53569322 34#include <lttng/ust-context-provider.h>
97b58163 35#include <stdint.h>
0305960f 36#include <assert.h>
97b58163
MD
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
0305960f 44/* Filter stack length, in number of entries */
9b33aac4
MD
45#define FILTER_STACK_LEN 10 /* includes 2 dummy */
46#define FILTER_STACK_EMPTY 1
97b58163 47
47e5f13e
MD
48#define FILTER_MAX_DATA_LEN 65536
49
97b58163
MD
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
f488575f
MD
64#define dbg_printf(fmt, args...) \
65 printf("[debug bytecode in %s:%s@%u] " fmt, \
66 __FILE__, __func__, __LINE__, ## args)
97b58163
MD
67#else
68#define dbg_printf(fmt, args...) \
69do { \
70 /* do nothing but check printf format */ \
71 if (0) \
f488575f
MD
72 printf("[debug bytecode in %s:%s@%u] " fmt, \
73 __FILE__, __func__, __LINE__, ## args); \
97b58163
MD
74} while (0)
75#endif
76
f488575f 77/* Linked bytecode. Child of struct lttng_bytecode_runtime. */
97b58163 78struct bytecode_runtime {
f488575f 79 struct lttng_bytecode_runtime p;
47e5f13e
MD
80 size_t data_len;
81 size_t data_alloc_len;
82 char *data;
97b58163 83 uint16_t len;
47e5f13e 84 char code[0];
97b58163
MD
85};
86
0305960f 87enum entry_type {
97b58163
MD
88 REG_S64,
89 REG_DOUBLE,
90 REG_STRING,
3151a51d 91 REG_STAR_GLOB_STRING,
53569322 92 REG_UNKNOWN,
47e5f13e
MD
93 REG_PTR,
94};
95
96enum load_type {
97 LOAD_ROOT_CONTEXT,
98 LOAD_ROOT_APP_CONTEXT,
99 LOAD_ROOT_PAYLOAD,
100 LOAD_OBJECT,
101};
102
103enum 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
125struct 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;
97b58163
MD
134};
135
0305960f 136/* Validation stack */
47e5f13e
MD
137struct 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
0305960f
MD
144struct vstack_entry {
145 enum entry_type type;
47e5f13e 146 struct vstack_load load;
97b58163
MD
147};
148
0305960f
MD
149struct vstack {
150 int top; /* top of stack */
151 struct vstack_entry e[FILTER_STACK_LEN];
152};
153
154static inline
155void vstack_init(struct vstack *stack)
156{
157 stack->top = -1;
158}
159
160static inline
161struct 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
168static inline
169struct 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
176static inline
177int 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
187static inline
188int 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 */
3151a51d
PP
199enum 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
47e5f13e
MD
205struct 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
0305960f 223struct estack_entry {
53569322 224 enum entry_type type; /* For dynamic typing. */
0305960f
MD
225 union {
226 int64_t v;
227 double d;
228
229 struct {
230 const char *str;
9b33aac4 231 size_t seq_len;
3151a51d 232 enum estack_string_literal_type literal_type;
0305960f 233 } s;
47e5f13e 234 struct load_ptr ptr;
0305960f
MD
235 } u;
236};
97b58163 237
0305960f
MD
238struct estack {
239 int top; /* top of stack */
240 struct estack_entry e[FILTER_STACK_LEN];
97b58163
MD
241};
242
53569322
MD
243/*
244 * Always use aliased type for ax/bx (top of stack).
245 * When ax/bx are S64, use aliased value.
246 */
9b33aac4
MD
247#define estack_ax_v ax
248#define estack_bx_v bx
53569322
MD
249#define estack_ax_t ax_t
250#define estack_bx_t bx_t
9b33aac4 251
53569322
MD
252/*
253 * ax and bx registers can hold either integer, double or string.
254 */
9b33aac4
MD
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
53569322
MD
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) \
9b33aac4
MD
271 do { \
272 assert((top) < FILTER_STACK_LEN - 1); \
273 (stack)->e[(top) - 1].u.v = (bx); \
53569322 274 (stack)->e[(top) - 1].type = (bx_t); \
9b33aac4 275 (bx) = (ax); \
53569322 276 (bx_t) = (ax_t); \
9b33aac4
MD
277 ++(top); \
278 } while (0)
279
53569322 280#define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
9b33aac4
MD
281 do { \
282 assert((top) > FILTER_STACK_EMPTY); \
283 (ax) = (bx); \
53569322 284 (ax_t) = (bx_t); \
9b33aac4 285 (bx) = (stack)->e[(top) - 2].u.v; \
53569322 286 (bx_t) = (stack)->e[(top) - 2].type; \
9b33aac4
MD
287 (top)--; \
288 } while (0)
0305960f 289
97b58163
MD
290const char *print_op(enum filter_op op);
291
292int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
47e5f13e
MD
293int lttng_filter_specialize_bytecode(struct lttng_event *event,
294 struct bytecode_runtime *bytecode);
97b58163 295
8a92ed2a 296uint64_t lttng_filter_false(void *filter_data,
97b58163 297 const char *filter_stack_data);
8a92ed2a 298uint64_t lttng_filter_interpret_bytecode(void *filter_data,
97b58163
MD
299 const char *filter_stack_data);
300
301#endif /* _LTTNG_FILTER_H */
This page took 0.03889 seconds and 4 git commands to generate.