1 #ifndef _LTTNG_FILTER_H
2 #define _LTTNG_FILTER_H
7 * LTTng UST filter header.
9 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
33 #include <lttng/ust-events.h>
34 #include <lttng/ust-context-provider.h>
41 #include <usterr-signal-safe.h>
42 #include "filter-bytecode.h"
44 /* Filter stack length, in number of entries */
45 #define FILTER_STACK_LEN 10 /* includes 2 dummy */
46 #define FILTER_STACK_EMPTY 1
48 #define FILTER_MAX_DATA_LEN 65536
51 #define min_t(type, a, b) \
52 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
56 #define likely(x) __builtin_expect(!!(x), 1)
60 #define unlikely(x) __builtin_expect(!!(x), 0)
64 #define dbg_printf(fmt, args...) \
65 printf("[debug bytecode in %s:%s@%u] " fmt, \
66 __FILE__, __func__, __LINE__, ## args)
68 #define dbg_printf(fmt, args...) \
70 /* do nothing but check printf format */ \
72 printf("[debug bytecode in %s:%s@%u] " fmt, \
73 __FILE__, __func__, __LINE__, ## args); \
77 /* Linked bytecode. Child of struct lttng_bytecode_runtime. */
78 struct bytecode_runtime
{
79 struct lttng_bytecode_runtime p
;
81 size_t data_alloc_len
;
98 LOAD_ROOT_APP_CONTEXT
,
115 OBJECT_TYPE_STRING_SEQUENCE
,
117 OBJECT_TYPE_SEQUENCE
,
125 struct filter_get_index_data
{
126 uint64_t offset
; /* in bytes */
131 enum object_type type
;
132 bool rev_bo
; /* reverse byte order */
136 /* Validation stack */
139 enum object_type object_type
;
140 const struct lttng_event_field
*field
;
141 bool rev_bo
; /* reverse byte order */
144 struct vstack_entry
{
145 enum entry_type type
;
146 struct vstack_load load
;
150 int top
; /* top of stack */
151 struct vstack_entry e
[FILTER_STACK_LEN
];
155 void vstack_init(struct vstack
*stack
)
161 struct vstack_entry
*vstack_ax(struct vstack
*stack
)
163 if (unlikely(stack
->top
< 0))
165 return &stack
->e
[stack
->top
];
169 struct vstack_entry
*vstack_bx(struct vstack
*stack
)
171 if (unlikely(stack
->top
< 1))
173 return &stack
->e
[stack
->top
- 1];
177 int vstack_push(struct vstack
*stack
)
179 if (stack
->top
>= FILTER_STACK_LEN
- 1) {
188 int vstack_pop(struct vstack
*stack
)
190 if (unlikely(stack
->top
< 0)) {
191 ERR("Stack empty\n");
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
,
207 enum object_type object_type
;
210 /* Temporary place-holders for contexts. */
217 * "field" is only needed when nested under a variant, in which
218 * case we cannot specialize the nested operations.
220 const struct lttng_event_field
*field
;
223 struct estack_entry
{
224 enum entry_type type
; /* For dynamic typing. */
232 enum estack_string_literal_type literal_type
;
239 int top
; /* top of stack */
240 struct estack_entry e
[FILTER_STACK_LEN
];
244 * Always use aliased type for ax/bx (top of stack).
245 * When ax/bx are S64, use aliased value.
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
253 * ax and bx registers can hold either integer, double or string.
255 #define estack_ax(stack, top) \
257 assert((top) > FILTER_STACK_EMPTY); \
261 #define estack_bx(stack, top) \
263 assert((top) > FILTER_STACK_EMPTY + 1); \
264 &(stack)->e[(top) - 1]; \
268 * Currently, only integers (REG_S64) can be pushed into the stack.
270 #define estack_push(stack, top, ax, bx, ax_t, bx_t) \
272 assert((top) < FILTER_STACK_LEN - 1); \
273 (stack)->e[(top) - 1].u.v = (bx); \
274 (stack)->e[(top) - 1].type = (bx_t); \
280 #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
282 assert((top) > FILTER_STACK_EMPTY); \
285 (bx) = (stack)->e[(top) - 2].u.v; \
286 (bx_t) = (stack)->e[(top) - 2].type; \
290 const char *print_op(enum filter_op op
);
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
);
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
);
301 #endif /* _LTTNG_FILTER_H */