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