Filter: Implement stack-based interpreter
[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 {
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};
97b58163 148
0305960f
MD
149struct estack {
150 int top; /* top of stack */
151 struct estack_entry e[FILTER_STACK_LEN];
97b58163
MD
152};
153
0305960f
MD
154static inline
155void estack_init(struct estack *stack)
156{
157 stack->top = -1;
158}
159
160static inline
161struct estack_entry *estack_ax(struct estack *stack)
162{
163 assert(stack->top >= 0);
164 return &stack->e[stack->top];
165}
166
167static inline
168struct estack_entry *estack_bx(struct estack *stack)
169{
170 assert(stack->top >= 1);
171 return &stack->e[stack->top - 1];
172}
173
174static inline
175void estack_push(struct estack *stack)
176{
177 assert(stack->top < FILTER_STACK_LEN - 1);
178 ++stack->top;
179}
180
181static inline
182void estack_pop(struct estack *stack)
183{
184 assert(stack->top >= 0);
185 stack->top--;
186}
187
97b58163
MD
188const char *print_op(enum filter_op op);
189
190int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
191int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode);
192
193int lttng_filter_false(void *filter_data,
194 const char *filter_stack_data);
195int lttng_filter_interpret_bytecode(void *filter_data,
196 const char *filter_stack_data);
197
198#endif /* _LTTNG_FILTER_H */
This page took 0.029953 seconds and 4 git commands to generate.