Filter: remove interpreter dynamic typing
[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 *
9 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <errno.h>
27#include <stdio.h>
28#include <helper.h>
29#include <lttng/ust-events.h>
30#include <stdint.h>
0305960f 31#include <assert.h>
97b58163
MD
32#include <errno.h>
33#include <string.h>
34#include <inttypes.h>
35#include <limits.h>
36#include <usterr-signal-safe.h>
37#include "filter-bytecode.h"
38
0305960f
MD
39/* Filter stack length, in number of entries */
40#define FILTER_STACK_LEN 8
97b58163
MD
41
42#ifndef min_t
43#define min_t(type, a, b) \
44 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
45#endif
46
47#ifndef likely
48#define likely(x) __builtin_expect(!!(x), 1)
49#endif
50
51#ifndef unlikely
52#define unlikely(x) __builtin_expect(!!(x), 0)
53#endif
54
55#ifdef DEBUG
56#define dbg_printf(fmt, args...) printf("[debug bytecode] " fmt, ## args)
57#else
58#define dbg_printf(fmt, args...) \
59do { \
60 /* do nothing but check printf format */ \
61 if (0) \
62 printf("[debug bytecode] " fmt, ## args); \
63} while (0)
64#endif
65
66/* Linked bytecode */
67struct bytecode_runtime {
68 uint16_t len;
69 char data[0];
70};
71
0305960f 72enum entry_type {
97b58163
MD
73 REG_S64,
74 REG_DOUBLE,
75 REG_STRING,
76 REG_TYPE_UNKNOWN,
77};
78
0305960f
MD
79/* Validation stack */
80struct vstack_entry {
81 enum entry_type type;
97b58163
MD
82};
83
0305960f
MD
84struct vstack {
85 int top; /* top of stack */
86 struct vstack_entry e[FILTER_STACK_LEN];
87};
88
89static inline
90void vstack_init(struct vstack *stack)
91{
92 stack->top = -1;
93}
94
95static inline
96struct vstack_entry *vstack_ax(struct vstack *stack)
97{
98 if (unlikely(stack->top < 0))
99 return NULL;
100 return &stack->e[stack->top];
101}
102
103static inline
104struct vstack_entry *vstack_bx(struct vstack *stack)
105{
106 if (unlikely(stack->top < 1))
107 return NULL;
108 return &stack->e[stack->top - 1];
109}
110
111static inline
112int vstack_push(struct vstack *stack)
113{
114 if (stack->top >= FILTER_STACK_LEN - 1) {
115 ERR("Stack full\n");
116 return -EINVAL;
117 }
118 ++stack->top;
119 return 0;
120}
121
122static inline
123int vstack_pop(struct vstack *stack)
124{
125 if (unlikely(stack->top < 0)) {
126 ERR("Stack empty\n");
127 return -EINVAL;
128 }
129 stack->top--;
130 return 0;
131}
132
133/* Execution stack */
134struct estack_entry {
0305960f
MD
135 union {
136 int64_t v;
137 double d;
138
139 struct {
140 const char *str;
141 size_t seq_len;
142 int literal; /* is string literal ? */
143 } s;
144 } u;
145};
97b58163 146
0305960f
MD
147struct estack {
148 int top; /* top of stack */
149 struct estack_entry e[FILTER_STACK_LEN];
97b58163
MD
150};
151
0305960f
MD
152static inline
153void estack_init(struct estack *stack)
154{
155 stack->top = -1;
156}
157
158static inline
159struct estack_entry *estack_ax(struct estack *stack)
160{
161 assert(stack->top >= 0);
162 return &stack->e[stack->top];
163}
164
165static inline
166struct estack_entry *estack_bx(struct estack *stack)
167{
168 assert(stack->top >= 1);
169 return &stack->e[stack->top - 1];
170}
171
172static inline
173void estack_push(struct estack *stack)
174{
175 assert(stack->top < FILTER_STACK_LEN - 1);
176 ++stack->top;
177}
178
179static inline
180void estack_pop(struct estack *stack)
181{
182 assert(stack->top >= 0);
183 stack->top--;
184}
185
97b58163
MD
186const char *print_op(enum filter_op op);
187
188int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
189int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
190
191int lttng_filter_false(void *filter_data,
192 const char *filter_stack_data);
193int lttng_filter_interpret_bytecode(void *filter_data,
194 const char *filter_stack_data);
195
196#endif /* _LTTNG_FILTER_H */
This page took 0.03123 seconds and 4 git commands to generate.