Commit | Line | Data |
---|---|---|
2d78951a MD |
1 | /* |
2 | * lttng-filter.c | |
3 | * | |
4 | * LTTng UST filter code. | |
5 | * | |
6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
f488575f | 23 | #include <urcu/rculist.h> |
97b58163 | 24 | #include "lttng-filter.h" |
cd54f6d9 MD |
25 | |
26 | static const char *opnames[] = { | |
27 | [ FILTER_OP_UNKNOWN ] = "UNKNOWN", | |
28 | ||
29 | [ FILTER_OP_RETURN ] = "RETURN", | |
30 | ||
31 | /* binary */ | |
32 | [ FILTER_OP_MUL ] = "MUL", | |
33 | [ FILTER_OP_DIV ] = "DIV", | |
34 | [ FILTER_OP_MOD ] = "MOD", | |
35 | [ FILTER_OP_PLUS ] = "PLUS", | |
36 | [ FILTER_OP_MINUS ] = "MINUS", | |
37 | [ FILTER_OP_RSHIFT ] = "RSHIFT", | |
38 | [ FILTER_OP_LSHIFT ] = "LSHIFT", | |
39 | [ FILTER_OP_BIN_AND ] = "BIN_AND", | |
40 | [ FILTER_OP_BIN_OR ] = "BIN_OR", | |
41 | [ FILTER_OP_BIN_XOR ] = "BIN_XOR", | |
226106c0 MD |
42 | |
43 | /* binary comparators */ | |
cd54f6d9 MD |
44 | [ FILTER_OP_EQ ] = "EQ", |
45 | [ FILTER_OP_NE ] = "NE", | |
46 | [ FILTER_OP_GT ] = "GT", | |
47 | [ FILTER_OP_LT ] = "LT", | |
48 | [ FILTER_OP_GE ] = "GE", | |
49 | [ FILTER_OP_LE ] = "LE", | |
50 | ||
226106c0 MD |
51 | /* string binary comparators */ |
52 | [ FILTER_OP_EQ_STRING ] = "EQ_STRING", | |
53 | [ FILTER_OP_NE_STRING ] = "NE_STRING", | |
54 | [ FILTER_OP_GT_STRING ] = "GT_STRING", | |
55 | [ FILTER_OP_LT_STRING ] = "LT_STRING", | |
56 | [ FILTER_OP_GE_STRING ] = "GE_STRING", | |
57 | [ FILTER_OP_LE_STRING ] = "LE_STRING", | |
58 | ||
59 | /* s64 binary comparators */ | |
60 | [ FILTER_OP_EQ_S64 ] = "EQ_S64", | |
61 | [ FILTER_OP_NE_S64 ] = "NE_S64", | |
62 | [ FILTER_OP_GT_S64 ] = "GT_S64", | |
63 | [ FILTER_OP_LT_S64 ] = "LT_S64", | |
64 | [ FILTER_OP_GE_S64 ] = "GE_S64", | |
65 | [ FILTER_OP_LE_S64 ] = "LE_S64", | |
66 | ||
67 | /* double binary comparators */ | |
68 | [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE", | |
69 | [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE", | |
70 | [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE", | |
71 | [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE", | |
72 | [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE", | |
73 | [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE", | |
74 | ||
1e5f62b4 MD |
75 | /* Mixed S64-double binary comparators */ |
76 | [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64", | |
77 | [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64", | |
78 | [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64", | |
79 | [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64", | |
80 | [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64", | |
81 | [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64", | |
82 | ||
83 | [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE", | |
84 | [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE", | |
85 | [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE", | |
86 | [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE", | |
87 | [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE", | |
88 | [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE", | |
226106c0 | 89 | |
cd54f6d9 MD |
90 | /* unary */ |
91 | [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS", | |
92 | [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS", | |
93 | [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT", | |
226106c0 MD |
94 | [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64", |
95 | [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64", | |
96 | [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64", | |
97 | [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE", | |
98 | [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE", | |
99 | [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE", | |
cd54f6d9 MD |
100 | |
101 | /* logical */ | |
102 | [ FILTER_OP_AND ] = "AND", | |
103 | [ FILTER_OP_OR ] = "OR", | |
104 | ||
105 | /* load */ | |
106 | [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF", | |
2f0145d1 MD |
107 | [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING", |
108 | [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE", | |
109 | [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64", | |
110 | [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE", | |
111 | ||
cd54f6d9 MD |
112 | [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING", |
113 | [ FILTER_OP_LOAD_S64 ] = "LOAD_S64", | |
da6eed25 | 114 | [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE", |
49905038 MD |
115 | |
116 | /* cast */ | |
117 | [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64", | |
118 | [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64", | |
119 | [ FILTER_OP_CAST_NOP ] = "CAST_NOP", | |
cd54f6d9 MD |
120 | }; |
121 | ||
cd54f6d9 MD |
122 | const char *print_op(enum filter_op op) |
123 | { | |
124 | if (op >= NR_FILTER_OPS) | |
125 | return "UNKNOWN"; | |
126 | else | |
127 | return opnames[op]; | |
128 | } | |
129 | ||
cd54f6d9 MD |
130 | static |
131 | int apply_field_reloc(struct ltt_event *event, | |
132 | struct bytecode_runtime *runtime, | |
133 | uint32_t runtime_len, | |
134 | uint32_t reloc_offset, | |
135 | const char *field_name) | |
136 | { | |
137 | const struct lttng_event_desc *desc; | |
138 | const struct lttng_event_field *fields, *field = NULL; | |
139 | unsigned int nr_fields, i; | |
140 | struct field_ref *field_ref; | |
2f0145d1 | 141 | struct load_op *op; |
cd54f6d9 MD |
142 | uint32_t field_offset = 0; |
143 | ||
a8c27c7c | 144 | dbg_printf("Apply reloc: %u %s\n", reloc_offset, field_name); |
cd54f6d9 MD |
145 | |
146 | /* Ensure that the reloc is within the code */ | |
147 | if (runtime_len - reloc_offset < sizeof(uint16_t)) | |
148 | return -EINVAL; | |
149 | ||
150 | /* Lookup event by name */ | |
151 | desc = event->desc; | |
152 | if (!desc) | |
153 | return -EINVAL; | |
154 | fields = desc->fields; | |
155 | if (!fields) | |
156 | return -EINVAL; | |
157 | nr_fields = desc->nr_fields; | |
158 | for (i = 0; i < nr_fields; i++) { | |
159 | if (!strcmp(fields[i].name, field_name)) { | |
160 | field = &fields[i]; | |
161 | break; | |
162 | } | |
163 | /* compute field offset */ | |
164 | switch (fields[i].type.atype) { | |
165 | case atype_integer: | |
166 | case atype_enum: | |
167 | field_offset += sizeof(int64_t); | |
168 | break; | |
169 | case atype_array: | |
170 | case atype_sequence: | |
171 | field_offset += sizeof(unsigned long); | |
172 | field_offset += sizeof(void *); | |
173 | break; | |
174 | case atype_string: | |
175 | field_offset += sizeof(void *); | |
176 | break; | |
177 | case atype_float: | |
178 | field_offset += sizeof(double); | |
da6eed25 | 179 | break; |
cd54f6d9 MD |
180 | default: |
181 | return -EINVAL; | |
182 | } | |
183 | } | |
184 | if (!field) | |
185 | return -EINVAL; | |
186 | ||
187 | /* Check if field offset is too large for 16-bit offset */ | |
5b4839a8 | 188 | if (field_offset > FILTER_BYTECODE_MAX_LEN - 1) |
cd54f6d9 MD |
189 | return -EINVAL; |
190 | ||
191 | /* set type */ | |
2f0145d1 MD |
192 | op = (struct load_op *) &runtime->data[reloc_offset]; |
193 | field_ref = (struct field_ref *) op->data; | |
cd54f6d9 MD |
194 | switch (field->type.atype) { |
195 | case atype_integer: | |
196 | case atype_enum: | |
2f0145d1 | 197 | op->op = FILTER_OP_LOAD_FIELD_REF_S64; |
cd54f6d9 MD |
198 | break; |
199 | case atype_array: | |
200 | case atype_sequence: | |
2f0145d1 | 201 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; |
cd54f6d9 MD |
202 | break; |
203 | case atype_string: | |
2f0145d1 | 204 | op->op = FILTER_OP_LOAD_FIELD_REF_STRING; |
cd54f6d9 MD |
205 | break; |
206 | case atype_float: | |
2f0145d1 | 207 | op->op = FILTER_OP_LOAD_FIELD_REF_DOUBLE; |
da6eed25 | 208 | break; |
cd54f6d9 MD |
209 | default: |
210 | return -EINVAL; | |
211 | } | |
212 | /* set offset */ | |
213 | field_ref->offset = (uint16_t) field_offset; | |
2d78951a MD |
214 | return 0; |
215 | } | |
216 | ||
f488575f MD |
217 | static |
218 | int bytecode_is_linked(struct lttng_ust_filter_bytecode_node *filter_bytecode, | |
219 | struct ltt_event *event) | |
220 | { | |
221 | struct lttng_bytecode_runtime *bc_runtime; | |
222 | ||
223 | cds_list_for_each_entry(bc_runtime, &event->bytecode_runtime, node) { | |
224 | if (bc_runtime->bc == filter_bytecode) | |
225 | return 1; | |
226 | } | |
227 | return 0; | |
228 | } | |
229 | ||
cd54f6d9 MD |
230 | /* |
231 | * Take a bytecode with reloc table and link it to an event to create a | |
232 | * bytecode runtime. | |
233 | */ | |
2d78951a MD |
234 | static |
235 | int _lttng_filter_event_link_bytecode(struct ltt_event *event, | |
f488575f | 236 | struct lttng_ust_filter_bytecode_node *filter_bytecode) |
2d78951a | 237 | { |
cd54f6d9 MD |
238 | int ret, offset, next_offset; |
239 | struct bytecode_runtime *runtime = NULL; | |
240 | size_t runtime_alloc_len; | |
241 | ||
2d78951a MD |
242 | if (!filter_bytecode) |
243 | return 0; | |
f488575f | 244 | /* Event is not connected to any description */ |
cd54f6d9 MD |
245 | if (!event->desc) |
246 | return 0; | |
247 | /* Bytecode already linked */ | |
f488575f | 248 | if (bytecode_is_linked(filter_bytecode, event)) |
cd54f6d9 | 249 | return 0; |
2d78951a | 250 | |
f488575f | 251 | dbg_printf("Linking...\n"); |
cd54f6d9 MD |
252 | |
253 | /* We don't need the reloc table in the runtime */ | |
f488575f | 254 | runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset; |
cd54f6d9 MD |
255 | runtime = zmalloc(runtime_alloc_len); |
256 | if (!runtime) { | |
257 | ret = -ENOMEM; | |
258 | goto link_error; | |
259 | } | |
f488575f MD |
260 | runtime->p.bc = filter_bytecode; |
261 | runtime->len = filter_bytecode->bc.reloc_offset; | |
cd54f6d9 | 262 | /* copy original bytecode */ |
f488575f | 263 | memcpy(runtime->data, filter_bytecode->bc.data, runtime->len); |
cd54f6d9 MD |
264 | /* |
265 | * apply relocs. Those are a uint16_t (offset in bytecode) | |
266 | * followed by a string (field name). | |
267 | */ | |
f488575f MD |
268 | for (offset = filter_bytecode->bc.reloc_offset; |
269 | offset < filter_bytecode->bc.len; | |
cd54f6d9 MD |
270 | offset = next_offset) { |
271 | uint16_t reloc_offset = | |
f488575f | 272 | *(uint16_t *) &filter_bytecode->bc.data[offset]; |
cd54f6d9 | 273 | const char *field_name = |
f488575f | 274 | (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)]; |
cd54f6d9 MD |
275 | |
276 | ret = apply_field_reloc(event, runtime, runtime->len, reloc_offset, field_name); | |
277 | if (ret) { | |
278 | goto link_error; | |
279 | } | |
280 | next_offset = offset + sizeof(uint16_t) + strlen(field_name) + 1; | |
281 | } | |
9522a886 MD |
282 | /* Validate bytecode */ |
283 | ret = lttng_filter_validate_bytecode(runtime); | |
284 | if (ret) { | |
285 | goto link_error; | |
286 | } | |
08c84b15 MD |
287 | /* Specialize bytecode */ |
288 | ret = lttng_filter_specialize_bytecode(runtime); | |
289 | if (ret) { | |
290 | goto link_error; | |
291 | } | |
f488575f MD |
292 | runtime->p.filter = lttng_filter_interpret_bytecode; |
293 | /* TODO: add with prio */ | |
294 | cds_list_add_rcu(&runtime->p.node, &event->bytecode_runtime); | |
295 | dbg_printf("Linking successful.\n"); | |
2d78951a | 296 | return 0; |
cd54f6d9 MD |
297 | |
298 | link_error: | |
f488575f MD |
299 | runtime->p.filter = lttng_filter_false; |
300 | /* TODO: add with prio */ | |
301 | cds_list_add_rcu(&runtime->p.node, &event->bytecode_runtime); | |
302 | dbg_printf("Linking failed.\n"); | |
cd54f6d9 | 303 | return ret; |
2d78951a MD |
304 | } |
305 | ||
f488575f | 306 | void lttng_filter_event_link_bytecode(struct ltt_event *event) |
2d78951a | 307 | { |
f488575f | 308 | struct lttng_ust_filter_bytecode_node *filter_bytecode; |
2d78951a MD |
309 | int ret; |
310 | ||
f488575f MD |
311 | cds_list_for_each_entry(filter_bytecode, &event->filter_bytecode, node) { |
312 | dbg_printf("linking bytecode\n"); | |
313 | ret = _lttng_filter_event_link_bytecode(event, filter_bytecode); | |
314 | if (ret) { | |
315 | dbg_printf("[lttng filter] warning: cannot link event bytecode\n"); | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
320 | void lttng_filter_event_link_wildcard_bytecode(struct ltt_event *event, | |
321 | struct session_wildcard *wildcard) | |
322 | { | |
323 | struct lttng_ust_filter_bytecode_node *filter_bytecode; | |
324 | int ret; | |
325 | ||
326 | cds_list_for_each_entry(filter_bytecode, &wildcard->filter_bytecode, node) { | |
327 | dbg_printf("linking bytecode\n"); | |
328 | ret = _lttng_filter_event_link_bytecode(event, filter_bytecode); | |
329 | if (ret) { | |
330 | dbg_printf("[lttng filter] error linking wildcard bytecode\n"); | |
331 | } | |
2d78951a MD |
332 | } |
333 | } | |
334 | ||
335 | /* | |
f488575f MD |
336 | * Link bytecode to all events for a wildcard. |
337 | * The "is_linked" check in _lttng_filter_event_link_bytecode() ensures | |
338 | * that we don't link the same bytecode to an event more than once. | |
2d78951a MD |
339 | */ |
340 | void lttng_filter_wildcard_link_bytecode(struct session_wildcard *wildcard) | |
341 | { | |
342 | struct ltt_event *event; | |
2d78951a | 343 | |
f488575f | 344 | if (cds_list_empty(&wildcard->filter_bytecode)) |
2d78951a | 345 | return; |
2d78951a | 346 | cds_list_for_each_entry(event, &wildcard->events, wildcard_list) { |
f488575f MD |
347 | dbg_printf("linking bytecode\n"); |
348 | lttng_filter_event_link_wildcard_bytecode(event, wildcard); | |
2d78951a MD |
349 | } |
350 | return; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Need to attach filter to an event before starting tracing for the | |
cd54f6d9 | 355 | * session. We own the filter_bytecode if we return success. |
2d78951a MD |
356 | */ |
357 | int lttng_filter_event_attach_bytecode(struct ltt_event *event, | |
f488575f | 358 | struct lttng_ust_filter_bytecode_node *filter_bytecode) |
2d78951a | 359 | { |
f488575f | 360 | cds_list_add(&filter_bytecode->node, &event->filter_bytecode); |
2d78951a MD |
361 | return 0; |
362 | } | |
363 | ||
364 | /* | |
365 | * Need to attach filter to a wildcard before starting tracing for the | |
cd54f6d9 | 366 | * session. We own the filter_bytecode if we return success. |
2d78951a MD |
367 | */ |
368 | int lttng_filter_wildcard_attach_bytecode(struct session_wildcard *wildcard, | |
f488575f | 369 | struct lttng_ust_filter_bytecode_node *filter_bytecode) |
2d78951a | 370 | { |
f488575f | 371 | cds_list_add(&filter_bytecode->node, &wildcard->filter_bytecode); |
2d78951a MD |
372 | return 0; |
373 | } | |
f488575f MD |
374 | |
375 | void lttng_free_event_filter_bytecode(struct ltt_event *event) | |
376 | { | |
377 | struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp; | |
378 | ||
379 | cds_list_for_each_entry_safe(filter_bytecode, tmp, | |
380 | &event->filter_bytecode, node) { | |
381 | free(filter_bytecode); | |
382 | } | |
383 | } | |
384 | ||
385 | void lttng_free_wildcard_filter_bytecode(struct session_wildcard *wildcard) | |
386 | { | |
387 | struct lttng_ust_filter_bytecode_node *filter_bytecode, *tmp; | |
388 | ||
389 | cds_list_for_each_entry_safe(filter_bytecode, tmp, | |
390 | &wildcard->filter_bytecode, node) { | |
391 | free(filter_bytecode); | |
392 | } | |
393 | } | |
394 | ||
395 | void lttng_free_event_filter_runtime(struct ltt_event *event) | |
396 | { | |
397 | struct bytecode_runtime *runtime, *tmp; | |
398 | ||
399 | cds_list_for_each_entry_safe(runtime, tmp, | |
400 | &event->bytecode_runtime, p.node) { | |
401 | free(runtime); | |
402 | } | |
403 | } |