Filter code relicensing to MIT license
[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 #ifndef min_t
49 #define min_t(type, a, b) \
50 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
51 #endif
52
53 #ifndef likely
54 #define likely(x) __builtin_expect(!!(x), 1)
55 #endif
56
57 #ifndef unlikely
58 #define unlikely(x) __builtin_expect(!!(x), 0)
59 #endif
60
61 #ifdef DEBUG
62 #define dbg_printf(fmt, args...) \
63 printf("[debug bytecode in %s:%s@%u] " fmt, \
64 __FILE__, __func__, __LINE__, ## args)
65 #else
66 #define dbg_printf(fmt, args...) \
67 do { \
68 /* do nothing but check printf format */ \
69 if (0) \
70 printf("[debug bytecode in %s:%s@%u] " fmt, \
71 __FILE__, __func__, __LINE__, ## args); \
72 } while (0)
73 #endif
74
75 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
76 struct bytecode_runtime {
77 struct lttng_bytecode_runtime p;
78 uint16_t len;
79 char data[0];
80 };
81
82 enum entry_type {
83 REG_S64,
84 REG_DOUBLE,
85 REG_STRING,
86 REG_UNKNOWN,
87 };
88
89 /* Validation stack */
90 struct vstack_entry {
91 enum entry_type type;
92 };
93
94 struct vstack {
95 int top; /* top of stack */
96 struct vstack_entry e[FILTER_STACK_LEN];
97 };
98
99 static inline
100 void vstack_init(struct vstack *stack)
101 {
102 stack->top = -1;
103 }
104
105 static inline
106 struct vstack_entry *vstack_ax(struct vstack *stack)
107 {
108 if (unlikely(stack->top < 0))
109 return NULL;
110 return &stack->e[stack->top];
111 }
112
113 static inline
114 struct vstack_entry *vstack_bx(struct vstack *stack)
115 {
116 if (unlikely(stack->top < 1))
117 return NULL;
118 return &stack->e[stack->top - 1];
119 }
120
121 static inline
122 int vstack_push(struct vstack *stack)
123 {
124 if (stack->top >= FILTER_STACK_LEN - 1) {
125 ERR("Stack full\n");
126 return -EINVAL;
127 }
128 ++stack->top;
129 return 0;
130 }
131
132 static inline
133 int vstack_pop(struct vstack *stack)
134 {
135 if (unlikely(stack->top < 0)) {
136 ERR("Stack empty\n");
137 return -EINVAL;
138 }
139 stack->top--;
140 return 0;
141 }
142
143 /* Execution stack */
144 struct estack_entry {
145 enum entry_type type; /* For dynamic typing. */
146 union {
147 int64_t v;
148 double d;
149
150 struct {
151 const char *str;
152 size_t seq_len;
153 int literal; /* is string literal ? */
154 } s;
155 } u;
156 };
157
158 struct estack {
159 int top; /* top of stack */
160 struct estack_entry e[FILTER_STACK_LEN];
161 };
162
163 /*
164 * Always use aliased type for ax/bx (top of stack).
165 * When ax/bx are S64, use aliased value.
166 */
167 #define estack_ax_v ax
168 #define estack_bx_v bx
169 #define estack_ax_t ax_t
170 #define estack_bx_t bx_t
171
172 /*
173 * ax and bx registers can hold either integer, double or string.
174 */
175 #define estack_ax(stack, top) \
176 ({ \
177 assert((top) > FILTER_STACK_EMPTY); \
178 &(stack)->e[top]; \
179 })
180
181 #define estack_bx(stack, top) \
182 ({ \
183 assert((top) > FILTER_STACK_EMPTY + 1); \
184 &(stack)->e[(top) - 1]; \
185 })
186
187 /*
188 * Currently, only integers (REG_S64) can be pushed into the stack.
189 */
190 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
191 do { \
192 assert((top) < FILTER_STACK_LEN - 1); \
193 (stack)->e[(top) - 1].u.v = (bx); \
194 (stack)->e[(top) - 1].type = (bx_t); \
195 (bx) = (ax); \
196 (bx_t) = (ax_t); \
197 ++(top); \
198 } while (0)
199
200 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
201 do { \
202 assert((top) > FILTER_STACK_EMPTY); \
203 (ax) = (bx); \
204 (ax_t) = (bx_t); \
205 (bx) = (stack)->e[(top) - 2].u.v; \
206 (bx_t) = (stack)->e[(top) - 2].type; \
207 (top)--; \
208 } while (0)
209
210 const char *print_op(enum filter_op op);
211
212 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
213 int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
214
215 uint64_t lttng_filter_false(void *filter_data,
216 const char *filter_stack_data);
217 uint64_t lttng_filter_interpret_bytecode(void *filter_data,
218 const char *filter_stack_data);
219
220 #endif /* _LTTNG_FILTER_H */
This page took 0.032809 seconds and 4 git commands to generate.