Commit | Line | Data |
---|---|---|
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 | 39 | /* Filter stack length, in number of entries */ |
9b33aac4 MD |
40 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
41 | #define FILTER_STACK_EMPTY 1 | |
97b58163 MD |
42 | |
43 | #ifndef min_t | |
44 | #define min_t(type, a, b) \ | |
45 | ((type) (a) < (type) (b) ? (type) (a) : (type) (b)) | |
46 | #endif | |
47 | ||
48 | #ifndef likely | |
49 | #define likely(x) __builtin_expect(!!(x), 1) | |
50 | #endif | |
51 | ||
52 | #ifndef unlikely | |
53 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
54 | #endif | |
55 | ||
56 | #ifdef DEBUG | |
57 | #define dbg_printf(fmt, args...) printf("[debug bytecode] " fmt, ## args) | |
58 | #else | |
59 | #define dbg_printf(fmt, args...) \ | |
60 | do { \ | |
61 | /* do nothing but check printf format */ \ | |
62 | if (0) \ | |
63 | printf("[debug bytecode] " fmt, ## args); \ | |
64 | } while (0) | |
65 | #endif | |
66 | ||
67 | /* Linked bytecode */ | |
68 | struct bytecode_runtime { | |
69 | uint16_t len; | |
70 | char data[0]; | |
71 | }; | |
72 | ||
0305960f | 73 | enum entry_type { |
97b58163 MD |
74 | REG_S64, |
75 | REG_DOUBLE, | |
76 | REG_STRING, | |
77 | REG_TYPE_UNKNOWN, | |
78 | }; | |
79 | ||
0305960f MD |
80 | /* Validation stack */ |
81 | struct vstack_entry { | |
82 | enum entry_type type; | |
97b58163 MD |
83 | }; |
84 | ||
0305960f MD |
85 | struct vstack { |
86 | int top; /* top of stack */ | |
87 | struct vstack_entry e[FILTER_STACK_LEN]; | |
88 | }; | |
89 | ||
90 | static inline | |
91 | void vstack_init(struct vstack *stack) | |
92 | { | |
93 | stack->top = -1; | |
94 | } | |
95 | ||
96 | static inline | |
97 | struct vstack_entry *vstack_ax(struct vstack *stack) | |
98 | { | |
99 | if (unlikely(stack->top < 0)) | |
100 | return NULL; | |
101 | return &stack->e[stack->top]; | |
102 | } | |
103 | ||
104 | static inline | |
105 | struct vstack_entry *vstack_bx(struct vstack *stack) | |
106 | { | |
107 | if (unlikely(stack->top < 1)) | |
108 | return NULL; | |
109 | return &stack->e[stack->top - 1]; | |
110 | } | |
111 | ||
112 | static inline | |
113 | int vstack_push(struct vstack *stack) | |
114 | { | |
115 | if (stack->top >= FILTER_STACK_LEN - 1) { | |
116 | ERR("Stack full\n"); | |
117 | return -EINVAL; | |
118 | } | |
119 | ++stack->top; | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static inline | |
124 | int vstack_pop(struct vstack *stack) | |
125 | { | |
126 | if (unlikely(stack->top < 0)) { | |
127 | ERR("Stack empty\n"); | |
128 | return -EINVAL; | |
129 | } | |
130 | stack->top--; | |
131 | return 0; | |
132 | } | |
133 | ||
134 | /* Execution stack */ | |
135 | struct estack_entry { | |
0305960f MD |
136 | union { |
137 | int64_t v; | |
138 | double d; | |
139 | ||
140 | struct { | |
141 | const char *str; | |
9b33aac4 MD |
142 | size_t seq_len; |
143 | int literal; /* is string literal ? */ | |
0305960f MD |
144 | } s; |
145 | } u; | |
146 | }; | |
97b58163 | 147 | |
0305960f MD |
148 | struct estack { |
149 | int top; /* top of stack */ | |
150 | struct estack_entry e[FILTER_STACK_LEN]; | |
97b58163 MD |
151 | }; |
152 | ||
9b33aac4 MD |
153 | #define estack_ax_v ax |
154 | #define estack_bx_v bx | |
155 | ||
156 | #define estack_ax(stack, top) \ | |
157 | ({ \ | |
158 | assert((top) > FILTER_STACK_EMPTY); \ | |
159 | &(stack)->e[top]; \ | |
160 | }) | |
161 | ||
162 | #define estack_bx(stack, top) \ | |
163 | ({ \ | |
164 | assert((top) > FILTER_STACK_EMPTY + 1); \ | |
165 | &(stack)->e[(top) - 1]; \ | |
166 | }) | |
167 | ||
168 | #define estack_push(stack, top, ax, bx) \ | |
169 | do { \ | |
170 | assert((top) < FILTER_STACK_LEN - 1); \ | |
171 | (stack)->e[(top) - 1].u.v = (bx); \ | |
172 | (bx) = (ax); \ | |
173 | ++(top); \ | |
174 | } while (0) | |
175 | ||
176 | #define estack_pop(stack, top, ax, bx) \ | |
177 | do { \ | |
178 | assert((top) > FILTER_STACK_EMPTY); \ | |
179 | (ax) = (bx); \ | |
180 | (bx) = (stack)->e[(top) - 2].u.v; \ | |
181 | (top)--; \ | |
182 | } while (0) | |
0305960f | 183 | |
97b58163 MD |
184 | const char *print_op(enum filter_op op); |
185 | ||
186 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); | |
187 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); | |
188 | ||
189 | int lttng_filter_false(void *filter_data, | |
190 | const char *filter_stack_data); | |
191 | int lttng_filter_interpret_bytecode(void *filter_data, | |
192 | const char *filter_stack_data); | |
193 | ||
194 | #endif /* _LTTNG_FILTER_H */ |