Filter: Implement stack-based interpreter
[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-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>
31 #include <assert.h>
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
39 /* Filter stack length, in number of entries */
40 #define FILTER_STACK_LEN 8
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...) \
59 do { \
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 */
67 struct bytecode_runtime {
68 uint16_t len;
69 char data[0];
70 };
71
72 enum entry_type {
73 REG_S64,
74 REG_DOUBLE,
75 REG_STRING,
76 REG_TYPE_UNKNOWN,
77 };
78
79 /* Validation stack */
80 struct vstack_entry {
81 enum entry_type type;
82 };
83
84 struct vstack {
85 int top; /* top of stack */
86 struct vstack_entry e[FILTER_STACK_LEN];
87 };
88
89 static inline
90 void vstack_init(struct vstack *stack)
91 {
92 stack->top = -1;
93 }
94
95 static inline
96 struct 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
103 static inline
104 struct 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
111 static inline
112 int 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
122 static inline
123 int 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 */
134 struct estack_entry {
135 enum entry_type type;
136
137 union {
138 int64_t v;
139 double d;
140
141 struct {
142 const char *str;
143 size_t seq_len;
144 int literal; /* is string literal ? */
145 } s;
146 } u;
147 };
148
149 struct estack {
150 int top; /* top of stack */
151 struct estack_entry e[FILTER_STACK_LEN];
152 };
153
154 static inline
155 void estack_init(struct estack *stack)
156 {
157 stack->top = -1;
158 }
159
160 static inline
161 struct estack_entry *estack_ax(struct estack *stack)
162 {
163 assert(stack->top >= 0);
164 return &stack->e[stack->top];
165 }
166
167 static inline
168 struct estack_entry *estack_bx(struct estack *stack)
169 {
170 assert(stack->top >= 1);
171 return &stack->e[stack->top - 1];
172 }
173
174 static inline
175 void estack_push(struct estack *stack)
176 {
177 assert(stack->top < FILTER_STACK_LEN - 1);
178 ++stack->top;
179 }
180
181 static inline
182 void estack_pop(struct estack *stack)
183 {
184 assert(stack->top >= 0);
185 stack->top--;
186 }
187
188 const char *print_op(enum filter_op op);
189
190 int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
191 int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
192
193 int lttng_filter_false(void *filter_data,
194 const char *filter_stack_data);
195 int lttng_filter_interpret_bytecode(void *filter_data,
196 const char *filter_stack_data);
197
198 #endif /* _LTTNG_FILTER_H */
This page took 0.03484 seconds and 5 git commands to generate.