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 | * | |
7e50015d | 9 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
97b58163 | 10 | * |
7e50015d MD |
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: | |
97b58163 | 17 | * |
7e50015d MD |
18 | * The above copyright notice and this permission notice shall be included in |
19 | * all copies or substantial portions of the Software. | |
97b58163 | 20 | * |
7e50015d MD |
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 | |
27 | * SOFTWARE. | |
97b58163 MD |
28 | */ |
29 | ||
30 | #include <errno.h> | |
31 | #include <stdio.h> | |
32 | #include <helper.h> | |
33 | #include <lttng/ust-events.h> | |
53569322 | 34 | #include <lttng/ust-context-provider.h> |
97b58163 | 35 | #include <stdint.h> |
0305960f | 36 | #include <assert.h> |
97b58163 MD |
37 | #include <errno.h> |
38 | #include <string.h> | |
39 | #include <inttypes.h> | |
40 | #include <limits.h> | |
41 | #include <usterr-signal-safe.h> | |
42 | #include "filter-bytecode.h" | |
43 | ||
0305960f | 44 | /* Filter stack length, in number of entries */ |
9b33aac4 MD |
45 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
46 | #define FILTER_STACK_EMPTY 1 | |
97b58163 MD |
47 | |
48 | #ifndef min_t | |
49 | #define min_t(type, a, b) \ | |
50 | ((type) (a) < (type) (b) ? (type) (a) : (type) (b)) | |
51 | #endif | |
52 | ||
53 | #ifndef likely | |
54 | #define likely(x) __builtin_expect(!!(x), 1) | |
55 | #endif | |
56 | ||
57 | #ifndef unlikely | |
58 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
59 | #endif | |
60 | ||
61 | #ifdef DEBUG | |
f488575f MD |
62 | #define dbg_printf(fmt, args...) \ |
63 | printf("[debug bytecode in %s:%s@%u] " fmt, \ | |
64 | __FILE__, __func__, __LINE__, ## args) | |
97b58163 MD |
65 | #else |
66 | #define dbg_printf(fmt, args...) \ | |
67 | do { \ | |
68 | /* do nothing but check printf format */ \ | |
69 | if (0) \ | |
f488575f MD |
70 | printf("[debug bytecode in %s:%s@%u] " fmt, \ |
71 | __FILE__, __func__, __LINE__, ## args); \ | |
97b58163 MD |
72 | } while (0) |
73 | #endif | |
74 | ||
f488575f | 75 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
97b58163 | 76 | struct bytecode_runtime { |
f488575f | 77 | struct lttng_bytecode_runtime p; |
97b58163 MD |
78 | uint16_t len; |
79 | char data[0]; | |
80 | }; | |
81 | ||
0305960f | 82 | enum entry_type { |
97b58163 MD |
83 | REG_S64, |
84 | REG_DOUBLE, | |
85 | REG_STRING, | |
3151a51d | 86 | REG_STAR_GLOB_STRING, |
53569322 | 87 | REG_UNKNOWN, |
97b58163 MD |
88 | }; |
89 | ||
0305960f MD |
90 | /* Validation stack */ |
91 | struct vstack_entry { | |
92 | enum entry_type type; | |
97b58163 MD |
93 | }; |
94 | ||
0305960f MD |
95 | struct vstack { |
96 | int top; /* top of stack */ | |
97 | struct vstack_entry e[FILTER_STACK_LEN]; | |
98 | }; | |
99 | ||
100 | static inline | |
101 | void vstack_init(struct vstack *stack) | |
102 | { | |
103 | stack->top = -1; | |
104 | } | |
105 | ||
106 | static inline | |
107 | struct vstack_entry *vstack_ax(struct vstack *stack) | |
108 | { | |
109 | if (unlikely(stack->top < 0)) | |
110 | return NULL; | |
111 | return &stack->e[stack->top]; | |
112 | } | |
113 | ||
114 | static inline | |
115 | struct vstack_entry *vstack_bx(struct vstack *stack) | |
116 | { | |
117 | if (unlikely(stack->top < 1)) | |
118 | return NULL; | |
119 | return &stack->e[stack->top - 1]; | |
120 | } | |
121 | ||
122 | static inline | |
123 | int vstack_push(struct vstack *stack) | |
124 | { | |
125 | if (stack->top >= FILTER_STACK_LEN - 1) { | |
126 | ERR("Stack full\n"); | |
127 | return -EINVAL; | |
128 | } | |
129 | ++stack->top; | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static inline | |
134 | int vstack_pop(struct vstack *stack) | |
135 | { | |
136 | if (unlikely(stack->top < 0)) { | |
137 | ERR("Stack empty\n"); | |
138 | return -EINVAL; | |
139 | } | |
140 | stack->top--; | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* Execution stack */ | |
3151a51d PP |
145 | enum estack_string_literal_type { |
146 | ESTACK_STRING_LITERAL_TYPE_NONE, | |
147 | ESTACK_STRING_LITERAL_TYPE_PLAIN, | |
148 | ESTACK_STRING_LITERAL_TYPE_STAR_GLOB, | |
149 | }; | |
150 | ||
0305960f | 151 | struct estack_entry { |
53569322 | 152 | enum entry_type type; /* For dynamic typing. */ |
0305960f MD |
153 | union { |
154 | int64_t v; | |
155 | double d; | |
156 | ||
157 | struct { | |
158 | const char *str; | |
9b33aac4 | 159 | size_t seq_len; |
3151a51d | 160 | enum estack_string_literal_type literal_type; |
0305960f MD |
161 | } s; |
162 | } u; | |
163 | }; | |
97b58163 | 164 | |
0305960f MD |
165 | struct estack { |
166 | int top; /* top of stack */ | |
167 | struct estack_entry e[FILTER_STACK_LEN]; | |
97b58163 MD |
168 | }; |
169 | ||
53569322 MD |
170 | /* |
171 | * Always use aliased type for ax/bx (top of stack). | |
172 | * When ax/bx are S64, use aliased value. | |
173 | */ | |
9b33aac4 MD |
174 | #define estack_ax_v ax |
175 | #define estack_bx_v bx | |
53569322 MD |
176 | #define estack_ax_t ax_t |
177 | #define estack_bx_t bx_t | |
9b33aac4 | 178 | |
53569322 MD |
179 | /* |
180 | * ax and bx registers can hold either integer, double or string. | |
181 | */ | |
9b33aac4 MD |
182 | #define estack_ax(stack, top) \ |
183 | ({ \ | |
184 | assert((top) > FILTER_STACK_EMPTY); \ | |
185 | &(stack)->e[top]; \ | |
186 | }) | |
187 | ||
188 | #define estack_bx(stack, top) \ | |
189 | ({ \ | |
190 | assert((top) > FILTER_STACK_EMPTY + 1); \ | |
191 | &(stack)->e[(top) - 1]; \ | |
192 | }) | |
193 | ||
53569322 MD |
194 | /* |
195 | * Currently, only integers (REG_S64) can be pushed into the stack. | |
196 | */ | |
197 | #define estack_push(stack, top, ax, bx, ax_t, bx_t) \ | |
9b33aac4 MD |
198 | do { \ |
199 | assert((top) < FILTER_STACK_LEN - 1); \ | |
200 | (stack)->e[(top) - 1].u.v = (bx); \ | |
53569322 | 201 | (stack)->e[(top) - 1].type = (bx_t); \ |
9b33aac4 | 202 | (bx) = (ax); \ |
53569322 | 203 | (bx_t) = (ax_t); \ |
9b33aac4 MD |
204 | ++(top); \ |
205 | } while (0) | |
206 | ||
53569322 | 207 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
9b33aac4 MD |
208 | do { \ |
209 | assert((top) > FILTER_STACK_EMPTY); \ | |
210 | (ax) = (bx); \ | |
53569322 | 211 | (ax_t) = (bx_t); \ |
9b33aac4 | 212 | (bx) = (stack)->e[(top) - 2].u.v; \ |
53569322 | 213 | (bx_t) = (stack)->e[(top) - 2].type; \ |
9b33aac4 MD |
214 | (top)--; \ |
215 | } while (0) | |
0305960f | 216 | |
97b58163 MD |
217 | const char *print_op(enum filter_op op); |
218 | ||
219 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); | |
220 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); | |
221 | ||
8a92ed2a | 222 | uint64_t lttng_filter_false(void *filter_data, |
97b58163 | 223 | const char *filter_stack_data); |
8a92ed2a | 224 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
97b58163 MD |
225 | const char *filter_stack_data); |
226 | ||
227 | #endif /* _LTTNG_FILTER_H */ |